LLVM  6.0.0svn
SparcELFObjectWriter.cpp
Go to the documentation of this file.
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 
13 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCValue.h"
19 
20 using namespace llvm;
21 
22 namespace {
23  class SparcELFObjectWriter : public MCELFObjectTargetWriter {
24  public:
25  SparcELFObjectWriter(bool Is64Bit, uint8_t OSABI)
26  : MCELFObjectTargetWriter(Is64Bit, OSABI,
27  Is64Bit ? ELF::EM_SPARCV9 : ELF::EM_SPARC,
28  /*HasRelocationAddend*/ true) {}
29 
30  ~SparcELFObjectWriter() override {}
31 
32  protected:
33  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
34  const MCFixup &Fixup, bool IsPCRel) const override;
35 
36  bool needsRelocateWithSymbol(const MCSymbol &Sym,
37  unsigned Type) const override;
38 
39  };
40 }
41 
43  const MCValue &Target,
44  const MCFixup &Fixup,
45  bool IsPCRel) const {
46 
47  if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Fixup.getValue())) {
48  if (SExpr->getKind() == SparcMCExpr::VK_Sparc_R_DISP32)
49  return ELF::R_SPARC_DISP32;
50  }
51 
52  if (IsPCRel) {
53  switch((unsigned)Fixup.getKind()) {
54  default:
55  llvm_unreachable("Unimplemented fixup -> relocation");
56  case FK_Data_1: return ELF::R_SPARC_DISP8;
57  case FK_Data_2: return ELF::R_SPARC_DISP16;
58  case FK_Data_4: return ELF::R_SPARC_DISP32;
59  case FK_Data_8: return ELF::R_SPARC_DISP64;
60  case Sparc::fixup_sparc_call30: return ELF::R_SPARC_WDISP30;
61  case Sparc::fixup_sparc_br22: return ELF::R_SPARC_WDISP22;
62  case Sparc::fixup_sparc_br19: return ELF::R_SPARC_WDISP19;
63  case Sparc::fixup_sparc_pc22: return ELF::R_SPARC_PC22;
64  case Sparc::fixup_sparc_pc10: return ELF::R_SPARC_PC10;
65  case Sparc::fixup_sparc_wplt30: return ELF::R_SPARC_WPLT30;
66  }
67  }
68 
69  switch((unsigned)Fixup.getKind()) {
70  default:
71  llvm_unreachable("Unimplemented fixup -> relocation");
72  case FK_Data_1: return ELF::R_SPARC_8;
73  case FK_Data_2: return ((Fixup.getOffset() % 2)
74  ? ELF::R_SPARC_UA16
75  : ELF::R_SPARC_16);
76  case FK_Data_4: return ((Fixup.getOffset() % 4)
77  ? ELF::R_SPARC_UA32
78  : ELF::R_SPARC_32);
79  case FK_Data_8: return ((Fixup.getOffset() % 8)
80  ? ELF::R_SPARC_UA64
81  : ELF::R_SPARC_64);
82  case Sparc::fixup_sparc_hi22: return ELF::R_SPARC_HI22;
83  case Sparc::fixup_sparc_lo10: return ELF::R_SPARC_LO10;
84  case Sparc::fixup_sparc_h44: return ELF::R_SPARC_H44;
85  case Sparc::fixup_sparc_m44: return ELF::R_SPARC_M44;
86  case Sparc::fixup_sparc_l44: return ELF::R_SPARC_L44;
87  case Sparc::fixup_sparc_hh: return ELF::R_SPARC_HH22;
88  case Sparc::fixup_sparc_hm: return ELF::R_SPARC_HM10;
89  case Sparc::fixup_sparc_got22: return ELF::R_SPARC_GOT22;
90  case Sparc::fixup_sparc_got10: return ELF::R_SPARC_GOT10;
91  case Sparc::fixup_sparc_tls_gd_hi22: return ELF::R_SPARC_TLS_GD_HI22;
92  case Sparc::fixup_sparc_tls_gd_lo10: return ELF::R_SPARC_TLS_GD_LO10;
93  case Sparc::fixup_sparc_tls_gd_add: return ELF::R_SPARC_TLS_GD_ADD;
94  case Sparc::fixup_sparc_tls_gd_call: return ELF::R_SPARC_TLS_GD_CALL;
95  case Sparc::fixup_sparc_tls_ldm_hi22: return ELF::R_SPARC_TLS_LDM_HI22;
96  case Sparc::fixup_sparc_tls_ldm_lo10: return ELF::R_SPARC_TLS_LDM_LO10;
97  case Sparc::fixup_sparc_tls_ldm_add: return ELF::R_SPARC_TLS_LDM_ADD;
98  case Sparc::fixup_sparc_tls_ldm_call: return ELF::R_SPARC_TLS_LDM_CALL;
99  case Sparc::fixup_sparc_tls_ldo_hix22: return ELF::R_SPARC_TLS_LDO_HIX22;
100  case Sparc::fixup_sparc_tls_ldo_lox10: return ELF::R_SPARC_TLS_LDO_LOX10;
101  case Sparc::fixup_sparc_tls_ldo_add: return ELF::R_SPARC_TLS_LDO_ADD;
102  case Sparc::fixup_sparc_tls_ie_hi22: return ELF::R_SPARC_TLS_IE_HI22;
103  case Sparc::fixup_sparc_tls_ie_lo10: return ELF::R_SPARC_TLS_IE_LO10;
104  case Sparc::fixup_sparc_tls_ie_ld: return ELF::R_SPARC_TLS_IE_LD;
105  case Sparc::fixup_sparc_tls_ie_ldx: return ELF::R_SPARC_TLS_IE_LDX;
106  case Sparc::fixup_sparc_tls_ie_add: return ELF::R_SPARC_TLS_IE_ADD;
107  case Sparc::fixup_sparc_tls_le_hix22: return ELF::R_SPARC_TLS_LE_HIX22;
108  case Sparc::fixup_sparc_tls_le_lox10: return ELF::R_SPARC_TLS_LE_LOX10;
109  }
110 
111  return ELF::R_SPARC_NONE;
112 }
113 
114 bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
115  unsigned Type) const {
116  switch (Type) {
117  default:
118  return false;
119 
120  // All relocations that use a GOT need a symbol, not an offset, as
121  // the offset of the symbol within the section is irrelevant to
122  // where the GOT entry is. Don't need to list all the TLS entries,
123  // as they're all marked as requiring a symbol anyways.
124  case ELF::R_SPARC_GOT10:
125  case ELF::R_SPARC_GOT13:
126  case ELF::R_SPARC_GOT22:
127  case ELF::R_SPARC_GOTDATA_HIX22:
128  case ELF::R_SPARC_GOTDATA_LOX10:
129  case ELF::R_SPARC_GOTDATA_OP_HIX22:
130  case ELF::R_SPARC_GOTDATA_OP_LOX10:
131  return true;
132  }
133 }
134 
135 std::unique_ptr<MCObjectWriter>
137  bool IsLittleEndian, uint8_t OSABI) {
138  auto MOTW = llvm::make_unique<SparcELFObjectWriter>(Is64Bit, OSABI);
139  return createELFObjectWriter(std::move(MOTW), OS, IsLittleEndian);
140 }
fixup_sparc_got22 - 22-bit fixup corresponding to got22(foo)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
fixup_sparc_m44 - 10-bit fixup corresponding to m44(foo)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
std::unique_ptr< MCObjectWriter > createSparcELFObjectWriter(raw_pwrite_stream &OS, bool Is64Bit, bool IsLIttleEndian, uint8_t OSABI)
fixup_sparc_got10 - 10-bit fixup corresponding to got10(foo)
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:59
fixup_sparc_hm - 10-bit fixup corresponding to hm(foo)
std::unique_ptr< MCObjectWriter > createELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
fixup_sparc_br19 - 19-bit PC relative relocation for branches on icc/xcc
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
fixup_sparc_hh - 22-bit fixup corresponding to hh(foo)
uint32_t getOffset() const
Definition: MCFixup.h:95
fixup_sparc_l44 - 12-bit fixup corresponding to l44(foo)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A one-byte fixup.
Definition: MCFixup.h:24
fixup_sparc_lo10 - 10-bit fixup corresponding to lo(foo)
Target - Wrapper for Target specific information.
fixup_sparc_pc22 - 22-bit fixup corresponding to pc22(foo)
Basic Alias true
A eight-byte fixup.
Definition: MCFixup.h:27
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:337
fixup_sparc_br22 - 22-bit PC relative relocation for branches
const MCExpr * getValue() const
Definition: MCFixup.h:98
fixups for Thread Local Storage
fixup_sparc_hi22 - 22-bit fixup corresponding to hi(foo) for sethi
A two-byte fixup.
Definition: MCFixup.h:25
fixup_sparc_h44 - 22-bit fixup corresponding to h44(foo)
MCFixupKind getKind() const
Definition: MCFixup.h:93
fixup_sparc_pc10 - 10-bit fixup corresponding to pc10(foo)