PPCXCOFFObjectWriter.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //===-- PPCXCOFFObjectWriter.cpp - PowerPC XCOFF Writer -------------------===//
  2. //
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "MCTargetDesc/PPCFixupKinds.h"
  10. #include "MCTargetDesc/PPCMCTargetDesc.h"
  11. #include "llvm/BinaryFormat/XCOFF.h"
  12. #include "llvm/MC/MCFixup.h"
  13. #include "llvm/MC/MCFixupKindInfo.h"
  14. #include "llvm/MC/MCValue.h"
  15. #include "llvm/MC/MCXCOFFObjectWriter.h"
  16. using namespace llvm;
  17. namespace {
  18. class PPCXCOFFObjectWriter : public MCXCOFFObjectTargetWriter {
  19. static constexpr uint8_t SignBitMask = 0x80;
  20. public:
  21. PPCXCOFFObjectWriter(bool Is64Bit);
  22. std::pair<uint8_t, uint8_t>
  23. getRelocTypeAndSignSize(const MCValue &Target, const MCFixup &Fixup,
  24. bool IsPCRel) const override;
  25. };
  26. } // end anonymous namespace
  27. PPCXCOFFObjectWriter::PPCXCOFFObjectWriter(bool Is64Bit)
  28. : MCXCOFFObjectTargetWriter(Is64Bit) {}
  29. std::unique_ptr<MCObjectTargetWriter>
  30. llvm::createPPCXCOFFObjectWriter(bool Is64Bit) {
  31. return std::make_unique<PPCXCOFFObjectWriter>(Is64Bit);
  32. }
  33. std::pair<uint8_t, uint8_t> PPCXCOFFObjectWriter::getRelocTypeAndSignSize(
  34. const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const {
  35. const MCSymbolRefExpr::VariantKind Modifier =
  36. Target.isAbsolute() ? MCSymbolRefExpr::VK_None
  37. : Target.getSymA()->getKind();
  38. // People from AIX OS team says AIX link editor does not care about
  39. // the sign bit in the relocation entry "most" of the time.
  40. // The system assembler seems to set the sign bit on relocation entry
  41. // based on similar property of IsPCRel. So we will do the same here.
  42. // TODO: More investigation on how assembler decides to set the sign
  43. // bit, and we might want to match that.
  44. const uint8_t EncodedSignednessIndicator = IsPCRel ? SignBitMask : 0u;
  45. // The magic number we use in SignAndSize has a strong relationship with
  46. // the corresponding MCFixupKind. In most cases, it's the MCFixupKind
  47. // number - 1, because SignAndSize encodes the bit length being
  48. // relocated minus 1.
  49. switch ((unsigned)Fixup.getKind()) {
  50. default:
  51. report_fatal_error("Unimplemented fixup kind.");
  52. case PPC::fixup_ppc_half16: {
  53. const uint8_t SignAndSizeForHalf16 = EncodedSignednessIndicator | 15;
  54. switch (Modifier) {
  55. default:
  56. report_fatal_error("Unsupported modifier for half16 fixup.");
  57. case MCSymbolRefExpr::VK_None:
  58. return {XCOFF::RelocationType::R_TOC, SignAndSizeForHalf16};
  59. case MCSymbolRefExpr::VK_PPC_U:
  60. return {XCOFF::RelocationType::R_TOCU, SignAndSizeForHalf16};
  61. case MCSymbolRefExpr::VK_PPC_L:
  62. return {XCOFF::RelocationType::R_TOCL, SignAndSizeForHalf16};
  63. }
  64. } break;
  65. case PPC::fixup_ppc_half16ds:
  66. case PPC::fixup_ppc_half16dq: {
  67. if (IsPCRel)
  68. report_fatal_error("Invalid PC-relative relocation.");
  69. switch (Modifier) {
  70. default:
  71. llvm_unreachable("Unsupported Modifier");
  72. case MCSymbolRefExpr::VK_None:
  73. return {XCOFF::RelocationType::R_TOC, 15};
  74. case MCSymbolRefExpr::VK_PPC_L:
  75. return {XCOFF::RelocationType::R_TOCL, 15};
  76. }
  77. } break;
  78. case PPC::fixup_ppc_br24:
  79. // Branches are 4 byte aligned, so the 24 bits we encode in
  80. // the instruction actually represents a 26 bit offset.
  81. return {XCOFF::RelocationType::R_RBR, EncodedSignednessIndicator | 25};
  82. case PPC::fixup_ppc_br24abs:
  83. return {XCOFF::RelocationType::R_RBA, EncodedSignednessIndicator | 25};
  84. case FK_Data_4:
  85. case FK_Data_8:
  86. const uint8_t SignAndSizeForFKData =
  87. EncodedSignednessIndicator |
  88. ((unsigned)Fixup.getKind() == FK_Data_4 ? 31 : 63);
  89. switch (Modifier) {
  90. default:
  91. report_fatal_error("Unsupported modifier");
  92. case MCSymbolRefExpr::VK_PPC_AIX_TLSGD:
  93. return {XCOFF::RelocationType::R_TLS, SignAndSizeForFKData};
  94. case MCSymbolRefExpr::VK_PPC_AIX_TLSGDM:
  95. return {XCOFF::RelocationType::R_TLSM, SignAndSizeForFKData};
  96. case MCSymbolRefExpr::VK_None:
  97. return {XCOFF::RelocationType::R_POS, SignAndSizeForFKData};
  98. }
  99. }
  100. }