sl@0: /* sl@0: * tclLoadAout.c -- sl@0: * sl@0: * This procedure provides a version of the TclLoadFile that sl@0: * provides pseudo-static linking using version-7 compatible sl@0: * a.out files described in either sys/exec.h or sys/a.out.h. sl@0: * sl@0: * Copyright (c) 1995, by General Electric Company. All rights reserved. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * This work was supported in part by the ARPA Manufacturing Automation sl@0: * and Design Engineering (MADE) Initiative through ARPA contract sl@0: * F33615-94-C-4400. sl@0: * sl@0: * RCS: @(#) $Id: tclLoadAout.c,v 1.14 2002/10/10 12:25:53 vincentdarley Exp $ sl@0: */ sl@0: sl@0: #include "tclInt.h" sl@0: #include sl@0: #ifdef HAVE_EXEC_AOUT_H sl@0: # include sl@0: #endif sl@0: #ifdef HAVE_UNISTD_H sl@0: # include sl@0: #else sl@0: # include "../compat/unistd.h" sl@0: #endif sl@0: sl@0: /* sl@0: * Some systems describe the a.out header in sys/exec.h, and some in sl@0: * a.out.h. sl@0: */ sl@0: sl@0: #ifdef USE_SYS_EXEC_H sl@0: #include sl@0: #endif sl@0: #ifdef USE_A_OUT_H sl@0: #include sl@0: #endif sl@0: #ifdef USE_SYS_EXEC_AOUT_H sl@0: #include sl@0: #define a_magic a_midmag sl@0: #endif sl@0: sl@0: /* sl@0: * TCL_LOADSHIM is the amount by which to shim the break when loading sl@0: */ sl@0: sl@0: #ifndef TCL_LOADSHIM sl@0: #define TCL_LOADSHIM 0x4000L sl@0: #endif sl@0: sl@0: /* sl@0: * TCL_LOADALIGN must be a power of 2, and is the alignment to which sl@0: * to force the origin of load modules sl@0: */ sl@0: sl@0: #ifndef TCL_LOADALIGN sl@0: #define TCL_LOADALIGN 0x4000L sl@0: #endif sl@0: sl@0: /* sl@0: * TCL_LOADMAX is the maximum size of a load module, and is used as sl@0: * a sanity check when loading sl@0: */ sl@0: sl@0: #ifndef TCL_LOADMAX sl@0: #define TCL_LOADMAX 2000000L sl@0: #endif sl@0: sl@0: /* sl@0: * Kernel calls that appear to be missing from the system .h files: sl@0: */ sl@0: sl@0: extern char * brk _ANSI_ARGS_((char *)); sl@0: extern char * sbrk _ANSI_ARGS_((size_t)); sl@0: sl@0: /* sl@0: * The static variable SymbolTableFile contains the file name where the sl@0: * result of the last link was stored. The file is kept because doing so sl@0: * allows one load module to use the symbols defined in another. sl@0: */ sl@0: sl@0: static char * SymbolTableFile = NULL; sl@0: sl@0: /* sl@0: * Type of the dictionary function that begins each load module. sl@0: */ sl@0: sl@0: typedef Tcl_PackageInitProc * (* DictFn) _ANSI_ARGS_ ((CONST char * symbol)); sl@0: sl@0: /* sl@0: * Prototypes for procedures referenced only in this file: sl@0: */ sl@0: sl@0: static int FindLibraries _ANSI_ARGS_((Tcl_Interp * interp, Tcl_Obj * pathPtr, sl@0: Tcl_DString * buf)); sl@0: static void UnlinkSymbolTable _ANSI_ARGS_((void)); sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpDlopen -- sl@0: * sl@0: * Dynamically loads a binary code file into memory and returns sl@0: * a handle to the new code. sl@0: * sl@0: * Results: sl@0: * A standard Tcl completion code. If an error occurs, an error sl@0: * message is left in the interp's result. sl@0: * sl@0: * Side effects: sl@0: * New code suddenly appears in memory. sl@0: * sl@0: * sl@0: * Bugs: sl@0: * This function does not attempt to handle the case where the sl@0: * BSS segment is not executable. It will therefore fail on sl@0: * Encore Multimax, Pyramid 90x, and similar machines. The sl@0: * reason is that the mprotect() kernel call, which would sl@0: * otherwise be employed to mark the newly-loaded text segment sl@0: * executable, results in a system crash on BSD/386. sl@0: * sl@0: * In an effort to make it fast, this function eschews the sl@0: * technique of linking the load module once, reading its header sl@0: * to determine its size, allocating memory for it, and linking sl@0: * it again. Instead, it `shims out' memory allocation by sl@0: * placing the module TCL_LOADSHIM bytes beyond the break, sl@0: * and assuming that any malloc() calls required to run the sl@0: * linker will not advance the break beyond that point. If sl@0: * the break is advanced beyonnd that point, the load will sl@0: * fail with an `inconsistent memory allocation' error. sl@0: * It perhaps ought to retry the link, but the failure has sl@0: * not been observed in two years of daily use of this function. sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: int sl@0: TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr) sl@0: Tcl_Interp *interp; /* Used for error reporting. */ sl@0: Tcl_Obj *pathPtr; /* Name of the file containing the desired sl@0: * code (UTF-8). */ sl@0: Tcl_LoadHandle *loadHandle; /* Filled with token for dynamically loaded sl@0: * file which will be passed back to sl@0: * (*unloadProcPtr)() to unload the file. */ sl@0: Tcl_FSUnloadFileProc **unloadProcPtr; sl@0: /* Filled with address of Tcl_FSUnloadFileProc sl@0: * function which should be used for sl@0: * this file. */ sl@0: { sl@0: char * inputSymbolTable; /* Name of the file containing the sl@0: * symbol table from the last link. */ sl@0: Tcl_DString linkCommandBuf; /* Command to do the run-time relocation sl@0: * of the module.*/ sl@0: char * linkCommand; sl@0: char relocatedFileName [L_tmpnam]; sl@0: /* Name of the file holding the relocated */ sl@0: /* text of the module */ sl@0: int relocatedFd; /* File descriptor of the file holding sl@0: * relocated text */ sl@0: struct exec relocatedHead; /* Header of the relocated text */ sl@0: unsigned long relocatedSize;/* Size of the relocated text */ sl@0: char * startAddress; /* Starting address of the module */ sl@0: int status; /* Status return from Tcl_ calls */ sl@0: char * p; sl@0: sl@0: /* Find the file that contains the symbols for the run-time link. */ sl@0: sl@0: if (SymbolTableFile != NULL) { sl@0: inputSymbolTable = SymbolTableFile; sl@0: } else if (tclExecutableName == NULL) { sl@0: Tcl_SetResult (interp, "can't find the tclsh executable", TCL_STATIC); sl@0: return TCL_ERROR; sl@0: } else { sl@0: inputSymbolTable = tclExecutableName; sl@0: } sl@0: sl@0: /* Construct the `ld' command that builds the relocated module */ sl@0: sl@0: tmpnam (relocatedFileName); sl@0: Tcl_DStringInit (&linkCommandBuf); sl@0: Tcl_DStringAppend (&linkCommandBuf, "exec ld -o ", -1); sl@0: Tcl_DStringAppend (&linkCommandBuf, relocatedFileName, -1); sl@0: #if defined(__mips) || defined(mips) sl@0: Tcl_DStringAppend (&linkCommandBuf, " -G 0 ", -1); sl@0: #endif sl@0: Tcl_DStringAppend (&linkCommandBuf, " -u TclLoadDictionary_", -1); sl@0: TclGuessPackageName(Tcl_GetString(pathPtr), &linkCommandBuf); sl@0: Tcl_DStringAppend (&linkCommandBuf, " -A ", -1); sl@0: Tcl_DStringAppend (&linkCommandBuf, inputSymbolTable, -1); sl@0: Tcl_DStringAppend (&linkCommandBuf, " -N -T XXXXXXXX ", -1); sl@0: Tcl_DStringAppend (&linkCommandBuf, Tcl_GetString(pathPtr), -1); sl@0: Tcl_DStringAppend (&linkCommandBuf, " ", -1); sl@0: sl@0: if (FindLibraries (interp, pathPtr, &linkCommandBuf) != TCL_OK) { sl@0: Tcl_DStringFree (&linkCommandBuf); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: linkCommand = Tcl_DStringValue (&linkCommandBuf); sl@0: sl@0: /* Determine the starting address, and plug it into the command */ sl@0: sl@0: startAddress = (char *) (((unsigned long) sbrk (0) sl@0: + TCL_LOADSHIM + TCL_LOADALIGN - 1) sl@0: & (- TCL_LOADALIGN)); sl@0: p = strstr (linkCommand, "-T") + 3; sl@0: sprintf (p, "%08lx", (long) startAddress); sl@0: p [8] = ' '; sl@0: sl@0: /* Run the linker */ sl@0: sl@0: status = Tcl_Eval (interp, linkCommand); sl@0: Tcl_DStringFree (&linkCommandBuf); sl@0: if (status != 0) { sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: /* Open the linker's result file and read the header */ sl@0: sl@0: relocatedFd = open (relocatedFileName, O_RDONLY); sl@0: if (relocatedFd < 0) { sl@0: goto ioError; sl@0: } sl@0: status= read (relocatedFd, (char *) & relocatedHead, sizeof relocatedHead); sl@0: if (status < sizeof relocatedHead) { sl@0: goto ioError; sl@0: } sl@0: sl@0: /* Check the magic number */ sl@0: sl@0: if (relocatedHead.a_magic != OMAGIC) { sl@0: Tcl_AppendResult (interp, "bad magic number in intermediate file \"", sl@0: relocatedFileName, "\"", (char *) NULL); sl@0: goto failure; sl@0: } sl@0: sl@0: /* Make sure that memory allocation is still consistent */ sl@0: sl@0: if ((unsigned long) sbrk (0) > (unsigned long) startAddress) { sl@0: Tcl_SetResult (interp, "can't load, memory allocation is inconsistent.", sl@0: TCL_STATIC); sl@0: goto failure; sl@0: } sl@0: sl@0: /* Make sure that the relocated module's size is reasonable */ sl@0: sl@0: relocatedSize = relocatedHead.a_text + relocatedHead.a_data sl@0: + relocatedHead.a_bss; sl@0: if (relocatedSize > TCL_LOADMAX) { sl@0: Tcl_SetResult (interp, "module too big to load", TCL_STATIC); sl@0: goto failure; sl@0: } sl@0: sl@0: /* Advance the break to protect the loaded module */ sl@0: sl@0: (void) brk (startAddress + relocatedSize); sl@0: sl@0: /* sl@0: * Seek to the start of the module's text. sl@0: * sl@0: * Note that this does not really work with large files (i.e. where sl@0: * lseek64 exists and is different to lseek), but anyone trying to sl@0: * dynamically load a binary that is larger than what can fit in sl@0: * addressable memory is in trouble anyway... sl@0: */ sl@0: sl@0: #if defined(__mips) || defined(mips) sl@0: status = lseek (relocatedFd, sl@0: (off_t) N_TXTOFF (relocatedHead.ex_f, relocatedHead.ex_o), sl@0: SEEK_SET); sl@0: #else sl@0: status = lseek (relocatedFd, (off_t) N_TXTOFF (relocatedHead), SEEK_SET); sl@0: #endif sl@0: if (status < 0) { sl@0: goto ioError; sl@0: } sl@0: sl@0: /* Read in the module's text and data */ sl@0: sl@0: relocatedSize = relocatedHead.a_text + relocatedHead.a_data; sl@0: if (read (relocatedFd, startAddress, relocatedSize) < relocatedSize) { sl@0: brk (startAddress); sl@0: ioError: sl@0: Tcl_AppendResult (interp, "error on intermediate file \"", sl@0: relocatedFileName, "\": ", Tcl_PosixError (interp), sl@0: (char *) NULL); sl@0: failure: sl@0: (void) unlink (relocatedFileName); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: /* Close the intermediate file. */ sl@0: sl@0: (void) close (relocatedFd); sl@0: sl@0: /* Arrange things so that intermediate symbol tables eventually get sl@0: * deleted. */ sl@0: sl@0: if (SymbolTableFile != NULL) { sl@0: UnlinkSymbolTable (); sl@0: } else { sl@0: atexit (UnlinkSymbolTable); sl@0: } sl@0: SymbolTableFile = ckalloc (strlen (relocatedFileName) + 1); sl@0: strcpy (SymbolTableFile, relocatedFileName); sl@0: sl@0: *loadHandle = startAddress; sl@0: return TCL_OK; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpFindSymbol -- sl@0: * sl@0: * Looks up a symbol, by name, through a handle associated with sl@0: * a previously loaded piece of code (shared library). sl@0: * sl@0: * Results: sl@0: * Returns a pointer to the function associated with 'symbol' if sl@0: * it is found. Otherwise returns NULL and may leave an error sl@0: * message in the interp's result. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: Tcl_PackageInitProc* sl@0: TclpFindSymbol(interp, loadHandle, symbol) sl@0: Tcl_Interp *interp; sl@0: Tcl_LoadHandle loadHandle; sl@0: CONST char *symbol; sl@0: { sl@0: /* Look up the entry point in the load module's dictionary. */ sl@0: DictFn dictionary = (DictFn) loadHandle; sl@0: return (Tcl_PackageInitProc*) dictionary(sym1); sl@0: } sl@0: sl@0: sl@0: /* sl@0: *------------------------------------------------------------------------ sl@0: * sl@0: * FindLibraries -- sl@0: * sl@0: * Find the libraries needed to link a load module at run time. sl@0: * sl@0: * Results: sl@0: * A standard Tcl completion code. If an error occurs, sl@0: * an error message is left in the interp's result. The -l and -L sl@0: * flags are concatenated onto the dynamic string `buf'. sl@0: * sl@0: *------------------------------------------------------------------------ sl@0: */ sl@0: sl@0: static int sl@0: FindLibraries (interp, pathPtr, buf) sl@0: Tcl_Interp * interp; /* Used for error reporting */ sl@0: Tcl_Obj * pathPtr; /* Name of the load module */ sl@0: Tcl_DString * buf; /* Buffer where the -l an -L flags */ sl@0: { sl@0: FILE * f; /* The load module */ sl@0: int c = 0; /* Byte from the load module */ sl@0: char * p; sl@0: CONST char *native; sl@0: sl@0: char *fileName = Tcl_GetString(pathPtr); sl@0: sl@0: /* Open the load module */ sl@0: sl@0: native = Tcl_FSGetNativePath(pathPtr); sl@0: f = fopen(native, "rb"); /* INTL: Native. */ sl@0: sl@0: if (f == NULL) { sl@0: Tcl_AppendResult (interp, "couldn't open \"", fileName, "\": ", sl@0: Tcl_PosixError (interp), (char *) NULL); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: /* Search for the library list in the load module */ sl@0: sl@0: p = "@LIBS: "; sl@0: while (*p != '\0' && (c = getc (f)) != EOF) { sl@0: if (c == *p) { sl@0: ++p; sl@0: } sl@0: else { sl@0: p = "@LIBS: "; sl@0: if (c == *p) { sl@0: ++p; sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* No library list -- this must be an ill-formed module */ sl@0: sl@0: if (c == EOF) { sl@0: Tcl_AppendResult (interp, "File \"", fileName, sl@0: "\" is not a Tcl load module.", (char *) NULL); sl@0: (void) fclose (f); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: /* Accumulate the library list */ sl@0: sl@0: while ((c = getc (f)) != '\0' && c != EOF) { sl@0: char cc = c; sl@0: Tcl_DStringAppend (buf, &cc, 1); sl@0: } sl@0: (void) fclose (f); sl@0: sl@0: if (c == EOF) { sl@0: Tcl_AppendResult (interp, "Library directory in \"", fileName, sl@0: "\" ends prematurely.", (char *) NULL); sl@0: return TCL_ERROR; sl@0: } sl@0: sl@0: return TCL_OK; sl@0: } sl@0: sl@0: /* sl@0: *------------------------------------------------------------------------ sl@0: * sl@0: * UnlinkSymbolTable -- sl@0: * sl@0: * Remove the symbol table file from the last dynamic link. sl@0: * sl@0: * Results: sl@0: * None. sl@0: * sl@0: * Side effects: sl@0: * The symbol table file from the last dynamic link is removed. sl@0: * This function is called when (a) a new symbol table is present sl@0: * because another dynamic link is complete, or (b) the process sl@0: * is exiting. sl@0: *------------------------------------------------------------------------ sl@0: */ sl@0: sl@0: static void sl@0: UnlinkSymbolTable () sl@0: { sl@0: (void) unlink (SymbolTableFile); sl@0: ckfree (SymbolTableFile); sl@0: SymbolTableFile = NULL; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclpUnloadFile -- sl@0: * sl@0: * Unloads a dynamically loaded binary code file from memory. sl@0: * Code pointers in the formerly loaded file are no longer valid sl@0: * after calling this function. sl@0: * sl@0: * Results: sl@0: * None. sl@0: * sl@0: * Side effects: sl@0: * Does nothing. Can anything be done? sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: void sl@0: TclpUnloadFile(loadHandle) sl@0: Tcl_LoadHandle loadHandle; /* loadHandle returned by a previous call sl@0: * to TclpDlopen(). The loadHandle is sl@0: * a token that represents the loaded sl@0: * file. */ sl@0: { sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclGuessPackageName -- sl@0: * sl@0: * If the "load" command is invoked without providing a package sl@0: * name, this procedure is invoked to try to figure it out. sl@0: * sl@0: * Results: sl@0: * Always returns 0 to indicate that we couldn't figure out a sl@0: * package name; generic code will then try to guess the package sl@0: * from the file name. A return value of 1 would have meant that sl@0: * we figured out the package name and put it in bufPtr. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: int sl@0: TclGuessPackageName(fileName, bufPtr) sl@0: CONST char *fileName; /* Name of file containing package (already sl@0: * translated to local form if needed). */ sl@0: Tcl_DString *bufPtr; /* Initialized empty dstring. Append sl@0: * package name to this if possible. */ sl@0: { sl@0: CONST char *p, *q; sl@0: char *r; sl@0: sl@0: if ((q = strrchr(fileName,'/'))) { sl@0: q++; sl@0: } else { sl@0: q = fileName; sl@0: } sl@0: if (!strncmp(q,"lib",3)) { sl@0: q+=3; sl@0: } sl@0: p = q; sl@0: while ((*p) && (*p != '.') && ((*p<'0') || (*p>'9'))) { sl@0: p++; sl@0: } sl@0: if ((p>q+2) && !strncmp(p-2,"_G0.",4)) { sl@0: p-=2; sl@0: } sl@0: if (p