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 // Verify ConcatInputSection's size on 64-bit builds. The size of std::vector
30 // can differ based on STL debug levels (e.g. iterator debugging on MSVC's STL),
31 // so account for that.
32 static_assert(sizeof(void *) != 8 ||
33                   sizeof(ConcatInputSection) == sizeof(std::vector<Reloc>) + 88,
34               "Try to minimize ConcatInputSection's size, we create many "
35               "instances of it");
36 
37 std::vector<ConcatInputSection *> macho::inputSections;
38 
39 uint64_t InputSection::getFileSize() const {
40   return isZeroFill(getFlags()) ? 0 : getSize();
41 }
42 
43 uint64_t InputSection::getVA(uint64_t off) const {
44   return parent->addr + getOffset(off);
45 }
46 
47 static uint64_t resolveSymbolVA(const Symbol *sym, uint8_t type) {
48   const RelocAttrs &relocAttrs = target->getRelocAttrs(type);
49   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH))
50     return sym->resolveBranchVA();
51   if (relocAttrs.hasAttr(RelocAttrBits::GOT))
52     return sym->resolveGotVA();
53   if (relocAttrs.hasAttr(RelocAttrBits::TLV))
54     return sym->resolveTlvVA();
55   return sym->getVA();
56 }
57 
58 const Defined *InputSection::getContainingSymbol(uint64_t off) const {
59   auto *nextSym = llvm::upper_bound(
60       symbols, off, [](uint64_t a, const Defined *b) { return a < b->value; });
61   if (nextSym == symbols.begin())
62     return nullptr;
63   return *std::prev(nextSym);
64 }
65 
66 std::string InputSection::getLocation(uint64_t off) const {
67   // First, try to find a symbol that's near the offset. Use it as a reference
68   // point.
69   if (auto *sym = getContainingSymbol(off))
70     return (toString(getFile()) + ":(symbol " + sym->getName() + "+0x" +
71             Twine::utohexstr(off - sym->value) + ")")
72         .str();
73 
74   // If that fails, use the section itself as a reference point.
75   for (const Subsection &subsec : section.subsections) {
76     if (subsec.isec == this) {
77       off += subsec.offset;
78       break;
79     }
80   }
81 
82   return (toString(getFile()) + ":(" + getName() + "+0x" +
83           Twine::utohexstr(off) + ")")
84       .str();
85 }
86 
87 std::string InputSection::getSourceLocation(uint64_t off) const {
88   auto *obj = dyn_cast_or_null<ObjFile>(getFile());
89   if (!obj)
90     return {};
91 
92   DWARFCache *dwarf = obj->getDwarf();
93   if (!dwarf)
94     return std::string();
95 
96   for (const Subsection &subsec : section.subsections) {
97     if (subsec.isec == this) {
98       off += subsec.offset;
99       break;
100     }
101   }
102 
103   auto createMsg = [&](StringRef path, unsigned line) {
104     std::string filename = sys::path::filename(path).str();
105     std::string lineStr = (":" + Twine(line)).str();
106     if (filename == path)
107       return filename + lineStr;
108     return (filename + lineStr + " (" + path + lineStr + ")").str();
109   };
110 
111   // First, look up a function for a given offset.
112   if (Optional<DILineInfo> li = dwarf->getDILineInfo(
113           section.addr + off, object::SectionedAddress::UndefSection))
114     return createMsg(li->FileName, li->Line);
115 
116   // If it failed, look up again as a variable.
117   if (const Defined *sym = getContainingSymbol(off)) {
118     // Symbols are generally prefixed with an underscore, which is not included
119     // in the debug information.
120     StringRef symName = sym->getName();
121     if (!symName.empty() && symName[0] == '_')
122       symName = symName.substr(1);
123 
124     if (Optional<std::pair<std::string, unsigned>> fileLine =
125             dwarf->getVariableLoc(symName))
126       return createMsg(fileLine->first, fileLine->second);
127   }
128 
129   // Try to get the source file's name from the DWARF information.
130   if (obj->compileUnit)
131     return obj->sourceFile();
132 
133   return {};
134 }
135 
136 void ConcatInputSection::foldIdentical(ConcatInputSection *copy) {
137   align = std::max(align, copy->align);
138   copy->live = false;
139   copy->wasCoalesced = true;
140   copy->replacement = this;
141   for (auto &copySym : copy->symbols)
142     copySym->wasIdenticalCodeFolded = true;
143 
144   // Merge the sorted vectors of symbols together.
145   auto it = symbols.begin();
146   for (auto copyIt = copy->symbols.begin(); copyIt != copy->symbols.end();) {
147     if (it == symbols.end()) {
148       symbols.push_back(*copyIt++);
149       it = symbols.end();
150     } else if ((*it)->value > (*copyIt)->value) {
151       std::swap(*it++, *copyIt);
152     } else {
153       ++it;
154     }
155   }
156   copy->symbols.clear();
157 
158   // Remove duplicate compact unwind info for symbols at the same address.
159   if (symbols.empty())
160     return;
161   it = symbols.begin();
162   uint64_t v = (*it)->value;
163   for (++it; it != symbols.end(); ++it) {
164     Defined *d = *it;
165     if (d->value == v)
166       d->unwindEntry = nullptr;
167     else
168       v = d->value;
169   }
170 }
171 
172 void ConcatInputSection::writeTo(uint8_t *buf) {
173   assert(!shouldOmitFromOutput());
174 
175   if (getFileSize() == 0)
176     return;
177 
178   memcpy(buf, data.data(), data.size());
179 
180   for (size_t i = 0; i < relocs.size(); i++) {
181     const Reloc &r = relocs[i];
182     uint8_t *loc = buf + r.offset;
183     uint64_t referentVA = 0;
184     if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
185       const Symbol *fromSym = r.referent.get<Symbol *>();
186       const Reloc &minuend = relocs[++i];
187       uint64_t minuendVA;
188       if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>())
189         minuendVA = toSym->getVA() + minuend.addend;
190       else {
191         auto *referentIsec = minuend.referent.get<InputSection *>();
192         assert(!::shouldOmitFromOutput(referentIsec));
193         minuendVA = referentIsec->getVA(minuend.addend);
194       }
195       referentVA = minuendVA - fromSym->getVA();
196     } else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
197       if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
198           !referentSym->isInGot())
199         target->relaxGotLoad(loc, r.type);
200       referentVA = resolveSymbolVA(referentSym, r.type) + r.addend;
201 
202       if (isThreadLocalVariables(getFlags())) {
203         // References from thread-local variable sections are treated as offsets
204         // relative to the start of the thread-local data memory area, which
205         // is initialized via copying all the TLV data sections (which are all
206         // contiguous).
207         if (isa<Defined>(referentSym))
208           referentVA -= firstTLVDataSection->addr;
209       }
210     } else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
211       assert(!::shouldOmitFromOutput(referentIsec));
212       referentVA = referentIsec->getVA(r.addend);
213     }
214     target->relocateOne(loc, r, referentVA, getVA() + r.offset);
215   }
216 }
217 
218 ConcatInputSection *macho::makeSyntheticInputSection(StringRef segName,
219                                                      StringRef sectName,
220                                                      uint32_t flags,
221                                                      ArrayRef<uint8_t> data,
222                                                      uint32_t align) {
223   Section &section =
224       *make<Section>(/*file=*/nullptr, segName, sectName, flags, /*addr=*/0);
225   auto isec = make<ConcatInputSection>(section, data, align);
226   section.subsections.push_back({0, isec});
227   return isec;
228 }
229 
230 void CStringInputSection::splitIntoPieces() {
231   size_t off = 0;
232   StringRef s = toStringRef(data);
233   while (!s.empty()) {
234     size_t end = s.find(0);
235     if (end == StringRef::npos)
236       fatal(getLocation(off) + ": string is not null terminated");
237     size_t size = end + 1;
238     uint32_t hash = config->dedupLiterals ? xxHash64(s.substr(0, size)) : 0;
239     pieces.emplace_back(off, hash);
240     s = s.substr(size);
241     off += size;
242   }
243 }
244 
245 StringPiece &CStringInputSection::getStringPiece(uint64_t off) {
246   if (off >= data.size())
247     fatal(toString(this) + ": offset is outside the section");
248 
249   auto it =
250       partition_point(pieces, [=](StringPiece p) { return p.inSecOff <= off; });
251   return it[-1];
252 }
253 
254 const StringPiece &CStringInputSection::getStringPiece(uint64_t off) const {
255   return const_cast<CStringInputSection *>(this)->getStringPiece(off);
256 }
257 
258 uint64_t CStringInputSection::getOffset(uint64_t off) const {
259   const StringPiece &piece = getStringPiece(off);
260   uint64_t addend = off - piece.inSecOff;
261   return piece.outSecOff + addend;
262 }
263 
264 WordLiteralInputSection::WordLiteralInputSection(const Section &section,
265                                                  ArrayRef<uint8_t> data,
266                                                  uint32_t align)
267     : InputSection(WordLiteralKind, section, data, align) {
268   switch (sectionType(getFlags())) {
269   case S_4BYTE_LITERALS:
270     power2LiteralSize = 2;
271     break;
272   case S_8BYTE_LITERALS:
273     power2LiteralSize = 3;
274     break;
275   case S_16BYTE_LITERALS:
276     power2LiteralSize = 4;
277     break;
278   default:
279     llvm_unreachable("invalid literal section type");
280   }
281 
282   live.resize(data.size() >> power2LiteralSize, !config->deadStrip);
283 }
284 
285 uint64_t WordLiteralInputSection::getOffset(uint64_t off) const {
286   auto *osec = cast<WordLiteralSection>(parent);
287   const uintptr_t buf = reinterpret_cast<uintptr_t>(data.data());
288   switch (sectionType(getFlags())) {
289   case S_4BYTE_LITERALS:
290     return osec->getLiteral4Offset(buf + (off & ~3LLU)) | (off & 3);
291   case S_8BYTE_LITERALS:
292     return osec->getLiteral8Offset(buf + (off & ~7LLU)) | (off & 7);
293   case S_16BYTE_LITERALS:
294     return osec->getLiteral16Offset(buf + (off & ~15LLU)) | (off & 15);
295   default:
296     llvm_unreachable("invalid literal section type");
297   }
298 }
299 
300 bool macho::isCodeSection(const InputSection *isec) {
301   uint32_t type = sectionType(isec->getFlags());
302   if (type != S_REGULAR && type != S_COALESCED)
303     return false;
304 
305   uint32_t attr = isec->getFlags() & SECTION_ATTRIBUTES_USR;
306   if (attr == S_ATTR_PURE_INSTRUCTIONS)
307     return true;
308 
309   if (isec->getSegName() == segment_names::text)
310     return StringSwitch<bool>(isec->getName())
311         .Cases(section_names::textCoalNt, section_names::staticInit, true)
312         .Default(false);
313 
314   return false;
315 }
316 
317 bool macho::isCfStringSection(const InputSection *isec) {
318   return isec->getName() == section_names::cfString &&
319          isec->getSegName() == segment_names::data;
320 }
321 
322 bool macho::isClassRefsSection(const InputSection *isec) {
323   return isec->getName() == section_names::objcClassRefs &&
324          isec->getSegName() == segment_names::data;
325 }
326 
327 bool macho::isEhFrameSection(const InputSection *isec) {
328   return isec->getName() == section_names::ehFrame &&
329          isec->getSegName() == segment_names::text;
330 }
331 
332 std::string lld::toString(const InputSection *isec) {
333   return (toString(isec->getFile()) + ":(" + isec->getName() + ")").str();
334 }
335