This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


On Wed, Apr 6, 2011 at 7:43 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
> On Tue, Apr 5, 2011 at 11:58 PM, Alan Modra <amodra@gmail.com> wrote:
>> On Tue, Apr 05, 2011 at 11:02:06PM -0700, H.J. Lu wrote:
>>> d) nm/readelf reports error on those bad input files.
>>>
>>> That is what my patch implements.
>>
>> Not exactly. ?Your elfcode.h:elf_slurp_symbol_table patch issues a
>> message but doesn't return an error. ?I think you ought to call
>> bfd_set_error and exit the function via error_return.
>
> Then we got
>
> [hjl@gnu-6 cq167859]$ make
> ./ld -o bad tr70098.bad.o start.o
> start.o: In function `_start':
> (.text+0x1): undefined reference to `main'
> make: *** [bad] Error 1
> [hjl@gnu-6 cq167859]$ ./nm tr70098.bad.o
> BFD: tr70098.bad.o: corrupted local symbol `main'
> ./nm: tr70098.bad.o: No symbols
> [hjl@gnu-6 cq167859]$
>
> "nm" won't display any symbols. I don't think it is very helpful.
>
>> On the readelf patch, I'd be inclined to report "Bad symtab" at the
>> end of the symbol table and not special case mips.
>>
>
> readelf displays many "<corrupt>" without something like
> "Bad XXXX" at the end. ?I don't think symtab is that special.
>

Here is the updated patch without special case for MIPS.
OK to install?

Thanks.

-- 
H.J.
---
bfd/

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

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

binutils/

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

	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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]