LLVM 22.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"
19#include "llvm/MC/MCSection.h"
23
24using namespace llvm;
25
27 "aarch64-mark-bti-property", cl::Hidden,
28 cl::desc("Add .note.gnu.property with BTI to assembly files"),
29 cl::init(false));
30
31//
32// AArch64TargetStreamer Implementation
33//
35 : MCTargetStreamer(S), ConstantPools(new AssemblerConstantPools()) {}
36
38
40 uint16_t Discriminator,
42 bool HasAddressDiversity) {
43 Streamer.emitValueImpl(AArch64AuthMCExpr::create(Expr, Discriminator, Key,
44 HasAddressDiversity,
46 8);
47}
48
49// The constant pool handling is shared by all AArch64TargetStreamer
50// implementations.
52 unsigned Size,
53 SMLoc Loc) {
54 return ConstantPools->addEntry(Streamer, Expr, Size, Loc);
55}
56
58 ConstantPools->emitForCurrentSection(Streamer);
59}
60
62 ConstantPools->emitAll(Streamer);
63}
64
65// finish() - write out any non-empty assembler constant pools and
66// write out note.gnu.properties if need.
70}
71
73 uint64_t PAuthABIPlatform,
74 uint64_t PAuthABIVersion) {
75 assert((PAuthABIPlatform == uint64_t(-1)) ==
76 (PAuthABIVersion == uint64_t(-1)));
77 uint64_t DescSz = 0;
78 if (Flags != 0)
79 DescSz += 4 * 4;
80 if (PAuthABIPlatform != uint64_t(-1))
81 DescSz += 4 + 4 + 8 * 2;
82 if (DescSz == 0)
83 return;
84
85 MCStreamer &OutStreamer = getStreamer();
86 MCContext &Context = OutStreamer.getContext();
87 // Emit a .note.gnu.property section with the flags.
88 MCSectionELF *Nt = Context.getELFSection(".note.gnu.property", ELF::SHT_NOTE,
90 if (Nt->isRegistered()) {
91 SMLoc Loc;
92 Context.reportWarning(
93 Loc,
94 "The .note.gnu.property is not emitted because it is already present.");
95 return;
96 }
97 MCSection *Cur = OutStreamer.getCurrentSectionOnly();
98 OutStreamer.switchSection(Nt);
99
100 // Emit the note header.
101 OutStreamer.emitValueToAlignment(Align(8));
102 OutStreamer.emitIntValue(4, 4); // data size for "GNU\0"
103 OutStreamer.emitIntValue(DescSz, 4); // Elf_Prop array size
105 OutStreamer.emitBytes(StringRef("GNU", 4)); // note name
106
107 // Emit the PAC/BTI properties.
108 if (Flags != 0) {
110 OutStreamer.emitIntValue(4, 4); // data size
111 OutStreamer.emitIntValue(Flags, 4); // data
112 OutStreamer.emitIntValue(0, 4); // pad
113 }
114
115 // Emit the PAuth ABI compatibility info
116 if (PAuthABIPlatform != uint64_t(-1)) {
118 OutStreamer.emitIntValue(8 * 2, 4); // data size
119 OutStreamer.emitIntValue(PAuthABIPlatform, 8);
120 OutStreamer.emitIntValue(PAuthABIVersion, 8);
121 }
122
123 OutStreamer.endSection(Nt);
124 OutStreamer.switchSection(Cur);
125}
126
128 char Buffer[4];
129
130 // We can't just use EmitIntValue here, as that will swap the
131 // endianness on big-endian systems (instructions are always
132 // little-endian).
133 for (char &C : Buffer) {
134 C = uint8_t(Inst);
135 Inst >>= 8;
136 }
137
138 getStreamer().emitBytes(StringRef(Buffer, 4));
139}
140
143 const MCSubtargetInfo &STI) {
144 const Triple &TT = STI.getTargetTriple();
145 if (TT.isOSBinFormatELF())
146 return new AArch64TargetELFStreamer(S);
147 if (TT.isOSBinFormatCOFF())
148 return new AArch64TargetWinCOFFStreamer(S);
149 return nullptr;
150}
151
153 return new AArch64TargetStreamer(S);
154}
155
159
160 // If exists, return.
162 if (VendorName == SubSection.VendorName) {
164 return;
165 }
166 }
167 // else, add the subsection
169 AttSubSection.VendorName = VendorName;
170 AttSubSection.IsOptional = IsOptional;
171 AttSubSection.ParameterType = ParameterType;
172 AttributeSubSections.push_back(AttSubSection);
174}
175
176std::unique_ptr<MCELFStreamer::AttributeSubSection>
179 if (SubSection.IsActive) {
180 return std::make_unique<MCELFStreamer::AttributeSubSection>(SubSection);
181 }
182 }
183 return nullptr;
184}
185
186std::unique_ptr<MCELFStreamer::AttributeSubSection>
189 if (Name == SubSection.VendorName) {
190 return std::make_unique<MCELFStreamer::AttributeSubSection>(SubSection);
191 }
192 }
193 return nullptr;
194}
195
197 unsigned Value, std::string String) {
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 // Tag already exists
218 if (Item.Tag == Tag) {
219 Item.Type = unsigned(-1) != Value
222 Item.IntValue = unsigned(-1) != Value ? Value : unsigned(-1);
223 Item.StringValue = unsigned(-1) != Value ? "" : String;
224 return;
225 }
226 }
227 if (unsigned(-1) != Value)
228 SubSection.Content.push_back(MCELFStreamer::AttributeItem(
230 if ("" != String)
231 SubSection.Content.push_back(MCELFStreamer::AttributeItem(
233 String));
234 return;
235 }
236 }
237 assert(0 && "Can not add AArch64 build attribute: required subsection does "
238 "not exist");
239}
240
243 if (VendorName == SubSection.VendorName) {
244 SubSection.IsActive = true;
245 } else {
246 SubSection.IsActive = false;
247 }
248 }
249}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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
static const AArch64AuthMCExpr * create(const MCExpr *Expr, uint16_t Discriminator, AArch64PACKey::ID Key, bool HasAddressDiversity, MCContext &Ctx, SMLoc Loc=SMLoc())
virtual void emitAttributesSubsection(StringRef VendorName, AArch64BuildAttributes::SubsectionOptional IsOptional, AArch64BuildAttributes::SubsectionType ParameterType)
Build attributes implementation.
void activateAttributesSubsection(StringRef VendorName)
std::unique_ptr< MCELFStreamer::AttributeSubSection > getActiveAttributesSubsection()
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 > getAttributesSubsectionByName(StringRef Name)
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 emitAttribute(StringRef VendorName, unsigned Tag, unsigned Value, std::string String)
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
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:496
bool isRegistered() const
Definition: MCSection.h:594
Streaming machine code generation interface.
Definition: MCStreamer.h:220
MCContext & getContext() const
Definition: MCStreamer.h:314
virtual void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=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:132
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:421
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:93
MCStreamer & getStreamer()
Definition: MCStreamer.h:101
MCStreamer & Streamer
Definition: MCStreamer.h:95
Represents a location in source code.
Definition: SMLoc.h:23
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
LLVM Value Representation.
Definition: Value.h:75
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ NT_GNU_PROPERTY_TYPE_0
Definition: ELF.h:1801
@ GNU_PROPERTY_AARCH64_FEATURE_PAUTH
Definition: ELF.h:1835
@ GNU_PROPERTY_AARCH64_FEATURE_1_AND
Definition: ELF.h:1834
@ GNU_PROPERTY_AARCH64_FEATURE_1_BTI
Definition: ELF.h:1850
@ SHT_NOTE
Definition: ELF.h:1146
@ SHF_ALLOC
Definition: ELF.h:1240
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
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:77
enum llvm::MCELFStreamer::AttributeItem::Types Type
ELF object attributes subsection support.
Definition: MCELFStreamer.h:95