| 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/SmallVector.h" |
| 12 | #include "llvm/ADT/Statistic.h" |
| 13 | #include "llvm/ADT/StringRef.h" |
| 14 | #include "llvm/ADT/Twine.h" |
| 15 | #include "llvm/MC/MCAsmBackend.h" |
| 16 | #include "llvm/MC/MCAsmInfo.h" |
| 17 | #include "llvm/MC/MCCodeEmitter.h" |
| 18 | #include "llvm/MC/MCCodeView.h" |
| 19 | #include "llvm/MC/MCContext.h" |
| 20 | #include "llvm/MC/MCDwarf.h" |
| 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCFixup.h" |
| 23 | #include "llvm/MC/MCInst.h" |
| 24 | #include "llvm/MC/MCObjectWriter.h" |
| 25 | #include "llvm/MC/MCSFrame.h" |
| 26 | #include "llvm/MC/MCSection.h" |
| 27 | #include "llvm/MC/MCSymbol.h" |
| 28 | #include "llvm/MC/MCValue.h" |
| 29 | #include "llvm/Support/Alignment.h" |
| 30 | #include "llvm/Support/Casting.h" |
| 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/EndianStream.h" |
| 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/LEB128.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
| 36 | #include <cassert> |
| 37 | #include <cstdint> |
| 38 | #include <tuple> |
| 39 | #include <utility> |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | namespace llvm { |
| 44 | class MCSubtargetInfo; |
| 45 | } |
| 46 | |
| 47 | #define DEBUG_TYPE "assembler" |
| 48 | |
| 49 | namespace { |
| 50 | namespace stats { |
| 51 | |
| 52 | STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total" ); |
| 53 | STATISTIC(EmittedRelaxableFragments, |
| 54 | "Number of emitted assembler fragments - relaxable" ); |
| 55 | STATISTIC(EmittedDataFragments, |
| 56 | "Number of emitted assembler fragments - data" ); |
| 57 | STATISTIC(EmittedAlignFragments, |
| 58 | "Number of emitted assembler fragments - align" ); |
| 59 | STATISTIC(EmittedFillFragments, |
| 60 | "Number of emitted assembler fragments - fill" ); |
| 61 | STATISTIC(EmittedNopsFragments, "Number of emitted assembler fragments - nops" ); |
| 62 | STATISTIC(EmittedOrgFragments, "Number of emitted assembler fragments - org" ); |
| 63 | STATISTIC(Fixups, "Number of fixups" ); |
| 64 | STATISTIC(FixupEvalForRelax, "Number of fixup evaluations for relaxation" ); |
| 65 | STATISTIC(ObjectBytes, "Number of emitted object file bytes" ); |
| 66 | STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps" ); |
| 67 | STATISTIC(RelaxedInstructions, "Number of relaxed instructions" ); |
| 68 | |
| 69 | } // end namespace stats |
| 70 | } // end anonymous namespace |
| 71 | |
| 72 | // FIXME FIXME FIXME: There are number of places in this file where we convert |
| 73 | // what is a 64-bit assembler value used for computation into a value in the |
| 74 | // object file, which may truncate it. We should detect that truncation where |
| 75 | // invalid and report errors back. |
| 76 | |
| 77 | /* *** */ |
| 78 | |
| 79 | MCAssembler::MCAssembler(MCContext &Context, |
| 80 | std::unique_ptr<MCAsmBackend> Backend, |
| 81 | std::unique_ptr<MCCodeEmitter> Emitter, |
| 82 | std::unique_ptr<MCObjectWriter> Writer) |
| 83 | : Context(Context), Backend(std::move(Backend)), |
| 84 | Emitter(std::move(Emitter)), Writer(std::move(Writer)) { |
| 85 | if (this->Backend) |
| 86 | this->Backend->setAssembler(this); |
| 87 | if (this->Writer) |
| 88 | this->Writer->setAssembler(this); |
| 89 | } |
| 90 | |
| 91 | void MCAssembler::reset() { |
| 92 | HasLayout = false; |
| 93 | HasFinalLayout = false; |
| 94 | RelaxAll = false; |
| 95 | BundleAlign.reset(); |
| 96 | Sections.clear(); |
| 97 | Symbols.clear(); |
| 98 | ThumbFuncs.clear(); |
| 99 | |
| 100 | // reset objects owned by us |
| 101 | if (getBackendPtr()) |
| 102 | getBackendPtr()->reset(); |
| 103 | if (getEmitterPtr()) |
| 104 | getEmitterPtr()->reset(); |
| 105 | if (Writer) |
| 106 | Writer->reset(); |
| 107 | } |
| 108 | |
| 109 | bool MCAssembler::registerSection(MCSection &Section) { |
| 110 | if (Section.isRegistered()) |
| 111 | return false; |
| 112 | Sections.push_back(Elt: &Section); |
| 113 | Section.setIsRegistered(true); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const { |
| 118 | if (ThumbFuncs.count(Ptr: Symbol)) |
| 119 | return true; |
| 120 | |
| 121 | if (!Symbol->isVariable()) |
| 122 | return false; |
| 123 | |
| 124 | const MCExpr *Expr = Symbol->getVariableValue(); |
| 125 | |
| 126 | MCValue V; |
| 127 | if (!Expr->evaluateAsRelocatable(Res&: V, Asm: nullptr)) |
| 128 | return false; |
| 129 | |
| 130 | if (V.getSubSym() || V.getSpecifier()) |
| 131 | return false; |
| 132 | |
| 133 | auto *Sym = V.getAddSym(); |
| 134 | if (!Sym || V.getSpecifier()) |
| 135 | return false; |
| 136 | |
| 137 | if (!isThumbFunc(Symbol: Sym)) |
| 138 | return false; |
| 139 | |
| 140 | ThumbFuncs.insert(Ptr: Symbol); // Cache it. |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | bool MCAssembler::evaluateFixup(const MCFragment &F, MCFixup &Fixup, |
| 145 | MCValue &Target, uint64_t &Value, |
| 146 | bool RecordReloc, uint8_t *Data) const { |
| 147 | if (RecordReloc) |
| 148 | ++stats::Fixups; |
| 149 | |
| 150 | // FIXME: This code has some duplication with recordRelocation. We should |
| 151 | // probably merge the two into a single callback that tries to evaluate a |
| 152 | // fixup and records a relocation if one is needed. |
| 153 | |
| 154 | // On error claim to have completely evaluated the fixup, to prevent any |
| 155 | // further processing from being done. |
| 156 | const MCExpr *Expr = Fixup.getValue(); |
| 157 | Value = 0; |
| 158 | if (!Expr->evaluateAsRelocatable(Res&: Target, Asm: this)) { |
| 159 | reportError(L: Fixup.getLoc(), Msg: "expected relocatable expression" ); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool IsResolved = false; |
| 164 | if (auto State = getBackend().evaluateFixup(F, Fixup, Target, Value)) { |
| 165 | IsResolved = *State; |
| 166 | } else { |
| 167 | const MCSymbol *Add = Target.getAddSym(); |
| 168 | const MCSymbol *Sub = Target.getSubSym(); |
| 169 | Value += Target.getConstant(); |
| 170 | if (Add && Add->isDefined()) |
| 171 | Value += getSymbolOffset(S: *Add); |
| 172 | if (Sub && Sub->isDefined()) |
| 173 | Value -= getSymbolOffset(S: *Sub); |
| 174 | |
| 175 | if (Fixup.isPCRel()) { |
| 176 | Value -= getFragmentOffset(F) + Fixup.getOffset(); |
| 177 | // During relaxation, F's offset is already updated but forward reference |
| 178 | // targets are stale. Add Stretch so that the displacement equals |
| 179 | // target_old - source_old, preventing premature relaxation. |
| 180 | if (Stretch) { |
| 181 | assert(!RecordReloc && |
| 182 | "Stretch should only be applied during relaxation" ); |
| 183 | MCFragment *AF = Add ? Add->getFragment() : nullptr; |
| 184 | if (AF && AF->getLayoutOrder() > F.getLayoutOrder()) |
| 185 | Value += Stretch; |
| 186 | MCFragment *SF = Sub ? Sub->getFragment() : nullptr; |
| 187 | if (SF && SF->getLayoutOrder() > F.getLayoutOrder()) |
| 188 | Value -= Stretch; |
| 189 | } |
| 190 | if (Add && !Sub && !Add->isUndefined() && !Add->isAbsolute()) { |
| 191 | IsResolved = getWriter().isSymbolRefDifferenceFullyResolvedImpl( |
| 192 | SymA: *Add, FB: F, InSet: false, IsPCRel: true); |
| 193 | } |
| 194 | } else { |
| 195 | IsResolved = Target.isAbsolute(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (!RecordReloc) |
| 200 | return IsResolved; |
| 201 | |
| 202 | if (IsResolved && mc::isRelocRelocation(FixupKind: Fixup.getKind())) |
| 203 | IsResolved = false; |
| 204 | getBackend().applyFixup(F, Fixup, Target, Data, Value, IsResolved); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | uint64_t MCAssembler::computeFragmentSize(const MCFragment &F) const { |
| 209 | assert(getBackendPtr() && "Requires assembler backend" ); |
| 210 | switch (F.getKind()) { |
| 211 | case MCFragment::FT_Data: |
| 212 | case MCFragment::FT_Relaxable: |
| 213 | case MCFragment::FT_Align: |
| 214 | case MCFragment::FT_LEB: |
| 215 | case MCFragment::FT_Dwarf: |
| 216 | case MCFragment::FT_DwarfFrame: |
| 217 | case MCFragment::FT_SFrame: |
| 218 | case MCFragment::FT_CVInlineLines: |
| 219 | case MCFragment::FT_CVDefRange: |
| 220 | return F.getSize(); |
| 221 | case MCFragment::FT_Fill: { |
| 222 | auto &FF = static_cast<const MCFillFragment &>(F); |
| 223 | int64_t NumValues = 0; |
| 224 | if (!FF.getNumValues().evaluateKnownAbsolute(Res&: NumValues, Asm: *this)) { |
| 225 | recordError(L: FF.getLoc(), Msg: "expected assembly-time absolute expression" ); |
| 226 | return 0; |
| 227 | } |
| 228 | int64_t Size = NumValues * FF.getValueSize(); |
| 229 | if (Size < 0) { |
| 230 | recordError(L: FF.getLoc(), Msg: "invalid number of bytes" ); |
| 231 | return 0; |
| 232 | } |
| 233 | return Size; |
| 234 | } |
| 235 | |
| 236 | case MCFragment::FT_PrefAlign: |
| 237 | return F.getSize(); |
| 238 | |
| 239 | case MCFragment::FT_Nops: |
| 240 | return cast<MCNopsFragment>(Val: F).getNumBytes(); |
| 241 | |
| 242 | case MCFragment::FT_BoundaryAlign: |
| 243 | return cast<MCBoundaryAlignFragment>(Val: F).getSize(); |
| 244 | |
| 245 | case MCFragment::FT_SymbolId: |
| 246 | return 4; |
| 247 | |
| 248 | case MCFragment::FT_Org: { |
| 249 | const MCOrgFragment &OF = cast<MCOrgFragment>(Val: F); |
| 250 | MCValue Value; |
| 251 | if (!OF.getOffset().evaluateAsValue(Res&: Value, Asm: *this)) { |
| 252 | recordError(L: OF.getLoc(), Msg: "expected assembly-time absolute expression" ); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | uint64_t FragmentOffset = getFragmentOffset(F: OF); |
| 257 | int64_t TargetLocation = Value.getConstant(); |
| 258 | if (const auto *SA = Value.getAddSym()) { |
| 259 | uint64_t Val; |
| 260 | if (!getSymbolOffset(S: *SA, Val)) { |
| 261 | recordError(L: OF.getLoc(), Msg: "expected absolute expression" ); |
| 262 | return 0; |
| 263 | } |
| 264 | TargetLocation += Val; |
| 265 | } |
| 266 | int64_t Size = TargetLocation - FragmentOffset; |
| 267 | if (Size < 0 || Size >= 0x40000000) { |
| 268 | recordError(L: OF.getLoc(), Msg: "invalid .org offset '" + Twine(TargetLocation) + |
| 269 | "' (at offset '" + Twine(FragmentOffset) + |
| 270 | "')" ); |
| 271 | return 0; |
| 272 | } |
| 273 | return Size; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | llvm_unreachable("invalid fragment kind" ); |
| 278 | } |
| 279 | |
| 280 | // Simple getSymbolOffset helper for the non-variable case. |
| 281 | static bool getLabelOffset(const MCAssembler &Asm, const MCSymbol &S, |
| 282 | bool ReportError, uint64_t &Val) { |
| 283 | if (!S.getFragment()) { |
| 284 | if (ReportError) |
| 285 | reportFatalUsageError(reason: "cannot evaluate undefined symbol '" + S.getName() + |
| 286 | "'" ); |
| 287 | return false; |
| 288 | } |
| 289 | Val = Asm.getFragmentOffset(F: *S.getFragment()) + S.getOffset(); |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | static bool getSymbolOffsetImpl(const MCAssembler &Asm, const MCSymbol &S, |
| 294 | bool ReportError, uint64_t &Val) { |
| 295 | if (!S.isVariable()) |
| 296 | return getLabelOffset(Asm, S, ReportError, Val); |
| 297 | |
| 298 | // If SD is a variable, evaluate it. |
| 299 | MCValue Target; |
| 300 | if (!S.getVariableValue()->evaluateAsValue(Res&: Target, Asm)) |
| 301 | reportFatalUsageError(reason: "cannot evaluate equated symbol '" + S.getName() + |
| 302 | "'" ); |
| 303 | |
| 304 | uint64_t Offset = Target.getConstant(); |
| 305 | |
| 306 | const MCSymbol *A = Target.getAddSym(); |
| 307 | if (A) { |
| 308 | uint64_t ValA; |
| 309 | // FIXME: On most platforms, `Target`'s component symbols are labels from |
| 310 | // having been simplified during evaluation, but on Mach-O they can be |
| 311 | // variables due to PR19203. This, and the line below for `B` can be |
| 312 | // restored to call `getLabelOffset` when PR19203 is fixed. |
| 313 | if (!getSymbolOffsetImpl(Asm, S: *A, ReportError, Val&: ValA)) |
| 314 | return false; |
| 315 | Offset += ValA; |
| 316 | } |
| 317 | |
| 318 | const MCSymbol *B = Target.getSubSym(); |
| 319 | if (B) { |
| 320 | uint64_t ValB; |
| 321 | if (!getSymbolOffsetImpl(Asm, S: *B, ReportError, Val&: ValB)) |
| 322 | return false; |
| 323 | Offset -= ValB; |
| 324 | } |
| 325 | |
| 326 | Val = Offset; |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | bool MCAssembler::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const { |
| 331 | return getSymbolOffsetImpl(Asm: *this, S, ReportError: false, Val); |
| 332 | } |
| 333 | |
| 334 | uint64_t MCAssembler::getSymbolOffset(const MCSymbol &S) const { |
| 335 | uint64_t Val; |
| 336 | getSymbolOffsetImpl(Asm: *this, S, ReportError: true, Val); |
| 337 | return Val; |
| 338 | } |
| 339 | |
| 340 | const MCSymbol *MCAssembler::getBaseSymbol(const MCSymbol &Symbol) const { |
| 341 | assert(HasLayout); |
| 342 | if (!Symbol.isVariable()) |
| 343 | return &Symbol; |
| 344 | |
| 345 | const MCExpr *Expr = Symbol.getVariableValue(); |
| 346 | MCValue Value; |
| 347 | if (!Expr->evaluateAsValue(Res&: Value, Asm: *this)) { |
| 348 | reportError(L: Expr->getLoc(), Msg: "expression could not be evaluated" ); |
| 349 | return nullptr; |
| 350 | } |
| 351 | |
| 352 | const MCSymbol *SymB = Value.getSubSym(); |
| 353 | if (SymB) { |
| 354 | reportError(L: Expr->getLoc(), |
| 355 | Msg: Twine("symbol '" ) + SymB->getName() + |
| 356 | "' could not be evaluated in a subtraction expression" ); |
| 357 | return nullptr; |
| 358 | } |
| 359 | |
| 360 | const MCSymbol *A = Value.getAddSym(); |
| 361 | if (!A) |
| 362 | return nullptr; |
| 363 | |
| 364 | const MCSymbol &ASym = *A; |
| 365 | if (ASym.isCommon()) { |
| 366 | reportError(L: Expr->getLoc(), Msg: "Common symbol '" + ASym.getName() + |
| 367 | "' cannot be used in assignment expr" ); |
| 368 | return nullptr; |
| 369 | } |
| 370 | |
| 371 | return &ASym; |
| 372 | } |
| 373 | |
| 374 | uint64_t MCAssembler::getSectionAddressSize(const MCSection &Sec) const { |
| 375 | const MCFragment &F = *Sec.curFragList()->Tail; |
| 376 | assert(HasLayout && F.getKind() == MCFragment::FT_Data); |
| 377 | return getFragmentOffset(F) + F.getSize(); |
| 378 | } |
| 379 | |
| 380 | uint64_t MCAssembler::getSectionFileSize(const MCSection &Sec) const { |
| 381 | // Virtual sections have no file size. |
| 382 | if (Sec.isBssSection()) |
| 383 | return 0; |
| 384 | return getSectionAddressSize(Sec); |
| 385 | } |
| 386 | |
| 387 | bool MCAssembler::registerSymbol(const MCSymbol &Symbol) { |
| 388 | bool Changed = !Symbol.isRegistered(); |
| 389 | if (Changed) { |
| 390 | Symbol.setIsRegistered(true); |
| 391 | Symbols.push_back(Elt: &Symbol); |
| 392 | } |
| 393 | return Changed; |
| 394 | } |
| 395 | |
| 396 | void MCAssembler::addRelocDirective(RelocDirective RD) { |
| 397 | relocDirectives.push_back(Elt: RD); |
| 398 | } |
| 399 | |
| 400 | /// Write \p NumBytes of NOPs at \p Offset in chunks of at most \p MaxNopSize. |
| 401 | /// When bundling is enabled, no chunk crosses a bundle boundary. |
| 402 | static void writeControlledNops(raw_ostream &OS, const MCAssembler &Asm, |
| 403 | uint64_t NumBytes, uint64_t Offset, |
| 404 | uint64_t MaxNopSize, |
| 405 | const MCSubtargetInfo *STI) { |
| 406 | while (NumBytes) { |
| 407 | uint64_t Size = std::min(a: NumBytes, b: MaxNopSize); |
| 408 | if (Asm.isBundlingEnabled()) { |
| 409 | uint64_t BundleSize = Asm.getBundleAlign().value(); |
| 410 | Size = std::min(a: Size, b: BundleSize - (Offset & (BundleSize - 1))); |
| 411 | } |
| 412 | assert(Size && "try to emit zero-sized NOP" ); |
| 413 | if (!Asm.getBackend().writeNopData(OS, Count: Size, STI)) |
| 414 | reportFatalInternalError(reason: "unable to write nop sequence of " + |
| 415 | Twine(Size) + " bytes" ); |
| 416 | NumBytes -= Size; |
| 417 | Offset += Size; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /// Write the fragment \p F to the output file. |
| 422 | static void writeFragment(raw_ostream &OS, const MCAssembler &Asm, |
| 423 | const MCFragment &F) { |
| 424 | // FIXME: Embed in fragments instead? |
| 425 | uint64_t FragmentSize = Asm.computeFragmentSize(F); |
| 426 | |
| 427 | llvm::endianness Endian = Asm.getBackend().Endian; |
| 428 | |
| 429 | // This variable (and its dummy usage) is to participate in the assert at |
| 430 | // the end of the function. |
| 431 | uint64_t Start = OS.tell(); |
| 432 | (void) Start; |
| 433 | |
| 434 | ++stats::EmittedFragments; |
| 435 | |
| 436 | switch (F.getKind()) { |
| 437 | case MCFragment::FT_Data: |
| 438 | case MCFragment::FT_Relaxable: |
| 439 | case MCFragment::FT_LEB: |
| 440 | case MCFragment::FT_Dwarf: |
| 441 | case MCFragment::FT_DwarfFrame: |
| 442 | case MCFragment::FT_SFrame: |
| 443 | case MCFragment::FT_CVInlineLines: |
| 444 | case MCFragment::FT_CVDefRange: { |
| 445 | if (F.getKind() == MCFragment::FT_Data) |
| 446 | ++stats::EmittedDataFragments; |
| 447 | else if (F.getKind() == MCFragment::FT_Relaxable) |
| 448 | ++stats::EmittedRelaxableFragments; |
| 449 | const auto &EF = cast<MCFragment>(Val: F); |
| 450 | OS << StringRef(EF.getContents().data(), EF.getContents().size()); |
| 451 | OS << StringRef(EF.getVarContents().data(), EF.getVarContents().size()); |
| 452 | } break; |
| 453 | |
| 454 | case MCFragment::FT_Align: { |
| 455 | ++stats::EmittedAlignFragments; |
| 456 | OS << StringRef(F.getContents().data(), F.getContents().size()); |
| 457 | assert(F.getAlignFillLen() && |
| 458 | "Invalid virtual align in concrete fragment!" ); |
| 459 | |
| 460 | uint64_t Count = (FragmentSize - F.getFixedSize()) / F.getAlignFillLen(); |
| 461 | assert((FragmentSize - F.getFixedSize()) % F.getAlignFillLen() == 0 && |
| 462 | "computeFragmentSize computed size is incorrect" ); |
| 463 | |
| 464 | // In the nops mode, call the backend hook to write `Count` nops. |
| 465 | if (F.hasAlignEmitNops()) { |
| 466 | writeControlledNops(OS, Asm, NumBytes: Count, |
| 467 | Offset: Asm.getFragmentOffset(F) + F.getFixedSize(), MaxNopSize: Count, |
| 468 | STI: F.getSubtargetInfo()); |
| 469 | } else { |
| 470 | // Otherwise, write out in multiples of the value size. |
| 471 | for (uint64_t i = 0; i != Count; ++i) { |
| 472 | switch (F.getAlignFillLen()) { |
| 473 | default: |
| 474 | llvm_unreachable("Invalid size!" ); |
| 475 | case 1: |
| 476 | OS << char(F.getAlignFill()); |
| 477 | break; |
| 478 | case 2: |
| 479 | support::endian::write<uint16_t>(os&: OS, value: F.getAlignFill(), endian: Endian); |
| 480 | break; |
| 481 | case 4: |
| 482 | support::endian::write<uint32_t>(os&: OS, value: F.getAlignFill(), endian: Endian); |
| 483 | break; |
| 484 | case 8: |
| 485 | support::endian::write<uint64_t>(os&: OS, value: F.getAlignFill(), endian: Endian); |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | } break; |
| 491 | |
| 492 | case MCFragment::FT_PrefAlign: { |
| 493 | OS << StringRef(F.getContents().data(), F.getContents().size()); |
| 494 | uint64_t PadSize = FragmentSize - F.getContents().size(); |
| 495 | if (F.getPrefAlignEmitNops()) { |
| 496 | if (!Asm.getBackend().writeNopData(OS, Count: PadSize, STI: F.getSubtargetInfo())) |
| 497 | reportFatalInternalError(reason: "unable to write nop sequence of " + |
| 498 | Twine(PadSize) + " bytes" ); |
| 499 | } else if (F.getPrefAlignFill() == 0) { |
| 500 | OS.write_zeros(NumZeros: PadSize); |
| 501 | } else { |
| 502 | char B = char(F.getPrefAlignFill()); |
| 503 | for (uint64_t I = 0; I < PadSize; ++I) |
| 504 | OS << B; |
| 505 | } |
| 506 | break; |
| 507 | } |
| 508 | |
| 509 | case MCFragment::FT_Fill: { |
| 510 | ++stats::EmittedFillFragments; |
| 511 | const MCFillFragment &FF = cast<MCFillFragment>(Val: F); |
| 512 | uint64_t V = FF.getValue(); |
| 513 | unsigned VSize = FF.getValueSize(); |
| 514 | const unsigned MaxChunkSize = 16; |
| 515 | char Data[MaxChunkSize]; |
| 516 | assert(0 < VSize && VSize <= MaxChunkSize && "Illegal fragment fill size" ); |
| 517 | // Duplicate V into Data as byte vector to reduce number of |
| 518 | // writes done. As such, do endian conversion here. |
| 519 | for (unsigned I = 0; I != VSize; ++I) { |
| 520 | unsigned index = Endian == llvm::endianness::little ? I : (VSize - I - 1); |
| 521 | Data[I] = uint8_t(V >> (index * 8)); |
| 522 | } |
| 523 | for (unsigned I = VSize; I < MaxChunkSize; ++I) |
| 524 | Data[I] = Data[I - VSize]; |
| 525 | |
| 526 | // Set to largest multiple of VSize in Data. |
| 527 | const unsigned NumPerChunk = MaxChunkSize / VSize; |
| 528 | // Set ChunkSize to largest multiple of VSize in Data |
| 529 | const unsigned ChunkSize = VSize * NumPerChunk; |
| 530 | |
| 531 | // Do copies by chunk. |
| 532 | StringRef Ref(Data, ChunkSize); |
| 533 | for (uint64_t I = 0, E = FragmentSize / ChunkSize; I != E; ++I) |
| 534 | OS << Ref; |
| 535 | |
| 536 | // do remainder if needed. |
| 537 | unsigned TrailingCount = FragmentSize % ChunkSize; |
| 538 | if (TrailingCount) |
| 539 | OS.write(Ptr: Data, Size: TrailingCount); |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | case MCFragment::FT_Nops: { |
| 544 | ++stats::EmittedNopsFragments; |
| 545 | const MCNopsFragment &NF = cast<MCNopsFragment>(Val: F); |
| 546 | |
| 547 | int64_t NumBytes = NF.getNumBytes(); |
| 548 | int64_t ControlledNopLength = NF.getControlledNopLength(); |
| 549 | int64_t MaximumNopLength = |
| 550 | Asm.getBackend().getMaximumNopSize(STI: *NF.getSubtargetInfo()); |
| 551 | |
| 552 | assert(NumBytes > 0 && "Expected positive NOPs fragment size" ); |
| 553 | assert(ControlledNopLength >= 0 && "Expected non-negative NOP size" ); |
| 554 | |
| 555 | if (ControlledNopLength > MaximumNopLength) { |
| 556 | Asm.reportError(L: NF.getLoc(), Msg: "illegal NOP size " + |
| 557 | std::to_string(val: ControlledNopLength) + |
| 558 | ". (expected within [0, " + |
| 559 | std::to_string(val: MaximumNopLength) + "])" ); |
| 560 | // Clamp the NOP length as reportError does not stop the execution |
| 561 | // immediately. |
| 562 | ControlledNopLength = MaximumNopLength; |
| 563 | } |
| 564 | |
| 565 | // Use maximum value if the size of each NOP is not specified |
| 566 | if (!ControlledNopLength) |
| 567 | ControlledNopLength = MaximumNopLength; |
| 568 | |
| 569 | writeControlledNops(OS, Asm, NumBytes: (uint64_t)NumBytes, Offset: Asm.getFragmentOffset(F: NF), |
| 570 | MaxNopSize: (uint64_t)ControlledNopLength, STI: NF.getSubtargetInfo()); |
| 571 | break; |
| 572 | } |
| 573 | |
| 574 | case MCFragment::FT_BoundaryAlign: { |
| 575 | const MCBoundaryAlignFragment &BF = cast<MCBoundaryAlignFragment>(Val: F); |
| 576 | writeControlledNops(OS, Asm, NumBytes: FragmentSize, Offset: Asm.getFragmentOffset(F: BF), |
| 577 | MaxNopSize: FragmentSize, STI: BF.getSubtargetInfo()); |
| 578 | break; |
| 579 | } |
| 580 | |
| 581 | case MCFragment::FT_SymbolId: { |
| 582 | const MCSymbolIdFragment &SF = cast<MCSymbolIdFragment>(Val: F); |
| 583 | support::endian::write<uint32_t>(os&: OS, value: SF.getSymbol()->getIndex(), endian: Endian); |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | case MCFragment::FT_Org: { |
| 588 | ++stats::EmittedOrgFragments; |
| 589 | const MCOrgFragment &OF = cast<MCOrgFragment>(Val: F); |
| 590 | |
| 591 | for (uint64_t i = 0, e = FragmentSize; i != e; ++i) |
| 592 | OS << char(OF.getValue()); |
| 593 | |
| 594 | break; |
| 595 | } |
| 596 | |
| 597 | } |
| 598 | |
| 599 | assert(OS.tell() - Start == FragmentSize && |
| 600 | "The stream should advance by fragment size" ); |
| 601 | } |
| 602 | |
| 603 | void MCAssembler::writeSectionData(raw_ostream &OS, |
| 604 | const MCSection *Sec) const { |
| 605 | assert(getBackendPtr() && "Expected assembler backend" ); |
| 606 | |
| 607 | if (Sec->isBssSection()) { |
| 608 | assert(getSectionFileSize(*Sec) == 0 && "Invalid size for section!" ); |
| 609 | |
| 610 | // Ensure no fixups or non-zero bytes are written to BSS sections, catching |
| 611 | // errors in both input assembly code and MCStreamer API usage. Location is |
| 612 | // not tracked for efficiency. |
| 613 | auto Fn = [](char c) { return c != 0; }; |
| 614 | for (const MCFragment &F : *Sec) { |
| 615 | bool HasNonZero = false; |
| 616 | switch (F.getKind()) { |
| 617 | default: |
| 618 | reportFatalInternalError(reason: "BSS section '" + Sec->getName() + |
| 619 | "' contains invalid fragment" ); |
| 620 | break; |
| 621 | case MCFragment::FT_Data: |
| 622 | case MCFragment::FT_Relaxable: |
| 623 | HasNonZero = |
| 624 | any_of(Range: F.getContents(), P: Fn) || any_of(Range: F.getVarContents(), P: Fn); |
| 625 | break; |
| 626 | case MCFragment::FT_Align: |
| 627 | // Disallowed for API usage. AsmParser changes non-zero fill values to |
| 628 | // 0. |
| 629 | assert(F.getAlignFill() == 0 && "Invalid align in virtual section!" ); |
| 630 | break; |
| 631 | case MCFragment::FT_PrefAlign: |
| 632 | assert(!F.getPrefAlignEmitNops() && F.getPrefAlignFill() == 0 && |
| 633 | "Invalid align in BSS" ); |
| 634 | break; |
| 635 | case MCFragment::FT_Fill: |
| 636 | HasNonZero = cast<MCFillFragment>(Val: F).getValue() != 0; |
| 637 | break; |
| 638 | case MCFragment::FT_Org: |
| 639 | HasNonZero = cast<MCOrgFragment>(Val: F).getValue() != 0; |
| 640 | break; |
| 641 | } |
| 642 | if (HasNonZero) { |
| 643 | reportError(L: SMLoc(), Msg: "BSS section '" + Sec->getName() + |
| 644 | "' cannot have non-zero bytes" ); |
| 645 | break; |
| 646 | } |
| 647 | if (F.getFixups().size() || F.getVarFixups().size()) { |
| 648 | reportError(L: SMLoc(), |
| 649 | Msg: "BSS section '" + Sec->getName() + "' cannot have fixups" ); |
| 650 | break; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | uint64_t Start = OS.tell(); |
| 658 | (void)Start; |
| 659 | |
| 660 | for (const MCFragment &F : *Sec) |
| 661 | writeFragment(OS, Asm: *this, F); |
| 662 | |
| 663 | flushPendingErrors(); |
| 664 | assert(getContext().hadError() || |
| 665 | OS.tell() - Start == getSectionAddressSize(*Sec)); |
| 666 | } |
| 667 | |
| 668 | void MCAssembler::layout() { |
| 669 | assert(getBackendPtr() && "Expected assembler backend" ); |
| 670 | DEBUG_WITH_TYPE("mc-dump-pre" , { |
| 671 | errs() << "assembler backend - pre-layout\n--\n" ; |
| 672 | dump(); |
| 673 | }); |
| 674 | |
| 675 | // Assign section ordinals. |
| 676 | unsigned SectionIndex = 0; |
| 677 | for (MCSection &Sec : *this) { |
| 678 | Sec.setOrdinal(SectionIndex++); |
| 679 | |
| 680 | // Chain together fragments from all subsections. |
| 681 | if (Sec.Subsections.size() > 1) { |
| 682 | MCFragment Dummy; |
| 683 | MCFragment *Tail = &Dummy; |
| 684 | for (auto &[_, List] : Sec.Subsections) { |
| 685 | assert(List.Head); |
| 686 | Tail->Next = List.Head; |
| 687 | Tail = List.Tail; |
| 688 | } |
| 689 | Sec.Subsections.clear(); |
| 690 | Sec.Subsections.push_back(Elt: {0u, {.Head: Dummy.getNext(), .Tail: Tail}}); |
| 691 | Sec.CurFragList = &Sec.Subsections[0].second; |
| 692 | |
| 693 | unsigned FragmentIndex = 0; |
| 694 | for (MCFragment &Frag : Sec) |
| 695 | Frag.setLayoutOrder(FragmentIndex++); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | // Layout until everything fits. |
| 700 | this->HasLayout = true; |
| 701 | for (MCSection &Sec : *this) |
| 702 | layoutSection(Sec); |
| 703 | unsigned FirstStable = Sections.size(); |
| 704 | while ((FirstStable = relaxOnce(FirstStable)) > 0) |
| 705 | if (getContext().hadError()) |
| 706 | return; |
| 707 | |
| 708 | // Some targets might want to adjust fragment offsets. If so, perform another |
| 709 | // layout iteration. |
| 710 | if (getBackend().finishLayout()) |
| 711 | for (MCSection &Sec : *this) |
| 712 | layoutSection(Sec); |
| 713 | |
| 714 | flushPendingErrors(); |
| 715 | |
| 716 | DEBUG_WITH_TYPE("mc-dump" , { |
| 717 | errs() << "assembler backend - final-layout\n--\n" ; |
| 718 | dump(); }); |
| 719 | |
| 720 | // Allow the object writer a chance to perform post-layout binding (for |
| 721 | // example, to set the index fields in the symbol data). |
| 722 | getWriter().executePostLayoutBinding(); |
| 723 | |
| 724 | // Fragment sizes are finalized. For RISC-V linker relaxation, this flag |
| 725 | // helps check whether a PC-relative fixup is fully resolved. |
| 726 | this->HasFinalLayout = true; |
| 727 | |
| 728 | // Stores the current .reloc group for each fragment. |
| 729 | // |
| 730 | // A .reloc group is a consecutive sequence of .reloc relocations that have |
| 731 | // an offset <= the first relocation's offset. A relocation with offset > the |
| 732 | // first relocation's offset starts a new group. Relocation groups are |
| 733 | // inserted in offset order using the offset of the first relocation, but the |
| 734 | // source ordering of relocations within the group is preserved. |
| 735 | DenseMap<MCFragment *, std::vector<MCFixup>> RelocGroups; |
| 736 | auto DrainRelocGroup = [](MCFragment *F, std::vector<MCFixup> &Group) { |
| 737 | F->insertRelocFixups(Fixups: Group); |
| 738 | Group.clear(); |
| 739 | }; |
| 740 | |
| 741 | // Resolve .reloc offsets and add fixups. |
| 742 | for (auto &PF : relocDirectives) { |
| 743 | MCValue Res; |
| 744 | auto &O = PF.Offset; |
| 745 | if (!O.evaluateAsValue(Res, Asm: *this)) { |
| 746 | getContext().reportError(L: O.getLoc(), Msg: ".reloc offset is not relocatable" ); |
| 747 | continue; |
| 748 | } |
| 749 | auto *Sym = Res.getAddSym(); |
| 750 | auto *F = Sym ? Sym->getFragment() : nullptr; |
| 751 | auto *Sec = F ? F->getParent() : nullptr; |
| 752 | if (Res.getSubSym() || !Sec) { |
| 753 | getContext().reportError(L: O.getLoc(), |
| 754 | Msg: ".reloc offset is not relative to a section" ); |
| 755 | continue; |
| 756 | } |
| 757 | |
| 758 | uint64_t Offset = Sym ? Sym->getOffset() + Res.getConstant() : 0; |
| 759 | auto Fixup = MCFixup::create(Offset, Value: PF.Expr, Kind: PF.Kind); |
| 760 | auto &Group = RelocGroups[F]; |
| 761 | if (!Group.empty() && Group[0].getOffset() < Offset) |
| 762 | DrainRelocGroup(F, Group); |
| 763 | Group.push_back(x: Fixup); |
| 764 | } |
| 765 | |
| 766 | for (auto &[F, Group] : RelocGroups) |
| 767 | DrainRelocGroup(F, Group); |
| 768 | |
| 769 | // Evaluate and apply the fixups, generating relocation entries as necessary. |
| 770 | for (MCSection &Sec : *this) { |
| 771 | for (MCFragment &F : Sec) { |
| 772 | // Process fragments with fixups here. |
| 773 | auto Contents = F.getContents(); |
| 774 | for (MCFixup &Fixup : F.getFixups()) { |
| 775 | uint64_t FixedValue; |
| 776 | MCValue Target; |
| 777 | assert(mc::isRelocRelocation(Fixup.getKind()) || |
| 778 | Fixup.getOffset() <= F.getFixedSize()); |
| 779 | auto *Data = |
| 780 | reinterpret_cast<uint8_t *>(Contents.data() + Fixup.getOffset()); |
| 781 | evaluateFixup(F, Fixup, Target, Value&: FixedValue, |
| 782 | /*RecordReloc=*/true, Data); |
| 783 | } |
| 784 | // In the variable part, fixup offsets are relative to the fixed part's |
| 785 | // start. |
| 786 | for (MCFixup &Fixup : F.getVarFixups()) { |
| 787 | uint64_t FixedValue; |
| 788 | MCValue Target; |
| 789 | assert(mc::isRelocRelocation(Fixup.getKind()) || |
| 790 | (Fixup.getOffset() >= F.getFixedSize() && |
| 791 | Fixup.getOffset() <= F.getSize())); |
| 792 | auto *Data = reinterpret_cast<uint8_t *>( |
| 793 | F.getVarContents().data() + (Fixup.getOffset() - F.getFixedSize())); |
| 794 | evaluateFixup(F, Fixup, Target, Value&: FixedValue, |
| 795 | /*RecordReloc=*/true, Data); |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | void MCAssembler::Finish() { |
| 802 | layout(); |
| 803 | |
| 804 | // Write the object file if there is no error. The output would be discarded |
| 805 | // anyway, and this avoids wasting time writing large files (e.g. when testing |
| 806 | // fixup overflow with `.space 0x80000000`). |
| 807 | if (!getContext().hadError()) |
| 808 | stats::ObjectBytes += getWriter().writeObject(); |
| 809 | |
| 810 | HasLayout = false; |
| 811 | assert(PendingErrors.empty()); |
| 812 | } |
| 813 | |
| 814 | void MCAssembler::relaxAlign(MCFragment &F) { |
| 815 | uint64_t Offset = F.Offset + F.getFixedSize(); |
| 816 | unsigned Size = offsetToAlignment(Value: Offset, Alignment: F.getAlignment()); |
| 817 | bool AlignFixup = false; |
| 818 | if (F.hasAlignEmitNops()) { |
| 819 | AlignFixup = getBackend().relaxAlign(F, Size); |
| 820 | if (!AlignFixup) |
| 821 | while (Size % getBackend().getMinimumNopSize()) |
| 822 | Size += F.getAlignment().value(); |
| 823 | } |
| 824 | if (!AlignFixup && Size > F.getAlignMaxBytesToEmit()) |
| 825 | Size = 0; |
| 826 | F.VarContentStart = F.getFixedSize(); |
| 827 | F.VarContentEnd = F.VarContentStart + Size; |
| 828 | if (F.VarContentEnd > F.getParent()->ContentStorage.size()) |
| 829 | F.getParent()->ContentStorage.resize(N: F.VarContentEnd); |
| 830 | } |
| 831 | |
| 832 | // Compute the body size by walking forward from F to the End symbol and |
| 833 | // summing fragment sizes. This avoids depending on stale layout offsets. |
| 834 | void MCAssembler::relaxPrefAlign(MCFragment &F) { |
| 835 | uint64_t RawStart = F.Offset + F.getFixedSize(); |
| 836 | const MCSymbol &End = F.getPrefAlignEnd(); |
| 837 | if (!End.getFragment() || End.getFragment()->getParent() != F.getParent()) { |
| 838 | recordError(L: SMLoc(), Msg: ".prefalign end symbol '" + End.getName() + |
| 839 | "' must be in the current section" ); |
| 840 | return; |
| 841 | } |
| 842 | const MCFragment *EndFrag = End.getFragment(); |
| 843 | if (EndFrag->getLayoutOrder() <= F.getLayoutOrder()) |
| 844 | return; |
| 845 | uint64_t BodySize = End.getOffset(); |
| 846 | for (auto *Cur = F.getNext(); Cur != EndFrag; Cur = Cur->getNext()) |
| 847 | BodySize += computeFragmentSize(F: *Cur); |
| 848 | // Intervening FT_Align's padding depends on where this prefalign lands, so |
| 849 | // `BodySize` depends on this prefalign's own padding and may not reach a |
| 850 | // fixed point. Break the cycle with a monotone value. |
| 851 | Align NewAlign = |
| 852 | std::min(a: Align(llvm::bit_ceil(Value: BodySize)), b: F.getPrefAlignPreferred()); |
| 853 | NewAlign = std::max(a: NewAlign, b: F.getPrefAlignComputed()); |
| 854 | F.setPrefAlignComputed(NewAlign); |
| 855 | uint64_t NewPadSize = offsetToAlignment(Value: RawStart, Alignment: NewAlign); |
| 856 | F.VarContentStart = F.getFixedSize(); |
| 857 | F.VarContentEnd = F.VarContentStart + NewPadSize; |
| 858 | if (F.VarContentEnd > F.getParent()->ContentStorage.size()) |
| 859 | F.getParent()->ContentStorage.resize(N: F.VarContentEnd); |
| 860 | // Update the maximum alignment on the current section if necessary, similar |
| 861 | // to MCObjectStreamer::emitValueToAlignment. |
| 862 | F.getParent()->ensureMinAlignment(MinAlignment: NewAlign); |
| 863 | } |
| 864 | |
| 865 | bool MCAssembler::fixupNeedsRelaxation(const MCFragment &F, |
| 866 | const MCFixup &Fixup) const { |
| 867 | ++stats::FixupEvalForRelax; |
| 868 | MCValue Target; |
| 869 | uint64_t Value; |
| 870 | bool Resolved = evaluateFixup(F, Fixup&: const_cast<MCFixup &>(Fixup), Target, Value, |
| 871 | /*RecordReloc=*/false, Data: {}); |
| 872 | return getBackend().fixupNeedsRelaxationAdvanced(F, Fixup, Target, Value, |
| 873 | Resolved); |
| 874 | } |
| 875 | |
| 876 | void MCAssembler::relaxInstruction(MCFragment &F) { |
| 877 | assert(getEmitterPtr() && |
| 878 | "Expected CodeEmitter defined for relaxInstruction" ); |
| 879 | // If this inst doesn't ever need relaxation, ignore it. This occurs when we |
| 880 | // are intentionally pushing out inst fragments, or because we relaxed a |
| 881 | // previous instruction to one that doesn't need relaxation. |
| 882 | if (!getBackend().mayNeedRelaxation(Opcode: F.getOpcode(), Operands: F.getOperands(), |
| 883 | STI: *F.getSubtargetInfo())) |
| 884 | return; |
| 885 | |
| 886 | bool DoRelax = false; |
| 887 | for (const MCFixup &Fixup : F.getVarFixups()) |
| 888 | if ((DoRelax = fixupNeedsRelaxation(F, Fixup))) |
| 889 | break; |
| 890 | if (!DoRelax) |
| 891 | return; |
| 892 | |
| 893 | ++stats::RelaxedInstructions; |
| 894 | |
| 895 | // TODO Refactor relaxInstruction to accept MCFragment and remove |
| 896 | // `setInst`. |
| 897 | MCInst Relaxed = F.getInst(); |
| 898 | getBackend().relaxInstruction(Inst&: Relaxed, STI: *F.getSubtargetInfo()); |
| 899 | |
| 900 | // Encode the new instruction. |
| 901 | F.setInst(Relaxed); |
| 902 | SmallVector<char, 16> Data; |
| 903 | SmallVector<MCFixup, 1> Fixups; |
| 904 | getEmitter().encodeInstruction(Inst: Relaxed, CB&: Data, Fixups, STI: *F.getSubtargetInfo()); |
| 905 | F.setVarContents(Data); |
| 906 | F.setVarFixups(Fixups); |
| 907 | } |
| 908 | |
| 909 | void MCAssembler::relaxLEB(MCFragment &F) { |
| 910 | unsigned PadTo = F.getVarSize(); |
| 911 | int64_t Value; |
| 912 | F.clearVarFixups(); |
| 913 | // Use evaluateKnownAbsolute for Mach-O as a hack: .subsections_via_symbols |
| 914 | // requires that .uleb128 A-B is foldable where A and B reside in different |
| 915 | // fragments. This is used by __gcc_except_table. |
| 916 | bool Abs = getWriter().getSubsectionsViaSymbols() |
| 917 | ? F.getLEBValue().evaluateKnownAbsolute(Res&: Value, Asm: *this) |
| 918 | : F.getLEBValue().evaluateAsAbsolute(Res&: Value, Asm: *this); |
| 919 | if (!Abs) { |
| 920 | bool Relaxed, UseZeroPad; |
| 921 | std::tie(args&: Relaxed, args&: UseZeroPad) = getBackend().relaxLEB128(F, Value); |
| 922 | if (!Relaxed) { |
| 923 | reportError(L: F.getLEBValue().getLoc(), |
| 924 | Msg: Twine(F.isLEBSigned() ? ".s" : ".u" ) + |
| 925 | "leb128 expression is not absolute" ); |
| 926 | F.setLEBValue(MCConstantExpr::create(Value: 0, Ctx&: Context)); |
| 927 | } |
| 928 | uint8_t Tmp[10]; // maximum size: ceil(64/7) |
| 929 | PadTo = std::max(a: PadTo, b: encodeULEB128(Value: uint64_t(Value), p: Tmp)); |
| 930 | if (UseZeroPad) |
| 931 | Value = 0; |
| 932 | } |
| 933 | uint8_t Data[16]; |
| 934 | size_t Size = 0; |
| 935 | // The compiler can generate EH table assembly that is impossible to assemble |
| 936 | // without either adding padding to an LEB fragment or adding extra padding |
| 937 | // to a later alignment fragment. To accommodate such tables, relaxation can |
| 938 | // only increase an LEB fragment size here, not decrease it. See PR35809. |
| 939 | if (F.isLEBSigned()) |
| 940 | Size = encodeSLEB128(Value, p: Data, PadTo); |
| 941 | else |
| 942 | Size = encodeULEB128(Value, p: Data, PadTo); |
| 943 | F.setVarContents({reinterpret_cast<char *>(Data), Size}); |
| 944 | } |
| 945 | |
| 946 | /// Check if the branch crosses the boundary. |
| 947 | /// |
| 948 | /// \param StartAddr start address of the fused/unfused branch. |
| 949 | /// \param Size size of the fused/unfused branch. |
| 950 | /// \param BoundaryAlignment alignment requirement of the branch. |
| 951 | /// \returns true if the branch cross the boundary. |
| 952 | static bool mayCrossBoundary(uint64_t StartAddr, uint64_t Size, |
| 953 | Align BoundaryAlignment) { |
| 954 | uint64_t EndAddr = StartAddr + Size; |
| 955 | return (StartAddr >> Log2(A: BoundaryAlignment)) != |
| 956 | ((EndAddr - 1) >> Log2(A: BoundaryAlignment)); |
| 957 | } |
| 958 | |
| 959 | /// Check if the branch is against the boundary. |
| 960 | /// |
| 961 | /// \param StartAddr start address of the fused/unfused branch. |
| 962 | /// \param Size size of the fused/unfused branch. |
| 963 | /// \param BoundaryAlignment alignment requirement of the branch. |
| 964 | /// \returns true if the branch is against the boundary. |
| 965 | static bool isAgainstBoundary(uint64_t StartAddr, uint64_t Size, |
| 966 | Align BoundaryAlignment) { |
| 967 | uint64_t EndAddr = StartAddr + Size; |
| 968 | return (EndAddr & (BoundaryAlignment.value() - 1)) == 0; |
| 969 | } |
| 970 | |
| 971 | /// Check if the branch needs padding. |
| 972 | /// |
| 973 | /// \param StartAddr start address of the fused/unfused branch. |
| 974 | /// \param Size size of the fused/unfused branch. |
| 975 | /// \param BoundaryAlignment alignment requirement of the branch. |
| 976 | /// \returns true if the branch needs padding. |
| 977 | static bool needPadding(uint64_t StartAddr, uint64_t Size, |
| 978 | Align BoundaryAlignment) { |
| 979 | return mayCrossBoundary(StartAddr, Size, BoundaryAlignment) || |
| 980 | isAgainstBoundary(StartAddr, Size, BoundaryAlignment); |
| 981 | } |
| 982 | |
| 983 | /// Compute the padding size to boundary-align the fragments BF is responsible |
| 984 | /// for. |
| 985 | static uint64_t computeBoundaryAlignSize(const MCAssembler &Asm, |
| 986 | const MCBoundaryAlignFragment &BF) { |
| 987 | assert(BF.getLastFragment() && "the fragment range to align must be known" ); |
| 988 | |
| 989 | uint64_t AlignedOffset = Asm.getFragmentOffset(F: BF); |
| 990 | uint64_t AlignedSize = 0; |
| 991 | for (const MCFragment *F = BF.getNext();; F = F->getNext()) { |
| 992 | AlignedSize += Asm.computeFragmentSize(F: *F); |
| 993 | if (F == BF.getLastFragment()) |
| 994 | break; |
| 995 | } |
| 996 | |
| 997 | Align BoundaryAlignment = BF.getAlignment(); |
| 998 | |
| 999 | if (!Asm.isBundlingEnabled()) |
| 1000 | return needPadding(StartAddr: AlignedOffset, Size: AlignedSize, BoundaryAlignment) |
| 1001 | ? offsetToAlignment(Value: AlignedOffset, Alignment: BoundaryAlignment) |
| 1002 | : 0U; |
| 1003 | if (BF.isAlignToEnd()) |
| 1004 | return offsetToAlignment(Value: AlignedOffset + AlignedSize, Alignment: BoundaryAlignment); |
| 1005 | |
| 1006 | // For bundle alignment, we only pad instructions that cross the boundary. |
| 1007 | return mayCrossBoundary(StartAddr: AlignedOffset, Size: AlignedSize, BoundaryAlignment) |
| 1008 | ? offsetToAlignment(Value: AlignedOffset, Alignment: BoundaryAlignment) |
| 1009 | : 0U; |
| 1010 | } |
| 1011 | |
| 1012 | void MCAssembler::relaxBoundaryAlign(MCBoundaryAlignFragment &BF) { |
| 1013 | // BoundaryAlignFragment that doesn't need to align any fragment should not be |
| 1014 | // relaxed. |
| 1015 | if (!BF.getLastFragment()) |
| 1016 | return; |
| 1017 | |
| 1018 | uint64_t NewSize = computeBoundaryAlignSize(Asm: *this, BF); |
| 1019 | if (NewSize == BF.getSize()) |
| 1020 | return; |
| 1021 | BF.setSize(NewSize); |
| 1022 | } |
| 1023 | |
| 1024 | void MCAssembler::relaxDwarfLineAddr(MCFragment &F) { |
| 1025 | if (getBackend().relaxDwarfLineAddr(F)) |
| 1026 | return; |
| 1027 | |
| 1028 | MCContext &Context = getContext(); |
| 1029 | int64_t AddrDelta; |
| 1030 | bool Abs = F.getDwarfAddrDelta().evaluateKnownAbsolute(Res&: AddrDelta, Asm: *this); |
| 1031 | assert(Abs && "We created a line delta with an invalid expression" ); |
| 1032 | (void)Abs; |
| 1033 | SmallVector<char, 8> Data; |
| 1034 | MCDwarfLineAddr::encode(Context, Params: getDWARFLinetableParams(), |
| 1035 | LineDelta: F.getDwarfLineDelta(), AddrDelta, OS&: Data); |
| 1036 | F.setVarContents(Data); |
| 1037 | F.clearVarFixups(); |
| 1038 | } |
| 1039 | |
| 1040 | void MCAssembler::relaxDwarfCallFrameFragment(MCFragment &F) { |
| 1041 | if (getBackend().relaxDwarfCFA(F)) |
| 1042 | return; |
| 1043 | |
| 1044 | MCContext &Context = getContext(); |
| 1045 | int64_t Value; |
| 1046 | bool Abs = F.getDwarfAddrDelta().evaluateAsAbsolute(Res&: Value, Asm: *this); |
| 1047 | if (!Abs) { |
| 1048 | reportError(L: F.getDwarfAddrDelta().getLoc(), |
| 1049 | Msg: "invalid CFI advance_loc expression" ); |
| 1050 | F.setDwarfAddrDelta(MCConstantExpr::create(Value: 0, Ctx&: Context)); |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | SmallVector<char, 8> Data; |
| 1055 | MCDwarfFrameEmitter::encodeAdvanceLoc(Context, AddrDelta: Value, OS&: Data); |
| 1056 | F.setVarContents(Data); |
| 1057 | F.clearVarFixups(); |
| 1058 | } |
| 1059 | |
| 1060 | void MCAssembler::relaxSFrameFragment(MCFragment &F) { |
| 1061 | assert(F.getKind() == MCFragment::FT_SFrame); |
| 1062 | MCContext &C = getContext(); |
| 1063 | int64_t Value; |
| 1064 | bool Abs = F.getSFrameAddrDelta().evaluateAsAbsolute(Res&: Value, Asm: *this); |
| 1065 | if (!Abs) { |
| 1066 | C.reportError(L: F.getSFrameAddrDelta().getLoc(), |
| 1067 | Msg: "invalid CFI advance_loc expression in sframe" ); |
| 1068 | F.setSFrameAddrDelta(MCConstantExpr::create(Value: 0, Ctx&: C)); |
| 1069 | return; |
| 1070 | } |
| 1071 | |
| 1072 | SmallVector<char, 4> Data; |
| 1073 | MCSFrameEmitter::encodeFuncOffset(C&: Context, Offset: Value, Out&: Data, FDEFrag: F.getSFrameFDE()); |
| 1074 | F.setVarContents(Data); |
| 1075 | F.clearVarFixups(); |
| 1076 | } |
| 1077 | |
| 1078 | void MCAssembler::relaxFragment(MCFragment &F) { |
| 1079 | switch (F.getKind()) { |
| 1080 | default: |
| 1081 | return; |
| 1082 | case MCFragment::FT_Align: |
| 1083 | relaxAlign(F); |
| 1084 | break; |
| 1085 | case MCFragment::FT_Relaxable: |
| 1086 | // Bundling emits every instruction as relaxable, so FT_Relaxable is |
| 1087 | // expected with RelaxAll mode once bundling is enabled. |
| 1088 | assert((isBundlingEnabled() || !getRelaxAll()) && |
| 1089 | "Did not expect a FT_Relaxable in RelaxAll mode" ); |
| 1090 | relaxInstruction(F); |
| 1091 | break; |
| 1092 | case MCFragment::FT_LEB: |
| 1093 | relaxLEB(F); |
| 1094 | break; |
| 1095 | case MCFragment::FT_Dwarf: |
| 1096 | relaxDwarfLineAddr(F); |
| 1097 | break; |
| 1098 | case MCFragment::FT_DwarfFrame: |
| 1099 | relaxDwarfCallFrameFragment(F); |
| 1100 | break; |
| 1101 | case MCFragment::FT_SFrame: |
| 1102 | relaxSFrameFragment(F); |
| 1103 | break; |
| 1104 | case MCFragment::FT_BoundaryAlign: |
| 1105 | relaxBoundaryAlign(BF&: static_cast<MCBoundaryAlignFragment &>(F)); |
| 1106 | break; |
| 1107 | case MCFragment::FT_PrefAlign: |
| 1108 | relaxPrefAlign(F); |
| 1109 | break; |
| 1110 | case MCFragment::FT_CVInlineLines: |
| 1111 | getContext().getCVContext().encodeInlineLineTable( |
| 1112 | Asm: *this, F&: static_cast<MCCVInlineLineTableFragment &>(F)); |
| 1113 | break; |
| 1114 | case MCFragment::FT_CVDefRange: |
| 1115 | getContext().getCVContext().encodeDefRange( |
| 1116 | Asm: *this, F&: static_cast<MCCVDefRangeFragment &>(F)); |
| 1117 | break; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | void MCAssembler::layoutSection(MCSection &Sec) { |
| 1122 | uint64_t Offset = 0; |
| 1123 | for (MCFragment &F : Sec) { |
| 1124 | F.Offset = Offset; |
| 1125 | if (F.getKind() == MCFragment::FT_Align) |
| 1126 | relaxAlign(F); |
| 1127 | Offset += computeFragmentSize(F); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | // Fused relaxation and layout: a single forward pass that updates each |
| 1132 | // fragment's offset before processing it, so upstream size changes are |
| 1133 | // immediately visible. |
| 1134 | unsigned MCAssembler::relaxOnce(unsigned FirstStable) { |
| 1135 | uint64_t MaxIterations = 0; |
| 1136 | PendingErrors.clear(); |
| 1137 | unsigned Res = 0; |
| 1138 | for (unsigned I = 0; I != FirstStable; ++I) { |
| 1139 | auto &Sec = *Sections[I]; |
| 1140 | uint64_t Iters = 0; |
| 1141 | for (;;) { |
| 1142 | bool Changed = false; |
| 1143 | uint64_t Offset = 0; |
| 1144 | for (MCFragment &F : Sec) { |
| 1145 | if (F.Offset != Offset) |
| 1146 | Changed = true; |
| 1147 | Stretch = Offset - F.Offset; |
| 1148 | F.Offset = Offset; |
| 1149 | if (F.getKind() != MCFragment::FT_Data) |
| 1150 | relaxFragment(F); |
| 1151 | Offset += computeFragmentSize(F); |
| 1152 | } |
| 1153 | ++Iters; |
| 1154 | |
| 1155 | if (!Changed) |
| 1156 | break; |
| 1157 | // If any fragment changed size, it might impact the layout of subsequent |
| 1158 | // sections. Therefore, we must re-evaluate all sections. |
| 1159 | FirstStable = Sections.size(); |
| 1160 | Res = I; |
| 1161 | // Assume each iteration finalizes at least one extra fragment. If the |
| 1162 | // layout does not converge after N+1 iterations, bail out. |
| 1163 | if (Iters > Sec.curFragList()->Tail->getLayoutOrder()) |
| 1164 | break; |
| 1165 | } |
| 1166 | MaxIterations = std::max(a: MaxIterations, b: Iters); |
| 1167 | } |
| 1168 | stats::RelaxationSteps += MaxIterations; |
| 1169 | Stretch = 0; |
| 1170 | // The subsequent relaxOnce call only needs to visit Sections [0,Res) if no |
| 1171 | // change occurred. |
| 1172 | return Res; |
| 1173 | } |
| 1174 | |
| 1175 | void MCAssembler::reportError(SMLoc L, const Twine &Msg) const { |
| 1176 | getContext().reportError(L, Msg); |
| 1177 | } |
| 1178 | |
| 1179 | void MCAssembler::recordError(SMLoc Loc, const Twine &Msg) const { |
| 1180 | PendingErrors.emplace_back(Args&: Loc, Args: Msg.str()); |
| 1181 | } |
| 1182 | |
| 1183 | void MCAssembler::flushPendingErrors() const { |
| 1184 | for (auto &Err : PendingErrors) |
| 1185 | reportError(L: Err.first, Msg: Err.second); |
| 1186 | PendingErrors.clear(); |
| 1187 | } |
| 1188 | |
| 1189 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 1190 | LLVM_DUMP_METHOD void MCAssembler::dump() const{ |
| 1191 | raw_ostream &OS = errs(); |
| 1192 | DenseMap<const MCFragment *, SmallVector<const MCSymbol *, 0>> FragToSyms; |
| 1193 | // Scan symbols and build a map of fragments to their corresponding symbols. |
| 1194 | // For variable symbols, we don't want to call their getFragment, which might |
| 1195 | // modify `Fragment`. |
| 1196 | for (const MCSymbol &Sym : symbols()) |
| 1197 | if (!Sym.isVariable()) |
| 1198 | if (auto *F = Sym.getFragment()) |
| 1199 | FragToSyms.try_emplace(F).first->second.push_back(&Sym); |
| 1200 | |
| 1201 | OS << "Sections:[" ; |
| 1202 | for (const MCSection &Sec : *this) { |
| 1203 | OS << '\n'; |
| 1204 | Sec.dump(&FragToSyms); |
| 1205 | } |
| 1206 | OS << "\n]\n" ; |
| 1207 | } |
| 1208 | #endif |
| 1209 | |
| 1210 | SMLoc MCFixup::getLoc() const { |
| 1211 | if (auto *E = getValue()) |
| 1212 | return E->getLoc(); |
| 1213 | return {}; |
| 1214 | } |
| 1215 | |