Jump to entry point of ELF from loader

后端 未结 1 567
别跟我提以往
别跟我提以往 2021-01-13 17:24

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

相关标签:
1条回答
  • 2021-01-13 18:06

    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

    1. initialize libc, and
    2. to find correct values of argc, argv and envp, and finally
    3. to call main(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.

    0 讨论(0)
提交回复
热议问题