os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclLoadDld.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* 
sl@0
     2
 * tclLoadDld.c --
sl@0
     3
 *
sl@0
     4
 *	This procedure provides a version of the TclLoadFile that
sl@0
     5
 *	works with the "dld_link" and "dld_get_func" library procedures
sl@0
     6
 *	for dynamic loading.  It has been tested on Linux 1.1.95 and
sl@0
     7
 *	dld-3.2.7.  This file probably isn't needed anymore, since it
sl@0
     8
 *	makes more sense to use "dl_open" etc.
sl@0
     9
 *
sl@0
    10
 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
sl@0
    11
 *
sl@0
    12
 * See the file "license.terms" for information on usage and redistribution
sl@0
    13
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
sl@0
    14
 *
sl@0
    15
 * RCS: @(#) $Id: tclLoadDld.c,v 1.12 2002/10/10 12:25:53 vincentdarley Exp $
sl@0
    16
 */
sl@0
    17
sl@0
    18
#include "tclInt.h"
sl@0
    19
#include "dld.h"
sl@0
    20
sl@0
    21
/*
sl@0
    22
 * In some systems, like SunOS 4.1.3, the RTLD_NOW flag isn't defined
sl@0
    23
 * and this argument to dlopen must always be 1.
sl@0
    24
 */
sl@0
    25
sl@0
    26
#ifndef RTLD_NOW
sl@0
    27
#   define RTLD_NOW 1
sl@0
    28
#endif
sl@0
    29

sl@0
    30
/*
sl@0
    31
 *----------------------------------------------------------------------
sl@0
    32
 *
sl@0
    33
 * TclpDlopen --
sl@0
    34
 *
sl@0
    35
 *	Dynamically loads a binary code file into memory and returns
sl@0
    36
 *	a handle to the new code.
sl@0
    37
 *
sl@0
    38
 * Results:
sl@0
    39
 *	A standard Tcl completion code.  If an error occurs, an error
sl@0
    40
 *	message is left in the interp's result.
sl@0
    41
 *
sl@0
    42
 * Side effects:
sl@0
    43
 *	New code suddenly appears in memory.
sl@0
    44
 *
sl@0
    45
 *----------------------------------------------------------------------
sl@0
    46
 */
sl@0
    47
sl@0
    48
int
sl@0
    49
TclpDlopen(interp, pathPtr, loadHandle, unloadProcPtr)
sl@0
    50
    Tcl_Interp *interp;		/* Used for error reporting. */
sl@0
    51
    Tcl_Obj *pathPtr;		/* Name of the file containing the desired
sl@0
    52
				 * code (UTF-8). */
sl@0
    53
    Tcl_LoadHandle *loadHandle;	/* Filled with token for dynamically loaded
sl@0
    54
				 * file which will be passed back to 
sl@0
    55
				 * (*unloadProcPtr)() to unload the file. */
sl@0
    56
    Tcl_FSUnloadFileProc **unloadProcPtr;	
sl@0
    57
				/* Filled with address of Tcl_FSUnloadFileProc
sl@0
    58
				 * function which should be used for
sl@0
    59
				 * this file. */
sl@0
    60
{
sl@0
    61
    static int firstTime = 1;
sl@0
    62
    int returnCode;
sl@0
    63
    char *fileName;
sl@0
    64
    CONST char *native;
sl@0
    65
    
sl@0
    66
    /*
sl@0
    67
     *  The dld package needs to know the pathname to the tcl binary.
sl@0
    68
     *  If that's not known, return an error.
sl@0
    69
     */
sl@0
    70
sl@0
    71
    if (firstTime) {
sl@0
    72
	if (tclExecutableName == NULL) {
sl@0
    73
	    Tcl_SetResult(interp,
sl@0
    74
		    "don't know name of application binary file, so can't initialize dynamic loader",
sl@0
    75
		    TCL_STATIC);
sl@0
    76
	    return TCL_ERROR;
sl@0
    77
	}
sl@0
    78
	returnCode = dld_init(tclExecutableName);
sl@0
    79
	if (returnCode != 0) {
sl@0
    80
	    Tcl_AppendResult(interp,
sl@0
    81
		    "initialization failed for dynamic loader: ",
sl@0
    82
		    dld_strerror(returnCode), (char *) NULL);
sl@0
    83
	    return TCL_ERROR;
sl@0
    84
	}
sl@0
    85
	firstTime = 0;
sl@0
    86
    }
sl@0
    87
sl@0
    88
    fileName = Tcl_GetString(pathPtr);
sl@0
    89
sl@0
    90
    /* 
sl@0
    91
     * First try the full path the user gave us.  This is particularly
sl@0
    92
     * important if the cwd is inside a vfs, and we are trying to load
sl@0
    93
     * using a relative path.
sl@0
    94
     */
sl@0
    95
    native = Tcl_FSGetNativePath(pathPtr);
sl@0
    96
    returnCode = dld_link(native);
sl@0
    97
    
sl@0
    98
    if (returnCode != 0) {
sl@0
    99
	Tcl_DString ds;
sl@0
   100
	native = Tcl_UtfToExternalDString(NULL, fileName, -1, &ds);
sl@0
   101
	returnCode = dld_link(native);
sl@0
   102
	Tcl_DStringFree(&ds);
sl@0
   103
    }
sl@0
   104
sl@0
   105
    if (returnCode != 0) {
sl@0
   106
	Tcl_AppendResult(interp, "couldn't load file \"", 
sl@0
   107
			 fileName, "\": ", 
sl@0
   108
			 dld_strerror(returnCode), (char *) NULL);
sl@0
   109
	return TCL_ERROR;
sl@0
   110
    }
sl@0
   111
    *loadHandle = (Tcl_LoadHandle) strcpy(
sl@0
   112
	    (char *) ckalloc((unsigned) (strlen(fileName) + 1)), fileName);
sl@0
   113
    *unloadProcPtr = &TclpUnloadFile;
sl@0
   114
    return TCL_OK;
sl@0
   115
}
sl@0
   116

sl@0
   117
/*
sl@0
   118
 *----------------------------------------------------------------------
sl@0
   119
 *
sl@0
   120
 * TclpFindSymbol --
sl@0
   121
 *
sl@0
   122
 *	Looks up a symbol, by name, through a handle associated with
sl@0
   123
 *	a previously loaded piece of code (shared library).
sl@0
   124
 *
sl@0
   125
 * Results:
sl@0
   126
 *	Returns a pointer to the function associated with 'symbol' if
sl@0
   127
 *	it is found.  Otherwise returns NULL and may leave an error
sl@0
   128
 *	message in the interp's result.
sl@0
   129
 *
sl@0
   130
 *----------------------------------------------------------------------
sl@0
   131
 */
sl@0
   132
Tcl_PackageInitProc*
sl@0
   133
TclpFindSymbol(interp, loadHandle, symbol) 
sl@0
   134
    Tcl_Interp *interp;
sl@0
   135
    Tcl_LoadHandle loadHandle;
sl@0
   136
    CONST char *symbol;
sl@0
   137
{
sl@0
   138
    return (Tcl_PackageInitProc *) dld_get_func(symbol);
sl@0
   139
}
sl@0
   140

sl@0
   141
/*
sl@0
   142
 *----------------------------------------------------------------------
sl@0
   143
 *
sl@0
   144
 * TclpUnloadFile --
sl@0
   145
 *
sl@0
   146
 *	Unloads a dynamically loaded binary code file from memory.
sl@0
   147
 *	Code pointers in the formerly loaded file are no longer valid
sl@0
   148
 *	after calling this function.
sl@0
   149
 *
sl@0
   150
 * Results:
sl@0
   151
 *	None.
sl@0
   152
 *
sl@0
   153
 * Side effects:
sl@0
   154
 *	Code removed from memory.
sl@0
   155
 *
sl@0
   156
 *----------------------------------------------------------------------
sl@0
   157
 */
sl@0
   158
sl@0
   159
void
sl@0
   160
TclpUnloadFile(loadHandle)
sl@0
   161
    Tcl_LoadHandle loadHandle;	/* loadHandle returned by a previous call
sl@0
   162
				 * to TclpDlopen().  The loadHandle is 
sl@0
   163
				 * a token that represents the loaded 
sl@0
   164
				 * file. */
sl@0
   165
{
sl@0
   166
    char *fileName;
sl@0
   167
sl@0
   168
    handle = (char *) loadHandle;
sl@0
   169
    dld_unlink_by_file(handle, 0);
sl@0
   170
    ckfree(handle);
sl@0
   171
}
sl@0
   172

sl@0
   173
/*
sl@0
   174
 *----------------------------------------------------------------------
sl@0
   175
 *
sl@0
   176
 * TclGuessPackageName --
sl@0
   177
 *
sl@0
   178
 *	If the "load" command is invoked without providing a package
sl@0
   179
 *	name, this procedure is invoked to try to figure it out.
sl@0
   180
 *
sl@0
   181
 * Results:
sl@0
   182
 *	Always returns 0 to indicate that we couldn't figure out a
sl@0
   183
 *	package name;  generic code will then try to guess the package
sl@0
   184
 *	from the file name.  A return value of 1 would have meant that
sl@0
   185
 *	we figured out the package name and put it in bufPtr.
sl@0
   186
 *
sl@0
   187
 * Side effects:
sl@0
   188
 *	None.
sl@0
   189
 *
sl@0
   190
 *----------------------------------------------------------------------
sl@0
   191
 */
sl@0
   192
sl@0
   193
int
sl@0
   194
TclGuessPackageName(fileName, bufPtr)
sl@0
   195
    CONST char *fileName;	/* Name of file containing package (already
sl@0
   196
				 * translated to local form if needed). */
sl@0
   197
    Tcl_DString *bufPtr;	/* Initialized empty dstring.  Append
sl@0
   198
				 * package name to this if possible. */
sl@0
   199
{
sl@0
   200
    return 0;
sl@0
   201
}