]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[efi] Place PE debug information in a hidden section
authorMichael Brown <mcb30@ipxe.org>
Thu, 23 Nov 2023 14:17:36 +0000 (14:17 +0000)
committerMichael Brown <mcb30@ipxe.org>
Thu, 23 Nov 2023 15:43:21 +0000 (15:43 +0000)
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>
src/util/elf2efi.c

index 76d02f2499ca626f0eaf9f41a391f85a40ae742a..c98c4d4a909fb2d74a7d3d53e5e06d52b595e3fb 100644 (file)
@@ -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];
 };
@@ -919,20 +918,6 @@ create_reloc_section ( struct pe_header *pe_header,
        return reloc;
 }
 
-/**
- * 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
  *
@@ -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 );