blob: ae5bd65507bc92bb439906631c3ec9eb87493f43 [file] [log] [blame]
Chandler Carruth8d736232015-12-29 09:06:16 +00001//===- lib/MC/MCFragment.cpp - Assembler Fragment Implementation ----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chandler Carruth8d736232015-12-29 09:06:16 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/MC/MCFragment.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000010#include "llvm/ADT/SmallVector.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000011#include "llvm/ADT/StringExtras.h"
12#include "llvm/ADT/Twine.h"
Nico Weber432a3882018-04-30 14:59:11 +000013#include "llvm/Config/llvm-config.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000014#include "llvm/MC/MCAsmLayout.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/MC/MCAssembler.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000016#include "llvm/MC/MCContext.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000017#include "llvm/MC/MCExpr.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000018#include "llvm/MC/MCFixup.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000019#include "llvm/MC/MCSection.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000020#include "llvm/MC/MCSymbol.h"
21#include "llvm/MC/MCValue.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000022#include "llvm/Support/Casting.h"
23#include "llvm/Support/Compiler.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000024#include "llvm/Support/ErrorHandling.h"
Chandler Carruth8d736232015-12-29 09:06:16 +000025#include "llvm/Support/raw_ostream.h"
Eugene Zelenko1d435522017-02-07 23:02:00 +000026#include <cassert>
27#include <cstdint>
28#include <utility>
29
Chandler Carruth8d736232015-12-29 09:06:16 +000030using namespace llvm;
31
Eugene Zelenko1d435522017-02-07 23:02:00 +000032MCAsmLayout::MCAsmLayout(MCAssembler &Asm) : Assembler(Asm) {
Chandler Carruth8d736232015-12-29 09:06:16 +000033 // Compute the section layout order. Virtual sections must go last.
34 for (MCSection &Sec : Asm)
35 if (!Sec.isVirtualSection())
36 SectionOrder.push_back(&Sec);
37 for (MCSection &Sec : Asm)
38 if (Sec.isVirtualSection())
39 SectionOrder.push_back(&Sec);
40}
41
42bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
43 const MCSection *Sec = F->getParent();
44 const MCFragment *LastValid = LastValidFragment.lookup(Sec);
45 if (!LastValid)
46 return false;
47 assert(LastValid->getParent() == Sec);
48 return F->getLayoutOrder() <= LastValid->getLayoutOrder();
49}
50
51void MCAsmLayout::invalidateFragmentsFrom(MCFragment *F) {
52 // If this fragment wasn't already valid, we don't need to do anything.
53 if (!isFragmentValid(F))
54 return;
55
56 // Otherwise, reset the last valid fragment to the previous fragment
57 // (if this is the first fragment, it will be NULL).
58 LastValidFragment[F->getParent()] = F->getPrevNode();
59}
60
61void MCAsmLayout::ensureValid(const MCFragment *F) const {
62 MCSection *Sec = F->getParent();
63 MCSection::iterator I;
64 if (MCFragment *Cur = LastValidFragment[Sec])
65 I = ++MCSection::iterator(Cur);
66 else
67 I = Sec->begin();
68
69 // Advance the layout position until the fragment is valid.
70 while (!isFragmentValid(F)) {
71 assert(I != Sec->end() && "Layout bookkeeping error");
72 const_cast<MCAsmLayout *>(this)->layoutFragment(&*I);
73 ++I;
74 }
75}
76
77uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
78 ensureValid(F);
79 assert(F->Offset != ~UINT64_C(0) && "Address not set!");
80 return F->Offset;
81}
82
Mandeep Singh Grang1be19e62017-09-15 20:01:43 +000083// Simple getSymbolOffset helper for the non-variable case.
Chandler Carruth8d736232015-12-29 09:06:16 +000084static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S,
85 bool ReportError, uint64_t &Val) {
86 if (!S.getFragment()) {
87 if (ReportError)
88 report_fatal_error("unable to evaluate offset to undefined symbol '" +
89 S.getName() + "'");
90 return false;
91 }
92 Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset();
93 return true;
94}
95
96static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S,
97 bool ReportError, uint64_t &Val) {
98 if (!S.isVariable())
99 return getLabelOffset(Layout, S, ReportError, Val);
100
101 // If SD is a variable, evaluate it.
102 MCValue Target;
103 if (!S.getVariableValue()->evaluateAsValue(Target, Layout))
104 report_fatal_error("unable to evaluate offset for variable '" +
105 S.getName() + "'");
106
107 uint64_t Offset = Target.getConstant();
108
109 const MCSymbolRefExpr *A = Target.getSymA();
110 if (A) {
111 uint64_t ValA;
112 if (!getLabelOffset(Layout, A->getSymbol(), ReportError, ValA))
113 return false;
114 Offset += ValA;
115 }
116
117 const MCSymbolRefExpr *B = Target.getSymB();
118 if (B) {
119 uint64_t ValB;
120 if (!getLabelOffset(Layout, B->getSymbol(), ReportError, ValB))
121 return false;
122 Offset -= ValB;
123 }
124
125 Val = Offset;
126 return true;
127}
128
129bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const {
130 return getSymbolOffsetImpl(*this, S, false, Val);
131}
132
133uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const {
134 uint64_t Val;
135 getSymbolOffsetImpl(*this, S, true, Val);
136 return Val;
137}
138
139const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
140 if (!Symbol.isVariable())
141 return &Symbol;
142
143 const MCExpr *Expr = Symbol.getVariableValue();
144 MCValue Value;
145 if (!Expr->evaluateAsValue(Value, *this)) {
146 Assembler.getContext().reportError(
Chad Rosier9245e122017-01-19 20:06:32 +0000147 Expr->getLoc(), "expression could not be evaluated");
Chandler Carruth8d736232015-12-29 09:06:16 +0000148 return nullptr;
149 }
150
151 const MCSymbolRefExpr *RefB = Value.getSymB();
152 if (RefB) {
153 Assembler.getContext().reportError(
Chad Rosier9245e122017-01-19 20:06:32 +0000154 Expr->getLoc(), Twine("symbol '") + RefB->getSymbol().getName() +
Chandler Carruth8d736232015-12-29 09:06:16 +0000155 "' could not be evaluated in a subtraction expression");
156 return nullptr;
157 }
158
159 const MCSymbolRefExpr *A = Value.getSymA();
160 if (!A)
161 return nullptr;
162
163 const MCSymbol &ASym = A->getSymbol();
164 const MCAssembler &Asm = getAssembler();
165 if (ASym.isCommon()) {
Chad Rosier9245e122017-01-19 20:06:32 +0000166 Asm.getContext().reportError(Expr->getLoc(),
Chandler Carruth8d736232015-12-29 09:06:16 +0000167 "Common symbol '" + ASym.getName() +
168 "' cannot be used in assignment expr");
169 return nullptr;
170 }
171
172 return &ASym;
173}
174
175uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const {
176 // The size is the last fragment's end offset.
177 const MCFragment &F = Sec->getFragmentList().back();
178 return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
179}
180
181uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
182 // Virtual sections have no file size.
183 if (Sec->isVirtualSection())
184 return 0;
185
186 // Otherwise, the file size is the same as the address space size.
187 return getSectionAddressSize(Sec);
188}
189
190uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler,
Peter Smith1503fc02018-06-15 09:48:18 +0000191 const MCEncodedFragment *F,
Chandler Carruth8d736232015-12-29 09:06:16 +0000192 uint64_t FOffset, uint64_t FSize) {
193 uint64_t BundleSize = Assembler.getBundleAlignSize();
194 assert(BundleSize > 0 &&
195 "computeBundlePadding should only be called if bundling is enabled");
196 uint64_t BundleMask = BundleSize - 1;
197 uint64_t OffsetInBundle = FOffset & BundleMask;
198 uint64_t EndOfFragment = OffsetInBundle + FSize;
199
200 // There are two kinds of bundling restrictions:
201 //
202 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
203 // *end* on a bundle boundary.
204 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
205 // would, add padding until the end of the bundle so that the fragment
206 // will start in a new one.
207 if (F->alignToBundleEnd()) {
208 // Three possibilities here:
209 //
210 // A) The fragment just happens to end at a bundle boundary, so we're good.
211 // B) The fragment ends before the current bundle boundary: pad it just
212 // enough to reach the boundary.
213 // C) The fragment ends after the current bundle boundary: pad it until it
214 // reaches the end of the next bundle boundary.
215 //
216 // Note: this code could be made shorter with some modulo trickery, but it's
217 // intentionally kept in its more explicit form for simplicity.
218 if (EndOfFragment == BundleSize)
219 return 0;
220 else if (EndOfFragment < BundleSize)
221 return BundleSize - EndOfFragment;
222 else { // EndOfFragment > BundleSize
223 return 2 * BundleSize - EndOfFragment;
224 }
225 } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
226 return BundleSize - OffsetInBundle;
227 else
228 return 0;
229}
230
231/* *** */
232
Duncan P. N. Exon Smithf947c3a2016-08-30 18:40:47 +0000233void ilist_alloc_traits<MCFragment>::deleteNode(MCFragment *V) { V->destroy(); }
Chandler Carruth8d736232015-12-29 09:06:16 +0000234
Eugene Zelenko1d435522017-02-07 23:02:00 +0000235MCFragment::~MCFragment() = default;
Chandler Carruth8d736232015-12-29 09:06:16 +0000236
237MCFragment::MCFragment(FragmentType Kind, bool HasInstructions,
Peter Smith1503fc02018-06-15 09:48:18 +0000238 MCSection *Parent)
George Rimara3d0d5f2018-12-05 11:06:29 +0000239 : Kind(Kind), HasInstructions(HasInstructions), LayoutOrder(0),
240 Parent(Parent), Atom(nullptr), Offset(~UINT64_C(0)) {
Chandler Carruth8d736232015-12-29 09:06:16 +0000241 if (Parent && !isDummy())
242 Parent->getFragmentList().push_back(this);
243}
244
245void MCFragment::destroy() {
246 // First check if we are the sentinal.
247 if (Kind == FragmentType(~0)) {
248 delete this;
249 return;
250 }
251
252 switch (Kind) {
253 case FT_Align:
254 delete cast<MCAlignFragment>(this);
255 return;
256 case FT_Data:
257 delete cast<MCDataFragment>(this);
258 return;
259 case FT_CompactEncodedInst:
260 delete cast<MCCompactEncodedInstFragment>(this);
261 return;
262 case FT_Fill:
263 delete cast<MCFillFragment>(this);
264 return;
265 case FT_Relaxable:
266 delete cast<MCRelaxableFragment>(this);
267 return;
268 case FT_Org:
269 delete cast<MCOrgFragment>(this);
270 return;
271 case FT_Dwarf:
272 delete cast<MCDwarfLineAddrFragment>(this);
273 return;
274 case FT_DwarfFrame:
275 delete cast<MCDwarfCallFrameFragment>(this);
276 return;
277 case FT_LEB:
278 delete cast<MCLEBFragment>(this);
279 return;
Omer Paparo Bivas2251c792017-10-24 06:16:03 +0000280 case FT_Padding:
281 delete cast<MCPaddingFragment>(this);
282 return;
Adrian McCarthy75248a72017-11-08 18:57:02 +0000283 case FT_SymbolId:
284 delete cast<MCSymbolIdFragment>(this);
Chandler Carruth8d736232015-12-29 09:06:16 +0000285 return;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000286 case FT_CVInlineLines:
287 delete cast<MCCVInlineLineTableFragment>(this);
288 return;
David Majnemer408b5e62016-02-05 01:55:49 +0000289 case FT_CVDefRange:
290 delete cast<MCCVDefRangeFragment>(this);
291 return;
Chandler Carruth8d736232015-12-29 09:06:16 +0000292 case FT_Dummy:
293 delete cast<MCDummyFragment>(this);
294 return;
295 }
296}
297
Chandler Carruth8d736232015-12-29 09:06:16 +0000298// Debugging methods
299
300namespace llvm {
301
302raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
303 OS << "<MCFixup" << " Offset:" << AF.getOffset()
304 << " Value:" << *AF.getValue()
305 << " Kind:" << AF.getKind() << ">";
306 return OS;
307}
308
Eugene Zelenko1d435522017-02-07 23:02:00 +0000309} // end namespace llvm
Chandler Carruth8d736232015-12-29 09:06:16 +0000310
Aaron Ballman615eb472017-10-15 14:32:27 +0000311#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Sam Clegg705f7982017-06-21 22:19:17 +0000312LLVM_DUMP_METHOD void MCFragment::dump() const {
Eugene Zelenko1d435522017-02-07 23:02:00 +0000313 raw_ostream &OS = errs();
Chandler Carruth8d736232015-12-29 09:06:16 +0000314
315 OS << "<";
316 switch (getKind()) {
317 case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
318 case MCFragment::FT_Data: OS << "MCDataFragment"; break;
319 case MCFragment::FT_CompactEncodedInst:
320 OS << "MCCompactEncodedInstFragment"; break;
321 case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
322 case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
323 case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
324 case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
325 case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
326 case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
Omer Paparo Bivas2251c792017-10-24 06:16:03 +0000327 case MCFragment::FT_Padding: OS << "MCPaddingFragment"; break;
Adrian McCarthy75248a72017-11-08 18:57:02 +0000328 case MCFragment::FT_SymbolId: OS << "MCSymbolIdFragment"; break;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000329 case MCFragment::FT_CVInlineLines: OS << "MCCVInlineLineTableFragment"; break;
David Majnemer408b5e62016-02-05 01:55:49 +0000330 case MCFragment::FT_CVDefRange: OS << "MCCVDefRangeTableFragment"; break;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000331 case MCFragment::FT_Dummy: OS << "MCDummyFragment"; break;
Chandler Carruth8d736232015-12-29 09:06:16 +0000332 }
333
Peter Smith1503fc02018-06-15 09:48:18 +0000334 OS << "<MCFragment " << (const void *)this << " LayoutOrder:" << LayoutOrder
335 << " Offset:" << Offset << " HasInstructions:" << hasInstructions();
Nirav Daveb35f9e12018-06-18 16:26:11 +0000336 if (const MCEncodedFragment *EF = dyn_cast<MCEncodedFragment>(this))
Peter Smith1503fc02018-06-15 09:48:18 +0000337 OS << " BundlePadding:" << static_cast<unsigned>(EF->getBundlePadding());
338 OS << ">";
Chandler Carruth8d736232015-12-29 09:06:16 +0000339
340 switch (getKind()) {
341 case MCFragment::FT_Align: {
342 const MCAlignFragment *AF = cast<MCAlignFragment>(this);
343 if (AF->hasEmitNops())
344 OS << " (emit nops)";
345 OS << "\n ";
346 OS << " Alignment:" << AF->getAlignment()
347 << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
348 << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
349 break;
350 }
351 case MCFragment::FT_Data: {
352 const MCDataFragment *DF = cast<MCDataFragment>(this);
353 OS << "\n ";
354 OS << " Contents:[";
355 const SmallVectorImpl<char> &Contents = DF->getContents();
356 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
357 if (i) OS << ",";
358 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
359 }
360 OS << "] (" << Contents.size() << " bytes)";
361
362 if (DF->fixup_begin() != DF->fixup_end()) {
363 OS << ",\n ";
364 OS << " Fixups:[";
365 for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
366 ie = DF->fixup_end(); it != ie; ++it) {
367 if (it != DF->fixup_begin()) OS << ",\n ";
368 OS << *it;
369 }
370 OS << "]";
371 }
372 break;
373 }
374 case MCFragment::FT_CompactEncodedInst: {
375 const MCCompactEncodedInstFragment *CEIF =
376 cast<MCCompactEncodedInstFragment>(this);
377 OS << "\n ";
378 OS << " Contents:[";
379 const SmallVectorImpl<char> &Contents = CEIF->getContents();
380 for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
381 if (i) OS << ",";
382 OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
383 }
384 OS << "] (" << Contents.size() << " bytes)";
385 break;
386 }
387 case MCFragment::FT_Fill: {
388 const MCFillFragment *FF = cast<MCFillFragment>(this);
Sam Clegg58ad080e2017-06-22 17:57:01 +0000389 OS << " Value:" << static_cast<unsigned>(FF->getValue())
Nirav Dave588fad42018-05-18 17:45:48 +0000390 << " ValueSize:" << static_cast<unsigned>(FF->getValueSize())
391 << " NumValues:" << FF->getNumValues();
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}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000467#endif