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::LinkerPrivateLinkage:
81 case Function::InternalLinkage:
82 case Function::DLLExportLinkage:
83 case Function::ExternalLinkage:
85 case Function::WeakAnyLinkage:
86 case Function::WeakODRLinkage:
87 case Function::LinkOnceAnyLinkage:
88 case Function::LinkOnceODRLinkage:
89 std::string Name = UniqueSectionForGlobal(GV, Kind);
90 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
91 return getNamedSection(Name.c_str(), Flags);
93 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
94 if (GVar->isWeakForLinker()) {
95 std::string Name = UniqueSectionForGlobal(GVar, Kind);
96 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
97 return getNamedSection(Name.c_str(), Flags);
100 case SectionKind::Data:
101 case SectionKind::SmallData:
103 case SectionKind::DataRel:
104 return DataRelSection;
105 case SectionKind::DataRelLocal:
106 return DataRelLocalSection;
107 case SectionKind::DataRelRO:
108 return DataRelROSection;
109 case SectionKind::DataRelROLocal:
110 return DataRelROLocalSection;
111 case SectionKind::BSS:
112 case SectionKind::SmallBSS:
113 // ELF targets usually have BSS sections
114 return getBSSSection_();
115 case SectionKind::ROData:
116 case SectionKind::SmallROData:
117 return getReadOnlySection();
118 case SectionKind::RODataMergeStr:
119 return MergeableStringSection(GVar);
120 case SectionKind::RODataMergeConst:
121 return MergeableConstSection(GVar);
122 case SectionKind::ThreadData:
123 // ELF targets usually support TLS stuff
124 return TLSDataSection;
125 case SectionKind::ThreadBSS:
126 return TLSBSSSection;
128 llvm_unreachable("Unsuported section kind for global");
132 llvm_unreachable("Unsupported global");
138 ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
139 // FIXME: Support data.rel stuff someday
140 return MergeableConstSection(Ty);
144 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
145 Constant *C = GV->getInitializer();
146 return MergeableConstSection(C->getType());
149 inline const Section*
150 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
151 const TargetData *TD = TM.getTargetData();
153 // FIXME: string here is temporary, until stuff will fully land in.
154 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
155 // currently directly used by asmprinter.
156 unsigned Size = TD->getTypeAllocSize(Ty);
157 if (Size == 4 || Size == 8 || Size == 16) {
158 std::string Name = ".rodata.cst" + utostr(Size);
160 return getNamedSection(Name.c_str(),
161 SectionFlags::setEntitySize(SectionFlags::Mergeable,
165 return getReadOnlySection();
169 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
170 const TargetData *TD = TM.getTargetData();
171 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
172 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
174 unsigned Size = TD->getTypeAllocSize(Ty);
176 assert(getCStringSection() && "Should have string section prefix");
178 // We also need alignment here
179 unsigned Align = TD->getPrefTypeAlignment(Ty);
183 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
184 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
185 SectionFlags::Strings,
187 return getNamedSection(Name.c_str(), Flags);
190 return getReadOnlySection();
193 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
194 std::string Flags = ",\"";
196 if (!(flags & SectionFlags::Debug))
198 if (flags & SectionFlags::Code)
200 if (flags & SectionFlags::Writeable)
202 if (flags & SectionFlags::Mergeable)
204 if (flags & SectionFlags::Strings)
206 if (flags & SectionFlags::TLS)
208 if (flags & SectionFlags::Small)
213 // If comment string is '@', e.g. as on ARM - use '%' instead
214 if (strcmp(CommentString, "@") == 0)
219 // FIXME: There can be exceptions here
220 if (flags & SectionFlags::BSS)
225 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
226 Flags += "," + utostr(entitySize);