10b57cec5SDimitry Andric //===- InputSection.cpp ---------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "InputSection.h" 100b57cec5SDimitry Andric #include "Config.h" 110b57cec5SDimitry Andric #include "InputFiles.h" 120b57cec5SDimitry Andric #include "OutputSections.h" 130b57cec5SDimitry Andric #include "Relocations.h" 140b57cec5SDimitry Andric #include "SymbolTable.h" 150b57cec5SDimitry Andric #include "Symbols.h" 160b57cec5SDimitry Andric #include "SyntheticSections.h" 170b57cec5SDimitry Andric #include "Target.h" 1804eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h" 190b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 200b57cec5SDimitry Andric #include "llvm/Support/Compression.h" 210b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 220b57cec5SDimitry Andric #include "llvm/Support/xxhash.h" 230b57cec5SDimitry Andric #include <algorithm> 240b57cec5SDimitry Andric #include <mutex> 255f757f3fSDimitry Andric #include <optional> 260b57cec5SDimitry Andric #include <vector> 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric using namespace llvm; 290b57cec5SDimitry Andric using namespace llvm::ELF; 300b57cec5SDimitry Andric using namespace llvm::object; 310b57cec5SDimitry Andric using namespace llvm::support; 320b57cec5SDimitry Andric using namespace llvm::support::endian; 330b57cec5SDimitry Andric using namespace llvm::sys; 345ffd83dbSDimitry Andric using namespace lld; 355ffd83dbSDimitry Andric using namespace lld::elf; 360b57cec5SDimitry Andric 375ffd83dbSDimitry Andric DenseSet<std::pair<const Symbol *, uint64_t>> elf::ppc64noTocRelax; 385ffd83dbSDimitry Andric 390b57cec5SDimitry Andric // Returns a string to construct an error message. 405ffd83dbSDimitry Andric std::string lld::toString(const InputSectionBase *sec) { 410b57cec5SDimitry Andric return (toString(sec->file) + ":(" + sec->name + ")").str(); 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric template <class ELFT> 450b57cec5SDimitry Andric static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &file, 460b57cec5SDimitry Andric const typename ELFT::Shdr &hdr) { 470b57cec5SDimitry Andric if (hdr.sh_type == SHT_NOBITS) 48bdd1243dSDimitry Andric return ArrayRef<uint8_t>(nullptr, hdr.sh_size); 49e8d8bef9SDimitry Andric return check(file.getObj().getSectionContents(hdr)); 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags, 530b57cec5SDimitry Andric uint32_t type, uint64_t entsize, 540b57cec5SDimitry Andric uint32_t link, uint32_t info, 55bdd1243dSDimitry Andric uint32_t addralign, ArrayRef<uint8_t> data, 560b57cec5SDimitry Andric StringRef name, Kind sectionKind) 57bdd1243dSDimitry Andric : SectionBase(sectionKind, name, flags, entsize, addralign, type, info, 580b57cec5SDimitry Andric link), 59bdd1243dSDimitry Andric file(file), content_(data.data()), size(data.size()) { 600b57cec5SDimitry Andric // In order to reduce memory allocation, we assume that mergeable 610b57cec5SDimitry Andric // sections are smaller than 4 GiB, which is not an unreasonable 620b57cec5SDimitry Andric // assumption as of 2017. 63bdd1243dSDimitry Andric if (sectionKind == SectionBase::Merge && content().size() > UINT32_MAX) 640b57cec5SDimitry Andric error(toString(this) + ": section too large"); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // The ELF spec states that a value of 0 means the section has 67480093f4SDimitry Andric // no alignment constraints. 68bdd1243dSDimitry Andric uint32_t v = std::max<uint32_t>(addralign, 1); 690b57cec5SDimitry Andric if (!isPowerOf2_64(v)) 700b57cec5SDimitry Andric fatal(toString(this) + ": sh_addralign is not a power of 2"); 71bdd1243dSDimitry Andric this->addralign = v; 720b57cec5SDimitry Andric 7381ad6265SDimitry Andric // If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no 7481ad6265SDimitry Andric // longer supported. 75753f127fSDimitry Andric if (flags & SHF_COMPRESSED) 7606c3fb27SDimitry Andric invokeELFT(parseCompressedHeader,); 770b57cec5SDimitry Andric } 780b57cec5SDimitry Andric 790fca6ea1SDimitry Andric // SHF_INFO_LINK and SHF_GROUP are normally resolved and not copied to the 800fca6ea1SDimitry Andric // output section. However, for relocatable linking without 810fca6ea1SDimitry Andric // --force-group-allocation, the SHF_GROUP flag and section groups are retained. 820b57cec5SDimitry Andric static uint64_t getFlags(uint64_t flags) { 830b57cec5SDimitry Andric flags &= ~(uint64_t)SHF_INFO_LINK; 840fca6ea1SDimitry Andric if (config->resolveGroups) 850b57cec5SDimitry Andric flags &= ~(uint64_t)SHF_GROUP; 860b57cec5SDimitry Andric return flags; 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric template <class ELFT> 900b57cec5SDimitry Andric InputSectionBase::InputSectionBase(ObjFile<ELFT> &file, 910b57cec5SDimitry Andric const typename ELFT::Shdr &hdr, 920b57cec5SDimitry Andric StringRef name, Kind sectionKind) 93349cc55cSDimitry Andric : InputSectionBase(&file, getFlags(hdr.sh_flags), hdr.sh_type, 94349cc55cSDimitry Andric hdr.sh_entsize, hdr.sh_link, hdr.sh_info, 95349cc55cSDimitry Andric hdr.sh_addralign, getSectionContents(file, hdr), name, 96349cc55cSDimitry Andric sectionKind) { 970b57cec5SDimitry Andric // We reject object files having insanely large alignments even though 980b57cec5SDimitry Andric // they are allowed by the spec. I think 4GB is a reasonable limitation. 990b57cec5SDimitry Andric // We might want to relax this in the future. 1000b57cec5SDimitry Andric if (hdr.sh_addralign > UINT32_MAX) 1010b57cec5SDimitry Andric fatal(toString(&file) + ": section sh_addralign is too large"); 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric size_t InputSectionBase::getSize() const { 1050b57cec5SDimitry Andric if (auto *s = dyn_cast<SyntheticSection>(this)) 1060b57cec5SDimitry Andric return s->getSize(); 107bdd1243dSDimitry Andric return size - bytesDropped; 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric 110bdd1243dSDimitry Andric template <class ELFT> 111bdd1243dSDimitry Andric static void decompressAux(const InputSectionBase &sec, uint8_t *out, 112bdd1243dSDimitry Andric size_t size) { 113bdd1243dSDimitry Andric auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(sec.content_); 114bdd1243dSDimitry Andric auto compressed = ArrayRef<uint8_t>(sec.content_, sec.compressedSize) 115bdd1243dSDimitry Andric .slice(sizeof(typename ELFT::Chdr)); 116bdd1243dSDimitry Andric if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB 117bdd1243dSDimitry Andric ? compression::zlib::decompress(compressed, out, size) 118bdd1243dSDimitry Andric : compression::zstd::decompress(compressed, out, size)) 119bdd1243dSDimitry Andric fatal(toString(&sec) + 120bdd1243dSDimitry Andric ": decompress failed: " + llvm::toString(std::move(e))); 121bdd1243dSDimitry Andric } 122bdd1243dSDimitry Andric 123bdd1243dSDimitry Andric void InputSectionBase::decompress() const { 124753f127fSDimitry Andric uint8_t *uncompressedBuf; 1250b57cec5SDimitry Andric { 1260b57cec5SDimitry Andric static std::mutex mu; 1270b57cec5SDimitry Andric std::lock_guard<std::mutex> lock(mu); 128753f127fSDimitry Andric uncompressedBuf = bAlloc().Allocate<uint8_t>(size); 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric 131bdd1243dSDimitry Andric invokeELFT(decompressAux, *this, uncompressedBuf, size); 132bdd1243dSDimitry Andric content_ = uncompressedBuf; 133bdd1243dSDimitry Andric compressed = false; 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 13652418fc2SDimitry Andric template <class ELFT> 13752418fc2SDimitry Andric RelsOrRelas<ELFT> InputSectionBase::relsOrRelas(bool supportsCrel) const { 138349cc55cSDimitry Andric if (relSecIdx == 0) 139349cc55cSDimitry Andric return {}; 140349cc55cSDimitry Andric RelsOrRelas<ELFT> ret; 14152418fc2SDimitry Andric auto *f = cast<ObjFile<ELFT>>(file); 14252418fc2SDimitry Andric typename ELFT::Shdr shdr = f->template getELFShdrs<ELFT>()[relSecIdx]; 14352418fc2SDimitry Andric if (shdr.sh_type == SHT_CREL) { 14452418fc2SDimitry Andric // Return an iterator if supported by caller. 14552418fc2SDimitry Andric if (supportsCrel) { 14652418fc2SDimitry Andric ret.crels = Relocs<typename ELFT::Crel>( 14752418fc2SDimitry Andric (const uint8_t *)f->mb.getBufferStart() + shdr.sh_offset); 14852418fc2SDimitry Andric return ret; 14952418fc2SDimitry Andric } 15052418fc2SDimitry Andric InputSectionBase *const &relSec = f->getSections()[relSecIdx]; 15152418fc2SDimitry Andric // Otherwise, allocate a buffer to hold the decoded RELA relocations. When 15252418fc2SDimitry Andric // called for the first time, relSec is null (without --emit-relocs) or an 153*6e516c87SDimitry Andric // InputSection with false decodedCrel. 154*6e516c87SDimitry Andric if (!relSec || !cast<InputSection>(relSec)->decodedCrel) { 15552418fc2SDimitry Andric auto *sec = makeThreadLocal<InputSection>(*f, shdr, name); 15652418fc2SDimitry Andric f->cacheDecodedCrel(relSecIdx, sec); 15752418fc2SDimitry Andric sec->type = SHT_RELA; 158*6e516c87SDimitry Andric sec->decodedCrel = true; 15952418fc2SDimitry Andric 16052418fc2SDimitry Andric RelocsCrel<ELFT::Is64Bits> entries(sec->content_); 16152418fc2SDimitry Andric sec->size = entries.size() * sizeof(typename ELFT::Rela); 16252418fc2SDimitry Andric auto *relas = makeThreadLocalN<typename ELFT::Rela>(entries.size()); 16352418fc2SDimitry Andric sec->content_ = reinterpret_cast<uint8_t *>(relas); 16452418fc2SDimitry Andric for (auto [i, r] : llvm::enumerate(entries)) { 16552418fc2SDimitry Andric relas[i].r_offset = r.r_offset; 16652418fc2SDimitry Andric relas[i].setSymbolAndType(r.r_symidx, r.r_type, false); 16752418fc2SDimitry Andric relas[i].r_addend = r.r_addend; 16852418fc2SDimitry Andric } 16952418fc2SDimitry Andric } 17052418fc2SDimitry Andric ret.relas = {ArrayRef( 17152418fc2SDimitry Andric reinterpret_cast<const typename ELFT::Rela *>(relSec->content_), 17252418fc2SDimitry Andric relSec->size / sizeof(typename ELFT::Rela))}; 17352418fc2SDimitry Andric return ret; 17452418fc2SDimitry Andric } 17552418fc2SDimitry Andric 17652418fc2SDimitry Andric const void *content = f->mb.getBufferStart() + shdr.sh_offset; 17752418fc2SDimitry Andric size_t size = shdr.sh_size; 178349cc55cSDimitry Andric if (shdr.sh_type == SHT_REL) { 17952418fc2SDimitry Andric ret.rels = {ArrayRef(reinterpret_cast<const typename ELFT::Rel *>(content), 18052418fc2SDimitry Andric size / sizeof(typename ELFT::Rel))}; 181349cc55cSDimitry Andric } else { 182349cc55cSDimitry Andric assert(shdr.sh_type == SHT_RELA); 18352418fc2SDimitry Andric ret.relas = { 18452418fc2SDimitry Andric ArrayRef(reinterpret_cast<const typename ELFT::Rela *>(content), 18552418fc2SDimitry Andric size / sizeof(typename ELFT::Rela))}; 186349cc55cSDimitry Andric } 187349cc55cSDimitry Andric return ret; 188349cc55cSDimitry Andric } 189349cc55cSDimitry Andric 1900b57cec5SDimitry Andric uint64_t SectionBase::getOffset(uint64_t offset) const { 1910b57cec5SDimitry Andric switch (kind()) { 1920b57cec5SDimitry Andric case Output: { 1930b57cec5SDimitry Andric auto *os = cast<OutputSection>(this); 1940b57cec5SDimitry Andric // For output sections we treat offset -1 as the end of the section. 1950b57cec5SDimitry Andric return offset == uint64_t(-1) ? os->size : offset; 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric case Regular: 1980b57cec5SDimitry Andric case Synthetic: 1990fca6ea1SDimitry Andric case Spill: 2004824e7fdSDimitry Andric return cast<InputSection>(this)->outSecOff + offset; 20181ad6265SDimitry Andric case EHFrame: { 20281ad6265SDimitry Andric // Two code paths may reach here. First, clang_rt.crtbegin.o and GCC 20381ad6265SDimitry Andric // crtbeginT.o may reference the start of an empty .eh_frame to identify the 20481ad6265SDimitry Andric // start of the output .eh_frame. Just return offset. 20581ad6265SDimitry Andric // 20681ad6265SDimitry Andric // Second, InputSection::copyRelocations on .eh_frame. Some pieces may be 20781ad6265SDimitry Andric // discarded due to GC/ICF. We should compute the output section offset. 20881ad6265SDimitry Andric const EhInputSection *es = cast<EhInputSection>(this); 209bdd1243dSDimitry Andric if (!es->content().empty()) 21081ad6265SDimitry Andric if (InputSection *isec = es->getParent()) 21181ad6265SDimitry Andric return isec->outSecOff + es->getParentOffset(offset); 2120b57cec5SDimitry Andric return offset; 21381ad6265SDimitry Andric } 2140b57cec5SDimitry Andric case Merge: 2150b57cec5SDimitry Andric const MergeInputSection *ms = cast<MergeInputSection>(this); 2160b57cec5SDimitry Andric if (InputSection *isec = ms->getParent()) 2174824e7fdSDimitry Andric return isec->outSecOff + ms->getParentOffset(offset); 2180b57cec5SDimitry Andric return ms->getParentOffset(offset); 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric llvm_unreachable("invalid section kind"); 2210b57cec5SDimitry Andric } 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric uint64_t SectionBase::getVA(uint64_t offset) const { 2240b57cec5SDimitry Andric const OutputSection *out = getOutputSection(); 2250b57cec5SDimitry Andric return (out ? out->addr : 0) + getOffset(offset); 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric OutputSection *SectionBase::getOutputSection() { 2290b57cec5SDimitry Andric InputSection *sec; 2300b57cec5SDimitry Andric if (auto *isec = dyn_cast<InputSection>(this)) 2310b57cec5SDimitry Andric sec = isec; 2320b57cec5SDimitry Andric else if (auto *ms = dyn_cast<MergeInputSection>(this)) 2330b57cec5SDimitry Andric sec = ms->getParent(); 2340b57cec5SDimitry Andric else if (auto *eh = dyn_cast<EhInputSection>(this)) 2350b57cec5SDimitry Andric sec = eh->getParent(); 2360b57cec5SDimitry Andric else 2370b57cec5SDimitry Andric return cast<OutputSection>(this); 2380b57cec5SDimitry Andric return sec ? sec->getParent() : nullptr; 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric // When a section is compressed, `rawData` consists with a header followed 2420b57cec5SDimitry Andric // by zlib-compressed data. This function parses a header to initialize 2430b57cec5SDimitry Andric // `uncompressedSize` member and remove the header from `rawData`. 244d69d0756SDimitry Andric template <typename ELFT> void InputSectionBase::parseCompressedHeader() { 2450b57cec5SDimitry Andric flags &= ~(uint64_t)SHF_COMPRESSED; 2460b57cec5SDimitry Andric 247d69d0756SDimitry Andric // New-style header 248bdd1243dSDimitry Andric if (content().size() < sizeof(typename ELFT::Chdr)) { 2490b57cec5SDimitry Andric error(toString(this) + ": corrupted compressed section"); 2500b57cec5SDimitry Andric return; 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric 253bdd1243dSDimitry Andric auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content().data()); 254753f127fSDimitry Andric if (hdr->ch_type == ELFCOMPRESS_ZLIB) { 255753f127fSDimitry Andric if (!compression::zlib::isAvailable()) 256753f127fSDimitry Andric error(toString(this) + " is compressed with ELFCOMPRESS_ZLIB, but lld is " 257753f127fSDimitry Andric "not built with zlib support"); 258bdd1243dSDimitry Andric } else if (hdr->ch_type == ELFCOMPRESS_ZSTD) { 259bdd1243dSDimitry Andric if (!compression::zstd::isAvailable()) 260bdd1243dSDimitry Andric error(toString(this) + " is compressed with ELFCOMPRESS_ZSTD, but lld is " 261bdd1243dSDimitry Andric "not built with zstd support"); 262753f127fSDimitry Andric } else { 263753f127fSDimitry Andric error(toString(this) + ": unsupported compression type (" + 264753f127fSDimitry Andric Twine(hdr->ch_type) + ")"); 2650b57cec5SDimitry Andric return; 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 268bdd1243dSDimitry Andric compressed = true; 269bdd1243dSDimitry Andric compressedSize = size; 270bdd1243dSDimitry Andric size = hdr->ch_size; 271bdd1243dSDimitry Andric addralign = std::max<uint32_t>(hdr->ch_addralign, 1); 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric InputSection *InputSectionBase::getLinkOrderDep() const { 2750b57cec5SDimitry Andric assert(flags & SHF_LINK_ORDER); 276e8d8bef9SDimitry Andric if (!link) 277e8d8bef9SDimitry Andric return nullptr; 2780b57cec5SDimitry Andric return cast<InputSection>(file->getSections()[link]); 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2815f757f3fSDimitry Andric // Find a symbol that encloses a given location. 2825f757f3fSDimitry Andric Defined *InputSectionBase::getEnclosingSymbol(uint64_t offset, 2835f757f3fSDimitry Andric uint8_t type) const { 2847a6dacacSDimitry Andric if (file->isInternal()) 2857a6dacacSDimitry Andric return nullptr; 2860b57cec5SDimitry Andric for (Symbol *b : file->getSymbols()) 2870b57cec5SDimitry Andric if (Defined *d = dyn_cast<Defined>(b)) 2885f757f3fSDimitry Andric if (d->section == this && d->value <= offset && 2895f757f3fSDimitry Andric offset < d->value + d->size && (type == 0 || type == d->type)) 2900b57cec5SDimitry Andric return d; 2910b57cec5SDimitry Andric return nullptr; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 294349cc55cSDimitry Andric // Returns an object file location string. Used to construct an error message. 2955f757f3fSDimitry Andric std::string InputSectionBase::getLocation(uint64_t offset) const { 296349cc55cSDimitry Andric std::string secAndOffset = 297349cc55cSDimitry Andric (name + "+0x" + Twine::utohexstr(offset) + ")").str(); 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric // We don't have file for synthetic sections. 30004eeddc0SDimitry Andric if (file == nullptr) 301349cc55cSDimitry Andric return (config->outputFile + ":(" + secAndOffset).str(); 3020b57cec5SDimitry Andric 30304eeddc0SDimitry Andric std::string filename = toString(file); 30404eeddc0SDimitry Andric if (Defined *d = getEnclosingFunction(offset)) 30504eeddc0SDimitry Andric return filename + ":(function " + toString(*d) + ": " + secAndOffset; 3060b57cec5SDimitry Andric 30704eeddc0SDimitry Andric return filename + ":(" + secAndOffset; 3080b57cec5SDimitry Andric } 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric // This function is intended to be used for constructing an error message. 3110b57cec5SDimitry Andric // The returned message looks like this: 3120b57cec5SDimitry Andric // 3130b57cec5SDimitry Andric // foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42) 3140b57cec5SDimitry Andric // 3150b57cec5SDimitry Andric // Returns an empty string if there's no way to get line info. 3165f757f3fSDimitry Andric std::string InputSectionBase::getSrcMsg(const Symbol &sym, 3175f757f3fSDimitry Andric uint64_t offset) const { 3180b57cec5SDimitry Andric return file->getSrcMsg(sym, *this, offset); 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric // Returns a filename string along with an optional section name. This 3220b57cec5SDimitry Andric // function is intended to be used for constructing an error 3230b57cec5SDimitry Andric // message. The returned message looks like this: 3240b57cec5SDimitry Andric // 3250b57cec5SDimitry Andric // path/to/foo.o:(function bar) 3260b57cec5SDimitry Andric // 3270b57cec5SDimitry Andric // or 3280b57cec5SDimitry Andric // 3290b57cec5SDimitry Andric // path/to/foo.o:(function bar) in archive path/to/bar.a 3305f757f3fSDimitry Andric std::string InputSectionBase::getObjMsg(uint64_t off) const { 3315ffd83dbSDimitry Andric std::string filename = std::string(file->getName()); 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric std::string archive; 3340b57cec5SDimitry Andric if (!file->archiveName.empty()) 3350eae32dcSDimitry Andric archive = (" in archive " + file->archiveName).str(); 3360b57cec5SDimitry Andric 33781ad6265SDimitry Andric // Find a symbol that encloses a given location. getObjMsg may be called 338bdd1243dSDimitry Andric // before ObjFile::initSectionsAndLocalSyms where local symbols are 339bdd1243dSDimitry Andric // initialized. 3405f757f3fSDimitry Andric if (Defined *d = getEnclosingSymbol(off)) 3410b57cec5SDimitry Andric return filename + ":(" + toString(*d) + ")" + archive; 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric // If there's no symbol, print out the offset in the section. 3440b57cec5SDimitry Andric return (filename + ":(" + name + "+0x" + utohexstr(off) + ")" + archive) 3450b57cec5SDimitry Andric .str(); 3460b57cec5SDimitry Andric } 3470b57cec5SDimitry Andric 3480fca6ea1SDimitry Andric PotentialSpillSection::PotentialSpillSection(const InputSectionBase &source, 3490fca6ea1SDimitry Andric InputSectionDescription &isd) 3500fca6ea1SDimitry Andric : InputSection(source.file, source.flags, source.type, source.addralign, {}, 3510fca6ea1SDimitry Andric source.name, SectionBase::Spill), 3520fca6ea1SDimitry Andric isd(&isd) {} 3530fca6ea1SDimitry Andric 3540b57cec5SDimitry Andric InputSection InputSection::discarded(nullptr, 0, 0, 0, ArrayRef<uint8_t>(), ""); 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric InputSection::InputSection(InputFile *f, uint64_t flags, uint32_t type, 357bdd1243dSDimitry Andric uint32_t addralign, ArrayRef<uint8_t> data, 3580b57cec5SDimitry Andric StringRef name, Kind k) 3590b57cec5SDimitry Andric : InputSectionBase(f, flags, type, 360bdd1243dSDimitry Andric /*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, addralign, data, 3610fca6ea1SDimitry Andric name, k) { 3620fca6ea1SDimitry Andric assert(f || this == &InputSection::discarded); 3630fca6ea1SDimitry Andric } 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric template <class ELFT> 3660b57cec5SDimitry Andric InputSection::InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 3670b57cec5SDimitry Andric StringRef name) 3680b57cec5SDimitry Andric : InputSectionBase(f, header, name, InputSectionBase::Regular) {} 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric // Copy SHT_GROUP section contents. Used only for the -r option. 3710b57cec5SDimitry Andric template <class ELFT> void InputSection::copyShtGroup(uint8_t *buf) { 3720b57cec5SDimitry Andric // ELFT::Word is the 32-bit integral type in the target endianness. 3730b57cec5SDimitry Andric using u32 = typename ELFT::Word; 3740b57cec5SDimitry Andric ArrayRef<u32> from = getDataAs<u32>(); 3750b57cec5SDimitry Andric auto *to = reinterpret_cast<u32 *>(buf); 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric // The first entry is not a section number but a flag. 3780b57cec5SDimitry Andric *to++ = from[0]; 3790b57cec5SDimitry Andric 380e8d8bef9SDimitry Andric // Adjust section numbers because section numbers in an input object files are 381e8d8bef9SDimitry Andric // different in the output. We also need to handle combined or discarded 382e8d8bef9SDimitry Andric // members. 3830b57cec5SDimitry Andric ArrayRef<InputSectionBase *> sections = file->getSections(); 3841fd87a68SDimitry Andric DenseSet<uint32_t> seen; 385e8d8bef9SDimitry Andric for (uint32_t idx : from.slice(1)) { 386e8d8bef9SDimitry Andric OutputSection *osec = sections[idx]->getOutputSection(); 387e8d8bef9SDimitry Andric if (osec && seen.insert(osec->sectionIndex).second) 388e8d8bef9SDimitry Andric *to++ = osec->sectionIndex; 389e8d8bef9SDimitry Andric } 3900b57cec5SDimitry Andric } 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric InputSectionBase *InputSection::getRelocatedSection() const { 3930fca6ea1SDimitry Andric if (file->isInternal() || !isStaticRelSecType(type)) 3940b57cec5SDimitry Andric return nullptr; 3950b57cec5SDimitry Andric ArrayRef<InputSectionBase *> sections = file->getSections(); 3960b57cec5SDimitry Andric return sections[info]; 3970b57cec5SDimitry Andric } 3980b57cec5SDimitry Andric 3995f757f3fSDimitry Andric template <class ELFT, class RelTy> 4005f757f3fSDimitry Andric void InputSection::copyRelocations(uint8_t *buf) { 40174626c16SDimitry Andric if (config->relax && !config->relocatable && 40274626c16SDimitry Andric (config->emachine == EM_RISCV || config->emachine == EM_LOONGARCH)) { 40374626c16SDimitry Andric // On LoongArch and RISC-V, relaxation might change relocations: copy 40474626c16SDimitry Andric // from internal ones that are updated by relaxation. 4055f757f3fSDimitry Andric InputSectionBase *sec = getRelocatedSection(); 4065f757f3fSDimitry Andric copyRelocations<ELFT, RelTy>(buf, llvm::make_range(sec->relocations.begin(), 4075f757f3fSDimitry Andric sec->relocations.end())); 4085f757f3fSDimitry Andric } else { 4095f757f3fSDimitry Andric // Convert the raw relocations in the input section into Relocation objects 4105f757f3fSDimitry Andric // suitable to be used by copyRelocations below. 4115f757f3fSDimitry Andric struct MapRel { 4125f757f3fSDimitry Andric const ObjFile<ELFT> &file; 4135f757f3fSDimitry Andric Relocation operator()(const RelTy &rel) const { 4145f757f3fSDimitry Andric // RelExpr is not used so set to a dummy value. 4155f757f3fSDimitry Andric return Relocation{R_NONE, rel.getType(config->isMips64EL), rel.r_offset, 4165f757f3fSDimitry Andric getAddend<ELFT>(rel), &file.getRelocTargetSym(rel)}; 4175f757f3fSDimitry Andric } 4185f757f3fSDimitry Andric }; 4195f757f3fSDimitry Andric 4205f757f3fSDimitry Andric using RawRels = ArrayRef<RelTy>; 4215f757f3fSDimitry Andric using MapRelIter = 4225f757f3fSDimitry Andric llvm::mapped_iterator<typename RawRels::iterator, MapRel>; 4235f757f3fSDimitry Andric auto mapRel = MapRel{*getFile<ELFT>()}; 4245f757f3fSDimitry Andric RawRels rawRels = getDataAs<RelTy>(); 4255f757f3fSDimitry Andric auto rels = llvm::make_range(MapRelIter(rawRels.begin(), mapRel), 4265f757f3fSDimitry Andric MapRelIter(rawRels.end(), mapRel)); 4275f757f3fSDimitry Andric copyRelocations<ELFT, RelTy>(buf, rels); 4285f757f3fSDimitry Andric } 4295f757f3fSDimitry Andric } 4305f757f3fSDimitry Andric 4310b57cec5SDimitry Andric // This is used for -r and --emit-relocs. We can't use memcpy to copy 4320b57cec5SDimitry Andric // relocations because we need to update symbol table offset and section index 4330b57cec5SDimitry Andric // for each relocation. So we copy relocations one by one. 4345f757f3fSDimitry Andric template <class ELFT, class RelTy, class RelIt> 4355f757f3fSDimitry Andric void InputSection::copyRelocations(uint8_t *buf, 4365f757f3fSDimitry Andric llvm::iterator_range<RelIt> rels) { 4370eae32dcSDimitry Andric const TargetInfo &target = *elf::target; 4380b57cec5SDimitry Andric InputSectionBase *sec = getRelocatedSection(); 439bdd1243dSDimitry Andric (void)sec->contentMaybeDecompress(); // uncompress if needed 4400b57cec5SDimitry Andric 4415f757f3fSDimitry Andric for (const Relocation &rel : rels) { 4425f757f3fSDimitry Andric RelType type = rel.type; 4430b57cec5SDimitry Andric const ObjFile<ELFT> *file = getFile<ELFT>(); 4445f757f3fSDimitry Andric Symbol &sym = *rel.sym; 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric auto *p = reinterpret_cast<typename ELFT::Rela *>(buf); 4470b57cec5SDimitry Andric buf += sizeof(RelTy); 4480b57cec5SDimitry Andric 4490fca6ea1SDimitry Andric if (RelTy::HasAddend) 4505f757f3fSDimitry Andric p->r_addend = rel.addend; 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric // Output section VA is zero for -r, so r_offset is an offset within the 453480093f4SDimitry Andric // section, but for --emit-relocs it is a virtual address. 4545f757f3fSDimitry Andric p->r_offset = sec->getVA(rel.offset); 4550fca6ea1SDimitry Andric p->setSymbolAndType(in.symTab->getSymbolIndex(sym), type, 4560b57cec5SDimitry Andric config->isMips64EL); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric if (sym.type == STT_SECTION) { 4590b57cec5SDimitry Andric // We combine multiple section symbols into only one per 4600b57cec5SDimitry Andric // section. This means we have to update the addend. That is 4610b57cec5SDimitry Andric // trivial for Elf_Rela, but for Elf_Rel we have to write to the 4620b57cec5SDimitry Andric // section data. We do that by adding to the Relocation vector. 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric // .eh_frame is horribly special and can reference discarded sections. To 4650b57cec5SDimitry Andric // avoid having to parse and recreate .eh_frame, we just replace any 4660b57cec5SDimitry Andric // relocation in it pointing to discarded sections with R_*_NONE, which 4670b57cec5SDimitry Andric // hopefully creates a frame that is ignored at runtime. Also, don't warn 4680b57cec5SDimitry Andric // on .gcc_except_table and debug sections. 4690b57cec5SDimitry Andric // 4705a0c326fSDimitry Andric // See the comment in maybeReportUndefined for PPC32 .got2 and PPC64 .toc 4710b57cec5SDimitry Andric auto *d = dyn_cast<Defined>(&sym); 4720b57cec5SDimitry Andric if (!d) { 473d65cd7a5SDimitry Andric if (!isDebugSection(*sec) && sec->name != ".eh_frame" && 4745a0c326fSDimitry Andric sec->name != ".gcc_except_table" && sec->name != ".got2" && 4755a0c326fSDimitry Andric sec->name != ".toc") { 4760b57cec5SDimitry Andric uint32_t secIdx = cast<Undefined>(sym).discardedSecIdx; 4770eae32dcSDimitry Andric Elf_Shdr_Impl<ELFT> sec = file->template getELFShdrs<ELFT>()[secIdx]; 4780b57cec5SDimitry Andric warn("relocation refers to a discarded section: " + 479e8d8bef9SDimitry Andric CHECK(file->getObj().getSectionName(sec), file) + 4800b57cec5SDimitry Andric "\n>>> referenced by " + getObjMsg(p->r_offset)); 4810b57cec5SDimitry Andric } 4820b57cec5SDimitry Andric p->setSymbolAndType(0, 0, false); 4830b57cec5SDimitry Andric continue; 4840b57cec5SDimitry Andric } 4850eae32dcSDimitry Andric SectionBase *section = d->section; 4865f757f3fSDimitry Andric assert(section->isLive()); 4870b57cec5SDimitry Andric 4885f757f3fSDimitry Andric int64_t addend = rel.addend; 4895f757f3fSDimitry Andric const uint8_t *bufLoc = sec->content().begin() + rel.offset; 4900fca6ea1SDimitry Andric if (!RelTy::HasAddend) 4910eae32dcSDimitry Andric addend = target.getImplicitAddend(bufLoc, type); 4920b57cec5SDimitry Andric 4935ffd83dbSDimitry Andric if (config->emachine == EM_MIPS && 4940eae32dcSDimitry Andric target.getRelExpr(type, sym, bufLoc) == R_MIPS_GOTREL) { 4950b57cec5SDimitry Andric // Some MIPS relocations depend on "gp" value. By default, 4960b57cec5SDimitry Andric // this value has 0x7ff0 offset from a .got section. But 497480093f4SDimitry Andric // relocatable files produced by a compiler or a linker 4980b57cec5SDimitry Andric // might redefine this default value and we must use it 4990b57cec5SDimitry Andric // for a calculation of the relocation result. When we 5000b57cec5SDimitry Andric // generate EXE or DSO it's trivial. Generating a relocatable 5010b57cec5SDimitry Andric // output is more difficult case because the linker does 5020b57cec5SDimitry Andric // not calculate relocations in this mode and loses 5030b57cec5SDimitry Andric // individual "gp" values used by each input object file. 5040b57cec5SDimitry Andric // As a workaround we add the "gp" value to the relocation 5050b57cec5SDimitry Andric // addend and save it back to the file. 5060b57cec5SDimitry Andric addend += sec->getFile<ELFT>()->mipsGp0; 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric 5090fca6ea1SDimitry Andric if (RelTy::HasAddend) 5100b57cec5SDimitry Andric p->r_addend = sym.getVA(addend) - section->getOutputSection()->addr; 5115f757f3fSDimitry Andric // For SHF_ALLOC sections relocated by REL, append a relocation to 5125f757f3fSDimitry Andric // sec->relocations so that relocateAlloc transitively called by 5135f757f3fSDimitry Andric // writeSections will update the implicit addend. Non-SHF_ALLOC sections 5145f757f3fSDimitry Andric // utilize relocateNonAlloc to process raw relocations and do not need 5155f757f3fSDimitry Andric // this sec->relocations change. 5165f757f3fSDimitry Andric else if (config->relocatable && (sec->flags & SHF_ALLOC) && 5175f757f3fSDimitry Andric type != target.noneRel) 5185f757f3fSDimitry Andric sec->addReloc({R_ABS, type, rel.offset, addend, &sym}); 51913138422SDimitry Andric } else if (config->emachine == EM_PPC && type == R_PPC_PLTREL24 && 5200eae32dcSDimitry Andric p->r_addend >= 0x8000 && sec->file->ppc32Got2) { 52113138422SDimitry Andric // Similar to R_MIPS_GPREL{16,32}. If the addend of R_PPC_PLTREL24 52213138422SDimitry Andric // indicates that r30 is relative to the input section .got2 52313138422SDimitry Andric // (r_addend>=0x8000), after linking, r30 should be relative to the output 52413138422SDimitry Andric // section .got2 . To compensate for the shift, adjust r_addend by 5250eae32dcSDimitry Andric // ppc32Got->outSecOff. 5260eae32dcSDimitry Andric p->r_addend += sec->file->ppc32Got2->outSecOff; 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric // The ARM and AArch64 ABI handle pc-relative relocations to undefined weak 5320b57cec5SDimitry Andric // references specially. The general rule is that the value of the symbol in 5330b57cec5SDimitry Andric // this context is the address of the place P. A further special case is that 5340b57cec5SDimitry Andric // branch relocations to an undefined weak reference resolve to the next 5350b57cec5SDimitry Andric // instruction. 5360b57cec5SDimitry Andric static uint32_t getARMUndefinedRelativeWeakVA(RelType type, uint32_t a, 5370b57cec5SDimitry Andric uint32_t p) { 5380b57cec5SDimitry Andric switch (type) { 5390b57cec5SDimitry Andric // Unresolved branch relocations to weak references resolve to next 5400b57cec5SDimitry Andric // instruction, this will be either 2 or 4 bytes on from P. 541349cc55cSDimitry Andric case R_ARM_THM_JUMP8: 5420b57cec5SDimitry Andric case R_ARM_THM_JUMP11: 5430b57cec5SDimitry Andric return p + 2 + a; 5440b57cec5SDimitry Andric case R_ARM_CALL: 5450b57cec5SDimitry Andric case R_ARM_JUMP24: 5460b57cec5SDimitry Andric case R_ARM_PC24: 5470b57cec5SDimitry Andric case R_ARM_PLT32: 5480b57cec5SDimitry Andric case R_ARM_PREL31: 5490b57cec5SDimitry Andric case R_ARM_THM_JUMP19: 5500b57cec5SDimitry Andric case R_ARM_THM_JUMP24: 5510b57cec5SDimitry Andric return p + 4 + a; 5520b57cec5SDimitry Andric case R_ARM_THM_CALL: 5530b57cec5SDimitry Andric // We don't want an interworking BLX to ARM 5540b57cec5SDimitry Andric return p + 5 + a; 5550b57cec5SDimitry Andric // Unresolved non branch pc-relative relocations 5560b57cec5SDimitry Andric // R_ARM_TARGET2 which can be resolved relatively is not present as it never 5570b57cec5SDimitry Andric // targets a weak-reference. 5580b57cec5SDimitry Andric case R_ARM_MOVW_PREL_NC: 5590b57cec5SDimitry Andric case R_ARM_MOVT_PREL: 5600b57cec5SDimitry Andric case R_ARM_REL32: 5615ffd83dbSDimitry Andric case R_ARM_THM_ALU_PREL_11_0: 5620b57cec5SDimitry Andric case R_ARM_THM_MOVW_PREL_NC: 5630b57cec5SDimitry Andric case R_ARM_THM_MOVT_PREL: 5645ffd83dbSDimitry Andric case R_ARM_THM_PC12: 5650b57cec5SDimitry Andric return p + a; 5665ffd83dbSDimitry Andric // p + a is unrepresentable as negative immediates can't be encoded. 5675ffd83dbSDimitry Andric case R_ARM_THM_PC8: 5685ffd83dbSDimitry Andric return p; 5690b57cec5SDimitry Andric } 5700b57cec5SDimitry Andric llvm_unreachable("ARM pc-relative relocation expected\n"); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric // The comment above getARMUndefinedRelativeWeakVA applies to this function. 574fe6060f1SDimitry Andric static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t type, uint64_t p) { 5750b57cec5SDimitry Andric switch (type) { 5760b57cec5SDimitry Andric // Unresolved branch relocations to weak references resolve to next 5770b57cec5SDimitry Andric // instruction, this is 4 bytes on from P. 5780b57cec5SDimitry Andric case R_AARCH64_CALL26: 5790b57cec5SDimitry Andric case R_AARCH64_CONDBR19: 5800b57cec5SDimitry Andric case R_AARCH64_JUMP26: 5810b57cec5SDimitry Andric case R_AARCH64_TSTBR14: 582fe6060f1SDimitry Andric return p + 4; 5830b57cec5SDimitry Andric // Unresolved non branch pc-relative relocations 5840b57cec5SDimitry Andric case R_AARCH64_PREL16: 5850b57cec5SDimitry Andric case R_AARCH64_PREL32: 5860b57cec5SDimitry Andric case R_AARCH64_PREL64: 5870b57cec5SDimitry Andric case R_AARCH64_ADR_PREL_LO21: 5880b57cec5SDimitry Andric case R_AARCH64_LD_PREL_LO19: 5895ffd83dbSDimitry Andric case R_AARCH64_PLT32: 590fe6060f1SDimitry Andric return p; 5910b57cec5SDimitry Andric } 5920b57cec5SDimitry Andric llvm_unreachable("AArch64 pc-relative relocation expected\n"); 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric 595fe6060f1SDimitry Andric static uint64_t getRISCVUndefinedRelativeWeakVA(uint64_t type, uint64_t p) { 596fe6060f1SDimitry Andric switch (type) { 597fe6060f1SDimitry Andric case R_RISCV_BRANCH: 598fe6060f1SDimitry Andric case R_RISCV_JAL: 599fe6060f1SDimitry Andric case R_RISCV_CALL: 600fe6060f1SDimitry Andric case R_RISCV_CALL_PLT: 601fe6060f1SDimitry Andric case R_RISCV_RVC_BRANCH: 602fe6060f1SDimitry Andric case R_RISCV_RVC_JUMP: 60306c3fb27SDimitry Andric case R_RISCV_PLT32: 604fe6060f1SDimitry Andric return p; 605fe6060f1SDimitry Andric default: 606fe6060f1SDimitry Andric return 0; 607fe6060f1SDimitry Andric } 608fe6060f1SDimitry Andric } 609fe6060f1SDimitry Andric 6100b57cec5SDimitry Andric // ARM SBREL relocations are of the form S + A - B where B is the static base 6110b57cec5SDimitry Andric // The ARM ABI defines base to be "addressing origin of the output segment 6120b57cec5SDimitry Andric // defining the symbol S". We defined the "addressing origin"/static base to be 6130b57cec5SDimitry Andric // the base of the PT_LOAD segment containing the Sym. 6140b57cec5SDimitry Andric // The procedure call standard only defines a Read Write Position Independent 6150b57cec5SDimitry Andric // RWPI variant so in practice we should expect the static base to be the base 6160b57cec5SDimitry Andric // of the RW segment. 6170b57cec5SDimitry Andric static uint64_t getARMStaticBase(const Symbol &sym) { 6180b57cec5SDimitry Andric OutputSection *os = sym.getOutputSection(); 6190b57cec5SDimitry Andric if (!os || !os->ptLoad || !os->ptLoad->firstSec) 6200b57cec5SDimitry Andric fatal("SBREL relocation to " + sym.getName() + " without static base"); 6210b57cec5SDimitry Andric return os->ptLoad->firstSec->addr; 6220b57cec5SDimitry Andric } 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric // For R_RISCV_PC_INDIRECT (R_RISCV_PCREL_LO12_{I,S}), the symbol actually 6250b57cec5SDimitry Andric // points the corresponding R_RISCV_PCREL_HI20 relocation, and the target VA 6260b57cec5SDimitry Andric // is calculated using PCREL_HI20's symbol. 6270b57cec5SDimitry Andric // 6280b57cec5SDimitry Andric // This function returns the R_RISCV_PCREL_HI20 relocation from 6290b57cec5SDimitry Andric // R_RISCV_PCREL_LO12's symbol and addend. 6300b57cec5SDimitry Andric static Relocation *getRISCVPCRelHi20(const Symbol *sym, uint64_t addend) { 6310b57cec5SDimitry Andric const Defined *d = cast<Defined>(sym); 6320b57cec5SDimitry Andric if (!d->section) { 63381ad6265SDimitry Andric errorOrWarn("R_RISCV_PCREL_LO12 relocation points to an absolute symbol: " + 6340b57cec5SDimitry Andric sym->getName()); 6350b57cec5SDimitry Andric return nullptr; 6360b57cec5SDimitry Andric } 6370b57cec5SDimitry Andric InputSection *isec = cast<InputSection>(d->section); 6380b57cec5SDimitry Andric 6390b57cec5SDimitry Andric if (addend != 0) 64004eeddc0SDimitry Andric warn("non-zero addend in R_RISCV_PCREL_LO12 relocation to " + 6410b57cec5SDimitry Andric isec->getObjMsg(d->value) + " is ignored"); 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric // Relocations are sorted by offset, so we can use std::equal_range to do 6440b57cec5SDimitry Andric // binary search. 6450b57cec5SDimitry Andric Relocation r; 6460b57cec5SDimitry Andric r.offset = d->value; 6470b57cec5SDimitry Andric auto range = 648bdd1243dSDimitry Andric std::equal_range(isec->relocs().begin(), isec->relocs().end(), r, 6490b57cec5SDimitry Andric [](const Relocation &lhs, const Relocation &rhs) { 6500b57cec5SDimitry Andric return lhs.offset < rhs.offset; 6510b57cec5SDimitry Andric }); 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric for (auto it = range.first; it != range.second; ++it) 6540b57cec5SDimitry Andric if (it->type == R_RISCV_PCREL_HI20 || it->type == R_RISCV_GOT_HI20 || 6550b57cec5SDimitry Andric it->type == R_RISCV_TLS_GD_HI20 || it->type == R_RISCV_TLS_GOT_HI20) 6560b57cec5SDimitry Andric return &*it; 6570b57cec5SDimitry Andric 65881ad6265SDimitry Andric errorOrWarn("R_RISCV_PCREL_LO12 relocation points to " + 65981ad6265SDimitry Andric isec->getObjMsg(d->value) + 6600b57cec5SDimitry Andric " without an associated R_RISCV_PCREL_HI20 relocation"); 6610b57cec5SDimitry Andric return nullptr; 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric // A TLS symbol's virtual address is relative to the TLS segment. Add a 6650b57cec5SDimitry Andric // target-specific adjustment to produce a thread-pointer-relative offset. 6660b57cec5SDimitry Andric static int64_t getTlsTpOffset(const Symbol &s) { 6670b57cec5SDimitry Andric // On targets that support TLSDESC, _TLS_MODULE_BASE_@tpoff = 0. 6680b57cec5SDimitry Andric if (&s == ElfSym::tlsModuleBase) 6690b57cec5SDimitry Andric return 0; 6700b57cec5SDimitry Andric 67185868e8aSDimitry Andric // There are 2 TLS layouts. Among targets we support, x86 uses TLS Variant 2 67285868e8aSDimitry Andric // while most others use Variant 1. At run time TP will be aligned to p_align. 67385868e8aSDimitry Andric 67485868e8aSDimitry Andric // Variant 1. TP will be followed by an optional gap (which is the size of 2 67585868e8aSDimitry Andric // pointers on ARM/AArch64, 0 on other targets), followed by alignment 67685868e8aSDimitry Andric // padding, then the static TLS blocks. The alignment padding is added so that 67785868e8aSDimitry Andric // (TP + gap + padding) is congruent to p_vaddr modulo p_align. 67885868e8aSDimitry Andric // 67985868e8aSDimitry Andric // Variant 2. Static TLS blocks, followed by alignment padding are placed 68085868e8aSDimitry Andric // before TP. The alignment padding is added so that (TP - padding - 68185868e8aSDimitry Andric // p_memsz) is congruent to p_vaddr modulo p_align. 68285868e8aSDimitry Andric PhdrEntry *tls = Out::tlsPhdr; 6830b57cec5SDimitry Andric switch (config->emachine) { 68485868e8aSDimitry Andric // Variant 1. 6850b57cec5SDimitry Andric case EM_ARM: 6860b57cec5SDimitry Andric case EM_AARCH64: 68785868e8aSDimitry Andric return s.getVA(0) + config->wordsize * 2 + 68885868e8aSDimitry Andric ((tls->p_vaddr - config->wordsize * 2) & (tls->p_align - 1)); 68985868e8aSDimitry Andric case EM_MIPS: 6900b57cec5SDimitry Andric case EM_PPC: 6910b57cec5SDimitry Andric case EM_PPC64: 69285868e8aSDimitry Andric // Adjusted Variant 1. TP is placed with a displacement of 0x7000, which is 69385868e8aSDimitry Andric // to allow a signed 16-bit offset to reach 0x1000 of TCB/thread-library 69485868e8aSDimitry Andric // data and 0xf000 of the program's TLS segment. 69585868e8aSDimitry Andric return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1)) - 0x7000; 69606c3fb27SDimitry Andric case EM_LOONGARCH: 6970b57cec5SDimitry Andric case EM_RISCV: 6980fca6ea1SDimitry Andric // See the comment in handleTlsRelocation. For TLSDESC=>IE, 6990fca6ea1SDimitry Andric // R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12_I,CALL} also reach here. While 7000fca6ea1SDimitry Andric // `tls` may be null, the return value is ignored. 7010fca6ea1SDimitry Andric if (s.type != STT_TLS) 7020fca6ea1SDimitry Andric return 0; 70385868e8aSDimitry Andric return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1)); 70485868e8aSDimitry Andric 70585868e8aSDimitry Andric // Variant 2. 706480093f4SDimitry Andric case EM_HEXAGON: 70774626c16SDimitry Andric case EM_S390: 7085ffd83dbSDimitry Andric case EM_SPARCV9: 70985868e8aSDimitry Andric case EM_386: 71085868e8aSDimitry Andric case EM_X86_64: 71185868e8aSDimitry Andric return s.getVA(0) - tls->p_memsz - 71285868e8aSDimitry Andric ((-tls->p_vaddr - tls->p_memsz) & (tls->p_align - 1)); 7130b57cec5SDimitry Andric default: 7140b57cec5SDimitry Andric llvm_unreachable("unhandled Config->EMachine"); 7150b57cec5SDimitry Andric } 7160b57cec5SDimitry Andric } 7170b57cec5SDimitry Andric 7185ffd83dbSDimitry Andric uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type, 7195ffd83dbSDimitry Andric int64_t a, uint64_t p, 7205ffd83dbSDimitry Andric const Symbol &sym, RelExpr expr) { 7210b57cec5SDimitry Andric switch (expr) { 7220b57cec5SDimitry Andric case R_ABS: 7230b57cec5SDimitry Andric case R_DTPREL: 7240b57cec5SDimitry Andric case R_RELAX_TLS_LD_TO_LE_ABS: 7250b57cec5SDimitry Andric case R_RELAX_GOT_PC_NOPIC: 7260fca6ea1SDimitry Andric case R_AARCH64_AUTH: 7270b57cec5SDimitry Andric case R_RISCV_ADD: 7281db9f3b2SDimitry Andric case R_RISCV_LEB128: 7290b57cec5SDimitry Andric return sym.getVA(a); 7300b57cec5SDimitry Andric case R_ADDEND: 7310b57cec5SDimitry Andric return a; 732753f127fSDimitry Andric case R_RELAX_HINT: 733753f127fSDimitry Andric return 0; 7340b57cec5SDimitry Andric case R_ARM_SBREL: 7350b57cec5SDimitry Andric return sym.getVA(a) - getARMStaticBase(sym); 7360b57cec5SDimitry Andric case R_GOT: 7370b57cec5SDimitry Andric case R_RELAX_TLS_GD_TO_IE_ABS: 7380b57cec5SDimitry Andric return sym.getGotVA() + a; 73906c3fb27SDimitry Andric case R_LOONGARCH_GOT: 74006c3fb27SDimitry Andric // The LoongArch TLS GD relocs reuse the R_LARCH_GOT_PC_LO12 reloc type 74106c3fb27SDimitry Andric // for their page offsets. The arithmetics are different in the TLS case 74206c3fb27SDimitry Andric // so we have to duplicate some logic here. 74306c3fb27SDimitry Andric if (sym.hasFlag(NEEDS_TLSGD) && type != R_LARCH_TLS_IE_PC_LO12) 74406c3fb27SDimitry Andric // Like R_LOONGARCH_TLSGD_PAGE_PC but taking the absolute value. 74506c3fb27SDimitry Andric return in.got->getGlobalDynAddr(sym) + a; 74606c3fb27SDimitry Andric return getRelocTargetVA(file, type, a, p, sym, R_GOT); 7470b57cec5SDimitry Andric case R_GOTONLY_PC: 7480b57cec5SDimitry Andric return in.got->getVA() + a - p; 7490b57cec5SDimitry Andric case R_GOTPLTONLY_PC: 7500b57cec5SDimitry Andric return in.gotPlt->getVA() + a - p; 7510b57cec5SDimitry Andric case R_GOTREL: 7520b57cec5SDimitry Andric case R_PPC64_RELAX_TOC: 7530b57cec5SDimitry Andric return sym.getVA(a) - in.got->getVA(); 7540b57cec5SDimitry Andric case R_GOTPLTREL: 7550b57cec5SDimitry Andric return sym.getVA(a) - in.gotPlt->getVA(); 7560b57cec5SDimitry Andric case R_GOTPLT: 7570b57cec5SDimitry Andric case R_RELAX_TLS_GD_TO_IE_GOTPLT: 7580b57cec5SDimitry Andric return sym.getGotVA() + a - in.gotPlt->getVA(); 7590b57cec5SDimitry Andric case R_TLSLD_GOT_OFF: 7600b57cec5SDimitry Andric case R_GOT_OFF: 7610b57cec5SDimitry Andric case R_RELAX_TLS_GD_TO_IE_GOT_OFF: 7620b57cec5SDimitry Andric return sym.getGotOffset() + a; 7630b57cec5SDimitry Andric case R_AARCH64_GOT_PAGE_PC: 7640b57cec5SDimitry Andric case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC: 7650b57cec5SDimitry Andric return getAArch64Page(sym.getGotVA() + a) - getAArch64Page(p); 766e8d8bef9SDimitry Andric case R_AARCH64_GOT_PAGE: 767e8d8bef9SDimitry Andric return sym.getGotVA() + a - getAArch64Page(in.got->getVA()); 7680b57cec5SDimitry Andric case R_GOT_PC: 7690b57cec5SDimitry Andric case R_RELAX_TLS_GD_TO_IE: 7700b57cec5SDimitry Andric return sym.getGotVA() + a - p; 77174626c16SDimitry Andric case R_GOTPLT_GOTREL: 77274626c16SDimitry Andric return sym.getGotPltVA() + a - in.got->getVA(); 77374626c16SDimitry Andric case R_GOTPLT_PC: 77474626c16SDimitry Andric return sym.getGotPltVA() + a - p; 77506c3fb27SDimitry Andric case R_LOONGARCH_GOT_PAGE_PC: 77606c3fb27SDimitry Andric if (sym.hasFlag(NEEDS_TLSGD)) 777297eecfbSDimitry Andric return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p, type); 778297eecfbSDimitry Andric return getLoongArchPageDelta(sym.getGotVA() + a, p, type); 7790b57cec5SDimitry Andric case R_MIPS_GOTREL: 7800b57cec5SDimitry Andric return sym.getVA(a) - in.mipsGot->getGp(file); 7810b57cec5SDimitry Andric case R_MIPS_GOT_GP: 7820b57cec5SDimitry Andric return in.mipsGot->getGp(file) + a; 7830b57cec5SDimitry Andric case R_MIPS_GOT_GP_PC: { 7840b57cec5SDimitry Andric // R_MIPS_LO16 expression has R_MIPS_GOT_GP_PC type iif the target 7850b57cec5SDimitry Andric // is _gp_disp symbol. In that case we should use the following 7860b57cec5SDimitry Andric // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at 7870b57cec5SDimitry Andric // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 7880b57cec5SDimitry Andric // microMIPS variants of these relocations use slightly different 7890b57cec5SDimitry Andric // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi() 7905ffd83dbSDimitry Andric // to correctly handle less-significant bit of the microMIPS symbol. 7910b57cec5SDimitry Andric uint64_t v = in.mipsGot->getGp(file) + a - p; 7920b57cec5SDimitry Andric if (type == R_MIPS_LO16 || type == R_MICROMIPS_LO16) 7930b57cec5SDimitry Andric v += 4; 7940b57cec5SDimitry Andric if (type == R_MICROMIPS_LO16 || type == R_MICROMIPS_HI16) 7950b57cec5SDimitry Andric v -= 1; 7960b57cec5SDimitry Andric return v; 7970b57cec5SDimitry Andric } 7980b57cec5SDimitry Andric case R_MIPS_GOT_LOCAL_PAGE: 7990b57cec5SDimitry Andric // If relocation against MIPS local symbol requires GOT entry, this entry 8000b57cec5SDimitry Andric // should be initialized by 'page address'. This address is high 16-bits 8010b57cec5SDimitry Andric // of sum the symbol's value and the addend. 8020b57cec5SDimitry Andric return in.mipsGot->getVA() + in.mipsGot->getPageEntryOffset(file, sym, a) - 8030b57cec5SDimitry Andric in.mipsGot->getGp(file); 8040b57cec5SDimitry Andric case R_MIPS_GOT_OFF: 8050b57cec5SDimitry Andric case R_MIPS_GOT_OFF32: 8060b57cec5SDimitry Andric // In case of MIPS if a GOT relocation has non-zero addend this addend 8070b57cec5SDimitry Andric // should be applied to the GOT entry content not to the GOT entry offset. 8080b57cec5SDimitry Andric // That is why we use separate expression type. 8090b57cec5SDimitry Andric return in.mipsGot->getVA() + in.mipsGot->getSymEntryOffset(file, sym, a) - 8100b57cec5SDimitry Andric in.mipsGot->getGp(file); 8110b57cec5SDimitry Andric case R_MIPS_TLSGD: 8120b57cec5SDimitry Andric return in.mipsGot->getVA() + in.mipsGot->getGlobalDynOffset(file, sym) - 8130b57cec5SDimitry Andric in.mipsGot->getGp(file); 8140b57cec5SDimitry Andric case R_MIPS_TLSLD: 8150b57cec5SDimitry Andric return in.mipsGot->getVA() + in.mipsGot->getTlsIndexOffset(file) - 8160b57cec5SDimitry Andric in.mipsGot->getGp(file); 8170b57cec5SDimitry Andric case R_AARCH64_PAGE_PC: { 8180b57cec5SDimitry Andric uint64_t val = sym.isUndefWeak() ? p + a : sym.getVA(a); 8190b57cec5SDimitry Andric return getAArch64Page(val) - getAArch64Page(p); 8200b57cec5SDimitry Andric } 8210b57cec5SDimitry Andric case R_RISCV_PC_INDIRECT: { 8220b57cec5SDimitry Andric if (const Relocation *hiRel = getRISCVPCRelHi20(&sym, a)) 8230b57cec5SDimitry Andric return getRelocTargetVA(file, hiRel->type, hiRel->addend, sym.getVA(), 8240b57cec5SDimitry Andric *hiRel->sym, hiRel->expr); 8250b57cec5SDimitry Andric return 0; 8260b57cec5SDimitry Andric } 82706c3fb27SDimitry Andric case R_LOONGARCH_PAGE_PC: 828297eecfbSDimitry Andric return getLoongArchPageDelta(sym.getVA(a), p, type); 8295ffd83dbSDimitry Andric case R_PC: 8305ffd83dbSDimitry Andric case R_ARM_PCA: { 8310b57cec5SDimitry Andric uint64_t dest; 8325ffd83dbSDimitry Andric if (expr == R_ARM_PCA) 8335ffd83dbSDimitry Andric // Some PC relative ARM (Thumb) relocations align down the place. 8345ffd83dbSDimitry Andric p = p & 0xfffffffc; 8352a66634dSDimitry Andric if (sym.isUndefined()) { 836fe6060f1SDimitry Andric // On ARM and AArch64 a branch to an undefined weak resolves to the next 83706c3fb27SDimitry Andric // instruction, otherwise the place. On RISC-V, resolve an undefined weak 838fe6060f1SDimitry Andric // to the same instruction to cause an infinite loop (making the user 839fe6060f1SDimitry Andric // aware of the issue) while ensuring no overflow. 8402a66634dSDimitry Andric // Note: if the symbol is hidden, its binding has been converted to local, 8412a66634dSDimitry Andric // so we just check isUndefined() here. 8420b57cec5SDimitry Andric if (config->emachine == EM_ARM) 8430b57cec5SDimitry Andric dest = getARMUndefinedRelativeWeakVA(type, a, p); 8440b57cec5SDimitry Andric else if (config->emachine == EM_AARCH64) 845fe6060f1SDimitry Andric dest = getAArch64UndefinedRelativeWeakVA(type, p) + a; 8460b57cec5SDimitry Andric else if (config->emachine == EM_PPC) 8470b57cec5SDimitry Andric dest = p; 848fe6060f1SDimitry Andric else if (config->emachine == EM_RISCV) 849fe6060f1SDimitry Andric dest = getRISCVUndefinedRelativeWeakVA(type, p) + a; 8500b57cec5SDimitry Andric else 8510b57cec5SDimitry Andric dest = sym.getVA(a); 8520b57cec5SDimitry Andric } else { 8530b57cec5SDimitry Andric dest = sym.getVA(a); 8540b57cec5SDimitry Andric } 8550b57cec5SDimitry Andric return dest - p; 8560b57cec5SDimitry Andric } 8570b57cec5SDimitry Andric case R_PLT: 8580b57cec5SDimitry Andric return sym.getPltVA() + a; 8590b57cec5SDimitry Andric case R_PLT_PC: 8600b57cec5SDimitry Andric case R_PPC64_CALL_PLT: 8610b57cec5SDimitry Andric return sym.getPltVA() + a - p; 86206c3fb27SDimitry Andric case R_LOONGARCH_PLT_PAGE_PC: 863297eecfbSDimitry Andric return getLoongArchPageDelta(sym.getPltVA() + a, p, type); 864349cc55cSDimitry Andric case R_PLT_GOTPLT: 865349cc55cSDimitry Andric return sym.getPltVA() + a - in.gotPlt->getVA(); 86674626c16SDimitry Andric case R_PLT_GOTREL: 86774626c16SDimitry Andric return sym.getPltVA() + a - in.got->getVA(); 8680b57cec5SDimitry Andric case R_PPC32_PLTREL: 8690b57cec5SDimitry Andric // R_PPC_PLTREL24 uses the addend (usually 0 or 0x8000) to indicate r30 8700b57cec5SDimitry Andric // stores _GLOBAL_OFFSET_TABLE_ or .got2+0x8000. The addend is ignored for 871480093f4SDimitry Andric // target VA computation. 8720b57cec5SDimitry Andric return sym.getPltVA() - p; 8730b57cec5SDimitry Andric case R_PPC64_CALL: { 8740b57cec5SDimitry Andric uint64_t symVA = sym.getVA(a); 8750b57cec5SDimitry Andric // If we have an undefined weak symbol, we might get here with a symbol 8760b57cec5SDimitry Andric // address of zero. That could overflow, but the code must be unreachable, 8770b57cec5SDimitry Andric // so don't bother doing anything at all. 8780b57cec5SDimitry Andric if (!symVA) 8790b57cec5SDimitry Andric return 0; 8800b57cec5SDimitry Andric 8810b57cec5SDimitry Andric // PPC64 V2 ABI describes two entry points to a function. The global entry 8820b57cec5SDimitry Andric // point is used for calls where the caller and callee (may) have different 8830b57cec5SDimitry Andric // TOC base pointers and r2 needs to be modified to hold the TOC base for 8840b57cec5SDimitry Andric // the callee. For local calls the caller and callee share the same 8850b57cec5SDimitry Andric // TOC base and so the TOC pointer initialization code should be skipped by 8860b57cec5SDimitry Andric // branching to the local entry point. 8870b57cec5SDimitry Andric return symVA - p + getPPC64GlobalEntryToLocalEntryOffset(sym.stOther); 8880b57cec5SDimitry Andric } 8890b57cec5SDimitry Andric case R_PPC64_TOCBASE: 8900b57cec5SDimitry Andric return getPPC64TocBase() + a; 8910b57cec5SDimitry Andric case R_RELAX_GOT_PC: 892e8d8bef9SDimitry Andric case R_PPC64_RELAX_GOT_PC: 8930b57cec5SDimitry Andric return sym.getVA(a) - p; 8940b57cec5SDimitry Andric case R_RELAX_TLS_GD_TO_LE: 8950b57cec5SDimitry Andric case R_RELAX_TLS_IE_TO_LE: 8960b57cec5SDimitry Andric case R_RELAX_TLS_LD_TO_LE: 897e8d8bef9SDimitry Andric case R_TPREL: 8980b57cec5SDimitry Andric // It is not very clear what to return if the symbol is undefined. With 8990b57cec5SDimitry Andric // --noinhibit-exec, even a non-weak undefined reference may reach here. 9000b57cec5SDimitry Andric // Just return A, which matches R_ABS, and the behavior of some dynamic 9010b57cec5SDimitry Andric // loaders. 902349cc55cSDimitry Andric if (sym.isUndefined()) 9030b57cec5SDimitry Andric return a; 9040b57cec5SDimitry Andric return getTlsTpOffset(sym) + a; 9050b57cec5SDimitry Andric case R_RELAX_TLS_GD_TO_LE_NEG: 906e8d8bef9SDimitry Andric case R_TPREL_NEG: 9070b57cec5SDimitry Andric if (sym.isUndefined()) 9080b57cec5SDimitry Andric return a; 9090b57cec5SDimitry Andric return -getTlsTpOffset(sym) + a; 9100b57cec5SDimitry Andric case R_SIZE: 9110b57cec5SDimitry Andric return sym.getSize() + a; 9120b57cec5SDimitry Andric case R_TLSDESC: 91304eeddc0SDimitry Andric return in.got->getTlsDescAddr(sym) + a; 9140b57cec5SDimitry Andric case R_TLSDESC_PC: 91504eeddc0SDimitry Andric return in.got->getTlsDescAddr(sym) + a - p; 916349cc55cSDimitry Andric case R_TLSDESC_GOTPLT: 91704eeddc0SDimitry Andric return in.got->getTlsDescAddr(sym) + a - in.gotPlt->getVA(); 9180b57cec5SDimitry Andric case R_AARCH64_TLSDESC_PAGE: 91904eeddc0SDimitry Andric return getAArch64Page(in.got->getTlsDescAddr(sym) + a) - getAArch64Page(p); 9200fca6ea1SDimitry Andric case R_LOONGARCH_TLSDESC_PAGE_PC: 9210fca6ea1SDimitry Andric return getLoongArchPageDelta(in.got->getTlsDescAddr(sym) + a, p, type); 9220b57cec5SDimitry Andric case R_TLSGD_GOT: 9230b57cec5SDimitry Andric return in.got->getGlobalDynOffset(sym) + a; 9240b57cec5SDimitry Andric case R_TLSGD_GOTPLT: 925fe6060f1SDimitry Andric return in.got->getGlobalDynAddr(sym) + a - in.gotPlt->getVA(); 9260b57cec5SDimitry Andric case R_TLSGD_PC: 9270b57cec5SDimitry Andric return in.got->getGlobalDynAddr(sym) + a - p; 92806c3fb27SDimitry Andric case R_LOONGARCH_TLSGD_PAGE_PC: 929297eecfbSDimitry Andric return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p, type); 9300b57cec5SDimitry Andric case R_TLSLD_GOTPLT: 9310b57cec5SDimitry Andric return in.got->getVA() + in.got->getTlsIndexOff() + a - in.gotPlt->getVA(); 9320b57cec5SDimitry Andric case R_TLSLD_GOT: 9330b57cec5SDimitry Andric return in.got->getTlsIndexOff() + a; 9340b57cec5SDimitry Andric case R_TLSLD_PC: 9350b57cec5SDimitry Andric return in.got->getTlsIndexVA() + a - p; 9360b57cec5SDimitry Andric default: 9370b57cec5SDimitry Andric llvm_unreachable("invalid expression"); 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric } 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric // This function applies relocations to sections without SHF_ALLOC bit. 9420b57cec5SDimitry Andric // Such sections are never mapped to memory at runtime. Debug sections are 9430b57cec5SDimitry Andric // an example. Relocations in non-alloc sections are much easier to 9440b57cec5SDimitry Andric // handle than in allocated sections because it will never need complex 945480093f4SDimitry Andric // treatment such as GOT or PLT (because at runtime no one refers them). 9460b57cec5SDimitry Andric // So, we handle relocations for non-alloc sections directly in this 9470b57cec5SDimitry Andric // function as a performance optimization. 9480b57cec5SDimitry Andric template <class ELFT, class RelTy> 94952418fc2SDimitry Andric void InputSection::relocateNonAlloc(uint8_t *buf, Relocs<RelTy> rels) { 9500b57cec5SDimitry Andric const unsigned bits = sizeof(typename ELFT::uint) * 8; 9510eae32dcSDimitry Andric const TargetInfo &target = *elf::target; 9525f757f3fSDimitry Andric const auto emachine = config->emachine; 9535ffd83dbSDimitry Andric const bool isDebug = isDebugSection(*this); 9545ffd83dbSDimitry Andric const bool isDebugLine = isDebug && name == ".debug_line"; 955bdd1243dSDimitry Andric std::optional<uint64_t> tombstone; 956cb14a3feSDimitry Andric if (isDebug) { 957cb14a3feSDimitry Andric if (name == ".debug_loc" || name == ".debug_ranges") 958cb14a3feSDimitry Andric tombstone = 1; 959cb14a3feSDimitry Andric else if (name == ".debug_names") 960cb14a3feSDimitry Andric tombstone = UINT64_MAX; // tombstone value 961cb14a3feSDimitry Andric else 962cb14a3feSDimitry Andric tombstone = 0; 963cb14a3feSDimitry Andric } 9645ffd83dbSDimitry Andric for (const auto &patAndValue : llvm::reverse(config->deadRelocInNonAlloc)) 9655ffd83dbSDimitry Andric if (patAndValue.first.match(this->name)) { 9665ffd83dbSDimitry Andric tombstone = patAndValue.second; 9675ffd83dbSDimitry Andric break; 9685ffd83dbSDimitry Andric } 9690b57cec5SDimitry Andric 9700fca6ea1SDimitry Andric const InputFile *f = this->file; 9710fca6ea1SDimitry Andric for (auto it = rels.begin(), end = rels.end(); it != end; ++it) { 9720fca6ea1SDimitry Andric const RelTy &rel = *it; 9735f757f3fSDimitry Andric const RelType type = rel.getType(config->isMips64EL); 9745f757f3fSDimitry Andric const uint64_t offset = rel.r_offset; 9750b57cec5SDimitry Andric uint8_t *bufLoc = buf + offset; 9760b57cec5SDimitry Andric int64_t addend = getAddend<ELFT>(rel); 9770fca6ea1SDimitry Andric if (!RelTy::HasAddend) 9780eae32dcSDimitry Andric addend += target.getImplicitAddend(bufLoc, type); 9790b57cec5SDimitry Andric 9800fca6ea1SDimitry Andric Symbol &sym = f->getRelocTargetSym(rel); 9810eae32dcSDimitry Andric RelExpr expr = target.getRelExpr(type, sym, bufLoc); 9820b57cec5SDimitry Andric if (expr == R_NONE) 9830b57cec5SDimitry Andric continue; 9845f757f3fSDimitry Andric auto *ds = dyn_cast<Defined>(&sym); 9855f757f3fSDimitry Andric 9865f757f3fSDimitry Andric if (emachine == EM_RISCV && type == R_RISCV_SET_ULEB128) { 9870fca6ea1SDimitry Andric if (++it != end && 9880fca6ea1SDimitry Andric it->getType(/*isMips64EL=*/false) == R_RISCV_SUB_ULEB128 && 9890fca6ea1SDimitry Andric it->r_offset == offset) { 9905f757f3fSDimitry Andric uint64_t val; 9915f757f3fSDimitry Andric if (!ds && tombstone) { 9925f757f3fSDimitry Andric val = *tombstone; 9935f757f3fSDimitry Andric } else { 9945f757f3fSDimitry Andric val = sym.getVA(addend) - 9950fca6ea1SDimitry Andric (f->getRelocTargetSym(*it).getVA(0) + getAddend<ELFT>(*it)); 9965f757f3fSDimitry Andric } 9975f757f3fSDimitry Andric if (overwriteULEB128(bufLoc, val) >= 0x80) 9985f757f3fSDimitry Andric errorOrWarn(getLocation(offset) + ": ULEB128 value " + Twine(val) + 9995f757f3fSDimitry Andric " exceeds available space; references '" + 10005f757f3fSDimitry Andric lld::toString(sym) + "'"); 10015f757f3fSDimitry Andric continue; 10025f757f3fSDimitry Andric } 10035f757f3fSDimitry Andric errorOrWarn(getLocation(offset) + 10045f757f3fSDimitry Andric ": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128"); 10055f757f3fSDimitry Andric return; 10065f757f3fSDimitry Andric } 10070b57cec5SDimitry Andric 1008cb14a3feSDimitry Andric if (tombstone && (expr == R_ABS || expr == R_DTPREL)) { 10095ffd83dbSDimitry Andric // Resolve relocations in .debug_* referencing (discarded symbols or ICF 10105ffd83dbSDimitry Andric // folded section symbols) to a tombstone value. Resolving to addend is 10115ffd83dbSDimitry Andric // unsatisfactory because the result address range may collide with a 10125ffd83dbSDimitry Andric // valid range of low address, or leave multiple CUs claiming ownership of 10135ffd83dbSDimitry Andric // the same range of code, which may confuse consumers. 10145ffd83dbSDimitry Andric // 10155ffd83dbSDimitry Andric // To address the problems, we use -1 as a tombstone value for most 10165ffd83dbSDimitry Andric // .debug_* sections. We have to ignore the addend because we don't want 10175ffd83dbSDimitry Andric // to resolve an address attribute (which may have a non-zero addend) to 10185ffd83dbSDimitry Andric // -1+addend (wrap around to a low address). 10195ffd83dbSDimitry Andric // 10205ffd83dbSDimitry Andric // R_DTPREL type relocations represent an offset into the dynamic thread 10215ffd83dbSDimitry Andric // vector. The computed value is st_value plus a non-negative offset. 10225ffd83dbSDimitry Andric // Negative values are invalid, so -1 can be used as the tombstone value. 10235ffd83dbSDimitry Andric // 1024b3edf446SDimitry Andric // If the referenced symbol is relative to a discarded section (due to 1025b3edf446SDimitry Andric // --gc-sections, COMDAT, etc), it has been converted to a Undefined. 1026b3edf446SDimitry Andric // `ds->folded` catches the ICF folded case. However, resolving a 1027b3edf446SDimitry Andric // relocation in .debug_line to -1 would stop debugger users from setting 1028b3edf446SDimitry Andric // breakpoints on the folded-in function, so exclude .debug_line. 10295ffd83dbSDimitry Andric // 10305ffd83dbSDimitry Andric // For pre-DWARF-v5 .debug_loc and .debug_ranges, -1 is a reserved value 10311106035dSDimitry Andric // (base address selection entry), use 1 (which is used by GNU ld for 10321106035dSDimitry Andric // .debug_ranges). 10331106035dSDimitry Andric // 10341106035dSDimitry Andric // TODO To reduce disruption, we use 0 instead of -1 as the tombstone 10351106035dSDimitry Andric // value. Enable -1 in a future release. 1036b3edf446SDimitry Andric if (!ds || (ds->folded && !isDebugLine)) { 10375ffd83dbSDimitry Andric // If -z dead-reloc-in-nonalloc= is specified, respect it. 1038cb14a3feSDimitry Andric uint64_t value = SignExtend64<bits>(*tombstone); 1039cb14a3feSDimitry Andric // For a 32-bit local TU reference in .debug_names, X86_64::relocate 1040cb14a3feSDimitry Andric // requires that the unsigned value for R_X86_64_32 is truncated to 1041cb14a3feSDimitry Andric // 32-bit. Other 64-bit targets's don't discern signed/unsigned 32-bit 1042cb14a3feSDimitry Andric // absolute relocations and do not need this change. 1043cb14a3feSDimitry Andric if (emachine == EM_X86_64 && type == R_X86_64_32) 1044cb14a3feSDimitry Andric value = static_cast<uint32_t>(value); 10450eae32dcSDimitry Andric target.relocateNoSym(bufLoc, type, value); 10465ffd83dbSDimitry Andric continue; 10475ffd83dbSDimitry Andric } 10485ffd83dbSDimitry Andric } 104904eeddc0SDimitry Andric 10500fca6ea1SDimitry Andric // For a relocatable link, content relocated by relocation types with an 10510fca6ea1SDimitry Andric // explicit addend, such as RELA, remain unchanged and we can stop here. 10520fca6ea1SDimitry Andric // While content relocated by relocation types with an implicit addend, such 10530fca6ea1SDimitry Andric // as REL, needs the implicit addend updated. 10540fca6ea1SDimitry Andric if (config->relocatable && (RelTy::HasAddend || sym.type != STT_SECTION)) 105504eeddc0SDimitry Andric continue; 105604eeddc0SDimitry Andric 10575f757f3fSDimitry Andric // R_ABS/R_DTPREL and some other relocations can be used from non-SHF_ALLOC 10585f757f3fSDimitry Andric // sections. 10595f757f3fSDimitry Andric if (LLVM_LIKELY(expr == R_ABS) || expr == R_DTPREL || expr == R_GOTPLTREL || 10605f757f3fSDimitry Andric expr == R_RISCV_ADD) { 10615f757f3fSDimitry Andric target.relocateNoSym(bufLoc, type, SignExtend64<bits>(sym.getVA(addend))); 10625f757f3fSDimitry Andric continue; 10635f757f3fSDimitry Andric } 10645f757f3fSDimitry Andric 106504eeddc0SDimitry Andric if (expr == R_SIZE) { 106604eeddc0SDimitry Andric target.relocateNoSym(bufLoc, type, 106704eeddc0SDimitry Andric SignExtend64<bits>(sym.getSize() + addend)); 106804eeddc0SDimitry Andric continue; 106904eeddc0SDimitry Andric } 107004eeddc0SDimitry Andric 107104eeddc0SDimitry Andric std::string msg = getLocation(offset) + ": has non-ABS relocation " + 107204eeddc0SDimitry Andric toString(type) + " against symbol '" + toString(sym) + 107304eeddc0SDimitry Andric "'"; 10745f757f3fSDimitry Andric if (expr != R_PC && !(emachine == EM_386 && type == R_386_GOTPC)) { 10755f757f3fSDimitry Andric errorOrWarn(msg); 107604eeddc0SDimitry Andric return; 107704eeddc0SDimitry Andric } 107804eeddc0SDimitry Andric 107904eeddc0SDimitry Andric // If the control reaches here, we found a PC-relative relocation in a 108004eeddc0SDimitry Andric // non-ALLOC section. Since non-ALLOC section is not loaded into memory 108104eeddc0SDimitry Andric // at runtime, the notion of PC-relative doesn't make sense here. So, 108204eeddc0SDimitry Andric // this is a usage error. However, GNU linkers historically accept such 108304eeddc0SDimitry Andric // relocations without any errors and relocate them as if they were at 1084bdd1243dSDimitry Andric // address 0. For bug-compatibility, we accept them with warnings. We 108504eeddc0SDimitry Andric // know Steel Bank Common Lisp as of 2018 have this bug. 10865f757f3fSDimitry Andric // 10875f757f3fSDimitry Andric // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations 10885f757f3fSDimitry Andric // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed in 10895f757f3fSDimitry Andric // 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we need to 10905f757f3fSDimitry Andric // keep this bug-compatible code for a while. 109104eeddc0SDimitry Andric warn(msg); 109204eeddc0SDimitry Andric target.relocateNoSym( 109304eeddc0SDimitry Andric bufLoc, type, 109404eeddc0SDimitry Andric SignExtend64<bits>(sym.getVA(addend - offset - outSecOff))); 10950b57cec5SDimitry Andric } 10960b57cec5SDimitry Andric } 10970b57cec5SDimitry Andric 10980b57cec5SDimitry Andric template <class ELFT> 10990b57cec5SDimitry Andric void InputSectionBase::relocate(uint8_t *buf, uint8_t *bufEnd) { 11000eae32dcSDimitry Andric if ((flags & SHF_EXECINSTR) && LLVM_UNLIKELY(getFile<ELFT>()->splitStack)) 11010b57cec5SDimitry Andric adjustSplitStackFunctionPrologues<ELFT>(buf, bufEnd); 11020b57cec5SDimitry Andric 11030b57cec5SDimitry Andric if (flags & SHF_ALLOC) { 1104bdd1243dSDimitry Andric target->relocateAlloc(*this, buf); 11050b57cec5SDimitry Andric return; 11060b57cec5SDimitry Andric } 11070b57cec5SDimitry Andric 11080b57cec5SDimitry Andric auto *sec = cast<InputSection>(this); 110904eeddc0SDimitry Andric // For a relocatable link, also call relocateNonAlloc() to rewrite applicable 111004eeddc0SDimitry Andric // locations with tombstone values. 111152418fc2SDimitry Andric invokeOnRelocs(*sec, sec->relocateNonAlloc<ELFT>, buf); 1112349cc55cSDimitry Andric } 11130b57cec5SDimitry Andric 11140b57cec5SDimitry Andric // For each function-defining prologue, find any calls to __morestack, 11150b57cec5SDimitry Andric // and replace them with calls to __morestack_non_split. 11160b57cec5SDimitry Andric static void switchMorestackCallsToMorestackNonSplit( 11171fd87a68SDimitry Andric DenseSet<Defined *> &prologues, 11181fd87a68SDimitry Andric SmallVector<Relocation *, 0> &morestackCalls) { 11190b57cec5SDimitry Andric 11200b57cec5SDimitry Andric // If the target adjusted a function's prologue, all calls to 11210b57cec5SDimitry Andric // __morestack inside that function should be switched to 11220b57cec5SDimitry Andric // __morestack_non_split. 1123bdd1243dSDimitry Andric Symbol *moreStackNonSplit = symtab.find("__morestack_non_split"); 11240b57cec5SDimitry Andric if (!moreStackNonSplit) { 112504eeddc0SDimitry Andric error("mixing split-stack objects requires a definition of " 11260b57cec5SDimitry Andric "__morestack_non_split"); 11270b57cec5SDimitry Andric return; 11280b57cec5SDimitry Andric } 11290b57cec5SDimitry Andric 11300b57cec5SDimitry Andric // Sort both collections to compare addresses efficiently. 11310b57cec5SDimitry Andric llvm::sort(morestackCalls, [](const Relocation *l, const Relocation *r) { 11320b57cec5SDimitry Andric return l->offset < r->offset; 11330b57cec5SDimitry Andric }); 11340b57cec5SDimitry Andric std::vector<Defined *> functions(prologues.begin(), prologues.end()); 11350b57cec5SDimitry Andric llvm::sort(functions, [](const Defined *l, const Defined *r) { 11360b57cec5SDimitry Andric return l->value < r->value; 11370b57cec5SDimitry Andric }); 11380b57cec5SDimitry Andric 11390b57cec5SDimitry Andric auto it = morestackCalls.begin(); 11400b57cec5SDimitry Andric for (Defined *f : functions) { 11410b57cec5SDimitry Andric // Find the first call to __morestack within the function. 11420b57cec5SDimitry Andric while (it != morestackCalls.end() && (*it)->offset < f->value) 11430b57cec5SDimitry Andric ++it; 11440b57cec5SDimitry Andric // Adjust all calls inside the function. 11450b57cec5SDimitry Andric while (it != morestackCalls.end() && (*it)->offset < f->value + f->size) { 11460b57cec5SDimitry Andric (*it)->sym = moreStackNonSplit; 11470b57cec5SDimitry Andric ++it; 11480b57cec5SDimitry Andric } 11490b57cec5SDimitry Andric } 11500b57cec5SDimitry Andric } 11510b57cec5SDimitry Andric 11520b57cec5SDimitry Andric static bool enclosingPrologueAttempted(uint64_t offset, 11530b57cec5SDimitry Andric const DenseSet<Defined *> &prologues) { 11540b57cec5SDimitry Andric for (Defined *f : prologues) 11550b57cec5SDimitry Andric if (f->value <= offset && offset < f->value + f->size) 11560b57cec5SDimitry Andric return true; 11570b57cec5SDimitry Andric return false; 11580b57cec5SDimitry Andric } 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric // If a function compiled for split stack calls a function not 11610b57cec5SDimitry Andric // compiled for split stack, then the caller needs its prologue 11620b57cec5SDimitry Andric // adjusted to ensure that the called function will have enough stack 11630b57cec5SDimitry Andric // available. Find those functions, and adjust their prologues. 11640b57cec5SDimitry Andric template <class ELFT> 11650b57cec5SDimitry Andric void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf, 11660b57cec5SDimitry Andric uint8_t *end) { 11670b57cec5SDimitry Andric DenseSet<Defined *> prologues; 11681fd87a68SDimitry Andric SmallVector<Relocation *, 0> morestackCalls; 11690b57cec5SDimitry Andric 1170bdd1243dSDimitry Andric for (Relocation &rel : relocs()) { 11710b57cec5SDimitry Andric // Ignore calls into the split-stack api. 117206c3fb27SDimitry Andric if (rel.sym->getName().starts_with("__morestack")) { 11730fca6ea1SDimitry Andric if (rel.sym->getName() == "__morestack") 11740b57cec5SDimitry Andric morestackCalls.push_back(&rel); 11750b57cec5SDimitry Andric continue; 11760b57cec5SDimitry Andric } 11770b57cec5SDimitry Andric 11780b57cec5SDimitry Andric // A relocation to non-function isn't relevant. Sometimes 11790b57cec5SDimitry Andric // __morestack is not marked as a function, so this check comes 11800b57cec5SDimitry Andric // after the name check. 11810b57cec5SDimitry Andric if (rel.sym->type != STT_FUNC) 11820b57cec5SDimitry Andric continue; 11830b57cec5SDimitry Andric 11840b57cec5SDimitry Andric // If the callee's-file was compiled with split stack, nothing to do. In 11850b57cec5SDimitry Andric // this context, a "Defined" symbol is one "defined by the binary currently 11860b57cec5SDimitry Andric // being produced". So an "undefined" symbol might be provided by a shared 11870b57cec5SDimitry Andric // library. It is not possible to tell how such symbols were compiled, so be 11880b57cec5SDimitry Andric // conservative. 11890b57cec5SDimitry Andric if (Defined *d = dyn_cast<Defined>(rel.sym)) 11900b57cec5SDimitry Andric if (InputSection *isec = cast_or_null<InputSection>(d->section)) 11910b57cec5SDimitry Andric if (!isec || !isec->getFile<ELFT>() || isec->getFile<ELFT>()->splitStack) 11920b57cec5SDimitry Andric continue; 11930b57cec5SDimitry Andric 11940b57cec5SDimitry Andric if (enclosingPrologueAttempted(rel.offset, prologues)) 11950b57cec5SDimitry Andric continue; 11960b57cec5SDimitry Andric 119704eeddc0SDimitry Andric if (Defined *f = getEnclosingFunction(rel.offset)) { 11980b57cec5SDimitry Andric prologues.insert(f); 1199e8d8bef9SDimitry Andric if (target->adjustPrologueForCrossSplitStack(buf + f->value, end, 1200e8d8bef9SDimitry Andric f->stOther)) 12010b57cec5SDimitry Andric continue; 12020b57cec5SDimitry Andric if (!getFile<ELFT>()->someNoSplitStack) 12035ffd83dbSDimitry Andric error(lld::toString(this) + ": " + f->getName() + 12040b57cec5SDimitry Andric " (with -fsplit-stack) calls " + rel.sym->getName() + 12050b57cec5SDimitry Andric " (without -fsplit-stack), but couldn't adjust its prologue"); 12060b57cec5SDimitry Andric } 12070b57cec5SDimitry Andric } 12080b57cec5SDimitry Andric 12090b57cec5SDimitry Andric if (target->needsMoreStackNonSplit) 12100b57cec5SDimitry Andric switchMorestackCallsToMorestackNonSplit(prologues, morestackCalls); 12110b57cec5SDimitry Andric } 12120b57cec5SDimitry Andric 12130b57cec5SDimitry Andric template <class ELFT> void InputSection::writeTo(uint8_t *buf) { 12140eae32dcSDimitry Andric if (LLVM_UNLIKELY(type == SHT_NOBITS)) 12150eae32dcSDimitry Andric return; 12160b57cec5SDimitry Andric // If -r or --emit-relocs is given, then an InputSection 12170b57cec5SDimitry Andric // may be a relocation section. 12180eae32dcSDimitry Andric if (LLVM_UNLIKELY(type == SHT_RELA)) { 12195f757f3fSDimitry Andric copyRelocations<ELFT, typename ELFT::Rela>(buf); 12200b57cec5SDimitry Andric return; 12210b57cec5SDimitry Andric } 12220eae32dcSDimitry Andric if (LLVM_UNLIKELY(type == SHT_REL)) { 12235f757f3fSDimitry Andric copyRelocations<ELFT, typename ELFT::Rel>(buf); 12240b57cec5SDimitry Andric return; 12250b57cec5SDimitry Andric } 12260b57cec5SDimitry Andric 12270b57cec5SDimitry Andric // If -r is given, we may have a SHT_GROUP section. 12280eae32dcSDimitry Andric if (LLVM_UNLIKELY(type == SHT_GROUP)) { 122904eeddc0SDimitry Andric copyShtGroup<ELFT>(buf); 12300b57cec5SDimitry Andric return; 12310b57cec5SDimitry Andric } 12320b57cec5SDimitry Andric 12330b57cec5SDimitry Andric // If this is a compressed section, uncompress section contents directly 12340b57cec5SDimitry Andric // to the buffer. 1235bdd1243dSDimitry Andric if (compressed) { 1236bdd1243dSDimitry Andric auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content_); 1237bdd1243dSDimitry Andric auto compressed = ArrayRef<uint8_t>(content_, compressedSize) 1238bdd1243dSDimitry Andric .slice(sizeof(typename ELFT::Chdr)); 1239bdd1243dSDimitry Andric size_t size = this->size; 1240bdd1243dSDimitry Andric if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB 1241bdd1243dSDimitry Andric ? compression::zlib::decompress(compressed, buf, size) 1242bdd1243dSDimitry Andric : compression::zstd::decompress(compressed, buf, size)) 12430b57cec5SDimitry Andric fatal(toString(this) + 1244bdd1243dSDimitry Andric ": decompress failed: " + llvm::toString(std::move(e))); 124504eeddc0SDimitry Andric uint8_t *bufEnd = buf + size; 124604eeddc0SDimitry Andric relocate<ELFT>(buf, bufEnd); 12470b57cec5SDimitry Andric return; 12480b57cec5SDimitry Andric } 12490b57cec5SDimitry Andric 12500b57cec5SDimitry Andric // Copy section contents from source object file to output file 12510b57cec5SDimitry Andric // and then apply relocations. 1252bdd1243dSDimitry Andric memcpy(buf, content().data(), content().size()); 1253bdd1243dSDimitry Andric relocate<ELFT>(buf, buf + content().size()); 12540b57cec5SDimitry Andric } 12550b57cec5SDimitry Andric 12560b57cec5SDimitry Andric void InputSection::replace(InputSection *other) { 1257bdd1243dSDimitry Andric addralign = std::max(addralign, other->addralign); 12580b57cec5SDimitry Andric 12590b57cec5SDimitry Andric // When a section is replaced with another section that was allocated to 12600b57cec5SDimitry Andric // another partition, the replacement section (and its associated sections) 12610b57cec5SDimitry Andric // need to be placed in the main partition so that both partitions will be 12620b57cec5SDimitry Andric // able to access it. 12630b57cec5SDimitry Andric if (partition != other->partition) { 12640b57cec5SDimitry Andric partition = 1; 12650b57cec5SDimitry Andric for (InputSection *isec : dependentSections) 12660b57cec5SDimitry Andric isec->partition = 1; 12670b57cec5SDimitry Andric } 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric other->repl = repl; 12700b57cec5SDimitry Andric other->markDead(); 12710b57cec5SDimitry Andric } 12720b57cec5SDimitry Andric 12730b57cec5SDimitry Andric template <class ELFT> 12740b57cec5SDimitry Andric EhInputSection::EhInputSection(ObjFile<ELFT> &f, 12750b57cec5SDimitry Andric const typename ELFT::Shdr &header, 12760b57cec5SDimitry Andric StringRef name) 12770b57cec5SDimitry Andric : InputSectionBase(f, header, name, InputSectionBase::EHFrame) {} 12780b57cec5SDimitry Andric 12790b57cec5SDimitry Andric SyntheticSection *EhInputSection::getParent() const { 12800b57cec5SDimitry Andric return cast_or_null<SyntheticSection>(parent); 12810b57cec5SDimitry Andric } 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andric // .eh_frame is a sequence of CIE or FDE records. 12840b57cec5SDimitry Andric // This function splits an input section into records and returns them. 12850b57cec5SDimitry Andric template <class ELFT> void EhInputSection::split() { 128652418fc2SDimitry Andric const RelsOrRelas<ELFT> rels = relsOrRelas<ELFT>(/*supportsCrel=*/false); 128704eeddc0SDimitry Andric // getReloc expects the relocations to be sorted by r_offset. See the comment 128804eeddc0SDimitry Andric // in scanRelocs. 128904eeddc0SDimitry Andric if (rels.areRelocsRel()) { 129004eeddc0SDimitry Andric SmallVector<typename ELFT::Rel, 0> storage; 129104eeddc0SDimitry Andric split<ELFT>(sortRels(rels.rels, storage)); 129204eeddc0SDimitry Andric } else { 129304eeddc0SDimitry Andric SmallVector<typename ELFT::Rela, 0> storage; 129404eeddc0SDimitry Andric split<ELFT>(sortRels(rels.relas, storage)); 129504eeddc0SDimitry Andric } 12960b57cec5SDimitry Andric } 12970b57cec5SDimitry Andric 12980b57cec5SDimitry Andric template <class ELFT, class RelTy> 12990b57cec5SDimitry Andric void EhInputSection::split(ArrayRef<RelTy> rels) { 1300bdd1243dSDimitry Andric ArrayRef<uint8_t> d = content(); 130104eeddc0SDimitry Andric const char *msg = nullptr; 13020b57cec5SDimitry Andric unsigned relI = 0; 130304eeddc0SDimitry Andric while (!d.empty()) { 130404eeddc0SDimitry Andric if (d.size() < 4) { 130504eeddc0SDimitry Andric msg = "CIE/FDE too small"; 13060b57cec5SDimitry Andric break; 13070b57cec5SDimitry Andric } 13080fca6ea1SDimitry Andric uint64_t size = endian::read32<ELFT::Endianness>(d.data()); 1309bdd1243dSDimitry Andric if (size == 0) // ZERO terminator 1310bdd1243dSDimitry Andric break; 13110fca6ea1SDimitry Andric uint32_t id = endian::read32<ELFT::Endianness>(d.data() + 4); 1312bdd1243dSDimitry Andric size += 4; 1313bdd1243dSDimitry Andric if (LLVM_UNLIKELY(size > d.size())) { 131404eeddc0SDimitry Andric // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead, 131504eeddc0SDimitry Andric // but we do not support that format yet. 1316bdd1243dSDimitry Andric msg = size == UINT32_MAX + uint64_t(4) 1317bdd1243dSDimitry Andric ? "CIE/FDE too large" 1318bdd1243dSDimitry Andric : "CIE/FDE ends past the end of the section"; 131904eeddc0SDimitry Andric break; 132004eeddc0SDimitry Andric } 132104eeddc0SDimitry Andric 1322bdd1243dSDimitry Andric // Find the first relocation that points to [off,off+size). Relocations 1323bdd1243dSDimitry Andric // have been sorted by r_offset. 1324bdd1243dSDimitry Andric const uint64_t off = d.data() - content().data(); 1325bdd1243dSDimitry Andric while (relI != rels.size() && rels[relI].r_offset < off) 1326bdd1243dSDimitry Andric ++relI; 1327bdd1243dSDimitry Andric unsigned firstRel = -1; 1328bdd1243dSDimitry Andric if (relI != rels.size() && rels[relI].r_offset < off + size) 1329bdd1243dSDimitry Andric firstRel = relI; 1330bdd1243dSDimitry Andric (id == 0 ? cies : fdes).emplace_back(off, this, size, firstRel); 133104eeddc0SDimitry Andric d = d.slice(size); 133204eeddc0SDimitry Andric } 133304eeddc0SDimitry Andric if (msg) 133404eeddc0SDimitry Andric errorOrWarn("corrupted .eh_frame: " + Twine(msg) + "\n>>> defined in " + 1335bdd1243dSDimitry Andric getObjMsg(d.data() - content().data())); 13360b57cec5SDimitry Andric } 13370b57cec5SDimitry Andric 133881ad6265SDimitry Andric // Return the offset in an output section for a given input offset. 133981ad6265SDimitry Andric uint64_t EhInputSection::getParentOffset(uint64_t offset) const { 1340bdd1243dSDimitry Andric auto it = partition_point( 1341bdd1243dSDimitry Andric fdes, [=](EhSectionPiece p) { return p.inputOff <= offset; }); 1342bdd1243dSDimitry Andric if (it == fdes.begin() || it[-1].inputOff + it[-1].size <= offset) { 1343bdd1243dSDimitry Andric it = partition_point( 1344bdd1243dSDimitry Andric cies, [=](EhSectionPiece p) { return p.inputOff <= offset; }); 1345bdd1243dSDimitry Andric if (it == cies.begin()) // invalid piece 1346bdd1243dSDimitry Andric return offset; 1347bdd1243dSDimitry Andric } 1348bdd1243dSDimitry Andric if (it[-1].outputOff == -1) // invalid piece 1349bdd1243dSDimitry Andric return offset - it[-1].inputOff; 1350bdd1243dSDimitry Andric return it[-1].outputOff + (offset - it[-1].inputOff); 135181ad6265SDimitry Andric } 135281ad6265SDimitry Andric 13530b57cec5SDimitry Andric static size_t findNull(StringRef s, size_t entSize) { 13540b57cec5SDimitry Andric for (unsigned i = 0, n = s.size(); i != n; i += entSize) { 13550b57cec5SDimitry Andric const char *b = s.begin() + i; 13560b57cec5SDimitry Andric if (std::all_of(b, b + entSize, [](char c) { return c == 0; })) 13570b57cec5SDimitry Andric return i; 13580b57cec5SDimitry Andric } 13591fd87a68SDimitry Andric llvm_unreachable(""); 13600b57cec5SDimitry Andric } 13610b57cec5SDimitry Andric 13620b57cec5SDimitry Andric // Split SHF_STRINGS section. Such section is a sequence of 13630b57cec5SDimitry Andric // null-terminated strings. 13641fd87a68SDimitry Andric void MergeInputSection::splitStrings(StringRef s, size_t entSize) { 13650eae32dcSDimitry Andric const bool live = !(flags & SHF_ALLOC) || !config->gcSections; 13661fd87a68SDimitry Andric const char *p = s.data(), *end = s.data() + s.size(); 13671fd87a68SDimitry Andric if (!std::all_of(end - entSize, end, [](char c) { return c == 0; })) 13680b57cec5SDimitry Andric fatal(toString(this) + ": string is not null terminated"); 13691fd87a68SDimitry Andric if (entSize == 1) { 13701fd87a68SDimitry Andric // Optimize the common case. 13711fd87a68SDimitry Andric do { 137281ad6265SDimitry Andric size_t size = strlen(p); 137306c3fb27SDimitry Andric pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live); 137481ad6265SDimitry Andric p += size + 1; 13751fd87a68SDimitry Andric } while (p != end); 13761fd87a68SDimitry Andric } else { 13771fd87a68SDimitry Andric do { 137881ad6265SDimitry Andric size_t size = findNull(StringRef(p, end - p), entSize); 137906c3fb27SDimitry Andric pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live); 138081ad6265SDimitry Andric p += size + entSize; 13811fd87a68SDimitry Andric } while (p != end); 13820b57cec5SDimitry Andric } 13830b57cec5SDimitry Andric } 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric // Split non-SHF_STRINGS section. Such section is a sequence of 13860b57cec5SDimitry Andric // fixed size records. 13870b57cec5SDimitry Andric void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> data, 13880b57cec5SDimitry Andric size_t entSize) { 13890b57cec5SDimitry Andric size_t size = data.size(); 13900b57cec5SDimitry Andric assert((size % entSize) == 0); 13910eae32dcSDimitry Andric const bool live = !(flags & SHF_ALLOC) || !config->gcSections; 13920b57cec5SDimitry Andric 13931fd87a68SDimitry Andric pieces.resize_for_overwrite(size / entSize); 13940eae32dcSDimitry Andric for (size_t i = 0, j = 0; i != size; i += entSize, j++) 139506c3fb27SDimitry Andric pieces[j] = {i, (uint32_t)xxh3_64bits(data.slice(i, entSize)), live}; 13960b57cec5SDimitry Andric } 13970b57cec5SDimitry Andric 13980b57cec5SDimitry Andric template <class ELFT> 13990b57cec5SDimitry Andric MergeInputSection::MergeInputSection(ObjFile<ELFT> &f, 14000b57cec5SDimitry Andric const typename ELFT::Shdr &header, 14010b57cec5SDimitry Andric StringRef name) 14020b57cec5SDimitry Andric : InputSectionBase(f, header, name, InputSectionBase::Merge) {} 14030b57cec5SDimitry Andric 14040b57cec5SDimitry Andric MergeInputSection::MergeInputSection(uint64_t flags, uint32_t type, 14050b57cec5SDimitry Andric uint64_t entsize, ArrayRef<uint8_t> data, 14060b57cec5SDimitry Andric StringRef name) 14070b57cec5SDimitry Andric : InputSectionBase(nullptr, flags, type, entsize, /*Link*/ 0, /*Info*/ 0, 14080b57cec5SDimitry Andric /*Alignment*/ entsize, data, name, SectionBase::Merge) {} 14090b57cec5SDimitry Andric 14100b57cec5SDimitry Andric // This function is called after we obtain a complete list of input sections 14110b57cec5SDimitry Andric // that need to be linked. This is responsible to split section contents 14120b57cec5SDimitry Andric // into small chunks for further processing. 14130b57cec5SDimitry Andric // 14140b57cec5SDimitry Andric // Note that this function is called from parallelForEach. This must be 14150b57cec5SDimitry Andric // thread-safe (i.e. no memory allocation from the pools). 14160b57cec5SDimitry Andric void MergeInputSection::splitIntoPieces() { 14170b57cec5SDimitry Andric assert(pieces.empty()); 14180b57cec5SDimitry Andric 14190b57cec5SDimitry Andric if (flags & SHF_STRINGS) 1420bdd1243dSDimitry Andric splitStrings(toStringRef(contentMaybeDecompress()), entsize); 14210b57cec5SDimitry Andric else 1422bdd1243dSDimitry Andric splitNonStrings(contentMaybeDecompress(), entsize); 14230b57cec5SDimitry Andric } 14240b57cec5SDimitry Andric 142581ad6265SDimitry Andric SectionPiece &MergeInputSection::getSectionPiece(uint64_t offset) { 1426bdd1243dSDimitry Andric if (content().size() <= offset) 14270b57cec5SDimitry Andric fatal(toString(this) + ": offset is outside the section"); 142881ad6265SDimitry Andric return partition_point( 142981ad6265SDimitry Andric pieces, [=](SectionPiece p) { return p.inputOff <= offset; })[-1]; 14300b57cec5SDimitry Andric } 14310b57cec5SDimitry Andric 143281ad6265SDimitry Andric // Return the offset in an output section for a given input offset. 14330b57cec5SDimitry Andric uint64_t MergeInputSection::getParentOffset(uint64_t offset) const { 143481ad6265SDimitry Andric const SectionPiece &piece = getSectionPiece(offset); 143581ad6265SDimitry Andric return piece.outputOff + (offset - piece.inputOff); 14360b57cec5SDimitry Andric } 14370b57cec5SDimitry Andric 14380b57cec5SDimitry Andric template InputSection::InputSection(ObjFile<ELF32LE> &, const ELF32LE::Shdr &, 14390b57cec5SDimitry Andric StringRef); 14400b57cec5SDimitry Andric template InputSection::InputSection(ObjFile<ELF32BE> &, const ELF32BE::Shdr &, 14410b57cec5SDimitry Andric StringRef); 14420b57cec5SDimitry Andric template InputSection::InputSection(ObjFile<ELF64LE> &, const ELF64LE::Shdr &, 14430b57cec5SDimitry Andric StringRef); 14440b57cec5SDimitry Andric template InputSection::InputSection(ObjFile<ELF64BE> &, const ELF64BE::Shdr &, 14450b57cec5SDimitry Andric StringRef); 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric template void InputSection::writeTo<ELF32LE>(uint8_t *); 14480b57cec5SDimitry Andric template void InputSection::writeTo<ELF32BE>(uint8_t *); 14490b57cec5SDimitry Andric template void InputSection::writeTo<ELF64LE>(uint8_t *); 14500b57cec5SDimitry Andric template void InputSection::writeTo<ELF64BE>(uint8_t *); 14510b57cec5SDimitry Andric 145252418fc2SDimitry Andric template RelsOrRelas<ELF32LE> 145352418fc2SDimitry Andric InputSectionBase::relsOrRelas<ELF32LE>(bool) const; 145452418fc2SDimitry Andric template RelsOrRelas<ELF32BE> 145552418fc2SDimitry Andric InputSectionBase::relsOrRelas<ELF32BE>(bool) const; 145652418fc2SDimitry Andric template RelsOrRelas<ELF64LE> 145752418fc2SDimitry Andric InputSectionBase::relsOrRelas<ELF64LE>(bool) const; 145852418fc2SDimitry Andric template RelsOrRelas<ELF64BE> 145952418fc2SDimitry Andric InputSectionBase::relsOrRelas<ELF64BE>(bool) const; 1460349cc55cSDimitry Andric 14610b57cec5SDimitry Andric template MergeInputSection::MergeInputSection(ObjFile<ELF32LE> &, 14620b57cec5SDimitry Andric const ELF32LE::Shdr &, StringRef); 14630b57cec5SDimitry Andric template MergeInputSection::MergeInputSection(ObjFile<ELF32BE> &, 14640b57cec5SDimitry Andric const ELF32BE::Shdr &, StringRef); 14650b57cec5SDimitry Andric template MergeInputSection::MergeInputSection(ObjFile<ELF64LE> &, 14660b57cec5SDimitry Andric const ELF64LE::Shdr &, StringRef); 14670b57cec5SDimitry Andric template MergeInputSection::MergeInputSection(ObjFile<ELF64BE> &, 14680b57cec5SDimitry Andric const ELF64BE::Shdr &, StringRef); 14690b57cec5SDimitry Andric 14700b57cec5SDimitry Andric template EhInputSection::EhInputSection(ObjFile<ELF32LE> &, 14710b57cec5SDimitry Andric const ELF32LE::Shdr &, StringRef); 14720b57cec5SDimitry Andric template EhInputSection::EhInputSection(ObjFile<ELF32BE> &, 14730b57cec5SDimitry Andric const ELF32BE::Shdr &, StringRef); 14740b57cec5SDimitry Andric template EhInputSection::EhInputSection(ObjFile<ELF64LE> &, 14750b57cec5SDimitry Andric const ELF64LE::Shdr &, StringRef); 14760b57cec5SDimitry Andric template EhInputSection::EhInputSection(ObjFile<ELF64BE> &, 14770b57cec5SDimitry Andric const ELF64BE::Shdr &, StringRef); 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric template void EhInputSection::split<ELF32LE>(); 14800b57cec5SDimitry Andric template void EhInputSection::split<ELF32BE>(); 14810b57cec5SDimitry Andric template void EhInputSection::split<ELF64LE>(); 14820b57cec5SDimitry Andric template void EhInputSection::split<ELF64BE>(); 1483