首页 > 解决方案 > 如何为 GCC 运行链接描述文件

问题描述

我正在尝试运行一个简单的链接器脚本以了解链接的工作原理以及如何使用链接器脚本实现函数重新排序

这是我的 C 代码

void A(){
        printf("A\n");
}

void B(){
        printf("B\n");
}

void main(){
        B();
        A();
}

这是链接器脚本

SECTIONS
     {
       . = 0x10000;
       .text : { *(.text) }
       . = 0x8000000;
       .data : { *(.data) }
       .bss : { *(.bss) }
     }

当我尝试使用这个外部链接器脚本进行编译时,它给了我这个错误

/usr/lib64/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x14): undefined reference to `__init_array_start'
/usr/lib64/libc_nonshared.a(elf-init.oS): In function `__libc_csu_init':
(.text+0x1c): undefined reference to `__init_array_end'
/usr/bin/ld: cl: hidden symbol `__init_array_end' isn't defined
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

我的编译命令是这样的

gcc -Wl,-no-whole-archive -ffunction-sections -T simple.ld cache_ex.c -o cl

你能告诉我我做错了什么吗?

标签: cgcclinkerldlinker-scripts

解决方案


推荐阅读