1.\" $NetBSD: dl_iterate_phdr.3,v 1.2 2010/10/16 12:05:48 wiz Exp $ 2.\" $OpenBSD: dl_iterate_phdr.3,v 1.3 2007/05/31 19:19:48 jmc Exp $ 3.\" 4.\" Copyright (c) 2005 Mark Kettenis 5.\" 6.\" Permission to use, copy, modify, and distribute this software for any 7.\" purpose with or without fee is hereby granted, provided that the above 8.\" copyright notice and this permission notice appear in all copies. 9.\" 10.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17.\" 18.Dd April 28, 2011 19.Dt DL_ITERATE_PHDR 3 20.Os 21.Sh NAME 22.Nm dl_iterate_phdr 23.Nd iterate over program headers 24.Sh LIBRARY 25This function is not in a library. 26It is included in every dynamically linked program automatically. 27.Sh SYNOPSIS 28.In link.h 29.Ft int 30.Fn dl_iterate_phdr "int (*callback)(struct dl_phdr_info *, size_t, void*)" "void *data" 31.Sh DESCRIPTION 32The 33.Fn dl_iterate_phdr 34function iterates over all shared objects loaded into a process's 35address space, calling 36.Fa callback 37for each shared object, passing it information about the object's 38program headers and the 39.Fa data 40argument. 41The information about the program headers is passed in a structure 42that is defined as: 43.Bd -literal 44struct dl_phdr_info { 45 Elf_Addr dlpi_addr; 46 const char *dlpi_name; 47 const Elf_Phdr *dlpi_phdr; 48 Elf_Half dlpi_phnum; 49 unsigned long long int dlpi_adds; 50 unsigned long long int dlpi_subs; 51 size_t dlpi_tls_modid; 52 void *dlpi_tls_data; 53}; 54.Ed 55.Pp 56The members of 57.Li struct dl_phdr_info 58have the following meaning: 59.Bl -tag -width XXXdlpi_phdr 60.It Fa dlpi_addr 61The base address at which the shared object is mapped into the address 62space of the calling process. 63.It Fa dlpi_name 64The name of the shared object. 65.It Fa dlpi_phdr 66A pointer to the shared object's program headers. 67.It Fa dlpi_phnum 68The number of program headers in the shared object. 69.It Fa dlpi_adds 70The number of objects added into the main program. 71.It Fa dlpi_subs 72The number of objects removed from the main program. 73.El 74.Pp 75To make it possible for programs to check whether any new members have 76been added, the size of the structure is passed as an argument to 77.Fa callback . 78.Sh EXAMPLE 79The following program displays a list of pathnames of the shared objects it has 80loaded. For each shared object, the program lists the virtual addresses at 81which the object's ELF segments are loaded. 82.Bd -literal 83#include <link.h> 84#include <stdlib.h> 85#include <stdio.h> 86 87static int 88callback(struct dl_phdr_info *info, size_t size, void *data) 89{ 90 int j; 91 printf("name=%s (%d segments)\en", info->dlpi_name, 92 info->dlpi_phnum); 93 for (j = 0; j < info->dlpi_phnum; j++) 94 printf("\t\t header %2d: address=%10p\en", j, 95 (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr)); 96 return 0; 97} 98 99int 100main(int argc, char *argv[]) 101{ 102 dl_iterate_phdr(callback, NULL); 103 exit(EXIT_SUCCESS); 104} 105.Ed 106.Sh RETURN VALUE 107The 108.Fn dl_iterate_phdr 109function returns whatever value was returned by the last call to callback. 110.Sh SEE ALSO 111.Xr ld 1 , 112.Xr ld-elf.so.2 1 , 113.Xr dlfcn 3 , 114.Xr elf 5 115.Sh HISTORY 116The 117.Nm 118function first appeared in 119.Dx 2.9 . 120