1//===- Target.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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_MACHO_TARGET_H
10#define LLD_MACHO_TARGET_H
11
12#include "MachOStructs.h"
13#include "Relocations.h"
14
15#include "llvm/ADT/BitmaskEnum.h"
16#include "llvm/BinaryFormat/MachO.h"
17#include "llvm/Support/MemoryBuffer.h"
18
19#include <cstddef>
20#include <cstdint>
21
22namespace lld {
23namespace macho {
24LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
25
26class Symbol;
27class DylibSymbol;
28class InputSection;
29
30class TargetInfo {
31public:
32 template <class LP> TargetInfo(LP) {
33 // Having these values available in TargetInfo allows us to access them
34 // without having to resort to templates.
35 magic = LP::magic;
36 pageZeroSize = LP::pageZeroSize;
37 headerSize = sizeof(typename LP::mach_header);
38 wordSize = LP::wordSize;
39 }
40
41 virtual ~TargetInfo() = default;
42
43 // Validate the relocation structure and get its addend.
44 virtual int64_t
45 getEmbeddedAddend(llvm::MemoryBufferRef, uint64_t offset,
46 const llvm::MachO::relocation_info) const = 0;
47 virtual void relocateOne(uint8_t *loc, const Reloc &, uint64_t va,
48 uint64_t relocVA) const = 0;
49
50 // Write code for lazy binding. See the comments on StubsSection for more
51 // details.
52 virtual void writeStub(uint8_t *buf, const Symbol &) const = 0;
53 virtual void writeStubHelperHeader(uint8_t *buf) const = 0;
54 virtual void writeStubHelperEntry(uint8_t *buf, const DylibSymbol &,
55 uint64_t entryAddr) const = 0;
56
57 // Symbols may be referenced via either the GOT or the stubs section,
58 // depending on the relocation type. prepareSymbolRelocation() will set up the
59 // GOT/stubs entries, and resolveSymbolVA() will return the addresses of those
60 // entries. resolveSymbolVA() may also relax the target instructions to save
61 // on a level of address indirection.
62 virtual void relaxGotLoad(uint8_t *loc, uint8_t type) const = 0;
63
64 virtual const RelocAttrs &getRelocAttrs(uint8_t type) const = 0;
65
66 virtual uint64_t getPageSize() const = 0;
67
68 bool hasAttr(uint8_t type, RelocAttrBits bit) const {
69 return getRelocAttrs(type).hasAttr(bit);
70 }
71
72 uint32_t magic;
73 uint32_t cpuType;
74 uint32_t cpuSubtype;
75
76 uint64_t pageZeroSize;
77 size_t headerSize;
78 size_t stubSize;
79 size_t stubHelperHeaderSize;
80 size_t stubHelperEntrySize;
81 size_t wordSize;
82};
83
84TargetInfo *createX86_64TargetInfo();
85TargetInfo *createARM64TargetInfo();
86TargetInfo *createARM64_32TargetInfo();
87TargetInfo *createARMTargetInfo(uint32_t cpuSubtype);
88
89struct LP64 {
90 using mach_header = llvm::MachO::mach_header_64;
91 using nlist = structs::nlist_64;
92 using segment_command = llvm::MachO::segment_command_64;
93 using section = llvm::MachO::section_64;
94 using encryption_info_command = llvm::MachO::encryption_info_command_64;
95
96 static constexpr uint32_t magic = llvm::MachO::MH_MAGIC_64;
97 static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT_64;
98 static constexpr uint32_t encryptionInfoLCType =
99 llvm::MachO::LC_ENCRYPTION_INFO_64;
100
101 static constexpr uint64_t pageZeroSize = 1ull << 32;
102 static constexpr size_t wordSize = 8;
103};
104
105struct ILP32 {
106 using mach_header = llvm::MachO::mach_header;
107 using nlist = structs::nlist;
108 using segment_command = llvm::MachO::segment_command;
109 using section = llvm::MachO::section;
110 using encryption_info_command = llvm::MachO::encryption_info_command;
111
112 static constexpr uint32_t magic = llvm::MachO::MH_MAGIC;
113 static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT;
114 static constexpr uint32_t encryptionInfoLCType =
115 llvm::MachO::LC_ENCRYPTION_INFO;
116
117 static constexpr uint64_t pageZeroSize = 1ull << 12;
118 static constexpr size_t wordSize = 4;
119};
120
121extern TargetInfo *target;
122
123} // namespace macho
124} // namespace lld
125
126#endif
127