- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试手动编写尽可能小的 x86_64 ELF hello world 程序,但在尝试运行它时收到段错误。
gdb 说:在启动过程中程序以信号 SIGSEGV 终止,段错误。
这是十六进制转储:
00000000: 7f45 4c46 0201 0100 0000 0000 0000 0000 .ELF............
00000010: 0200 3e00 0100 0000 7800 0000 0000 0000 ..>.....x.......
00000020: 4000 0000 0000 0000 0000 0000 0000 0000 @...............
00000030: 0000 0000 4000 3800 0100 0000 0000 0000 ....@.8.........
00000040: 0100 0000 0500 0000 0000 0000 0000 0000 ................
00000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000060: 3100 0000 0000 0000 3100 0000 0000 0000 1.......1.......
00000070: 0200 0000 0000 0000 b801 0000 00bf 0100 ................
00000080: 0000 be9a 0000 00ba 0a00 0000 0f05 b83c ..."...........<
00000090: 0000 00bf 0000 0000 0f05 4865 6c6c 6f2c ..........Hello,
000000a0: 2057 6f72 6c64 210a 00 World!..
这是 readelf -a 的输出:
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x78
Start of program headers: 64 (bytes into file)
Start of section headers: 0 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 1
Size of section headers: 0 (bytes)
Number of section headers: 0
Section header string table index: 0
There are no sections in this file.
There are no section groups in this file.
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000031 0x0000000000000031 R E 0x2
There is no dynamic section in this file.
There are no relocations in this file.
No processor specific unwind information to decode
Dynamic symbol information is not available for displaying symbols.
No version information found in this file.
代码如下:
0xb8 0x01 0x00 0x00 0x00 /* mov %rax, 1 ; sys_write */
0xbf 0x01 0x00 0x00 0x00 /* mov %rdi, 1 ; STDOUT */
0xbe 0x9a 0x00 0x00 0x00 /* mov %rsi, 0x9a ; address of string */
0xba 0x0a 0x00 0x00 0x00 /* mov %rdi, 15 ; size of string */
0x0f 0x05 /* syscall */
0xb8 0x3c 0x00 0x00 0x00 /* mov %rax, 60 ; sys_exit */
0xbf 0x00 0x00 0x00 0x00 /* mov %rdi, 0 ; exit status */
0x0f 0x05 /* syscall */
“Hello, World!\n”字符串紧随其后。我一直在用this MOV instruction .尝试使用程序头偏移量、对齐方式和虚拟地址字段没有产生任何结果。 manpage这部分有点困惑。我还尝试将此二进制文件与用汇编语言编写的二进制文件进行比较,但我发现没有任何用处。
现在回答我的问题:您能告诉我错误是什么和/或我该如何调试这个二进制文件吗?
最佳答案
I tried to write the smallest possible x86_64 ELF hello world program by hand
您应该为您的程序提供一个源代码,以便我们修复它。
gdb says:
During startup program terminated with signal SIGSEGV
这是 GDB 告诉你它调用了 fork/execve
来创建目标程序,并期望内核通知 GDB 该程序现在可以调试了。取而代之的是,内核通知 GDB 该程序已通过 SIGSEGV
终止,还没有到达它的第一条指令。
GDB 没有预料到这一点。为什么会这样?
当内核查看您的可执行文件并说“我无法从中创建正在运行的程序”时,就会发生这种情况。
为什么会这样?因为这个 LOAD
段:
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000031 0x0000000000000031 R E 0x2
要求内核将文件中偏移量 0
的 0x31
字节映射到虚拟地址 0
。但是内核(理所当然地)拒绝了这种无意义的请求,并在从 execve
返回之前用 SIGSEGV
终止了程序。
您可能可以通过制作文件 ET_DYN
而不是 ET_EXEC
来避免这种情况——这会将您的程序头的含义从“将此段映射到 0”更改为“将此段映射到任何地方”。
您绝对可以通过保留 ET_EXEC
来避免这种情况,但将段的 .p_vaddr
和 .p_paddr
更改为类似 0x10000
.
TL;DR:您的程序和文件头必须对内核有意义,否则您将永远无法启动。
关于linux - 最小的 x86_64 Hello World ELF 二进制文件是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72930779/
我们正在为大学编写一个模拟器项目,我们必须使用 ELF 文件作为输入,但我们不太确定 ELF 文件是什么样的。它应该包含我们可以执行的某些操作(所有这些都应该用二进制表示),但它是否包含其他内容?还是
我目前正在研究 ELF 格式。我想确认一下我认为正确的事情。 ELF是一种格式,它代表可执行和可链接格式。在linux中,一切都是ELF格式。 当使用gcc编译带有-c和-fPIC文件的代码时,它会将
基本上,我希望能够在 elf 二进制文件的 debug dwarf 部分中更改源文件的路径。 最佳答案 Basically I want to be able to change the path t
我想编写一个实用程序来从 ELF 二进制文件中删除程序头。例如,当我运行 readelf -l/my/elf 时,我会得到所有程序头的列表:PHDR INTERP ... GNU_STACK GNU_
考虑使用 GCC 编译的 C 标准 hello world 程序,没有任何开关。如 readelf -s说,它包含 64 个符号。它还说.symtab部分是 1024 字节长。然而,每个符号表条目有
我正在尝试为 ARM 平台编写 elf 可执行加载程序。我在这里有一些查询 1) 如何生成可重定位或位置无关的 ELF 可执行文件(编译器和链接器选项是什么) 2) 如何加载上面生成的 ELF 可执行
假设我有一个 lib xxx.so。所以我得到了所有的函数名和参数 以下命令: readelf -Ws xxx.so |c++filt 它将输出以下内容: 711: 00270209 40
我正在创建一个 ELF 可执行文件,我需要知道操作系统需要哪些部分才能加载和执行它。 Details: OS: Ubuntu 10.04 (64-bit)Kernel ve
SHT_NULL的目的是什么? ELF 中的部分? 它是由操作系统还是加载程序引用的? 这个部分的大小是多少? 它与 NULL 指针有什么关系吗? 此外,为什么此部分在部分段映射中没有条目? 来自 E
我正在学习 ELF 并且有一段时间的疑问。我试图寻找答案,但徒劳无功。如果有人能给我答案或引导我到地方寻找答案,我会很感激。 我读到的几乎所有关于 ELF 的文档都说 .text 部分包含可执行的二进
问题场景 : 简单来说,我们是否有一个 Trace32 命令来从加载到目标的 ELF 文件中读取符号(及其内容)?我们有这种特殊情况,其中 ELF 文件的应用程序特定调试符号作为 ELF 中“.nol
我试图从 Linux 程序的 elf 文件中提取特定的字符串变量(即符号),甚至从它来自的 .o 中提取。 它在 .rodata 部分,显然我知道符号名称。 是否有一系列 objdump 样式的命令和
在 Linux 上,我试图将静态链接的 ELF 文件剥离为基本要素。当我运行时: strip --strip-unneeded foo 或者 strip --strip-all foo 生成的文件仍然
我有一个 STM32,我将 ELF 文件加载到 RAM 中(使用 OpenOCD 和 JTAG)。到目前为止,我还没有真正关注我加载的 ELF 文件的大小。 通常,当我编译一个对我的板来说太大的 EL
我有一个 STM32,我将 ELF 文件加载到 RAM 中(使用 OpenOCD 和 JTAG)。到目前为止,我还没有真正关注我加载的 ELF 文件的大小。 通常,当我编译一个对我的板来说太大的 EL
我需要学习手动创建 ELF 可执行文件。到目前为止,我一直在使用在线指南,例如: Manually Creating an ELF Executable ELF reference 几次失败后,我将我
我在 readelf 实用程序的帮助下打开了我的对象 Sprite 文件: readelf -a ./my_object.o |较少的 结果我得到了很多有趣的信息。 我在部分表中看到了一个带有“GRO
ELF 文件包含两个结构来处理重定位: Elf64_Rel: typedef struct { Elf64_Addr r_offset; Uint64_t r_info; }
我一直在阅读 ELF 规范,但无法弄清楚程序入口点和 _start 地址从何而来。 看起来他们应该在一个非常一致的地方,但我做了一些琐碎的程序,_start 总是在不同的地方。 谁能澄清一下? 最佳答
这是我的测试。我有一个由源 main.c 和 misc.c 组成的 main 应用程序和一个由 lib.cname); m++; } return 0; } misc.
我是一名优秀的程序员,十分优秀!