DLINFO(3) | Linux Programmer's Manual | DLINFO(3) |
dlinfo - obtain information about a dynamically loaded object
#define _GNU_SOURCE #include <link.h> #include <dlfcn.h>
int dlinfo(void *handle, int request, void *info);
Link with -ldl.
The dlinfo() function obtains information about the dynamically loaded object referred to by handle (typically obtained by an earlier call to dlopen(3) or dlmopen(3)). The request argument specifies which information is to be returned. The info argument is a pointer to a buffer used to store information returned by the call; the type of this argument depends on request.
The following values are supported for request (with the corresponding type for info shown in parentheses):
struct link_map {
ElfW(Addr) l_addr; /* Difference between the
address in the ELF file and
the address in memory */
char *l_name; /* Absolute pathname where
object was found */
ElfW(Dyn) *l_ld; /* Dynamic section of the
shared object */
struct link_map *l_next, *l_prev;
/* Chain of loaded objects */
/* Plus additional fields private to the
implementation */ };
typedef struct {
size_t dls_size; /* Size in bytes of
the whole buffer */
unsigned int dls_cnt; /* Number of elements
in 'dls_serpath' */
Dl_serpath dls_serpath[1]; /* Actually longer,
'dls_cnt' elements */ } Dl_serinfo;
typedef struct {
char *dls_name; /* Name of library search
path directory */
unsigned int dls_flags; /* Indicates where this
directory came from */ } Dl_serpath;
On success, dlinfo() returns 0. On failure, it returns -1; the cause of the error can be diagnosed using dlerror(3).
dlinfo() first appeared in glibc 2.3.3.
For an explanation of the terms used in this section, see attributes(7).
Interface | Attribute | Value |
dlinfo () | Thread safety | MT-Safe |
This function is a nonstandard GNU extension.
This function derives from the Solaris function of the same name and also appears on some other systems. The sets of requests supported by the various implementations overlaps only partially.
The program below opens a shared objects using dlopen(3) and then uses the RTLD_DI_SERINFOSIZE and RTLD_DI_SERINFO requests to obtain the library search path list for the library. Here is an example of what we might see when running the program:
$ ./a.out /lib64/libm.so.6 dls_serpath[0].dls_name = /lib64 dls_serpath[1].dls_name = /usr/lib64
#define _GNU_SOURCE #include <dlfcn.h> #include <link.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) {
void *handle;
Dl_serinfo serinfo;
Dl_serinfo *sip;
if (argc != 2) {
fprintf(stderr, "Usage: %s <libpath>\n", argv[0]);
exit(EXIT_FAILURE);
}
/* Obtain a handle for shared object specified on command line */
handle = dlopen(argv[1], RTLD_NOW);
if (handle == NULL) {
fprintf(stderr, "dlopen() failed: %s\n", dlerror());
exit(EXIT_FAILURE);
}
/* Discover the size of the buffer that we must pass to
RTLD_DI_SERINFO */
if (dlinfo(handle, RTLD_DI_SERINFOSIZE, &serinfo) == -1) {
fprintf(stderr, "RTLD_DI_SERINFOSIZE failed: %s\n", dlerror());
exit(EXIT_FAILURE);
}
/* Allocate the buffer for use with RTLD_DI_SERINFO */
sip = malloc(serinfo.dls_size);
if (sip == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
/* Initialize the 'dls_size' and 'dls_cnt' fields in the newly
allocated buffer */
if (dlinfo(handle, RTLD_DI_SERINFOSIZE, sip) == -1) {
fprintf(stderr, "RTLD_DI_SERINFOSIZE failed: %s\n", dlerror());
exit(EXIT_FAILURE);
}
/* Fetch and print library search list */
if (dlinfo(handle, RTLD_DI_SERINFO, sip) == -1) {
fprintf(stderr, "RTLD_DI_SERINFO failed: %s\n", dlerror());
exit(EXIT_FAILURE);
}
for (int j = 0; j < serinfo.dls_cnt; j++)
printf("dls_serpath[%d].dls_name = %s\n",
j, sip->dls_serpath[j].dls_name);
exit(EXIT_SUCCESS); }
dl_iterate_phdr(3), dladdr(3), dlerror(3), dlopen(3), dlsym(3), ld.so(8)
This page is part of release 5.10 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.
2020-11-01 | Linux |