Path: blob/main/contrib/llvm-project/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp
35294 views
//===-- LoongArchELFObjectWriter.cpp - LoongArch ELF Writer ---*- C++ -*---===//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/LoongArchFixupKinds.h"9#include "MCTargetDesc/LoongArchMCTargetDesc.h"10#include "llvm/BinaryFormat/ELF.h"11#include "llvm/MC/MCContext.h"12#include "llvm/MC/MCELFObjectWriter.h"13#include "llvm/MC/MCFixup.h"14#include "llvm/MC/MCObjectWriter.h"15#include "llvm/Support/ErrorHandling.h"1617using namespace llvm;1819namespace {20class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {21public:22LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool EnableRelax);2324~LoongArchELFObjectWriter() override;2526bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,27unsigned Type) const override {28return EnableRelax;29}3031protected:32unsigned getRelocType(MCContext &Ctx, const MCValue &Target,33const MCFixup &Fixup, bool IsPCRel) const override;34bool EnableRelax;35};36} // end namespace3738LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit,39bool EnableRelax)40: MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH,41/*HasRelocationAddend=*/true),42EnableRelax(EnableRelax) {}4344LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {}4546unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx,47const MCValue &Target,48const MCFixup &Fixup,49bool IsPCRel) const {50// Determine the type of the relocation51unsigned Kind = Fixup.getTargetKind();5253if (Kind >= FirstLiteralRelocationKind)54return Kind - FirstLiteralRelocationKind;5556switch (Kind) {57default:58Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type");59return ELF::R_LARCH_NONE;60case FK_Data_1:61Ctx.reportError(Fixup.getLoc(), "1-byte data relocations not supported");62return ELF::R_LARCH_NONE;63case FK_Data_2:64Ctx.reportError(Fixup.getLoc(), "2-byte data relocations not supported");65return ELF::R_LARCH_NONE;66case FK_Data_4:67return IsPCRel ? ELF::R_LARCH_32_PCREL : ELF::R_LARCH_32;68case FK_Data_8:69return IsPCRel ? ELF::R_LARCH_64_PCREL : ELF::R_LARCH_64;70case LoongArch::fixup_loongarch_b16:71return ELF::R_LARCH_B16;72case LoongArch::fixup_loongarch_b21:73return ELF::R_LARCH_B21;74case LoongArch::fixup_loongarch_b26:75return ELF::R_LARCH_B26;76case LoongArch::fixup_loongarch_abs_hi20:77return ELF::R_LARCH_ABS_HI20;78case LoongArch::fixup_loongarch_abs_lo12:79return ELF::R_LARCH_ABS_LO12;80case LoongArch::fixup_loongarch_abs64_lo20:81return ELF::R_LARCH_ABS64_LO20;82case LoongArch::fixup_loongarch_abs64_hi12:83return ELF::R_LARCH_ABS64_HI12;84case LoongArch::fixup_loongarch_tls_le_hi20:85return ELF::R_LARCH_TLS_LE_HI20;86case LoongArch::fixup_loongarch_tls_le_lo12:87return ELF::R_LARCH_TLS_LE_LO12;88case LoongArch::fixup_loongarch_tls_le64_lo20:89return ELF::R_LARCH_TLS_LE64_LO20;90case LoongArch::fixup_loongarch_tls_le64_hi12:91return ELF::R_LARCH_TLS_LE64_HI12;92case LoongArch::fixup_loongarch_call36:93return ELF::R_LARCH_CALL36;94// TODO: Handle more fixup-kinds.95}96}9798std::unique_ptr<MCObjectTargetWriter>99llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax) {100return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit, Relax);101}102103104