From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,6a0170ee33c96180,start X-Google-Attributes: gidfee84,public From: "T. Cornilleau" Subject: Dynamic loading on OSF Date: 1996/11/26 Message-ID: <329B9055.7945@cnam.fr> X-Deja-AN: 200858890 organization: Laboratoire Cedric - CNAM content-type: multipart/mixed; boundary="------------27F33BF07052" mime-version: 1.0 newsgroups: comp.unix.osf.osf1 x-mailer: Mozilla 3.0Gold (Win16; I) This is a multi-part message in MIME format. --------------27F33BF07052 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hello, I'm trying to dynamically load, in a running process, a C function stored in a file. To do that, I have rewrittent a C program which works fin on a SUN OS 4.1.3. But on a DEC OSF 3.2, i have a segmentation fault. Both OS use COFF as object file format. All my programs are attached. May you help me ? Thanks in advance. T. Cornilleau --------------27F33BF07052 Content-Type: text/plain; charset=us-ascii; name="MAIL" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="MAIL" /* function to load compilation : cc -c addition.c */ int addition(int a, int b) { return a + b; } -------------------------------------------------------------------------- /* Version for SUN OS 4.1.3 compilation : gcc -static -o load load.c */ #include #include #include /* char *sbrk(int) ; */ /* Make a loadable program - call a ld(1) */ relink( char *symbols, /* file where is the symbols table */ char *file, /* file to build */ char *function, /* entry point symbol */ void *zero) /* loading address in the process */ { char command[256]; sprintf(command,"ld -T %x -A %s %s.o -e _%s -o %s \n", (long)zero, symbols, file, function, file); printf("%s",command); system(command); } /* Dynamic load of a function 'function' stored in the file 'function.o'; 'loader' returns the memory address where the function is loaded. */ int (* loader(char *executable, char *function))() { void *zero = (void *)sbrk(0); int fd; int size; struct exec aouthdr; relink(executable, function, function, zero); if ((fd = open(function, O_RDONLY)) == -1 || read(fd, &aouthdr, sizeof aouthdr) < sizeof aouthdr) { fprintf(stderr, "Impossible to open %s\n", function); return 0; } size = N_SYMOFF(aouthdr) - N_TXTOFF(aouthdr); printf("Function size : %d\n", size); /* Data segment extension */ if( sbrk(aouthdr.a_text+aouthdr.a_data+aouthdr.a_bss) == -1) { fprintf(stderr, "Program %s too big\n", function); return 0; } /* Executable load */ lseek(fd, (long) N_TXTOFF(aouthdr), 0); read(fd, zero, size); close(fd) ; return (int (*) ()) aouthdr.a_entry; } char *argv0; void usage(); main(int argc, char **argv) { int (*operation)(int, int); argv0 = argv[0]; if(argc < 4) usage(); operation = loader(argv[0], argv[1]); printf("Called function address : %x\n", operation); printf("%s of %s and %s = ", argv[1], argv[2], argv[3]); printf("%d\n", (* operation)(atoi(argv[2]),atoi(argv[3])) ); } void usage() { fprintf(stderr, "Usage: %s operation n1 n2\n", argv0); exit(1); } ---------------------------------------------------------------------------- /* Adaptation to OSF 3.2 compilation : cc -non_shared -o load load.c SUN a.out header is now divided in two headers FILEHDR and AOUTHDR */ #include #include #include /* Make a loadable program - call a ld(1) */ relink( char *symbols, /* file where is the symbols table */ char *file, /* file to build */ char *function, /* entry point symbol */ void *zero) /* loading address in the process */ { char command[256]; sprintf(command,"ld -A %s %s.o -e %s -T %x -o %s \n", symbols, file, function, (long)zero, file); printf("%s",command); system(command); } void ShowFileHeader(FILHDR *hdr) { printf("FileHeader\n"); printf("Magic number :\t\t\t%d\n",hdr->f_magic); printf("Number of sections :\t\t%d\n", hdr->f_nscns); printf("Time & date stamp:\t\t%d\n", hdr->f_timdat); printf("File pointer to symbolic header:\t%d\n", hdr->f_symptr); printf("sizeof(symbolic hdr):\t\t%d\n", hdr->f_nsyms); printf("sizeof(optional hdr):\t\t%d\n", hdr->f_opthdr); printf("flags:\t\t\t%x\n", hdr->f_flags); } void ShowAOutHeader(AOUTHDR *hdr) { printf("AOutHeader\n"); printf("Magic number :\t\t\t%d\n",hdr->magic); printf("Version stamp :\t\t%d\n", hdr->vstamp); printf("? :\t\t%d\n", hdr->bldrev); printf("? :\t\t\t%d\n", hdr->padcell); printf("text size in bytes :\t\t%d\n", hdr->tsize); printf("initialized data size in bytes :\t%d\n", hdr->dsize); printf("uninitialized data size in bytes :\t%d\n", hdr->bsize); printf("entry :\t\t\t%x\n", hdr->entry); printf("base of text : \t\t\t%x\n",hdr->text_start); printf("base of data : \t\t\t%x\n",hdr->data_start); printf("base of bss : \t\t\t%x\n",hdr->bss_start); printf("general purpose register mask : \t%x\n",hdr->gprmask); printf("floating point register mask : \t%x\n",hdr->fprmask); printf("the gp value used for this object : %x\n",hdr->gp_value); } /* Dynamic load of a function 'function' stored in the file 'function.o'; 'loader' returns the memory address where the function is loaded. */ int (* loader(char *executable, char *function))() { void *zero = (void *)sbrk((ssize_t)0); int size; FILE *file; FILHDR filehdr; AOUTHDR aouthdr; int address; relink(executable, function, function, zero); if ((file = fopen(function, "rb")) == NULL) { fprintf(stderr, "Impossible to open %s\n", function); return 0; } if(fread(&filehdr, sizeof filehdr, 1, file) != 1) { fprintf(stderr, "Impossible to load filehdr\n"); return 0; } if(fread(&aouthdr, sizeof aouthdr, 1, file) != 1) { fprintf(stderr, "Impossible to load aouthdr\n"); return 0; } ShowFileHeader(&filehdr); ShowAOutHeader(&aouthdr); size = filehdr.f_symptr - N_TXTOFF(filehdr, aouthdr); printf("Function size : %d\n", size); /* Data segment extension */ address = sbrk(aouthdr.tsize+aouthdr.dsize+aouthdr.bsize); printf("\nSbrk : %x\n\n", address); if(address == -1) { fprintf(stderr, "Program %s too big\n", function); return 0; } /* Executable load */ fseek(file, (long) N_TXTOFF(filehdr,aouthdr), SEEK_SET); fread(zero, size, 1, file); if (fclose(file) != 0) { fprintf(stderr, "Error when closing %s\n",function); return 0; } return (int (*) ()) aouthdr.entry; } char *argv0; void usage(); main(int argc, char **argv) { int (*operation)(); argv0 = argv[0]; if(argc < 4) usage(); operation = (void *)loader(argv[0], argv[1]); printf("Called function address : %x\n", operation); printf("%s of %s and %s = \n", argv[1], argv[2], argv[3]); printf("%d\n", (* operation)(atoi(argv[2]), atoi(argv[3])) ); } void usage() { fprintf(stderr, "Usage: %s operation n1 n2\n", argv0); exit(1); } --------------27F33BF07052--