Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* Dynamic loading on OSF
@ 1996-11-26  5:00 T. Cornilleau
  1996-12-03  5:00 ` Craig Neth USG
  0 siblings, 1 reply; 2+ messages in thread
From: T. Cornilleau @ 1996-11-26  5:00 UTC (permalink / 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);
}

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Dynamic loading on OSF
  1996-11-26  5:00 Dynamic loading on OSF T. Cornilleau
@ 1996-12-03  5:00 ` Craig Neth USG
  0 siblings, 0 replies; 2+ messages in thread
From: Craig Neth USG @ 1996-12-03  5:00 UTC (permalink / raw)
  Cc: T. Cornilleau


T. Cornilleau wrote:
> 
> 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.
[... programs deleted ...]

Your fundamental problem is that the linker will round the -T value that
you pass it to one that is aligned on a 64K boundary.   So you are
loading
the program at a non-rounded address but the linker is relocating it to
a rounded address, with the result being you're jumping to someplace
other
than where the code is...

You need to round the address you pass to your relink function, making
sure
you sbrk() the difference between the current break and however much it
takes
to round the address to a multiple of 65536.   Also, you have a bunch of
int/long mismatches in your example - addresses are longs in DIGITAL
UNIX.
In particular, you need to fix the sprintf that builds the 'ld' command
line - you have a %x for the address field and it needs to be a %lx.

I hacked up your program with these changes and got it working.

One last thing - I think to be absolutely kosher you need to issue a 
'imb' PAL call to flush the instruction cache before jumping to the
routine, 
as the Alpha processor has separate instruction and data caches and you
are putting instructions into data space.   Here's a sequence you can
use to define
a macro, IMB(), that does what you need:

#include <c_asm.h>
#include <machine/pal.h>

#define QUOTE(s) #s
#define STR(s) QUOTE(s)
#define IMB() asm("call_pal " STR(PAL_imb))


-- 
--------------------------------------------------------------------------------
Craig Neth                         ! neth@zk3.dec.com
DIGITAL Equipment Corporation      ! 
110 Spit Brook Road ZKO3-3/W17     !
Nashua, NH 03062                   ! Opinions expressed herein are my
own.
================================================================================



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1996-12-03  5:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1996-11-26  5:00 Dynamic loading on OSF T. Cornilleau
1996-12-03  5:00 ` Craig Neth USG

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