aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brown <mcb30@ipxe.org>2023-04-10 17:05:05 +0100
committerMichael Brown <mcb30@ipxe.org>2023-04-10 17:05:05 +0100
commitdf1767642f9f3cfd6e4dcd7781b49ecac4fc6ef8 (patch)
tree2042e0ed0e385d78adc5f46054c0e12ad9b56460
parent1f2a29edb016abcf22132c50f2ca7d360c1ef1d0 (diff)
downloadipxe-wimboot.tar.gz
-rw-r--r--src/util/elf2efi.c39
1 files changed, 35 insertions, 4 deletions
diff --git a/src/util/elf2efi.c b/src/util/elf2efi.c
index 5e17751bd..295f22f42 100644
--- a/src/util/elf2efi.c
+++ b/src/util/elf2efi.c
@@ -730,6 +730,37 @@ static struct pe_section * process_section ( struct elf_file *elf,
}
/**
+ * Update image base address
+ *
+ * @v pe_header PE file header
+ * @v pe_sections List of PE sections
+ */
+static void update_image_base ( struct pe_header *pe_header,
+ struct pe_section *pe_sections ) {
+ struct pe_section *section;
+ unsigned long base;
+
+ /* Set ImageBase to the highest possible value, leaving space
+ * for the PE header itself (if not deliberately overlapped).
+ */
+ for ( section = pe_sections ; section ; section = section->next ) {
+ base = ( section->hdr.VirtualAddress -
+ section->hdr.PointerToRawData );
+ if ( ( pe_header->nt.OptionalHeader.ImageBase == 0 ) ||
+ ( pe_header->nt.OptionalHeader.ImageBase > base ) ) {
+ pe_header->nt.OptionalHeader.ImageBase = base;
+ }
+ }
+ base = pe_header->nt.OptionalHeader.ImageBase;
+
+ /* Adjust RVA of all sections to match ImageBase */
+ for ( section = pe_sections ; section ; section = section->next ) {
+ section->hdr.VirtualAddress -= base;
+ }
+ pe_header->nt.OptionalHeader.SizeOfImage -= base;
+}
+
+/**
* Process relocation record
*
* @v elf ELF file
@@ -956,6 +987,7 @@ create_debug_section ( struct pe_header *pe_header, const char *filename ) {
EFI_IMAGE_SCN_MEM_NOT_PAGED |
EFI_IMAGE_SCN_MEM_READ );
debug->fixup = fixup_debug_section;
+ debug->hidden = 1;
/* Create section contents */
contents->debug.TimeDateStamp = 0x10d1a884;
@@ -970,10 +1002,6 @@ create_debug_section ( struct pe_header *pe_header, const char *filename ) {
filename );
/* Update file header details */
- pe_header->nt.FileHeader.NumberOfSections++;
- pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( debug->hdr );
- pe_header->nt.OptionalHeader.SizeOfImage +=
- efi_image_align ( section_memsz );
debugdir = &(pe_header->nt.OptionalHeader.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
debugdir->VirtualAddress = debug->hdr.VirtualAddress;
@@ -1125,6 +1153,9 @@ static void elf2pe ( const char *elf_name, const char *pe_name,
}
}
+ /* Update image base address */
+ update_image_base ( &pe_header, pe_sections );
+
/* Create the .reloc section */
*(next_pe_section) = create_reloc_section ( &pe_header, pe_reltab );
next_pe_section = &(*next_pe_section)->next;