blob: 662fc11f20da51ef56bbdb3c9b0b747667c78ea9 [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);
Chris Lattnerbdb23b32009-07-22 00:05:44 +000057 if (GVar->hasInitializer() && TM.getRelocationModel() != Reloc::Static) {
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000058 Constant *C = GVar->getInitializer();
59 bool isConstant = GVar->isConstant();
Chris Lattner06a55c32009-07-21 23:47:11 +000060
Chris Lattner06a55c32009-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 Lattnerbdb23b32009-07-22 00:05:44 +000063 switch (C->getRelocationInfo()) {
64 default: break;
Chris Lattner83ca6d92009-07-24 03:27:21 +000065 case Constant::LocalRelocation:
Chris Lattnerbdb23b32009-07-22 00:05:44 +000066 return isConstant ? SectionKind::DataRelROLocal :
67 SectionKind::DataRelLocal;
Chris Lattner83ca6d92009-07-24 03:27:21 +000068 case Constant::GlobalRelocations:
Chris Lattnerbdb23b32009-07-22 00:05:44 +000069 return isConstant ? SectionKind::DataRelRO : SectionKind::DataRel;
70 }
Anton Korobeynikovb3882b62009-03-30 15:27:43 +000071 }
72
73 return Kind;
74}
75
asle0d0f482008-07-19 13:14:11 +000076const Section*
Evan Chengeb774e12008-08-08 17:56:50 +000077ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
asle0d0f482008-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()) {
Chris Lattnere09c7d72009-07-24 04:52:38 +000082 default: llvm_unreachable("Unknown linkage type!");
83 case Function::PrivateLinkage:
84 case Function::LinkerPrivateLinkage:
85 case Function::InternalLinkage:
86 case Function::DLLExportLinkage:
87 case Function::ExternalLinkage:
88 return TextSection;
asle0d0f482008-07-19 13:14:11 +000089 }
Chris Lattnera06e19b2009-07-24 05:01:55 +000090 }
91
92 const GlobalVariable *GVar = cast<GlobalVariable>(GV);
93 switch (Kind) {
94 default: llvm_unreachable("Unsuported section kind for global");
95 case SectionKind::Data:
96 case SectionKind::DataRel:
97 return DataRelSection;
98 case SectionKind::DataRelLocal:
99 return DataRelLocalSection;
100 case SectionKind::DataRelRO:
101 return DataRelROSection;
102 case SectionKind::DataRelROLocal:
103 return DataRelROLocalSection;
104 case SectionKind::BSS:
105 return getBSSSection_();
106 case SectionKind::ROData:
107 return getReadOnlySection();
108 case SectionKind::RODataMergeStr:
109 return MergeableStringSection(GVar);
110 case SectionKind::RODataMergeConst: {
111 const Type *Ty = GVar->getInitializer()->getType();
112 const TargetData *TD = TM.getTargetData();
113 return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
114 }
115 case SectionKind::ThreadData:
116 // ELF targets usually support TLS stuff
117 return TLSDataSection;
118 case SectionKind::ThreadBSS:
119 return TLSBSSSection;
Chris Lattnere09c7d72009-07-24 04:52:38 +0000120 }
asle0d0f482008-07-19 13:14:11 +0000121}
122
Chris Lattner680c6f62009-07-22 00:28:43 +0000123/// getSectionForMergableConstant - Given a mergable constant with the
124/// specified size and relocation information, return a section that it
125/// should be placed in.
126const Section *
127ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
128 unsigned ReloInfo) const {
129 // FIXME: IF this global requires a relocation, can we really put it in
130 // rodata??? This should check ReloInfo like darwin.
131
132 const char *SecName = 0;
133 switch (Size) {
134 default: break;
135 case 4: SecName = ".rodata.cst4"; break;
136 case 8: SecName = ".rodata.cst8"; break;
137 case 16: SecName = ".rodata.cst16"; break;
138 }
139
140 if (SecName)
141 return getNamedSection(SecName,
Duncan Sands5bc722e2009-07-22 10:35:05 +0000142 SectionFlags::setEntitySize(SectionFlags::Mergeable,
Chris Lattner680c6f62009-07-22 00:28:43 +0000143 Size));
144
145 return getReadOnlySection(); // .rodata
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000146}
147
Chris Lattner680c6f62009-07-22 00:28:43 +0000148
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000149const Section*
asle0d0f482008-07-19 13:14:11 +0000150ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000151 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000152 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000153 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000154
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000155 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000156 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000157 assert(getCStringSection() && "Should have string section prefix");
158
asle0d0f482008-07-19 13:14:11 +0000159 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000160 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000161 if (Align < Size)
162 Align = Size;
163
164 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
165 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
166 SectionFlags::Strings,
167 Size);
168 return getNamedSection(Name.c_str(), Flags);
169 }
170
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000171 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000172}
173
asl27ffd262008-08-16 12:57:07 +0000174std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000175 std::string Flags = ",\"";
176
177 if (!(flags & SectionFlags::Debug))
178 Flags += 'a';
179 if (flags & SectionFlags::Code)
180 Flags += 'x';
181 if (flags & SectionFlags::Writeable)
182 Flags += 'w';
183 if (flags & SectionFlags::Mergeable)
184 Flags += 'M';
185 if (flags & SectionFlags::Strings)
186 Flags += 'S';
187 if (flags & SectionFlags::TLS)
188 Flags += 'T';
asle0d0f482008-07-19 13:14:11 +0000189
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000190 Flags += "\",";
191
192 // If comment string is '@', e.g. as on ARM - use '%' instead
193 if (strcmp(CommentString, "@") == 0)
194 Flags += '%';
195 else
196 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000197
198 // FIXME: There can be exceptions here
199 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000200 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000201 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000202 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000203
204 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
205 Flags += "," + utostr(entitySize);
206
207 return Flags;
208}