15ffd83dbSDimitry Andric //===- SyntheticSections.cpp ---------------------------------------------===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric
95ffd83dbSDimitry Andric #include "SyntheticSections.h"
10fe6060f1SDimitry Andric #include "ConcatOutputSection.h"
115ffd83dbSDimitry Andric #include "Config.h"
125ffd83dbSDimitry Andric #include "ExportTrie.h"
135ffd83dbSDimitry Andric #include "InputFiles.h"
145ffd83dbSDimitry Andric #include "MachOStructs.h"
15*0fca6ea1SDimitry Andric #include "ObjC.h"
165ffd83dbSDimitry Andric #include "OutputSegment.h"
175ffd83dbSDimitry Andric #include "SymbolTable.h"
185ffd83dbSDimitry Andric #include "Symbols.h"
195ffd83dbSDimitry Andric
2004eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h"
21e8d8bef9SDimitry Andric #include "llvm/ADT/STLExtras.h"
22fe6060f1SDimitry Andric #include "llvm/Config/llvm-config.h"
235ffd83dbSDimitry Andric #include "llvm/Support/EndianStream.h"
24e8d8bef9SDimitry Andric #include "llvm/Support/FileSystem.h"
255ffd83dbSDimitry Andric #include "llvm/Support/LEB128.h"
2681ad6265SDimitry Andric #include "llvm/Support/Parallel.h"
27e8d8bef9SDimitry Andric #include "llvm/Support/Path.h"
28bdd1243dSDimitry Andric #include "llvm/Support/xxhash.h"
29fe6060f1SDimitry Andric
30fe6060f1SDimitry Andric #if defined(__APPLE__)
31fe6060f1SDimitry Andric #include <sys/mman.h>
3281ad6265SDimitry Andric
3381ad6265SDimitry Andric #define COMMON_DIGEST_FOR_OPENSSL
3481ad6265SDimitry Andric #include <CommonCrypto/CommonDigest.h>
3581ad6265SDimitry Andric #else
3681ad6265SDimitry Andric #include "llvm/Support/SHA256.h"
37fe6060f1SDimitry Andric #endif
38fe6060f1SDimitry Andric
395ffd83dbSDimitry Andric using namespace llvm;
40fe6060f1SDimitry Andric using namespace llvm::MachO;
415ffd83dbSDimitry Andric using namespace llvm::support;
425ffd83dbSDimitry Andric using namespace llvm::support::endian;
435ffd83dbSDimitry Andric using namespace lld;
445ffd83dbSDimitry Andric using namespace lld::macho;
455ffd83dbSDimitry Andric
4681ad6265SDimitry Andric // Reads `len` bytes at data and writes the 32-byte SHA256 checksum to `output`.
sha256(const uint8_t * data,size_t len,uint8_t * output)4781ad6265SDimitry Andric static void sha256(const uint8_t *data, size_t len, uint8_t *output) {
4881ad6265SDimitry Andric #if defined(__APPLE__)
4981ad6265SDimitry Andric // FIXME: Make LLVM's SHA256 faster and use it unconditionally. See PR56121
5081ad6265SDimitry Andric // for some notes on this.
5181ad6265SDimitry Andric CC_SHA256(data, len, output);
5281ad6265SDimitry Andric #else
5381ad6265SDimitry Andric ArrayRef<uint8_t> block(data, len);
5481ad6265SDimitry Andric std::array<uint8_t, 32> hash = SHA256::hash(block);
55bdd1243dSDimitry Andric static_assert(hash.size() == CodeSignatureSection::hashSize);
5681ad6265SDimitry Andric memcpy(output, hash.data(), hash.size());
5781ad6265SDimitry Andric #endif
5881ad6265SDimitry Andric }
5981ad6265SDimitry Andric
605ffd83dbSDimitry Andric InStruct macho::in;
615ffd83dbSDimitry Andric std::vector<SyntheticSection *> macho::syntheticSections;
625ffd83dbSDimitry Andric
SyntheticSection(const char * segname,const char * name)635ffd83dbSDimitry Andric SyntheticSection::SyntheticSection(const char *segname, const char *name)
64fe6060f1SDimitry Andric : OutputSection(SyntheticKind, name) {
65fe6060f1SDimitry Andric std::tie(this->segname, this->name) = maybeRenameSection({segname, name});
6681ad6265SDimitry Andric isec = makeSyntheticInputSection(segname, name);
67fe6060f1SDimitry Andric isec->parent = this;
685ffd83dbSDimitry Andric syntheticSections.push_back(this);
695ffd83dbSDimitry Andric }
705ffd83dbSDimitry Andric
715ffd83dbSDimitry Andric // dyld3's MachOLoaded::getSlide() assumes that the __TEXT segment starts
725ffd83dbSDimitry Andric // from the beginning of the file (i.e. the header).
MachHeaderSection()735ffd83dbSDimitry Andric MachHeaderSection::MachHeaderSection()
74fe6060f1SDimitry Andric : SyntheticSection(segment_names::text, section_names::header) {
75fe6060f1SDimitry Andric // XXX: This is a hack. (See D97007)
76fe6060f1SDimitry Andric // Setting the index to 1 to pretend that this section is the text
77fe6060f1SDimitry Andric // section.
78fe6060f1SDimitry Andric index = 1;
79fe6060f1SDimitry Andric isec->isFinal = true;
80fe6060f1SDimitry Andric }
815ffd83dbSDimitry Andric
addLoadCommand(LoadCommand * lc)825ffd83dbSDimitry Andric void MachHeaderSection::addLoadCommand(LoadCommand *lc) {
835ffd83dbSDimitry Andric loadCommands.push_back(lc);
845ffd83dbSDimitry Andric sizeOfCmds += lc->getSize();
855ffd83dbSDimitry Andric }
865ffd83dbSDimitry Andric
getSize() const875ffd83dbSDimitry Andric uint64_t MachHeaderSection::getSize() const {
88fe6060f1SDimitry Andric uint64_t size = target->headerSize + sizeOfCmds + config->headerPad;
89fe6060f1SDimitry Andric // If we are emitting an encryptable binary, our load commands must have a
90fe6060f1SDimitry Andric // separate (non-encrypted) page to themselves.
91fe6060f1SDimitry Andric if (config->emitEncryptionInfo)
9206c3fb27SDimitry Andric size = alignToPowerOf2(size, target->getPageSize());
93fe6060f1SDimitry Andric return size;
94fe6060f1SDimitry Andric }
95fe6060f1SDimitry Andric
cpuSubtype()96fe6060f1SDimitry Andric static uint32_t cpuSubtype() {
97fe6060f1SDimitry Andric uint32_t subtype = target->cpuSubtype;
98fe6060f1SDimitry Andric
99fe6060f1SDimitry Andric if (config->outputType == MH_EXECUTE && !config->staticLink &&
100fe6060f1SDimitry Andric target->cpuSubtype == CPU_SUBTYPE_X86_64_ALL &&
10104eeddc0SDimitry Andric config->platform() == PLATFORM_MACOS &&
10206c3fb27SDimitry Andric config->platformInfo.target.MinDeployment >= VersionTuple(10, 5))
103fe6060f1SDimitry Andric subtype |= CPU_SUBTYPE_LIB64;
104fe6060f1SDimitry Andric
105fe6060f1SDimitry Andric return subtype;
1065ffd83dbSDimitry Andric }
1075ffd83dbSDimitry Andric
hasWeakBinding()108bdd1243dSDimitry Andric static bool hasWeakBinding() {
109bdd1243dSDimitry Andric return config->emitChainedFixups ? in.chainedFixups->hasWeakBinding()
110bdd1243dSDimitry Andric : in.weakBinding->hasEntry();
111bdd1243dSDimitry Andric }
112bdd1243dSDimitry Andric
hasNonWeakDefinition()113bdd1243dSDimitry Andric static bool hasNonWeakDefinition() {
114bdd1243dSDimitry Andric return config->emitChainedFixups ? in.chainedFixups->hasNonWeakDefinition()
115bdd1243dSDimitry Andric : in.weakBinding->hasNonWeakDefinition();
116bdd1243dSDimitry Andric }
117bdd1243dSDimitry Andric
writeTo(uint8_t * buf) const1185ffd83dbSDimitry Andric void MachHeaderSection::writeTo(uint8_t *buf) const {
119fe6060f1SDimitry Andric auto *hdr = reinterpret_cast<mach_header *>(buf);
120fe6060f1SDimitry Andric hdr->magic = target->magic;
121fe6060f1SDimitry Andric hdr->cputype = target->cpuType;
122fe6060f1SDimitry Andric hdr->cpusubtype = cpuSubtype();
1235ffd83dbSDimitry Andric hdr->filetype = config->outputType;
1245ffd83dbSDimitry Andric hdr->ncmds = loadCommands.size();
1255ffd83dbSDimitry Andric hdr->sizeofcmds = sizeOfCmds;
126fe6060f1SDimitry Andric hdr->flags = MH_DYLDLINK;
127e8d8bef9SDimitry Andric
128fe6060f1SDimitry Andric if (config->namespaceKind == NamespaceKind::twolevel)
129fe6060f1SDimitry Andric hdr->flags |= MH_NOUNDEFS | MH_TWOLEVEL;
1305ffd83dbSDimitry Andric
131fe6060f1SDimitry Andric if (config->outputType == MH_DYLIB && !config->hasReexports)
132fe6060f1SDimitry Andric hdr->flags |= MH_NO_REEXPORTED_DYLIBS;
133fe6060f1SDimitry Andric
134fe6060f1SDimitry Andric if (config->markDeadStrippableDylib)
135fe6060f1SDimitry Andric hdr->flags |= MH_DEAD_STRIPPABLE_DYLIB;
136fe6060f1SDimitry Andric
137fe6060f1SDimitry Andric if (config->outputType == MH_EXECUTE && config->isPic)
138fe6060f1SDimitry Andric hdr->flags |= MH_PIE;
139fe6060f1SDimitry Andric
140fe6060f1SDimitry Andric if (config->outputType == MH_DYLIB && config->applicationExtension)
141fe6060f1SDimitry Andric hdr->flags |= MH_APP_EXTENSION_SAFE;
142e8d8bef9SDimitry Andric
143bdd1243dSDimitry Andric if (in.exports->hasWeakSymbol || hasNonWeakDefinition())
144fe6060f1SDimitry Andric hdr->flags |= MH_WEAK_DEFINES;
145e8d8bef9SDimitry Andric
146bdd1243dSDimitry Andric if (in.exports->hasWeakSymbol || hasWeakBinding())
147fe6060f1SDimitry Andric hdr->flags |= MH_BINDS_TO_WEAK;
148e8d8bef9SDimitry Andric
149fe6060f1SDimitry Andric for (const OutputSegment *seg : outputSegments) {
150fe6060f1SDimitry Andric for (const OutputSection *osec : seg->getSections()) {
151e8d8bef9SDimitry Andric if (isThreadLocalVariables(osec->flags)) {
152fe6060f1SDimitry Andric hdr->flags |= MH_HAS_TLV_DESCRIPTORS;
153e8d8bef9SDimitry Andric break;
154e8d8bef9SDimitry Andric }
155e8d8bef9SDimitry Andric }
156e8d8bef9SDimitry Andric }
157e8d8bef9SDimitry Andric
158fe6060f1SDimitry Andric uint8_t *p = reinterpret_cast<uint8_t *>(hdr) + target->headerSize;
159fe6060f1SDimitry Andric for (const LoadCommand *lc : loadCommands) {
1605ffd83dbSDimitry Andric lc->writeTo(p);
1615ffd83dbSDimitry Andric p += lc->getSize();
1625ffd83dbSDimitry Andric }
1635ffd83dbSDimitry Andric }
1645ffd83dbSDimitry Andric
PageZeroSection()1655ffd83dbSDimitry Andric PageZeroSection::PageZeroSection()
1665ffd83dbSDimitry Andric : SyntheticSection(segment_names::pageZero, section_names::pageZero) {}
1675ffd83dbSDimitry Andric
RebaseSection()168e8d8bef9SDimitry Andric RebaseSection::RebaseSection()
169e8d8bef9SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::rebase) {}
170e8d8bef9SDimitry Andric
171e8d8bef9SDimitry Andric namespace {
172fcaf7f86SDimitry Andric struct RebaseState {
173fcaf7f86SDimitry Andric uint64_t sequenceLength;
174fcaf7f86SDimitry Andric uint64_t skipLength;
175e8d8bef9SDimitry Andric };
176e8d8bef9SDimitry Andric } // namespace
177e8d8bef9SDimitry Andric
emitIncrement(uint64_t incr,raw_svector_ostream & os)178fcaf7f86SDimitry Andric static void emitIncrement(uint64_t incr, raw_svector_ostream &os) {
179fcaf7f86SDimitry Andric assert(incr != 0);
180e8d8bef9SDimitry Andric
181fcaf7f86SDimitry Andric if ((incr >> target->p2WordSize) <= REBASE_IMMEDIATE_MASK &&
182fcaf7f86SDimitry Andric (incr % target->wordSize) == 0) {
18381ad6265SDimitry Andric os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_IMM_SCALED |
184fcaf7f86SDimitry Andric (incr >> target->p2WordSize));
18581ad6265SDimitry Andric } else {
186e8d8bef9SDimitry Andric os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_ULEB);
187fcaf7f86SDimitry Andric encodeULEB128(incr, os);
188e8d8bef9SDimitry Andric }
189e8d8bef9SDimitry Andric }
190fcaf7f86SDimitry Andric
flushRebase(const RebaseState & state,raw_svector_ostream & os)191fcaf7f86SDimitry Andric static void flushRebase(const RebaseState &state, raw_svector_ostream &os) {
192fcaf7f86SDimitry Andric assert(state.sequenceLength > 0);
193fcaf7f86SDimitry Andric
194fcaf7f86SDimitry Andric if (state.skipLength == target->wordSize) {
195fcaf7f86SDimitry Andric if (state.sequenceLength <= REBASE_IMMEDIATE_MASK) {
196fcaf7f86SDimitry Andric os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_IMM_TIMES |
197fcaf7f86SDimitry Andric state.sequenceLength);
198fcaf7f86SDimitry Andric } else {
199fcaf7f86SDimitry Andric os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ULEB_TIMES);
200fcaf7f86SDimitry Andric encodeULEB128(state.sequenceLength, os);
201fcaf7f86SDimitry Andric }
202fcaf7f86SDimitry Andric } else if (state.sequenceLength == 1) {
203fcaf7f86SDimitry Andric os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB);
204fcaf7f86SDimitry Andric encodeULEB128(state.skipLength - target->wordSize, os);
205fcaf7f86SDimitry Andric } else {
206fcaf7f86SDimitry Andric os << static_cast<uint8_t>(
207fcaf7f86SDimitry Andric REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB);
208fcaf7f86SDimitry Andric encodeULEB128(state.sequenceLength, os);
209fcaf7f86SDimitry Andric encodeULEB128(state.skipLength - target->wordSize, os);
210fcaf7f86SDimitry Andric }
211fcaf7f86SDimitry Andric }
212fcaf7f86SDimitry Andric
213fcaf7f86SDimitry Andric // Rebases are communicated to dyld using a bytecode, whose opcodes cause the
214fcaf7f86SDimitry Andric // memory location at a specific address to be rebased and/or the address to be
215fcaf7f86SDimitry Andric // incremented.
216fcaf7f86SDimitry Andric //
217fcaf7f86SDimitry Andric // Opcode REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB is the most generic
218fcaf7f86SDimitry Andric // one, encoding a series of evenly spaced addresses. This algorithm works by
219fcaf7f86SDimitry Andric // splitting up the sorted list of addresses into such chunks. If the locations
220fcaf7f86SDimitry Andric // are consecutive or the sequence consists of a single location, flushRebase
221fcaf7f86SDimitry Andric // will use a smaller, more specialized encoding.
encodeRebases(const OutputSegment * seg,MutableArrayRef<Location> locations,raw_svector_ostream & os)222fcaf7f86SDimitry Andric static void encodeRebases(const OutputSegment *seg,
223fcaf7f86SDimitry Andric MutableArrayRef<Location> locations,
224fcaf7f86SDimitry Andric raw_svector_ostream &os) {
225fcaf7f86SDimitry Andric // dyld operates on segments. Translate section offsets into segment offsets.
226fcaf7f86SDimitry Andric for (Location &loc : locations)
227fcaf7f86SDimitry Andric loc.offset =
228fcaf7f86SDimitry Andric loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset);
229fcaf7f86SDimitry Andric // The algorithm assumes that locations are unique.
230fcaf7f86SDimitry Andric Location *end =
231fcaf7f86SDimitry Andric llvm::unique(locations, [](const Location &a, const Location &b) {
232fcaf7f86SDimitry Andric return a.offset == b.offset;
233fcaf7f86SDimitry Andric });
234fcaf7f86SDimitry Andric size_t count = end - locations.begin();
235fcaf7f86SDimitry Andric
236fcaf7f86SDimitry Andric os << static_cast<uint8_t>(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
237fcaf7f86SDimitry Andric seg->index);
238fcaf7f86SDimitry Andric assert(!locations.empty());
239fcaf7f86SDimitry Andric uint64_t offset = locations[0].offset;
240fcaf7f86SDimitry Andric encodeULEB128(offset, os);
241fcaf7f86SDimitry Andric
242fcaf7f86SDimitry Andric RebaseState state{1, target->wordSize};
243fcaf7f86SDimitry Andric
244fcaf7f86SDimitry Andric for (size_t i = 1; i < count; ++i) {
245fcaf7f86SDimitry Andric offset = locations[i].offset;
246fcaf7f86SDimitry Andric
247fcaf7f86SDimitry Andric uint64_t skip = offset - locations[i - 1].offset;
248fcaf7f86SDimitry Andric assert(skip != 0 && "duplicate locations should have been weeded out");
249fcaf7f86SDimitry Andric
250fcaf7f86SDimitry Andric if (skip == state.skipLength) {
251fcaf7f86SDimitry Andric ++state.sequenceLength;
252fcaf7f86SDimitry Andric } else if (state.sequenceLength == 1) {
253fcaf7f86SDimitry Andric ++state.sequenceLength;
254fcaf7f86SDimitry Andric state.skipLength = skip;
255fcaf7f86SDimitry Andric } else if (skip < state.skipLength) {
256fcaf7f86SDimitry Andric // The address is lower than what the rebase pointer would be if the last
257fcaf7f86SDimitry Andric // location would be part of a sequence. We start a new sequence from the
258fcaf7f86SDimitry Andric // previous location.
259fcaf7f86SDimitry Andric --state.sequenceLength;
260fcaf7f86SDimitry Andric flushRebase(state, os);
261fcaf7f86SDimitry Andric
262fcaf7f86SDimitry Andric state.sequenceLength = 2;
263fcaf7f86SDimitry Andric state.skipLength = skip;
264fcaf7f86SDimitry Andric } else {
265fcaf7f86SDimitry Andric // The address is at some positive offset from the rebase pointer. We
266fcaf7f86SDimitry Andric // start a new sequence which begins with the current location.
267fcaf7f86SDimitry Andric flushRebase(state, os);
268fcaf7f86SDimitry Andric emitIncrement(skip - state.skipLength, os);
269fcaf7f86SDimitry Andric state.sequenceLength = 1;
270fcaf7f86SDimitry Andric state.skipLength = target->wordSize;
271fcaf7f86SDimitry Andric }
272fcaf7f86SDimitry Andric }
273fcaf7f86SDimitry Andric flushRebase(state, os);
274e8d8bef9SDimitry Andric }
275e8d8bef9SDimitry Andric
finalizeContents()276e8d8bef9SDimitry Andric void RebaseSection::finalizeContents() {
277e8d8bef9SDimitry Andric if (locations.empty())
278e8d8bef9SDimitry Andric return;
279e8d8bef9SDimitry Andric
280e8d8bef9SDimitry Andric raw_svector_ostream os{contents};
281e8d8bef9SDimitry Andric os << static_cast<uint8_t>(REBASE_OPCODE_SET_TYPE_IMM | REBASE_TYPE_POINTER);
282e8d8bef9SDimitry Andric
283e8d8bef9SDimitry Andric llvm::sort(locations, [](const Location &a, const Location &b) {
284fe6060f1SDimitry Andric return a.isec->getVA(a.offset) < b.isec->getVA(b.offset);
285e8d8bef9SDimitry Andric });
286e8d8bef9SDimitry Andric
287fcaf7f86SDimitry Andric for (size_t i = 0, count = locations.size(); i < count;) {
288fcaf7f86SDimitry Andric const OutputSegment *seg = locations[i].isec->parent->parent;
289fcaf7f86SDimitry Andric size_t j = i + 1;
290fcaf7f86SDimitry Andric while (j < count && locations[j].isec->parent->parent == seg)
291fcaf7f86SDimitry Andric ++j;
292fcaf7f86SDimitry Andric encodeRebases(seg, {locations.data() + i, locations.data() + j}, os);
293fcaf7f86SDimitry Andric i = j;
294fcaf7f86SDimitry Andric }
295e8d8bef9SDimitry Andric os << static_cast<uint8_t>(REBASE_OPCODE_DONE);
296e8d8bef9SDimitry Andric }
297e8d8bef9SDimitry Andric
writeTo(uint8_t * buf) const298e8d8bef9SDimitry Andric void RebaseSection::writeTo(uint8_t *buf) const {
299e8d8bef9SDimitry Andric memcpy(buf, contents.data(), contents.size());
300e8d8bef9SDimitry Andric }
301e8d8bef9SDimitry Andric
NonLazyPointerSectionBase(const char * segname,const char * name)302e8d8bef9SDimitry Andric NonLazyPointerSectionBase::NonLazyPointerSectionBase(const char *segname,
303e8d8bef9SDimitry Andric const char *name)
304e8d8bef9SDimitry Andric : SyntheticSection(segname, name) {
305fe6060f1SDimitry Andric align = target->wordSize;
306fe6060f1SDimitry Andric }
307fe6060f1SDimitry Andric
addNonLazyBindingEntries(const Symbol * sym,const InputSection * isec,uint64_t offset,int64_t addend)308fe6060f1SDimitry Andric void macho::addNonLazyBindingEntries(const Symbol *sym,
309fe6060f1SDimitry Andric const InputSection *isec, uint64_t offset,
310fe6060f1SDimitry Andric int64_t addend) {
311bdd1243dSDimitry Andric if (config->emitChainedFixups) {
312bdd1243dSDimitry Andric if (needsBinding(sym))
313bdd1243dSDimitry Andric in.chainedFixups->addBinding(sym, isec, offset, addend);
314bdd1243dSDimitry Andric else if (isa<Defined>(sym))
315bdd1243dSDimitry Andric in.chainedFixups->addRebase(isec, offset);
316bdd1243dSDimitry Andric else
317bdd1243dSDimitry Andric llvm_unreachable("cannot bind to an undefined symbol");
318bdd1243dSDimitry Andric return;
319bdd1243dSDimitry Andric }
320bdd1243dSDimitry Andric
321fe6060f1SDimitry Andric if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
322fe6060f1SDimitry Andric in.binding->addEntry(dysym, isec, offset, addend);
323fe6060f1SDimitry Andric if (dysym->isWeakDef())
324fe6060f1SDimitry Andric in.weakBinding->addEntry(sym, isec, offset, addend);
325fe6060f1SDimitry Andric } else if (const auto *defined = dyn_cast<Defined>(sym)) {
326fe6060f1SDimitry Andric in.rebase->addEntry(isec, offset);
327fe6060f1SDimitry Andric if (defined->isExternalWeakDef())
328fe6060f1SDimitry Andric in.weakBinding->addEntry(sym, isec, offset, addend);
32981ad6265SDimitry Andric else if (defined->interposable)
33081ad6265SDimitry Andric in.binding->addEntry(sym, isec, offset, addend);
331fe6060f1SDimitry Andric } else {
332fe6060f1SDimitry Andric // Undefined symbols are filtered out in scanRelocations(); we should never
333fe6060f1SDimitry Andric // get here
334fe6060f1SDimitry Andric llvm_unreachable("cannot bind to an undefined symbol");
335fe6060f1SDimitry Andric }
3365ffd83dbSDimitry Andric }
3375ffd83dbSDimitry Andric
addEntry(Symbol * sym)338e8d8bef9SDimitry Andric void NonLazyPointerSectionBase::addEntry(Symbol *sym) {
339e8d8bef9SDimitry Andric if (entries.insert(sym)) {
340e8d8bef9SDimitry Andric assert(!sym->isInGot());
341e8d8bef9SDimitry Andric sym->gotIndex = entries.size() - 1;
342e8d8bef9SDimitry Andric
343fe6060f1SDimitry Andric addNonLazyBindingEntries(sym, isec, sym->gotIndex * target->wordSize);
3445ffd83dbSDimitry Andric }
3455ffd83dbSDimitry Andric }
3465ffd83dbSDimitry Andric
writeChainedRebase(uint8_t * buf,uint64_t targetVA)347bdd1243dSDimitry Andric void macho::writeChainedRebase(uint8_t *buf, uint64_t targetVA) {
348bdd1243dSDimitry Andric assert(config->emitChainedFixups);
349bdd1243dSDimitry Andric assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
350bdd1243dSDimitry Andric auto *rebase = reinterpret_cast<dyld_chained_ptr_64_rebase *>(buf);
351bdd1243dSDimitry Andric rebase->target = targetVA & 0xf'ffff'ffff;
352bdd1243dSDimitry Andric rebase->high8 = (targetVA >> 56);
353bdd1243dSDimitry Andric rebase->reserved = 0;
354bdd1243dSDimitry Andric rebase->next = 0;
355bdd1243dSDimitry Andric rebase->bind = 0;
356bdd1243dSDimitry Andric
357bdd1243dSDimitry Andric // The fixup format places a 64 GiB limit on the output's size.
358bdd1243dSDimitry Andric // Should we handle this gracefully?
359bdd1243dSDimitry Andric uint64_t encodedVA = rebase->target | ((uint64_t)rebase->high8 << 56);
360bdd1243dSDimitry Andric if (encodedVA != targetVA)
361bdd1243dSDimitry Andric error("rebase target address 0x" + Twine::utohexstr(targetVA) +
362bdd1243dSDimitry Andric " does not fit into chained fixup. Re-link with -no_fixup_chains");
363bdd1243dSDimitry Andric }
364bdd1243dSDimitry Andric
writeChainedBind(uint8_t * buf,const Symbol * sym,int64_t addend)365bdd1243dSDimitry Andric static void writeChainedBind(uint8_t *buf, const Symbol *sym, int64_t addend) {
366bdd1243dSDimitry Andric assert(config->emitChainedFixups);
367bdd1243dSDimitry Andric assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
368bdd1243dSDimitry Andric auto *bind = reinterpret_cast<dyld_chained_ptr_64_bind *>(buf);
369bdd1243dSDimitry Andric auto [ordinal, inlineAddend] = in.chainedFixups->getBinding(sym, addend);
370bdd1243dSDimitry Andric bind->ordinal = ordinal;
371bdd1243dSDimitry Andric bind->addend = inlineAddend;
372bdd1243dSDimitry Andric bind->reserved = 0;
373bdd1243dSDimitry Andric bind->next = 0;
374bdd1243dSDimitry Andric bind->bind = 1;
375bdd1243dSDimitry Andric }
376bdd1243dSDimitry Andric
writeChainedFixup(uint8_t * buf,const Symbol * sym,int64_t addend)377bdd1243dSDimitry Andric void macho::writeChainedFixup(uint8_t *buf, const Symbol *sym, int64_t addend) {
378bdd1243dSDimitry Andric if (needsBinding(sym))
379bdd1243dSDimitry Andric writeChainedBind(buf, sym, addend);
380bdd1243dSDimitry Andric else
381bdd1243dSDimitry Andric writeChainedRebase(buf, sym->getVA() + addend);
382bdd1243dSDimitry Andric }
383bdd1243dSDimitry Andric
writeTo(uint8_t * buf) const384e8d8bef9SDimitry Andric void NonLazyPointerSectionBase::writeTo(uint8_t *buf) const {
385bdd1243dSDimitry Andric if (config->emitChainedFixups) {
386bdd1243dSDimitry Andric for (const auto &[i, entry] : llvm::enumerate(entries))
387bdd1243dSDimitry Andric writeChainedFixup(&buf[i * target->wordSize], entry, 0);
388bdd1243dSDimitry Andric } else {
389bdd1243dSDimitry Andric for (const auto &[i, entry] : llvm::enumerate(entries))
390bdd1243dSDimitry Andric if (auto *defined = dyn_cast<Defined>(entry))
391fe6060f1SDimitry Andric write64le(&buf[i * target->wordSize], defined->getVA());
392fe6060f1SDimitry Andric }
393bdd1243dSDimitry Andric }
394fe6060f1SDimitry Andric
GotSection()395fe6060f1SDimitry Andric GotSection::GotSection()
396349cc55cSDimitry Andric : NonLazyPointerSectionBase(segment_names::data, section_names::got) {
397fe6060f1SDimitry Andric flags = S_NON_LAZY_SYMBOL_POINTERS;
398fe6060f1SDimitry Andric }
399fe6060f1SDimitry Andric
TlvPointerSection()400fe6060f1SDimitry Andric TlvPointerSection::TlvPointerSection()
401fe6060f1SDimitry Andric : NonLazyPointerSectionBase(segment_names::data,
402fe6060f1SDimitry Andric section_names::threadPtrs) {
403fe6060f1SDimitry Andric flags = S_THREAD_LOCAL_VARIABLE_POINTERS;
4045ffd83dbSDimitry Andric }
4055ffd83dbSDimitry Andric
BindingSection()4065ffd83dbSDimitry Andric BindingSection::BindingSection()
407e8d8bef9SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::binding) {}
4085ffd83dbSDimitry Andric
4095ffd83dbSDimitry Andric namespace {
4105ffd83dbSDimitry Andric struct Binding {
4115ffd83dbSDimitry Andric OutputSegment *segment = nullptr;
4125ffd83dbSDimitry Andric uint64_t offset = 0;
4135ffd83dbSDimitry Andric int64_t addend = 0;
414fe6060f1SDimitry Andric };
415fe6060f1SDimitry Andric struct BindIR {
416fe6060f1SDimitry Andric // Default value of 0xF0 is not valid opcode and should make the program
417fe6060f1SDimitry Andric // scream instead of accidentally writing "valid" values.
418fe6060f1SDimitry Andric uint8_t opcode = 0xF0;
419fe6060f1SDimitry Andric uint64_t data = 0;
420fe6060f1SDimitry Andric uint64_t consecutiveCount = 0;
4215ffd83dbSDimitry Andric };
4225ffd83dbSDimitry Andric } // namespace
4235ffd83dbSDimitry Andric
424e8d8bef9SDimitry Andric // Encode a sequence of opcodes that tell dyld to write the address of symbol +
4255ffd83dbSDimitry Andric // addend at osec->addr + outSecOff.
4265ffd83dbSDimitry Andric //
4275ffd83dbSDimitry Andric // The bind opcode "interpreter" remembers the values of each binding field, so
4285ffd83dbSDimitry Andric // we only need to encode the differences between bindings. Hence the use of
4295ffd83dbSDimitry Andric // lastBinding.
encodeBinding(const OutputSection * osec,uint64_t outSecOff,int64_t addend,Binding & lastBinding,std::vector<BindIR> & opcodes)430fe6060f1SDimitry Andric static void encodeBinding(const OutputSection *osec, uint64_t outSecOff,
431fe6060f1SDimitry Andric int64_t addend, Binding &lastBinding,
432fe6060f1SDimitry Andric std::vector<BindIR> &opcodes) {
4335ffd83dbSDimitry Andric OutputSegment *seg = osec->parent;
4345ffd83dbSDimitry Andric uint64_t offset = osec->getSegmentOffset() + outSecOff;
4355ffd83dbSDimitry Andric if (lastBinding.segment != seg) {
436fe6060f1SDimitry Andric opcodes.push_back(
437fe6060f1SDimitry Andric {static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
438fe6060f1SDimitry Andric seg->index),
439fe6060f1SDimitry Andric offset});
4405ffd83dbSDimitry Andric lastBinding.segment = seg;
4415ffd83dbSDimitry Andric lastBinding.offset = offset;
4425ffd83dbSDimitry Andric } else if (lastBinding.offset != offset) {
443fe6060f1SDimitry Andric opcodes.push_back({BIND_OPCODE_ADD_ADDR_ULEB, offset - lastBinding.offset});
4445ffd83dbSDimitry Andric lastBinding.offset = offset;
4455ffd83dbSDimitry Andric }
4465ffd83dbSDimitry Andric
4475ffd83dbSDimitry Andric if (lastBinding.addend != addend) {
448fe6060f1SDimitry Andric opcodes.push_back(
449fe6060f1SDimitry Andric {BIND_OPCODE_SET_ADDEND_SLEB, static_cast<uint64_t>(addend)});
4505ffd83dbSDimitry Andric lastBinding.addend = addend;
4515ffd83dbSDimitry Andric }
4525ffd83dbSDimitry Andric
453fe6060f1SDimitry Andric opcodes.push_back({BIND_OPCODE_DO_BIND, 0});
4545ffd83dbSDimitry Andric // DO_BIND causes dyld to both perform the binding and increment the offset
455fe6060f1SDimitry Andric lastBinding.offset += target->wordSize;
456fe6060f1SDimitry Andric }
457fe6060f1SDimitry Andric
optimizeOpcodes(std::vector<BindIR> & opcodes)458fe6060f1SDimitry Andric static void optimizeOpcodes(std::vector<BindIR> &opcodes) {
459fe6060f1SDimitry Andric // Pass 1: Combine bind/add pairs
460fe6060f1SDimitry Andric size_t i;
461fe6060f1SDimitry Andric int pWrite = 0;
462fe6060f1SDimitry Andric for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
463fe6060f1SDimitry Andric if ((opcodes[i].opcode == BIND_OPCODE_ADD_ADDR_ULEB) &&
464fe6060f1SDimitry Andric (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND)) {
465fe6060f1SDimitry Andric opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB;
466fe6060f1SDimitry Andric opcodes[pWrite].data = opcodes[i].data;
467fe6060f1SDimitry Andric ++i;
468fe6060f1SDimitry Andric } else {
469fe6060f1SDimitry Andric opcodes[pWrite] = opcodes[i - 1];
470fe6060f1SDimitry Andric }
471fe6060f1SDimitry Andric }
472fe6060f1SDimitry Andric if (i == opcodes.size())
473fe6060f1SDimitry Andric opcodes[pWrite] = opcodes[i - 1];
474fe6060f1SDimitry Andric opcodes.resize(pWrite + 1);
475fe6060f1SDimitry Andric
476fe6060f1SDimitry Andric // Pass 2: Compress two or more bind_add opcodes
477fe6060f1SDimitry Andric pWrite = 0;
478fe6060f1SDimitry Andric for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
479fe6060f1SDimitry Andric if ((opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
480fe6060f1SDimitry Andric (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
481fe6060f1SDimitry Andric (opcodes[i].data == opcodes[i - 1].data)) {
482fe6060f1SDimitry Andric opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB;
483fe6060f1SDimitry Andric opcodes[pWrite].consecutiveCount = 2;
484fe6060f1SDimitry Andric opcodes[pWrite].data = opcodes[i].data;
485fe6060f1SDimitry Andric ++i;
486fe6060f1SDimitry Andric while (i < opcodes.size() &&
487fe6060f1SDimitry Andric (opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
488fe6060f1SDimitry Andric (opcodes[i].data == opcodes[i - 1].data)) {
489fe6060f1SDimitry Andric opcodes[pWrite].consecutiveCount++;
490fe6060f1SDimitry Andric ++i;
491fe6060f1SDimitry Andric }
492fe6060f1SDimitry Andric } else {
493fe6060f1SDimitry Andric opcodes[pWrite] = opcodes[i - 1];
494fe6060f1SDimitry Andric }
495fe6060f1SDimitry Andric }
496fe6060f1SDimitry Andric if (i == opcodes.size())
497fe6060f1SDimitry Andric opcodes[pWrite] = opcodes[i - 1];
498fe6060f1SDimitry Andric opcodes.resize(pWrite + 1);
499fe6060f1SDimitry Andric
500fe6060f1SDimitry Andric // Pass 3: Use immediate encodings
501fe6060f1SDimitry Andric // Every binding is the size of one pointer. If the next binding is a
502fe6060f1SDimitry Andric // multiple of wordSize away that is within BIND_IMMEDIATE_MASK, the
503fe6060f1SDimitry Andric // opcode can be scaled by wordSize into a single byte and dyld will
504fe6060f1SDimitry Andric // expand it to the correct address.
505fe6060f1SDimitry Andric for (auto &p : opcodes) {
506fe6060f1SDimitry Andric // It's unclear why the check needs to be less than BIND_IMMEDIATE_MASK,
507fe6060f1SDimitry Andric // but ld64 currently does this. This could be a potential bug, but
508fe6060f1SDimitry Andric // for now, perform the same behavior to prevent mysterious bugs.
509fe6060f1SDimitry Andric if ((p.opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
510fe6060f1SDimitry Andric ((p.data / target->wordSize) < BIND_IMMEDIATE_MASK) &&
511fe6060f1SDimitry Andric ((p.data % target->wordSize) == 0)) {
512fe6060f1SDimitry Andric p.opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED;
513fe6060f1SDimitry Andric p.data /= target->wordSize;
514fe6060f1SDimitry Andric }
515fe6060f1SDimitry Andric }
516fe6060f1SDimitry Andric }
517fe6060f1SDimitry Andric
flushOpcodes(const BindIR & op,raw_svector_ostream & os)518fe6060f1SDimitry Andric static void flushOpcodes(const BindIR &op, raw_svector_ostream &os) {
519fe6060f1SDimitry Andric uint8_t opcode = op.opcode & BIND_OPCODE_MASK;
520fe6060f1SDimitry Andric switch (opcode) {
521fe6060f1SDimitry Andric case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
522fe6060f1SDimitry Andric case BIND_OPCODE_ADD_ADDR_ULEB:
523fe6060f1SDimitry Andric case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
524fe6060f1SDimitry Andric os << op.opcode;
525fe6060f1SDimitry Andric encodeULEB128(op.data, os);
526fe6060f1SDimitry Andric break;
527fe6060f1SDimitry Andric case BIND_OPCODE_SET_ADDEND_SLEB:
528fe6060f1SDimitry Andric os << op.opcode;
529fe6060f1SDimitry Andric encodeSLEB128(static_cast<int64_t>(op.data), os);
530fe6060f1SDimitry Andric break;
531fe6060f1SDimitry Andric case BIND_OPCODE_DO_BIND:
532fe6060f1SDimitry Andric os << op.opcode;
533fe6060f1SDimitry Andric break;
534fe6060f1SDimitry Andric case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
535fe6060f1SDimitry Andric os << op.opcode;
536fe6060f1SDimitry Andric encodeULEB128(op.consecutiveCount, os);
537fe6060f1SDimitry Andric encodeULEB128(op.data, os);
538fe6060f1SDimitry Andric break;
539fe6060f1SDimitry Andric case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
540fe6060f1SDimitry Andric os << static_cast<uint8_t>(op.opcode | op.data);
541fe6060f1SDimitry Andric break;
542fe6060f1SDimitry Andric default:
543fe6060f1SDimitry Andric llvm_unreachable("cannot bind to an unrecognized symbol");
544fe6060f1SDimitry Andric }
5455ffd83dbSDimitry Andric }
5465ffd83dbSDimitry Andric
needsWeakBind(const Symbol & sym)547*0fca6ea1SDimitry Andric static bool needsWeakBind(const Symbol &sym) {
548*0fca6ea1SDimitry Andric if (auto *dysym = dyn_cast<DylibSymbol>(&sym))
549*0fca6ea1SDimitry Andric return dysym->isWeakDef();
550*0fca6ea1SDimitry Andric if (auto *defined = dyn_cast<Defined>(&sym))
551*0fca6ea1SDimitry Andric return defined->isExternalWeakDef();
552*0fca6ea1SDimitry Andric return false;
553*0fca6ea1SDimitry Andric }
554*0fca6ea1SDimitry Andric
555e8d8bef9SDimitry Andric // Non-weak bindings need to have their dylib ordinal encoded as well.
ordinalForDylibSymbol(const DylibSymbol & dysym)556fe6060f1SDimitry Andric static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) {
557fe6060f1SDimitry Andric if (config->namespaceKind == NamespaceKind::flat || dysym.isDynamicLookup())
558fe6060f1SDimitry Andric return static_cast<int16_t>(BIND_SPECIAL_DYLIB_FLAT_LOOKUP);
559fe6060f1SDimitry Andric assert(dysym.getFile()->isReferenced());
560fe6060f1SDimitry Andric return dysym.getFile()->ordinal;
561fe6060f1SDimitry Andric }
562fe6060f1SDimitry Andric
ordinalForSymbol(const Symbol & sym)56381ad6265SDimitry Andric static int16_t ordinalForSymbol(const Symbol &sym) {
564*0fca6ea1SDimitry Andric if (config->emitChainedFixups && needsWeakBind(sym))
565*0fca6ea1SDimitry Andric return BIND_SPECIAL_DYLIB_WEAK_LOOKUP;
56681ad6265SDimitry Andric if (const auto *dysym = dyn_cast<DylibSymbol>(&sym))
56781ad6265SDimitry Andric return ordinalForDylibSymbol(*dysym);
56881ad6265SDimitry Andric assert(cast<Defined>(&sym)->interposable);
56981ad6265SDimitry Andric return BIND_SPECIAL_DYLIB_FLAT_LOOKUP;
57081ad6265SDimitry Andric }
57181ad6265SDimitry Andric
encodeDylibOrdinal(int16_t ordinal,raw_svector_ostream & os)572fe6060f1SDimitry Andric static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) {
573fe6060f1SDimitry Andric if (ordinal <= 0) {
574fe6060f1SDimitry Andric os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
575fe6060f1SDimitry Andric (ordinal & BIND_IMMEDIATE_MASK));
576fe6060f1SDimitry Andric } else if (ordinal <= BIND_IMMEDIATE_MASK) {
577fe6060f1SDimitry Andric os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | ordinal);
578e8d8bef9SDimitry Andric } else {
579e8d8bef9SDimitry Andric os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
580fe6060f1SDimitry Andric encodeULEB128(ordinal, os);
581e8d8bef9SDimitry Andric }
582e8d8bef9SDimitry Andric }
583e8d8bef9SDimitry Andric
encodeWeakOverride(const Defined * defined,raw_svector_ostream & os)584e8d8bef9SDimitry Andric static void encodeWeakOverride(const Defined *defined,
585e8d8bef9SDimitry Andric raw_svector_ostream &os) {
586e8d8bef9SDimitry Andric os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM |
587e8d8bef9SDimitry Andric BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
588e8d8bef9SDimitry Andric << defined->getName() << '\0';
589e8d8bef9SDimitry Andric }
590e8d8bef9SDimitry Andric
591fe6060f1SDimitry Andric // Organize the bindings so we can encoded them with fewer opcodes.
592fe6060f1SDimitry Andric //
593fe6060f1SDimitry Andric // First, all bindings for a given symbol should be grouped together.
594fe6060f1SDimitry Andric // BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM is the largest opcode (since it
595fe6060f1SDimitry Andric // has an associated symbol string), so we only want to emit it once per symbol.
596fe6060f1SDimitry Andric //
597fe6060f1SDimitry Andric // Within each group, we sort the bindings by address. Since bindings are
598fe6060f1SDimitry Andric // delta-encoded, sorting them allows for a more compact result. Note that
599fe6060f1SDimitry Andric // sorting by address alone ensures that bindings for the same segment / section
600fe6060f1SDimitry Andric // are located together, minimizing the number of times we have to emit
601fe6060f1SDimitry Andric // BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB.
602fe6060f1SDimitry Andric //
603fe6060f1SDimitry Andric // Finally, we sort the symbols by the address of their first binding, again
604fe6060f1SDimitry Andric // to facilitate the delta-encoding process.
605fe6060f1SDimitry Andric template <class Sym>
606fe6060f1SDimitry Andric std::vector<std::pair<const Sym *, std::vector<BindingEntry>>>
sortBindings(const BindingsMap<const Sym * > & bindingsMap)607fe6060f1SDimitry Andric sortBindings(const BindingsMap<const Sym *> &bindingsMap) {
608fe6060f1SDimitry Andric std::vector<std::pair<const Sym *, std::vector<BindingEntry>>> bindingsVec(
609fe6060f1SDimitry Andric bindingsMap.begin(), bindingsMap.end());
610fe6060f1SDimitry Andric for (auto &p : bindingsVec) {
611fe6060f1SDimitry Andric std::vector<BindingEntry> &bindings = p.second;
612fe6060f1SDimitry Andric llvm::sort(bindings, [](const BindingEntry &a, const BindingEntry &b) {
613fe6060f1SDimitry Andric return a.target.getVA() < b.target.getVA();
614fe6060f1SDimitry Andric });
615fe6060f1SDimitry Andric }
616fe6060f1SDimitry Andric llvm::sort(bindingsVec, [](const auto &a, const auto &b) {
617fe6060f1SDimitry Andric return a.second[0].target.getVA() < b.second[0].target.getVA();
618fe6060f1SDimitry Andric });
619fe6060f1SDimitry Andric return bindingsVec;
620fe6060f1SDimitry Andric }
621fe6060f1SDimitry Andric
6225ffd83dbSDimitry Andric // Emit bind opcodes, which are a stream of byte-sized opcodes that dyld
6235ffd83dbSDimitry Andric // interprets to update a record with the following fields:
6245ffd83dbSDimitry Andric // * segment index (of the segment to write the symbol addresses to, typically
6255ffd83dbSDimitry Andric // the __DATA_CONST segment which contains the GOT)
6265ffd83dbSDimitry Andric // * offset within the segment, indicating the next location to write a binding
6275ffd83dbSDimitry Andric // * symbol type
6285ffd83dbSDimitry Andric // * symbol library ordinal (the index of its library's LC_LOAD_DYLIB command)
6295ffd83dbSDimitry Andric // * symbol name
6305ffd83dbSDimitry Andric // * addend
6315ffd83dbSDimitry Andric // When dyld sees BIND_OPCODE_DO_BIND, it uses the current record state to bind
6325ffd83dbSDimitry Andric // a symbol in the GOT, and increments the segment offset to point to the next
6335ffd83dbSDimitry Andric // entry. It does *not* clear the record state after doing the bind, so
6345ffd83dbSDimitry Andric // subsequent opcodes only need to encode the differences between bindings.
finalizeContents()6355ffd83dbSDimitry Andric void BindingSection::finalizeContents() {
6365ffd83dbSDimitry Andric raw_svector_ostream os{contents};
6375ffd83dbSDimitry Andric Binding lastBinding;
638fe6060f1SDimitry Andric int16_t lastOrdinal = 0;
6395ffd83dbSDimitry Andric
640fe6060f1SDimitry Andric for (auto &p : sortBindings(bindingsMap)) {
64181ad6265SDimitry Andric const Symbol *sym = p.first;
642fe6060f1SDimitry Andric std::vector<BindingEntry> &bindings = p.second;
643fe6060f1SDimitry Andric uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
644fe6060f1SDimitry Andric if (sym->isWeakRef())
645fe6060f1SDimitry Andric flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
646fe6060f1SDimitry Andric os << flags << sym->getName() << '\0'
647fe6060f1SDimitry Andric << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
64881ad6265SDimitry Andric int16_t ordinal = ordinalForSymbol(*sym);
649fe6060f1SDimitry Andric if (ordinal != lastOrdinal) {
650fe6060f1SDimitry Andric encodeDylibOrdinal(ordinal, os);
651fe6060f1SDimitry Andric lastOrdinal = ordinal;
6525ffd83dbSDimitry Andric }
653fe6060f1SDimitry Andric std::vector<BindIR> opcodes;
654fe6060f1SDimitry Andric for (const BindingEntry &b : bindings)
655fe6060f1SDimitry Andric encodeBinding(b.target.isec->parent,
656fe6060f1SDimitry Andric b.target.isec->getOffset(b.target.offset), b.addend,
657fe6060f1SDimitry Andric lastBinding, opcodes);
658fe6060f1SDimitry Andric if (config->optimize > 1)
659fe6060f1SDimitry Andric optimizeOpcodes(opcodes);
660fe6060f1SDimitry Andric for (const auto &op : opcodes)
661fe6060f1SDimitry Andric flushOpcodes(op, os);
662e8d8bef9SDimitry Andric }
663fe6060f1SDimitry Andric if (!bindingsMap.empty())
664fe6060f1SDimitry Andric os << static_cast<uint8_t>(BIND_OPCODE_DONE);
6655ffd83dbSDimitry Andric }
6665ffd83dbSDimitry Andric
writeTo(uint8_t * buf) const6675ffd83dbSDimitry Andric void BindingSection::writeTo(uint8_t *buf) const {
6685ffd83dbSDimitry Andric memcpy(buf, contents.data(), contents.size());
6695ffd83dbSDimitry Andric }
6705ffd83dbSDimitry Andric
WeakBindingSection()671e8d8bef9SDimitry Andric WeakBindingSection::WeakBindingSection()
672e8d8bef9SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::weakBinding) {}
673e8d8bef9SDimitry Andric
finalizeContents()674e8d8bef9SDimitry Andric void WeakBindingSection::finalizeContents() {
675e8d8bef9SDimitry Andric raw_svector_ostream os{contents};
676e8d8bef9SDimitry Andric Binding lastBinding;
677e8d8bef9SDimitry Andric
678e8d8bef9SDimitry Andric for (const Defined *defined : definitions)
679e8d8bef9SDimitry Andric encodeWeakOverride(defined, os);
680e8d8bef9SDimitry Andric
681fe6060f1SDimitry Andric for (auto &p : sortBindings(bindingsMap)) {
682fe6060f1SDimitry Andric const Symbol *sym = p.first;
683fe6060f1SDimitry Andric std::vector<BindingEntry> &bindings = p.second;
684fe6060f1SDimitry Andric os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
685fe6060f1SDimitry Andric << sym->getName() << '\0'
686fe6060f1SDimitry Andric << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
687fe6060f1SDimitry Andric std::vector<BindIR> opcodes;
688fe6060f1SDimitry Andric for (const BindingEntry &b : bindings)
689fe6060f1SDimitry Andric encodeBinding(b.target.isec->parent,
690fe6060f1SDimitry Andric b.target.isec->getOffset(b.target.offset), b.addend,
691fe6060f1SDimitry Andric lastBinding, opcodes);
692fe6060f1SDimitry Andric if (config->optimize > 1)
693fe6060f1SDimitry Andric optimizeOpcodes(opcodes);
694fe6060f1SDimitry Andric for (const auto &op : opcodes)
695fe6060f1SDimitry Andric flushOpcodes(op, os);
696e8d8bef9SDimitry Andric }
697fe6060f1SDimitry Andric if (!bindingsMap.empty() || !definitions.empty())
698fe6060f1SDimitry Andric os << static_cast<uint8_t>(BIND_OPCODE_DONE);
699e8d8bef9SDimitry Andric }
700e8d8bef9SDimitry Andric
writeTo(uint8_t * buf) const701e8d8bef9SDimitry Andric void WeakBindingSection::writeTo(uint8_t *buf) const {
702e8d8bef9SDimitry Andric memcpy(buf, contents.data(), contents.size());
703e8d8bef9SDimitry Andric }
704e8d8bef9SDimitry Andric
StubsSection()7055ffd83dbSDimitry Andric StubsSection::StubsSection()
706fe6060f1SDimitry Andric : SyntheticSection(segment_names::text, section_names::stubs) {
707fe6060f1SDimitry Andric flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
708fe6060f1SDimitry Andric // The stubs section comprises machine instructions, which are aligned to
709fe6060f1SDimitry Andric // 4 bytes on the archs we care about.
710fe6060f1SDimitry Andric align = 4;
711e8d8bef9SDimitry Andric reserved2 = target->stubSize;
712e8d8bef9SDimitry Andric }
7135ffd83dbSDimitry Andric
getSize() const7145ffd83dbSDimitry Andric uint64_t StubsSection::getSize() const {
7155ffd83dbSDimitry Andric return entries.size() * target->stubSize;
7165ffd83dbSDimitry Andric }
7175ffd83dbSDimitry Andric
writeTo(uint8_t * buf) const7185ffd83dbSDimitry Andric void StubsSection::writeTo(uint8_t *buf) const {
7195ffd83dbSDimitry Andric size_t off = 0;
720e8d8bef9SDimitry Andric for (const Symbol *sym : entries) {
721bdd1243dSDimitry Andric uint64_t pointerVA =
722bdd1243dSDimitry Andric config->emitChainedFixups ? sym->getGotVA() : sym->getLazyPtrVA();
723bdd1243dSDimitry Andric target->writeStub(buf + off, *sym, pointerVA);
7245ffd83dbSDimitry Andric off += target->stubSize;
7255ffd83dbSDimitry Andric }
7265ffd83dbSDimitry Andric }
7275ffd83dbSDimitry Andric
finalize()728fe6060f1SDimitry Andric void StubsSection::finalize() { isFinal = true; }
729fe6060f1SDimitry Andric
addBindingsForStub(Symbol * sym)730bdd1243dSDimitry Andric static void addBindingsForStub(Symbol *sym) {
731bdd1243dSDimitry Andric assert(!config->emitChainedFixups);
732bdd1243dSDimitry Andric if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
733bdd1243dSDimitry Andric if (sym->isWeakDef()) {
734bdd1243dSDimitry Andric in.binding->addEntry(dysym, in.lazyPointers->isec,
735bdd1243dSDimitry Andric sym->stubsIndex * target->wordSize);
736bdd1243dSDimitry Andric in.weakBinding->addEntry(sym, in.lazyPointers->isec,
737bdd1243dSDimitry Andric sym->stubsIndex * target->wordSize);
738bdd1243dSDimitry Andric } else {
739bdd1243dSDimitry Andric in.lazyBinding->addEntry(dysym);
740bdd1243dSDimitry Andric }
741bdd1243dSDimitry Andric } else if (auto *defined = dyn_cast<Defined>(sym)) {
742bdd1243dSDimitry Andric if (defined->isExternalWeakDef()) {
743bdd1243dSDimitry Andric in.rebase->addEntry(in.lazyPointers->isec,
744bdd1243dSDimitry Andric sym->stubsIndex * target->wordSize);
745bdd1243dSDimitry Andric in.weakBinding->addEntry(sym, in.lazyPointers->isec,
746bdd1243dSDimitry Andric sym->stubsIndex * target->wordSize);
747bdd1243dSDimitry Andric } else if (defined->interposable) {
748bdd1243dSDimitry Andric in.lazyBinding->addEntry(sym);
749bdd1243dSDimitry Andric } else {
750bdd1243dSDimitry Andric llvm_unreachable("invalid stub target");
751bdd1243dSDimitry Andric }
752bdd1243dSDimitry Andric } else {
753bdd1243dSDimitry Andric llvm_unreachable("invalid stub target symbol type");
754bdd1243dSDimitry Andric }
755bdd1243dSDimitry Andric }
756bdd1243dSDimitry Andric
addEntry(Symbol * sym)757bdd1243dSDimitry Andric void StubsSection::addEntry(Symbol *sym) {
758e8d8bef9SDimitry Andric bool inserted = entries.insert(sym);
759bdd1243dSDimitry Andric if (inserted) {
760e8d8bef9SDimitry Andric sym->stubsIndex = entries.size() - 1;
761bdd1243dSDimitry Andric
762bdd1243dSDimitry Andric if (config->emitChainedFixups)
763bdd1243dSDimitry Andric in.got->addEntry(sym);
764bdd1243dSDimitry Andric else
765bdd1243dSDimitry Andric addBindingsForStub(sym);
766bdd1243dSDimitry Andric }
7675ffd83dbSDimitry Andric }
7685ffd83dbSDimitry Andric
StubHelperSection()7695ffd83dbSDimitry Andric StubHelperSection::StubHelperSection()
770fe6060f1SDimitry Andric : SyntheticSection(segment_names::text, section_names::stubHelper) {
771fe6060f1SDimitry Andric flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
772fe6060f1SDimitry Andric align = 4; // This section comprises machine instructions
773fe6060f1SDimitry Andric }
7745ffd83dbSDimitry Andric
getSize() const7755ffd83dbSDimitry Andric uint64_t StubHelperSection::getSize() const {
7765ffd83dbSDimitry Andric return target->stubHelperHeaderSize +
777e8d8bef9SDimitry Andric in.lazyBinding->getEntries().size() * target->stubHelperEntrySize;
7785ffd83dbSDimitry Andric }
7795ffd83dbSDimitry Andric
isNeeded() const780e8d8bef9SDimitry Andric bool StubHelperSection::isNeeded() const { return in.lazyBinding->isNeeded(); }
7815ffd83dbSDimitry Andric
writeTo(uint8_t * buf) const7825ffd83dbSDimitry Andric void StubHelperSection::writeTo(uint8_t *buf) const {
7835ffd83dbSDimitry Andric target->writeStubHelperHeader(buf);
7845ffd83dbSDimitry Andric size_t off = target->stubHelperHeaderSize;
78581ad6265SDimitry Andric for (const Symbol *sym : in.lazyBinding->getEntries()) {
7865ffd83dbSDimitry Andric target->writeStubHelperEntry(buf + off, *sym, addr + off);
7875ffd83dbSDimitry Andric off += target->stubHelperEntrySize;
7885ffd83dbSDimitry Andric }
7895ffd83dbSDimitry Andric }
7905ffd83dbSDimitry Andric
setUp()791bdd1243dSDimitry Andric void StubHelperSection::setUp() {
792fe6060f1SDimitry Andric Symbol *binder = symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr,
793fe6060f1SDimitry Andric /*isWeakRef=*/false);
794fe6060f1SDimitry Andric if (auto *undefined = dyn_cast<Undefined>(binder))
795fe6060f1SDimitry Andric treatUndefinedSymbol(*undefined,
796fe6060f1SDimitry Andric "lazy binding (normally in libSystem.dylib)");
797fe6060f1SDimitry Andric
798fe6060f1SDimitry Andric // treatUndefinedSymbol() can replace binder with a DylibSymbol; re-check.
799fe6060f1SDimitry Andric stubBinder = dyn_cast_or_null<DylibSymbol>(binder);
800fe6060f1SDimitry Andric if (stubBinder == nullptr)
8015ffd83dbSDimitry Andric return;
802fe6060f1SDimitry Andric
803e8d8bef9SDimitry Andric in.got->addEntry(stubBinder);
8045ffd83dbSDimitry Andric
805fe6060f1SDimitry Andric in.imageLoaderCache->parent =
806fe6060f1SDimitry Andric ConcatOutputSection::getOrCreateForInput(in.imageLoaderCache);
807*0fca6ea1SDimitry Andric addInputSection(in.imageLoaderCache);
808fe6060f1SDimitry Andric // Since this isn't in the symbol table or in any input file, the noDeadStrip
809349cc55cSDimitry Andric // argument doesn't matter.
810e8d8bef9SDimitry Andric dyldPrivate =
811fe6060f1SDimitry Andric make<Defined>("__dyld_private", nullptr, in.imageLoaderCache, 0, 0,
812e8d8bef9SDimitry Andric /*isWeakDef=*/false,
813fe6060f1SDimitry Andric /*isExternal=*/false, /*isPrivateExtern=*/false,
81481ad6265SDimitry Andric /*includeInSymtab=*/true,
81506c3fb27SDimitry Andric /*isReferencedDynamically=*/false,
816fe6060f1SDimitry Andric /*noDeadStrip=*/false);
817349cc55cSDimitry Andric dyldPrivate->used = true;
8185ffd83dbSDimitry Andric }
8195ffd83dbSDimitry Andric
820*0fca6ea1SDimitry Andric llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>
821*0fca6ea1SDimitry Andric ObjCSelRefsHelper::methnameToSelref;
initialize()822*0fca6ea1SDimitry Andric void ObjCSelRefsHelper::initialize() {
823*0fca6ea1SDimitry Andric // Do not fold selrefs without ICF.
824*0fca6ea1SDimitry Andric if (config->icfLevel == ICFLevel::none)
825*0fca6ea1SDimitry Andric return;
826*0fca6ea1SDimitry Andric
827*0fca6ea1SDimitry Andric // Search methnames already referenced in __objc_selrefs
828*0fca6ea1SDimitry Andric // Map the name to the corresponding selref entry
829*0fca6ea1SDimitry Andric // which we will reuse when creating objc stubs.
830*0fca6ea1SDimitry Andric for (ConcatInputSection *isec : inputSections) {
831*0fca6ea1SDimitry Andric if (isec->shouldOmitFromOutput())
832*0fca6ea1SDimitry Andric continue;
833*0fca6ea1SDimitry Andric if (isec->getName() != section_names::objcSelrefs)
834*0fca6ea1SDimitry Andric continue;
835*0fca6ea1SDimitry Andric // We expect a single relocation per selref entry to __objc_methname that
836*0fca6ea1SDimitry Andric // might be aggregated.
837*0fca6ea1SDimitry Andric assert(isec->relocs.size() == 1);
838*0fca6ea1SDimitry Andric auto Reloc = isec->relocs[0];
839*0fca6ea1SDimitry Andric if (const auto *sym = Reloc.referent.dyn_cast<Symbol *>()) {
840*0fca6ea1SDimitry Andric if (const auto *d = dyn_cast<Defined>(sym)) {
841*0fca6ea1SDimitry Andric auto *cisec = cast<CStringInputSection>(d->isec());
842*0fca6ea1SDimitry Andric auto methname = cisec->getStringRefAtOffset(d->value);
843*0fca6ea1SDimitry Andric methnameToSelref[CachedHashStringRef(methname)] = isec;
844*0fca6ea1SDimitry Andric }
845*0fca6ea1SDimitry Andric }
846*0fca6ea1SDimitry Andric }
847*0fca6ea1SDimitry Andric }
848*0fca6ea1SDimitry Andric
cleanup()849*0fca6ea1SDimitry Andric void ObjCSelRefsHelper::cleanup() { methnameToSelref.clear(); }
850*0fca6ea1SDimitry Andric
makeSelRef(StringRef methname)851*0fca6ea1SDimitry Andric ConcatInputSection *ObjCSelRefsHelper::makeSelRef(StringRef methname) {
852*0fca6ea1SDimitry Andric auto methnameOffset =
853*0fca6ea1SDimitry Andric in.objcMethnameSection->getStringOffset(methname).outSecOff;
854*0fca6ea1SDimitry Andric
855*0fca6ea1SDimitry Andric size_t wordSize = target->wordSize;
856*0fca6ea1SDimitry Andric uint8_t *selrefData = bAlloc().Allocate<uint8_t>(wordSize);
857*0fca6ea1SDimitry Andric write64le(selrefData, methnameOffset);
858*0fca6ea1SDimitry Andric ConcatInputSection *objcSelref =
859*0fca6ea1SDimitry Andric makeSyntheticInputSection(segment_names::data, section_names::objcSelrefs,
860*0fca6ea1SDimitry Andric S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP,
861*0fca6ea1SDimitry Andric ArrayRef<uint8_t>{selrefData, wordSize},
862*0fca6ea1SDimitry Andric /*align=*/wordSize);
863*0fca6ea1SDimitry Andric assert(objcSelref->live);
864*0fca6ea1SDimitry Andric objcSelref->relocs.push_back({/*type=*/target->unsignedRelocType,
865*0fca6ea1SDimitry Andric /*pcrel=*/false, /*length=*/3,
866*0fca6ea1SDimitry Andric /*offset=*/0,
867*0fca6ea1SDimitry Andric /*addend=*/static_cast<int64_t>(methnameOffset),
868*0fca6ea1SDimitry Andric /*referent=*/in.objcMethnameSection->isec});
869*0fca6ea1SDimitry Andric objcSelref->parent = ConcatOutputSection::getOrCreateForInput(objcSelref);
870*0fca6ea1SDimitry Andric addInputSection(objcSelref);
871*0fca6ea1SDimitry Andric objcSelref->isFinal = true;
872*0fca6ea1SDimitry Andric methnameToSelref[CachedHashStringRef(methname)] = objcSelref;
873*0fca6ea1SDimitry Andric return objcSelref;
874*0fca6ea1SDimitry Andric }
875*0fca6ea1SDimitry Andric
getSelRef(StringRef methname)876*0fca6ea1SDimitry Andric ConcatInputSection *ObjCSelRefsHelper::getSelRef(StringRef methname) {
877*0fca6ea1SDimitry Andric auto it = methnameToSelref.find(CachedHashStringRef(methname));
878*0fca6ea1SDimitry Andric if (it == methnameToSelref.end())
879*0fca6ea1SDimitry Andric return nullptr;
880*0fca6ea1SDimitry Andric return it->second;
881*0fca6ea1SDimitry Andric }
882*0fca6ea1SDimitry Andric
ObjCStubsSection()883bdd1243dSDimitry Andric ObjCStubsSection::ObjCStubsSection()
884bdd1243dSDimitry Andric : SyntheticSection(segment_names::text, section_names::objcStubs) {
885bdd1243dSDimitry Andric flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
8867a6dacacSDimitry Andric align = config->objcStubsMode == ObjCStubsMode::fast
8877a6dacacSDimitry Andric ? target->objcStubsFastAlignment
8887a6dacacSDimitry Andric : target->objcStubsSmallAlignment;
889bdd1243dSDimitry Andric }
890bdd1243dSDimitry Andric
isObjCStubSymbol(Symbol * sym)891*0fca6ea1SDimitry Andric bool ObjCStubsSection::isObjCStubSymbol(Symbol *sym) {
892*0fca6ea1SDimitry Andric return sym->getName().starts_with(symbolPrefix);
893*0fca6ea1SDimitry Andric }
894*0fca6ea1SDimitry Andric
getMethname(Symbol * sym)895*0fca6ea1SDimitry Andric StringRef ObjCStubsSection::getMethname(Symbol *sym) {
896*0fca6ea1SDimitry Andric assert(isObjCStubSymbol(sym) && "not an objc stub");
897*0fca6ea1SDimitry Andric auto name = sym->getName();
898*0fca6ea1SDimitry Andric StringRef methname = name.drop_front(symbolPrefix.size());
899*0fca6ea1SDimitry Andric return methname;
900*0fca6ea1SDimitry Andric }
901*0fca6ea1SDimitry Andric
addEntry(Symbol * sym)902bdd1243dSDimitry Andric void ObjCStubsSection::addEntry(Symbol *sym) {
903*0fca6ea1SDimitry Andric StringRef methname = getMethname(sym);
904*0fca6ea1SDimitry Andric // We create a selref entry for each unique methname.
905*0fca6ea1SDimitry Andric if (!ObjCSelRefsHelper::getSelRef(methname))
906*0fca6ea1SDimitry Andric ObjCSelRefsHelper::makeSelRef(methname);
9077a6dacacSDimitry Andric
9087a6dacacSDimitry Andric auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
9097a6dacacSDimitry Andric ? target->objcStubsFastSize
9107a6dacacSDimitry Andric : target->objcStubsSmallSize;
911bdd1243dSDimitry Andric Defined *newSym = replaceSymbol<Defined>(
912bdd1243dSDimitry Andric sym, sym->getName(), nullptr, isec,
9137a6dacacSDimitry Andric /*value=*/symbols.size() * stubSize,
9147a6dacacSDimitry Andric /*size=*/stubSize,
915bdd1243dSDimitry Andric /*isWeakDef=*/false, /*isExternal=*/true, /*isPrivateExtern=*/true,
91606c3fb27SDimitry Andric /*includeInSymtab=*/true, /*isReferencedDynamically=*/false,
91706c3fb27SDimitry Andric /*noDeadStrip=*/false);
918bdd1243dSDimitry Andric symbols.push_back(newSym);
919bdd1243dSDimitry Andric }
920bdd1243dSDimitry Andric
setUp()921bdd1243dSDimitry Andric void ObjCStubsSection::setUp() {
9227a6dacacSDimitry Andric objcMsgSend = symtab->addUndefined("_objc_msgSend", /*file=*/nullptr,
923bdd1243dSDimitry Andric /*isWeakRef=*/false);
9247a6dacacSDimitry Andric if (auto *undefined = dyn_cast<Undefined>(objcMsgSend))
9257a6dacacSDimitry Andric treatUndefinedSymbol(*undefined,
9267a6dacacSDimitry Andric "lazy binding (normally in libobjc.dylib)");
927bdd1243dSDimitry Andric objcMsgSend->used = true;
9287a6dacacSDimitry Andric if (config->objcStubsMode == ObjCStubsMode::fast) {
929bdd1243dSDimitry Andric in.got->addEntry(objcMsgSend);
930bdd1243dSDimitry Andric assert(objcMsgSend->isInGot());
9317a6dacacSDimitry Andric } else {
9327a6dacacSDimitry Andric assert(config->objcStubsMode == ObjCStubsMode::small);
9337a6dacacSDimitry Andric // In line with ld64's behavior, when objc_msgSend is a direct symbol,
9347a6dacacSDimitry Andric // we directly reference it.
9357a6dacacSDimitry Andric // In other cases, typically when binding in libobjc.dylib,
9367a6dacacSDimitry Andric // we generate a stub to invoke objc_msgSend.
9377a6dacacSDimitry Andric if (!isa<Defined>(objcMsgSend))
9387a6dacacSDimitry Andric in.stubs->addEntry(objcMsgSend);
9397a6dacacSDimitry Andric }
940bdd1243dSDimitry Andric }
941bdd1243dSDimitry Andric
getSize() const942bdd1243dSDimitry Andric uint64_t ObjCStubsSection::getSize() const {
9437a6dacacSDimitry Andric auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
9447a6dacacSDimitry Andric ? target->objcStubsFastSize
9457a6dacacSDimitry Andric : target->objcStubsSmallSize;
9467a6dacacSDimitry Andric return stubSize * symbols.size();
947bdd1243dSDimitry Andric }
948bdd1243dSDimitry Andric
writeTo(uint8_t * buf) const949bdd1243dSDimitry Andric void ObjCStubsSection::writeTo(uint8_t *buf) const {
950bdd1243dSDimitry Andric uint64_t stubOffset = 0;
951bdd1243dSDimitry Andric for (size_t i = 0, n = symbols.size(); i < n; ++i) {
952bdd1243dSDimitry Andric Defined *sym = symbols[i];
953*0fca6ea1SDimitry Andric
954*0fca6ea1SDimitry Andric auto methname = getMethname(sym);
955*0fca6ea1SDimitry Andric InputSection *selRef = ObjCSelRefsHelper::getSelRef(methname);
956*0fca6ea1SDimitry Andric assert(selRef != nullptr && "no selref for methname");
957*0fca6ea1SDimitry Andric auto selrefAddr = selRef->getVA(0);
958bdd1243dSDimitry Andric target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr,
959*0fca6ea1SDimitry Andric stubOffset, selrefAddr, objcMsgSend);
960bdd1243dSDimitry Andric }
961bdd1243dSDimitry Andric }
962bdd1243dSDimitry Andric
LazyPointerSection()9635ffd83dbSDimitry Andric LazyPointerSection::LazyPointerSection()
964fe6060f1SDimitry Andric : SyntheticSection(segment_names::data, section_names::lazySymbolPtr) {
965fe6060f1SDimitry Andric align = target->wordSize;
966fe6060f1SDimitry Andric flags = S_LAZY_SYMBOL_POINTERS;
9675ffd83dbSDimitry Andric }
9685ffd83dbSDimitry Andric
getSize() const9695ffd83dbSDimitry Andric uint64_t LazyPointerSection::getSize() const {
970fe6060f1SDimitry Andric return in.stubs->getEntries().size() * target->wordSize;
9715ffd83dbSDimitry Andric }
9725ffd83dbSDimitry Andric
isNeeded() const9735ffd83dbSDimitry Andric bool LazyPointerSection::isNeeded() const {
9745ffd83dbSDimitry Andric return !in.stubs->getEntries().empty();
9755ffd83dbSDimitry Andric }
9765ffd83dbSDimitry Andric
writeTo(uint8_t * buf) const9775ffd83dbSDimitry Andric void LazyPointerSection::writeTo(uint8_t *buf) const {
9785ffd83dbSDimitry Andric size_t off = 0;
979e8d8bef9SDimitry Andric for (const Symbol *sym : in.stubs->getEntries()) {
980e8d8bef9SDimitry Andric if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
981e8d8bef9SDimitry Andric if (dysym->hasStubsHelper()) {
982e8d8bef9SDimitry Andric uint64_t stubHelperOffset =
983e8d8bef9SDimitry Andric target->stubHelperHeaderSize +
984e8d8bef9SDimitry Andric dysym->stubsHelperIndex * target->stubHelperEntrySize;
9855ffd83dbSDimitry Andric write64le(buf + off, in.stubHelper->addr + stubHelperOffset);
986e8d8bef9SDimitry Andric }
987e8d8bef9SDimitry Andric } else {
988e8d8bef9SDimitry Andric write64le(buf + off, sym->getVA());
989e8d8bef9SDimitry Andric }
990fe6060f1SDimitry Andric off += target->wordSize;
9915ffd83dbSDimitry Andric }
9925ffd83dbSDimitry Andric }
9935ffd83dbSDimitry Andric
LazyBindingSection()9945ffd83dbSDimitry Andric LazyBindingSection::LazyBindingSection()
995e8d8bef9SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::lazyBinding) {}
9965ffd83dbSDimitry Andric
finalizeContents()9975ffd83dbSDimitry Andric void LazyBindingSection::finalizeContents() {
9985ffd83dbSDimitry Andric // TODO: Just precompute output size here instead of writing to a temporary
9995ffd83dbSDimitry Andric // buffer
100081ad6265SDimitry Andric for (Symbol *sym : entries)
10015ffd83dbSDimitry Andric sym->lazyBindOffset = encode(*sym);
10025ffd83dbSDimitry Andric }
10035ffd83dbSDimitry Andric
writeTo(uint8_t * buf) const10045ffd83dbSDimitry Andric void LazyBindingSection::writeTo(uint8_t *buf) const {
10055ffd83dbSDimitry Andric memcpy(buf, contents.data(), contents.size());
10065ffd83dbSDimitry Andric }
10075ffd83dbSDimitry Andric
addEntry(Symbol * sym)100881ad6265SDimitry Andric void LazyBindingSection::addEntry(Symbol *sym) {
1009bdd1243dSDimitry Andric assert(!config->emitChainedFixups && "Chained fixups always bind eagerly");
101081ad6265SDimitry Andric if (entries.insert(sym)) {
101181ad6265SDimitry Andric sym->stubsHelperIndex = entries.size() - 1;
1012fe6060f1SDimitry Andric in.rebase->addEntry(in.lazyPointers->isec,
101381ad6265SDimitry Andric sym->stubsIndex * target->wordSize);
1014e8d8bef9SDimitry Andric }
1015e8d8bef9SDimitry Andric }
1016e8d8bef9SDimitry Andric
10175ffd83dbSDimitry Andric // Unlike the non-lazy binding section, the bind opcodes in this section aren't
10185ffd83dbSDimitry Andric // interpreted all at once. Rather, dyld will start interpreting opcodes at a
10195ffd83dbSDimitry Andric // given offset, typically only binding a single symbol before it finds a
10205ffd83dbSDimitry Andric // BIND_OPCODE_DONE terminator. As such, unlike in the non-lazy-binding case,
10215ffd83dbSDimitry Andric // we cannot encode just the differences between symbols; we have to emit the
10225ffd83dbSDimitry Andric // complete bind information for each symbol.
encode(const Symbol & sym)102381ad6265SDimitry Andric uint32_t LazyBindingSection::encode(const Symbol &sym) {
10245ffd83dbSDimitry Andric uint32_t opstreamOffset = contents.size();
10255ffd83dbSDimitry Andric OutputSegment *dataSeg = in.lazyPointers->parent;
1026fe6060f1SDimitry Andric os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
10275ffd83dbSDimitry Andric dataSeg->index);
102881ad6265SDimitry Andric uint64_t offset =
102981ad6265SDimitry Andric in.lazyPointers->addr - dataSeg->addr + sym.stubsIndex * target->wordSize;
10305ffd83dbSDimitry Andric encodeULEB128(offset, os);
103181ad6265SDimitry Andric encodeDylibOrdinal(ordinalForSymbol(sym), os);
10325ffd83dbSDimitry Andric
1033fe6060f1SDimitry Andric uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
1034e8d8bef9SDimitry Andric if (sym.isWeakRef())
1035fe6060f1SDimitry Andric flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
1036e8d8bef9SDimitry Andric
1037e8d8bef9SDimitry Andric os << flags << sym.getName() << '\0'
1038fe6060f1SDimitry Andric << static_cast<uint8_t>(BIND_OPCODE_DO_BIND)
1039fe6060f1SDimitry Andric << static_cast<uint8_t>(BIND_OPCODE_DONE);
10405ffd83dbSDimitry Andric return opstreamOffset;
10415ffd83dbSDimitry Andric }
10425ffd83dbSDimitry Andric
ExportSection()10435ffd83dbSDimitry Andric ExportSection::ExportSection()
1044e8d8bef9SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::export_) {}
10455ffd83dbSDimitry Andric
finalizeContents()10465ffd83dbSDimitry Andric void ExportSection::finalizeContents() {
1047e8d8bef9SDimitry Andric trieBuilder.setImageBase(in.header->addr);
1048e8d8bef9SDimitry Andric for (const Symbol *sym : symtab->getSymbols()) {
1049e8d8bef9SDimitry Andric if (const auto *defined = dyn_cast<Defined>(sym)) {
1050fe6060f1SDimitry Andric if (defined->privateExtern || !defined->isLive())
1051e8d8bef9SDimitry Andric continue;
10525ffd83dbSDimitry Andric trieBuilder.addSymbol(*defined);
1053e8d8bef9SDimitry Andric hasWeakSymbol = hasWeakSymbol || sym->isWeakDef();
105406c3fb27SDimitry Andric } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
105506c3fb27SDimitry Andric if (dysym->shouldReexport)
105606c3fb27SDimitry Andric trieBuilder.addSymbol(*dysym);
1057e8d8bef9SDimitry Andric }
1058e8d8bef9SDimitry Andric }
10595ffd83dbSDimitry Andric size = trieBuilder.build();
10605ffd83dbSDimitry Andric }
10615ffd83dbSDimitry Andric
writeTo(uint8_t * buf) const10625ffd83dbSDimitry Andric void ExportSection::writeTo(uint8_t *buf) const { trieBuilder.writeTo(buf); }
10635ffd83dbSDimitry Andric
DataInCodeSection()1064fe6060f1SDimitry Andric DataInCodeSection::DataInCodeSection()
1065fe6060f1SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::dataInCode) {}
1066fe6060f1SDimitry Andric
1067fe6060f1SDimitry Andric template <class LP>
collectDataInCodeEntries()1068fe6060f1SDimitry Andric static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() {
1069fe6060f1SDimitry Andric std::vector<MachO::data_in_code_entry> dataInCodeEntries;
1070fe6060f1SDimitry Andric for (const InputFile *inputFile : inputFiles) {
1071fe6060f1SDimitry Andric if (!isa<ObjFile>(inputFile))
1072fe6060f1SDimitry Andric continue;
1073fe6060f1SDimitry Andric const ObjFile *objFile = cast<ObjFile>(inputFile);
10740eae32dcSDimitry Andric ArrayRef<MachO::data_in_code_entry> entries = objFile->getDataInCode();
1075fe6060f1SDimitry Andric if (entries.empty())
1076fe6060f1SDimitry Andric continue;
10770eae32dcSDimitry Andric
1078*0fca6ea1SDimitry Andric std::vector<MachO::data_in_code_entry> sortedEntries;
1079*0fca6ea1SDimitry Andric sortedEntries.assign(entries.begin(), entries.end());
1080*0fca6ea1SDimitry Andric llvm::sort(sortedEntries, [](const data_in_code_entry &lhs,
10810eae32dcSDimitry Andric const data_in_code_entry &rhs) {
10820eae32dcSDimitry Andric return lhs.offset < rhs.offset;
1083*0fca6ea1SDimitry Andric });
1084*0fca6ea1SDimitry Andric
1085fe6060f1SDimitry Andric // For each code subsection find 'data in code' entries residing in it.
1086fe6060f1SDimitry Andric // Compute the new offset values as
1087fe6060f1SDimitry Andric // <offset within subsection> + <subsection address> - <__TEXT address>.
108881ad6265SDimitry Andric for (const Section *section : objFile->sections) {
108981ad6265SDimitry Andric for (const Subsection &subsec : section->subsections) {
1090349cc55cSDimitry Andric const InputSection *isec = subsec.isec;
1091fe6060f1SDimitry Andric if (!isCodeSection(isec))
1092fe6060f1SDimitry Andric continue;
1093fe6060f1SDimitry Andric if (cast<ConcatInputSection>(isec)->shouldOmitFromOutput())
1094fe6060f1SDimitry Andric continue;
109581ad6265SDimitry Andric const uint64_t beginAddr = section->addr + subsec.offset;
1096fe6060f1SDimitry Andric auto it = llvm::lower_bound(
1097*0fca6ea1SDimitry Andric sortedEntries, beginAddr,
1098fe6060f1SDimitry Andric [](const MachO::data_in_code_entry &entry, uint64_t addr) {
1099fe6060f1SDimitry Andric return entry.offset < addr;
1100fe6060f1SDimitry Andric });
110181ad6265SDimitry Andric const uint64_t endAddr = beginAddr + isec->getSize();
1102*0fca6ea1SDimitry Andric for (const auto end = sortedEntries.end();
1103fe6060f1SDimitry Andric it != end && it->offset + it->length <= endAddr; ++it)
1104fe6060f1SDimitry Andric dataInCodeEntries.push_back(
1105fe6060f1SDimitry Andric {static_cast<uint32_t>(isec->getVA(it->offset - beginAddr) -
1106fe6060f1SDimitry Andric in.header->addr),
1107fe6060f1SDimitry Andric it->length, it->kind});
1108fe6060f1SDimitry Andric }
1109fe6060f1SDimitry Andric }
1110fe6060f1SDimitry Andric }
1111bdd1243dSDimitry Andric
1112bdd1243dSDimitry Andric // ld64 emits the table in sorted order too.
1113bdd1243dSDimitry Andric llvm::sort(dataInCodeEntries,
1114bdd1243dSDimitry Andric [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) {
1115bdd1243dSDimitry Andric return lhs.offset < rhs.offset;
1116bdd1243dSDimitry Andric });
1117fe6060f1SDimitry Andric return dataInCodeEntries;
1118fe6060f1SDimitry Andric }
1119fe6060f1SDimitry Andric
finalizeContents()1120fe6060f1SDimitry Andric void DataInCodeSection::finalizeContents() {
1121fe6060f1SDimitry Andric entries = target->wordSize == 8 ? collectDataInCodeEntries<LP64>()
1122fe6060f1SDimitry Andric : collectDataInCodeEntries<ILP32>();
1123fe6060f1SDimitry Andric }
1124fe6060f1SDimitry Andric
writeTo(uint8_t * buf) const1125fe6060f1SDimitry Andric void DataInCodeSection::writeTo(uint8_t *buf) const {
1126fe6060f1SDimitry Andric if (!entries.empty())
1127fe6060f1SDimitry Andric memcpy(buf, entries.data(), getRawSize());
1128fe6060f1SDimitry Andric }
1129fe6060f1SDimitry Andric
FunctionStartsSection()1130fe6060f1SDimitry Andric FunctionStartsSection::FunctionStartsSection()
1131fe6060f1SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::functionStarts) {}
1132fe6060f1SDimitry Andric
finalizeContents()1133fe6060f1SDimitry Andric void FunctionStartsSection::finalizeContents() {
1134fe6060f1SDimitry Andric raw_svector_ostream os{contents};
1135fe6060f1SDimitry Andric std::vector<uint64_t> addrs;
11364824e7fdSDimitry Andric for (const InputFile *file : inputFiles) {
11374824e7fdSDimitry Andric if (auto *objFile = dyn_cast<ObjFile>(file)) {
11384824e7fdSDimitry Andric for (const Symbol *sym : objFile->symbols) {
11394824e7fdSDimitry Andric if (const auto *defined = dyn_cast_or_null<Defined>(sym)) {
1140*0fca6ea1SDimitry Andric if (!defined->isec() || !isCodeSection(defined->isec()) ||
11414824e7fdSDimitry Andric !defined->isLive())
1142fe6060f1SDimitry Andric continue;
1143fe6060f1SDimitry Andric addrs.push_back(defined->getVA());
1144fe6060f1SDimitry Andric }
1145fe6060f1SDimitry Andric }
11464824e7fdSDimitry Andric }
11474824e7fdSDimitry Andric }
1148fe6060f1SDimitry Andric llvm::sort(addrs);
1149fe6060f1SDimitry Andric uint64_t addr = in.header->addr;
1150fe6060f1SDimitry Andric for (uint64_t nextAddr : addrs) {
1151fe6060f1SDimitry Andric uint64_t delta = nextAddr - addr;
1152fe6060f1SDimitry Andric if (delta == 0)
1153fe6060f1SDimitry Andric continue;
1154fe6060f1SDimitry Andric encodeULEB128(delta, os);
1155fe6060f1SDimitry Andric addr = nextAddr;
1156fe6060f1SDimitry Andric }
1157fe6060f1SDimitry Andric os << '\0';
1158fe6060f1SDimitry Andric }
1159fe6060f1SDimitry Andric
writeTo(uint8_t * buf) const1160fe6060f1SDimitry Andric void FunctionStartsSection::writeTo(uint8_t *buf) const {
1161fe6060f1SDimitry Andric memcpy(buf, contents.data(), contents.size());
1162fe6060f1SDimitry Andric }
1163fe6060f1SDimitry Andric
SymtabSection(StringTableSection & stringTableSection)11645ffd83dbSDimitry Andric SymtabSection::SymtabSection(StringTableSection &stringTableSection)
1165e8d8bef9SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::symbolTable),
1166e8d8bef9SDimitry Andric stringTableSection(stringTableSection) {}
1167e8d8bef9SDimitry Andric
emitBeginSourceStab(StringRef sourceFile)116881ad6265SDimitry Andric void SymtabSection::emitBeginSourceStab(StringRef sourceFile) {
1169fe6060f1SDimitry Andric StabsEntry stab(N_SO);
117081ad6265SDimitry Andric stab.strx = stringTableSection.addString(saver().save(sourceFile));
1171e8d8bef9SDimitry Andric stabs.emplace_back(std::move(stab));
1172e8d8bef9SDimitry Andric }
1173e8d8bef9SDimitry Andric
emitEndSourceStab()1174e8d8bef9SDimitry Andric void SymtabSection::emitEndSourceStab() {
1175fe6060f1SDimitry Andric StabsEntry stab(N_SO);
1176e8d8bef9SDimitry Andric stab.sect = 1;
1177e8d8bef9SDimitry Andric stabs.emplace_back(std::move(stab));
1178e8d8bef9SDimitry Andric }
1179e8d8bef9SDimitry Andric
emitObjectFileStab(ObjFile * file)1180e8d8bef9SDimitry Andric void SymtabSection::emitObjectFileStab(ObjFile *file) {
1181fe6060f1SDimitry Andric StabsEntry stab(N_OSO);
1182e8d8bef9SDimitry Andric stab.sect = target->cpuSubtype;
1183e8d8bef9SDimitry Andric SmallString<261> path(!file->archiveName.empty() ? file->archiveName
1184e8d8bef9SDimitry Andric : file->getName());
1185e8d8bef9SDimitry Andric std::error_code ec = sys::fs::make_absolute(path);
1186e8d8bef9SDimitry Andric if (ec)
1187e8d8bef9SDimitry Andric fatal("failed to get absolute path for " + path);
1188e8d8bef9SDimitry Andric
1189e8d8bef9SDimitry Andric if (!file->archiveName.empty())
1190e8d8bef9SDimitry Andric path.append({"(", file->getName(), ")"});
1191e8d8bef9SDimitry Andric
119204eeddc0SDimitry Andric StringRef adjustedPath = saver().save(path.str());
1193349cc55cSDimitry Andric adjustedPath.consume_front(config->osoPrefix);
1194349cc55cSDimitry Andric
1195349cc55cSDimitry Andric stab.strx = stringTableSection.addString(adjustedPath);
1196e8d8bef9SDimitry Andric stab.desc = 1;
1197e8d8bef9SDimitry Andric stab.value = file->modTime;
1198e8d8bef9SDimitry Andric stabs.emplace_back(std::move(stab));
1199e8d8bef9SDimitry Andric }
1200e8d8bef9SDimitry Andric
emitEndFunStab(Defined * defined)1201e8d8bef9SDimitry Andric void SymtabSection::emitEndFunStab(Defined *defined) {
1202fe6060f1SDimitry Andric StabsEntry stab(N_FUN);
1203fe6060f1SDimitry Andric stab.value = defined->size;
1204e8d8bef9SDimitry Andric stabs.emplace_back(std::move(stab));
1205e8d8bef9SDimitry Andric }
1206e8d8bef9SDimitry Andric
emitStabs()1207e8d8bef9SDimitry Andric void SymtabSection::emitStabs() {
1208349cc55cSDimitry Andric if (config->omitDebugInfo)
1209349cc55cSDimitry Andric return;
1210349cc55cSDimitry Andric
1211fe6060f1SDimitry Andric for (const std::string &s : config->astPaths) {
1212fe6060f1SDimitry Andric StabsEntry astStab(N_AST);
1213fe6060f1SDimitry Andric astStab.strx = stringTableSection.addString(s);
1214fe6060f1SDimitry Andric stabs.emplace_back(std::move(astStab));
1215fe6060f1SDimitry Andric }
1216fe6060f1SDimitry Andric
121781ad6265SDimitry Andric // Cache the file ID for each symbol in an std::pair for faster sorting.
121881ad6265SDimitry Andric using SortingPair = std::pair<Defined *, int>;
121981ad6265SDimitry Andric std::vector<SortingPair> symbolsNeedingStabs;
1220e8d8bef9SDimitry Andric for (const SymtabEntry &entry :
1221e8d8bef9SDimitry Andric concat<SymtabEntry>(localSymbols, externalSymbols)) {
1222e8d8bef9SDimitry Andric Symbol *sym = entry.sym;
1223fe6060f1SDimitry Andric assert(sym->isLive() &&
1224fe6060f1SDimitry Andric "dead symbols should not be in localSymbols, externalSymbols");
1225e8d8bef9SDimitry Andric if (auto *defined = dyn_cast<Defined>(sym)) {
122681ad6265SDimitry Andric // Excluded symbols should have been filtered out in finalizeContents().
122781ad6265SDimitry Andric assert(defined->includeInSymtab);
122881ad6265SDimitry Andric
1229e8d8bef9SDimitry Andric if (defined->isAbsolute())
1230e8d8bef9SDimitry Andric continue;
123181ad6265SDimitry Andric
123281ad6265SDimitry Andric // Constant-folded symbols go in the executable's symbol table, but don't
1233*0fca6ea1SDimitry Andric // get a stabs entry unless --keep-icf-stabs flag is specified
1234*0fca6ea1SDimitry Andric if (!config->keepICFStabs && defined->wasIdenticalCodeFolded)
123581ad6265SDimitry Andric continue;
123681ad6265SDimitry Andric
1237bdd1243dSDimitry Andric ObjFile *file = defined->getObjectFile();
1238e8d8bef9SDimitry Andric if (!file || !file->compileUnit)
1239e8d8bef9SDimitry Andric continue;
124081ad6265SDimitry Andric
1241*0fca6ea1SDimitry Andric // We use 'originalIsec' to get the file id of the symbol since 'isec()'
1242*0fca6ea1SDimitry Andric // might point to the merged ICF symbol's file
1243*0fca6ea1SDimitry Andric symbolsNeedingStabs.emplace_back(defined,
1244*0fca6ea1SDimitry Andric defined->originalIsec->getFile()->id);
1245e8d8bef9SDimitry Andric }
1246e8d8bef9SDimitry Andric }
1247e8d8bef9SDimitry Andric
124881ad6265SDimitry Andric llvm::stable_sort(symbolsNeedingStabs,
124981ad6265SDimitry Andric [&](const SortingPair &a, const SortingPair &b) {
125081ad6265SDimitry Andric return a.second < b.second;
1251e8d8bef9SDimitry Andric });
1252e8d8bef9SDimitry Andric
1253e8d8bef9SDimitry Andric // Emit STABS symbols so that dsymutil and/or the debugger can map address
1254e8d8bef9SDimitry Andric // regions in the final binary to the source and object files from which they
1255e8d8bef9SDimitry Andric // originated.
1256e8d8bef9SDimitry Andric InputFile *lastFile = nullptr;
125781ad6265SDimitry Andric for (SortingPair &pair : symbolsNeedingStabs) {
125881ad6265SDimitry Andric Defined *defined = pair.first;
1259*0fca6ea1SDimitry Andric // We use 'originalIsec' of the symbol since we care about the actual origin
1260*0fca6ea1SDimitry Andric // of the symbol, not the canonical location returned by `isec()`.
1261*0fca6ea1SDimitry Andric InputSection *isec = defined->originalIsec;
1262fe6060f1SDimitry Andric ObjFile *file = cast<ObjFile>(isec->getFile());
1263e8d8bef9SDimitry Andric
1264e8d8bef9SDimitry Andric if (lastFile == nullptr || lastFile != file) {
1265e8d8bef9SDimitry Andric if (lastFile != nullptr)
1266e8d8bef9SDimitry Andric emitEndSourceStab();
1267e8d8bef9SDimitry Andric lastFile = file;
1268e8d8bef9SDimitry Andric
126981ad6265SDimitry Andric emitBeginSourceStab(file->sourceFile());
1270e8d8bef9SDimitry Andric emitObjectFileStab(file);
1271e8d8bef9SDimitry Andric }
1272e8d8bef9SDimitry Andric
1273e8d8bef9SDimitry Andric StabsEntry symStab;
1274*0fca6ea1SDimitry Andric symStab.sect = isec->parent->index;
1275e8d8bef9SDimitry Andric symStab.strx = stringTableSection.addString(defined->getName());
1276e8d8bef9SDimitry Andric symStab.value = defined->getVA();
1277e8d8bef9SDimitry Andric
1278e8d8bef9SDimitry Andric if (isCodeSection(isec)) {
1279fe6060f1SDimitry Andric symStab.type = N_FUN;
1280e8d8bef9SDimitry Andric stabs.emplace_back(std::move(symStab));
1281e8d8bef9SDimitry Andric emitEndFunStab(defined);
1282e8d8bef9SDimitry Andric } else {
1283fe6060f1SDimitry Andric symStab.type = defined->isExternal() ? N_GSYM : N_STSYM;
1284e8d8bef9SDimitry Andric stabs.emplace_back(std::move(symStab));
1285e8d8bef9SDimitry Andric }
1286e8d8bef9SDimitry Andric }
1287e8d8bef9SDimitry Andric
1288e8d8bef9SDimitry Andric if (!stabs.empty())
1289e8d8bef9SDimitry Andric emitEndSourceStab();
12905ffd83dbSDimitry Andric }
12915ffd83dbSDimitry Andric
finalizeContents()12925ffd83dbSDimitry Andric void SymtabSection::finalizeContents() {
1293e8d8bef9SDimitry Andric auto addSymbol = [&](std::vector<SymtabEntry> &symbols, Symbol *sym) {
1294e8d8bef9SDimitry Andric uint32_t strx = stringTableSection.addString(sym->getName());
1295e8d8bef9SDimitry Andric symbols.push_back({sym, strx});
1296e8d8bef9SDimitry Andric };
1297e8d8bef9SDimitry Andric
129881ad6265SDimitry Andric std::function<void(Symbol *)> localSymbolsHandler;
129981ad6265SDimitry Andric switch (config->localSymbolsPresence) {
130081ad6265SDimitry Andric case SymtabPresence::All:
130181ad6265SDimitry Andric localSymbolsHandler = [&](Symbol *sym) { addSymbol(localSymbols, sym); };
130281ad6265SDimitry Andric break;
130381ad6265SDimitry Andric case SymtabPresence::None:
130481ad6265SDimitry Andric localSymbolsHandler = [&](Symbol *) { /* Do nothing*/ };
130581ad6265SDimitry Andric break;
130681ad6265SDimitry Andric case SymtabPresence::SelectivelyIncluded:
130781ad6265SDimitry Andric localSymbolsHandler = [&](Symbol *sym) {
130881ad6265SDimitry Andric if (config->localSymbolPatterns.match(sym->getName()))
130981ad6265SDimitry Andric addSymbol(localSymbols, sym);
131081ad6265SDimitry Andric };
131181ad6265SDimitry Andric break;
131281ad6265SDimitry Andric case SymtabPresence::SelectivelyExcluded:
131381ad6265SDimitry Andric localSymbolsHandler = [&](Symbol *sym) {
131481ad6265SDimitry Andric if (!config->localSymbolPatterns.match(sym->getName()))
131581ad6265SDimitry Andric addSymbol(localSymbols, sym);
131681ad6265SDimitry Andric };
131781ad6265SDimitry Andric break;
131881ad6265SDimitry Andric }
131981ad6265SDimitry Andric
1320e8d8bef9SDimitry Andric // Local symbols aren't in the SymbolTable, so we walk the list of object
1321e8d8bef9SDimitry Andric // files to gather them.
132281ad6265SDimitry Andric // But if `-x` is set, then we don't need to. localSymbolsHandler() will do
132381ad6265SDimitry Andric // the right thing regardless, but this check is a perf optimization because
132481ad6265SDimitry Andric // iterating through all the input files and their symbols is expensive.
132581ad6265SDimitry Andric if (config->localSymbolsPresence != SymtabPresence::None) {
1326fe6060f1SDimitry Andric for (const InputFile *file : inputFiles) {
1327e8d8bef9SDimitry Andric if (auto *objFile = dyn_cast<ObjFile>(file)) {
1328e8d8bef9SDimitry Andric for (Symbol *sym : objFile->symbols) {
1329fe6060f1SDimitry Andric if (auto *defined = dyn_cast_or_null<Defined>(sym)) {
133081ad6265SDimitry Andric if (defined->isExternal() || !defined->isLive() ||
133181ad6265SDimitry Andric !defined->includeInSymtab)
133281ad6265SDimitry Andric continue;
133381ad6265SDimitry Andric localSymbolsHandler(sym);
1334e8d8bef9SDimitry Andric }
1335e8d8bef9SDimitry Andric }
1336e8d8bef9SDimitry Andric }
1337e8d8bef9SDimitry Andric }
1338fe6060f1SDimitry Andric }
1339e8d8bef9SDimitry Andric
1340e8d8bef9SDimitry Andric // __dyld_private is a local symbol too. It's linker-created and doesn't
1341e8d8bef9SDimitry Andric // exist in any object file.
1342bdd1243dSDimitry Andric if (in.stubHelper && in.stubHelper->dyldPrivate)
1343bdd1243dSDimitry Andric localSymbolsHandler(in.stubHelper->dyldPrivate);
1344e8d8bef9SDimitry Andric
1345e8d8bef9SDimitry Andric for (Symbol *sym : symtab->getSymbols()) {
1346fe6060f1SDimitry Andric if (!sym->isLive())
1347fe6060f1SDimitry Andric continue;
1348e8d8bef9SDimitry Andric if (auto *defined = dyn_cast<Defined>(sym)) {
1349fe6060f1SDimitry Andric if (!defined->includeInSymtab)
1350fe6060f1SDimitry Andric continue;
1351e8d8bef9SDimitry Andric assert(defined->isExternal());
1352fe6060f1SDimitry Andric if (defined->privateExtern)
135381ad6265SDimitry Andric localSymbolsHandler(defined);
1354fe6060f1SDimitry Andric else
1355fe6060f1SDimitry Andric addSymbol(externalSymbols, defined);
1356e8d8bef9SDimitry Andric } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
1357e8d8bef9SDimitry Andric if (dysym->isReferenced())
1358e8d8bef9SDimitry Andric addSymbol(undefinedSymbols, sym);
1359e8d8bef9SDimitry Andric }
1360e8d8bef9SDimitry Andric }
1361e8d8bef9SDimitry Andric
1362e8d8bef9SDimitry Andric emitStabs();
1363e8d8bef9SDimitry Andric uint32_t symtabIndex = stabs.size();
1364e8d8bef9SDimitry Andric for (const SymtabEntry &entry :
1365e8d8bef9SDimitry Andric concat<SymtabEntry>(localSymbols, externalSymbols, undefinedSymbols)) {
1366e8d8bef9SDimitry Andric entry.sym->symtabIndex = symtabIndex++;
1367e8d8bef9SDimitry Andric }
1368e8d8bef9SDimitry Andric }
1369e8d8bef9SDimitry Andric
getNumSymbols() const1370e8d8bef9SDimitry Andric uint32_t SymtabSection::getNumSymbols() const {
1371e8d8bef9SDimitry Andric return stabs.size() + localSymbols.size() + externalSymbols.size() +
1372e8d8bef9SDimitry Andric undefinedSymbols.size();
13735ffd83dbSDimitry Andric }
13745ffd83dbSDimitry Andric
1375fe6060f1SDimitry Andric // This serves to hide (type-erase) the template parameter from SymtabSection.
1376fe6060f1SDimitry Andric template <class LP> class SymtabSectionImpl final : public SymtabSection {
1377fe6060f1SDimitry Andric public:
SymtabSectionImpl(StringTableSection & stringTableSection)1378fe6060f1SDimitry Andric SymtabSectionImpl(StringTableSection &stringTableSection)
1379fe6060f1SDimitry Andric : SymtabSection(stringTableSection) {}
1380fe6060f1SDimitry Andric uint64_t getRawSize() const override;
1381fe6060f1SDimitry Andric void writeTo(uint8_t *buf) const override;
1382fe6060f1SDimitry Andric };
1383fe6060f1SDimitry Andric
getRawSize() const1384fe6060f1SDimitry Andric template <class LP> uint64_t SymtabSectionImpl<LP>::getRawSize() const {
1385fe6060f1SDimitry Andric return getNumSymbols() * sizeof(typename LP::nlist);
1386fe6060f1SDimitry Andric }
1387fe6060f1SDimitry Andric
writeTo(uint8_t * buf) const1388fe6060f1SDimitry Andric template <class LP> void SymtabSectionImpl<LP>::writeTo(uint8_t *buf) const {
1389fe6060f1SDimitry Andric auto *nList = reinterpret_cast<typename LP::nlist *>(buf);
1390e8d8bef9SDimitry Andric // Emit the stabs entries before the "real" symbols. We cannot emit them
1391e8d8bef9SDimitry Andric // after as that would render Symbol::symtabIndex inaccurate.
1392e8d8bef9SDimitry Andric for (const StabsEntry &entry : stabs) {
13935ffd83dbSDimitry Andric nList->n_strx = entry.strx;
1394e8d8bef9SDimitry Andric nList->n_type = entry.type;
1395e8d8bef9SDimitry Andric nList->n_sect = entry.sect;
1396e8d8bef9SDimitry Andric nList->n_desc = entry.desc;
1397e8d8bef9SDimitry Andric nList->n_value = entry.value;
1398e8d8bef9SDimitry Andric ++nList;
1399e8d8bef9SDimitry Andric }
1400e8d8bef9SDimitry Andric
1401e8d8bef9SDimitry Andric for (const SymtabEntry &entry : concat<const SymtabEntry>(
1402e8d8bef9SDimitry Andric localSymbols, externalSymbols, undefinedSymbols)) {
1403e8d8bef9SDimitry Andric nList->n_strx = entry.strx;
1404e8d8bef9SDimitry Andric // TODO populate n_desc with more flags
14055ffd83dbSDimitry Andric if (auto *defined = dyn_cast<Defined>(entry.sym)) {
1406e8d8bef9SDimitry Andric uint8_t scope = 0;
1407e8d8bef9SDimitry Andric if (defined->privateExtern) {
1408e8d8bef9SDimitry Andric // Private external -- dylib scoped symbol.
1409e8d8bef9SDimitry Andric // Promote to non-external at link time.
1410fe6060f1SDimitry Andric scope = N_PEXT;
1411e8d8bef9SDimitry Andric } else if (defined->isExternal()) {
1412e8d8bef9SDimitry Andric // Normal global symbol.
1413fe6060f1SDimitry Andric scope = N_EXT;
1414e8d8bef9SDimitry Andric } else {
1415e8d8bef9SDimitry Andric // TU-local symbol from localSymbols.
1416e8d8bef9SDimitry Andric scope = 0;
1417e8d8bef9SDimitry Andric }
1418e8d8bef9SDimitry Andric
1419e8d8bef9SDimitry Andric if (defined->isAbsolute()) {
1420fe6060f1SDimitry Andric nList->n_type = scope | N_ABS;
1421fe6060f1SDimitry Andric nList->n_sect = NO_SECT;
1422e8d8bef9SDimitry Andric nList->n_value = defined->value;
1423e8d8bef9SDimitry Andric } else {
1424fe6060f1SDimitry Andric nList->n_type = scope | N_SECT;
1425*0fca6ea1SDimitry Andric nList->n_sect = defined->isec()->parent->index;
14265ffd83dbSDimitry Andric // For the N_SECT symbol type, n_value is the address of the symbol
1427e8d8bef9SDimitry Andric nList->n_value = defined->getVA();
1428e8d8bef9SDimitry Andric }
1429fe6060f1SDimitry Andric nList->n_desc |= defined->isExternalWeakDef() ? N_WEAK_DEF : 0;
1430fe6060f1SDimitry Andric nList->n_desc |=
1431fe6060f1SDimitry Andric defined->referencedDynamically ? REFERENCED_DYNAMICALLY : 0;
1432e8d8bef9SDimitry Andric } else if (auto *dysym = dyn_cast<DylibSymbol>(entry.sym)) {
1433e8d8bef9SDimitry Andric uint16_t n_desc = nList->n_desc;
1434fe6060f1SDimitry Andric int16_t ordinal = ordinalForDylibSymbol(*dysym);
1435fe6060f1SDimitry Andric if (ordinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
1436fe6060f1SDimitry Andric SET_LIBRARY_ORDINAL(n_desc, DYNAMIC_LOOKUP_ORDINAL);
1437fe6060f1SDimitry Andric else if (ordinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
1438fe6060f1SDimitry Andric SET_LIBRARY_ORDINAL(n_desc, EXECUTABLE_ORDINAL);
1439fe6060f1SDimitry Andric else {
1440fe6060f1SDimitry Andric assert(ordinal > 0);
1441fe6060f1SDimitry Andric SET_LIBRARY_ORDINAL(n_desc, static_cast<uint8_t>(ordinal));
1442fe6060f1SDimitry Andric }
1443fe6060f1SDimitry Andric
1444fe6060f1SDimitry Andric nList->n_type = N_EXT;
1445fe6060f1SDimitry Andric n_desc |= dysym->isWeakDef() ? N_WEAK_DEF : 0;
1446fe6060f1SDimitry Andric n_desc |= dysym->isWeakRef() ? N_WEAK_REF : 0;
1447e8d8bef9SDimitry Andric nList->n_desc = n_desc;
14485ffd83dbSDimitry Andric }
14495ffd83dbSDimitry Andric ++nList;
14505ffd83dbSDimitry Andric }
14515ffd83dbSDimitry Andric }
14525ffd83dbSDimitry Andric
1453fe6060f1SDimitry Andric template <class LP>
1454fe6060f1SDimitry Andric SymtabSection *
makeSymtabSection(StringTableSection & stringTableSection)1455fe6060f1SDimitry Andric macho::makeSymtabSection(StringTableSection &stringTableSection) {
1456fe6060f1SDimitry Andric return make<SymtabSectionImpl<LP>>(stringTableSection);
1457fe6060f1SDimitry Andric }
1458fe6060f1SDimitry Andric
IndirectSymtabSection()1459e8d8bef9SDimitry Andric IndirectSymtabSection::IndirectSymtabSection()
1460e8d8bef9SDimitry Andric : LinkEditSection(segment_names::linkEdit,
1461e8d8bef9SDimitry Andric section_names::indirectSymbolTable) {}
1462e8d8bef9SDimitry Andric
getNumSymbols() const1463e8d8bef9SDimitry Andric uint32_t IndirectSymtabSection::getNumSymbols() const {
1464bdd1243dSDimitry Andric uint32_t size = in.got->getEntries().size() +
1465bdd1243dSDimitry Andric in.tlvPointers->getEntries().size() +
1466bdd1243dSDimitry Andric in.stubs->getEntries().size();
1467bdd1243dSDimitry Andric if (!config->emitChainedFixups)
1468bdd1243dSDimitry Andric size += in.stubs->getEntries().size();
1469bdd1243dSDimitry Andric return size;
1470e8d8bef9SDimitry Andric }
1471e8d8bef9SDimitry Andric
isNeeded() const1472e8d8bef9SDimitry Andric bool IndirectSymtabSection::isNeeded() const {
1473e8d8bef9SDimitry Andric return in.got->isNeeded() || in.tlvPointers->isNeeded() ||
1474e8d8bef9SDimitry Andric in.stubs->isNeeded();
1475e8d8bef9SDimitry Andric }
1476e8d8bef9SDimitry Andric
finalizeContents()1477e8d8bef9SDimitry Andric void IndirectSymtabSection::finalizeContents() {
1478e8d8bef9SDimitry Andric uint32_t off = 0;
1479e8d8bef9SDimitry Andric in.got->reserved1 = off;
1480e8d8bef9SDimitry Andric off += in.got->getEntries().size();
1481e8d8bef9SDimitry Andric in.tlvPointers->reserved1 = off;
1482e8d8bef9SDimitry Andric off += in.tlvPointers->getEntries().size();
1483fe6060f1SDimitry Andric in.stubs->reserved1 = off;
1484bdd1243dSDimitry Andric if (in.lazyPointers) {
1485fe6060f1SDimitry Andric off += in.stubs->getEntries().size();
1486fe6060f1SDimitry Andric in.lazyPointers->reserved1 = off;
1487fe6060f1SDimitry Andric }
1488bdd1243dSDimitry Andric }
1489fe6060f1SDimitry Andric
indirectValue(const Symbol * sym)1490fe6060f1SDimitry Andric static uint32_t indirectValue(const Symbol *sym) {
1491*0fca6ea1SDimitry Andric if (sym->symtabIndex == UINT32_MAX || !needsBinding(sym))
1492349cc55cSDimitry Andric return INDIRECT_SYMBOL_LOCAL;
1493349cc55cSDimitry Andric return sym->symtabIndex;
1494e8d8bef9SDimitry Andric }
1495e8d8bef9SDimitry Andric
writeTo(uint8_t * buf) const1496e8d8bef9SDimitry Andric void IndirectSymtabSection::writeTo(uint8_t *buf) const {
1497e8d8bef9SDimitry Andric uint32_t off = 0;
1498e8d8bef9SDimitry Andric for (const Symbol *sym : in.got->getEntries()) {
1499fe6060f1SDimitry Andric write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1500e8d8bef9SDimitry Andric ++off;
1501e8d8bef9SDimitry Andric }
1502e8d8bef9SDimitry Andric for (const Symbol *sym : in.tlvPointers->getEntries()) {
1503fe6060f1SDimitry Andric write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1504e8d8bef9SDimitry Andric ++off;
1505e8d8bef9SDimitry Andric }
1506e8d8bef9SDimitry Andric for (const Symbol *sym : in.stubs->getEntries()) {
1507fe6060f1SDimitry Andric write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1508fe6060f1SDimitry Andric ++off;
1509fe6060f1SDimitry Andric }
1510bdd1243dSDimitry Andric
1511bdd1243dSDimitry Andric if (in.lazyPointers) {
1512fe6060f1SDimitry Andric // There is a 1:1 correspondence between stubs and LazyPointerSection
1513fe6060f1SDimitry Andric // entries. But giving __stubs and __la_symbol_ptr the same reserved1
1514fe6060f1SDimitry Andric // (the offset into the indirect symbol table) so that they both refer
1515fe6060f1SDimitry Andric // to the same range of offsets confuses `strip`, so write the stubs
1516fe6060f1SDimitry Andric // symbol table offsets a second time.
1517fe6060f1SDimitry Andric for (const Symbol *sym : in.stubs->getEntries()) {
1518fe6060f1SDimitry Andric write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1519e8d8bef9SDimitry Andric ++off;
1520e8d8bef9SDimitry Andric }
1521e8d8bef9SDimitry Andric }
1522bdd1243dSDimitry Andric }
1523e8d8bef9SDimitry Andric
StringTableSection()15245ffd83dbSDimitry Andric StringTableSection::StringTableSection()
1525e8d8bef9SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::stringTable) {}
15265ffd83dbSDimitry Andric
addString(StringRef str)15275ffd83dbSDimitry Andric uint32_t StringTableSection::addString(StringRef str) {
15285ffd83dbSDimitry Andric uint32_t strx = size;
1529e8d8bef9SDimitry Andric strings.push_back(str); // TODO: consider deduplicating strings
15305ffd83dbSDimitry Andric size += str.size() + 1; // account for null terminator
15315ffd83dbSDimitry Andric return strx;
15325ffd83dbSDimitry Andric }
15335ffd83dbSDimitry Andric
writeTo(uint8_t * buf) const15345ffd83dbSDimitry Andric void StringTableSection::writeTo(uint8_t *buf) const {
15355ffd83dbSDimitry Andric uint32_t off = 0;
15365ffd83dbSDimitry Andric for (StringRef str : strings) {
15375ffd83dbSDimitry Andric memcpy(buf + off, str.data(), str.size());
15385ffd83dbSDimitry Andric off += str.size() + 1; // account for null terminator
15395ffd83dbSDimitry Andric }
15405ffd83dbSDimitry Andric }
1541fe6060f1SDimitry Andric
1542bdd1243dSDimitry Andric static_assert((CodeSignatureSection::blobHeadersSize % 8) == 0);
1543bdd1243dSDimitry Andric static_assert((CodeSignatureSection::fixedHeadersSize % 8) == 0);
1544fe6060f1SDimitry Andric
CodeSignatureSection()1545fe6060f1SDimitry Andric CodeSignatureSection::CodeSignatureSection()
1546fe6060f1SDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::codeSignature) {
1547fe6060f1SDimitry Andric align = 16; // required by libstuff
154806c3fb27SDimitry Andric
154906c3fb27SDimitry Andric // XXX: This mimics LD64, where it uses the install-name as codesign
155006c3fb27SDimitry Andric // identifier, if available.
155106c3fb27SDimitry Andric if (!config->installName.empty())
155206c3fb27SDimitry Andric fileName = config->installName;
155306c3fb27SDimitry Andric else
1554fe6060f1SDimitry Andric // FIXME: Consider using finalOutput instead of outputFile.
1555fe6060f1SDimitry Andric fileName = config->outputFile;
155606c3fb27SDimitry Andric
1557fe6060f1SDimitry Andric size_t slashIndex = fileName.rfind("/");
1558fe6060f1SDimitry Andric if (slashIndex != std::string::npos)
1559fe6060f1SDimitry Andric fileName = fileName.drop_front(slashIndex + 1);
1560349cc55cSDimitry Andric
1561349cc55cSDimitry Andric // NOTE: Any changes to these calculations should be repeated
1562349cc55cSDimitry Andric // in llvm-objcopy's MachOLayoutBuilder::layoutTail.
1563fe6060f1SDimitry Andric allHeadersSize = alignTo<16>(fixedHeadersSize + fileName.size() + 1);
1564fe6060f1SDimitry Andric fileNamePad = allHeadersSize - fixedHeadersSize - fileName.size();
1565fe6060f1SDimitry Andric }
1566fe6060f1SDimitry Andric
getBlockCount() const1567fe6060f1SDimitry Andric uint32_t CodeSignatureSection::getBlockCount() const {
1568fe6060f1SDimitry Andric return (fileOff + blockSize - 1) / blockSize;
1569fe6060f1SDimitry Andric }
1570fe6060f1SDimitry Andric
getRawSize() const1571fe6060f1SDimitry Andric uint64_t CodeSignatureSection::getRawSize() const {
1572fe6060f1SDimitry Andric return allHeadersSize + getBlockCount() * hashSize;
1573fe6060f1SDimitry Andric }
1574fe6060f1SDimitry Andric
writeHashes(uint8_t * buf) const1575fe6060f1SDimitry Andric void CodeSignatureSection::writeHashes(uint8_t *buf) const {
1576349cc55cSDimitry Andric // NOTE: Changes to this functionality should be repeated in llvm-objcopy's
1577349cc55cSDimitry Andric // MachOWriter::writeSignatureData.
157881ad6265SDimitry Andric uint8_t *hashes = buf + fileOff + allHeadersSize;
157981ad6265SDimitry Andric parallelFor(0, getBlockCount(), [&](size_t i) {
158081ad6265SDimitry Andric sha256(buf + i * blockSize,
1581bdd1243dSDimitry Andric std::min(static_cast<size_t>(fileOff - i * blockSize), blockSize),
158281ad6265SDimitry Andric hashes + i * hashSize);
158381ad6265SDimitry Andric });
1584fe6060f1SDimitry Andric #if defined(__APPLE__)
1585fe6060f1SDimitry Andric // This is macOS-specific work-around and makes no sense for any
1586fe6060f1SDimitry Andric // other host OS. See https://openradar.appspot.com/FB8914231
1587fe6060f1SDimitry Andric //
1588fe6060f1SDimitry Andric // The macOS kernel maintains a signature-verification cache to
1589fe6060f1SDimitry Andric // quickly validate applications at time of execve(2). The trouble
1590fe6060f1SDimitry Andric // is that for the kernel creates the cache entry at the time of the
1591fe6060f1SDimitry Andric // mmap(2) call, before we have a chance to write either the code to
1592fe6060f1SDimitry Andric // sign or the signature header+hashes. The fix is to invalidate
1593fe6060f1SDimitry Andric // all cached data associated with the output file, thus discarding
1594fe6060f1SDimitry Andric // the bogus prematurely-cached signature.
1595fe6060f1SDimitry Andric msync(buf, fileOff + getSize(), MS_INVALIDATE);
1596fe6060f1SDimitry Andric #endif
1597fe6060f1SDimitry Andric }
1598fe6060f1SDimitry Andric
writeTo(uint8_t * buf) const1599fe6060f1SDimitry Andric void CodeSignatureSection::writeTo(uint8_t *buf) const {
1600349cc55cSDimitry Andric // NOTE: Changes to this functionality should be repeated in llvm-objcopy's
1601349cc55cSDimitry Andric // MachOWriter::writeSignatureData.
1602fe6060f1SDimitry Andric uint32_t signatureSize = static_cast<uint32_t>(getSize());
1603fe6060f1SDimitry Andric auto *superBlob = reinterpret_cast<CS_SuperBlob *>(buf);
1604fe6060f1SDimitry Andric write32be(&superBlob->magic, CSMAGIC_EMBEDDED_SIGNATURE);
1605fe6060f1SDimitry Andric write32be(&superBlob->length, signatureSize);
1606fe6060f1SDimitry Andric write32be(&superBlob->count, 1);
1607fe6060f1SDimitry Andric auto *blobIndex = reinterpret_cast<CS_BlobIndex *>(&superBlob[1]);
1608fe6060f1SDimitry Andric write32be(&blobIndex->type, CSSLOT_CODEDIRECTORY);
1609fe6060f1SDimitry Andric write32be(&blobIndex->offset, blobHeadersSize);
1610fe6060f1SDimitry Andric auto *codeDirectory =
1611fe6060f1SDimitry Andric reinterpret_cast<CS_CodeDirectory *>(buf + blobHeadersSize);
1612fe6060f1SDimitry Andric write32be(&codeDirectory->magic, CSMAGIC_CODEDIRECTORY);
1613fe6060f1SDimitry Andric write32be(&codeDirectory->length, signatureSize - blobHeadersSize);
1614fe6060f1SDimitry Andric write32be(&codeDirectory->version, CS_SUPPORTSEXECSEG);
1615fe6060f1SDimitry Andric write32be(&codeDirectory->flags, CS_ADHOC | CS_LINKER_SIGNED);
1616fe6060f1SDimitry Andric write32be(&codeDirectory->hashOffset,
1617fe6060f1SDimitry Andric sizeof(CS_CodeDirectory) + fileName.size() + fileNamePad);
1618fe6060f1SDimitry Andric write32be(&codeDirectory->identOffset, sizeof(CS_CodeDirectory));
1619fe6060f1SDimitry Andric codeDirectory->nSpecialSlots = 0;
1620fe6060f1SDimitry Andric write32be(&codeDirectory->nCodeSlots, getBlockCount());
1621fe6060f1SDimitry Andric write32be(&codeDirectory->codeLimit, fileOff);
1622fe6060f1SDimitry Andric codeDirectory->hashSize = static_cast<uint8_t>(hashSize);
1623fe6060f1SDimitry Andric codeDirectory->hashType = kSecCodeSignatureHashSHA256;
1624fe6060f1SDimitry Andric codeDirectory->platform = 0;
1625fe6060f1SDimitry Andric codeDirectory->pageSize = blockSizeShift;
1626fe6060f1SDimitry Andric codeDirectory->spare2 = 0;
1627fe6060f1SDimitry Andric codeDirectory->scatterOffset = 0;
1628fe6060f1SDimitry Andric codeDirectory->teamOffset = 0;
1629fe6060f1SDimitry Andric codeDirectory->spare3 = 0;
1630fe6060f1SDimitry Andric codeDirectory->codeLimit64 = 0;
1631fe6060f1SDimitry Andric OutputSegment *textSeg = getOrCreateOutputSegment(segment_names::text);
1632fe6060f1SDimitry Andric write64be(&codeDirectory->execSegBase, textSeg->fileOff);
1633fe6060f1SDimitry Andric write64be(&codeDirectory->execSegLimit, textSeg->fileSize);
1634fe6060f1SDimitry Andric write64be(&codeDirectory->execSegFlags,
1635fe6060f1SDimitry Andric config->outputType == MH_EXECUTE ? CS_EXECSEG_MAIN_BINARY : 0);
1636fe6060f1SDimitry Andric auto *id = reinterpret_cast<char *>(&codeDirectory[1]);
1637fe6060f1SDimitry Andric memcpy(id, fileName.begin(), fileName.size());
1638fe6060f1SDimitry Andric memset(id + fileName.size(), 0, fileNamePad);
1639fe6060f1SDimitry Andric }
1640fe6060f1SDimitry Andric
CStringSection(const char * name)1641bdd1243dSDimitry Andric CStringSection::CStringSection(const char *name)
1642bdd1243dSDimitry Andric : SyntheticSection(segment_names::text, name) {
1643fe6060f1SDimitry Andric flags = S_CSTRING_LITERALS;
1644fe6060f1SDimitry Andric }
1645fe6060f1SDimitry Andric
addInput(CStringInputSection * isec)1646fe6060f1SDimitry Andric void CStringSection::addInput(CStringInputSection *isec) {
1647fe6060f1SDimitry Andric isec->parent = this;
1648fe6060f1SDimitry Andric inputs.push_back(isec);
1649fe6060f1SDimitry Andric if (isec->align > align)
1650fe6060f1SDimitry Andric align = isec->align;
1651fe6060f1SDimitry Andric }
1652fe6060f1SDimitry Andric
writeTo(uint8_t * buf) const1653fe6060f1SDimitry Andric void CStringSection::writeTo(uint8_t *buf) const {
1654fe6060f1SDimitry Andric for (const CStringInputSection *isec : inputs) {
1655bdd1243dSDimitry Andric for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1656bdd1243dSDimitry Andric if (!piece.live)
1657fe6060f1SDimitry Andric continue;
1658fe6060f1SDimitry Andric StringRef string = isec->getStringRef(i);
1659bdd1243dSDimitry Andric memcpy(buf + piece.outSecOff, string.data(), string.size());
1660fe6060f1SDimitry Andric }
1661fe6060f1SDimitry Andric }
1662fe6060f1SDimitry Andric }
1663fe6060f1SDimitry Andric
finalizeContents()1664fe6060f1SDimitry Andric void CStringSection::finalizeContents() {
1665fe6060f1SDimitry Andric uint64_t offset = 0;
1666fe6060f1SDimitry Andric for (CStringInputSection *isec : inputs) {
1667bdd1243dSDimitry Andric for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1668bdd1243dSDimitry Andric if (!piece.live)
1669fe6060f1SDimitry Andric continue;
167081ad6265SDimitry Andric // See comment above DeduplicatedCStringSection for how alignment is
167181ad6265SDimitry Andric // handled.
1672bdd1243dSDimitry Andric uint32_t pieceAlign = 1
167306c3fb27SDimitry Andric << llvm::countr_zero(isec->align | piece.inSecOff);
167406c3fb27SDimitry Andric offset = alignToPowerOf2(offset, pieceAlign);
1675bdd1243dSDimitry Andric piece.outSecOff = offset;
1676fe6060f1SDimitry Andric isec->isFinal = true;
1677fe6060f1SDimitry Andric StringRef string = isec->getStringRef(i);
1678bdd1243dSDimitry Andric offset += string.size() + 1; // account for null terminator
1679fe6060f1SDimitry Andric }
1680fe6060f1SDimitry Andric }
1681fe6060f1SDimitry Andric size = offset;
1682fe6060f1SDimitry Andric }
168381ad6265SDimitry Andric
1684fe6060f1SDimitry Andric // Mergeable cstring literals are found under the __TEXT,__cstring section. In
1685fe6060f1SDimitry Andric // contrast to ELF, which puts strings that need different alignments into
1686fe6060f1SDimitry Andric // different sections, clang's Mach-O backend puts them all in one section.
1687fe6060f1SDimitry Andric // Strings that need to be aligned have the .p2align directive emitted before
168881ad6265SDimitry Andric // them, which simply translates into zero padding in the object file. In other
168981ad6265SDimitry Andric // words, we have to infer the desired alignment of these cstrings from their
169081ad6265SDimitry Andric // addresses.
1691fe6060f1SDimitry Andric //
169281ad6265SDimitry Andric // We differ slightly from ld64 in how we've chosen to align these cstrings.
169381ad6265SDimitry Andric // Both LLD and ld64 preserve the number of trailing zeros in each cstring's
169481ad6265SDimitry Andric // address in the input object files. When deduplicating identical cstrings,
169581ad6265SDimitry Andric // both linkers pick the cstring whose address has more trailing zeros, and
169681ad6265SDimitry Andric // preserve the alignment of that address in the final binary. However, ld64
169781ad6265SDimitry Andric // goes a step further and also preserves the offset of the cstring from the
169881ad6265SDimitry Andric // last section-aligned address. I.e. if a cstring is at offset 18 in the
169981ad6265SDimitry Andric // input, with a section alignment of 16, then both LLD and ld64 will ensure the
170081ad6265SDimitry Andric // final address is 2-byte aligned (since 18 == 16 + 2). But ld64 will also
170181ad6265SDimitry Andric // ensure that the final address is of the form 16 * k + 2 for some k.
1702fe6060f1SDimitry Andric //
170381ad6265SDimitry Andric // Note that ld64's heuristic means that a dedup'ed cstring's final address is
170481ad6265SDimitry Andric // dependent on the order of the input object files. E.g. if in addition to the
170581ad6265SDimitry Andric // cstring at offset 18 above, we have a duplicate one in another file with a
170681ad6265SDimitry Andric // `.cstring` section alignment of 2 and an offset of zero, then ld64 will pick
170781ad6265SDimitry Andric // the cstring from the object file earlier on the command line (since both have
170881ad6265SDimitry Andric // the same number of trailing zeros in their address). So the final cstring may
170981ad6265SDimitry Andric // either be at some address `16 * k + 2` or at some address `2 * k`.
171081ad6265SDimitry Andric //
171181ad6265SDimitry Andric // I've opted not to follow this behavior primarily for implementation
171281ad6265SDimitry Andric // simplicity, and secondarily to save a few more bytes. It's not clear to me
171381ad6265SDimitry Andric // that preserving the section alignment + offset is ever necessary, and there
171481ad6265SDimitry Andric // are many cases that are clearly redundant. In particular, if an x86_64 object
171581ad6265SDimitry Andric // file contains some strings that are accessed via SIMD instructions, then the
171681ad6265SDimitry Andric // .cstring section in the object file will be 16-byte-aligned (since SIMD
171781ad6265SDimitry Andric // requires its operand addresses to be 16-byte aligned). However, there will
171881ad6265SDimitry Andric // typically also be other cstrings in the same file that aren't used via SIMD
171981ad6265SDimitry Andric // and don't need this alignment. They will be emitted at some arbitrary address
172081ad6265SDimitry Andric // `A`, but ld64 will treat them as being 16-byte aligned with an offset of `16
172181ad6265SDimitry Andric // % A`.
finalizeContents()1722fe6060f1SDimitry Andric void DeduplicatedCStringSection::finalizeContents() {
172381ad6265SDimitry Andric // Find the largest alignment required for each string.
172481ad6265SDimitry Andric for (const CStringInputSection *isec : inputs) {
1725bdd1243dSDimitry Andric for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
172681ad6265SDimitry Andric if (!piece.live)
172781ad6265SDimitry Andric continue;
172881ad6265SDimitry Andric auto s = isec->getCachedHashStringRef(i);
172981ad6265SDimitry Andric assert(isec->align != 0);
173006c3fb27SDimitry Andric uint8_t trailingZeros = llvm::countr_zero(isec->align | piece.inSecOff);
173181ad6265SDimitry Andric auto it = stringOffsetMap.insert(
173281ad6265SDimitry Andric std::make_pair(s, StringOffset(trailingZeros)));
173381ad6265SDimitry Andric if (!it.second && it.first->second.trailingZeros < trailingZeros)
173481ad6265SDimitry Andric it.first->second.trailingZeros = trailingZeros;
173581ad6265SDimitry Andric }
1736fe6060f1SDimitry Andric }
173704eeddc0SDimitry Andric
173881ad6265SDimitry Andric // Assign an offset for each string and save it to the corresponding
173981ad6265SDimitry Andric // StringPieces for easy access.
174081ad6265SDimitry Andric for (CStringInputSection *isec : inputs) {
1741bdd1243dSDimitry Andric for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1742bdd1243dSDimitry Andric if (!piece.live)
174381ad6265SDimitry Andric continue;
174481ad6265SDimitry Andric auto s = isec->getCachedHashStringRef(i);
174581ad6265SDimitry Andric auto it = stringOffsetMap.find(s);
174681ad6265SDimitry Andric assert(it != stringOffsetMap.end());
174781ad6265SDimitry Andric StringOffset &offsetInfo = it->second;
174881ad6265SDimitry Andric if (offsetInfo.outSecOff == UINT64_MAX) {
174906c3fb27SDimitry Andric offsetInfo.outSecOff =
175006c3fb27SDimitry Andric alignToPowerOf2(size, 1ULL << offsetInfo.trailingZeros);
1751bdd1243dSDimitry Andric size =
1752bdd1243dSDimitry Andric offsetInfo.outSecOff + s.size() + 1; // account for null terminator
175381ad6265SDimitry Andric }
1754bdd1243dSDimitry Andric piece.outSecOff = offsetInfo.outSecOff;
175581ad6265SDimitry Andric }
175681ad6265SDimitry Andric isec->isFinal = true;
175781ad6265SDimitry Andric }
175881ad6265SDimitry Andric }
175981ad6265SDimitry Andric
writeTo(uint8_t * buf) const176081ad6265SDimitry Andric void DeduplicatedCStringSection::writeTo(uint8_t *buf) const {
176181ad6265SDimitry Andric for (const auto &p : stringOffsetMap) {
176281ad6265SDimitry Andric StringRef data = p.first.val();
176381ad6265SDimitry Andric uint64_t off = p.second.outSecOff;
176481ad6265SDimitry Andric if (!data.empty())
176581ad6265SDimitry Andric memcpy(buf + off, data.data(), data.size());
176681ad6265SDimitry Andric }
1767fe6060f1SDimitry Andric }
1768fe6060f1SDimitry Andric
1769bdd1243dSDimitry Andric DeduplicatedCStringSection::StringOffset
getStringOffset(StringRef str) const1770bdd1243dSDimitry Andric DeduplicatedCStringSection::getStringOffset(StringRef str) const {
1771bdd1243dSDimitry Andric // StringPiece uses 31 bits to store the hashes, so we replicate that
177206c3fb27SDimitry Andric uint32_t hash = xxh3_64bits(str) & 0x7fffffff;
1773bdd1243dSDimitry Andric auto offset = stringOffsetMap.find(CachedHashStringRef(str, hash));
1774bdd1243dSDimitry Andric assert(offset != stringOffsetMap.end() &&
1775bdd1243dSDimitry Andric "Looked-up strings should always exist in section");
1776bdd1243dSDimitry Andric return offset->second;
1777bdd1243dSDimitry Andric }
1778bdd1243dSDimitry Andric
1779fe6060f1SDimitry Andric // This section is actually emitted as __TEXT,__const by ld64, but clang may
1780fe6060f1SDimitry Andric // emit input sections of that name, and LLD doesn't currently support mixing
1781fe6060f1SDimitry Andric // synthetic and concat-type OutputSections. To work around this, I've given
1782fe6060f1SDimitry Andric // our merged-literals section a different name.
WordLiteralSection()1783fe6060f1SDimitry Andric WordLiteralSection::WordLiteralSection()
1784fe6060f1SDimitry Andric : SyntheticSection(segment_names::text, section_names::literals) {
1785fe6060f1SDimitry Andric align = 16;
1786fe6060f1SDimitry Andric }
1787fe6060f1SDimitry Andric
addInput(WordLiteralInputSection * isec)1788fe6060f1SDimitry Andric void WordLiteralSection::addInput(WordLiteralInputSection *isec) {
1789fe6060f1SDimitry Andric isec->parent = this;
1790fe6060f1SDimitry Andric inputs.push_back(isec);
1791fe6060f1SDimitry Andric }
1792fe6060f1SDimitry Andric
finalizeContents()1793fe6060f1SDimitry Andric void WordLiteralSection::finalizeContents() {
1794fe6060f1SDimitry Andric for (WordLiteralInputSection *isec : inputs) {
1795fe6060f1SDimitry Andric // We do all processing of the InputSection here, so it will be effectively
1796fe6060f1SDimitry Andric // finalized.
1797fe6060f1SDimitry Andric isec->isFinal = true;
1798fe6060f1SDimitry Andric const uint8_t *buf = isec->data.data();
1799fe6060f1SDimitry Andric switch (sectionType(isec->getFlags())) {
1800fe6060f1SDimitry Andric case S_4BYTE_LITERALS: {
1801fe6060f1SDimitry Andric for (size_t off = 0, e = isec->data.size(); off < e; off += 4) {
1802fe6060f1SDimitry Andric if (!isec->isLive(off))
1803fe6060f1SDimitry Andric continue;
1804fe6060f1SDimitry Andric uint32_t value = *reinterpret_cast<const uint32_t *>(buf + off);
1805fe6060f1SDimitry Andric literal4Map.emplace(value, literal4Map.size());
1806fe6060f1SDimitry Andric }
1807fe6060f1SDimitry Andric break;
1808fe6060f1SDimitry Andric }
1809fe6060f1SDimitry Andric case S_8BYTE_LITERALS: {
1810fe6060f1SDimitry Andric for (size_t off = 0, e = isec->data.size(); off < e; off += 8) {
1811fe6060f1SDimitry Andric if (!isec->isLive(off))
1812fe6060f1SDimitry Andric continue;
1813fe6060f1SDimitry Andric uint64_t value = *reinterpret_cast<const uint64_t *>(buf + off);
1814fe6060f1SDimitry Andric literal8Map.emplace(value, literal8Map.size());
1815fe6060f1SDimitry Andric }
1816fe6060f1SDimitry Andric break;
1817fe6060f1SDimitry Andric }
1818fe6060f1SDimitry Andric case S_16BYTE_LITERALS: {
1819fe6060f1SDimitry Andric for (size_t off = 0, e = isec->data.size(); off < e; off += 16) {
1820fe6060f1SDimitry Andric if (!isec->isLive(off))
1821fe6060f1SDimitry Andric continue;
1822fe6060f1SDimitry Andric UInt128 value = *reinterpret_cast<const UInt128 *>(buf + off);
1823fe6060f1SDimitry Andric literal16Map.emplace(value, literal16Map.size());
1824fe6060f1SDimitry Andric }
1825fe6060f1SDimitry Andric break;
1826fe6060f1SDimitry Andric }
1827fe6060f1SDimitry Andric default:
1828fe6060f1SDimitry Andric llvm_unreachable("invalid literal section type");
1829fe6060f1SDimitry Andric }
1830fe6060f1SDimitry Andric }
1831fe6060f1SDimitry Andric }
1832fe6060f1SDimitry Andric
writeTo(uint8_t * buf) const1833fe6060f1SDimitry Andric void WordLiteralSection::writeTo(uint8_t *buf) const {
1834fe6060f1SDimitry Andric // Note that we don't attempt to do any endianness conversion in addInput(),
1835fe6060f1SDimitry Andric // so we don't do it here either -- just write out the original value,
1836fe6060f1SDimitry Andric // byte-for-byte.
1837fe6060f1SDimitry Andric for (const auto &p : literal16Map)
1838fe6060f1SDimitry Andric memcpy(buf + p.second * 16, &p.first, 16);
1839fe6060f1SDimitry Andric buf += literal16Map.size() * 16;
1840fe6060f1SDimitry Andric
1841fe6060f1SDimitry Andric for (const auto &p : literal8Map)
1842fe6060f1SDimitry Andric memcpy(buf + p.second * 8, &p.first, 8);
1843fe6060f1SDimitry Andric buf += literal8Map.size() * 8;
1844fe6060f1SDimitry Andric
1845fe6060f1SDimitry Andric for (const auto &p : literal4Map)
1846fe6060f1SDimitry Andric memcpy(buf + p.second * 4, &p.first, 4);
1847fe6060f1SDimitry Andric }
1848fe6060f1SDimitry Andric
ObjCImageInfoSection()1849fcaf7f86SDimitry Andric ObjCImageInfoSection::ObjCImageInfoSection()
1850fcaf7f86SDimitry Andric : SyntheticSection(segment_names::data, section_names::objCImageInfo) {}
1851fcaf7f86SDimitry Andric
1852fcaf7f86SDimitry Andric ObjCImageInfoSection::ImageInfo
parseImageInfo(const InputFile * file)1853fcaf7f86SDimitry Andric ObjCImageInfoSection::parseImageInfo(const InputFile *file) {
1854fcaf7f86SDimitry Andric ImageInfo info;
1855fcaf7f86SDimitry Andric ArrayRef<uint8_t> data = file->objCImageInfo;
1856fcaf7f86SDimitry Andric // The image info struct has the following layout:
1857fcaf7f86SDimitry Andric // struct {
1858fcaf7f86SDimitry Andric // uint32_t version;
1859fcaf7f86SDimitry Andric // uint32_t flags;
1860fcaf7f86SDimitry Andric // };
1861fcaf7f86SDimitry Andric if (data.size() < 8) {
1862fcaf7f86SDimitry Andric warn(toString(file) + ": invalid __objc_imageinfo size");
1863fcaf7f86SDimitry Andric return info;
1864fcaf7f86SDimitry Andric }
1865fcaf7f86SDimitry Andric
1866fcaf7f86SDimitry Andric auto *buf = reinterpret_cast<const uint32_t *>(data.data());
1867fcaf7f86SDimitry Andric if (read32le(buf) != 0) {
1868fcaf7f86SDimitry Andric warn(toString(file) + ": invalid __objc_imageinfo version");
1869fcaf7f86SDimitry Andric return info;
1870fcaf7f86SDimitry Andric }
1871fcaf7f86SDimitry Andric
1872fcaf7f86SDimitry Andric uint32_t flags = read32le(buf + 1);
1873fcaf7f86SDimitry Andric info.swiftVersion = (flags >> 8) & 0xff;
1874fcaf7f86SDimitry Andric info.hasCategoryClassProperties = flags & 0x40;
1875fcaf7f86SDimitry Andric return info;
1876fcaf7f86SDimitry Andric }
1877fcaf7f86SDimitry Andric
swiftVersionString(uint8_t version)1878fcaf7f86SDimitry Andric static std::string swiftVersionString(uint8_t version) {
1879fcaf7f86SDimitry Andric switch (version) {
1880fcaf7f86SDimitry Andric case 1:
1881fcaf7f86SDimitry Andric return "1.0";
1882fcaf7f86SDimitry Andric case 2:
1883fcaf7f86SDimitry Andric return "1.1";
1884fcaf7f86SDimitry Andric case 3:
1885fcaf7f86SDimitry Andric return "2.0";
1886fcaf7f86SDimitry Andric case 4:
1887fcaf7f86SDimitry Andric return "3.0";
1888fcaf7f86SDimitry Andric case 5:
1889fcaf7f86SDimitry Andric return "4.0";
1890fcaf7f86SDimitry Andric default:
1891fcaf7f86SDimitry Andric return ("0x" + Twine::utohexstr(version)).str();
1892fcaf7f86SDimitry Andric }
1893fcaf7f86SDimitry Andric }
1894fcaf7f86SDimitry Andric
1895fcaf7f86SDimitry Andric // Validate each object file's __objc_imageinfo and use them to generate the
1896fcaf7f86SDimitry Andric // image info for the output binary. Only two pieces of info are relevant:
1897fcaf7f86SDimitry Andric // 1. The Swift version (should be identical across inputs)
1898fcaf7f86SDimitry Andric // 2. `bool hasCategoryClassProperties` (true only if true for all inputs)
finalizeContents()1899fcaf7f86SDimitry Andric void ObjCImageInfoSection::finalizeContents() {
1900fcaf7f86SDimitry Andric assert(files.size() != 0); // should have already been checked via isNeeded()
1901fcaf7f86SDimitry Andric
1902fcaf7f86SDimitry Andric info.hasCategoryClassProperties = true;
1903fcaf7f86SDimitry Andric const InputFile *firstFile;
190406c3fb27SDimitry Andric for (const InputFile *file : files) {
1905fcaf7f86SDimitry Andric ImageInfo inputInfo = parseImageInfo(file);
1906fcaf7f86SDimitry Andric info.hasCategoryClassProperties &= inputInfo.hasCategoryClassProperties;
1907fcaf7f86SDimitry Andric
1908fcaf7f86SDimitry Andric // swiftVersion 0 means no Swift is present, so no version checking required
1909fcaf7f86SDimitry Andric if (inputInfo.swiftVersion == 0)
1910fcaf7f86SDimitry Andric continue;
1911fcaf7f86SDimitry Andric
1912fcaf7f86SDimitry Andric if (info.swiftVersion != 0 && info.swiftVersion != inputInfo.swiftVersion) {
1913fcaf7f86SDimitry Andric error("Swift version mismatch: " + toString(firstFile) + " has version " +
1914fcaf7f86SDimitry Andric swiftVersionString(info.swiftVersion) + " but " + toString(file) +
1915fcaf7f86SDimitry Andric " has version " + swiftVersionString(inputInfo.swiftVersion));
1916fcaf7f86SDimitry Andric } else {
1917fcaf7f86SDimitry Andric info.swiftVersion = inputInfo.swiftVersion;
1918fcaf7f86SDimitry Andric firstFile = file;
1919fcaf7f86SDimitry Andric }
1920fcaf7f86SDimitry Andric }
1921fcaf7f86SDimitry Andric }
1922fcaf7f86SDimitry Andric
writeTo(uint8_t * buf) const1923fcaf7f86SDimitry Andric void ObjCImageInfoSection::writeTo(uint8_t *buf) const {
1924fcaf7f86SDimitry Andric uint32_t flags = info.hasCategoryClassProperties ? 0x40 : 0x0;
1925fcaf7f86SDimitry Andric flags |= info.swiftVersion << 8;
1926fcaf7f86SDimitry Andric write32le(buf + 4, flags);
1927fcaf7f86SDimitry Andric }
1928fcaf7f86SDimitry Andric
InitOffsetsSection()1929bdd1243dSDimitry Andric InitOffsetsSection::InitOffsetsSection()
1930bdd1243dSDimitry Andric : SyntheticSection(segment_names::text, section_names::initOffsets) {
1931bdd1243dSDimitry Andric flags = S_INIT_FUNC_OFFSETS;
1932bdd1243dSDimitry Andric align = 4; // This section contains 32-bit integers.
1933bdd1243dSDimitry Andric }
1934bdd1243dSDimitry Andric
getSize() const1935bdd1243dSDimitry Andric uint64_t InitOffsetsSection::getSize() const {
1936bdd1243dSDimitry Andric size_t count = 0;
1937bdd1243dSDimitry Andric for (const ConcatInputSection *isec : sections)
1938bdd1243dSDimitry Andric count += isec->relocs.size();
1939bdd1243dSDimitry Andric return count * sizeof(uint32_t);
1940bdd1243dSDimitry Andric }
1941bdd1243dSDimitry Andric
writeTo(uint8_t * buf) const1942bdd1243dSDimitry Andric void InitOffsetsSection::writeTo(uint8_t *buf) const {
1943bdd1243dSDimitry Andric // FIXME: Add function specified by -init when that argument is implemented.
1944bdd1243dSDimitry Andric for (ConcatInputSection *isec : sections) {
1945bdd1243dSDimitry Andric for (const Reloc &rel : isec->relocs) {
1946bdd1243dSDimitry Andric const Symbol *referent = rel.referent.dyn_cast<Symbol *>();
1947bdd1243dSDimitry Andric assert(referent && "section relocation should have been rejected");
1948bdd1243dSDimitry Andric uint64_t offset = referent->getVA() - in.header->addr;
1949bdd1243dSDimitry Andric // FIXME: Can we handle this gracefully?
1950bdd1243dSDimitry Andric if (offset > UINT32_MAX)
1951bdd1243dSDimitry Andric fatal(isec->getLocation(rel.offset) + ": offset to initializer " +
1952bdd1243dSDimitry Andric referent->getName() + " (" + utohexstr(offset) +
1953bdd1243dSDimitry Andric ") does not fit in 32 bits");
1954bdd1243dSDimitry Andric
1955bdd1243dSDimitry Andric // Entries need to be added in the order they appear in the section, but
1956bdd1243dSDimitry Andric // relocations aren't guaranteed to be sorted.
1957bdd1243dSDimitry Andric size_t index = rel.offset >> target->p2WordSize;
1958bdd1243dSDimitry Andric write32le(&buf[index * sizeof(uint32_t)], offset);
1959bdd1243dSDimitry Andric }
1960bdd1243dSDimitry Andric buf += isec->relocs.size() * sizeof(uint32_t);
1961bdd1243dSDimitry Andric }
1962bdd1243dSDimitry Andric }
1963bdd1243dSDimitry Andric
1964bdd1243dSDimitry Andric // The inputs are __mod_init_func sections, which contain pointers to
1965bdd1243dSDimitry Andric // initializer functions, therefore all relocations should be of the UNSIGNED
1966bdd1243dSDimitry Andric // type. InitOffsetsSection stores offsets, so if the initializer's address is
1967bdd1243dSDimitry Andric // not known at link time, stub-indirection has to be used.
setUp()1968bdd1243dSDimitry Andric void InitOffsetsSection::setUp() {
1969bdd1243dSDimitry Andric for (const ConcatInputSection *isec : sections) {
1970bdd1243dSDimitry Andric for (const Reloc &rel : isec->relocs) {
1971bdd1243dSDimitry Andric RelocAttrs attrs = target->getRelocAttrs(rel.type);
1972bdd1243dSDimitry Andric if (!attrs.hasAttr(RelocAttrBits::UNSIGNED))
1973bdd1243dSDimitry Andric error(isec->getLocation(rel.offset) +
1974bdd1243dSDimitry Andric ": unsupported relocation type: " + attrs.name);
1975bdd1243dSDimitry Andric if (rel.addend != 0)
1976bdd1243dSDimitry Andric error(isec->getLocation(rel.offset) +
1977bdd1243dSDimitry Andric ": relocation addend is not representable in __init_offsets");
1978bdd1243dSDimitry Andric if (rel.referent.is<InputSection *>())
1979bdd1243dSDimitry Andric error(isec->getLocation(rel.offset) +
1980bdd1243dSDimitry Andric ": unexpected section relocation");
1981bdd1243dSDimitry Andric
1982bdd1243dSDimitry Andric Symbol *sym = rel.referent.dyn_cast<Symbol *>();
1983bdd1243dSDimitry Andric if (auto *undefined = dyn_cast<Undefined>(sym))
1984bdd1243dSDimitry Andric treatUndefinedSymbol(*undefined, isec, rel.offset);
1985bdd1243dSDimitry Andric if (needsBinding(sym))
1986bdd1243dSDimitry Andric in.stubs->addEntry(sym);
1987bdd1243dSDimitry Andric }
1988bdd1243dSDimitry Andric }
1989bdd1243dSDimitry Andric }
1990bdd1243dSDimitry Andric
ObjCMethListSection()1991*0fca6ea1SDimitry Andric ObjCMethListSection::ObjCMethListSection()
1992*0fca6ea1SDimitry Andric : SyntheticSection(segment_names::text, section_names::objcMethList) {
1993*0fca6ea1SDimitry Andric flags = S_ATTR_NO_DEAD_STRIP;
1994*0fca6ea1SDimitry Andric align = relativeOffsetSize;
1995*0fca6ea1SDimitry Andric }
1996*0fca6ea1SDimitry Andric
1997*0fca6ea1SDimitry Andric // Go through all input method lists and ensure that we have selrefs for all
1998*0fca6ea1SDimitry Andric // their method names. The selrefs will be needed later by ::writeTo. We need to
1999*0fca6ea1SDimitry Andric // create them early on here to ensure they are processed correctly by the lld
2000*0fca6ea1SDimitry Andric // pipeline.
setUp()2001*0fca6ea1SDimitry Andric void ObjCMethListSection::setUp() {
2002*0fca6ea1SDimitry Andric for (const ConcatInputSection *isec : inputs) {
2003*0fca6ea1SDimitry Andric uint32_t structSizeAndFlags = 0, structCount = 0;
2004*0fca6ea1SDimitry Andric readMethodListHeader(isec->data.data(), structSizeAndFlags, structCount);
2005*0fca6ea1SDimitry Andric uint32_t originalStructSize = structSizeAndFlags & structSizeMask;
2006*0fca6ea1SDimitry Andric // Method name is immediately after header
2007*0fca6ea1SDimitry Andric uint32_t methodNameOff = methodListHeaderSize;
2008*0fca6ea1SDimitry Andric
2009*0fca6ea1SDimitry Andric // Loop through all methods, and ensure a selref for each of them exists.
2010*0fca6ea1SDimitry Andric while (methodNameOff < isec->data.size()) {
2011*0fca6ea1SDimitry Andric const Reloc *reloc = isec->getRelocAt(methodNameOff);
2012*0fca6ea1SDimitry Andric assert(reloc && "Relocation expected at method list name slot");
2013*0fca6ea1SDimitry Andric auto *def = dyn_cast_or_null<Defined>(reloc->referent.get<Symbol *>());
2014*0fca6ea1SDimitry Andric assert(def && "Expected valid Defined at method list name slot");
2015*0fca6ea1SDimitry Andric auto *cisec = cast<CStringInputSection>(def->isec());
2016*0fca6ea1SDimitry Andric assert(cisec && "Expected method name to be in a CStringInputSection");
2017*0fca6ea1SDimitry Andric auto methname = cisec->getStringRefAtOffset(def->value);
2018*0fca6ea1SDimitry Andric if (!ObjCSelRefsHelper::getSelRef(methname))
2019*0fca6ea1SDimitry Andric ObjCSelRefsHelper::makeSelRef(methname);
2020*0fca6ea1SDimitry Andric
2021*0fca6ea1SDimitry Andric // Jump to method name offset in next struct
2022*0fca6ea1SDimitry Andric methodNameOff += originalStructSize;
2023*0fca6ea1SDimitry Andric }
2024*0fca6ea1SDimitry Andric }
2025*0fca6ea1SDimitry Andric }
2026*0fca6ea1SDimitry Andric
2027*0fca6ea1SDimitry Andric // Calculate section size and final offsets for where InputSection's need to be
2028*0fca6ea1SDimitry Andric // written.
finalize()2029*0fca6ea1SDimitry Andric void ObjCMethListSection::finalize() {
2030*0fca6ea1SDimitry Andric // sectionSize will be the total size of the __objc_methlist section
2031*0fca6ea1SDimitry Andric sectionSize = 0;
2032*0fca6ea1SDimitry Andric for (ConcatInputSection *isec : inputs) {
2033*0fca6ea1SDimitry Andric // We can also use sectionSize as write offset for isec
2034*0fca6ea1SDimitry Andric assert(sectionSize == alignToPowerOf2(sectionSize, relativeOffsetSize) &&
2035*0fca6ea1SDimitry Andric "expected __objc_methlist to be aligned by default with the "
2036*0fca6ea1SDimitry Andric "required section alignment");
2037*0fca6ea1SDimitry Andric isec->outSecOff = sectionSize;
2038*0fca6ea1SDimitry Andric
2039*0fca6ea1SDimitry Andric isec->isFinal = true;
2040*0fca6ea1SDimitry Andric uint32_t relativeListSize =
2041*0fca6ea1SDimitry Andric computeRelativeMethodListSize(isec->data.size());
2042*0fca6ea1SDimitry Andric sectionSize += relativeListSize;
2043*0fca6ea1SDimitry Andric
2044*0fca6ea1SDimitry Andric // If encoding the method list in relative offset format shrinks the size,
2045*0fca6ea1SDimitry Andric // then we also need to adjust symbol sizes to match the new size. Note that
2046*0fca6ea1SDimitry Andric // on 32bit platforms the size of the method list will remain the same when
2047*0fca6ea1SDimitry Andric // encoded in relative offset format.
2048*0fca6ea1SDimitry Andric if (relativeListSize != isec->data.size()) {
2049*0fca6ea1SDimitry Andric for (Symbol *sym : isec->symbols) {
2050*0fca6ea1SDimitry Andric assert(isa<Defined>(sym) &&
2051*0fca6ea1SDimitry Andric "Unexpected undefined symbol in ObjC method list");
2052*0fca6ea1SDimitry Andric auto *def = cast<Defined>(sym);
2053*0fca6ea1SDimitry Andric // There can be 0-size symbols, check if this is the case and ignore
2054*0fca6ea1SDimitry Andric // them.
2055*0fca6ea1SDimitry Andric if (def->size) {
2056*0fca6ea1SDimitry Andric assert(
2057*0fca6ea1SDimitry Andric def->size == isec->data.size() &&
2058*0fca6ea1SDimitry Andric "Invalid ObjC method list symbol size: expected symbol size to "
2059*0fca6ea1SDimitry Andric "match isec size");
2060*0fca6ea1SDimitry Andric def->size = relativeListSize;
2061*0fca6ea1SDimitry Andric }
2062*0fca6ea1SDimitry Andric }
2063*0fca6ea1SDimitry Andric }
2064*0fca6ea1SDimitry Andric }
2065*0fca6ea1SDimitry Andric }
2066*0fca6ea1SDimitry Andric
writeTo(uint8_t * bufStart) const2067*0fca6ea1SDimitry Andric void ObjCMethListSection::writeTo(uint8_t *bufStart) const {
2068*0fca6ea1SDimitry Andric uint8_t *buf = bufStart;
2069*0fca6ea1SDimitry Andric for (const ConcatInputSection *isec : inputs) {
2070*0fca6ea1SDimitry Andric assert(buf - bufStart == long(isec->outSecOff) &&
2071*0fca6ea1SDimitry Andric "Writing at unexpected offset");
2072*0fca6ea1SDimitry Andric uint32_t writtenSize = writeRelativeMethodList(isec, buf);
2073*0fca6ea1SDimitry Andric buf += writtenSize;
2074*0fca6ea1SDimitry Andric }
2075*0fca6ea1SDimitry Andric assert(buf - bufStart == sectionSize &&
2076*0fca6ea1SDimitry Andric "Written size does not match expected section size");
2077*0fca6ea1SDimitry Andric }
2078*0fca6ea1SDimitry Andric
2079*0fca6ea1SDimitry Andric // Check if an InputSection is a method list. To do this we scan the
2080*0fca6ea1SDimitry Andric // InputSection for any symbols who's names match the patterns we expect clang
2081*0fca6ea1SDimitry Andric // to generate for method lists.
isMethodList(const ConcatInputSection * isec)2082*0fca6ea1SDimitry Andric bool ObjCMethListSection::isMethodList(const ConcatInputSection *isec) {
2083*0fca6ea1SDimitry Andric const char *symPrefixes[] = {objc::symbol_names::classMethods,
2084*0fca6ea1SDimitry Andric objc::symbol_names::instanceMethods,
2085*0fca6ea1SDimitry Andric objc::symbol_names::categoryInstanceMethods,
2086*0fca6ea1SDimitry Andric objc::symbol_names::categoryClassMethods};
2087*0fca6ea1SDimitry Andric if (!isec)
2088*0fca6ea1SDimitry Andric return false;
2089*0fca6ea1SDimitry Andric for (const Symbol *sym : isec->symbols) {
2090*0fca6ea1SDimitry Andric auto *def = dyn_cast_or_null<Defined>(sym);
2091*0fca6ea1SDimitry Andric if (!def)
2092*0fca6ea1SDimitry Andric continue;
2093*0fca6ea1SDimitry Andric for (const char *prefix : symPrefixes) {
2094*0fca6ea1SDimitry Andric if (def->getName().starts_with(prefix)) {
2095*0fca6ea1SDimitry Andric assert(def->size == isec->data.size() &&
2096*0fca6ea1SDimitry Andric "Invalid ObjC method list symbol size: expected symbol size to "
2097*0fca6ea1SDimitry Andric "match isec size");
2098*0fca6ea1SDimitry Andric assert(def->value == 0 &&
2099*0fca6ea1SDimitry Andric "Offset of ObjC method list symbol must be 0");
2100*0fca6ea1SDimitry Andric return true;
2101*0fca6ea1SDimitry Andric }
2102*0fca6ea1SDimitry Andric }
2103*0fca6ea1SDimitry Andric }
2104*0fca6ea1SDimitry Andric
2105*0fca6ea1SDimitry Andric return false;
2106*0fca6ea1SDimitry Andric }
2107*0fca6ea1SDimitry Andric
2108*0fca6ea1SDimitry Andric // Encode a single relative offset value. The input is the data/symbol at
2109*0fca6ea1SDimitry Andric // (&isec->data[inSecOff]). The output is written to (&buf[outSecOff]).
2110*0fca6ea1SDimitry Andric // 'createSelRef' indicates that we should not directly use the specified
2111*0fca6ea1SDimitry Andric // symbol, but instead get the selRef for the symbol and use that instead.
writeRelativeOffsetForIsec(const ConcatInputSection * isec,uint8_t * buf,uint32_t & inSecOff,uint32_t & outSecOff,bool useSelRef) const2112*0fca6ea1SDimitry Andric void ObjCMethListSection::writeRelativeOffsetForIsec(
2113*0fca6ea1SDimitry Andric const ConcatInputSection *isec, uint8_t *buf, uint32_t &inSecOff,
2114*0fca6ea1SDimitry Andric uint32_t &outSecOff, bool useSelRef) const {
2115*0fca6ea1SDimitry Andric const Reloc *reloc = isec->getRelocAt(inSecOff);
2116*0fca6ea1SDimitry Andric assert(reloc && "Relocation expected at __objc_methlist Offset");
2117*0fca6ea1SDimitry Andric auto *def = dyn_cast_or_null<Defined>(reloc->referent.get<Symbol *>());
2118*0fca6ea1SDimitry Andric assert(def && "Expected all syms in __objc_methlist to be defined");
2119*0fca6ea1SDimitry Andric uint32_t symVA = def->getVA();
2120*0fca6ea1SDimitry Andric
2121*0fca6ea1SDimitry Andric if (useSelRef) {
2122*0fca6ea1SDimitry Andric auto *cisec = cast<CStringInputSection>(def->isec());
2123*0fca6ea1SDimitry Andric auto methname = cisec->getStringRefAtOffset(def->value);
2124*0fca6ea1SDimitry Andric ConcatInputSection *selRef = ObjCSelRefsHelper::getSelRef(methname);
2125*0fca6ea1SDimitry Andric assert(selRef && "Expected all selector names to already be already be "
2126*0fca6ea1SDimitry Andric "present in __objc_selrefs");
2127*0fca6ea1SDimitry Andric symVA = selRef->getVA();
2128*0fca6ea1SDimitry Andric assert(selRef->data.size() == sizeof(target->wordSize) &&
2129*0fca6ea1SDimitry Andric "Expected one selref per ConcatInputSection");
2130*0fca6ea1SDimitry Andric }
2131*0fca6ea1SDimitry Andric
2132*0fca6ea1SDimitry Andric uint32_t currentVA = isec->getVA() + outSecOff;
2133*0fca6ea1SDimitry Andric uint32_t delta = symVA - currentVA;
2134*0fca6ea1SDimitry Andric write32le(buf + outSecOff, delta);
2135*0fca6ea1SDimitry Andric
2136*0fca6ea1SDimitry Andric // Move one pointer forward in the absolute method list
2137*0fca6ea1SDimitry Andric inSecOff += target->wordSize;
2138*0fca6ea1SDimitry Andric // Move one relative offset forward in the relative method list (32 bits)
2139*0fca6ea1SDimitry Andric outSecOff += relativeOffsetSize;
2140*0fca6ea1SDimitry Andric }
2141*0fca6ea1SDimitry Andric
2142*0fca6ea1SDimitry Andric // Write a relative method list to buf, return the size of the written
2143*0fca6ea1SDimitry Andric // information
2144*0fca6ea1SDimitry Andric uint32_t
writeRelativeMethodList(const ConcatInputSection * isec,uint8_t * buf) const2145*0fca6ea1SDimitry Andric ObjCMethListSection::writeRelativeMethodList(const ConcatInputSection *isec,
2146*0fca6ea1SDimitry Andric uint8_t *buf) const {
2147*0fca6ea1SDimitry Andric // Copy over the header, and add the "this is a relative method list" magic
2148*0fca6ea1SDimitry Andric // value flag
2149*0fca6ea1SDimitry Andric uint32_t structSizeAndFlags = 0, structCount = 0;
2150*0fca6ea1SDimitry Andric readMethodListHeader(isec->data.data(), structSizeAndFlags, structCount);
2151*0fca6ea1SDimitry Andric // Set the struct size for the relative method list
2152*0fca6ea1SDimitry Andric uint32_t relativeStructSizeAndFlags =
2153*0fca6ea1SDimitry Andric (relativeOffsetSize * pointersPerStruct) & structSizeMask;
2154*0fca6ea1SDimitry Andric // Carry over the old flags from the input struct
2155*0fca6ea1SDimitry Andric relativeStructSizeAndFlags |= structSizeAndFlags & structFlagsMask;
2156*0fca6ea1SDimitry Andric // Set the relative method list flag
2157*0fca6ea1SDimitry Andric relativeStructSizeAndFlags |= relMethodHeaderFlag;
2158*0fca6ea1SDimitry Andric
2159*0fca6ea1SDimitry Andric writeMethodListHeader(buf, relativeStructSizeAndFlags, structCount);
2160*0fca6ea1SDimitry Andric
2161*0fca6ea1SDimitry Andric assert(methodListHeaderSize +
2162*0fca6ea1SDimitry Andric (structCount * pointersPerStruct * target->wordSize) ==
2163*0fca6ea1SDimitry Andric isec->data.size() &&
2164*0fca6ea1SDimitry Andric "Invalid computed ObjC method list size");
2165*0fca6ea1SDimitry Andric
2166*0fca6ea1SDimitry Andric uint32_t inSecOff = methodListHeaderSize;
2167*0fca6ea1SDimitry Andric uint32_t outSecOff = methodListHeaderSize;
2168*0fca6ea1SDimitry Andric
2169*0fca6ea1SDimitry Andric // Go through the method list and encode input absolute pointers as relative
2170*0fca6ea1SDimitry Andric // offsets. writeRelativeOffsetForIsec will be incrementing inSecOff and
2171*0fca6ea1SDimitry Andric // outSecOff
2172*0fca6ea1SDimitry Andric for (uint32_t i = 0; i < structCount; i++) {
2173*0fca6ea1SDimitry Andric // Write the name of the method
2174*0fca6ea1SDimitry Andric writeRelativeOffsetForIsec(isec, buf, inSecOff, outSecOff, true);
2175*0fca6ea1SDimitry Andric // Write the type of the method
2176*0fca6ea1SDimitry Andric writeRelativeOffsetForIsec(isec, buf, inSecOff, outSecOff, false);
2177*0fca6ea1SDimitry Andric // Write reference to the selector of the method
2178*0fca6ea1SDimitry Andric writeRelativeOffsetForIsec(isec, buf, inSecOff, outSecOff, false);
2179*0fca6ea1SDimitry Andric }
2180*0fca6ea1SDimitry Andric
2181*0fca6ea1SDimitry Andric // Expecting to have read all the data in the isec
2182*0fca6ea1SDimitry Andric assert(inSecOff == isec->data.size() &&
2183*0fca6ea1SDimitry Andric "Invalid actual ObjC method list size");
2184*0fca6ea1SDimitry Andric assert(
2185*0fca6ea1SDimitry Andric outSecOff == computeRelativeMethodListSize(inSecOff) &&
2186*0fca6ea1SDimitry Andric "Mismatch between input & output size when writing relative method list");
2187*0fca6ea1SDimitry Andric return outSecOff;
2188*0fca6ea1SDimitry Andric }
2189*0fca6ea1SDimitry Andric
2190*0fca6ea1SDimitry Andric // Given the size of an ObjC method list InputSection, return the size of the
2191*0fca6ea1SDimitry Andric // method list when encoded in relative offsets format. We can do this without
2192*0fca6ea1SDimitry Andric // decoding the actual data, as it can be directly inferred from the size of the
2193*0fca6ea1SDimitry Andric // isec.
computeRelativeMethodListSize(uint32_t absoluteMethodListSize) const2194*0fca6ea1SDimitry Andric uint32_t ObjCMethListSection::computeRelativeMethodListSize(
2195*0fca6ea1SDimitry Andric uint32_t absoluteMethodListSize) const {
2196*0fca6ea1SDimitry Andric uint32_t oldPointersSize = absoluteMethodListSize - methodListHeaderSize;
2197*0fca6ea1SDimitry Andric uint32_t pointerCount = oldPointersSize / target->wordSize;
2198*0fca6ea1SDimitry Andric assert(((pointerCount % pointersPerStruct) == 0) &&
2199*0fca6ea1SDimitry Andric "__objc_methlist expects method lists to have multiple-of-3 pointers");
2200*0fca6ea1SDimitry Andric
2201*0fca6ea1SDimitry Andric uint32_t newPointersSize = pointerCount * relativeOffsetSize;
2202*0fca6ea1SDimitry Andric uint32_t newTotalSize = methodListHeaderSize + newPointersSize;
2203*0fca6ea1SDimitry Andric
2204*0fca6ea1SDimitry Andric assert((newTotalSize <= absoluteMethodListSize) &&
2205*0fca6ea1SDimitry Andric "Expected relative method list size to be smaller or equal than "
2206*0fca6ea1SDimitry Andric "original size");
2207*0fca6ea1SDimitry Andric return newTotalSize;
2208*0fca6ea1SDimitry Andric }
2209*0fca6ea1SDimitry Andric
2210*0fca6ea1SDimitry Andric // Read a method list header from buf
readMethodListHeader(const uint8_t * buf,uint32_t & structSizeAndFlags,uint32_t & structCount) const2211*0fca6ea1SDimitry Andric void ObjCMethListSection::readMethodListHeader(const uint8_t *buf,
2212*0fca6ea1SDimitry Andric uint32_t &structSizeAndFlags,
2213*0fca6ea1SDimitry Andric uint32_t &structCount) const {
2214*0fca6ea1SDimitry Andric structSizeAndFlags = read32le(buf);
2215*0fca6ea1SDimitry Andric structCount = read32le(buf + sizeof(uint32_t));
2216*0fca6ea1SDimitry Andric }
2217*0fca6ea1SDimitry Andric
2218*0fca6ea1SDimitry Andric // Write a method list header to buf
writeMethodListHeader(uint8_t * buf,uint32_t structSizeAndFlags,uint32_t structCount) const2219*0fca6ea1SDimitry Andric void ObjCMethListSection::writeMethodListHeader(uint8_t *buf,
2220*0fca6ea1SDimitry Andric uint32_t structSizeAndFlags,
2221*0fca6ea1SDimitry Andric uint32_t structCount) const {
2222*0fca6ea1SDimitry Andric write32le(buf, structSizeAndFlags);
2223*0fca6ea1SDimitry Andric write32le(buf + sizeof(structSizeAndFlags), structCount);
2224*0fca6ea1SDimitry Andric }
2225*0fca6ea1SDimitry Andric
createSyntheticSymbols()2226fe6060f1SDimitry Andric void macho::createSyntheticSymbols() {
2227fe6060f1SDimitry Andric auto addHeaderSymbol = [](const char *name) {
2228fe6060f1SDimitry Andric symtab->addSynthetic(name, in.header->isec, /*value=*/0,
222904eeddc0SDimitry Andric /*isPrivateExtern=*/true, /*includeInSymtab=*/false,
2230fe6060f1SDimitry Andric /*referencedDynamically=*/false);
2231fe6060f1SDimitry Andric };
2232fe6060f1SDimitry Andric
2233fe6060f1SDimitry Andric switch (config->outputType) {
2234fe6060f1SDimitry Andric // FIXME: Assign the right address value for these symbols
2235fe6060f1SDimitry Andric // (rather than 0). But we need to do that after assignAddresses().
2236fe6060f1SDimitry Andric case MH_EXECUTE:
2237fe6060f1SDimitry Andric // If linking PIE, __mh_execute_header is a defined symbol in
2238fe6060f1SDimitry Andric // __TEXT, __text)
2239fe6060f1SDimitry Andric // Otherwise, it's an absolute symbol.
2240fe6060f1SDimitry Andric if (config->isPic)
2241fe6060f1SDimitry Andric symtab->addSynthetic("__mh_execute_header", in.header->isec, /*value=*/0,
224204eeddc0SDimitry Andric /*isPrivateExtern=*/false, /*includeInSymtab=*/true,
2243fe6060f1SDimitry Andric /*referencedDynamically=*/true);
2244fe6060f1SDimitry Andric else
2245fe6060f1SDimitry Andric symtab->addSynthetic("__mh_execute_header", /*isec=*/nullptr, /*value=*/0,
224604eeddc0SDimitry Andric /*isPrivateExtern=*/false, /*includeInSymtab=*/true,
2247fe6060f1SDimitry Andric /*referencedDynamically=*/true);
2248fe6060f1SDimitry Andric break;
2249fe6060f1SDimitry Andric
2250fe6060f1SDimitry Andric // The following symbols are N_SECT symbols, even though the header is not
2251fe6060f1SDimitry Andric // part of any section and that they are private to the bundle/dylib/object
2252fe6060f1SDimitry Andric // they are part of.
2253fe6060f1SDimitry Andric case MH_BUNDLE:
2254fe6060f1SDimitry Andric addHeaderSymbol("__mh_bundle_header");
2255fe6060f1SDimitry Andric break;
2256fe6060f1SDimitry Andric case MH_DYLIB:
2257fe6060f1SDimitry Andric addHeaderSymbol("__mh_dylib_header");
2258fe6060f1SDimitry Andric break;
2259fe6060f1SDimitry Andric case MH_DYLINKER:
2260fe6060f1SDimitry Andric addHeaderSymbol("__mh_dylinker_header");
2261fe6060f1SDimitry Andric break;
2262fe6060f1SDimitry Andric case MH_OBJECT:
2263fe6060f1SDimitry Andric addHeaderSymbol("__mh_object_header");
2264fe6060f1SDimitry Andric break;
2265fe6060f1SDimitry Andric default:
2266fe6060f1SDimitry Andric llvm_unreachable("unexpected outputType");
2267fe6060f1SDimitry Andric break;
2268fe6060f1SDimitry Andric }
2269fe6060f1SDimitry Andric
2270fe6060f1SDimitry Andric // The Itanium C++ ABI requires dylibs to pass a pointer to __cxa_atexit
2271fe6060f1SDimitry Andric // which does e.g. cleanup of static global variables. The ABI document
2272fe6060f1SDimitry Andric // says that the pointer can point to any address in one of the dylib's
2273fe6060f1SDimitry Andric // segments, but in practice ld64 seems to set it to point to the header,
2274fe6060f1SDimitry Andric // so that's what's implemented here.
2275fe6060f1SDimitry Andric addHeaderSymbol("___dso_handle");
2276fe6060f1SDimitry Andric }
2277fe6060f1SDimitry Andric
ChainedFixupsSection()2278bdd1243dSDimitry Andric ChainedFixupsSection::ChainedFixupsSection()
2279bdd1243dSDimitry Andric : LinkEditSection(segment_names::linkEdit, section_names::chainFixups) {}
2280bdd1243dSDimitry Andric
isNeeded() const2281bdd1243dSDimitry Andric bool ChainedFixupsSection::isNeeded() const {
2282bdd1243dSDimitry Andric assert(config->emitChainedFixups);
2283bdd1243dSDimitry Andric // dyld always expects LC_DYLD_CHAINED_FIXUPS to point to a valid
2284bdd1243dSDimitry Andric // dyld_chained_fixups_header, so we create this section even if there aren't
2285bdd1243dSDimitry Andric // any fixups.
2286bdd1243dSDimitry Andric return true;
2287bdd1243dSDimitry Andric }
2288bdd1243dSDimitry Andric
addBinding(const Symbol * sym,const InputSection * isec,uint64_t offset,int64_t addend)2289bdd1243dSDimitry Andric void ChainedFixupsSection::addBinding(const Symbol *sym,
2290bdd1243dSDimitry Andric const InputSection *isec, uint64_t offset,
2291bdd1243dSDimitry Andric int64_t addend) {
2292bdd1243dSDimitry Andric locations.emplace_back(isec, offset);
2293bdd1243dSDimitry Andric int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0;
2294bdd1243dSDimitry Andric auto [it, inserted] = bindings.insert(
2295bdd1243dSDimitry Andric {{sym, outlineAddend}, static_cast<uint32_t>(bindings.size())});
2296bdd1243dSDimitry Andric
2297bdd1243dSDimitry Andric if (inserted) {
2298bdd1243dSDimitry Andric symtabSize += sym->getName().size() + 1;
2299bdd1243dSDimitry Andric hasWeakBind = hasWeakBind || needsWeakBind(*sym);
2300bdd1243dSDimitry Andric if (!isInt<23>(outlineAddend))
2301bdd1243dSDimitry Andric needsLargeAddend = true;
2302bdd1243dSDimitry Andric else if (outlineAddend != 0)
2303bdd1243dSDimitry Andric needsAddend = true;
2304bdd1243dSDimitry Andric }
2305bdd1243dSDimitry Andric }
2306bdd1243dSDimitry Andric
2307bdd1243dSDimitry Andric std::pair<uint32_t, uint8_t>
getBinding(const Symbol * sym,int64_t addend) const2308bdd1243dSDimitry Andric ChainedFixupsSection::getBinding(const Symbol *sym, int64_t addend) const {
2309bdd1243dSDimitry Andric int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0;
2310bdd1243dSDimitry Andric auto it = bindings.find({sym, outlineAddend});
2311bdd1243dSDimitry Andric assert(it != bindings.end() && "binding not found in the imports table");
2312bdd1243dSDimitry Andric if (outlineAddend == 0)
2313bdd1243dSDimitry Andric return {it->second, addend};
2314bdd1243dSDimitry Andric return {it->second, 0};
2315bdd1243dSDimitry Andric }
2316bdd1243dSDimitry Andric
writeImport(uint8_t * buf,int format,int16_t libOrdinal,bool weakRef,uint32_t nameOffset,int64_t addend)2317*0fca6ea1SDimitry Andric static size_t writeImport(uint8_t *buf, int format, int16_t libOrdinal,
2318bdd1243dSDimitry Andric bool weakRef, uint32_t nameOffset, int64_t addend) {
2319bdd1243dSDimitry Andric switch (format) {
2320bdd1243dSDimitry Andric case DYLD_CHAINED_IMPORT: {
2321bdd1243dSDimitry Andric auto *import = reinterpret_cast<dyld_chained_import *>(buf);
2322bdd1243dSDimitry Andric import->lib_ordinal = libOrdinal;
2323bdd1243dSDimitry Andric import->weak_import = weakRef;
2324bdd1243dSDimitry Andric import->name_offset = nameOffset;
2325bdd1243dSDimitry Andric return sizeof(dyld_chained_import);
2326bdd1243dSDimitry Andric }
2327bdd1243dSDimitry Andric case DYLD_CHAINED_IMPORT_ADDEND: {
2328bdd1243dSDimitry Andric auto *import = reinterpret_cast<dyld_chained_import_addend *>(buf);
2329bdd1243dSDimitry Andric import->lib_ordinal = libOrdinal;
2330bdd1243dSDimitry Andric import->weak_import = weakRef;
2331bdd1243dSDimitry Andric import->name_offset = nameOffset;
2332bdd1243dSDimitry Andric import->addend = addend;
2333bdd1243dSDimitry Andric return sizeof(dyld_chained_import_addend);
2334bdd1243dSDimitry Andric }
2335bdd1243dSDimitry Andric case DYLD_CHAINED_IMPORT_ADDEND64: {
2336bdd1243dSDimitry Andric auto *import = reinterpret_cast<dyld_chained_import_addend64 *>(buf);
2337bdd1243dSDimitry Andric import->lib_ordinal = libOrdinal;
2338bdd1243dSDimitry Andric import->weak_import = weakRef;
2339bdd1243dSDimitry Andric import->name_offset = nameOffset;
2340bdd1243dSDimitry Andric import->addend = addend;
2341bdd1243dSDimitry Andric return sizeof(dyld_chained_import_addend64);
2342bdd1243dSDimitry Andric }
2343bdd1243dSDimitry Andric default:
2344bdd1243dSDimitry Andric llvm_unreachable("Unknown import format");
2345bdd1243dSDimitry Andric }
2346bdd1243dSDimitry Andric }
2347bdd1243dSDimitry Andric
getSize() const2348bdd1243dSDimitry Andric size_t ChainedFixupsSection::SegmentInfo::getSize() const {
2349bdd1243dSDimitry Andric assert(pageStarts.size() > 0 && "SegmentInfo for segment with no fixups?");
2350bdd1243dSDimitry Andric return alignTo<8>(sizeof(dyld_chained_starts_in_segment) +
2351bdd1243dSDimitry Andric pageStarts.back().first * sizeof(uint16_t));
2352bdd1243dSDimitry Andric }
2353bdd1243dSDimitry Andric
writeTo(uint8_t * buf) const2354bdd1243dSDimitry Andric size_t ChainedFixupsSection::SegmentInfo::writeTo(uint8_t *buf) const {
2355bdd1243dSDimitry Andric auto *segInfo = reinterpret_cast<dyld_chained_starts_in_segment *>(buf);
2356bdd1243dSDimitry Andric segInfo->size = getSize();
2357bdd1243dSDimitry Andric segInfo->page_size = target->getPageSize();
2358bdd1243dSDimitry Andric // FIXME: Use DYLD_CHAINED_PTR_64_OFFSET on newer OS versions.
2359bdd1243dSDimitry Andric segInfo->pointer_format = DYLD_CHAINED_PTR_64;
2360bdd1243dSDimitry Andric segInfo->segment_offset = oseg->addr - in.header->addr;
2361bdd1243dSDimitry Andric segInfo->max_valid_pointer = 0; // not used on 64-bit
2362bdd1243dSDimitry Andric segInfo->page_count = pageStarts.back().first + 1;
2363bdd1243dSDimitry Andric
2364bdd1243dSDimitry Andric uint16_t *starts = segInfo->page_start;
2365bdd1243dSDimitry Andric for (size_t i = 0; i < segInfo->page_count; ++i)
2366bdd1243dSDimitry Andric starts[i] = DYLD_CHAINED_PTR_START_NONE;
2367bdd1243dSDimitry Andric
2368bdd1243dSDimitry Andric for (auto [pageIdx, startAddr] : pageStarts)
2369bdd1243dSDimitry Andric starts[pageIdx] = startAddr;
2370bdd1243dSDimitry Andric return segInfo->size;
2371bdd1243dSDimitry Andric }
2372bdd1243dSDimitry Andric
importEntrySize(int format)2373bdd1243dSDimitry Andric static size_t importEntrySize(int format) {
2374bdd1243dSDimitry Andric switch (format) {
2375bdd1243dSDimitry Andric case DYLD_CHAINED_IMPORT:
2376bdd1243dSDimitry Andric return sizeof(dyld_chained_import);
2377bdd1243dSDimitry Andric case DYLD_CHAINED_IMPORT_ADDEND:
2378bdd1243dSDimitry Andric return sizeof(dyld_chained_import_addend);
2379bdd1243dSDimitry Andric case DYLD_CHAINED_IMPORT_ADDEND64:
2380bdd1243dSDimitry Andric return sizeof(dyld_chained_import_addend64);
2381bdd1243dSDimitry Andric default:
2382bdd1243dSDimitry Andric llvm_unreachable("Unknown import format");
2383bdd1243dSDimitry Andric }
2384bdd1243dSDimitry Andric }
2385bdd1243dSDimitry Andric
2386bdd1243dSDimitry Andric // This is step 3 of the algorithm described in the class comment of
2387bdd1243dSDimitry Andric // ChainedFixupsSection.
2388bdd1243dSDimitry Andric //
2389bdd1243dSDimitry Andric // LC_DYLD_CHAINED_FIXUPS data consists of (in this order):
2390bdd1243dSDimitry Andric // * A dyld_chained_fixups_header
2391bdd1243dSDimitry Andric // * A dyld_chained_starts_in_image
2392bdd1243dSDimitry Andric // * One dyld_chained_starts_in_segment per segment
2393bdd1243dSDimitry Andric // * List of all imports (dyld_chained_import, dyld_chained_import_addend, or
2394bdd1243dSDimitry Andric // dyld_chained_import_addend64)
2395bdd1243dSDimitry Andric // * Names of imported symbols
writeTo(uint8_t * buf) const2396bdd1243dSDimitry Andric void ChainedFixupsSection::writeTo(uint8_t *buf) const {
2397bdd1243dSDimitry Andric auto *header = reinterpret_cast<dyld_chained_fixups_header *>(buf);
2398bdd1243dSDimitry Andric header->fixups_version = 0;
2399bdd1243dSDimitry Andric header->imports_count = bindings.size();
2400bdd1243dSDimitry Andric header->imports_format = importFormat;
2401bdd1243dSDimitry Andric header->symbols_format = 0;
2402bdd1243dSDimitry Andric
2403bdd1243dSDimitry Andric buf += alignTo<8>(sizeof(*header));
2404bdd1243dSDimitry Andric
2405bdd1243dSDimitry Andric auto curOffset = [&buf, &header]() -> uint32_t {
2406bdd1243dSDimitry Andric return buf - reinterpret_cast<uint8_t *>(header);
2407bdd1243dSDimitry Andric };
2408bdd1243dSDimitry Andric
2409bdd1243dSDimitry Andric header->starts_offset = curOffset();
2410bdd1243dSDimitry Andric
2411bdd1243dSDimitry Andric auto *imageInfo = reinterpret_cast<dyld_chained_starts_in_image *>(buf);
2412bdd1243dSDimitry Andric imageInfo->seg_count = outputSegments.size();
2413bdd1243dSDimitry Andric uint32_t *segStarts = imageInfo->seg_info_offset;
2414bdd1243dSDimitry Andric
2415bdd1243dSDimitry Andric // dyld_chained_starts_in_image ends in a flexible array member containing an
2416bdd1243dSDimitry Andric // uint32_t for each segment. Leave room for it, and fill it via segStarts.
2417bdd1243dSDimitry Andric buf += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset) +
2418bdd1243dSDimitry Andric outputSegments.size() * sizeof(uint32_t));
2419bdd1243dSDimitry Andric
2420bdd1243dSDimitry Andric // Initialize all offsets to 0, which indicates that the segment does not have
2421bdd1243dSDimitry Andric // fixups. Those that do have them will be filled in below.
2422bdd1243dSDimitry Andric for (size_t i = 0; i < outputSegments.size(); ++i)
2423bdd1243dSDimitry Andric segStarts[i] = 0;
2424bdd1243dSDimitry Andric
2425bdd1243dSDimitry Andric for (const SegmentInfo &seg : fixupSegments) {
2426bdd1243dSDimitry Andric segStarts[seg.oseg->index] = curOffset() - header->starts_offset;
2427bdd1243dSDimitry Andric buf += seg.writeTo(buf);
2428bdd1243dSDimitry Andric }
2429bdd1243dSDimitry Andric
2430bdd1243dSDimitry Andric // Write imports table.
2431bdd1243dSDimitry Andric header->imports_offset = curOffset();
2432bdd1243dSDimitry Andric uint64_t nameOffset = 0;
2433bdd1243dSDimitry Andric for (auto [import, idx] : bindings) {
2434bdd1243dSDimitry Andric const Symbol &sym = *import.first;
2435*0fca6ea1SDimitry Andric buf += writeImport(buf, importFormat, ordinalForSymbol(sym),
2436*0fca6ea1SDimitry Andric sym.isWeakRef(), nameOffset, import.second);
2437bdd1243dSDimitry Andric nameOffset += sym.getName().size() + 1;
2438bdd1243dSDimitry Andric }
2439bdd1243dSDimitry Andric
2440bdd1243dSDimitry Andric // Write imported symbol names.
2441bdd1243dSDimitry Andric header->symbols_offset = curOffset();
2442bdd1243dSDimitry Andric for (auto [import, idx] : bindings) {
2443bdd1243dSDimitry Andric StringRef name = import.first->getName();
2444bdd1243dSDimitry Andric memcpy(buf, name.data(), name.size());
2445bdd1243dSDimitry Andric buf += name.size() + 1; // account for null terminator
2446bdd1243dSDimitry Andric }
2447bdd1243dSDimitry Andric
2448bdd1243dSDimitry Andric assert(curOffset() == getRawSize());
2449bdd1243dSDimitry Andric }
2450bdd1243dSDimitry Andric
2451bdd1243dSDimitry Andric // This is step 2 of the algorithm described in the class comment of
2452bdd1243dSDimitry Andric // ChainedFixupsSection.
finalizeContents()2453bdd1243dSDimitry Andric void ChainedFixupsSection::finalizeContents() {
2454bdd1243dSDimitry Andric assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
2455bdd1243dSDimitry Andric assert(config->emitChainedFixups);
2456bdd1243dSDimitry Andric
2457bdd1243dSDimitry Andric if (!isUInt<32>(symtabSize))
2458bdd1243dSDimitry Andric error("cannot encode chained fixups: imported symbols table size " +
2459bdd1243dSDimitry Andric Twine(symtabSize) + " exceeds 4 GiB");
2460bdd1243dSDimitry Andric
2461*0fca6ea1SDimitry Andric bool needsLargeOrdinal = any_of(bindings, [](const auto &p) {
2462*0fca6ea1SDimitry Andric // 0xF1 - 0xFF are reserved for special ordinals in the 8-bit encoding.
2463*0fca6ea1SDimitry Andric return ordinalForSymbol(*p.first.first) > 0xF0;
2464*0fca6ea1SDimitry Andric });
2465*0fca6ea1SDimitry Andric
2466*0fca6ea1SDimitry Andric if (needsLargeAddend || !isUInt<23>(symtabSize) || needsLargeOrdinal)
2467bdd1243dSDimitry Andric importFormat = DYLD_CHAINED_IMPORT_ADDEND64;
2468bdd1243dSDimitry Andric else if (needsAddend)
2469bdd1243dSDimitry Andric importFormat = DYLD_CHAINED_IMPORT_ADDEND;
2470bdd1243dSDimitry Andric else
2471bdd1243dSDimitry Andric importFormat = DYLD_CHAINED_IMPORT;
2472bdd1243dSDimitry Andric
2473bdd1243dSDimitry Andric for (Location &loc : locations)
2474bdd1243dSDimitry Andric loc.offset =
2475bdd1243dSDimitry Andric loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset);
2476bdd1243dSDimitry Andric
2477bdd1243dSDimitry Andric llvm::sort(locations, [](const Location &a, const Location &b) {
2478bdd1243dSDimitry Andric const OutputSegment *segA = a.isec->parent->parent;
2479bdd1243dSDimitry Andric const OutputSegment *segB = b.isec->parent->parent;
2480bdd1243dSDimitry Andric if (segA == segB)
2481bdd1243dSDimitry Andric return a.offset < b.offset;
2482bdd1243dSDimitry Andric return segA->addr < segB->addr;
2483bdd1243dSDimitry Andric });
2484bdd1243dSDimitry Andric
2485bdd1243dSDimitry Andric auto sameSegment = [](const Location &a, const Location &b) {
2486bdd1243dSDimitry Andric return a.isec->parent->parent == b.isec->parent->parent;
2487bdd1243dSDimitry Andric };
2488bdd1243dSDimitry Andric
2489bdd1243dSDimitry Andric const uint64_t pageSize = target->getPageSize();
2490bdd1243dSDimitry Andric for (size_t i = 0, count = locations.size(); i < count;) {
2491bdd1243dSDimitry Andric const Location &firstLoc = locations[i];
2492bdd1243dSDimitry Andric fixupSegments.emplace_back(firstLoc.isec->parent->parent);
2493bdd1243dSDimitry Andric while (i < count && sameSegment(locations[i], firstLoc)) {
2494bdd1243dSDimitry Andric uint32_t pageIdx = locations[i].offset / pageSize;
2495bdd1243dSDimitry Andric fixupSegments.back().pageStarts.emplace_back(
2496bdd1243dSDimitry Andric pageIdx, locations[i].offset % pageSize);
2497bdd1243dSDimitry Andric ++i;
2498bdd1243dSDimitry Andric while (i < count && sameSegment(locations[i], firstLoc) &&
2499bdd1243dSDimitry Andric locations[i].offset / pageSize == pageIdx)
2500bdd1243dSDimitry Andric ++i;
2501bdd1243dSDimitry Andric }
2502bdd1243dSDimitry Andric }
2503bdd1243dSDimitry Andric
2504bdd1243dSDimitry Andric // Compute expected encoded size.
2505bdd1243dSDimitry Andric size = alignTo<8>(sizeof(dyld_chained_fixups_header));
2506bdd1243dSDimitry Andric size += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset) +
2507bdd1243dSDimitry Andric outputSegments.size() * sizeof(uint32_t));
2508bdd1243dSDimitry Andric for (const SegmentInfo &seg : fixupSegments)
2509bdd1243dSDimitry Andric size += seg.getSize();
2510bdd1243dSDimitry Andric size += importEntrySize(importFormat) * bindings.size();
2511bdd1243dSDimitry Andric size += symtabSize;
2512bdd1243dSDimitry Andric }
2513bdd1243dSDimitry Andric
2514fe6060f1SDimitry Andric template SymtabSection *macho::makeSymtabSection<LP64>(StringTableSection &);
2515fe6060f1SDimitry Andric template SymtabSection *macho::makeSymtabSection<ILP32>(StringTableSection &);
2516