xref: /freebsd/contrib/llvm-project/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchELFObjectWriter.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
181ad6265SDimitry Andric //===-- LoongArchELFObjectWriter.cpp - LoongArch ELF Writer ---*- C++ -*---===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric 
9bdd1243dSDimitry Andric #include "MCTargetDesc/LoongArchFixupKinds.h"
1081ad6265SDimitry Andric #include "MCTargetDesc/LoongArchMCTargetDesc.h"
11bdd1243dSDimitry Andric #include "llvm/BinaryFormat/ELF.h"
1281ad6265SDimitry Andric #include "llvm/MC/MCContext.h"
1381ad6265SDimitry Andric #include "llvm/MC/MCELFObjectWriter.h"
1481ad6265SDimitry Andric #include "llvm/MC/MCFixup.h"
1581ad6265SDimitry Andric #include "llvm/MC/MCObjectWriter.h"
1681ad6265SDimitry Andric #include "llvm/Support/ErrorHandling.h"
1781ad6265SDimitry Andric 
1881ad6265SDimitry Andric using namespace llvm;
1981ad6265SDimitry Andric 
2081ad6265SDimitry Andric namespace {
2181ad6265SDimitry Andric class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {
2281ad6265SDimitry Andric public:
235f757f3fSDimitry Andric   LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool EnableRelax);
2481ad6265SDimitry Andric 
2581ad6265SDimitry Andric   ~LoongArchELFObjectWriter() override;
2681ad6265SDimitry Andric 
275f757f3fSDimitry Andric   bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
285f757f3fSDimitry Andric                                unsigned Type) const override {
295f757f3fSDimitry Andric     return EnableRelax;
305f757f3fSDimitry Andric   }
315f757f3fSDimitry Andric 
3281ad6265SDimitry Andric protected:
3381ad6265SDimitry Andric   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
3481ad6265SDimitry Andric                         const MCFixup &Fixup, bool IsPCRel) const override;
355f757f3fSDimitry Andric   bool EnableRelax;
3681ad6265SDimitry Andric };
37972a253aSDimitry Andric } // end namespace
3881ad6265SDimitry Andric 
395f757f3fSDimitry Andric LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit,
405f757f3fSDimitry Andric                                                    bool EnableRelax)
4181ad6265SDimitry Andric     : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH,
425f757f3fSDimitry Andric                               /*HasRelocationAddend=*/true),
435f757f3fSDimitry Andric       EnableRelax(EnableRelax) {}
4481ad6265SDimitry Andric 
4581ad6265SDimitry Andric LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {}
4681ad6265SDimitry Andric 
4781ad6265SDimitry Andric unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx,
4881ad6265SDimitry Andric                                                 const MCValue &Target,
4981ad6265SDimitry Andric                                                 const MCFixup &Fixup,
5081ad6265SDimitry Andric                                                 bool IsPCRel) const {
5181ad6265SDimitry Andric   // Determine the type of the relocation
5281ad6265SDimitry Andric   unsigned Kind = Fixup.getTargetKind();
5381ad6265SDimitry Andric 
5481ad6265SDimitry Andric   if (Kind >= FirstLiteralRelocationKind)
5581ad6265SDimitry Andric     return Kind - FirstLiteralRelocationKind;
5681ad6265SDimitry Andric 
5781ad6265SDimitry Andric   switch (Kind) {
5881ad6265SDimitry Andric   default:
59bdd1243dSDimitry Andric     Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type");
6081ad6265SDimitry Andric     return ELF::R_LARCH_NONE;
61bdd1243dSDimitry Andric   case FK_Data_1:
62bdd1243dSDimitry Andric     Ctx.reportError(Fixup.getLoc(), "1-byte data relocations not supported");
63bdd1243dSDimitry Andric     return ELF::R_LARCH_NONE;
64bdd1243dSDimitry Andric   case FK_Data_2:
65bdd1243dSDimitry Andric     Ctx.reportError(Fixup.getLoc(), "2-byte data relocations not supported");
66bdd1243dSDimitry Andric     return ELF::R_LARCH_NONE;
67bdd1243dSDimitry Andric   case FK_Data_4:
68bdd1243dSDimitry Andric     return IsPCRel ? ELF::R_LARCH_32_PCREL : ELF::R_LARCH_32;
69bdd1243dSDimitry Andric   case FK_Data_8:
7006c3fb27SDimitry Andric     return IsPCRel ? ELF::R_LARCH_64_PCREL : ELF::R_LARCH_64;
71bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_b16:
72bdd1243dSDimitry Andric     return ELF::R_LARCH_B16;
73bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_b21:
74bdd1243dSDimitry Andric     return ELF::R_LARCH_B21;
75bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_b26:
76bdd1243dSDimitry Andric     return ELF::R_LARCH_B26;
77bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_abs_hi20:
78bdd1243dSDimitry Andric     return ELF::R_LARCH_ABS_HI20;
79bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_abs_lo12:
80bdd1243dSDimitry Andric     return ELF::R_LARCH_ABS_LO12;
81bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_abs64_lo20:
82bdd1243dSDimitry Andric     return ELF::R_LARCH_ABS64_LO20;
83bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_abs64_hi12:
84bdd1243dSDimitry Andric     return ELF::R_LARCH_ABS64_HI12;
85bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_tls_le_hi20:
86bdd1243dSDimitry Andric     return ELF::R_LARCH_TLS_LE_HI20;
87bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_tls_le_lo12:
88bdd1243dSDimitry Andric     return ELF::R_LARCH_TLS_LE_LO12;
89bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_tls_le64_lo20:
90bdd1243dSDimitry Andric     return ELF::R_LARCH_TLS_LE64_LO20;
91bdd1243dSDimitry Andric   case LoongArch::fixup_loongarch_tls_le64_hi12:
92bdd1243dSDimitry Andric     return ELF::R_LARCH_TLS_LE64_HI12;
93*1db9f3b2SDimitry Andric   case LoongArch::fixup_loongarch_call36:
94*1db9f3b2SDimitry Andric     return ELF::R_LARCH_CALL36;
95bdd1243dSDimitry Andric     // TODO: Handle more fixup-kinds.
9681ad6265SDimitry Andric   }
9781ad6265SDimitry Andric }
9881ad6265SDimitry Andric 
9981ad6265SDimitry Andric std::unique_ptr<MCObjectTargetWriter>
1005f757f3fSDimitry Andric llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax) {
1015f757f3fSDimitry Andric   return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit, Relax);
10281ad6265SDimitry Andric }
103