I have a simple c file that prints a message in main and do nothing else Compiled it using gcc to generate .out file.Use the elf parser to get the entry_address of start function and using this addressas function pointer i am trying to execute the main function in c file but it is giving seg fault.
e.g
test.c
void main()
{
print("something");
}
generated test.out elf file by gcc
and i get the following by doing nm test.out
0000000000601020 A _edata
0000000000601030 A _end
00000000004005e8 T _fini
00000000004003c8 T _init
0000000000400410 T _start
000000000040043c t call_gmon_start
0000000000601020 b completed.6531
0000000000601010 W data_start
0000000000601028 b dtor_idx.6533
00000000004004d0 t frame_dummy
00000000004004f4 T main
The start address is 0x0400410 T _start
.
Now i write another c code like below to execute the main function in test.c
execute.c
void main()
{
typedef int func(void);
f = (func*)0x00400410;
f();
}
compiling excute.c by gcc execute.c -o execute.out
is giving me segmentation fault on calling f()
.
The desired output is to print something
.
Is it possible to execute elf file function from address,where i am getting wrong.