blob: ea0783621168ac6423f7604ace2e6d5d4d69a97b [file] [log] [blame]
asle0d0f482008-07-19 13:14:11 +00001//===-- ELFTargetAsmInfo.cpp - ELF asm properties ---------------*- C++ -*-===//
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//
10// This file defines target asm properties related what form asm statements
11// should take in general on ELF-based targets
12//
13//===----------------------------------------------------------------------===//
14
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"
Anton Korobeynikovfc54db12008-08-07 09:50:34 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Edwin Török675d5622009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
asle0d0f482008-07-19 13:14:11 +000022#include "llvm/Target/ELFTargetAsmInfo.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetData.h"
25
26using namespace llvm;
27
Dan Gohmana004d7b2008-11-03 18:22:42 +000028ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM)
29 : TargetAsmInfo(TM) {
asle0d0f482008-07-19 13:14:11 +000030
asle0d0f482008-07-19 13:14:11 +000031 BSSSection_ = getUnnamedSection("\t.bss",
Chris Lattner4c530ee2009-07-25 18:57:34 +000032 SectionFlags::Writable | SectionFlags::BSS);
Anton Korobeynikov4d433222008-09-24 22:20:27 +000033 ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None);
Anton Korobeynikov9a213e42008-09-24 22:17:06 +000034 TLSDataSection = getNamedSection("\t.tdata",
Chris Lattner4c530ee2009-07-25 18:57:34 +000035 SectionFlags::Writable | SectionFlags::TLS);
Anton Korobeynikov9a213e42008-09-24 22:17:06 +000036 TLSBSSSection = getNamedSection("\t.tbss",
Chris Lattner4c530ee2009-07-25 18:57:34 +000037 SectionFlags::Writable | SectionFlags::TLS | SectionFlags::BSS);
asle0d0f482008-07-19 13:14:11 +000038
Chris Lattner4c530ee2009-07-25 18:57:34 +000039 DataRelSection = getNamedSection("\t.data.rel", SectionFlags::Writable);
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000040 DataRelLocalSection = getNamedSection("\t.data.rel.local",
Chris Lattner4c530ee2009-07-25 18:57:34 +000041 SectionFlags::Writable);
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000042 DataRelROSection = getNamedSection("\t.data.rel.ro",
Chris Lattner4c530ee2009-07-25 18:57:34 +000043 SectionFlags::Writable);
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000044 DataRelROLocalSection = getNamedSection("\t.data.rel.ro.local",
Chris Lattner4c530ee2009-07-25 18:57:34 +000045 SectionFlags::Writable);
asle0d0f482008-07-19 13:14:11 +000046}
47
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000048
asle0d0f482008-07-19 13:14:11 +000049const Section*
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +000050ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
51 SectionKind::Kind Kind) const {
asle0d0f482008-07-19 13:14:11 +000052 if (const Function *F = dyn_cast<Function>(GV)) {
53 switch (F->getLinkage()) {
Chris Lattnere09c7d72009-07-24 04:52:38 +000054 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:
60 return TextSection;
asle0d0f482008-07-19 13:14:11 +000061 }
Chris Lattnera06e19b2009-07-24 05:01:55 +000062 }
63
64 const GlobalVariable *GVar = cast<GlobalVariable>(GV);
65 switch (Kind) {
66 default: llvm_unreachable("Unsuported section kind for global");
Chris Lattnerd398a8d2009-07-24 19:15:47 +000067 case SectionKind::BSS:
68 return getBSSSection_();
Chris Lattnera06e19b2009-07-24 05:01:55 +000069 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;
Chris Lattnera06e19b2009-07-24 05:01:55 +000078 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);
86 }
87 case SectionKind::ThreadData:
88 // ELF targets usually support TLS stuff
89 return TLSDataSection;
90 case SectionKind::ThreadBSS:
91 return TLSBSSSection;
Chris Lattnere09c7d72009-07-24 04:52:38 +000092 }
asle0d0f482008-07-19 13:14:11 +000093}
94
Chris Lattner680c6f62009-07-22 00:28:43 +000095/// getSectionForMergableConstant - Given a mergable constant with the
96/// specified size and relocation information, return a section that it
97/// should be placed in.
98const Section *
99ELFTargetAsmInfo::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.
103
104 const char *SecName = 0;
105 switch (Size) {
106 default: break;
107 case 4: SecName = ".rodata.cst4"; break;
108 case 8: SecName = ".rodata.cst8"; break;
109 case 16: SecName = ".rodata.cst16"; break;
110 }
111
112 if (SecName)
113 return getNamedSection(SecName,
Duncan Sands5bc722e2009-07-22 10:35:05 +0000114 SectionFlags::setEntitySize(SectionFlags::Mergeable,
Chris Lattner680c6f62009-07-22 00:28:43 +0000115 Size));
116
117 return getReadOnlySection(); // .rodata
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000118}
119
Chris Lattnerdfd15982009-07-24 17:02:17 +0000120/// 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.
123unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
124 unsigned Flags = 0;
125 if (Name[0] != '.') return 0;
126
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;
143
144 return Flags;
145}
146
Chris Lattner680c6f62009-07-22 00:28:43 +0000147
Chris Lattner4c530ee2009-07-25 18:57:34 +0000148
149const char *
150ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind::Kind Kind) const{
151 switch (Kind) {
152 default: llvm_unreachable("Unknown section kind");
153 case SectionKind::Text: return ".gnu.linkonce.t.";
154 case SectionKind::Data: return ".gnu.linkonce.d.";
155 case SectionKind::DataRel: return ".gnu.linkonce.d.rel.";
156 case SectionKind::DataRelLocal: return ".gnu.linkonce.d.rel.local.";
157 case SectionKind::DataRelRO: return ".gnu.linkonce.d.rel.ro.";
158 case SectionKind::DataRelROLocal: return ".gnu.linkonce.d.rel.ro.local.";
159 case SectionKind::BSS: return ".gnu.linkonce.b.";
160 case SectionKind::ROData:
161 case SectionKind::RODataMergeConst:
162 case SectionKind::RODataMergeStr: return ".gnu.linkonce.r.";
163 case SectionKind::ThreadData: return ".gnu.linkonce.td.";
164 case SectionKind::ThreadBSS: return ".gnu.linkonce.tb.";
165 }
166}
167
168
169
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000170const Section*
asle0d0f482008-07-19 13:14:11 +0000171ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000172 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000173 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000174 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000175
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000176 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000177 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000178 assert(getCStringSection() && "Should have string section prefix");
179
asle0d0f482008-07-19 13:14:11 +0000180 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000181 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000182 if (Align < Size)
183 Align = Size;
184
185 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
186 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
187 SectionFlags::Strings,
188 Size);
189 return getNamedSection(Name.c_str(), Flags);
190 }
191
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000192 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000193}
194
asl27ffd262008-08-16 12:57:07 +0000195std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000196 std::string Flags = ",\"";
197
198 if (!(flags & SectionFlags::Debug))
199 Flags += 'a';
200 if (flags & SectionFlags::Code)
201 Flags += 'x';
Chris Lattner4c530ee2009-07-25 18:57:34 +0000202 if (flags & SectionFlags::Writable)
asle0d0f482008-07-19 13:14:11 +0000203 Flags += 'w';
204 if (flags & SectionFlags::Mergeable)
205 Flags += 'M';
206 if (flags & SectionFlags::Strings)
207 Flags += 'S';
208 if (flags & SectionFlags::TLS)
209 Flags += 'T';
asle0d0f482008-07-19 13:14:11 +0000210
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000211 Flags += "\",";
212
213 // If comment string is '@', e.g. as on ARM - use '%' instead
214 if (strcmp(CommentString, "@") == 0)
215 Flags += '%';
216 else
217 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000218
219 // FIXME: There can be exceptions here
220 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000221 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000222 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000223 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000224
225 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
226 Flags += "," + utostr(entitySize);
227
228 return Flags;
229}