BaseTools/GenFw: add new option to not zero PE/COFF optional header fields

Add new option --keepoptionalheader and that flag does not zero PE/COFF
optional header fields including the version fields. It can support the
case that the PE/COFF optional header would be kept.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Yonghong Zhu <yonghong.zhu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18767 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
Yonghong Zhu 2015-11-11 06:30:42 +00:00 committed by yzhu52
parent 9a12358cbe
commit 30fbfcc929
1 changed files with 48 additions and 34 deletions

View File

@ -1,7 +1,7 @@
/** @file
Converts a pe32+ image to an FW, Te image type, or other specific image.
Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2004 - 2015, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@ -168,7 +168,7 @@ Returns:
//
// Copyright declaration
//
fprintf (stdout, "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.\n\n");
fprintf (stdout, "Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.\n\n");
//
// Details Option
@ -184,9 +184,9 @@ Returns:
PIC_PEIM, RELOCATABLE_PEIM, BS_DRIVER, RT_DRIVER,\n\
APPLICATION, SAL_RT_DRIVER to support all module types\n\
It can only be used together with --keepexceptiontable,\n\
--keepzeropending, -r, -o option.It is a action option.\n\
If it is combined with other action options, the later\n\
input action option will override the previous one.\n");
--keepzeropending, --keepoptionalheader, -r, -o option.\n\
It is a action option. If it is combined with other action options,\n\
the later input action option will override the previous one.\n");
fprintf (stdout, " -c, --acpi Create Acpi table.\n\
It can't be combined with other action options\n\
except for -o, -r option. It is a action option.\n\
@ -194,9 +194,9 @@ Returns:
input action option will override the previous one.\n");
fprintf (stdout, " -t, --terse Create Te Image.\n\
It can only be used together with --keepexceptiontable,\n\
--keepzeropending, -r, -o option.It is a action option.\n\
If it is combined with other action options, the later\n\
input action option will override the previous one.\n");
--keepzeropending, --keepoptionalheader, -r, -o option.\n\
It is a action option. If it is combined with other action options,\n\
the later input action option will override the previous one.\n");
fprintf (stdout, " -u, --dump Dump TeImage Header.\n\
It can't be combined with other action options\n\
except for -o, -r option. It is a action option.\n\
@ -246,6 +246,9 @@ Returns:
fprintf (stdout, " --keepexceptiontable Don't clear exception table.\n\
This option can be used together with -e or -t.\n\
It doesn't work for other options.\n");
fprintf (stdout, " --keepoptionalheader Don't zero PE/COFF optional header fields.\n\
This option can be used together with -e or -t.\n\
It doesn't work for other options.\n");
fprintf (stdout, " --keepzeropending Don't strip zero pending of .reloc.\n\
This option can be used together with -e or -t.\n\
It doesn't work for other options.\n");
@ -1079,6 +1082,7 @@ Returns:
STATUS Status;
BOOLEAN ReplaceFlag;
BOOLEAN KeepExceptionTableFlag;
BOOLEAN KeepOptionalHeaderFlag;
BOOLEAN KeepZeroPendingFlag;
UINT64 LogLevel;
EFI_TE_IMAGE_HEADER TEImageHeader;
@ -1141,6 +1145,7 @@ Returns:
Optional32 = NULL;
Optional64 = NULL;
KeepExceptionTableFlag = FALSE;
KeepOptionalHeaderFlag = FALSE;
KeepZeroPendingFlag = FALSE;
NumberOfFormPacakge = 0;
HiiPackageListBuffer = NULL;
@ -1269,6 +1274,13 @@ Returns:
continue;
}
if (stricmp(argv[0], "--keepoptionalheader") == 0) {
KeepOptionalHeaderFlag = TRUE;
argc--;
argv++;
continue;
}
if (stricmp (argv[0], "--keepzeropending") == 0) {
KeepZeroPendingFlag = TRUE;
argc --;
@ -2303,6 +2315,7 @@ Returns:
if (PeHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
Optional32 = (EFI_IMAGE_OPTIONAL_HEADER32 *)&PeHdr->Pe32.OptionalHeader;
if (!KeepOptionalHeaderFlag) {
Optional32->MajorOperatingSystemVersion = 0;
Optional32->MinorOperatingSystemVersion = 0;
Optional32->MajorImageVersion = 0;
@ -2315,7 +2328,7 @@ Returns:
Optional32->SizeOfStackCommit = 0;
Optional32->SizeOfHeapReserve = 0;
Optional32->SizeOfHeapCommit = 0;
}
TEImageHeader.AddressOfEntryPoint = Optional32->AddressOfEntryPoint;
TEImageHeader.BaseOfCode = Optional32->BaseOfCode;
TEImageHeader.ImageBase = (UINT64) (Optional32->ImageBase);
@ -2395,6 +2408,7 @@ Returns:
}
} else if (PeHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
Optional64 = (EFI_IMAGE_OPTIONAL_HEADER64 *)&PeHdr->Pe32.OptionalHeader;
if (!KeepOptionalHeaderFlag) {
Optional64->MajorOperatingSystemVersion = 0;
Optional64->MinorOperatingSystemVersion = 0;
Optional64->MajorImageVersion = 0;
@ -2407,7 +2421,7 @@ Returns:
Optional64->SizeOfStackCommit = 0;
Optional64->SizeOfHeapReserve = 0;
Optional64->SizeOfHeapCommit = 0;
}
TEImageHeader.AddressOfEntryPoint = Optional64->AddressOfEntryPoint;
TEImageHeader.BaseOfCode = Optional64->BaseOfCode;
TEImageHeader.ImageBase = (UINT64) (Optional64->ImageBase);