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//
30 CLOpts(AArch64_MC::getCLOpts(S.getContext().getTargetOptions())),
31 ConstantPools(new AssemblerConstantPools()) {}
32
34
36 uint16_t Discriminator,
38 bool HasAddressDiversity) {
39 Streamer.emitValueImpl(AArch64AuthMCExpr::create(Expr, Discriminator, Key,
40 HasAddressDiversity,
41 Streamer.getContext()),
42 8);
43}
44
45// The constant pool handling is shared by all AArch64TargetStreamer
46// implementations.
48 unsigned Size,
49 SMLoc Loc) {
50 return ConstantPools->addEntry(Streamer, Expr, Size, Loc);
51}
52
54 ConstantPools->emitForCurrentSection(Streamer);
55}
56
58 ConstantPools->emitAll(Streamer);
59}
60
61// finish() - write out any non-empty assembler constant pools and
62// write out note.gnu.properties if need.
67
69 uint64_t PAuthABIPlatform,
70 uint64_t PAuthABIVersion) {
71 assert((PAuthABIPlatform == uint64_t(-1)) ==
72 (PAuthABIVersion == uint64_t(-1)));
73 uint64_t DescSz = 0;
74 if (Flags != 0)
75 DescSz += 4 * 4;
76 if (PAuthABIPlatform != uint64_t(-1))
77 DescSz += 4 + 4 + 8 * 2;
78 if (DescSz == 0)
79 return;
80
81 MCStreamer &OutStreamer = getStreamer();
82 MCContext &Context = OutStreamer.getContext();
83 // Emit a .note.gnu.property section with the flags.
84 MCSectionELF *Nt = Context.getELFSection(".note.gnu.property", ELF::SHT_NOTE,
86 if (Nt->isRegistered()) {
87 SMLoc Loc;
88 Context.reportWarning(
89 Loc,
90 "The .note.gnu.property is not emitted because it is already present.");
91 return;
92 }
93 MCSection *Cur = OutStreamer.getCurrentSectionOnly();
94 OutStreamer.switchSection(Nt);
95
96 // Emit the note header.
97 OutStreamer.emitValueToAlignment(Align(8));
98 OutStreamer.emitIntValue(4, 4); // data size for "GNU\0"
99 OutStreamer.emitIntValue(DescSz, 4); // Elf_Prop array size
101 OutStreamer.emitBytes(StringRef("GNU", 4)); // note name
102
103 // Emit the PAC/BTI properties.
104 if (Flags != 0) {
106 OutStreamer.emitIntValue(4, 4); // data size
107 OutStreamer.emitIntValue(Flags, 4); // data
108 OutStreamer.emitIntValue(0, 4); // pad
109 }
110
111 // Emit the PAuth ABI compatibility info
112 if (PAuthABIPlatform != uint64_t(-1)) {
114 OutStreamer.emitIntValue(8 * 2, 4); // data size
115 OutStreamer.emitIntValue(PAuthABIPlatform, 8);
116 OutStreamer.emitIntValue(PAuthABIVersion, 8);
117 }
118
119 OutStreamer.endSection(Nt);
120 OutStreamer.switchSection(Cur);
121}
122
124 char Buffer[4];
125
126 // We can't just use EmitIntValue here, as that will swap the
127 // endianness on big-endian systems (instructions are always
128 // little-endian).
129 for (char &C : Buffer) {
130 C = uint8_t(Inst);
131 Inst >>= 8;
132 }
133
134 getStreamer().emitBytes(StringRef(Buffer, 4));
135}
136
139 const MCSubtargetInfo &STI) {
140 const Triple &TT = STI.getTargetTriple();
141 if (TT.isOSBinFormatELF())
142 return new AArch64TargetELFStreamer(S);
143 if (TT.isOSBinFormatCOFF())
144 return new AArch64TargetWinCOFFStreamer(S);
145 return nullptr;
146}
147
151
155
156 // If exists, return.
158 if (VendorName == SubSection.VendorName) {
160 return;
161 }
162 }
163 // else, add the subsection
165 AttSubSection.VendorName = VendorName;
166 AttSubSection.IsOptional = IsOptional;
167 AttSubSection.ParameterType = ParameterType;
168 AttributeSubSections.push_back(AttSubSection);
170}
171
172std::unique_ptr<MCELFStreamer::AttributeSubSection>
175 if (SubSection.IsActive) {
176 return std::make_unique<MCELFStreamer::AttributeSubSection>(SubSection);
177 }
178 }
179 return nullptr;
180}
181
182std::unique_ptr<MCELFStreamer::AttributeSubSection>
185 if (Name == SubSection.VendorName) {
186 return std::make_unique<MCELFStreamer::AttributeSubSection>(SubSection);
187 }
188 }
189 return nullptr;
190}
191
193 unsigned Value, std::string String) {
194
195 if (unsigned(-1) == Value && "" == String) {
196 assert(0 && "Arguments error");
197 return;
198 }
199 if (AttributeSubSections.size() == 0) {
200 assert(0 &&
201 "Can not add AArch64 build attribute: no AArch64 subsection exists");
202 return;
203 }
204
206 if (VendorName == SubSection.VendorName) {
207 if (!SubSection.IsActive) {
208 assert(0 &&
209 "Can not add AArch64 build attribute: subsection is not active");
210 return;
211 }
212 for (MCELFStreamer::AttributeItem &Item : SubSection.Content) {
213 // Tag already exists
214 if (Item.Tag == Tag) {
215 Item.Type = unsigned(-1) != Value
218 Item.IntValue = unsigned(-1) != Value ? Value : unsigned(-1);
219 Item.StringValue = unsigned(-1) != Value ? "" : String;
220 return;
221 }
222 }
223 if (unsigned(-1) != Value)
224 SubSection.Content.push_back(MCELFStreamer::AttributeItem(
226 if ("" != String)
227 SubSection.Content.push_back(MCELFStreamer::AttributeItem(
229 String));
230 return;
231 }
232 }
233 assert(0 && "Can not add AArch64 build attribute: required subsection does "
234 "not exist");
235}
236
239 if (VendorName == SubSection.VendorName) {
240 SubSection.IsActive = true;
241 } else {
242 SubSection.IsActive = false;
243 }
244 }
245}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t 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
MCContext & getContext()
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
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.