blob: a70f1c3d2247b7a74ec051a3d6b573651b9c13de [file] [log] [blame]
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001//===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000010#include "llvm/ADT/StringMap.h"
11#include "llvm/ADT/Twine.h"
12#include "llvm/MC/MCAssembler.h"
Daniel Dunbar207e06e2010-03-24 03:43:40 +000013#include "llvm/MC/MCAsmLayout.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000014#include "llvm/MC/MCExpr.h"
15#include "llvm/MC/MCObjectWriter.h"
16#include "llvm/MC/MCSectionMachO.h"
17#include "llvm/MC/MCSymbol.h"
Kevin Enderbya6eeb6e2010-05-07 21:44:23 +000018#include "llvm/MC/MCMachOSymbolFlags.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000019#include "llvm/MC/MCValue.h"
Daniel Dunbar821ecd72010-11-27 04:19:38 +000020#include "llvm/Object/MachOFormat.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000021#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000022#include "llvm/Target/TargetAsmBackend.h"
23
24// FIXME: Gross.
25#include "../Target/X86/X86FixupKinds.h"
26
27#include <vector>
28using namespace llvm;
Daniel Dunbar821ecd72010-11-27 04:19:38 +000029using namespace llvm::object;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000030
Rafael Espindolaa8c02c32010-09-30 03:11:42 +000031// FIXME: this has been copied from (or to) X86AsmBackend.cpp
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000032static unsigned getFixupKindLog2Size(unsigned Kind) {
33 switch (Kind) {
34 default: llvm_unreachable("invalid fixup kind!");
35 case X86::reloc_pcrel_1byte:
36 case FK_Data_1: return 0;
Chris Lattnerda3051a2010-07-07 22:35:13 +000037 case X86::reloc_pcrel_2byte:
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000038 case FK_Data_2: return 1;
39 case X86::reloc_pcrel_4byte:
40 case X86::reloc_riprel_4byte:
Daniel Dunbar602b40f2010-03-19 18:07:55 +000041 case X86::reloc_riprel_4byte_movq_load:
Rafael Espindolaa8c02c32010-09-30 03:11:42 +000042 case X86::reloc_signed_4byte:
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000043 case FK_Data_4: return 2;
44 case FK_Data_8: return 3;
45 }
46}
47
48static bool isFixupKindPCRel(unsigned Kind) {
49 switch (Kind) {
50 default:
51 return false;
52 case X86::reloc_pcrel_1byte:
Chris Lattnerda3051a2010-07-07 22:35:13 +000053 case X86::reloc_pcrel_2byte:
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000054 case X86::reloc_pcrel_4byte:
55 case X86::reloc_riprel_4byte:
Daniel Dunbar602b40f2010-03-19 18:07:55 +000056 case X86::reloc_riprel_4byte_movq_load:
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000057 return true;
58 }
59}
60
Daniel Dunbar602b40f2010-03-19 18:07:55 +000061static bool isFixupKindRIPRel(unsigned Kind) {
62 return Kind == X86::reloc_riprel_4byte ||
63 Kind == X86::reloc_riprel_4byte_movq_load;
64}
65
Daniel Dunbare9460ec2010-05-10 23:15:13 +000066static bool doesSymbolRequireExternRelocation(MCSymbolData *SD) {
67 // Undefined symbols are always extern.
68 if (SD->Symbol->isUndefined())
69 return true;
70
71 // References to weak definitions require external relocation entries; the
72 // definition may not always be the one in the same object file.
73 if (SD->getFlags() & SF_WeakDefinition)
74 return true;
75
76 // Otherwise, we can use an internal relocation.
77 return false;
78}
79
Rafael Espindola70703872010-09-30 02:22:20 +000080static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
81 const MCValue Target,
82 const MCSymbolData *BaseSymbol) {
83 // The effective fixup address is
84 // addr(atom(A)) + offset(A)
85 // - addr(atom(B)) - offset(B)
86 // - addr(BaseSymbol) + <fixup offset from base symbol>
87 // and the offsets are not relocatable, so the fixup is fully resolved when
88 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
89 //
90 // Note that "false" is almost always conservatively correct (it means we emit
91 // a relocation which is unnecessary), except when it would force us to emit a
92 // relocation which the target cannot encode.
93
94 const MCSymbolData *A_Base = 0, *B_Base = 0;
95 if (const MCSymbolRefExpr *A = Target.getSymA()) {
96 // Modified symbol references cannot be resolved.
97 if (A->getKind() != MCSymbolRefExpr::VK_None)
98 return false;
99
100 A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
101 if (!A_Base)
102 return false;
103 }
104
105 if (const MCSymbolRefExpr *B = Target.getSymB()) {
106 // Modified symbol references cannot be resolved.
107 if (B->getKind() != MCSymbolRefExpr::VK_None)
108 return false;
109
110 B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
111 if (!B_Base)
112 return false;
113 }
114
115 // If there is no base, A and B have to be the same atom for this fixup to be
116 // fully resolved.
117 if (!BaseSymbol)
118 return A_Base == B_Base;
119
120 // Otherwise, B must be missing and A must be the base.
121 return !B_Base && BaseSymbol == A_Base;
122}
123
124static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
125 const MCValue Target,
126 const MCSection *BaseSection) {
127 // The effective fixup address is
128 // addr(atom(A)) + offset(A)
129 // - addr(atom(B)) - offset(B)
130 // - addr(<base symbol>) + <fixup offset from base symbol>
131 // and the offsets are not relocatable, so the fixup is fully resolved when
132 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
133 //
134 // The simple (Darwin, except on x86_64) way of dealing with this was to
135 // assume that any reference to a temporary symbol *must* be a temporary
136 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
137 // relocation to a temporary symbol (in the same section) is fully
138 // resolved. This also works in conjunction with absolutized .set, which
139 // requires the compiler to use .set to absolutize the differences between
140 // symbols which the compiler knows to be assembly time constants, so we don't
141 // need to worry about considering symbol differences fully resolved.
142
143 // Non-relative fixups are only resolved if constant.
144 if (!BaseSection)
145 return Target.isAbsolute();
146
147 // Otherwise, relative fixups are only resolved if not a difference and the
148 // target is a temporary in the same section.
149 if (Target.isAbsolute() || Target.getSymB())
150 return false;
151
152 const MCSymbol *A = &Target.getSymA()->getSymbol();
153 if (!A->isTemporary() || !A->isInSection() ||
154 &A->getSection() != BaseSection)
155 return false;
156
157 return true;
158}
159
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000160namespace {
161
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000162class MachObjectWriter : public MCObjectWriter {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000163 /// MachSymbolData - Helper struct for containing some precomputed information
164 /// on symbols.
165 struct MachSymbolData {
166 MCSymbolData *SymbolData;
167 uint64_t StringIndex;
168 uint8_t SectionIndex;
169
170 // Support lexicographic sorting.
171 bool operator<(const MachSymbolData &RHS) const {
Benjamin Kramerc37791e2010-05-20 14:14:22 +0000172 return SymbolData->getSymbol().getName() <
173 RHS.SymbolData->getSymbol().getName();
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000174 }
175 };
176
177 /// @name Relocation Data
178 /// @{
179
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000180 llvm::DenseMap<const MCSectionData*,
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000181 std::vector<macho::RelocationEntry> > Relocations;
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000182 llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000183
184 /// @}
185 /// @name Symbol Table Data
186 /// @{
187
188 SmallString<256> StringTable;
189 std::vector<MachSymbolData> LocalSymbolData;
190 std::vector<MachSymbolData> ExternalSymbolData;
191 std::vector<MachSymbolData> UndefinedSymbolData;
192
193 /// @}
194
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000195 unsigned Is64Bit : 1;
196
Jim Grosbachc9d14392010-11-05 18:48:58 +0000197 uint32_t CPUType;
198 uint32_t CPUSubtype;
199
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000200public:
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000201 MachObjectWriter(raw_ostream &_OS,
202 bool _Is64Bit, uint32_t _CPUType, uint32_t _CPUSubtype,
203 bool _IsLittleEndian)
204 : MCObjectWriter(_OS, _IsLittleEndian),
205 Is64Bit(_Is64Bit), CPUType(_CPUType), CPUSubtype(_CPUSubtype) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000206 }
207
208 void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
209 bool SubsectionsViaSymbols) {
210 uint32_t Flags = 0;
211
212 if (SubsectionsViaSymbols)
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000213 Flags |= macho::HF_SubsectionsViaSymbols;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000214
215 // struct mach_header (28 bytes) or
216 // struct mach_header_64 (32 bytes)
217
218 uint64_t Start = OS.tell();
219 (void) Start;
220
Daniel Dunbar821ecd72010-11-27 04:19:38 +0000221 Write32(Is64Bit ? macho::HM_Object64 : macho::HM_Object32);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000222
Jim Grosbachc9d14392010-11-05 18:48:58 +0000223 Write32(CPUType);
224 Write32(CPUSubtype);
225
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000226 Write32(macho::HFT_Object);
Daniel Dunbar590956f2010-11-27 07:39:37 +0000227 Write32(NumLoadCommands);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000228 Write32(LoadCommandsSize);
229 Write32(Flags);
230 if (Is64Bit)
231 Write32(0); // reserved
232
Daniel Dunbar821ecd72010-11-27 04:19:38 +0000233 assert(OS.tell() - Start == Is64Bit ?
234 macho::Header64Size : macho::Header32Size);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000235 }
236
237 /// WriteSegmentLoadCommand - Write a segment load command.
238 ///
239 /// \arg NumSections - The number of sections in this segment.
240 /// \arg SectionDataSize - The total size of the sections.
241 void WriteSegmentLoadCommand(unsigned NumSections,
242 uint64_t VMSize,
243 uint64_t SectionDataStartOffset,
244 uint64_t SectionDataSize) {
245 // struct segment_command (56 bytes) or
246 // struct segment_command_64 (72 bytes)
247
248 uint64_t Start = OS.tell();
249 (void) Start;
250
Daniel Dunbar821ecd72010-11-27 04:19:38 +0000251 unsigned SegmentLoadCommandSize = Is64Bit ? macho::SegmentLoadCommand64Size:
252 macho::SegmentLoadCommand32Size;
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000253 Write32(Is64Bit ? macho::LCT_Segment64 : macho::LCT_Segment);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000254 Write32(SegmentLoadCommandSize +
Daniel Dunbar821ecd72010-11-27 04:19:38 +0000255 NumSections * (Is64Bit ? macho::Section64Size :
256 macho::Section32Size));
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000257
258 WriteBytes("", 16);
259 if (Is64Bit) {
260 Write64(0); // vmaddr
261 Write64(VMSize); // vmsize
262 Write64(SectionDataStartOffset); // file offset
263 Write64(SectionDataSize); // file size
264 } else {
265 Write32(0); // vmaddr
266 Write32(VMSize); // vmsize
267 Write32(SectionDataStartOffset); // file offset
268 Write32(SectionDataSize); // file size
269 }
270 Write32(0x7); // maxprot
271 Write32(0x7); // initprot
272 Write32(NumSections);
273 Write32(0); // flags
274
275 assert(OS.tell() - Start == SegmentLoadCommandSize);
276 }
277
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000278 void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
279 const MCSectionData &SD, uint64_t FileOffset,
280 uint64_t RelocationsStart, unsigned NumRelocations) {
Daniel Dunbar5d428512010-03-25 02:00:07 +0000281 uint64_t SectionSize = Layout.getSectionSize(&SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000282
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000283 // The offset is unused for virtual sections.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000284 if (SD.getSection().isVirtualSection()) {
Daniel Dunbarb026d642010-03-25 07:10:05 +0000285 assert(Layout.getSectionFileSize(&SD) == 0 && "Invalid file size!");
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000286 FileOffset = 0;
287 }
288
289 // struct section (68 bytes) or
290 // struct section_64 (80 bytes)
291
292 uint64_t Start = OS.tell();
293 (void) Start;
294
Daniel Dunbar56279f42010-05-18 17:28:20 +0000295 const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection());
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000296 WriteBytes(Section.getSectionName(), 16);
297 WriteBytes(Section.getSegmentName(), 16);
298 if (Is64Bit) {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000299 Write64(Layout.getSectionAddress(&SD)); // address
Daniel Dunbar5d428512010-03-25 02:00:07 +0000300 Write64(SectionSize); // size
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000301 } else {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000302 Write32(Layout.getSectionAddress(&SD)); // address
Daniel Dunbar5d428512010-03-25 02:00:07 +0000303 Write32(SectionSize); // size
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000304 }
305 Write32(FileOffset);
306
307 unsigned Flags = Section.getTypeAndAttributes();
308 if (SD.hasInstructions())
309 Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
310
311 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
312 Write32(Log2_32(SD.getAlignment()));
313 Write32(NumRelocations ? RelocationsStart : 0);
314 Write32(NumRelocations);
315 Write32(Flags);
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000316 Write32(IndirectSymBase.lookup(&SD)); // reserved1
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000317 Write32(Section.getStubSize()); // reserved2
318 if (Is64Bit)
319 Write32(0); // reserved3
320
Daniel Dunbar821ecd72010-11-27 04:19:38 +0000321 assert(OS.tell() - Start == Is64Bit ? macho::Section64Size :
322 macho::Section32Size);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000323 }
324
325 void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
326 uint32_t StringTableOffset,
327 uint32_t StringTableSize) {
328 // struct symtab_command (24 bytes)
329
330 uint64_t Start = OS.tell();
331 (void) Start;
332
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000333 Write32(macho::LCT_Symtab);
Daniel Dunbar821ecd72010-11-27 04:19:38 +0000334 Write32(macho::SymtabLoadCommandSize);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000335 Write32(SymbolOffset);
336 Write32(NumSymbols);
337 Write32(StringTableOffset);
338 Write32(StringTableSize);
339
Daniel Dunbar821ecd72010-11-27 04:19:38 +0000340 assert(OS.tell() - Start == macho::SymtabLoadCommandSize);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000341 }
342
343 void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
344 uint32_t NumLocalSymbols,
345 uint32_t FirstExternalSymbol,
346 uint32_t NumExternalSymbols,
347 uint32_t FirstUndefinedSymbol,
348 uint32_t NumUndefinedSymbols,
349 uint32_t IndirectSymbolOffset,
350 uint32_t NumIndirectSymbols) {
351 // struct dysymtab_command (80 bytes)
352
353 uint64_t Start = OS.tell();
354 (void) Start;
355
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000356 Write32(macho::LCT_Dysymtab);
Daniel Dunbar821ecd72010-11-27 04:19:38 +0000357 Write32(macho::DysymtabLoadCommandSize);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000358 Write32(FirstLocalSymbol);
359 Write32(NumLocalSymbols);
360 Write32(FirstExternalSymbol);
361 Write32(NumExternalSymbols);
362 Write32(FirstUndefinedSymbol);
363 Write32(NumUndefinedSymbols);
364 Write32(0); // tocoff
365 Write32(0); // ntoc
366 Write32(0); // modtaboff
367 Write32(0); // nmodtab
368 Write32(0); // extrefsymoff
369 Write32(0); // nextrefsyms
370 Write32(IndirectSymbolOffset);
371 Write32(NumIndirectSymbols);
372 Write32(0); // extreloff
373 Write32(0); // nextrel
374 Write32(0); // locreloff
375 Write32(0); // nlocrel
376
Daniel Dunbar821ecd72010-11-27 04:19:38 +0000377 assert(OS.tell() - Start == macho::DysymtabLoadCommandSize);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000378 }
379
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000380 void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000381 MCSymbolData &Data = *MSD.SymbolData;
382 const MCSymbol &Symbol = Data.getSymbol();
383 uint8_t Type = 0;
384 uint16_t Flags = Data.getFlags();
385 uint32_t Address = 0;
386
387 // Set the N_TYPE bits. See <mach-o/nlist.h>.
388 //
389 // FIXME: Are the prebound or indirect fields possible here?
390 if (Symbol.isUndefined())
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000391 Type = macho::STT_Undefined;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000392 else if (Symbol.isAbsolute())
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000393 Type = macho::STT_Absolute;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000394 else
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000395 Type = macho::STT_Section;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000396
397 // FIXME: Set STAB bits.
398
399 if (Data.isPrivateExtern())
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000400 Type |= macho::STF_PrivateExtern;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000401
402 // Set external bit.
403 if (Data.isExternal() || Symbol.isUndefined())
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000404 Type |= macho::STF_External;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000405
406 // Compute the symbol address.
407 if (Symbol.isDefined()) {
408 if (Symbol.isAbsolute()) {
Daniel Dunbar2d7fd612010-05-05 19:01:05 +0000409 Address = cast<MCConstantExpr>(Symbol.getVariableValue())->getValue();
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000410 } else {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000411 Address = Layout.getSymbolAddress(&Data);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000412 }
413 } else if (Data.isCommon()) {
414 // Common symbols are encoded with the size in the address
415 // field, and their alignment in the flags.
416 Address = Data.getCommonSize();
417
418 // Common alignment is packed into the 'desc' bits.
419 if (unsigned Align = Data.getCommonAlignment()) {
420 unsigned Log2Size = Log2_32(Align);
421 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
422 if (Log2Size > 15)
Chris Lattner75361b62010-04-07 22:58:41 +0000423 report_fatal_error("invalid 'common' alignment '" +
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000424 Twine(Align) + "'");
425 // FIXME: Keep this mask with the SymbolFlags enumeration.
426 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
427 }
428 }
429
430 // struct nlist (12 bytes)
431
432 Write32(MSD.StringIndex);
433 Write8(Type);
434 Write8(MSD.SectionIndex);
435
436 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
437 // value.
438 Write16(Flags);
439 if (Is64Bit)
440 Write64(Address);
441 else
442 Write32(Address);
443 }
444
Daniel Dunbar35b06572010-03-22 23:16:43 +0000445 // FIXME: We really need to improve the relocation validation. Basically, we
446 // want to implement a separate computation which evaluates the relocation
447 // entry as the linker would, and verifies that the resultant fixup value is
448 // exactly what the encoder wanted. This will catch several classes of
449 // problems:
450 //
451 // - Relocation entry bugs, the two algorithms are unlikely to have the same
452 // exact bug.
453 //
454 // - Relaxation issues, where we forget to relax something.
455 //
456 // - Input errors, where something cannot be correctly encoded. 'as' allows
457 // these through in many cases.
458
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000459 void RecordX86_64Relocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Daniel Dunbarb7514182010-03-22 20:35:50 +0000460 const MCFragment *Fragment,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000461 const MCFixup &Fixup, MCValue Target,
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000462 uint64_t &FixedValue) {
Daniel Dunbar482ad802010-05-26 15:18:31 +0000463 unsigned IsPCRel = isFixupKindPCRel(Fixup.getKind());
464 unsigned IsRIPRel = isFixupKindRIPRel(Fixup.getKind());
465 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000466
467 // See <reloc.h>.
Daniel Dunbar482ad802010-05-26 15:18:31 +0000468 uint32_t FixupOffset =
469 Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
470 uint32_t FixupAddress =
471 Layout.getFragmentAddress(Fragment) + Fixup.getOffset();
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000472 int64_t Value = 0;
473 unsigned Index = 0;
474 unsigned IsExtern = 0;
475 unsigned Type = 0;
476
477 Value = Target.getConstant();
478
479 if (IsPCRel) {
480 // Compensate for the relocation offset, Darwin x86_64 relocations only
481 // have the addend and appear to have attempted to define it to be the
482 // actual expression addend without the PCrel bias. However, instructions
483 // with data following the relocation are not accomodated for (see comment
484 // below regarding SIGNED{1,2,4}), so it isn't exactly that either.
Benjamin Kramer454c4ce2010-04-08 15:25:57 +0000485 Value += 1LL << Log2Size;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000486 }
487
488 if (Target.isAbsolute()) { // constant
489 // SymbolNum of 0 indicates the absolute section.
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000490 Type = macho::RIT_X86_64_Unsigned;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000491 Index = 0;
492
493 // FIXME: I believe this is broken, I don't think the linker can
494 // understand it. I think it would require a local relocation, but I'm not
495 // sure if that would work either. The official way to get an absolute
496 // PCrel relocation is to use an absolute symbol (which we don't support
497 // yet).
498 if (IsPCRel) {
499 IsExtern = 1;
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000500 Type = macho::RIT_X86_64_Branch;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000501 }
502 } else if (Target.getSymB()) { // A - B + constant
503 const MCSymbol *A = &Target.getSymA()->getSymbol();
504 MCSymbolData &A_SD = Asm.getSymbolData(*A);
Rafael Espindolab8141102010-09-27 18:13:03 +0000505 const MCSymbolData *A_Base = Asm.getAtom(&A_SD);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000506
507 const MCSymbol *B = &Target.getSymB()->getSymbol();
508 MCSymbolData &B_SD = Asm.getSymbolData(*B);
Rafael Espindolab8141102010-09-27 18:13:03 +0000509 const MCSymbolData *B_Base = Asm.getAtom(&B_SD);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000510
511 // Neither symbol can be modified.
512 if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
513 Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
Chris Lattner75361b62010-04-07 22:58:41 +0000514 report_fatal_error("unsupported relocation of modified symbol");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000515
516 // We don't support PCrel relocations of differences. Darwin 'as' doesn't
517 // implement most of these correctly.
518 if (IsPCRel)
Chris Lattner75361b62010-04-07 22:58:41 +0000519 report_fatal_error("unsupported pc-relative relocation of difference");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000520
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000521 // The support for the situation where one or both of the symbols would
522 // require a local relocation is handled just like if the symbols were
523 // external. This is certainly used in the case of debug sections where
524 // the section has only temporary symbols and thus the symbols don't have
525 // base symbols. This is encoded using the section ordinal and
526 // non-extern relocation entries.
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000527
528 // Darwin 'as' doesn't emit correct relocations for this (it ends up with
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000529 // a single SIGNED relocation); reject it for now. Except the case where
530 // both symbols don't have a base, equal but both NULL.
531 if (A_Base == B_Base && A_Base)
Chris Lattner75361b62010-04-07 22:58:41 +0000532 report_fatal_error("unsupported relocation with identical base");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000533
Jim Grosbach3c384922010-11-11 20:16:23 +0000534 Value += Layout.getSymbolAddress(&A_SD) -
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000535 (A_Base == NULL ? 0 : Layout.getSymbolAddress(A_Base));
536 Value -= Layout.getSymbolAddress(&B_SD) -
537 (B_Base == NULL ? 0 : Layout.getSymbolAddress(B_Base));
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000538
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000539 if (A_Base) {
540 Index = A_Base->getIndex();
541 IsExtern = 1;
542 }
543 else {
544 Index = A_SD.getFragment()->getParent()->getOrdinal() + 1;
545 IsExtern = 0;
546 }
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000547 Type = macho::RIT_X86_64_Unsigned;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000548
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000549 macho::RelocationEntry MRE;
Daniel Dunbar640e9482010-05-11 23:53:07 +0000550 MRE.Word0 = FixupOffset;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000551 MRE.Word1 = ((Index << 0) |
552 (IsPCRel << 24) |
553 (Log2Size << 25) |
554 (IsExtern << 27) |
555 (Type << 28));
Daniel Dunbarb7514182010-03-22 20:35:50 +0000556 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000557
Kevin Enderby8c9aa922010-10-02 00:13:41 +0000558 if (B_Base) {
559 Index = B_Base->getIndex();
560 IsExtern = 1;
561 }
562 else {
563 Index = B_SD.getFragment()->getParent()->getOrdinal() + 1;
564 IsExtern = 0;
565 }
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000566 Type = macho::RIT_X86_64_Subtractor;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000567 } else {
568 const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
569 MCSymbolData &SD = Asm.getSymbolData(*Symbol);
Rafael Espindolab8141102010-09-27 18:13:03 +0000570 const MCSymbolData *Base = Asm.getAtom(&SD);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000571
Daniel Dunbarae7fb0b2010-05-05 17:22:39 +0000572 // Relocations inside debug sections always use local relocations when
573 // possible. This seems to be done because the debugger doesn't fully
574 // understand x86_64 relocation entries, and expects to find values that
575 // have already been fixed up.
Daniel Dunbar2d7fd612010-05-05 19:01:05 +0000576 if (Symbol->isInSection()) {
Daniel Dunbarae7fb0b2010-05-05 17:22:39 +0000577 const MCSectionMachO &Section = static_cast<const MCSectionMachO&>(
578 Fragment->getParent()->getSection());
579 if (Section.hasAttribute(MCSectionMachO::S_ATTR_DEBUG))
580 Base = 0;
581 }
582
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000583 // x86_64 almost always uses external relocations, except when there is no
584 // symbol to use as a base address (a local symbol with no preceeding
585 // non-local symbol).
586 if (Base) {
587 Index = Base->getIndex();
588 IsExtern = 1;
589
590 // Add the local offset, if needed.
591 if (Base != &SD)
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000592 Value += Layout.getSymbolAddress(&SD) - Layout.getSymbolAddress(Base);
Daniel Dunbaref4591e2010-05-11 23:53:05 +0000593 } else if (Symbol->isInSection()) {
Daniel Dunbar8fb04032010-03-25 08:08:54 +0000594 // The index is the section ordinal (1-based).
595 Index = SD.getFragment()->getParent()->getOrdinal() + 1;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000596 IsExtern = 0;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000597 Value += Layout.getSymbolAddress(&SD);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000598
599 if (IsPCRel)
Daniel Dunbardb4c7e62010-05-11 23:53:11 +0000600 Value -= FixupAddress + (1 << Log2Size);
Daniel Dunbaref4591e2010-05-11 23:53:05 +0000601 } else {
602 report_fatal_error("unsupported relocation of undefined symbol '" +
603 Symbol->getName() + "'");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000604 }
605
606 MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
607 if (IsPCRel) {
608 if (IsRIPRel) {
609 if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
610 // x86_64 distinguishes movq foo@GOTPCREL so that the linker can
611 // rewrite the movq to an leaq at link time if the symbol ends up in
612 // the same linkage unit.
Daniel Dunbar482ad802010-05-26 15:18:31 +0000613 if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load)
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000614 Type = macho::RIT_X86_64_GOTLoad;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000615 else
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000616 Type = macho::RIT_X86_64_GOT;
Eric Christopheraeed4d82010-05-27 00:52:31 +0000617 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000618 Type = macho::RIT_X86_64_TLV;
Eric Christopheraeed4d82010-05-27 00:52:31 +0000619 } else if (Modifier != MCSymbolRefExpr::VK_None) {
620 report_fatal_error("unsupported symbol modifier in relocation");
Daniel Dunbarf0f6cdb2010-05-14 18:53:40 +0000621 } else {
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000622 Type = macho::RIT_X86_64_Signed;
Daniel Dunbarf0f6cdb2010-05-14 18:53:40 +0000623
624 // The Darwin x86_64 relocation format has a problem where it cannot
625 // encode an address (L<foo> + <constant>) which is outside the atom
626 // containing L<foo>. Generally, this shouldn't occur but it does
627 // happen when we have a RIPrel instruction with data following the
628 // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel
629 // adjustment Darwin x86_64 uses, the offset is still negative and
630 // the linker has no way to recognize this.
631 //
632 // To work around this, Darwin uses several special relocation types
633 // to indicate the offsets. However, the specification or
634 // implementation of these seems to also be incomplete; they should
635 // adjust the addend as well based on the actual encoded instruction
636 // (the additional bias), but instead appear to just look at the
637 // final offset.
638 switch (-(Target.getConstant() + (1LL << Log2Size))) {
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000639 case 1: Type = macho::RIT_X86_64_Signed1; break;
640 case 2: Type = macho::RIT_X86_64_Signed2; break;
641 case 4: Type = macho::RIT_X86_64_Signed4; break;
Daniel Dunbarf0f6cdb2010-05-14 18:53:40 +0000642 }
643 }
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000644 } else {
645 if (Modifier != MCSymbolRefExpr::VK_None)
Chris Lattner75361b62010-04-07 22:58:41 +0000646 report_fatal_error("unsupported symbol modifier in branch "
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000647 "relocation");
648
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000649 Type = macho::RIT_X86_64_Branch;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000650 }
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000651 } else {
Daniel Dunbar1de558b2010-03-29 23:56:40 +0000652 if (Modifier == MCSymbolRefExpr::VK_GOT) {
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000653 Type = macho::RIT_X86_64_GOT;
Daniel Dunbar1de558b2010-03-29 23:56:40 +0000654 } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
655 // GOTPCREL is allowed as a modifier on non-PCrel instructions, in
656 // which case all we do is set the PCrel bit in the relocation entry;
657 // this is used with exception handling, for example. The source is
658 // required to include any necessary offset directly.
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000659 Type = macho::RIT_X86_64_GOT;
Daniel Dunbar1de558b2010-03-29 23:56:40 +0000660 IsPCRel = 1;
Eric Christopher96ac5152010-05-26 00:02:12 +0000661 } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
662 report_fatal_error("TLVP symbol modifier should have been rip-rel");
Daniel Dunbar1de558b2010-03-29 23:56:40 +0000663 } else if (Modifier != MCSymbolRefExpr::VK_None)
Chris Lattner75361b62010-04-07 22:58:41 +0000664 report_fatal_error("unsupported symbol modifier in relocation");
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000665 else
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000666 Type = macho::RIT_X86_64_Unsigned;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000667 }
668 }
669
670 // x86_64 always writes custom values into the fixups.
671 FixedValue = Value;
672
673 // struct relocation_info (8 bytes)
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000674 macho::RelocationEntry MRE;
Daniel Dunbar640e9482010-05-11 23:53:07 +0000675 MRE.Word0 = FixupOffset;
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000676 MRE.Word1 = ((Index << 0) |
677 (IsPCRel << 24) |
678 (Log2Size << 25) |
679 (IsExtern << 27) |
680 (Type << 28));
Daniel Dunbarb7514182010-03-22 20:35:50 +0000681 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000682 }
683
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000684 void RecordScatteredRelocation(const MCAssembler &Asm,
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000685 const MCAsmLayout &Layout,
Daniel Dunbarb7514182010-03-22 20:35:50 +0000686 const MCFragment *Fragment,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000687 const MCFixup &Fixup, MCValue Target,
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000688 uint64_t &FixedValue) {
Daniel Dunbar482ad802010-05-26 15:18:31 +0000689 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
690 unsigned IsPCRel = isFixupKindPCRel(Fixup.getKind());
691 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000692 unsigned Type = macho::RIT_Vanilla;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000693
694 // See <reloc.h>.
695 const MCSymbol *A = &Target.getSymA()->getSymbol();
696 MCSymbolData *A_SD = &Asm.getSymbolData(*A);
697
698 if (!A_SD->getFragment())
Chris Lattner75361b62010-04-07 22:58:41 +0000699 report_fatal_error("symbol '" + A->getName() +
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000700 "' can not be undefined in a subtraction expression");
701
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000702 uint32_t Value = Layout.getSymbolAddress(A_SD);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000703 uint32_t Value2 = 0;
704
705 if (const MCSymbolRefExpr *B = Target.getSymB()) {
706 MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
707
708 if (!B_SD->getFragment())
Chris Lattner75361b62010-04-07 22:58:41 +0000709 report_fatal_error("symbol '" + B->getSymbol().getName() +
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000710 "' can not be undefined in a subtraction expression");
711
712 // Select the appropriate difference relocation type.
713 //
714 // Note that there is no longer any semantic difference between these two
715 // relocation types from the linkers point of view, this is done solely
716 // for pedantic compatibility with 'as'.
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000717 Type = A_SD->isExternal() ? macho::RIT_Difference :
718 macho::RIT_LocalDifference;
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000719 Value2 = Layout.getSymbolAddress(B_SD);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000720 }
721
722 // Relocations are written out in reverse order, so the PAIR comes first.
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000723 if (Type == macho::RIT_Difference || Type == macho::RIT_LocalDifference) {
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000724 macho::RelocationEntry MRE;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000725 MRE.Word0 = ((0 << 0) |
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000726 (macho::RIT_Pair << 24) |
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000727 (Log2Size << 28) |
728 (IsPCRel << 30) |
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000729 macho::RF_Scattered);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000730 MRE.Word1 = Value2;
Daniel Dunbarb7514182010-03-22 20:35:50 +0000731 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000732 }
733
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000734 macho::RelocationEntry MRE;
Daniel Dunbar640e9482010-05-11 23:53:07 +0000735 MRE.Word0 = ((FixupOffset << 0) |
736 (Type << 24) |
737 (Log2Size << 28) |
738 (IsPCRel << 30) |
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000739 macho::RF_Scattered);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000740 MRE.Word1 = Value;
Daniel Dunbarb7514182010-03-22 20:35:50 +0000741 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000742 }
743
Eric Christopherc9ada472010-06-15 22:59:05 +0000744 void RecordTLVPRelocation(const MCAssembler &Asm,
Eric Christophere48dbf82010-06-16 00:26:36 +0000745 const MCAsmLayout &Layout,
746 const MCFragment *Fragment,
747 const MCFixup &Fixup, MCValue Target,
748 uint64_t &FixedValue) {
Eric Christopherc9ada472010-06-15 22:59:05 +0000749 assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP &&
750 !Is64Bit &&
751 "Should only be called with a 32-bit TLVP relocation!");
752
Eric Christopherc9ada472010-06-15 22:59:05 +0000753 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
754 uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
755 unsigned IsPCRel = 0;
756
757 // Get the symbol data.
758 MCSymbolData *SD_A = &Asm.getSymbolData(Target.getSymA()->getSymbol());
759 unsigned Index = SD_A->getIndex();
760
Eric Christopherbc067372010-06-16 21:32:38 +0000761 // We're only going to have a second symbol in pic mode and it'll be a
762 // subtraction from the picbase. For 32-bit pic the addend is the difference
Eric Christopher04b8d3c2010-06-17 00:49:46 +0000763 // between the picbase and the next address. For 32-bit static the addend
764 // is zero.
Eric Christopherbc067372010-06-16 21:32:38 +0000765 if (Target.getSymB()) {
Eric Christopher1008d352010-06-22 23:51:47 +0000766 // If this is a subtraction then we're pcrel.
767 uint32_t FixupAddress =
768 Layout.getFragmentAddress(Fragment) + Fixup.getOffset();
769 MCSymbolData *SD_B = &Asm.getSymbolData(Target.getSymB()->getSymbol());
Eric Christopherc9ada472010-06-15 22:59:05 +0000770 IsPCRel = 1;
Eric Christopher1008d352010-06-22 23:51:47 +0000771 FixedValue = (FixupAddress - Layout.getSymbolAddress(SD_B) +
772 Target.getConstant());
Chris Lattnerabf8f9c2010-08-16 16:35:20 +0000773 FixedValue += 1ULL << Log2Size;
Eric Christopherbc067372010-06-16 21:32:38 +0000774 } else {
775 FixedValue = 0;
776 }
Jim Grosbach3c384922010-11-11 20:16:23 +0000777
Eric Christopherc9ada472010-06-15 22:59:05 +0000778 // struct relocation_info (8 bytes)
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000779 macho::RelocationEntry MRE;
Eric Christopherc9ada472010-06-15 22:59:05 +0000780 MRE.Word0 = Value;
781 MRE.Word1 = ((Index << 0) |
782 (IsPCRel << 24) |
783 (Log2Size << 25) |
784 (1 << 27) | // Extern
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000785 (macho::RIT_TLV << 28)); // Type
Eric Christopherc9ada472010-06-15 22:59:05 +0000786 Relocations[Fragment->getParent()].push_back(MRE);
787 }
Jim Grosbach3c384922010-11-11 20:16:23 +0000788
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000789 void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000790 const MCFragment *Fragment, const MCFixup &Fixup,
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000791 MCValue Target, uint64_t &FixedValue) {
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000792 if (Is64Bit) {
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000793 RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
Daniel Dunbar602b40f2010-03-19 18:07:55 +0000794 return;
795 }
796
Daniel Dunbar482ad802010-05-26 15:18:31 +0000797 unsigned IsPCRel = isFixupKindPCRel(Fixup.getKind());
798 unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000799
Eric Christopherc9ada472010-06-15 22:59:05 +0000800 // If this is a 32-bit TLVP reloc it's handled a bit differently.
Daniel Dunbar23bea412010-09-17 15:21:50 +0000801 if (Target.getSymA() &&
802 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) {
Eric Christopherc9ada472010-06-15 22:59:05 +0000803 RecordTLVPRelocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
804 return;
805 }
Jim Grosbach3c384922010-11-11 20:16:23 +0000806
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000807 // If this is a difference or a defined symbol plus an offset, then we need
808 // a scattered relocation entry.
Daniel Dunbara8251fa2010-05-10 23:15:20 +0000809 // Differences always require scattered relocations.
810 if (Target.getSymB())
811 return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
812 Target, FixedValue);
813
814 // Get the symbol data, if any.
815 MCSymbolData *SD = 0;
816 if (Target.getSymA())
817 SD = &Asm.getSymbolData(Target.getSymA()->getSymbol());
818
819 // If this is an internal relocation with an offset, it also needs a
820 // scattered relocation entry.
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000821 uint32_t Offset = Target.getConstant();
822 if (IsPCRel)
823 Offset += 1 << Log2Size;
Daniel Dunbara8251fa2010-05-10 23:15:20 +0000824 if (Offset && SD && !doesSymbolRequireExternRelocation(SD))
825 return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
826 Target, FixedValue);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000827
828 // See <reloc.h>.
Daniel Dunbar482ad802010-05-26 15:18:31 +0000829 uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000830 unsigned Index = 0;
831 unsigned IsExtern = 0;
832 unsigned Type = 0;
833
834 if (Target.isAbsolute()) { // constant
835 // SymbolNum of 0 indicates the absolute section.
836 //
837 // FIXME: Currently, these are never generated (see code below). I cannot
838 // find a case where they are actually emitted.
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000839 Type = macho::RIT_Vanilla;
Michael J. Spencerb0f3b3e2010-08-10 16:00:49 +0000840 } else {
Daniel Dunbare9460ec2010-05-10 23:15:13 +0000841 // Check whether we need an external or internal relocation.
842 if (doesSymbolRequireExternRelocation(SD)) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000843 IsExtern = 1;
844 Index = SD->getIndex();
Daniel Dunbare9460ec2010-05-10 23:15:13 +0000845 // For external relocations, make sure to offset the fixup value to
846 // compensate for the addend of the symbol address, if it was
847 // undefined. This occurs with weak definitions, for example.
848 if (!SD->Symbol->isUndefined())
Kevin Enderbya6eeb6e2010-05-07 21:44:23 +0000849 FixedValue -= Layout.getSymbolAddress(SD);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000850 } else {
Daniel Dunbar8fb04032010-03-25 08:08:54 +0000851 // The index is the section ordinal (1-based).
852 Index = SD->getFragment()->getParent()->getOrdinal() + 1;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000853 }
854
Daniel Dunbarf52788f2010-11-27 04:59:14 +0000855 Type = macho::RIT_Vanilla;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000856 }
857
858 // struct relocation_info (8 bytes)
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000859 macho::RelocationEntry MRE;
Daniel Dunbar640e9482010-05-11 23:53:07 +0000860 MRE.Word0 = FixupOffset;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000861 MRE.Word1 = ((Index << 0) |
862 (IsPCRel << 24) |
863 (Log2Size << 25) |
864 (IsExtern << 27) |
865 (Type << 28));
Daniel Dunbarb7514182010-03-22 20:35:50 +0000866 Relocations[Fragment->getParent()].push_back(MRE);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000867 }
868
869 void BindIndirectSymbols(MCAssembler &Asm) {
870 // This is the point where 'as' creates actual symbols for indirect symbols
871 // (in the following two passes). It would be easier for us to do this
872 // sooner when we see the attribute, but that makes getting the order in the
873 // symbol table much more complicated than it is worth.
874 //
875 // FIXME: Revisit this when the dust settles.
876
877 // Bind non lazy symbol pointers first.
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000878 unsigned IndirectIndex = 0;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000879 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000880 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000881 const MCSectionMachO &Section =
Daniel Dunbar56279f42010-05-18 17:28:20 +0000882 cast<MCSectionMachO>(it->SectionData->getSection());
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000883
884 if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
885 continue;
886
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000887 // Initialize the section indirect symbol base, if necessary.
888 if (!IndirectSymBase.count(it->SectionData))
889 IndirectSymBase[it->SectionData] = IndirectIndex;
Jim Grosbach3c384922010-11-11 20:16:23 +0000890
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000891 Asm.getOrCreateSymbolData(*it->Symbol);
892 }
893
894 // Then lazy symbol pointers and symbol stubs.
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000895 IndirectIndex = 0;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000896 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000897 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000898 const MCSectionMachO &Section =
Daniel Dunbar56279f42010-05-18 17:28:20 +0000899 cast<MCSectionMachO>(it->SectionData->getSection());
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000900
901 if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
902 Section.getType() != MCSectionMachO::S_SYMBOL_STUBS)
903 continue;
904
Daniel Dunbar2ae4bfd2010-05-18 17:28:24 +0000905 // Initialize the section indirect symbol base, if necessary.
906 if (!IndirectSymBase.count(it->SectionData))
907 IndirectSymBase[it->SectionData] = IndirectIndex;
908
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000909 // Set the symbol type to undefined lazy, but only on construction.
910 //
911 // FIXME: Do not hardcode.
912 bool Created;
913 MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
914 if (Created)
915 Entry.setFlags(Entry.getFlags() | 0x0001);
916 }
917 }
918
919 /// ComputeSymbolTable - Compute the symbol table data
920 ///
921 /// \param StringTable [out] - The string table data.
922 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
923 /// string table.
924 void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
925 std::vector<MachSymbolData> &LocalSymbolData,
926 std::vector<MachSymbolData> &ExternalSymbolData,
927 std::vector<MachSymbolData> &UndefinedSymbolData) {
928 // Build section lookup table.
929 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
930 unsigned Index = 1;
931 for (MCAssembler::iterator it = Asm.begin(),
932 ie = Asm.end(); it != ie; ++it, ++Index)
933 SectionIndexMap[&it->getSection()] = Index;
934 assert(Index <= 256 && "Too many sections!");
935
936 // Index 0 is always the empty string.
937 StringMap<uint64_t> StringIndexMap;
938 StringTable += '\x00';
939
940 // Build the symbol arrays and the string table, but only for non-local
941 // symbols.
942 //
943 // The particular order that we collect the symbols and create the string
944 // table, then sort the symbols is chosen to match 'as'. Even though it
945 // doesn't matter for correctness, this is important for letting us diff .o
946 // files.
947 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
948 ie = Asm.symbol_end(); it != ie; ++it) {
949 const MCSymbol &Symbol = it->getSymbol();
950
951 // Ignore non-linker visible symbols.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000952 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000953 continue;
954
955 if (!it->isExternal() && !Symbol.isUndefined())
956 continue;
957
958 uint64_t &Entry = StringIndexMap[Symbol.getName()];
959 if (!Entry) {
960 Entry = StringTable.size();
961 StringTable += Symbol.getName();
962 StringTable += '\x00';
963 }
964
965 MachSymbolData MSD;
966 MSD.SymbolData = it;
967 MSD.StringIndex = Entry;
968
969 if (Symbol.isUndefined()) {
970 MSD.SectionIndex = 0;
971 UndefinedSymbolData.push_back(MSD);
972 } else if (Symbol.isAbsolute()) {
973 MSD.SectionIndex = 0;
974 ExternalSymbolData.push_back(MSD);
975 } else {
976 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
977 assert(MSD.SectionIndex && "Invalid section index!");
978 ExternalSymbolData.push_back(MSD);
979 }
980 }
981
982 // Now add the data for local symbols.
983 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
984 ie = Asm.symbol_end(); it != ie; ++it) {
985 const MCSymbol &Symbol = it->getSymbol();
986
987 // Ignore non-linker visible symbols.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000988 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000989 continue;
990
991 if (it->isExternal() || Symbol.isUndefined())
992 continue;
993
994 uint64_t &Entry = StringIndexMap[Symbol.getName()];
995 if (!Entry) {
996 Entry = StringTable.size();
997 StringTable += Symbol.getName();
998 StringTable += '\x00';
999 }
1000
1001 MachSymbolData MSD;
1002 MSD.SymbolData = it;
1003 MSD.StringIndex = Entry;
1004
1005 if (Symbol.isAbsolute()) {
1006 MSD.SectionIndex = 0;
1007 LocalSymbolData.push_back(MSD);
1008 } else {
1009 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
1010 assert(MSD.SectionIndex && "Invalid section index!");
1011 LocalSymbolData.push_back(MSD);
1012 }
1013 }
1014
1015 // External and undefined symbols are required to be in lexicographic order.
1016 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1017 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1018
1019 // Set the symbol indices.
1020 Index = 0;
1021 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1022 LocalSymbolData[i].SymbolData->setIndex(Index++);
1023 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1024 ExternalSymbolData[i].SymbolData->setIndex(Index++);
1025 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1026 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1027
1028 // The string table is padded to a multiple of 4.
1029 while (StringTable.size() % 4)
1030 StringTable += '\x00';
1031 }
1032
Daniel Dunbar873decb2010-03-20 01:58:40 +00001033 void ExecutePostLayoutBinding(MCAssembler &Asm) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001034 // Create symbol data for any indirect symbols.
1035 BindIndirectSymbols(Asm);
1036
1037 // Compute symbol table information and bind symbol indices.
1038 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
1039 UndefinedSymbolData);
1040 }
1041
Rafael Espindola70703872010-09-30 02:22:20 +00001042
1043 bool IsFixupFullyResolved(const MCAssembler &Asm,
1044 const MCValue Target,
1045 bool IsPCRel,
1046 const MCFragment *DF) const {
Daniel Dunbar7976b882010-11-27 05:18:48 +00001047 // If we aren't using scattered symbols, the fixup is fully resolved.
1048 if (!Asm.getBackend().hasScatteredSymbols())
1049 return true;
Rafael Espindola70703872010-09-30 02:22:20 +00001050
Daniel Dunbar7976b882010-11-27 05:18:48 +00001051 // Otherwise, determine whether this value is actually resolved; scattering
1052 // may cause atoms to move.
Rafael Espindola70703872010-09-30 02:22:20 +00001053
Daniel Dunbar7976b882010-11-27 05:18:48 +00001054 // Check if we are using the "simple" resolution algorithm (e.g.,
1055 // i386).
1056 if (!Asm.getBackend().hasReliableSymbolDifference()) {
1057 const MCSection *BaseSection = 0;
1058 if (IsPCRel)
1059 BaseSection = &DF->getParent()->getSection();
1060
1061 return isScatteredFixupFullyResolvedSimple(Asm, Target, BaseSection);
Rafael Espindola70703872010-09-30 02:22:20 +00001062 }
Daniel Dunbar7976b882010-11-27 05:18:48 +00001063
1064 // Otherwise, compute the proper answer as reliably as possible.
1065
1066 // If this is a PCrel relocation, find the base atom (identified by its
1067 // symbol) that the fixup value is relative to.
1068 const MCSymbolData *BaseSymbol = 0;
1069 if (IsPCRel) {
1070 BaseSymbol = DF->getAtom();
1071 if (!BaseSymbol)
1072 return false;
1073 }
1074
1075 return isScatteredFixupFullyResolved(Asm, Target, BaseSymbol);
Rafael Espindola70703872010-09-30 02:22:20 +00001076 }
1077
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001078 void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001079 unsigned NumSections = Asm.size();
1080
1081 // The section data starts after the header, the segment load command (and
1082 // section headers) and the symbol table.
1083 unsigned NumLoadCommands = 1;
1084 uint64_t LoadCommandsSize = Is64Bit ?
Daniel Dunbar821ecd72010-11-27 04:19:38 +00001085 macho::SegmentLoadCommand64Size + NumSections * macho::Section64Size :
1086 macho::SegmentLoadCommand32Size + NumSections * macho::Section32Size;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001087
1088 // Add the symbol table load command sizes, if used.
1089 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
1090 UndefinedSymbolData.size();
1091 if (NumSymbols) {
1092 NumLoadCommands += 2;
Daniel Dunbar821ecd72010-11-27 04:19:38 +00001093 LoadCommandsSize += (macho::SymtabLoadCommandSize +
1094 macho::DysymtabLoadCommandSize);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001095 }
1096
1097 // Compute the total size of the section data, as well as its file size and
1098 // vm size.
Daniel Dunbar821ecd72010-11-27 04:19:38 +00001099 uint64_t SectionDataStart = (Is64Bit ? macho::Header64Size :
1100 macho::Header32Size) + LoadCommandsSize;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001101 uint64_t SectionDataSize = 0;
1102 uint64_t SectionDataFileSize = 0;
1103 uint64_t VMSize = 0;
1104 for (MCAssembler::const_iterator it = Asm.begin(),
1105 ie = Asm.end(); it != ie; ++it) {
1106 const MCSectionData &SD = *it;
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001107 uint64_t Address = Layout.getSectionAddress(&SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +00001108 uint64_t Size = Layout.getSectionSize(&SD);
1109 uint64_t FileSize = Layout.getSectionFileSize(&SD);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001110
Daniel Dunbar5d428512010-03-25 02:00:07 +00001111 VMSize = std::max(VMSize, Address + Size);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001112
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +00001113 if (SD.getSection().isVirtualSection())
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001114 continue;
1115
Daniel Dunbar5d428512010-03-25 02:00:07 +00001116 SectionDataSize = std::max(SectionDataSize, Address + Size);
1117 SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001118 }
1119
1120 // The section data is padded to 4 bytes.
1121 //
1122 // FIXME: Is this machine dependent?
1123 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
1124 SectionDataFileSize += SectionDataPadding;
1125
1126 // Write the prolog, starting with the header and load command...
1127 WriteHeader(NumLoadCommands, LoadCommandsSize,
1128 Asm.getSubsectionsViaSymbols());
1129 WriteSegmentLoadCommand(NumSections, VMSize,
1130 SectionDataStart, SectionDataSize);
1131
1132 // ... and then the section headers.
1133 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
1134 for (MCAssembler::const_iterator it = Asm.begin(),
1135 ie = Asm.end(); it != ie; ++it) {
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +00001136 std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001137 unsigned NumRelocs = Relocs.size();
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001138 uint64_t SectionStart = SectionDataStart + Layout.getSectionAddress(it);
1139 WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
Daniel Dunbar821ecd72010-11-27 04:19:38 +00001140 RelocTableEnd += NumRelocs * macho::RelocationInfoSize;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001141 }
1142
1143 // Write the symbol table load command, if used.
1144 if (NumSymbols) {
1145 unsigned FirstLocalSymbol = 0;
1146 unsigned NumLocalSymbols = LocalSymbolData.size();
1147 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
1148 unsigned NumExternalSymbols = ExternalSymbolData.size();
1149 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
1150 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
1151 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
1152 unsigned NumSymTabSymbols =
1153 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
1154 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
1155 uint64_t IndirectSymbolOffset = 0;
1156
1157 // If used, the indirect symbols are written after the section data.
1158 if (NumIndirectSymbols)
1159 IndirectSymbolOffset = RelocTableEnd;
1160
1161 // The symbol table is written after the indirect symbol data.
1162 uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
1163
1164 // The string table is written after symbol table.
1165 uint64_t StringTableOffset =
Daniel Dunbar821ecd72010-11-27 04:19:38 +00001166 SymbolTableOffset + NumSymTabSymbols * (Is64Bit ? macho::Nlist64Size :
1167 macho::Nlist32Size);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001168 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
1169 StringTableOffset, StringTable.size());
1170
1171 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
1172 FirstExternalSymbol, NumExternalSymbols,
1173 FirstUndefinedSymbol, NumUndefinedSymbols,
1174 IndirectSymbolOffset, NumIndirectSymbols);
1175 }
1176
1177 // Write the actual section data.
1178 for (MCAssembler::const_iterator it = Asm.begin(),
1179 ie = Asm.end(); it != ie; ++it)
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001180 Asm.WriteSectionData(it, Layout, this);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001181
1182 // Write the extra padding.
1183 WriteZeros(SectionDataPadding);
1184
1185 // Write the relocation entries.
1186 for (MCAssembler::const_iterator it = Asm.begin(),
1187 ie = Asm.end(); it != ie; ++it) {
1188 // Write the section relocation entries, in reverse order to match 'as'
1189 // (approximately, the exact algorithm is more complicated than this).
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +00001190 std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001191 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1192 Write32(Relocs[e - i - 1].Word0);
1193 Write32(Relocs[e - i - 1].Word1);
1194 }
1195 }
1196
1197 // Write the symbol table data, if used.
1198 if (NumSymbols) {
1199 // Write the indirect symbol entries.
1200 for (MCAssembler::const_indirect_symbol_iterator
1201 it = Asm.indirect_symbol_begin(),
1202 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
1203 // Indirect symbols in the non lazy symbol pointer section have some
1204 // special handling.
1205 const MCSectionMachO &Section =
1206 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
1207 if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
1208 // If this symbol is defined and internal, mark it as such.
1209 if (it->Symbol->isDefined() &&
1210 !Asm.getSymbolData(*it->Symbol).isExternal()) {
Daniel Dunbarf52788f2010-11-27 04:59:14 +00001211 uint32_t Flags = macho::ISF_Local;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001212 if (it->Symbol->isAbsolute())
Daniel Dunbarf52788f2010-11-27 04:59:14 +00001213 Flags |= macho::ISF_Absolute;
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001214 Write32(Flags);
1215 continue;
1216 }
1217 }
1218
1219 Write32(Asm.getSymbolData(*it->Symbol).getIndex());
1220 }
1221
1222 // FIXME: Check that offsets match computed ones.
1223
1224 // Write the symbol table entries.
1225 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001226 WriteNlist(LocalSymbolData[i], Layout);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001227 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001228 WriteNlist(ExternalSymbolData[i], Layout);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001229 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
Daniel Dunbar207e06e2010-03-24 03:43:40 +00001230 WriteNlist(UndefinedSymbolData[i], Layout);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001231
1232 // Write the string table.
1233 OS << StringTable.str();
1234 }
1235 }
1236};
1237
1238}
1239
Daniel Dunbar115a3dd2010-11-13 07:33:40 +00001240MCObjectWriter *llvm::createMachObjectWriter(raw_ostream &OS, bool is64Bit,
1241 uint32_t CPUType,
1242 uint32_t CPUSubtype,
1243 bool IsLittleEndian) {
1244 return new MachObjectWriter(OS, is64Bit, CPUType, CPUSubtype, IsLittleEndian);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001245}