xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AArch64/MCTargetDesc/AArch64TargetStreamer.cpp (revision a90b9d0159070121c221b966469c3e36d912bf82)
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/MCSection.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/Support/CommandLine.h"
22 
23 using namespace llvm;
24 
25 static cl::opt<bool> MarkBTIProperty(
26     "aarch64-mark-bti-property", cl::Hidden,
27     cl::desc("Add .note.gnu.property with BTI to assembly files"),
28     cl::init(false));
29 
30 //
31 // AArch64TargetStreamer Implemenation
32 //
33 AArch64TargetStreamer::AArch64TargetStreamer(MCStreamer &S)
34     : MCTargetStreamer(S), ConstantPools(new AssemblerConstantPools()) {}
35 
36 AArch64TargetStreamer::~AArch64TargetStreamer() = default;
37 
38 // The constant pool handling is shared by all AArch64TargetStreamer
39 // implementations.
40 const MCExpr *AArch64TargetStreamer::addConstantPoolEntry(const MCExpr *Expr,
41                                                           unsigned Size,
42                                                           SMLoc Loc) {
43   return ConstantPools->addEntry(Streamer, Expr, Size, Loc);
44 }
45 
46 void AArch64TargetStreamer::emitCurrentConstantPool() {
47   ConstantPools->emitForCurrentSection(Streamer);
48 }
49 
50 void AArch64TargetStreamer::emitConstantPools() {
51   ConstantPools->emitAll(Streamer);
52 }
53 
54 // finish() - write out any non-empty assembler constant pools and
55 //   write out note.gnu.properties if need.
56 void AArch64TargetStreamer::finish() {
57   if (MarkBTIProperty)
58     emitNoteSection(ELF::GNU_PROPERTY_AARCH64_FEATURE_1_BTI);
59 }
60 
61 void AArch64TargetStreamer::emitNoteSection(unsigned Flags) {
62   if (Flags == 0)
63     return;
64 
65   MCStreamer &OutStreamer = getStreamer();
66   MCContext &Context = OutStreamer.getContext();
67   // Emit a .note.gnu.property section with the flags.
68   MCSectionELF *Nt = Context.getELFSection(".note.gnu.property", ELF::SHT_NOTE,
69                                            ELF::SHF_ALLOC);
70   if (Nt->isRegistered()) {
71     SMLoc Loc;
72     Context.reportWarning(
73         Loc,
74         "The .note.gnu.property is not emitted because it is already present.");
75     return;
76   }
77   MCSection *Cur = OutStreamer.getCurrentSectionOnly();
78   OutStreamer.switchSection(Nt);
79 
80   // Emit the note header.
81   OutStreamer.emitValueToAlignment(Align(8));
82   OutStreamer.emitIntValue(4, 4);     // data size for "GNU\0"
83   OutStreamer.emitIntValue(4 * 4, 4); // Elf_Prop size
84   OutStreamer.emitIntValue(ELF::NT_GNU_PROPERTY_TYPE_0, 4);
85   OutStreamer.emitBytes(StringRef("GNU", 4)); // note name
86 
87   // Emit the PAC/BTI properties.
88   OutStreamer.emitIntValue(ELF::GNU_PROPERTY_AARCH64_FEATURE_1_AND, 4);
89   OutStreamer.emitIntValue(4, 4);     // data size
90   OutStreamer.emitIntValue(Flags, 4); // data
91   OutStreamer.emitIntValue(0, 4);     // pad
92 
93   OutStreamer.endSection(Nt);
94   OutStreamer.switchSection(Cur);
95 }
96 
97 void AArch64TargetStreamer::emitInst(uint32_t Inst) {
98   char Buffer[4];
99 
100   // We can't just use EmitIntValue here, as that will swap the
101   // endianness on big-endian systems (instructions are always
102   // little-endian).
103   for (char &C : Buffer) {
104     C = uint8_t(Inst);
105     Inst >>= 8;
106   }
107 
108   getStreamer().emitBytes(StringRef(Buffer, 4));
109 }
110 
111 MCTargetStreamer *
112 llvm::createAArch64ObjectTargetStreamer(MCStreamer &S,
113                                         const MCSubtargetInfo &STI) {
114   const Triple &TT = STI.getTargetTriple();
115   if (TT.isOSBinFormatELF())
116     return new AArch64TargetELFStreamer(S);
117   if (TT.isOSBinFormatCOFF())
118     return new AArch64TargetWinCOFFStreamer(S);
119   return nullptr;
120 }
121 
122 MCTargetStreamer *llvm::createAArch64NullTargetStreamer(MCStreamer &S) {
123   return new AArch64TargetStreamer(S);
124 }
125