1DL_ITERATE_PHDR(3)         Linux Programmer's Manual        DL_ITERATE_PHDR(3)
2
3
4

NAME

6       dl_iterate_phdr - walk through list of shared objects
7

SYNOPSIS

9       #define _GNU_SOURCE         /* See feature_test_macros(7) */
10       #include <link.h>
11
12       int dl_iterate_phdr(
13                 int (*callback) (struct dl_phdr_info *info,
14                                  size_t size, void *data),
15                 void *data);
16

DESCRIPTION

18       The  dl_iterate_phdr() function allows an application to inquire at run
19       time to find out which shared objects it has loaded, and the  order  in
20       which they were loaded.
21
22       The  dl_iterate_phdr()  function  walks through the list of an applica‐
23       tion's shared objects and calls the function  callback  once  for  each
24       object, until either all shared objects have been processed or callback
25       returns a nonzero value.
26
27       Each call to callback  receives  three  arguments:  info,  which  is  a
28       pointer  to a structure containing information about the shared object;
29       size, which is the size of the structure pointed to by info; and  data,
30       which  is a copy of whatever value was passed by the calling program as
31       the second argument (also named data) in the call to dl_iterate_phdr().
32
33       The info argument is a structure of the following type:
34
35           struct dl_phdr_info {
36               ElfW(Addr)        dlpi_addr;  /* Base address of object */
37               const char       *dlpi_name;  /* (Null-terminated) name of
38                                                object */
39               const ElfW(Phdr) *dlpi_phdr;  /* Pointer to array of
40                                                ELF program headers
41                                                for this object */
42               ElfW(Half)        dlpi_phnum; /* # of items in dlpi_phdr */
43
44               /* The following fields were added in glibc 2.4, after the first
45                  version of this structure was available.  Check the size
46                  argument passed to the dl_iterate_phdr callback to determine
47                  whether or not each later member is available.  */
48
49               unsigned long long int dlpi_adds;
50                               /* Incremented when a new object may
51                                  have been added */
52               unsigned long long int dlpi_subs;
53                               /* Incremented when an object may
54                                  have been removed */
55               size_t dlpi_tls_modid;
56                               /* If there is a PT_TLS segment, its module
57                                  ID as used in TLS relocations, else zero */
58               void  *dlpi_tls_data;
59                               /* The address of the calling thread's instance
60                                  of this module's PT_TLS segment, if it has
61                                  one and it has been allocated in the calling
62                                  thread, otherwise a null pointer */
63           };
64
65       (The ElfW() macro definition turns its argument into the name of an ELF
66       data  type  suitable  for the hardware architecture.  For example, on a
67       32-bit platform, ElfW(Addr) yields the data type name Elf32_Addr.  Fur‐
68       ther  information  on  these  types  can  be  found  in the <elf.h> and
69       <link.h> header files.)
70
71       The dlpi_addr field indicates the base address  of  the  shared  object
72       (i.e.,  the difference between the virtual memory address of the shared
73       object and the offset of that object in the  file  from  which  it  was
74       loaded).   The  dlpi_name  field is a null-terminated string giving the
75       pathname from which the shared object was loaded.
76
77       To understand the meaning of the dlpi_phdr and  dlpi_phnum  fields,  we
78       need to be aware that an ELF shared object consists of a number of seg‐
79       ments, each of which has a corresponding program header describing  the
80       segment.   The  dlpi_phdr field is a pointer to an array of the program
81       headers for this shared object.  The  dlpi_phnum  field  indicates  the
82       size of this array.
83
84       These program headers are structures of the following form:
85
86           typedef struct {
87               Elf32_Word  p_type;    /* Segment type */
88               Elf32_Off   p_offset;  /* Segment file offset */
89               Elf32_Addr  p_vaddr;   /* Segment virtual address */
90               Elf32_Addr  p_paddr;   /* Segment physical address */
91               Elf32_Word  p_filesz;  /* Segment size in file */
92               Elf32_Word  p_memsz;   /* Segment size in memory */
93               Elf32_Word  p_flags;   /* Segment flags */
94               Elf32_Word  p_align;   /* Segment alignment */
95           } Elf32_Phdr;
96
97       Note that we can calculate the location of a particular program header,
98       x, in virtual memory using the formula:
99
100           addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr;
101
102       Possible values for p_type include the following (see <elf.h> for  fur‐
103       ther details):
104
105           #define PT_LOAD         1    /* Loadable program segment */
106           #define PT_DYNAMIC      2    /* Dynamic linking information */
107           #define PT_INTERP       3    /* Program interpreter */
108           #define PT_NOTE         4    /* Auxiliary information */
109           #define PT_SHLIB        5    /* Reserved */
110           #define PT_PHDR         6    /* Entry for header table itself */
111           #define PT_TLS          7    /* Thread-local storage segment */
112           #define PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */
113           #define PT_GNU_STACK  0x6474e551 /* Indicates stack executability */
114           #define PT_GNU_RELRO  0x6474e552 /* Read-only after relocation */
115

RETURN VALUE

117       The  dl_iterate_phdr()  function returns whatever value was returned by
118       the last call to callback.
119

VERSIONS

121       dl_iterate_phdr() has been supported in glibc since version 2.2.4.
122

ATTRIBUTES

124       For  an  explanation  of  the  terms  used   in   this   section,   see
125       attributes(7).
126
127       ┌──────────────────┬───────────────┬─────────┐
128Interface         Attribute     Value   
129       ├──────────────────┼───────────────┼─────────┤
130dl_iterate_phdr() │ Thread safety │ MT-Safe │
131       └──────────────────┴───────────────┴─────────┘
132

CONFORMING TO

134       The dl_iterate_phdr() function is not specified in any standard.  Vari‐
135       ous other systems provide a version of this function, although  details
136       of  the  returned  dl_phdr_info  structure  differ.   On  the  BSDs and
137       Solaris,  the  structure  includes  the  fields  dlpi_addr,  dlpi_name,
138       dlpi_phdr,  and dlpi_phnum in addition to other implementation-specific
139       fields.
140

NOTES

142       Future versions of  the  C  library  may  add  further  fields  to  the
143       dl_phdr_info  structure;  in  that  event, the size argument provides a
144       mechanism for the callback function to discover whether it  is  running
145       on a system with added fields.
146
147       The first object visited by callback is the main program.  For the main
148       program, the dlpi_name field will be an empty string.
149

EXAMPLE

151       The following program displays  a  list  of  pathnames  of  the  shared
152       objects  it has loaded.  For each shared object, the program lists some
153       information (virtual address, size, flags, and type) for  each  of  the
154       objects ELF segments.
155
156       The  following  shell  session  demonstrates the output produced by the
157       program on an x86-64 system.  The first shared object for which  output
158       is displayed (where the name is an empty string) is the main program.
159
160           $ ./a.out
161           Name: "" (9 segments)
162                0: [      0x400040; memsz:    1f8] flags: 0x5; PT_PHDR
163                1: [      0x400238; memsz:     1c] flags: 0x4; PT_INTERP
164                2: [      0x400000; memsz:    ac4] flags: 0x5; PT_LOAD
165                3: [      0x600e10; memsz:    240] flags: 0x6; PT_LOAD
166                4: [      0x600e28; memsz:    1d0] flags: 0x6; PT_DYNAMIC
167                5: [      0x400254; memsz:     44] flags: 0x4; PT_NOTE
168                6: [      0x400970; memsz:     3c] flags: 0x4; PT_GNU_EH_FRAME
169                7: [         (nil); memsz:      0] flags: 0x6; PT_GNU_STACK
170                8: [      0x600e10; memsz:    1f0] flags: 0x4; PT_GNU_RELRO
171           Name: "linux-vdso.so.1" (4 segments)
172                0: [0x7ffc6edd1000; memsz:    e89] flags: 0x5; PT_LOAD
173                1: [0x7ffc6edd1360; memsz:    110] flags: 0x4; PT_DYNAMIC
174                2: [0x7ffc6edd17b0; memsz:     3c] flags: 0x4; PT_NOTE
175                3: [0x7ffc6edd17ec; memsz:     3c] flags: 0x4; PT_GNU_EH_FRAME
176           Name: "/lib64/libc.so.6" (10 segments)
177                0: [0x7f55712ce040; memsz:    230] flags: 0x5; PT_PHDR
178                1: [0x7f557145b980; memsz:     1c] flags: 0x4; PT_INTERP
179                2: [0x7f55712ce000; memsz: 1b6a5c] flags: 0x5; PT_LOAD
180                3: [0x7f55716857a0; memsz:   9240] flags: 0x6; PT_LOAD
181                4: [0x7f5571688b80; memsz:    1f0] flags: 0x6; PT_DYNAMIC
182                5: [0x7f55712ce270; memsz:     44] flags: 0x4; PT_NOTE
183                6: [0x7f55716857a0; memsz:     78] flags: 0x4; PT_TLS
184                7: [0x7f557145b99c; memsz:   544c] flags: 0x4; PT_GNU_EH_FRAME
185                8: [0x7f55712ce000; memsz:      0] flags: 0x6; PT_GNU_STACK
186                9: [0x7f55716857a0; memsz:   3860] flags: 0x4; PT_GNU_RELRO
187           Name: "/lib64/ld-linux-x86-64.so.2" (7 segments)
188                0: [0x7f557168f000; memsz:  20828] flags: 0x5; PT_LOAD
189                1: [0x7f55718afba0; memsz:   15a8] flags: 0x6; PT_LOAD
190                2: [0x7f55718afe10; memsz:    190] flags: 0x6; PT_DYNAMIC
191                3: [0x7f557168f1c8; memsz:     24] flags: 0x4; PT_NOTE
192                4: [0x7f55716acec4; memsz:    604] flags: 0x4; PT_GNU_EH_FRAME
193                5: [0x7f557168f000; memsz:      0] flags: 0x6; PT_GNU_STACK
194                6: [0x7f55718afba0; memsz:    460] flags: 0x4; PT_GNU_RELRO
195
196   Program source
197
198       #define _GNU_SOURCE
199       #include <link.h>
200       #include <stdlib.h>
201       #include <stdio.h>
202
203       static int
204       callback(struct dl_phdr_info *info, size_t size, void *data)
205       {
206           char *type;
207           int p_type, j;
208
209           printf("Name: \"%s\" (%d segments)\n", info->dlpi_name,
210                      info->dlpi_phnum);
211
212           for (j = 0; j < info->dlpi_phnum; j++) {
213               p_type = info->dlpi_phdr[j].p_type;
214               type =  (p_type == PT_LOAD) ? "PT_LOAD" :
215                       (p_type == PT_DYNAMIC) ? "PT_DYNAMIC" :
216                       (p_type == PT_INTERP) ? "PT_INTERP" :
217                       (p_type == PT_NOTE) ? "PT_NOTE" :
218                       (p_type == PT_INTERP) ? "PT_INTERP" :
219                       (p_type == PT_PHDR) ? "PT_PHDR" :
220                       (p_type == PT_TLS) ? "PT_TLS" :
221                       (p_type == PT_GNU_EH_FRAME) ? "PT_GNU_EH_FRAME" :
222                       (p_type == PT_GNU_STACK) ? "PT_GNU_STACK" :
223                       (p_type == PT_GNU_RELRO) ? "PT_GNU_RELRO" : NULL;
224
225               printf("    %2d: [%14p; memsz:%7lx] flags: 0x%x; ", j,
226                       (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr),
227                       info->dlpi_phdr[j].p_memsz,
228                       info->dlpi_phdr[j].p_flags);
229               if (type != NULL)
230                   printf("%s\n", type);
231               else
232                   printf("[other (0x%x)]\n", p_type);
233           }
234
235           return 0;
236       }
237
238       int
239       main(int argc, char *argv[])
240       {
241           dl_iterate_phdr(callback, NULL);
242
243           exit(EXIT_SUCCESS);
244       }
245

SEE ALSO

247       ldd(1), objdump(1), readelf(1), dladdr(3), dlopen(3), elf(5), ld.so(8)
248
249       Executable and Linking Format Specification, available at various loca‐
250       tions online.
251

COLOPHON

253       This page is part of release 5.04 of the Linux  man-pages  project.   A
254       description  of  the project, information about reporting bugs, and the
255       latest    version    of    this    page,    can     be     found     at
256       https://www.kernel.org/doc/man-pages/.
257
258
259
260GNU                               2019-03-06                DL_ITERATE_PHDR(3)
Impressum