Archive | October, 2008

Digging Deeper into Weak Symbols

Following on from my last post, I thought I’d dig a bit deeper into GCC’s weak symbols and use the NM utility from GNU’s Binutils package to help understand a bit better how it works.

I probably don’t use the Binutils package as often as I should when debugging and thought this post would be a perfect opportunity to do so – for those that are unfamiliar with it, it’s a package that usually comes with your compiler and provides the ability to explore and manipulate the object code of various binaries. It consists of things like readelf, objdump, nm, etc – sound familiar?

Continue Reading →

GCC Weak Symbols

GNU’s GCC has a useful (and perhaps not very well known) feature known as ‘weak symbols’. I first discovered this a while back when building a Linux kernel – however unbeknown to me the Linux kernel makes great use of weak symbols yet the compiler I used did not correctly support them. Rather than a failed build the kernel built fine and even run – I was instead presented with a number of interesting bugs, but more on this later.

In a nutshell weak symbols permit you to define a symbol that doesn’t need to be resolved at link time, i.e. it allows you to tell the compiler that this function may not have a body and that is OK. Furthermore, if later the compiler comes across another symbol with the same name that doesn’t have the weak attribute the original symbol will be overwritten with the stronger symbol (Without getting a multiple defination linker error). And finally you can also use the symbol to determine, at run-time, if such a body exists.

Continue Reading →