LLVM 20.0.0git
CSKYELFObjectWriter.cpp
Go to the documentation of this file.
1//===-- CSKYELFObjectWriter.cpp - CSKY ELF Writer -------------------------===//
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 "CSKYFixupKinds.h"
10#include "CSKYMCExpr.h"
11#include "CSKYMCTargetDesc.h"
12#include "llvm/MC/MCContext.h"
15
16#define DEBUG_TYPE "csky-elf-object-writer"
17
18using namespace llvm;
19
20namespace {
21
22class CSKYELFObjectWriter : public MCELFObjectTargetWriter {
23public:
24 CSKYELFObjectWriter(uint8_t OSABI = 0)
25 : MCELFObjectTargetWriter(false, OSABI, ELF::EM_CSKY, true){};
26 ~CSKYELFObjectWriter() {}
27
28 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
29 const MCFixup &Fixup, bool IsPCRel) const override;
30};
31
32} // namespace
33
34unsigned CSKYELFObjectWriter::getRelocType(MCContext &Ctx,
35 const MCValue &Target,
36 const MCFixup &Fixup,
37 bool IsPCRel) const {
38 const MCExpr *Expr = Fixup.getValue();
39 // Determine the type of the relocation
40 unsigned Kind = Fixup.getTargetKind();
41 MCSymbolRefExpr::VariantKind Modifier = Target.getAccessVariant();
42
43 if (IsPCRel) {
44 switch (Kind) {
45 default:
46 LLVM_DEBUG(dbgs() << "Unknown Kind1 = " << Kind);
47 Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type");
48 return ELF::R_CKCORE_NONE;
49 case FK_Data_4:
50 case FK_PCRel_4:
51 return ELF::R_CKCORE_PCREL32;
53 return ELF::R_CKCORE_PCREL_IMM16_4;
55 return ELF::R_CKCORE_PCREL_IMM8_4;
57 return ELF::R_CKCORE_PCREL_IMM26_2;
59 return ELF::R_CKCORE_PCREL_IMM18_2;
61 return ELF::R_CKCORE_PCREL_IMM16_2;
63 return ELF::R_CKCORE_PCREL_IMM10_2;
65 return ELF::R_CKCORE_PCREL_IMM7_4;
66 }
67 }
68
69 switch (Kind) {
70 default:
71 LLVM_DEBUG(dbgs() << "Unknown Kind2 = " << Kind);
72 Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type");
73 return ELF::R_CKCORE_NONE;
74 case FK_Data_1:
75 Ctx.reportError(Fixup.getLoc(), "1-byte data relocations not supported");
76 return ELF::R_CKCORE_NONE;
77 case FK_Data_2:
78 Ctx.reportError(Fixup.getLoc(), "2-byte data relocations not supported");
79 return ELF::R_CKCORE_NONE;
80 case FK_Data_4:
81 if (Expr->getKind() == MCExpr::Target) {
82 auto TK = cast<CSKYMCExpr>(Expr)->getKind();
84 return ELF::R_CKCORE_ADDR32;
86 return ELF::R_CKCORE_GOT32;
88 return ELF::R_CKCORE_GOTOFF;
90 return ELF::R_CKCORE_PLT32;
92 return ELF::R_CKCORE_TLS_IE32;
94 return ELF::R_CKCORE_TLS_LE32;
96 return ELF::R_CKCORE_TLS_GD32;
98 return ELF::R_CKCORE_TLS_LDM32;
100 return ELF::R_CKCORE_TLS_LDO32;
102 return ELF::R_CKCORE_GOTPC;
103 if (TK == CSKYMCExpr::VK_CSKY_None)
104 return ELF::R_CKCORE_ADDR32;
105
106 LLVM_DEBUG(dbgs() << "Unknown FK_Data_4 TK = " << TK);
107 Ctx.reportError(Fixup.getLoc(), "unknown target FK_Data_4");
108 } else {
109 switch (Modifier) {
110 default:
111 Ctx.reportError(Fixup.getLoc(),
112 "invalid fixup for 4-byte data relocation");
113 return ELF::R_CKCORE_NONE;
115 return ELF::R_CKCORE_GOT32;
117 return ELF::R_CKCORE_GOTOFF;
119 return ELF::R_CKCORE_PLT32;
121 return ELF::R_CKCORE_TLS_GD32;
123 return ELF::R_CKCORE_TLS_LDM32;
125 return ELF::R_CKCORE_TLS_LE32;
127 return ELF::R_CKCORE_ADDR32;
128 }
129 }
130 return ELF::R_CKCORE_NONE;
131 case FK_Data_8:
132 Ctx.reportError(Fixup.getLoc(), "8-byte data relocations not supported");
133 return ELF::R_CKCORE_NONE;
135 return ELF::R_CKCORE_ADDR32;
137 return ELF::R_CKCORE_ADDR_HI16;
139 return ELF::R_CKCORE_ADDR_LO16;
141 return ELF::R_CKCORE_DOFFSET_IMM18;
143 return ELF::R_CKCORE_DOFFSET_IMM18_2;
145 return ELF::R_CKCORE_DOFFSET_IMM18_4;
147 return ELF::R_CKCORE_GOT_IMM18_4;
149 return ELF::R_CKCORE_PLT_IMM18_4;
150 }
151}
152
153std::unique_ptr<MCObjectTargetWriter> llvm::createCSKYELFObjectWriter() {
154 return std::make_unique<CSKYELFObjectWriter>();
155}
#define LLVM_DEBUG(X)
Definition: Debug.h:101
PowerPC TLS Dynamic Call Fixup
Context object for machine code objects.
Definition: MCContext.h:83
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1068
virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const =0
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
@ Target
Target specific expression.
Definition: MCExpr.h:41
ExprKind getKind() const
Definition: MCExpr.h:78
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
This represents an "assembler immediate".
Definition: MCValue.h:36
Target - Wrapper for Target specific information.
@ fixup_csky_pcrel_imm10_scale2
@ fixup_csky_pcrel_uimm7_scale4
@ fixup_csky_doffset_imm18_scale2
@ fixup_csky_pcrel_imm16_scale2
@ fixup_csky_pcrel_imm18_scale2
@ fixup_csky_doffset_imm18_scale4
@ fixup_csky_pcrel_uimm16_scale4
@ fixup_csky_plt_imm18_scale4
@ fixup_csky_pcrel_imm26_scale2
@ fixup_csky_got_imm18_scale4
@ fixup_csky_pcrel_uimm8_scale4
@ fixup_csky_addr_hi16
@ fixup_csky_addr_lo16
@ fixup_csky_doffset_imm18
@ EM_CSKY
Definition: ELF.h:322
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectTargetWriter > createCSKYELFObjectWriter()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
@ FK_PCRel_4
A four-byte pc relative fixup.
Definition: MCFixup.h:30
@ 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