如果引用了某些符号,则导致链接失败 [英] Cause link to fail if certain symbol is referenced

查看:126
本文介绍了如果引用了某些符号,则导致链接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果代码引用了库中的某个符号,是否有办法使链接失败?

Is there a way to make linking fail if the code references a certain symbol from a library?

我似乎模糊地记得在链接脚本语言中有这样一条指令,但是显然它不是GNU LD(也许是错误的记忆).

I seem to remember dimly that there was such a directive in the linker script language, but apparently it was not GNU LD (maybe it's a false memory).

我需要它来防止第三方库的某些部分意外链接到应用程序.如果确实链接,则会添加一些静态初始化程序,这些初始化程序会在运行时造成严重破坏(这是一个嵌入式项目,因此环境有点古怪).我无法更改相关的第三方库.我想在构建时检测到错误.我想我可以编写一个构建后脚本来解析映射文件,如果发现有问题的部分就会发出错误,但是上面提到的[false?]内存提示我确定不能单独使用链接器来完成.

I need it to prevent some part of a third-party library from accidentally linking into the application. If it does link, it adds some static initializers which wreak havoc at runtime (it's an embedded project so the environment is a bit quirky). I cannot change the third-party library in question. I would like to detect the error at build time. I guess I can write a post-build script that parses the map file and issues an error if it finds the offending parts, but the mentioned above [false?] memory prompts me to ascertain it can't be done using the linker alone.

我正在使用GNU GCC工具链.

I'm using the GNU GCC toolchain.

推荐答案

好吧,出去吃午饭有助于我找到这个(非常明显的)解决方案:

Okay, stepping out for a lunch helped me find this (pretty obvious) solution:

/* stubs.c */

void ERROR_DO_NOT_REFERENCE_THIS_SYMBOL( void );

void Offending3rdPartyFunction( void )
{
    ERROR_DO_NOT_REFERENCE_THIS_SYMBOL();
}

此处,从未定义符号ERROR_DO_NOT_REFERENCE_THIS_SYMBOL.一个附加的源文件已添加到项目中,其中的存根(如上面显示的存根)必须禁止引用每个功能.如果代码确实引用了这些功能之一,则存根将优先于库提供的符号,并且链接将失败:

Here, the symbol ERROR_DO_NOT_REFERENCE_THIS_SYMBOL is never defined. An additional source file is added to the project, with stubs like the one shown above for each function that must not be referenced. If the code does reference one of these functions, the stub will take precedence over the library-provided symbol, and the link will fail:

test.cpp:(.text._TestFunc+0x4): undefined reference to `ERROR_DO_NOT_REFERENCE_THIS_SYMBOL()'
collect2.exe: error: ld returned 1 exit status

这篇关于如果引用了某些符号,则导致链接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆