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"
20#include "llvm/MC/MCStreamer.h"
25
26static const char NoteNamespace[] = "LFI";
27
28namespace llvm {
29
30cl::opt<bool> FlagEnableRewriting("lfi-enable-rewriter",
31 cl::desc("Enable rewriting for LFI."),
32 cl::init(true));
33
35 const Triple &TheTriple) {
36 assert(TheTriple.isLFI());
37 const char *NoteName;
38 const char *NoteArch;
39 switch (TheTriple.getArch()) {
40 case Triple::aarch64:
41 NoteName = ".note.LFI.ABI.aarch64";
42 NoteArch = "aarch64";
43 break;
44 default:
45 reportFatalUsageError("Unsupported architecture for LFI");
46 }
47
48 std::string Error; // empty
49 const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple, Error);
50
51 // Create the Target specific MCLFIRewriter.
52 assert(TheTarget != nullptr);
54 TheTarget->createMCLFIRewriter(
55 Streamer,
56 std::unique_ptr<MCRegisterInfo>(TheTarget->createMCRegInfo(TheTriple)),
57 std::unique_ptr<MCInstrInfo>(TheTarget->createMCInstrInfo()));
58 }
59
60 // Emit an ELF Note section in its own COMDAT group which identifies LFI
61 // object files.
62 MCSectionELF *Note = Ctx.getELFSection(NoteName, ELF::SHT_NOTE,
64 NoteName, /*IsComdat=*/true);
65
66 Streamer.pushSection();
67 Streamer.switchSection(Note);
68 Streamer.emitIntValue(strlen(NoteNamespace) + 1, 4);
69 Streamer.emitIntValue(strlen(NoteArch) + 1, 4);
70 Streamer.emitIntValue(ELF::NT_VERSION, 4);
71 Streamer.emitBytes(NoteNamespace);
72 Streamer.emitIntValue(0, 1); // NUL terminator
73 Streamer.emitValueToAlignment(Align(4));
74 Streamer.emitBytes(NoteArch);
75 Streamer.emitIntValue(0, 1); // NUL terminator
76 Streamer.emitValueToAlignment(Align(4));
77 Streamer.popSection();
78}
79
80} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static const char NoteNamespace[]
Definition MCLFI.cpp:26
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:221
virtual bool popSection()
Restore the current and previous section from the section stack.
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 pushSection()
Save the current and previous section on the section stack.
Definition MCStreamer.h:449
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.
void createMCLFIRewriter(MCStreamer &S, 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:958
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:419
@ 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.
Definition Types.h:26
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:34
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.