brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · 6d69af5 Raw
106 lines · cpp
1//===-- LoongArchELFObjectWriter.cpp - LoongArch ELF Writer ---*- C++ -*---===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "MCTargetDesc/LoongArchFixupKinds.h"10#include "MCTargetDesc/LoongArchMCTargetDesc.h"11#include "llvm/BinaryFormat/ELF.h"12#include "llvm/MC/MCContext.h"13#include "llvm/MC/MCELFObjectWriter.h"14#include "llvm/MC/MCFixup.h"15#include "llvm/MC/MCObjectWriter.h"16#include "llvm/MC/MCValue.h"17#include "llvm/Support/ErrorHandling.h"18 19using namespace llvm;20 21namespace {22class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {23public:24  LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit);25 26  ~LoongArchELFObjectWriter() override;27 28  bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const override {29    return true;30  }31 32protected:33  unsigned getRelocType(const MCFixup &, const MCValue &,34                        bool IsPCRel) const override;35};36} // end namespace37 38LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit)39    : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH,40                              /*HasRelocationAddend=*/true) {}41 42LoongArchELFObjectWriter::~LoongArchELFObjectWriter() = default;43 44unsigned LoongArchELFObjectWriter::getRelocType(const MCFixup &Fixup,45                                                const MCValue &Target,46                                                bool IsPCRel) const {47  switch (Target.getSpecifier()) {48  case ELF::R_LARCH_TLS_LE_HI20:49  case ELF::R_LARCH_TLS_IE_PC_HI20:50  case ELF::R_LARCH_TLS_IE_HI20:51  case ELF::R_LARCH_TLS_LD_PC_HI20:52  case ELF::R_LARCH_TLS_LD_HI20:53  case ELF::R_LARCH_TLS_GD_PC_HI20:54  case ELF::R_LARCH_TLS_GD_HI20:55  case ELF::R_LARCH_TLS_DESC_PC_HI20:56  case ELF::R_LARCH_TLS_DESC_HI20:57  case ELF::R_LARCH_TLS_LE_HI20_R:58  case ELF::R_LARCH_TLS_LD_PCREL20_S2:59  case ELF::R_LARCH_TLS_GD_PCREL20_S2:60  case ELF::R_LARCH_TLS_DESC_PCREL20_S2:61    if (auto *SA = const_cast<MCSymbol *>(Target.getAddSym()))62      static_cast<MCSymbolELF *>(SA)->setType(ELF::STT_TLS);63    break;64  default:65    break;66  }67 68  auto Kind = Fixup.getKind();69  if (mc::isRelocation(Fixup.getKind()))70    return Kind;71  switch (Kind) {72  default:73    reportError(Fixup.getLoc(), "Unsupported relocation type");74    return ELF::R_LARCH_NONE;75  case FK_Data_1:76    reportError(Fixup.getLoc(), "1-byte data relocations not supported");77    return ELF::R_LARCH_NONE;78  case FK_Data_2:79    reportError(Fixup.getLoc(), "2-byte data relocations not supported");80    return ELF::R_LARCH_NONE;81  case FK_Data_4:82    return IsPCRel ? ELF::R_LARCH_32_PCREL : ELF::R_LARCH_32;83  case FK_Data_8:84    return IsPCRel ? ELF::R_LARCH_64_PCREL : ELF::R_LARCH_64;85  case LoongArch::fixup_loongarch_b16:86    return ELF::R_LARCH_B16;87  case LoongArch::fixup_loongarch_b21:88    return ELF::R_LARCH_B21;89  case LoongArch::fixup_loongarch_b26:90    return ELF::R_LARCH_B26;91  case LoongArch::fixup_loongarch_abs_hi20:92    return ELF::R_LARCH_ABS_HI20;93  case LoongArch::fixup_loongarch_abs_lo12:94    return ELF::R_LARCH_ABS_LO12;95  case LoongArch::fixup_loongarch_abs64_lo20:96    return ELF::R_LARCH_ABS64_LO20;97  case LoongArch::fixup_loongarch_abs64_hi12:98    return ELF::R_LARCH_ABS64_HI12;99  }100}101 102std::unique_ptr<MCObjectTargetWriter>103llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) {104  return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit);105}106