首页 > 解决方案 > 编译为 FDPIC ELF 时出现分段错误

问题描述

我有一个简单的 c helloworld 程序。

#include <stdio.h>

int main() {
   printf("Hello World!\n");
   return 0;
}

当我正常编译它时( arm-linux-gnueabi-gcc helloworld.c -o out.o),它执行没有任何问题,但是当我尝试将它编译为 FDPIC 精灵( arm-linux-gnueabi-gcc -shared -fPIC -fPIE helloworld.c -o out.o)时,它崩溃了。

$ qemu-arm -cpu cortex-a7 out.o
qemu: uncaught target signal 11 (Segmentation fault) - core dumped
Segmentation fault

标签: clinuxarmexecutableelf

解决方案


这个:

 arm-linux-gnueabi-gcc -shared -fPIC -fPIE helloworld.c -o out.o

建立一个共享库。共享库缺少启动代码,无法正常执行。

也许您想构建一个 PIE 可执行文件?如果是这样,请执行以下操作:

 arm-linux-gnueabi-gcc -pie -fPIE helloworld.c -o out.pie

推荐阅读