Weak symbol link on Mac OS X

北城以北 提交于 2019-12-06 20:42:00

问题


Currently I encountered a weak link issue on Mac OS X 10.6.7 with Xcode 4.0.2.

robin@chameleon:/tmp/o$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)

As the document http://developer.apple.com/library/mac/#technotes/tn2064/_index.html said, we can use gcc attribute((weak_import)) for weak link symbol. However, the following sample code always throw compile error. As the following:

weak.c:

#include <stdlib.h>
#include <stdio.h>

extern int SayHello() __attribute__((weak));

int main()
{
    int result;

    if (SayHello!=NULL)
    {
        printf("SayHello is present!\n");
        result=SayHello();
    }
    else
        printf("SayHello is not present!\n");
}

The error message is the following:

robin@chameleon:/tmp/o$ gcc weak.c 
Undefined symbols for architecture x86_64:
  "_f", referenced from:
      _main in cceOf2wN.o
     (maybe you meant: __dyld_func_lookup)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Even if use option -undefined dynamic_lookup, it still throws error at runtime:

robin@chameleon:/tmp/o$ gcc -undefined dynamic_lookup weak.c 
robin@chameleon:/tmp/o$ ./a.out 
dyld: Symbol not found: _SayHello
  Referenced from: /private/tmp/o/./a.out
  Expected in: dynamic lookup

Trace/BPT trap

The nm -m message of "a.out" is the following:

robin@chameleon:/tmp/o$ nm -m a.out  | grep Hello

(undefined) external _SayHello (dynamically looked up)

Which was expected as the following:

(undefined) weak external _SayHello (dynamically looked up)

However, when I compile on Ubuntu with gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5, it works as expected:

weak.c:

#include <stdlib.h>
#include <stdio.h>

extern int SayHello() __attribute__((weak));

int main()
{
    int result;

    if (SayHello!=NULL)
    {
        printf("SayHello is present!\n");
        result=SayHello();
    }
    else
        printf("SayHello is not present!\n");
}

  robin@robinz:/tmp/o$ gcc weak.c robin@robinz:/tmp/o$ ./a.out SayHello is not present!

The symbol of SayHello in binary is:

robin@robinz:/tmp/o$ nm a.out | grep Hello
w SayHello

"w" The symbol is a weak symbol that has not been specifically tagged as a weak object symbol.

And I test the old xcode 3.2, it works as expected.

Could anyone help me on this? Was it a bug of ld?

And I found more interested things. When I create a dummy lib to export the SayHello symbol in dynamic lib, it works as expected.

dummy.c:

int SayHello() {
    return;
}

 

robin@chameleon:/tmp/o$ gcc -dynamiclib -o libdummy.dylib dummy.c 
robin@chameleon:/tmp/o$ gcc weak.c libdummy.dylib 
robin@chameleon:/tmp/o$ ./a.out 
SayHello is present!

If the "libdummy.dylib" does not exist:

robin@chameleon:/tmp/o$ rm libdummy.dylib 
robin@chameleon:/tmp/o$ ./a.out 
SayHello is not present!

Works as expected! Weak symbol now in nm message, as expected:

robin@chameleon:/tmp/o$ nm -m a.out | grep Hello

                 (undefined) weak external _SayHello (from libdummy)

来源:https://stackoverflow.com/questions/6009321/weak-symbol-link-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!