blob: fb5ab28bcf506939fe2e0e06d93acb6bf12a4313 [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");
Eli Bendersky9ccb7692013-01-15 23:22:09 +000041STATISTIC(EmittedCompactEncodedInstFragments,
42 "Number of emitted assembler fragments - compact encoded inst");
Eli Bendersky6ac81f52012-12-10 18:59:39 +000043STATISTIC(EmittedAlignFragments,
44 "Number of emitted assembler fragments - align");
45STATISTIC(EmittedFillFragments,
46 "Number of emitted assembler fragments - fill");
47STATISTIC(EmittedOrgFragments,
48 "Number of emitted assembler fragments - org");
Jim Grosbachf77d5b12011-12-06 00:03:48 +000049STATISTIC(evaluateFixup, "Number of evaluated fixups");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000050STATISTIC(FragmentLayouts, "Number of fragment layouts");
Daniel Dunbarff547842010-03-23 23:47:14 +000051STATISTIC(ObjectBytes, "Number of emitted object file bytes");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000052STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
53STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
Daniel Dunbarff547842010-03-23 23:47:14 +000054}
55}
Daniel Dunbar0adcd352009-08-25 21:10:45 +000056
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000057// FIXME FIXME FIXME: There are number of places in this file where we convert
58// what is a 64-bit assembler value used for computation into a value in the
59// object file, which may truncate it. We should detect that truncation where
60// invalid and report errors back.
61
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000062/* *** */
63
Daniel Dunbar9005d452010-05-14 00:37:21 +000064MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
Rafael Espindola4f4363a2010-12-07 23:32:26 +000065 : Assembler(Asm), LastValidFragment()
Daniel Dunbar9005d452010-05-14 00:37:21 +000066 {
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000067 // Compute the section layout order. Virtual sections must go last.
68 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000069 if (!it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000070 SectionOrder.push_back(&*it);
71 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000072 if (it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000073 SectionOrder.push_back(&*it);
74}
75
Eli Benderskyd52a2c02012-12-12 19:54:05 +000076bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000077 const MCSectionData &SD = *F->getParent();
78 const MCFragment *LastValid = LastValidFragment.lookup(&SD);
79 if (!LastValid)
80 return false;
81 assert(LastValid->getParent() == F->getParent());
82 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
Daniel Dunbar9005d452010-05-14 00:37:21 +000083}
84
Derek Schufff918d7f2013-02-05 17:55:27 +000085void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) {
Eli Benderskyd52a2c02012-12-12 19:54:05 +000086 // If this fragment wasn't already valid, we don't need to do anything.
87 if (!isFragmentValid(F))
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000088 return;
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000089
Derek Schufff918d7f2013-02-05 17:55:27 +000090 // Otherwise, reset the last valid fragment to the previous fragment
91 // (if this is the first fragment, it will be NULL).
Rafael Espindola4f4363a2010-12-07 23:32:26 +000092 const MCSectionData &SD = *F->getParent();
Derek Schufff918d7f2013-02-05 17:55:27 +000093 LastValidFragment[&SD] = F->getPrevNode();
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000094}
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000095
Eli Benderskyd52a2c02012-12-12 19:54:05 +000096void MCAsmLayout::ensureValid(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000097 MCSectionData &SD = *F->getParent();
98
99 MCFragment *Cur = LastValidFragment[&SD];
100 if (!Cur)
101 Cur = &*SD.begin();
102 else
103 Cur = Cur->getNextNode();
104
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000105 // Advance the layout position until the fragment is valid.
106 while (!isFragmentValid(F)) {
107 assert(Cur && "Layout bookkeeping error");
108 const_cast<MCAsmLayout*>(this)->layoutFragment(Cur);
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000109 Cur = Cur->getNextNode();
Daniel Dunbar47b3ec42010-05-14 00:51:14 +0000110 }
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000111}
112
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000113uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000114 ensureValid(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000115 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
116 return F->Offset;
117}
118
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000119uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000120 const MCSymbol &S = SD->getSymbol();
121
122 // If this is a variable, then recursively evaluate now.
123 if (S.isVariable()) {
124 MCValue Target;
125 if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this))
126 report_fatal_error("unable to evaluate offset for variable '" +
127 S.getName() + "'");
128
129 // Verify that any used symbols are defined.
130 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
131 report_fatal_error("unable to evaluate offset to undefined symbol '" +
132 Target.getSymA()->getSymbol().getName() + "'");
133 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
134 report_fatal_error("unable to evaluate offset to undefined symbol '" +
135 Target.getSymB()->getSymbol().getName() + "'");
Jim Grosbach684457d2011-10-26 22:44:41 +0000136
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000137 uint64_t Offset = Target.getConstant();
138 if (Target.getSymA())
139 Offset += getSymbolOffset(&Assembler.getSymbolData(
140 Target.getSymA()->getSymbol()));
141 if (Target.getSymB())
142 Offset -= getSymbolOffset(&Assembler.getSymbolData(
143 Target.getSymB()->getSymbol()));
144 return Offset;
145 }
146
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000147 assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
148 return getFragmentOffset(SD->getFragment()) + SD->getOffset();
149}
150
Daniel Dunbar2661f112010-05-13 03:19:50 +0000151uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
Daniel Dunbarafc6acd2010-05-14 00:37:11 +0000152 // The size is the last fragment's end offset.
Daniel Dunbar2661f112010-05-13 03:19:50 +0000153 const MCFragment &F = SD->getFragmentList().back();
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000154 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000155}
156
157uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
Daniel Dunbar2661f112010-05-13 03:19:50 +0000158 // Virtual sections have no file size.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000159 if (SD->getSection().isVirtualSection())
Daniel Dunbar2661f112010-05-13 03:19:50 +0000160 return 0;
161
162 // Otherwise, the file size is the same as the address space size.
163 return getSectionAddressSize(SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000164}
165
Eli Bendersky4766ef42012-12-20 19:05:53 +0000166uint64_t MCAsmLayout::computeBundlePadding(const MCFragment *F,
167 uint64_t FOffset, uint64_t FSize) {
168 uint64_t BundleSize = Assembler.getBundleAlignSize();
Derek Schufff918d7f2013-02-05 17:55:27 +0000169 assert(BundleSize > 0 &&
Eli Bendersky4766ef42012-12-20 19:05:53 +0000170 "computeBundlePadding should only be called if bundling is enabled");
171 uint64_t BundleMask = BundleSize - 1;
172 uint64_t OffsetInBundle = FOffset & BundleMask;
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000173 uint64_t EndOfFragment = OffsetInBundle + FSize;
Eli Bendersky4766ef42012-12-20 19:05:53 +0000174
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000175 // There are two kinds of bundling restrictions:
Derek Schufff918d7f2013-02-05 17:55:27 +0000176 //
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000177 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
178 // *end* on a bundle boundary.
179 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
180 // would, add padding until the end of the bundle so that the fragment
181 // will start in a new one.
182 if (F->alignToBundleEnd()) {
183 // Three possibilities here:
184 //
185 // A) The fragment just happens to end at a bundle boundary, so we're good.
186 // B) The fragment ends before the current bundle boundary: pad it just
187 // enough to reach the boundary.
188 // C) The fragment ends after the current bundle boundary: pad it until it
189 // reaches the end of the next bundle boundary.
190 //
191 // Note: this code could be made shorter with some modulo trickery, but it's
192 // intentionally kept in its more explicit form for simplicity.
193 if (EndOfFragment == BundleSize)
194 return 0;
195 else if (EndOfFragment < BundleSize)
196 return BundleSize - EndOfFragment;
197 else { // EndOfFragment > BundleSize
198 return 2 * BundleSize - EndOfFragment;
199 }
200 } else if (EndOfFragment > BundleSize)
Eli Bendersky4766ef42012-12-20 19:05:53 +0000201 return BundleSize - OffsetInBundle;
202 else
203 return 0;
204}
205
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000206/* *** */
207
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000208MCFragment::MCFragment() : Kind(FragmentType(~0)) {
209}
210
Daniel Dunbar36880e72010-07-28 20:28:45 +0000211MCFragment::~MCFragment() {
212}
213
Daniel Dunbar5e835962009-08-26 02:48:04 +0000214MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Rafael Espindola2bf6afc2010-12-15 08:45:53 +0000215 : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000216{
Peter Collingbournedf39be62013-04-17 21:18:16 +0000217 if (Parent)
218 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000219}
220
221/* *** */
222
Eli Bendersky64d9a322012-12-07 19:13:57 +0000223MCEncodedFragment::~MCEncodedFragment() {
224}
225
226/* *** */
227
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000228MCEncodedFragmentWithFixups::~MCEncodedFragmentWithFixups() {
229}
230
231/* *** */
232
Daniel Dunbar81e40002009-08-27 00:38:04 +0000233MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000234
235MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000236 : Section(&_Section),
Rafael Espindolaf8803fe2010-12-06 03:48:09 +0000237 Ordinal(~UINT32_C(0)),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000238 Alignment(1),
Eli Bendersky6c1d4972013-01-07 21:51:08 +0000239 BundleLockState(NotBundleLocked), BundleGroupBeforeFirstInst(false),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000240 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000241{
242 if (A)
243 A->getSectionList().push_back(this);
244}
245
Peter Collingbournedf39be62013-04-17 21:18:16 +0000246MCSectionData::iterator
247MCSectionData::getSubsectionInsertionPoint(unsigned Subsection) {
248 if (Subsection == 0 && SubsectionFragmentMap.empty())
249 return end();
250
251 SmallVectorImpl<std::pair<unsigned, MCFragment *> >::iterator MI =
252 std::lower_bound(SubsectionFragmentMap.begin(), SubsectionFragmentMap.end(),
253 std::make_pair(Subsection, (MCFragment *)0));
254 bool ExactMatch = false;
255 if (MI != SubsectionFragmentMap.end()) {
256 ExactMatch = MI->first == Subsection;
257 if (ExactMatch)
258 ++MI;
259 }
260 iterator IP;
261 if (MI == SubsectionFragmentMap.end())
262 IP = end();
263 else
264 IP = MI->second;
265 if (!ExactMatch && Subsection != 0) {
266 // The GNU as documentation claims that subsections have an alignment of 4,
267 // although this appears not to be the case.
268 MCFragment *F = new MCDataFragment();
269 SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
270 getFragmentList().insert(IP, F);
271 F->setParent(this);
272 }
273 return IP;
274}
275
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000276/* *** */
277
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000278MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000279
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000280MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000281 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000282 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000283 IsExternal(false), IsPrivateExtern(false),
Matt Fleming6c8b3d22010-08-16 18:34:31 +0000284 CommonSize(0), SymbolSize(0), CommonAlign(0),
285 Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000286{
287 if (A)
288 A->getSymbolList().push_back(this);
289}
290
291/* *** */
292
Evan Cheng78c10ee2011-07-25 23:24:55 +0000293MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000294 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
295 raw_ostream &OS_)
296 : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
Eli Benderskybc873612012-12-20 22:51:52 +0000297 OS(OS_), BundleAlignSize(0), RelaxAll(false), NoExecStack(false),
Jack Carter9a7bf432013-01-30 02:09:52 +0000298 SubsectionsViaSymbols(false), ELFHeaderEFlags(0) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000299}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000300
301MCAssembler::~MCAssembler() {
302}
303
Pedro Artigas5399d252012-12-12 22:59:46 +0000304void MCAssembler::reset() {
305 Sections.clear();
306 Symbols.clear();
307 SectionMap.clear();
308 SymbolMap.clear();
309 IndirectSymbols.clear();
310 DataRegions.clear();
311 ThumbFuncs.clear();
312 RelaxAll = false;
313 NoExecStack = false;
314 SubsectionsViaSymbols = false;
Jack Carter9a7bf432013-01-30 02:09:52 +0000315 ELFHeaderEFlags = 0;
Pedro Artigas99cbdde2012-12-14 18:52:11 +0000316
317 // reset objects owned by us
318 getBackend().reset();
319 getEmitter().reset();
320 getWriter().reset();
Pedro Artigas5399d252012-12-12 22:59:46 +0000321}
322
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000323bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
Daniel Dunbar23869852010-03-19 03:18:09 +0000324 // Non-temporary labels should always be visible to the linker.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000325 if (!Symbol.isTemporary())
Daniel Dunbar23869852010-03-19 03:18:09 +0000326 return true;
327
328 // Absolute temporary labels are never visible.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000329 if (!Symbol.isInSection())
Daniel Dunbar23869852010-03-19 03:18:09 +0000330 return false;
331
332 // Otherwise, check if the section requires symbols even for temporary labels.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000333 return getBackend().doesSectionRequireSymbols(Symbol.getSection());
Daniel Dunbar23869852010-03-19 03:18:09 +0000334}
335
Rafael Espindolab8141102010-09-27 18:13:03 +0000336const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000337 // Linker visible symbols define atoms.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000338 if (isSymbolLinkerVisible(SD->getSymbol()))
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000339 return SD;
340
341 // Absolute and undefined symbols have no defining atom.
342 if (!SD->getFragment())
343 return 0;
344
Daniel Dunbara5f1d572010-05-12 00:38:17 +0000345 // Non-linker visible symbols in sections which can't be atomized have no
346 // defining atom.
347 if (!getBackend().isSectionAtomizable(
348 SD->getFragment()->getParent()->getSection()))
349 return 0;
350
Daniel Dunbar651804c2010-05-11 17:22:50 +0000351 // Otherwise, return the atom for the containing fragment.
352 return SD->getFragment()->getAtom();
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000353}
354
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000355bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000356 const MCFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000357 MCValue &Target, uint64_t &Value) const {
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000358 ++stats::evaluateFixup;
Daniel Dunbarff547842010-03-23 23:47:14 +0000359
Rafael Espindola33a38a12010-12-22 16:11:57 +0000360 if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout))
Jim Grosbachf3c93672012-01-27 00:51:23 +0000361 getContext().FatalError(Fixup.getLoc(), "expected relocatable expression");
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000362
Rafael Espindolafea753b2010-12-24 21:22:02 +0000363 bool IsPCRel = Backend.getFixupKindInfo(
364 Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
365
366 bool IsResolved;
367 if (IsPCRel) {
368 if (Target.getSymB()) {
369 IsResolved = false;
370 } else if (!Target.getSymA()) {
371 IsResolved = false;
372 } else {
Rafael Espindola908159b2011-02-16 03:25:55 +0000373 const MCSymbolRefExpr *A = Target.getSymA();
374 const MCSymbol &SA = A->getSymbol();
375 if (A->getKind() != MCSymbolRefExpr::VK_None ||
376 SA.AliasedSymbol().isUndefined()) {
Rafael Espindolafea753b2010-12-24 21:22:02 +0000377 IsResolved = false;
378 } else {
379 const MCSymbolData &DataA = getSymbolData(SA);
380 IsResolved =
381 getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
382 *DF, false, true);
383 }
384 }
385 } else {
386 IsResolved = Target.isAbsolute();
387 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000388
389 Value = Target.getConstant();
390
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000391 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000392 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
393 if (Sym.isDefined())
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000394 Value += Layout.getSymbolOffset(&getSymbolData(Sym));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000395 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000396 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000397 const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
398 if (Sym.isDefined())
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000399 Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000400 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000401
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000402
Daniel Dunbar2761fc42010-12-16 03:20:06 +0000403 bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
Owen Anderson47dbd422010-12-15 18:48:27 +0000404 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
405 assert((ShouldAlignPC ? IsPCRel : true) &&
406 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
407
Owen Anderson05018c22010-12-09 20:27:52 +0000408 if (IsPCRel) {
Owen Anderson175fb362010-12-17 21:49:48 +0000409 uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
Jim Grosbach684457d2011-10-26 22:44:41 +0000410
Owen Anderson47dbd422010-12-15 18:48:27 +0000411 // A number of ARM fixups in Thumb mode require that the effective PC
412 // address be determined as the 32-bit aligned version of the actual offset.
Owen Andersond18e0112010-12-15 19:24:24 +0000413 if (ShouldAlignPC) Offset &= ~0x3;
Owen Anderson175fb362010-12-17 21:49:48 +0000414 Value -= Offset;
Owen Anderson05018c22010-12-09 20:27:52 +0000415 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000416
Jim Grosbach7b25ecf2012-02-27 21:36:23 +0000417 // Let the backend adjust the fixup value if necessary, including whether
418 // we need a relocation.
419 Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value,
420 IsResolved);
Jim Grosbach47500202010-12-14 18:46:57 +0000421
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000422 return IsResolved;
423}
424
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000425uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
Rafael Espindola7a459032010-12-21 20:35:18 +0000426 const MCFragment &F) const {
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000427 switch (F.getKind()) {
428 case MCFragment::FT_Data:
Eli Bendersky0fdcef62013-01-08 22:05:10 +0000429 case MCFragment::FT_Relaxable:
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000430 case MCFragment::FT_CompactEncodedInst:
Eli Bendersky0fdcef62013-01-08 22:05:10 +0000431 return cast<MCEncodedFragment>(F).getContents().size();
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000432 case MCFragment::FT_Fill:
433 return cast<MCFillFragment>(F).getSize();
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000434
Rafael Espindola3ff57092010-11-02 17:22:24 +0000435 case MCFragment::FT_LEB:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000436 return cast<MCLEBFragment>(F).getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000437
Rafael Espindola7a459032010-12-21 20:35:18 +0000438 case MCFragment::FT_Align: {
439 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
440 unsigned Offset = Layout.getFragmentOffset(&AF);
441 unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
Owen Anderson15b7a982012-08-29 22:18:56 +0000442 // If we are padding with nops, force the padding to be larger than the
443 // minimum nop size.
444 if (Size > 0 && AF.hasEmitNops()) {
445 while (Size % getBackend().getMinimumNopSize())
446 Size += AF.getAlignment();
447 }
Rafael Espindola7a459032010-12-21 20:35:18 +0000448 if (Size > AF.getMaxBytesToEmit())
449 return 0;
450 return Size;
451 }
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000452
Rafael Espindola7a459032010-12-21 20:35:18 +0000453 case MCFragment::FT_Org: {
David Blaikief12b3792013-02-11 01:16:51 +0000454 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
Rafael Espindola7a459032010-12-21 20:35:18 +0000455 int64_t TargetLocation;
456 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
457 report_fatal_error("expected assembly-time absolute expression");
458
459 // FIXME: We need a way to communicate this error.
460 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
461 int64_t Size = TargetLocation - FragmentOffset;
462 if (Size < 0 || Size >= 0x40000000)
463 report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
464 "' (at offset '" + Twine(FragmentOffset) + "')");
465 return Size;
466 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000467
Rafael Espindola187d8332010-11-07 02:07:12 +0000468 case MCFragment::FT_Dwarf:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000469 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
Rafael Espindola245a1e22010-12-28 05:39:27 +0000470 case MCFragment::FT_DwarfFrame:
471 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000472 }
473
Craig Topper85814382012-02-07 05:05:23 +0000474 llvm_unreachable("invalid fragment kind");
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000475}
476
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000477void MCAsmLayout::layoutFragment(MCFragment *F) {
Daniel Dunbar9005d452010-05-14 00:37:21 +0000478 MCFragment *Prev = F->getPrevNode();
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000479
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000480 // We should never try to recompute something which is valid.
481 assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
482 // We should never try to compute the fragment layout if its predecessor
483 // isn't valid.
484 assert((!Prev || isFragmentValid(Prev)) &&
485 "Attempt to compute fragment before its predecessor!");
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000486
487 ++stats::FragmentLayouts;
488
Daniel Dunbarb69fc042010-05-13 20:40:12 +0000489 // Compute fragment offset and size.
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000490 if (Prev)
Eli Bendersky4766ef42012-12-20 19:05:53 +0000491 F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
492 else
493 F->Offset = 0;
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000494 LastValidFragment[F->getParent()] = F;
Eli Bendersky4766ef42012-12-20 19:05:53 +0000495
496 // If bundling is enabled and this fragment has instructions in it, it has to
497 // obey the bundling restrictions. With padding, we'll have:
498 //
499 //
500 // BundlePadding
Derek Schuffb11917c2013-01-31 17:00:03 +0000501 // |||
Eli Bendersky4766ef42012-12-20 19:05:53 +0000502 // -------------------------------------
503 // Prev |##########| F |
504 // -------------------------------------
505 // ^
506 // |
507 // F->Offset
508 //
509 // The fragment's offset will point to after the padding, and its computed
510 // size won't include the padding.
511 //
512 if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
513 assert(isa<MCEncodedFragment>(F) &&
514 "Only MCEncodedFragment implementations have instructions");
515 uint64_t FSize = Assembler.computeFragmentSize(*this, *F);
516
517 if (FSize > Assembler.getBundleAlignSize())
518 report_fatal_error("Fragment can't be larger than a bundle size");
519
520 uint64_t RequiredBundlePadding = computeBundlePadding(F, F->Offset, FSize);
521 if (RequiredBundlePadding > UINT8_MAX)
522 report_fatal_error("Padding cannot exceed 255 bytes");
523 F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
524 F->Offset += RequiredBundlePadding;
525 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000526}
527
Eli Bendersky64d9a322012-12-07 19:13:57 +0000528/// \brief Write the contents of a fragment to the given object writer. Expects
529/// a MCEncodedFragment.
530static void writeFragmentContents(const MCFragment &F, MCObjectWriter *OW) {
David Blaikief12b3792013-02-11 01:16:51 +0000531 const MCEncodedFragment &EF = cast<MCEncodedFragment>(F);
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000532 OW->WriteBytes(EF.getContents());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000533}
534
535/// \brief Write the fragment \p F to the output file.
536static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout,
537 const MCFragment &F) {
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000538 MCObjectWriter *OW = &Asm.getWriter();
Eli Bendersky4766ef42012-12-20 19:05:53 +0000539
Derek Schuffb11917c2013-01-31 17:00:03 +0000540 // FIXME: Embed in fragments instead?
541 uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
542
Eli Bendersky4766ef42012-12-20 19:05:53 +0000543 // Should NOP padding be written out before this fragment?
544 unsigned BundlePadding = F.getBundlePadding();
545 if (BundlePadding > 0) {
546 assert(Asm.isBundlingEnabled() &&
547 "Writing bundle padding with disabled bundling");
548 assert(F.hasInstructions() &&
549 "Writing bundle padding for a fragment without instructions");
550
Derek Schuffb11917c2013-01-31 17:00:03 +0000551 unsigned TotalLength = BundlePadding + static_cast<unsigned>(FragmentSize);
552 if (F.alignToBundleEnd() && TotalLength > Asm.getBundleAlignSize()) {
553 // If the padding itself crosses a bundle boundary, it must be emitted
554 // in 2 pieces, since even nop instructions must not cross boundaries.
555 // v--------------v <- BundleAlignSize
556 // v---------v <- BundlePadding
557 // ----------------------------
558 // | Prev |####|####| F |
559 // ----------------------------
560 // ^-------------------^ <- TotalLength
561 unsigned DistanceToBoundary = TotalLength - Asm.getBundleAlignSize();
562 if (!Asm.getBackend().writeNopData(DistanceToBoundary, OW))
563 report_fatal_error("unable to write NOP sequence of " +
564 Twine(DistanceToBoundary) + " bytes");
565 BundlePadding -= DistanceToBoundary;
566 }
Eli Bendersky4766ef42012-12-20 19:05:53 +0000567 if (!Asm.getBackend().writeNopData(BundlePadding, OW))
568 report_fatal_error("unable to write NOP sequence of " +
569 Twine(BundlePadding) + " bytes");
570 }
571
572 // This variable (and its dummy usage) is to participate in the assert at
573 // the end of the function.
Daniel Dunbar53b23382010-03-19 09:28:59 +0000574 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000575 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000576
Daniel Dunbarff547842010-03-23 23:47:14 +0000577 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000578
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000579 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000580 case MCFragment::FT_Align: {
Eli Bendersky6ac81f52012-12-10 18:59:39 +0000581 ++stats::EmittedAlignFragments;
David Blaikief12b3792013-02-11 01:16:51 +0000582 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000583 uint64_t Count = FragmentSize / AF.getValueSize();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000584
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000585 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
586
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000587 // FIXME: This error shouldn't actually occur (the front end should emit
588 // multiple .align directives to enforce the semantics it wants), but is
589 // severe enough that we want to report it. How to handle this?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000590 if (Count * AF.getValueSize() != FragmentSize)
Chris Lattner75361b62010-04-07 22:58:41 +0000591 report_fatal_error("undefined .align directive, value size '" +
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000592 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000593 "' is not a divisor of padding size '" +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000594 Twine(FragmentSize) + "'");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000595
Kevin Enderby6e720482010-02-23 18:26:34 +0000596 // See if we are aligning with nops, and if so do that first to try to fill
597 // the Count bytes. Then if that did not fill any bytes or there are any
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000598 // bytes left to fill use the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000599 // If we are aligning with nops, ask that target to emit the right data.
Daniel Dunbar1c154132010-05-12 22:56:23 +0000600 if (AF.hasEmitNops()) {
Jim Grosbachec343382012-01-18 18:52:16 +0000601 if (!Asm.getBackend().writeNopData(Count, OW))
Chris Lattner75361b62010-04-07 22:58:41 +0000602 report_fatal_error("unable to write nop sequence of " +
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000603 Twine(Count) + " bytes");
604 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000605 }
606
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000607 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000608 for (uint64_t i = 0; i != Count; ++i) {
609 switch (AF.getValueSize()) {
Craig Topper85814382012-02-07 05:05:23 +0000610 default: llvm_unreachable("Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000611 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
612 case 2: OW->Write16(uint16_t(AF.getValue())); break;
613 case 4: OW->Write32(uint32_t(AF.getValue())); break;
614 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000615 }
616 }
617 break;
618 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000619
Eli Bendersky64d9a322012-12-07 19:13:57 +0000620 case MCFragment::FT_Data:
Eli Bendersky8ddc5a12012-12-07 17:59:21 +0000621 ++stats::EmittedDataFragments;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000622 writeFragmentContents(F, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000623 break;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000624
Eli Bendersky251040b2013-01-08 00:22:56 +0000625 case MCFragment::FT_Relaxable:
Eli Bendersky6f6204f2013-01-08 17:41:59 +0000626 ++stats::EmittedRelaxableFragments;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000627 writeFragmentContents(F, OW);
628 break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000629
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000630 case MCFragment::FT_CompactEncodedInst:
631 ++stats::EmittedCompactEncodedInstFragments;
632 writeFragmentContents(F, OW);
633 break;
634
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000635 case MCFragment::FT_Fill: {
Eli Bendersky6ac81f52012-12-10 18:59:39 +0000636 ++stats::EmittedFillFragments;
David Blaikief12b3792013-02-11 01:16:51 +0000637 const MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000638
639 assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
640
Daniel Dunbar3153fec2010-05-12 22:51:32 +0000641 for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000642 switch (FF.getValueSize()) {
Craig Topper85814382012-02-07 05:05:23 +0000643 default: llvm_unreachable("Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000644 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
645 case 2: OW->Write16(uint16_t(FF.getValue())); break;
646 case 4: OW->Write32(uint32_t(FF.getValue())); break;
647 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000648 }
649 }
650 break;
651 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000652
Rafael Espindola3ff57092010-11-02 17:22:24 +0000653 case MCFragment::FT_LEB: {
David Blaikief12b3792013-02-11 01:16:51 +0000654 const MCLEBFragment &LF = cast<MCLEBFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000655 OW->WriteBytes(LF.getContents().str());
Rafael Espindola3ff57092010-11-02 17:22:24 +0000656 break;
657 }
658
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000659 case MCFragment::FT_Org: {
Eli Bendersky6ac81f52012-12-10 18:59:39 +0000660 ++stats::EmittedOrgFragments;
David Blaikief12b3792013-02-11 01:16:51 +0000661 const MCOrgFragment &OF = cast<MCOrgFragment>(F);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000662
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000663 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000664 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000665
666 break;
667 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000668
669 case MCFragment::FT_Dwarf: {
670 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000671 OW->WriteBytes(OF.getContents().str());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000672 break;
673 }
Rafael Espindola245a1e22010-12-28 05:39:27 +0000674 case MCFragment::FT_DwarfFrame: {
675 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
676 OW->WriteBytes(CF.getContents().str());
677 break;
678 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000679 }
680
Eli Bendersky4766ef42012-12-20 19:05:53 +0000681 assert(OW->getStream().tell() - Start == FragmentSize &&
682 "The stream should advance by fragment size");
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000683}
684
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000685void MCAssembler::writeSectionData(const MCSectionData *SD,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000686 const MCAsmLayout &Layout) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000687 // Ignore virtual sections.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000688 if (SD->getSection().isVirtualSection()) {
Daniel Dunbar054be922010-05-13 03:50:50 +0000689 assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000690
691 // Check that contents are only things legal inside a virtual section.
692 for (MCSectionData::const_iterator it = SD->begin(),
693 ie = SD->end(); it != ie; ++it) {
694 switch (it->getKind()) {
Craig Topper85814382012-02-07 05:05:23 +0000695 default: llvm_unreachable("Invalid fragment in virtual section!");
Daniel Dunbarc983b202010-08-18 18:22:37 +0000696 case MCFragment::FT_Data: {
697 // Check that we aren't trying to write a non-zero contents (or fixups)
698 // into a virtual section. This is to support clients which use standard
699 // directives to fill the contents of virtual sections.
David Blaikief12b3792013-02-11 01:16:51 +0000700 const MCDataFragment &DF = cast<MCDataFragment>(*it);
Daniel Dunbarc983b202010-08-18 18:22:37 +0000701 assert(DF.fixup_begin() == DF.fixup_end() &&
702 "Cannot have fixups in virtual section!");
703 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
704 assert(DF.getContents()[i] == 0 &&
705 "Invalid data value for virtual section!");
706 break;
707 }
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000708 case MCFragment::FT_Align:
Daniel Dunbarc983b202010-08-18 18:22:37 +0000709 // Check that we aren't trying to write a non-zero value into a virtual
710 // section.
711 assert((!cast<MCAlignFragment>(it)->getValueSize() ||
712 !cast<MCAlignFragment>(it)->getValue()) &&
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000713 "Invalid align in virtual section!");
714 break;
715 case MCFragment::FT_Fill:
716 assert(!cast<MCFillFragment>(it)->getValueSize() &&
717 "Invalid fill in virtual section!");
718 break;
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000719 }
720 }
721
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000722 return;
723 }
724
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000725 uint64_t Start = getWriter().getStream().tell();
Jim Grosbachb5762bf2012-09-18 23:05:18 +0000726 (void)Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000727
Eli Bendersky64d9a322012-12-07 19:13:57 +0000728 for (MCSectionData::const_iterator it = SD->begin(), ie = SD->end();
729 it != ie; ++it)
730 writeFragment(*this, Layout, *it);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000731
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000732 assert(getWriter().getStream().tell() - Start ==
733 Layout.getSectionAddressSize(SD));
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000734}
735
Rafael Espindola179821a2010-12-06 19:08:48 +0000736
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000737uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000738 MCFragment &F,
739 const MCFixup &Fixup) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000740 // Evaluate the fixup.
741 MCValue Target;
742 uint64_t FixedValue;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000743 if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000744 // The fixup was unresolved, we need a relocation. Inform the object
745 // writer of the relocation, and give it an opportunity to adjust the
746 // fixup value if need be.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000747 getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
Rafael Espindola179821a2010-12-06 19:08:48 +0000748 }
749 return FixedValue;
750 }
751
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000752void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000753 DEBUG_WITH_TYPE("mc-dump", {
754 llvm::errs() << "assembler backend - pre-layout\n--\n";
755 dump(); });
756
Daniel Dunbar61066db2010-05-13 02:34:14 +0000757 // Create the layout object.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000758 MCAsmLayout Layout(*this);
Daniel Dunbar61066db2010-05-13 02:34:14 +0000759
Daniel Dunbar337718e2010-05-14 00:37:14 +0000760 // Create dummy fragments and assign section ordinals.
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000761 unsigned SectionIndex = 0;
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000762 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
763 // Create dummy fragments to eliminate any empty sections, this simplifies
764 // layout.
Duncan Sands6f74f692010-06-29 13:30:08 +0000765 if (it->getFragmentList().empty())
Rafael Espindolad80781b2010-09-15 21:48:40 +0000766 new MCDataFragment(it);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000767
768 it->setOrdinal(SectionIndex++);
Daniel Dunbar337718e2010-05-14 00:37:14 +0000769 }
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000770
Daniel Dunbar337718e2010-05-14 00:37:14 +0000771 // Assign layout order indices to sections and fragments.
Daniel Dunbar337718e2010-05-14 00:37:14 +0000772 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
773 MCSectionData *SD = Layout.getSectionOrder()[i];
774 SD->setLayoutOrder(i);
775
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000776 unsigned FragmentIndex = 0;
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000777 for (MCSectionData::iterator iFrag = SD->begin(), iFragEnd = SD->end();
778 iFrag != iFragEnd; ++iFrag)
779 iFrag->setLayoutOrder(FragmentIndex++);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000780 }
781
Daniel Dunbar61066db2010-05-13 02:34:14 +0000782 // Layout until everything fits.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000783 while (layoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000784 continue;
785
786 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000787 llvm::errs() << "assembler backend - post-relaxation\n--\n";
788 dump(); });
789
790 // Finalize the layout, including fragment lowering.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000791 finishLayout(Layout);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000792
793 DEBUG_WITH_TYPE("mc-dump", {
794 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000795 dump(); });
796
Daniel Dunbarff547842010-03-23 23:47:14 +0000797 uint64_t StartOffset = OS.tell();
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000798
Daniel Dunbarbacba992010-03-19 07:09:33 +0000799 // Allow the object writer a chance to perform post-layout binding (for
800 // example, to set the index fields in the symbol data).
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000801 getWriter().ExecutePostLayoutBinding(*this, Layout);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000802
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000803 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000804 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
805 for (MCSectionData::iterator it2 = it->begin(),
806 ie2 = it->end(); it2 != ie2; ++it2) {
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000807 MCEncodedFragmentWithFixups *F =
808 dyn_cast<MCEncodedFragmentWithFixups>(it2);
Eli Bendersky64d9a322012-12-07 19:13:57 +0000809 if (F) {
Eli Bendersky9ccb7692013-01-15 23:22:09 +0000810 for (MCEncodedFragmentWithFixups::fixup_iterator it3 = F->fixup_begin(),
Eli Bendersky64d9a322012-12-07 19:13:57 +0000811 ie3 = F->fixup_end(); it3 != ie3; ++it3) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000812 MCFixup &Fixup = *it3;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000813 uint64_t FixedValue = handleFixup(Layout, *F, Fixup);
814 getBackend().applyFixup(Fixup, F->getContents().data(),
815 F->getContents().size(), FixedValue);
Rafael Espindola179821a2010-12-06 19:08:48 +0000816 }
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000817 }
818 }
819 }
820
Daniel Dunbarbacba992010-03-19 07:09:33 +0000821 // Write the object file.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000822 getWriter().WriteObject(*this, Layout);
Daniel Dunbarff547842010-03-23 23:47:14 +0000823
824 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000825}
826
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000827bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
Eli Bendersky251040b2013-01-08 00:22:56 +0000828 const MCRelaxableFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000829 const MCAsmLayout &Layout) const {
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000830 // If we cannot resolve the fixup value, it requires relaxation.
831 MCValue Target;
832 uint64_t Value;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000833 if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000834 return true;
835
Jim Grosbach370b78d2011-12-06 00:47:03 +0000836 return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000837}
838
Eli Bendersky251040b2013-01-08 00:22:56 +0000839bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000840 const MCAsmLayout &Layout) const {
841 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
842 // are intentionally pushing out inst fragments, or because we relaxed a
843 // previous instruction to one that doesn't need relaxation.
Eli Bendersky251040b2013-01-08 00:22:56 +0000844 if (!getBackend().mayNeedRelaxation(F->getInst()))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000845 return false;
846
Eli Bendersky251040b2013-01-08 00:22:56 +0000847 for (MCRelaxableFragment::const_fixup_iterator it = F->fixup_begin(),
848 ie = F->fixup_end(); it != ie; ++it)
849 if (fixupNeedsRelaxation(*it, F, Layout))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000850 return true;
851
852 return false;
853}
854
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000855bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
Eli Bendersky251040b2013-01-08 00:22:56 +0000856 MCRelaxableFragment &F) {
857 if (!fragmentNeedsRelaxation(&F, Layout))
Rafael Espindola3ff57092010-11-02 17:22:24 +0000858 return false;
859
860 ++stats::RelaxedInstructions;
861
862 // FIXME-PERF: We could immediately lower out instructions if we can tell
863 // they are fully resolved, to avoid retesting on later passes.
864
865 // Relax the fragment.
866
867 MCInst Relaxed;
Eli Bendersky251040b2013-01-08 00:22:56 +0000868 getBackend().relaxInstruction(F.getInst(), Relaxed);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000869
870 // Encode the new instruction.
871 //
872 // FIXME-PERF: If it matters, we could let the target do this. It can
873 // probably do so more efficiently in many cases.
874 SmallVector<MCFixup, 4> Fixups;
875 SmallString<256> Code;
876 raw_svector_ostream VecOS(Code);
877 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
878 VecOS.flush();
879
Eli Bendersky251040b2013-01-08 00:22:56 +0000880 // Update the fragment.
881 F.setInst(Relaxed);
882 F.getContents() = Code;
883 F.getFixups() = Fixups;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000884
Rafael Espindola3ff57092010-11-02 17:22:24 +0000885 return true;
886}
887
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000888bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000889 int64_t Value = 0;
890 uint64_t OldSize = LF.getContents().size();
Rafael Espindolad88cac02011-04-26 02:17:58 +0000891 bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout);
892 (void)IsAbs;
893 assert(IsAbs);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000894 SmallString<8> &Data = LF.getContents();
895 Data.clear();
896 raw_svector_ostream OSE(Data);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000897 if (LF.isSigned())
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000898 encodeSLEB128(Value, OSE);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000899 else
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000900 encodeULEB128(Value, OSE);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000901 OSE.flush();
902 return OldSize != LF.getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000903}
904
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000905bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000906 MCDwarfLineAddrFragment &DF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000907 int64_t AddrDelta = 0;
908 uint64_t OldSize = DF.getContents().size();
Rafael Espindola835439a2010-12-22 22:04:28 +0000909 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
910 (void)IsAbs;
911 assert(IsAbs);
Rafael Espindola187d8332010-11-07 02:07:12 +0000912 int64_t LineDelta;
913 LineDelta = DF.getLineDelta();
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000914 SmallString<8> &Data = DF.getContents();
915 Data.clear();
916 raw_svector_ostream OSE(Data);
917 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE);
918 OSE.flush();
919 return OldSize != Data.size();
Rafael Espindola187d8332010-11-07 02:07:12 +0000920}
921
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000922bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
Rafael Espindola245a1e22010-12-28 05:39:27 +0000923 MCDwarfCallFrameFragment &DF) {
924 int64_t AddrDelta = 0;
925 uint64_t OldSize = DF.getContents().size();
926 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
927 (void)IsAbs;
928 assert(IsAbs);
929 SmallString<8> &Data = DF.getContents();
930 Data.clear();
931 raw_svector_ostream OSE(Data);
Rafael Espindola4eafe102011-05-08 14:35:21 +0000932 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE);
Rafael Espindola245a1e22010-12-28 05:39:27 +0000933 OSE.flush();
934 return OldSize != Data.size();
935}
936
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000937bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD) {
938 // Holds the first fragment which needed relaxing during this layout. It will
939 // remain NULL if none were relaxed.
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000940 // When a fragment is relaxed, all the fragments following it should get
941 // invalidated because their offset is going to change.
942 MCFragment *FirstRelaxedFragment = NULL;
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000943
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000944 // Attempt to relax all the fragments in the section.
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000945 for (MCSectionData::iterator I = SD.begin(), IE = SD.end(); I != IE; ++I) {
946 // Check if this is a fragment that needs relaxation.
947 bool RelaxedFrag = false;
948 switch(I->getKind()) {
Rafael Espindola62b83b62010-12-21 04:22:09 +0000949 default:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000950 break;
Eli Bendersky251040b2013-01-08 00:22:56 +0000951 case MCFragment::FT_Relaxable:
Eli Bendersky37a98302012-12-11 17:16:00 +0000952 assert(!getRelaxAll() &&
Eli Bendersky251040b2013-01-08 00:22:56 +0000953 "Did not expect a MCRelaxableFragment in RelaxAll mode");
954 RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
Rafael Espindola62b83b62010-12-21 04:22:09 +0000955 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000956 case MCFragment::FT_Dwarf:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000957 RelaxedFrag = relaxDwarfLineAddr(Layout,
958 *cast<MCDwarfLineAddrFragment>(I));
Rafael Espindola62b83b62010-12-21 04:22:09 +0000959 break;
Rafael Espindola245a1e22010-12-28 05:39:27 +0000960 case MCFragment::FT_DwarfFrame:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000961 RelaxedFrag =
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000962 relaxDwarfCallFrameFragment(Layout,
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000963 *cast<MCDwarfCallFrameFragment>(I));
Rafael Espindola245a1e22010-12-28 05:39:27 +0000964 break;
965 case MCFragment::FT_LEB:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000966 RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
Rafael Espindola245a1e22010-12-28 05:39:27 +0000967 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000968 }
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000969 if (RelaxedFrag && !FirstRelaxedFragment)
970 FirstRelaxedFragment = I;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000971 }
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000972 if (FirstRelaxedFragment) {
Derek Schufff918d7f2013-02-05 17:55:27 +0000973 Layout.invalidateFragmentsFrom(FirstRelaxedFragment);
Rafael Espindola62b83b62010-12-21 04:22:09 +0000974 return true;
975 }
976 return false;
977}
978
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000979bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000980 ++stats::RelaxationSteps;
981
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000982 bool WasRelaxed = false;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000983 for (iterator it = begin(), ie = end(); it != ie; ++it) {
984 MCSectionData &SD = *it;
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000985 while (layoutSectionOnce(Layout, SD))
Rafael Espindola62b83b62010-12-21 04:22:09 +0000986 WasRelaxed = true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000987 }
988
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000989 return WasRelaxed;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000990}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000991
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000992void MCAssembler::finishLayout(MCAsmLayout &Layout) {
Rafael Espindolab4172fa2010-12-04 22:47:22 +0000993 // The layout is done. Mark every fragment as valid.
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000994 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
995 Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
996 }
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000997}
998
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000999// Debugging methods
1000
1001namespace llvm {
1002
Daniel Dunbarc90e30a2010-05-26 15:18:56 +00001003raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
1004 OS << "<MCFixup" << " Offset:" << AF.getOffset()
Daniel Dunbar482ad802010-05-26 15:18:31 +00001005 << " Value:" << *AF.getValue()
1006 << " Kind:" << AF.getKind() << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001007 return OS;
1008}
1009
1010}
1011
Manman Ren286c4dc2012-09-12 05:06:18 +00001012#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001013void MCFragment::dump() {
1014 raw_ostream &OS = llvm::errs();
1015
Daniel Dunbare614e392010-05-26 06:50:57 +00001016 OS << "<";
1017 switch (getKind()) {
1018 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
1019 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
Eli Bendersky9ccb7692013-01-15 23:22:09 +00001020 case MCFragment::FT_CompactEncodedInst:
1021 OS << "MCCompactEncodedInstFragment"; break;
Daniel Dunbare614e392010-05-26 06:50:57 +00001022 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
Eli Bendersky251040b2013-01-08 00:22:56 +00001023 case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
Daniel Dunbare614e392010-05-26 06:50:57 +00001024 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
Kevin Enderbyc0957932010-09-30 16:52:03 +00001025 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
Rafael Espindola245a1e22010-12-28 05:39:27 +00001026 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
Rafael Espindola3ff57092010-11-02 17:22:24 +00001027 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
Daniel Dunbare614e392010-05-26 06:50:57 +00001028 }
1029
Daniel Dunbar337718e2010-05-14 00:37:14 +00001030 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
Eli Bendersky4766ef42012-12-20 19:05:53 +00001031 << " Offset:" << Offset
1032 << " HasInstructions:" << hasInstructions()
Derek Schufff918d7f2013-02-05 17:55:27 +00001033 << " BundlePadding:" << static_cast<unsigned>(getBundlePadding()) << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001034
Daniel Dunbare614e392010-05-26 06:50:57 +00001035 switch (getKind()) {
1036 case MCFragment::FT_Align: {
1037 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
1038 if (AF->hasEmitNops())
1039 OS << " (emit nops)";
Daniel Dunbare614e392010-05-26 06:50:57 +00001040 OS << "\n ";
1041 OS << " Alignment:" << AF->getAlignment()
1042 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
1043 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
1044 break;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001045 }
Daniel Dunbare614e392010-05-26 06:50:57 +00001046 case MCFragment::FT_Data: {
1047 const MCDataFragment *DF = cast<MCDataFragment>(this);
1048 OS << "\n ";
1049 OS << " Contents:[";
1050 const SmallVectorImpl<char> &Contents = DF->getContents();
1051 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
1052 if (i) OS << ",";
1053 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001054 }
Daniel Dunbare614e392010-05-26 06:50:57 +00001055 OS << "] (" << Contents.size() << " bytes)";
1056
Eli Bendersky5c10f502012-12-05 22:11:02 +00001057 if (DF->fixup_begin() != DF->fixup_end()) {
Daniel Dunbare614e392010-05-26 06:50:57 +00001058 OS << ",\n ";
1059 OS << " Fixups:[";
1060 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
1061 ie = DF->fixup_end(); it != ie; ++it) {
1062 if (it != DF->fixup_begin()) OS << ",\n ";
1063 OS << *it;
1064 }
1065 OS << "]";
1066 }
1067 break;
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001068 }
Eli Bendersky9ccb7692013-01-15 23:22:09 +00001069 case MCFragment::FT_CompactEncodedInst: {
1070 const MCCompactEncodedInstFragment *CEIF =
1071 cast<MCCompactEncodedInstFragment>(this);
1072 OS << "\n ";
1073 OS << " Contents:[";
1074 const SmallVectorImpl<char> &Contents = CEIF->getContents();
1075 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
1076 if (i) OS << ",";
1077 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
1078 }
1079 OS << "] (" << Contents.size() << " bytes)";
1080 break;
1081 }
Daniel Dunbare614e392010-05-26 06:50:57 +00001082 case MCFragment::FT_Fill: {
1083 const MCFillFragment *FF = cast<MCFillFragment>(this);
1084 OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
1085 << " Size:" << FF->getSize();
1086 break;
1087 }
Eli Bendersky251040b2013-01-08 00:22:56 +00001088 case MCFragment::FT_Relaxable: {
1089 const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
Daniel Dunbare614e392010-05-26 06:50:57 +00001090 OS << "\n ";
1091 OS << " Inst:";
Eli Bendersky251040b2013-01-08 00:22:56 +00001092 F->getInst().dump_pretty(OS);
Daniel Dunbare614e392010-05-26 06:50:57 +00001093 break;
1094 }
1095 case MCFragment::FT_Org: {
1096 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
1097 OS << "\n ";
1098 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
1099 break;
1100 }
Kevin Enderbyc0957932010-09-30 16:52:03 +00001101 case MCFragment::FT_Dwarf: {
1102 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
1103 OS << "\n ";
1104 OS << " AddrDelta:" << OF->getAddrDelta()
1105 << " LineDelta:" << OF->getLineDelta();
1106 break;
1107 }
Rafael Espindola245a1e22010-12-28 05:39:27 +00001108 case MCFragment::FT_DwarfFrame: {
1109 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
1110 OS << "\n ";
1111 OS << " AddrDelta:" << CF->getAddrDelta();
1112 break;
1113 }
Rafael Espindola3ff57092010-11-02 17:22:24 +00001114 case MCFragment::FT_LEB: {
1115 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
1116 OS << "\n ";
1117 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
1118 break;
1119 }
Daniel Dunbare614e392010-05-26 06:50:57 +00001120 }
Daniel Dunbar0bcf0742010-02-13 09:28:43 +00001121 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001122}
1123
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001124void MCSectionData::dump() {
1125 raw_ostream &OS = llvm::errs();
1126
1127 OS << "<MCSectionData";
Eli Bendersky4766ef42012-12-20 19:05:53 +00001128 OS << " Alignment:" << getAlignment()
1129 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001130 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1131 if (it != begin()) OS << ",\n ";
1132 it->dump();
1133 }
1134 OS << "]>";
1135}
1136
1137void MCSymbolData::dump() {
1138 raw_ostream &OS = llvm::errs();
1139
1140 OS << "<MCSymbolData Symbol:" << getSymbol()
1141 << " Fragment:" << getFragment() << " Offset:" << getOffset()
1142 << " Flags:" << getFlags() << " Index:" << getIndex();
1143 if (isCommon())
1144 OS << " (common, size:" << getCommonSize()
1145 << " align: " << getCommonAlignment() << ")";
1146 if (isExternal())
1147 OS << " (external)";
1148 if (isPrivateExtern())
1149 OS << " (private extern)";
1150 OS << ">";
1151}
1152
1153void MCAssembler::dump() {
1154 raw_ostream &OS = llvm::errs();
1155
1156 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001157 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001158 for (iterator it = begin(), ie = end(); it != ie; ++it) {
1159 if (it != begin()) OS << ",\n ";
1160 it->dump();
1161 }
1162 OS << "],\n";
1163 OS << " Symbols:[";
1164
1165 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +00001166 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +00001167 it->dump();
1168 }
1169 OS << "]>\n";
1170}
Manman Rencc77eec2012-09-06 19:55:56 +00001171#endif
David Blaikie2d24e2a2011-12-20 02:50:00 +00001172
1173// anchors for MC*Fragment vtables
Eli Bendersky64d9a322012-12-07 19:13:57 +00001174void MCEncodedFragment::anchor() { }
Eli Bendersky9ccb7692013-01-15 23:22:09 +00001175void MCEncodedFragmentWithFixups::anchor() { }
David Blaikie2d24e2a2011-12-20 02:50:00 +00001176void MCDataFragment::anchor() { }
Eli Bendersky9ccb7692013-01-15 23:22:09 +00001177void MCCompactEncodedInstFragment::anchor() { }
Eli Bendersky251040b2013-01-08 00:22:56 +00001178void MCRelaxableFragment::anchor() { }
David Blaikie2d24e2a2011-12-20 02:50:00 +00001179void MCAlignFragment::anchor() { }
1180void MCFillFragment::anchor() { }
1181void MCOrgFragment::anchor() { }
1182void MCLEBFragment::anchor() { }
1183void MCDwarfLineAddrFragment::anchor() { }
1184void MCDwarfCallFrameFragment::anchor() { }