OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:14 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: STB_WEAK in ELF symbol tables
PostPosted: Mon Feb 16, 2015 10:11 am 
Offline
Member
Member

Joined: Wed Oct 30, 2013 1:57 pm
Posts: 306
Location: Germany
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?

_________________
managarm


Top
 Profile  
 
 Post subject: Re: STB_WEAK in ELF symbol tables
PostPosted: Mon Feb 16, 2015 11:55 am 
Offline
Member
Member

Joined: Mon Apr 09, 2007 12:10 pm
Posts: 775
Location: London, UK
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.

_________________
Tysos | rpi-boot


Top
 Profile  
 
 Post subject: Re: STB_WEAK in ELF symbol tables
PostPosted: Tue Feb 24, 2015 3:40 pm 
Offline
Member
Member
User avatar

Joined: Tue Oct 17, 2006 11:33 pm
Posts: 3882
Location: Eindhoven
It's used a lot - RTTI info, vtable structures, functions that should by default be overridable (operator new for example)...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 38 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group