xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Lanai/MCTargetDesc/LanaiELFObjectWriter.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- LanaiELFObjectWriter.cpp - Lanai ELF Writer -----------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "MCTargetDesc/LanaiBaseInfo.h"
100b57cec5SDimitry Andric #include "MCTargetDesc/LanaiFixupKinds.h"
110b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
120b57cec5SDimitry Andric #include "llvm/MC/MCELFObjectWriter.h"
130b57cec5SDimitry Andric #include "llvm/MC/MCObjectWriter.h"
140b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric using namespace llvm;
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric namespace {
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric class LanaiELFObjectWriter : public MCELFObjectTargetWriter {
210b57cec5SDimitry Andric public:
220b57cec5SDimitry Andric   explicit LanaiELFObjectWriter(uint8_t OSABI);
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric   ~LanaiELFObjectWriter() override = default;
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric protected:
270b57cec5SDimitry Andric   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
280b57cec5SDimitry Andric                         const MCFixup &Fixup, bool IsPCRel) const override;
29*5f757f3fSDimitry Andric   bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
300b57cec5SDimitry Andric                                unsigned Type) const override;
310b57cec5SDimitry Andric };
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric } // end anonymous namespace
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric LanaiELFObjectWriter::LanaiELFObjectWriter(uint8_t OSABI)
360b57cec5SDimitry Andric     : MCELFObjectTargetWriter(/*Is64Bit_=*/false, OSABI, ELF::EM_LANAI,
370b57cec5SDimitry Andric                               /*HasRelocationAddend_=*/true) {}
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric unsigned LanaiELFObjectWriter::getRelocType(MCContext & /*Ctx*/,
400b57cec5SDimitry Andric                                             const MCValue & /*Target*/,
410b57cec5SDimitry Andric                                             const MCFixup &Fixup,
420b57cec5SDimitry Andric                                             bool /*IsPCRel*/) const {
430b57cec5SDimitry Andric   unsigned Type;
440b57cec5SDimitry Andric   unsigned Kind = static_cast<unsigned>(Fixup.getKind());
450b57cec5SDimitry Andric   switch (Kind) {
460b57cec5SDimitry Andric   case Lanai::FIXUP_LANAI_21:
470b57cec5SDimitry Andric     Type = ELF::R_LANAI_21;
480b57cec5SDimitry Andric     break;
490b57cec5SDimitry Andric   case Lanai::FIXUP_LANAI_21_F:
500b57cec5SDimitry Andric     Type = ELF::R_LANAI_21_F;
510b57cec5SDimitry Andric     break;
520b57cec5SDimitry Andric   case Lanai::FIXUP_LANAI_25:
530b57cec5SDimitry Andric     Type = ELF::R_LANAI_25;
540b57cec5SDimitry Andric     break;
550b57cec5SDimitry Andric   case Lanai::FIXUP_LANAI_32:
560b57cec5SDimitry Andric   case FK_Data_4:
570b57cec5SDimitry Andric     Type = ELF::R_LANAI_32;
580b57cec5SDimitry Andric     break;
590b57cec5SDimitry Andric   case Lanai::FIXUP_LANAI_HI16:
600b57cec5SDimitry Andric     Type = ELF::R_LANAI_HI16;
610b57cec5SDimitry Andric     break;
620b57cec5SDimitry Andric   case Lanai::FIXUP_LANAI_LO16:
630b57cec5SDimitry Andric     Type = ELF::R_LANAI_LO16;
640b57cec5SDimitry Andric     break;
650b57cec5SDimitry Andric   case Lanai::FIXUP_LANAI_NONE:
660b57cec5SDimitry Andric     Type = ELF::R_LANAI_NONE;
670b57cec5SDimitry Andric     break;
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   default:
700b57cec5SDimitry Andric     llvm_unreachable("Invalid fixup kind!");
710b57cec5SDimitry Andric   }
720b57cec5SDimitry Andric   return Type;
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
75*5f757f3fSDimitry Andric bool LanaiELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
76*5f757f3fSDimitry Andric                                                    const MCSymbol &,
770b57cec5SDimitry Andric                                                    unsigned Type) const {
780b57cec5SDimitry Andric   switch (Type) {
790b57cec5SDimitry Andric   case ELF::R_LANAI_21:
800b57cec5SDimitry Andric   case ELF::R_LANAI_21_F:
810b57cec5SDimitry Andric   case ELF::R_LANAI_25:
820b57cec5SDimitry Andric   case ELF::R_LANAI_32:
830b57cec5SDimitry Andric   case ELF::R_LANAI_HI16:
840b57cec5SDimitry Andric     return true;
850b57cec5SDimitry Andric   default:
860b57cec5SDimitry Andric     return false;
870b57cec5SDimitry Andric   }
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric std::unique_ptr<MCObjectTargetWriter>
910b57cec5SDimitry Andric llvm::createLanaiELFObjectWriter(uint8_t OSABI) {
928bcb0991SDimitry Andric   return std::make_unique<LanaiELFObjectWriter>(OSABI);
930b57cec5SDimitry Andric }
94