blob: c872bd62c9abe9e7d8e75ee9a78a1b35f2d96366 [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"
Craig Topperf1d0f772012-03-26 06:58:25 +000016#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000017#include "llvm/MC/MCObjectWriter.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000018#include "llvm/MC/MCSection.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000019#include "llvm/MC/MCSymbol.h"
20#include "llvm/MC/MCValue.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000021#include "llvm/MC/MCDwarf.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000022#include "llvm/MC/MCAsmBackend.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"
Jim Grosbach2d39a0e2012-08-08 23:56:06 +000030#include "llvm/Support/LEB128.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 {
Daniel Dunbar0adcd352009-08-25 21:10:45 +000036STATISTIC(EmittedFragments, "Number of emitted assembler fragments");
Jim Grosbachf77d5b12011-12-06 00:03:48 +000037STATISTIC(evaluateFixup, "Number of evaluated fixups");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000038STATISTIC(FragmentLayouts, "Number of fragment layouts");
Daniel Dunbarff547842010-03-23 23:47:14 +000039STATISTIC(ObjectBytes, "Number of emitted object file bytes");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000040STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
41STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
Daniel Dunbarff547842010-03-23 23:47:14 +000042}
43}
Daniel Dunbar0adcd352009-08-25 21:10:45 +000044
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000045// FIXME FIXME FIXME: There are number of places in this file where we convert
46// what is a 64-bit assembler value used for computation into a value in the
47// object file, which may truncate it. We should detect that truncation where
48// invalid and report errors back.
49
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000050/* *** */
51
Daniel Dunbar9005d452010-05-14 00:37:21 +000052MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
Rafael Espindola4f4363a2010-12-07 23:32:26 +000053 : Assembler(Asm), LastValidFragment()
Daniel Dunbar9005d452010-05-14 00:37:21 +000054 {
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000055 // Compute the section layout order. Virtual sections must go last.
56 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000057 if (!it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000058 SectionOrder.push_back(&*it);
59 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000060 if (it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000061 SectionOrder.push_back(&*it);
62}
63
Daniel Dunbar9005d452010-05-14 00:37:21 +000064bool MCAsmLayout::isFragmentUpToDate(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000065 const MCSectionData &SD = *F->getParent();
66 const MCFragment *LastValid = LastValidFragment.lookup(&SD);
67 if (!LastValid)
68 return false;
69 assert(LastValid->getParent() == F->getParent());
70 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
Daniel Dunbar9005d452010-05-14 00:37:21 +000071}
72
Rafael Espindolaa9d42812010-11-23 08:08:33 +000073void MCAsmLayout::Invalidate(MCFragment *F) {
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000074 // If this fragment wasn't already up-to-date, we don't need to do anything.
75 if (!isFragmentUpToDate(F))
76 return;
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000077
Rafael Espindola2bf6afc2010-12-15 08:45:53 +000078 // Otherwise, reset the last valid fragment to this fragment.
Rafael Espindola4f4363a2010-12-07 23:32:26 +000079 const MCSectionData &SD = *F->getParent();
Rafael Espindola2bf6afc2010-12-15 08:45:53 +000080 LastValidFragment[&SD] = F;
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000081}
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000082
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000083void MCAsmLayout::EnsureValid(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000084 MCSectionData &SD = *F->getParent();
85
86 MCFragment *Cur = LastValidFragment[&SD];
87 if (!Cur)
88 Cur = &*SD.begin();
89 else
90 Cur = Cur->getNextNode();
91
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000092 // Advance the layout position until the fragment is up-to-date.
93 while (!isFragmentUpToDate(F)) {
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000094 const_cast<MCAsmLayout*>(this)->LayoutFragment(Cur);
Rafael Espindola4f4363a2010-12-07 23:32:26 +000095 Cur = Cur->getNextNode();
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000096 }
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000097}
98
Daniel Dunbar432cd5f2010-03-25 02:00:02 +000099uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
Daniel Dunbar47b3ec42010-05-14 00:51:14 +0000100 EnsureValid(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000101 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
102 return F->Offset;
103}
104
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000105uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000106 const MCSymbol &S = SD->getSymbol();
107
108 // If this is a variable, then recursively evaluate now.
109 if (S.isVariable()) {
110 MCValue Target;
111 if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this))
112 report_fatal_error("unable to evaluate offset for variable '" +
113 S.getName() + "'");
114
115 // Verify that any used symbols are defined.
116 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
117 report_fatal_error("unable to evaluate offset to undefined symbol '" +
118 Target.getSymA()->getSymbol().getName() + "'");
119 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
120 report_fatal_error("unable to evaluate offset to undefined symbol '" +
121 Target.getSymB()->getSymbol().getName() + "'");
Jim Grosbach684457d2011-10-26 22:44:41 +0000122
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000123 uint64_t Offset = Target.getConstant();
124 if (Target.getSymA())
125 Offset += getSymbolOffset(&Assembler.getSymbolData(
126 Target.getSymA()->getSymbol()));
127 if (Target.getSymB())
128 Offset -= getSymbolOffset(&Assembler.getSymbolData(
129 Target.getSymB()->getSymbol()));
130 return Offset;
131 }
132
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000133 assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
134 return getFragmentOffset(SD->getFragment()) + SD->getOffset();
135}
136
Daniel Dunbar2661f112010-05-13 03:19:50 +0000137uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
Daniel Dunbarafc6acd2010-05-14 00:37:11 +0000138 // The size is the last fragment's end offset.
Daniel Dunbar2661f112010-05-13 03:19:50 +0000139 const MCFragment &F = SD->getFragmentList().back();
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000140 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000141}
142
143uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
Daniel Dunbar2661f112010-05-13 03:19:50 +0000144 // Virtual sections have no file size.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000145 if (SD->getSection().isVirtualSection())
Daniel Dunbar2661f112010-05-13 03:19:50 +0000146 return 0;
147
148 // Otherwise, the file size is the same as the address space size.
149 return getSectionAddressSize(SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000150}
151
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000152/* *** */
153
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000154MCFragment::MCFragment() : Kind(FragmentType(~0)) {
155}
156
Daniel Dunbar36880e72010-07-28 20:28:45 +0000157MCFragment::~MCFragment() {
158}
159
Daniel Dunbar5e835962009-08-26 02:48:04 +0000160MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Rafael Espindola2bf6afc2010-12-15 08:45:53 +0000161 : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000162{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000163 if (Parent)
164 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000165}
166
167/* *** */
168
Daniel Dunbar81e40002009-08-27 00:38:04 +0000169MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000170
171MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000172 : Section(&_Section),
Rafael Espindolaf8803fe2010-12-06 03:48:09 +0000173 Ordinal(~UINT32_C(0)),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000174 Alignment(1),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000175 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000176{
177 if (A)
178 A->getSectionList().push_back(this);
179}
180
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000181/* *** */
182
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000183MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000184
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000185MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000186 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000187 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000188 IsExternal(false), IsPrivateExtern(false),
Matt Fleming6c8b3d22010-08-16 18:34:31 +0000189 CommonSize(0), SymbolSize(0), CommonAlign(0),
190 Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000191{
192 if (A)
193 A->getSymbolList().push_back(this);
194}
195
196/* *** */
197
Evan Cheng78c10ee2011-07-25 23:24:55 +0000198MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000199 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
200 raw_ostream &OS_)
201 : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
Rafael Espindola96aa78c2011-01-23 17:55:27 +0000202 OS(OS_), RelaxAll(false), NoExecStack(false), SubsectionsViaSymbols(false)
Daniel Dunbar6009db42009-08-26 21:22:22 +0000203{
204}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000205
206MCAssembler::~MCAssembler() {
207}
208
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000209bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
Daniel Dunbar23869852010-03-19 03:18:09 +0000210 // Non-temporary labels should always be visible to the linker.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000211 if (!Symbol.isTemporary())
Daniel Dunbar23869852010-03-19 03:18:09 +0000212 return true;
213
214 // Absolute temporary labels are never visible.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000215 if (!Symbol.isInSection())
Daniel Dunbar23869852010-03-19 03:18:09 +0000216 return false;
217
218 // Otherwise, check if the section requires symbols even for temporary labels.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000219 return getBackend().doesSectionRequireSymbols(Symbol.getSection());
Daniel Dunbar23869852010-03-19 03:18:09 +0000220}
221
Rafael Espindolab8141102010-09-27 18:13:03 +0000222const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000223 // Linker visible symbols define atoms.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000224 if (isSymbolLinkerVisible(SD->getSymbol()))
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000225 return SD;
226
227 // Absolute and undefined symbols have no defining atom.
228 if (!SD->getFragment())
229 return 0;
230
Daniel Dunbara5f1d572010-05-12 00:38:17 +0000231 // Non-linker visible symbols in sections which can't be atomized have no
232 // defining atom.
233 if (!getBackend().isSectionAtomizable(
234 SD->getFragment()->getParent()->getSection()))
235 return 0;
236
Daniel Dunbar651804c2010-05-11 17:22:50 +0000237 // Otherwise, return the atom for the containing fragment.
238 return SD->getFragment()->getAtom();
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000239}
240
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000241bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000242 const MCFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000243 MCValue &Target, uint64_t &Value) const {
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000244 ++stats::evaluateFixup;
Daniel Dunbarff547842010-03-23 23:47:14 +0000245
Rafael Espindola33a38a12010-12-22 16:11:57 +0000246 if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout))
Jim Grosbachf3c93672012-01-27 00:51:23 +0000247 getContext().FatalError(Fixup.getLoc(), "expected relocatable expression");
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000248
Rafael Espindolafea753b2010-12-24 21:22:02 +0000249 bool IsPCRel = Backend.getFixupKindInfo(
250 Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
251
252 bool IsResolved;
253 if (IsPCRel) {
254 if (Target.getSymB()) {
255 IsResolved = false;
256 } else if (!Target.getSymA()) {
257 IsResolved = false;
258 } else {
Rafael Espindola908159b2011-02-16 03:25:55 +0000259 const MCSymbolRefExpr *A = Target.getSymA();
260 const MCSymbol &SA = A->getSymbol();
261 if (A->getKind() != MCSymbolRefExpr::VK_None ||
262 SA.AliasedSymbol().isUndefined()) {
Rafael Espindolafea753b2010-12-24 21:22:02 +0000263 IsResolved = false;
264 } else {
265 const MCSymbolData &DataA = getSymbolData(SA);
266 IsResolved =
267 getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
268 *DF, false, true);
269 }
270 }
271 } else {
272 IsResolved = Target.isAbsolute();
273 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000274
275 Value = Target.getConstant();
276
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));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000281 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000282 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000283 const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
284 if (Sym.isDefined())
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000285 Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000286 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000287
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000288
Daniel Dunbar2761fc42010-12-16 03:20:06 +0000289 bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
Owen Anderson47dbd422010-12-15 18:48:27 +0000290 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
291 assert((ShouldAlignPC ? IsPCRel : true) &&
292 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
293
Owen Anderson05018c22010-12-09 20:27:52 +0000294 if (IsPCRel) {
Owen Anderson175fb362010-12-17 21:49:48 +0000295 uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
Jim Grosbach684457d2011-10-26 22:44:41 +0000296
Owen Anderson47dbd422010-12-15 18:48:27 +0000297 // A number of ARM fixups in Thumb mode require that the effective PC
298 // address be determined as the 32-bit aligned version of the actual offset.
Owen Andersond18e0112010-12-15 19:24:24 +0000299 if (ShouldAlignPC) Offset &= ~0x3;
Owen Anderson175fb362010-12-17 21:49:48 +0000300 Value -= Offset;
Owen Anderson05018c22010-12-09 20:27:52 +0000301 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000302
Jim Grosbach7b25ecf2012-02-27 21:36:23 +0000303 // Let the backend adjust the fixup value if necessary, including whether
304 // we need a relocation.
305 Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value,
306 IsResolved);
Jim Grosbach47500202010-12-14 18:46:57 +0000307
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000308 return IsResolved;
309}
310
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000311uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
Rafael Espindola7a459032010-12-21 20:35:18 +0000312 const MCFragment &F) const {
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000313 switch (F.getKind()) {
314 case MCFragment::FT_Data:
315 return cast<MCDataFragment>(F).getContents().size();
316 case MCFragment::FT_Fill:
317 return cast<MCFillFragment>(F).getSize();
318 case MCFragment::FT_Inst:
319 return cast<MCInstFragment>(F).getInstSize();
320
Rafael Espindola3ff57092010-11-02 17:22:24 +0000321 case MCFragment::FT_LEB:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000322 return cast<MCLEBFragment>(F).getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000323
Rafael Espindola7a459032010-12-21 20:35:18 +0000324 case MCFragment::FT_Align: {
325 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
326 unsigned Offset = Layout.getFragmentOffset(&AF);
327 unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
Owen Anderson15b7a982012-08-29 22:18:56 +0000328 // If we are padding with nops, force the padding to be larger than the
329 // minimum nop size.
330 if (Size > 0 && AF.hasEmitNops()) {
331 while (Size % getBackend().getMinimumNopSize())
332 Size += AF.getAlignment();
333 }
Rafael Espindola7a459032010-12-21 20:35:18 +0000334 if (Size > AF.getMaxBytesToEmit())
335 return 0;
336 return Size;
337 }
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000338
Rafael Espindola7a459032010-12-21 20:35:18 +0000339 case MCFragment::FT_Org: {
340 MCOrgFragment &OF = cast<MCOrgFragment>(F);
341 int64_t TargetLocation;
342 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
343 report_fatal_error("expected assembly-time absolute expression");
344
345 // FIXME: We need a way to communicate this error.
346 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
347 int64_t Size = TargetLocation - FragmentOffset;
348 if (Size < 0 || Size >= 0x40000000)
349 report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
350 "' (at offset '" + Twine(FragmentOffset) + "')");
351 return Size;
352 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000353
Rafael Espindola187d8332010-11-07 02:07:12 +0000354 case MCFragment::FT_Dwarf:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000355 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
Rafael Espindola245a1e22010-12-28 05:39:27 +0000356 case MCFragment::FT_DwarfFrame:
357 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000358 }
359
Craig Topper85814382012-02-07 05:05:23 +0000360 llvm_unreachable("invalid fragment kind");
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000361}
362
Daniel Dunbarb69fc042010-05-13 20:40:12 +0000363void MCAsmLayout::LayoutFragment(MCFragment *F) {
Daniel Dunbar9005d452010-05-14 00:37:21 +0000364 MCFragment *Prev = F->getPrevNode();
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000365
Daniel Dunbar9005d452010-05-14 00:37:21 +0000366 // We should never try to recompute something which is up-to-date.
367 assert(!isFragmentUpToDate(F) && "Attempt to recompute up-to-date fragment!");
Daniel Dunbar9005d452010-05-14 00:37:21 +0000368 // We should never try to compute the fragment layout if it's predecessor
369 // isn't up-to-date.
370 assert((!Prev || isFragmentUpToDate(Prev)) &&
371 "Attempt to compute fragment before it's predecessor!");
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000372
373 ++stats::FragmentLayouts;
374
Daniel Dunbarb69fc042010-05-13 20:40:12 +0000375 // Compute fragment offset and size.
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000376 uint64_t Offset = 0;
377 if (Prev)
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000378 Offset += Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000379
380 F->Offset = Offset;
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000381 LastValidFragment[F->getParent()] = F;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000382}
383
Daniel Dunbar53b23382010-03-19 09:28:59 +0000384/// WriteFragmentData - Write the \arg F data to the output file.
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000385static void WriteFragmentData(const MCAssembler &Asm, const MCAsmLayout &Layout,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000386 const MCFragment &F) {
387 MCObjectWriter *OW = &Asm.getWriter();
Daniel Dunbar53b23382010-03-19 09:28:59 +0000388 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000389 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000390
Daniel Dunbarff547842010-03-23 23:47:14 +0000391 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000392
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000393 // FIXME: Embed in fragments instead?
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000394 uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000395 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000396 case MCFragment::FT_Align: {
397 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000398 uint64_t Count = FragmentSize / AF.getValueSize();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000399
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000400 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
401
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000402 // FIXME: This error shouldn't actually occur (the front end should emit
403 // multiple .align directives to enforce the semantics it wants), but is
404 // severe enough that we want to report it. How to handle this?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000405 if (Count * AF.getValueSize() != FragmentSize)
Chris Lattner75361b62010-04-07 22:58:41 +0000406 report_fatal_error("undefined .align directive, value size '" +
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000407 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000408 "' is not a divisor of padding size '" +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000409 Twine(FragmentSize) + "'");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000410
Kevin Enderby6e720482010-02-23 18:26:34 +0000411 // See if we are aligning with nops, and if so do that first to try to fill
412 // the Count bytes. Then if that did not fill any bytes or there are any
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000413 // bytes left to fill use the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000414 // If we are aligning with nops, ask that target to emit the right data.
Daniel Dunbar1c154132010-05-12 22:56:23 +0000415 if (AF.hasEmitNops()) {
Jim Grosbachec343382012-01-18 18:52:16 +0000416 if (!Asm.getBackend().writeNopData(Count, OW))
Chris Lattner75361b62010-04-07 22:58:41 +0000417 report_fatal_error("unable to write nop sequence of " +
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000418 Twine(Count) + " bytes");
419 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000420 }
421
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000422 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000423 for (uint64_t i = 0; i != Count; ++i) {
424 switch (AF.getValueSize()) {
Craig Topper85814382012-02-07 05:05:23 +0000425 default: llvm_unreachable("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()) {
Craig Topper85814382012-02-07 05:05:23 +0000449 default: llvm_unreachable("Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000450 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
451 case 2: OW->Write16(uint16_t(FF.getValue())); break;
452 case 4: OW->Write32(uint32_t(FF.getValue())); break;
453 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000454 }
455 }
456 break;
457 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000458
Rafael Espindola179821a2010-12-06 19:08:48 +0000459 case MCFragment::FT_Inst: {
460 MCInstFragment &IF = cast<MCInstFragment>(F);
461 OW->WriteBytes(StringRef(IF.getCode().begin(), IF.getCode().size()));
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000462 break;
Rafael Espindola179821a2010-12-06 19:08:48 +0000463 }
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000464
Rafael Espindola3ff57092010-11-02 17:22:24 +0000465 case MCFragment::FT_LEB: {
466 MCLEBFragment &LF = cast<MCLEBFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000467 OW->WriteBytes(LF.getContents().str());
Rafael Espindola3ff57092010-11-02 17:22:24 +0000468 break;
469 }
470
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000471 case MCFragment::FT_Org: {
472 MCOrgFragment &OF = cast<MCOrgFragment>(F);
473
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000474 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000475 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000476
477 break;
478 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000479
480 case MCFragment::FT_Dwarf: {
481 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000482 OW->WriteBytes(OF.getContents().str());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000483 break;
484 }
Rafael Espindola245a1e22010-12-28 05:39:27 +0000485 case MCFragment::FT_DwarfFrame: {
486 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
487 OW->WriteBytes(CF.getContents().str());
488 break;
489 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000490 }
491
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000492 assert(OW->getStream().tell() - Start == FragmentSize);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000493}
494
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000495void MCAssembler::writeSectionData(const MCSectionData *SD,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000496 const MCAsmLayout &Layout) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000497 // Ignore virtual sections.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000498 if (SD->getSection().isVirtualSection()) {
Daniel Dunbar054be922010-05-13 03:50:50 +0000499 assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000500
501 // Check that contents are only things legal inside a virtual section.
502 for (MCSectionData::const_iterator it = SD->begin(),
503 ie = SD->end(); it != ie; ++it) {
504 switch (it->getKind()) {
Craig Topper85814382012-02-07 05:05:23 +0000505 default: llvm_unreachable("Invalid fragment in virtual section!");
Daniel Dunbarc983b202010-08-18 18:22:37 +0000506 case MCFragment::FT_Data: {
507 // Check that we aren't trying to write a non-zero contents (or fixups)
508 // into a virtual section. This is to support clients which use standard
509 // directives to fill the contents of virtual sections.
510 MCDataFragment &DF = cast<MCDataFragment>(*it);
511 assert(DF.fixup_begin() == DF.fixup_end() &&
512 "Cannot have fixups in virtual section!");
513 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
514 assert(DF.getContents()[i] == 0 &&
515 "Invalid data value for virtual section!");
516 break;
517 }
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000518 case MCFragment::FT_Align:
Daniel Dunbarc983b202010-08-18 18:22:37 +0000519 // Check that we aren't trying to write a non-zero value into a virtual
520 // section.
521 assert((!cast<MCAlignFragment>(it)->getValueSize() ||
522 !cast<MCAlignFragment>(it)->getValue()) &&
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000523 "Invalid align in virtual section!");
524 break;
525 case MCFragment::FT_Fill:
526 assert(!cast<MCFillFragment>(it)->getValueSize() &&
527 "Invalid fill in virtual section!");
528 break;
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000529 }
530 }
531
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000532 return;
533 }
534
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000535 uint64_t Start = getWriter().getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000536 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000537
Daniel Dunbar53b23382010-03-19 09:28:59 +0000538 for (MCSectionData::const_iterator it = SD->begin(),
539 ie = SD->end(); it != ie; ++it)
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000540 WriteFragmentData(*this, Layout, *it);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000541
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000542 assert(getWriter().getStream().tell() - Start ==
543 Layout.getSectionAddressSize(SD));
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000544}
545
Rafael Espindola179821a2010-12-06 19:08:48 +0000546
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000547uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000548 MCFragment &F,
549 const MCFixup &Fixup) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000550 // Evaluate the fixup.
551 MCValue Target;
552 uint64_t FixedValue;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000553 if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000554 // The fixup was unresolved, we need a relocation. Inform the object
555 // writer of the relocation, and give it an opportunity to adjust the
556 // fixup value if need be.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000557 getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
Rafael Espindola179821a2010-12-06 19:08:48 +0000558 }
559 return FixedValue;
560 }
561
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000562void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000563 DEBUG_WITH_TYPE("mc-dump", {
564 llvm::errs() << "assembler backend - pre-layout\n--\n";
565 dump(); });
566
Daniel Dunbar61066db2010-05-13 02:34:14 +0000567 // Create the layout object.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000568 MCAsmLayout Layout(*this);
Daniel Dunbar61066db2010-05-13 02:34:14 +0000569
Daniel Dunbar337718e2010-05-14 00:37:14 +0000570 // Create dummy fragments and assign section ordinals.
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000571 unsigned SectionIndex = 0;
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000572 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
573 // Create dummy fragments to eliminate any empty sections, this simplifies
574 // layout.
Duncan Sands6f74f692010-06-29 13:30:08 +0000575 if (it->getFragmentList().empty())
Rafael Espindolad80781b2010-09-15 21:48:40 +0000576 new MCDataFragment(it);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000577
578 it->setOrdinal(SectionIndex++);
Daniel Dunbar337718e2010-05-14 00:37:14 +0000579 }
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000580
Daniel Dunbar337718e2010-05-14 00:37:14 +0000581 // Assign layout order indices to sections and fragments.
Daniel Dunbar337718e2010-05-14 00:37:14 +0000582 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
583 MCSectionData *SD = Layout.getSectionOrder()[i];
584 SD->setLayoutOrder(i);
585
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000586 unsigned FragmentIndex = 0;
Daniel Dunbar337718e2010-05-14 00:37:14 +0000587 for (MCSectionData::iterator it2 = SD->begin(),
588 ie2 = SD->end(); it2 != ie2; ++it2)
589 it2->setLayoutOrder(FragmentIndex++);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000590 }
591
Daniel Dunbar61066db2010-05-13 02:34:14 +0000592 // Layout until everything fits.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000593 while (layoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000594 continue;
595
596 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000597 llvm::errs() << "assembler backend - post-relaxation\n--\n";
598 dump(); });
599
600 // Finalize the layout, including fragment lowering.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000601 finishLayout(Layout);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000602
603 DEBUG_WITH_TYPE("mc-dump", {
604 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000605 dump(); });
606
Daniel Dunbarff547842010-03-23 23:47:14 +0000607 uint64_t StartOffset = OS.tell();
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000608
Daniel Dunbarbacba992010-03-19 07:09:33 +0000609 // Allow the object writer a chance to perform post-layout binding (for
610 // example, to set the index fields in the symbol data).
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000611 getWriter().ExecutePostLayoutBinding(*this, Layout);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000612
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000613 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000614 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
615 for (MCSectionData::iterator it2 = it->begin(),
616 ie2 = it->end(); it2 != ie2; ++it2) {
617 MCDataFragment *DF = dyn_cast<MCDataFragment>(it2);
Rafael Espindola179821a2010-12-06 19:08:48 +0000618 if (DF) {
619 for (MCDataFragment::fixup_iterator it3 = DF->fixup_begin(),
620 ie3 = DF->fixup_end(); it3 != ie3; ++it3) {
621 MCFixup &Fixup = *it3;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000622 uint64_t FixedValue = handleFixup(Layout, *DF, Fixup);
Jim Grosbachec343382012-01-18 18:52:16 +0000623 getBackend().applyFixup(Fixup, DF->getContents().data(),
Rafael Espindola179821a2010-12-06 19:08:48 +0000624 DF->getContents().size(), FixedValue);
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000625 }
Rafael Espindola179821a2010-12-06 19:08:48 +0000626 }
627 MCInstFragment *IF = dyn_cast<MCInstFragment>(it2);
628 if (IF) {
629 for (MCInstFragment::fixup_iterator it3 = IF->fixup_begin(),
630 ie3 = IF->fixup_end(); it3 != ie3; ++it3) {
631 MCFixup &Fixup = *it3;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000632 uint64_t FixedValue = handleFixup(Layout, *IF, Fixup);
Jim Grosbachec343382012-01-18 18:52:16 +0000633 getBackend().applyFixup(Fixup, IF->getCode().data(),
Rafael Espindola179821a2010-12-06 19:08:48 +0000634 IF->getCode().size(), FixedValue);
635 }
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000636 }
637 }
638 }
639
Daniel Dunbarbacba992010-03-19 07:09:33 +0000640 // Write the object file.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000641 getWriter().WriteObject(*this, Layout);
Daniel Dunbarff547842010-03-23 23:47:14 +0000642
643 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000644}
645
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000646bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
Jim Grosbach370b78d2011-12-06 00:47:03 +0000647 const MCInstFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000648 const MCAsmLayout &Layout) const {
Daniel Dunbarac2884a2010-03-25 22:49:09 +0000649 if (getRelaxAll())
650 return true;
651
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000652 // If we cannot resolve the fixup value, it requires relaxation.
653 MCValue Target;
654 uint64_t Value;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000655 if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000656 return true;
657
Jim Grosbach370b78d2011-12-06 00:47:03 +0000658 return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000659}
660
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000661bool MCAssembler::fragmentNeedsRelaxation(const MCInstFragment *IF,
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000662 const MCAsmLayout &Layout) const {
663 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
664 // are intentionally pushing out inst fragments, or because we relaxed a
665 // previous instruction to one that doesn't need relaxation.
Jim Grosbachec343382012-01-18 18:52:16 +0000666 if (!getBackend().mayNeedRelaxation(IF->getInst()))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000667 return false;
668
669 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
670 ie = IF->fixup_end(); it != ie; ++it)
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000671 if (fixupNeedsRelaxation(*it, IF, Layout))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000672 return true;
673
674 return false;
675}
676
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000677bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
Rafael Espindola3ff57092010-11-02 17:22:24 +0000678 MCInstFragment &IF) {
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000679 if (!fragmentNeedsRelaxation(&IF, Layout))
Rafael Espindola3ff57092010-11-02 17:22:24 +0000680 return false;
681
682 ++stats::RelaxedInstructions;
683
684 // FIXME-PERF: We could immediately lower out instructions if we can tell
685 // they are fully resolved, to avoid retesting on later passes.
686
687 // Relax the fragment.
688
689 MCInst Relaxed;
Jim Grosbachec343382012-01-18 18:52:16 +0000690 getBackend().relaxInstruction(IF.getInst(), Relaxed);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000691
692 // Encode the new instruction.
693 //
694 // FIXME-PERF: If it matters, we could let the target do this. It can
695 // probably do so more efficiently in many cases.
696 SmallVector<MCFixup, 4> Fixups;
697 SmallString<256> Code;
698 raw_svector_ostream VecOS(Code);
699 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
700 VecOS.flush();
701
702 // Update the instruction fragment.
Rafael Espindola3ff57092010-11-02 17:22:24 +0000703 IF.setInst(Relaxed);
704 IF.getCode() = Code;
705 IF.getFixups().clear();
706 // FIXME: Eliminate copy.
707 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
708 IF.getFixups().push_back(Fixups[i]);
709
Rafael Espindola3ff57092010-11-02 17:22:24 +0000710 return true;
711}
712
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000713bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000714 int64_t Value = 0;
715 uint64_t OldSize = LF.getContents().size();
Rafael Espindolad88cac02011-04-26 02:17:58 +0000716 bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout);
717 (void)IsAbs;
718 assert(IsAbs);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000719 SmallString<8> &Data = LF.getContents();
720 Data.clear();
721 raw_svector_ostream OSE(Data);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000722 if (LF.isSigned())
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000723 encodeSLEB128(Value, OSE);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000724 else
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000725 encodeULEB128(Value, OSE);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000726 OSE.flush();
727 return OldSize != LF.getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000728}
729
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000730bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000731 MCDwarfLineAddrFragment &DF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000732 int64_t AddrDelta = 0;
733 uint64_t OldSize = DF.getContents().size();
Rafael Espindola835439a2010-12-22 22:04:28 +0000734 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
735 (void)IsAbs;
736 assert(IsAbs);
Rafael Espindola187d8332010-11-07 02:07:12 +0000737 int64_t LineDelta;
738 LineDelta = DF.getLineDelta();
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000739 SmallString<8> &Data = DF.getContents();
740 Data.clear();
741 raw_svector_ostream OSE(Data);
742 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE);
743 OSE.flush();
744 return OldSize != Data.size();
Rafael Espindola187d8332010-11-07 02:07:12 +0000745}
746
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000747bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
Rafael Espindola245a1e22010-12-28 05:39:27 +0000748 MCDwarfCallFrameFragment &DF) {
749 int64_t AddrDelta = 0;
750 uint64_t OldSize = DF.getContents().size();
751 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
752 (void)IsAbs;
753 assert(IsAbs);
754 SmallString<8> &Data = DF.getContents();
755 Data.clear();
756 raw_svector_ostream OSE(Data);
Rafael Espindola4eafe102011-05-08 14:35:21 +0000757 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE);
Rafael Espindola245a1e22010-12-28 05:39:27 +0000758 OSE.flush();
759 return OldSize != Data.size();
760}
761
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000762bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout,
Rafael Espindola62b83b62010-12-21 04:22:09 +0000763 MCSectionData &SD) {
764 MCFragment *FirstInvalidFragment = NULL;
765 // Scan for fragments that need relaxation.
766 for (MCSectionData::iterator it2 = SD.begin(),
767 ie2 = SD.end(); it2 != ie2; ++it2) {
768 // Check if this is an fragment that needs relaxation.
769 bool relaxedFrag = false;
770 switch(it2->getKind()) {
771 default:
772 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000773 case MCFragment::FT_Inst:
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000774 relaxedFrag = relaxInstruction(Layout, *cast<MCInstFragment>(it2));
Rafael Espindola62b83b62010-12-21 04:22:09 +0000775 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000776 case MCFragment::FT_Dwarf:
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000777 relaxedFrag = relaxDwarfLineAddr(Layout,
Rafael Espindola62b83b62010-12-21 04:22:09 +0000778 *cast<MCDwarfLineAddrFragment>(it2));
779 break;
Rafael Espindola245a1e22010-12-28 05:39:27 +0000780 case MCFragment::FT_DwarfFrame:
781 relaxedFrag =
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000782 relaxDwarfCallFrameFragment(Layout,
Rafael Espindola245a1e22010-12-28 05:39:27 +0000783 *cast<MCDwarfCallFrameFragment>(it2));
784 break;
785 case MCFragment::FT_LEB:
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000786 relaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(it2));
Rafael Espindola245a1e22010-12-28 05:39:27 +0000787 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000788 }
789 // Update the layout, and remember that we relaxed.
790 if (relaxedFrag && !FirstInvalidFragment)
791 FirstInvalidFragment = it2;
792 }
793 if (FirstInvalidFragment) {
794 Layout.Invalidate(FirstInvalidFragment);
795 return true;
796 }
797 return false;
798}
799
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000800bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000801 ++stats::RelaxationSteps;
802
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000803 bool WasRelaxed = false;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000804 for (iterator it = begin(), ie = end(); it != ie; ++it) {
805 MCSectionData &SD = *it;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000806 while(layoutSectionOnce(Layout, SD))
Rafael Espindola62b83b62010-12-21 04:22:09 +0000807 WasRelaxed = true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000808 }
809
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000810 return WasRelaxed;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000811}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000812
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000813void MCAssembler::finishLayout(MCAsmLayout &Layout) {
Rafael Espindolab4172fa2010-12-04 22:47:22 +0000814 // The layout is done. Mark every fragment as valid.
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000815 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
816 Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
817 }
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000818}
819
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000820// Debugging methods
821
822namespace llvm {
823
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000824raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
825 OS << "<MCFixup" << " Offset:" << AF.getOffset()
Daniel Dunbar482ad802010-05-26 15:18:31 +0000826 << " Value:" << *AF.getValue()
827 << " Kind:" << AF.getKind() << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000828 return OS;
829}
830
831}
832
833void MCFragment::dump() {
834 raw_ostream &OS = llvm::errs();
835
Daniel Dunbare614e392010-05-26 06:50:57 +0000836 OS << "<";
837 switch (getKind()) {
838 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
839 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
840 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
841 case MCFragment::FT_Inst: OS << "MCInstFragment"; break;
842 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000843 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
Rafael Espindola245a1e22010-12-28 05:39:27 +0000844 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000845 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
Daniel Dunbare614e392010-05-26 06:50:57 +0000846 }
847
Daniel Dunbar337718e2010-05-14 00:37:14 +0000848 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
Rafael Espindola2bf6afc2010-12-15 08:45:53 +0000849 << " Offset:" << Offset << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000850
Daniel Dunbare614e392010-05-26 06:50:57 +0000851 switch (getKind()) {
852 case MCFragment::FT_Align: {
853 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
854 if (AF->hasEmitNops())
855 OS << " (emit nops)";
Daniel Dunbare614e392010-05-26 06:50:57 +0000856 OS << "\n ";
857 OS << " Alignment:" << AF->getAlignment()
858 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
859 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
860 break;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000861 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000862 case MCFragment::FT_Data: {
863 const MCDataFragment *DF = cast<MCDataFragment>(this);
864 OS << "\n ";
865 OS << " Contents:[";
866 const SmallVectorImpl<char> &Contents = DF->getContents();
867 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
868 if (i) OS << ",";
869 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000870 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000871 OS << "] (" << Contents.size() << " bytes)";
872
873 if (!DF->getFixups().empty()) {
874 OS << ",\n ";
875 OS << " Fixups:[";
876 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
877 ie = DF->fixup_end(); it != ie; ++it) {
878 if (it != DF->fixup_begin()) OS << ",\n ";
879 OS << *it;
880 }
881 OS << "]";
882 }
883 break;
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000884 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000885 case MCFragment::FT_Fill: {
886 const MCFillFragment *FF = cast<MCFillFragment>(this);
887 OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
888 << " Size:" << FF->getSize();
889 break;
890 }
891 case MCFragment::FT_Inst: {
892 const MCInstFragment *IF = cast<MCInstFragment>(this);
893 OS << "\n ";
894 OS << " Inst:";
895 IF->getInst().dump_pretty(OS);
896 break;
897 }
898 case MCFragment::FT_Org: {
899 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
900 OS << "\n ";
901 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
902 break;
903 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000904 case MCFragment::FT_Dwarf: {
905 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
906 OS << "\n ";
907 OS << " AddrDelta:" << OF->getAddrDelta()
908 << " LineDelta:" << OF->getLineDelta();
909 break;
910 }
Rafael Espindola245a1e22010-12-28 05:39:27 +0000911 case MCFragment::FT_DwarfFrame: {
912 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
913 OS << "\n ";
914 OS << " AddrDelta:" << CF->getAddrDelta();
915 break;
916 }
Rafael Espindola3ff57092010-11-02 17:22:24 +0000917 case MCFragment::FT_LEB: {
918 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
919 OS << "\n ";
920 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
921 break;
922 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000923 }
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000924 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000925}
926
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000927void MCSectionData::dump() {
928 raw_ostream &OS = llvm::errs();
929
930 OS << "<MCSectionData";
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000931 OS << " Alignment:" << getAlignment() << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000932 for (iterator it = begin(), ie = end(); it != ie; ++it) {
933 if (it != begin()) OS << ",\n ";
934 it->dump();
935 }
936 OS << "]>";
937}
938
939void MCSymbolData::dump() {
940 raw_ostream &OS = llvm::errs();
941
942 OS << "<MCSymbolData Symbol:" << getSymbol()
943 << " Fragment:" << getFragment() << " Offset:" << getOffset()
944 << " Flags:" << getFlags() << " Index:" << getIndex();
945 if (isCommon())
946 OS << " (common, size:" << getCommonSize()
947 << " align: " << getCommonAlignment() << ")";
948 if (isExternal())
949 OS << " (external)";
950 if (isPrivateExtern())
951 OS << " (private extern)";
952 OS << ">";
953}
954
955void MCAssembler::dump() {
956 raw_ostream &OS = llvm::errs();
957
958 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000959 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000960 for (iterator it = begin(), ie = end(); it != ie; ++it) {
961 if (it != begin()) OS << ",\n ";
962 it->dump();
963 }
964 OS << "],\n";
965 OS << " Symbols:[";
966
967 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000968 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000969 it->dump();
970 }
971 OS << "]>\n";
972}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000973
974// anchors for MC*Fragment vtables
975void MCDataFragment::anchor() { }
976void MCInstFragment::anchor() { }
977void MCAlignFragment::anchor() { }
978void MCFillFragment::anchor() { }
979void MCOrgFragment::anchor() { }
980void MCLEBFragment::anchor() { }
981void MCDwarfLineAddrFragment::anchor() { }
982void MCDwarfCallFrameFragment::anchor() { }