LLVM 23.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
34 if (Size < getAlign().value())
35 return getAlign();
36
37 if (Size < getPreferredAlignment().value())
38 return Align(NextPowerOf2(Size - 1));
39
40 return getPreferredAlignment();
41}
42
43bool MCSection::hasEnded() const { return End && End->isInSection(); }
44
45#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
48 const {
49 raw_ostream &OS = errs();
50
51 OS << "MCSection Name:" << getName();
53 OS << " FirstLinkerRelaxable:" << firstLinkerRelaxable();
54 for (auto &F : *this) {
55 OS << '\n';
56 F.dump();
57 if (!FragToSyms)
58 continue;
59 auto It = FragToSyms->find(&F);
60 if (It == FragToSyms->end())
61 continue;
62 for (auto *Sym : It->second) {
63 OS << "\n Symbol @" << Sym->getOffset() << ' ' << Sym->getName();
64 if (Sym->isTemporary())
65 OS << " Temporary";
66 }
67 }
68}
69#endif
70
72 auto &S = getParent()->ContentStorage;
73 if (VarContentStart + Contents.size() > VarContentEnd) {
74 VarContentStart = S.size();
75 S.resize_for_overwrite(S.size() + Contents.size());
76 }
77 VarContentEnd = VarContentStart + Contents.size();
78 llvm::copy(Contents, S.begin() + VarContentStart);
79}
80
82
84 auto &S = getParent()->FixupStorage;
85 if (LLVM_UNLIKELY(FixupEnd != S.size())) {
86 // Move the elements to the end. Reserve space to avoid invalidating
87 // S.begin()+I for `append`.
88 auto Size = FixupEnd - FixupStart;
89 auto I = std::exchange(FixupStart, S.size());
90 S.reserve(S.size() + Size);
91 S.append(S.begin() + I, S.begin() + I + Size);
92 }
93 S.append(Fixups.begin(), Fixups.end());
94 FixupEnd = S.size();
95}
96
98 assert(Fixups.size() < 256 &&
99 "variable-size tail cannot have more than 256 fixups");
100 auto &S = getParent()->FixupStorage;
101 if (Fixups.size() > VarFixupSize) {
102 VarFixupStart = S.size();
103 S.resize_for_overwrite(S.size() + Fixups.size());
104 }
105 VarFixupSize = Fixups.size();
106 // Source fixup offsets are relative to the variable part's start. Add the
107 // fixed part size to make them relative to the fixed part's start.
108 std::transform(Fixups.begin(), Fixups.end(), S.begin() + VarFixupStart,
109 [Fixed = getFixedSize()](MCFixup F) {
110 F.setOffset(Fixed + F.getOffset());
111 return F;
112 });
113}
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:661
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
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:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
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:83
MCSection * getParent() const
Definition MCSection.h:167
LLVM_ABI void setVarFixups(ArrayRef< MCFixup > Fixups)
Definition MCSection.cpp:97
LLVM_ABI void addFixup(MCFixup Fixup)
Definition MCSection.cpp:81
size_t getFixedSize() const
Definition MCSection.h:209
LLVM_ABI void setVarContents(ArrayRef< char > Contents)
Definition MCSection.cpp:71
Align getPreferredAlignment() const
Definition MCSection.h:610
Align getAlignmentForObjectFile(uint64_t Size) const
Definition MCSection.cpp:33
MCSymbol * getEndSymbol(MCContext &Ctx)
Definition MCSection.cpp:27
Align getAlign() const
Definition MCSection.h:601
bool isLinkerRelaxable() const
Definition MCSection.h:633
void dump(DenseMap< const MCFragment *, SmallVector< const MCSymbol *, 0 > > *FragToSyms=nullptr) const
Definition MCSection.cpp:46
StringRef Name
Definition MCSection.h:579
StringRef getName() const
Definition MCSection.h:587
friend class MCFragment
Definition MCSection.h:521
bool hasEnded() const
Definition MCSection.cpp:43
unsigned firstLinkerRelaxable() const
Definition MCSection.h:632
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.
Definition Types.h:26
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:1883
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39