1DL_ITERATE_PHDR(3)         Library Functions 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
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.
20
21       The dl_iterate_phdr() function walks through the list  of  an  applica‐
22       tion's  shared  objects  and  calls the function callback once for each
23       object, until either all shared objects have been processed or callback
24       returns a non-zero value.
25
26       Each  call  to  callback  receives  three  arguments:  info, which is a
27       pointer to a structure containing information about the shared  object;
28       size,  which is the size of the structure pointed to by info; and data,
29       which is a copy of whatever value was passed by the calling program  as
30       the second argument (also named data) in the call to dl_iterate_phdr().
31
32       The info argument is a structure of the following type:
33
34         struct dl_phdr_info {
35           ElfW(Addr)        dlpi_addr;  /* Base address of object */
36           const char       *dlpi_name;  /* (Null-terminated) name of
37                                            object
38           const ElfW(Phdr) *dlpi_phdr;  /* Pointer to array of
39                                            ELF program headers
40                                            for this object */
41           ElfW(Half)        dlpi_phnum; /* # of items in 'dlpi_phdr' */
42         };
43
44       (The ElfW() macro definition turns its argument into the name of an ELF
45       data type suitable for the hardware architecture.  For  example,  on  a
46       32-bit platform, ElfW(Addr) yields the data type name Elf32_Addr.  Fur‐
47       ther information on these  types  can  be  found  in  the  <elf.h>  and
48       <link.h> header files.)
49
50       The  dlpi_addr  field  indicates  the base address of the shared object
51       (i.e., the difference between the virtual memory address of the  shared
52       object  and  the  offset  of  that object in the file from which it was
53       loaded).  The dlpi_name field is a null-terminated  string  giving  the
54       pathname from which the shared object was loaded.
55
56       To  understand  the  meaning of the dlpi_phdr and dlpi_phnum fields, we
57       need to be aware that an ELF shared object consists of a number of seg‐
58       ments,  each of which has a corresponding program header describing the
59       segment.  The dlpi_phdr field is a pointer to an array of  the  program
60       headers  for  this  shared  object.  The dlpi_phnum field indicates the
61       size of this array.
62
63       These program headers are structures of the following form:
64
65         typedef struct
66         {
67           Elf32_Word  p_type;    /* Segment type */
68           Elf32_Off   p_offset;  /* Segment file offset */
69           Elf32_Addr  p_vaddr;   /* Segment virtual address */
70           Elf32_Addr  p_paddr;   /* Segment physical address */
71           Elf32_Word  p_filesz;  /* Segment size in file */
72           Elf32_Word  p_memsz;   /* Segment size in memory */
73           Elf32_Word  p_flags;   /* Segment flags */
74           Elf32_Word  p_align;   /* Segment alignment */
75         } Elf32_Phdr;
76
77       Note that we can calculate the location of a particular program header,
78       x, in virtual memory using the formula:
79
80         addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr;
81

EXAMPLE

83       The  following  program  displays  a  list  of  pathnames of the shared
84       objects it has loaded.  For each shared object, the program  lists  the
85       virtual addresses at which the object's ELF segments are loaded.
86
87       #define _GNU_SOURCE
88       #include <link.h>
89       #include <stdlib.h>
90       #include <stdio.h>
91
92       static int
93       callback(struct dl_phdr_info *info, size_t size, void *data)
94       {
95           int j;
96
97           printf("name=%s (%d segments)\n", info->dlpi_name,
98               info->dlpi_phnum);
99
100           for (j = 0; j < info->dlpi_phnum; j++)
101                printf("\t\t header %2d: address=%10p\n", j,
102                    (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));
103           return 0;
104       }
105
106       int
107       main(int argc, char *argv[])
108       {
109           dl_iterate_phdr(callback, NULL);
110
111           exit(EXIT_SUCCESS);
112       }
113

RETURN VALUE

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

CONFORMING TO

119       The dl_iterate_phdr() function is Linux specific and should be  avoided
120       in portable applications.
121

SEE ALSO

123       ldd(1),   objdump(1),  readelf(1),  dlopen(3),  feature_test_macros(7),
124       ld.so(8), and the Executable and Linking Format Specification available
125       at various locations online.
126
127
128
129Linux Programmer's Manual        Linux 2.4.21               DL_ITERATE_PHDR(3)
Impressum