LLVM 22.0.0git
MCSection.cpp
Go to the documentation of this file.
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"
11#include "llvm/Config/llvm-config.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCSymbol.h"
17#include <utility>
18
19using namespace llvm;
20
21MCSection::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
28 if (!End)
29 End = Ctx.createTempSymbol("sec_end");
30 return End;
31}
32
33bool MCSection::hasEnded() const { return End && End->isInSection(); }
34
35#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
38 const {
39 raw_ostream &OS = errs();
40
41 OS << "MCSection Name:" << getName();
43 OS << " FirstLinkerRelaxable:" << firstLinkerRelaxable();
44 for (auto &F : *this) {
45 OS << '\n';
46 F.dump();
47 if (!FragToSyms)
48 continue;
49 auto It = FragToSyms->find(&F);
50 if (It == FragToSyms->end())
51 continue;
52 for (auto *Sym : It->second) {
53 OS << "\n Symbol @" << Sym->getOffset() << ' ' << Sym->getName();
54 if (Sym->isTemporary())
55 OS << " Temporary";
56 }
57 }
58}
59#endif
60
62 auto &S = getParent()->ContentStorage;
63 if (VarContentStart + Contents.size() > VarContentEnd) {
64 VarContentStart = S.size();
65 S.resize_for_overwrite(S.size() + Contents.size());
66 }
67 VarContentEnd = VarContentStart + Contents.size();
68 llvm::copy(Contents, S.begin() + VarContentStart);
69}
70
72
74 auto &S = getParent()->FixupStorage;
75 if (LLVM_UNLIKELY(FixupEnd != S.size())) {
76 // Move the elements to the end. Reserve space to avoid invalidating
77 // S.begin()+I for `append`.
78 auto Size = FixupEnd - FixupStart;
79 auto I = std::exchange(FixupStart, S.size());
80 S.reserve(S.size() + Size);
81 S.append(S.begin() + I, S.begin() + I + Size);
82 }
83 S.append(Fixups.begin(), Fixups.end());
84 FixupEnd = S.size();
85}
86
88 assert(Fixups.size() < 256 &&
89 "variable-size tail cannot have more than 256 fixups");
90 auto &S = getParent()->FixupStorage;
91 if (Fixups.size() > VarFixupSize) {
92 VarFixupStart = S.size();
93 S.resize_for_overwrite(S.size() + Fixups.size());
94 }
95 VarFixupSize = Fixups.size();
96 // Source fixup offsets are relative to the variable part's start. Add the
97 // fixed part size to make them relative to the fixed part's start.
98 std::transform(Fixups.begin(), Fixups.end(), S.begin() + VarFixupStart,
100 F.setOffset(Fixed + F.getOffset());
101 return F;
102 });
103}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:336
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
PowerPC TLS Dynamic Call Fixup
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
iterator begin() const
Definition ArrayRef.h:135
Context object for machine code objects.
Definition MCContext.h:83
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:61
LLVM_ABI void appendFixups(ArrayRef< MCFixup > Fixups)
Definition MCSection.cpp:73
MCSection * getParent() const
Definition MCSection.h:158
LLVM_ABI void setVarFixups(ArrayRef< MCFixup > Fixups)
Definition MCSection.cpp:87
LLVM_ABI void addFixup(MCFixup Fixup)
Definition MCSection.cpp:71
size_t getFixedSize() const
Definition MCSection.h:200
LLVM_ABI void setVarContents(ArrayRef< char > Contents)
Definition MCSection.cpp:61
MCSymbol * getEndSymbol(MCContext &Ctx)
Definition MCSection.cpp:27
bool isLinkerRelaxable() const
Definition MCSection.h:598
void dump(DenseMap< const MCFragment *, SmallVector< const MCSymbol *, 0 > > *FragToSyms=nullptr) const
Definition MCSection.cpp:36
StringRef Name
Definition MCSection.h:557
StringRef getName() const
Definition MCSection.h:565
friend class MCFragment
Definition MCSection.h:500
bool hasEnded() const
Definition MCSection.cpp:33
unsigned firstLinkerRelaxable() const
Definition MCSection.h:597
MCSection(StringRef Name, bool IsText, bool IsBss, MCSymbol *Begin)
Definition MCSection.cpp:21
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1817