xref: /freebsd/lib/libc/gen/dl_iterate_phdr.3 (revision 0a36787e4c1fa0cf77dcf83be0867178476e372b)
1.\" Copyright (c) 2005 Mark Kettenis
2.\" Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
3.\"
4.\" Permission to use, copy, modify, and distribute this software for any
5.\" purpose with or without fee is hereby granted, provided that the above
6.\" copyright notice and this permission notice appear in all copies.
7.\"
8.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15.\"
16.\"   $OpenBSD: dl_iterate_phdr.3,v 1.3 2007/05/31 19:19:48 jmc Exp $
17.\" $FreeBSD$
18.Dd April 5, 2021
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
25For the dynamically linked binaries, the service is provided by
26.Xr ld-elf.so.1 1
27dynamic linker.
28Statically linked programs use an implementation of
29.Fn dl_iterate_phdr
30from libc.
31.Sh SYNOPSIS
32.In link.h
33.Ft int
34.Fn dl_iterate_phdr "int (*callback)(struct dl_phdr_info *, size_t, void *)" "void *data"
35.Sh DESCRIPTION
36The
37.Fn dl_iterate_phdr
38function iterates over all ELF objects loaded into a process's
39address space, calling
40.Fa callback
41for each object, passing it information about the object's
42program headers and the
43.Fa data
44argument.
45The iteration is aborted when all objects are passed, or when the next
46.Fa callback
47call returns non-zero value.
48The information about the program headers is passed in a structure
49that is defined as:
50.Bd -literal
51struct dl_phdr_info {
52	Elf_Addr	dlpi_addr;
53	const char	*dlpi_name;
54	const Elf_Phdr	*dlpi_phdr;
55	Elf_Half	dlpi_phnum;
56	unsigned long long int dlpi_adds;
57	unsigned long long int dlpi_subs;
58	size_t		dlpi_tls_modid;
59	void		*dlpi_tls_data;
60};
61.Ed
62.Pp
63The members of
64.Li struct dl_phdr_info
65have the following meaning:
66.Bl -tag -width dlpi_tls_modid
67.It Fa dlpi_addr
68The base address at which the object is mapped into the address
69space of the calling process.
70.It Fa dlpi_name
71The pathname of the ELF object.
72.It Fa dlpi_phdr
73A pointer to the object's program headers.
74.It Fa dlpi_phnum
75The number of program headers in the object.
76.It Fa dlpi_adds
77The counter of the object loads performed by the dynamic linker.
78.It Fa dlpi_subs
79The counter of the object unloads performed by the dynamic linker.
80.It Fa dlpi_tls_modid
81The TLS index of the object.
82.It Fa dlpi_tls_data
83A pointer to the calling thread' TLS data segment for this module,
84if it was allocated,
85.Dv NULL
86otherwise.
87.El
88.Pp
89Future versions of
90.Fx
91might add more members to this structure.
92To make it possible for programs to check whether any new members have
93been added, the size of the structure is passed as an second argument to
94.Fa callback .
95.Pp
96The third argument to callback is the
97.Fa data
98value passed to the call to
99.Fn dl_iterate_phdr ,
100allowing the
101.Fa callback
102to have a context.
103.Sh RETURN VALUES
104The
105.Fn dl_iterate_phdr
106returns the value returned by the last
107.Fa callback
108call executed.
109.Sh SEE ALSO
110.Xr ld 1 ,
111.Xr ld-elf.so.1 1 ,
112.Xr dlopen 3 ,
113.Xr elf 5
114.Sh HISTORY
115The
116.Nm
117function first appeared in
118.Fx 7.0 .
119