blob: 82c993b685a8230f60f002cd1153d13a42d6aaf9 [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);
Chris Lattner52954842009-07-26 06:16:11 +000046
47 MergableConst4Section = getNamedSection(".rodata.cst4",
48 SectionFlags::setEntitySize(SectionFlags::Mergeable, 4));
49 MergableConst8Section = getNamedSection(".rodata.cst8",
50 SectionFlags::setEntitySize(SectionFlags::Mergeable, 8));
51 MergableConst16Section = getNamedSection(".rodata.cst16",
52 SectionFlags::setEntitySize(SectionFlags::Mergeable, 16));
asle0d0f482008-07-19 13:14:11 +000053}
54
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000055
asle0d0f482008-07-19 13:14:11 +000056const Section*
Chris Lattnerbb0c9bf2009-07-24 18:42:53 +000057ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnercc195212009-07-25 23:21:55 +000058 SectionKind Kind) const {
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +000059 if (Kind.isText()) return TextSection;
60 if (Kind.isMergableCString())
Chris Lattner3a2a55a2009-07-26 03:06:11 +000061 return MergeableStringSection(cast<GlobalVariable>(GV));
Chris Lattner2a7dd7d2009-07-26 06:11:33 +000062
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +000063 if (Kind.isMergableConst()) {
Chris Lattner52954842009-07-26 06:16:11 +000064 if (Kind.isMergableConst4())
65 return MergableConst4Section;
66 if (Kind.isMergableConst8())
67 return MergableConst8Section;
68 if (Kind.isMergableConst16())
69 return MergableConst16Section;
70 return ReadOnlySection; // .const
Chris Lattnera06e19b2009-07-24 05:01:55 +000071 }
Chris Lattner52954842009-07-26 06:16:11 +000072
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +000073 if (Kind.isReadOnly()) return getReadOnlySection();
74
75
76 if (Kind.isThreadData()) return TLSDataSection;
77 if (Kind.isThreadBSS()) return TLSBSSSection;
78
79 if (Kind.isBSS()) return getBSSSection_();
80
81
82 if (Kind.isDataNoRel()) return DataSection;
83 if (Kind.isDataRelLocal()) return DataRelLocalSection;
84 if (Kind.isDataRel()) return DataRelSection;
85 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
86
87 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
88 return DataRelROSection;
asle0d0f482008-07-19 13:14:11 +000089}
90
Chris Lattner680c6f62009-07-22 00:28:43 +000091/// getSectionForMergableConstant - Given a mergable constant with the
92/// specified size and relocation information, return a section that it
93/// should be placed in.
94const Section *
95ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
96 unsigned ReloInfo) const {
Chris Lattner07203df2009-07-26 05:55:20 +000097 // If this constant pool entry has relocations, stick it into a relocatable
98 // section.
99 if (ReloInfo == 2)
100 return DataRelROSection;
101 if (ReloInfo == 1)
102 return DataRelROLocalSection;
103
Chris Lattner680c6f62009-07-22 00:28:43 +0000104 switch (Size) {
Chris Lattner52954842009-07-26 06:16:11 +0000105 default: return ReadOnlySection; // .rodata
106 case 4: return MergableConst4Section;
107 case 8: return MergableConst8Section;
108 case 16: return MergableConst16Section;
Chris Lattner680c6f62009-07-22 00:28:43 +0000109 }
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000110}
111
Chris Lattnerdfd15982009-07-24 17:02:17 +0000112/// getFlagsForNamedSection - If this target wants to be able to infer
113/// section flags based on the name of the section specified for a global
114/// variable, it can implement this.
115unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
116 unsigned Flags = 0;
117 if (Name[0] != '.') return 0;
118
119 // Some lame default implementation based on some magic section names.
120 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
121 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
122 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
123 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
124 Flags |= SectionFlags::BSS;
125 else if (strcmp(Name, ".tdata") == 0 ||
126 strncmp(Name, ".tdata.", 7) == 0 ||
127 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
128 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
129 Flags |= SectionFlags::TLS;
130 else if (strcmp(Name, ".tbss") == 0 ||
131 strncmp(Name, ".tbss.", 6) == 0 ||
132 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
133 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
134 Flags |= SectionFlags::BSS | SectionFlags::TLS;
135
136 return Flags;
137}
138
Chris Lattner4c530ee2009-07-25 18:57:34 +0000139const char *
Chris Lattnercc195212009-07-25 23:21:55 +0000140ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind Kind) const{
Chris Lattnerdc9d8cf2009-07-26 05:44:20 +0000141 if (Kind.isText()) return ".gnu.linkonce.t.";
142 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
143
144 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
145 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
146
147 if (Kind.isBSS()) return ".gnu.linkonce.b.";
148 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
149 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
150 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
151 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
152
153 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
154 return ".gnu.linkonce.d.rel.ro.";
Chris Lattner4c530ee2009-07-25 18:57:34 +0000155}
156
157
158
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000159const Section*
asle0d0f482008-07-19 13:14:11 +0000160ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000161 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000162 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000163 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000164
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000165 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000166 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000167 assert(getCStringSection() && "Should have string section prefix");
168
asle0d0f482008-07-19 13:14:11 +0000169 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000170 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000171 if (Align < Size)
172 Align = Size;
173
174 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
175 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
176 SectionFlags::Strings,
177 Size);
178 return getNamedSection(Name.c_str(), Flags);
179 }
180
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000181 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000182}
183
asl27ffd262008-08-16 12:57:07 +0000184std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000185 std::string Flags = ",\"";
186
187 if (!(flags & SectionFlags::Debug))
188 Flags += 'a';
189 if (flags & SectionFlags::Code)
190 Flags += 'x';
Chris Lattner4c530ee2009-07-25 18:57:34 +0000191 if (flags & SectionFlags::Writable)
asle0d0f482008-07-19 13:14:11 +0000192 Flags += 'w';
193 if (flags & SectionFlags::Mergeable)
194 Flags += 'M';
195 if (flags & SectionFlags::Strings)
196 Flags += 'S';
197 if (flags & SectionFlags::TLS)
198 Flags += 'T';
asle0d0f482008-07-19 13:14:11 +0000199
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000200 Flags += "\",";
201
202 // If comment string is '@', e.g. as on ARM - use '%' instead
203 if (strcmp(CommentString, "@") == 0)
204 Flags += '%';
205 else
206 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000207
208 // FIXME: There can be exceptions here
209 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000210 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000211 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000212 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000213
214 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
215 Flags += "," + utostr(entitySize);
216
217 return Flags;
218}