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 "UnwindInfoSection.h"
16 #include "Writer.h"
17 #include "lld/Common/Memory.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/xxhash.h"
20 
21 using namespace llvm;
22 using namespace llvm::MachO;
23 using namespace llvm::support;
24 using namespace lld;
25 using namespace lld::macho;
26 
27 std::vector<InputSection *> macho::inputSections;
28 
29 uint64_t InputSection::getFileSize() const {
30   return isZeroFill(flags) ? 0 : getSize();
31 }
32 
33 uint64_t InputSection::getVA(uint64_t off) const {
34   return parent->addr + getOffset(off);
35 }
36 
37 static uint64_t resolveSymbolVA(const Symbol *sym, uint8_t type) {
38   const RelocAttrs &relocAttrs = target->getRelocAttrs(type);
39   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH))
40     return sym->resolveBranchVA();
41   else if (relocAttrs.hasAttr(RelocAttrBits::GOT))
42     return sym->resolveGotVA();
43   else if (relocAttrs.hasAttr(RelocAttrBits::TLV))
44     return sym->resolveTlvVA();
45   return sym->getVA();
46 }
47 
48 // ICF needs to hash any section that might potentially be duplicated so
49 // that it can match on content rather than identity.
50 bool InputSection::isHashableForICF(bool isText) const {
51   if (auto const *concatIsec = dyn_cast<ConcatInputSection>(this))
52     if (concatIsec->shouldOmitFromOutput())
53       return false;
54   switch (sectionType(flags)) {
55   case S_REGULAR:
56     if (isText)
57       return !hasPersonality;
58     // One might hope that we could hash __TEXT,__const subsections to fold
59     // references to duplicated values, but alas, many tests fail.
60     return false;
61   case S_CSTRING_LITERALS:
62   case S_4BYTE_LITERALS:
63   case S_8BYTE_LITERALS:
64   case S_16BYTE_LITERALS:
65   case S_LITERAL_POINTERS:
66     // FIXME(gkm): once literal sections are deduplicated, their content and
67     // identity correlate, so we can assign unique IDs to them rather than hash
68     // them.
69     return true;
70   case S_ZEROFILL:
71   case S_GB_ZEROFILL:
72   case S_NON_LAZY_SYMBOL_POINTERS:
73   case S_LAZY_SYMBOL_POINTERS:
74   case S_SYMBOL_STUBS:
75   case S_MOD_INIT_FUNC_POINTERS:
76   case S_MOD_TERM_FUNC_POINTERS:
77   case S_COALESCED:
78   case S_INTERPOSING:
79   case S_DTRACE_DOF:
80   case S_LAZY_DYLIB_SYMBOL_POINTERS:
81   case S_THREAD_LOCAL_REGULAR:
82   case S_THREAD_LOCAL_ZEROFILL:
83   case S_THREAD_LOCAL_VARIABLES:
84   case S_THREAD_LOCAL_VARIABLE_POINTERS:
85   case S_THREAD_LOCAL_INIT_FUNCTION_POINTERS:
86     return false;
87   default:
88     llvm_unreachable("Section type");
89   }
90 }
91 
92 void InputSection::hashForICF() {
93   assert(data.data()); // zeroFill section data has nullptr with non-zero size
94   assert(icfEqClass[0] == 0); // don't overwrite a unique ID!
95   // Turn-on the top bit to guarantee that valid hashes have no collisions
96   // with the small-integer unique IDs for ICF-ineligible sections
97   icfEqClass[0] = xxHash64(data) | (1ull << 63);
98 }
99 
100 void ConcatInputSection::foldIdentical(ConcatInputSection *copy) {
101   align = std::max(align, copy->align);
102   copy->live = false;
103   copy->wasCoalesced = true;
104   numRefs += copy->numRefs;
105   copy->numRefs = 0;
106   copy->replacement = this;
107 }
108 
109 void ConcatInputSection::writeTo(uint8_t *buf) {
110   assert(!shouldOmitFromOutput());
111 
112   if (getFileSize() == 0)
113     return;
114 
115   memcpy(buf, data.data(), data.size());
116 
117   for (size_t i = 0; i < relocs.size(); i++) {
118     const Reloc &r = relocs[i];
119     uint8_t *loc = buf + r.offset;
120     uint64_t referentVA = 0;
121     if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
122       const Symbol *fromSym = r.referent.get<Symbol *>();
123       const Reloc &minuend = relocs[++i];
124       uint64_t minuendVA;
125       if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>())
126         minuendVA = toSym->getVA() + minuend.addend;
127       else {
128         auto *referentIsec = minuend.referent.get<InputSection *>();
129         assert(!::shouldOmitFromOutput(referentIsec));
130         minuendVA = referentIsec->getVA(minuend.addend);
131       }
132       referentVA = minuendVA - fromSym->getVA();
133     } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
134       if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
135           !referentSym->isInGot())
136         target->relaxGotLoad(loc, r.type);
137       referentVA = resolveSymbolVA(referentSym, r.type) + r.addend;
138 
139       if (isThreadLocalVariables(flags)) {
140         // References from thread-local variable sections are treated as offsets
141         // relative to the start of the thread-local data memory area, which
142         // is initialized via copying all the TLV data sections (which are all
143         // contiguous).
144         if (isa<Defined>(referentSym))
145           referentVA -= firstTLVDataSection->addr;
146       }
147     } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
148       assert(!::shouldOmitFromOutput(referentIsec));
149       referentVA = referentIsec->getVA(r.addend);
150     }
151     target->relocateOne(loc, r, referentVA, getVA() + r.offset);
152   }
153 }
154 
155 void CStringInputSection::splitIntoPieces() {
156   size_t off = 0;
157   StringRef s = toStringRef(data);
158   while (!s.empty()) {
159     size_t end = s.find(0);
160     if (end == StringRef::npos)
161       fatal(toString(this) + ": string is not null terminated");
162     size_t size = end + 1;
163     pieces.emplace_back(off, xxHash64(s.substr(0, size)));
164     s = s.substr(size);
165     off += size;
166   }
167 }
168 
169 StringPiece &CStringInputSection::getStringPiece(uint64_t off) {
170   if (off >= data.size())
171     fatal(toString(this) + ": offset is outside the section");
172 
173   auto it =
174       partition_point(pieces, [=](StringPiece p) { return p.inSecOff <= off; });
175   return it[-1];
176 }
177 
178 const StringPiece &CStringInputSection::getStringPiece(uint64_t off) const {
179   return const_cast<CStringInputSection *>(this)->getStringPiece(off);
180 }
181 
182 uint64_t CStringInputSection::getOffset(uint64_t off) const {
183   const StringPiece &piece = getStringPiece(off);
184   uint64_t addend = off - piece.inSecOff;
185   return piece.outSecOff + addend;
186 }
187 
188 WordLiteralInputSection::WordLiteralInputSection(StringRef segname,
189                                                  StringRef name,
190                                                  InputFile *file,
191                                                  ArrayRef<uint8_t> data,
192                                                  uint32_t align, uint32_t flags)
193     : InputSection(WordLiteralKind, segname, name, file, data, align, flags) {
194   switch (sectionType(flags)) {
195   case S_4BYTE_LITERALS:
196     power2LiteralSize = 2;
197     break;
198   case S_8BYTE_LITERALS:
199     power2LiteralSize = 3;
200     break;
201   case S_16BYTE_LITERALS:
202     power2LiteralSize = 4;
203     break;
204   default:
205     llvm_unreachable("invalid literal section type");
206   }
207 
208   live.resize(data.size() >> power2LiteralSize, !config->deadStrip);
209 }
210 
211 uint64_t WordLiteralInputSection::getOffset(uint64_t off) const {
212   auto *osec = cast<WordLiteralSection>(parent);
213   const uint8_t *buf = data.data();
214   switch (sectionType(flags)) {
215   case S_4BYTE_LITERALS:
216     return osec->getLiteral4Offset(buf + off);
217   case S_8BYTE_LITERALS:
218     return osec->getLiteral8Offset(buf + off);
219   case S_16BYTE_LITERALS:
220     return osec->getLiteral16Offset(buf + off);
221   default:
222     llvm_unreachable("invalid literal section type");
223   }
224 }
225 
226 bool macho::isCodeSection(const InputSection *isec) {
227   uint32_t type = sectionType(isec->flags);
228   if (type != S_REGULAR && type != S_COALESCED)
229     return false;
230 
231   uint32_t attr = isec->flags & SECTION_ATTRIBUTES_USR;
232   if (attr == S_ATTR_PURE_INSTRUCTIONS)
233     return true;
234 
235   if (isec->segname == segment_names::text)
236     return StringSwitch<bool>(isec->name)
237         .Cases(section_names::textCoalNt, section_names::staticInit, true)
238         .Default(false);
239 
240   return false;
241 }
242 
243 std::string lld::toString(const InputSection *isec) {
244   return (toString(isec->file) + ":(" + isec->name + ")").str();
245 }
246