brintos

brintos / llvm-project-archived public Read only

0
0
Text · 32.1 KiB · a37dd66 Raw
891 lines · c
1//===- SyntheticSections.h -------------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLD_MACHO_SYNTHETIC_SECTIONS_H10#define LLD_MACHO_SYNTHETIC_SECTIONS_H11 12#include "Config.h"13#include "ExportTrie.h"14#include "InputSection.h"15#include "OutputSection.h"16#include "OutputSegment.h"17#include "Target.h"18#include "Writer.h"19 20#include "llvm/ADT/DenseMap.h"21#include "llvm/ADT/Hashing.h"22#include "llvm/ADT/MapVector.h"23#include "llvm/ADT/SetVector.h"24#include "llvm/BinaryFormat/MachO.h"25#include "llvm/Support/MathExtras.h"26#include "llvm/Support/raw_ostream.h"27 28#include <unordered_map>29 30namespace llvm {31class DWARFUnit;32} // namespace llvm33 34namespace lld::macho {35 36class Defined;37class DylibSymbol;38class LoadCommand;39class ObjFile;40class UnwindInfoSection;41 42class SyntheticSection : public OutputSection {43public:44  SyntheticSection(const char *segname, const char *name);45  virtual ~SyntheticSection() = default;46 47  static bool classof(const OutputSection *sec) {48    return sec->kind() == SyntheticKind;49  }50 51  StringRef segname;52  // This fake InputSection makes it easier for us to write code that applies53  // generically to both user inputs and synthetics.54  InputSection *isec;55};56 57// All sections in __LINKEDIT should inherit from this.58class LinkEditSection : public SyntheticSection {59public:60  LinkEditSection(const char *segname, const char *name)61      : SyntheticSection(segname, name) {62    align = target->wordSize;63  }64 65  // Implementations of this method can assume that the regular (non-__LINKEDIT)66  // sections already have their addresses assigned.67  virtual void finalizeContents() {}68 69  // Sections in __LINKEDIT are special: their offsets are recorded in the70  // load commands like LC_DYLD_INFO_ONLY and LC_SYMTAB, instead of in section71  // headers.72  bool isHidden() const final { return true; }73 74  virtual uint64_t getRawSize() const = 0;75 76  // codesign (or more specifically libstuff) checks that each section in77  // __LINKEDIT ends where the next one starts -- no gaps are permitted. We78  // therefore align every section's start and end points to WordSize.79  //80  // NOTE: This assumes that the extra bytes required for alignment can be81  // zero-valued bytes.82  uint64_t getSize() const final { return llvm::alignTo(getRawSize(), align); }83};84 85// The header of the Mach-O file, which must have a file offset of zero.86class MachHeaderSection final : public SyntheticSection {87public:88  MachHeaderSection();89  bool isHidden() const override { return true; }90  uint64_t getSize() const override;91  void writeTo(uint8_t *buf) const override;92 93  void addLoadCommand(LoadCommand *);94 95protected:96  std::vector<LoadCommand *> loadCommands;97  uint32_t sizeOfCmds = 0;98};99 100// A hidden section that exists solely for the purpose of creating the101// __PAGEZERO segment, which is used to catch null pointer dereferences.102class PageZeroSection final : public SyntheticSection {103public:104  PageZeroSection();105  bool isHidden() const override { return true; }106  bool isNeeded() const override { return target->pageZeroSize != 0; }107  uint64_t getSize() const override { return target->pageZeroSize; }108  uint64_t getFileSize() const override { return 0; }109  void writeTo(uint8_t *buf) const override {}110};111 112// This is the base class for the GOT and TLVPointer sections, which are nearly113// functionally identical -- they will both be populated by dyld with addresses114// to non-lazily-loaded dylib symbols. The main difference is that the115// TLVPointerSection stores references to thread-local variables.116class NonLazyPointerSectionBase : public SyntheticSection {117public:118  NonLazyPointerSectionBase(const char *segname, const char *name);119  const llvm::SetVector<const Symbol *> &getEntries() const { return entries; }120  bool isNeeded() const override { return !entries.empty(); }121  uint64_t getSize() const override {122    return entries.size() * target->wordSize;123  }124  void writeTo(uint8_t *buf) const override;125  void addEntry(Symbol *sym);126  uint64_t getVA(uint32_t gotIndex) const {127    return addr + gotIndex * target->wordSize;128  }129 130private:131  llvm::SetVector<const Symbol *> entries;132};133 134class GotSection final : public NonLazyPointerSectionBase {135public:136  GotSection();137};138 139class TlvPointerSection final : public NonLazyPointerSectionBase {140public:141  TlvPointerSection();142};143 144struct Location {145  const InputSection *isec;146  uint64_t offset;147 148  Location(const InputSection *isec, uint64_t offset)149      : isec(isec), offset(offset) {}150  uint64_t getVA() const { return isec->getVA(offset); }151};152 153// Stores rebase opcodes, which tell dyld where absolute addresses have been154// encoded in the binary. If the binary is not loaded at its preferred address,155// dyld has to rebase these addresses by adding an offset to them.156class RebaseSection final : public LinkEditSection {157public:158  RebaseSection();159  void finalizeContents() override;160  uint64_t getRawSize() const override { return contents.size(); }161  bool isNeeded() const override { return !locations.empty(); }162  void writeTo(uint8_t *buf) const override;163 164  void addEntry(const InputSection *isec, uint64_t offset) {165    if (config->isPic)166      locations.emplace_back(isec, offset);167  }168 169private:170  std::vector<Location> locations;171  SmallVector<char, 128> contents;172};173 174struct BindingEntry {175  int64_t addend;176  Location target;177  BindingEntry(int64_t addend, Location target)178      : addend(addend), target(target) {}179};180 181template <class Sym>182using BindingsMap = llvm::DenseMap<Sym, std::vector<BindingEntry>>;183 184// Stores bind opcodes for telling dyld which symbols to load non-lazily.185class BindingSection final : public LinkEditSection {186public:187  BindingSection();188  void finalizeContents() override;189  uint64_t getRawSize() const override { return contents.size(); }190  bool isNeeded() const override { return !bindingsMap.empty(); }191  void writeTo(uint8_t *buf) const override;192 193  void addEntry(const Symbol *dysym, const InputSection *isec, uint64_t offset,194                int64_t addend = 0) {195    bindingsMap[dysym].emplace_back(addend, Location(isec, offset));196  }197 198private:199  BindingsMap<const Symbol *> bindingsMap;200  SmallVector<char, 128> contents;201};202 203// Stores bind opcodes for telling dyld which weak symbols need coalescing.204// There are two types of entries in this section:205//206//   1) Non-weak definitions: This is a symbol definition that weak symbols in207//   other dylibs should coalesce to.208//209//   2) Weak bindings: These tell dyld that a given symbol reference should210//   coalesce to a non-weak definition if one is found. Note that unlike the211//   entries in the BindingSection, the bindings here only refer to these212//   symbols by name, but do not specify which dylib to load them from.213class WeakBindingSection final : public LinkEditSection {214public:215  WeakBindingSection();216  void finalizeContents() override;217  uint64_t getRawSize() const override { return contents.size(); }218  bool isNeeded() const override {219    return !bindingsMap.empty() || !definitions.empty();220  }221 222  void writeTo(uint8_t *buf) const override;223 224  void addEntry(const Symbol *symbol, const InputSection *isec, uint64_t offset,225                int64_t addend = 0) {226    bindingsMap[symbol].emplace_back(addend, Location(isec, offset));227  }228 229  bool hasEntry() const { return !bindingsMap.empty(); }230 231  void addNonWeakDefinition(const Defined *defined) {232    definitions.emplace_back(defined);233  }234 235  bool hasNonWeakDefinition() const { return !definitions.empty(); }236 237private:238  BindingsMap<const Symbol *> bindingsMap;239  std::vector<const Defined *> definitions;240  SmallVector<char, 128> contents;241};242 243// The following sections implement lazy symbol binding -- very similar to the244// PLT mechanism in ELF.245//246// ELF's .plt section is broken up into two sections in Mach-O: StubsSection247// and StubHelperSection. Calls to functions in dylibs will end up calling into248// StubsSection, which contains indirect jumps to addresses stored in the249// LazyPointerSection (the counterpart to ELF's .plt.got).250//251// We will first describe how non-weak symbols are handled.252//253// At program start, the LazyPointerSection contains addresses that point into254// one of the entry points in the middle of the StubHelperSection. The code in255// StubHelperSection will push on the stack an offset into the256// LazyBindingSection. The push is followed by a jump to the beginning of the257// StubHelperSection (similar to PLT0), which then calls into dyld_stub_binder.258// dyld_stub_binder is a non-lazily-bound symbol, so this call looks it up in259// the GOT.260//261// The stub binder will look up the bind opcodes in the LazyBindingSection at262// the given offset. The bind opcodes will tell the binder to update the263// address in the LazyPointerSection to point to the symbol, so that subsequent264// calls don't have to redo the symbol resolution. The binder will then jump to265// the resolved symbol.266//267// With weak symbols, the situation is slightly different. Since there is no268// "weak lazy" lookup, function calls to weak symbols are always non-lazily269// bound. We emit both regular non-lazy bindings as well as weak bindings, in270// order that the weak bindings may overwrite the non-lazy bindings if an271// appropriate symbol is found at runtime. However, the bound addresses will272// still be written (non-lazily) into the LazyPointerSection.273//274// Symbols are always bound eagerly when chained fixups are used. In that case,275// StubsSection contains indirect jumps to addresses stored in the GotSection.276// The GOT directly contains the fixup entries, which will be replaced by the277// address of the target symbols on load. LazyPointerSection and278// StubHelperSection are not used.279 280class StubsSection final : public SyntheticSection {281public:282  StubsSection();283  uint64_t getSize() const override;284  bool isNeeded() const override { return !entries.empty(); }285  void finalize() override;286  void writeTo(uint8_t *buf) const override;287  const llvm::SetVector<Symbol *> &getEntries() const { return entries; }288  // Creates a stub for the symbol and the corresponding entry in the289  // LazyPointerSection.290  void addEntry(Symbol *);291  uint64_t getVA(uint32_t stubsIndex) const {292    assert(isFinal || target->usesThunks());293    // ConcatOutputSection::finalize() can seek the address of a294    // stub before its address is assigned. Before __stubs is295    // finalized, return a contrived out-of-range address.296    return isFinal ? addr + stubsIndex * target->stubSize297                   : TargetInfo::outOfRangeVA;298  }299 300  bool isFinal = false; // is address assigned?301 302private:303  llvm::SetVector<Symbol *> entries;304};305 306class StubHelperSection final : public SyntheticSection {307public:308  StubHelperSection();309  uint64_t getSize() const override;310  bool isNeeded() const override;311  void writeTo(uint8_t *buf) const override;312 313  void setUp();314 315  DylibSymbol *stubBinder = nullptr;316  Defined *dyldPrivate = nullptr;317};318 319class ObjCSelRefsHelper {320public:321  static void initialize();322  static void cleanup();323 324  static ConcatInputSection *getSelRef(StringRef methname);325  static ConcatInputSection *makeSelRef(StringRef methname);326 327private:328  static llvm::DenseMap<llvm::CachedHashStringRef, ConcatInputSection *>329      methnameToSelref;330};331 332// Objective-C stubs are hoisted objc_msgSend calls per selector called in the333// program. Apple Clang produces undefined symbols to each stub, such as334// '_objc_msgSend$foo', which are then synthesized by the linker. The stubs335// load the particular selector 'foo' from __objc_selrefs, setting it to the336// first argument of the objc_msgSend call, and then jumps to objc_msgSend. The337// actual stub contents are mirrored from ld64.338class ObjCStubsSection final : public SyntheticSection {339public:340  ObjCStubsSection();341  void addEntry(Symbol *sym);342  uint64_t getSize() const override;343  bool isNeeded() const override { return !symbols.empty(); }344  void finalize() override { isec->isFinal = true; }345  void writeTo(uint8_t *buf) const override;346  void setUp();347 348  static constexpr llvm::StringLiteral symbolPrefix = "_objc_msgSend$";349  static bool isObjCStubSymbol(Symbol *sym);350  static StringRef getMethname(Symbol *sym);351 352private:353  std::vector<Defined *> symbols;354  Symbol *objcMsgSend = nullptr;355};356 357// Note that this section may also be targeted by non-lazy bindings. In358// particular, this happens when branch relocations target weak symbols.359class LazyPointerSection final : public SyntheticSection {360public:361  LazyPointerSection();362  uint64_t getSize() const override;363  bool isNeeded() const override;364  void writeTo(uint8_t *buf) const override;365  uint64_t getVA(uint32_t index) const {366    return addr + (index << target->p2WordSize);367  }368};369 370class LazyBindingSection final : public LinkEditSection {371public:372  LazyBindingSection();373  void finalizeContents() override;374  uint64_t getRawSize() const override { return contents.size(); }375  bool isNeeded() const override { return !entries.empty(); }376  void writeTo(uint8_t *buf) const override;377  // Note that every entry here will by referenced by a corresponding entry in378  // the StubHelperSection.379  void addEntry(Symbol *dysym);380  const llvm::SetVector<Symbol *> &getEntries() const { return entries; }381 382private:383  uint32_t encode(const Symbol &);384 385  llvm::SetVector<Symbol *> entries;386  SmallVector<char, 128> contents;387  llvm::raw_svector_ostream os{contents};388};389 390// Stores a trie that describes the set of exported symbols.391class ExportSection final : public LinkEditSection {392public:393  ExportSection();394  void finalizeContents() override;395  uint64_t getRawSize() const override { return size; }396  bool isNeeded() const override { return size; }397  void writeTo(uint8_t *buf) const override;398 399  bool hasWeakSymbol = false;400 401private:402  TrieBuilder trieBuilder;403  size_t size = 0;404};405 406// Stores 'data in code' entries that describe the locations of data regions407// inside code sections. This is used by llvm-objdump to distinguish jump tables408// and stop them from being disassembled as instructions.409class DataInCodeSection final : public LinkEditSection {410public:411  DataInCodeSection();412  void finalizeContents() override;413  uint64_t getRawSize() const override {414    return sizeof(llvm::MachO::data_in_code_entry) * entries.size();415  }416  void writeTo(uint8_t *buf) const override;417 418private:419  std::vector<llvm::MachO::data_in_code_entry> entries;420};421 422// Stores ULEB128 delta encoded addresses of functions.423class FunctionStartsSection final : public LinkEditSection {424public:425  FunctionStartsSection();426  void finalizeContents() override;427  uint64_t getRawSize() const override { return contents.size(); }428  void writeTo(uint8_t *buf) const override;429 430private:431  SmallVector<char, 128> contents;432};433 434// Stores the strings referenced by the symbol table.435class StringTableSection final : public LinkEditSection {436public:437  StringTableSection();438  // Returns the start offset of the added string.439  uint32_t addString(StringRef);440  uint64_t getRawSize() const override { return size; }441  void writeTo(uint8_t *buf) const override;442 443  static constexpr size_t emptyStringIndex = 1;444 445private:446  // ld64 emits string tables which start with a space and a zero byte. We447  // match its behavior here since some tools depend on it.448  // Consequently, the empty string will be at index 1, not zero.449  std::vector<StringRef> strings{" "};450  llvm::DenseMap<llvm::CachedHashStringRef, uint32_t> stringMap;451  size_t size = 2;452};453 454struct SymtabEntry {455  Symbol *sym;456  size_t strx;457};458 459struct StabsEntry {460  uint8_t type = 0;461  uint32_t strx = StringTableSection::emptyStringIndex;462  uint8_t sect = 0;463  uint16_t desc = 0;464  uint64_t value = 0;465 466  StabsEntry() = default;467  explicit StabsEntry(uint8_t type) : type(type) {}468};469 470// Symbols of the same type must be laid out contiguously: we choose to emit471// all local symbols first, then external symbols, and finally undefined472// symbols. For each symbol type, the LC_DYSYMTAB load command will record the473// range (start index and total number) of those symbols in the symbol table.474class SymtabSection : public LinkEditSection {475public:476  void finalizeContents() override;477  uint32_t getNumSymbols() const;478  uint32_t getNumLocalSymbols() const {479    return stabs.size() + localSymbols.size();480  }481  uint32_t getNumExternalSymbols() const { return externalSymbols.size(); }482  uint32_t getNumUndefinedSymbols() const { return undefinedSymbols.size(); }483 484private:485  void emitBeginSourceStab(StringRef);486  void emitEndSourceStab();487  void emitObjectFileStab(ObjFile *);488  void emitEndFunStab(Defined *);489  Defined *getFuncBodySym(Defined *);490  void emitStabs();491 492protected:493  SymtabSection(StringTableSection &);494 495  StringTableSection &stringTableSection;496  // STABS symbols are always local symbols, but we represent them with special497  // entries because they may use fields like n_sect and n_desc differently.498  std::vector<StabsEntry> stabs;499  std::vector<SymtabEntry> localSymbols;500  std::vector<SymtabEntry> externalSymbols;501  std::vector<SymtabEntry> undefinedSymbols;502};503 504template <class LP> SymtabSection *makeSymtabSection(StringTableSection &);505 506// The indirect symbol table is a list of 32-bit integers that serve as indices507// into the (actual) symbol table. The indirect symbol table is a508// concatenation of several sub-arrays of indices, each sub-array belonging to509// a separate section. The starting offset of each sub-array is stored in the510// reserved1 header field of the respective section.511//512// These sub-arrays provide symbol information for sections that store513// contiguous sequences of symbol references. These references can be pointers514// (e.g. those in the GOT and TLVP sections) or assembly sequences (e.g.515// function stubs).516class IndirectSymtabSection final : public LinkEditSection {517public:518  IndirectSymtabSection();519  void finalizeContents() override;520  uint32_t getNumSymbols() const;521  uint64_t getRawSize() const override {522    return getNumSymbols() * sizeof(uint32_t);523  }524  bool isNeeded() const override;525  void writeTo(uint8_t *buf) const override;526};527 528// The code signature comes at the very end of the linked output file.529class CodeSignatureSection final : public LinkEditSection {530public:531  // NOTE: These values are duplicated in llvm-objcopy's MachO/Object.h file532  // and any changes here, should be repeated there.533  static constexpr uint8_t blockSizeShift = 12;534  static constexpr size_t blockSize = (1 << blockSizeShift); // 4 KiB535  static constexpr size_t hashSize = 256 / 8;536  static constexpr size_t blobHeadersSize = llvm::alignTo<8>(537      sizeof(llvm::MachO::CS_SuperBlob) + sizeof(llvm::MachO::CS_BlobIndex));538  static constexpr uint32_t fixedHeadersSize =539      blobHeadersSize + sizeof(llvm::MachO::CS_CodeDirectory);540 541  uint32_t fileNamePad = 0;542  uint32_t allHeadersSize = 0;543  StringRef fileName;544 545  CodeSignatureSection();546  uint64_t getRawSize() const override;547  bool isNeeded() const override { return true; }548  void writeTo(uint8_t *buf) const override;549  uint32_t getBlockCount() const;550  void writeHashes(uint8_t *buf) const;551};552 553class CStringSection : public SyntheticSection {554public:555  CStringSection(const char *name);556  void addInput(CStringInputSection *);557  uint64_t getSize() const override { return size; }558  virtual void finalizeContents();559  bool isNeeded() const override { return !inputs.empty(); }560  void writeTo(uint8_t *buf) const override;561 562  std::vector<CStringInputSection *> inputs;563 564private:565  uint64_t size;566};567 568class DeduplicatedCStringSection final : public CStringSection {569public:570  DeduplicatedCStringSection(const char *name) : CStringSection(name){};571  uint64_t getSize() const override { return size; }572  void finalizeContents() override;573  void writeTo(uint8_t *buf) const override;574  uint64_t getStringOffset(StringRef str) const;575 576private:577  llvm::DenseMap<llvm::CachedHashStringRef, uint64_t> stringOffsetMap;578  size_t size = 0;579};580 581/*582 * This section contains deduplicated literal values. The 16-byte values are583 * laid out first, followed by the 8- and then the 4-byte ones.584 */585class WordLiteralSection final : public SyntheticSection {586public:587  using UInt128 = std::pair<uint64_t, uint64_t>;588  // I don't think the standard guarantees the size of a pair, so let's make589  // sure it's exact -- that way we can construct it via `mmap`.590  static_assert(sizeof(UInt128) == 16);591 592  WordLiteralSection();593  void addInput(WordLiteralInputSection *);594  void finalizeContents();595  void writeTo(uint8_t *buf) const override;596 597  uint64_t getSize() const override {598    return literal16Map.size() * 16 + literal8Map.size() * 8 +599           literal4Map.size() * 4;600  }601 602  bool isNeeded() const override {603    return !literal16Map.empty() || !literal4Map.empty() ||604           !literal8Map.empty();605  }606 607  uint64_t getLiteral16Offset(uintptr_t buf) const {608    return literal16Map.at(*reinterpret_cast<const UInt128 *>(buf)) * 16;609  }610 611  uint64_t getLiteral8Offset(uintptr_t buf) const {612    return literal16Map.size() * 16 +613           literal8Map.at(*reinterpret_cast<const uint64_t *>(buf)) * 8;614  }615 616  uint64_t getLiteral4Offset(uintptr_t buf) const {617    return literal16Map.size() * 16 + literal8Map.size() * 8 +618           literal4Map.at(*reinterpret_cast<const uint32_t *>(buf)) * 4;619  }620 621private:622  std::vector<WordLiteralInputSection *> inputs;623 624  template <class T> struct Hasher {625    llvm::hash_code operator()(T v) const { return llvm::hash_value(v); }626  };627  // We're using unordered_map instead of DenseMap here because we need to628  // support all possible integer values -- there are no suitable tombstone629  // values for DenseMap.630  std::unordered_map<UInt128, uint64_t, Hasher<UInt128>> literal16Map;631  std::unordered_map<uint64_t, uint64_t> literal8Map;632  std::unordered_map<uint32_t, uint64_t> literal4Map;633};634 635class ObjCImageInfoSection final : public SyntheticSection {636public:637  ObjCImageInfoSection();638  bool isNeeded() const override { return !files.empty(); }639  uint64_t getSize() const override { return 8; }640  void addFile(const InputFile *file) {641    assert(!file->objCImageInfo.empty());642    files.push_back(file);643  }644  void finalizeContents();645  void writeTo(uint8_t *buf) const override;646 647private:648  struct ImageInfo {649    uint8_t swiftVersion = 0;650    bool hasCategoryClassProperties = false;651  } info;652  static ImageInfo parseImageInfo(const InputFile *);653  std::vector<const InputFile *> files; // files with image info654};655 656// This section stores 32-bit __TEXT segment offsets of initializer functions.657//658// The compiler stores pointers to initializers in __mod_init_func. These need659// to be fixed up at load time, which takes time and dirties memory. By660// synthesizing InitOffsetsSection from them, this data can live in the661// read-only __TEXT segment instead. This section is used by default when662// chained fixups are enabled.663//664// There is no similar counterpart to __mod_term_func, as that section is665// deprecated, and static destructors are instead handled by registering them666// via __cxa_atexit from an autogenerated initializer function (see D121736).667class InitOffsetsSection final : public SyntheticSection {668public:669  InitOffsetsSection();670  bool isNeeded() const override { return !sections.empty(); }671  uint64_t getSize() const override;672  void writeTo(uint8_t *buf) const override;673  void setUp();674 675  void addInput(ConcatInputSection *isec) { sections.push_back(isec); }676  const std::vector<ConcatInputSection *> &inputs() const { return sections; }677 678private:679  std::vector<ConcatInputSection *> sections;680};681 682// This SyntheticSection is for the __objc_methlist section, which contains683// relative method lists if the -objc_relative_method_lists option is enabled.684class ObjCMethListSection final : public SyntheticSection {685public:686  ObjCMethListSection();687 688  static bool isMethodList(const ConcatInputSection *isec);689  void addInput(ConcatInputSection *isec) { inputs.push_back(isec); }690  std::vector<ConcatInputSection *> getInputs() { return inputs; }691 692  void setUp();693  void finalize() override;694  bool isNeeded() const override { return !inputs.empty(); }695  uint64_t getSize() const override { return sectionSize; }696  void writeTo(uint8_t *bufStart) const override;697 698private:699  void readMethodListHeader(const uint8_t *buf, uint32_t &structSizeAndFlags,700                            uint32_t &structCount) const;701  void writeMethodListHeader(uint8_t *buf, uint32_t structSizeAndFlags,702                             uint32_t structCount) const;703  uint32_t computeRelativeMethodListSize(uint32_t absoluteMethodListSize) const;704  void writeRelativeOffsetForIsec(const ConcatInputSection *isec, uint8_t *buf,705                                  uint32_t &inSecOff, uint32_t &outSecOff,706                                  bool useSelRef) const;707  uint32_t writeRelativeMethodList(const ConcatInputSection *isec,708                                   uint8_t *buf) const;709 710  static constexpr uint32_t methodListHeaderSize =711      /*structSizeAndFlags*/ sizeof(uint32_t) +712      /*structCount*/ sizeof(uint32_t);713  // Relative method lists are supported only for 3-pointer method lists714  static constexpr uint32_t pointersPerStruct = 3;715  // The runtime identifies relative method lists via this magic value716  static constexpr uint32_t relMethodHeaderFlag = 0x80000000;717  // In the method list header, the first 2 bytes are the size of struct718  static constexpr uint32_t structSizeMask = 0x0000FFFF;719  // In the method list header, the last 2 bytes are the flags for the struct720  static constexpr uint32_t structFlagsMask = 0xFFFF0000;721  // Relative method lists have 4 byte alignment as all data in the InputSection722  // is 4 byte723  static constexpr uint32_t relativeOffsetSize = sizeof(uint32_t);724 725  // The output size of the __objc_methlist section, computed during finalize()726  uint32_t sectionSize = 0;727  std::vector<ConcatInputSection *> inputs;728};729 730// Chained fixups are a replacement for classic dyld opcodes. In this format,731// most of the metadata necessary for binding symbols and rebasing addresses is732// stored directly in the memory location that will have the fixup applied.733//734// The fixups form singly linked lists; each one covering a single page in735// memory. The __LINKEDIT,__chainfixups section stores the page offset of the736// first fixup of each page; the rest can be found by walking the chain using737// the offset that is embedded in each entry.738//739// This setup allows pages to be relocated lazily at page-in time and without740// being dirtied. The kernel can discard and load them again as needed. This741// technique, called page-in linking, was introduced in macOS 13.742//743// The benefits of this format are:744//  - smaller __LINKEDIT segment, as most of the fixup information is stored in745//    the data segment746//  - faster startup, since not all relocations need to be done upfront747//  - slightly lower memory usage, as fewer pages are dirtied748//749// Userspace x86_64 and arm64 binaries have two types of fixup entries:750//   - Rebase entries contain an absolute address, to which the object's load751//     address will be added to get the final value. This is used for loading752//     the address of a symbol defined in the same binary.753//   - Binding entries are mostly used for symbols imported from other dylibs,754//     but for weakly bound and interposable symbols as well. They are looked up755//     by a (symbol name, library) pair stored in __chainfixups. This import756//     entry also encodes whether the import is weak (i.e. if the symbol is757//     missing, it should be set to null instead of producing a load error).758//     The fixup encodes an ordinal associated with the import, and an optional759//     addend.760//761// The entries are tightly packed 64-bit bitfields. One of the bits specifies762// which kind of fixup to interpret them as.763//764// LLD generates the fixup data in 5 stages:765//   1. While scanning relocations, we make a note of each location that needs766//      a fixup by calling addRebase() or addBinding(). During this, we assign767//      a unique ordinal for each (symbol name, library, addend) import tuple.768//   2. After addresses have been assigned to all sections, and thus the memory769//      layout of the linked image is final; finalizeContents() is called. Here,770//      the page offsets of the chain start entries are calculated.771//   3. ChainedFixupsSection::writeTo() writes the page start offsets and the772//      imports table to the output file.773//   4. Each section's fixup entries are encoded and written to disk in774//      ConcatInputSection::writeTo(), but without writing the offsets that form775//      the chain.776//   5. Finally, each page's (which might correspond to multiple sections)777//      fixups are linked together in Writer::buildFixupChains().778class ChainedFixupsSection final : public LinkEditSection {779public:780  ChainedFixupsSection();781  void finalizeContents() override;782  uint64_t getRawSize() const override { return size; }783  bool isNeeded() const override;784  void writeTo(uint8_t *buf) const override;785 786  void addRebase(const InputSection *isec, uint64_t offset) {787    locations.emplace_back(isec, offset);788  }789  void addBinding(const Symbol *dysym, const InputSection *isec,790                  uint64_t offset, int64_t addend = 0);791 792  void setHasNonWeakDefinition() { hasNonWeakDef = true; }793 794  // Returns an (ordinal, inline addend) tuple used by dyld_chained_ptr_64_bind.795  std::pair<uint32_t, uint8_t> getBinding(const Symbol *sym,796                                          int64_t addend) const;797 798  const std::vector<Location> &getLocations() const { return locations; }799 800  bool hasWeakBinding() const { return hasWeakBind; }801  bool hasNonWeakDefinition() const { return hasNonWeakDef; }802 803private:804  // Location::offset initially stores the offset within an InputSection, but805  // contains output segment offsets after finalizeContents().806  std::vector<Location> locations;807  // (target symbol, addend) => import ordinal808  llvm::MapVector<std::pair<const Symbol *, int64_t>, uint32_t> bindings;809 810  struct SegmentInfo {811    SegmentInfo(const OutputSegment *oseg) : oseg(oseg) {}812 813    const OutputSegment *oseg;814    // (page index, fixup starts offset)815    llvm::SmallVector<std::pair<uint16_t, uint16_t>> pageStarts;816 817    size_t getSize() const;818    size_t writeTo(uint8_t *buf) const;819  };820  llvm::SmallVector<SegmentInfo, 4> fixupSegments;821 822  size_t symtabSize = 0;823  size_t size = 0;824 825  bool needsAddend = false;826  bool needsLargeAddend = false;827  bool hasWeakBind = false;828  bool hasNonWeakDef = false;829  llvm::MachO::ChainedImportFormat importFormat;830};831 832void writeChainedRebase(uint8_t *buf, uint64_t targetVA);833void writeChainedFixup(uint8_t *buf, const Symbol *sym, int64_t addend);834 835struct InStruct {836  const uint8_t *bufferStart = nullptr;837  MachHeaderSection *header = nullptr;838  /// The list of cstring sections. Note that this includes \p cStringSection839  /// and \p objcMethnameSection already.840  llvm::SmallVector<CStringSection *> cStringSections;841  CStringSection *cStringSection = nullptr;842  DeduplicatedCStringSection *objcMethnameSection = nullptr;843  WordLiteralSection *wordLiteralSection = nullptr;844  RebaseSection *rebase = nullptr;845  BindingSection *binding = nullptr;846  WeakBindingSection *weakBinding = nullptr;847  LazyBindingSection *lazyBinding = nullptr;848  ExportSection *exports = nullptr;849  GotSection *got = nullptr;850  TlvPointerSection *tlvPointers = nullptr;851  LazyPointerSection *lazyPointers = nullptr;852  StubsSection *stubs = nullptr;853  StubHelperSection *stubHelper = nullptr;854  ObjCStubsSection *objcStubs = nullptr;855  UnwindInfoSection *unwindInfo = nullptr;856  ObjCImageInfoSection *objCImageInfo = nullptr;857  ConcatInputSection *imageLoaderCache = nullptr;858  InitOffsetsSection *initOffsets = nullptr;859  ObjCMethListSection *objcMethList = nullptr;860  ChainedFixupsSection *chainedFixups = nullptr;861 862  CStringSection *getOrCreateCStringSection(StringRef name,863                                            bool forceDedupStrings = false) {864    auto [it, didEmplace] =865        cStringSectionMap.try_emplace(name, cStringSections.size());866    if (!didEmplace)867      return cStringSections[it->getValue()];868 869    std::string &nameData = *make<std::string>(name);870    CStringSection *sec;871    if (config->dedupStrings || forceDedupStrings)872      sec = make<DeduplicatedCStringSection>(nameData.c_str());873    else874      sec = make<CStringSection>(nameData.c_str());875    cStringSections.push_back(sec);876    return sec;877  }878 879private:880  llvm::StringMap<unsigned> cStringSectionMap;881};882 883extern InStruct in;884extern std::vector<SyntheticSection *> syntheticSections;885 886void createSyntheticSymbols();887 888} // namespace lld::macho889 890#endif891