diff options
-rw-r--r-- | src/util/elf2efi.c | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/util/elf2efi.c b/src/util/elf2efi.c index 159f14e..a68154e 100644 --- a/src/util/elf2efi.c +++ b/src/util/elf2efi.c @@ -168,6 +168,9 @@ */ #define EFI_IMAGE_ALIGN 0x1000 +/** Number of data directory entries */ +#define NUMBER_OF_DIRECTORY_ENTRIES 8 + struct elf_file { void *data; size_t len; @@ -204,7 +207,11 @@ static struct pe_header efi_pe_header = { .FileHeader = { .TimeDateStamp = 0x10d1a884, .SizeOfOptionalHeader = - sizeof ( efi_pe_header.nt.OptionalHeader ), + ( sizeof ( efi_pe_header.nt.OptionalHeader ) - + ( ( EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES - + NUMBER_OF_DIRECTORY_ENTRIES ) * + sizeof ( efi_pe_header.nt.OptionalHeader. + DataDirectory[0] ) ) ), .Characteristics = ( EFI_IMAGE_FILE_DLL | EFI_IMAGE_FILE_MACHINE | EFI_IMAGE_FILE_EXECUTABLE_IMAGE ), @@ -217,8 +224,7 @@ static struct pe_header efi_pe_header = { .FileAlignment = EFI_FILE_ALIGN, .SizeOfImage = EFI_IMAGE_ALIGN, .SizeOfHeaders = sizeof ( efi_pe_header ), - .NumberOfRvaAndSizes = - EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES, + .NumberOfRvaAndSizes = NUMBER_OF_DIRECTORY_ENTRIES, }, }, }; @@ -930,7 +936,10 @@ static void write_pe_file ( struct pe_header *pe_header, } /* Write file header */ - if ( fwrite ( pe_header, sizeof ( *pe_header ), 1, pe ) != 1 ) { + if ( fwrite ( pe_header, + ( offsetof ( typeof ( *pe_header ), nt.OptionalHeader ) + + pe_header->nt.FileHeader.SizeOfOptionalHeader ), + 1, pe ) != 1 ) { perror ( "Could not write PE header" ); exit ( 1 ); } |