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 <sys/cdefs.h> 37 38 #include <stdint.h> 39 #include <sys/types.h> 40 41 #include <elf.h> 42 43 __BEGIN_DECLS 44 45 #if defined(__LP64__) 46 /** Convenience macro to get the appropriate 32-bit or 64-bit <elf.h> type for the caller's bitness. */ 47 #define ElfW(type) Elf64_ ## type 48 #else 49 /** Convenience macro to get the appropriate 32-bit or 64-bit <elf.h> type for the caller's bitness. */ 50 #define ElfW(type) Elf32_ ## type 51 #endif 52 53 /** 54 * Information passed by dl_iterate_phdr() to the callback. 55 */ 56 struct dl_phdr_info { 57 /** The address of the shared object. */ 58 ElfW(Addr) dlpi_addr; 59 /** The name of the shared object. */ 60 const char* _Nullable dlpi_name; 61 /** Pointer to the shared object's program headers. */ 62 const ElfW(Phdr)* _Nullable dlpi_phdr; 63 /** Number of program headers pointed to by `dlpi_phdr`. */ 64 ElfW(Half) dlpi_phnum; 65 66 /** 67 * The total number of library load events at the time dl_iterate_phdr() was 68 * called. 69 * 70 * This field is only available since API level 30; you can use the size 71 * passed to the callback to determine whether you have the full struct, 72 * or just the fields up to and including `dlpi_phnum`. 73 */ 74 unsigned long long dlpi_adds; 75 /** 76 * The total number of library unload events at the time dl_iterate_phdr() was 77 * called. 78 * 79 * This field is only available since API level 30; you can use the size 80 * passed to the callback to determine whether you have the full struct, 81 * or just the fields up to and including `dlpi_phnum`. 82 */ 83 unsigned long long dlpi_subs; 84 /** 85 * The module ID for TLS relocations in this shared object. 86 * 87 * This field is only available since API level 30; you can use the size 88 * passed to the callback to determine whether you have the full struct, 89 * or just the fields up to and including `dlpi_phnum`. 90 */ 91 size_t dlpi_tls_modid; 92 /** 93 * The caller's TLS data for this shared object. 94 * 95 * This field is only available since API level 30; you can use the size 96 * passed to the callback to determine whether you have the full struct, 97 * or just the fields up to and including `dlpi_phnum`. 98 */ 99 void* _Nullable dlpi_tls_data; 100 }; 101 102 /** 103 * [dl_iterate_phdr(3)](https://man7.org/linux/man-pages/man3/dl_iterate_phdr.3.html) 104 * calls the given callback once for every loaded shared object. The size 105 * argument to the callback lets you determine whether you have a smaller 106 * `dl_phdr_info` from before API level 30, or the newer full one. 107 * The data argument to the callback is whatever you pass as the data argument 108 * to dl_iterate_phdr(). 109 * 110 * Returns the value returned by the final call to the callback. 111 */ 112 int dl_iterate_phdr(int (* _Nonnull __callback)(struct dl_phdr_info* _Nonnull __info, size_t __size, void* _Nullable __data), void* _Nullable __data); 113 114 #ifdef __arm__ 115 typedef uintptr_t _Unwind_Ptr; 116 _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr, int* _Nonnull); 117 #endif 118 119 /** Used by the dynamic linker to communicate with the debugger. */ 120 struct link_map { 121 ElfW(Addr) l_addr; 122 char* _Nullable l_name; 123 ElfW(Dyn)* _Nullable l_ld; 124 struct link_map* _Nullable l_next; 125 struct link_map* _Nullable l_prev; 126 }; 127 128 /** Used by the dynamic linker to communicate with the debugger. */ 129 struct r_debug { 130 int32_t r_version; 131 struct link_map* _Nullable r_map; 132 ElfW(Addr) r_brk; 133 enum { 134 RT_CONSISTENT, 135 RT_ADD, 136 RT_DELETE 137 } r_state; 138 ElfW(Addr) r_ldbase; 139 }; 140 141 __END_DECLS 142