Override GCC linker symbols in c code using weak declaration

瘦欲@ 提交于 2021-01-28 09:02:26

问题


I am building an elf target. I have a linker script where I input some of the symbol locations like(these symbols are defined in a different locations like ROM whose address is provided below),

A = 0x12345678;
B = 0x1234567c;
D = 0x1234568c;

In the C code I can use these variables A and B without declaring them which is expected. I want to know if I can override the symbol D i.e., My current executable can have its own declaration of D. In that case the linker should ignore D. Is there a way to declare the symbols in linker script as 'weak'? so that the linker can use 'input symbols' only if it is not declared in any of the linked objects.


回答1:


Use PROVIDE directive

PROVIDE(D = 0x1234568c);

From ld documentation

In some cases, it is desirable for a linker script to define a symbol only if it is referenced and is not defined by any object included in the link. … If, on the other hand, the program defines … the linker will silently use the definition in the program.



来源:https://stackoverflow.com/questions/53441952/override-gcc-linker-symbols-in-c-code-using-weak-declaration

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