LLVM 24.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"
22
23using namespace llvm;
24
25//
26// AArch64TargetStreamer Implementation
27//
29 : MCTargetStreamer(S), CLOpts(AArch64MCOptions::Global),
30 ConstantPools(new AssemblerConstantPools()) {}
31
33
35 uint16_t Discriminator,
37 bool HasAddressDiversity) {
38 Streamer.emitValueImpl(AArch64AuthMCExpr::create(Expr, Discriminator, Key,
39 HasAddressDiversity,
40 Streamer.getContext()),
41 8);
42}
43
44// The constant pool handling is shared by all AArch64TargetStreamer
45// implementations.
47 unsigned Size,
48 SMLoc Loc) {
49 return ConstantPools->addEntry(Streamer, Expr, Size, Loc);
50}
51
53 ConstantPools->emitForCurrentSection(Streamer);
54}
55
57 ConstantPools->emitAll(Streamer);
58}
59
60// finish() - write out any non-empty assembler constant pools and
61// write out note.gnu.properties if need.
66
68 uint64_t PAuthABIPlatform,
69 uint64_t PAuthABIVersion) {
70 assert((PAuthABIPlatform == uint64_t(-1)) ==
71 (PAuthABIVersion == uint64_t(-1)));
72 uint64_t DescSz = 0;
73 if (Flags != 0)
74 DescSz += 4 * 4;
75 if (PAuthABIPlatform != uint64_t(-1))
76 DescSz += 4 + 4 + 8 * 2;
77 if (DescSz == 0)
78 return;
79
80 MCStreamer &OutStreamer = getStreamer();
81 MCContext &Context = OutStreamer.getContext();
82 // Emit a .note.gnu.property section with the flags.
83 MCSectionELF *Nt = Context.getELFSection(".note.gnu.property", ELF::SHT_NOTE,
85 if (Nt->isRegistered()) {
86 SMLoc Loc;
87 Context.reportWarning(
88 Loc,
89 "The .note.gnu.property is not emitted because it is already present.");
90 return;
91 }
92 MCSection *Cur = OutStreamer.getCurrentSectionOnly();
93 OutStreamer.switchSection(Nt);
94
95 // Emit the note header.
96 OutStreamer.emitValueToAlignment(Align(8));
97 OutStreamer.emitIntValue(4, 4); // data size for "GNU\0"
98 OutStreamer.emitIntValue(DescSz, 4); // Elf_Prop array size
100 OutStreamer.emitBytes(StringRef("GNU", 4)); // note name
101
102 // Emit the PAC/BTI properties.
103 if (Flags != 0) {
105 OutStreamer.emitIntValue(4, 4); // data size
106 OutStreamer.emitIntValue(Flags, 4); // data
107 OutStreamer.emitIntValue(0, 4); // pad
108 }
109
110 // Emit the PAuth ABI compatibility info
111 if (PAuthABIPlatform != uint64_t(-1)) {
113 OutStreamer.emitIntValue(8 * 2, 4); // data size
114 OutStreamer.emitIntValue(PAuthABIPlatform, 8);
115 OutStreamer.emitIntValue(PAuthABIVersion, 8);
116 }
117
118 OutStreamer.endSection(Nt);
119 OutStreamer.switchSection(Cur);
120}
121
123 char Buffer[4];
124
125 // We can't just use EmitIntValue here, as that will swap the
126 // endianness on big-endian systems (instructions are always
127 // little-endian).
128 for (char &C : Buffer) {
129 C = uint8_t(Inst);
130 Inst >>= 8;
131 }
132
133 getStreamer().emitBytes(StringRef(Buffer, 4));
134}
135
138 const MCSubtargetInfo &STI) {
139 const Triple &TT = STI.getTargetTriple();
140 if (TT.isOSBinFormatELF())
141 return new AArch64TargetELFStreamer(S);
142 if (TT.isOSBinFormatCOFF())
143 return new AArch64TargetWinCOFFStreamer(S);
144 return nullptr;
145}
146
150
154
155 // If exists, return.
157 if (VendorName == SubSection.VendorName) {
159 return;
160 }
161 }
162 // else, add the subsection
164 AttSubSection.VendorName = VendorName;
165 AttSubSection.IsOptional = IsOptional;
166 AttSubSection.ParameterType = ParameterType;
167 AttributeSubSections.push_back(AttSubSection);
169}
170
171std::unique_ptr<MCELFStreamer::AttributeSubSection>
174 if (SubSection.IsActive) {
175 return std::make_unique<MCELFStreamer::AttributeSubSection>(SubSection);
176 }
177 }
178 return nullptr;
179}
180
181std::unique_ptr<MCELFStreamer::AttributeSubSection>
184 if (Name == SubSection.VendorName) {
185 return std::make_unique<MCELFStreamer::AttributeSubSection>(SubSection);
186 }
187 }
188 return nullptr;
189}
190
192 unsigned Value, std::string String) {
193
194 if (unsigned(-1) == Value && "" == String) {
195 assert(0 && "Arguments error");
196 return;
197 }
198 if (AttributeSubSections.size() == 0) {
199 assert(0 &&
200 "Can not add AArch64 build attribute: no AArch64 subsection exists");
201 return;
202 }
203
205 if (VendorName == SubSection.VendorName) {
206 if (!SubSection.IsActive) {
207 assert(0 &&
208 "Can not add AArch64 build attribute: subsection is not active");
209 return;
210 }
211 for (MCELFStreamer::AttributeItem &Item : SubSection.Content) {
212 // Tag already exists
213 if (Item.Tag == Tag) {
214 Item.Type = unsigned(-1) != Value
217 Item.IntValue = unsigned(-1) != Value ? Value : unsigned(-1);
218 Item.StringValue = unsigned(-1) != Value ? "" : String;
219 return;
220 }
221 }
222 if (unsigned(-1) != Value)
223 SubSection.Content.push_back(MCELFStreamer::AttributeItem(
225 if ("" != String)
226 SubSection.Content.push_back(MCELFStreamer::AttributeItem(
228 String));
229 return;
230 }
231 }
232 assert(0 && "Can not add AArch64 build attribute: required subsection does "
233 "not exist");
234}
235
238 if (VendorName == SubSection.VendorName) {
239 SubSection.IsActive = true;
240 } else {
241 SubSection.IsActive = false;
242 }
243 }
244}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition MCSection.h:580
bool isRegistered() const
Definition MCSection.h:679
Streaming machine code generation interface.
Definition MCStreamer.h:222
MCContext & getContext() const
Definition MCStreamer.h:326
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.
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:438
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:95
MCStreamer & getStreamer()
Definition MCStreamer.h:103
MCStreamer & Streamer
Definition MCStreamer.h:97
MCTargetStreamer(MCStreamer &S)
Represents a location in source code.
Definition SMLoc.h:22
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
LLVM Value Representation.
Definition Value.h:75
@ NT_GNU_PROPERTY_TYPE_0
Definition ELF.h:1828
@ SHF_ALLOC
Definition ELF.h:1259
@ GNU_PROPERTY_AARCH64_FEATURE_1_BTI
Definition ELF.h:1877
@ SHT_NOTE
Definition ELF.h:1163
@ GNU_PROPERTY_AARCH64_FEATURE_PAUTH
Definition ELF.h:1862
@ GNU_PROPERTY_AARCH64_FEATURE_1_AND
Definition ELF.h:1861
This is an optimization pass for GlobalISel generic memory operations.
MCTargetStreamer * createAArch64NullTargetStreamer(MCStreamer &S)
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
@ Global
Append to llvm.global_dtors.
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.
enum llvm::MCELFStreamer::AttributeItem::Types Type
ELF object attributes subsection support.