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: unassig...@gcc.gnu.org
        ReportedBy: etienne_lorr...@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

Reply via email to