1 //===-- ELFTargetAsmInfo.cpp - ELF asm properties ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines target asm properties related what form asm statements
11 // should take in general on ELF-based targets
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Target/ELFTargetAsmInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetData.h"
28 ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM)
31 BSSSection_ = getUnnamedSection("\t.bss",
32 SectionFlags::Writeable | SectionFlags::BSS);
33 ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None);
34 TLSDataSection = getNamedSection("\t.tdata",
35 SectionFlags::Writeable | SectionFlags::TLS);
36 TLSBSSSection = getNamedSection("\t.tbss",
37 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
39 DataRelSection = getNamedSection("\t.data.rel", SectionFlags::Writeable);
40 DataRelLocalSection = getNamedSection("\t.data.rel.local",
41 SectionFlags::Writeable);
42 DataRelROSection = getNamedSection("\t.data.rel.ro",
43 SectionFlags::Writeable);
44 DataRelROLocalSection = getNamedSection("\t.data.rel.ro.local",
45 SectionFlags::Writeable);
49 ELFTargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
50 SectionKind::Kind Kind = TargetAsmInfo::SectionKindForGlobal(GV);
52 if (Kind != SectionKind::Data)
55 // Decide, whether we need data.rel stuff
56 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
57 if (GVar->hasInitializer()) {
58 Constant *C = GVar->getInitializer();
59 bool isConstant = GVar->isConstant();
60 unsigned Reloc = RelocBehaviour();
61 if (Reloc != Reloc::None && C->ContainsRelocations(Reloc))
62 return (C->ContainsRelocations(Reloc::Global) ?
64 SectionKind::DataRelRO : SectionKind::DataRel) :
66 SectionKind::DataRelROLocal : SectionKind::DataRelLocal));
73 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
74 SectionKind::Kind Kind = SectionKindForGlobal(GV);
76 if (const Function *F = dyn_cast<Function>(GV)) {
77 switch (F->getLinkage()) {
78 default: llvm_unreachable("Unknown linkage type!");
79 case Function::PrivateLinkage:
80 case Function::InternalLinkage:
81 case Function::DLLExportLinkage:
82 case Function::ExternalLinkage:
84 case Function::WeakAnyLinkage:
85 case Function::WeakODRLinkage:
86 case Function::LinkOnceAnyLinkage:
87 case Function::LinkOnceODRLinkage:
88 std::string Name = UniqueSectionForGlobal(GV, Kind);
89 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
90 return getNamedSection(Name.c_str(), Flags);
92 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
93 if (GVar->isWeakForLinker()) {
94 std::string Name = UniqueSectionForGlobal(GVar, Kind);
95 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
96 return getNamedSection(Name.c_str(), Flags);
99 case SectionKind::Data:
100 case SectionKind::SmallData:
102 case SectionKind::DataRel:
103 return DataRelSection;
104 case SectionKind::DataRelLocal:
105 return DataRelLocalSection;
106 case SectionKind::DataRelRO:
107 return DataRelROSection;
108 case SectionKind::DataRelROLocal:
109 return DataRelROLocalSection;
110 case SectionKind::BSS:
111 case SectionKind::SmallBSS:
112 // ELF targets usually have BSS sections
113 return getBSSSection_();
114 case SectionKind::ROData:
115 case SectionKind::SmallROData:
116 return getReadOnlySection();
117 case SectionKind::RODataMergeStr:
118 return MergeableStringSection(GVar);
119 case SectionKind::RODataMergeConst:
120 return MergeableConstSection(GVar);
121 case SectionKind::ThreadData:
122 // ELF targets usually support TLS stuff
123 return TLSDataSection;
124 case SectionKind::ThreadBSS:
125 return TLSBSSSection;
127 llvm_unreachable("Unsuported section kind for global");
131 llvm_unreachable("Unsupported global");
137 ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
138 // FIXME: Support data.rel stuff someday
139 return MergeableConstSection(Ty);
143 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
144 Constant *C = GV->getInitializer();
145 return MergeableConstSection(C->getType());
148 inline const Section*
149 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
150 const TargetData *TD = TM.getTargetData();
152 // FIXME: string here is temporary, until stuff will fully land in.
153 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
154 // currently directly used by asmprinter.
155 unsigned Size = TD->getTypeAllocSize(Ty);
156 if (Size == 4 || Size == 8 || Size == 16) {
157 std::string Name = ".rodata.cst" + utostr(Size);
159 return getNamedSection(Name.c_str(),
160 SectionFlags::setEntitySize(SectionFlags::Mergeable,
164 return getReadOnlySection();
168 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
169 const TargetData *TD = TM.getTargetData();
170 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
171 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
173 unsigned Size = TD->getTypeAllocSize(Ty);
175 assert(getCStringSection() && "Should have string section prefix");
177 // We also need alignment here
178 unsigned Align = TD->getPrefTypeAlignment(Ty);
182 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
183 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
184 SectionFlags::Strings,
186 return getNamedSection(Name.c_str(), Flags);
189 return getReadOnlySection();
192 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
193 std::string Flags = ",\"";
195 if (!(flags & SectionFlags::Debug))
197 if (flags & SectionFlags::Code)
199 if (flags & SectionFlags::Writeable)
201 if (flags & SectionFlags::Mergeable)
203 if (flags & SectionFlags::Strings)
205 if (flags & SectionFlags::TLS)
207 if (flags & SectionFlags::Small)
212 // If comment string is '@', e.g. as on ARM - use '%' instead
213 if (strcmp(CommentString, "@") == 0)
218 // FIXME: There can be exceptions here
219 if (flags & SectionFlags::BSS)
224 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
225 Flags += "," + utostr(entitySize);