LLVM 20.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"
18#include "llvm/MC/MCSection.h"
22
23using namespace llvm;
24
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//
34 : MCTargetStreamer(S), ConstantPools(new AssemblerConstantPools()) {}
35
37
39 uint16_t Discriminator,
41 bool HasAddressDiversity) {
42 Streamer.emitValueImpl(AArch64AuthMCExpr::create(Expr, Discriminator, Key,
43 HasAddressDiversity,
45 8);
46}
47
48// The constant pool handling is shared by all AArch64TargetStreamer
49// implementations.
51 unsigned Size,
52 SMLoc Loc) {
53 return ConstantPools->addEntry(Streamer, Expr, Size, Loc);
54}
55
57 ConstantPools->emitForCurrentSection(Streamer);
58}
59
61 ConstantPools->emitAll(Streamer);
62}
63
64// finish() - write out any non-empty assembler constant pools and
65// write out note.gnu.properties if need.
69}
70
72 uint64_t PAuthABIPlatform,
73 uint64_t PAuthABIVersion) {
74 assert((PAuthABIPlatform == uint64_t(-1)) ==
75 (PAuthABIVersion == uint64_t(-1)));
76 uint64_t DescSz = 0;
77 if (Flags != 0)
78 DescSz += 4 * 4;
79 if (PAuthABIPlatform != uint64_t(-1))
80 DescSz += 4 + 4 + 8 * 2;
81 if (DescSz == 0)
82 return;
83
84 MCStreamer &OutStreamer = getStreamer();
85 MCContext &Context = OutStreamer.getContext();
86 // Emit a .note.gnu.property section with the flags.
87 MCSectionELF *Nt = Context.getELFSection(".note.gnu.property", ELF::SHT_NOTE,
89 if (Nt->isRegistered()) {
90 SMLoc Loc;
91 Context.reportWarning(
92 Loc,
93 "The .note.gnu.property is not emitted because it is already present.");
94 return;
95 }
96 MCSection *Cur = OutStreamer.getCurrentSectionOnly();
97 OutStreamer.switchSection(Nt);
98
99 // Emit the note header.
100 OutStreamer.emitValueToAlignment(Align(8));
101 OutStreamer.emitIntValue(4, 4); // data size for "GNU\0"
102 OutStreamer.emitIntValue(DescSz, 4); // Elf_Prop array size
104 OutStreamer.emitBytes(StringRef("GNU", 4)); // note name
105
106 // Emit the PAC/BTI properties.
107 if (Flags != 0) {
109 OutStreamer.emitIntValue(4, 4); // data size
110 OutStreamer.emitIntValue(Flags, 4); // data
111 OutStreamer.emitIntValue(0, 4); // pad
112 }
113
114 // Emit the PAuth ABI compatibility info
115 if (PAuthABIPlatform != uint64_t(-1)) {
117 OutStreamer.emitIntValue(8 * 2, 4); // data size
118 OutStreamer.emitIntValue(PAuthABIPlatform, 8);
119 OutStreamer.emitIntValue(PAuthABIVersion, 8);
120 }
121
122 OutStreamer.endSection(Nt);
123 OutStreamer.switchSection(Cur);
124}
125
127 char Buffer[4];
128
129 // We can't just use EmitIntValue here, as that will swap the
130 // endianness on big-endian systems (instructions are always
131 // little-endian).
132 for (char &C : Buffer) {
133 C = uint8_t(Inst);
134 Inst >>= 8;
135 }
136
137 getStreamer().emitBytes(StringRef(Buffer, 4));
138}
139
142 const MCSubtargetInfo &STI) {
143 const Triple &TT = STI.getTargetTriple();
144 if (TT.isOSBinFormatELF())
145 return new AArch64TargetELFStreamer(S);
146 if (TT.isOSBinFormatCOFF())
147 return new AArch64TargetWinCOFFStreamer(S);
148 return nullptr;
149}
150
152 return new AArch64TargetStreamer(S);
153}
static cl::opt< bool > MarkBTIProperty("aarch64-mark-bti-property", cl::Hidden, cl::desc("Add .note.gnu.property with BTI to assembly files"), cl::init(false))
uint64_t Size
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const AArch64AuthMCExpr * create(const MCExpr *Expr, uint16_t Discriminator, AArch64PACKey::ID Key, bool HasAddressDiversity, MCContext &Ctx)
const MCExpr * addConstantPoolEntry(const MCExpr *, unsigned Size, SMLoc Loc)
Callback used to implement the ldr= pseudo.
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.
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
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:551
void reportWarning(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1079
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.
Definition: MCSectionELF.h:27
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:36
bool isRegistered() const
Definition: MCSection.h:175
Streaming machine code generation interface.
Definition: MCStreamer.h:213
MCContext & getContext() const
Definition: MCStreamer.h:300
virtual void emitValueToAlignment(Align Alignment, int64_t Value=0, unsigned ValueSize=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.
Definition: MCStreamer.cpp:133
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:400
virtual void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Emit the expression Value into the output as a native integer of the given Size bytes.
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:94
MCStreamer & getStreamer()
Definition: MCStreamer.h:102
MCStreamer & Streamer
Definition: MCStreamer.h:96
Represents a location in source code.
Definition: SMLoc.h:23
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SHT_NOTE
Definition: ELF.h:1098
@ GNU_PROPERTY_AARCH64_FEATURE_1_BTI
Definition: ELF.h:1795
@ SHF_ALLOC
Definition: ELF.h:1190
@ NT_GNU_PROPERTY_TYPE_0
Definition: ELF.h:1747
@ GNU_PROPERTY_AARCH64_FEATURE_PAUTH
Definition: ELF.h:1781
@ GNU_PROPERTY_AARCH64_FEATURE_1_AND
Definition: ELF.h:1780
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MCTargetStreamer * createAArch64NullTargetStreamer(MCStreamer &S)
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