With newer GCC compilers, const struct { int a,b; } mystruct = {15, 0}; int adder (int x) { return x + mystruct.b; }; there isn't any addition performed in adder() because GCC knows that mystruct.b is null, and assumes that nobody is initialising mystruct.b is assembler or declaring mystruct as non-const in another module. But if we add the weak attribute to mystruct, GCC-4.4.5 does the addition in case mystruct has been pre-loaded by for instance LD_PRELOAD, GCC-4.6 do not do the addition. const struct { int a,b; } mystruct __attribute__((weak))= {15, 0}; int adder (int x) { return x + mystruct.b; }; LD_PRELOAD=/home/etienne/projet/toolchain/lib/libmpc.so.2:/home/etienne/projet/toolchain/lib/libgmp.so.10 /home/etienne/projet/toolchain/bin/gcc -Os -fomit-frame-pointer -S t.c -o t.s gives: .file "t.c" .text .globl adder .type adder, @function adder: .LFB0: .cfi_startproc movl 4(%esp), %eax ret .cfi_endproc .LFE0: .size adder, .-adder .weak mystruct .section .rodata .align 4 .type mystruct, @object .size mystruct, 8 mystruct: .long 15 .long 0 .ident "GCC: (GNU) 4.6.0" .section .note.GNU-stack,"",@progbits
You aren't compiling with -fpic, therefore the weak attribute is there irrelevant - the symbol is known to be defined in the current TU, and for anything that goes into the executable it can't be overridden. Note LD_PRELOAD inserts preloaded lib symbol search scope after executables, not before.
Well, with gcc-4.4.5-8 the weak attribute did the trick: $ gcc -v Using built-in specs. Target: i486-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu Thread model: posix gcc version 4.4.5 (Debian 4.4.5-8) $ cat t.c //const struct { int a,b; } mystruct __attribute__((weak))= {15, 0}; const struct { int a,b; } mystruct = {15, 0}; int adder (int x) { return x + mystruct.b; }; $ gcc -Os -fomit-frame-pointer t.c -S -o t.s $ cat t.s .file "t.c" .text .globl adder .type adder, @function adder: movl 4(%esp), %eax ret .size adder, .-adder .globl mystruct .section .rodata .align 4 .type mystruct, @object .size mystruct, 8 mystruct: .long 15 .long 0 .ident "GCC: (Debian 4.4.5-8) 4.4.5" .section .note.GNU-stack,"",@progbits $ cat t.c const struct { int a,b; } mystruct __attribute__((weak))= {15, 0}; //const struct { int a,b; } mystruct = {15, 0}; int adder (int x) { return x + mystruct.b; }; $ cat t.s .file "t.c" .text .globl adder .type adder, @function adder: movl 4(%esp), %eax addl mystruct+4, %eax ret .size adder, .-adder .weak mystruct .section .rodata .align 4 .type mystruct, @object .size mystruct, 8 mystruct: .long 15 .long 0 .ident "GCC: (Debian 4.4.5-8) 4.4.5" .section .note.GNU-stack,"",@progbits But in fact I just discovered "volatile const" structures which enables me to have a C constant initialised at run-time by the assembly preceding main(), so personally I do not need this "weak" trick. Thanks for the quick answer, Etienne.
I suppose PR47278 might be related.
Invalid as you need to compile with -fPIC to be able to change weak symbols.