llvm.org GIT mirror llvm / 80e2a2f
Target: Change various section classifiers in TargetLoweringObjectFile to take a GlobalObject. These functions are about classifying a global which will actually be emitted, so it does not make sense for them to take a GlobalValue which may for example be an alias. Change the Mach-O object writer and the Hexagon, Lanai and MIPS backends to look through aliases before using TargetLoweringObjectFile interfaces. These are functional changes but all appear to be bug fixes. Differential Revision: https://reviews.llvm.org/D25917 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285006 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 2 years ago
25 changed file(s) with 209 addition(s) and 185 deletion(s). Raw diff Collapse all Expand all
5252 const Constant *C,
5353 unsigned &Align) const override;
5454
55 MCSection *getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
55 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
5656 const TargetMachine &TM) const override;
5757
58 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
58 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
5959 const TargetMachine &TM) const override;
6060
6161 MCSection *getSectionForJumpTable(const Function &F,
102102 ArrayRef ModuleFlags,
103103 const TargetMachine &TM) const override;
104104
105 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
105 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
106106 const TargetMachine &TM) const override;
107107
108 MCSection *getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
108 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
109109 const TargetMachine &TM) const override;
110110
111111 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
143143 ~TargetLoweringObjectFileCOFF() override {}
144144
145145 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
146 MCSection *getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
146 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
147147 const TargetMachine &TM) const override;
148148
149 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
149 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
150150 const TargetMachine &TM) const override;
151151
152152 void getNameWithPrefix(SmallVectorImpl &OutName, const GlobalValue *GV,
2525 namespace llvm {
2626
2727 class Comdat;
28 class GlobalObject;
2829 class PointerType;
2930 class Module;
3031
491492 // increased.
492493 bool canIncreaseAlignment() const;
493494
495 const GlobalObject *getBaseObject() const {
496 return const_cast(this)->getBaseObject();
497 }
498 GlobalObject *getBaseObject();
499
494500 /// This method unlinks 'this' from the containing module, but does not delete
495501 /// it.
496502 virtual void removeFromParent() = 0;
8585
8686 /// Classify the specified global variable into a set of target independent
8787 /// categories embodied in SectionKind.
88 static SectionKind getKindForGlobal(const GlobalValue *GV,
88 static SectionKind getKindForGlobal(const GlobalObject *GO,
8989 const TargetMachine &TM);
9090
9191 /// This method computes the appropriate section to emit the specified global
9292 /// variable or function definition. This should not be passed external (or
9393 /// available externally) globals.
94 MCSection *SectionForGlobal(const GlobalValue *GV, SectionKind Kind,
94 MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind,
9595 const TargetMachine &TM) const;
9696
9797 /// This method computes the appropriate section to emit the specified global
9898 /// variable or function definition. This should not be passed external (or
9999 /// available externally) globals.
100 MCSection *SectionForGlobal(const GlobalValue *GV,
100 MCSection *SectionForGlobal(const GlobalObject *GO,
101101 const TargetMachine &TM) const {
102 return SectionForGlobal(GV, getKindForGlobal(GV, TM), TM);
102 return SectionForGlobal(GO, getKindForGlobal(GO, TM), TM);
103103 }
104104
105105 virtual void getNameWithPrefix(SmallVectorImpl &OutName,
114114
115115 /// Targets should implement this method to assign a section to globals with
116116 /// an explicit section specfied. The implementation of this method can
117 /// assume that GV->hasSection() is true.
117 /// assume that GO->hasSection() is true.
118118 virtual MCSection *
119 getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
119 getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
120120 const TargetMachine &TM) const = 0;
121121
122122 /// Return an MCExpr to use for a reference to the specified global variable
186186 const GlobalValue *GV) const {}
187187
188188 protected:
189 virtual MCSection *SelectSectionForGlobal(const GlobalValue *GV,
189 virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO,
190190 SectionKind Kind,
191191 const TargetMachine &TM) const = 0;
192192 };
208208 }
209209
210210 MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
211 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
212 StringRef SectionName = GV->getSection();
211 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
212 StringRef SectionName = GO->getSection();
213213
214214 // Infer section flags from the section name if we can.
215215 Kind = getELFKindForNamedSection(SectionName, Kind);
216216
217217 StringRef Group = "";
218218 unsigned Flags = getELFSectionFlags(Kind);
219 if (const Comdat *C = getELFComdat(GV)) {
219 if (const Comdat *C = getELFComdat(GO)) {
220220 Group = C->getName();
221221 Flags |= ELF::SHF_GROUP;
222222 }
245245 }
246246
247247 static MCSectionELF *
248 selectELFSectionForGlobal(MCContext &Ctx, const GlobalValue *GV,
248 selectELFSectionForGlobal(MCContext &Ctx, const GlobalObject *GO,
249249 SectionKind Kind, Mangler &Mang,
250250 const TargetMachine &TM, bool EmitUniqueSection,
251251 unsigned Flags, unsigned *NextUniqueID) {
273273 }
274274
275275 StringRef Group = "";
276 if (const Comdat *C = getELFComdat(GV)) {
276 if (const Comdat *C = getELFComdat(GO)) {
277277 Flags |= ELF::SHF_GROUP;
278278 Group = C->getName();
279279 }
284284 // We also need alignment here.
285285 // FIXME: this is getting the alignment of the character, not the
286286 // alignment of the global!
287 unsigned Align = GV->getParent()->getDataLayout().getPreferredAlignment(
288 cast(GV));
287 unsigned Align = GO->getParent()->getDataLayout().getPreferredAlignment(
288 cast(GO));
289289
290290 std::string SizeSpec = ".rodata.str" + utostr(EntrySize) + ".";
291291 Name = SizeSpec + utostr(Align);
296296 Name = getSectionPrefixForGlobal(Kind);
297297 }
298298
299 if (const Function *F = dyn_cast(GV)) {
299 if (const auto *F = dyn_cast(GO)) {
300300 const auto &OptionalPrefix = F->getSectionPrefix();
301301 if (OptionalPrefix)
302302 Name += *OptionalPrefix;
304304
305305 if (EmitUniqueSection && UniqueSectionNames) {
306306 Name.push_back('.');
307 TM.getNameWithPrefix(Name, GV, Mang, true);
307 TM.getNameWithPrefix(Name, GO, Mang, true);
308308 }
309309 unsigned UniqueID = MCContext::GenericSectionID;
310310 if (EmitUniqueSection && !UniqueSectionNames) {
316316 }
317317
318318 MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
319 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
319 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
320320 unsigned Flags = getELFSectionFlags(Kind);
321321
322322 // If we have -ffunction-section or -fdata-section then we should emit the
328328 else
329329 EmitUniqueSection = TM.getDataSections();
330330 }
331 EmitUniqueSection |= GV->hasComdat();
332
333 return selectELFSectionForGlobal(getContext(), GV, Kind, getMangler(), TM,
331 EmitUniqueSection |= GO->hasComdat();
332
333 return selectELFSectionForGlobal(getContext(), GO, Kind, getMangler(), TM,
334334 EmitUniqueSection, Flags, &NextUniqueID);
335335 }
336336
571571 }
572572
573573 MCSection *TargetLoweringObjectFileMachO::getExplicitSectionGlobal(
574 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
574 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
575575 // Parse the section specifier and create it if valid.
576576 StringRef Segment, Section;
577577 unsigned TAA = 0, StubSize = 0;
578578 bool TAAParsed;
579579
580 checkMachOComdat(GV);
580 checkMachOComdat(GO);
581581
582582 std::string ErrorCode =
583 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
583 MCSectionMachO::ParseSectionSpecifier(GO->getSection(), Segment, Section,
584584 TAA, TAAParsed, StubSize);
585585 if (!ErrorCode.empty()) {
586586 // If invalid, report the error with report_fatal_error.
587 report_fatal_error("Global variable '" + GV->getName() +
587 report_fatal_error("Global variable '" + GO->getName() +
588588 "' has an invalid section specifier '" +
589 GV->getSection() + "': " + ErrorCode + ".");
589 GO->getSection() + "': " + ErrorCode + ".");
590590 }
591591
592592 // Get the section.
603603 // to reject it here.
604604 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
605605 // If invalid, report the error with report_fatal_error.
606 report_fatal_error("Global variable '" + GV->getName() +
606 report_fatal_error("Global variable '" + GO->getName() +
607607 "' section type or attributes does not match previous"
608608 " section specifier");
609609 }
612612 }
613613
614614 MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
615 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
616 checkMachOComdat(GV);
615 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
616 checkMachOComdat(GO);
617617
618618 // Handle thread local data.
619619 if (Kind.isThreadBSS()) return TLSBSSSection;
620620 if (Kind.isThreadData()) return TLSDataSection;
621621
622622 if (Kind.isText())
623 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
623 return GO->isWeakForLinker() ? TextCoalSection : TextSection;
624624
625625 // If this is weak/linkonce, put this in a coalescable section, either in text
626626 // or data depending on if it is writable.
627 if (GV->isWeakForLinker()) {
627 if (GO->isWeakForLinker()) {
628628 if (Kind.isReadOnly())
629629 return ConstTextCoalSection;
630630 return DataCoalSection;
632632
633633 // FIXME: Alignment check should be handled by section classifier.
634634 if (Kind.isMergeable1ByteCString() &&
635 GV->getParent()->getDataLayout().getPreferredAlignment(
636 cast(GV)) < 32)
635 GO->getParent()->getDataLayout().getPreferredAlignment(
636 cast(GO)) < 32)
637637 return CStringSection;
638638
639639 // Do not put 16-bit arrays in the UString section if they have an
640640 // externally visible label, this runs into issues with certain linker
641641 // versions.
642 if (Kind.isMergeable2ByteCString() && !GV->hasExternalLinkage() &&
643 GV->getParent()->getDataLayout().getPreferredAlignment(
644 cast(GV)) < 32)
642 if (Kind.isMergeable2ByteCString() && !GO->hasExternalLinkage() &&
643 GO->getParent()->getDataLayout().getPreferredAlignment(
644 cast(GO)) < 32)
645645 return UStringSection;
646646
647647 // With MachO only variables whose corresponding symbol starts with 'l' or
648648 // 'L' can be merged, so we only try merging GVs with private linkage.
649 if (GV->hasPrivateLinkage() && Kind.isMergeableConst()) {
649 if (GO->hasPrivateLinkage() && Kind.isMergeableConst()) {
650650 if (Kind.isMergeableConst4())
651651 return FourByteConstantSection;
652652 if (Kind.isMergeableConst8())
820820 void TargetLoweringObjectFileMachO::getNameWithPrefix(
821821 SmallVectorImpl &OutName, const GlobalValue *GV,
822822 const TargetMachine &TM) const {
823 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
824 const MCSection *TheSection = SectionForGlobal(GV, GVKind, TM);
825 bool CannotUsePrivateLabel =
826 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
823 bool CannotUsePrivateLabel = true;
824 if (auto *GO = GV->getBaseObject()) {
825 SectionKind GOKind = TargetLoweringObjectFile::getKindForGlobal(GO, TM);
826 const MCSection *TheSection = SectionForGlobal(GO, GOKind, TM);
827 CannotUsePrivateLabel =
828 !canUsePrivateLabel(*TM.getMCAsmInfo(), *TheSection);
829 }
827830 getMangler().getNameWithPrefix(OutName, GV, CannotUsePrivateLabel);
828831 }
829832
911914 }
912915
913916 MCSection *TargetLoweringObjectFileCOFF::getExplicitSectionGlobal(
914 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
917 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
915918 int Selection = 0;
916919 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
917 StringRef Name = GV->getSection();
920 StringRef Name = GO->getSection();
918921 StringRef COMDATSymName = "";
919 if (GV->hasComdat()) {
920 Selection = getSelectionForCOFF(GV);
922 if (GO->hasComdat()) {
923 Selection = getSelectionForCOFF(GO);
921924 const GlobalValue *ComdatGV;
922925 if (Selection == COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE)
923 ComdatGV = getComdatGVForCOFF(GV);
926 ComdatGV = getComdatGVForCOFF(GO);
924927 else
925 ComdatGV = GV;
928 ComdatGV = GO;
926929
927930 if (!ComdatGV->hasPrivateLinkage()) {
928931 MCSymbol *Sym = TM.getSymbol(ComdatGV, getMangler());
950953 }
951954
952955 MCSection *TargetLoweringObjectFileCOFF::SelectSectionForGlobal(
953 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
956 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
954957 // If we have -ffunction-sections then we should emit the global value to a
955958 // uniqued section specifically for it.
956959 bool EmitUniquedSection;
959962 else
960963 EmitUniquedSection = TM.getDataSections();
961964
962 if ((EmitUniquedSection && !Kind.isCommon()) || GV->hasComdat()) {
965 if ((EmitUniquedSection && !Kind.isCommon()) || GO->hasComdat()) {
963966 const char *Name = getCOFFSectionNameForUniqueGlobal(Kind);
964967 unsigned Characteristics = getCOFFSectionFlags(Kind, TM);
965968
966969 Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
967 int Selection = getSelectionForCOFF(GV);
970 int Selection = getSelectionForCOFF(GO);
968971 if (!Selection)
969972 Selection = COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
970973 const GlobalValue *ComdatGV;
971 if (GV->hasComdat())
972 ComdatGV = getComdatGVForCOFF(GV);
974 if (GO->hasComdat())
975 ComdatGV = getComdatGVForCOFF(GO);
973976 else
974 ComdatGV = GV;
977 ComdatGV = GO;
975978
976979 unsigned UniqueID = MCContext::GenericSectionID;
977980 if (EmitUniquedSection)
984987 COMDATSymName, Selection, UniqueID);
985988 } else {
986989 SmallString<256> TmpData;
987 getMangler().getNameWithPrefix(TmpData, GV, /*CannotUsePrivateLabel=*/true);
990 getMangler().getNameWithPrefix(TmpData, GO, /*CannotUsePrivateLabel=*/true);
988991 return getContext().getCOFFSection(Name, Characteristics, Kind, TmpData,
989992 Selection, UniqueID);
990993 }
212212 return true;
213213 }
214214
215 GlobalObject *GlobalValue::getBaseObject() {
216 if (auto *GO = dyn_cast(this))
217 return GO;
218 if (auto *GA = dyn_cast(this))
219 return GA->getBaseObject();
220 return nullptr;
221 }
222
215223 //===----------------------------------------------------------------------===//
216224 // GlobalVariable Implementation
217225 //===----------------------------------------------------------------------===//
2020 //===----------------------------------------------------------------------===//
2121
2222 MCSection *AMDGPUTargetObjectFile::SelectSectionForGlobal(
23 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
24 if (Kind.isReadOnly() && AMDGPU::isReadOnlySegment(GV) &&
23 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
24 if (Kind.isReadOnly() && AMDGPU::isReadOnlySegment(GO) &&
2525 AMDGPU::shouldEmitConstantsToTextSection(TM.getTargetTriple()))
2626 return TextSection;
2727
28 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, TM);
28 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
2929 }
2222
2323 class AMDGPUTargetObjectFile : public TargetLoweringObjectFileELF {
2424 public:
25 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
25 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
2626 const TargetMachine &TM) const override;
2727 };
2828
2525 }
2626
2727 MCSection *
28 AVRTargetObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
28 AVRTargetObjectFile::SelectSectionForGlobal(const GlobalObject *GO,
2929 SectionKind Kind,
3030 const TargetMachine &TM) const {
3131 // Global values in flash memory are placed in the progmem.data section
3232 // unless they already have a user assigned section.
33 if (AVR::isProgramMemoryAddress(GV) && !GV->hasSection())
33 if (AVR::isProgramMemoryAddress(GO) && !GO->hasSection())
3434 return ProgmemDataSection;
3535
3636 // Otherwise, we work the same way as ELF.
37 return Base::SelectSectionForGlobal(GV, Kind, TM);
37 return Base::SelectSectionForGlobal(GO, Kind, TM);
3838 }
3939 } // end of namespace llvm
4040
2020 public:
2121 void Initialize(MCContext &ctx, const TargetMachine &TM) override;
2222
23 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
23 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
2424 const TargetMachine &TM) const override;
2525
2626 private:
15061506
15071507 if (RM == Reloc::Static) {
15081508 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
1509 if (HLOF.isGlobalInSmallSection(GV, HTM))
1509 const GlobalObject *GO = GV->getBaseObject();
1510 if (GO && HLOF.isGlobalInSmallSection(GO, HTM))
15101511 return DAG.getNode(HexagonISD::CONST32_GP, dl, PtrVT, GA);
15111512 return DAG.getNode(HexagonISD::CONST32, dl, PtrVT, GA);
15121513 }
100100 }
101101
102102 MCSection *HexagonTargetObjectFile::SelectSectionForGlobal(
103 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
104 TRACE("[SelectSectionForGlobal] GV(" << GV->getName() << ") ");
105 TRACE("input section(" << GV->getSection() << ") ");
106
107 TRACE((GV->hasPrivateLinkage() ? "private_linkage " : "")
108 << (GV->hasLocalLinkage() ? "local_linkage " : "")
109 << (GV->hasInternalLinkage() ? "internal " : "")
110 << (GV->hasExternalLinkage() ? "external " : "")
111 << (GV->hasCommonLinkage() ? "common_linkage " : "")
112 << (GV->hasCommonLinkage() ? "common " : "" )
103 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
104 TRACE("[SelectSectionForGlobal] GO(" << GO->getName() << ") ");
105 TRACE("input section(" << GO->getSection() << ") ");
106
107 TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
108 << (GO->hasLocalLinkage() ? "local_linkage " : "")
109 << (GO->hasInternalLinkage() ? "internal " : "")
110 << (GO->hasExternalLinkage() ? "external " : "")
111 << (GO->hasCommonLinkage() ? "common_linkage " : "")
112 << (GO->hasCommonLinkage() ? "common " : "" )
113113 << (Kind.isCommon() ? "kind_common " : "" )
114114 << (Kind.isBSS() ? "kind_bss " : "" )
115115 << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
116116
117 if (isGlobalInSmallSection(GV, TM))
118 return selectSmallSectionForGlobal(GV, Kind, TM);
117 if (isGlobalInSmallSection(GO, TM))
118 return selectSmallSectionForGlobal(GO, Kind, TM);
119119
120120 if (Kind.isCommon()) {
121121 // This is purely for LTO+Linker Script because commons don't really have a
127127
128128 TRACE("default_ELF_section\n");
129129 // Otherwise, we work the same as ELF.
130 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, TM);
130 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
131131 }
132132
133133 MCSection *HexagonTargetObjectFile::getExplicitSectionGlobal(
134 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
135 TRACE("[getExplicitSectionGlobal] GV(" << GV->getName() << ") from("
136 << GV->getSection() << ") ");
137 TRACE((GV->hasPrivateLinkage() ? "private_linkage " : "")
138 << (GV->hasLocalLinkage() ? "local_linkage " : "")
139 << (GV->hasInternalLinkage() ? "internal " : "")
140 << (GV->hasExternalLinkage() ? "external " : "")
141 << (GV->hasCommonLinkage() ? "common_linkage " : "")
142 << (GV->hasCommonLinkage() ? "common " : "" )
134 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
135 TRACE("[getExplicitSectionGlobal] GO(" << GO->getName() << ") from("
136 << GO->getSection() << ") ");
137 TRACE((GO->hasPrivateLinkage() ? "private_linkage " : "")
138 << (GO->hasLocalLinkage() ? "local_linkage " : "")
139 << (GO->hasInternalLinkage() ? "internal " : "")
140 << (GO->hasExternalLinkage() ? "external " : "")
141 << (GO->hasCommonLinkage() ? "common_linkage " : "")
142 << (GO->hasCommonLinkage() ? "common " : "" )
143143 << (Kind.isCommon() ? "kind_common " : "" )
144144 << (Kind.isBSS() ? "kind_bss " : "" )
145145 << (Kind.isBSSLocal() ? "kind_bss_local " : "" ));
146146
147 if (GV->hasSection()) {
148 StringRef Section = GV->getSection();
147 if (GO->hasSection()) {
148 StringRef Section = GO->getSection();
149149 if (Section.find(".access.text.group") != StringRef::npos)
150 return getContext().getELFSection(GV->getSection(), ELF::SHT_PROGBITS,
150 return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
151151 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
152152 if (Section.find(".access.data.group") != StringRef::npos)
153 return getContext().getELFSection(GV->getSection(), ELF::SHT_PROGBITS,
153 return getContext().getELFSection(GO->getSection(), ELF::SHT_PROGBITS,
154154 ELF::SHF_WRITE | ELF::SHF_ALLOC);
155155 }
156156
157 if (isGlobalInSmallSection(GV, TM))
158 return selectSmallSectionForGlobal(GV, Kind, TM);
157 if (isGlobalInSmallSection(GO, TM))
158 return selectSmallSectionForGlobal(GO, Kind, TM);
159159
160160 // Otherwise, we work the same as ELF.
161161 TRACE("default_ELF_section\n");
162 return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GV, Kind, TM);
162 return TargetLoweringObjectFileELF::getExplicitSectionGlobal(GO, Kind, TM);
163163 }
164164
165165
166166 /// Return true if this global value should be placed into small data/bss
167167 /// section.
168 bool HexagonTargetObjectFile::isGlobalInSmallSection(const GlobalValue *GV,
168 bool HexagonTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
169169 const TargetMachine &TM) const {
170170 // Only global variables, not functions.
171171 DEBUG(dbgs() << "Checking if value is in small-data, -G"
172 << SmallDataThreshold << ": \"" << GV->getName() << "\": ");
173 const GlobalVariable *GVar = dyn_cast(GV);
172 << SmallDataThreshold << ": \"" << GO->getName() << "\": ");
173 const GlobalVariable *GVar = dyn_cast(GO);
174174 if (!GVar) {
175175 DEBUG(dbgs() << "no, not a global variable\n");
176176 return false;
296296 }
297297
298298 MCSection *HexagonTargetObjectFile::selectSmallSectionForGlobal(
299 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
300 const Type *GTy = GV->getType()->getElementType();
301 unsigned Size = getSmallestAddressableSize(GTy, GV, TM);
299 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
300 const Type *GTy = GO->getType()->getElementType();
301 unsigned Size = getSmallestAddressableSize(GTy, GO, TM);
302302
303303 // If we have -ffunction-section or -fdata-section then we should emit the
304304 // global value to a unique section specifically for it... even for sdata.
324324
325325 if (EmitUniquedSection) {
326326 Name.append(".");
327 Name.append(GV->getName());
327 Name.append(GO->getName());
328328 }
329329 TRACE(" unique sbss(" << Name << ")\n");
330330 return getContext().getELFSection(Name.str(), ELF::SHT_NOBITS,
351351 // case the Kind could be wrong for it.
352352 if (Kind.isMergeableConst()) {
353353 TRACE(" const_object_as_data ");
354 const GlobalVariable *GVar = dyn_cast(GV);
354 const GlobalVariable *GVar = dyn_cast(GO);
355355 if (GVar->hasSection() && isSmallDataSection(GVar->getSection()))
356356 Kind = SectionKind::getData();
357357 }
368368
369369 if (EmitUniquedSection) {
370370 Name.append(".");
371 Name.append(GV->getName());
371 Name.append(GO->getName());
372372 }
373373 TRACE(" unique sdata(" << Name << ")\n");
374374 return getContext().getELFSection(Name.str(), ELF::SHT_PROGBITS,
377377
378378 TRACE("default ELF section\n");
379379 // Otherwise, we work the same as ELF.
380 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, TM);
381 }
380 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
381 }
1818 public:
1919 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
2020
21 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
21 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
2222 const TargetMachine &TM) const override;
2323
24 MCSection *getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
24 MCSection *getExplicitSectionGlobal(const GlobalObject *GO,
25 SectionKind Kind,
2526 const TargetMachine &TM) const override;
2627
27 bool isGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM)
28 const;
28 bool isGlobalInSmallSection(const GlobalObject *GO,
29 const TargetMachine &TM) const;
2930
3031 bool isSmallDataEnabled() const;
3132
3839 unsigned getSmallestAddressableSize(const Type *Ty, const GlobalValue *GV,
3940 const TargetMachine &TM) const;
4041
41 MCSection *selectSmallSectionForGlobal(const GlobalValue *GV,
42 MCSection *selectSmallSectionForGlobal(const GlobalObject *GO,
4243 SectionKind Kind,
4344 const TargetMachine &TM) const;
4445 };
11661166
11671167 // If the code model is small or global variable will be placed in the small
11681168 // section, then assume address will fit in 21-bits.
1169 const GlobalObject *GO = GV->getBaseObject();
11691170 if (getTargetMachine().getCodeModel() == CodeModel::Small ||
1170 TLOF->isGlobalInSmallSection(GV, getTargetMachine())) {
1171 (GO && TLOF->isGlobalInSmallSection(GO, getTargetMachine()))) {
11711172 SDValue Small = DAG.getTargetGlobalAddress(
11721173 GV, DL, getPointerTy(DAG.getDataLayout()), Offset, LanaiII::MO_NO_FLAG);
11731174 return DAG.getNode(ISD::OR, DL, MVT::i32,
4848 // Return true if this global address should be placed into small data/bss
4949 // section.
5050 bool LanaiTargetObjectFile::isGlobalInSmallSection(
51 const GlobalValue *GV, const TargetMachine &TM) const {
51 const GlobalObject *GO, const TargetMachine &TM) const {
5252 // We first check the case where global is a declaration, because finding
5353 // section kind using getKindForGlobal() is only allowed for global
5454 // definitions.
55 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
56 return isGlobalInSmallSectionImpl(GV, TM);
55 if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
56 return isGlobalInSmallSectionImpl(GO, TM);
5757
58 return isGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
58 return isGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
5959 }
6060
6161 // Return true if this global address should be placed into small data/bss
6262 // section.
63 bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalValue *GV,
63 bool LanaiTargetObjectFile::isGlobalInSmallSection(const GlobalObject *GO,
6464 const TargetMachine &TM,
6565 SectionKind Kind) const {
66 return (isGlobalInSmallSectionImpl(GV, TM) &&
66 return (isGlobalInSmallSectionImpl(GO, TM) &&
6767 (Kind.isData() || Kind.isBSS() || Kind.isCommon()));
6868 }
6969
7171 // section. This method does all the work, except for checking the section
7272 // kind.
7373 bool LanaiTargetObjectFile::isGlobalInSmallSectionImpl(
74 const GlobalValue *GV, const TargetMachine & /*TM*/) const {
74 const GlobalObject *GO, const TargetMachine & /*TM*/) const {
7575 // Only global variables, not functions.
76 const GlobalVariable *GVA = dyn_cast(GV);
76 const auto *GVA = dyn_cast(GO);
7777 if (!GVA)
7878 return false;
7979
80 if (GV->hasLocalLinkage())
80 if (GVA->hasLocalLinkage())
8181 return false;
8282
83 if (((GV->hasExternalLinkage() && GV->isDeclaration()) ||
84 GV->hasCommonLinkage()))
83 if (((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
84 GVA->hasCommonLinkage()))
8585 return false;
8686
87 Type *Ty = GV->getType()->getElementType();
87 Type *Ty = GVA->getValueType();
8888 return isInSmallSection(
89 GV->getParent()->getDataLayout().getTypeAllocSize(Ty));
89 GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
9090 }
9191
9292 MCSection *LanaiTargetObjectFile::SelectSectionForGlobal(
93 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
93 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
9494 // Handle Small Section classification here.
95 if (Kind.isBSS() && isGlobalInSmallSection(GV, TM, Kind))
95 if (Kind.isBSS() && isGlobalInSmallSection(GO, TM, Kind))
9696 return SmallBSSSection;
97 if (Kind.isData() && isGlobalInSmallSection(GV, TM, Kind))
97 if (Kind.isData() && isGlobalInSmallSection(GO, TM, Kind))
9898 return SmallDataSection;
9999
100100 // Otherwise, we work the same as ELF.
101 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, TM);
101 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
102102 }
103103
104104 /// Return true if this constant should be placed into small data section.
1717 MCSection *SmallDataSection;
1818 MCSection *SmallBSSSection;
1919
20 bool isGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
21 SectionKind Kind) const;
22 bool isGlobalInSmallSectionImpl(const GlobalObject *GO,
23 const TargetMachine &TM) const;
24
2025 public:
2126 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
2227
2328 /// Return true if this global address should be placed into small data/bss
2429 /// section.
25 bool isGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
26 SectionKind Kind) const;
27 bool isGlobalInSmallSection(const GlobalValue *GV,
30 bool isGlobalInSmallSection(const GlobalObject *GO,
2831 const TargetMachine &TM) const;
29 bool isGlobalInSmallSectionImpl(const GlobalValue *GV,
30 const TargetMachine &TM) const;
3132
32 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
33 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
3334 const TargetMachine &TM) const override;
3435
3536 /// Return true if this constant should be placed into small data section.
17711771 const MipsTargetObjectFile *TLOF =
17721772 static_cast(
17731773 getTargetMachine().getObjFileLowering());
1774 if (TLOF->IsGlobalInSmallSection(GV, getTargetMachine()))
1774 const GlobalObject *GO = GV->getBaseObject();
1775 if (GO && TLOF->IsGlobalInSmallSection(GO, getTargetMachine()))
17751776 // %gp_rel relocation
17761777 return getAddrGPRel(N, SDLoc(N), Ty, DAG);
17771778
6060
6161 /// Return true if this global address should be placed into small data/bss
6262 /// section.
63 bool MipsTargetObjectFile::
64 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM) const {
63 bool MipsTargetObjectFile::IsGlobalInSmallSection(
64 const GlobalObject *GO, const TargetMachine &TM) const {
6565 // We first check the case where global is a declaration, because finding
6666 // section kind using getKindForGlobal() is only allowed for global
6767 // definitions.
68 if (GV->isDeclaration() || GV->hasAvailableExternallyLinkage())
69 return IsGlobalInSmallSectionImpl(GV, TM);
68 if (GO->isDeclaration() || GO->hasAvailableExternallyLinkage())
69 return IsGlobalInSmallSectionImpl(GO, TM);
7070
71 return IsGlobalInSmallSection(GV, TM, getKindForGlobal(GV, TM));
71 return IsGlobalInSmallSection(GO, TM, getKindForGlobal(GO, TM));
7272 }
7373
7474 /// Return true if this global address should be placed into small data/bss
7575 /// section.
7676 bool MipsTargetObjectFile::
77 IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
77 IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
7878 SectionKind Kind) const {
79 return (IsGlobalInSmallSectionImpl(GV, TM) &&
79 return (IsGlobalInSmallSectionImpl(GO, TM) &&
8080 (Kind.isData() || Kind.isBSS() || Kind.isCommon()));
8181 }
8282
8484 /// section. This method does all the work, except for checking the section
8585 /// kind.
8686 bool MipsTargetObjectFile::
87 IsGlobalInSmallSectionImpl(const GlobalValue *GV,
87 IsGlobalInSmallSectionImpl(const GlobalObject *GO,
8888 const TargetMachine &TM) const {
8989 const MipsSubtarget &Subtarget =
9090 *static_cast(TM).getSubtargetImpl();
9494 return false;
9595
9696 // Only global variables, not functions.
97 const GlobalVariable *GVA = dyn_cast(GV);
97 const GlobalVariable *GVA = dyn_cast(GO);
9898 if (!GVA)
9999 return false;
100100
101101 // Enforce -mlocal-sdata.
102 if (!LocalSData && GV->hasLocalLinkage())
102 if (!LocalSData && GVA->hasLocalLinkage())
103103 return false;
104104
105105 // Enforce -mextern-sdata.
106 if (!ExternSData && ((GV->hasExternalLinkage() && GV->isDeclaration()) ||
107 GV->hasCommonLinkage()))
106 if (!ExternSData && ((GVA->hasExternalLinkage() && GVA->isDeclaration()) ||
107 GVA->hasCommonLinkage()))
108108 return false;
109109
110 Type *Ty = GV->getValueType();
110 Type *Ty = GVA->getValueType();
111111 return IsInSmallSection(
112 GV->getParent()->getDataLayout().getTypeAllocSize(Ty));
112 GVA->getParent()->getDataLayout().getTypeAllocSize(Ty));
113113 }
114114
115115 MCSection *MipsTargetObjectFile::SelectSectionForGlobal(
116 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
116 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
117117 // TODO: Could also support "weak" symbols as well with ".gnu.linkonce.s.*"
118118 // sections?
119119
120120 // Handle Small Section classification here.
121 if (Kind.isBSS() && IsGlobalInSmallSection(GV, TM, Kind))
121 if (Kind.isBSS() && IsGlobalInSmallSection(GO, TM, Kind))
122122 return SmallBSSSection;
123 if (Kind.isData() && IsGlobalInSmallSection(GV, TM, Kind))
123 if (Kind.isData() && IsGlobalInSmallSection(GO, TM, Kind))
124124 return SmallDataSection;
125125
126126 // Otherwise, we work the same as ELF.
127 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, TM);
127 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
128128 }
129129
130130 /// Return true if this constant should be placed into small data section.
1717 MCSection *SmallDataSection;
1818 MCSection *SmallBSSSection;
1919 const MipsTargetMachine *TM;
20
21 bool IsGlobalInSmallSection(const GlobalObject *GO, const TargetMachine &TM,
22 SectionKind Kind) const;
23 bool IsGlobalInSmallSectionImpl(const GlobalObject *GO,
24 const TargetMachine &TM) const;
2025 public:
2126
2227 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
2328
2429 /// Return true if this global address should be placed into small data/bss
2530 /// section.
26 bool IsGlobalInSmallSection(const GlobalValue *GV, const TargetMachine &TM,
27 SectionKind Kind) const;
28 bool IsGlobalInSmallSection(const GlobalValue *GV,
31 bool IsGlobalInSmallSection(const GlobalObject *GO,
2932 const TargetMachine &TM) const;
30 bool IsGlobalInSmallSectionImpl(const GlobalValue *GV,
31 const TargetMachine &TM) const;
3233
33 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
34 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
3435 const TargetMachine &TM) const override;
3536
3637 /// Return true if this constant should be placed into small data section.
45974597 }
45984598
45994599 MCSection *NVPTXTargetObjectFile::SelectSectionForGlobal(
4600 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
4600 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
46014601 return getDataSection();
46024602 }
9090 return ReadOnlySection;
9191 }
9292
93 MCSection *getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
93 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
9494 const TargetMachine &TM) const override {
9595 return DataSection;
9696 }
9797
98 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
98 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
9999 const TargetMachine &TM) const override;
100100 };
101101
2222 }
2323
2424 MCSection *PPC64LinuxTargetObjectFile::SelectSectionForGlobal(
25 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
25 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
2626 // Here override ReadOnlySection to DataRelROSection for PPC64 SVR4 ABI
2727 // when we have a constant that contains global relocations. This is
2828 // necessary because of this ABI's handling of pointers to functions in
3838 // For more information, see the description of ELIMINATE_COPY_RELOCS in
3939 // GNU ld.
4040 if (Kind.isReadOnly()) {
41 const GlobalVariable *GVar = dyn_cast(GV);
41 const auto *GVar = dyn_cast(GO);
4242
4343 if (GVar && GVar->isConstant() && GVar->getInitializer()->needsRelocation())
4444 Kind = SectionKind::getReadOnlyWithRel();
4545 }
4646
47 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, TM);
47 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
4848 }
4949
5050 const MCExpr *PPC64LinuxTargetObjectFile::
2121
2222 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
2323
24 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
24 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
2525 const TargetMachine &TM) const override;
2626
2727 /// \brief Describe a TLS variable address within debug info.
131131 /// classifies the global in a variety of ways that make various target
132132 /// implementations simpler. The target implementation is free to ignore this
133133 /// extra info of course.
134 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
134 SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
135135 const TargetMachine &TM){
136 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
136 assert(!GO->isDeclaration() && !GO->hasAvailableExternallyLinkage() &&
137137 "Can only be used for global definitions");
138138
139139 Reloc::Model ReloModel = TM.getRelocationModel();
140140
141141 // Early exit - functions should be always in text sections.
142 const GlobalVariable *GVar = dyn_cast(GV);
142 const auto *GVar = dyn_cast(GO);
143143 if (!GVar)
144144 return SectionKind::getText();
145145
200200 // Otherwise, just drop it into a mergable constant section. If we have
201201 // a section for this size, use it, otherwise use the arbitrary sized
202202 // mergable section.
203 switch (GV->getParent()->getDataLayout().getTypeAllocSize(C->getType())) {
203 switch (
204 GVar->getParent()->getDataLayout().getTypeAllocSize(C->getType())) {
204205 case 4: return SectionKind::getMergeableConst4();
205206 case 8: return SectionKind::getMergeableConst8();
206207 case 16: return SectionKind::getMergeableConst16();
233234 /// variable or function definition. This should not be passed external (or
234235 /// available externally) globals.
235236 MCSection *TargetLoweringObjectFile::SectionForGlobal(
236 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
237 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
237238 // Select section name.
238 if (GV->hasSection())
239 return getExplicitSectionGlobal(GV, Kind, TM);
239 if (GO->hasSection())
240 return getExplicitSectionGlobal(GO, Kind, TM);
240241
241242 // Use default section depending on the 'type' of global
242 return SelectSectionForGlobal(GV, Kind, TM);
243 return SelectSectionForGlobal(GO, Kind, TM);
243244 }
244245
245246 MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
9595 }
9696
9797 MCSection *XCoreTargetObjectFile::getExplicitSectionGlobal(
98 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
99 StringRef SectionName = GV->getSection();
98 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
99 StringRef SectionName = GO->getSection();
100100 // Infer section flags from the section name if we can.
101101 bool IsCPRel = SectionName.startswith(".cp.");
102102 if (IsCPRel && !Kind.isReadOnly())
106106 }
107107
108108 MCSection *XCoreTargetObjectFile::SelectSectionForGlobal(
109 const GlobalValue *GV, SectionKind Kind, const TargetMachine &TM) const {
109 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
110110
111 bool UseCPRel = GV->isLocalLinkage(GV->getLinkage());
111 bool UseCPRel = GO->hasLocalLinkage();
112112
113113 if (Kind.isText()) return TextSection;
114114 if (UseCPRel) {
117117 if (Kind.isMergeableConst8()) return MergeableConst8Section;
118118 if (Kind.isMergeableConst16()) return MergeableConst16Section;
119119 }
120 Type *ObjType = GV->getValueType();
121 auto &DL = GV->getParent()->getDataLayout();
120 Type *ObjType = GO->getValueType();
121 auto &DL = GO->getParent()->getDataLayout();
122122 if (TM.getCodeModel() == CodeModel::Small || !ObjType->isSized() ||
123123 DL.getTypeAllocSize(ObjType) < CodeModelLargeSize) {
124124 if (Kind.isReadOnly()) return UseCPRel? ReadOnlySection
2424 public:
2525 void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
2626
27 MCSection *getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
27 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
2828 const TargetMachine &TM) const override;
2929
30 MCSection *SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
30 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind,
3131 const TargetMachine &TM) const override;
3232
3333 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,