llvm.org GIT mirror llvm / 9c3dd1b
Move everything depending on Object/MachOFormat.h over to Support/MachO.h. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@189315 91177308-0d34-0410-b5e6-96231b3b80d8 Charles Davis 7 years ago
20 changed file(s) with 1001 addition(s) and 1425 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"
1817 #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<object::macho::RelocationEntry> > Relocations;
100 std::vector<MachO::any_relocation_info> > 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() &
158 ~object::mach::CTFM_ArchMask;
159 return CPUType == object::mach::CTM_ARM;
157 uint32_t CPUType = TargetObjectWriter->getCPUType() & ~MachO::CPU_ARCH_MASK;
158 return CPUType == MachO::CPU_TYPE_ARM;
160159 }
161160
162161 /// @}
212211 // these through in many cases.
213212
214213 void addRelocation(const MCSectionData *SD,
215 object::macho::RelocationEntry &MRE) {
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) {
216229 Relocations[SD].push_back(MRE);
217230 }
218231
1717 #include "llvm/ADT/ArrayRef.h"
1818 #include "llvm/ADT/SmallVector.h"
1919 #include "llvm/ADT/Triple.h"
20 #include "llvm/Object/MachOFormat.h"
2120 #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::LoadCommand C; // The command itself.
55 MachO::load_command 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::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;
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;
158161
159162 // Walk load commands.
160163 LoadCommandInfo getFirstLoadCommandInfo() const;
161164 LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
162165
163166 // MachO specific structures.
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
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
172175 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
173 macho::SegmentLoadCommand
176 MachO::segment_command
174177 getSegmentLoadCommand(const LoadCommandInfo &L) const;
175 macho::Segment64LoadCommand
178 MachO::segment_command_64
176179 getSegment64LoadCommand(const LoadCommandInfo &L) const;
177 macho::LinkerOptionsLoadCommand
180 MachO::linker_options_command
178181 getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
179182
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,
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,
186189 unsigned Index) 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;
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;
192195
193196 StringRef getStringTableData() const;
194197 bool is64Bit() const;
222225
223226 inline error_code DiceRef::getNext(DiceRef &Result) const {
224227 DataRefImpl Rel = DicePimpl;
225 const macho::DataInCodeTableEntry *P =
226 reinterpret_cast(Rel.p);
228 const MachO::data_in_code_entry *P =
229 reinterpret_cast(Rel.p);
227230 Rel.p = reinterpret_cast(P + 1);
228231 Result = DiceRef(Rel, OwningObject);
229232 return object_error::success;
236239 inline error_code DiceRef::getOffset(uint32_t &Result) const {
237240 const MachOObjectFile *MachOOF =
238241 static_cast(OwningObject);
239 macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
240 Result = Dice.Offset;
242 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
243 Result = Dice.offset;
241244 return object_error::success;
242245 }
243246
244247 inline error_code DiceRef::getLength(uint16_t &Result) const {
245248 const MachOObjectFile *MachOOF =
246249 static_cast(OwningObject);
247 macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
248 Result = Dice.Length;
250 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
251 Result = Dice.length;
249252 return object_error::success;
250253 }
251254
252255 inline error_code DiceRef::getKind(uint16_t &Result) const {
253256 const MachOObjectFile *MachOOF =
254257 static_cast(OwningObject);
255 macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
256 Result = Dice.Kind;
258 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
259 Result = Dice.kind;
257260 return object_error::success;
258261 }
259262
+0
-452
include/llvm/Object/MachOFormat.h less more
None //===- 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/Object/MachOFormat.h"
20 #include "llvm/Support/MachO.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::FatArchHeader Header;
37 MachO::fat_arch 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::RIT_Vanilla: {
182 case MachO::GENERIC_RELOC_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::RIT_Difference:
192 case macho::RIT_Generic_LocalDifference:
193 case macho::RIT_Generic_PreboundLazyPointer:
191 case MachO::GENERIC_RELOC_SECTDIFF:
192 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF:
193 case MachO::GENERIC_RELOC_PB_LA_PTR:
194194 return Error("Relocation type not implemented yet!");
195195 }
196196 }
212212 switch(Type) {
213213 default:
214214 llvm_unreachable("Invalid relocation type!");
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: {
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: {
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::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:
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:
235235 return Error("Relocation type not implemented yet!");
236236 }
237237 }
256256 switch(Type) {
257257 default:
258258 llvm_unreachable("Invalid relocation type!");
259 case macho::RIT_Vanilla: {
259 case MachO::ARM_RELOC_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;
264 *p++ = (uint8_t)(Value & 0xff);
265265 Value >>= 8;
266266 }
267267 break;
268268 }
269 case macho::RIT_ARM_Branch24Bit: {
269 case MachO::ARM_RELOC_BR24: {
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::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:
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:
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::RelocationEntry RE = MachO->getRelocation(RelI.getRawDataRefImpl());
306 MachO::any_relocation_info 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::RIT_X86_64_GOT ||
362 RelType == macho::RIT_X86_64_GOTLoad)) {
361 if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
362 RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
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::RIT_X86_64_Unsigned, 0, false, 3);
373 MachO::X86_64_RELOC_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::RIT_X86_64_Unsigned, Value.Addend, true, 2);
382 MachO::X86_64_RELOC_UNSIGNED, Value.Addend, true, 2);
383383 } else if (Arch == Triple::arm &&
384 (RelType & 0xf) == macho::RIT_ARM_Branch24Bit) {
384 (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
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::RIT_Vanilla, Value.Addend);
399 MachO::GENERIC_RELOC_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().NumLoadCommands;
539 uint32_t LoadCommandCount = MOOF.getHeader().ncmds;
540540 MachOObjectFile::LoadCommandInfo Load = MOOF.getFirstLoadCommandInfo();
541541 for (unsigned I = 0;; ++I) {
542 if (Load.C.Type == MachO::LC_MAIN) {
542 if (Load.C.cmd == 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::Section64 S = MOOF->getSection64(StubsSec.getRawDataRefImpl());
63 StubsIndSymIndex = S.Reserved1;
64 StubSize = S.Reserved2;
62 MachO::section_64 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 macho::IndirectSymbolTableEntry ISTE =
88 uint32_t SymtabIdx =
8989 MOOF->getIndirectSymbolTableEntry(MOOF->getDysymtabLoadCommand(), StubIdx);
90 uint32_t SymtabIdx = ISTE.Index;
9190
9291 StringRef SymName;
9392 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"
2322 #include "llvm/Support/Debug.h"
2423 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/MachO.h"
2525 #include
2626 using namespace llvm;
27 using namespace llvm::object;
2827
2928 void MachObjectWriter::reset() {
3029 Relocations.clear();
127126 uint32_t Flags = 0;
128127
129128 if (SubsectionsViaSymbols)
130 Flags |= macho::HF_SubsectionsViaSymbols;
129 Flags |= MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
131130
132131 // struct mach_header (28 bytes) or
133132 // struct mach_header_64 (32 bytes)
135134 uint64_t Start = OS.tell();
136135 (void) Start;
137136
138 Write32(is64Bit() ? macho::HM_Object64 : macho::HM_Object32);
137 Write32(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC);
139138
140139 Write32(TargetObjectWriter->getCPUType());
141140 Write32(TargetObjectWriter->getCPUSubtype());
142141
143 Write32(macho::HFT_Object);
142 Write32(MachO::MH_OBJECT);
144143 Write32(NumLoadCommands);
145144 Write32(LoadCommandsSize);
146145 Write32(Flags);
148147 Write32(0); // reserved
149148
150149 assert(OS.tell() - Start ==
151 (is64Bit() ? macho::Header64Size : macho::Header32Size));
150 (is64Bit()?sizeof(MachO::mach_header_64): sizeof(MachO::mach_header)));
152151 }
153152
154153 /// WriteSegmentLoadCommand - Write a segment load command.
166165 (void) Start;
167166
168167 unsigned SegmentLoadCommandSize =
169 is64Bit() ? macho::SegmentLoadCommand64Size:
170 macho::SegmentLoadCommand32Size;
171 Write32(is64Bit() ? macho::LCT_Segment64 : macho::LCT_Segment);
168 is64Bit() ? sizeof(MachO::segment_command_64):
169 sizeof(MachO::segment_command);
170 Write32(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT);
172171 Write32(SegmentLoadCommandSize +
173 NumSections * (is64Bit() ? macho::Section64Size :
174 macho::Section32Size));
172 NumSections * (is64Bit() ? sizeof(MachO::section_64) :
173 sizeof(MachO::section)));
175174
176175 WriteBytes("", 16);
177176 if (is64Bit()) {
239238 if (is64Bit())
240239 Write32(0); // reserved3
241240
242 assert(OS.tell() - Start == (is64Bit() ? macho::Section64Size :
243 macho::Section32Size));
241 assert(OS.tell() - Start == (is64Bit() ? sizeof(MachO::section_64) :
242 sizeof(MachO::section)));
244243 }
245244
246245 void MachObjectWriter::WriteSymtabLoadCommand(uint32_t SymbolOffset,
252251 uint64_t Start = OS.tell();
253252 (void) Start;
254253
255 Write32(macho::LCT_Symtab);
256 Write32(macho::SymtabLoadCommandSize);
254 Write32(MachO::LC_SYMTAB);
255 Write32(sizeof(MachO::symtab_command));
257256 Write32(SymbolOffset);
258257 Write32(NumSymbols);
259258 Write32(StringTableOffset);
260259 Write32(StringTableSize);
261260
262 assert(OS.tell() - Start == macho::SymtabLoadCommandSize);
261 assert(OS.tell() - Start == sizeof(MachO::symtab_command));
263262 }
264263
265264 void MachObjectWriter::WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
275274 uint64_t Start = OS.tell();
276275 (void) Start;
277276
278 Write32(macho::LCT_Dysymtab);
279 Write32(macho::DysymtabLoadCommandSize);
277 Write32(MachO::LC_DYSYMTAB);
278 Write32(sizeof(MachO::dysymtab_command));
280279 Write32(FirstLocalSymbol);
281280 Write32(NumLocalSymbols);
282281 Write32(FirstExternalSymbol);
296295 Write32(0); // locreloff
297296 Write32(0); // nlocrel
298297
299 assert(OS.tell() - Start == macho::DysymtabLoadCommandSize);
298 assert(OS.tell() - Start == sizeof(MachO::dysymtab_command));
300299 }
301300
302301 void MachObjectWriter::WriteNlist(MachSymbolData &MSD,
311310 //
312311 // FIXME: Are the prebound or indirect fields possible here?
313312 if (Symbol.isUndefined())
314 Type = macho::STT_Undefined;
313 Type = MachO::N_UNDF;
315314 else if (Symbol.isAbsolute())
316 Type = macho::STT_Absolute;
315 Type = MachO::N_ABS;
317316 else
318 Type = macho::STT_Section;
317 Type = MachO::N_SECT;
319318
320319 // FIXME: Set STAB bits.
321320
322321 if (Data.isPrivateExtern())
323 Type |= macho::STF_PrivateExtern;
322 Type |= MachO::N_PEXT;
324323
325324 // Set external bit.
326325 if (Data.isExternal() || Symbol.isUndefined())
327 Type |= macho::STF_External;
326 Type |= MachO::N_EXT;
328327
329328 // Compute the symbol address.
330329 if (Symbol.isDefined()) {
368367 (void) Start;
369368
370369 Write32(Type);
371 Write32(macho::LinkeditLoadCommandSize);
370 Write32(sizeof(MachO::linkedit_data_command));
372371 Write32(DataOffset);
373372 Write32(DataSize);
374373
375 assert(OS.tell() - Start == macho::LinkeditLoadCommandSize);
374 assert(OS.tell() - Start == sizeof(MachO::linkedit_data_command));
376375 }
377376
378377 static unsigned ComputeLinkerOptionsLoadCommandSize(
379378 const std::vector &Options, bool is64Bit)
380379 {
381 unsigned Size = sizeof(macho::LinkerOptionsLoadCommand);
380 unsigned Size = sizeof(MachO::linker_options_command);
382381 for (unsigned i = 0, e = Options.size(); i != e; ++i)
383382 Size += Options[i].size() + 1;
384383 return RoundUpToAlignment(Size, is64Bit ? 8 : 4);
391390 uint64_t Start = OS.tell();
392391 (void) Start;
393392
394 Write32(macho::LCT_LinkerOptions);
393 Write32(MachO::LC_LINKER_OPTIONS);
395394 Write32(Size);
396395 Write32(Options.size());
397 uint64_t BytesWritten = sizeof(macho::LinkerOptionsLoadCommand);
396 uint64_t BytesWritten = sizeof(MachO::linker_options_command);
398397 for (unsigned i = 0, e = Options.size(); i != e; ++i) {
399398 // Write each string, including the null byte.
400399 const std::string &Option = Options[i];
722721 // section headers) and the symbol table.
723722 unsigned NumLoadCommands = 1;
724723 uint64_t LoadCommandsSize = is64Bit() ?
725 macho::SegmentLoadCommand64Size + NumSections * macho::Section64Size :
726 macho::SegmentLoadCommand32Size + NumSections * macho::Section32Size;
724 sizeof(MachO::segment_command_64) + NumSections * sizeof(MachO::section_64):
725 sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
727726
728727 // Add the data-in-code load command size, if used.
729728 unsigned NumDataRegions = Asm.getDataRegions().size();
730729 if (NumDataRegions) {
731730 ++NumLoadCommands;
732 LoadCommandsSize += macho::LinkeditLoadCommandSize;
731 LoadCommandsSize += sizeof(MachO::linkedit_data_command);
733732 }
734733
735734 // Add the symbol table load command sizes, if used.
737736 UndefinedSymbolData.size();
738737 if (NumSymbols) {
739738 NumLoadCommands += 2;
740 LoadCommandsSize += (macho::SymtabLoadCommandSize +
741 macho::DysymtabLoadCommandSize);
739 LoadCommandsSize += (sizeof(MachO::symtab_command) +
740 sizeof(MachO::dysymtab_command));
742741 }
743742
744743 // Add the linker option load commands sizes.
752751
753752 // Compute the total size of the section data, as well as its file size and vm
754753 // size.
755 uint64_t SectionDataStart = (is64Bit() ? macho::Header64Size :
756 macho::Header32Size) + LoadCommandsSize;
754 uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) :
755 sizeof(MachO::mach_header)) + LoadCommandsSize;
757756 uint64_t SectionDataSize = 0;
758757 uint64_t SectionDataFileSize = 0;
759758 uint64_t VMSize = 0;
790789 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
791790 for (MCAssembler::const_iterator it = Asm.begin(),
792791 ie = Asm.end(); it != ie; ++it) {
793 std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
792 std::vector<MachO::any_relocation_info> &Relocs = Relocations[it];
794793 unsigned NumRelocs = Relocs.size();
795794 uint64_t SectionStart = SectionDataStart + getSectionAddress(it);
796795 WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
797 RelocTableEnd += NumRelocs * macho::RelocationInfoSize;
796 RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info);
798797 }
799798
800799 // Write the data-in-code load command, if used.
802801 if (NumDataRegions) {
803802 uint64_t DataRegionsOffset = RelocTableEnd;
804803 uint64_t DataRegionsSize = NumDataRegions * 8;
805 WriteLinkeditLoadCommand(macho::LCT_DataInCode, DataRegionsOffset,
804 WriteLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset,
806805 DataRegionsSize);
807806 }
808807
829828
830829 // The string table is written after symbol table.
831830 uint64_t StringTableOffset =
832 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ? macho::Nlist64Size :
833 macho::Nlist32Size);
831 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ?
832 sizeof(MachO::nlist_64) :
833 sizeof(MachO::nlist));
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::RelocationEntry> &Relocs = Relocations[it];
866 std::vector<MachO::any_relocation_info> &Relocs = Relocations[it];
867867 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
868 Write32(Relocs[e - i - 1].Word0);
869 Write32(Relocs[e - i - 1].Word1);
868 Write32(Relocs[e - i - 1].r_word0);
869 Write32(Relocs[e - i - 1].r_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::ISF_Local;
908 uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL;
909909 if (it->Symbol->isAbsolute())
910 Flags |= macho::ISF_Absolute;
910 Flags |= MachO::INDIRECT_SYMBOL_ABS;
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"
1716 #include "llvm/Support/DataExtractor.h"
1817 #include "llvm/Support/Format.h"
1918 #include "llvm/Support/Host.h"
20 #include "llvm/Support/MachO.h"
2119 #include "llvm/Support/MemoryBuffer.h"
2220 #include "llvm/Support/raw_ostream.h"
2321 #include
3028 namespace llvm {
3129 namespace object {
3230
33 struct SymbolTableEntryBase {
34 uint32_t StringIndex;
35 uint8_t Type;
36 uint8_t SectionIndex;
37 uint16_t Flags;
31 struct nlist_base {
32 uint32_t n_strx;
33 uint8_t n_type;
34 uint8_t n_sect;
35 uint16_t n_desc;
3836 };
3937
40 struct SectionBase {
41 char Name[16];
42 char SegmentName[16];
38 struct section_base {
39 char sectname[16];
40 char segname[16];
4341 };
4442
4543 template
5149 static void SwapStruct(T &Value);
5250
5351 template<>
54 void SwapStruct(macho::RelocationEntry &H) {
55 SwapValue(H.Word0);
56 SwapValue(H.Word1);
52 void SwapStruct(MachO::any_relocation_info &H) {
53 SwapValue(H.r_word0);
54 SwapValue(H.r_word1);
5755 }
5856
5957 template<>
60 void SwapStruct(macho::LoadCommand &L) {
61 SwapValue(L.Type);
62 SwapValue(L.Size);
58 void SwapStruct(MachO::load_command &L) {
59 SwapValue(L.cmd);
60 SwapValue(L.cmdsize);
6361 }
6462
6563 template<>
66 void SwapStruct(SymbolTableEntryBase &S) {
67 SwapValue(S.StringIndex);
68 SwapValue(S.Flags);
64 void SwapStruct(nlist_base &S) {
65 SwapValue(S.n_strx);
66 SwapValue(S.n_desc);
6967 }
7068
7169 template<>
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);
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);
8280 }
8381
8482 template<>
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);
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);
9694 }
9795
9896 template<>
99 void SwapStruct(macho::SymbolTableEntry &S) {
100 SwapValue(S.StringIndex);
101 SwapValue(S.Flags);
102 SwapValue(S.Value);
97 void SwapStruct(MachO::nlist &S) {
98 SwapValue(S.n_strx);
99 SwapValue(S.n_desc);
100 SwapValue(S.n_value);
103101 }
104102
105103 template<>
106 void SwapStruct(macho::Symbol64TableEntry &S) {
107 SwapValue(S.StringIndex);
108 SwapValue(S.Flags);
109 SwapValue(S.Value);
104 void SwapStruct(MachO::nlist_64 &S) {
105 SwapValue(S.n_strx);
106 SwapValue(S.n_desc);
107 SwapValue(S.n_value);
110108 }
111109
112110 template<>
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);
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);
121119 }
122120
123121 template<>
124 void SwapStruct(macho::Header64Ext &E) {
125 SwapValue(E.Reserved);
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);
126131 }
127132
128133 template<>
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);
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);
136141 }
137142
138143 template<>
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);
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);
160165 }
161166
162167 template<>
163 void SwapStruct(macho::LinkeditDataLoadCommand &C) {
164 SwapValue(C.Type);
165 SwapValue(C.Size);
166 SwapValue(C.DataOffset);
167 SwapValue(C.DataSize);
168 void SwapStruct(MachO::linkedit_data_command &C) {
169 SwapValue(C.cmd);
170 SwapValue(C.cmdsize);
171 SwapValue(C.dataoff);
172 SwapValue(C.datasize);
168173 }
169174
170175 template<>
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);
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);
182187 }
183188
184189 template<>
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);
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);
196201 }
197202
198203 template<>
199 void SwapStruct(macho::IndirectSymbolTableEntry &C) {
200 SwapValue(C.Index);
204 void SwapStruct(uint32_t &C) {
205 SwapValue(C);
201206 }
202207
203208 template<>
204 void SwapStruct(macho::LinkerOptionsLoadCommand &C) {
205 SwapValue(C.Type);
206 SwapValue(C.Size);
207 SwapValue(C.Count);
209 void SwapStruct(MachO::linker_options_command &C) {
210 SwapValue(C.cmd);
211 SwapValue(C.cmdsize);
212 SwapValue(C.count);
208213 }
209214
210215 template<>
211 void SwapStruct(macho::DataInCodeTableEntry &C) {
212 SwapValue(C.Offset);
213 SwapValue(C.Length);
214 SwapValue(C.Kind);
216 void SwapStruct(MachO::data_in_code_entry &C) {
217 SwapValue(C.offset);
218 SwapValue(C.length);
219 SwapValue(C.kind);
215220 }
216221
217222 template
227232 getSegmentLoadCommandNumSections(const MachOObjectFile *O,
228233 const MachOObjectFile::LoadCommandInfo &L) {
229234 if (O->is64Bit()) {
230 macho::Segment64LoadCommand S = O->getSegment64LoadCommand(L);
231 return S.NumSections;
232 }
233 macho::SegmentLoadCommand S = O->getSegmentLoadCommand(L);
234 return S.NumSections;
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;
235240 }
236241
237242 static const char *
240245 uintptr_t CommandAddr = reinterpret_cast(L.Ptr);
241246
242247 bool Is64 = O->is64Bit();
243 unsigned SegmentLoadSize = Is64 ? sizeof(macho::Segment64LoadCommand) :
244 sizeof(macho::SegmentLoadCommand);
245 unsigned SectionSize = Is64 ? sizeof(macho::Section64) :
246 sizeof(macho::Section);
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);
247252
248253 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
249 return reinterpret_cast*>(SectionAddr);
254 return reinterpret_cast *>(SectionAddr);
250255 }
251256
252257 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
253258 return O->getData().substr(Offset, 1).data();
254259 }
255260
256 static SymbolTableEntryBase
261 static nlist_base
257262 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
258263 const char *P = reinterpret_cast(DRI.p);
259 return getStruct<SymbolTableEntryBase>(O, P);
264 return getStruct<nlist_base>(O, P);
260265 }
261266
262267 static StringRef parseSegmentOrSectionName(const char *P) {
284289 }
285290
286291 static unsigned getCPUType(const MachOObjectFile *O) {
287 return O->getHeader().CPUType;
292 return O->getHeader().cputype;
288293 }
289294
290295 static void printRelocationTargetName(const MachOObjectFile *O,
291 const macho::RelocationEntry &RE,
296 const MachO::any_relocation_info &RE,
292297 raw_string_ostream &fmt) {
293298 bool IsScattered = O->isRelocationScattered(RE);
294299
356361 fmt << S;
357362 }
358363
359 static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) {
360 return RE.Word0;
364 static uint32_t
365 getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
366 return RE.r_word0;
361367 }
362368
363369 static unsigned
364 getScatteredRelocationAddress(const macho::RelocationEntry &RE) {
365 return RE.Word0 & 0xffffff;
370 getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
371 return RE.r_word0 & 0xffffff;
366372 }
367373
368374 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
369 const macho::RelocationEntry &RE) {
375 const MachO::any_relocation_info &RE) {
370376 if (O->isLittleEndian())
371 return (RE.Word1 >> 24) & 1;
372 return (RE.Word1 >> 7) & 1;
377 return (RE.r_word1 >> 24) & 1;
378 return (RE.r_word1 >> 7) & 1;
373379 }
374380
375381 static bool
376382 getScatteredRelocationPCRel(const MachOObjectFile *O,
377 const macho::RelocationEntry &RE) {
378 return (RE.Word0 >> 30) & 1;
383 const MachO::any_relocation_info &RE) {
384 return (RE.r_word0 >> 30) & 1;
379385 }
380386
381387 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
382 const macho::RelocationEntry &RE) {
388 const MachO::any_relocation_info &RE) {
383389 if (O->isLittleEndian())
384 return (RE.Word1 >> 25) & 3;
385 return (RE.Word1 >> 5) & 3;
390 return (RE.r_word1 >> 25) & 3;
391 return (RE.r_word1 >> 5) & 3;
386392 }
387393
388394 static unsigned
389 getScatteredRelocationLength(const macho::RelocationEntry &RE) {
390 return (RE.Word0 >> 28) & 3;
395 getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
396 return (RE.r_word0 >> 28) & 3;
391397 }
392398
393399 static unsigned getPlainRelocationType(const MachOObjectFile *O,
394 const macho::RelocationEntry &RE) {
400 const MachO::any_relocation_info &RE) {
395401 if (O->isLittleEndian())
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;
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;
402409 }
403410
404411 static uint32_t getSectionFlags(const MachOObjectFile *O,
405412 DataRefImpl Sec) {
406413 if (O->is64Bit()) {
407 macho::Section64 Sect = O->getSection64(Sec);
408 return Sect.Flags;
409 }
410 macho::Section Sect = O->getSection(Sec);
411 return Sect.Flags;
414 MachO::section_64 Sect = O->getSection64(Sec);
415 return Sect.flags;
416 }
417 MachO::section Sect = O->getSection(Sec);
418 return Sect.flags;
412419 }
413420
414421 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
416423 error_code &ec)
417424 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
418425 SymtabLoadCmd(NULL), DysymtabLoadCmd(NULL), DataInCodeLoadCmd(NULL) {
419 uint32_t LoadCommandCount = this->getHeader().NumLoadCommands;
420 macho::LoadCommandType SegmentLoadType = is64Bit() ?
421 macho::LCT_Segment64 : macho::LCT_Segment;
426 uint32_t LoadCommandCount = this->getHeader().ncmds;
427 MachO::LoadCommandType SegmentLoadType = is64Bit() ?
428 MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
422429
423430 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
424431 for (unsigned I = 0; ; ++I) {
425 if (Load.C.Type == macho::LCT_Symtab) {
432 if (Load.C.cmd == MachO::LC_SYMTAB) {
426433 assert(!SymtabLoadCmd && "Multiple symbol tables");
427434 SymtabLoadCmd = Load.Ptr;
428 } else if (Load.C.Type == macho::LCT_Dysymtab) {
435 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
429436 assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
430437 DysymtabLoadCmd = Load.Ptr;
431 } else if (Load.C.Type == macho::LCT_DataInCode) {
438 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
432439 assert(!DataInCodeLoadCmd && "Multiple data in code tables");
433440 DataInCodeLoadCmd = Load.Ptr;
434 } else if (Load.C.Type == SegmentLoadType) {
441 } else if (Load.C.cmd == SegmentLoadType) {
435442 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
436443 for (unsigned J = 0; J < NumSections; ++J) {
437444 const char *Sec = getSectionPtr(this, Load, J);
449456 error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
450457 SymbolRef &Res) const {
451458 unsigned SymbolTableEntrySize = is64Bit() ?
452 sizeof(macho::Symbol64TableEntry) :
453 sizeof(macho::SymbolTableEntry);
459 sizeof(MachO::nlist_64) :
460 sizeof(MachO::nlist);
454461 Symb.p += SymbolTableEntrySize;
455462 Res = SymbolRef(Symb, this);
456463 return object_error::success;
459466 error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
460467 StringRef &Res) const {
461468 StringRef StringTable = getStringTableData();
462 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
463 const char *Start = &StringTable.data()[Entry.StringIndex];
469 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
470 const char *Start = &StringTable.data()[Entry.n_strx];
464471 Res = StringRef(Start);
465472 return object_error::success;
466473 }
468475 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
469476 uint64_t &Res) const {
470477 if (is64Bit()) {
471 macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb);
472 Res = Entry.Value;
478 MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
479 Res = Entry.n_value;
473480 } else {
474 macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb);
475 Res = Entry.Value;
481 MachO::nlist Entry = getSymbolTableEntry(Symb);
482 Res = Entry.n_value;
476483 }
477484 return object_error::success;
478485 }
480487 error_code
481488 MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
482489 uint64_t &Res) const {
483 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
490 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
484491 getSymbolAddress(Symb, Res);
485 if (Entry.SectionIndex) {
492 if (Entry.n_sect) {
486493 uint64_t Delta;
487494 DataRefImpl SecRel;
488 SecRel.d.a = Entry.SectionIndex-1;
495 SecRel.d.a = Entry.n_sect-1;
489496 if (is64Bit()) {
490 macho::Section64 Sec = getSection64(SecRel);
491 Delta = Sec.Offset - Sec.Address;
497 MachO::section_64 Sec = getSection64(SecRel);
498 Delta = Sec.offset - Sec.addr;
492499 } else {
493 macho::Section Sec = getSection(SecRel);
494 Delta = Sec.Offset - Sec.Address;
500 MachO::section Sec = getSection(SecRel);
501 Delta = Sec.offset - Sec.addr;
495502 }
496503
497504 Res += Delta;
505512 uint32_t flags;
506513 this->getSymbolFlags(DRI, flags);
507514 if (flags & SymbolRef::SF_Common) {
508 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
509 Result = 1 << MachO::GET_COMM_ALIGN(Entry.Flags);
515 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
516 Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
510517 } else {
511518 Result = 0;
512519 }
519526 uint64_t EndOffset = 0;
520527 uint8_t SectionIndex;
521528
522 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
529 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
523530 uint64_t Value;
524531 getSymbolAddress(DRI, Value);
525532
526533 BeginOffset = Value;
527534
528 SectionIndex = Entry.SectionIndex;
535 SectionIndex = Entry.n_sect;
529536 if (!SectionIndex) {
530537 uint32_t flags = SymbolRef::SF_None;
531538 this->getSymbolFlags(DRI, flags);
543550 DataRefImpl DRI = I->getRawDataRefImpl();
544551 Entry = getSymbolTableEntryBase(this, DRI);
545552 getSymbolAddress(DRI, Value);
546 if (Entry.SectionIndex == SectionIndex && Value > BeginOffset)
553 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
547554 if (!EndOffset || Value < EndOffset)
548555 EndOffset = Value;
549556 }
561568
562569 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
563570 SymbolRef::Type &Res) const {
564 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
565 uint8_t n_type = Entry.Type;
571 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
572 uint8_t n_type = Entry.n_type;
566573
567574 Res = SymbolRef::ST_Other;
568575
585592
586593 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
587594 char &Res) const {
588 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
589 uint8_t Type = Entry.Type;
590 uint16_t Flags = Entry.Flags;
595 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
596 uint8_t Type = Entry.n_type;
597 uint16_t Flags = Entry.n_desc;
591598
592599 char Char;
593 switch (Type & macho::STF_TypeMask) {
594 case macho::STT_Undefined:
600 switch (Type & MachO::N_TYPE) {
601 case MachO::N_UNDF:
595602 Char = 'u';
596603 break;
597 case macho::STT_Absolute:
598 case macho::STT_Section:
604 case MachO::N_ABS:
605 case MachO::N_SECT:
599606 Char = 's';
600607 break;
601608 default:
603610 break;
604611 }
605612
606 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
613 if (Flags & (MachO::N_EXT | MachO::N_PEXT))
607614 Char = toupper(static_cast(Char));
608615 Res = Char;
609616 return object_error::success;
611618
612619 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
613620 uint32_t &Result) const {
614 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
615
616 uint8_t MachOType = Entry.Type;
617 uint16_t MachOFlags = Entry.Flags;
621 nlist_base Entry = getSymbolTableEntryBase(this, DRI);
622
623 uint8_t MachOType = Entry.n_type;
624 uint16_t MachOFlags = Entry.n_desc;
618625
619626 // TODO: Correctly set SF_ThreadLocal
620627 Result = SymbolRef::SF_None;
622629 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
623630 Result |= SymbolRef::SF_Undefined;
624631
625 if (MachOFlags & macho::STF_StabsEntryMask)
632 if (MachOFlags & MachO::N_STAB)
626633 Result |= SymbolRef::SF_FormatSpecific;
627634
628635 if (MachOType & MachO::N_EXT) {
647654 error_code
648655 MachOObjectFile::getSymbolSection(DataRefImpl Symb,
649656 section_iterator &Res) const {
650 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
651 uint8_t index = Entry.SectionIndex;
657 nlist_base Entry = getSymbolTableEntryBase(this, Symb);
658 uint8_t index = Entry.n_sect;
652659
653660 if (index == 0) {
654661 Res = end_sections();
683690 error_code
684691 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
685692 if (is64Bit()) {
686 macho::Section64 Sect = getSection64(Sec);
687 Res = Sect.Address;
693 MachO::section_64 Sect = getSection64(Sec);
694 Res = Sect.addr;
688695 } else {
689 macho::Section Sect = getSection(Sec);
690 Res = Sect.Address;
696 MachO::section Sect = getSection(Sec);
697 Res = Sect.addr;
691698 }
692699 return object_error::success;
693700 }
695702 error_code
696703 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
697704 if (is64Bit()) {
698 macho::Section64 Sect = getSection64(Sec);
699 Res = Sect.Size;
705 MachO::section_64 Sect = getSection64(Sec);
706 Res = Sect.size;
700707 } else {
701 macho::Section Sect = getSection(Sec);
702 Res = Sect.Size;
708 MachO::section Sect = getSection(Sec);
709 Res = Sect.size;
703710 }
704711
705712 return object_error::success;
711718 uint64_t Size;
712719
713720 if (is64Bit()) {
714 macho::Section64 Sect = getSection64(Sec);
715 Offset = Sect.Offset;
716 Size = Sect.Size;
721 MachO::section_64 Sect = getSection64(Sec);
722 Offset = Sect.offset;
723 Size = Sect.size;
717724 } else {
718 macho::Section Sect =getSection(Sec);
719 Offset = Sect.Offset;
720 Size = Sect.Size;
725 MachO::section Sect = getSection(Sec);
726 Offset = Sect.offset;
727 Size = Sect.size;
721728 }
722729
723730 Res = this->getData().substr(Offset, Size);
728735 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
729736 uint32_t Align;
730737 if (is64Bit()) {
731 macho::Section64 Sect = getSection64(Sec);
732 Align = Sect.Align;
738 MachO::section_64 Sect = getSection64(Sec);
739 Align = Sect.align;
733740 } else {
734 macho::Section Sect = getSection(Sec);
735 Align = Sect.Align;
741 MachO::section Sect = getSection(Sec);
742 Align = Sect.align;
736743 }
737744
738745 Res = uint64_t(1) << Align;
742749 error_code
743750 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
744751 uint32_t Flags = getSectionFlags(this, Sec);
745 Res = Flags & macho::SF_PureInstructions;
752 Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
746753 return object_error::success;
747754 }
748755
818825 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
819826 uint32_t Offset;
820827 if (is64Bit()) {
821 macho::Section64 Sect = getSection64(Sec);
822 Offset = Sect.RelocationTableOffset;
828 MachO::section_64 Sect = getSection64(Sec);
829 Offset = Sect.reloff;
823830 } else {
824 macho::Section Sect = getSection(Sec);
825 Offset = Sect.RelocationTableOffset;
831 MachO::section Sect = getSection(Sec);
832 Offset = Sect.reloff;
826833 }
827834
828835 DataRefImpl Ret;
835842 uint32_t Offset;
836843 uint32_t Num;
837844 if (is64Bit()) {
838 macho::Section64 Sect = getSection64(Sec);
839 Offset = Sect.RelocationTableOffset;
840 Num = Sect.NumRelocationTableEntries;
845 MachO::section_64 Sect = getSection64(Sec);
846 Offset = Sect.reloff;
847 Num = Sect.nreloc;
841848 } else {
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));
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));
849856
850857 DataRefImpl Ret;
851858 Ret.p = reinterpret_cast(P + Num);
854861
855862 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
856863 RelocationRef &Res) const {
857 const macho::RelocationEntry *P =
858 reinterpret_cast(Rel.p);
864 const MachO::any_relocation_info *P =
865 reinterpret_cast(Rel.p);
859866 Rel.p = reinterpret_cast(P + 1);
860867 Res = RelocationRef(Rel, this);
861868 return object_error::success;
868875
869876 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
870877 uint64_t &Res) const {
871 macho::RelocationEntry RE = getRelocation(Rel);
878 MachO::any_relocation_info RE = getRelocation(Rel);
872879 Res = getAnyRelocationAddress(RE);
873880 return object_error::success;
874881 }
875882
876883 symbol_iterator
877884 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
878 macho::RelocationEntry RE = getRelocation(Rel);
885 MachO::any_relocation_info RE = getRelocation(Rel);
879886 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
880887 bool isExtern = getPlainRelocationExternal(RE);
881888 if (!isExtern)
882889 return end_symbols();
883890
884 macho::SymtabLoadCommand S = getSymtabLoadCommand();
891 MachO::symtab_command S = getSymtabLoadCommand();
885892 unsigned SymbolTableEntrySize = is64Bit() ?
886 sizeof(macho::Symbol64TableEntry) :
887 sizeof(macho::SymbolTableEntry);
888 uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize;
893 sizeof(MachO::nlist_64) :
894 sizeof(MachO::nlist);
895 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
889896 DataRefImpl Sym;
890897 Sym.p = reinterpret_cast(getPtr(this, Offset));
891898 return symbol_iterator(SymbolRef(Sym, this));
893900
894901 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
895902 uint64_t &Res) const {
896 macho::RelocationEntry RE = getRelocation(Rel);
903 MachO::any_relocation_info RE = getRelocation(Rel);
897904 Res = getAnyRelocationType(RE);
898905 return object_error::success;
899906 }
9941001 error_code
9951002 MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
9961003 SmallVectorImpl &Result) const {
997 macho::RelocationEntry RE = getRelocation(Rel);
1004 MachO::any_relocation_info RE = getRelocation(Rel);
9981005
9991006 unsigned Arch = this->getArch();
10001007
10111018 bool isPCRel = getAnyRelocationPCRel(RE);
10121019
10131020 switch (Type) {
1014 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1015 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1021 case MachO::X86_64_RELOC_GOT_LOAD:
1022 case MachO::X86_64_RELOC_GOT: {
10161023 printRelocationTargetName(this, RE, fmt);
10171024 fmt << "@GOT";
10181025 if (isPCRel) fmt << "PCREL";
10191026 break;
10201027 }
1021 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1028 case MachO::X86_64_RELOC_SUBTRACTOR: {
10221029 DataRefImpl RelNext = Rel;
10231030 RelNext.d.a++;
1024 macho::RelocationEntry RENext = getRelocation(RelNext);
1025
1026 // X86_64_SUBTRACTOR must be followed by a relocation of type
1031 MachO::any_relocation_info RENext = getRelocation(RelNext);
1032
1033 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
10271034 // X86_64_RELOC_UNSIGNED.
10281035 // NOTE: Scattered relocations don't exist on x86_64.
10291036 unsigned RType = getAnyRelocationType(RENext);
1030 if (RType != 0)
1037 if (RType != MachO::X86_64_RELOC_UNSIGNED)
10311038 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
10321039 "X86_64_RELOC_SUBTRACTOR.");
10331040
1034 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1035 // X86_64_SUBTRACTOR contains to the subtrahend.
1041 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1042 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
10361043 printRelocationTargetName(this, RENext, fmt);
10371044 fmt << "-";
10381045 printRelocationTargetName(this, RE, fmt);
10391046 break;
10401047 }
1041 case macho::RIT_X86_64_TLV:
1048 case MachO::X86_64_RELOC_TLV:
10421049 printRelocationTargetName(this, RE, fmt);
10431050 fmt << "@TLV";
10441051 if (isPCRel) fmt << "P";
10451052 break;
1046 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1053 case MachO::X86_64_RELOC_SIGNED_1:
10471054 printRelocationTargetName(this, RE, fmt);
10481055 fmt << "-1";
10491056 break;
1050 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1057 case MachO::X86_64_RELOC_SIGNED_2:
10511058 printRelocationTargetName(this, RE, fmt);
10521059 fmt << "-2";
10531060 break;
1054 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1061 case MachO::X86_64_RELOC_SIGNED_4:
10551062 printRelocationTargetName(this, RE, fmt);
10561063 fmt << "-4";
10571064 break;
10641071 Arch == Triple::ppc) {
10651072 // Generic relocation types...
10661073 switch (Type) {
1067 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1074 case MachO::GENERIC_RELOC_PAIR: // prints no info
10681075 return object_error::success;
1069 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1076 case MachO::GENERIC_RELOC_SECTDIFF: {
10701077 DataRefImpl RelNext = Rel;
10711078 RelNext.d.a++;
1072 macho::RelocationEntry RENext = getRelocation(RelNext);
1079 MachO::any_relocation_info RENext = getRelocation(RelNext);
10731080
10741081 // X86 sect diff's must be followed by a relocation of type
10751082 // GENERIC_RELOC_PAIR.
10761083 unsigned RType = getAnyRelocationType(RENext);
10771084
1078 if (RType != 1)
1085 if (RType != MachO::GENERIC_RELOC_PAIR)
10791086 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
10801087 "GENERIC_RELOC_SECTDIFF.");
10811088
10871094 }
10881095
10891096 if (Arch == Triple::x86 || Arch == Triple::ppc) {
1090 // All X86 relocations that need special printing were already
1091 // handled in the generic code.
10921097 switch (Type) {
1093 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1098 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
10941099 DataRefImpl RelNext = Rel;
10951100 RelNext.d.a++;
1096 macho::RelocationEntry RENext = getRelocation(RelNext);
1101 MachO::any_relocation_info RENext = getRelocation(RelNext);
10971102
10981103 // X86 sect diff's must be followed by a relocation of type
10991104 // GENERIC_RELOC_PAIR.
11001105 unsigned RType = getAnyRelocationType(RENext);
1101 if (RType != 1)
1106 if (RType != MachO::GENERIC_RELOC_PAIR)
11021107 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
11031108 "GENERIC_RELOC_LOCAL_SECTDIFF.");
11041109
11071112 printRelocationTargetName(this, RENext, fmt);
11081113 break;
11091114 }
1110 case macho::RIT_Generic_TLV: {
1115 case MachO::GENERIC_RELOC_TLV: {
11111116 printRelocationTargetName(this, RE, fmt);
11121117 fmt << "@TLV";
11131118 if (IsPCRel) fmt << "P";
11181123 }
11191124 } else { // ARM-specific relocations
11201125 switch (Type) {
1121 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1122 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1126 case MachO::ARM_RELOC_HALF:
1127 case MachO::ARM_RELOC_HALF_SECTDIFF: {
11231128 // Half relocations steal a bit from the length field to encode
11241129 // whether this is an upper16 or a lower16 relocation.
11251130 bool isUpper = getAnyRelocationLength(RE) >> 1;
11321137
11331138 DataRefImpl RelNext = Rel;
11341139 RelNext.d.a++;
1135 macho::RelocationEntry RENext = getRelocation(RelNext);
1140 MachO::any_relocation_info RENext = getRelocation(RelNext);
11361141
11371142 // ARM half relocs must be followed by a relocation of type
11381143 // ARM_RELOC_PAIR.
11391144 unsigned RType = getAnyRelocationType(RENext);
1140 if (RType != 1)
1145 if (RType != MachO::ARM_RELOC_PAIR)
11411146 report_fatal_error("Expected ARM_RELOC_PAIR after "
1142 "GENERIC_RELOC_HALF");
1147 "ARM_RELOC_HALF");
11431148
11441149 // NOTE: The half of the target virtual address is stashed in the
11451150 // address field of the secondary relocation, but we can't reverse
11481153
11491154 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
11501155 // symbol/section pointer of the follow-on relocation.
1151 if (Type == macho::RIT_ARM_HalfDifference) {
1156 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
11521157 fmt << "-";
11531158 printRelocationTargetName(this, RENext, fmt);
11541159 }
11801185 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
11811186 // is always hidden.
11821187 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
1183 if (Type == macho::RIT_Pair) Result = true;
1188 if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
11841189 } else if (Arch == Triple::x86_64) {
11851190 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
11861191 // an X86_64_RELOC_SUBTRACTOR.
1187 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1192 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
11881193 DataRefImpl RelPrev = Rel;
11891194 RelPrev.d.a--;
11901195 uint64_t PrevType;
11911196 getRelocationType(RelPrev, PrevType);
1192 if (PrevType == macho::RIT_X86_64_Subtractor)
1197 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
11931198 Result = true;
11941199 }
11951200 }
12121217 if (!SymtabLoadCmd)
12131218 return symbol_iterator(SymbolRef(DRI, this));
12141219
1215 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1216 DRI.p = reinterpret_cast(getPtr(this, Symtab.SymbolTableOffset));
1220 MachO::symtab_command Symtab = getSymtabLoadCommand();
1221 DRI.p = reinterpret_cast(getPtr(this, Symtab.symoff));
12171222 return symbol_iterator(SymbolRef(DRI, this));
12181223 }
12191224
12221227 if (!SymtabLoadCmd)
12231228 return symbol_iterator(SymbolRef(DRI, this));
12241229
1225 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1230 MachO::symtab_command Symtab = getSymtabLoadCommand();
12261231 unsigned SymbolTableEntrySize = is64Bit() ?
1227 sizeof(macho::Symbol64TableEntry) :
1228 sizeof(macho::SymbolTableEntry);
1229 unsigned Offset = Symtab.SymbolTableOffset +
1230 Symtab.NumSymbolTableEntries * SymbolTableEntrySize;
1232 sizeof(MachO::nlist_64) :
1233 sizeof(MachO::nlist);
1234 unsigned Offset = Symtab.symoff +
1235 Symtab.nsyms * SymbolTableEntrySize;
12311236 DRI.p = reinterpret_cast(getPtr(this, Offset));
12321237 return symbol_iterator(SymbolRef(DRI, this));
12331238 }
13221327
13231328 StringRef MachOObjectFile::getLoadName() const {
13241329 // TODO: Implement
1325 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1330 report_fatal_error("Load name unimplemented in MachOObjectFile");
13261331 }
13271332
13281333 relocation_iterator MachOObjectFile::getSectionRelBegin(unsigned Index) const {
13421347 if (!DataInCodeLoadCmd)
13431348 return dice_iterator(DiceRef(DRI, this));
13441349
1345 macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand();
1346 DRI.p = reinterpret_cast(getPtr(this, DicLC.DataOffset));
1350 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1351 DRI.p = reinterpret_cast(getPtr(this, DicLC.dataoff));
13471352 return dice_iterator(DiceRef(DRI, this));
13481353 }
13491354
13521357 if (!DataInCodeLoadCmd)
13531358 return dice_iterator(DiceRef(DRI, this));
13541359
1355 macho::LinkeditDataLoadCommand DicLC = getDataInCodeLoadCommand();
1356 unsigned Offset = DicLC.DataOffset + DicLC.DataSize;
1360 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1361 unsigned Offset = DicLC.dataoff + DicLC.datasize;
13571362 DRI.p = reinterpret_cast(getPtr(this, Offset));
13581363 return dice_iterator(DiceRef(DRI, this));
13591364 }
13661371
13671372 ArrayRef
13681373 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1369 const SectionBase *Base =
1370 reinterpret_cast(Sections[Sec.d.a]);
1371 return ArrayRef(Base->Name);
1374 const section_base *Base =
1375 reinterpret_cast(Sections[Sec.d.a]);
1376 return ArrayRef(Base->sectname);
13721377 }
13731378
13741379 ArrayRef
13751380 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1376 const SectionBase *Base =
1377 reinterpret_cast(Sections[Sec.d.a]);
1378 return ArrayRef(Base->SegmentName);
1381 const section_base *Base =
1382 reinterpret_cast(Sections[Sec.d.a]);
1383 return ArrayRef(Base->segname);
13791384 }
13801385
13811386 bool
1382 MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE)
1387 MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
13831388 const {
1384 if (getCPUType(this) == llvm::MachO::CPU_TYPE_X86_64)
1389 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
13851390 return false;
1386 return getPlainRelocationAddress(RE) & macho::RF_Scattered;
1391 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
13871392 }
13881393
13891394 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
1390 const macho::RelocationEntry &RE) const {
1395 const MachO::any_relocation_info &RE) const {
13911396 if (isLittleEndian())
1392 return RE.Word1 & 0xffffff;
1393 return RE.Word1 >> 8;
1397 return RE.r_word1 & 0xffffff;
1398 return RE.r_word1 >> 8;
13941399 }
13951400
13961401 bool MachOObjectFile::getPlainRelocationExternal(
1397 const macho::RelocationEntry &RE) const {
1402 const MachO::any_relocation_info &RE) const {
13981403 if (isLittleEndian())
1399 return (RE.Word1 >> 27) & 1;
1400 return (RE.Word1 >> 4) & 1;
1404 return (RE.r_word1 >> 27) & 1;
1405 return (RE.r_word1 >> 4) & 1;
14011406 }
14021407
14031408 bool MachOObjectFile::getScatteredRelocationScattered(
1404 const macho::RelocationEntry &RE) const {
1405 return RE.Word0 >> 31;
1409 const MachO::any_relocation_info &RE) const {
1410 return RE.r_word0 >> 31;
14061411 }
14071412
14081413 uint32_t MachOObjectFile::getScatteredRelocationValue(
1409 const macho::RelocationEntry &RE) const {
1410 return RE.Word1;
1414 const MachO::any_relocation_info &RE) const {
1415 return RE.r_word1;
14111416 }
14121417
14131418 unsigned MachOObjectFile::getAnyRelocationAddress(
1414 const macho::RelocationEntry &RE) const {
1419 const MachO::any_relocation_info &RE) const {
14151420 if (isRelocationScattered(RE))
14161421 return getScatteredRelocationAddress(RE);
14171422 return getPlainRelocationAddress(RE);
14181423 }
14191424
1420 unsigned
1421 MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const {
1425 unsigned MachOObjectFile::getAnyRelocationPCRel(
1426 const MachO::any_relocation_info &RE) const {
14221427 if (isRelocationScattered(RE))
14231428 return getScatteredRelocationPCRel(this, RE);
14241429 return getPlainRelocationPCRel(this, RE);
14251430 }
14261431
14271432 unsigned MachOObjectFile::getAnyRelocationLength(
1428 const macho::RelocationEntry &RE) const {
1433 const MachO::any_relocation_info &RE) const {
14291434 if (isRelocationScattered(RE))
14301435 return getScatteredRelocationLength(RE);
14311436 return getPlainRelocationLength(this, RE);
14321437 }
14331438
14341439 unsigned
1435 MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const {
1440 MachOObjectFile::getAnyRelocationType(
1441 const MachO::any_relocation_info &RE) const {
14361442 if (isRelocationScattered(RE))
14371443 return getScatteredRelocationType(RE);
14381444 return getPlainRelocationType(this, RE);
14391445 }
14401446
14411447 SectionRef
1442 MachOObjectFile::getRelocationSection(const macho::RelocationEntry &RE) const {
1448 MachOObjectFile::getRelocationSection(
1449 const MachO::any_relocation_info &RE) const {
14431450 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
14441451 return *end_sections();
14451452 unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
14521459 MachOObjectFile::getFirstLoadCommandInfo() const {
14531460 MachOObjectFile::LoadCommandInfo Load;
14541461
1455 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
1462 unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1463 sizeof(MachO::mach_header);
14561464 Load.Ptr = getPtr(this, HeaderSize);
1457 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
1465 Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
14581466 return Load;
14591467 }
14601468
14611469 MachOObjectFile::LoadCommandInfo
14621470 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
14631471 MachOObjectFile::LoadCommandInfo Next;
1464 Next.Ptr = L.Ptr + L.C.Size;
1465 Next.C = getStruct(this, Next.Ptr);
1472 Next.Ptr = L.Ptr + L.C.cmdsize;
1473 Next.C = getStruct(this, Next.Ptr);
14661474 return Next;
14671475 }
14681476
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,
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,
14781486 unsigned Index) const {
14791487 const char *Sec = getSectionPtr(this, L, Index);
1480 return getStruct(this, Sec);
1481 }
1482
1483 macho::Section64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1484 unsigned Index) const {
1488 return getStruct(this, Sec);
1489 }
1490
1491 MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1492 unsigned Index) const {
14851493 const char *Sec = getSectionPtr(this, L, Index);
1486 return getStruct(this, Sec);
1487 }
1488
1489 macho::SymbolTableEntry
1494 return getStruct(this, Sec);
1495 }
1496
1497 MachO::nlist
14901498 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
14911499 const char *P = reinterpret_cast(DRI.p);
1492 return getStruct(this, P);
1493 }
1494
1495 macho::Symbol64TableEntry
1500 return getStruct(this, P);
1501 }
1502
1503 MachO::nlist_64
14961504 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
14971505 const char *P = reinterpret_cast(DRI.p);
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
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
15071515 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1508 return getStruct(this, L.Ptr);
1509 }
1510
1511 macho::Segment64LoadCommand
1516 return getStruct(this, L.Ptr);
1517 }
1518
1519 MachO::segment_command_64
15121520 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1513 return getStruct(this, L.Ptr);
1514 }
1515
1516 macho::LinkerOptionsLoadCommand
1521 return getStruct(this, L.Ptr);
1522 }
1523
1524 MachO::linker_options_command
15171525 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1518 return getStruct(this, L.Ptr);
1519 }
1520
1521 macho::RelocationEntry
1526 return getStruct(this, L.Ptr);
1527 }
1528
1529 MachO::any_relocation_info
15221530 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
15231531 const char *P = reinterpret_cast(Rel.p);
1524 return getStruct(this, P);
1525 }
1526
1527 macho::DataInCodeTableEntry
1532 return getStruct(this, P);
1533 }
1534
1535 MachO::data_in_code_entry
15281536 MachOObjectFile::getDice(DataRefImpl Rel) const {
15291537 const char *P = reinterpret_cast(Rel.p);
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
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
15511557 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
15521558 unsigned Index) const {
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
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
15661572 MachOObjectFile::getDataInCodeLoadCommand() const {
15671573 if (DataInCodeLoadCmd)
1568 return getStruct<macho::LinkeditDataLoadCommand>(this, DataInCodeLoadCmd);
1574 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
15691575
15701576 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
1571 macho::LinkeditDataLoadCommand Cmd;
1572 Cmd.Type = macho::LCT_DataInCode;
1573 Cmd.Size = macho::LinkeditLoadCommandSize;
1574 Cmd.DataOffset = 0;
1575 Cmd.DataSize = 0;
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;
15761582 return Cmd;
15771583 }
15781584
15791585 StringRef MachOObjectFile::getStringTableData() const {
1580 macho::SymtabLoadCommand S = getSymtabLoadCommand();
1581 return getData().substr(S.StringTableOffset, S.StringTableSize);
1586 MachO::symtab_command S = getSymtabLoadCommand();
1587 return getData().substr(S.stroff, S.strsize);
15821588 }
15831589
15841590 bool MachOObjectFile::is64Bit() const {
3030 static void SwapStruct(T &Value);
3131
3232 template<>
33 void SwapStruct(macho::FatHeader &H) {
34 SwapValue(H.Magic);
35 SwapValue(H.NumFatArch);
33 void SwapStruct(MachO::fat_header &H) {
34 SwapValue(H.magic);
35 SwapValue(H.nfat_arch);
3636 }
3737
3838 template<>
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);
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);
4545 }
4646
4747 template
6262 } else {
6363 // Parse object header.
6464 StringRef ParentData = Parent->getData();
65 const char *HeaderPos = ParentData.begin() + macho::FatHeaderSize +
66 Index * macho::FatArchHeaderSize;
67 Header = getUniversalBinaryStruct(HeaderPos);
68 if (ParentData.size() < Header.Offset + Header.Size) {
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) {
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() < macho::FatHeaderSize) {
98 if (Source->getBufferSize() < sizeof(MachO::fat_header)) {
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::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) {
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) {
109109 ec = object_error::parse_failed;
110110 return;
111111 }
112112 ec = object_error::success;
113113 }
114114
115 static bool getCTMForArch(Triple::ArchType Arch, mach::CPUTypeMachine &CTM) {
115 static bool getCTMForArch(Triple::ArchType Arch, MachO::CPUType &CTM) {
116116 switch (Arch) {
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;
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;
123123 default: return false;
124124 }
125125 }
127127 error_code
128128 MachOUniversalBinary::getObjectForArch(Triple::ArchType Arch,
129129 OwningPtr &Result) const {
130 mach::CPUTypeMachine CTM;
130 MachO::CPUType 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"
2827 #include "llvm/Support/ELF.h"
2928 #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 object::mach::CPUSubtypeARM Subtype;
642 const MachO::CPUSubTypeARM Subtype;
643643 DarwinARMAsmBackend(const Target &T, const StringRef TT,
644 object::mach::CPUSubtypeARM st)
644 MachO::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 object::mach::CTM_ARM,
651 MachO::CPU_TYPE_ARM,
652652 Subtype);
653653 }
654654
663663 Triple TheTriple(TT);
664664
665665 if (TheTriple.isOSDarwin()) {
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);
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);
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"
2322 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MachO.h"
2424 using namespace llvm;
25 using namespace llvm::object;
2625
2726 namespace {
2827 class ARMMachObjectWriter : public MCMachObjectTargetWriter {
6261
6362 static bool getARMFixupKindMachOInfo(unsigned Kind, unsigned &RelocType,
6463 unsigned &Log2Size) {
65 RelocType = unsigned(macho::RIT_Vanilla);
64 RelocType = unsigned(MachO::ARM_RELOC_VANILLA);
6665 Log2Size = ~0U;
6766
6867 switch (Kind) {
9190 case ARM::fixup_arm_uncondbl:
9291 case ARM::fixup_arm_condbl:
9392 case ARM::fixup_arm_blx:
94 RelocType = unsigned(macho::RIT_ARM_Branch24Bit);
93 RelocType = unsigned(MachO::ARM_RELOC_BR24);
9594 // Report as 'long', even though that is not quite accurate.
9695 Log2Size = llvm::Log2_32(4);
9796 return true;
9897
9998 // Handle Thumb branches.
10099 case ARM::fixup_arm_thumb_br:
101 RelocType = unsigned(macho::RIT_ARM_ThumbBranch22Bit);
100 RelocType = unsigned(MachO::ARM_THUMB_RELOC_BR22);
102101 Log2Size = llvm::Log2_32(2);
103102 return true;
104103
105104 case ARM::fixup_t2_uncondbranch:
106105 case ARM::fixup_arm_thumb_bl:
107106 case ARM::fixup_arm_thumb_blx:
108 RelocType = unsigned(macho::RIT_ARM_ThumbBranch22Bit);
107 RelocType = unsigned(MachO::ARM_THUMB_RELOC_BR22);
109108 Log2Size = llvm::Log2_32(4);
110109 return true;
111110
120119 // 1 - thumb instructions
121120 case ARM::fixup_arm_movt_hi16:
122121 case ARM::fixup_arm_movt_hi16_pcrel:
123 RelocType = unsigned(macho::RIT_ARM_Half);
122 RelocType = unsigned(MachO::ARM_RELOC_HALF);
124123 Log2Size = 1;
125124 return true;
126125 case ARM::fixup_t2_movt_hi16:
127126 case ARM::fixup_t2_movt_hi16_pcrel:
128 RelocType = unsigned(macho::RIT_ARM_Half);
127 RelocType = unsigned(MachO::ARM_RELOC_HALF);
129128 Log2Size = 3;
130129 return true;
131130
132131 case ARM::fixup_arm_movw_lo16:
133132 case ARM::fixup_arm_movw_lo16_pcrel:
134 RelocType = unsigned(macho::RIT_ARM_Half);
133 RelocType = unsigned(MachO::ARM_RELOC_HALF);
135134 Log2Size = 0;
136135 return true;
137136 case ARM::fixup_t2_movw_lo16:
138137 case ARM::fixup_t2_movw_lo16_pcrel:
139 RelocType = unsigned(macho::RIT_ARM_Half);
138 RelocType = unsigned(MachO::ARM_RELOC_HALF);
140139 Log2Size = 2;
141140 return true;
142141 }
152151 uint64_t &FixedValue) {
153152 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
154153 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
155 unsigned Type = macho::RIT_ARM_Half;
154 unsigned Type = MachO::ARM_RELOC_HALF;
156155
157156 // See .
158157 const MCSymbol *A = &Target.getSymA()->getSymbol();
178177 "' can not be undefined in a subtraction expression");
179178
180179 // Select the appropriate difference relocation type.
181 Type = macho::RIT_ARM_HalfDifference;
180 Type = MachO::ARM_RELOC_HALF_SECTDIFF;
182181 Value2 = Writer->getSymbolAddress(B_SD, Layout);
183182 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
184183 }
222221 break;
223222 }
224223
225 if (Type == macho::RIT_ARM_HalfDifference) {
224 MachO::scattered_relocation_info MRE;
225 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
226226 uint32_t OtherHalf = MovtBit
227227 ? (FixedValue & 0xffff) : ((FixedValue & 0xffff0000) >> 16);
228228
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;
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;
237236 Writer->addRelocation(Fragment->getParent(), MRE);
238237 }
239238
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;
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;
248246 Writer->addRelocation(Fragment->getParent(), MRE);
249247 }
250248
258256 uint64_t &FixedValue) {
259257 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
260258 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
261 unsigned Type = macho::RIT_Vanilla;
259 unsigned Type = MachO::ARM_RELOC_VANILLA;
262260
263261 // See .
264262 const MCSymbol *A = &Target.getSymA()->getSymbol();
283281 "' can not be undefined in a subtraction expression");
284282
285283 // Select the appropriate difference relocation type.
286 Type = macho::RIT_Difference;
284 Type = MachO::ARM_RELOC_SECTDIFF;
287285 Value2 = Writer->getSymbolAddress(B_SD, Layout);
288286 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
289287 }
290288
289 MachO::scattered_relocation_info MRE;
291290 // Relocations are written out in reverse order, so the PAIR comes first.
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;
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;
301299 Writer->addRelocation(Fragment->getParent(), MRE);
302300 }
303301
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;
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;
311308 Writer->addRelocation(Fragment->getParent(), MRE);
312309 }
313310
325322 switch (RelocType) {
326323 default:
327324 return false;
328 case macho::RIT_ARM_Branch24Bit:
325 case MachO::ARM_RELOC_BR24:
329326 // PC pre-adjustment of 8 for these instructions.
330327 Value -= 8;
331328 // ARM BL/BLX has a 25-bit offset.
332329 Range = 0x1ffffff;
333330 break;
334 case macho::RIT_ARM_ThumbBranch22Bit:
331 case MachO::ARM_THUMB_RELOC_BR22:
335332 // PC pre-adjustment of 4 for these instructions.
336333 Value -= 4;
337334 // Thumb BL/BLX has a 24-bit offset.
360357 uint64_t &FixedValue) {
361358 unsigned IsPCRel = Writer->isFixupKindPCRel(Asm, Fixup.getKind());
362359 unsigned Log2Size;
363 unsigned RelocType = macho::RIT_Vanilla;
360 unsigned RelocType = MachO::ARM_RELOC_VANILLA;
364361 if (!getARMFixupKindMachOInfo(Fixup.getKind(), RelocType, Log2Size))
365362 // If we failed to get fixup kind info, it's because there's no legal
366363 // relocation type for the fixup kind. This happens when it's a fixup that's
373370 // scattered relocation entry. Differences always require scattered
374371 // relocations.
375372 if (Target.getSymB()) {
376 if (RelocType == macho::RIT_ARM_Half)
373 if (RelocType == MachO::ARM_RELOC_HALF)
377374 return RecordARMScatteredHalfRelocation(Writer, Asm, Layout, Fragment,
378375 Fixup, Target, FixedValue);
379376 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
391388 //
392389 // Is this right for ARM?
393390 uint32_t Offset = Target.getConstant();
394 if (IsPCRel && RelocType == macho::RIT_Vanilla)
391 if (IsPCRel && RelocType == MachO::ARM_RELOC_VANILLA)
395392 Offset += 1 << Log2Size;
396393 if (Offset && SD && !Writer->doesSymbolRequireExternRelocation(SD))
397394 return RecordARMScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup,
444441 }
445442
446443 // struct relocation_info (8 bytes)
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));
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;
454451
455452 // Even when it's not a scattered relocation, movw/movt always uses
456453 // a PAIR relocation.
457 if (Type == macho::RIT_ARM_Half) {
454 if (Type == MachO::ARM_RELOC_HALF) {
458455 // The other-half value only gets populated for the movt and movw
459456 // relocation entries.
460457 uint32_t Value = 0;
473470 Value = FixedValue & 0xffff;
474471 break;
475472 }
476 macho::RelocationEntry MREPair;
477 MREPair.Word0 = Value;
478 MREPair.Word1 = ((0xffffff) |
479 (Log2Size << 25) |
480 (macho::RIT_Pair << 28));
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;
481479
482480 Writer->addRelocation(Fragment->getParent(), MREPair);
483481 }
1515 #include "llvm/MC/MCObjectWriter.h"
1616 #include "llvm/MC/MCSectionMachO.h"
1717 #include "llvm/MC/MCValue.h"
18 #include "llvm/Object/MachOFormat.h"
1918 #include "llvm/Support/ELF.h"
2019 #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 ? object::mach::CTM_PowerPC64 : object::mach::CTM_PowerPC),
167 object::mach::CSPPC_ALL);
166 (is64 ? MachO::CPU_TYPE_POWERPC64 : MachO::CPU_TYPE_POWERPC),
167 MachO::CPU_SUBTYPE_POWERPC_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"
1918 #include "llvm/Support/ErrorHandling.h"
2019 #include "llvm/Support/Format.h"
20 #include "llvm/Support/MachO.h"
2121
2222 using namespace llvm;
23 using namespace llvm::object;
2423
2524 namespace {
2625 class PPCMachObjectWriter : public MCMachObjectTargetWriter {
8988 Target.isAbsolute() ? MCSymbolRefExpr::VK_None
9089 : Target.getSymA()->getKind();
9190 // determine the type of the relocation
92 unsigned Type = macho::RIT_Vanilla;
91 unsigned Type = MachO::GENERIC_RELOC_VANILLA;
9392 if (IsPCRel) { // relative to PC
9493 switch ((unsigned)FixupKind) {
9594 default:
9695 report_fatal_error("Unimplemented fixup kind (relative)");
9796 case PPC::fixup_ppc_br24:
98 Type = macho::RIT_PPC_BR24; // R_PPC_REL24
97 Type = MachO::PPC_RELOC_BR24; // R_PPC_REL24
9998 break;
10099 case PPC::fixup_ppc_brcond14:
101 Type = macho::RIT_PPC_BR14;
100 Type = MachO::PPC_RELOC_BR14;
102101 break;
103102 case PPC::fixup_ppc_half16:
104103 switch (Modifier) {
105104 default:
106105 llvm_unreachable("Unsupported modifier for half16 fixup");
107106 case MCSymbolRefExpr::VK_PPC_HA:
108 Type = macho::RIT_PPC_HA16;
107 Type = MachO::PPC_RELOC_HA16;
109108 break;
110109 case MCSymbolRefExpr::VK_PPC_LO:
111 Type = macho::RIT_PPC_LO16;
110 Type = MachO::PPC_RELOC_LO16;
112111 break;
113112 case MCSymbolRefExpr::VK_PPC_HI:
114 Type = macho::RIT_PPC_HI16;
113 Type = MachO::PPC_RELOC_HI16;
115114 break;
116115 }
117116 break;
125124 default:
126125 llvm_unreachable("Unsupported modifier for half16 fixup");
127126 case MCSymbolRefExpr::VK_PPC_HA:
128 Type = macho::RIT_PPC_HA16_SECTDIFF;
127 Type = MachO::PPC_RELOC_HA16_SECTDIFF;
129128 break;
130129 case MCSymbolRefExpr::VK_PPC_LO:
131 Type = macho::RIT_PPC_LO16_SECTDIFF;
130 Type = MachO::PPC_RELOC_LO16_SECTDIFF;
132131 break;
133132 case MCSymbolRefExpr::VK_PPC_HI:
134 Type = macho::RIT_PPC_HI16_SECTDIFF;
133 Type = MachO::PPC_RELOC_HI16_SECTDIFF;
135134 break;
136135 }
137136 break;
144143 return Type;
145144 }
146145
147 static void makeRelocationInfo(macho::RelocationEntry &MRE,
146 static void makeRelocationInfo(MachO::any_relocation_info &MRE,
148147 const uint32_t FixupOffset, const uint32_t Index,
149148 const unsigned IsPCRel, const unsigned Log2Size,
150149 const unsigned IsExtern, const unsigned Type) {
151 MRE.Word0 = FixupOffset;
150 MRE.r_word0 = FixupOffset;
152151 // The bitfield offsets that work (as determined by trial-and-error)
153152 // are different than what is documented in the mach-o manuals.
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
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
160161 }
161162
162163 static void
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;
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;
171174 }
172175
173176 /// Compute fixup offset (address).
222225 report_fatal_error("symbol '" + B->getSymbol().getName() +
223226 "' can not be undefined in a subtraction expression");
224227
225 // FIXME: is Type correct? see include/llvm/Object/MachOFormat.h
228 // FIXME: is Type correct? see include/llvm/Support/MachO.h
226229 Value2 = Writer->getSymbolAddress(B_SD, Layout);
227230 FixedValue -= Writer->getSectionAddress(B_SD->getFragment()->getParent());
228231 }
229232 // FIXME: does FixedValue get used??
230233
231234 // Relocations are written out in reverse order, so the PAIR comes first.
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) {
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) {
237241 // X86 had this piece, but ARM does not
238242 // If the offset is too large to fit in a scattered relocation,
239243 // we're hosed. It's an unfortunate limitation of the MachO format.
252256 // see PPCMCExpr::EvaluateAsRelocatableImpl()
253257 uint32_t other_half = 0;
254258 switch (Type) {
255 case macho::RIT_PPC_LO16_SECTDIFF:
259 case MachO::PPC_RELOC_LO16_SECTDIFF:
256260 other_half = (FixedValue >> 16) & 0xffff;
257261 // applyFixupOffset longer extracts the high part because it now assumes
258262 // this was already done.
261265 // So we need to adjust FixedValue again here.
262266 FixedValue &= 0xffff;
263267 break;
264 case macho::RIT_PPC_HA16_SECTDIFF:
268 case MachO::PPC_RELOC_HA16_SECTDIFF:
265269 other_half = FixedValue & 0xffff;
266270 FixedValue =
267271 ((FixedValue >> 16) + ((FixedValue & 0x8000) ? 1 : 0)) & 0xffff;
268272 break;
269 case macho::RIT_PPC_HI16_SECTDIFF:
273 case MachO::PPC_RELOC_HI16_SECTDIFF:
270274 other_half = FixedValue & 0xffff;
271275 FixedValue = (FixedValue >> 16) & 0xffff;
272276 break;
275279 break;
276280 }
277281
278 macho::RelocationEntry MRE;
279 makeScatteredRelocationInfo(MRE, other_half, macho::RIT_Pair, Log2Size,
280 IsPCRel, Value2);
282 MachO::scattered_relocation_info MRE;
283 makeScatteredRelocationInfo(MRE, other_half, MachO::GENERIC_RELOC_PAIR,
284 Log2Size, IsPCRel, Value2);
281285 Writer->addRelocation(Fragment->getParent(), MRE);
282286 } else {
283287 // If the offset is more than 24-bits, it won't fit in a scattered
290294 if (FixupOffset > 0xffffff)
291295 return false;
292296 }
293 macho::RelocationEntry MRE;
297 MachO::scattered_relocation_info MRE;
294298 makeScatteredRelocationInfo(MRE, FixupOffset, Type, Log2Size, IsPCRel, Value);
295299 Writer->addRelocation(Fragment->getParent(), MRE);
296300 return true;
311315 // relocations.
312316 if (Target.getSymB() &&
313317 // Q: are branch targets ever scattered?
314 RelocType != macho::RIT_PPC_BR24 && RelocType != macho::RIT_PPC_BR14) {
318 RelocType != MachO::PPC_RELOC_BR24 &&
319 RelocType != MachO::PPC_RELOC_BR14) {
315320 RecordScatteredRelocation(Writer, Asm, Layout, Fragment, Fixup, Target,
316321 Log2Size, FixedValue);
317322 return;
368373 FixedValue -= Writer->getSectionAddress(Fragment->getParent());
369374 }
370375
371 // struct relocation_info (8 bytes)
372 macho::RelocationEntry MRE;
376 MachO::any_relocation_info MRE;
373377 makeRelocationInfo(MRE, FixupOffset, Index, IsPCRel, Log2Size, IsExtern,
374378 Type);
375379 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"
2221 #include "llvm/Support/CommandLine.h"
2322 #include "llvm/Support/ELF.h"
2423 #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 object::mach::CTM_i386,
398 object::mach::CSX86_ALL);
397 MachO::CPU_TYPE_I386,
398 MachO::CPU_SUBTYPE_I386_ALL);
399399 }
400400 };
401401
408408
409409 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
410410 return createX86MachObjectWriter(OS, /*Is64Bit=*/true,
411 object::mach::CTM_x86_64,
412 object::mach::CSX86_ALL);
411 MachO::CPU_TYPE_X86_64,
412 MachO::CPU_SUBTYPE_X86_64_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 RelocationEntry RE = Obj->getRelocation(Rel.getRawDataRefImpl());
35 any_relocation_info 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 RIT_X86_64_TLV:
45 case X86_64_RELOC_TLV:
4646 Expr = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_TLVP, Ctx);
4747 break;
48 case RIT_X86_64_Signed4:
48 case X86_64_RELOC_SIGNED_4:
4949 Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, Ctx),
5050 MCConstantExpr::Create(4, Ctx),
5151 Ctx);
5252 break;
53 case RIT_X86_64_Signed2:
53 case X86_64_RELOC_SIGNED_2:
5454 Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, Ctx),
5555 MCConstantExpr::Create(2, Ctx),
5656 Ctx);
5757 break;
58 case RIT_X86_64_Signed1:
58 case X86_64_RELOC_SIGNED_1:
5959 Expr = MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, Ctx),
6060 MCConstantExpr::Create(1, Ctx),
6161 Ctx);
6262 break;
63 case RIT_X86_64_GOTLoad:
63 case X86_64_RELOC_GOT_LOAD:
6464 Expr = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, Ctx);
6565 break;
66 case RIT_X86_64_GOT:
66 case X86_64_RELOC_GOT:
6767 Expr = MCSymbolRefExpr::Create(Sym, isPCRel ?
6868 MCSymbolRefExpr::VK_GOTPCREL :
6969 MCSymbolRefExpr::VK_GOT,
7070 Ctx);
7171 break;
72 case RIT_X86_64_Subtractor:
72 case X86_64_RELOC_SUBTRACTOR:
7373 {
7474 RelocationRef RelNext;
7575 Obj->getRelocationNext(Rel.getRawDataRefImpl(), RelNext);
76 RelocationEntry RENext = Obj->getRelocation(RelNext.getRawDataRefImpl());
76 any_relocation_info 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 // NOTE: Scattered relocations don't exist on x86_64.
8181 unsigned RType = Obj->getAnyRelocationType(RENext);
82 if (RType != RIT_X86_64_Unsigned)
82 if (RType != X86_64_RELOC_UNSIGNED)
8383 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
8484 "X86_64_RELOC_SUBTRACTOR.");
8585
1515 #include "llvm/MC/MCMachObjectWriter.h"
1616 #include "llvm/MC/MCSectionMachO.h"
1717 #include "llvm/MC/MCValue.h"
18 #include "llvm/Object/MachOFormat.h"
1918 #include "llvm/Support/ErrorHandling.h"
2019 #include "llvm/Support/Format.h"
20 #include "llvm/Support/MachO.h"
2121
2222 using namespace llvm;
23 using namespace llvm::object;
2423
2524 namespace {
2625 class X86MachObjectWriter : public MCMachObjectTargetWriter {
131130
132