forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathInputSection.h
386 lines (333 loc) · 14.9 KB
/
InputSection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//===- InputSection.h -------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLD_MACHO_INPUT_SECTION_H
#defineLLD_MACHO_INPUT_SECTION_H
#include"Config.h"
#include"Relocations.h"
#include"Symbols.h"
#include"lld/Common/LLVM.h"
#include"lld/Common/Memory.h"
#include"llvm/ADT/ArrayRef.h"
#include"llvm/ADT/BitVector.h"
#include"llvm/ADT/CachedHashString.h"
#include"llvm/ADT/TinyPtrVector.h"
#include"llvm/BinaryFormat/MachO.h"
namespacelld {
namespacemacho {
classInputFile;
classOutputSection;
classInputSection {
public:
enum Kind : uint8_t {
ConcatKind,
CStringLiteralKind,
WordLiteralKind,
};
Kind kind() const { return sectionKind; }
virtual~InputSection() = default;
virtualuint64_tgetSize() const { return data.size(); }
virtualboolempty() const { return data.empty(); }
InputFile *getFile() const { return section.file; }
StringRef getName() const { return section.name; }
StringRef getSegName() const { return section.segname; }
uint32_tgetFlags() const { return section.flags; }
uint64_tgetFileSize() const;
// Translates \p off -- an offset relative to this InputSection -- into an
// offset from the beginning of its parent OutputSection.
virtualuint64_tgetOffset(uint64_t off) const = 0;
// The offset from the beginning of the file.
uint64_tgetVA(uint64_t off) const;
// Return a user-friendly string for use in diagnostics.
// Format: /path/to/object.o:(symbol _func+0x123)
std::string getLocation(uint64_t off) const;
// Return the source line corresponding to an address, or the empty string.
// Format: Source.cpp:123 (/path/to/Source.cpp:123)
std::string getSourceLocation(uint64_t off) const;
// Return the relocation at \p off, if it exists. This does a linear search.
const Reloc *getRelocAt(uint32_t off) const;
// Whether the data at \p off in this InputSection is live.
virtualboolisLive(uint64_t off) const = 0;
virtualvoidmarkLive(uint64_t off) = 0;
virtual InputSection *canonical() { returnthis; }
virtualconst InputSection *canonical() const { returnthis; }
protected:
InputSection(Kind kind, const Section §ion, ArrayRef<uint8_t> data,
uint32_t align)
: sectionKind(kind), keepUnique(false), hasAltEntry(false), align(align),
data(data), section(section) {}
InputSection(const InputSection &rhs)
: sectionKind(rhs.sectionKind), keepUnique(false), hasAltEntry(false),
align(rhs.align), data(rhs.data), section(rhs.section) {}
Kind sectionKind;
public:
// is address assigned?
bool isFinal = false;
// keep the address of the symbol(s) in this section unique in the final
// binary ?
bool keepUnique : 1;
// Does this section have symbols at offsets other than zero? (NOTE: only
// applies to ConcatInputSections.)
bool hasAltEntry : 1;
uint32_t align = 1;
OutputSection *parent = nullptr;
ArrayRef<uint8_t> data;
std::vector<Reloc> relocs;
// The symbols that belong to this InputSection, sorted by value. With
// .subsections_via_symbols, there is typically only one element here.
llvm::TinyPtrVector<Defined *> symbols;
const Section §ion;
protected:
const Defined *getContainingSymbol(uint64_t off) const;
};
// ConcatInputSections are combined into (Concat)OutputSections through simple
// concatenation, in contrast with literal sections which may have their
// contents merged before output.
classConcatInputSectionfinal : public InputSection {
public:
ConcatInputSection(const Section §ion, ArrayRef<uint8_t> data,
uint32_t align = 1)
: InputSection(ConcatKind, section, data, align) {}
uint64_tgetOffset(uint64_t off) constoverride { return outSecOff + off; }
uint64_tgetVA() const { returnInputSection::getVA(0); }
// ConcatInputSections are entirely live or dead, so the offset is irrelevant.
boolisLive(uint64_t off) constoverride { return live; }
voidmarkLive(uint64_t off) override { live = true; }
boolisCoalescedWeak() const { return wasCoalesced && symbols.empty(); }
boolshouldOmitFromOutput() const { return !live || isCoalescedWeak(); }
voidwriteTo(uint8_t *buf);
voidfoldIdentical(ConcatInputSection *redundant,
Symbol::ICFFoldKind foldKind = Symbol::ICFFoldKind::Body);
ConcatInputSection *canonical() override {
return replacement ? replacement : this;
}
const InputSection *canonical() constoverride {
return replacement ? replacement : this;
}
staticboolclassof(const InputSection *isec) {
return isec->kind() == ConcatKind;
}
// Points to the surviving section after this one is folded by ICF
ConcatInputSection *replacement = nullptr;
// Equivalence-class ID for ICF
uint32_t icfEqClass[2] = {0, 0};
// With subsections_via_symbols, most symbols have their own InputSection,
// and for weak symbols (e.g. from inline functions), only the
// InputSection from one translation unit will make it to the output,
// while all copies in other translation units are coalesced into the
// first and not copied to the output.
bool wasCoalesced = false;
bool live = !config->deadStrip;
bool hasCallSites = false;
// This variable has two usages. Initially, it represents the input order.
// After assignAddresses is called, it represents the offset from the
// beginning of the output section this section was assigned to.
uint64_t outSecOff = 0;
};
// Initialize a fake InputSection that does not belong to any InputFile.
// The created ConcatInputSection will always have 'live=true'
ConcatInputSection *makeSyntheticInputSection(StringRef segName,
StringRef sectName,
uint32_t flags = 0,
ArrayRef<uint8_t> data = {},
uint32_t align = 1);
// Helper functions to make it easy to sprinkle asserts.
inlineboolshouldOmitFromOutput(InputSection *isec) {
return isa<ConcatInputSection>(isec) &&
cast<ConcatInputSection>(isec)->shouldOmitFromOutput();
}
inlineboolisCoalescedWeak(InputSection *isec) {
return isa<ConcatInputSection>(isec) &&
cast<ConcatInputSection>(isec)->isCoalescedWeak();
}
// We allocate a lot of these and binary search on them, so they should be as
// compact as possible. Hence the use of 31 rather than 64 bits for the hash.
structStringPiece {
// Offset from the start of the containing input section.
uint32_t inSecOff;
uint32_t live : 1;
// Only set if deduplicating literals
uint32_t hash : 31;
// Offset from the start of the containing output section.
uint64_t outSecOff = 0;
StringPiece(uint64_t off, uint32_t hash)
: inSecOff(off), live(!config->deadStrip), hash(hash) {}
};
static_assert(sizeof(StringPiece) == 16, "StringPiece is too big!");
// CStringInputSections are composed of multiple null-terminated string
// literals, which we represent using StringPieces. These literals can be
// deduplicated and tail-merged, so translating offsets between the input and
// outputs sections is more complicated.
//
// NOTE: One significant difference between LLD and ld64 is that we merge all
// cstring literals, even those referenced directly by non-private symbols.
// ld64 is more conservative and does not do that. This was mostly done for
// implementation simplicity; if we find programs that need the more
// conservative behavior we can certainly implement that.
classCStringInputSectionfinal : public InputSection {
public:
CStringInputSection(const Section §ion, ArrayRef<uint8_t> data,
uint32_t align, bool dedupLiterals)
: InputSection(CStringLiteralKind, section, data, align),
deduplicateLiterals(dedupLiterals) {}
uint64_tgetOffset(uint64_t off) constoverride;
boolisLive(uint64_t off) constoverride { returngetStringPiece(off).live; }
voidmarkLive(uint64_t off) override { getStringPiece(off).live = true; }
// Find the StringPiece that contains this offset.
StringPiece &getStringPiece(uint64_t off);
const StringPiece &getStringPiece(uint64_t off) const;
// Split at each null byte.
voidsplitIntoPieces();
LLVM_ATTRIBUTE_ALWAYS_INLINE
StringRef getStringRef(size_t i) const {
size_t begin = pieces[i].inSecOff;
// The endpoint should be *at* the null terminator, not after. This matches
// the behavior of StringRef(const char *Str).
size_t end =
((pieces.size() - 1 == i) ? data.size() : pieces[i + 1].inSecOff) - 1;
returntoStringRef(data.slice(begin, end - begin));
}
StringRef getStringRefAtOffset(uint64_t off) const {
returngetStringRef(getStringPieceIndex(off));
}
// Returns i'th piece as a CachedHashStringRef. This function is very hot when
// string merging is enabled, so we want to inline.
LLVM_ATTRIBUTE_ALWAYS_INLINE
llvm::CachedHashStringRef getCachedHashStringRef(size_t i) const {
assert(deduplicateLiterals);
return {getStringRef(i), pieces[i].hash};
}
staticboolclassof(const InputSection *isec) {
return isec->kind() == CStringLiteralKind;
}
bool deduplicateLiterals = false;
std::vector<StringPiece> pieces;
private:
size_tgetStringPieceIndex(uint64_t off) const;
};
classWordLiteralInputSectionfinal : public InputSection {
public:
WordLiteralInputSection(const Section §ion, ArrayRef<uint8_t> data,
uint32_t align);
uint64_tgetOffset(uint64_t off) constoverride;
boolisLive(uint64_t off) constoverride {
return live[off >> power2LiteralSize];
}
voidmarkLive(uint64_t off) override {
live[off >> power2LiteralSize] = true;
}
staticboolclassof(const InputSection *isec) {
return isec->kind() == WordLiteralKind;
}
private:
unsigned power2LiteralSize;
// The liveness of data[off] is tracked by live[off >> power2LiteralSize].
llvm::BitVector live;
};
inlineuint8_tsectionType(uint32_t flags) {
return flags & llvm::MachO::SECTION_TYPE;
}
inlineboolisZeroFill(uint32_t flags) {
returnllvm::MachO::isVirtualSection(sectionType(flags));
}
inlineboolisThreadLocalVariables(uint32_t flags) {
returnsectionType(flags) == llvm::MachO::S_THREAD_LOCAL_VARIABLES;
}
// These sections contain the data for initializing thread-local variables.
inlineboolisThreadLocalData(uint32_t flags) {
returnsectionType(flags) == llvm::MachO::S_THREAD_LOCAL_REGULAR ||
sectionType(flags) == llvm::MachO::S_THREAD_LOCAL_ZEROFILL;
}
inlineboolisDebugSection(uint32_t flags) {
return (flags & llvm::MachO::SECTION_ATTRIBUTES_USR) ==
llvm::MachO::S_ATTR_DEBUG;
}
inlineboolisWordLiteralSection(uint32_t flags) {
returnsectionType(flags) == llvm::MachO::S_4BYTE_LITERALS ||
sectionType(flags) == llvm::MachO::S_8BYTE_LITERALS ||
sectionType(flags) == llvm::MachO::S_16BYTE_LITERALS;
}
boolisCodeSection(const InputSection *);
boolisCfStringSection(const InputSection *);
boolisClassRefsSection(const InputSection *);
boolisSelRefsSection(const InputSection *);
boolisEhFrameSection(const InputSection *);
boolisGccExceptTabSection(const InputSection *);
extern std::vector<ConcatInputSection *> inputSections;
// This is used as a counter for specyfing input order for input sections
externint inputSectionsOrder;
namespacesection_names {
constexprconstchar authGot[] = "__auth_got";
constexprconstchar authPtr[] = "__auth_ptr";
constexprconstchar binding[] = "__binding";
constexprconstchar bitcodeBundle[] = "__bundle";
constexprconstchar cString[] = "__cstring";
constexprconstchar cfString[] = "__cfstring";
constexprconstchar cgProfile[] = "__cg_profile";
constexprconstchar chainFixups[] = "__chainfixups";
constexprconstchar codeSignature[] = "__code_signature";
constexprconstchar common[] = "__common";
constexprconstchar compactUnwind[] = "__compact_unwind";
constexprconstchar data[] = "__data";
constexprconstchar debugAbbrev[] = "__debug_abbrev";
constexprconstchar debugInfo[] = "__debug_info";
constexprconstchar debugLine[] = "__debug_line";
constexprconstchar debugStr[] = "__debug_str";
constexprconstchar debugStrOffs[] = "__debug_str_offs";
constexprconstchar ehFrame[] = "__eh_frame";
constexprconstchar gccExceptTab[] = "__gcc_except_tab";
constexprconstchar export_[] = "__export";
constexprconstchar dataInCode[] = "__data_in_code";
constexprconstchar functionStarts[] = "__func_starts";
constexprconstchar got[] = "__got";
constexprconstchar header[] = "__mach_header";
constexprconstchar indirectSymbolTable[] = "__ind_sym_tab";
constexprconstchar initOffsets[] = "__init_offsets";
constexprconstchar const_[] = "__const";
constexprconstchar lazySymbolPtr[] = "__la_symbol_ptr";
constexprconstchar lazyBinding[] = "__lazy_binding";
constexprconstchar literals[] = "__literals";
constexprconstchar functionMap[] = "__llvm_merge";
constexprconstchar moduleInitFunc[] = "__mod_init_func";
constexprconstchar moduleTermFunc[] = "__mod_term_func";
constexprconstchar nonLazySymbolPtr[] = "__nl_symbol_ptr";
constexprconstchar objcCatList[] = "__objc_catlist";
constexprconstchar objcClassList[] = "__objc_classlist";
constexprconstchar objcMethList[] = "__objc_methlist";
constexprconstchar objcClassRefs[] = "__objc_classrefs";
constexprconstchar objcConst[] = "__objc_const";
constexprconstchar objCImageInfo[] = "__objc_imageinfo";
constexprconstchar objcStubs[] = "__objc_stubs";
constexprconstchar objcSelrefs[] = "__objc_selrefs";
constexprconstchar objcMethname[] = "__objc_methname";
constexprconstchar objcNonLazyCatList[] = "__objc_nlcatlist";
constexprconstchar objcNonLazyClassList[] = "__objc_nlclslist";
constexprconstchar objcProtoList[] = "__objc_protolist";
constexprconstchar outlinedHashTree[] = "__llvm_outline";
constexprconstchar pageZero[] = "__pagezero";
constexprconstchar pointers[] = "__pointers";
constexprconstchar rebase[] = "__rebase";
constexprconstchar staticInit[] = "__StaticInit";
constexprconstchar stringTable[] = "__string_table";
constexprconstchar stubHelper[] = "__stub_helper";
constexprconstchar stubs[] = "__stubs";
constexprconstchar swift[] = "__swift";
constexprconstchar symbolTable[] = "__symbol_table";
constexprconstchar textCoalNt[] = "__textcoal_nt";
constexprconstchar text[] = "__text";
constexprconstchar threadPtrs[] = "__thread_ptrs";
constexprconstchar threadVars[] = "__thread_vars";
constexprconstchar unwindInfo[] = "__unwind_info";
constexprconstchar weakBinding[] = "__weak_binding";
constexprconstchar zeroFill[] = "__zerofill";
constexprconstchar addrSig[] = "__llvm_addrsig";
} // namespace section_names
voidaddInputSection(InputSection *inputSection);
} // namespace macho
std::string toString(const macho::InputSection *);
} // namespace lld
#endif