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
MCSection(SectionVariant V,StringRef Name,bool IsText,bool IsVirtual,MCSymbol * Begin)21 MCSection::MCSection(SectionVariant V, StringRef Name, bool IsText,
22 bool IsVirtual, MCSymbol *Begin)
23 : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
24 IsRegistered(false), IsText(IsText), IsVirtual(IsVirtual),
25 LinkerRelaxable(false), Name(Name), Variant(V) {
26 // The initial subsection number is 0. Create a fragment list.
27 CurFragList = &Subsections.emplace_back(0u, FragList{}).second;
28 }
29
getEndSymbol(MCContext & Ctx)30 MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
31 if (!End)
32 End = Ctx.createTempSymbol("sec_end");
33 return End;
34 }
35
hasEnded() const36 bool MCSection::hasEnded() const { return End && End->isInSection(); }
37
setBundleLockState(BundleLockStateType NewState)38 void MCSection::setBundleLockState(BundleLockStateType NewState) {
39 if (NewState == NotBundleLocked) {
40 if (BundleLockNestingDepth == 0) {
41 report_fatal_error("Mismatched bundle_lock/unlock directives");
42 }
43 if (--BundleLockNestingDepth == 0) {
44 BundleLockState = NotBundleLocked;
45 }
46 return;
47 }
48
49 // If any of the directives is an align_to_end directive, the whole nested
50 // group is align_to_end. So don't downgrade from align_to_end to just locked.
51 if (BundleLockState != BundleLockedAlignToEnd) {
52 BundleLockState = NewState;
53 }
54 ++BundleLockNestingDepth;
55 }
56
getVirtualSectionKind() const57 StringRef MCSection::getVirtualSectionKind() const { return "virtual"; }
58
59 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump(DenseMap<const MCFragment *,SmallVector<const MCSymbol *,0>> * FragToSyms) const60 LLVM_DUMP_METHOD void MCSection::dump(
61 DenseMap<const MCFragment *, SmallVector<const MCSymbol *, 0>> *FragToSyms)
62 const {
63 raw_ostream &OS = errs();
64
65 OS << "MCSection Name:" << getName();
66 if (isLinkerRelaxable())
67 OS << " LinkerRelaxable";
68 for (auto &F : *this) {
69 OS << '\n';
70 F.dump();
71 if (!FragToSyms)
72 continue;
73 auto It = FragToSyms->find(&F);
74 if (It == FragToSyms->end())
75 continue;
76 for (auto *Sym : It->second) {
77 OS << "\n Symbol @" << Sym->getOffset() << ' ' << Sym->getName();
78 if (Sym->isTemporary())
79 OS << " Temporary";
80 }
81 }
82 }
83 #endif
84
setContents(ArrayRef<char> Contents)85 void MCEncodedFragment::setContents(ArrayRef<char> Contents) {
86 auto &S = getParent()->ContentStorage;
87 if (Contents.size() > ContentSize) {
88 ContentStart = S.size();
89 S.resize_for_overwrite(S.size() + Contents.size());
90 }
91 ContentSize = Contents.size();
92 llvm::copy(Contents, S.begin() + ContentStart);
93 }
94
addFixup(MCFixup Fixup)95 void MCEncodedFragment::addFixup(MCFixup Fixup) { appendFixups({Fixup}); }
96
appendFixups(ArrayRef<MCFixup> Fixups)97 void MCEncodedFragment::appendFixups(ArrayRef<MCFixup> Fixups) {
98 auto &S = getParent()->FixupStorage;
99 if (LLVM_UNLIKELY(FixupEnd != S.size())) {
100 // Move the elements to the end. Reserve space to avoid invalidating
101 // S.begin()+I for `append`.
102 auto Size = FixupEnd - FixupStart;
103 auto I = std::exchange(FixupStart, S.size());
104 S.reserve(S.size() + Size);
105 S.append(S.begin() + I, S.begin() + I + Size);
106 }
107 S.append(Fixups.begin(), Fixups.end());
108 FixupEnd = S.size();
109 }
110
setFixups(ArrayRef<MCFixup> Fixups)111 void MCEncodedFragment::setFixups(ArrayRef<MCFixup> Fixups) {
112 auto &S = getParent()->FixupStorage;
113 if (FixupStart + Fixups.size() > FixupEnd) {
114 FixupStart = S.size();
115 S.resize_for_overwrite(S.size() + Fixups.size());
116 }
117 FixupEnd = FixupStart + Fixups.size();
118 llvm::copy(Fixups, S.begin() + FixupStart);
119 }
120