LLVM  15.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"
14 #include "llvm/MC/MCObjectWriter.h"
15 
16 #define DEBUG_TYPE "csky-elf-object-writer"
17 
18 using namespace llvm;
19 
20 namespace {
21 
22 class CSKYELFObjectWriter : public MCELFObjectTargetWriter {
23 public:
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 
34 unsigned 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();
83  if (TK == CSKYMCExpr::VK_CSKY_ADDR)
84  return ELF::R_CKCORE_ADDR32;
85  if (TK == CSKYMCExpr::VK_CSKY_GOT)
86  return ELF::R_CKCORE_GOT32;
88  return ELF::R_CKCORE_GOTOFF;
89  if (TK == CSKYMCExpr::VK_CSKY_PLT)
90  return ELF::R_CKCORE_PLT32;
91  if (TK == CSKYMCExpr::VK_CSKY_TLSIE)
92  return ELF::R_CKCORE_TLS_IE32;
93  if (TK == CSKYMCExpr::VK_CSKY_TLSLE)
94  return ELF::R_CKCORE_TLS_LE32;
95  if (TK == CSKYMCExpr::VK_CSKY_TLSGD)
96  return ELF::R_CKCORE_TLS_GD32;
98  return ELF::R_CKCORE_TLS_LDM32;
100  return ELF::R_CKCORE_TLS_LDO32;
101  if (TK == CSKYMCExpr::VK_CSKY_GOTPC)
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_ADDR32;
122  }
123  }
124  return ELF::R_CKCORE_NONE;
125  case FK_Data_8:
126  Ctx.reportError(Fixup.getLoc(), "8-byte data relocations not supported");
127  return ELF::R_CKCORE_NONE;
129  return ELF::R_CKCORE_ADDR32;
131  return ELF::R_CKCORE_ADDR_HI16;
133  return ELF::R_CKCORE_ADDR_LO16;
135  return ELF::R_CKCORE_DOFFSET_IMM18;
137  return ELF::R_CKCORE_DOFFSET_IMM18_2;
139  return ELF::R_CKCORE_DOFFSET_IMM18_4;
141  return ELF::R_CKCORE_GOT_IMM18_4;
143  return ELF::R_CKCORE_PLT_IMM18_4;
144  }
145 }
146 
147 std::unique_ptr<MCObjectTargetWriter> llvm::createCSKYELFObjectWriter() {
148  return std::make_unique<CSKYELFObjectWriter>();
149 }
llvm::CSKYMCExpr::VK_CSKY_GOTOFF
@ VK_CSKY_GOTOFF
Definition: CSKYMCExpr.h:28
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::CSKYMCExpr::VK_CSKY_GOTPC
@ VK_CSKY_GOTPC
Definition: CSKYMCExpr.h:27
llvm::MCSymbolRefExpr::VK_GOT
@ VK_GOT
Definition: MCExpr.h:198
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:74
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:140
llvm::CSKY::fixup_csky_pcrel_imm10_scale2
@ fixup_csky_pcrel_imm10_scale2
Definition: CSKYFixupKinds.h:43
llvm::CSKY::fixup_csky_pcrel_uimm7_scale4
@ fixup_csky_pcrel_uimm7_scale4
Definition: CSKYFixupKinds.h:45
llvm::CSKY::fixup_csky_doffset_imm18
@ fixup_csky_doffset_imm18
Definition: CSKYFixupKinds.h:49
llvm::CSKYMCExpr::VK_CSKY_GOT
@ VK_CSKY_GOT
Definition: CSKYMCExpr.h:25
llvm::MCSymbolRefExpr::VK_GOTOFF
@ VK_GOTOFF
Definition: MCExpr.h:199
llvm::CSKY::fixup_csky_got_imm18_scale4
@ fixup_csky_got_imm18_scale4
Definition: CSKYFixupKinds.h:37
llvm::CSKY::fixup_csky_pcrel_uimm8_scale4
@ fixup_csky_pcrel_uimm8_scale4
Definition: CSKYFixupKinds.h:47
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
llvm::CSKYMCExpr::VK_CSKY_None
@ VK_CSKY_None
Definition: CSKYMCExpr.h:20
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::CSKYMCExpr::VK_CSKY_TLSLDO
@ VK_CSKY_TLSLDO
Definition: CSKYMCExpr.h:34
llvm::FK_Data_4
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
llvm::MCExpr::Target
@ Target
Target specific expression.
Definition: MCExpr.h:42
llvm::CSKYMCExpr::VK_CSKY_ADDR
@ VK_CSKY_ADDR
Definition: CSKYMCExpr.h:21
llvm::CSKY::fixup_csky_doffset_imm18_scale4
@ fixup_csky_doffset_imm18_scale4
Definition: CSKYFixupKinds.h:53
MCContext.h
llvm::CSKY::fixup_csky_doffset_imm18_scale2
@ fixup_csky_doffset_imm18_scale2
Definition: CSKYFixupKinds.h:51
llvm::MCELFObjectTargetWriter
Definition: MCELFObjectWriter.h:53
llvm::CSKYMCExpr::VK_CSKY_PLT
@ VK_CSKY_PLT
Definition: CSKYMCExpr.h:29
llvm::MCExpr::getKind
ExprKind getKind() const
Definition: MCExpr.h:81
llvm::CSKY::fixup_csky_addr_lo16
@ fixup_csky_addr_lo16
Definition: CSKYFixupKinds.h:21
llvm::CSKYMCExpr::VK_CSKY_TLSLDM
@ VK_CSKY_TLSLDM
Definition: CSKYMCExpr.h:35
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::CSKY::fixup_csky_pcrel_imm26_scale2
@ fixup_csky_pcrel_imm26_scale2
Definition: CSKYFixupKinds.h:27
llvm::CSKY::fixup_csky_pcrel_imm16_scale2
@ fixup_csky_pcrel_imm16_scale2
Definition: CSKYFixupKinds.h:23
llvm::ELF::EM_CSKY
@ EM_CSKY
Definition: ELF.h:321
llvm::MCSymbolRefExpr::VariantKind
VariantKind
Definition: MCExpr.h:194
MCELFObjectWriter.h
llvm::MCSymbolRefExpr::VK_PLT
@ VK_PLT
Definition: MCExpr.h:208
llvm::CSKY::fixup_csky_addr_hi16
@ fixup_csky_addr_hi16
Definition: CSKYFixupKinds.h:19
CSKYMCTargetDesc.h
llvm::MCContext::reportError
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1005
llvm::CSKY::fixup_csky_pcrel_uimm16_scale4
@ fixup_csky_pcrel_uimm16_scale4
Definition: CSKYFixupKinds.h:25
llvm::FK_Data_1
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
llvm::FK_PCRel_4
@ FK_PCRel_4
A four-byte pc relative fixup.
Definition: MCFixup.h:30
llvm::CSKYMCExpr::VK_CSKY_TLSIE
@ VK_CSKY_TLSIE
Definition: CSKYMCExpr.h:31
llvm::CSKY::fixup_csky_plt_imm18_scale4
@ fixup_csky_plt_imm18_scale4
Definition: CSKYFixupKinds.h:41
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:233
llvm::CSKYMCExpr::VK_CSKY_TLSLE
@ VK_CSKY_TLSLE
Definition: CSKYMCExpr.h:32
llvm::CSKY::fixup_csky_pcrel_imm18_scale2
@ fixup_csky_pcrel_imm18_scale2
Definition: CSKYFixupKinds.h:29
llvm::createCSKYELFObjectWriter
std::unique_ptr< MCObjectTargetWriter > createCSKYELFObjectWriter()
Definition: CSKYELFObjectWriter.cpp:147
MCObjectWriter.h
llvm::CSKYMCExpr::VK_CSKY_TLSGD
@ VK_CSKY_TLSGD
Definition: CSKYMCExpr.h:33
CSKYFixupKinds.h
CSKYMCExpr.h
llvm::FK_Data_8
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
llvm::CSKY::fixup_csky_addr32
@ fixup_csky_addr32
Definition: CSKYFixupKinds.h:17
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:36
llvm::MCSymbolRefExpr::VK_None
@ VK_None
Definition: MCExpr.h:195
llvm::FK_Data_2
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:24
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35