2015-10-19 67 views
4

我需要學習手動創建ELF可執行文件。到目前爲止,我一直在利用在線指南,如:用SIGSEGV手動創建ELF可執行文件崩潰

經過多次失敗我簡化我的程序下面的(它應該只是出口返回代碼0):

0000000: 7f45 4c46 0101 0100 0000 0000 0000 0010 .ELF............ 
0000010: 0200 0300 0100 0000 8080 0408 3400 0000 ............4... 
0000020: 0000 0000 0000 0000 3400 2000 0100 2800 ........4. ...(. 
0000030: 0000 0000 0100 0000 5400 0000 8080 0408 ........T....... 
0000040: 0000 0000 0c00 0000 0c00 0000 0500 0000 ................ 
0000050: 0010 0000 b801 0000 00bb 0000 0000 cd80 ................ 

當我嘗試執行它時,它與SIGSEGV崩潰。 GDB打印:

During startup program terminated with signal SIGSEGV, Segmentation fault. 

我做錯了什麼?

+0

的可能的複製[如何製作一個可執行在Linux中使用十六進制編輯器的ELF文件?](http://stackoverflow.com/questions/26294034/how-to-make-an-executable-elf-file-in-linux-using-a-hex-editor)或否則它是調試這個「代碼」問題。 –

回答

2

有了您的二進制文件,我正在從GDB不同的輸出:

(gdb) r 
Starting program: /tmp/sample.elf.bad 
During startup program terminated with signal SIGKILL, Killed. 

在二進制展望:

readelf -l sample.elf 

Elf file type is EXEC (Executable file) 
Entry point 0x8048080 
There are 1 program headers, starting at offset 52 

Program Headers: 
    Type   Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align 
    LOAD   0x000054 0x08048080 0x00000000 0x0000c 0x0000c R E 0x1000 

這裏你問內核mmap與文件段偏移0x54在虛擬地址0x08048080

由於這兩個數字不等於彼此模頁面大小,內核拒絕:

strace ./sample.elf 
execve("./sample.elf", ["./sample.elf"], [/* 42 vars */] <unfinished ...> 
+++ killed by SIGKILL +++ 
Killed 

以上strace的意味着內核試圖創建的過程中,並沒有像它所認爲,和終止它與偏見。沒有執行二進制文件的單個指令。

殺青LOAD虛擬地址和入口點是0x08048054生產所需的工作可執行:

strace ./sample.elf 
execve("./sample.elf", ["./sample.elf"], [/* 42 vars */]) = 0 
[ Process PID=23172 runs in 32 bit mode. ] 
_exit(0)        = ? 
+++ exited with 0 +++ 

這裏是它的hexdump都:

hd ./sample.elf 
00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 10 |.ELF............| 
00000010 02 00 03 00 01 00 00 00 54 80 04 08 34 00 00 00 |........T...4...| 
00000020 00 00 00 00 00 00 00 00 34 00 20 00 01 00 28 00 |........4. ...(.| 
00000030 00 00 00 00 01 00 00 00 54 00 00 00 54 80 04 08 |........T...T...| 
00000040 00 00 00 00 0c 00 00 00 0c 00 00 00 05 00 00 00 |................| 
00000050 00 10 00 00 b8 01 00 00 00 bb 00 00 00 00 cd 80 |................| 
00000060 
+0

我得到這個錯誤./sample.elf:行1:7F:命令未找到8次。有沒有步驟來手動運行寫入精靈? – shami

+1

@shami這是ELF文件的十六進制轉儲(二進制文件)。你不能運行十六進制轉儲(你似乎已經嘗試過),而不能運行源代碼。要將字節轉換爲二進制,你可以使用'perl''packet'例程,或者寫一個簡單的「編譯器」。 –

+0

感謝您的回覆。我正在關注操作的鏈接。據此,它應該在chmod + x之後運行。爲了清楚起見,我添加了elf標題,文本標題,數據標題,然後是hello word program hex。 – shami