diff --git a/BaseTools/Source/C/GenFw/Elf32Convert.c b/BaseTools/Source/C/GenFw/Elf32Convert.c index 5c7b689ee8..10d9892ba1 100644 --- a/BaseTools/Source/C/GenFw/Elf32Convert.c +++ b/BaseTools/Source/C/GenFw/Elf32Convert.c @@ -96,7 +96,7 @@ STATIC Elf_Phdr *mPhdrBase; // // Coff information // -STATIC const UINT32 mCoffAlignment = 0x20; +STATIC UINT32 mCoffAlignment = 0x20; // // PE section alignment. @@ -291,6 +291,20 @@ ScanSections32 ( mTableOffset = mCoffOffset; mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER); + // + // Set mCoffAlignment to the maximum alignment of the input sections + // we care about + // + for (i = 0; i < mEhdr->e_shnum; i++) { + Elf_Shdr *shdr = GetShdrByIndex(i); + if (shdr->sh_addralign <= mCoffAlignment) { + continue; + } + if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) { + mCoffAlignment = (UINT32)shdr->sh_addralign; + } + } + // // First text sections. // diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c index 25b90e2f7b..d2becf165f 100644 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -97,7 +97,7 @@ STATIC Elf_Phdr *mPhdrBase; // // Coff information // -STATIC const UINT32 mCoffAlignment = 0x20; +STATIC UINT32 mCoffAlignment = 0x20; // // PE section alignment. @@ -285,6 +285,20 @@ ScanSections64 ( mTableOffset = mCoffOffset; mCoffOffset += mCoffNbrSections * sizeof(EFI_IMAGE_SECTION_HEADER); + // + // Set mCoffAlignment to the maximum alignment of the input sections + // we care about + // + for (i = 0; i < mEhdr->e_shnum; i++) { + Elf_Shdr *shdr = GetShdrByIndex(i); + if (shdr->sh_addralign <= mCoffAlignment) { + continue; + } + if (IsTextShdr(shdr) || IsDataShdr(shdr) || IsHiiRsrcShdr(shdr)) { + mCoffAlignment = (UINT32)shdr->sh_addralign; + } + } + // // First text sections. // diff --git a/BaseTools/Source/C/GenFw/ElfConvert.c b/BaseTools/Source/C/GenFw/ElfConvert.c index 1a84d3c287..6211389ba1 100644 --- a/BaseTools/Source/C/GenFw/ElfConvert.c +++ b/BaseTools/Source/C/GenFw/ElfConvert.c @@ -96,11 +96,11 @@ CoffAddFixup( mCoffFile = realloc ( mCoffFile, - mCoffOffset + sizeof(EFI_IMAGE_BASE_RELOCATION) + 2*0x1000 + mCoffOffset + sizeof(EFI_IMAGE_BASE_RELOCATION) + 2 * MAX_COFF_ALIGNMENT ); memset ( mCoffFile + mCoffOffset, 0, - sizeof(EFI_IMAGE_BASE_RELOCATION) + 2*0x1000 + sizeof(EFI_IMAGE_BASE_RELOCATION) + 2 * MAX_COFF_ALIGNMENT ); mCoffBaseRel = (EFI_IMAGE_BASE_RELOCATION*)(mCoffFile + mCoffOffset); diff --git a/BaseTools/Source/C/GenFw/ElfConvert.h b/BaseTools/Source/C/GenFw/ElfConvert.h index b27a2f9d22..56f165eae5 100644 --- a/BaseTools/Source/C/GenFw/ElfConvert.h +++ b/BaseTools/Source/C/GenFw/ElfConvert.h @@ -34,6 +34,7 @@ extern UINT32 mOutImageType; // Common EFI specific data. // #define ELF_HII_SECTION_NAME ".hii" +#define MAX_COFF_ALIGNMENT 0x10000 // // Filter Types