LLVM 22.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"
16#include "llvm/MC/MCValue.h"
18
19using namespace llvm;
20
21namespace {
22class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {
23public:
24 LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit);
25
26 ~LoongArchELFObjectWriter() override;
27
28 bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const override {
29 return true;
30 }
31
32protected:
33 unsigned getRelocType(const MCFixup &, const MCValue &,
34 bool IsPCRel) const override;
35};
36} // end namespace
37
38LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit)
39 : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH,
40 /*HasRelocationAddend=*/true) {}
41
42LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {}
43
44unsigned LoongArchELFObjectWriter::getRelocType(const MCFixup &Fixup,
45 const MCValue &Target,
46 bool IsPCRel) const {
47 switch (Target.getSpecifier()) {
48 case ELF::R_LARCH_TLS_LE_HI20:
49 case ELF::R_LARCH_TLS_IE_PC_HI20:
50 case ELF::R_LARCH_TLS_IE_HI20:
51 case ELF::R_LARCH_TLS_LD_PC_HI20:
52 case ELF::R_LARCH_TLS_LD_HI20:
53 case ELF::R_LARCH_TLS_GD_PC_HI20:
54 case ELF::R_LARCH_TLS_GD_HI20:
55 case ELF::R_LARCH_TLS_DESC_PC_HI20:
56 case ELF::R_LARCH_TLS_DESC_HI20:
57 case ELF::R_LARCH_TLS_LE_HI20_R:
58 case ELF::R_LARCH_TLS_LD_PCREL20_S2:
59 case ELF::R_LARCH_TLS_GD_PCREL20_S2:
60 case ELF::R_LARCH_TLS_DESC_PCREL20_S2:
61 if (auto *SA = const_cast<MCSymbol *>(Target.getAddSym()))
62 static_cast<MCSymbolELF *>(SA)->setType(ELF::STT_TLS);
63 break;
64 default:
65 break;
66 }
67
68 auto Kind = Fixup.getKind();
69 if (mc::isRelocation(Fixup.getKind()))
70 return Kind;
71 switch (Kind) {
72 default:
73 reportError(Fixup.getLoc(), "Unsupported relocation type");
74 return ELF::R_LARCH_NONE;
75 case FK_Data_1:
76 reportError(Fixup.getLoc(), "1-byte data relocations not supported");
77 return ELF::R_LARCH_NONE;
78 case FK_Data_2:
79 reportError(Fixup.getLoc(), "2-byte data relocations not supported");
80 return ELF::R_LARCH_NONE;
81 case FK_Data_4:
82 return IsPCRel ? ELF::R_LARCH_32_PCREL : ELF::R_LARCH_32;
83 case FK_Data_8:
84 return IsPCRel ? ELF::R_LARCH_64_PCREL : ELF::R_LARCH_64;
86 return ELF::R_LARCH_B16;
88 return ELF::R_LARCH_B21;
90 return ELF::R_LARCH_B26;
92 return ELF::R_LARCH_ABS_HI20;
94 return ELF::R_LARCH_ABS_LO12;
96 return ELF::R_LARCH_ABS64_LO20;
98 return ELF::R_LARCH_ABS64_HI12;
99 }
100}
101
102std::unique_ptr<MCObjectTargetWriter>
104 return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit);
105}
static Error reportError(StringRef Message)
PowerPC TLS Dynamic Call Fixup
virtual unsigned getRelocType(const MCFixup &Fixup, const MCValue &Target, bool IsPCRel) const =0
virtual bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:61
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
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:327
@ STT_TLS
Definition: ELF.h:1414
bool isRelocation(MCFixupKind FixupKind)
Definition: MCFixup.h:130
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:37
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:34
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:36
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:35
std::unique_ptr< MCObjectTargetWriter > createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit)