Automatically configuring --hash style

In recent enough binutils, GNU ld supports a new option --hash-style. When set to 'gnu', it produces a new section which has hash for each symbol name and the runtime link phase uses this hash information for comparing symbols.

Obviously for C++ programs/libraries, where symbol names are often very long due to namespace usage, this is a boon as it cuts off the loading time, claimed to be around 50%.

For backwards compatibility, one can always use 'both' as value for the option.

Now here is how to configure use of --hash-style automatically for every C/C++ compile. This is taken on a Slackware current system which is one patch update ahead of Slackware 12.0 RC1.

root@darkstar:/usr/lib/gcc/i486-slackware-linux/4.1.2# diff -bBu specs.orig specs
--- specs.orig 2007-06-24 20:09:33.000000000 +0530
+++ specs 2007-06-24 20:17:14.000000000 +0530
@@ -48,7 +48,7 @@
%{ffast-math|funsafe-math-optimizations:crtfastmath.o%s} %{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s

*link:
-%{!static:--eh-frame-hdr} -m %(link_emulation) %{shared:-shared} %{!shared: %{!ibcs: %{!static: %{rdynamic:-export-dynamic} %{!dynamic-linker:-dynamic-linker %(dynamic_linker)}} %{static:-static}}}
+%{!static:--eh-frame-hdr} -m %(link_emulation) --hash-style=gnu %{shared:-shared} %{!shared: %{!ibcs: %{!static: %{rdynamic:-export-dynamic} %{!dynamic-linker:-dynamic-linker %(dynamic_linker)}} %{static:-static}}}

*lib:
%{pthread:-lpthread} %{shared:-lc} %{!shared:%{mieee-fp:-lieee} %{profile:-lc_p}%{!profile:-lc}}

Looks big but actually it is only insertion of one word.

Now how do we verify it? By creating a simple hello world program that is.

$ cat hello.cpp
#include

int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}

$ g++ -s -O2 -o hello hello.cpp;ls -la hello;readelf -a hello |grep -i hash
-rwxr-xr-x 1 shridhar users 3784 2007-06-24 22:00 hello*
[ 3] .hash HASH 08048168 000168 000044 04 A 4 0 4
02 .interp .note.ABI-tag .hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame
0x00000004 (HASH) 0x8048168

$ g++ -s -O2 -o hello hello.cpp;ls -la hello;readelf -a hello |grep -i hash
-rwxr-xr-x 1 shridhar users 3772 2007-06-24 22:00 hello*
[ 3] .gnu.hash GNU_HASH 08048168 000168 00002c 04 A 4 0 4
02 .interp .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame
0x6ffffef5 (GNU_HASH) 0x8048168
Histogram for `.gnu.hash' bucket list length (total of 3 buckets):

Of course, the specs file have been shuffled in another terminal.

Here are some links for references.
http://lwn.net/Articles/192624/
http://gentoo-wiki.com/HOWTO_Hashstyle
http://go-oo.org/~michael/OOoStartup.pdf
http://sourceware.org/ml/libc-alpha/2006-06/msg00095.html

fedora
http://docs.fedoraproject.org/release-notes/fc6/en_US/sn-Devel.html
http://download.fedora.redhat.com/pub/fedora/linux/core/6/i386/os/RELEASE-NOTES-en_US.html
http://www.fefe.de/dietlibc/FAQ.txt
http://groups.google.com/group/linux.kernel/browse_thread/thread/f4ac8e0074d4cfa7/13c34f6f99798ebb?lnk=st&q=fedora+%22--hash-style%22&rnum=1#13c34f6f99798ebb
http://fedoranews.org/cms/node/1304

http://farragut.flameeyes.is-a-geek.org/articles/2006/10/17/my-thoughts-about-the-gnu-hash-style
http://dev.laptop.org/ticket/124
http://www.mail-archive.com/performance-list%40gnome.org/msg00400.html
http://www.elfenbeinturm.cc/2006/07/

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options