Dynamic Downloading and executing of ELF files

Dear Group,

I want to prepare an ELF file which can be downloaded dynamically to any address in the moemory and executes as a new task/thread/process.

1) for this what are all the compileation, linker options while building the ELF file?
2) which parts of ELF file has to modified while loading?(what setions or offset)
3) please suggest me how the Dynamic download loader should be to achieve this.

Thanks,
Ravinder

You can't just download an ELF file into memory and execute it because an ELF file needs processing to load in the first place. An ELF file has potentially many separate memory segments with different characteristics, not one glob that it just lives in.

Build a shared library, use dlopen, that's what they're there for. Not to mention that how libraries are handled can depend a lot on your architecture and configuration, doing that manually yourself would be very unportable(not to mention reinventing the wheel.)

It handles everything for you, from filename to symbol name in two easy steps. Here's an example:

// shared.c
// compile like:  gcc shared.c -shared -fPIC -o shared.so
#include <stdio.h>

int library_function(int argc, char *argv[])
{
        fprintf(stderr, "library_function called\n");
        return(0);
}
// main.c
// compile like:  gcc main.c -ldl -o main
#include <stdio.h>
#include <dlfcn.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
        void *h=dlopen("./shared.so", RTLD_NOW);
        int (*fn)(int argc, char *argv[]);
        pid_t pid;

        if(h == NULL)
        {
                fprintf(stderr, "Couldn't open shared.so\n");
                return(1);
        }

        fn=dlsym(h, "library_function");
        if(fn == NULL)
        {
                fprintf(stderr, "Couldn't find library_function\n");
        }

        pid=fork();  // Create new process
        if(pid == 0)    // child code
        {       return(fn(argc, argv));         }
        else // Wait for child process
        {
                int status;
                wait(&status);
                fprintf(stderr, "Child returned %d\n", WEXITSTATUS(status));
        }

        dlclose(h);

        return(0);
}