Path: blob/main/contrib/llvm-project/llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp
35293 views
//===-- VEELFObjectWriter.cpp - VE 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 "VEFixupKinds.h"9#include "VEMCExpr.h"10#include "VEMCTargetDesc.h"11#include "llvm/MC/MCContext.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 VEELFObjectWriter : public MCELFObjectTargetWriter {22public:23VEELFObjectWriter(uint8_t OSABI)24: MCELFObjectTargetWriter(/* Is64Bit */ true, OSABI, ELF::EM_VE,25/* HasRelocationAddend */ true) {}2627~VEELFObjectWriter() override = default;2829protected:30unsigned getRelocType(MCContext &Ctx, const MCValue &Target,31const MCFixup &Fixup, bool IsPCRel) const override;3233bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,34unsigned Type) const override;35};36} // namespace3738unsigned VEELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,39const MCFixup &Fixup,40bool IsPCRel) const {41if (const VEMCExpr *SExpr = dyn_cast<VEMCExpr>(Fixup.getValue())) {42if (SExpr->getKind() == VEMCExpr::VK_VE_PC_LO32)43return ELF::R_VE_PC_LO32;44}4546if (IsPCRel) {47switch (Fixup.getTargetKind()) {48default:49Ctx.reportError(Fixup.getLoc(), "Unsupported pc-relative fixup kind");50return ELF::R_VE_NONE;51case FK_Data_1:52case FK_PCRel_1:53Ctx.reportError(Fixup.getLoc(),54"1-byte pc-relative data relocation is not supported");55return ELF::R_VE_NONE;56case FK_Data_2:57case FK_PCRel_2:58Ctx.reportError(Fixup.getLoc(),59"2-byte pc-relative data relocation is not supported");60return ELF::R_VE_NONE;61case FK_Data_4:62case FK_PCRel_4:63return ELF::R_VE_SREL32;64case FK_Data_8:65case FK_PCRel_8:66Ctx.reportError(Fixup.getLoc(),67"8-byte pc-relative data relocation is not supported");68return ELF::R_VE_NONE;69case VE::fixup_ve_reflong:70case VE::fixup_ve_srel32:71return ELF::R_VE_SREL32;72case VE::fixup_ve_pc_hi32:73return ELF::R_VE_PC_HI32;74case VE::fixup_ve_pc_lo32:75return ELF::R_VE_PC_LO32;76}77}7879switch (Fixup.getTargetKind()) {80default:81Ctx.reportError(Fixup.getLoc(), "Unknown ELF relocation type");82return ELF::R_VE_NONE;83case FK_Data_1:84Ctx.reportError(Fixup.getLoc(), "1-byte data relocation is not supported");85return ELF::R_VE_NONE;86case FK_Data_2:87Ctx.reportError(Fixup.getLoc(), "2-byte data relocation is not supported");88return ELF::R_VE_NONE;89case FK_Data_4:90return ELF::R_VE_REFLONG;91case FK_Data_8:92return ELF::R_VE_REFQUAD;93case VE::fixup_ve_reflong:94return ELF::R_VE_REFLONG;95case VE::fixup_ve_srel32:96Ctx.reportError(Fixup.getLoc(),97"A non pc-relative srel32 relocation is not supported");98return ELF::R_VE_NONE;99case VE::fixup_ve_hi32:100return ELF::R_VE_HI32;101case VE::fixup_ve_lo32:102return ELF::R_VE_LO32;103case VE::fixup_ve_pc_hi32:104Ctx.reportError(Fixup.getLoc(),105"A non pc-relative pc_hi32 relocation is not supported");106return ELF::R_VE_NONE;107case VE::fixup_ve_pc_lo32:108Ctx.reportError(Fixup.getLoc(),109"A non pc-relative pc_lo32 relocation is not supported");110return ELF::R_VE_NONE;111case VE::fixup_ve_got_hi32:112return ELF::R_VE_GOT_HI32;113case VE::fixup_ve_got_lo32:114return ELF::R_VE_GOT_LO32;115case VE::fixup_ve_gotoff_hi32:116return ELF::R_VE_GOTOFF_HI32;117case VE::fixup_ve_gotoff_lo32:118return ELF::R_VE_GOTOFF_LO32;119case VE::fixup_ve_plt_hi32:120return ELF::R_VE_PLT_HI32;121case VE::fixup_ve_plt_lo32:122return ELF::R_VE_PLT_LO32;123case VE::fixup_ve_tls_gd_hi32:124return ELF::R_VE_TLS_GD_HI32;125case VE::fixup_ve_tls_gd_lo32:126return ELF::R_VE_TLS_GD_LO32;127case VE::fixup_ve_tpoff_hi32:128return ELF::R_VE_TPOFF_HI32;129case VE::fixup_ve_tpoff_lo32:130return ELF::R_VE_TPOFF_LO32;131}132133return ELF::R_VE_NONE;134}135136bool VEELFObjectWriter::needsRelocateWithSymbol(const MCValue &,137const MCSymbol &,138unsigned Type) const {139switch (Type) {140default:141return false;142143// All relocations that use a GOT need a symbol, not an offset, as144// the offset of the symbol within the section is irrelevant to145// where the GOT entry is. Don't need to list all the TLS entries,146// as they're all marked as requiring a symbol anyways.147case ELF::R_VE_GOT_HI32:148case ELF::R_VE_GOT_LO32:149case ELF::R_VE_GOTOFF_HI32:150case ELF::R_VE_GOTOFF_LO32:151case ELF::R_VE_TLS_GD_HI32:152case ELF::R_VE_TLS_GD_LO32:153return true;154}155}156157std::unique_ptr<MCObjectTargetWriter>158llvm::createVEELFObjectWriter(uint8_t OSABI) {159return std::make_unique<VEELFObjectWriter>(OSABI);160}161162163