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]

PATCH: PR 12467: Warn about empty non-zero header offsets


Hi Guys,

  I am applying the patch below so that readelf will issue a
  warning message if it encounters a file with an ELF header containing
  a non-zero section headers offset, but no section headers.

  Similarly the patch also has readelf issue a warning if there is a
  non-zero program headers offset but no program headers.  Interestingly
  this test was actually triggered by a few tests in the linker
  testsuite because of code in elf.c, so the patch also fixes that.

  Tested with lots and lots of toolchains. :-)
  
Cheers
  Nick

binutils/ChangeLog
2011-02-08  Nick Clifton  <nickc@redhat.com>

	PR binutils/12467
	* readelf.c (process_program_headers): Issue a warning if there
	are no program headers but the file header has a non-zero program
	header offset.
	(process_section_headers): Issue a warning if there are no section
	headers but the file header has a non-zero section header offset.
	(process_section_groups): Reword the no section message so that it
	can be distinguished from the one issued by process_section_headers.

bfd/ChangeLog
2011-02-08  Nick Clifton  <nickc@redhat.com>

	PR binutils/12467
	* elf.c (assign_file_positions_for_load_sections): Set the program
	header offset and entry size to zero if there are no program
	headers.

Index: binutils/readelf.c
===================================================================
RCS file: /cvs/src/src/binutils/readelf.c,v
retrieving revision 1.531
diff -u -3 -p -r1.531 readelf.c
--- binutils/readelf.c	19 Jan 2011 18:19:54 -0000	1.531
+++ binutils/readelf.c	8 Feb 2011 09:26:55 -0000
@@ -3638,7 +3638,11 @@ process_program_headers (FILE * file)
 
   if (elf_header.e_phnum == 0)
     {
-      if (do_segments)
+      /* PR binutils/12467.  */
+      if (elf_header.e_phoff != 0)
+	warn (_("possibly corrupt ELF header - it has a non-zero program"
+		" header offset, but no program headers"));
+      else if (do_segments)
 	printf (_("\nThere are no program headers in this file.\n"));
       return 0;
     }
@@ -4377,7 +4381,11 @@ process_section_headers (FILE * file)
 
   if (elf_header.e_shnum == 0)
     {
-      if (do_sections)
+      /* PR binutils/12467.  */
+      if (elf_header.e_shoff != 0)
+	warn (_("possibly corrupt ELF file header - it has a non-zero"
+		" section header offset, but no section headers\n"));
+      else if (do_sections)
 	printf (_("\nThere are no sections in this file.\n"));
 
       return 1;
@@ -4860,7 +4868,7 @@ process_section_groups (FILE * file)
   if (elf_header.e_shnum == 0)
     {
       if (do_section_groups)
-	printf (_("\nThere are no sections in this file.\n"));
+	printf (_("\nThere are no sections to group in this file.\n"));
 
       return 1;
     }
Index: bfd/elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.528
diff -u -3 -p -r1.528 elf.c
--- bfd/elf.c	14 Jan 2011 12:35:55 -0000	1.528
+++ bfd/elf.c	8 Feb 2011 09:26:56 -0000
@@ -4334,8 +4334,18 @@ assign_file_positions_for_load_sections 
 	header_pad = m->header_size;
     }
 
-  elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
-  elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
+  if (alloc)
+    {
+      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
+      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
+    }
+  else
+    {
+      /* PR binutils/12467.  */
+      elf_elfheader (abfd)->e_phoff = 0;
+      elf_elfheader (abfd)->e_phentsize = 0;
+    }
+  
   elf_elfheader (abfd)->e_phnum = alloc;
 
   if (elf_tdata (abfd)->program_header_size == (bfd_size_type) -1)


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