11556Srgrimes//===- SyntheticSections.cpp ---------------------------------------------===//
21556Srgrimes//
31556Srgrimes// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41556Srgrimes// See https://llvm.org/LICENSE.txt for license information.
51556Srgrimes// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61556Srgrimes//
71556Srgrimes//===----------------------------------------------------------------------===//
81556Srgrimes
91556Srgrimes#include "SyntheticSections.h"
101556Srgrimes#include "ConcatOutputSection.h"
111556Srgrimes#include "Config.h"
121556Srgrimes#include "ExportTrie.h"
131556Srgrimes#include "InputFiles.h"
141556Srgrimes#include "MachOStructs.h"
151556Srgrimes#include "OutputSegment.h"
161556Srgrimes#include "SymbolTable.h"
171556Srgrimes#include "Symbols.h"
181556Srgrimes
191556Srgrimes#include "lld/Common/CommonLinkerContext.h"
201556Srgrimes#include "llvm/ADT/STLExtras.h"
211556Srgrimes#include "llvm/Config/llvm-config.h"
221556Srgrimes#include "llvm/Support/EndianStream.h"
231556Srgrimes#include "llvm/Support/FileSystem.h"
241556Srgrimes#include "llvm/Support/LEB128.h"
251556Srgrimes#include "llvm/Support/Parallel.h"
261556Srgrimes#include "llvm/Support/Path.h"
271556Srgrimes#include "llvm/Support/xxhash.h"
281556Srgrimes
291556Srgrimes#if defined(__APPLE__)
301556Srgrimes#include <sys/mman.h>
311556Srgrimes
321556Srgrimes#define COMMON_DIGEST_FOR_OPENSSL
331556Srgrimes#include <CommonCrypto/CommonDigest.h>
3436150Scharnier#else
3536150Scharnier#include "llvm/Support/SHA256.h"
3636150Scharnier#endif
371556Srgrimes
3899110Sobrienusing namespace llvm;
3999110Sobrienusing namespace llvm::MachO;
401556Srgrimesusing namespace llvm::support;
411556Srgrimesusing namespace llvm::support::endian;
4246684Skrisusing namespace lld;
431556Srgrimesusing namespace lld::macho;
441556Srgrimes
4517987Speter// Reads `len` bytes at data and writes the 32-byte SHA256 checksum to `output`.
4617987Speterstatic void sha256(const uint8_t *data, size_t len, uint8_t *output) {
4717987Speter#if defined(__APPLE__)
4817987Speter  // FIXME: Make LLVM's SHA256 faster and use it unconditionally. See PR56121
4917987Speter  // for some notes on this.
5017987Speter  CC_SHA256(data, len, output);
5118016Speter#else
52104282Smux  ArrayRef<uint8_t> block(data, len);
5318018Speter  std::array<uint8_t, 32> hash = SHA256::hash(block);
5438536Scracauer  static_assert(hash.size() == CodeSignatureSection::hashSize);
5529983Smsmith  memcpy(output, hash.data(), hash.size());
5617987Speter#endif
571556Srgrimes}
581556Srgrimes
591556SrgrimesInStruct macho::in;
601556Srgrimesstd::vector<SyntheticSection *> macho::syntheticSections;
611556Srgrimes
621556SrgrimesSyntheticSection::SyntheticSection(const char *segname, const char *name)
631556Srgrimes    : OutputSection(SyntheticKind, name) {
641556Srgrimes  std::tie(this->segname, this->name) = maybeRenameSection({segname, name});
651556Srgrimes  isec = makeSyntheticInputSection(segname, name);
661556Srgrimes  isec->parent = this;
67149018Sstefanf  syntheticSections.push_back(this);
68149018Sstefanf}
69149018Sstefanf
70149018Sstefanf// dyld3's MachOLoaded::getSlide() assumes that the __TEXT segment starts
711556Srgrimes// from the beginning of the file (i.e. the header).
7250394StgMachHeaderSection::MachHeaderSection()
7350394Stg    : SyntheticSection(segment_names::text, section_names::header) {
741556Srgrimes  // XXX: This is a hack. (See D97007)
751556Srgrimes  // Setting the index to 1 to pretend that this section is the text
76190295Sstefanf  // section.
77190295Sstefanf  index = 1;
78190295Sstefanf  isec->isFinal = true;
79190295Sstefanf}
80190295Sstefanf
81190295Sstefanfvoid MachHeaderSection::addLoadCommand(LoadCommand *lc) {
82190295Sstefanf  loadCommands.push_back(lc);
83190295Sstefanf  sizeOfCmds += lc->getSize();
84190295Sstefanf}
85190295Sstefanf
861556Srgrimesuint64_t MachHeaderSection::getSize() const {
871556Srgrimes  uint64_t size = target->headerSize + sizeOfCmds + config->headerPad;
8817987Speter  // If we are emitting an encryptable binary, our load commands must have a
8990111Simp  // separate (non-encrypted) page to themselves.
9017987Speter  if (config->emitEncryptionInfo)
911556Srgrimes    size = alignToPowerOf2(size, target->getPageSize());
921556Srgrimes  return size;
931556Srgrimes}
9450394Stg
951556Srgrimesstatic uint32_t cpuSubtype() {
96201053Sjilles  uint32_t subtype = target->cpuSubtype;
971556Srgrimes
981556Srgrimes  if (config->outputType == MH_EXECUTE && !config->staticLink &&
991556Srgrimes      target->cpuSubtype == CPU_SUBTYPE_X86_64_ALL &&
1001556Srgrimes      config->platform() == PLATFORM_MACOS &&
101190295Sstefanf      config->platformInfo.target.MinDeployment >= VersionTuple(10, 5))
102190295Sstefanf    subtype |= CPU_SUBTYPE_LIB64;
10329983Smsmith
10429983Smsmith  return subtype;
10529983Smsmith}
1061556Srgrimes
10750394Stgstatic bool hasWeakBinding() {
1081556Srgrimes  return config->emitChainedFixups ? in.chainedFixups->hasWeakBinding()
10929983Smsmith                                   : in.weakBinding->hasEntry();
11029983Smsmith}
11150394Stg
11229983Smsmithstatic bool hasNonWeakDefinition() {
11329983Smsmith  return config->emitChainedFixups ? in.chainedFixups->hasNonWeakDefinition()
11459436Scracauer                                   : in.weakBinding->hasNonWeakDefinition();
11529983Smsmith}
11629983Smsmith
11729983Smsmithvoid MachHeaderSection::writeTo(uint8_t *buf) const {
11850394Stg  auto *hdr = reinterpret_cast<mach_header *>(buf);
11950394Stg  hdr->magic = target->magic;
12050394Stg  hdr->cputype = target->cpuType;
12129983Smsmith  hdr->cpusubtype = cpuSubtype();
12259436Scracauer  hdr->filetype = config->outputType;
12359436Scracauer  hdr->ncmds = loadCommands.size();
12429983Smsmith  hdr->sizeofcmds = sizeOfCmds;
12529983Smsmith  hdr->flags = MH_DYLDLINK;
12629983Smsmith
12729983Smsmith  if (config->namespaceKind == NamespaceKind::twolevel)
12829983Smsmith    hdr->flags |= MH_NOUNDEFS | MH_TWOLEVEL;
12929983Smsmith
13029983Smsmith  if (config->outputType == MH_DYLIB && !config->hasReexports)
13129983Smsmith    hdr->flags |= MH_NO_REEXPORTED_DYLIBS;
13229983Smsmith
13329983Smsmith  if (config->markDeadStrippableDylib)
13429983Smsmith    hdr->flags |= MH_DEAD_STRIPPABLE_DYLIB;
13529983Smsmith
13629983Smsmith  if (config->outputType == MH_EXECUTE && config->isPic)
13729983Smsmith    hdr->flags |= MH_PIE;
13829983Smsmith
13929983Smsmith  if (config->outputType == MH_DYLIB && config->applicationExtension)
1401556Srgrimes    hdr->flags |= MH_APP_EXTENSION_SAFE;
1411556Srgrimes
1421556Srgrimes  if (in.exports->hasWeakSymbol || hasNonWeakDefinition())
1431556Srgrimes    hdr->flags |= MH_WEAK_DEFINES;
1441556Srgrimes
1451556Srgrimes  if (in.exports->hasWeakSymbol || hasWeakBinding())
1461556Srgrimes    hdr->flags |= MH_BINDS_TO_WEAK;
1471556Srgrimes
148190298Sstefanf  for (const OutputSegment *seg : outputSegments) {
14929983Smsmith    for (const OutputSection *osec : seg->getSections()) {
15029983Smsmith      if (isThreadLocalVariables(osec->flags)) {
15129983Smsmith        hdr->flags |= MH_HAS_TLV_DESCRIPTORS;
15229983Smsmith        break;
15329983Smsmith      }
15429983Smsmith    }
15529983Smsmith  }
15629983Smsmith
15729983Smsmith  uint8_t *p = reinterpret_cast<uint8_t *>(hdr) + target->headerSize;
15829983Smsmith  for (const LoadCommand *lc : loadCommands) {
15929983Smsmith    lc->writeTo(p);
16029983Smsmith    p += lc->getSize();
16129983Smsmith  }
16229983Smsmith}
16329983Smsmith
1641556SrgrimesPageZeroSection::PageZeroSection()
165190295Sstefanf    : SyntheticSection(segment_names::pageZero, section_names::pageZero) {}
1661556Srgrimes
1671556SrgrimesRebaseSection::RebaseSection()
1681556Srgrimes    : LinkEditSection(segment_names::linkEdit, section_names::rebase) {}
16980381Ssheldonh
1701556Srgrimesnamespace {
1711556Srgrimesstruct RebaseState {
1721556Srgrimes  uint64_t sequenceLength;
1731556Srgrimes  uint64_t skipLength;
1741556Srgrimes};
1751556Srgrimes} // namespace
1761556Srgrimes
1771556Srgrimesstatic void emitIncrement(uint64_t incr, raw_svector_ostream &os) {
1781556Srgrimes  assert(incr != 0);
1791556Srgrimes
1801556Srgrimes  if ((incr >> target->p2WordSize) <= REBASE_IMMEDIATE_MASK &&
18150394Stg      (incr % target->wordSize) == 0) {
1821556Srgrimes    os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_IMM_SCALED |
1831556Srgrimes                               (incr >> target->p2WordSize));
1841556Srgrimes  } else {
1851556Srgrimes    os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_ULEB);
1861556Srgrimes    encodeULEB128(incr, os);
187190295Sstefanf  }
188190295Sstefanf}
189190295Sstefanf
190190295Sstefanfstatic void flushRebase(const RebaseState &state, raw_svector_ostream &os) {
191190295Sstefanf  assert(state.sequenceLength > 0);
192190295Sstefanf
193190295Sstefanf  if (state.skipLength == target->wordSize) {
194190295Sstefanf    if (state.sequenceLength <= REBASE_IMMEDIATE_MASK) {
195190295Sstefanf      os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_IMM_TIMES |
196190295Sstefanf                                 state.sequenceLength);
197190295Sstefanf    } else {
198190295Sstefanf      os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ULEB_TIMES);
199190295Sstefanf      encodeULEB128(state.sequenceLength, os);
200190295Sstefanf    }
201190295Sstefanf  } else if (state.sequenceLength == 1) {
202190295Sstefanf    os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB);
203190295Sstefanf    encodeULEB128(state.skipLength - target->wordSize, os);
204190295Sstefanf  } else {
205190295Sstefanf    os << static_cast<uint8_t>(
206190295Sstefanf        REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB);
207190295Sstefanf    encodeULEB128(state.sequenceLength, os);
208190295Sstefanf    encodeULEB128(state.skipLength - target->wordSize, os);
209190295Sstefanf  }
210190295Sstefanf}
211190295Sstefanf
212190295Sstefanf// Rebases are communicated to dyld using a bytecode, whose opcodes cause the
213190295Sstefanf// memory location at a specific address to be rebased and/or the address to be
214190295Sstefanf// incremented.
2151556Srgrimes//
2161556Srgrimes// Opcode REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB is the most generic
217190295Sstefanf// one, encoding a series of evenly spaced addresses. This algorithm works by
218190295Sstefanf// splitting up the sorted list of addresses into such chunks. If the locations
219190295Sstefanf// are consecutive or the sequence consists of a single location, flushRebase
220190295Sstefanf// will use a smaller, more specialized encoding.
221190295Sstefanfstatic void encodeRebases(const OutputSegment *seg,
222190295Sstefanf                          MutableArrayRef<Location> locations,
223190295Sstefanf                          raw_svector_ostream &os) {
2241556Srgrimes  // dyld operates on segments. Translate section offsets into segment offsets.
225190295Sstefanf  for (Location &loc : locations)
2261556Srgrimes    loc.offset =
227190295Sstefanf        loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset);
228190295Sstefanf  // The algorithm assumes that locations are unique.
229190295Sstefanf  Location *end =
230190295Sstefanf      llvm::unique(locations, [](const Location &a, const Location &b) {
231190295Sstefanf        return a.offset == b.offset;
2321556Srgrimes      });
2331556Srgrimes  size_t count = end - locations.begin();
234190295Sstefanf
235190295Sstefanf  os << static_cast<uint8_t>(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
236190295Sstefanf                             seg->index);
237190295Sstefanf  assert(!locations.empty());
238190295Sstefanf  uint64_t offset = locations[0].offset;
239190295Sstefanf  encodeULEB128(offset, os);
240190295Sstefanf
241190295Sstefanf  RebaseState state{1, target->wordSize};
242190295Sstefanf
243190295Sstefanf  for (size_t i = 1; i < count; ++i) {
244190295Sstefanf    offset = locations[i].offset;
245190295Sstefanf
2461556Srgrimes    uint64_t skip = offset - locations[i - 1].offset;
247190295Sstefanf    assert(skip != 0 && "duplicate locations should have been weeded out");
248190295Sstefanf
2491556Srgrimes    if (skip == state.skipLength) {
2501556Srgrimes      ++state.sequenceLength;
2511556Srgrimes    } else if (state.sequenceLength == 1) {
2521556Srgrimes      ++state.sequenceLength;
2531556Srgrimes      state.skipLength = skip;
2541556Srgrimes    } else if (skip < state.skipLength) {
2551556Srgrimes      // The address is lower than what the rebase pointer would be if the last
25617987Speter      // location would be part of a sequence. We start a new sequence from the
257201053Sjilles      // previous location.
25817987Speter      --state.sequenceLength;
25917987Speter      flushRebase(state, os);
2601556Srgrimes
2611556Srgrimes      state.sequenceLength = 2;
26217987Speter      state.skipLength = skip;
2631556Srgrimes    } else {
26417987Speter      // The address is at some positive offset from the rebase pointer. We
26517987Speter      // start a new sequence which begins with the current location.
2661556Srgrimes      flushRebase(state, os);
26711571Sjoerg      emitIncrement(skip - state.skipLength, os);
26817987Speter      state.sequenceLength = 1;
26917987Speter      state.skipLength = target->wordSize;
27017987Speter    }
27117987Speter  }
27211571Sjoerg  flushRebase(state, os);
27317987Speter}
27417987Speter
27517987Spetervoid RebaseSection::finalizeContents() {
27611571Sjoerg  if (locations.empty())
27717987Speter    return;
27817987Speter
27917987Speter  raw_svector_ostream os{contents};
28017987Speter  os << static_cast<uint8_t>(REBASE_OPCODE_SET_TYPE_IMM | REBASE_TYPE_POINTER);
28117987Speter
28217987Speter  llvm::sort(locations, [](const Location &a, const Location &b) {
28317987Speter    return a.isec->getVA(a.offset) < b.isec->getVA(b.offset);
28417987Speter  });
28511571Sjoerg
28617987Speter  for (size_t i = 0, count = locations.size(); i < count;) {
28717987Speter    const OutputSegment *seg = locations[i].isec->parent->parent;
28817987Speter    size_t j = i + 1;
28917987Speter    while (j < count && locations[j].isec->parent->parent == seg)
29017987Speter      ++j;
29117987Speter    encodeRebases(seg, {locations.data() + i, locations.data() + j}, os);
29217987Speter    i = j;
29317987Speter  }
29411571Sjoerg  os << static_cast<uint8_t>(REBASE_OPCODE_DONE);
29517987Speter}
29617987Speter
29717987Spetervoid RebaseSection::writeTo(uint8_t *buf) const {
29817987Speter  memcpy(buf, contents.data(), contents.size());
29917987Speter}
30017987Speter
30117987SpeterNonLazyPointerSectionBase::NonLazyPointerSectionBase(const char *segname,
30217987Speter                                                     const char *name)
30311571Sjoerg    : SyntheticSection(segname, name) {
30417987Speter  align = target->wordSize;
30517987Speter}
30617987Speter
30717987Spetervoid macho::addNonLazyBindingEntries(const Symbol *sym,
30817987Speter                                     const InputSection *isec, uint64_t offset,
30917987Speter                                     int64_t addend) {
31017987Speter  if (config->emitChainedFixups) {
31117987Speter    if (needsBinding(sym))
31217987Speter      in.chainedFixups->addBinding(sym, isec, offset, addend);
313149918Sstefanf    else if (isa<Defined>(sym))
31417987Speter      in.chainedFixups->addRebase(isec, offset);
31517987Speter    else
31617987Speter      llvm_unreachable("cannot bind to an undefined symbol");
31717987Speter    return;
31820425Ssteve  }
319151795Sstefanf
32017987Speter  if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
32141844Simp    in.binding->addEntry(dysym, isec, offset, addend);
32211571Sjoerg    if (dysym->isWeakDef())
32317987Speter      in.weakBinding->addEntry(sym, isec, offset, addend);
32417987Speter  } else if (const auto *defined = dyn_cast<Defined>(sym)) {
32541844Simp    in.rebase->addEntry(isec, offset);
326151795Sstefanf    if (defined->isExternalWeakDef())
32717987Speter      in.weakBinding->addEntry(sym, isec, offset, addend);
32817987Speter    else if (defined->interposable)
32911571Sjoerg      in.binding->addEntry(sym, isec, offset, addend);
33011571Sjoerg  } else {
33111571Sjoerg    // Undefined symbols are filtered out in scanRelocations(); we should never
33217987Speter    // get here
33317987Speter    llvm_unreachable("cannot bind to an undefined symbol");
33417987Speter  }
33517987Speter}
33617987Speter
33717987Spetervoid NonLazyPointerSectionBase::addEntry(Symbol *sym) {
33817987Speter  if (entries.insert(sym)) {
33917987Speter    assert(!sym->isInGot());
34017987Speter    sym->gotIndex = entries.size() - 1;
34111571Sjoerg
34217987Speter    addNonLazyBindingEntries(sym, isec, sym->gotIndex * target->wordSize);
34317987Speter  }
34418016Speter}
34517987Speter
34617987Spetervoid macho::writeChainedRebase(uint8_t *buf, uint64_t targetVA) {
34717987Speter  assert(config->emitChainedFixups);
34817987Speter  assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
34911571Sjoerg  auto *rebase = reinterpret_cast<dyld_chained_ptr_64_rebase *>(buf);
35017987Speter  rebase->target = targetVA & 0xf'ffff'ffff;
35117987Speter  rebase->high8 = (targetVA >> 56);
35218016Speter  rebase->reserved = 0;
35317987Speter  rebase->next = 0;
35417987Speter  rebase->bind = 0;
35518016Speter
35617987Speter  // The fixup format places a 64 GiB limit on the output's size.
35717987Speter  // Should we handle this gracefully?
35818016Speter  uint64_t encodedVA = rebase->target | ((uint64_t)rebase->high8 << 56);
35917987Speter  if (encodedVA != targetVA)
36017987Speter    error("rebase target address 0x" + Twine::utohexstr(targetVA) +
36118016Speter          " does not fit into chained fixup. Re-link with -no_fixup_chains");
36217987Speter}
36317987Speter
36418016Speterstatic void writeChainedBind(uint8_t *buf, const Symbol *sym, int64_t addend) {
36517987Speter  assert(config->emitChainedFixups);
36617987Speter  assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
36718016Speter  auto *bind = reinterpret_cast<dyld_chained_ptr_64_bind *>(buf);
36817987Speter  auto [ordinal, inlineAddend] = in.chainedFixups->getBinding(sym, addend);
36917987Speter  bind->ordinal = ordinal;
37018016Speter  bind->addend = inlineAddend;
37117987Speter  bind->reserved = 0;
37217987Speter  bind->next = 0;
37318016Speter  bind->bind = 1;
37417987Speter}
37517987Speter
37618016Spetervoid macho::writeChainedFixup(uint8_t *buf, const Symbol *sym, int64_t addend) {
37717987Speter  if (needsBinding(sym))
37817987Speter    writeChainedBind(buf, sym, addend);
37918016Speter  else
38017987Speter    writeChainedRebase(buf, sym->getVA() + addend);
38117987Speter}
38218016Speter
38317987Spetervoid NonLazyPointerSectionBase::writeTo(uint8_t *buf) const {
38452072Sgreen  if (config->emitChainedFixups) {
38552072Sgreen    for (const auto &[i, entry] : llvm::enumerate(entries))
38652072Sgreen      writeChainedFixup(&buf[i * target->wordSize], entry, 0);
387181905Sed  } else {
388181905Sed    for (const auto &[i, entry] : llvm::enumerate(entries))
389181905Sed      if (auto *defined = dyn_cast<Defined>(entry))
39018016Speter        write64le(&buf[i * target->wordSize], defined->getVA());
39117987Speter  }
39217987Speter}
39317987Speter
39490111SimpGotSection::GotSection()
39517987Speter    : NonLazyPointerSectionBase(segment_names::data, section_names::got) {
39625222Ssteve  flags = S_NON_LAZY_SYMBOL_POINTERS;
397104282Smux}
39817987Speter
39917987SpeterTlvPointerSection::TlvPointerSection()
40017987Speter    : NonLazyPointerSectionBase(segment_names::data,
40117987Speter                                section_names::threadPtrs) {
40217987Speter  flags = S_THREAD_LOCAL_VARIABLE_POINTERS;
40317987Speter}
40411571Sjoerg
40517987SpeterBindingSection::BindingSection()
406194767Skib    : LinkEditSection(segment_names::linkEdit, section_names::binding) {}
40717987Speter
40811571Sjoergnamespace {
40917987Speterstruct Binding {
41011571Sjoerg  OutputSegment *segment = nullptr;
41111571Sjoerg  uint64_t offset = 0;
41217987Speter  int64_t addend = 0;
41311571Sjoerg};
41411571Sjoergstruct BindIR {
41517987Speter  // Default value of 0xF0 is not valid opcode and should make the program
41611571Sjoerg  // scream instead of accidentally writing "valid" values.
41717987Speter  uint8_t opcode = 0xF0;
41817987Speter  uint64_t data = 0;
41911571Sjoerg  uint64_t consecutiveCount = 0;
42011571Sjoerg};
42117987Speter} // namespace
42217987Speter
42317987Speter// Encode a sequence of opcodes that tell dyld to write the address of symbol +
424104208Stjr// addend at osec->addr + outSecOff.
42517987Speter//
42617987Speter// The bind opcode "interpreter" remembers the values of each binding field, so
42717987Speter// we only need to encode the differences between bindings. Hence the use of
42817987Speter// lastBinding.
42917987Speterstatic void encodeBinding(const OutputSection *osec, uint64_t outSecOff,
43017987Speter                          int64_t addend, Binding &lastBinding,
431104208Stjr                          std::vector<BindIR> &opcodes) {
43217987Speter  OutputSegment *seg = osec->parent;
43311571Sjoerg  uint64_t offset = osec->getSegmentOffset() + outSecOff;
43411571Sjoerg  if (lastBinding.segment != seg) {
435104282Smux    opcodes.push_back(
43617987Speter        {static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
43717987Speter                              seg->index),
43817987Speter         offset});
43917987Speter    lastBinding.segment = seg;
440104282Smux    lastBinding.offset = offset;
44117987Speter  } else if (lastBinding.offset != offset) {
44217987Speter    opcodes.push_back({BIND_OPCODE_ADD_ADDR_ULEB, offset - lastBinding.offset});
44317987Speter    lastBinding.offset = offset;
444104208Stjr  }
44517987Speter
44611571Sjoerg  if (lastBinding.addend != addend) {
44717987Speter    opcodes.push_back(
44817987Speter        {BIND_OPCODE_SET_ADDEND_SLEB, static_cast<uint64_t>(addend)});
449155301Sschweikh    lastBinding.addend = addend;
45018016Speter  }
45118016Speter
452104208Stjr  opcodes.push_back({BIND_OPCODE_DO_BIND, 0});
45317987Speter  // DO_BIND causes dyld to both perform the binding and increment the offset
45417987Speter  lastBinding.offset += target->wordSize;
45517987Speter}
45617987Speter
45717987Speterstatic void optimizeOpcodes(std::vector<BindIR> &opcodes) {
45818016Speter  // Pass 1: Combine bind/add pairs
45918016Speter  size_t i;
46018019Speter  int pWrite = 0;
46118016Speter  for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
46218016Speter    if ((opcodes[i].opcode == BIND_OPCODE_ADD_ADDR_ULEB) &&
46318019Speter        (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND)) {
46418019Speter      opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB;
46517987Speter      opcodes[pWrite].data = opcodes[i].data;
46617987Speter      ++i;
46717987Speter    } else {
46817987Speter      opcodes[pWrite] = opcodes[i - 1];
46917987Speter    }
470104282Smux  }
47117987Speter  if (i == opcodes.size())
47211571Sjoerg    opcodes[pWrite] = opcodes[i - 1];
47317987Speter  opcodes.resize(pWrite + 1);
47411571Sjoerg
47517987Speter  // Pass 2: Compress two or more bind_add opcodes
47618016Speter  pWrite = 0;
477104208Stjr  for (i = 1; i < opcodes.size(); ++i, ++pWrite) {
47817987Speter    if ((opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
47917987Speter        (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
48017987Speter        (opcodes[i].data == opcodes[i - 1].data)) {
48117987Speter      opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB;
48217987Speter      opcodes[pWrite].consecutiveCount = 2;
48317987Speter      opcodes[pWrite].data = opcodes[i].data;
484104208Stjr      ++i;
48517987Speter      while (i < opcodes.size() &&
48617987Speter             (opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
48717987Speter             (opcodes[i].data == opcodes[i - 1].data)) {
48817987Speter        opcodes[pWrite].consecutiveCount++;
48917987Speter        ++i;
49017987Speter      }
49117987Speter    } else {
49217987Speter      opcodes[pWrite] = opcodes[i - 1];
49317987Speter    }
49417987Speter  }
49517987Speter  if (i == opcodes.size())
496104282Smux    opcodes[pWrite] = opcodes[i - 1];
49717987Speter  opcodes.resize(pWrite + 1);
49817987Speter
49911571Sjoerg  // Pass 3: Use immediate encodings
50011571Sjoerg  // Every binding is the size of one pointer. If the next binding is a
501  // multiple of wordSize away that is within BIND_IMMEDIATE_MASK, the
502  // opcode can be scaled by wordSize into a single byte and dyld will
503  // expand it to the correct address.
504  for (auto &p : opcodes) {
505    // It's unclear why the check needs to be less than BIND_IMMEDIATE_MASK,
506    // but ld64 currently does this. This could be a potential bug, but
507    // for now, perform the same behavior to prevent mysterious bugs.
508    if ((p.opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) &&
509        ((p.data / target->wordSize) < BIND_IMMEDIATE_MASK) &&
510        ((p.data % target->wordSize) == 0)) {
511      p.opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED;
512      p.data /= target->wordSize;
513    }
514  }
515}
516
517static void flushOpcodes(const BindIR &op, raw_svector_ostream &os) {
518  uint8_t opcode = op.opcode & BIND_OPCODE_MASK;
519  switch (opcode) {
520  case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
521  case BIND_OPCODE_ADD_ADDR_ULEB:
522  case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
523    os << op.opcode;
524    encodeULEB128(op.data, os);
525    break;
526  case BIND_OPCODE_SET_ADDEND_SLEB:
527    os << op.opcode;
528    encodeSLEB128(static_cast<int64_t>(op.data), os);
529    break;
530  case BIND_OPCODE_DO_BIND:
531    os << op.opcode;
532    break;
533  case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
534    os << op.opcode;
535    encodeULEB128(op.consecutiveCount, os);
536    encodeULEB128(op.data, os);
537    break;
538  case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
539    os << static_cast<uint8_t>(op.opcode | op.data);
540    break;
541  default:
542    llvm_unreachable("cannot bind to an unrecognized symbol");
543  }
544}
545
546// Non-weak bindings need to have their dylib ordinal encoded as well.
547static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) {
548  if (config->namespaceKind == NamespaceKind::flat || dysym.isDynamicLookup())
549    return static_cast<int16_t>(BIND_SPECIAL_DYLIB_FLAT_LOOKUP);
550  assert(dysym.getFile()->isReferenced());
551  return dysym.getFile()->ordinal;
552}
553
554static int16_t ordinalForSymbol(const Symbol &sym) {
555  if (const auto *dysym = dyn_cast<DylibSymbol>(&sym))
556    return ordinalForDylibSymbol(*dysym);
557  assert(cast<Defined>(&sym)->interposable);
558  return BIND_SPECIAL_DYLIB_FLAT_LOOKUP;
559}
560
561static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) {
562  if (ordinal <= 0) {
563    os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
564                               (ordinal & BIND_IMMEDIATE_MASK));
565  } else if (ordinal <= BIND_IMMEDIATE_MASK) {
566    os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | ordinal);
567  } else {
568    os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
569    encodeULEB128(ordinal, os);
570  }
571}
572
573static void encodeWeakOverride(const Defined *defined,
574                               raw_svector_ostream &os) {
575  os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM |
576                             BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
577     << defined->getName() << '\0';
578}
579
580// Organize the bindings so we can encoded them with fewer opcodes.
581//
582// First, all bindings for a given symbol should be grouped together.
583// BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM is the largest opcode (since it
584// has an associated symbol string), so we only want to emit it once per symbol.
585//
586// Within each group, we sort the bindings by address. Since bindings are
587// delta-encoded, sorting them allows for a more compact result. Note that
588// sorting by address alone ensures that bindings for the same segment / section
589// are located together, minimizing the number of times we have to emit
590// BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB.
591//
592// Finally, we sort the symbols by the address of their first binding, again
593// to facilitate the delta-encoding process.
594template <class Sym>
595std::vector<std::pair<const Sym *, std::vector<BindingEntry>>>
596sortBindings(const BindingsMap<const Sym *> &bindingsMap) {
597  std::vector<std::pair<const Sym *, std::vector<BindingEntry>>> bindingsVec(
598      bindingsMap.begin(), bindingsMap.end());
599  for (auto &p : bindingsVec) {
600    std::vector<BindingEntry> &bindings = p.second;
601    llvm::sort(bindings, [](const BindingEntry &a, const BindingEntry &b) {
602      return a.target.getVA() < b.target.getVA();
603    });
604  }
605  llvm::sort(bindingsVec, [](const auto &a, const auto &b) {
606    return a.second[0].target.getVA() < b.second[0].target.getVA();
607  });
608  return bindingsVec;
609}
610
611// Emit bind opcodes, which are a stream of byte-sized opcodes that dyld
612// interprets to update a record with the following fields:
613//  * segment index (of the segment to write the symbol addresses to, typically
614//    the __DATA_CONST segment which contains the GOT)
615//  * offset within the segment, indicating the next location to write a binding
616//  * symbol type
617//  * symbol library ordinal (the index of its library's LC_LOAD_DYLIB command)
618//  * symbol name
619//  * addend
620// When dyld sees BIND_OPCODE_DO_BIND, it uses the current record state to bind
621// a symbol in the GOT, and increments the segment offset to point to the next
622// entry. It does *not* clear the record state after doing the bind, so
623// subsequent opcodes only need to encode the differences between bindings.
624void BindingSection::finalizeContents() {
625  raw_svector_ostream os{contents};
626  Binding lastBinding;
627  int16_t lastOrdinal = 0;
628
629  for (auto &p : sortBindings(bindingsMap)) {
630    const Symbol *sym = p.first;
631    std::vector<BindingEntry> &bindings = p.second;
632    uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
633    if (sym->isWeakRef())
634      flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
635    os << flags << sym->getName() << '\0'
636       << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
637    int16_t ordinal = ordinalForSymbol(*sym);
638    if (ordinal != lastOrdinal) {
639      encodeDylibOrdinal(ordinal, os);
640      lastOrdinal = ordinal;
641    }
642    std::vector<BindIR> opcodes;
643    for (const BindingEntry &b : bindings)
644      encodeBinding(b.target.isec->parent,
645                    b.target.isec->getOffset(b.target.offset), b.addend,
646                    lastBinding, opcodes);
647    if (config->optimize > 1)
648      optimizeOpcodes(opcodes);
649    for (const auto &op : opcodes)
650      flushOpcodes(op, os);
651  }
652  if (!bindingsMap.empty())
653    os << static_cast<uint8_t>(BIND_OPCODE_DONE);
654}
655
656void BindingSection::writeTo(uint8_t *buf) const {
657  memcpy(buf, contents.data(), contents.size());
658}
659
660WeakBindingSection::WeakBindingSection()
661    : LinkEditSection(segment_names::linkEdit, section_names::weakBinding) {}
662
663void WeakBindingSection::finalizeContents() {
664  raw_svector_ostream os{contents};
665  Binding lastBinding;
666
667  for (const Defined *defined : definitions)
668    encodeWeakOverride(defined, os);
669
670  for (auto &p : sortBindings(bindingsMap)) {
671    const Symbol *sym = p.first;
672    std::vector<BindingEntry> &bindings = p.second;
673    os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
674       << sym->getName() << '\0'
675       << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER);
676    std::vector<BindIR> opcodes;
677    for (const BindingEntry &b : bindings)
678      encodeBinding(b.target.isec->parent,
679                    b.target.isec->getOffset(b.target.offset), b.addend,
680                    lastBinding, opcodes);
681    if (config->optimize > 1)
682      optimizeOpcodes(opcodes);
683    for (const auto &op : opcodes)
684      flushOpcodes(op, os);
685  }
686  if (!bindingsMap.empty() || !definitions.empty())
687    os << static_cast<uint8_t>(BIND_OPCODE_DONE);
688}
689
690void WeakBindingSection::writeTo(uint8_t *buf) const {
691  memcpy(buf, contents.data(), contents.size());
692}
693
694StubsSection::StubsSection()
695    : SyntheticSection(segment_names::text, section_names::stubs) {
696  flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
697  // The stubs section comprises machine instructions, which are aligned to
698  // 4 bytes on the archs we care about.
699  align = 4;
700  reserved2 = target->stubSize;
701}
702
703uint64_t StubsSection::getSize() const {
704  return entries.size() * target->stubSize;
705}
706
707void StubsSection::writeTo(uint8_t *buf) const {
708  size_t off = 0;
709  for (const Symbol *sym : entries) {
710    uint64_t pointerVA =
711        config->emitChainedFixups ? sym->getGotVA() : sym->getLazyPtrVA();
712    target->writeStub(buf + off, *sym, pointerVA);
713    off += target->stubSize;
714  }
715}
716
717void StubsSection::finalize() { isFinal = true; }
718
719static void addBindingsForStub(Symbol *sym) {
720  assert(!config->emitChainedFixups);
721  if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
722    if (sym->isWeakDef()) {
723      in.binding->addEntry(dysym, in.lazyPointers->isec,
724                           sym->stubsIndex * target->wordSize);
725      in.weakBinding->addEntry(sym, in.lazyPointers->isec,
726                               sym->stubsIndex * target->wordSize);
727    } else {
728      in.lazyBinding->addEntry(dysym);
729    }
730  } else if (auto *defined = dyn_cast<Defined>(sym)) {
731    if (defined->isExternalWeakDef()) {
732      in.rebase->addEntry(in.lazyPointers->isec,
733                          sym->stubsIndex * target->wordSize);
734      in.weakBinding->addEntry(sym, in.lazyPointers->isec,
735                               sym->stubsIndex * target->wordSize);
736    } else if (defined->interposable) {
737      in.lazyBinding->addEntry(sym);
738    } else {
739      llvm_unreachable("invalid stub target");
740    }
741  } else {
742    llvm_unreachable("invalid stub target symbol type");
743  }
744}
745
746void StubsSection::addEntry(Symbol *sym) {
747  bool inserted = entries.insert(sym);
748  if (inserted) {
749    sym->stubsIndex = entries.size() - 1;
750
751    if (config->emitChainedFixups)
752      in.got->addEntry(sym);
753    else
754      addBindingsForStub(sym);
755  }
756}
757
758StubHelperSection::StubHelperSection()
759    : SyntheticSection(segment_names::text, section_names::stubHelper) {
760  flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
761  align = 4; // This section comprises machine instructions
762}
763
764uint64_t StubHelperSection::getSize() const {
765  return target->stubHelperHeaderSize +
766         in.lazyBinding->getEntries().size() * target->stubHelperEntrySize;
767}
768
769bool StubHelperSection::isNeeded() const { return in.lazyBinding->isNeeded(); }
770
771void StubHelperSection::writeTo(uint8_t *buf) const {
772  target->writeStubHelperHeader(buf);
773  size_t off = target->stubHelperHeaderSize;
774  for (const Symbol *sym : in.lazyBinding->getEntries()) {
775    target->writeStubHelperEntry(buf + off, *sym, addr + off);
776    off += target->stubHelperEntrySize;
777  }
778}
779
780void StubHelperSection::setUp() {
781  Symbol *binder = symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr,
782                                        /*isWeakRef=*/false);
783  if (auto *undefined = dyn_cast<Undefined>(binder))
784    treatUndefinedSymbol(*undefined,
785                         "lazy binding (normally in libSystem.dylib)");
786
787  // treatUndefinedSymbol() can replace binder with a DylibSymbol; re-check.
788  stubBinder = dyn_cast_or_null<DylibSymbol>(binder);
789  if (stubBinder == nullptr)
790    return;
791
792  in.got->addEntry(stubBinder);
793
794  in.imageLoaderCache->parent =
795      ConcatOutputSection::getOrCreateForInput(in.imageLoaderCache);
796  inputSections.push_back(in.imageLoaderCache);
797  // Since this isn't in the symbol table or in any input file, the noDeadStrip
798  // argument doesn't matter.
799  dyldPrivate =
800      make<Defined>("__dyld_private", nullptr, in.imageLoaderCache, 0, 0,
801                    /*isWeakDef=*/false,
802                    /*isExternal=*/false, /*isPrivateExtern=*/false,
803                    /*includeInSymtab=*/true,
804                    /*isReferencedDynamically=*/false,
805                    /*noDeadStrip=*/false);
806  dyldPrivate->used = true;
807}
808
809ObjCStubsSection::ObjCStubsSection()
810    : SyntheticSection(segment_names::text, section_names::objcStubs) {
811  flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
812  align = config->objcStubsMode == ObjCStubsMode::fast
813              ? target->objcStubsFastAlignment
814              : target->objcStubsSmallAlignment;
815}
816
817void ObjCStubsSection::addEntry(Symbol *sym) {
818  assert(sym->getName().starts_with(symbolPrefix) && "not an objc stub");
819  StringRef methname = sym->getName().drop_front(symbolPrefix.size());
820  offsets.push_back(
821      in.objcMethnameSection->getStringOffset(methname).outSecOff);
822
823  auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
824                      ? target->objcStubsFastSize
825                      : target->objcStubsSmallSize;
826  Defined *newSym = replaceSymbol<Defined>(
827      sym, sym->getName(), nullptr, isec,
828      /*value=*/symbols.size() * stubSize,
829      /*size=*/stubSize,
830      /*isWeakDef=*/false, /*isExternal=*/true, /*isPrivateExtern=*/true,
831      /*includeInSymtab=*/true, /*isReferencedDynamically=*/false,
832      /*noDeadStrip=*/false);
833  symbols.push_back(newSym);
834}
835
836void ObjCStubsSection::setUp() {
837  objcMsgSend = symtab->addUndefined("_objc_msgSend", /*file=*/nullptr,
838                                     /*isWeakRef=*/false);
839  if (auto *undefined = dyn_cast<Undefined>(objcMsgSend))
840    treatUndefinedSymbol(*undefined,
841                         "lazy binding (normally in libobjc.dylib)");
842  objcMsgSend->used = true;
843  if (config->objcStubsMode == ObjCStubsMode::fast) {
844    in.got->addEntry(objcMsgSend);
845    assert(objcMsgSend->isInGot());
846  } else {
847    assert(config->objcStubsMode == ObjCStubsMode::small);
848    // In line with ld64's behavior, when objc_msgSend is a direct symbol,
849    // we directly reference it.
850    // In other cases, typically when binding in libobjc.dylib,
851    // we generate a stub to invoke objc_msgSend.
852    if (!isa<Defined>(objcMsgSend))
853      in.stubs->addEntry(objcMsgSend);
854  }
855
856  size_t size = offsets.size() * target->wordSize;
857  uint8_t *selrefsData = bAlloc().Allocate<uint8_t>(size);
858  for (size_t i = 0, n = offsets.size(); i < n; ++i)
859    write64le(&selrefsData[i * target->wordSize], offsets[i]);
860
861  in.objcSelrefs =
862      makeSyntheticInputSection(segment_names::data, section_names::objcSelrefs,
863                                S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP,
864                                ArrayRef<uint8_t>{selrefsData, size},
865                                /*align=*/target->wordSize);
866  in.objcSelrefs->live = true;
867
868  for (size_t i = 0, n = offsets.size(); i < n; ++i) {
869    in.objcSelrefs->relocs.push_back(
870        {/*type=*/target->unsignedRelocType,
871         /*pcrel=*/false, /*length=*/3,
872         /*offset=*/static_cast<uint32_t>(i * target->wordSize),
873         /*addend=*/offsets[i] * in.objcMethnameSection->align,
874         /*referent=*/in.objcMethnameSection->isec});
875  }
876
877  in.objcSelrefs->parent =
878      ConcatOutputSection::getOrCreateForInput(in.objcSelrefs);
879  inputSections.push_back(in.objcSelrefs);
880  in.objcSelrefs->isFinal = true;
881}
882
883uint64_t ObjCStubsSection::getSize() const {
884  auto stubSize = config->objcStubsMode == ObjCStubsMode::fast
885                      ? target->objcStubsFastSize
886                      : target->objcStubsSmallSize;
887  return stubSize * symbols.size();
888}
889
890void ObjCStubsSection::writeTo(uint8_t *buf) const {
891  assert(in.objcSelrefs->live);
892  assert(in.objcSelrefs->isFinal);
893
894  uint64_t stubOffset = 0;
895  for (size_t i = 0, n = symbols.size(); i < n; ++i) {
896    Defined *sym = symbols[i];
897    target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr,
898                                 stubOffset, in.objcSelrefs->getVA(), i,
899                                 objcMsgSend);
900  }
901}
902
903LazyPointerSection::LazyPointerSection()
904    : SyntheticSection(segment_names::data, section_names::lazySymbolPtr) {
905  align = target->wordSize;
906  flags = S_LAZY_SYMBOL_POINTERS;
907}
908
909uint64_t LazyPointerSection::getSize() const {
910  return in.stubs->getEntries().size() * target->wordSize;
911}
912
913bool LazyPointerSection::isNeeded() const {
914  return !in.stubs->getEntries().empty();
915}
916
917void LazyPointerSection::writeTo(uint8_t *buf) const {
918  size_t off = 0;
919  for (const Symbol *sym : in.stubs->getEntries()) {
920    if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
921      if (dysym->hasStubsHelper()) {
922        uint64_t stubHelperOffset =
923            target->stubHelperHeaderSize +
924            dysym->stubsHelperIndex * target->stubHelperEntrySize;
925        write64le(buf + off, in.stubHelper->addr + stubHelperOffset);
926      }
927    } else {
928      write64le(buf + off, sym->getVA());
929    }
930    off += target->wordSize;
931  }
932}
933
934LazyBindingSection::LazyBindingSection()
935    : LinkEditSection(segment_names::linkEdit, section_names::lazyBinding) {}
936
937void LazyBindingSection::finalizeContents() {
938  // TODO: Just precompute output size here instead of writing to a temporary
939  // buffer
940  for (Symbol *sym : entries)
941    sym->lazyBindOffset = encode(*sym);
942}
943
944void LazyBindingSection::writeTo(uint8_t *buf) const {
945  memcpy(buf, contents.data(), contents.size());
946}
947
948void LazyBindingSection::addEntry(Symbol *sym) {
949  assert(!config->emitChainedFixups && "Chained fixups always bind eagerly");
950  if (entries.insert(sym)) {
951    sym->stubsHelperIndex = entries.size() - 1;
952    in.rebase->addEntry(in.lazyPointers->isec,
953                        sym->stubsIndex * target->wordSize);
954  }
955}
956
957// Unlike the non-lazy binding section, the bind opcodes in this section aren't
958// interpreted all at once. Rather, dyld will start interpreting opcodes at a
959// given offset, typically only binding a single symbol before it finds a
960// BIND_OPCODE_DONE terminator. As such, unlike in the non-lazy-binding case,
961// we cannot encode just the differences between symbols; we have to emit the
962// complete bind information for each symbol.
963uint32_t LazyBindingSection::encode(const Symbol &sym) {
964  uint32_t opstreamOffset = contents.size();
965  OutputSegment *dataSeg = in.lazyPointers->parent;
966  os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
967                             dataSeg->index);
968  uint64_t offset =
969      in.lazyPointers->addr - dataSeg->addr + sym.stubsIndex * target->wordSize;
970  encodeULEB128(offset, os);
971  encodeDylibOrdinal(ordinalForSymbol(sym), os);
972
973  uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
974  if (sym.isWeakRef())
975    flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
976
977  os << flags << sym.getName() << '\0'
978     << static_cast<uint8_t>(BIND_OPCODE_DO_BIND)
979     << static_cast<uint8_t>(BIND_OPCODE_DONE);
980  return opstreamOffset;
981}
982
983ExportSection::ExportSection()
984    : LinkEditSection(segment_names::linkEdit, section_names::export_) {}
985
986void ExportSection::finalizeContents() {
987  trieBuilder.setImageBase(in.header->addr);
988  for (const Symbol *sym : symtab->getSymbols()) {
989    if (const auto *defined = dyn_cast<Defined>(sym)) {
990      if (defined->privateExtern || !defined->isLive())
991        continue;
992      trieBuilder.addSymbol(*defined);
993      hasWeakSymbol = hasWeakSymbol || sym->isWeakDef();
994    } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
995      if (dysym->shouldReexport)
996        trieBuilder.addSymbol(*dysym);
997    }
998  }
999  size = trieBuilder.build();
1000}
1001
1002void ExportSection::writeTo(uint8_t *buf) const { trieBuilder.writeTo(buf); }
1003
1004DataInCodeSection::DataInCodeSection()
1005    : LinkEditSection(segment_names::linkEdit, section_names::dataInCode) {}
1006
1007template <class LP>
1008static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() {
1009  std::vector<MachO::data_in_code_entry> dataInCodeEntries;
1010  for (const InputFile *inputFile : inputFiles) {
1011    if (!isa<ObjFile>(inputFile))
1012      continue;
1013    const ObjFile *objFile = cast<ObjFile>(inputFile);
1014    ArrayRef<MachO::data_in_code_entry> entries = objFile->getDataInCode();
1015    if (entries.empty())
1016      continue;
1017
1018    assert(is_sorted(entries, [](const data_in_code_entry &lhs,
1019                                 const data_in_code_entry &rhs) {
1020      return lhs.offset < rhs.offset;
1021    }));
1022    // For each code subsection find 'data in code' entries residing in it.
1023    // Compute the new offset values as
1024    // <offset within subsection> + <subsection address> - <__TEXT address>.
1025    for (const Section *section : objFile->sections) {
1026      for (const Subsection &subsec : section->subsections) {
1027        const InputSection *isec = subsec.isec;
1028        if (!isCodeSection(isec))
1029          continue;
1030        if (cast<ConcatInputSection>(isec)->shouldOmitFromOutput())
1031          continue;
1032        const uint64_t beginAddr = section->addr + subsec.offset;
1033        auto it = llvm::lower_bound(
1034            entries, beginAddr,
1035            [](const MachO::data_in_code_entry &entry, uint64_t addr) {
1036              return entry.offset < addr;
1037            });
1038        const uint64_t endAddr = beginAddr + isec->getSize();
1039        for (const auto end = entries.end();
1040             it != end && it->offset + it->length <= endAddr; ++it)
1041          dataInCodeEntries.push_back(
1042              {static_cast<uint32_t>(isec->getVA(it->offset - beginAddr) -
1043                                     in.header->addr),
1044               it->length, it->kind});
1045      }
1046    }
1047  }
1048
1049  // ld64 emits the table in sorted order too.
1050  llvm::sort(dataInCodeEntries,
1051             [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) {
1052               return lhs.offset < rhs.offset;
1053             });
1054  return dataInCodeEntries;
1055}
1056
1057void DataInCodeSection::finalizeContents() {
1058  entries = target->wordSize == 8 ? collectDataInCodeEntries<LP64>()
1059                                  : collectDataInCodeEntries<ILP32>();
1060}
1061
1062void DataInCodeSection::writeTo(uint8_t *buf) const {
1063  if (!entries.empty())
1064    memcpy(buf, entries.data(), getRawSize());
1065}
1066
1067FunctionStartsSection::FunctionStartsSection()
1068    : LinkEditSection(segment_names::linkEdit, section_names::functionStarts) {}
1069
1070void FunctionStartsSection::finalizeContents() {
1071  raw_svector_ostream os{contents};
1072  std::vector<uint64_t> addrs;
1073  for (const InputFile *file : inputFiles) {
1074    if (auto *objFile = dyn_cast<ObjFile>(file)) {
1075      for (const Symbol *sym : objFile->symbols) {
1076        if (const auto *defined = dyn_cast_or_null<Defined>(sym)) {
1077          if (!defined->isec || !isCodeSection(defined->isec) ||
1078              !defined->isLive())
1079            continue;
1080          addrs.push_back(defined->getVA());
1081        }
1082      }
1083    }
1084  }
1085  llvm::sort(addrs);
1086  uint64_t addr = in.header->addr;
1087  for (uint64_t nextAddr : addrs) {
1088    uint64_t delta = nextAddr - addr;
1089    if (delta == 0)
1090      continue;
1091    encodeULEB128(delta, os);
1092    addr = nextAddr;
1093  }
1094  os << '\0';
1095}
1096
1097void FunctionStartsSection::writeTo(uint8_t *buf) const {
1098  memcpy(buf, contents.data(), contents.size());
1099}
1100
1101SymtabSection::SymtabSection(StringTableSection &stringTableSection)
1102    : LinkEditSection(segment_names::linkEdit, section_names::symbolTable),
1103      stringTableSection(stringTableSection) {}
1104
1105void SymtabSection::emitBeginSourceStab(StringRef sourceFile) {
1106  StabsEntry stab(N_SO);
1107  stab.strx = stringTableSection.addString(saver().save(sourceFile));
1108  stabs.emplace_back(std::move(stab));
1109}
1110
1111void SymtabSection::emitEndSourceStab() {
1112  StabsEntry stab(N_SO);
1113  stab.sect = 1;
1114  stabs.emplace_back(std::move(stab));
1115}
1116
1117void SymtabSection::emitObjectFileStab(ObjFile *file) {
1118  StabsEntry stab(N_OSO);
1119  stab.sect = target->cpuSubtype;
1120  SmallString<261> path(!file->archiveName.empty() ? file->archiveName
1121                                                   : file->getName());
1122  std::error_code ec = sys::fs::make_absolute(path);
1123  if (ec)
1124    fatal("failed to get absolute path for " + path);
1125
1126  if (!file->archiveName.empty())
1127    path.append({"(", file->getName(), ")"});
1128
1129  StringRef adjustedPath = saver().save(path.str());
1130  adjustedPath.consume_front(config->osoPrefix);
1131
1132  stab.strx = stringTableSection.addString(adjustedPath);
1133  stab.desc = 1;
1134  stab.value = file->modTime;
1135  stabs.emplace_back(std::move(stab));
1136}
1137
1138void SymtabSection::emitEndFunStab(Defined *defined) {
1139  StabsEntry stab(N_FUN);
1140  stab.value = defined->size;
1141  stabs.emplace_back(std::move(stab));
1142}
1143
1144void SymtabSection::emitStabs() {
1145  if (config->omitDebugInfo)
1146    return;
1147
1148  for (const std::string &s : config->astPaths) {
1149    StabsEntry astStab(N_AST);
1150    astStab.strx = stringTableSection.addString(s);
1151    stabs.emplace_back(std::move(astStab));
1152  }
1153
1154  // Cache the file ID for each symbol in an std::pair for faster sorting.
1155  using SortingPair = std::pair<Defined *, int>;
1156  std::vector<SortingPair> symbolsNeedingStabs;
1157  for (const SymtabEntry &entry :
1158       concat<SymtabEntry>(localSymbols, externalSymbols)) {
1159    Symbol *sym = entry.sym;
1160    assert(sym->isLive() &&
1161           "dead symbols should not be in localSymbols, externalSymbols");
1162    if (auto *defined = dyn_cast<Defined>(sym)) {
1163      // Excluded symbols should have been filtered out in finalizeContents().
1164      assert(defined->includeInSymtab);
1165
1166      if (defined->isAbsolute())
1167        continue;
1168
1169      // Constant-folded symbols go in the executable's symbol table, but don't
1170      // get a stabs entry.
1171      if (defined->wasIdenticalCodeFolded)
1172        continue;
1173
1174      ObjFile *file = defined->getObjectFile();
1175      if (!file || !file->compileUnit)
1176        continue;
1177
1178      symbolsNeedingStabs.emplace_back(defined, defined->isec->getFile()->id);
1179    }
1180  }
1181
1182  llvm::stable_sort(symbolsNeedingStabs,
1183                    [&](const SortingPair &a, const SortingPair &b) {
1184                      return a.second < b.second;
1185                    });
1186
1187  // Emit STABS symbols so that dsymutil and/or the debugger can map address
1188  // regions in the final binary to the source and object files from which they
1189  // originated.
1190  InputFile *lastFile = nullptr;
1191  for (SortingPair &pair : symbolsNeedingStabs) {
1192    Defined *defined = pair.first;
1193    InputSection *isec = defined->isec;
1194    ObjFile *file = cast<ObjFile>(isec->getFile());
1195
1196    if (lastFile == nullptr || lastFile != file) {
1197      if (lastFile != nullptr)
1198        emitEndSourceStab();
1199      lastFile = file;
1200
1201      emitBeginSourceStab(file->sourceFile());
1202      emitObjectFileStab(file);
1203    }
1204
1205    StabsEntry symStab;
1206    symStab.sect = defined->isec->parent->index;
1207    symStab.strx = stringTableSection.addString(defined->getName());
1208    symStab.value = defined->getVA();
1209
1210    if (isCodeSection(isec)) {
1211      symStab.type = N_FUN;
1212      stabs.emplace_back(std::move(symStab));
1213      emitEndFunStab(defined);
1214    } else {
1215      symStab.type = defined->isExternal() ? N_GSYM : N_STSYM;
1216      stabs.emplace_back(std::move(symStab));
1217    }
1218  }
1219
1220  if (!stabs.empty())
1221    emitEndSourceStab();
1222}
1223
1224void SymtabSection::finalizeContents() {
1225  auto addSymbol = [&](std::vector<SymtabEntry> &symbols, Symbol *sym) {
1226    uint32_t strx = stringTableSection.addString(sym->getName());
1227    symbols.push_back({sym, strx});
1228  };
1229
1230  std::function<void(Symbol *)> localSymbolsHandler;
1231  switch (config->localSymbolsPresence) {
1232  case SymtabPresence::All:
1233    localSymbolsHandler = [&](Symbol *sym) { addSymbol(localSymbols, sym); };
1234    break;
1235  case SymtabPresence::None:
1236    localSymbolsHandler = [&](Symbol *) { /* Do nothing*/ };
1237    break;
1238  case SymtabPresence::SelectivelyIncluded:
1239    localSymbolsHandler = [&](Symbol *sym) {
1240      if (config->localSymbolPatterns.match(sym->getName()))
1241        addSymbol(localSymbols, sym);
1242    };
1243    break;
1244  case SymtabPresence::SelectivelyExcluded:
1245    localSymbolsHandler = [&](Symbol *sym) {
1246      if (!config->localSymbolPatterns.match(sym->getName()))
1247        addSymbol(localSymbols, sym);
1248    };
1249    break;
1250  }
1251
1252  // Local symbols aren't in the SymbolTable, so we walk the list of object
1253  // files to gather them.
1254  // But if `-x` is set, then we don't need to. localSymbolsHandler() will do
1255  // the right thing regardless, but this check is a perf optimization because
1256  // iterating through all the input files and their symbols is expensive.
1257  if (config->localSymbolsPresence != SymtabPresence::None) {
1258    for (const InputFile *file : inputFiles) {
1259      if (auto *objFile = dyn_cast<ObjFile>(file)) {
1260        for (Symbol *sym : objFile->symbols) {
1261          if (auto *defined = dyn_cast_or_null<Defined>(sym)) {
1262            if (defined->isExternal() || !defined->isLive() ||
1263                !defined->includeInSymtab)
1264              continue;
1265            localSymbolsHandler(sym);
1266          }
1267        }
1268      }
1269    }
1270  }
1271
1272  // __dyld_private is a local symbol too. It's linker-created and doesn't
1273  // exist in any object file.
1274  if (in.stubHelper && in.stubHelper->dyldPrivate)
1275    localSymbolsHandler(in.stubHelper->dyldPrivate);
1276
1277  for (Symbol *sym : symtab->getSymbols()) {
1278    if (!sym->isLive())
1279      continue;
1280    if (auto *defined = dyn_cast<Defined>(sym)) {
1281      if (!defined->includeInSymtab)
1282        continue;
1283      assert(defined->isExternal());
1284      if (defined->privateExtern)
1285        localSymbolsHandler(defined);
1286      else
1287        addSymbol(externalSymbols, defined);
1288    } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
1289      if (dysym->isReferenced())
1290        addSymbol(undefinedSymbols, sym);
1291    }
1292  }
1293
1294  emitStabs();
1295  uint32_t symtabIndex = stabs.size();
1296  for (const SymtabEntry &entry :
1297       concat<SymtabEntry>(localSymbols, externalSymbols, undefinedSymbols)) {
1298    entry.sym->symtabIndex = symtabIndex++;
1299  }
1300}
1301
1302uint32_t SymtabSection::getNumSymbols() const {
1303  return stabs.size() + localSymbols.size() + externalSymbols.size() +
1304         undefinedSymbols.size();
1305}
1306
1307// This serves to hide (type-erase) the template parameter from SymtabSection.
1308template <class LP> class SymtabSectionImpl final : public SymtabSection {
1309public:
1310  SymtabSectionImpl(StringTableSection &stringTableSection)
1311      : SymtabSection(stringTableSection) {}
1312  uint64_t getRawSize() const override;
1313  void writeTo(uint8_t *buf) const override;
1314};
1315
1316template <class LP> uint64_t SymtabSectionImpl<LP>::getRawSize() const {
1317  return getNumSymbols() * sizeof(typename LP::nlist);
1318}
1319
1320template <class LP> void SymtabSectionImpl<LP>::writeTo(uint8_t *buf) const {
1321  auto *nList = reinterpret_cast<typename LP::nlist *>(buf);
1322  // Emit the stabs entries before the "real" symbols. We cannot emit them
1323  // after as that would render Symbol::symtabIndex inaccurate.
1324  for (const StabsEntry &entry : stabs) {
1325    nList->n_strx = entry.strx;
1326    nList->n_type = entry.type;
1327    nList->n_sect = entry.sect;
1328    nList->n_desc = entry.desc;
1329    nList->n_value = entry.value;
1330    ++nList;
1331  }
1332
1333  for (const SymtabEntry &entry : concat<const SymtabEntry>(
1334           localSymbols, externalSymbols, undefinedSymbols)) {
1335    nList->n_strx = entry.strx;
1336    // TODO populate n_desc with more flags
1337    if (auto *defined = dyn_cast<Defined>(entry.sym)) {
1338      uint8_t scope = 0;
1339      if (defined->privateExtern) {
1340        // Private external -- dylib scoped symbol.
1341        // Promote to non-external at link time.
1342        scope = N_PEXT;
1343      } else if (defined->isExternal()) {
1344        // Normal global symbol.
1345        scope = N_EXT;
1346      } else {
1347        // TU-local symbol from localSymbols.
1348        scope = 0;
1349      }
1350
1351      if (defined->isAbsolute()) {
1352        nList->n_type = scope | N_ABS;
1353        nList->n_sect = NO_SECT;
1354        nList->n_value = defined->value;
1355      } else {
1356        nList->n_type = scope | N_SECT;
1357        nList->n_sect = defined->isec->parent->index;
1358        // For the N_SECT symbol type, n_value is the address of the symbol
1359        nList->n_value = defined->getVA();
1360      }
1361      nList->n_desc |= defined->isExternalWeakDef() ? N_WEAK_DEF : 0;
1362      nList->n_desc |=
1363          defined->referencedDynamically ? REFERENCED_DYNAMICALLY : 0;
1364    } else if (auto *dysym = dyn_cast<DylibSymbol>(entry.sym)) {
1365      uint16_t n_desc = nList->n_desc;
1366      int16_t ordinal = ordinalForDylibSymbol(*dysym);
1367      if (ordinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
1368        SET_LIBRARY_ORDINAL(n_desc, DYNAMIC_LOOKUP_ORDINAL);
1369      else if (ordinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
1370        SET_LIBRARY_ORDINAL(n_desc, EXECUTABLE_ORDINAL);
1371      else {
1372        assert(ordinal > 0);
1373        SET_LIBRARY_ORDINAL(n_desc, static_cast<uint8_t>(ordinal));
1374      }
1375
1376      nList->n_type = N_EXT;
1377      n_desc |= dysym->isWeakDef() ? N_WEAK_DEF : 0;
1378      n_desc |= dysym->isWeakRef() ? N_WEAK_REF : 0;
1379      nList->n_desc = n_desc;
1380    }
1381    ++nList;
1382  }
1383}
1384
1385template <class LP>
1386SymtabSection *
1387macho::makeSymtabSection(StringTableSection &stringTableSection) {
1388  return make<SymtabSectionImpl<LP>>(stringTableSection);
1389}
1390
1391IndirectSymtabSection::IndirectSymtabSection()
1392    : LinkEditSection(segment_names::linkEdit,
1393                      section_names::indirectSymbolTable) {}
1394
1395uint32_t IndirectSymtabSection::getNumSymbols() const {
1396  uint32_t size = in.got->getEntries().size() +
1397                  in.tlvPointers->getEntries().size() +
1398                  in.stubs->getEntries().size();
1399  if (!config->emitChainedFixups)
1400    size += in.stubs->getEntries().size();
1401  return size;
1402}
1403
1404bool IndirectSymtabSection::isNeeded() const {
1405  return in.got->isNeeded() || in.tlvPointers->isNeeded() ||
1406         in.stubs->isNeeded();
1407}
1408
1409void IndirectSymtabSection::finalizeContents() {
1410  uint32_t off = 0;
1411  in.got->reserved1 = off;
1412  off += in.got->getEntries().size();
1413  in.tlvPointers->reserved1 = off;
1414  off += in.tlvPointers->getEntries().size();
1415  in.stubs->reserved1 = off;
1416  if (in.lazyPointers) {
1417    off += in.stubs->getEntries().size();
1418    in.lazyPointers->reserved1 = off;
1419  }
1420}
1421
1422static uint32_t indirectValue(const Symbol *sym) {
1423  if (sym->symtabIndex == UINT32_MAX)
1424    return INDIRECT_SYMBOL_LOCAL;
1425  if (auto *defined = dyn_cast<Defined>(sym))
1426    if (defined->privateExtern)
1427      return INDIRECT_SYMBOL_LOCAL;
1428  return sym->symtabIndex;
1429}
1430
1431void IndirectSymtabSection::writeTo(uint8_t *buf) const {
1432  uint32_t off = 0;
1433  for (const Symbol *sym : in.got->getEntries()) {
1434    write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1435    ++off;
1436  }
1437  for (const Symbol *sym : in.tlvPointers->getEntries()) {
1438    write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1439    ++off;
1440  }
1441  for (const Symbol *sym : in.stubs->getEntries()) {
1442    write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1443    ++off;
1444  }
1445
1446  if (in.lazyPointers) {
1447    // There is a 1:1 correspondence between stubs and LazyPointerSection
1448    // entries. But giving __stubs and __la_symbol_ptr the same reserved1
1449    // (the offset into the indirect symbol table) so that they both refer
1450    // to the same range of offsets confuses `strip`, so write the stubs
1451    // symbol table offsets a second time.
1452    for (const Symbol *sym : in.stubs->getEntries()) {
1453      write32le(buf + off * sizeof(uint32_t), indirectValue(sym));
1454      ++off;
1455    }
1456  }
1457}
1458
1459StringTableSection::StringTableSection()
1460    : LinkEditSection(segment_names::linkEdit, section_names::stringTable) {}
1461
1462uint32_t StringTableSection::addString(StringRef str) {
1463  uint32_t strx = size;
1464  strings.push_back(str); // TODO: consider deduplicating strings
1465  size += str.size() + 1; // account for null terminator
1466  return strx;
1467}
1468
1469void StringTableSection::writeTo(uint8_t *buf) const {
1470  uint32_t off = 0;
1471  for (StringRef str : strings) {
1472    memcpy(buf + off, str.data(), str.size());
1473    off += str.size() + 1; // account for null terminator
1474  }
1475}
1476
1477static_assert((CodeSignatureSection::blobHeadersSize % 8) == 0);
1478static_assert((CodeSignatureSection::fixedHeadersSize % 8) == 0);
1479
1480CodeSignatureSection::CodeSignatureSection()
1481    : LinkEditSection(segment_names::linkEdit, section_names::codeSignature) {
1482  align = 16; // required by libstuff
1483
1484  // XXX: This mimics LD64, where it uses the install-name as codesign
1485  // identifier, if available.
1486  if (!config->installName.empty())
1487    fileName = config->installName;
1488  else
1489    // FIXME: Consider using finalOutput instead of outputFile.
1490    fileName = config->outputFile;
1491
1492  size_t slashIndex = fileName.rfind("/");
1493  if (slashIndex != std::string::npos)
1494    fileName = fileName.drop_front(slashIndex + 1);
1495
1496  // NOTE: Any changes to these calculations should be repeated
1497  // in llvm-objcopy's MachOLayoutBuilder::layoutTail.
1498  allHeadersSize = alignTo<16>(fixedHeadersSize + fileName.size() + 1);
1499  fileNamePad = allHeadersSize - fixedHeadersSize - fileName.size();
1500}
1501
1502uint32_t CodeSignatureSection::getBlockCount() const {
1503  return (fileOff + blockSize - 1) / blockSize;
1504}
1505
1506uint64_t CodeSignatureSection::getRawSize() const {
1507  return allHeadersSize + getBlockCount() * hashSize;
1508}
1509
1510void CodeSignatureSection::writeHashes(uint8_t *buf) const {
1511  // NOTE: Changes to this functionality should be repeated in llvm-objcopy's
1512  // MachOWriter::writeSignatureData.
1513  uint8_t *hashes = buf + fileOff + allHeadersSize;
1514  parallelFor(0, getBlockCount(), [&](size_t i) {
1515    sha256(buf + i * blockSize,
1516           std::min(static_cast<size_t>(fileOff - i * blockSize), blockSize),
1517           hashes + i * hashSize);
1518  });
1519#if defined(__APPLE__)
1520  // This is macOS-specific work-around and makes no sense for any
1521  // other host OS. See https://openradar.appspot.com/FB8914231
1522  //
1523  // The macOS kernel maintains a signature-verification cache to
1524  // quickly validate applications at time of execve(2).  The trouble
1525  // is that for the kernel creates the cache entry at the time of the
1526  // mmap(2) call, before we have a chance to write either the code to
1527  // sign or the signature header+hashes.  The fix is to invalidate
1528  // all cached data associated with the output file, thus discarding
1529  // the bogus prematurely-cached signature.
1530  msync(buf, fileOff + getSize(), MS_INVALIDATE);
1531#endif
1532}
1533
1534void CodeSignatureSection::writeTo(uint8_t *buf) const {
1535  // NOTE: Changes to this functionality should be repeated in llvm-objcopy's
1536  // MachOWriter::writeSignatureData.
1537  uint32_t signatureSize = static_cast<uint32_t>(getSize());
1538  auto *superBlob = reinterpret_cast<CS_SuperBlob *>(buf);
1539  write32be(&superBlob->magic, CSMAGIC_EMBEDDED_SIGNATURE);
1540  write32be(&superBlob->length, signatureSize);
1541  write32be(&superBlob->count, 1);
1542  auto *blobIndex = reinterpret_cast<CS_BlobIndex *>(&superBlob[1]);
1543  write32be(&blobIndex->type, CSSLOT_CODEDIRECTORY);
1544  write32be(&blobIndex->offset, blobHeadersSize);
1545  auto *codeDirectory =
1546      reinterpret_cast<CS_CodeDirectory *>(buf + blobHeadersSize);
1547  write32be(&codeDirectory->magic, CSMAGIC_CODEDIRECTORY);
1548  write32be(&codeDirectory->length, signatureSize - blobHeadersSize);
1549  write32be(&codeDirectory->version, CS_SUPPORTSEXECSEG);
1550  write32be(&codeDirectory->flags, CS_ADHOC | CS_LINKER_SIGNED);
1551  write32be(&codeDirectory->hashOffset,
1552            sizeof(CS_CodeDirectory) + fileName.size() + fileNamePad);
1553  write32be(&codeDirectory->identOffset, sizeof(CS_CodeDirectory));
1554  codeDirectory->nSpecialSlots = 0;
1555  write32be(&codeDirectory->nCodeSlots, getBlockCount());
1556  write32be(&codeDirectory->codeLimit, fileOff);
1557  codeDirectory->hashSize = static_cast<uint8_t>(hashSize);
1558  codeDirectory->hashType = kSecCodeSignatureHashSHA256;
1559  codeDirectory->platform = 0;
1560  codeDirectory->pageSize = blockSizeShift;
1561  codeDirectory->spare2 = 0;
1562  codeDirectory->scatterOffset = 0;
1563  codeDirectory->teamOffset = 0;
1564  codeDirectory->spare3 = 0;
1565  codeDirectory->codeLimit64 = 0;
1566  OutputSegment *textSeg = getOrCreateOutputSegment(segment_names::text);
1567  write64be(&codeDirectory->execSegBase, textSeg->fileOff);
1568  write64be(&codeDirectory->execSegLimit, textSeg->fileSize);
1569  write64be(&codeDirectory->execSegFlags,
1570            config->outputType == MH_EXECUTE ? CS_EXECSEG_MAIN_BINARY : 0);
1571  auto *id = reinterpret_cast<char *>(&codeDirectory[1]);
1572  memcpy(id, fileName.begin(), fileName.size());
1573  memset(id + fileName.size(), 0, fileNamePad);
1574}
1575
1576CStringSection::CStringSection(const char *name)
1577    : SyntheticSection(segment_names::text, name) {
1578  flags = S_CSTRING_LITERALS;
1579}
1580
1581void CStringSection::addInput(CStringInputSection *isec) {
1582  isec->parent = this;
1583  inputs.push_back(isec);
1584  if (isec->align > align)
1585    align = isec->align;
1586}
1587
1588void CStringSection::writeTo(uint8_t *buf) const {
1589  for (const CStringInputSection *isec : inputs) {
1590    for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1591      if (!piece.live)
1592        continue;
1593      StringRef string = isec->getStringRef(i);
1594      memcpy(buf + piece.outSecOff, string.data(), string.size());
1595    }
1596  }
1597}
1598
1599void CStringSection::finalizeContents() {
1600  uint64_t offset = 0;
1601  for (CStringInputSection *isec : inputs) {
1602    for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1603      if (!piece.live)
1604        continue;
1605      // See comment above DeduplicatedCStringSection for how alignment is
1606      // handled.
1607      uint32_t pieceAlign = 1
1608                            << llvm::countr_zero(isec->align | piece.inSecOff);
1609      offset = alignToPowerOf2(offset, pieceAlign);
1610      piece.outSecOff = offset;
1611      isec->isFinal = true;
1612      StringRef string = isec->getStringRef(i);
1613      offset += string.size() + 1; // account for null terminator
1614    }
1615  }
1616  size = offset;
1617}
1618
1619// Mergeable cstring literals are found under the __TEXT,__cstring section. In
1620// contrast to ELF, which puts strings that need different alignments into
1621// different sections, clang's Mach-O backend puts them all in one section.
1622// Strings that need to be aligned have the .p2align directive emitted before
1623// them, which simply translates into zero padding in the object file. In other
1624// words, we have to infer the desired alignment of these cstrings from their
1625// addresses.
1626//
1627// We differ slightly from ld64 in how we've chosen to align these cstrings.
1628// Both LLD and ld64 preserve the number of trailing zeros in each cstring's
1629// address in the input object files. When deduplicating identical cstrings,
1630// both linkers pick the cstring whose address has more trailing zeros, and
1631// preserve the alignment of that address in the final binary. However, ld64
1632// goes a step further and also preserves the offset of the cstring from the
1633// last section-aligned address.  I.e. if a cstring is at offset 18 in the
1634// input, with a section alignment of 16, then both LLD and ld64 will ensure the
1635// final address is 2-byte aligned (since 18 == 16 + 2). But ld64 will also
1636// ensure that the final address is of the form 16 * k + 2 for some k.
1637//
1638// Note that ld64's heuristic means that a dedup'ed cstring's final address is
1639// dependent on the order of the input object files. E.g. if in addition to the
1640// cstring at offset 18 above, we have a duplicate one in another file with a
1641// `.cstring` section alignment of 2 and an offset of zero, then ld64 will pick
1642// the cstring from the object file earlier on the command line (since both have
1643// the same number of trailing zeros in their address). So the final cstring may
1644// either be at some address `16 * k + 2` or at some address `2 * k`.
1645//
1646// I've opted not to follow this behavior primarily for implementation
1647// simplicity, and secondarily to save a few more bytes. It's not clear to me
1648// that preserving the section alignment + offset is ever necessary, and there
1649// are many cases that are clearly redundant. In particular, if an x86_64 object
1650// file contains some strings that are accessed via SIMD instructions, then the
1651// .cstring section in the object file will be 16-byte-aligned (since SIMD
1652// requires its operand addresses to be 16-byte aligned). However, there will
1653// typically also be other cstrings in the same file that aren't used via SIMD
1654// and don't need this alignment. They will be emitted at some arbitrary address
1655// `A`, but ld64 will treat them as being 16-byte aligned with an offset of `16
1656// % A`.
1657void DeduplicatedCStringSection::finalizeContents() {
1658  // Find the largest alignment required for each string.
1659  for (const CStringInputSection *isec : inputs) {
1660    for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1661      if (!piece.live)
1662        continue;
1663      auto s = isec->getCachedHashStringRef(i);
1664      assert(isec->align != 0);
1665      uint8_t trailingZeros = llvm::countr_zero(isec->align | piece.inSecOff);
1666      auto it = stringOffsetMap.insert(
1667          std::make_pair(s, StringOffset(trailingZeros)));
1668      if (!it.second && it.first->second.trailingZeros < trailingZeros)
1669        it.first->second.trailingZeros = trailingZeros;
1670    }
1671  }
1672
1673  // Assign an offset for each string and save it to the corresponding
1674  // StringPieces for easy access.
1675  for (CStringInputSection *isec : inputs) {
1676    for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
1677      if (!piece.live)
1678        continue;
1679      auto s = isec->getCachedHashStringRef(i);
1680      auto it = stringOffsetMap.find(s);
1681      assert(it != stringOffsetMap.end());
1682      StringOffset &offsetInfo = it->second;
1683      if (offsetInfo.outSecOff == UINT64_MAX) {
1684        offsetInfo.outSecOff =
1685            alignToPowerOf2(size, 1ULL << offsetInfo.trailingZeros);
1686        size =
1687            offsetInfo.outSecOff + s.size() + 1; // account for null terminator
1688      }
1689      piece.outSecOff = offsetInfo.outSecOff;
1690    }
1691    isec->isFinal = true;
1692  }
1693}
1694
1695void DeduplicatedCStringSection::writeTo(uint8_t *buf) const {
1696  for (const auto &p : stringOffsetMap) {
1697    StringRef data = p.first.val();
1698    uint64_t off = p.second.outSecOff;
1699    if (!data.empty())
1700      memcpy(buf + off, data.data(), data.size());
1701  }
1702}
1703
1704DeduplicatedCStringSection::StringOffset
1705DeduplicatedCStringSection::getStringOffset(StringRef str) const {
1706  // StringPiece uses 31 bits to store the hashes, so we replicate that
1707  uint32_t hash = xxh3_64bits(str) & 0x7fffffff;
1708  auto offset = stringOffsetMap.find(CachedHashStringRef(str, hash));
1709  assert(offset != stringOffsetMap.end() &&
1710         "Looked-up strings should always exist in section");
1711  return offset->second;
1712}
1713
1714// This section is actually emitted as __TEXT,__const by ld64, but clang may
1715// emit input sections of that name, and LLD doesn't currently support mixing
1716// synthetic and concat-type OutputSections. To work around this, I've given
1717// our merged-literals section a different name.
1718WordLiteralSection::WordLiteralSection()
1719    : SyntheticSection(segment_names::text, section_names::literals) {
1720  align = 16;
1721}
1722
1723void WordLiteralSection::addInput(WordLiteralInputSection *isec) {
1724  isec->parent = this;
1725  inputs.push_back(isec);
1726}
1727
1728void WordLiteralSection::finalizeContents() {
1729  for (WordLiteralInputSection *isec : inputs) {
1730    // We do all processing of the InputSection here, so it will be effectively
1731    // finalized.
1732    isec->isFinal = true;
1733    const uint8_t *buf = isec->data.data();
1734    switch (sectionType(isec->getFlags())) {
1735    case S_4BYTE_LITERALS: {
1736      for (size_t off = 0, e = isec->data.size(); off < e; off += 4) {
1737        if (!isec->isLive(off))
1738          continue;
1739        uint32_t value = *reinterpret_cast<const uint32_t *>(buf + off);
1740        literal4Map.emplace(value, literal4Map.size());
1741      }
1742      break;
1743    }
1744    case S_8BYTE_LITERALS: {
1745      for (size_t off = 0, e = isec->data.size(); off < e; off += 8) {
1746        if (!isec->isLive(off))
1747          continue;
1748        uint64_t value = *reinterpret_cast<const uint64_t *>(buf + off);
1749        literal8Map.emplace(value, literal8Map.size());
1750      }
1751      break;
1752    }
1753    case S_16BYTE_LITERALS: {
1754      for (size_t off = 0, e = isec->data.size(); off < e; off += 16) {
1755        if (!isec->isLive(off))
1756          continue;
1757        UInt128 value = *reinterpret_cast<const UInt128 *>(buf + off);
1758        literal16Map.emplace(value, literal16Map.size());
1759      }
1760      break;
1761    }
1762    default:
1763      llvm_unreachable("invalid literal section type");
1764    }
1765  }
1766}
1767
1768void WordLiteralSection::writeTo(uint8_t *buf) const {
1769  // Note that we don't attempt to do any endianness conversion in addInput(),
1770  // so we don't do it here either -- just write out the original value,
1771  // byte-for-byte.
1772  for (const auto &p : literal16Map)
1773    memcpy(buf + p.second * 16, &p.first, 16);
1774  buf += literal16Map.size() * 16;
1775
1776  for (const auto &p : literal8Map)
1777    memcpy(buf + p.second * 8, &p.first, 8);
1778  buf += literal8Map.size() * 8;
1779
1780  for (const auto &p : literal4Map)
1781    memcpy(buf + p.second * 4, &p.first, 4);
1782}
1783
1784ObjCImageInfoSection::ObjCImageInfoSection()
1785    : SyntheticSection(segment_names::data, section_names::objCImageInfo) {}
1786
1787ObjCImageInfoSection::ImageInfo
1788ObjCImageInfoSection::parseImageInfo(const InputFile *file) {
1789  ImageInfo info;
1790  ArrayRef<uint8_t> data = file->objCImageInfo;
1791  // The image info struct has the following layout:
1792  // struct {
1793  //   uint32_t version;
1794  //   uint32_t flags;
1795  // };
1796  if (data.size() < 8) {
1797    warn(toString(file) + ": invalid __objc_imageinfo size");
1798    return info;
1799  }
1800
1801  auto *buf = reinterpret_cast<const uint32_t *>(data.data());
1802  if (read32le(buf) != 0) {
1803    warn(toString(file) + ": invalid __objc_imageinfo version");
1804    return info;
1805  }
1806
1807  uint32_t flags = read32le(buf + 1);
1808  info.swiftVersion = (flags >> 8) & 0xff;
1809  info.hasCategoryClassProperties = flags & 0x40;
1810  return info;
1811}
1812
1813static std::string swiftVersionString(uint8_t version) {
1814  switch (version) {
1815    case 1:
1816      return "1.0";
1817    case 2:
1818      return "1.1";
1819    case 3:
1820      return "2.0";
1821    case 4:
1822      return "3.0";
1823    case 5:
1824      return "4.0";
1825    default:
1826      return ("0x" + Twine::utohexstr(version)).str();
1827  }
1828}
1829
1830// Validate each object file's __objc_imageinfo and use them to generate the
1831// image info for the output binary. Only two pieces of info are relevant:
1832// 1. The Swift version (should be identical across inputs)
1833// 2. `bool hasCategoryClassProperties` (true only if true for all inputs)
1834void ObjCImageInfoSection::finalizeContents() {
1835  assert(files.size() != 0); // should have already been checked via isNeeded()
1836
1837  info.hasCategoryClassProperties = true;
1838  const InputFile *firstFile;
1839  for (const InputFile *file : files) {
1840    ImageInfo inputInfo = parseImageInfo(file);
1841    info.hasCategoryClassProperties &= inputInfo.hasCategoryClassProperties;
1842
1843    // swiftVersion 0 means no Swift is present, so no version checking required
1844    if (inputInfo.swiftVersion == 0)
1845      continue;
1846
1847    if (info.swiftVersion != 0 && info.swiftVersion != inputInfo.swiftVersion) {
1848      error("Swift version mismatch: " + toString(firstFile) + " has version " +
1849            swiftVersionString(info.swiftVersion) + " but " + toString(file) +
1850            " has version " + swiftVersionString(inputInfo.swiftVersion));
1851    } else {
1852      info.swiftVersion = inputInfo.swiftVersion;
1853      firstFile = file;
1854    }
1855  }
1856}
1857
1858void ObjCImageInfoSection::writeTo(uint8_t *buf) const {
1859  uint32_t flags = info.hasCategoryClassProperties ? 0x40 : 0x0;
1860  flags |= info.swiftVersion << 8;
1861  write32le(buf + 4, flags);
1862}
1863
1864InitOffsetsSection::InitOffsetsSection()
1865    : SyntheticSection(segment_names::text, section_names::initOffsets) {
1866  flags = S_INIT_FUNC_OFFSETS;
1867  align = 4; // This section contains 32-bit integers.
1868}
1869
1870uint64_t InitOffsetsSection::getSize() const {
1871  size_t count = 0;
1872  for (const ConcatInputSection *isec : sections)
1873    count += isec->relocs.size();
1874  return count * sizeof(uint32_t);
1875}
1876
1877void InitOffsetsSection::writeTo(uint8_t *buf) const {
1878  // FIXME: Add function specified by -init when that argument is implemented.
1879  for (ConcatInputSection *isec : sections) {
1880    for (const Reloc &rel : isec->relocs) {
1881      const Symbol *referent = rel.referent.dyn_cast<Symbol *>();
1882      assert(referent && "section relocation should have been rejected");
1883      uint64_t offset = referent->getVA() - in.header->addr;
1884      // FIXME: Can we handle this gracefully?
1885      if (offset > UINT32_MAX)
1886        fatal(isec->getLocation(rel.offset) + ": offset to initializer " +
1887              referent->getName() + " (" + utohexstr(offset) +
1888              ") does not fit in 32 bits");
1889
1890      // Entries need to be added in the order they appear in the section, but
1891      // relocations aren't guaranteed to be sorted.
1892      size_t index = rel.offset >> target->p2WordSize;
1893      write32le(&buf[index * sizeof(uint32_t)], offset);
1894    }
1895    buf += isec->relocs.size() * sizeof(uint32_t);
1896  }
1897}
1898
1899// The inputs are __mod_init_func sections, which contain pointers to
1900// initializer functions, therefore all relocations should be of the UNSIGNED
1901// type. InitOffsetsSection stores offsets, so if the initializer's address is
1902// not known at link time, stub-indirection has to be used.
1903void InitOffsetsSection::setUp() {
1904  for (const ConcatInputSection *isec : sections) {
1905    for (const Reloc &rel : isec->relocs) {
1906      RelocAttrs attrs = target->getRelocAttrs(rel.type);
1907      if (!attrs.hasAttr(RelocAttrBits::UNSIGNED))
1908        error(isec->getLocation(rel.offset) +
1909              ": unsupported relocation type: " + attrs.name);
1910      if (rel.addend != 0)
1911        error(isec->getLocation(rel.offset) +
1912              ": relocation addend is not representable in __init_offsets");
1913      if (rel.referent.is<InputSection *>())
1914        error(isec->getLocation(rel.offset) +
1915              ": unexpected section relocation");
1916
1917      Symbol *sym = rel.referent.dyn_cast<Symbol *>();
1918      if (auto *undefined = dyn_cast<Undefined>(sym))
1919        treatUndefinedSymbol(*undefined, isec, rel.offset);
1920      if (needsBinding(sym))
1921        in.stubs->addEntry(sym);
1922    }
1923  }
1924}
1925
1926void macho::createSyntheticSymbols() {
1927  auto addHeaderSymbol = [](const char *name) {
1928    symtab->addSynthetic(name, in.header->isec, /*value=*/0,
1929                         /*isPrivateExtern=*/true, /*includeInSymtab=*/false,
1930                         /*referencedDynamically=*/false);
1931  };
1932
1933  switch (config->outputType) {
1934    // FIXME: Assign the right address value for these symbols
1935    // (rather than 0). But we need to do that after assignAddresses().
1936  case MH_EXECUTE:
1937    // If linking PIE, __mh_execute_header is a defined symbol in
1938    //  __TEXT, __text)
1939    // Otherwise, it's an absolute symbol.
1940    if (config->isPic)
1941      symtab->addSynthetic("__mh_execute_header", in.header->isec, /*value=*/0,
1942                           /*isPrivateExtern=*/false, /*includeInSymtab=*/true,
1943                           /*referencedDynamically=*/true);
1944    else
1945      symtab->addSynthetic("__mh_execute_header", /*isec=*/nullptr, /*value=*/0,
1946                           /*isPrivateExtern=*/false, /*includeInSymtab=*/true,
1947                           /*referencedDynamically=*/true);
1948    break;
1949
1950    // The following symbols are N_SECT symbols, even though the header is not
1951    // part of any section and that they are private to the bundle/dylib/object
1952    // they are part of.
1953  case MH_BUNDLE:
1954    addHeaderSymbol("__mh_bundle_header");
1955    break;
1956  case MH_DYLIB:
1957    addHeaderSymbol("__mh_dylib_header");
1958    break;
1959  case MH_DYLINKER:
1960    addHeaderSymbol("__mh_dylinker_header");
1961    break;
1962  case MH_OBJECT:
1963    addHeaderSymbol("__mh_object_header");
1964    break;
1965  default:
1966    llvm_unreachable("unexpected outputType");
1967    break;
1968  }
1969
1970  // The Itanium C++ ABI requires dylibs to pass a pointer to __cxa_atexit
1971  // which does e.g. cleanup of static global variables. The ABI document
1972  // says that the pointer can point to any address in one of the dylib's
1973  // segments, but in practice ld64 seems to set it to point to the header,
1974  // so that's what's implemented here.
1975  addHeaderSymbol("___dso_handle");
1976}
1977
1978ChainedFixupsSection::ChainedFixupsSection()
1979    : LinkEditSection(segment_names::linkEdit, section_names::chainFixups) {}
1980
1981bool ChainedFixupsSection::isNeeded() const {
1982  assert(config->emitChainedFixups);
1983  // dyld always expects LC_DYLD_CHAINED_FIXUPS to point to a valid
1984  // dyld_chained_fixups_header, so we create this section even if there aren't
1985  // any fixups.
1986  return true;
1987}
1988
1989static bool needsWeakBind(const Symbol &sym) {
1990  if (auto *dysym = dyn_cast<DylibSymbol>(&sym))
1991    return dysym->isWeakDef();
1992  if (auto *defined = dyn_cast<Defined>(&sym))
1993    return defined->isExternalWeakDef();
1994  return false;
1995}
1996
1997void ChainedFixupsSection::addBinding(const Symbol *sym,
1998                                      const InputSection *isec, uint64_t offset,
1999                                      int64_t addend) {
2000  locations.emplace_back(isec, offset);
2001  int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0;
2002  auto [it, inserted] = bindings.insert(
2003      {{sym, outlineAddend}, static_cast<uint32_t>(bindings.size())});
2004
2005  if (inserted) {
2006    symtabSize += sym->getName().size() + 1;
2007    hasWeakBind = hasWeakBind || needsWeakBind(*sym);
2008    if (!isInt<23>(outlineAddend))
2009      needsLargeAddend = true;
2010    else if (outlineAddend != 0)
2011      needsAddend = true;
2012  }
2013}
2014
2015std::pair<uint32_t, uint8_t>
2016ChainedFixupsSection::getBinding(const Symbol *sym, int64_t addend) const {
2017  int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0;
2018  auto it = bindings.find({sym, outlineAddend});
2019  assert(it != bindings.end() && "binding not found in the imports table");
2020  if (outlineAddend == 0)
2021    return {it->second, addend};
2022  return {it->second, 0};
2023}
2024
2025static size_t writeImport(uint8_t *buf, int format, uint32_t libOrdinal,
2026                          bool weakRef, uint32_t nameOffset, int64_t addend) {
2027  switch (format) {
2028  case DYLD_CHAINED_IMPORT: {
2029    auto *import = reinterpret_cast<dyld_chained_import *>(buf);
2030    import->lib_ordinal = libOrdinal;
2031    import->weak_import = weakRef;
2032    import->name_offset = nameOffset;
2033    return sizeof(dyld_chained_import);
2034  }
2035  case DYLD_CHAINED_IMPORT_ADDEND: {
2036    auto *import = reinterpret_cast<dyld_chained_import_addend *>(buf);
2037    import->lib_ordinal = libOrdinal;
2038    import->weak_import = weakRef;
2039    import->name_offset = nameOffset;
2040    import->addend = addend;
2041    return sizeof(dyld_chained_import_addend);
2042  }
2043  case DYLD_CHAINED_IMPORT_ADDEND64: {
2044    auto *import = reinterpret_cast<dyld_chained_import_addend64 *>(buf);
2045    import->lib_ordinal = libOrdinal;
2046    import->weak_import = weakRef;
2047    import->name_offset = nameOffset;
2048    import->addend = addend;
2049    return sizeof(dyld_chained_import_addend64);
2050  }
2051  default:
2052    llvm_unreachable("Unknown import format");
2053  }
2054}
2055
2056size_t ChainedFixupsSection::SegmentInfo::getSize() const {
2057  assert(pageStarts.size() > 0 && "SegmentInfo for segment with no fixups?");
2058  return alignTo<8>(sizeof(dyld_chained_starts_in_segment) +
2059                    pageStarts.back().first * sizeof(uint16_t));
2060}
2061
2062size_t ChainedFixupsSection::SegmentInfo::writeTo(uint8_t *buf) const {
2063  auto *segInfo = reinterpret_cast<dyld_chained_starts_in_segment *>(buf);
2064  segInfo->size = getSize();
2065  segInfo->page_size = target->getPageSize();
2066  // FIXME: Use DYLD_CHAINED_PTR_64_OFFSET on newer OS versions.
2067  segInfo->pointer_format = DYLD_CHAINED_PTR_64;
2068  segInfo->segment_offset = oseg->addr - in.header->addr;
2069  segInfo->max_valid_pointer = 0; // not used on 64-bit
2070  segInfo->page_count = pageStarts.back().first + 1;
2071
2072  uint16_t *starts = segInfo->page_start;
2073  for (size_t i = 0; i < segInfo->page_count; ++i)
2074    starts[i] = DYLD_CHAINED_PTR_START_NONE;
2075
2076  for (auto [pageIdx, startAddr] : pageStarts)
2077    starts[pageIdx] = startAddr;
2078  return segInfo->size;
2079}
2080
2081static size_t importEntrySize(int format) {
2082  switch (format) {
2083  case DYLD_CHAINED_IMPORT:
2084    return sizeof(dyld_chained_import);
2085  case DYLD_CHAINED_IMPORT_ADDEND:
2086    return sizeof(dyld_chained_import_addend);
2087  case DYLD_CHAINED_IMPORT_ADDEND64:
2088    return sizeof(dyld_chained_import_addend64);
2089  default:
2090    llvm_unreachable("Unknown import format");
2091  }
2092}
2093
2094// This is step 3 of the algorithm described in the class comment of
2095// ChainedFixupsSection.
2096//
2097// LC_DYLD_CHAINED_FIXUPS data consists of (in this order):
2098// * A dyld_chained_fixups_header
2099// * A dyld_chained_starts_in_image
2100// * One dyld_chained_starts_in_segment per segment
2101// * List of all imports (dyld_chained_import, dyld_chained_import_addend, or
2102//   dyld_chained_import_addend64)
2103// * Names of imported symbols
2104void ChainedFixupsSection::writeTo(uint8_t *buf) const {
2105  auto *header = reinterpret_cast<dyld_chained_fixups_header *>(buf);
2106  header->fixups_version = 0;
2107  header->imports_count = bindings.size();
2108  header->imports_format = importFormat;
2109  header->symbols_format = 0;
2110
2111  buf += alignTo<8>(sizeof(*header));
2112
2113  auto curOffset = [&buf, &header]() -> uint32_t {
2114    return buf - reinterpret_cast<uint8_t *>(header);
2115  };
2116
2117  header->starts_offset = curOffset();
2118
2119  auto *imageInfo = reinterpret_cast<dyld_chained_starts_in_image *>(buf);
2120  imageInfo->seg_count = outputSegments.size();
2121  uint32_t *segStarts = imageInfo->seg_info_offset;
2122
2123  // dyld_chained_starts_in_image ends in a flexible array member containing an
2124  // uint32_t for each segment. Leave room for it, and fill it via segStarts.
2125  buf += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset) +
2126                    outputSegments.size() * sizeof(uint32_t));
2127
2128  // Initialize all offsets to 0, which indicates that the segment does not have
2129  // fixups. Those that do have them will be filled in below.
2130  for (size_t i = 0; i < outputSegments.size(); ++i)
2131    segStarts[i] = 0;
2132
2133  for (const SegmentInfo &seg : fixupSegments) {
2134    segStarts[seg.oseg->index] = curOffset() - header->starts_offset;
2135    buf += seg.writeTo(buf);
2136  }
2137
2138  // Write imports table.
2139  header->imports_offset = curOffset();
2140  uint64_t nameOffset = 0;
2141  for (auto [import, idx] : bindings) {
2142    const Symbol &sym = *import.first;
2143    int16_t libOrdinal = needsWeakBind(sym)
2144                             ? (int64_t)BIND_SPECIAL_DYLIB_WEAK_LOOKUP
2145                             : ordinalForSymbol(sym);
2146    buf += writeImport(buf, importFormat, libOrdinal, sym.isWeakRef(),
2147                       nameOffset, import.second);
2148    nameOffset += sym.getName().size() + 1;
2149  }
2150
2151  // Write imported symbol names.
2152  header->symbols_offset = curOffset();
2153  for (auto [import, idx] : bindings) {
2154    StringRef name = import.first->getName();
2155    memcpy(buf, name.data(), name.size());
2156    buf += name.size() + 1; // account for null terminator
2157  }
2158
2159  assert(curOffset() == getRawSize());
2160}
2161
2162// This is step 2 of the algorithm described in the class comment of
2163// ChainedFixupsSection.
2164void ChainedFixupsSection::finalizeContents() {
2165  assert(target->wordSize == 8 && "Only 64-bit platforms are supported");
2166  assert(config->emitChainedFixups);
2167
2168  if (!isUInt<32>(symtabSize))
2169    error("cannot encode chained fixups: imported symbols table size " +
2170          Twine(symtabSize) + " exceeds 4 GiB");
2171
2172  if (needsLargeAddend || !isUInt<23>(symtabSize))
2173    importFormat = DYLD_CHAINED_IMPORT_ADDEND64;
2174  else if (needsAddend)
2175    importFormat = DYLD_CHAINED_IMPORT_ADDEND;
2176  else
2177    importFormat = DYLD_CHAINED_IMPORT;
2178
2179  for (Location &loc : locations)
2180    loc.offset =
2181        loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset);
2182
2183  llvm::sort(locations, [](const Location &a, const Location &b) {
2184    const OutputSegment *segA = a.isec->parent->parent;
2185    const OutputSegment *segB = b.isec->parent->parent;
2186    if (segA == segB)
2187      return a.offset < b.offset;
2188    return segA->addr < segB->addr;
2189  });
2190
2191  auto sameSegment = [](const Location &a, const Location &b) {
2192    return a.isec->parent->parent == b.isec->parent->parent;
2193  };
2194
2195  const uint64_t pageSize = target->getPageSize();
2196  for (size_t i = 0, count = locations.size(); i < count;) {
2197    const Location &firstLoc = locations[i];
2198    fixupSegments.emplace_back(firstLoc.isec->parent->parent);
2199    while (i < count && sameSegment(locations[i], firstLoc)) {
2200      uint32_t pageIdx = locations[i].offset / pageSize;
2201      fixupSegments.back().pageStarts.emplace_back(
2202          pageIdx, locations[i].offset % pageSize);
2203      ++i;
2204      while (i < count && sameSegment(locations[i], firstLoc) &&
2205             locations[i].offset / pageSize == pageIdx)
2206        ++i;
2207    }
2208  }
2209
2210  // Compute expected encoded size.
2211  size = alignTo<8>(sizeof(dyld_chained_fixups_header));
2212  size += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset) +
2213                     outputSegments.size() * sizeof(uint32_t));
2214  for (const SegmentInfo &seg : fixupSegments)
2215    size += seg.getSize();
2216  size += importEntrySize(importFormat) * bindings.size();
2217  size += symtabSize;
2218}
2219
2220template SymtabSection *macho::makeSymtabSection<LP64>(StringTableSection &);
2221template SymtabSection *macho::makeSymtabSection<ILP32>(StringTableSection &);
2222