Blame man3/dladdr.3

Packit 7cfc04
'\" t
Packit 7cfc04
.\" Copyright (C) 2015 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" and Copyright (C) 2008 Petr Baudis <pasky@suse.cz> (dladdr caveat)
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 DLADDR 3 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
dladdr, dladdr1 \- translate address to symbolic information
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #define _GNU_SOURCE
Packit 7cfc04
.B #include <dlfcn.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int dladdr(void *" addr ", Dl_info *" info );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int dladdr1(void *" addr ", Dl_info *" info ", void **" \
Packit 7cfc04
        extra_info ", int " flags );
Packit 7cfc04
.PP
Packit 7cfc04
Link with \fI\-ldl\fP.
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The function
Packit 7cfc04
.BR dladdr ()
Packit 7cfc04
determines whether the address specified in
Packit 7cfc04
.IR addr
Packit 7cfc04
is located in one of the shared objects loaded by the calling application.
Packit 7cfc04
If it is, then
Packit 7cfc04
.BR dladdr ()
Packit 7cfc04
returns information about the shared object and symbol that overlaps
Packit 7cfc04
.IR addr .
Packit 7cfc04
This information is returned in a
Packit 7cfc04
.I Dl_info
Packit 7cfc04
structure:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
typedef struct {
Packit 7cfc04
    const char *dli_fname;  /* Pathname of shared object that
Packit 7cfc04
                               contains address */
Packit 7cfc04
    void       *dli_fbase;  /* Base address at which shared
Packit 7cfc04
                               object is loaded */
Packit 7cfc04
    const char *dli_sname;  /* Name of symbol whose definition
Packit 7cfc04
                               overlaps \fIaddr\fP */
Packit 7cfc04
    void       *dli_saddr;  /* Exact address of symbol named
Packit 7cfc04
                               in \fIdli_sname\fP */
Packit 7cfc04
} Dl_info;
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
If no symbol matching
Packit 7cfc04
.I addr
Packit 7cfc04
could be found, then
Packit 7cfc04
.I dli_sname
Packit 7cfc04
and
Packit 7cfc04
.I dli_saddr
Packit 7cfc04
are set to NULL.
Packit 7cfc04
.PP
Packit 7cfc04
The function
Packit 7cfc04
.BR dladdr1 ()
Packit 7cfc04
is like
Packit 7cfc04
.BR dladdr (),
Packit 7cfc04
but returns additional information via the argument
Packit 7cfc04
.IR extra_info .
Packit 7cfc04
The information returned depends on the value specified in
Packit 7cfc04
.IR flags ,
Packit 7cfc04
which can have one of the following values:
Packit 7cfc04
.TP
Packit 7cfc04
.B RTLD_DL_LINKMAP
Packit 7cfc04
Obtain a pointer to the link map for the matched file.
Packit 7cfc04
The
Packit 7cfc04
.IR extra_info
Packit 7cfc04
argument points to a pointer to a
Packit 7cfc04
.I link_map
Packit 7cfc04
structure (i.e.,
Packit 7cfc04
.IR "struct link_map\ **" ),
Packit 7cfc04
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
.B RTLD_DL_SYMENT
Packit 7cfc04
Obtain a pointer to the ELF symbol table entry of the matching symbol.
Packit 7cfc04
The
Packit 7cfc04
.IR extra_info
Packit 7cfc04
argument is a pointer to a symbol pointer:
Packit 7cfc04
.IR "const ElfW(Sym) **" .
Packit 7cfc04
The
Packit 7cfc04
.IR ElfW ()
Packit 7cfc04
macro definition turns its argument into the name of an ELF data
Packit 7cfc04
type suitable for the hardware architecture.
Packit 7cfc04
For example, on a 64-bit platform,
Packit 7cfc04
.I ElfW(Sym)
Packit 7cfc04
yields the data type name
Packit 7cfc04
.IR Elf64_Sym ,
Packit 7cfc04
which is defined in
Packit 7cfc04
.IR <elf.h>
Packit 7cfc04
as:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
typedef struct  {
Packit 7cfc04
    Elf64_Word    st_name;     /* Symbol name */
Packit 7cfc04
    unsigned char st_info;     /* Symbol type and binding */
Packit 7cfc04
    unsigned char st_other;    /* Symbol visibility */
Packit 7cfc04
    Elf64_Section st_shndx;    /* Section index */
Packit 7cfc04
    Elf64_Addr    st_value;    /* Symbol value */
Packit 7cfc04
    Elf64_Xword   st_size;     /* Symbol size */
Packit 7cfc04
} Elf64_Sym;
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I st_name
Packit 7cfc04
field is an index into the string table.
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I st_info
Packit 7cfc04
field encodes the symbol's type and binding.
Packit 7cfc04
The type can be extracted using the macro
Packit 7cfc04
.BR ELF64_ST_TYPE(st_info)
Packit 7cfc04
(or
Packit 7cfc04
.BR ELF32_ST_TYPE()
Packit 7cfc04
on 32-bit platforms), which yields one of the following values:
Packit 7cfc04
.in +4n
Packit 7cfc04
.TS
Packit 7cfc04
lb lb
Packit 7cfc04
lb l.
Packit 7cfc04
Value	Description
Packit 7cfc04
STT_NOTYPE	Symbol type is unspecified
Packit 7cfc04
STT_OBJECT	Symbol is a data object
Packit 7cfc04
STT_FUNC	Symbol is a code object
Packit 7cfc04
STT_SECTION	Symbol associated with a section
Packit 7cfc04
STT_FILE	Symbol's name is file name
Packit 7cfc04
STT_COMMON	Symbol is a common data object
Packit 7cfc04
STT_TLS	Symbol is thread-local data object
Packit 7cfc04
STT_GNU_IFUNC	Symbol is indirect code object
Packit 7cfc04
.TE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The symbol binding can be extracted from the
Packit 7cfc04
.I st_info
Packit 7cfc04
field using the macro
Packit 7cfc04
.BR ELF64_ST_BIND(st_info)
Packit 7cfc04
(or
Packit 7cfc04
.BR ELF32_ST_BIND()
Packit 7cfc04
on 32-bit platforms), which yields one of the following values:
Packit 7cfc04
.in +4n
Packit 7cfc04
.TS
Packit 7cfc04
lb lb
Packit 7cfc04
lb l.
Packit 7cfc04
Value	Description
Packit 7cfc04
STB_LOCAL	Local symbol
Packit 7cfc04
STB_GLOBAL	Global symbol
Packit 7cfc04
STB_WEAK	Weak symbol
Packit 7cfc04
STB_GNU_UNIQUE	Unique symbol
Packit 7cfc04
.TE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
The
Packit 7cfc04
.I st_other
Packit 7cfc04
field contains the symbol's visibility, which can be extracted using the macro
Packit 7cfc04
.BR ELF64_ST_VISIBILITY(st_info)
Packit 7cfc04
(or
Packit 7cfc04
.BR ELF32_ST_VISIBILITY()
Packit 7cfc04
on 32-bit platforms), which yields one of the following values:
Packit 7cfc04
.in +4n
Packit 7cfc04
.TS
Packit 7cfc04
lb lb
Packit 7cfc04
lb l.
Packit 7cfc04
Value	Description
Packit 7cfc04
STV_DEFAULT	Default symbol visibility rules
Packit 7cfc04
STV_INTERNAL	Processor-specific hidden class
Packit 7cfc04
STV_HIDDEN	Symbol unavailable in other modules
Packit 7cfc04
STV_PROTECTED	Not preemptible, not exported
Packit 7cfc04
.TE
Packit 7cfc04
.in
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, these functions return a nonzero value.
Packit 7cfc04
If the address specified in
Packit 7cfc04
.I addr
Packit 7cfc04
could be matched to a shared object,
Packit 7cfc04
but not to a symbol in the shared object, then the
Packit 7cfc04
.I info->dli_sname
Packit 7cfc04
and
Packit 7cfc04
.I info->dli_saddr
Packit 7cfc04
fields are set to NULL.
Packit 7cfc04
.PP
Packit 7cfc04
If the address specified in
Packit 7cfc04
.I addr
Packit 7cfc04
could not be matched to a shared object, then these functions return 0.
Packit 7cfc04
In this case, an error message is
Packit 7cfc04
.I not
Packit 7cfc04
.\" According to the FreeBSD man page, dladdr1() does signal an
Packit 7cfc04
.\" error via dlerror() for this case.
Packit 7cfc04
available via
Packit 7cfc04
.BR dlerror (3).
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.BR dladdr ()
Packit 7cfc04
is present in glibc 2.0 and later.
Packit 7cfc04
.BR dladdr1 ()
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
lbw19 lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR dladdr (),
Packit 7cfc04
.BR dladdr1 ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
These functions are nonstandard GNU extensions
Packit 7cfc04
that are also present on Solaris.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
Sometimes, the function pointers you pass to
Packit 7cfc04
.BR dladdr ()
Packit 7cfc04
may surprise you.
Packit 7cfc04
On some architectures (notably i386 and x86-64),
Packit 7cfc04
.I dli_fname
Packit 7cfc04
and
Packit 7cfc04
.I dli_fbase
Packit 7cfc04
may end up pointing back at the object from which you called
Packit 7cfc04
.BR dladdr (),
Packit 7cfc04
even if the function used as an argument should come from
Packit 7cfc04
a dynamically linked library.
Packit 7cfc04
.PP
Packit 7cfc04
The problem is that the function pointer will still be resolved
Packit 7cfc04
at compile time, but merely point to the
Packit 7cfc04
.I plt
Packit 7cfc04
(Procedure Linkage Table)
Packit 7cfc04
section of the original object (which dispatches the call after
Packit 7cfc04
asking the dynamic linker to resolve the symbol).
Packit 7cfc04
To work around this,
Packit 7cfc04
you can try to compile the code to be position-independent:
Packit 7cfc04
then, the compiler cannot prepare the pointer
Packit 7cfc04
at compile time any more and
Packit 7cfc04
.BR gcc (1)
Packit 7cfc04
will generate code that just loads the final symbol address from the
Packit 7cfc04
.I got
Packit 7cfc04
(Global Offset Table) at run time before passing it to
Packit 7cfc04
.BR dladdr ().
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR dl_iterate_phdr (3),
Packit 7cfc04
.BR dlinfo (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/.