Silence compiler warning.
[llvm.git] / lib / MC / MCAssembler.cpp
blob506f2a650a1eae0e4d11f99545c4242428505492
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 //===----------------------------------------------------------------------===//
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/MCExpr.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSymbol.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetRegistry.h"
26 #include "llvm/Target/TargetAsmBackend.h"
28 #include <vector>
29 using namespace llvm;
31 namespace {
32 namespace stats {
33 STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
34 STATISTIC(EvaluateFixup, "Number of evaluated fixups");
35 STATISTIC(FragmentLayouts, "Number of fragment layouts");
36 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
37 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
38 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
39 STATISTIC(SectionLayouts, "Number of section layouts");
43 // FIXME FIXME FIXME: There are number of places in this file where we convert
44 // what is a 64-bit assembler value used for computation into a value in the
45 // object file, which may truncate it. We should detect that truncation where
46 // invalid and report errors back.
48 /* *** */
50 MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
51 : Assembler(Asm), LastValidFragment(0)
53 // Compute the section layout order. Virtual sections must go last.
54 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
55 if (!Asm.getBackend().isVirtualSection(it->getSection()))
56 SectionOrder.push_back(&*it);
57 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
58 if (Asm.getBackend().isVirtualSection(it->getSection()))
59 SectionOrder.push_back(&*it);
62 bool MCAsmLayout::isSectionUpToDate(const MCSectionData *SD) const {
63 // The first section is always up-to-date.
64 unsigned Index = SD->getLayoutOrder();
65 if (!Index)
66 return true;
68 // Otherwise, sections are always implicitly computed when the preceeding
69 // fragment is layed out.
70 const MCSectionData *Prev = getSectionOrder()[Index - 1];
71 return isFragmentUpToDate(&(Prev->getFragmentList().back()));
74 bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const {
75 return (LastValidFragment &&
76 F->getLayoutOrder() <= LastValidFragment->getLayoutOrder());
79 void MCAsmLayout::UpdateForSlide(MCFragment *F, int SlideAmount) {
80 // If this fragment wasn't already up-to-date, we don't need to do anything.
81 if (!isFragmentUpToDate(F))
82 return;
84 // Otherwise, reset the last valid fragment to the predecessor of the
85 // invalidated fragment.
86 LastValidFragment = F->getPrevNode();
87 if (!LastValidFragment) {
88 unsigned Index = F->getParent()->getLayoutOrder();
89 if (Index != 0) {
90 MCSectionData *Prev = getSectionOrder()[Index - 1];
91 LastValidFragment = &(Prev->getFragmentList().back());
96 void MCAsmLayout::EnsureValid(const MCFragment *F) const {
97 // Advance the layout position until the fragment is up-to-date.
98 while (!isFragmentUpToDate(F)) {
99 // Advance to the next fragment.
100 MCFragment *Cur = LastValidFragment;
101 if (Cur)
102 Cur = Cur->getNextNode();
103 if (!Cur) {
104 unsigned NextIndex = 0;
105 if (LastValidFragment)
106 NextIndex = LastValidFragment->getParent()->getLayoutOrder() + 1;
107 Cur = SectionOrder[NextIndex]->begin();
110 const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur);
114 void MCAsmLayout::FragmentReplaced(MCFragment *Src, MCFragment *Dst) {
115 if (LastValidFragment == Src)
116 LastValidFragment = Dst;
118 Dst->Offset = Src->Offset;
119 Dst->EffectiveSize = Src->EffectiveSize;
122 uint64_t MCAsmLayout::getFragmentAddress(const MCFragment *F) const {
123 assert(F->getParent() && "Missing section()!");
124 return getSectionAddress(F->getParent()) + getFragmentOffset(F);
127 uint64_t MCAsmLayout::getFragmentEffectiveSize(const MCFragment *F) const {
128 EnsureValid(F);
129 assert(F->EffectiveSize != ~UINT64_C(0) && "Address not set!");
130 return F->EffectiveSize;
133 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
134 EnsureValid(F);
135 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
136 return F->Offset;
139 uint64_t MCAsmLayout::getSymbolAddress(const MCSymbolData *SD) const {
140 assert(SD->getFragment() && "Invalid getAddress() on undefined symbol!");
141 return getFragmentAddress(SD->getFragment()) + SD->getOffset();
144 uint64_t MCAsmLayout::getSectionAddress(const MCSectionData *SD) const {
145 EnsureValid(SD->begin());
146 assert(SD->Address != ~UINT64_C(0) && "Address not set!");
147 return SD->Address;
150 uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
151 // The size is the last fragment's end offset.
152 const MCFragment &F = SD->getFragmentList().back();
153 return getFragmentOffset(&F) + getFragmentEffectiveSize(&F);
156 uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
157 // Virtual sections have no file size.
158 if (getAssembler().getBackend().isVirtualSection(SD->getSection()))
159 return 0;
161 // Otherwise, the file size is the same as the address space size.
162 return getSectionAddressSize(SD);
165 uint64_t MCAsmLayout::getSectionSize(const MCSectionData *SD) const {
166 // The logical size is the address space size minus any tail padding.
167 uint64_t Size = getSectionAddressSize(SD);
168 const MCAlignFragment *AF =
169 dyn_cast<MCAlignFragment>(&(SD->getFragmentList().back()));
170 if (AF && AF->hasOnlyAlignAddress())
171 Size -= getFragmentEffectiveSize(AF);
173 return Size;
176 /* *** */
178 MCFragment::MCFragment() : Kind(FragmentType(~0)) {
181 MCFragment::~MCFragment() {
184 MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
185 : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0)),
186 EffectiveSize(~UINT64_C(0))
188 if (Parent)
189 Parent->getFragmentList().push_back(this);
192 /* *** */
194 MCSectionData::MCSectionData() : Section(0) {}
196 MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
197 : Section(&_Section),
198 Alignment(1),
199 Address(~UINT64_C(0)),
200 HasInstructions(false)
202 if (A)
203 A->getSectionList().push_back(this);
206 /* *** */
208 MCSymbolData::MCSymbolData() : Symbol(0) {}
210 MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
211 uint64_t _Offset, MCAssembler *A)
212 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
213 IsExternal(false), IsPrivateExtern(false),
214 CommonSize(0), SymbolSize(0), CommonAlign(0),
215 Flags(0), Index(0)
217 if (A)
218 A->getSymbolList().push_back(this);
221 /* *** */
223 MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
224 MCCodeEmitter &_Emitter, bool _PadSectionToAlignment,
225 raw_ostream &_OS)
226 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
227 OS(_OS), RelaxAll(false), SubsectionsViaSymbols(false),
228 PadSectionToAlignment(_PadSectionToAlignment)
232 MCAssembler::~MCAssembler() {
235 static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
236 const MCFixup &Fixup,
237 const MCValue Target,
238 const MCSection *BaseSection) {
239 // The effective fixup address is
240 // addr(atom(A)) + offset(A)
241 // - addr(atom(B)) - offset(B)
242 // - addr(<base symbol>) + <fixup offset from base symbol>
243 // and the offsets are not relocatable, so the fixup is fully resolved when
244 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
246 // The simple (Darwin, except on x86_64) way of dealing with this was to
247 // assume that any reference to a temporary symbol *must* be a temporary
248 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
249 // relocation to a temporary symbol (in the same section) is fully
250 // resolved. This also works in conjunction with absolutized .set, which
251 // requires the compiler to use .set to absolutize the differences between
252 // symbols which the compiler knows to be assembly time constants, so we don't
253 // need to worry about considering symbol differences fully resolved.
255 // Non-relative fixups are only resolved if constant.
256 if (!BaseSection)
257 return Target.isAbsolute();
259 // Otherwise, relative fixups are only resolved if not a difference and the
260 // target is a temporary in the same section.
261 if (Target.isAbsolute() || Target.getSymB())
262 return false;
264 const MCSymbol *A = &Target.getSymA()->getSymbol();
265 if (!A->isTemporary() || !A->isInSection() ||
266 &A->getSection() != BaseSection)
267 return false;
269 return true;
272 static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
273 const MCAsmLayout &Layout,
274 const MCFixup &Fixup,
275 const MCValue Target,
276 const MCSymbolData *BaseSymbol) {
277 // The effective fixup address is
278 // addr(atom(A)) + offset(A)
279 // - addr(atom(B)) - offset(B)
280 // - addr(BaseSymbol) + <fixup offset from base symbol>
281 // and the offsets are not relocatable, so the fixup is fully resolved when
282 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
284 // Note that "false" is almost always conservatively correct (it means we emit
285 // a relocation which is unnecessary), except when it would force us to emit a
286 // relocation which the target cannot encode.
288 const MCSymbolData *A_Base = 0, *B_Base = 0;
289 if (const MCSymbolRefExpr *A = Target.getSymA()) {
290 // Modified symbol references cannot be resolved.
291 if (A->getKind() != MCSymbolRefExpr::VK_None)
292 return false;
294 A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
295 if (!A_Base)
296 return false;
299 if (const MCSymbolRefExpr *B = Target.getSymB()) {
300 // Modified symbol references cannot be resolved.
301 if (B->getKind() != MCSymbolRefExpr::VK_None)
302 return false;
304 B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
305 if (!B_Base)
306 return false;
309 // If there is no base, A and B have to be the same atom for this fixup to be
310 // fully resolved.
311 if (!BaseSymbol)
312 return A_Base == B_Base;
314 // Otherwise, B must be missing and A must be the base.
315 return !B_Base && BaseSymbol == A_Base;
318 bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
319 // Non-temporary labels should always be visible to the linker.
320 if (!Symbol.isTemporary())
321 return true;
323 // Absolute temporary labels are never visible.
324 if (!Symbol.isInSection())
325 return false;
327 // Otherwise, check if the section requires symbols even for temporary labels.
328 return getBackend().doesSectionRequireSymbols(Symbol.getSection());
331 const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
332 // Linker visible symbols define atoms.
333 if (isSymbolLinkerVisible(SD->getSymbol()))
334 return SD;
336 // Absolute and undefined symbols have no defining atom.
337 if (!SD->getFragment())
338 return 0;
340 // Non-linker visible symbols in sections which can't be atomized have no
341 // defining atom.
342 if (!getBackend().isSectionAtomizable(
343 SD->getFragment()->getParent()->getSection()))
344 return 0;
346 // Otherwise, return the atom for the containing fragment.
347 return SD->getFragment()->getAtom();
350 bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
351 const MCFixup &Fixup, const MCFragment *DF,
352 MCValue &Target, uint64_t &Value) const {
353 ++stats::EvaluateFixup;
355 if (!Fixup.getValue()->EvaluateAsRelocatable(Target, &Layout))
356 report_fatal_error("expected relocatable expression");
358 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
359 // doesn't support small relocations, but then under what criteria does the
360 // assembler allow symbol differences?
362 Value = Target.getConstant();
364 bool IsPCRel = Emitter.getFixupKindInfo(
365 Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
366 bool IsResolved = true;
367 if (const MCSymbolRefExpr *A = Target.getSymA()) {
368 if (A->getSymbol().isDefined())
369 Value += Layout.getSymbolAddress(&getSymbolData(A->getSymbol()));
370 else
371 IsResolved = false;
373 if (const MCSymbolRefExpr *B = Target.getSymB()) {
374 if (B->getSymbol().isDefined())
375 Value -= Layout.getSymbolAddress(&getSymbolData(B->getSymbol()));
376 else
377 IsResolved = false;
380 // If we are using scattered symbols, determine whether this value is actually
381 // resolved; scattering may cause atoms to move.
382 if (IsResolved && getBackend().hasScatteredSymbols()) {
383 if (getBackend().hasReliableSymbolDifference()) {
384 // If this is a PCrel relocation, find the base atom (identified by its
385 // symbol) that the fixup value is relative to.
386 const MCSymbolData *BaseSymbol = 0;
387 if (IsPCRel) {
388 BaseSymbol = DF->getAtom();
389 if (!BaseSymbol)
390 IsResolved = false;
393 if (IsResolved)
394 IsResolved = isScatteredFixupFullyResolved(*this, Layout, Fixup, Target,
395 BaseSymbol);
396 } else {
397 const MCSection *BaseSection = 0;
398 if (IsPCRel)
399 BaseSection = &DF->getParent()->getSection();
401 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
402 BaseSection);
406 if (IsPCRel)
407 Value -= Layout.getFragmentAddress(DF) + Fixup.getOffset();
409 return IsResolved;
412 uint64_t MCAssembler::ComputeFragmentSize(MCAsmLayout &Layout,
413 const MCFragment &F,
414 uint64_t SectionAddress,
415 uint64_t FragmentOffset) const {
416 switch (F.getKind()) {
417 case MCFragment::FT_Data:
418 return cast<MCDataFragment>(F).getContents().size();
419 case MCFragment::FT_Fill:
420 return cast<MCFillFragment>(F).getSize();
421 case MCFragment::FT_Inst:
422 return cast<MCInstFragment>(F).getInstSize();
424 case MCFragment::FT_Align: {
425 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
427 assert((!AF.hasOnlyAlignAddress() || !AF.getNextNode()) &&
428 "Invalid OnlyAlignAddress bit, not the last fragment!");
430 uint64_t Size = OffsetToAlignment(SectionAddress + FragmentOffset,
431 AF.getAlignment());
433 // Honor MaxBytesToEmit.
434 if (Size > AF.getMaxBytesToEmit())
435 return 0;
437 return Size;
440 case MCFragment::FT_Org: {
441 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
443 // FIXME: We should compute this sooner, we don't want to recurse here, and
444 // we would like to be more functional.
445 int64_t TargetLocation;
446 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
447 report_fatal_error("expected assembly-time absolute expression");
449 // FIXME: We need a way to communicate this error.
450 int64_t Offset = TargetLocation - FragmentOffset;
451 if (Offset < 0 || Offset >= 0x40000000)
452 report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
453 "' (at offset '" + Twine(FragmentOffset) + "')");
455 return Offset;
459 assert(0 && "invalid fragment kind");
460 return 0;
463 void MCAsmLayout::LayoutFile() {
464 // Initialize the first section and set the valid fragment layout point. All
465 // actual layout computations are done lazily.
466 LastValidFragment = 0;
467 if (!getSectionOrder().empty())
468 getSectionOrder().front()->Address = 0;
471 void MCAsmLayout::LayoutFragment(MCFragment *F) {
472 MCFragment *Prev = F->getPrevNode();
474 // We should never try to recompute something which is up-to-date.
475 assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!");
476 // We should never try to compute the fragment layout if the section isn't
477 // up-to-date.
478 assert(isSectionUpToDate(F->getParent()) &&
479 "Attempt to compute fragment before it's section!");
480 // We should never try to compute the fragment layout if it's predecessor
481 // isn't up-to-date.
482 assert((!Prev || isFragmentUpToDate(Prev)) &&
483 "Attempt to compute fragment before it's predecessor!");
485 ++stats::FragmentLayouts;
487 // Compute the fragment start address.
488 uint64_t StartAddress = F->getParent()->Address;
489 uint64_t Address = StartAddress;
490 if (Prev)
491 Address += Prev->Offset + Prev->EffectiveSize;
493 // Compute fragment offset and size.
494 F->Offset = Address - StartAddress;
495 F->EffectiveSize = getAssembler().ComputeFragmentSize(*this, *F, StartAddress,
496 F->Offset);
497 LastValidFragment = F;
499 // If this is the last fragment in a section, update the next section address.
500 if (!F->getNextNode()) {
501 unsigned NextIndex = F->getParent()->getLayoutOrder() + 1;
502 if (NextIndex != getSectionOrder().size())
503 LayoutSection(getSectionOrder()[NextIndex]);
507 void MCAsmLayout::LayoutSection(MCSectionData *SD) {
508 unsigned SectionOrderIndex = SD->getLayoutOrder();
510 ++stats::SectionLayouts;
512 // Compute the section start address.
513 uint64_t StartAddress = 0;
514 if (SectionOrderIndex) {
515 MCSectionData *Prev = getSectionOrder()[SectionOrderIndex - 1];
516 StartAddress = getSectionAddress(Prev) + getSectionAddressSize(Prev);
519 // Honor the section alignment requirements.
520 StartAddress = RoundUpToAlignment(StartAddress, SD->getAlignment());
522 // Set the section address.
523 SD->Address = StartAddress;
526 /// WriteFragmentData - Write the \arg F data to the output file.
527 static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
528 const MCFragment &F, MCObjectWriter *OW) {
529 uint64_t Start = OW->getStream().tell();
530 (void) Start;
532 ++stats::EmittedFragments;
534 // FIXME: Embed in fragments instead?
535 uint64_t FragmentSize = Layout.getFragmentEffectiveSize(&F);
536 switch (F.getKind()) {
537 case MCFragment::FT_Align: {
538 MCAlignFragment &AF = cast<MCAlignFragment>(F);
539 uint64_t Count = FragmentSize / AF.getValueSize();
541 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
543 // FIXME: This error shouldn't actually occur (the front end should emit
544 // multiple .align directives to enforce the semantics it wants), but is
545 // severe enough that we want to report it. How to handle this?
546 if (Count * AF.getValueSize() != FragmentSize)
547 report_fatal_error("undefined .align directive, value size '" +
548 Twine(AF.getValueSize()) +
549 "' is not a divisor of padding size '" +
550 Twine(FragmentSize) + "'");
552 // See if we are aligning with nops, and if so do that first to try to fill
553 // the Count bytes. Then if that did not fill any bytes or there are any
554 // bytes left to fill use the the Value and ValueSize to fill the rest.
555 // If we are aligning with nops, ask that target to emit the right data.
556 if (AF.hasEmitNops()) {
557 if (!Asm.getBackend().WriteNopData(Count, OW))
558 report_fatal_error("unable to write nop sequence of " +
559 Twine(Count) + " bytes");
560 break;
563 // Otherwise, write out in multiples of the value size.
564 for (uint64_t i = 0; i != Count; ++i) {
565 switch (AF.getValueSize()) {
566 default:
567 assert(0 && "Invalid size!");
568 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
569 case 2: OW->Write16(uint16_t(AF.getValue())); break;
570 case 4: OW->Write32(uint32_t(AF.getValue())); break;
571 case 8: OW->Write64(uint64_t(AF.getValue())); break;
574 break;
577 case MCFragment::FT_Data: {
578 MCDataFragment &DF = cast<MCDataFragment>(F);
579 assert(FragmentSize == DF.getContents().size() && "Invalid size!");
580 OW->WriteBytes(DF.getContents().str());
581 break;
584 case MCFragment::FT_Fill: {
585 MCFillFragment &FF = cast<MCFillFragment>(F);
587 assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
589 for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
590 switch (FF.getValueSize()) {
591 default:
592 assert(0 && "Invalid size!");
593 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
594 case 2: OW->Write16(uint16_t(FF.getValue())); break;
595 case 4: OW->Write32(uint32_t(FF.getValue())); break;
596 case 8: OW->Write64(uint64_t(FF.getValue())); break;
599 break;
602 case MCFragment::FT_Inst:
603 llvm_unreachable("unexpected inst fragment after lowering");
604 break;
606 case MCFragment::FT_Org: {
607 MCOrgFragment &OF = cast<MCOrgFragment>(F);
609 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
610 OW->Write8(uint8_t(OF.getValue()));
612 break;
616 assert(OW->getStream().tell() - Start == FragmentSize);
619 void MCAssembler::WriteSectionData(const MCSectionData *SD,
620 const MCAsmLayout &Layout,
621 MCObjectWriter *OW) const {
622 // Ignore virtual sections.
623 if (getBackend().isVirtualSection(SD->getSection())) {
624 assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
626 // Check that contents are only things legal inside a virtual section.
627 for (MCSectionData::const_iterator it = SD->begin(),
628 ie = SD->end(); it != ie; ++it) {
629 switch (it->getKind()) {
630 default:
631 assert(0 && "Invalid fragment in virtual section!");
632 case MCFragment::FT_Data: {
633 // Check that we aren't trying to write a non-zero contents (or fixups)
634 // into a virtual section. This is to support clients which use standard
635 // directives to fill the contents of virtual sections.
636 MCDataFragment &DF = cast<MCDataFragment>(*it);
637 assert(DF.fixup_begin() == DF.fixup_end() &&
638 "Cannot have fixups in virtual section!");
639 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
640 assert(DF.getContents()[i] == 0 &&
641 "Invalid data value for virtual section!");
642 break;
644 case MCFragment::FT_Align:
645 // Check that we aren't trying to write a non-zero value into a virtual
646 // section.
647 assert((!cast<MCAlignFragment>(it)->getValueSize() ||
648 !cast<MCAlignFragment>(it)->getValue()) &&
649 "Invalid align in virtual section!");
650 break;
651 case MCFragment::FT_Fill:
652 assert(!cast<MCFillFragment>(it)->getValueSize() &&
653 "Invalid fill in virtual section!");
654 break;
658 return;
661 uint64_t Start = OW->getStream().tell();
662 (void) Start;
664 for (MCSectionData::const_iterator it = SD->begin(),
665 ie = SD->end(); it != ie; ++it)
666 WriteFragmentData(*this, Layout, *it, OW);
668 assert(OW->getStream().tell() - Start == Layout.getSectionFileSize(SD));
671 void MCAssembler::AddSectionToTheEnd(MCSectionData &SD, MCAsmLayout &Layout) {
672 // Create dummy fragments and assign section ordinals.
673 unsigned SectionIndex = 0;
674 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it)
675 SectionIndex++;
677 SD.setOrdinal(SectionIndex);
679 // Assign layout order indices to sections and fragments.
680 unsigned FragmentIndex = 0;
681 unsigned i = 0;
682 for (unsigned e = Layout.getSectionOrder().size(); i != e; ++i) {
683 MCSectionData *SD = Layout.getSectionOrder()[i];
685 for (MCSectionData::iterator it2 = SD->begin(),
686 ie2 = SD->end(); it2 != ie2; ++it2)
687 FragmentIndex++;
690 SD.setLayoutOrder(i);
691 for (MCSectionData::iterator it2 = SD.begin(),
692 ie2 = SD.end(); it2 != ie2; ++it2) {
693 it2->setLayoutOrder(FragmentIndex++);
695 Layout.getSectionOrder().push_back(&SD);
697 Layout.LayoutSection(&SD);
699 // Layout until everything fits.
700 while (LayoutOnce(Layout))
701 continue;
705 void MCAssembler::Finish(MCObjectWriter *Writer) {
706 DEBUG_WITH_TYPE("mc-dump", {
707 llvm::errs() << "assembler backend - pre-layout\n--\n";
708 dump(); });
710 // Create the layout object.
711 MCAsmLayout Layout(*this);
713 // Insert additional align fragments for concrete sections to explicitly pad
714 // the previous section to match their alignment requirements. This is for
715 // 'gas' compatibility, it shouldn't strictly be necessary.
716 if (PadSectionToAlignment) {
717 for (unsigned i = 1, e = Layout.getSectionOrder().size(); i < e; ++i) {
718 MCSectionData *SD = Layout.getSectionOrder()[i];
720 // Ignore sections without alignment requirements.
721 unsigned Align = SD->getAlignment();
722 if (Align <= 1)
723 continue;
725 // Ignore virtual sections, they don't cause file size modifications.
726 if (getBackend().isVirtualSection(SD->getSection()))
727 continue;
729 // Otherwise, create a new align fragment at the end of the previous
730 // section.
731 MCAlignFragment *AF = new MCAlignFragment(Align, 0, 1, Align,
732 Layout.getSectionOrder()[i - 1]);
733 AF->setOnlyAlignAddress(true);
737 // Create dummy fragments and assign section ordinals.
738 unsigned SectionIndex = 0;
739 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
740 // Create dummy fragments to eliminate any empty sections, this simplifies
741 // layout.
742 if (it->getFragmentList().empty())
743 new MCDataFragment(it);
745 it->setOrdinal(SectionIndex++);
748 // Assign layout order indices to sections and fragments.
749 unsigned FragmentIndex = 0;
750 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
751 MCSectionData *SD = Layout.getSectionOrder()[i];
752 SD->setLayoutOrder(i);
754 for (MCSectionData::iterator it2 = SD->begin(),
755 ie2 = SD->end(); it2 != ie2; ++it2)
756 it2->setLayoutOrder(FragmentIndex++);
759 // Layout until everything fits.
760 while (LayoutOnce(Layout))
761 continue;
763 DEBUG_WITH_TYPE("mc-dump", {
764 llvm::errs() << "assembler backend - post-relaxation\n--\n";
765 dump(); });
767 // Finalize the layout, including fragment lowering.
768 FinishLayout(Layout);
770 DEBUG_WITH_TYPE("mc-dump", {
771 llvm::errs() << "assembler backend - final-layout\n--\n";
772 dump(); });
774 uint64_t StartOffset = OS.tell();
776 llvm::OwningPtr<MCObjectWriter> OwnWriter(0);
777 if (Writer == 0) {
778 //no custom Writer_ : create the default one life-managed by OwningPtr
779 OwnWriter.reset(getBackend().createObjectWriter(OS));
780 Writer = OwnWriter.get();
781 if (!Writer)
782 report_fatal_error("unable to create object writer!");
785 // Allow the object writer a chance to perform post-layout binding (for
786 // example, to set the index fields in the symbol data).
787 Writer->ExecutePostLayoutBinding(*this);
789 // Evaluate and apply the fixups, generating relocation entries as necessary.
790 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
791 for (MCSectionData::iterator it2 = it->begin(),
792 ie2 = it->end(); it2 != ie2; ++it2) {
793 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
794 if (!DF)
795 continue;
797 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
798 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
799 MCFixup &Fixup = *it3;
801 // Evaluate the fixup.
802 MCValue Target;
803 uint64_t FixedValue;
804 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
805 // The fixup was unresolved, we need a relocation. Inform the object
806 // writer of the relocation, and give it an opportunity to adjust the
807 // fixup value if need be.
808 Writer->RecordRelocation(*this, Layout, DF, Fixup, Target,FixedValue);
811 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
816 // Write the object file.
817 Writer->WriteObject(*this, Layout);
819 stats::ObjectBytes += OS.tell() - StartOffset;
822 bool MCAssembler::FixupNeedsRelaxation(const MCFixup &Fixup,
823 const MCFragment *DF,
824 const MCAsmLayout &Layout) const {
825 if (getRelaxAll())
826 return true;
828 // If we cannot resolve the fixup value, it requires relaxation.
829 MCValue Target;
830 uint64_t Value;
831 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
832 return true;
834 // Otherwise, relax if the value is too big for a (signed) i8.
836 // FIXME: This is target dependent!
837 return int64_t(Value) != int64_t(int8_t(Value));
840 bool MCAssembler::FragmentNeedsRelaxation(const MCInstFragment *IF,
841 const MCAsmLayout &Layout) const {
842 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
843 // are intentionally pushing out inst fragments, or because we relaxed a
844 // previous instruction to one that doesn't need relaxation.
845 if (!getBackend().MayNeedRelaxation(IF->getInst()))
846 return false;
848 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
849 ie = IF->fixup_end(); it != ie; ++it)
850 if (FixupNeedsRelaxation(*it, IF, Layout))
851 return true;
853 return false;
856 bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
857 ++stats::RelaxationSteps;
859 // Layout the sections in order.
860 Layout.LayoutFile();
862 // Scan for fragments that need relaxation.
863 bool WasRelaxed = false;
864 for (iterator it = begin(), ie = end(); it != ie; ++it) {
865 MCSectionData &SD = *it;
867 for (MCSectionData::iterator it2 = SD.begin(),
868 ie2 = SD.end(); it2 != ie2; ++it2) {
869 // Check if this is an instruction fragment that needs relaxation.
870 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
871 if (!IF || !FragmentNeedsRelaxation(IF, Layout))
872 continue;
874 ++stats::RelaxedInstructions;
876 // FIXME-PERF: We could immediately lower out instructions if we can tell
877 // they are fully resolved, to avoid retesting on later passes.
879 // Relax the fragment.
881 MCInst Relaxed;
882 getBackend().RelaxInstruction(IF->getInst(), Relaxed);
884 // Encode the new instruction.
886 // FIXME-PERF: If it matters, we could let the target do this. It can
887 // probably do so more efficiently in many cases.
888 SmallVector<MCFixup, 4> Fixups;
889 SmallString<256> Code;
890 raw_svector_ostream VecOS(Code);
891 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
892 VecOS.flush();
894 // Update the instruction fragment.
895 int SlideAmount = Code.size() - IF->getInstSize();
896 IF->setInst(Relaxed);
897 IF->getCode() = Code;
898 IF->getFixups().clear();
899 // FIXME: Eliminate copy.
900 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
901 IF->getFixups().push_back(Fixups[i]);
903 // Update the layout, and remember that we relaxed.
904 Layout.UpdateForSlide(IF, SlideAmount);
905 WasRelaxed = true;
909 return WasRelaxed;
912 void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
913 // Lower out any instruction fragments, to simplify the fixup application and
914 // output.
916 // FIXME-PERF: We don't have to do this, but the assumption is that it is
917 // cheap (we will mostly end up eliminating fragments and appending on to data
918 // fragments), so the extra complexity downstream isn't worth it. Evaluate
919 // this assumption.
920 for (iterator it = begin(), ie = end(); it != ie; ++it) {
921 MCSectionData &SD = *it;
923 for (MCSectionData::iterator it2 = SD.begin(),
924 ie2 = SD.end(); it2 != ie2; ++it2) {
925 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
926 if (!IF)
927 continue;
929 // Create a new data fragment for the instruction.
931 // FIXME-PERF: Reuse previous data fragment if possible.
932 MCDataFragment *DF = new MCDataFragment();
933 SD.getFragmentList().insert(it2, DF);
935 // Update the data fragments layout data.
936 DF->setParent(IF->getParent());
937 DF->setAtom(IF->getAtom());
938 DF->setLayoutOrder(IF->getLayoutOrder());
939 Layout.FragmentReplaced(IF, DF);
941 // Copy in the data and the fixups.
942 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
943 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
944 DF->getFixups().push_back(IF->getFixups()[i]);
946 // Delete the instruction fragment and update the iterator.
947 SD.getFragmentList().erase(IF);
948 it2 = DF;
953 // Debugging methods
955 namespace llvm {
957 raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
958 OS << "<MCFixup" << " Offset:" << AF.getOffset()
959 << " Value:" << *AF.getValue()
960 << " Kind:" << AF.getKind() << ">";
961 return OS;
966 void MCFragment::dump() {
967 raw_ostream &OS = llvm::errs();
969 OS << "<";
970 switch (getKind()) {
971 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
972 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
973 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
974 case MCFragment::FT_Inst: OS << "MCInstFragment"; break;
975 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
978 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
979 << " Offset:" << Offset << " EffectiveSize:" << EffectiveSize << ">";
981 switch (getKind()) {
982 case MCFragment::FT_Align: {
983 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
984 if (AF->hasEmitNops())
985 OS << " (emit nops)";
986 if (AF->hasOnlyAlignAddress())
987 OS << " (only align section)";
988 OS << "\n ";
989 OS << " Alignment:" << AF->getAlignment()
990 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
991 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
992 break;
994 case MCFragment::FT_Data: {
995 const MCDataFragment *DF = cast<MCDataFragment>(this);
996 OS << "\n ";
997 OS << " Contents:[";
998 const SmallVectorImpl<char> &Contents = DF->getContents();
999 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
1000 if (i) OS << ",";
1001 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1003 OS << "] (" << Contents.size() << " bytes)";
1005 if (!DF->getFixups().empty()) {
1006 OS << ",\n ";
1007 OS << " Fixups:[";
1008 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
1009 ie = DF->fixup_end(); it != ie; ++it) {
1010 if (it != DF->fixup_begin()) OS << ",\n ";
1011 OS << *it;
1013 OS << "]";
1015 break;
1017 case MCFragment::FT_Fill: {
1018 const MCFillFragment *FF = cast<MCFillFragment>(this);
1019 OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
1020 << " Size:" << FF->getSize();
1021 break;
1023 case MCFragment::FT_Inst: {
1024 const MCInstFragment *IF = cast<MCInstFragment>(this);
1025 OS << "\n ";
1026 OS << " Inst:";
1027 IF->getInst().dump_pretty(OS);
1028 break;
1030 case MCFragment::FT_Org: {
1031 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
1032 OS << "\n ";
1033 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
1034 break;
1037 OS << ">";
1040 void MCSectionData::dump() {
1041 raw_ostream &OS = llvm::errs();
1043 OS << "<MCSectionData";
1044 OS << " Alignment:" << getAlignment() << " Address:" << Address
1045 << " Fragments:[\n ";
1046 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1047 if (it != begin()) OS << ",\n ";
1048 it->dump();
1050 OS << "]>";
1053 void MCSymbolData::dump() {
1054 raw_ostream &OS = llvm::errs();
1056 OS << "<MCSymbolData Symbol:" << getSymbol()
1057 << " Fragment:" << getFragment() << " Offset:" << getOffset()
1058 << " Flags:" << getFlags() << " Index:" << getIndex();
1059 if (isCommon())
1060 OS << " (common, size:" << getCommonSize()
1061 << " align: " << getCommonAlignment() << ")";
1062 if (isExternal())
1063 OS << " (external)";
1064 if (isPrivateExtern())
1065 OS << " (private extern)";
1066 OS << ">";
1069 void MCAssembler::dump() {
1070 raw_ostream &OS = llvm::errs();
1072 OS << "<MCAssembler\n";
1073 OS << " Sections:[\n ";
1074 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1075 if (it != begin()) OS << ",\n ";
1076 it->dump();
1078 OS << "],\n";
1079 OS << " Symbols:[";
1081 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1082 if (it != symbol_begin()) OS << ",\n ";
1083 it->dump();
1085 OS << "]>\n";