]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenFw/Elf32Convert.c
BaseTools/GenFw: disregard payload in PE debug directory entry size
[mirror_edk2.git] / BaseTools / Source / C / GenFw / Elf32Convert.c
index 41091e0888ba1acbe41e7e8c13f77970e3950352..14fe4a285857d694a2e1ab038f05c14a02d73be7 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 - 2017, 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
@@ -166,6 +166,10 @@ InitializeElf32 (
   // 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
@@ -299,18 +303,31 @@ 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
-  return (UINT8*)mEhdr + StrtabShdr->sh_offset + Sym->st_name;\r
+  StrtabContents = (UINT8*)mEhdr + StrtabShdr->sh_offset;\r
+\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
+  return StrtabContents + Sym->st_name;\r
 }\r
 \r
 //\r
@@ -516,6 +533,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
@@ -539,7 +560,7 @@ ScanSections32 (
     NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;\r
     break;\r
   default:\r
-    VerboseMsg ("%s unknown e_machine type. Assume IA-32", (UINTN)mEhdr->e_machine);\r
+    VerboseMsg ("%s unknown e_machine type %hu. Assume IA-32", mInImageName, mEhdr->e_machine);\r
     NtHdr->Pe32.FileHeader.Machine = EFI_IMAGE_MACHINE_IA32;\r
     NtHdr->Pe32.OptionalHeader.Magic = EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC;\r
   }\r
@@ -725,7 +746,7 @@ WriteSections32 (
           }\r
 \r
           Error (NULL, 0, 3000, "Invalid",\r
-                 "%s: Bad definition for symbol '%s'@%p or unsupported symbol type.  "\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
@@ -1121,7 +1142,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