Extracting native code from C

Richard A. O'Keefe ok at cs.otago.ac.nz
Wed Mar 13 23:45:03 UTC 2002


Anthony Hannan <ajh18 at cornell.edu> wrote:
	Below is a small C program that copies some of its machine code to
	dynamic memory and executes it.

The code happens to work on *86 machines.  It will NOT work on most of
the machines that GCC supports.  Issues include
 - alignment; *86 code is NOT made of ints, it is made of bytes, but
   the *86s have relatively cheap misaligned loads and stores.  Some
   machines have code made of bytes or shorts, but no or expensive
   misaligned int loads and stores.

 - protection; some operating systems (including some versions of UNIX)
   never allow code to be executed from a writable area

 - cache; modifying an area and then trying to execute can confuse the
   heck out of split I/D caches on some machines; *86 machines allow it
   (with a fine point which you will discover in the Pentium manual);
   SPARCs allow it *IF* you manually flush the cache, which you can't
   do from portable C; other machines could be like either.

There are a number of packages around for dynamically generating code,
notably tick-C and Vcode.  But it isn't this easy, not by a long way.



More information about the Squeak-dev mailing list