1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/MC/MCAssembler.h" 10 #include "llvm/ADT/ArrayRef.h" 11 #include "llvm/ADT/SmallString.h" 12 #include "llvm/ADT/SmallVector.h" 13 #include "llvm/ADT/Statistic.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/ADT/Twine.h" 16 #include "llvm/MC/MCAsmBackend.h" 17 #include "llvm/MC/MCAsmInfo.h" 18 #include "llvm/MC/MCCodeEmitter.h" 19 #include "llvm/MC/MCCodeView.h" 20 #include "llvm/MC/MCContext.h" 21 #include "llvm/MC/MCDwarf.h" 22 #include "llvm/MC/MCExpr.h" 23 #include "llvm/MC/MCFixup.h" 24 #include "llvm/MC/MCFixupKindInfo.h" 25 #include "llvm/MC/MCFragment.h" 26 #include "llvm/MC/MCInst.h" 27 #include "llvm/MC/MCObjectWriter.h" 28 #include "llvm/MC/MCSection.h" 29 #include "llvm/MC/MCSymbol.h" 30 #include "llvm/MC/MCValue.h" 31 #include "llvm/Support/Alignment.h" 32 #include "llvm/Support/Casting.h" 33 #include "llvm/Support/Debug.h" 34 #include "llvm/Support/EndianStream.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/LEB128.h" 37 #include "llvm/Support/raw_ostream.h" 38 #include <cassert> 39 #include <cstdint> 40 #include <tuple> 41 #include <utility> 42 43 using namespace llvm; 44 45 namespace llvm { 46 class MCSubtargetInfo; 47 } 48 49 #define DEBUG_TYPE "assembler" 50 51 namespace { 52 namespace stats { 53 54 STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total"); 55 STATISTIC(EmittedRelaxableFragments, 56 "Number of emitted assembler fragments - relaxable"); 57 STATISTIC(EmittedDataFragments, 58 "Number of emitted assembler fragments - data"); 59 STATISTIC(EmittedAlignFragments, 60 "Number of emitted assembler fragments - align"); 61 STATISTIC(EmittedFillFragments, 62 "Number of emitted assembler fragments - fill"); 63 STATISTIC(EmittedNopsFragments, "Number of emitted assembler fragments - nops"); 64 STATISTIC(EmittedOrgFragments, "Number of emitted assembler fragments - org"); 65 STATISTIC(evaluateFixup, "Number of evaluated fixups"); 66 STATISTIC(ObjectBytes, "Number of emitted object file bytes"); 67 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps"); 68 STATISTIC(RelaxedInstructions, "Number of relaxed instructions"); 69 70 } // end namespace stats 71 } // end anonymous namespace 72 73 // FIXME FIXME FIXME: There are number of places in this file where we convert 74 // what is a 64-bit assembler value used for computation into a value in the 75 // object file, which may truncate it. We should detect that truncation where 76 // invalid and report errors back. 77 78 /* *** */ 79 80 MCAssembler::MCAssembler(MCContext &Context, 81 std::unique_ptr<MCAsmBackend> Backend, 82 std::unique_ptr<MCCodeEmitter> Emitter, 83 std::unique_ptr<MCObjectWriter> Writer) 84 : Context(Context), Backend(std::move(Backend)), 85 Emitter(std::move(Emitter)), Writer(std::move(Writer)) {} 86 87 void MCAssembler::reset() { 88 RelaxAll = false; 89 Sections.clear(); 90 Symbols.clear(); 91 ThumbFuncs.clear(); 92 BundleAlignSize = 0; 93 94 // reset objects owned by us 95 if (getBackendPtr()) 96 getBackendPtr()->reset(); 97 if (getEmitterPtr()) 98 getEmitterPtr()->reset(); 99 if (Writer) 100 Writer->reset(); 101 } 102 103 bool MCAssembler::registerSection(MCSection &Section) { 104 if (Section.isRegistered()) 105 return false; 106 assert(Section.curFragList()->Head && "allocInitialFragment not called"); 107 Sections.push_back(&Section); 108 Section.setIsRegistered(true); 109 return true; 110 } 111 112 bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const { 113 if (ThumbFuncs.count(Symbol)) 114 return true; 115 116 if (!Symbol->isVariable()) 117 return false; 118 119 const MCExpr *Expr = Symbol->getVariableValue(); 120 121 MCValue V; 122 if (!Expr->evaluateAsRelocatable(V, nullptr, nullptr)) 123 return false; 124 125 if (V.getSymB() || V.getRefKind() != MCSymbolRefExpr::VK_None) 126 return false; 127 128 const MCSymbolRefExpr *Ref = V.getSymA(); 129 if (!Ref) 130 return false; 131 132 if (Ref->getKind() != MCSymbolRefExpr::VK_None) 133 return false; 134 135 const MCSymbol &Sym = Ref->getSymbol(); 136 if (!isThumbFunc(&Sym)) 137 return false; 138 139 ThumbFuncs.insert(Symbol); // Cache it. 140 return true; 141 } 142 143 bool MCAssembler::evaluateFixup(const MCFixup &Fixup, const MCFragment *DF, 144 MCValue &Target, const MCSubtargetInfo *STI, 145 uint64_t &Value, bool &WasForced) const { 146 ++stats::evaluateFixup; 147 148 // FIXME: This code has some duplication with recordRelocation. We should 149 // probably merge the two into a single callback that tries to evaluate a 150 // fixup and records a relocation if one is needed. 151 152 // On error claim to have completely evaluated the fixup, to prevent any 153 // further processing from being done. 154 const MCExpr *Expr = Fixup.getValue(); 155 MCContext &Ctx = getContext(); 156 Value = 0; 157 WasForced = false; 158 if (!Expr->evaluateAsRelocatable(Target, this, &Fixup)) { 159 Ctx.reportError(Fixup.getLoc(), "expected relocatable expression"); 160 return true; 161 } 162 if (const MCSymbolRefExpr *RefB = Target.getSymB()) { 163 if (RefB->getKind() != MCSymbolRefExpr::VK_None) { 164 Ctx.reportError(Fixup.getLoc(), 165 "unsupported subtraction of qualified symbol"); 166 return true; 167 } 168 } 169 170 unsigned FixupFlags = getBackend().getFixupKindInfo(Fixup.getKind()).Flags; 171 if (FixupFlags & MCFixupKindInfo::FKF_IsTarget) 172 return getBackend().evaluateTargetFixup(*this, Fixup, DF, Target, STI, 173 Value, WasForced); 174 175 bool IsPCRel = FixupFlags & MCFixupKindInfo::FKF_IsPCRel; 176 bool IsResolved = false; 177 if (IsPCRel) { 178 if (Target.getSymB()) { 179 IsResolved = false; 180 } else if (!Target.getSymA()) { 181 IsResolved = false; 182 } else { 183 const MCSymbolRefExpr *A = Target.getSymA(); 184 const MCSymbol &SA = A->getSymbol(); 185 if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) { 186 IsResolved = false; 187 } else { 188 IsResolved = (FixupFlags & MCFixupKindInfo::FKF_Constant) || 189 getWriter().isSymbolRefDifferenceFullyResolvedImpl( 190 *this, SA, *DF, false, true); 191 } 192 } 193 } else { 194 IsResolved = Target.isAbsolute(); 195 } 196 197 Value = Target.getConstant(); 198 199 if (const MCSymbolRefExpr *A = Target.getSymA()) { 200 const MCSymbol &Sym = A->getSymbol(); 201 if (Sym.isDefined()) 202 Value += getSymbolOffset(Sym); 203 } 204 if (const MCSymbolRefExpr *B = Target.getSymB()) { 205 const MCSymbol &Sym = B->getSymbol(); 206 if (Sym.isDefined()) 207 Value -= getSymbolOffset(Sym); 208 } 209 210 bool ShouldAlignPC = FixupFlags & MCFixupKindInfo::FKF_IsAlignedDownTo32Bits; 211 assert((ShouldAlignPC ? IsPCRel : true) && 212 "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!"); 213 214 if (IsPCRel) { 215 uint64_t Offset = getFragmentOffset(*DF) + Fixup.getOffset(); 216 217 // A number of ARM fixups in Thumb mode require that the effective PC 218 // address be determined as the 32-bit aligned version of the actual offset. 219 if (ShouldAlignPC) Offset &= ~0x3; 220 Value -= Offset; 221 } 222 223 // Let the backend force a relocation if needed. 224 if (IsResolved && 225 getBackend().shouldForceRelocation(*this, Fixup, Target, Value, STI)) { 226 IsResolved = false; 227 WasForced = true; 228 } 229 230 // A linker relaxation target may emit ADD/SUB relocations for A-B+C. Let 231 // recordRelocation handle non-VK_None cases like A@plt-B+C. 232 if (!IsResolved && Target.getSymA() && Target.getSymB() && 233 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_None && 234 getBackend().handleAddSubRelocations(*this, *DF, Fixup, Target, Value)) 235 return true; 236 237 return IsResolved; 238 } 239 240 uint64_t MCAssembler::computeFragmentSize(const MCFragment &F) const { 241 assert(getBackendPtr() && "Requires assembler backend"); 242 switch (F.getKind()) { 243 case MCFragment::FT_Data: 244 return cast<MCDataFragment>(F).getContents().size(); 245 case MCFragment::FT_Relaxable: 246 return cast<MCRelaxableFragment>(F).getContents().size(); 247 case MCFragment::FT_Fill: { 248 auto &FF = cast<MCFillFragment>(F); 249 int64_t NumValues = 0; 250 if (!FF.getNumValues().evaluateKnownAbsolute(NumValues, *this)) { 251 getContext().reportError(FF.getLoc(), 252 "expected assembly-time absolute expression"); 253 return 0; 254 } 255 int64_t Size = NumValues * FF.getValueSize(); 256 if (Size < 0) { 257 getContext().reportError(FF.getLoc(), "invalid number of bytes"); 258 return 0; 259 } 260 return Size; 261 } 262 263 case MCFragment::FT_Nops: 264 return cast<MCNopsFragment>(F).getNumBytes(); 265 266 case MCFragment::FT_LEB: 267 return cast<MCLEBFragment>(F).getContents().size(); 268 269 case MCFragment::FT_BoundaryAlign: 270 return cast<MCBoundaryAlignFragment>(F).getSize(); 271 272 case MCFragment::FT_SymbolId: 273 return 4; 274 275 case MCFragment::FT_Align: { 276 const MCAlignFragment &AF = cast<MCAlignFragment>(F); 277 unsigned Offset = getFragmentOffset(AF); 278 unsigned Size = offsetToAlignment(Offset, AF.getAlignment()); 279 280 // Insert extra Nops for code alignment if the target define 281 // shouldInsertExtraNopBytesForCodeAlign target hook. 282 if (AF.getParent()->useCodeAlign() && AF.hasEmitNops() && 283 getBackend().shouldInsertExtraNopBytesForCodeAlign(AF, Size)) 284 return Size; 285 286 // If we are padding with nops, force the padding to be larger than the 287 // minimum nop size. 288 if (Size > 0 && AF.hasEmitNops()) { 289 while (Size % getBackend().getMinimumNopSize()) 290 Size += AF.getAlignment().value(); 291 } 292 if (Size > AF.getMaxBytesToEmit()) 293 return 0; 294 return Size; 295 } 296 297 case MCFragment::FT_Org: { 298 const MCOrgFragment &OF = cast<MCOrgFragment>(F); 299 MCValue Value; 300 if (!OF.getOffset().evaluateAsValue(Value, *this)) { 301 getContext().reportError(OF.getLoc(), 302 "expected assembly-time absolute expression"); 303 return 0; 304 } 305 306 uint64_t FragmentOffset = getFragmentOffset(OF); 307 int64_t TargetLocation = Value.getConstant(); 308 if (const MCSymbolRefExpr *A = Value.getSymA()) { 309 uint64_t Val; 310 if (!getSymbolOffset(A->getSymbol(), Val)) { 311 getContext().reportError(OF.getLoc(), "expected absolute expression"); 312 return 0; 313 } 314 TargetLocation += Val; 315 } 316 int64_t Size = TargetLocation - FragmentOffset; 317 if (Size < 0 || Size >= 0x40000000) { 318 getContext().reportError( 319 OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) + 320 "' (at offset '" + Twine(FragmentOffset) + "')"); 321 return 0; 322 } 323 return Size; 324 } 325 326 case MCFragment::FT_Dwarf: 327 return cast<MCDwarfLineAddrFragment>(F).getContents().size(); 328 case MCFragment::FT_DwarfFrame: 329 return cast<MCDwarfCallFrameFragment>(F).getContents().size(); 330 case MCFragment::FT_CVInlineLines: 331 return cast<MCCVInlineLineTableFragment>(F).getContents().size(); 332 case MCFragment::FT_CVDefRange: 333 return cast<MCCVDefRangeFragment>(F).getContents().size(); 334 case MCFragment::FT_PseudoProbe: 335 return cast<MCPseudoProbeAddrFragment>(F).getContents().size(); 336 case MCFragment::FT_Dummy: 337 llvm_unreachable("Should not have been added"); 338 } 339 340 llvm_unreachable("invalid fragment kind"); 341 } 342 343 // Compute the amount of padding required before the fragment \p F to 344 // obey bundling restrictions, where \p FOffset is the fragment's offset in 345 // its section and \p FSize is the fragment's size. 346 static uint64_t computeBundlePadding(unsigned BundleSize, 347 const MCEncodedFragment *F, 348 uint64_t FOffset, uint64_t FSize) { 349 uint64_t OffsetInBundle = FOffset & (BundleSize - 1); 350 uint64_t EndOfFragment = OffsetInBundle + FSize; 351 352 // There are two kinds of bundling restrictions: 353 // 354 // 1) For alignToBundleEnd(), add padding to ensure that the fragment will 355 // *end* on a bundle boundary. 356 // 2) Otherwise, check if the fragment would cross a bundle boundary. If it 357 // would, add padding until the end of the bundle so that the fragment 358 // will start in a new one. 359 if (F->alignToBundleEnd()) { 360 // Three possibilities here: 361 // 362 // A) The fragment just happens to end at a bundle boundary, so we're good. 363 // B) The fragment ends before the current bundle boundary: pad it just 364 // enough to reach the boundary. 365 // C) The fragment ends after the current bundle boundary: pad it until it 366 // reaches the end of the next bundle boundary. 367 // 368 // Note: this code could be made shorter with some modulo trickery, but it's 369 // intentionally kept in its more explicit form for simplicity. 370 if (EndOfFragment == BundleSize) 371 return 0; 372 else if (EndOfFragment < BundleSize) 373 return BundleSize - EndOfFragment; 374 else { // EndOfFragment > BundleSize 375 return 2 * BundleSize - EndOfFragment; 376 } 377 } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize) 378 return BundleSize - OffsetInBundle; 379 else 380 return 0; 381 } 382 383 void MCAssembler::layoutBundle(MCFragment *Prev, MCFragment *F) const { 384 // If bundling is enabled and this fragment has instructions in it, it has to 385 // obey the bundling restrictions. With padding, we'll have: 386 // 387 // 388 // BundlePadding 389 // ||| 390 // ------------------------------------- 391 // Prev |##########| F | 392 // ------------------------------------- 393 // ^ 394 // | 395 // F->Offset 396 // 397 // The fragment's offset will point to after the padding, and its computed 398 // size won't include the padding. 399 // 400 // ".align N" is an example of a directive that introduces multiple 401 // fragments. We could add a special case to handle ".align N" by emitting 402 // within-fragment padding (which would produce less padding when N is less 403 // than the bundle size), but for now we don't. 404 // 405 assert(isa<MCEncodedFragment>(F) && 406 "Only MCEncodedFragment implementations have instructions"); 407 MCEncodedFragment *EF = cast<MCEncodedFragment>(F); 408 uint64_t FSize = computeFragmentSize(*EF); 409 410 if (FSize > getBundleAlignSize()) 411 report_fatal_error("Fragment can't be larger than a bundle size"); 412 413 uint64_t RequiredBundlePadding = 414 computeBundlePadding(getBundleAlignSize(), EF, EF->Offset, FSize); 415 if (RequiredBundlePadding > UINT8_MAX) 416 report_fatal_error("Padding cannot exceed 255 bytes"); 417 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding)); 418 EF->Offset += RequiredBundlePadding; 419 if (auto *DF = dyn_cast_or_null<MCDataFragment>(Prev)) 420 if (DF->getContents().empty()) 421 DF->Offset = EF->Offset; 422 } 423 424 void MCAssembler::ensureValid(MCSection &Sec) const { 425 if (Sec.hasLayout()) 426 return; 427 Sec.setHasLayout(true); 428 MCFragment *Prev = nullptr; 429 uint64_t Offset = 0; 430 for (MCFragment &F : Sec) { 431 F.Offset = Offset; 432 if (isBundlingEnabled() && F.hasInstructions()) { 433 layoutBundle(Prev, &F); 434 Offset = F.Offset; 435 } 436 Offset += computeFragmentSize(F); 437 Prev = &F; 438 } 439 } 440 441 uint64_t MCAssembler::getFragmentOffset(const MCFragment &F) const { 442 ensureValid(*F.getParent()); 443 return F.Offset; 444 } 445 446 // Simple getSymbolOffset helper for the non-variable case. 447 static bool getLabelOffset(const MCAssembler &Asm, const MCSymbol &S, 448 bool ReportError, uint64_t &Val) { 449 if (!S.getFragment()) { 450 if (ReportError) 451 report_fatal_error("unable to evaluate offset to undefined symbol '" + 452 S.getName() + "'"); 453 return false; 454 } 455 Val = Asm.getFragmentOffset(*S.getFragment()) + S.getOffset(); 456 return true; 457 } 458 459 static bool getSymbolOffsetImpl(const MCAssembler &Asm, const MCSymbol &S, 460 bool ReportError, uint64_t &Val) { 461 if (!S.isVariable()) 462 return getLabelOffset(Asm, S, ReportError, Val); 463 464 // If SD is a variable, evaluate it. 465 MCValue Target; 466 if (!S.getVariableValue()->evaluateAsValue(Target, Asm)) 467 report_fatal_error("unable to evaluate offset for variable '" + 468 S.getName() + "'"); 469 470 uint64_t Offset = Target.getConstant(); 471 472 const MCSymbolRefExpr *A = Target.getSymA(); 473 if (A) { 474 uint64_t ValA; 475 // FIXME: On most platforms, `Target`'s component symbols are labels from 476 // having been simplified during evaluation, but on Mach-O they can be 477 // variables due to PR19203. This, and the line below for `B` can be 478 // restored to call `getLabelOffset` when PR19203 is fixed. 479 if (!getSymbolOffsetImpl(Asm, A->getSymbol(), ReportError, ValA)) 480 return false; 481 Offset += ValA; 482 } 483 484 const MCSymbolRefExpr *B = Target.getSymB(); 485 if (B) { 486 uint64_t ValB; 487 if (!getSymbolOffsetImpl(Asm, B->getSymbol(), ReportError, ValB)) 488 return false; 489 Offset -= ValB; 490 } 491 492 Val = Offset; 493 return true; 494 } 495 496 bool MCAssembler::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const { 497 return getSymbolOffsetImpl(*this, S, false, Val); 498 } 499 500 uint64_t MCAssembler::getSymbolOffset(const MCSymbol &S) const { 501 uint64_t Val; 502 getSymbolOffsetImpl(*this, S, true, Val); 503 return Val; 504 } 505 506 const MCSymbol *MCAssembler::getBaseSymbol(const MCSymbol &Symbol) const { 507 assert(HasLayout); 508 if (!Symbol.isVariable()) 509 return &Symbol; 510 511 const MCExpr *Expr = Symbol.getVariableValue(); 512 MCValue Value; 513 if (!Expr->evaluateAsValue(Value, *this)) { 514 getContext().reportError(Expr->getLoc(), 515 "expression could not be evaluated"); 516 return nullptr; 517 } 518 519 const MCSymbolRefExpr *RefB = Value.getSymB(); 520 if (RefB) { 521 getContext().reportError( 522 Expr->getLoc(), 523 Twine("symbol '") + RefB->getSymbol().getName() + 524 "' could not be evaluated in a subtraction expression"); 525 return nullptr; 526 } 527 528 const MCSymbolRefExpr *A = Value.getSymA(); 529 if (!A) 530 return nullptr; 531 532 const MCSymbol &ASym = A->getSymbol(); 533 if (ASym.isCommon()) { 534 getContext().reportError(Expr->getLoc(), 535 "Common symbol '" + ASym.getName() + 536 "' cannot be used in assignment expr"); 537 return nullptr; 538 } 539 540 return &ASym; 541 } 542 543 uint64_t MCAssembler::getSectionAddressSize(const MCSection &Sec) const { 544 assert(HasLayout); 545 // The size is the last fragment's end offset. 546 const MCFragment &F = *Sec.curFragList()->Tail; 547 return getFragmentOffset(F) + computeFragmentSize(F); 548 } 549 550 uint64_t MCAssembler::getSectionFileSize(const MCSection &Sec) const { 551 // Virtual sections have no file size. 552 if (Sec.isVirtualSection()) 553 return 0; 554 return getSectionAddressSize(Sec); 555 } 556 557 bool MCAssembler::registerSymbol(const MCSymbol &Symbol) { 558 bool Changed = !Symbol.isRegistered(); 559 if (Changed) { 560 Symbol.setIsRegistered(true); 561 Symbols.push_back(&Symbol); 562 } 563 return Changed; 564 } 565 566 void MCAssembler::writeFragmentPadding(raw_ostream &OS, 567 const MCEncodedFragment &EF, 568 uint64_t FSize) const { 569 assert(getBackendPtr() && "Expected assembler backend"); 570 // Should NOP padding be written out before this fragment? 571 unsigned BundlePadding = EF.getBundlePadding(); 572 if (BundlePadding > 0) { 573 assert(isBundlingEnabled() && 574 "Writing bundle padding with disabled bundling"); 575 assert(EF.hasInstructions() && 576 "Writing bundle padding for a fragment without instructions"); 577 578 unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize); 579 const MCSubtargetInfo *STI = EF.getSubtargetInfo(); 580 if (EF.alignToBundleEnd() && TotalLength > getBundleAlignSize()) { 581 // If the padding itself crosses a bundle boundary, it must be emitted 582 // in 2 pieces, since even nop instructions must not cross boundaries. 583 // v--------------v <- BundleAlignSize 584 // v---------v <- BundlePadding 585 // ---------------------------- 586 // | Prev |####|####| F | 587 // ---------------------------- 588 // ^-------------------^ <- TotalLength 589 unsigned DistanceToBoundary = TotalLength - getBundleAlignSize(); 590 if (!getBackend().writeNopData(OS, DistanceToBoundary, STI)) 591 report_fatal_error("unable to write NOP sequence of " + 592 Twine(DistanceToBoundary) + " bytes"); 593 BundlePadding -= DistanceToBoundary; 594 } 595 if (!getBackend().writeNopData(OS, BundlePadding, STI)) 596 report_fatal_error("unable to write NOP sequence of " + 597 Twine(BundlePadding) + " bytes"); 598 } 599 } 600 601 /// Write the fragment \p F to the output file. 602 static void writeFragment(raw_ostream &OS, const MCAssembler &Asm, 603 const MCFragment &F) { 604 // FIXME: Embed in fragments instead? 605 uint64_t FragmentSize = Asm.computeFragmentSize(F); 606 607 llvm::endianness Endian = Asm.getBackend().Endian; 608 609 if (const MCEncodedFragment *EF = dyn_cast<MCEncodedFragment>(&F)) 610 Asm.writeFragmentPadding(OS, *EF, FragmentSize); 611 612 // This variable (and its dummy usage) is to participate in the assert at 613 // the end of the function. 614 uint64_t Start = OS.tell(); 615 (void) Start; 616 617 ++stats::EmittedFragments; 618 619 switch (F.getKind()) { 620 case MCFragment::FT_Align: { 621 ++stats::EmittedAlignFragments; 622 const MCAlignFragment &AF = cast<MCAlignFragment>(F); 623 assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!"); 624 625 uint64_t Count = FragmentSize / AF.getValueSize(); 626 627 // FIXME: This error shouldn't actually occur (the front end should emit 628 // multiple .align directives to enforce the semantics it wants), but is 629 // severe enough that we want to report it. How to handle this? 630 if (Count * AF.getValueSize() != FragmentSize) 631 report_fatal_error("undefined .align directive, value size '" + 632 Twine(AF.getValueSize()) + 633 "' is not a divisor of padding size '" + 634 Twine(FragmentSize) + "'"); 635 636 // See if we are aligning with nops, and if so do that first to try to fill 637 // the Count bytes. Then if that did not fill any bytes or there are any 638 // bytes left to fill use the Value and ValueSize to fill the rest. 639 // If we are aligning with nops, ask that target to emit the right data. 640 if (AF.hasEmitNops()) { 641 if (!Asm.getBackend().writeNopData(OS, Count, AF.getSubtargetInfo())) 642 report_fatal_error("unable to write nop sequence of " + 643 Twine(Count) + " bytes"); 644 break; 645 } 646 647 // Otherwise, write out in multiples of the value size. 648 for (uint64_t i = 0; i != Count; ++i) { 649 switch (AF.getValueSize()) { 650 default: llvm_unreachable("Invalid size!"); 651 case 1: OS << char(AF.getValue()); break; 652 case 2: 653 support::endian::write<uint16_t>(OS, AF.getValue(), Endian); 654 break; 655 case 4: 656 support::endian::write<uint32_t>(OS, AF.getValue(), Endian); 657 break; 658 case 8: 659 support::endian::write<uint64_t>(OS, AF.getValue(), Endian); 660 break; 661 } 662 } 663 break; 664 } 665 666 case MCFragment::FT_Data: 667 ++stats::EmittedDataFragments; 668 OS << cast<MCDataFragment>(F).getContents(); 669 break; 670 671 case MCFragment::FT_Relaxable: 672 ++stats::EmittedRelaxableFragments; 673 OS << cast<MCRelaxableFragment>(F).getContents(); 674 break; 675 676 case MCFragment::FT_Fill: { 677 ++stats::EmittedFillFragments; 678 const MCFillFragment &FF = cast<MCFillFragment>(F); 679 uint64_t V = FF.getValue(); 680 unsigned VSize = FF.getValueSize(); 681 const unsigned MaxChunkSize = 16; 682 char Data[MaxChunkSize]; 683 assert(0 < VSize && VSize <= MaxChunkSize && "Illegal fragment fill size"); 684 // Duplicate V into Data as byte vector to reduce number of 685 // writes done. As such, do endian conversion here. 686 for (unsigned I = 0; I != VSize; ++I) { 687 unsigned index = Endian == llvm::endianness::little ? I : (VSize - I - 1); 688 Data[I] = uint8_t(V >> (index * 8)); 689 } 690 for (unsigned I = VSize; I < MaxChunkSize; ++I) 691 Data[I] = Data[I - VSize]; 692 693 // Set to largest multiple of VSize in Data. 694 const unsigned NumPerChunk = MaxChunkSize / VSize; 695 // Set ChunkSize to largest multiple of VSize in Data 696 const unsigned ChunkSize = VSize * NumPerChunk; 697 698 // Do copies by chunk. 699 StringRef Ref(Data, ChunkSize); 700 for (uint64_t I = 0, E = FragmentSize / ChunkSize; I != E; ++I) 701 OS << Ref; 702 703 // do remainder if needed. 704 unsigned TrailingCount = FragmentSize % ChunkSize; 705 if (TrailingCount) 706 OS.write(Data, TrailingCount); 707 break; 708 } 709 710 case MCFragment::FT_Nops: { 711 ++stats::EmittedNopsFragments; 712 const MCNopsFragment &NF = cast<MCNopsFragment>(F); 713 714 int64_t NumBytes = NF.getNumBytes(); 715 int64_t ControlledNopLength = NF.getControlledNopLength(); 716 int64_t MaximumNopLength = 717 Asm.getBackend().getMaximumNopSize(*NF.getSubtargetInfo()); 718 719 assert(NumBytes > 0 && "Expected positive NOPs fragment size"); 720 assert(ControlledNopLength >= 0 && "Expected non-negative NOP size"); 721 722 if (ControlledNopLength > MaximumNopLength) { 723 Asm.getContext().reportError(NF.getLoc(), 724 "illegal NOP size " + 725 std::to_string(ControlledNopLength) + 726 ". (expected within [0, " + 727 std::to_string(MaximumNopLength) + "])"); 728 // Clamp the NOP length as reportError does not stop the execution 729 // immediately. 730 ControlledNopLength = MaximumNopLength; 731 } 732 733 // Use maximum value if the size of each NOP is not specified 734 if (!ControlledNopLength) 735 ControlledNopLength = MaximumNopLength; 736 737 while (NumBytes) { 738 uint64_t NumBytesToEmit = 739 (uint64_t)std::min(NumBytes, ControlledNopLength); 740 assert(NumBytesToEmit && "try to emit empty NOP instruction"); 741 if (!Asm.getBackend().writeNopData(OS, NumBytesToEmit, 742 NF.getSubtargetInfo())) { 743 report_fatal_error("unable to write nop sequence of the remaining " + 744 Twine(NumBytesToEmit) + " bytes"); 745 break; 746 } 747 NumBytes -= NumBytesToEmit; 748 } 749 break; 750 } 751 752 case MCFragment::FT_LEB: { 753 const MCLEBFragment &LF = cast<MCLEBFragment>(F); 754 OS << LF.getContents(); 755 break; 756 } 757 758 case MCFragment::FT_BoundaryAlign: { 759 const MCBoundaryAlignFragment &BF = cast<MCBoundaryAlignFragment>(F); 760 if (!Asm.getBackend().writeNopData(OS, FragmentSize, BF.getSubtargetInfo())) 761 report_fatal_error("unable to write nop sequence of " + 762 Twine(FragmentSize) + " bytes"); 763 break; 764 } 765 766 case MCFragment::FT_SymbolId: { 767 const MCSymbolIdFragment &SF = cast<MCSymbolIdFragment>(F); 768 support::endian::write<uint32_t>(OS, SF.getSymbol()->getIndex(), Endian); 769 break; 770 } 771 772 case MCFragment::FT_Org: { 773 ++stats::EmittedOrgFragments; 774 const MCOrgFragment &OF = cast<MCOrgFragment>(F); 775 776 for (uint64_t i = 0, e = FragmentSize; i != e; ++i) 777 OS << char(OF.getValue()); 778 779 break; 780 } 781 782 case MCFragment::FT_Dwarf: { 783 const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F); 784 OS << OF.getContents(); 785 break; 786 } 787 case MCFragment::FT_DwarfFrame: { 788 const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F); 789 OS << CF.getContents(); 790 break; 791 } 792 case MCFragment::FT_CVInlineLines: { 793 const auto &OF = cast<MCCVInlineLineTableFragment>(F); 794 OS << OF.getContents(); 795 break; 796 } 797 case MCFragment::FT_CVDefRange: { 798 const auto &DRF = cast<MCCVDefRangeFragment>(F); 799 OS << DRF.getContents(); 800 break; 801 } 802 case MCFragment::FT_PseudoProbe: { 803 const MCPseudoProbeAddrFragment &PF = cast<MCPseudoProbeAddrFragment>(F); 804 OS << PF.getContents(); 805 break; 806 } 807 case MCFragment::FT_Dummy: 808 llvm_unreachable("Should not have been added"); 809 } 810 811 assert(OS.tell() - Start == FragmentSize && 812 "The stream should advance by fragment size"); 813 } 814 815 void MCAssembler::writeSectionData(raw_ostream &OS, 816 const MCSection *Sec) const { 817 assert(getBackendPtr() && "Expected assembler backend"); 818 819 // Ignore virtual sections. 820 if (Sec->isVirtualSection()) { 821 assert(getSectionFileSize(*Sec) == 0 && "Invalid size for section!"); 822 823 // Check that contents are only things legal inside a virtual section. 824 for (const MCFragment &F : *Sec) { 825 switch (F.getKind()) { 826 default: llvm_unreachable("Invalid fragment in virtual section!"); 827 case MCFragment::FT_Data: { 828 // Check that we aren't trying to write a non-zero contents (or fixups) 829 // into a virtual section. This is to support clients which use standard 830 // directives to fill the contents of virtual sections. 831 const MCDataFragment &DF = cast<MCDataFragment>(F); 832 if (DF.getFixups().size()) 833 getContext().reportError(SMLoc(), Sec->getVirtualSectionKind() + 834 " section '" + Sec->getName() + 835 "' cannot have fixups"); 836 for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i) 837 if (DF.getContents()[i]) { 838 getContext().reportError(SMLoc(), 839 Sec->getVirtualSectionKind() + 840 " section '" + Sec->getName() + 841 "' cannot have non-zero initializers"); 842 break; 843 } 844 break; 845 } 846 case MCFragment::FT_Align: 847 // Check that we aren't trying to write a non-zero value into a virtual 848 // section. 849 assert((cast<MCAlignFragment>(F).getValueSize() == 0 || 850 cast<MCAlignFragment>(F).getValue() == 0) && 851 "Invalid align in virtual section!"); 852 break; 853 case MCFragment::FT_Fill: 854 assert((cast<MCFillFragment>(F).getValue() == 0) && 855 "Invalid fill in virtual section!"); 856 break; 857 case MCFragment::FT_Org: 858 break; 859 } 860 } 861 862 return; 863 } 864 865 uint64_t Start = OS.tell(); 866 (void)Start; 867 868 for (const MCFragment &F : *Sec) 869 writeFragment(OS, *this, F); 870 871 assert(getContext().hadError() || 872 OS.tell() - Start == getSectionAddressSize(*Sec)); 873 } 874 875 std::tuple<MCValue, uint64_t, bool> 876 MCAssembler::handleFixup(MCFragment &F, const MCFixup &Fixup, 877 const MCSubtargetInfo *STI) { 878 // Evaluate the fixup. 879 MCValue Target; 880 uint64_t FixedValue; 881 bool WasForced; 882 bool IsResolved = 883 evaluateFixup(Fixup, &F, Target, STI, FixedValue, WasForced); 884 if (!IsResolved) { 885 // The fixup was unresolved, we need a relocation. Inform the object 886 // writer of the relocation, and give it an opportunity to adjust the 887 // fixup value if need be. 888 getWriter().recordRelocation(*this, &F, Fixup, Target, FixedValue); 889 } 890 return std::make_tuple(Target, FixedValue, IsResolved); 891 } 892 893 void MCAssembler::layout() { 894 assert(getBackendPtr() && "Expected assembler backend"); 895 DEBUG_WITH_TYPE("mc-dump", { 896 errs() << "assembler backend - pre-layout\n--\n"; 897 dump(); }); 898 899 // Assign section ordinals. 900 unsigned SectionIndex = 0; 901 for (MCSection &Sec : *this) { 902 Sec.setOrdinal(SectionIndex++); 903 904 // Chain together fragments from all subsections. 905 if (Sec.Subsections.size() > 1) { 906 MCDummyFragment Dummy; 907 MCFragment *Tail = &Dummy; 908 for (auto &[_, List] : Sec.Subsections) { 909 assert(List.Head); 910 Tail->Next = List.Head; 911 Tail = List.Tail; 912 } 913 Sec.Subsections.clear(); 914 Sec.Subsections.push_back({0u, {Dummy.getNext(), Tail}}); 915 Sec.CurFragList = &Sec.Subsections[0].second; 916 917 unsigned FragmentIndex = 0; 918 for (MCFragment &Frag : Sec) 919 Frag.setLayoutOrder(FragmentIndex++); 920 } 921 } 922 923 // Layout until everything fits. 924 this->HasLayout = true; 925 while (layoutOnce()) { 926 if (getContext().hadError()) 927 return; 928 // Size of fragments in one section can depend on the size of fragments in 929 // another. If any fragment has changed size, we have to re-layout (and 930 // as a result possibly further relax) all. 931 for (MCSection &Sec : *this) 932 Sec.setHasLayout(false); 933 } 934 935 DEBUG_WITH_TYPE("mc-dump", { 936 errs() << "assembler backend - post-relaxation\n--\n"; 937 dump(); }); 938 939 // Finalize the layout, including fragment lowering. 940 getBackend().finishLayout(*this); 941 942 DEBUG_WITH_TYPE("mc-dump", { 943 errs() << "assembler backend - final-layout\n--\n"; 944 dump(); }); 945 946 // Allow the object writer a chance to perform post-layout binding (for 947 // example, to set the index fields in the symbol data). 948 getWriter().executePostLayoutBinding(*this); 949 950 // Evaluate and apply the fixups, generating relocation entries as necessary. 951 for (MCSection &Sec : *this) { 952 for (MCFragment &Frag : Sec) { 953 ArrayRef<MCFixup> Fixups; 954 MutableArrayRef<char> Contents; 955 const MCSubtargetInfo *STI = nullptr; 956 957 // Process MCAlignFragment and MCEncodedFragmentWithFixups here. 958 switch (Frag.getKind()) { 959 default: 960 continue; 961 case MCFragment::FT_Align: { 962 MCAlignFragment &AF = cast<MCAlignFragment>(Frag); 963 // Insert fixup type for code alignment if the target define 964 // shouldInsertFixupForCodeAlign target hook. 965 if (Sec.useCodeAlign() && AF.hasEmitNops()) 966 getBackend().shouldInsertFixupForCodeAlign(*this, AF); 967 continue; 968 } 969 case MCFragment::FT_Data: { 970 MCDataFragment &DF = cast<MCDataFragment>(Frag); 971 Fixups = DF.getFixups(); 972 Contents = DF.getContents(); 973 STI = DF.getSubtargetInfo(); 974 assert(!DF.hasInstructions() || STI != nullptr); 975 break; 976 } 977 case MCFragment::FT_Relaxable: { 978 MCRelaxableFragment &RF = cast<MCRelaxableFragment>(Frag); 979 Fixups = RF.getFixups(); 980 Contents = RF.getContents(); 981 STI = RF.getSubtargetInfo(); 982 assert(!RF.hasInstructions() || STI != nullptr); 983 break; 984 } 985 case MCFragment::FT_CVDefRange: { 986 MCCVDefRangeFragment &CF = cast<MCCVDefRangeFragment>(Frag); 987 Fixups = CF.getFixups(); 988 Contents = CF.getContents(); 989 break; 990 } 991 case MCFragment::FT_Dwarf: { 992 MCDwarfLineAddrFragment &DF = cast<MCDwarfLineAddrFragment>(Frag); 993 Fixups = DF.getFixups(); 994 Contents = DF.getContents(); 995 break; 996 } 997 case MCFragment::FT_DwarfFrame: { 998 MCDwarfCallFrameFragment &DF = cast<MCDwarfCallFrameFragment>(Frag); 999 Fixups = DF.getFixups(); 1000 Contents = DF.getContents(); 1001 break; 1002 } 1003 case MCFragment::FT_LEB: { 1004 auto &LF = cast<MCLEBFragment>(Frag); 1005 Fixups = LF.getFixups(); 1006 Contents = LF.getContents(); 1007 break; 1008 } 1009 case MCFragment::FT_PseudoProbe: { 1010 MCPseudoProbeAddrFragment &PF = cast<MCPseudoProbeAddrFragment>(Frag); 1011 Fixups = PF.getFixups(); 1012 Contents = PF.getContents(); 1013 break; 1014 } 1015 } 1016 for (const MCFixup &Fixup : Fixups) { 1017 uint64_t FixedValue; 1018 bool IsResolved; 1019 MCValue Target; 1020 std::tie(Target, FixedValue, IsResolved) = 1021 handleFixup(Frag, Fixup, STI); 1022 getBackend().applyFixup(*this, Fixup, Target, Contents, FixedValue, 1023 IsResolved, STI); 1024 } 1025 } 1026 } 1027 } 1028 1029 void MCAssembler::Finish() { 1030 layout(); 1031 1032 // Write the object file. 1033 stats::ObjectBytes += getWriter().writeObject(*this); 1034 1035 HasLayout = false; 1036 } 1037 1038 bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup, 1039 const MCRelaxableFragment *DF) const { 1040 assert(getBackendPtr() && "Expected assembler backend"); 1041 MCValue Target; 1042 uint64_t Value; 1043 bool WasForced; 1044 bool Resolved = evaluateFixup(Fixup, DF, Target, DF->getSubtargetInfo(), 1045 Value, WasForced); 1046 if (Target.getSymA() && 1047 Target.getSymA()->getKind() == MCSymbolRefExpr::VK_X86_ABS8 && 1048 Fixup.getKind() == FK_Data_1) 1049 return false; 1050 return getBackend().fixupNeedsRelaxationAdvanced(*this, Fixup, Resolved, 1051 Value, DF, WasForced); 1052 } 1053 1054 bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F) const { 1055 assert(getBackendPtr() && "Expected assembler backend"); 1056 // If this inst doesn't ever need relaxation, ignore it. This occurs when we 1057 // are intentionally pushing out inst fragments, or because we relaxed a 1058 // previous instruction to one that doesn't need relaxation. 1059 if (!getBackend().mayNeedRelaxation(F->getInst(), *F->getSubtargetInfo())) 1060 return false; 1061 1062 for (const MCFixup &Fixup : F->getFixups()) 1063 if (fixupNeedsRelaxation(Fixup, F)) 1064 return true; 1065 1066 return false; 1067 } 1068 1069 bool MCAssembler::relaxInstruction(MCRelaxableFragment &F) { 1070 assert(getEmitterPtr() && 1071 "Expected CodeEmitter defined for relaxInstruction"); 1072 if (!fragmentNeedsRelaxation(&F)) 1073 return false; 1074 1075 ++stats::RelaxedInstructions; 1076 1077 // FIXME-PERF: We could immediately lower out instructions if we can tell 1078 // they are fully resolved, to avoid retesting on later passes. 1079 1080 // Relax the fragment. 1081 1082 MCInst Relaxed = F.getInst(); 1083 getBackend().relaxInstruction(Relaxed, *F.getSubtargetInfo()); 1084 1085 // Encode the new instruction. 1086 F.setInst(Relaxed); 1087 F.getFixups().clear(); 1088 F.getContents().clear(); 1089 getEmitter().encodeInstruction(Relaxed, F.getContents(), F.getFixups(), 1090 *F.getSubtargetInfo()); 1091 return true; 1092 } 1093 1094 bool MCAssembler::relaxLEB(MCLEBFragment &LF) { 1095 const unsigned OldSize = static_cast<unsigned>(LF.getContents().size()); 1096 unsigned PadTo = OldSize; 1097 int64_t Value; 1098 SmallVectorImpl<char> &Data = LF.getContents(); 1099 LF.getFixups().clear(); 1100 // Use evaluateKnownAbsolute for Mach-O as a hack: .subsections_via_symbols 1101 // requires that .uleb128 A-B is foldable where A and B reside in different 1102 // fragments. This is used by __gcc_except_table. 1103 bool Abs = getWriter().getSubsectionsViaSymbols() 1104 ? LF.getValue().evaluateKnownAbsolute(Value, *this) 1105 : LF.getValue().evaluateAsAbsolute(Value, *this); 1106 if (!Abs) { 1107 bool Relaxed, UseZeroPad; 1108 std::tie(Relaxed, UseZeroPad) = getBackend().relaxLEB128(*this, LF, Value); 1109 if (!Relaxed) { 1110 getContext().reportError(LF.getValue().getLoc(), 1111 Twine(LF.isSigned() ? ".s" : ".u") + 1112 "leb128 expression is not absolute"); 1113 LF.setValue(MCConstantExpr::create(0, Context)); 1114 } 1115 uint8_t Tmp[10]; // maximum size: ceil(64/7) 1116 PadTo = std::max(PadTo, encodeULEB128(uint64_t(Value), Tmp)); 1117 if (UseZeroPad) 1118 Value = 0; 1119 } 1120 Data.clear(); 1121 raw_svector_ostream OSE(Data); 1122 // The compiler can generate EH table assembly that is impossible to assemble 1123 // without either adding padding to an LEB fragment or adding extra padding 1124 // to a later alignment fragment. To accommodate such tables, relaxation can 1125 // only increase an LEB fragment size here, not decrease it. See PR35809. 1126 if (LF.isSigned()) 1127 encodeSLEB128(Value, OSE, PadTo); 1128 else 1129 encodeULEB128(Value, OSE, PadTo); 1130 return OldSize != LF.getContents().size(); 1131 } 1132 1133 /// Check if the branch crosses the boundary. 1134 /// 1135 /// \param StartAddr start address of the fused/unfused branch. 1136 /// \param Size size of the fused/unfused branch. 1137 /// \param BoundaryAlignment alignment requirement of the branch. 1138 /// \returns true if the branch cross the boundary. 1139 static bool mayCrossBoundary(uint64_t StartAddr, uint64_t Size, 1140 Align BoundaryAlignment) { 1141 uint64_t EndAddr = StartAddr + Size; 1142 return (StartAddr >> Log2(BoundaryAlignment)) != 1143 ((EndAddr - 1) >> Log2(BoundaryAlignment)); 1144 } 1145 1146 /// Check if the branch is against the boundary. 1147 /// 1148 /// \param StartAddr start address of the fused/unfused branch. 1149 /// \param Size size of the fused/unfused branch. 1150 /// \param BoundaryAlignment alignment requirement of the branch. 1151 /// \returns true if the branch is against the boundary. 1152 static bool isAgainstBoundary(uint64_t StartAddr, uint64_t Size, 1153 Align BoundaryAlignment) { 1154 uint64_t EndAddr = StartAddr + Size; 1155 return (EndAddr & (BoundaryAlignment.value() - 1)) == 0; 1156 } 1157 1158 /// Check if the branch needs padding. 1159 /// 1160 /// \param StartAddr start address of the fused/unfused branch. 1161 /// \param Size size of the fused/unfused branch. 1162 /// \param BoundaryAlignment alignment requirement of the branch. 1163 /// \returns true if the branch needs padding. 1164 static bool needPadding(uint64_t StartAddr, uint64_t Size, 1165 Align BoundaryAlignment) { 1166 return mayCrossBoundary(StartAddr, Size, BoundaryAlignment) || 1167 isAgainstBoundary(StartAddr, Size, BoundaryAlignment); 1168 } 1169 1170 bool MCAssembler::relaxBoundaryAlign(MCBoundaryAlignFragment &BF) { 1171 // BoundaryAlignFragment that doesn't need to align any fragment should not be 1172 // relaxed. 1173 if (!BF.getLastFragment()) 1174 return false; 1175 1176 uint64_t AlignedOffset = getFragmentOffset(BF); 1177 uint64_t AlignedSize = 0; 1178 for (const MCFragment *F = BF.getNext();; F = F->getNext()) { 1179 AlignedSize += computeFragmentSize(*F); 1180 if (F == BF.getLastFragment()) 1181 break; 1182 } 1183 1184 Align BoundaryAlignment = BF.getAlignment(); 1185 uint64_t NewSize = needPadding(AlignedOffset, AlignedSize, BoundaryAlignment) 1186 ? offsetToAlignment(AlignedOffset, BoundaryAlignment) 1187 : 0U; 1188 if (NewSize == BF.getSize()) 1189 return false; 1190 BF.setSize(NewSize); 1191 return true; 1192 } 1193 1194 bool MCAssembler::relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF) { 1195 bool WasRelaxed; 1196 if (getBackend().relaxDwarfLineAddr(*this, DF, WasRelaxed)) 1197 return WasRelaxed; 1198 1199 MCContext &Context = getContext(); 1200 uint64_t OldSize = DF.getContents().size(); 1201 int64_t AddrDelta; 1202 bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, *this); 1203 assert(Abs && "We created a line delta with an invalid expression"); 1204 (void)Abs; 1205 int64_t LineDelta; 1206 LineDelta = DF.getLineDelta(); 1207 SmallVectorImpl<char> &Data = DF.getContents(); 1208 Data.clear(); 1209 DF.getFixups().clear(); 1210 1211 MCDwarfLineAddr::encode(Context, getDWARFLinetableParams(), LineDelta, 1212 AddrDelta, Data); 1213 return OldSize != Data.size(); 1214 } 1215 1216 bool MCAssembler::relaxDwarfCallFrameFragment(MCDwarfCallFrameFragment &DF) { 1217 bool WasRelaxed; 1218 if (getBackend().relaxDwarfCFA(*this, DF, WasRelaxed)) 1219 return WasRelaxed; 1220 1221 MCContext &Context = getContext(); 1222 int64_t Value; 1223 bool Abs = DF.getAddrDelta().evaluateAsAbsolute(Value, *this); 1224 if (!Abs) { 1225 getContext().reportError(DF.getAddrDelta().getLoc(), 1226 "invalid CFI advance_loc expression"); 1227 DF.setAddrDelta(MCConstantExpr::create(0, Context)); 1228 return false; 1229 } 1230 1231 SmallVectorImpl<char> &Data = DF.getContents(); 1232 uint64_t OldSize = Data.size(); 1233 Data.clear(); 1234 DF.getFixups().clear(); 1235 1236 MCDwarfFrameEmitter::encodeAdvanceLoc(Context, Value, Data); 1237 return OldSize != Data.size(); 1238 } 1239 1240 bool MCAssembler::relaxCVInlineLineTable(MCCVInlineLineTableFragment &F) { 1241 unsigned OldSize = F.getContents().size(); 1242 getContext().getCVContext().encodeInlineLineTable(*this, F); 1243 return OldSize != F.getContents().size(); 1244 } 1245 1246 bool MCAssembler::relaxCVDefRange(MCCVDefRangeFragment &F) { 1247 unsigned OldSize = F.getContents().size(); 1248 getContext().getCVContext().encodeDefRange(*this, F); 1249 return OldSize != F.getContents().size(); 1250 } 1251 1252 bool MCAssembler::relaxPseudoProbeAddr(MCPseudoProbeAddrFragment &PF) { 1253 uint64_t OldSize = PF.getContents().size(); 1254 int64_t AddrDelta; 1255 bool Abs = PF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, *this); 1256 assert(Abs && "We created a pseudo probe with an invalid expression"); 1257 (void)Abs; 1258 SmallVectorImpl<char> &Data = PF.getContents(); 1259 Data.clear(); 1260 raw_svector_ostream OSE(Data); 1261 PF.getFixups().clear(); 1262 1263 // AddrDelta is a signed integer 1264 encodeSLEB128(AddrDelta, OSE, OldSize); 1265 return OldSize != Data.size(); 1266 } 1267 1268 bool MCAssembler::relaxFragment(MCFragment &F) { 1269 switch(F.getKind()) { 1270 default: 1271 return false; 1272 case MCFragment::FT_Relaxable: 1273 assert(!getRelaxAll() && 1274 "Did not expect a MCRelaxableFragment in RelaxAll mode"); 1275 return relaxInstruction(cast<MCRelaxableFragment>(F)); 1276 case MCFragment::FT_Dwarf: 1277 return relaxDwarfLineAddr(cast<MCDwarfLineAddrFragment>(F)); 1278 case MCFragment::FT_DwarfFrame: 1279 return relaxDwarfCallFrameFragment(cast<MCDwarfCallFrameFragment>(F)); 1280 case MCFragment::FT_LEB: 1281 return relaxLEB(cast<MCLEBFragment>(F)); 1282 case MCFragment::FT_BoundaryAlign: 1283 return relaxBoundaryAlign(cast<MCBoundaryAlignFragment>(F)); 1284 case MCFragment::FT_CVInlineLines: 1285 return relaxCVInlineLineTable(cast<MCCVInlineLineTableFragment>(F)); 1286 case MCFragment::FT_CVDefRange: 1287 return relaxCVDefRange(cast<MCCVDefRangeFragment>(F)); 1288 case MCFragment::FT_PseudoProbe: 1289 return relaxPseudoProbeAddr(cast<MCPseudoProbeAddrFragment>(F)); 1290 } 1291 } 1292 1293 bool MCAssembler::layoutOnce() { 1294 ++stats::RelaxationSteps; 1295 1296 bool Changed = false; 1297 for (MCSection &Sec : *this) 1298 for (MCFragment &Frag : Sec) 1299 if (relaxFragment(Frag)) 1300 Changed = true; 1301 return Changed; 1302 } 1303 1304 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1305 LLVM_DUMP_METHOD void MCAssembler::dump() const{ 1306 raw_ostream &OS = errs(); 1307 1308 OS << "<MCAssembler\n"; 1309 OS << " Sections:[\n "; 1310 bool First = true; 1311 for (const MCSection &Sec : *this) { 1312 if (First) 1313 First = false; 1314 else 1315 OS << ",\n "; 1316 Sec.dump(); 1317 } 1318 OS << "],\n"; 1319 OS << " Symbols:["; 1320 1321 First = true; 1322 for (const MCSymbol &Sym : symbols()) { 1323 if (First) 1324 First = false; 1325 else 1326 OS << ",\n "; 1327 OS << "("; 1328 Sym.dump(); 1329 OS << ", Index:" << Sym.getIndex() << ", "; 1330 OS << ")"; 1331 } 1332 OS << "]>\n"; 1333 } 1334 #endif 1335