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