summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brown2023-11-23 15:17:36 +0100
committerMichael Brown2023-11-23 16:43:21 +0100
commit6714b20ea20af5046cdb36b58ab777b55a3c003c (patch)
tree6125c6fed24cec675145261a52d816e13303b2a6
parent[efi] Fix recorded overall size of headers in NT optional header (diff)
downloadipxe-6714b20ea20af5046cdb36b58ab777b55a3c003c.tar.gz
ipxe-6714b20ea20af5046cdb36b58ab777b55a3c003c.tar.xz
ipxe-6714b20ea20af5046cdb36b58ab777b55a3c003c.zip
[efi] Place PE debug information in a hidden section
The PE debug information generated by elf2efi is used only to hold the image filename, and the debug information is located via the relevant data directory entry rather than via the section table. Make the .debug section a hidden section in order to save one entry in the PE section list. Choose to place the debug information in the unused space at the end of the PE headers, since it no longer needs to satisfy the general section alignment constraints. Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/util/elf2efi.c46
1 files changed, 15 insertions, 31 deletions
diff --git a/src/util/elf2efi.c b/src/util/elf2efi.c
index 76d02f24..c98c4d4a 100644
--- a/src/util/elf2efi.c
+++ b/src/util/elf2efi.c
@@ -185,7 +185,6 @@ struct elf_file {
struct pe_section {
struct pe_section *next;
EFI_IMAGE_SECTION_HEADER hdr;
- void ( * fixup ) ( struct pe_section *section );
int hidden;
uint8_t contents[0];
};
@@ -920,20 +919,6 @@ create_reloc_section ( struct pe_header *pe_header,
}
/**
- * Fix up debug section
- *
- * @v debug Debug section
- */
-static void fixup_debug_section ( struct pe_section *debug ) {
- EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *contents;
-
- /* Fix up FileOffset */
- contents = ( ( void * ) debug->contents );
- contents->FileOffset += ( debug->hdr.PointerToRawData -
- debug->hdr.VirtualAddress );
-}
-
-/**
* Create debug section
*
* @v pe_header PE file header
@@ -952,24 +937,27 @@ create_debug_section ( struct pe_header *pe_header, const char *filename ) {
} *contents;
/* Allocate PE section */
- section_memsz = sizeof ( *contents );
- section_filesz = efi_file_align ( section_memsz );
+ section_filesz = section_memsz = sizeof ( *contents );
debug = xmalloc ( sizeof ( *debug ) + section_filesz );
memset ( debug, 0, sizeof ( *debug ) + section_filesz );
contents = ( void * ) debug->contents;
+ /* Place at end of headers */
+ pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( *contents );
+ pe_header->nt.OptionalHeader.SizeOfHeaders =
+ efi_file_align ( pe_header->nt.OptionalHeader.SizeOfHeaders );
+ pe_header->nt.OptionalHeader.SizeOfHeaders -= sizeof ( *contents );
+
/* Fill in section header details */
strncpy ( ( char * ) debug->hdr.Name, ".debug",
sizeof ( debug->hdr.Name ) );
debug->hdr.Misc.VirtualSize = section_memsz;
- debug->hdr.VirtualAddress = pe_header->nt.OptionalHeader.SizeOfImage;
+ debug->hdr.VirtualAddress =
+ pe_header->nt.OptionalHeader.SizeOfHeaders;
debug->hdr.SizeOfRawData = section_filesz;
- debug->hdr.PointerToRawData = PTRD_AUTO;
- debug->hdr.Characteristics = ( EFI_IMAGE_SCN_CNT_INITIALIZED_DATA |
- EFI_IMAGE_SCN_MEM_DISCARDABLE |
- EFI_IMAGE_SCN_MEM_NOT_PAGED |
- EFI_IMAGE_SCN_MEM_READ );
- debug->fixup = fixup_debug_section;
+ debug->hdr.PointerToRawData =
+ pe_header->nt.OptionalHeader.SizeOfHeaders;
+ debug->hidden = 1;
/* Create section contents */
contents->debug.TimeDateStamp = 0x10d1a884;
@@ -984,10 +972,7 @@ 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 );
+ pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( *contents );
debugdir = &(pe_header->nt.OptionalHeader.DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
debugdir->VirtualAddress = debug->hdr.VirtualAddress;
@@ -1027,13 +1012,12 @@ static void write_pe_file ( struct pe_header *pe_header,
fpos = efi_file_align ( fpos );
if ( fpos > fposmax )
fposmax = fpos;
- if ( section->fixup )
- section->fixup ( section );
}
/* Write sections */
for ( section = pe_sections ; section ; section = section->next ) {
- if ( section->hdr.PointerToRawData & ( EFI_FILE_ALIGN - 1 ) ) {
+ if ( ( section->hdr.PointerToRawData & ( EFI_FILE_ALIGN - 1 ) )
+ && ( ! section->hidden ) ) {
eprintf ( "Section %.8s file offset %x is "
"misaligned\n", section->hdr.Name,
section->hdr.PointerToRawData );