Я написал следующую простую C программу
#include <stdio.h>
int main()
{
printf("Hello\n");
return 0;
}
После компиляции я загружаю эту программу в GDB и продолжаю следующим образом
> catch syscall brk
*run and wait for GDB to catch syscall*
> info proc mappings
Start Addr End Addr Size Offset
0x8000000 0x8001000 0x1000 0x0 <--- this region is r-xp, so this is the code in memory
0x8200000 0x8202000 0x2000 0x0 <--- this region is rw-p, so this is the data in memory
> dump binary memory testdump 0x8000000 0x8001000
(причина, по которой я установил точка перехвата на brk заключается в том, что этот системный вызов вызывается перед выполнением программы, но с программой, загруженной в память)
Использование readelf в дампе памяти дает следующий вывод
$ readelf -h testdump
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: DYN (Shared object file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x530
Start of program headers: 64 (bytes into file)
Start of section headers: 6440 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 29
Section header string table index: 28
readelf: Error: Reading 1856 bytes extends past end of file for section headers
readelf: Error: Section headers are not available!
Мне было интересно, как я могу исправить эти ошибки и создать исполняемый двоичный файл ELF из этого дампа памяти, который работает так же, как и оригинальная программа. Обратите внимание, что я хочу сделать это только с помощью шестнадцатеричного редактора (например, hexedit ) и без использования исходного двоичного файла.