blob: bd2dd0a06445d9130918a98f72db1d8b372fa8a7 [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",
32 SectionFlags::Writeable | 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",
35 SectionFlags::Writeable | SectionFlags::TLS);
36 TLSBSSSection = getNamedSection("\t.tbss",
asle0d0f482008-07-19 13:14:11 +000037 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
38
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +000039 DataRelSection = getNamedSection("\t.data.rel", SectionFlags::Writeable);
40 DataRelLocalSection = getNamedSection("\t.data.rel.local",
41 SectionFlags::Writeable);
42 DataRelROSection = getNamedSection("\t.data.rel.ro",
43 SectionFlags::Writeable);
44 DataRelROLocalSection = getNamedSection("\t.data.rel.ro.local",
45 SectionFlags::Writeable);
asle0d0f482008-07-19 13:14:11 +000046}
47
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000048SectionKind::Kind
49ELFTargetAsmInfo::SectionKindForGlobal(const GlobalValue *GV) const {
50 SectionKind::Kind Kind = TargetAsmInfo::SectionKindForGlobal(GV);
51
52 if (Kind != SectionKind::Data)
53 return Kind;
54
55 // Decide, whether we need data.rel stuff
56 const GlobalVariable* GVar = dyn_cast<GlobalVariable>(GV);
57 if (GVar->hasInitializer()) {
58 Constant *C = GVar->getInitializer();
59 bool isConstant = GVar->isConstant();
60 unsigned Reloc = RelocBehaviour();
61 if (Reloc != Reloc::None && C->ContainsRelocations(Reloc))
Anton Korobeynikov54df41b2009-03-30 17:37:43 +000062 return (C->ContainsRelocations(Reloc::Global) ?
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000063 (isConstant ?
Anton Korobeynikov54df41b2009-03-30 17:37:43 +000064 SectionKind::DataRelRO : SectionKind::DataRel) :
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000065 (isConstant ?
Anton Korobeynikov54df41b2009-03-30 17:37:43 +000066 SectionKind::DataRelROLocal : SectionKind::DataRelLocal));
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000067 }
68
69 return Kind;
70}
71
asle0d0f482008-07-19 13:14:11 +000072const Section*
Evan Chengeb774e12008-08-08 17:56:50 +000073ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
asle0d0f482008-07-19 13:14:11 +000074 SectionKind::Kind Kind = SectionKindForGlobal(GV);
75
76 if (const Function *F = dyn_cast<Function>(GV)) {
77 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +000078 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +000079 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +000080 case Function::LinkerPrivateLinkage:
asle0d0f482008-07-19 13:14:11 +000081 case Function::InternalLinkage:
82 case Function::DLLExportLinkage:
83 case Function::ExternalLinkage:
Anton Korobeynikovf760ffb2008-09-24 22:16:33 +000084 return TextSection;
Duncan Sands19d161f2009-03-07 15:45:40 +000085 case Function::WeakAnyLinkage:
86 case Function::WeakODRLinkage:
87 case Function::LinkOnceAnyLinkage:
88 case Function::LinkOnceODRLinkage:
asle0d0f482008-07-19 13:14:11 +000089 std::string Name = UniqueSectionForGlobal(GV, Kind);
90 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
91 return getNamedSection(Name.c_str(), Flags);
92 }
93 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
Duncan Sands19d161f2009-03-07 15:45:40 +000094 if (GVar->isWeakForLinker()) {
asle0d0f482008-07-19 13:14:11 +000095 std::string Name = UniqueSectionForGlobal(GVar, Kind);
96 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
97 return getNamedSection(Name.c_str(), Flags);
98 } else {
99 switch (Kind) {
Chris Lattnerd49a0752009-07-21 21:29:08 +0000100 case SectionKind::Data:
101 case SectionKind::SmallData:
Anton Korobeynikovf760ffb2008-09-24 22:16:33 +0000102 return DataSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000103 case SectionKind::DataRel:
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +0000104 return DataRelSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000105 case SectionKind::DataRelLocal:
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +0000106 return DataRelLocalSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000107 case SectionKind::DataRelRO:
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +0000108 return DataRelROSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000109 case SectionKind::DataRelROLocal:
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +0000110 return DataRelROLocalSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000111 case SectionKind::BSS:
112 case SectionKind::SmallBSS:
asle0d0f482008-07-19 13:14:11 +0000113 // ELF targets usually have BSS sections
114 return getBSSSection_();
Chris Lattnerd49a0752009-07-21 21:29:08 +0000115 case SectionKind::ROData:
116 case SectionKind::SmallROData:
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000117 return getReadOnlySection();
Chris Lattnerd49a0752009-07-21 21:29:08 +0000118 case SectionKind::RODataMergeStr:
asle0d0f482008-07-19 13:14:11 +0000119 return MergeableStringSection(GVar);
Chris Lattnerd49a0752009-07-21 21:29:08 +0000120 case SectionKind::RODataMergeConst:
121 return MergeableConstSection(GVar->getInitializer()->getType());
122 case SectionKind::ThreadData:
asle0d0f482008-07-19 13:14:11 +0000123 // ELF targets usually support TLS stuff
Anton Korobeynikov9a213e42008-09-24 22:17:06 +0000124 return TLSDataSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000125 case SectionKind::ThreadBSS:
Anton Korobeynikov9a213e42008-09-24 22:17:06 +0000126 return TLSBSSSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000127 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000128 llvm_unreachable("Unsuported section kind for global");
asle0d0f482008-07-19 13:14:11 +0000129 }
130 }
131 } else
Edwin Törökbd448e32009-07-14 16:55:14 +0000132 llvm_unreachable("Unsupported global");
Devang Patelf3707e82009-01-05 17:31:22 +0000133
134 return NULL;
asle0d0f482008-07-19 13:14:11 +0000135}
136
137const Section*
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000138ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
139 // FIXME: Support data.rel stuff someday
140 return MergeableConstSection(Ty);
141}
142
143const Section*
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000144ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000145 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000146
147 // FIXME: string here is temporary, until stuff will fully land in.
148 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
149 // currently directly used by asmprinter.
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000150 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000151 if (Size == 4 || Size == 8 || Size == 16) {
152 std::string Name = ".rodata.cst" + utostr(Size);
153
154 return getNamedSection(Name.c_str(),
155 SectionFlags::setEntitySize(SectionFlags::Mergeable,
156 Size));
157 }
158
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000159 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000160}
161
162const Section*
163ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000164 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000165 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000166 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000167
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000168 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000169 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000170 assert(getCStringSection() && "Should have string section prefix");
171
asle0d0f482008-07-19 13:14:11 +0000172 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000173 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000174 if (Align < Size)
175 Align = Size;
176
177 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
178 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
179 SectionFlags::Strings,
180 Size);
181 return getNamedSection(Name.c_str(), Flags);
182 }
183
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000184 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000185}
186
asl27ffd262008-08-16 12:57:07 +0000187std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000188 std::string Flags = ",\"";
189
190 if (!(flags & SectionFlags::Debug))
191 Flags += 'a';
192 if (flags & SectionFlags::Code)
193 Flags += 'x';
194 if (flags & SectionFlags::Writeable)
195 Flags += 'w';
196 if (flags & SectionFlags::Mergeable)
197 Flags += 'M';
198 if (flags & SectionFlags::Strings)
199 Flags += 'S';
200 if (flags & SectionFlags::TLS)
201 Flags += 'T';
Anton Korobeynikov96e01f12008-07-22 17:09:41 +0000202 if (flags & SectionFlags::Small)
203 Flags += 's';
asle0d0f482008-07-19 13:14:11 +0000204
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000205 Flags += "\",";
206
207 // If comment string is '@', e.g. as on ARM - use '%' instead
208 if (strcmp(CommentString, "@") == 0)
209 Flags += '%';
210 else
211 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000212
213 // FIXME: There can be exceptions here
214 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000215 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000216 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000217 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000218
219 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
220 Flags += "," + utostr(entitySize);
221
222 return Flags;
223}