Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include "linker_relocate.h" |
| 30 | |
| 31 | #include <elf.h> |
| 32 | #include <link.h> |
| 33 | |
| 34 | #include <type_traits> |
| 35 | |
| 36 | #include "linker.h" |
| 37 | #include "linker_debug.h" |
| 38 | #include "linker_globals.h" |
| 39 | #include "linker_gnu_hash.h" |
| 40 | #include "linker_phdr.h" |
| 41 | #include "linker_relocs.h" |
| 42 | #include "linker_reloc_iterators.h" |
| 43 | #include "linker_sleb128.h" |
| 44 | #include "linker_soinfo.h" |
| 45 | #include "private/bionic_globals.h" |
| 46 | |
Florian Mayer | 4edc20d | 2024-10-30 14:24:26 -0700 | [diff] [blame] | 47 | #include <platform/bionic/mte.h> |
| 48 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 49 | static bool is_tls_reloc(ElfW(Word) type) { |
| 50 | switch (type) { |
| 51 | case R_GENERIC_TLS_DTPMOD: |
| 52 | case R_GENERIC_TLS_DTPREL: |
| 53 | case R_GENERIC_TLS_TPREL: |
Elliott Hughes | 4346270 | 2022-10-10 19:21:44 +0000 | [diff] [blame] | 54 | #if defined(R_GENERIC_TLSDESC) |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 55 | case R_GENERIC_TLSDESC: |
Elliott Hughes | 4346270 | 2022-10-10 19:21:44 +0000 | [diff] [blame] | 56 | #endif |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 57 | return true; |
| 58 | default: |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | class Relocator { |
| 64 | public: |
| 65 | Relocator(const VersionTracker& version_tracker, const SymbolLookupList& lookup_list) |
| 66 | : version_tracker(version_tracker), lookup_list(lookup_list) |
| 67 | {} |
| 68 | |
| 69 | soinfo* si = nullptr; |
| 70 | const char* si_strtab = nullptr; |
| 71 | size_t si_strtab_size = 0; |
| 72 | ElfW(Sym)* si_symtab = nullptr; |
| 73 | |
| 74 | const VersionTracker& version_tracker; |
| 75 | const SymbolLookupList& lookup_list; |
| 76 | |
| 77 | // Cache key |
| 78 | ElfW(Word) cache_sym_val = 0; |
| 79 | // Cache value |
| 80 | const ElfW(Sym)* cache_sym = nullptr; |
| 81 | soinfo* cache_si = nullptr; |
| 82 | |
| 83 | std::vector<TlsDynamicResolverArg>* tlsdesc_args; |
| 84 | std::vector<std::pair<TlsDescriptor*, size_t>> deferred_tlsdesc_relocs; |
| 85 | size_t tls_tp_base = 0; |
| 86 | |
| 87 | __attribute__((always_inline)) |
| 88 | const char* get_string(ElfW(Word) index) { |
| 89 | if (__predict_false(index >= si_strtab_size)) { |
| 90 | async_safe_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", |
| 91 | si->get_realpath(), si_strtab_size, index); |
| 92 | } |
| 93 | return si_strtab + index; |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | template <bool DoLogging> |
| 98 | __attribute__((always_inline)) |
| 99 | static inline bool lookup_symbol(Relocator& relocator, uint32_t r_sym, const char* sym_name, |
| 100 | soinfo** found_in, const ElfW(Sym)** sym) { |
| 101 | if (r_sym == relocator.cache_sym_val) { |
| 102 | *found_in = relocator.cache_si; |
| 103 | *sym = relocator.cache_sym; |
| 104 | count_relocation_if<DoLogging>(kRelocSymbolCached); |
| 105 | } else { |
| 106 | const version_info* vi = nullptr; |
| 107 | if (!relocator.si->lookup_version_info(relocator.version_tracker, r_sym, sym_name, &vi)) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | soinfo* local_found_in = nullptr; |
| 112 | const ElfW(Sym)* local_sym = soinfo_do_lookup(sym_name, vi, &local_found_in, relocator.lookup_list); |
| 113 | |
| 114 | relocator.cache_sym_val = r_sym; |
| 115 | relocator.cache_si = local_found_in; |
| 116 | relocator.cache_sym = local_sym; |
| 117 | *found_in = local_found_in; |
| 118 | *sym = local_sym; |
| 119 | } |
| 120 | |
| 121 | if (*sym == nullptr) { |
| 122 | if (ELF_ST_BIND(relocator.si_symtab[r_sym].st_info) != STB_WEAK) { |
| 123 | DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, relocator.si->get_realpath()); |
| 124 | return false; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | count_relocation_if<DoLogging>(kRelocSymbol); |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | enum class RelocMode { |
| 133 | // Fast path for JUMP_SLOT relocations. |
| 134 | JumpTable, |
| 135 | // Fast path for typical relocations: ABSOLUTE, GLOB_DAT, or RELATIVE. |
| 136 | Typical, |
| 137 | // Handle all relocation types, relocations in text sections, and statistics/tracing. |
| 138 | General, |
| 139 | }; |
| 140 | |
| 141 | struct linker_stats_t { |
| 142 | int count[kRelocMax]; |
| 143 | }; |
| 144 | |
| 145 | static linker_stats_t linker_stats; |
| 146 | |
| 147 | void count_relocation(RelocationKind kind) { |
| 148 | ++linker_stats.count[kind]; |
| 149 | } |
| 150 | |
| 151 | void print_linker_stats() { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 152 | LD_DEBUG(statistics, |
| 153 | "RELO STATS: %s: %d abs, %d rel, %d symbol (%d cached)", |
| 154 | g_argv[0], |
| 155 | linker_stats.count[kRelocAbsolute], |
| 156 | linker_stats.count[kRelocRelative], |
| 157 | linker_stats.count[kRelocSymbol], |
| 158 | linker_stats.count[kRelocSymbolCached]); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static bool process_relocation_general(Relocator& relocator, const rel_t& reloc); |
| 162 | |
| 163 | template <RelocMode Mode> |
| 164 | __attribute__((always_inline)) |
| 165 | static bool process_relocation_impl(Relocator& relocator, const rel_t& reloc) { |
| 166 | constexpr bool IsGeneral = Mode == RelocMode::General; |
| 167 | |
Florian Mayer | 4edc20d | 2024-10-30 14:24:26 -0700 | [diff] [blame] | 168 | void* const rel_target = reinterpret_cast<void*>( |
| 169 | relocator.si->apply_memtag_if_mte_globals(reloc.r_offset + relocator.si->load_bias)); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 170 | const uint32_t r_type = ELFW(R_TYPE)(reloc.r_info); |
| 171 | const uint32_t r_sym = ELFW(R_SYM)(reloc.r_info); |
| 172 | |
| 173 | soinfo* found_in = nullptr; |
| 174 | const ElfW(Sym)* sym = nullptr; |
| 175 | const char* sym_name = nullptr; |
| 176 | ElfW(Addr) sym_addr = 0; |
| 177 | |
| 178 | if (r_sym != 0) { |
| 179 | sym_name = relocator.get_string(relocator.si_symtab[r_sym].st_name); |
| 180 | } |
| 181 | |
| 182 | // While relocating a DSO with text relocations (obsolete and 32-bit only), the .text segment is |
| 183 | // writable (but not executable). To call an ifunc, temporarily remap the segment as executable |
| 184 | // (but not writable). Then switch it back to continue applying relocations in the segment. |
| 185 | #if defined(__LP64__) |
| 186 | const bool handle_text_relocs = false; |
| 187 | auto protect_segments = []() { return true; }; |
| 188 | auto unprotect_segments = []() { return true; }; |
| 189 | #else |
| 190 | const bool handle_text_relocs = IsGeneral && relocator.si->has_text_relocations; |
| 191 | auto protect_segments = [&]() { |
| 192 | // Make .text executable. |
| 193 | if (phdr_table_protect_segments(relocator.si->phdr, relocator.si->phnum, |
Kalesh Singh | b23787f | 2024-09-05 08:22:06 +0000 | [diff] [blame] | 194 | relocator.si->load_bias, relocator.si->should_pad_segments(), |
| 195 | relocator.si->should_use_16kib_app_compat()) < 0) { |
Elliott Hughes | f5e21d9 | 2024-07-26 11:48:19 +0000 | [diff] [blame] | 196 | DL_ERR("can't protect segments for \"%s\": %m", relocator.si->get_realpath()); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | return true; |
| 200 | }; |
| 201 | auto unprotect_segments = [&]() { |
| 202 | // Make .text writable. |
| 203 | if (phdr_table_unprotect_segments(relocator.si->phdr, relocator.si->phnum, |
Kalesh Singh | b23787f | 2024-09-05 08:22:06 +0000 | [diff] [blame] | 204 | relocator.si->load_bias, relocator.si->should_pad_segments(), |
| 205 | relocator.si->should_use_16kib_app_compat()) < 0) { |
Elliott Hughes | f5e21d9 | 2024-07-26 11:48:19 +0000 | [diff] [blame] | 206 | DL_ERR("can't unprotect loadable segments for \"%s\": %m", |
| 207 | relocator.si->get_realpath()); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | return true; |
| 211 | }; |
| 212 | #endif |
| 213 | |
Ryan Prichard | 4f14069 | 2020-01-15 14:44:31 -0800 | [diff] [blame] | 214 | // Skip symbol lookup for R_GENERIC_NONE relocations. |
| 215 | if (__predict_false(r_type == R_GENERIC_NONE)) { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 216 | LD_DEBUG(reloc && IsGeneral, "RELO NONE"); |
Ryan Prichard | 4f14069 | 2020-01-15 14:44:31 -0800 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 220 | #if defined(USE_RELA) |
| 221 | auto get_addend_rel = [&]() -> ElfW(Addr) { return reloc.r_addend; }; |
| 222 | auto get_addend_norel = [&]() -> ElfW(Addr) { return reloc.r_addend; }; |
| 223 | #else |
| 224 | auto get_addend_rel = [&]() -> ElfW(Addr) { return *static_cast<ElfW(Addr)*>(rel_target); }; |
| 225 | auto get_addend_norel = [&]() -> ElfW(Addr) { return 0; }; |
| 226 | #endif |
| 227 | |
Ryan Prichard | 8ea6af5 | 2022-03-24 21:14:27 -0700 | [diff] [blame] | 228 | if (!IsGeneral && __predict_false(is_tls_reloc(r_type))) { |
| 229 | // Always process TLS relocations using the slow code path, so that STB_LOCAL symbols are |
| 230 | // diagnosed, and ifunc processing is skipped. |
| 231 | return process_relocation_general(relocator, reloc); |
| 232 | } |
| 233 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 234 | if (IsGeneral && is_tls_reloc(r_type)) { |
| 235 | if (r_sym == 0) { |
| 236 | // By convention in ld.bfd and lld, an omitted symbol on a TLS relocation |
| 237 | // is a reference to the current module. |
| 238 | found_in = relocator.si; |
| 239 | } else if (ELF_ST_BIND(relocator.si_symtab[r_sym].st_info) == STB_LOCAL) { |
| 240 | // In certain situations, the Gold linker accesses a TLS symbol using a |
| 241 | // relocation to an STB_LOCAL symbol in .dynsym of either STT_SECTION or |
| 242 | // STT_TLS type. Bionic doesn't support these relocations, so issue an |
| 243 | // error. References: |
| 244 | // - https://groups.google.com/d/topic/generic-abi/dJ4_Y78aQ2M/discussion |
| 245 | // - https://sourceware.org/bugzilla/show_bug.cgi?id=17699 |
| 246 | sym = &relocator.si_symtab[r_sym]; |
Ryan Prichard | 8ea6af5 | 2022-03-24 21:14:27 -0700 | [diff] [blame] | 247 | auto sym_type = ELF_ST_TYPE(sym->st_info); |
| 248 | if (sym_type == STT_SECTION) { |
| 249 | DL_ERR("unexpected TLS reference to local section in \"%s\": sym type %d, rel type %u", |
| 250 | relocator.si->get_realpath(), sym_type, r_type); |
| 251 | } else { |
| 252 | DL_ERR( |
| 253 | "unexpected TLS reference to local symbol \"%s\" in \"%s\": sym type %d, rel type %u", |
| 254 | sym_name, relocator.si->get_realpath(), sym_type, r_type); |
| 255 | } |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 256 | return false; |
| 257 | } else if (!lookup_symbol<IsGeneral>(relocator, r_sym, sym_name, &found_in, &sym)) { |
| 258 | return false; |
| 259 | } |
| 260 | if (found_in != nullptr && found_in->get_tls() == nullptr) { |
| 261 | // sym_name can be nullptr if r_sym is 0. A linker should never output an ELF file like this. |
| 262 | DL_ERR("TLS relocation refers to symbol \"%s\" in solib \"%s\" with no TLS segment", |
| 263 | sym_name, found_in->get_realpath()); |
| 264 | return false; |
| 265 | } |
| 266 | if (sym != nullptr) { |
| 267 | if (ELF_ST_TYPE(sym->st_info) != STT_TLS) { |
| 268 | // A toolchain should never output a relocation like this. |
| 269 | DL_ERR("reference to non-TLS symbol \"%s\" from TLS relocation in \"%s\"", |
| 270 | sym_name, relocator.si->get_realpath()); |
| 271 | return false; |
| 272 | } |
| 273 | sym_addr = sym->st_value; |
| 274 | } |
| 275 | } else { |
| 276 | if (r_sym == 0) { |
| 277 | // Do nothing. |
| 278 | } else { |
| 279 | if (!lookup_symbol<IsGeneral>(relocator, r_sym, sym_name, &found_in, &sym)) return false; |
| 280 | if (sym != nullptr) { |
| 281 | const bool should_protect_segments = handle_text_relocs && |
| 282 | found_in == relocator.si && |
| 283 | ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC; |
| 284 | if (should_protect_segments && !protect_segments()) return false; |
| 285 | sym_addr = found_in->resolve_symbol_address(sym); |
| 286 | if (should_protect_segments && !unprotect_segments()) return false; |
| 287 | } else if constexpr (IsGeneral) { |
| 288 | // A weak reference to an undefined symbol. We typically use a zero symbol address, but |
| 289 | // use the relocation base for PC-relative relocations, so that the value written is zero. |
| 290 | switch (r_type) { |
| 291 | #if defined(__x86_64__) |
| 292 | case R_X86_64_PC32: |
| 293 | sym_addr = reinterpret_cast<ElfW(Addr)>(rel_target); |
| 294 | break; |
| 295 | #elif defined(__i386__) |
| 296 | case R_386_PC32: |
| 297 | sym_addr = reinterpret_cast<ElfW(Addr)>(rel_target); |
| 298 | break; |
| 299 | #endif |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | if constexpr (IsGeneral || Mode == RelocMode::JumpTable) { |
| 306 | if (r_type == R_GENERIC_JUMP_SLOT) { |
| 307 | count_relocation_if<IsGeneral>(kRelocAbsolute); |
| 308 | const ElfW(Addr) result = sym_addr + get_addend_norel(); |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 309 | LD_DEBUG(reloc && IsGeneral, "RELO JMP_SLOT %16p <- %16p %s", |
| 310 | rel_target, reinterpret_cast<void*>(result), sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 311 | *static_cast<ElfW(Addr)*>(rel_target) = result; |
| 312 | return true; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if constexpr (IsGeneral || Mode == RelocMode::Typical) { |
| 317 | // Almost all dynamic relocations are of one of these types, and most will be |
| 318 | // R_GENERIC_ABSOLUTE. The platform typically uses RELR instead, but R_GENERIC_RELATIVE is |
| 319 | // common in non-platform binaries. |
| 320 | if (r_type == R_GENERIC_ABSOLUTE) { |
| 321 | count_relocation_if<IsGeneral>(kRelocAbsolute); |
Florian Mayer | 4edc20d | 2024-10-30 14:24:26 -0700 | [diff] [blame] | 322 | if (found_in) sym_addr = found_in->apply_memtag_if_mte_globals(sym_addr); |
Evgenii Stepanov | 6bbb75a | 2023-12-06 18:54:45 +0000 | [diff] [blame] | 323 | const ElfW(Addr) result = sym_addr + get_addend_rel(); |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 324 | LD_DEBUG(reloc && IsGeneral, "RELO ABSOLUTE %16p <- %16p %s", |
| 325 | rel_target, reinterpret_cast<void*>(result), sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 326 | *static_cast<ElfW(Addr)*>(rel_target) = result; |
| 327 | return true; |
| 328 | } else if (r_type == R_GENERIC_GLOB_DAT) { |
| 329 | // The i386 psABI specifies that R_386_GLOB_DAT doesn't have an addend. The ARM ELF ABI |
| 330 | // document (IHI0044F) specifies that R_ARM_GLOB_DAT has an addend, but Bionic isn't adding |
| 331 | // it. |
| 332 | count_relocation_if<IsGeneral>(kRelocAbsolute); |
Florian Mayer | 4edc20d | 2024-10-30 14:24:26 -0700 | [diff] [blame] | 333 | if (found_in) sym_addr = found_in->apply_memtag_if_mte_globals(sym_addr); |
Evgenii Stepanov | 6bbb75a | 2023-12-06 18:54:45 +0000 | [diff] [blame] | 334 | const ElfW(Addr) result = sym_addr + get_addend_norel(); |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 335 | LD_DEBUG(reloc && IsGeneral, "RELO GLOB_DAT %16p <- %16p %s", |
| 336 | rel_target, reinterpret_cast<void*>(result), sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 337 | *static_cast<ElfW(Addr)*>(rel_target) = result; |
| 338 | return true; |
| 339 | } else if (r_type == R_GENERIC_RELATIVE) { |
| 340 | // In practice, r_sym is always zero, but if it weren't, the linker would still look up the |
| 341 | // referenced symbol (and abort if the symbol isn't found), even though it isn't used. |
| 342 | count_relocation_if<IsGeneral>(kRelocRelative); |
Florian Mayer | 4edc20d | 2024-10-30 14:24:26 -0700 | [diff] [blame] | 343 | ElfW(Addr) result = relocator.si->load_bias + get_addend_rel(); |
| 344 | // MTE globals reuses the place bits for additional tag-derivation metadata for |
| 345 | // R_AARCH64_RELATIVE relocations, which makes it incompatible with |
| 346 | // `-Wl,--apply-dynamic-relocs`. This is enforced by lld, however there's nothing stopping |
| 347 | // Android binaries (particularly prebuilts) from building with this linker flag if they're |
| 348 | // not built with MTE globals. Thus, don't use the new relocation semantics if this DSO |
| 349 | // doesn't have MTE globals. |
| 350 | if (relocator.si->should_tag_memtag_globals()) { |
| 351 | int64_t* place = static_cast<int64_t*>(rel_target); |
| 352 | int64_t offset = *place; |
| 353 | result = relocator.si->apply_memtag_if_mte_globals(result + offset) - offset; |
| 354 | } |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 355 | LD_DEBUG(reloc && IsGeneral, "RELO RELATIVE %16p <- %16p", |
| 356 | rel_target, reinterpret_cast<void*>(result)); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 357 | *static_cast<ElfW(Addr)*>(rel_target) = result; |
| 358 | return true; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if constexpr (!IsGeneral) { |
| 363 | // Almost all relocations are handled above. Handle the remaining relocations below, in a |
| 364 | // separate function call. The symbol lookup will be repeated, but the result should be served |
| 365 | // from the 1-symbol lookup cache. |
| 366 | return process_relocation_general(relocator, reloc); |
| 367 | } |
| 368 | |
| 369 | switch (r_type) { |
| 370 | case R_GENERIC_IRELATIVE: |
| 371 | // In the linker, ifuncs are called as soon as possible so that string functions work. We must |
| 372 | // not call them again. (e.g. On arm32, resolving an ifunc changes the meaning of the addend |
| 373 | // from a resolver function to the implementation.) |
| 374 | if (!relocator.si->is_linker()) { |
| 375 | count_relocation_if<IsGeneral>(kRelocRelative); |
| 376 | const ElfW(Addr) ifunc_addr = relocator.si->load_bias + get_addend_rel(); |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 377 | LD_DEBUG(reloc && IsGeneral, "RELO IRELATIVE %16p <- %16p", |
| 378 | rel_target, reinterpret_cast<void*>(ifunc_addr)); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 379 | if (handle_text_relocs && !protect_segments()) return false; |
| 380 | const ElfW(Addr) result = call_ifunc_resolver(ifunc_addr); |
| 381 | if (handle_text_relocs && !unprotect_segments()) return false; |
| 382 | *static_cast<ElfW(Addr)*>(rel_target) = result; |
| 383 | } |
| 384 | break; |
| 385 | case R_GENERIC_COPY: |
| 386 | // Copy relocations allow read-only data or code in a non-PIE executable to access a |
| 387 | // variable from a DSO. The executable reserves extra space in its .bss section, and the |
| 388 | // linker copies the variable into the extra space. The executable then exports its copy |
| 389 | // to interpose the copy in the DSO. |
| 390 | // |
| 391 | // Bionic only supports PIE executables, so copy relocations aren't supported. The ARM and |
| 392 | // AArch64 ABI documents only allow them for ET_EXEC (non-PIE) objects. See IHI0056B and |
| 393 | // IHI0044F. |
| 394 | DL_ERR("%s COPY relocations are not supported", relocator.si->get_realpath()); |
| 395 | return false; |
| 396 | case R_GENERIC_TLS_TPREL: |
| 397 | count_relocation_if<IsGeneral>(kRelocRelative); |
| 398 | { |
| 399 | ElfW(Addr) tpoff = 0; |
| 400 | if (found_in == nullptr) { |
| 401 | // Unresolved weak relocation. Leave tpoff at 0 to resolve |
| 402 | // &weak_tls_symbol to __get_tls(). |
| 403 | } else { |
| 404 | CHECK(found_in->get_tls() != nullptr); // We rejected a missing TLS segment above. |
| 405 | const TlsModule& mod = get_tls_module(found_in->get_tls()->module_id); |
| 406 | if (mod.static_offset != SIZE_MAX) { |
| 407 | tpoff += mod.static_offset - relocator.tls_tp_base; |
| 408 | } else { |
| 409 | DL_ERR("TLS symbol \"%s\" in dlopened \"%s\" referenced from \"%s\" using IE access model", |
| 410 | sym_name, found_in->get_realpath(), relocator.si->get_realpath()); |
| 411 | return false; |
| 412 | } |
| 413 | } |
| 414 | tpoff += sym_addr + get_addend_rel(); |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 415 | LD_DEBUG(reloc && IsGeneral, "RELO TLS_TPREL %16p <- %16p %s", |
| 416 | rel_target, reinterpret_cast<void*>(tpoff), sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 417 | *static_cast<ElfW(Addr)*>(rel_target) = tpoff; |
| 418 | } |
| 419 | break; |
| 420 | case R_GENERIC_TLS_DTPMOD: |
| 421 | count_relocation_if<IsGeneral>(kRelocRelative); |
| 422 | { |
| 423 | size_t module_id = 0; |
| 424 | if (found_in == nullptr) { |
| 425 | // Unresolved weak relocation. Evaluate the module ID to 0. |
| 426 | } else { |
| 427 | CHECK(found_in->get_tls() != nullptr); // We rejected a missing TLS segment above. |
| 428 | module_id = found_in->get_tls()->module_id; |
Ryan Prichard | b493746 | 2024-03-13 23:07:15 -0700 | [diff] [blame] | 429 | CHECK(module_id != kTlsUninitializedModuleId); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 430 | } |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 431 | LD_DEBUG(reloc && IsGeneral, "RELO TLS_DTPMOD %16p <- %zu %s", |
| 432 | rel_target, module_id, sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 433 | *static_cast<ElfW(Addr)*>(rel_target) = module_id; |
| 434 | } |
| 435 | break; |
| 436 | case R_GENERIC_TLS_DTPREL: |
| 437 | count_relocation_if<IsGeneral>(kRelocRelative); |
| 438 | { |
Elliott Hughes | 4346270 | 2022-10-10 19:21:44 +0000 | [diff] [blame] | 439 | const ElfW(Addr) result = sym_addr + get_addend_rel() - TLS_DTV_OFFSET; |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 440 | LD_DEBUG(reloc && IsGeneral, "RELO TLS_DTPREL %16p <- %16p %s", |
| 441 | rel_target, reinterpret_cast<void*>(result), sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 442 | *static_cast<ElfW(Addr)*>(rel_target) = result; |
| 443 | } |
| 444 | break; |
| 445 | |
Paul Kirth | 4d43778 | 2024-01-30 23:03:14 +0000 | [diff] [blame] | 446 | #if defined(__aarch64__) || defined(__riscv) |
| 447 | // Bionic currently implements TLSDESC for arm64 and riscv64. This implementation should work |
| 448 | // with other architectures, as long as the resolver functions are implemented. |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 449 | case R_GENERIC_TLSDESC: |
| 450 | count_relocation_if<IsGeneral>(kRelocRelative); |
| 451 | { |
| 452 | ElfW(Addr) addend = reloc.r_addend; |
| 453 | TlsDescriptor* desc = static_cast<TlsDescriptor*>(rel_target); |
| 454 | if (found_in == nullptr) { |
| 455 | // Unresolved weak relocation. |
| 456 | desc->func = tlsdesc_resolver_unresolved_weak; |
| 457 | desc->arg = addend; |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 458 | LD_DEBUG(reloc && IsGeneral, "RELO TLSDESC %16p <- unresolved weak, addend 0x%zx %s", |
| 459 | rel_target, static_cast<size_t>(addend), sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 460 | } else { |
| 461 | CHECK(found_in->get_tls() != nullptr); // We rejected a missing TLS segment above. |
| 462 | size_t module_id = found_in->get_tls()->module_id; |
| 463 | const TlsModule& mod = get_tls_module(module_id); |
| 464 | if (mod.static_offset != SIZE_MAX) { |
| 465 | desc->func = tlsdesc_resolver_static; |
| 466 | desc->arg = mod.static_offset - relocator.tls_tp_base + sym_addr + addend; |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 467 | LD_DEBUG(reloc && IsGeneral, "RELO TLSDESC %16p <- static (0x%zx - 0x%zx + 0x%zx + 0x%zx) %s", |
| 468 | rel_target, mod.static_offset, relocator.tls_tp_base, |
| 469 | static_cast<size_t>(sym_addr), static_cast<size_t>(addend), |
| 470 | sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 471 | } else { |
| 472 | relocator.tlsdesc_args->push_back({ |
| 473 | .generation = mod.first_generation, |
| 474 | .index.module_id = module_id, |
| 475 | .index.offset = sym_addr + addend, |
| 476 | }); |
| 477 | // Defer the TLSDESC relocation until the address of the TlsDynamicResolverArg object |
| 478 | // is finalized. |
| 479 | relocator.deferred_tlsdesc_relocs.push_back({ |
| 480 | desc, relocator.tlsdesc_args->size() - 1 |
| 481 | }); |
| 482 | const TlsDynamicResolverArg& desc_arg = relocator.tlsdesc_args->back(); |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 483 | LD_DEBUG(reloc && IsGeneral, "RELO TLSDESC %16p <- dynamic (gen %zu, mod %zu, off %zu) %s", |
| 484 | rel_target, desc_arg.generation, desc_arg.index.module_id, |
| 485 | desc_arg.index.offset, sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | } |
| 489 | break; |
Paul Kirth | 4d43778 | 2024-01-30 23:03:14 +0000 | [diff] [blame] | 490 | #endif // defined(__aarch64__) || defined(__riscv) |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 491 | |
| 492 | #if defined(__x86_64__) |
| 493 | case R_X86_64_32: |
| 494 | count_relocation_if<IsGeneral>(kRelocAbsolute); |
| 495 | { |
| 496 | const Elf32_Addr result = sym_addr + reloc.r_addend; |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 497 | LD_DEBUG(reloc && IsGeneral, "RELO R_X86_64_32 %16p <- 0x%08x %s", |
| 498 | rel_target, result, sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 499 | *static_cast<Elf32_Addr*>(rel_target) = result; |
| 500 | } |
| 501 | break; |
| 502 | case R_X86_64_PC32: |
| 503 | count_relocation_if<IsGeneral>(kRelocRelative); |
| 504 | { |
| 505 | const ElfW(Addr) target = sym_addr + reloc.r_addend; |
| 506 | const ElfW(Addr) base = reinterpret_cast<ElfW(Addr)>(rel_target); |
| 507 | const Elf32_Addr result = target - base; |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 508 | LD_DEBUG(reloc && IsGeneral, "RELO R_X86_64_PC32 %16p <- 0x%08x (%16p - %16p) %s", |
| 509 | rel_target, result, reinterpret_cast<void*>(target), |
| 510 | reinterpret_cast<void*>(base), sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 511 | *static_cast<Elf32_Addr*>(rel_target) = result; |
| 512 | } |
| 513 | break; |
| 514 | #elif defined(__i386__) |
| 515 | case R_386_PC32: |
| 516 | count_relocation_if<IsGeneral>(kRelocRelative); |
| 517 | { |
| 518 | const ElfW(Addr) target = sym_addr + get_addend_rel(); |
| 519 | const ElfW(Addr) base = reinterpret_cast<ElfW(Addr)>(rel_target); |
| 520 | const ElfW(Addr) result = target - base; |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 521 | LD_DEBUG(reloc && IsGeneral, "RELO R_386_PC32 %16p <- 0x%08x (%16p - %16p) %s", |
| 522 | rel_target, result, reinterpret_cast<void*>(target), |
| 523 | reinterpret_cast<void*>(base), sym_name); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 524 | *static_cast<ElfW(Addr)*>(rel_target) = result; |
| 525 | } |
| 526 | break; |
| 527 | #endif |
| 528 | default: |
| 529 | DL_ERR("unknown reloc type %d in \"%s\"", r_type, relocator.si->get_realpath()); |
| 530 | return false; |
| 531 | } |
| 532 | return true; |
| 533 | } |
| 534 | |
| 535 | __attribute__((noinline)) |
| 536 | static bool process_relocation_general(Relocator& relocator, const rel_t& reloc) { |
| 537 | return process_relocation_impl<RelocMode::General>(relocator, reloc); |
| 538 | } |
| 539 | |
| 540 | template <RelocMode Mode> |
| 541 | __attribute__((always_inline)) |
| 542 | static inline bool process_relocation(Relocator& relocator, const rel_t& reloc) { |
| 543 | return Mode == RelocMode::General ? |
| 544 | process_relocation_general(relocator, reloc) : |
| 545 | process_relocation_impl<Mode>(relocator, reloc); |
| 546 | } |
| 547 | |
| 548 | template <RelocMode Mode> |
| 549 | __attribute__((noinline)) |
| 550 | static bool plain_relocate_impl(Relocator& relocator, rel_t* rels, size_t rel_count) { |
| 551 | for (size_t i = 0; i < rel_count; ++i) { |
| 552 | if (!process_relocation<Mode>(relocator, rels[i])) { |
| 553 | return false; |
| 554 | } |
| 555 | } |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | template <RelocMode Mode> |
| 560 | __attribute__((noinline)) |
| 561 | static bool packed_relocate_impl(Relocator& relocator, sleb128_decoder decoder) { |
| 562 | return for_all_packed_relocs(decoder, [&](const rel_t& reloc) { |
| 563 | return process_relocation<Mode>(relocator, reloc); |
| 564 | }); |
| 565 | } |
| 566 | |
| 567 | static bool needs_slow_relocate_loop(const Relocator& relocator __unused) { |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 568 | #if !defined(__LP64__) |
| 569 | if (relocator.si->has_text_relocations) return true; |
| 570 | #endif |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 571 | // Both LD_DEBUG relocation logging and statistics need the slow path. |
| 572 | if (g_linker_debug_config.any || g_linker_debug_config.statistics) { |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 573 | return true; |
| 574 | } |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | template <RelocMode OptMode, typename ...Args> |
| 579 | static bool plain_relocate(Relocator& relocator, Args ...args) { |
| 580 | return needs_slow_relocate_loop(relocator) ? |
| 581 | plain_relocate_impl<RelocMode::General>(relocator, args...) : |
| 582 | plain_relocate_impl<OptMode>(relocator, args...); |
| 583 | } |
| 584 | |
| 585 | template <RelocMode OptMode, typename ...Args> |
| 586 | static bool packed_relocate(Relocator& relocator, Args ...args) { |
| 587 | return needs_slow_relocate_loop(relocator) ? |
| 588 | packed_relocate_impl<RelocMode::General>(relocator, args...) : |
| 589 | packed_relocate_impl<OptMode>(relocator, args...); |
| 590 | } |
| 591 | |
| 592 | bool soinfo::relocate(const SymbolLookupList& lookup_list) { |
Ryan Prichard | 32bb367 | 2024-03-08 16:53:06 -0800 | [diff] [blame] | 593 | // For ldd, don't apply relocations because TLS segments are not registered. |
| 594 | // We don't care whether ldd diagnoses unresolved symbols. |
| 595 | if (g_is_ldd) { |
| 596 | return true; |
| 597 | } |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 598 | |
| 599 | VersionTracker version_tracker; |
| 600 | |
| 601 | if (!version_tracker.init(this)) { |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | Relocator relocator(version_tracker, lookup_list); |
| 606 | relocator.si = this; |
| 607 | relocator.si_strtab = strtab_; |
| 608 | relocator.si_strtab_size = has_min_version(1) ? strtab_size_ : SIZE_MAX; |
| 609 | relocator.si_symtab = symtab_; |
| 610 | relocator.tlsdesc_args = &tlsdesc_args_; |
| 611 | relocator.tls_tp_base = __libc_shared_globals()->static_tls_layout.offset_thread_pointer(); |
| 612 | |
Ryan Prichard | 2a901e6 | 2024-04-04 03:05:35 -0700 | [diff] [blame] | 613 | // The linker already applied its RELR relocations in an earlier pass, so |
| 614 | // skip the RELR relocations for the linker. |
| 615 | if (relr_ != nullptr && !is_linker()) { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 616 | LD_DEBUG(reloc, "[ relocating %s relr ]", get_realpath()); |
Ryan Prichard | 2a901e6 | 2024-04-04 03:05:35 -0700 | [diff] [blame] | 617 | const ElfW(Relr)* begin = relr_; |
| 618 | const ElfW(Relr)* end = relr_ + relr_count_; |
Florian Mayer | 4edc20d | 2024-10-30 14:24:26 -0700 | [diff] [blame] | 619 | if (!relocate_relr(begin, end, load_bias, should_tag_memtag_globals())) { |
Peter Collingbourne | e0ebca8 | 2024-03-26 15:39:42 -0700 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | } |
| 623 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 624 | if (android_relocs_ != nullptr) { |
| 625 | // check signature |
| 626 | if (android_relocs_size_ > 3 && |
| 627 | android_relocs_[0] == 'A' && |
| 628 | android_relocs_[1] == 'P' && |
| 629 | android_relocs_[2] == 'S' && |
| 630 | android_relocs_[3] == '2') { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 631 | LD_DEBUG(reloc, "[ relocating %s android rel/rela ]", get_realpath()); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 632 | |
| 633 | const uint8_t* packed_relocs = android_relocs_ + 4; |
| 634 | const size_t packed_relocs_size = android_relocs_size_ - 4; |
| 635 | |
| 636 | if (!packed_relocate<RelocMode::Typical>(relocator, sleb128_decoder(packed_relocs, packed_relocs_size))) { |
| 637 | return false; |
| 638 | } |
| 639 | } else { |
| 640 | DL_ERR("bad android relocation header."); |
| 641 | return false; |
| 642 | } |
| 643 | } |
| 644 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 645 | #if defined(USE_RELA) |
| 646 | if (rela_ != nullptr) { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 647 | LD_DEBUG(reloc, "[ relocating %s rela ]", get_realpath()); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 648 | |
| 649 | if (!plain_relocate<RelocMode::Typical>(relocator, rela_, rela_count_)) { |
| 650 | return false; |
| 651 | } |
| 652 | } |
| 653 | if (plt_rela_ != nullptr) { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 654 | LD_DEBUG(reloc, "[ relocating %s plt rela ]", get_realpath()); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 655 | if (!plain_relocate<RelocMode::JumpTable>(relocator, plt_rela_, plt_rela_count_)) { |
| 656 | return false; |
| 657 | } |
| 658 | } |
| 659 | #else |
| 660 | if (rel_ != nullptr) { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 661 | LD_DEBUG(reloc, "[ relocating %s rel ]", get_realpath()); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 662 | if (!plain_relocate<RelocMode::Typical>(relocator, rel_, rel_count_)) { |
| 663 | return false; |
| 664 | } |
| 665 | } |
| 666 | if (plt_rel_ != nullptr) { |
Elliott Hughes | f08d0eb | 2024-09-03 17:31:29 -0400 | [diff] [blame] | 667 | LD_DEBUG(reloc, "[ relocating %s plt rel ]", get_realpath()); |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 668 | if (!plain_relocate<RelocMode::JumpTable>(relocator, plt_rel_, plt_rel_count_)) { |
| 669 | return false; |
| 670 | } |
| 671 | } |
| 672 | #endif |
| 673 | |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 674 | // Once the tlsdesc_args_ vector's size is finalized, we can write the addresses of its elements |
| 675 | // into the TLSDESC relocations. |
Paul Kirth | 4d43778 | 2024-01-30 23:03:14 +0000 | [diff] [blame] | 676 | #if defined(__aarch64__) || defined(__riscv) |
| 677 | // Bionic currently only implements TLSDESC for arm64 and riscv64. |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 678 | for (const std::pair<TlsDescriptor*, size_t>& pair : relocator.deferred_tlsdesc_relocs) { |
| 679 | TlsDescriptor* desc = pair.first; |
| 680 | desc->func = tlsdesc_resolver_dynamic; |
| 681 | desc->arg = reinterpret_cast<size_t>(&tlsdesc_args_[pair.second]); |
| 682 | } |
Paul Kirth | 4d43778 | 2024-01-30 23:03:14 +0000 | [diff] [blame] | 683 | #endif // defined(__aarch64__) || defined(__riscv) |
Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 684 | |
| 685 | return true; |
| 686 | } |