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 "llvm/BinaryFormat/ELF.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCInstrInfo.h"
18#include "llvm/MC/MCLFIRewriter.h"
19#include "llvm/MC/MCRegisterInfo.h"
20#include "llvm/MC/MCSectionELF.h"
21#include "llvm/MC/MCStreamer.h"
22#include "llvm/MC/TargetRegistry.h"
23#include "llvm/Support/Alignment.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/TargetParser/Triple.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(Val: true), cl::Hidden);
36
37void initializeLFIMCStreamer(MCStreamer &Streamer, MCContext &Ctx,
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);
46 if (FlagEnableRewriting) {
47 auto MRI =
48 std::unique_ptr<MCRegisterInfo>(TheTarget->createMCRegInfo(TT: TheTriple));
49 auto MII = std::unique_ptr<MCInstrInfo>(TheTarget->createMCInstrInfo());
50 Streamer.setLFIRewriter(std::unique_ptr<MCLFIRewriter>(
51 TheTarget->createMCLFIRewriter(Ctx, RegInfo: std::move(MRI), InstInfo: std::move(MII))));
52 }
53}
54
55void emitLFIBundleAlign(MCStreamer &Streamer, MCContext &Ctx) {
56 const Triple &TheTriple = Ctx.getTargetTriple();
57 assert(TheTriple.isLFI());
58
59 if (TheTriple.getArch() == Triple::x86_64)
60 Streamer.emitBundleAlignMode(Alignment: Align(X86BundleSize));
61}
62
63void emitLFINoteSection(MCStreamer &Streamer, MCContext &Ctx) {
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(reason: "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(Section: NoteName, Type: ELF::SHT_NOTE,
85 Flags: ELF::SHF_ALLOC | ELF::SHF_GROUP, EntrySize: 0,
86 Group: NoteName, /*IsComdat=*/true);
87
88 Streamer.switchSection(Section: Note);
89 Streamer.emitIntValue(Value: strlen(s: NoteNamespace) + 1, Size: 4);
90 Streamer.emitIntValue(Value: strlen(s: NoteArch) + 1, Size: 4);
91 Streamer.emitIntValue(Value: ELF::NT_VERSION, Size: 4);
92 Streamer.emitBytes(Data: NoteNamespace);
93 Streamer.emitIntValue(Value: 0, Size: 1); // NUL terminator
94 Streamer.emitValueToAlignment(Alignment: Align(4));
95 Streamer.emitBytes(Data: NoteArch);
96 Streamer.emitIntValue(Value: 0, Size: 1); // NUL terminator
97 Streamer.emitValueToAlignment(Alignment: Align(4));
98}
99
100} // namespace llvm
101