MC/Mach-O: Shuffle enums a bit to make it harder to inadvertently use the wrong
[oota-llvm.git] / lib / MC / MachObjectWriter.cpp
1 //===- 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
10 #include "llvm/MC/MCMachObjectWriter.h"
11 #include "llvm/ADT/OwningPtr.h"
12 #include "llvm/ADT/StringMap.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCMachOSymbolFlags.h"
21 #include "llvm/MC/MCValue.h"
22 #include "llvm/Object/MachOFormat.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Target/TargetAsmBackend.h"
25
26 // FIXME: Gross.
27 #include "../Target/X86/X86FixupKinds.h"
28
29 #include <vector>
30 using namespace llvm;
31 using namespace llvm::object;
32
33 // FIXME: this has been copied from (or to) X86AsmBackend.cpp
34 static unsigned getFixupKindLog2Size(unsigned Kind) {
35   switch (Kind) {
36   // FIXME: Until ARM has it's own relocation stuff spun off, it comes
37   // through here and we don't want it to puke all over. Any reasonable
38   // values will only come when ARM relocation support gets added, at which
39   // point this will be X86 only again and the llvm_unreachable can be
40   // re-enabled.
41   default: return 0;// llvm_unreachable("invalid fixup kind!");
42   case FK_PCRel_1:
43   case FK_Data_1: return 0;
44   case FK_PCRel_2:
45   case FK_Data_2: return 1;
46   case FK_PCRel_4:
47   case X86::reloc_riprel_4byte:
48   case X86::reloc_riprel_4byte_movq_load:
49   case X86::reloc_signed_4byte:
50   case FK_Data_4: return 2;
51   case FK_Data_8: return 3;
52   }
53 }
54
55 static bool doesSymbolRequireExternRelocation(MCSymbolData *SD) {
56   // Undefined symbols are always extern.
57   if (SD->Symbol->isUndefined())
58     return true;
59
60   // References to weak definitions require external relocation entries; the
61   // definition may not always be the one in the same object file.
62   if (SD->getFlags() & SF_WeakDefinition)
63     return true;
64
65   // Otherwise, we can use an internal relocation.
66   return false;
67 }
68
69 static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
70                                           const MCValue Target,
71                                           const MCSymbolData *BaseSymbol) {
72   // The effective fixup address is
73   //     addr(atom(A)) + offset(A)
74   //   - addr(atom(B)) - offset(B)
75   //   - addr(BaseSymbol) + <fixup offset from base symbol>
76   // and the offsets are not relocatable, so the fixup is fully resolved when
77   //  addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
78   //
79   // Note that "false" is almost always conservatively correct (it means we emit
80   // a relocation which is unnecessary), except when it would force us to emit a
81   // relocation which the target cannot encode.
82
83   const MCSymbolData *A_Base = 0, *B_Base = 0;
84   if (const MCSymbolRefExpr *A = Target.getSymA()) {
85     // Modified symbol references cannot be resolved.
86     if (A->getKind() != MCSymbolRefExpr::VK_None)
87       return false;
88
89     A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
90     if (!A_Base)
91       return false;
92   }
93
94   if (const MCSymbolRefExpr *B = Target.getSymB()) {
95     // Modified symbol references cannot be resolved.
96     if (B->getKind() != MCSymbolRefExpr::VK_None)
97       return false;
98
99     B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
100     if (!B_Base)
101       return false;
102   }
103
104   // If there is no base, A and B have to be the same atom for this fixup to be
105   // fully resolved.
106   if (!BaseSymbol)
107     return A_Base == B_Base;
108
109   // Otherwise, B must be missing and A must be the base.
110   return !B_Base && BaseSymbol == A_Base;
111 }
112
113 static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
114                                                 const MCValue Target,
115                                                 const MCSection *BaseSection) {
116   // The effective fixup address is
117   //     addr(atom(A)) + offset(A)
118   //   - addr(atom(B)) - offset(B)
119   //   - addr(<base symbol>) + <fixup offset from base symbol>
120   // and the offsets are not relocatable, so the fixup is fully resolved when
121   //  addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
122   //
123   // The simple (Darwin, except on x86_64) way of dealing with this was to
124   // assume that any reference to a temporary symbol *must* be a temporary
125   // symbol in the same atom, unless the sections differ. Therefore, any PCrel
126   // relocation to a temporary symbol (in the same section) is fully
127   // resolved. This also works in conjunction with absolutized .set, which
128   // requires the compiler to use .set to absolutize the differences between
129   // symbols which the compiler knows to be assembly time constants, so we don't
130   // need to worry about considering symbol differences fully resolved.
131
132   // Non-relative fixups are only resolved if constant.
133   if (!BaseSection)
134     return Target.isAbsolute();
135
136   // Otherwise, relative fixups are only resolved if not a difference and the
137   // target is a temporary in the same section.
138   if (Target.isAbsolute() || Target.getSymB())
139     return false;
140
141   const MCSymbol *A = &Target.getSymA()->getSymbol();
142   if (!A->isTemporary() || !A->isInSection() ||
143       &A->getSection() != BaseSection)
144     return false;
145
146   return true;
147 }
148
149 namespace {
150
151 class MachObjectWriter : public MCObjectWriter {
152   /// MachSymbolData - Helper struct for containing some precomputed information
153   /// on symbols.
154   struct MachSymbolData {
155     MCSymbolData *SymbolData;
156     uint64_t StringIndex;
157     uint8_t SectionIndex;
158
159     // Support lexicographic sorting.
160     bool operator<(const MachSymbolData &RHS) const {
161       return SymbolData->getSymbol().getName() <
162              RHS.SymbolData->getSymbol().getName();
163     }
164   };
165
166   /// The target specific Mach-O writer instance.
167   llvm::OwningPtr<MCMachObjectTargetWriter> TargetObjectWriter;
168
169   /// @name Relocation Data
170   /// @{
171
172   llvm::DenseMap<const MCSectionData*,
173                  std::vector<macho::RelocationEntry> > Relocations;
174   llvm::DenseMap<const MCSectionData*, unsigned> IndirectSymBase;
175
176   /// @}
177   /// @name Symbol Table Data
178   /// @{
179
180   SmallString<256> StringTable;
181   std::vector<MachSymbolData> LocalSymbolData;
182   std::vector<MachSymbolData> ExternalSymbolData;
183   std::vector<MachSymbolData> UndefinedSymbolData;
184
185   /// @}
186
187 private:
188   /// @name Utility Methods
189   /// @{
190
191   bool isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
192     const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo(
193       (MCFixupKind) Kind);
194
195     return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
196   }
197
198   /// @}
199
200   SectionAddrMap SectionAddress;
201   uint64_t getSectionAddress(const MCSectionData* SD) const {
202     return SectionAddress.lookup(SD);
203   }
204   uint64_t getSymbolAddress(const MCSymbolData* SD,
205                             const MCAsmLayout &Layout) const {
206     return getSectionAddress(SD->getFragment()->getParent()) +
207       Layout.getSymbolOffset(SD);
208   }
209   uint64_t getFragmentAddress(const MCFragment *Fragment,
210                             const MCAsmLayout &Layout) const {
211     return getSectionAddress(Fragment->getParent()) +
212       Layout.getFragmentOffset(Fragment);
213   }
214
215   uint64_t getPaddingSize(const MCSectionData *SD,
216                           const MCAsmLayout &Layout) const {
217     uint64_t EndAddr = getSectionAddress(SD) + Layout.getSectionAddressSize(SD);
218     unsigned Next = SD->getLayoutOrder() + 1;
219     if (Next >= Layout.getSectionOrder().size())
220       return 0;
221
222     const MCSectionData &NextSD = *Layout.getSectionOrder()[Next];
223     if (NextSD.getSection().isVirtualSection())
224       return 0;
225     return OffsetToAlignment(EndAddr, NextSD.getAlignment());
226   }
227
228 public:
229   MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS,
230                    bool _IsLittleEndian)
231     : MCObjectWriter(_OS, _IsLittleEndian), TargetObjectWriter(MOTW) {
232   }
233
234   /// @name Target Writer Proxy Accessors
235   /// @{
236
237   bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
238
239   /// @}
240
241   void WriteHeader(unsigned NumLoadCommands, unsigned LoadCommandsSize,
242                    bool SubsectionsViaSymbols) {
243     uint32_t Flags = 0;
244
245     if (SubsectionsViaSymbols)
246       Flags |= macho::HF_SubsectionsViaSymbols;
247
248     // struct mach_header (28 bytes) or
249     // struct mach_header_64 (32 bytes)
250
251     uint64_t Start = OS.tell();
252     (void) Start;
253
254     Write32(is64Bit() ? macho::HM_Object64 : macho::HM_Object32);
255
256     Write32(TargetObjectWriter->getCPUType());
257     Write32(TargetObjectWriter->getCPUSubtype());
258
259     Write32(macho::HFT_Object);
260     Write32(NumLoadCommands);
261     Write32(LoadCommandsSize);
262     Write32(Flags);
263     if (is64Bit())
264       Write32(0); // reserved
265
266     assert(OS.tell() - Start == is64Bit() ? 
267            macho::Header64Size : macho::Header32Size);
268   }
269
270   /// WriteSegmentLoadCommand - Write a segment load command.
271   ///
272   /// \arg NumSections - The number of sections in this segment.
273   /// \arg SectionDataSize - The total size of the sections.
274   void WriteSegmentLoadCommand(unsigned NumSections,
275                                uint64_t VMSize,
276                                uint64_t SectionDataStartOffset,
277                                uint64_t SectionDataSize) {
278     // struct segment_command (56 bytes) or
279     // struct segment_command_64 (72 bytes)
280
281     uint64_t Start = OS.tell();
282     (void) Start;
283
284     unsigned SegmentLoadCommandSize =
285       is64Bit() ? macho::SegmentLoadCommand64Size:
286       macho::SegmentLoadCommand32Size;
287     Write32(is64Bit() ? macho::LCT_Segment64 : macho::LCT_Segment);
288     Write32(SegmentLoadCommandSize +
289             NumSections * (is64Bit() ? macho::Section64Size :
290                            macho::Section32Size));
291
292     WriteBytes("", 16);
293     if (is64Bit()) {
294       Write64(0); // vmaddr
295       Write64(VMSize); // vmsize
296       Write64(SectionDataStartOffset); // file offset
297       Write64(SectionDataSize); // file size
298     } else {
299       Write32(0); // vmaddr
300       Write32(VMSize); // vmsize
301       Write32(SectionDataStartOffset); // file offset
302       Write32(SectionDataSize); // file size
303     }
304     Write32(0x7); // maxprot
305     Write32(0x7); // initprot
306     Write32(NumSections);
307     Write32(0); // flags
308
309     assert(OS.tell() - Start == SegmentLoadCommandSize);
310   }
311
312   void WriteSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
313                     const MCSectionData &SD, uint64_t FileOffset,
314                     uint64_t RelocationsStart, unsigned NumRelocations) {
315     uint64_t SectionSize = Layout.getSectionAddressSize(&SD);
316
317     // The offset is unused for virtual sections.
318     if (SD.getSection().isVirtualSection()) {
319       assert(Layout.getSectionFileSize(&SD) == 0 && "Invalid file size!");
320       FileOffset = 0;
321     }
322
323     // struct section (68 bytes) or
324     // struct section_64 (80 bytes)
325
326     uint64_t Start = OS.tell();
327     (void) Start;
328
329     const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection());
330     WriteBytes(Section.getSectionName(), 16);
331     WriteBytes(Section.getSegmentName(), 16);
332     if (is64Bit()) {
333       Write64(getSectionAddress(&SD)); // address
334       Write64(SectionSize); // size
335     } else {
336       Write32(getSectionAddress(&SD)); // address
337       Write32(SectionSize); // size
338     }
339     Write32(FileOffset);
340
341     unsigned Flags = Section.getTypeAndAttributes();
342     if (SD.hasInstructions())
343       Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
344
345     assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
346     Write32(Log2_32(SD.getAlignment()));
347     Write32(NumRelocations ? RelocationsStart : 0);
348     Write32(NumRelocations);
349     Write32(Flags);
350     Write32(IndirectSymBase.lookup(&SD)); // reserved1
351     Write32(Section.getStubSize()); // reserved2
352     if (is64Bit())
353       Write32(0); // reserved3
354
355     assert(OS.tell() - Start == is64Bit() ? macho::Section64Size :
356            macho::Section32Size);
357   }
358
359   void WriteSymtabLoadCommand(uint32_t SymbolOffset, uint32_t NumSymbols,
360                               uint32_t StringTableOffset,
361                               uint32_t StringTableSize) {
362     // struct symtab_command (24 bytes)
363
364     uint64_t Start = OS.tell();
365     (void) Start;
366
367     Write32(macho::LCT_Symtab);
368     Write32(macho::SymtabLoadCommandSize);
369     Write32(SymbolOffset);
370     Write32(NumSymbols);
371     Write32(StringTableOffset);
372     Write32(StringTableSize);
373
374     assert(OS.tell() - Start == macho::SymtabLoadCommandSize);
375   }
376
377   void WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
378                                 uint32_t NumLocalSymbols,
379                                 uint32_t FirstExternalSymbol,
380                                 uint32_t NumExternalSymbols,
381                                 uint32_t FirstUndefinedSymbol,
382                                 uint32_t NumUndefinedSymbols,
383                                 uint32_t IndirectSymbolOffset,
384                                 uint32_t NumIndirectSymbols) {
385     // struct dysymtab_command (80 bytes)
386
387     uint64_t Start = OS.tell();
388     (void) Start;
389
390     Write32(macho::LCT_Dysymtab);
391     Write32(macho::DysymtabLoadCommandSize);
392     Write32(FirstLocalSymbol);
393     Write32(NumLocalSymbols);
394     Write32(FirstExternalSymbol);
395     Write32(NumExternalSymbols);
396     Write32(FirstUndefinedSymbol);
397     Write32(NumUndefinedSymbols);
398     Write32(0); // tocoff
399     Write32(0); // ntoc
400     Write32(0); // modtaboff
401     Write32(0); // nmodtab
402     Write32(0); // extrefsymoff
403     Write32(0); // nextrefsyms
404     Write32(IndirectSymbolOffset);
405     Write32(NumIndirectSymbols);
406     Write32(0); // extreloff
407     Write32(0); // nextrel
408     Write32(0); // locreloff
409     Write32(0); // nlocrel
410
411     assert(OS.tell() - Start == macho::DysymtabLoadCommandSize);
412   }
413
414   void WriteNlist(MachSymbolData &MSD, const MCAsmLayout &Layout) {
415     MCSymbolData &Data = *MSD.SymbolData;
416     const MCSymbol &Symbol = Data.getSymbol();
417     uint8_t Type = 0;
418     uint16_t Flags = Data.getFlags();
419     uint32_t Address = 0;
420
421     // Set the N_TYPE bits. See <mach-o/nlist.h>.
422     //
423     // FIXME: Are the prebound or indirect fields possible here?
424     if (Symbol.isUndefined())
425       Type = macho::STT_Undefined;
426     else if (Symbol.isAbsolute())
427       Type = macho::STT_Absolute;
428     else
429       Type = macho::STT_Section;
430
431     // FIXME: Set STAB bits.
432
433     if (Data.isPrivateExtern())
434       Type |= macho::STF_PrivateExtern;
435
436     // Set external bit.
437     if (Data.isExternal() || Symbol.isUndefined())
438       Type |= macho::STF_External;
439
440     // Compute the symbol address.
441     if (Symbol.isDefined()) {
442       if (Symbol.isAbsolute()) {
443         Address = cast<MCConstantExpr>(Symbol.getVariableValue())->getValue();
444       } else {
445         Address = getSymbolAddress(&Data, Layout);
446       }
447     } else if (Data.isCommon()) {
448       // Common symbols are encoded with the size in the address
449       // field, and their alignment in the flags.
450       Address = Data.getCommonSize();
451
452       // Common alignment is packed into the 'desc' bits.
453       if (unsigned Align = Data.getCommonAlignment()) {
454         unsigned Log2Size = Log2_32(Align);
455         assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
456         if (Log2Size > 15)
457           report_fatal_error("invalid 'common' alignment '" +
458                             Twine(Align) + "'");
459         // FIXME: Keep this mask with the SymbolFlags enumeration.
460         Flags = (Flags & 0xF0FF) | (Log2Size << 8);
461       }
462     }
463
464     // struct nlist (12 bytes)
465
466     Write32(MSD.StringIndex);
467     Write8(Type);
468     Write8(MSD.SectionIndex);
469
470     // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
471     // value.
472     Write16(Flags);
473     if (is64Bit())
474       Write64(Address);
475     else
476       Write32(Address);
477   }
478
479   // FIXME: We really need to improve the relocation validation. Basically, we
480   // want to implement a separate computation which evaluates the relocation
481   // entry as the linker would, and verifies that the resultant fixup value is
482   // exactly what the encoder wanted. This will catch several classes of
483   // problems:
484   //
485   //  - Relocation entry bugs, the two algorithms are unlikely to have the same
486   //    exact bug.
487   //
488   //  - Relaxation issues, where we forget to relax something.
489   //
490   //  - Input errors, where something cannot be correctly encoded. 'as' allows
491   //    these through in many cases.
492
493   static bool isFixupKindRIPRel(unsigned Kind) {
494     return Kind == X86::reloc_riprel_4byte ||
495       Kind == X86::reloc_riprel_4byte_movq_load;
496   }
497   void RecordX86_64Relocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
498                               const MCFragment *Fragment,
499                               const MCFixup &Fixup, MCValue Target,
500                               uint64_t &FixedValue) {
501     unsigned IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
502     unsigned IsRIPRel = isFixupKindRIPRel(Fixup.getKind());
503     unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
504
505     // See <reloc.h>.
506     uint32_t FixupOffset =
507       Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
508     uint32_t FixupAddress =
509       getFragmentAddress(Fragment, Layout) + Fixup.getOffset();
510     int64_t Value = 0;
511     unsigned Index = 0;
512     unsigned IsExtern = 0;
513     unsigned Type = 0;
514
515     Value = Target.getConstant();
516
517     if (IsPCRel) {
518       // Compensate for the relocation offset, Darwin x86_64 relocations only
519       // have the addend and appear to have attempted to define it to be the
520       // actual expression addend without the PCrel bias. However, instructions
521       // with data following the relocation are not accomodated for (see comment
522       // below regarding SIGNED{1,2,4}), so it isn't exactly that either.
523       Value += 1LL << Log2Size;
524     }
525
526     if (Target.isAbsolute()) { // constant
527       // SymbolNum of 0 indicates the absolute section.
528       Type = macho::RIT_X86_64_Unsigned;
529       Index = 0;
530
531       // FIXME: I believe this is broken, I don't think the linker can
532       // understand it. I think it would require a local relocation, but I'm not
533       // sure if that would work either. The official way to get an absolute
534       // PCrel relocation is to use an absolute symbol (which we don't support
535       // yet).
536       if (IsPCRel) {
537         IsExtern = 1;
538         Type = macho::RIT_X86_64_Branch;
539       }
540     } else if (Target.getSymB()) { // A - B + constant
541       const MCSymbol *A = &Target.getSymA()->getSymbol();
542       MCSymbolData &A_SD = Asm.getSymbolData(*A);
543       const MCSymbolData *A_Base = Asm.getAtom(&A_SD);
544
545       const MCSymbol *B = &Target.getSymB()->getSymbol();
546       MCSymbolData &B_SD = Asm.getSymbolData(*B);
547       const MCSymbolData *B_Base = Asm.getAtom(&B_SD);
548
549       // Neither symbol can be modified.
550       if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None ||
551           Target.getSymB()->getKind() != MCSymbolRefExpr::VK_None)
552         report_fatal_error("unsupported relocation of modified symbol");
553
554       // We don't support PCrel relocations of differences. Darwin 'as' doesn't
555       // implement most of these correctly.
556       if (IsPCRel)
557         report_fatal_error("unsupported pc-relative relocation of difference");
558
559       // The support for the situation where one or both of the symbols would
560       // require a local relocation is handled just like if the symbols were
561       // external.  This is certainly used in the case of debug sections where
562       // the section has only temporary symbols and thus the symbols don't have
563       // base symbols.  This is encoded using the section ordinal and
564       // non-extern relocation entries.
565
566       // Darwin 'as' doesn't emit correct relocations for this (it ends up with
567       // a single SIGNED relocation); reject it for now.  Except the case where
568       // both symbols don't have a base, equal but both NULL.
569       if (A_Base == B_Base && A_Base)
570         report_fatal_error("unsupported relocation with identical base");
571
572       Value += getSymbolAddress(&A_SD, Layout) -
573         (A_Base == NULL ? 0 : getSymbolAddress(A_Base, Layout));
574       Value -= getSymbolAddress(&B_SD, Layout) -
575         (B_Base == NULL ? 0 : getSymbolAddress(B_Base, Layout));
576
577       if (A_Base) {
578         Index = A_Base->getIndex();
579         IsExtern = 1;
580       }
581       else {
582         Index = A_SD.getFragment()->getParent()->getOrdinal() + 1;
583         IsExtern = 0;
584       }
585       Type = macho::RIT_X86_64_Unsigned;
586
587       macho::RelocationEntry MRE;
588       MRE.Word0 = FixupOffset;
589       MRE.Word1 = ((Index     <<  0) |
590                    (IsPCRel   << 24) |
591                    (Log2Size  << 25) |
592                    (IsExtern  << 27) |
593                    (Type      << 28));
594       Relocations[Fragment->getParent()].push_back(MRE);
595
596       if (B_Base) {
597         Index = B_Base->getIndex();
598         IsExtern = 1;
599       }
600       else {
601         Index = B_SD.getFragment()->getParent()->getOrdinal() + 1;
602         IsExtern = 0;
603       }
604       Type = macho::RIT_X86_64_Subtractor;
605     } else {
606       const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
607       MCSymbolData &SD = Asm.getSymbolData(*Symbol);
608       const MCSymbolData *Base = Asm.getAtom(&SD);
609
610       // Relocations inside debug sections always use local relocations when
611       // possible. This seems to be done because the debugger doesn't fully
612       // understand x86_64 relocation entries, and expects to find values that
613       // have already been fixed up.
614       if (Symbol->isInSection()) {
615         const MCSectionMachO &Section = static_cast<const MCSectionMachO&>(
616           Fragment->getParent()->getSection());
617         if (Section.hasAttribute(MCSectionMachO::S_ATTR_DEBUG))
618           Base = 0;
619       }
620
621       // x86_64 almost always uses external relocations, except when there is no
622       // symbol to use as a base address (a local symbol with no preceeding
623       // non-local symbol).
624       if (Base) {
625         Index = Base->getIndex();
626         IsExtern = 1;
627
628         // Add the local offset, if needed.
629         if (Base != &SD)
630           Value += Layout.getSymbolOffset(&SD) - Layout.getSymbolOffset(Base);
631       } else if (Symbol->isInSection()) {
632         // The index is the section ordinal (1-based).
633         Index = SD.getFragment()->getParent()->getOrdinal() + 1;
634         IsExtern = 0;
635         Value += getSymbolAddress(&SD, Layout);
636
637         if (IsPCRel)
638           Value -= FixupAddress + (1 << Log2Size);
639       } else if (Symbol->isVariable()) {
640         const MCExpr *Value = Symbol->getVariableValue();
641         int64_t Res;
642         bool isAbs = Value->EvaluateAsAbsolute(Res, Layout, SectionAddress);
643         if (isAbs) {
644           FixedValue = Res;
645           return;
646         } else {
647           report_fatal_error("unsupported relocation of variable '" +
648                              Symbol->getName() + "'");
649         }
650       } else {
651         report_fatal_error("unsupported relocation of undefined symbol '" +
652                            Symbol->getName() + "'");
653       }
654
655       MCSymbolRefExpr::VariantKind Modifier = Target.getSymA()->getKind();
656       if (IsPCRel) {
657         if (IsRIPRel) {
658           if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
659             // x86_64 distinguishes movq foo@GOTPCREL so that the linker can
660             // rewrite the movq to an leaq at link time if the symbol ends up in
661             // the same linkage unit.
662             if (unsigned(Fixup.getKind()) == X86::reloc_riprel_4byte_movq_load)
663               Type = macho::RIT_X86_64_GOTLoad;
664             else
665               Type = macho::RIT_X86_64_GOT;
666           }  else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
667             Type = macho::RIT_X86_64_TLV;
668           }  else if (Modifier != MCSymbolRefExpr::VK_None) {
669             report_fatal_error("unsupported symbol modifier in relocation");
670           } else {
671             Type = macho::RIT_X86_64_Signed;
672
673             // The Darwin x86_64 relocation format has a problem where it cannot
674             // encode an address (L<foo> + <constant>) which is outside the atom
675             // containing L<foo>. Generally, this shouldn't occur but it does
676             // happen when we have a RIPrel instruction with data following the
677             // relocation entry (e.g., movb $012, L0(%rip)). Even with the PCrel
678             // adjustment Darwin x86_64 uses, the offset is still negative and
679             // the linker has no way to recognize this.
680             //
681             // To work around this, Darwin uses several special relocation types
682             // to indicate the offsets. However, the specification or
683             // implementation of these seems to also be incomplete; they should
684             // adjust the addend as well based on the actual encoded instruction
685             // (the additional bias), but instead appear to just look at the
686             // final offset.
687             switch (-(Target.getConstant() + (1LL << Log2Size))) {
688             case 1: Type = macho::RIT_X86_64_Signed1; break;
689             case 2: Type = macho::RIT_X86_64_Signed2; break;
690             case 4: Type = macho::RIT_X86_64_Signed4; break;
691             }
692           }
693         } else {
694           if (Modifier != MCSymbolRefExpr::VK_None)
695             report_fatal_error("unsupported symbol modifier in branch "
696                               "relocation");
697
698           Type = macho::RIT_X86_64_Branch;
699         }
700       } else {
701         if (Modifier == MCSymbolRefExpr::VK_GOT) {
702           Type = macho::RIT_X86_64_GOT;
703         } else if (Modifier == MCSymbolRefExpr::VK_GOTPCREL) {
704           // GOTPCREL is allowed as a modifier on non-PCrel instructions, in
705           // which case all we do is set the PCrel bit in the relocation entry;
706           // this is used with exception handling, for example. The source is
707           // required to include any necessary offset directly.
708           Type = macho::RIT_X86_64_GOT;
709           IsPCRel = 1;
710         } else if (Modifier == MCSymbolRefExpr::VK_TLVP) {
711           report_fatal_error("TLVP symbol modifier should have been rip-rel");
712         } else if (Modifier != MCSymbolRefExpr::VK_None)
713           report_fatal_error("unsupported symbol modifier in relocation");
714         else
715           Type = macho::RIT_X86_64_Unsigned;
716       }
717     }
718
719     // x86_64 always writes custom values into the fixups.
720     FixedValue = Value;
721
722     // struct relocation_info (8 bytes)
723     macho::RelocationEntry MRE;
724     MRE.Word0 = FixupOffset;
725     MRE.Word1 = ((Index     <<  0) |
726                  (IsPCRel   << 24) |
727                  (Log2Size  << 25) |
728                  (IsExtern  << 27) |
729                  (Type      << 28));
730     Relocations[Fragment->getParent()].push_back(MRE);
731   }
732
733   void RecordScatteredRelocation(const MCAssembler &Asm,
734                                  const MCAsmLayout &Layout,
735                                  const MCFragment *Fragment,
736                                  const MCFixup &Fixup, MCValue Target,
737                                  uint64_t &FixedValue) {
738     uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
739     unsigned IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
740     unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
741     unsigned Type = macho::RIT_Vanilla;
742
743     // See <reloc.h>.
744     const MCSymbol *A = &Target.getSymA()->getSymbol();
745     MCSymbolData *A_SD = &Asm.getSymbolData(*A);
746
747     if (!A_SD->getFragment())
748       report_fatal_error("symbol '" + A->getName() +
749                         "' can not be undefined in a subtraction expression");
750
751     uint32_t Value = getSymbolAddress(A_SD, Layout);
752     uint64_t SecAddr = getSectionAddress(A_SD->getFragment()->getParent());
753     FixedValue += SecAddr;
754     uint32_t Value2 = 0;
755
756     if (const MCSymbolRefExpr *B = Target.getSymB()) {
757       MCSymbolData *B_SD = &Asm.getSymbolData(B->getSymbol());
758
759       if (!B_SD->getFragment())
760         report_fatal_error("symbol '" + B->getSymbol().getName() +
761                           "' can not be undefined in a subtraction expression");
762
763       // Select the appropriate difference relocation type.
764       //
765       // Note that there is no longer any semantic difference between these two
766       // relocation types from the linkers point of view, this is done solely
767       // for pedantic compatibility with 'as'.
768       Type = A_SD->isExternal() ? macho::RIT_Difference :
769         macho::RIT_Generic_LocalDifference;
770       Value2 = getSymbolAddress(B_SD, Layout);
771       FixedValue -= getSectionAddress(B_SD->getFragment()->getParent());
772     }
773
774     // Relocations are written out in reverse order, so the PAIR comes first.
775     if (Type == macho::RIT_Difference ||
776         Type == macho::RIT_Generic_LocalDifference) {
777       macho::RelocationEntry MRE;
778       MRE.Word0 = ((0         <<  0) |
779                    (macho::RIT_Pair  << 24) |
780                    (Log2Size  << 28) |
781                    (IsPCRel   << 30) |
782                    macho::RF_Scattered);
783       MRE.Word1 = Value2;
784       Relocations[Fragment->getParent()].push_back(MRE);
785     }
786
787     macho::RelocationEntry MRE;
788     MRE.Word0 = ((FixupOffset <<  0) |
789                  (Type        << 24) |
790                  (Log2Size    << 28) |
791                  (IsPCRel     << 30) |
792                  macho::RF_Scattered);
793     MRE.Word1 = Value;
794     Relocations[Fragment->getParent()].push_back(MRE);
795   }
796
797   void RecordTLVPRelocation(const MCAssembler &Asm,
798                             const MCAsmLayout &Layout,
799                             const MCFragment *Fragment,
800                             const MCFixup &Fixup, MCValue Target,
801                             uint64_t &FixedValue) {
802     assert(Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP &&
803            !is64Bit() &&
804            "Should only be called with a 32-bit TLVP relocation!");
805
806     unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
807     uint32_t Value = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
808     unsigned IsPCRel = 0;
809
810     // Get the symbol data.
811     MCSymbolData *SD_A = &Asm.getSymbolData(Target.getSymA()->getSymbol());
812     unsigned Index = SD_A->getIndex();
813
814     // We're only going to have a second symbol in pic mode and it'll be a
815     // subtraction from the picbase. For 32-bit pic the addend is the difference
816     // between the picbase and the next address.  For 32-bit static the addend
817     // is zero.
818     if (Target.getSymB()) {
819       // If this is a subtraction then we're pcrel.
820       uint32_t FixupAddress =
821         getFragmentAddress(Fragment, Layout) + Fixup.getOffset();
822       MCSymbolData *SD_B = &Asm.getSymbolData(Target.getSymB()->getSymbol());
823       IsPCRel = 1;
824       FixedValue = (FixupAddress - getSymbolAddress(SD_B, Layout) +
825                     Target.getConstant());
826       FixedValue += 1ULL << Log2Size;
827     } else {
828       FixedValue = 0;
829     }
830
831     // struct relocation_info (8 bytes)
832     macho::RelocationEntry MRE;
833     MRE.Word0 = Value;
834     MRE.Word1 = ((Index                  <<  0) |
835                  (IsPCRel                << 24) |
836                  (Log2Size               << 25) |
837                  (1                      << 27) | // Extern
838                  (macho::RIT_Generic_TLV << 28)); // Type
839     Relocations[Fragment->getParent()].push_back(MRE);
840   }
841
842   void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
843                         const MCFragment *Fragment, const MCFixup &Fixup,
844                         MCValue Target, uint64_t &FixedValue) {
845     if (is64Bit()) {
846       RecordX86_64Relocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
847       return;
848     }
849
850     unsigned IsPCRel = isFixupKindPCRel(Asm, Fixup.getKind());
851     unsigned Log2Size = getFixupKindLog2Size(Fixup.getKind());
852
853     // If this is a 32-bit TLVP reloc it's handled a bit differently.
854     if (Target.getSymA() &&
855         Target.getSymA()->getKind() == MCSymbolRefExpr::VK_TLVP) {
856       RecordTLVPRelocation(Asm, Layout, Fragment, Fixup, Target, FixedValue);
857       return;
858     }
859
860     // If this is a difference or a defined symbol plus an offset, then we need
861     // a scattered relocation entry.
862     // Differences always require scattered relocations.
863     if (Target.getSymB())
864         return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
865                                          Target, FixedValue);
866
867     // Get the symbol data, if any.
868     MCSymbolData *SD = 0;
869     if (Target.getSymA())
870       SD = &Asm.getSymbolData(Target.getSymA()->getSymbol());
871
872     // If this is an internal relocation with an offset, it also needs a
873     // scattered relocation entry.
874     uint32_t Offset = Target.getConstant();
875     if (IsPCRel)
876       Offset += 1 << Log2Size;
877     if (Offset && SD && !doesSymbolRequireExternRelocation(SD))
878       return RecordScatteredRelocation(Asm, Layout, Fragment, Fixup,
879                                        Target, FixedValue);
880
881     // See <reloc.h>.
882     uint32_t FixupOffset = Layout.getFragmentOffset(Fragment)+Fixup.getOffset();
883     unsigned Index = 0;
884     unsigned IsExtern = 0;
885     unsigned Type = 0;
886
887     if (Target.isAbsolute()) { // constant
888       // SymbolNum of 0 indicates the absolute section.
889       //
890       // FIXME: Currently, these are never generated (see code below). I cannot
891       // find a case where they are actually emitted.
892       Type = macho::RIT_Vanilla;
893     } else if (SD->getSymbol().isVariable()) {
894       const MCExpr *Value = SD->getSymbol().getVariableValue();
895       int64_t Res;
896       bool isAbs = Value->EvaluateAsAbsolute(Res, Layout, SectionAddress);
897       if (isAbs) {
898         FixedValue = Res;
899         return;
900       } else {
901         report_fatal_error("unsupported relocation of variable '" +
902                            SD->getSymbol().getName() + "'");
903       }
904     } else {
905       // Check whether we need an external or internal relocation.
906       if (doesSymbolRequireExternRelocation(SD)) {
907         IsExtern = 1;
908         Index = SD->getIndex();
909         // For external relocations, make sure to offset the fixup value to
910         // compensate for the addend of the symbol address, if it was
911         // undefined. This occurs with weak definitions, for example.
912         if (!SD->Symbol->isUndefined())
913           FixedValue -= Layout.getSymbolOffset(SD);
914       } else {
915         // The index is the section ordinal (1-based).
916         Index = SD->getFragment()->getParent()->getOrdinal() + 1;
917         FixedValue += getSectionAddress(SD->getFragment()->getParent());
918       }
919       if (IsPCRel)
920         FixedValue -= getSectionAddress(Fragment->getParent());
921
922       Type = macho::RIT_Vanilla;
923     }
924
925     // struct relocation_info (8 bytes)
926     macho::RelocationEntry MRE;
927     MRE.Word0 = FixupOffset;
928     MRE.Word1 = ((Index     <<  0) |
929                  (IsPCRel   << 24) |
930                  (Log2Size  << 25) |
931                  (IsExtern  << 27) |
932                  (Type      << 28));
933     Relocations[Fragment->getParent()].push_back(MRE);
934   }
935
936   void BindIndirectSymbols(MCAssembler &Asm) {
937     // This is the point where 'as' creates actual symbols for indirect symbols
938     // (in the following two passes). It would be easier for us to do this
939     // sooner when we see the attribute, but that makes getting the order in the
940     // symbol table much more complicated than it is worth.
941     //
942     // FIXME: Revisit this when the dust settles.
943
944     // Bind non lazy symbol pointers first.
945     unsigned IndirectIndex = 0;
946     for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
947            ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
948       const MCSectionMachO &Section =
949         cast<MCSectionMachO>(it->SectionData->getSection());
950
951       if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
952         continue;
953
954       // Initialize the section indirect symbol base, if necessary.
955       if (!IndirectSymBase.count(it->SectionData))
956         IndirectSymBase[it->SectionData] = IndirectIndex;
957
958       Asm.getOrCreateSymbolData(*it->Symbol);
959     }
960
961     // Then lazy symbol pointers and symbol stubs.
962     IndirectIndex = 0;
963     for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
964            ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
965       const MCSectionMachO &Section =
966         cast<MCSectionMachO>(it->SectionData->getSection());
967
968       if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
969           Section.getType() != MCSectionMachO::S_SYMBOL_STUBS)
970         continue;
971
972       // Initialize the section indirect symbol base, if necessary.
973       if (!IndirectSymBase.count(it->SectionData))
974         IndirectSymBase[it->SectionData] = IndirectIndex;
975
976       // Set the symbol type to undefined lazy, but only on construction.
977       //
978       // FIXME: Do not hardcode.
979       bool Created;
980       MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
981       if (Created)
982         Entry.setFlags(Entry.getFlags() | 0x0001);
983     }
984   }
985
986   /// ComputeSymbolTable - Compute the symbol table data
987   ///
988   /// \param StringTable [out] - The string table data.
989   /// \param StringIndexMap [out] - Map from symbol names to offsets in the
990   /// string table.
991   void ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
992                           std::vector<MachSymbolData> &LocalSymbolData,
993                           std::vector<MachSymbolData> &ExternalSymbolData,
994                           std::vector<MachSymbolData> &UndefinedSymbolData) {
995     // Build section lookup table.
996     DenseMap<const MCSection*, uint8_t> SectionIndexMap;
997     unsigned Index = 1;
998     for (MCAssembler::iterator it = Asm.begin(),
999            ie = Asm.end(); it != ie; ++it, ++Index)
1000       SectionIndexMap[&it->getSection()] = Index;
1001     assert(Index <= 256 && "Too many sections!");
1002
1003     // Index 0 is always the empty string.
1004     StringMap<uint64_t> StringIndexMap;
1005     StringTable += '\x00';
1006
1007     // Build the symbol arrays and the string table, but only for non-local
1008     // symbols.
1009     //
1010     // The particular order that we collect the symbols and create the string
1011     // table, then sort the symbols is chosen to match 'as'. Even though it
1012     // doesn't matter for correctness, this is important for letting us diff .o
1013     // files.
1014     for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
1015            ie = Asm.symbol_end(); it != ie; ++it) {
1016       const MCSymbol &Symbol = it->getSymbol();
1017
1018       // Ignore non-linker visible symbols.
1019       if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
1020         continue;
1021
1022       if (!it->isExternal() && !Symbol.isUndefined())
1023         continue;
1024
1025       uint64_t &Entry = StringIndexMap[Symbol.getName()];
1026       if (!Entry) {
1027         Entry = StringTable.size();
1028         StringTable += Symbol.getName();
1029         StringTable += '\x00';
1030       }
1031
1032       MachSymbolData MSD;
1033       MSD.SymbolData = it;
1034       MSD.StringIndex = Entry;
1035
1036       if (Symbol.isUndefined()) {
1037         MSD.SectionIndex = 0;
1038         UndefinedSymbolData.push_back(MSD);
1039       } else if (Symbol.isAbsolute()) {
1040         MSD.SectionIndex = 0;
1041         ExternalSymbolData.push_back(MSD);
1042       } else {
1043         MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
1044         assert(MSD.SectionIndex && "Invalid section index!");
1045         ExternalSymbolData.push_back(MSD);
1046       }
1047     }
1048
1049     // Now add the data for local symbols.
1050     for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
1051            ie = Asm.symbol_end(); it != ie; ++it) {
1052       const MCSymbol &Symbol = it->getSymbol();
1053
1054       // Ignore non-linker visible symbols.
1055       if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
1056         continue;
1057
1058       if (it->isExternal() || Symbol.isUndefined())
1059         continue;
1060
1061       uint64_t &Entry = StringIndexMap[Symbol.getName()];
1062       if (!Entry) {
1063         Entry = StringTable.size();
1064         StringTable += Symbol.getName();
1065         StringTable += '\x00';
1066       }
1067
1068       MachSymbolData MSD;
1069       MSD.SymbolData = it;
1070       MSD.StringIndex = Entry;
1071
1072       if (Symbol.isAbsolute()) {
1073         MSD.SectionIndex = 0;
1074         LocalSymbolData.push_back(MSD);
1075       } else {
1076         MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
1077         assert(MSD.SectionIndex && "Invalid section index!");
1078         LocalSymbolData.push_back(MSD);
1079       }
1080     }
1081
1082     // External and undefined symbols are required to be in lexicographic order.
1083     std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
1084     std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
1085
1086     // Set the symbol indices.
1087     Index = 0;
1088     for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1089       LocalSymbolData[i].SymbolData->setIndex(Index++);
1090     for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1091       ExternalSymbolData[i].SymbolData->setIndex(Index++);
1092     for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1093       UndefinedSymbolData[i].SymbolData->setIndex(Index++);
1094
1095     // The string table is padded to a multiple of 4.
1096     while (StringTable.size() % 4)
1097       StringTable += '\x00';
1098   }
1099
1100   void computeSectionAddresses(const MCAssembler &Asm,
1101                                const MCAsmLayout &Layout) {
1102     uint64_t StartAddress = 0;
1103     const SmallVectorImpl<MCSectionData*> &Order = Layout.getSectionOrder();
1104     for (int i = 0, n = Order.size(); i != n ; ++i) {
1105       const MCSectionData *SD = Order[i];
1106       StartAddress = RoundUpToAlignment(StartAddress, SD->getAlignment());
1107       SectionAddress[SD] = StartAddress;
1108       StartAddress += Layout.getSectionAddressSize(SD);
1109       // Explicitly pad the section to match the alignment requirements of the
1110       // following one. This is for 'gas' compatibility, it shouldn't
1111       /// strictly be necessary.
1112       StartAddress += getPaddingSize(SD, Layout);
1113     }
1114   }
1115
1116   void ExecutePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout) {
1117     computeSectionAddresses(Asm, Layout);
1118
1119     // Create symbol data for any indirect symbols.
1120     BindIndirectSymbols(Asm);
1121
1122     // Compute symbol table information and bind symbol indices.
1123     ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
1124                        UndefinedSymbolData);
1125   }
1126
1127   bool IsSymbolRefDifferenceFullyResolved(const MCAssembler &Asm,
1128                                           const MCSymbolRefExpr *A,
1129                                           const MCSymbolRefExpr *B,
1130                                           bool InSet) const {
1131     if (InSet)
1132       return true;
1133
1134     if (!TargetObjectWriter->useAggressiveSymbolFolding())
1135       return false;
1136
1137     // The effective address is
1138     //     addr(atom(A)) + offset(A)
1139     //   - addr(atom(B)) - offset(B)
1140     // and the offsets are not relocatable, so the fixup is fully resolved when
1141     //  addr(atom(A)) - addr(atom(B)) == 0.
1142     const MCSymbolData *A_Base = 0, *B_Base = 0;
1143
1144     // Modified symbol references cannot be resolved.
1145     if (A->getKind() != MCSymbolRefExpr::VK_None ||
1146         B->getKind() != MCSymbolRefExpr::VK_None)
1147       return false;
1148
1149     A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
1150     if (!A_Base)
1151       return false;
1152
1153     B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
1154     if (!B_Base)
1155       return false;
1156
1157     // If the atoms are the same, they are guaranteed to have the same address.
1158     if (A_Base == B_Base)
1159       return true;
1160
1161     // Otherwise, we can't prove this is fully resolved.
1162     return false;
1163   }
1164
1165   bool IsFixupFullyResolved(const MCAssembler &Asm,
1166                             const MCValue Target,
1167                             bool IsPCRel,
1168                             const MCFragment *DF) const {
1169     // Otherwise, determine whether this value is actually resolved; scattering
1170     // may cause atoms to move.
1171
1172     // Check if we are using the "simple" resolution algorithm (e.g.,
1173     // i386).
1174     if (!Asm.getBackend().hasReliableSymbolDifference()) {
1175       const MCSection *BaseSection = 0;
1176       if (IsPCRel)
1177         BaseSection = &DF->getParent()->getSection();
1178
1179       return isScatteredFixupFullyResolvedSimple(Asm, Target, BaseSection);
1180     }
1181
1182     // Otherwise, compute the proper answer as reliably as possible.
1183
1184     // If this is a PCrel relocation, find the base atom (identified by its
1185     // symbol) that the fixup value is relative to.
1186     const MCSymbolData *BaseSymbol = 0;
1187     if (IsPCRel) {
1188       BaseSymbol = DF->getAtom();
1189       if (!BaseSymbol)
1190         return false;
1191     }
1192
1193     return isScatteredFixupFullyResolved(Asm, Target, BaseSymbol);
1194   }
1195
1196   void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) {
1197     unsigned NumSections = Asm.size();
1198
1199     // The section data starts after the header, the segment load command (and
1200     // section headers) and the symbol table.
1201     unsigned NumLoadCommands = 1;
1202     uint64_t LoadCommandsSize = is64Bit() ?
1203       macho::SegmentLoadCommand64Size + NumSections * macho::Section64Size :
1204       macho::SegmentLoadCommand32Size + NumSections * macho::Section32Size;
1205
1206     // Add the symbol table load command sizes, if used.
1207     unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
1208       UndefinedSymbolData.size();
1209     if (NumSymbols) {
1210       NumLoadCommands += 2;
1211       LoadCommandsSize += (macho::SymtabLoadCommandSize +
1212                            macho::DysymtabLoadCommandSize);
1213     }
1214
1215     // Compute the total size of the section data, as well as its file size and
1216     // vm size.
1217     uint64_t SectionDataStart = (is64Bit() ? macho::Header64Size :
1218                                  macho::Header32Size) + LoadCommandsSize;
1219     uint64_t SectionDataSize = 0;
1220     uint64_t SectionDataFileSize = 0;
1221     uint64_t VMSize = 0;
1222     for (MCAssembler::const_iterator it = Asm.begin(),
1223            ie = Asm.end(); it != ie; ++it) {
1224       const MCSectionData &SD = *it;
1225       uint64_t Address = getSectionAddress(&SD);
1226       uint64_t Size = Layout.getSectionAddressSize(&SD);
1227       uint64_t FileSize = Layout.getSectionFileSize(&SD);
1228       FileSize += getPaddingSize(&SD, Layout);
1229
1230       VMSize = std::max(VMSize, Address + Size);
1231
1232       if (SD.getSection().isVirtualSection())
1233         continue;
1234
1235       SectionDataSize = std::max(SectionDataSize, Address + Size);
1236       SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize);
1237     }
1238
1239     // The section data is padded to 4 bytes.
1240     //
1241     // FIXME: Is this machine dependent?
1242     unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
1243     SectionDataFileSize += SectionDataPadding;
1244
1245     // Write the prolog, starting with the header and load command...
1246     WriteHeader(NumLoadCommands, LoadCommandsSize,
1247                 Asm.getSubsectionsViaSymbols());
1248     WriteSegmentLoadCommand(NumSections, VMSize,
1249                             SectionDataStart, SectionDataSize);
1250
1251     // ... and then the section headers.
1252     uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
1253     for (MCAssembler::const_iterator it = Asm.begin(),
1254            ie = Asm.end(); it != ie; ++it) {
1255       std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
1256       unsigned NumRelocs = Relocs.size();
1257       uint64_t SectionStart = SectionDataStart + getSectionAddress(it);
1258       WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
1259       RelocTableEnd += NumRelocs * macho::RelocationInfoSize;
1260     }
1261
1262     // Write the symbol table load command, if used.
1263     if (NumSymbols) {
1264       unsigned FirstLocalSymbol = 0;
1265       unsigned NumLocalSymbols = LocalSymbolData.size();
1266       unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
1267       unsigned NumExternalSymbols = ExternalSymbolData.size();
1268       unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
1269       unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
1270       unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
1271       unsigned NumSymTabSymbols =
1272         NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
1273       uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
1274       uint64_t IndirectSymbolOffset = 0;
1275
1276       // If used, the indirect symbols are written after the section data.
1277       if (NumIndirectSymbols)
1278         IndirectSymbolOffset = RelocTableEnd;
1279
1280       // The symbol table is written after the indirect symbol data.
1281       uint64_t SymbolTableOffset = RelocTableEnd + IndirectSymbolSize;
1282
1283       // The string table is written after symbol table.
1284       uint64_t StringTableOffset =
1285         SymbolTableOffset + NumSymTabSymbols * (is64Bit() ? macho::Nlist64Size :
1286                                                 macho::Nlist32Size);
1287       WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
1288                              StringTableOffset, StringTable.size());
1289
1290       WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
1291                                FirstExternalSymbol, NumExternalSymbols,
1292                                FirstUndefinedSymbol, NumUndefinedSymbols,
1293                                IndirectSymbolOffset, NumIndirectSymbols);
1294     }
1295
1296     // Write the actual section data.
1297     for (MCAssembler::const_iterator it = Asm.begin(),
1298            ie = Asm.end(); it != ie; ++it) {
1299       Asm.WriteSectionData(it, Layout);
1300
1301       uint64_t Pad = getPaddingSize(it, Layout);
1302       for (unsigned int i = 0; i < Pad; ++i)
1303         Write8(0);
1304     }
1305
1306     // Write the extra padding.
1307     WriteZeros(SectionDataPadding);
1308
1309     // Write the relocation entries.
1310     for (MCAssembler::const_iterator it = Asm.begin(),
1311            ie = Asm.end(); it != ie; ++it) {
1312       // Write the section relocation entries, in reverse order to match 'as'
1313       // (approximately, the exact algorithm is more complicated than this).
1314       std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
1315       for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1316         Write32(Relocs[e - i - 1].Word0);
1317         Write32(Relocs[e - i - 1].Word1);
1318       }
1319     }
1320
1321     // Write the symbol table data, if used.
1322     if (NumSymbols) {
1323       // Write the indirect symbol entries.
1324       for (MCAssembler::const_indirect_symbol_iterator
1325              it = Asm.indirect_symbol_begin(),
1326              ie = Asm.indirect_symbol_end(); it != ie; ++it) {
1327         // Indirect symbols in the non lazy symbol pointer section have some
1328         // special handling.
1329         const MCSectionMachO &Section =
1330           static_cast<const MCSectionMachO&>(it->SectionData->getSection());
1331         if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
1332           // If this symbol is defined and internal, mark it as such.
1333           if (it->Symbol->isDefined() &&
1334               !Asm.getSymbolData(*it->Symbol).isExternal()) {
1335             uint32_t Flags = macho::ISF_Local;
1336             if (it->Symbol->isAbsolute())
1337               Flags |= macho::ISF_Absolute;
1338             Write32(Flags);
1339             continue;
1340           }
1341         }
1342
1343         Write32(Asm.getSymbolData(*it->Symbol).getIndex());
1344       }
1345
1346       // FIXME: Check that offsets match computed ones.
1347
1348       // Write the symbol table entries.
1349       for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1350         WriteNlist(LocalSymbolData[i], Layout);
1351       for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1352         WriteNlist(ExternalSymbolData[i], Layout);
1353       for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1354         WriteNlist(UndefinedSymbolData[i], Layout);
1355
1356       // Write the string table.
1357       OS << StringTable.str();
1358     }
1359   }
1360 };
1361
1362 }
1363
1364 MCObjectWriter *llvm::createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
1365                                              raw_ostream &OS,
1366                                              bool IsLittleEndian) {
1367   return new MachObjectWriter(MOTW, OS, IsLittleEndian);
1368 }