public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/49012] New: weak const optimisations
@ 2011-05-16 14:13 etienne_lorrain at yahoo dot fr
2011-05-16 14:20 ` [Bug c/49012] " jakub at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: etienne_lorrain at yahoo dot fr @ 2011-05-16 14:13 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49012
Summary: weak const optimisations
Product: gcc
Version: 4.6.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: etienne_lorrain@yahoo.fr
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/49012] weak const optimisations
2011-05-16 14:13 [Bug c/49012] New: weak const optimisations etienne_lorrain at yahoo dot fr
@ 2011-05-16 14:20 ` jakub at gcc dot gnu.org
2011-05-16 14:56 ` etienne_lorrain at yahoo dot fr
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-16 14:20 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49012
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> 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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/49012] weak const optimisations
2011-05-16 14:13 [Bug c/49012] New: weak const optimisations etienne_lorrain at yahoo dot fr
2011-05-16 14:20 ` [Bug c/49012] " jakub at gcc dot gnu.org
@ 2011-05-16 14:56 ` etienne_lorrain at yahoo dot fr
2011-05-17 9:56 ` rguenth at gcc dot gnu.org
2011-07-23 23:38 ` [Bug middle-end/49012] " pinskia at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: etienne_lorrain at yahoo dot fr @ 2011-05-16 14:56 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49012
--- Comment #2 from etienne_lorrain at yahoo dot fr 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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c/49012] weak const optimisations
2011-05-16 14:13 [Bug c/49012] New: weak const optimisations etienne_lorrain at yahoo dot fr
2011-05-16 14:20 ` [Bug c/49012] " jakub at gcc dot gnu.org
2011-05-16 14:56 ` etienne_lorrain at yahoo dot fr
@ 2011-05-17 9:56 ` rguenth at gcc dot gnu.org
2011-07-23 23:38 ` [Bug middle-end/49012] " pinskia at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-05-17 9:56 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49012
--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-05-17 08:39:50 UTC ---
I suppose PR47278 might be related.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug middle-end/49012] weak const optimisations
2011-05-16 14:13 [Bug c/49012] New: weak const optimisations etienne_lorrain at yahoo dot fr
` (2 preceding siblings ...)
2011-05-17 9:56 ` rguenth at gcc dot gnu.org
@ 2011-07-23 23:38 ` pinskia at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-07-23 23:38 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49012
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Component|c |middle-end
Resolution| |INVALID
--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-07-23 23:37:49 UTC ---
Invalid as you need to compile with -fPIC to be able to change weak symbols.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2011-07-23 23:38 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-16 14:13 [Bug c/49012] New: weak const optimisations etienne_lorrain at yahoo dot fr
2011-05-16 14:20 ` [Bug c/49012] " jakub at gcc dot gnu.org
2011-05-16 14:56 ` etienne_lorrain at yahoo dot fr
2011-05-17 9:56 ` rguenth at gcc dot gnu.org
2011-07-23 23:38 ` [Bug middle-end/49012] " pinskia at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).