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 February 10, 2011 19.Os 20.Dt DL_ITERATE_PHDR 3 21.Sh NAME 22.Nm dl_iterate_phdr 23.Nd iterate over program headers 24.Sh SYNOPSIS 25.In link.h 26.Ft int 27.Fn dl_iterate_phdr "int (*callback)(struct dl_phdr_info *, size_t, void*)" "void *data" 28.Sh DESCRIPTION 29The 30.Fn dl_iterate_phdr 31function iterates over all shared objects loaded into a process's 32address space, calling 33.Fa callback 34for each shared object, passing it information about the object's 35program headers and the 36.Fa data 37argument. 38The information about the program headers is passed in a structure 39that is defined as: 40.Bd -literal 41struct dl_phdr_info { 42 Elf_Addr dlpi_addr; 43 const char *dlpi_name; 44 const Elf_Phdr *dlpi_phdr; 45 Elf_Half dlpi_phnum; 46 unsigned long long int dlpi_adds; 47 unsigned long long int dlpi_subs; 48 size_t dlpi_tls_modid; 49 void *dlpi_tls_data; 50}; 51.Ed 52.Pp 53The members of 54.Li struct dl_phdr_info 55have the following meaning: 56.Bl -tag -width XXXdlpi_phdr 57.It Fa dlpi_addr 58The base address at which the shared object is mapped into the address 59space of the calling process. 60.It Fa dlpi_name 61The name of the shared object. 62.It Fa dlpi_phdr 63A pointer to the shared object's program headers. 64.It Fa dlpi_phnum 65The number of program headers in the shared object. 66.It Fa dlpi_adds 67The number of objects added into the main program. 68.It Fa dlpi_subs 69The number of objects removed from the main program. 70.El 71.Pp 72To make it possible for programs to check whether any new members have 73been added, the size of the structure is passed as an argument to 74.Fa callback . 75.Sh EXAMPLE 76The following program displays a list of pathnames of the shared objects it has 77loaded. For each shared object, the program lists the virtual addresses at 78which the object's ELF segments are loaded. 79.Bd -literal 80#include <link.h> 81#include <stdlib.h> 82#include <stdio.h> 83 84static int 85callback(struct dl_phdr_info *info, size_t size, void *data) 86{ 87 int j; 88 printf("name=%s (%d segments)\n", info->dlpi_name, 89 info->dlpi_phnum); 90 for (j = 0; j < info->dlpi_phnum; j++) 91 printf("\t\t header %2d: address=%10p\n", j, 92 (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr)); 93 return 0; 94} 95 96int 97main(int argc, char *argv[]) 98{ 99 dl_iterate_phdr(callback, NULL); 100 exit(EXIT_SUCCESS); 101} 102.Ed 103.Sh RETURN VALUE 104The 105.Fn dl_iterate_phdr 106function returns whatever value was returned by the last call to callback. 107.Sh SEE ALSO 108.Xr ld 1 , 109.Xr ld-elf.so 1 , 110.Xr dlfcn 3 , 111.Xr elf 5 112.Sh HISTORY 113The 114.Nm 115function first appeared in 116.Dx 2.9 . 117