Esempio n. 1
0
void DynaLoader::loadLibrary ( const QString& fileName ) 
    /*throw (DynaLoader::LibraryLoadException)*/
{
    freeLibrary();    
    QFileInfo dlInfo( fileName );
    QString dlFileName = dlInfo.fileName();

    QString convertedFileName = fileName;
//TODO: should load MAC frameworks with out .dyld postfix specified
#ifndef _MAC_
    QRegExp regExp( "^" + libPrefix() + ".+\\." + libExtension() + "$" );
    if ( -1 == regExp.indexIn( dlFileName ) ) {
        dlFileName = libPrefix() + dlFileName + "." + libExtension();
        convertedFileName = dlInfo.absolutePath() + QDir::separator() +
                            dlFileName;
    }
#endif
    convertedFileName = QDir::convertSeparators( convertedFileName );
#if defined UNICODE && (defined _WIN32 || defined WIN32)
    handle = dl_open(convertedFileName.toStdWString().data());
#else
    handle = dl_open(QFile::encodeName(convertedFileName));
#endif
    if ( handle == 0 )
        throw LibraryLoadException( dl_error() );
}
Esempio n. 2
0
/*
 * call-seq: initialize(libname, libflags)
 * @param [String] libname name of library to open
 * @param [Fixnum] libflags flags for library to open
 * @return [FFI::DynamicLibrary]
 * @raise {LoadError} if +libname+ cannot be opened
 * A new DynamicLibrary instance.
 */
static VALUE
library_initialize(VALUE self, VALUE libname, VALUE libflags)
{
    Library* library;
    int flags;

    Check_Type(libflags, T_FIXNUM);

    Data_Get_Struct(self, Library, library);
    flags = libflags != Qnil ? NUM2UINT(libflags) : 0;
    
    library->handle = dl_open(libname != Qnil ? StringValueCStr(libname) : NULL, flags);
    if (library->handle == NULL) {
        char errmsg[1024];
        dl_error(errmsg, sizeof(errmsg));
        rb_raise(rb_eLoadError, "Could not open library '%s': %s",
                libname != Qnil ? StringValueCStr(libname) : "[current process]",
                errmsg);
    }
#ifdef __CYGWIN__
    // On Cygwin 1.7.17 "dlsym(dlopen(0,0), 'getpid')" fails. (dlerror: "No such process")
    // As a workaround we can use "dlsym(RTLD_DEFAULT, 'getpid')" instead.
    // Since 0 == RTLD_DEFAULT we won't call dl_close later.
    if (libname == Qnil) {
        dl_close(library->handle);
        library->handle = RTLD_DEFAULT;
    }
#endif
    rb_iv_set(self, "@name", libname != Qnil ? libname : rb_str_new2("[current process]"));
    return self;
}
Esempio n. 3
0
/*
 * Class:     com_kenai_jffi_Foreign
 * Method:    dlopen
 * Signature: (Ljava/lang/String;I)J
 */
JNIEXPORT jlong JNICALL
Java_com_kenai_jffi_Foreign_dlopen(JNIEnv* env, jobject self, jstring jPath, jint jFlags)
{
#ifdef _WIN32
    wchar_t path[PATH_MAX];
    getWideString(env, path, jstr, sizeof(path) / sizeof(path[0]));
    return p2j(LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH));
#else
    char path_[PATH_MAX];
    const char* path = NULL; // Handle dlopen(NULL, flags);
    int flags = 0;
#define F(x) (jFlags & com_kenai_jffi_Foreign_RTLD_##x) != 0 ? RTLD_##x : 0;
    flags |= F(LAZY);
    flags |= F(GLOBAL);
    flags |= F(LOCAL);
    flags |= F(NOW);
#undef F

#ifdef _AIX
    flags |= RTLD_MEMBER; //  Needed for AIX
#endif
    
    if (jPath != NULL) {
        path = path_;
        getMultibyteString(env, path_, jPath, sizeof(path_));
    }
    return p2j(dl_open(path, flags));
#endif
}
Esempio n. 4
0
long mac_addr_dlpi( const char *dev, int unit, u_char  *addr){
	int fd;
	u_char mac_addr[25];

	if(GOT_ERR != dl_open(dev, unit, &fd)){
        	if(GOT_ERR != dl_bind(fd, INSAP, mac_addr)){
                	bcopy( mac_addr, addr, 6);
                	return 0;
			}
		}
        close(fd);

	return -1;
	}
Esempio n. 5
0
void *
pg_dlopen(char *filename)
{
	static int	dl_initialized = 0;
	void	   *handle;

	/*
	 * initializes the dynamic loader with the executable's pathname. (only
	 * needs to do this the first time pg_dlopen is called.)
	 */
	if (!dl_initialized)
	{
		if (!dl_init(my_exec_path))
			return NULL;

		/*
		 * if there are undefined symbols, we want dl to search from the
		 * following libraries also.
		 */
		dl_setLibraries("/usr/lib/libm_G0.a:/usr/lib/libc_G0.a");
		dl_initialized = 1;
	}

	/*
	 * open the file. We do the symbol resolution right away so that we will
	 * know if there are undefined symbols. (This is in fact the same
	 * semantics as "ld -A". ie. you cannot have undefined symbols.
	 */
	if ((handle = dl_open(filename, DL_NOW)) == NULL)
	{
		int			count;
		char	  **list = dl_undefinedSymbols(&count);

		/* list the undefined symbols, if any */
		if (count)
		{
			while (*list)
			{
				elog(WARNING, "\"%s\" is undefined", *list);
				list++;
			}
		}
	}

	return (void *) handle;
}
Esempio n. 6
0
File: Library.c Progetto: jnr/jffi
/*
 * Class:     com_kenai_jffi_Foreign
 * Method:    dlopen
 * Signature: (Ljava/lang/String;I)J
 */
JNIEXPORT jlong JNICALL
Java_com_kenai_jffi_Foreign_dlopen(JNIEnv* env, jobject self, jstring jPath, jint jFlags)
{
#ifdef _WIN32
    if (jPath == NULL) {
        return p2j(GetModuleHandle(NULL));
    } else {
        wchar_t path[PATH_MAX];
        DWORD dwFlags;
        getWideString(env, path, jPath, sizeof(path) / sizeof(path[0]));
        dwFlags = PathIsRelativeW(path) ? 0 : LOAD_WITH_ALTERED_SEARCH_PATH;
        return p2j(LoadLibraryExW(path, NULL, dwFlags));
    }
#else
    char path_[PATH_MAX];
    const char* path = NULL; // Handle dlopen(NULL, flags);
    void* handle = NULL;
    int flags = 0;
#define F(x) (jFlags & com_kenai_jffi_Foreign_RTLD_##x) != 0 ? RTLD_##x : 0;
    flags |= F(LAZY);
    flags |= F(GLOBAL);
    flags |= F(LOCAL);
    flags |= F(NOW);
#undef F

#ifdef _AIX
    flags |= RTLD_MEMBER; //  Needed for AIX
#endif
    
    if (jPath != NULL) {
        path = path_;
        getMultibyteString(env, path_, jPath, sizeof(path_));
    }

    handle = dl_open(path, flags);
    if (handle == NULL) {
        char errbuf[1024] = { 0 };
        dl_error(errbuf, sizeof(errbuf) - 1);
        throwException(env, UnsatisfiedLink, "%s", errbuf);
    }
    
    return p2j(handle);
#endif
}
Esempio n. 7
0
static VALUE
library_initialize(VALUE self, VALUE libname, VALUE libflags)
{
    Library* library;
    int flags;

    Check_Type(libflags, T_FIXNUM);

    Data_Get_Struct(self, Library, library);
    flags = libflags != Qnil ? NUM2UINT(libflags) : 0;
    
    library->handle = dl_open(libname != Qnil ? StringValueCStr(libname) : NULL, flags);
    if (library->handle == NULL) {
        char errmsg[1024];
        dl_error(errmsg, sizeof(errmsg));
        rb_raise(rb_eLoadError, "Could not open library '%s': %s",
                libname != Qnil ? StringValueCStr(libname) : "[current process]",
                errmsg);
    }
    rb_iv_set(self, "@name", libname != Qnil ? libname : rb_str_new2("[current process]"));
    return self;
}
Esempio n. 8
0
 int
libload_ASL(AmplExports *ae, const char *s, int ns, int warn)
{
	Funcadd *fa;
	char buf0[2048], *buf;
	int ns1, rc, rcnf, warned;
	shl_t h;
	size_t n, nx;

	nx = 0;
	buf = buf0;
	if (!Abspath(s)) {
		if (!GetCurrentDirectory(sizeof(buf0),buf0))
			return 2;
		nx = strlen(buf0);
		}
	n = ns + sizeof(afdll) + nx + 3; /* +3 for inserting _32 or _64 */
	if (n > sizeof(buf0)) {
		buf = (char*)mymalloc(n);
		if (nx)
			memcpy(buf, buf0, nx);
		}
	if (nx)
		buf[nx++] = SLASH;
	strncpy(buf+nx, s, ns);
	buf[nx+ns] = 0;
	rc = warned = 0;
	rcnf = warn >> 1;
	warn &= 1;
	if ((h = dl_open(ae, buf, &warned, &ns1))) {
 found:
		if (find_dlsym(fa, h, FUNCADD)
		 || find_dlsym(fa, h, "funcadd")) {
#ifdef CLOSE_AT_RESET
			aflibname_ASL(ae,buf,buf+nx,ns1-nx,fa,0,dl_close,h);
				/* -DCLOSE_AT_RESET is for use in shared */
				/* libraries, such as MATLAB mex functions, */
				/* that may be loaded and unloaded several */
				/* times during execution of the program. */
#else
			aflibname_ASL(ae,buf,buf+nx,ns1-nx,fa,1,dl_close,h);
#endif
			}
		else {
			fprintf(stderr, "Could not find funcadd in %s\n", buf);
			dl_close(h);
			rc = 3;
			}
		}
	else if (warn) {
		if (!warned) {
			strcpy(buf+nx+ns, afdll);
			if ((h = dl_open(ae, buf, &warned, &ns1)))
				goto found;
			}
		if (warned)
			rc = 2;
		else
			goto notfound;
		}
	else {
 notfound:
		rc = rcnf;
		if (warn)
			fprintf(Stderr, "Cannot find library %.*s\nor %.*s%s\n",
				ns, s, ns, s, afdll);
		}
	if (buf != buf0)
		free(buf);
	return rc;
	}