BPFELFObjectWriter.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //===-- BPFELFObjectWriter.cpp - BPF ELF Writer ---------------------------===//
  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/BPFMCTargetDesc.h"
  9. #include "llvm/BinaryFormat/ELF.h"
  10. #include "llvm/MC/MCELFObjectWriter.h"
  11. #include "llvm/MC/MCFixup.h"
  12. #include "llvm/MC/MCObjectWriter.h"
  13. #include "llvm/MC/MCValue.h"
  14. #include "llvm/Support/ErrorHandling.h"
  15. #include <cstdint>
  16. using namespace llvm;
  17. namespace {
  18. class BPFELFObjectWriter : public MCELFObjectTargetWriter {
  19. public:
  20. BPFELFObjectWriter(uint8_t OSABI);
  21. ~BPFELFObjectWriter() override = default;
  22. protected:
  23. unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
  24. const MCFixup &Fixup, bool IsPCRel) const override;
  25. };
  26. } // end anonymous namespace
  27. BPFELFObjectWriter::BPFELFObjectWriter(uint8_t OSABI)
  28. : MCELFObjectTargetWriter(/*Is64Bit*/ true, OSABI, ELF::EM_BPF,
  29. /*HasRelocationAddend*/ false) {}
  30. unsigned BPFELFObjectWriter::getRelocType(MCContext &Ctx, const MCValue &Target,
  31. const MCFixup &Fixup,
  32. bool IsPCRel) const {
  33. // determine the type of the relocation
  34. switch (Fixup.getKind()) {
  35. default:
  36. llvm_unreachable("invalid fixup kind!");
  37. case FK_SecRel_8:
  38. // LD_imm64 instruction.
  39. return ELF::R_BPF_64_64;
  40. case FK_PCRel_4:
  41. // CALL instruction.
  42. return ELF::R_BPF_64_32;
  43. case FK_Data_8:
  44. return ELF::R_BPF_64_ABS64;
  45. case FK_Data_4:
  46. if (const MCSymbolRefExpr *A = Target.getSymA()) {
  47. const MCSymbol &Sym = A->getSymbol();
  48. if (Sym.isDefined()) {
  49. MCSection &Section = Sym.getSection();
  50. const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
  51. assert(SectionELF && "Null section for reloc symbol");
  52. unsigned Flags = SectionELF->getFlags();
  53. if (Sym.isTemporary()) {
  54. // .BTF.ext generates FK_Data_4 relocations for
  55. // insn offset by creating temporary labels.
  56. // The reloc symbol should be in text section.
  57. // Use a different relocation to instruct ExecutionEngine
  58. // RuntimeDyld not to do relocation for it, yet still to
  59. // allow lld to do proper adjustment when merging sections.
  60. if ((Flags & ELF::SHF_ALLOC) && (Flags & ELF::SHF_EXECINSTR))
  61. return ELF::R_BPF_64_NODYLD32;
  62. } else {
  63. // .BTF generates FK_Data_4 relocations for variable
  64. // offset in DataSec kind.
  65. // The reloc symbol should be in data section.
  66. if ((Flags & ELF::SHF_ALLOC) && (Flags & ELF::SHF_WRITE))
  67. return ELF::R_BPF_64_NODYLD32;
  68. }
  69. }
  70. }
  71. return ELF::R_BPF_64_ABS32;
  72. }
  73. }
  74. std::unique_ptr<MCObjectTargetWriter>
  75. llvm::createBPFELFObjectWriter(uint8_t OSABI) {
  76. return std::make_unique<BPFELFObjectWriter>(OSABI);
  77. }