OSDN Git Service

am 1c4ad5ef: Merge branch \'upstream\' into merge-2012_09_10
[android-x86/external-llvm.git] / lib / MC / MCAssembler.cpp
1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
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 #define DEBUG_TYPE "assembler"
11 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/MC/MCAsmLayout.h"
13 #include "llvm/MC/MCCodeEmitter.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFixupKindInfo.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCValue.h"
21 #include "llvm/MC/MCDwarf.h"
22 #include "llvm/MC/MCAsmBackend.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/TargetRegistry.h"
30 #include "llvm/Support/LEB128.h"
31
32 using namespace llvm;
33
34 namespace {
35 namespace stats {
36 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
37 STATISTIC(evaluateFixup, "Number of evaluated fixups");
38 STATISTIC(FragmentLayouts, "Number of fragment layouts");
39 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
40 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
41 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
42 }
43 }
44
45 // FIXME FIXME FIXME: There are number of places in this file where we convert
46 // what is a 64-bit assembler value used for computation into a value in the
47 // object file, which may truncate it. We should detect that truncation where
48 // invalid and report errors back.
49
50 /* *** */
51
52 MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
53   : Assembler(Asm), LastValidFragment()
54  {
55   // Compute the section layout order. Virtual sections must go last.
56   for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
57     if (!it->getSection().isVirtualSection())
58       SectionOrder.push_back(&*it);
59   for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
60     if (it->getSection().isVirtualSection())
61       SectionOrder.push_back(&*it);
62 }
63
64 bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const {
65   const MCSectionData &SD = *F->getParent();
66   const MCFragment *LastValid = LastValidFragment.lookup(&SD);
67   if (!LastValid)
68     return false;
69   assert(LastValid->getParent() == F->getParent());
70   return F->getLayoutOrder() <= LastValid->getLayoutOrder();
71 }
72
73 void MCAsmLayout::Invalidate(MCFragment *F) {
74   // If this fragment wasn't already up-to-date, we don't need to do anything.
75   if (!isFragmentUpToDate(F))
76     return;
77
78   // Otherwise, reset the last valid fragment to this fragment.
79   const MCSectionData &SD = *F->getParent();
80   LastValidFragment[&SD] = F;
81 }
82
83 void MCAsmLayout::EnsureValid(const MCFragment *F) const {
84   MCSectionData &SD = *F->getParent();
85
86   MCFragment *Cur = LastValidFragment[&SD];
87   if (!Cur)
88     Cur = &*SD.begin();
89   else
90     Cur = Cur->getNextNode();
91
92   // Advance the layout position until the fragment is up-to-date.
93   while (!isFragmentUpToDate(F)) {
94     const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur);
95     Cur = Cur->getNextNode();
96   }
97 }
98
99 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
100   EnsureValid(F);
101   assert(F->Offset != ~UINT64_C(0) && "Address not set!");
102   return F->Offset;
103 }
104
105 uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
106   const MCSymbol &S = SD->getSymbol();
107
108   // If this is a variable, then recursively evaluate now.
109   if (S.isVariable()) {
110     MCValue Target;
111     if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this))
112       report_fatal_error("unable to evaluate offset for variable '" +
113                          S.getName() + "'");
114
115     // Verify that any used symbols are defined.
116     if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
117       report_fatal_error("unable to evaluate offset to undefined symbol '" +
118                          Target.getSymA()->getSymbol().getName() + "'");
119     if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
120       report_fatal_error("unable to evaluate offset to undefined symbol '" +
121                          Target.getSymB()->getSymbol().getName() + "'");
122
123     uint64_t Offset = Target.getConstant();
124     if (Target.getSymA())
125       Offset += getSymbolOffset(&Assembler.getSymbolData(
126                                   Target.getSymA()->getSymbol()));
127     if (Target.getSymB())
128       Offset -= getSymbolOffset(&Assembler.getSymbolData(
129                                   Target.getSymB()->getSymbol()));
130     return Offset;
131   }
132
133   assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
134   return getFragmentOffset(SD->getFragment()) + SD->getOffset();
135 }
136
137 uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
138   // The size is the last fragment's end offset.
139   const MCFragment &F = SD->getFragmentList().back();
140   return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
141 }
142
143 uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
144   // Virtual sections have no file size.
145   if (SD->getSection().isVirtualSection())
146     return 0;
147
148   // Otherwise, the file size is the same as the address space size.
149   return getSectionAddressSize(SD);
150 }
151
152 /* *** */
153
154 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
155 }
156
157 MCFragment::~MCFragment() {
158 }
159
160 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
161   : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0)),
162     LayoutOrder(~(0U))
163 {
164   if (Parent)
165     Parent->getFragmentList().push_back(this);
166 }
167
168 /* *** */
169
170 MCSectionData::MCSectionData() : Section(0) {}
171
172 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
173   : Section(&_Section),
174     Ordinal(~UINT32_C(0)),
175     Alignment(1),
176     HasInstructions(false)
177 {
178   if (A)
179     A->getSectionList().push_back(this);
180 }
181
182 /* *** */
183
184 MCSymbolData::MCSymbolData() : Symbol(0) {}
185
186 MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
187                            uint64_t _Offset, MCAssembler *A)
188   : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
189     IsExternal(false), IsPrivateExtern(false),
190     CommonSize(0), SymbolSize(0), CommonAlign(0),
191     Flags(0), Index(0)
192 {
193   if (A)
194     A->getSymbolList().push_back(this);
195 }
196
197 /* *** */
198
199 MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
200                          MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
201                          raw_ostream &OS_)
202   : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(&Writer_),
203     OS(OS_), RelaxAll(false), NoExecStack(false), SubsectionsViaSymbols(false)
204 {
205 }
206
207 MCAssembler::~MCAssembler() {
208 }
209
210 void MCAssembler::setWriter(MCObjectWriter &ObjectWriter) {
211   delete Writer;
212   Writer = &ObjectWriter;
213 }
214
215 bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
216   // Non-temporary labels should always be visible to the linker.
217   if (!Symbol.isTemporary())
218     return true;
219
220   // Absolute temporary labels are never visible.
221   if (!Symbol.isInSection())
222     return false;
223
224   // Otherwise, check if the section requires symbols even for temporary labels.
225   return getBackend().doesSectionRequireSymbols(Symbol.getSection());
226 }
227
228 const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
229   // Linker visible symbols define atoms.
230   if (isSymbolLinkerVisible(SD->getSymbol()))
231     return SD;
232
233   // Absolute and undefined symbols have no defining atom.
234   if (!SD->getFragment())
235     return 0;
236
237   // Non-linker visible symbols in sections which can't be atomized have no
238   // defining atom.
239   if (!getBackend().isSectionAtomizable(
240         SD->getFragment()->getParent()->getSection()))
241     return 0;
242
243   // Otherwise, return the atom for the containing fragment.
244   return SD->getFragment()->getAtom();
245 }
246
247 bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
248                                 const MCFixup &Fixup, const MCFragment *DF,
249                                 MCValue &Target, uint64_t &Value) const {
250   ++stats::evaluateFixup;
251
252   if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout))
253     getContext().FatalError(Fixup.getLoc(), "expected relocatable expression");
254
255   bool IsPCRel = Backend.getFixupKindInfo(
256     Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
257
258   bool IsResolved;
259   if (IsPCRel) {
260     if (Target.getSymB()) {
261       IsResolved = false;
262     } else if (!Target.getSymA()) {
263       IsResolved = false;
264     } else {
265       const MCSymbolRefExpr *A = Target.getSymA();
266       const MCSymbol &SA = A->getSymbol();
267       if (A->getKind() != MCSymbolRefExpr::VK_None ||
268           SA.AliasedSymbol().isUndefined()) {
269         IsResolved = false;
270       } else {
271         const MCSymbolData &DataA = getSymbolData(SA);
272         IsResolved =
273           getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
274                                                              *DF, false, true);
275       }
276     }
277   } else {
278     IsResolved = Target.isAbsolute();
279   }
280
281   Value = Target.getConstant();
282
283   if (const MCSymbolRefExpr *A = Target.getSymA()) {
284     const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
285     if (Sym.isDefined())
286       Value += Layout.getSymbolOffset(&getSymbolData(Sym));
287   }
288   if (const MCSymbolRefExpr *B = Target.getSymB()) {
289     const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
290     if (Sym.isDefined())
291       Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
292   }
293
294
295   bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
296                          MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
297   assert((ShouldAlignPC ? IsPCRel : true) &&
298     "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
299
300   if (IsPCRel) {
301     uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
302
303     // A number of ARM fixups in Thumb mode require that the effective PC
304     // address be determined as the 32-bit aligned version of the actual offset.
305     if (ShouldAlignPC) Offset &= ~0x3;
306     Value -= Offset;
307   }
308
309   // Let the backend adjust the fixup value if necessary, including whether
310   // we need a relocation.
311   Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value,
312                             IsResolved);
313
314   return IsResolved;
315 }
316
317 uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
318                                           const MCFragment &F) const {
319   switch (F.getKind()) {
320   case MCFragment::FT_Data:
321     return cast<MCDataFragment>(F).getContents().size();
322   case MCFragment::FT_Fill:
323     return cast<MCFillFragment>(F).getSize();
324   case MCFragment::FT_Inst:
325     return cast<MCInstFragment>(F).getInstSize();
326
327   case MCFragment::FT_LEB:
328     return cast<MCLEBFragment>(F).getContents().size();
329
330   case MCFragment::FT_Align: {
331     const MCAlignFragment &AF = cast<MCAlignFragment>(F);
332     unsigned Offset = Layout.getFragmentOffset(&AF);
333     unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
334     // If we are padding with nops, force the padding to be larger than the
335     // minimum nop size.
336     if (Size > 0 && AF.hasEmitNops()) {
337       while (Size % getBackend().getMinimumNopSize())
338         Size += AF.getAlignment();
339     }
340     if (Size > AF.getMaxBytesToEmit())
341       return 0;
342     return Size;
343   }
344
345   case MCFragment::FT_Org: {
346     MCOrgFragment &OF = cast<MCOrgFragment>(F);
347     int64_t TargetLocation;
348     if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
349       report_fatal_error("expected assembly-time absolute expression");
350
351     // FIXME: We need a way to communicate this error.
352     uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
353     int64_t Size = TargetLocation - FragmentOffset;
354     if (Size < 0 || Size >= 0x40000000)
355       report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
356                          "' (at offset '" + Twine(FragmentOffset) + "')");
357     return Size;
358   }
359
360   case MCFragment::FT_Dwarf:
361     return cast<MCDwarfLineAddrFragment>(F).getContents().size();
362   case MCFragment::FT_DwarfFrame:
363     return cast<MCDwarfCallFrameFragment>(F).getContents().size();
364   }
365
366   llvm_unreachable("invalid fragment kind");
367 }
368
369 void MCAsmLayout::LayoutFragment(MCFragment *F) {
370   MCFragment *Prev = F->getPrevNode();
371
372   // We should never try to recompute something which is up-to-date.
373   assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!");
374   // We should never try to compute the fragment layout if it's predecessor
375   // isn't up-to-date.
376   assert((!Prev || isFragmentUpToDate(Prev)) &&
377          "Attempt to compute fragment before it's predecessor!");
378
379   ++stats::FragmentLayouts;
380
381   // Compute fragment offset and size.
382   uint64_t Offset = 0;
383   if (Prev)
384     Offset += Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
385
386   F->Offset = Offset;
387   LastValidFragment[F->getParent()] = F;
388 }
389
390 /// WriteFragmentData - Write the \arg F data to the output file.
391 static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
392                               const MCFragment &F) {
393   MCObjectWriter *OW = &Asm.getWriter();
394   uint64_t Start = OW->getStream().tell();
395   (void) Start;
396
397   ++stats::EmittedFragments;
398
399   // FIXME: Embed in fragments instead?
400   uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
401   switch (F.getKind()) {
402   case MCFragment::FT_Align: {
403     MCAlignFragment &AF = cast<MCAlignFragment>(F);
404     uint64_t Count = FragmentSize / AF.getValueSize();
405
406     assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
407
408     // FIXME: This error shouldn't actually occur (the front end should emit
409     // multiple .align directives to enforce the semantics it wants), but is
410     // severe enough that we want to report it. How to handle this?
411     if (Count * AF.getValueSize() != FragmentSize)
412       report_fatal_error("undefined .align directive, value size '" +
413                         Twine(AF.getValueSize()) +
414                         "' is not a divisor of padding size '" +
415                         Twine(FragmentSize) + "'");
416
417     // See if we are aligning with nops, and if so do that first to try to fill
418     // the Count bytes.  Then if that did not fill any bytes or there are any
419     // bytes left to fill use the Value and ValueSize to fill the rest.
420     // If we are aligning with nops, ask that target to emit the right data.
421     if (AF.hasEmitNops()) {
422       if (!Asm.getBackend().writeNopData(Count, OW))
423         report_fatal_error("unable to write nop sequence of " +
424                           Twine(Count) + " bytes");
425       break;
426     }
427
428     // Otherwise, write out in multiples of the value size.
429     for (uint64_t i = 0; i != Count; ++i) {
430       switch (AF.getValueSize()) {
431       default: llvm_unreachable("Invalid size!");
432       case 1: OW->Write8 (uint8_t (AF.getValue())); break;
433       case 2: OW->Write16(uint16_t(AF.getValue())); break;
434       case 4: OW->Write32(uint32_t(AF.getValue())); break;
435       case 8: OW->Write64(uint64_t(AF.getValue())); break;
436       }
437     }
438     break;
439   }
440
441   case MCFragment::FT_Data: {
442     MCDataFragment &DF = cast<MCDataFragment>(F);
443     assert(FragmentSize == DF.getContents().size() && "Invalid size!");
444     OW->WriteBytes(DF.getContents().str());
445     break;
446   }
447
448   case MCFragment::FT_Fill: {
449     MCFillFragment &FF = cast<MCFillFragment>(F);
450
451     assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
452
453     for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
454       switch (FF.getValueSize()) {
455       default: llvm_unreachable("Invalid size!");
456       case 1: OW->Write8 (uint8_t (FF.getValue())); break;
457       case 2: OW->Write16(uint16_t(FF.getValue())); break;
458       case 4: OW->Write32(uint32_t(FF.getValue())); break;
459       case 8: OW->Write64(uint64_t(FF.getValue())); break;
460       }
461     }
462     break;
463   }
464
465   case MCFragment::FT_Inst: {
466     MCInstFragment &IF = cast<MCInstFragment>(F);
467     OW->WriteBytes(StringRef(IF.getCode().begin(), IF.getCode().size()));
468     break;
469   }
470
471   case MCFragment::FT_LEB: {
472     MCLEBFragment &LF = cast<MCLEBFragment>(F);
473     OW->WriteBytes(LF.getContents().str());
474     break;
475   }
476
477   case MCFragment::FT_Org: {
478     MCOrgFragment &OF = cast<MCOrgFragment>(F);
479
480     for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
481       OW->Write8(uint8_t(OF.getValue()));
482
483     break;
484   }
485
486   case MCFragment::FT_Dwarf: {
487     const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
488     OW->WriteBytes(OF.getContents().str());
489     break;
490   }
491   case MCFragment::FT_DwarfFrame: {
492     const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
493     OW->WriteBytes(CF.getContents().str());
494     break;
495   }
496   }
497
498   assert(OW->getStream().tell() - Start == FragmentSize);
499 }
500
501 void MCAssembler::writeSectionData(const MCSectionData *SD,
502                                    const MCAsmLayout &Layout) const {
503   // Ignore virtual sections.
504   if (SD->getSection().isVirtualSection()) {
505     assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
506
507     // Check that contents are only things legal inside a virtual section.
508     for (MCSectionData::const_iterator it = SD->begin(),
509            ie = SD->end(); it != ie; ++it) {
510       switch (it->getKind()) {
511       default: llvm_unreachable("Invalid fragment in virtual section!");
512       case MCFragment::FT_Data: {
513         // Check that we aren't trying to write a non-zero contents (or fixups)
514         // into a virtual section. This is to support clients which use standard
515         // directives to fill the contents of virtual sections.
516         MCDataFragment &DF = cast<MCDataFragment>(*it);
517         assert(DF.fixup_begin() == DF.fixup_end() &&
518                "Cannot have fixups in virtual section!");
519         for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
520           assert(DF.getContents()[i] == 0 &&
521                  "Invalid data value for virtual section!");
522         break;
523       }
524       case MCFragment::FT_Align:
525         // Check that we aren't trying to write a non-zero value into a virtual
526         // section.
527         assert((!cast<MCAlignFragment>(it)->getValueSize() ||
528                 !cast<MCAlignFragment>(it)->getValue()) &&
529                "Invalid align in virtual section!");
530         break;
531       case MCFragment::FT_Fill:
532         assert(!cast<MCFillFragment>(it)->getValueSize() &&
533                "Invalid fill in virtual section!");
534         break;
535       }
536     }
537
538     return;
539   }
540
541   uint64_t Start = getWriter().getStream().tell();
542   (void) Start;
543
544   for (MCSectionData::const_iterator it = SD->begin(),
545          ie = SD->end(); it != ie; ++it)
546     WriteFragmentData(*this, Layout, *it);
547
548   assert(getWriter().getStream().tell() - Start ==
549          Layout.getSectionAddressSize(SD));
550 }
551
552
553 uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout,
554                                   MCFragment &F,
555                                   const MCFixup &Fixup) {
556    // Evaluate the fixup.
557    MCValue Target;
558    uint64_t FixedValue;
559    if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
560      // The fixup was unresolved, we need a relocation. Inform the object
561      // writer of the relocation, and give it an opportunity to adjust the
562      // fixup value if need be.
563      getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
564    }
565    return FixedValue;
566  }
567
568 void MCAssembler::Finish() {
569   DEBUG_WITH_TYPE("mc-dump", {
570       llvm::errs() << "assembler backend - pre-layout\n--\n";
571       dump(); });
572
573   // Create the layout object.
574   MCAsmLayout Layout(*this);
575
576   // Create dummy fragments and assign section ordinals.
577   unsigned SectionIndex = 0;
578   for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
579     // Create dummy fragments to eliminate any empty sections, this simplifies
580     // layout.
581     if (it->getFragmentList().empty())
582       new MCDataFragment(it);
583
584     it->setOrdinal(SectionIndex++);
585   }
586
587   // Assign layout order indices to sections and fragments.
588   for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
589     MCSectionData *SD = Layout.getSectionOrder()[i];
590     SD->setLayoutOrder(i);
591
592     unsigned FragmentIndex = 0;
593     for (MCSectionData::iterator it2 = SD->begin(),
594            ie2 = SD->end(); it2 != ie2; ++it2)
595       it2->setLayoutOrder(FragmentIndex++);
596   }
597
598   // Layout until everything fits.
599   while (layoutOnce(Layout))
600     continue;
601
602   DEBUG_WITH_TYPE("mc-dump", {
603       llvm::errs() << "assembler backend - post-relaxation\n--\n";
604       dump(); });
605
606   // Finalize the layout, including fragment lowering.
607   finishLayout(Layout);
608
609   DEBUG_WITH_TYPE("mc-dump", {
610       llvm::errs() << "assembler backend - final-layout\n--\n";
611       dump(); });
612
613   uint64_t StartOffset = OS.tell();
614
615   // Allow the object writer a chance to perform post-layout binding (for
616   // example, to set the index fields in the symbol data).
617   getWriter().ExecutePostLayoutBinding(*this, Layout);
618
619   // Evaluate and apply the fixups, generating relocation entries as necessary.
620   for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
621     for (MCSectionData::iterator it2 = it->begin(),
622            ie2 = it->end(); it2 != ie2; ++it2) {
623       MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
624       if (DF) {
625         for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
626                ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
627           MCFixup &Fixup = *it3;
628           uint64_t FixedValue = handleFixup(Layout, *DF, Fixup);
629           getBackend().applyFixup(Fixup, DF->getContents().data(),
630                                   DF->getContents().size(), FixedValue);
631         }
632       }
633       MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
634       if (IF) {
635         for (MCInstFragment::fixup_iterator it3 = IF->fixup_begin(),
636                ie3 = IF->fixup_end(); it3 != ie3; ++it3) {
637           MCFixup &Fixup = *it3;
638           uint64_t FixedValue = handleFixup(Layout, *IF, Fixup);
639           getBackend().applyFixup(Fixup, IF->getCode().data(),
640                                   IF->getCode().size(), FixedValue);
641         }
642       }
643     }
644   }
645
646   // Write the object file.
647   getWriter().WriteObject(*this, Layout);
648
649   stats::ObjectBytes += OS.tell() - StartOffset;
650 }
651
652 bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
653                                        const MCInstFragment *DF,
654                                        const MCAsmLayout &Layout) const {
655   if (getRelaxAll())
656     return true;
657
658   // If we cannot resolve the fixup value, it requires relaxation.
659   MCValue Target;
660   uint64_t Value;
661   if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
662     return true;
663
664   return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
665 }
666
667 bool MCAssembler::fragmentNeedsRelaxation(const MCInstFragment *IF,
668                                           const MCAsmLayout &Layout) const {
669   // If this inst doesn't ever need relaxation, ignore it. This occurs when we
670   // are intentionally pushing out inst fragments, or because we relaxed a
671   // previous instruction to one that doesn't need relaxation.
672   if (!getBackend().mayNeedRelaxation(IF->getInst()))
673     return false;
674
675   for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
676          ie = IF->fixup_end(); it != ie; ++it)
677     if (fixupNeedsRelaxation(*it, IF, Layout))
678       return true;
679
680   return false;
681 }
682
683 bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
684                                    MCInstFragment &IF) {
685   if (!fragmentNeedsRelaxation(&IF, Layout))
686     return false;
687
688   ++stats::RelaxedInstructions;
689
690   // FIXME-PERF: We could immediately lower out instructions if we can tell
691   // they are fully resolved, to avoid retesting on later passes.
692
693   // Relax the fragment.
694
695   MCInst Relaxed;
696   getBackend().relaxInstruction(IF.getInst(), Relaxed);
697
698   // Encode the new instruction.
699   //
700   // FIXME-PERF: If it matters, we could let the target do this. It can
701   // probably do so more efficiently in many cases.
702   SmallVector<MCFixup, 4> Fixups;
703   SmallString<256> Code;
704   raw_svector_ostream VecOS(Code);
705   getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
706   VecOS.flush();
707
708   // Update the instruction fragment.
709   IF.setInst(Relaxed);
710   IF.getCode() = Code;
711   IF.getFixups().clear();
712   // FIXME: Eliminate copy.
713   for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
714     IF.getFixups().push_back(Fixups[i]);
715
716   return true;
717 }
718
719 bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
720   int64_t Value = 0;
721   uint64_t OldSize = LF.getContents().size();
722   bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout);
723   (void)IsAbs;
724   assert(IsAbs);
725   SmallString<8> &Data = LF.getContents();
726   Data.clear();
727   raw_svector_ostream OSE(Data);
728   if (LF.isSigned())
729     encodeSLEB128(Value, OSE);
730   else
731     encodeULEB128(Value, OSE);
732   OSE.flush();
733   return OldSize != LF.getContents().size();
734 }
735
736 bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
737                                      MCDwarfLineAddrFragment &DF) {
738   int64_t AddrDelta = 0;
739   uint64_t OldSize = DF.getContents().size();
740   bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
741   (void)IsAbs;
742   assert(IsAbs);
743   int64_t LineDelta;
744   LineDelta = DF.getLineDelta();
745   SmallString<8> &Data = DF.getContents();
746   Data.clear();
747   raw_svector_ostream OSE(Data);
748   MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE);
749   OSE.flush();
750   return OldSize != Data.size();
751 }
752
753 bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
754                                               MCDwarfCallFrameFragment &DF) {
755   int64_t AddrDelta = 0;
756   uint64_t OldSize = DF.getContents().size();
757   bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
758   (void)IsAbs;
759   assert(IsAbs);
760   SmallString<8> &Data = DF.getContents();
761   Data.clear();
762   raw_svector_ostream OSE(Data);
763   MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE);
764   OSE.flush();
765   return OldSize != Data.size();
766 }
767
768 bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout,
769                                     MCSectionData &SD) {
770   MCFragment *FirstInvalidFragment = NULL;
771   // Scan for fragments that need relaxation.
772   for (MCSectionData::iterator it2 = SD.begin(),
773          ie2 = SD.end(); it2 != ie2; ++it2) {
774     // Check if this is an fragment that needs relaxation.
775     bool relaxedFrag = false;
776     switch(it2->getKind()) {
777     default:
778           break;
779     case MCFragment::FT_Inst:
780       relaxedFrag = relaxInstruction(Layout, *cast<MCInstFragment>(it2));
781       break;
782     case MCFragment::FT_Dwarf:
783       relaxedFrag = relaxDwarfLineAddr(Layout,
784                                        *cast<MCDwarfLineAddrFragment>(it2));
785       break;
786     case MCFragment::FT_DwarfFrame:
787       relaxedFrag =
788         relaxDwarfCallFrameFragment(Layout,
789                                     *cast<MCDwarfCallFrameFragment>(it2));
790       break;
791     case MCFragment::FT_LEB:
792       relaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(it2));
793       break;
794     }
795     // Update the layout, and remember that we relaxed.
796     if (relaxedFrag && !FirstInvalidFragment)
797       FirstInvalidFragment = it2;
798   }
799   if (FirstInvalidFragment) {
800     Layout.Invalidate(FirstInvalidFragment);
801     return true;
802   }
803   return false;
804 }
805
806 bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
807   ++stats::RelaxationSteps;
808
809   bool WasRelaxed = false;
810   for (iterator it = begin(), ie = end(); it != ie; ++it) {
811     MCSectionData &SD = *it;
812     while(layoutSectionOnce(Layout, SD))
813       WasRelaxed = true;
814   }
815
816   return WasRelaxed;
817 }
818
819 void MCAssembler::finishLayout(MCAsmLayout &Layout) {
820   // The layout is done. Mark every fragment as valid.
821   for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
822     Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
823   }
824 }
825
826 // Debugging methods
827
828 namespace llvm {
829
830 raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
831   OS << "<MCFixup" << " Offset:" << AF.getOffset()
832      << " Value:" << *AF.getValue()
833      << " Kind:" << AF.getKind() << ">";
834   return OS;
835 }
836
837 }
838
839 #ifndef NDEBUG
840 void MCFragment::dump() {
841   raw_ostream &OS = llvm::errs();
842
843   OS << "<";
844   switch (getKind()) {
845   case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
846   case MCFragment::FT_Data:  OS << "MCDataFragment"; break;
847   case MCFragment::FT_Fill:  OS << "MCFillFragment"; break;
848   case MCFragment::FT_Inst:  OS << "MCInstFragment"; break;
849   case MCFragment::FT_Org:   OS << "MCOrgFragment"; break;
850   case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
851   case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
852   case MCFragment::FT_LEB:   OS << "MCLEBFragment"; break;
853   }
854
855   OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
856      << " Offset:" << Offset << ">";
857
858   switch (getKind()) {
859   case MCFragment::FT_Align: {
860     const MCAlignFragment *AF = cast<MCAlignFragment>(this);
861     if (AF->hasEmitNops())
862       OS << " (emit nops)";
863     OS << "\n       ";
864     OS << " Alignment:" << AF->getAlignment()
865        << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
866        << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
867     break;
868   }
869   case MCFragment::FT_Data:  {
870     const MCDataFragment *DF = cast<MCDataFragment>(this);
871     OS << "\n       ";
872     OS << " Contents:[";
873     const SmallVectorImpl<char> &Contents = DF->getContents();
874     for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
875       if (i) OS << ",";
876       OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
877     }
878     OS << "] (" << Contents.size() << " bytes)";
879
880     if (!DF->getFixups().empty()) {
881       OS << ",\n       ";
882       OS << " Fixups:[";
883       for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
884              ie = DF->fixup_end(); it != ie; ++it) {
885         if (it != DF->fixup_begin()) OS << ",\n                ";
886         OS << *it;
887       }
888       OS << "]";
889     }
890     break;
891   }
892   case MCFragment::FT_Fill:  {
893     const MCFillFragment *FF = cast<MCFillFragment>(this);
894     OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
895        << " Size:" << FF->getSize();
896     break;
897   }
898   case MCFragment::FT_Inst:  {
899     const MCInstFragment *IF = cast<MCInstFragment>(this);
900     OS << "\n       ";
901     OS << " Inst:";
902     IF->getInst().dump_pretty(OS);
903     break;
904   }
905   case MCFragment::FT_Org:  {
906     const MCOrgFragment *OF = cast<MCOrgFragment>(this);
907     OS << "\n       ";
908     OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
909     break;
910   }
911   case MCFragment::FT_Dwarf:  {
912     const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
913     OS << "\n       ";
914     OS << " AddrDelta:" << OF->getAddrDelta()
915        << " LineDelta:" << OF->getLineDelta();
916     break;
917   }
918   case MCFragment::FT_DwarfFrame:  {
919     const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
920     OS << "\n       ";
921     OS << " AddrDelta:" << CF->getAddrDelta();
922     break;
923   }
924   case MCFragment::FT_LEB: {
925     const MCLEBFragment *LF = cast<MCLEBFragment>(this);
926     OS << "\n       ";
927     OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
928     break;
929   }
930   }
931   OS << ">";
932 }
933
934 void MCSectionData::dump() {
935   raw_ostream &OS = llvm::errs();
936
937   OS << "<MCSectionData";
938   OS << " Alignment:" << getAlignment() << " Fragments:[\n      ";
939   for (iterator it = begin(), ie = end(); it != ie; ++it) {
940     if (it != begin()) OS << ",\n      ";
941     it->dump();
942   }
943   OS << "]>";
944 }
945
946 void MCSymbolData::dump() {
947   raw_ostream &OS = llvm::errs();
948
949   OS << "<MCSymbolData Symbol:" << getSymbol()
950      << " Fragment:" << getFragment() << " Offset:" << getOffset()
951      << " Flags:" << getFlags() << " Index:" << getIndex();
952   if (isCommon())
953     OS << " (common, size:" << getCommonSize()
954        << " align: " << getCommonAlignment() << ")";
955   if (isExternal())
956     OS << " (external)";
957   if (isPrivateExtern())
958     OS << " (private extern)";
959   OS << ">";
960 }
961
962 void MCAssembler::dump() {
963   raw_ostream &OS = llvm::errs();
964
965   OS << "<MCAssembler\n";
966   OS << "  Sections:[\n    ";
967   for (iterator it = begin(), ie = end(); it != ie; ++it) {
968     if (it != begin()) OS << ",\n    ";
969     it->dump();
970   }
971   OS << "],\n";
972   OS << "  Symbols:[";
973
974   for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
975     if (it != symbol_begin()) OS << ",\n           ";
976     it->dump();
977   }
978   OS << "]>\n";
979 }
980 #endif
981
982 // anchors for MC*Fragment vtables
983 void MCDataFragment::anchor() { }
984 void MCInstFragment::anchor() { }
985 void MCAlignFragment::anchor() { }
986 void MCFillFragment::anchor() { }
987 void MCOrgFragment::anchor() { }
988 void MCLEBFragment::anchor() { }
989 void MCDwarfLineAddrFragment::anchor() { }
990 void MCDwarfCallFrameFragment::anchor() { }