blob: e7775ca3ab1be0d53c1039cfc453ea843d086760 [file] [log] [blame]
Chandler Carruth8d736232015-12-29 09:06:16 +00001//===- lib/MC/MCFragment.cpp - Assembler Fragment 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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/MC/MCFragment.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000011#include "llvm/ADT/SmallVector.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000012#include "llvm/ADT/StringExtras.h"
13#include "llvm/ADT/Twine.h"
Nico Weber432a3882018-04-30 14:59:11 +000014#include "llvm/Config/llvm-config.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000015#include "llvm/MC/MCAsmLayout.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/MC/MCAssembler.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000017#include "llvm/MC/MCContext.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000018#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000019#include "llvm/MC/MCFixup.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000020#include "llvm/MC/MCSection.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000021#include "llvm/MC/MCSymbol.h"
22#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000023#include "llvm/Support/Casting.h"
24#include "llvm/Support/Compiler.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000025#include "llvm/Support/ErrorHandling.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000026#include "llvm/Support/raw_ostream.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000027#include <cassert>
28#include <cstdint>
29#include <utility>
30
Chandler Carruth8d736232015-12-29 09:06:16 +000031using namespace llvm;
32
Eugene Zelenko1d435522017-02-07 23:02:00 +000033MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) {
Chandler Carruth8d736232015-12-29 09:06:16 +000034 // Compute the section layout order. Virtual sections must go last.
35 for (MCSection &Sec : Asm)
36 if (!Sec.isVirtualSection())
37 SectionOrder.push_back(&Sec);
38 for (MCSection &Sec : Asm)
39 if (Sec.isVirtualSection())
40 SectionOrder.push_back(&Sec);
41}
42
43bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
44 const MCSection *Sec = F->getParent();
45 const MCFragment *LastValid = LastValidFragment.lookup(Sec);
46 if (!LastValid)
47 return false;
48 assert(LastValid->getParent() == Sec);
49 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
50}
51
52void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) {
53 // If this fragment wasn't already valid, we don't need to do anything.
54 if (!isFragmentValid(F))
55 return;
56
57 // Otherwise, reset the last valid fragment to the previous fragment
58 // (if this is the first fragment, it will be NULL).
59 LastValidFragment[F->getParent()] = F->getPrevNode();
60}
61
62void MCAsmLayout::ensureValid(const MCFragment *F) const {
63 MCSection *Sec = F->getParent();
64 MCSection::iterator I;
65 if (MCFragment *Cur = LastValidFragment[Sec])
66 I = ++MCSection::iterator(Cur);
67 else
68 I = Sec->begin();
69
70 // Advance the layout position until the fragment is valid.
71 while (!isFragmentValid(F)) {
72 assert(I != Sec->end() && "Layout bookkeeping error");
73 const_cast<MCAsmLayout *>(this)->layoutFragment(&*I);
74 ++I;
75 }
76}
77
78uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
79 ensureValid(F);
80 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
81 return F->Offset;
82}
83
Mandeep Singh Grang1be19e62017-09-15 20:01:43 +000084// Simple getSymbolOffset helper for the non-variable case.
Chandler Carruth8d736232015-12-29 09:06:16 +000085static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S,
86 bool ReportError, uint64_t &Val) {
87 if (!S.getFragment()) {
88 if (ReportError)
89 report_fatal_error("unable to evaluate offset to undefined symbol '" +
90 S.getName() + "'");
91 return false;
92 }
93 Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset();
94 return true;
95}
96
97static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S,
98 bool ReportError, uint64_t &Val) {
99 if (!S.isVariable())
100 return getLabelOffset(Layout, S, ReportError, Val);
101
102 // If SD is a variable, evaluate it.
103 MCValue Target;
104 if (!S.getVariableValue()->evaluateAsValue(Target, Layout))
105 report_fatal_error("unable to evaluate offset for variable '" +
106 S.getName() + "'");
107
108 uint64_t Offset = Target.getConstant();
109
110 const MCSymbolRefExpr *A = Target.getSymA();
111 if (A) {
112 uint64_t ValA;
113 if (!getLabelOffset(Layout, A->getSymbol(), ReportError, ValA))
114 return false;
115 Offset += ValA;
116 }
117
118 const MCSymbolRefExpr *B = Target.getSymB();
119 if (B) {
120 uint64_t ValB;
121 if (!getLabelOffset(Layout, B->getSymbol(), ReportError, ValB))
122 return false;
123 Offset -= ValB;
124 }
125
126 Val = Offset;
127 return true;
128}
129
130bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const {
131 return getSymbolOffsetImpl(*this, S, false, Val);
132}
133
134uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const {
135 uint64_t Val;
136 getSymbolOffsetImpl(*this, S, true, Val);
137 return Val;
138}
139
140const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
141 if (!Symbol.isVariable())
142 return &Symbol;
143
144 const MCExpr *Expr = Symbol.getVariableValue();
145 MCValue Value;
146 if (!Expr->evaluateAsValue(Value, *this)) {
147 Assembler.getContext().reportError(
Chad Rosier9245e122017-01-19 20:06:32 +0000148 Expr->getLoc(), "expression could not be evaluated");
Chandler Carruth8d736232015-12-29 09:06:16 +0000149 return nullptr;
150 }
151
152 const MCSymbolRefExpr *RefB = Value.getSymB();
153 if (RefB) {
154 Assembler.getContext().reportError(
Chad Rosier9245e122017-01-19 20:06:32 +0000155 Expr->getLoc(), Twine("symbol '") + RefB->getSymbol().getName() +
Chandler Carruth8d736232015-12-29 09:06:16 +0000156 "' could not be evaluated in a subtraction expression");
157 return nullptr;
158 }
159
160 const MCSymbolRefExpr *A = Value.getSymA();
161 if (!A)
162 return nullptr;
163
164 const MCSymbol &ASym = A->getSymbol();
165 const MCAssembler &Asm = getAssembler();
166 if (ASym.isCommon()) {
Chad Rosier9245e122017-01-19 20:06:32 +0000167 Asm.getContext().reportError(Expr->getLoc(),
Chandler Carruth8d736232015-12-29 09:06:16 +0000168 "Common symbol '" + ASym.getName() +
169 "' cannot be used in assignment expr");
170 return nullptr;
171 }
172
173 return &ASym;
174}
175
176uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const {
177 // The size is the last fragment's end offset.
178 const MCFragment &F = Sec->getFragmentList().back();
179 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
180}
181
182uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
183 // Virtual sections have no file size.
184 if (Sec->isVirtualSection())
185 return 0;
186
187 // Otherwise, the file size is the same as the address space size.
188 return getSectionAddressSize(Sec);
189}
190
191uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler,
192 const MCFragment *F,
193 uint64_t FOffset, uint64_t FSize) {
194 uint64_t BundleSize = Assembler.getBundleAlignSize();
195 assert(BundleSize > 0 &&
196 "computeBundlePadding should only be called if bundling is enabled");
197 uint64_t BundleMask = BundleSize - 1;
198 uint64_t OffsetInBundle = FOffset & BundleMask;
199 uint64_t EndOfFragment = OffsetInBundle + FSize;
200
201 // There are two kinds of bundling restrictions:
202 //
203 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
204 // *end* on a bundle boundary.
205 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
206 // would, add padding until the end of the bundle so that the fragment
207 // will start in a new one.
208 if (F->alignToBundleEnd()) {
209 // Three possibilities here:
210 //
211 // A) The fragment just happens to end at a bundle boundary, so we're good.
212 // B) The fragment ends before the current bundle boundary: pad it just
213 // enough to reach the boundary.
214 // C) The fragment ends after the current bundle boundary: pad it until it
215 // reaches the end of the next bundle boundary.
216 //
217 // Note: this code could be made shorter with some modulo trickery, but it's
218 // intentionally kept in its more explicit form for simplicity.
219 if (EndOfFragment == BundleSize)
220 return 0;
221 else if (EndOfFragment < BundleSize)
222 return BundleSize - EndOfFragment;
223 else { // EndOfFragment > BundleSize
224 return 2 * BundleSize - EndOfFragment;
225 }
226 } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
227 return BundleSize - OffsetInBundle;
228 else
229 return 0;
230}
231
232/* *** */
233
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000234void ilist_alloc_traits<MCFragment>::deleteNode(MCFragment *V) { V->destroy(); }
Chandler Carruth8d736232015-12-29 09:06:16 +0000235
Eugene Zelenko1d435522017-02-07 23:02:00 +0000236MCFragment::~MCFragment() = default;
Chandler Carruth8d736232015-12-29 09:06:16 +0000237
238MCFragment::MCFragment(FragmentType Kind, bool HasInstructions,
239 uint8_t BundlePadding, MCSection *Parent)
240 : Kind(Kind), HasInstructions(HasInstructions), AlignToBundleEnd(false),
241 BundlePadding(BundlePadding), Parent(Parent), Atom(nullptr),
242 Offset(~UINT64_C(0)) {
243 if (Parent && !isDummy())
244 Parent->getFragmentList().push_back(this);
245}
246
247void MCFragment::destroy() {
248 // First check if we are the sentinal.
249 if (Kind == FragmentType(~0)) {
250 delete this;
251 return;
252 }
253
254 switch (Kind) {
255 case FT_Align:
256 delete cast<MCAlignFragment>(this);
257 return;
258 case FT_Data:
259 delete cast<MCDataFragment>(this);
260 return;
261 case FT_CompactEncodedInst:
262 delete cast<MCCompactEncodedInstFragment>(this);
263 return;
264 case FT_Fill:
265 delete cast<MCFillFragment>(this);
266 return;
267 case FT_Relaxable:
268 delete cast<MCRelaxableFragment>(this);
269 return;
270 case FT_Org:
271 delete cast<MCOrgFragment>(this);
272 return;
273 case FT_Dwarf:
274 delete cast<MCDwarfLineAddrFragment>(this);
275 return;
276 case FT_DwarfFrame:
277 delete cast<MCDwarfCallFrameFragment>(this);
278 return;
279 case FT_LEB:
280 delete cast<MCLEBFragment>(this);
281 return;
Omer Paparo Bivas2251c792017-10-24 06:16:03 +0000282 case FT_Padding:
283 delete cast<MCPaddingFragment>(this);
284 return;
Adrian McCarthy75248a72017-11-08 18:57:02 +0000285 case FT_SymbolId:
286 delete cast<MCSymbolIdFragment>(this);
Chandler Carruth8d736232015-12-29 09:06:16 +0000287 return;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000288 case FT_CVInlineLines:
289 delete cast<MCCVInlineLineTableFragment>(this);
290 return;
David Majnemer408b5e62016-02-05 01:55:49 +0000291 case FT_CVDefRange:
292 delete cast<MCCVDefRangeFragment>(this);
293 return;
Chandler Carruth8d736232015-12-29 09:06:16 +0000294 case FT_Dummy:
295 delete cast<MCDummyFragment>(this);
296 return;
297 }
298}
299
Chandler Carruth8d736232015-12-29 09:06:16 +0000300// Debugging methods
301
302namespace llvm {
303
304raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
305 OS << "<MCFixup" << " Offset:" << AF.getOffset()
306 << " Value:" << *AF.getValue()
307 << " Kind:" << AF.getKind() << ">";
308 return OS;
309}
310
Eugene Zelenko1d435522017-02-07 23:02:00 +0000311} // end namespace llvm
Chandler Carruth8d736232015-12-29 09:06:16 +0000312
Aaron Ballman615eb472017-10-15 14:32:27 +0000313#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +0000314LLVM_DUMP_METHOD void MCFragment::dump() const {
Eugene Zelenko1d435522017-02-07 23:02:00 +0000315 raw_ostream &OS = errs();
Chandler Carruth8d736232015-12-29 09:06:16 +0000316
317 OS << "<";
318 switch (getKind()) {
319 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
320 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
321 case MCFragment::FT_CompactEncodedInst:
322 OS << "MCCompactEncodedInstFragment"; break;
323 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
324 case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
325 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
326 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
327 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
328 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
Omer Paparo Bivas2251c792017-10-24 06:16:03 +0000329 case MCFragment::FT_Padding: OS << "MCPaddingFragment"; break;
Adrian McCarthy75248a72017-11-08 18:57:02 +0000330 case MCFragment::FT_SymbolId: OS << "MCSymbolIdFragment"; break;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000331 case MCFragment::FT_CVInlineLines: OS << "MCCVInlineLineTableFragment"; break;
David Majnemer408b5e62016-02-05 01:55:49 +0000332 case MCFragment::FT_CVDefRange: OS << "MCCVDefRangeTableFragment"; break;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000333 case MCFragment::FT_Dummy: OS << "MCDummyFragment"; break;
Chandler Carruth8d736232015-12-29 09:06:16 +0000334 }
335
Ekaterina Vaartisc4a63222017-06-22 19:08:30 +0000336 OS << "<MCFragment " << (const void*) this << " LayoutOrder:" << LayoutOrder
Chandler Carruth8d736232015-12-29 09:06:16 +0000337 << " Offset:" << Offset
Ekaterina Vaartisc4a63222017-06-22 19:08:30 +0000338 << " HasInstructions:" << hasInstructions()
Chandler Carruth8d736232015-12-29 09:06:16 +0000339 << " BundlePadding:" << static_cast<unsigned>(getBundlePadding()) << ">";
340
341 switch (getKind()) {
342 case MCFragment::FT_Align: {
343 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
344 if (AF->hasEmitNops())
345 OS << " (emit nops)";
346 OS << "\n ";
347 OS << " Alignment:" << AF->getAlignment()
348 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
349 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
350 break;
351 }
352 case MCFragment::FT_Data: {
353 const MCDataFragment *DF = cast<MCDataFragment>(this);
354 OS << "\n ";
355 OS << " Contents:[";
356 const SmallVectorImpl<char> &Contents = DF->getContents();
357 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
358 if (i) OS << ",";
359 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
360 }
361 OS << "] (" << Contents.size() << " bytes)";
362
363 if (DF->fixup_begin() != DF->fixup_end()) {
364 OS << ",\n ";
365 OS << " Fixups:[";
366 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
367 ie = DF->fixup_end(); it != ie; ++it) {
368 if (it != DF->fixup_begin()) OS << ",\n ";
369 OS << *it;
370 }
371 OS << "]";
372 }
373 break;
374 }
375 case MCFragment::FT_CompactEncodedInst: {
376 const MCCompactEncodedInstFragment *CEIF =
377 cast<MCCompactEncodedInstFragment>(this);
378 OS << "\n ";
379 OS << " Contents:[";
380 const SmallVectorImpl<char> &Contents = CEIF->getContents();
381 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
382 if (i) OS << ",";
383 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
384 }
385 OS << "] (" << Contents.size() << " bytes)";
386 break;
387 }
388 case MCFragment::FT_Fill: {
389 const MCFillFragment *FF = cast<MCFillFragment>(this);
Sam Clegg58ad080e2017-06-22 17:57:01 +0000390 OS << " Value:" << static_cast<unsigned>(FF->getValue())
391 << " Size:" << FF->getSize();
Chandler Carruth8d736232015-12-29 09:06:16 +0000392 break;
393 }
394 case MCFragment::FT_Relaxable: {
395 const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
396 OS << "\n ";
397 OS << " Inst:";
398 F->getInst().dump_pretty(OS);
399 break;
400 }
401 case MCFragment::FT_Org: {
402 const MCOrgFragment *OF = cast<MCOrgFragment>(this);
403 OS << "\n ";
Sam Clegg58ad080e2017-06-22 17:57:01 +0000404 OS << " Offset:" << OF->getOffset()
405 << " Value:" << static_cast<unsigned>(OF->getValue());
Chandler Carruth8d736232015-12-29 09:06:16 +0000406 break;
407 }
408 case MCFragment::FT_Dwarf: {
409 const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
410 OS << "\n ";
411 OS << " AddrDelta:" << OF->getAddrDelta()
412 << " LineDelta:" << OF->getLineDelta();
413 break;
414 }
415 case MCFragment::FT_DwarfFrame: {
416 const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
417 OS << "\n ";
418 OS << " AddrDelta:" << CF->getAddrDelta();
419 break;
420 }
421 case MCFragment::FT_LEB: {
422 const MCLEBFragment *LF = cast<MCLEBFragment>(this);
423 OS << "\n ";
424 OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
425 break;
426 }
Omer Paparo Bivas2251c792017-10-24 06:16:03 +0000427 case MCFragment::FT_Padding: {
428 const MCPaddingFragment *F = cast<MCPaddingFragment>(this);
429 OS << "\n ";
430 OS << " PaddingPoliciesMask:" << F->getPaddingPoliciesMask()
431 << " IsInsertionPoint:" << F->isInsertionPoint()
432 << " Size:" << F->getSize();
433 OS << "\n ";
434 OS << " Inst:";
435 F->getInst().dump_pretty(OS);
436 OS << " InstSize:" << F->getInstSize();
437 OS << "\n ";
438 break;
439 }
Adrian McCarthy75248a72017-11-08 18:57:02 +0000440 case MCFragment::FT_SymbolId: {
441 const MCSymbolIdFragment *F = cast<MCSymbolIdFragment>(this);
Chandler Carruth8d736232015-12-29 09:06:16 +0000442 OS << "\n ";
443 OS << " Sym:" << F->getSymbol();
444 break;
445 }
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000446 case MCFragment::FT_CVInlineLines: {
447 const auto *F = cast<MCCVInlineLineTableFragment>(this);
448 OS << "\n ";
449 OS << " Sym:" << *F->getFnStartSym();
450 break;
451 }
David Majnemer408b5e62016-02-05 01:55:49 +0000452 case MCFragment::FT_CVDefRange: {
453 const auto *F = cast<MCCVDefRangeFragment>(this);
454 OS << "\n ";
455 for (std::pair<const MCSymbol *, const MCSymbol *> RangeStartEnd :
456 F->getRanges()) {
457 OS << " RangeStart:" << RangeStartEnd.first;
458 OS << " RangeEnd:" << RangeStartEnd.second;
459 }
460 break;
461 }
Chandler Carruth8d736232015-12-29 09:06:16 +0000462 case MCFragment::FT_Dummy:
463 break;
464 }
465 OS << ">";
466}
467
Sam Clegg705f7982017-06-21 22:19:17 +0000468LLVM_DUMP_METHOD void MCAssembler::dump() const{
Eugene Zelenko1d435522017-02-07 23:02:00 +0000469 raw_ostream &OS = errs();
Chandler Carruth8d736232015-12-29 09:06:16 +0000470
471 OS << "<MCAssembler\n";
472 OS << " Sections:[\n ";
Sam Clegg705f7982017-06-21 22:19:17 +0000473 for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
Chandler Carruth8d736232015-12-29 09:06:16 +0000474 if (it != begin()) OS << ",\n ";
475 it->dump();
476 }
477 OS << "],\n";
478 OS << " Symbols:[";
479
Sam Clegg705f7982017-06-21 22:19:17 +0000480 for (const_symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
Chandler Carruth8d736232015-12-29 09:06:16 +0000481 if (it != symbol_begin()) OS << ",\n ";
482 OS << "(";
483 it->dump();
484 OS << ", Index:" << it->getIndex() << ", ";
485 OS << ")";
486 }
487 OS << "]>\n";
488}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000489#endif