Trying to write a simple Hello, World! program with ELF headers on x86_64 OS, I’m use Linux and write with c++. The program prints Hello, World! but its followed with “segmentation fault” and “(core dumped)” errors. I follow https://en.wikipedia.org/wiki/Executable_and_Linkable_Format resource along with Intel’s ISA sheet.
I’m new and learning about ELF headers so id really appreciate details and explanations to why the error shows up.
Note that the ELFHeader struct has these by default:
string e_ident_MAG[4] = {"7F", "45", "4C", "46"};
string e_ident_DATA[1] = {"01"};
string e_ident_VERSION[1] = {"01"};
string e_ident_OSABI[1] = {"03"};
string e_ident_ABIVERSION[1] = {"00"};
string e_ident_PAD[7] = {"00", "00", "00", "00", "00", "00", "00"};
string e_version[4] = {"01", "00", "00", "00"};
string e_phoff[8] = {"40", "00", "00", "00", "00", "00", "00", "00"};
string e_flags[4] = {"00", "00", "00", "00"};
string e_shstrndx[2] = {"00", "00"};
Otherwise here is how my code looks like, if you don’t see a field here then you can assume its all 00s.
#include <iostream>
#include <fstream>
#include <string>
#include "dataStructs.h"
using namespace std;
int main() {
dataStructs DS; // contains ELF format
ofstream hex_dump("output/hex_dump.txt", ios::trunc);
DS.ELFHeader.e_ident_CLASS[0] = "02";
DS.ELFHeader.e_type[0] = "02";
DS.ELFHeader.e_machine[0] = "3E";
DS.ELFHeader.e_entry[2] = "40";
DS.ELFHeader.e_entry[0] = "B0";
DS.ELFHeader.e_ehsize[0] = "40";
DS.ELFHeader.e_phentsize[0] = "38";
DS.ELFHeader.e_phnum[0] = "02";
hex_dump << DS.buildELFHeader();
dataStructs::PHt text;
text.p_type[0] = "01";
text.p_flags64[0] = "05";
text.p_offset[0] = "B0";
text.p_vaddr[2] = "40";
text.p_vaddr[0] = "B0";
text.p_paddr[2] = "40";
text.p_paddr[0] = "B0";
text.p_filesz[0] = "39";
text.p_memsz[0] = "39";
text.p_align[1] = "10";
hex_dump << DS.buildPHt(text);
dataStructs::PHt data;
data.p_type[0] = "01";
data.p_flags64[0] = "06";
data.p_offset[0] = "E9";
data.p_vaddr[2] = "60";
data.p_vaddr[0] = "E9";
data.p_paddr[2] = "60";
data.p_paddr[0] = "E9";
data.p_filesz[0] = "0D";
data.p_memsz[0] = "0D";
data.p_align[1] = "10";
hex_dump << DS.buildPHt(data);
hex_dump << "48B80100000000000000 48BF0100000000000000 48BEE900600000000000 48BA0D00000000000000 0F05"; // code 42
hex_dump << "48B83600000000000000 4831FF 0F05"; // exit call 15
hex_dump << "48656c6c6f2c20576f726c6421"; // hello world 13
hex_dump.close();
return 0;
}
The program does run and prints what its supposed to but I belive the problem may lay in the system exit code. The “code” and “exit call” commented lines would translate to:
mov rax, 1 ; syscall number for sys_write
mov rdi, 1 ; file descriptor 1 (standard output)
mov rsi, 0x00000000006000E9 ; pointer to the message to print
mov rdx, 13 ; length of the message
syscall ; invoke the syscall
mov rax, 60 ; syscall exit
xor rdi, rdi ; exit code 0
syscall
Terminal output:
kurisu@absolutesolver:~/Documents/VS Projects/moasm$ ./moasm.sh debug
Hello, World!./moasm.sh: line 39: 52771 Segmentation fault (core dumped) ./output/executable.bin
Hex dump:
00000000: 7f45 4c46 0201 0103 0000 0000 0000 0000 .ELF............
00000010: 0200 3e00 0100 0000 b000 4000 0000 0000 ..>.......@.....
00000020: 4000 0000 0000 0000 0000 0000 0000 0000 @...............
00000030: 0000 0000 4000 3800 0200 0000 0000 0000 [email protected].........
00000040: 0100 0000 0500 0000 b000 0000 0000 0000 ................
00000050: b000 4000 0000 0000 b000 4000 0000 0000 ..@.......@.....
00000060: 3900 0000 0000 0000 3900 0000 0000 0000 9.......9.......
00000070: 0010 0000 0000 0000 0100 0000 0600 0000 ................
00000080: e900 0000 0000 0000 e900 6000 0000 0000 ..........`.....
00000090: e900 6000 0000 0000 0d00 0000 0000 0000 ..`.............
000000a0: 0d00 0000 0000 0000 0010 0000 0000 0000 ................
000000b0: 48b8 0100 0000 0000 0000 48bf 0100 0000 H.........H.....
000000c0: 0000 0000 48be e900 6000 0000 0000 48ba ....H...`.....H.
000000d0: 0d00 0000 0000 0000 0f05 48b8 3600 0000 ..........H.6...
000000e0: 0000 0000 4831 ff0f 0548 656c 6c6f 2c20 ....H1...Hello,
000000f0: 576f 726c 6421 World!
You can clone my repo and try the code yourself, I will reply to this post if I fix the problem.
Also, you can use “./moasm.sh dump” to get the hex dump
and “./moasm.sh debug” to complie and run the program.
Tried asking ChatGPT, wasnt useful for identifing the problems but gave useful info when asked about specific stuff.
Kurisu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.