PPCXCOFFObjectWriter.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_br24:
  66. // Branches are 4 byte aligned, so the 24 bits we encode in
  67. // the instruction actually represents a 26 bit offset.
  68. return {XCOFF::RelocationType::R_RBR, EncodedSignednessIndicator | 25};
  69. case PPC::fixup_ppc_br24abs:
  70. return {XCOFF::RelocationType::R_RBA, EncodedSignednessIndicator | 25};
  71. case FK_Data_4:
  72. switch (Modifier) {
  73. default:
  74. report_fatal_error("Unsupported modifier");
  75. case MCSymbolRefExpr::VK_PPC_AIX_TLSGD:
  76. return {XCOFF::RelocationType::R_TLS, EncodedSignednessIndicator | 31};
  77. case MCSymbolRefExpr::VK_PPC_AIX_TLSGDM:
  78. return {XCOFF::RelocationType::R_TLSM, EncodedSignednessIndicator | 31};
  79. case MCSymbolRefExpr::VK_None:
  80. return {XCOFF::RelocationType::R_POS, EncodedSignednessIndicator | 31};
  81. }
  82. }
  83. }