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