blob: 8608d36747ab91bae1c0eab4d9e8493547ba33b0 [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
47const Section*
Evan Chengeb774e12008-08-08 17:56:50 +000048ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
asle0d0f482008-07-19 13:14:11 +000049 SectionKind::Kind Kind = SectionKindForGlobal(GV);
50
51 if (const Function *F = dyn_cast<Function>(GV)) {
52 switch (F->getLinkage()) {
53 default: assert(0 && "Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +000054 case Function::PrivateLinkage:
asle0d0f482008-07-19 13:14:11 +000055 case Function::InternalLinkage:
56 case Function::DLLExportLinkage:
57 case Function::ExternalLinkage:
Anton Korobeynikovf760ffb2008-09-24 22:16:33 +000058 return TextSection;
Duncan Sands19d161f2009-03-07 15:45:40 +000059 case Function::WeakAnyLinkage:
60 case Function::WeakODRLinkage:
61 case Function::LinkOnceAnyLinkage:
62 case Function::LinkOnceODRLinkage:
asle0d0f482008-07-19 13:14:11 +000063 std::string Name = UniqueSectionForGlobal(GV, Kind);
64 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
65 return getNamedSection(Name.c_str(), Flags);
66 }
67 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
Duncan Sands19d161f2009-03-07 15:45:40 +000068 if (GVar->isWeakForLinker()) {
asle0d0f482008-07-19 13:14:11 +000069 std::string Name = UniqueSectionForGlobal(GVar, Kind);
70 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
71 return getNamedSection(Name.c_str(), Flags);
72 } else {
73 switch (Kind) {
74 case SectionKind::Data:
Anton Korobeynikov96e01f12008-07-22 17:09:41 +000075 case SectionKind::SmallData:
Anton Korobeynikovf760ffb2008-09-24 22:16:33 +000076 return DataSection;
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000077 case SectionKind::DataRel:
78 return DataRelSection;
79 case SectionKind::DataRelLocal:
80 return DataRelLocalSection;
81 case SectionKind::DataRelRO:
82 return DataRelROSection;
83 case SectionKind::DataRelROLocal:
84 return DataRelROLocalSection;
asle0d0f482008-07-19 13:14:11 +000085 case SectionKind::BSS:
Anton Korobeynikov96e01f12008-07-22 17:09:41 +000086 case SectionKind::SmallBSS:
asle0d0f482008-07-19 13:14:11 +000087 // ELF targets usually have BSS sections
88 return getBSSSection_();
89 case SectionKind::ROData:
Anton Korobeynikov96e01f12008-07-22 17:09:41 +000090 case SectionKind::SmallROData:
Anton Korobeynikov4d433222008-09-24 22:20:27 +000091 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +000092 case SectionKind::RODataMergeStr:
93 return MergeableStringSection(GVar);
94 case SectionKind::RODataMergeConst:
95 return MergeableConstSection(GVar);
96 case SectionKind::ThreadData:
97 // ELF targets usually support TLS stuff
Anton Korobeynikov9a213e42008-09-24 22:17:06 +000098 return TLSDataSection;
asle0d0f482008-07-19 13:14:11 +000099 case SectionKind::ThreadBSS:
Anton Korobeynikov9a213e42008-09-24 22:17:06 +0000100 return TLSBSSSection;
asle0d0f482008-07-19 13:14:11 +0000101 default:
102 assert(0 && "Unsuported section kind for global");
103 }
104 }
105 } else
106 assert(0 && "Unsupported global");
Devang Patelf3707e82009-01-05 17:31:22 +0000107
108 return NULL;
asle0d0f482008-07-19 13:14:11 +0000109}
110
111const Section*
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000112ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
113 // FIXME: Support data.rel stuff someday
114 return MergeableConstSection(Ty);
115}
116
117const Section*
asle0d0f482008-07-19 13:14:11 +0000118ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikov08f10bc2008-09-24 22:17:27 +0000119 Constant *C = GV->getInitializer();
Anton Korobeynikovfee95e02008-08-07 09:51:02 +0000120 return MergeableConstSection(C->getType());
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000121}
122
123inline const Section*
124ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000125 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000126
127 // FIXME: string here is temporary, until stuff will fully land in.
128 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
129 // currently directly used by asmprinter.
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000130 unsigned Size = TD->getTypePaddedSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000131 if (Size == 4 || Size == 8 || Size == 16) {
132 std::string Name = ".rodata.cst" + utostr(Size);
133
134 return getNamedSection(Name.c_str(),
135 SectionFlags::setEntitySize(SectionFlags::Mergeable,
136 Size));
137 }
138
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000139 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000140}
141
142const Section*
143ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000144 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000145 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000146 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000147
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000148 unsigned Size = TD->getTypePaddedSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000149 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000150 assert(getCStringSection() && "Should have string section prefix");
151
asle0d0f482008-07-19 13:14:11 +0000152 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000153 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000154 if (Align < Size)
155 Align = Size;
156
157 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
158 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
159 SectionFlags::Strings,
160 Size);
161 return getNamedSection(Name.c_str(), Flags);
162 }
163
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000164 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000165}
166
asl27ffd262008-08-16 12:57:07 +0000167std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000168 std::string Flags = ",\"";
169
170 if (!(flags & SectionFlags::Debug))
171 Flags += 'a';
172 if (flags & SectionFlags::Code)
173 Flags += 'x';
174 if (flags & SectionFlags::Writeable)
175 Flags += 'w';
176 if (flags & SectionFlags::Mergeable)
177 Flags += 'M';
178 if (flags & SectionFlags::Strings)
179 Flags += 'S';
180 if (flags & SectionFlags::TLS)
181 Flags += 'T';
Anton Korobeynikov96e01f12008-07-22 17:09:41 +0000182 if (flags & SectionFlags::Small)
183 Flags += 's';
asle0d0f482008-07-19 13:14:11 +0000184
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000185 Flags += "\",";
186
187 // If comment string is '@', e.g. as on ARM - use '%' instead
188 if (strcmp(CommentString, "@") == 0)
189 Flags += '%';
190 else
191 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000192
193 // FIXME: There can be exceptions here
194 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000195 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000196 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000197 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000198
199 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
200 Flags += "," + utostr(entitySize);
201
202 return Flags;
203}