LLVM 24.0.0git
MCLFI.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// LFI-specific MC implementation.
11///
12//===----------------------------------------------------------------------===//
13
14#include "llvm/MC/MCLFI.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCInstrInfo.h"
21#include "llvm/MC/MCStreamer.h"
26
27static const char NoteNamespace[] = "LFI";
28
29static constexpr unsigned X86BundleSize = 32;
30
31namespace llvm {
32
33cl::opt<bool> FlagEnableRewriting("lfi-enable-rewriter",
34 cl::desc("Enable rewriting for LFI."),
35 cl::init(true), cl::Hidden);
36
38 const Triple &TheTriple) {
39 assert(TheTriple.isLFI());
40
41 std::string Error;
42 const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple, Error);
43
44 // Create the target-specific MCLFIRewriter.
45 assert(TheTarget != nullptr);
47 auto MRI =
48 std::unique_ptr<MCRegisterInfo>(TheTarget->createMCRegInfo(TheTriple));
49 auto MII = std::unique_ptr<MCInstrInfo>(TheTarget->createMCInstrInfo());
50 Streamer.setLFIRewriter(std::unique_ptr<MCLFIRewriter>(
51 TheTarget->createMCLFIRewriter(Ctx, std::move(MRI), std::move(MII))));
52 }
53}
54
56 const Triple &TheTriple = Ctx.getTargetTriple();
57 assert(TheTriple.isLFI());
58
59 if (TheTriple.getArch() == Triple::x86_64)
60 Streamer.emitBundleAlignMode(Align(X86BundleSize));
61}
62
64 const Triple &TheTriple = Ctx.getTargetTriple();
65 assert(TheTriple.isLFI());
66
67 const char *NoteName;
68 const char *NoteArch;
69 switch (TheTriple.getArch()) {
70 case Triple::aarch64:
71 NoteName = ".note.LFI.ABI.aarch64";
72 NoteArch = "aarch64";
73 break;
74 case Triple::x86_64:
75 NoteName = ".note.LFI.ABI.x86_64";
76 NoteArch = "x86_64";
77 break;
78 default:
79 reportFatalUsageError("Unsupported architecture for LFI");
80 }
81
82 // Emit an ELF Note section in its own COMDAT group which identifies LFI
83 // object files.
84 MCSectionELF *Note = Ctx.getELFSection(NoteName, ELF::SHT_NOTE,
86 NoteName, /*IsComdat=*/true);
87
88 Streamer.switchSection(Note);
89 Streamer.emitIntValue(strlen(NoteNamespace) + 1, 4);
90 Streamer.emitIntValue(strlen(NoteArch) + 1, 4);
91 Streamer.emitIntValue(ELF::NT_VERSION, 4);
92 Streamer.emitBytes(NoteNamespace);
93 Streamer.emitIntValue(0, 1); // NUL terminator
94 Streamer.emitValueToAlignment(Align(4));
95 Streamer.emitBytes(NoteArch);
96 Streamer.emitIntValue(0, 1); // NUL terminator
97 Streamer.emitValueToAlignment(Align(4));
98}
99
100} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares the MCLFIRewriter class, an abstract class that encapsulates the rewriting logic f...
static const char NoteNamespace[]
Definition MCLFI.cpp:27
static constexpr unsigned X86BundleSize
Definition MCLFI.cpp:29
LFI-specific code for MC.
std::unique_ptr< MCStreamer > && Streamer
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Context object for machine code objects.
Definition MCContext.h:83
This represents a section on linux, lots of unix variants and some bare metal systems.
Streaming machine code generation interface.
Definition MCStreamer.h:222
Target - Wrapper for Target specific information.
MCRegisterInfo * createMCRegInfo(const Triple &TT) const
Create a MCRegisterInfo implementation.
MCLFIRewriter * createMCLFIRewriter(MCContext &Ctx, std::unique_ptr< MCRegisterInfo > &&RegInfo, std::unique_ptr< MCInstrInfo > &&InstInfo) const
MCInstrInfo * createMCInstrInfo() const
createMCInstrInfo - Create a MCInstrInfo implementation.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
bool isLFI() const
Tests whether the target is LFI.
Definition Triple.h:1006
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:513
@ SHF_ALLOC
Definition ELF.h:1259
@ SHF_GROUP
Definition ELF.h:1281
@ SHT_NOTE
Definition ELF.h:1163
@ NT_VERSION
Definition ELF.h:1737
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
cl::opt< bool > FlagEnableRewriting("lfi-enable-rewriter", cl::desc("Enable rewriting for LFI."), cl::init(true), cl::Hidden)
LLVM_ABI void initializeLFIMCStreamer(MCStreamer &Streamer, MCContext &Ctx, const Triple &TheTriple)
Definition MCLFI.cpp:37
LLVM_ABI void emitLFINoteSection(MCStreamer &Streamer, MCContext &Ctx)
Definition MCLFI.cpp:63
LLVM_ABI void emitLFIBundleAlign(MCStreamer &Streamer, MCContext &Ctx)
Definition MCLFI.cpp:55
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
static LLVM_ABI const Target * lookupTarget(const Triple &TheTriple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.