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