Re:Main is usually a function, so when is it not? (2015)
by dhosek on Monday June 14, 2021 @03:03PM
(#27506777)
I don't know that that would work since if the code is generated at runtime it would live in .data and not .text. At least for the architecture being targeted, you aren't allowed to create executable code at runtime like that (note that the original poster had to declare his main array as const to be able to have it in the .text segment.
Re:Main is usually a function, so when is it not? (2015)
by Sharlin on Monday June 14, 2021 @05:53PM
(#27508785)
Coercing a data pointer into a function pointer is undefined behavior in standard C (they don't even need to be the same size), but at least on POSIX platforms the compiler must do the right thing because `dlsym` depends on it working. Generating and executing native code at runtime is not that special, mind; after all JIT compilers are ubiquitous these days!
Re:Main is usually a function, so when is it not? (2015)
by twoodfin on Monday June 14, 2021 @06:11PM
(#27508983)
Popularity of the no-execute NX bit significantly postdates Unix. As I recall, Microsoft only started flipping it on by default for the 64-bit Windows NT kernel, since so many preexisting 32-bit applications relied on self-modifying code.
Re:Main is usually a function, so when is it not? (2015)
by 1vuio0pswjnm7 on Monday June 14, 2021 @11:33PM
(#27510982)
Request: URL pointing to the code for this regex engine.
So there's the utility, if you're hardcore enough to build machine code at runtime.
If you wanted to abuse main() particularly, I guess you've got argc and argv in registers, and your hand-compiled main 'function' could maybe have some self-modifying code?