LLVM 23.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
29namespace llvm {
30
31cl::opt<bool> FlagEnableRewriting("lfi-enable-rewriter",
32 cl::desc("Enable rewriting for LFI."),
33 cl::init(true));
34
36 const Triple &TheTriple) {
37 assert(TheTriple.isLFI());
38
39 std::string Error;
40 const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple, Error);
41
42 // Create the target-specific MCLFIRewriter.
43 assert(TheTarget != nullptr);
45 auto MRI =
46 std::unique_ptr<MCRegisterInfo>(TheTarget->createMCRegInfo(TheTriple));
47 auto MII = std::unique_ptr<MCInstrInfo>(TheTarget->createMCInstrInfo());
48 Streamer.setLFIRewriter(std::unique_ptr<MCLFIRewriter>(
49 TheTarget->createMCLFIRewriter(Ctx, std::move(MRI), std::move(MII))));
50 }
51}
52
54 const Triple &TheTriple = Ctx.getTargetTriple();
55 assert(TheTriple.isLFI());
56
57 const char *NoteName;
58 const char *NoteArch;
59 switch (TheTriple.getArch()) {
60 case Triple::aarch64:
61 NoteName = ".note.LFI.ABI.aarch64";
62 NoteArch = "aarch64";
63 break;
64 default:
65 reportFatalUsageError("Unsupported architecture for LFI");
66 }
67
68 // Emit an ELF Note section in its own COMDAT group which identifies LFI
69 // object files.
70 MCSectionELF *Note = Ctx.getELFSection(NoteName, ELF::SHT_NOTE,
72 NoteName, /*IsComdat=*/true);
73
74 Streamer.switchSection(Note);
75 Streamer.emitIntValue(strlen(NoteNamespace) + 1, 4);
76 Streamer.emitIntValue(strlen(NoteArch) + 1, 4);
77 Streamer.emitIntValue(ELF::NT_VERSION, 4);
78 Streamer.emitBytes(NoteNamespace);
79 Streamer.emitIntValue(0, 1); // NUL terminator
80 Streamer.emitValueToAlignment(Align(4));
81 Streamer.emitBytes(NoteArch);
82 Streamer.emitIntValue(0, 1); // NUL terminator
83 Streamer.emitValueToAlignment(Align(4));
84}
85
86} // 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
LFI-specific code for MC.
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
virtual void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=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.
void setLFIRewriter(std::unique_ptr< MCLFIRewriter > Rewriter)
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
virtual void emitBytes(StringRef Data)
Emit the bytes in Data into the output.
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:47
bool isLFI() const
Tests whether the target is LFI.
Definition Triple.h:969
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:427
@ SHF_ALLOC
Definition ELF.h:1249
@ SHF_GROUP
Definition ELF.h:1271
@ SHT_NOTE
Definition ELF.h:1154
@ NT_VERSION
Definition ELF.h:1727
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))
LLVM_ABI void initializeLFIMCStreamer(MCStreamer &Streamer, MCContext &Ctx, const Triple &TheTriple)
Definition MCLFI.cpp:35
LLVM_ABI void emitLFINoteSection(MCStreamer &Streamer, MCContext &Ctx)
Definition MCLFI.cpp:53
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 const Target * lookupTarget(StringRef TripleStr, std::string &Error)
lookupTarget - Lookup a target based on a target triple.