ARMTargetObjectFile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===-- llvm/Target/ARMTargetObjectFile.cpp - ARM Object Info Impl --------===//
  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 "ARMTargetObjectFile.h"
  9. #include "ARMSubtarget.h"
  10. #include "ARMTargetMachine.h"
  11. #include "llvm/BinaryFormat/Dwarf.h"
  12. #include "llvm/BinaryFormat/ELF.h"
  13. #include "llvm/MC/MCAsmInfo.h"
  14. #include "llvm/MC/MCContext.h"
  15. #include "llvm/MC/MCExpr.h"
  16. #include "llvm/MC/MCSectionELF.h"
  17. #include "llvm/MC/MCTargetOptions.h"
  18. #include "llvm/MC/SectionKind.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. #include <cassert>
  21. using namespace llvm;
  22. using namespace dwarf;
  23. //===----------------------------------------------------------------------===//
  24. // ELF Target
  25. //===----------------------------------------------------------------------===//
  26. void ARMElfTargetObjectFile::Initialize(MCContext &Ctx,
  27. const TargetMachine &TM) {
  28. const ARMBaseTargetMachine &ARM_TM = static_cast<const ARMBaseTargetMachine &>(TM);
  29. bool isAAPCS_ABI = ARM_TM.TargetABI == ARMBaseTargetMachine::ARMABI::ARM_ABI_AAPCS;
  30. bool genExecuteOnly =
  31. ARM_TM.getMCSubtargetInfo()->hasFeature(ARM::FeatureExecuteOnly);
  32. TargetLoweringObjectFileELF::Initialize(Ctx, TM);
  33. InitializeELF(isAAPCS_ABI);
  34. if (isAAPCS_ABI) {
  35. LSDASection = nullptr;
  36. }
  37. // Make code section unreadable when in execute-only mode
  38. if (genExecuteOnly) {
  39. unsigned Type = ELF::SHT_PROGBITS;
  40. unsigned Flags =
  41. ELF::SHF_EXECINSTR | ELF::SHF_ALLOC | ELF::SHF_ARM_PURECODE;
  42. // Since we cannot modify flags for an existing section, we create a new
  43. // section with the right flags, and use 0 as the unique ID for
  44. // execute-only text
  45. TextSection =
  46. Ctx.getELFSection(".text", Type, Flags, 0, "", false, 0U, nullptr);
  47. }
  48. }
  49. MCRegister ARMElfTargetObjectFile::getStaticBase() const { return ARM::R9; }
  50. const MCExpr *ARMElfTargetObjectFile::
  51. getIndirectSymViaRWPI(const MCSymbol *Sym) const {
  52. return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_ARM_SBREL,
  53. getContext());
  54. }
  55. const MCExpr *ARMElfTargetObjectFile::getTTypeGlobalReference(
  56. const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
  57. MachineModuleInfo *MMI, MCStreamer &Streamer) const {
  58. if (TM.getMCAsmInfo()->getExceptionHandlingType() != ExceptionHandling::ARM)
  59. return TargetLoweringObjectFileELF::getTTypeGlobalReference(
  60. GV, Encoding, TM, MMI, Streamer);
  61. assert(Encoding == DW_EH_PE_absptr && "Can handle absptr encoding only");
  62. return MCSymbolRefExpr::create(TM.getSymbol(GV),
  63. MCSymbolRefExpr::VK_ARM_TARGET2, getContext());
  64. }
  65. const MCExpr *ARMElfTargetObjectFile::
  66. getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
  67. return MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_ARM_TLSLDO,
  68. getContext());
  69. }
  70. static bool isExecuteOnlyFunction(const GlobalObject *GO, SectionKind SK,
  71. const TargetMachine &TM) {
  72. if (const Function *F = dyn_cast<Function>(GO))
  73. if (TM.getSubtarget<ARMSubtarget>(*F).genExecuteOnly() && SK.isText())
  74. return true;
  75. return false;
  76. }
  77. MCSection *ARMElfTargetObjectFile::getExplicitSectionGlobal(
  78. const GlobalObject *GO, SectionKind SK, const TargetMachine &TM) const {
  79. // Set execute-only access for the explicit section
  80. if (isExecuteOnlyFunction(GO, SK, TM))
  81. SK = SectionKind::getExecuteOnly();
  82. return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, SK, TM);
  83. }
  84. MCSection *ARMElfTargetObjectFile::SelectSectionForGlobal(
  85. const GlobalObject *GO, SectionKind SK, const TargetMachine &TM) const {
  86. // Place the global in the execute-only text section
  87. if (isExecuteOnlyFunction(GO, SK, TM))
  88. SK = SectionKind::getExecuteOnly();
  89. return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, SK, TM);
  90. }