234 ms·
Here's what works on my Mac, compiled with clang -m32. #include <stdio.h> int main() { int i; // Another stack variabl
by saagarjha 9y ago
Here's what works on my Mac, compiled with clang -m32.
#include <stdio.h>
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.
- kbumsik 9y agoI 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?
- saagarjha 9y agoThey're pushed on to the stack if another function will touch them.
- doomjunky 9y agoOn Windows this dependes on the calling convention. The calling convention determines whether function arguments are passed: In order or in reverse order. Who takes care of cleaning the stack: caller or callee? And even if the arguments are passed by stack or CPU registers. In Visual Studio you can control this behavoir with the __cdecl, __clrcall, __stdcall, __fastcall, __thiscall, __vectorcall [1] calling convention function prefixes, but this also depends on the optimization options [2] of the compiler. Passing arguments via CPU registers has a huge preformance benefit, but also some drawbacks. [1]: https://msdn.microsoft.com/en-us/library/984x0h58.aspx https://msdn.microsoft.com/en-us/library/984x0h58.aspx [2]: https://msdn.microsoft.com/en-us/library/46t77ak2.aspx https://msdn.microsoft.com/en-us/library/46t77ak2.aspx