mirror of
https://github.com/ipxe/ipxe
synced 2026-02-14 02:31:26 +03:00
[efi] Allow for sections to be excluded from the generated PE file
Hybrid bzImage and UEFI binaries (such as wimboot) include a bzImage header within a section starting at offset zero, with the PE header effectively occupying unused space within this section. This section should not appear as a named section in the resulting PE file. Allow for the existence of hidden sections that do not result in a section header being written to the PE file. Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
@@ -181,6 +181,7 @@ struct pe_section {
|
|||||||
struct pe_section *next;
|
struct pe_section *next;
|
||||||
EFI_IMAGE_SECTION_HEADER hdr;
|
EFI_IMAGE_SECTION_HEADER hdr;
|
||||||
void ( * fixup ) ( struct pe_section *section );
|
void ( * fixup ) ( struct pe_section *section );
|
||||||
|
int hidden;
|
||||||
uint8_t contents[0];
|
uint8_t contents[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -641,10 +642,12 @@ static struct pe_section * process_section ( struct elf_file *elf,
|
|||||||
/* Update RVA limits */
|
/* Update RVA limits */
|
||||||
start = new->hdr.VirtualAddress;
|
start = new->hdr.VirtualAddress;
|
||||||
end = ( start + new->hdr.Misc.VirtualSize );
|
end = ( start + new->hdr.Misc.VirtualSize );
|
||||||
if ( ( ! *applicable_start ) || ( *applicable_start >= start ) )
|
if ( ! new->hidden ) {
|
||||||
*applicable_start = start;
|
if ( ( ! *applicable_start ) || ( *applicable_start >= start ) )
|
||||||
if ( *applicable_end < end )
|
*applicable_start = start;
|
||||||
*applicable_end = end;
|
if ( *applicable_end < end )
|
||||||
|
*applicable_end = end;
|
||||||
|
}
|
||||||
if ( data_start < code_end )
|
if ( data_start < code_end )
|
||||||
data_start = code_end;
|
data_start = code_end;
|
||||||
if ( data_mid < data_start )
|
if ( data_mid < data_start )
|
||||||
@@ -664,8 +667,11 @@ static struct pe_section * process_section ( struct elf_file *elf,
|
|||||||
( data_end - data_mid );
|
( data_end - data_mid );
|
||||||
|
|
||||||
/* Update remaining file header fields */
|
/* Update remaining file header fields */
|
||||||
pe_header->nt.FileHeader.NumberOfSections++;
|
if ( ! new->hidden ) {
|
||||||
pe_header->nt.OptionalHeader.SizeOfHeaders += sizeof ( new->hdr );
|
pe_header->nt.FileHeader.NumberOfSections++;
|
||||||
|
pe_header->nt.OptionalHeader.SizeOfHeaders +=
|
||||||
|
sizeof ( new->hdr );
|
||||||
|
}
|
||||||
pe_header->nt.OptionalHeader.SizeOfImage =
|
pe_header->nt.OptionalHeader.SizeOfImage =
|
||||||
efi_image_align ( data_end );
|
efi_image_align ( data_end );
|
||||||
|
|
||||||
@@ -935,6 +941,7 @@ static void write_pe_file ( struct pe_header *pe_header,
|
|||||||
FILE *pe ) {
|
FILE *pe ) {
|
||||||
struct pe_section *section;
|
struct pe_section *section;
|
||||||
unsigned long fpos = 0;
|
unsigned long fpos = 0;
|
||||||
|
unsigned int count = 0;
|
||||||
|
|
||||||
/* Align length of headers */
|
/* Align length of headers */
|
||||||
fpos = pe_header->nt.OptionalHeader.SizeOfHeaders =
|
fpos = pe_header->nt.OptionalHeader.SizeOfHeaders =
|
||||||
@@ -962,12 +969,16 @@ static void write_pe_file ( struct pe_header *pe_header,
|
|||||||
|
|
||||||
/* Write section headers */
|
/* Write section headers */
|
||||||
for ( section = pe_sections ; section ; section = section->next ) {
|
for ( section = pe_sections ; section ; section = section->next ) {
|
||||||
|
if ( section->hidden )
|
||||||
|
continue;
|
||||||
if ( fwrite ( §ion->hdr, sizeof ( section->hdr ),
|
if ( fwrite ( §ion->hdr, sizeof ( section->hdr ),
|
||||||
1, pe ) != 1 ) {
|
1, pe ) != 1 ) {
|
||||||
perror ( "Could not write section header" );
|
perror ( "Could not write section header" );
|
||||||
exit ( 1 );
|
exit ( 1 );
|
||||||
}
|
}
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
assert ( count == pe_header->nt.FileHeader.NumberOfSections );
|
||||||
|
|
||||||
/* Write sections */
|
/* Write sections */
|
||||||
for ( section = pe_sections ; section ; section = section->next ) {
|
for ( section = pe_sections ; section ; section = section->next ) {
|
||||||
|
|||||||
Reference in New Issue
Block a user