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: Can ELF file contain more than one symbol table?


> I'm writing utility for ELF files processing.
> And if I planing to handle only one SYMTAB, things are simpler.
> But just want to be sure.
> I found here http://www.sco.com/developers/gabi/latest/ch4.sheader.html
> ... that in case of SHT_REL section, sh_link contain "The section header
> index of the associated symbol table."
> That disappointing me. Is it rudiment?

$ cat sections.c
int x, y;

int f() { return x; }

int g() { return y; }

$ gcc -c -ffunction-sections sections.s
$ readelf --all sections.o
Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 4] .text.f           PROGBITS         0000000000000000  00000040
       000000000000000c  0000000000000000  AX       0     0     1
  [ 5] .rela.text.f      RELA             0000000000000000  00000670
       0000000000000018  0000000000000018          13     4     8
  [ 6] .text.g           PROGBITS         0000000000000000  0000004c
       000000000000000c  0000000000000000  AX       0     0     1
  [ 7] .rela.text.g      RELA             0000000000000000  00000688
       0000000000000018  0000000000000018          13     6     8
  [13] .symtab           SYMTAB           0000000000000000  00000508
       0000000000000150  0000000000000018          14    10     8
  [14] .strtab           STRTAB           0000000000000000  00000658
       0000000000000014  0000000000000000           0     0     1
$

This shows that "gcc -ffunction-sections" splits the .text and .rel[a]
for each function into a separate Elf64_Shdr (sections 4, 6; sections 5, 7.)
In this case the .sh_link to the SYMTAB section is the same (section 13),
and all in-the-wild compilers/assemblers use only one SYMTAB section.
However, it would be a legal ELF file if there were multiple SYMTAB
sections, say one for each REL[A] section; it would just be inefficient
(in space for the ElfXX_Shdr, and possibly for duplicate ElfXX_Sym
if more than one function referenced the same symbol) and cumbersome.
Similarly, there is no requirement that the ElfXX_Sym entries in
any symbol table are unique; there could be a different entry for each
reference, ignoring any commonality of name.  (Of course, multiple
entries for the same symbol probably have some consistency in .st_value, etc.)
Also, there is no requirement that the STRTAB have exactly one entry
for any given name.  For instance, in .shstrtab the entries for the
section names ".text.f" and ".rela.text.f" often overlap the common tail.

It is simple to process REL[A] regardless of how many SYMTAB there are:
index an array of ElfXX_Shdr by the .sh_link.  If your algorithm
depends on only one SYMTAB, then start by just detecting and reporting
any violation of that assumption.  It would not be the first case
of a utility that accepted only a subset of the entire specified domain.

-- 


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