Article 86417 of comp.os.vms:
Xref: mv comp.os.vms:86417
Path: mv!news.sprintlink.net!news.clark.net!rahul.net!a2i!bug.rahul.net!a2i!olivea!spool.mu.edu!howland.reston.ans.net!swrinde!elroy.jpl.nasa.gov!lll-winken.llnl.gov!fnnews.fnal.gov!nntp-server.caltech.edu!SOL1.GPS.CALTECH.EDU!CARL
From: carl@SOL1.GPS.CALTECH.EDU (Carl J Lydick)
Newsgroups: comp.os.vms
Subject: Re: Generating machine code on the fly and running it
Date: 10 Oct 1995 23:03:07 GMT
Organization: HST Wide Field/Planetary Camera
Lines: 59
Distribution: world
Message-ID: <45eu3b$kgb@gap.cco.caltech.edu>
References: <DG6pxr.MJJ@advent.co.uk>
Reply-To: carl@SOL1.GPS.CALTECH.EDU
NNTP-Posting-Host: sol1.gps.caltech.edu

In article <DG6pxr.MJJ@advent.co.uk>, dave@advent.co.uk (David Boyce) writes:
=We're porting an imaging application from Unix to OpenVMS on Alpha.
=In the Unix version, we generate some machine code on the fly, put
=it into a suitably mmaped area of memory, and execute it. Is it
=possible to do this on VMS? I've been beating my head against the
=bookreader documentation for days now. I've tried SYS$EXPREG, which
=gave me memory I could write into but not execute. I've also tried
=to use MACRO to create an executable, writable PSECT but that gave me the
=same result.

In a previous response, I supplied an unnecessarily complicated technique to
achieve this.  I've been reminded in another thread that LIB$FIND_IMAGE_SYMBOL
accepts an optional fourth argument which makes defining a logical name to
point to an image that doesn't reside in SYS$SHARE or doesn't have the file
type .EXE unnecessary.  Here's a slightly simpler program that uses the fourth
argument to LIB$FIND_IMAGE_SYMBOL.  As before, if you're using DEC C instead of
VAX C, you don't need the "SYS$SHARE:VAXCRTL/SHARE" line in the options file.

#include stdio
#include descrip
#define ckstat(x) if (((stat = x) & 7) != 1) SYS$EXIT(stat)

main()
{	$DESCRIPTOR(imgnam, "TEST");
	$DESCRIPTOR(defspec, "SYS$DISK:[].EXE");
	$DESCRIPTOR(symbol, "test");
	$DESCRIPTOR(cmd, "@TEST.COM");
	void (*symval)();
	FILE *fp;
	unsigned long stat, LIB$FIND_IMAGE_SYMBOL(), SYS$EXIT(), LIB$SPAWN();
	int strlen();

	puts("Creating DCL procedure to create shareable image.");
	if((fp = fopen("TEST.COM", "w")) == 0)
	{	puts("Couldn't create DCL procedure.");
		SYS$EXIT(1);
	}
	fprintf(fp, "$ CC/OBJECT=TEST SYS$INPUT: !'F$VERIFY(1)'\n");
	fprintf(fp, "test() { void puts(); puts(\"Hello, world!\"); }\n");
	fprintf(fp, "$ LINK/SHARE TEST,SYS$INPUT:/OPTION\n");
	fprintf(fp, "SYS$SHARE:VAXCRTL/SHARE\n");
	fprintf(fp, "UNIVERSAL=test");
	fclose(fp);
	puts("Creating shareable image.");
	ckstat(LIB$SPAWN(&cmd));
	puts("Mapping image.");
	ckstat(LIB$FIND_IMAGE_SYMBOL(&imgnam, &symbol, &symval, &defspec));
	puts("Branching to entry point.");
	(*symval)();
	puts("Exiting from main program.");
}
--------------------------------------------------------------------------------
Carl J Lydick | INTERnet: CARL@SOL1.GPS.CALTECH.EDU | NSI/HEPnet: SOL1::CARL

Disclaimer:  Hey, I understand VAXen and VMS.  That's what I get paid for.  My
understanding of astronomy is purely at the amateur level (or below).  So
unless what I'm saying is directly related to VAX/VMS, don't hold me or my
organization responsible for it.  If it IS related to VAX/VMS, you can try to
hold me responsible for it, but my organization had nothing to do with it.


