summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/MachO/MachOTargetInfo.cpp
blob: f116716e7961b9fd637f1b7a46a29498726eedf3 (plain)
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
//===- lib/ReaderWriter/MachO/MachOTargetInfo.cpp -------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lld/ReaderWriter/MachOTargetInfo.h"
#include "GOTPass.hpp"
#include "StubsPass.hpp"

#include "lld/Core/LinkerOptions.h"
#include "lld/Core/PassManager.h"

#include "llvm/ADT/Triple.h"
#include "llvm/Support/MachO.h"

namespace lld {
uint32_t MachOTargetInfo::getCPUType() const {
  switch (getTriple().getArch()) {
  case llvm::Triple::x86:
    return llvm::MachO::CPUTypeI386;
  case llvm::Triple::x86_64:
    return llvm::MachO::CPUTypeX86_64;
  case llvm::Triple::arm:
    return llvm::MachO::CPUTypeARM;
  default:
    llvm_unreachable("Unknown arch type");
  }
}

uint32_t MachOTargetInfo::getCPUSubType() const {
  switch (getTriple().getArch()) {
  case llvm::Triple::x86:
    return llvm::MachO::CPUSubType_I386_ALL;
  case llvm::Triple::x86_64:
    return llvm::MachO::CPUSubType_X86_64_ALL;
  case llvm::Triple::arm:
    return llvm::MachO::CPUSubType_ARM_ALL;
  default:
    llvm_unreachable("Unknown arch type");
  }
}

bool MachOTargetInfo::addEntryPointLoadCommand() const {
  switch (_options._outputKind) {
  case OutputKind::Executable:
    return true;
  default:
    return false;
  }
}

bool MachOTargetInfo::addUnixThreadLoadCommand() const {
  switch (_options._outputKind) {
  case OutputKind::Executable:
    return true;
  default:
    return false;
  }
}

class GenericMachOTargetInfo LLVM_FINAL : public MachOTargetInfo {
public:
  GenericMachOTargetInfo(const LinkerOptions &lo) : MachOTargetInfo(lo) {}

  virtual uint64_t getPageSize() const { return 0x1000; }
  virtual uint64_t getPageZeroSize() const { return getPageSize(); }

  virtual StringRef getEntry() const {
    if (_options._outputKind != OutputKind::Executable)
      return "";
    if (!_options._entrySymbol.empty())
      return _options._entrySymbol;
    return "_main";
  }

  virtual void addPasses(PassManager &pm) const {
    pm.add(std::unique_ptr<Pass>(new mach_o::GOTPass));
    pm.add(std::unique_ptr<Pass>(new mach_o::StubsPass(*this)));
  }
};

std::unique_ptr<MachOTargetInfo>
MachOTargetInfo::create(const LinkerOptions &lo) {
  return std::unique_ptr<MachOTargetInfo>(new GenericMachOTargetInfo(lo));
}
} // end namespace lld
OpenPOWER on IntegriCloud