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 "InputFiles.h"
11 #include "OutputSegment.h"
12 #include "Symbols.h"
13 #include "SyntheticSections.h"
14 #include "Target.h"
15 #include "Writer.h"
16 #include "lld/Common/Memory.h"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/xxhash.h"
19 
20 using namespace llvm;
21 using namespace llvm::MachO;
22 using namespace llvm::support;
23 using namespace lld;
24 using namespace lld::macho;
25 
26 std::vector<InputSection *> macho::inputSections;
27 
28 uint64_t ConcatInputSection::getFileOffset(uint64_t off) const {
29   return parent->fileOff + outSecFileOff + off;
30 }
31 
32 uint64_t InputSection::getFileSize() const {
33   return isZeroFill(flags) ? 0 : getSize();
34 }
35 
36 uint64_t InputSection::getVA(uint64_t off) const {
37   return parent->addr + getOffset(off);
38 }
39 
40 static uint64_t resolveSymbolVA(const Symbol *sym, uint8_t type) {
41   const RelocAttrs &relocAttrs = target->getRelocAttrs(type);
42   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH))
43     return sym->resolveBranchVA();
44   else if (relocAttrs.hasAttr(RelocAttrBits::GOT))
45     return sym->resolveGotVA();
46   else if (relocAttrs.hasAttr(RelocAttrBits::TLV))
47     return sym->resolveTlvVA();
48   return sym->getVA();
49 }
50 
51 void InputSection::writeTo(uint8_t *buf) {
52   assert(!shouldOmitFromOutput());
53 
54   if (getFileSize() == 0)
55     return;
56 
57   memcpy(buf, data.data(), data.size());
58 
59   for (size_t i = 0; i < relocs.size(); i++) {
60     const Reloc &r = relocs[i];
61     uint8_t *loc = buf + r.offset;
62     uint64_t referentVA = 0;
63     if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
64       const Symbol *fromSym = r.referent.get<Symbol *>();
65       const Reloc &minuend = relocs[++i];
66       uint64_t minuendVA;
67       if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>())
68         minuendVA = toSym->getVA() + minuend.addend;
69       else {
70         auto *referentIsec = minuend.referent.get<InputSection *>();
71         assert(!referentIsec->shouldOmitFromOutput());
72         minuendVA = referentIsec->getVA(minuend.addend);
73       }
74       referentVA = minuendVA - fromSym->getVA();
75     } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
76       if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
77           !referentSym->isInGot())
78         target->relaxGotLoad(loc, r.type);
79       referentVA = resolveSymbolVA(referentSym, r.type) + r.addend;
80 
81       if (isThreadLocalVariables(flags)) {
82         // References from thread-local variable sections are treated as offsets
83         // relative to the start of the thread-local data memory area, which
84         // is initialized via copying all the TLV data sections (which are all
85         // contiguous).
86         if (isa<Defined>(referentSym))
87           referentVA -= firstTLVDataSection->addr;
88       }
89     } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
90       assert(!referentIsec->shouldOmitFromOutput());
91       referentVA = referentIsec->getVA(r.addend);
92     }
93     target->relocateOne(loc, r, referentVA, getVA(r.offset));
94   }
95 }
96 
97 void CStringInputSection::splitIntoPieces() {
98   size_t off = 0;
99   StringRef s = toStringRef(data);
100   while (!s.empty()) {
101     size_t end = s.find(0);
102     if (end == StringRef::npos)
103       fatal(toString(this) + ": string is not null terminated");
104     size_t size = end + 1;
105     pieces.emplace_back(off, xxHash64(s.substr(0, size)));
106     s = s.substr(size);
107     off += size;
108   }
109 }
110 
111 const StringPiece &CStringInputSection::getStringPiece(uint64_t off) const {
112   if (off >= data.size())
113     fatal(toString(this) + ": offset is outside the section");
114 
115   auto it =
116       partition_point(pieces, [=](StringPiece p) { return p.inSecOff <= off; });
117   return it[-1];
118 }
119 
120 uint64_t CStringInputSection::getFileOffset(uint64_t off) const {
121   return parent->fileOff + getOffset(off);
122 }
123 
124 uint64_t CStringInputSection::getOffset(uint64_t off) const {
125   const StringPiece &piece = getStringPiece(off);
126   uint64_t addend = off - piece.inSecOff;
127   return piece.outSecOff + addend;
128 }
129 
130 bool macho::isCodeSection(const InputSection *isec) {
131   uint32_t type = isec->flags & SECTION_TYPE;
132   if (type != S_REGULAR && type != S_COALESCED)
133     return false;
134 
135   uint32_t attr = isec->flags & SECTION_ATTRIBUTES_USR;
136   if (attr == S_ATTR_PURE_INSTRUCTIONS)
137     return true;
138 
139   if (isec->segname == segment_names::text)
140     return StringSwitch<bool>(isec->name)
141         .Cases(section_names::textCoalNt, section_names::staticInit, true)
142         .Default(false);
143 
144   return false;
145 }
146 
147 std::string lld::toString(const InputSection *isec) {
148   return (toString(isec->file) + ":(" + isec->name + ")").str();
149 }
150