« Return to Thread: PATCH: PR binutils/12639: nm/readelf failed to detect corrupted symtab

PATCH: PR binutils/12639: nm/readelf failed to detect corrupted symtab

by H.J. Lu :: Rate this Message:

| View in Thread

Here is the re-submission of

http://sourceware.org/ml/binutils/2011-04/msg00063.html

OK to install?

Thanks.

H.J.
---
bfd/

2011-04-06  H.J. Lu  <hongjiu.lu@...>

        PR binutils/12639
        * elfcode.h (elf_slurp_symbol_table): Check valid local symbols.

binutils/

2011-04-06  H.J. Lu  <hongjiu.lu@...>

        PR binutils/12639
        * readelf.c (process_symbol_table): Detect corrupted symtab.

diff --git a/bfd/elfcode.h b/bfd/elfcode.h
index 28b6b90..c56b4f1 100644
--- a/bfd/elfcode.h
+++ b/bfd/elfcode.h
@@ -1218,6 +1218,9 @@ elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bfd_boolean dynamic)
     sym = symbase = NULL;
   else
     {
+      /* Start of global symbols */
+      Elf_Internal_Sym *start_global;
+
       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
       NULL, NULL, NULL);
       if (isymbuf == NULL)
@@ -1262,6 +1265,9 @@ elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bfd_boolean dynamic)
       if (xver != NULL)
  ++xver;
       isymend = isymbuf + symcount;
+      start_global = isymbuf;
+      if (!elf_bad_symtab (abfd))
+ start_global += hdr->sh_info;
       for (isym = isymbuf + 1, sym = symbase; isym < isymend; isym++, sym++)
  {
   memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
@@ -1306,6 +1312,18 @@ elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bfd_boolean dynamic)
   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
     sym->symbol.value -= sym->symbol.section->vma;
 
+  if (isym < start_global
+      && ELF_ST_BIND (isym->st_info) != STB_LOCAL)
+    {
+      (*_bfd_error_handler)
+ (_("%s: corrupted local symbol `%s'"),
+ abfd->filename, sym->symbol.name);
+
+      /* Force it to local symbol.  */
+      isym->st_info = ELF_ST_INFO (STB_LOCAL,
+   ELF_ST_TYPE (isym->st_info));
+    }
+
   switch (ELF_ST_BIND (isym->st_info))
     {
     case STB_LOCAL:
diff --git a/binutils/readelf.c b/binutils/readelf.c
index f8c6bb3..ebeddb7 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -8825,7 +8825,7 @@ process_symbol_table (FILE * file)
    i < elf_header.e_shnum;
    i++, section++)
  {
-  unsigned int si;
+  unsigned int si, start_global;
   char * strtab = NULL;
   unsigned long int strtab_size = 0;
   Elf_Internal_Sym * symtab;
@@ -8857,6 +8857,7 @@ process_symbol_table (FILE * file)
   if (symtab == NULL)
     continue;
 
+  start_global = section->sh_info;
   if (section->sh_link == elf_header.e_shstrndx)
     {
       strtab = string_table;
@@ -8883,7 +8884,11 @@ process_symbol_table (FILE * file)
       putchar (' ');
       print_vma (psym->st_size, DEC_5);
       printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
-      printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
+      if (si < start_global
+  && ELF_ST_BIND (psym->st_info) != STB_LOCAL)
+ printf (" %-6s", "<corrupt>");
+      else
+ printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
       printf (" %-7s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
       /* Check to see if any other bits in the st_other field are set.
          Note - displaying this information disrupts the layout of the

 « Return to Thread: PATCH: PR binutils/12639: nm/readelf failed to detect corrupted symtab