| File: | build/source/lld/ELF/Relocations.cpp |
| Warning: | line 2024, column 9 3rd function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | //===- Relocations.cpp ----------------------------------------------------===// | |||
| 2 | // | |||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
| 4 | // See https://llvm.org/LICENSE.txt for license information. | |||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
| 6 | // | |||
| 7 | //===----------------------------------------------------------------------===// | |||
| 8 | // | |||
| 9 | // This file contains platform-independent functions to process relocations. | |||
| 10 | // I'll describe the overview of this file here. | |||
| 11 | // | |||
| 12 | // Simple relocations are easy to handle for the linker. For example, | |||
| 13 | // for R_X86_64_PC64 relocs, the linker just has to fix up locations | |||
| 14 | // with the relative offsets to the target symbols. It would just be | |||
| 15 | // reading records from relocation sections and applying them to output. | |||
| 16 | // | |||
| 17 | // But not all relocations are that easy to handle. For example, for | |||
| 18 | // R_386_GOTOFF relocs, the linker has to create new GOT entries for | |||
| 19 | // symbols if they don't exist, and fix up locations with GOT entry | |||
| 20 | // offsets from the beginning of GOT section. So there is more than | |||
| 21 | // fixing addresses in relocation processing. | |||
| 22 | // | |||
| 23 | // ELF defines a large number of complex relocations. | |||
| 24 | // | |||
| 25 | // The functions in this file analyze relocations and do whatever needs | |||
| 26 | // to be done. It includes, but not limited to, the following. | |||
| 27 | // | |||
| 28 | // - create GOT/PLT entries | |||
| 29 | // - create new relocations in .dynsym to let the dynamic linker resolve | |||
| 30 | // them at runtime (since ELF supports dynamic linking, not all | |||
| 31 | // relocations can be resolved at link-time) | |||
| 32 | // - create COPY relocs and reserve space in .bss | |||
| 33 | // - replace expensive relocs (in terms of runtime cost) with cheap ones | |||
| 34 | // - error out infeasible combinations such as PIC and non-relative relocs | |||
| 35 | // | |||
| 36 | // Note that the functions in this file don't actually apply relocations | |||
| 37 | // because it doesn't know about the output file nor the output file buffer. | |||
| 38 | // It instead stores Relocation objects to InputSection's Relocations | |||
| 39 | // vector to let it apply later in InputSection::writeTo. | |||
| 40 | // | |||
| 41 | //===----------------------------------------------------------------------===// | |||
| 42 | ||||
| 43 | #include "Relocations.h" | |||
| 44 | #include "Config.h" | |||
| 45 | #include "InputFiles.h" | |||
| 46 | #include "LinkerScript.h" | |||
| 47 | #include "OutputSections.h" | |||
| 48 | #include "SymbolTable.h" | |||
| 49 | #include "Symbols.h" | |||
| 50 | #include "SyntheticSections.h" | |||
| 51 | #include "Target.h" | |||
| 52 | #include "Thunks.h" | |||
| 53 | #include "lld/Common/ErrorHandler.h" | |||
| 54 | #include "lld/Common/Memory.h" | |||
| 55 | #include "llvm/ADT/SmallSet.h" | |||
| 56 | #include "llvm/Demangle/Demangle.h" | |||
| 57 | #include "llvm/Support/Endian.h" | |||
| 58 | #include <algorithm> | |||
| 59 | ||||
| 60 | using namespace llvm; | |||
| 61 | using namespace llvm::ELF; | |||
| 62 | using namespace llvm::object; | |||
| 63 | using namespace llvm::support::endian; | |||
| 64 | using namespace lld; | |||
| 65 | using namespace lld::elf; | |||
| 66 | ||||
| 67 | static std::optional<std::string> getLinkerScriptLocation(const Symbol &sym) { | |||
| 68 | for (SectionCommand *cmd : script->sectionCommands) | |||
| 69 | if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) | |||
| 70 | if (assign->sym == &sym) | |||
| 71 | return assign->location; | |||
| 72 | return std::nullopt; | |||
| 73 | } | |||
| 74 | ||||
| 75 | static std::string getDefinedLocation(const Symbol &sym) { | |||
| 76 | const char msg[] = "\n>>> defined in "; | |||
| 77 | if (sym.file) | |||
| 78 | return msg + toString(sym.file); | |||
| 79 | if (std::optional<std::string> loc = getLinkerScriptLocation(sym)) | |||
| 80 | return msg + *loc; | |||
| 81 | return ""; | |||
| 82 | } | |||
| 83 | ||||
| 84 | // Construct a message in the following format. | |||
| 85 | // | |||
| 86 | // >>> defined in /home/alice/src/foo.o | |||
| 87 | // >>> referenced by bar.c:12 (/home/alice/src/bar.c:12) | |||
| 88 | // >>> /home/alice/src/bar.o:(.text+0x1) | |||
| 89 | static std::string getLocation(InputSectionBase &s, const Symbol &sym, | |||
| 90 | uint64_t off) { | |||
| 91 | std::string msg = getDefinedLocation(sym) + "\n>>> referenced by "; | |||
| 92 | std::string src = s.getSrcMsg(sym, off); | |||
| 93 | if (!src.empty()) | |||
| 94 | msg += src + "\n>>> "; | |||
| 95 | return msg + s.getObjMsg(off); | |||
| 96 | } | |||
| 97 | ||||
| 98 | void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, | |||
| 99 | int64_t min, uint64_t max) { | |||
| 100 | ErrorPlace errPlace = getErrorPlace(loc); | |||
| 101 | std::string hint; | |||
| 102 | if (rel.sym) { | |||
| 103 | if (!rel.sym->isSection()) | |||
| 104 | hint = "; references '" + lld::toString(*rel.sym) + '\''; | |||
| 105 | else if (auto *d = dyn_cast<Defined>(rel.sym)) | |||
| 106 | hint = ("; references section '" + d->section->name + "'").str(); | |||
| 107 | } | |||
| 108 | if (!errPlace.srcLoc.empty()) | |||
| 109 | hint += "\n>>> referenced by " + errPlace.srcLoc; | |||
| 110 | if (rel.sym && !rel.sym->isSection()) | |||
| 111 | hint += getDefinedLocation(*rel.sym); | |||
| 112 | ||||
| 113 | if (errPlace.isec && errPlace.isec->name.startswith(".debug")) | |||
| 114 | hint += "; consider recompiling with -fdebug-types-section to reduce size " | |||
| 115 | "of debug sections"; | |||
| 116 | ||||
| 117 | errorOrWarn(errPlace.loc + "relocation " + lld::toString(rel.type) + | |||
| 118 | " out of range: " + v.str() + " is not in [" + Twine(min).str() + | |||
| 119 | ", " + Twine(max).str() + "]" + hint); | |||
| 120 | } | |||
| 121 | ||||
| 122 | void elf::reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym, | |||
| 123 | const Twine &msg) { | |||
| 124 | ErrorPlace errPlace = getErrorPlace(loc); | |||
| 125 | std::string hint; | |||
| 126 | if (!sym.getName().empty()) | |||
| 127 | hint = | |||
| 128 | "; references '" + lld::toString(sym) + '\'' + getDefinedLocation(sym); | |||
| 129 | errorOrWarn(errPlace.loc + msg + " is out of range: " + Twine(v) + | |||
| 130 | " is not in [" + Twine(llvm::minIntN(n)) + ", " + | |||
| 131 | Twine(llvm::maxIntN(n)) + "]" + hint); | |||
| 132 | } | |||
| 133 | ||||
| 134 | // Build a bitmask with one bit set for each 64 subset of RelExpr. | |||
| 135 | static constexpr uint64_t buildMask() { return 0; } | |||
| 136 | ||||
| 137 | template <typename... Tails> | |||
| 138 | static constexpr uint64_t buildMask(int head, Tails... tails) { | |||
| 139 | return (0 <= head && head < 64 ? uint64_t(1) << head : 0) | | |||
| 140 | buildMask(tails...); | |||
| 141 | } | |||
| 142 | ||||
| 143 | // Return true if `Expr` is one of `Exprs`. | |||
| 144 | // There are more than 64 but less than 128 RelExprs, so we divide the set of | |||
| 145 | // exprs into [0, 64) and [64, 128) and represent each range as a constant | |||
| 146 | // 64-bit mask. Then we decide which mask to test depending on the value of | |||
| 147 | // expr and use a simple shift and bitwise-and to test for membership. | |||
| 148 | template <RelExpr... Exprs> static bool oneof(RelExpr expr) { | |||
| 149 | assert(0 <= expr && (int)expr < 128 &&(static_cast <bool> (0 <= expr && (int)expr < 128 && "RelExpr is too large for 128-bit mask!") ? void (0) : __assert_fail ("0 <= expr && (int)expr < 128 && \"RelExpr is too large for 128-bit mask!\"" , "lld/ELF/Relocations.cpp", 150, __extension__ __PRETTY_FUNCTION__ )) | |||
| 150 | "RelExpr is too large for 128-bit mask!")(static_cast <bool> (0 <= expr && (int)expr < 128 && "RelExpr is too large for 128-bit mask!") ? void (0) : __assert_fail ("0 <= expr && (int)expr < 128 && \"RelExpr is too large for 128-bit mask!\"" , "lld/ELF/Relocations.cpp", 150, __extension__ __PRETTY_FUNCTION__ )); | |||
| 151 | ||||
| 152 | if (expr >= 64) | |||
| 153 | return (uint64_t(1) << (expr - 64)) & buildMask((Exprs - 64)...); | |||
| 154 | return (uint64_t(1) << expr) & buildMask(Exprs...); | |||
| 155 | } | |||
| 156 | ||||
| 157 | static RelType getMipsPairType(RelType type, bool isLocal) { | |||
| 158 | switch (type) { | |||
| 159 | case R_MIPS_HI16: | |||
| 160 | return R_MIPS_LO16; | |||
| 161 | case R_MIPS_GOT16: | |||
| 162 | // In case of global symbol, the R_MIPS_GOT16 relocation does not | |||
| 163 | // have a pair. Each global symbol has a unique entry in the GOT | |||
| 164 | // and a corresponding instruction with help of the R_MIPS_GOT16 | |||
| 165 | // relocation loads an address of the symbol. In case of local | |||
| 166 | // symbol, the R_MIPS_GOT16 relocation creates a GOT entry to hold | |||
| 167 | // the high 16 bits of the symbol's value. A paired R_MIPS_LO16 | |||
| 168 | // relocations handle low 16 bits of the address. That allows | |||
| 169 | // to allocate only one GOT entry for every 64 KBytes of local data. | |||
| 170 | return isLocal ? R_MIPS_LO16 : R_MIPS_NONE; | |||
| 171 | case R_MICROMIPS_GOT16: | |||
| 172 | return isLocal ? R_MICROMIPS_LO16 : R_MIPS_NONE; | |||
| 173 | case R_MIPS_PCHI16: | |||
| 174 | return R_MIPS_PCLO16; | |||
| 175 | case R_MICROMIPS_HI16: | |||
| 176 | return R_MICROMIPS_LO16; | |||
| 177 | default: | |||
| 178 | return R_MIPS_NONE; | |||
| 179 | } | |||
| 180 | } | |||
| 181 | ||||
| 182 | // True if non-preemptable symbol always has the same value regardless of where | |||
| 183 | // the DSO is loaded. | |||
| 184 | static bool isAbsolute(const Symbol &sym) { | |||
| 185 | if (sym.isUndefWeak()) | |||
| 186 | return true; | |||
| 187 | if (const auto *dr = dyn_cast<Defined>(&sym)) | |||
| 188 | return dr->section == nullptr; // Absolute symbol. | |||
| 189 | return false; | |||
| 190 | } | |||
| 191 | ||||
| 192 | static bool isAbsoluteValue(const Symbol &sym) { | |||
| 193 | return isAbsolute(sym) || sym.isTls(); | |||
| 194 | } | |||
| 195 | ||||
| 196 | // Returns true if Expr refers a PLT entry. | |||
| 197 | static bool needsPlt(RelExpr expr) { | |||
| 198 | return oneof<R_PLT, R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT>( | |||
| 199 | expr); | |||
| 200 | } | |||
| 201 | ||||
| 202 | // Returns true if Expr refers a GOT entry. Note that this function | |||
| 203 | // returns false for TLS variables even though they need GOT, because | |||
| 204 | // TLS variables uses GOT differently than the regular variables. | |||
| 205 | static bool needsGot(RelExpr expr) { | |||
| 206 | return oneof<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF, | |||
| 207 | R_MIPS_GOT_OFF32, R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTPLT, | |||
| 208 | R_AARCH64_GOT_PAGE>(expr); | |||
| 209 | } | |||
| 210 | ||||
| 211 | // True if this expression is of the form Sym - X, where X is a position in the | |||
| 212 | // file (PC, or GOT for example). | |||
| 213 | static bool isRelExpr(RelExpr expr) { | |||
| 214 | return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC64_CALL, | |||
| 215 | R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC, | |||
| 216 | R_RISCV_PC_INDIRECT, R_PPC64_RELAX_GOT_PC>(expr); | |||
| 217 | } | |||
| 218 | ||||
| 219 | ||||
| 220 | static RelExpr toPlt(RelExpr expr) { | |||
| 221 | switch (expr) { | |||
| 222 | case R_PPC64_CALL: | |||
| 223 | return R_PPC64_CALL_PLT; | |||
| 224 | case R_PC: | |||
| 225 | return R_PLT_PC; | |||
| 226 | case R_ABS: | |||
| 227 | return R_PLT; | |||
| 228 | default: | |||
| 229 | return expr; | |||
| 230 | } | |||
| 231 | } | |||
| 232 | ||||
| 233 | static RelExpr fromPlt(RelExpr expr) { | |||
| 234 | // We decided not to use a plt. Optimize a reference to the plt to a | |||
| 235 | // reference to the symbol itself. | |||
| 236 | switch (expr) { | |||
| 237 | case R_PLT_PC: | |||
| 238 | case R_PPC32_PLTREL: | |||
| 239 | return R_PC; | |||
| 240 | case R_PPC64_CALL_PLT: | |||
| 241 | return R_PPC64_CALL; | |||
| 242 | case R_PLT: | |||
| 243 | return R_ABS; | |||
| 244 | case R_PLT_GOTPLT: | |||
| 245 | return R_GOTPLTREL; | |||
| 246 | default: | |||
| 247 | return expr; | |||
| 248 | } | |||
| 249 | } | |||
| 250 | ||||
| 251 | // Returns true if a given shared symbol is in a read-only segment in a DSO. | |||
| 252 | template <class ELFT> static bool isReadOnly(SharedSymbol &ss) { | |||
| 253 | using Elf_Phdr = typename ELFT::Phdr; | |||
| 254 | ||||
| 255 | // Determine if the symbol is read-only by scanning the DSO's program headers. | |||
| 256 | const auto &file = cast<SharedFile>(*ss.file); | |||
| 257 | for (const Elf_Phdr &phdr : | |||
| 258 | check(file.template getObj<ELFT>().program_headers())) | |||
| 259 | if ((phdr.p_type == ELF::PT_LOAD || phdr.p_type == ELF::PT_GNU_RELRO) && | |||
| 260 | !(phdr.p_flags & ELF::PF_W) && ss.value >= phdr.p_vaddr && | |||
| 261 | ss.value < phdr.p_vaddr + phdr.p_memsz) | |||
| 262 | return true; | |||
| 263 | return false; | |||
| 264 | } | |||
| 265 | ||||
| 266 | // Returns symbols at the same offset as a given symbol, including SS itself. | |||
| 267 | // | |||
| 268 | // If two or more symbols are at the same offset, and at least one of | |||
| 269 | // them are copied by a copy relocation, all of them need to be copied. | |||
| 270 | // Otherwise, they would refer to different places at runtime. | |||
| 271 | template <class ELFT> | |||
| 272 | static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) { | |||
| 273 | using Elf_Sym = typename ELFT::Sym; | |||
| 274 | ||||
| 275 | const auto &file = cast<SharedFile>(*ss.file); | |||
| 276 | ||||
| 277 | SmallSet<SharedSymbol *, 4> ret; | |||
| 278 | for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) { | |||
| 279 | if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS || | |||
| 280 | s.getType() == STT_TLS || s.st_value != ss.value) | |||
| 281 | continue; | |||
| 282 | StringRef name = check(s.getName(file.getStringTable())); | |||
| 283 | Symbol *sym = symtab.find(name); | |||
| 284 | if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym)) | |||
| 285 | ret.insert(alias); | |||
| 286 | } | |||
| 287 | ||||
| 288 | // The loop does not check SHT_GNU_verneed, so ret does not contain | |||
| 289 | // non-default version symbols. If ss has a non-default version, ret won't | |||
| 290 | // contain ss. Just add ss unconditionally. If a non-default version alias is | |||
| 291 | // separately copy relocated, it and ss will have different addresses. | |||
| 292 | // Fortunately this case is impractical and fails with GNU ld as well. | |||
| 293 | ret.insert(&ss); | |||
| 294 | return ret; | |||
| 295 | } | |||
| 296 | ||||
| 297 | // When a symbol is copy relocated or we create a canonical plt entry, it is | |||
| 298 | // effectively a defined symbol. In the case of copy relocation the symbol is | |||
| 299 | // in .bss and in the case of a canonical plt entry it is in .plt. This function | |||
| 300 | // replaces the existing symbol with a Defined pointing to the appropriate | |||
| 301 | // location. | |||
| 302 | static void replaceWithDefined(Symbol &sym, SectionBase &sec, uint64_t value, | |||
| 303 | uint64_t size) { | |||
| 304 | Symbol old = sym; | |||
| 305 | Defined(sym.file, StringRef(), sym.binding, sym.stOther, sym.type, value, | |||
| 306 | size, &sec) | |||
| 307 | .overwrite(sym); | |||
| 308 | ||||
| 309 | sym.verdefIndex = old.verdefIndex; | |||
| 310 | sym.exportDynamic = true; | |||
| 311 | sym.isUsedInRegularObj = true; | |||
| 312 | // A copy relocated alias may need a GOT entry. | |||
| 313 | sym.flags.store(old.flags.load(std::memory_order_relaxed) & NEEDS_GOT, | |||
| 314 | std::memory_order_relaxed); | |||
| 315 | } | |||
| 316 | ||||
| 317 | // Reserve space in .bss or .bss.rel.ro for copy relocation. | |||
| 318 | // | |||
| 319 | // The copy relocation is pretty much a hack. If you use a copy relocation | |||
| 320 | // in your program, not only the symbol name but the symbol's size, RW/RO | |||
| 321 | // bit and alignment become part of the ABI. In addition to that, if the | |||
| 322 | // symbol has aliases, the aliases become part of the ABI. That's subtle, | |||
| 323 | // but if you violate that implicit ABI, that can cause very counter- | |||
| 324 | // intuitive consequences. | |||
| 325 | // | |||
| 326 | // So, what is the copy relocation? It's for linking non-position | |||
| 327 | // independent code to DSOs. In an ideal world, all references to data | |||
| 328 | // exported by DSOs should go indirectly through GOT. But if object files | |||
| 329 | // are compiled as non-PIC, all data references are direct. There is no | |||
| 330 | // way for the linker to transform the code to use GOT, as machine | |||
| 331 | // instructions are already set in stone in object files. This is where | |||
| 332 | // the copy relocation takes a role. | |||
| 333 | // | |||
| 334 | // A copy relocation instructs the dynamic linker to copy data from a DSO | |||
| 335 | // to a specified address (which is usually in .bss) at load-time. If the | |||
| 336 | // static linker (that's us) finds a direct data reference to a DSO | |||
| 337 | // symbol, it creates a copy relocation, so that the symbol can be | |||
| 338 | // resolved as if it were in .bss rather than in a DSO. | |||
| 339 | // | |||
| 340 | // As you can see in this function, we create a copy relocation for the | |||
| 341 | // dynamic linker, and the relocation contains not only symbol name but | |||
| 342 | // various other information about the symbol. So, such attributes become a | |||
| 343 | // part of the ABI. | |||
| 344 | // | |||
| 345 | // Note for application developers: I can give you a piece of advice if | |||
| 346 | // you are writing a shared library. You probably should export only | |||
| 347 | // functions from your library. You shouldn't export variables. | |||
| 348 | // | |||
| 349 | // As an example what can happen when you export variables without knowing | |||
| 350 | // the semantics of copy relocations, assume that you have an exported | |||
| 351 | // variable of type T. It is an ABI-breaking change to add new members at | |||
| 352 | // end of T even though doing that doesn't change the layout of the | |||
| 353 | // existing members. That's because the space for the new members are not | |||
| 354 | // reserved in .bss unless you recompile the main program. That means they | |||
| 355 | // are likely to overlap with other data that happens to be laid out next | |||
| 356 | // to the variable in .bss. This kind of issue is sometimes very hard to | |||
| 357 | // debug. What's a solution? Instead of exporting a variable V from a DSO, | |||
| 358 | // define an accessor getV(). | |||
| 359 | template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) { | |||
| 360 | // Copy relocation against zero-sized symbol doesn't make sense. | |||
| 361 | uint64_t symSize = ss.getSize(); | |||
| 362 | if (symSize == 0 || ss.alignment == 0) | |||
| 363 | fatal("cannot create a copy relocation for symbol " + toString(ss)); | |||
| 364 | ||||
| 365 | // See if this symbol is in a read-only segment. If so, preserve the symbol's | |||
| 366 | // memory protection by reserving space in the .bss.rel.ro section. | |||
| 367 | bool isRO = isReadOnly<ELFT>(ss); | |||
| 368 | BssSection *sec = | |||
| 369 | make<BssSection>(isRO ? ".bss.rel.ro" : ".bss", symSize, ss.alignment); | |||
| 370 | OutputSection *osec = (isRO ? in.bssRelRo : in.bss)->getParent(); | |||
| 371 | ||||
| 372 | // At this point, sectionBases has been migrated to sections. Append sec to | |||
| 373 | // sections. | |||
| 374 | if (osec->commands.empty() || | |||
| 375 | !isa<InputSectionDescription>(osec->commands.back())) | |||
| 376 | osec->commands.push_back(make<InputSectionDescription>("")); | |||
| 377 | auto *isd = cast<InputSectionDescription>(osec->commands.back()); | |||
| 378 | isd->sections.push_back(sec); | |||
| 379 | osec->commitSection(sec); | |||
| 380 | ||||
| 381 | // Look through the DSO's dynamic symbol table for aliases and create a | |||
| 382 | // dynamic symbol for each one. This causes the copy relocation to correctly | |||
| 383 | // interpose any aliases. | |||
| 384 | for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss)) | |||
| 385 | replaceWithDefined(*sym, *sec, 0, sym->size); | |||
| 386 | ||||
| 387 | mainPart->relaDyn->addSymbolReloc(target->copyRel, *sec, 0, ss); | |||
| 388 | } | |||
| 389 | ||||
| 390 | // .eh_frame sections are mergeable input sections, so their input | |||
| 391 | // offsets are not linearly mapped to output section. For each input | |||
| 392 | // offset, we need to find a section piece containing the offset and | |||
| 393 | // add the piece's base address to the input offset to compute the | |||
| 394 | // output offset. That isn't cheap. | |||
| 395 | // | |||
| 396 | // This class is to speed up the offset computation. When we process | |||
| 397 | // relocations, we access offsets in the monotonically increasing | |||
| 398 | // order. So we can optimize for that access pattern. | |||
| 399 | // | |||
| 400 | // For sections other than .eh_frame, this class doesn't do anything. | |||
| 401 | namespace { | |||
| 402 | class OffsetGetter { | |||
| 403 | public: | |||
| 404 | OffsetGetter() = default; | |||
| 405 | explicit OffsetGetter(InputSectionBase &sec) { | |||
| 406 | if (auto *eh = dyn_cast<EhInputSection>(&sec)) { | |||
| 407 | cies = eh->cies; | |||
| 408 | fdes = eh->fdes; | |||
| 409 | i = cies.begin(); | |||
| 410 | j = fdes.begin(); | |||
| 411 | } | |||
| 412 | } | |||
| 413 | ||||
| 414 | // Translates offsets in input sections to offsets in output sections. | |||
| 415 | // Given offset must increase monotonically. We assume that Piece is | |||
| 416 | // sorted by inputOff. | |||
| 417 | uint64_t get(uint64_t off) { | |||
| 418 | if (cies.empty()) | |||
| 419 | return off; | |||
| 420 | ||||
| 421 | while (j != fdes.end() && j->inputOff <= off) | |||
| 422 | ++j; | |||
| 423 | auto it = j; | |||
| 424 | if (j == fdes.begin() || j[-1].inputOff + j[-1].size <= off) { | |||
| 425 | while (i != cies.end() && i->inputOff <= off) | |||
| 426 | ++i; | |||
| 427 | if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off) | |||
| 428 | fatal(".eh_frame: relocation is not in any piece"); | |||
| 429 | it = i; | |||
| 430 | } | |||
| 431 | ||||
| 432 | // Offset -1 means that the piece is dead (i.e. garbage collected). | |||
| 433 | if (it[-1].outputOff == -1) | |||
| 434 | return -1; | |||
| 435 | return it[-1].outputOff + (off - it[-1].inputOff); | |||
| 436 | } | |||
| 437 | ||||
| 438 | private: | |||
| 439 | ArrayRef<EhSectionPiece> cies, fdes; | |||
| 440 | ArrayRef<EhSectionPiece>::iterator i, j; | |||
| 441 | }; | |||
| 442 | ||||
| 443 | // This class encapsulates states needed to scan relocations for one | |||
| 444 | // InputSectionBase. | |||
| 445 | class RelocationScanner { | |||
| 446 | public: | |||
| 447 | template <class ELFT> void scanSection(InputSectionBase &s); | |||
| 448 | ||||
| 449 | private: | |||
| 450 | InputSectionBase *sec; | |||
| 451 | OffsetGetter getter; | |||
| 452 | ||||
| 453 | // End of relocations, used by Mips/PPC64. | |||
| 454 | const void *end = nullptr; | |||
| 455 | ||||
| 456 | template <class RelTy> RelType getMipsN32RelType(RelTy *&rel) const; | |||
| 457 | template <class ELFT, class RelTy> | |||
| 458 | int64_t computeMipsAddend(const RelTy &rel, RelExpr expr, bool isLocal) const; | |||
| 459 | bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym, | |||
| 460 | uint64_t relOff) const; | |||
| 461 | void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym, | |||
| 462 | int64_t addend) const; | |||
| 463 | template <class ELFT, class RelTy> void scanOne(RelTy *&i); | |||
| 464 | template <class ELFT, class RelTy> void scan(ArrayRef<RelTy> rels); | |||
| 465 | }; | |||
| 466 | } // namespace | |||
| 467 | ||||
| 468 | // MIPS has an odd notion of "paired" relocations to calculate addends. | |||
| 469 | // For example, if a relocation is of R_MIPS_HI16, there must be a | |||
| 470 | // R_MIPS_LO16 relocation after that, and an addend is calculated using | |||
| 471 | // the two relocations. | |||
| 472 | template <class ELFT, class RelTy> | |||
| 473 | int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr, | |||
| 474 | bool isLocal) const { | |||
| 475 | if (expr == R_MIPS_GOTREL && isLocal) | |||
| 476 | return sec->getFile<ELFT>()->mipsGp0; | |||
| 477 | ||||
| 478 | // The ABI says that the paired relocation is used only for REL. | |||
| 479 | // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf | |||
| 480 | if (RelTy::IsRela) | |||
| 481 | return 0; | |||
| 482 | ||||
| 483 | RelType type = rel.getType(config->isMips64EL); | |||
| 484 | uint32_t pairTy = getMipsPairType(type, isLocal); | |||
| 485 | if (pairTy == R_MIPS_NONE) | |||
| 486 | return 0; | |||
| 487 | ||||
| 488 | const uint8_t *buf = sec->content().data(); | |||
| 489 | uint32_t symIndex = rel.getSymbol(config->isMips64EL); | |||
| 490 | ||||
| 491 | // To make things worse, paired relocations might not be contiguous in | |||
| 492 | // the relocation table, so we need to do linear search. *sigh* | |||
| 493 | for (const RelTy *ri = &rel; ri != static_cast<const RelTy *>(end); ++ri) | |||
| 494 | if (ri->getType(config->isMips64EL) == pairTy && | |||
| 495 | ri->getSymbol(config->isMips64EL) == symIndex) | |||
| 496 | return target->getImplicitAddend(buf + ri->r_offset, pairTy); | |||
| 497 | ||||
| 498 | warn("can't find matching " + toString(pairTy) + " relocation for " + | |||
| 499 | toString(type)); | |||
| 500 | return 0; | |||
| 501 | } | |||
| 502 | ||||
| 503 | // Custom error message if Sym is defined in a discarded section. | |||
| 504 | template <class ELFT> | |||
| 505 | static std::string maybeReportDiscarded(Undefined &sym) { | |||
| 506 | auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file); | |||
| 507 | if (!file || !sym.discardedSecIdx || | |||
| 508 | file->getSections()[sym.discardedSecIdx] != &InputSection::discarded) | |||
| 509 | return ""; | |||
| 510 | ArrayRef<typename ELFT::Shdr> objSections = | |||
| 511 | file->template getELFShdrs<ELFT>(); | |||
| 512 | ||||
| 513 | std::string msg; | |||
| 514 | if (sym.type == ELF::STT_SECTION) { | |||
| 515 | msg = "relocation refers to a discarded section: "; | |||
| 516 | msg += CHECK(check2((file->getObj().getSectionName(objSections[sym.discardedSecIdx ])), [&] { return toString(file); }) | |||
| 517 | file->getObj().getSectionName(objSections[sym.discardedSecIdx]), file)check2((file->getObj().getSectionName(objSections[sym.discardedSecIdx ])), [&] { return toString(file); }); | |||
| 518 | } else { | |||
| 519 | msg = "relocation refers to a symbol in a discarded section: " + | |||
| 520 | toString(sym); | |||
| 521 | } | |||
| 522 | msg += "\n>>> defined in " + toString(file); | |||
| 523 | ||||
| 524 | Elf_Shdr_Impl<ELFT> elfSec = objSections[sym.discardedSecIdx - 1]; | |||
| 525 | if (elfSec.sh_type != SHT_GROUP) | |||
| 526 | return msg; | |||
| 527 | ||||
| 528 | // If the discarded section is a COMDAT. | |||
| 529 | StringRef signature = file->getShtGroupSignature(objSections, elfSec); | |||
| 530 | if (const InputFile *prevailing = | |||
| 531 | symtab.comdatGroups.lookup(CachedHashStringRef(signature))) { | |||
| 532 | msg += "\n>>> section group signature: " + signature.str() + | |||
| 533 | "\n>>> prevailing definition is in " + toString(prevailing); | |||
| 534 | if (sym.nonPrevailing) { | |||
| 535 | msg += "\n>>> or the symbol in the prevailing group had STB_WEAK " | |||
| 536 | "binding and the symbol in a non-prevailing group had STB_GLOBAL " | |||
| 537 | "binding. Mixing groups with STB_WEAK and STB_GLOBAL binding " | |||
| 538 | "signature is not supported"; | |||
| 539 | } | |||
| 540 | } | |||
| 541 | return msg; | |||
| 542 | } | |||
| 543 | ||||
| 544 | namespace { | |||
| 545 | // Undefined diagnostics are collected in a vector and emitted once all of | |||
| 546 | // them are known, so that some postprocessing on the list of undefined symbols | |||
| 547 | // can happen before lld emits diagnostics. | |||
| 548 | struct UndefinedDiag { | |||
| 549 | Undefined *sym; | |||
| 550 | struct Loc { | |||
| 551 | InputSectionBase *sec; | |||
| 552 | uint64_t offset; | |||
| 553 | }; | |||
| 554 | std::vector<Loc> locs; | |||
| 555 | bool isWarning; | |||
| 556 | }; | |||
| 557 | ||||
| 558 | std::vector<UndefinedDiag> undefs; | |||
| 559 | std::mutex relocMutex; | |||
| 560 | } | |||
| 561 | ||||
| 562 | // Check whether the definition name def is a mangled function name that matches | |||
| 563 | // the reference name ref. | |||
| 564 | static bool canSuggestExternCForCXX(StringRef ref, StringRef def) { | |||
| 565 | llvm::ItaniumPartialDemangler d; | |||
| 566 | std::string name = def.str(); | |||
| 567 | if (d.partialDemangle(name.c_str())) | |||
| 568 | return false; | |||
| 569 | char *buf = d.getFunctionName(nullptr, nullptr); | |||
| 570 | if (!buf) | |||
| 571 | return false; | |||
| 572 | bool ret = ref == buf; | |||
| 573 | free(buf); | |||
| 574 | return ret; | |||
| 575 | } | |||
| 576 | ||||
| 577 | // Suggest an alternative spelling of an "undefined symbol" diagnostic. Returns | |||
| 578 | // the suggested symbol, which is either in the symbol table, or in the same | |||
| 579 | // file of sym. | |||
| 580 | static const Symbol *getAlternativeSpelling(const Undefined &sym, | |||
| 581 | std::string &pre_hint, | |||
| 582 | std::string &post_hint) { | |||
| 583 | DenseMap<StringRef, const Symbol *> map; | |||
| 584 | if (sym.file && sym.file->kind() == InputFile::ObjKind) { | |||
| 585 | auto *file = cast<ELFFileBase>(sym.file); | |||
| 586 | // If sym is a symbol defined in a discarded section, maybeReportDiscarded() | |||
| 587 | // will give an error. Don't suggest an alternative spelling. | |||
| 588 | if (file && sym.discardedSecIdx != 0 && | |||
| 589 | file->getSections()[sym.discardedSecIdx] == &InputSection::discarded) | |||
| 590 | return nullptr; | |||
| 591 | ||||
| 592 | // Build a map of local defined symbols. | |||
| 593 | for (const Symbol *s : sym.file->getSymbols()) | |||
| 594 | if (s->isLocal() && s->isDefined() && !s->getName().empty()) | |||
| 595 | map.try_emplace(s->getName(), s); | |||
| 596 | } | |||
| 597 | ||||
| 598 | auto suggest = [&](StringRef newName) -> const Symbol * { | |||
| 599 | // If defined locally. | |||
| 600 | if (const Symbol *s = map.lookup(newName)) | |||
| 601 | return s; | |||
| 602 | ||||
| 603 | // If in the symbol table and not undefined. | |||
| 604 | if (const Symbol *s = symtab.find(newName)) | |||
| 605 | if (!s->isUndefined()) | |||
| 606 | return s; | |||
| 607 | ||||
| 608 | return nullptr; | |||
| 609 | }; | |||
| 610 | ||||
| 611 | // This loop enumerates all strings of Levenshtein distance 1 as typo | |||
| 612 | // correction candidates and suggests the one that exists as a non-undefined | |||
| 613 | // symbol. | |||
| 614 | StringRef name = sym.getName(); | |||
| 615 | for (size_t i = 0, e = name.size(); i != e + 1; ++i) { | |||
| 616 | // Insert a character before name[i]. | |||
| 617 | std::string newName = (name.substr(0, i) + "0" + name.substr(i)).str(); | |||
| 618 | for (char c = '0'; c <= 'z'; ++c) { | |||
| 619 | newName[i] = c; | |||
| 620 | if (const Symbol *s = suggest(newName)) | |||
| 621 | return s; | |||
| 622 | } | |||
| 623 | if (i == e) | |||
| 624 | break; | |||
| 625 | ||||
| 626 | // Substitute name[i]. | |||
| 627 | newName = std::string(name); | |||
| 628 | for (char c = '0'; c <= 'z'; ++c) { | |||
| 629 | newName[i] = c; | |||
| 630 | if (const Symbol *s = suggest(newName)) | |||
| 631 | return s; | |||
| 632 | } | |||
| 633 | ||||
| 634 | // Transpose name[i] and name[i+1]. This is of edit distance 2 but it is | |||
| 635 | // common. | |||
| 636 | if (i + 1 < e) { | |||
| 637 | newName[i] = name[i + 1]; | |||
| 638 | newName[i + 1] = name[i]; | |||
| 639 | if (const Symbol *s = suggest(newName)) | |||
| 640 | return s; | |||
| 641 | } | |||
| 642 | ||||
| 643 | // Delete name[i]. | |||
| 644 | newName = (name.substr(0, i) + name.substr(i + 1)).str(); | |||
| 645 | if (const Symbol *s = suggest(newName)) | |||
| 646 | return s; | |||
| 647 | } | |||
| 648 | ||||
| 649 | // Case mismatch, e.g. Foo vs FOO. | |||
| 650 | for (auto &it : map) | |||
| 651 | if (name.equals_insensitive(it.first)) | |||
| 652 | return it.second; | |||
| 653 | for (Symbol *sym : symtab.getSymbols()) | |||
| 654 | if (!sym->isUndefined() && name.equals_insensitive(sym->getName())) | |||
| 655 | return sym; | |||
| 656 | ||||
| 657 | // The reference may be a mangled name while the definition is not. Suggest a | |||
| 658 | // missing extern "C". | |||
| 659 | if (name.startswith("_Z")) { | |||
| 660 | std::string buf = name.str(); | |||
| 661 | llvm::ItaniumPartialDemangler d; | |||
| 662 | if (!d.partialDemangle(buf.c_str())) | |||
| 663 | if (char *buf = d.getFunctionName(nullptr, nullptr)) { | |||
| 664 | const Symbol *s = suggest(buf); | |||
| 665 | free(buf); | |||
| 666 | if (s) { | |||
| 667 | pre_hint = ": extern \"C\" "; | |||
| 668 | return s; | |||
| 669 | } | |||
| 670 | } | |||
| 671 | } else { | |||
| 672 | const Symbol *s = nullptr; | |||
| 673 | for (auto &it : map) | |||
| 674 | if (canSuggestExternCForCXX(name, it.first)) { | |||
| 675 | s = it.second; | |||
| 676 | break; | |||
| 677 | } | |||
| 678 | if (!s) | |||
| 679 | for (Symbol *sym : symtab.getSymbols()) | |||
| 680 | if (canSuggestExternCForCXX(name, sym->getName())) { | |||
| 681 | s = sym; | |||
| 682 | break; | |||
| 683 | } | |||
| 684 | if (s) { | |||
| 685 | pre_hint = " to declare "; | |||
| 686 | post_hint = " as extern \"C\"?"; | |||
| 687 | return s; | |||
| 688 | } | |||
| 689 | } | |||
| 690 | ||||
| 691 | return nullptr; | |||
| 692 | } | |||
| 693 | ||||
| 694 | static void reportUndefinedSymbol(const UndefinedDiag &undef, | |||
| 695 | bool correctSpelling) { | |||
| 696 | Undefined &sym = *undef.sym; | |||
| 697 | ||||
| 698 | auto visibility = [&]() -> std::string { | |||
| 699 | switch (sym.visibility()) { | |||
| 700 | case STV_INTERNAL: | |||
| 701 | return "internal "; | |||
| 702 | case STV_HIDDEN: | |||
| 703 | return "hidden "; | |||
| 704 | case STV_PROTECTED: | |||
| 705 | return "protected "; | |||
| 706 | default: | |||
| 707 | return ""; | |||
| 708 | } | |||
| 709 | }; | |||
| 710 | ||||
| 711 | std::string msg; | |||
| 712 | switch (config->ekind) { | |||
| 713 | case ELF32LEKind: | |||
| 714 | msg = maybeReportDiscarded<ELF32LE>(sym); | |||
| 715 | break; | |||
| 716 | case ELF32BEKind: | |||
| 717 | msg = maybeReportDiscarded<ELF32BE>(sym); | |||
| 718 | break; | |||
| 719 | case ELF64LEKind: | |||
| 720 | msg = maybeReportDiscarded<ELF64LE>(sym); | |||
| 721 | break; | |||
| 722 | case ELF64BEKind: | |||
| 723 | msg = maybeReportDiscarded<ELF64BE>(sym); | |||
| 724 | break; | |||
| 725 | default: | |||
| 726 | llvm_unreachable("")::llvm::llvm_unreachable_internal("", "lld/ELF/Relocations.cpp" , 726); | |||
| 727 | } | |||
| 728 | if (msg.empty()) | |||
| 729 | msg = "undefined " + visibility() + "symbol: " + toString(sym); | |||
| 730 | ||||
| 731 | const size_t maxUndefReferences = 3; | |||
| 732 | size_t i = 0; | |||
| 733 | for (UndefinedDiag::Loc l : undef.locs) { | |||
| 734 | if (i >= maxUndefReferences) | |||
| 735 | break; | |||
| 736 | InputSectionBase &sec = *l.sec; | |||
| 737 | uint64_t offset = l.offset; | |||
| 738 | ||||
| 739 | msg += "\n>>> referenced by "; | |||
| 740 | std::string src = sec.getSrcMsg(sym, offset); | |||
| 741 | if (!src.empty()) | |||
| 742 | msg += src + "\n>>> "; | |||
| 743 | msg += sec.getObjMsg(offset); | |||
| 744 | i++; | |||
| 745 | } | |||
| 746 | ||||
| 747 | if (i < undef.locs.size()) | |||
| 748 | msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times") | |||
| 749 | .str(); | |||
| 750 | ||||
| 751 | if (correctSpelling) { | |||
| 752 | std::string pre_hint = ": ", post_hint; | |||
| 753 | if (const Symbol *corrected = | |||
| 754 | getAlternativeSpelling(sym, pre_hint, post_hint)) { | |||
| 755 | msg += "\n>>> did you mean" + pre_hint + toString(*corrected) + post_hint; | |||
| 756 | if (corrected->file) | |||
| 757 | msg += "\n>>> defined in: " + toString(corrected->file); | |||
| 758 | } | |||
| 759 | } | |||
| 760 | ||||
| 761 | if (sym.getName().startswith("_ZTV")) | |||
| 762 | msg += | |||
| 763 | "\n>>> the vtable symbol may be undefined because the class is missing " | |||
| 764 | "its key function (see https://lld.llvm.org/missingkeyfunction)"; | |||
| 765 | if (config->gcSections && config->zStartStopGC && | |||
| 766 | sym.getName().startswith("__start_")) { | |||
| 767 | msg += "\n>>> the encapsulation symbol needs to be retained under " | |||
| 768 | "--gc-sections properly; consider -z nostart-stop-gc " | |||
| 769 | "(see https://lld.llvm.org/ELF/start-stop-gc)"; | |||
| 770 | } | |||
| 771 | ||||
| 772 | if (undef.isWarning) | |||
| 773 | warn(msg); | |||
| 774 | else | |||
| 775 | error(msg, ErrorTag::SymbolNotFound, {sym.getName()}); | |||
| 776 | } | |||
| 777 | ||||
| 778 | void elf::reportUndefinedSymbols() { | |||
| 779 | // Find the first "undefined symbol" diagnostic for each diagnostic, and | |||
| 780 | // collect all "referenced from" lines at the first diagnostic. | |||
| 781 | DenseMap<Symbol *, UndefinedDiag *> firstRef; | |||
| 782 | for (UndefinedDiag &undef : undefs) { | |||
| 783 | assert(undef.locs.size() == 1)(static_cast <bool> (undef.locs.size() == 1) ? void (0) : __assert_fail ("undef.locs.size() == 1", "lld/ELF/Relocations.cpp" , 783, __extension__ __PRETTY_FUNCTION__)); | |||
| 784 | if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) { | |||
| 785 | canon->locs.push_back(undef.locs[0]); | |||
| 786 | undef.locs.clear(); | |||
| 787 | } else | |||
| 788 | firstRef[undef.sym] = &undef; | |||
| 789 | } | |||
| 790 | ||||
| 791 | // Enable spell corrector for the first 2 diagnostics. | |||
| 792 | for (const auto &[i, undef] : llvm::enumerate(undefs)) | |||
| 793 | if (!undef.locs.empty()) | |||
| 794 | reportUndefinedSymbol(undef, i < 2); | |||
| 795 | undefs.clear(); | |||
| 796 | } | |||
| 797 | ||||
| 798 | // Report an undefined symbol if necessary. | |||
| 799 | // Returns true if the undefined symbol will produce an error message. | |||
| 800 | static bool maybeReportUndefined(Undefined &sym, InputSectionBase &sec, | |||
| 801 | uint64_t offset) { | |||
| 802 | std::lock_guard<std::mutex> lock(relocMutex); | |||
| 803 | // If versioned, issue an error (even if the symbol is weak) because we don't | |||
| 804 | // know the defining filename which is required to construct a Verneed entry. | |||
| 805 | if (sym.hasVersionSuffix) { | |||
| 806 | undefs.push_back({&sym, {{&sec, offset}}, false}); | |||
| 807 | return true; | |||
| 808 | } | |||
| 809 | if (sym.isWeak()) | |||
| 810 | return false; | |||
| 811 | ||||
| 812 | bool canBeExternal = !sym.isLocal() && sym.visibility() == STV_DEFAULT; | |||
| 813 | if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal) | |||
| 814 | return false; | |||
| 815 | ||||
| 816 | // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc | |||
| 817 | // which references a switch table in a discarded .rodata/.text section. The | |||
| 818 | // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF | |||
| 819 | // spec says references from outside the group to a STB_LOCAL symbol are not | |||
| 820 | // allowed. Work around the bug. | |||
| 821 | // | |||
| 822 | // PPC32 .got2 is similar but cannot be fixed. Multiple .got2 is infeasible | |||
| 823 | // because .LC0-.LTOC is not representable if the two labels are in different | |||
| 824 | // .got2 | |||
| 825 | if (sym.discardedSecIdx != 0 && (sec.name == ".got2" || sec.name == ".toc")) | |||
| 826 | return false; | |||
| 827 | ||||
| 828 | bool isWarning = | |||
| 829 | (config->unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) || | |||
| 830 | config->noinhibitExec; | |||
| 831 | undefs.push_back({&sym, {{&sec, offset}}, isWarning}); | |||
| 832 | return !isWarning; | |||
| 833 | } | |||
| 834 | ||||
| 835 | // MIPS N32 ABI treats series of successive relocations with the same offset | |||
| 836 | // as a single relocation. The similar approach used by N64 ABI, but this ABI | |||
| 837 | // packs all relocations into the single relocation record. Here we emulate | |||
| 838 | // this for the N32 ABI. Iterate over relocation with the same offset and put | |||
| 839 | // theirs types into the single bit-set. | |||
| 840 | template <class RelTy> | |||
| 841 | RelType RelocationScanner::getMipsN32RelType(RelTy *&rel) const { | |||
| 842 | RelType type = 0; | |||
| 843 | uint64_t offset = rel->r_offset; | |||
| 844 | ||||
| 845 | int n = 0; | |||
| 846 | while (rel != static_cast<const RelTy *>(end) && rel->r_offset == offset) | |||
| 847 | type |= (rel++)->getType(config->isMips64EL) << (8 * n++); | |||
| 848 | return type; | |||
| 849 | } | |||
| 850 | ||||
| 851 | template <bool shard = false> | |||
| 852 | static void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec, | |||
| 853 | Symbol &sym, int64_t addend, RelExpr expr, | |||
| 854 | RelType type) { | |||
| 855 | Partition &part = isec.getPartition(); | |||
| 856 | ||||
| 857 | // Add a relative relocation. If relrDyn section is enabled, and the | |||
| 858 | // relocation offset is guaranteed to be even, add the relocation to | |||
| 859 | // the relrDyn section, otherwise add it to the relaDyn section. | |||
| 860 | // relrDyn sections don't support odd offsets. Also, relrDyn sections | |||
| 861 | // don't store the addend values, so we must write it to the relocated | |||
| 862 | // address. | |||
| 863 | if (part.relrDyn && isec.addralign >= 2 && offsetInSec % 2 == 0) { | |||
| 864 | isec.addReloc({expr, type, offsetInSec, addend, &sym}); | |||
| 865 | if (shard) | |||
| 866 | part.relrDyn->relocsVec[parallel::getThreadIndex()].push_back( | |||
| 867 | {&isec, offsetInSec}); | |||
| 868 | else | |||
| 869 | part.relrDyn->relocs.push_back({&isec, offsetInSec}); | |||
| 870 | return; | |||
| 871 | } | |||
| 872 | part.relaDyn->addRelativeReloc<shard>(target->relativeRel, isec, offsetInSec, | |||
| 873 | sym, addend, type, expr); | |||
| 874 | } | |||
| 875 | ||||
| 876 | template <class PltSection, class GotPltSection> | |||
| 877 | static void addPltEntry(PltSection &plt, GotPltSection &gotPlt, | |||
| 878 | RelocationBaseSection &rel, RelType type, Symbol &sym) { | |||
| 879 | plt.addEntry(sym); | |||
| 880 | gotPlt.addEntry(sym); | |||
| 881 | rel.addReloc({type, &gotPlt, sym.getGotPltOffset(), | |||
| 882 | sym.isPreemptible ? DynamicReloc::AgainstSymbol | |||
| 883 | : DynamicReloc::AddendOnlyWithTargetVA, | |||
| 884 | sym, 0, R_ABS}); | |||
| 885 | } | |||
| 886 | ||||
| 887 | static void addGotEntry(Symbol &sym) { | |||
| 888 | in.got->addEntry(sym); | |||
| 889 | uint64_t off = sym.getGotOffset(); | |||
| 890 | ||||
| 891 | // If preemptible, emit a GLOB_DAT relocation. | |||
| 892 | if (sym.isPreemptible) { | |||
| 893 | mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off, | |||
| 894 | DynamicReloc::AgainstSymbol, sym, 0, R_ABS}); | |||
| 895 | return; | |||
| 896 | } | |||
| 897 | ||||
| 898 | // Otherwise, the value is either a link-time constant or the load base | |||
| 899 | // plus a constant. | |||
| 900 | if (!config->isPic || isAbsolute(sym)) | |||
| 901 | in.got->addConstant({R_ABS, target->symbolicRel, off, 0, &sym}); | |||
| 902 | else | |||
| 903 | addRelativeReloc(*in.got, off, sym, 0, R_ABS, target->symbolicRel); | |||
| 904 | } | |||
| 905 | ||||
| 906 | static void addTpOffsetGotEntry(Symbol &sym) { | |||
| 907 | in.got->addEntry(sym); | |||
| 908 | uint64_t off = sym.getGotOffset(); | |||
| 909 | if (!sym.isPreemptible && !config->isPic) { | |||
| 910 | in.got->addConstant({R_TPREL, target->symbolicRel, off, 0, &sym}); | |||
| 911 | return; | |||
| 912 | } | |||
| 913 | mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( | |||
| 914 | target->tlsGotRel, *in.got, off, sym, target->symbolicRel); | |||
| 915 | } | |||
| 916 | ||||
| 917 | // Return true if we can define a symbol in the executable that | |||
| 918 | // contains the value/function of a symbol defined in a shared | |||
| 919 | // library. | |||
| 920 | static bool canDefineSymbolInExecutable(Symbol &sym) { | |||
| 921 | // If the symbol has default visibility the symbol defined in the | |||
| 922 | // executable will preempt it. | |||
| 923 | // Note that we want the visibility of the shared symbol itself, not | |||
| 924 | // the visibility of the symbol in the output file we are producing. | |||
| 925 | if (!sym.dsoProtected) | |||
| 926 | return true; | |||
| 927 | ||||
| 928 | // If we are allowed to break address equality of functions, defining | |||
| 929 | // a plt entry will allow the program to call the function in the | |||
| 930 | // .so, but the .so and the executable will no agree on the address | |||
| 931 | // of the function. Similar logic for objects. | |||
| 932 | return ((sym.isFunc() && config->ignoreFunctionAddressEquality) || | |||
| 933 | (sym.isObject() && config->ignoreDataAddressEquality)); | |||
| 934 | } | |||
| 935 | ||||
| 936 | // Returns true if a given relocation can be computed at link-time. | |||
| 937 | // This only handles relocation types expected in processAux. | |||
| 938 | // | |||
| 939 | // For instance, we know the offset from a relocation to its target at | |||
| 940 | // link-time if the relocation is PC-relative and refers a | |||
| 941 | // non-interposable function in the same executable. This function | |||
| 942 | // will return true for such relocation. | |||
| 943 | // | |||
| 944 | // If this function returns false, that means we need to emit a | |||
| 945 | // dynamic relocation so that the relocation will be fixed at load-time. | |||
| 946 | bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type, | |||
| 947 | const Symbol &sym, | |||
| 948 | uint64_t relOff) const { | |||
| 949 | // These expressions always compute a constant | |||
| 950 | if (oneof<R_GOTPLT, R_GOT_OFF, R_RELAX_HINT, R_MIPS_GOT_LOCAL_PAGE, | |||
| 951 | R_MIPS_GOTREL, R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC, | |||
| 952 | R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC, | |||
| 953 | R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT, | |||
| 954 | R_PPC64_RELAX_TOC, R_RISCV_ADD, R_AARCH64_GOT_PAGE>(e)) | |||
| 955 | return true; | |||
| 956 | ||||
| 957 | // These never do, except if the entire file is position dependent or if | |||
| 958 | // only the low bits are used. | |||
| 959 | if (e == R_GOT || e == R_PLT) | |||
| 960 | return target->usesOnlyLowPageBits(type) || !config->isPic; | |||
| 961 | ||||
| 962 | if (sym.isPreemptible) | |||
| 963 | return false; | |||
| 964 | if (!config->isPic) | |||
| 965 | return true; | |||
| 966 | ||||
| 967 | // The size of a non preemptible symbol is a constant. | |||
| 968 | if (e == R_SIZE) | |||
| 969 | return true; | |||
| 970 | ||||
| 971 | // For the target and the relocation, we want to know if they are | |||
| 972 | // absolute or relative. | |||
| 973 | bool absVal = isAbsoluteValue(sym); | |||
| 974 | bool relE = isRelExpr(e); | |||
| 975 | if (absVal && !relE) | |||
| 976 | return true; | |||
| 977 | if (!absVal && relE) | |||
| 978 | return true; | |||
| 979 | if (!absVal && !relE) | |||
| 980 | return target->usesOnlyLowPageBits(type); | |||
| 981 | ||||
| 982 | assert(absVal && relE)(static_cast <bool> (absVal && relE) ? void (0) : __assert_fail ("absVal && relE", "lld/ELF/Relocations.cpp" , 982, __extension__ __PRETTY_FUNCTION__)); | |||
| 983 | ||||
| 984 | // Allow R_PLT_PC (optimized to R_PC here) to a hidden undefined weak symbol | |||
| 985 | // in PIC mode. This is a little strange, but it allows us to link function | |||
| 986 | // calls to such symbols (e.g. glibc/stdlib/exit.c:__run_exit_handlers). | |||
| 987 | // Normally such a call will be guarded with a comparison, which will load a | |||
| 988 | // zero from the GOT. | |||
| 989 | if (sym.isUndefWeak()) | |||
| 990 | return true; | |||
| 991 | ||||
| 992 | // We set the final symbols values for linker script defined symbols later. | |||
| 993 | // They always can be computed as a link time constant. | |||
| 994 | if (sym.scriptDefined) | |||
| 995 | return true; | |||
| 996 | ||||
| 997 | error("relocation " + toString(type) + " cannot refer to absolute symbol: " + | |||
| 998 | toString(sym) + getLocation(*sec, sym, relOff)); | |||
| 999 | return true; | |||
| 1000 | } | |||
| 1001 | ||||
| 1002 | // The reason we have to do this early scan is as follows | |||
| 1003 | // * To mmap the output file, we need to know the size | |||
| 1004 | // * For that, we need to know how many dynamic relocs we will have. | |||
| 1005 | // It might be possible to avoid this by outputting the file with write: | |||
| 1006 | // * Write the allocated output sections, computing addresses. | |||
| 1007 | // * Apply relocations, recording which ones require a dynamic reloc. | |||
| 1008 | // * Write the dynamic relocations. | |||
| 1009 | // * Write the rest of the file. | |||
| 1010 | // This would have some drawbacks. For example, we would only know if .rela.dyn | |||
| 1011 | // is needed after applying relocations. If it is, it will go after rw and rx | |||
| 1012 | // sections. Given that it is ro, we will need an extra PT_LOAD. This | |||
| 1013 | // complicates things for the dynamic linker and means we would have to reserve | |||
| 1014 | // space for the extra PT_LOAD even if we end up not using it. | |||
| 1015 | void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset, | |||
| 1016 | Symbol &sym, int64_t addend) const { | |||
| 1017 | // If non-ifunc non-preemptible, change PLT to direct call and optimize GOT | |||
| 1018 | // indirection. | |||
| 1019 | const bool isIfunc = sym.isGnuIFunc(); | |||
| 1020 | if (!sym.isPreemptible && (!isIfunc || config->zIfuncNoplt)) { | |||
| 1021 | if (expr != R_GOT_PC) { | |||
| 1022 | // The 0x8000 bit of r_addend of R_PPC_PLTREL24 is used to choose call | |||
| 1023 | // stub type. It should be ignored if optimized to R_PC. | |||
| 1024 | if (config->emachine == EM_PPC && expr == R_PPC32_PLTREL) | |||
| 1025 | addend &= ~0x8000; | |||
| 1026 | // R_HEX_GD_PLT_B22_PCREL (call a@GDPLT) is transformed into | |||
| 1027 | // call __tls_get_addr even if the symbol is non-preemptible. | |||
| 1028 | if (!(config->emachine == EM_HEXAGON && | |||
| 1029 | (type == R_HEX_GD_PLT_B22_PCREL || | |||
| 1030 | type == R_HEX_GD_PLT_B22_PCREL_X || | |||
| 1031 | type == R_HEX_GD_PLT_B32_PCREL_X))) | |||
| 1032 | expr = fromPlt(expr); | |||
| 1033 | } else if (!isAbsoluteValue(sym)) { | |||
| 1034 | expr = | |||
| 1035 | target->adjustGotPcExpr(type, addend, sec->content().data() + offset); | |||
| 1036 | } | |||
| 1037 | } | |||
| 1038 | ||||
| 1039 | // We were asked not to generate PLT entries for ifuncs. Instead, pass the | |||
| 1040 | // direct relocation on through. | |||
| 1041 | if (LLVM_UNLIKELY(isIfunc)__builtin_expect((bool)(isIfunc), false) && config->zIfuncNoplt) { | |||
| 1042 | std::lock_guard<std::mutex> lock(relocMutex); | |||
| 1043 | sym.exportDynamic = true; | |||
| 1044 | mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type); | |||
| 1045 | return; | |||
| 1046 | } | |||
| 1047 | ||||
| 1048 | if (needsGot(expr)) { | |||
| 1049 | if (config->emachine == EM_MIPS) { | |||
| 1050 | // MIPS ABI has special rules to process GOT entries and doesn't | |||
| 1051 | // require relocation entries for them. A special case is TLS | |||
| 1052 | // relocations. In that case dynamic loader applies dynamic | |||
| 1053 | // relocations to initialize TLS GOT entries. | |||
| 1054 | // See "Global Offset Table" in Chapter 5 in the following document | |||
| 1055 | // for detailed description: | |||
| 1056 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf | |||
| 1057 | in.mipsGot->addEntry(*sec->file, sym, addend, expr); | |||
| 1058 | } else { | |||
| 1059 | sym.setFlags(NEEDS_GOT); | |||
| 1060 | } | |||
| 1061 | } else if (needsPlt(expr)) { | |||
| 1062 | sym.setFlags(NEEDS_PLT); | |||
| 1063 | } else if (LLVM_UNLIKELY(isIfunc)__builtin_expect((bool)(isIfunc), false)) { | |||
| 1064 | sym.setFlags(HAS_DIRECT_RELOC); | |||
| 1065 | } | |||
| 1066 | ||||
| 1067 | // If the relocation is known to be a link-time constant, we know no dynamic | |||
| 1068 | // relocation will be created, pass the control to relocateAlloc() or | |||
| 1069 | // relocateNonAlloc() to resolve it. | |||
| 1070 | // | |||
| 1071 | // The behavior of an undefined weak reference is implementation defined. For | |||
| 1072 | // non-link-time constants, we resolve relocations statically (let | |||
| 1073 | // relocate{,Non}Alloc() resolve them) for -no-pie and try producing dynamic | |||
| 1074 | // relocations for -pie and -shared. | |||
| 1075 | // | |||
| 1076 | // The general expectation of -no-pie static linking is that there is no | |||
| 1077 | // dynamic relocation (except IRELATIVE). Emitting dynamic relocations for | |||
| 1078 | // -shared matches the spirit of its -z undefs default. -pie has freedom on | |||
| 1079 | // choices, and we choose dynamic relocations to be consistent with the | |||
| 1080 | // handling of GOT-generating relocations. | |||
| 1081 | if (isStaticLinkTimeConstant(expr, type, sym, offset) || | |||
| 1082 | (!config->isPic && sym.isUndefWeak())) { | |||
| 1083 | sec->addReloc({expr, type, offset, addend, &sym}); | |||
| 1084 | return; | |||
| 1085 | } | |||
| 1086 | ||||
| 1087 | // Use a simple -z notext rule that treats all sections except .eh_frame as | |||
| 1088 | // writable. GNU ld does not produce dynamic relocations in .eh_frame (and our | |||
| 1089 | // SectionBase::getOffset would incorrectly adjust the offset). | |||
| 1090 | // | |||
| 1091 | // For MIPS, we don't implement GNU ld's DW_EH_PE_absptr to DW_EH_PE_pcrel | |||
| 1092 | // conversion. We still emit a dynamic relocation. | |||
| 1093 | bool canWrite = (sec->flags & SHF_WRITE) || | |||
| 1094 | !(config->zText || | |||
| 1095 | (isa<EhInputSection>(sec) && config->emachine != EM_MIPS)); | |||
| 1096 | if (canWrite) { | |||
| 1097 | RelType rel = target->getDynRel(type); | |||
| 1098 | if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) { | |||
| 1099 | addRelativeReloc<true>(*sec, offset, sym, addend, expr, type); | |||
| 1100 | return; | |||
| 1101 | } else if (rel != 0) { | |||
| 1102 | if (config->emachine == EM_MIPS && rel == target->symbolicRel) | |||
| 1103 | rel = target->relativeRel; | |||
| 1104 | std::lock_guard<std::mutex> lock(relocMutex); | |||
| 1105 | sec->getPartition().relaDyn->addSymbolReloc(rel, *sec, offset, sym, | |||
| 1106 | addend, type); | |||
| 1107 | ||||
| 1108 | // MIPS ABI turns using of GOT and dynamic relocations inside out. | |||
| 1109 | // While regular ABI uses dynamic relocations to fill up GOT entries | |||
| 1110 | // MIPS ABI requires dynamic linker to fills up GOT entries using | |||
| 1111 | // specially sorted dynamic symbol table. This affects even dynamic | |||
| 1112 | // relocations against symbols which do not require GOT entries | |||
| 1113 | // creation explicitly, i.e. do not have any GOT-relocations. So if | |||
| 1114 | // a preemptible symbol has a dynamic relocation we anyway have | |||
| 1115 | // to create a GOT entry for it. | |||
| 1116 | // If a non-preemptible symbol has a dynamic relocation against it, | |||
| 1117 | // dynamic linker takes it st_value, adds offset and writes down | |||
| 1118 | // result of the dynamic relocation. In case of preemptible symbol | |||
| 1119 | // dynamic linker performs symbol resolution, writes the symbol value | |||
| 1120 | // to the GOT entry and reads the GOT entry when it needs to perform | |||
| 1121 | // a dynamic relocation. | |||
| 1122 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19 | |||
| 1123 | if (config->emachine == EM_MIPS) | |||
| 1124 | in.mipsGot->addEntry(*sec->file, sym, addend, expr); | |||
| 1125 | return; | |||
| 1126 | } | |||
| 1127 | } | |||
| 1128 | ||||
| 1129 | // When producing an executable, we can perform copy relocations (for | |||
| 1130 | // STT_OBJECT) and canonical PLT (for STT_FUNC). | |||
| 1131 | if (!config->shared) { | |||
| 1132 | if (!canDefineSymbolInExecutable(sym)) { | |||
| 1133 | errorOrWarn("cannot preempt symbol: " + toString(sym) + | |||
| 1134 | getLocation(*sec, sym, offset)); | |||
| 1135 | return; | |||
| 1136 | } | |||
| 1137 | ||||
| 1138 | if (sym.isObject()) { | |||
| 1139 | // Produce a copy relocation. | |||
| 1140 | if (auto *ss = dyn_cast<SharedSymbol>(&sym)) { | |||
| 1141 | if (!config->zCopyreloc) | |||
| 1142 | error("unresolvable relocation " + toString(type) + | |||
| 1143 | " against symbol '" + toString(*ss) + | |||
| 1144 | "'; recompile with -fPIC or remove '-z nocopyreloc'" + | |||
| 1145 | getLocation(*sec, sym, offset)); | |||
| 1146 | sym.setFlags(NEEDS_COPY); | |||
| 1147 | } | |||
| 1148 | sec->addReloc({expr, type, offset, addend, &sym}); | |||
| 1149 | return; | |||
| 1150 | } | |||
| 1151 | ||||
| 1152 | // This handles a non PIC program call to function in a shared library. In | |||
| 1153 | // an ideal world, we could just report an error saying the relocation can | |||
| 1154 | // overflow at runtime. In the real world with glibc, crt1.o has a | |||
| 1155 | // R_X86_64_PC32 pointing to libc.so. | |||
| 1156 | // | |||
| 1157 | // The general idea on how to handle such cases is to create a PLT entry and | |||
| 1158 | // use that as the function value. | |||
| 1159 | // | |||
| 1160 | // For the static linking part, we just return a plt expr and everything | |||
| 1161 | // else will use the PLT entry as the address. | |||
| 1162 | // | |||
| 1163 | // The remaining problem is making sure pointer equality still works. We | |||
| 1164 | // need the help of the dynamic linker for that. We let it know that we have | |||
| 1165 | // a direct reference to a so symbol by creating an undefined symbol with a | |||
| 1166 | // non zero st_value. Seeing that, the dynamic linker resolves the symbol to | |||
| 1167 | // the value of the symbol we created. This is true even for got entries, so | |||
| 1168 | // pointer equality is maintained. To avoid an infinite loop, the only entry | |||
| 1169 | // that points to the real function is a dedicated got entry used by the | |||
| 1170 | // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT, | |||
| 1171 | // R_386_JMP_SLOT, etc). | |||
| 1172 | ||||
| 1173 | // For position independent executable on i386, the plt entry requires ebx | |||
| 1174 | // to be set. This causes two problems: | |||
| 1175 | // * If some code has a direct reference to a function, it was probably | |||
| 1176 | // compiled without -fPIE/-fPIC and doesn't maintain ebx. | |||
| 1177 | // * If a library definition gets preempted to the executable, it will have | |||
| 1178 | // the wrong ebx value. | |||
| 1179 | if (sym.isFunc()) { | |||
| 1180 | if (config->pie && config->emachine == EM_386) | |||
| 1181 | errorOrWarn("symbol '" + toString(sym) + | |||
| 1182 | "' cannot be preempted; recompile with -fPIE" + | |||
| 1183 | getLocation(*sec, sym, offset)); | |||
| 1184 | sym.setFlags(NEEDS_COPY | NEEDS_PLT); | |||
| 1185 | sec->addReloc({expr, type, offset, addend, &sym}); | |||
| 1186 | return; | |||
| 1187 | } | |||
| 1188 | } | |||
| 1189 | ||||
| 1190 | errorOrWarn("relocation " + toString(type) + " cannot be used against " + | |||
| 1191 | (sym.getName().empty() ? "local symbol" | |||
| 1192 | : "symbol '" + toString(sym) + "'") + | |||
| 1193 | "; recompile with -fPIC" + getLocation(*sec, sym, offset)); | |||
| 1194 | } | |||
| 1195 | ||||
| 1196 | // This function is similar to the `handleTlsRelocation`. MIPS does not | |||
| 1197 | // support any relaxations for TLS relocations so by factoring out MIPS | |||
| 1198 | // handling in to the separate function we can simplify the code and do not | |||
| 1199 | // pollute other `handleTlsRelocation` by MIPS `ifs` statements. | |||
| 1200 | // Mips has a custom MipsGotSection that handles the writing of GOT entries | |||
| 1201 | // without dynamic relocations. | |||
| 1202 | static unsigned handleMipsTlsRelocation(RelType type, Symbol &sym, | |||
| 1203 | InputSectionBase &c, uint64_t offset, | |||
| 1204 | int64_t addend, RelExpr expr) { | |||
| 1205 | if (expr == R_MIPS_TLSLD) { | |||
| 1206 | in.mipsGot->addTlsIndex(*c.file); | |||
| 1207 | c.addReloc({expr, type, offset, addend, &sym}); | |||
| 1208 | return 1; | |||
| 1209 | } | |||
| 1210 | if (expr == R_MIPS_TLSGD) { | |||
| 1211 | in.mipsGot->addDynTlsEntry(*c.file, sym); | |||
| 1212 | c.addReloc({expr, type, offset, addend, &sym}); | |||
| 1213 | return 1; | |||
| 1214 | } | |||
| 1215 | return 0; | |||
| 1216 | } | |||
| 1217 | ||||
| 1218 | // Notes about General Dynamic and Local Dynamic TLS models below. They may | |||
| 1219 | // require the generation of a pair of GOT entries that have associated dynamic | |||
| 1220 | // relocations. The pair of GOT entries created are of the form GOT[e0] Module | |||
| 1221 | // Index (Used to find pointer to TLS block at run-time) GOT[e1] Offset of | |||
| 1222 | // symbol in TLS block. | |||
| 1223 | // | |||
| 1224 | // Returns the number of relocations processed. | |||
| 1225 | static unsigned handleTlsRelocation(RelType type, Symbol &sym, | |||
| 1226 | InputSectionBase &c, uint64_t offset, | |||
| 1227 | int64_t addend, RelExpr expr) { | |||
| 1228 | if (expr == R_TPREL || expr == R_TPREL_NEG) { | |||
| 1229 | if (config->shared) { | |||
| 1230 | errorOrWarn("relocation " + toString(type) + " against " + toString(sym) + | |||
| 1231 | " cannot be used with -shared" + getLocation(c, sym, offset)); | |||
| 1232 | return 1; | |||
| 1233 | } | |||
| 1234 | return 0; | |||
| 1235 | } | |||
| 1236 | ||||
| 1237 | if (config->emachine == EM_MIPS) | |||
| 1238 | return handleMipsTlsRelocation(type, sym, c, offset, addend, expr); | |||
| 1239 | ||||
| 1240 | if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC, | |||
| 1241 | R_TLSDESC_GOTPLT>(expr) && | |||
| 1242 | config->shared) { | |||
| 1243 | if (expr != R_TLSDESC_CALL) { | |||
| 1244 | sym.setFlags(NEEDS_TLSDESC); | |||
| 1245 | c.addReloc({expr, type, offset, addend, &sym}); | |||
| 1246 | } | |||
| 1247 | return 1; | |||
| 1248 | } | |||
| 1249 | ||||
| 1250 | // ARM, Hexagon and RISC-V do not support GD/LD to IE/LE relaxation. For | |||
| 1251 | // PPC64, if the file has missing R_PPC64_TLSGD/R_PPC64_TLSLD, disable | |||
| 1252 | // relaxation as well. | |||
| 1253 | bool toExecRelax = !config->shared && config->emachine != EM_ARM && | |||
| 1254 | config->emachine != EM_HEXAGON && | |||
| 1255 | config->emachine != EM_RISCV && | |||
| 1256 | !c.file->ppc64DisableTLSRelax; | |||
| 1257 | ||||
| 1258 | // If we are producing an executable and the symbol is non-preemptable, it | |||
| 1259 | // must be defined and the code sequence can be relaxed to use Local-Exec. | |||
| 1260 | // | |||
| 1261 | // ARM and RISC-V do not support any relaxations for TLS relocations, however, | |||
| 1262 | // we can omit the DTPMOD dynamic relocations and resolve them at link time | |||
| 1263 | // because them are always 1. This may be necessary for static linking as | |||
| 1264 | // DTPMOD may not be expected at load time. | |||
| 1265 | bool isLocalInExecutable = !sym.isPreemptible && !config->shared; | |||
| 1266 | ||||
| 1267 | // Local Dynamic is for access to module local TLS variables, while still | |||
| 1268 | // being suitable for being dynamically loaded via dlopen. GOT[e0] is the | |||
| 1269 | // module index, with a special value of 0 for the current module. GOT[e1] is | |||
| 1270 | // unused. There only needs to be one module index entry. | |||
| 1271 | if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>( | |||
| 1272 | expr)) { | |||
| 1273 | // Local-Dynamic relocs can be relaxed to Local-Exec. | |||
| 1274 | if (toExecRelax) { | |||
| 1275 | c.addReloc({target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE), type, | |||
| 1276 | offset, addend, &sym}); | |||
| 1277 | return target->getTlsGdRelaxSkip(type); | |||
| 1278 | } | |||
| 1279 | if (expr == R_TLSLD_HINT) | |||
| 1280 | return 1; | |||
| 1281 | ctx.needsTlsLd.store(true, std::memory_order_relaxed); | |||
| 1282 | c.addReloc({expr, type, offset, addend, &sym}); | |||
| 1283 | return 1; | |||
| 1284 | } | |||
| 1285 | ||||
| 1286 | // Local-Dynamic relocs can be relaxed to Local-Exec. | |||
| 1287 | if (expr == R_DTPREL) { | |||
| 1288 | if (toExecRelax) | |||
| 1289 | expr = target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE); | |||
| 1290 | c.addReloc({expr, type, offset, addend, &sym}); | |||
| 1291 | return 1; | |||
| 1292 | } | |||
| 1293 | ||||
| 1294 | // Local-Dynamic sequence where offset of tls variable relative to dynamic | |||
| 1295 | // thread pointer is stored in the got. This cannot be relaxed to Local-Exec. | |||
| 1296 | if (expr == R_TLSLD_GOT_OFF) { | |||
| 1297 | sym.setFlags(NEEDS_GOT_DTPREL); | |||
| 1298 | c.addReloc({expr, type, offset, addend, &sym}); | |||
| 1299 | return 1; | |||
| 1300 | } | |||
| 1301 | ||||
| 1302 | if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC, | |||
| 1303 | R_TLSDESC_GOTPLT, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) { | |||
| 1304 | if (!toExecRelax) { | |||
| 1305 | sym.setFlags(NEEDS_TLSGD); | |||
| 1306 | c.addReloc({expr, type, offset, addend, &sym}); | |||
| 1307 | return 1; | |||
| 1308 | } | |||
| 1309 | ||||
| 1310 | // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec | |||
| 1311 | // depending on the symbol being locally defined or not. | |||
| 1312 | if (sym.isPreemptible) { | |||
| 1313 | sym.setFlags(NEEDS_TLSGD_TO_IE); | |||
| 1314 | c.addReloc({target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_IE), type, | |||
| 1315 | offset, addend, &sym}); | |||
| 1316 | } else { | |||
| 1317 | c.addReloc({target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_LE), type, | |||
| 1318 | offset, addend, &sym}); | |||
| 1319 | } | |||
| 1320 | return target->getTlsGdRelaxSkip(type); | |||
| 1321 | } | |||
| 1322 | ||||
| 1323 | if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF, | |||
| 1324 | R_TLSIE_HINT>(expr)) { | |||
| 1325 | ctx.hasTlsIe.store(true, std::memory_order_relaxed); | |||
| 1326 | // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally | |||
| 1327 | // defined. | |||
| 1328 | if (toExecRelax && isLocalInExecutable) { | |||
| 1329 | c.addReloc({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym}); | |||
| 1330 | } else if (expr != R_TLSIE_HINT) { | |||
| 1331 | sym.setFlags(NEEDS_TLSIE); | |||
| 1332 | // R_GOT needs a relative relocation for PIC on i386 and Hexagon. | |||
| 1333 | if (expr == R_GOT && config->isPic && !target->usesOnlyLowPageBits(type)) | |||
| 1334 | addRelativeReloc<true>(c, offset, sym, addend, expr, type); | |||
| 1335 | else | |||
| 1336 | c.addReloc({expr, type, offset, addend, &sym}); | |||
| 1337 | } | |||
| 1338 | return 1; | |||
| 1339 | } | |||
| 1340 | ||||
| 1341 | return 0; | |||
| 1342 | } | |||
| 1343 | ||||
| 1344 | template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) { | |||
| 1345 | const RelTy &rel = *i; | |||
| 1346 | uint32_t symIndex = rel.getSymbol(config->isMips64EL); | |||
| 1347 | Symbol &sym = sec->getFile<ELFT>()->getSymbol(symIndex); | |||
| 1348 | RelType type; | |||
| 1349 | if (config->mipsN32Abi) { | |||
| 1350 | type = getMipsN32RelType(i); | |||
| 1351 | } else { | |||
| 1352 | type = rel.getType(config->isMips64EL); | |||
| 1353 | ++i; | |||
| 1354 | } | |||
| 1355 | // Get an offset in an output section this relocation is applied to. | |||
| 1356 | uint64_t offset = getter.get(rel.r_offset); | |||
| 1357 | if (offset == uint64_t(-1)) | |||
| 1358 | return; | |||
| 1359 | ||||
| 1360 | RelExpr expr = target->getRelExpr(type, sym, sec->content().data() + offset); | |||
| 1361 | int64_t addend = RelTy::IsRela | |||
| 1362 | ? getAddend<ELFT>(rel) | |||
| 1363 | : target->getImplicitAddend( | |||
| 1364 | sec->content().data() + rel.r_offset, type); | |||
| 1365 | if (LLVM_UNLIKELY(config->emachine == EM_MIPS)__builtin_expect((bool)(config->emachine == EM_MIPS), false )) | |||
| 1366 | addend += computeMipsAddend<ELFT>(rel, expr, sym.isLocal()); | |||
| 1367 | else if (config->emachine == EM_PPC64 && config->isPic && type == R_PPC64_TOC) | |||
| 1368 | addend += getPPC64TocBase(); | |||
| 1369 | ||||
| 1370 | // Ignore R_*_NONE and other marker relocations. | |||
| 1371 | if (expr == R_NONE) | |||
| 1372 | return; | |||
| 1373 | ||||
| 1374 | // Error if the target symbol is undefined. Symbol index 0 may be used by | |||
| 1375 | // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them. | |||
| 1376 | if (sym.isUndefined() && symIndex != 0 && | |||
| 1377 | maybeReportUndefined(cast<Undefined>(sym), *sec, offset)) | |||
| 1378 | return; | |||
| 1379 | ||||
| 1380 | if (config->emachine == EM_PPC64) { | |||
| 1381 | // We can separate the small code model relocations into 2 categories: | |||
| 1382 | // 1) Those that access the compiler generated .toc sections. | |||
| 1383 | // 2) Those that access the linker allocated got entries. | |||
| 1384 | // lld allocates got entries to symbols on demand. Since we don't try to | |||
| 1385 | // sort the got entries in any way, we don't have to track which objects | |||
| 1386 | // have got-based small code model relocs. The .toc sections get placed | |||
| 1387 | // after the end of the linker allocated .got section and we do sort those | |||
| 1388 | // so sections addressed with small code model relocations come first. | |||
| 1389 | if (type == R_PPC64_TOC16 || type == R_PPC64_TOC16_DS) | |||
| 1390 | sec->file->ppc64SmallCodeModelTocRelocs = true; | |||
| 1391 | ||||
| 1392 | // Record the TOC entry (.toc + addend) as not relaxable. See the comment in | |||
| 1393 | // InputSectionBase::relocateAlloc(). | |||
| 1394 | if (type == R_PPC64_TOC16_LO && sym.isSection() && isa<Defined>(sym) && | |||
| 1395 | cast<Defined>(sym).section->name == ".toc") | |||
| 1396 | ppc64noTocRelax.insert({&sym, addend}); | |||
| 1397 | ||||
| 1398 | if ((type == R_PPC64_TLSGD && expr == R_TLSDESC_CALL) || | |||
| 1399 | (type == R_PPC64_TLSLD && expr == R_TLSLD_HINT)) { | |||
| 1400 | if (i == end) { | |||
| 1401 | errorOrWarn("R_PPC64_TLSGD/R_PPC64_TLSLD may not be the last " | |||
| 1402 | "relocation" + | |||
| 1403 | getLocation(*sec, sym, offset)); | |||
| 1404 | return; | |||
| 1405 | } | |||
| 1406 | ||||
| 1407 | // Offset the 4-byte aligned R_PPC64_TLSGD by one byte in the NOTOC case, | |||
| 1408 | // so we can discern it later from the toc-case. | |||
| 1409 | if (i->getType(/*isMips64EL=*/false) == R_PPC64_REL24_NOTOC) | |||
| 1410 | ++offset; | |||
| 1411 | } | |||
| 1412 | } | |||
| 1413 | ||||
| 1414 | // If the relocation does not emit a GOT or GOTPLT entry but its computation | |||
| 1415 | // uses their addresses, we need GOT or GOTPLT to be created. | |||
| 1416 | // | |||
| 1417 | // The 5 types that relative GOTPLT are all x86 and x86-64 specific. | |||
| 1418 | if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_PLT_GOTPLT, | |||
| 1419 | R_TLSDESC_GOTPLT, R_TLSGD_GOTPLT>(expr)) { | |||
| 1420 | in.gotPlt->hasGotPltOffRel.store(true, std::memory_order_relaxed); | |||
| 1421 | } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC32_PLTREL, R_PPC64_TOCBASE, | |||
| 1422 | R_PPC64_RELAX_TOC>(expr)) { | |||
| 1423 | in.got->hasGotOffRel.store(true, std::memory_order_relaxed); | |||
| 1424 | } | |||
| 1425 | ||||
| 1426 | // Process TLS relocations, including relaxing TLS relocations. Note that | |||
| 1427 | // R_TPREL and R_TPREL_NEG relocations are resolved in processAux. | |||
| 1428 | if (sym.isTls()) { | |||
| 1429 | if (unsigned processed = | |||
| 1430 | handleTlsRelocation(type, sym, *sec, offset, addend, expr)) { | |||
| 1431 | i += processed - 1; | |||
| 1432 | return; | |||
| 1433 | } | |||
| 1434 | } | |||
| 1435 | ||||
| 1436 | processAux(expr, type, offset, sym, addend); | |||
| 1437 | } | |||
| 1438 | ||||
| 1439 | // R_PPC64_TLSGD/R_PPC64_TLSLD is required to mark `bl __tls_get_addr` for | |||
| 1440 | // General Dynamic/Local Dynamic code sequences. If a GD/LD GOT relocation is | |||
| 1441 | // found but no R_PPC64_TLSGD/R_PPC64_TLSLD is seen, we assume that the | |||
| 1442 | // instructions are generated by very old IBM XL compilers. Work around the | |||
| 1443 | // issue by disabling GD/LD to IE/LE relaxation. | |||
| 1444 | template <class RelTy> | |||
| 1445 | static void checkPPC64TLSRelax(InputSectionBase &sec, ArrayRef<RelTy> rels) { | |||
| 1446 | // Skip if sec is synthetic (sec.file is null) or if sec has been marked. | |||
| 1447 | if (!sec.file || sec.file->ppc64DisableTLSRelax) | |||
| 1448 | return; | |||
| 1449 | bool hasGDLD = false; | |||
| 1450 | for (const RelTy &rel : rels) { | |||
| 1451 | RelType type = rel.getType(false); | |||
| 1452 | switch (type) { | |||
| 1453 | case R_PPC64_TLSGD: | |||
| 1454 | case R_PPC64_TLSLD: | |||
| 1455 | return; // Found a marker | |||
| 1456 | case R_PPC64_GOT_TLSGD16: | |||
| 1457 | case R_PPC64_GOT_TLSGD16_HA: | |||
| 1458 | case R_PPC64_GOT_TLSGD16_HI: | |||
| 1459 | case R_PPC64_GOT_TLSGD16_LO: | |||
| 1460 | case R_PPC64_GOT_TLSLD16: | |||
| 1461 | case R_PPC64_GOT_TLSLD16_HA: | |||
| 1462 | case R_PPC64_GOT_TLSLD16_HI: | |||
| 1463 | case R_PPC64_GOT_TLSLD16_LO: | |||
| 1464 | hasGDLD = true; | |||
| 1465 | break; | |||
| 1466 | } | |||
| 1467 | } | |||
| 1468 | if (hasGDLD) { | |||
| 1469 | sec.file->ppc64DisableTLSRelax = true; | |||
| 1470 | warn(toString(sec.file) + | |||
| 1471 | ": disable TLS relaxation due to R_PPC64_GOT_TLS* relocations without " | |||
| 1472 | "R_PPC64_TLSGD/R_PPC64_TLSLD relocations"); | |||
| 1473 | } | |||
| 1474 | } | |||
| 1475 | ||||
| 1476 | template <class ELFT, class RelTy> | |||
| 1477 | void RelocationScanner::scan(ArrayRef<RelTy> rels) { | |||
| 1478 | // Not all relocations end up in Sec->Relocations, but a lot do. | |||
| 1479 | sec->relocations.reserve(rels.size()); | |||
| 1480 | ||||
| 1481 | if (config->emachine == EM_PPC64) | |||
| 1482 | checkPPC64TLSRelax<RelTy>(*sec, rels); | |||
| 1483 | ||||
| 1484 | // For EhInputSection, OffsetGetter expects the relocations to be sorted by | |||
| 1485 | // r_offset. In rare cases (.eh_frame pieces are reordered by a linker | |||
| 1486 | // script), the relocations may be unordered. | |||
| 1487 | SmallVector<RelTy, 0> storage; | |||
| 1488 | if (isa<EhInputSection>(sec)) | |||
| 1489 | rels = sortRels(rels, storage); | |||
| 1490 | ||||
| 1491 | end = static_cast<const void *>(rels.end()); | |||
| 1492 | for (auto i = rels.begin(); i != end;) | |||
| 1493 | scanOne<ELFT>(i); | |||
| 1494 | ||||
| 1495 | // Sort relocations by offset for more efficient searching for | |||
| 1496 | // R_RISCV_PCREL_HI20 and R_PPC64_ADDR64. | |||
| 1497 | if (config->emachine == EM_RISCV || | |||
| 1498 | (config->emachine == EM_PPC64 && sec->name == ".toc")) | |||
| 1499 | llvm::stable_sort(sec->relocs(), | |||
| 1500 | [](const Relocation &lhs, const Relocation &rhs) { | |||
| 1501 | return lhs.offset < rhs.offset; | |||
| 1502 | }); | |||
| 1503 | } | |||
| 1504 | ||||
| 1505 | template <class ELFT> void RelocationScanner::scanSection(InputSectionBase &s) { | |||
| 1506 | sec = &s; | |||
| 1507 | getter = OffsetGetter(s); | |||
| 1508 | const RelsOrRelas<ELFT> rels = s.template relsOrRelas<ELFT>(); | |||
| 1509 | if (rels.areRelocsRel()) | |||
| 1510 | scan<ELFT>(rels.rels); | |||
| 1511 | else | |||
| 1512 | scan<ELFT>(rels.relas); | |||
| 1513 | } | |||
| 1514 | ||||
| 1515 | template <class ELFT> void elf::scanRelocations() { | |||
| 1516 | // Scan all relocations. Each relocation goes through a series of tests to | |||
| 1517 | // determine if it needs special treatment, such as creating GOT, PLT, | |||
| 1518 | // copy relocations, etc. Note that relocations for non-alloc sections are | |||
| 1519 | // directly processed by InputSection::relocateNonAlloc. | |||
| 1520 | ||||
| 1521 | // Deterministic parallellism needs sorting relocations which is unsuitable | |||
| 1522 | // for -z nocombreloc. MIPS and PPC64 use global states which are not suitable | |||
| 1523 | // for parallelism. | |||
| 1524 | bool serial = !config->zCombreloc || config->emachine == EM_MIPS || | |||
| 1525 | config->emachine == EM_PPC64; | |||
| 1526 | parallel::TaskGroup tg; | |||
| 1527 | for (ELFFileBase *f : ctx.objectFiles) { | |||
| 1528 | auto fn = [f]() { | |||
| 1529 | RelocationScanner scanner; | |||
| 1530 | for (InputSectionBase *s : f->getSections()) { | |||
| 1531 | if (s && s->kind() == SectionBase::Regular && s->isLive() && | |||
| 1532 | (s->flags & SHF_ALLOC) && | |||
| 1533 | !(s->type == SHT_ARM_EXIDX && config->emachine == EM_ARM)) | |||
| 1534 | scanner.template scanSection<ELFT>(*s); | |||
| 1535 | } | |||
| 1536 | }; | |||
| 1537 | tg.spawn(fn, serial); | |||
| 1538 | } | |||
| 1539 | ||||
| 1540 | tg.spawn([] { | |||
| 1541 | RelocationScanner scanner; | |||
| 1542 | for (Partition &part : partitions) { | |||
| 1543 | for (EhInputSection *sec : part.ehFrame->sections) | |||
| 1544 | scanner.template scanSection<ELFT>(*sec); | |||
| 1545 | if (part.armExidx && part.armExidx->isLive()) | |||
| 1546 | for (InputSection *sec : part.armExidx->exidxSections) | |||
| 1547 | scanner.template scanSection<ELFT>(*sec); | |||
| 1548 | } | |||
| 1549 | }); | |||
| 1550 | } | |||
| 1551 | ||||
| 1552 | static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) { | |||
| 1553 | // Handle a reference to a non-preemptible ifunc. These are special in a | |||
| 1554 | // few ways: | |||
| 1555 | // | |||
| 1556 | // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have | |||
| 1557 | // a fixed value. But assuming that all references to the ifunc are | |||
| 1558 | // GOT-generating or PLT-generating, the handling of an ifunc is | |||
| 1559 | // relatively straightforward. We create a PLT entry in Iplt, which is | |||
| 1560 | // usually at the end of .plt, which makes an indirect call using a | |||
| 1561 | // matching GOT entry in igotPlt, which is usually at the end of .got.plt. | |||
| 1562 | // The GOT entry is relocated using an IRELATIVE relocation in relaIplt, | |||
| 1563 | // which is usually at the end of .rela.plt. Unlike most relocations in | |||
| 1564 | // .rela.plt, which may be evaluated lazily without -z now, dynamic | |||
| 1565 | // loaders evaluate IRELATIVE relocs eagerly, which means that for | |||
| 1566 | // IRELATIVE relocs only, GOT-generating relocations can point directly to | |||
| 1567 | // .got.plt without requiring a separate GOT entry. | |||
| 1568 | // | |||
| 1569 | // - Despite the fact that an ifunc does not have a fixed value, compilers | |||
| 1570 | // that are not passed -fPIC will assume that they do, and will emit | |||
| 1571 | // direct (non-GOT-generating, non-PLT-generating) relocations to the | |||
| 1572 | // symbol. This means that if a direct relocation to the symbol is | |||
| 1573 | // seen, the linker must set a value for the symbol, and this value must | |||
| 1574 | // be consistent no matter what type of reference is made to the symbol. | |||
| 1575 | // This can be done by creating a PLT entry for the symbol in the way | |||
| 1576 | // described above and making it canonical, that is, making all references | |||
| 1577 | // point to the PLT entry instead of the resolver. In lld we also store | |||
| 1578 | // the address of the PLT entry in the dynamic symbol table, which means | |||
| 1579 | // that the symbol will also have the same value in other modules. | |||
| 1580 | // Because the value loaded from the GOT needs to be consistent with | |||
| 1581 | // the value computed using a direct relocation, a non-preemptible ifunc | |||
| 1582 | // may end up with two GOT entries, one in .got.plt that points to the | |||
| 1583 | // address returned by the resolver and is used only by the PLT entry, | |||
| 1584 | // and another in .got that points to the PLT entry and is used by | |||
| 1585 | // GOT-generating relocations. | |||
| 1586 | // | |||
| 1587 | // - The fact that these symbols do not have a fixed value makes them an | |||
| 1588 | // exception to the general rule that a statically linked executable does | |||
| 1589 | // not require any form of dynamic relocation. To handle these relocations | |||
| 1590 | // correctly, the IRELATIVE relocations are stored in an array which a | |||
| 1591 | // statically linked executable's startup code must enumerate using the | |||
| 1592 | // linker-defined symbols __rela?_iplt_{start,end}. | |||
| 1593 | if (!sym.isGnuIFunc() || sym.isPreemptible || config->zIfuncNoplt) | |||
| 1594 | return false; | |||
| 1595 | // Skip unreferenced non-preemptible ifunc. | |||
| 1596 | if (!(flags & (NEEDS_GOT | NEEDS_PLT | HAS_DIRECT_RELOC))) | |||
| 1597 | return true; | |||
| 1598 | ||||
| 1599 | sym.isInIplt = true; | |||
| 1600 | ||||
| 1601 | // Create an Iplt and the associated IRELATIVE relocation pointing to the | |||
| 1602 | // original section/value pairs. For non-GOT non-PLT relocation case below, we | |||
| 1603 | // may alter section/value, so create a copy of the symbol to make | |||
| 1604 | // section/value fixed. | |||
| 1605 | auto *directSym = makeDefined(cast<Defined>(sym)); | |||
| 1606 | directSym->allocateAux(); | |||
| 1607 | addPltEntry(*in.iplt, *in.igotPlt, *in.relaIplt, target->iRelativeRel, | |||
| 1608 | *directSym); | |||
| 1609 | sym.allocateAux(); | |||
| 1610 | symAux.back().pltIdx = symAux[directSym->auxIdx].pltIdx; | |||
| 1611 | ||||
| 1612 | if (flags & HAS_DIRECT_RELOC) { | |||
| 1613 | // Change the value to the IPLT and redirect all references to it. | |||
| 1614 | auto &d = cast<Defined>(sym); | |||
| 1615 | d.section = in.iplt.get(); | |||
| 1616 | d.value = d.getPltIdx() * target->ipltEntrySize; | |||
| 1617 | d.size = 0; | |||
| 1618 | // It's important to set the symbol type here so that dynamic loaders | |||
| 1619 | // don't try to call the PLT as if it were an ifunc resolver. | |||
| 1620 | d.type = STT_FUNC; | |||
| 1621 | ||||
| 1622 | if (flags & NEEDS_GOT) | |||
| 1623 | addGotEntry(sym); | |||
| 1624 | } else if (flags & NEEDS_GOT) { | |||
| 1625 | // Redirect GOT accesses to point to the Igot. | |||
| 1626 | sym.gotInIgot = true; | |||
| 1627 | } | |||
| 1628 | return true; | |||
| 1629 | } | |||
| 1630 | ||||
| 1631 | void elf::postScanRelocations() { | |||
| 1632 | auto fn = [](Symbol &sym) { | |||
| 1633 | auto flags = sym.flags.load(std::memory_order_relaxed); | |||
| 1634 | if (handleNonPreemptibleIfunc(sym, flags)) | |||
| 1635 | return; | |||
| 1636 | if (!sym.needsDynReloc()) | |||
| 1637 | return; | |||
| 1638 | sym.allocateAux(); | |||
| 1639 | ||||
| 1640 | if (flags & NEEDS_GOT) | |||
| 1641 | addGotEntry(sym); | |||
| 1642 | if (flags & NEEDS_PLT) | |||
| 1643 | addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, sym); | |||
| 1644 | if (flags & NEEDS_COPY) { | |||
| 1645 | if (sym.isObject()) { | |||
| 1646 | invokeELFT(addCopyRelSymbol, cast<SharedSymbol>(sym))switch (config->ekind) { case ELF32LEKind: addCopyRelSymbol <ELF32LE>(cast<SharedSymbol>(sym)); break; case ELF32BEKind : addCopyRelSymbol<ELF32BE>(cast<SharedSymbol>(sym )); break; case ELF64LEKind: addCopyRelSymbol<ELF64LE>( cast<SharedSymbol>(sym)); break; case ELF64BEKind: addCopyRelSymbol <ELF64BE>(cast<SharedSymbol>(sym)); break; default : ::llvm::llvm_unreachable_internal("unknown config->ekind" , "lld/ELF/Relocations.cpp", 1646); }; | |||
| 1647 | // NEEDS_COPY is cleared for sym and its aliases so that in | |||
| 1648 | // later iterations aliases won't cause redundant copies. | |||
| 1649 | assert(!sym.hasFlag(NEEDS_COPY))(static_cast <bool> (!sym.hasFlag(NEEDS_COPY)) ? void ( 0) : __assert_fail ("!sym.hasFlag(NEEDS_COPY)", "lld/ELF/Relocations.cpp" , 1649, __extension__ __PRETTY_FUNCTION__)); | |||
| 1650 | } else { | |||
| 1651 | assert(sym.isFunc() && sym.hasFlag(NEEDS_PLT))(static_cast <bool> (sym.isFunc() && sym.hasFlag (NEEDS_PLT)) ? void (0) : __assert_fail ("sym.isFunc() && sym.hasFlag(NEEDS_PLT)" , "lld/ELF/Relocations.cpp", 1651, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1652 | if (!sym.isDefined()) { | |||
| 1653 | replaceWithDefined(sym, *in.plt, | |||
| 1654 | target->pltHeaderSize + | |||
| 1655 | target->pltEntrySize * sym.getPltIdx(), | |||
| 1656 | 0); | |||
| 1657 | sym.setFlags(NEEDS_COPY); | |||
| 1658 | if (config->emachine == EM_PPC) { | |||
| 1659 | // PPC32 canonical PLT entries are at the beginning of .glink | |||
| 1660 | cast<Defined>(sym).value = in.plt->headerSize; | |||
| 1661 | in.plt->headerSize += 16; | |||
| 1662 | cast<PPC32GlinkSection>(*in.plt).canonical_plts.push_back(&sym); | |||
| 1663 | } | |||
| 1664 | } | |||
| 1665 | } | |||
| 1666 | } | |||
| 1667 | ||||
| 1668 | if (!sym.isTls()) | |||
| 1669 | return; | |||
| 1670 | bool isLocalInExecutable = !sym.isPreemptible && !config->shared; | |||
| 1671 | GotSection *got = in.got.get(); | |||
| 1672 | ||||
| 1673 | if (flags & NEEDS_TLSDESC) { | |||
| 1674 | got->addTlsDescEntry(sym); | |||
| 1675 | mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( | |||
| 1676 | target->tlsDescRel, *got, got->getTlsDescOffset(sym), sym, | |||
| 1677 | target->tlsDescRel); | |||
| 1678 | } | |||
| 1679 | if (flags & NEEDS_TLSGD) { | |||
| 1680 | got->addDynTlsEntry(sym); | |||
| 1681 | uint64_t off = got->getGlobalDynOffset(sym); | |||
| 1682 | if (isLocalInExecutable) | |||
| 1683 | // Write one to the GOT slot. | |||
| 1684 | got->addConstant({R_ADDEND, target->symbolicRel, off, 1, &sym}); | |||
| 1685 | else | |||
| 1686 | mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *got, off, | |||
| 1687 | sym); | |||
| 1688 | ||||
| 1689 | // If the symbol is preemptible we need the dynamic linker to write | |||
| 1690 | // the offset too. | |||
| 1691 | uint64_t offsetOff = off + config->wordsize; | |||
| 1692 | if (sym.isPreemptible) | |||
| 1693 | mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *got, offsetOff, | |||
| 1694 | sym); | |||
| 1695 | else | |||
| 1696 | got->addConstant({R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym}); | |||
| 1697 | } | |||
| 1698 | if (flags & NEEDS_TLSGD_TO_IE) { | |||
| 1699 | got->addEntry(sym); | |||
| 1700 | mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *got, | |||
| 1701 | sym.getGotOffset(), sym); | |||
| 1702 | } | |||
| 1703 | if (flags & NEEDS_GOT_DTPREL) { | |||
| 1704 | got->addEntry(sym); | |||
| 1705 | got->addConstant( | |||
| 1706 | {R_ABS, target->tlsOffsetRel, sym.getGotOffset(), 0, &sym}); | |||
| 1707 | } | |||
| 1708 | ||||
| 1709 | if ((flags & NEEDS_TLSIE) && !(flags & NEEDS_TLSGD_TO_IE)) | |||
| 1710 | addTpOffsetGotEntry(sym); | |||
| 1711 | }; | |||
| 1712 | ||||
| 1713 | GotSection *got = in.got.get(); | |||
| 1714 | if (ctx.needsTlsLd.load(std::memory_order_relaxed) && got->addTlsIndex()) { | |||
| 1715 | static Undefined dummy(nullptr, "", STB_LOCAL, 0, 0); | |||
| 1716 | if (config->shared) | |||
| 1717 | mainPart->relaDyn->addReloc( | |||
| 1718 | {target->tlsModuleIndexRel, got, got->getTlsIndexOff()}); | |||
| 1719 | else | |||
| 1720 | got->addConstant( | |||
| 1721 | {R_ADDEND, target->symbolicRel, got->getTlsIndexOff(), 1, &dummy}); | |||
| 1722 | } | |||
| 1723 | ||||
| 1724 | assert(symAux.size() == 1)(static_cast <bool> (symAux.size() == 1) ? void (0) : __assert_fail ("symAux.size() == 1", "lld/ELF/Relocations.cpp", 1724, __extension__ __PRETTY_FUNCTION__)); | |||
| 1725 | for (Symbol *sym : symtab.getSymbols()) | |||
| 1726 | fn(*sym); | |||
| 1727 | ||||
| 1728 | // Local symbols may need the aforementioned non-preemptible ifunc and GOT | |||
| 1729 | // handling. They don't need regular PLT. | |||
| 1730 | for (ELFFileBase *file : ctx.objectFiles) | |||
| 1731 | for (Symbol *sym : file->getLocalSymbols()) | |||
| 1732 | fn(*sym); | |||
| 1733 | } | |||
| 1734 | ||||
| 1735 | static bool mergeCmp(const InputSection *a, const InputSection *b) { | |||
| 1736 | // std::merge requires a strict weak ordering. | |||
| 1737 | if (a->outSecOff < b->outSecOff) | |||
| 1738 | return true; | |||
| 1739 | ||||
| 1740 | // FIXME dyn_cast<ThunkSection> is non-null for any SyntheticSection. | |||
| 1741 | if (a->outSecOff == b->outSecOff && a != b) { | |||
| 1742 | auto *ta = dyn_cast<ThunkSection>(a); | |||
| 1743 | auto *tb = dyn_cast<ThunkSection>(b); | |||
| 1744 | ||||
| 1745 | // Check if Thunk is immediately before any specific Target | |||
| 1746 | // InputSection for example Mips LA25 Thunks. | |||
| 1747 | if (ta && ta->getTargetInputSection() == b) | |||
| 1748 | return true; | |||
| 1749 | ||||
| 1750 | // Place Thunk Sections without specific targets before | |||
| 1751 | // non-Thunk Sections. | |||
| 1752 | if (ta && !tb && !ta->getTargetInputSection()) | |||
| 1753 | return true; | |||
| 1754 | } | |||
| 1755 | ||||
| 1756 | return false; | |||
| 1757 | } | |||
| 1758 | ||||
| 1759 | // Call Fn on every executable InputSection accessed via the linker script | |||
| 1760 | // InputSectionDescription::Sections. | |||
| 1761 | static void forEachInputSectionDescription( | |||
| 1762 | ArrayRef<OutputSection *> outputSections, | |||
| 1763 | llvm::function_ref<void(OutputSection *, InputSectionDescription *)> fn) { | |||
| 1764 | for (OutputSection *os : outputSections) { | |||
| 1765 | if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR)) | |||
| 1766 | continue; | |||
| 1767 | for (SectionCommand *bc : os->commands) | |||
| 1768 | if (auto *isd = dyn_cast<InputSectionDescription>(bc)) | |||
| 1769 | fn(os, isd); | |||
| 1770 | } | |||
| 1771 | } | |||
| 1772 | ||||
| 1773 | // Thunk Implementation | |||
| 1774 | // | |||
| 1775 | // Thunks (sometimes called stubs, veneers or branch islands) are small pieces | |||
| 1776 | // of code that the linker inserts inbetween a caller and a callee. The thunks | |||
| 1777 | // are added at link time rather than compile time as the decision on whether | |||
| 1778 | // a thunk is needed, such as the caller and callee being out of range, can only | |||
| 1779 | // be made at link time. | |||
| 1780 | // | |||
| 1781 | // It is straightforward to tell given the current state of the program when a | |||
| 1782 | // thunk is needed for a particular call. The more difficult part is that | |||
| 1783 | // the thunk needs to be placed in the program such that the caller can reach | |||
| 1784 | // the thunk and the thunk can reach the callee; furthermore, adding thunks to | |||
| 1785 | // the program alters addresses, which can mean more thunks etc. | |||
| 1786 | // | |||
| 1787 | // In lld we have a synthetic ThunkSection that can hold many Thunks. | |||
| 1788 | // The decision to have a ThunkSection act as a container means that we can | |||
| 1789 | // more easily handle the most common case of a single block of contiguous | |||
| 1790 | // Thunks by inserting just a single ThunkSection. | |||
| 1791 | // | |||
| 1792 | // The implementation of Thunks in lld is split across these areas | |||
| 1793 | // Relocations.cpp : Framework for creating and placing thunks | |||
| 1794 | // Thunks.cpp : The code generated for each supported thunk | |||
| 1795 | // Target.cpp : Target specific hooks that the framework uses to decide when | |||
| 1796 | // a thunk is used | |||
| 1797 | // Synthetic.cpp : Implementation of ThunkSection | |||
| 1798 | // Writer.cpp : Iteratively call framework until no more Thunks added | |||
| 1799 | // | |||
| 1800 | // Thunk placement requirements: | |||
| 1801 | // Mips LA25 thunks. These must be placed immediately before the callee section | |||
| 1802 | // We can assume that the caller is in range of the Thunk. These are modelled | |||
| 1803 | // by Thunks that return the section they must precede with | |||
| 1804 | // getTargetInputSection(). | |||
| 1805 | // | |||
| 1806 | // ARM interworking and range extension thunks. These thunks must be placed | |||
| 1807 | // within range of the caller. All implemented ARM thunks can always reach the | |||
| 1808 | // callee as they use an indirect jump via a register that has no range | |||
| 1809 | // restrictions. | |||
| 1810 | // | |||
| 1811 | // Thunk placement algorithm: | |||
| 1812 | // For Mips LA25 ThunkSections; the placement is explicit, it has to be before | |||
| 1813 | // getTargetInputSection(). | |||
| 1814 | // | |||
| 1815 | // For thunks that must be placed within range of the caller there are many | |||
| 1816 | // possible choices given that the maximum range from the caller is usually | |||
| 1817 | // much larger than the average InputSection size. Desirable properties include: | |||
| 1818 | // - Maximize reuse of thunks by multiple callers | |||
| 1819 | // - Minimize number of ThunkSections to simplify insertion | |||
| 1820 | // - Handle impact of already added Thunks on addresses | |||
| 1821 | // - Simple to understand and implement | |||
| 1822 | // | |||
| 1823 | // In lld for the first pass, we pre-create one or more ThunkSections per | |||
| 1824 | // InputSectionDescription at Target specific intervals. A ThunkSection is | |||
| 1825 | // placed so that the estimated end of the ThunkSection is within range of the | |||
| 1826 | // start of the InputSectionDescription or the previous ThunkSection. For | |||
| 1827 | // example: | |||
| 1828 | // InputSectionDescription | |||
| 1829 | // Section 0 | |||
| 1830 | // ... | |||
| 1831 | // Section N | |||
| 1832 | // ThunkSection 0 | |||
| 1833 | // Section N + 1 | |||
| 1834 | // ... | |||
| 1835 | // Section N + K | |||
| 1836 | // Thunk Section 1 | |||
| 1837 | // | |||
| 1838 | // The intention is that we can add a Thunk to a ThunkSection that is well | |||
| 1839 | // spaced enough to service a number of callers without having to do a lot | |||
| 1840 | // of work. An important principle is that it is not an error if a Thunk cannot | |||
| 1841 | // be placed in a pre-created ThunkSection; when this happens we create a new | |||
| 1842 | // ThunkSection placed next to the caller. This allows us to handle the vast | |||
| 1843 | // majority of thunks simply, but also handle rare cases where the branch range | |||
| 1844 | // is smaller than the target specific spacing. | |||
| 1845 | // | |||
| 1846 | // The algorithm is expected to create all the thunks that are needed in a | |||
| 1847 | // single pass, with a small number of programs needing a second pass due to | |||
| 1848 | // the insertion of thunks in the first pass increasing the offset between | |||
| 1849 | // callers and callees that were only just in range. | |||
| 1850 | // | |||
| 1851 | // A consequence of allowing new ThunkSections to be created outside of the | |||
| 1852 | // pre-created ThunkSections is that in rare cases calls to Thunks that were in | |||
| 1853 | // range in pass K, are out of range in some pass > K due to the insertion of | |||
| 1854 | // more Thunks in between the caller and callee. When this happens we retarget | |||
| 1855 | // the relocation back to the original target and create another Thunk. | |||
| 1856 | ||||
| 1857 | // Remove ThunkSections that are empty, this should only be the initial set | |||
| 1858 | // precreated on pass 0. | |||
| 1859 | ||||
| 1860 | // Insert the Thunks for OutputSection OS into their designated place | |||
| 1861 | // in the Sections vector, and recalculate the InputSection output section | |||
| 1862 | // offsets. | |||
| 1863 | // This may invalidate any output section offsets stored outside of InputSection | |||
| 1864 | void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) { | |||
| 1865 | forEachInputSectionDescription( | |||
| 1866 | outputSections, [&](OutputSection *os, InputSectionDescription *isd) { | |||
| 1867 | if (isd->thunkSections.empty()) | |||
| 1868 | return; | |||
| 1869 | ||||
| 1870 | // Remove any zero sized precreated Thunks. | |||
| 1871 | llvm::erase_if(isd->thunkSections, | |||
| 1872 | [](const std::pair<ThunkSection *, uint32_t> &ts) { | |||
| 1873 | return ts.first->getSize() == 0; | |||
| 1874 | }); | |||
| 1875 | ||||
| 1876 | // ISD->ThunkSections contains all created ThunkSections, including | |||
| 1877 | // those inserted in previous passes. Extract the Thunks created this | |||
| 1878 | // pass and order them in ascending outSecOff. | |||
| 1879 | std::vector<ThunkSection *> newThunks; | |||
| 1880 | for (std::pair<ThunkSection *, uint32_t> ts : isd->thunkSections) | |||
| 1881 | if (ts.second == pass) | |||
| 1882 | newThunks.push_back(ts.first); | |||
| 1883 | llvm::stable_sort(newThunks, | |||
| 1884 | [](const ThunkSection *a, const ThunkSection *b) { | |||
| 1885 | return a->outSecOff < b->outSecOff; | |||
| 1886 | }); | |||
| 1887 | ||||
| 1888 | // Merge sorted vectors of Thunks and InputSections by outSecOff | |||
| 1889 | SmallVector<InputSection *, 0> tmp; | |||
| 1890 | tmp.reserve(isd->sections.size() + newThunks.size()); | |||
| 1891 | ||||
| 1892 | std::merge(isd->sections.begin(), isd->sections.end(), | |||
| 1893 | newThunks.begin(), newThunks.end(), std::back_inserter(tmp), | |||
| 1894 | mergeCmp); | |||
| 1895 | ||||
| 1896 | isd->sections = std::move(tmp); | |||
| 1897 | }); | |||
| 1898 | } | |||
| 1899 | ||||
| 1900 | static int64_t getPCBias(RelType type) { | |||
| 1901 | if (config->emachine != EM_ARM) | |||
| 1902 | return 0; | |||
| 1903 | switch (type) { | |||
| 1904 | case R_ARM_THM_JUMP19: | |||
| 1905 | case R_ARM_THM_JUMP24: | |||
| 1906 | case R_ARM_THM_CALL: | |||
| 1907 | return 4; | |||
| 1908 | default: | |||
| 1909 | return 8; | |||
| 1910 | } | |||
| 1911 | } | |||
| 1912 | ||||
| 1913 | // Find or create a ThunkSection within the InputSectionDescription (ISD) that | |||
| 1914 | // is in range of Src. An ISD maps to a range of InputSections described by a | |||
| 1915 | // linker script section pattern such as { .text .text.* }. | |||
| 1916 | ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os, | |||
| 1917 | InputSection *isec, | |||
| 1918 | InputSectionDescription *isd, | |||
| 1919 | const Relocation &rel, | |||
| 1920 | uint64_t src) { | |||
| 1921 | // See the comment in getThunk for -pcBias below. | |||
| 1922 | const int64_t pcBias = getPCBias(rel.type); | |||
| 1923 | for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) { | |||
| 1924 | ThunkSection *ts = tp.first; | |||
| 1925 | uint64_t tsBase = os->addr + ts->outSecOff - pcBias; | |||
| 1926 | uint64_t tsLimit = tsBase + ts->getSize(); | |||
| 1927 | if (target->inBranchRange(rel.type, src, | |||
| 1928 | (src > tsLimit) ? tsBase : tsLimit)) | |||
| 1929 | return ts; | |||
| 1930 | } | |||
| 1931 | ||||
| 1932 | // No suitable ThunkSection exists. This can happen when there is a branch | |||
| 1933 | // with lower range than the ThunkSection spacing or when there are too | |||
| 1934 | // many Thunks. Create a new ThunkSection as close to the InputSection as | |||
| 1935 | // possible. Error if InputSection is so large we cannot place ThunkSection | |||
| 1936 | // anywhere in Range. | |||
| 1937 | uint64_t thunkSecOff = isec->outSecOff; | |||
| 1938 | if (!target->inBranchRange(rel.type, src, | |||
| 1939 | os->addr + thunkSecOff + rel.addend)) { | |||
| 1940 | thunkSecOff = isec->outSecOff + isec->getSize(); | |||
| 1941 | if (!target->inBranchRange(rel.type, src, | |||
| 1942 | os->addr + thunkSecOff + rel.addend)) | |||
| 1943 | fatal("InputSection too large for range extension thunk " + | |||
| 1944 | isec->getObjMsg(src - (os->addr + isec->outSecOff))); | |||
| 1945 | } | |||
| 1946 | return addThunkSection(os, isd, thunkSecOff); | |||
| 1947 | } | |||
| 1948 | ||||
| 1949 | // Add a Thunk that needs to be placed in a ThunkSection that immediately | |||
| 1950 | // precedes its Target. | |||
| 1951 | ThunkSection *ThunkCreator::getISThunkSec(InputSection *isec) { | |||
| 1952 | ThunkSection *ts = thunkedSections.lookup(isec); | |||
| 1953 | if (ts) | |||
| 1954 | return ts; | |||
| 1955 | ||||
| 1956 | // Find InputSectionRange within Target Output Section (TOS) that the | |||
| 1957 | // InputSection (IS) that we need to precede is in. | |||
| 1958 | OutputSection *tos = isec->getParent(); | |||
| 1959 | for (SectionCommand *bc : tos->commands) { | |||
| 1960 | auto *isd = dyn_cast<InputSectionDescription>(bc); | |||
| 1961 | if (!isd || isd->sections.empty()) | |||
| 1962 | continue; | |||
| 1963 | ||||
| 1964 | InputSection *first = isd->sections.front(); | |||
| 1965 | InputSection *last = isd->sections.back(); | |||
| 1966 | ||||
| 1967 | if (isec->outSecOff < first->outSecOff || last->outSecOff < isec->outSecOff) | |||
| 1968 | continue; | |||
| 1969 | ||||
| 1970 | ts = addThunkSection(tos, isd, isec->outSecOff); | |||
| 1971 | thunkedSections[isec] = ts; | |||
| 1972 | return ts; | |||
| 1973 | } | |||
| 1974 | ||||
| 1975 | return nullptr; | |||
| 1976 | } | |||
| 1977 | ||||
| 1978 | // Create one or more ThunkSections per OS that can be used to place Thunks. | |||
| 1979 | // We attempt to place the ThunkSections using the following desirable | |||
| 1980 | // properties: | |||
| 1981 | // - Within range of the maximum number of callers | |||
| 1982 | // - Minimise the number of ThunkSections | |||
| 1983 | // | |||
| 1984 | // We follow a simple but conservative heuristic to place ThunkSections at | |||
| 1985 | // offsets that are multiples of a Target specific branch range. | |||
| 1986 | // For an InputSectionDescription that is smaller than the range, a single | |||
| 1987 | // ThunkSection at the end of the range will do. | |||
| 1988 | // | |||
| 1989 | // For an InputSectionDescription that is more than twice the size of the range, | |||
| 1990 | // we place the last ThunkSection at range bytes from the end of the | |||
| 1991 | // InputSectionDescription in order to increase the likelihood that the | |||
| 1992 | // distance from a thunk to its target will be sufficiently small to | |||
| 1993 | // allow for the creation of a short thunk. | |||
| 1994 | void ThunkCreator::createInitialThunkSections( | |||
| 1995 | ArrayRef<OutputSection *> outputSections) { | |||
| 1996 | uint32_t thunkSectionSpacing = target->getThunkSectionSpacing(); | |||
| 1997 | ||||
| 1998 | forEachInputSectionDescription( | |||
| 1999 | outputSections, [&](OutputSection *os, InputSectionDescription *isd) { | |||
| 2000 | if (isd->sections.empty()) | |||
| ||||
| 2001 | return; | |||
| 2002 | ||||
| 2003 | uint32_t isdBegin = isd->sections.front()->outSecOff; | |||
| 2004 | uint32_t isdEnd = | |||
| 2005 | isd->sections.back()->outSecOff + isd->sections.back()->getSize(); | |||
| 2006 | uint32_t lastThunkLowerBound = -1; | |||
| 2007 | if (isdEnd - isdBegin > thunkSectionSpacing * 2) | |||
| 2008 | lastThunkLowerBound = isdEnd - thunkSectionSpacing; | |||
| 2009 | ||||
| 2010 | uint32_t isecLimit; | |||
| 2011 | uint32_t prevIsecLimit = isdBegin; | |||
| 2012 | uint32_t thunkUpperBound = isdBegin + thunkSectionSpacing; | |||
| 2013 | ||||
| 2014 | for (const InputSection *isec : isd->sections) { | |||
| 2015 | isecLimit = isec->outSecOff + isec->getSize(); | |||
| 2016 | if (isecLimit > thunkUpperBound) { | |||
| 2017 | addThunkSection(os, isd, prevIsecLimit); | |||
| 2018 | thunkUpperBound = prevIsecLimit + thunkSectionSpacing; | |||
| 2019 | } | |||
| 2020 | if (isecLimit > lastThunkLowerBound) | |||
| 2021 | break; | |||
| 2022 | prevIsecLimit = isecLimit; | |||
| 2023 | } | |||
| 2024 | addThunkSection(os, isd, isecLimit); | |||
| ||||
| 2025 | }); | |||
| 2026 | } | |||
| 2027 | ||||
| 2028 | ThunkSection *ThunkCreator::addThunkSection(OutputSection *os, | |||
| 2029 | InputSectionDescription *isd, | |||
| 2030 | uint64_t off) { | |||
| 2031 | auto *ts = make<ThunkSection>(os, off); | |||
| 2032 | ts->partition = os->partition; | |||
| 2033 | if ((config->fixCortexA53Errata843419 || config->fixCortexA8) && | |||
| 2034 | !isd->sections.empty()) { | |||
| 2035 | // The errata fixes are sensitive to addresses modulo 4 KiB. When we add | |||
| 2036 | // thunks we disturb the base addresses of sections placed after the thunks | |||
| 2037 | // this makes patches we have generated redundant, and may cause us to | |||
| 2038 | // generate more patches as different instructions are now in sensitive | |||
| 2039 | // locations. When we generate more patches we may force more branches to | |||
| 2040 | // go out of range, causing more thunks to be generated. In pathological | |||
| 2041 | // cases this can cause the address dependent content pass not to converge. | |||
| 2042 | // We fix this by rounding up the size of the ThunkSection to 4KiB, this | |||
| 2043 | // limits the insertion of a ThunkSection on the addresses modulo 4 KiB, | |||
| 2044 | // which means that adding Thunks to the section does not invalidate | |||
| 2045 | // errata patches for following code. | |||
| 2046 | // Rounding up the size to 4KiB has consequences for code-size and can | |||
| 2047 | // trip up linker script defined assertions. For example the linux kernel | |||
| 2048 | // has an assertion that what LLD represents as an InputSectionDescription | |||
| 2049 | // does not exceed 4 KiB even if the overall OutputSection is > 128 Mib. | |||
| 2050 | // We use the heuristic of rounding up the size when both of the following | |||
| 2051 | // conditions are true: | |||
| 2052 | // 1.) The OutputSection is larger than the ThunkSectionSpacing. This | |||
| 2053 | // accounts for the case where no single InputSectionDescription is | |||
| 2054 | // larger than the OutputSection size. This is conservative but simple. | |||
| 2055 | // 2.) The InputSectionDescription is larger than 4 KiB. This will prevent | |||
| 2056 | // any assertion failures that an InputSectionDescription is < 4 KiB | |||
| 2057 | // in size. | |||
| 2058 | uint64_t isdSize = isd->sections.back()->outSecOff + | |||
| 2059 | isd->sections.back()->getSize() - | |||
| 2060 | isd->sections.front()->outSecOff; | |||
| 2061 | if (os->size > target->getThunkSectionSpacing() && isdSize > 4096) | |||
| 2062 | ts->roundUpSizeForErrata = true; | |||
| 2063 | } | |||
| 2064 | isd->thunkSections.push_back({ts, pass}); | |||
| 2065 | return ts; | |||
| 2066 | } | |||
| 2067 | ||||
| 2068 | static bool isThunkSectionCompatible(InputSection *source, | |||
| 2069 | SectionBase *target) { | |||
| 2070 | // We can't reuse thunks in different loadable partitions because they might | |||
| 2071 | // not be loaded. But partition 1 (the main partition) will always be loaded. | |||
| 2072 | if (source->partition != target->partition) | |||
| 2073 | return target->partition == 1; | |||
| 2074 | return true; | |||
| 2075 | } | |||
| 2076 | ||||
| 2077 | std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec, | |||
| 2078 | Relocation &rel, uint64_t src) { | |||
| 2079 | std::vector<Thunk *> *thunkVec = nullptr; | |||
| 2080 | // Arm and Thumb have a PC Bias of 8 and 4 respectively, this is cancelled | |||
| 2081 | // out in the relocation addend. We compensate for the PC bias so that | |||
| 2082 | // an Arm and Thumb relocation to the same destination get the same keyAddend, | |||
| 2083 | // which is usually 0. | |||
| 2084 | const int64_t pcBias = getPCBias(rel.type); | |||
| 2085 | const int64_t keyAddend = rel.addend + pcBias; | |||
| 2086 | ||||
| 2087 | // We use a ((section, offset), addend) pair to find the thunk position if | |||
| 2088 | // possible so that we create only one thunk for aliased symbols or ICFed | |||
| 2089 | // sections. There may be multiple relocations sharing the same (section, | |||
| 2090 | // offset + addend) pair. We may revert the relocation back to its original | |||
| 2091 | // non-Thunk target, so we cannot fold offset + addend. | |||
| 2092 | if (auto *d = dyn_cast<Defined>(rel.sym)) | |||
| 2093 | if (!d->isInPlt() && d->section) | |||
| 2094 | thunkVec = &thunkedSymbolsBySectionAndAddend[{{d->section, d->value}, | |||
| 2095 | keyAddend}]; | |||
| 2096 | if (!thunkVec) | |||
| 2097 | thunkVec = &thunkedSymbols[{rel.sym, keyAddend}]; | |||
| 2098 | ||||
| 2099 | // Check existing Thunks for Sym to see if they can be reused | |||
| 2100 | for (Thunk *t : *thunkVec) | |||
| 2101 | if (isThunkSectionCompatible(isec, t->getThunkTargetSym()->section) && | |||
| 2102 | t->isCompatibleWith(*isec, rel) && | |||
| 2103 | target->inBranchRange(rel.type, src, | |||
| 2104 | t->getThunkTargetSym()->getVA(-pcBias))) | |||
| 2105 | return std::make_pair(t, false); | |||
| 2106 | ||||
| 2107 | // No existing compatible Thunk in range, create a new one | |||
| 2108 | Thunk *t = addThunk(*isec, rel); | |||
| 2109 | thunkVec->push_back(t); | |||
| 2110 | return std::make_pair(t, true); | |||
| 2111 | } | |||
| 2112 | ||||
| 2113 | // Return true if the relocation target is an in range Thunk. | |||
| 2114 | // Return false if the relocation is not to a Thunk. If the relocation target | |||
| 2115 | // was originally to a Thunk, but is no longer in range we revert the | |||
| 2116 | // relocation back to its original non-Thunk target. | |||
| 2117 | bool ThunkCreator::normalizeExistingThunk(Relocation &rel, uint64_t src) { | |||
| 2118 | if (Thunk *t = thunks.lookup(rel.sym)) { | |||
| 2119 | if (target->inBranchRange(rel.type, src, rel.sym->getVA(rel.addend))) | |||
| 2120 | return true; | |||
| 2121 | rel.sym = &t->destination; | |||
| 2122 | rel.addend = t->addend; | |||
| 2123 | if (rel.sym->isInPlt()) | |||
| 2124 | rel.expr = toPlt(rel.expr); | |||
| 2125 | } | |||
| 2126 | return false; | |||
| 2127 | } | |||
| 2128 | ||||
| 2129 | // Process all relocations from the InputSections that have been assigned | |||
| 2130 | // to InputSectionDescriptions and redirect through Thunks if needed. The | |||
| 2131 | // function should be called iteratively until it returns false. | |||
| 2132 | // | |||
| 2133 | // PreConditions: | |||
| 2134 | // All InputSections that may need a Thunk are reachable from | |||
| 2135 | // OutputSectionCommands. | |||
| 2136 | // | |||
| 2137 | // All OutputSections have an address and all InputSections have an offset | |||
| 2138 | // within the OutputSection. | |||
| 2139 | // | |||
| 2140 | // The offsets between caller (relocation place) and callee | |||
| 2141 | // (relocation target) will not be modified outside of createThunks(). | |||
| 2142 | // | |||
| 2143 | // PostConditions: | |||
| 2144 | // If return value is true then ThunkSections have been inserted into | |||
| 2145 | // OutputSections. All relocations that needed a Thunk based on the information | |||
| 2146 | // available to createThunks() on entry have been redirected to a Thunk. Note | |||
| 2147 | // that adding Thunks changes offsets between caller and callee so more Thunks | |||
| 2148 | // may be required. | |||
| 2149 | // | |||
| 2150 | // If return value is false then no more Thunks are needed, and createThunks has | |||
| 2151 | // made no changes. If the target requires range extension thunks, currently | |||
| 2152 | // ARM, then any future change in offset between caller and callee risks a | |||
| 2153 | // relocation out of range error. | |||
| 2154 | bool ThunkCreator::createThunks(uint32_t pass, | |||
| 2155 | ArrayRef<OutputSection *> outputSections) { | |||
| 2156 | this->pass = pass; | |||
| 2157 | bool addressesChanged = false; | |||
| 2158 | ||||
| 2159 | if (pass == 0 && target->getThunkSectionSpacing()) | |||
| 2160 | createInitialThunkSections(outputSections); | |||
| 2161 | ||||
| 2162 | // Create all the Thunks and insert them into synthetic ThunkSections. The | |||
| 2163 | // ThunkSections are later inserted back into InputSectionDescriptions. | |||
| 2164 | // We separate the creation of ThunkSections from the insertion of the | |||
| 2165 | // ThunkSections as ThunkSections are not always inserted into the same | |||
| 2166 | // InputSectionDescription as the caller. | |||
| 2167 | forEachInputSectionDescription( | |||
| 2168 | outputSections, [&](OutputSection *os, InputSectionDescription *isd) { | |||
| 2169 | for (InputSection *isec : isd->sections) | |||
| 2170 | for (Relocation &rel : isec->relocs()) { | |||
| 2171 | uint64_t src = isec->getVA(rel.offset); | |||
| 2172 | ||||
| 2173 | // If we are a relocation to an existing Thunk, check if it is | |||
| 2174 | // still in range. If not then Rel will be altered to point to its | |||
| 2175 | // original target so another Thunk can be generated. | |||
| 2176 | if (pass > 0 && normalizeExistingThunk(rel, src)) | |||
| 2177 | continue; | |||
| 2178 | ||||
| 2179 | if (!target->needsThunk(rel.expr, rel.type, isec->file, src, | |||
| 2180 | *rel.sym, rel.addend)) | |||
| 2181 | continue; | |||
| 2182 | ||||
| 2183 | Thunk *t; | |||
| 2184 | bool isNew; | |||
| 2185 | std::tie(t, isNew) = getThunk(isec, rel, src); | |||
| 2186 | ||||
| 2187 | if (isNew) { | |||
| 2188 | // Find or create a ThunkSection for the new Thunk | |||
| 2189 | ThunkSection *ts; | |||
| 2190 | if (auto *tis = t->getTargetInputSection()) | |||
| 2191 | ts = getISThunkSec(tis); | |||
| 2192 | else | |||
| 2193 | ts = getISDThunkSec(os, isec, isd, rel, src); | |||
| 2194 | ts->addThunk(t); | |||
| 2195 | thunks[t->getThunkTargetSym()] = t; | |||
| 2196 | } | |||
| 2197 | ||||
| 2198 | // Redirect relocation to Thunk, we never go via the PLT to a Thunk | |||
| 2199 | rel.sym = t->getThunkTargetSym(); | |||
| 2200 | rel.expr = fromPlt(rel.expr); | |||
| 2201 | ||||
| 2202 | // On AArch64 and PPC, a jump/call relocation may be encoded as | |||
| 2203 | // STT_SECTION + non-zero addend, clear the addend after | |||
| 2204 | // redirection. | |||
| 2205 | if (config->emachine != EM_MIPS) | |||
| 2206 | rel.addend = -getPCBias(rel.type); | |||
| 2207 | } | |||
| 2208 | ||||
| 2209 | for (auto &p : isd->thunkSections) | |||
| 2210 | addressesChanged |= p.first->assignOffsets(); | |||
| 2211 | }); | |||
| 2212 | ||||
| 2213 | for (auto &p : thunkedSections) | |||
| 2214 | addressesChanged |= p.second->assignOffsets(); | |||
| 2215 | ||||
| 2216 | // Merge all created synthetic ThunkSections back into OutputSection | |||
| 2217 | mergeThunks(outputSections); | |||
| 2218 | return addressesChanged; | |||
| 2219 | } | |||
| 2220 | ||||
| 2221 | // The following aid in the conversion of call x@GDPLT to call __tls_get_addr | |||
| 2222 | // hexagonNeedsTLSSymbol scans for relocations would require a call to | |||
| 2223 | // __tls_get_addr. | |||
| 2224 | // hexagonTLSSymbolUpdate rebinds the relocation to __tls_get_addr. | |||
| 2225 | bool elf::hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections) { | |||
| 2226 | bool needTlsSymbol = false; | |||
| 2227 | forEachInputSectionDescription( | |||
| 2228 | outputSections, [&](OutputSection *os, InputSectionDescription *isd) { | |||
| 2229 | for (InputSection *isec : isd->sections) | |||
| 2230 | for (Relocation &rel : isec->relocs()) | |||
| 2231 | if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) { | |||
| 2232 | needTlsSymbol = true; | |||
| 2233 | return; | |||
| 2234 | } | |||
| 2235 | }); | |||
| 2236 | return needTlsSymbol; | |||
| 2237 | } | |||
| 2238 | ||||
| 2239 | void elf::hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections) { | |||
| 2240 | Symbol *sym = symtab.find("__tls_get_addr"); | |||
| 2241 | if (!sym) | |||
| 2242 | return; | |||
| 2243 | bool needEntry = true; | |||
| 2244 | forEachInputSectionDescription( | |||
| 2245 | outputSections, [&](OutputSection *os, InputSectionDescription *isd) { | |||
| 2246 | for (InputSection *isec : isd->sections) | |||
| 2247 | for (Relocation &rel : isec->relocs()) | |||
| 2248 | if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) { | |||
| 2249 | if (needEntry) { | |||
| 2250 | sym->allocateAux(); | |||
| 2251 | addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, | |||
| 2252 | *sym); | |||
| 2253 | needEntry = false; | |||
| 2254 | } | |||
| 2255 | rel.sym = sym; | |||
| 2256 | } | |||
| 2257 | }); | |||
| 2258 | } | |||
| 2259 | ||||
| 2260 | template void elf::scanRelocations<ELF32LE>(); | |||
| 2261 | template void elf::scanRelocations<ELF32BE>(); | |||
| 2262 | template void elf::scanRelocations<ELF64LE>(); | |||
| 2263 | template void elf::scanRelocations<ELF64BE>(); |