blob: cf4e3a2f1484292dc5416370b0e1a883ce0cfa6d [file] [log] [blame]
Anton Korobeynikovdebe34b2008-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 Korobeynikov93cacf12008-08-07 09:50:34 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikovdebe34b2008-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 Gohman8f092252008-11-03 18:22:42 +000028ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM)
29 : TargetAsmInfo(TM) {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000030
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000031 BSSSection_ = getUnnamedSection("\t.bss",
32 SectionFlags::Writeable | SectionFlags::BSS);
Anton Korobeynikov00181a32008-09-24 22:20:27 +000033 ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None);
Anton Korobeynikov36133dd2008-09-24 22:17:06 +000034 TLSDataSection = getNamedSection("\t.tdata",
35 SectionFlags::Writeable | SectionFlags::TLS);
36 TLSBSSSection = getNamedSection("\t.tbss",
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000037 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
38
Anton Korobeynikov71a7c6c2009-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);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000046}
47
Anton Korobeynikovfca82de2009-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);
Chris Lattner7cf12c72009-07-22 00:05:44 +000057 if (GVar->hasInitializer() && TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikovfca82de2009-03-30 15:27:43 +000058 Constant *C = GVar->getInitializer();
59 bool isConstant = GVar->isConstant();
Chris Lattner97d2cae2009-07-21 23:47:11 +000060
Chris Lattner97d2cae2009-07-21 23:47:11 +000061 // By default - all relocations in PIC mode would force symbol to be
62 // placed in r/w section.
Chris Lattner7cf12c72009-07-22 00:05:44 +000063 switch (C->getRelocationInfo()) {
64 default: break;
65 case 1:
66 return isConstant ? SectionKind::DataRelROLocal :
67 SectionKind::DataRelLocal;
68 case 2:
69 return isConstant ? SectionKind::DataRelRO : SectionKind::DataRel;
70 }
Anton Korobeynikovfca82de2009-03-30 15:27:43 +000071 }
72
73 return Kind;
74}
75
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000076const Section*
Evan Cheng42ccc212008-08-08 17:56:50 +000077ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000078 SectionKind::Kind Kind = SectionKindForGlobal(GV);
79
80 if (const Function *F = dyn_cast<Function>(GV)) {
81 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000082 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +000083 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +000084 case Function::LinkerPrivateLinkage:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000085 case Function::InternalLinkage:
86 case Function::DLLExportLinkage:
87 case Function::ExternalLinkage:
Anton Korobeynikov0b501d12008-09-24 22:16:33 +000088 return TextSection;
Duncan Sands667d4b82009-03-07 15:45:40 +000089 case Function::WeakAnyLinkage:
90 case Function::WeakODRLinkage:
91 case Function::LinkOnceAnyLinkage:
92 case Function::LinkOnceODRLinkage:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000093 std::string Name = UniqueSectionForGlobal(GV, Kind);
94 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
95 return getNamedSection(Name.c_str(), Flags);
96 }
97 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
Duncan Sands667d4b82009-03-07 15:45:40 +000098 if (GVar->isWeakForLinker()) {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000099 std::string Name = UniqueSectionForGlobal(GVar, Kind);
100 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
101 return getNamedSection(Name.c_str(), Flags);
102 } else {
103 switch (Kind) {
Chris Lattner7886ae92009-07-21 21:29:08 +0000104 case SectionKind::Data:
105 case SectionKind::SmallData:
Anton Korobeynikov0b501d12008-09-24 22:16:33 +0000106 return DataSection;
Chris Lattner7886ae92009-07-21 21:29:08 +0000107 case SectionKind::DataRel:
Anton Korobeynikov71a7c6c2009-03-30 15:27:03 +0000108 return DataRelSection;
Chris Lattner7886ae92009-07-21 21:29:08 +0000109 case SectionKind::DataRelLocal:
Anton Korobeynikov71a7c6c2009-03-30 15:27:03 +0000110 return DataRelLocalSection;
Chris Lattner7886ae92009-07-21 21:29:08 +0000111 case SectionKind::DataRelRO:
Anton Korobeynikov71a7c6c2009-03-30 15:27:03 +0000112 return DataRelROSection;
Chris Lattner7886ae92009-07-21 21:29:08 +0000113 case SectionKind::DataRelROLocal:
Anton Korobeynikov71a7c6c2009-03-30 15:27:03 +0000114 return DataRelROLocalSection;
Chris Lattner7886ae92009-07-21 21:29:08 +0000115 case SectionKind::BSS:
116 case SectionKind::SmallBSS:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000117 // ELF targets usually have BSS sections
118 return getBSSSection_();
Chris Lattner7886ae92009-07-21 21:29:08 +0000119 case SectionKind::ROData:
120 case SectionKind::SmallROData:
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000121 return getReadOnlySection();
Chris Lattner7886ae92009-07-21 21:29:08 +0000122 case SectionKind::RODataMergeStr:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000123 return MergeableStringSection(GVar);
Chris Lattnerf1581562009-07-22 00:47:11 +0000124 case SectionKind::RODataMergeConst: {
125 const Type *Ty = GVar->getInitializer()->getType();
126 const TargetData *TD = TM.getTargetData();
127 return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
128 }
Chris Lattner7886ae92009-07-21 21:29:08 +0000129 case SectionKind::ThreadData:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000130 // ELF targets usually support TLS stuff
Anton Korobeynikov36133dd2008-09-24 22:17:06 +0000131 return TLSDataSection;
Chris Lattner7886ae92009-07-21 21:29:08 +0000132 case SectionKind::ThreadBSS:
Anton Korobeynikov36133dd2008-09-24 22:17:06 +0000133 return TLSBSSSection;
Chris Lattner7886ae92009-07-21 21:29:08 +0000134 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000135 llvm_unreachable("Unsuported section kind for global");
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000136 }
137 }
138 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000139 llvm_unreachable("Unsupported global");
Devang Patel8a84e442009-01-05 17:31:22 +0000140
141 return NULL;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000142}
143
Chris Lattner298414e2009-07-22 00:28:43 +0000144/// getSectionForMergableConstant - Given a mergable constant with the
145/// specified size and relocation information, return a section that it
146/// should be placed in.
147const Section *
148ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
149 unsigned ReloInfo) const {
150 // FIXME: IF this global requires a relocation, can we really put it in
151 // rodata??? This should check ReloInfo like darwin.
152
153 const char *SecName = 0;
154 switch (Size) {
155 default: break;
156 case 4: SecName = ".rodata.cst4"; break;
157 case 8: SecName = ".rodata.cst8"; break;
158 case 16: SecName = ".rodata.cst16"; break;
159 }
160
161 if (SecName)
162 return getNamedSection(SecName,
Chris Lattner828f7042009-07-22 00:41:56 +0000163 SectionFlags::setEntitySize(SectionFlags::Mergeable|
164 SectionFlags::Small,
Chris Lattner298414e2009-07-22 00:28:43 +0000165 Size));
166
167 return getReadOnlySection(); // .rodata
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000168}
169
Chris Lattner298414e2009-07-22 00:28:43 +0000170
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000171const Section*
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000172ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000173 const TargetData *TD = TM.getTargetData();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000174 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
Anton Korobeynikov72bb4022009-01-27 22:29:24 +0000175 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000176
Duncan Sands777d2302009-05-09 07:06:46 +0000177 unsigned Size = TD->getTypeAllocSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000178 if (Size <= 16) {
Anton Korobeynikov1e27da32008-08-07 09:54:40 +0000179 assert(getCStringSection() && "Should have string section prefix");
180
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000181 // We also need alignment here
Anton Korobeynikov96855062008-07-21 18:29:23 +0000182 unsigned Align = TD->getPrefTypeAlignment(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000183 if (Align < Size)
184 Align = Size;
185
186 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
187 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
188 SectionFlags::Strings,
189 Size);
190 return getNamedSection(Name.c_str(), Flags);
191 }
192
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000193 return getReadOnlySection();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000194}
195
Anton Korobeynikovd0c1e292008-08-16 12:57:07 +0000196std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000197 std::string Flags = ",\"";
198
199 if (!(flags & SectionFlags::Debug))
200 Flags += 'a';
201 if (flags & SectionFlags::Code)
202 Flags += 'x';
203 if (flags & SectionFlags::Writeable)
204 Flags += 'w';
205 if (flags & SectionFlags::Mergeable)
206 Flags += 'M';
207 if (flags & SectionFlags::Strings)
208 Flags += 'S';
209 if (flags & SectionFlags::TLS)
210 Flags += 'T';
Anton Korobeynikov09809802008-07-22 17:09:41 +0000211 if (flags & SectionFlags::Small)
212 Flags += 's';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000213
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000214 Flags += "\",";
215
216 // If comment string is '@', e.g. as on ARM - use '%' instead
217 if (strcmp(CommentString, "@") == 0)
218 Flags += '%';
219 else
220 Flags += '@';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000221
222 // FIXME: There can be exceptions here
223 if (flags & SectionFlags::BSS)
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000224 Flags += "nobits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000225 else
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000226 Flags += "progbits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000227
228 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
229 Flags += "," + utostr(entitySize);
230
231 return Flags;
232}