Thanks to the help in this question, the loader can now map a statically compiled hello world into memory and jump somewhere in that memory region. The problem I\'m facing n
The problem I'm facing now is I seem not to jump to the right address or I'm calling the function in the wrong way
Your problem is neither of the above (although "calling the wrong way" is not too far).
For a statically-linked executable, Elf64_Ehdr.e_entry
is the correct address to call (it points to _start
), and _start
takes no arguments.
The problem is that it is the job of _start
to
argc
, argv
and envp
, and finallymain(argc, argv, envp)
.The question then is: how can _start
accomplish step 2?
The answer: there is a protocol that Linux kernel implements and _start
uses to accomplish step 2.
In particular, the kernel copies the actual (string) values of argv[0]
, argv[1]
, ... envp[0]
, envp[1]
, etc. to the stack, then the pointers to these strings. There is also something called auxilliary vector.
The _start
expects to find all of this info on the stack, and will misbehave when it doesn't find it. I believe that is the root cause of your current problem.
Here is an article which explains expected setup with references to Linux kernel source code. Another article.