Bug 49012 - weak const optimisations
Summary: weak const optimisations
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-16 13:19 UTC by etienne_lorrain
Modified: 2011-07-23 23:37 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description etienne_lorrain 2011-05-16 13:19:49 UTC
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
Comment 1 Jakub Jelinek 2011-05-16 13:46:10 UTC
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.
Comment 2 etienne_lorrain 2011-05-16 14:36:41 UTC
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.
Comment 3 Richard Biener 2011-05-17 08:39:50 UTC
I suppose PR47278 might be related.
Comment 4 Andrew Pinski 2011-07-23 23:37:49 UTC
Invalid as you need to compile with -fPIC to be able to change weak symbols.