| 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 |  | 
 | 232 |   if (IsGeneral && is_tls_reloc(r_type)) { | 
 | 233 |     if (r_sym == 0) { | 
 | 234 |       // By convention in ld.bfd and lld, an omitted symbol on a TLS relocation | 
 | 235 |       // is a reference to the current module. | 
 | 236 |       found_in = relocator.si; | 
 | 237 |     } else if (ELF_ST_BIND(relocator.si_symtab[r_sym].st_info) == STB_LOCAL) { | 
 | 238 |       // In certain situations, the Gold linker accesses a TLS symbol using a | 
 | 239 |       // relocation to an STB_LOCAL symbol in .dynsym of either STT_SECTION or | 
 | 240 |       // STT_TLS type. Bionic doesn't support these relocations, so issue an | 
 | 241 |       // error. References: | 
 | 242 |       //  - https://groups.google.com/d/topic/generic-abi/dJ4_Y78aQ2M/discussion | 
 | 243 |       //  - https://sourceware.org/bugzilla/show_bug.cgi?id=17699 | 
 | 244 |       sym = &relocator.si_symtab[r_sym]; | 
 | 245 |       DL_ERR("unexpected TLS reference to local symbol \"%s\" in \"%s\": sym type %d, rel type %u", | 
 | 246 |              sym_name, relocator.si->get_realpath(), ELF_ST_TYPE(sym->st_info), r_type); | 
 | 247 |       return false; | 
 | 248 |     } else if (!lookup_symbol<IsGeneral>(relocator, r_sym, sym_name, &found_in, &sym)) { | 
 | 249 |       return false; | 
 | 250 |     } | 
 | 251 |     if (found_in != nullptr && found_in->get_tls() == nullptr) { | 
 | 252 |       // sym_name can be nullptr if r_sym is 0. A linker should never output an ELF file like this. | 
 | 253 |       DL_ERR("TLS relocation refers to symbol \"%s\" in solib \"%s\" with no TLS segment", | 
 | 254 |              sym_name, found_in->get_realpath()); | 
 | 255 |       return false; | 
 | 256 |     } | 
 | 257 |     if (sym != nullptr) { | 
 | 258 |       if (ELF_ST_TYPE(sym->st_info) != STT_TLS) { | 
 | 259 |         // A toolchain should never output a relocation like this. | 
 | 260 |         DL_ERR("reference to non-TLS symbol \"%s\" from TLS relocation in \"%s\"", | 
 | 261 |                sym_name, relocator.si->get_realpath()); | 
 | 262 |         return false; | 
 | 263 |       } | 
 | 264 |       sym_addr = sym->st_value; | 
 | 265 |     } | 
 | 266 |   } else { | 
 | 267 |     if (r_sym == 0) { | 
 | 268 |       // Do nothing. | 
 | 269 |     } else { | 
 | 270 |       if (!lookup_symbol<IsGeneral>(relocator, r_sym, sym_name, &found_in, &sym)) return false; | 
 | 271 |       if (sym != nullptr) { | 
 | 272 |         const bool should_protect_segments = handle_text_relocs && | 
 | 273 |                                              found_in == relocator.si && | 
 | 274 |                                              ELF_ST_TYPE(sym->st_info) == STT_GNU_IFUNC; | 
 | 275 |         if (should_protect_segments && !protect_segments()) return false; | 
 | 276 |         sym_addr = found_in->resolve_symbol_address(sym); | 
 | 277 |         if (should_protect_segments && !unprotect_segments()) return false; | 
 | 278 |       } else if constexpr (IsGeneral) { | 
 | 279 |         // A weak reference to an undefined symbol. We typically use a zero symbol address, but | 
 | 280 |         // use the relocation base for PC-relative relocations, so that the value written is zero. | 
 | 281 |         switch (r_type) { | 
 | 282 | #if defined(__x86_64__) | 
 | 283 |           case R_X86_64_PC32: | 
 | 284 |             sym_addr = reinterpret_cast<ElfW(Addr)>(rel_target); | 
 | 285 |             break; | 
 | 286 | #elif defined(__i386__) | 
 | 287 |           case R_386_PC32: | 
 | 288 |             sym_addr = reinterpret_cast<ElfW(Addr)>(rel_target); | 
 | 289 |             break; | 
 | 290 | #endif | 
 | 291 |         } | 
 | 292 |       } | 
 | 293 |     } | 
 | 294 |   } | 
 | 295 |  | 
 | 296 |   if constexpr (IsGeneral || Mode == RelocMode::JumpTable) { | 
 | 297 |     if (r_type == R_GENERIC_JUMP_SLOT) { | 
 | 298 |       count_relocation_if<IsGeneral>(kRelocAbsolute); | 
 | 299 |       const ElfW(Addr) result = sym_addr + get_addend_norel(); | 
 | 300 |       trace_reloc("RELO JMP_SLOT %16p <- %16p %s", | 
 | 301 |                   rel_target, reinterpret_cast<void*>(result), sym_name); | 
 | 302 |       *static_cast<ElfW(Addr)*>(rel_target) = result; | 
 | 303 |       return true; | 
 | 304 |     } | 
 | 305 |   } | 
 | 306 |  | 
 | 307 |   if constexpr (IsGeneral || Mode == RelocMode::Typical) { | 
 | 308 |     // Almost all dynamic relocations are of one of these types, and most will be | 
 | 309 |     // R_GENERIC_ABSOLUTE. The platform typically uses RELR instead, but R_GENERIC_RELATIVE is | 
 | 310 |     // common in non-platform binaries. | 
 | 311 |     if (r_type == R_GENERIC_ABSOLUTE) { | 
 | 312 |       count_relocation_if<IsGeneral>(kRelocAbsolute); | 
 | 313 |       const ElfW(Addr) result = sym_addr + get_addend_rel(); | 
 | 314 |       trace_reloc("RELO ABSOLUTE %16p <- %16p %s", | 
 | 315 |                   rel_target, reinterpret_cast<void*>(result), sym_name); | 
 | 316 |       *static_cast<ElfW(Addr)*>(rel_target) = result; | 
 | 317 |       return true; | 
 | 318 |     } else if (r_type == R_GENERIC_GLOB_DAT) { | 
 | 319 |       // The i386 psABI specifies that R_386_GLOB_DAT doesn't have an addend. The ARM ELF ABI | 
 | 320 |       // document (IHI0044F) specifies that R_ARM_GLOB_DAT has an addend, but Bionic isn't adding | 
 | 321 |       // it. | 
 | 322 |       count_relocation_if<IsGeneral>(kRelocAbsolute); | 
 | 323 |       const ElfW(Addr) result = sym_addr + get_addend_norel(); | 
 | 324 |       trace_reloc("RELO GLOB_DAT %16p <- %16p %s", | 
 | 325 |                   rel_target, reinterpret_cast<void*>(result), sym_name); | 
 | 326 |       *static_cast<ElfW(Addr)*>(rel_target) = result; | 
 | 327 |       return true; | 
 | 328 |     } else if (r_type == R_GENERIC_RELATIVE) { | 
 | 329 |       // In practice, r_sym is always zero, but if it weren't, the linker would still look up the | 
 | 330 |       // referenced symbol (and abort if the symbol isn't found), even though it isn't used. | 
 | 331 |       count_relocation_if<IsGeneral>(kRelocRelative); | 
 | 332 |       const ElfW(Addr) result = relocator.si->load_bias + get_addend_rel(); | 
 | 333 |       trace_reloc("RELO RELATIVE %16p <- %16p", | 
 | 334 |                   rel_target, reinterpret_cast<void*>(result)); | 
 | 335 |       *static_cast<ElfW(Addr)*>(rel_target) = result; | 
 | 336 |       return true; | 
 | 337 |     } | 
 | 338 |   } | 
 | 339 |  | 
 | 340 |   if constexpr (!IsGeneral) { | 
 | 341 |     // Almost all relocations are handled above. Handle the remaining relocations below, in a | 
 | 342 |     // separate function call. The symbol lookup will be repeated, but the result should be served | 
 | 343 |     // from the 1-symbol lookup cache. | 
 | 344 |     return process_relocation_general(relocator, reloc); | 
 | 345 |   } | 
 | 346 |  | 
 | 347 |   switch (r_type) { | 
 | 348 |     case R_GENERIC_IRELATIVE: | 
 | 349 |       // In the linker, ifuncs are called as soon as possible so that string functions work. We must | 
 | 350 |       // not call them again. (e.g. On arm32, resolving an ifunc changes the meaning of the addend | 
 | 351 |       // from a resolver function to the implementation.) | 
 | 352 |       if (!relocator.si->is_linker()) { | 
 | 353 |         count_relocation_if<IsGeneral>(kRelocRelative); | 
 | 354 |         const ElfW(Addr) ifunc_addr = relocator.si->load_bias + get_addend_rel(); | 
 | 355 |         trace_reloc("RELO IRELATIVE %16p <- %16p", | 
 | 356 |                     rel_target, reinterpret_cast<void*>(ifunc_addr)); | 
 | 357 |         if (handle_text_relocs && !protect_segments()) return false; | 
 | 358 |         const ElfW(Addr) result = call_ifunc_resolver(ifunc_addr); | 
 | 359 |         if (handle_text_relocs && !unprotect_segments()) return false; | 
 | 360 |         *static_cast<ElfW(Addr)*>(rel_target) = result; | 
 | 361 |       } | 
 | 362 |       break; | 
 | 363 |     case R_GENERIC_COPY: | 
 | 364 |       // Copy relocations allow read-only data or code in a non-PIE executable to access a | 
 | 365 |       // variable from a DSO. The executable reserves extra space in its .bss section, and the | 
 | 366 |       // linker copies the variable into the extra space. The executable then exports its copy | 
 | 367 |       // to interpose the copy in the DSO. | 
 | 368 |       // | 
 | 369 |       // Bionic only supports PIE executables, so copy relocations aren't supported. The ARM and | 
 | 370 |       // AArch64 ABI documents only allow them for ET_EXEC (non-PIE) objects. See IHI0056B and | 
 | 371 |       // IHI0044F. | 
 | 372 |       DL_ERR("%s COPY relocations are not supported", relocator.si->get_realpath()); | 
 | 373 |       return false; | 
 | 374 |     case R_GENERIC_TLS_TPREL: | 
 | 375 |       count_relocation_if<IsGeneral>(kRelocRelative); | 
 | 376 |       { | 
 | 377 |         ElfW(Addr) tpoff = 0; | 
 | 378 |         if (found_in == nullptr) { | 
 | 379 |           // Unresolved weak relocation. Leave tpoff at 0 to resolve | 
 | 380 |           // &weak_tls_symbol to __get_tls(). | 
 | 381 |         } else { | 
 | 382 |           CHECK(found_in->get_tls() != nullptr); // We rejected a missing TLS segment above. | 
 | 383 |           const TlsModule& mod = get_tls_module(found_in->get_tls()->module_id); | 
 | 384 |           if (mod.static_offset != SIZE_MAX) { | 
 | 385 |             tpoff += mod.static_offset - relocator.tls_tp_base; | 
 | 386 |           } else { | 
 | 387 |             DL_ERR("TLS symbol \"%s\" in dlopened \"%s\" referenced from \"%s\" using IE access model", | 
 | 388 |                    sym_name, found_in->get_realpath(), relocator.si->get_realpath()); | 
 | 389 |             return false; | 
 | 390 |           } | 
 | 391 |         } | 
 | 392 |         tpoff += sym_addr + get_addend_rel(); | 
 | 393 |         trace_reloc("RELO TLS_TPREL %16p <- %16p %s", | 
 | 394 |                     rel_target, reinterpret_cast<void*>(tpoff), sym_name); | 
 | 395 |         *static_cast<ElfW(Addr)*>(rel_target) = tpoff; | 
 | 396 |       } | 
 | 397 |       break; | 
 | 398 |     case R_GENERIC_TLS_DTPMOD: | 
 | 399 |       count_relocation_if<IsGeneral>(kRelocRelative); | 
 | 400 |       { | 
 | 401 |         size_t module_id = 0; | 
 | 402 |         if (found_in == nullptr) { | 
 | 403 |           // Unresolved weak relocation. Evaluate the module ID to 0. | 
 | 404 |         } else { | 
 | 405 |           CHECK(found_in->get_tls() != nullptr); // We rejected a missing TLS segment above. | 
 | 406 |           module_id = found_in->get_tls()->module_id; | 
 | 407 |         } | 
 | 408 |         trace_reloc("RELO TLS_DTPMOD %16p <- %zu %s", | 
 | 409 |                     rel_target, module_id, sym_name); | 
 | 410 |         *static_cast<ElfW(Addr)*>(rel_target) = module_id; | 
 | 411 |       } | 
 | 412 |       break; | 
 | 413 |     case R_GENERIC_TLS_DTPREL: | 
 | 414 |       count_relocation_if<IsGeneral>(kRelocRelative); | 
 | 415 |       { | 
 | 416 |         const ElfW(Addr) result = sym_addr + get_addend_rel(); | 
 | 417 |         trace_reloc("RELO TLS_DTPREL %16p <- %16p %s", | 
 | 418 |                     rel_target, reinterpret_cast<void*>(result), sym_name); | 
 | 419 |         *static_cast<ElfW(Addr)*>(rel_target) = result; | 
 | 420 |       } | 
 | 421 |       break; | 
 | 422 |  | 
 | 423 | #if defined(__aarch64__) | 
 | 424 |     // Bionic currently only implements TLSDESC for arm64. This implementation should work with | 
 | 425 |     // other architectures, as long as the resolver functions are implemented. | 
 | 426 |     case R_GENERIC_TLSDESC: | 
 | 427 |       count_relocation_if<IsGeneral>(kRelocRelative); | 
 | 428 |       { | 
 | 429 |         ElfW(Addr) addend = reloc.r_addend; | 
 | 430 |         TlsDescriptor* desc = static_cast<TlsDescriptor*>(rel_target); | 
 | 431 |         if (found_in == nullptr) { | 
 | 432 |           // Unresolved weak relocation. | 
 | 433 |           desc->func = tlsdesc_resolver_unresolved_weak; | 
 | 434 |           desc->arg = addend; | 
 | 435 |           trace_reloc("RELO TLSDESC %16p <- unresolved weak, addend 0x%zx %s", | 
 | 436 |                       rel_target, static_cast<size_t>(addend), sym_name); | 
 | 437 |         } else { | 
 | 438 |           CHECK(found_in->get_tls() != nullptr); // We rejected a missing TLS segment above. | 
 | 439 |           size_t module_id = found_in->get_tls()->module_id; | 
 | 440 |           const TlsModule& mod = get_tls_module(module_id); | 
 | 441 |           if (mod.static_offset != SIZE_MAX) { | 
 | 442 |             desc->func = tlsdesc_resolver_static; | 
 | 443 |             desc->arg = mod.static_offset - relocator.tls_tp_base + sym_addr + addend; | 
 | 444 |             trace_reloc("RELO TLSDESC %16p <- static (0x%zx - 0x%zx + 0x%zx + 0x%zx) %s", | 
 | 445 |                         rel_target, mod.static_offset, relocator.tls_tp_base, | 
 | 446 |                         static_cast<size_t>(sym_addr), static_cast<size_t>(addend), | 
 | 447 |                         sym_name); | 
 | 448 |           } else { | 
 | 449 |             relocator.tlsdesc_args->push_back({ | 
 | 450 |               .generation = mod.first_generation, | 
 | 451 |               .index.module_id = module_id, | 
 | 452 |               .index.offset = sym_addr + addend, | 
 | 453 |             }); | 
 | 454 |             // Defer the TLSDESC relocation until the address of the TlsDynamicResolverArg object | 
 | 455 |             // is finalized. | 
 | 456 |             relocator.deferred_tlsdesc_relocs.push_back({ | 
 | 457 |               desc, relocator.tlsdesc_args->size() - 1 | 
 | 458 |             }); | 
 | 459 |             const TlsDynamicResolverArg& desc_arg = relocator.tlsdesc_args->back(); | 
 | 460 |             trace_reloc("RELO TLSDESC %16p <- dynamic (gen %zu, mod %zu, off %zu) %s", | 
 | 461 |                         rel_target, desc_arg.generation, desc_arg.index.module_id, | 
 | 462 |                         desc_arg.index.offset, sym_name); | 
 | 463 |           } | 
 | 464 |         } | 
 | 465 |       } | 
 | 466 |       break; | 
 | 467 | #endif  // defined(__aarch64__) | 
 | 468 |  | 
 | 469 | #if defined(__x86_64__) | 
 | 470 |     case R_X86_64_32: | 
 | 471 |       count_relocation_if<IsGeneral>(kRelocAbsolute); | 
 | 472 |       { | 
 | 473 |         const Elf32_Addr result = sym_addr + reloc.r_addend; | 
 | 474 |         trace_reloc("RELO R_X86_64_32 %16p <- 0x%08x %s", | 
 | 475 |                     rel_target, result, sym_name); | 
 | 476 |         *static_cast<Elf32_Addr*>(rel_target) = result; | 
 | 477 |       } | 
 | 478 |       break; | 
 | 479 |     case R_X86_64_PC32: | 
 | 480 |       count_relocation_if<IsGeneral>(kRelocRelative); | 
 | 481 |       { | 
 | 482 |         const ElfW(Addr) target = sym_addr + reloc.r_addend; | 
 | 483 |         const ElfW(Addr) base = reinterpret_cast<ElfW(Addr)>(rel_target); | 
 | 484 |         const Elf32_Addr result = target - base; | 
 | 485 |         trace_reloc("RELO R_X86_64_PC32 %16p <- 0x%08x (%16p - %16p) %s", | 
 | 486 |                     rel_target, result, reinterpret_cast<void*>(target), | 
 | 487 |                     reinterpret_cast<void*>(base), sym_name); | 
 | 488 |         *static_cast<Elf32_Addr*>(rel_target) = result; | 
 | 489 |       } | 
 | 490 |       break; | 
 | 491 | #elif defined(__i386__) | 
 | 492 |     case R_386_PC32: | 
 | 493 |       count_relocation_if<IsGeneral>(kRelocRelative); | 
 | 494 |       { | 
 | 495 |         const ElfW(Addr) target = sym_addr + get_addend_rel(); | 
 | 496 |         const ElfW(Addr) base = reinterpret_cast<ElfW(Addr)>(rel_target); | 
 | 497 |         const ElfW(Addr) result = target - base; | 
 | 498 |         trace_reloc("RELO R_386_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<ElfW(Addr)*>(rel_target) = result; | 
 | 502 |       } | 
 | 503 |       break; | 
 | 504 | #endif | 
 | 505 |     default: | 
 | 506 |       DL_ERR("unknown reloc type %d in \"%s\"", r_type, relocator.si->get_realpath()); | 
 | 507 |       return false; | 
 | 508 |   } | 
 | 509 |   return true; | 
 | 510 | } | 
 | 511 |  | 
 | 512 | __attribute__((noinline)) | 
 | 513 | static bool process_relocation_general(Relocator& relocator, const rel_t& reloc) { | 
 | 514 |   return process_relocation_impl<RelocMode::General>(relocator, reloc); | 
 | 515 | } | 
 | 516 |  | 
 | 517 | template <RelocMode Mode> | 
 | 518 | __attribute__((always_inline)) | 
 | 519 | static inline bool process_relocation(Relocator& relocator, const rel_t& reloc) { | 
 | 520 |   return Mode == RelocMode::General ? | 
 | 521 |       process_relocation_general(relocator, reloc) : | 
 | 522 |       process_relocation_impl<Mode>(relocator, reloc); | 
 | 523 | } | 
 | 524 |  | 
 | 525 | template <RelocMode Mode> | 
 | 526 | __attribute__((noinline)) | 
 | 527 | static bool plain_relocate_impl(Relocator& relocator, rel_t* rels, size_t rel_count) { | 
 | 528 |   for (size_t i = 0; i < rel_count; ++i) { | 
 | 529 |     if (!process_relocation<Mode>(relocator, rels[i])) { | 
 | 530 |       return false; | 
 | 531 |     } | 
 | 532 |   } | 
 | 533 |   return true; | 
 | 534 | } | 
 | 535 |  | 
 | 536 | template <RelocMode Mode> | 
 | 537 | __attribute__((noinline)) | 
 | 538 | static bool packed_relocate_impl(Relocator& relocator, sleb128_decoder decoder) { | 
 | 539 |   return for_all_packed_relocs(decoder, [&](const rel_t& reloc) { | 
 | 540 |     return process_relocation<Mode>(relocator, reloc); | 
 | 541 |   }); | 
 | 542 | } | 
 | 543 |  | 
 | 544 | static bool needs_slow_relocate_loop(const Relocator& relocator __unused) { | 
 | 545 | #if STATS | 
 | 546 |   // TODO: This could become a run-time flag. | 
 | 547 |   return true; | 
 | 548 | #endif | 
 | 549 | #if !defined(__LP64__) | 
 | 550 |   if (relocator.si->has_text_relocations) return true; | 
 | 551 | #endif | 
 | 552 |   if (g_ld_debug_verbosity > LINKER_VERBOSITY_TRACE) { | 
 | 553 |     // If linker TRACE() is enabled, then each relocation is logged. | 
 | 554 |     return true; | 
 | 555 |   } | 
 | 556 |   return false; | 
 | 557 | } | 
 | 558 |  | 
 | 559 | template <RelocMode OptMode, typename ...Args> | 
 | 560 | static bool plain_relocate(Relocator& relocator, Args ...args) { | 
 | 561 |   return needs_slow_relocate_loop(relocator) ? | 
 | 562 |       plain_relocate_impl<RelocMode::General>(relocator, args...) : | 
 | 563 |       plain_relocate_impl<OptMode>(relocator, args...); | 
 | 564 | } | 
 | 565 |  | 
 | 566 | template <RelocMode OptMode, typename ...Args> | 
 | 567 | static bool packed_relocate(Relocator& relocator, Args ...args) { | 
 | 568 |   return needs_slow_relocate_loop(relocator) ? | 
 | 569 |       packed_relocate_impl<RelocMode::General>(relocator, args...) : | 
 | 570 |       packed_relocate_impl<OptMode>(relocator, args...); | 
 | 571 | } | 
 | 572 |  | 
 | 573 | bool soinfo::relocate(const SymbolLookupList& lookup_list) { | 
 | 574 |  | 
 | 575 |   VersionTracker version_tracker; | 
 | 576 |  | 
 | 577 |   if (!version_tracker.init(this)) { | 
 | 578 |     return false; | 
 | 579 |   } | 
 | 580 |  | 
 | 581 |   Relocator relocator(version_tracker, lookup_list); | 
 | 582 |   relocator.si = this; | 
 | 583 |   relocator.si_strtab = strtab_; | 
 | 584 |   relocator.si_strtab_size = has_min_version(1) ? strtab_size_ : SIZE_MAX; | 
 | 585 |   relocator.si_symtab = symtab_; | 
 | 586 |   relocator.tlsdesc_args = &tlsdesc_args_; | 
 | 587 |   relocator.tls_tp_base = __libc_shared_globals()->static_tls_layout.offset_thread_pointer(); | 
 | 588 |  | 
 | 589 |   if (android_relocs_ != nullptr) { | 
 | 590 |     // check signature | 
 | 591 |     if (android_relocs_size_ > 3 && | 
 | 592 |         android_relocs_[0] == 'A' && | 
 | 593 |         android_relocs_[1] == 'P' && | 
 | 594 |         android_relocs_[2] == 'S' && | 
 | 595 |         android_relocs_[3] == '2') { | 
 | 596 |       DEBUG("[ android relocating %s ]", get_realpath()); | 
 | 597 |  | 
 | 598 |       const uint8_t* packed_relocs = android_relocs_ + 4; | 
 | 599 |       const size_t packed_relocs_size = android_relocs_size_ - 4; | 
 | 600 |  | 
 | 601 |       if (!packed_relocate<RelocMode::Typical>(relocator, sleb128_decoder(packed_relocs, packed_relocs_size))) { | 
 | 602 |         return false; | 
 | 603 |       } | 
 | 604 |     } else { | 
 | 605 |       DL_ERR("bad android relocation header."); | 
 | 606 |       return false; | 
 | 607 |     } | 
 | 608 |   } | 
 | 609 |  | 
 | 610 |   if (relr_ != nullptr) { | 
 | 611 |     DEBUG("[ relocating %s relr ]", get_realpath()); | 
 | 612 |     if (!relocate_relr()) { | 
 | 613 |       return false; | 
 | 614 |     } | 
 | 615 |   } | 
 | 616 |  | 
 | 617 | #if defined(USE_RELA) | 
 | 618 |   if (rela_ != nullptr) { | 
 | 619 |     DEBUG("[ relocating %s rela ]", get_realpath()); | 
 | 620 |  | 
 | 621 |     if (!plain_relocate<RelocMode::Typical>(relocator, rela_, rela_count_)) { | 
 | 622 |       return false; | 
 | 623 |     } | 
 | 624 |   } | 
 | 625 |   if (plt_rela_ != nullptr) { | 
 | 626 |     DEBUG("[ relocating %s plt rela ]", get_realpath()); | 
 | 627 |     if (!plain_relocate<RelocMode::JumpTable>(relocator, plt_rela_, plt_rela_count_)) { | 
 | 628 |       return false; | 
 | 629 |     } | 
 | 630 |   } | 
 | 631 | #else | 
 | 632 |   if (rel_ != nullptr) { | 
 | 633 |     DEBUG("[ relocating %s rel ]", get_realpath()); | 
 | 634 |     if (!plain_relocate<RelocMode::Typical>(relocator, rel_, rel_count_)) { | 
 | 635 |       return false; | 
 | 636 |     } | 
 | 637 |   } | 
 | 638 |   if (plt_rel_ != nullptr) { | 
 | 639 |     DEBUG("[ relocating %s plt rel ]", get_realpath()); | 
 | 640 |     if (!plain_relocate<RelocMode::JumpTable>(relocator, plt_rel_, plt_rel_count_)) { | 
 | 641 |       return false; | 
 | 642 |     } | 
 | 643 |   } | 
 | 644 | #endif | 
 | 645 |  | 
| Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 646 |   // Once the tlsdesc_args_ vector's size is finalized, we can write the addresses of its elements | 
 | 647 |   // into the TLSDESC relocations. | 
 | 648 | #if defined(__aarch64__) | 
 | 649 |   // Bionic currently only implements TLSDESC for arm64. | 
 | 650 |   for (const std::pair<TlsDescriptor*, size_t>& pair : relocator.deferred_tlsdesc_relocs) { | 
 | 651 |     TlsDescriptor* desc = pair.first; | 
 | 652 |     desc->func = tlsdesc_resolver_dynamic; | 
 | 653 |     desc->arg = reinterpret_cast<size_t>(&tlsdesc_args_[pair.second]); | 
 | 654 |   } | 
 | 655 | #endif | 
 | 656 |  | 
 | 657 |   return true; | 
 | 658 | } |