What happens before main() is executed in C and why is it important?
11–20 of 104 posts
Re: What happens before main() is executed in C and why is it important?
#12The linker script is what runs the global constructors in C++. Nothing preventing using a custom linker script in C to call some `premain()` function.
Re: What happens before main() is executed in C and why is it important?
#13This seems to be concentrating on what the system loader is doing without mentioning whatever the statically linked standard library gets up to before calling the main function?
I would add that in some (most?) environments, the code that gets executed before main() is easily accessible: it sits in files within the project (written in assembly most of the time).
Re: What happens before main() is executed in C and why is it important?
#14Re: What happens before main() is executed in C and why is it important?
#15In embedded applications a lot of things happen before you can even attach a debugger. Most microcontrollers go through a non programmable hardware initialisation sequence which initialises enough hardware for the processor to run. The processor then runs from a section of read only (or at least write protected) memory. This code performs a further stage of initialisation and patching. This can be used by the manufac…
Is this even public for us as developers, can we influence it and it influence us? If not, should we even think about it? I had a lot of curiosity about how JTAG works and how I can build my own debugger for general targets, but is seems documentation for this is thin. I should look over openocd to see how those guys are doing stuff.
[1] https://community.cypress.com/community/psoc-6/blog/2017/05/...
Re: What happens before main() is executed in C and why is it important?
#16There have historically been some big security holes when parsing XML that it is a security code smell now if you are working with it (especially in lower level languages like C or C++).
Re: What happens before main() is executed in C and why is it important?
#17This seems to be concentrating on what the system loader is doing without mentioning whatever the statically linked standard library gets up to before calling the main function?
On microcontrollers, you often don't have a standard library, or if you do, it is limited, and doesn't do things before main. The article describes pretty well what happens on a microcontroller. I would add that in some (most?) environments, the code that gets executed before main() is easily accessible: it sits in files within the project (written in assembly most of the time).
Re: What happens before main() is executed in C and why is it important?
#18In embedded applications a lot of things happen before you can even attach a debugger. Most microcontrollers go through a non programmable hardware initialisation sequence which initialises enough hardware for the processor to run. The processor then runs from a section of read only (or at least write protected) memory. This code performs a further stage of initialisation and patching. This can be used by the manufac…
Sometimes strange bugs manifest due to misalignments between configs at early boot stages and what the later code needs, so you want to be able to review and change those stages.
Re: What happens before main() is executed in C and why is it important?
#19It should be noted that relying on static initialization as suggested in the article, while at times useful, can open a new set of problems until initialization order is well-defined (it's out of scope for the article, so I imagine the author is aware and just opting for brevity). Some compilers provide an explicit ordering mechanism (clang and GCC) and the there are proposals in the C++ standards at the moment, but…
I had a really good experience designing an RTOS initialization system that both depended on static initialization, but didn't have issues with ordering either (and in fact didn't even have a main, post init just went right into the idle thread). Basically I only let children of one class be statically initialized (and enforced this with a tool that goes through the generated binary and type information). These 'subs…
embedded systems software in one phrase. :-D
Re: What happens before main() is executed in C and why is it important?
#20Personally, I think global's with constructors are kind of an anti pattern. I've noticed a high correlation between crappy app's and app's with a lot of pre main() code. Especially if some of your global's have threads: because then you potentially have threads running AFTER main exits... I've seen a number of apps with random crashes at shutdown because a thread that was started in a global was still running and sin…