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"
15#include "MCCLOptions.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCInstrInfo.h"
22#include "llvm/MC/MCStreamer.h"
26
27using namespace llvm;
28
29const char NoteNamespace[] = "LFI";
30
31constexpr unsigned X86BundleSize = 32;
32
34 const Triple &TheTriple) {
35 assert(TheTriple.isLFI());
36
37 std::string Error;
38 const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple, Error);
39
40 // Create the target-specific MCLFIRewriter.
41 assert(TheTarget != nullptr);
42 if (MCCLOptions::Global.lfi_enable_rewriter) {
43 auto MRI =
44 std::unique_ptr<MCRegisterInfo>(TheTarget->createMCRegInfo(TheTriple));
45 auto MII = std::unique_ptr<MCInstrInfo>(TheTarget->createMCInstrInfo());
46 Streamer.setLFIRewriter(std::unique_ptr<MCLFIRewriter>(
47 TheTarget->createMCLFIRewriter(Ctx, std::move(MRI), std::move(MII))));
48 }
49}
50
52 const Triple &TheTriple = Ctx.getTargetTriple();
53 assert(TheTriple.isLFI());
54 if (TheTriple.getArch() == Triple::x86_64)
55 Streamer.emitBundleAlignMode(Align(X86BundleSize));
56}
57
59 const Triple &TheTriple = Ctx.getTargetTriple();
60 assert(TheTriple.isLFI());
61
62 const char *NoteName;
63 const char *NoteArch;
64 switch (TheTriple.getArch()) {
65 case Triple::aarch64:
66 NoteName = ".note.LFI.ABI.aarch64";
67 NoteArch = "aarch64";
68 break;
69 case Triple::x86_64:
70 NoteName = ".note.LFI.ABI.x86_64";
71 NoteArch = "x86_64";
72 break;
73 default:
74 reportFatalUsageError("Unsupported architecture for LFI");
75 }
76
77 // Emit an ELF Note section in its own COMDAT group which identifies LFI
78 // object files.
79 MCSectionELF *Note = Ctx.getELFSection(NoteName, ELF::SHT_NOTE,
81 NoteName, /*IsComdat=*/true);
82
83 Streamer.switchSection(Note);
84 Streamer.emitIntValue(strlen(NoteNamespace) + 1, 4);
85 Streamer.emitIntValue(strlen(NoteArch) + 1, 4);
86 Streamer.emitIntValue(ELF::NT_VERSION, 4);
87 Streamer.emitBytes(NoteNamespace);
88 Streamer.emitIntValue(0, 1); // NUL terminator
89 Streamer.emitValueToAlignment(Align(4));
90 Streamer.emitBytes(NoteArch);
91 Streamer.emitIntValue(0, 1); // NUL terminator
92 Streamer.emitValueToAlignment(Align(4));
93}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares the MCLFIRewriter class, an abstract class that encapsulates the rewriting logic f...
const char NoteNamespace[]
Definition MCLFI.cpp:29
constexpr unsigned X86BundleSize
Definition MCLFI.cpp:31
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:1010
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:514
@ SHF_ALLOC
Definition ELF.h:1259
@ SHF_GROUP
Definition ELF.h:1281
@ SHT_NOTE
Definition ELF.h:1163
@ NT_VERSION
Definition ELF.h:1743
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void initializeLFIMCStreamer(MCStreamer &Streamer, MCContext &Ctx, const Triple &TheTriple)
Definition MCLFI.cpp:33
LLVM_ABI void emitLFINoteSection(MCStreamer &Streamer, MCContext &Ctx)
Definition MCLFI.cpp:58
LLVM_ABI void emitLFIBundleAlign(MCStreamer &Streamer, MCContext &Ctx)
Definition MCLFI.cpp:51
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.