c - 使用 GCC 在 Linux 上生成“平面”hello world 二进制文件
问题描述
我试图让 gcc 生成一个“平面二进制”hello world 程序,即一个可重定位的自包含程序,它由一个组合 .text、.data 和 .rodata 的部分组成,其入口和出口点只是文件。
到目前为止,我已经编写了以下 C 代码:
#include <sys/types.h>
static inline ssize_t _write(int fd, const void *buf, size_t count) {
ssize_t ret;
__asm__ volatile(
"mov %0, %%rdi\n"
"mov %1, %%rsi\n"
"mov %2, %%rdx\n"
"mov $1, %%rax\n"
"syscall\n"
:
: "g"(fd), "g"(buf), "g"(count));
__asm__ volatile ("mov %%rax, %0" : "=r"(ret));
return ret;
}
int _start() {
char const *msg = "hello world\n";
_write(0, msg, sizeof(msg));
__builtin_unreachable(); /* don't generate ret statement */
}
我正在尝试用以下方式编译它:
gcc -std=c99 -fPIC -fno-stack-protector -c hello.c -o hello.o
gcc -N -nostdlib -fno-stack-protector hello.c -o hello
但这会导致:
/usr/bin/ld: hello: error: PHDR segment not covered by LOAD segment
collect2: error: ld returned 1 exit status
这似乎是由-N
链接器选项引起的,但我不知道究竟是为什么。
我正在研究5.12.12-arch1-1
GCC11.1.0
和 GNU ld 2.36.1
。
解决方案
推荐阅读
- java - 读取用户输入
- opencart - 如何在 openchart 3.0.2.0 上安装扩展语言“bahasa indonesia”?我在 linux ubuntu 18.04 上使用 xampp
- android - 如何在没有键名的情况下进行改造查询参数请求
- docker - 使用 Docker 在 React Native 中热重载不起作用
- android - 按钮单击事件不起作用 - 为什么会这样?
- database - 关系不存在(golang/mac OS)
- delphi - 为什么将 varEmpty 与零评估的 varInteger 进行比较时,“=”运算符的结果为真?
- java - Java 应用程序在 Eclipse 中运行,但不是作为独立 jar
- ruby-on-rails - 如何使用 Rails 中的 API 从 Stripe 帐户向客户发送付款?
- angular - 如何在组件创建时只进行一次 HTTP 调用