Hello,

I have a problem with linkage of weak symbols. I have a testcase where I define a weak symbol in module.c, and refer to it from main.c, and then I (optionally) want to override it with a definition in cb.c:

$ cat main.c
#include <stdio.h>

extern unsigned int x;
void module_print_x();

int main()
{
    printf("x %x\n", x);
    module_print_x();
    return 0;
}

$ cat module.c
#include <stdio.h>

unsigned int __attribute__ ((weak)) x = 0xdeadbeef;

int module_print_x()
{
    printf("module x: %x\n", x);

    return 0;
}

$ cat cb.c
unsigned int x = 0xbeefdead;

$ cat make.sh
CFLAGS="-Wall"
# GCC=/c/MinGW_4.8/mingw64/bin/gcc
GCC=gcc

${GCC} ${CFLAGS} -c module.c
${GCC} ${CFLAGS} -c main.c
${GCC} ${CFLAGS} -c cb.c -o cb.o

${GCC} ${CFLAGS} -o main module.o main.o
${GCC} ${CFLAGS} -o main-cb-ovrd module.o main.o cb.o

In Linux I receive the expected output:

$ ./main
x deadbeef
module x: deadbeef
$ ./main-cb-ovrd
x beefdead
module x: beefdead

On Windows I get a link error while trying to link main (without the strong symbol in cb.o):

$ sh -x make.sh
+ CFLAGS=-Wall
+ GCC=gcc
+ gcc -Wall -c module.c
+ gcc -Wall -c main.c
+ gcc -Wall -c cb.c -o cb.o
+ gcc -Wall -o main module.o main.o
module.o:module.c:(.text+0xb): undefined reference to `x'
main.o:main.c:(.text+0xf): undefined reference to `x'
collect2.exe: error: ld returned 1 exit status
+ gcc -Wall -o main-cb-ovrd module.o main.o cb.o

Looking at the files with nm I see the following:

$ nm cb.o
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 t .text
0000000000000000 D x

$ nm module.o
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 T module_print_x
0000000000000000 p .pdata
                 U printf
0000000000000000 r .rdata
0000000000000000 t .text
0000000000000000 D .weak.x.module_print_x
                 w x
0000000000000000 r .xdata

$ nm main.o
0000000000000000 b .bss
0000000000000000 d .data
0000000000000000 T main
                 U __main
                 U module_print_x
0000000000000000 p .pdata
                 U printf
0000000000000000 r .rdata
0000000000000000 t .text
                 U x
0000000000000000 r .xdata

I have tried with various versions of MinGW including GCC 4.8.0 with Binutils 2.23.2 and I get similar errors.

Any help would be appreciated!

Kind regards,

Ulf