Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/llvm/lib/MC/MCSection.cpp
Line
Count
Source (jump to first uncovered line)
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/STLExtras.h"
11
#include "llvm/ADT/SmallVector.h"
12
#include "llvm/Config/llvm-config.h"
13
#include "llvm/MC/MCContext.h"
14
#include "llvm/MC/MCFragment.h"
15
#include "llvm/MC/MCSymbol.h"
16
#include "llvm/Support/Compiler.h"
17
#include "llvm/Support/ErrorHandling.h"
18
#include "llvm/Support/raw_ostream.h"
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
2.15M
      Kind(K) {}
28
29
0
MCSymbol *MCSection::getEndSymbol(MCContext &Ctx) {
30
0
  if (!End)
31
0
    End = Ctx.createTempSymbol("sec_end");
32
0
  return End;
33
0
}
34
35
321k
bool MCSection::hasEnded() const { return End && End->isInSection(); }
36
37
2.15M
MCSection::~MCSection() = default;
38
39
0
void MCSection::setBundleLockState(BundleLockStateType NewState) {
40
0
  if (NewState == NotBundleLocked) {
41
0
    if (BundleLockNestingDepth == 0) {
42
0
      report_fatal_error("Mismatched bundle_lock/unlock directives");
43
0
    }
44
0
    if (--BundleLockNestingDepth == 0) {
45
0
      BundleLockState = NotBundleLocked;
46
0
    }
47
0
    return;
48
0
  }
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
0
  if (BundleLockState != BundleLockedAlignToEnd) {
53
0
    BundleLockState = NewState;
54
0
  }
55
0
  ++BundleLockNestingDepth;
56
0
}
57
58
MCSection::iterator
59
0
MCSection::getSubsectionInsertionPoint(unsigned Subsection) {
60
0
  if (Subsection == 0 && SubsectionFragmentMap.empty())
61
0
    return end();
62
63
0
  SmallVectorImpl<std::pair<unsigned, MCFragment *>>::iterator MI = lower_bound(
64
0
      SubsectionFragmentMap, std::make_pair(Subsection, (MCFragment *)nullptr));
65
0
  bool ExactMatch = false;
66
0
  if (MI != SubsectionFragmentMap.end()) {
67
0
    ExactMatch = MI->first == Subsection;
68
0
    if (ExactMatch)
69
0
      ++MI;
70
0
  }
71
0
  iterator IP;
72
0
  if (MI == SubsectionFragmentMap.end())
73
0
    IP = end();
74
0
  else
75
0
    IP = MI->second->getIterator();
76
0
  if (!ExactMatch && Subsection != 0) {
77
    // The GNU as documentation claims that subsections have an alignment of 4,
78
    // although this appears not to be the case.
79
0
    MCFragment *F = new MCDataFragment();
80
0
    SubsectionFragmentMap.insert(MI, std::make_pair(Subsection, F));
81
0
    getFragmentList().insert(IP, F);
82
0
    F->setParent(this);
83
0
    F->setSubsectionNumber(Subsection);
84
0
  }
85
86
0
  return IP;
87
0
}
88
89
0
StringRef MCSection::getVirtualSectionKind() const { return "virtual"; }
90
91
0
void MCSection::addPendingLabel(MCSymbol *label, unsigned Subsection) {
92
0
  PendingLabels.push_back(PendingLabel(label, Subsection));
93
0
}
94
95
void MCSection::flushPendingLabels(MCFragment *F, uint64_t FOffset,
96
0
           unsigned Subsection) {
97
  // Set the fragment and fragment offset for all pending symbols in the
98
  // specified Subsection, and remove those symbols from the pending list.
99
0
  for (auto It = PendingLabels.begin(); It != PendingLabels.end(); ++It) {
100
0
    PendingLabel& Label = *It;
101
0
    if (Label.Subsection == Subsection) {
102
0
      Label.Sym->setFragment(F);
103
0
      Label.Sym->setOffset(FOffset);
104
0
      PendingLabels.erase(It--);
105
0
    }
106
0
  }
107
0
}
108
109
0
void MCSection::flushPendingLabels() {
110
  // Make sure all remaining pending labels point to data fragments, by
111
  // creating new empty data fragments for each Subsection with labels pending.
112
0
  while (!PendingLabels.empty()) {
113
0
    PendingLabel& Label = PendingLabels[0];
114
0
    iterator CurInsertionPoint =
115
0
      this->getSubsectionInsertionPoint(Label.Subsection);
116
0
    const MCSymbol *Atom = nullptr;
117
0
    if (CurInsertionPoint != begin())
118
0
      Atom = std::prev(CurInsertionPoint)->getAtom();
119
0
    MCFragment *F = new MCDataFragment();
120
0
    getFragmentList().insert(CurInsertionPoint, F);
121
0
    F->setParent(this);
122
0
    F->setAtom(Atom);
123
0
    flushPendingLabels(F, 0, Label.Subsection);
124
0
  }
125
0
}
126
127
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
128
0
LLVM_DUMP_METHOD void MCSection::dump() const {
129
0
  raw_ostream &OS = errs();
130
131
0
  OS << "<MCSection";
132
0
  OS << " Fragments:[\n      ";
133
0
  for (auto it = begin(), ie = end(); it != ie; ++it) {
134
0
    if (it != begin())
135
0
      OS << ",\n      ";
136
0
    it->dump();
137
0
  }
138
0
  OS << "]>";
139
0
}
140
#endif