ARMWinCOFFObjectWriter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===-- ARMWinCOFFObjectWriter.cpp - ARM Windows COFF Object 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/ARMFixupKinds.h"
  9. #include "llvm/ADT/Twine.h"
  10. #include "llvm/BinaryFormat/COFF.h"
  11. #include "llvm/MC/MCAsmBackend.h"
  12. #include "llvm/MC/MCContext.h"
  13. #include "llvm/MC/MCExpr.h"
  14. #include "llvm/MC/MCFixup.h"
  15. #include "llvm/MC/MCFixupKindInfo.h"
  16. #include "llvm/MC/MCObjectWriter.h"
  17. #include "llvm/MC/MCValue.h"
  18. #include "llvm/MC/MCWinCOFFObjectWriter.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <cassert>
  22. using namespace llvm;
  23. namespace {
  24. class ARMWinCOFFObjectWriter : public MCWinCOFFObjectTargetWriter {
  25. public:
  26. ARMWinCOFFObjectWriter()
  27. : MCWinCOFFObjectTargetWriter(COFF::IMAGE_FILE_MACHINE_ARMNT) {
  28. }
  29. ~ARMWinCOFFObjectWriter() override = default;
  30. unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
  31. const MCFixup &Fixup, bool IsCrossSection,
  32. const MCAsmBackend &MAB) const override;
  33. bool recordRelocation(const MCFixup &) const override;
  34. };
  35. } // end anonymous namespace
  36. unsigned ARMWinCOFFObjectWriter::getRelocType(MCContext &Ctx,
  37. const MCValue &Target,
  38. const MCFixup &Fixup,
  39. bool IsCrossSection,
  40. const MCAsmBackend &MAB) const {
  41. MCSymbolRefExpr::VariantKind Modifier =
  42. Target.isAbsolute() ? MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
  43. unsigned FixupKind = Fixup.getKind();
  44. if (IsCrossSection) {
  45. if (FixupKind != FK_Data_4) {
  46. Ctx.reportError(Fixup.getLoc(), "Cannot represent this expression");
  47. return COFF::IMAGE_REL_ARM_ADDR32;
  48. }
  49. FixupKind = FK_PCRel_4;
  50. }
  51. switch (FixupKind) {
  52. default: {
  53. const MCFixupKindInfo &Info = MAB.getFixupKindInfo(Fixup.getKind());
  54. report_fatal_error(Twine("unsupported relocation type: ") + Info.Name);
  55. }
  56. case FK_Data_4:
  57. switch (Modifier) {
  58. case MCSymbolRefExpr::VK_COFF_IMGREL32:
  59. return COFF::IMAGE_REL_ARM_ADDR32NB;
  60. case MCSymbolRefExpr::VK_SECREL:
  61. return COFF::IMAGE_REL_ARM_SECREL;
  62. default:
  63. return COFF::IMAGE_REL_ARM_ADDR32;
  64. }
  65. case FK_PCRel_4:
  66. return COFF::IMAGE_REL_ARM_REL32;
  67. case FK_SecRel_2:
  68. return COFF::IMAGE_REL_ARM_SECTION;
  69. case FK_SecRel_4:
  70. return COFF::IMAGE_REL_ARM_SECREL;
  71. case ARM::fixup_t2_condbranch:
  72. return COFF::IMAGE_REL_ARM_BRANCH20T;
  73. case ARM::fixup_t2_uncondbranch:
  74. case ARM::fixup_arm_thumb_bl:
  75. return COFF::IMAGE_REL_ARM_BRANCH24T;
  76. case ARM::fixup_arm_thumb_blx:
  77. return COFF::IMAGE_REL_ARM_BLX23T;
  78. case ARM::fixup_t2_movw_lo16:
  79. case ARM::fixup_t2_movt_hi16:
  80. return COFF::IMAGE_REL_ARM_MOV32T;
  81. }
  82. }
  83. bool ARMWinCOFFObjectWriter::recordRelocation(const MCFixup &Fixup) const {
  84. return static_cast<unsigned>(Fixup.getKind()) != ARM::fixup_t2_movt_hi16;
  85. }
  86. namespace llvm {
  87. std::unique_ptr<MCObjectTargetWriter>
  88. createARMWinCOFFObjectWriter() {
  89. return std::make_unique<ARMWinCOFFObjectWriter>();
  90. }
  91. } // end namespace llvm