1 //===-- BPFAsmBackend.cpp - BPF Assembler Backend -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MCTargetDesc/BPFMCTargetDesc.h"
10 #include "llvm/ADT/StringRef.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/MCObjectWriter.h"
16 #include "llvm/Support/EndianStream.h"
24 class BPFAsmBackend
: public MCAsmBackend
{
26 BPFAsmBackend(support::endianness Endian
) : MCAsmBackend(Endian
) {}
27 ~BPFAsmBackend() override
= default;
29 void applyFixup(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
30 const MCValue
&Target
, MutableArrayRef
<char> Data
,
31 uint64_t Value
, bool IsResolved
,
32 const MCSubtargetInfo
*STI
) const override
;
34 std::unique_ptr
<MCObjectTargetWriter
>
35 createObjectTargetWriter() const override
;
37 // No instruction requires relaxation
38 bool fixupNeedsRelaxation(const MCFixup
&Fixup
, uint64_t Value
,
39 const MCRelaxableFragment
*DF
,
40 const MCAsmLayout
&Layout
) const override
{
44 unsigned getNumFixupKinds() const override
{ return 1; }
46 bool writeNopData(raw_ostream
&OS
, uint64_t Count
) const override
;
49 } // end anonymous namespace
51 bool BPFAsmBackend::writeNopData(raw_ostream
&OS
, uint64_t Count
) const {
55 for (uint64_t i
= 0; i
< Count
; i
+= 8)
56 support::endian::write
<uint64_t>(OS
, 0x15000000, Endian
);
61 void BPFAsmBackend::applyFixup(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
62 const MCValue
&Target
,
63 MutableArrayRef
<char> Data
, uint64_t Value
,
65 const MCSubtargetInfo
*STI
) const {
66 if (Fixup
.getKind() == FK_SecRel_8
) {
67 // The Value is 0 for global variables, and the in-section offset
68 // for static variables. Write to the immediate field of the inst.
69 assert(Value
<= UINT32_MAX
);
70 support::endian::write
<uint32_t>(&Data
[Fixup
.getOffset() + 4],
71 static_cast<uint32_t>(Value
),
73 } else if (Fixup
.getKind() == FK_Data_4
) {
74 support::endian::write
<uint32_t>(&Data
[Fixup
.getOffset()], Value
, Endian
);
75 } else if (Fixup
.getKind() == FK_Data_8
) {
76 support::endian::write
<uint64_t>(&Data
[Fixup
.getOffset()], Value
, Endian
);
77 } else if (Fixup
.getKind() == FK_PCRel_4
) {
78 Value
= (uint32_t)((Value
- 8) / 8);
79 if (Endian
== support::little
) {
80 Data
[Fixup
.getOffset() + 1] = 0x10;
81 support::endian::write32le(&Data
[Fixup
.getOffset() + 4], Value
);
83 Data
[Fixup
.getOffset() + 1] = 0x1;
84 support::endian::write32be(&Data
[Fixup
.getOffset() + 4], Value
);
87 assert(Fixup
.getKind() == FK_PCRel_2
);
88 Value
= (uint16_t)((Value
- 8) / 8);
89 support::endian::write
<uint16_t>(&Data
[Fixup
.getOffset() + 2], Value
,
94 std::unique_ptr
<MCObjectTargetWriter
>
95 BPFAsmBackend::createObjectTargetWriter() const {
96 return createBPFELFObjectWriter(0);
99 MCAsmBackend
*llvm::createBPFAsmBackend(const Target
&T
,
100 const MCSubtargetInfo
&STI
,
101 const MCRegisterInfo
&MRI
,
102 const MCTargetOptions
&) {
103 return new BPFAsmBackend(support::little
);
106 MCAsmBackend
*llvm::createBPFbeAsmBackend(const Target
&T
,
107 const MCSubtargetInfo
&STI
,
108 const MCRegisterInfo
&MRI
,
109 const MCTargetOptions
&) {
110 return new BPFAsmBackend(support::big
);