1//===- SyntheticSections.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 "SyntheticSections.h"
10#include "ConcatOutputSection.h"
11#include "Config.h"
12#include "ExportTrie.h"
13#include "ICF.h"
14#include "InputFiles.h"
15#include "ObjC.h"
16#include "OutputSegment.h"
17#include "SectionPriorities.h"
18#include "SymbolTable.h"
19#include "Symbols.h"
20
21#include "lld/Common/CommonLinkerContext.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/Config/llvm-config.h"
24#include "llvm/Support/FileSystem.h"
25#include "llvm/Support/LEB128.h"
26#include "llvm/Support/Parallel.h"
27#include "llvm/Support/xxhash.h"
28
29#include <limits>
30
31#if defined(__APPLE__)
32#include <sys/mman.h>
33
34#define COMMON_DIGEST_FOR_OPENSSL
35#include <CommonCrypto/CommonDigest.h>
36#else
37#include "llvm/Support/SHA256.h"
38#endif
39
40using namespace llvm;
41using namespace llvm::MachO;
42using namespace llvm::support;
43using namespace llvm::support::endian;
44using namespace lld;
45using namespace lld::macho;
46
47// Reads `len` bytes at data and writes the 32-byte SHA256 checksum to `output`.
48static void sha256(const uint8_t *data, size_t len, uint8_t *output) {
49#if defined(__APPLE__)
50 // FIXME: Make LLVM's SHA256 faster and use it unconditionally. See PR56121
51 // for some notes on this.
52 CC_SHA256(data, len, output);
53#else
54 ArrayRef<uint8_t> block(data, len);
55 std::array<uint8_t, 32> hash = SHA256::hash(Data: block);
56 static_assert(hash.size() == CodeSignatureSection::hashSize);
57 memcpy(dest: output, src: hash.data(), n: hash.size());
58#endif
59}
60
61InStruct macho::in;
62std::vector<SyntheticSection *> macho::syntheticSections;
63
64SyntheticSection::SyntheticSection(const char *segname, const char *name)
65 : OutputSection(SyntheticKind, name) {
66 std::tie(args&: this->segname, args&: this->name) = maybeRenameSection(key: {segname, name});
67 isec = makeSyntheticInputSection(segName: segname, sectName: name);
68 isec->parent = this;
69 syntheticSections.push_back(x: this);
70}
71
72// dyld3's MachOLoaded::getSlide() assumes that the __TEXT segment starts
73// from the beginning of the file (i.e. the header).
74MachHeaderSection::MachHeaderSection()
75 : SyntheticSection(segment_names::text, section_names::header) {
76 // XXX: This is a hack. (See D97007)
77 // Setting the index to 1 to pretend that this section is the text
78 // section.
79 index = 1;
80 isec->isFinal = true;
81}
82
83void MachHeaderSection::addLoadCommand(LoadCommand *lc) {
84 loadCommands.push_back(x: lc);
85 sizeOfCmds += lc->getSize();
86}
87
88uint64_t MachHeaderSection::getSize() const {
89 uint64_t size = target->headerSize + sizeOfCmds + config->headerPad;
90 // If we are emitting an encryptable binary, our load commands must have a
91 // separate (non-encrypted) page to themselves.
92 if (config->emitEncryptionInfo)
93 size = alignToPowerOf2(Value: size, Align: target->getPageSize());
94 return size;
95}
96
97static uint32_t cpuSubtype() {
98 uint32_t subtype = target->cpuSubtype;
99
100 if (config->outputType == MH_EXECUTE && !config->staticLink &&
101 target->cpuSubtype == CPU_SUBTYPE_X86_64_ALL &&
102 config->platform() == PLATFORM_MACOS &&
103 config->platformInfo.target.MinDeployment >= VersionTuple(10, 5))
104 subtype |= CPU_SUBTYPE_LIB64;
105
106 if (config->arch() == AK_arm64e) {
107 // ld64 marks macOS and Mac Catalyst executables with ptrauth ABI version 1.
108 bool isMacExecutable = config->outputType == MH_EXECUTE &&
109 (config->platform() == PLATFORM_MACOS ||
110 config->platform() == PLATFORM_MACCATALYST);
111 subtype = CPU_SUBTYPE_ARM64E_WITH_PTRAUTH_VERSION(
112 /*PtrAuthABIVersion=*/isMacExecutable ? 1 : 0,
113 /*PtrAuthKernelABIVersion=*/false);
114 }
115
116 return subtype;
117}
118
119static bool hasWeakBinding() {
120 return config->emitChainedFixups ? in.chainedFixups->hasWeakBinding()
121 : in.weakBinding->hasEntry();
122}
123
124static bool hasNonWeakDefinition() {
125 return config->emitChainedFixups ? in.chainedFixups->hasNonWeakDefinition()
126 : in.weakBinding->hasNonWeakDefinition();
127}
128
129void MachHeaderSection::writeTo(uint8_t *buf) const {
130 auto *hdr = reinterpret_cast<mach_header *>(buf);
131 hdr->magic = target->magic;
132 hdr->cputype = target->cpuType;
133 hdr->cpusubtype = cpuSubtype();
134 hdr->filetype = config->outputType;
135 hdr->ncmds = loadCommands.size();
136 hdr->sizeofcmds = sizeOfCmds;
137 hdr->flags = MH_DYLDLINK;
138
139 if (config->namespaceKind == NamespaceKind::twolevel)
140 hdr->flags |= MH_NOUNDEFS | MH_TWOLEVEL;
141
142 if (config->outputType == MH_DYLIB && !config->hasReexports)
143 hdr->flags |= MH_NO_REEXPORTED_DYLIBS;
144
145 if (config->markDeadStrippableDylib)
146 hdr->flags |= MH_DEAD_STRIPPABLE_DYLIB;
147
148 if (config->outputType == MH_EXECUTE && config->isPic)
149 hdr->flags |= MH_PIE;
150
151 if (config->outputType == MH_DYLIB && config->applicationExtension)
152 hdr->flags |= MH_APP_EXTENSION_SAFE;
153
154 if (in.exports->hasWeakSymbol || hasNonWeakDefinition())
155 hdr->flags |= MH_WEAK_DEFINES;
156
157 if (in.exports->hasWeakSymbol || hasWeakBinding())
158 hdr->flags |= MH_BINDS_TO_WEAK;
159
160 for (const OutputSegment *seg : outputSegments) {
161 for (const OutputSection *osec : seg->getSections()) {
162 if (isThreadLocalVariables(flags: osec->flags)) {
163 hdr->flags |= MH_HAS_TLV_DESCRIPTORS;
164 break;
165 }
166 }
167 }
168
169 uint8_t *p = reinterpret_cast<uint8_t *>(hdr) + target->headerSize;
170 for (const LoadCommand *lc : loadCommands) {
171 lc->writeTo(buf: p);
172 p += lc->getSize();
173 }
174}
175
176PageZeroSection::PageZeroSection()
177 : SyntheticSection(segment_names::pageZero, section_names::pageZero) {}
178
179RebaseSection::RebaseSection()
180 : LinkEditSection(segment_names::linkEdit, section_names::rebase) {}
181
182namespace {
183struct RebaseState {
184 uint64_t sequenceLength;
185 uint64_t skipLength;
186};
187} // namespace
188
189static void emitIncrement(uint64_t incr, raw_svector_ostream &os) {
190 assert(incr != 0);
191
192 if ((incr >> target->p2WordSize) <= REBASE_IMMEDIATE_MASK &&
193 (incr % target->wordSize) == 0) {
194 os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_IMM_SCALED |
195 (incr >> target->p2WordSize));
196 } else {
197 os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_ULEB);
198 encodeULEB128(Value: incr, OS&: os);
199 }
200}
201
202static void flushRebase(const RebaseState &state, raw_svector_ostream &os) {
203 assert(state.sequenceLength > 0);
204
205 if (state.skipLength == target->wordSize) {
206 if (state.sequenceLength <= REBASE_IMMEDIATE_MASK) {
207 os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_IMM_TIMES |
208 state.sequenceLength);
209 } else {
210 os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ULEB_TIMES);
211 encodeULEB128(Value: state.sequenceLength, OS&: os);
212 }
213 } else if (state.sequenceLength == 1) {
214 os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB);
215 encodeULEB128(Value: state.skipLength - target->wordSize, OS&: os);
216 } else {
217 os << static_cast<uint8_t>(
218 REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB);
219 encodeULEB128(Value: state.sequenceLength, OS&: os);
220 encodeULEB128(Value: state.skipLength - target->wordSize, OS&: os);
221 }
222}
223
224// Rebases are communicated to dyld using a bytecode, whose opcodes cause the
225// memory location at a specific address to be rebased and/or the address to be
226// incremented.
227//
228// Opcode REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB is the most generic
229// one, encoding a series of evenly spaced addresses. This algorithm works by
230// splitting up the sorted list of addresses into such chunks. If the locations
231// are consecutive or the sequence consists of a single location, flushRebase
232// will use a smaller, more specialized encoding.
233static void encodeRebases(const OutputSegment *seg,
234 MutableArrayRef<Location> locations,
235 raw_svector_ostream &os) {
236 // dyld operates on segments. Translate section offsets into segment offsets.
237 for (Location &loc : locations)
238 loc.offset =
239 loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(off: loc.offset);
240 // The algorithm assumes that locations are unique.
241 Location *end =
242 llvm::unique(R&: locations, P: [](const Location &a, const Location &b) {
243 return a.offset == b.offset;
244 });
245 size_t count = end - locations.begin();
246
247 os << static_cast<uint8_t>(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
248 seg->index);
249 assert(!locations.empty());
250 uint64_t offset = locations[0].offset;
251 encodeULEB128(Value: offset, OS&: os);
252
253 RebaseState state{.sequenceLength: 1, .skipLength: target->wordSize};
254
255 for (size_t i = 1; i < count; ++i) {
256 offset = locations[i].offset;
257
258 uint64_t skip = offset - locations[i - 1].offset;
259 assert(skip != 0 && "duplicate locations should have been weeded out");
260
261 if (skip == state.skipLength) {
262 ++state.sequenceLength;
263 } else if (state.sequenceLength == 1) {
264 ++state.sequenceLength;
265 state.skipLength = skip;
266 } else if (skip < state.skipLength) {
267 // The address is lower than what the rebase pointer would be if the last
268 // location would be part of a sequence. We start a new sequence from the
269 // previous location.
270 --state.sequenceLength;
271 flushRebase(state, os);
272
273 state.sequenceLength = 2;
274 state.skipLength = skip;
275 } else {
276 // The address is at some positive offset from the rebase pointer. We
277 // start a new sequence which begins with the current location.
278 flushRebase(state, os);
279 emitIncrement(incr: skip - state.skipLength, os);
280 state.sequenceLength = 1;
281 state.skipLength = target->wordSize;
282 }
283 }
284 flushRebase(state, os);
285}
286
287void RebaseSection::finalizeContents() {
288 if (locations.empty())
289 return;
290
291 raw_svector_ostream os{contents};
292 os << static_cast<uint8_t>(REBASE_OPCODE_SET_TYPE_IMM | REBASE_TYPE_POINTER);
293
294 llvm::sort(C&: locations, Comp: [](const Location &a, const Location &b) {
295 return a.isec->getVA(off: a.offset) < b.isec->getVA(off: b.offset);
296 });
297
298 for (size_t i = 0, count = locations.size(); i < count;) {
299 const OutputSegment *seg = locations[i].isec->parent->parent;
300 size_t j = i + 1;
301 while (j < count && locations[j].isec->parent->parent == seg)
302 ++j;
303 encodeRebases(seg, locations: {locations.data() + i, locations.data() + j}, os);
304 i = j;
305 }
306 os << static_cast<uint8_t>(REBASE_OPCODE_DONE);
307}
308
309void RebaseSection::writeTo(uint8_t *buf) const {
310 memcpy(dest: buf, src: contents.data(), n: contents.size());
311}
312
313GotSection::GotSection()
314 : SyntheticSection(segment_names::data, section_names::got) {
315 align = target->wordSize;
316 flags = S_NON_LAZY_SYMBOL_POINTERS;
317}
318
319void macho::addNonLazyBindingEntries(const Symbol *sym,
320 const InputSection *isec, uint64_t offset,
321 int64_t addend, bool forceOutlineAuth) {
322 if (config->emitChainedFixups) {
323 if (needsBinding(sym))
324 in.chainedFixups->addBinding(dysym: sym, isec, offset, addend, forceOutline: forceOutlineAuth);
325 else if (isa<Defined>(Val: sym))
326 in.chainedFixups->addRebase(isec, offset);
327 else
328 llvm_unreachable("cannot bind to an undefined symbol");
329 return;
330 }
331
332 if (const auto *dysym = dyn_cast<DylibSymbol>(Val: sym)) {
333 in.binding->addEntry(dysym, isec, offset, addend);
334 if (dysym->isWeakDef())
335 in.weakBinding->addEntry(symbol: sym, isec, offset, addend);
336 } else if (const auto *defined = dyn_cast<Defined>(Val: sym)) {
337 in.rebase->addEntry(isec, offset);
338 if (defined->isExternalWeakDef())
339 in.weakBinding->addEntry(symbol: sym, isec, offset, addend);
340 else if (defined->interposable)
341 in.binding->addEntry(dysym: sym, isec, offset, addend);
342 } else {
343 // Undefined symbols are filtered out in scanRelocations(); we should never
344 // get here
345 llvm_unreachable("cannot bind to an undefined symbol");
346 }
347}
348
349void GotSection::addEntry(Symbol *sym) {
350 if (entries.insert(X: sym)) {
351 // Every symbol has at most one non-lazy pointer slot.
352 assert(!sym->isInGot());
353 sym->gotIndex = entries.size() - 1;
354
355 addNonLazyBindingEntries(sym, isec, offset: sym->gotIndex * target->wordSize);
356 }
357}
358
359// Reports, with ld64's message, a rebase target the pointer format can't hold.
360static void reportUnencodableRebase(const uint8_t *buf, bool isAddress,
361 uint64_t value) {
362 uint64_t fileOff = buf - in.bufferStart;
363 std::string loc = "<unknown>";
364 for (const OutputSegment *seg : outputSegments)
365 if (fileOff >= seg->fileOff && fileOff - seg->fileOff < seg->fileSize)
366 loc = (seg->name + "+0x" + utohexstr(X: fileOff - seg->fileOff)).str();
367 error(msg: Twine(isAddress ? "vmAddress" : "vmOffset") + " (0x" +
368 utohexstr(X: value) + ") cannot fit in fixup at " + loc);
369}
370
371static void writeArm64eChainedRebase(uint8_t *buf, uint64_t targetVA,
372 const AuthInfo *auth) {
373 uint64_t imageBase = in.header->addr;
374 if (auth) {
375 auto *rebase = reinterpret_cast<dyld_chained_ptr_arm64e_auth_rebase *>(buf);
376 uint64_t runtimeOffset = targetVA - imageBase;
377 if (!isUInt<32>(x: runtimeOffset))
378 reportUnencodableRebase(buf, /*isAddress=*/false, value: runtimeOffset);
379 rebase->target = runtimeOffset;
380 rebase->diversity = auth->diversity;
381 rebase->addrDiv = auth->addrDiv;
382 rebase->key = auth->key;
383 rebase->next = 0;
384 rebase->bind = 0;
385 rebase->auth = 1;
386 return;
387 }
388
389 // USERLAND24 rebases hold an offset from the image base, ARM64E rebases an
390 // address.
391 uint64_t targetValue =
392 config->chainedFixupsPointerFormat == DYLD_CHAINED_PTR_ARM64E_USERLAND24
393 ? targetVA - imageBase
394 : targetVA;
395 auto *rebase = reinterpret_cast<dyld_chained_ptr_arm64e_rebase *>(buf);
396 rebase->target = targetValue & 0x7ff'ffff'ffff;
397 rebase->high8 = targetValue >> 56;
398 rebase->next = 0;
399 rebase->bind = 0;
400 rebase->auth = 0;
401 // Reject targets that lose bits in the encoding and, like ld64, targets below
402 // the image base.
403 uint64_t encodedValue = rebase->target | (uint64_t(rebase->high8) << 56);
404 uint64_t untaggedTarget = targetVA & ((uint64_t(1) << 56) - 1);
405 if (encodedValue != targetValue || untaggedTarget < imageBase)
406 reportUnencodableRebase(buf,
407 isAddress: config->chainedFixupsPointerFormat !=
408 DYLD_CHAINED_PTR_ARM64E_USERLAND24,
409 value: targetValue);
410}
411
412void macho::writeChainedRebase(uint8_t *buf, uint64_t targetVA,
413 const AuthInfo *auth) {
414 assert(config->emitChainedFixups);
415 assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
416 if (config->arch() == AK_arm64e) {
417 writeArm64eChainedRebase(buf, targetVA, auth);
418 return;
419 }
420 // DYLD_CHAINED_PTR_64 fixups only need to be 4-byte aligned, so build the
421 // pointer here and copy it out.
422 dyld_chained_ptr_64_rebase rebase;
423 rebase.target = targetVA & 0xf'ffff'ffff;
424 rebase.high8 = (targetVA >> 56);
425 rebase.reserved = 0;
426 rebase.next = 0;
427 rebase.bind = 0;
428 memcpy(dest: buf, src: &rebase, n: sizeof(rebase));
429
430 // The fixup format places a 64 GiB limit on the output's size.
431 // Should we handle this gracefully?
432 uint64_t encodedVA = rebase.target | ((uint64_t)rebase.high8 << 56);
433 if (encodedVA != targetVA)
434 error(msg: "rebase target address 0x" + Twine::utohexstr(Val: targetVA) +
435 " does not fit into chained fixup. Re-link with -no_fixup_chains");
436}
437
438// Bind is dyld_chained_ptr_arm64e_bind or dyld_chained_ptr_arm64e_bind24.
439template <class Bind>
440static void writeArm64eChainedBind(uint8_t *buf, const Symbol *sym,
441 int64_t addend) {
442 auto *bind = reinterpret_cast<Bind *>(buf);
443 auto [ordinal, inlineAddend] = in.chainedFixups->getBinding(sym, addend);
444 bind->ordinal = ordinal;
445 bind->zero = 0;
446 bind->addend = inlineAddend;
447 bind->next = 0;
448 bind->bind = 1;
449 bind->auth = 0;
450}
451
452// AuthBind is dyld_chained_ptr_arm64e_auth_bind or
453// dyld_chained_ptr_arm64e_auth_bind24, neither of which has room for an
454// addend.
455template <class AuthBind>
456static void writeArm64eChainedAuthBind(uint8_t *buf, const Symbol *sym,
457 int64_t addend, const AuthInfo &auth) {
458 auto *bind = reinterpret_cast<AuthBind *>(buf);
459 bind->ordinal =
460 in.chainedFixups->getBinding(sym, addend, /*forceOutline=*/true).first;
461 bind->zero = 0;
462 bind->diversity = auth.diversity;
463 bind->addrDiv = auth.addrDiv;
464 bind->key = auth.key;
465 bind->next = 0;
466 bind->bind = 1;
467 bind->auth = 1;
468}
469
470static void writeChainedBind(uint8_t *buf, const Symbol *sym, int64_t addend,
471 const AuthInfo *auth) {
472 assert(config->emitChainedFixups);
473 assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
474 if (config->arch() == AK_arm64e) {
475 bool isUserland24 = config->chainedFixupsPointerFormat ==
476 DYLD_CHAINED_PTR_ARM64E_USERLAND24;
477 if (auth && isUserland24)
478 writeArm64eChainedAuthBind<dyld_chained_ptr_arm64e_auth_bind24>(
479 buf, sym, addend, auth: *auth);
480 else if (auth)
481 writeArm64eChainedAuthBind<dyld_chained_ptr_arm64e_auth_bind>(
482 buf, sym, addend, auth: *auth);
483 else if (isUserland24)
484 writeArm64eChainedBind<dyld_chained_ptr_arm64e_bind24>(buf, sym, addend);
485 else
486 writeArm64eChainedBind<dyld_chained_ptr_arm64e_bind>(buf, sym, addend);
487 return;
488 }
489 // See writeChainedRebase() for why this is copied out.
490 dyld_chained_ptr_64_bind bind;
491 auto [ordinal, inlineAddend] = in.chainedFixups->getBinding(sym, addend);
492 bind.ordinal = ordinal;
493 bind.addend = inlineAddend;
494 bind.reserved = 0;
495 bind.next = 0;
496 bind.bind = 1;
497 memcpy(dest: buf, src: &bind, n: sizeof(bind));
498}
499
500void macho::writeChainedFixup(uint8_t *buf, const Symbol *sym, int64_t addend,
501 const AuthInfo *auth) {
502 if (needsBinding(sym))
503 writeChainedBind(buf, sym, addend, auth);
504 else
505 writeChainedRebase(buf, targetVA: sym->getVA() + addend, auth);
506}
507
508bool macho::canDisableChainedFixups() { return config->arch() != AK_arm64e; }
509
510void GotSection::writeTo(uint8_t *buf) const {
511 if (config->emitChainedFixups) {
512 for (const auto &[i, entry] : llvm::enumerate(First: entries))
513 writeChainedFixup(buf: &buf[i * target->wordSize], sym: entry, addend: 0);
514 } else {
515 for (const auto &[i, entry] : llvm::enumerate(First: entries))
516 if (auto *defined = dyn_cast<Defined>(Val: entry))
517 write64le(P: &buf[i * target->wordSize], V: defined->getVA());
518 }
519}
520
521AuthGotSection::AuthGotSection()
522 : SyntheticSection(segment_names::data, section_names::authGot) {
523 align = target->wordSize;
524 flags = S_NON_LAZY_SYMBOL_POINTERS;
525}
526
527void AuthGotSection::addEntry(Symbol *sym) {
528 auto [it, inserted] = indices.try_emplace(Key: sym, Args: entries.size());
529 if (!inserted)
530 return;
531 entries.push_back(x: sym);
532 addNonLazyBindingEntries(sym, isec, offset: it->second * target->wordSize, addend: 0,
533 /*forceOutlineAuth=*/true);
534}
535
536void AuthGotSection::writeTo(uint8_t *buf) const {
537 static constexpr AuthInfo auth = {/*diversity=*/0, .key: PtrAuthKeyIA,
538 /*addrDiv=*/true};
539 for (const auto &[i, entry] : llvm::enumerate(First: entries))
540 writeChainedFixup(buf: &buf[i * target->wordSize], sym: entry, addend: 0, auth: &auth);
541}
542
543BindingSection::BindingSection()
544 : LinkEditSection(segment_names::linkEdit, section_names::binding) {}
545
546namespace {
547struct Binding {
548 OutputSegment *segment = nullptr;
549 uint64_t offset = 0;
550 int64_t addend = 0;
551};
552struct BindIR {
553 // Default value of 0xF0 is not valid opcode and should make the program
554 // scream instead of accidentally writing "valid" values.
555 uint8_t opcode = 0xF0;
556 uint64_t data = 0;
557 uint64_t consecutiveCount = 0;
558};
559} // namespace
560
561// Encode a sequence of opcodes that tell dyld to write the address of symbol +
562// addend at osec->addr + outSecOff.
563//
564// The bind opcode "interpreter" remembers the values of each binding field, so
565// we only need to encode the differences between bindings. Hence the use of
566// lastBinding.
567static void encodeBinding(const OutputSection *osec, uint64_t outSecOff,
568 int64_t addend, Binding &lastBinding,
569 std::vector<BindIR> &opcodes) {
570 OutputSegment *seg = osec->parent;
571 uint64_t offset = osec->getSegmentOffset() + outSecOff;
572 if (lastBinding.segment != seg) {
573 opcodes.push_back(
574 x: {.opcode: static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
575 seg->index),
576 .data: offset});
577 lastBinding.segment = seg;
578 lastBinding.offset = offset;
579 } else if (lastBinding.offset != offset) {
580 opcodes.push_back(x: {.opcode: BIND_OPCODE_ADD_ADDR_ULEB, .data: offset - lastBinding.offset});
581 lastBinding.offset = offset;
582 }
583
584 if (lastBinding.addend != addend) {
585 opcodes.push_back(
586 x: {.opcode: BIND_OPCODE_SET_ADDEND_SLEB, .data: static_cast<uint64_t>(addend)});
587 lastBinding.addend = addend;
588 }
589
590 opcodes.push_back(x: {.opcode: BIND_OPCODE_DO_BIND, .data: 0});
591 // DO_BIND causes dyld to both perform the binding and increment the offset
592 lastBinding.offset += target->wordSize;
593}
594
595static void optimizeOpcodes(std::vector<BindIR> &opcodes) {
596 // Pass 1: Combine bind/add pairs
597 size_t i;
598 int pWrite = 0;
599 for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
600 if ((opcodes[i].opcode == BIND_OPCODE_ADD_ADDR_ULEB) &&
601 (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND)) {
602 opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB;
603 opcodes[pWrite].data = opcodes[i].data;
604 ++i;
605 } else {
606 opcodes[pWrite] = opcodes[i - 1];
607 }
608 }
609 if (i == opcodes.size())
610 opcodes[pWrite] = opcodes[i - 1];
611 opcodes.resize(new_size: pWrite + 1);
612
613 // Pass 2: Compress two or more bind_add opcodes
614 pWrite = 0;
615 for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
616 if ((opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
617 (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
618 (opcodes[i].data == opcodes[i - 1].data)) {
619 opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB;
620 opcodes[pWrite].consecutiveCount = 2;
621 opcodes[pWrite].data = opcodes[i].data;
622 ++i;
623 while (i < opcodes.size() &&
624 (opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
625 (opcodes[i].data == opcodes[i - 1].data)) {
626 opcodes[pWrite].consecutiveCount++;
627 ++i;
628 }
629 } else {
630 opcodes[pWrite] = opcodes[i - 1];
631 }
632 }
633 if (i == opcodes.size())
634 opcodes[pWrite] = opcodes[i - 1];
635 opcodes.resize(new_size: pWrite + 1);
636
637 // Pass 3: Use immediate encodings
638 // Every binding is the size of one pointer. If the next binding is a
639 // multiple of wordSize away that is within BIND_IMMEDIATE_MASK, the
640 // opcode can be scaled by wordSize into a single byte and dyld will
641 // expand it to the correct address.
642 for (auto &p : opcodes) {
643 // It's unclear why the check needs to be less than BIND_IMMEDIATE_MASK,
644 // but ld64 currently does this. This could be a potential bug, but
645 // for now, perform the same behavior to prevent mysterious bugs.
646 if ((p.opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
647 ((p.data / target->wordSize) < BIND_IMMEDIATE_MASK) &&
648 ((p.data % target->wordSize) == 0)) {
649 p.opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED;
650 p.data /= target->wordSize;
651 }
652 }
653}
654
655static void flushOpcodes(const BindIR &op, raw_svector_ostream &os) {
656 uint8_t opcode = op.opcode & BIND_OPCODE_MASK;
657 switch (opcode) {
658 case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
659 case BIND_OPCODE_ADD_ADDR_ULEB:
660 case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
661 os << op.opcode;
662 encodeULEB128(Value: op.data, OS&: os);
663 break;
664 case BIND_OPCODE_SET_ADDEND_SLEB:
665 os << op.opcode;
666 encodeSLEB128(Value: static_cast<int64_t>(op.data), OS&: os);
667 break;
668 case BIND_OPCODE_DO_BIND:
669 os << op.opcode;
670 break;
671 case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
672 os << op.opcode;
673 encodeULEB128(Value: op.consecutiveCount, OS&: os);
674 encodeULEB128(Value: op.data, OS&: os);
675 break;
676 case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
677 os << static_cast<uint8_t>(op.opcode | op.data);
678 break;
679 default:
680 llvm_unreachable("cannot bind to an unrecognized symbol");
681 }
682}
683
684static bool needsWeakBind(const Symbol &sym) {
685 if (auto *dysym = dyn_cast<DylibSymbol>(Val: &sym))
686 return dysym->isWeakDef();
687 if (auto *defined = dyn_cast<Defined>(Val: &sym))
688 return defined->isExternalWeakDef();
689 return false;
690}
691
692// Non-weak bindings need to have their dylib ordinal encoded as well.
693static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) {
694 if (config->namespaceKind == NamespaceKind::flat || dysym.isDynamicLookup())
695 return static_cast<int16_t>(BIND_SPECIAL_DYLIB_FLAT_LOOKUP);
696 assert(dysym.getFile()->isReferenced());
697 return dysym.getFile()->ordinal;
698}
699
700static int16_t ordinalForSymbol(const Symbol &sym) {
701 if (config->emitChainedFixups && needsWeakBind(sym))
702 return BIND_SPECIAL_DYLIB_WEAK_LOOKUP;
703 if (const auto *dysym = dyn_cast<DylibSymbol>(Val: &sym))
704 return ordinalForDylibSymbol(dysym: *dysym);
705 assert(cast<Defined>(&sym)->interposable);
706 return BIND_SPECIAL_DYLIB_FLAT_LOOKUP;
707}
708
709static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) {
710 if (ordinal <= 0) {
711 os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
712 (ordinal & BIND_IMMEDIATE_MASK));
713 } else if (ordinal <= BIND_IMMEDIATE_MASK) {
714 os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | ordinal);
715 } else {
716 os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
717 encodeULEB128(Value: ordinal, OS&: os);
718 }
719}
720
721static void encodeWeakOverride(const Defined *defined,
722 raw_svector_ostream &os) {
723 os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM |
724 BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
725 << defined->getName() << '\0';
726}
727
728// Organize the bindings so we can encoded them with fewer opcodes.
729//
730// First, all bindings for a given symbol should be grouped together.
731// BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM is the largest opcode (since it
732// has an associated symbol string), so we only want to emit it once per symbol.
733//
734// Within each group, we sort the bindings by address. Since bindings are
735// delta-encoded, sorting them allows for a more compact result. Note that
736// sorting by address alone ensures that bindings for the same segment / section
737// are located together, minimizing the number of times we have to emit
738// BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB.
739//
740// Finally, we sort the symbols by the address of their first binding, again
741// to facilitate the delta-encoding process.
742template <class Sym>
743std::vector<std::pair<const Sym *, std::vector<BindingEntry>>>
744sortBindings(const BindingsMap<const Sym *> &bindingsMap) {
745 std::vector<std::pair<const Sym *, std::vector<BindingEntry>>> bindingsVec(
746 bindingsMap.begin(), bindingsMap.end());
747 for (auto &p : bindingsVec) {
748 std::vector<BindingEntry> &bindings = p.second;
749 llvm::sort(bindings, [](const BindingEntry &a, const BindingEntry &b) {
750 return a.target.getVA() < b.target.getVA();
751 });
752 }
753 llvm::sort(bindingsVec, [](const auto &a, const auto &b) {
754 return a.second[0].target.getVA() < b.second[0].target.getVA();
755 });
756 return bindingsVec;
757}
758
759// Emit bind opcodes, which are a stream of byte-sized opcodes that dyld
760// interprets to update a record with the following fields:
761// * segment index (of the segment to write the symbol addresses to, typically
762// the __DATA_CONST segment which contains the GOT)
763// * offset within the segment, indicating the next location to write a binding
764// * symbol type
765// * symbol library ordinal (the index of its library's LC_LOAD_DYLIB command)
766// * symbol name
767// * addend
768// When dyld sees BIND_OPCODE_DO_BIND, it uses the current record state to bind
769// a symbol in the GOT, and increments the segment offset to point to the next
770// entry. It does *not* clear the record state after doing the bind, so
771// subsequent opcodes only need to encode the differences between bindings.
772void BindingSection::finalizeContents() {
773 raw_svector_ostream os{contents};
774 Binding lastBinding;
775 int16_t lastOrdinal = 0;
776
777 for (auto &p : sortBindings(bindingsMap)) {
778 const Symbol *sym = p.first;
779 std::vector<BindingEntry> &bindings = p.second;
780 uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
781 if (sym->isWeakRef())
782 flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
783 os << flags << sym->getName() << '\0'
784 << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
785 int16_t ordinal = ordinalForSymbol(sym: *sym);
786 if (ordinal != lastOrdinal) {
787 encodeDylibOrdinal(ordinal, os);
788 lastOrdinal = ordinal;
789 }
790 std::vector<BindIR> opcodes;
791 for (const BindingEntry &b : bindings)
792 encodeBinding(osec: b.target.isec->parent,
793 outSecOff: b.target.isec->getOffset(off: b.target.offset), addend: b.addend,
794 lastBinding, opcodes);
795 if (config->optimize > 1)
796 optimizeOpcodes(opcodes);
797 for (const auto &op : opcodes)
798 flushOpcodes(op, os);
799 }
800 if (!bindingsMap.empty())
801 os << static_cast<uint8_t>(BIND_OPCODE_DONE);
802}
803
804void BindingSection::writeTo(uint8_t *buf) const {
805 memcpy(dest: buf, src: contents.data(), n: contents.size());
806}
807
808WeakBindingSection::WeakBindingSection()
809 : LinkEditSection(segment_names::linkEdit, section_names::weakBinding) {}
810
811void WeakBindingSection::finalizeContents() {
812 raw_svector_ostream os{contents};
813 Binding lastBinding;
814
815 for (const Defined *defined : definitions)
816 encodeWeakOverride(defined, os);
817
818 for (auto &p : sortBindings(bindingsMap)) {
819 const Symbol *sym = p.first;
820 std::vector<BindingEntry> &bindings = p.second;
821 os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
822 << sym->getName() << '\0'
823 << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
824 std::vector<BindIR> opcodes;
825 for (const BindingEntry &b : bindings)
826 encodeBinding(osec: b.target.isec->parent,
827 outSecOff: b.target.isec->getOffset(off: b.target.offset), addend: b.addend,
828 lastBinding, opcodes);
829 if (config->optimize > 1)
830 optimizeOpcodes(opcodes);
831 for (const auto &op : opcodes)
832 flushOpcodes(op, os);
833 }
834 if (!bindingsMap.empty() || !definitions.empty())
835 os << static_cast<uint8_t>(BIND_OPCODE_DONE);
836}
837
838void WeakBindingSection::writeTo(uint8_t *buf) const {
839 memcpy(dest: buf, src: contents.data(), n: contents.size());
840}
841
842StubsSection::StubsSection()
843 : SyntheticSection(segment_names::text, config->arch() == AK_arm64e
844 ? section_names::authStubs
845 : section_names::stubs) {
846 flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
847 // The stubs section comprises machine instructions, which are aligned to
848 // 4 bytes on the archs we care about.
849 align = 4;
850 reserved2 = target->stubSize;
851}
852
853uint64_t StubsSection::getSize() const {
854 return entries.size() * target->stubSize;
855}
856
857void StubsSection::writeTo(uint8_t *buf) const {
858 size_t off = 0;
859 for (const Symbol *sym : entries) {
860 uint64_t pointerVA;
861 if (!config->emitChainedFixups)
862 pointerVA = sym->getLazyPtrVA();
863 else if (config->arch() == AK_arm64e)
864 pointerVA = in.authGot->getVA(sym);
865 else
866 pointerVA = sym->getGotVA();
867 target->writeStub(buf: buf + off, *sym, pointerVA);
868 off += target->stubSize;
869 }
870}
871
872void StubsSection::finalize() { isFinal = true; }
873
874static void addBindingsForStub(Symbol *sym) {
875 assert(!config->emitChainedFixups);
876 if (auto *dysym = dyn_cast<DylibSymbol>(Val: sym)) {
877 if (sym->isWeakDef()) {
878 in.binding->addEntry(dysym, isec: in.lazyPointers->isec,
879 offset: sym->stubsIndex * target->wordSize);
880 in.weakBinding->addEntry(symbol: sym, isec: in.lazyPointers->isec,
881 offset: sym->stubsIndex * target->wordSize);
882 } else {
883 in.lazyBinding->addEntry(dysym);
884 }
885 } else if (auto *defined = dyn_cast<Defined>(Val: sym)) {
886 if (defined->isExternalWeakDef()) {
887 in.rebase->addEntry(isec: in.lazyPointers->isec,
888 offset: sym->stubsIndex * target->wordSize);
889 in.weakBinding->addEntry(symbol: sym, isec: in.lazyPointers->isec,
890 offset: sym->stubsIndex * target->wordSize);
891 } else if (defined->interposable) {
892 in.lazyBinding->addEntry(dysym: sym);
893 } else {
894 llvm_unreachable("invalid stub target");
895 }
896 } else {
897 llvm_unreachable("invalid stub target symbol type");
898 }
899}
900
901void StubsSection::addEntry(Symbol *sym) {
902 bool inserted = entries.insert(X: sym);
903 if (inserted) {
904 sym->stubsIndex = entries.size() - 1;
905
906 if (!config->emitChainedFixups)
907 addBindingsForStub(sym);
908 else if (config->arch() == AK_arm64e)
909 in.authGot->addEntry(sym);
910 else
911 in.got->addEntry(sym);
912 }
913}
914
915StubHelperSection::StubHelperSection()
916 : SyntheticSection(segment_names::text, section_names::stubHelper) {
917 flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
918 align = 4; // This section comprises machine instructions
919}
920
921uint64_t StubHelperSection::getSize() const {
922 return target->stubHelperHeaderSize +
923 in.lazyBinding->getEntries().size() * target->stubHelperEntrySize;
924}
925
926bool StubHelperSection::isNeeded() const { return in.lazyBinding->isNeeded(); }
927
928void StubHelperSection::writeTo(uint8_t *buf) const {
929 target->writeStubHelperHeader(buf);
930 size_t off = target->stubHelperHeaderSize;
931 for (const Symbol *sym : in.lazyBinding->getEntries()) {
932 target->writeStubHelperEntry(buf: buf + off, *sym, entryAddr: addr + off);
933 off += target->stubHelperEntrySize;
934 }
935}
936
937void StubHelperSection::setUp() {
938 Symbol *binder = symtab->addUndefined(name: "dyld_stub_binder", /*file=*/nullptr,
939 /*isWeakRef=*/false);
940 if (auto *undefined = dyn_cast<Undefined>(Val: binder))
941 treatUndefinedSymbol(*undefined,
942 source: "lazy binding (normally in libSystem.dylib)");
943
944 // treatUndefinedSymbol() can replace binder with a DylibSymbol; re-check.
945 stubBinder = dyn_cast_or_null<DylibSymbol>(Val: binder);
946 if (stubBinder == nullptr)
947 return;
948
949 in.got->addEntry(sym: stubBinder);
950
951 in.imageLoaderCache->parent =
952 ConcatOutputSection::getOrCreateForInput(in.imageLoaderCache);
953 addInputSection(inputSection: in.imageLoaderCache);
954 // Since this isn't in the symbol table or in any input file, the noDeadStrip
955 // argument doesn't matter.
956 dyldPrivate =
957 make<Defined>(args: "__dyld_private", args: nullptr, args&: in.imageLoaderCache, args: 0, args: 0,
958 /*isWeakDef=*/args: false,
959 /*isExternal=*/args: false, /*isPrivateExtern=*/args: false,
960 /*includeInSymtab=*/args: true,
961 /*isReferencedDynamically=*/args: false,
962 /*noDeadStrip=*/args: false);
963 dyldPrivate->used = true;
964}
965
966llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>
967 ObjCSelRefsHelper::methnameToSelref;
968void ObjCSelRefsHelper::initialize() {
969 // Do not fold selrefs without ICF.
970 if (config->icfLevel == ICFLevel::none)
971 return;
972
973 // Search methnames already referenced in __objc_selrefs
974 // Map the name to the corresponding selref entry
975 // which we will reuse when creating objc stubs.
976 for (ConcatInputSection *isec : inputSections) {
977 if (isec->shouldOmitFromOutput())
978 continue;
979 if (isec->getName() != section_names::objcSelrefs)
980 continue;
981 // We expect a single relocation per selref entry to __objc_methname that
982 // might be aggregated.
983 assert(isec->relocs.size() == 1);
984 auto Reloc = isec->relocs[0];
985 if (const auto *sym = dyn_cast_if_present<Symbol *>(Val&: Reloc.referent)) {
986 if (const auto *d = dyn_cast<Defined>(Val: sym)) {
987 auto *cisec = cast<CStringInputSection>(Val: d->isec());
988 auto methname = cisec->getStringRefAtOffset(off: d->value);
989 methnameToSelref[CachedHashStringRef(methname)] = isec;
990 }
991 }
992 }
993}
994
995void ObjCSelRefsHelper::cleanup() { methnameToSelref.clear(); }
996
997ConcatInputSection *ObjCSelRefsHelper::makeSelRef(StringRef methname) {
998 auto methnameOffset = in.objcMethnameSection->getStringOffset(str: methname);
999
1000 size_t wordSize = target->wordSize;
1001 uint8_t *selrefData = bAlloc().Allocate<uint8_t>(Num: wordSize);
1002 write64le(P: selrefData, V: methnameOffset);
1003 ConcatInputSection *objcSelref =
1004 makeSyntheticInputSection(segName: segment_names::data, sectName: section_names::objcSelrefs,
1005 flags: S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP,
1006 data: ArrayRef<uint8_t>{selrefData, wordSize},
1007 /*align=*/wordSize);
1008 assert(objcSelref->live);
1009 objcSelref->relocs.push_back(x: {/*type=*/target->unsignedRelocType,
1010 /*pcrel=*/false, /*length=*/3,
1011 /*offset=*/0,
1012 /*addend=*/static_cast<int64_t>(methnameOffset),
1013 /*referent=*/in.objcMethnameSection->isec});
1014 objcSelref->parent = ConcatOutputSection::getOrCreateForInput(objcSelref);
1015 addInputSection(inputSection: objcSelref);
1016 objcSelref->isFinal = true;
1017 methnameToSelref[CachedHashStringRef(methname)] = objcSelref;
1018 return objcSelref;
1019}
1020
1021ConcatInputSection *ObjCSelRefsHelper::getSelRef(StringRef methname) {
1022 auto it = methnameToSelref.find(Val: CachedHashStringRef(methname));
1023 if (it == methnameToSelref.end())
1024 return nullptr;
1025 return it->second;
1026}
1027
1028ObjCStubsSection::ObjCStubsSection()
1029 : SyntheticSection(segment_names::text, section_names::objcStubs) {
1030 flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
1031 align = config->objcStubsMode == ObjCStubsMode::fast
1032 ? target->objcStubsFastAlignment
1033 : target->objcStubsSmallAlignment;
1034}
1035
1036bool ObjCStubsSection::isObjCMsgSendStubSymbol(Symbol *sym) {
1037 return sym->getName().starts_with(Prefix: objcMsgSendStubPrefix);
1038}
1039
1040bool ObjCStubsSection::isObjCClassStubSymbol(Symbol *sym) {
1041 return sym->getName().starts_with(Prefix: objcMsgSendClassStubPrefix);
1042}
1043
1044bool ObjCStubsSection::isObjCStubSymbol(Symbol *sym) {
1045 return isObjCMsgSendStubSymbol(sym) || isObjCClassStubSymbol(sym);
1046}
1047
1048StringRef ObjCStubsSection::getMethname(Symbol *sym) {
1049 assert(isObjCStubSymbol(sym) && "not an objc stub");
1050 if (isObjCMsgSendStubSymbol(sym))
1051 return sym->getName().drop_front(N: objcMsgSendStubPrefix.size());
1052
1053 assert(isObjCClassStubSymbol(sym) && "not an objc class stub");
1054 std::optional<ObjCClassStubNames> parsed = parseObjCClassStubSymbol(sym);
1055 assert(parsed && "malformed objc class stub");
1056 return parsed->selectorName;
1057}
1058
1059std::optional<ObjCStubsSection::ObjCClassStubNames>
1060ObjCStubsSection::parseObjCClassStubSymbol(Symbol *sym) {
1061 assert(isObjCClassStubSymbol(sym) && "not an objc class stub");
1062 StringRef name = sym->getName().drop_front(N: objcMsgSendClassStubPrefix.size());
1063 // For _objc_msgSendClass$alloc$_OBJC_CLASS_$_Foo, return
1064 // selectorName "alloc" and classSymbolName "_OBJC_CLASS_$_Foo".
1065 auto [selectorName, classSymbolName] = name.split(Separator: '$');
1066 if (selectorName.empty() || classSymbolName.empty() ||
1067 !classSymbolName.starts_with(Prefix: objcClassSymbolPrefix))
1068 return std::nullopt;
1069 return ObjCClassStubNames{.selectorName: selectorName, .classSymbolName: classSymbolName};
1070}
1071
1072void ObjCStubsSection::recordClassSymbol(Symbol *stubSym, Symbol *classSym) {
1073 // SymbolTable updates symbols in place with replaceSymbol(), so this pointer
1074 // remains valid if an Undefined later becomes a DylibSymbol or Defined.
1075 classSymbols[stubSym] = classSym;
1076}
1077
1078Symbol *ObjCStubsSection::lookupClassSymbol(Symbol *stubSym) const {
1079 return classSymbols.lookup(Val: stubSym);
1080}
1081
1082size_t ObjCStubsSection::getStubSize(Symbol *sym) const {
1083 if (config->objcStubsMode == ObjCStubsMode::fast)
1084 return isObjCClassStubSymbol(sym) ? target->objcClassStubsFastSize
1085 : target->objcStubsFastSize;
1086 return isObjCClassStubSymbol(sym) ? target->objcClassStubsSmallSize
1087 : target->objcStubsSmallSize;
1088}
1089
1090void ObjCStubsSection::addEntry(Symbol *sym) {
1091 // Replace invalid stubs to avoid follow-on undefined-symbol diagnostics.
1092 auto replaceWithEmptyStub = [&] {
1093 replaceSymbol<Defined>(
1094 s: sym, arg: sym->getName(), arg: nullptr, arg&: isec, /*value=*/arg&: stubsSectionSize,
1095 /*size=*/arg: 0,
1096 /*isWeakDef=*/arg: false, /*isExternal=*/arg: true, /*isPrivateExtern=*/arg: true,
1097 /*includeInSymtab=*/arg: true, /*isReferencedDynamically=*/arg: false,
1098 /*noDeadStrip=*/arg: false);
1099 };
1100
1101 StringRef selectorName;
1102 Symbol *classSym = nullptr;
1103 if (isObjCClassStubSymbol(sym)) {
1104 if (!target->supportsObjCClassStubs()) {
1105 error(msg: "objc class stubs are not supported for " +
1106 getArchitectureName(Arch: config->arch()));
1107 replaceWithEmptyStub();
1108 return;
1109 }
1110 std::optional<ObjCClassStubNames> parsed = parseObjCClassStubSymbol(sym);
1111 if (!parsed) {
1112 error(msg: "malformed objc class stub symbol " + sym->getName() +
1113 "; expected " + objcMsgSendClassStubPrefix + "<selector>" + "$" +
1114 objcClassSymbolPrefix + "<class>");
1115 replaceWithEmptyStub();
1116 return;
1117 }
1118 selectorName = parsed->selectorName;
1119 classSym = lookupClassSymbol(stubSym: sym);
1120 if (!classSym) {
1121 error(msg: "objc class stub symbol " + sym->getName() +
1122 " references missing class symbol " + parsed->classSymbolName);
1123 replaceWithEmptyStub();
1124 return;
1125 }
1126 if (auto *undefined = dyn_cast<Undefined>(Val: classSym)) {
1127 treatUndefinedSymbol(*undefined, source: "objc class stub");
1128 if (isa<Undefined>(Val: classSym)) {
1129 replaceWithEmptyStub();
1130 return;
1131 }
1132 }
1133 if (auto *dysym = dyn_cast<DylibSymbol>(Val: classSym)) {
1134 // Preserve effective import weakness from a classref or a weak library.
1135 // Otherwise, an unreferenced class still defaults to a strong import.
1136 dysym->reference(newState: dysym->isWeakRef() ? RefState::Weak : RefState::Strong);
1137 in.got->addEntry(sym: dysym);
1138 } else if (needsBinding(sym: classSym)) {
1139 // Interposable or weak definitions in this image.
1140 in.got->addEntry(sym: classSym);
1141 }
1142 classSym->used = true;
1143 } else {
1144 selectorName = getMethname(sym);
1145 }
1146
1147 size_t stubSize = getStubSize(sym);
1148 // We create a selref entry for each unique selector.
1149 if (!ObjCSelRefsHelper::getSelRef(methname: selectorName))
1150 ObjCSelRefsHelper::makeSelRef(methname: selectorName);
1151
1152 Defined *newSym = replaceSymbol<Defined>(
1153 s: sym, arg: sym->getName(), arg: nullptr, arg&: isec,
1154 /*value=*/arg&: stubsSectionSize,
1155 /*size=*/arg&: stubSize,
1156 /*isWeakDef=*/arg: false, /*isExternal=*/arg: true, /*isPrivateExtern=*/arg: true,
1157 /*includeInSymtab=*/arg: true, /*isReferencedDynamically=*/arg: false,
1158 /*noDeadStrip=*/arg: false);
1159 symbols.push_back(x: newSym);
1160 stubsSectionSize += stubSize;
1161}
1162
1163void ObjCStubsSection::setUp() {
1164 objcMsgSend = symtab->addUndefined(name: "_objc_msgSend", /*file=*/nullptr,
1165 /*isWeakRef=*/false);
1166 if (auto *undefined = dyn_cast<Undefined>(Val: objcMsgSend)) {
1167 treatUndefinedSymbol(*undefined,
1168 source: "lazy binding (normally in libobjc.dylib)");
1169 // treatUndefinedSymbol() can replace its arg with a DylibSymbol; re-check.
1170 if (isa<Undefined>(Val: objcMsgSend))
1171 return;
1172 }
1173 objcMsgSend->used = true;
1174 if (config->objcStubsMode == ObjCStubsMode::fast) {
1175 if (config->arch() == AK_arm64e) {
1176 in.authGot->addEntry(sym: objcMsgSend);
1177 } else {
1178 in.got->addEntry(sym: objcMsgSend);
1179 assert(objcMsgSend->isInGot());
1180 }
1181 } else {
1182 assert(config->objcStubsMode == ObjCStubsMode::small);
1183 // In line with ld64's behavior, when objc_msgSend is a direct symbol,
1184 // we directly reference it.
1185 // In other cases, typically when binding in libobjc.dylib,
1186 // we generate a stub to invoke objc_msgSend.
1187 if (!isa<Defined>(Val: objcMsgSend))
1188 in.stubs->addEntry(sym: objcMsgSend);
1189 }
1190}
1191
1192uint64_t ObjCStubsSection::getSize() const { return stubsSectionSize; }
1193
1194void ObjCStubsSection::sortSymbols(
1195 const llvm::DenseMap<const Symbol *, int> &priorities) {
1196 llvm::stable_sort(Range&: symbols, C: [&](const Defined *a, const Defined *b) {
1197 auto priority = [&](const Defined *sym) {
1198 auto it = priorities.find(Val: sym);
1199 return it == priorities.end() ? std::numeric_limits<int>::max()
1200 : it->second;
1201 };
1202 return priority(a) < priority(b);
1203 });
1204 size_t stubOffset = 0;
1205 for (Defined *sym : symbols) {
1206 sym->value = stubOffset;
1207 stubOffset += sym->size;
1208 }
1209 assert(stubOffset == stubsSectionSize);
1210}
1211
1212void ObjCStubsSection::writeTo(uint8_t *buf) const {
1213 uint64_t stubOffset = 0;
1214 for (Defined *sym : symbols) {
1215 auto methname = getMethname(sym);
1216 InputSection *selRef = ObjCSelRefsHelper::getSelRef(methname);
1217 assert(selRef != nullptr && "no selref for methname");
1218 auto selrefAddr = selRef->getVA(off: 0);
1219 if (isObjCClassStubSymbol(sym)) {
1220 Symbol *classSym = classSymbols.lookup(Val: sym);
1221 assert(classSym != nullptr && "no class symbol for objc class stub");
1222 target->writeObjCMsgSendClassStub(buf + stubOffset, sym,
1223 in.objcStubs->addr, stubOffset,
1224 selrefAddr, classSym, objcMsgSend);
1225 } else {
1226 target->writeObjCMsgSendStub(buf: buf + stubOffset, sym, stubsAddr: in.objcStubs->addr,
1227 stubOffset, selrefVA: selrefAddr, objcMsgSend);
1228 }
1229 }
1230}
1231
1232LazyPointerSection::LazyPointerSection()
1233 : SyntheticSection(segment_names::data, section_names::lazySymbolPtr) {
1234 align = target->wordSize;
1235 flags = S_LAZY_SYMBOL_POINTERS;
1236}
1237
1238uint64_t LazyPointerSection::getSize() const {
1239 return in.stubs->getEntries().size() * target->wordSize;
1240}
1241
1242bool LazyPointerSection::isNeeded() const {
1243 return !in.stubs->getEntries().empty();
1244}
1245
1246void LazyPointerSection::writeTo(uint8_t *buf) const {
1247 size_t off = 0;
1248 for (const Symbol *sym : in.stubs->getEntries()) {
1249 if (const auto *dysym = dyn_cast<DylibSymbol>(Val: sym)) {
1250 if (dysym->hasStubsHelper()) {
1251 uint64_t stubHelperOffset =
1252 target->stubHelperHeaderSize +
1253 dysym->stubsHelperIndex * target->stubHelperEntrySize;
1254 write64le(P: buf + off, V: in.stubHelper->addr + stubHelperOffset);
1255 }
1256 } else {
1257 write64le(P: buf + off, V: sym->getVA());
1258 }
1259 off += target->wordSize;
1260 }
1261}
1262
1263LazyBindingSection::LazyBindingSection()
1264 : LinkEditSection(segment_names::linkEdit, section_names::lazyBinding) {}
1265
1266void LazyBindingSection::finalizeContents() {
1267 // TODO: Just precompute output size here instead of writing to a temporary
1268 // buffer
1269 for (Symbol *sym : entries)
1270 sym->lazyBindOffset = encode(*sym);
1271}
1272
1273void LazyBindingSection::writeTo(uint8_t *buf) const {
1274 memcpy(dest: buf, src: contents.data(), n: contents.size());
1275}
1276
1277void LazyBindingSection::addEntry(Symbol *sym) {
1278 assert(!config->emitChainedFixups && "Chained fixups always bind eagerly");
1279 if (entries.insert(X: sym)) {
1280 sym->stubsHelperIndex = entries.size() - 1;
1281 in.rebase->addEntry(isec: in.lazyPointers->isec,
1282 offset: sym->stubsIndex * target->wordSize);
1283 }
1284}
1285
1286// Unlike the non-lazy binding section, the bind opcodes in this section aren't
1287// interpreted all at once. Rather, dyld will start interpreting opcodes at a
1288// given offset, typically only binding a single symbol before it finds a
1289// BIND_OPCODE_DONE terminator. As such, unlike in the non-lazy-binding case,
1290// we cannot encode just the differences between symbols; we have to emit the
1291// complete bind information for each symbol.
1292uint32_t LazyBindingSection::encode(const Symbol &sym) {
1293 uint32_t opstreamOffset = contents.size();
1294 OutputSegment *dataSeg = in.lazyPointers->parent;
1295 os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
1296 dataSeg->index);
1297 uint64_t offset =
1298 in.lazyPointers->addr - dataSeg->addr + sym.stubsIndex * target->wordSize;
1299 encodeULEB128(Value: offset, OS&: os);
1300 encodeDylibOrdinal(ordinal: ordinalForSymbol(sym), os);
1301
1302 uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
1303 if (sym.isWeakRef())
1304 flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
1305
1306 os << flags << sym.getName() << '\0'
1307 << static_cast<uint8_t>(BIND_OPCODE_DO_BIND)
1308 << static_cast<uint8_t>(BIND_OPCODE_DONE);
1309 return opstreamOffset;
1310}
1311
1312ExportSection::ExportSection()
1313 : LinkEditSection(segment_names::linkEdit, section_names::export_) {}
1314
1315void ExportSection::finalizeContents() {
1316 trieBuilder.setImageBase(in.header->addr);
1317 for (const Symbol *sym : symtab->getSymbols()) {
1318 if (const auto *defined = dyn_cast<Defined>(Val: sym)) {
1319 if (defined->privateExtern || !defined->isLive())
1320 continue;
1321 trieBuilder.addSymbol(sym: *defined);
1322 hasWeakSymbol = hasWeakSymbol || sym->isWeakDef();
1323 } else if (auto *dysym = dyn_cast<DylibSymbol>(Val: sym)) {
1324 if (dysym->shouldReexport)
1325 trieBuilder.addSymbol(sym: *dysym);
1326 }
1327 }
1328 size = trieBuilder.build();
1329}
1330
1331void ExportSection::writeTo(uint8_t *buf) const { trieBuilder.writeTo(buf); }
1332
1333DataInCodeSection::DataInCodeSection()
1334 : LinkEditSection(segment_names::linkEdit, section_names::dataInCode) {}
1335
1336template <class LP>
1337static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() {
1338 std::vector<MachO::data_in_code_entry> dataInCodeEntries;
1339 for (const InputFile *inputFile : inputFiles) {
1340 if (!isa<ObjFile>(Val: inputFile))
1341 continue;
1342 const ObjFile *objFile = cast<ObjFile>(Val: inputFile);
1343 ArrayRef<MachO::data_in_code_entry> entries = objFile->getDataInCode();
1344 if (entries.empty())
1345 continue;
1346
1347 std::vector<MachO::data_in_code_entry> sortedEntries;
1348 sortedEntries.assign(first: entries.begin(), last: entries.end());
1349 llvm::sort(sortedEntries, [](const data_in_code_entry &lhs,
1350 const data_in_code_entry &rhs) {
1351 return lhs.offset < rhs.offset;
1352 });
1353
1354 // For each code subsection find 'data in code' entries residing in it.
1355 // Compute the new offset values as
1356 // <offset within subsection> + <subsection address> - <__TEXT address>.
1357 for (const Section *section : objFile->sections) {
1358 for (const Subsection &subsec : section->subsections) {
1359 const InputSection *isec = subsec.isec;
1360 if (!isCodeSection(isec))
1361 continue;
1362 if (cast<ConcatInputSection>(Val: isec)->shouldOmitFromOutput())
1363 continue;
1364 const uint64_t beginAddr = section->addr + subsec.offset;
1365 auto it = llvm::lower_bound(
1366 sortedEntries, beginAddr,
1367 [](const MachO::data_in_code_entry &entry, uint64_t addr) {
1368 return entry.offset < addr;
1369 });
1370 const uint64_t endAddr = beginAddr + isec->getSize();
1371 for (const auto end = sortedEntries.end();
1372 it != end && it->offset + it->length <= endAddr; ++it)
1373 dataInCodeEntries.push_back(
1374 {static_cast<uint32_t>(isec->getVA(off: it->offset - beginAddr) -
1375 in.header->addr),
1376 it->length, it->kind});
1377 }
1378 }
1379 }
1380
1381 // ld64 emits the table in sorted order too.
1382 llvm::sort(dataInCodeEntries,
1383 [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) {
1384 return lhs.offset < rhs.offset;
1385 });
1386 return dataInCodeEntries;
1387}
1388
1389void DataInCodeSection::finalizeContents() {
1390 entries = target->wordSize == 8 ? collectDataInCodeEntries<LP64>()
1391 : collectDataInCodeEntries<ILP32>();
1392}
1393
1394void DataInCodeSection::writeTo(uint8_t *buf) const {
1395 if (!entries.empty())
1396 memcpy(dest: buf, src: entries.data(), n: getRawSize());
1397}
1398
1399FunctionStartsSection::FunctionStartsSection()
1400 : LinkEditSection(segment_names::linkEdit, section_names::functionStarts) {}
1401
1402void FunctionStartsSection::finalizeContents() {
1403 raw_svector_ostream os{contents};
1404 std::vector<uint64_t> addrs;
1405 for (const InputFile *file : inputFiles) {
1406 if (auto *objFile = dyn_cast<ObjFile>(Val: file)) {
1407 for (const Symbol *sym : objFile->symbols) {
1408 if (const auto *defined = dyn_cast_or_null<Defined>(Val: sym)) {
1409 if (!defined->isec() || !isCodeSection(defined->isec()) ||
1410 !defined->isLive())
1411 continue;
1412 addrs.push_back(x: defined->getVA());
1413 }
1414 }
1415 }
1416 }
1417 llvm::sort(C&: addrs);
1418 uint64_t addr = in.header->addr;
1419 for (uint64_t nextAddr : addrs) {
1420 uint64_t delta = nextAddr - addr;
1421 if (delta == 0)
1422 continue;
1423 encodeULEB128(Value: delta, OS&: os);
1424 addr = nextAddr;
1425 }
1426 os << '\0';
1427}
1428
1429void FunctionStartsSection::writeTo(uint8_t *buf) const {
1430 memcpy(dest: buf, src: contents.data(), n: contents.size());
1431}
1432
1433SymtabSection::SymtabSection(StringTableSection &stringTableSection)
1434 : LinkEditSection(segment_names::linkEdit, section_names::symbolTable),
1435 stringTableSection(stringTableSection) {}
1436
1437void SymtabSection::emitBeginSourceStab(StringRef sourceFile) {
1438 StabsEntry stab(N_SO);
1439 stab.strx = stringTableSection.addString(saver().save(S: sourceFile));
1440 stabs.emplace_back(args: std::move(stab));
1441}
1442
1443void SymtabSection::emitEndSourceStab() {
1444 StabsEntry stab(N_SO);
1445 stab.sect = 1;
1446 stabs.emplace_back(args: std::move(stab));
1447}
1448
1449void SymtabSection::emitObjectFileStab(ObjFile *file) {
1450 StabsEntry stab(N_OSO);
1451 stab.sect = target->cpuSubtype;
1452 SmallString<261> path(!file->archiveName.empty() ? file->archiveName
1453 : file->getName());
1454 std::error_code ec = sys::fs::make_absolute(path);
1455 if (ec)
1456 fatal(msg: "failed to get absolute path for " + path);
1457
1458 if (!file->archiveName.empty())
1459 path.append(Refs: {"(", file->getName(), ")"});
1460
1461 StringRef adjustedPath = saver().save(S: path.str());
1462 adjustedPath.consume_front(Prefix: config->osoPrefix);
1463
1464 stab.strx = stringTableSection.addString(adjustedPath);
1465 stab.desc = 1;
1466 stab.value = file->modTime;
1467 stabs.emplace_back(args: std::move(stab));
1468}
1469
1470void SymtabSection::emitEndFunStab(Defined *defined) {
1471 StabsEntry stab(N_FUN);
1472 stab.value = defined->size;
1473 stabs.emplace_back(args: std::move(stab));
1474}
1475
1476void SymtabSection::emitStabs() {
1477 if (config->omitDebugInfo)
1478 return;
1479
1480 for (const std::string &s : config->astPaths) {
1481 StabsEntry astStab(N_AST);
1482 astStab.strx = stringTableSection.addString(s);
1483 stabs.emplace_back(args: std::move(astStab));
1484 }
1485
1486 // Cache the file ID for each symbol in an std::pair for faster sorting.
1487 using SortingPair = std::pair<Defined *, int>;
1488 std::vector<SortingPair> symbolsNeedingStabs;
1489 for (const SymtabEntry &entry :
1490 concat<SymtabEntry>(Ranges&: localSymbols, Ranges&: externalSymbols)) {
1491 Symbol *sym = entry.sym;
1492 assert(sym->isLive() &&
1493 "dead symbols should not be in localSymbols, externalSymbols");
1494 if (auto *defined = dyn_cast<Defined>(Val: sym)) {
1495 // Excluded symbols should have been filtered out in finalizeContents().
1496 assert(defined->includeInSymtab);
1497 if (defined->isAbsolute())
1498 continue;
1499
1500 // Constant-folded symbols go in the executable's symbol table, but don't
1501 // get a stabs entry unless --keep-icf-stabs flag is specified.
1502 if (!config->keepICFStabs &&
1503 defined->identicalCodeFoldingKind != Symbol::ICFFoldKind::None)
1504 continue;
1505
1506 ObjFile *file = defined->getObjectFile();
1507 if (!file || !file->compileUnit)
1508 continue;
1509
1510 // We use the symbol's original InputSection to get the file id,
1511 // even for ICF folded symbols, to ensure STABS entries point to the
1512 // correct object file where the symbol was originally defined
1513 symbolsNeedingStabs.emplace_back(args&: defined,
1514 args: defined->originalIsec->getFile()->id);
1515 }
1516 }
1517
1518 llvm::stable_sort(Range&: symbolsNeedingStabs, C: llvm::less_second());
1519
1520 llvm::MapVector<ObjFile *, std::string> stabFiles;
1521 for (const auto &[defined, fileId] : symbolsNeedingStabs) {
1522 ObjFile *file = cast<ObjFile>(Val: defined->originalIsec->getFile());
1523 stabFiles[file] = "";
1524 }
1525 parallelForEach(R&: stabFiles,
1526 Fn: [&](auto &it) { it.second = it.first->sourceFile(); });
1527
1528 // Emit STABS symbols so that dsymutil and/or the debugger can map address
1529 // regions in the final binary to the source and object files from which they
1530 // originated.
1531 InputFile *lastFile = nullptr;
1532 for (SortingPair &pair : symbolsNeedingStabs) {
1533 Defined *defined = pair.first;
1534 // When emitting STABS entries for a symbol, always use the original
1535 // InputSection of the defined symbol, not the section of the function body
1536 // (which might be a different function entirely if ICF folded this
1537 // function). This ensures STABS entries point back to the original object
1538 // file.
1539 InputSection *isec = defined->originalIsec;
1540 ObjFile *file = cast<ObjFile>(Val: isec->getFile());
1541
1542 if (lastFile == nullptr || lastFile != file) {
1543 if (lastFile != nullptr)
1544 emitEndSourceStab();
1545 lastFile = file;
1546
1547 emitBeginSourceStab(sourceFile: stabFiles[file]);
1548 emitObjectFileStab(file);
1549 }
1550
1551 StabsEntry symStab;
1552 symStab.sect = isec->parent->index;
1553 symStab.strx = stringTableSection.addString(defined->getName());
1554
1555 // When using --keep-icf-stabs, we need to use the VA of the actual function
1556 // body that the linker will place in the binary. This is the function that
1557 // the symbol refers to after ICF folding.
1558 if (defined->identicalCodeFoldingKind == Symbol::ICFFoldKind::Thunk) {
1559 // For thunks, we need to get the function they point to
1560 Defined *target = getBodyForThunkFoldedSym(foldedSym: defined);
1561 symStab.value = target->getVA();
1562 } else {
1563 symStab.value = defined->getVA();
1564 }
1565
1566 if (isCodeSection(isec)) {
1567 symStab.type = N_FUN;
1568 stabs.emplace_back(args: std::move(symStab));
1569 // For the end function marker in STABS, we need to use the size of the
1570 // actual function body that exists in the output binary
1571 if (defined->identicalCodeFoldingKind == Symbol::ICFFoldKind::Thunk) {
1572 // For thunks, we use the target's size
1573 Defined *target = getBodyForThunkFoldedSym(foldedSym: defined);
1574 emitEndFunStab(defined: target);
1575 } else {
1576 emitEndFunStab(defined);
1577 }
1578 } else {
1579 symStab.type = defined->isExternal() ? N_GSYM : N_STSYM;
1580 stabs.emplace_back(args: std::move(symStab));
1581 }
1582 }
1583
1584 if (!stabs.empty())
1585 emitEndSourceStab();
1586}
1587
1588void SymtabSection::finalizeContents() {
1589 auto addSymbol = [&](std::vector<SymtabEntry> &symbols, Symbol *sym) {
1590 uint32_t strx = stringTableSection.addString(sym->getName());
1591 symbols.push_back(x: {.sym: sym, .strx: strx});
1592 };
1593
1594 std::function<void(Symbol *)> localSymbolsHandler;
1595 switch (config->localSymbolsPresence) {
1596 case SymtabPresence::All:
1597 localSymbolsHandler = [&](Symbol *sym) { addSymbol(localSymbols, sym); };
1598 break;
1599 case SymtabPresence::None:
1600 localSymbolsHandler = [&](Symbol *) { /* Do nothing*/ };
1601 break;
1602 case SymtabPresence::SelectivelyIncluded:
1603 localSymbolsHandler = [&](Symbol *sym) {
1604 if (config->localSymbolPatterns.match(symbolName: sym->getName()))
1605 addSymbol(localSymbols, sym);
1606 };
1607 break;
1608 case SymtabPresence::SelectivelyExcluded:
1609 localSymbolsHandler = [&](Symbol *sym) {
1610 if (!config->localSymbolPatterns.match(symbolName: sym->getName()))
1611 addSymbol(localSymbols, sym);
1612 };
1613 break;
1614 }
1615
1616 // Local symbols aren't in the SymbolTable, so we walk the list of object
1617 // files to gather them.
1618 // But if `-x` is set, then we don't need to. localSymbolsHandler() will do
1619 // the right thing regardless, but this check is a perf optimization because
1620 // iterating through all the input files and their symbols is expensive.
1621 if (config->localSymbolsPresence != SymtabPresence::None) {
1622 for (const InputFile *file : inputFiles) {
1623 if (auto *objFile = dyn_cast<ObjFile>(Val: file)) {
1624 for (Symbol *sym : objFile->symbols) {
1625 if (auto *defined = dyn_cast_or_null<Defined>(Val: sym)) {
1626 if (defined->isExternal() || !defined->isLive() ||
1627 !defined->includeInSymtab)
1628 continue;
1629 localSymbolsHandler(sym);
1630 }
1631 }
1632 }
1633 }
1634 }
1635
1636 // __dyld_private is a local symbol too. It's linker-created and doesn't
1637 // exist in any object file.
1638 if (in.stubHelper && in.stubHelper->dyldPrivate)
1639 localSymbolsHandler(in.stubHelper->dyldPrivate);
1640
1641 for (Symbol *sym : symtab->getSymbols()) {
1642 if (!sym->isLive())
1643 continue;
1644 if (auto *defined = dyn_cast<Defined>(Val: sym)) {
1645 if (!defined->includeInSymtab)
1646 continue;
1647 assert(defined->isExternal());
1648 if (defined->privateExtern)
1649 localSymbolsHandler(defined);
1650 else
1651 addSymbol(externalSymbols, defined);
1652 } else if (auto *dysym = dyn_cast<DylibSymbol>(Val: sym)) {
1653 if (dysym->isReferenced())
1654 addSymbol(undefinedSymbols, sym);
1655 }
1656 }
1657
1658 emitStabs();
1659 uint32_t symtabIndex = stabs.size();
1660 for (const SymtabEntry &entry :
1661 concat<SymtabEntry>(Ranges&: localSymbols, Ranges&: externalSymbols, Ranges&: undefinedSymbols)) {
1662 entry.sym->symtabIndex = symtabIndex++;
1663 }
1664}
1665
1666uint32_t SymtabSection::getNumSymbols() const {
1667 return stabs.size() + localSymbols.size() + externalSymbols.size() +
1668 undefinedSymbols.size();
1669}
1670
1671// This serves to hide (type-erase) the template parameter from SymtabSection.
1672template <class LP> class SymtabSectionImpl final : public SymtabSection {
1673public:
1674 SymtabSectionImpl(StringTableSection &stringTableSection)
1675 : SymtabSection(stringTableSection) {}
1676 uint64_t getRawSize() const override;
1677 void writeTo(uint8_t *buf) const override;
1678};
1679
1680template <class LP> uint64_t SymtabSectionImpl<LP>::getRawSize() const {
1681 return getNumSymbols() * sizeof(typename LP::nlist);
1682}
1683
1684template <class LP> void SymtabSectionImpl<LP>::writeTo(uint8_t *buf) const {
1685 auto *nList = reinterpret_cast<typename LP::nlist *>(buf);
1686 // Emit the stabs entries before the "real" symbols. We cannot emit them
1687 // after as that would render Symbol::symtabIndex inaccurate.
1688 for (const StabsEntry &entry : stabs) {
1689 nList->n_strx = entry.strx;
1690 nList->n_type = entry.type;
1691 nList->n_sect = entry.sect;
1692 nList->n_desc = entry.desc;
1693 nList->n_value = entry.value;
1694 ++nList;
1695 }
1696
1697 for (const SymtabEntry &entry : concat<const SymtabEntry>(
1698 localSymbols, externalSymbols, undefinedSymbols)) {
1699 nList->n_strx = entry.strx;
1700 // TODO populate n_desc with more flags
1701 if (auto *defined = dyn_cast<Defined>(Val: entry.sym)) {
1702 uint8_t scope = 0;
1703 if (defined->privateExtern) {
1704 // Private external -- dylib scoped symbol.
1705 // Promote to non-external at link time.
1706 scope = N_PEXT;
1707 } else if (defined->isExternal()) {
1708 // Normal global symbol.
1709 scope = N_EXT;
1710 } else {
1711 // TU-local symbol from localSymbols.
1712 scope = 0;
1713 }
1714
1715 if (defined->isAbsolute()) {
1716 nList->n_type = scope | N_ABS;
1717 nList->n_sect = NO_SECT;
1718 nList->n_value = defined->value;
1719 } else {
1720 nList->n_type = scope | N_SECT;
1721 nList->n_sect = defined->isec()->parent->index;
1722 // For the N_SECT symbol type, n_value is the address of the symbol
1723 nList->n_value = defined->getVA();
1724 }
1725 nList->n_desc |= defined->isExternalWeakDef() ? N_WEAK_DEF : 0;
1726 nList->n_desc |=
1727 defined->referencedDynamically ? REFERENCED_DYNAMICALLY : 0;
1728 if (config->outputType == MH_OBJECT)
1729 nList->n_desc |= defined->isCold() ? N_COLD_FUNC : 0;
1730 } else if (auto *dysym = dyn_cast<DylibSymbol>(Val: entry.sym)) {
1731 uint16_t n_desc = nList->n_desc;
1732 int16_t ordinal = ordinalForDylibSymbol(dysym: *dysym);
1733 if (ordinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
1734 SET_LIBRARY_ORDINAL(n_desc, ordinal: DYNAMIC_LOOKUP_ORDINAL);
1735 else if (ordinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
1736 SET_LIBRARY_ORDINAL(n_desc, ordinal: EXECUTABLE_ORDINAL);
1737 else {
1738 assert(ordinal > 0);
1739 SET_LIBRARY_ORDINAL(n_desc, ordinal: static_cast<uint8_t>(ordinal));
1740 }
1741
1742 nList->n_type = N_EXT;
1743 n_desc |= dysym->isWeakDef() ? N_WEAK_DEF : 0;
1744 n_desc |= dysym->isWeakRef() ? N_WEAK_REF : 0;
1745 nList->n_desc = n_desc;
1746 }
1747 ++nList;
1748 }
1749}
1750
1751template <class LP>
1752SymtabSection *
1753macho::makeSymtabSection(StringTableSection &stringTableSection) {
1754 return make<SymtabSectionImpl<LP>>(stringTableSection);
1755}
1756
1757IndirectSymtabSection::IndirectSymtabSection()
1758 : LinkEditSection(segment_names::linkEdit,
1759 section_names::indirectSymbolTable) {}
1760
1761uint32_t IndirectSymtabSection::getNumSymbols() const {
1762 uint32_t size = in.got->getEntries().size() + in.stubs->getEntries().size();
1763 if (in.authGot)
1764 size += in.authGot->getEntries().size();
1765 if (!config->emitChainedFixups)
1766 size += in.stubs->getEntries().size();
1767 return size;
1768}
1769
1770bool IndirectSymtabSection::isNeeded() const {
1771 return in.got->isNeeded() || in.stubs->isNeeded() ||
1772 (in.authGot && in.authGot->isNeeded());
1773}
1774
1775void IndirectSymtabSection::finalizeContents() {
1776 uint32_t off = 0;
1777 in.got->reserved1 = off;
1778 off += in.got->getEntries().size();
1779 if (in.authGot) {
1780 in.authGot->reserved1 = off;
1781 off += in.authGot->getEntries().size();
1782 }
1783 in.stubs->reserved1 = off;
1784 if (in.lazyPointers) {
1785 off += in.stubs->getEntries().size();
1786 in.lazyPointers->reserved1 = off;
1787 }
1788}
1789
1790static uint32_t indirectValue(const Symbol *sym) {
1791 if (sym->symtabIndex == UINT32_MAX || !needsBinding(sym))
1792 return INDIRECT_SYMBOL_LOCAL;
1793 return sym->symtabIndex;
1794}
1795
1796void IndirectSymtabSection::writeTo(uint8_t *buf) const {
1797 uint32_t off = 0;
1798 for (const Symbol *sym : in.got->getEntries()) {
1799 write32le(P: buf + off * sizeof(uint32_t), V: indirectValue(sym));
1800 ++off;
1801 }
1802 if (in.authGot) {
1803 for (const Symbol *sym : in.authGot->getEntries()) {
1804 write32le(P: buf + off * sizeof(uint32_t), V: indirectValue(sym));
1805 ++off;
1806 }
1807 }
1808 for (const Symbol *sym : in.stubs->getEntries()) {
1809 write32le(P: buf + off * sizeof(uint32_t), V: indirectValue(sym));
1810 ++off;
1811 }
1812
1813 if (in.lazyPointers) {
1814 // There is a 1:1 correspondence between stubs and LazyPointerSection
1815 // entries. But giving __stubs and __la_symbol_ptr the same reserved1
1816 // (the offset into the indirect symbol table) so that they both refer
1817 // to the same range of offsets confuses `strip`, so write the stubs
1818 // symbol table offsets a second time.
1819 for (const Symbol *sym : in.stubs->getEntries()) {
1820 write32le(P: buf + off * sizeof(uint32_t), V: indirectValue(sym));
1821 ++off;
1822 }
1823 }
1824}
1825
1826StringTableSection::StringTableSection()
1827 : LinkEditSection(segment_names::linkEdit, section_names::stringTable) {}
1828
1829uint32_t StringTableSection::addString(StringRef str) {
1830 uint32_t strx = size;
1831 if (config->dedupSymbolStrings) {
1832 llvm::CachedHashStringRef hashedStr(str);
1833 auto [it, inserted] = stringMap.try_emplace(Key: hashedStr, Args&: strx);
1834 if (!inserted)
1835 return it->second;
1836 }
1837
1838 strings.push_back(x: str);
1839 size += str.size() + 1; // account for null terminator
1840 return strx;
1841}
1842
1843void StringTableSection::writeTo(uint8_t *buf) const {
1844 uint32_t off = 0;
1845 for (StringRef str : strings) {
1846 memcpy(dest: buf + off, src: str.data(), n: str.size());
1847 off += str.size() + 1; // account for null terminator
1848 }
1849}
1850
1851static_assert((CodeSignatureSection::blobHeadersSize % 8) == 0);
1852static_assert((CodeSignatureSection::fixedHeadersSize % 8) == 0);
1853
1854CodeSignatureSection::CodeSignatureSection()
1855 : LinkEditSection(segment_names::linkEdit, section_names::codeSignature) {
1856 align = 16; // required by libstuff
1857
1858 // XXX: This mimics LD64, where it uses the install-name as codesign
1859 // identifier, if available.
1860 if (!config->installName.empty())
1861 fileName = config->installName;
1862 else
1863 // FIXME: Consider using finalOutput instead of outputFile.
1864 fileName = config->outputFile;
1865
1866 size_t slashIndex = fileName.rfind(Str: "/");
1867 if (slashIndex != std::string::npos)
1868 fileName = fileName.drop_front(N: slashIndex + 1);
1869
1870 // NOTE: Any changes to these calculations should be repeated
1871 // in llvm-objcopy's MachOLayoutBuilder::layoutTail.
1872 allHeadersSize = alignTo<16>(Value: fixedHeadersSize + fileName.size() + 1);
1873 fileNamePad = allHeadersSize - fixedHeadersSize - fileName.size();
1874}
1875
1876uint32_t CodeSignatureSection::getBlockCount() const {
1877 return (fileOff + blockSize - 1) / blockSize;
1878}
1879
1880uint64_t CodeSignatureSection::getRawSize() const {
1881 return allHeadersSize + getBlockCount() * hashSize;
1882}
1883
1884void CodeSignatureSection::writeHashes(uint8_t *buf) const {
1885 // NOTE: Changes to this functionality should be repeated in llvm-objcopy's
1886 // MachOWriter::writeSignatureData.
1887 uint8_t *hashes = buf + fileOff + allHeadersSize;
1888 parallelFor(Begin: 0, End: getBlockCount(), Fn: [&](size_t i) {
1889 sha256(data: buf + i * blockSize,
1890 len: std::min(a: static_cast<size_t>(fileOff - i * blockSize), b: blockSize),
1891 output: hashes + i * hashSize);
1892 });
1893#if defined(__APPLE__)
1894 // This is macOS-specific work-around and makes no sense for any
1895 // other host OS. See https://openradar.appspot.com/FB8914231
1896 //
1897 // The macOS kernel maintains a signature-verification cache to
1898 // quickly validate applications at time of execve(2). The trouble
1899 // is that for the kernel creates the cache entry at the time of the
1900 // mmap(2) call, before we have a chance to write either the code to
1901 // sign or the signature header+hashes. The fix is to invalidate
1902 // all cached data associated with the output file, thus discarding
1903 // the bogus prematurely-cached signature.
1904 msync(buf, fileOff + getSize(), MS_INVALIDATE);
1905#endif
1906}
1907
1908void CodeSignatureSection::writeTo(uint8_t *buf) const {
1909 // NOTE: Changes to this functionality should be repeated in llvm-objcopy's
1910 // MachOWriter::writeSignatureData.
1911 uint32_t signatureSize = static_cast<uint32_t>(getSize());
1912 auto *superBlob = reinterpret_cast<CS_SuperBlob *>(buf);
1913 write32be(P: &superBlob->magic, V: CSMAGIC_EMBEDDED_SIGNATURE);
1914 write32be(P: &superBlob->length, V: signatureSize);
1915 write32be(P: &superBlob->count, V: 1);
1916 auto *blobIndex = reinterpret_cast<CS_BlobIndex *>(&superBlob[1]);
1917 write32be(P: &blobIndex->type, V: CSSLOT_CODEDIRECTORY);
1918 write32be(P: &blobIndex->offset, V: blobHeadersSize);
1919 auto *codeDirectory =
1920 reinterpret_cast<CS_CodeDirectory *>(buf + blobHeadersSize);
1921 write32be(P: &codeDirectory->magic, V: CSMAGIC_CODEDIRECTORY);
1922 write32be(P: &codeDirectory->length, V: signatureSize - blobHeadersSize);
1923 write32be(P: &codeDirectory->version, V: CS_SUPPORTSEXECSEG);
1924 write32be(P: &codeDirectory->flags, V: CS_ADHOC | CS_LINKER_SIGNED);
1925 write32be(P: &codeDirectory->hashOffset,
1926 V: sizeof(CS_CodeDirectory) + fileName.size() + fileNamePad);
1927 write32be(P: &codeDirectory->identOffset, V: sizeof(CS_CodeDirectory));
1928 codeDirectory->nSpecialSlots = 0;
1929 write32be(P: &codeDirectory->nCodeSlots, V: getBlockCount());
1930 write32be(P: &codeDirectory->codeLimit, V: fileOff);
1931 codeDirectory->hashSize = static_cast<uint8_t>(hashSize);
1932 codeDirectory->hashType = kSecCodeSignatureHashSHA256;
1933 codeDirectory->platform = 0;
1934 codeDirectory->pageSize = blockSizeShift;
1935 codeDirectory->spare2 = 0;
1936 codeDirectory->scatterOffset = 0;
1937 codeDirectory->teamOffset = 0;
1938 codeDirectory->spare3 = 0;
1939 codeDirectory->codeLimit64 = 0;
1940 OutputSegment *textSeg = getOrCreateOutputSegment(name: segment_names::text);
1941 write64be(P: &codeDirectory->execSegBase, V: textSeg->fileOff);
1942 write64be(P: &codeDirectory->execSegLimit, V: textSeg->fileSize);
1943 write64be(P: &codeDirectory->execSegFlags,
1944 V: config->outputType == MH_EXECUTE ? CS_EXECSEG_MAIN_BINARY : 0);
1945 auto *id = reinterpret_cast<char *>(&codeDirectory[1]);
1946 memcpy(dest: id, src: fileName.begin(), n: fileName.size());
1947 memset(s: id + fileName.size(), c: 0, n: fileNamePad);
1948}
1949
1950CStringSection::CStringSection(const char *name)
1951 : SyntheticSection(segment_names::text, name) {
1952 flags = S_CSTRING_LITERALS;
1953}
1954
1955void CStringSection::addInput(CStringInputSection *isec) {
1956 isec->parent = this;
1957 inputs.push_back(x: isec);
1958 if (isec->align > align)
1959 align = isec->align;
1960}
1961
1962void CStringSection::writeTo(uint8_t *buf) const {
1963 for (const CStringInputSection *isec : inputs) {
1964 for (const auto &[i, piece] : llvm::enumerate(First: isec->pieces)) {
1965 if (!piece.live)
1966 continue;
1967 StringRef string = isec->getStringRef(i);
1968 memcpy(dest: buf + piece.outSecOff, src: string.data(), n: string.size());
1969 }
1970 }
1971}
1972
1973// In contrast to ELF, which puts strings that need different alignments into
1974// different sections, clang's Mach-O backend puts them all in one section.
1975// Strings that need to be aligned have the .p2align directive emitted before
1976// them, which simply translates into zero padding in the object file. In other
1977// words, we have to infer the desired alignment of these cstrings from their
1978// addresses.
1979//
1980// We differ slightly from ld64 in how we've chosen to align these cstrings.
1981// Both LLD and ld64 preserve the number of trailing zeros in each cstring's
1982// address in the input object files. When deduplicating identical cstrings,
1983// both linkers pick the cstring whose address has more trailing zeros, and
1984// preserve the alignment of that address in the final binary. However, ld64
1985// goes a step further and also preserves the offset of the cstring from the
1986// last section-aligned address. I.e. if a cstring is at offset 18 in the
1987// input, with a section alignment of 16, then both LLD and ld64 will ensure the
1988// final address is 2-byte aligned (since 18 == 16 + 2). But ld64 will also
1989// ensure that the final address is of the form 16 * k + 2 for some k.
1990//
1991// Note that ld64's heuristic means that a dedup'ed cstring's final address is
1992// dependent on the order of the input object files. E.g. if in addition to the
1993// cstring at offset 18 above, we have a duplicate one in another file with a
1994// `.cstring` section alignment of 2 and an offset of zero, then ld64 will pick
1995// the cstring from the object file earlier on the command line (since both have
1996// the same number of trailing zeros in their address). So the final cstring may
1997// either be at some address `16 * k + 2` or at some address `2 * k`.
1998//
1999// I've opted not to follow this behavior primarily for implementation
2000// simplicity, and secondarily to save a few more bytes. It's not clear to me
2001// that preserving the section alignment + offset is ever necessary, and there
2002// are many cases that are clearly redundant. In particular, if an x86_64 object
2003// file contains some strings that are accessed via SIMD instructions, then the
2004// .cstring section in the object file will be 16-byte-aligned (since SIMD
2005// requires its operand addresses to be 16-byte aligned). However, there will
2006// typically also be other cstrings in the same file that aren't used via SIMD
2007// and don't need this alignment. They will be emitted at some arbitrary address
2008// `A`, but ld64 will treat them as being 16-byte aligned with an offset of
2009// `16 % A`.
2010static Align getStringPieceAlignment(const CStringInputSection &isec,
2011 const StringPiece &piece) {
2012 return llvm::Align::fromLog2(Log2Value: llvm::countr_zero(Val: isec.align | piece.inSecOff));
2013}
2014
2015void CStringSection::finalizeContents() {
2016 size = 0;
2017 priorityBuilder.forEachStringPiece(
2018 inputs,
2019 f: [&](CStringInputSection &isec, StringPiece &piece, size_t pieceIdx) {
2020 piece.outSecOff = alignTo(Size: size, A: getStringPieceAlignment(isec, piece));
2021 StringRef string = isec.getStringRef(i: pieceIdx);
2022 size =
2023 piece.outSecOff + string.size() + 1; // account for null terminator
2024 },
2025 /*forceInputOrder=*/false, /*computeHash=*/true);
2026 for (CStringInputSection *isec : inputs)
2027 isec->isFinal = true;
2028}
2029
2030void DeduplicatedCStringSection::finalizeContents() {
2031 // Find the largest alignment required for each string.
2032 DenseMap<CachedHashStringRef, Align> strToAlignment;
2033 // Used for tail merging only
2034 std::vector<CachedHashStringRef> deduplicatedStrs;
2035 priorityBuilder.forEachStringPiece(
2036 inputs,
2037 f: [&](CStringInputSection &isec, StringPiece &piece, size_t pieceIdx) {
2038 auto s = isec.getCachedHashStringRef(i: pieceIdx);
2039 assert(isec.align != 0);
2040 auto align = getStringPieceAlignment(isec, piece);
2041 auto [it, wasInserted] = strToAlignment.try_emplace(Key: s, Args&: align);
2042 if (config->tailMergeStrings && wasInserted)
2043 deduplicatedStrs.push_back(x: s);
2044 if (!wasInserted && it->second < align)
2045 it->second = align;
2046 },
2047 /*forceInputOrder=*/true);
2048
2049 // Like lexicographical sort, except we read strings in reverse and take the
2050 // longest string first
2051 // TODO: We could improve performance by implementing our own sort that avoids
2052 // comparing characters we know to be the same. See
2053 // StringTableBuilder::multikeySort() for details
2054 llvm::sort(C&: deduplicatedStrs, Comp: [](const auto &left, const auto &right) {
2055 for (const auto &[leftChar, rightChar] :
2056 llvm::zip(llvm::reverse(left.val()), llvm::reverse(right.val()))) {
2057 if (leftChar == rightChar)
2058 continue;
2059 return leftChar < rightChar;
2060 }
2061 return left.size() > right.size();
2062 });
2063 std::optional<CachedHashStringRef> mergeCandidate;
2064 DenseMap<CachedHashStringRef, std::pair<CachedHashStringRef, uint64_t>>
2065 tailMergeMap;
2066 for (auto &s : deduplicatedStrs) {
2067 if (!mergeCandidate || !mergeCandidate->val().ends_with(Suffix: s.val())) {
2068 mergeCandidate = s;
2069 continue;
2070 }
2071 uint64_t tailMergeOffset = mergeCandidate->size() - s.size();
2072 // TODO: If the tail offset is incompatible with this string's alignment, we
2073 // might be able to find another superstring with a compatible tail offset.
2074 // The difficulty is how to do this efficiently
2075 const auto &align = strToAlignment.at(Val: s);
2076 if (!isAligned(Lhs: align, SizeInBytes: tailMergeOffset))
2077 continue;
2078 auto &mergeCandidateAlign = strToAlignment[*mergeCandidate];
2079 if (align > mergeCandidateAlign)
2080 mergeCandidateAlign = align;
2081 tailMergeMap.try_emplace(Key: s, Args&: *mergeCandidate, Args&: tailMergeOffset);
2082 }
2083
2084 // Sort the strings for performance and compression size win, and then
2085 // assign an offset for each string and save it to the corresponding
2086 // StringPieces for easy access.
2087 priorityBuilder.forEachStringPiece(inputs, f: [&](CStringInputSection &isec,
2088 StringPiece &piece,
2089 size_t pieceIdx) {
2090 auto s = isec.getCachedHashStringRef(i: pieceIdx);
2091 // Any string can be tail merged with itself with an offset of zero
2092 uint64_t tailMergeOffset = 0;
2093 auto mergeIt =
2094 config->tailMergeStrings ? tailMergeMap.find(Val: s) : tailMergeMap.end();
2095 if (mergeIt != tailMergeMap.end()) {
2096 auto &[superString, offset] = mergeIt->second;
2097 // s can be tail merged with superString. Do not layout s. Instead layout
2098 // superString if we haven't already
2099 assert(superString.val().ends_with(s.val()));
2100 s = superString;
2101 tailMergeOffset = offset;
2102 }
2103 auto [it, wasInserted] = stringOffsetMap.try_emplace(Key: s, /*placeholder*/ Args: 0);
2104 if (wasInserted) {
2105 // Avoid computing the offset until we are sure we will need to
2106 uint64_t offset = alignTo(Size: size, A: strToAlignment.at(Val: s));
2107 it->second = offset;
2108 size = offset + s.size() + 1; // account for null terminator
2109 }
2110 piece.outSecOff = it->second + tailMergeOffset;
2111 if (mergeIt != tailMergeMap.end()) {
2112 auto &tailMergedString = mergeIt->first;
2113 stringOffsetMap[tailMergedString] = piece.outSecOff;
2114 assert(isAligned(strToAlignment.at(tailMergedString), piece.outSecOff));
2115 }
2116 });
2117 for (CStringInputSection *isec : inputs)
2118 isec->isFinal = true;
2119}
2120
2121void DeduplicatedCStringSection::writeTo(uint8_t *buf) const {
2122 for (const auto &[s, outSecOff] : stringOffsetMap)
2123 if (s.size())
2124 memcpy(dest: buf + outSecOff, src: s.data(), n: s.size());
2125}
2126
2127uint64_t DeduplicatedCStringSection::getStringOffset(StringRef str) const {
2128 // StringPiece uses 31 bits to store the hashes, so we replicate that
2129 uint32_t hash = xxh3_64bits(data: str) & 0x7fffffff;
2130 return stringOffsetMap.at(Val: CachedHashStringRef(str, hash));
2131}
2132
2133// This section is actually emitted as __TEXT,__const by ld64, but clang may
2134// emit input sections of that name, and LLD doesn't currently support mixing
2135// synthetic and concat-type OutputSections. To work around this, I've given
2136// our merged-literals section a different name.
2137WordLiteralSection::WordLiteralSection()
2138 : SyntheticSection(segment_names::text, section_names::literals) {
2139 align = 16;
2140}
2141
2142void WordLiteralSection::addInput(WordLiteralInputSection *isec) {
2143 isec->parent = this;
2144 inputs.push_back(x: isec);
2145}
2146
2147void WordLiteralSection::finalizeContents() {
2148 for (WordLiteralInputSection *isec : inputs) {
2149 // We do all processing of the InputSection here, so it will be effectively
2150 // finalized.
2151 isec->isFinal = true;
2152 const uint8_t *buf = isec->data.data();
2153 switch (sectionType(flags: isec->getFlags())) {
2154 case S_4BYTE_LITERALS: {
2155 for (size_t off = 0, e = isec->data.size(); off < e; off += 4) {
2156 if (!isec->isLive(off))
2157 continue;
2158 uint32_t value = *reinterpret_cast<const uint32_t *>(buf + off);
2159 literal4Map.try_emplace(Key: value, Args: literal4Map.size());
2160 }
2161 break;
2162 }
2163 case S_8BYTE_LITERALS: {
2164 for (size_t off = 0, e = isec->data.size(); off < e; off += 8) {
2165 if (!isec->isLive(off))
2166 continue;
2167 uint64_t value = *reinterpret_cast<const uint64_t *>(buf + off);
2168 literal8Map.try_emplace(Key: value, Args: literal8Map.size());
2169 }
2170 break;
2171 }
2172 case S_16BYTE_LITERALS: {
2173 for (size_t off = 0, e = isec->data.size(); off < e; off += 16) {
2174 if (!isec->isLive(off))
2175 continue;
2176 UInt128 value = *reinterpret_cast<const UInt128 *>(buf + off);
2177 literal16Map.try_emplace(Key: value, Args: literal16Map.size());
2178 }
2179 break;
2180 }
2181 default:
2182 llvm_unreachable("invalid literal section type");
2183 }
2184 }
2185}
2186
2187void WordLiteralSection::writeTo(uint8_t *buf) const {
2188 // Note that we don't attempt to do any endianness conversion in addInput(),
2189 // so we don't do it here either -- just write out the original value,
2190 // byte-for-byte.
2191 for (const auto &p : literal16Map)
2192 memcpy(dest: buf + p.second * 16, src: &p.first, n: 16);
2193 buf += literal16Map.size() * 16;
2194
2195 for (const auto &p : literal8Map)
2196 memcpy(dest: buf + p.second * 8, src: &p.first, n: 8);
2197 buf += literal8Map.size() * 8;
2198
2199 for (const auto &p : literal4Map)
2200 memcpy(dest: buf + p.second * 4, src: &p.first, n: 4);
2201}
2202
2203ObjCImageInfoSection::ObjCImageInfoSection()
2204 : SyntheticSection(segment_names::data, section_names::objCImageInfo) {}
2205
2206ObjCImageInfoSection::ImageInfo
2207ObjCImageInfoSection::parseImageInfo(const InputFile *file) {
2208 ImageInfo info;
2209 ArrayRef<uint8_t> data = file->objCImageInfo;
2210 // The image info struct has the following layout:
2211 // struct {
2212 // uint32_t version;
2213 // uint32_t flags;
2214 // };
2215 if (data.size() < 8) {
2216 warn(msg: toString(file) + ": invalid __objc_imageinfo size");
2217 return info;
2218 }
2219
2220 auto *buf = reinterpret_cast<const uint32_t *>(data.data());
2221 if (read32le(P: buf) != 0) {
2222 warn(msg: toString(file) + ": invalid __objc_imageinfo version");
2223 return info;
2224 }
2225
2226 uint32_t flags = read32le(P: buf + 1);
2227 info.swiftVersion = (flags >> 8) & 0xff;
2228 info.hasCategoryClassProperties = flags & 0x40;
2229 info.hasSignedClassROs = flags & 0x10;
2230 return info;
2231}
2232
2233static std::string swiftVersionString(uint8_t version) {
2234 switch (version) {
2235 case 1:
2236 return "1.0";
2237 case 2:
2238 return "1.1";
2239 case 3:
2240 return "2.0";
2241 case 4:
2242 return "3.0";
2243 case 5:
2244 return "4.0";
2245 default:
2246 return ("0x" + Twine::utohexstr(Val: version)).str();
2247 }
2248}
2249
2250// ld-prime treats an input with __objc_classlist entries as defining classes.
2251static bool definesObjCClasses(const InputFile *file) {
2252 for (const Section *sec : file->sections)
2253 if (sec->name == section_names::objcClassList)
2254 for (const Subsection &subsec : sec->subsections)
2255 if (!subsec.isec->empty())
2256 return true;
2257 return false;
2258}
2259
2260// Validate each object file's __objc_imageinfo and use them to generate the
2261// image info for the output binary. Only three pieces of info are relevant:
2262// 1. The Swift version (should be identical across inputs)
2263// 2. `bool hasCategoryClassProperties` (true only if true for all inputs)
2264// 3. `bool hasSignedClassROs` (true if some input has it and every input that
2265// defines classes does; an input without classes has no class_ro_t)
2266void ObjCImageInfoSection::finalizeContents() {
2267 assert(files.size() != 0); // should have already been checked via isNeeded()
2268
2269 info.hasCategoryClassProperties = true;
2270 // Like ld-prime, warn about a signed input if an earlier input that defines
2271 // classes was unsigned, and about an unsigned input that defines classes if
2272 // all earlier ones were signed. Once an input that defines classes is
2273 // unsigned, so is the output.
2274 enum class ClassROSigning { Unknown, Signed, Unsigned };
2275 ClassROSigning classROSigning = ClassROSigning::Unknown;
2276 bool anySignedClassROs = false;
2277 const InputFile *firstFile;
2278 for (const InputFile *file : files) {
2279 ImageInfo inputInfo = parseImageInfo(file);
2280 info.hasCategoryClassProperties &= inputInfo.hasCategoryClassProperties;
2281
2282 if (inputInfo.hasSignedClassROs) {
2283 anySignedClassROs = true;
2284 if (classROSigning == ClassROSigning::Unsigned)
2285 warn(msg: "'" + toString(file) +
2286 "' was built with class_ro_t pointer signing enabled, but "
2287 "previous .o file was not");
2288 else if (definesObjCClasses(file))
2289 classROSigning = ClassROSigning::Signed;
2290 } else if (definesObjCClasses(file)) {
2291 if (classROSigning == ClassROSigning::Signed)
2292 warn(msg: "'" + toString(file) +
2293 "' was not built with class_ro_t pointer signing enabled, but "
2294 "previous .o file was");
2295 classROSigning = ClassROSigning::Unsigned;
2296 }
2297
2298 // swiftVersion 0 means no Swift is present, so no version checking required
2299 if (inputInfo.swiftVersion == 0)
2300 continue;
2301
2302 if (info.swiftVersion != 0 && info.swiftVersion != inputInfo.swiftVersion) {
2303 error(msg: "Swift version mismatch: " + toString(file: firstFile) + " has version " +
2304 swiftVersionString(version: info.swiftVersion) + " but " + toString(file) +
2305 " has version " + swiftVersionString(version: inputInfo.swiftVersion));
2306 } else {
2307 info.swiftVersion = inputInfo.swiftVersion;
2308 firstFile = file;
2309 }
2310 }
2311 info.hasSignedClassROs =
2312 anySignedClassROs && classROSigning != ClassROSigning::Unsigned;
2313}
2314
2315void ObjCImageInfoSection::writeTo(uint8_t *buf) const {
2316 uint32_t flags = info.hasCategoryClassProperties ? 0x40 : 0x0;
2317 if (info.hasSignedClassROs)
2318 flags |= 0x10;
2319 flags |= info.swiftVersion << 8;
2320 write32le(P: buf + 4, V: flags);
2321}
2322
2323InitOffsetsSection::InitOffsetsSection()
2324 : SyntheticSection(segment_names::text, section_names::initOffsets) {
2325 flags = S_INIT_FUNC_OFFSETS;
2326 align = 4; // This section contains 32-bit integers.
2327}
2328
2329uint64_t InitOffsetsSection::getSize() const {
2330 size_t count = 0;
2331 for (const ConcatInputSection *isec : sections)
2332 count += isec->relocs.size();
2333 return count * sizeof(uint32_t);
2334}
2335
2336void InitOffsetsSection::writeTo(uint8_t *buf) const {
2337 // FIXME: Add function specified by -init when that argument is implemented.
2338 for (ConcatInputSection *isec : sections) {
2339 for (const Relocation &rel : isec->relocs) {
2340 const Symbol *referent = cast<Symbol *>(Val: rel.referent);
2341 assert(referent && "section relocation should have been rejected");
2342 uint64_t offset = referent->getVA() - in.header->addr;
2343 // FIXME: Can we handle this gracefully?
2344 if (offset > UINT32_MAX)
2345 fatal(msg: isec->getLocation(off: rel.offset) + ": offset to initializer " +
2346 referent->getName() + " (" + utohexstr(X: offset) +
2347 ") does not fit in 32 bits");
2348
2349 // Entries need to be added in the order they appear in the section, but
2350 // relocations aren't guaranteed to be sorted.
2351 size_t index = rel.offset >> target->p2WordSize;
2352 write32le(P: &buf[index * sizeof(uint32_t)], V: offset);
2353 }
2354 buf += isec->relocs.size() * sizeof(uint32_t);
2355 }
2356}
2357
2358// The inputs are __mod_init_func sections, which contain pointers to
2359// initializer functions, therefore all relocations should be of the UNSIGNED
2360// type. InitOffsetsSection stores offsets, so if the initializer's address is
2361// not known at link time, stub-indirection has to be used.
2362void InitOffsetsSection::setUp() {
2363 for (const ConcatInputSection *isec : sections) {
2364 for (const Relocation &rel : isec->relocs) {
2365 RelocAttrs attrs = target->getRelocAttrs(type: rel.type);
2366 if (!attrs.hasAttr(b: RelocAttrBits::UNSIGNED))
2367 error(msg: isec->getLocation(off: rel.offset) +
2368 ": unsupported relocation type: " + attrs.name);
2369 if (rel.getAddend() != 0)
2370 error(msg: isec->getLocation(off: rel.offset) +
2371 ": relocation addend is not representable in __init_offsets");
2372 if (isa<InputSection *>(Val: rel.referent))
2373 error(msg: isec->getLocation(off: rel.offset) +
2374 ": unexpected section relocation");
2375
2376 Symbol *sym = dyn_cast<Symbol *>(Val: rel.referent);
2377 if (auto *undefined = dyn_cast<Undefined>(Val: sym))
2378 treatUndefinedSymbol(*undefined, isec, offset: rel.offset);
2379 if (needsBinding(sym))
2380 in.stubs->addEntry(sym);
2381 }
2382 }
2383}
2384
2385ObjCMethListSection::ObjCMethListSection()
2386 : SyntheticSection(segment_names::text, section_names::objcMethList) {
2387 flags = S_ATTR_NO_DEAD_STRIP;
2388 align = relativeOffsetSize;
2389}
2390
2391// Go through all input method lists and ensure that we have selrefs for all
2392// their method names. The selrefs will be needed later by ::writeTo. We need to
2393// create them early on here to ensure they are processed correctly by the lld
2394// pipeline.
2395void ObjCMethListSection::setUp() {
2396 llvm::erase_if(C&: inputs, P: [](const ConcatInputSection *isec) {
2397 return isec->shouldOmitFromOutput();
2398 });
2399 for (const ConcatInputSection *isec : inputs) {
2400 uint32_t structSizeAndFlags = 0, structCount = 0;
2401 readMethodListHeader(buf: isec->data.data(), structSizeAndFlags, structCount);
2402 uint32_t originalStructSize = structSizeAndFlags & structSizeMask;
2403 // Method name is immediately after header
2404 uint32_t methodNameOff = methodListHeaderSize;
2405
2406 // Loop through all methods, and ensure a selref for each of them exists.
2407 while (methodNameOff < isec->data.size()) {
2408 const Relocation *reloc = isec->getRelocAt(off: methodNameOff);
2409 assert(reloc && "Relocation expected at method list name slot");
2410
2411 StringRef methname = reloc->getReferentString();
2412 if (!ObjCSelRefsHelper::getSelRef(methname))
2413 ObjCSelRefsHelper::makeSelRef(methname);
2414
2415 // Jump to method name offset in next struct
2416 methodNameOff += originalStructSize;
2417 }
2418 }
2419}
2420
2421// Calculate section size and final offsets for where InputSection's need to be
2422// written.
2423void ObjCMethListSection::finalize() {
2424 // sectionSize will be the total size of the __objc_methlist section
2425 sectionSize = 0;
2426 for (ConcatInputSection *isec : inputs) {
2427 // We can also use sectionSize as write offset for isec
2428 assert(sectionSize == alignToPowerOf2(sectionSize, relativeOffsetSize) &&
2429 "expected __objc_methlist to be aligned by default with the "
2430 "required section alignment");
2431 isec->outSecOff = sectionSize;
2432
2433 isec->isFinal = true;
2434 uint32_t relativeListSize =
2435 computeRelativeMethodListSize(absoluteMethodListSize: isec->data.size());
2436 sectionSize += relativeListSize;
2437
2438 // If encoding the method list in relative offset format shrinks the size,
2439 // then we also need to adjust symbol sizes to match the new size. Note that
2440 // on 32bit platforms the size of the method list will remain the same when
2441 // encoded in relative offset format.
2442 if (relativeListSize != isec->data.size()) {
2443 for (Symbol *sym : isec->symbols) {
2444 assert(isa<Defined>(sym) &&
2445 "Unexpected undefined symbol in ObjC method list");
2446 auto *def = cast<Defined>(Val: sym);
2447 // There can be 0-size symbols, check if this is the case and ignore
2448 // them.
2449 if (def->size) {
2450 assert(
2451 def->size == isec->data.size() &&
2452 "Invalid ObjC method list symbol size: expected symbol size to "
2453 "match isec size");
2454 def->size = relativeListSize;
2455 }
2456 }
2457 }
2458 }
2459}
2460
2461void ObjCMethListSection::writeTo(uint8_t *bufStart) const {
2462 uint8_t *buf = bufStart;
2463 for (const ConcatInputSection *isec : inputs) {
2464 assert(!isec->shouldOmitFromOutput() &&
2465 "dead method list should have been dropped in setUp()");
2466 assert(buf - bufStart == std::ptrdiff_t(isec->outSecOff) &&
2467 "Writing at unexpected offset");
2468 uint32_t writtenSize = writeRelativeMethodList(isec, buf);
2469 buf += writtenSize;
2470 }
2471 assert(buf - bufStart == std::ptrdiff_t(sectionSize) &&
2472 "Written size does not match expected section size");
2473}
2474
2475// Check if an InputSection is a method list. To do this we scan the
2476// InputSection for any symbols who's names match the patterns we expect clang
2477// to generate for method lists.
2478bool ObjCMethListSection::isMethodList(const ConcatInputSection *isec) {
2479 const char *symPrefixes[] = {objc::symbol_names::classMethods,
2480 objc::symbol_names::instanceMethods,
2481 objc::symbol_names::categoryInstanceMethods,
2482 objc::symbol_names::categoryClassMethods};
2483 if (!isec)
2484 return false;
2485 for (const Symbol *sym : isec->symbols) {
2486 auto *def = dyn_cast_or_null<Defined>(Val: sym);
2487 if (!def)
2488 continue;
2489 for (const char *prefix : symPrefixes) {
2490 if (def->getName().starts_with(Prefix: prefix)) {
2491 assert(def->size == isec->data.size() &&
2492 "Invalid ObjC method list symbol size: expected symbol size to "
2493 "match isec size");
2494 assert(def->value == 0 &&
2495 "Offset of ObjC method list symbol must be 0");
2496 return true;
2497 }
2498 }
2499 }
2500
2501 return false;
2502}
2503
2504// Encode a single relative offset value. The input is the data/symbol at
2505// (&isec->data[inSecOff]). The output is written to (&buf[outSecOff]).
2506// 'createSelRef' indicates that we should not directly use the specified
2507// symbol, but instead get the selRef for the symbol and use that instead.
2508void ObjCMethListSection::writeRelativeOffsetForIsec(
2509 const ConcatInputSection *isec, uint8_t *buf, uint32_t &inSecOff,
2510 uint32_t &outSecOff, bool useSelRef) const {
2511 const Relocation *reloc = isec->getRelocAt(off: inSecOff);
2512 assert(reloc && "Relocation expected at __objc_methlist Offset");
2513
2514 uint32_t symVA = 0;
2515 if (useSelRef) {
2516 StringRef methname = reloc->getReferentString();
2517 ConcatInputSection *selRef = ObjCSelRefsHelper::getSelRef(methname);
2518 assert(selRef && "Expected all selector names to already be already be "
2519 "present in __objc_selrefs");
2520 symVA = selRef->getVA();
2521 assert(selRef->data.size() == target->wordSize &&
2522 "Expected one selref per ConcatInputSection");
2523 } else if (auto *sym = dyn_cast<Symbol *>(Val: reloc->referent)) {
2524 auto *def = dyn_cast_or_null<Defined>(Val: sym);
2525 assert(def && "Expected all syms in __objc_methlist to be defined");
2526 symVA = def->getVA();
2527 } else {
2528 auto *isec = cast<InputSection *>(Val: reloc->referent);
2529 symVA = isec->getVA(off: reloc->getAddend());
2530 }
2531
2532 uint32_t currentVA = isec->getVA() + outSecOff;
2533 uint32_t delta = symVA - currentVA;
2534 write32le(P: buf + outSecOff, V: delta);
2535
2536 // Move one pointer forward in the absolute method list
2537 inSecOff += target->wordSize;
2538 // Move one relative offset forward in the relative method list (32 bits)
2539 outSecOff += relativeOffsetSize;
2540}
2541
2542// Write a relative method list to buf, return the size of the written
2543// information
2544uint32_t
2545ObjCMethListSection::writeRelativeMethodList(const ConcatInputSection *isec,
2546 uint8_t *buf) const {
2547 // Copy over the header, and add the "this is a relative method list" magic
2548 // value flag
2549 uint32_t structSizeAndFlags = 0, structCount = 0;
2550 readMethodListHeader(buf: isec->data.data(), structSizeAndFlags, structCount);
2551 // Set the struct size for the relative method list
2552 uint32_t relativeStructSizeAndFlags =
2553 (relativeOffsetSize * pointersPerStruct) & structSizeMask;
2554 // Carry over the old flags from the input struct
2555 relativeStructSizeAndFlags |= structSizeAndFlags & structFlagsMask;
2556 // Set the relative method list flag
2557 relativeStructSizeAndFlags |= relMethodHeaderFlag;
2558
2559 writeMethodListHeader(buf, structSizeAndFlags: relativeStructSizeAndFlags, structCount);
2560
2561 assert(methodListHeaderSize +
2562 (structCount * pointersPerStruct * target->wordSize) ==
2563 isec->data.size() &&
2564 "Invalid computed ObjC method list size");
2565
2566 uint32_t inSecOff = methodListHeaderSize;
2567 uint32_t outSecOff = methodListHeaderSize;
2568
2569 // Go through the method list and encode input absolute pointers as relative
2570 // offsets. writeRelativeOffsetForIsec will be incrementing inSecOff and
2571 // outSecOff
2572 for (uint32_t i = 0; i < structCount; i++) {
2573 // Write the name of the method
2574 writeRelativeOffsetForIsec(isec, buf, inSecOff, outSecOff, useSelRef: true);
2575 // Write the type of the method
2576 writeRelativeOffsetForIsec(isec, buf, inSecOff, outSecOff, useSelRef: false);
2577 // Write reference to the selector of the method
2578 writeRelativeOffsetForIsec(isec, buf, inSecOff, outSecOff, useSelRef: false);
2579 }
2580
2581 // Expecting to have read all the data in the isec
2582 assert(inSecOff == isec->data.size() &&
2583 "Invalid actual ObjC method list size");
2584 assert(
2585 outSecOff == computeRelativeMethodListSize(inSecOff) &&
2586 "Mismatch between input & output size when writing relative method list");
2587 return outSecOff;
2588}
2589
2590// Given the size of an ObjC method list InputSection, return the size of the
2591// method list when encoded in relative offsets format. We can do this without
2592// decoding the actual data, as it can be directly inferred from the size of the
2593// isec.
2594uint32_t ObjCMethListSection::computeRelativeMethodListSize(
2595 uint32_t absoluteMethodListSize) const {
2596 uint32_t oldPointersSize = absoluteMethodListSize - methodListHeaderSize;
2597 uint32_t pointerCount = oldPointersSize / target->wordSize;
2598 assert(((pointerCount % pointersPerStruct) == 0) &&
2599 "__objc_methlist expects method lists to have multiple-of-3 pointers");
2600
2601 uint32_t newPointersSize = pointerCount * relativeOffsetSize;
2602 uint32_t newTotalSize = methodListHeaderSize + newPointersSize;
2603
2604 assert((newTotalSize <= absoluteMethodListSize) &&
2605 "Expected relative method list size to be smaller or equal than "
2606 "original size");
2607 return newTotalSize;
2608}
2609
2610// Read a method list header from buf
2611void ObjCMethListSection::readMethodListHeader(const uint8_t *buf,
2612 uint32_t &structSizeAndFlags,
2613 uint32_t &structCount) const {
2614 structSizeAndFlags = read32le(P: buf);
2615 structCount = read32le(P: buf + sizeof(uint32_t));
2616}
2617
2618// Write a method list header to buf
2619void ObjCMethListSection::writeMethodListHeader(uint8_t *buf,
2620 uint32_t structSizeAndFlags,
2621 uint32_t structCount) const {
2622 write32le(P: buf, V: structSizeAndFlags);
2623 write32le(P: buf + sizeof(structSizeAndFlags), V: structCount);
2624}
2625
2626void macho::createSyntheticSymbols() {
2627 auto addHeaderSymbol = [](const char *name) {
2628 symtab->addSynthetic(name, in.header->isec, /*value=*/0,
2629 /*isPrivateExtern=*/true, /*includeInSymtab=*/false,
2630 /*referencedDynamically=*/false);
2631 };
2632
2633 switch (config->outputType) {
2634 // FIXME: Assign the right address value for these symbols
2635 // (rather than 0). But we need to do that after assignAddresses().
2636 case MH_EXECUTE:
2637 // If linking PIE, __mh_execute_header is a defined symbol in
2638 // __TEXT, __text)
2639 // Otherwise, it's an absolute symbol.
2640 if (config->isPic)
2641 symtab->addSynthetic(name: "__mh_execute_header", in.header->isec, /*value=*/0,
2642 /*isPrivateExtern=*/false, /*includeInSymtab=*/true,
2643 /*referencedDynamically=*/true);
2644 else
2645 symtab->addSynthetic(name: "__mh_execute_header", /*isec=*/nullptr, /*value=*/0,
2646 /*isPrivateExtern=*/false, /*includeInSymtab=*/true,
2647 /*referencedDynamically=*/true);
2648 break;
2649
2650 // The following symbols are N_SECT symbols, even though the header is not
2651 // part of any section and that they are private to the bundle/dylib/object
2652 // they are part of.
2653 case MH_BUNDLE:
2654 addHeaderSymbol("__mh_bundle_header");
2655 break;
2656 case MH_DYLIB:
2657 addHeaderSymbol("__mh_dylib_header");
2658 break;
2659 case MH_DYLINKER:
2660 addHeaderSymbol("__mh_dylinker_header");
2661 break;
2662 case MH_OBJECT:
2663 addHeaderSymbol("__mh_object_header");
2664 break;
2665 default:
2666 llvm_unreachable("unexpected outputType");
2667 break;
2668 }
2669
2670 // The Itanium C++ ABI requires dylibs to pass a pointer to __cxa_atexit
2671 // which does e.g. cleanup of static global variables. The ABI document
2672 // says that the pointer can point to any address in one of the dylib's
2673 // segments, but in practice ld64 seems to set it to point to the header,
2674 // so that's what's implemented here.
2675 addHeaderSymbol("___dso_handle");
2676}
2677
2678ChainedFixupsSection::ChainedFixupsSection()
2679 : LinkEditSection(segment_names::linkEdit, section_names::chainFixups) {}
2680
2681bool ChainedFixupsSection::isNeeded() const {
2682 assert(config->emitChainedFixups);
2683 // dyld always expects LC_DYLD_CHAINED_FIXUPS to point to a valid
2684 // dyld_chained_fixups_header, so we create this section even if there aren't
2685 // any fixups.
2686 return true;
2687}
2688
2689// Whether a bind keeps its addend in its own addend field rather than in the
2690// imports table. The 19-bit addend field is two's complement, but Xcode's ld
2691// inlines only addends below 256 KiB in magnitude, and with
2692// DYLD_CHAINED_PTR_ARM64E no negative ones, which it rejects ("addend cannot
2693// fit in fixup"); we move those to the imports table instead.
2694static bool fitsInlineAddend(int64_t addend) {
2695 switch (config->chainedFixupsPointerFormat) {
2696 case DYLD_CHAINED_PTR_64:
2697 return isUInt<8>(x: addend);
2698 case DYLD_CHAINED_PTR_ARM64E:
2699 return addend >= 0 && addend < (1 << 18);
2700 case DYLD_CHAINED_PTR_ARM64E_USERLAND24:
2701 return addend > -(1 << 18) && addend < (1 << 18);
2702 }
2703 llvm_unreachable("unexpected chained fixup pointer format");
2704}
2705
2706void ChainedFixupsSection::addBinding(const Symbol *sym,
2707 const InputSection *isec, uint64_t offset,
2708 int64_t addend, bool forceOutline) {
2709 locations.emplace_back(args&: isec, args&: offset);
2710 int64_t outlineAddend =
2711 (forceOutline || !fitsInlineAddend(addend)) ? addend : 0;
2712 auto [it, inserted] = bindings.insert(
2713 KV: {{sym, outlineAddend}, static_cast<uint32_t>(bindings.size())});
2714
2715 if (inserted) {
2716 symtabSize += sym->getName().size() + 1;
2717 hasWeakBind = hasWeakBind || needsWeakBind(sym: *sym);
2718 if (!isInt<23>(x: outlineAddend))
2719 needsLargeAddend = true;
2720 else if (outlineAddend != 0)
2721 needsAddend = true;
2722 }
2723}
2724
2725std::pair<uint32_t, int32_t>
2726ChainedFixupsSection::getBinding(const Symbol *sym, int64_t addend,
2727 bool forceOutline) const {
2728 int64_t outlineAddend =
2729 (forceOutline || !fitsInlineAddend(addend)) ? addend : 0;
2730 auto it = bindings.find(Key: {sym, outlineAddend});
2731 assert(it != bindings.end() && "binding not found in the imports table");
2732 if (outlineAddend == 0)
2733 return {it->second, addend};
2734 return {it->second, 0};
2735}
2736
2737static size_t writeImport(uint8_t *buf, int format, int16_t libOrdinal,
2738 bool weakRef, uint32_t nameOffset, int64_t addend) {
2739 switch (format) {
2740 case DYLD_CHAINED_IMPORT: {
2741 auto *import = reinterpret_cast<dyld_chained_import *>(buf);
2742 import->lib_ordinal = libOrdinal;
2743 import->weak_import = weakRef;
2744 import->name_offset = nameOffset;
2745 return sizeof(dyld_chained_import);
2746 }
2747 case DYLD_CHAINED_IMPORT_ADDEND: {
2748 auto *import = reinterpret_cast<dyld_chained_import_addend *>(buf);
2749 import->lib_ordinal = libOrdinal;
2750 import->weak_import = weakRef;
2751 import->name_offset = nameOffset;
2752 import->addend = addend;
2753 return sizeof(dyld_chained_import_addend);
2754 }
2755 case DYLD_CHAINED_IMPORT_ADDEND64: {
2756 auto *import = reinterpret_cast<dyld_chained_import_addend64 *>(buf);
2757 import->lib_ordinal = libOrdinal;
2758 import->weak_import = weakRef;
2759 import->name_offset = nameOffset;
2760 import->addend = addend;
2761 return sizeof(dyld_chained_import_addend64);
2762 }
2763 default:
2764 llvm_unreachable("Unknown import format");
2765 }
2766}
2767
2768size_t ChainedFixupsSection::SegmentInfo::getSize() const {
2769 assert(pageStarts.size() > 0 && "SegmentInfo for segment with no fixups?");
2770 return alignTo<8>(Value: sizeof(dyld_chained_starts_in_segment) +
2771 pageStarts.back().first * sizeof(uint16_t));
2772}
2773
2774size_t ChainedFixupsSection::SegmentInfo::writeTo(uint8_t *buf) const {
2775 auto *segInfo = reinterpret_cast<dyld_chained_starts_in_segment *>(buf);
2776 segInfo->size = getSize();
2777 segInfo->page_size = target->getPageSize();
2778 segInfo->pointer_format = config->chainedFixupsPointerFormat;
2779 segInfo->segment_offset = oseg->addr - in.header->addr;
2780 segInfo->max_valid_pointer = 0; // not used on 64-bit
2781 segInfo->page_count = pageStarts.back().first + 1;
2782
2783 uint16_t *starts = segInfo->page_start;
2784 for (size_t i = 0; i < segInfo->page_count; ++i)
2785 starts[i] = DYLD_CHAINED_PTR_START_NONE;
2786
2787 for (auto [pageIdx, startAddr] : pageStarts)
2788 starts[pageIdx] = startAddr;
2789 return segInfo->size;
2790}
2791
2792static size_t importEntrySize(int format) {
2793 switch (format) {
2794 case DYLD_CHAINED_IMPORT:
2795 return sizeof(dyld_chained_import);
2796 case DYLD_CHAINED_IMPORT_ADDEND:
2797 return sizeof(dyld_chained_import_addend);
2798 case DYLD_CHAINED_IMPORT_ADDEND64:
2799 return sizeof(dyld_chained_import_addend64);
2800 default:
2801 llvm_unreachable("Unknown import format");
2802 }
2803}
2804
2805// This is step 3 of the algorithm described in the class comment of
2806// ChainedFixupsSection.
2807//
2808// LC_DYLD_CHAINED_FIXUPS data consists of (in this order):
2809// * A dyld_chained_fixups_header
2810// * A dyld_chained_starts_in_image
2811// * One dyld_chained_starts_in_segment per segment
2812// * List of all imports (dyld_chained_import, dyld_chained_import_addend, or
2813// dyld_chained_import_addend64)
2814// * Names of imported symbols
2815void ChainedFixupsSection::writeTo(uint8_t *buf) const {
2816 auto *header = reinterpret_cast<dyld_chained_fixups_header *>(buf);
2817 header->fixups_version = 0;
2818 header->imports_count = bindings.size();
2819 header->imports_format = importFormat;
2820 header->symbols_format = 0;
2821
2822 buf += alignTo<8>(Value: sizeof(*header));
2823
2824 auto curOffset = [&buf, &header]() -> uint32_t {
2825 return buf - reinterpret_cast<uint8_t *>(header);
2826 };
2827
2828 header->starts_offset = curOffset();
2829
2830 auto *imageInfo = reinterpret_cast<dyld_chained_starts_in_image *>(buf);
2831 imageInfo->seg_count = outputSegments.size();
2832 uint32_t *segStarts = imageInfo->seg_info_offset;
2833
2834 // dyld_chained_starts_in_image ends in a flexible array member containing an
2835 // uint32_t for each segment. Leave room for it, and fill it via segStarts.
2836 buf += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset) +
2837 outputSegments.size() * sizeof(uint32_t));
2838
2839 // Initialize all offsets to 0, which indicates that the segment does not have
2840 // fixups. Those that do have them will be filled in below.
2841 for (size_t i = 0; i < outputSegments.size(); ++i)
2842 segStarts[i] = 0;
2843
2844 for (const SegmentInfo &seg : fixupSegments) {
2845 segStarts[seg.oseg->index] = curOffset() - header->starts_offset;
2846 buf += seg.writeTo(buf);
2847 }
2848
2849 // Write imports table.
2850 header->imports_offset = curOffset();
2851 uint64_t nameOffset = 0;
2852 for (auto [import, idx] : bindings) {
2853 const Symbol &sym = *import.first;
2854 buf += writeImport(buf, format: importFormat, libOrdinal: ordinalForSymbol(sym),
2855 weakRef: sym.isWeakRef(), nameOffset, addend: import.second);
2856 nameOffset += sym.getName().size() + 1;
2857 }
2858
2859 // Write imported symbol names.
2860 header->symbols_offset = curOffset();
2861 for (auto [import, idx] : bindings) {
2862 StringRef name = import.first->getName();
2863 memcpy(dest: buf, src: name.data(), n: name.size());
2864 buf += name.size() + 1; // account for null terminator
2865 }
2866
2867 assert(curOffset() == getRawSize());
2868}
2869
2870uint32_t ChainedFixupsSection::chainStride() {
2871 // All ARM64E userland formats use 8-byte stride; DYLD_CHAINED_PTR_64 uses 4.
2872 return config->arch() == AK_arm64e ? 8 : 4;
2873}
2874
2875// This is step 2 of the algorithm described in the class comment of
2876// ChainedFixupsSection.
2877void ChainedFixupsSection::finalizeContents() {
2878 assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
2879 assert(config->emitChainedFixups);
2880
2881 if (!isUInt<32>(x: symtabSize))
2882 error(msg: "cannot encode chained fixups: imported symbols table size " +
2883 Twine(symtabSize) + " exceeds 4 GiB");
2884
2885 // DYLD_CHAINED_PTR_ARM64E binds have 16-bit import ordinals.
2886 if (config->chainedFixupsPointerFormat == DYLD_CHAINED_PTR_ARM64E &&
2887 bindings.size() > UINT16_MAX)
2888 error(msg: "chained fixups, imports_count (" + Twine(bindings.size()) +
2889 ") exceeds max of 65535");
2890
2891 bool needsLargeOrdinal = any_of(Range&: bindings, P: [](const auto &p) {
2892 // 0xF1 - 0xFF are reserved for special ordinals in the 8-bit encoding.
2893 return ordinalForSymbol(*p.first.first) > 0xF0;
2894 });
2895
2896 if (needsLargeAddend || !isUInt<23>(x: symtabSize) || needsLargeOrdinal)
2897 importFormat = DYLD_CHAINED_IMPORT_ADDEND64;
2898 else if (needsAddend)
2899 importFormat = DYLD_CHAINED_IMPORT_ADDEND;
2900 else
2901 importFormat = DYLD_CHAINED_IMPORT;
2902
2903 const uint64_t pageSize = target->getPageSize();
2904 for (Location &loc : locations) {
2905 uint64_t offset =
2906 loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(off: loc.offset);
2907 auto report = [&](const Twine &problem) {
2908 std::string msg =
2909 (loc.isec->getLocation(off: loc.offset) + ": " + problem +
2910 "; stop packing structs containing pointers" +
2911 (canDisableChainedFixups() ? " or link with -no_fixup_chains" : ""))
2912 .str();
2913 std::string src = loc.isec->getSourceLocation(off: loc.offset);
2914 if (!src.empty())
2915 msg += "\n>>> defined at " + src;
2916 error(msg);
2917 };
2918 if (offset % pageSize + target->wordSize > pageSize)
2919 report("fixup straddles page boundary");
2920 if (offset % chainStride() != 0)
2921 report("fixup is not " + Twine(chainStride()) + "-byte aligned");
2922 loc.offset = offset;
2923 }
2924
2925 llvm::sort(C&: locations, Comp: [](const Location &a, const Location &b) {
2926 const OutputSegment *segA = a.isec->parent->parent;
2927 const OutputSegment *segB = b.isec->parent->parent;
2928 if (segA == segB)
2929 return a.offset < b.offset;
2930 return segA->addr < segB->addr;
2931 });
2932
2933 auto sameSegment = [](const Location &a, const Location &b) {
2934 return a.isec->parent->parent == b.isec->parent->parent;
2935 };
2936
2937 for (size_t i = 0, count = locations.size(); i < count;) {
2938 const Location &firstLoc = locations[i];
2939 fixupSegments.emplace_back(Args&: firstLoc.isec->parent->parent);
2940 while (i < count && sameSegment(locations[i], firstLoc)) {
2941 uint64_t pageIdx = locations[i].offset / pageSize;
2942 if (pageIdx >= UINT16_MAX) {
2943 fatal(msg: "page index in " + locations[i].isec->getSegName() + "," +
2944 locations[i].isec->getName() + " too large for chained fixups" +
2945 (canDisableChainedFixups() ? "; use -no_fixup_chains" : ""));
2946 }
2947 fixupSegments.back().pageStarts.emplace_back(
2948 Args&: pageIdx, Args: locations[i].offset % pageSize);
2949 ++i;
2950 while (i < count && sameSegment(locations[i], firstLoc) &&
2951 locations[i].offset / pageSize == pageIdx)
2952 ++i;
2953 }
2954 }
2955
2956 // Compute expected encoded size.
2957 size = alignTo<8>(Value: sizeof(dyld_chained_fixups_header));
2958 size += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset) +
2959 outputSegments.size() * sizeof(uint32_t));
2960 for (const SegmentInfo &seg : fixupSegments)
2961 size += seg.getSize();
2962 size += importEntrySize(format: importFormat) * bindings.size();
2963 size += symtabSize;
2964}
2965
2966template SymtabSection *macho::makeSymtabSection<LP64>(StringTableSection &);
2967template SymtabSection *macho::makeSymtabSection<ILP32>(StringTableSection &);
2968