{"id":2388,"date":"2012-02-23T10:46:50","date_gmt":"2012-02-23T09:46:50","guid":{"rendered":"http:\/\/glandium.org\/blog\/?p=2388"},"modified":"2012-02-23T10:46:50","modified_gmt":"2012-02-23T09:46:50","slug":"fun-with-weak-symbols","status":"publish","type":"post","link":"https:\/\/glandium.org\/blog\/?p=2388","title":{"rendered":"Fun with weak symbols"},"content":{"rendered":"<p>Consider the following <code>foo.c<\/code> source file:<\/p>\n<blockquote><p><code>extern int bar() __attribute__((weak));<br \/>\nint foo() {<br \/>\n&nbsp;&nbsp;return bar();<br \/>\n}<\/code><\/p><\/blockquote>\n<p>And the following <code>bar.c<\/code> source file:<\/p>\n<blockquote><p><code>int bar() {<br \/>\n&nbsp;&nbsp;return 42;<br \/>\n}<\/code><\/p><\/blockquote>\n<p>Compile both sources:<\/p>\n<blockquote><p><code>$ gcc -o foo.o -c foo.c -fPIC<br \/>\n$ gcc -o bar.o -c bar.c -fPIC<\/code><\/p><\/blockquote>\n<p>In the resulting object for <code>foo.c<\/code>, we have an undefined symbol reference to <code>bar<\/code>. That symbol is marked weak.<\/p>\n<p>In the resulting object for <code>bar.c<\/code>, the <code>bar<\/code> symbol is defined and not weak.<\/p>\n<p>What we expect from linking both objects is that the weak reference is fulfilled by the existence of the <code>bar<\/code> symbol in the second object, and that in the resulting binary, the <code>foo<\/code> function calls <code>bar<\/code>.<\/p>\n<blockquote><p><code>$ ld -shared -o test1.so foo.o bar.o<\/code><\/p><\/blockquote>\n<p>And indeed, this is what happens.<\/p>\n<blockquote><p><code>$ objdump -T test1.so | grep \"\\(foo\\|bar\\)\"<br \/>\n0000000000000260 g&nbsp;&nbsp;&nbsp;&nbsp;DF .text&nbsp;&nbsp;0000000000000007 foo<br \/>\n0000000000000270 g&nbsp;&nbsp;&nbsp;&nbsp;DF .text&nbsp;&nbsp;0000000000000006 bar<\/code><\/p><\/blockquote>\n<p>What do you think happens if the <code>bar.o<\/code> object file is embedded in a static library?<\/p>\n<blockquote><p><code>$ ar cr libbar.a bar.o<br \/>\n$ ld -shared -o test2.so foo.o libbar.a<br \/>\n$ objdump -T test2.so | grep \"\\(foo\\|bar\\)\"<br \/>\n0000000000000260 g&nbsp;&nbsp;&nbsp;&nbsp;DF .text&nbsp;&nbsp;0000000000000007 foo<br \/>\n0000000000000000&nbsp;&nbsp;w&nbsp;&nbsp;&nbsp;D&nbsp;&nbsp;*UND*&nbsp;&nbsp;0000000000000000 bar<\/code><\/p><\/blockquote>\n<p>The <code>bar<\/code> function is now undefined and there is a weak reference for the symbol. Calling <code>foo<\/code> will crash at runtime.<\/p>\n<p>This is apparently a feature of the linker. If anyone knows why, I would be interested to hear about it. Good to know, though.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider the following foo.c source file: extern int bar() __attribute__((weak)); int foo() { &nbsp;&nbsp;return bar(); } And the following bar.c source file: int bar() { &nbsp;&nbsp;return 42; } Compile both sources: $ gcc -o foo.o -c foo.c -fPIC $ gcc -o bar.o -c bar.c -fPIC In the resulting object for foo.c, we have an undefined [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,25],"tags":[23],"class_list":["post-2388","post","type-post","status-publish","format-standard","hentry","category-pdo","category-planet-mozilla","tag-en"],"_links":{"self":[{"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2388"}],"version-history":[{"count":17,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2388\/revisions"}],"predecessor-version":[{"id":2415,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2388\/revisions\/2415"}],"wp:attachment":[{"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/glandium.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}