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