STB_WEAK in ELF symbol tables

Programming, for all ages and all languages.
Post Reply
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

STB_WEAK in ELF symbol tables

Post by no92 »

Hi,

in the process of writing my assembler I read through the ELF spec to familiarize myself with the structure of such a file and make my assembler output valid ELF binaries. I figured out the File Header, the Program Header and the Section Header Table. Now, I'm working on adding a symbol table. I ran into a question at figure 1-17 (page 24) in the spec at skyfree.org: I understand that STB_LOCAL marks a symbol local, i.e. it's only visible to the object file it's located in. STB_GLOBAL makes a symbol visible to every object file that is linked with the one the symbol is in. STB_WEAK is like STB_GLOBAL, but that it may be overridden by a STB_GLOBAL symbol.

The question here is whether STB_WEAK is actually used or just something the ELF spec proposes but isn't really used. Should I care about it or just neglect it for my assembler?
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: STB_WEAK in ELF symbol tables

Post by jnc100 »

They are commonly used for run-time type information structures which are identified by address. Consider, for example, that you have a C++ header file defining MyTemplateClass<T>, and two source files a.cpp and b.cpp which both instantiate MyTemplateClass<int>. The compiler will produce weak symbols for the RTTI for MyTemplateClass<int> in both a.o and b.o, and then when linked together only one of them is actually used. This is necessary because it is the address of rtti objects which is used to test for equality or not - if each file referenced their own version then this would not hold. There is a bit more to it than this (in ELF typically each template instantiation is put in its own section and only one is included in the final link thus reducing output file size).

I can think of other examples too: e.g. my compilers support library has implementations of memcpy, memset etc defined as weak symbols with the assumption that the user can provide their own if they want to, but if not there is a default version available too (albeit not a very optimized one).

Regards,
John.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: STB_WEAK in ELF symbol tables

Post by Candy »

It's used a lot - RTTI info, vtable structures, functions that should by default be overridable (operator new for example)...
Post Reply