简体   繁体   English

手工ELF文件

[英]ELF file by hand

Hey, I've created an ELF file by hand, it has two sections(.text and .shstrtab) and a programm header which loads the .text section. 嘿,我手工创建了一个ELF文件,它有两个部分(.text和.shstrtab)和一个加载.text部分的程序头。 The .text section is very small and it only consists of following three instructions... .text部分非常小,它只包含以下三条指令......

    # and exit
    movl    $0,%ebx       # first argument: exit code
    movl    $1,%eax       # system call number (sys_exit)
    int     $0x80         # call kernel

The readelf does not complain when I run it on this elf file. 当我在这个elf文件上运行时,readelf不会抱怨。 If I excute this file, then as soon as I execute it, it gets killed and the message "Killed" appears on the screen. 如果我执行此文件,那么一旦执行它,它就会被杀死并且屏幕上会出现“Killed”消息。 I've gone through the following post here at stackoverflow and I'm still going through it. 我已经在stackoverflow上看到了以下帖子,我仍然会经历它。

Now my concern is that this programm does not ask for any (additional)memory and also is it really possible to do an ELF by hand and expect it to be tolerated at all by the system?. 现在我担心的是这个程序不会要求任何(额外的)内存,而且是否真的可以手动执行ELF并期望系统可以容忍它?

Thank you, 谢谢,

The ELF loader can send SIGKILL to your process for a variety of reasons; ELF加载程序可以出于各种原因将SIGKILL发送到您的进程; you probably have a bad address and/or length somewhere in the headers. 你可能在标题中的某个地方有一个糟糕的地址和/或长度。

eg a PT_LOAD segment must map the appropriate part of the executable to a sensible address (the usual address for x86 Linux is 0x08048000, although that's probably not critical as long it is page aligned, not 0, and not too high) and the addresses in both the .text section header and the entry point in the ELF header need to match up with that. 例如,一个PT_LOAD段必须将可执行文件的相应部分映射到一个合理的地址(x86 Linux的通常地址是0x08048000,尽管这可能并不重要,只要它是页面对齐,而不是0,而不是太高)并且地址在.text段头和ELF头中的入口点都需要与之匹配。

There's no reason why you shouldn't be able to do this by hand (if the linker can create it, so can you!) - if you really want to. 没有理由你不能手工完成这个(如果链接器可以创建它,你也可以!) - 如果你真的想要。 But note that if you simply assemble then link with symbols stripped (the -s flag to ld below): 但请注意,如果你只是组装然后链接剥离的符号( -s标志到下面的ld ):

$ cat exit.s
.globl _start
_start:
 movl $0,%ebx
 movl $1,%eax
 int $0x80
$ as -o exit.o exit.s
$ ld -s -o exit exit.o
$ ./exit
$ hexdump -Cv exit
00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 03 00 01 00 00 00  54 80 04 08 34 00 00 00  |........T...4...|
00000020  74 00 00 00 00 00 00 00  34 00 20 00 01 00 28 00  |t.......4. ...(.|
00000030  03 00 02 00 01 00 00 00  00 00 00 00 00 80 04 08  |................|
00000040  00 80 04 08 60 00 00 00  60 00 00 00 05 00 00 00  |....`...`.......|
00000050  00 10 00 00 bb 00 00 00  00 b8 01 00 00 00 cd 80  |................|
00000060  00 2e 73 68 73 74 72 74  61 62 00 2e 74 65 78 74  |..shstrtab..text|
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000090  00 00 00 00 00 00 00 00  00 00 00 00 0b 00 00 00  |................|
000000a0  01 00 00 00 06 00 00 00  54 80 04 08 54 00 00 00  |........T...T...|
000000b0  0c 00 00 00 00 00 00 00  00 00 00 00 04 00 00 00  |................|
000000c0  00 00 00 00 01 00 00 00  03 00 00 00 00 00 00 00  |................|
000000d0  00 00 00 00 60 00 00 00  11 00 00 00 00 00 00 00  |....`...........|
000000e0  00 00 00 00 01 00 00 00  00 00 00 00              |............|
000000ec
$

...then the result is fairly minimal anyway (probably sufficiently minimal to compare with your failing hand-crafted file to see where you've gone wrong). ...然后结果是相当小的(可能足够小,以与你失败的手工文件比较,看看你哪里出错了)。

I recently also try to do similar experiments. 我最近也尝试做类似的实验。
During the process, I also get the error info when running the generated elf file, it seems 在此过程中,我似乎也在运行生成的elf文件时收到错误信息
results from wrong setting of the viraddr and the placement of the segments, diff your own 由于错误的viraddr设置和段的放置导致了自己的差异
elf file with a standard elf file(compiled and linked). 带有标准elf文件的elf文件(已编译和链接)。
By the way, to keey the simplicity of the standard a.out elf file, you can use the -s parameter 顺便说一下,为了遵守标准a.out elf文件的简单性,您可以使用-s参数
to eliminate the symbol tables in elf file: ld -s ao 消除elf文件中的符号表:ld -s ao

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM