Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: "T. Cornilleau" <Thierry.Cornilleau@cnam.fr>
Subject: Dynamic loading on OSF
Date: 1996/11/26	[thread overview]
Message-ID: <329B9055.7945@cnam.fr> (raw)

[-- Attachment #1: Type: text/plain, Size: 346 bytes --]

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

[-- Attachment #2: MAIL --]
[-- Type: text/plain, Size: 5849 bytes --]


/*
  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 <stdio.h>
#include <fcntl.h>
#include <a.out.h>

/*
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 <stdio.h>
#include <fcntl.h>
#include <a.out.h>

/* 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);
}

             reply	other threads:[~1996-11-26  5:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-26  5:00 T. Cornilleau [this message]
1996-12-03  5:00 ` Dynamic loading on OSF Craig Neth USG
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox