MCAsmBackend.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //===- MCAsmBackend.cpp - Target MC Assembly Backend ----------------------===//
  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 "llvm/MC/MCAsmBackend.h"
  9. #include "llvm/ADT/None.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/MC/MCELFObjectWriter.h"
  12. #include "llvm/MC/MCFixupKindInfo.h"
  13. #include "llvm/MC/MCMachObjectWriter.h"
  14. #include "llvm/MC/MCObjectWriter.h"
  15. #include "llvm/MC/MCWasmObjectWriter.h"
  16. #include "llvm/MC/MCWinCOFFObjectWriter.h"
  17. #include "llvm/MC/MCXCOFFObjectWriter.h"
  18. #include <cassert>
  19. #include <cstddef>
  20. #include <cstdint>
  21. using namespace llvm;
  22. MCAsmBackend::MCAsmBackend(support::endianness Endian) : Endian(Endian) {}
  23. MCAsmBackend::~MCAsmBackend() = default;
  24. std::unique_ptr<MCObjectWriter>
  25. MCAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
  26. auto TW = createObjectTargetWriter();
  27. switch (TW->getFormat()) {
  28. case Triple::ELF:
  29. return createELFObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)), OS,
  30. Endian == support::little);
  31. case Triple::MachO:
  32. return createMachObjectWriter(cast<MCMachObjectTargetWriter>(std::move(TW)),
  33. OS, Endian == support::little);
  34. case Triple::COFF:
  35. return createWinCOFFObjectWriter(
  36. cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS);
  37. case Triple::Wasm:
  38. return createWasmObjectWriter(cast<MCWasmObjectTargetWriter>(std::move(TW)),
  39. OS);
  40. case Triple::XCOFF:
  41. return createXCOFFObjectWriter(
  42. cast<MCXCOFFObjectTargetWriter>(std::move(TW)), OS);
  43. default:
  44. llvm_unreachable("unexpected object format");
  45. }
  46. }
  47. std::unique_ptr<MCObjectWriter>
  48. MCAsmBackend::createDwoObjectWriter(raw_pwrite_stream &OS,
  49. raw_pwrite_stream &DwoOS) const {
  50. auto TW = createObjectTargetWriter();
  51. switch (TW->getFormat()) {
  52. case Triple::ELF:
  53. return createELFDwoObjectWriter(
  54. cast<MCELFObjectTargetWriter>(std::move(TW)), OS, DwoOS,
  55. Endian == support::little);
  56. case Triple::Wasm:
  57. return createWasmDwoObjectWriter(
  58. cast<MCWasmObjectTargetWriter>(std::move(TW)), OS, DwoOS);
  59. default:
  60. report_fatal_error("dwo only supported with ELF and Wasm");
  61. }
  62. }
  63. Optional<MCFixupKind> MCAsmBackend::getFixupKind(StringRef Name) const {
  64. return None;
  65. }
  66. const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
  67. static const MCFixupKindInfo Builtins[] = {
  68. {"FK_NONE", 0, 0, 0},
  69. {"FK_Data_1", 0, 8, 0},
  70. {"FK_Data_2", 0, 16, 0},
  71. {"FK_Data_4", 0, 32, 0},
  72. {"FK_Data_8", 0, 64, 0},
  73. {"FK_Data_6b", 0, 6, 0},
  74. {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel},
  75. {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
  76. {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
  77. {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel},
  78. {"FK_GPRel_1", 0, 8, 0},
  79. {"FK_GPRel_2", 0, 16, 0},
  80. {"FK_GPRel_4", 0, 32, 0},
  81. {"FK_GPRel_8", 0, 64, 0},
  82. {"FK_DTPRel_4", 0, 32, 0},
  83. {"FK_DTPRel_8", 0, 64, 0},
  84. {"FK_TPRel_4", 0, 32, 0},
  85. {"FK_TPRel_8", 0, 64, 0},
  86. {"FK_SecRel_1", 0, 8, 0},
  87. {"FK_SecRel_2", 0, 16, 0},
  88. {"FK_SecRel_4", 0, 32, 0},
  89. {"FK_SecRel_8", 0, 64, 0},
  90. {"FK_Data_Add_1", 0, 8, 0},
  91. {"FK_Data_Add_2", 0, 16, 0},
  92. {"FK_Data_Add_4", 0, 32, 0},
  93. {"FK_Data_Add_8", 0, 64, 0},
  94. {"FK_Data_Add_6b", 0, 6, 0},
  95. {"FK_Data_Sub_1", 0, 8, 0},
  96. {"FK_Data_Sub_2", 0, 16, 0},
  97. {"FK_Data_Sub_4", 0, 32, 0},
  98. {"FK_Data_Sub_8", 0, 64, 0},
  99. {"FK_Data_Sub_6b", 0, 6, 0}};
  100. assert((size_t)Kind <= array_lengthof(Builtins) && "Unknown fixup kind");
  101. return Builtins[Kind];
  102. }
  103. bool MCAsmBackend::fixupNeedsRelaxationAdvanced(
  104. const MCFixup &Fixup, bool Resolved, uint64_t Value,
  105. const MCRelaxableFragment *DF, const MCAsmLayout &Layout,
  106. const bool WasForced) const {
  107. if (!Resolved)
  108. return true;
  109. return fixupNeedsRelaxation(Fixup, Value, DF, Layout);
  110. }