1//===- InputSection.cpp ---------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "InputSection.h"
10#include "Config.h"
11#include "InputFiles.h"
12#include "OutputSections.h"
13#include "Relocations.h"
14#include "SymbolTable.h"
15#include "Symbols.h"
16#include "SyntheticSections.h"
17#include "Target.h"
18#include "lld/Common/CommonLinkerContext.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/Compression.h"
21#include "llvm/Support/Endian.h"
22#include "llvm/Support/xxhash.h"
23#include <algorithm>
24#include <mutex>
25#include <optional>
26#include <vector>
27
28using namespace llvm;
29using namespace llvm::ELF;
30using namespace llvm::object;
31using namespace llvm::support;
32using namespace llvm::support::endian;
33using namespace llvm::sys;
34using namespace lld;
35using namespace lld::elf;
36
37DenseSet<std::pair<const Symbol *, uint64_t>> elf::ppc64noTocRelax;
38
39// Returns a string to construct an error message.
40std::string lld::toString(const InputSectionBase *sec) {
41  return (toString(sec->file) + ":(" + sec->name + ")").str();
42}
43
44template <class ELFT>
45static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &file,
46                                            const typename ELFT::Shdr &hdr) {
47  if (hdr.sh_type == SHT_NOBITS)
48    return ArrayRef<uint8_t>(nullptr, hdr.sh_size);
49  return check(file.getObj().getSectionContents(hdr));
50}
51
52InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags,
53                                   uint32_t type, uint64_t entsize,
54                                   uint32_t link, uint32_t info,
55                                   uint32_t addralign, ArrayRef<uint8_t> data,
56                                   StringRef name, Kind sectionKind)
57    : SectionBase(sectionKind, name, flags, entsize, addralign, type, info,
58                  link),
59      file(file), content_(data.data()), size(data.size()) {
60  // In order to reduce memory allocation, we assume that mergeable
61  // sections are smaller than 4 GiB, which is not an unreasonable
62  // assumption as of 2017.
63  if (sectionKind == SectionBase::Merge && content().size() > UINT32_MAX)
64    error(toString(this) + ": section too large");
65
66  // The ELF spec states that a value of 0 means the section has
67  // no alignment constraints.
68  uint32_t v = std::max<uint32_t>(addralign, 1);
69  if (!isPowerOf2_64(v))
70    fatal(toString(this) + ": sh_addralign is not a power of 2");
71  this->addralign = v;
72
73  // If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no
74  // longer supported.
75  if (flags & SHF_COMPRESSED)
76    invokeELFT(parseCompressedHeader,);
77}
78
79// Drop SHF_GROUP bit unless we are producing a re-linkable object file.
80// SHF_GROUP is a marker that a section belongs to some comdat group.
81// That flag doesn't make sense in an executable.
82static uint64_t getFlags(uint64_t flags) {
83  flags &= ~(uint64_t)SHF_INFO_LINK;
84  if (!config->relocatable)
85    flags &= ~(uint64_t)SHF_GROUP;
86  return flags;
87}
88
89template <class ELFT>
90InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,
91                                   const typename ELFT::Shdr &hdr,
92                                   StringRef name, Kind sectionKind)
93    : InputSectionBase(&file, getFlags(hdr.sh_flags), hdr.sh_type,
94                       hdr.sh_entsize, hdr.sh_link, hdr.sh_info,
95                       hdr.sh_addralign, getSectionContents(file, hdr), name,
96                       sectionKind) {
97  // We reject object files having insanely large alignments even though
98  // they are allowed by the spec. I think 4GB is a reasonable limitation.
99  // We might want to relax this in the future.
100  if (hdr.sh_addralign > UINT32_MAX)
101    fatal(toString(&file) + ": section sh_addralign is too large");
102}
103
104size_t InputSectionBase::getSize() const {
105  if (auto *s = dyn_cast<SyntheticSection>(this))
106    return s->getSize();
107  return size - bytesDropped;
108}
109
110template <class ELFT>
111static void decompressAux(const InputSectionBase &sec, uint8_t *out,
112                          size_t size) {
113  auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(sec.content_);
114  auto compressed = ArrayRef<uint8_t>(sec.content_, sec.compressedSize)
115                        .slice(sizeof(typename ELFT::Chdr));
116  if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
117                    ? compression::zlib::decompress(compressed, out, size)
118                    : compression::zstd::decompress(compressed, out, size))
119    fatal(toString(&sec) +
120          ": decompress failed: " + llvm::toString(std::move(e)));
121}
122
123void InputSectionBase::decompress() const {
124  uint8_t *uncompressedBuf;
125  {
126    static std::mutex mu;
127    std::lock_guard<std::mutex> lock(mu);
128    uncompressedBuf = bAlloc().Allocate<uint8_t>(size);
129  }
130
131  invokeELFT(decompressAux, *this, uncompressedBuf, size);
132  content_ = uncompressedBuf;
133  compressed = false;
134}
135
136template <class ELFT> RelsOrRelas<ELFT> InputSectionBase::relsOrRelas() const {
137  if (relSecIdx == 0)
138    return {};
139  RelsOrRelas<ELFT> ret;
140  typename ELFT::Shdr shdr =
141      cast<ELFFileBase>(file)->getELFShdrs<ELFT>()[relSecIdx];
142  if (shdr.sh_type == SHT_REL) {
143    ret.rels = ArrayRef(reinterpret_cast<const typename ELFT::Rel *>(
144                            file->mb.getBufferStart() + shdr.sh_offset),
145                        shdr.sh_size / sizeof(typename ELFT::Rel));
146  } else {
147    assert(shdr.sh_type == SHT_RELA);
148    ret.relas = ArrayRef(reinterpret_cast<const typename ELFT::Rela *>(
149                             file->mb.getBufferStart() + shdr.sh_offset),
150                         shdr.sh_size / sizeof(typename ELFT::Rela));
151  }
152  return ret;
153}
154
155uint64_t SectionBase::getOffset(uint64_t offset) const {
156  switch (kind()) {
157  case Output: {
158    auto *os = cast<OutputSection>(this);
159    // For output sections we treat offset -1 as the end of the section.
160    return offset == uint64_t(-1) ? os->size : offset;
161  }
162  case Regular:
163  case Synthetic:
164    return cast<InputSection>(this)->outSecOff + offset;
165  case EHFrame: {
166    // Two code paths may reach here. First, clang_rt.crtbegin.o and GCC
167    // crtbeginT.o may reference the start of an empty .eh_frame to identify the
168    // start of the output .eh_frame. Just return offset.
169    //
170    // Second, InputSection::copyRelocations on .eh_frame. Some pieces may be
171    // discarded due to GC/ICF. We should compute the output section offset.
172    const EhInputSection *es = cast<EhInputSection>(this);
173    if (!es->content().empty())
174      if (InputSection *isec = es->getParent())
175        return isec->outSecOff + es->getParentOffset(offset);
176    return offset;
177  }
178  case Merge:
179    const MergeInputSection *ms = cast<MergeInputSection>(this);
180    if (InputSection *isec = ms->getParent())
181      return isec->outSecOff + ms->getParentOffset(offset);
182    return ms->getParentOffset(offset);
183  }
184  llvm_unreachable("invalid section kind");
185}
186
187uint64_t SectionBase::getVA(uint64_t offset) const {
188  const OutputSection *out = getOutputSection();
189  return (out ? out->addr : 0) + getOffset(offset);
190}
191
192OutputSection *SectionBase::getOutputSection() {
193  InputSection *sec;
194  if (auto *isec = dyn_cast<InputSection>(this))
195    sec = isec;
196  else if (auto *ms = dyn_cast<MergeInputSection>(this))
197    sec = ms->getParent();
198  else if (auto *eh = dyn_cast<EhInputSection>(this))
199    sec = eh->getParent();
200  else
201    return cast<OutputSection>(this);
202  return sec ? sec->getParent() : nullptr;
203}
204
205// When a section is compressed, `rawData` consists with a header followed
206// by zlib-compressed data. This function parses a header to initialize
207// `uncompressedSize` member and remove the header from `rawData`.
208template <typename ELFT> void InputSectionBase::parseCompressedHeader() {
209  flags &= ~(uint64_t)SHF_COMPRESSED;
210
211  // New-style header
212  if (content().size() < sizeof(typename ELFT::Chdr)) {
213    error(toString(this) + ": corrupted compressed section");
214    return;
215  }
216
217  auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content().data());
218  if (hdr->ch_type == ELFCOMPRESS_ZLIB) {
219    if (!compression::zlib::isAvailable())
220      error(toString(this) + " is compressed with ELFCOMPRESS_ZLIB, but lld is "
221                             "not built with zlib support");
222  } else if (hdr->ch_type == ELFCOMPRESS_ZSTD) {
223    if (!compression::zstd::isAvailable())
224      error(toString(this) + " is compressed with ELFCOMPRESS_ZSTD, but lld is "
225                             "not built with zstd support");
226  } else {
227    error(toString(this) + ": unsupported compression type (" +
228          Twine(hdr->ch_type) + ")");
229    return;
230  }
231
232  compressed = true;
233  compressedSize = size;
234  size = hdr->ch_size;
235  addralign = std::max<uint32_t>(hdr->ch_addralign, 1);
236}
237
238InputSection *InputSectionBase::getLinkOrderDep() const {
239  assert(flags & SHF_LINK_ORDER);
240  if (!link)
241    return nullptr;
242  return cast<InputSection>(file->getSections()[link]);
243}
244
245// Find a symbol that encloses a given location.
246Defined *InputSectionBase::getEnclosingSymbol(uint64_t offset,
247                                              uint8_t type) const {
248  if (file->isInternal())
249    return nullptr;
250  for (Symbol *b : file->getSymbols())
251    if (Defined *d = dyn_cast<Defined>(b))
252      if (d->section == this && d->value <= offset &&
253          offset < d->value + d->size && (type == 0 || type == d->type))
254        return d;
255  return nullptr;
256}
257
258// Returns an object file location string. Used to construct an error message.
259std::string InputSectionBase::getLocation(uint64_t offset) const {
260  std::string secAndOffset =
261      (name + "+0x" + Twine::utohexstr(offset) + ")").str();
262
263  // We don't have file for synthetic sections.
264  if (file == nullptr)
265    return (config->outputFile + ":(" + secAndOffset).str();
266
267  std::string filename = toString(file);
268  if (Defined *d = getEnclosingFunction(offset))
269    return filename + ":(function " + toString(*d) + ": " + secAndOffset;
270
271  return filename + ":(" + secAndOffset;
272}
273
274// This function is intended to be used for constructing an error message.
275// The returned message looks like this:
276//
277//   foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42)
278//
279//  Returns an empty string if there's no way to get line info.
280std::string InputSectionBase::getSrcMsg(const Symbol &sym,
281                                        uint64_t offset) const {
282  return file->getSrcMsg(sym, *this, offset);
283}
284
285// Returns a filename string along with an optional section name. This
286// function is intended to be used for constructing an error
287// message. The returned message looks like this:
288//
289//   path/to/foo.o:(function bar)
290//
291// or
292//
293//   path/to/foo.o:(function bar) in archive path/to/bar.a
294std::string InputSectionBase::getObjMsg(uint64_t off) const {
295  std::string filename = std::string(file->getName());
296
297  std::string archive;
298  if (!file->archiveName.empty())
299    archive = (" in archive " + file->archiveName).str();
300
301  // Find a symbol that encloses a given location. getObjMsg may be called
302  // before ObjFile::initSectionsAndLocalSyms where local symbols are
303  // initialized.
304  if (Defined *d = getEnclosingSymbol(off))
305    return filename + ":(" + toString(*d) + ")" + archive;
306
307  // If there's no symbol, print out the offset in the section.
308  return (filename + ":(" + name + "+0x" + utohexstr(off) + ")" + archive)
309      .str();
310}
311
312InputSection InputSection::discarded(nullptr, 0, 0, 0, ArrayRef<uint8_t>(), "");
313
314InputSection::InputSection(InputFile *f, uint64_t flags, uint32_t type,
315                           uint32_t addralign, ArrayRef<uint8_t> data,
316                           StringRef name, Kind k)
317    : InputSectionBase(f, flags, type,
318                       /*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, addralign, data,
319                       name, k) {}
320
321template <class ELFT>
322InputSection::InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
323                           StringRef name)
324    : InputSectionBase(f, header, name, InputSectionBase::Regular) {}
325
326// Copy SHT_GROUP section contents. Used only for the -r option.
327template <class ELFT> void InputSection::copyShtGroup(uint8_t *buf) {
328  // ELFT::Word is the 32-bit integral type in the target endianness.
329  using u32 = typename ELFT::Word;
330  ArrayRef<u32> from = getDataAs<u32>();
331  auto *to = reinterpret_cast<u32 *>(buf);
332
333  // The first entry is not a section number but a flag.
334  *to++ = from[0];
335
336  // Adjust section numbers because section numbers in an input object files are
337  // different in the output. We also need to handle combined or discarded
338  // members.
339  ArrayRef<InputSectionBase *> sections = file->getSections();
340  DenseSet<uint32_t> seen;
341  for (uint32_t idx : from.slice(1)) {
342    OutputSection *osec = sections[idx]->getOutputSection();
343    if (osec && seen.insert(osec->sectionIndex).second)
344      *to++ = osec->sectionIndex;
345  }
346}
347
348InputSectionBase *InputSection::getRelocatedSection() const {
349  if (!file || file->isInternal() || (type != SHT_RELA && type != SHT_REL))
350    return nullptr;
351  ArrayRef<InputSectionBase *> sections = file->getSections();
352  return sections[info];
353}
354
355template <class ELFT, class RelTy>
356void InputSection::copyRelocations(uint8_t *buf) {
357  if (config->relax && !config->relocatable &&
358      (config->emachine == EM_RISCV || config->emachine == EM_LOONGARCH)) {
359    // On LoongArch and RISC-V, relaxation might change relocations: copy
360    // from internal ones that are updated by relaxation.
361    InputSectionBase *sec = getRelocatedSection();
362    copyRelocations<ELFT, RelTy>(buf, llvm::make_range(sec->relocations.begin(),
363                                                       sec->relocations.end()));
364  } else {
365    // Convert the raw relocations in the input section into Relocation objects
366    // suitable to be used by copyRelocations below.
367    struct MapRel {
368      const ObjFile<ELFT> &file;
369      Relocation operator()(const RelTy &rel) const {
370        // RelExpr is not used so set to a dummy value.
371        return Relocation{R_NONE, rel.getType(config->isMips64EL), rel.r_offset,
372                          getAddend<ELFT>(rel), &file.getRelocTargetSym(rel)};
373      }
374    };
375
376    using RawRels = ArrayRef<RelTy>;
377    using MapRelIter =
378        llvm::mapped_iterator<typename RawRels::iterator, MapRel>;
379    auto mapRel = MapRel{*getFile<ELFT>()};
380    RawRels rawRels = getDataAs<RelTy>();
381    auto rels = llvm::make_range(MapRelIter(rawRels.begin(), mapRel),
382                                 MapRelIter(rawRels.end(), mapRel));
383    copyRelocations<ELFT, RelTy>(buf, rels);
384  }
385}
386
387// This is used for -r and --emit-relocs. We can't use memcpy to copy
388// relocations because we need to update symbol table offset and section index
389// for each relocation. So we copy relocations one by one.
390template <class ELFT, class RelTy, class RelIt>
391void InputSection::copyRelocations(uint8_t *buf,
392                                   llvm::iterator_range<RelIt> rels) {
393  const TargetInfo &target = *elf::target;
394  InputSectionBase *sec = getRelocatedSection();
395  (void)sec->contentMaybeDecompress(); // uncompress if needed
396
397  for (const Relocation &rel : rels) {
398    RelType type = rel.type;
399    const ObjFile<ELFT> *file = getFile<ELFT>();
400    Symbol &sym = *rel.sym;
401
402    auto *p = reinterpret_cast<typename ELFT::Rela *>(buf);
403    buf += sizeof(RelTy);
404
405    if (RelTy::IsRela)
406      p->r_addend = rel.addend;
407
408    // Output section VA is zero for -r, so r_offset is an offset within the
409    // section, but for --emit-relocs it is a virtual address.
410    p->r_offset = sec->getVA(rel.offset);
411    p->setSymbolAndType(in.symTab->getSymbolIndex(&sym), type,
412                        config->isMips64EL);
413
414    if (sym.type == STT_SECTION) {
415      // We combine multiple section symbols into only one per
416      // section. This means we have to update the addend. That is
417      // trivial for Elf_Rela, but for Elf_Rel we have to write to the
418      // section data. We do that by adding to the Relocation vector.
419
420      // .eh_frame is horribly special and can reference discarded sections. To
421      // avoid having to parse and recreate .eh_frame, we just replace any
422      // relocation in it pointing to discarded sections with R_*_NONE, which
423      // hopefully creates a frame that is ignored at runtime. Also, don't warn
424      // on .gcc_except_table and debug sections.
425      //
426      // See the comment in maybeReportUndefined for PPC32 .got2 and PPC64 .toc
427      auto *d = dyn_cast<Defined>(&sym);
428      if (!d) {
429        if (!isDebugSection(*sec) && sec->name != ".eh_frame" &&
430            sec->name != ".gcc_except_table" && sec->name != ".got2" &&
431            sec->name != ".toc") {
432          uint32_t secIdx = cast<Undefined>(sym).discardedSecIdx;
433          Elf_Shdr_Impl<ELFT> sec = file->template getELFShdrs<ELFT>()[secIdx];
434          warn("relocation refers to a discarded section: " +
435               CHECK(file->getObj().getSectionName(sec), file) +
436               "\n>>> referenced by " + getObjMsg(p->r_offset));
437        }
438        p->setSymbolAndType(0, 0, false);
439        continue;
440      }
441      SectionBase *section = d->section;
442      assert(section->isLive());
443
444      int64_t addend = rel.addend;
445      const uint8_t *bufLoc = sec->content().begin() + rel.offset;
446      if (!RelTy::IsRela)
447        addend = target.getImplicitAddend(bufLoc, type);
448
449      if (config->emachine == EM_MIPS &&
450          target.getRelExpr(type, sym, bufLoc) == R_MIPS_GOTREL) {
451        // Some MIPS relocations depend on "gp" value. By default,
452        // this value has 0x7ff0 offset from a .got section. But
453        // relocatable files produced by a compiler or a linker
454        // might redefine this default value and we must use it
455        // for a calculation of the relocation result. When we
456        // generate EXE or DSO it's trivial. Generating a relocatable
457        // output is more difficult case because the linker does
458        // not calculate relocations in this mode and loses
459        // individual "gp" values used by each input object file.
460        // As a workaround we add the "gp" value to the relocation
461        // addend and save it back to the file.
462        addend += sec->getFile<ELFT>()->mipsGp0;
463      }
464
465      if (RelTy::IsRela)
466        p->r_addend = sym.getVA(addend) - section->getOutputSection()->addr;
467      // For SHF_ALLOC sections relocated by REL, append a relocation to
468      // sec->relocations so that relocateAlloc transitively called by
469      // writeSections will update the implicit addend. Non-SHF_ALLOC sections
470      // utilize relocateNonAlloc to process raw relocations and do not need
471      // this sec->relocations change.
472      else if (config->relocatable && (sec->flags & SHF_ALLOC) &&
473               type != target.noneRel)
474        sec->addReloc({R_ABS, type, rel.offset, addend, &sym});
475    } else if (config->emachine == EM_PPC && type == R_PPC_PLTREL24 &&
476               p->r_addend >= 0x8000 && sec->file->ppc32Got2) {
477      // Similar to R_MIPS_GPREL{16,32}. If the addend of R_PPC_PLTREL24
478      // indicates that r30 is relative to the input section .got2
479      // (r_addend>=0x8000), after linking, r30 should be relative to the output
480      // section .got2 . To compensate for the shift, adjust r_addend by
481      // ppc32Got->outSecOff.
482      p->r_addend += sec->file->ppc32Got2->outSecOff;
483    }
484  }
485}
486
487// The ARM and AArch64 ABI handle pc-relative relocations to undefined weak
488// references specially. The general rule is that the value of the symbol in
489// this context is the address of the place P. A further special case is that
490// branch relocations to an undefined weak reference resolve to the next
491// instruction.
492static uint32_t getARMUndefinedRelativeWeakVA(RelType type, uint32_t a,
493                                              uint32_t p) {
494  switch (type) {
495  // Unresolved branch relocations to weak references resolve to next
496  // instruction, this will be either 2 or 4 bytes on from P.
497  case R_ARM_THM_JUMP8:
498  case R_ARM_THM_JUMP11:
499    return p + 2 + a;
500  case R_ARM_CALL:
501  case R_ARM_JUMP24:
502  case R_ARM_PC24:
503  case R_ARM_PLT32:
504  case R_ARM_PREL31:
505  case R_ARM_THM_JUMP19:
506  case R_ARM_THM_JUMP24:
507    return p + 4 + a;
508  case R_ARM_THM_CALL:
509    // We don't want an interworking BLX to ARM
510    return p + 5 + a;
511  // Unresolved non branch pc-relative relocations
512  // R_ARM_TARGET2 which can be resolved relatively is not present as it never
513  // targets a weak-reference.
514  case R_ARM_MOVW_PREL_NC:
515  case R_ARM_MOVT_PREL:
516  case R_ARM_REL32:
517  case R_ARM_THM_ALU_PREL_11_0:
518  case R_ARM_THM_MOVW_PREL_NC:
519  case R_ARM_THM_MOVT_PREL:
520  case R_ARM_THM_PC12:
521    return p + a;
522  // p + a is unrepresentable as negative immediates can't be encoded.
523  case R_ARM_THM_PC8:
524    return p;
525  }
526  llvm_unreachable("ARM pc-relative relocation expected\n");
527}
528
529// The comment above getARMUndefinedRelativeWeakVA applies to this function.
530static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t type, uint64_t p) {
531  switch (type) {
532  // Unresolved branch relocations to weak references resolve to next
533  // instruction, this is 4 bytes on from P.
534  case R_AARCH64_CALL26:
535  case R_AARCH64_CONDBR19:
536  case R_AARCH64_JUMP26:
537  case R_AARCH64_TSTBR14:
538    return p + 4;
539  // Unresolved non branch pc-relative relocations
540  case R_AARCH64_PREL16:
541  case R_AARCH64_PREL32:
542  case R_AARCH64_PREL64:
543  case R_AARCH64_ADR_PREL_LO21:
544  case R_AARCH64_LD_PREL_LO19:
545  case R_AARCH64_PLT32:
546    return p;
547  }
548  llvm_unreachable("AArch64 pc-relative relocation expected\n");
549}
550
551static uint64_t getRISCVUndefinedRelativeWeakVA(uint64_t type, uint64_t p) {
552  switch (type) {
553  case R_RISCV_BRANCH:
554  case R_RISCV_JAL:
555  case R_RISCV_CALL:
556  case R_RISCV_CALL_PLT:
557  case R_RISCV_RVC_BRANCH:
558  case R_RISCV_RVC_JUMP:
559  case R_RISCV_PLT32:
560    return p;
561  default:
562    return 0;
563  }
564}
565
566// ARM SBREL relocations are of the form S + A - B where B is the static base
567// The ARM ABI defines base to be "addressing origin of the output segment
568// defining the symbol S". We defined the "addressing origin"/static base to be
569// the base of the PT_LOAD segment containing the Sym.
570// The procedure call standard only defines a Read Write Position Independent
571// RWPI variant so in practice we should expect the static base to be the base
572// of the RW segment.
573static uint64_t getARMStaticBase(const Symbol &sym) {
574  OutputSection *os = sym.getOutputSection();
575  if (!os || !os->ptLoad || !os->ptLoad->firstSec)
576    fatal("SBREL relocation to " + sym.getName() + " without static base");
577  return os->ptLoad->firstSec->addr;
578}
579
580// For R_RISCV_PC_INDIRECT (R_RISCV_PCREL_LO12_{I,S}), the symbol actually
581// points the corresponding R_RISCV_PCREL_HI20 relocation, and the target VA
582// is calculated using PCREL_HI20's symbol.
583//
584// This function returns the R_RISCV_PCREL_HI20 relocation from
585// R_RISCV_PCREL_LO12's symbol and addend.
586static Relocation *getRISCVPCRelHi20(const Symbol *sym, uint64_t addend) {
587  const Defined *d = cast<Defined>(sym);
588  if (!d->section) {
589    errorOrWarn("R_RISCV_PCREL_LO12 relocation points to an absolute symbol: " +
590                sym->getName());
591    return nullptr;
592  }
593  InputSection *isec = cast<InputSection>(d->section);
594
595  if (addend != 0)
596    warn("non-zero addend in R_RISCV_PCREL_LO12 relocation to " +
597         isec->getObjMsg(d->value) + " is ignored");
598
599  // Relocations are sorted by offset, so we can use std::equal_range to do
600  // binary search.
601  Relocation r;
602  r.offset = d->value;
603  auto range =
604      std::equal_range(isec->relocs().begin(), isec->relocs().end(), r,
605                       [](const Relocation &lhs, const Relocation &rhs) {
606                         return lhs.offset < rhs.offset;
607                       });
608
609  for (auto it = range.first; it != range.second; ++it)
610    if (it->type == R_RISCV_PCREL_HI20 || it->type == R_RISCV_GOT_HI20 ||
611        it->type == R_RISCV_TLS_GD_HI20 || it->type == R_RISCV_TLS_GOT_HI20)
612      return &*it;
613
614  errorOrWarn("R_RISCV_PCREL_LO12 relocation points to " +
615              isec->getObjMsg(d->value) +
616              " without an associated R_RISCV_PCREL_HI20 relocation");
617  return nullptr;
618}
619
620// A TLS symbol's virtual address is relative to the TLS segment. Add a
621// target-specific adjustment to produce a thread-pointer-relative offset.
622static int64_t getTlsTpOffset(const Symbol &s) {
623  // On targets that support TLSDESC, _TLS_MODULE_BASE_@tpoff = 0.
624  if (&s == ElfSym::tlsModuleBase)
625    return 0;
626
627  // There are 2 TLS layouts. Among targets we support, x86 uses TLS Variant 2
628  // while most others use Variant 1. At run time TP will be aligned to p_align.
629
630  // Variant 1. TP will be followed by an optional gap (which is the size of 2
631  // pointers on ARM/AArch64, 0 on other targets), followed by alignment
632  // padding, then the static TLS blocks. The alignment padding is added so that
633  // (TP + gap + padding) is congruent to p_vaddr modulo p_align.
634  //
635  // Variant 2. Static TLS blocks, followed by alignment padding are placed
636  // before TP. The alignment padding is added so that (TP - padding -
637  // p_memsz) is congruent to p_vaddr modulo p_align.
638  PhdrEntry *tls = Out::tlsPhdr;
639  switch (config->emachine) {
640    // Variant 1.
641  case EM_ARM:
642  case EM_AARCH64:
643    return s.getVA(0) + config->wordsize * 2 +
644           ((tls->p_vaddr - config->wordsize * 2) & (tls->p_align - 1));
645  case EM_MIPS:
646  case EM_PPC:
647  case EM_PPC64:
648    // Adjusted Variant 1. TP is placed with a displacement of 0x7000, which is
649    // to allow a signed 16-bit offset to reach 0x1000 of TCB/thread-library
650    // data and 0xf000 of the program's TLS segment.
651    return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1)) - 0x7000;
652  case EM_LOONGARCH:
653  case EM_RISCV:
654    return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1));
655
656    // Variant 2.
657  case EM_HEXAGON:
658  case EM_S390:
659  case EM_SPARCV9:
660  case EM_386:
661  case EM_X86_64:
662    return s.getVA(0) - tls->p_memsz -
663           ((-tls->p_vaddr - tls->p_memsz) & (tls->p_align - 1));
664  default:
665    llvm_unreachable("unhandled Config->EMachine");
666  }
667}
668
669uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type,
670                                            int64_t a, uint64_t p,
671                                            const Symbol &sym, RelExpr expr) {
672  switch (expr) {
673  case R_ABS:
674  case R_DTPREL:
675  case R_RELAX_TLS_LD_TO_LE_ABS:
676  case R_RELAX_GOT_PC_NOPIC:
677  case R_RISCV_ADD:
678  case R_RISCV_LEB128:
679    return sym.getVA(a);
680  case R_ADDEND:
681    return a;
682  case R_RELAX_HINT:
683    return 0;
684  case R_ARM_SBREL:
685    return sym.getVA(a) - getARMStaticBase(sym);
686  case R_GOT:
687  case R_RELAX_TLS_GD_TO_IE_ABS:
688    return sym.getGotVA() + a;
689  case R_LOONGARCH_GOT:
690    // The LoongArch TLS GD relocs reuse the R_LARCH_GOT_PC_LO12 reloc type
691    // for their page offsets. The arithmetics are different in the TLS case
692    // so we have to duplicate some logic here.
693    if (sym.hasFlag(NEEDS_TLSGD) && type != R_LARCH_TLS_IE_PC_LO12)
694      // Like R_LOONGARCH_TLSGD_PAGE_PC but taking the absolute value.
695      return in.got->getGlobalDynAddr(sym) + a;
696    return getRelocTargetVA(file, type, a, p, sym, R_GOT);
697  case R_GOTONLY_PC:
698    return in.got->getVA() + a - p;
699  case R_GOTPLTONLY_PC:
700    return in.gotPlt->getVA() + a - p;
701  case R_GOTREL:
702  case R_PPC64_RELAX_TOC:
703    return sym.getVA(a) - in.got->getVA();
704  case R_GOTPLTREL:
705    return sym.getVA(a) - in.gotPlt->getVA();
706  case R_GOTPLT:
707  case R_RELAX_TLS_GD_TO_IE_GOTPLT:
708    return sym.getGotVA() + a - in.gotPlt->getVA();
709  case R_TLSLD_GOT_OFF:
710  case R_GOT_OFF:
711  case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
712    return sym.getGotOffset() + a;
713  case R_AARCH64_GOT_PAGE_PC:
714  case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:
715    return getAArch64Page(sym.getGotVA() + a) - getAArch64Page(p);
716  case R_AARCH64_GOT_PAGE:
717    return sym.getGotVA() + a - getAArch64Page(in.got->getVA());
718  case R_GOT_PC:
719  case R_RELAX_TLS_GD_TO_IE:
720    return sym.getGotVA() + a - p;
721  case R_GOTPLT_GOTREL:
722    return sym.getGotPltVA() + a - in.got->getVA();
723  case R_GOTPLT_PC:
724    return sym.getGotPltVA() + a - p;
725  case R_LOONGARCH_GOT_PAGE_PC:
726    if (sym.hasFlag(NEEDS_TLSGD))
727      return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p, type);
728    return getLoongArchPageDelta(sym.getGotVA() + a, p, type);
729  case R_MIPS_GOTREL:
730    return sym.getVA(a) - in.mipsGot->getGp(file);
731  case R_MIPS_GOT_GP:
732    return in.mipsGot->getGp(file) + a;
733  case R_MIPS_GOT_GP_PC: {
734    // R_MIPS_LO16 expression has R_MIPS_GOT_GP_PC type iif the target
735    // is _gp_disp symbol. In that case we should use the following
736    // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at
737    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
738    // microMIPS variants of these relocations use slightly different
739    // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi()
740    // to correctly handle less-significant bit of the microMIPS symbol.
741    uint64_t v = in.mipsGot->getGp(file) + a - p;
742    if (type == R_MIPS_LO16 || type == R_MICROMIPS_LO16)
743      v += 4;
744    if (type == R_MICROMIPS_LO16 || type == R_MICROMIPS_HI16)
745      v -= 1;
746    return v;
747  }
748  case R_MIPS_GOT_LOCAL_PAGE:
749    // If relocation against MIPS local symbol requires GOT entry, this entry
750    // should be initialized by 'page address'. This address is high 16-bits
751    // of sum the symbol's value and the addend.
752    return in.mipsGot->getVA() + in.mipsGot->getPageEntryOffset(file, sym, a) -
753           in.mipsGot->getGp(file);
754  case R_MIPS_GOT_OFF:
755  case R_MIPS_GOT_OFF32:
756    // In case of MIPS if a GOT relocation has non-zero addend this addend
757    // should be applied to the GOT entry content not to the GOT entry offset.
758    // That is why we use separate expression type.
759    return in.mipsGot->getVA() + in.mipsGot->getSymEntryOffset(file, sym, a) -
760           in.mipsGot->getGp(file);
761  case R_MIPS_TLSGD:
762    return in.mipsGot->getVA() + in.mipsGot->getGlobalDynOffset(file, sym) -
763           in.mipsGot->getGp(file);
764  case R_MIPS_TLSLD:
765    return in.mipsGot->getVA() + in.mipsGot->getTlsIndexOffset(file) -
766           in.mipsGot->getGp(file);
767  case R_AARCH64_PAGE_PC: {
768    uint64_t val = sym.isUndefWeak() ? p + a : sym.getVA(a);
769    return getAArch64Page(val) - getAArch64Page(p);
770  }
771  case R_RISCV_PC_INDIRECT: {
772    if (const Relocation *hiRel = getRISCVPCRelHi20(&sym, a))
773      return getRelocTargetVA(file, hiRel->type, hiRel->addend, sym.getVA(),
774                              *hiRel->sym, hiRel->expr);
775    return 0;
776  }
777  case R_LOONGARCH_PAGE_PC:
778    return getLoongArchPageDelta(sym.getVA(a), p, type);
779  case R_PC:
780  case R_ARM_PCA: {
781    uint64_t dest;
782    if (expr == R_ARM_PCA)
783      // Some PC relative ARM (Thumb) relocations align down the place.
784      p = p & 0xfffffffc;
785    if (sym.isUndefined()) {
786      // On ARM and AArch64 a branch to an undefined weak resolves to the next
787      // instruction, otherwise the place. On RISC-V, resolve an undefined weak
788      // to the same instruction to cause an infinite loop (making the user
789      // aware of the issue) while ensuring no overflow.
790      // Note: if the symbol is hidden, its binding has been converted to local,
791      // so we just check isUndefined() here.
792      if (config->emachine == EM_ARM)
793        dest = getARMUndefinedRelativeWeakVA(type, a, p);
794      else if (config->emachine == EM_AARCH64)
795        dest = getAArch64UndefinedRelativeWeakVA(type, p) + a;
796      else if (config->emachine == EM_PPC)
797        dest = p;
798      else if (config->emachine == EM_RISCV)
799        dest = getRISCVUndefinedRelativeWeakVA(type, p) + a;
800      else
801        dest = sym.getVA(a);
802    } else {
803      dest = sym.getVA(a);
804    }
805    return dest - p;
806  }
807  case R_PLT:
808    return sym.getPltVA() + a;
809  case R_PLT_PC:
810  case R_PPC64_CALL_PLT:
811    return sym.getPltVA() + a - p;
812  case R_LOONGARCH_PLT_PAGE_PC:
813    return getLoongArchPageDelta(sym.getPltVA() + a, p, type);
814  case R_PLT_GOTPLT:
815    return sym.getPltVA() + a - in.gotPlt->getVA();
816  case R_PLT_GOTREL:
817    return sym.getPltVA() + a - in.got->getVA();
818  case R_PPC32_PLTREL:
819    // R_PPC_PLTREL24 uses the addend (usually 0 or 0x8000) to indicate r30
820    // stores _GLOBAL_OFFSET_TABLE_ or .got2+0x8000. The addend is ignored for
821    // target VA computation.
822    return sym.getPltVA() - p;
823  case R_PPC64_CALL: {
824    uint64_t symVA = sym.getVA(a);
825    // If we have an undefined weak symbol, we might get here with a symbol
826    // address of zero. That could overflow, but the code must be unreachable,
827    // so don't bother doing anything at all.
828    if (!symVA)
829      return 0;
830
831    // PPC64 V2 ABI describes two entry points to a function. The global entry
832    // point is used for calls where the caller and callee (may) have different
833    // TOC base pointers and r2 needs to be modified to hold the TOC base for
834    // the callee. For local calls the caller and callee share the same
835    // TOC base and so the TOC pointer initialization code should be skipped by
836    // branching to the local entry point.
837    return symVA - p + getPPC64GlobalEntryToLocalEntryOffset(sym.stOther);
838  }
839  case R_PPC64_TOCBASE:
840    return getPPC64TocBase() + a;
841  case R_RELAX_GOT_PC:
842  case R_PPC64_RELAX_GOT_PC:
843    return sym.getVA(a) - p;
844  case R_RELAX_TLS_GD_TO_LE:
845  case R_RELAX_TLS_IE_TO_LE:
846  case R_RELAX_TLS_LD_TO_LE:
847  case R_TPREL:
848    // It is not very clear what to return if the symbol is undefined. With
849    // --noinhibit-exec, even a non-weak undefined reference may reach here.
850    // Just return A, which matches R_ABS, and the behavior of some dynamic
851    // loaders.
852    if (sym.isUndefined())
853      return a;
854    return getTlsTpOffset(sym) + a;
855  case R_RELAX_TLS_GD_TO_LE_NEG:
856  case R_TPREL_NEG:
857    if (sym.isUndefined())
858      return a;
859    return -getTlsTpOffset(sym) + a;
860  case R_SIZE:
861    return sym.getSize() + a;
862  case R_TLSDESC:
863    return in.got->getTlsDescAddr(sym) + a;
864  case R_TLSDESC_PC:
865    return in.got->getTlsDescAddr(sym) + a - p;
866  case R_TLSDESC_GOTPLT:
867    return in.got->getTlsDescAddr(sym) + a - in.gotPlt->getVA();
868  case R_AARCH64_TLSDESC_PAGE:
869    return getAArch64Page(in.got->getTlsDescAddr(sym) + a) - getAArch64Page(p);
870  case R_TLSGD_GOT:
871    return in.got->getGlobalDynOffset(sym) + a;
872  case R_TLSGD_GOTPLT:
873    return in.got->getGlobalDynAddr(sym) + a - in.gotPlt->getVA();
874  case R_TLSGD_PC:
875    return in.got->getGlobalDynAddr(sym) + a - p;
876  case R_LOONGARCH_TLSGD_PAGE_PC:
877    return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p, type);
878  case R_TLSLD_GOTPLT:
879    return in.got->getVA() + in.got->getTlsIndexOff() + a - in.gotPlt->getVA();
880  case R_TLSLD_GOT:
881    return in.got->getTlsIndexOff() + a;
882  case R_TLSLD_PC:
883    return in.got->getTlsIndexVA() + a - p;
884  default:
885    llvm_unreachable("invalid expression");
886  }
887}
888
889// This function applies relocations to sections without SHF_ALLOC bit.
890// Such sections are never mapped to memory at runtime. Debug sections are
891// an example. Relocations in non-alloc sections are much easier to
892// handle than in allocated sections because it will never need complex
893// treatment such as GOT or PLT (because at runtime no one refers them).
894// So, we handle relocations for non-alloc sections directly in this
895// function as a performance optimization.
896template <class ELFT, class RelTy>
897void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {
898  const unsigned bits = sizeof(typename ELFT::uint) * 8;
899  const TargetInfo &target = *elf::target;
900  const auto emachine = config->emachine;
901  const bool isDebug = isDebugSection(*this);
902  const bool isDebugLine = isDebug && name == ".debug_line";
903  std::optional<uint64_t> tombstone;
904  if (isDebug) {
905    if (name == ".debug_loc" || name == ".debug_ranges")
906      tombstone = 1;
907    else if (name == ".debug_names")
908      tombstone = UINT64_MAX; // tombstone value
909    else
910      tombstone = 0;
911  }
912  for (const auto &patAndValue : llvm::reverse(config->deadRelocInNonAlloc))
913    if (patAndValue.first.match(this->name)) {
914      tombstone = patAndValue.second;
915      break;
916    }
917
918  for (size_t i = 0, relsSize = rels.size(); i != relsSize; ++i) {
919    const RelTy &rel = rels[i];
920    const RelType type = rel.getType(config->isMips64EL);
921    const uint64_t offset = rel.r_offset;
922    uint8_t *bufLoc = buf + offset;
923    int64_t addend = getAddend<ELFT>(rel);
924    if (!RelTy::IsRela)
925      addend += target.getImplicitAddend(bufLoc, type);
926
927    Symbol &sym = getFile<ELFT>()->getRelocTargetSym(rel);
928    RelExpr expr = target.getRelExpr(type, sym, bufLoc);
929    if (expr == R_NONE)
930      continue;
931    auto *ds = dyn_cast<Defined>(&sym);
932
933    if (emachine == EM_RISCV && type == R_RISCV_SET_ULEB128) {
934      if (++i < relsSize &&
935          rels[i].getType(/*isMips64EL=*/false) == R_RISCV_SUB_ULEB128 &&
936          rels[i].r_offset == offset) {
937        uint64_t val;
938        if (!ds && tombstone) {
939          val = *tombstone;
940        } else {
941          val = sym.getVA(addend) -
942                (getFile<ELFT>()->getRelocTargetSym(rels[i]).getVA(0) +
943                 getAddend<ELFT>(rels[i]));
944        }
945        if (overwriteULEB128(bufLoc, val) >= 0x80)
946          errorOrWarn(getLocation(offset) + ": ULEB128 value " + Twine(val) +
947                      " exceeds available space; references '" +
948                      lld::toString(sym) + "'");
949        continue;
950      }
951      errorOrWarn(getLocation(offset) +
952                  ": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128");
953      return;
954    }
955
956    if (tombstone && (expr == R_ABS || expr == R_DTPREL)) {
957      // Resolve relocations in .debug_* referencing (discarded symbols or ICF
958      // folded section symbols) to a tombstone value. Resolving to addend is
959      // unsatisfactory because the result address range may collide with a
960      // valid range of low address, or leave multiple CUs claiming ownership of
961      // the same range of code, which may confuse consumers.
962      //
963      // To address the problems, we use -1 as a tombstone value for most
964      // .debug_* sections. We have to ignore the addend because we don't want
965      // to resolve an address attribute (which may have a non-zero addend) to
966      // -1+addend (wrap around to a low address).
967      //
968      // R_DTPREL type relocations represent an offset into the dynamic thread
969      // vector. The computed value is st_value plus a non-negative offset.
970      // Negative values are invalid, so -1 can be used as the tombstone value.
971      //
972      // If the referenced symbol is relative to a discarded section (due to
973      // --gc-sections, COMDAT, etc), it has been converted to a Undefined.
974      // `ds->folded` catches the ICF folded case. However, resolving a
975      // relocation in .debug_line to -1 would stop debugger users from setting
976      // breakpoints on the folded-in function, so exclude .debug_line.
977      //
978      // For pre-DWARF-v5 .debug_loc and .debug_ranges, -1 is a reserved value
979      // (base address selection entry), use 1 (which is used by GNU ld for
980      // .debug_ranges).
981      //
982      // TODO To reduce disruption, we use 0 instead of -1 as the tombstone
983      // value. Enable -1 in a future release.
984      if (!ds || (ds->folded && !isDebugLine)) {
985        // If -z dead-reloc-in-nonalloc= is specified, respect it.
986        uint64_t value = SignExtend64<bits>(*tombstone);
987        // For a 32-bit local TU reference in .debug_names, X86_64::relocate
988        // requires that the unsigned value for R_X86_64_32 is truncated to
989        // 32-bit. Other 64-bit targets's don't discern signed/unsigned 32-bit
990        // absolute relocations and do not need this change.
991        if (emachine == EM_X86_64 && type == R_X86_64_32)
992          value = static_cast<uint32_t>(value);
993        target.relocateNoSym(bufLoc, type, value);
994        continue;
995      }
996    }
997
998    // For a relocatable link, content relocated by RELA remains unchanged and
999    // we can stop here, while content relocated by REL referencing STT_SECTION
1000    // needs updating implicit addends.
1001    if (config->relocatable && (RelTy::IsRela || sym.type != STT_SECTION))
1002      continue;
1003
1004    // R_ABS/R_DTPREL and some other relocations can be used from non-SHF_ALLOC
1005    // sections.
1006    if (LLVM_LIKELY(expr == R_ABS) || expr == R_DTPREL || expr == R_GOTPLTREL ||
1007        expr == R_RISCV_ADD) {
1008      target.relocateNoSym(bufLoc, type, SignExtend64<bits>(sym.getVA(addend)));
1009      continue;
1010    }
1011
1012    if (expr == R_SIZE) {
1013      target.relocateNoSym(bufLoc, type,
1014                           SignExtend64<bits>(sym.getSize() + addend));
1015      continue;
1016    }
1017
1018    std::string msg = getLocation(offset) + ": has non-ABS relocation " +
1019                      toString(type) + " against symbol '" + toString(sym) +
1020                      "'";
1021    if (expr != R_PC && !(emachine == EM_386 && type == R_386_GOTPC)) {
1022      errorOrWarn(msg);
1023      return;
1024    }
1025
1026    // If the control reaches here, we found a PC-relative relocation in a
1027    // non-ALLOC section. Since non-ALLOC section is not loaded into memory
1028    // at runtime, the notion of PC-relative doesn't make sense here. So,
1029    // this is a usage error. However, GNU linkers historically accept such
1030    // relocations without any errors and relocate them as if they were at
1031    // address 0. For bug-compatibility, we accept them with warnings. We
1032    // know Steel Bank Common Lisp as of 2018 have this bug.
1033    //
1034    // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations
1035    // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed in
1036    // 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we need to
1037    // keep this bug-compatible code for a while.
1038    warn(msg);
1039    target.relocateNoSym(
1040        bufLoc, type,
1041        SignExtend64<bits>(sym.getVA(addend - offset - outSecOff)));
1042  }
1043}
1044
1045template <class ELFT>
1046void InputSectionBase::relocate(uint8_t *buf, uint8_t *bufEnd) {
1047  if ((flags & SHF_EXECINSTR) && LLVM_UNLIKELY(getFile<ELFT>()->splitStack))
1048    adjustSplitStackFunctionPrologues<ELFT>(buf, bufEnd);
1049
1050  if (flags & SHF_ALLOC) {
1051    target->relocateAlloc(*this, buf);
1052    return;
1053  }
1054
1055  auto *sec = cast<InputSection>(this);
1056  // For a relocatable link, also call relocateNonAlloc() to rewrite applicable
1057  // locations with tombstone values.
1058  const RelsOrRelas<ELFT> rels = sec->template relsOrRelas<ELFT>();
1059  if (rels.areRelocsRel())
1060    sec->relocateNonAlloc<ELFT>(buf, rels.rels);
1061  else
1062    sec->relocateNonAlloc<ELFT>(buf, rels.relas);
1063}
1064
1065// For each function-defining prologue, find any calls to __morestack,
1066// and replace them with calls to __morestack_non_split.
1067static void switchMorestackCallsToMorestackNonSplit(
1068    DenseSet<Defined *> &prologues,
1069    SmallVector<Relocation *, 0> &morestackCalls) {
1070
1071  // If the target adjusted a function's prologue, all calls to
1072  // __morestack inside that function should be switched to
1073  // __morestack_non_split.
1074  Symbol *moreStackNonSplit = symtab.find("__morestack_non_split");
1075  if (!moreStackNonSplit) {
1076    error("mixing split-stack objects requires a definition of "
1077          "__morestack_non_split");
1078    return;
1079  }
1080
1081  // Sort both collections to compare addresses efficiently.
1082  llvm::sort(morestackCalls, [](const Relocation *l, const Relocation *r) {
1083    return l->offset < r->offset;
1084  });
1085  std::vector<Defined *> functions(prologues.begin(), prologues.end());
1086  llvm::sort(functions, [](const Defined *l, const Defined *r) {
1087    return l->value < r->value;
1088  });
1089
1090  auto it = morestackCalls.begin();
1091  for (Defined *f : functions) {
1092    // Find the first call to __morestack within the function.
1093    while (it != morestackCalls.end() && (*it)->offset < f->value)
1094      ++it;
1095    // Adjust all calls inside the function.
1096    while (it != morestackCalls.end() && (*it)->offset < f->value + f->size) {
1097      (*it)->sym = moreStackNonSplit;
1098      ++it;
1099    }
1100  }
1101}
1102
1103static bool enclosingPrologueAttempted(uint64_t offset,
1104                                       const DenseSet<Defined *> &prologues) {
1105  for (Defined *f : prologues)
1106    if (f->value <= offset && offset < f->value + f->size)
1107      return true;
1108  return false;
1109}
1110
1111// If a function compiled for split stack calls a function not
1112// compiled for split stack, then the caller needs its prologue
1113// adjusted to ensure that the called function will have enough stack
1114// available. Find those functions, and adjust their prologues.
1115template <class ELFT>
1116void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf,
1117                                                         uint8_t *end) {
1118  DenseSet<Defined *> prologues;
1119  SmallVector<Relocation *, 0> morestackCalls;
1120
1121  for (Relocation &rel : relocs()) {
1122    // Ignore calls into the split-stack api.
1123    if (rel.sym->getName().starts_with("__morestack")) {
1124      if (rel.sym->getName().equals("__morestack"))
1125        morestackCalls.push_back(&rel);
1126      continue;
1127    }
1128
1129    // A relocation to non-function isn't relevant. Sometimes
1130    // __morestack is not marked as a function, so this check comes
1131    // after the name check.
1132    if (rel.sym->type != STT_FUNC)
1133      continue;
1134
1135    // If the callee's-file was compiled with split stack, nothing to do.  In
1136    // this context, a "Defined" symbol is one "defined by the binary currently
1137    // being produced". So an "undefined" symbol might be provided by a shared
1138    // library. It is not possible to tell how such symbols were compiled, so be
1139    // conservative.
1140    if (Defined *d = dyn_cast<Defined>(rel.sym))
1141      if (InputSection *isec = cast_or_null<InputSection>(d->section))
1142        if (!isec || !isec->getFile<ELFT>() || isec->getFile<ELFT>()->splitStack)
1143          continue;
1144
1145    if (enclosingPrologueAttempted(rel.offset, prologues))
1146      continue;
1147
1148    if (Defined *f = getEnclosingFunction(rel.offset)) {
1149      prologues.insert(f);
1150      if (target->adjustPrologueForCrossSplitStack(buf + f->value, end,
1151                                                   f->stOther))
1152        continue;
1153      if (!getFile<ELFT>()->someNoSplitStack)
1154        error(lld::toString(this) + ": " + f->getName() +
1155              " (with -fsplit-stack) calls " + rel.sym->getName() +
1156              " (without -fsplit-stack), but couldn't adjust its prologue");
1157    }
1158  }
1159
1160  if (target->needsMoreStackNonSplit)
1161    switchMorestackCallsToMorestackNonSplit(prologues, morestackCalls);
1162}
1163
1164template <class ELFT> void InputSection::writeTo(uint8_t *buf) {
1165  if (LLVM_UNLIKELY(type == SHT_NOBITS))
1166    return;
1167  // If -r or --emit-relocs is given, then an InputSection
1168  // may be a relocation section.
1169  if (LLVM_UNLIKELY(type == SHT_RELA)) {
1170    copyRelocations<ELFT, typename ELFT::Rela>(buf);
1171    return;
1172  }
1173  if (LLVM_UNLIKELY(type == SHT_REL)) {
1174    copyRelocations<ELFT, typename ELFT::Rel>(buf);
1175    return;
1176  }
1177
1178  // If -r is given, we may have a SHT_GROUP section.
1179  if (LLVM_UNLIKELY(type == SHT_GROUP)) {
1180    copyShtGroup<ELFT>(buf);
1181    return;
1182  }
1183
1184  // If this is a compressed section, uncompress section contents directly
1185  // to the buffer.
1186  if (compressed) {
1187    auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content_);
1188    auto compressed = ArrayRef<uint8_t>(content_, compressedSize)
1189                          .slice(sizeof(typename ELFT::Chdr));
1190    size_t size = this->size;
1191    if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB
1192                      ? compression::zlib::decompress(compressed, buf, size)
1193                      : compression::zstd::decompress(compressed, buf, size))
1194      fatal(toString(this) +
1195            ": decompress failed: " + llvm::toString(std::move(e)));
1196    uint8_t *bufEnd = buf + size;
1197    relocate<ELFT>(buf, bufEnd);
1198    return;
1199  }
1200
1201  // Copy section contents from source object file to output file
1202  // and then apply relocations.
1203  memcpy(buf, content().data(), content().size());
1204  relocate<ELFT>(buf, buf + content().size());
1205}
1206
1207void InputSection::replace(InputSection *other) {
1208  addralign = std::max(addralign, other->addralign);
1209
1210  // When a section is replaced with another section that was allocated to
1211  // another partition, the replacement section (and its associated sections)
1212  // need to be placed in the main partition so that both partitions will be
1213  // able to access it.
1214  if (partition != other->partition) {
1215    partition = 1;
1216    for (InputSection *isec : dependentSections)
1217      isec->partition = 1;
1218  }
1219
1220  other->repl = repl;
1221  other->markDead();
1222}
1223
1224template <class ELFT>
1225EhInputSection::EhInputSection(ObjFile<ELFT> &f,
1226                               const typename ELFT::Shdr &header,
1227                               StringRef name)
1228    : InputSectionBase(f, header, name, InputSectionBase::EHFrame) {}
1229
1230SyntheticSection *EhInputSection::getParent() const {
1231  return cast_or_null<SyntheticSection>(parent);
1232}
1233
1234// .eh_frame is a sequence of CIE or FDE records.
1235// This function splits an input section into records and returns them.
1236template <class ELFT> void EhInputSection::split() {
1237  const RelsOrRelas<ELFT> rels = relsOrRelas<ELFT>();
1238  // getReloc expects the relocations to be sorted by r_offset. See the comment
1239  // in scanRelocs.
1240  if (rels.areRelocsRel()) {
1241    SmallVector<typename ELFT::Rel, 0> storage;
1242    split<ELFT>(sortRels(rels.rels, storage));
1243  } else {
1244    SmallVector<typename ELFT::Rela, 0> storage;
1245    split<ELFT>(sortRels(rels.relas, storage));
1246  }
1247}
1248
1249template <class ELFT, class RelTy>
1250void EhInputSection::split(ArrayRef<RelTy> rels) {
1251  ArrayRef<uint8_t> d = content();
1252  const char *msg = nullptr;
1253  unsigned relI = 0;
1254  while (!d.empty()) {
1255    if (d.size() < 4) {
1256      msg = "CIE/FDE too small";
1257      break;
1258    }
1259    uint64_t size = endian::read32<ELFT::TargetEndianness>(d.data());
1260    if (size == 0) // ZERO terminator
1261      break;
1262    uint32_t id = endian::read32<ELFT::TargetEndianness>(d.data() + 4);
1263    size += 4;
1264    if (LLVM_UNLIKELY(size > d.size())) {
1265      // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
1266      // but we do not support that format yet.
1267      msg = size == UINT32_MAX + uint64_t(4)
1268                ? "CIE/FDE too large"
1269                : "CIE/FDE ends past the end of the section";
1270      break;
1271    }
1272
1273    // Find the first relocation that points to [off,off+size). Relocations
1274    // have been sorted by r_offset.
1275    const uint64_t off = d.data() - content().data();
1276    while (relI != rels.size() && rels[relI].r_offset < off)
1277      ++relI;
1278    unsigned firstRel = -1;
1279    if (relI != rels.size() && rels[relI].r_offset < off + size)
1280      firstRel = relI;
1281    (id == 0 ? cies : fdes).emplace_back(off, this, size, firstRel);
1282    d = d.slice(size);
1283  }
1284  if (msg)
1285    errorOrWarn("corrupted .eh_frame: " + Twine(msg) + "\n>>> defined in " +
1286                getObjMsg(d.data() - content().data()));
1287}
1288
1289// Return the offset in an output section for a given input offset.
1290uint64_t EhInputSection::getParentOffset(uint64_t offset) const {
1291  auto it = partition_point(
1292      fdes, [=](EhSectionPiece p) { return p.inputOff <= offset; });
1293  if (it == fdes.begin() || it[-1].inputOff + it[-1].size <= offset) {
1294    it = partition_point(
1295        cies, [=](EhSectionPiece p) { return p.inputOff <= offset; });
1296    if (it == cies.begin()) // invalid piece
1297      return offset;
1298  }
1299  if (it[-1].outputOff == -1) // invalid piece
1300    return offset - it[-1].inputOff;
1301  return it[-1].outputOff + (offset - it[-1].inputOff);
1302}
1303
1304static size_t findNull(StringRef s, size_t entSize) {
1305  for (unsigned i = 0, n = s.size(); i != n; i += entSize) {
1306    const char *b = s.begin() + i;
1307    if (std::all_of(b, b + entSize, [](char c) { return c == 0; }))
1308      return i;
1309  }
1310  llvm_unreachable("");
1311}
1312
1313// Split SHF_STRINGS section. Such section is a sequence of
1314// null-terminated strings.
1315void MergeInputSection::splitStrings(StringRef s, size_t entSize) {
1316  const bool live = !(flags & SHF_ALLOC) || !config->gcSections;
1317  const char *p = s.data(), *end = s.data() + s.size();
1318  if (!std::all_of(end - entSize, end, [](char c) { return c == 0; }))
1319    fatal(toString(this) + ": string is not null terminated");
1320  if (entSize == 1) {
1321    // Optimize the common case.
1322    do {
1323      size_t size = strlen(p);
1324      pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live);
1325      p += size + 1;
1326    } while (p != end);
1327  } else {
1328    do {
1329      size_t size = findNull(StringRef(p, end - p), entSize);
1330      pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live);
1331      p += size + entSize;
1332    } while (p != end);
1333  }
1334}
1335
1336// Split non-SHF_STRINGS section. Such section is a sequence of
1337// fixed size records.
1338void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> data,
1339                                        size_t entSize) {
1340  size_t size = data.size();
1341  assert((size % entSize) == 0);
1342  const bool live = !(flags & SHF_ALLOC) || !config->gcSections;
1343
1344  pieces.resize_for_overwrite(size / entSize);
1345  for (size_t i = 0, j = 0; i != size; i += entSize, j++)
1346    pieces[j] = {i, (uint32_t)xxh3_64bits(data.slice(i, entSize)), live};
1347}
1348
1349template <class ELFT>
1350MergeInputSection::MergeInputSection(ObjFile<ELFT> &f,
1351                                     const typename ELFT::Shdr &header,
1352                                     StringRef name)
1353    : InputSectionBase(f, header, name, InputSectionBase::Merge) {}
1354
1355MergeInputSection::MergeInputSection(uint64_t flags, uint32_t type,
1356                                     uint64_t entsize, ArrayRef<uint8_t> data,
1357                                     StringRef name)
1358    : InputSectionBase(nullptr, flags, type, entsize, /*Link*/ 0, /*Info*/ 0,
1359                       /*Alignment*/ entsize, data, name, SectionBase::Merge) {}
1360
1361// This function is called after we obtain a complete list of input sections
1362// that need to be linked. This is responsible to split section contents
1363// into small chunks for further processing.
1364//
1365// Note that this function is called from parallelForEach. This must be
1366// thread-safe (i.e. no memory allocation from the pools).
1367void MergeInputSection::splitIntoPieces() {
1368  assert(pieces.empty());
1369
1370  if (flags & SHF_STRINGS)
1371    splitStrings(toStringRef(contentMaybeDecompress()), entsize);
1372  else
1373    splitNonStrings(contentMaybeDecompress(), entsize);
1374}
1375
1376SectionPiece &MergeInputSection::getSectionPiece(uint64_t offset) {
1377  if (content().size() <= offset)
1378    fatal(toString(this) + ": offset is outside the section");
1379  return partition_point(
1380      pieces, [=](SectionPiece p) { return p.inputOff <= offset; })[-1];
1381}
1382
1383// Return the offset in an output section for a given input offset.
1384uint64_t MergeInputSection::getParentOffset(uint64_t offset) const {
1385  const SectionPiece &piece = getSectionPiece(offset);
1386  return piece.outputOff + (offset - piece.inputOff);
1387}
1388
1389template InputSection::InputSection(ObjFile<ELF32LE> &, const ELF32LE::Shdr &,
1390                                    StringRef);
1391template InputSection::InputSection(ObjFile<ELF32BE> &, const ELF32BE::Shdr &,
1392                                    StringRef);
1393template InputSection::InputSection(ObjFile<ELF64LE> &, const ELF64LE::Shdr &,
1394                                    StringRef);
1395template InputSection::InputSection(ObjFile<ELF64BE> &, const ELF64BE::Shdr &,
1396                                    StringRef);
1397
1398template void InputSection::writeTo<ELF32LE>(uint8_t *);
1399template void InputSection::writeTo<ELF32BE>(uint8_t *);
1400template void InputSection::writeTo<ELF64LE>(uint8_t *);
1401template void InputSection::writeTo<ELF64BE>(uint8_t *);
1402
1403template RelsOrRelas<ELF32LE> InputSectionBase::relsOrRelas<ELF32LE>() const;
1404template RelsOrRelas<ELF32BE> InputSectionBase::relsOrRelas<ELF32BE>() const;
1405template RelsOrRelas<ELF64LE> InputSectionBase::relsOrRelas<ELF64LE>() const;
1406template RelsOrRelas<ELF64BE> InputSectionBase::relsOrRelas<ELF64BE>() const;
1407
1408template MergeInputSection::MergeInputSection(ObjFile<ELF32LE> &,
1409                                              const ELF32LE::Shdr &, StringRef);
1410template MergeInputSection::MergeInputSection(ObjFile<ELF32BE> &,
1411                                              const ELF32BE::Shdr &, StringRef);
1412template MergeInputSection::MergeInputSection(ObjFile<ELF64LE> &,
1413                                              const ELF64LE::Shdr &, StringRef);
1414template MergeInputSection::MergeInputSection(ObjFile<ELF64BE> &,
1415                                              const ELF64BE::Shdr &, StringRef);
1416
1417template EhInputSection::EhInputSection(ObjFile<ELF32LE> &,
1418                                        const ELF32LE::Shdr &, StringRef);
1419template EhInputSection::EhInputSection(ObjFile<ELF32BE> &,
1420                                        const ELF32BE::Shdr &, StringRef);
1421template EhInputSection::EhInputSection(ObjFile<ELF64LE> &,
1422                                        const ELF64LE::Shdr &, StringRef);
1423template EhInputSection::EhInputSection(ObjFile<ELF64BE> &,
1424                                        const ELF64BE::Shdr &, StringRef);
1425
1426template void EhInputSection::split<ELF32LE>();
1427template void EhInputSection::split<ELF32BE>();
1428template void EhInputSection::split<ELF64LE>();
1429template void EhInputSection::split<ELF64BE>();
1430