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