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
29namespace llvm {
30
31cl::opt<bool> FlagEnableRewriting("lfi-enable-rewriter",
32 cl::desc("Enable rewriting for LFI."),
33 cl::init(Val: true));
34
35void initializeLFIMCStreamer(MCStreamer &Streamer, MCContext &Ctx,
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);
44 if (FlagEnableRewriting) {
45 auto MRI =
46 std::unique_ptr<MCRegisterInfo>(TheTarget->createMCRegInfo(TT: TheTriple));
47 auto MII = std::unique_ptr<MCInstrInfo>(TheTarget->createMCInstrInfo());
48 Streamer.setLFIRewriter(std::unique_ptr<MCLFIRewriter>(
49 TheTarget->createMCLFIRewriter(Ctx, RegInfo: std::move(MRI), InstInfo: std::move(MII))));
50 }
51}
52
53void emitLFINoteSection(MCStreamer &Streamer, MCContext &Ctx) {
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(reason: "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(Section: NoteName, Type: ELF::SHT_NOTE,
71 Flags: ELF::SHF_ALLOC | ELF::SHF_GROUP, EntrySize: 0,
72 Group: NoteName, /*IsComdat=*/true);
73
74 Streamer.switchSection(Section: Note);
75 Streamer.emitIntValue(Value: strlen(s: NoteNamespace) + 1, Size: 4);
76 Streamer.emitIntValue(Value: strlen(s: NoteArch) + 1, Size: 4);
77 Streamer.emitIntValue(Value: ELF::NT_VERSION, Size: 4);
78 Streamer.emitBytes(Data: NoteNamespace);
79 Streamer.emitIntValue(Value: 0, Size: 1); // NUL terminator
80 Streamer.emitValueToAlignment(Alignment: Align(4));
81 Streamer.emitBytes(Data: NoteArch);
82 Streamer.emitIntValue(Value: 0, Size: 1); // NUL terminator
83 Streamer.emitValueToAlignment(Alignment: Align(4));
84}
85
86} // namespace llvm
87