Path: blob/main/contrib/llvm-project/llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
35293 views
//===-- SparcELFObjectWriter.cpp - Sparc ELF Writer -----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "MCTargetDesc/SparcFixupKinds.h"9#include "MCTargetDesc/SparcMCExpr.h"10#include "MCTargetDesc/SparcMCTargetDesc.h"11#include "llvm/ADT/STLExtras.h"12#include "llvm/MC/MCELFObjectWriter.h"13#include "llvm/MC/MCExpr.h"14#include "llvm/MC/MCObjectWriter.h"15#include "llvm/MC/MCValue.h"16#include "llvm/Support/ErrorHandling.h"1718using namespace llvm;1920namespace {21class SparcELFObjectWriter : public MCELFObjectTargetWriter {22public:23SparcELFObjectWriter(bool Is64Bit, bool HasV9, uint8_t OSABI)24: MCELFObjectTargetWriter(25Is64Bit, OSABI,26Is64Bit ? ELF::EM_SPARCV927: (HasV9 ? ELF::EM_SPARC32PLUS : ELF::EM_SPARC),28/*HasRelocationAddend*/ true) {}2930~SparcELFObjectWriter() override = default;3132protected:33unsigned getRelocType(MCContext &Ctx, const MCValue &Target,34const MCFixup &Fixup, bool IsPCRel) const override;3536bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,37unsigned Type) const override;38};39}4041unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,42const MCValue &Target,43const MCFixup &Fixup,44bool IsPCRel) const {45MCFixupKind Kind = Fixup.getKind();46if (Kind >= FirstLiteralRelocationKind)47return Kind - FirstLiteralRelocationKind;4849if (const SparcMCExpr *SExpr = dyn_cast<SparcMCExpr>(Fixup.getValue())) {50if (SExpr->getKind() == SparcMCExpr::VK_Sparc_R_DISP32)51return ELF::R_SPARC_DISP32;52}5354if (IsPCRel) {55switch(Fixup.getTargetKind()) {56default:57llvm_unreachable("Unimplemented fixup -> relocation");58case FK_Data_1: return ELF::R_SPARC_DISP8;59case FK_Data_2: return ELF::R_SPARC_DISP16;60case FK_Data_4: return ELF::R_SPARC_DISP32;61case FK_Data_8: return ELF::R_SPARC_DISP64;62case Sparc::fixup_sparc_call30: return ELF::R_SPARC_WDISP30;63case Sparc::fixup_sparc_br22: return ELF::R_SPARC_WDISP22;64case Sparc::fixup_sparc_br19: return ELF::R_SPARC_WDISP19;65case Sparc::fixup_sparc_br16:66return ELF::R_SPARC_WDISP16;67case Sparc::fixup_sparc_pc22: return ELF::R_SPARC_PC22;68case Sparc::fixup_sparc_pc10: return ELF::R_SPARC_PC10;69case Sparc::fixup_sparc_wplt30: return ELF::R_SPARC_WPLT30;70}71}7273switch(Fixup.getTargetKind()) {74default:75llvm_unreachable("Unimplemented fixup -> relocation");76case FK_NONE: return ELF::R_SPARC_NONE;77case FK_Data_1: return ELF::R_SPARC_8;78case FK_Data_2: return ((Fixup.getOffset() % 2)79? ELF::R_SPARC_UA1680: ELF::R_SPARC_16);81case FK_Data_4: return ((Fixup.getOffset() % 4)82? ELF::R_SPARC_UA3283: ELF::R_SPARC_32);84case FK_Data_8: return ((Fixup.getOffset() % 8)85? ELF::R_SPARC_UA6486: ELF::R_SPARC_64);87case Sparc::fixup_sparc_13: return ELF::R_SPARC_13;88case Sparc::fixup_sparc_hi22: return ELF::R_SPARC_HI22;89case Sparc::fixup_sparc_lo10: return ELF::R_SPARC_LO10;90case Sparc::fixup_sparc_h44: return ELF::R_SPARC_H44;91case Sparc::fixup_sparc_m44: return ELF::R_SPARC_M44;92case Sparc::fixup_sparc_l44: return ELF::R_SPARC_L44;93case Sparc::fixup_sparc_hh: return ELF::R_SPARC_HH22;94case Sparc::fixup_sparc_hm: return ELF::R_SPARC_HM10;95case Sparc::fixup_sparc_lm: return ELF::R_SPARC_LM22;96case Sparc::fixup_sparc_got22: return ELF::R_SPARC_GOT22;97case Sparc::fixup_sparc_got10: return ELF::R_SPARC_GOT10;98case Sparc::fixup_sparc_got13: return ELF::R_SPARC_GOT13;99case Sparc::fixup_sparc_tls_gd_hi22: return ELF::R_SPARC_TLS_GD_HI22;100case Sparc::fixup_sparc_tls_gd_lo10: return ELF::R_SPARC_TLS_GD_LO10;101case Sparc::fixup_sparc_tls_gd_add: return ELF::R_SPARC_TLS_GD_ADD;102case Sparc::fixup_sparc_tls_gd_call: return ELF::R_SPARC_TLS_GD_CALL;103case Sparc::fixup_sparc_tls_ldm_hi22: return ELF::R_SPARC_TLS_LDM_HI22;104case Sparc::fixup_sparc_tls_ldm_lo10: return ELF::R_SPARC_TLS_LDM_LO10;105case Sparc::fixup_sparc_tls_ldm_add: return ELF::R_SPARC_TLS_LDM_ADD;106case Sparc::fixup_sparc_tls_ldm_call: return ELF::R_SPARC_TLS_LDM_CALL;107case Sparc::fixup_sparc_tls_ldo_hix22: return ELF::R_SPARC_TLS_LDO_HIX22;108case Sparc::fixup_sparc_tls_ldo_lox10: return ELF::R_SPARC_TLS_LDO_LOX10;109case Sparc::fixup_sparc_tls_ldo_add: return ELF::R_SPARC_TLS_LDO_ADD;110case Sparc::fixup_sparc_tls_ie_hi22: return ELF::R_SPARC_TLS_IE_HI22;111case Sparc::fixup_sparc_tls_ie_lo10: return ELF::R_SPARC_TLS_IE_LO10;112case Sparc::fixup_sparc_tls_ie_ld: return ELF::R_SPARC_TLS_IE_LD;113case Sparc::fixup_sparc_tls_ie_ldx: return ELF::R_SPARC_TLS_IE_LDX;114case Sparc::fixup_sparc_tls_ie_add: return ELF::R_SPARC_TLS_IE_ADD;115case Sparc::fixup_sparc_tls_le_hix22: return ELF::R_SPARC_TLS_LE_HIX22;116case Sparc::fixup_sparc_tls_le_lox10: return ELF::R_SPARC_TLS_LE_LOX10;117case Sparc::fixup_sparc_hix22: return ELF::R_SPARC_HIX22;118case Sparc::fixup_sparc_lox10: return ELF::R_SPARC_LOX10;119case Sparc::fixup_sparc_gotdata_hix22: return ELF::R_SPARC_GOTDATA_HIX22;120case Sparc::fixup_sparc_gotdata_lox10: return ELF::R_SPARC_GOTDATA_LOX10;121case Sparc::fixup_sparc_gotdata_op: return ELF::R_SPARC_GOTDATA_OP;122}123124return ELF::R_SPARC_NONE;125}126127bool SparcELFObjectWriter::needsRelocateWithSymbol(const MCValue &,128const MCSymbol &,129unsigned Type) const {130switch (Type) {131default:132return false;133134// All relocations that use a GOT need a symbol, not an offset, as135// the offset of the symbol within the section is irrelevant to136// where the GOT entry is. Don't need to list all the TLS entries,137// as they're all marked as requiring a symbol anyways.138case ELF::R_SPARC_GOT10:139case ELF::R_SPARC_GOT13:140case ELF::R_SPARC_GOT22:141case ELF::R_SPARC_GOTDATA_HIX22:142case ELF::R_SPARC_GOTDATA_LOX10:143case ELF::R_SPARC_GOTDATA_OP_HIX22:144case ELF::R_SPARC_GOTDATA_OP_LOX10:145return true;146}147}148149std::unique_ptr<MCObjectTargetWriter>150llvm::createSparcELFObjectWriter(bool Is64Bit, bool HasV9, uint8_t OSABI) {151return std::make_unique<SparcELFObjectWriter>(Is64Bit, HasV9, OSABI);152}153154155