This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/49012] weak const optimisations
- From: "etienne_lorrain at yahoo dot fr" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 16 May 2011 14:52:51 +0000
- Subject: [Bug c/49012] weak const optimisations
- Auto-submitted: auto-generated
- References: <bug-49012-4@http.gcc.gnu.org/bugzilla/>
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.