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