1 //===-- SparcELFObjectWriter.cpp - Sparc ELF Writer -----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "MCTargetDesc/SparcFixupKinds.h"
11 #include "MCTargetDesc/SparcMCExpr.h"
12 #include "MCTargetDesc/SparcMCTargetDesc.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/Support/ErrorHandling.h"
18
19 using namespace llvm;
20
21 namespace {
22 class SparcELFObjectWriter : public MCELFObjectTargetWriter {
23 public:
SparcELFObjectWriter(bool Is64Bit,uint8_t OSABI)24 SparcELFObjectWriter(bool Is64Bit, uint8_t OSABI)
25 : MCELFObjectTargetWriter(Is64Bit, OSABI,
26 Is64Bit ? ELF::EM_SPARCV9 : ELF::EM_SPARC,
27 /*HasRelocationAddend*/ true) {}
28
~SparcELFObjectWriter()29 virtual ~SparcELFObjectWriter() {}
30 protected:
31 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
32 bool IsPCRel) const override;
33 };
34 }
35
GetRelocType(const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const36 unsigned SparcELFObjectWriter::GetRelocType(const MCValue &Target,
37 const MCFixup &Fixup,
38 bool IsPCRel) const {
39
40 if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Fixup.getValue())) {
41 if (SExpr->getKind() == SparcMCExpr::VK_Sparc_R_DISP32)
42 return ELF::R_SPARC_DISP32;
43 }
44
45 if (IsPCRel) {
46 switch((unsigned)Fixup.getKind()) {
47 default:
48 llvm_unreachable("Unimplemented fixup -> relocation");
49 case FK_Data_1: return ELF::R_SPARC_DISP8;
50 case FK_Data_2: return ELF::R_SPARC_DISP16;
51 case FK_Data_4: return ELF::R_SPARC_DISP32;
52 case FK_Data_8: return ELF::R_SPARC_DISP64;
53 case Sparc::fixup_sparc_call30: return ELF::R_SPARC_WDISP30;
54 case Sparc::fixup_sparc_br22: return ELF::R_SPARC_WDISP22;
55 case Sparc::fixup_sparc_br19: return ELF::R_SPARC_WDISP19;
56 case Sparc::fixup_sparc_pc22: return ELF::R_SPARC_PC22;
57 case Sparc::fixup_sparc_pc10: return ELF::R_SPARC_PC10;
58 case Sparc::fixup_sparc_wplt30: return ELF::R_SPARC_WPLT30;
59 }
60 }
61
62 switch((unsigned)Fixup.getKind()) {
63 default:
64 llvm_unreachable("Unimplemented fixup -> relocation");
65 case FK_Data_1: return ELF::R_SPARC_8;
66 case FK_Data_2: return ((Fixup.getOffset() % 2)
67 ? ELF::R_SPARC_UA16
68 : ELF::R_SPARC_16);
69 case FK_Data_4: return ((Fixup.getOffset() % 4)
70 ? ELF::R_SPARC_UA32
71 : ELF::R_SPARC_32);
72 case FK_Data_8: return ((Fixup.getOffset() % 8)
73 ? ELF::R_SPARC_UA64
74 : ELF::R_SPARC_64);
75 case Sparc::fixup_sparc_hi22: return ELF::R_SPARC_HI22;
76 case Sparc::fixup_sparc_lo10: return ELF::R_SPARC_LO10;
77 case Sparc::fixup_sparc_h44: return ELF::R_SPARC_H44;
78 case Sparc::fixup_sparc_m44: return ELF::R_SPARC_M44;
79 case Sparc::fixup_sparc_l44: return ELF::R_SPARC_L44;
80 case Sparc::fixup_sparc_hh: return ELF::R_SPARC_HH22;
81 case Sparc::fixup_sparc_hm: return ELF::R_SPARC_HM10;
82 case Sparc::fixup_sparc_got22: return ELF::R_SPARC_GOT22;
83 case Sparc::fixup_sparc_got10: return ELF::R_SPARC_GOT10;
84 case Sparc::fixup_sparc_tls_gd_hi22: return ELF::R_SPARC_TLS_GD_HI22;
85 case Sparc::fixup_sparc_tls_gd_lo10: return ELF::R_SPARC_TLS_GD_LO10;
86 case Sparc::fixup_sparc_tls_gd_add: return ELF::R_SPARC_TLS_GD_ADD;
87 case Sparc::fixup_sparc_tls_gd_call: return ELF::R_SPARC_TLS_GD_CALL;
88 case Sparc::fixup_sparc_tls_ldm_hi22: return ELF::R_SPARC_TLS_LDM_HI22;
89 case Sparc::fixup_sparc_tls_ldm_lo10: return ELF::R_SPARC_TLS_LDM_LO10;
90 case Sparc::fixup_sparc_tls_ldm_add: return ELF::R_SPARC_TLS_LDM_ADD;
91 case Sparc::fixup_sparc_tls_ldm_call: return ELF::R_SPARC_TLS_LDM_CALL;
92 case Sparc::fixup_sparc_tls_ldo_hix22: return ELF::R_SPARC_TLS_LDO_HIX22;
93 case Sparc::fixup_sparc_tls_ldo_lox10: return ELF::R_SPARC_TLS_LDO_LOX10;
94 case Sparc::fixup_sparc_tls_ldo_add: return ELF::R_SPARC_TLS_LDO_ADD;
95 case Sparc::fixup_sparc_tls_ie_hi22: return ELF::R_SPARC_TLS_IE_HI22;
96 case Sparc::fixup_sparc_tls_ie_lo10: return ELF::R_SPARC_TLS_IE_LO10;
97 case Sparc::fixup_sparc_tls_ie_ld: return ELF::R_SPARC_TLS_IE_LD;
98 case Sparc::fixup_sparc_tls_ie_ldx: return ELF::R_SPARC_TLS_IE_LDX;
99 case Sparc::fixup_sparc_tls_ie_add: return ELF::R_SPARC_TLS_IE_ADD;
100 case Sparc::fixup_sparc_tls_le_hix22: return ELF::R_SPARC_TLS_LE_HIX22;
101 case Sparc::fixup_sparc_tls_le_lox10: return ELF::R_SPARC_TLS_LE_LOX10;
102 }
103
104 return ELF::R_SPARC_NONE;
105 }
106
createSparcELFObjectWriter(raw_ostream & OS,bool Is64Bit,uint8_t OSABI)107 MCObjectWriter *llvm::createSparcELFObjectWriter(raw_ostream &OS,
108 bool Is64Bit,
109 uint8_t OSABI) {
110 MCELFObjectTargetWriter *MOTW = new SparcELFObjectWriter(Is64Bit, OSABI);
111 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false);
112 }
113