]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenFw/Elf32Convert.c
BaseTools: Various typo
[mirror_edk2.git] / BaseTools / Source / C / GenFw / Elf32Convert.c
index d115291b062c9f4c3ae5f1876e467aeaa0ebe705..62cdb30cf541938bc1fbe54b3e9a9cf1722aa815 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
 Elf32 Convert solution\r
 \r
-Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
 Portions copyright (c) 2013, ARM Ltd. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials are licensed and made available\r
@@ -21,7 +21,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <io.h>\r
 #endif\r
 #include <assert.h>\r
-#include <stdbool.h>\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <string.h>\r
@@ -75,7 +74,7 @@ CleanUp32 (
   );\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
@@ -131,7 +130,7 @@ InitializeElf32 (
   //\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
@@ -143,12 +142,12 @@ InitializeElf32 (
   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
+  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
   }\r
@@ -156,17 +155,21 @@ InitializeElf32 (
     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
   mCoffSectionsOffset = (UINT32 *)malloc(mEhdr->e_shnum * sizeof (UINT32));\r
+  if (mCoffSectionsOffset == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return FALSE;\r
+  }\r
   memset(mCoffSectionsOffset, 0, mEhdr->e_shnum * sizeof(UINT32));\r
 \r
   //\r
@@ -300,22 +303,27 @@ GetSymName (
   Elf_Sym *Sym\r
   )\r
 {\r
+  Elf_Shdr *StrtabShdr;\r
+  UINT8    *StrtabContents;\r
+  BOOLEAN  foundEnd;\r
+  UINT32   i;\r
+\r
   if (Sym->st_name == 0) {\r
     return NULL;\r
   }\r
 \r
-  Elf_Shdr *StrtabShdr = FindStrtabShdr();\r
+  StrtabShdr = FindStrtabShdr();\r
   if (StrtabShdr == NULL) {\r
     return NULL;\r
   }\r
 \r
   assert(Sym->st_name < StrtabShdr->sh_size);\r
 \r
-  UINT8* StrtabContents = (UINT8*)mEhdr + StrtabShdr->sh_offset;\r
+  StrtabContents = (UINT8*)mEhdr + StrtabShdr->sh_offset;\r
 \r
-  bool foundEnd = false;\r
-  for (UINT32 i = Sym->st_name; (i < StrtabShdr->sh_size) && !foundEnd; i++) {\r
-    foundEnd = StrtabContents[i] == 0;\r
+  foundEnd = FALSE;\r
+  for (i = Sym->st_name; (i < StrtabShdr->sh_size) && !foundEnd; i++) {\r
+    foundEnd = (BOOLEAN)(StrtabContents[i] == 0);\r
   }\r
   assert(foundEnd);\r
 \r
@@ -375,6 +383,14 @@ ScanSections32 (
     }\r
   }\r
 \r
+  //\r
+  // Check if mCoffAlignment is larger than MAX_COFF_ALIGNMENT\r
+  //\r
+  if (mCoffAlignment > MAX_COFF_ALIGNMENT) {\r
+    Error (NULL, 0, 3000, "Invalid", "Section alignment is larger than MAX_COFF_ALIGNMENT.");\r
+    assert (FALSE);\r
+  }\r
+\r
   //\r
   // Move the PE/COFF header right before the first section. This will help us\r
   // save space when converting to TE.\r
@@ -434,7 +450,7 @@ ScanSections32 (
   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
@@ -471,7 +487,7 @@ ScanSections32 (
   }\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
@@ -525,6 +541,10 @@ ScanSections32 (
   // Allocate base Coff file.  Will be expanded later for relocations.\r
   //\r
   mCoffFile = (UINT8 *)malloc(mCoffOffset);\r
+  if (mCoffFile == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+  }\r
+  assert (mCoffFile != NULL);\r
   memset(mCoffFile, 0, mCoffOffset);\r
 \r
   //\r
@@ -654,6 +674,9 @@ WriteSections32 (
       switch (Shdr->sh_type) {\r
       case SHT_PROGBITS:\r
         /* Copy.  */\r
+        if (Shdr->sh_offset + Shdr->sh_size > mFileBufferSize) {\r
+          return FALSE;\r
+        }\r
         memcpy(mCoffFile + mCoffSectionsOffset[Idx],\r
               (UINT8*)mEhdr + Shdr->sh_offset,\r
               Shdr->sh_size);\r
@@ -665,9 +688,9 @@ WriteSections32 (
 \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
@@ -684,20 +707,20 @@ WriteSections32 (
     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
@@ -712,18 +735,18 @@ WriteSections32 (
         // 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
@@ -745,7 +768,7 @@ WriteSections32 (
         //\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
@@ -791,9 +814,9 @@ WriteSections32 (
           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
@@ -814,7 +837,6 @@ WriteSections32 (
           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
@@ -886,7 +908,7 @@ WriteRelocations32 (
         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
@@ -918,9 +940,9 @@ WriteRelocations32 (
             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
@@ -941,7 +963,6 @@ WriteRelocations32 (
             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
@@ -1067,9 +1088,9 @@ WriteRelocations32 (
           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
@@ -1130,7 +1151,7 @@ WriteDebug32 (
   NtHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)(mCoffFile + mNtHdrOffset);\r
   DataDir = &NtHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG];\r
   DataDir->VirtualAddress = mDebugOffset;\r
-  DataDir->Size = Dir->SizeOfData + sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r
+  DataDir->Size = sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);\r
 }\r
 \r
 STATIC\r
@@ -1140,7 +1161,7 @@ SetImageSize32 (
   )\r
 {\r
   EFI_IMAGE_OPTIONAL_HEADER_UNION *NtHdr;\r
-  \r
+\r
   //\r
   // Set image size\r
   //\r