To me one curious thing about the main() function without arguments is, these arguments are still pushed onto the stack. You can find them by dereferencing a pointer of another stack variable and playing around with the offset. // Just tested with mingw-gcc on Win8.1 // Maybe you need to play around with the offset, +28 int main() { int i; // Another stack variable printf("%d\n", (long long)&i + 28); // &argv printf(…
What happens before main() is executed in C and why is it important?
71–80 of 104 posts
Re: What happens before main() is executed in C and why is it important?
#72Re: What happens before main() is executed in C and why is it important?
#73Is it strange that C++ is able to do this but C itself cannot? Sometimes I hear the argument that "C is lower level" and their is the idiom that "C++ is C but better" (not that I believe these are true)
C and C++ have different syntax for what that code is, but it's pure semantics as far as the computer is concerned and C++ gives you zero extra power.
Re: What happens before main() is executed in C and why is it important?
#74Re: What happens before main() is executed in C and why is it important?
#75Is it strange that C++ is able to do this but C itself cannot? Sometimes I hear the argument that "C is lower level" and their is the idiom that "C++ is C but better" (not that I believe these are true)
Besides that, I can't see much difference between doing something before main() and just putting some code on the very top of main().
Re: What happens before main() is executed in C and why is it important?
#76To me one curious thing about the main() function without arguments is, these arguments are still pushed onto the stack. You can find them by dereferencing a pointer of another stack variable and playing around with the offset. // Just tested with mingw-gcc on Win8.1 // Maybe you need to play around with the offset, +28 int main() { int i; // Another stack variable printf("%d\n", (long long)&i + 28); // &argv printf(…
Here's what works on my Mac, compiled with clang -m32. #include int main() { int i; // Another stack variable printf("%s\n", **(char ***)(&i + 8)); // argv[0] return 0; } Of course, this won't work at all on x86_64 because arguments are passed on registers instead of the stack.
Re: What happens before main() is executed in C and why is it important?
#77I attached an example of Arduino Zero varients [1] (it is directly from Atmel's SDK.) It is quite straightforward to see what happends before main(). In embedded system, you can even change it easily if you want.
In this example, the stack pointer is updated at very first point after powering up the device [2]. And the undersocre-prefixed variables are defined in its linker script. [3]
[1]: https://github.com/arduino/ArduinoCore-samd/blob/master/boot...
[2]: https://github.com/arduino/ArduinoCore-samd/blob/master/boot...
[3]: https://github.com/arduino/ArduinoCore-samd/blob/master/boot...
Re: What happens before main() is executed in C and why is it important?
#78Earlier quoted context omitted.
Here's what works on my Mac, compiled with clang -m32. #include int main() { int i; // Another stack variable printf("%s\n", **(char ***)(&i + 8)); // argv[0] return 0; } Of course, this won't work at all on x86_64 because arguments are passed on registers instead of the stack.
I don't know much about AMD64 but if the arguments are passed to registers then how it is possible to retrieve argc/argv later, as other functions likely to touch registers?
Re: What happens before main() is executed in C and why is it important?
#79Earlier quoted context omitted.
If it's not, you can drop the cost. There are AVR like 8051s that you can get for 10 cents.
I can't find any 8051 less than 30 cents and I digikey sells attinys for 17 cents.
I've been working on an agricultural spray controller that uses some silab part only because there is already assembly code written for it. The company that sells the controller doesn't want to spend the time rewriting it in C and moving to arm or pic.
Re: What happens before main() is executed in C and why is it important?
#80To me one curious thing about the main() function without arguments is, these arguments are still pushed onto the stack. You can find them by dereferencing a pointer of another stack variable and playing around with the offset. // Just tested with mingw-gcc on Win8.1 // Maybe you need to play around with the offset, +28 int main() { int i; // Another stack variable printf("%d\n", (long long)&i + 28); // &argv printf(…
The handful of times I've written startup code for an embedded micro, I've always passed zero arguments to the program's "command line", not even its own program name.
Ie, from the perspective of _start, declare the application's entry point as int main(int argc, char argv), call it with main(0, NULL), and ignore the return value.
ARM passes the first several arguments in registers anyway, so your UB trick wouldn't reveal anything interesting.