LLVM 20.0.0git
AArch64TargetStreamer.cpp
Go to the documentation of this file.
1//===- AArch64TargetStreamer.cpp - AArch64TargetStreamer class ------------===//
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// This file implements the AArch64TargetStreamer class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "AArch64MCAsmInfo.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCSection.h"
22
23using namespace llvm;
24
26 "aarch64-mark-bti-property", cl::Hidden,
27 cl::desc("Add .note.gnu.property with BTI to assembly files"),
28 cl::init(false));
29
30//
31// AArch64TargetStreamer Implemenation
32//
34 : MCTargetStreamer(S), ConstantPools(new AssemblerConstantPools()) {}
35
37
39 uint16_t Discriminator,
41 bool HasAddressDiversity) {
42 Streamer.emitValueImpl(AArch64AuthMCExpr::create(Expr, Discriminator, Key,
43 HasAddressDiversity,
45 8);
46}
47
48// The constant pool handling is shared by all AArch64TargetStreamer
49// implementations.
51 unsigned Size,
52 SMLoc Loc) {
53 return ConstantPools->addEntry(Streamer, Expr, Size, Loc);
54}
55
57 ConstantPools->emitForCurrentSection(Streamer);
58}
59
61 ConstantPools->emitAll(Streamer);
62}
63
64// finish() - write out any non-empty assembler constant pools and
65// write out note.gnu.properties if need.
69}
70
72 uint64_t PAuthABIPlatform,
73 uint64_t PAuthABIVersion) {
74 assert((PAuthABIPlatform == uint64_t(-1)) ==
75 (PAuthABIVersion == uint64_t(-1)));
76 uint64_t DescSz = 0;
77 if (Flags != 0)
78 DescSz += 4 * 4;
79 if (PAuthABIPlatform != uint64_t(-1))
80 DescSz += 4 + 4 + 8 * 2;
81 if (DescSz == 0)
82 return;
83
84 MCStreamer &OutStreamer = getStreamer();
85 MCContext &Context = OutStreamer.getContext();
86 // Emit a .note.gnu.property section with the flags.
87 MCSectionELF *Nt = Context.getELFSection(".note.gnu.property", ELF::SHT_NOTE,
89 if (Nt->isRegistered()) {
90 SMLoc Loc;
91 Context.reportWarning(
92 Loc,
93 "The .note.gnu.property is not emitted because it is already present.");
94 return;
95 }
96 MCSection *Cur = OutStreamer.getCurrentSectionOnly();
97 OutStreamer.switchSection(Nt);
98
99 // Emit the note header.
100 OutStreamer.emitValueToAlignment(Align(8));
101 OutStreamer.emitIntValue(4, 4); // data size for "GNU\0"
102 OutStreamer.emitIntValue(DescSz, 4); // Elf_Prop array size
104 OutStreamer.emitBytes(StringRef("GNU", 4)); // note name
105
106 // Emit the PAC/BTI properties.
107 if (Flags != 0) {
109 OutStreamer.emitIntValue(4, 4); // data size
110 OutStreamer.emitIntValue(Flags, 4); // data
111 OutStreamer.emitIntValue(0, 4); // pad
112 }
113
114 // Emit the PAuth ABI compatibility info
115 if (PAuthABIPlatform != uint64_t(-1)) {
117 OutStreamer.emitIntValue(8 * 2, 4); // data size
118 OutStreamer.emitIntValue(PAuthABIPlatform, 8);
119 OutStreamer.emitIntValue(PAuthABIVersion, 8);
120 }
121
122 OutStreamer.endSection(Nt);
123 OutStreamer.switchSection(Cur);
124}
125
127 char Buffer[4];
128
129 // We can't just use EmitIntValue here, as that will swap the
130 // endianness on big-endian systems (instructions are always
131 // little-endian).
132 for (char &C : Buffer) {
133 C = uint8_t(Inst);
134 Inst >>= 8;
135 }
136
137 getStreamer().emitBytes(StringRef(Buffer, 4));
138}
139
142 const MCSubtargetInfo &STI) {
143 const Triple &TT = STI.getTargetTriple();
144 if (TT.isOSBinFormatELF())
145 return new AArch64TargetELFStreamer(S);
146 if (TT.isOSBinFormatCOFF())
147 return new AArch64TargetWinCOFFStreamer(S);
148 return nullptr;
149}
150
152 return new AArch64TargetStreamer(S);
153}
154
158
159 // If exists, return.
161 if (VendorName == SubSection.VendorName) {
162 activateAtributesSubsection(VendorName);
163 return;
164 }
165 }
166 // else, add the subsection
168 AttSubSection.VendorName = VendorName;
169 AttSubSection.IsOptional = IsOptional;
170 AttSubSection.ParameterType = ParameterType;
171 AttributeSubSections.push_back(AttSubSection);
172 activateAtributesSubsection(VendorName);
173}
174
175std::unique_ptr<MCELFStreamer::AttributeSubSection>
178 if (SubSection.IsActive) {
179 return std::make_unique<MCELFStreamer::AttributeSubSection>(SubSection);
180 }
181 }
182 return nullptr;
183}
184
185std::unique_ptr<MCELFStreamer::AttributeSubSection>
188 if (Name == SubSection.VendorName) {
189 return std::make_unique<MCELFStreamer::AttributeSubSection>(SubSection);
190 }
191 }
192 return nullptr;
193}
194
196 unsigned Value, std::string String,
197 bool Override) {
198
199 if (unsigned(-1) == Value && "" == String) {
200 assert(0 && "Arguments error");
201 return;
202 }
203 if (AttributeSubSections.size() == 0) {
204 assert(0 &&
205 "Can not add AArch64 build attribute: no AArch64 subsection exists");
206 return;
207 }
208
210 if (VendorName == SubSection.VendorName) {
211 if (!SubSection.IsActive) {
212 assert(0 &&
213 "Can not add AArch64 build attribute: subsection is not active");
214 return;
215 }
216 for (MCELFStreamer::AttributeItem &Item : SubSection.Content) {
217 if (Item.Tag == Tag) {
218 if (!Override) {
219 if ((unsigned(-1) != Value && Item.IntValue != Value) ||
220 ("" != String && Item.StringValue != String)) {
221 assert(0 &&
222 "Can not add AArch64 build attribute: An attribute with "
223 "the same tag and a different value already exists");
224 return;
225 } else {
226 // Case Item.IntValue == Value, no need to emit twice
227 assert(0 &&
228 "AArch64 build attribute: An attribute with the same tag "
229 "and a same value already exists");
230 return;
231 }
232 }
233 }
234 }
235 if (unsigned(-1) != Value)
236 SubSection.Content.push_back(MCELFStreamer::AttributeItem(
238 if ("" != String)
239 SubSection.Content.push_back(MCELFStreamer::AttributeItem(
241 String));
242 return;
243 }
244 }
245 assert(0 && "Can not add AArch64 build attribute: required subsection does "
246 "not exist");
247}
248
251 if (VendorName == SubSection.VendorName) {
252 SubSection.IsActive = true;
253 } else {
254 SubSection.IsActive = false;
255 }
256 }
257}
static cl::opt< bool > MarkBTIProperty("aarch64-mark-bti-property", cl::Hidden, cl::desc("Add .note.gnu.property with BTI to assembly files"), cl::init(false))
std::string Name
uint64_t Size
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const AArch64AuthMCExpr * create(const MCExpr *Expr, uint16_t Discriminator, AArch64PACKey::ID Key, bool HasAddressDiversity, MCContext &Ctx)
void activateAtributesSubsection(StringRef VendorName)
virtual void emitAttribute(StringRef VendorName, unsigned Tag, unsigned Value, std::string String, bool Override)
const MCExpr * addConstantPoolEntry(const MCExpr *, unsigned Size, SMLoc Loc)
Callback used to implement the ldr= pseudo.
SmallVector< MCELFStreamer::AttributeSubSection, 64 > AttributeSubSections
std::unique_ptr< MCELFStreamer::AttributeSubSection > getActiveAtributesSubsection()
void emitNoteSection(unsigned Flags, uint64_t PAuthABIPlatform=-1, uint64_t PAuthABIVersion=-1)
Callback used to implement the .note.gnu.property section.
void emitAuthValue(const MCExpr *Expr, uint16_t Discriminator, AArch64PACKey::ID Key, bool HasAddressDiversity)
Callback used to emit AUTH expressions (e.g.
virtual void emitAtributesSubsection(StringRef VendorName, AArch64BuildAttributes::SubsectionOptional IsOptional, AArch64BuildAttributes::SubsectionType ParameterType)
Build attributes implementation.
std::unique_ptr< MCELFStreamer::AttributeSubSection > getAtributesSubsectionByName(StringRef Name)
void emitCurrentConstantPool()
Callback used to implement the .ltorg directive.
virtual void emitInst(uint32_t Inst)
Callback used to implement the .inst directive.
Context object for machine code objects.
Definition: MCContext.h:83
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:551
void reportWarning(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1079
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
bool isRegistered() const
Definition: MCSection.h:175
Streaming machine code generation interface.
Definition: MCStreamer.h:213
MCContext & getContext() const
Definition: MCStreamer.h:300
virtual void emitValueToAlignment(Align Alignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
virtual void emitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers.
Definition: MCStreamer.cpp:133
MCSymbol * endSection(MCSection *Section)
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:400
virtual void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Emit the expression Value into the output as a native integer of the given Size bytes.
virtual void emitBytes(StringRef Data)
Emit the bytes in Data into the output.
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
Target specific streamer interface.
Definition: MCStreamer.h:94
MCStreamer & getStreamer()
Definition: MCStreamer.h:102
MCStreamer & Streamer
Definition: MCStreamer.h:96
Represents a location in source code.
Definition: SMLoc.h:23
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVM Value Representation.
Definition: Value.h:74
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SHT_NOTE
Definition: ELF.h:1104
@ GNU_PROPERTY_AARCH64_FEATURE_1_BTI
Definition: ELF.h:1803
@ SHF_ALLOC
Definition: ELF.h:1198
@ NT_GNU_PROPERTY_TYPE_0
Definition: ELF.h:1755
@ GNU_PROPERTY_AARCH64_FEATURE_PAUTH
Definition: ELF.h:1789
@ GNU_PROPERTY_AARCH64_FEATURE_1_AND
Definition: ELF.h:1788
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCTargetStreamer * createAArch64NullTargetStreamer(MCStreamer &S)
MCTargetStreamer * createAArch64ObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
ELF object attributes section emission support.
Definition: MCELFStreamer.h:95
ELF object attributes subsection support.