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);
50 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
51 SectionKind::Kind Kind) const {
52 if (const Function *F = dyn_cast<Function>(GV)) {
53 switch (F->getLinkage()) {
54 default: llvm_unreachable("Unknown linkage type!");
55 case Function::PrivateLinkage:
56 case Function::LinkerPrivateLinkage:
57 case Function::InternalLinkage:
58 case Function::DLLExportLinkage:
59 case Function::ExternalLinkage:
64 const GlobalVariable *GVar = cast<GlobalVariable>(GV);
66 default: llvm_unreachable("Unsuported section kind for global");
67 case SectionKind::BSS:
68 return getBSSSection_();
69 case SectionKind::Data:
70 case SectionKind::DataRel:
71 return DataRelSection;
72 case SectionKind::DataRelLocal:
73 return DataRelLocalSection;
74 case SectionKind::DataRelRO:
75 return DataRelROSection;
76 case SectionKind::DataRelROLocal:
77 return DataRelROLocalSection;
78 case SectionKind::ROData:
79 return getReadOnlySection();
80 case SectionKind::RODataMergeStr:
81 return MergeableStringSection(GVar);
82 case SectionKind::RODataMergeConst: {
83 const Type *Ty = GVar->getInitializer()->getType();
84 const TargetData *TD = TM.getTargetData();
85 return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
87 case SectionKind::ThreadData:
88 // ELF targets usually support TLS stuff
89 return TLSDataSection;
90 case SectionKind::ThreadBSS:
95 /// getSectionForMergableConstant - Given a mergable constant with the
96 /// specified size and relocation information, return a section that it
97 /// should be placed in.
99 ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
100 unsigned ReloInfo) const {
101 // FIXME: IF this global requires a relocation, can we really put it in
102 // rodata??? This should check ReloInfo like darwin.
104 const char *SecName = 0;
107 case 4: SecName = ".rodata.cst4"; break;
108 case 8: SecName = ".rodata.cst8"; break;
109 case 16: SecName = ".rodata.cst16"; break;
113 return getNamedSection(SecName,
114 SectionFlags::setEntitySize(SectionFlags::Mergeable,
117 return getReadOnlySection(); // .rodata
120 /// getFlagsForNamedSection - If this target wants to be able to infer
121 /// section flags based on the name of the section specified for a global
122 /// variable, it can implement this.
123 unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
125 if (Name[0] != '.') return 0;
127 // Some lame default implementation based on some magic section names.
128 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
129 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
130 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
131 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
132 Flags |= SectionFlags::BSS;
133 else if (strcmp(Name, ".tdata") == 0 ||
134 strncmp(Name, ".tdata.", 7) == 0 ||
135 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
136 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
137 Flags |= SectionFlags::TLS;
138 else if (strcmp(Name, ".tbss") == 0 ||
139 strncmp(Name, ".tbss.", 6) == 0 ||
140 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
141 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
142 Flags |= SectionFlags::BSS | SectionFlags::TLS;
149 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
150 const TargetData *TD = TM.getTargetData();
151 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
152 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
154 unsigned Size = TD->getTypeAllocSize(Ty);
156 assert(getCStringSection() && "Should have string section prefix");
158 // We also need alignment here
159 unsigned Align = TD->getPrefTypeAlignment(Ty);
163 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
164 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
165 SectionFlags::Strings,
167 return getNamedSection(Name.c_str(), Flags);
170 return getReadOnlySection();
173 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
174 std::string Flags = ",\"";
176 if (!(flags & SectionFlags::Debug))
178 if (flags & SectionFlags::Code)
180 if (flags & SectionFlags::Writeable)
182 if (flags & SectionFlags::Mergeable)
184 if (flags & SectionFlags::Strings)
186 if (flags & SectionFlags::TLS)
191 // If comment string is '@', e.g. as on ARM - use '%' instead
192 if (strcmp(CommentString, "@") == 0)
197 // FIXME: There can be exceptions here
198 if (flags & SectionFlags::BSS)
203 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
204 Flags += "," + utostr(entitySize);