blob: c5bf6b9f875573235cce7afbd77215a36545a359 [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"
Rafael Espindolafea753b2010-12-24 21:22:02 +000014#include "llvm/MC/MCContext.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000016#include "llvm/MC/MCObjectWriter.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000017#include "llvm/MC/MCSection.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000018#include "llvm/MC/MCSymbol.h"
19#include "llvm/MC/MCValue.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000020#include "llvm/MC/MCDwarf.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000021#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar1a9158c2010-03-19 10:43:26 +000022#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar0adcd352009-08-25 21:10:45 +000023#include "llvm/ADT/Statistic.h"
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +000024#include "llvm/ADT/StringExtras.h"
Daniel Dunbard6f761e2009-08-21 23:07:38 +000025#include "llvm/ADT/Twine.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"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000028#include "llvm/Support/raw_ostream.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000029#include "llvm/Support/TargetRegistry.h"
Daniel Dunbarf6346762010-02-13 09:29:02 +000030
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000031using namespace llvm;
32
Daniel Dunbarff547842010-03-23 23:47:14 +000033namespace {
34namespace stats {
Daniel Dunbar0adcd352009-08-25 21:10:45 +000035STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
Jim Grosbachf77d5b12011-12-06 00:03:48 +000036STATISTIC(evaluateFixup, "Number of evaluated fixups");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000037STATISTIC(FragmentLayouts, "Number of fragment layouts");
Daniel Dunbarff547842010-03-23 23:47:14 +000038STATISTIC(ObjectBytes, "Number of emitted object file bytes");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000039STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
40STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
Daniel Dunbarff547842010-03-23 23:47:14 +000041}
42}
Daniel Dunbar0adcd352009-08-25 21:10:45 +000043
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000044// FIXME FIXME FIXME: There are number of places in this file where we convert
45// what is a 64-bit assembler value used for computation into a value in the
46// object file, which may truncate it. We should detect that truncation where
47// invalid and report errors back.
48
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000049/* *** */
50
Daniel Dunbar9005d452010-05-14 00:37:21 +000051MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
Rafael Espindola4f4363a2010-12-07 23:32:26 +000052 : Assembler(Asm), LastValidFragment()
Daniel Dunbar9005d452010-05-14 00:37:21 +000053 {
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000054 // Compute the section layout order. Virtual sections must go last.
55 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000056 if (!it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000057 SectionOrder.push_back(&*it);
58 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000059 if (it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000060 SectionOrder.push_back(&*it);
61}
62
Daniel Dunbar9005d452010-05-14 00:37:21 +000063bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000064 const MCSectionData &SD = *F->getParent();
65 const MCFragment *LastValid = LastValidFragment.lookup(&SD);
66 if (!LastValid)
67 return false;
68 assert(LastValid->getParent() == F->getParent());
69 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
Daniel Dunbar9005d452010-05-14 00:37:21 +000070}
71
Rafael Espindolaa9d42812010-11-23 08:08:33 +000072void MCAsmLayout::Invalidate(MCFragment *F) {
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000073 // If this fragment wasn't already up-to-date, we don't need to do anything.
74 if (!isFragmentUpToDate(F))
75 return;
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000076
Rafael Espindola2bf6afc2010-12-15 08:45:53 +000077 // Otherwise, reset the last valid fragment to this fragment.
Rafael Espindola4f4363a2010-12-07 23:32:26 +000078 const MCSectionData &SD = *F->getParent();
Rafael Espindola2bf6afc2010-12-15 08:45:53 +000079 LastValidFragment[&SD] = F;
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000080}
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000081
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000082void MCAsmLayout::EnsureValid(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000083 MCSectionData &SD = *F->getParent();
84
85 MCFragment *Cur = LastValidFragment[&SD];
86 if (!Cur)
87 Cur = &*SD.begin();
88 else
89 Cur = Cur->getNextNode();
90
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000091 // Advance the layout position until the fragment is up-to-date.
92 while (!isFragmentUpToDate(F)) {
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000093 const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur);
Rafael Espindola4f4363a2010-12-07 23:32:26 +000094 Cur = Cur->getNextNode();
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000095 }
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000096}
97
Daniel Dunbar432cd5f2010-03-25 02:00:02 +000098uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000099 EnsureValid(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000100 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
101 return F->Offset;
102}
103
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000104uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000105 const MCSymbol &S = SD->getSymbol();
106
107 // If this is a variable, then recursively evaluate now.
108 if (S.isVariable()) {
109 MCValue Target;
110 if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this))
111 report_fatal_error("unable to evaluate offset for variable '" +
112 S.getName() + "'");
113
114 // Verify that any used symbols are defined.
115 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
116 report_fatal_error("unable to evaluate offset to undefined symbol '" +
117 Target.getSymA()->getSymbol().getName() + "'");
118 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
119 report_fatal_error("unable to evaluate offset to undefined symbol '" +
120 Target.getSymB()->getSymbol().getName() + "'");
Jim Grosbach684457d2011-10-26 22:44:41 +0000121
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000122 uint64_t Offset = Target.getConstant();
123 if (Target.getSymA())
124 Offset += getSymbolOffset(&Assembler.getSymbolData(
125 Target.getSymA()->getSymbol()));
126 if (Target.getSymB())
127 Offset -= getSymbolOffset(&Assembler.getSymbolData(
128 Target.getSymB()->getSymbol()));
129 return Offset;
130 }
131
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000132 assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
133 return getFragmentOffset(SD->getFragment()) + SD->getOffset();
134}
135
Daniel Dunbar2661f112010-05-13 03:19:50 +0000136uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
Daniel Dunbarafc6acd2010-05-14 00:37:11 +0000137 // The size is the last fragment's end offset.
Daniel Dunbar2661f112010-05-13 03:19:50 +0000138 const MCFragment &F = SD->getFragmentList().back();
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000139 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000140}
141
142uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
Daniel Dunbar2661f112010-05-13 03:19:50 +0000143 // Virtual sections have no file size.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000144 if (SD->getSection().isVirtualSection())
Daniel Dunbar2661f112010-05-13 03:19:50 +0000145 return 0;
146
147 // Otherwise, the file size is the same as the address space size.
148 return getSectionAddressSize(SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000149}
150
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000151/* *** */
152
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000153MCFragment::MCFragment() : Kind(FragmentType(~0)) {
154}
155
Daniel Dunbar36880e72010-07-28 20:28:45 +0000156MCFragment::~MCFragment() {
157}
158
Daniel Dunbar5e835962009-08-26 02:48:04 +0000159MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Rafael Espindola2bf6afc2010-12-15 08:45:53 +0000160 : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000161{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000162 if (Parent)
163 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000164}
165
166/* *** */
167
Daniel Dunbar81e40002009-08-27 00:38:04 +0000168MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000169
170MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000171 : Section(&_Section),
Rafael Espindolaf8803fe2010-12-06 03:48:09 +0000172 Ordinal(~UINT32_C(0)),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000173 Alignment(1),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000174 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000175{
176 if (A)
177 A->getSectionList().push_back(this);
178}
179
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000180/* *** */
181
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000182MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000183
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000184MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000185 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000186 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000187 IsExternal(false), IsPrivateExtern(false),
Matt Fleming6c8b3d22010-08-16 18:34:31 +0000188 CommonSize(0), SymbolSize(0), CommonAlign(0),
189 Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000190{
191 if (A)
192 A->getSymbolList().push_back(this);
193}
194
195/* *** */
196
Evan Cheng78c10ee2011-07-25 23:24:55 +0000197MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000198 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
199 raw_ostream &OS_)
200 : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000201 OS(OS_), RelaxAll(false), NoExecStack(false), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +0000202{
203}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000204
205MCAssembler::~MCAssembler() {
206}
207
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000208bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
Daniel Dunbar23869852010-03-19 03:18:09 +0000209 // Non-temporary labels should always be visible to the linker.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000210 if (!Symbol.isTemporary())
Daniel Dunbar23869852010-03-19 03:18:09 +0000211 return true;
212
213 // Absolute temporary labels are never visible.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000214 if (!Symbol.isInSection())
Daniel Dunbar23869852010-03-19 03:18:09 +0000215 return false;
216
217 // Otherwise, check if the section requires symbols even for temporary labels.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000218 return getBackend().doesSectionRequireSymbols(Symbol.getSection());
Daniel Dunbar23869852010-03-19 03:18:09 +0000219}
220
Rafael Espindolab8141102010-09-27 18:13:03 +0000221const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000222 // Linker visible symbols define atoms.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000223 if (isSymbolLinkerVisible(SD->getSymbol()))
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000224 return SD;
225
226 // Absolute and undefined symbols have no defining atom.
227 if (!SD->getFragment())
228 return 0;
229
Daniel Dunbara5f1d572010-05-12 00:38:17 +0000230 // Non-linker visible symbols in sections which can't be atomized have no
231 // defining atom.
232 if (!getBackend().isSectionAtomizable(
233 SD->getFragment()->getParent()->getSection()))
234 return 0;
235
Daniel Dunbar651804c2010-05-11 17:22:50 +0000236 // Otherwise, return the atom for the containing fragment.
237 return SD->getFragment()->getAtom();
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000238}
239
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000240bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000241 const MCFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000242 MCValue &Target, uint64_t &Value) const {
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000243 ++stats::evaluateFixup;
Daniel Dunbarff547842010-03-23 23:47:14 +0000244
Rafael Espindola33a38a12010-12-22 16:11:57 +0000245 if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout))
Chris Lattner75361b62010-04-07 22:58:41 +0000246 report_fatal_error("expected relocatable expression");
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000247
Rafael Espindolafea753b2010-12-24 21:22:02 +0000248 bool IsPCRel = Backend.getFixupKindInfo(
249 Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
250
251 bool IsResolved;
252 if (IsPCRel) {
253 if (Target.getSymB()) {
254 IsResolved = false;
255 } else if (!Target.getSymA()) {
256 IsResolved = false;
257 } else {
Rafael Espindola908159b2011-02-16 03:25:55 +0000258 const MCSymbolRefExpr *A = Target.getSymA();
259 const MCSymbol &SA = A->getSymbol();
260 if (A->getKind() != MCSymbolRefExpr::VK_None ||
261 SA.AliasedSymbol().isUndefined()) {
Rafael Espindolafea753b2010-12-24 21:22:02 +0000262 IsResolved = false;
263 } else {
264 const MCSymbolData &DataA = getSymbolData(SA);
265 IsResolved =
266 getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
267 *DF, false, true);
268 }
269 }
270 } else {
271 IsResolved = Target.isAbsolute();
272 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000273
274 Value = Target.getConstant();
275
Jim Grosbach62006112011-11-29 01:15:25 +0000276 bool IsThumb = false;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000277 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000278 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
279 if (Sym.isDefined())
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000280 Value += Layout.getSymbolOffset(&getSymbolData(Sym));
Jim Grosbach62006112011-11-29 01:15:25 +0000281 if (isThumbFunc(&Sym))
282 IsThumb = true;
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000283 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000284 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000285 const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
286 if (Sym.isDefined())
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000287 Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000288 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000289
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000290
Daniel Dunbar2761fc42010-12-16 03:20:06 +0000291 bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
Owen Anderson47dbd422010-12-15 18:48:27 +0000292 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
293 assert((ShouldAlignPC ? IsPCRel : true) &&
294 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
295
Owen Anderson05018c22010-12-09 20:27:52 +0000296 if (IsPCRel) {
Owen Anderson175fb362010-12-17 21:49:48 +0000297 uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
Jim Grosbach684457d2011-10-26 22:44:41 +0000298
Owen Anderson47dbd422010-12-15 18:48:27 +0000299 // A number of ARM fixups in Thumb mode require that the effective PC
300 // address be determined as the 32-bit aligned version of the actual offset.
Owen Andersond18e0112010-12-15 19:24:24 +0000301 if (ShouldAlignPC) Offset &= ~0x3;
Owen Anderson175fb362010-12-17 21:49:48 +0000302 Value -= Offset;
Owen Anderson05018c22010-12-09 20:27:52 +0000303 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000304
Jim Grosbach62006112011-11-29 01:15:25 +0000305 // ARM fixups based from a thumb function address need to have the low
306 // bit set. The actual value is always at least 16-bit aligned, so the
307 // low bit is normally clear and available for use as an ISA flag for
308 // interworking.
309 if (IsThumb)
Jim Grosbach47500202010-12-14 18:46:57 +0000310 Value |= 1;
311
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000312 return IsResolved;
313}
314
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000315uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
Rafael Espindola7a459032010-12-21 20:35:18 +0000316 const MCFragment &F) const {
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000317 switch (F.getKind()) {
318 case MCFragment::FT_Data:
319 return cast<MCDataFragment>(F).getContents().size();
320 case MCFragment::FT_Fill:
321 return cast<MCFillFragment>(F).getSize();
322 case MCFragment::FT_Inst:
323 return cast<MCInstFragment>(F).getInstSize();
324
Rafael Espindola3ff57092010-11-02 17:22:24 +0000325 case MCFragment::FT_LEB:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000326 return cast<MCLEBFragment>(F).getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000327
Rafael Espindola7a459032010-12-21 20:35:18 +0000328 case MCFragment::FT_Align: {
329 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
330 unsigned Offset = Layout.getFragmentOffset(&AF);
331 unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
332 if (Size > AF.getMaxBytesToEmit())
333 return 0;
334 return Size;
335 }
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000336
Rafael Espindola7a459032010-12-21 20:35:18 +0000337 case MCFragment::FT_Org: {
338 MCOrgFragment &OF = cast<MCOrgFragment>(F);
339 int64_t TargetLocation;
340 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
341 report_fatal_error("expected assembly-time absolute expression");
342
343 // FIXME: We need a way to communicate this error.
344 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
345 int64_t Size = TargetLocation - FragmentOffset;
346 if (Size < 0 || Size >= 0x40000000)
347 report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
348 "' (at offset '" + Twine(FragmentOffset) + "')");
349 return Size;
350 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000351
Rafael Espindola187d8332010-11-07 02:07:12 +0000352 case MCFragment::FT_Dwarf:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000353 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
Rafael Espindola245a1e22010-12-28 05:39:27 +0000354 case MCFragment::FT_DwarfFrame:
355 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000356 }
357
358 assert(0 && "invalid fragment kind");
359 return 0;
360}
361
Daniel Dunbarb69fc042010-05-13 20:40:12 +0000362void MCAsmLayout::LayoutFragment(MCFragment *F) {
Daniel Dunbar9005d452010-05-14 00:37:21 +0000363 MCFragment *Prev = F->getPrevNode();
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000364
Daniel Dunbar9005d452010-05-14 00:37:21 +0000365 // We should never try to recompute something which is up-to-date.
366 assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!");
Daniel Dunbar9005d452010-05-14 00:37:21 +0000367 // We should never try to compute the fragment layout if it's predecessor
368 // isn't up-to-date.
369 assert((!Prev || isFragmentUpToDate(Prev)) &&
370 "Attempt to compute fragment before it's predecessor!");
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000371
372 ++stats::FragmentLayouts;
373
Daniel Dunbarb69fc042010-05-13 20:40:12 +0000374 // Compute fragment offset and size.
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000375 uint64_t Offset = 0;
376 if (Prev)
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000377 Offset += Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000378
379 F->Offset = Offset;
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000380 LastValidFragment[F->getParent()] = F;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000381}
382
Daniel Dunbar53b23382010-03-19 09:28:59 +0000383/// WriteFragmentData - Write the \arg F data to the output file.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000384static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000385 const MCFragment &F) {
386 MCObjectWriter *OW = &Asm.getWriter();
Daniel Dunbar53b23382010-03-19 09:28:59 +0000387 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000388 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000389
Daniel Dunbarff547842010-03-23 23:47:14 +0000390 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000391
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000392 // FIXME: Embed in fragments instead?
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000393 uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000394 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000395 case MCFragment::FT_Align: {
396 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000397 uint64_t Count = FragmentSize / AF.getValueSize();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000398
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000399 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
400
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000401 // FIXME: This error shouldn't actually occur (the front end should emit
402 // multiple .align directives to enforce the semantics it wants), but is
403 // severe enough that we want to report it. How to handle this?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000404 if (Count * AF.getValueSize() != FragmentSize)
Chris Lattner75361b62010-04-07 22:58:41 +0000405 report_fatal_error("undefined .align directive, value size '" +
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000406 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000407 "' is not a divisor of padding size '" +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000408 Twine(FragmentSize) + "'");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000409
Kevin Enderby6e720482010-02-23 18:26:34 +0000410 // See if we are aligning with nops, and if so do that first to try to fill
411 // the Count bytes. Then if that did not fill any bytes or there are any
412 // bytes left to fill use the the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000413 // If we are aligning with nops, ask that target to emit the right data.
Daniel Dunbar1c154132010-05-12 22:56:23 +0000414 if (AF.hasEmitNops()) {
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000415 if (!Asm.getBackend().WriteNopData(Count, OW))
Chris Lattner75361b62010-04-07 22:58:41 +0000416 report_fatal_error("unable to write nop sequence of " +
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000417 Twine(Count) + " bytes");
418 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000419 }
420
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000421 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000422 for (uint64_t i = 0; i != Count; ++i) {
423 switch (AF.getValueSize()) {
424 default:
425 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000426 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
427 case 2: OW->Write16(uint16_t(AF.getValue())); break;
428 case 4: OW->Write32(uint32_t(AF.getValue())); break;
429 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000430 }
431 }
432 break;
433 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000434
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000435 case MCFragment::FT_Data: {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000436 MCDataFragment &DF = cast<MCDataFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000437 assert(FragmentSize == DF.getContents().size() && "Invalid size!");
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000438 OW->WriteBytes(DF.getContents().str());
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000439 break;
Daniel Dunbar3a30b822010-02-13 09:28:15 +0000440 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000441
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000442 case MCFragment::FT_Fill: {
443 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000444
445 assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
446
Daniel Dunbar3153fec2010-05-12 22:51:32 +0000447 for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000448 switch (FF.getValueSize()) {
449 default:
450 assert(0 && "Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000451 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
452 case 2: OW->Write16(uint16_t(FF.getValue())); break;
453 case 4: OW->Write32(uint32_t(FF.getValue())); break;
454 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000455 }
456 }
457 break;
458 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000459
Rafael Espindola179821a2010-12-06 19:08:48 +0000460 case MCFragment::FT_Inst: {
461 MCInstFragment &IF = cast<MCInstFragment>(F);
462 OW->WriteBytes(StringRef(IF.getCode().begin(), IF.getCode().size()));
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000463 break;
Rafael Espindola179821a2010-12-06 19:08:48 +0000464 }
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000465
Rafael Espindola3ff57092010-11-02 17:22:24 +0000466 case MCFragment::FT_LEB: {
467 MCLEBFragment &LF = cast<MCLEBFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000468 OW->WriteBytes(LF.getContents().str());
Rafael Espindola3ff57092010-11-02 17:22:24 +0000469 break;
470 }
471
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000472 case MCFragment::FT_Org: {
473 MCOrgFragment &OF = cast<MCOrgFragment>(F);
474
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000475 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000476 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000477
478 break;
479 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000480
481 case MCFragment::FT_Dwarf: {
482 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000483 OW->WriteBytes(OF.getContents().str());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000484 break;
485 }
Rafael Espindola245a1e22010-12-28 05:39:27 +0000486 case MCFragment::FT_DwarfFrame: {
487 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
488 OW->WriteBytes(CF.getContents().str());
489 break;
490 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000491 }
492
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000493 assert(OW->getStream().tell() - Start == FragmentSize);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000494}
495
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000496void MCAssembler::writeSectionData(const MCSectionData *SD,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000497 const MCAsmLayout &Layout) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000498 // Ignore virtual sections.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000499 if (SD->getSection().isVirtualSection()) {
Daniel Dunbar054be922010-05-13 03:50:50 +0000500 assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000501
502 // Check that contents are only things legal inside a virtual section.
503 for (MCSectionData::const_iterator it = SD->begin(),
504 ie = SD->end(); it != ie; ++it) {
505 switch (it->getKind()) {
506 default:
507 assert(0 && "Invalid fragment in virtual section!");
Daniel Dunbarc983b202010-08-18 18:22:37 +0000508 case MCFragment::FT_Data: {
509 // Check that we aren't trying to write a non-zero contents (or fixups)
510 // into a virtual section. This is to support clients which use standard
511 // directives to fill the contents of virtual sections.
512 MCDataFragment &DF = cast<MCDataFragment>(*it);
513 assert(DF.fixup_begin() == DF.fixup_end() &&
514 "Cannot have fixups in virtual section!");
515 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
516 assert(DF.getContents()[i] == 0 &&
517 "Invalid data value for virtual section!");
518 break;
519 }
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000520 case MCFragment::FT_Align:
Daniel Dunbarc983b202010-08-18 18:22:37 +0000521 // Check that we aren't trying to write a non-zero value into a virtual
522 // section.
523 assert((!cast<MCAlignFragment>(it)->getValueSize() ||
524 !cast<MCAlignFragment>(it)->getValue()) &&
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000525 "Invalid align in virtual section!");
526 break;
527 case MCFragment::FT_Fill:
528 assert(!cast<MCFillFragment>(it)->getValueSize() &&
529 "Invalid fill in virtual section!");
530 break;
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000531 }
532 }
533
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000534 return;
535 }
536
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000537 uint64_t Start = getWriter().getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000538 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000539
Daniel Dunbar53b23382010-03-19 09:28:59 +0000540 for (MCSectionData::const_iterator it = SD->begin(),
541 ie = SD->end(); it != ie; ++it)
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000542 WriteFragmentData(*this, Layout, *it);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000543
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000544 assert(getWriter().getStream().tell() - Start ==
545 Layout.getSectionAddressSize(SD));
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000546}
547
Rafael Espindola179821a2010-12-06 19:08:48 +0000548
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000549uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000550 MCFragment &F,
551 const MCFixup &Fixup) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000552 // Evaluate the fixup.
553 MCValue Target;
554 uint64_t FixedValue;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000555 if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000556 // The fixup was unresolved, we need a relocation. Inform the object
557 // writer of the relocation, and give it an opportunity to adjust the
558 // fixup value if need be.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000559 getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
Rafael Espindola179821a2010-12-06 19:08:48 +0000560 }
561 return FixedValue;
562 }
563
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000564void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000565 DEBUG_WITH_TYPE("mc-dump", {
566 llvm::errs() << "assembler backend - pre-layout\n--\n";
567 dump(); });
568
Daniel Dunbar61066db2010-05-13 02:34:14 +0000569 // Create the layout object.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000570 MCAsmLayout Layout(*this);
Daniel Dunbar61066db2010-05-13 02:34:14 +0000571
Daniel Dunbar337718e2010-05-14 00:37:14 +0000572 // Create dummy fragments and assign section ordinals.
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000573 unsigned SectionIndex = 0;
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000574 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
575 // Create dummy fragments to eliminate any empty sections, this simplifies
576 // layout.
Duncan Sands6f74f692010-06-29 13:30:08 +0000577 if (it->getFragmentList().empty())
Rafael Espindolad80781b2010-09-15 21:48:40 +0000578 new MCDataFragment(it);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000579
580 it->setOrdinal(SectionIndex++);
Daniel Dunbar337718e2010-05-14 00:37:14 +0000581 }
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000582
Daniel Dunbar337718e2010-05-14 00:37:14 +0000583 // Assign layout order indices to sections and fragments.
Daniel Dunbar337718e2010-05-14 00:37:14 +0000584 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
585 MCSectionData *SD = Layout.getSectionOrder()[i];
586 SD->setLayoutOrder(i);
587
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000588 unsigned FragmentIndex = 0;
Daniel Dunbar337718e2010-05-14 00:37:14 +0000589 for (MCSectionData::iterator it2 = SD->begin(),
590 ie2 = SD->end(); it2 != ie2; ++it2)
591 it2->setLayoutOrder(FragmentIndex++);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000592 }
593
Daniel Dunbar61066db2010-05-13 02:34:14 +0000594 // Layout until everything fits.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000595 while (layoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000596 continue;
597
598 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000599 llvm::errs() << "assembler backend - post-relaxation\n--\n";
600 dump(); });
601
602 // Finalize the layout, including fragment lowering.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000603 finishLayout(Layout);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000604
605 DEBUG_WITH_TYPE("mc-dump", {
606 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000607 dump(); });
608
Daniel Dunbarff547842010-03-23 23:47:14 +0000609 uint64_t StartOffset = OS.tell();
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000610
Daniel Dunbarbacba992010-03-19 07:09:33 +0000611 // Allow the object writer a chance to perform post-layout binding (for
612 // example, to set the index fields in the symbol data).
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000613 getWriter().ExecutePostLayoutBinding(*this, Layout);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000614
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000615 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000616 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
617 for (MCSectionData::iterator it2 = it->begin(),
618 ie2 = it->end(); it2 != ie2; ++it2) {
619 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
Rafael Espindola179821a2010-12-06 19:08:48 +0000620 if (DF) {
621 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
622 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
623 MCFixup &Fixup = *it3;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000624 uint64_t FixedValue = handleFixup(Layout, *DF, Fixup);
Rafael Espindola179821a2010-12-06 19:08:48 +0000625 getBackend().ApplyFixup(Fixup, DF->getContents().data(),
626 DF->getContents().size(), FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000627 }
Rafael Espindola179821a2010-12-06 19:08:48 +0000628 }
629 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
630 if (IF) {
631 for (MCInstFragment::fixup_iterator it3 = IF->fixup_begin(),
632 ie3 = IF->fixup_end(); it3 != ie3; ++it3) {
633 MCFixup &Fixup = *it3;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000634 uint64_t FixedValue = handleFixup(Layout, *IF, Fixup);
Rafael Espindola179821a2010-12-06 19:08:48 +0000635 getBackend().ApplyFixup(Fixup, IF->getCode().data(),
636 IF->getCode().size(), FixedValue);
637 }
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000638 }
639 }
640 }
641
Daniel Dunbarbacba992010-03-19 07:09:33 +0000642 // Write the object file.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000643 getWriter().WriteObject(*this, Layout);
Daniel Dunbarff547842010-03-23 23:47:14 +0000644
645 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000646}
647
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000648bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
Jim Grosbach370b78d2011-12-06 00:47:03 +0000649 const MCInstFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000650 const MCAsmLayout &Layout) const {
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000651 if (getRelaxAll())
652 return true;
653
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000654 // If we cannot resolve the fixup value, it requires relaxation.
655 MCValue Target;
656 uint64_t Value;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000657 if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000658 return true;
659
Jim Grosbach370b78d2011-12-06 00:47:03 +0000660 return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000661}
662
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000663bool MCAssembler::fragmentNeedsRelaxation(const MCInstFragment *IF,
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000664 const MCAsmLayout &Layout) const {
665 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
666 // are intentionally pushing out inst fragments, or because we relaxed a
667 // previous instruction to one that doesn't need relaxation.
Daniel Dunbar84882522010-05-26 17:45:29 +0000668 if (!getBackend().MayNeedRelaxation(IF->getInst()))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000669 return false;
670
671 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
672 ie = IF->fixup_end(); it != ie; ++it)
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000673 if (fixupNeedsRelaxation(*it, IF, Layout))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000674 return true;
675
676 return false;
677}
678
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000679bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
Rafael Espindola3ff57092010-11-02 17:22:24 +0000680 MCInstFragment &IF) {
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000681 if (!fragmentNeedsRelaxation(&IF, Layout))
Rafael Espindola3ff57092010-11-02 17:22:24 +0000682 return false;
683
684 ++stats::RelaxedInstructions;
685
686 // FIXME-PERF: We could immediately lower out instructions if we can tell
687 // they are fully resolved, to avoid retesting on later passes.
688
689 // Relax the fragment.
690
691 MCInst Relaxed;
692 getBackend().RelaxInstruction(IF.getInst(), Relaxed);
693
694 // Encode the new instruction.
695 //
696 // FIXME-PERF: If it matters, we could let the target do this. It can
697 // probably do so more efficiently in many cases.
698 SmallVector<MCFixup, 4> Fixups;
699 SmallString<256> Code;
700 raw_svector_ostream VecOS(Code);
701 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
702 VecOS.flush();
703
704 // Update the instruction fragment.
Rafael Espindola3ff57092010-11-02 17:22:24 +0000705 IF.setInst(Relaxed);
706 IF.getCode() = Code;
707 IF.getFixups().clear();
708 // FIXME: Eliminate copy.
709 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
710 IF.getFixups().push_back(Fixups[i]);
711
Rafael Espindola3ff57092010-11-02 17:22:24 +0000712 return true;
713}
714
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000715bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000716 int64_t Value = 0;
717 uint64_t OldSize = LF.getContents().size();
Rafael Espindolad88cac02011-04-26 02:17:58 +0000718 bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout);
719 (void)IsAbs;
720 assert(IsAbs);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000721 SmallString<8> &Data = LF.getContents();
722 Data.clear();
723 raw_svector_ostream OSE(Data);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000724 if (LF.isSigned())
725 MCObjectWriter::EncodeSLEB128(Value, OSE);
726 else
727 MCObjectWriter::EncodeULEB128(Value, OSE);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000728 OSE.flush();
729 return OldSize != LF.getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000730}
731
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000732bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000733 MCDwarfLineAddrFragment &DF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000734 int64_t AddrDelta = 0;
735 uint64_t OldSize = DF.getContents().size();
Rafael Espindola835439a2010-12-22 22:04:28 +0000736 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
737 (void)IsAbs;
738 assert(IsAbs);
Rafael Espindola187d8332010-11-07 02:07:12 +0000739 int64_t LineDelta;
740 LineDelta = DF.getLineDelta();
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000741 SmallString<8> &Data = DF.getContents();
742 Data.clear();
743 raw_svector_ostream OSE(Data);
744 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE);
745 OSE.flush();
746 return OldSize != Data.size();
Rafael Espindola187d8332010-11-07 02:07:12 +0000747}
748
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000749bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
Rafael Espindola245a1e22010-12-28 05:39:27 +0000750 MCDwarfCallFrameFragment &DF) {
751 int64_t AddrDelta = 0;
752 uint64_t OldSize = DF.getContents().size();
753 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
754 (void)IsAbs;
755 assert(IsAbs);
756 SmallString<8> &Data = DF.getContents();
757 Data.clear();
758 raw_svector_ostream OSE(Data);
Rafael Espindola4eafe102011-05-08 14:35:21 +0000759 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE);
Rafael Espindola245a1e22010-12-28 05:39:27 +0000760 OSE.flush();
761 return OldSize != Data.size();
762}
763
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000764bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout,
Rafael Espindola62b83b62010-12-21 04:22:09 +0000765 MCSectionData &SD) {
766 MCFragment *FirstInvalidFragment = NULL;
767 // Scan for fragments that need relaxation.
768 for (MCSectionData::iterator it2 = SD.begin(),
769 ie2 = SD.end(); it2 != ie2; ++it2) {
770 // Check if this is an fragment that needs relaxation.
771 bool relaxedFrag = false;
772 switch(it2->getKind()) {
773 default:
774 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000775 case MCFragment::FT_Inst:
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000776 relaxedFrag = relaxInstruction(Layout, *cast<MCInstFragment>(it2));
Rafael Espindola62b83b62010-12-21 04:22:09 +0000777 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000778 case MCFragment::FT_Dwarf:
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000779 relaxedFrag = relaxDwarfLineAddr(Layout,
Rafael Espindola62b83b62010-12-21 04:22:09 +0000780 *cast<MCDwarfLineAddrFragment>(it2));
781 break;
Rafael Espindola245a1e22010-12-28 05:39:27 +0000782 case MCFragment::FT_DwarfFrame:
783 relaxedFrag =
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000784 relaxDwarfCallFrameFragment(Layout,
Rafael Espindola245a1e22010-12-28 05:39:27 +0000785 *cast<MCDwarfCallFrameFragment>(it2));
786 break;
787 case MCFragment::FT_LEB:
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000788 relaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(it2));
Rafael Espindola245a1e22010-12-28 05:39:27 +0000789 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000790 }
791 // Update the layout, and remember that we relaxed.
792 if (relaxedFrag && !FirstInvalidFragment)
793 FirstInvalidFragment = it2;
794 }
795 if (FirstInvalidFragment) {
796 Layout.Invalidate(FirstInvalidFragment);
797 return true;
798 }
799 return false;
800}
801
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000802bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000803 ++stats::RelaxationSteps;
804
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000805 bool WasRelaxed = false;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000806 for (iterator it = begin(), ie = end(); it != ie; ++it) {
807 MCSectionData &SD = *it;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000808 while(layoutSectionOnce(Layout, SD))
Rafael Espindola62b83b62010-12-21 04:22:09 +0000809 WasRelaxed = true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000810 }
811
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000812 return WasRelaxed;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000813}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000814
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000815void MCAssembler::finishLayout(MCAsmLayout &Layout) {
Rafael Espindolab4172fa2010-12-04 22:47:22 +0000816 // The layout is done. Mark every fragment as valid.
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000817 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
818 Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
819 }
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000820}
821
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000822// Debugging methods
823
824namespace llvm {
825
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000826raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
827 OS << "<MCFixup" << " Offset:" << AF.getOffset()
Daniel Dunbar482ad802010-05-26 15:18:31 +0000828 << " Value:" << *AF.getValue()
829 << " Kind:" << AF.getKind() << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000830 return OS;
831}
832
833}
834
835void MCFragment::dump() {
836 raw_ostream &OS = llvm::errs();
837
Daniel Dunbare614e392010-05-26 06:50:57 +0000838 OS << "<";
839 switch (getKind()) {
840 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
841 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
842 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
843 case MCFragment::FT_Inst: OS << "MCInstFragment"; break;
844 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000845 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
Rafael Espindola245a1e22010-12-28 05:39:27 +0000846 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000847 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
Daniel Dunbare614e392010-05-26 06:50:57 +0000848 }
849
Daniel Dunbar337718e2010-05-14 00:37:14 +0000850 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
Rafael Espindola2bf6afc2010-12-15 08:45:53 +0000851 << " Offset:" << Offset << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000852
Daniel Dunbare614e392010-05-26 06:50:57 +0000853 switch (getKind()) {
854 case MCFragment::FT_Align: {
855 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
856 if (AF->hasEmitNops())
857 OS << " (emit nops)";
Daniel Dunbare614e392010-05-26 06:50:57 +0000858 OS << "\n ";
859 OS << " Alignment:" << AF->getAlignment()
860 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
861 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
862 break;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000863 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000864 case MCFragment::FT_Data: {
865 const MCDataFragment *DF = cast<MCDataFragment>(this);
866 OS << "\n ";
867 OS << " Contents:[";
868 const SmallVectorImpl<char> &Contents = DF->getContents();
869 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
870 if (i) OS << ",";
871 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000872 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000873 OS << "] (" << Contents.size() << " bytes)";
874
875 if (!DF->getFixups().empty()) {
876 OS << ",\n ";
877 OS << " Fixups:[";
878 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
879 ie = DF->fixup_end(); it != ie; ++it) {
880 if (it != DF->fixup_begin()) OS << ",\n ";
881 OS << *it;
882 }
883 OS << "]";
884 }
885 break;
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000886 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000887 case MCFragment::FT_Fill: {
888 const MCFillFragment *FF = cast<MCFillFragment>(this);
889 OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
890 << " Size:" << FF->getSize();
891 break;
892 }
893 case MCFragment::FT_Inst: {
894 const MCInstFragment *IF = cast<MCInstFragment>(this);
895 OS << "\n ";
896 OS << " Inst:";
897 IF->getInst().dump_pretty(OS);
898 break;
899 }
900 case MCFragment::FT_Org: {
901 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
902 OS << "\n ";
903 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
904 break;
905 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000906 case MCFragment::FT_Dwarf: {
907 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
908 OS << "\n ";
909 OS << " AddrDelta:" << OF->getAddrDelta()
910 << " LineDelta:" << OF->getLineDelta();
911 break;
912 }
Rafael Espindola245a1e22010-12-28 05:39:27 +0000913 case MCFragment::FT_DwarfFrame: {
914 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
915 OS << "\n ";
916 OS << " AddrDelta:" << CF->getAddrDelta();
917 break;
918 }
Rafael Espindola3ff57092010-11-02 17:22:24 +0000919 case MCFragment::FT_LEB: {
920 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
921 OS << "\n ";
922 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
923 break;
924 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000925 }
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000926 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000927}
928
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000929void MCSectionData::dump() {
930 raw_ostream &OS = llvm::errs();
931
932 OS << "<MCSectionData";
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000933 OS << " Alignment:" << getAlignment() << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000934 for (iterator it = begin(), ie = end(); it != ie; ++it) {
935 if (it != begin()) OS << ",\n ";
936 it->dump();
937 }
938 OS << "]>";
939}
940
941void MCSymbolData::dump() {
942 raw_ostream &OS = llvm::errs();
943
944 OS << "<MCSymbolData Symbol:" << getSymbol()
945 << " Fragment:" << getFragment() << " Offset:" << getOffset()
946 << " Flags:" << getFlags() << " Index:" << getIndex();
947 if (isCommon())
948 OS << " (common, size:" << getCommonSize()
949 << " align: " << getCommonAlignment() << ")";
950 if (isExternal())
951 OS << " (external)";
952 if (isPrivateExtern())
953 OS << " (private extern)";
954 OS << ">";
955}
956
957void MCAssembler::dump() {
958 raw_ostream &OS = llvm::errs();
959
960 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000961 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000962 for (iterator it = begin(), ie = end(); it != ie; ++it) {
963 if (it != begin()) OS << ",\n ";
964 it->dump();
965 }
966 OS << "],\n";
967 OS << " Symbols:[";
968
969 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000970 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000971 it->dump();
972 }
973 OS << "]>\n";
974}