1ece8a530Spatrick //===- Relocations.cpp ----------------------------------------------------===// 2ece8a530Spatrick // 3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information. 5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6ece8a530Spatrick // 7ece8a530Spatrick //===----------------------------------------------------------------------===// 8ece8a530Spatrick // 9ece8a530Spatrick // This file contains platform-independent functions to process relocations. 10ece8a530Spatrick // I'll describe the overview of this file here. 11ece8a530Spatrick // 12ece8a530Spatrick // Simple relocations are easy to handle for the linker. For example, 13ece8a530Spatrick // for R_X86_64_PC64 relocs, the linker just has to fix up locations 14ece8a530Spatrick // with the relative offsets to the target symbols. It would just be 15ece8a530Spatrick // reading records from relocation sections and applying them to output. 16ece8a530Spatrick // 17ece8a530Spatrick // But not all relocations are that easy to handle. For example, for 18ece8a530Spatrick // R_386_GOTOFF relocs, the linker has to create new GOT entries for 19ece8a530Spatrick // symbols if they don't exist, and fix up locations with GOT entry 20ece8a530Spatrick // offsets from the beginning of GOT section. So there is more than 21ece8a530Spatrick // fixing addresses in relocation processing. 22ece8a530Spatrick // 23ece8a530Spatrick // ELF defines a large number of complex relocations. 24ece8a530Spatrick // 25ece8a530Spatrick // The functions in this file analyze relocations and do whatever needs 26ece8a530Spatrick // to be done. It includes, but not limited to, the following. 27ece8a530Spatrick // 28ece8a530Spatrick // - create GOT/PLT entries 29ece8a530Spatrick // - create new relocations in .dynsym to let the dynamic linker resolve 30ece8a530Spatrick // them at runtime (since ELF supports dynamic linking, not all 31ece8a530Spatrick // relocations can be resolved at link-time) 32ece8a530Spatrick // - create COPY relocs and reserve space in .bss 33ece8a530Spatrick // - replace expensive relocs (in terms of runtime cost) with cheap ones 34ece8a530Spatrick // - error out infeasible combinations such as PIC and non-relative relocs 35ece8a530Spatrick // 36ece8a530Spatrick // Note that the functions in this file don't actually apply relocations 37ece8a530Spatrick // because it doesn't know about the output file nor the output file buffer. 38ece8a530Spatrick // It instead stores Relocation objects to InputSection's Relocations 39ece8a530Spatrick // vector to let it apply later in InputSection::writeTo. 40ece8a530Spatrick // 41ece8a530Spatrick //===----------------------------------------------------------------------===// 42ece8a530Spatrick 43ece8a530Spatrick #include "Relocations.h" 44ece8a530Spatrick #include "Config.h" 4505edf1c1Srobert #include "InputFiles.h" 46ece8a530Spatrick #include "LinkerScript.h" 47ece8a530Spatrick #include "OutputSections.h" 48ece8a530Spatrick #include "SymbolTable.h" 49ece8a530Spatrick #include "Symbols.h" 50ece8a530Spatrick #include "SyntheticSections.h" 51ece8a530Spatrick #include "Target.h" 52ece8a530Spatrick #include "Thunks.h" 53ece8a530Spatrick #include "lld/Common/ErrorHandler.h" 54ece8a530Spatrick #include "lld/Common/Memory.h" 55ece8a530Spatrick #include "llvm/ADT/SmallSet.h" 56ece8a530Spatrick #include "llvm/Demangle/Demangle.h" 57ece8a530Spatrick #include "llvm/Support/Endian.h" 58ece8a530Spatrick #include <algorithm> 59ece8a530Spatrick 60ece8a530Spatrick using namespace llvm; 61ece8a530Spatrick using namespace llvm::ELF; 62ece8a530Spatrick using namespace llvm::object; 63ece8a530Spatrick using namespace llvm::support::endian; 64adae0cfdSpatrick using namespace lld; 65adae0cfdSpatrick using namespace lld::elf; 66ece8a530Spatrick 6705edf1c1Srobert static std::optional<std::string> getLinkerScriptLocation(const Symbol &sym) { 6805edf1c1Srobert for (SectionCommand *cmd : script->sectionCommands) 6905edf1c1Srobert if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) 7005edf1c1Srobert if (assign->sym == &sym) 7105edf1c1Srobert return assign->location; 7205edf1c1Srobert return std::nullopt; 73ece8a530Spatrick } 74ece8a530Spatrick 75adae0cfdSpatrick static std::string getDefinedLocation(const Symbol &sym) { 76a0747c9fSpatrick const char msg[] = "\n>>> defined in "; 77adae0cfdSpatrick if (sym.file) 78a0747c9fSpatrick return msg + toString(sym.file); 7905edf1c1Srobert if (std::optional<std::string> loc = getLinkerScriptLocation(sym)) 80a0747c9fSpatrick return msg + *loc; 81a0747c9fSpatrick return ""; 82adae0cfdSpatrick } 83adae0cfdSpatrick 84ece8a530Spatrick // Construct a message in the following format. 85ece8a530Spatrick // 86ece8a530Spatrick // >>> defined in /home/alice/src/foo.o 87ece8a530Spatrick // >>> referenced by bar.c:12 (/home/alice/src/bar.c:12) 88ece8a530Spatrick // >>> /home/alice/src/bar.o:(.text+0x1) 89ece8a530Spatrick static std::string getLocation(InputSectionBase &s, const Symbol &sym, 90ece8a530Spatrick uint64_t off) { 91adae0cfdSpatrick std::string msg = getDefinedLocation(sym) + "\n>>> referenced by "; 92ece8a530Spatrick std::string src = s.getSrcMsg(sym, off); 93ece8a530Spatrick if (!src.empty()) 94ece8a530Spatrick msg += src + "\n>>> "; 95ece8a530Spatrick return msg + s.getObjMsg(off); 96ece8a530Spatrick } 97ece8a530Spatrick 98adae0cfdSpatrick void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v, 99adae0cfdSpatrick int64_t min, uint64_t max) { 100adae0cfdSpatrick ErrorPlace errPlace = getErrorPlace(loc); 101adae0cfdSpatrick std::string hint; 10205edf1c1Srobert if (rel.sym && !rel.sym->isSection()) 10305edf1c1Srobert hint = "; references " + lld::toString(*rel.sym); 10405edf1c1Srobert if (!errPlace.srcLoc.empty()) 10505edf1c1Srobert hint += "\n>>> referenced by " + errPlace.srcLoc; 10605edf1c1Srobert if (rel.sym && !rel.sym->isSection()) 10705edf1c1Srobert hint += getDefinedLocation(*rel.sym); 108adae0cfdSpatrick 109adae0cfdSpatrick if (errPlace.isec && errPlace.isec->name.startswith(".debug")) 110adae0cfdSpatrick hint += "; consider recompiling with -fdebug-types-section to reduce size " 111adae0cfdSpatrick "of debug sections"; 112adae0cfdSpatrick 113adae0cfdSpatrick errorOrWarn(errPlace.loc + "relocation " + lld::toString(rel.type) + 114adae0cfdSpatrick " out of range: " + v.str() + " is not in [" + Twine(min).str() + 115adae0cfdSpatrick ", " + Twine(max).str() + "]" + hint); 116adae0cfdSpatrick } 117adae0cfdSpatrick 118a0747c9fSpatrick void elf::reportRangeError(uint8_t *loc, int64_t v, int n, const Symbol &sym, 119a0747c9fSpatrick const Twine &msg) { 120a0747c9fSpatrick ErrorPlace errPlace = getErrorPlace(loc); 121a0747c9fSpatrick std::string hint; 122a0747c9fSpatrick if (!sym.getName().empty()) 123a0747c9fSpatrick hint = "; references " + lld::toString(sym) + getDefinedLocation(sym); 124a0747c9fSpatrick errorOrWarn(errPlace.loc + msg + " is out of range: " + Twine(v) + 125a0747c9fSpatrick " is not in [" + Twine(llvm::minIntN(n)) + ", " + 126a0747c9fSpatrick Twine(llvm::maxIntN(n)) + "]" + hint); 127a0747c9fSpatrick } 128a0747c9fSpatrick 12905edf1c1Srobert // Build a bitmask with one bit set for each 64 subset of RelExpr. 13005edf1c1Srobert static constexpr uint64_t buildMask() { return 0; } 131ece8a530Spatrick 13205edf1c1Srobert template <typename... Tails> 13305edf1c1Srobert static constexpr uint64_t buildMask(int head, Tails... tails) { 13405edf1c1Srobert return (0 <= head && head < 64 ? uint64_t(1) << head : 0) | 13505edf1c1Srobert buildMask(tails...); 136ece8a530Spatrick } 137ece8a530Spatrick 138ece8a530Spatrick // Return true if `Expr` is one of `Exprs`. 13905edf1c1Srobert // There are more than 64 but less than 128 RelExprs, so we divide the set of 14005edf1c1Srobert // exprs into [0, 64) and [64, 128) and represent each range as a constant 14105edf1c1Srobert // 64-bit mask. Then we decide which mask to test depending on the value of 14205edf1c1Srobert // expr and use a simple shift and bitwise-and to test for membership. 14305edf1c1Srobert template <RelExpr... Exprs> static bool oneof(RelExpr expr) { 14405edf1c1Srobert assert(0 <= expr && (int)expr < 128 && 14505edf1c1Srobert "RelExpr is too large for 128-bit mask!"); 146ece8a530Spatrick 14705edf1c1Srobert if (expr >= 64) 14805edf1c1Srobert return (uint64_t(1) << (expr - 64)) & buildMask((Exprs - 64)...); 14905edf1c1Srobert return (uint64_t(1) << expr) & buildMask(Exprs...); 150ece8a530Spatrick } 151ece8a530Spatrick 152ece8a530Spatrick static RelType getMipsPairType(RelType type, bool isLocal) { 153ece8a530Spatrick switch (type) { 154ece8a530Spatrick case R_MIPS_HI16: 155ece8a530Spatrick return R_MIPS_LO16; 156ece8a530Spatrick case R_MIPS_GOT16: 157ece8a530Spatrick // In case of global symbol, the R_MIPS_GOT16 relocation does not 158ece8a530Spatrick // have a pair. Each global symbol has a unique entry in the GOT 159ece8a530Spatrick // and a corresponding instruction with help of the R_MIPS_GOT16 160ece8a530Spatrick // relocation loads an address of the symbol. In case of local 161ece8a530Spatrick // symbol, the R_MIPS_GOT16 relocation creates a GOT entry to hold 162ece8a530Spatrick // the high 16 bits of the symbol's value. A paired R_MIPS_LO16 163ece8a530Spatrick // relocations handle low 16 bits of the address. That allows 164ece8a530Spatrick // to allocate only one GOT entry for every 64 KBytes of local data. 165ece8a530Spatrick return isLocal ? R_MIPS_LO16 : R_MIPS_NONE; 166ece8a530Spatrick case R_MICROMIPS_GOT16: 167ece8a530Spatrick return isLocal ? R_MICROMIPS_LO16 : R_MIPS_NONE; 168ece8a530Spatrick case R_MIPS_PCHI16: 169ece8a530Spatrick return R_MIPS_PCLO16; 170ece8a530Spatrick case R_MICROMIPS_HI16: 171ece8a530Spatrick return R_MICROMIPS_LO16; 172ece8a530Spatrick default: 173ece8a530Spatrick return R_MIPS_NONE; 174ece8a530Spatrick } 175ece8a530Spatrick } 176ece8a530Spatrick 177ece8a530Spatrick // True if non-preemptable symbol always has the same value regardless of where 178ece8a530Spatrick // the DSO is loaded. 179ece8a530Spatrick static bool isAbsolute(const Symbol &sym) { 180ece8a530Spatrick if (sym.isUndefWeak()) 181ece8a530Spatrick return true; 182ece8a530Spatrick if (const auto *dr = dyn_cast<Defined>(&sym)) 183ece8a530Spatrick return dr->section == nullptr; // Absolute symbol. 184ece8a530Spatrick return false; 185ece8a530Spatrick } 186ece8a530Spatrick 187ece8a530Spatrick static bool isAbsoluteValue(const Symbol &sym) { 188ece8a530Spatrick return isAbsolute(sym) || sym.isTls(); 189ece8a530Spatrick } 190ece8a530Spatrick 191ece8a530Spatrick // Returns true if Expr refers a PLT entry. 192ece8a530Spatrick static bool needsPlt(RelExpr expr) { 19305edf1c1Srobert return oneof<R_PLT, R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT>( 19405edf1c1Srobert expr); 195ece8a530Spatrick } 196ece8a530Spatrick 197ece8a530Spatrick // Returns true if Expr refers a GOT entry. Note that this function 198ece8a530Spatrick // returns false for TLS variables even though they need GOT, because 199ece8a530Spatrick // TLS variables uses GOT differently than the regular variables. 200ece8a530Spatrick static bool needsGot(RelExpr expr) { 201ece8a530Spatrick return oneof<R_GOT, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE, R_MIPS_GOT_OFF, 202a0747c9fSpatrick R_MIPS_GOT_OFF32, R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTPLT, 203a0747c9fSpatrick R_AARCH64_GOT_PAGE>(expr); 204ece8a530Spatrick } 205ece8a530Spatrick 206ece8a530Spatrick // True if this expression is of the form Sym - X, where X is a position in the 207ece8a530Spatrick // file (PC, or GOT for example). 208ece8a530Spatrick static bool isRelExpr(RelExpr expr) { 209ece8a530Spatrick return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC64_CALL, 210ece8a530Spatrick R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC, 211a0747c9fSpatrick R_RISCV_PC_INDIRECT, R_PPC64_RELAX_GOT_PC>(expr); 212ece8a530Spatrick } 213ece8a530Spatrick 214ece8a530Spatrick 215ece8a530Spatrick static RelExpr toPlt(RelExpr expr) { 216ece8a530Spatrick switch (expr) { 217ece8a530Spatrick case R_PPC64_CALL: 218ece8a530Spatrick return R_PPC64_CALL_PLT; 219ece8a530Spatrick case R_PC: 220ece8a530Spatrick return R_PLT_PC; 221ece8a530Spatrick case R_ABS: 222ece8a530Spatrick return R_PLT; 223ece8a530Spatrick default: 224ece8a530Spatrick return expr; 225ece8a530Spatrick } 226ece8a530Spatrick } 227ece8a530Spatrick 228ece8a530Spatrick static RelExpr fromPlt(RelExpr expr) { 229ece8a530Spatrick // We decided not to use a plt. Optimize a reference to the plt to a 230ece8a530Spatrick // reference to the symbol itself. 231ece8a530Spatrick switch (expr) { 232ece8a530Spatrick case R_PLT_PC: 233ece8a530Spatrick case R_PPC32_PLTREL: 234ece8a530Spatrick return R_PC; 235ece8a530Spatrick case R_PPC64_CALL_PLT: 236ece8a530Spatrick return R_PPC64_CALL; 237ece8a530Spatrick case R_PLT: 238ece8a530Spatrick return R_ABS; 23905edf1c1Srobert case R_PLT_GOTPLT: 24005edf1c1Srobert return R_GOTPLTREL; 241ece8a530Spatrick default: 242ece8a530Spatrick return expr; 243ece8a530Spatrick } 244ece8a530Spatrick } 245ece8a530Spatrick 246ece8a530Spatrick // Returns true if a given shared symbol is in a read-only segment in a DSO. 247ece8a530Spatrick template <class ELFT> static bool isReadOnly(SharedSymbol &ss) { 248ece8a530Spatrick using Elf_Phdr = typename ELFT::Phdr; 249ece8a530Spatrick 250ece8a530Spatrick // Determine if the symbol is read-only by scanning the DSO's program headers. 25105edf1c1Srobert const auto &file = cast<SharedFile>(*ss.file); 252ece8a530Spatrick for (const Elf_Phdr &phdr : 253ece8a530Spatrick check(file.template getObj<ELFT>().program_headers())) 254ece8a530Spatrick if ((phdr.p_type == ELF::PT_LOAD || phdr.p_type == ELF::PT_GNU_RELRO) && 255ece8a530Spatrick !(phdr.p_flags & ELF::PF_W) && ss.value >= phdr.p_vaddr && 256ece8a530Spatrick ss.value < phdr.p_vaddr + phdr.p_memsz) 257ece8a530Spatrick return true; 258ece8a530Spatrick return false; 259ece8a530Spatrick } 260ece8a530Spatrick 261ece8a530Spatrick // Returns symbols at the same offset as a given symbol, including SS itself. 262ece8a530Spatrick // 263ece8a530Spatrick // If two or more symbols are at the same offset, and at least one of 264ece8a530Spatrick // them are copied by a copy relocation, all of them need to be copied. 265ece8a530Spatrick // Otherwise, they would refer to different places at runtime. 266ece8a530Spatrick template <class ELFT> 267ece8a530Spatrick static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &ss) { 268ece8a530Spatrick using Elf_Sym = typename ELFT::Sym; 269ece8a530Spatrick 27005edf1c1Srobert const auto &file = cast<SharedFile>(*ss.file); 271ece8a530Spatrick 272ece8a530Spatrick SmallSet<SharedSymbol *, 4> ret; 273ece8a530Spatrick for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) { 274ece8a530Spatrick if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS || 275ece8a530Spatrick s.getType() == STT_TLS || s.st_value != ss.value) 276ece8a530Spatrick continue; 277ece8a530Spatrick StringRef name = check(s.getName(file.getStringTable())); 27805edf1c1Srobert Symbol *sym = symtab.find(name); 279ece8a530Spatrick if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym)) 280ece8a530Spatrick ret.insert(alias); 281ece8a530Spatrick } 282a0747c9fSpatrick 283a0747c9fSpatrick // The loop does not check SHT_GNU_verneed, so ret does not contain 284a0747c9fSpatrick // non-default version symbols. If ss has a non-default version, ret won't 285a0747c9fSpatrick // contain ss. Just add ss unconditionally. If a non-default version alias is 286a0747c9fSpatrick // separately copy relocated, it and ss will have different addresses. 287a0747c9fSpatrick // Fortunately this case is impractical and fails with GNU ld as well. 288a0747c9fSpatrick ret.insert(&ss); 289ece8a530Spatrick return ret; 290ece8a530Spatrick } 291ece8a530Spatrick 292ece8a530Spatrick // When a symbol is copy relocated or we create a canonical plt entry, it is 293ece8a530Spatrick // effectively a defined symbol. In the case of copy relocation the symbol is 294ece8a530Spatrick // in .bss and in the case of a canonical plt entry it is in .plt. This function 295ece8a530Spatrick // replaces the existing symbol with a Defined pointing to the appropriate 296ece8a530Spatrick // location. 29705edf1c1Srobert static void replaceWithDefined(Symbol &sym, SectionBase &sec, uint64_t value, 298ece8a530Spatrick uint64_t size) { 299ece8a530Spatrick Symbol old = sym; 30005edf1c1Srobert Defined(sym.file, StringRef(), sym.binding, sym.stOther, sym.type, value, 30105edf1c1Srobert size, &sec) 30205edf1c1Srobert .overwrite(sym); 303ece8a530Spatrick 304ece8a530Spatrick sym.verdefIndex = old.verdefIndex; 305ece8a530Spatrick sym.exportDynamic = true; 306ece8a530Spatrick sym.isUsedInRegularObj = true; 30705edf1c1Srobert // A copy relocated alias may need a GOT entry. 30805edf1c1Srobert sym.flags.store(old.flags.load(std::memory_order_relaxed) & NEEDS_GOT, 30905edf1c1Srobert std::memory_order_relaxed); 310ece8a530Spatrick } 311ece8a530Spatrick 312ece8a530Spatrick // Reserve space in .bss or .bss.rel.ro for copy relocation. 313ece8a530Spatrick // 314ece8a530Spatrick // The copy relocation is pretty much a hack. If you use a copy relocation 315ece8a530Spatrick // in your program, not only the symbol name but the symbol's size, RW/RO 316ece8a530Spatrick // bit and alignment become part of the ABI. In addition to that, if the 317ece8a530Spatrick // symbol has aliases, the aliases become part of the ABI. That's subtle, 318ece8a530Spatrick // but if you violate that implicit ABI, that can cause very counter- 319ece8a530Spatrick // intuitive consequences. 320ece8a530Spatrick // 321ece8a530Spatrick // So, what is the copy relocation? It's for linking non-position 322ece8a530Spatrick // independent code to DSOs. In an ideal world, all references to data 323ece8a530Spatrick // exported by DSOs should go indirectly through GOT. But if object files 324ece8a530Spatrick // are compiled as non-PIC, all data references are direct. There is no 325ece8a530Spatrick // way for the linker to transform the code to use GOT, as machine 326ece8a530Spatrick // instructions are already set in stone in object files. This is where 327ece8a530Spatrick // the copy relocation takes a role. 328ece8a530Spatrick // 329ece8a530Spatrick // A copy relocation instructs the dynamic linker to copy data from a DSO 330ece8a530Spatrick // to a specified address (which is usually in .bss) at load-time. If the 331ece8a530Spatrick // static linker (that's us) finds a direct data reference to a DSO 332ece8a530Spatrick // symbol, it creates a copy relocation, so that the symbol can be 333ece8a530Spatrick // resolved as if it were in .bss rather than in a DSO. 334ece8a530Spatrick // 335ece8a530Spatrick // As you can see in this function, we create a copy relocation for the 336ece8a530Spatrick // dynamic linker, and the relocation contains not only symbol name but 337ece8a530Spatrick // various other information about the symbol. So, such attributes become a 338ece8a530Spatrick // part of the ABI. 339ece8a530Spatrick // 340ece8a530Spatrick // Note for application developers: I can give you a piece of advice if 341ece8a530Spatrick // you are writing a shared library. You probably should export only 342ece8a530Spatrick // functions from your library. You shouldn't export variables. 343ece8a530Spatrick // 344ece8a530Spatrick // As an example what can happen when you export variables without knowing 345ece8a530Spatrick // the semantics of copy relocations, assume that you have an exported 346ece8a530Spatrick // variable of type T. It is an ABI-breaking change to add new members at 347ece8a530Spatrick // end of T even though doing that doesn't change the layout of the 348ece8a530Spatrick // existing members. That's because the space for the new members are not 349ece8a530Spatrick // reserved in .bss unless you recompile the main program. That means they 350ece8a530Spatrick // are likely to overlap with other data that happens to be laid out next 351ece8a530Spatrick // to the variable in .bss. This kind of issue is sometimes very hard to 352ece8a530Spatrick // debug. What's a solution? Instead of exporting a variable V from a DSO, 353ece8a530Spatrick // define an accessor getV(). 354ece8a530Spatrick template <class ELFT> static void addCopyRelSymbol(SharedSymbol &ss) { 355ece8a530Spatrick // Copy relocation against zero-sized symbol doesn't make sense. 356ece8a530Spatrick uint64_t symSize = ss.getSize(); 357ece8a530Spatrick if (symSize == 0 || ss.alignment == 0) 358ece8a530Spatrick fatal("cannot create a copy relocation for symbol " + toString(ss)); 359ece8a530Spatrick 360ece8a530Spatrick // See if this symbol is in a read-only segment. If so, preserve the symbol's 361ece8a530Spatrick // memory protection by reserving space in the .bss.rel.ro section. 362ece8a530Spatrick bool isRO = isReadOnly<ELFT>(ss); 363ece8a530Spatrick BssSection *sec = 364ece8a530Spatrick make<BssSection>(isRO ? ".bss.rel.ro" : ".bss", symSize, ss.alignment); 365ece8a530Spatrick OutputSection *osec = (isRO ? in.bssRelRo : in.bss)->getParent(); 366ece8a530Spatrick 367ece8a530Spatrick // At this point, sectionBases has been migrated to sections. Append sec to 368ece8a530Spatrick // sections. 36905edf1c1Srobert if (osec->commands.empty() || 37005edf1c1Srobert !isa<InputSectionDescription>(osec->commands.back())) 37105edf1c1Srobert osec->commands.push_back(make<InputSectionDescription>("")); 37205edf1c1Srobert auto *isd = cast<InputSectionDescription>(osec->commands.back()); 373ece8a530Spatrick isd->sections.push_back(sec); 374ece8a530Spatrick osec->commitSection(sec); 375ece8a530Spatrick 376ece8a530Spatrick // Look through the DSO's dynamic symbol table for aliases and create a 377ece8a530Spatrick // dynamic symbol for each one. This causes the copy relocation to correctly 378ece8a530Spatrick // interpose any aliases. 379ece8a530Spatrick for (SharedSymbol *sym : getSymbolsAt<ELFT>(ss)) 38005edf1c1Srobert replaceWithDefined(*sym, *sec, 0, sym->size); 381ece8a530Spatrick 38205edf1c1Srobert mainPart->relaDyn->addSymbolReloc(target->copyRel, *sec, 0, ss); 383ece8a530Spatrick } 384ece8a530Spatrick 38505edf1c1Srobert // .eh_frame sections are mergeable input sections, so their input 38605edf1c1Srobert // offsets are not linearly mapped to output section. For each input 38705edf1c1Srobert // offset, we need to find a section piece containing the offset and 38805edf1c1Srobert // add the piece's base address to the input offset to compute the 38905edf1c1Srobert // output offset. That isn't cheap. 39005edf1c1Srobert // 39105edf1c1Srobert // This class is to speed up the offset computation. When we process 39205edf1c1Srobert // relocations, we access offsets in the monotonically increasing 39305edf1c1Srobert // order. So we can optimize for that access pattern. 39405edf1c1Srobert // 39505edf1c1Srobert // For sections other than .eh_frame, this class doesn't do anything. 39605edf1c1Srobert namespace { 39705edf1c1Srobert class OffsetGetter { 39805edf1c1Srobert public: 39905edf1c1Srobert OffsetGetter() = default; 40005edf1c1Srobert explicit OffsetGetter(InputSectionBase &sec) { 40105edf1c1Srobert if (auto *eh = dyn_cast<EhInputSection>(&sec)) { 40205edf1c1Srobert cies = eh->cies; 40305edf1c1Srobert fdes = eh->fdes; 40405edf1c1Srobert i = cies.begin(); 40505edf1c1Srobert j = fdes.begin(); 40605edf1c1Srobert } 40705edf1c1Srobert } 40805edf1c1Srobert 40905edf1c1Srobert // Translates offsets in input sections to offsets in output sections. 41005edf1c1Srobert // Given offset must increase monotonically. We assume that Piece is 41105edf1c1Srobert // sorted by inputOff. 41205edf1c1Srobert uint64_t get(uint64_t off) { 41305edf1c1Srobert if (cies.empty()) 41405edf1c1Srobert return off; 41505edf1c1Srobert 41605edf1c1Srobert while (j != fdes.end() && j->inputOff <= off) 41705edf1c1Srobert ++j; 41805edf1c1Srobert auto it = j; 41905edf1c1Srobert if (j == fdes.begin() || j[-1].inputOff + j[-1].size <= off) { 42005edf1c1Srobert while (i != cies.end() && i->inputOff <= off) 42105edf1c1Srobert ++i; 42205edf1c1Srobert if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off) 42305edf1c1Srobert fatal(".eh_frame: relocation is not in any piece"); 42405edf1c1Srobert it = i; 42505edf1c1Srobert } 42605edf1c1Srobert 42705edf1c1Srobert // Offset -1 means that the piece is dead (i.e. garbage collected). 42805edf1c1Srobert if (it[-1].outputOff == -1) 42905edf1c1Srobert return -1; 43005edf1c1Srobert return it[-1].outputOff + (off - it[-1].inputOff); 43105edf1c1Srobert } 43205edf1c1Srobert 43305edf1c1Srobert private: 43405edf1c1Srobert ArrayRef<EhSectionPiece> cies, fdes; 43505edf1c1Srobert ArrayRef<EhSectionPiece>::iterator i, j; 43605edf1c1Srobert }; 43705edf1c1Srobert 43805edf1c1Srobert // This class encapsulates states needed to scan relocations for one 43905edf1c1Srobert // InputSectionBase. 44005edf1c1Srobert class RelocationScanner { 44105edf1c1Srobert public: 44205edf1c1Srobert template <class ELFT> void scanSection(InputSectionBase &s); 44305edf1c1Srobert 44405edf1c1Srobert private: 44505edf1c1Srobert InputSectionBase *sec; 44605edf1c1Srobert OffsetGetter getter; 44705edf1c1Srobert 44805edf1c1Srobert // End of relocations, used by Mips/PPC64. 44905edf1c1Srobert const void *end = nullptr; 45005edf1c1Srobert 45105edf1c1Srobert template <class RelTy> RelType getMipsN32RelType(RelTy *&rel) const; 45205edf1c1Srobert template <class ELFT, class RelTy> 45305edf1c1Srobert int64_t computeMipsAddend(const RelTy &rel, RelExpr expr, bool isLocal) const; 45405edf1c1Srobert bool isStaticLinkTimeConstant(RelExpr e, RelType type, const Symbol &sym, 45505edf1c1Srobert uint64_t relOff) const; 45605edf1c1Srobert void processAux(RelExpr expr, RelType type, uint64_t offset, Symbol &sym, 45705edf1c1Srobert int64_t addend) const; 45805edf1c1Srobert template <class ELFT, class RelTy> void scanOne(RelTy *&i); 45905edf1c1Srobert template <class ELFT, class RelTy> void scan(ArrayRef<RelTy> rels); 46005edf1c1Srobert }; 46105edf1c1Srobert } // namespace 46205edf1c1Srobert 463ece8a530Spatrick // MIPS has an odd notion of "paired" relocations to calculate addends. 464ece8a530Spatrick // For example, if a relocation is of R_MIPS_HI16, there must be a 465ece8a530Spatrick // R_MIPS_LO16 relocation after that, and an addend is calculated using 466ece8a530Spatrick // the two relocations. 467ece8a530Spatrick template <class ELFT, class RelTy> 46805edf1c1Srobert int64_t RelocationScanner::computeMipsAddend(const RelTy &rel, RelExpr expr, 46905edf1c1Srobert bool isLocal) const { 470ece8a530Spatrick if (expr == R_MIPS_GOTREL && isLocal) 47105edf1c1Srobert return sec->getFile<ELFT>()->mipsGp0; 472ece8a530Spatrick 473ece8a530Spatrick // The ABI says that the paired relocation is used only for REL. 474ece8a530Spatrick // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 475ece8a530Spatrick if (RelTy::IsRela) 476ece8a530Spatrick return 0; 477ece8a530Spatrick 478ece8a530Spatrick RelType type = rel.getType(config->isMips64EL); 479ece8a530Spatrick uint32_t pairTy = getMipsPairType(type, isLocal); 480ece8a530Spatrick if (pairTy == R_MIPS_NONE) 481ece8a530Spatrick return 0; 482ece8a530Spatrick 48305edf1c1Srobert const uint8_t *buf = sec->content().data(); 484ece8a530Spatrick uint32_t symIndex = rel.getSymbol(config->isMips64EL); 485ece8a530Spatrick 486ece8a530Spatrick // To make things worse, paired relocations might not be contiguous in 487ece8a530Spatrick // the relocation table, so we need to do linear search. *sigh* 48805edf1c1Srobert for (const RelTy *ri = &rel; ri != static_cast<const RelTy *>(end); ++ri) 489ece8a530Spatrick if (ri->getType(config->isMips64EL) == pairTy && 490ece8a530Spatrick ri->getSymbol(config->isMips64EL) == symIndex) 491ece8a530Spatrick return target->getImplicitAddend(buf + ri->r_offset, pairTy); 492ece8a530Spatrick 493ece8a530Spatrick warn("can't find matching " + toString(pairTy) + " relocation for " + 494ece8a530Spatrick toString(type)); 495ece8a530Spatrick return 0; 496ece8a530Spatrick } 497ece8a530Spatrick 498ece8a530Spatrick // Custom error message if Sym is defined in a discarded section. 499ece8a530Spatrick template <class ELFT> 500ece8a530Spatrick static std::string maybeReportDiscarded(Undefined &sym) { 501ece8a530Spatrick auto *file = dyn_cast_or_null<ObjFile<ELFT>>(sym.file); 502ece8a530Spatrick if (!file || !sym.discardedSecIdx || 503ece8a530Spatrick file->getSections()[sym.discardedSecIdx] != &InputSection::discarded) 504ece8a530Spatrick return ""; 50505edf1c1Srobert ArrayRef<typename ELFT::Shdr> objSections = 50605edf1c1Srobert file->template getELFShdrs<ELFT>(); 507ece8a530Spatrick 508ece8a530Spatrick std::string msg; 509ece8a530Spatrick if (sym.type == ELF::STT_SECTION) { 510ece8a530Spatrick msg = "relocation refers to a discarded section: "; 511ece8a530Spatrick msg += CHECK( 512a0747c9fSpatrick file->getObj().getSectionName(objSections[sym.discardedSecIdx]), file); 513ece8a530Spatrick } else { 514ece8a530Spatrick msg = "relocation refers to a symbol in a discarded section: " + 515ece8a530Spatrick toString(sym); 516ece8a530Spatrick } 517ece8a530Spatrick msg += "\n>>> defined in " + toString(file); 518ece8a530Spatrick 519ece8a530Spatrick Elf_Shdr_Impl<ELFT> elfSec = objSections[sym.discardedSecIdx - 1]; 520ece8a530Spatrick if (elfSec.sh_type != SHT_GROUP) 521ece8a530Spatrick return msg; 522ece8a530Spatrick 523ece8a530Spatrick // If the discarded section is a COMDAT. 524ece8a530Spatrick StringRef signature = file->getShtGroupSignature(objSections, elfSec); 525ece8a530Spatrick if (const InputFile *prevailing = 52605edf1c1Srobert symtab.comdatGroups.lookup(CachedHashStringRef(signature))) { 527ece8a530Spatrick msg += "\n>>> section group signature: " + signature.str() + 528ece8a530Spatrick "\n>>> prevailing definition is in " + toString(prevailing); 52905edf1c1Srobert if (sym.nonPrevailing) { 53005edf1c1Srobert msg += "\n>>> or the symbol in the prevailing group had STB_WEAK " 53105edf1c1Srobert "binding and the symbol in a non-prevailing group had STB_GLOBAL " 53205edf1c1Srobert "binding. Mixing groups with STB_WEAK and STB_GLOBAL binding " 53305edf1c1Srobert "signature is not supported"; 53405edf1c1Srobert } 53505edf1c1Srobert } 536ece8a530Spatrick return msg; 537ece8a530Spatrick } 538ece8a530Spatrick 53905edf1c1Srobert namespace { 540ece8a530Spatrick // Undefined diagnostics are collected in a vector and emitted once all of 541ece8a530Spatrick // them are known, so that some postprocessing on the list of undefined symbols 542ece8a530Spatrick // can happen before lld emits diagnostics. 543ece8a530Spatrick struct UndefinedDiag { 54405edf1c1Srobert Undefined *sym; 545ece8a530Spatrick struct Loc { 546ece8a530Spatrick InputSectionBase *sec; 547ece8a530Spatrick uint64_t offset; 548ece8a530Spatrick }; 549ece8a530Spatrick std::vector<Loc> locs; 550ece8a530Spatrick bool isWarning; 551ece8a530Spatrick }; 552ece8a530Spatrick 55305edf1c1Srobert std::vector<UndefinedDiag> undefs; 55405edf1c1Srobert std::mutex relocMutex; 55505edf1c1Srobert } 556ece8a530Spatrick 557ece8a530Spatrick // Check whether the definition name def is a mangled function name that matches 558ece8a530Spatrick // the reference name ref. 559ece8a530Spatrick static bool canSuggestExternCForCXX(StringRef ref, StringRef def) { 560ece8a530Spatrick llvm::ItaniumPartialDemangler d; 561ece8a530Spatrick std::string name = def.str(); 562ece8a530Spatrick if (d.partialDemangle(name.c_str())) 563ece8a530Spatrick return false; 564ece8a530Spatrick char *buf = d.getFunctionName(nullptr, nullptr); 565ece8a530Spatrick if (!buf) 566ece8a530Spatrick return false; 567ece8a530Spatrick bool ret = ref == buf; 568ece8a530Spatrick free(buf); 569ece8a530Spatrick return ret; 570ece8a530Spatrick } 571ece8a530Spatrick 572ece8a530Spatrick // Suggest an alternative spelling of an "undefined symbol" diagnostic. Returns 573ece8a530Spatrick // the suggested symbol, which is either in the symbol table, or in the same 574ece8a530Spatrick // file of sym. 575ece8a530Spatrick static const Symbol *getAlternativeSpelling(const Undefined &sym, 576ece8a530Spatrick std::string &pre_hint, 577ece8a530Spatrick std::string &post_hint) { 578ece8a530Spatrick DenseMap<StringRef, const Symbol *> map; 57905edf1c1Srobert if (sym.file && sym.file->kind() == InputFile::ObjKind) { 58005edf1c1Srobert auto *file = cast<ELFFileBase>(sym.file); 581ece8a530Spatrick // If sym is a symbol defined in a discarded section, maybeReportDiscarded() 582ece8a530Spatrick // will give an error. Don't suggest an alternative spelling. 583ece8a530Spatrick if (file && sym.discardedSecIdx != 0 && 584ece8a530Spatrick file->getSections()[sym.discardedSecIdx] == &InputSection::discarded) 585ece8a530Spatrick return nullptr; 586ece8a530Spatrick 587ece8a530Spatrick // Build a map of local defined symbols. 588ece8a530Spatrick for (const Symbol *s : sym.file->getSymbols()) 589a0747c9fSpatrick if (s->isLocal() && s->isDefined() && !s->getName().empty()) 590ece8a530Spatrick map.try_emplace(s->getName(), s); 591ece8a530Spatrick } 592ece8a530Spatrick 593ece8a530Spatrick auto suggest = [&](StringRef newName) -> const Symbol * { 594ece8a530Spatrick // If defined locally. 595ece8a530Spatrick if (const Symbol *s = map.lookup(newName)) 596ece8a530Spatrick return s; 597ece8a530Spatrick 598ece8a530Spatrick // If in the symbol table and not undefined. 59905edf1c1Srobert if (const Symbol *s = symtab.find(newName)) 600ece8a530Spatrick if (!s->isUndefined()) 601ece8a530Spatrick return s; 602ece8a530Spatrick 603ece8a530Spatrick return nullptr; 604ece8a530Spatrick }; 605ece8a530Spatrick 606ece8a530Spatrick // This loop enumerates all strings of Levenshtein distance 1 as typo 607ece8a530Spatrick // correction candidates and suggests the one that exists as a non-undefined 608ece8a530Spatrick // symbol. 609ece8a530Spatrick StringRef name = sym.getName(); 610ece8a530Spatrick for (size_t i = 0, e = name.size(); i != e + 1; ++i) { 611ece8a530Spatrick // Insert a character before name[i]. 612ece8a530Spatrick std::string newName = (name.substr(0, i) + "0" + name.substr(i)).str(); 613ece8a530Spatrick for (char c = '0'; c <= 'z'; ++c) { 614ece8a530Spatrick newName[i] = c; 615ece8a530Spatrick if (const Symbol *s = suggest(newName)) 616ece8a530Spatrick return s; 617ece8a530Spatrick } 618ece8a530Spatrick if (i == e) 619ece8a530Spatrick break; 620ece8a530Spatrick 621ece8a530Spatrick // Substitute name[i]. 622adae0cfdSpatrick newName = std::string(name); 623ece8a530Spatrick for (char c = '0'; c <= 'z'; ++c) { 624ece8a530Spatrick newName[i] = c; 625ece8a530Spatrick if (const Symbol *s = suggest(newName)) 626ece8a530Spatrick return s; 627ece8a530Spatrick } 628ece8a530Spatrick 629ece8a530Spatrick // Transpose name[i] and name[i+1]. This is of edit distance 2 but it is 630ece8a530Spatrick // common. 631ece8a530Spatrick if (i + 1 < e) { 632ece8a530Spatrick newName[i] = name[i + 1]; 633ece8a530Spatrick newName[i + 1] = name[i]; 634ece8a530Spatrick if (const Symbol *s = suggest(newName)) 635ece8a530Spatrick return s; 636ece8a530Spatrick } 637ece8a530Spatrick 638ece8a530Spatrick // Delete name[i]. 639ece8a530Spatrick newName = (name.substr(0, i) + name.substr(i + 1)).str(); 640ece8a530Spatrick if (const Symbol *s = suggest(newName)) 641ece8a530Spatrick return s; 642ece8a530Spatrick } 643ece8a530Spatrick 644ece8a530Spatrick // Case mismatch, e.g. Foo vs FOO. 645ece8a530Spatrick for (auto &it : map) 646a0747c9fSpatrick if (name.equals_insensitive(it.first)) 647ece8a530Spatrick return it.second; 64805edf1c1Srobert for (Symbol *sym : symtab.getSymbols()) 649a0747c9fSpatrick if (!sym->isUndefined() && name.equals_insensitive(sym->getName())) 650ece8a530Spatrick return sym; 651ece8a530Spatrick 652ece8a530Spatrick // The reference may be a mangled name while the definition is not. Suggest a 653ece8a530Spatrick // missing extern "C". 654ece8a530Spatrick if (name.startswith("_Z")) { 655ece8a530Spatrick std::string buf = name.str(); 656ece8a530Spatrick llvm::ItaniumPartialDemangler d; 657ece8a530Spatrick if (!d.partialDemangle(buf.c_str())) 658ece8a530Spatrick if (char *buf = d.getFunctionName(nullptr, nullptr)) { 659ece8a530Spatrick const Symbol *s = suggest(buf); 660ece8a530Spatrick free(buf); 661ece8a530Spatrick if (s) { 662ece8a530Spatrick pre_hint = ": extern \"C\" "; 663ece8a530Spatrick return s; 664ece8a530Spatrick } 665ece8a530Spatrick } 666ece8a530Spatrick } else { 667ece8a530Spatrick const Symbol *s = nullptr; 668ece8a530Spatrick for (auto &it : map) 669ece8a530Spatrick if (canSuggestExternCForCXX(name, it.first)) { 670ece8a530Spatrick s = it.second; 671ece8a530Spatrick break; 672ece8a530Spatrick } 673ece8a530Spatrick if (!s) 67405edf1c1Srobert for (Symbol *sym : symtab.getSymbols()) 675ece8a530Spatrick if (canSuggestExternCForCXX(name, sym->getName())) { 676ece8a530Spatrick s = sym; 677ece8a530Spatrick break; 678ece8a530Spatrick } 679ece8a530Spatrick if (s) { 680ece8a530Spatrick pre_hint = " to declare "; 681ece8a530Spatrick post_hint = " as extern \"C\"?"; 682ece8a530Spatrick return s; 683ece8a530Spatrick } 684ece8a530Spatrick } 685ece8a530Spatrick 686ece8a530Spatrick return nullptr; 687ece8a530Spatrick } 688ece8a530Spatrick 689ece8a530Spatrick static void reportUndefinedSymbol(const UndefinedDiag &undef, 690ece8a530Spatrick bool correctSpelling) { 69105edf1c1Srobert Undefined &sym = *undef.sym; 692ece8a530Spatrick 693ece8a530Spatrick auto visibility = [&]() -> std::string { 69405edf1c1Srobert switch (sym.visibility()) { 695ece8a530Spatrick case STV_INTERNAL: 696ece8a530Spatrick return "internal "; 697ece8a530Spatrick case STV_HIDDEN: 698ece8a530Spatrick return "hidden "; 699ece8a530Spatrick case STV_PROTECTED: 700ece8a530Spatrick return "protected "; 701ece8a530Spatrick default: 702ece8a530Spatrick return ""; 703ece8a530Spatrick } 704ece8a530Spatrick }; 705ece8a530Spatrick 70605edf1c1Srobert std::string msg; 70705edf1c1Srobert switch (config->ekind) { 70805edf1c1Srobert case ELF32LEKind: 70905edf1c1Srobert msg = maybeReportDiscarded<ELF32LE>(sym); 71005edf1c1Srobert break; 71105edf1c1Srobert case ELF32BEKind: 71205edf1c1Srobert msg = maybeReportDiscarded<ELF32BE>(sym); 71305edf1c1Srobert break; 71405edf1c1Srobert case ELF64LEKind: 71505edf1c1Srobert msg = maybeReportDiscarded<ELF64LE>(sym); 71605edf1c1Srobert break; 71705edf1c1Srobert case ELF64BEKind: 71805edf1c1Srobert msg = maybeReportDiscarded<ELF64BE>(sym); 71905edf1c1Srobert break; 72005edf1c1Srobert default: 72105edf1c1Srobert llvm_unreachable(""); 72205edf1c1Srobert } 723ece8a530Spatrick if (msg.empty()) 724ece8a530Spatrick msg = "undefined " + visibility() + "symbol: " + toString(sym); 725ece8a530Spatrick 726adae0cfdSpatrick const size_t maxUndefReferences = 3; 727ece8a530Spatrick size_t i = 0; 728ece8a530Spatrick for (UndefinedDiag::Loc l : undef.locs) { 729ece8a530Spatrick if (i >= maxUndefReferences) 730ece8a530Spatrick break; 731ece8a530Spatrick InputSectionBase &sec = *l.sec; 732ece8a530Spatrick uint64_t offset = l.offset; 733ece8a530Spatrick 734ece8a530Spatrick msg += "\n>>> referenced by "; 735ece8a530Spatrick std::string src = sec.getSrcMsg(sym, offset); 736ece8a530Spatrick if (!src.empty()) 737ece8a530Spatrick msg += src + "\n>>> "; 738ece8a530Spatrick msg += sec.getObjMsg(offset); 739ece8a530Spatrick i++; 740ece8a530Spatrick } 741ece8a530Spatrick 742ece8a530Spatrick if (i < undef.locs.size()) 743ece8a530Spatrick msg += ("\n>>> referenced " + Twine(undef.locs.size() - i) + " more times") 744ece8a530Spatrick .str(); 745ece8a530Spatrick 746ece8a530Spatrick if (correctSpelling) { 747ece8a530Spatrick std::string pre_hint = ": ", post_hint; 74805edf1c1Srobert if (const Symbol *corrected = 74905edf1c1Srobert getAlternativeSpelling(sym, pre_hint, post_hint)) { 750ece8a530Spatrick msg += "\n>>> did you mean" + pre_hint + toString(*corrected) + post_hint; 751ece8a530Spatrick if (corrected->file) 752ece8a530Spatrick msg += "\n>>> defined in: " + toString(corrected->file); 753ece8a530Spatrick } 754ece8a530Spatrick } 755ece8a530Spatrick 756ece8a530Spatrick if (sym.getName().startswith("_ZTV")) 757adae0cfdSpatrick msg += 758adae0cfdSpatrick "\n>>> the vtable symbol may be undefined because the class is missing " 759ece8a530Spatrick "its key function (see https://lld.llvm.org/missingkeyfunction)"; 76005edf1c1Srobert if (config->gcSections && config->zStartStopGC && 76105edf1c1Srobert sym.getName().startswith("__start_")) { 76205edf1c1Srobert msg += "\n>>> the encapsulation symbol needs to be retained under " 76305edf1c1Srobert "--gc-sections properly; consider -z nostart-stop-gc " 76405edf1c1Srobert "(see https://lld.llvm.org/ELF/start-stop-gc)"; 76505edf1c1Srobert } 766ece8a530Spatrick 767ece8a530Spatrick if (undef.isWarning) 768ece8a530Spatrick warn(msg); 769ece8a530Spatrick else 770a0747c9fSpatrick error(msg, ErrorTag::SymbolNotFound, {sym.getName()}); 771ece8a530Spatrick } 772ece8a530Spatrick 77305edf1c1Srobert void elf::reportUndefinedSymbols() { 774ece8a530Spatrick // Find the first "undefined symbol" diagnostic for each diagnostic, and 775ece8a530Spatrick // collect all "referenced from" lines at the first diagnostic. 776ece8a530Spatrick DenseMap<Symbol *, UndefinedDiag *> firstRef; 777ece8a530Spatrick for (UndefinedDiag &undef : undefs) { 778ece8a530Spatrick assert(undef.locs.size() == 1); 779ece8a530Spatrick if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) { 780ece8a530Spatrick canon->locs.push_back(undef.locs[0]); 781ece8a530Spatrick undef.locs.clear(); 782ece8a530Spatrick } else 783ece8a530Spatrick firstRef[undef.sym] = &undef; 784ece8a530Spatrick } 785ece8a530Spatrick 786ece8a530Spatrick // Enable spell corrector for the first 2 diagnostics. 78705edf1c1Srobert for (const auto &[i, undef] : llvm::enumerate(undefs)) 78805edf1c1Srobert if (!undef.locs.empty()) 78905edf1c1Srobert reportUndefinedSymbol(undef, i < 2); 790ece8a530Spatrick undefs.clear(); 791ece8a530Spatrick } 792ece8a530Spatrick 7937c5ea754Srobert static void reportGNUWarning(Symbol &sym, InputSectionBase &sec, 7947c5ea754Srobert uint64_t offset) { 795e8ae9400Srobert std::lock_guard<std::mutex> lock(relocMutex); 7967c5ea754Srobert if (sym.gwarn) { 7977c5ea754Srobert StringRef gnuWarning = gnuWarnings.lookup(sym.getName()); 7987c5ea754Srobert // report first occurance only 7997c5ea754Srobert sym.gwarn = false; 8007c5ea754Srobert if (!gnuWarning.empty()) 801*2df0ffa8Santon warn(sec.getSrcMsg(sym, offset) + "(" + sec.getObjMsg(offset) + 8027c5ea754Srobert "): warning: " + gnuWarning); 8037c5ea754Srobert } 8047c5ea754Srobert } 8057c5ea754Srobert 806ece8a530Spatrick // Report an undefined symbol if necessary. 807ece8a530Spatrick // Returns true if the undefined symbol will produce an error message. 80805edf1c1Srobert static bool maybeReportUndefined(Undefined &sym, InputSectionBase &sec, 809ece8a530Spatrick uint64_t offset) { 81005edf1c1Srobert std::lock_guard<std::mutex> lock(relocMutex); 811a0747c9fSpatrick // If versioned, issue an error (even if the symbol is weak) because we don't 812a0747c9fSpatrick // know the defining filename which is required to construct a Verneed entry. 81305edf1c1Srobert if (sym.hasVersionSuffix) { 814a0747c9fSpatrick undefs.push_back({&sym, {{&sec, offset}}, false}); 815a0747c9fSpatrick return true; 816a0747c9fSpatrick } 817a0747c9fSpatrick if (sym.isWeak()) 818ece8a530Spatrick return false; 819ece8a530Spatrick 82005edf1c1Srobert bool canBeExternal = !sym.isLocal() && sym.visibility() == STV_DEFAULT; 821ece8a530Spatrick if (config->unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal) 822ece8a530Spatrick return false; 823ece8a530Spatrick 824ece8a530Spatrick // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc 825ece8a530Spatrick // which references a switch table in a discarded .rodata/.text section. The 826ece8a530Spatrick // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF 827ece8a530Spatrick // spec says references from outside the group to a STB_LOCAL symbol are not 828ece8a530Spatrick // allowed. Work around the bug. 8293bed555dSkettenis // 8303bed555dSkettenis // PPC32 .got2 is similar but cannot be fixed. Multiple .got2 is infeasible 8313bed555dSkettenis // because .LC0-.LTOC is not representable if the two labels are in different 8323bed555dSkettenis // .got2 83305edf1c1Srobert if (sym.discardedSecIdx != 0 && (sec.name == ".got2" || sec.name == ".toc")) 834ece8a530Spatrick return false; 835ece8a530Spatrick 8365e1a4d20Sjca #ifdef __OpenBSD__ 8375e1a4d20Sjca // GCC (at least 8 and 11) can produce a ".gcc_except_table" with relocations 8385e1a4d20Sjca // to discarded sections on riscv64 8395e1a4d20Sjca if (sym.discardedSecIdx != 0 && sec.name == ".gcc_except_table") 8405e1a4d20Sjca return false; 8415e1a4d20Sjca #endif 8425e1a4d20Sjca 843ece8a530Spatrick bool isWarning = 844ece8a530Spatrick (config->unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) || 845ece8a530Spatrick config->noinhibitExec; 846ece8a530Spatrick undefs.push_back({&sym, {{&sec, offset}}, isWarning}); 847ece8a530Spatrick return !isWarning; 848ece8a530Spatrick } 849ece8a530Spatrick 850ece8a530Spatrick // MIPS N32 ABI treats series of successive relocations with the same offset 851ece8a530Spatrick // as a single relocation. The similar approach used by N64 ABI, but this ABI 852ece8a530Spatrick // packs all relocations into the single relocation record. Here we emulate 853ece8a530Spatrick // this for the N32 ABI. Iterate over relocation with the same offset and put 854ece8a530Spatrick // theirs types into the single bit-set. 85505edf1c1Srobert template <class RelTy> 85605edf1c1Srobert RelType RelocationScanner::getMipsN32RelType(RelTy *&rel) const { 857ece8a530Spatrick RelType type = 0; 858ece8a530Spatrick uint64_t offset = rel->r_offset; 859ece8a530Spatrick 860ece8a530Spatrick int n = 0; 86105edf1c1Srobert while (rel != static_cast<const RelTy *>(end) && rel->r_offset == offset) 862ece8a530Spatrick type |= (rel++)->getType(config->isMips64EL) << (8 * n++); 863ece8a530Spatrick return type; 864ece8a530Spatrick } 865ece8a530Spatrick 86605edf1c1Srobert template <bool shard = false> 86705edf1c1Srobert static void addRelativeReloc(InputSectionBase &isec, uint64_t offsetInSec, 868a0747c9fSpatrick Symbol &sym, int64_t addend, RelExpr expr, 869ece8a530Spatrick RelType type) { 87005edf1c1Srobert Partition &part = isec.getPartition(); 871ece8a530Spatrick 872ece8a530Spatrick // Add a relative relocation. If relrDyn section is enabled, and the 873ece8a530Spatrick // relocation offset is guaranteed to be even, add the relocation to 874ece8a530Spatrick // the relrDyn section, otherwise add it to the relaDyn section. 875ece8a530Spatrick // relrDyn sections don't support odd offsets. Also, relrDyn sections 876ece8a530Spatrick // don't store the addend values, so we must write it to the relocated 877ece8a530Spatrick // address. 87805edf1c1Srobert if (part.relrDyn && isec.addralign >= 2 && offsetInSec % 2 == 0) { 87905edf1c1Srobert isec.addReloc({expr, type, offsetInSec, addend, &sym}); 88005edf1c1Srobert if (shard) 88105edf1c1Srobert part.relrDyn->relocsVec[parallel::getThreadIndex()].push_back( 88205edf1c1Srobert {&isec, offsetInSec}); 88305edf1c1Srobert else 88405edf1c1Srobert part.relrDyn->relocs.push_back({&isec, offsetInSec}); 885ece8a530Spatrick return; 886ece8a530Spatrick } 88705edf1c1Srobert part.relaDyn->addRelativeReloc<shard>(target->relativeRel, isec, offsetInSec, 88805edf1c1Srobert sym, addend, type, expr); 889ece8a530Spatrick } 890ece8a530Spatrick 891ece8a530Spatrick template <class PltSection, class GotPltSection> 89205edf1c1Srobert static void addPltEntry(PltSection &plt, GotPltSection &gotPlt, 89305edf1c1Srobert RelocationBaseSection &rel, RelType type, Symbol &sym) { 89405edf1c1Srobert plt.addEntry(sym); 89505edf1c1Srobert gotPlt.addEntry(sym); 89605edf1c1Srobert rel.addReloc({type, &gotPlt, sym.getGotPltOffset(), 897a0747c9fSpatrick sym.isPreemptible ? DynamicReloc::AgainstSymbol 898a0747c9fSpatrick : DynamicReloc::AddendOnlyWithTargetVA, 899a0747c9fSpatrick sym, 0, R_ABS}); 900ece8a530Spatrick } 901ece8a530Spatrick 902ece8a530Spatrick static void addGotEntry(Symbol &sym) { 903ece8a530Spatrick in.got->addEntry(sym); 904ece8a530Spatrick uint64_t off = sym.getGotOffset(); 905ece8a530Spatrick 90605edf1c1Srobert // If preemptible, emit a GLOB_DAT relocation. 90705edf1c1Srobert if (sym.isPreemptible) { 90805edf1c1Srobert mainPart->relaDyn->addReloc({target->gotRel, in.got.get(), off, 90905edf1c1Srobert DynamicReloc::AgainstSymbol, sym, 0, R_ABS}); 910ece8a530Spatrick return; 911ece8a530Spatrick } 912ece8a530Spatrick 91305edf1c1Srobert // Otherwise, the value is either a link-time constant or the load base 91405edf1c1Srobert // plus a constant. 91505edf1c1Srobert if (!config->isPic || isAbsolute(sym)) 91605edf1c1Srobert in.got->addConstant({R_ABS, target->symbolicRel, off, 0, &sym}); 91705edf1c1Srobert else 91805edf1c1Srobert addRelativeReloc(*in.got, off, sym, 0, R_ABS, target->symbolicRel); 91905edf1c1Srobert } 92005edf1c1Srobert 92105edf1c1Srobert static void addTpOffsetGotEntry(Symbol &sym) { 92205edf1c1Srobert in.got->addEntry(sym); 92305edf1c1Srobert uint64_t off = sym.getGotOffset(); 92405edf1c1Srobert if (!sym.isPreemptible && !config->isPic) { 92505edf1c1Srobert in.got->addConstant({R_TPREL, target->symbolicRel, off, 0, &sym}); 926ece8a530Spatrick return; 927ece8a530Spatrick } 928a0747c9fSpatrick mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( 92905edf1c1Srobert target->tlsGotRel, *in.got, off, sym, target->symbolicRel); 930ece8a530Spatrick } 931ece8a530Spatrick 932ece8a530Spatrick // Return true if we can define a symbol in the executable that 933ece8a530Spatrick // contains the value/function of a symbol defined in a shared 934ece8a530Spatrick // library. 935ece8a530Spatrick static bool canDefineSymbolInExecutable(Symbol &sym) { 936ece8a530Spatrick // If the symbol has default visibility the symbol defined in the 937ece8a530Spatrick // executable will preempt it. 938ece8a530Spatrick // Note that we want the visibility of the shared symbol itself, not 93905edf1c1Srobert // the visibility of the symbol in the output file we are producing. 94005edf1c1Srobert if (!sym.dsoProtected) 941ece8a530Spatrick return true; 942ece8a530Spatrick 943ece8a530Spatrick // If we are allowed to break address equality of functions, defining 944ece8a530Spatrick // a plt entry will allow the program to call the function in the 945ece8a530Spatrick // .so, but the .so and the executable will no agree on the address 946ece8a530Spatrick // of the function. Similar logic for objects. 947ece8a530Spatrick return ((sym.isFunc() && config->ignoreFunctionAddressEquality) || 948ece8a530Spatrick (sym.isObject() && config->ignoreDataAddressEquality)); 949ece8a530Spatrick } 950ece8a530Spatrick 95105edf1c1Srobert // Returns true if a given relocation can be computed at link-time. 95205edf1c1Srobert // This only handles relocation types expected in processAux. 95305edf1c1Srobert // 95405edf1c1Srobert // For instance, we know the offset from a relocation to its target at 95505edf1c1Srobert // link-time if the relocation is PC-relative and refers a 95605edf1c1Srobert // non-interposable function in the same executable. This function 95705edf1c1Srobert // will return true for such relocation. 95805edf1c1Srobert // 95905edf1c1Srobert // If this function returns false, that means we need to emit a 96005edf1c1Srobert // dynamic relocation so that the relocation will be fixed at load-time. 96105edf1c1Srobert bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type, 96205edf1c1Srobert const Symbol &sym, 96305edf1c1Srobert uint64_t relOff) const { 96405edf1c1Srobert // These expressions always compute a constant 96505edf1c1Srobert if (oneof<R_GOTPLT, R_GOT_OFF, R_RELAX_HINT, R_MIPS_GOT_LOCAL_PAGE, 96605edf1c1Srobert R_MIPS_GOTREL, R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC, 96705edf1c1Srobert R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC, 96805edf1c1Srobert R_PLT_PC, R_PLT_GOTPLT, R_PPC32_PLTREL, R_PPC64_CALL_PLT, 96905edf1c1Srobert R_PPC64_RELAX_TOC, R_RISCV_ADD, R_AARCH64_GOT_PAGE>(e)) 97005edf1c1Srobert return true; 97105edf1c1Srobert 97205edf1c1Srobert // These never do, except if the entire file is position dependent or if 97305edf1c1Srobert // only the low bits are used. 97405edf1c1Srobert if (e == R_GOT || e == R_PLT) 97505edf1c1Srobert return target->usesOnlyLowPageBits(type) || !config->isPic; 97605edf1c1Srobert 97705edf1c1Srobert if (sym.isPreemptible) 97805edf1c1Srobert return false; 97905edf1c1Srobert if (!config->isPic) 98005edf1c1Srobert return true; 98105edf1c1Srobert 98205edf1c1Srobert // The size of a non preemptible symbol is a constant. 98305edf1c1Srobert if (e == R_SIZE) 98405edf1c1Srobert return true; 98505edf1c1Srobert 98605edf1c1Srobert // For the target and the relocation, we want to know if they are 98705edf1c1Srobert // absolute or relative. 98805edf1c1Srobert bool absVal = isAbsoluteValue(sym); 98905edf1c1Srobert bool relE = isRelExpr(e); 99005edf1c1Srobert if (absVal && !relE) 99105edf1c1Srobert return true; 99205edf1c1Srobert if (!absVal && relE) 99305edf1c1Srobert return true; 99405edf1c1Srobert if (!absVal && !relE) 99505edf1c1Srobert return target->usesOnlyLowPageBits(type); 99605edf1c1Srobert 99705edf1c1Srobert assert(absVal && relE); 99805edf1c1Srobert 99905edf1c1Srobert // Allow R_PLT_PC (optimized to R_PC here) to a hidden undefined weak symbol 100005edf1c1Srobert // in PIC mode. This is a little strange, but it allows us to link function 100105edf1c1Srobert // calls to such symbols (e.g. glibc/stdlib/exit.c:__run_exit_handlers). 100205edf1c1Srobert // Normally such a call will be guarded with a comparison, which will load a 100305edf1c1Srobert // zero from the GOT. 100405edf1c1Srobert if (sym.isUndefWeak()) 100505edf1c1Srobert return true; 100605edf1c1Srobert 100705edf1c1Srobert // We set the final symbols values for linker script defined symbols later. 100805edf1c1Srobert // They always can be computed as a link time constant. 100905edf1c1Srobert if (sym.scriptDefined) 101005edf1c1Srobert return true; 101105edf1c1Srobert 101205edf1c1Srobert error("relocation " + toString(type) + " cannot refer to absolute symbol: " + 101305edf1c1Srobert toString(sym) + getLocation(*sec, sym, relOff)); 101405edf1c1Srobert return true; 101505edf1c1Srobert } 101605edf1c1Srobert 1017ece8a530Spatrick // The reason we have to do this early scan is as follows 1018ece8a530Spatrick // * To mmap the output file, we need to know the size 1019ece8a530Spatrick // * For that, we need to know how many dynamic relocs we will have. 1020ece8a530Spatrick // It might be possible to avoid this by outputting the file with write: 1021ece8a530Spatrick // * Write the allocated output sections, computing addresses. 1022ece8a530Spatrick // * Apply relocations, recording which ones require a dynamic reloc. 1023ece8a530Spatrick // * Write the dynamic relocations. 1024ece8a530Spatrick // * Write the rest of the file. 1025ece8a530Spatrick // This would have some drawbacks. For example, we would only know if .rela.dyn 1026ece8a530Spatrick // is needed after applying relocations. If it is, it will go after rw and rx 1027ece8a530Spatrick // sections. Given that it is ro, we will need an extra PT_LOAD. This 1028ece8a530Spatrick // complicates things for the dynamic linker and means we would have to reserve 1029ece8a530Spatrick // space for the extra PT_LOAD even if we end up not using it. 103005edf1c1Srobert void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset, 103105edf1c1Srobert Symbol &sym, int64_t addend) const { 103205edf1c1Srobert // If non-ifunc non-preemptible, change PLT to direct call and optimize GOT 103305edf1c1Srobert // indirection. 103405edf1c1Srobert const bool isIfunc = sym.isGnuIFunc(); 103505edf1c1Srobert if (!sym.isPreemptible && (!isIfunc || config->zIfuncNoplt)) { 103605edf1c1Srobert if (expr != R_GOT_PC) { 103705edf1c1Srobert // The 0x8000 bit of r_addend of R_PPC_PLTREL24 is used to choose call 103805edf1c1Srobert // stub type. It should be ignored if optimized to R_PC. 103905edf1c1Srobert if (config->emachine == EM_PPC && expr == R_PPC32_PLTREL) 104005edf1c1Srobert addend &= ~0x8000; 104105edf1c1Srobert // R_HEX_GD_PLT_B22_PCREL (call a@GDPLT) is transformed into 104205edf1c1Srobert // call __tls_get_addr even if the symbol is non-preemptible. 104305edf1c1Srobert if (!(config->emachine == EM_HEXAGON && 104405edf1c1Srobert (type == R_HEX_GD_PLT_B22_PCREL || 104505edf1c1Srobert type == R_HEX_GD_PLT_B22_PCREL_X || 104605edf1c1Srobert type == R_HEX_GD_PLT_B32_PCREL_X))) 104705edf1c1Srobert expr = fromPlt(expr); 104805edf1c1Srobert } else if (!isAbsoluteValue(sym)) { 104905edf1c1Srobert expr = 105005edf1c1Srobert target->adjustGotPcExpr(type, addend, sec->content().data() + offset); 105105edf1c1Srobert } 105205edf1c1Srobert } 105305edf1c1Srobert 105405edf1c1Srobert // We were asked not to generate PLT entries for ifuncs. Instead, pass the 105505edf1c1Srobert // direct relocation on through. 105605edf1c1Srobert if (LLVM_UNLIKELY(isIfunc) && config->zIfuncNoplt) { 105705edf1c1Srobert std::lock_guard<std::mutex> lock(relocMutex); 105805edf1c1Srobert sym.exportDynamic = true; 105905edf1c1Srobert mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type); 106005edf1c1Srobert return; 106105edf1c1Srobert } 106205edf1c1Srobert 106305edf1c1Srobert if (needsGot(expr)) { 106405edf1c1Srobert if (config->emachine == EM_MIPS) { 106505edf1c1Srobert // MIPS ABI has special rules to process GOT entries and doesn't 106605edf1c1Srobert // require relocation entries for them. A special case is TLS 106705edf1c1Srobert // relocations. In that case dynamic loader applies dynamic 106805edf1c1Srobert // relocations to initialize TLS GOT entries. 106905edf1c1Srobert // See "Global Offset Table" in Chapter 5 in the following document 107005edf1c1Srobert // for detailed description: 107105edf1c1Srobert // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 107205edf1c1Srobert in.mipsGot->addEntry(*sec->file, sym, addend, expr); 107305edf1c1Srobert } else { 107405edf1c1Srobert sym.setFlags(NEEDS_GOT); 107505edf1c1Srobert } 107605edf1c1Srobert } else if (needsPlt(expr)) { 107705edf1c1Srobert sym.setFlags(NEEDS_PLT); 107805edf1c1Srobert } else if (LLVM_UNLIKELY(isIfunc)) { 107905edf1c1Srobert sym.setFlags(HAS_DIRECT_RELOC); 108005edf1c1Srobert } 108105edf1c1Srobert 1082ece8a530Spatrick // If the relocation is known to be a link-time constant, we know no dynamic 1083ece8a530Spatrick // relocation will be created, pass the control to relocateAlloc() or 1084ece8a530Spatrick // relocateNonAlloc() to resolve it. 1085ece8a530Spatrick // 1086a0747c9fSpatrick // The behavior of an undefined weak reference is implementation defined. For 1087a0747c9fSpatrick // non-link-time constants, we resolve relocations statically (let 1088a0747c9fSpatrick // relocate{,Non}Alloc() resolve them) for -no-pie and try producing dynamic 1089a0747c9fSpatrick // relocations for -pie and -shared. 1090a0747c9fSpatrick // 1091a0747c9fSpatrick // The general expectation of -no-pie static linking is that there is no 1092a0747c9fSpatrick // dynamic relocation (except IRELATIVE). Emitting dynamic relocations for 1093a0747c9fSpatrick // -shared matches the spirit of its -z undefs default. -pie has freedom on 1094a0747c9fSpatrick // choices, and we choose dynamic relocations to be consistent with the 1095a0747c9fSpatrick // handling of GOT-generating relocations. 109605edf1c1Srobert if (isStaticLinkTimeConstant(expr, type, sym, offset) || 1097a0747c9fSpatrick (!config->isPic && sym.isUndefWeak())) { 109805edf1c1Srobert sec->addReloc({expr, type, offset, addend, &sym}); 1099ece8a530Spatrick return; 1100ece8a530Spatrick } 1101ece8a530Spatrick 110205edf1c1Srobert // Use a simple -z notext rule that treats all sections except .eh_frame as 110305edf1c1Srobert // writable. GNU ld does not produce dynamic relocations in .eh_frame (and our 110405edf1c1Srobert // SectionBase::getOffset would incorrectly adjust the offset). 110505edf1c1Srobert // 110605edf1c1Srobert // For MIPS, we don't implement GNU ld's DW_EH_PE_absptr to DW_EH_PE_pcrel 110705edf1c1Srobert // conversion. We still emit a dynamic relocation. 110805edf1c1Srobert bool canWrite = (sec->flags & SHF_WRITE) || 110905edf1c1Srobert !(config->zText || 111005edf1c1Srobert (isa<EhInputSection>(sec) && config->emachine != EM_MIPS)); 1111ece8a530Spatrick if (canWrite) { 1112ece8a530Spatrick RelType rel = target->getDynRel(type); 1113ece8a530Spatrick if (expr == R_GOT || (rel == target->symbolicRel && !sym.isPreemptible)) { 111405edf1c1Srobert addRelativeReloc<true>(*sec, offset, sym, addend, expr, type); 1115ece8a530Spatrick return; 1116ece8a530Spatrick } else if (rel != 0) { 1117ece8a530Spatrick if (config->emachine == EM_MIPS && rel == target->symbolicRel) 1118ece8a530Spatrick rel = target->relativeRel; 111905edf1c1Srobert std::lock_guard<std::mutex> lock(relocMutex); 112005edf1c1Srobert sec->getPartition().relaDyn->addSymbolReloc(rel, *sec, offset, sym, 112105edf1c1Srobert addend, type); 1122ece8a530Spatrick 1123ece8a530Spatrick // MIPS ABI turns using of GOT and dynamic relocations inside out. 1124ece8a530Spatrick // While regular ABI uses dynamic relocations to fill up GOT entries 1125ece8a530Spatrick // MIPS ABI requires dynamic linker to fills up GOT entries using 1126ece8a530Spatrick // specially sorted dynamic symbol table. This affects even dynamic 1127ece8a530Spatrick // relocations against symbols which do not require GOT entries 1128ece8a530Spatrick // creation explicitly, i.e. do not have any GOT-relocations. So if 1129ece8a530Spatrick // a preemptible symbol has a dynamic relocation we anyway have 1130ece8a530Spatrick // to create a GOT entry for it. 1131ece8a530Spatrick // If a non-preemptible symbol has a dynamic relocation against it, 1132ece8a530Spatrick // dynamic linker takes it st_value, adds offset and writes down 1133ece8a530Spatrick // result of the dynamic relocation. In case of preemptible symbol 1134ece8a530Spatrick // dynamic linker performs symbol resolution, writes the symbol value 1135ece8a530Spatrick // to the GOT entry and reads the GOT entry when it needs to perform 1136ece8a530Spatrick // a dynamic relocation. 1137ece8a530Spatrick // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19 1138ece8a530Spatrick if (config->emachine == EM_MIPS) 113905edf1c1Srobert in.mipsGot->addEntry(*sec->file, sym, addend, expr); 1140ece8a530Spatrick return; 1141ece8a530Spatrick } 1142ece8a530Spatrick } 1143ece8a530Spatrick 1144ece8a530Spatrick // When producing an executable, we can perform copy relocations (for 1145ece8a530Spatrick // STT_OBJECT) and canonical PLT (for STT_FUNC). 1146ece8a530Spatrick if (!config->shared) { 1147ece8a530Spatrick if (!canDefineSymbolInExecutable(sym)) { 1148ece8a530Spatrick errorOrWarn("cannot preempt symbol: " + toString(sym) + 114905edf1c1Srobert getLocation(*sec, sym, offset)); 1150ece8a530Spatrick return; 1151ece8a530Spatrick } 1152ece8a530Spatrick 1153ece8a530Spatrick if (sym.isObject()) { 1154ece8a530Spatrick // Produce a copy relocation. 1155ece8a530Spatrick if (auto *ss = dyn_cast<SharedSymbol>(&sym)) { 1156ece8a530Spatrick if (!config->zCopyreloc) 1157ece8a530Spatrick error("unresolvable relocation " + toString(type) + 1158ece8a530Spatrick " against symbol '" + toString(*ss) + 1159ece8a530Spatrick "'; recompile with -fPIC or remove '-z nocopyreloc'" + 116005edf1c1Srobert getLocation(*sec, sym, offset)); 116105edf1c1Srobert sym.setFlags(NEEDS_COPY); 1162ece8a530Spatrick } 116305edf1c1Srobert sec->addReloc({expr, type, offset, addend, &sym}); 1164ece8a530Spatrick return; 1165ece8a530Spatrick } 1166ece8a530Spatrick 1167ece8a530Spatrick // This handles a non PIC program call to function in a shared library. In 1168ece8a530Spatrick // an ideal world, we could just report an error saying the relocation can 1169ece8a530Spatrick // overflow at runtime. In the real world with glibc, crt1.o has a 1170ece8a530Spatrick // R_X86_64_PC32 pointing to libc.so. 1171ece8a530Spatrick // 1172ece8a530Spatrick // The general idea on how to handle such cases is to create a PLT entry and 1173ece8a530Spatrick // use that as the function value. 1174ece8a530Spatrick // 1175ece8a530Spatrick // For the static linking part, we just return a plt expr and everything 1176ece8a530Spatrick // else will use the PLT entry as the address. 1177ece8a530Spatrick // 1178ece8a530Spatrick // The remaining problem is making sure pointer equality still works. We 1179ece8a530Spatrick // need the help of the dynamic linker for that. We let it know that we have 1180ece8a530Spatrick // a direct reference to a so symbol by creating an undefined symbol with a 1181ece8a530Spatrick // non zero st_value. Seeing that, the dynamic linker resolves the symbol to 1182ece8a530Spatrick // the value of the symbol we created. This is true even for got entries, so 1183ece8a530Spatrick // pointer equality is maintained. To avoid an infinite loop, the only entry 1184ece8a530Spatrick // that points to the real function is a dedicated got entry used by the 1185ece8a530Spatrick // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT, 1186ece8a530Spatrick // R_386_JMP_SLOT, etc). 1187ece8a530Spatrick 1188ece8a530Spatrick // For position independent executable on i386, the plt entry requires ebx 1189ece8a530Spatrick // to be set. This causes two problems: 1190ece8a530Spatrick // * If some code has a direct reference to a function, it was probably 1191ece8a530Spatrick // compiled without -fPIE/-fPIC and doesn't maintain ebx. 1192ece8a530Spatrick // * If a library definition gets preempted to the executable, it will have 1193ece8a530Spatrick // the wrong ebx value. 1194ece8a530Spatrick if (sym.isFunc()) { 1195ece8a530Spatrick if (config->pie && config->emachine == EM_386) 1196ece8a530Spatrick errorOrWarn("symbol '" + toString(sym) + 1197ece8a530Spatrick "' cannot be preempted; recompile with -fPIE" + 119805edf1c1Srobert getLocation(*sec, sym, offset)); 119905edf1c1Srobert sym.setFlags(NEEDS_COPY | NEEDS_PLT); 120005edf1c1Srobert sec->addReloc({expr, type, offset, addend, &sym}); 1201ece8a530Spatrick return; 1202ece8a530Spatrick } 1203ece8a530Spatrick } 1204ece8a530Spatrick 120505edf1c1Srobert errorOrWarn("relocation " + toString(type) + " cannot be used against " + 1206ece8a530Spatrick (sym.getName().empty() ? "local symbol" 120705edf1c1Srobert : "symbol '" + toString(sym) + "'") + 120805edf1c1Srobert "; recompile with -fPIC" + getLocation(*sec, sym, offset)); 120905edf1c1Srobert } 121005edf1c1Srobert 121105edf1c1Srobert // This function is similar to the `handleTlsRelocation`. MIPS does not 121205edf1c1Srobert // support any relaxations for TLS relocations so by factoring out MIPS 121305edf1c1Srobert // handling in to the separate function we can simplify the code and do not 121405edf1c1Srobert // pollute other `handleTlsRelocation` by MIPS `ifs` statements. 121505edf1c1Srobert // Mips has a custom MipsGotSection that handles the writing of GOT entries 121605edf1c1Srobert // without dynamic relocations. 121705edf1c1Srobert static unsigned handleMipsTlsRelocation(RelType type, Symbol &sym, 121805edf1c1Srobert InputSectionBase &c, uint64_t offset, 121905edf1c1Srobert int64_t addend, RelExpr expr) { 122005edf1c1Srobert if (expr == R_MIPS_TLSLD) { 122105edf1c1Srobert in.mipsGot->addTlsIndex(*c.file); 122205edf1c1Srobert c.addReloc({expr, type, offset, addend, &sym}); 122305edf1c1Srobert return 1; 122405edf1c1Srobert } 122505edf1c1Srobert if (expr == R_MIPS_TLSGD) { 122605edf1c1Srobert in.mipsGot->addDynTlsEntry(*c.file, sym); 122705edf1c1Srobert c.addReloc({expr, type, offset, addend, &sym}); 122805edf1c1Srobert return 1; 122905edf1c1Srobert } 123005edf1c1Srobert return 0; 123105edf1c1Srobert } 123205edf1c1Srobert 123305edf1c1Srobert // Notes about General Dynamic and Local Dynamic TLS models below. They may 123405edf1c1Srobert // require the generation of a pair of GOT entries that have associated dynamic 123505edf1c1Srobert // relocations. The pair of GOT entries created are of the form GOT[e0] Module 123605edf1c1Srobert // Index (Used to find pointer to TLS block at run-time) GOT[e1] Offset of 123705edf1c1Srobert // symbol in TLS block. 123805edf1c1Srobert // 123905edf1c1Srobert // Returns the number of relocations processed. 124005edf1c1Srobert static unsigned handleTlsRelocation(RelType type, Symbol &sym, 124105edf1c1Srobert InputSectionBase &c, uint64_t offset, 124205edf1c1Srobert int64_t addend, RelExpr expr) { 124305edf1c1Srobert if (expr == R_TPREL || expr == R_TPREL_NEG) { 124405edf1c1Srobert if (config->shared) { 124505edf1c1Srobert errorOrWarn("relocation " + toString(type) + " against " + toString(sym) + 124605edf1c1Srobert " cannot be used with -shared" + getLocation(c, sym, offset)); 124705edf1c1Srobert return 1; 124805edf1c1Srobert } 124905edf1c1Srobert return 0; 125005edf1c1Srobert } 125105edf1c1Srobert 125205edf1c1Srobert if (config->emachine == EM_MIPS) 125305edf1c1Srobert return handleMipsTlsRelocation(type, sym, c, offset, addend, expr); 125405edf1c1Srobert 125505edf1c1Srobert if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC, 125605edf1c1Srobert R_TLSDESC_GOTPLT>(expr) && 125705edf1c1Srobert config->shared) { 125805edf1c1Srobert if (expr != R_TLSDESC_CALL) { 125905edf1c1Srobert sym.setFlags(NEEDS_TLSDESC); 126005edf1c1Srobert c.addReloc({expr, type, offset, addend, &sym}); 126105edf1c1Srobert } 126205edf1c1Srobert return 1; 126305edf1c1Srobert } 126405edf1c1Srobert 126505edf1c1Srobert // ARM, Hexagon and RISC-V do not support GD/LD to IE/LE relaxation. For 126605edf1c1Srobert // PPC64, if the file has missing R_PPC64_TLSGD/R_PPC64_TLSLD, disable 126705edf1c1Srobert // relaxation as well. 126805edf1c1Srobert bool toExecRelax = !config->shared && config->emachine != EM_ARM && 126905edf1c1Srobert config->emachine != EM_HEXAGON && 127005edf1c1Srobert config->emachine != EM_RISCV && 127105edf1c1Srobert !c.file->ppc64DisableTLSRelax; 127205edf1c1Srobert 127305edf1c1Srobert // If we are producing an executable and the symbol is non-preemptable, it 127405edf1c1Srobert // must be defined and the code sequence can be relaxed to use Local-Exec. 127505edf1c1Srobert // 127605edf1c1Srobert // ARM and RISC-V do not support any relaxations for TLS relocations, however, 127705edf1c1Srobert // we can omit the DTPMOD dynamic relocations and resolve them at link time 127805edf1c1Srobert // because them are always 1. This may be necessary for static linking as 127905edf1c1Srobert // DTPMOD may not be expected at load time. 128005edf1c1Srobert bool isLocalInExecutable = !sym.isPreemptible && !config->shared; 128105edf1c1Srobert 128205edf1c1Srobert // Local Dynamic is for access to module local TLS variables, while still 128305edf1c1Srobert // being suitable for being dynamically loaded via dlopen. GOT[e0] is the 128405edf1c1Srobert // module index, with a special value of 0 for the current module. GOT[e1] is 128505edf1c1Srobert // unused. There only needs to be one module index entry. 128605edf1c1Srobert if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>( 128705edf1c1Srobert expr)) { 128805edf1c1Srobert // Local-Dynamic relocs can be relaxed to Local-Exec. 128905edf1c1Srobert if (toExecRelax) { 129005edf1c1Srobert c.addReloc({target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE), type, 129105edf1c1Srobert offset, addend, &sym}); 129205edf1c1Srobert return target->getTlsGdRelaxSkip(type); 129305edf1c1Srobert } 129405edf1c1Srobert if (expr == R_TLSLD_HINT) 129505edf1c1Srobert return 1; 129605edf1c1Srobert ctx.needsTlsLd.store(true, std::memory_order_relaxed); 129705edf1c1Srobert c.addReloc({expr, type, offset, addend, &sym}); 129805edf1c1Srobert return 1; 129905edf1c1Srobert } 130005edf1c1Srobert 130105edf1c1Srobert // Local-Dynamic relocs can be relaxed to Local-Exec. 130205edf1c1Srobert if (expr == R_DTPREL) { 130305edf1c1Srobert if (toExecRelax) 130405edf1c1Srobert expr = target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE); 130505edf1c1Srobert c.addReloc({expr, type, offset, addend, &sym}); 130605edf1c1Srobert return 1; 130705edf1c1Srobert } 130805edf1c1Srobert 130905edf1c1Srobert // Local-Dynamic sequence where offset of tls variable relative to dynamic 131005edf1c1Srobert // thread pointer is stored in the got. This cannot be relaxed to Local-Exec. 131105edf1c1Srobert if (expr == R_TLSLD_GOT_OFF) { 131205edf1c1Srobert sym.setFlags(NEEDS_GOT_DTPREL); 131305edf1c1Srobert c.addReloc({expr, type, offset, addend, &sym}); 131405edf1c1Srobert return 1; 131505edf1c1Srobert } 131605edf1c1Srobert 131705edf1c1Srobert if (oneof<R_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC, 131805edf1c1Srobert R_TLSDESC_GOTPLT, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC>(expr)) { 131905edf1c1Srobert if (!toExecRelax) { 132005edf1c1Srobert sym.setFlags(NEEDS_TLSGD); 132105edf1c1Srobert c.addReloc({expr, type, offset, addend, &sym}); 132205edf1c1Srobert return 1; 132305edf1c1Srobert } 132405edf1c1Srobert 132505edf1c1Srobert // Global-Dynamic relocs can be relaxed to Initial-Exec or Local-Exec 132605edf1c1Srobert // depending on the symbol being locally defined or not. 132705edf1c1Srobert if (sym.isPreemptible) { 132805edf1c1Srobert sym.setFlags(NEEDS_TLSGD_TO_IE); 132905edf1c1Srobert c.addReloc({target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_IE), type, 133005edf1c1Srobert offset, addend, &sym}); 133105edf1c1Srobert } else { 133205edf1c1Srobert c.addReloc({target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_LE), type, 133305edf1c1Srobert offset, addend, &sym}); 133405edf1c1Srobert } 133505edf1c1Srobert return target->getTlsGdRelaxSkip(type); 133605edf1c1Srobert } 133705edf1c1Srobert 133805edf1c1Srobert if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, R_AARCH64_GOT_PAGE_PC, R_GOT_OFF, 133905edf1c1Srobert R_TLSIE_HINT>(expr)) { 134005edf1c1Srobert ctx.hasTlsIe.store(true, std::memory_order_relaxed); 134105edf1c1Srobert // Initial-Exec relocs can be relaxed to Local-Exec if the symbol is locally 134205edf1c1Srobert // defined. 134305edf1c1Srobert if (toExecRelax && isLocalInExecutable) { 134405edf1c1Srobert c.addReloc({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym}); 134505edf1c1Srobert } else if (expr != R_TLSIE_HINT) { 134605edf1c1Srobert sym.setFlags(NEEDS_TLSIE); 134705edf1c1Srobert // R_GOT needs a relative relocation for PIC on i386 and Hexagon. 134805edf1c1Srobert if (expr == R_GOT && config->isPic && !target->usesOnlyLowPageBits(type)) 134905edf1c1Srobert addRelativeReloc<true>(c, offset, sym, addend, expr, type); 1350ece8a530Spatrick else 135105edf1c1Srobert c.addReloc({expr, type, offset, addend, &sym}); 135205edf1c1Srobert } 135305edf1c1Srobert return 1; 1354ece8a530Spatrick } 1355ece8a530Spatrick 135605edf1c1Srobert return 0; 1357ece8a530Spatrick } 1358ece8a530Spatrick 135905edf1c1Srobert template <class ELFT, class RelTy> void RelocationScanner::scanOne(RelTy *&i) { 1360ece8a530Spatrick const RelTy &rel = *i; 1361ece8a530Spatrick uint32_t symIndex = rel.getSymbol(config->isMips64EL); 136205edf1c1Srobert Symbol &sym = sec->getFile<ELFT>()->getSymbol(symIndex); 1363ece8a530Spatrick RelType type; 1364ece8a530Spatrick if (config->mipsN32Abi) { 136505edf1c1Srobert type = getMipsN32RelType(i); 1366ece8a530Spatrick } else { 1367ece8a530Spatrick type = rel.getType(config->isMips64EL); 1368ece8a530Spatrick ++i; 1369ece8a530Spatrick } 1370ece8a530Spatrick // Get an offset in an output section this relocation is applied to. 137105edf1c1Srobert uint64_t offset = getter.get(rel.r_offset); 1372ece8a530Spatrick if (offset == uint64_t(-1)) 1373ece8a530Spatrick return; 1374ece8a530Spatrick 137505edf1c1Srobert reportGNUWarning(sym, *sec, rel.r_offset); 1376ece8a530Spatrick 137705edf1c1Srobert RelExpr expr = target->getRelExpr(type, sym, sec->content().data() + offset); 137805edf1c1Srobert int64_t addend = RelTy::IsRela 137905edf1c1Srobert ? getAddend<ELFT>(rel) 138005edf1c1Srobert : target->getImplicitAddend( 138105edf1c1Srobert sec->content().data() + rel.r_offset, type); 138205edf1c1Srobert if (LLVM_UNLIKELY(config->emachine == EM_MIPS)) 138305edf1c1Srobert addend += computeMipsAddend<ELFT>(rel, expr, sym.isLocal()); 138405edf1c1Srobert else if (config->emachine == EM_PPC64 && config->isPic && type == R_PPC64_TOC) 138505edf1c1Srobert addend += getPPC64TocBase(); 1386ece8a530Spatrick 1387ece8a530Spatrick // Ignore R_*_NONE and other marker relocations. 1388ece8a530Spatrick if (expr == R_NONE) 1389ece8a530Spatrick return; 1390ece8a530Spatrick 139105edf1c1Srobert // Error if the target symbol is undefined. Symbol index 0 may be used by 139205edf1c1Srobert // marker relocations, e.g. R_*_NONE and R_ARM_V4BX. Don't error on them. 139305edf1c1Srobert if (sym.isUndefined() && symIndex != 0 && 139405edf1c1Srobert maybeReportUndefined(cast<Undefined>(sym), *sec, offset)) 139505edf1c1Srobert return; 1396ece8a530Spatrick 1397adae0cfdSpatrick if (config->emachine == EM_PPC64) { 1398adae0cfdSpatrick // We can separate the small code model relocations into 2 categories: 1399adae0cfdSpatrick // 1) Those that access the compiler generated .toc sections. 1400adae0cfdSpatrick // 2) Those that access the linker allocated got entries. 1401adae0cfdSpatrick // lld allocates got entries to symbols on demand. Since we don't try to 1402adae0cfdSpatrick // sort the got entries in any way, we don't have to track which objects 1403adae0cfdSpatrick // have got-based small code model relocs. The .toc sections get placed 1404adae0cfdSpatrick // after the end of the linker allocated .got section and we do sort those 1405adae0cfdSpatrick // so sections addressed with small code model relocations come first. 140605edf1c1Srobert if (type == R_PPC64_TOC16 || type == R_PPC64_TOC16_DS) 140705edf1c1Srobert sec->file->ppc64SmallCodeModelTocRelocs = true; 1408adae0cfdSpatrick 1409adae0cfdSpatrick // Record the TOC entry (.toc + addend) as not relaxable. See the comment in 1410adae0cfdSpatrick // InputSectionBase::relocateAlloc(). 1411adae0cfdSpatrick if (type == R_PPC64_TOC16_LO && sym.isSection() && isa<Defined>(sym) && 1412adae0cfdSpatrick cast<Defined>(sym).section->name == ".toc") 1413adae0cfdSpatrick ppc64noTocRelax.insert({&sym, addend}); 1414a0747c9fSpatrick 1415a0747c9fSpatrick if ((type == R_PPC64_TLSGD && expr == R_TLSDESC_CALL) || 1416a0747c9fSpatrick (type == R_PPC64_TLSLD && expr == R_TLSLD_HINT)) { 1417a0747c9fSpatrick if (i == end) { 1418a0747c9fSpatrick errorOrWarn("R_PPC64_TLSGD/R_PPC64_TLSLD may not be the last " 1419a0747c9fSpatrick "relocation" + 142005edf1c1Srobert getLocation(*sec, sym, offset)); 1421a0747c9fSpatrick return; 1422a0747c9fSpatrick } 1423a0747c9fSpatrick 1424a0747c9fSpatrick // Offset the 4-byte aligned R_PPC64_TLSGD by one byte in the NOTOC case, 1425a0747c9fSpatrick // so we can discern it later from the toc-case. 1426a0747c9fSpatrick if (i->getType(/*isMips64EL=*/false) == R_PPC64_REL24_NOTOC) 1427a0747c9fSpatrick ++offset; 1428a0747c9fSpatrick } 1429adae0cfdSpatrick } 1430adae0cfdSpatrick 1431ece8a530Spatrick // If the relocation does not emit a GOT or GOTPLT entry but its computation 1432ece8a530Spatrick // uses their addresses, we need GOT or GOTPLT to be created. 1433ece8a530Spatrick // 143405edf1c1Srobert // The 5 types that relative GOTPLT are all x86 and x86-64 specific. 143505edf1c1Srobert if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_PLT_GOTPLT, 143605edf1c1Srobert R_TLSDESC_GOTPLT, R_TLSGD_GOTPLT>(expr)) { 143705edf1c1Srobert in.gotPlt->hasGotPltOffRel.store(true, std::memory_order_relaxed); 1438a0747c9fSpatrick } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC32_PLTREL, R_PPC64_TOCBASE, 1439a0747c9fSpatrick R_PPC64_RELAX_TOC>(expr)) { 144005edf1c1Srobert in.got->hasGotOffRel.store(true, std::memory_order_relaxed); 1441ece8a530Spatrick } 1442ece8a530Spatrick 1443a0747c9fSpatrick // Process TLS relocations, including relaxing TLS relocations. Note that 144405edf1c1Srobert // R_TPREL and R_TPREL_NEG relocations are resolved in processAux. 144505edf1c1Srobert if (sym.isTls()) { 144605edf1c1Srobert if (unsigned processed = 144705edf1c1Srobert handleTlsRelocation(type, sym, *sec, offset, addend, expr)) { 144805edf1c1Srobert i += processed - 1; 1449a0747c9fSpatrick return; 1450a0747c9fSpatrick } 1451ece8a530Spatrick } 1452ece8a530Spatrick 145305edf1c1Srobert processAux(expr, type, offset, sym, addend); 1454ece8a530Spatrick } 1455ece8a530Spatrick 1456a0747c9fSpatrick // R_PPC64_TLSGD/R_PPC64_TLSLD is required to mark `bl __tls_get_addr` for 1457a0747c9fSpatrick // General Dynamic/Local Dynamic code sequences. If a GD/LD GOT relocation is 1458a0747c9fSpatrick // found but no R_PPC64_TLSGD/R_PPC64_TLSLD is seen, we assume that the 1459a0747c9fSpatrick // instructions are generated by very old IBM XL compilers. Work around the 1460a0747c9fSpatrick // issue by disabling GD/LD to IE/LE relaxation. 1461a0747c9fSpatrick template <class RelTy> 1462a0747c9fSpatrick static void checkPPC64TLSRelax(InputSectionBase &sec, ArrayRef<RelTy> rels) { 1463a0747c9fSpatrick // Skip if sec is synthetic (sec.file is null) or if sec has been marked. 1464a0747c9fSpatrick if (!sec.file || sec.file->ppc64DisableTLSRelax) 1465a0747c9fSpatrick return; 1466a0747c9fSpatrick bool hasGDLD = false; 1467a0747c9fSpatrick for (const RelTy &rel : rels) { 1468a0747c9fSpatrick RelType type = rel.getType(false); 1469a0747c9fSpatrick switch (type) { 1470a0747c9fSpatrick case R_PPC64_TLSGD: 1471a0747c9fSpatrick case R_PPC64_TLSLD: 1472a0747c9fSpatrick return; // Found a marker 1473a0747c9fSpatrick case R_PPC64_GOT_TLSGD16: 1474a0747c9fSpatrick case R_PPC64_GOT_TLSGD16_HA: 1475a0747c9fSpatrick case R_PPC64_GOT_TLSGD16_HI: 1476a0747c9fSpatrick case R_PPC64_GOT_TLSGD16_LO: 1477a0747c9fSpatrick case R_PPC64_GOT_TLSLD16: 1478a0747c9fSpatrick case R_PPC64_GOT_TLSLD16_HA: 1479a0747c9fSpatrick case R_PPC64_GOT_TLSLD16_HI: 1480a0747c9fSpatrick case R_PPC64_GOT_TLSLD16_LO: 1481a0747c9fSpatrick hasGDLD = true; 1482a0747c9fSpatrick break; 1483a0747c9fSpatrick } 1484a0747c9fSpatrick } 1485a0747c9fSpatrick if (hasGDLD) { 1486a0747c9fSpatrick sec.file->ppc64DisableTLSRelax = true; 1487a0747c9fSpatrick warn(toString(sec.file) + 1488a0747c9fSpatrick ": disable TLS relaxation due to R_PPC64_GOT_TLS* relocations without " 1489a0747c9fSpatrick "R_PPC64_TLSGD/R_PPC64_TLSLD relocations"); 1490a0747c9fSpatrick } 1491a0747c9fSpatrick } 1492a0747c9fSpatrick 1493ece8a530Spatrick template <class ELFT, class RelTy> 149405edf1c1Srobert void RelocationScanner::scan(ArrayRef<RelTy> rels) { 149505edf1c1Srobert // Not all relocations end up in Sec->Relocations, but a lot do. 149605edf1c1Srobert sec->relocations.reserve(rels.size()); 1497ece8a530Spatrick 1498a0747c9fSpatrick if (config->emachine == EM_PPC64) 149905edf1c1Srobert checkPPC64TLSRelax<RelTy>(*sec, rels); 1500a0747c9fSpatrick 1501a0747c9fSpatrick // For EhInputSection, OffsetGetter expects the relocations to be sorted by 1502a0747c9fSpatrick // r_offset. In rare cases (.eh_frame pieces are reordered by a linker 1503a0747c9fSpatrick // script), the relocations may be unordered. 1504a0747c9fSpatrick SmallVector<RelTy, 0> storage; 1505a0747c9fSpatrick if (isa<EhInputSection>(sec)) 1506a0747c9fSpatrick rels = sortRels(rels, storage); 1507a0747c9fSpatrick 150805edf1c1Srobert end = static_cast<const void *>(rels.end()); 150905edf1c1Srobert for (auto i = rels.begin(); i != end;) 151005edf1c1Srobert scanOne<ELFT>(i); 1511ece8a530Spatrick 1512ece8a530Spatrick // Sort relocations by offset for more efficient searching for 1513ece8a530Spatrick // R_RISCV_PCREL_HI20 and R_PPC64_ADDR64. 1514ece8a530Spatrick if (config->emachine == EM_RISCV || 151505edf1c1Srobert (config->emachine == EM_PPC64 && sec->name == ".toc")) 151605edf1c1Srobert llvm::stable_sort(sec->relocs(), 1517ece8a530Spatrick [](const Relocation &lhs, const Relocation &rhs) { 1518ece8a530Spatrick return lhs.offset < rhs.offset; 1519ece8a530Spatrick }); 1520ece8a530Spatrick } 1521ece8a530Spatrick 152205edf1c1Srobert template <class ELFT> void RelocationScanner::scanSection(InputSectionBase &s) { 152305edf1c1Srobert sec = &s; 152405edf1c1Srobert getter = OffsetGetter(s); 152505edf1c1Srobert const RelsOrRelas<ELFT> rels = s.template relsOrRelas<ELFT>(); 152605edf1c1Srobert if (rels.areRelocsRel()) 152705edf1c1Srobert scan<ELFT>(rels.rels); 1528ece8a530Spatrick else 152905edf1c1Srobert scan<ELFT>(rels.relas); 153005edf1c1Srobert } 153105edf1c1Srobert 153205edf1c1Srobert template <class ELFT> void elf::scanRelocations() { 153305edf1c1Srobert // Scan all relocations. Each relocation goes through a series of tests to 153405edf1c1Srobert // determine if it needs special treatment, such as creating GOT, PLT, 153505edf1c1Srobert // copy relocations, etc. Note that relocations for non-alloc sections are 153605edf1c1Srobert // directly processed by InputSection::relocateNonAlloc. 153705edf1c1Srobert 153805edf1c1Srobert // Deterministic parallellism needs sorting relocations which is unsuitable 153905edf1c1Srobert // for -z nocombreloc. MIPS and PPC64 use global states which are not suitable 154005edf1c1Srobert // for parallelism. 154105edf1c1Srobert bool serial = !config->zCombreloc || config->emachine == EM_MIPS || 154205edf1c1Srobert config->emachine == EM_PPC64; 154305edf1c1Srobert parallel::TaskGroup tg; 154405edf1c1Srobert for (ELFFileBase *f : ctx.objectFiles) { 154505edf1c1Srobert auto fn = [f]() { 154605edf1c1Srobert RelocationScanner scanner; 154705edf1c1Srobert for (InputSectionBase *s : f->getSections()) { 154805edf1c1Srobert if (s && s->kind() == SectionBase::Regular && s->isLive() && 154905edf1c1Srobert (s->flags & SHF_ALLOC) && 155005edf1c1Srobert !(s->type == SHT_ARM_EXIDX && config->emachine == EM_ARM)) 155105edf1c1Srobert scanner.template scanSection<ELFT>(*s); 155205edf1c1Srobert } 155305edf1c1Srobert }; 155405edf1c1Srobert if (serial) 155505edf1c1Srobert fn(); 155605edf1c1Srobert else 155705edf1c1Srobert tg.execute(fn); 155805edf1c1Srobert } 155905edf1c1Srobert 156005edf1c1Srobert // Both the main thread and thread pool index 0 use getThreadIndex()==0. Be 156105edf1c1Srobert // careful that they don't concurrently run scanSections. When serial is 156205edf1c1Srobert // true, fn() has finished at this point, so running execute is safe. 156305edf1c1Srobert tg.execute([] { 156405edf1c1Srobert RelocationScanner scanner; 156505edf1c1Srobert for (Partition &part : partitions) { 156605edf1c1Srobert for (EhInputSection *sec : part.ehFrame->sections) 156705edf1c1Srobert scanner.template scanSection<ELFT>(*sec); 156805edf1c1Srobert if (part.armExidx && part.armExidx->isLive()) 156905edf1c1Srobert for (InputSection *sec : part.armExidx->exidxSections) 157005edf1c1Srobert scanner.template scanSection<ELFT>(*sec); 157105edf1c1Srobert } 157205edf1c1Srobert }); 157305edf1c1Srobert } 157405edf1c1Srobert 157505edf1c1Srobert static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) { 157605edf1c1Srobert // Handle a reference to a non-preemptible ifunc. These are special in a 157705edf1c1Srobert // few ways: 157805edf1c1Srobert // 157905edf1c1Srobert // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have 158005edf1c1Srobert // a fixed value. But assuming that all references to the ifunc are 158105edf1c1Srobert // GOT-generating or PLT-generating, the handling of an ifunc is 158205edf1c1Srobert // relatively straightforward. We create a PLT entry in Iplt, which is 158305edf1c1Srobert // usually at the end of .plt, which makes an indirect call using a 158405edf1c1Srobert // matching GOT entry in igotPlt, which is usually at the end of .got.plt. 158505edf1c1Srobert // The GOT entry is relocated using an IRELATIVE relocation in relaIplt, 158605edf1c1Srobert // which is usually at the end of .rela.plt. Unlike most relocations in 158705edf1c1Srobert // .rela.plt, which may be evaluated lazily without -z now, dynamic 158805edf1c1Srobert // loaders evaluate IRELATIVE relocs eagerly, which means that for 158905edf1c1Srobert // IRELATIVE relocs only, GOT-generating relocations can point directly to 159005edf1c1Srobert // .got.plt without requiring a separate GOT entry. 159105edf1c1Srobert // 159205edf1c1Srobert // - Despite the fact that an ifunc does not have a fixed value, compilers 159305edf1c1Srobert // that are not passed -fPIC will assume that they do, and will emit 159405edf1c1Srobert // direct (non-GOT-generating, non-PLT-generating) relocations to the 159505edf1c1Srobert // symbol. This means that if a direct relocation to the symbol is 159605edf1c1Srobert // seen, the linker must set a value for the symbol, and this value must 159705edf1c1Srobert // be consistent no matter what type of reference is made to the symbol. 159805edf1c1Srobert // This can be done by creating a PLT entry for the symbol in the way 159905edf1c1Srobert // described above and making it canonical, that is, making all references 160005edf1c1Srobert // point to the PLT entry instead of the resolver. In lld we also store 160105edf1c1Srobert // the address of the PLT entry in the dynamic symbol table, which means 160205edf1c1Srobert // that the symbol will also have the same value in other modules. 160305edf1c1Srobert // Because the value loaded from the GOT needs to be consistent with 160405edf1c1Srobert // the value computed using a direct relocation, a non-preemptible ifunc 160505edf1c1Srobert // may end up with two GOT entries, one in .got.plt that points to the 160605edf1c1Srobert // address returned by the resolver and is used only by the PLT entry, 160705edf1c1Srobert // and another in .got that points to the PLT entry and is used by 160805edf1c1Srobert // GOT-generating relocations. 160905edf1c1Srobert // 161005edf1c1Srobert // - The fact that these symbols do not have a fixed value makes them an 161105edf1c1Srobert // exception to the general rule that a statically linked executable does 161205edf1c1Srobert // not require any form of dynamic relocation. To handle these relocations 161305edf1c1Srobert // correctly, the IRELATIVE relocations are stored in an array which a 161405edf1c1Srobert // statically linked executable's startup code must enumerate using the 161505edf1c1Srobert // linker-defined symbols __rela?_iplt_{start,end}. 161605edf1c1Srobert if (!sym.isGnuIFunc() || sym.isPreemptible || config->zIfuncNoplt) 161705edf1c1Srobert return false; 161805edf1c1Srobert // Skip unreferenced non-preemptible ifunc. 161905edf1c1Srobert if (!(flags & (NEEDS_GOT | NEEDS_PLT | HAS_DIRECT_RELOC))) 162005edf1c1Srobert return true; 162105edf1c1Srobert 162205edf1c1Srobert sym.isInIplt = true; 162305edf1c1Srobert 162405edf1c1Srobert // Create an Iplt and the associated IRELATIVE relocation pointing to the 162505edf1c1Srobert // original section/value pairs. For non-GOT non-PLT relocation case below, we 162605edf1c1Srobert // may alter section/value, so create a copy of the symbol to make 162705edf1c1Srobert // section/value fixed. 162805edf1c1Srobert auto *directSym = makeDefined(cast<Defined>(sym)); 162905edf1c1Srobert directSym->allocateAux(); 163005edf1c1Srobert addPltEntry(*in.iplt, *in.igotPlt, *in.relaIplt, target->iRelativeRel, 163105edf1c1Srobert *directSym); 163205edf1c1Srobert sym.allocateAux(); 163305edf1c1Srobert symAux.back().pltIdx = symAux[directSym->auxIdx].pltIdx; 163405edf1c1Srobert 163505edf1c1Srobert if (flags & HAS_DIRECT_RELOC) { 163605edf1c1Srobert // Change the value to the IPLT and redirect all references to it. 163705edf1c1Srobert auto &d = cast<Defined>(sym); 163805edf1c1Srobert d.section = in.iplt.get(); 163905edf1c1Srobert d.value = d.getPltIdx() * target->ipltEntrySize; 164005edf1c1Srobert d.size = 0; 164105edf1c1Srobert // It's important to set the symbol type here so that dynamic loaders 164205edf1c1Srobert // don't try to call the PLT as if it were an ifunc resolver. 164305edf1c1Srobert d.type = STT_FUNC; 164405edf1c1Srobert 164505edf1c1Srobert if (flags & NEEDS_GOT) 164605edf1c1Srobert addGotEntry(sym); 164705edf1c1Srobert } else if (flags & NEEDS_GOT) { 164805edf1c1Srobert // Redirect GOT accesses to point to the Igot. 164905edf1c1Srobert sym.gotInIgot = true; 165005edf1c1Srobert } 165105edf1c1Srobert return true; 165205edf1c1Srobert } 165305edf1c1Srobert 165405edf1c1Srobert void elf::postScanRelocations() { 165505edf1c1Srobert auto fn = [](Symbol &sym) { 165605edf1c1Srobert auto flags = sym.flags.load(std::memory_order_relaxed); 165705edf1c1Srobert if (handleNonPreemptibleIfunc(sym, flags)) 165805edf1c1Srobert return; 165905edf1c1Srobert if (!sym.needsDynReloc()) 166005edf1c1Srobert return; 166105edf1c1Srobert sym.allocateAux(); 166205edf1c1Srobert 166305edf1c1Srobert if (flags & NEEDS_GOT) 166405edf1c1Srobert addGotEntry(sym); 166505edf1c1Srobert if (flags & NEEDS_PLT) 166605edf1c1Srobert addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, sym); 166705edf1c1Srobert if (flags & NEEDS_COPY) { 166805edf1c1Srobert if (sym.isObject()) { 166905edf1c1Srobert invokeELFT(addCopyRelSymbol, cast<SharedSymbol>(sym)); 167005edf1c1Srobert // NEEDS_COPY is cleared for sym and its aliases so that in 167105edf1c1Srobert // later iterations aliases won't cause redundant copies. 167205edf1c1Srobert assert(!sym.hasFlag(NEEDS_COPY)); 167305edf1c1Srobert } else { 167405edf1c1Srobert assert(sym.isFunc() && sym.hasFlag(NEEDS_PLT)); 167505edf1c1Srobert if (!sym.isDefined()) { 167605edf1c1Srobert replaceWithDefined(sym, *in.plt, 167705edf1c1Srobert target->pltHeaderSize + 167805edf1c1Srobert target->pltEntrySize * sym.getPltIdx(), 167905edf1c1Srobert 0); 168005edf1c1Srobert sym.setFlags(NEEDS_COPY); 168105edf1c1Srobert if (config->emachine == EM_PPC) { 168205edf1c1Srobert // PPC32 canonical PLT entries are at the beginning of .glink 168305edf1c1Srobert cast<Defined>(sym).value = in.plt->headerSize; 168405edf1c1Srobert in.plt->headerSize += 16; 168505edf1c1Srobert cast<PPC32GlinkSection>(*in.plt).canonical_plts.push_back(&sym); 168605edf1c1Srobert } 168705edf1c1Srobert } 168805edf1c1Srobert } 168905edf1c1Srobert } 169005edf1c1Srobert 169105edf1c1Srobert if (!sym.isTls()) 169205edf1c1Srobert return; 169305edf1c1Srobert bool isLocalInExecutable = !sym.isPreemptible && !config->shared; 169405edf1c1Srobert GotSection *got = in.got.get(); 169505edf1c1Srobert 169605edf1c1Srobert if (flags & NEEDS_TLSDESC) { 169705edf1c1Srobert got->addTlsDescEntry(sym); 169805edf1c1Srobert mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible( 169905edf1c1Srobert target->tlsDescRel, *got, got->getTlsDescOffset(sym), sym, 170005edf1c1Srobert target->tlsDescRel); 170105edf1c1Srobert } 170205edf1c1Srobert if (flags & NEEDS_TLSGD) { 170305edf1c1Srobert got->addDynTlsEntry(sym); 170405edf1c1Srobert uint64_t off = got->getGlobalDynOffset(sym); 170505edf1c1Srobert if (isLocalInExecutable) 170605edf1c1Srobert // Write one to the GOT slot. 170705edf1c1Srobert got->addConstant({R_ADDEND, target->symbolicRel, off, 1, &sym}); 170805edf1c1Srobert else 170905edf1c1Srobert mainPart->relaDyn->addSymbolReloc(target->tlsModuleIndexRel, *got, off, 171005edf1c1Srobert sym); 171105edf1c1Srobert 171205edf1c1Srobert // If the symbol is preemptible we need the dynamic linker to write 171305edf1c1Srobert // the offset too. 171405edf1c1Srobert uint64_t offsetOff = off + config->wordsize; 171505edf1c1Srobert if (sym.isPreemptible) 171605edf1c1Srobert mainPart->relaDyn->addSymbolReloc(target->tlsOffsetRel, *got, offsetOff, 171705edf1c1Srobert sym); 171805edf1c1Srobert else 171905edf1c1Srobert got->addConstant({R_ABS, target->tlsOffsetRel, offsetOff, 0, &sym}); 172005edf1c1Srobert } 172105edf1c1Srobert if (flags & NEEDS_TLSGD_TO_IE) { 172205edf1c1Srobert got->addEntry(sym); 172305edf1c1Srobert mainPart->relaDyn->addSymbolReloc(target->tlsGotRel, *got, 172405edf1c1Srobert sym.getGotOffset(), sym); 172505edf1c1Srobert } 172605edf1c1Srobert if (flags & NEEDS_GOT_DTPREL) { 172705edf1c1Srobert got->addEntry(sym); 172805edf1c1Srobert got->addConstant( 172905edf1c1Srobert {R_ABS, target->tlsOffsetRel, sym.getGotOffset(), 0, &sym}); 173005edf1c1Srobert } 173105edf1c1Srobert 173205edf1c1Srobert if ((flags & NEEDS_TLSIE) && !(flags & NEEDS_TLSGD_TO_IE)) 173305edf1c1Srobert addTpOffsetGotEntry(sym); 173405edf1c1Srobert }; 173505edf1c1Srobert 173605edf1c1Srobert GotSection *got = in.got.get(); 173705edf1c1Srobert if (ctx.needsTlsLd.load(std::memory_order_relaxed) && got->addTlsIndex()) { 173805edf1c1Srobert static Undefined dummy(nullptr, "", STB_LOCAL, 0, 0); 173905edf1c1Srobert if (config->shared) 174005edf1c1Srobert mainPart->relaDyn->addReloc( 174105edf1c1Srobert {target->tlsModuleIndexRel, got, got->getTlsIndexOff()}); 174205edf1c1Srobert else 174305edf1c1Srobert got->addConstant( 174405edf1c1Srobert {R_ADDEND, target->symbolicRel, got->getTlsIndexOff(), 1, &dummy}); 174505edf1c1Srobert } 174605edf1c1Srobert 174705edf1c1Srobert assert(symAux.size() == 1); 174805edf1c1Srobert for (Symbol *sym : symtab.getSymbols()) 174905edf1c1Srobert fn(*sym); 175005edf1c1Srobert 175105edf1c1Srobert // Local symbols may need the aforementioned non-preemptible ifunc and GOT 175205edf1c1Srobert // handling. They don't need regular PLT. 175305edf1c1Srobert for (ELFFileBase *file : ctx.objectFiles) 175405edf1c1Srobert for (Symbol *sym : file->getLocalSymbols()) 175505edf1c1Srobert fn(*sym); 1756ece8a530Spatrick } 1757ece8a530Spatrick 1758ece8a530Spatrick static bool mergeCmp(const InputSection *a, const InputSection *b) { 1759ece8a530Spatrick // std::merge requires a strict weak ordering. 1760ece8a530Spatrick if (a->outSecOff < b->outSecOff) 1761ece8a530Spatrick return true; 1762ece8a530Spatrick 176305edf1c1Srobert // FIXME dyn_cast<ThunkSection> is non-null for any SyntheticSection. 176405edf1c1Srobert if (a->outSecOff == b->outSecOff && a != b) { 1765ece8a530Spatrick auto *ta = dyn_cast<ThunkSection>(a); 1766ece8a530Spatrick auto *tb = dyn_cast<ThunkSection>(b); 1767ece8a530Spatrick 1768ece8a530Spatrick // Check if Thunk is immediately before any specific Target 1769ece8a530Spatrick // InputSection for example Mips LA25 Thunks. 1770ece8a530Spatrick if (ta && ta->getTargetInputSection() == b) 1771ece8a530Spatrick return true; 1772ece8a530Spatrick 1773ece8a530Spatrick // Place Thunk Sections without specific targets before 1774ece8a530Spatrick // non-Thunk Sections. 1775ece8a530Spatrick if (ta && !tb && !ta->getTargetInputSection()) 1776ece8a530Spatrick return true; 1777ece8a530Spatrick } 1778ece8a530Spatrick 1779ece8a530Spatrick return false; 1780ece8a530Spatrick } 1781ece8a530Spatrick 1782ece8a530Spatrick // Call Fn on every executable InputSection accessed via the linker script 1783ece8a530Spatrick // InputSectionDescription::Sections. 1784ece8a530Spatrick static void forEachInputSectionDescription( 1785ece8a530Spatrick ArrayRef<OutputSection *> outputSections, 1786ece8a530Spatrick llvm::function_ref<void(OutputSection *, InputSectionDescription *)> fn) { 1787ece8a530Spatrick for (OutputSection *os : outputSections) { 1788ece8a530Spatrick if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR)) 1789ece8a530Spatrick continue; 179005edf1c1Srobert for (SectionCommand *bc : os->commands) 1791ece8a530Spatrick if (auto *isd = dyn_cast<InputSectionDescription>(bc)) 1792ece8a530Spatrick fn(os, isd); 1793ece8a530Spatrick } 1794ece8a530Spatrick } 1795ece8a530Spatrick 1796ece8a530Spatrick // Thunk Implementation 1797ece8a530Spatrick // 1798ece8a530Spatrick // Thunks (sometimes called stubs, veneers or branch islands) are small pieces 1799ece8a530Spatrick // of code that the linker inserts inbetween a caller and a callee. The thunks 1800ece8a530Spatrick // are added at link time rather than compile time as the decision on whether 1801ece8a530Spatrick // a thunk is needed, such as the caller and callee being out of range, can only 1802ece8a530Spatrick // be made at link time. 1803ece8a530Spatrick // 1804ece8a530Spatrick // It is straightforward to tell given the current state of the program when a 1805ece8a530Spatrick // thunk is needed for a particular call. The more difficult part is that 1806ece8a530Spatrick // the thunk needs to be placed in the program such that the caller can reach 1807ece8a530Spatrick // the thunk and the thunk can reach the callee; furthermore, adding thunks to 1808ece8a530Spatrick // the program alters addresses, which can mean more thunks etc. 1809ece8a530Spatrick // 1810ece8a530Spatrick // In lld we have a synthetic ThunkSection that can hold many Thunks. 1811ece8a530Spatrick // The decision to have a ThunkSection act as a container means that we can 1812ece8a530Spatrick // more easily handle the most common case of a single block of contiguous 1813ece8a530Spatrick // Thunks by inserting just a single ThunkSection. 1814ece8a530Spatrick // 1815ece8a530Spatrick // The implementation of Thunks in lld is split across these areas 1816ece8a530Spatrick // Relocations.cpp : Framework for creating and placing thunks 1817ece8a530Spatrick // Thunks.cpp : The code generated for each supported thunk 1818ece8a530Spatrick // Target.cpp : Target specific hooks that the framework uses to decide when 1819ece8a530Spatrick // a thunk is used 1820ece8a530Spatrick // Synthetic.cpp : Implementation of ThunkSection 1821ece8a530Spatrick // Writer.cpp : Iteratively call framework until no more Thunks added 1822ece8a530Spatrick // 1823ece8a530Spatrick // Thunk placement requirements: 1824ece8a530Spatrick // Mips LA25 thunks. These must be placed immediately before the callee section 1825ece8a530Spatrick // We can assume that the caller is in range of the Thunk. These are modelled 1826ece8a530Spatrick // by Thunks that return the section they must precede with 1827ece8a530Spatrick // getTargetInputSection(). 1828ece8a530Spatrick // 1829ece8a530Spatrick // ARM interworking and range extension thunks. These thunks must be placed 1830ece8a530Spatrick // within range of the caller. All implemented ARM thunks can always reach the 1831ece8a530Spatrick // callee as they use an indirect jump via a register that has no range 1832ece8a530Spatrick // restrictions. 1833ece8a530Spatrick // 1834ece8a530Spatrick // Thunk placement algorithm: 1835ece8a530Spatrick // For Mips LA25 ThunkSections; the placement is explicit, it has to be before 1836ece8a530Spatrick // getTargetInputSection(). 1837ece8a530Spatrick // 1838ece8a530Spatrick // For thunks that must be placed within range of the caller there are many 1839ece8a530Spatrick // possible choices given that the maximum range from the caller is usually 1840ece8a530Spatrick // much larger than the average InputSection size. Desirable properties include: 1841ece8a530Spatrick // - Maximize reuse of thunks by multiple callers 1842ece8a530Spatrick // - Minimize number of ThunkSections to simplify insertion 1843ece8a530Spatrick // - Handle impact of already added Thunks on addresses 1844ece8a530Spatrick // - Simple to understand and implement 1845ece8a530Spatrick // 1846ece8a530Spatrick // In lld for the first pass, we pre-create one or more ThunkSections per 1847ece8a530Spatrick // InputSectionDescription at Target specific intervals. A ThunkSection is 1848ece8a530Spatrick // placed so that the estimated end of the ThunkSection is within range of the 1849ece8a530Spatrick // start of the InputSectionDescription or the previous ThunkSection. For 1850ece8a530Spatrick // example: 1851ece8a530Spatrick // InputSectionDescription 1852ece8a530Spatrick // Section 0 1853ece8a530Spatrick // ... 1854ece8a530Spatrick // Section N 1855ece8a530Spatrick // ThunkSection 0 1856ece8a530Spatrick // Section N + 1 1857ece8a530Spatrick // ... 1858ece8a530Spatrick // Section N + K 1859ece8a530Spatrick // Thunk Section 1 1860ece8a530Spatrick // 1861ece8a530Spatrick // The intention is that we can add a Thunk to a ThunkSection that is well 1862ece8a530Spatrick // spaced enough to service a number of callers without having to do a lot 1863ece8a530Spatrick // of work. An important principle is that it is not an error if a Thunk cannot 1864ece8a530Spatrick // be placed in a pre-created ThunkSection; when this happens we create a new 1865ece8a530Spatrick // ThunkSection placed next to the caller. This allows us to handle the vast 1866ece8a530Spatrick // majority of thunks simply, but also handle rare cases where the branch range 1867ece8a530Spatrick // is smaller than the target specific spacing. 1868ece8a530Spatrick // 1869ece8a530Spatrick // The algorithm is expected to create all the thunks that are needed in a 1870ece8a530Spatrick // single pass, with a small number of programs needing a second pass due to 1871ece8a530Spatrick // the insertion of thunks in the first pass increasing the offset between 1872ece8a530Spatrick // callers and callees that were only just in range. 1873ece8a530Spatrick // 1874ece8a530Spatrick // A consequence of allowing new ThunkSections to be created outside of the 1875ece8a530Spatrick // pre-created ThunkSections is that in rare cases calls to Thunks that were in 1876ece8a530Spatrick // range in pass K, are out of range in some pass > K due to the insertion of 1877ece8a530Spatrick // more Thunks in between the caller and callee. When this happens we retarget 1878ece8a530Spatrick // the relocation back to the original target and create another Thunk. 1879ece8a530Spatrick 1880ece8a530Spatrick // Remove ThunkSections that are empty, this should only be the initial set 1881ece8a530Spatrick // precreated on pass 0. 1882ece8a530Spatrick 1883ece8a530Spatrick // Insert the Thunks for OutputSection OS into their designated place 1884ece8a530Spatrick // in the Sections vector, and recalculate the InputSection output section 1885ece8a530Spatrick // offsets. 1886ece8a530Spatrick // This may invalidate any output section offsets stored outside of InputSection 1887ece8a530Spatrick void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) { 1888ece8a530Spatrick forEachInputSectionDescription( 1889ece8a530Spatrick outputSections, [&](OutputSection *os, InputSectionDescription *isd) { 1890ece8a530Spatrick if (isd->thunkSections.empty()) 1891ece8a530Spatrick return; 1892ece8a530Spatrick 1893ece8a530Spatrick // Remove any zero sized precreated Thunks. 1894ece8a530Spatrick llvm::erase_if(isd->thunkSections, 1895ece8a530Spatrick [](const std::pair<ThunkSection *, uint32_t> &ts) { 1896ece8a530Spatrick return ts.first->getSize() == 0; 1897ece8a530Spatrick }); 1898ece8a530Spatrick 1899ece8a530Spatrick // ISD->ThunkSections contains all created ThunkSections, including 1900ece8a530Spatrick // those inserted in previous passes. Extract the Thunks created this 1901ece8a530Spatrick // pass and order them in ascending outSecOff. 1902ece8a530Spatrick std::vector<ThunkSection *> newThunks; 1903ece8a530Spatrick for (std::pair<ThunkSection *, uint32_t> ts : isd->thunkSections) 1904ece8a530Spatrick if (ts.second == pass) 1905ece8a530Spatrick newThunks.push_back(ts.first); 1906ece8a530Spatrick llvm::stable_sort(newThunks, 1907ece8a530Spatrick [](const ThunkSection *a, const ThunkSection *b) { 1908ece8a530Spatrick return a->outSecOff < b->outSecOff; 1909ece8a530Spatrick }); 1910ece8a530Spatrick 1911ece8a530Spatrick // Merge sorted vectors of Thunks and InputSections by outSecOff 191205edf1c1Srobert SmallVector<InputSection *, 0> tmp; 1913ece8a530Spatrick tmp.reserve(isd->sections.size() + newThunks.size()); 1914ece8a530Spatrick 1915ece8a530Spatrick std::merge(isd->sections.begin(), isd->sections.end(), 1916ece8a530Spatrick newThunks.begin(), newThunks.end(), std::back_inserter(tmp), 1917ece8a530Spatrick mergeCmp); 1918ece8a530Spatrick 1919ece8a530Spatrick isd->sections = std::move(tmp); 1920ece8a530Spatrick }); 1921ece8a530Spatrick } 1922ece8a530Spatrick 192305edf1c1Srobert static int64_t getPCBias(RelType type) { 192405edf1c1Srobert if (config->emachine != EM_ARM) 192505edf1c1Srobert return 0; 192605edf1c1Srobert switch (type) { 192705edf1c1Srobert case R_ARM_THM_JUMP19: 192805edf1c1Srobert case R_ARM_THM_JUMP24: 192905edf1c1Srobert case R_ARM_THM_CALL: 193005edf1c1Srobert return 4; 193105edf1c1Srobert default: 193205edf1c1Srobert return 8; 193305edf1c1Srobert } 193405edf1c1Srobert } 193505edf1c1Srobert 1936ece8a530Spatrick // Find or create a ThunkSection within the InputSectionDescription (ISD) that 1937ece8a530Spatrick // is in range of Src. An ISD maps to a range of InputSections described by a 1938ece8a530Spatrick // linker script section pattern such as { .text .text.* }. 1939a0747c9fSpatrick ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os, 1940a0747c9fSpatrick InputSection *isec, 1941ece8a530Spatrick InputSectionDescription *isd, 1942a0747c9fSpatrick const Relocation &rel, 1943a0747c9fSpatrick uint64_t src) { 194405edf1c1Srobert // See the comment in getThunk for -pcBias below. 194505edf1c1Srobert const int64_t pcBias = getPCBias(rel.type); 1946ece8a530Spatrick for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) { 1947ece8a530Spatrick ThunkSection *ts = tp.first; 194805edf1c1Srobert uint64_t tsBase = os->addr + ts->outSecOff - pcBias; 194905edf1c1Srobert uint64_t tsLimit = tsBase + ts->getSize(); 1950a0747c9fSpatrick if (target->inBranchRange(rel.type, src, 1951a0747c9fSpatrick (src > tsLimit) ? tsBase : tsLimit)) 1952ece8a530Spatrick return ts; 1953ece8a530Spatrick } 1954ece8a530Spatrick 1955ece8a530Spatrick // No suitable ThunkSection exists. This can happen when there is a branch 1956ece8a530Spatrick // with lower range than the ThunkSection spacing or when there are too 1957ece8a530Spatrick // many Thunks. Create a new ThunkSection as close to the InputSection as 1958ece8a530Spatrick // possible. Error if InputSection is so large we cannot place ThunkSection 1959ece8a530Spatrick // anywhere in Range. 1960ece8a530Spatrick uint64_t thunkSecOff = isec->outSecOff; 1961a0747c9fSpatrick if (!target->inBranchRange(rel.type, src, 1962a0747c9fSpatrick os->addr + thunkSecOff + rel.addend)) { 1963ece8a530Spatrick thunkSecOff = isec->outSecOff + isec->getSize(); 1964a0747c9fSpatrick if (!target->inBranchRange(rel.type, src, 1965a0747c9fSpatrick os->addr + thunkSecOff + rel.addend)) 1966ece8a530Spatrick fatal("InputSection too large for range extension thunk " + 1967ece8a530Spatrick isec->getObjMsg(src - (os->addr + isec->outSecOff))); 1968ece8a530Spatrick } 1969ece8a530Spatrick return addThunkSection(os, isd, thunkSecOff); 1970ece8a530Spatrick } 1971ece8a530Spatrick 1972ece8a530Spatrick // Add a Thunk that needs to be placed in a ThunkSection that immediately 1973ece8a530Spatrick // precedes its Target. 1974ece8a530Spatrick ThunkSection *ThunkCreator::getISThunkSec(InputSection *isec) { 1975ece8a530Spatrick ThunkSection *ts = thunkedSections.lookup(isec); 1976ece8a530Spatrick if (ts) 1977ece8a530Spatrick return ts; 1978ece8a530Spatrick 1979ece8a530Spatrick // Find InputSectionRange within Target Output Section (TOS) that the 1980ece8a530Spatrick // InputSection (IS) that we need to precede is in. 1981ece8a530Spatrick OutputSection *tos = isec->getParent(); 198205edf1c1Srobert for (SectionCommand *bc : tos->commands) { 1983ece8a530Spatrick auto *isd = dyn_cast<InputSectionDescription>(bc); 1984ece8a530Spatrick if (!isd || isd->sections.empty()) 1985ece8a530Spatrick continue; 1986ece8a530Spatrick 1987ece8a530Spatrick InputSection *first = isd->sections.front(); 1988ece8a530Spatrick InputSection *last = isd->sections.back(); 1989ece8a530Spatrick 1990ece8a530Spatrick if (isec->outSecOff < first->outSecOff || last->outSecOff < isec->outSecOff) 1991ece8a530Spatrick continue; 1992ece8a530Spatrick 1993ece8a530Spatrick ts = addThunkSection(tos, isd, isec->outSecOff); 1994ece8a530Spatrick thunkedSections[isec] = ts; 1995ece8a530Spatrick return ts; 1996ece8a530Spatrick } 1997ece8a530Spatrick 1998ece8a530Spatrick return nullptr; 1999ece8a530Spatrick } 2000ece8a530Spatrick 2001ece8a530Spatrick // Create one or more ThunkSections per OS that can be used to place Thunks. 2002ece8a530Spatrick // We attempt to place the ThunkSections using the following desirable 2003ece8a530Spatrick // properties: 2004ece8a530Spatrick // - Within range of the maximum number of callers 2005ece8a530Spatrick // - Minimise the number of ThunkSections 2006ece8a530Spatrick // 2007ece8a530Spatrick // We follow a simple but conservative heuristic to place ThunkSections at 2008ece8a530Spatrick // offsets that are multiples of a Target specific branch range. 2009ece8a530Spatrick // For an InputSectionDescription that is smaller than the range, a single 2010ece8a530Spatrick // ThunkSection at the end of the range will do. 2011ece8a530Spatrick // 2012ece8a530Spatrick // For an InputSectionDescription that is more than twice the size of the range, 2013ece8a530Spatrick // we place the last ThunkSection at range bytes from the end of the 2014ece8a530Spatrick // InputSectionDescription in order to increase the likelihood that the 2015ece8a530Spatrick // distance from a thunk to its target will be sufficiently small to 2016ece8a530Spatrick // allow for the creation of a short thunk. 2017ece8a530Spatrick void ThunkCreator::createInitialThunkSections( 2018ece8a530Spatrick ArrayRef<OutputSection *> outputSections) { 2019ece8a530Spatrick uint32_t thunkSectionSpacing = target->getThunkSectionSpacing(); 2020ece8a530Spatrick 2021ece8a530Spatrick forEachInputSectionDescription( 2022ece8a530Spatrick outputSections, [&](OutputSection *os, InputSectionDescription *isd) { 2023ece8a530Spatrick if (isd->sections.empty()) 2024ece8a530Spatrick return; 2025ece8a530Spatrick 2026ece8a530Spatrick uint32_t isdBegin = isd->sections.front()->outSecOff; 2027ece8a530Spatrick uint32_t isdEnd = 2028ece8a530Spatrick isd->sections.back()->outSecOff + isd->sections.back()->getSize(); 2029ece8a530Spatrick uint32_t lastThunkLowerBound = -1; 2030ece8a530Spatrick if (isdEnd - isdBegin > thunkSectionSpacing * 2) 2031ece8a530Spatrick lastThunkLowerBound = isdEnd - thunkSectionSpacing; 2032ece8a530Spatrick 2033ece8a530Spatrick uint32_t isecLimit; 2034ece8a530Spatrick uint32_t prevIsecLimit = isdBegin; 2035ece8a530Spatrick uint32_t thunkUpperBound = isdBegin + thunkSectionSpacing; 2036ece8a530Spatrick 2037ece8a530Spatrick for (const InputSection *isec : isd->sections) { 2038ece8a530Spatrick isecLimit = isec->outSecOff + isec->getSize(); 2039ece8a530Spatrick if (isecLimit > thunkUpperBound) { 2040ece8a530Spatrick addThunkSection(os, isd, prevIsecLimit); 2041ece8a530Spatrick thunkUpperBound = prevIsecLimit + thunkSectionSpacing; 2042ece8a530Spatrick } 2043ece8a530Spatrick if (isecLimit > lastThunkLowerBound) 2044ece8a530Spatrick break; 2045ece8a530Spatrick prevIsecLimit = isecLimit; 2046ece8a530Spatrick } 2047ece8a530Spatrick addThunkSection(os, isd, isecLimit); 2048ece8a530Spatrick }); 2049ece8a530Spatrick } 2050ece8a530Spatrick 2051ece8a530Spatrick ThunkSection *ThunkCreator::addThunkSection(OutputSection *os, 2052ece8a530Spatrick InputSectionDescription *isd, 2053ece8a530Spatrick uint64_t off) { 2054ece8a530Spatrick auto *ts = make<ThunkSection>(os, off); 2055ece8a530Spatrick ts->partition = os->partition; 2056ece8a530Spatrick if ((config->fixCortexA53Errata843419 || config->fixCortexA8) && 2057ece8a530Spatrick !isd->sections.empty()) { 2058ece8a530Spatrick // The errata fixes are sensitive to addresses modulo 4 KiB. When we add 2059ece8a530Spatrick // thunks we disturb the base addresses of sections placed after the thunks 2060ece8a530Spatrick // this makes patches we have generated redundant, and may cause us to 2061ece8a530Spatrick // generate more patches as different instructions are now in sensitive 2062ece8a530Spatrick // locations. When we generate more patches we may force more branches to 2063ece8a530Spatrick // go out of range, causing more thunks to be generated. In pathological 2064ece8a530Spatrick // cases this can cause the address dependent content pass not to converge. 2065ece8a530Spatrick // We fix this by rounding up the size of the ThunkSection to 4KiB, this 2066ece8a530Spatrick // limits the insertion of a ThunkSection on the addresses modulo 4 KiB, 2067ece8a530Spatrick // which means that adding Thunks to the section does not invalidate 2068ece8a530Spatrick // errata patches for following code. 2069ece8a530Spatrick // Rounding up the size to 4KiB has consequences for code-size and can 2070ece8a530Spatrick // trip up linker script defined assertions. For example the linux kernel 2071ece8a530Spatrick // has an assertion that what LLD represents as an InputSectionDescription 2072ece8a530Spatrick // does not exceed 4 KiB even if the overall OutputSection is > 128 Mib. 2073ece8a530Spatrick // We use the heuristic of rounding up the size when both of the following 2074ece8a530Spatrick // conditions are true: 2075ece8a530Spatrick // 1.) The OutputSection is larger than the ThunkSectionSpacing. This 2076ece8a530Spatrick // accounts for the case where no single InputSectionDescription is 2077ece8a530Spatrick // larger than the OutputSection size. This is conservative but simple. 2078ece8a530Spatrick // 2.) The InputSectionDescription is larger than 4 KiB. This will prevent 2079ece8a530Spatrick // any assertion failures that an InputSectionDescription is < 4 KiB 2080ece8a530Spatrick // in size. 2081ece8a530Spatrick uint64_t isdSize = isd->sections.back()->outSecOff + 2082ece8a530Spatrick isd->sections.back()->getSize() - 2083ece8a530Spatrick isd->sections.front()->outSecOff; 2084ece8a530Spatrick if (os->size > target->getThunkSectionSpacing() && isdSize > 4096) 2085ece8a530Spatrick ts->roundUpSizeForErrata = true; 2086ece8a530Spatrick } 2087ece8a530Spatrick isd->thunkSections.push_back({ts, pass}); 2088ece8a530Spatrick return ts; 2089ece8a530Spatrick } 2090ece8a530Spatrick 2091ece8a530Spatrick static bool isThunkSectionCompatible(InputSection *source, 2092ece8a530Spatrick SectionBase *target) { 2093ece8a530Spatrick // We can't reuse thunks in different loadable partitions because they might 2094ece8a530Spatrick // not be loaded. But partition 1 (the main partition) will always be loaded. 2095ece8a530Spatrick if (source->partition != target->partition) 2096ece8a530Spatrick return target->partition == 1; 2097ece8a530Spatrick return true; 2098ece8a530Spatrick } 2099ece8a530Spatrick 2100ece8a530Spatrick std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec, 2101ece8a530Spatrick Relocation &rel, uint64_t src) { 2102ece8a530Spatrick std::vector<Thunk *> *thunkVec = nullptr; 2103a0747c9fSpatrick // Arm and Thumb have a PC Bias of 8 and 4 respectively, this is cancelled 2104a0747c9fSpatrick // out in the relocation addend. We compensate for the PC bias so that 2105a0747c9fSpatrick // an Arm and Thumb relocation to the same destination get the same keyAddend, 2106a0747c9fSpatrick // which is usually 0. 210705edf1c1Srobert const int64_t pcBias = getPCBias(rel.type); 210805edf1c1Srobert const int64_t keyAddend = rel.addend + pcBias; 2109ece8a530Spatrick 2110ece8a530Spatrick // We use a ((section, offset), addend) pair to find the thunk position if 2111ece8a530Spatrick // possible so that we create only one thunk for aliased symbols or ICFed 2112ece8a530Spatrick // sections. There may be multiple relocations sharing the same (section, 2113ece8a530Spatrick // offset + addend) pair. We may revert the relocation back to its original 2114ece8a530Spatrick // non-Thunk target, so we cannot fold offset + addend. 2115ece8a530Spatrick if (auto *d = dyn_cast<Defined>(rel.sym)) 2116ece8a530Spatrick if (!d->isInPlt() && d->section) 211705edf1c1Srobert thunkVec = &thunkedSymbolsBySectionAndAddend[{{d->section, d->value}, 211805edf1c1Srobert keyAddend}]; 2119ece8a530Spatrick if (!thunkVec) 2120a0747c9fSpatrick thunkVec = &thunkedSymbols[{rel.sym, keyAddend}]; 2121ece8a530Spatrick 2122ece8a530Spatrick // Check existing Thunks for Sym to see if they can be reused 2123ece8a530Spatrick for (Thunk *t : *thunkVec) 2124ece8a530Spatrick if (isThunkSectionCompatible(isec, t->getThunkTargetSym()->section) && 2125ece8a530Spatrick t->isCompatibleWith(*isec, rel) && 2126ece8a530Spatrick target->inBranchRange(rel.type, src, 212705edf1c1Srobert t->getThunkTargetSym()->getVA(-pcBias))) 2128ece8a530Spatrick return std::make_pair(t, false); 2129ece8a530Spatrick 2130ece8a530Spatrick // No existing compatible Thunk in range, create a new one 2131ece8a530Spatrick Thunk *t = addThunk(*isec, rel); 2132ece8a530Spatrick thunkVec->push_back(t); 2133ece8a530Spatrick return std::make_pair(t, true); 2134ece8a530Spatrick } 2135ece8a530Spatrick 2136ece8a530Spatrick // Return true if the relocation target is an in range Thunk. 2137ece8a530Spatrick // Return false if the relocation is not to a Thunk. If the relocation target 2138ece8a530Spatrick // was originally to a Thunk, but is no longer in range we revert the 2139ece8a530Spatrick // relocation back to its original non-Thunk target. 2140ece8a530Spatrick bool ThunkCreator::normalizeExistingThunk(Relocation &rel, uint64_t src) { 2141ece8a530Spatrick if (Thunk *t = thunks.lookup(rel.sym)) { 2142a0747c9fSpatrick if (target->inBranchRange(rel.type, src, rel.sym->getVA(rel.addend))) 2143ece8a530Spatrick return true; 2144ece8a530Spatrick rel.sym = &t->destination; 2145ece8a530Spatrick rel.addend = t->addend; 2146ece8a530Spatrick if (rel.sym->isInPlt()) 2147ece8a530Spatrick rel.expr = toPlt(rel.expr); 2148ece8a530Spatrick } 2149ece8a530Spatrick return false; 2150ece8a530Spatrick } 2151ece8a530Spatrick 2152ece8a530Spatrick // Process all relocations from the InputSections that have been assigned 2153ece8a530Spatrick // to InputSectionDescriptions and redirect through Thunks if needed. The 2154ece8a530Spatrick // function should be called iteratively until it returns false. 2155ece8a530Spatrick // 2156ece8a530Spatrick // PreConditions: 2157ece8a530Spatrick // All InputSections that may need a Thunk are reachable from 2158ece8a530Spatrick // OutputSectionCommands. 2159ece8a530Spatrick // 2160ece8a530Spatrick // All OutputSections have an address and all InputSections have an offset 2161ece8a530Spatrick // within the OutputSection. 2162ece8a530Spatrick // 2163ece8a530Spatrick // The offsets between caller (relocation place) and callee 2164ece8a530Spatrick // (relocation target) will not be modified outside of createThunks(). 2165ece8a530Spatrick // 2166ece8a530Spatrick // PostConditions: 2167ece8a530Spatrick // If return value is true then ThunkSections have been inserted into 2168ece8a530Spatrick // OutputSections. All relocations that needed a Thunk based on the information 2169ece8a530Spatrick // available to createThunks() on entry have been redirected to a Thunk. Note 2170ece8a530Spatrick // that adding Thunks changes offsets between caller and callee so more Thunks 2171ece8a530Spatrick // may be required. 2172ece8a530Spatrick // 2173ece8a530Spatrick // If return value is false then no more Thunks are needed, and createThunks has 2174ece8a530Spatrick // made no changes. If the target requires range extension thunks, currently 2175ece8a530Spatrick // ARM, then any future change in offset between caller and callee risks a 2176ece8a530Spatrick // relocation out of range error. 217705edf1c1Srobert bool ThunkCreator::createThunks(uint32_t pass, 217805edf1c1Srobert ArrayRef<OutputSection *> outputSections) { 217905edf1c1Srobert this->pass = pass; 2180ece8a530Spatrick bool addressesChanged = false; 2181ece8a530Spatrick 2182ece8a530Spatrick if (pass == 0 && target->getThunkSectionSpacing()) 2183ece8a530Spatrick createInitialThunkSections(outputSections); 2184ece8a530Spatrick 2185ece8a530Spatrick // Create all the Thunks and insert them into synthetic ThunkSections. The 2186ece8a530Spatrick // ThunkSections are later inserted back into InputSectionDescriptions. 2187ece8a530Spatrick // We separate the creation of ThunkSections from the insertion of the 2188ece8a530Spatrick // ThunkSections as ThunkSections are not always inserted into the same 2189ece8a530Spatrick // InputSectionDescription as the caller. 2190ece8a530Spatrick forEachInputSectionDescription( 2191ece8a530Spatrick outputSections, [&](OutputSection *os, InputSectionDescription *isd) { 2192ece8a530Spatrick for (InputSection *isec : isd->sections) 219305edf1c1Srobert for (Relocation &rel : isec->relocs()) { 2194ece8a530Spatrick uint64_t src = isec->getVA(rel.offset); 2195ece8a530Spatrick 2196ece8a530Spatrick // If we are a relocation to an existing Thunk, check if it is 2197ece8a530Spatrick // still in range. If not then Rel will be altered to point to its 2198ece8a530Spatrick // original target so another Thunk can be generated. 2199ece8a530Spatrick if (pass > 0 && normalizeExistingThunk(rel, src)) 2200ece8a530Spatrick continue; 2201ece8a530Spatrick 2202ece8a530Spatrick if (!target->needsThunk(rel.expr, rel.type, isec->file, src, 2203ece8a530Spatrick *rel.sym, rel.addend)) 2204ece8a530Spatrick continue; 2205ece8a530Spatrick 2206ece8a530Spatrick Thunk *t; 2207ece8a530Spatrick bool isNew; 2208ece8a530Spatrick std::tie(t, isNew) = getThunk(isec, rel, src); 2209ece8a530Spatrick 2210ece8a530Spatrick if (isNew) { 2211ece8a530Spatrick // Find or create a ThunkSection for the new Thunk 2212ece8a530Spatrick ThunkSection *ts; 2213ece8a530Spatrick if (auto *tis = t->getTargetInputSection()) 2214ece8a530Spatrick ts = getISThunkSec(tis); 2215ece8a530Spatrick else 2216a0747c9fSpatrick ts = getISDThunkSec(os, isec, isd, rel, src); 2217ece8a530Spatrick ts->addThunk(t); 2218ece8a530Spatrick thunks[t->getThunkTargetSym()] = t; 2219ece8a530Spatrick } 2220ece8a530Spatrick 2221ece8a530Spatrick // Redirect relocation to Thunk, we never go via the PLT to a Thunk 2222ece8a530Spatrick rel.sym = t->getThunkTargetSym(); 2223ece8a530Spatrick rel.expr = fromPlt(rel.expr); 2224ece8a530Spatrick 2225ece8a530Spatrick // On AArch64 and PPC, a jump/call relocation may be encoded as 2226ece8a530Spatrick // STT_SECTION + non-zero addend, clear the addend after 2227ece8a530Spatrick // redirection. 2228ece8a530Spatrick if (config->emachine != EM_MIPS) 2229ece8a530Spatrick rel.addend = -getPCBias(rel.type); 2230ece8a530Spatrick } 2231ece8a530Spatrick 2232ece8a530Spatrick for (auto &p : isd->thunkSections) 2233ece8a530Spatrick addressesChanged |= p.first->assignOffsets(); 2234ece8a530Spatrick }); 2235ece8a530Spatrick 2236ece8a530Spatrick for (auto &p : thunkedSections) 2237ece8a530Spatrick addressesChanged |= p.second->assignOffsets(); 2238ece8a530Spatrick 2239ece8a530Spatrick // Merge all created synthetic ThunkSections back into OutputSection 2240ece8a530Spatrick mergeThunks(outputSections); 2241ece8a530Spatrick return addressesChanged; 2242ece8a530Spatrick } 2243ece8a530Spatrick 2244adae0cfdSpatrick // The following aid in the conversion of call x@GDPLT to call __tls_get_addr 2245adae0cfdSpatrick // hexagonNeedsTLSSymbol scans for relocations would require a call to 2246adae0cfdSpatrick // __tls_get_addr. 2247adae0cfdSpatrick // hexagonTLSSymbolUpdate rebinds the relocation to __tls_get_addr. 2248adae0cfdSpatrick bool elf::hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections) { 2249adae0cfdSpatrick bool needTlsSymbol = false; 2250adae0cfdSpatrick forEachInputSectionDescription( 2251adae0cfdSpatrick outputSections, [&](OutputSection *os, InputSectionDescription *isd) { 2252adae0cfdSpatrick for (InputSection *isec : isd->sections) 225305edf1c1Srobert for (Relocation &rel : isec->relocs()) 2254adae0cfdSpatrick if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) { 2255adae0cfdSpatrick needTlsSymbol = true; 2256adae0cfdSpatrick return; 2257adae0cfdSpatrick } 2258adae0cfdSpatrick }); 2259adae0cfdSpatrick return needTlsSymbol; 2260adae0cfdSpatrick } 2261ece8a530Spatrick 2262adae0cfdSpatrick void elf::hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections) { 226305edf1c1Srobert Symbol *sym = symtab.find("__tls_get_addr"); 2264adae0cfdSpatrick if (!sym) 2265adae0cfdSpatrick return; 2266adae0cfdSpatrick bool needEntry = true; 2267adae0cfdSpatrick forEachInputSectionDescription( 2268adae0cfdSpatrick outputSections, [&](OutputSection *os, InputSectionDescription *isd) { 2269adae0cfdSpatrick for (InputSection *isec : isd->sections) 227005edf1c1Srobert for (Relocation &rel : isec->relocs()) 2271adae0cfdSpatrick if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) { 2272adae0cfdSpatrick if (needEntry) { 227305edf1c1Srobert sym->allocateAux(); 227405edf1c1Srobert addPltEntry(*in.plt, *in.gotPlt, *in.relaPlt, target->pltRel, 2275adae0cfdSpatrick *sym); 2276adae0cfdSpatrick needEntry = false; 2277adae0cfdSpatrick } 2278adae0cfdSpatrick rel.sym = sym; 2279adae0cfdSpatrick } 2280adae0cfdSpatrick }); 2281adae0cfdSpatrick } 2282adae0cfdSpatrick 228305edf1c1Srobert template void elf::scanRelocations<ELF32LE>(); 228405edf1c1Srobert template void elf::scanRelocations<ELF32BE>(); 228505edf1c1Srobert template void elf::scanRelocations<ELF64LE>(); 228605edf1c1Srobert template void elf::scanRelocations<ELF64BE>(); 2287