AArch64MCExpr.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===-- AArch64MCExpr.cpp - AArch64 specific MC expression classes --------===//
  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. //
  9. // This file contains the implementation of the assembly expression modifiers
  10. // accepted by the AArch64 architecture (e.g. ":lo12:", ":gottprel_g1:", ...).
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "AArch64MCExpr.h"
  14. #include "llvm/BinaryFormat/ELF.h"
  15. #include "llvm/MC/MCContext.h"
  16. #include "llvm/MC/MCStreamer.h"
  17. #include "llvm/MC/MCSymbolELF.h"
  18. #include "llvm/MC/MCValue.h"
  19. #include "llvm/Support/Casting.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "aarch64symbolrefexpr"
  23. const AArch64MCExpr *AArch64MCExpr::create(const MCExpr *Expr, VariantKind Kind,
  24. MCContext &Ctx) {
  25. return new (Ctx) AArch64MCExpr(Expr, Kind);
  26. }
  27. StringRef AArch64MCExpr::getVariantKindName() const {
  28. switch (static_cast<uint32_t>(getKind())) {
  29. case VK_CALL: return "";
  30. case VK_LO12: return ":lo12:";
  31. case VK_ABS_G3: return ":abs_g3:";
  32. case VK_ABS_G2: return ":abs_g2:";
  33. case VK_ABS_G2_S: return ":abs_g2_s:";
  34. case VK_ABS_G2_NC: return ":abs_g2_nc:";
  35. case VK_ABS_G1: return ":abs_g1:";
  36. case VK_ABS_G1_S: return ":abs_g1_s:";
  37. case VK_ABS_G1_NC: return ":abs_g1_nc:";
  38. case VK_ABS_G0: return ":abs_g0:";
  39. case VK_ABS_G0_S: return ":abs_g0_s:";
  40. case VK_ABS_G0_NC: return ":abs_g0_nc:";
  41. case VK_PREL_G3: return ":prel_g3:";
  42. case VK_PREL_G2: return ":prel_g2:";
  43. case VK_PREL_G2_NC: return ":prel_g2_nc:";
  44. case VK_PREL_G1: return ":prel_g1:";
  45. case VK_PREL_G1_NC: return ":prel_g1_nc:";
  46. case VK_PREL_G0: return ":prel_g0:";
  47. case VK_PREL_G0_NC: return ":prel_g0_nc:";
  48. case VK_DTPREL_G2: return ":dtprel_g2:";
  49. case VK_DTPREL_G1: return ":dtprel_g1:";
  50. case VK_DTPREL_G1_NC: return ":dtprel_g1_nc:";
  51. case VK_DTPREL_G0: return ":dtprel_g0:";
  52. case VK_DTPREL_G0_NC: return ":dtprel_g0_nc:";
  53. case VK_DTPREL_HI12: return ":dtprel_hi12:";
  54. case VK_DTPREL_LO12: return ":dtprel_lo12:";
  55. case VK_DTPREL_LO12_NC: return ":dtprel_lo12_nc:";
  56. case VK_TPREL_G2: return ":tprel_g2:";
  57. case VK_TPREL_G1: return ":tprel_g1:";
  58. case VK_TPREL_G1_NC: return ":tprel_g1_nc:";
  59. case VK_TPREL_G0: return ":tprel_g0:";
  60. case VK_TPREL_G0_NC: return ":tprel_g0_nc:";
  61. case VK_TPREL_HI12: return ":tprel_hi12:";
  62. case VK_TPREL_LO12: return ":tprel_lo12:";
  63. case VK_TPREL_LO12_NC: return ":tprel_lo12_nc:";
  64. case VK_TLSDESC_LO12: return ":tlsdesc_lo12:";
  65. case VK_ABS_PAGE: return "";
  66. case VK_ABS_PAGE_NC: return ":pg_hi21_nc:";
  67. case VK_GOT: return ":got:";
  68. case VK_GOT_PAGE: return ":got:";
  69. case VK_GOT_PAGE_LO15: return ":gotpage_lo15:";
  70. case VK_GOT_LO12: return ":got_lo12:";
  71. case VK_GOTTPREL: return ":gottprel:";
  72. case VK_GOTTPREL_PAGE: return ":gottprel:";
  73. case VK_GOTTPREL_LO12_NC: return ":gottprel_lo12:";
  74. case VK_GOTTPREL_G1: return ":gottprel_g1:";
  75. case VK_GOTTPREL_G0_NC: return ":gottprel_g0_nc:";
  76. case VK_TLSDESC: return "";
  77. case VK_TLSDESC_PAGE: return ":tlsdesc:";
  78. case VK_SECREL_LO12: return ":secrel_lo12:";
  79. case VK_SECREL_HI12: return ":secrel_hi12:";
  80. default:
  81. llvm_unreachable("Invalid ELF symbol kind");
  82. }
  83. }
  84. void AArch64MCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
  85. OS << getVariantKindName();
  86. Expr->print(OS, MAI);
  87. }
  88. void AArch64MCExpr::visitUsedExpr(MCStreamer &Streamer) const {
  89. Streamer.visitUsedExpr(*getSubExpr());
  90. }
  91. MCFragment *AArch64MCExpr::findAssociatedFragment() const {
  92. llvm_unreachable("FIXME: what goes here?");
  93. }
  94. bool AArch64MCExpr::evaluateAsRelocatableImpl(MCValue &Res,
  95. const MCAsmLayout *Layout,
  96. const MCFixup *Fixup) const {
  97. if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
  98. return false;
  99. Res =
  100. MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
  101. return true;
  102. }
  103. static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
  104. switch (Expr->getKind()) {
  105. case MCExpr::Target:
  106. llvm_unreachable("Can't handle nested target expression");
  107. break;
  108. case MCExpr::Constant:
  109. break;
  110. case MCExpr::Binary: {
  111. const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
  112. fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
  113. fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
  114. break;
  115. }
  116. case MCExpr::SymbolRef: {
  117. // We're known to be under a TLS fixup, so any symbol should be
  118. // modified. There should be only one.
  119. const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
  120. cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
  121. break;
  122. }
  123. case MCExpr::Unary:
  124. fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
  125. break;
  126. }
  127. }
  128. void AArch64MCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
  129. switch (getSymbolLoc(Kind)) {
  130. default:
  131. return;
  132. case VK_DTPREL:
  133. case VK_GOTTPREL:
  134. case VK_TPREL:
  135. case VK_TLSDESC:
  136. break;
  137. }
  138. fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
  139. }