Self-modifying code using GCC (2011)
nikmav.blogspot.com
nikmav.blogspot.com
Right. It stopped being a useful optimization decades ago.
One of the few architectural mistakes von Neumann made back in 1945 is that he didn't think of index registers. He was addressing arrays by storing into the address part of instructions. By 1949, the index register (the "B Box") had been invented, and storing into instructions was mostly obsolete.
Turns out going from calculating an algorithm, optimized for whatever specialization is needed for performance, functionality & formats; to object code; to execution, with the simplest syntax imaginable, is pretty useful.
And fun
Cough, Forth, cough!
- hot patching
- making sure sysadmins have healthy sense of panic available to them at all times
edit: There might be room for some other extension here, but as an other commenter already wrote:
1. functions might not be contiguous in memory
2. the code that you are moving should better be position independent
So any such extension should handle these issues one way or an other.
That being said, in a loop you'd only take this hit on the first run. You'd be back to cache misses after that.
I think obfuscation is generally a really bad idea, but that was brought up in the article. One practical application for self-modifying code is hotpatching. Sometimes you really don't want to stop the process but want to change the implementation for some function.
Some systems apparently allow you to add executable without removing write as that is what the OP does with "mprotect(page, getpagesize(), PROT_READ | PROT_WRITE | PROT_EXEC);". However,
I never got into this but "GNU lightning is a free-software library for generating assembly language code at run-time." https://en.wikipedia.org/wiki/GNU_lightning
https://onesphere.hashnode.dev/upstart
And for some reason: https://news.ycombinator.com/item?id=36976878