News for hackers. Stuff that matters.
Re:You can't copy code with memcpy by WalterBright on Wednesday December 29, 2021 @11:14PM (#29733532)
In my 1980s version of Empire, all the global variables were kept contiguously in one source file. To save/restore the game, it just took the address of the first one, the address of the last one, and blitted it to a disk file, and blitted it back.

Very fast & easy.

Of course, it broke when COMDATs were introduced.

I did a similar thing with my text editor. The colors were configurable. The usual way was to have a configuration file, which the editor would read upon startup. But floppy disk systems were unbearably slow. So what I did was take the address of the configuration data in the data segment. I'd work backwards to where those bytes were in the EXE file, and patch the EXE file. This worked great!

Until the advent of virus scanners, which broke that. Virus scanners hated self-modifying EXE files.

Re:You can't copy code with memcpy by halt40 on Thursday December 30, 2021 @06:53AM (#29735995)
I love these little "factoid" comments that you hand out from time to time. What would be even better is a book with all the nitty-gritty details of the software you've written, along with all the misadventures in doing so.

You can name it: 'Bright Moments'

I'd be happy to place an advanced order if it leans you in that direction!

Re:You can't copy code with memcpy by philbo on Thursday December 30, 2021 @07:39AM (#29736236)
If we're bikeshedding the book title, surely "Bright Ideas" warrants a mention.
Re:You can't copy code with memcpy by Cullinet on Thursday December 30, 2021 @02:34PM (#29740466)
Something using Bright Lines would be my first thought.
Re:You can't copy code with memcpy by __david__ on Thursday December 30, 2021 @02:21AM (#29734503)
See also: Emacs's old unexec() function. How do you speed up a bunch standard library lisp loading and initialization? Just do it once then write a new exe out with all your state pre-computed. Genius.
Re:You can't copy code with memcpy by weinzierl on Thursday December 30, 2021 @05:17AM (#29735416)
Yup, and early TeX did it too. You could run a memory dump of a previously initialized version to improve start-up times.

For a modern take on similar idea see also this discussion from a while ago:

"Improving startup time in Atom with V8 snapshots"

https://news.ycombinator.com/item?id=14140421

Re:You can't copy code with memcpy by WalterBright on Thursday December 30, 2021 @03:06AM (#29734726)
I stole that idea from the 1970's FORTRAN program ADVENT, the precursor to Zork.

I was amazed that I never ran across another DOS program that used the technique.

Re:You can't copy code with memcpy by barchar on Saturday January 01, 2022 @10:18PM (#29765454)
there's also unfork(2)!

Do you unfork then unexec or unexec then unfork?

Re:You can't copy code with memcpy by withinrafael on Thursday December 30, 2021 @12:51PM (#29739111)
How did it break? COMDAT usage is optional today. (Perhaps it wasn't when first created?)
Re:You can't copy code with memcpy by WalterBright on Thursday December 30, 2021 @01:29PM (#29739659)
COMDATs aren't placed sequentially by the linker. Also, zero initialized data got placed in a separate section.
Re:You can't copy code with memcpy by floor_ on Thursday December 30, 2021 @08:10AM (#29736416)
I've pre-packed game asset data to the end of EXE files using this trick and it seems that newer neural network virus scanners seem to the the only type of scanner to pick it up. So we may even lose the ability to do that soon once these type of scanner become more popular/prevalent.
Re:You can't copy code with memcpy by pvillano on Saturday January 01, 2022 @01:37PM (#29761201)
how would you do this today? could you wrap it in a struct?