IFXOGLPROC CIFXOpenGLOS::GetOpenGLFunc(const char* szFuncName)
{
	char newFuncName[_MAXIMUM_FUNCNAME_LENGTH+1];
    int size = (U32)strlen(szFuncName);

    if (size <= _MAXIMUM_FUNCNAME_LENGTH)
    {
        strcpy(newFuncName, "_");
        strcat(newFuncName, szFuncName);
    }
    else
    {
        IFXTRACE_GENERIC(L"%s:%i \"%s%s\" FuncName lenth is more then %i\n",__FILE__, __LINE__, szFuncName, _MAXIMUM_FUNCNAME_LENGTH);
    }

	IFXOGLPROC procAddress = (IFXOGLPROC)NSAddressOfSymbol(NSLookupSymbolInImage(s_phAGL, newFuncName,
															NSLOOKUPSYMBOLINIMAGE_OPTION_BIND));
	if (NULL == procAddress)
	{
		procAddress = (IFXOGLPROC)NSAddressOfSymbol(NSLookupSymbolInImage(s_phOpenGL, newFuncName,
															NSLOOKUPSYMBOLINIMAGE_OPTION_BIND));
	}

	if (NULL == procAddress)
	{
	IFXTRACE_GENERIC(L"GL function [%s] is not allowed\n", szFuncName);
	}

	return procAddress;
}
void*	CASharedLibrary::GetRoutineAddressIfLibraryLoaded(const char* inRoutineName, const char* /*inLibraryName*/, const char* inLibraryPath)
{
	void* theRoutine = 0;
		
#if	CASharedLibrary_Use_DYLD
	const struct mach_header* theImage = NSAddImage(inLibraryPath, NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED);
	if(theImage != 0)
	{
		NSSymbol theSymbol = NSLookupSymbolInImage(theImage, inRoutineName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND);
		if(theSymbol != 0)
		{
			theRoutine = NSAddressOfSymbol(theSymbol);
		}
	}
#else
	void* theImage = dlopen(inLibraryPath, RTLD_LAZY | RTLD_NOLOAD);
	if(theImage != NULL)
	{
		//	we assume that all routine names passed here have a leading underscore which gets shaved
		//	off when passed to dlsym
		theRoutine = dlsym(theImage, &(inRoutineName[1]));
	}
#endif

	return theRoutine;
}
static void *GetSymbol(void *handle, const char *symbol)
{
	NSSymbol sym;

	/* We have to use a different lookup approach for images and modules */
	if((((struct mach_header *)handle)->magic == MH_MAGIC) ||
	   (((struct mach_header *)handle)->magic == MH_CIGAM))
	{
		if(NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol))
		{
			sym = NSLookupSymbolInImage((struct mach_header *)handle, symbol,
						NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
						NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
		}
		else
		{
			sym = 0;
		}
	}
	else
	{
		sym = NSLookupSymbolInModule((NSModule)handle, symbol);
	}

	/* Did we find the symbol? */
	if(sym == 0)
	{
		return 0;
	}

	/* Convert the symbol into the address that we require */
	return (void *)NSAddressOfSymbol(sym);
}
Exemple #4
0
/* 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);
}
Exemple #5
0
void* NSGLGetProcAddress (const GLubyte *name)
{
  static const struct mach_header* image = NULL;
  NSSymbol symbol;
  char* symbolName;
  if (NULL == image)
  {
#ifdef GLEW_REGAL
    image = NSAddImage("libRegal.dylib", NSADDIMAGE_OPTION_RETURN_ON_ERROR);
#else
    image = NSAddImage("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", NSADDIMAGE_OPTION_RETURN_ON_ERROR);
#endif
  }
  /* 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); */
  symbol = image ? NSLookupSymbolInImage(image, symbolName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR) : NULL;
  free(symbolName);
  if( symbol ) return NSAddressOfSymbol(symbol);
#ifdef GLEW_APPLE_GLX
  return dlGetProcAddress( name ); // try next for glx symbols
#else
  return NULL;
#endif
}
Exemple #6
0
static void notify(const struct mach_header *mh, intptr_t vmaddr_slide) 
{
// NSLookupSymbolInImage is only available on Mac OS X - not iPhone OS
#if __MAC_OS_X_VERSION_MIN_REQUIRED
	//fprintf(stderr, "mh=%p\n", mh);
	NSLookupSymbolInImage(mh, "_bar", NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_FULLY | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
#endif
}
void *getFunctionPointer(const struct mach_header *header, const char *functionName)
{
    NSSymbol symbol = NSLookupSymbolInImage(header, functionName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND);
    if (symbol!=NULL) {
        return NSAddressOfSymbol(symbol);
    }
    return 0;
}
Exemple #8
0
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;
}
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);
}
Exemple #10
0
	void (*priv::SO_LOADER::getProcAddr(const std::string &name))()
	{
		NSSymbol symbol;
		char *symbolName;
		
		// prepend a '_' for the Unix C symbol mangling convention
		std::string name2 = '_'+name;
		symbol = image ? NSLookupSymbolInImage((const mach_header *)image, 
											   name2.c_str(), 
											   NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR) : nullptr;
		
		return symbol ? NSAddressOfSymbol(symbol) : nullptr;
	}
Exemple #11
0
void*	CASharedLibrary::GetRoutineAddressIfLibraryLoaded(const char* inRoutineName, const char* /*inLibraryName*/, const char* inLibraryPath)
{
	void* theRoutine = 0;
	
	const struct mach_header* theImage = NSAddImage(inLibraryPath, NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED);
	if(theImage != 0)
	{
		NSSymbol theSymbol = NSLookupSymbolInImage(theImage, inRoutineName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND);
		if(theSymbol != 0)
		{
			theRoutine = NSAddressOfSymbol(theSymbol);
		}
	}

	return theRoutine;
}
Exemple #12
0
/* 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);
}
Exemple #13
0
void* NSGLGetProcAddress (const GLubyte *name)
{
  static const struct mach_header* image = NULL;
  NSSymbol symbol;
  char* symbolName;
  if (NULL == image)
  {
    image = NSAddImage("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", NSADDIMAGE_OPTION_RETURN_ON_ERROR);
  }
  /* 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); */
  symbol = image ? NSLookupSymbolInImage(image, symbolName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR) : NULL;
  free(symbolName);
  return symbol ? NSAddressOfSymbol(symbol) : NULL;
}
Exemple #14
0
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);
}
static void *_CFBundleDYLDGetSymbolByNameWithSearch(CFBundleRef bundle, CFStringRef symbolName, Boolean globalSearch) {
    void *result = NULL;
    char buff[1026];
    NSSymbol symbol = NULL;
    
    buff[0] = '_';
    if (CFStringGetCString(symbolName, &(buff[1]), 1024, kCFStringEncodingUTF8)) {
        if (bundle->_moduleCookie) {
            symbol = NSLookupSymbolInModule((NSModule)(bundle->_moduleCookie), buff);
        } else if (bundle->_imageCookie) {
            symbol = NSLookupSymbolInImage(bundle->_imageCookie, buff, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND|NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
        } 
        if (!symbol && !bundle->_moduleCookie && (!bundle->_imageCookie || globalSearch)) {
            char hintBuff[1026];
            CFStringRef executableName = _CFBundleCopyExecutableName(bundle, NULL, NULL);
            hintBuff[0] = '\0';
            if (executableName) {
                if (!CFStringGetCString(executableName, hintBuff, 1024, kCFStringEncodingUTF8)) hintBuff[0] = '\0';
                CFRelease(executableName);
            }
            // Nowdays, NSIsSymbolNameDefinedWithHint() and NSLookupAndBindSymbolWithHint()
            // are identical, except the first just returns a bool, so checking with the
            // Is function first just causes a redundant lookup.
            // This returns NULL on failure.
            symbol = NSLookupAndBindSymbolWithHint(buff, hintBuff);
        }
        if (symbol) result = NSAddressOfSymbol(symbol);
#if defined(DEBUG)
        if (!result) CFLog(__kCFLogBundle, CFSTR("dyld cannot find symbol %@ in %@"), symbolName, bundle);
#endif /* DEBUG */
#if LOG_BUNDLE_LOAD
        printf("bundle %p handle %p module %p image %p dyld returns symbol %p for %s\n", bundle, bundle->_handleCookie, bundle->_moduleCookie, bundle->_imageCookie, result, buff + 1);
#endif /* LOG_BUNDLE_LOAD */
    }
    return result;
}
Exemple #17
0
GdkGLProc
gdk_gl_get_proc_address (const char *proc_name)
{
  typedef GdkGLProc (*__glXGetProcAddressProc) (const GLubyte *);
  static __glXGetProcAddressProc glx_get_proc_address = (__glXGetProcAddressProc) -1;
  const char *image_name;
  static const struct mach_header *libgl_image = NULL;
  static const struct mach_header *libglu_image = NULL;
  NSSymbol symbol;
  char *symbol_name;
  GdkGLProc proc_address;

  GDK_GL_NOTE_FUNC ();

  if (strncmp ("glu", proc_name, 3) != 0)
    {
      /* libGL */

      if (libgl_image == NULL)
        {
          image_name = g_getenv ("GDK_GL_LIBGL_PATH");
          if (image_name == NULL)
            image_name = _GDK_GL_LIBGL_PATH;

          GDK_GL_NOTE (MISC, g_message (" - Add Mach-O image %s", image_name));

          libgl_image = NSAddImage (image_name, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
          if (libgl_image == NULL)
            {
              g_warning ("Cannot add Mach-O image %s", image_name);
              return NULL;
            }
        }

      if (glx_get_proc_address == (__glXGetProcAddressProc) -1)
        {
          /*
           * Look up glXGetProcAddress () function.
           */

          symbol = NSLookupSymbolInImage (libgl_image,
                                          "_glXGetProcAddress",
                                          NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                          NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
          if (symbol == NULL)
            {
              symbol = NSLookupSymbolInImage (libgl_image,
                                              "_glXGetProcAddressARB",
                                              NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                              NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
              if (symbol == NULL)
                {
                  symbol = NSLookupSymbolInImage (libgl_image,
                                                  "_glXGetProcAddressEXT",
                                                  NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                                  NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
                }
            }
          GDK_GL_NOTE (MISC, g_message (" - glXGetProcAddress () - %s",
                                        symbol ? "supported" : "not supported"));
          if (symbol != NULL)
            glx_get_proc_address = NSAddressOfSymbol (symbol);
          else
            glx_get_proc_address = NULL;
        }

      /* Try glXGetProcAddress () */

      if (glx_get_proc_address != NULL)
        {
          proc_address = glx_get_proc_address (proc_name);
          GDK_GL_NOTE (IMPL, g_message (" ** glXGetProcAddress () - %s",
                                        proc_address ? "succeeded" : "failed"));
          if (proc_address != NULL)
            return proc_address;
        }

      /* Try Mach-O dyld */

      symbol_name = g_strconcat ("_", proc_name, NULL);

      symbol = NSLookupSymbolInImage (libgl_image,
                                      symbol_name,
                                      NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                      NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
      GDK_GL_NOTE (MISC, g_message (" - NSLookupSymbolInImage () - %s",
                                    symbol ? "succeeded" : "failed"));

      g_free (symbol_name);

      if (symbol != NULL)
        return NSAddressOfSymbol (symbol);
    }
  else
    {
      /* libGLU */

      if (libglu_image == NULL)
        {
          image_name = g_getenv ("GDK_GL_LIBGLU_PATH");
          if (image_name == NULL)
            image_name = _GDK_GL_LIBGLU_PATH;

          GDK_GL_NOTE (MISC, g_message (" - Add Mach-O image %s", image_name));

          libglu_image = NSAddImage (image_name, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
          if (libglu_image == NULL)
            {
              g_warning ("Cannot add Mach-O image %s", image_name);
              return NULL;
            }
        }

      symbol_name = g_strconcat ("_", proc_name, NULL);

      symbol = NSLookupSymbolInImage (libglu_image,
                                      symbol_name,
                                      NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                      NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
      GDK_GL_NOTE (MISC, g_message (" - NSLookupSymbolInImage () - %s",
                                    symbol ? "succeeded" : "failed"));

      g_free (symbol_name);

      if (symbol != NULL)
        return NSAddressOfSymbol (symbol);
    }

  return NULL;
}
Exemple #18
0
void *
cc_dl_sym(cc_libhandle handle, const char * symbolname)
{
  void * ptr = NULL;  

#ifdef HAVE_DL_LIB

  if ((handle == NULL) || (handle->nativehnd == NULL)) return NULL;
  ptr = dlsym((void *)handle->nativehnd, symbolname);

  if (cc_dl_debugging()) {
    const char * e = dlerror();
    if (e) {
      cc_debugerror_post("cc_dl_sym", "dlsym(\"%s\", \"%s\") failed with: '%s'",
                         handle->libname.getString(), symbolname, e);
    }
  }

#elif defined (HAVE_DYLD_RUNTIME_BINDING) 

  /* Note: The dlopen() version returns NULL here if handle or
     handle->nativehnd are NULL, but we do not need a handle for
     symbol lookup on Mac OS X - if we have one, it makes the lookup
     faster, but that's all, so we can get away with having no valid
     handle. */

  NSSymbol symbol = NULL;
  char * mangledname;
  NSLinkEditErrors c;
  int e;
  const char * file;
  const char * errstr;

  if (cc_dl_debugging()) {
    cc_debugerror_postinfo("cc_dl_sym", "Looking up symbol %s", 
                       symbolname);
  }

  mangledname = malloc(strlen(symbolname) + 2);
  strcpy(mangledname + 1, symbolname);
  mangledname[0] = '_';

  if (handle && handle->nativehnd) {
    if (NSIsSymbolNameDefinedInImage(handle->nativehnd, mangledname))
      symbol = NSLookupSymbolInImage(handle->nativehnd, mangledname,
               NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
               NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
  } 

  /* If we did not specifically load the library ourselves
     (handle->nativehnd being NULL), or if the symbol could not be
     found in the library, let's try if we can find it in any of the
     loaded libs. */

  if (!symbol && NSIsSymbolNameDefined(mangledname)) {
    symbol = NSLookupAndBindSymbol(mangledname);
  } 

  if (cc_dl_debugging()) {
    if (symbol == NULL) {
      NSLinkEditError(&c, &e, &file, &errstr);
      cc_debugerror_post("cc_dl_sym", "symbol %s not found: %s", 
                         symbolname, errstr);
    }
  }

  free (mangledname);
  ptr = symbol ? NSAddressOfSymbol(symbol) : NULL;  
  
#elif defined (HAVE_WINDLL_RUNTIME_BINDING)

  if ((handle == NULL) || (handle->nativehnd == NULL)) return NULL;
 
  ptr = dl_internal::cstyle_cast<void *>(GetProcAddress((HINSTANCE) handle->nativehnd, symbolname));

  if (cc_dl_debugging() && (ptr == NULL)) {
    cc_string funcstr;
    cc_string_construct(&funcstr);
    cc_string_sprintf(&funcstr, "GetProcAddress(\"%s\", \"%s\")", handle->libname.getString(), symbolname);
    cc_win32_print_error("cc_dl_sym", cc_string_get_text(&funcstr), GetLastError());
    cc_string_clean(&funcstr);
  }

#elif defined (HAVE_DLD_LIB)

  {
    int retval = shl_findsym((shl_t *)(&handle->nativehnd), symbolname, TYPE_UNDEFINED, &ptr);

    if (cc_dl_debugging() && (retval == -1)) {
      const char * e = strerror(errno);
      cc_debugerror_post("cc_dl_sym", "shl_findsym(\"%s\", \"%s\", ...) failed with: '%s'",
                         handle->libname.getString(), symbolname, e);
    }
  }

#endif

  return ptr;
}
Exemple #19
0
GdkGLProc
_gdk_x11_gl_get_proc_address (const char *proc_name)
{
#ifdef __APPLE__

#define _GDK_GL_LIBGL_PATH  "/usr/X11R6/lib/libGL.1.dylib"
#define _GDK_GL_LIBGLU_PATH "/usr/X11R6/lib/libGLU.1.dylib"

    typedef GdkGLProc (*__glXGetProcAddressProc) (const GLubyte *);
    static __glXGetProcAddressProc glx_get_proc_address = (__glXGetProcAddressProc) -1;
    const char *image_name;
    static const struct mach_header *libgl_image = NULL;
    static const struct mach_header *libglu_image = NULL;
    NSSymbol symbol;
    char *symbol_name;
    GdkGLProc proc_address;

    GDK_GL_NOTE_FUNC ();

    if (strncmp ("glu", proc_name, 3) != 0)
    {
        /* libGL */

        if (libgl_image == NULL)
        {
            image_name = g_getenv ("GDK_GL_LIBGL_PATH");
            if (image_name == NULL)
                image_name = _GDK_GL_LIBGL_PATH;

            GDK_GL_NOTE (MISC, g_message (" - Add Mach-O image %s", image_name));

            libgl_image = NSAddImage (image_name, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
            if (libgl_image == NULL)
            {
                g_warning ("Cannot add Mach-O image %s", image_name);
                return NULL;
            }
        }

        if (glx_get_proc_address == (__glXGetProcAddressProc) -1)
        {
            /*
             * Look up glXGetProcAddress () function.
             */

            symbol = NSLookupSymbolInImage (libgl_image,
                                            "_glXGetProcAddress",
                                            NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                            NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
            if (symbol == NULL)
            {
                symbol = NSLookupSymbolInImage (libgl_image,
                                                "_glXGetProcAddressARB",
                                                NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                                NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
                if (symbol == NULL)
                {
                    symbol = NSLookupSymbolInImage (libgl_image,
                                                    "_glXGetProcAddressEXT",
                                                    NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                                    NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
                }
            }
            GDK_GL_NOTE (MISC, g_message (" - glXGetProcAddress () - %s",
                                          symbol ? "supported" : "not supported"));
            if (symbol != NULL)
                glx_get_proc_address = NSAddressOfSymbol (symbol);
            else
                glx_get_proc_address = NULL;
        }

        /* Try glXGetProcAddress () */

        if (glx_get_proc_address != NULL)
        {
            proc_address = glx_get_proc_address ((unsigned char *) proc_name);
            GDK_GL_NOTE (IMPL, g_message (" ** glXGetProcAddress () - %s",
                                          proc_address ? "succeeded" : "failed"));
            if (proc_address != NULL)
                return proc_address;
        }

        /* Try Mach-O dyld */

        symbol_name = g_strconcat ("_", proc_name, NULL);

        symbol = NSLookupSymbolInImage (libgl_image,
                                        symbol_name,
                                        NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                        NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
        GDK_GL_NOTE (MISC, g_message (" - NSLookupSymbolInImage () - %s",
                                      symbol ? "succeeded" : "failed"));

        g_free (symbol_name);

        if (symbol != NULL)
            return NSAddressOfSymbol (symbol);
    }
    else
    {
        /* libGLU */

        if (libglu_image == NULL)
        {
            image_name = g_getenv ("GDK_GL_LIBGLU_PATH");
            if (image_name == NULL)
                image_name = _GDK_GL_LIBGLU_PATH;

            GDK_GL_NOTE (MISC, g_message (" - Add Mach-O image %s", image_name));

            libglu_image = NSAddImage (image_name, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
            if (libglu_image == NULL)
            {
                g_warning ("Cannot add Mach-O image %s", image_name);
                return NULL;
            }
        }

        symbol_name = g_strconcat ("_", proc_name, NULL);

        symbol = NSLookupSymbolInImage (libglu_image,
                                        symbol_name,
                                        NSLOOKUPSYMBOLINIMAGE_OPTION_BIND |
                                        NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
        GDK_GL_NOTE (MISC, g_message (" - NSLookupSymbolInImage () - %s",
                                      symbol ? "succeeded" : "failed"));

        g_free (symbol_name);

        if (symbol != NULL)
            return NSAddressOfSymbol (symbol);
    }

    return NULL;

#else  /* __APPLE__ */

    typedef GdkGLProc (*__glXGetProcAddressProc) (const GLubyte *);
    static __glXGetProcAddressProc glx_get_proc_address = (__glXGetProcAddressProc) -1;
    gchar *file_name;
    GModule *module;
    GdkGLProc proc_address = NULL;

    GDK_GL_NOTE_FUNC ();

    if (strncmp ("glu", proc_name, 3) != 0)
    {
        if (glx_get_proc_address == (__glXGetProcAddressProc) -1)
        {
            /*
             * Look up glXGetProcAddress () function.
             */

            file_name = g_module_build_path (NULL, "GL");
            GDK_GL_NOTE (MISC, g_message (" - Open %s", file_name));
            module = g_module_open (file_name, G_MODULE_BIND_LAZY);
            g_free (file_name);

            if (module != NULL)
            {
                g_module_symbol (module, "glXGetProcAddress",
                                 (gpointer) &glx_get_proc_address);
                if (glx_get_proc_address == NULL)
                {
                    g_module_symbol (module, "glXGetProcAddressARB",
                                     (gpointer) &glx_get_proc_address);
                    if (glx_get_proc_address == NULL)
                    {
                        g_module_symbol (module, "glXGetProcAddressEXT",
                                         (gpointer) &glx_get_proc_address);
                    }
                }
                GDK_GL_NOTE (MISC, g_message (" - glXGetProcAddress () - %s",
                                              glx_get_proc_address ? "supported" : "not supported"));
                g_module_close (module);
            }
            else
            {
                g_warning ("Cannot open %s", file_name);
                glx_get_proc_address = NULL;
                return NULL;
            }
        }

        /* Try glXGetProcAddress () */

        if (glx_get_proc_address != NULL)
        {
            proc_address = glx_get_proc_address ((unsigned char *) proc_name);
            GDK_GL_NOTE (IMPL, g_message (" ** glXGetProcAddress () - %s",
                                          proc_address ? "succeeded" : "failed"));
            if (proc_address != NULL)
                return proc_address;
        }

        /* Try g_module_symbol () */

        /* libGL */
        file_name = g_module_build_path (NULL, "GL");
        GDK_GL_NOTE (MISC, g_message (" - Open %s", file_name));
        module = g_module_open (file_name, G_MODULE_BIND_LAZY);
        g_free (file_name);

        if (module != NULL)
        {
            g_module_symbol (module, proc_name, (gpointer) &proc_address);
            GDK_GL_NOTE (MISC, g_message (" - g_module_symbol () - %s",
                                          proc_address ? "succeeded" : "failed"));
            g_module_close (module);
        }
        else
        {
            g_warning ("Cannot open %s", file_name);
        }

        if (proc_address == NULL)
        {
            /* libGLcore */
            file_name = g_module_build_path (NULL, "GLcore");
            GDK_GL_NOTE (MISC, g_message (" - Open %s", file_name));
            module = g_module_open (file_name, G_MODULE_BIND_LAZY);
            g_free (file_name);

            if (module != NULL)
            {
                g_module_symbol (module, proc_name, (gpointer) &proc_address);
                GDK_GL_NOTE (MISC, g_message (" - g_module_symbol () - %s",
                                              proc_address ? "succeeded" : "failed"));
                g_module_close (module);
            }
        }
    }
    else
    {
        /* libGLU */
        file_name = g_module_build_path (NULL, "GLU");
        GDK_GL_NOTE (MISC, g_message (" - Open %s", file_name));
        module = g_module_open (file_name, G_MODULE_BIND_LAZY);
        g_free (file_name);

        if (module != NULL)
        {
            g_module_symbol (module, proc_name, (gpointer) &proc_address);
            GDK_GL_NOTE (MISC, g_message (" - g_module_symbol () - %s",
                                          proc_address ? "succeeded" : "failed"));
            g_module_close (module);
        }
        else
        {
            g_warning ("Cannot open %s", file_name);
        }
    }

    return proc_address;

#endif /* __APPLE__ */
}
Exemple #20
0
MODULE_SCOPE Tcl_PackageInitProc *
TclpFindSymbol(
    Tcl_Interp *interp,		/* For error reporting. */
    Tcl_LoadHandle loadHandle,	/* Handle from TclpDlopen. */
    CONST char *symbol)		/* Symbol name to look up. */
{
    Tcl_DyldLoadHandle *dyldLoadHandle = (Tcl_DyldLoadHandle *) loadHandle;
    Tcl_PackageInitProc *proc = NULL;
    const char *errMsg = NULL;
    Tcl_DString ds;
    const char *native;

    native = Tcl_UtfToExternalDString(NULL, symbol, -1, &ds);
#if TCL_DYLD_USE_DLFCN
    if (dyldLoadHandle->dlHandle) {
	proc = dlsym(dyldLoadHandle->dlHandle, native);
	if (proc) {
	    TclLoadDbgMsg("dlsym() successful");
	} else {
	    errMsg = dlerror();
	    TclLoadDbgMsg("dlsym() failed: %s", errMsg);
	}
    } else
#endif /* TCL_DYLD_USE_DLFCN */
    {
#if TCL_DYLD_USE_NSMODULE || defined(TCL_LOAD_FROM_MEMORY)
	NSSymbol nsSymbol = NULL;
	Tcl_DString newName;

	/*
	 * dyld adds an underscore to the beginning of symbol names.
	 */

	Tcl_DStringInit(&newName);
	Tcl_DStringAppend(&newName, "_", 1);
	native = Tcl_DStringAppend(&newName, native, -1);
	if (dyldLoadHandle->dyldLibHeader) {
	    nsSymbol = NSLookupSymbolInImage(dyldLoadHandle->dyldLibHeader,
		    native, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_NOW |
		    NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
	    if (nsSymbol) {
		TclLoadDbgMsg("NSLookupSymbolInImage() successful");
#ifdef DYLD_SUPPORTS_DYLIB_UNLOADING
		/*
		 * Until dyld supports unloading of MY_DYLIB binaries, the
		 * following is not needed.
		 */

		NSModule module = NSModuleForSymbol(nsSymbol);
		Tcl_DyldModuleHandle *modulePtr = dyldLoadHandle->modulePtr;

		while (modulePtr != NULL) {
		    if (module == modulePtr->module) {
			break;
		    }
		    modulePtr = modulePtr->nextPtr;
		}
		if (modulePtr == NULL) {
		    modulePtr = (Tcl_DyldModuleHandle *)
			    ckalloc(sizeof(Tcl_DyldModuleHandle));
		    modulePtr->module = module;
		    modulePtr->nextPtr = dyldLoadHandle->modulePtr;
		    dyldLoadHandle->modulePtr = modulePtr;
		}
#endif /* DYLD_SUPPORTS_DYLIB_UNLOADING */
	    } else {
		NSLinkEditErrors editError;
		int errorNumber;
		const char *errorName;

		NSLinkEditError(&editError, &errorNumber, &errorName, &errMsg);
		TclLoadDbgMsg("NSLookupSymbolInImage() failed: %s", errMsg);
	    }
	} else if (dyldLoadHandle->modulePtr) {
	    nsSymbol = NSLookupSymbolInModule(
		    dyldLoadHandle->modulePtr->module, native);
	    if (nsSymbol) {
		TclLoadDbgMsg("NSLookupSymbolInModule() successful");
	    } else {
		TclLoadDbgMsg("NSLookupSymbolInModule() failed");
	    }
	}
	if (nsSymbol) {
	    proc = NSAddressOfSymbol(nsSymbol);
	    if (proc) {
		TclLoadDbgMsg("NSAddressOfSymbol() successful");
	    } else {
		TclLoadDbgMsg("NSAddressOfSymbol() failed");
	    }
	}
	Tcl_DStringFree(&newName);
#endif /* TCL_DYLD_USE_NSMODULE */
    }
    Tcl_DStringFree(&ds);
    if (errMsg) {
	Tcl_AppendResult(interp, errMsg, NULL);
    }
    return proc;
}
Exemple #21
0
const void*
dlopen(const char* filename, int flags)
{
    static char has_callback = 0;
    if (!has_callback) {
        _dyld_register_func_for_add_image(dlshim_image_callback);
    }
    if (!filename) {
        return &dl_self;
    } else {
        const struct mach_header* img = NULL;
        if (!img)
            img = NSAddImage(filename, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
        if (!img)
            img = NSAddImage(filename,
                             NSADDIMAGE_OPTION_RETURN_ON_ERROR |
                             NSADDIMAGE_OPTION_WITH_SEARCHING);
        if (!img) {
            NSObjectFileImage fileImage;
            callback_count = 0;
            last_header = NULL;
            if (NSCreateObjectFileImageFromFile(filename, &fileImage)
                == NSObjectFileImageSuccess) {
                NSLinkModule(fileImage, filename,
                             NSLINKMODULE_OPTION_BINDNOW |
                             ((flags & RTLD_GLOBAL)?NSLINKMODULE_OPTION_PRIVATE:0) |
                             NSLINKMODULE_OPTION_RETURN_ON_ERROR);
                if (callback_count && last_header)
                    img = last_header;
            }
        }
        if (!img) {
            NSObjectFileImage fileImage;
            int i, maxi;
            const char* prefixfilename;
            maxi = lib_path_count();
            for (i = 0; i < maxi && !img; i++) {
                prefixfilename = lib_path_prefixify(i, filename);
                callback_count = 0;
                last_header = NULL;
                if (NSCreateObjectFileImageFromFile(prefixfilename, &fileImage)
                    == NSObjectFileImageSuccess) {
                    NSLinkModule(fileImage, filename,
                                 NSLINKMODULE_OPTION_BINDNOW |
                                 ((flags & RTLD_GLOBAL)?NSLINKMODULE_OPTION_PRIVATE:0) |
                                 NSLINKMODULE_OPTION_RETURN_ON_ERROR);
                    if (callback_count && last_header)
                        img = last_header;
                }
            }
        }
        if (img) {
            if (flags & RTLD_NOW) {
                NSLookupSymbolInImage(img, "",
                                      NSLOOKUPSYMBOLINIMAGE_OPTION_BIND_FULLY |
                                      NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
            }
            if (NSIsSymbolNameDefinedInImage(img, "__init")) {
                NSSymbol initsymbol;
                void (*initfunc) (void);
                initsymbol = NSLookupSymbolInImage(img, "__init", 0);
                initfunc = NSAddressOfSymbol(initsymbol);
                initfunc();
            }
        } else
            last_error = DLOPEN_ERROR;
        return img;
    }
}
Exemple #22
0
static void * dlsymIntern(struct dlstatus * dls, const char *symbol,int canSetError)
{
    NSSymbol * nssym = 0;
    /* If it is a module - use NSLookupSymbolInModule */
    if (dls->module != MAGIC_DYLIB_MOD) 
    {
    
        nssym = NSLookupSymbolInModule(dls->module, symbol);
        if (!nssym && canSetError)
            error("unable to find symbol \"%s\"", symbol);
    }        
    else 
    {
        if (dls->lib != NULL) 
        {
            /* dylib, use NSIsSymbolNameDefinedInImage */
            if (NSIsSymbolNameDefinedInImage(dls->lib,symbol)) 
            {
                nssym = NSLookupSymbolInImage(dls->lib,
                                  symbol,
                                  NSLOOKUPSYMBOLINIMAGE_OPTION_BIND 
                                  | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR
                                  );
            }
        }
        else 
        {
            /* Global context, use NSLookupAndBindSymbol */
            if (NSIsSymbolNameDefined(symbol)) 
            {
                /* There doesn't seem to be a return on error option for this call???
                   this is potentially broken, if binding fails, it will improperly
                   exit the application. If anyone comes up with an example of this
                   happening I will install error handlers before making this call, and
                   remove them afterwards. The error handlers themselves can cause problems
                   as they are global to the process, and errors that have nothing to do
                   with dlcompat could cause the error handlers to get called */
                nssym = NSLookupAndBindSymbol(symbol);
            }    
        }                          
        if (!nssym) 
        {
            NSLinkEditErrors ler;
            int lerno;
            const char* errstr;
            const char* file;        
            NSLinkEditError(&ler,&lerno,&file,&errstr);    
            if (errstr && strlen(errstr)) 
            {    
                if (canSetError)    
                    error(errstr);
                debug("%s",errstr);    
            }
            else if (canSetError) 
            {
                error("unable to find symbol \"%s\"", symbol);
            }
            
        }
    }
    if (!nssym)
        return NULL;
    return NSAddressOfSymbol(nssym);
}