/** @file\r
Elf32 Convert solution\r
\r
-Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2021, Intel Corporation. All rights reserved.<BR>\r
Portions copyright (c) 2013, ARM Ltd. All rights reserved.<BR>\r
+Portions Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>\r
\r
-This program and the accompanying materials are licensed and made available\r
-under the terms and conditions of the BSD License which accompanies this\r
-distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
\r
**/\r
\r
);\r
\r
//\r
-// Rename ELF32 strucutres to common names to help when porting to ELF64.\r
+// Rename ELF32 structures to common names to help when porting to ELF64.\r
//\r
typedef Elf32_Shdr Elf_Shdr;\r
typedef Elf32_Ehdr Elf_Ehdr;\r
//\r
// Initialize data pointer and structures.\r
//\r
- mEhdr = (Elf_Ehdr*) FileBuffer; \r
+ mEhdr = (Elf_Ehdr*) FileBuffer;\r
\r
//\r
// Check the ELF32 specific header information.\r
if (mEhdr->e_ident[EI_DATA] != ELFDATA2LSB) {\r
Error (NULL, 0, 3000, "Unsupported", "ELF EI_DATA not ELFDATA2LSB");\r
return FALSE;\r
- } \r
+ }\r
if ((mEhdr->e_type != ET_EXEC) && (mEhdr->e_type != ET_DYN)) {\r
Error (NULL, 0, 3000, "Unsupported", "ELF e_type not ET_EXEC or ET_DYN");\r
return FALSE;\r
}\r
- if (!((mEhdr->e_machine == EM_386) || (mEhdr->e_machine == EM_ARM))) { \r
- Error (NULL, 0, 3000, "Unsupported", "ELF e_machine not EM_386 or EM_ARM");\r
- return FALSE;\r
+ if (!((mEhdr->e_machine == EM_386) || (mEhdr->e_machine == EM_ARM) || (mEhdr->e_machine == EM_RISCV))) {\r
+ Warning (NULL, 0, 3000, "Unsupported", "ELF e_machine is not Elf32 machine.");\r
}\r
if (mEhdr->e_version != EV_CURRENT) {\r
Error (NULL, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr->e_version, EV_CURRENT);\r
return FALSE;\r
}\r
- \r
+\r
//\r
// Update section header pointers\r
//\r
mShdrBase = (Elf_Shdr *)((UINT8 *)mEhdr + mEhdr->e_shoff);\r
mPhdrBase = (Elf_Phdr *)((UINT8 *)mEhdr + mEhdr->e_phoff);\r
- \r
+\r
//\r
// Create COFF Section offset buffer and zero.\r
//\r
Elf_Shdr *Shdr\r
)\r
{\r
- return (BOOLEAN) ((Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC);\r
+ return (BOOLEAN) (((Shdr->sh_flags & (SHF_EXECINSTR | SHF_ALLOC)) == (SHF_EXECINSTR | SHF_ALLOC)) ||\r
+ ((Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC));\r
}\r
\r
STATIC\r
if (IsHiiRsrcShdr(Shdr)) {\r
return FALSE;\r
}\r
- return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);\r
+ return (BOOLEAN) (Shdr->sh_flags & (SHF_EXECINSTR | SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);\r
}\r
\r
STATIC\r
mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS32);\r
break;\r
default:\r
- VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN)mEhdr->e_machine);\r
+ VerboseMsg ("%u unknown e_machine type. Assume IA-32", (UINTN)mEhdr->e_machine);\r
mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS32);\r
break;\r
}\r
}\r
}\r
\r
- if (!FoundSection) {\r
+ if (!FoundSection && mOutImageType != FW_ACPI_IMAGE) {\r
Error (NULL, 0, 3000, "Invalid", "Did not find any '.text' section.");\r
assert (FALSE);\r
}\r
mCoffOffset = CoffAlign(mCoffOffset);\r
\r
if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {\r
- Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);\r
+ Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);\r
}\r
\r
//\r
}\r
\r
if (SectionCount > 1 && mOutImageType == FW_EFI_IMAGE) {\r
- Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);\r
+ Warning (NULL, 0, 0, NULL, "Multiple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);\r
}\r
\r
//\r
\r
default:\r
//\r
- // Ignore for unkown section type.\r
+ // Ignore for unknown section type.\r
//\r
- VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName, (unsigned)Shdr->sh_type);\r
+ VerboseMsg ("%s unknown section type %x. We ignore this unknown section type.", mInImageName, (unsigned)Shdr->sh_type);\r
break;\r
}\r
}\r
if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {\r
continue;\r
}\r
- \r
+\r
//\r
// Relocation section found. Now extract section information that the relocations\r
// apply to in the ELF data and the new COFF data.\r
//\r
SecShdr = GetShdrByIndex(RelShdr->sh_info);\r
SecOffset = mCoffSectionsOffset[RelShdr->sh_info];\r
- \r
+\r
//\r
// Only process relocations for the current filter type.\r
//\r
if (RelShdr->sh_type == SHT_REL && (*Filter)(SecShdr)) {\r
UINT32 RelOffset;\r
- \r
+\r
//\r
// Determine the symbol table referenced by the relocation data.\r
//\r
// Set pointer to relocation entry\r
//\r
Elf_Rel *Rel = (Elf_Rel *)((UINT8*)mEhdr + RelShdr->sh_offset + RelOffset);\r
- \r
+\r
//\r
// Set pointer to symbol table entry associated with the relocation entry.\r
//\r
Elf_Sym *Sym = (Elf_Sym *)(Symtab + ELF_R_SYM(Rel->r_info) * SymtabShdr->sh_entsize);\r
- \r
+\r
Elf_Shdr *SymShdr;\r
UINT8 *Targ;\r
UINT16 Address;\r
\r
//\r
- // Check section header index found in symbol table and get the section \r
+ // Check section header index found in symbol table and get the section\r
// header location.\r
//\r
if (Sym->st_shndx == SHN_UNDEF\r
if (SymName == NULL) {\r
SymName = (const UINT8 *)"<unknown>";\r
}\r
-\r
- Error (NULL, 0, 3000, "Invalid",\r
- "%s: Bad definition for symbol '%s'@%#x or unsupported symbol type. "\r
- "For example, absolute and undefined symbols are not supported.",\r
- mInImageName, SymName, Sym->st_value);\r
-\r
- exit(EXIT_FAILURE);\r
+ continue;\r
}\r
SymShdr = GetShdrByIndex(Sym->st_shndx);\r
\r
//\r
// Convert the relocation data to a pointer into the coff file.\r
//\r
- // Note: \r
+ // Note:\r
// r_offset is the virtual address of the storage unit to be relocated.\r
// sh_addr is the virtual address for the base of the section.\r
//\r
case R_ARM_THM_JUMP19:\r
case R_ARM_CALL:\r
case R_ARM_JMP24:\r
- case R_ARM_THM_JUMP24: \r
- case R_ARM_PREL31: \r
- case R_ARM_MOVW_PREL_NC: \r
+ case R_ARM_THM_JUMP24:\r
+ case R_ARM_PREL31:\r
+ case R_ARM_MOVW_PREL_NC:\r
case R_ARM_MOVT_PREL:\r
case R_ARM_THM_MOVW_PREL_NC:\r
case R_ARM_THM_MOVT_PREL:\r
case R_ARM_LDC_PC_G0:\r
case R_ARM_LDC_PC_G1:\r
case R_ARM_LDC_PC_G2:\r
- case R_ARM_GOT_PREL:\r
case R_ARM_THM_JUMP11:\r
case R_ARM_THM_JUMP8:\r
case R_ARM_TLS_GD32:\r
for (RelIdx = 0; RelIdx < RelShdr->sh_size; RelIdx += RelShdr->sh_entsize) {\r
Rel = (Elf_Rel *)((UINT8*)mEhdr + RelShdr->sh_offset + RelIdx);\r
\r
- if (mEhdr->e_machine == EM_386) { \r
+ if (mEhdr->e_machine == EM_386) {\r
switch (ELF_R_TYPE(Rel->r_info)) {\r
case R_386_NONE:\r
case R_386_PC32:\r
case R_ARM_THM_JUMP19:\r
case R_ARM_CALL:\r
case R_ARM_JMP24:\r
- case R_ARM_THM_JUMP24: \r
- case R_ARM_PREL31: \r
- case R_ARM_MOVW_PREL_NC: \r
+ case R_ARM_THM_JUMP24:\r
+ case R_ARM_PREL31:\r
+ case R_ARM_MOVW_PREL_NC:\r
case R_ARM_MOVT_PREL:\r
case R_ARM_THM_MOVW_PREL_NC:\r
case R_ARM_THM_MOVT_PREL:\r
case R_ARM_LDC_PC_G0:\r
case R_ARM_LDC_PC_G1:\r
case R_ARM_LDC_PC_G2:\r
- case R_ARM_GOT_PREL:\r
case R_ARM_THM_JUMP11:\r
case R_ARM_THM_JUMP8:\r
case R_ARM_TLS_GD32:\r
case R_ARM_RABS32:\r
CoffAddFixup (Rel->r_offset, EFI_IMAGE_REL_BASED_HIGHLOW);\r
break;\r
- \r
+\r
default:\r
- Error (NULL, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unkown type %d.", mInImageName, ELF32_R_TYPE (Rel->r_info));\r
+ Error (NULL, 0, 3000, "Invalid", "%s bad ARM dynamic relocations, unknown type %d.", mInImageName, ELF32_R_TYPE (Rel->r_info));\r
break;\r
}\r
}\r
)\r
{\r
EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;\r
- \r
+\r
//\r
// Set image size\r
//\r