I was wondering if it'd be possible to write self modifying code in C? I guess if you knew where the program is in memory it shouldn't be too hard to do, but outside of that... I was wondering if maybe anyone here knew of any good resources on this topic. Self modifying code in general, really. I'm not looking for a solution for meta-programming, like those provided in most modern day languages. It's a learning exercise more than anything.
edit: I am aware that modern OS tend to protect running processes from just this thing. I'll install an ancient build of linux on an emulator if I have to :P
Well, do you know asm? Then all you need is http://download.intel.com/products/processor/manual/325383.pdf
I'm not sure what you could possibly want to achieve though...
Are you, instead, looking for advice on how to write your code so that it is easy to modify?
After searching round a bit, the best solution I have come up with is building a small VM, and have it parse a string that you can then mutate at run time.
I'm rusty but I used to do a lot of work with z80 asm with my graphic calculator and SOME arm asm when I found my old Ipaq pda a few years ago.
The exercise I've set up for my self is to write a program that rather than using a for-loop, modifies it's self so that it prints 1 through 10.
Ultimately I'm trying to get a better grasp of what my software looks like under the hood (so to speak). My next step in this project will be make a function return to a different point on the execution stack than it was called from. Though, I think I might need to do that to achieve the self modifying program I described above anyways. I dunno, this is all pretty new to me.
This used to be done a lot a long time ago, but sadly it's pretty shut down anymore. Ended up turning into a security risk.
Hence most modern OS do their darnedest to prevent this from happening.
Part of the reason I want to do this is that I've found that the comp sci curriculum at ever liberal arts college I've looked into are dime-a-dozen web dev farms at best and over glorified Tech support programs at worst. As I cant afford/no longer have the grades to get into a good engineering/science school I'm learning a lot of things on my own. Not that I mind doing so, I'm starting to think I'll drop out and continue to be self taught before all is said and done. I've heard from a number of advisors that it is not uncommon for self taught applicants to be on par with MIT/Cal Tech/Berkly/etc grad applicants when selecting a new employee.
This used to be done a lot a long time ago, but sadly it's pretty shut down anymore. Ended up turning into a security risk.
Hence most modern OS do their darnedest to prevent this from happening.
Nope, you are wrong. They only do their darndest to prevent this from happening *unintentionally*. But nothing is shut down. See VirtualProtect in Windows or mprotect in Linux.