dlclose on FreeBSD


 
Thread Tools Search this Thread
Top Forums Programming dlclose on FreeBSD
# 1  
Old 06-12-2003
Question dlclose on FreeBSD

Anyone knows how to use dynamic module loading through dlopen/dlsym/dlclose properly on FreeBSD?

I wrote a tiny program to learn KDE programming. Perhaps I give you a small snippet to look at

Code:
int IPTablesApp::Start(int argc, char* argv[]) {
	typedef void (*init_t)(int, char*[]);
	init_t init;
	void *view = dlopen("./libkde-plugin.so", RTLD_NOW);
	if (!view) {
		// error loading libraries
		fprintf(stderr, "Interface library load error: %s\n", dlerror());
		return -1;
	} else {
		init = (init_t)dlsym(view, "_Init");
		init(argc, argv);
	}
	dlclose(view);
	return 0;
}

The KDE app is in libkde-plugin.so and is not shown here. Basically, when the app ends and execute the dlclose, gdb catches a segmentation fault for "Bad Address".

The same code runs okay on my Linux. Anyone can tell me if my way of using dl- functions are correct and are there workarounds (I saw some similar apps just comment out the dlclose line, but I don't know if this is proper or not)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Fedora

FreeBSD

I'm using Windows mostly and the only *nix thing I used during my life was cygwin (I like command line :)). But currently I have (by an occasion) a DVD with latest FreeBSD. I don't know why, but I want to install it... But probably, this OS is too difficult for a beginner. I heard, it's used on... (3 Replies)
Discussion started by: TeenageWerewolf
3 Replies

2. Programming

Application crashes in FreeBSD 7.1 while working ok in FreeBSD 6.3

Hello there, My mulithreaded application (which is too large to represent the source code here) is crashing after installing FreeBSD 7.1-RELEASE/amd64. It worked properly on others machines (Dual Cores with 4GB of RAM - FreeBSD 6.2-RELEASE/i386). The current machine has 2x Core 2 Duo... (1 Reply)
Discussion started by: Seenquev
1 Replies

3. Programming

dlclose crashing in 64bit

Hi I have a 64bit C++ dynamic component built using Sun Forte compiler(CC) on one server. I am opening this shared component using dlopen and checking if a particular function is defined or not. After that, when I am closing the component using dlclose, the program is crashing. The... (3 Replies)
Discussion started by: ajphaj
3 Replies

4. BSD

please help me in FreeBSD

Hi to all, Iam doing a project in Free BSD and i am stuck with a puzzle. Please any one of you clarify my doubt : How to add a mechanism to check the status of the file system which alerts the root user via. email if any single partition is greater than 90% full. This alert should include the... (3 Replies)
Discussion started by: tadakamalla
3 Replies

5. BSD

I want the Best FREEBSD

B]I want the best of the best FreeBSD version for my study in Unix...please sugest me... Thanks (5 Replies)
Discussion started by: israel
5 Replies

6. BSD

FreeBSD 5.2.1

I want someone tell me the positives and negatives points of FreeBSD 5.2.1, if someone help me, i thanks a lot! (4 Replies)
Discussion started by: israel
4 Replies

7. UNIX for Dummies Questions & Answers

FreeBSD

What is FreeBSD, who does use Free? (3 Replies)
Discussion started by: cyberangel
3 Replies

8. UNIX for Dummies Questions & Answers

Freebsd 4.5

i am new at unix and am VERY confused about the compression processes. what progs do i need to unzip and zip files? there must be a standard one similar to winzip? (1 Reply)
Discussion started by: Mindscan
1 Replies

9. UNIX for Dummies Questions & Answers

FreeBSD

I am on AOL now,and i am using 56 k,i was gonna install FreeBSD,but i dont know if aol works on BSD,or my modem. Do they work on BSD? (2 Replies)
Discussion started by: kita
2 Replies

10. UNIX for Dummies Questions & Answers

need help with FreeBSD!!!

Hi I want to write to my output_file using: if((fptr = creat(output_file, _S_IWRITE)) == -1) { printf("output_file..."..); return (1); } for(...) { _write(fptr, buffer, BUF_SIZE); } It says "_S_IWRITE" undeclared!!! Anybody knows what function I can use for that and what I... (3 Replies)
Discussion started by: lacasa
3 Replies
Login or Register to Ask a Question
DLOPEN(3)						     Linux Programmer's Manual							 DLOPEN(3)

NAME
dlclose, dlerror, dlopen, dlsym - Programming interface to dynamic linking loader. SYNOPSIS
#include <dlfcn.h> void *dlopen(const char *filename, int flag); const char *dlerror(void); void *dlsym(void *handle, char *symbol); int dlclose(void *handle); Special symbols: _init, _fini. DESCRIPTION
dlopen loads a dynamic library from the file named by the null terminated string filename and returns an opaque "handle" for the dynamic library. If filename is not an absolute path (i.e., it does not begin with a "/"), then the file is searched for in the following loca- tions: A colon-separated list of directories in the user's LD_LIBRARY_PATH environment variable. The list of libraries cached in /etc/ld.so.cache. /lib, followed by /usr/lib. If filename is a NULL pointer, then the returned handle is for the main program. External references in the library are resolved using the libraries in that library's dependency list and any other libraries previously opened with the RTLD_GLOBAL flag. If the executable was linked with the flag "-rdynamic", then the global symbols in the executable will also be used to resolve references in a dynamically loaded library. flag must be either RTLD_LAZY, meaning resolve undefined symbols as code from the dynamic library is executed, or RTLD_NOW, meaning resolve all undefined symbols before dlopen returns, and fail if this cannot be done. Optionally, RTLD_GLOBAL may be or'ed with flag, in which case the external symbols defined in the library will be made available to subsequently loaded libraries. If the library exports a routine named _init, then that code is executed before dlopen returns. If the same library is loaded twice with dlopen(), the same file handle is returned. The dl library maintains link counts for dynamic file handles, so a dynamic library is not deallocated until dlclose has been called on it as many times as dlopen has succeeded on it. If dlopen fails for any reason, it returns NULL. A human readable string describing the most recent error that occurred from any of the dl routines (dlopen, dlsym or dlclose) can be extracted with dlerror(). dlerror returns NULL if no errors have occurred since initialization or since it was last called. (Calling dlerror() twice consecutively, will always result in the second call returning NULL.) dlsym takes a "handle" of a dynamic library returned by dlopen and the null terminated symbol name, returning the address where that symbol is loaded. If the symbol is not found, dlsym returns NULL; however, the correct way to test for an error from dlsym is to save the result of dlerror into a variable, and then check if saved value is not NULL. This is because the value of the symbol could actually be NULL. It is also necessary to save the results of dlerror into a variable because if dlerror is called again, it will return NULL. There are two special pseudo-handles, RTLD_DEFAULT and RTLD_NEXT. The former will find the first occurrence of the desired symbol using the default library search order. The latter, which is usable only from within a dynamic library, will find the next occurrence of a func- tion in the search order after the current library. This allows one to provide a wrapper around a function in another shared library. dlclose decrements the reference count on the dynamic library handle handle. If the reference count drops to zero and no other loaded libraries use symbols in it, then the dynamic library is unloaded. If the dynamic library exports a routine named _fini, then that routine is called just before the library is unloaded. RETURN VALUE
dlclose returns 0 on success, and non-zero on error. EXAMPLE
Load the math library, and print the cosine of 2.0: #include <stdio.h> #include <dlfcn.h> int main(int argc, char **argv) { void *handle; double (*cosine)(double); char *error; handle = dlopen ("libm.so", RTLD_LAZY); if (!handle) { fprintf (stderr, "%s ", dlerror()); exit(1); } cosine = dlsym(handle, "cos"); if ((error = dlerror()) != NULL) { fprintf (stderr, "%s ", error); exit(1); } printf ("%f ", (*cosine)(2.0)); dlclose(handle); return 0; } If this program were in a file named "foo.c", you would build the program with the following command: gcc -rdynamic -o foo foo.c -ldl NOTES
The symbols RTLD_DEFAULT and RTLD_NEXT are defined by <dlfcn.h> only when _GNU_SOURCE was defined before including it. ACKNOWLEDGEMENTS
The dlopen interface standard comes from Solaris. The Linux dlopen implementation was primarily written by Eric Youngdale with help from Mitch D'Souza, David Engel, Hongjiu Lu, Andreas Schwab and others. The manual page was written by Adam Richter. SEE ALSO
ld(1), ld.so(8), ldconfig(8), ldd(1), ld.so.info Linux 2001-12-14 DLOPEN(3)