Page 1 of 1

STB_WEAK in ELF symbol tables

Posted: Mon Feb 16, 2015 10:11 am
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?

Re: STB_WEAK in ELF symbol tables

Posted: Mon Feb 16, 2015 11:55 am
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.

Re: STB_WEAK in ELF symbol tables

Posted: Tue Feb 24, 2015 3:40 pm
by Candy
It's used a lot - RTTI info, vtable structures, functions that should by default be overridable (operator new for example)...