main (int argc, char **argv) { char *libname = NULL; if (LIBNAME) libname = LIBNAME; else if (argc == 2 && argv[1] != NULL) libname = argv[1]; if (libname) { if (! NSAddLibrary (libname)) { fprintf (stderr, "Unable to load `%s' library.\n", libname); exit (1); } if (NSIsSymbolNameDefined ("_foo")) { int (*addr)(void) = NSAddressOfSymbol (NSLookupAndBindSymbol ("_foo")); printf ("foo is resolved to address %lx\n", (unsigned long) addr); if (addr) addr (); } } else { fprintf (stderr, "No library specified.\n"); } if (SECONDLIBNAME) libname = SECONDLIBNAME; else if (argc == 2 && argv[1] != NULL) libname = argv[1]; if (libname) { if (! NSAddLibrary (libname)) { fprintf (stderr, "Unable to load `%s' library.\n", libname); exit (1); } if (NSIsSymbolNameDefined ("_blubby")) { int (*addr)(int) = NSAddressOfSymbol (NSLookupAndBindSymbol ("_blubby")); printf ("blubby is resolved to address %lx\n", (unsigned long) addr); if (addr) { addr (5); addr (6); } } } else { fprintf (stderr, "No library specified.\n"); } }
/* ggDarwinDLSym implements a "dlsym" wrapper */ void *ggDarwinDLSym(gg_dlhand handle, const char *symbol) { void *nsaddr = NULL; NSSymbol nssymbol = 0; struct gg_dlhand_darwin_t *darwin_module; darwin_module = (struct gg_dlhand_darwin_t *)handle; switch (darwin_module->nsmodule_flags) { case NSLINKMODULE_OPTION_NONE: nssymbol = NSLookupAndBindSymbol(symbol); break; case NSLINKMODULE_OPTION_PRIVATE: nssymbol = NSLookupSymbolInModule(darwin_module->nsmodule, symbol); break; } /* switch */ nsaddr = NSAddressOfSymbol(nssymbol); /* no error handling needed here. The error handlers * are called, when an error occurs. */ return nsaddr; } /* ggDarwinDLSym */
void* hs_GLUT_getProcAddress(const char *name) { NSSymbol symbol; /* Prepend a '_' for the Unix C symbol mangling convention */ char* symbolName = (char*)malloc(strlen(name) + 2); if (!symbolName) { return NULL; } symbolName[0] = '_'; strcpy(symbolName + 1, name); if (!NSIsSymbolNameDefined(symbolName)) { free(symbolName); return NULL; } symbol = NSLookupAndBindSymbol(symbolName); free(symbolName); if (!symbol) { return NULL; } return NSAddressOfSymbol(symbol); }
/* used by dlsym to find the symbol */ void *dlsymIntern(void *handle, const char *symbol) { NSSymbol nssym = NULL; if (handle == (void *)-1) { /* Global context */ if (NSIsSymbolNameDefined(symbol)) nssym = NSLookupAndBindSymbol(symbol); } else { if (is_mach_header(handle)) { /* library */ if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol)) nssym = NSLookupSymbolInImage((struct mach_header *)handle, symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } else /* bundle */ nssym = NSLookupSymbolInModule((NSModule)handle, symbol); } if (!nssym) { error(0, "Symbol \"%s\" Not found", symbol); return NULL; } return NSAddressOfSymbol(nssym); }
static void* get_proc_addr(const char *name) { NSSymbol symbol; /* Prepend a '_' for the Unix C symbol mangling convention */ char* symbolName = (char*)malloc(strlen(name) + 2); if (!symbolName) { fprintf(stderr, "Failed to allocate memory for NSGLGetProcAddress\n"); return NULL; } symbolName[0] = '_'; strcpy(symbolName + 1, name); if (!NSIsSymbolNameDefined(symbolName)) { free(symbolName); return NULL; } symbol = NSLookupAndBindSymbol(symbolName); free(symbolName); if (!symbol) { return NULL; } return NSAddressOfSymbol(symbol); }
int MyGetExecutablePath(char *execPath, size_t *execPathSize) { if (NSIsSymbolNameDefined("__NSGetExecutablePath")) { return ((NSGetExecutablePathProcPtr) NSAddressOfSymbol(NSLookupAndBindSymbol("__NSGetExecutablePath")))(execPath, execPathSize); } return(0); }
void *FFGLExtensions::GetProcAddress(char *name) { #ifdef _WIN32 void *result = wglGetProcAddress(name); if (result!=NULL) return result; #else #ifdef TARGET_OS_MAC // Prepend a '_' for the Unix C symbol mangling convention int symbolLength = strlen(name) + 2; //1 for the _, another for the trailing null char symbolName[1024]; if (symbolLength>sizeof(symbolName)) { //symbol name too long; throw; return NULL; } strcpy(symbolName + 1, name); symbolName[0] = '_'; NSSymbol symbol = NULL; if (NSIsSymbolNameDefined(symbolName)) symbol = NSLookupAndBindSymbol(symbolName); if (symbol!=NULL) { return NSAddressOfSymbol(symbol); } #else #ifdef __linux__ void *result = (void *)(unsigned)glXGetProcAddress((const GLubyte *)name); if (result!=NULL) return result; #else #error Define this for your OS #endif #endif #endif throw;//this will be caught by one of the Init() functions below return NULL; }
void *dlsym(void *handle, const char *symbol) { int sym_len = str_len(symbol); void *value = NULL; char *malloc_sym = NULL; NSSymbol *nssym = 0; malloc_sym = malloc(sym_len + 2); if (malloc_sym) { sprintf(malloc_sym, "_%s", symbol); /* If the handle is -1, if is the app global context */ if (handle == (void *)-1) { /* Global context, use NSLookupAndBindSymbol */ if (NSIsSymbolNameDefined(malloc_sym)) { nssym = NSLookupAndBindSymbol(malloc_sym); } } /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image for libraries, and NSLookupSymbolInModule for bundles */ else { /* Check for both possible magic numbers depending on x86/ppc byte order */ if ((((struct mach_header *)handle)->magic == MH_MAGIC) || (((struct mach_header *)handle)->magic == MH_CIGAM)) { if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, malloc_sym)) { nssym = NSLookupSymbolInImage((struct mach_header *)handle, malloc_sym, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } } else { nssym = NSLookupSymbolInModule(handle, malloc_sym); } } if (!nssym) { error(0, "Symbol \"%s\" Not found", symbol); } value = NSAddressOfSymbol(nssym); free(malloc_sym); } else { error(-1, "Unable to allocate memory"); } return value; }
void __CFRecordAllocationEvent(int eventnum, void *ptr, int size, int data, const char *classname) { static void (*dyfunc)(int, void *, int, int, const char *) = (void *)0xFFFFFFFF; if (!__CFOASafe) return; if ((void *)0xFFFFFFFF == dyfunc) { dyfunc = NULL; if (NSIsSymbolNameDefined("__OARecordAllocationEvent")) dyfunc = (void *)NSAddressOfSymbol(NSLookupAndBindSymbol("__OARecordAllocationEvent")); } if (NULL != dyfunc) { dyfunc(eventnum, ptr, size, data, classname); } }
void __CFSetLastAllocationEventName(void *ptr, const char *classname) { static void (*dyfunc)(void *, const char *) = (void *)0xFFFFFFFF; if (!__CFOASafe) return; if ((void *)0xFFFFFFFF == dyfunc) { dyfunc = NULL; if (NSIsSymbolNameDefined("__OASetLastAllocationEventName")) dyfunc = (void *)NSAddressOfSymbol(NSLookupAndBindSymbol("__OASetLastAllocationEventName")); } if (NULL != dyfunc) { dyfunc(ptr, classname); } }
static void *dlsym(void *handle, const char *symbol) { char _symbol[256]; NSSymbol *nsSymbol= 0; snprintf(_symbol, sizeof(_symbol), "_%s", symbol); dprintf((stderr, "dlsym: looking for %s (%s) in %d\n", symbol, _symbol, (int)handle)); if (!handle) { dprintf((stderr, "dlsym: setting app context for this handle\n")); handle= DL_APP_CONTEXT; } if (DL_APP_CONTEXT == handle) { dprintf((stderr, "dlsym: looking in app context\n")); if (NSIsSymbolNameDefined(_symbol)) nsSymbol= NSLookupAndBindSymbol(_symbol); } else { if (( (MH_MAGIC == ((struct mach_header *)handle)->magic)) /* ppc */ || (MH_CIGAM == ((struct mach_header *)handle)->magic)) /* 386 */ { if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, _symbol)) { nsSymbol= NSLookupSymbolInImage ((struct mach_header *)handle, _symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND /*| NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR*/); dprintf((stderr, "dlsym: bundle (image) lookup returned %p\n", nsSymbol)); } else dprintf((stderr, "dlsym: bundle (image) symbol not defined\n")); } else { nsSymbol= NSLookupSymbolInModule(handle, _symbol); dprintf((stderr, "dlsym: dylib (module) lookup returned %p\n", nsSymbol)); } } if (!nsSymbol) { dlSetError("symbol not found: %s", _symbol); return 0; } return NSAddressOfSymbol(nsSymbol); }
void * MyNSGLGetProcAddress (const char *name) { NSSymbol symbol; char *symbolName; symbolName = (char*) malloc (strlen (name) + 2); // 1 strcpy(symbolName + 1, name); // 2 symbolName[0] = '_'; // 3 symbol = NULL; if (NSIsSymbolNameDefined (symbolName)) // 4 symbol = NSLookupAndBindSymbol (symbolName); free (symbolName); // 5 return symbol ? NSAddressOfSymbol (symbol) : NULL; // 6 }
void* glGetProcAddress( const char* name ) { NSSymbol symbol; char *symbolName; symbolName = malloc (strlen (name) + 2); strcpy(symbolName + 1, name); symbolName[0] = '_'; symbol = NULL; if (NSIsSymbolNameDefined (symbolName)) symbol = NSLookupAndBindSymbol (symbolName); free (symbolName); return symbol ? NSAddressOfSymbol (symbol) : NULL; }
void __CFOAInitialize(void) { static void (*dyfunc)(void) = (void *)0xFFFFFFFF; if (NULL == getenv("OAKeepAllocationStatistics")) return; if ((void *)0xFFFFFFFF == dyfunc) { dyfunc = NULL; if (NSIsSymbolNameDefined("__OAInitialize")) dyfunc = (void *)NSAddressOfSymbol(NSLookupAndBindSymbol("__OAInitialize")); } if (NULL != dyfunc) { dyfunc(); __CFOASafe = true; } }
/* * uuid() is called to set the uuid[] bytes for the uuid load command. */ extern void uuid( uint8_t *uuid) { struct uuid_command u; #if !(defined(KLD) && defined(__STATIC__)) void (*uuid_func)(uint8_t *out); NSSymbol nssymbol; int fd; ssize_t n; /* * We would like to just #include <uuid/uuid.h> and but that header * file did not exist on system until Mac OS 10.4 . So instead we * dynamically lookup uuid_generate_random() and if it is defined we * call it indirectly. */ #if HAVE_NSISSYMBOLNAMEDEFINED if(NSIsSymbolNameDefined("_uuid_generate_random")){ nssymbol = (void *)NSLookupAndBindSymbol("_uuid_generate_random"); uuid_func = NSAddressOfSymbol(nssymbol); uuid_func(uuid); #else if (0){ #endif } /* * Since we don't have uuid_generate() just read bytes from /dev/urandom */ else{ fd = open("/dev/urandom", O_RDONLY, 0); if(fd == -1){ system_warning("can't open: /dev/urandom to fill in uuid load " "command (using bytes of zero)"); memset(uuid, '\0', sizeof(u.uuid)); } else{ n = read(fd, uuid, sizeof(u.uuid)); if(n != sizeof(u.uuid)){ system_warning("can't read bytes from: /dev/urandom to " "fill in uuid load command (using bytes of zero)"); memset(uuid, '\0', sizeof(u.uuid)); } (void)close(fd); } } #else /* defined(KLD) && defined(__STATIC__) */ memset(uuid, '\0', sizeof(u.uuid)); #endif /* !(defined(KLD) && defined(__STATIC__)) */ }
void* NSGLGetProcAddress (const GLubyte *name) { NSSymbol symbol; char* symbolName; /* prepend a '_' for the Unix C symbol mangling convention */ symbolName = malloc(strlen((const char*)name) + 2); strcpy(symbolName+1, (const char*)name); symbolName[0] = '_'; symbol = NULL; if (NSIsSymbolNameDefined(symbolName)) symbol = NSLookupAndBindSymbol(symbolName); free(symbolName); return symbol ? NSAddressOfSymbol(symbol) : NULL; }
int main () { fputs ("hi\n", stdout); fputs ("there\n", stdout); puts ("test"); /* this call will always go to libSystem, even though it is defined in mylib.dylib */ if (NSIsSymbolNameDefined ("_foo")) { int (*addr)(void) = NSAddressOfSymbol (NSLookupAndBindSymbol ("_foo")); if (addr) { addr (); } } }
static void * mydlsym(char *symbol) { void *addr; char funcname[256]; #if HAVE_SNPRINTF snprintf(funcname, sizeof(funcname), "_%.200s", symbol); #else sprintf(funcname, "_%.200s", symbol); #endif if (NSIsSymbolNameDefined(funcname)) addr = NSAddressOfSymbol(NSLookupAndBindSymbol(funcname)); else addr = NULL; return addr; }
//---------------------------------------------------------------------------- void* GTGetFunctionPointer (const char* function) { NSSymbol symbol; char* symbolName; // Prepend a '_' for the Unix C symbol mangling convention. symbolName = (char*)malloc(strlen((const char*)function) + 2); strcpy(symbolName + 1,(const char*)function); symbolName[0] = '_'; symbol = 0; if (NSIsSymbolNameDefined(symbolName)) { symbol = NSLookupAndBindSymbol(symbolName); } free(symbolName); return symbol ? NSAddressOfSymbol(symbol) : 0; }
PGFunction pg_dlsym(void *handle, char *funcname) { NSSymbol symbol; char *symname = (char *) malloc(strlen(funcname) + 2); sprintf(symname, "_%s", funcname); if (NSIsSymbolNameDefined(symname)) { symbol = NSLookupAndBindSymbol(symname); free(symname); return (PGFunction) NSAddressOfSymbol(symbol); } else { free(symname); return NULL; } }
/* Get the address for a specifed symbol */ void *dso_symbol(dso_handle hdl, const char *nam) { NSSymbol sym = NULL; NSModule mod = NULL; char *und = NULL; void *add = NULL; int x = 0; /* Check parameters */ if (hdl == NULL) { log_error("Invalid library handler specified"); return (NULL); } if (nam == NULL) { log_error("Invalid symbol name specified"); return (NULL); } /* Process the correct name (add a _ before the name) */ while (nam[x] != '\0') x++; und = (char *)malloc(sizeof(char) * (x + 2)); while (x >= 0) und[x + 1] = nam[x--]; und[0] = '_'; /* Find the symbol */ sym = NSLookupAndBindSymbol(und); free(und); if (sym == NULL) return (NULL); /* Dump some debugging output since this part is shaky */ mod = NSModuleForSymbol(sym); add = NSAddressOfSymbol(sym); log_debug("Symbol \"%s\" found in module \"%s\" at address \"0x%08X\"", NSNameOfSymbol(sym), NSNameOfModule(mod), add); /* We want to return the address of the symbol */ return (add); }
/* dlsymIntern is used by dlsym to find the symbol */ void *dlsymIntern(void *handle, const char *symbol) { NSSymbol *nssym = 0; /* If the handle is -1, if is the app global context */ if (handle == (void *)-1) { /* Global context, use NSLookupAndBindSymbol */ if (NSIsSymbolNameDefined(symbol)) { nssym = NSLookupAndBindSymbol(symbol); } } /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image for libraries, and NSLookupSymbolInModule for bundles */ else { /* Check for both possible magic numbers depending on x86/ppc byte order */ if ((((struct mach_header *)handle)->magic == MH_MAGIC) || (((struct mach_header *)handle)->magic == MH_CIGAM)) { if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol)) { nssym = NSLookupSymbolInImage((struct mach_header *)handle, symbol, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } } else { nssym = NSLookupSymbolInModule(handle, symbol); } } if (!nssym) { error(0, "Symbol \"%s\" Not found", symbol); return NULL; } return NSAddressOfSymbol(nssym); }
void* dlsym(void* handle, char* symbol) { if (handle == &dl_self) { if (NSIsSymbolNameDefined(symbol)) { NSSymbol retsym; retsym = NSLookupAndBindSymbol(symbol); return NSAddressOfSymbol(retsym); } else { last_error = DLSYM_ERROR; return NULL; } } else { if (NSIsSymbolNameDefinedInImage(handle, symbol)) { NSSymbol retsym; retsym = NSLookupSymbolInImage(handle, symbol, 0); return NSAddressOfSymbol(retsym); } else { last_error = DLSYM_ERROR; return NULL; } } }
static void* LookupSymbol(const mach_header* aLib, const char* aSymbolName) { // Try to use |NSLookupSymbolInImage| since it is faster than searching // the global symbol table. If we couldn't get a mach_header pointer // for the XPCOM dylib, then use |NSLookupAndBindSymbol| to search the // global symbol table (this shouldn't normally happen, unless the user // has called XPCOMGlueStartup(".") and relies on libxpcom.dylib // already being loaded). NSSymbol sym = nsnull; if (aLib) { sym = NSLookupSymbolInImage(aLib, aSymbolName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR); } else { if (NSIsSymbolNameDefined(aSymbolName)) sym = NSLookupAndBindSymbol(aSymbolName); } if (!sym) return nsnull; return NSAddressOfSymbol(sym); }
Togl_FuncPtr Togl_GetProcAddr(const char *funcname) { #if defined(TOGL_OSMESA) return (Togl_FuncPtr) OSMesaGetProcAddress(funcname); #elif defined(TOGL_WGL) return (Togl_FuncPtr) wglGetProcAddress(funcname); #elif defined(__APPLE__) char buf[256]; snprintf(buf, sizeof buf - 1, "_%s", funcname); buf[sizeof buf - 1] = '\0'; if (NSIsSymbolNameDefined(buf)) { NSSymbol nssym; nssym = NSLookupAndBindSymbol(buf); if (nssym) return (Togl_FuncPtr) NSAddressOfSymbol(nssym); } return NULL; #else # if defined(TOGL_X11) && defined(GLX_VERSION_1_4) /* Strictly speaking, we can only call glXGetProcAddress if glXQueryVersion * says we're using version 1.4 or later. */ return (Togl_FuncPtr) glXGetProcAddress(funcname); # else /* Linux, IRIX, OSF/1, ? */ static void *dlHandle = NULL; if (dlHandle == NULL) dlHandle = dlopen(NULL, RTLD_LAZY); /* Strictly speaking, the following cast of a data pointer to a function * pointer is not legal in ISO C, but we don't have any choice. */ return (Togl_FuncPtr) dlsym(dlHandle, funcname); # endif #endif }
APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym, apr_dso_handle_t *handle, const char *symname) { #if defined(DSO_USE_SHL) void *symaddr = NULL; int status; errno = 0; status = shl_findsym((shl_t *)&handle->handle, symname, TYPE_PROCEDURE, &symaddr); if (status == -1 && errno == 0) /* try TYPE_DATA instead */ status = shl_findsym((shl_t *)&handle->handle, symname, TYPE_DATA, &symaddr); if (status == -1) return APR_ESYMNOTFOUND; *ressym = symaddr; return APR_SUCCESS; #elif defined(DSO_USE_DYLD) void *retval = NULL; NSSymbol symbol; char *symname2 = (char*)malloc(sizeof(char)*(strlen(symname)+2)); sprintf(symname2, "_%s", symname); #ifdef NSLINKMODULE_OPTION_PRIVATE if (handle->handle == DYLD_LIBRARY_HANDLE) { symbol = NSLookupAndBindSymbol(symname2); } else { symbol = NSLookupSymbolInModule((NSModule)handle->handle, symname2); } #else symbol = NSLookupAndBindSymbol(symname2); #endif free(symname2); if (symbol == NULL) { handle->errormsg = "undefined symbol"; return APR_ESYMNOTFOUND; } retval = NSAddressOfSymbol(symbol); if (retval == NULL) { handle->errormsg = "cannot resolve symbol"; return APR_ESYMNOTFOUND; } *ressym = retval; return APR_SUCCESS; #elif defined(DSO_USE_DLFCN) #if defined(DLSYM_NEEDS_UNDERSCORE) void *retval; char *symbol = (char*)malloc(sizeof(char)*(strlen(symname)+2)); sprintf(symbol, "_%s", symname); retval = dlsym(handle->handle, symbol); free(symbol); #elif defined(SEQUENT) || defined(SNI) void *retval = dlsym(handle->handle, (char *)symname); #else void *retval = dlsym(handle->handle, symname); #endif /* DLSYM_NEEDS_UNDERSCORE */ if (retval == NULL) { handle->errormsg = dlerror(); return APR_ESYMNOTFOUND; } *ressym = retval; return APR_SUCCESS; #endif /* DSO_USE_x */ }
static void calculate_path(void) { extern char *Py_GetProgramName(void); static char delimiter[2] = {DELIM, '\0'}; static char separator[2] = {SEP, '\0'}; char *pythonpath = PYTHONPATH; char *rtpypath = Py_GETENV("PYTHONPATH"); char *home = Py_GetPythonHome(); char *path = getenv("PATH"); char *prog = Py_GetProgramName(); char argv0_path[MAXPATHLEN+1]; char zip_path[MAXPATHLEN+1]; int pfound, efound; /* 1 if found; -1 if found build directory */ char *buf; size_t bufsz; size_t prefixsz; char *defpath = pythonpath; #ifdef WITH_NEXT_FRAMEWORK NSModule pythonModule; #endif /* If there is no slash in the argv0 path, then we have to * assume python is on the user's $PATH, since there's no * other way to find a directory to start the search from. If * $PATH isn't exported, you lose. */ if (strchr(prog, SEP)) strncpy(progpath, prog, MAXPATHLEN); else if (path) { while (1) { char *delim = strchr(path, DELIM); if (delim) { size_t len = delim - path; if (len > MAXPATHLEN) len = MAXPATHLEN; strncpy(progpath, path, len); *(progpath + len) = '\0'; } else strncpy(progpath, path, MAXPATHLEN); joinpath(progpath, prog); if (isxfile(progpath)) break; if (!delim) { progpath[0] = '\0'; break; } path = delim + 1; } } else progpath[0] = '\0'; if (progpath[0] != SEP) absolutize(progpath); strncpy(argv0_path, progpath, MAXPATHLEN); argv0_path[MAXPATHLEN] = '\0'; #ifdef WITH_NEXT_FRAMEWORK /* On Mac OS X we have a special case if we're running from a framework. ** This is because the python home should be set relative to the library, ** which is in the framework, not relative to the executable, which may ** be outside of the framework. Except when we're in the build directory... */ pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize")); /* Use dylib functions to find out where the framework was loaded from */ buf = (char *)NSLibraryNameForModule(pythonModule); if (buf != NULL) { /* We're in a framework. */ /* See if we might be in the build directory. The framework in the ** build directory is incomplete, it only has the .dylib and a few ** needed symlinks, it doesn't have the Lib directories and such. ** If we're running with the framework from the build directory we must ** be running the interpreter in the build directory, so we use the ** build-directory-specific logic to find Lib and such. */ strncpy(argv0_path, buf, MAXPATHLEN); reduce(argv0_path); joinpath(argv0_path, lib_python); joinpath(argv0_path, LANDMARK); if (!ismodule(argv0_path)) { /* We are in the build directory so use the name of the executable - we know that the absolute path is passed */ strncpy(argv0_path, prog, MAXPATHLEN); } else { /* Use the location of the library as the progpath */ strncpy(argv0_path, buf, MAXPATHLEN); } } #endif #if HAVE_READLINK { char tmpbuffer[MAXPATHLEN+1]; int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN); while (linklen != -1) { /* It's not null terminated! */ tmpbuffer[linklen] = '\0'; if (tmpbuffer[0] == SEP) /* tmpbuffer should never be longer than MAXPATHLEN, but extra check does not hurt */ strncpy(argv0_path, tmpbuffer, MAXPATHLEN); else { /* Interpret relative to progpath */ reduce(argv0_path); joinpath(argv0_path, tmpbuffer); } linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN); } } #endif /* HAVE_READLINK */ reduce(argv0_path); /* At this point, argv0_path is guaranteed to be less than MAXPATHLEN bytes long. */ if (!(pfound = search_for_prefix(argv0_path, home))) { if (!Py_FrozenFlag) fprintf(stderr, "Could not find platform independent libraries <prefix>\n"); strncpy(prefix, PREFIX, MAXPATHLEN); joinpath(prefix, lib_python); } else reduce(prefix); strncpy(zip_path, prefix, MAXPATHLEN); zip_path[MAXPATHLEN] = '\0'; if (pfound > 0) { /* Use the reduced prefix returned by Py_GetPrefix() */ reduce(zip_path); reduce(zip_path); } else strncpy(zip_path, PREFIX, MAXPATHLEN); joinpath(zip_path, "lib/python00.zip"); bufsz = strlen(zip_path); /* Replace "00" with version */ zip_path[bufsz - 6] = VERSION[0]; zip_path[bufsz - 5] = VERSION[2]; if (!(efound = search_for_exec_prefix(argv0_path, home))) { if (!Py_FrozenFlag) fprintf(stderr, "Could not find platform dependent libraries <exec_prefix>\n"); strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN); joinpath(exec_prefix, "lib/lib-dynload"); } /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */ if ((!pfound || !efound) && !Py_FrozenFlag) fprintf(stderr, "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n"); /* Calculate size of return buffer. */ bufsz = 0; if (rtpypath) bufsz += strlen(rtpypath) + 1; prefixsz = strlen(prefix) + 1; while (1) { char *delim = strchr(defpath, DELIM); if (defpath[0] != SEP) /* Paths are relative to prefix */ bufsz += prefixsz; if (delim) bufsz += delim - defpath + 1; else { bufsz += strlen(defpath) + 1; break; } defpath = delim + 1; } bufsz += strlen(zip_path) + 1; bufsz += strlen(exec_prefix) + 1; /* This is the only malloc call in this file */ buf = PyMem_Malloc(bufsz); if (buf == NULL) { /* We can't exit, so print a warning and limp along */ fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n"); fprintf(stderr, "Using default static PYTHONPATH.\n"); module_search_path = PYTHONPATH; } else { /* Run-time value of $PYTHONPATH goes first */ if (rtpypath) { strcpy(buf, rtpypath); strcat(buf, delimiter); } else buf[0] = '\0'; /* Next is the default zip path */ strcat(buf, zip_path); strcat(buf, delimiter); /* Next goes merge of compile-time $PYTHONPATH with * dynamically located prefix. */ defpath = pythonpath; while (1) { char *delim = strchr(defpath, DELIM); if (defpath[0] != SEP) { strcat(buf, prefix); strcat(buf, separator); } if (delim) { size_t len = delim - defpath + 1; size_t end = strlen(buf) + len; strncat(buf, defpath, len); *(buf + end) = '\0'; } else { strcat(buf, defpath); break; } defpath = delim + 1; } strcat(buf, delimiter); /* Finally, on goes the directory for dynamic-load modules */ strcat(buf, exec_prefix); /* And publish the results */ module_search_path = buf; } /* Reduce prefix and exec_prefix to their essence, * e.g. /usr/local/lib/python1.5 is reduced to /usr/local. * If we're loading relative to the build directory, * return the compiled-in defaults instead. */ if (pfound > 0) { reduce(prefix); reduce(prefix); } else strncpy(prefix, PREFIX, MAXPATHLEN); if (efound > 0) { reduce(exec_prefix); reduce(exec_prefix); reduce(exec_prefix); } else strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN); }
static void calculate_path(void) { extern wchar_t *Py_GetProgramName(void); static wchar_t delimiter[2] = {DELIM, '\0'}; static wchar_t separator[2] = {SEP, '\0'}; char *_rtpypath = Py_GETENV("PYTHONPATH"); /* XXX use wide version on Windows */ wchar_t rtpypath[MAXPATHLEN+1]; wchar_t *home = Py_GetPythonHome(); char *_path = getenv("PATH"); wchar_t *path_buffer = NULL; wchar_t *path = NULL; wchar_t *prog = Py_GetProgramName(); wchar_t argv0_path[MAXPATHLEN+1]; wchar_t zip_path[MAXPATHLEN+1]; int pfound, efound; /* 1 if found; -1 if found build directory */ wchar_t *buf; size_t bufsz; size_t prefixsz; wchar_t *defpath; #ifdef WITH_NEXT_FRAMEWORK NSModule pythonModule; #endif #ifdef __APPLE__ #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4 uint32_t nsexeclength = MAXPATHLEN; #else unsigned long nsexeclength = MAXPATHLEN; #endif char execpath[MAXPATHLEN+1]; #endif wchar_t *_pythonpath, *_prefix, *_exec_prefix; _pythonpath = _Py_char2wchar(PYTHONPATH, NULL); _prefix = _Py_char2wchar(PREFIX, NULL); _exec_prefix = _Py_char2wchar(EXEC_PREFIX, NULL); if (!_pythonpath || !_prefix || !_exec_prefix) { Py_FatalError( "Unable to decode path variables in getpath.c: " "memory error"); } if (_path) { path_buffer = _Py_char2wchar(_path, NULL); path = path_buffer; } /* If there is no slash in the argv0 path, then we have to * assume python is on the user's $PATH, since there's no * other way to find a directory to start the search from. If * $PATH isn't exported, you lose. */ if (wcschr(prog, SEP)) wcsncpy(progpath, prog, MAXPATHLEN); #ifdef __APPLE__ /* On Mac OS X, if a script uses an interpreter of the form * "#!/opt/python2.3/bin/python", the kernel only passes "python" * as argv[0], which falls through to the $PATH search below. * If /opt/python2.3/bin isn't in your path, or is near the end, * this algorithm may incorrectly find /usr/bin/python. To work * around this, we can use _NSGetExecutablePath to get a better * hint of what the intended interpreter was, although this * will fail if a relative path was used. but in that case, * absolutize() should help us out below */ else if(0 == _NSGetExecutablePath(execpath, &nsexeclength) && execpath[0] == SEP) { size_t r = mbstowcs(progpath, execpath, MAXPATHLEN+1); if (r == (size_t)-1 || r > MAXPATHLEN) { /* Could not convert execpath, or it's too long. */ progpath[0] = '\0'; } } #endif /* __APPLE__ */ else if (path) { while (1) { wchar_t *delim = wcschr(path, DELIM); if (delim) { size_t len = delim - path; if (len > MAXPATHLEN) len = MAXPATHLEN; wcsncpy(progpath, path, len); *(progpath + len) = '\0'; } else wcsncpy(progpath, path, MAXPATHLEN); joinpath(progpath, prog); if (isxfile(progpath)) break; if (!delim) { progpath[0] = L'\0'; break; } path = delim + 1; } } else progpath[0] = '\0'; if (path_buffer != NULL) PyMem_Free(path_buffer); if (progpath[0] != SEP && progpath[0] != '\0') absolutize(progpath); wcsncpy(argv0_path, progpath, MAXPATHLEN); argv0_path[MAXPATHLEN] = '\0'; #ifdef WITH_NEXT_FRAMEWORK /* On Mac OS X we have a special case if we're running from a framework. ** This is because the python home should be set relative to the library, ** which is in the framework, not relative to the executable, which may ** be outside of the framework. Except when we're in the build directory... */ pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize")); /* Use dylib functions to find out where the framework was loaded from */ buf = (wchar_t *)NSLibraryNameForModule(pythonModule); if (buf != NULL) { /* We're in a framework. */ /* See if we might be in the build directory. The framework in the ** build directory is incomplete, it only has the .dylib and a few ** needed symlinks, it doesn't have the Lib directories and such. ** If we're running with the framework from the build directory we must ** be running the interpreter in the build directory, so we use the ** build-directory-specific logic to find Lib and such. */ wcsncpy(argv0_path, buf, MAXPATHLEN); reduce(argv0_path); joinpath(argv0_path, lib_python); joinpath(argv0_path, LANDMARK); if (!ismodule(argv0_path)) { /* We are in the build directory so use the name of the executable - we know that the absolute path is passed */ wcsncpy(argv0_path, progpath, MAXPATHLEN); } else { /* Use the location of the library as the progpath */ wcsncpy(argv0_path, buf, MAXPATHLEN); } } #endif #if HAVE_READLINK { wchar_t tmpbuffer[MAXPATHLEN+1]; int linklen = _Py_wreadlink(progpath, tmpbuffer, MAXPATHLEN); while (linklen != -1) { if (tmpbuffer[0] == SEP) /* tmpbuffer should never be longer than MAXPATHLEN, but extra check does not hurt */ wcsncpy(argv0_path, tmpbuffer, MAXPATHLEN); else { /* Interpret relative to progpath */ reduce(argv0_path); joinpath(argv0_path, tmpbuffer); } linklen = _Py_wreadlink(argv0_path, tmpbuffer, MAXPATHLEN); } } #endif /* HAVE_READLINK */ reduce(argv0_path); /* At this point, argv0_path is guaranteed to be less than MAXPATHLEN bytes long. */ if (!(pfound = search_for_prefix(argv0_path, home, _prefix))) { if (!Py_FrozenFlag) fprintf(stderr, "Could not find platform independent libraries <prefix>\n"); wcsncpy(prefix, _prefix, MAXPATHLEN); joinpath(prefix, lib_python); } else reduce(prefix); wcsncpy(zip_path, prefix, MAXPATHLEN); zip_path[MAXPATHLEN] = L'\0'; if (pfound > 0) { /* Use the reduced prefix returned by Py_GetPrefix() */ reduce(zip_path); reduce(zip_path); } else wcsncpy(zip_path, _prefix, MAXPATHLEN); joinpath(zip_path, L"lib/python00.zip"); bufsz = wcslen(zip_path); /* Replace "00" with version */ zip_path[bufsz - 6] = VERSION[0]; zip_path[bufsz - 5] = VERSION[2]; if (!(efound = search_for_exec_prefix(argv0_path, home, _exec_prefix))) { if (!Py_FrozenFlag) fprintf(stderr, "Could not find platform dependent libraries <exec_prefix>\n"); wcsncpy(exec_prefix, _exec_prefix, MAXPATHLEN); joinpath(exec_prefix, L"lib/lib-dynload"); } /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */ if ((!pfound || !efound) && !Py_FrozenFlag) fprintf(stderr, "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n"); /* Calculate size of return buffer. */ bufsz = 0; if (_rtpypath) { size_t s = mbstowcs(rtpypath, _rtpypath, sizeof(rtpypath)/sizeof(wchar_t)); if (s == (size_t)-1 || s >=sizeof(rtpypath)) /* XXX deal with errors more gracefully */ _rtpypath = NULL; if (_rtpypath) bufsz += wcslen(rtpypath) + 1; } defpath = _pythonpath; prefixsz = wcslen(prefix) + 1; while (1) { wchar_t *delim = wcschr(defpath, DELIM); if (defpath[0] != SEP) /* Paths are relative to prefix */ bufsz += prefixsz; if (delim) bufsz += delim - defpath + 1; else { bufsz += wcslen(defpath) + 1; break; } defpath = delim + 1; } bufsz += wcslen(zip_path) + 1; bufsz += wcslen(exec_prefix) + 1; /* This is the only malloc call in this file */ buf = (wchar_t *)PyMem_Malloc(bufsz*sizeof(wchar_t)); if (buf == NULL) { /* We can't exit, so print a warning and limp along */ fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n"); fprintf(stderr, "Using default static PYTHONPATH.\n"); module_search_path = L"" PYTHONPATH; } else { /* Run-time value of $PYTHONPATH goes first */ if (_rtpypath) { wcscpy(buf, rtpypath); wcscat(buf, delimiter); } else buf[0] = '\0'; /* Next is the default zip path */ wcscat(buf, zip_path); wcscat(buf, delimiter); /* Next goes merge of compile-time $PYTHONPATH with * dynamically located prefix. */ defpath = _pythonpath; while (1) { wchar_t *delim = wcschr(defpath, DELIM); if (defpath[0] != SEP) { wcscat(buf, prefix); wcscat(buf, separator); } if (delim) { size_t len = delim - defpath + 1; size_t end = wcslen(buf) + len; wcsncat(buf, defpath, len); *(buf + end) = '\0'; } else { wcscat(buf, defpath); break; } defpath = delim + 1; } wcscat(buf, delimiter); /* Finally, on goes the directory for dynamic-load modules */ wcscat(buf, exec_prefix); /* And publish the results */ module_search_path = buf; } /* Reduce prefix and exec_prefix to their essence, * e.g. /usr/local/lib/python1.5 is reduced to /usr/local. * If we're loading relative to the build directory, * return the compiled-in defaults instead. */ if (pfound > 0) { reduce(prefix); reduce(prefix); /* The prefix is the root directory, but reduce() chopped * off the "/". */ if (!prefix[0]) wcscpy(prefix, separator); } else wcsncpy(prefix, _prefix, MAXPATHLEN); if (efound > 0) { reduce(exec_prefix); reduce(exec_prefix); reduce(exec_prefix); if (!exec_prefix[0]) wcscpy(exec_prefix, separator); } else wcsncpy(exec_prefix, _exec_prefix, MAXPATHLEN); PyMem_Free(_pythonpath); PyMem_Free(_prefix); PyMem_Free(_exec_prefix); }
dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname, const char *pathname, FILE *fp) { dl_funcptr p = NULL; char funcname[258]; NSObjectFileImageReturnCode rc; NSObjectFileImage image; NSModule newModule; NSSymbol theSym; const char *errString; char errBuf[512]; PyOS_snprintf(funcname, sizeof(funcname), "_init%.200s", shortname); #ifdef USE_DYLD_GLOBAL_NAMESPACE if (NSIsSymbolNameDefined(funcname)) { theSym = NSLookupAndBindSymbol(funcname); p = (dl_funcptr)NSAddressOfSymbol(theSym); return p; } #endif rc = NSCreateObjectFileImageFromFile(pathname, &image); switch(rc) { default: case NSObjectFileImageFailure: case NSObjectFileImageFormat: /* for these a message is printed on stderr by dyld */ errString = "Can't create object file image"; break; case NSObjectFileImageSuccess: errString = NULL; break; case NSObjectFileImageInappropriateFile: errString = "Inappropriate file type for dynamic loading"; break; case NSObjectFileImageArch: errString = "Wrong CPU type in object file"; break; case NSObjectFileImageAccess: errString = "Can't read object file (no access)"; break; } if (errString == NULL) { newModule = NSLinkModule(image, pathname, LINKOPTIONS); if (newModule == NULL) { int errNo; const char *fileName, *moreErrorStr; NSLinkEditErrors c; NSLinkEditError( &c, &errNo, &fileName, &moreErrorStr ); PyOS_snprintf(errBuf, 512, "Failure linking new module: %s: %s", fileName, moreErrorStr); errString = errBuf; } } if (errString != NULL) { PyErr_SetString(PyExc_ImportError, errString); return NULL; } #ifdef USE_DYLD_GLOBAL_NAMESPACE if (!NSIsSymbolNameDefined(funcname)) { /* UnlinkModule() isn't implemented in current versions, but calling it does no harm */ NSUnLinkModule(newModule, FALSE); PyErr_Format(PyExc_ImportError, "Loaded module does not contain symbol %.200s", funcname); return NULL; } theSym = NSLookupAndBindSymbol(funcname); #else theSym = NSLookupSymbolInModule(newModule, funcname); if ( theSym == NULL ) { NSUnLinkModule(newModule, FALSE); PyErr_Format(PyExc_ImportError, "Loaded module does not contain symbol %.200s", funcname); return NULL; } #endif p = (dl_funcptr)NSAddressOfSymbol(theSym); return p; }
wxString GetApplicationPath() { static bool found = false; static wxString path; if (!found) { /* Windows */ #ifdef __WXMSW__ wxChar buf[512] = wxT(""); GetModuleFileName(NULL, buf, 511); path = wxString(buf, wxConvUTF8); /* UNIX & MAC*/ #else wxString argv0; # ifdef __WXMAC__ if (NSIsSymbolNameDefined("__NSGetExecutablePath")) { char buf[512]; size_t bufLen = 512; buf[0] = 0; ((NSGetExecutablePathProcPtr) NSAddressOfSymbol(NSLookupAndBindSymbol("__NSGetExecutablePath")))(buf, &bufLen); wxString strBuf = wxString(); size_t actualBuflen = strlen(buf); if (actualBuflen > 0) { // FIXME: we *assume* that the NS stuff returns utf-8 encoded strings path = wxString(buf, wxConvUTF8); found=true; return path; } } # endif argv0 = wxTheApp->argv[0]; /* check absolute path */ if (wxIsAbsolutePath(argv0)) { path = argv0; } else { /* check relative path */ wxString fname = wxGetCwd() + wxFILE_SEP_PATH + argv0; if (wxFileExists(fname)) { path = fname; } else { /* find on PATH */ wxPathList pathlist; pathlist.AddEnvList(wxT("PATH")); path = pathlist.FindAbsoluteValidPath(argv0); } } wxFileName filename(path); filename.Normalize(); path = filename.GetFullPath(); #endif found = true; } return path; }