mainThe GNU Binary Utilities - Support: sr #104731, readelf bug printing Section to...


sr #104731: readelf bug printing Section to Segment mapping

Submitter:  None
Submitted:  Wed 12 Oct 2005 07:59:11 PM UTC
Category:  None Priority:  5 - Normal
Severity:  2 - Minor Status:  None
Privacy:  Public Assigned to:  None
Originator Email:  -email is unavailable- Open/Closed:  Open
Operating System:  GNU/Linux
* Mandatory Fields

Add a New Comment Rich Markup

Wed 12 Oct 2005 07:59:11 PM UTC, original submission:  

I believe that there is a bug in the section to segment mapping printed by readelf. It does not consider the last section in the section header table when printing the sections in a segment. The code for binutils-2.15 looks like this:

  if (do_segments && section_headers != NULL)
      printf (_("\n Section to Segment mapping:\n"));
      printf (_("  Segment Sections...\n"));

      assert (string_table != NULL);

      for (i = 0; i < elf_header.e_phnum; i++)
  unsigned int j;
  Elf_Internal_Shdr *section;

  segment = program_headers + i;
  section = section_headers;

  printf ("   %2.2d     ", i);

  for (j = 1; j < elf_header.e_shnum; j++, section++)
      if (section->sh_size > 0
  /* Compare allocated sections by VMA, unallocated
     sections by file offset.  */
  && (section->sh_flags & SHF_ALLOC
      ? (section->sh_addr >= segment->p_vaddr
&& section->sh_addr + section->sh_size
<= segment->p_vaddr + segment->p_memsz)
      : ((bfd_vma) section->sh_offset >= segment->p_offset
&& (section->sh_offset + section->sh_size
     <= segment->p_offset + segment->p_filesz))))
printf ("%s ", SECTION_NAME (section));

  putc ('\n',stdout);

The elf_header.e_shnum seems to be a count of the number of section headers, so if the section headers are numbered in the readelf print as 0 to 12, the elf_header.e_shnum will be 13. The logic checks if it has reached the last section this way:

  for (j = 1; j < elf_header.e_shnum; j++, section++)

but this is wrong. Counted this way, the last section is 13, and elf_header.e_shnum is also 13, so the compare will fail. The comparison should be this:

          for (j = 1; j <= elf_header.e_shnum; j++, section++)

Usually this is not apparent to users of readelf because the interesting sections, i.e. those with SHF_ALLOC, are not the last section. Or at least in the cases I have examined. However, in our case one is the last section.

How do I go about either fixing this or getting it fixed?



(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


CC list is empty


There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

Only logged-in users can vote.


Follows 1 latest change.

Date Changed by Updated Field Previous Value => Replaced by
2005-10-12 None Carbon-Copy- Added ddunkle --AT-- arxan --DOT-- com

Back to the top

Powered by Savane 3.13-8484.
Corresponding source code