blob: 8f6e96e2751dfec4bba4b7343b2aa5e7dfa1a8fc [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"
asle0d0f482008-07-19 13:14:11 +000021#include "llvm/Target/ELFTargetAsmInfo.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetData.h"
24
25using 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
asle0d0f482008-07-19 13:14:11 +000030 BSSSection_ = getUnnamedSection("\t.bss",
31 SectionFlags::Writeable | 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",
34 SectionFlags::Writeable | SectionFlags::TLS);
35 TLSBSSSection = getNamedSection("\t.tbss",
asle0d0f482008-07-19 13:14:11 +000036 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
37
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000038 DataRelSection = getNamedSection("\t.data.rel", SectionFlags::Writeable);
39 DataRelLocalSection = getNamedSection("\t.data.rel.local",
40 SectionFlags::Writeable);
41 DataRelROSection = getNamedSection("\t.data.rel.ro",
42 SectionFlags::Writeable);
43 DataRelROLocalSection = getNamedSection("\t.data.rel.ro.local",
44 SectionFlags::Writeable);
asle0d0f482008-07-19 13:14:11 +000045}
46
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000047SectionKind::Kind
48ELFTargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
49 SectionKind::Kind Kind = TargetAsmInfo::SectionKindForGlobal(GV);
50
51 if (Kind != SectionKind::Data)
52 return Kind;
53
54 // Decide, whether we need data.rel stuff
55 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
56 if (GVar->hasInitializer()) {
57 Constant *C = GVar->getInitializer();
58 bool isConstant = GVar->isConstant();
59 unsigned Reloc = RelocBehaviour();
60 if (Reloc != Reloc::None && C->ContainsRelocations(Reloc))
Anton Korobeynikov54df41b2009-03-30 17:37:43 +000061 return (C->ContainsRelocations(Reloc::Global) ?
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000062 (isConstant ?
Anton Korobeynikov54df41b2009-03-30 17:37:43 +000063 SectionKind::DataRelRO : SectionKind::DataRel) :
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000064 (isConstant ?
Anton Korobeynikov54df41b2009-03-30 17:37:43 +000065 SectionKind::DataRelROLocal : SectionKind::DataRelLocal));
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000066 }
67
68 return Kind;
69}
70
asle0d0f482008-07-19 13:14:11 +000071const Section*
Evan Chengeb774e12008-08-08 17:56:50 +000072ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
asle0d0f482008-07-19 13:14:11 +000073 SectionKind::Kind Kind = SectionKindForGlobal(GV);
74
75 if (const Function *F = dyn_cast<Function>(GV)) {
76 switch (F->getLinkage()) {
77 default: assert(0 && "Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +000078 case Function::PrivateLinkage:
asle0d0f482008-07-19 13:14:11 +000079 case Function::InternalLinkage:
80 case Function::DLLExportLinkage:
81 case Function::ExternalLinkage:
Anton Korobeynikovf760ffb2008-09-24 22:16:33 +000082 return TextSection;
Duncan Sands19d161f2009-03-07 15:45:40 +000083 case Function::WeakAnyLinkage:
84 case Function::WeakODRLinkage:
85 case Function::LinkOnceAnyLinkage:
86 case Function::LinkOnceODRLinkage:
asle0d0f482008-07-19 13:14:11 +000087 std::string Name = UniqueSectionForGlobal(GV, Kind);
88 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
89 return getNamedSection(Name.c_str(), Flags);
90 }
91 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
Duncan Sands19d161f2009-03-07 15:45:40 +000092 if (GVar->isWeakForLinker()) {
asle0d0f482008-07-19 13:14:11 +000093 std::string Name = UniqueSectionForGlobal(GVar, Kind);
94 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
95 return getNamedSection(Name.c_str(), Flags);
96 } else {
97 switch (Kind) {
98 case SectionKind::Data:
Anton Korobeynikov96e01f12008-07-22 17:09:41 +000099 case SectionKind::SmallData:
Anton Korobeynikovf760ffb2008-09-24 22:16:33 +0000100 return DataSection;
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +0000101 case SectionKind::DataRel:
102 return DataRelSection;
103 case SectionKind::DataRelLocal:
104 return DataRelLocalSection;
105 case SectionKind::DataRelRO:
106 return DataRelROSection;
107 case SectionKind::DataRelROLocal:
108 return DataRelROLocalSection;
asle0d0f482008-07-19 13:14:11 +0000109 case SectionKind::BSS:
Anton Korobeynikov96e01f12008-07-22 17:09:41 +0000110 case SectionKind::SmallBSS:
asle0d0f482008-07-19 13:14:11 +0000111 // ELF targets usually have BSS sections
112 return getBSSSection_();
113 case SectionKind::ROData:
Anton Korobeynikov96e01f12008-07-22 17:09:41 +0000114 case SectionKind::SmallROData:
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000115 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000116 case SectionKind::RODataMergeStr:
117 return MergeableStringSection(GVar);
118 case SectionKind::RODataMergeConst:
119 return MergeableConstSection(GVar);
120 case SectionKind::ThreadData:
121 // ELF targets usually support TLS stuff
Anton Korobeynikov9a213e42008-09-24 22:17:06 +0000122 return TLSDataSection;
asle0d0f482008-07-19 13:14:11 +0000123 case SectionKind::ThreadBSS:
Anton Korobeynikov9a213e42008-09-24 22:17:06 +0000124 return TLSBSSSection;
asle0d0f482008-07-19 13:14:11 +0000125 default:
126 assert(0 && "Unsuported section kind for global");
127 }
128 }
129 } else
130 assert(0 && "Unsupported global");
Devang Patelf3707e82009-01-05 17:31:22 +0000131
132 return NULL;
asle0d0f482008-07-19 13:14:11 +0000133}
134
135const Section*
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000136ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
137 // FIXME: Support data.rel stuff someday
138 return MergeableConstSection(Ty);
139}
140
141const Section*
asle0d0f482008-07-19 13:14:11 +0000142ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikov08f10bc2008-09-24 22:17:27 +0000143 Constant *C = GV->getInitializer();
Anton Korobeynikovfee95e02008-08-07 09:51:02 +0000144 return MergeableConstSection(C->getType());
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000145}
146
147inline const Section*
148ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000149 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000150
151 // FIXME: string here is temporary, until stuff will fully land in.
152 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
153 // currently directly used by asmprinter.
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000154 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000155 if (Size == 4 || Size == 8 || Size == 16) {
156 std::string Name = ".rodata.cst" + utostr(Size);
157
158 return getNamedSection(Name.c_str(),
159 SectionFlags::setEntitySize(SectionFlags::Mergeable,
160 Size));
161 }
162
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000163 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000164}
165
166const Section*
167ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000168 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000169 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000170 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000171
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000172 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000173 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000174 assert(getCStringSection() && "Should have string section prefix");
175
asle0d0f482008-07-19 13:14:11 +0000176 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000177 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000178 if (Align < Size)
179 Align = Size;
180
181 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
182 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
183 SectionFlags::Strings,
184 Size);
185 return getNamedSection(Name.c_str(), Flags);
186 }
187
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000188 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000189}
190
asl27ffd262008-08-16 12:57:07 +0000191std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000192 std::string Flags = ",\"";
193
194 if (!(flags & SectionFlags::Debug))
195 Flags += 'a';
196 if (flags & SectionFlags::Code)
197 Flags += 'x';
198 if (flags & SectionFlags::Writeable)
199 Flags += 'w';
200 if (flags & SectionFlags::Mergeable)
201 Flags += 'M';
202 if (flags & SectionFlags::Strings)
203 Flags += 'S';
204 if (flags & SectionFlags::TLS)
205 Flags += 'T';
Anton Korobeynikov96e01f12008-07-22 17:09:41 +0000206 if (flags & SectionFlags::Small)
207 Flags += 's';
asle0d0f482008-07-19 13:14:11 +0000208
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000209 Flags += "\",";
210
211 // If comment string is '@', e.g. as on ARM - use '%' instead
212 if (strcmp(CommentString, "@") == 0)
213 Flags += '%';
214 else
215 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000216
217 // FIXME: There can be exceptions here
218 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000219 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000220 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000221 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000222
223 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
224 Flags += "," + utostr(entitySize);
225
226 return Flags;
227}