Tk

 view release on metacpan or  search on metacpan

pTk/mTk/tclUnix/tclLoadAout.c  view on Meta::CPAN

 * Prototypes for procedures referenced only in this file:
 */

static int FindLibraries _ANSI_ARGS_((Tcl_Interp * interp, Tcl_Obj * pathPtr,
				      Tcl_DString * buf));
static void UnlinkSymbolTable _ANSI_ARGS_((void));

/*
 *----------------------------------------------------------------------
 *
 * TclpDlopen --
 *
 *	Dynamically loads a binary code file into memory and returns
 *	a handle to the new code.
 *
 * Results:
 *	A standard Tcl completion code.  If an error occurs, an error
 *	message is left in the interp's result.
 *
 * Side effects:
 *	New code suddenly appears in memory.
 *
 *
 * Bugs:
 *	This function does not attempt to handle the case where the
 *	BSS segment is not executable.  It will therefore fail on
 *	Encore Multimax, Pyramid 90x, and similar machines.  The
 *	reason is that the mprotect() kernel call, which would
 *	otherwise be employed to mark the newly-loaded text segment
 *	executable, results in a system crash on BSD/386.
 *
 *	In an effort to make it fast, this function eschews the
 *	technique of linking the load module once, reading its header
 *	to determine its size, allocating memory for it, and linking
 *	it again.  Instead, it `shims out' memory allocation by
 *	placing the module TCL_LOADSHIM bytes beyond the break,
 *	and assuming that any malloc() calls required to run the
 *	linker will not advance the break beyond that point.  If
 *	the break is advanced beyonnd that point, the load will
 *	fail with an `inconsistent memory allocation' error.
 *	It perhaps ought to retry the link, but the failure has
 *	not been observed in two years of daily use of this function.
 *----------------------------------------------------------------------
 */

int
TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
    Tcl_Interp *interp;		/* Used for error reporting. */
    Tcl_Obj *pathPtr;		/* Name of the file containing the desired
				 * code (UTF-8). */
    Tcl_LoadHandle *loadHandle;	/* Filled with token for dynamically loaded
				 * file which will be passed back to
				 * (*unloadProcPtr)() to unload the file. */
    Tcl_FSUnloadFileProc **unloadProcPtr;
				/* Filled with address of Tcl_FSUnloadFileProc
				 * function which should be used for
				 * this file. */
{
    char * inputSymbolTable;	/* Name of the file containing the
				 * symbol table from the last link. */
    Tcl_DString linkCommandBuf;	/* Command to do the run-time relocation
				 * of the module.*/
    char * linkCommand;
    char relocatedFileName [L_tmpnam];
				/* Name of the file holding the relocated */
				/* text of the module */
    int relocatedFd;		/* File descriptor of the file holding
				 * relocated text */
    struct exec relocatedHead;	/* Header of the relocated text */
    unsigned long relocatedSize;/* Size of the relocated text */
    char * startAddress;	/* Starting address of the module */
    int status;			/* Status return from Tcl_ calls */
    char * p;

    /* Find the file that contains the symbols for the run-time link. */

    if (SymbolTableFile != NULL) {
	inputSymbolTable = SymbolTableFile;
    } else if (tclExecutableName == NULL) {
	Tcl_SetResult (interp, "can't find the tclsh executable", TCL_STATIC);
	return TCL_ERROR;
    } else {
	inputSymbolTable = tclExecutableName;
    }

    /* Construct the `ld' command that builds the relocated module */

    tmpnam (relocatedFileName);
    Tcl_DStringInit (&linkCommandBuf);
    Tcl_DStringAppend (&linkCommandBuf, "exec ld -o ", -1);
    Tcl_DStringAppend (&linkCommandBuf, relocatedFileName, -1);
#if defined(__mips) || defined(mips)
    Tcl_DStringAppend (&linkCommandBuf, " -G 0 ", -1);
#endif
    Tcl_DStringAppend (&linkCommandBuf, " -u TclLoadDictionary_", -1);
    TclGuessPackageName(Tcl_GetString(pathPtr), &linkCommandBuf);
    Tcl_DStringAppend (&linkCommandBuf, " -A ", -1);
    Tcl_DStringAppend (&linkCommandBuf, inputSymbolTable, -1);
    Tcl_DStringAppend (&linkCommandBuf, " -N -T XXXXXXXX ", -1);
    Tcl_DStringAppend (&linkCommandBuf, Tcl_GetString(pathPtr), -1);
    Tcl_DStringAppend (&linkCommandBuf, " ", -1);

    if (FindLibraries (interp, pathPtr, &linkCommandBuf) != TCL_OK) {
	Tcl_DStringFree (&linkCommandBuf);
	return TCL_ERROR;
    }

    linkCommand = Tcl_DStringValue (&linkCommandBuf);

    /* Determine the starting address, and plug it into the command */

    startAddress = (char *) (((unsigned long) sbrk (0)
			      + TCL_LOADSHIM + TCL_LOADALIGN - 1)
			     & (- TCL_LOADALIGN));
    p = strstr (linkCommand, "-T") + 3;
    sprintf (p, "%08lx", (long) startAddress);
    p [8] = ' ';

    /* Run the linker */

    status = Tcl_Eval (interp, linkCommand);
    Tcl_DStringFree (&linkCommandBuf);
    if (status != 0) {
	return TCL_ERROR;
    }

    /* Open the linker's result file and read the header */

    relocatedFd = open (relocatedFileName, O_RDONLY);
    if (relocatedFd < 0) {
	goto ioError;
    }
    status= read (relocatedFd, (char *) & relocatedHead, sizeof relocatedHead);
    if (status < sizeof relocatedHead) {
	goto ioError;
    }

    /* Check the magic number */

    if (relocatedHead.a_magic != OMAGIC) {
	Tcl_AppendResult (interp, "bad magic number in intermediate file \"",
			  relocatedFileName, "\"", (char *) NULL);
	goto failure;
    }

    /* Make sure that memory allocation is still consistent */

    if ((unsigned long) sbrk (0) > (unsigned long) startAddress) {
	Tcl_SetResult (interp, "can't load, memory allocation is inconsistent.",
		       TCL_STATIC);
	goto failure;
    }

    /* Make sure that the relocated module's size is reasonable */

    relocatedSize = relocatedHead.a_text + relocatedHead.a_data
      + relocatedHead.a_bss;
    if (relocatedSize > TCL_LOADMAX) {
	Tcl_SetResult (interp, "module too big to load", TCL_STATIC);
	goto failure;
    }

    /* Advance the break to protect the loaded module */

    (void) brk (startAddress + relocatedSize);

    /*
     * Seek to the start of the module's text.
     *
     * Note that this does not really work with large files (i.e. where
     * lseek64 exists and is different to lseek), but anyone trying to
     * dynamically load a binary that is larger than what can fit in
     * addressable memory is in trouble anyway...
     */

#if defined(__mips) || defined(mips)
    status = lseek (relocatedFd,
		    (off_t) N_TXTOFF (relocatedHead.ex_f, relocatedHead.ex_o),
		    SEEK_SET);
#else
    status = lseek (relocatedFd, (off_t) N_TXTOFF (relocatedHead), SEEK_SET);
#endif
    if (status < 0) {
	goto ioError;
    }

    /* Read in the module's text and data */

    relocatedSize = relocatedHead.a_text + relocatedHead.a_data;
    if (read (relocatedFd, startAddress, relocatedSize) < relocatedSize) {
	brk (startAddress);
      ioError:
	Tcl_AppendResult (interp, "error on intermediate file \"",
			  relocatedFileName, "\": ", Tcl_PosixError (interp),
			  (char *) NULL);
      failure:
	(void) unlink (relocatedFileName);
	return TCL_ERROR;
    }

    /* Close the intermediate file. */

    (void) close (relocatedFd);

    /* Arrange things so that intermediate symbol tables eventually get
    * deleted. */

    if (SymbolTableFile != NULL) {
	UnlinkSymbolTable ();
    } else {
	atexit (UnlinkSymbolTable);
    }
    SymbolTableFile = ckalloc (strlen (relocatedFileName) + 1);
    strcpy (SymbolTableFile, relocatedFileName);

    *loadHandle = startAddress;
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TclpFindSymbol --
 *
 *	Looks up a symbol, by name, through a handle associated with
 *	a previously loaded piece of code (shared library).
 *
 * Results:
 *	Returns a pointer to the function associated with 'symbol' if
 *	it is found.  Otherwise returns NULL and may leave an error
 *	message in the interp's result.
 *
 *----------------------------------------------------------------------
 */
Tcl_PackageInitProc*
TclpFindSymbol(interp, loadHandle, symbol)
    Tcl_Interp *interp;
    Tcl_LoadHandle loadHandle;
    CONST char *symbol;
{
    /* Look up the entry point in the load module's dictionary. */
    DictFn dictionary = (DictFn) loadHandle;
    return (Tcl_PackageInitProc*) dictionary(sym1);
}


/*
 *------------------------------------------------------------------------
 *
 * FindLibraries --
 *
 *	Find the libraries needed to link a load module at run time.
 *
 * Results:
 *	A standard Tcl completion code.  If an error occurs,
 *	an error message is left in the interp's result.  The -l and -L
 *	flags are concatenated onto the dynamic string `buf'.
 *
 *------------------------------------------------------------------------
 */

static int
FindLibraries (interp, pathPtr, buf)
    Tcl_Interp * interp;	/* Used for error reporting */
    Tcl_Obj * pathPtr;		/* Name of the load module */
    Tcl_DString * buf;		/* Buffer where the -l an -L flags */
{
    FILE * f;			/* The load module */
    int c = 0;			/* Byte from the load module */
    char * p;
    CONST char *native;

    char *fileName = Tcl_GetString(pathPtr);



( run in 0.855 second using v1.01-cache-2.11-cpan-5511b514fd6 )