XCoreTargetObjectFile.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===-- XCoreTargetObjectFile.cpp - XCore object files --------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "XCoreTargetObjectFile.h"
  10. #include "XCoreSubtarget.h"
  11. #include "llvm/IR/DataLayout.h"
  12. #include "llvm/MC/MCContext.h"
  13. #include "llvm/MC/MCSectionELF.h"
  14. #include "llvm/Support/ELF.h"
  15. #include "llvm/Target/TargetMachine.h"
  16. using namespace llvm;
  17. void XCoreTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM){
  18. TargetLoweringObjectFileELF::Initialize(Ctx, TM);
  19. BSSSection = Ctx.getELFSection(".dp.bss", ELF::SHT_NOBITS,
  20. ELF::SHF_ALLOC | ELF::SHF_WRITE |
  21. ELF::XCORE_SHF_DP_SECTION);
  22. BSSSectionLarge = Ctx.getELFSection(".dp.bss.large", ELF::SHT_NOBITS,
  23. ELF::SHF_ALLOC | ELF::SHF_WRITE |
  24. ELF::XCORE_SHF_DP_SECTION);
  25. DataSection = Ctx.getELFSection(".dp.data", ELF::SHT_PROGBITS,
  26. ELF::SHF_ALLOC | ELF::SHF_WRITE |
  27. ELF::XCORE_SHF_DP_SECTION);
  28. DataSectionLarge = Ctx.getELFSection(".dp.data.large", ELF::SHT_PROGBITS,
  29. ELF::SHF_ALLOC | ELF::SHF_WRITE |
  30. ELF::XCORE_SHF_DP_SECTION);
  31. DataRelROSection = Ctx.getELFSection(".dp.rodata", ELF::SHT_PROGBITS,
  32. ELF::SHF_ALLOC | ELF::SHF_WRITE |
  33. ELF::XCORE_SHF_DP_SECTION);
  34. DataRelROSectionLarge = Ctx.getELFSection(
  35. ".dp.rodata.large", ELF::SHT_PROGBITS,
  36. ELF::SHF_ALLOC | ELF::SHF_WRITE | ELF::XCORE_SHF_DP_SECTION);
  37. ReadOnlySection =
  38. Ctx.getELFSection(".cp.rodata", ELF::SHT_PROGBITS,
  39. ELF::SHF_ALLOC | ELF::XCORE_SHF_CP_SECTION);
  40. ReadOnlySectionLarge =
  41. Ctx.getELFSection(".cp.rodata.large", ELF::SHT_PROGBITS,
  42. ELF::SHF_ALLOC | ELF::XCORE_SHF_CP_SECTION);
  43. MergeableConst4Section = Ctx.getELFSection(
  44. ".cp.rodata.cst4", ELF::SHT_PROGBITS,
  45. ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::XCORE_SHF_CP_SECTION, 4, "");
  46. MergeableConst8Section = Ctx.getELFSection(
  47. ".cp.rodata.cst8", ELF::SHT_PROGBITS,
  48. ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::XCORE_SHF_CP_SECTION, 8, "");
  49. MergeableConst16Section = Ctx.getELFSection(
  50. ".cp.rodata.cst16", ELF::SHT_PROGBITS,
  51. ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::XCORE_SHF_CP_SECTION, 16, "");
  52. CStringSection =
  53. Ctx.getELFSection(".cp.rodata.string", ELF::SHT_PROGBITS,
  54. ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS |
  55. ELF::XCORE_SHF_CP_SECTION);
  56. // TextSection - see MObjectFileInfo.cpp
  57. // StaticCtorSection - see MObjectFileInfo.cpp
  58. // StaticDtorSection - see MObjectFileInfo.cpp
  59. }
  60. static unsigned getXCoreSectionType(SectionKind K) {
  61. if (K.isBSS())
  62. return ELF::SHT_NOBITS;
  63. return ELF::SHT_PROGBITS;
  64. }
  65. static unsigned getXCoreSectionFlags(SectionKind K, bool IsCPRel) {
  66. unsigned Flags = 0;
  67. if (!K.isMetadata())
  68. Flags |= ELF::SHF_ALLOC;
  69. if (K.isText())
  70. Flags |= ELF::SHF_EXECINSTR;
  71. else if (IsCPRel)
  72. Flags |= ELF::XCORE_SHF_CP_SECTION;
  73. else
  74. Flags |= ELF::XCORE_SHF_DP_SECTION;
  75. if (K.isWriteable())
  76. Flags |= ELF::SHF_WRITE;
  77. if (K.isMergeableCString() || K.isMergeableConst4() ||
  78. K.isMergeableConst8() || K.isMergeableConst16())
  79. Flags |= ELF::SHF_MERGE;
  80. if (K.isMergeableCString())
  81. Flags |= ELF::SHF_STRINGS;
  82. return Flags;
  83. }
  84. MCSection *XCoreTargetObjectFile::getExplicitSectionGlobal(
  85. const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
  86. StringRef SectionName = GV->getSection();
  87. // Infer section flags from the section name if we can.
  88. bool IsCPRel = SectionName.startswith(".cp.");
  89. if (IsCPRel && !Kind.isReadOnly())
  90. report_fatal_error("Using .cp. section for writeable object.");
  91. return getContext().getELFSection(SectionName, getXCoreSectionType(Kind),
  92. getXCoreSectionFlags(Kind, IsCPRel));
  93. }
  94. MCSection *XCoreTargetObjectFile::SelectSectionForGlobal(
  95. const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
  96. bool UseCPRel = GV->isLocalLinkage(GV->getLinkage());
  97. if (Kind.isText()) return TextSection;
  98. if (UseCPRel) {
  99. if (Kind.isMergeable1ByteCString()) return CStringSection;
  100. if (Kind.isMergeableConst4()) return MergeableConst4Section;
  101. if (Kind.isMergeableConst8()) return MergeableConst8Section;
  102. if (Kind.isMergeableConst16()) return MergeableConst16Section;
  103. }
  104. Type *ObjType = GV->getValueType();
  105. auto &DL = GV->getParent()->getDataLayout();
  106. if (TM.getCodeModel() == CodeModel::Small || !ObjType->isSized() ||
  107. DL.getTypeAllocSize(ObjType) < CodeModelLargeSize) {
  108. if (Kind.isReadOnly()) return UseCPRel? ReadOnlySection
  109. : DataRelROSection;
  110. if (Kind.isBSS() || Kind.isCommon())return BSSSection;
  111. if (Kind.isData())
  112. return DataSection;
  113. if (Kind.isReadOnlyWithRel()) return DataRelROSection;
  114. } else {
  115. if (Kind.isReadOnly()) return UseCPRel? ReadOnlySectionLarge
  116. : DataRelROSectionLarge;
  117. if (Kind.isBSS() || Kind.isCommon())return BSSSectionLarge;
  118. if (Kind.isData())
  119. return DataSectionLarge;
  120. if (Kind.isReadOnlyWithRel()) return DataRelROSectionLarge;
  121. }
  122. assert((Kind.isThreadLocal() || Kind.isCommon()) && "Unknown section kind");
  123. report_fatal_error("Target does not support TLS or Common sections");
  124. }
  125. MCSection *XCoreTargetObjectFile::getSectionForConstant(const DataLayout &DL,
  126. SectionKind Kind,
  127. const Constant *C,
  128. unsigned &Align) const {
  129. if (Kind.isMergeableConst4()) return MergeableConst4Section;
  130. if (Kind.isMergeableConst8()) return MergeableConst8Section;
  131. if (Kind.isMergeableConst16()) return MergeableConst16Section;
  132. assert((Kind.isReadOnly() || Kind.isReadOnlyWithRel()) &&
  133. "Unknown section kind");
  134. // We assume the size of the object is never greater than CodeModelLargeSize.
  135. // To handle CodeModelLargeSize changes to AsmPrinter would be required.
  136. return ReadOnlySection;
  137. }