xref: /aosp_15_r20/bionic/libc/include/link.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2012 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #pragma once
30*8d67ca89SAndroid Build Coastguard Worker 
31*8d67ca89SAndroid Build Coastguard Worker /**
32*8d67ca89SAndroid Build Coastguard Worker  * @file link.h
33*8d67ca89SAndroid Build Coastguard Worker  * @brief Extra dynamic linker functionality (see also <dlfcn.h>).
34*8d67ca89SAndroid Build Coastguard Worker  */
35*8d67ca89SAndroid Build Coastguard Worker 
36*8d67ca89SAndroid Build Coastguard Worker #include <sys/cdefs.h>
37*8d67ca89SAndroid Build Coastguard Worker 
38*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
39*8d67ca89SAndroid Build Coastguard Worker #include <sys/types.h>
40*8d67ca89SAndroid Build Coastguard Worker 
41*8d67ca89SAndroid Build Coastguard Worker #include <elf.h>
42*8d67ca89SAndroid Build Coastguard Worker 
43*8d67ca89SAndroid Build Coastguard Worker __BEGIN_DECLS
44*8d67ca89SAndroid Build Coastguard Worker 
45*8d67ca89SAndroid Build Coastguard Worker #if defined(__LP64__)
46*8d67ca89SAndroid Build Coastguard Worker /** Convenience macro to get the appropriate 32-bit or 64-bit <elf.h> type for the caller's bitness. */
47*8d67ca89SAndroid Build Coastguard Worker #define ElfW(type) Elf64_ ## type
48*8d67ca89SAndroid Build Coastguard Worker #else
49*8d67ca89SAndroid Build Coastguard Worker /** Convenience macro to get the appropriate 32-bit or 64-bit <elf.h> type for the caller's bitness. */
50*8d67ca89SAndroid Build Coastguard Worker #define ElfW(type) Elf32_ ## type
51*8d67ca89SAndroid Build Coastguard Worker #endif
52*8d67ca89SAndroid Build Coastguard Worker 
53*8d67ca89SAndroid Build Coastguard Worker /**
54*8d67ca89SAndroid Build Coastguard Worker  * Information passed by dl_iterate_phdr() to the callback.
55*8d67ca89SAndroid Build Coastguard Worker  */
56*8d67ca89SAndroid Build Coastguard Worker struct dl_phdr_info {
57*8d67ca89SAndroid Build Coastguard Worker   /** The address of the shared object. */
58*8d67ca89SAndroid Build Coastguard Worker   ElfW(Addr) dlpi_addr;
59*8d67ca89SAndroid Build Coastguard Worker   /** The name of the shared object. */
60*8d67ca89SAndroid Build Coastguard Worker   const char* _Nullable dlpi_name;
61*8d67ca89SAndroid Build Coastguard Worker   /** Pointer to the shared object's program headers. */
62*8d67ca89SAndroid Build Coastguard Worker   const ElfW(Phdr)* _Nullable dlpi_phdr;
63*8d67ca89SAndroid Build Coastguard Worker   /** Number of program headers pointed to by `dlpi_phdr`. */
64*8d67ca89SAndroid Build Coastguard Worker   ElfW(Half) dlpi_phnum;
65*8d67ca89SAndroid Build Coastguard Worker 
66*8d67ca89SAndroid Build Coastguard Worker   /**
67*8d67ca89SAndroid Build Coastguard Worker    * The total number of library load events at the time dl_iterate_phdr() was
68*8d67ca89SAndroid Build Coastguard Worker    * called.
69*8d67ca89SAndroid Build Coastguard Worker    *
70*8d67ca89SAndroid Build Coastguard Worker    * This field is only available since API level 30; you can use the size
71*8d67ca89SAndroid Build Coastguard Worker    * passed to the callback to determine whether you have the full struct,
72*8d67ca89SAndroid Build Coastguard Worker    * or just the fields up to and including `dlpi_phnum`.
73*8d67ca89SAndroid Build Coastguard Worker    */
74*8d67ca89SAndroid Build Coastguard Worker   unsigned long long dlpi_adds;
75*8d67ca89SAndroid Build Coastguard Worker   /**
76*8d67ca89SAndroid Build Coastguard Worker    * The total number of library unload events at the time dl_iterate_phdr() was
77*8d67ca89SAndroid Build Coastguard Worker    * called.
78*8d67ca89SAndroid Build Coastguard Worker    *
79*8d67ca89SAndroid Build Coastguard Worker    * This field is only available since API level 30; you can use the size
80*8d67ca89SAndroid Build Coastguard Worker    * passed to the callback to determine whether you have the full struct,
81*8d67ca89SAndroid Build Coastguard Worker    * or just the fields up to and including `dlpi_phnum`.
82*8d67ca89SAndroid Build Coastguard Worker    */
83*8d67ca89SAndroid Build Coastguard Worker   unsigned long long dlpi_subs;
84*8d67ca89SAndroid Build Coastguard Worker   /**
85*8d67ca89SAndroid Build Coastguard Worker    * The module ID for TLS relocations in this shared object.
86*8d67ca89SAndroid Build Coastguard Worker    *
87*8d67ca89SAndroid Build Coastguard Worker    * This field is only available since API level 30; you can use the size
88*8d67ca89SAndroid Build Coastguard Worker    * passed to the callback to determine whether you have the full struct,
89*8d67ca89SAndroid Build Coastguard Worker    * or just the fields up to and including `dlpi_phnum`.
90*8d67ca89SAndroid Build Coastguard Worker    */
91*8d67ca89SAndroid Build Coastguard Worker   size_t dlpi_tls_modid;
92*8d67ca89SAndroid Build Coastguard Worker   /**
93*8d67ca89SAndroid Build Coastguard Worker    * The caller's TLS data for this shared object.
94*8d67ca89SAndroid Build Coastguard Worker    *
95*8d67ca89SAndroid Build Coastguard Worker    * This field is only available since API level 30; you can use the size
96*8d67ca89SAndroid Build Coastguard Worker    * passed to the callback to determine whether you have the full struct,
97*8d67ca89SAndroid Build Coastguard Worker    * or just the fields up to and including `dlpi_phnum`.
98*8d67ca89SAndroid Build Coastguard Worker    */
99*8d67ca89SAndroid Build Coastguard Worker   void* _Nullable dlpi_tls_data;
100*8d67ca89SAndroid Build Coastguard Worker };
101*8d67ca89SAndroid Build Coastguard Worker 
102*8d67ca89SAndroid Build Coastguard Worker /**
103*8d67ca89SAndroid Build Coastguard Worker  * [dl_iterate_phdr(3)](https://man7.org/linux/man-pages/man3/dl_iterate_phdr.3.html)
104*8d67ca89SAndroid Build Coastguard Worker  * calls the given callback once for every loaded shared object. The size
105*8d67ca89SAndroid Build Coastguard Worker  * argument to the callback lets you determine whether you have a smaller
106*8d67ca89SAndroid Build Coastguard Worker  * `dl_phdr_info` from before API level 30, or the newer full one.
107*8d67ca89SAndroid Build Coastguard Worker  * The data argument to the callback is whatever you pass as the data argument
108*8d67ca89SAndroid Build Coastguard Worker  * to dl_iterate_phdr().
109*8d67ca89SAndroid Build Coastguard Worker  *
110*8d67ca89SAndroid Build Coastguard Worker  * Returns the value returned by the final call to the callback.
111*8d67ca89SAndroid Build Coastguard Worker  */
112*8d67ca89SAndroid Build Coastguard Worker int dl_iterate_phdr(int (* _Nonnull __callback)(struct dl_phdr_info* _Nonnull __info, size_t __size, void* _Nullable __data), void* _Nullable __data);
113*8d67ca89SAndroid Build Coastguard Worker 
114*8d67ca89SAndroid Build Coastguard Worker #ifdef __arm__
115*8d67ca89SAndroid Build Coastguard Worker typedef uintptr_t _Unwind_Ptr;
116*8d67ca89SAndroid Build Coastguard Worker _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr, int* _Nonnull);
117*8d67ca89SAndroid Build Coastguard Worker #endif
118*8d67ca89SAndroid Build Coastguard Worker 
119*8d67ca89SAndroid Build Coastguard Worker /** Used by the dynamic linker to communicate with the debugger. */
120*8d67ca89SAndroid Build Coastguard Worker struct link_map {
121*8d67ca89SAndroid Build Coastguard Worker   ElfW(Addr) l_addr;
122*8d67ca89SAndroid Build Coastguard Worker   char* _Nullable l_name;
123*8d67ca89SAndroid Build Coastguard Worker   ElfW(Dyn)* _Nullable l_ld;
124*8d67ca89SAndroid Build Coastguard Worker   struct link_map* _Nullable l_next;
125*8d67ca89SAndroid Build Coastguard Worker   struct link_map* _Nullable l_prev;
126*8d67ca89SAndroid Build Coastguard Worker };
127*8d67ca89SAndroid Build Coastguard Worker 
128*8d67ca89SAndroid Build Coastguard Worker /** Used by the dynamic linker to communicate with the debugger. */
129*8d67ca89SAndroid Build Coastguard Worker struct r_debug {
130*8d67ca89SAndroid Build Coastguard Worker   int32_t r_version;
131*8d67ca89SAndroid Build Coastguard Worker   struct link_map* _Nullable r_map;
132*8d67ca89SAndroid Build Coastguard Worker   ElfW(Addr) r_brk;
133*8d67ca89SAndroid Build Coastguard Worker   enum {
134*8d67ca89SAndroid Build Coastguard Worker     RT_CONSISTENT,
135*8d67ca89SAndroid Build Coastguard Worker     RT_ADD,
136*8d67ca89SAndroid Build Coastguard Worker     RT_DELETE
137*8d67ca89SAndroid Build Coastguard Worker   } r_state;
138*8d67ca89SAndroid Build Coastguard Worker   ElfW(Addr) r_ldbase;
139*8d67ca89SAndroid Build Coastguard Worker };
140*8d67ca89SAndroid Build Coastguard Worker 
141*8d67ca89SAndroid Build Coastguard Worker __END_DECLS
142