LLVM 19.0.0git
LoongArchELFObjectWriter.cpp
Go to the documentation of this file.
1//===-- LoongArchELFObjectWriter.cpp - LoongArch ELF Writer ---*- C++ -*---===//
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
12#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCFixup.h"
17
18using namespace llvm;
19
20namespace {
21class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {
22public:
23 LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool EnableRelax);
24
25 ~LoongArchELFObjectWriter() override;
26
27 bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
28 unsigned Type) const override {
29 return EnableRelax;
30 }
31
32protected:
33 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
34 const MCFixup &Fixup, bool IsPCRel) const override;
35 bool EnableRelax;
36};
37} // end namespace
38
39LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit,
40 bool EnableRelax)
41 : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH,
42 /*HasRelocationAddend=*/true),
43 EnableRelax(EnableRelax) {}
44
45LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {}
46
47unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx,
48 const MCValue &Target,
49 const MCFixup &Fixup,
50 bool IsPCRel) const {
51 // Determine the type of the relocation
52 unsigned Kind = Fixup.getTargetKind();
53
56
57 switch (Kind) {
58 default:
59 Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type");
60 return ELF::R_LARCH_NONE;
61 case FK_Data_1:
62 Ctx.reportError(Fixup.getLoc(), "1-byte data relocations not supported");
63 return ELF::R_LARCH_NONE;
64 case FK_Data_2:
65 Ctx.reportError(Fixup.getLoc(), "2-byte data relocations not supported");
66 return ELF::R_LARCH_NONE;
67 case FK_Data_4:
68 return IsPCRel ? ELF::R_LARCH_32_PCREL : ELF::R_LARCH_32;
69 case FK_Data_8:
70 return IsPCRel ? ELF::R_LARCH_64_PCREL : ELF::R_LARCH_64;
72 return ELF::R_LARCH_B16;
74 return ELF::R_LARCH_B21;
76 return ELF::R_LARCH_B26;
78 return ELF::R_LARCH_ABS_HI20;
80 return ELF::R_LARCH_ABS_LO12;
82 return ELF::R_LARCH_ABS64_LO20;
84 return ELF::R_LARCH_ABS64_HI12;
86 return ELF::R_LARCH_TLS_LE_HI20;
88 return ELF::R_LARCH_TLS_LE_LO12;
90 return ELF::R_LARCH_TLS_LE64_LO20;
92 return ELF::R_LARCH_TLS_LE64_HI12;
94 return ELF::R_LARCH_CALL36;
95 // TODO: Handle more fixup-kinds.
96 }
97}
98
99std::unique_ptr<MCObjectTargetWriter>
100llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax) {
101 return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit, Relax);
102}
basic Basic Alias true
Symbol * Sym
Definition: ELF_riscv.cpp:479
PowerPC TLS Dynamic Call Fixup
Context object for machine code objects.
Definition: MCContext.h:76
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1064
virtual bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym, unsigned Type) const
virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const =0
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
This represents an "assembler immediate".
Definition: MCValue.h:36
Target - Wrapper for Target specific information.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
@ EM_LOONGARCH
Definition: ELF.h:322
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectTargetWriter > createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax)
@ FirstLiteralRelocationKind
The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for relocations coming from ....
Definition: MCFixup.h:50
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:24