5

For example, I have simple C program that only have main function that just returns 0. What registers should loader (Linux exec loader, I guess) install before start a program? I didn't find information about this except stack pointer register.

4
  • 1
    Why do you need to know that? Possibly your problem can be solved in a different way? C standard does not discuss registers, and any answer given here would be necessarily platform-specific, and may vary from one version of the platform to the next.
    – user285148
    Commented Jan 24, 2018 at 10:11
  • Are you asking what an OS has to do to prepare the environment for a newly launched process (this does include quite a bit more than just setting a few registers)?
    – Mael
    Commented Jan 24, 2018 at 10:18
  • @user8734617 It's just satisfying my interest, there is no specific task.
    – YourJest
    Commented Jan 24, 2018 at 13:49
  • @Mael In global - yes. I understand that it is more than setting some registers and it really depends on platform. I just want to know about main registers which set is necessary to start a program. Maybe the question is more about how exec create new process.
    – YourJest
    Commented Jan 24, 2018 at 14:01

1 Answer 1

5

This functionality you ascribe to the loader is usually called crt0 or crt0.o. It usually has an entry point called __start, that the operating system will transfer initial control to.

It is part of the basic C library code and it takes control of the new process, ultimately to invoke your main. Depending on the environment it runs in it might have to actually open stdin & stdout, and collect the command line parameters into an argv array to pass to your main. Initially as you suspect it may establish the stack pointer register, however, that may be done already by exec, depending of course. It may also establish a (global) data pointer again depending on the architecture (if it uses one) and the operating system (whether it has already done this or not).

Since the C main is a 100% normal C function, the arguments will be passed to it using the same calling convention and register usage as most any other function with the same signature. The calling convention varies across different compilers and operating systems, so a good bet is to decompile your C main and see how it accesses it's parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.