为什么 TCC 不生成我的自定义部分?

问题描述 投票:0回答:1

我有一个小C程序:

#include <stdio.h>

struct foo {
  int x;
  struct foo *next;
};

#define CONST __attribute__((used, section("const_heap")))

struct foo f CONST = { 1, 0 };
struct foo g CONST = { 2, &g };

extern char *__start_const_heap;
extern char *__stop_const_heap;

int main() {
  printf("f: %p, g: %p\n", &f, &g);
  printf("start: %p, end %p\n", &__start_const_heap, &__stop_const_heap);
}

并且我希望能够检查指针是否在某个部分内

const_heap
。 GCC 和 Clang 编译并运行良好; TCC 对于
__start
__stop
符号存在链接器错误。

hickory% gcc const.c && ./a.out
f: 0x5c97a966d020, g: 0x5c97a966d030
start: 0x5c97a966d048, end 0x5c97a966d050
hickory% clang const.c && ./a.out
f: 0x574924409030, g: 0x574924409040
start: 0x574924409068, end 0x574924409070
hickory% tcc const.c
tcc: error: undefined symbol '__stop_const_heap'
tcc: error: undefined symbol '__start_const_heap'
hickory%

(确切的数字并不重要;只要程序运行并打印一个指针即可)

我用

readelf
检查了(未链接的)目标文件,果然,不存在
const_heap
部分。

TCC 显然支持这个,从 0.9.9 开始,我正在使用 0.9.27(更新了 20 多年)。甚至“TCC 代码”看起来也应该可以工作。我做错了什么? 编辑:刚刚下载并编译了最新的提交,但它仍然失败并出现相同的错误。

c linker tcc
1个回答
0
投票
tcc_add_linker_symbols()

中,被考虑的部分是

.text
.data
.data.ro
.bss
symtab
strtab
.note.gnu.property
、.note.ABI-tag
, .rela.text
.rodata.cst4
.eh_frame
.node.GNU-stack
。 然而,由于
.
,它们都被跳过,因为领先的
if (!isid(c) && !isnum(c)) goto next_sec;
    

© www.soinside.com 2019 - 2024. All rights reserved.