How can I find the library where the function open() is? Like, the name of the actual "xxxxxx.so" file that contains that function? Also, is there a place I could typically get this information for other functions?
Asked
Active
Viewed 870 times
0
-
4The nm command will search an object file and list the names of entry points. You can start with that. open() is a syscall - the declaration and entry point are in libc.so.[nnnn]. See the syscall() man page here: http://man7.org/linux/man-pages/man2/syscall.2.html – jim mcnamara Dec 02 '14 at 03:34
-
1Do you need the filename or just a handle for `dlsym`? Maybe have a look at `RTLD_DEFAULT` and `RTLD_NEXT`. – mafso Dec 02 '14 at 03:36
-
2XY problem anyone? Why do you need that? – n. m. could be an AI Dec 02 '14 at 03:56
-
Thanks for the responses. And I do need it for dlsym, and I have it using RTLD_NEXT, but I wanted to try it with dlopen. – user3475234 Dec 02 '14 at 03:58
-
Just for the sake of trying it for getting familiar with `dlopen`/`dlsym`? I mean, is a hard-coded location OK (platform-specific, look into `/lib/...` or `/lib64/...`) The general case will be impossible, I think (and sounds like an XY problem/design-flaw -- if you use `RTLD_NEXT`, you usually don't care about the file name, and if you used `dlopen` but forgot the filename, then this is the problem). E.g. under Linux, you can load a library, delete (unlink) the file and the handler remains valid (but there is no file name). – mafso Dec 02 '14 at 04:37
-
1I just wanted to see if I could get the same result with dlopen. Also, I realized I didn't know how to find the library a given function was in, which was required for dlopen, so rather than ask for a specific solution to my problem, I thought I'd try to learn something instead :) – user3475234 Dec 02 '14 at 06:15
1 Answers
0
I didn't know how to find the library a given function was in, which was required for dlopen
Knowing the library is not required for dlopen()
:
If file is a null pointer, dlopen() shall return a global symbol table handle for the currently running process image. This symbol table handle shall provide access to the symbols from an ordered set of executable object files consisting of the original program image file, any executable object files loaded at program start-up as specified by that process file (for example, shared libraries), and the set of executable object files loaded using dlopen() operations with the RTLD_GLOBAL flag. …
But if you really want to know:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main()
{
// We don't need to know the library for dlopen():
void *handle = dlopen(NULL, RTLD_NOW);
if (!handle) puts(dlerror()), exit(1);
void *p = dlsym(handle, "open");
char *s = dlerror();
if (s) puts(s), exit(1);
printf("open = %p\n", p);
// But we can find the library with a Glibc extension:
Dl_info info;
if (dladdr(p, &info))
printf("%s contains %s\n", info.dli_fname, info.dli_sname);
}

Armali
- 18,255
- 14
- 57
- 171