llvm.org GIT mirror llvm / f69a29b
Revert "Fix the build broken by r189315." and "Move everything depending on Object/MachOFormat.h over to Support/MachO.h." This reverts commits r189319 and r189315. r189315 broke some tests on what I believe are big-endian platforms. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@189321 91177308-0d34-0410-b5e6-96231b3b80d8 Charles Davis 7 years ago
20 changed file(s) with 1416 addition(s) and 983 deletion(s). Raw diff Collapse all Expand all
1414 #include "llvm/ADT/SmallString.h"
1515 #include "llvm/MC/MCExpr.h"
1616 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/Object/MachOFormat.h"
1718 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/MachO.h"
1919 #include
2020
2121 namespace llvm {
9797 /// @{
9898
9999 llvm::DenseMap
100 std::vector<MachO::any_relocation_info> > Relocations;
100 std::vector<object::macho::RelocationEntry> > Relocations;
101101 llvm::DenseMap IndirectSymBase;
102102
103103 /// @}
154154
155155 bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
156156 bool isARM() const {
157 uint32_t CPUType = TargetObjectWriter->getCPUType() & ~MachO::CPU_ARCH_MASK;
158 return CPUType == MachO::CPU_TYPE_ARM;
157 uint32_t CPUType = TargetObjectWriter->getCPUType() &
158 ~object::mach::CTFM_ArchMask;
159 return CPUType == object::mach::CTM_ARM;
159160 }
160161
161162 /// @}
211212 // these through in many cases.
212213
213214 void addRelocation(const MCSectionData *SD,
214 MachO::relocation_info &MRE) {
215 MachO::any_relocation_info AMRE;
216 memcpy(&AMRE, &MRE, sizeof(MRE));
217 Relocations[SD].push_back(AMRE);
218 }
219
220 void addRelocation(const MCSectionData *SD,
221 MachO::scattered_relocation_info &SMRE) {
222 MachO::any_relocation_info MRE;
223 memcpy(&MRE, &SMRE, sizeof(MRE));
224 Relocations[SD].push_back(MRE);
225 }
226
227 void addRelocation(const MCSectionData *SD,
228 MachO::any_relocation_info &MRE) {
215 object::macho::RelocationEntry &MRE) {
229216 Relocations[SD].push_back(MRE);
230217 }
231218
1717 #include "llvm/ADT/ArrayRef.h"
1818 #include "llvm/ADT/SmallVector.h"
1919 #include "llvm/ADT/Triple.h"
20 #include "llvm/Object/MachOFormat.h"
2021 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/MachO.h"
2222
2323 namespace llvm {
2424 namespace object {
5252 public:
5353 struct LoadCommandInfo {
5454 const char *Ptr; // Where in memory the load command is.
55 MachO::load_command C; // The command itself.
55 macho::LoadCommand C; // The command itself.
5656 };
5757
5858 MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
145145 ArrayRef getSectionRawFinalSegmentName(DataRefImpl Sec) const;
146146
147147 // MachO specific Info about relocations.
148 bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
149 unsigned getPlainRelocationSymbolNum(
150 const MachO::any_relocation_info &RE) const;
151 bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
152 bool getScatteredRelocationScattered(
153 const MachO::any_relocation_info &RE) const;
154 uint32_t getScatteredRelocationValue(
155 const MachO::any_relocation_info &RE) const;
156 unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
157 unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
158 unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
159 unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
160 SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const;
148 bool isRelocationScattered(const macho::RelocationEntry &RE) const;
149 unsigned getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const;
150 bool getPlainRelocationExternal(const macho::RelocationEntry &RE) const;
151 bool getScatteredRelocationScattered(const macho::RelocationEntry &RE) const;
152 uint32_t getScatteredRelocationValue(const macho::RelocationEntry &RE) const;
153 unsigned getAnyRelocationAddress(const macho::RelocationEntry &RE) const;
154 unsigned getAnyRelocationPCRel(const macho::RelocationEntry &RE) const;
155 unsigned getAnyRelocationLength(const macho::RelocationEntry &RE) const;
156 unsigned getAnyRelocationType(const macho::RelocationEntry &RE) const;
157 SectionRef getRelocationSection(const macho::RelocationEntry &RE) const;
161158
162159 // Walk load commands.
163160 LoadCommandInfo getFirstLoadCommandInfo() const;
164161 LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
165162
166163 // MachO specific structures.
167 MachO::section getSection(DataRefImpl DRI) const;
168 MachO::section_64 getSection64(DataRefImpl DRI) const;
169 MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
170 MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
171 MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
172 MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
173
174 MachO::linkedit_data_command
164 macho::Section getSection(DataRefImpl DRI) const;
165 macho::Section64 getSection64(DataRefImpl DRI) const;
166 macho::Section getSection(const LoadCommandInfo &L, unsigned Index) const;
167 macho::Section64 getSection64(const LoadCommandInfo &L, unsigned Index) const;
168 macho::SymbolTableEntry getSymbolTableEntry(DataRefImpl DRI) const;
169 macho::Symbol64TableEntry getSymbol64TableEntry(DataRefImpl DRI) const;
170
171 macho::LinkeditDataLoadCommand
175172 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
176 MachO::segment_command
173 macho::SegmentLoadCommand
177174 getSegmentLoadCommand(const LoadCommandInfo &L) const;
178 MachO::segment_command_64
175 macho::Segment64LoadCommand
179176 getSegment64LoadCommand(const LoadCommandInfo &L) const;
180 MachO::linker_options_command
177 macho::LinkerOptionsLoadCommand
181178 getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
182179
183 MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
184 MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
185 MachO::mach_header getHeader() const;
186 MachO::mach_header_64 getHeader64() const;
187 uint32_t
188 getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
180 macho::RelocationEntry getRelocation(DataRefImpl Rel) const;
181 macho::DataInCodeTableEntry getDice(DataRefImpl Rel) const;
182 macho::Header getHeader() const;
183 macho::Header64Ext getHeader64Ext() const;
184 macho::IndirectSymbolTableEntry
185 getIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
189186 unsigned Index) const;
190 MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
191 unsigned Index) const;
192 MachO::symtab_command getSymtabLoadCommand() const;
193 MachO::dysymtab_command getDysymtabLoadCommand() const;
194 MachO::linkedit_data_command getDataInCodeLoadCommand() const;
187 macho::DataInCodeTableEntry getDataInCodeTableEntry(uint32_t DataOffset,
188 unsigned Index) const;
189 macho::SymtabLoadCommand getSymtabLoadCommand() const;
190 macho::DysymtabLoadCommand getDysymtabLoadCommand() const;
191 macho::LinkeditDataLoadCommand getDataInCodeLoadCommand() const;
195192
196193 StringRef getStringTableData() const;
197194 bool is64Bit() const;
225222
226223 inline error_code DiceRef::getNext(DiceRef &Result) const {
227224 DataRefImpl Rel = DicePimpl;
228 const MachO::data_in_code_entry *P =
229 reinterpret_cast(Rel.p);
225 const macho::DataInCodeTableEntry *P =
226 reinterpret_cast(Rel.p);
230227 Rel.p = reinterpret_cast(P + 1);
231228 Result = DiceRef(Rel, OwningObject);
232229 return object_error::success;
239236 inline error_code DiceRef::getOffset(uint32_t &Result) const {
240237 const MachOObjectFile *MachOOF =
241238 static_cast(OwningObject);
242 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
243 Result = Dice.offset;
239 macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
240 Result = Dice.Offset;
244241 return object_error::success;
245242 }
246243
247244 inline error_code DiceRef::getLength(uint16_t &Result) const {
248245 const MachOObjectFile *MachOOF =
249246 static_cast(OwningObject);
250 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
251 Result = Dice.length;
247 macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
248 Result = Dice.Length;
252249 return object_error::success;
253250 }
254251
255252 inline error_code DiceRef::getKind(uint16_t &Result) const {
256253 const MachOObjectFile *MachOOF =
257254 static_cast(OwningObject);
258 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
259 Result = Dice.kind;
255 macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
256 Result = Dice.Kind;
260257 return object_error::success;
261258 }
262259
0 //===- MachOFormat.h - Mach-O Format Structures And Constants ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares various structures and constants which are platform
10 // independent and can be shared by any client which wishes to interact with
11 // Mach object files.
12 //
13 // The definitions here are purposely chosen to match the LLVM style as opposed
14 // to following the platform specific definition of the format.
15 //
16 // On a Mach system, see the includes for more information, in
17 // particular .
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_OBJECT_MACHOFORMAT_H
22 #define LLVM_OBJECT_MACHOFORMAT_H
23
24 #include "llvm/Support/DataTypes.h"
25
26 namespace llvm {
27 namespace object {
28
29 /// General Mach platform information.
30 namespace mach {
31 /// @name CPU Type and Subtype Information
32 /// {
33
34 /// \brief Capability bits used in CPU type encoding.
35 enum CPUTypeFlagsMask {
36 CTFM_ArchMask = 0xFF000000,
37 CTFM_ArchABI64 = 0x01000000
38 };
39
40 /// \brief Machine type IDs used in CPU type encoding.
41 enum CPUTypeMachine {
42 CTM_i386 = 7,
43 CTM_x86_64 = CTM_i386 | CTFM_ArchABI64,
44 CTM_ARM = 12,
45 CTM_SPARC = 14,
46 CTM_PowerPC = 18,
47 CTM_PowerPC64 = CTM_PowerPC | CTFM_ArchABI64
48 };
49
50 /// \brief Capability bits used in CPU subtype encoding.
51 enum CPUSubtypeFlagsMask {
52 CSFM_SubtypeMask = 0xFF000000,
53 CSFM_SubtypeLib64 = 0x80000000
54 };
55
56 /// \brief ARM Machine Subtypes.
57 enum CPUSubtypeARM {
58 CSARM_ALL = 0,
59 CSARM_V4T = 5,
60 CSARM_V6 = 6,
61 CSARM_V5TEJ = 7,
62 CSARM_XSCALE = 8,
63 CSARM_V7 = 9,
64 CSARM_V7F = 10,
65 CSARM_V7S = 11,
66 CSARM_V7K = 12,
67 CSARM_V6M = 14,
68 CSARM_V7M = 15,
69 CSARM_V7EM = 16
70 };
71
72 /// \brief PowerPC Machine Subtypes.
73 enum CPUSubtypePowerPC {
74 CSPPC_ALL = 0
75 };
76
77 /// \brief SPARC Machine Subtypes.
78 enum CPUSubtypeSPARC {
79 CSSPARC_ALL = 0
80 };
81
82 /// \brief x86 Machine Subtypes.
83 enum CPUSubtypeX86 {
84 CSX86_ALL = 3
85 };
86
87 /// @}
88
89 } // end namespace mach
90
91 /// Format information for Mach object files.
92 namespace macho {
93 /// \brief Constants for structure sizes.
94 enum StructureSizes {
95 Header32Size = 28,
96 Header64Size = 32,
97 FatHeaderSize = 8,
98 FatArchHeaderSize = 20,
99 SegmentLoadCommand32Size = 56,
100 SegmentLoadCommand64Size = 72,
101 Section32Size = 68,
102 Section64Size = 80,
103 SymtabLoadCommandSize = 24,
104 DysymtabLoadCommandSize = 80,
105 Nlist32Size = 12,
106 Nlist64Size = 16,
107 RelocationInfoSize = 8,
108 LinkeditLoadCommandSize = 16
109 };
110
111 /// \brief Constants for header magic field.
112 enum HeaderMagic {
113 HM_Object32 = 0xFEEDFACE, ///< 32-bit mach object file
114 HM_Object64 = 0xFEEDFACF, ///< 64-bit mach object file
115 HM_Universal = 0xCAFEBABE ///< Universal object file
116 };
117
118 /// \brief Header common to all Mach object files.
119 struct Header {
120 uint32_t Magic;
121 uint32_t CPUType;
122 uint32_t CPUSubtype;
123 uint32_t FileType;
124 uint32_t NumLoadCommands;
125 uint32_t SizeOfLoadCommands;
126 uint32_t Flags;
127 };
128
129 /// \brief Extended header for 64-bit object files.
130 struct Header64Ext {
131 uint32_t Reserved;
132 };
133
134 /// \brief Header for universal object files.
135 struct FatHeader {
136 uint32_t Magic;
137 uint32_t NumFatArch;
138 };
139
140 /// \brief Header for a single-architecture object file in a
141 /// universal binary.
142 struct FatArchHeader {
143 uint32_t CPUType;
144 uint32_t CPUSubtype;
145 uint32_t Offset;
146 uint32_t Size;
147 uint32_t Align;
148 };
149
150 // See .
151 enum HeaderFileType {
152 HFT_Object = 0x1
153 };
154
155 enum HeaderFlags {
156 HF_SubsectionsViaSymbols = 0x2000
157 };
158
159 enum LoadCommandType {
160 LCT_Segment = 0x1,
161 LCT_Symtab = 0x2,
162 LCT_Dysymtab = 0xb,
163 LCT_Segment64 = 0x19,
164 LCT_UUID = 0x1b,
165 LCT_CodeSignature = 0x1d,
166 LCT_SegmentSplitInfo = 0x1e,
167 LCT_FunctionStarts = 0x26,
168 LCT_DataInCode = 0x29,
169 LCT_LinkerOptions = 0x2D
170 };
171
172 /// \brief Load command structure.
173 struct LoadCommand {
174 uint32_t Type;
175 uint32_t Size;
176 };
177
178 /// @name Load Command Structures
179 /// @{
180
181 struct SegmentLoadCommand {
182 uint32_t Type;
183 uint32_t Size;
184 char Name[16];
185 uint32_t VMAddress;
186 uint32_t VMSize;
187 uint32_t FileOffset;
188 uint32_t FileSize;
189 uint32_t MaxVMProtection;
190 uint32_t InitialVMProtection;
191 uint32_t NumSections;
192 uint32_t Flags;
193 };
194
195 struct Segment64LoadCommand {
196 uint32_t Type;
197 uint32_t Size;
198 char Name[16];
199 uint64_t VMAddress;
200 uint64_t VMSize;
201 uint64_t FileOffset;
202 uint64_t FileSize;
203 uint32_t MaxVMProtection;
204 uint32_t InitialVMProtection;
205 uint32_t NumSections;
206 uint32_t Flags;
207 };
208
209 struct SymtabLoadCommand {
210 uint32_t Type;
211 uint32_t Size;
212 uint32_t SymbolTableOffset;
213 uint32_t NumSymbolTableEntries;
214 uint32_t StringTableOffset;
215 uint32_t StringTableSize;
216 };
217
218 struct DysymtabLoadCommand {
219 uint32_t Type;
220 uint32_t Size;
221
222 uint32_t LocalSymbolsIndex;
223 uint32_t NumLocalSymbols;
224
225 uint32_t ExternalSymbolsIndex;
226 uint32_t NumExternalSymbols;
227
228 uint32_t UndefinedSymbolsIndex;
229 uint32_t NumUndefinedSymbols;
230
231 uint32_t TOCOffset;
232 uint32_t NumTOCEntries;
233
234 uint32_t ModuleTableOffset;
235 uint32_t NumModuleTableEntries;
236
237 uint32_t ReferenceSymbolTableOffset;
238 uint32_t NumReferencedSymbolTableEntries;
239
240 uint32_t IndirectSymbolTableOffset;
241 uint32_t NumIndirectSymbolTableEntries;
242
243 uint32_t ExternalRelocationTableOffset;
244 uint32_t NumExternalRelocationTableEntries;
245
246 uint32_t LocalRelocationTableOffset;
247 uint32_t NumLocalRelocationTableEntries;
248 };
249
250 struct LinkeditDataLoadCommand {
251 uint32_t Type;
252 uint32_t Size;
253 uint32_t DataOffset;
254 uint32_t DataSize;
255 };
256
257 struct LinkerOptionsLoadCommand {
258 uint32_t Type;
259 uint32_t Size;
260 uint32_t Count;
261 // Load command is followed by Count number of zero-terminated UTF8 strings,
262 // and then zero-filled to be 4-byte aligned.
263 };
264
265 /// @}
266 /// @name Section Data
267 /// @{
268
269 enum SectionFlags {
270 SF_PureInstructions = 0x80000000
271 };
272
273 struct Section {
274 char Name[16];
275 char SegmentName[16];
276 uint32_t Address;
277 uint32_t Size;
278 uint32_t Offset;
279 uint32_t Align;
280 uint32_t RelocationTableOffset;
281 uint32_t NumRelocationTableEntries;
282 uint32_t Flags;
283 uint32_t Reserved1;
284 uint32_t Reserved2;
285 };
286 struct Section64 {
287 char Name[16];
288 char SegmentName[16];
289 uint64_t Address;
290 uint64_t Size;
291 uint32_t Offset;
292 uint32_t Align;
293 uint32_t RelocationTableOffset;
294 uint32_t NumRelocationTableEntries;
295 uint32_t Flags;
296 uint32_t Reserved1;
297 uint32_t Reserved2;
298 uint32_t Reserved3;
299 };
300
301 /// @}
302 /// @name Symbol Table Entries
303 /// @{
304
305 struct SymbolTableEntry {
306 uint32_t StringIndex;
307 uint8_t Type;
308 uint8_t SectionIndex;
309 uint16_t Flags;
310 uint32_t Value;
311 };
312 // Despite containing a uint64_t, this structure is only 4-byte aligned within
313 // a MachO file.
314 #pragma pack(push)
315 #pragma pack(4)
316 struct Symbol64TableEntry {
317 uint32_t StringIndex;
318 uint8_t Type;
319 uint8_t SectionIndex;
320 uint16_t Flags;
321 uint64_t Value;
322 };
323 #pragma pack(pop)
324
325 /// @}
326 /// @name Data-in-code Table Entry
327 /// @{
328
329 // See .
330 enum DataRegionType { Data = 1, JumpTable8, JumpTable16, JumpTable32 };
331 struct DataInCodeTableEntry {
332 uint32_t Offset; /* from mach_header to start of data region */
333 uint16_t Length; /* number of bytes in data region */
334 uint16_t Kind; /* a DataRegionType value */
335 };
336
337 /// @}
338 /// @name Indirect Symbol Table
339 /// @{
340
341 struct IndirectSymbolTableEntry {
342 uint32_t Index;
343 };
344
345 /// @}
346 /// @name Relocation Data
347 /// @{
348
349 struct RelocationEntry {
350 uint32_t Word0;
351 uint32_t Word1;
352 };
353
354 /// @}
355
356 // See .
357 enum SymbolTypeType {
358 STT_Undefined = 0x00,
359 STT_Absolute = 0x02,
360 STT_Section = 0x0e
361 };
362
363 enum SymbolTypeFlags {
364 // If any of these bits are set, then the entry is a stab entry number (see
365 // . Otherwise the other masks apply.
366 STF_StabsEntryMask = 0xe0,
367
368 STF_TypeMask = 0x0e,
369 STF_External = 0x01,
370 STF_PrivateExtern = 0x10
371 };
372
373 /// IndirectSymbolFlags - Flags for encoding special values in the indirect
374 /// symbol entry.
375 enum IndirectSymbolFlags {
376 ISF_Local = 0x80000000,
377 ISF_Absolute = 0x40000000
378 };
379
380 /// RelocationFlags - Special flags for addresses.
381 enum RelocationFlags {
382 RF_Scattered = 0x80000000
383 };
384
385 /// Common relocation info types.
386 enum RelocationInfoType {
387 RIT_Vanilla = 0,
388 RIT_Pair = 1,
389 RIT_Difference = 2
390 };
391
392 /// Generic relocation info types, which are shared by some (but not all)
393 /// platforms.
394 enum RelocationInfoType_Generic {
395 RIT_Generic_PreboundLazyPointer = 3,
396 RIT_Generic_LocalDifference = 4,
397 RIT_Generic_TLV = 5
398 };
399
400 /// X86_64 uses its own relocation types.
401 enum RelocationInfoTypeX86_64 {
402 // Note that x86_64 doesn't even share the common relocation types.
403 RIT_X86_64_Unsigned = 0,
404 RIT_X86_64_Signed = 1,
405 RIT_X86_64_Branch = 2,
406 RIT_X86_64_GOTLoad = 3,
407 RIT_X86_64_GOT = 4,
408 RIT_X86_64_Subtractor = 5,
409 RIT_X86_64_Signed1 = 6,
410 RIT_X86_64_Signed2 = 7,
411 RIT_X86_64_Signed4 = 8,
412 RIT_X86_64_TLV = 9
413 };
414
415 /// ARM uses its own relocation types.
416 enum RelocationInfoTypeARM {
417 RIT_ARM_LocalDifference = 3,
418 RIT_ARM_PreboundLazyPointer = 4,
419 RIT_ARM_Branch24Bit = 5,
420 RIT_ARM_ThumbBranch22Bit = 6,
421 RIT_ARM_ThumbBranch32Bit = 7,
422 RIT_ARM_Half = 8,
423 RIT_ARM_HalfDifference = 9
424
425 };
426
427 /// PPC relocation types from
428 enum RelocationInfoTypePPC {
429 RIT_PPC_BR14 = RIT_Pair +1,
430 RIT_PPC_BR24,
431 RIT_PPC_HI16,
432 RIT_PPC_LO16,
433 RIT_PPC_HA16,
434 RIT_PPC_LO14,
435 RIT_PPC_SECTDIFF,
436 RIT_PPC_PB_LA_PTR,
437 RIT_PPC_HI16_SECTDIFF,
438 RIT_PPC_LO16_SECTDIFF,
439 RIT_PPC_HA16_SECTDIFF,
440 RIT_PPC_JBSR,
441 RIT_PPC_LO14_SECTDIFF,
442 RIT_PPC_LOCAL_SECTDIFF,
443 RIT_PPC_TLV
444 };
445
446 } // end namespace macho
447
448 } // end namespace object
449 } // end namespace llvm
450
451 #endif
1717 #include "llvm/ADT/StringRef.h"
1818 #include "llvm/ADT/Triple.h"
1919 #include "llvm/Object/Binary.h"
20 #include "llvm/Support/MachO.h"
20 #include "llvm/Object/MachOFormat.h"
2121
2222 namespace llvm {
2323 namespace object {
3434 /// \brief Index of object in the universal binary.
3535 uint32_t Index;
3636 /// \brief Descriptor of the object.
37 MachO::fat_arch Header;
37 macho::FatArchHeader Header;
3838
3939 public:
4040 ObjectForArch(const MachOUniversalBinary *Parent, uint32_t Index);
4949 }
5050
5151 ObjectForArch getNext() const { return ObjectForArch(Parent, Index + 1); }
52 uint32_t getCPUType() const { return Header.cputype; }
52 uint32_t getCPUType() const { return Header.CPUType; }
5353
5454 error_code getAsObjectFile(OwningPtr &Result) const;
5555 };
179179 switch (Type) {
180180 default:
181181 llvm_unreachable("Invalid relocation type!");
182 case MachO::GENERIC_RELOC_VANILLA: {
182 case macho::RIT_Vanilla: {
183183 uint8_t *p = LocalAddress;
184184 uint64_t ValueToWrite = Value + Addend;
185185 for (unsigned i = 0; i < Size; ++i) {
188188 }
189189 return false;
190190 }
191 case MachO::GENERIC_RELOC_SECTDIFF:
192 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF:
193 case MachO::GENERIC_RELOC_PB_LA_PTR:
191 case macho::RIT_Difference:
192 case macho::RIT_Generic_LocalDifference:
193 case macho::RIT_Generic_PreboundLazyPointer:
194194 return Error("Relocation type not implemented yet!");
195195 }
196196 }
212212 switch(Type) {
213213 default:
214214 llvm_unreachable("Invalid relocation type!");
215 case MachO::X86_64_RELOC_SIGNED_1:
216 case MachO::X86_64_RELOC_SIGNED_2:
217 case MachO::X86_64_RELOC_SIGNED_4:
218 case MachO::X86_64_RELOC_SIGNED:
219 case MachO::X86_64_RELOC_UNSIGNED:
220 case MachO::X86_64_RELOC_BRANCH: {
215 case macho::RIT_X86_64_Signed1:
216 case macho::RIT_X86_64_Signed2:
217 case macho::RIT_X86_64_Signed4:
218 case macho::RIT_X86_64_Signed:
219 case macho::RIT_X86_64_Unsigned:
220 case macho::RIT_X86_64_Branch: {
221221 Value += Addend;
222222 // Mask in the target value a byte at a time (we don't have an alignment
223223 // guarantee for the target address, so this is safest).
228228 }
229229 return false;
230230 }
231 case MachO::X86_64_RELOC_GOT_LOAD:
232 case MachO::X86_64_RELOC_GOT:
233 case MachO::X86_64_RELOC_SUBTRACTOR:
234 case MachO::X86_64_RELOC_TLV:
231 case macho::RIT_X86_64_GOTLoad:
232 case macho::RIT_X86_64_GOT:
233 case macho::RIT_X86_64_Subtractor:
234 case macho::RIT_X86_64_TLV:
235235 return Error("Relocation type not implemented yet!");
236236 }
237237 }
256256 switch(Type) {
257257 default:
258258 llvm_unreachable("Invalid relocation type!");
259 case MachO::ARM_RELOC_VANILLA: {
259 case macho::RIT_Vanilla: {
260260 // Mask in the target value a byte at a time (we don't have an alignment
261261 // guarantee for the target address, so this is safest).
262262 uint8_t *p = (uint8_t*)LocalAddress;
263263 for (unsigned i = 0; i < Size; ++i) {
264 *p++ = (uint8_t)(Value & 0xff);
264 *p++ = (uint8_t)Value;
265265 Value >>= 8;
266266 }
267267 break;
268268 }
269 case MachO::ARM_RELOC_BR24: {
269 case macho::RIT_ARM_Branch24Bit: {
270270 // Mask the value into the target address. We know instructions are
271271 // 32-bit aligned, so we can do it all at once.
272272 uint32_t *p = (uint32_t*)LocalAddress;
282282 *p = (*p & ~0xffffff) | Value;
283283 break;
284284 }
285 case MachO::ARM_THUMB_RELOC_BR22:
286 case MachO::ARM_THUMB_32BIT_BRANCH:
287 case MachO::ARM_RELOC_HALF:
288 case MachO::ARM_RELOC_HALF_SECTDIFF:
289 case MachO::ARM_RELOC_PAIR:
290 case MachO::ARM_RELOC_SECTDIFF:
291 case MachO::ARM_RELOC_LOCAL_SECTDIFF:
292 case MachO::ARM_RELOC_PB_LA_PTR:
285 case macho::RIT_ARM_ThumbBranch22Bit:
286 case macho::RIT_ARM_ThumbBranch32Bit:
287 case macho::RIT_ARM_Half:
288 case macho::RIT_ARM_HalfDifference:
289 case macho::RIT_Pair:
290 case macho::RIT_Difference:
291 case macho::RIT_ARM_LocalDifference:
292 case macho::RIT_ARM_PreboundLazyPointer:
293293 return Error("Relocation type not implemented yet!");
294294 }
295295 return false;
303303 StubMap &Stubs) {
304304 const ObjectFile *OF = Obj.getObjectFile();
305305 const MachOObjectFile *MachO = static_cast(OF);
306 MachO::any_relocation_info RE= MachO->getRelocation(RelI.getRawDataRefImpl());
306 macho::RelocationEntry RE = MachO->getRelocation(RelI.getRawDataRefImpl());
307307
308308 uint32_t RelType = MachO->getAnyRelocationType(RE);
309309
358358 Value.Addend = Addend - Addr;
359359 }
360360
361 if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
362 RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
361 if (Arch == Triple::x86_64 && (RelType == macho::RIT_X86_64_GOT ||
362 RelType == macho::RIT_X86_64_GOTLoad)) {
363363 assert(IsPCRel);
364364 assert(Size == 2);
365365 StubMap::const_iterator i = Stubs.find(Value);
370370 Stubs[Value] = Section.StubOffset;
371371 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
372372 RelocationEntry RE(SectionID, Section.StubOffset,
373 MachO::X86_64_RELOC_UNSIGNED, 0, false, 3);
373 macho::RIT_X86_64_Unsigned, 0, false, 3);
374374 if (Value.SymbolName)
375375 addRelocationForSymbol(RE, Value.SymbolName);
376376 else
379379 Addr = GOTEntry;
380380 }
381381 resolveRelocation(Section, Offset, (uint64_t)Addr,
382 MachO::X86_64_RELOC_UNSIGNED, Value.Addend, true, 2);
382 macho::RIT_X86_64_Unsigned, Value.Addend, true, 2);
383383 } else if (Arch == Triple::arm &&
384 (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
384 (RelType & 0xf) == macho::RIT_ARM_Branch24Bit) {
385385 // This is an ARM branch relocation, need to use a stub function.
386386
387387 // Look up for existing stub.
396396 uint8_t *StubTargetAddr = createStubFunction(Section.Address +
397397 Section.StubOffset);
398398 RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
399 MachO::GENERIC_RELOC_VANILLA, Value.Addend);
399 macho::RIT_Vanilla, Value.Addend);
400400 if (Value.SymbolName)
401401 addRelocationForSymbol(RE, Value.SymbolName);
402402 else
536536
537537 // Look for LC_MAIN.
538538 {
539 uint32_t LoadCommandCount = MOOF.getHeader().ncmds;
539 uint32_t LoadCommandCount = MOOF.getHeader().NumLoadCommands;
540540 MachOObjectFile::LoadCommandInfo Load = MOOF.getFirstLoadCommandInfo();
541541 for (unsigned I = 0;; ++I) {
542 if (Load.C.cmd == MachO::LC_MAIN) {
542 if (Load.C.Type == MachO::LC_MAIN) {
543543 EntryFileOffset =
544544 ((const MachO::entry_point_command *)Load.Ptr)->entryoff;
545545 break;
5959 if (Name == "__stubs") {
6060 SectionRef StubsSec = *SI;
6161 if (MOOF->is64Bit()) {
62 MachO::section_64 S = MOOF->getSection64(StubsSec.getRawDataRefImpl());
63 StubsIndSymIndex = S.reserved1;
64 StubSize = S.reserved2;
62 macho::Section64 S = MOOF->getSection64(StubsSec.getRawDataRefImpl());
63 StubsIndSymIndex = S.Reserved1;
64 StubSize = S.Reserved2;
6565 } else {
66 MachO::section S = MOOF->getSection(StubsSec.getRawDataRefImpl());
67 StubsIndSymIndex = S.reserved1;
68 StubSize = S.reserved2;
66 macho::Section S = MOOF->getSection(StubsSec.getRawDataRefImpl());
67 StubsIndSymIndex = S.Reserved1;
68 StubSize = S.Reserved2;
6969 }
7070 assert(StubSize && "Mach-O stub entry size can't be zero!");
7171 StubsSec.getAddress(StubsStart);
8585 if (StubIdx >= StubsCount)
8686 return StringRef();
8787
88 uint32_t SymtabIdx =
88 macho::IndirectSymbolTableEntry ISTE =
8989 MOOF->getIndirectSymbolTableEntry(MOOF->getDysymtabLoadCommand(), StubIdx);
90 uint32_t SymtabIdx = ISTE.Index;
9091
9192 StringRef SymName;
9293 symbol_iterator SI = MOOF->begin_symbols();
1919 #include "llvm/MC/MCSectionMachO.h"
2020 #include "llvm/MC/MCSymbol.h"
2121 #include "llvm/MC/MCValue.h"
22 #include "llvm/Object/MachOFormat.h"
2223 #include "llvm/Support/Debug.h"
2324 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/MachO.h"
2525 #include
2626 using namespace llvm;
27 using namespace llvm::object;
2728
2829 void MachObjectWriter::reset() {
2930 Relocations.clear();
126127 uint32_t Flags = 0;
127128
128129 if (SubsectionsViaSymbols)
129 Flags |= MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
130 Flags |= macho::HF_SubsectionsViaSymbols;
130131
131132 // struct mach_header (28 bytes) or
132133 // struct mach_header_64 (32 bytes)
134135 uint64_t Start = OS.tell();
135136 (void) Start;
136137
137 Write32(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC);
138 Write32(is64Bit() ? macho::HM_Object64 : macho::HM_Object32);
138139
139140 Write32(TargetObjectWriter->getCPUType());
140141 Write32(TargetObjectWriter->getCPUSubtype());
141142
142 Write32(MachO::MH_OBJECT);
143 Write32(macho::HFT_Object);
143144 Write32(NumLoadCommands);
144145 Write32(LoadCommandsSize);
145146 Write32(Flags);
147148 Write32(0); // reserved
148149
149150 assert(OS.tell() - Start ==
150 (is64Bit()?sizeof(MachO::mach_header_64): sizeof(MachO::mach_header)));
151 (is64Bit() ? macho::Header64Size : macho::Header32Size));
151152 }
152153
153154 /// WriteSegmentLoadCommand - Write a segment load command.
165166 (void) Start;
166167
167168 unsigned SegmentLoadCommandSize =
168 is64Bit() ? sizeof(MachO::segment_command_64):
169 sizeof(MachO::segment_command);
170 Write32(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT);
169 is64Bit() ? macho::SegmentLoadCommand64Size:
170 macho::SegmentLoadCommand32Size;
171 Write32(is64Bit() ? macho::LCT_Segment64 : macho::LCT_Segment);
171172 Write32(SegmentLoadCommandSize +
172 NumSections * (is64Bit() ? sizeof(MachO::section_64) :
173 sizeof(MachO::section)));
173 NumSections * (is64Bit() ? macho::Section64Size :
174 macho::Section32Size));
174175
175176 WriteBytes("", 16);
176177 if (is64Bit()) {
238239 if (is64Bit())
239240 Write32(0); // reserved3
240241
241 assert(OS.tell() - Start == (is64Bit() ? sizeof(MachO::section_64) :
242 sizeof(MachO::section)));
242 assert(OS.tell() - Start == (is64Bit() ? macho::Section64Size :
243 macho::Section32Size));
243244 }
244245
245246 void MachObjectWriter::WriteSymtabLoadCommand(uint32_t SymbolOffset,
251252 uint64_t Start = OS.tell();
252253 (void) Start;
253254
254 Write32(MachO::LC_SYMTAB);
255 Write32(sizeof(MachO::symtab_command));
255 Write32(macho::LCT_Symtab);
256 Write32(macho::SymtabLoadCommandSize);
256257 Write32(SymbolOffset);
257258 Write32(NumSymbols);
258259 Write32(StringTableOffset);
259260 Write32(StringTableSize);
260261
261 assert(OS.tell() - Start == sizeof(MachO::symtab_command));
262 assert(OS.tell() - Start == macho::SymtabLoadCommandSize);
262263 }
263264
264265 void MachObjectWriter::WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
274275 uint64_t Start = OS.tell();
275276 (void) Start;
276277
277 Write32(MachO::LC_DYSYMTAB);
278 Write32(sizeof(MachO::dysymtab_command));
278 Write32(macho::LCT_Dysymtab);
279 Write32(macho::DysymtabLoadCommandSize);
279280 Write32(FirstLocalSymbol);
280281 Write32(NumLocalSymbols);
281282 Write32(FirstExternalSymbol);
295296 Write32(0); // locreloff
296297 Write32(0); // nlocrel
297298
298 assert(OS.tell() - Start == sizeof(MachO::dysymtab_command));
299 assert(OS.tell() - Start == macho::DysymtabLoadCommandSize);
299300 }
300301
301302 void MachObjectWriter::WriteNlist(MachSymbolData &MSD,
310311 //
311312 // FIXME: Are the prebound or indirect fields possible here?
312313 if (Symbol.isUndefined())
313 Type = MachO::N_UNDF;
314 Type = macho::STT_Undefined;
314315 else if (Symbol.isAbsolute())
315 Type = MachO::N_ABS;
316 Type = macho::STT_Absolute;
316317 else
317 Type = MachO::N_SECT;
318 Type = macho::STT_Section;
318319
319320 // FIXME: Set STAB bits.
320321
321322 if (Data.isPrivateExtern())
322 Type |= MachO::N_PEXT;
323 Type |= macho::STF_PrivateExtern;
323324
324325 // Set external bit.
325326 if (Data.isExternal() || Symbol.isUndefined())
326 Type |= MachO::N_EXT;
327 Type |= macho::STF_External;
327328
328329 // Compute the symbol address.
329330 if (Symbol.isDefined()) {
367368 (void) Start;
368369
369370 Write32(Type);
370 Write32(sizeof(MachO::linkedit_data_command));
371 Write32(macho::LinkeditLoadCommandSize);
371372 Write32(DataOffset);
372373 Write32(DataSize);
373374
374 assert(OS.tell() - Start == sizeof(MachO::linkedit_data_command));
375 assert(OS.tell() - Start == macho::LinkeditLoadCommandSize);
375376 }
376377
377378 static unsigned ComputeLinkerOptionsLoadCommandSize(
378379 const std::vector &Options, bool is64Bit)
379380 {
380 unsigned Size = sizeof(MachO::linker_options_command);
381 unsigned Size = sizeof(macho::LinkerOptionsLoadCommand);
381382 for (unsigned i = 0, e = Options.size(); i != e; ++i)
382383 Size += Options[i].size() + 1;
383384 return RoundUpToAlignment(Size, is64Bit ? 8 : 4);
390391 uint64_t Start = OS.tell();
391392 (void) Start;
392393
393 Write32(MachO::LC_LINKER_OPTIONS);
394 Write32(macho::LCT_LinkerOptions);
394395 Write32(Size);
395396 Write32(Options.size());
396 uint64_t BytesWritten = sizeof(MachO::linker_options_command);
397 uint64_t BytesWritten = sizeof(macho::LinkerOptionsLoadCommand);
397398 for (unsigned i = 0, e = Options.size(); i != e; ++i) {
398399 // Write each string, including the null byte.
399400 const std::string &Option = Options[i];
721722 // section headers) and the symbol table.
722723 unsigned NumLoadCommands = 1;
723724 uint64_t LoadCommandsSize = is64Bit() ?
724 sizeof(MachO::segment_command_64) + NumSections * sizeof(MachO::section_64):
725 sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
725 macho::SegmentLoadCommand64Size + NumSections * macho::Section64Size :
726 macho::SegmentLoadCommand32Size + NumSections * macho::Section32Size;
726727
727728 // Add the data-in-code load command size, if used.
728729 unsigned NumDataRegions = Asm.getDataRegions().size();
729730 if (NumDataRegions) {
730731 ++NumLoadCommands;
731 LoadCommandsSize += sizeof(MachO::linkedit_data_command);
732 LoadCommandsSize += macho::LinkeditLoadCommandSize;
732733 }
733734
734735 // Add the symbol table load command sizes, if used.
736737 UndefinedSymbolData.size();
737738 if (NumSymbols) {
738739 NumLoadCommands += 2;
739 LoadCommandsSize += (sizeof(MachO::symtab_command) +
740 sizeof(MachO::dysymtab_command));
740 LoadCommandsSize += (macho::SymtabLoadCommandSize +
741 macho::DysymtabLoadCommandSize);
741742 }
742743
743744 // Add the linker option load commands sizes.
751752
752753 // Compute the total size of the section data, as well as its file size and vm
753754 // size.
754 uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) :
755 sizeof(MachO::mach_header)) + LoadCommandsSize;
755 uint64_t SectionDataStart = (is64Bit() ? macho::Header64Size :
756 macho::Header32Size) + LoadCommandsSize;
756757 uint64_t SectionDataSize = 0;
757758 uint64_t SectionDataFileSize = 0;
758759 uint64_t VMSize = 0;
789790 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
790791 for (MCAssembler::const_iterator it = Asm.begin(),
791792 ie = Asm.end(); it != ie; ++it) {
792 std::vector<MachO::any_relocation_info> &Relocs = Relocations[it];
793 std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
793794 unsigned NumRelocs = Relocs.size();
794795 uint64_t SectionStart = SectionDataStart + getSectionAddress(it);
795796 WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
796 RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info);
797 RelocTableEnd += NumRelocs * macho::RelocationInfoSize;
797798 }
798799
799800 // Write the data-in-code load command, if used.
801802 if (NumDataRegions) {
802803 uint64_t DataRegionsOffset = RelocTableEnd;
803804 uint64_t DataRegionsSize = NumDataRegions * 8;
804 WriteLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset,
805 WriteLinkeditLoadCommand(macho::LCT_DataInCode, DataRegionsOffset,
805806 DataRegionsSize);
806807 }
807808
828829
829830 // The string table is written after symbol table.
830831 uint64_t StringTableOffset =
831 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ?
832 sizeof(MachO::nlist_64) :
833 sizeof(MachO::nlist));
832 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ? macho::Nlist64Size :
833 macho::Nlist32Size);
834834 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
835835 StringTableOffset, StringTable.size());
836836
863863 ie = Asm.end(); it != ie; ++it) {
864864 // Write the section relocation entries, in reverse order to match 'as'
865865 // (approximately, the exact algorithm is more complicated than this).
866 std::vector<MachO::any_relocation_info> &Relocs = Relocations[it];
866 std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
867867 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
868 Write32(Relocs[e - i - 1].r_word0);
869 Write32(Relocs[e - i - 1].r_word1);
868 Write32(Relocs[e - i - 1].Word0);
869 Write32(Relocs[e - i - 1].Word1);
870870 }
871871 }
872872
905905 // If this symbol is defined and internal, mark it as such.
906906 if (it->Symbol->isDefined() &&
907907 !Asm.getSymbolData(*it->Symbol).isExternal()) {
908 uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL;
908 uint32_t Flags = macho::ISF_Local;
909909 if (it->Symbol->isAbsolute())
910 Flags |= MachO::INDIRECT_SYMBOL_ABS;
910 Flags |= macho::ISF_Absolute;
911911 Write32(Flags);
912912 continue;
913913 }
1313
1414 #include "llvm/Object/MachO.h"
1515 #include "llvm/ADT/Triple.h"
16 #include "llvm/Object/MachOFormat.h"
1617 #include "llvm/Support/DataExtractor.h"
1718 #include "llvm/Support/Format.h"
1819 #include "llvm/Support/Host.h"
20 #include "llvm/Support/MachO.h"
1921 #include "llvm/Support/MemoryBuffer.h"
2022 #include "llvm/Support/raw_ostream.h"
2123 #include
2830 namespace llvm {
2931 namespace object {
3032
31 struct nlist_base {
32 uint32_t n_strx;
33 uint8_t n_type;
34 uint8_t n_sect;
35 uint16_t n_desc;
33 struct SymbolTableEntryBase {
34 uint32_t StringIndex;
35 uint8_t Type;
36 uint8_t SectionIndex;
37 uint16_t Flags;
3638 };
3739
38 struct section_base {
39 char sectname[16];
40 char segname[16];
40 struct SectionBase {
41 char Name[16];
42 char SegmentName[16];
4143 };
4244
4345 template
4951 static void SwapStruct(T &Value);
5052
5153 template<>
52 void SwapStruct(MachO::any_relocation_info &H) {
53 SwapValue(H.r_word0);
54 SwapValue(H.r_word1);
54 void SwapStruct(macho::RelocationEntry &H) {
55 SwapValue(H.Word0);
56 SwapValue(H.Word1);
5557 }
5658
5759 template<>
58 void SwapStruct(MachO::load_command &L) {
59 SwapValue(L.cmd);
60 SwapValue(L.cmdsize);
60 void SwapStruct(macho::LoadCommand &L) {
61 SwapValue(L.Type);
62 SwapValue(L.Size);
6163 }
6264
6365 template<>
64 void SwapStruct(nlist_base &S) {
65 SwapValue(S.n_strx);
66 SwapValue(S.n_desc);
66 void SwapStruct(SymbolTableEntryBase &S) {
67 SwapValue(S.StringIndex);
68 SwapValue(S.Flags);
6769 }
6870
6971 template<>
70 void SwapStruct(MachO::section &S) {
71 SwapValue(S.addr);
72 SwapValue(S.size);
73 SwapValue(S.offset);
74 SwapValue(S.align);
75 SwapValue(S.reloff);
76 SwapValue(S.nreloc);
77 SwapValue(S.flags);
78 SwapValue(S.reserved1);
79 SwapValue(S.reserved2);
72 void SwapStruct(macho::Section &S) {
73 SwapValue(S.Address);
74 SwapValue(S.Size);
75 SwapValue(S.Offset);
76 SwapValue(S.Align);
77 SwapValue(S.RelocationTableOffset);
78 SwapValue(S.NumRelocationTableEntries);
79 SwapValue(S.Flags);
80 SwapValue(S.Reserved1);
81 SwapValue(S.Reserved2);
8082 }
8183
8284 template<>
83 void SwapStruct(MachO::section_64 &S) {
84 SwapValue(S.addr);
85 SwapValue(S.size);
86 SwapValue(S.offset);
87 SwapValue(S.align);
88 SwapValue(S.reloff);
89 SwapValue(S.nreloc);
90 SwapValue(S.flags);
91 SwapValue(S.reserved1);
92 SwapValue(S.reserved2);
93 SwapValue(S.reserved3);
85 void SwapStruct(macho::Section64 &S) {
86 SwapValue(S.Address);
87 SwapValue(S.Size);
88 SwapValue(S.Offset);
89 SwapValue(S.Align);
90 SwapValue(S.RelocationTableOffset);
91 SwapValue(S.NumRelocationTableEntries);
92 SwapValue(S.Flags);
93 SwapValue(S.Reserved1);
94 SwapValue(S.Reserved2);
95 SwapValue(S.Reserved3);
9496 }
9597
9698 template<>
97 void SwapStruct(MachO::nlist &S) {
98 SwapValue(S.n_strx);
99 SwapValue(S.n_desc);
100 SwapValue(S.n_value);
99 void SwapStruct(macho::SymbolTableEntry &S) {
100 SwapValue(S.StringIndex);
101 SwapValue(S.Flags);
102 SwapValue(S.Value);
101103 }
102104
103105 template<>
104 void SwapStruct(MachO::nlist_64 &S) {
105 SwapValue(S.n_strx);
106 SwapValue(S.n_desc);
107 SwapValue(S.n_value);
106 void SwapStruct(macho::Symbol64TableEntry &S) {
107 SwapValue(S.StringIndex);
108 SwapValue(S.Flags);
109 SwapValue(S.Value);
108110 }
109111
110112 template<>
111 void SwapStruct(MachO::mach_header &H) {
112 SwapValue(H.magic);
113 SwapValue(H.cputype);
114 SwapValue(H.cpusubtype);
115 SwapValue(H.filetype);
116 SwapValue(H.ncmds);
117 SwapValue(H.sizeofcmds);
118 SwapValue(H.flags);
113 void SwapStruct(macho::Header &H) {
114 SwapValue(H.Magic);
115 SwapValue(H.CPUType);
116 SwapValue(H.CPUSubtype);
117 SwapValue(H.FileType);
118 SwapValue(H.NumLoadCommands);
119 SwapValue(H.SizeOfLoadCommands);
120 SwapValue(H.Flags);
119121 }
120122
121123 template<>
122 void SwapStruct(MachO::mach_header_64 &H) {
123 SwapValue(H.magic);
124 SwapValue(H.cputype);
125 SwapValue(H.cpusubtype);
126 SwapValue(H.filetype);
127 SwapValue(H.ncmds);
128 SwapValue(H.sizeofcmds);
129 SwapValue(H.flags);
130 SwapValue(H.reserved);
124 void SwapStruct(macho::Header64Ext &E) {
125 SwapValue(E.Reserved);
131126 }
132127
133128 template<>
134 void SwapStruct(MachO::symtab_command &C) {
135 SwapValue(C.cmd);
136 SwapValue(C.cmdsize);
137 SwapValue(C.symoff);
138 SwapValue(C.nsyms);
139 SwapValue(C.stroff);
140 SwapValue(C.strsize);
129 void SwapStruct(macho::SymtabLoadCommand &C) {
130 SwapValue(C.Type);
131 SwapValue(C.Size);
132 SwapValue(C.SymbolTableOffset);
133 SwapValue(C.NumSymbolTableEntries);
134 SwapValue(C.StringTableOffset);
135 SwapValue(C.StringTableSize);
141136 }
142137
143138 template<>
144 void SwapStruct(MachO::dysymtab_command &C) {
145 SwapValue(C.cmd);
146 SwapValue(C.cmdsize);
147 SwapValue(C.ilocalsym);
148 SwapValue(C.nlocalsym);
149 SwapValue(C.iextdefsym);
150 SwapValue(C.nextdefsym);
151 SwapValue(C.iundefsym);
152 SwapValue(C.nundefsym);
153 SwapValue(C.tocoff);
154 SwapValue(C.ntoc);
155 SwapValue(C.modtaboff);
156 SwapValue(C.nmodtab);
157 SwapValue(C.extrefsymoff);
158 SwapValue(C.nextrefsyms);
159 SwapValue(C.indirectsymoff);
160 SwapValue(C.nindirectsyms);
161 SwapValue(C.extreloff);
162 SwapValue(C.nextrel);
163 SwapValue(C.locreloff);
164 SwapValue(C.nlocrel);
139 void SwapStruct(macho::DysymtabLoadCommand &C) {
140 SwapValue(C.Type);
141 SwapValue(C.Size);
142 SwapValue(C.LocalSymbolsIndex);
143 SwapValue(C.NumLocalSymbols);
144 SwapValue(C.ExternalSymbolsIndex);
145 SwapValue(C.NumExternalSymbols);
146 SwapValue(C.UndefinedSymbolsIndex);
147 SwapValue(C.NumUndefinedSymbols);
148 SwapValue(C.TOCOffset);
149 SwapValue(C.NumTOCEntries);
150 SwapValue(C.ModuleTableOffset);
151 SwapValue(C.NumModuleTableEntries);
152 SwapValue(C.ReferenceSymbolTableOffset);
153 SwapValue(C.NumReferencedSymbolTableEntries);
154 SwapValue(C.IndirectSymbolTableOffset);
155 SwapValue(C.NumIndirectSymbolTableEntries);
156 SwapValue(C.ExternalRelocationTableOffset);
157 SwapValue(C.NumExternalRelocationTableEntries);
158 SwapValue(C.LocalRelocationTableOffset);
159 SwapValue(C.NumLocalRelocationTableEntries);
165160 }
166161
167162 template<>
168 void SwapStruct(MachO::linkedit_data_command &C) {
169 SwapValue(C.cmd);
170 SwapValue(C.cmdsize);
171 SwapValue(C.dataoff);
172 SwapValue(C.datasize);
163 void SwapStruct(macho::LinkeditDataLoadCommand &C) {
164 SwapValue(C.Type);
165 SwapValue(C.Size);
166 SwapValue(C.DataOffset);
167 SwapValue(C.DataSize);
173168 }
174169
175170 template<>
176 void SwapStruct(MachO::segment_command &C) {
177 SwapValue(C.cmd);
178 SwapValue(C.cmdsize);
179 SwapValue(C.vmaddr);
180 SwapValue(C.vmsize);
181 SwapValue(C.fileoff);
182 SwapValue(C.filesize);
183 SwapValue(C.maxprot);
184 SwapValue(C.initprot);
185 SwapValue(C.nsects);
186 SwapValue(C.flags);
171 void SwapStruct(macho::SegmentLoadCommand &C) {
172 SwapValue(C.Type);
173 SwapValue(C.Size);
174 SwapValue(C.VMAddress);
175 SwapValue(C.VMSize);
176 SwapValue(C.FileOffset);
177 SwapValue(C.FileSize);
178 SwapValue(C.MaxVMProtection);
179 SwapValue(C.InitialVMProtection);
180 SwapValue(C.NumSections);
181 SwapValue(C.Flags);
187182 }
188183
189184 template<>
190 void SwapStruct(MachO::segment_command_64 &C) {
191 SwapValue(C.cmd);
192 SwapValue(C.cmdsize);
193 SwapValue(C.vmaddr);
194 SwapValue(C.vmsize);
195 SwapValue(C.fileoff);
196 SwapValue(C.filesize);
197 SwapValue(C.maxprot);
198 SwapValue(C.initprot);
199 SwapValue(C.nsects);
200 SwapValue(C.flags);
185 void SwapStruct(macho::Segment64LoadCommand &C) {
186 SwapValue(C.Type);
187 SwapValue(C.Size);
188 SwapValue(C.VMAddress);
189 SwapValue(C.VMSize);
190 SwapValue(C.FileOffset);
191 SwapValue(C.FileSize);
192 SwapValue(C.MaxVMProtection);
193 SwapValue(C.InitialVMProtection);
194 SwapValue(C.NumSections);
195 SwapValue(C.Flags);
201196 }
202197
203198 template<>
204 void SwapStruct(uint32_t &C) {
205 SwapValue(C);
199 void SwapStruct(macho::IndirectSymbolTableEntry &C) {
200 SwapValue(C.Index);
206201 }
207202
208203 template<>
209 void SwapStruct(MachO::linker_options_command &C) {
210 SwapValue(C.cmd);
211 SwapValue(C.cmdsize);
212 SwapValue(C.count);
204 void SwapStruct(macho::LinkerOptionsLoadCommand &C) {
205 SwapValue(C.Type);
206 SwapValue(C.Size);
207 SwapValue(C.Count);
213208 }
214209
215210 template<>
216 void SwapStruct(MachO::data_in_code_entry &C) {
217 SwapValue(C.offset);
218 SwapValue(C.length);
219 SwapValue(C.kind);
211 void SwapStruct(macho::DataInCodeTableEntry &C) {
212 SwapValue(C.Offset);
213 SwapValue(C.Length);
214 SwapValue(C.Kind);
220215 }
221216
222217 template
232227 getSegmentLoadCommandNumSections(const MachOObjectFile *O,
233228 const MachOObjectFile::LoadCommandInfo &L) {
234229 if (O->is64Bit()) {
235 MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
236 return S.nsects;
237 }
238 MachO::segment_command S = O->getSegmentLoadCommand(L);
239 return S.nsects;
230 macho::Segment64LoadCommand S = O->getSegment64LoadCommand(L);
231 return S.NumSections;
232 }
233 macho::SegmentLoadCommand S = O->getSegmentLoadCommand(L);
234 return S.NumSections;
240235 }
241236
242237 static const char *
245240 uintptr_t CommandAddr = reinterpret_cast(L.Ptr);
246241
247242 bool Is64 = O->is64Bit();
248 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
249 sizeof(MachO::segment_command);
250 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
251 sizeof(MachO::section);
243 unsigned SegmentLoadSize = Is64 ? sizeof(macho::Segment64LoadCommand) :
244 sizeof(macho::SegmentLoadCommand);
245 unsigned SectionSize = Is64 ? sizeof(macho::Section64) :
246 sizeof(macho::Section);
252247
253248 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
254 return reinterpret_cast *>(SectionAddr);
249 return reinterpret_cast*>(SectionAddr);
255250 }
256251
257252 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
258253 return O->getData().substr(Offset, 1).data();
259254 }
260255
261 static nlist_base
256 static SymbolTableEntryBase
262257 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
263258 const char *P = reinterpret_cast(DRI.p);
264 return getStruct<nlist_base>(O, P);
259 return getStruct<SymbolTableEntryBase>(O, P);
265260 }
266261
267262 static StringRef parseSegmentOrSectionName(const char *P) {
289284 }
290285
291286 static unsigned getCPUType(const MachOObjectFile *O) {
292 return O->getHeader().cputype;
287 return O->getHeader().CPUType;
293288 }
294289
295290 static void printRelocationTargetName(const MachOObjectFile *O,
296 const MachO::any_relocation_info &RE,
291 const macho::RelocationEntry &RE,
297292 raw_string_ostream &fmt) {
298293 bool IsScattered = O->isRelocationScattered(RE);
299294
361356 fmt << S;
362357 }
363358
364 static uint32_t
365 getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
366 return RE.r_word0;
359 static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) {
360 return RE.Word0;
367361 }
368362
369363 static unsigned
370 getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
371 return RE.r_word0 & 0xffffff;
364 getScatteredRelocationAddress(const macho::RelocationEntry &RE) {
365 return RE.Word0 & 0xffffff;
372366 }
373367
374368 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
375 const MachO::any_relocation_info &RE) {
369 const macho::RelocationEntry &RE) {
376370 if (O->isLittleEndian())
377 return (RE.r_word1 >> 24) & 1;
378 return (RE.r_word1 >> 7) & 1;
371 return (RE.Word1 >> 24) & 1;
372 return (RE.Word1 >> 7) & 1;
379373 }
380374
381375 static bool
382376 getScatteredRelocationPCRel(const MachOObjectFile *O,
383 const MachO::any_relocation_info &RE) {
384 return (RE.r_word0 >> 30) & 1;
377 const macho::RelocationEntry &RE) {
378 return (RE.Word0 >> 30) & 1;
385379 }
386380
387381 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
388 const MachO::any_relocation_info &RE) {
382 const macho::RelocationEntry &RE) {
389383 if (O->isLittleEndian())
390 return (RE.r_word1 >> 25) & 3;
391 return (RE.r_word1 >> 5) & 3;
384 return (RE.Word1 >> 25) & 3;
385 return (RE.Word1 >> 5) & 3;
392386 }
393387
394388 static unsigned
395 getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
396 return (RE.r_word0 >> 28) & 3;
389 getScatteredRelocationLength(const macho::RelocationEntry &RE) {
390 return (RE.Word0 >> 28) & 3;
397391 }
398392
399393 static unsigned getPlainRelocationType(const MachOObjectFile *O,
400 const MachO::any_relocation_info &RE) {
394 const macho::RelocationEntry &RE) {
401395 if (O->isLittleEndian())
402 return RE.r_word1 >> 28;
403 return RE.r_word1 & 0xf;
404 }
405
406 static unsigned
407 getScatteredRelocationType(const MachO::any_relocation_info &RE) {
408 return (RE.r_word0 >> 24) & 0xf;
396 return RE.Word1 >> 28;
397 return RE.Word1 & 0xf;
398 }
399
400 static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) {
401 return (RE.Word0 >> 24) & 0xf;
409402 }
410403
411404 static uint32_t getSectionFlags(const MachOObjectFile *O,
412405 DataRefImpl Sec) {
413406 if (O->is64Bit()) {
414 MachO::section_64 Sect = O->getSection64(Sec);
415 return Sect.flags;
416 }
417 MachO::section Sect = O->getSection(Sec);
418 return Sect.flags;
407 macho::Section64 Sect = O->getSection64(Sec);
408 return Sect.Flags;
409 }
410 macho::Section Sect = O->getSection(Sec);
411 return Sect.Flags;
419412 }
420413
421414 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
423416 error_code &ec)
424417 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
425418 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
426 uint32_t LoadCommandCount = this->getHeader().ncmds;
427 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
428 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
419 uint32_t LoadCommandCount = this->getHeader().NumLoadCommands;
420 macho::LoadCommandType SegmentLoadType = is64Bit() ?
421 macho::LCT_Segment64 : macho::LCT_Segment;
429422
430423 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
431424 for (unsigned I = 0; ; ++I) {
432 if (Load.C.cmd == MachO::LC_SYMTAB) {
425 if (Load.C.Type == macho::LCT_Symtab) {
433426 assert(!SymtabLoadCmd && "Multiple symbol tables");
434427 SymtabLoadCmd = Load.Ptr;
435 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
428 } else if (Load.C.Type == macho::LCT_Dysymtab) {
436429 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
437430 DysymtabLoadCmd = Load.Ptr;
438 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
431 } else if (Load.C.Type == macho::LCT_DataInCode) {
439432 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
440433 DataInCodeLoadCmd = Load.Ptr;
441 } else if (Load.C.cmd == SegmentLoadType) {
434 } else if (Load.C.Type == SegmentLoadType) {
442435 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
443436 for (unsigned J = 0; J < NumSections; ++J) {
444437 const char *Sec = getSectionPtr(this, Load, J);
456449 error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
457450 SymbolRef &Res) const {
458451 unsigned SymbolTableEntrySize = is64Bit() ?
459 sizeof(MachO::nlist_64) :
460 sizeof(MachO::nlist);
452 sizeof(macho::Symbol64TableEntry) :
453 sizeof(macho::SymbolTableEntry);
461454 Symb.p += SymbolTableEntrySize;
462455 Res = SymbolRef(Symb, this);
463456 return object_error::success;
466459 error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
467460 StringRef &Res) const {
468461 StringRef StringTable = getStringTableData();
469 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
470 const char *Start = &StringTable.data()[Entry.n_strx];
462 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
463 const char *Start = &StringTable.data()[Entry.StringIndex];
471464 Res = StringRef(Start);
472465 return object_error::success;
473466 }
475468 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
476469 uint64_t &Res) const {
477470 if (is64Bit()) {
478 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
479 Res = Entry.n_value;
471 macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb);
472 Res = Entry.Value;
480473 } else {
481 MachO::nlist Entry = getSymbolTableEntry(Symb);
482 Res = Entry.n_value;
474 macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb);
475 Res = Entry.Value;
483476 }
484477 return object_error::success;
485478 }
487480 error_code
488481 MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
489482 uint64_t &Res) const {
490 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
483 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
491484 getSymbolAddress(Symb, Res);
492 if (Entry.n_sect) {
485 if (Entry.SectionIndex) {
493486 uint64_t Delta;
494487 DataRefImpl SecRel;
495 SecRel.d.a = Entry.n_sect-1;
488 SecRel.d.a = Entry.SectionIndex-1;
496489 if (is64Bit()) {
497 MachO::section_64 Sec = getSection64(SecRel);
498 Delta = Sec.offset - Sec.addr;
490 macho::Section64 Sec = getSection64(SecRel);
491 Delta = Sec.Offset - Sec.Address;
499492 } else {
500 MachO::section Sec = getSection(SecRel);
501 Delta = Sec.offset - Sec.addr;
493 macho::Section Sec = getSection(SecRel);
494 Delta = Sec.Offset - Sec.Address;
502495 }
503496
504497 Res += Delta;
512505 uint32_t flags;
513506 this->getSymbolFlags(DRI, flags);
514507 if (flags & SymbolRef::SF_Common) {
515 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
516 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
508 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
509 Result = 1 << MachO::GET_COMM_ALIGN(Entry.Flags);
517510 } else {
518511 Result = 0;
519512 }
526519 uint64_t EndOffset = 0;
527520 uint8_t SectionIndex;
528521
529 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
522 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
530523 uint64_t Value;
531524 getSymbolAddress(DRI, Value);
532525
533526 BeginOffset = Value;
534527
535 SectionIndex = Entry.n_sect;
528 SectionIndex = Entry.SectionIndex;
536529 if (!SectionIndex) {
537530 uint32_t flags = SymbolRef::SF_None;
538531 this->getSymbolFlags(DRI, flags);
550543 DataRefImpl DRI = I->getRawDataRefImpl();
551544 Entry = getSymbolTableEntryBase(this, DRI);
552545 getSymbolAddress(DRI, Value);
553 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
546 if (Entry.SectionIndex == SectionIndex && Value > BeginOffset)
554547 if (!EndOffset || Value < EndOffset)
555548 EndOffset = Value;
556549 }
568561
569562 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
570563 SymbolRef::Type &Res) const {
571 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
572 uint8_t n_type = Entry.n_type;
564 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
565 uint8_t n_type = Entry.Type;
573566
574567 Res = SymbolRef::ST_Other;
575568
592585
593586 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
594587 char &Res) const {
595 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
596 uint8_t Type = Entry.n_type;
597 uint16_t Flags = Entry.n_desc;
588 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
589 uint8_t Type = Entry.Type;
590 uint16_t Flags = Entry.Flags;
598591
599592 char Char;
600 switch (Type & MachO::N_TYPE) {
601 case MachO::N_UNDF:
593 switch (Type & macho::STF_TypeMask) {
594 case macho::STT_Undefined:
602595 Char = 'u';
603596 break;
604 case MachO::N_ABS:
605 case MachO::N_SECT:
597 case macho::STT_Absolute:
598 case macho::STT_Section:
606599 Char = 's';
607600 break;
608601 default:
610603 break;
611604 }
612605
613 if (Flags & (MachO::N_EXT | MachO::N_PEXT))
606 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
614607 Char = toupper(static_cast(Char));
615608 Res = Char;
616609 return object_error::success;
618611
619612 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
620613 uint32_t &Result) const {
621 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
622
623 uint8_t MachOType = Entry.n_type;
624 uint16_t MachOFlags = Entry.n_desc;
614 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
615
616 uint8_t MachOType = Entry.Type;
617 uint16_t MachOFlags = Entry.Flags;
625618
626619 // TODO: Correctly set SF_ThreadLocal
627620 Result = SymbolRef::SF_None;
629622 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
630623 Result |= SymbolRef::SF_Undefined;
631624
632 if (MachOFlags & MachO::N_STAB)
625 if (MachOFlags & macho::STF_StabsEntryMask)
633626 Result |= SymbolRef::SF_FormatSpecific;
634627
635628 if (MachOType & MachO::N_EXT) {
654647 error_code
655648 MachOObjectFile::getSymbolSection(DataRefImpl Symb,
656649 section_iterator &Res) const {
657 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
658 uint8_t index = Entry.n_sect;
650 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
651 uint8_t index = Entry.SectionIndex;
659652
660653 if (index == 0) {
661654 Res = end_sections();
690683 error_code
691684 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
692685 if (is64Bit()) {
693 MachO::section_64 Sect = getSection64(Sec);
694 Res = Sect.addr;
686 macho::Section64 Sect = getSection64(Sec);
687 Res = Sect.Address;
695688 } else {
696 MachO::section Sect = getSection(Sec);
697 Res = Sect.addr;
689 macho::Section Sect = getSection(Sec);
690 Res = Sect.Address;
698691 }
699692 return object_error::success;
700693 }
702695 error_code
703696 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
704697 if (is64Bit()) {
705 MachO::section_64 Sect = getSection64(Sec);
706 Res = Sect.size;
698 macho::Section64 Sect = getSection64(Sec);
699 Res = Sect.Size;
707700 } else {
708 MachO::section Sect = getSection(Sec);
709 Res = Sect.size;
701 macho::Section Sect = getSection(Sec);
702 Res = Sect.Size;
710703 }
711704
712705 return object_error::success;
718711 uint64_t Size;
719712
720713 if (is64Bit()) {
721 MachO::section_64 Sect = getSection64(Sec);
722 Offset = Sect.offset;
723 Size = Sect.size;
714 macho::Section64 Sect = getSection64(Sec);
715 Offset = Sect.Offset;
716 Size = Sect.Size;
724717 } else {
725 MachO::section Sect = getSection(Sec);
726 Offset = Sect.offset;
727 Size = Sect.size;
718 macho::Section Sect =getSection(Sec);
719 Offset = Sect.Offset;
720 Size = Sect.Size;
728721 }
729722
730723 Res = this->getData().substr(Offset, Size);
735728 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
736729 uint32_t Align;
737730 if (is64Bit()) {
738 MachO::section_64 Sect = getSection64(Sec);
739 Align = Sect.align;
731 macho::Section64 Sect = getSection64(Sec);
732 Align = Sect.Align;
740733 } else {
741 MachO::section Sect = getSection(Sec);
742 Align = Sect.align;
734 macho::Section Sect = getSection(Sec);
735 Align = Sect.Align;
743736 }
744737
745738 Res = uint64_t(1) << Align;
749742 error_code
750743 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
751744 uint32_t Flags = getSectionFlags(this, Sec);
752 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
745 Res = Flags & macho::SF_PureInstructions;
753746 return object_error::success;
754747 }
755748
825818 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
826819 uint32_t Offset;
827820 if (is64Bit()) {
828 MachO::section_64 Sect = getSection64(Sec);
829 Offset = Sect.reloff;
821 macho::Section64 Sect = getSection64(Sec);
822 Offset = Sect.RelocationTableOffset;
830823 } else {
831 MachO::section Sect = getSection(Sec);
832 Offset = Sect.reloff;
824 macho::Section Sect = getSection(Sec);
825 Offset = Sect.RelocationTableOffset;
833826 }
834827
835828 DataRefImpl Ret;
842835 uint32_t Offset;
843836 uint32_t Num;
844837 if (is64Bit()) {
845 MachO::section_64 Sect = getSection64(Sec);
846 Offset = Sect.reloff;
847 Num = Sect.nreloc;
838 macho::Section64 Sect = getSection64(Sec);
839 Offset = Sect.RelocationTableOffset;
840 Num = Sect.NumRelocationTableEntries;
848841 } else {
849 MachO::section Sect = getSection(Sec);
850 Offset = Sect.reloff;
851 Num = Sect.nreloc;
852 }
853
854 const MachO::any_relocation_info *P =
855 reinterpret_cast(getPtr(this, Offset));
842 macho::Section Sect = getSection(Sec);
843 Offset = Sect.RelocationTableOffset;
844 Num = Sect.NumRelocationTableEntries;
845 }
846
847 const macho::RelocationEntry *P =
848 reinterpret_cast(getPtr(this, Offset));
856849
857850 DataRefImpl Ret;
858851 Ret.p = reinterpret_cast(P + Num);
861854
862855 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
863856 RelocationRef &Res) const {
864 const MachO::any_relocation_info *P =
865 reinterpret_cast(Rel.p);
857 const macho::RelocationEntry *P =
858 reinterpret_cast(Rel.p);
866859 Rel.p = reinterpret_cast(P + 1);
867860 Res = RelocationRef(Rel, this);
868861 return object_error::success;
875868
876869 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
877870 uint64_t &Res) const {
878 MachO::any_relocation_info RE = getRelocation(Rel);
871 macho::RelocationEntry RE = getRelocation(Rel);
879872 Res = getAnyRelocationAddress(RE);
880873 return object_error::success;
881874 }
882875
883876 symbol_iterator
884877 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
885 MachO::any_relocation_info RE = getRelocation(Rel);
878 macho::RelocationEntry RE = getRelocation(Rel);
886879 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
887880 bool isExtern = getPlainRelocationExternal(RE);
888881 if (!isExtern)
889882 return end_symbols();
890883
891 MachO::symtab_command S = getSymtabLoadCommand();
884 macho::SymtabLoadCommand S = getSymtabLoadCommand();
892885 unsigned SymbolTableEntrySize = is64Bit() ?
893 sizeof(MachO::nlist_64) :
894 sizeof(MachO::nlist);
895 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
886 sizeof(macho::Symbol64TableEntry) :
887 sizeof(macho::SymbolTableEntry);
888 uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize;
896889 DataRefImpl Sym;
897890 Sym.p = reinterpret_cast(getPtr(this, Offset));
898891 return symbol_iterator(SymbolRef(Sym, this));
900893
901894 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
902895 uint64_t &Res) const {
903 MachO::any_relocation_info RE = getRelocation(Rel);
896 macho::RelocationEntry RE = getRelocation(Rel);
904897 Res = getAnyRelocationType(RE);
905898 return object_error::success;
906899 }
1001994 error_code
1002995 MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1003996 SmallVectorImpl &Result) const {
1004 MachO::any_relocation_info RE = getRelocation(Rel);
997 macho::RelocationEntry RE = getRelocation(Rel);
1005998
1006999 unsigned Arch = this->getArch();
10071000
10181011 bool isPCRel = getAnyRelocationPCRel(RE);
10191012
10201013 switch (Type) {
1021 case MachO::X86_64_RELOC_GOT_LOAD:
1022 case MachO::X86_64_RELOC_GOT: {
1014 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1015 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
10231016 printRelocationTargetName(this, RE, fmt);
10241017 fmt << "@GOT";
10251018 if (isPCRel) fmt << "PCREL";
10261019 break;
10271020 }
1028 case MachO::X86_64_RELOC_SUBTRACTOR: {
1021 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
10291022 DataRefImpl RelNext = Rel;
10301023 RelNext.d.a++;
1031 MachO::any_relocation_info RENext = getRelocation(RelNext);
1032
1033 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
1024 macho::RelocationEntry RENext = getRelocation(RelNext);
1025
1026 // X86_64_SUBTRACTOR must be followed by a relocation of type
10341027 // X86_64_RELOC_UNSIGNED.
10351028 // NOTE: Scattered relocations don't exist on x86_64.
10361029 unsigned RType = getAnyRelocationType(RENext);
1037 if (RType != MachO::X86_64_RELOC_UNSIGNED)
1030 if (RType != 0)
10381031 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
10391032 "X86_64_RELOC_SUBTRACTOR.");
10401033
1041 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1042 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
1034 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1035 // X86_64_SUBTRACTOR contains to the subtrahend.
10431036 printRelocationTargetName(this, RENext, fmt);
10441037 fmt << "-";
10451038 printRelocationTargetName(this, RE, fmt);
10461039 break;
10471040 }
1048 case MachO::X86_64_RELOC_TLV:
1041 case macho::RIT_X86_64_TLV:
10491042 printRelocationTargetName(this, RE, fmt);
10501043 fmt << "@TLV";
10511044 if (isPCRel) fmt << "P";
10521045 break;
1053 case MachO::X86_64_RELOC_SIGNED_1:
1046 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
10541047 printRelocationTargetName(this, RE, fmt);
10551048 fmt << "-1";
10561049 break;
1057 case MachO::X86_64_RELOC_SIGNED_2:
1050 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
10581051 printRelocationTargetName(this, RE, fmt);
10591052 fmt << "-2";
10601053 break;
1061 case MachO::X86_64_RELOC_SIGNED_4:
1054 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
10621055 printRelocationTargetName(this, RE, fmt);
10631056 fmt << "-4";
10641057 break;
10711064 Arch == Triple::ppc) {
10721065 // Generic relocation types...
10731066 switch (Type) {
1074 case MachO::GENERIC_RELOC_PAIR: // prints no info
1067 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
10751068 return object_error::success;
1076 case MachO::GENERIC_RELOC_SECTDIFF: {
1069 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
10771070 DataRefImpl RelNext = Rel;
10781071 RelNext.d.a++;
1079 MachO::any_relocation_info RENext = getRelocation(RelNext);
1072 macho::RelocationEntry RENext = getRelocation(RelNext);
10801073
10811074 // X86 sect diff's must be followed by a relocation of type
10821075 // GENERIC_RELOC_PAIR.
10831076 unsigned RType = getAnyRelocationType(RENext);
10841077
1085 if (RType != MachO::GENERIC_RELOC_PAIR)
1078 if (RType != 1)
10861079 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
10871080 "GENERIC_RELOC_SECTDIFF.");
10881081
10941087 }
10951088
10961089 if (Arch == Triple::x86 || Arch == Triple::ppc) {
1090 // All X86 relocations that need special printing were already
1091 // handled in the generic code.
10971092 switch (Type) {
1098 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
1093 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
10991094 DataRefImpl RelNext = Rel;
11001095 RelNext.d.a++;
1101 MachO::any_relocation_info RENext = getRelocation(RelNext);
1096 macho::RelocationEntry RENext = getRelocation(RelNext);
11021097
11031098 // X86 sect diff's must be followed by a relocation of type
11041099 // GENERIC_RELOC_PAIR.
11051100 unsigned RType = getAnyRelocationType(RENext);
1106 if (RType != MachO::GENERIC_RELOC_PAIR)
1101 if (RType != 1)
11071102 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
11081103 "GENERIC_RELOC_LOCAL_SECTDIFF.");
11091104
11121107 printRelocationTargetName(this, RENext, fmt);
11131108 break;
11141109 }
1115 case MachO::GENERIC_RELOC_TLV: {
1110 case macho::RIT_Generic_TLV: {
11161111 printRelocationTargetName(this, RE, fmt);
11171112 fmt << "@TLV";
11181113 if (IsPCRel) fmt << "P";
11231118 }
11241119 } else { // ARM-specific relocations
11251120 switch (Type) {
1126 case MachO::ARM_RELOC_HALF:
1127 case MachO::ARM_RELOC_HALF_SECTDIFF: {
1121 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1122 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
11281123 // Half relocations steal a bit from the length field to encode
11291124 // whether this is an upper16 or a lower16 relocation.
11301125 bool isUpper = getAnyRelocationLength(RE) >> 1;
11371132
11381133 DataRefImpl RelNext = Rel;
11391134 RelNext.d.a++;
1140 MachO::any_relocation_info RENext = getRelocation(RelNext);
1135 macho::RelocationEntry RENext = getRelocation(RelNext);
11411136
11421137 // ARM half relocs must be followed by a relocation of type
11431138 // ARM_RELOC_PAIR.
11441139 unsigned RType = getAnyRelocationType(RENext);
1145 if (RType != MachO::ARM_RELOC_PAIR)
1140 if (RType != 1)
11461141 report_fatal_error("Expected ARM_RELOC_PAIR after "
1147 "ARM_RELOC_HALF");
1142 "GENERIC_RELOC_HALF");
11481143
11491144 // NOTE: The half of the target virtual address is stashed in the
11501145 // address field of the secondary relocation, but we can't reverse
11531148
11541149 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
11551150 // symbol/section pointer of the follow-on relocation.
1156 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1151 if (Type == macho::RIT_ARM_HalfDifference) {
11571152 fmt << "-";
11581153 printRelocationTargetName(this, RENext, fmt);
11591154 }
11851180 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
11861181 // is always hidden.
11871182 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
1188 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
1183 if (Type == macho::RIT_Pair) Result = true;
11891184 } else if (Arch == Triple::x86_64) {
11901185 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
11911186 // an X86_64_RELOC_SUBTRACTOR.
1192 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
1187 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
11931188 DataRefImpl RelPrev = Rel;
11941189 RelPrev.d.a--;
11951190 uint64_t PrevType;
11961191 getRelocationType(RelPrev, PrevType);
1197 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
1192 if (PrevType == macho::RIT_X86_64_Subtractor)
11981193 Result = true;
11991194 }
12001195 }
12171212 if (!SymtabLoadCmd)
12181213 return symbol_iterator(SymbolRef(DRI, this));
12191214
1220 MachO::symtab_command Symtab = getSymtabLoadCommand();
1221 DRI.p = reinterpret_cast(getPtr(this, Symtab.symoff));
1215 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1216 DRI.p = reinterpret_cast(getPtr(this, Symtab.SymbolTableOffset));
12221217 return symbol_iterator(SymbolRef(DRI, this));
12231218 }
12241219
12271222 if (!SymtabLoadCmd)
12281223 return symbol_iterator(SymbolRef(DRI, this));
12291224
1230 MachO::symtab_command Symtab = getSymtabLoadCommand();
1225 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
12311226 unsigned SymbolTableEntrySize = is64Bit() ?
1232 sizeof(MachO::nlist_64) :
1233 sizeof(MachO::nlist);
1234 unsigned Offset = Symtab.symoff +
1235 Symtab.nsyms * SymbolTableEntrySize;
1227 sizeof(macho::Symbol64TableEntry) :
1228 sizeof(macho::SymbolTableEntry);
1229 unsigned Offset = Symtab.SymbolTableOffset +
1230 Symtab.NumSymbolTableEntries * SymbolTableEntrySize;
12361231 DRI.p = reinterpret_cast(getPtr(this, Offset));
12371232 return symbol_iterator(SymbolRef(DRI, this));
12381233 }
13271322
13281323 StringRef MachOObjectFile::getLoadName() const {
13291324 // TODO: Implement
1330 report_fatal_error("Load name unimplemented in MachOObjectFile");
1325 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
13311326 }
13321327
13331328 relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const {
13471342 if (!DataInCodeLoadCmd)
13481343 return dice_iterator(DiceRef(DRI, this));
13491344
1350 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1351 DRI.p = reinterpret_cast(getPtr(this, DicLC.dataoff));
1345 macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand();
1346 DRI.p = reinterpret_cast(getPtr(this, DicLC.DataOffset));
13521347 return dice_iterator(DiceRef(DRI, this));
13531348 }
13541349
13571352 if (!DataInCodeLoadCmd)
13581353 return dice_iterator(DiceRef(DRI, this));
13591354
1360 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1361 unsigned Offset = DicLC.dataoff + DicLC.datasize;
1355 macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand();
1356 unsigned Offset = DicLC.DataOffset + DicLC.DataSize;
13621357 DRI.p = reinterpret_cast(getPtr(this, Offset));
13631358 return dice_iterator(DiceRef(DRI, this));
13641359 }
13711366
13721367 ArrayRef
13731368 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1374 const section_base *Base =
1375 reinterpret_cast(Sections[Sec.d.a]);
1376 return ArrayRef(Base->sectname);
1369 const SectionBase *Base =
1370 reinterpret_cast(Sections[Sec.d.a]);
1371 return ArrayRef(Base->Name);
13771372 }
13781373
13791374 ArrayRef
13801375 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1381 const section_base *Base =
1382 reinterpret_cast(Sections[Sec.d.a]);
1383 return ArrayRef(Base->segname);
1376 const SectionBase *Base =
1377 reinterpret_cast(Sections[Sec.d.a]);
1378 return ArrayRef(Base->SegmentName);
13841379 }
13851380
13861381 bool
1387 MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
1382 MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE)
13881383 const {
1389 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
1384 if (getCPUType(this) == llvm::MachO::CPU_TYPE_X86_64)
13901385 return false;
1391 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
1386 return getPlainRelocationAddress(RE) & macho::RF_Scattered;
13921387 }
13931388
13941389 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
1395 const MachO::any_relocation_info &RE) const {
1390 const macho::RelocationEntry &RE) const {
13961391 if (isLittleEndian())
1397 return RE.r_word1 & 0xffffff;
1398 return RE.r_word1 >> 8;
1392 return RE.Word1 & 0xffffff;
1393 return RE.Word1 >> 8;
13991394 }
14001395
14011396 bool MachOObjectFile::getPlainRelocationExternal(
1402 const MachO::any_relocation_info &RE) const {
1397 const macho::RelocationEntry &RE) const {
14031398 if (isLittleEndian())
1404 return (RE.r_word1 >> 27) & 1;
1405 return (RE.r_word1 >> 4) & 1;
1399 return (RE.Word1 >> 27) & 1;
1400 return (RE.Word1 >> 4) & 1;
14061401 }
14071402
14081403 bool MachOObjectFile::getScatteredRelocationScattered(
1409 const MachO::any_relocation_info &RE) const {
1410 return RE.r_word0 >> 31;
1404 const macho::RelocationEntry &RE) const {
1405 return RE.Word0 >> 31;
14111406 }
14121407
14131408 uint32_t MachOObjectFile::getScatteredRelocationValue(
1414 const MachO::any_relocation_info &RE) const {
1415 return RE.r_word1;
1409 const macho::RelocationEntry &RE) const {
1410 return RE.Word1;
14161411 }
14171412
14181413 unsigned MachOObjectFile::getAnyRelocationAddress(
1419 const MachO::any_relocation_info &RE) const {
1414 const macho::RelocationEntry &RE) const {
14201415 if (isRelocationScattered(RE))
14211416 return getScatteredRelocationAddress(RE);
14221417 return getPlainRelocationAddress(RE);
14231418 }
14241419
1425 unsigned MachOObjectFile::getAnyRelocationPCRel(
1426 const MachO::any_relocation_info &RE) const {
1420 unsigned
1421 MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const {
14271422 if (isRelocationScattered(RE))
14281423 return getScatteredRelocationPCRel(this, RE);
14291424 return getPlainRelocationPCRel(this, RE);
14301425 }
14311426
14321427 unsigned MachOObjectFile::getAnyRelocationLength(
1433 const MachO::any_relocation_info &RE) const {
1428 const macho::RelocationEntry &RE) const {
14341429 if (isRelocationScattered(RE))
14351430 return getScatteredRelocationLength(RE);
14361431 return getPlainRelocationLength(this, RE);
14371432 }
14381433
14391434 unsigned
1440 MachOObjectFile::getAnyRelocationType(
1441 const MachO::any_relocation_info &RE) const {
1435 MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const {
14421436 if (isRelocationScattered(RE))
14431437 return getScatteredRelocationType(RE);
14441438 return getPlainRelocationType(this, RE);
14451439 }
14461440
14471441 SectionRef
1448 MachOObjectFile::getRelocationSection(
1449 const MachO::any_relocation_info &RE) const {
1442 MachOObjectFile::getRelocationSection(const macho::RelocationEntry &RE) const {
14501443 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
14511444 return *end_sections();
14521445 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
14591452 MachOObjectFile::getFirstLoadCommandInfo() const {
14601453 MachOObjectFile::LoadCommandInfo Load;
14611454
1462 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1463 sizeof(MachO::mach_header);
1455 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
14641456 Load.Ptr = getPtr(this, HeaderSize);
1465 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
1457 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
14661458 return Load;
14671459 }
14681460
14691461 MachOObjectFile::LoadCommandInfo
14701462 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
14711463 MachOObjectFile::LoadCommandInfo Next;
1472 Next.Ptr = L.Ptr + L.C.cmdsize;
1473 Next.C = getStruct(this, Next.Ptr);
1464 Next.Ptr = L.Ptr + L.C.Size;
1465 Next.C = getStruct(this, Next.Ptr);
14741466 return Next;
14751467 }
14761468
1477 MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1478 return getStruct(this, Sections[DRI.d.a]);
1479 }
1480
1481 MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1482 return getStruct(this, Sections[DRI.d.a]);
1483 }
1484
1485 MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
1469 macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const {
1470 return getStruct(this, Sections[DRI.d.a]);
1471 }
1472
1473 macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1474 return getStruct(this, Sections[DRI.d.a]);
1475 }
1476
1477 macho::Section MachOObjectFile::getSection(const LoadCommandInfo &L,
14861478 unsigned Index) const {
14871479 const char *Sec = getSectionPtr(this, L, Index);
1488 return getStruct(this, Sec);
1489 }
1490
1491 MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1492 unsigned Index) const {
1480 return getStruct(this, Sec);
1481 }
1482
1483 macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1484 unsigned Index) const {
14931485 const char *Sec = getSectionPtr(this, L, Index);
1494 return getStruct(this, Sec);
1495 }
1496
1497 MachO::nlist
1486 return getStruct(this, Sec);
1487 }
1488
1489 macho::SymbolTableEntry
14981490 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
14991491 const char *P = reinterpret_cast(DRI.p);
1500 return getStruct(this, P);
1501 }
1502
1503 MachO::nlist_64
1492 return getStruct(this, P);
1493 }
1494
1495 macho::Symbol64TableEntry
15041496 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
15051497 const char *P = reinterpret_cast(DRI.p);
1506 return getStruct(this, P);
1507 }
1508
1509 MachO::linkedit_data_command
1510 MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1511 return getStruct(this, L.Ptr);
1512 }
1513
1514 MachO::segment_command
1498 return getStruct(this, P);
1499 }
1500
1501 macho::LinkeditDataLoadCommand MachOObjectFile::getLinkeditDataLoadCommand(
1502 const MachOObjectFile::LoadCommandInfo &L) const {
1503 return getStruct(this, L.Ptr);
1504 }
1505
1506 macho::SegmentLoadCommand
15151507 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1516 return getStruct(this, L.Ptr);
1517 }
1518
1519 MachO::segment_command_64
1508 return getStruct(this, L.Ptr);
1509 }
1510
1511 macho::Segment64LoadCommand
15201512 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1521 return getStruct(this, L.Ptr);
1522 }
1523
1524 MachO::linker_options_command
1513 return getStruct(this, L.Ptr);
1514 }
1515
1516 macho::LinkerOptionsLoadCommand
15251517 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1526 return getStruct(this, L.Ptr);
1527 }
1528
1529 MachO::any_relocation_info
1518 return getStruct(this, L.Ptr);
1519 }
1520
1521 macho::RelocationEntry
15301522 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
15311523 const char *P = reinterpret_cast(Rel.p);
1532 return getStruct(this, P);
1533 }
1534
1535 MachO::data_in_code_entry
1524 return getStruct(this, P);
1525 }
1526
1527 macho::DataInCodeTableEntry
15361528 MachOObjectFile::getDice(DataRefImpl Rel) const {
15371529 const char *P = reinterpret_cast(Rel.p);
1538 return getStruct(this, P);
1539 }
1540
1541 MachO::mach_header MachOObjectFile::getHeader() const {
1542 return getStruct(this, getPtr(this, 0));
1543 }
1544
1545 MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1546 return getStruct(this, getPtr(this, 0));
1547 }
1548
1549 uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1550 const MachO::dysymtab_command &DLC,
1551 unsigned Index) const {
1552 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1553 return getStruct(this, getPtr(this, Offset));
1554 }
1555
1556 MachO::data_in_code_entry
1530 return getStruct(this, P);
1531 }
1532
1533 macho::Header MachOObjectFile::getHeader() const {
1534 return getStruct(this, getPtr(this, 0));
1535 }
1536
1537 macho::Header64Ext MachOObjectFile::getHeader64Ext() const {
1538 return
1539 getStruct(this, getPtr(this, sizeof(macho::Header)));
1540 }
1541
1542 macho::IndirectSymbolTableEntry MachOObjectFile::getIndirectSymbolTableEntry(
1543 const macho::DysymtabLoadCommand &DLC,
1544 unsigned Index) const {
1545 uint64_t Offset = DLC.IndirectSymbolTableOffset +
1546 Index * sizeof(macho::IndirectSymbolTableEntry);
1547 return getStruct(this, getPtr(this, Offset));
1548 }
1549
1550 macho::DataInCodeTableEntry
15571551 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
15581552 unsigned Index) const {
1559 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1560 return getStruct(this, getPtr(this, Offset));
1561 }
1562
1563 MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1564 return getStruct(this, SymtabLoadCmd);
1565 }
1566
1567 MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1568 return getStruct(this, DysymtabLoadCmd);
1569 }
1570
1571 MachO::linkedit_data_command
1553 uint64_t Offset = DataOffset + Index * sizeof(macho::DataInCodeTableEntry);
1554 return getStruct(this, getPtr(this, Offset));
1555 }
1556
1557 macho::SymtabLoadCommand MachOObjectFile::getSymtabLoadCommand() const {
1558 return getStruct(this, SymtabLoadCmd);
1559 }
1560
1561 macho::DysymtabLoadCommand MachOObjectFile::getDysymtabLoadCommand() const {
1562 return getStruct(this, DysymtabLoadCmd);
1563 }
1564
1565 macho::LinkeditDataLoadCommand
15721566 MachOObjectFile::getDataInCodeLoadCommand() const {
15731567 if (DataInCodeLoadCmd)
1574 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
1568 return getStruct<macho::LinkeditDataLoadCommand>(this, DataInCodeLoadCmd);
15751569
15761570 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
1577 MachO::linkedit_data_command Cmd;
1578 Cmd.cmd = MachO::LC_DATA_IN_CODE;
1579 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1580 Cmd.dataoff = 0;
1581 Cmd.datasize = 0;
1571 macho::LinkeditDataLoadCommand Cmd;
1572 Cmd.Type = macho::LCT_DataInCode;
1573 Cmd.Size = macho::LinkeditLoadCommandSize;
1574 Cmd.DataOffset = 0;
1575 Cmd.DataSize = 0;
15821576 return Cmd;
15831577 }
15841578
15851579 StringRef MachOObjectFile::getStringTableData() const {
1586 MachO::symtab_command S = getSymtabLoadCommand();
1587 return getData().substr(S.stroff, S.strsize);
1580 macho::SymtabLoadCommand S = getSymtabLoadCommand();
1581 return getData().substr(S.StringTableOffset, S.StringTableSize);
15881582 }
15891583
15901584 bool MachOObjectFile::is64Bit() const {
3030 static void SwapStruct(T &Value);
3131
3232 template<>
33 void SwapStruct(MachO::fat_header &H) {
34 SwapValue(H.magic);
35 SwapValue(H.nfat_arch);
33 void SwapStruct(macho::FatHeader &H) {
34 SwapValue(H.Magic);
35 SwapValue(H.NumFatArch);
3636 }
3737
3838 template<>
39 void SwapStruct(MachO::fat_arch &H) {
40 SwapValue(H.cputype);
41 SwapValue(H.cpusubtype);
42 SwapValue(H.offset);
43 SwapValue(H.size);
44 SwapValue(H.align);
39 void SwapStruct(macho::FatArchHeader &H) {
40 SwapValue(H.CPUType);
41 SwapValue(H.CPUSubtype);
42 SwapValue(H.Offset);
43 SwapValue(H.Size);
44 SwapValue(H.Align);
4545 }
4646
4747 template
6262 } else {
6363 // Parse object header.
6464 StringRef ParentData = Parent->getData();
65 const char *HeaderPos = ParentData.begin() + sizeof(MachO::fat_header) +
66 Index * sizeof(MachO::fat_arch);
67 Header = getUniversalBinaryStruct(HeaderPos);
68 if (ParentData.size() < Header.offset + Header.size) {
65 const char *HeaderPos = ParentData.begin() + macho::FatHeaderSize +
66 Index * macho::FatArchHeaderSize;
67 Header = getUniversalBinaryStruct(HeaderPos);
68 if (ParentData.size() < Header.Offset + Header.Size) {
6969 clear();
7070 }
7171 }
7575 OwningPtr &Result) const {
7676 if (Parent) {
7777 StringRef ParentData = Parent->getData();
78 StringRef ObjectData = ParentData.substr(Header.offset, Header.size);
78 StringRef ObjectData = ParentData.substr(Header.Offset, Header.Size);
7979 std::string ObjectName =
8080 Parent->getFileName().str() + ":" +
81 Triple::getArchTypeName(MachOObjectFile::getArch(Header.cputype));
81 Triple::getArchTypeName(MachOObjectFile::getArch(Header.CPUType));
8282 MemoryBuffer *ObjBuffer = MemoryBuffer::getMemBuffer(
8383 ObjectData, ObjectName, false);
8484 if (ObjectFile *Obj = ObjectFile::createMachOObjectFile(ObjBuffer)) {
9595 error_code &ec)
9696 : Binary(Binary::ID_MachOUniversalBinary, Source),
9797 NumberOfObjects(0) {
98 if (Source->getBufferSize() < sizeof(MachO::fat_header)) {
98 if (Source->getBufferSize() < macho::FatHeaderSize) {
9999 ec = object_error::invalid_file_type;
100100 return;
101101 }
102102 // Check for magic value and sufficient header size.
103103 StringRef Buf = getData();
104 MachO::fat_header H= getUniversalBinaryStruct(Buf.begin());
105 NumberOfObjects = H.nfat_arch;
106 uint32_t MinSize = sizeof(MachO::fat_header) +
107 sizeof(MachO::fat_arch) * NumberOfObjects;
108 if (H.magic != MachO::FAT_MAGIC || Buf.size() < MinSize) {
104 macho::FatHeader H = getUniversalBinaryStruct(Buf.begin());
105 NumberOfObjects = H.NumFatArch;
106 uint32_t MinSize = macho::FatHeaderSize +
107 macho::FatArchHeaderSize * NumberOfObjects;
108 if (H.Magic != macho::HM_Universal || Buf.size() < MinSize) {
109109 ec = object_error::parse_failed;
110110 return;
111111 }
112112 ec = object_error::success;
113113 }
114114
115 static bool getCTMForArch(Triple::ArchType Arch, MachO::CPUType &CTM) {
115 static bool getCTMForArch(Triple::ArchType Arch, mach::CPUTypeMachine &CTM) {
116116 switch (Arch) {
117 case Triple::x86: CTM = MachO::CPU_TYPE_I386; return true;
118 case Triple::x86_64: CTM = MachO::CPU_TYPE_X86_64; return true;
119 case Triple::arm: CTM = MachO::CPU_TYPE_ARM; return true;
120 case Triple::sparc: CTM = MachO::CPU_TYPE_SPARC; return true;
121 case Triple::ppc: CTM = MachO::CPU_TYPE_POWERPC; return true;
122 case Triple::ppc64: CTM = MachO::CPU_TYPE_POWERPC64; return true;
117 case Triple::x86: CTM = mach::CTM_i386; return true;
118 case Triple::x86_64: CTM = mach::CTM_x86_64; return true;
119 case Triple::arm: CTM = mach::CTM_ARM; return true;
120 case Triple::sparc: CTM = mach::CTM_SPARC; return true;
121 case Triple::ppc: CTM = mach::CTM_PowerPC; return true;
122 case Triple::ppc64: CTM = mach::CTM_PowerPC64; return true;
123123 default: return false;
124124 }
125125 }
127127 error_code
128128 MachOUniversalBinary::getObjectForArch(Triple::ArchType Arch,
129129 OwningPtr &Result) const {
130 MachO::CPUType CTM;
130 mach::CPUTypeMachine CTM;
131131 if (!getCTMForArch(Arch, CTM))
132132 return object_error::arch_not_found;
133133 for (object_iterator I = begin_objects(), E = end_objects(); I != E; ++I) {
2424 #include "llvm/MC/MCSectionMachO.h"
2525 #include "llvm/MC/MCSubtargetInfo.h"
2626 #include "llvm/MC/MCValue.h"
27 #include "llvm/Object/MachOFormat.h"
2728 #include "llvm/Support/ELF.h"
2829 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/MachO.h"
3030 #include "llvm/Support/raw_ostream.h"
3131 using namespace llvm;
3232
639639 // FIXME: This should be in a separate file.
640640 class DarwinARMAsmBackend : public ARMAsmBackend {
641641 public:
642 const MachO::CPUSubTypeARM Subtype;
642 const object::mach::CPUSubtypeARM Subtype;
643643 DarwinARMAsmBackend(const Target &T, const StringRef TT,
644 MachO::CPUSubTypeARM st)
644 object::mach::CPUSubtypeARM st)
645645 : ARMAsmBackend(T, TT), Subtype(st) {
646646 HasDataInCodeSupport = true;
647647 }
648648
649649 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
650650 return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
651 MachO::CPU_TYPE_ARM,
651 object::mach::CTM_ARM,
652652 Subtype);
653653 }
654654
663663 Triple TheTriple(TT);
664664
665665 if (TheTriple.isOSDarwin()) {
666 MachO::CPUSubTypeARM CS =
667 StringSwitch(TheTriple.getArchName())
668 .Cases("armv4t", "thumbv4t", MachO::CPU_SUBTYPE_ARM_V4T)
669 .Cases("armv5e", "thumbv5e", MachO::CPU_SUBTYPE_ARM_V5TEJ)
670 .Cases("armv6", "thumbv6", MachO::CPU_SUBTYPE_ARM_V6)
671 .Cases("armv6m", "thumbv6m", MachO::CPU_SUBTYPE_ARM_V6M)
672 .Cases("armv7em", "thumbv7em", MachO::CPU_SUBTYPE_ARM_V7EM)
673 .Cases("armv7f", "thumbv7f", MachO::CPU_SUBTYPE_ARM_V7F)
674 .Cases("armv7k", "thumbv7k", MachO::CPU_SUBTYPE_ARM_V7K)
675 .Cases("armv7m", "thumbv7m", MachO::CPU_SUBTYPE_ARM_V7M)
676 .Cases("armv7s", "thumbv7s", MachO::CPU_SUBTYPE_ARM_V7S)
677 .Default(MachO::CPU_SUBTYPE_ARM_V7);
666 object::mach::CPUSubtypeARM CS =
667 StringSwitch(TheTriple.getArchName())
668 .Cases("armv4t", "thumbv4t", object::mach::CSARM_V4T)
669 .Cases("armv5e", "thumbv5e",object::mach::CSARM_V5TEJ)
670 .Cases("armv6", "thumbv6", object::mach::CSARM_V6)
671 .Cases("armv6m", "thumbv6m", object::mach::CSARM_V6M)
672 .Cases("armv7em", "thumbv7em", object::mach::CSARM_V7EM)
673 .Cases("armv7f", "thumbv7f", object::mach::CSARM_V7F)
674 .Cases("armv7k", "thumbv7k", object::mach::CSARM_V7K)
675 .Cases("armv7m", "thumbv7m", object::mach::CSARM_V7M)
676 .Cases("armv7s", "thumbv7s", object::mach::CSARM_V7S)
677 .Default(object::mach::CSARM_V7);
678678
679679 return new DarwinARMAsmBackend(T, TT, CS);
680680 }
1919 #include "llvm/MC/MCMachOSymbolFlags.h"
2020 #include "llvm/MC/MCMachObjectWriter.h"
2121 #include "llvm/MC/MCValue.h"
22 #include "llvm/Object/MachOFormat.h"
2223 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MachO.h"
2424 using namespace llvm;
25 using namespace llvm::object;
2526
2627 namespace {
2728 class ARMMachObjectWriter : public MCMachObjectTargetWriter {
6162
6263 static bool getARMFixupKindMachOInfo(unsigned Kind, unsigned &RelocType,
6364 unsigned &Log2Size) {
64 RelocType = unsigned(MachO::ARM_RELOC_VANILLA);
65 RelocType = unsigned(macho::RIT_Vanilla);
6566 Log2Size = ~0U;
6667
6768 switch (Kind) {
9091 case ARM::fixup_arm_uncondbl:
9192 case ARM::fixup_arm_condbl:
9293 case ARM::fixup_arm_blx:
93 RelocType = unsigned(MachO::ARM_RELOC_BR24);
94 RelocType = unsigned(macho::RIT_ARM_Branch24Bit);
9495 // Report as 'long', even though that is not quite accurate.
9596 Log2Size = llvm::Log2_32(4);
9697 return true;
9798
9899 // Handle Thumb branches.
99100 case ARM::fixup_arm_thumb_br:
100 RelocType = unsigned(MachO::ARM_THUMB_RELOC_BR22);
101 RelocType = unsigned(macho::RIT_ARM_ThumbBranch22Bit);
101102 Log2Size = llvm::Log2_32(2);
102103 return true;
103104
104105 case ARM::fixup_t2_uncondbranch:
105106 case ARM::fixup_arm_thumb_bl:
106107 case ARM::fixup_arm_thumb_blx:
107 RelocType = unsigned(MachO::ARM_THUMB_RELOC_BR22);
108 RelocType = unsigned(macho::RIT_ARM_ThumbBranch22Bit);
108109 Log2Size = llvm::Log2_32(4);
109110 return true;
110111
119120 // 1 - thumb instructions
120121 case ARM::fixup_arm_movt_hi16:
121122 case ARM::fixup_arm_movt_hi16_pcrel:
122 RelocType = unsigned(MachO::ARM_RELOC_HALF);
123 RelocType = unsigned(macho::RIT_ARM_Half);
123124 Log2Size = 1;
124125 return true;
125126 case ARM::fixup_t2_movt_hi16:
126127 case ARM::fixup_t2_movt_hi16_pcrel:
127 RelocType = unsigned(MachO::ARM_RELOC_HALF);
128 RelocType = unsigned(macho::RIT_ARM_Half);
128129 Log2Size = 3;
129130 return true;
130131
131132 case ARM::fixup_arm_movw_lo16:
132133 case ARM::fixup_arm_movw_lo16_pcrel:
133 RelocType = unsigned(MachO::ARM_RELOC_HALF);
134 RelocType = unsigned(macho::RIT_ARM_Half);
134135 Log2Size = 0;
135136 return true;
136137 case ARM::fixup_t2_movw_lo16:
137138 case ARM::fixup_t2_movw_lo16_pcrel:
138 RelocType = unsigned(MachO::ARM_RELOC_HALF);
139 RelocType = unsigned(macho::RIT_ARM_Half);
139140 Log2Size = 2;
140141 return true;
141142 }
151152 uint64_t &FixedValue) {
152153 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
153154 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
154 unsigned Type = MachO::ARM_RELOC_HALF;
155 unsigned Type = macho::RIT_ARM_Half;
155156
156157 // See .
157158 const MCSymbol *A = &Target.getSymA()->getSymbol();
177178 "' can not be undefined in a subtraction expression");
178179
179180 // Select the appropriate difference relocation type.
180 Type = MachO::ARM_RELOC_HALF_SECTDIFF;
181 Type = macho::RIT_ARM_HalfDifference;
181182 Value2 = Writer->getSymbolAddress(B_SD, Layout);
182183 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
183184 }
221222 break;
222223 }
223224
224 MachO::scattered_relocation_info MRE;
225 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
225 if (Type == macho::RIT_ARM_HalfDifference) {
226226 uint32_t OtherHalf = MovtBit
227227 ? (FixedValue & 0xffff) : ((FixedValue & 0xffff0000) >> 16);
228228
229 MRE.r_address = OtherHalf;
230 MRE.r_type = MachO::ARM_RELOC_PAIR;
231 MRE.r_length = ((MovtBit << 0) |
232 (ThumbBit << 1));
233 MRE.r_pcrel = IsPCRel;
234 MRE.r_scattered = 1;
235 MRE.r_value = Value2;
229 macho::RelocationEntry MRE;
230 MRE.Word0 = ((OtherHalf << 0) |
231 (macho::RIT_Pair << 24) |
232 (MovtBit << 28) |
233 (ThumbBit << 29) |
234 (IsPCRel << 30) |
235 macho::RF_Scattered);
236 MRE.Word1 = Value2;
236237 Writer->addRelocation(Fragment->getParent(), MRE);
237238 }
238239
239 MRE.r_address = FixupOffset;
240 MRE.r_type = Type;
241 MRE.r_length = ((MovtBit << 0) |
242 (ThumbBit << 1));
243 MRE.r_pcrel = IsPCRel;
244 MRE.r_scattered = 1;
245 MRE.r_value = Value;
240 macho::RelocationEntry MRE;
241 MRE.Word0 = ((FixupOffset << 0) |
242 (Type << 24) |
243 (MovtBit << 28) |
244 (ThumbBit << 29) |
245 (IsPCRel << 30) |
246 macho::RF_Scattered);
247 MRE.Word1 = Value;
246248 Writer->addRelocation(Fragment->getParent(), MRE);
247249 }
248250
256258 uint64_t &FixedValue) {
257259 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
258260 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
259 unsigned Type = MachO::ARM_RELOC_VANILLA;
261 unsigned Type = macho::RIT_Vanilla;
260262
261263 // See .
262264 const MCSymbol *A = &Target.getSymA()->getSymbol();
281283 "' can not be undefined in a subtraction expression");
282284
283285 // Select the appropriate difference relocation type.
284 Type = MachO::ARM_RELOC_SECTDIFF;
286 Type = macho::RIT_Difference;
285287 Value2 = Writer->getSymbolAddress(B_SD, Layout);
286288 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
287289 }
288290
289 MachO::scattered_relocation_info MRE;
290291 // Relocations are written out in reverse order, so the PAIR comes first.
291 if (Type == MachO::ARM_RELOC_SECTDIFF ||
292 Type == MachO::ARM_RELOC_LOCAL_SECTDIFF) {
293 MRE.r_address = 0;
294 MRE.r_type = MachO::ARM_RELOC_PAIR;
295 MRE.r_length = Log2Size;
296 MRE.r_pcrel = IsPCRel;
297 MRE.r_scattered = 1;
298 MRE.r_value = Value2;
292 if (Type == macho::RIT_Difference ||
293 Type == macho::RIT_Generic_LocalDifference) {
294 macho::RelocationEntry MRE;
295 MRE.Word0 = ((0 << 0) |
296 (macho::RIT_Pair << 24) |
297 (Log2Size << 28) |
298 (IsPCRel << 30) |
299 macho::RF_Scattered);
300 MRE.Word1 = Value2;
299301 Writer->addRelocation(Fragment->getParent(), MRE);
300302 }
301303
302 MRE.r_address = FixupOffset;
303 MRE.r_type = Type;
304 MRE.r_length = Log2Size;
305 MRE.r_pcrel = IsPCRel;
306 MRE.r_scattered = 1;
307 MRE.r_value = Value;
304 macho::RelocationEntry MRE;
305 MRE.Word0 = ((FixupOffset << 0) |
306 (Type << 24) |
307 (Log2Size << 28) |
308 (IsPCRel << 30) |
309 macho::RF_Scattered);
310 MRE.Word1 = Value;
308311 Writer->addRelocation(Fragment->getParent(), MRE);
309312 }
310313
322325 switch (RelocType) {
323326 default:
324327 return false;
325 case MachO::ARM_RELOC_BR24:
328 case macho::RIT_ARM_Branch24Bit:
326329 // PC pre-adjustment of 8 for these instructions.
327330 Value -= 8;
328331 // ARM BL/BLX has a 25-bit offset.
329332 Range = 0x1ffffff;
330333 break;
331 case MachO::ARM_THUMB_RELOC_BR22:
334 case macho::RIT_ARM_ThumbBranch22Bit:
332335 // PC pre-adjustment of 4 for these instructions.
333336 Value -= 4;
334337 // Thumb BL/BLX has a 24-bit offset.
357360 uint64_t &FixedValue) {
358361 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
359362 unsigned Log2Size;
360 unsigned RelocType = MachO::ARM_RELOC_VANILLA;
363 unsigned RelocType = macho::RIT_Vanilla;
361364 if (!getARMFixupKindMachOInfo(Fixup.getKind(), RelocType, Log2Size))
362365 // If we failed to get fixup kind info, it's because there's no legal
363366 // relocation type for the fixup kind. This happens when it's a fixup that's
370373 // scattered relocation entry. Differences always require scattered
371374 // relocations.
372375 if (Target.getSymB()) {
373 if (RelocType == MachO::ARM_RELOC_HALF)
376 if (RelocType == macho::RIT_ARM_Half)
374377 return RecordARMScatteredHalfRelocation(Writer, Asm, Layout, Fragment,
375378 Fixup, Target, FixedValue);
376379 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
388391 //
389392 // Is this right for ARM?
390393 uint32_t Offset = Target.getConstant();
391 if (IsPCRel && RelocType == MachO::ARM_RELOC_VANILLA)
394 if (IsPCRel && RelocType == macho::RIT_Vanilla)
392395 Offset += 1 << Log2Size;
393396 if (Offset && SD && !Writer->doesSymbolRequireExternRelocation(SD))
394397 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
441444 }
442445
443446 // struct relocation_info (8 bytes)
444 MachO::relocation_info MRE;
445 MRE.r_address = FixupOffset;
446 MRE.r_symbolnum = Index;
447 MRE.r_pcrel = IsPCRel;
448 MRE.r_length = Log2Size;
449 MRE.r_extern = IsExtern;
450 MRE.r_type = Type;
447 macho::RelocationEntry MRE;
448 MRE.Word0 = FixupOffset;
449 MRE.Word1 = ((Index << 0) |
450 (IsPCRel << 24) |
451 (Log2Size << 25) |
452 (IsExtern << 27) |
453 (Type << 28));
451454
452455 // Even when it's not a scattered relocation, movw/movt always uses
453456 // a PAIR relocation.
454 if (Type == MachO::ARM_RELOC_HALF) {
457 if (Type == macho::RIT_ARM_Half) {
455458 // The other-half value only gets populated for the movt and movw
456459 // relocation entries.
457460 uint32_t Value = 0;
470473 Value = FixedValue & 0xffff;
471474 break;
472475 }
473 MachO::relocation_info MREPair;
474 MREPair.r_address = Value;
475 MREPair.r_symbolnum = 0xffffff;
476 MREPair.r_length = Log2Size;
477 MREPair.r_pcrel = MREPair.r_extern = 0;
478 MREPair.r_type = MachO::ARM_RELOC_PAIR;
476 macho::RelocationEntry MREPair;
477 MREPair.Word0 = Value;
478 MREPair.Word1 = ((0xffffff) |
479 (Log2Size << 25) |
480 (macho::RIT_Pair << 28));
479481
480482 Writer->addRelocation(Fragment->getParent(), MREPair);
481483 }
1515 #include "llvm/MC/MCObjectWriter.h"
1616 #include "llvm/MC/MCSectionMachO.h"
1717 #include "llvm/MC/MCValue.h"
18 #include "llvm/Object/MachOFormat.h"
1819 #include "llvm/Support/ELF.h"
1920 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/MachO.h"
2121 #include "llvm/Support/TargetRegistry.h"
2222 using namespace llvm;
2323
163163 return createPPCMachObjectWriter(
164164 OS,
165165 /*Is64Bit=*/is64,
166 (is64 ? MachO::CPU_TYPE_POWERPC64 : MachO::CPU_TYPE_POWERPC),
167 MachO::CPU_SUBTYPE_POWERPC_ALL);
166 (is64 ? object::mach::CTM_PowerPC64 : object::mach::CTM_PowerPC),
167 object::mach::CSPPC_ALL);
168168 }
169169
170170 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
1515 #include "llvm/MC/MCMachObjectWriter.h"
1616 #include "llvm/MC/MCSectionMachO.h"
1717 #include "llvm/MC/MCValue.h"
18 #include "llvm/Object/MachOFormat.h"
1819 #include "llvm/Support/ErrorHandling.h"
1920 #include "llvm/Support/Format.h"
20 #include "llvm/Support/MachO.h"
2121
2222 using namespace llvm;
23 using namespace llvm::object;
2324
2425 namespace {
2526 class PPCMachObjectWriter : public MCMachObjectTargetWriter {
8889 Target.isAbsolute() ? MCSymbolRefExpr::VK_None
8990 : Target.getSymA()->getKind();
9091 // determine the type of the relocation
91 unsigned Type = MachO::GENERIC_RELOC_VANILLA;
92 unsigned Type = macho::RIT_Vanilla;
9293 if (IsPCRel) { // relative to PC
9394 switch ((unsigned)FixupKind) {
9495 default:
9596 report_fatal_error("Unimplemented fixup kind (relative)");
9697 case PPC::fixup_ppc_br24:
97 Type = MachO::PPC_RELOC_BR24; // R_PPC_REL24
98 Type = macho::RIT_PPC_BR24; // R_PPC_REL24
9899 break;
99100 case PPC::fixup_ppc_brcond14:
100 Type = MachO::PPC_RELOC_BR14;
101 Type = macho::RIT_PPC_BR14;
101102 break;
102103 case PPC::fixup_ppc_half16:
103104 switch (Modifier) {
104105 default:
105106 llvm_unreachable("Unsupported modifier for half16 fixup");
106107 case MCSymbolRefExpr::VK_PPC_HA:
107 Type = MachO::PPC_RELOC_HA16;
108 Type = macho::RIT_PPC_HA16;
108109 break;
109110 case MCSymbolRefExpr::VK_PPC_LO:
110 Type = MachO::PPC_RELOC_LO16;
111 Type = macho::RIT_PPC_LO16;
111112 break;
112113 case MCSymbolRefExpr::VK_PPC_HI:
113 Type = MachO::PPC_RELOC_HI16;
114 Type = macho::RIT_PPC_HI16;
114115 break;
115116 }
116117 break;
124125 default:
125126 llvm_unreachable("Unsupported modifier for half16 fixup");
126127 case MCSymbolRefExpr::VK_PPC_HA:
127 Type = MachO::PPC_RELOC_HA16_SECTDIFF;
128 Type = macho::RIT_PPC_HA16_SECTDIFF;
128129 break;
129130 case MCSymbolRefExpr::VK_PPC_LO:
130 Type = MachO::PPC_RELOC_LO16_SECTDIFF;
131 Type = macho::RIT_PPC_LO16_SECTDIFF;
131132 break;
132133 case MCSymbolRefExpr::VK_PPC_HI:
133 Type = MachO::PPC_RELOC_HI16_SECTDIFF;
134 Type = macho::RIT_PPC_HI16_SECTDIFF;
134135 break;
135136 }
136137 break;
143144 return Type;
144145 }
145146
146 static void makeRelocationInfo(MachO::any_relocation_info &MRE,
147 static void makeRelocationInfo(macho::RelocationEntry &MRE,
147148 const uint32_t FixupOffset, const uint32_t Index,
148149 const unsigned IsPCRel, const unsigned Log2Size,
149150 const unsigned IsExtern, const unsigned Type) {
150 MRE.r_word0 = FixupOffset;
151 MRE.Word0 = FixupOffset;
151152 // The bitfield offsets that work (as determined by trial-and-error)
152153 // are different than what is documented in the mach-o manuals.
153 // This appears to be an endianness issue; reversing the order of the
154 // documented bitfields in fixes this (but
155 // breaks x86/ARM assembly).
156 MRE.r_word1 = ((Index << 8) | // was << 0
157 (IsPCRel << 7) | // was << 24
158 (Log2Size << 5) | // was << 25
159 (IsExtern << 4) | // was << 27
160 (Type << 0)); // was << 28
154 // Is this an endianness issue w/ PPC?
155 MRE.Word1 = ((Index << 8) | // was << 0
156 (IsPCRel << 7) | // was << 24
157 (Log2Size << 5) | // was << 25
158 (IsExtern << 4) | // was << 27
159 (Type << 0)); // was << 28
161160 }
162161
163162 static void
164 makeScatteredRelocationInfo(MachO::scattered_relocation_info &MRE,
165 const uint32_t Addr, const unsigned Type,
166 const unsigned Log2Size, const unsigned IsPCRel,
167 const uint32_t Value2) {
168 MRE.r_scattered = true;
169 MRE.r_pcrel = IsPCRel;
170 MRE.r_length = Log2Size;
171 MRE.r_type = Type;
172 MRE.r_address = Addr;
173 MRE.r_value = Value2;
163 makeScatteredRelocationInfo(macho::RelocationEntry &MRE, const uint32_t Addr,
164 const unsigned Type, const unsigned Log2Size,
165 const unsigned IsPCRel, const uint32_t Value2) {
166 // For notes on bitfield positions and endianness, see:
167 // https://developer.apple.com/library/mac/documentation/developertools/conceptual/MachORuntime/Reference/reference.html#//apple_ref/doc/uid/20001298-scattered_relocation_entry
168 MRE.Word0 = ((Addr << 0) | (Type << 24) | (Log2Size << 28) | (IsPCRel << 30) |
169 macho::RF_Scattered);
170 MRE.Word1 = Value2;
174171 }
175172
176173 /// Compute fixup offset (address).
225222 report_fatal_error("symbol '" + B->getSymbol().getName() +
226223 "' can not be undefined in a subtraction expression");
227224
228 // FIXME: is Type correct? see include/llvm/Support/MachO.h
225 // FIXME: is Type correct? see include/llvm/Object/MachOFormat.h
229226 Value2 = Writer->getSymbolAddress(B_SD, Layout);
230227 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
231228 }
232229 // FIXME: does FixedValue get used??
233230
234231 // Relocations are written out in reverse order, so the PAIR comes first.
235 if (Type == MachO::PPC_RELOC_SECTDIFF ||
236 Type == MachO::PPC_RELOC_HI16_SECTDIFF ||
237 Type == MachO::PPC_RELOC_LO16_SECTDIFF ||
238 Type == MachO::PPC_RELOC_HA16_SECTDIFF ||
239 Type == MachO::PPC_RELOC_LO14_SECTDIFF ||
240 Type == MachO::PPC_RELOC_LOCAL_SECTDIFF) {
232 if (Type == macho::RIT_PPC_SECTDIFF || Type == macho::RIT_PPC_HI16_SECTDIFF ||
233 Type == macho::RIT_PPC_LO16_SECTDIFF ||
234 Type == macho::RIT_PPC_HA16_SECTDIFF ||
235 Type == macho::RIT_PPC_LO14_SECTDIFF ||
236 Type == macho::RIT_PPC_LOCAL_SECTDIFF) {
241237 // X86 had this piece, but ARM does not
242238 // If the offset is too large to fit in a scattered relocation,
243239 // we're hosed. It's an unfortunate limitation of the MachO format.
256252 // see PPCMCExpr::EvaluateAsRelocatableImpl()
257253 uint32_t other_half = 0;
258254 switch (Type) {
259 case MachO::PPC_RELOC_LO16_SECTDIFF:
255 case macho::RIT_PPC_LO16_SECTDIFF:
260256 other_half = (FixedValue >> 16) & 0xffff;
261257 // applyFixupOffset longer extracts the high part because it now assumes
262258 // this was already done.
265261 // So we need to adjust FixedValue again here.
266262 FixedValue &= 0xffff;
267263 break;
268 case MachO::PPC_RELOC_HA16_SECTDIFF:
264 case macho::RIT_PPC_HA16_SECTDIFF:
269265 other_half = FixedValue & 0xffff;
270266 FixedValue =
271267 ((FixedValue >> 16) + ((FixedValue & 0x8000) ? 1 : 0)) & 0xffff;
272268 break;
273 case MachO::PPC_RELOC_HI16_SECTDIFF:
269 case macho::RIT_PPC_HI16_SECTDIFF:
274270 other_half = FixedValue & 0xffff;
275271 FixedValue = (FixedValue >> 16) & 0xffff;
276272 break;
279275 break;
280276 }
281277
282 MachO::scattered_relocation_info MRE;
283 makeScatteredRelocationInfo(MRE, other_half, MachO::GENERIC_RELOC_PAIR,
284 Log2Size, IsPCRel, Value2);
278 macho::RelocationEntry MRE;
279 makeScatteredRelocationInfo(MRE, other_half, macho::RIT_Pair, Log2Size,
280 IsPCRel, Value2);
285281 Writer->addRelocation(Fragment->getParent(), MRE);
286282 } else {
287283 // If the offset is more than 24-bits, it won't fit in a scattered
294290 if (FixupOffset > 0xffffff)
295291 return false;
296292 }
297 MachO::scattered_relocation_info MRE;
293 macho::RelocationEntry MRE;
298294 makeScatteredRelocationInfo(MRE, FixupOffset, Type, Log2Size, IsPCRel, Value);
299295 Writer->addRelocation(Fragment->getParent(), MRE);
300296 return true;
315311 // relocations.
316312 if (Target.getSymB() &&
317313 // Q: are branch targets ever scattered?
318 RelocType != MachO::PPC_RELOC_BR24 &&
319 RelocType != MachO::PPC_RELOC_BR14) {
314 RelocType != macho::RIT_PPC_BR24 && RelocType != macho::RIT_PPC_BR14) {
320315 RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
321316 Log2Size, FixedValue);
322317 return;
373368 FixedValue -= Writer->getSectionAddress(Fragment->getParent());
374369 }
375370
376 MachO::any_relocation_info MRE;
371 // struct relocation_info (8 bytes)
372 macho::RelocationEntry MRE;
377373 makeRelocationInfo(MRE, FixupOffset, Index, IsPCRel, Log2Size, IsExtern,
378374 Type);
379375 Writer->addRelocation(Fragment->getParent(), MRE);
1818 #include "llvm/MC/MCSectionCOFF.h"
1919 #include "llvm/MC/MCSectionELF.h"
2020 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/Object/MachOFormat.h"
2122 #include "llvm/Support/CommandLine.h"
2223 #include "llvm/Support/ELF.h"
2324 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/MachO.h"
2525 #include "llvm/Support/TargetRegistry.h"
2626 #include "llvm/Support/raw_ostream.h"
2727 using namespace llvm;
394394
395395 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
396396 return createX86MachObjectWriter(OS, /*Is64Bit=*/false,
397 MachO::CPU_TYPE_I386,
398 MachO::CPU_SUBTYPE_I386_ALL);
397 object::mach::CTM_i386,
398 object::mach::CSX86_ALL);
399399 }
400400 };
401401
408408
409409 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
410410 return createX86MachObjectWriter(OS, /*Is64Bit=*/true,
411 MachO::CPU_TYPE_X86_64,
412 MachO::CPU_SUBTYPE_X86_64_ALL);
411 object::mach::CTM_x86_64,
412 object::mach::CSX86_ALL);
413413 }
414414
415415 virtual bool doesSectionRequireSymbols(const MCSection &Section) const {
1616
1717 using namespace llvm;
1818 using namespace object;
19 using namespace MachO;
19 using namespace macho;
2020
2121 namespace {
2222 class X86_64MachORelocationInfo : public MCRelocationInfo {
3232 StringRef SymName; SymI->getName(SymName);
3333 uint64_t SymAddr; SymI->getAddress(SymAddr);
3434
35 any_relocation_info RE = Obj->getRelocation(Rel.getRawDataRefImpl());
35 RelocationEntry RE = Obj->getRelocation(Rel.getRawDataRefImpl());
3636 bool isPCRel = Obj->getAnyRelocationPCRel(RE);
3737
3838 MCSymbol *Sym = Ctx.GetOrCreateSymbol(SymName);
4242 const MCExpr *Expr = 0;
4343
4444 switch(RelType) {
45 case X86_64_RELOC_TLV:
45 case RIT_X86_64_TLV:
4646 Expr = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_TLVP, Ctx);
4747 break;
48 case X86_64_RELOC_SIGNED_4:
48 case RIT_X86_64_Signed4:
4949 Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, Ctx),
5050 MCConstantExpr::Create(4, Ctx),
5151 Ctx);
5252 break;
53 case X86_64_RELOC_SIGNED_2:
53 case RIT_X86_64_Signed2:
5454 Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, Ctx),
5555 MCConstantExpr::Create(2, Ctx),
5656 Ctx);
5757 break;
58 case X86_64_RELOC_SIGNED_1:
58 case RIT_X86_64_Signed1:
5959 Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, Ctx),
6060 MCConstantExpr::Create(1, Ctx),
6161 Ctx);
6262 break;
63 case X86_64_RELOC_GOT_LOAD:
63 case RIT_X86_64_GOTLoad:
6464 Expr = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, Ctx);
6565 break;
66 case X86_64_RELOC_GOT:
66 case RIT_X86_64_GOT:
6767 Expr = MCSymbolRefExpr::Create(Sym, isPCRel ?
6868 MCSymbolRefExpr::VK_GOTPCREL :
6969 MCSymbolRefExpr::VK_GOT,
7070 Ctx);
7171 break;
72 case X86_64_RELOC_SUBTRACTOR:
72 case RIT_X86_64_Subtractor:
7373 {
7474 RelocationRef RelNext;
7575 Obj->getRelocationNext(Rel.getRawDataRefImpl(), RelNext);
76 any_relocation_info RENext = Obj->getRelocation(RelNext.getRawDataRefImpl());
76 RelocationEntry RENext = Obj->getRelocation(RelNext.getRawDataRefImpl());
7777
7878 // X86_64_SUBTRACTOR must be followed by a relocation of type
79 // X86_64_RELOC_UNSIGNED.
79 // X86_64_RELOC_UNSIGNED .
8080<