reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
//===- lib/ReaderWriter/MachO/ExecutableAtoms.h ---------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLD_READER_WRITER_MACHO_EXECUTABLE_ATOMS_H
#define LLD_READER_WRITER_MACHO_EXECUTABLE_ATOMS_H

#include "Atoms.h"
#include "File.h"

#include "llvm/BinaryFormat/MachO.h"

#include "lld/Core/DefinedAtom.h"
#include "lld/Core/File.h"
#include "lld/Core/LinkingContext.h"
#include "lld/Core/Reference.h"
#include "lld/Core/Simple.h"
#include "lld/Core/UndefinedAtom.h"
#include "lld/ReaderWriter/MachOLinkingContext.h"

namespace lld {
namespace mach_o {


//
// CEntryFile adds an UndefinedAtom for "_main" so that the Resolving
// phase will fail if "_main" is undefined.
//
class CEntryFile : public SimpleFile {
public:
  CEntryFile(const MachOLinkingContext &context)
      : SimpleFile("C entry", kindCEntryObject),
       _undefMain(*this, context.entrySymbolName()) {
    this->addAtom(_undefMain);
  }

private:
  SimpleUndefinedAtom   _undefMain;
};


//
// StubHelperFile adds an UndefinedAtom for "dyld_stub_binder" so that
// the Resolveing phase will fail if "dyld_stub_binder" is undefined.
//
class StubHelperFile : public SimpleFile {
public:
  StubHelperFile(const MachOLinkingContext &context)
      : SimpleFile("stub runtime", kindStubHelperObject),
        _undefBinder(*this, context.binderSymbolName()) {
    this->addAtom(_undefBinder);
  }

private:
  SimpleUndefinedAtom   _undefBinder;
};


//
// MachHeaderAliasFile lazily instantiates the magic symbols that mark the start
// of the mach_header for final linked images.
//
class MachHeaderAliasFile : public SimpleFile {
public:
  MachHeaderAliasFile(const MachOLinkingContext &context)
    : SimpleFile("mach_header symbols", kindHeaderObject) {
    StringRef machHeaderSymbolName;
    DefinedAtom::Scope symbolScope = DefinedAtom::scopeLinkageUnit;
    StringRef dsoHandleName;
    switch (context.outputMachOType()) {
    case llvm::MachO::MH_OBJECT:
      machHeaderSymbolName = "__mh_object_header";
      break;
    case llvm::MachO::MH_EXECUTE:
      machHeaderSymbolName = "__mh_execute_header";
      symbolScope = DefinedAtom::scopeGlobal;
      dsoHandleName = "___dso_handle";
      break;
    case llvm::MachO::MH_FVMLIB:
      llvm_unreachable("no mach_header symbol for file type");
    case llvm::MachO::MH_CORE:
      llvm_unreachable("no mach_header symbol for file type");
    case llvm::MachO::MH_PRELOAD:
      llvm_unreachable("no mach_header symbol for file type");
    case llvm::MachO::MH_DYLIB:
      machHeaderSymbolName = "__mh_dylib_header";
      dsoHandleName = "___dso_handle";
      break;
    case llvm::MachO::MH_DYLINKER:
      machHeaderSymbolName = "__mh_dylinker_header";
      dsoHandleName = "___dso_handle";
      break;
    case llvm::MachO::MH_BUNDLE:
      machHeaderSymbolName = "__mh_bundle_header";
      dsoHandleName = "___dso_handle";
      break;
    case llvm::MachO::MH_DYLIB_STUB:
      llvm_unreachable("no mach_header symbol for file type");
    case llvm::MachO::MH_DSYM:
      llvm_unreachable("no mach_header symbol for file type");
    case llvm::MachO::MH_KEXT_BUNDLE:
      dsoHandleName = "___dso_handle";
      break;
    }
    if (!machHeaderSymbolName.empty())
      _definedAtoms.push_back(new (allocator()) MachODefinedAtom(
          *this, machHeaderSymbolName, symbolScope,
          DefinedAtom::typeMachHeader, DefinedAtom::mergeNo, false,
          true /* noDeadStrip */,
          ArrayRef<uint8_t>(), DefinedAtom::Alignment(4096)));

    if (!dsoHandleName.empty())
      _definedAtoms.push_back(new (allocator()) MachODefinedAtom(
          *this, dsoHandleName, DefinedAtom::scopeLinkageUnit,
          DefinedAtom::typeDSOHandle, DefinedAtom::mergeNo, false,
          true /* noDeadStrip */,
          ArrayRef<uint8_t>(), DefinedAtom::Alignment(1)));
  }

  const AtomRange<DefinedAtom> defined() const override {
    return _definedAtoms;
  }
  const AtomRange<UndefinedAtom> undefined() const override {
    return _noUndefinedAtoms;
  }

  const AtomRange<SharedLibraryAtom> sharedLibrary() const override {
    return _noSharedLibraryAtoms;
  }

  const AtomRange<AbsoluteAtom> absolute() const override {
    return _noAbsoluteAtoms;
  }

  void clearAtoms() override {
    _definedAtoms.clear();
    _noUndefinedAtoms.clear();
    _noSharedLibraryAtoms.clear();
    _noAbsoluteAtoms.clear();
  }


private:
  mutable AtomVector<DefinedAtom> _definedAtoms;
};

} // namespace mach_o
} // namespace lld

#endif // LLD_READER_WRITER_MACHO_EXECUTABLE_ATOMS_H