【问题标题】:Remove section created with __attribute__((section))删除使用 __attribute__((section)) 创建的部分
【发布时间】:2018-12-23 12:11:27
【问题描述】:

我有许多函数和相应的单元测试。我想将测试嵌入代码库本身。我想出了以下解决方案:

void a() {
  // this code should be tested
}

__attribute__((section(".tests")))
void a_test() {
  // test if a() works
}

测试放在.tests 部分中,我可以将其剥离以进行发布构建。我尝试了以下方法:

$ gcc -c a.c -o a.orig.o # a.c is shown above
$ objcopy -R.tests a.orig.o a.o
objcopy: a.o: symbol `.tests' required but not present
objcopy:a.o: no symbols

我发现this question 描述了一个类似的问题,答案说这是因为该部分正在从某个地方交叉引用。

我认为.eh_frame 是有罪的部分,因为删除它会 工作:

$ objcopy -R.tests -R.eh_frame a.orig.o a.o
  • 我可以删除此部分而不产生任何后果吗?
  • 有没有更好的方法来解决这个问题?

【问题讨论】:

    标签: c elf objcopy


    【解决方案1】:

    丢弃链接描述文件中的部分。示例:

    SECTIONS {
      /DISCARD/ : { *(.tests) }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-08-23
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2016-02-13
      • 2021-01-19
      • 1970-01-01
      相关资源
      最近更新 更多