aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brown <mcb30@ipxe.org>2023-11-24 12:26:43 +0000
committerMichael Brown <mcb30@ipxe.org>2023-11-24 16:18:16 +0000
commitd8a286d52651d44be649c502b4853c131a64829c (patch)
tree3d80eec6c75b07d1b2a1ef66753c0d5ec20fc6a5
parent6d1e3b9f333722d15193a18efe96f0acc517d116 (diff)
downloadipxe-wimboot2.tar.gz
WIP - ImageBasewimboot2
-rw-r--r--src/util/elf2efi.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/util/elf2efi.c b/src/util/elf2efi.c
index 31b2a2bb1..8fa86deef 100644
--- a/src/util/elf2efi.c
+++ b/src/util/elf2efi.c
@@ -729,6 +729,48 @@ 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
+ * @v pe_reltab PE relocation table
+ */
+static void update_image_base ( struct pe_header *pe_header,
+ struct pe_section *pe_sections,
+ struct pe_relocs *pe_reltab ) {
+ struct pe_section *section;
+ struct pe_relocs *pe_rel;
+ 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 RVAs to match ImageBase */
+ pe_header->nt.OptionalHeader.AddressOfEntryPoint -= base;
+ pe_header->nt.OptionalHeader.BaseOfCode -= base;
+#if defined(EFI_TARGET32)
+ pe_header->nt.OptionalHeader.BaseOfData -= base;
+#endif
+ pe_header->nt.OptionalHeader.SizeOfImage -= base;
+ for ( section = pe_sections ; section ; section = section->next ) {
+ section->hdr.VirtualAddress -= base;
+ }
+ for ( pe_rel = pe_reltab ; pe_rel ; pe_rel = pe_rel->next ) {
+ pe_rel->start_rva -= base;
+ }
+}
+
+/**
* Process relocation record
*
* @v elf ELF file
@@ -1112,6 +1154,9 @@ static void elf2pe ( const char *elf_name, const char *pe_name,
}
}
+ /* Update image base address */
+ update_image_base ( &pe_header, pe_sections, pe_reltab );
+
/* Create the .reloc section */
*(next_pe_section) = create_reloc_section ( &pe_header, pe_reltab );
next_pe_section = &(*next_pe_section)->next;