Blame man3/dlinfo.3

Packit 7cfc04
'\" t
Packit 7cfc04
.\" Copyright (C) 2015 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH DLINFO 3 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
dlinfo \- obtain information about a dynamically loaded object
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #define _GNU_SOURCE
Packit 7cfc04
.B #include <link.h>
Packit 7cfc04
.B #include <dlfcn.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BR "int dlinfo(void *" handle ", int " request ", void *" info );
Packit 7cfc04
.PP
Packit 7cfc04
Link with \fI\-ldl\fP.
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR dlinfo ()
Packit 7cfc04
function obtains information about the dynamically loaded object
Packit 7cfc04
referred to by
Packit 7cfc04
.IR handle
Packit 7cfc04
(typically obtained by an earlier call to
Packit 7cfc04
.BR dlopen (3)
Packit 7cfc04
or
Packit 7cfc04
.BR dlmopen (3)).
Packit 7cfc04
The
Packit 7cfc04
.I request
Packit 7cfc04
argument specifies which information is to be returned.
Packit 7cfc04
The
Packit 7cfc04
.I info
Packit 7cfc04
argument is a pointer to a buffer used to store information
Packit 7cfc04
returned by the call; the type of this argument depends on
Packit 7cfc04
.IR request .
Packit 7cfc04
.PP
Packit 7cfc04
The following values are supported for
Packit 7cfc04
.IR request
Packit 7cfc04
(with the corresponding type for
Packit 7cfc04
.IR info
Packit 7cfc04
shown in parentheses):
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTLD_DI_LMID " (\fILmid_t *\fP)"
Packit 7cfc04
Obtain the ID of the link-map list (namespace) in which
Packit 7cfc04
.I handle
Packit 7cfc04
is loaded.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTLD_DI_LINKMAP " (\fIstruct link_map **\fP)"
Packit 7cfc04
Obtain a pointer to the
Packit 7cfc04
.I link_map
Packit 7cfc04
structure corresponding to
Packit 7cfc04
.IR handle .
Packit 7cfc04
The
Packit 7cfc04
.IR info
Packit 7cfc04
argument points to a pointer to a
Packit 7cfc04
.I link_map
Packit 7cfc04
structure, defined in
Packit 7cfc04
.I <link.h>
Packit 7cfc04
as:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct link_map {
Packit 7cfc04
    ElfW(Addr) l_addr;  /* Difference between the
Packit 7cfc04
                           address in the ELF file and
Packit 7cfc04
                           the address in memory */
Packit 7cfc04
    char      *l_name;  /* Absolute pathname where
Packit 7cfc04
                           object was found */
Packit 7cfc04
    ElfW(Dyn) *l_ld;    /* Dynamic section of the
Packit 7cfc04
                           shared object */
Packit 7cfc04
    struct link_map *l_next, *l_prev;
Packit 7cfc04
                        /* Chain of loaded objects */
Packit 7cfc04
Packit 7cfc04
    /* Plus additional fields private to the
Packit 7cfc04
       implementation */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTLD_DI_ORIGIN " (\fIchar *\fP)"
Packit 7cfc04
Copy the pathname of the origin of the shared object corresponding to
Packit 7cfc04
.IR handle
Packit 7cfc04
to the location pointed to by
Packit 7cfc04
.IR info .
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTLD_DI_SERINFO " (\fIDl_serinfo *\fP)"
Packit 7cfc04
Obtain the library search paths for the shared object referred to by
Packit 7cfc04
.IR handle .
Packit 7cfc04
The
Packit 7cfc04
.I info
Packit 7cfc04
argument is a pointer to a
Packit 7cfc04
.I Dl_serinfo
Packit 7cfc04
that contains the search paths.
Packit 7cfc04
Because the number of search paths may vary,
Packit 7cfc04
the size of the structure pointed to by
Packit 7cfc04
.IR info
Packit 7cfc04
can vary.
Packit 7cfc04
The
Packit 7cfc04
.B RTLD_DI_SERINFOSIZE
Packit 7cfc04
request described below allows applications to size the buffer suitably.
Packit 7cfc04
The caller must perform the following steps:
Packit 7cfc04
.RS
Packit 7cfc04
.IP 1. 3
Packit 7cfc04
Use a
Packit 7cfc04
.B RTLD_DI_SERINFOSIZE
Packit 7cfc04
request to populate a
Packit 7cfc04
.I Dl_serinfo
Packit 7cfc04
structure with the size
Packit 7cfc04
.RI ( dls_size )
Packit 7cfc04
of the structure needed for the subsequent
Packit 7cfc04
.B RTLD_DI_SERINFO
Packit 7cfc04
request.
Packit 7cfc04
.IP 2.
Packit 7cfc04
Allocate a
Packit 7cfc04
.I Dl_serinfo
Packit 7cfc04
buffer of the correct size
Packit 7cfc04
.RI ( dls_size ).
Packit 7cfc04
.IP 3.
Packit 7cfc04
Use a further
Packit 7cfc04
.B RTLD_DI_SERINFOSIZE
Packit 7cfc04
request to populate the
Packit 7cfc04
.I dls_size
Packit 7cfc04
and
Packit 7cfc04
.I dls_cnt
Packit 7cfc04
fields of the buffer allocated in the previous step.
Packit 7cfc04
.IP 4.
Packit 7cfc04
Use a
Packit 7cfc04
.B RTLD_DI_SERINFO
Packit 7cfc04
to obtain the library search paths.
Packit 7cfc04
.IP
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I Dl_serinfo
Packit 7cfc04
structure is defined as follows:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
typedef struct {
Packit 7cfc04
    size_t dls_size;           /* Size in bytes of
Packit 7cfc04
                                  the whole buffer */
Packit 7cfc04
    unsigned int dls_cnt;      /* Number of elements
Packit 7cfc04
                                  in 'dls_serpath' */
Packit 7cfc04
    Dl_serpath dls_serpath[1]; /* Actually longer,
Packit 7cfc04
                                  'dls_cnt' elements */
Packit 7cfc04
} Dl_serinfo;
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
Each of the
Packit 7cfc04
.I dls_serpath
Packit 7cfc04
elements in the above structure is a structure of the following form:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
typedef struct {
Packit 7cfc04
    char *dls_name;            /* Name of library search
Packit 7cfc04
                                  path directory */
Packit 7cfc04
    unsigned int dls_flags;    /* Indicates where this
Packit 7cfc04
                                  directory came from */
Packit 7cfc04
} Dl_serpath;
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I dls_flags
Packit 7cfc04
field is currently unused, and always contains zero.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTLD_DI_SERINFOSIZE " (\fIDl_serinfo *\fP)"
Packit 7cfc04
Populate the
Packit 7cfc04
.I dls_size
Packit 7cfc04
and
Packit 7cfc04
.I dls_cnt
Packit 7cfc04
fields of the
Packit 7cfc04
.I Dl_serinfo
Packit 7cfc04
structure pointed to by
Packit 7cfc04
.IR info
Packit 7cfc04
with values suitable for allocating a buffer for use in a subsequent
Packit 7cfc04
.B RTLD_DI_SERINFO
Packit 7cfc04
request.
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTLD_DI_TLS_MODID " (\fIsize_t *\fP, since glibc 2.4)"
Packit 7cfc04
Obtain the module ID of this shared object's TLS (thread-local storage)
Packit 7cfc04
segment, as used in TLS relocations.
Packit 7cfc04
If this object does not define a TLS segment, zero is placed in
Packit 7cfc04
.IR *info .
Packit 7cfc04
.TP
Packit 7cfc04
.BR RTLD_DI_TLS_DATA " (\fIvoid **\fP, since glibc 2.4)"
Packit 7cfc04
Obtain a pointer to the calling
Packit 7cfc04
thread's TLS block corresponding to this shared object's TLS segment.
Packit 7cfc04
If this object does not define a PT_TLS segment,
Packit 7cfc04
or if the calling thread has not allocated a block for it,
Packit 7cfc04
NULL is placed in
Packit 7cfc04
.IR *info .
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success,
Packit 7cfc04
.BR dlinfo ()
Packit 7cfc04
returns 0.
Packit 7cfc04
On failure, it returns \-1; the cause of the error can be diagnosed using
Packit 7cfc04
.BR dlerror (3).
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.BR dlinfo ()
Packit 7cfc04
first appeared in glibc 2.3.3.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lb lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR dlinfo ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
This function is a nonstandard GNU extension.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
This function derives from the Solaris function of the same name
Packit 7cfc04
and also appears on some other systems.
Packit 7cfc04
The sets of requests supported by the various implementations
Packit 7cfc04
overlaps only partially.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The program below opens a shared objects using
Packit 7cfc04
.BR dlopen (3)
Packit 7cfc04
and then uses the
Packit 7cfc04
.B RTLD_DI_SERINFOSIZE
Packit 7cfc04
and
Packit 7cfc04
.B RTLD_DI_SERINFO
Packit 7cfc04
requests to obtain the library search path list for the library.
Packit 7cfc04
Here is an example of what we might see when running the program:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ \fB./a.out /lib64/libm.so.6\fP
Packit 7cfc04
dls_serpath[0].dls_name = /lib64
Packit 7cfc04
dls_serpath[1].dls_name = /usr/lib64
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS Program source
Packit 7cfc04
\&
Packit 7cfc04
.EX
Packit 7cfc04
#define _GNU_SOURCE
Packit 7cfc04
#include <dlfcn.h>
Packit 7cfc04
#include <link.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    void *handle;
Packit 7cfc04
    Dl_serinfo serinfo;
Packit 7cfc04
    Dl_serinfo *sip;
Packit 7cfc04
    int j;
Packit 7cfc04
Packit 7cfc04
    if (argc != 2) {
Packit 7cfc04
        fprintf(stderr, "Usage: %s <libpath>\\n", argv[0]);
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* Obtain a handle for shared objects specified on command line */
Packit 7cfc04
Packit 7cfc04
    handle = dlopen(argv[1], RTLD_NOW);
Packit 7cfc04
    if (handle == NULL) {
Packit 7cfc04
        fprintf(stderr, "dlopen() failed: %s\\n", dlerror());
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* Discover the size of the buffer that we must pass to
Packit 7cfc04
       RTLD_DI_SERINFO */
Packit 7cfc04
Packit 7cfc04
    if (dlinfo(handle, RTLD_DI_SERINFOSIZE, &serinfo) == \-1) {
Packit 7cfc04
        fprintf(stderr, "RTLD_DI_SERINFOSIZE failed: %s\\n", dlerror());
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* Allocate the buffer for use with RTLD_DI_SERINFO */
Packit 7cfc04
Packit 7cfc04
    sip = malloc(serinfo.dls_size);
Packit 7cfc04
    if (sip == NULL) {
Packit 7cfc04
        perror("malloc");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* Initialize the \(aqdls_size\(aq and \(aqdls_cnt\(aq fields in the newly
Packit 7cfc04
       allocated buffer */
Packit 7cfc04
Packit 7cfc04
    if (dlinfo(handle, RTLD_DI_SERINFOSIZE, sip) == \-1) {
Packit 7cfc04
        fprintf(stderr, "RTLD_DI_SERINFOSIZE failed: %s\\n", dlerror());
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /* Fetch and print library search list */
Packit 7cfc04
Packit 7cfc04
    if (dlinfo(handle, RTLD_DI_SERINFO, sip) == \-1) {
Packit 7cfc04
        fprintf(stderr, "RTLD_DI_SERINFO failed: %s\\n", dlerror());
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    for (j = 0; j < serinfo.dls_cnt; j++)
Packit 7cfc04
        printf("dls_serpath[%d].dls_name = %s\\n",
Packit 7cfc04
                j, sip\->dls_serpath[j].dls_name);
Packit 7cfc04
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR dl_iterate_phdr (3),
Packit 7cfc04
.BR dladdr (3),
Packit 7cfc04
.BR dlerror (3),
Packit 7cfc04
.BR dlopen (3),
Packit 7cfc04
.BR dlsym (3),
Packit 7cfc04
.BR ld.so (8)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.