blob: 927cb38b1de97e73b49b4135c34cab796d1c993e [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"
asle0d0f482008-07-19 13:14:11 +000025using namespace llvm;
26
Dan Gohmana004d7b2008-11-03 18:22:42 +000027ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM)
28 : TargetAsmInfo(TM) {
asle0d0f482008-07-19 13:14:11 +000029
Chris Lattnerc51585b2009-07-26 19:23:28 +000030 BSSSection_ = getUnnamedSection("\t.bss",
31 SectionFlags::Writable | SectionFlags::BSS);
Anton Korobeynikov4d433222008-09-24 22:20:27 +000032 ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None);
Anton Korobeynikov9a213e42008-09-24 22:17:06 +000033 TLSDataSection = getNamedSection("\t.tdata",
Chris Lattner4c530ee2009-07-25 18:57:34 +000034 SectionFlags::Writable | SectionFlags::TLS);
Anton Korobeynikov9a213e42008-09-24 22:17:06 +000035 TLSBSSSection = getNamedSection("\t.tbss",
Chris Lattner4c530ee2009-07-25 18:57:34 +000036 SectionFlags::Writable | SectionFlags::TLS | SectionFlags::BSS);
asle0d0f482008-07-19 13:14:11 +000037
Chris Lattner4c530ee2009-07-25 18:57:34 +000038 DataRelSection = getNamedSection("\t.data.rel", SectionFlags::Writable);
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000039 DataRelLocalSection = getNamedSection("\t.data.rel.local",
Chris Lattner4c530ee2009-07-25 18:57:34 +000040 SectionFlags::Writable);
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000041 DataRelROSection = getNamedSection("\t.data.rel.ro",
Chris Lattner4c530ee2009-07-25 18:57:34 +000042 SectionFlags::Writable);
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000043 DataRelROLocalSection = getNamedSection("\t.data.rel.ro.local",
Chris Lattner4c530ee2009-07-25 18:57:34 +000044 SectionFlags::Writable);
Chris Lattner52954842009-07-26 06:16:11 +000045
Chris Lattner1c379242009-07-26 06:48:26 +000046 MergeableConst4Section = getNamedSection(".rodata.cst4",
Chris Lattner52954842009-07-26 06:16:11 +000047 SectionFlags::setEntitySize(SectionFlags::Mergeable, 4));
Chris Lattner1c379242009-07-26 06:48:26 +000048 MergeableConst8Section = getNamedSection(".rodata.cst8",
Chris Lattner52954842009-07-26 06:16:11 +000049 SectionFlags::setEntitySize(SectionFlags::Mergeable, 8));
Chris Lattner1c379242009-07-26 06:48:26 +000050 MergeableConst16Section = getNamedSection(".rodata.cst16",
Chris Lattner52954842009-07-26 06:16:11 +000051 SectionFlags::setEntitySize(SectionFlags::Mergeable, 16));
asle0d0f482008-07-19 13:14:11 +000052}
53
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000054
asle0d0f482008-07-19 13:14:11 +000055const Section*
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +000056ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnercc195212009-07-25 23:21:55 +000057 SectionKind Kind) const {
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +000058 if (Kind.isText()) return TextSection;
Chris Lattner1c379242009-07-26 06:48:26 +000059 if (Kind.isMergeableCString())
Chris Lattner3a2a55a2009-07-26 03:06:11 +000060 return MergeableStringSection(cast<GlobalVariable>(GV));
Chris Lattner2a7dd7d2009-07-26 06:11:33 +000061
Chris Lattner1c379242009-07-26 06:48:26 +000062 if (Kind.isMergeableConst()) {
63 if (Kind.isMergeableConst4())
64 return MergeableConst4Section;
65 if (Kind.isMergeableConst8())
66 return MergeableConst8Section;
67 if (Kind.isMergeableConst16())
68 return MergeableConst16Section;
Chris Lattner52954842009-07-26 06:16:11 +000069 return ReadOnlySection; // .const
Chris Lattnera06e19b2009-07-24 05:01:55 +000070 }
Chris Lattner52954842009-07-26 06:16:11 +000071
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +000072 if (Kind.isReadOnly()) return getReadOnlySection();
73
74
75 if (Kind.isThreadData()) return TLSDataSection;
76 if (Kind.isThreadBSS()) return TLSBSSSection;
77
78 if (Kind.isBSS()) return getBSSSection_();
79
80
81 if (Kind.isDataNoRel()) return DataSection;
82 if (Kind.isDataRelLocal()) return DataRelLocalSection;
83 if (Kind.isDataRel()) return DataRelSection;
84 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
85
86 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
87 return DataRelROSection;
asle0d0f482008-07-19 13:14:11 +000088}
89
Chris Lattner1c379242009-07-26 06:48:26 +000090/// getSectionForMergeableConstant - Given a Mergeable constant with the
Chris Lattner680c6f62009-07-22 00:28:43 +000091/// specified size and relocation information, return a section that it
92/// should be placed in.
93const Section *
Chris Lattner1c379242009-07-26 06:48:26 +000094ELFTargetAsmInfo::getSectionForMergeableConstant(SectionKind Kind) const {
Chris Lattnerfc60ba12009-07-26 06:26:55 +000095 return SelectSectionForGlobal(0, Kind);
Anton Korobeynikovfc54db12008-08-07 09:50:34 +000096}
97
Chris Lattnerdfd15982009-07-24 17:02:17 +000098/// getFlagsForNamedSection - If this target wants to be able to infer
99/// section flags based on the name of the section specified for a global
100/// variable, it can implement this.
101unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
102 unsigned Flags = 0;
103 if (Name[0] != '.') return 0;
104
105 // Some lame default implementation based on some magic section names.
106 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
107 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
108 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
109 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
110 Flags |= SectionFlags::BSS;
111 else if (strcmp(Name, ".tdata") == 0 ||
112 strncmp(Name, ".tdata.", 7) == 0 ||
113 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
114 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
115 Flags |= SectionFlags::TLS;
116 else if (strcmp(Name, ".tbss") == 0 ||
117 strncmp(Name, ".tbss.", 6) == 0 ||
118 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
119 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
120 Flags |= SectionFlags::BSS | SectionFlags::TLS;
121
122 return Flags;
123}
124
Chris Lattner4c530ee2009-07-25 18:57:34 +0000125const char *
Chris Lattnercc195212009-07-25 23:21:55 +0000126ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind Kind) const{
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +0000127 if (Kind.isText()) return ".gnu.linkonce.t.";
128 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
129
130 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
131 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
132
133 if (Kind.isBSS()) return ".gnu.linkonce.b.";
134 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
135 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
136 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
137 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
138
139 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
140 return ".gnu.linkonce.d.rel.ro.";
Chris Lattner4c530ee2009-07-25 18:57:34 +0000141}
142
143
144
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000145const Section*
asle0d0f482008-07-19 13:14:11 +0000146ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000147 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000148 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000149 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000150
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000151 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000152 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000153 assert(getCStringSection() && "Should have string section prefix");
154
asle0d0f482008-07-19 13:14:11 +0000155 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000156 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000157 if (Align < Size)
158 Align = Size;
159
160 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
161 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
162 SectionFlags::Strings,
163 Size);
164 return getNamedSection(Name.c_str(), Flags);
165 }
166
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000167 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000168}
169
Chris Lattner94d8a762009-07-26 07:33:58 +0000170void ELFTargetAsmInfo::getSectionFlags(unsigned Flags,
171 SmallVectorImpl<char> &Str) const {
172 Str.push_back(',');
173 Str.push_back('"');
174
175 if (!(Flags & SectionFlags::Debug))
176 Str.push_back('a');
177 if (Flags & SectionFlags::Code)
178 Str.push_back('x');
179 if (Flags & SectionFlags::Writable)
180 Str.push_back('w');
181 if (Flags & SectionFlags::Mergeable)
182 Str.push_back('M');
183 if (Flags & SectionFlags::Strings)
184 Str.push_back('S');
185 if (Flags & SectionFlags::TLS)
186 Str.push_back('T');
asle0d0f482008-07-19 13:14:11 +0000187
Chris Lattner94d8a762009-07-26 07:33:58 +0000188 Str.push_back('"');
189 Str.push_back(',');
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000190
191 // If comment string is '@', e.g. as on ARM - use '%' instead
192 if (strcmp(CommentString, "@") == 0)
Chris Lattner94d8a762009-07-26 07:33:58 +0000193 Str.push_back('%');
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000194 else
Chris Lattner94d8a762009-07-26 07:33:58 +0000195 Str.push_back('@');
asle0d0f482008-07-19 13:14:11 +0000196
Chris Lattner94d8a762009-07-26 07:33:58 +0000197 const char *KindStr;
198 if (Flags & SectionFlags::BSS)
199 KindStr = "nobits";
asle0d0f482008-07-19 13:14:11 +0000200 else
Chris Lattner94d8a762009-07-26 07:33:58 +0000201 KindStr = "progbits";
202
203 Str.append(KindStr, KindStr+strlen(KindStr));
asle0d0f482008-07-19 13:14:11 +0000204
Chris Lattner94d8a762009-07-26 07:33:58 +0000205 if (unsigned entitySize = SectionFlags::getEntitySize(Flags)) {
206 Str.push_back(',');
207 std::string Size = utostr(entitySize);
208 Str.append(Size.begin(), Size.end());
209 }
asle0d0f482008-07-19 13:14:11 +0000210}