blob: 4631cda2296ce47545e63cd659edd82f4037ee63 [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,
Chris Lattnercc195212009-07-25 23:21:55 +000051 SectionKind Kind) const {
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +000052 if (Kind.isText()) return TextSection;
53 if (Kind.isMergableCString())
Chris Lattner3a2a55a2009-07-26 03:06:11 +000054 return MergeableStringSection(cast<GlobalVariable>(GV));
Chris Lattner2a7dd7d2009-07-26 06:11:33 +000055
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +000056 if (Kind.isMergableConst()) {
Chris Lattner3a2a55a2009-07-26 03:06:11 +000057 const Type *Ty = cast<GlobalVariable>(GV)->getInitializer()->getType();
Chris Lattnera06e19b2009-07-24 05:01:55 +000058 const TargetData *TD = TM.getTargetData();
59 return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
60 }
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +000061 if (Kind.isReadOnly()) return getReadOnlySection();
62
63
64 if (Kind.isThreadData()) return TLSDataSection;
65 if (Kind.isThreadBSS()) return TLSBSSSection;
66
67 if (Kind.isBSS()) return getBSSSection_();
68
69
70 if (Kind.isDataNoRel()) return DataSection;
71 if (Kind.isDataRelLocal()) return DataRelLocalSection;
72 if (Kind.isDataRel()) return DataRelSection;
73 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
74
75 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
76 return DataRelROSection;
asle0d0f482008-07-19 13:14:11 +000077}
78
Chris Lattner680c6f62009-07-22 00:28:43 +000079/// getSectionForMergableConstant - Given a mergable constant with the
80/// specified size and relocation information, return a section that it
81/// should be placed in.
82const Section *
83ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
84 unsigned ReloInfo) const {
Chris Lattner07203df2009-07-26 05:55:20 +000085 // If this constant pool entry has relocations, stick it into a relocatable
86 // section.
87 if (ReloInfo == 2)
88 return DataRelROSection;
89 if (ReloInfo == 1)
90 return DataRelROLocalSection;
91
Chris Lattner680c6f62009-07-22 00:28:43 +000092
93 const char *SecName = 0;
94 switch (Size) {
95 default: break;
96 case 4: SecName = ".rodata.cst4"; break;
97 case 8: SecName = ".rodata.cst8"; break;
98 case 16: SecName = ".rodata.cst16"; break;
99 }
100
101 if (SecName)
102 return getNamedSection(SecName,
Duncan Sands5bc722e2009-07-22 10:35:05 +0000103 SectionFlags::setEntitySize(SectionFlags::Mergeable,
Chris Lattner680c6f62009-07-22 00:28:43 +0000104 Size));
105
106 return getReadOnlySection(); // .rodata
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000107}
108
Chris Lattnerdfd15982009-07-24 17:02:17 +0000109/// getFlagsForNamedSection - If this target wants to be able to infer
110/// section flags based on the name of the section specified for a global
111/// variable, it can implement this.
112unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
113 unsigned Flags = 0;
114 if (Name[0] != '.') return 0;
115
116 // Some lame default implementation based on some magic section names.
117 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
118 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
119 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
120 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
121 Flags |= SectionFlags::BSS;
122 else if (strcmp(Name, ".tdata") == 0 ||
123 strncmp(Name, ".tdata.", 7) == 0 ||
124 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
125 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
126 Flags |= SectionFlags::TLS;
127 else if (strcmp(Name, ".tbss") == 0 ||
128 strncmp(Name, ".tbss.", 6) == 0 ||
129 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
130 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
131 Flags |= SectionFlags::BSS | SectionFlags::TLS;
132
133 return Flags;
134}
135
Chris Lattner4c530ee2009-07-25 18:57:34 +0000136const char *
Chris Lattnercc195212009-07-25 23:21:55 +0000137ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind Kind) const{
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +0000138 if (Kind.isText()) return ".gnu.linkonce.t.";
139 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
140
141 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
142 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
143
144 if (Kind.isBSS()) return ".gnu.linkonce.b.";
145 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
146 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
147 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
148 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
149
150 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
151 return ".gnu.linkonce.d.rel.ro.";
Chris Lattner4c530ee2009-07-25 18:57:34 +0000152}
153
154
155
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000156const Section*
asle0d0f482008-07-19 13:14:11 +0000157ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000158 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000159 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000160 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000161
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000162 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000163 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000164 assert(getCStringSection() && "Should have string section prefix");
165
asle0d0f482008-07-19 13:14:11 +0000166 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000167 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000168 if (Align < Size)
169 Align = Size;
170
171 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
172 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
173 SectionFlags::Strings,
174 Size);
175 return getNamedSection(Name.c_str(), Flags);
176 }
177
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000178 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000179}
180
asl27ffd262008-08-16 12:57:07 +0000181std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000182 std::string Flags = ",\"";
183
184 if (!(flags & SectionFlags::Debug))
185 Flags += 'a';
186 if (flags & SectionFlags::Code)
187 Flags += 'x';
Chris Lattner4c530ee2009-07-25 18:57:34 +0000188 if (flags & SectionFlags::Writable)
asle0d0f482008-07-19 13:14:11 +0000189 Flags += 'w';
190 if (flags & SectionFlags::Mergeable)
191 Flags += 'M';
192 if (flags & SectionFlags::Strings)
193 Flags += 'S';
194 if (flags & SectionFlags::TLS)
195 Flags += 'T';
asle0d0f482008-07-19 13:14:11 +0000196
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000197 Flags += "\",";
198
199 // If comment string is '@', e.g. as on ARM - use '%' instead
200 if (strcmp(CommentString, "@") == 0)
201 Flags += '%';
202 else
203 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000204
205 // FIXME: There can be exceptions here
206 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000207 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000208 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000209 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000210
211 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
212 Flags += "," + utostr(entitySize);
213
214 return Flags;
215}