blob: 031820d528645007a89db23ba2f47575776ddaaf [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"
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +000012#include "llvm/MC/MCAsmLayout.h"
Daniel Dunbarb36052f2010-03-19 10:43:23 +000013#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000014#include "llvm/MC/MCExpr.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000015#include "llvm/MC/MCObjectWriter.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000016#include "llvm/MC/MCSymbol.h"
17#include "llvm/MC/MCValue.h"
Daniel Dunbar1a9158c2010-03-19 10:43:26 +000018#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar0adcd352009-08-25 21:10:45 +000019#include "llvm/ADT/Statistic.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000020#include "llvm/ADT/StringExtras.h"
Daniel Dunbard6f761e2009-08-21 23:07:38 +000021#include "llvm/ADT/Twine.h"
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000022#include "llvm/Support/ErrorHandling.h"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000023#include "llvm/Support/raw_ostream.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000024#include "llvm/Support/Debug.h"
Daniel Dunbaree0d8922010-03-13 22:10:17 +000025#include "llvm/Target/TargetRegistry.h"
Daniel Dunbardf3c8f22010-03-12 21:00:49 +000026#include "llvm/Target/TargetAsmBackend.h"
Daniel Dunbarf6346762010-02-13 09:29:02 +000027
28// FIXME: Gross.
29#include "../Target/X86/X86FixupKinds.h"
30
Chris Lattner23132b12009-08-24 03:52:50 +000031#include <vector>
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000032using namespace llvm;
33
Daniel Dunbar0adcd352009-08-25 21:10:45 +000034STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
35
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000036// FIXME FIXME FIXME: There are number of places in this file where we convert
37// what is a 64-bit assembler value used for computation into a value in the
38// object file, which may truncate it. We should detect that truncation where
39// invalid and report errors back.
40
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000041/* *** */
42
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000043MCFragment::MCFragment() : Kind(FragmentType(~0)) {
44}
45
Daniel Dunbar5e835962009-08-26 02:48:04 +000046MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000047 : Kind(_Kind),
Daniel Dunbar5e835962009-08-26 02:48:04 +000048 Parent(_Parent),
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000049 FileSize(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000050{
Daniel Dunbar5e835962009-08-26 02:48:04 +000051 if (Parent)
52 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000053}
54
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000055MCFragment::~MCFragment() {
56}
57
Daniel Dunbar5e835962009-08-26 02:48:04 +000058uint64_t MCFragment::getAddress() const {
59 assert(getParent() && "Missing Section!");
60 return getParent()->getAddress() + Offset;
61}
62
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000063/* *** */
64
Daniel Dunbar81e40002009-08-27 00:38:04 +000065MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000066
67MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +000068 : Section(&_Section),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000069 Alignment(1),
Daniel Dunbar5e835962009-08-26 02:48:04 +000070 Address(~UINT64_C(0)),
Daniel Dunbar6742e342009-08-26 04:13:32 +000071 Size(~UINT64_C(0)),
Daniel Dunbar3f6a9602009-08-26 13:58:10 +000072 FileSize(~UINT64_C(0)),
Daniel Dunbare1ec6172010-02-02 21:44:01 +000073 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000074{
75 if (A)
76 A->getSectionList().push_back(this);
77}
78
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000079/* *** */
80
Daniel Dunbarefbb5332009-09-01 04:09:03 +000081MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000082
Daniel Dunbarcb579b32009-08-31 08:08:06 +000083MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000084 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +000085 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000086 IsExternal(false), IsPrivateExtern(false),
87 CommonSize(0), CommonAlign(0), Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +000088{
89 if (A)
90 A->getSymbolList().push_back(this);
91}
92
93/* *** */
94
Daniel Dunbar1f3e4452010-03-11 01:34:27 +000095MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
Daniel Dunbarcf871e52010-03-19 10:43:18 +000096 MCCodeEmitter &_Emitter, raw_ostream &_OS)
97 : Context(_Context), Backend(_Backend), Emitter(_Emitter),
98 OS(_OS), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +000099{
100}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000101
102MCAssembler::~MCAssembler() {
103}
104
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000105static bool isScatteredFixupFullyResolvedSimple(const MCAssembler &Asm,
106 const MCAsmFixup &Fixup,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000107 const MCValue Target,
108 const MCSection *BaseSection) {
109 // The effective fixup address is
110 // addr(atom(A)) + offset(A)
111 // - addr(atom(B)) - offset(B)
112 // - addr(<base symbol>) + <fixup offset from base symbol>
113 // and the offsets are not relocatable, so the fixup is fully resolved when
114 // addr(atom(A)) - addr(atom(B)) - addr(<base symbol>)) == 0.
115 //
116 // The simple (Darwin, except on x86_64) way of dealing with this was to
117 // assume that any reference to a temporary symbol *must* be a temporary
118 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
119 // relocation to a temporary symbol (in the same section) is fully
120 // resolved. This also works in conjunction with absolutized .set, which
121 // requires the compiler to use .set to absolutize the differences between
122 // symbols which the compiler knows to be assembly time constants, so we don't
123 // need to worry about consider symbol differences fully resolved.
124
125 // Non-relative fixups are only resolved if constant.
126 if (!BaseSection)
127 return Target.isAbsolute();
128
129 // Otherwise, relative fixups are only resolved if not a difference and the
130 // target is a temporary in the same section.
131 if (Target.isAbsolute() || Target.getSymB())
132 return false;
133
134 const MCSymbol *A = &Target.getSymA()->getSymbol();
135 if (!A->isTemporary() || !A->isInSection() ||
136 &A->getSection() != BaseSection)
137 return false;
138
139 return true;
140}
141
Daniel Dunbar034843a2010-03-19 03:18:18 +0000142static bool isScatteredFixupFullyResolved(const MCAssembler &Asm,
143 const MCAsmFixup &Fixup,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000144 const MCValue Target,
145 const MCSymbolData *BaseSymbol) {
146 // The effective fixup address is
147 // addr(atom(A)) + offset(A)
148 // - addr(atom(B)) - offset(B)
149 // - addr(BaseSymbol) + <fixup offset from base symbol>
150 // and the offsets are not relocatable, so the fixup is fully resolved when
151 // addr(atom(A)) - addr(atom(B)) - addr(BaseSymbol) == 0.
152 //
153 // Note that "false" is almost always conservatively correct (it means we emit
154 // a relocation which is unnecessary), except when it would force us to emit a
155 // relocation which the target cannot encode.
156
157 const MCSymbolData *A_Base = 0, *B_Base = 0;
158 if (const MCSymbolRefExpr *A = Target.getSymA()) {
159 // Modified symbol references cannot be resolved.
160 if (A->getKind() != MCSymbolRefExpr::VK_None)
161 return false;
162
163 A_Base = Asm.getAtom(&Asm.getSymbolData(A->getSymbol()));
164 if (!A_Base)
165 return false;
166 }
167
168 if (const MCSymbolRefExpr *B = Target.getSymB()) {
169 // Modified symbol references cannot be resolved.
170 if (B->getKind() != MCSymbolRefExpr::VK_None)
171 return false;
172
173 B_Base = Asm.getAtom(&Asm.getSymbolData(B->getSymbol()));
174 if (!B_Base)
175 return false;
176 }
177
178 // If there is no base, A and B have to be the same atom for this fixup to be
179 // fully resolved.
180 if (!BaseSymbol)
181 return A_Base == B_Base;
182
183 // Otherwise, B must be missing and A must be the base.
184 return !B_Base && BaseSymbol == A_Base;
185}
186
Daniel Dunbar23869852010-03-19 03:18:09 +0000187bool MCAssembler::isSymbolLinkerVisible(const MCSymbolData *SD) const {
188 // Non-temporary labels should always be visible to the linker.
189 if (!SD->getSymbol().isTemporary())
190 return true;
191
192 // Absolute temporary labels are never visible.
193 if (!SD->getFragment())
194 return false;
195
196 // Otherwise, check if the section requires symbols even for temporary labels.
197 return getBackend().doesSectionRequireSymbols(
198 SD->getFragment()->getParent()->getSection());
199}
200
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000201const MCSymbolData *MCAssembler::getAtomForAddress(const MCSectionData *Section,
202 uint64_t Address) const {
203 const MCSymbolData *Best = 0;
204 for (MCAssembler::const_symbol_iterator it = symbol_begin(),
205 ie = symbol_end(); it != ie; ++it) {
206 // Ignore non-linker visible symbols.
207 if (!isSymbolLinkerVisible(it))
208 continue;
209
210 // Ignore symbols not in the same section.
211 if (!it->getFragment() || it->getFragment()->getParent() != Section)
212 continue;
213
214 // Otherwise, find the closest symbol preceding this address (ties are
215 // resolved in favor of the last defined symbol).
216 if (it->getAddress() <= Address &&
217 (!Best || it->getAddress() >= Best->getAddress()))
218 Best = it;
219 }
220
221 return Best;
222}
223
224const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
225 // Linker visible symbols define atoms.
226 if (isSymbolLinkerVisible(SD))
227 return SD;
228
229 // Absolute and undefined symbols have no defining atom.
230 if (!SD->getFragment())
231 return 0;
232
233 // Otherwise, search by address.
234 return getAtomForAddress(SD->getFragment()->getParent(), SD->getAddress());
235}
236
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000237bool MCAssembler::EvaluateFixup(const MCAsmLayout &Layout,
238 const MCAsmFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000239 MCValue &Target, uint64_t &Value) const {
240 if (!Fixup.Value->EvaluateAsRelocatable(Target, &Layout))
241 llvm_report_error("expected relocatable expression");
242
243 // FIXME: How do non-scattered symbols work in ELF? I presume the linker
244 // doesn't support small relocations, but then under what criteria does the
245 // assembler allow symbol differences?
246
247 Value = Target.getConstant();
248
Daniel Dunbarb36052f2010-03-19 10:43:23 +0000249 bool IsPCRel =
250 Emitter.getFixupKindInfo(Fixup.Kind).Flags & MCFixupKindInfo::FKF_IsPCRel;
251 bool IsResolved = true;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000252 if (const MCSymbolRefExpr *A = Target.getSymA()) {
253 if (A->getSymbol().isDefined())
254 Value += getSymbolData(A->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000255 else
256 IsResolved = false;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000257 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000258 if (const MCSymbolRefExpr *B = Target.getSymB()) {
259 if (B->getSymbol().isDefined())
260 Value -= getSymbolData(B->getSymbol()).getAddress();
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000261 else
262 IsResolved = false;
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000263 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000264
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000265 // If we are using scattered symbols, determine whether this value is actually
266 // resolved; scattering may cause atoms to move.
267 if (IsResolved && getBackend().hasScatteredSymbols()) {
268 if (getBackend().hasReliableSymbolDifference()) {
Daniel Dunbar034843a2010-03-19 03:18:18 +0000269 // If this is a PCrel relocation, find the base atom (identified by its
270 // symbol) that the fixup value is relative to.
271 const MCSymbolData *BaseSymbol = 0;
272 if (IsPCRel) {
273 BaseSymbol = getAtomForAddress(
274 DF->getParent(), DF->getAddress() + Fixup.Offset);
275 if (!BaseSymbol)
276 IsResolved = false;
277 }
278
279 if (IsResolved)
Daniel Dunbarc6f59822010-03-22 21:49:38 +0000280 IsResolved = isScatteredFixupFullyResolved(*this, Fixup, Target,
Daniel Dunbar034843a2010-03-19 03:18:18 +0000281 BaseSymbol);
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000282 } else {
283 const MCSection *BaseSection = 0;
284 if (IsPCRel)
285 BaseSection = &DF->getParent()->getSection();
286
Daniel Dunbarc6f59822010-03-22 21:49:38 +0000287 IsResolved = isScatteredFixupFullyResolvedSimple(*this, Fixup, Target,
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000288 BaseSection);
289 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000290 }
291
292 if (IsPCRel)
Daniel Dunbarda3e9f72010-03-13 02:38:00 +0000293 Value -= DF->getAddress() + Fixup.Offset;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000294
295 return IsResolved;
296}
297
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000298void MCAssembler::LayoutSection(MCSectionData &SD,
299 MCAsmLayout &Layout) {
Daniel Dunbar6742e342009-08-26 04:13:32 +0000300 uint64_t Address = SD.getAddress();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000301
302 for (MCSectionData::iterator it = SD.begin(), ie = SD.end(); it != ie; ++it) {
303 MCFragment &F = *it;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000304
Daniel Dunbar6742e342009-08-26 04:13:32 +0000305 F.setOffset(Address - SD.getAddress());
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000306
307 // Evaluate fragment size.
308 switch (F.getKind()) {
309 case MCFragment::FT_Align: {
310 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000311
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000312 uint64_t Size = OffsetToAlignment(Address, AF.getAlignment());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000313 if (Size > AF.getMaxBytesToEmit())
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000314 AF.setFileSize(0);
315 else
Daniel Dunbar6742e342009-08-26 04:13:32 +0000316 AF.setFileSize(Size);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000317 break;
318 }
319
320 case MCFragment::FT_Data:
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000321 F.setFileSize(cast<MCDataFragment>(F).getContents().size());
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000322 break;
323
Daniel Dunbar2a6e3f52010-03-22 20:35:43 +0000324 case MCFragment::FT_Fill: {
325 MCFillFragment &FF = cast<MCFillFragment>(F);
326 F.setFileSize(FF.getValueSize() * FF.getCount());
327 break;
328 }
329
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000330 case MCFragment::FT_Inst:
331 F.setFileSize(cast<MCInstFragment>(F).getInstSize());
332 break;
333
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000334 case MCFragment::FT_Org: {
335 MCOrgFragment &OF = cast<MCOrgFragment>(F);
336
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000337 int64_t TargetLocation;
338 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, &Layout))
339 llvm_report_error("expected assembly-time absolute expression");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000340
341 // FIXME: We need a way to communicate this error.
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000342 int64_t Offset = TargetLocation - F.getOffset();
343 if (Offset < 0)
344 llvm_report_error("invalid .org offset '" + Twine(TargetLocation) +
345 "' (at offset '" + Twine(F.getOffset()) + "'");
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000346
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +0000347 F.setFileSize(Offset);
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000348 break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000349 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000350
351 case MCFragment::FT_ZeroFill: {
352 MCZeroFillFragment &ZFF = cast<MCZeroFillFragment>(F);
353
354 // Align the fragment offset; it is safe to adjust the offset freely since
355 // this is only in virtual sections.
Daniel Dunbar37fad5c2010-03-08 21:10:42 +0000356 Address = RoundUpToAlignment(Address, ZFF.getAlignment());
357 F.setOffset(Address - SD.getAddress());
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000358
359 // FIXME: This is misnamed.
360 F.setFileSize(ZFF.getSize());
361 break;
362 }
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000363 }
364
Daniel Dunbar6742e342009-08-26 04:13:32 +0000365 Address += F.getFileSize();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000366 }
367
Daniel Dunbar6742e342009-08-26 04:13:32 +0000368 // Set the section sizes.
369 SD.setSize(Address - SD.getAddress());
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000370 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000371 SD.setFileSize(0);
372 else
373 SD.setFileSize(Address - SD.getAddress());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000374}
375
Daniel Dunbar53b23382010-03-19 09:28:59 +0000376/// WriteFragmentData - Write the \arg F data to the output file.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000377static void WriteFragmentData(const MCAssembler &Asm, const MCFragment &F,
378 MCObjectWriter *OW) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000379 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000380 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000381
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000382 ++EmittedFragments;
383
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000384 // FIXME: Embed in fragments instead?
385 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000386 case MCFragment::FT_Align: {
387 MCAlignFragment &AF = cast<MCAlignFragment>(F);
388 uint64_t Count = AF.getFileSize() / AF.getValueSize();
389
390 // FIXME: This error shouldn't actually occur (the front end should emit
391 // multiple .align directives to enforce the semantics it wants), but is
392 // severe enough that we want to report it. How to handle this?
393 if (Count * AF.getValueSize() != AF.getFileSize())
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000394 llvm_report_error("undefined .align directive, value size '" +
395 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000396 "' is not a divisor of padding size '" +
397 Twine(AF.getFileSize()) + "'");
398
Kevin Enderby6e720482010-02-23 18:26:34 +0000399 // See if we are aligning with nops, and if so do that first to try to fill
400 // the Count bytes. Then if that did not fill any bytes or there are any
401 // bytes left to fill use the the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000402 // If we are aligning with nops, ask that target to emit the right data.
Kevin Enderby6e720482010-02-23 18:26:34 +0000403 if (AF.getEmitNops()) {
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000404 if (!Asm.getBackend().WriteNopData(Count, OW))
405 llvm_report_error("unable to write nop sequence of " +
406 Twine(Count) + " bytes");
407 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000408 }
409
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000410 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000411 for (uint64_t i = 0; i != Count; ++i) {
412 switch (AF.getValueSize()) {
413 default:
414 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000415 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
416 case 2: OW->Write16(uint16_t(AF.getValue())); break;
417 case 4: OW->Write32(uint32_t(AF.getValue())); break;
418 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000419 }
420 }
421 break;
422 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000423
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000424 case MCFragment::FT_Data: {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000425 MCDataFragment &DF = cast<MCDataFragment>(F);
426 assert(DF.getFileSize() == DF.getContents().size() && "Invalid size!");
427 OW->WriteBytes(DF.getContents().str());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000428 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000429 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000430
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000431 case MCFragment::FT_Fill: {
432 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000433 for (uint64_t i = 0, e = FF.getCount(); i != e; ++i) {
434 switch (FF.getValueSize()) {
435 default:
436 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000437 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
438 case 2: OW->Write16(uint16_t(FF.getValue())); break;
439 case 4: OW->Write32(uint32_t(FF.getValue())); break;
440 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000441 }
442 }
443 break;
444 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000445
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000446 case MCFragment::FT_Inst:
447 llvm_unreachable("unexpected inst fragment after lowering");
448 break;
449
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000450 case MCFragment::FT_Org: {
451 MCOrgFragment &OF = cast<MCOrgFragment>(F);
452
453 for (uint64_t i = 0, e = OF.getFileSize(); i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000454 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000455
456 break;
457 }
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000458
459 case MCFragment::FT_ZeroFill: {
460 assert(0 && "Invalid zero fill fragment in concrete section!");
461 break;
462 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000463 }
464
Daniel Dunbar53b23382010-03-19 09:28:59 +0000465 assert(OW->getStream().tell() - Start == F.getFileSize());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000466}
467
Daniel Dunbar53b23382010-03-19 09:28:59 +0000468void MCAssembler::WriteSectionData(const MCSectionData *SD,
469 MCObjectWriter *OW) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000470 // Ignore virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000471 if (getBackend().isVirtualSection(SD->getSection())) {
Daniel Dunbar53b23382010-03-19 09:28:59 +0000472 assert(SD->getFileSize() == 0);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000473 return;
474 }
475
Daniel Dunbar53b23382010-03-19 09:28:59 +0000476 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000477 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000478
Daniel Dunbar53b23382010-03-19 09:28:59 +0000479 for (MCSectionData::const_iterator it = SD->begin(),
480 ie = SD->end(); it != ie; ++it)
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000481 WriteFragmentData(*this, *it, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000482
Daniel Dunbar6742e342009-08-26 04:13:32 +0000483 // Add section padding.
Daniel Dunbar53b23382010-03-19 09:28:59 +0000484 assert(SD->getFileSize() >= SD->getSize() && "Invalid section sizes!");
485 OW->WriteZeros(SD->getFileSize() - SD->getSize());
Daniel Dunbar6742e342009-08-26 04:13:32 +0000486
Daniel Dunbar53b23382010-03-19 09:28:59 +0000487 assert(OW->getStream().tell() - Start == SD->getFileSize());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000488}
489
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000490void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000491 DEBUG_WITH_TYPE("mc-dump", {
492 llvm::errs() << "assembler backend - pre-layout\n--\n";
493 dump(); });
494
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000495 // Layout until everything fits.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000496 MCAsmLayout Layout(*this);
497 while (LayoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000498 continue;
499
500 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000501 llvm::errs() << "assembler backend - post-relaxation\n--\n";
502 dump(); });
503
504 // Finalize the layout, including fragment lowering.
505 FinishLayout(Layout);
506
507 DEBUG_WITH_TYPE("mc-dump", {
508 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000509 dump(); });
510
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000511 llvm::OwningPtr<MCObjectWriter> Writer(getBackend().createObjectWriter(OS));
512 if (!Writer)
513 llvm_report_error("unable to create object writer!");
Daniel Dunbarbacba992010-03-19 07:09:33 +0000514
515 // Allow the object writer a chance to perform post-layout binding (for
516 // example, to set the index fields in the symbol data).
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000517 Writer->ExecutePostLayoutBinding(*this);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000518
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000519 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000520 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
521 for (MCSectionData::iterator it2 = it->begin(),
522 ie2 = it->end(); it2 != ie2; ++it2) {
523 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
524 if (!DF)
525 continue;
526
527 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
528 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
529 MCAsmFixup &Fixup = *it3;
530
531 // Evaluate the fixup.
532 MCValue Target;
533 uint64_t FixedValue;
534 if (!EvaluateFixup(Layout, Fixup, DF, Target, FixedValue)) {
535 // The fixup was unresolved, we need a relocation. Inform the object
536 // writer of the relocation, and give it an opportunity to adjust the
537 // fixup value if need be.
Daniel Dunbarb7514182010-03-22 20:35:50 +0000538 Writer->RecordRelocation(*this, DF, Fixup, Target, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000539 }
540
Daniel Dunbar87190c42010-03-19 09:28:12 +0000541 getBackend().ApplyFixup(Fixup, *DF, FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000542 }
543 }
544 }
545
Daniel Dunbarbacba992010-03-19 07:09:33 +0000546 // Write the object file.
Daniel Dunbar1a9158c2010-03-19 10:43:26 +0000547 Writer->WriteObject(*this);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000548 OS.flush();
549}
550
Daniel Dunbar9d39e612010-03-22 21:49:41 +0000551bool MCAssembler::FixupNeedsRelaxation(const MCAsmFixup &Fixup,
552 const MCFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000553 const MCAsmLayout &Layout) const {
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000554 // Currently we only need to relax X86::reloc_pcrel_1byte.
555 if (unsigned(Fixup.Kind) != X86::reloc_pcrel_1byte)
556 return false;
557
558 // If we cannot resolve the fixup value, it requires relaxation.
559 MCValue Target;
560 uint64_t Value;
561 if (!EvaluateFixup(Layout, Fixup, DF, Target, Value))
562 return true;
563
564 // Otherwise, relax if the value is too big for a (signed) i8.
565 return int64_t(Value) != int64_t(int8_t(Value));
566}
567
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000568bool MCAssembler::LayoutOnce(MCAsmLayout &Layout) {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000569 // Layout the concrete sections and fragments.
Daniel Dunbar5e835962009-08-26 02:48:04 +0000570 uint64_t Address = 0;
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000571 MCSectionData *Prev = 0;
572 for (iterator it = begin(), ie = end(); it != ie; ++it) {
Daniel Dunbar6742e342009-08-26 04:13:32 +0000573 MCSectionData &SD = *it;
574
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000575 // Skip virtual sections.
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000576 if (getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000577 continue;
578
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000579 // Align this section if necessary by adding padding bytes to the previous
580 // section.
581 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment())) {
582 assert(Prev && "Missing prev section!");
583 Prev->setFileSize(Prev->getFileSize() + Pad);
584 Address += Pad;
585 }
Daniel Dunbar6742e342009-08-26 04:13:32 +0000586
587 // Layout the section fragments and its size.
588 SD.setAddress(Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000589 LayoutSection(SD, Layout);
Daniel Dunbar6742e342009-08-26 04:13:32 +0000590 Address += SD.getFileSize();
Daniel Dunbaredc670f2009-08-28 05:49:04 +0000591
592 Prev = &SD;
Daniel Dunbar5e835962009-08-26 02:48:04 +0000593 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000594
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000595 // Layout the virtual sections.
596 for (iterator it = begin(), ie = end(); it != ie; ++it) {
597 MCSectionData &SD = *it;
598
Daniel Dunbarcc5b84c2010-03-19 09:29:03 +0000599 if (!getBackend().isVirtualSection(SD.getSection()))
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000600 continue;
601
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000602 // Align this section if necessary by adding padding bytes to the previous
603 // section.
Daniel Dunbarf8b8ad72010-03-09 01:12:20 +0000604 if (uint64_t Pad = OffsetToAlignment(Address, it->getAlignment()))
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000605 Address += Pad;
Daniel Dunbarb2b4acd2010-03-08 22:03:42 +0000606
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000607 SD.setAddress(Address);
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000608 LayoutSection(SD, Layout);
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000609 Address += SD.getSize();
610 }
611
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000612 // Scan the fixups in order and relax any that don't fit.
613 for (iterator it = begin(), ie = end(); it != ie; ++it) {
614 MCSectionData &SD = *it;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000615
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000616 for (MCSectionData::iterator it2 = SD.begin(),
617 ie2 = SD.end(); it2 != ie2; ++it2) {
618 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
619 if (!DF)
620 continue;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000621
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000622 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
623 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
624 MCAsmFixup &Fixup = *it3;
625
626 // Check whether we need to relax this fixup.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000627 if (!FixupNeedsRelaxation(Fixup, DF, Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000628 continue;
629
630 // Relax the instruction.
631 //
632 // FIXME: This is a huge temporary hack which just looks for x86
633 // branches; the only thing we need to relax on x86 is
634 // 'X86::reloc_pcrel_1byte'. Once we have MCInst fragments, this will be
635 // replaced by a TargetAsmBackend hook (most likely tblgen'd) to relax
636 // an individual MCInst.
637 SmallVectorImpl<char> &C = DF->getContents();
638 uint64_t PrevOffset = Fixup.Offset;
639 unsigned Amt = 0;
640
641 // jcc instructions
642 if (unsigned(C[Fixup.Offset-1]) >= 0x70 &&
643 unsigned(C[Fixup.Offset-1]) <= 0x7f) {
644 C[Fixup.Offset] = C[Fixup.Offset-1] + 0x10;
645 C[Fixup.Offset-1] = char(0x0f);
646 ++Fixup.Offset;
647 Amt = 4;
648
649 // jmp rel8
650 } else if (C[Fixup.Offset-1] == char(0xeb)) {
651 C[Fixup.Offset-1] = char(0xe9);
652 Amt = 3;
653
654 } else
655 llvm_unreachable("unknown 1 byte pcrel instruction!");
656
657 Fixup.Value = MCBinaryExpr::Create(
658 MCBinaryExpr::Sub, Fixup.Value,
659 MCConstantExpr::Create(3, getContext()),
660 getContext());
661 C.insert(C.begin() + Fixup.Offset, Amt, char(0));
662 Fixup.Kind = MCFixupKind(X86::reloc_pcrel_4byte);
663
664 // Update the remaining fixups, which have slid.
665 //
666 // FIXME: This is bad for performance, but will be eliminated by the
667 // move to MCInst specific fragments.
668 ++it3;
669 for (; it3 != ie3; ++it3)
670 it3->Offset += Amt;
671
672 // Update all the symbols for this fragment, which may have slid.
673 //
674 // FIXME: This is really really bad for performance, but will be
675 // eliminated by the move to MCInst specific fragments.
676 for (MCAssembler::symbol_iterator it = symbol_begin(),
677 ie = symbol_end(); it != ie; ++it) {
678 MCSymbolData &SD = *it;
679
680 if (it->getFragment() != DF)
681 continue;
682
683 if (SD.getOffset() > PrevOffset)
684 SD.setOffset(SD.getOffset() + Amt);
685 }
686
687 // Restart layout.
688 //
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000689 // FIXME-PERF: This is O(N^2), but will be eliminated once we have a
690 // smart MCAsmLayout object.
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000691 return true;
692 }
693 }
694 }
695
696 return false;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000697}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000698
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000699void MCAssembler::FinishLayout(MCAsmLayout &Layout) {
700 // Lower out any instruction fragments, to simplify the fixup application and
701 // output.
702 //
703 // FIXME-PERF: We don't have to do this, but the assumption is that it is
704 // cheap (we will mostly end up eliminating fragments and appending on to data
705 // fragments), so the extra complexity downstream isn't worth it. Evaluate
706 // this assumption.
707 for (iterator it = begin(), ie = end(); it != ie; ++it) {
708 MCSectionData &SD = *it;
709
710 for (MCSectionData::iterator it2 = SD.begin(),
711 ie2 = SD.end(); it2 != ie2; ++it2) {
712 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
713 if (!IF)
714 continue;
715
716 // Create a new data fragment for the instruction.
717 //
Daniel Dunbar337055e2010-03-23 03:13:05 +0000718 // FIXME-PERF: Reuse previous data fragment if possible.
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000719 MCDataFragment *DF = new MCDataFragment();
720 SD.getFragmentList().insert(it2, DF);
721
722 // Update the data fragments layout data.
Daniel Dunbar9799de92010-03-23 01:39:05 +0000723 DF->setParent(IF->getParent());
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000724 DF->setOffset(IF->getOffset());
725 DF->setFileSize(IF->getInstSize());
726
Daniel Dunbar9799de92010-03-23 01:39:05 +0000727 // Copy in the data and the fixups.
728 DF->getContents().append(IF->getCode().begin(), IF->getCode().end());
729 for (unsigned i = 0, e = IF->getFixups().size(); i != e; ++i)
730 DF->getFixups().push_back(IF->getFixups()[i]);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000731
732 // Delete the instruction fragment and update the iterator.
733 SD.getFragmentList().erase(IF);
734 it2 = DF;
735 }
736 }
737}
738
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000739// Debugging methods
740
741namespace llvm {
742
743raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) {
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000744 OS << "<MCAsmFixup" << " Offset:" << AF.Offset << " Value:" << *AF.Value
745 << " Kind:" << AF.Kind << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000746 return OS;
747}
748
749}
750
751void MCFragment::dump() {
752 raw_ostream &OS = llvm::errs();
753
754 OS << "<MCFragment " << (void*) this << " Offset:" << Offset
755 << " FileSize:" << FileSize;
756
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000757 OS << ">";
758}
759
760void MCAlignFragment::dump() {
761 raw_ostream &OS = llvm::errs();
762
763 OS << "<MCAlignFragment ";
764 this->MCFragment::dump();
765 OS << "\n ";
766 OS << " Alignment:" << getAlignment()
767 << " Value:" << getValue() << " ValueSize:" << getValueSize()
768 << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">";
769}
770
771void MCDataFragment::dump() {
772 raw_ostream &OS = llvm::errs();
773
774 OS << "<MCDataFragment ";
775 this->MCFragment::dump();
776 OS << "\n ";
777 OS << " Contents:[";
778 for (unsigned i = 0, e = getContents().size(); i != e; ++i) {
779 if (i) OS << ",";
780 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
781 }
Daniel Dunbar2be2fd02010-02-13 09:28:54 +0000782 OS << "] (" << getContents().size() << " bytes)";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000783
784 if (!getFixups().empty()) {
785 OS << ",\n ";
786 OS << " Fixups:[";
787 for (fixup_iterator it = fixup_begin(), ie = fixup_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000788 if (it != fixup_begin()) OS << ",\n ";
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000789 OS << *it;
790 }
791 OS << "]";
792 }
793
794 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000795}
796
797void MCFillFragment::dump() {
798 raw_ostream &OS = llvm::errs();
799
800 OS << "<MCFillFragment ";
801 this->MCFragment::dump();
802 OS << "\n ";
803 OS << " Value:" << getValue() << " ValueSize:" << getValueSize()
804 << " Count:" << getCount() << ">";
805}
806
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000807void MCInstFragment::dump() {
808 raw_ostream &OS = llvm::errs();
809
810 OS << "<MCInstFragment ";
811 this->MCFragment::dump();
812 OS << "\n ";
813 OS << " Inst:";
814 getInst().dump_pretty(OS);
815 OS << ">";
816}
817
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000818void MCOrgFragment::dump() {
819 raw_ostream &OS = llvm::errs();
820
821 OS << "<MCOrgFragment ";
822 this->MCFragment::dump();
823 OS << "\n ";
824 OS << " Offset:" << getOffset() << " Value:" << getValue() << ">";
825}
826
827void MCZeroFillFragment::dump() {
828 raw_ostream &OS = llvm::errs();
829
830 OS << "<MCZeroFillFragment ";
831 this->MCFragment::dump();
832 OS << "\n ";
833 OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">";
834}
835
836void MCSectionData::dump() {
837 raw_ostream &OS = llvm::errs();
838
839 OS << "<MCSectionData";
840 OS << " Alignment:" << getAlignment() << " Address:" << Address
841 << " Size:" << Size << " FileSize:" << FileSize
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000842 << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000843 for (iterator it = begin(), ie = end(); it != ie; ++it) {
844 if (it != begin()) OS << ",\n ";
845 it->dump();
846 }
847 OS << "]>";
848}
849
850void MCSymbolData::dump() {
851 raw_ostream &OS = llvm::errs();
852
853 OS << "<MCSymbolData Symbol:" << getSymbol()
854 << " Fragment:" << getFragment() << " Offset:" << getOffset()
855 << " Flags:" << getFlags() << " Index:" << getIndex();
856 if (isCommon())
857 OS << " (common, size:" << getCommonSize()
858 << " align: " << getCommonAlignment() << ")";
859 if (isExternal())
860 OS << " (external)";
861 if (isPrivateExtern())
862 OS << " (private extern)";
863 OS << ">";
864}
865
866void MCAssembler::dump() {
867 raw_ostream &OS = llvm::errs();
868
869 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000870 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000871 for (iterator it = begin(), ie = end(); it != ie; ++it) {
872 if (it != begin()) OS << ",\n ";
873 it->dump();
874 }
875 OS << "],\n";
876 OS << " Symbols:[";
877
878 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000879 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000880 it->dump();
881 }
882 OS << "]>\n";
883}