blob: 2558eff045507daf1e220b37dbcee47338d9bca6 [file] [log] [blame]
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +00001//===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbar0adcd352009-08-25 21:10:45 +000010#define DEBUG_TYPE "assembler"
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000011#include "llvm/MC/MCAssembler.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000012#include "llvm/ADT/Statistic.h"
13#include "llvm/ADT/StringExtras.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar18ff2cc2010-03-11 05:53:33 +000016#include "llvm/MC/MCAsmLayout.h"
Daniel Dunbarb36052f2010-03-19 10:43:23 +000017#include "llvm/MC/MCCodeEmitter.h"
Rafael Espindolafea753b2010-12-24 21:22:02 +000018#include "llvm/MC/MCContext.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/MC/MCDwarf.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000020#include "llvm/MC/MCExpr.h"
Craig Topperf1d0f772012-03-26 06:58:25 +000021#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar53b23382010-03-19 09:28:59 +000022#include "llvm/MC/MCObjectWriter.h"
Kevin Enderbyc0957932010-09-30 16:52:03 +000023#include "llvm/MC/MCSection.h"
Daniel Dunbar1253a6f2009-10-16 01:58:03 +000024#include "llvm/MC/MCSymbol.h"
25#include "llvm/MC/MCValue.h"
Daniel Dunbarac2884a2010-03-25 22:49:09 +000026#include "llvm/Support/Debug.h"
Daniel Dunbar0705fbf2009-08-21 18:29:01 +000027#include "llvm/Support/ErrorHandling.h"
Jim Grosbach2d39a0e2012-08-08 23:56:06 +000028#include "llvm/Support/LEB128.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Support/TargetRegistry.h"
30#include "llvm/Support/raw_ostream.h"
Daniel Dunbarf6346762010-02-13 09:29:02 +000031
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000032using namespace llvm;
33
Daniel Dunbarff547842010-03-23 23:47:14 +000034namespace {
35namespace stats {
Eli Bendersky8ddc5a12012-12-07 17:59:21 +000036STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
Eli Bendersky6ac81f52012-12-10 18:59:39 +000037STATISTIC(EmittedInstFragments,
38 "Number of emitted assembler fragments - instruction");
39STATISTIC(EmittedDataFragments,
40 "Number of emitted assembler fragments - data");
41STATISTIC(EmittedAlignFragments,
42 "Number of emitted assembler fragments - align");
43STATISTIC(EmittedFillFragments,
44 "Number of emitted assembler fragments - fill");
45STATISTIC(EmittedOrgFragments,
46 "Number of emitted assembler fragments - org");
Jim Grosbachf77d5b12011-12-06 00:03:48 +000047STATISTIC(evaluateFixup, "Number of evaluated fixups");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000048STATISTIC(FragmentLayouts, "Number of fragment layouts");
Daniel Dunbarff547842010-03-23 23:47:14 +000049STATISTIC(ObjectBytes, "Number of emitted object file bytes");
Daniel Dunbarac2884a2010-03-25 22:49:09 +000050STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
51STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
Daniel Dunbarff547842010-03-23 23:47:14 +000052}
53}
Daniel Dunbar0adcd352009-08-25 21:10:45 +000054
Daniel Dunbar8f4d1462009-08-28 07:08:35 +000055// FIXME FIXME FIXME: There are number of places in this file where we convert
56// what is a 64-bit assembler value used for computation into a value in the
57// object file, which may truncate it. We should detect that truncation where
58// invalid and report errors back.
59
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +000060/* *** */
61
Daniel Dunbar9005d452010-05-14 00:37:21 +000062MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
Rafael Espindola4f4363a2010-12-07 23:32:26 +000063 : Assembler(Asm), LastValidFragment()
Daniel Dunbar9005d452010-05-14 00:37:21 +000064 {
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000065 // Compute the section layout order. Virtual sections must go last.
66 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000067 if (!it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000068 SectionOrder.push_back(&*it);
69 for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +000070 if (it->getSection().isVirtualSection())
Daniel Dunbarbc1a0cf2010-05-12 15:42:59 +000071 SectionOrder.push_back(&*it);
72}
73
Eli Benderskyd52a2c02012-12-12 19:54:05 +000074bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000075 const MCSectionData &SD = *F->getParent();
76 const MCFragment *LastValid = LastValidFragment.lookup(&SD);
77 if (!LastValid)
78 return false;
79 assert(LastValid->getParent() == F->getParent());
80 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
Daniel Dunbar9005d452010-05-14 00:37:21 +000081}
82
Eli Benderskyf43e3fd2012-12-10 20:13:43 +000083void MCAsmLayout::invalidateFragmentsAfter(MCFragment *F) {
Eli Benderskyd52a2c02012-12-12 19:54:05 +000084 // If this fragment wasn't already valid, we don't need to do anything.
85 if (!isFragmentValid(F))
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000086 return;
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000087
Rafael Espindola2bf6afc2010-12-15 08:45:53 +000088 // Otherwise, reset the last valid fragment to this fragment.
Rafael Espindola4f4363a2010-12-07 23:32:26 +000089 const MCSectionData &SD = *F->getParent();
Rafael Espindola2bf6afc2010-12-15 08:45:53 +000090 LastValidFragment[&SD] = F;
Daniel Dunbar47b3ec42010-05-14 00:51:14 +000091}
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +000092
Eli Benderskyd52a2c02012-12-12 19:54:05 +000093void MCAsmLayout::ensureValid(const MCFragment *F) const {
Rafael Espindola4f4363a2010-12-07 23:32:26 +000094 MCSectionData &SD = *F->getParent();
95
96 MCFragment *Cur = LastValidFragment[&SD];
97 if (!Cur)
98 Cur = &*SD.begin();
99 else
100 Cur = Cur->getNextNode();
101
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000102 // Advance the layout position until the fragment is valid.
103 while (!isFragmentValid(F)) {
104 assert(Cur && "Layout bookkeeping error");
105 const_cast<MCAsmLayout*>(this)->layoutFragment(Cur);
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000106 Cur = Cur->getNextNode();
Daniel Dunbar47b3ec42010-05-14 00:51:14 +0000107 }
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000108}
109
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000110uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000111 ensureValid(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000112 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
113 return F->Offset;
114}
115
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000116uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000117 const MCSymbol &S = SD->getSymbol();
118
119 // If this is a variable, then recursively evaluate now.
120 if (S.isVariable()) {
121 MCValue Target;
122 if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this))
123 report_fatal_error("unable to evaluate offset for variable '" +
124 S.getName() + "'");
125
126 // Verify that any used symbols are defined.
127 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
128 report_fatal_error("unable to evaluate offset to undefined symbol '" +
129 Target.getSymA()->getSymbol().getName() + "'");
130 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
131 report_fatal_error("unable to evaluate offset to undefined symbol '" +
132 Target.getSymB()->getSymbol().getName() + "'");
Jim Grosbach684457d2011-10-26 22:44:41 +0000133
Daniel Dunbarc8497b62011-04-29 18:20:20 +0000134 uint64_t Offset = Target.getConstant();
135 if (Target.getSymA())
136 Offset += getSymbolOffset(&Assembler.getSymbolData(
137 Target.getSymA()->getSymbol()));
138 if (Target.getSymB())
139 Offset -= getSymbolOffset(&Assembler.getSymbolData(
140 Target.getSymB()->getSymbol()));
141 return Offset;
142 }
143
Rafael Espindolaffd902b2010-12-06 02:57:26 +0000144 assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
145 return getFragmentOffset(SD->getFragment()) + SD->getOffset();
146}
147
Daniel Dunbar2661f112010-05-13 03:19:50 +0000148uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
Daniel Dunbarafc6acd2010-05-14 00:37:11 +0000149 // The size is the last fragment's end offset.
Daniel Dunbar2661f112010-05-13 03:19:50 +0000150 const MCFragment &F = SD->getFragmentList().back();
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000151 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000152}
153
154uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
Daniel Dunbar2661f112010-05-13 03:19:50 +0000155 // Virtual sections have no file size.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000156 if (SD->getSection().isVirtualSection())
Daniel Dunbar2661f112010-05-13 03:19:50 +0000157 return 0;
158
159 // Otherwise, the file size is the same as the address space size.
160 return getSectionAddressSize(SD);
Daniel Dunbar5d428512010-03-25 02:00:07 +0000161}
162
Daniel Dunbar207e06e2010-03-24 03:43:40 +0000163/* *** */
164
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000165MCFragment::MCFragment() : Kind(FragmentType(~0)) {
166}
167
Daniel Dunbar36880e72010-07-28 20:28:45 +0000168MCFragment::~MCFragment() {
169}
170
Daniel Dunbar5e835962009-08-26 02:48:04 +0000171MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
Rafael Espindola2bf6afc2010-12-15 08:45:53 +0000172 : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000173{
Daniel Dunbar5e835962009-08-26 02:48:04 +0000174 if (Parent)
175 Parent->getFragmentList().push_back(this);
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000176}
177
178/* *** */
179
Eli Bendersky64d9a322012-12-07 19:13:57 +0000180MCEncodedFragment::~MCEncodedFragment() {
181}
182
183/* *** */
184
Daniel Dunbar81e40002009-08-27 00:38:04 +0000185MCSectionData::MCSectionData() : Section(0) {}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000186
187MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
Daniel Dunbar81e40002009-08-27 00:38:04 +0000188 : Section(&_Section),
Rafael Espindolaf8803fe2010-12-06 03:48:09 +0000189 Ordinal(~UINT32_C(0)),
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000190 Alignment(1),
Daniel Dunbare1ec6172010-02-02 21:44:01 +0000191 HasInstructions(false)
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000192{
193 if (A)
194 A->getSectionList().push_back(this);
195}
196
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000197/* *** */
198
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000199MCSymbolData::MCSymbolData() : Symbol(0) {}
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000200
Daniel Dunbarcb579b32009-08-31 08:08:06 +0000201MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000202 uint64_t _Offset, MCAssembler *A)
Daniel Dunbarefbb5332009-09-01 04:09:03 +0000203 : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
Daniel Dunbar8f4d1462009-08-28 07:08:35 +0000204 IsExternal(false), IsPrivateExtern(false),
Matt Fleming6c8b3d22010-08-16 18:34:31 +0000205 CommonSize(0), SymbolSize(0), CommonAlign(0),
206 Flags(0), Index(0)
Daniel Dunbarf3d2ef02009-08-22 10:13:24 +0000207{
208 if (A)
209 A->getSymbolList().push_back(this);
210}
211
212/* *** */
213
Evan Cheng78c10ee2011-07-25 23:24:55 +0000214MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000215 MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
216 raw_ostream &OS_)
217 : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
Jim Grosbachb5762bf2012-09-18 23:05:18 +0000218 OS(OS_), RelaxAll(false), NoExecStack(false), SubsectionsViaSymbols(false) {
Daniel Dunbar6009db42009-08-26 21:22:22 +0000219}
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000220
221MCAssembler::~MCAssembler() {
222}
223
Pedro Artigas5399d252012-12-12 22:59:46 +0000224void MCAssembler::reset() {
225 Sections.clear();
226 Symbols.clear();
227 SectionMap.clear();
228 SymbolMap.clear();
229 IndirectSymbols.clear();
230 DataRegions.clear();
231 ThumbFuncs.clear();
232 RelaxAll = false;
233 NoExecStack = false;
234 SubsectionsViaSymbols = false;
Pedro Artigas99cbdde2012-12-14 18:52:11 +0000235
236 // reset objects owned by us
237 getBackend().reset();
238 getEmitter().reset();
239 getWriter().reset();
Pedro Artigas5399d252012-12-12 22:59:46 +0000240}
241
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000242bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
Daniel Dunbar23869852010-03-19 03:18:09 +0000243 // Non-temporary labels should always be visible to the linker.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000244 if (!Symbol.isTemporary())
Daniel Dunbar23869852010-03-19 03:18:09 +0000245 return true;
246
247 // Absolute temporary labels are never visible.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000248 if (!Symbol.isInSection())
Daniel Dunbar23869852010-03-19 03:18:09 +0000249 return false;
250
251 // Otherwise, check if the section requires symbols even for temporary labels.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000252 return getBackend().doesSectionRequireSymbols(Symbol.getSection());
Daniel Dunbar23869852010-03-19 03:18:09 +0000253}
254
Rafael Espindolab8141102010-09-27 18:13:03 +0000255const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000256 // Linker visible symbols define atoms.
Daniel Dunbar843aa1f2010-06-16 20:04:29 +0000257 if (isSymbolLinkerVisible(SD->getSymbol()))
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000258 return SD;
259
260 // Absolute and undefined symbols have no defining atom.
261 if (!SD->getFragment())
262 return 0;
263
Daniel Dunbara5f1d572010-05-12 00:38:17 +0000264 // Non-linker visible symbols in sections which can't be atomized have no
265 // defining atom.
266 if (!getBackend().isSectionAtomizable(
267 SD->getFragment()->getParent()->getSection()))
268 return 0;
269
Daniel Dunbar651804c2010-05-11 17:22:50 +0000270 // Otherwise, return the atom for the containing fragment.
271 return SD->getFragment()->getAtom();
Daniel Dunbar8ad0dcc2010-03-19 03:18:15 +0000272}
273
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000274bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000275 const MCFixup &Fixup, const MCFragment *DF,
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000276 MCValue &Target, uint64_t &Value) const {
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000277 ++stats::evaluateFixup;
Daniel Dunbarff547842010-03-23 23:47:14 +0000278
Rafael Espindola33a38a12010-12-22 16:11:57 +0000279 if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout))
Jim Grosbachf3c93672012-01-27 00:51:23 +0000280 getContext().FatalError(Fixup.getLoc(), "expected relocatable expression");
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000281
Rafael Espindolafea753b2010-12-24 21:22:02 +0000282 bool IsPCRel = Backend.getFixupKindInfo(
283 Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
284
285 bool IsResolved;
286 if (IsPCRel) {
287 if (Target.getSymB()) {
288 IsResolved = false;
289 } else if (!Target.getSymA()) {
290 IsResolved = false;
291 } else {
Rafael Espindola908159b2011-02-16 03:25:55 +0000292 const MCSymbolRefExpr *A = Target.getSymA();
293 const MCSymbol &SA = A->getSymbol();
294 if (A->getKind() != MCSymbolRefExpr::VK_None ||
295 SA.AliasedSymbol().isUndefined()) {
Rafael Espindolafea753b2010-12-24 21:22:02 +0000296 IsResolved = false;
297 } else {
298 const MCSymbolData &DataA = getSymbolData(SA);
299 IsResolved =
300 getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
301 *DF, false, true);
302 }
303 }
304 } else {
305 IsResolved = Target.isAbsolute();
306 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000307
308 Value = Target.getConstant();
309
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000310 if (const MCSymbolRefExpr *A = Target.getSymA()) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000311 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
312 if (Sym.isDefined())
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000313 Value += Layout.getSymbolOffset(&getSymbolData(Sym));
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000314 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000315 if (const MCSymbolRefExpr *B = Target.getSymB()) {
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000316 const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
317 if (Sym.isDefined())
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000318 Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
Daniel Dunbar939f8d72010-03-19 03:18:12 +0000319 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000320
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000321
Daniel Dunbar2761fc42010-12-16 03:20:06 +0000322 bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
Owen Anderson47dbd422010-12-15 18:48:27 +0000323 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
324 assert((ShouldAlignPC ? IsPCRel : true) &&
325 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
326
Owen Anderson05018c22010-12-09 20:27:52 +0000327 if (IsPCRel) {
Owen Anderson175fb362010-12-17 21:49:48 +0000328 uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
Jim Grosbach684457d2011-10-26 22:44:41 +0000329
Owen Anderson47dbd422010-12-15 18:48:27 +0000330 // A number of ARM fixups in Thumb mode require that the effective PC
331 // address be determined as the 32-bit aligned version of the actual offset.
Owen Andersond18e0112010-12-15 19:24:24 +0000332 if (ShouldAlignPC) Offset &= ~0x3;
Owen Anderson175fb362010-12-17 21:49:48 +0000333 Value -= Offset;
Owen Anderson05018c22010-12-09 20:27:52 +0000334 }
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000335
Jim Grosbach7b25ecf2012-02-27 21:36:23 +0000336 // Let the backend adjust the fixup value if necessary, including whether
337 // we need a relocation.
338 Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value,
339 IsResolved);
Jim Grosbach47500202010-12-14 18:46:57 +0000340
Daniel Dunbardf3c8f22010-03-12 21:00:49 +0000341 return IsResolved;
342}
343
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000344uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
Rafael Espindola7a459032010-12-21 20:35:18 +0000345 const MCFragment &F) const {
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000346 switch (F.getKind()) {
347 case MCFragment::FT_Data:
348 return cast<MCDataFragment>(F).getContents().size();
349 case MCFragment::FT_Fill:
350 return cast<MCFillFragment>(F).getSize();
351 case MCFragment::FT_Inst:
352 return cast<MCInstFragment>(F).getInstSize();
353
Rafael Espindola3ff57092010-11-02 17:22:24 +0000354 case MCFragment::FT_LEB:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000355 return cast<MCLEBFragment>(F).getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000356
Rafael Espindola7a459032010-12-21 20:35:18 +0000357 case MCFragment::FT_Align: {
358 const MCAlignFragment &AF = cast<MCAlignFragment>(F);
359 unsigned Offset = Layout.getFragmentOffset(&AF);
360 unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
Owen Anderson15b7a982012-08-29 22:18:56 +0000361 // If we are padding with nops, force the padding to be larger than the
362 // minimum nop size.
363 if (Size > 0 && AF.hasEmitNops()) {
364 while (Size % getBackend().getMinimumNopSize())
365 Size += AF.getAlignment();
366 }
Rafael Espindola7a459032010-12-21 20:35:18 +0000367 if (Size > AF.getMaxBytesToEmit())
368 return 0;
369 return Size;
370 }
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000371
Rafael Espindola7a459032010-12-21 20:35:18 +0000372 case MCFragment::FT_Org: {
373 MCOrgFragment &OF = cast<MCOrgFragment>(F);
374 int64_t TargetLocation;
375 if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
376 report_fatal_error("expected assembly-time absolute expression");
377
378 // FIXME: We need a way to communicate this error.
379 uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
380 int64_t Size = TargetLocation - FragmentOffset;
381 if (Size < 0 || Size >= 0x40000000)
382 report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
383 "' (at offset '" + Twine(FragmentOffset) + "')");
384 return Size;
385 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000386
Rafael Espindola187d8332010-11-07 02:07:12 +0000387 case MCFragment::FT_Dwarf:
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000388 return cast<MCDwarfLineAddrFragment>(F).getContents().size();
Rafael Espindola245a1e22010-12-28 05:39:27 +0000389 case MCFragment::FT_DwarfFrame:
390 return cast<MCDwarfCallFrameFragment>(F).getContents().size();
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000391 }
392
Craig Topper85814382012-02-07 05:05:23 +0000393 llvm_unreachable("invalid fragment kind");
Daniel Dunbar2c18d3b2010-05-13 18:35:06 +0000394}
395
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000396void MCAsmLayout::layoutFragment(MCFragment *F) {
Daniel Dunbar9005d452010-05-14 00:37:21 +0000397 MCFragment *Prev = F->getPrevNode();
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000398
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000399 // We should never try to recompute something which is valid.
400 assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
401 // We should never try to compute the fragment layout if its predecessor
402 // isn't valid.
403 assert((!Prev || isFragmentValid(Prev)) &&
404 "Attempt to compute fragment before its predecessor!");
Daniel Dunbarf0d17d22010-05-12 21:35:25 +0000405
406 ++stats::FragmentLayouts;
407
Daniel Dunbarb69fc042010-05-13 20:40:12 +0000408 // Compute fragment offset and size.
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000409 uint64_t Offset = 0;
410 if (Prev)
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000411 Offset += Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000412
413 F->Offset = Offset;
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000414 LastValidFragment[F->getParent()] = F;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000415}
416
Eli Bendersky64d9a322012-12-07 19:13:57 +0000417/// \brief Write the contents of a fragment to the given object writer. Expects
418/// a MCEncodedFragment.
419static void writeFragmentContents(const MCFragment &F, MCObjectWriter *OW) {
420 MCEncodedFragment &EF = cast<MCEncodedFragment>(F);
Eli Bendersky550f0ad2012-12-07 22:06:56 +0000421 OW->WriteBytes(EF.getContents());
Eli Bendersky64d9a322012-12-07 19:13:57 +0000422}
423
424/// \brief Write the fragment \p F to the output file.
425static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout,
426 const MCFragment &F) {
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000427 MCObjectWriter *OW = &Asm.getWriter();
Daniel Dunbar53b23382010-03-19 09:28:59 +0000428 uint64_t Start = OW->getStream().tell();
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000429 (void) Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000430
Daniel Dunbarff547842010-03-23 23:47:14 +0000431 ++stats::EmittedFragments;
Daniel Dunbar0adcd352009-08-25 21:10:45 +0000432
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000433 // FIXME: Embed in fragments instead?
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000434 uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000435 switch (F.getKind()) {
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000436 case MCFragment::FT_Align: {
Eli Bendersky6ac81f52012-12-10 18:59:39 +0000437 ++stats::EmittedAlignFragments;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000438 MCAlignFragment &AF = cast<MCAlignFragment>(F);
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000439 uint64_t Count = FragmentSize / AF.getValueSize();
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000440
Daniel Dunbare73d49e2010-05-12 22:51:27 +0000441 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
442
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000443 // FIXME: This error shouldn't actually occur (the front end should emit
444 // multiple .align directives to enforce the semantics it wants), but is
445 // severe enough that we want to report it. How to handle this?
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000446 if (Count * AF.getValueSize() != FragmentSize)
Chris Lattner75361b62010-04-07 22:58:41 +0000447 report_fatal_error("undefined .align directive, value size '" +
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000448 Twine(AF.getValueSize()) +
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000449 "' is not a divisor of padding size '" +
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000450 Twine(FragmentSize) + "'");
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000451
Kevin Enderby6e720482010-02-23 18:26:34 +0000452 // See if we are aligning with nops, and if so do that first to try to fill
453 // the Count bytes. Then if that did not fill any bytes or there are any
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +0000454 // bytes left to fill use the Value and ValueSize to fill the rest.
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000455 // If we are aligning with nops, ask that target to emit the right data.
Daniel Dunbar1c154132010-05-12 22:56:23 +0000456 if (AF.hasEmitNops()) {
Jim Grosbachec343382012-01-18 18:52:16 +0000457 if (!Asm.getBackend().writeNopData(Count, OW))
Chris Lattner75361b62010-04-07 22:58:41 +0000458 report_fatal_error("unable to write nop sequence of " +
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000459 Twine(Count) + " bytes");
460 break;
Kevin Enderby6e720482010-02-23 18:26:34 +0000461 }
462
Daniel Dunbar8f9b80e2010-03-23 02:36:58 +0000463 // Otherwise, write out in multiples of the value size.
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000464 for (uint64_t i = 0; i != Count; ++i) {
465 switch (AF.getValueSize()) {
Craig Topper85814382012-02-07 05:05:23 +0000466 default: llvm_unreachable("Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000467 case 1: OW->Write8 (uint8_t (AF.getValue())); break;
468 case 2: OW->Write16(uint16_t(AF.getValue())); break;
469 case 4: OW->Write32(uint32_t(AF.getValue())); break;
470 case 8: OW->Write64(uint64_t(AF.getValue())); break;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000471 }
472 }
473 break;
474 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000475
Eli Bendersky64d9a322012-12-07 19:13:57 +0000476 case MCFragment::FT_Data:
Eli Bendersky8ddc5a12012-12-07 17:59:21 +0000477 ++stats::EmittedDataFragments;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000478 writeFragmentContents(F, OW);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000479 break;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000480
481 case MCFragment::FT_Inst:
482 ++stats::EmittedInstFragments;
483 writeFragmentContents(F, OW);
484 break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000485
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000486 case MCFragment::FT_Fill: {
Eli Bendersky6ac81f52012-12-10 18:59:39 +0000487 ++stats::EmittedFillFragments;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000488 MCFillFragment &FF = cast<MCFillFragment>(F);
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000489
490 assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
491
Daniel Dunbar3153fec2010-05-12 22:51:32 +0000492 for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000493 switch (FF.getValueSize()) {
Craig Topper85814382012-02-07 05:05:23 +0000494 default: llvm_unreachable("Invalid size!");
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000495 case 1: OW->Write8 (uint8_t (FF.getValue())); break;
496 case 2: OW->Write16(uint16_t(FF.getValue())); break;
497 case 4: OW->Write32(uint32_t(FF.getValue())); break;
498 case 8: OW->Write64(uint64_t(FF.getValue())); break;
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000499 }
500 }
501 break;
502 }
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000503
Rafael Espindola3ff57092010-11-02 17:22:24 +0000504 case MCFragment::FT_LEB: {
505 MCLEBFragment &LF = cast<MCLEBFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000506 OW->WriteBytes(LF.getContents().str());
Rafael Espindola3ff57092010-11-02 17:22:24 +0000507 break;
508 }
509
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000510 case MCFragment::FT_Org: {
Eli Bendersky6ac81f52012-12-10 18:59:39 +0000511 ++stats::EmittedOrgFragments;
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000512 MCOrgFragment &OF = cast<MCOrgFragment>(F);
513
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000514 for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
Daniel Dunbarbdd92812010-03-19 09:28:55 +0000515 OW->Write8(uint8_t(OF.getValue()));
Daniel Dunbard6f761e2009-08-21 23:07:38 +0000516
517 break;
518 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000519
520 case MCFragment::FT_Dwarf: {
521 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000522 OW->WriteBytes(OF.getContents().str());
Kevin Enderbyc0957932010-09-30 16:52:03 +0000523 break;
524 }
Rafael Espindola245a1e22010-12-28 05:39:27 +0000525 case MCFragment::FT_DwarfFrame: {
526 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
527 OW->WriteBytes(CF.getContents().str());
528 break;
529 }
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000530 }
531
Daniel Dunbar432cd5f2010-03-25 02:00:02 +0000532 assert(OW->getStream().tell() - Start == FragmentSize);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000533}
534
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000535void MCAssembler::writeSectionData(const MCSectionData *SD,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000536 const MCAsmLayout &Layout) const {
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000537 // Ignore virtual sections.
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000538 if (SD->getSection().isVirtualSection()) {
Daniel Dunbar054be922010-05-13 03:50:50 +0000539 assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000540
541 // Check that contents are only things legal inside a virtual section.
542 for (MCSectionData::const_iterator it = SD->begin(),
543 ie = SD->end(); it != ie; ++it) {
544 switch (it->getKind()) {
Craig Topper85814382012-02-07 05:05:23 +0000545 default: llvm_unreachable("Invalid fragment in virtual section!");
Daniel Dunbarc983b202010-08-18 18:22:37 +0000546 case MCFragment::FT_Data: {
547 // Check that we aren't trying to write a non-zero contents (or fixups)
548 // into a virtual section. This is to support clients which use standard
549 // directives to fill the contents of virtual sections.
550 MCDataFragment &DF = cast<MCDataFragment>(*it);
551 assert(DF.fixup_begin() == DF.fixup_end() &&
552 "Cannot have fixups in virtual section!");
553 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
554 assert(DF.getContents()[i] == 0 &&
555 "Invalid data value for virtual section!");
556 break;
557 }
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000558 case MCFragment::FT_Align:
Daniel Dunbarc983b202010-08-18 18:22:37 +0000559 // Check that we aren't trying to write a non-zero value into a virtual
560 // section.
561 assert((!cast<MCAlignFragment>(it)->getValueSize() ||
562 !cast<MCAlignFragment>(it)->getValue()) &&
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000563 "Invalid align in virtual section!");
564 break;
565 case MCFragment::FT_Fill:
566 assert(!cast<MCFillFragment>(it)->getValueSize() &&
567 "Invalid fill in virtual section!");
568 break;
Daniel Dunbare2fee5b2010-05-12 22:51:35 +0000569 }
570 }
571
Daniel Dunbard5a8e982009-08-28 05:49:21 +0000572 return;
573 }
574
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000575 uint64_t Start = getWriter().getStream().tell();
Jim Grosbachb5762bf2012-09-18 23:05:18 +0000576 (void)Start;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000577
Eli Bendersky64d9a322012-12-07 19:13:57 +0000578 for (MCSectionData::const_iterator it = SD->begin(), ie = SD->end();
579 it != ie; ++it)
580 writeFragment(*this, Layout, *it);
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000581
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000582 assert(getWriter().getStream().tell() - Start ==
583 Layout.getSectionAddressSize(SD));
Daniel Dunbar0705fbf2009-08-21 18:29:01 +0000584}
585
Rafael Espindola179821a2010-12-06 19:08:48 +0000586
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000587uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout,
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000588 MCFragment &F,
589 const MCFixup &Fixup) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000590 // Evaluate the fixup.
591 MCValue Target;
592 uint64_t FixedValue;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000593 if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000594 // The fixup was unresolved, we need a relocation. Inform the object
595 // writer of the relocation, and give it an opportunity to adjust the
596 // fixup value if need be.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000597 getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
Rafael Espindola179821a2010-12-06 19:08:48 +0000598 }
599 return FixedValue;
600 }
601
Daniel Dunbarfeb7ba32010-12-17 02:45:41 +0000602void MCAssembler::Finish() {
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000603 DEBUG_WITH_TYPE("mc-dump", {
604 llvm::errs() << "assembler backend - pre-layout\n--\n";
605 dump(); });
606
Daniel Dunbar61066db2010-05-13 02:34:14 +0000607 // Create the layout object.
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000608 MCAsmLayout Layout(*this);
Daniel Dunbar61066db2010-05-13 02:34:14 +0000609
Daniel Dunbar337718e2010-05-14 00:37:14 +0000610 // Create dummy fragments and assign section ordinals.
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000611 unsigned SectionIndex = 0;
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000612 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
613 // Create dummy fragments to eliminate any empty sections, this simplifies
614 // layout.
Duncan Sands6f74f692010-06-29 13:30:08 +0000615 if (it->getFragmentList().empty())
Rafael Espindolad80781b2010-09-15 21:48:40 +0000616 new MCDataFragment(it);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000617
618 it->setOrdinal(SectionIndex++);
Daniel Dunbar337718e2010-05-14 00:37:14 +0000619 }
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000620
Daniel Dunbar337718e2010-05-14 00:37:14 +0000621 // Assign layout order indices to sections and fragments.
Daniel Dunbar337718e2010-05-14 00:37:14 +0000622 for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
623 MCSectionData *SD = Layout.getSectionOrder()[i];
624 SD->setLayoutOrder(i);
625
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000626 unsigned FragmentIndex = 0;
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000627 for (MCSectionData::iterator iFrag = SD->begin(), iFragEnd = SD->end();
628 iFrag != iFragEnd; ++iFrag)
629 iFrag->setLayoutOrder(FragmentIndex++);
Daniel Dunbar49ed9212010-05-13 08:43:37 +0000630 }
631
Daniel Dunbar61066db2010-05-13 02:34:14 +0000632 // Layout until everything fits.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000633 while (layoutOnce(Layout))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000634 continue;
635
636 DEBUG_WITH_TYPE("mc-dump", {
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000637 llvm::errs() << "assembler backend - post-relaxation\n--\n";
638 dump(); });
639
640 // Finalize the layout, including fragment lowering.
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000641 finishLayout(Layout);
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000642
643 DEBUG_WITH_TYPE("mc-dump", {
644 llvm::errs() << "assembler backend - final-layout\n--\n";
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000645 dump(); });
646
Daniel Dunbarff547842010-03-23 23:47:14 +0000647 uint64_t StartOffset = OS.tell();
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000648
Daniel Dunbarbacba992010-03-19 07:09:33 +0000649 // Allow the object writer a chance to perform post-layout binding (for
650 // example, to set the index fields in the symbol data).
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000651 getWriter().ExecutePostLayoutBinding(*this, Layout);
Daniel Dunbarbacba992010-03-19 07:09:33 +0000652
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000653 // Evaluate and apply the fixups, generating relocation entries as necessary.
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000654 for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
655 for (MCSectionData::iterator it2 = it->begin(),
656 ie2 = it->end(); it2 != ie2; ++it2) {
Eli Bendersky64d9a322012-12-07 19:13:57 +0000657 MCEncodedFragment *F = dyn_cast<MCEncodedFragment>(it2);
658 if (F) {
659 for (MCEncodedFragment::fixup_iterator it3 = F->fixup_begin(),
660 ie3 = F->fixup_end(); it3 != ie3; ++it3) {
Rafael Espindola179821a2010-12-06 19:08:48 +0000661 MCFixup &Fixup = *it3;
Eli Bendersky64d9a322012-12-07 19:13:57 +0000662 uint64_t FixedValue = handleFixup(Layout, *F, Fixup);
663 getBackend().applyFixup(Fixup, F->getContents().data(),
664 F->getContents().size(), FixedValue);
Rafael Espindola179821a2010-12-06 19:08:48 +0000665 }
Daniel Dunbarb1e98942010-03-19 07:09:47 +0000666 }
667 }
668 }
669
Daniel Dunbarbacba992010-03-19 07:09:33 +0000670 // Write the object file.
Daniel Dunbar5d2477c2010-12-17 02:45:59 +0000671 getWriter().WriteObject(*this, Layout);
Daniel Dunbarff547842010-03-23 23:47:14 +0000672
673 stats::ObjectBytes += OS.tell() - StartOffset;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000674}
675
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000676bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
Jim Grosbach370b78d2011-12-06 00:47:03 +0000677 const MCInstFragment *DF,
Daniel Dunbar8d39eb42010-03-22 20:35:35 +0000678 const MCAsmLayout &Layout) const {
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000679 // If we cannot resolve the fixup value, it requires relaxation.
680 MCValue Target;
681 uint64_t Value;
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000682 if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000683 return true;
684
Jim Grosbach370b78d2011-12-06 00:47:03 +0000685 return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000686}
687
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000688bool MCAssembler::fragmentNeedsRelaxation(const MCInstFragment *IF,
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000689 const MCAsmLayout &Layout) const {
690 // If this inst doesn't ever need relaxation, ignore it. This occurs when we
691 // are intentionally pushing out inst fragments, or because we relaxed a
692 // previous instruction to one that doesn't need relaxation.
Jim Grosbachec343382012-01-18 18:52:16 +0000693 if (!getBackend().mayNeedRelaxation(IF->getInst()))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000694 return false;
695
696 for (MCInstFragment::const_fixup_iterator it = IF->fixup_begin(),
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000697 ie = IF->fixup_end(); it != ie; ++it)
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000698 if (fixupNeedsRelaxation(*it, IF, Layout))
Daniel Dunbard8036fb2010-03-23 05:09:03 +0000699 return true;
700
701 return false;
702}
703
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000704bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
Rafael Espindola3ff57092010-11-02 17:22:24 +0000705 MCInstFragment &IF) {
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000706 if (!fragmentNeedsRelaxation(&IF, Layout))
Rafael Espindola3ff57092010-11-02 17:22:24 +0000707 return false;
708
709 ++stats::RelaxedInstructions;
710
711 // FIXME-PERF: We could immediately lower out instructions if we can tell
712 // they are fully resolved, to avoid retesting on later passes.
713
714 // Relax the fragment.
715
716 MCInst Relaxed;
Jim Grosbachec343382012-01-18 18:52:16 +0000717 getBackend().relaxInstruction(IF.getInst(), Relaxed);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000718
719 // Encode the new instruction.
720 //
721 // FIXME-PERF: If it matters, we could let the target do this. It can
722 // probably do so more efficiently in many cases.
723 SmallVector<MCFixup, 4> Fixups;
724 SmallString<256> Code;
725 raw_svector_ostream VecOS(Code);
726 getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
727 VecOS.flush();
728
729 // Update the instruction fragment.
Rafael Espindola3ff57092010-11-02 17:22:24 +0000730 IF.setInst(Relaxed);
Eli Bendersky64d9a322012-12-07 19:13:57 +0000731 IF.getContents() = Code;
732 IF.getFixups() = Fixups;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000733
Rafael Espindola3ff57092010-11-02 17:22:24 +0000734 return true;
735}
736
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000737bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000738 int64_t Value = 0;
739 uint64_t OldSize = LF.getContents().size();
Rafael Espindolad88cac02011-04-26 02:17:58 +0000740 bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout);
741 (void)IsAbs;
742 assert(IsAbs);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000743 SmallString<8> &Data = LF.getContents();
744 Data.clear();
745 raw_svector_ostream OSE(Data);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000746 if (LF.isSigned())
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000747 encodeSLEB128(Value, OSE);
Rafael Espindola3ff57092010-11-02 17:22:24 +0000748 else
Jim Grosbach2d39a0e2012-08-08 23:56:06 +0000749 encodeULEB128(Value, OSE);
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000750 OSE.flush();
751 return OldSize != LF.getContents().size();
Rafael Espindola3ff57092010-11-02 17:22:24 +0000752}
753
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000754bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000755 MCDwarfLineAddrFragment &DF) {
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000756 int64_t AddrDelta = 0;
757 uint64_t OldSize = DF.getContents().size();
Rafael Espindola835439a2010-12-22 22:04:28 +0000758 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
759 (void)IsAbs;
760 assert(IsAbs);
Rafael Espindola187d8332010-11-07 02:07:12 +0000761 int64_t LineDelta;
762 LineDelta = DF.getLineDelta();
Rafael Espindoladb74aea2010-12-04 21:58:52 +0000763 SmallString<8> &Data = DF.getContents();
764 Data.clear();
765 raw_svector_ostream OSE(Data);
766 MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE);
767 OSE.flush();
768 return OldSize != Data.size();
Rafael Espindola187d8332010-11-07 02:07:12 +0000769}
770
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000771bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
Rafael Espindola245a1e22010-12-28 05:39:27 +0000772 MCDwarfCallFrameFragment &DF) {
773 int64_t AddrDelta = 0;
774 uint64_t OldSize = DF.getContents().size();
775 bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
776 (void)IsAbs;
777 assert(IsAbs);
778 SmallString<8> &Data = DF.getContents();
779 Data.clear();
780 raw_svector_ostream OSE(Data);
Rafael Espindola4eafe102011-05-08 14:35:21 +0000781 MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE);
Rafael Espindola245a1e22010-12-28 05:39:27 +0000782 OSE.flush();
783 return OldSize != Data.size();
784}
785
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000786bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD) {
787 // Holds the first fragment which needed relaxing during this layout. It will
788 // remain NULL if none were relaxed.
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000789 // When a fragment is relaxed, all the fragments following it should get
790 // invalidated because their offset is going to change.
791 MCFragment *FirstRelaxedFragment = NULL;
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000792
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000793 // Attempt to relax all the fragments in the section.
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000794 for (MCSectionData::iterator I = SD.begin(), IE = SD.end(); I != IE; ++I) {
795 // Check if this is a fragment that needs relaxation.
796 bool RelaxedFrag = false;
797 switch(I->getKind()) {
Rafael Espindola62b83b62010-12-21 04:22:09 +0000798 default:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000799 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000800 case MCFragment::FT_Inst:
Eli Bendersky37a98302012-12-11 17:16:00 +0000801 assert(!getRelaxAll() &&
802 "Did not expect a MCInstFragment in RelaxAll mode");
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000803 RelaxedFrag = relaxInstruction(Layout, *cast<MCInstFragment>(I));
Rafael Espindola62b83b62010-12-21 04:22:09 +0000804 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000805 case MCFragment::FT_Dwarf:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000806 RelaxedFrag = relaxDwarfLineAddr(Layout,
807 *cast<MCDwarfLineAddrFragment>(I));
Rafael Espindola62b83b62010-12-21 04:22:09 +0000808 break;
Rafael Espindola245a1e22010-12-28 05:39:27 +0000809 case MCFragment::FT_DwarfFrame:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000810 RelaxedFrag =
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000811 relaxDwarfCallFrameFragment(Layout,
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000812 *cast<MCDwarfCallFrameFragment>(I));
Rafael Espindola245a1e22010-12-28 05:39:27 +0000813 break;
814 case MCFragment::FT_LEB:
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000815 RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
Rafael Espindola245a1e22010-12-28 05:39:27 +0000816 break;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000817 }
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000818 if (RelaxedFrag && !FirstRelaxedFragment)
819 FirstRelaxedFragment = I;
Rafael Espindola62b83b62010-12-21 04:22:09 +0000820 }
Eli Benderskyd52a2c02012-12-12 19:54:05 +0000821 if (FirstRelaxedFragment) {
822 Layout.invalidateFragmentsAfter(FirstRelaxedFragment);
Rafael Espindola62b83b62010-12-21 04:22:09 +0000823 return true;
824 }
825 return false;
826}
827
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000828bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
Daniel Dunbarff547842010-03-23 23:47:14 +0000829 ++stats::RelaxationSteps;
830
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000831 bool WasRelaxed = false;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000832 for (iterator it = begin(), ie = end(); it != ie; ++it) {
833 MCSectionData &SD = *it;
Eli Benderskyf43e3fd2012-12-10 20:13:43 +0000834 while (layoutSectionOnce(Layout, SD))
Rafael Espindola62b83b62010-12-21 04:22:09 +0000835 WasRelaxed = true;
Daniel Dunbarf08fde42010-03-12 22:07:14 +0000836 }
837
Daniel Dunbar0cc8bd42010-03-25 19:35:56 +0000838 return WasRelaxed;
Daniel Dunbarfb4a6b32009-08-21 09:11:24 +0000839}
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000840
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000841void MCAssembler::finishLayout(MCAsmLayout &Layout) {
Rafael Espindolab4172fa2010-12-04 22:47:22 +0000842 // The layout is done. Mark every fragment as valid.
Rafael Espindola4f4363a2010-12-07 23:32:26 +0000843 for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
844 Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
845 }
Daniel Dunbar3f4dcd92010-03-22 23:16:48 +0000846}
847
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000848// Debugging methods
849
850namespace llvm {
851
Daniel Dunbarc90e30a2010-05-26 15:18:56 +0000852raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
853 OS << "<MCFixup" << " Offset:" << AF.getOffset()
Daniel Dunbar482ad802010-05-26 15:18:31 +0000854 << " Value:" << *AF.getValue()
855 << " Kind:" << AF.getKind() << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000856 return OS;
857}
858
859}
860
Manman Ren286c4dc2012-09-12 05:06:18 +0000861#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000862void MCFragment::dump() {
863 raw_ostream &OS = llvm::errs();
864
Daniel Dunbare614e392010-05-26 06:50:57 +0000865 OS << "<";
866 switch (getKind()) {
867 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
868 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
869 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
870 case MCFragment::FT_Inst: OS << "MCInstFragment"; break;
871 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
Kevin Enderbyc0957932010-09-30 16:52:03 +0000872 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
Rafael Espindola245a1e22010-12-28 05:39:27 +0000873 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
Rafael Espindola3ff57092010-11-02 17:22:24 +0000874 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
Daniel Dunbare614e392010-05-26 06:50:57 +0000875 }
876
Daniel Dunbar337718e2010-05-14 00:37:14 +0000877 OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
Rafael Espindola2bf6afc2010-12-15 08:45:53 +0000878 << " Offset:" << Offset << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000879
Daniel Dunbare614e392010-05-26 06:50:57 +0000880 switch (getKind()) {
881 case MCFragment::FT_Align: {
882 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
883 if (AF->hasEmitNops())
884 OS << " (emit nops)";
Daniel Dunbare614e392010-05-26 06:50:57 +0000885 OS << "\n ";
886 OS << " Alignment:" << AF->getAlignment()
887 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
888 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
889 break;
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000890 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000891 case MCFragment::FT_Data: {
892 const MCDataFragment *DF = cast<MCDataFragment>(this);
893 OS << "\n ";
894 OS << " Contents:[";
895 const SmallVectorImpl<char> &Contents = DF->getContents();
896 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
897 if (i) OS << ",";
898 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000899 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000900 OS << "] (" << Contents.size() << " bytes)";
901
Eli Bendersky5c10f502012-12-05 22:11:02 +0000902 if (DF->fixup_begin() != DF->fixup_end()) {
Daniel Dunbare614e392010-05-26 06:50:57 +0000903 OS << ",\n ";
904 OS << " Fixups:[";
905 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
906 ie = DF->fixup_end(); it != ie; ++it) {
907 if (it != DF->fixup_begin()) OS << ",\n ";
908 OS << *it;
909 }
910 OS << "]";
911 }
912 break;
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000913 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000914 case MCFragment::FT_Fill: {
915 const MCFillFragment *FF = cast<MCFillFragment>(this);
916 OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
917 << " Size:" << FF->getSize();
918 break;
919 }
920 case MCFragment::FT_Inst: {
921 const MCInstFragment *IF = cast<MCInstFragment>(this);
922 OS << "\n ";
923 OS << " Inst:";
924 IF->getInst().dump_pretty(OS);
925 break;
926 }
927 case MCFragment::FT_Org: {
928 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
929 OS << "\n ";
930 OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
931 break;
932 }
Kevin Enderbyc0957932010-09-30 16:52:03 +0000933 case MCFragment::FT_Dwarf: {
934 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
935 OS << "\n ";
936 OS << " AddrDelta:" << OF->getAddrDelta()
937 << " LineDelta:" << OF->getLineDelta();
938 break;
939 }
Rafael Espindola245a1e22010-12-28 05:39:27 +0000940 case MCFragment::FT_DwarfFrame: {
941 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
942 OS << "\n ";
943 OS << " AddrDelta:" << CF->getAddrDelta();
944 break;
945 }
Rafael Espindola3ff57092010-11-02 17:22:24 +0000946 case MCFragment::FT_LEB: {
947 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
948 OS << "\n ";
949 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
950 break;
951 }
Daniel Dunbare614e392010-05-26 06:50:57 +0000952 }
Daniel Dunbar0bcf0742010-02-13 09:28:43 +0000953 OS << ">";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000954}
955
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000956void MCSectionData::dump() {
957 raw_ostream &OS = llvm::errs();
958
959 OS << "<MCSectionData";
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000960 OS << " Alignment:" << getAlignment() << " Fragments:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000961 for (iterator it = begin(), ie = end(); it != ie; ++it) {
962 if (it != begin()) OS << ",\n ";
963 it->dump();
964 }
965 OS << "]>";
966}
967
968void MCSymbolData::dump() {
969 raw_ostream &OS = llvm::errs();
970
971 OS << "<MCSymbolData Symbol:" << getSymbol()
972 << " Fragment:" << getFragment() << " Offset:" << getOffset()
973 << " Flags:" << getFlags() << " Index:" << getIndex();
974 if (isCommon())
975 OS << " (common, size:" << getCommonSize()
976 << " align: " << getCommonAlignment() << ")";
977 if (isExternal())
978 OS << " (external)";
979 if (isPrivateExtern())
980 OS << " (private extern)";
981 OS << ">";
982}
983
984void MCAssembler::dump() {
985 raw_ostream &OS = llvm::errs();
986
987 OS << "<MCAssembler\n";
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000988 OS << " Sections:[\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000989 for (iterator it = begin(), ie = end(); it != ie; ++it) {
990 if (it != begin()) OS << ",\n ";
991 it->dump();
992 }
993 OS << "],\n";
994 OS << " Symbols:[";
995
996 for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Daniel Dunbar45aefff2010-03-09 01:12:23 +0000997 if (it != symbol_begin()) OS << ",\n ";
Daniel Dunbarb7c3a4b2010-02-13 09:28:03 +0000998 it->dump();
999 }
1000 OS << "]>\n";
1001}
Manman Rencc77eec2012-09-06 19:55:56 +00001002#endif
David Blaikie2d24e2a2011-12-20 02:50:00 +00001003
1004// anchors for MC*Fragment vtables
Eli Bendersky64d9a322012-12-07 19:13:57 +00001005void MCEncodedFragment::anchor() { }
David Blaikie2d24e2a2011-12-20 02:50:00 +00001006void MCDataFragment::anchor() { }
1007void MCInstFragment::anchor() { }
1008void MCAlignFragment::anchor() { }
1009void MCFillFragment::anchor() { }
1010void MCOrgFragment::anchor() { }
1011void MCLEBFragment::anchor() { }
1012void MCDwarfLineAddrFragment::anchor() { }
1013void MCDwarfCallFrameFragment::anchor() { }