RISCVTargetObjectFile.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===-- RISCVTargetObjectFile.cpp - RISCV Object Info -----------------===//
  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 "RISCVTargetObjectFile.h"
  9. #include "RISCVTargetMachine.h"
  10. #include "llvm/BinaryFormat/ELF.h"
  11. #include "llvm/MC/MCContext.h"
  12. #include "llvm/MC/MCSectionELF.h"
  13. using namespace llvm;
  14. void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
  15. const TargetMachine &TM) {
  16. TargetLoweringObjectFileELF::Initialize(Ctx, TM);
  17. SmallDataSection = getContext().getELFSection(
  18. ".sdata", ELF::SHT_PROGBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
  19. SmallBSSSection = getContext().getELFSection(".sbss", ELF::SHT_NOBITS,
  20. ELF::SHF_WRITE | ELF::SHF_ALLOC);
  21. }
  22. // A address must be loaded from a small section if its size is less than the
  23. // small section size threshold. Data in this section could be addressed by
  24. // using gp_rel operator.
  25. bool RISCVELFTargetObjectFile::isInSmallSection(uint64_t Size) const {
  26. // gcc has traditionally not treated zero-sized objects as small data, so this
  27. // is effectively part of the ABI.
  28. return Size > 0 && Size <= SSThreshold;
  29. }
  30. // Return true if this global address should be placed into small data/bss
  31. // section.
  32. bool RISCVELFTargetObjectFile::isGlobalInSmallSection(
  33. const GlobalObject *GO, const TargetMachine &TM) const {
  34. // Only global variables, not functions.
  35. const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
  36. if (!GVA)
  37. return false;
  38. // If the variable has an explicit section, it is placed in that section.
  39. if (GVA->hasSection()) {
  40. StringRef Section = GVA->getSection();
  41. // Explicitly placing any variable in the small data section overrides
  42. // the global -G value.
  43. if (Section == ".sdata" || Section == ".sbss")
  44. return true;
  45. // Otherwise reject putting the variable to small section if it has an
  46. // explicit section name.
  47. return false;
  48. }
  49. if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
  50. GVA->hasCommonLinkage()))
  51. return false;
  52. Type *Ty = GVA->getValueType();
  53. // It is possible that the type of the global is unsized, i.e. a declaration
  54. // of a extern struct. In this case don't presume it is in the small data
  55. // section. This happens e.g. when building the FreeBSD kernel.
  56. if (!Ty->isSized())
  57. return false;
  58. return isInSmallSection(
  59. GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
  60. }
  61. MCSection *RISCVELFTargetObjectFile::SelectSectionForGlobal(
  62. const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
  63. // Handle Small Section classification here.
  64. if (Kind.isBSS() && isGlobalInSmallSection(GO, TM))
  65. return SmallBSSSection;
  66. if (Kind.isData() && isGlobalInSmallSection(GO, TM))
  67. return SmallDataSection;
  68. // Otherwise, we work the same as ELF.
  69. return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
  70. }
  71. void RISCVELFTargetObjectFile::getModuleMetadata(Module &M) {
  72. TargetLoweringObjectFileELF::getModuleMetadata(M);
  73. SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
  74. M.getModuleFlagsMetadata(ModuleFlags);
  75. for (const auto &MFE : ModuleFlags) {
  76. StringRef Key = MFE.Key->getString();
  77. if (Key == "SmallDataLimit") {
  78. SSThreshold = mdconst::extract<ConstantInt>(MFE.Val)->getZExtValue();
  79. break;
  80. }
  81. }
  82. }
  83. /// Return true if this constant should be placed into small data section.
  84. bool RISCVELFTargetObjectFile::isConstantInSmallSection(
  85. const DataLayout &DL, const Constant *CN) const {
  86. return isInSmallSection(DL.getTypeAllocSize(CN->getType()));
  87. }
  88. MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
  89. const DataLayout &DL, SectionKind Kind, const Constant *C,
  90. Align &Alignment) const {
  91. if (isConstantInSmallSection(DL, C))
  92. return SmallDataSection;
  93. // Otherwise, we work the same as ELF.
  94. return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
  95. Alignment);
  96. }