X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FC%2FGenFw%2FElf32Convert.c;h=14fe4a285857d694a2e1ab038f05c14a02d73be7;hp=85e8ba6614086e72411a2216033c8c782bbc0f9c;hb=60e85a39fe49071683f3ac5e208f1582511d26bf;hpb=97fa0ee9b1cffbb4b97ee35365afa7afcf50e174 diff --git a/BaseTools/Source/C/GenFw/Elf32Convert.c b/BaseTools/Source/C/GenFw/Elf32Convert.c index 85e8ba6614..14fe4a2858 100644 --- a/BaseTools/Source/C/GenFw/Elf32Convert.c +++ b/BaseTools/Source/C/GenFw/Elf32Convert.c @@ -1,7 +1,7 @@ /** @file Elf32 Convert solution -Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
Portions copyright (c) 2013, ARM Ltd. All rights reserved.
This program and the accompanying materials are licensed and made available @@ -96,12 +96,12 @@ STATIC Elf_Phdr *mPhdrBase; // // Coff information // -STATIC const UINT32 mCoffAlignment = 0x20; +STATIC UINT32 mCoffAlignment = 0x20; // // PE section alignment. // -STATIC const UINT16 mCoffNbrSections = 5; +STATIC const UINT16 mCoffNbrSections = 4; // // ELF sections to offset in Coff file. @@ -116,6 +116,7 @@ STATIC UINT32 mTextOffset; STATIC UINT32 mDataOffset; STATIC UINT32 mHiiRsrcOffset; STATIC UINT32 mRelocOffset; +STATIC UINT32 mDebugOffset; // // Initialization Function @@ -165,6 +166,10 @@ InitializeElf32 ( // Create COFF Section offset buffer and zero. // mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32)); + if (mCoffSectionsOffset == NULL) { + Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!"); + return FALSE; + } memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32)); // @@ -190,8 +195,11 @@ GetShdrByIndex ( UINT32 Num ) { - if (Num >= mEhdr->e_shnum) - return NULL; + if (Num >= mEhdr->e_shnum) { + Error (NULL, 0, 3000, "Invalid", "GetShdrByIndex: Index %u is too high.", Num); + exit(EXIT_FAILURE); + } + return (Elf_Shdr*)((UINT8*)mShdrBase + Num * mEhdr->e_shentsize); } @@ -202,7 +210,8 @@ GetPhdrByIndex ( ) { if (num >= mEhdr->e_phnum) { - return NULL; + Error (NULL, 0, 3000, "Invalid", "GetPhdrByIndex: Index %u is too high.", num); + exit(EXIT_FAILURE); } return (Elf_Phdr *)((UINT8*)mPhdrBase + num * mEhdr->e_phentsize); @@ -217,6 +226,15 @@ CoffAlign ( return (Offset + mCoffAlignment - 1) & ~(mCoffAlignment - 1); } +STATIC +UINT32 +DebugRvaAlign ( + UINT32 Offset + ) +{ + return (Offset + 3) & ~3; +} + // // filter functions // @@ -252,6 +270,66 @@ IsDataShdr ( return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE); } +STATIC +BOOLEAN +IsStrtabShdr ( + Elf_Shdr *Shdr + ) +{ + Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx); + + return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_STRTAB_SECTION_NAME) == 0); +} + +STATIC +Elf_Shdr * +FindStrtabShdr ( + VOID + ) +{ + UINT32 i; + for (i = 0; i < mEhdr->e_shnum; i++) { + Elf_Shdr *shdr = GetShdrByIndex(i); + if (IsStrtabShdr(shdr)) { + return shdr; + } + } + return NULL; +} + +STATIC +const UINT8 * +GetSymName ( + Elf_Sym *Sym + ) +{ + Elf_Shdr *StrtabShdr; + UINT8 *StrtabContents; + BOOLEAN foundEnd; + UINT32 i; + + if (Sym->st_name == 0) { + return NULL; + } + + StrtabShdr = FindStrtabShdr(); + if (StrtabShdr == NULL) { + return NULL; + } + + assert(Sym->st_name < StrtabShdr->sh_size); + + StrtabContents = (UINT8*)mEhdr + StrtabShdr->sh_offset; + + foundEnd = FALSE; + for (i = Sym->st_name; (i < StrtabShdr->sh_size) && !foundEnd; i++) { + foundEnd = (BOOLEAN)(StrtabContents[i] == 0); + } + assert(foundEnd); + + return StrtabContents + Sym->st_name; +} + // // Elf functions interface implementation // @@ -267,12 +345,10 @@ ScanSections32 ( EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr; UINT32 CoffEntry; UINT32 SectionCount; - BOOLEAN FoundText; + BOOLEAN FoundSection; CoffEntry = 0; mCoffOffset = 0; - mTextOffset = 0; - FoundText = FALSE; // // Coff file start with a DOS header. @@ -293,10 +369,36 @@ 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; + } + } + + // + // Move the PE/COFF header right before the first section. This will help us + // save space when converting to TE. + // + if (mCoffAlignment > mCoffOffset) { + mNtHdrOffset += mCoffAlignment - mCoffOffset; + mTableOffset += mCoffAlignment - mCoffOffset; + mCoffOffset = mCoffAlignment; + } + // // First text sections. // mCoffOffset = CoffAlign(mCoffOffset); + mTextOffset = mCoffOffset; + FoundSection = FALSE; SectionCount = 0; for (i = 0; i < mEhdr->e_shnum; i++) { Elf_Shdr *shdr = GetShdrByIndex(i); @@ -306,12 +408,8 @@ ScanSections32 ( if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) { // if the section address is aligned we must align PE/COFF mCoffOffset = (mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1); - } else if ((shdr->sh_addr % shdr->sh_addralign) != (mCoffOffset % shdr->sh_addralign)) { - // ARM RVCT tools have behavior outside of the ELF specification to try - // and make images smaller. If sh_addr is not aligned to sh_addralign - // then the section needs to preserve sh_addr MOD sh_addralign. - // Normally doing nothing here works great. - Error (NULL, 0, 3000, "Invalid", "Unsupported section alignment."); + } else { + Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment."); } } @@ -324,9 +422,9 @@ ScanSections32 ( // // Set mTextOffset with the offset of the first '.text' section // - if (!FoundText) { + if (!FoundSection) { mTextOffset = mCoffOffset; - FoundText = TRUE; + FoundSection = TRUE; } mCoffSectionsOffset[i] = mCoffOffset; @@ -335,14 +433,13 @@ ScanSections32 ( } } - if (!FoundText) { + if (!FoundSection) { Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section."); assert (FALSE); } - if (mEhdr->e_machine != EM_ARM) { - mCoffOffset = CoffAlign(mCoffOffset); - } + mDebugOffset = DebugRvaAlign(mCoffOffset); + mCoffOffset = CoffAlign(mCoffOffset); if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) { Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName); @@ -352,6 +449,7 @@ ScanSections32 ( // Then data sections. // mDataOffset = mCoffOffset; + FoundSection = FALSE; SectionCount = 0; for (i = 0; i < mEhdr->e_shnum; i++) { Elf_Shdr *shdr = GetShdrByIndex(i); @@ -361,25 +459,47 @@ ScanSections32 ( if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) { // if the section address is aligned we must align PE/COFF mCoffOffset = (mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1); - } else if ((shdr->sh_addr % shdr->sh_addralign) != (mCoffOffset % shdr->sh_addralign)) { - // ARM RVCT tools have behavior outside of the ELF specification to try - // and make images smaller. If sh_addr is not aligned to sh_addralign - // then the section needs to preserve sh_addr MOD sh_addralign. - // Normally doing nothing here works great. - Error (NULL, 0, 3000, "Invalid", "Unsupported section alignment."); + } else { + Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment."); } } + + // + // Set mDataOffset with the offset of the first '.data' section + // + if (!FoundSection) { + mDataOffset = mCoffOffset; + FoundSection = TRUE; + } + mCoffSectionsOffset[i] = mCoffOffset; mCoffOffset += shdr->sh_size; SectionCount ++; } } - mCoffOffset = CoffAlign(mCoffOffset); if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) { Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName); } + // + // Make room for .debug data in .data (or .text if .data is empty) instead of + // putting it in a section of its own. This is explicitly allowed by the + // PE/COFF spec, and prevents bloat in the binary when using large values for + // section alignment. + // + if (SectionCount > 0) { + mDebugOffset = DebugRvaAlign(mCoffOffset); + } + mCoffOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) + + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + + strlen(mInImageName) + 1; + + mCoffOffset = CoffAlign(mCoffOffset); + if (SectionCount == 0) { + mDataOffset = mCoffOffset; + } + // // The HII resource sections. // @@ -392,15 +512,12 @@ ScanSections32 ( if ((shdr->sh_addr & (shdr->sh_addralign - 1)) == 0) { // if the section address is aligned we must align PE/COFF mCoffOffset = (mCoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1); - } else if ((shdr->sh_addr % shdr->sh_addralign) != (mCoffOffset % shdr->sh_addralign)) { - // ARM RVCT tools have behavior outside of the ELF specification to try - // and make images smaller. If sh_addr is not aligned to sh_addralign - // then the section needs to preserve sh_addr MOD sh_addralign. - // Normally doing nothing here works great. - Error (NULL, 0, 3000, "Invalid", "Unsupported section alignment."); + } else { + Error (NULL, 0, 3000, "Invalid", "Section address not aligned to its own alignment."); } } if (shdr->sh_size != 0) { + mHiiRsrcOffset = mCoffOffset; mCoffSectionsOffset[i] = mCoffOffset; mCoffOffset += shdr->sh_size; mCoffOffset = CoffAlign(mCoffOffset); @@ -416,6 +533,10 @@ ScanSections32 ( // Allocate base Coff file. Will be expanded later for relocations. // mCoffFile = (UINT8 *)malloc(mCoffOffset); + if (mCoffFile == NULL) { + Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!"); + } + assert (mCoffFile != NULL); memset(mCoffFile, 0, mCoffOffset); // @@ -439,7 +560,7 @@ ScanSections32 ( NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC; break; default: - VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN)mEhdr->e_machine); + VerboseMsg ("%s unknown e_machine type %hu. Assume IA-32", mInImageName, mEhdr->e_machine); NtHdr->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_IA32; NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC; } @@ -618,9 +739,18 @@ WriteSections32 ( // header location. // if (Sym->st_shndx == SHN_UNDEF - || Sym->st_shndx == SHN_ABS - || Sym->st_shndx > mEhdr->e_shnum) { - Error (NULL, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName); + || Sym->st_shndx >= mEhdr->e_shnum) { + const UINT8 *SymName = GetSymName(Sym); + if (SymName == NULL) { + SymName = (const UINT8 *)""; + } + + Error (NULL, 0, 3000, "Invalid", + "%s: Bad definition for symbol '%s'@%#x or unsupported symbol type. " + "For example, absolute and undefined symbols are not supported.", + mInImageName, SymName, Sym->st_value); + + exit(EXIT_FAILURE); } SymShdr = GetShdrByIndex(Sym->st_shndx); @@ -667,6 +797,7 @@ WriteSections32 ( // break skipped case R_ARM_PC24: + case R_ARM_REL32: case R_ARM_XPC25: case R_ARM_THM_PC22: case R_ARM_THM_JUMP19: @@ -754,9 +885,7 @@ WriteRelocations32 ( UINTN RelSize; UINTN RelOffset; UINTN K; - UINT8 *Targ; Elf32_Phdr *DynamicSegment; - Elf32_Phdr *TargetSegment; for (Index = 0, FoundRelocations = FALSE; Index < mEhdr->e_shnum; Index++) { Elf_Shdr *RelShdr = GetShdrByIndex(Index); @@ -767,7 +896,7 @@ WriteRelocations32 ( FoundRelocations = TRUE; for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) { - Elf_Rel *Rel = (Elf_Rel *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx); + Rel = (Elf_Rel *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx); if (mEhdr->e_machine == EM_386) { switch (ELF_R_TYPE(Rel->r_info)) { @@ -795,6 +924,7 @@ WriteRelocations32 ( // break skipped case R_ARM_PC24: + case R_ARM_REL32: case R_ARM_XPC25: case R_ARM_THM_PC22: case R_ARM_THM_JUMP19: @@ -906,6 +1036,31 @@ WriteRelocations32 ( Error (NULL, 0, 3000, "Invalid", "%s bad ARM dynamic relocations.", mInImageName); } + for (Index = 0; Index < mEhdr->e_shnum; Index++) { + Elf_Shdr *shdr = GetShdrByIndex(Index); + + // + // The PT_DYNAMIC section contains DT_REL relocations whose r_offset + // field is relative to the base of a segment (or the entire image), + // and not to the base of an ELF input section as is the case for + // SHT_REL sections. This means that we cannot fix up such relocations + // unless we cross-reference ELF sections and segments, considering + // that the output placement recorded in mCoffSectionsOffset[] is + // section based, not segment based. + // + // Fortunately, there is a simple way around this: we require that the + // in-memory layout of the ELF and PE/COFF versions of the binary is + // identical. That way, r_offset will retain its validity as a PE/COFF + // image offset, and we can record it in the COFF fixup table + // unmodified. + // + if (shdr->sh_addr != mCoffSectionsOffset[Index]) { + Error (NULL, 0, 3000, + "Invalid", "%s: PT_DYNAMIC relocations require identical ELF and PE/COFF section offsets.", + mInImageName); + } + } + for (K = 0; K < RelSize; K += RelElementSize) { if (DynamicSegment->p_paddr == 0) { @@ -922,14 +1077,7 @@ WriteRelocations32 ( break; case R_ARM_RABS32: - TargetSegment = GetPhdrByIndex (ELF32_R_SYM (Rel->r_info) - 1); - - // Note: r_offset in a memory address. Convert it to a pointer in the coff file. - Targ = mCoffFile + mCoffSectionsOffset[ ELF32_R_SYM( Rel->r_info ) ] + Rel->r_offset - TargetSegment->p_vaddr; - - *(UINT32 *)Targ = *(UINT32 *)Targ + mCoffSectionsOffset [ELF32_R_SYM( Rel->r_info )]; - - CoffAddFixup (mCoffSectionsOffset[ELF32_R_SYM (Rel->r_info)] + (Rel->r_offset - TargetSegment->p_vaddr), EFI_IMAGE_REL_BASED_HIGHLOW); + CoffAddFixup (Rel->r_offset, EFI_IMAGE_REL_BASED_HIGHLOW); break; default: @@ -973,28 +1121,18 @@ WriteDebug32 ( ) { UINT32 Len; - UINT32 DebugOffset; EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr; EFI_IMAGE_DATA_DIRECTORY *DataDir; EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *Dir; EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY *Nb10; Len = strlen(mInImageName) + 1; - DebugOffset = mCoffOffset; - - mCoffOffset += sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY) - + sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) - + Len; - mCoffOffset = CoffAlign(mCoffOffset); - - mCoffFile = realloc(mCoffFile, mCoffOffset); - memset(mCoffFile + DebugOffset, 0, mCoffOffset - DebugOffset); - Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + DebugOffset); + Dir = (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY*)(mCoffFile + mDebugOffset); Dir->Type = EFI_IMAGE_DEBUG_TYPE_CODEVIEW; Dir->SizeOfData = sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY) + Len; - Dir->RVA = DebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); - Dir->FileOffset = DebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); + Dir->RVA = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); + Dir->FileOffset = mDebugOffset + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); Nb10 = (EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY*)(Dir + 1); Nb10->Signature = CODEVIEW_SIGNATURE_NB10; @@ -1003,20 +1141,8 @@ WriteDebug32 ( NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset); DataDir = &NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]; - DataDir->VirtualAddress = DebugOffset; - DataDir->Size = mCoffOffset - DebugOffset; - if (DataDir->Size == 0) { - // If no debug, null out the directory entry and don't add the .debug section - DataDir->VirtualAddress = 0; - NtHdr->Pe32.FileHeader.NumberOfSections--; - } else { - DataDir->VirtualAddress = DebugOffset; - CreateSectionHeader (".debug", DebugOffset, mCoffOffset - DebugOffset, - EFI_IMAGE_SCN_CNT_INITIALIZED_DATA - | EFI_IMAGE_SCN_MEM_DISCARDABLE - | EFI_IMAGE_SCN_MEM_READ); - - } + DataDir->VirtualAddress = mDebugOffset; + DataDir->Size = sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); } STATIC