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