comparison lld/MachO/SyntheticSections.h @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 0572611fdcc8
children 5f17cb93ff66
comparison
equal deleted inserted replaced
173:0572611fdcc8 207:2e18cbf3894f
7 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
8 8
9 #ifndef LLD_MACHO_SYNTHETIC_SECTIONS_H 9 #ifndef LLD_MACHO_SYNTHETIC_SECTIONS_H
10 #define LLD_MACHO_SYNTHETIC_SECTIONS_H 10 #define LLD_MACHO_SYNTHETIC_SECTIONS_H
11 11
12 #include "Config.h"
12 #include "ExportTrie.h" 13 #include "ExportTrie.h"
13 #include "InputSection.h" 14 #include "InputSection.h"
14 #include "OutputSection.h" 15 #include "OutputSection.h"
16 #include "OutputSegment.h"
15 #include "Target.h" 17 #include "Target.h"
16 18
19 #include "llvm/ADT/Hashing.h"
17 #include "llvm/ADT/SetVector.h" 20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/Support/MathExtras.h"
18 #include "llvm/Support/raw_ostream.h" 22 #include "llvm/Support/raw_ostream.h"
23
24 namespace llvm {
25 class DWARFUnit;
26 } // namespace llvm
19 27
20 namespace lld { 28 namespace lld {
21 namespace macho { 29 namespace macho {
22 30
23 namespace section_names { 31 class Defined;
24
25 constexpr const char *pageZero = "__pagezero";
26 constexpr const char *header = "__mach_header";
27 constexpr const char *binding = "__binding";
28 constexpr const char *lazyBinding = "__lazy_binding";
29 constexpr const char *export_ = "__export";
30 constexpr const char *symbolTable = "__symbol_table";
31 constexpr const char *stringTable = "__string_table";
32 constexpr const char *got = "__got";
33
34 } // namespace section_names
35
36 class DylibSymbol; 32 class DylibSymbol;
37 class LoadCommand; 33 class LoadCommand;
34 class ObjFile;
35 class UnwindInfoSection;
38 36
39 class SyntheticSection : public OutputSection { 37 class SyntheticSection : public OutputSection {
40 public: 38 public:
41 SyntheticSection(const char *segname, const char *name); 39 SyntheticSection(const char *segname, const char *name);
42 virtual ~SyntheticSection() = default; 40 virtual ~SyntheticSection() = default;
43 41
44 static bool classof(const OutputSection *sec) { 42 static bool classof(const OutputSection *sec) {
45 return sec->kind() == SyntheticKind; 43 return sec->kind() == SyntheticKind;
46 } 44 }
45
46 const StringRef segname;
47 // This fake InputSection makes it easier for us to write code that applies
48 // generically to both user inputs and synthetics.
49 InputSection *isec;
50 };
51
52 // All sections in __LINKEDIT should inherit from this.
53 class LinkEditSection : public SyntheticSection {
54 public:
55 LinkEditSection(const char *segname, const char *name)
56 : SyntheticSection(segname, name) {
57 align = target->wordSize;
58 }
59
60 virtual void finalizeContents() {}
61
62 // Sections in __LINKEDIT are special: their offsets are recorded in the
63 // load commands like LC_DYLD_INFO_ONLY and LC_SYMTAB, instead of in section
64 // headers.
65 bool isHidden() const override final { return true; }
66
67 virtual uint64_t getRawSize() const = 0;
68
69 // codesign (or more specifically libstuff) checks that each section in
70 // __LINKEDIT ends where the next one starts -- no gaps are permitted. We
71 // therefore align every section's start and end points to WordSize.
72 //
73 // NOTE: This assumes that the extra bytes required for alignment can be
74 // zero-valued bytes.
75 uint64_t getSize() const override final {
76 return llvm::alignTo(getRawSize(), align);
77 }
47 }; 78 };
48 79
49 // The header of the Mach-O file, which must have a file offset of zero. 80 // The header of the Mach-O file, which must have a file offset of zero.
50 class MachHeaderSection : public SyntheticSection { 81 class MachHeaderSection : public SyntheticSection {
51 public: 82 public:
52 MachHeaderSection(); 83 MachHeaderSection();
84 bool isHidden() const override { return true; }
85 uint64_t getSize() const override;
86 void writeTo(uint8_t *buf) const override;
87
53 void addLoadCommand(LoadCommand *); 88 void addLoadCommand(LoadCommand *);
54 bool isHidden() const override { return true; } 89
55 size_t getSize() const override; 90 protected:
56 void writeTo(uint8_t *buf) const override;
57
58 private:
59 std::vector<LoadCommand *> loadCommands; 91 std::vector<LoadCommand *> loadCommands;
60 uint32_t sizeOfCmds = 0; 92 uint32_t sizeOfCmds = 0;
61 }; 93 };
62 94
63 // A hidden section that exists solely for the purpose of creating the 95 // A hidden section that exists solely for the purpose of creating the
64 // __PAGEZERO segment, which is used to catch null pointer dereferences. 96 // __PAGEZERO segment, which is used to catch null pointer dereferences.
65 class PageZeroSection : public SyntheticSection { 97 class PageZeroSection : public SyntheticSection {
66 public: 98 public:
67 PageZeroSection(); 99 PageZeroSection();
68 bool isHidden() const override { return true; } 100 bool isHidden() const override { return true; }
69 size_t getSize() const override { return ImageBase; } 101 uint64_t getSize() const override { return target->pageZeroSize; }
70 uint64_t getFileSize() const override { return 0; } 102 uint64_t getFileSize() const override { return 0; }
71 void writeTo(uint8_t *buf) const override {} 103 void writeTo(uint8_t *buf) const override {}
72 }; 104 };
73 105
74 // This section will be populated by dyld with addresses to non-lazily-loaded 106 // This is the base class for the GOT and TLVPointer sections, which are nearly
75 // dylib symbols. 107 // functionally identical -- they will both be populated by dyld with addresses
76 class GotSection : public SyntheticSection { 108 // to non-lazily-loaded dylib symbols. The main difference is that the
77 public: 109 // TLVPointerSection stores references to thread-local variables.
78 GotSection(); 110 class NonLazyPointerSectionBase : public SyntheticSection {
79 111 public:
80 const llvm::SetVector<const DylibSymbol *> &getEntries() const { 112 NonLazyPointerSectionBase(const char *segname, const char *name);
81 return entries; 113
82 } 114 const llvm::SetVector<const Symbol *> &getEntries() const { return entries; }
83 115
84 bool isNeeded() const override { return !entries.empty(); } 116 bool isNeeded() const override { return !entries.empty(); }
85 117
86 size_t getSize() const override { return entries.size() * WordSize; } 118 uint64_t getSize() const override {
87 119 return entries.size() * target->wordSize;
88 void writeTo(uint8_t *buf) const override { 120 }
89 // Nothing to write, GOT contains all zeros at link time; it's populated at 121
90 // runtime by dyld. 122 void writeTo(uint8_t *buf) const override;
91 } 123
92 124 void addEntry(Symbol *sym);
93 void addEntry(DylibSymbol &sym); 125
94 126 uint64_t getVA(uint32_t gotIndex) const {
95 private: 127 return addr + gotIndex * target->wordSize;
96 llvm::SetVector<const DylibSymbol *> entries; 128 }
129
130 private:
131 llvm::SetVector<const Symbol *> entries;
132 };
133
134 class GotSection : public NonLazyPointerSectionBase {
135 public:
136 GotSection()
137 : NonLazyPointerSectionBase(segment_names::dataConst,
138 section_names::got) {
139 // TODO: section_64::reserved1 should be an index into the indirect symbol
140 // table, which we do not currently emit
141 }
142 };
143
144 class TlvPointerSection : public NonLazyPointerSectionBase {
145 public:
146 TlvPointerSection()
147 : NonLazyPointerSectionBase(segment_names::data,
148 section_names::threadPtrs) {}
149 };
150
151 struct Location {
152 const InputSection *isec;
153 uint64_t offset;
154
155 Location(const InputSection *isec, uint64_t offset)
156 : isec(isec), offset(offset) {}
157 uint64_t getVA() const { return isec->getVA() + offset; }
158 };
159
160 // Stores rebase opcodes, which tell dyld where absolute addresses have been
161 // encoded in the binary. If the binary is not loaded at its preferred address,
162 // dyld has to rebase these addresses by adding an offset to them.
163 class RebaseSection : public LinkEditSection {
164 public:
165 RebaseSection();
166 void finalizeContents() override;
167 uint64_t getRawSize() const override { return contents.size(); }
168 bool isNeeded() const override { return !locations.empty(); }
169 void writeTo(uint8_t *buf) const override;
170
171 void addEntry(const InputSection *isec, uint64_t offset) {
172 if (config->isPic)
173 locations.push_back({isec, offset});
174 }
175
176 private:
177 std::vector<Location> locations;
178 SmallVector<char, 128> contents;
179 };
180
181 struct BindingEntry {
182 const DylibSymbol *dysym;
183 int64_t addend;
184 Location target;
185 BindingEntry(const DylibSymbol *dysym, int64_t addend, Location target)
186 : dysym(dysym), addend(addend), target(std::move(target)) {}
97 }; 187 };
98 188
99 // Stores bind opcodes for telling dyld which symbols to load non-lazily. 189 // Stores bind opcodes for telling dyld which symbols to load non-lazily.
100 class BindingSection : public SyntheticSection { 190 class BindingSection : public LinkEditSection {
101 public: 191 public:
102 BindingSection(); 192 BindingSection();
103 void finalizeContents(); 193 void finalizeContents() override;
104 size_t getSize() const override { return contents.size(); } 194 uint64_t getRawSize() const override { return contents.size(); }
105 // Like other sections in __LINKEDIT, the binding section is special: its 195 bool isNeeded() const override { return !bindings.empty(); }
106 // offsets are recorded in the LC_DYLD_INFO_ONLY load command, instead of in 196 void writeTo(uint8_t *buf) const override;
107 // section headers. 197
108 bool isHidden() const override { return true; } 198 void addEntry(const DylibSymbol *dysym, const InputSection *isec,
109 bool isNeeded() const override; 199 uint64_t offset, int64_t addend = 0) {
110 void writeTo(uint8_t *buf) const override; 200 bindings.emplace_back(dysym, addend, Location(isec, offset));
111 201 }
202
203 private:
204 std::vector<BindingEntry> bindings;
205 SmallVector<char, 128> contents;
206 };
207
208 struct WeakBindingEntry {
209 const Symbol *symbol;
210 int64_t addend;
211 Location target;
212 WeakBindingEntry(const Symbol *symbol, int64_t addend, Location target)
213 : symbol(symbol), addend(addend), target(std::move(target)) {}
214 };
215
216 // Stores bind opcodes for telling dyld which weak symbols need coalescing.
217 // There are two types of entries in this section:
218 //
219 // 1) Non-weak definitions: This is a symbol definition that weak symbols in
220 // other dylibs should coalesce to.
221 //
222 // 2) Weak bindings: These tell dyld that a given symbol reference should
223 // coalesce to a non-weak definition if one is found. Note that unlike the
224 // entries in the BindingSection, the bindings here only refer to these
225 // symbols by name, but do not specify which dylib to load them from.
226 class WeakBindingSection : public LinkEditSection {
227 public:
228 WeakBindingSection();
229 void finalizeContents() override;
230 uint64_t getRawSize() const override { return contents.size(); }
231 bool isNeeded() const override {
232 return !bindings.empty() || !definitions.empty();
233 }
234
235 void writeTo(uint8_t *buf) const override;
236
237 void addEntry(const Symbol *symbol, const InputSection *isec, uint64_t offset,
238 int64_t addend = 0) {
239 bindings.emplace_back(symbol, addend, Location(isec, offset));
240 }
241
242 bool hasEntry() const { return !bindings.empty(); }
243
244 void addNonWeakDefinition(const Defined *defined) {
245 definitions.emplace_back(defined);
246 }
247
248 bool hasNonWeakDefinition() const { return !definitions.empty(); }
249
250 private:
251 std::vector<WeakBindingEntry> bindings;
252 std::vector<const Defined *> definitions;
112 SmallVector<char, 128> contents; 253 SmallVector<char, 128> contents;
113 }; 254 };
114 255
115 // The following sections implement lazy symbol binding -- very similar to the 256 // The following sections implement lazy symbol binding -- very similar to the
116 // PLT mechanism in ELF. 257 // PLT mechanism in ELF.
117 // 258 //
118 // ELF's .plt section is broken up into two sections in Mach-O: StubsSection and 259 // ELF's .plt section is broken up into two sections in Mach-O: StubsSection
119 // StubHelperSection. Calls to functions in dylibs will end up calling into 260 // and StubHelperSection. Calls to functions in dylibs will end up calling into
120 // StubsSection, which contains indirect jumps to addresses stored in the 261 // StubsSection, which contains indirect jumps to addresses stored in the
121 // LazyPointerSection (the counterpart to ELF's .plt.got). 262 // LazyPointerSection (the counterpart to ELF's .plt.got).
122 // 263 //
123 // Initially, the LazyPointerSection contains addresses that point into one of 264 // We will first describe how non-weak symbols are handled.
124 // the entry points in the middle of the StubHelperSection. The code in 265 //
266 // At program start, the LazyPointerSection contains addresses that point into
267 // one of the entry points in the middle of the StubHelperSection. The code in
125 // StubHelperSection will push on the stack an offset into the 268 // StubHelperSection will push on the stack an offset into the
126 // LazyBindingSection. The push is followed by a jump to the beginning of the 269 // LazyBindingSection. The push is followed by a jump to the beginning of the
127 // StubHelperSection (similar to PLT0), which then calls into dyld_stub_binder. 270 // StubHelperSection (similar to PLT0), which then calls into dyld_stub_binder.
128 // dyld_stub_binder is a non-lazily-bound symbol, so this call looks it up in 271 // dyld_stub_binder is a non-lazily-bound symbol, so this call looks it up in
129 // the GOT. 272 // the GOT.
130 // 273 //
131 // The stub binder will look up the bind opcodes in the LazyBindingSection at 274 // The stub binder will look up the bind opcodes in the LazyBindingSection at
132 // the given offset. The bind opcodes will tell the binder to update the address 275 // the given offset. The bind opcodes will tell the binder to update the
133 // in the LazyPointerSection to point to the symbol, so that subsequent calls 276 // address in the LazyPointerSection to point to the symbol, so that subsequent
134 // don't have to redo the symbol resolution. The binder will then jump to the 277 // calls don't have to redo the symbol resolution. The binder will then jump to
135 // resolved symbol. 278 // the resolved symbol.
279 //
280 // With weak symbols, the situation is slightly different. Since there is no
281 // "weak lazy" lookup, function calls to weak symbols are always non-lazily
282 // bound. We emit both regular non-lazy bindings as well as weak bindings, in
283 // order that the weak bindings may overwrite the non-lazy bindings if an
284 // appropriate symbol is found at runtime. However, the bound addresses will
285 // still be written (non-lazily) into the LazyPointerSection.
136 286
137 class StubsSection : public SyntheticSection { 287 class StubsSection : public SyntheticSection {
138 public: 288 public:
139 StubsSection(); 289 StubsSection();
140 size_t getSize() const override; 290 uint64_t getSize() const override;
141 bool isNeeded() const override { return !entries.empty(); } 291 bool isNeeded() const override { return !entries.empty(); }
142 void writeTo(uint8_t *buf) const override; 292 void finalize() override;
143 293 void writeTo(uint8_t *buf) const override;
144 const llvm::SetVector<DylibSymbol *> &getEntries() const { return entries; } 294 const llvm::SetVector<Symbol *> &getEntries() const { return entries; }
145 295 // Returns whether the symbol was added. Note that every stubs entry will
146 void addEntry(DylibSymbol &sym); 296 // have a corresponding entry in the LazyPointerSection.
147 297 bool addEntry(Symbol *);
148 private: 298 uint64_t getVA(uint32_t stubsIndex) const {
149 llvm::SetVector<DylibSymbol *> entries; 299 // ConcatOutputSection::finalize() can seek the address of a
300 // stub before its address is assigned. Before __stubs is
301 // finalized, return a contrived out-of-range address.
302 return isFinal ? addr + stubsIndex * target->stubSize
303 : TargetInfo::outOfRangeVA;
304 }
305
306 bool isFinal = false; // is address assigned?
307
308 private:
309 llvm::SetVector<Symbol *> entries;
150 }; 310 };
151 311
152 class StubHelperSection : public SyntheticSection { 312 class StubHelperSection : public SyntheticSection {
153 public: 313 public:
154 StubHelperSection(); 314 StubHelperSection();
155 size_t getSize() const override; 315 uint64_t getSize() const override;
156 bool isNeeded() const override; 316 bool isNeeded() const override;
157 void writeTo(uint8_t *buf) const override; 317 void writeTo(uint8_t *buf) const override;
158 318
159 void setup(); 319 void setup();
160 320
161 DylibSymbol *stubBinder = nullptr; 321 DylibSymbol *stubBinder = nullptr;
322 Defined *dyldPrivate = nullptr;
162 }; 323 };
163 324
164 // This section contains space for just a single word, and will be used by dyld 325 // This section contains space for just a single word, and will be used by dyld
165 // to cache an address to the image loader it uses. Note that unlike the other 326 // to cache an address to the image loader it uses. Note that unlike the other
166 // synthetic sections, which are OutputSections, the ImageLoaderCacheSection is 327 // synthetic sections, which are OutputSections, the ImageLoaderCacheSection is
167 // an InputSection that gets merged into the __data OutputSection. 328 // an InputSection that gets merged into the __data OutputSection.
168 class ImageLoaderCacheSection : public InputSection { 329 class ImageLoaderCacheSection : public InputSection {
169 public: 330 public:
170 ImageLoaderCacheSection(); 331 ImageLoaderCacheSection();
171 size_t getSize() const override { return WordSize; } 332 uint64_t getSize() const override { return target->wordSize; }
172 }; 333 };
173 334
335 // Note that this section may also be targeted by non-lazy bindings. In
336 // particular, this happens when branch relocations target weak symbols.
174 class LazyPointerSection : public SyntheticSection { 337 class LazyPointerSection : public SyntheticSection {
175 public: 338 public:
176 LazyPointerSection(); 339 LazyPointerSection();
177 size_t getSize() const override; 340 uint64_t getSize() const override;
178 bool isNeeded() const override; 341 bool isNeeded() const override;
179 void writeTo(uint8_t *buf) const override; 342 void writeTo(uint8_t *buf) const override;
180 }; 343 };
181 344
182 class LazyBindingSection : public SyntheticSection { 345 class LazyBindingSection : public LinkEditSection {
183 public: 346 public:
184 LazyBindingSection(); 347 LazyBindingSection();
185 void finalizeContents(); 348 void finalizeContents() override;
186 size_t getSize() const override { return contents.size(); } 349 uint64_t getRawSize() const override { return contents.size(); }
350 bool isNeeded() const override { return !entries.empty(); }
351 void writeTo(uint8_t *buf) const override;
352 // Note that every entry here will by referenced by a corresponding entry in
353 // the StubHelperSection.
354 void addEntry(DylibSymbol *dysym);
355 const llvm::SetVector<DylibSymbol *> &getEntries() const { return entries; }
356
357 private:
187 uint32_t encode(const DylibSymbol &); 358 uint32_t encode(const DylibSymbol &);
188 // Like other sections in __LINKEDIT, the lazy binding section is special: its 359
189 // offsets are recorded in the LC_DYLD_INFO_ONLY load command, instead of in 360 llvm::SetVector<DylibSymbol *> entries;
190 // section headers.
191 bool isHidden() const override { return true; }
192 bool isNeeded() const override;
193 void writeTo(uint8_t *buf) const override;
194
195 private:
196 SmallVector<char, 128> contents; 361 SmallVector<char, 128> contents;
197 llvm::raw_svector_ostream os{contents}; 362 llvm::raw_svector_ostream os{contents};
198 }; 363 };
199 364
200 // Stores a trie that describes the set of exported symbols. 365 // Stores a trie that describes the set of exported symbols.
201 class ExportSection : public SyntheticSection { 366 class ExportSection : public LinkEditSection {
202 public: 367 public:
203 ExportSection(); 368 ExportSection();
204 void finalizeContents(); 369 void finalizeContents() override;
205 size_t getSize() const override { return size; } 370 uint64_t getRawSize() const override { return size; }
206 // Like other sections in __LINKEDIT, the export section is special: its 371 void writeTo(uint8_t *buf) const override;
207 // offsets are recorded in the LC_DYLD_INFO_ONLY load command, instead of in 372
208 // section headers. 373 bool hasWeakSymbol = false;
209 bool isHidden() const override { return true; }
210 void writeTo(uint8_t *buf) const override;
211 374
212 private: 375 private:
213 TrieBuilder trieBuilder; 376 TrieBuilder trieBuilder;
214 size_t size = 0; 377 size_t size = 0;
215 }; 378 };
216 379
380 class FunctionStartsSection : public LinkEditSection {
381 public:
382 FunctionStartsSection();
383 void finalizeContents() override;
384 uint64_t getRawSize() const override { return contents.size(); }
385 void writeTo(uint8_t *buf) const override;
386
387 private:
388 SmallVector<char, 128> contents;
389 };
390
217 // Stores the strings referenced by the symbol table. 391 // Stores the strings referenced by the symbol table.
218 class StringTableSection : public SyntheticSection { 392 class StringTableSection : public LinkEditSection {
219 public: 393 public:
220 StringTableSection(); 394 StringTableSection();
221 // Returns the start offset of the added string. 395 // Returns the start offset of the added string.
222 uint32_t addString(StringRef); 396 uint32_t addString(StringRef);
223 size_t getSize() const override { return size; } 397 uint64_t getRawSize() const override { return size; }
224 // Like other sections in __LINKEDIT, the string table section is special: its 398 void writeTo(uint8_t *buf) const override;
225 // offsets are recorded in the LC_SYMTAB load command, instead of in section 399
226 // headers. 400 static constexpr size_t emptyStringIndex = 1;
227 bool isHidden() const override { return true; } 401
228 void writeTo(uint8_t *buf) const override; 402 private:
229 403 // ld64 emits string tables which start with a space and a zero byte. We
230 private: 404 // match its behavior here since some tools depend on it.
231 // An n_strx value of 0 always indicates the empty string, so we must locate 405 // Consequently, the empty string will be at index 1, not zero.
232 // our non-empty string values at positive offsets in the string table. 406 std::vector<StringRef> strings{" "};
233 // Therefore we insert a dummy value at position zero. 407 size_t size = 2;
234 std::vector<StringRef> strings{"\0"};
235 size_t size = 1;
236 }; 408 };
237 409
238 struct SymtabEntry { 410 struct SymtabEntry {
239 Symbol *sym; 411 Symbol *sym;
240 size_t strx; 412 size_t strx;
241 }; 413 };
242 414
243 class SymtabSection : public SyntheticSection { 415 struct StabsEntry {
244 public: 416 uint8_t type = 0;
417 uint32_t strx = StringTableSection::emptyStringIndex;
418 uint8_t sect = 0;
419 uint16_t desc = 0;
420 uint64_t value = 0;
421
422 StabsEntry() = default;
423 explicit StabsEntry(uint8_t type) : type(type) {}
424 };
425
426 // Symbols of the same type must be laid out contiguously: we choose to emit
427 // all local symbols first, then external symbols, and finally undefined
428 // symbols. For each symbol type, the LC_DYSYMTAB load command will record the
429 // range (start index and total number) of those symbols in the symbol table.
430 class SymtabSection : public LinkEditSection {
431 public:
432 void finalizeContents() override;
433 uint32_t getNumSymbols() const;
434 uint32_t getNumLocalSymbols() const {
435 return stabs.size() + localSymbols.size();
436 }
437 uint32_t getNumExternalSymbols() const { return externalSymbols.size(); }
438 uint32_t getNumUndefinedSymbols() const { return undefinedSymbols.size(); }
439
440 private:
441 void emitBeginSourceStab(llvm::DWARFUnit *compileUnit);
442 void emitEndSourceStab();
443 void emitObjectFileStab(ObjFile *);
444 void emitEndFunStab(Defined *);
445 void emitStabs();
446
447 protected:
245 SymtabSection(StringTableSection &); 448 SymtabSection(StringTableSection &);
246 void finalizeContents(); 449
247 size_t getNumSymbols() const { return symbols.size(); }
248 size_t getSize() const override;
249 // Like other sections in __LINKEDIT, the symtab section is special: its
250 // offsets are recorded in the LC_SYMTAB load command, instead of in section
251 // headers.
252 bool isHidden() const override { return true; }
253 void writeTo(uint8_t *buf) const override;
254
255 private:
256 StringTableSection &stringTableSection; 450 StringTableSection &stringTableSection;
257 std::vector<SymtabEntry> symbols; 451 // STABS symbols are always local symbols, but we represent them with special
452 // entries because they may use fields like n_sect and n_desc differently.
453 std::vector<StabsEntry> stabs;
454 std::vector<SymtabEntry> localSymbols;
455 std::vector<SymtabEntry> externalSymbols;
456 std::vector<SymtabEntry> undefinedSymbols;
457 };
458
459 template <class LP> SymtabSection *makeSymtabSection(StringTableSection &);
460
461 // The indirect symbol table is a list of 32-bit integers that serve as indices
462 // into the (actual) symbol table. The indirect symbol table is a
463 // concatenation of several sub-arrays of indices, each sub-array belonging to
464 // a separate section. The starting offset of each sub-array is stored in the
465 // reserved1 header field of the respective section.
466 //
467 // These sub-arrays provide symbol information for sections that store
468 // contiguous sequences of symbol references. These references can be pointers
469 // (e.g. those in the GOT and TLVP sections) or assembly sequences (e.g.
470 // function stubs).
471 class IndirectSymtabSection : public LinkEditSection {
472 public:
473 IndirectSymtabSection();
474 void finalizeContents() override;
475 uint32_t getNumSymbols() const;
476 uint64_t getRawSize() const override {
477 return getNumSymbols() * sizeof(uint32_t);
478 }
479 bool isNeeded() const override;
480 void writeTo(uint8_t *buf) const override;
481 };
482
483 // The code signature comes at the very end of the linked output file.
484 class CodeSignatureSection : public LinkEditSection {
485 public:
486 static constexpr uint8_t blockSizeShift = 12;
487 static constexpr size_t blockSize = (1 << blockSizeShift); // 4 KiB
488 static constexpr size_t hashSize = 256 / 8;
489 static constexpr size_t blobHeadersSize = llvm::alignTo<8>(
490 sizeof(llvm::MachO::CS_SuperBlob) + sizeof(llvm::MachO::CS_BlobIndex));
491 static constexpr uint32_t fixedHeadersSize =
492 blobHeadersSize + sizeof(llvm::MachO::CS_CodeDirectory);
493
494 uint32_t fileNamePad = 0;
495 uint32_t allHeadersSize = 0;
496 StringRef fileName;
497
498 CodeSignatureSection();
499 uint64_t getRawSize() const override;
500 bool isNeeded() const override { return true; }
501 void writeTo(uint8_t *buf) const override;
502 uint32_t getBlockCount() const;
503 void writeHashes(uint8_t *buf) const;
504 };
505
506 class BitcodeBundleSection : public SyntheticSection {
507 public:
508 BitcodeBundleSection();
509 uint64_t getSize() const override { return xarSize; }
510 void finalize() override;
511 void writeTo(uint8_t *buf) const override;
512
513 private:
514 llvm::SmallString<261> xarPath;
515 uint64_t xarSize;
258 }; 516 };
259 517
260 struct InStruct { 518 struct InStruct {
519 MachHeaderSection *header = nullptr;
520 RebaseSection *rebase = nullptr;
521 BindingSection *binding = nullptr;
522 WeakBindingSection *weakBinding = nullptr;
523 LazyBindingSection *lazyBinding = nullptr;
524 ExportSection *exports = nullptr;
261 GotSection *got = nullptr; 525 GotSection *got = nullptr;
526 TlvPointerSection *tlvPointers = nullptr;
262 LazyPointerSection *lazyPointers = nullptr; 527 LazyPointerSection *lazyPointers = nullptr;
263 StubsSection *stubs = nullptr; 528 StubsSection *stubs = nullptr;
264 StubHelperSection *stubHelper = nullptr; 529 StubHelperSection *stubHelper = nullptr;
265 ImageLoaderCacheSection *imageLoaderCache = nullptr; 530 ImageLoaderCacheSection *imageLoaderCache = nullptr;
531 UnwindInfoSection *unwindInfo = nullptr;
266 }; 532 };
267 533
268 extern InStruct in; 534 extern InStruct in;
535 extern std::vector<SyntheticSection *> syntheticSections;
536
537 void createSyntheticSymbols();
269 538
270 } // namespace macho 539 } // namespace macho
271 } // namespace lld 540 } // namespace lld
272 541
273 #endif 542 #endif