
There shouldn't be any 'gotchas' as long as you make sure that the code
you write (by whatever means) properly does it's thing, preserves the
proper registers, and returns with a level stack.


Here's another example:

#include <stdio.h>

int foo(void);

asm (".text\n"\
     "\t.align 4\n"\
     "\t.global main\n"\
     "\tmain:\n"\
     "\tjmp foo\n");

int foo()
{
    puts("Hello World!");
    fflush(stdout);
    return 0;
}


Let's say that foo() was some data allocated from malloc(). You opened
a file and dumped a binary image of what you wanted executed into that
array. After that, you freed the data and did the same thing with
some more 'data' that is really code. You could execute any 'program'
without ever doing a fork() or execXX().


You can make the binary image of the code you want executed in a
simple 'C' program by doing:


code_i_want_executed()
{

   return whatever;
   label:;
}


Writer_code()
{
    int len;
    len = (char *) &&label - (char *) code_i_want_executed;
                   ^^__ gnu extension
    fd = open(...);
    write(fd, code_i_want_executed, len);
    close(fd);
}


This won't work if you are accessing data outside the program unit
itself because the relocation will be wrong. However, if you wanted
to make a bunch of filters or convolution kernels that are selectable,
they don't all have to be in memory at the same time and/or they don't
have to be a bunch of executables that are relocated by the loader.

Cheers,
    DJ
Richard B. Johnson
Project Engineer
Analogic Corporation
Penguin : Linux version 2.2.6 on an i586 machine (66.36 BogoMips).
Warning : It's hard to stay on the trailing edge of technology.
 MS-DOS : Program Loader
   Unix : Angry Operating System
VAX/VMS : Obsolete Operating System
  Linux : Engineering tool
Windows : Typewriter


