]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/GenFw: Enhance error message for bad symbol definitions
authorMichael LeMay <michael.lemay@intel.com>
Fri, 29 Jan 2016 04:17:13 +0000 (12:17 +0800)
committerYonghong Zhu <yonghong.zhu@intel.com>
Wed, 17 Feb 2016 05:21:44 +0000 (13:21 +0800)
This patch expands the error message that is output when GenFw
encounters a bad symbol definition or an unsupported symbol type.  It
displays the symbol name, the symbol address, and a message that
describes both possibilities (bad symbol definition or unsupported
symbol type).  It also provides two examples of unsupported symbol
types.

Furthermore, this patch revises the conditional for detecting bad
symbol definitions to eliminate a redundant test (a Sym->st_shndx
value of SHN_ABS should certainly be greater than mEhdr->e_shnum) and
to change another test from 'Sym->st_shndx > mEhdr->e_shnum' to
'Sym->st_shndx >= mEhdr->e_shnum' for consistency with the test in
GetShdrByIndex.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Michael LeMay <michael.lemay@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
BaseTools/Source/C/GenFw/Elf32Convert.c
BaseTools/Source/C/GenFw/Elf64Convert.c
BaseTools/Source/C/GenFw/ElfConvert.h

index dbdf05671bd19979c32969ba267ce660fcb77576..41091e0888ba1acbe41e7e8c13f77970e3950352 100644 (file)
@@ -266,6 +266,53 @@ IsDataShdr (
   return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);\r
 }\r
 \r
+STATIC\r
+BOOLEAN\r
+IsStrtabShdr (\r
+  Elf_Shdr *Shdr\r
+  )\r
+{\r
+  Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);\r
+\r
+  return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_STRTAB_SECTION_NAME) == 0);\r
+}\r
+\r
+STATIC\r
+Elf_Shdr *\r
+FindStrtabShdr (\r
+  VOID\r
+  )\r
+{\r
+  UINT32 i;\r
+  for (i = 0; i < mEhdr->e_shnum; i++) {\r
+    Elf_Shdr *shdr = GetShdrByIndex(i);\r
+    if (IsStrtabShdr(shdr)) {\r
+      return shdr;\r
+    }\r
+  }\r
+  return NULL;\r
+}\r
+\r
+STATIC\r
+const UINT8 *\r
+GetSymName (\r
+  Elf_Sym *Sym\r
+  )\r
+{\r
+  if (Sym->st_name == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  Elf_Shdr *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
+}\r
+\r
 //\r
 // Elf functions interface implementation\r
 //\r
@@ -671,9 +718,18 @@ WriteSections32 (
         // header location.\r
         //\r
         if (Sym->st_shndx == SHN_UNDEF\r
-            || Sym->st_shndx == SHN_ABS\r
-            || Sym->st_shndx > mEhdr->e_shnum) {\r
-          Error (NULL, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName);\r
+            || Sym->st_shndx >= mEhdr->e_shnum) {\r
+          const UINT8 *SymName = GetSymName(Sym);\r
+          if (SymName == NULL) {\r
+            SymName = (const UINT8 *)"<unknown>";\r
+          }\r
+\r
+          Error (NULL, 0, 3000, "Invalid",\r
+                 "%s: Bad definition for symbol '%s'@%p 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
         }\r
         SymShdr = GetShdrByIndex(Sym->st_shndx);\r
 \r
index 974f3ca53a3ebf1f4d2cec57c5a046550226a295..5afd2ab7cab36bca7e2a95c853f7db3b0040e5f5 100644 (file)
@@ -258,6 +258,53 @@ IsDataShdr (
   return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);\r
 }\r
 \r
+STATIC\r
+BOOLEAN\r
+IsStrtabShdr (\r
+  Elf_Shdr *Shdr\r
+  )\r
+{\r
+  Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);\r
+\r
+  return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_STRTAB_SECTION_NAME) == 0);\r
+}\r
+\r
+STATIC\r
+Elf_Shdr *\r
+FindStrtabShdr (\r
+  VOID\r
+  )\r
+{\r
+  UINT32 i;\r
+  for (i = 0; i < mEhdr->e_shnum; i++) {\r
+    Elf_Shdr *shdr = GetShdrByIndex(i);\r
+    if (IsStrtabShdr(shdr)) {\r
+      return shdr;\r
+    }\r
+  }\r
+  return NULL;\r
+}\r
+\r
+STATIC\r
+const UINT8 *\r
+GetSymName (\r
+  Elf_Sym *Sym\r
+  )\r
+{\r
+  if (Sym->st_name == 0) {\r
+    return NULL;\r
+  }\r
+\r
+  Elf_Shdr *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
+}\r
+\r
 //\r
 // Elf functions interface implementation\r
 //\r
@@ -667,9 +714,18 @@ WriteSections64 (
         // header location.\r
         //\r
         if (Sym->st_shndx == SHN_UNDEF\r
-            || Sym->st_shndx == SHN_ABS\r
-            || Sym->st_shndx > mEhdr->e_shnum) {\r
-          Error (NULL, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName);\r
+            || Sym->st_shndx >= mEhdr->e_shnum) {\r
+          const UINT8 *SymName = GetSymName(Sym);\r
+          if (SymName == NULL) {\r
+            SymName = (const UINT8 *)"<unknown>";\r
+          }\r
+\r
+          Error (NULL, 0, 3000, "Invalid",\r
+                 "%s: Bad definition for symbol '%s'@%p 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
         }\r
         SymShdr = GetShdrByIndex(Sym->st_shndx);\r
 \r
index 56f165eae59b35c8b4e7e5f05520275a0f64ce6d..abf434dd11c8fdd74a545998d14777b225947637 100644 (file)
@@ -34,6 +34,7 @@ extern UINT32 mOutImageType;
 // Common EFI specific data.\r
 //\r
 #define ELF_HII_SECTION_NAME ".hii"\r
+#define ELF_STRTAB_SECTION_NAME ".strtab"\r
 #define MAX_COFF_ALIGNMENT 0x10000\r
 \r
 //\r