Assembler .weak directive does only work with Cross Compile GCC

非 Y 不嫁゛ 提交于 2019-12-11 08:05:54

问题


I have a program which I compile with 2 different compilers:

GCC 3.4.4 Cross Compiler for PowerPC

GCC 4.8.1 MinGW Compiler

In the program I am using the assembler directive .weak . The documentation says:

Makes a symbol with weak binding globally visible to the linker.

So I am going like this:

__asm__(".weak " "foo" "\n.set " "foo" "," "dummy_foo" "\n");

To cleare foo weak and give it an alias to dummy_foo.

This code works fine under GCC 3.4.4 when I cross compile für PowerPC but it doesn't work with GCC 4.8.1 when I compile for x86 target. - The Code compiles, but foo is not declared weak and my linker gives me an undefined reference. What is the problem here?

//edit:

As BSH suggested, it has to be:

__asm__(".weak " "_foo" "\n.set " "_foo" "," "_dummy_foo" "\n");

If I put this line into the same C-File as my declaration of foo() it works fine. The problem still persists when I put it in a seperate C-File (then it works for the GCC 3.4.4 Cross Compiler, but not for the GCC 4.8.1 )


回答1:


In MinGW, symbols are prefixed with an underscore, _foo, you need to change it to:

__asm__(".weak " "_foo" "\n.set " "_foo" "," "_dummy_foo" "\n");

or consider using the attribute __attribute__((weak, alias("dummy_foo"))) with foo instead.



来源:https://stackoverflow.com/questions/19765335/assembler-weak-directive-does-only-work-with-cross-compile-gcc

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