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
13#include "AArch64TargetStreamer.h"
14#include "AArch64MCAsmInfo.h"
15#include "llvm/BinaryFormat/ELF.h"
16#include "llvm/MC/ConstantPools.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCELFStreamer.h"
19#include "llvm/MC/MCSection.h"
20#include "llvm/MC/MCSectionELF.h"
21#include "llvm/MC/MCSubtargetInfo.h"
22
23using namespace llvm;
24
25//
26// AArch64TargetStreamer Implementation
27//
28AArch64TargetStreamer::AArch64TargetStreamer(MCStreamer &S)
29 : MCTargetStreamer(S),
30 CLOpts(AArch64_MC::getCLOpts(Options: S.getContext().getTargetOptions())),
31 ConstantPools(new AssemblerConstantPools()) {}
32
33AArch64TargetStreamer::~AArch64TargetStreamer() = default;
34
35void AArch64TargetStreamer::emitAuthValue(const MCExpr *Expr,
36 uint16_t Discriminator,
37 AArch64PACKey::ID Key,
38 bool HasAddressDiversity) {
39 Streamer.emitValueImpl(Value: AArch64AuthMCExpr::create(Expr, Discriminator, Key,
40 HasAddressDiversity,
41 Ctx&: Streamer.getContext()),
42 Size: 8);
43}
44
45// The constant pool handling is shared by all AArch64TargetStreamer
46// implementations.
47const MCExpr *AArch64TargetStreamer::addConstantPoolEntry(const MCExpr *Expr,
48 unsigned Size,
49 SMLoc Loc) {
50 return ConstantPools->addEntry(Streamer, Expr, Size, Loc);
51}
52
53void AArch64TargetStreamer::emitCurrentConstantPool() {
54 ConstantPools->emitForCurrentSection(Streamer);
55}
56
57void AArch64TargetStreamer::emitConstantPools() {
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.
63void AArch64TargetStreamer::finish() {
64 if (CLOpts.mark_bti_property)
65 emitNoteSection(Flags: ELF::GNU_PROPERTY_AARCH64_FEATURE_1_BTI);
66}
67
68void AArch64TargetStreamer::emitNoteSection(unsigned Flags,
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(Section: ".note.gnu.property", Type: ELF::SHT_NOTE,
85 Flags: ELF::SHF_ALLOC);
86 if (Nt->isRegistered()) {
87 SMLoc Loc;
88 Context.reportWarning(
89 L: Loc,
90 Msg: "The .note.gnu.property is not emitted because it is already present.");
91 return;
92 }
93 MCSection *Cur = OutStreamer.getCurrentSectionOnly();
94 OutStreamer.switchSection(Section: Nt);
95
96 // Emit the note header.
97 OutStreamer.emitValueToAlignment(Alignment: Align(8));
98 OutStreamer.emitIntValue(Value: 4, Size: 4); // data size for "GNU\0"
99 OutStreamer.emitIntValue(Value: DescSz, Size: 4); // Elf_Prop array size
100 OutStreamer.emitIntValue(Value: ELF::NT_GNU_PROPERTY_TYPE_0, Size: 4);
101 OutStreamer.emitBytes(Data: StringRef("GNU", 4)); // note name
102
103 // Emit the PAC/BTI properties.
104 if (Flags != 0) {
105 OutStreamer.emitIntValue(Value: ELF::GNU_PROPERTY_AARCH64_FEATURE_1_AND, Size: 4);
106 OutStreamer.emitIntValue(Value: 4, Size: 4); // data size
107 OutStreamer.emitIntValue(Value: Flags, Size: 4); // data
108 OutStreamer.emitIntValue(Value: 0, Size: 4); // pad
109 }
110
111 // Emit the PAuth ABI compatibility info
112 if (PAuthABIPlatform != uint64_t(-1)) {
113 OutStreamer.emitIntValue(Value: ELF::GNU_PROPERTY_AARCH64_FEATURE_PAUTH, Size: 4);
114 OutStreamer.emitIntValue(Value: 8 * 2, Size: 4); // data size
115 OutStreamer.emitIntValue(Value: PAuthABIPlatform, Size: 8);
116 OutStreamer.emitIntValue(Value: PAuthABIVersion, Size: 8);
117 }
118
119 OutStreamer.endSection(Section: Nt);
120 OutStreamer.switchSection(Section: Cur);
121}
122
123void AArch64TargetStreamer::emitInst(uint32_t Inst) {
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(Data: StringRef(Buffer, 4));
135}
136
137MCTargetStreamer *
138llvm::createAArch64ObjectTargetStreamer(MCStreamer &S,
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
148MCTargetStreamer *llvm::createAArch64NullTargetStreamer(MCStreamer &S) {
149 return new AArch64TargetStreamer(S);
150}
151
152void AArch64TargetStreamer::emitAttributesSubsection(
153 StringRef VendorName, AArch64BuildAttributes::SubsectionOptional IsOptional,
154 AArch64BuildAttributes::SubsectionType ParameterType) {
155
156 // If exists, return.
157 for (MCELFStreamer::AttributeSubSection &SubSection : AttributeSubSections) {
158 if (VendorName == SubSection.VendorName) {
159 activateAttributesSubsection(VendorName);
160 return;
161 }
162 }
163 // else, add the subsection
164 MCELFStreamer::AttributeSubSection AttSubSection;
165 AttSubSection.VendorName = VendorName;
166 AttSubSection.IsOptional = IsOptional;
167 AttSubSection.ParameterType = ParameterType;
168 AttributeSubSections.push_back(Elt: AttSubSection);
169 activateAttributesSubsection(VendorName);
170}
171
172std::unique_ptr<MCELFStreamer::AttributeSubSection>
173AArch64TargetStreamer::getActiveAttributesSubsection() {
174 for (MCELFStreamer::AttributeSubSection &SubSection : AttributeSubSections) {
175 if (SubSection.IsActive) {
176 return std::make_unique<MCELFStreamer::AttributeSubSection>(args&: SubSection);
177 }
178 }
179 return nullptr;
180}
181
182std::unique_ptr<MCELFStreamer::AttributeSubSection>
183AArch64TargetStreamer::getAttributesSubsectionByName(StringRef Name) {
184 for (MCELFStreamer::AttributeSubSection &SubSection : AttributeSubSections) {
185 if (Name == SubSection.VendorName) {
186 return std::make_unique<MCELFStreamer::AttributeSubSection>(args&: SubSection);
187 }
188 }
189 return nullptr;
190}
191
192void AArch64TargetStreamer::emitAttribute(StringRef VendorName, unsigned Tag,
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
205 for (MCELFStreamer::AttributeSubSection &SubSection : AttributeSubSections) {
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
216 ? MCELFStreamer::AttributeItem::NumericAttribute
217 : MCELFStreamer::AttributeItem::TextAttribute;
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(Elt: MCELFStreamer::AttributeItem(
225 MCELFStreamer::AttributeItem::NumericAttribute, Tag, Value, ""));
226 if ("" != String)
227 SubSection.Content.push_back(Elt: MCELFStreamer::AttributeItem(
228 MCELFStreamer::AttributeItem::TextAttribute, Tag, unsigned(-1),
229 String));
230 return;
231 }
232 }
233 assert(0 && "Can not add AArch64 build attribute: required subsection does "
234 "not exist");
235}
236
237void AArch64TargetStreamer::activateAttributesSubsection(StringRef VendorName) {
238 for (MCELFStreamer::AttributeSubSection &SubSection : AttributeSubSections) {
239 if (VendorName == SubSection.VendorName) {
240 SubSection.IsActive = true;
241 } else {
242 SubSection.IsActive = false;
243 }
244 }
245}
246