1 | //===-- BPFAsmBackend.cpp - BPF Assembler Backend -------------------------===// |
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 | #include "MCTargetDesc/BPFMCFixups.h" |
10 | #include "MCTargetDesc/BPFMCTargetDesc.h" |
11 | #include "llvm/MC/MCAsmBackend.h" |
12 | #include "llvm/MC/MCAssembler.h" |
13 | #include "llvm/MC/MCContext.h" |
14 | #include "llvm/MC/MCFixup.h" |
15 | #include "llvm/MC/MCFixupKindInfo.h" |
16 | #include "llvm/MC/MCObjectWriter.h" |
17 | #include "llvm/Support/EndianStream.h" |
18 | #include <cassert> |
19 | #include <cstdint> |
20 | |
21 | using namespace llvm; |
22 | |
23 | namespace { |
24 | |
25 | class BPFAsmBackend : public MCAsmBackend { |
26 | public: |
27 | BPFAsmBackend(llvm::endianness Endian) : MCAsmBackend(Endian) {} |
28 | ~BPFAsmBackend() override = default; |
29 | |
30 | void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target, |
31 | MutableArrayRef<char> Data, uint64_t Value, |
32 | bool IsResolved) override; |
33 | |
34 | std::unique_ptr<MCObjectTargetWriter> |
35 | createObjectTargetWriter() const override; |
36 | |
37 | MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override; |
38 | |
39 | bool writeNopData(raw_ostream &OS, uint64_t Count, |
40 | const MCSubtargetInfo *STI) const override; |
41 | }; |
42 | |
43 | } // end anonymous namespace |
44 | |
45 | MCFixupKindInfo BPFAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { |
46 | const static MCFixupKindInfo Infos[BPF::NumTargetFixupKinds] = { |
47 | { .Name: "FK_BPF_PCRel_4" , .TargetOffset: 0, .TargetSize: 32, .Flags: MCFixupKindInfo::FKF_IsPCRel }, |
48 | }; |
49 | |
50 | if (Kind < FirstTargetFixupKind) |
51 | return MCAsmBackend::getFixupKindInfo(Kind); |
52 | |
53 | assert(unsigned(Kind - FirstTargetFixupKind) < BPF::NumTargetFixupKinds && |
54 | "Invalid kind!" ); |
55 | return Infos[Kind - FirstTargetFixupKind]; |
56 | } |
57 | |
58 | bool BPFAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count, |
59 | const MCSubtargetInfo *STI) const { |
60 | if ((Count % 8) != 0) |
61 | return false; |
62 | |
63 | for (uint64_t i = 0; i < Count; i += 8) |
64 | support::endian::write<uint64_t>(os&: OS, value: 0x15000000, endian: Endian); |
65 | |
66 | return true; |
67 | } |
68 | |
69 | void BPFAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup, |
70 | const MCValue &Target, |
71 | MutableArrayRef<char> Data, uint64_t Value, |
72 | bool IsResolved) { |
73 | maybeAddReloc(F, Fixup, Target, Value, IsResolved); |
74 | if (Fixup.getKind() == FK_SecRel_8) { |
75 | // The Value is 0 for global variables, and the in-section offset |
76 | // for static variables. Write to the immediate field of the inst. |
77 | assert(Value <= UINT32_MAX); |
78 | support::endian::write<uint32_t>(memory: &Data[Fixup.getOffset() + 4], |
79 | value: static_cast<uint32_t>(Value), |
80 | endian: Endian); |
81 | } else if (Fixup.getKind() == FK_Data_4) { |
82 | support::endian::write<uint32_t>(memory: &Data[Fixup.getOffset()], value: Value, endian: Endian); |
83 | } else if (Fixup.getKind() == FK_Data_8) { |
84 | support::endian::write<uint64_t>(memory: &Data[Fixup.getOffset()], value: Value, endian: Endian); |
85 | } else if (Fixup.getKind() == FK_PCRel_4) { |
86 | Value = (uint32_t)((Value - 8) / 8); |
87 | if (Endian == llvm::endianness::little) { |
88 | Data[Fixup.getOffset() + 1] = 0x10; |
89 | support::endian::write32le(P: &Data[Fixup.getOffset() + 4], V: Value); |
90 | } else { |
91 | Data[Fixup.getOffset() + 1] = 0x1; |
92 | support::endian::write32be(P: &Data[Fixup.getOffset() + 4], V: Value); |
93 | } |
94 | } else if (Fixup.getTargetKind() == BPF::FK_BPF_PCRel_4) { |
95 | // The input Value represents the number of bytes. |
96 | Value = (uint32_t)((Value - 8) / 8); |
97 | support::endian::write<uint32_t>(memory: &Data[Fixup.getOffset() + 4], value: Value, |
98 | endian: Endian); |
99 | } else { |
100 | assert(Fixup.getKind() == FK_PCRel_2); |
101 | |
102 | int64_t ByteOff = (int64_t)Value - 8; |
103 | if (ByteOff > INT16_MAX * 8 || ByteOff < INT16_MIN * 8) |
104 | report_fatal_error(reason: "Branch target out of insn range" ); |
105 | |
106 | Value = (uint16_t)((Value - 8) / 8); |
107 | support::endian::write<uint16_t>(memory: &Data[Fixup.getOffset() + 2], value: Value, |
108 | endian: Endian); |
109 | } |
110 | } |
111 | |
112 | std::unique_ptr<MCObjectTargetWriter> |
113 | BPFAsmBackend::createObjectTargetWriter() const { |
114 | return createBPFELFObjectWriter(OSABI: 0); |
115 | } |
116 | |
117 | MCAsmBackend *llvm::createBPFAsmBackend(const Target &T, |
118 | const MCSubtargetInfo &STI, |
119 | const MCRegisterInfo &MRI, |
120 | const MCTargetOptions &) { |
121 | return new BPFAsmBackend(llvm::endianness::little); |
122 | } |
123 | |
124 | MCAsmBackend *llvm::createBPFbeAsmBackend(const Target &T, |
125 | const MCSubtargetInfo &STI, |
126 | const MCRegisterInfo &MRI, |
127 | const MCTargetOptions &) { |
128 | return new BPFAsmBackend(llvm::endianness::big); |
129 | } |
130 | |