This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Weak symbols not overriden
- From: Pan ruochen <panruochen at gmail dot com>
- To: gcc-help <gcc-help at gcc dot gnu dot org>, binutils <binutils at sourceware dot org>
- Date: Thu, 17 Mar 2011 16:11:22 +0800
- Subject: Weak symbols not overriden
Hi All,
I found weak symbols wouldn't be overriden by the strong symbols with
same names.
Here is my test code.
$cat s1.c
extern void foo(void) __attribute__((weak, visibility("default")));
extern void function1(void) __attribute__((visibility("default")));
void foo(void) {
printf("Enter %s:%s\n", __FILE__,__FUNCTION__);
}
void function1(void) {
printf("Enter %s:%s\n", __FILE__,__FUNCTION__);
}
$cat s2.c
extern void foo(void) __attribute__((visibility("default")));
extern void function2(void) __attribute__((visibility("default")));
void foo(void) {
printf("Enter %s:%s\n", __FILE__,__FUNCTION__);
}
void function2(void) {
printf("Enter %s:%s\n", __FILE__,__FUNCTION__);
}
$cat main.c
extern void foo(void);
extern void function1(void);
extern void function2(void);
int main() {
foo();
function1();
function2();
return 0;
}
$gcc -fpic -shared -fvisibility=hidden -O2 -o libs1.so s1.c
$gcc -fpic -shared -fvisibility=hidden -O2 -o libs2.so s2.c
$gcc -O2 -o foo -Wl,-rpath=. main.c libs1.so libs2.so
$nm -D libs1.so
00000480 W foo
$nm -D libs2.so
00000480 T foo
$./foo
Enter s1.c:foo
Enter s1.c:function1
Enter s2.c:function2
The result means the `foo' function in libs1.so is called, not that in
libso2.so,
even if `foo' is a weak in libs1.so and is strong in libso2.so.
What's wrong?
PRC
Mar 17,2011