【问题标题】:Can the GCC linker emit an error when a specific symbol is included in the link?当链接中包含特定符号时,GCC 链接器是否会发出错误?
【发布时间】:2023-02-24 02:43:48
【问题描述】:

我想禁止将一组特别命名的符号链接到我的可执行文件中,以确保我的可执行文件没有携带我不想要的功能和依赖项。

在我的具体情况下,这些符号来自 ARM Cortex-M MCU 上的libc_nano.a,但我希望应用程序级代码以及“更大”的计算机上也有此功能,所以我的问题是通用的。

如果最终链接中包含命名符号,我没有看到会触发错误的链接器标志,但我认为这会做我想要的。

有没有办法用 GCC 链接器实现这一点?

【问题讨论】:

  • 让它发出一个地图文件,然后解析它。或者使用objdump 并解析它。或者使用nm 并解析它。
  • 我问这个问题是关于将此作为调用 GCC 链接器的一部分,因为我明确不想编写强大的链接后构建系统步骤来执行此操作。
  • 您可能可以在链接描述文件中做一些事情。比如ASSERT(DEFINED(<symbol>), "Error!")
  • 您要避免的是“稳健”部分还是“链接后步骤”部分?后者对我来说很有意义,但前者回避了鲁棒性问题反对什么?您已经是特定于工具链的,因此可移植性似乎不是您关心的问题。
  • 做一个测试链接,其中包括一个定义符号的目标模块(在任何提供它们的库之后列出)。如果该测试链接出现错误(由于多重定义的符号),请报告错误并停止构建。否则,继续进行没有附加目标模块的常规链接。

标签: c gcc linker


【解决方案1】:

有趣的问题,下面是一个可能有帮助的例子。示例代码来自Hello World for bare metal ARM

如上所述,这种方法使用ASSERT ( DEFINED( symbol, "message" ) )

问题

. . .禁止将一组特别命名的符号链接到我的可执行文件中。 . .有没有办法用 GCC 链接器实现这一点?

输出

在此输出中,符号 print_uart0 是被排除的符号。

arm-none-eabi-ld -T test.ld test.o startup.o -o test.elf
arm-none-eabi-ld: OOPS, THE SYMBOL print_uart0 IS DEFINED

过程

  1. 将输入构建到工作 ARM 示例中
  2. 修改链接器脚本以防止在包含print_uart0时链接
    arm-none-eabi-as -mcpu=arm926ej-s -g startup.s -o startup.o
    arm-none-eabi-gcc -c -mcpu=arm926ej-s -g test.c -o test.o
    arm-none-eabi-ld -T test.ld test.o startup.o -o test.elf
    arm-none-eabi-objcopy -O binary test.elf test.bin
    qemu-system-arm -M versatilepb -m 128M -nographic -kernel test.bin
    # ctrl-a x
    
    arm-none-eabi-ld -T test-no-print-uart0.ld test.o startup.o -o test.elf
    arm-none-eabi-ld: OOPS, THE SYMBOL print_uart0 IS DEFINED
    

    输入

    启动.s

    .global _Reset
    _Reset:
     LDR sp, =stack_top
     BL c_entry
     B .
    

    测试.c

    volatile unsigned int * const UART0DR = (unsigned int *)0x101f1000;
     
    void print_uart0(const char *s) {
     while(*s != '
猜你喜欢
  • 2011-07-23
  • 2014-12-28
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
相关资源
最近更新 更多