blob: da8ed73852be2cab960fc8b1f6fb4e346b854668 [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 Lattnerd9509d12009-07-26 01:44:55 +000052 if (isa<Function>(GV))
53 return TextSection;
Chris Lattnera06e19b2009-07-24 05:01:55 +000054
55 const GlobalVariable *GVar = cast<GlobalVariable>(GV);
Chris Lattnercc195212009-07-25 23:21:55 +000056 switch (Kind.getKind()) {
Chris Lattnera06e19b2009-07-24 05:01:55 +000057 default: llvm_unreachable("Unsuported section kind for global");
Chris Lattnerd398a8d2009-07-24 19:15:47 +000058 case SectionKind::BSS:
59 return getBSSSection_();
Chris Lattnera06e19b2009-07-24 05:01:55 +000060 case SectionKind::Data:
61 case SectionKind::DataRel:
62 return DataRelSection;
63 case SectionKind::DataRelLocal:
64 return DataRelLocalSection;
65 case SectionKind::DataRelRO:
66 return DataRelROSection;
67 case SectionKind::DataRelROLocal:
68 return DataRelROLocalSection;
Chris Lattnera06e19b2009-07-24 05:01:55 +000069 case SectionKind::ROData:
70 return getReadOnlySection();
71 case SectionKind::RODataMergeStr:
72 return MergeableStringSection(GVar);
73 case SectionKind::RODataMergeConst: {
74 const Type *Ty = GVar->getInitializer()->getType();
75 const TargetData *TD = TM.getTargetData();
76 return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
77 }
78 case SectionKind::ThreadData:
79 // ELF targets usually support TLS stuff
80 return TLSDataSection;
81 case SectionKind::ThreadBSS:
82 return TLSBSSSection;
Chris Lattnere09c7d72009-07-24 04:52:38 +000083 }
asle0d0f482008-07-19 13:14:11 +000084}
85
Chris Lattner680c6f62009-07-22 00:28:43 +000086/// getSectionForMergableConstant - Given a mergable constant with the
87/// specified size and relocation information, return a section that it
88/// should be placed in.
89const Section *
90ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
91 unsigned ReloInfo) const {
92 // FIXME: IF this global requires a relocation, can we really put it in
93 // rodata??? This should check ReloInfo like darwin.
94
95 const char *SecName = 0;
96 switch (Size) {
97 default: break;
98 case 4: SecName = ".rodata.cst4"; break;
99 case 8: SecName = ".rodata.cst8"; break;
100 case 16: SecName = ".rodata.cst16"; break;
101 }
102
103 if (SecName)
104 return getNamedSection(SecName,
Duncan Sands5bc722e2009-07-22 10:35:05 +0000105 SectionFlags::setEntitySize(SectionFlags::Mergeable,
Chris Lattner680c6f62009-07-22 00:28:43 +0000106 Size));
107
108 return getReadOnlySection(); // .rodata
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000109}
110
Chris Lattnerdfd15982009-07-24 17:02:17 +0000111/// getFlagsForNamedSection - If this target wants to be able to infer
112/// section flags based on the name of the section specified for a global
113/// variable, it can implement this.
114unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
115 unsigned Flags = 0;
116 if (Name[0] != '.') return 0;
117
118 // Some lame default implementation based on some magic section names.
119 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
120 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
121 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
122 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
123 Flags |= SectionFlags::BSS;
124 else if (strcmp(Name, ".tdata") == 0 ||
125 strncmp(Name, ".tdata.", 7) == 0 ||
126 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
127 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
128 Flags |= SectionFlags::TLS;
129 else if (strcmp(Name, ".tbss") == 0 ||
130 strncmp(Name, ".tbss.", 6) == 0 ||
131 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
132 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
133 Flags |= SectionFlags::BSS | SectionFlags::TLS;
134
135 return Flags;
136}
137
Chris Lattner680c6f62009-07-22 00:28:43 +0000138
Chris Lattner4c530ee2009-07-25 18:57:34 +0000139
140const char *
Chris Lattnercc195212009-07-25 23:21:55 +0000141ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind Kind) const{
142 switch (Kind.getKind()) {
Chris Lattner4c530ee2009-07-25 18:57:34 +0000143 default: llvm_unreachable("Unknown section kind");
144 case SectionKind::Text: return ".gnu.linkonce.t.";
145 case SectionKind::Data: return ".gnu.linkonce.d.";
146 case SectionKind::DataRel: return ".gnu.linkonce.d.rel.";
147 case SectionKind::DataRelLocal: return ".gnu.linkonce.d.rel.local.";
148 case SectionKind::DataRelRO: return ".gnu.linkonce.d.rel.ro.";
149 case SectionKind::DataRelROLocal: return ".gnu.linkonce.d.rel.ro.local.";
150 case SectionKind::BSS: return ".gnu.linkonce.b.";
151 case SectionKind::ROData:
152 case SectionKind::RODataMergeConst:
153 case SectionKind::RODataMergeStr: return ".gnu.linkonce.r.";
154 case SectionKind::ThreadData: return ".gnu.linkonce.td.";
155 case SectionKind::ThreadBSS: return ".gnu.linkonce.tb.";
156 }
157}
158
159
160
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000161const Section*
asle0d0f482008-07-19 13:14:11 +0000162ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000163 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000164 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000165 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000166
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000167 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000168 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000169 assert(getCStringSection() && "Should have string section prefix");
170
asle0d0f482008-07-19 13:14:11 +0000171 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000172 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000173 if (Align < Size)
174 Align = Size;
175
176 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
177 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
178 SectionFlags::Strings,
179 Size);
180 return getNamedSection(Name.c_str(), Flags);
181 }
182
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000183 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000184}
185
asl27ffd262008-08-16 12:57:07 +0000186std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000187 std::string Flags = ",\"";
188
189 if (!(flags & SectionFlags::Debug))
190 Flags += 'a';
191 if (flags & SectionFlags::Code)
192 Flags += 'x';
Chris Lattner4c530ee2009-07-25 18:57:34 +0000193 if (flags & SectionFlags::Writable)
asle0d0f482008-07-19 13:14:11 +0000194 Flags += 'w';
195 if (flags & SectionFlags::Mergeable)
196 Flags += 'M';
197 if (flags & SectionFlags::Strings)
198 Flags += 'S';
199 if (flags & SectionFlags::TLS)
200 Flags += 'T';
asle0d0f482008-07-19 13:14:11 +0000201
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000202 Flags += "\",";
203
204 // If comment string is '@', e.g. as on ARM - use '%' instead
205 if (strcmp(CommentString, "@") == 0)
206 Flags += '%';
207 else
208 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000209
210 // FIXME: There can be exceptions here
211 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000212 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000213 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000214 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000215
216 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
217 Flags += "," + utostr(entitySize);
218
219 return Flags;
220}