| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 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 | */ | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | #pragma once | 
|  | 30 |  | 
|  | 31 | /** | 
|  | 32 | * @file link.h | 
|  | 33 | * @brief Extra dynamic linker functionality (see also <dlfcn.h>). | 
|  | 34 | */ | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 35 |  | 
| Elliott Hughes | f2c6ad6 | 2017-04-21 10:25:56 -0700 | [diff] [blame] | 36 | #include <stdint.h> | 
| Elliott Hughes | 203e13d | 2016-07-22 14:56:18 -0700 | [diff] [blame] | 37 | #include <sys/cdefs.h> | 
|  | 38 | #include <sys/types.h> | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 39 |  | 
| Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 40 | #include <elf.h> | 
|  | 41 |  | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 42 | __BEGIN_DECLS | 
|  | 43 |  | 
| Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 44 | #if defined(__LP64__) | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 45 | /** Convenience macro to get the appropriate 32-bit or 64-bit <elf.h> type for the caller's bitness. */ | 
| Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 46 | #define ElfW(type) Elf64_ ## type | 
|  | 47 | #else | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 48 | /** Convenience macro to get the appropriate 32-bit or 64-bit <elf.h> type for the caller's bitness. */ | 
| Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 49 | #define ElfW(type) Elf32_ ## type | 
|  | 50 | #endif | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 51 |  | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 52 | /** | 
|  | 53 | * Information passed by dl_iterate_phdr() to the callback. | 
|  | 54 | */ | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 55 | struct dl_phdr_info { | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 56 | /** The address of the shared object. */ | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 57 | ElfW(Addr) dlpi_addr; | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 58 | /** The name of the shared object. */ | 
| zijunzhao | 0a51582 | 2023-03-17 02:52:15 +0000 | [diff] [blame] | 59 | const char* _Nullable dlpi_name; | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 60 | /** Pointer to the shared object's program headers. */ | 
| zijunzhao | 0a51582 | 2023-03-17 02:52:15 +0000 | [diff] [blame] | 61 | const ElfW(Phdr)* _Nullable dlpi_phdr; | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 62 | /** Number of program headers pointed to by `dlpi_phdr`. */ | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 63 | ElfW(Half) dlpi_phnum; | 
| Ryan Prichard | a2e83ab | 2019-08-16 17:25:43 -0700 | [diff] [blame] | 64 |  | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 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 | */ | 
| Ryan Prichard | a2e83ab | 2019-08-16 17:25:43 -0700 | [diff] [blame] | 73 | unsigned long long dlpi_adds; | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 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 | */ | 
| Ryan Prichard | a2e83ab | 2019-08-16 17:25:43 -0700 | [diff] [blame] | 82 | unsigned long long dlpi_subs; | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 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 | */ | 
| Ryan Prichard | a2e83ab | 2019-08-16 17:25:43 -0700 | [diff] [blame] | 90 | size_t dlpi_tls_modid; | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 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 | */ | 
| zijunzhao | 0a51582 | 2023-03-17 02:52:15 +0000 | [diff] [blame] | 98 | void* _Nullable dlpi_tls_data; | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 99 | }; | 
|  | 100 |  | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 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); | 
| Christopher Ferris | 24053a4 | 2013-08-19 17:45:09 -0700 | [diff] [blame] | 112 |  | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 113 | #ifdef __arm__ | 
| Elliott Hughes | f2c6ad6 | 2017-04-21 10:25:56 -0700 | [diff] [blame] | 114 | typedef uintptr_t _Unwind_Ptr; | 
| zijunzhao | 0a51582 | 2023-03-17 02:52:15 +0000 | [diff] [blame] | 115 | _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr, int* _Nonnull); | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 116 | #endif | 
|  | 117 |  | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 118 | /** Used by the dynamic linker to communicate with the debugger. */ | 
| Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 119 | struct link_map { | 
|  | 120 | ElfW(Addr) l_addr; | 
| zijunzhao | 0a51582 | 2023-03-17 02:52:15 +0000 | [diff] [blame] | 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; | 
| Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 125 | }; | 
|  | 126 |  | 
| Elliott Hughes | 72b10fc | 2024-05-16 18:10:23 +0000 | [diff] [blame] | 127 | /** Used by the dynamic linker to communicate with the debugger. */ | 
| Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 128 | struct r_debug { | 
|  | 129 | int32_t r_version; | 
| zijunzhao | 0a51582 | 2023-03-17 02:52:15 +0000 | [diff] [blame] | 130 | struct link_map* _Nullable r_map; | 
| Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 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 |  | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 140 | __END_DECLS |