blob: 40b766294f0c86113c9402b768502924118092a0 [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",
32 SectionFlags::Writeable | 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",
35 SectionFlags::Writeable | SectionFlags::TLS);
36 TLSBSSSection = getNamedSection("\t.tbss",
asle0d0f482008-07-19 13:14:11 +000037 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
38
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000039 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);
asle0d0f482008-07-19 13:14:11 +000046}
47
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000048SectionKind::Kind
49ELFTargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
50 SectionKind::Kind Kind = TargetAsmInfo::SectionKindForGlobal(GV);
51
52 if (Kind != SectionKind::Data)
53 return Kind;
54
55 // Decide, whether we need data.rel stuff
56 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
Chris Lattnerbdb23b32009-07-22 00:05:44 +000057 if (GVar->hasInitializer() && TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000058 Constant *C = GVar->getInitializer();
59 bool isConstant = GVar->isConstant();
Chris Lattner06a55c32009-07-21 23:47:11 +000060
Chris Lattner06a55c32009-07-21 23:47:11 +000061 // By default - all relocations in PIC mode would force symbol to be
62 // placed in r/w section.
Chris Lattnerbdb23b32009-07-22 00:05:44 +000063 switch (C->getRelocationInfo()) {
64 default: break;
Chris Lattner83ca6d92009-07-24 03:27:21 +000065 case Constant::LocalRelocation:
Chris Lattnerbdb23b32009-07-22 00:05:44 +000066 return isConstant ? SectionKind::DataRelROLocal :
67 SectionKind::DataRelLocal;
Chris Lattner83ca6d92009-07-24 03:27:21 +000068 case Constant::GlobalRelocations:
Chris Lattnerbdb23b32009-07-22 00:05:44 +000069 return isConstant ? SectionKind::DataRelRO : SectionKind::DataRel;
70 }
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000071 }
72
73 return Kind;
74}
75
asle0d0f482008-07-19 13:14:11 +000076const Section*
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +000077ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
78 SectionKind::Kind Kind) const {
asle0d0f482008-07-19 13:14:11 +000079 if (const Function *F = dyn_cast<Function>(GV)) {
80 switch (F->getLinkage()) {
Chris Lattnere09c7d72009-07-24 04:52:38 +000081 default: llvm_unreachable("Unknown linkage type!");
82 case Function::PrivateLinkage:
83 case Function::LinkerPrivateLinkage:
84 case Function::InternalLinkage:
85 case Function::DLLExportLinkage:
86 case Function::ExternalLinkage:
87 return TextSection;
asle0d0f482008-07-19 13:14:11 +000088 }
Chris Lattnera06e19b2009-07-24 05:01:55 +000089 }
90
91 const GlobalVariable *GVar = cast<GlobalVariable>(GV);
92 switch (Kind) {
93 default: llvm_unreachable("Unsuported section kind for global");
94 case SectionKind::Data:
95 case SectionKind::DataRel:
96 return DataRelSection;
97 case SectionKind::DataRelLocal:
98 return DataRelLocalSection;
99 case SectionKind::DataRelRO:
100 return DataRelROSection;
101 case SectionKind::DataRelROLocal:
102 return DataRelROLocalSection;
103 case SectionKind::BSS:
104 return getBSSSection_();
105 case SectionKind::ROData:
106 return getReadOnlySection();
107 case SectionKind::RODataMergeStr:
108 return MergeableStringSection(GVar);
109 case SectionKind::RODataMergeConst: {
110 const Type *Ty = GVar->getInitializer()->getType();
111 const TargetData *TD = TM.getTargetData();
112 return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
113 }
114 case SectionKind::ThreadData:
115 // ELF targets usually support TLS stuff
116 return TLSDataSection;
117 case SectionKind::ThreadBSS:
118 return TLSBSSSection;
Chris Lattnere09c7d72009-07-24 04:52:38 +0000119 }
asle0d0f482008-07-19 13:14:11 +0000120}
121
Chris Lattner680c6f62009-07-22 00:28:43 +0000122/// getSectionForMergableConstant - Given a mergable constant with the
123/// specified size and relocation information, return a section that it
124/// should be placed in.
125const Section *
126ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
127 unsigned ReloInfo) const {
128 // FIXME: IF this global requires a relocation, can we really put it in
129 // rodata??? This should check ReloInfo like darwin.
130
131 const char *SecName = 0;
132 switch (Size) {
133 default: break;
134 case 4: SecName = ".rodata.cst4"; break;
135 case 8: SecName = ".rodata.cst8"; break;
136 case 16: SecName = ".rodata.cst16"; break;
137 }
138
139 if (SecName)
140 return getNamedSection(SecName,
Duncan Sands5bc722e2009-07-22 10:35:05 +0000141 SectionFlags::setEntitySize(SectionFlags::Mergeable,
Chris Lattner680c6f62009-07-22 00:28:43 +0000142 Size));
143
144 return getReadOnlySection(); // .rodata
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000145}
146
Chris Lattnerdfd15982009-07-24 17:02:17 +0000147/// getFlagsForNamedSection - If this target wants to be able to infer
148/// section flags based on the name of the section specified for a global
149/// variable, it can implement this.
150unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
151 unsigned Flags = 0;
152 if (Name[0] != '.') return 0;
153
154 // Some lame default implementation based on some magic section names.
155 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
156 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
157 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
158 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
159 Flags |= SectionFlags::BSS;
160 else if (strcmp(Name, ".tdata") == 0 ||
161 strncmp(Name, ".tdata.", 7) == 0 ||
162 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
163 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
164 Flags |= SectionFlags::TLS;
165 else if (strcmp(Name, ".tbss") == 0 ||
166 strncmp(Name, ".tbss.", 6) == 0 ||
167 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
168 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
169 Flags |= SectionFlags::BSS | SectionFlags::TLS;
170
171 return Flags;
172}
173
Chris Lattner680c6f62009-07-22 00:28:43 +0000174
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000175const Section*
asle0d0f482008-07-19 13:14:11 +0000176ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000177 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000178 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000179 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000180
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000181 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000182 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000183 assert(getCStringSection() && "Should have string section prefix");
184
asle0d0f482008-07-19 13:14:11 +0000185 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000186 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000187 if (Align < Size)
188 Align = Size;
189
190 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
191 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
192 SectionFlags::Strings,
193 Size);
194 return getNamedSection(Name.c_str(), Flags);
195 }
196
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000197 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000198}
199
asl27ffd262008-08-16 12:57:07 +0000200std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000201 std::string Flags = ",\"";
202
203 if (!(flags & SectionFlags::Debug))
204 Flags += 'a';
205 if (flags & SectionFlags::Code)
206 Flags += 'x';
207 if (flags & SectionFlags::Writeable)
208 Flags += 'w';
209 if (flags & SectionFlags::Mergeable)
210 Flags += 'M';
211 if (flags & SectionFlags::Strings)
212 Flags += 'S';
213 if (flags & SectionFlags::TLS)
214 Flags += 'T';
asle0d0f482008-07-19 13:14:11 +0000215
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000216 Flags += "\",";
217
218 // If comment string is '@', e.g. as on ARM - use '%' instead
219 if (strcmp(CommentString, "@") == 0)
220 Flags += '%';
221 else
222 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000223
224 // FIXME: There can be exceptions here
225 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000226 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000227 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000228 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000229
230 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
231 Flags += "," + utostr(entitySize);
232
233 return Flags;
234}