xref: /freebsd/contrib/llvm-project/lld/MachO/Target.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 
22 namespace lld {
23 namespace macho {
24 LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
25 
26 class Symbol;
27 class Defined;
28 class DylibSymbol;
29 class InputSection;
30 
31 class TargetInfo {
32 public:
33   template <class LP> TargetInfo(LP) {
34     // Having these values available in TargetInfo allows us to access them
35     // without having to resort to templates.
36     magic = LP::magic;
37     pageZeroSize = LP::pageZeroSize;
38     headerSize = sizeof(typename LP::mach_header);
39     wordSize = LP::wordSize;
40   }
41 
42   virtual ~TargetInfo() = default;
43 
44   // Validate the relocation structure and get its addend.
45   virtual int64_t
46   getEmbeddedAddend(llvm::MemoryBufferRef, uint64_t offset,
47                     const llvm::MachO::relocation_info) const = 0;
48   virtual void relocateOne(uint8_t *loc, const Reloc &, uint64_t va,
49                            uint64_t relocVA) const = 0;
50 
51   // Write code for lazy binding. See the comments on StubsSection for more
52   // details.
53   virtual void writeStub(uint8_t *buf, const Symbol &) const = 0;
54   virtual void writeStubHelperHeader(uint8_t *buf) const = 0;
55   virtual void writeStubHelperEntry(uint8_t *buf, const DylibSymbol &,
56                                     uint64_t entryAddr) const = 0;
57 
58   // Symbols may be referenced via either the GOT or the stubs section,
59   // depending on the relocation type. prepareSymbolRelocation() will set up the
60   // GOT/stubs entries, and resolveSymbolVA() will return the addresses of those
61   // entries. resolveSymbolVA() may also relax the target instructions to save
62   // on a level of address indirection.
63   virtual void relaxGotLoad(uint8_t *loc, uint8_t type) const = 0;
64 
65   virtual const RelocAttrs &getRelocAttrs(uint8_t type) const = 0;
66 
67   virtual uint64_t getPageSize() const = 0;
68 
69   virtual void populateThunk(InputSection *thunk, Symbol *funcSym) {
70     llvm_unreachable("target does not use thunks");
71   }
72 
73   bool hasAttr(uint8_t type, RelocAttrBits bit) const {
74     return getRelocAttrs(type).hasAttr(bit);
75   }
76 
77   bool usesThunks() const { return thunkSize > 0; }
78 
79   uint32_t magic;
80   llvm::MachO::CPUType cpuType;
81   uint32_t cpuSubtype;
82 
83   uint64_t pageZeroSize;
84   size_t headerSize;
85   size_t stubSize;
86   size_t stubHelperHeaderSize;
87   size_t stubHelperEntrySize;
88   size_t wordSize;
89 
90   size_t thunkSize = 0;
91   uint64_t forwardBranchRange = 0;
92   uint64_t backwardBranchRange = 0;
93 
94   // We contrive this value as sufficiently far from any valid address that it
95   // will always be out-of-range for any architecture. UINT64_MAX is not a
96   // good choice because it is (a) only 1 away from wrapping to 0, and (b) the
97   // tombstone value for DenseMap<> and caused weird assertions for me.
98   static constexpr uint64_t outOfRangeVA = 0xfull << 60;
99 };
100 
101 TargetInfo *createX86_64TargetInfo();
102 TargetInfo *createARM64TargetInfo();
103 TargetInfo *createARM64_32TargetInfo();
104 TargetInfo *createARMTargetInfo(uint32_t cpuSubtype);
105 
106 struct LP64 {
107   using mach_header = llvm::MachO::mach_header_64;
108   using nlist = structs::nlist_64;
109   using segment_command = llvm::MachO::segment_command_64;
110   using section = llvm::MachO::section_64;
111   using encryption_info_command = llvm::MachO::encryption_info_command_64;
112 
113   static constexpr uint32_t magic = llvm::MachO::MH_MAGIC_64;
114   static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT_64;
115   static constexpr uint32_t encryptionInfoLCType =
116       llvm::MachO::LC_ENCRYPTION_INFO_64;
117 
118   static constexpr uint64_t pageZeroSize = 1ull << 32;
119   static constexpr size_t wordSize = 8;
120 };
121 
122 struct ILP32 {
123   using mach_header = llvm::MachO::mach_header;
124   using nlist = structs::nlist;
125   using segment_command = llvm::MachO::segment_command;
126   using section = llvm::MachO::section;
127   using encryption_info_command = llvm::MachO::encryption_info_command;
128 
129   static constexpr uint32_t magic = llvm::MachO::MH_MAGIC;
130   static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT;
131   static constexpr uint32_t encryptionInfoLCType =
132       llvm::MachO::LC_ENCRYPTION_INFO;
133 
134   static constexpr uint64_t pageZeroSize = 1ull << 12;
135   static constexpr size_t wordSize = 4;
136 };
137 
138 extern TargetInfo *target;
139 
140 } // namespace macho
141 } // namespace lld
142 
143 #endif
144