blob: 3a2a00c6582cfa0ee754fb197e2f0c5e20aafc70 [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
38}
39
40const Section*
Evan Chengeb774e12008-08-08 17:56:50 +000041ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
asle0d0f482008-07-19 13:14:11 +000042 SectionKind::Kind Kind = SectionKindForGlobal(GV);
43
44 if (const Function *F = dyn_cast<Function>(GV)) {
45 switch (F->getLinkage()) {
46 default: assert(0 && "Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +000047 case Function::PrivateLinkage:
asle0d0f482008-07-19 13:14:11 +000048 case Function::InternalLinkage:
49 case Function::DLLExportLinkage:
50 case Function::ExternalLinkage:
Anton Korobeynikovf760ffb2008-09-24 22:16:33 +000051 return TextSection;
Duncan Sands19d161f2009-03-07 15:45:40 +000052 case Function::WeakAnyLinkage:
53 case Function::WeakODRLinkage:
54 case Function::LinkOnceAnyLinkage:
55 case Function::LinkOnceODRLinkage:
asle0d0f482008-07-19 13:14:11 +000056 std::string Name = UniqueSectionForGlobal(GV, Kind);
57 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
58 return getNamedSection(Name.c_str(), Flags);
59 }
60 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
Duncan Sands19d161f2009-03-07 15:45:40 +000061 if (GVar->isWeakForLinker()) {
asle0d0f482008-07-19 13:14:11 +000062 std::string Name = UniqueSectionForGlobal(GVar, Kind);
63 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
64 return getNamedSection(Name.c_str(), Flags);
65 } else {
66 switch (Kind) {
67 case SectionKind::Data:
Anton Korobeynikov96e01f12008-07-22 17:09:41 +000068 case SectionKind::SmallData:
Anton Korobeynikovf760ffb2008-09-24 22:16:33 +000069 return DataSection;
asle0d0f482008-07-19 13:14:11 +000070 case SectionKind::BSS:
Anton Korobeynikov96e01f12008-07-22 17:09:41 +000071 case SectionKind::SmallBSS:
asle0d0f482008-07-19 13:14:11 +000072 // ELF targets usually have BSS sections
73 return getBSSSection_();
74 case SectionKind::ROData:
Anton Korobeynikov96e01f12008-07-22 17:09:41 +000075 case SectionKind::SmallROData:
Anton Korobeynikov4d433222008-09-24 22:20:27 +000076 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +000077 case SectionKind::RODataMergeStr:
78 return MergeableStringSection(GVar);
79 case SectionKind::RODataMergeConst:
80 return MergeableConstSection(GVar);
81 case SectionKind::ThreadData:
82 // ELF targets usually support TLS stuff
Anton Korobeynikov9a213e42008-09-24 22:17:06 +000083 return TLSDataSection;
asle0d0f482008-07-19 13:14:11 +000084 case SectionKind::ThreadBSS:
Anton Korobeynikov9a213e42008-09-24 22:17:06 +000085 return TLSBSSSection;
asle0d0f482008-07-19 13:14:11 +000086 default:
87 assert(0 && "Unsuported section kind for global");
88 }
89 }
90 } else
91 assert(0 && "Unsupported global");
Devang Patelf3707e82009-01-05 17:31:22 +000092
93 return NULL;
asle0d0f482008-07-19 13:14:11 +000094}
95
96const Section*
Anton Korobeynikovfc54db12008-08-07 09:50:34 +000097ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
98 // FIXME: Support data.rel stuff someday
99 return MergeableConstSection(Ty);
100}
101
102const Section*
asle0d0f482008-07-19 13:14:11 +0000103ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikov08f10bc2008-09-24 22:17:27 +0000104 Constant *C = GV->getInitializer();
Anton Korobeynikovfee95e02008-08-07 09:51:02 +0000105 return MergeableConstSection(C->getType());
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000106}
107
108inline const Section*
109ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000110 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000111
112 // FIXME: string here is temporary, until stuff will fully land in.
113 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
114 // currently directly used by asmprinter.
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000115 unsigned Size = TD->getTypePaddedSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000116 if (Size == 4 || Size == 8 || Size == 16) {
117 std::string Name = ".rodata.cst" + utostr(Size);
118
119 return getNamedSection(Name.c_str(),
120 SectionFlags::setEntitySize(SectionFlags::Mergeable,
121 Size));
122 }
123
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000124 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000125}
126
127const Section*
128ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000129 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000130 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000131 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000132
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000133 unsigned Size = TD->getTypePaddedSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000134 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000135 assert(getCStringSection() && "Should have string section prefix");
136
asle0d0f482008-07-19 13:14:11 +0000137 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000138 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000139 if (Align < Size)
140 Align = Size;
141
142 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
143 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
144 SectionFlags::Strings,
145 Size);
146 return getNamedSection(Name.c_str(), Flags);
147 }
148
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000149 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000150}
151
asl27ffd262008-08-16 12:57:07 +0000152std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000153 std::string Flags = ",\"";
154
155 if (!(flags & SectionFlags::Debug))
156 Flags += 'a';
157 if (flags & SectionFlags::Code)
158 Flags += 'x';
159 if (flags & SectionFlags::Writeable)
160 Flags += 'w';
161 if (flags & SectionFlags::Mergeable)
162 Flags += 'M';
163 if (flags & SectionFlags::Strings)
164 Flags += 'S';
165 if (flags & SectionFlags::TLS)
166 Flags += 'T';
Anton Korobeynikov96e01f12008-07-22 17:09:41 +0000167 if (flags & SectionFlags::Small)
168 Flags += 's';
asle0d0f482008-07-19 13:14:11 +0000169
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000170 Flags += "\",";
171
172 // If comment string is '@', e.g. as on ARM - use '%' instead
173 if (strcmp(CommentString, "@") == 0)
174 Flags += '%';
175 else
176 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000177
178 // FIXME: There can be exceptions here
179 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000180 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000181 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000182 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000183
184 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
185 Flags += "," + utostr(entitySize);
186
187 return Flags;
188}