LoongArchELFObjectWriter.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #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"
  16. using namespace llvm;
  17. namespace {
  18. class LoongArchELFObjectWriter : public MCELFObjectTargetWriter {
  19. public:
  20. LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit);
  21. ~LoongArchELFObjectWriter() override;
  22. protected:
  23. unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
  24. const MCFixup &Fixup, bool IsPCRel) const override;
  25. };
  26. } // end namespace
  27. LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit)
  28. : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH,
  29. /*HasRelocationAddend*/ true) {}
  30. LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {}
  31. unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx,
  32. const MCValue &Target,
  33. const MCFixup &Fixup,
  34. bool IsPCRel) const {
  35. // Determine the type of the relocation
  36. unsigned Kind = Fixup.getTargetKind();
  37. if (Kind >= FirstLiteralRelocationKind)
  38. return Kind - FirstLiteralRelocationKind;
  39. switch (Kind) {
  40. default:
  41. Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type");
  42. return ELF::R_LARCH_NONE;
  43. case FK_Data_1:
  44. Ctx.reportError(Fixup.getLoc(), "1-byte data relocations not supported");
  45. return ELF::R_LARCH_NONE;
  46. case FK_Data_2:
  47. Ctx.reportError(Fixup.getLoc(), "2-byte data relocations not supported");
  48. return ELF::R_LARCH_NONE;
  49. case FK_Data_4:
  50. return IsPCRel ? ELF::R_LARCH_32_PCREL : ELF::R_LARCH_32;
  51. case FK_Data_8:
  52. return ELF::R_LARCH_64;
  53. case LoongArch::fixup_loongarch_b16:
  54. return ELF::R_LARCH_B16;
  55. case LoongArch::fixup_loongarch_b21:
  56. return ELF::R_LARCH_B21;
  57. case LoongArch::fixup_loongarch_b26:
  58. return ELF::R_LARCH_B26;
  59. case LoongArch::fixup_loongarch_abs_hi20:
  60. return ELF::R_LARCH_ABS_HI20;
  61. case LoongArch::fixup_loongarch_abs_lo12:
  62. return ELF::R_LARCH_ABS_LO12;
  63. case LoongArch::fixup_loongarch_abs64_lo20:
  64. return ELF::R_LARCH_ABS64_LO20;
  65. case LoongArch::fixup_loongarch_abs64_hi12:
  66. return ELF::R_LARCH_ABS64_HI12;
  67. case LoongArch::fixup_loongarch_tls_le_hi20:
  68. return ELF::R_LARCH_TLS_LE_HI20;
  69. case LoongArch::fixup_loongarch_tls_le_lo12:
  70. return ELF::R_LARCH_TLS_LE_LO12;
  71. case LoongArch::fixup_loongarch_tls_le64_lo20:
  72. return ELF::R_LARCH_TLS_LE64_LO20;
  73. case LoongArch::fixup_loongarch_tls_le64_hi12:
  74. return ELF::R_LARCH_TLS_LE64_HI12;
  75. // TODO: Handle more fixup-kinds.
  76. }
  77. }
  78. std::unique_ptr<MCObjectTargetWriter>
  79. llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) {
  80. return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit);
  81. }