blob: 880dfbc0a9365c1cf0c6cd5a6768980c670bbcee [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 Lattnercc195212009-07-25 23:21:55 +000052 switch (Kind.getKind()) {
Chris Lattner3a2a55a2009-07-26 03:06:11 +000053 default: llvm_unreachable("Unknown section kind");
54 case SectionKind::Text: return TextSection;
55 case SectionKind::BSS: return getBSSSection_();
56 case SectionKind::Data: return DataSection;
57 case SectionKind::DataRel: return DataRelSection;
58 case SectionKind::DataRelLocal: return DataRelLocalSection;
59 case SectionKind::DataRelRO: return DataRelROSection;
60 case SectionKind::DataRelROLocal: return DataRelROLocalSection;
61 case SectionKind::ROData: return getReadOnlySection();
Chris Lattnera06e19b2009-07-24 05:01:55 +000062 case SectionKind::RODataMergeStr:
Chris Lattner3a2a55a2009-07-26 03:06:11 +000063 return MergeableStringSection(cast<GlobalVariable>(GV));
Chris Lattnera06e19b2009-07-24 05:01:55 +000064 case SectionKind::RODataMergeConst: {
Chris Lattner3a2a55a2009-07-26 03:06:11 +000065 const Type *Ty = cast<GlobalVariable>(GV)->getInitializer()->getType();
Chris Lattnera06e19b2009-07-24 05:01:55 +000066 const TargetData *TD = TM.getTargetData();
67 return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
68 }
69 case SectionKind::ThreadData:
Chris Lattnera06e19b2009-07-24 05:01:55 +000070 return TLSDataSection;
71 case SectionKind::ThreadBSS:
72 return TLSBSSSection;
Chris Lattnere09c7d72009-07-24 04:52:38 +000073 }
asle0d0f482008-07-19 13:14:11 +000074}
75
Chris Lattner680c6f62009-07-22 00:28:43 +000076/// getSectionForMergableConstant - Given a mergable constant with the
77/// specified size and relocation information, return a section that it
78/// should be placed in.
79const Section *
80ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
81 unsigned ReloInfo) const {
82 // FIXME: IF this global requires a relocation, can we really put it in
83 // rodata??? This should check ReloInfo like darwin.
84
85 const char *SecName = 0;
86 switch (Size) {
87 default: break;
88 case 4: SecName = ".rodata.cst4"; break;
89 case 8: SecName = ".rodata.cst8"; break;
90 case 16: SecName = ".rodata.cst16"; break;
91 }
92
93 if (SecName)
94 return getNamedSection(SecName,
Duncan Sands5bc722e2009-07-22 10:35:05 +000095 SectionFlags::setEntitySize(SectionFlags::Mergeable,
Chris Lattner680c6f62009-07-22 00:28:43 +000096 Size));
97
98 return getReadOnlySection(); // .rodata
Anton Korobeynikovfc54db12008-08-07 09:50:34 +000099}
100
Chris Lattnerdfd15982009-07-24 17:02:17 +0000101/// getFlagsForNamedSection - If this target wants to be able to infer
102/// section flags based on the name of the section specified for a global
103/// variable, it can implement this.
104unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
105 unsigned Flags = 0;
106 if (Name[0] != '.') return 0;
107
108 // Some lame default implementation based on some magic section names.
109 if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
110 strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
111 strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
112 strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
113 Flags |= SectionFlags::BSS;
114 else if (strcmp(Name, ".tdata") == 0 ||
115 strncmp(Name, ".tdata.", 7) == 0 ||
116 strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
117 strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
118 Flags |= SectionFlags::TLS;
119 else if (strcmp(Name, ".tbss") == 0 ||
120 strncmp(Name, ".tbss.", 6) == 0 ||
121 strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
122 strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
123 Flags |= SectionFlags::BSS | SectionFlags::TLS;
124
125 return Flags;
126}
127
Chris Lattner680c6f62009-07-22 00:28:43 +0000128
Chris Lattner4c530ee2009-07-25 18:57:34 +0000129
130const char *
Chris Lattnercc195212009-07-25 23:21:55 +0000131ELFTargetAsmInfo::getSectionPrefixForUniqueGlobal(SectionKind Kind) const{
132 switch (Kind.getKind()) {
Chris Lattner4c530ee2009-07-25 18:57:34 +0000133 default: llvm_unreachable("Unknown section kind");
134 case SectionKind::Text: return ".gnu.linkonce.t.";
135 case SectionKind::Data: return ".gnu.linkonce.d.";
136 case SectionKind::DataRel: return ".gnu.linkonce.d.rel.";
137 case SectionKind::DataRelLocal: return ".gnu.linkonce.d.rel.local.";
138 case SectionKind::DataRelRO: return ".gnu.linkonce.d.rel.ro.";
139 case SectionKind::DataRelROLocal: return ".gnu.linkonce.d.rel.ro.local.";
140 case SectionKind::BSS: return ".gnu.linkonce.b.";
141 case SectionKind::ROData:
142 case SectionKind::RODataMergeConst:
143 case SectionKind::RODataMergeStr: return ".gnu.linkonce.r.";
144 case SectionKind::ThreadData: return ".gnu.linkonce.td.";
145 case SectionKind::ThreadBSS: return ".gnu.linkonce.tb.";
146 }
147}
148
149
150
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000151const Section*
asle0d0f482008-07-19 13:14:11 +0000152ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000153 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000154 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000155 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000156
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000157 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000158 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000159 assert(getCStringSection() && "Should have string section prefix");
160
asle0d0f482008-07-19 13:14:11 +0000161 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000162 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000163 if (Align < Size)
164 Align = Size;
165
166 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
167 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
168 SectionFlags::Strings,
169 Size);
170 return getNamedSection(Name.c_str(), Flags);
171 }
172
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000173 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000174}
175
asl27ffd262008-08-16 12:57:07 +0000176std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000177 std::string Flags = ",\"";
178
179 if (!(flags & SectionFlags::Debug))
180 Flags += 'a';
181 if (flags & SectionFlags::Code)
182 Flags += 'x';
Chris Lattner4c530ee2009-07-25 18:57:34 +0000183 if (flags & SectionFlags::Writable)
asle0d0f482008-07-19 13:14:11 +0000184 Flags += 'w';
185 if (flags & SectionFlags::Mergeable)
186 Flags += 'M';
187 if (flags & SectionFlags::Strings)
188 Flags += 'S';
189 if (flags & SectionFlags::TLS)
190 Flags += 'T';
asle0d0f482008-07-19 13:14:11 +0000191
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000192 Flags += "\",";
193
194 // If comment string is '@', e.g. as on ARM - use '%' instead
195 if (strcmp(CommentString, "@") == 0)
196 Flags += '%';
197 else
198 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000199
200 // FIXME: There can be exceptions here
201 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000202 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000203 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000204 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000205
206 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
207 Flags += "," + utostr(entitySize);
208
209 return Flags;
210}