xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCSection.cpp (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
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/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"
18 #include <algorithm>
19 #include <utility>
20 
21 using namespace llvm;
22 
23 MCSection::MCSection(SectionVariant V, StringRef Name, SectionKind K,
24                      MCSymbol *Begin)
25     : Begin(Begin), BundleGroupBeforeFirstInst(false), HasInstructions(false),
26       IsRegistered(false), DummyFragment(this), Name(Name), Variant(V),
27       Kind(K) {}
28 
29 MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
30   if (!End)
31     End = Ctx.createTempSymbol("sec_end");
32   return End;
33 }
34 
35 bool MCSection::hasEnded() const { return End && End->isInSection(); }
36 
37 MCSection::~MCSection() = default;
38 
39 void MCSection::setBundleLockState(BundleLockStateType NewState) {
40   if (NewState == NotBundleLocked) {
41     if (BundleLockNestingDepth == 0) {
42       report_fatal_error("Mismatched bundle_lock/unlock directives");
43     }
44     if (--BundleLockNestingDepth == 0) {
45       BundleLockState = NotBundleLocked;
46     }
47     return;
48   }
49 
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;
54   }
55   ++BundleLockNestingDepth;
56 }
57 
58 MCSection::iterator
59 MCSection::getSubsectionInsertionPoint(unsigned Subsection) {
60   if (Subsection == 0 && SubsectionFragmentMap.empty())
61     return end();
62 
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;
70     if (ExactMatch)
71       ++MI;
72   }
73   iterator IP;
74   if (MI == SubsectionFragmentMap.end())
75     IP = end();
76   else
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);
84     F->setParent(this);
85     F->setSubsectionNumber(Subsection);
86   }
87 
88   return IP;
89 }
90 
91 StringRef MCSection::getVirtualSectionKind() const { return "virtual"; }
92 
93 void MCSection::addPendingLabel(MCSymbol *label, unsigned Subsection) {
94   PendingLabels.push_back(PendingLabel(label, Subsection));
95 }
96 
97 void MCSection::flushPendingLabels(MCFragment *F, uint64_t FOffset,
98 				   unsigned Subsection) {
99   if (PendingLabels.empty())
100     return;
101 
102   // Set the fragment and fragment offset for all pending symbols in the
103   // specified Subsection, and remove those symbols from the pending list.
104   for (auto It = PendingLabels.begin(); It != PendingLabels.end(); ++It) {
105     PendingLabel& Label = *It;
106     if (Label.Subsection == Subsection) {
107       Label.Sym->setFragment(F);
108       Label.Sym->setOffset(FOffset);
109       PendingLabels.erase(It--);
110     }
111   }
112 }
113 
114 void MCSection::flushPendingLabels() {
115   // Make sure all remaining pending labels point to data fragments, by
116   // creating new empty data fragments for each Subsection with labels pending.
117   while (!PendingLabels.empty()) {
118     PendingLabel& Label = PendingLabels[0];
119     iterator CurInsertionPoint =
120       this->getSubsectionInsertionPoint(Label.Subsection);
121     MCFragment *F = new MCDataFragment();
122     getFragmentList().insert(CurInsertionPoint, F);
123     F->setParent(this);
124     flushPendingLabels(F, 0, Label.Subsection);
125   }
126 }
127 
128 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
129 LLVM_DUMP_METHOD void MCSection::dump() const {
130   raw_ostream &OS = errs();
131 
132   OS << "<MCSection";
133   OS << " Fragments:[\n      ";
134   for (auto it = begin(), ie = end(); it != ie; ++it) {
135     if (it != begin())
136       OS << ",\n      ";
137     it->dump();
138   }
139   OS << "]>";
140 }
141 #endif
142