blob: 5fdc57ad30953b7ea89c6eae2e1705babb398788 [file] [log] [blame]
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001//===- 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
Daniel Dunbar0adcd352009-08-25 21:10:45 +000010#define DEBUG_TYPE "assembler"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000011#include "llvm/MC/MCAssembler.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000012#include "llvm/ADT/Statistic.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +000016#include "llvm/MC/MCAsmLayout.h"
Daniel Dunbarb36052f2010-03-19 10:43:23 +000017#include "llvm/MC/MCCodeEmitter.h"
Rafael Espindolafea753b2010-12-24 21:22:02 +000018#include "llvm/MC/MCContext.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/MC/MCDwarf.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000020#include "llvm/MC/MCExpr.h"
Craig Topperf1d0f772012-03-26 06:58:25 +000021#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000022#include "llvm/MC/MCObjectWriter.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000023#include "llvm/MC/MCSection.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000024#include "llvm/MC/MCSymbol.h"
25#include "llvm/MC/MCValue.h"
Daniel Dunbarac2884a2010-03-25 22:49:09 +000026#include "llvm/Support/Debug.h"
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000027#include "llvm/Support/ErrorHandling.h"
Jim Grosbach2d39a0e2012-08-08 23:56:06 +000028#include "llvm/Support/LEB128.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Support/TargetRegistry.h"
30#include "llvm/Support/raw_ostream.h"
Daniel Dunbarf6346762010-02-13 09:29:02 +000031
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000032using namespace llvm;
33
Daniel Dunbarff547842010-03-23 23:47:14 +000034namespace {
35namespace stats {
Eli Bendersky8ddc5a12012-12-07 17:59:21 +000036STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
Eli Bendersky6f6204f2013-01-08 17:41:59 +000037STATISTIC(EmittedRelaxableFragments,
38 "Number of emitted assembler fragments - relaxable");
Eli Bendersky6ac81f52012-12-10 18:59:39 +000039STATISTIC(EmittedDataFragments,
40 "Number of emitted assembler fragments - data");
41STATISTIC(EmittedAlignFragments,
42 "Number of emitted assembler fragments - align");
43STATISTIC(EmittedFillFragments,
44 "Number of emitted assembler fragments - fill");
45STATISTIC(EmittedOrgFragments,
46 "Number of emitted assembler fragments - org");
Jim Grosbachf77d5b12011-12-06 00:03:48 +000047STATISTIC(evaluateFixup, "Number of evaluated fixups");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000048STATISTIC(FragmentLayouts, "Number of fragment layouts");
Daniel Dunbarff547842010-03-23 23:47:14 +000049STATISTIC(ObjectBytes, "Number of emitted object file bytes");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000050STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
51STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
Daniel Dunbarff547842010-03-23 23:47:14 +000052}
53}
Daniel Dunbar0adcd352009-08-25 21:10:45 +000054
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000055// FIXME FIXME FIXME: There are number of places in this file where we convert
56// what is a 64-bit assembler value used for computation into a value in the
57// object file, which may truncate it. We should detect that truncation where
58// invalid and report errors back.
59
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000060/* *** */
61
Daniel Dunbar9005d452010-05-14 00:37:21 +000062MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
Rafael Espindola4f4363a2010-12-07 23:32:26 +000063 : Assembler(Asm), LastValidFragment()
Daniel Dunbar9005d452010-05-14 00:37:21 +000064 {
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000065 // Compute the section layout order. Virtual sections must go last.
66 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000067 if (!it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000068 SectionOrder.push_back(&*it);
69 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000070 if (it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000071 SectionOrder.push_back(&*it);
72}
73
Eli Benderskyd52a2c02012-12-12 19:54:05 +000074bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000075 const MCSectionData &SD = *F->getParent();
76 const MCFragment *LastValid = LastValidFragment.lookup(&SD);
77 if (!LastValid)
78 return false;
79 assert(LastValid->getParent() == F->getParent());
80 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
Daniel Dunbar9005d452010-05-14 00:37:21 +000081}
82
Eli Benderskyf43e3fd2012-12-10 20:13:43 +000083void MCAsmLayout::invalidateFragmentsAfter(MCFragment *F) {
Eli Benderskyd52a2c02012-12-12 19:54:05 +000084 // If this fragment wasn't already valid, we don't need to do anything.
85 if (!isFragmentValid(F))
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000086 return;
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000087
Rafael Espindola2bf6afc2010-12-15 08:45:53 +000088 // Otherwise, reset the last valid fragment to this fragment.
Rafael Espindola4f4363a2010-12-07 23:32:26 +000089 const MCSectionData &SD = *F->getParent();
Rafael Espindola2bf6afc2010-12-15 08:45:53 +000090 LastValidFragment[&SD] = F;
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000091}
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000092
Eli Benderskyd52a2c02012-12-12 19:54:05 +000093void MCAsmLayout::ensureValid(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000094 MCSectionData &SD = *F->getParent();
95
96 MCFragment *Cur = LastValidFragment[&SD];
97 if (!Cur)
98 Cur = &*SD.begin();
99 else
100 Cur = Cur->getNextNode();
101
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000102 // Advance the layout position until the fragment is valid.
103 while (!isFragmentValid(F)) {
104 assert(Cur && "Layout bookkeeping error");
105 const_cast<MCAsmLayout*>(this)->layoutFragment(Cur);
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000106 Cur = Cur->getNextNode();
Daniel Dunbar47b3ec42010-05-14 00:51:14 +0000107 }
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000108}
109
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000110uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000111 ensureValid(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000112 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
113 return F->Offset;
114}
115
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000116uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000117 const MCSymbol &S = SD->getSymbol();
118
119 // If this is a variable, then recursively evaluate now.
120 if (S.isVariable()) {
121 MCValue Target;
122 if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this))
123 report_fatal_error("unable to evaluate offset for variable '" +
124 S.getName() + "'");
125
126 // Verify that any used symbols are defined.
127 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
128 report_fatal_error("unable to evaluate offset to undefined symbol '" +
129 Target.getSymA()->getSymbol().getName() + "'");
130 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
131 report_fatal_error("unable to evaluate offset to undefined symbol '" +
132 Target.getSymB()->getSymbol().getName() + "'");
Jim Grosbach684457d2011-10-26 22:44:41 +0000133
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000134 uint64_t Offset = Target.getConstant();
135 if (Target.getSymA())
136 Offset += getSymbolOffset(&Assembler.getSymbolData(
137 Target.getSymA()->getSymbol()));
138 if (Target.getSymB())
139 Offset -= getSymbolOffset(&Assembler.getSymbolData(
140 Target.getSymB()->getSymbol()));
141 return Offset;
142 }
143
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000144 assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
145 return getFragmentOffset(SD->getFragment()) + SD->getOffset();
146}
147
Daniel Dunbar2661f112010-05-13 03:19:50 +0000148uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
Daniel Dunbarafc6acd2010-05-14 00:37:11 +0000149 // The size is the last fragment's end offset.
Daniel Dunbar2661f112010-05-13 03:19:50 +0000150 const MCFragment &F = SD->getFragmentList().back();
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000151 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000152}
153
154uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
Daniel Dunbar2661f112010-05-13 03:19:50 +0000155 // Virtual sections have no file size.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000156 if (SD->getSection().isVirtualSection())
Daniel Dunbar2661f112010-05-13 03:19:50 +0000157 return 0;
158
159 // Otherwise, the file size is the same as the address space size.
160 return getSectionAddressSize(SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000161}
162
Eli Bendersky4766ef42012-12-20 19:05:53 +0000163uint64_t MCAsmLayout::computeBundlePadding(const MCFragment *F,
164 uint64_t FOffset, uint64_t FSize) {
165 uint64_t BundleSize = Assembler.getBundleAlignSize();
166 assert(BundleSize > 0 &&
167 "computeBundlePadding should only be called if bundling is enabled");
168 uint64_t BundleMask = BundleSize - 1;
169 uint64_t OffsetInBundle = FOffset & BundleMask;
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000170 uint64_t EndOfFragment = OffsetInBundle + FSize;
Eli Bendersky4766ef42012-12-20 19:05:53 +0000171
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000172 // There are two kinds of bundling restrictions:
173 //
174 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
175 // *end* on a bundle boundary.
176 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
177 // would, add padding until the end of the bundle so that the fragment
178 // will start in a new one.
179 if (F->alignToBundleEnd()) {
180 // Three possibilities here:
181 //
182 // A) The fragment just happens to end at a bundle boundary, so we're good.
183 // B) The fragment ends before the current bundle boundary: pad it just
184 // enough to reach the boundary.
185 // C) The fragment ends after the current bundle boundary: pad it until it
186 // reaches the end of the next bundle boundary.
187 //
188 // Note: this code could be made shorter with some modulo trickery, but it's
189 // intentionally kept in its more explicit form for simplicity.
190 if (EndOfFragment == BundleSize)
191 return 0;
192 else if (EndOfFragment < BundleSize)
193 return BundleSize - EndOfFragment;
194 else { // EndOfFragment > BundleSize
195 return 2 * BundleSize - EndOfFragment;
196 }
197 } else if (EndOfFragment > BundleSize)
Eli Bendersky4766ef42012-12-20 19:05:53 +0000198 return BundleSize - OffsetInBundle;
199 else
200 return 0;
201}
202
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000203/* *** */
204
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000205MCFragment::MCFragment() : Kind(FragmentType(~0)) {
206}
207
Daniel Dunbar36880e72010-07-28 20:28:45 +0000208MCFragment::~MCFragment() {
209}
210
Daniel Dunbar5e835962009-08-26 02:48:04 +0000211MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Rafael Espindola2bf6afc2010-12-15 08:45:53 +0000212 : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000213{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000214 if (Parent)
215 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000216}
217
218/* *** */
219
Eli Bendersky64d9a322012-12-07 19:13:57 +0000220MCEncodedFragment::~MCEncodedFragment() {
221}
222
223/* *** */
224
Daniel Dunbar81e40002009-08-27 00:38:04 +0000225MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000226
227MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000228 : Section(&_Section),
Rafael Espindolaf8803fe2010-12-06 03:48:09 +0000229 Ordinal(~UINT32_C(0)),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000230 Alignment(1),
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000231 BundleLockState(NotBundleLocked), BundleGroupBeforeFirstInst(false),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000232 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000233{
234 if (A)
235 A->getSectionList().push_back(this);
236}
237
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000238/* *** */
239
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000240MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000241
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000242MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000243 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000244 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000245 IsExternal(false), IsPrivateExtern(false),
Matt Fleming6c8b3d22010-08-16 18:34:31 +0000246 CommonSize(0), SymbolSize(0), CommonAlign(0),
247 Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000248{
249 if (A)
250 A->getSymbolList().push_back(this);
251}
252
253/* *** */
254
Evan Cheng78c10ee2011-07-25 23:24:55 +0000255MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000256 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
257 raw_ostream &OS_)
258 : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
Eli Benderskybc873612012-12-20 22:51:52 +0000259 OS(OS_), BundleAlignSize(0), RelaxAll(false), NoExecStack(false),
260 SubsectionsViaSymbols(false) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000261}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000262
263MCAssembler::~MCAssembler() {
264}
265
Pedro Artigas5399d252012-12-12 22:59:46 +0000266void MCAssembler::reset() {
267 Sections.clear();
268 Symbols.clear();
269 SectionMap.clear();
270 SymbolMap.clear();
271 IndirectSymbols.clear();
272 DataRegions.clear();
273 ThumbFuncs.clear();
274 RelaxAll = false;
275 NoExecStack = false;
276 SubsectionsViaSymbols = false;
Pedro Artigas99cbdde2012-12-14 18:52:11 +0000277
278 // reset objects owned by us
279 getBackend().reset();
280 getEmitter().reset();
281 getWriter().reset();
Pedro Artigas5399d252012-12-12 22:59:46 +0000282}
283
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000284bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
Daniel Dunbar23869852010-03-19 03:18:09 +0000285 // Non-temporary labels should always be visible to the linker.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000286 if (!Symbol.isTemporary())
Daniel Dunbar23869852010-03-19 03:18:09 +0000287 return true;
288
289 // Absolute temporary labels are never visible.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000290 if (!Symbol.isInSection())
Daniel Dunbar23869852010-03-19 03:18:09 +0000291 return false;
292
293 // Otherwise, check if the section requires symbols even for temporary labels.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000294 return getBackend().doesSectionRequireSymbols(Symbol.getSection());
Daniel Dunbar23869852010-03-19 03:18:09 +0000295}
296
Rafael Espindolab8141102010-09-27 18:13:03 +0000297const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000298 // Linker visible symbols define atoms.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000299 if (isSymbolLinkerVisible(SD->getSymbol()))
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000300 return SD;
301
302 // Absolute and undefined symbols have no defining atom.
303 if (!SD->getFragment())
304 return 0;
305
Daniel Dunbara5f1d572010-05-12 00:38:17 +0000306 // Non-linker visible symbols in sections which can't be atomized have no
307 // defining atom.
308 if (!getBackend().isSectionAtomizable(
309 SD->getFragment()->getParent()->getSection()))
310 return 0;
311
Daniel Dunbar651804c2010-05-11 17:22:50 +0000312 // Otherwise, return the atom for the containing fragment.
313 return SD->getFragment()->getAtom();
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000314}
315
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000316bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000317 const MCFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000318 MCValue &Target, uint64_t &Value) const {
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000319 ++stats::evaluateFixup;
Daniel Dunbarff547842010-03-23 23:47:14 +0000320
Rafael Espindola33a38a12010-12-22 16:11:57 +0000321 if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout))
Jim Grosbachf3c93672012-01-27 00:51:23 +0000322 getContext().FatalError(Fixup.getLoc(), "expected relocatable expression");
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000323
Rafael Espindolafea753b2010-12-24 21:22:02 +0000324 bool IsPCRel = Backend.getFixupKindInfo(
325 Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
326
327 bool IsResolved;
328 if (IsPCRel) {
329 if (Target.getSymB()) {
330 IsResolved = false;
331 } else if (!Target.getSymA()) {
332 IsResolved = false;
333 } else {
Rafael Espindola908159b2011-02-16 03:25:55 +0000334 const MCSymbolRefExpr *A = Target.getSymA();
335 const MCSymbol &SA = A->getSymbol();
336 if (A->getKind() != MCSymbolRefExpr::VK_None ||
337 SA.AliasedSymbol().isUndefined()) {
Rafael Espindolafea753b2010-12-24 21:22:02 +0000338 IsResolved = false;
339 } else {
340 const MCSymbolData &DataA = getSymbolData(SA);
341 IsResolved =
342 getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
343 *DF, false, true);
344 }
345 }
346 } else {
347 IsResolved = Target.isAbsolute();
348 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000349
350 Value = Target.getConstant();
351
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000352 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000353 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
354 if (Sym.isDefined())
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000355 Value += Layout.getSymbolOffset(&getSymbolData(Sym));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000356 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000357 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000358 const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
359 if (Sym.isDefined())
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000360 Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000361 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000362
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000363
Daniel Dunbar2761fc42010-12-16 03:20:06 +0000364 bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
Owen Anderson47dbd422010-12-15 18:48:27 +0000365 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
366 assert((ShouldAlignPC ? IsPCRel : true) &&
367 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
368
Owen Anderson05018c22010-12-09 20:27:52 +0000369 if (IsPCRel) {
Owen Anderson175fb362010-12-17 21:49:48 +0000370 uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
Jim Grosbach684457d2011-10-26 22:44:41 +0000371
Owen Anderson47dbd422010-12-15 18:48:27 +0000372 // A number of ARM fixups in Thumb mode require that the effective PC
373 // address be determined as the 32-bit aligned version of the actual offset.
Owen Andersond18e0112010-12-15 19:24:24 +0000374 if (ShouldAlignPC) Offset &= ~0x3;
Owen Anderson175fb362010-12-17 21:49:48 +0000375 Value -= Offset;
Owen Anderson05018c22010-12-09 20:27:52 +0000376 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000377
Jim Grosbach7b25ecf2012-02-27 21:36:23 +0000378 // Let the backend adjust the fixup value if necessary, including whether
379 // we need a relocation.
380 Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value,
381 IsResolved);
Jim Grosbach47500202010-12-14 18:46:57 +0000382
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000383 return IsResolved;
384}
385
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000386uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
Rafael Espindola7a459032010-12-21 20:35:18 +0000387 const MCFragment &F) const {
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000388 switch (F.getKind()) {
389 case MCFragment::FT_Data:
Eli Bendersky0fdcef62013-01-08 22:05:10 +0000390 case MCFragment::FT_Relaxable:
391 return cast<MCEncodedFragment>(F).getContents().size();
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000392 case MCFragment::FT_Fill:
393 return cast<MCFillFragment>(F).getSize();
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000394
Rafael Espindola3ff57092010-11-02 17:22:24 +0000395 case MCFragment::FT_LEB:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000396 return cast<MCLEBFragment>(F).getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000397
Rafael Espindola7a459032010-12-21 20:35:18 +0000398 case MCFragment::FT_Align: {
399 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
400 unsigned Offset = Layout.getFragmentOffset(&AF);
401 unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
Owen Anderson15b7a982012-08-29 22:18:56 +0000402 // If we are padding with nops, force the padding to be larger than the
403 // minimum nop size.
404 if (Size > 0 && AF.hasEmitNops()) {
405 while (Size % getBackend().getMinimumNopSize())
406 Size += AF.getAlignment();
407 }
Rafael Espindola7a459032010-12-21 20:35:18 +0000408 if (Size > AF.getMaxBytesToEmit())
409 return 0;
410 return Size;
411 }
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000412
Rafael Espindola7a459032010-12-21 20:35:18 +0000413 case MCFragment::FT_Org: {
414 MCOrgFragment &OF = cast<MCOrgFragment>(F);
415 int64_t TargetLocation;
416 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
417 report_fatal_error("expected assembly-time absolute expression");
418
419 // FIXME: We need a way to communicate this error.
420 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
421 int64_t Size = TargetLocation - FragmentOffset;
422 if (Size < 0 || Size >= 0x40000000)
423 report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
424 "' (at offset '" + Twine(FragmentOffset) + "')");
425 return Size;
426 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000427
Rafael Espindola187d8332010-11-07 02:07:12 +0000428 case MCFragment::FT_Dwarf:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000429 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
Rafael Espindola245a1e22010-12-28 05:39:27 +0000430 case MCFragment::FT_DwarfFrame:
431 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000432 }
433
Craig Topper85814382012-02-07 05:05:23 +0000434 llvm_unreachable("invalid fragment kind");
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000435}
436
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000437void MCAsmLayout::layoutFragment(MCFragment *F) {
Daniel Dunbar9005d452010-05-14 00:37:21 +0000438 MCFragment *Prev = F->getPrevNode();
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000439
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000440 // We should never try to recompute something which is valid.
441 assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
442 // We should never try to compute the fragment layout if its predecessor
443 // isn't valid.
444 assert((!Prev || isFragmentValid(Prev)) &&
445 "Attempt to compute fragment before its predecessor!");
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000446
447 ++stats::FragmentLayouts;
448
Daniel Dunbarb69fc042010-05-13 20:40:12 +0000449 // Compute fragment offset and size.
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000450 if (Prev)
Eli Bendersky4766ef42012-12-20 19:05:53 +0000451 F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
452 else
453 F->Offset = 0;
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000454 LastValidFragment[F->getParent()] = F;
Eli Bendersky4766ef42012-12-20 19:05:53 +0000455
456 // If bundling is enabled and this fragment has instructions in it, it has to
457 // obey the bundling restrictions. With padding, we'll have:
458 //
459 //
460 // BundlePadding
461 // |||
462 // -------------------------------------
463 // Prev |##########| F |
464 // -------------------------------------
465 // ^
466 // |
467 // F->Offset
468 //
469 // The fragment's offset will point to after the padding, and its computed
470 // size won't include the padding.
471 //
472 if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
473 assert(isa<MCEncodedFragment>(F) &&
474 "Only MCEncodedFragment implementations have instructions");
475 uint64_t FSize = Assembler.computeFragmentSize(*this, *F);
476
477 if (FSize > Assembler.getBundleAlignSize())
478 report_fatal_error("Fragment can't be larger than a bundle size");
479
480 uint64_t RequiredBundlePadding = computeBundlePadding(F, F->Offset, FSize);
481 if (RequiredBundlePadding > UINT8_MAX)
482 report_fatal_error("Padding cannot exceed 255 bytes");
483 F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
484 F->Offset += RequiredBundlePadding;
485 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000486}
487
Eli Bendersky64d9a322012-12-07 19:13:57 +0000488/// \brief Write the contents of a fragment to the given object writer. Expects
489/// a MCEncodedFragment.
490static void writeFragmentContents(const MCFragment &F, MCObjectWriter *OW) {
491 MCEncodedFragment &EF = cast<MCEncodedFragment>(F);
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000492 OW->WriteBytes(EF.getContents());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000493}
494
495/// \brief Write the fragment \p F to the output file.
496static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout,
497 const MCFragment &F) {
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000498 MCObjectWriter *OW = &Asm.getWriter();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000499
500 // Should NOP padding be written out before this fragment?
501 unsigned BundlePadding = F.getBundlePadding();
502 if (BundlePadding > 0) {
503 assert(Asm.isBundlingEnabled() &&
504 "Writing bundle padding with disabled bundling");
505 assert(F.hasInstructions() &&
506 "Writing bundle padding for a fragment without instructions");
507
508 if (!Asm.getBackend().writeNopData(BundlePadding, OW))
509 report_fatal_error("unable to write NOP sequence of " +
510 Twine(BundlePadding) + " bytes");
511 }
512
513 // This variable (and its dummy usage) is to participate in the assert at
514 // the end of the function.
Daniel Dunbar53b23382010-03-19 09:28:59 +0000515 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000516 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000517
Daniel Dunbarff547842010-03-23 23:47:14 +0000518 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000519
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000520 // FIXME: Embed in fragments instead?
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000521 uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000522 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000523 case MCFragment::FT_Align: {
Eli Bendersky6ac81f52012-12-10 18:59:39 +0000524 ++stats::EmittedAlignFragments;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000525 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000526 uint64_t Count = FragmentSize / AF.getValueSize();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000527
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000528 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
529
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000530 // FIXME: This error shouldn't actually occur (the front end should emit
531 // multiple .align directives to enforce the semantics it wants), but is
532 // severe enough that we want to report it. How to handle this?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000533 if (Count * AF.getValueSize() != FragmentSize)
Chris Lattner75361b62010-04-07 22:58:41 +0000534 report_fatal_error("undefined .align directive, value size '" +
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000535 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000536 "' is not a divisor of padding size '" +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000537 Twine(FragmentSize) + "'");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000538
Kevin Enderby6e720482010-02-23 18:26:34 +0000539 // See if we are aligning with nops, and if so do that first to try to fill
540 // the Count bytes. Then if that did not fill any bytes or there are any
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000541 // bytes left to fill use the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000542 // If we are aligning with nops, ask that target to emit the right data.
Daniel Dunbar1c154132010-05-12 22:56:23 +0000543 if (AF.hasEmitNops()) {
Jim Grosbachec343382012-01-18 18:52:16 +0000544 if (!Asm.getBackend().writeNopData(Count, OW))
Chris Lattner75361b62010-04-07 22:58:41 +0000545 report_fatal_error("unable to write nop sequence of " +
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000546 Twine(Count) + " bytes");
547 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000548 }
549
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000550 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000551 for (uint64_t i = 0; i != Count; ++i) {
552 switch (AF.getValueSize()) {
Craig Topper85814382012-02-07 05:05:23 +0000553 default: llvm_unreachable("Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000554 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
555 case 2: OW->Write16(uint16_t(AF.getValue())); break;
556 case 4: OW->Write32(uint32_t(AF.getValue())); break;
557 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000558 }
559 }
560 break;
561 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000562
Eli Bendersky64d9a322012-12-07 19:13:57 +0000563 case MCFragment::FT_Data:
Eli Bendersky8ddc5a12012-12-07 17:59:21 +0000564 ++stats::EmittedDataFragments;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000565 writeFragmentContents(F, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000566 break;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000567
Eli Bendersky251040b2013-01-08 00:22:56 +0000568 case MCFragment::FT_Relaxable:
Eli Bendersky6f6204f2013-01-08 17:41:59 +0000569 ++stats::EmittedRelaxableFragments;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000570 writeFragmentContents(F, OW);
571 break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000572
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000573 case MCFragment::FT_Fill: {
Eli Bendersky6ac81f52012-12-10 18:59:39 +0000574 ++stats::EmittedFillFragments;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000575 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000576
577 assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
578
Daniel Dunbar3153fec2010-05-12 22:51:32 +0000579 for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000580 switch (FF.getValueSize()) {
Craig Topper85814382012-02-07 05:05:23 +0000581 default: llvm_unreachable("Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000582 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
583 case 2: OW->Write16(uint16_t(FF.getValue())); break;
584 case 4: OW->Write32(uint32_t(FF.getValue())); break;
585 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000586 }
587 }
588 break;
589 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000590
Rafael Espindola3ff57092010-11-02 17:22:24 +0000591 case MCFragment::FT_LEB: {
592 MCLEBFragment &LF = cast<MCLEBFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000593 OW->WriteBytes(LF.getContents().str());
Rafael Espindola3ff57092010-11-02 17:22:24 +0000594 break;
595 }
596
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000597 case MCFragment::FT_Org: {
Eli Bendersky6ac81f52012-12-10 18:59:39 +0000598 ++stats::EmittedOrgFragments;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000599 MCOrgFragment &OF = cast<MCOrgFragment>(F);
600
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000601 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000602 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000603
604 break;
605 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000606
607 case MCFragment::FT_Dwarf: {
608 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000609 OW->WriteBytes(OF.getContents().str());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000610 break;
611 }
Rafael Espindola245a1e22010-12-28 05:39:27 +0000612 case MCFragment::FT_DwarfFrame: {
613 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
614 OW->WriteBytes(CF.getContents().str());
615 break;
616 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000617 }
618
Eli Bendersky4766ef42012-12-20 19:05:53 +0000619 assert(OW->getStream().tell() - Start == FragmentSize &&
620 "The stream should advance by fragment size");
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000621}
622
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000623void MCAssembler::writeSectionData(const MCSectionData *SD,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000624 const MCAsmLayout &Layout) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000625 // Ignore virtual sections.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000626 if (SD->getSection().isVirtualSection()) {
Daniel Dunbar054be922010-05-13 03:50:50 +0000627 assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000628
629 // Check that contents are only things legal inside a virtual section.
630 for (MCSectionData::const_iterator it = SD->begin(),
631 ie = SD->end(); it != ie; ++it) {
632 switch (it->getKind()) {
Craig Topper85814382012-02-07 05:05:23 +0000633 default: llvm_unreachable("Invalid fragment in virtual section!");
Daniel Dunbarc983b202010-08-18 18:22:37 +0000634 case MCFragment::FT_Data: {
635 // Check that we aren't trying to write a non-zero contents (or fixups)
636 // into a virtual section. This is to support clients which use standard
637 // directives to fill the contents of virtual sections.
638 MCDataFragment &DF = cast<MCDataFragment>(*it);
639 assert(DF.fixup_begin() == DF.fixup_end() &&
640 "Cannot have fixups in virtual section!");
641 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
642 assert(DF.getContents()[i] == 0 &&
643 "Invalid data value for virtual section!");
644 break;
645 }
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000646 case MCFragment::FT_Align:
Daniel Dunbarc983b202010-08-18 18:22:37 +0000647 // Check that we aren't trying to write a non-zero value into a virtual
648 // section.
649 assert((!cast<MCAlignFragment>(it)->getValueSize() ||
650 !cast<MCAlignFragment>(it)->getValue()) &&
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000651 "Invalid align in virtual section!");
652 break;
653 case MCFragment::FT_Fill:
654 assert(!cast<MCFillFragment>(it)->getValueSize() &&
655 "Invalid fill in virtual section!");
656 break;
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000657 }
658 }
659
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000660 return;
661 }
662
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000663 uint64_t Start = getWriter().getStream().tell();
Jim Grosbachb5762bf2012-09-18 23:05:18 +0000664 (void)Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000665
Eli Bendersky64d9a322012-12-07 19:13:57 +0000666 for (MCSectionData::const_iterator it = SD->begin(), ie = SD->end();
667 it != ie; ++it)
668 writeFragment(*this, Layout, *it);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000669
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000670 assert(getWriter().getStream().tell() - Start ==
671 Layout.getSectionAddressSize(SD));
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000672}
673
Rafael Espindola179821a2010-12-06 19:08:48 +0000674
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000675uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000676 MCFragment &F,
677 const MCFixup &Fixup) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000678 // Evaluate the fixup.
679 MCValue Target;
680 uint64_t FixedValue;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000681 if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000682 // The fixup was unresolved, we need a relocation. Inform the object
683 // writer of the relocation, and give it an opportunity to adjust the
684 // fixup value if need be.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000685 getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
Rafael Espindola179821a2010-12-06 19:08:48 +0000686 }
687 return FixedValue;
688 }
689
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000690void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000691 DEBUG_WITH_TYPE("mc-dump", {
692 llvm::errs() << "assembler backend - pre-layout\n--\n";
693 dump(); });
694
Daniel Dunbar61066db2010-05-13 02:34:14 +0000695 // Create the layout object.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000696 MCAsmLayout Layout(*this);
Daniel Dunbar61066db2010-05-13 02:34:14 +0000697
Daniel Dunbar337718e2010-05-14 00:37:14 +0000698 // Create dummy fragments and assign section ordinals.
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000699 unsigned SectionIndex = 0;
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000700 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
701 // Create dummy fragments to eliminate any empty sections, this simplifies
702 // layout.
Duncan Sands6f74f692010-06-29 13:30:08 +0000703 if (it->getFragmentList().empty())
Rafael Espindolad80781b2010-09-15 21:48:40 +0000704 new MCDataFragment(it);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000705
706 it->setOrdinal(SectionIndex++);
Daniel Dunbar337718e2010-05-14 00:37:14 +0000707 }
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000708
Daniel Dunbar337718e2010-05-14 00:37:14 +0000709 // Assign layout order indices to sections and fragments.
Daniel Dunbar337718e2010-05-14 00:37:14 +0000710 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
711 MCSectionData *SD = Layout.getSectionOrder()[i];
712 SD->setLayoutOrder(i);
713
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000714 unsigned FragmentIndex = 0;
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000715 for (MCSectionData::iterator iFrag = SD->begin(), iFragEnd = SD->end();
716 iFrag != iFragEnd; ++iFrag)
717 iFrag->setLayoutOrder(FragmentIndex++);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000718 }
719
Daniel Dunbar61066db2010-05-13 02:34:14 +0000720 // Layout until everything fits.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000721 while (layoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000722 continue;
723
724 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000725 llvm::errs() << "assembler backend - post-relaxation\n--\n";
726 dump(); });
727
728 // Finalize the layout, including fragment lowering.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000729 finishLayout(Layout);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000730
731 DEBUG_WITH_TYPE("mc-dump", {
732 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000733 dump(); });
734
Daniel Dunbarff547842010-03-23 23:47:14 +0000735 uint64_t StartOffset = OS.tell();
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000736
Daniel Dunbarbacba992010-03-19 07:09:33 +0000737 // Allow the object writer a chance to perform post-layout binding (for
738 // example, to set the index fields in the symbol data).
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000739 getWriter().ExecutePostLayoutBinding(*this, Layout);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000740
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000741 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000742 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
743 for (MCSectionData::iterator it2 = it->begin(),
744 ie2 = it->end(); it2 != ie2; ++it2) {
Eli Bendersky64d9a322012-12-07 19:13:57 +0000745 MCEncodedFragment *F = dyn_cast<MCEncodedFragment>(it2);
746 if (F) {
747 for (MCEncodedFragment::fixup_iterator it3 = F->fixup_begin(),
748 ie3 = F->fixup_end(); it3 != ie3; ++it3) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000749 MCFixup &Fixup = *it3;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000750 uint64_t FixedValue = handleFixup(Layout, *F, Fixup);
751 getBackend().applyFixup(Fixup, F->getContents().data(),
752 F->getContents().size(), FixedValue);
Rafael Espindola179821a2010-12-06 19:08:48 +0000753 }
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000754 }
755 }
756 }
757
Daniel Dunbarbacba992010-03-19 07:09:33 +0000758 // Write the object file.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000759 getWriter().WriteObject(*this, Layout);
Daniel Dunbarff547842010-03-23 23:47:14 +0000760
761 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000762}
763
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000764bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
Eli Bendersky251040b2013-01-08 00:22:56 +0000765 const MCRelaxableFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000766 const MCAsmLayout &Layout) const {
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000767 // If we cannot resolve the fixup value, it requires relaxation.
768 MCValue Target;
769 uint64_t Value;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000770 if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000771 return true;
772
Jim Grosbach370b78d2011-12-06 00:47:03 +0000773 return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000774}
775
Eli Bendersky251040b2013-01-08 00:22:56 +0000776bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000777 const MCAsmLayout &Layout) const {
778 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
779 // are intentionally pushing out inst fragments, or because we relaxed a
780 // previous instruction to one that doesn't need relaxation.
Eli Bendersky251040b2013-01-08 00:22:56 +0000781 if (!getBackend().mayNeedRelaxation(F->getInst()))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000782 return false;
783
Eli Bendersky251040b2013-01-08 00:22:56 +0000784 for (MCRelaxableFragment::const_fixup_iterator it = F->fixup_begin(),
785 ie = F->fixup_end(); it != ie; ++it)
786 if (fixupNeedsRelaxation(*it, F, Layout))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000787 return true;
788
789 return false;
790}
791
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000792bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
Eli Bendersky251040b2013-01-08 00:22:56 +0000793 MCRelaxableFragment &F) {
794 if (!fragmentNeedsRelaxation(&F, Layout))
Rafael Espindola3ff57092010-11-02 17:22:24 +0000795 return false;
796
797 ++stats::RelaxedInstructions;
798
799 // FIXME-PERF: We could immediately lower out instructions if we can tell
800 // they are fully resolved, to avoid retesting on later passes.
801
802 // Relax the fragment.
803
804 MCInst Relaxed;
Eli Bendersky251040b2013-01-08 00:22:56 +0000805 getBackend().relaxInstruction(F.getInst(), Relaxed);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000806
807 // Encode the new instruction.
808 //
809 // FIXME-PERF: If it matters, we could let the target do this. It can
810 // probably do so more efficiently in many cases.
811 SmallVector<MCFixup, 4> Fixups;
812 SmallString<256> Code;
813 raw_svector_ostream VecOS(Code);
814 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
815 VecOS.flush();
816
Eli Bendersky251040b2013-01-08 00:22:56 +0000817 // Update the fragment.
818 F.setInst(Relaxed);
819 F.getContents() = Code;
820 F.getFixups() = Fixups;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000821
Rafael Espindola3ff57092010-11-02 17:22:24 +0000822 return true;
823}
824
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000825bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000826 int64_t Value = 0;
827 uint64_t OldSize = LF.getContents().size();
Rafael Espindolad88cac02011-04-26 02:17:58 +0000828 bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout);
829 (void)IsAbs;
830 assert(IsAbs);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000831 SmallString<8> &Data = LF.getContents();
832 Data.clear();
833 raw_svector_ostream OSE(Data);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000834 if (LF.isSigned())
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000835 encodeSLEB128(Value, OSE);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000836 else
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000837 encodeULEB128(Value, OSE);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000838 OSE.flush();
839 return OldSize != LF.getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000840}
841
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000842bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000843 MCDwarfLineAddrFragment &DF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000844 int64_t AddrDelta = 0;
845 uint64_t OldSize = DF.getContents().size();
Rafael Espindola835439a2010-12-22 22:04:28 +0000846 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
847 (void)IsAbs;
848 assert(IsAbs);
Rafael Espindola187d8332010-11-07 02:07:12 +0000849 int64_t LineDelta;
850 LineDelta = DF.getLineDelta();
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000851 SmallString<8> &Data = DF.getContents();
852 Data.clear();
853 raw_svector_ostream OSE(Data);
854 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE);
855 OSE.flush();
856 return OldSize != Data.size();
Rafael Espindola187d8332010-11-07 02:07:12 +0000857}
858
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000859bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
Rafael Espindola245a1e22010-12-28 05:39:27 +0000860 MCDwarfCallFrameFragment &DF) {
861 int64_t AddrDelta = 0;
862 uint64_t OldSize = DF.getContents().size();
863 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
864 (void)IsAbs;
865 assert(IsAbs);
866 SmallString<8> &Data = DF.getContents();
867 Data.clear();
868 raw_svector_ostream OSE(Data);
Rafael Espindola4eafe102011-05-08 14:35:21 +0000869 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE);
Rafael Espindola245a1e22010-12-28 05:39:27 +0000870 OSE.flush();
871 return OldSize != Data.size();
872}
873
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000874bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD) {
875 // Holds the first fragment which needed relaxing during this layout. It will
876 // remain NULL if none were relaxed.
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000877 // When a fragment is relaxed, all the fragments following it should get
878 // invalidated because their offset is going to change.
879 MCFragment *FirstRelaxedFragment = NULL;
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000880
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000881 // Attempt to relax all the fragments in the section.
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000882 for (MCSectionData::iterator I = SD.begin(), IE = SD.end(); I != IE; ++I) {
883 // Check if this is a fragment that needs relaxation.
884 bool RelaxedFrag = false;
885 switch(I->getKind()) {
Rafael Espindola62b83b62010-12-21 04:22:09 +0000886 default:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000887 break;
Eli Bendersky251040b2013-01-08 00:22:56 +0000888 case MCFragment::FT_Relaxable:
Eli Bendersky37a98302012-12-11 17:16:00 +0000889 assert(!getRelaxAll() &&
Eli Bendersky251040b2013-01-08 00:22:56 +0000890 "Did not expect a MCRelaxableFragment in RelaxAll mode");
891 RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
Rafael Espindola62b83b62010-12-21 04:22:09 +0000892 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000893 case MCFragment::FT_Dwarf:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000894 RelaxedFrag = relaxDwarfLineAddr(Layout,
895 *cast<MCDwarfLineAddrFragment>(I));
Rafael Espindola62b83b62010-12-21 04:22:09 +0000896 break;
Rafael Espindola245a1e22010-12-28 05:39:27 +0000897 case MCFragment::FT_DwarfFrame:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000898 RelaxedFrag =
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000899 relaxDwarfCallFrameFragment(Layout,
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000900 *cast<MCDwarfCallFrameFragment>(I));
Rafael Espindola245a1e22010-12-28 05:39:27 +0000901 break;
902 case MCFragment::FT_LEB:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000903 RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
Rafael Espindola245a1e22010-12-28 05:39:27 +0000904 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000905 }
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000906 if (RelaxedFrag && !FirstRelaxedFragment)
907 FirstRelaxedFragment = I;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000908 }
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000909 if (FirstRelaxedFragment) {
910 Layout.invalidateFragmentsAfter(FirstRelaxedFragment);
Rafael Espindola62b83b62010-12-21 04:22:09 +0000911 return true;
912 }
913 return false;
914}
915
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000916bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000917 ++stats::RelaxationSteps;
918
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000919 bool WasRelaxed = false;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000920 for (iterator it = begin(), ie = end(); it != ie; ++it) {
921 MCSectionData &SD = *it;
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000922 while (layoutSectionOnce(Layout, SD))
Rafael Espindola62b83b62010-12-21 04:22:09 +0000923 WasRelaxed = true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000924 }
925
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000926 return WasRelaxed;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000927}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000928
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000929void MCAssembler::finishLayout(MCAsmLayout &Layout) {
Rafael Espindolab4172fa2010-12-04 22:47:22 +0000930 // The layout is done. Mark every fragment as valid.
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000931 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
932 Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
933 }
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000934}
935
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000936// Debugging methods
937
938namespace llvm {
939
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000940raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
941 OS << "<MCFixup" << " Offset:" << AF.getOffset()
Daniel Dunbar482ad802010-05-26 15:18:31 +0000942 << " Value:" << *AF.getValue()
943 << " Kind:" << AF.getKind() << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000944 return OS;
945}
946
947}
948
Manman Ren286c4dc2012-09-12 05:06:18 +0000949#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000950void MCFragment::dump() {
951 raw_ostream &OS = llvm::errs();
952
Daniel Dunbare614e392010-05-26 06:50:57 +0000953 OS << "<";
954 switch (getKind()) {
955 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
956 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
957 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
Eli Bendersky251040b2013-01-08 00:22:56 +0000958 case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
Daniel Dunbare614e392010-05-26 06:50:57 +0000959 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000960 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
Rafael Espindola245a1e22010-12-28 05:39:27 +0000961 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000962 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
Daniel Dunbare614e392010-05-26 06:50:57 +0000963 }
964
Daniel Dunbar337718e2010-05-14 00:37:14 +0000965 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
Eli Bendersky4766ef42012-12-20 19:05:53 +0000966 << " Offset:" << Offset
967 << " HasInstructions:" << hasInstructions()
968 << " BundlePadding:" << getBundlePadding() << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000969
Daniel Dunbare614e392010-05-26 06:50:57 +0000970 switch (getKind()) {
971 case MCFragment::FT_Align: {
972 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
973 if (AF->hasEmitNops())
974 OS << " (emit nops)";
Daniel Dunbare614e392010-05-26 06:50:57 +0000975 OS << "\n ";
976 OS << " Alignment:" << AF->getAlignment()
977 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
978 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
979 break;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000980 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000981 case MCFragment::FT_Data: {
982 const MCDataFragment *DF = cast<MCDataFragment>(this);
983 OS << "\n ";
984 OS << " Contents:[";
985 const SmallVectorImpl<char> &Contents = DF->getContents();
986 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
987 if (i) OS << ",";
988 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000989 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000990 OS << "] (" << Contents.size() << " bytes)";
991
Eli Bendersky5c10f502012-12-05 22:11:02 +0000992 if (DF->fixup_begin() != DF->fixup_end()) {
Daniel Dunbare614e392010-05-26 06:50:57 +0000993 OS << ",\n ";
994 OS << " Fixups:[";
995 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
996 ie = DF->fixup_end(); it != ie; ++it) {
997 if (it != DF->fixup_begin()) OS << ",\n ";
998 OS << *it;
999 }
1000 OS << "]";
1001 }
1002 break;
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001003 }
Daniel Dunbare614e392010-05-26 06:50:57 +00001004 case MCFragment::FT_Fill: {
1005 const MCFillFragment *FF = cast<MCFillFragment>(this);
1006 OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
1007 << " Size:" << FF->getSize();
1008 break;
1009 }
Eli Bendersky251040b2013-01-08 00:22:56 +00001010 case MCFragment::FT_Relaxable: {
1011 const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
Daniel Dunbare614e392010-05-26 06:50:57 +00001012 OS << "\n ";
1013 OS << " Inst:";
Eli Bendersky251040b2013-01-08 00:22:56 +00001014 F->getInst().dump_pretty(OS);
Daniel Dunbare614e392010-05-26 06:50:57 +00001015 break;
1016 }
1017 case MCFragment::FT_Org: {
1018 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
1019 OS << "\n ";
1020 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
1021 break;
1022 }
Kevin Enderbyc0957932010-09-30 16:52:03 +00001023 case MCFragment::FT_Dwarf: {
1024 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
1025 OS << "\n ";
1026 OS << " AddrDelta:" << OF->getAddrDelta()
1027 << " LineDelta:" << OF->getLineDelta();
1028 break;
1029 }
Rafael Espindola245a1e22010-12-28 05:39:27 +00001030 case MCFragment::FT_DwarfFrame: {
1031 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
1032 OS << "\n ";
1033 OS << " AddrDelta:" << CF->getAddrDelta();
1034 break;
1035 }
Rafael Espindola3ff57092010-11-02 17:22:24 +00001036 case MCFragment::FT_LEB: {
1037 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
1038 OS << "\n ";
1039 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
1040 break;
1041 }
Daniel Dunbare614e392010-05-26 06:50:57 +00001042 }
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001043 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001044}
1045
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001046void MCSectionData::dump() {
1047 raw_ostream &OS = llvm::errs();
1048
1049 OS << "<MCSectionData";
Eli Bendersky4766ef42012-12-20 19:05:53 +00001050 OS << " Alignment:" << getAlignment()
1051 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001052 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1053 if (it != begin()) OS << ",\n ";
1054 it->dump();
1055 }
1056 OS << "]>";
1057}
1058
1059void MCSymbolData::dump() {
1060 raw_ostream &OS = llvm::errs();
1061
1062 OS << "<MCSymbolData Symbol:" << getSymbol()
1063 << " Fragment:" << getFragment() << " Offset:" << getOffset()
1064 << " Flags:" << getFlags() << " Index:" << getIndex();
1065 if (isCommon())
1066 OS << " (common, size:" << getCommonSize()
1067 << " align: " << getCommonAlignment() << ")";
1068 if (isExternal())
1069 OS << " (external)";
1070 if (isPrivateExtern())
1071 OS << " (private extern)";
1072 OS << ">";
1073}
1074
1075void MCAssembler::dump() {
1076 raw_ostream &OS = llvm::errs();
1077
1078 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001079 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001080 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1081 if (it != begin()) OS << ",\n ";
1082 it->dump();
1083 }
1084 OS << "],\n";
1085 OS << " Symbols:[";
1086
1087 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001088 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001089 it->dump();
1090 }
1091 OS << "]>\n";
1092}
Manman Rencc77eec2012-09-06 19:55:56 +00001093#endif
David Blaikie2d24e2a2011-12-20 02:50:00 +00001094
1095// anchors for MC*Fragment vtables
Eli Bendersky64d9a322012-12-07 19:13:57 +00001096void MCEncodedFragment::anchor() { }
David Blaikie2d24e2a2011-12-20 02:50:00 +00001097void MCDataFragment::anchor() { }
Eli Bendersky251040b2013-01-08 00:22:56 +00001098void MCRelaxableFragment::anchor() { }
David Blaikie2d24e2a2011-12-20 02:50:00 +00001099void MCAlignFragment::anchor() { }
1100void MCFillFragment::anchor() { }
1101void MCOrgFragment::anchor() { }
1102void MCLEBFragment::anchor() { }
1103void MCDwarfLineAddrFragment::anchor() { }
1104void MCDwarfCallFrameFragment::anchor() { }