blob: 9e7bb4aee26963e797b3337150fdfc3e2326db41 [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;
65 case 1:
66 return isConstant ? SectionKind::DataRelROLocal :
67 SectionKind::DataRelLocal;
68 case 2:
69 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()) {
Edwin Törökbd448e32009-07-14 16:55:14 +000082 default: llvm_unreachable("Unknown linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +000083 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +000084 case Function::LinkerPrivateLinkage:
asle0d0f482008-07-19 13:14:11 +000085 case Function::InternalLinkage:
86 case Function::DLLExportLinkage:
87 case Function::ExternalLinkage:
Anton Korobeynikovf760ffb2008-09-24 22:16:33 +000088 return TextSection;
Duncan Sands19d161f2009-03-07 15:45:40 +000089 case Function::WeakAnyLinkage:
90 case Function::WeakODRLinkage:
91 case Function::LinkOnceAnyLinkage:
92 case Function::LinkOnceODRLinkage:
asle0d0f482008-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 Sands19d161f2009-03-07 15:45:40 +000098 if (GVar->isWeakForLinker()) {
asle0d0f482008-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 Lattnerd49a0752009-07-21 21:29:08 +0000104 case SectionKind::Data:
Chris Lattnerd49a0752009-07-21 21:29:08 +0000105 case SectionKind::DataRel:
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +0000106 return DataRelSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000107 case SectionKind::DataRelLocal:
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +0000108 return DataRelLocalSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000109 case SectionKind::DataRelRO:
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +0000110 return DataRelROSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000111 case SectionKind::DataRelROLocal:
Anton Korobeynikov8a4fbe42009-03-30 15:27:03 +0000112 return DataRelROLocalSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000113 case SectionKind::BSS:
asle0d0f482008-07-19 13:14:11 +0000114 return getBSSSection_();
Chris Lattnerd49a0752009-07-21 21:29:08 +0000115 case SectionKind::ROData:
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000116 return getReadOnlySection();
Chris Lattnerd49a0752009-07-21 21:29:08 +0000117 case SectionKind::RODataMergeStr:
asle0d0f482008-07-19 13:14:11 +0000118 return MergeableStringSection(GVar);
Chris Lattner81466bc2009-07-22 00:47:11 +0000119 case SectionKind::RODataMergeConst: {
120 const Type *Ty = GVar->getInitializer()->getType();
121 const TargetData *TD = TM.getTargetData();
122 return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
123 }
Chris Lattnerd49a0752009-07-21 21:29:08 +0000124 case SectionKind::ThreadData:
asle0d0f482008-07-19 13:14:11 +0000125 // ELF targets usually support TLS stuff
Anton Korobeynikov9a213e42008-09-24 22:17:06 +0000126 return TLSDataSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000127 case SectionKind::ThreadBSS:
Anton Korobeynikov9a213e42008-09-24 22:17:06 +0000128 return TLSBSSSection;
Chris Lattnerd49a0752009-07-21 21:29:08 +0000129 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000130 llvm_unreachable("Unsuported section kind for global");
asle0d0f482008-07-19 13:14:11 +0000131 }
132 }
133 } else
Edwin Törökbd448e32009-07-14 16:55:14 +0000134 llvm_unreachable("Unsupported global");
Devang Patelf3707e82009-01-05 17:31:22 +0000135
136 return NULL;
asle0d0f482008-07-19 13:14:11 +0000137}
138
Chris Lattner680c6f62009-07-22 00:28:43 +0000139/// getSectionForMergableConstant - Given a mergable constant with the
140/// specified size and relocation information, return a section that it
141/// should be placed in.
142const Section *
143ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
144 unsigned ReloInfo) const {
145 // FIXME: IF this global requires a relocation, can we really put it in
146 // rodata??? This should check ReloInfo like darwin.
147
148 const char *SecName = 0;
149 switch (Size) {
150 default: break;
151 case 4: SecName = ".rodata.cst4"; break;
152 case 8: SecName = ".rodata.cst8"; break;
153 case 16: SecName = ".rodata.cst16"; break;
154 }
155
156 if (SecName)
157 return getNamedSection(SecName,
Duncan Sands5bc722e2009-07-22 10:35:05 +0000158 SectionFlags::setEntitySize(SectionFlags::Mergeable,
Chris Lattner680c6f62009-07-22 00:28:43 +0000159 Size));
160
161 return getReadOnlySection(); // .rodata
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000162}
163
Chris Lattner680c6f62009-07-22 00:28:43 +0000164
Anton Korobeynikovfc54db12008-08-07 09:50:34 +0000165const Section*
asle0d0f482008-07-19 13:14:11 +0000166ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohmana004d7b2008-11-03 18:22:42 +0000167 const TargetData *TD = TM.getTargetData();
asle0d0f482008-07-19 13:14:11 +0000168 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
asl4eeee012009-01-27 22:29:24 +0000169 const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
asle0d0f482008-07-19 13:14:11 +0000170
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000171 unsigned Size = TD->getTypeAllocSize(Ty);
asle0d0f482008-07-19 13:14:11 +0000172 if (Size <= 16) {
Anton Korobeynikovfb4bf862008-08-07 09:54:40 +0000173 assert(getCStringSection() && "Should have string section prefix");
174
asle0d0f482008-07-19 13:14:11 +0000175 // We also need alignment here
Anton Korobeynikov6fbf6d52008-07-21 18:29:23 +0000176 unsigned Align = TD->getPrefTypeAlignment(Ty);
asle0d0f482008-07-19 13:14:11 +0000177 if (Align < Size)
178 Align = Size;
179
180 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
181 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
182 SectionFlags::Strings,
183 Size);
184 return getNamedSection(Name.c_str(), Flags);
185 }
186
Anton Korobeynikov4d433222008-09-24 22:20:27 +0000187 return getReadOnlySection();
asle0d0f482008-07-19 13:14:11 +0000188}
189
asl27ffd262008-08-16 12:57:07 +0000190std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
asle0d0f482008-07-19 13:14:11 +0000191 std::string Flags = ",\"";
192
193 if (!(flags & SectionFlags::Debug))
194 Flags += 'a';
195 if (flags & SectionFlags::Code)
196 Flags += 'x';
197 if (flags & SectionFlags::Writeable)
198 Flags += 'w';
199 if (flags & SectionFlags::Mergeable)
200 Flags += 'M';
201 if (flags & SectionFlags::Strings)
202 Flags += 'S';
203 if (flags & SectionFlags::TLS)
204 Flags += 'T';
asle0d0f482008-07-19 13:14:11 +0000205
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000206 Flags += "\",";
207
208 // If comment string is '@', e.g. as on ARM - use '%' instead
209 if (strcmp(CommentString, "@") == 0)
210 Flags += '%';
211 else
212 Flags += '@';
asle0d0f482008-07-19 13:14:11 +0000213
214 // FIXME: There can be exceptions here
215 if (flags & SectionFlags::BSS)
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000216 Flags += "nobits";
asle0d0f482008-07-19 13:14:11 +0000217 else
Anton Korobeynikov4183e112008-08-07 09:55:06 +0000218 Flags += "progbits";
asle0d0f482008-07-19 13:14:11 +0000219
220 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
221 Flags += "," + utostr(entitySize);
222
223 return Flags;
224}