Stack initialization, making sure that the stack is properly aligned per the ABI requirements
Frame pointer initialization
Initialization of the C/C++ runtime Relocate any relocatable sections (if not handled by the loader or linker)
Initializing global and static memory
Runtime initializes a subset of uninitialized memory (no = in the declaration) to 0. This includes global and static variables, but not stack variables. All uninitialized data that needs to be set to 0 is placed into the .bss section of the compiled program image by the linker. The location of the .bss section is identified during initialization, and the memory is typically set to 0 with memset.
C++ global objects must be constructed before calling main. The linker places these constructors into the .init, .init_array, or .ctors section of the image. Some compilers also allow C and C++ functions to be marked as a constructor using a compiler attribute (e.g., __attribute__((constuctor))). The constructors are stored in a list by the linker. The runtime initialization process iterates through the list and calls each constructor.
Prepare the argc and argv variables for invoking main (even if it’s just setting these to 0/NULL)
Perform any additional setup steps required by the C/C++ standard library implementation. These additional runtime initialization steps are run for many programs (but not all):
Heap initialization
Initialize stdio (i.e., stdin, stdout, stderr)
Initialize exception support (if using C++)
Register destructors and other cleanup functions that will run when exiting the program (using atexit and __cxa_atexit)
Assembly files commonly found during this portion of the startup process are crtbegin.s, crtend.s, crti.s, and crtn.s.
Prepare environment variables
Initialization of other scaffolding required by the system Program scaffolding setup before main might include:
Threading support and thread local storage
Buffer overrun detection
Stack logging
Run-time error checks
Locale settings
Math error handling
Default math library precision
Jumping to main
Exiting the program with the return code from main
So, how do we get to the _start?
Baremetal: Reset Vector
Bootloader Launches Application
OS Calls an exec function Loaders will often perform the following actions:
Check permissions
Allocate space for the program’s stack
Allocate space for the program’s heap
Initialize registers (e.g., stack pointer)
Push argc, argv, and envp onto the program stack
Map virtual address spaces
Dynamic linking
Relocations
Call pre-initialization functions
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.