1 //===- lib/MC/MCSection.cpp - Machine Code Section Representation ---------===//
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
7 //===----------------------------------------------------------------------===//
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/MCFragment.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
23 MCSection::MCSection(SectionVariant V, StringRef Name, SectionKind K,
25 : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
26 IsRegistered(false), DummyFragment(this), Name(Name), Variant(V),
29 MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
31 End = Ctx.createTempSymbol("sec_end", true);
35 bool MCSection::hasEnded() const { return End && End->isInSection(); }
37 MCSection::~MCSection() = default;
39 void MCSection::setBundleLockState(BundleLockStateType NewState) {
40 if (NewState == NotBundleLocked) {
41 if (BundleLockNestingDepth == 0) {
42 report_fatal_error("Mismatched bundle_lock/unlock directives");
44 if (--BundleLockNestingDepth == 0) {
45 BundleLockState = NotBundleLocked;
50 // If any of the directives is an align_to_end directive, the whole nested
51 // group is align_to_end. So don't downgrade from align_to_end to just locked.
52 if (BundleLockState != BundleLockedAlignToEnd) {
53 BundleLockState = NewState;
55 ++BundleLockNestingDepth;
59 MCSection::getSubsectionInsertionPoint(unsigned Subsection) {
60 if (Subsection == 0 && SubsectionFragmentMap.empty())
63 SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI =
64 std::lower_bound(SubsectionFragmentMap.begin(),
65 SubsectionFragmentMap.end(),
66 std::make_pair(Subsection, (MCFragment *)nullptr));
67 bool ExactMatch = false;
68 if (MI != SubsectionFragmentMap.end()) {
69 ExactMatch = MI->first == Subsection;
74 if (MI == SubsectionFragmentMap.end())
77 IP = MI->second->getIterator();
78 if (!ExactMatch && Subsection != 0) {
79 // The GNU as documentation claims that subsections have an alignment of 4,
80 // although this appears not to be the case.
81 MCFragment *F = new MCDataFragment();
82 SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
83 getFragmentList().insert(IP, F);
90 StringRef MCSection::getVirtualSectionKind() const { return "virtual"; }
92 void MCSection::addPendingLabel(MCSymbol *label, unsigned Subsection) {
93 PendingLabels.push_back(PendingLabel(label, Subsection));
96 void MCSection::flushPendingLabels(MCFragment *F, uint64_t FOffset,
97 unsigned Subsection) {
98 if (PendingLabels.empty())
101 // Set the fragment and fragment offset for all pending symbols in the
102 // specified Subsection, and remove those symbols from the pending list.
103 for (auto It = PendingLabels.begin(); It != PendingLabels.end(); ++It) {
104 PendingLabel& Label = *It;
105 if (Label.Subsection == Subsection) {
106 Label.Sym->setFragment(F);
107 Label.Sym->setOffset(FOffset);
108 PendingLabels.erase(It--);
113 void MCSection::flushPendingLabels() {
114 // Make sure all remaining pending labels point to data fragments, by
115 // creating new empty data fragments for each Subsection with labels pending.
116 while (!PendingLabels.empty()) {
117 PendingLabel& Label = PendingLabels[0];
118 iterator CurInsertionPoint =
119 this->getSubsectionInsertionPoint(Label.Subsection);
120 MCFragment *F = new MCDataFragment();
121 getFragmentList().insert(CurInsertionPoint, F);
123 flushPendingLabels(F, 0, Label.Subsection);
127 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
128 LLVM_DUMP_METHOD void MCSection::dump() const {
129 raw_ostream &OS = errs();
132 OS << " Fragments:[\n ";
133 for (auto it = begin(), ie = end(); it != ie; ++it) {