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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
//===- lib/FileFormat/MachO/ArchHandler.h ---------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "MachONormalizedFile.h"
#include "lld/Core/LLVM.h"
#include "lld/Core/Reference.h"
#include "lld/Core/Simple.h"
#include "lld/ReaderWriter/MachOLinkingContext.h"
#include "llvm/ADT/Triple.h"
#ifndef LLD_READER_WRITER_MACHO_ARCH_HANDLER_H
#define LLD_READER_WRITER_MACHO_ARCH_HANDLER_H
namespace lld {
namespace mach_o {
///
/// The ArchHandler class handles all architecture specific aspects of
/// mach-o linking.
///
class ArchHandler {
public:
virtual ~ArchHandler();
/// There is no public interface to subclasses of ArchHandler, so this
/// is the only way to instantiate an ArchHandler.
static std::unique_ptr<ArchHandler> create(MachOLinkingContext::Arch arch);
/// Get (arch specific) kind strings used by Registry.
virtual const Registry::KindStrings *kindStrings() = 0;
/// Convert mach-o Arch to Reference::KindArch.
virtual Reference::KindArch kindArch() = 0;
/// Used by StubPass to update References to shared library functions
/// to be references to a stub.
virtual bool isCallSite(const Reference &) = 0;
/// Used by GOTPass to locate GOT References
virtual bool isGOTAccess(const Reference &, bool &canBypassGOT) {
return false;
}
/// Used by GOTPass to update GOT References
virtual void updateReferenceToGOT(const Reference *, bool targetIsNowGOT) {}
/// Used by normalizedFromAtoms() to know where to generated rebasing and
/// binding info in final executables.
virtual bool isPointer(const Reference &) = 0;
/// Used by normalizedFromAtoms() to know where to generated lazy binding
/// info in final executables.
virtual bool isLazyPointer(const Reference &);
/// Returns true if the specified relocation is paired to the next relocation.
virtual bool isPairedReloc(const normalized::Relocation &) = 0;
/// Prototype for a helper function. Given a sectionIndex and address,
/// finds the atom and offset with that atom of that address.
typedef std::function<std::error_code (uint32_t sectionIndex, uint64_t addr,
const lld::Atom **, Reference::Addend *)>
FindAtomBySectionAndAddress;
/// Prototype for a helper function. Given a symbolIndex, finds the atom
/// representing that symbol.
typedef std::function<std::error_code (uint32_t symbolIndex,
const lld::Atom **)> FindAtomBySymbolIndex;
/// Analyzes a relocation from a .o file and returns the info
/// (kind, target, addend) needed to instantiate a Reference.
/// Two helper functions are passed as parameters to find the target atom
/// given a symbol index or address.
virtual std::error_code
getReferenceInfo(const normalized::Relocation &reloc,
const DefinedAtom *inAtom,
uint32_t offsetInAtom,
uint64_t fixupAddress, bool swap,
FindAtomBySectionAndAddress atomFromAddress,
FindAtomBySymbolIndex atomFromSymbolIndex,
Reference::KindValue *kind,
const lld::Atom **target,
Reference::Addend *addend) = 0;
/// Analyzes a pair of relocations from a .o file and returns the info
/// (kind, target, addend) needed to instantiate a Reference.
/// Two helper functions are passed as parameters to find the target atom
/// given a symbol index or address.
virtual std::error_code
getPairReferenceInfo(const normalized::Relocation &reloc1,
const normalized::Relocation &reloc2,
const DefinedAtom *inAtom,
uint32_t offsetInAtom,
uint64_t fixupAddress, bool swap,
FindAtomBySectionAndAddress atomFromAddress,
FindAtomBySymbolIndex atomFromSymbolIndex,
Reference::KindValue *kind,
const lld::Atom **target,
Reference::Addend *addend) = 0;
/// Prototype for a helper function. Given an atom, finds the symbol table
/// index for it in the output file.
typedef std::function<uint32_t (const Atom &atom)> FindSymbolIndexForAtom;
/// Prototype for a helper function. Given an atom, finds the index
/// of the section that will contain the atom.
typedef std::function<uint32_t (const Atom &atom)> FindSectionIndexForAtom;
/// Prototype for a helper function. Given an atom, finds the address
/// assigned to it in the output file.
typedef std::function<uint64_t (const Atom &atom)> FindAddressForAtom;
/// Some architectures require local symbols on anonymous atoms.
virtual bool needsLocalSymbolInRelocatableFile(const DefinedAtom *atom) {
return false;
}
/// Copy raw content then apply all fixup References on an Atom.
virtual void generateAtomContent(const DefinedAtom &atom, bool relocatable,
FindAddressForAtom findAddress,
uint8_t *atomContentBuffer) = 0;
/// Used in -r mode to convert a Reference to a mach-o relocation.
virtual void appendSectionRelocations(const DefinedAtom &atom,
uint64_t atomSectionOffset,
const Reference &ref,
FindSymbolIndexForAtom,
FindSectionIndexForAtom,
FindAddressForAtom,
normalized::Relocations&) = 0;
struct ReferenceInfo {
Reference::KindArch arch;
uint16_t kind;
uint32_t offset;
int32_t addend;
};
/// Table of architecture specific information for creating stubs.
struct StubInfo {
const char* binderSymbolName;
ReferenceInfo lazyPointerReferenceToHelper;
ReferenceInfo lazyPointerReferenceToFinal;
ReferenceInfo nonLazyPointerReferenceToBinder;
uint8_t codeAlignment;
uint32_t stubSize;
uint8_t stubBytes[16];
ReferenceInfo stubReferenceToLP;
uint32_t stubHelperSize;
uint8_t stubHelperBytes[16];
ReferenceInfo stubHelperReferenceToImm;
ReferenceInfo stubHelperReferenceToHelperCommon;
uint32_t stubHelperCommonSize;
uint8_t stubHelperCommonBytes[36];
ReferenceInfo stubHelperCommonReferenceToCache;
ReferenceInfo stubHelperCommonReferenceToBinder;
};
virtual const StubInfo &stubInfo() = 0;
protected:
ArchHandler();
static std::unique_ptr<mach_o::ArchHandler> create_x86_64();
static std::unique_ptr<mach_o::ArchHandler> create_x86();
static std::unique_ptr<mach_o::ArchHandler> create_arm();
// Handy way to pack mach-o r_type and other bit fields into one 16-bit value.
typedef uint16_t RelocPattern;
enum {
rScattered = 0x8000,
rPcRel = 0x4000,
rExtern = 0x2000,
rLength1 = 0x0000,
rLength2 = 0x0100,
rLength4 = 0x0200,
rLength8 = 0x0300,
rLenArmLo = rLength1,
rLenArmHi = rLength2,
rLenThmbLo = rLength4,
rLenThmbHi = rLength8
};
/// Extract RelocPattern from normalized mach-o relocation.
static RelocPattern relocPattern(const normalized::Relocation &reloc);
/// Create normalized Relocation initialized from pattern.
static normalized::Relocation relocFromPattern(RelocPattern pattern);
/// One liner to add a relocation.
static void appendReloc(normalized::Relocations &relocs, uint32_t offset,
uint32_t symbol, uint32_t value,
RelocPattern pattern);
static int16_t readS16(bool swap, const uint8_t *addr);
static int32_t readS32(bool swap, const uint8_t *addr);
static uint32_t readU32(bool swap, const uint8_t *addr);
static int64_t readS64(bool swap, const uint8_t *addr);
};
} // namespace mach_o
} // namespace lld
#endif // LLD_READER_WRITER_MACHO_ARCH_HANDLER_H
|