| 1 | //===- lib/MC/MCSection.cpp - Machine Code Section Representation ---------===// |
| 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/MCSection.h" |
| 10 | #include "llvm/ADT/SmallVector.h" |
| 11 | #include "llvm/Config/llvm-config.h" |
| 12 | #include "llvm/MC/MCContext.h" |
| 13 | #include "llvm/MC/MCSymbol.h" |
| 14 | #include "llvm/Support/Compiler.h" |
| 15 | #include "llvm/Support/ErrorHandling.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include <utility> |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | MCSection::MCSection(StringRef Name, bool IsText, bool IsBss, MCSymbol *Begin) |
| 22 | : Begin(Begin), HasInstructions(false), IsRegistered(false), IsText(IsText), |
| 23 | IsBss(IsBss), Name(Name) { |
| 24 | DummyFragment.setParent(this); |
| 25 | } |
| 26 | |
| 27 | MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) { |
| 28 | if (!End) |
| 29 | End = Ctx.createTempSymbol(Name: "sec_end" ); |
| 30 | return End; |
| 31 | } |
| 32 | |
| 33 | Align MCSection::getAlignmentForObjectFile(uint64_t Size) const { |
| 34 | if (Size < getAlign().value()) |
| 35 | return getAlign(); |
| 36 | |
| 37 | if (Size < getPreferredAlignment().value()) |
| 38 | return Align(NextPowerOf2(A: Size - 1)); |
| 39 | |
| 40 | return getPreferredAlignment(); |
| 41 | } |
| 42 | |
| 43 | bool MCSection::hasEnded() const { return End && End->isInSection(); } |
| 44 | |
| 45 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 46 | LLVM_DUMP_METHOD void MCSection::dump( |
| 47 | DenseMap<const MCFragment *, SmallVector<const MCSymbol *, 0>> *FragToSyms) |
| 48 | const { |
| 49 | raw_ostream &OS = errs(); |
| 50 | |
| 51 | OS << "MCSection Name:" << getName(); |
| 52 | if (isLinkerRelaxable()) |
| 53 | OS << " FirstLinkerRelaxable:" << firstLinkerRelaxable(); |
| 54 | for (auto &F : *this) { |
| 55 | OS << '\n'; |
| 56 | F.dump(); |
| 57 | if (!FragToSyms) |
| 58 | continue; |
| 59 | auto It = FragToSyms->find(&F); |
| 60 | if (It == FragToSyms->end()) |
| 61 | continue; |
| 62 | for (auto *Sym : It->second) { |
| 63 | OS << "\n Symbol @" << Sym->getOffset() << ' ' << Sym->getName(); |
| 64 | if (Sym->isTemporary()) |
| 65 | OS << " Temporary" ; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | void MCFragment::setVarContents(ArrayRef<char> Contents) { |
| 72 | auto &S = getParent()->ContentStorage; |
| 73 | if (VarContentStart + Contents.size() > VarContentEnd) { |
| 74 | VarContentStart = S.size(); |
| 75 | S.resize_for_overwrite(N: S.size() + Contents.size()); |
| 76 | } |
| 77 | VarContentEnd = VarContentStart + Contents.size(); |
| 78 | llvm::copy(Range&: Contents, Out: S.begin() + VarContentStart); |
| 79 | } |
| 80 | |
| 81 | void MCFragment::addFixup(MCFixup Fixup) { appendFixups(Fixups: {Fixup}); } |
| 82 | |
| 83 | void MCFragment::appendFixups(ArrayRef<MCFixup> Fixups) { |
| 84 | auto &S = getParent()->FixupStorage; |
| 85 | if (LLVM_UNLIKELY(FixupEnd != S.size())) { |
| 86 | // Move the elements to the end. Reserve space to avoid invalidating |
| 87 | // S.begin()+I for `append`. |
| 88 | auto Size = FixupEnd - FixupStart; |
| 89 | auto I = std::exchange(obj&: FixupStart, new_val: S.size()); |
| 90 | S.reserve(N: S.size() + Size); |
| 91 | S.append(in_start: S.begin() + I, in_end: S.begin() + I + Size); |
| 92 | } |
| 93 | S.append(in_start: Fixups.begin(), in_end: Fixups.end()); |
| 94 | FixupEnd = S.size(); |
| 95 | } |
| 96 | |
| 97 | void MCFragment::setVarFixups(ArrayRef<MCFixup> Fixups) { |
| 98 | assert(Fixups.size() < 256 && |
| 99 | "variable-size tail cannot have more than 256 fixups" ); |
| 100 | auto &S = getParent()->FixupStorage; |
| 101 | if (Fixups.size() > VarFixupSize) { |
| 102 | VarFixupStart = S.size(); |
| 103 | S.resize_for_overwrite(N: S.size() + Fixups.size()); |
| 104 | } |
| 105 | VarFixupSize = Fixups.size(); |
| 106 | // Source fixup offsets are relative to the variable part's start. Add the |
| 107 | // fixed part size to make them relative to the fixed part's start. |
| 108 | std::transform(first: Fixups.begin(), last: Fixups.end(), result: S.begin() + VarFixupStart, |
| 109 | unary_op: [Fixed = getFixedSize()](MCFixup F) { |
| 110 | F.setOffset(Fixed + F.getOffset()); |
| 111 | return F; |
| 112 | }); |
| 113 | } |
| 114 | |