llvm-project
412 строк · 14.1 Кб
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
20#include "lld/Common/ErrorHandler.h"
21#include "lld/Common/Memory.h"
22#include "llvm/Support/Endian.h"
23#include "llvm/Support/xxhash.h"
24
25using namespace llvm;
26using namespace llvm::MachO;
27using namespace llvm::support;
28using namespace lld;
29using namespace lld::macho;
30
31// Verify ConcatInputSection's size on 64-bit builds. The size of std::vector
32// can differ based on STL debug levels (e.g. iterator debugging on MSVC's STL),
33// so account for that.
34static_assert(sizeof(void *) != 8 ||
35sizeof(ConcatInputSection) == sizeof(std::vector<Reloc>) + 88,
36"Try to minimize ConcatInputSection's size, we create many "
37"instances of it");
38
39std::vector<ConcatInputSection *> macho::inputSections;
40int macho::inputSectionsOrder = 0;
41
42// Call this function to add a new InputSection and have it routed to the
43// appropriate container. Depending on its type and current config, it will
44// either be added to 'inputSections' vector or to a synthetic section.
45void lld::macho::addInputSection(InputSection *inputSection) {
46if (auto *isec = dyn_cast<ConcatInputSection>(inputSection)) {
47if (isec->isCoalescedWeak())
48return;
49if (config->emitRelativeMethodLists &&
50ObjCMethListSection::isMethodList(isec)) {
51if (in.objcMethList->inputOrder == UnspecifiedInputOrder)
52in.objcMethList->inputOrder = inputSectionsOrder++;
53in.objcMethList->addInput(isec);
54isec->parent = in.objcMethList;
55return;
56}
57if (config->emitInitOffsets &&
58sectionType(isec->getFlags()) == S_MOD_INIT_FUNC_POINTERS) {
59in.initOffsets->addInput(isec);
60return;
61}
62isec->outSecOff = inputSectionsOrder++;
63auto *osec = ConcatOutputSection::getOrCreateForInput(isec);
64isec->parent = osec;
65inputSections.push_back(isec);
66} else if (auto *isec = dyn_cast<CStringInputSection>(inputSection)) {
67if (isec->getName() == section_names::objcMethname) {
68if (in.objcMethnameSection->inputOrder == UnspecifiedInputOrder)
69in.objcMethnameSection->inputOrder = inputSectionsOrder++;
70in.objcMethnameSection->addInput(isec);
71} else {
72if (in.cStringSection->inputOrder == UnspecifiedInputOrder)
73in.cStringSection->inputOrder = inputSectionsOrder++;
74in.cStringSection->addInput(isec);
75}
76} else if (auto *isec = dyn_cast<WordLiteralInputSection>(inputSection)) {
77if (in.wordLiteralSection->inputOrder == UnspecifiedInputOrder)
78in.wordLiteralSection->inputOrder = inputSectionsOrder++;
79in.wordLiteralSection->addInput(isec);
80} else {
81llvm_unreachable("unexpected input section kind");
82}
83
84assert(inputSectionsOrder <= UnspecifiedInputOrder);
85}
86
87uint64_t InputSection::getFileSize() const {
88return isZeroFill(getFlags()) ? 0 : getSize();
89}
90
91uint64_t InputSection::getVA(uint64_t off) const {
92return parent->addr + getOffset(off);
93}
94
95static uint64_t resolveSymbolVA(const Symbol *sym, uint8_t type) {
96const RelocAttrs &relocAttrs = target->getRelocAttrs(type);
97if (relocAttrs.hasAttr(RelocAttrBits::BRANCH))
98return sym->resolveBranchVA();
99if (relocAttrs.hasAttr(RelocAttrBits::GOT))
100return sym->resolveGotVA();
101if (relocAttrs.hasAttr(RelocAttrBits::TLV))
102return sym->resolveTlvVA();
103return sym->getVA();
104}
105
106const Defined *InputSection::getContainingSymbol(uint64_t off) const {
107auto *nextSym = llvm::upper_bound(
108symbols, off, [](uint64_t a, const Defined *b) { return a < b->value; });
109if (nextSym == symbols.begin())
110return nullptr;
111return *std::prev(nextSym);
112}
113
114std::string InputSection::getLocation(uint64_t off) const {
115// First, try to find a symbol that's near the offset. Use it as a reference
116// point.
117if (auto *sym = getContainingSymbol(off))
118return (toString(getFile()) + ":(symbol " + toString(*sym) + "+0x" +
119Twine::utohexstr(off - sym->value) + ")")
120.str();
121
122// If that fails, use the section itself as a reference point.
123for (const Subsection &subsec : section.subsections) {
124if (subsec.isec == this) {
125off += subsec.offset;
126break;
127}
128}
129
130return (toString(getFile()) + ":(" + getName() + "+0x" +
131Twine::utohexstr(off) + ")")
132.str();
133}
134
135std::string InputSection::getSourceLocation(uint64_t off) const {
136auto *obj = dyn_cast_or_null<ObjFile>(getFile());
137if (!obj)
138return {};
139
140DWARFCache *dwarf = obj->getDwarf();
141if (!dwarf)
142return std::string();
143
144for (const Subsection &subsec : section.subsections) {
145if (subsec.isec == this) {
146off += subsec.offset;
147break;
148}
149}
150
151auto createMsg = [&](StringRef path, unsigned line) {
152std::string filename = sys::path::filename(path).str();
153std::string lineStr = (":" + Twine(line)).str();
154if (filename == path)
155return filename + lineStr;
156return (filename + lineStr + " (" + path + lineStr + ")").str();
157};
158
159// First, look up a function for a given offset.
160if (std::optional<DILineInfo> li = dwarf->getDILineInfo(
161section.addr + off, object::SectionedAddress::UndefSection))
162return createMsg(li->FileName, li->Line);
163
164// If it failed, look up again as a variable.
165if (const Defined *sym = getContainingSymbol(off)) {
166// Symbols are generally prefixed with an underscore, which is not included
167// in the debug information.
168StringRef symName = sym->getName();
169if (!symName.empty() && symName[0] == '_')
170symName = symName.substr(1);
171
172if (std::optional<std::pair<std::string, unsigned>> fileLine =
173dwarf->getVariableLoc(symName))
174return createMsg(fileLine->first, fileLine->second);
175}
176
177// Try to get the source file's name from the DWARF information.
178if (obj->compileUnit)
179return obj->sourceFile();
180
181return {};
182}
183
184const Reloc *InputSection::getRelocAt(uint32_t off) const {
185auto it = llvm::find_if(
186relocs, [=](const macho::Reloc &r) { return r.offset == off; });
187if (it == relocs.end())
188return nullptr;
189return &*it;
190}
191
192void ConcatInputSection::foldIdentical(ConcatInputSection *copy) {
193align = std::max(align, copy->align);
194copy->live = false;
195copy->wasCoalesced = true;
196copy->replacement = this;
197for (auto ©Sym : copy->symbols)
198copySym->wasIdenticalCodeFolded = true;
199
200symbols.insert(symbols.end(), copy->symbols.begin(), copy->symbols.end());
201copy->symbols.clear();
202
203// Remove duplicate compact unwind info for symbols at the same address.
204if (symbols.empty())
205return;
206for (auto it = symbols.begin() + 1; it != symbols.end(); ++it) {
207assert((*it)->value == 0);
208(*it)->originalUnwindEntry = nullptr;
209}
210}
211
212void ConcatInputSection::writeTo(uint8_t *buf) {
213assert(!shouldOmitFromOutput());
214
215if (getFileSize() == 0)
216return;
217
218memcpy(buf, data.data(), data.size());
219
220for (size_t i = 0; i < relocs.size(); i++) {
221const Reloc &r = relocs[i];
222uint8_t *loc = buf + r.offset;
223uint64_t referentVA = 0;
224
225const bool needsFixup = config->emitChainedFixups &&
226target->hasAttr(r.type, RelocAttrBits::UNSIGNED);
227if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
228const Symbol *fromSym = r.referent.get<Symbol *>();
229const Reloc &minuend = relocs[++i];
230uint64_t minuendVA;
231if (const Symbol *toSym = minuend.referent.dyn_cast<Symbol *>())
232minuendVA = toSym->getVA() + minuend.addend;
233else {
234auto *referentIsec = minuend.referent.get<InputSection *>();
235assert(!::shouldOmitFromOutput(referentIsec));
236minuendVA = referentIsec->getVA(minuend.addend);
237}
238referentVA = minuendVA - fromSym->getVA();
239} else if (auto *referentSym = r.referent.dyn_cast<Symbol *>()) {
240if (target->hasAttr(r.type, RelocAttrBits::LOAD) &&
241!referentSym->isInGot())
242target->relaxGotLoad(loc, r.type);
243// For dtrace symbols, do not handle them as normal undefined symbols
244if (referentSym->getName().starts_with("___dtrace_")) {
245// Change dtrace call site to pre-defined instructions
246target->handleDtraceReloc(referentSym, r, loc);
247continue;
248}
249referentVA = resolveSymbolVA(referentSym, r.type) + r.addend;
250
251if (isThreadLocalVariables(getFlags()) && isa<Defined>(referentSym)) {
252// References from thread-local variable sections are treated as offsets
253// relative to the start of the thread-local data memory area, which
254// is initialized via copying all the TLV data sections (which are all
255// contiguous).
256referentVA -= firstTLVDataSection->addr;
257} else if (needsFixup) {
258writeChainedFixup(loc, referentSym, r.addend);
259continue;
260}
261} else if (auto *referentIsec = r.referent.dyn_cast<InputSection *>()) {
262assert(!::shouldOmitFromOutput(referentIsec));
263referentVA = referentIsec->getVA(r.addend);
264
265if (needsFixup) {
266writeChainedRebase(loc, referentVA);
267continue;
268}
269}
270target->relocateOne(loc, r, referentVA, getVA() + r.offset);
271}
272}
273
274ConcatInputSection *macho::makeSyntheticInputSection(StringRef segName,
275StringRef sectName,
276uint32_t flags,
277ArrayRef<uint8_t> data,
278uint32_t align) {
279Section §ion =
280*make<Section>(/*file=*/nullptr, segName, sectName, flags, /*addr=*/0);
281auto isec = make<ConcatInputSection>(section, data, align);
282// Since this is an explicitly created 'fake' input section,
283// it should not be dead stripped.
284isec->live = true;
285section.subsections.push_back({0, isec});
286return isec;
287}
288
289void CStringInputSection::splitIntoPieces() {
290size_t off = 0;
291StringRef s = toStringRef(data);
292while (!s.empty()) {
293size_t end = s.find(0);
294if (end == StringRef::npos)
295fatal(getLocation(off) + ": string is not null terminated");
296uint32_t hash = deduplicateLiterals ? xxh3_64bits(s.take_front(end)) : 0;
297pieces.emplace_back(off, hash);
298size_t size = end + 1; // include null terminator
299s = s.substr(size);
300off += size;
301}
302}
303
304StringPiece &CStringInputSection::getStringPiece(uint64_t off) {
305if (off >= data.size())
306fatal(toString(this) + ": offset is outside the section");
307
308auto it =
309partition_point(pieces, [=](StringPiece p) { return p.inSecOff <= off; });
310return it[-1];
311}
312
313const StringPiece &CStringInputSection::getStringPiece(uint64_t off) const {
314return const_cast<CStringInputSection *>(this)->getStringPiece(off);
315}
316
317size_t CStringInputSection::getStringPieceIndex(uint64_t off) const {
318if (off >= data.size())
319fatal(toString(this) + ": offset is outside the section");
320
321auto it =
322partition_point(pieces, [=](StringPiece p) { return p.inSecOff <= off; });
323return std::distance(pieces.begin(), it) - 1;
324}
325
326uint64_t CStringInputSection::getOffset(uint64_t off) const {
327const StringPiece &piece = getStringPiece(off);
328uint64_t addend = off - piece.inSecOff;
329return piece.outSecOff + addend;
330}
331
332WordLiteralInputSection::WordLiteralInputSection(const Section §ion,
333ArrayRef<uint8_t> data,
334uint32_t align)
335: InputSection(WordLiteralKind, section, data, align) {
336switch (sectionType(getFlags())) {
337case S_4BYTE_LITERALS:
338power2LiteralSize = 2;
339break;
340case S_8BYTE_LITERALS:
341power2LiteralSize = 3;
342break;
343case S_16BYTE_LITERALS:
344power2LiteralSize = 4;
345break;
346default:
347llvm_unreachable("invalid literal section type");
348}
349
350live.resize(data.size() >> power2LiteralSize, !config->deadStrip);
351}
352
353uint64_t WordLiteralInputSection::getOffset(uint64_t off) const {
354auto *osec = cast<WordLiteralSection>(parent);
355const uintptr_t buf = reinterpret_cast<uintptr_t>(data.data());
356switch (sectionType(getFlags())) {
357case S_4BYTE_LITERALS:
358return osec->getLiteral4Offset(buf + (off & ~3LLU)) | (off & 3);
359case S_8BYTE_LITERALS:
360return osec->getLiteral8Offset(buf + (off & ~7LLU)) | (off & 7);
361case S_16BYTE_LITERALS:
362return osec->getLiteral16Offset(buf + (off & ~15LLU)) | (off & 15);
363default:
364llvm_unreachable("invalid literal section type");
365}
366}
367
368bool macho::isCodeSection(const InputSection *isec) {
369uint32_t type = sectionType(isec->getFlags());
370if (type != S_REGULAR && type != S_COALESCED)
371return false;
372
373uint32_t attr = isec->getFlags() & SECTION_ATTRIBUTES_USR;
374if (attr == S_ATTR_PURE_INSTRUCTIONS)
375return true;
376
377if (isec->getSegName() == segment_names::text)
378return StringSwitch<bool>(isec->getName())
379.Cases(section_names::textCoalNt, section_names::staticInit, true)
380.Default(false);
381
382return false;
383}
384
385bool macho::isCfStringSection(const InputSection *isec) {
386return isec->getName() == section_names::cfString &&
387isec->getSegName() == segment_names::data;
388}
389
390bool macho::isClassRefsSection(const InputSection *isec) {
391return isec->getName() == section_names::objcClassRefs &&
392isec->getSegName() == segment_names::data;
393}
394
395bool macho::isSelRefsSection(const InputSection *isec) {
396return isec->getName() == section_names::objcSelrefs &&
397isec->getSegName() == segment_names::data;
398}
399
400bool macho::isEhFrameSection(const InputSection *isec) {
401return isec->getName() == section_names::ehFrame &&
402isec->getSegName() == segment_names::text;
403}
404
405bool macho::isGccExceptTabSection(const InputSection *isec) {
406return isec->getName() == section_names::gccExceptTab &&
407isec->getSegName() == segment_names::text;
408}
409
410std::string lld::toString(const InputSection *isec) {
411return (toString(isec->getFile()) + ":(" + isec->getName() + ")").str();
412}
413