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