启动期间生成的ELF可执行段错误 [英] Generated ELF executable segfaults during startup
问题描述
我正在生成一个ELF可执行文件,并将.text节加载到LOAD段中.它可以很好地反汇编,但是尝试在 gdb
下运行它会给出在启动程序期间因信号SIGSEGV终止,出现分段错误.
I'm generating an ELF executable with a .text section loaded into a LOAD segment. It disassembles fine, but trying to run it under gdb
gives During startup program terminated with signal SIGSEGV, Segmentation fault.
readelf
给出:
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: 0x400056
Start of program headers: 759 (bytes into file)
Start of section headers: 503 (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: 64 (bytes)
Number of section headers: 4
Section header string table index: 2
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .text PROGBITS 0000000000400040 00000040
000000000000005b 0000000000000000 AX 0 0 16
[ 2] .strtab STRTAB 0000000000000000 0000009b
000000000000009c 0000000000000000 0 0 4
[ 3] .symtab SYMTAB 0000000000000000 00000137
00000000000000c0 0000000000000018 2 1 4
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
O (extra OS processing required) o (OS specific), p (processor specific)
There are no section groups in this file.
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000040 0x0000000000400000 0x0000000000400000
0x000000000000009b 0x000000000000009b R E 200000
Section to Segment mapping:
Segment Sections...
00 .text
There is no dynamic section in this file.
There are no relocations in this file.
The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently supported.
Symbol table '.symtab' contains 8 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000000 0 FUNC GLOBAL DEFAULT 1
2: 0000000000400040 16 FUNC GLOBAL DEFAULT 1 _R_Potato
3: 0000000000400050 6 FUNC GLOBAL DEFAULT 1 _R_Main
4: 0000000000000000 0 SECTION LOCAL DEFAULT 1
5: 0000000000400056 18 FUNC GLOBAL DEFAULT 1 _start
6: 0000000000400068 25 FUNC GLOBAL DEFAULT 1 GetStringLength
7: 0000000000400081 26 FUNC GLOBAL DEFAULT 1 _R_Print
如果需要,可以在此处中找到十六进制转储.我假设问题出在内核试图映射LOAD段中的内容,但是我没有看到问题.这是怎么回事?
The hexdump, if needed, can be found here. I'm assuming the problem is with the kernel trying to mmap the stuff from the LOAD segment, but I'm failing to see a problem. What's going on here?
推荐答案
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
LOAD 0x0000000000000040 0x0000000000400000 0x0000000000400000
0x000000000000009b 0x000000000000009b R E 200000
这是您的问题所在:您正在要求内核执行等效的 mmap(0x400000、0x9b,PROT_READ | PROT_EXEC,MAP_FIXED,$ fd,0x40)
,但是这样的mmap
调用始终会失败,并显示 EINVAL
,因为地址和偏移量不是等效的模对齐方式.
This is your problem right there: you are asking the kernel to perform an equivalent of mmap(0x400000, 0x9b, PROT_READ|PROT_EXEC, MAP_FIXED, $fd, 0x40)
, but such mmap
call will always fail with EINVAL
because the address and offset are not equivalent modulo alignment.
您希望文件偏移量为 0
,而不是 0x40
.也就是说,您的 LOAD
段应该同时覆盖 和 Elf64_Ehdr
和程序 .text
(您的 LOAD
段的大小已经包括了两者).
You want file offset to be 0
, not 0x40
. That is, your LOAD
segment should cover both the Elf64_Ehdr
and program .text
together (the size of your LOAD
segment already includes both).
这篇关于启动期间生成的ELF可执行段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!