Browse Source

[efi] Fix the 32-bit version of elf2efi64

Currently, if elf2efi.c is compiled using a 32-bit HOST_CC, then the
resulting elf2efi64 binary will generate 32-bit EFI binaries instead
of 64-bit EFI binaries.

The problem is that elf2efi.c uses the MDE_CPU_* definitions to decide
whether to output a 32-bit or 64-bit PE binary.  However, MDE_CPU_*
gets defined in ProcessorBind.h, depending on the compiler's target
architecture.  Overriding them on the command line doesn't work in the
expected way, and you can end up in cases where both MDE_CPU_IA32 and
MDE_CPU_X64 are defined.

Fix by using a separate definition, EFI_TARGET_IA32/EFI_TARGET_X64,
which is specified only on the command line.

Signed-off-by: Geoff Lywood <glywood@vmware.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Geoff Lywood 14 years ago
parent
commit
b8dd94686b
2 changed files with 12 additions and 12 deletions
  1. 2
    2
      src/Makefile.housekeeping
  2. 10
    10
      src/util/elf2efi.c

+ 2
- 2
src/Makefile.housekeeping View File

907
 
907
 
908
 $(ELF2EFI32) : util/elf2efi.c $(MAKEDEPS)
908
 $(ELF2EFI32) : util/elf2efi.c $(MAKEDEPS)
909
 	$(QM)$(ECHO) "  [HOSTCC] $@"
909
 	$(QM)$(ECHO) "  [HOSTCC] $@"
910
-	$(Q)$(HOST_CC) $< $(ELF2EFI_CFLAGS) -DMDE_CPU_IA32 -O2 -o $@
910
+	$(Q)$(HOST_CC) $< $(ELF2EFI_CFLAGS) -DEFI_TARGET_IA32 -O2 -o $@
911
 CLEANUP += $(ELF2EFI32)
911
 CLEANUP += $(ELF2EFI32)
912
 
912
 
913
 $(ELF2EFI64) : util/elf2efi.c $(MAKEDEPS)
913
 $(ELF2EFI64) : util/elf2efi.c $(MAKEDEPS)
914
 	$(QM)$(ECHO) "  [HOSTCC] $@"
914
 	$(QM)$(ECHO) "  [HOSTCC] $@"
915
-	$(Q)$(HOST_CC) $< $(ELF2EFI_CFLAGS) -DMDE_CPU_X64 -O2 -o $@
915
+	$(Q)$(HOST_CC) $< $(ELF2EFI_CFLAGS) -DEFI_TARGET_X64 -O2 -o $@
916
 CLEANUP += $(ELF2EFI64)
916
 CLEANUP += $(ELF2EFI64)
917
 
917
 
918
 $(EFIROM) : util/efirom.c $(MAKEDEPS)
918
 $(EFIROM) : util/efirom.c $(MAKEDEPS)

+ 10
- 10
src/util/elf2efi.c View File

52
 struct pe_header {
52
 struct pe_header {
53
 	EFI_IMAGE_DOS_HEADER dos;
53
 	EFI_IMAGE_DOS_HEADER dos;
54
 	uint8_t padding[128];
54
 	uint8_t padding[128];
55
-#if defined(MDE_CPU_IA32)
55
+#if defined(EFI_TARGET_IA32)
56
 	EFI_IMAGE_NT_HEADERS32 nt;
56
 	EFI_IMAGE_NT_HEADERS32 nt;
57
-#elif defined(MDE_CPU_X64)
57
+#elif defined(EFI_TARGET_X64)
58
 	EFI_IMAGE_NT_HEADERS64 nt;
58
 	EFI_IMAGE_NT_HEADERS64 nt;
59
 #endif
59
 #endif
60
 };
60
 };
67
 	.nt = {
67
 	.nt = {
68
 		.Signature = EFI_IMAGE_NT_SIGNATURE,
68
 		.Signature = EFI_IMAGE_NT_SIGNATURE,
69
 		.FileHeader = {
69
 		.FileHeader = {
70
-#if defined(MDE_CPU_IA32)
70
+#if defined(EFI_TARGET_IA32)
71
 			.Machine = EFI_IMAGE_MACHINE_IA32,
71
 			.Machine = EFI_IMAGE_MACHINE_IA32,
72
-#elif defined(MDE_CPU_X64)
72
+#elif defined(EFI_TARGET_X64)
73
 			.Machine = EFI_IMAGE_MACHINE_X64,
73
 			.Machine = EFI_IMAGE_MACHINE_X64,
74
 #endif
74
 #endif
75
 			.TimeDateStamp = 0x10d1a884,
75
 			.TimeDateStamp = 0x10d1a884,
76
 			.SizeOfOptionalHeader =
76
 			.SizeOfOptionalHeader =
77
 				sizeof ( efi_pe_header.nt.OptionalHeader ),
77
 				sizeof ( efi_pe_header.nt.OptionalHeader ),
78
 			.Characteristics = ( EFI_IMAGE_FILE_DLL |
78
 			.Characteristics = ( EFI_IMAGE_FILE_DLL |
79
-#if defined(MDE_CPU_IA32)
79
+#if defined(EFI_TARGET_IA32)
80
 					     EFI_IMAGE_FILE_32BIT_MACHINE |
80
 					     EFI_IMAGE_FILE_32BIT_MACHINE |
81
 #endif
81
 #endif
82
 					     EFI_IMAGE_FILE_EXECUTABLE_IMAGE ),
82
 					     EFI_IMAGE_FILE_EXECUTABLE_IMAGE ),
83
 		},
83
 		},
84
 		.OptionalHeader = {
84
 		.OptionalHeader = {
85
-#if defined(MDE_CPU_IA32)
85
+#if defined(EFI_TARGET_IA32)
86
 			.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC,
86
 			.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC,
87
-#elif defined(MDE_CPU_X64)
87
+#elif defined(EFI_TARGET_X64)
88
 			.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC,
88
 			.Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC,
89
 #endif
89
 #endif
90
 			.SectionAlignment = EFI_FILE_ALIGN,
90
 			.SectionAlignment = EFI_FILE_ALIGN,
345
 	/* Extract current RVA limits from file header */
345
 	/* Extract current RVA limits from file header */
346
 	code_start = pe_header->nt.OptionalHeader.BaseOfCode;
346
 	code_start = pe_header->nt.OptionalHeader.BaseOfCode;
347
 	code_end = ( code_start + pe_header->nt.OptionalHeader.SizeOfCode );
347
 	code_end = ( code_start + pe_header->nt.OptionalHeader.SizeOfCode );
348
-#if defined(MDE_CPU_IA32)
348
+#if defined(EFI_TARGET_IA32)
349
 	data_start = pe_header->nt.OptionalHeader.BaseOfData;
349
 	data_start = pe_header->nt.OptionalHeader.BaseOfData;
350
-#elif defined(MDE_CPU_X64)
350
+#elif defined(EFI_TARGET_X64)
351
 	data_start = code_end;
351
 	data_start = code_end;
352
 #endif
352
 #endif
353
 	data_mid = ( data_start +
353
 	data_mid = ( data_start +
434
 	/* Write RVA limits back to file header */
434
 	/* Write RVA limits back to file header */
435
 	pe_header->nt.OptionalHeader.BaseOfCode = code_start;
435
 	pe_header->nt.OptionalHeader.BaseOfCode = code_start;
436
 	pe_header->nt.OptionalHeader.SizeOfCode = ( code_end - code_start );
436
 	pe_header->nt.OptionalHeader.SizeOfCode = ( code_end - code_start );
437
-#if defined(MDE_CPU_IA32)
437
+#if defined(EFI_TARGET_IA32)
438
 	pe_header->nt.OptionalHeader.BaseOfData = data_start;
438
 	pe_header->nt.OptionalHeader.BaseOfData = data_start;
439
 #endif
439
 #endif
440
 	pe_header->nt.OptionalHeader.SizeOfInitializedData =
440
 	pe_header->nt.OptionalHeader.SizeOfInitializedData =

Loading…
Cancel
Save