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