I'm either not understanding these instructions correctly or I'm doing something else wrong to get a segmentation fault, here's what I got for the assembly (I included asm/unistd_64.h)
Code:
typedef struct {
u8 mov0_edi_to_rsp[3];
u8 lea0_rsi_to_8rsp[5];
u8 call0_func[5];
u8 mov1_edi_to_edx[2];
u8 mov2_x0000003C[5];
u8 syscall_exit[2];
} start_t;
start_t begin__start = {
/* mov edi,DWORD PTR [rsp] #Move argc into param1 register */
{ 0x8B, 0x3C, 0x24 },
/* lea rsi,[rsp+0x8] #Move argv into param2 register */
{ 0x48, 0x80, 0x74, 0x24, 0x08 },
/* call 0x12345678 #Call main() */
{ 0xE8, 0x78, 0x56, 0x34, 0x12 },
/* mov edi,eax #Move result of main() into param1 register */
{ 0x89, 0xC7 },
/* mov eax,0x3C #Designate exit() for syscall instruction */
{ 0xB8, __NR_exit, 0x00, 0x00, 0x00 },
/* syscall */
{ 0x0F, 0x05 }
};
typedef struct {
u8 rex_rx[1];
u8 xor_eax_eax[2];
u8 ret[1];
} main_t;
main_t begin_main = {
{0x48}, /* treat following dst & src as qword */
{0x31, 0xC0}, /* xor rax,rax */
{0xC3} /* ret */
};
And here's the output of both my test.elf & readelf when I run it on test.elf:
Code:
./test.elf
Segmentation fault
readelf -all test.elf
ELF Header:
Magic: 7f 45 4c 46 02 01 01 ff 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: <unknown: ff>
ABI Version: 0
Type: EXEC (Executable file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x2d0
Start of program headers: 64 (bytes into file)
Start of section headers: 232 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 3
Size of section headers: 64 (bytes)
Number of section headers: 4
Section header string table index: 1
readelf: Warning: Section 2 has an out of range sh_info value of 7
Section Headers:
[Nr] Name Type Address Offset
Size EntSize Flags Link Info Align
[ 0] NULL 0000000000000000 00000000
0000000000000000 0000000000000000 0 0 0
[ 1] .shstrtab STRTAB 00000000000001e8 000001e8
0000000000000040 0000000000000001 AS 0 0 1
[ 2] .symtab SYMTAB 0000000000000228 00000228
00000000000000a8 0000000000000018 WAI 1 7 8
[ 3] .text PROGBITS 00000000000002d0 000002d0
000000000000001a 0000000000000001 WAXlp 0 0 1
Key to Flags:
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
l (large), p (processor specific)
There are no section groups in this file.
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
NULL 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 0x0
PHDR 0x0000000000000000 0x0000000000000040 0x0000000000000040
0x00000000000000a8 0x00000000000000a8 R 0x0
LOAD 0x0000000000000000 0x00000000000002d0 0x00000000000002d0
0x000000000000001a 0x000000000000001a RWE 0x0
Section to Segment mapping:
Segment Sections...
00
01
02
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 7 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000000001e8 64 SECTION LOCAL DEFAULT 1 .shstrtab
2: 0000000000000228 168 SECTION LOCAL DEFAULT 2 .symtab
3: 00000000000002d0 26 SECTION LOCAL DEFAULT 3 .text
4: 00000000000002d0 26 FILE LOCAL DEFAULT ABS mitsy.c
5: 00000000000002d0 22 NOTYPE GLOBAL DEFAULT 3 _start
6: 00000000000002e6 4 FUNC GLOBAL DEFAULT 3 main
Gotta go to work now so won't be able to try any suggestions until late tonight, might respond though if I have something to say