LLVM API Documentation

SparcELFObjectWriter.cpp
Go to the documentation of this file.
00001 //===-- SparcELFObjectWriter.cpp - Sparc ELF Writer -----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "MCTargetDesc/SparcFixupKinds.h"
00011 #include "MCTargetDesc/SparcMCExpr.h"
00012 #include "MCTargetDesc/SparcMCTargetDesc.h"
00013 #include "llvm/ADT/STLExtras.h"
00014 #include "llvm/MC/MCELFObjectWriter.h"
00015 #include "llvm/MC/MCExpr.h"
00016 #include "llvm/MC/MCValue.h"
00017 #include "llvm/Support/ErrorHandling.h"
00018 
00019 using namespace llvm;
00020 
00021 namespace {
00022   class SparcELFObjectWriter : public MCELFObjectTargetWriter {
00023   public:
00024     SparcELFObjectWriter(bool Is64Bit, uint8_t OSABI)
00025       : MCELFObjectTargetWriter(Is64Bit, OSABI,
00026                                 Is64Bit ?  ELF::EM_SPARCV9 : ELF::EM_SPARC,
00027                                 /*HasRelocationAddend*/ true) {}
00028 
00029     virtual ~SparcELFObjectWriter() {}
00030   protected:
00031     unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
00032                           bool IsPCRel) const override;
00033   };
00034 }
00035 
00036 unsigned SparcELFObjectWriter::GetRelocType(const MCValue &Target,
00037                                             const MCFixup &Fixup,
00038                                             bool IsPCRel) const {
00039 
00040   if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Fixup.getValue())) {
00041     if (SExpr->getKind() == SparcMCExpr::VK_Sparc_R_DISP32)
00042       return ELF::R_SPARC_DISP32;
00043   }
00044 
00045   if (IsPCRel) {
00046     switch((unsigned)Fixup.getKind()) {
00047     default:
00048       llvm_unreachable("Unimplemented fixup -> relocation");
00049     case FK_Data_1:                  return ELF::R_SPARC_DISP8;
00050     case FK_Data_2:                  return ELF::R_SPARC_DISP16;
00051     case FK_Data_4:                  return ELF::R_SPARC_DISP32;
00052     case FK_Data_8:                  return ELF::R_SPARC_DISP64;
00053     case Sparc::fixup_sparc_call30:  return ELF::R_SPARC_WDISP30;
00054     case Sparc::fixup_sparc_br22:    return ELF::R_SPARC_WDISP22;
00055     case Sparc::fixup_sparc_br19:    return ELF::R_SPARC_WDISP19;
00056     case Sparc::fixup_sparc_pc22:    return ELF::R_SPARC_PC22;
00057     case Sparc::fixup_sparc_pc10:    return ELF::R_SPARC_PC10;
00058     case Sparc::fixup_sparc_wplt30:  return ELF::R_SPARC_WPLT30;
00059     }
00060   }
00061 
00062   switch((unsigned)Fixup.getKind()) {
00063   default:
00064     llvm_unreachable("Unimplemented fixup -> relocation");
00065   case FK_Data_1:                return ELF::R_SPARC_8;
00066   case FK_Data_2:                return ((Fixup.getOffset() % 2)
00067                                          ? ELF::R_SPARC_UA16
00068                                          : ELF::R_SPARC_16);
00069   case FK_Data_4:                return ((Fixup.getOffset() % 4)
00070                                          ? ELF::R_SPARC_UA32
00071                                          : ELF::R_SPARC_32);
00072   case FK_Data_8:                return ((Fixup.getOffset() % 8)
00073                                          ? ELF::R_SPARC_UA64
00074                                          : ELF::R_SPARC_64);
00075   case Sparc::fixup_sparc_hi22:  return ELF::R_SPARC_HI22;
00076   case Sparc::fixup_sparc_lo10:  return ELF::R_SPARC_LO10;
00077   case Sparc::fixup_sparc_h44:   return ELF::R_SPARC_H44;
00078   case Sparc::fixup_sparc_m44:   return ELF::R_SPARC_M44;
00079   case Sparc::fixup_sparc_l44:   return ELF::R_SPARC_L44;
00080   case Sparc::fixup_sparc_hh:    return ELF::R_SPARC_HH22;
00081   case Sparc::fixup_sparc_hm:    return ELF::R_SPARC_HM10;
00082   case Sparc::fixup_sparc_got22: return ELF::R_SPARC_GOT22;
00083   case Sparc::fixup_sparc_got10: return ELF::R_SPARC_GOT10;
00084   case Sparc::fixup_sparc_tls_gd_hi22:   return ELF::R_SPARC_TLS_GD_HI22;
00085   case Sparc::fixup_sparc_tls_gd_lo10:   return ELF::R_SPARC_TLS_GD_LO10;
00086   case Sparc::fixup_sparc_tls_gd_add:    return ELF::R_SPARC_TLS_GD_ADD;
00087   case Sparc::fixup_sparc_tls_gd_call:   return ELF::R_SPARC_TLS_GD_CALL;
00088   case Sparc::fixup_sparc_tls_ldm_hi22:  return ELF::R_SPARC_TLS_LDM_HI22;
00089   case Sparc::fixup_sparc_tls_ldm_lo10:  return ELF::R_SPARC_TLS_LDM_LO10;
00090   case Sparc::fixup_sparc_tls_ldm_add:   return ELF::R_SPARC_TLS_LDM_ADD;
00091   case Sparc::fixup_sparc_tls_ldm_call:  return ELF::R_SPARC_TLS_LDM_CALL;
00092   case Sparc::fixup_sparc_tls_ldo_hix22: return ELF::R_SPARC_TLS_LDO_HIX22;
00093   case Sparc::fixup_sparc_tls_ldo_lox10: return ELF::R_SPARC_TLS_LDO_LOX10;
00094   case Sparc::fixup_sparc_tls_ldo_add:   return ELF::R_SPARC_TLS_LDO_ADD;
00095   case Sparc::fixup_sparc_tls_ie_hi22:   return ELF::R_SPARC_TLS_IE_HI22;
00096   case Sparc::fixup_sparc_tls_ie_lo10:   return ELF::R_SPARC_TLS_IE_LO10;
00097   case Sparc::fixup_sparc_tls_ie_ld:     return ELF::R_SPARC_TLS_IE_LD;
00098   case Sparc::fixup_sparc_tls_ie_ldx:    return ELF::R_SPARC_TLS_IE_LDX;
00099   case Sparc::fixup_sparc_tls_ie_add:    return ELF::R_SPARC_TLS_IE_ADD;
00100   case Sparc::fixup_sparc_tls_le_hix22:  return ELF::R_SPARC_TLS_LE_HIX22;
00101   case Sparc::fixup_sparc_tls_le_lox10:  return ELF::R_SPARC_TLS_LE_LOX10;
00102   }
00103 
00104   return ELF::R_SPARC_NONE;
00105 }
00106 
00107 MCObjectWriter *llvm::createSparcELFObjectWriter(raw_ostream &OS,
00108                                                  bool Is64Bit,
00109                                                  uint8_t OSABI) {
00110   MCELFObjectTargetWriter *MOTW = new SparcELFObjectWriter(Is64Bit, OSABI);
00111   return createELFObjectWriter(MOTW, OS,  /*IsLittleEndian=*/false);
00112 }