b513a604c00dea8d5bae79d0bb265d45f737994d
[oota-llvm.git] / lib / Target / ELFTargetAsmInfo.cpp
1 //===-- 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"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Target/ELFTargetAsmInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetData.h"
25
26 using namespace llvm;
27
28 ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM)
29   : TargetAsmInfo(TM) {
30
31   BSSSection_  = getUnnamedSection("\t.bss",
32                                    SectionFlags::Writeable | SectionFlags::BSS);
33   ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None);
34   TLSDataSection = getNamedSection("\t.tdata",
35                                    SectionFlags::Writeable | SectionFlags::TLS);
36   TLSBSSSection = getNamedSection("\t.tbss",
37                 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
38
39   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);
46 }
47
48 SectionKind::Kind
49 ELFTargetAsmInfo::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))
62       return (C->ContainsRelocations(Reloc::Global) ?
63               (isConstant ?
64                SectionKind::DataRelRO : SectionKind::DataRel) :
65               (isConstant ?
66                SectionKind::DataRelROLocal : SectionKind::DataRelLocal));
67   }
68
69   return Kind;
70 }
71
72 const Section*
73 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
74   SectionKind::Kind Kind = SectionKindForGlobal(GV);
75
76   if (const Function *F = dyn_cast<Function>(GV)) {
77     switch (F->getLinkage()) {
78      default: LLVM_UNREACHABLE("Unknown linkage type!");
79      case Function::PrivateLinkage:
80      case Function::InternalLinkage:
81      case Function::DLLExportLinkage:
82      case Function::ExternalLinkage:
83       return TextSection;
84      case Function::WeakAnyLinkage:
85      case Function::WeakODRLinkage:
86      case Function::LinkOnceAnyLinkage:
87      case Function::LinkOnceODRLinkage:
88       std::string Name = UniqueSectionForGlobal(GV, Kind);
89       unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
90       return getNamedSection(Name.c_str(), Flags);
91     }
92   } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
93     if (GVar->isWeakForLinker()) {
94       std::string Name = UniqueSectionForGlobal(GVar, Kind);
95       unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
96       return getNamedSection(Name.c_str(), Flags);
97     } else {
98       switch (Kind) {
99        case SectionKind::Data:
100        case SectionKind::SmallData:
101         return DataSection;
102        case SectionKind::DataRel:
103         return DataRelSection;
104        case SectionKind::DataRelLocal:
105         return DataRelLocalSection;
106        case SectionKind::DataRelRO:
107         return DataRelROSection;
108        case SectionKind::DataRelROLocal:
109         return DataRelROLocalSection;
110        case SectionKind::BSS:
111        case SectionKind::SmallBSS:
112         // ELF targets usually have BSS sections
113         return getBSSSection_();
114        case SectionKind::ROData:
115        case SectionKind::SmallROData:
116         return getReadOnlySection();
117        case SectionKind::RODataMergeStr:
118         return MergeableStringSection(GVar);
119        case SectionKind::RODataMergeConst:
120         return MergeableConstSection(GVar);
121        case SectionKind::ThreadData:
122         // ELF targets usually support TLS stuff
123         return TLSDataSection;
124        case SectionKind::ThreadBSS:
125         return TLSBSSSection;
126        default:
127         LLVM_UNREACHABLE("Unsuported section kind for global");
128       }
129     }
130   } else
131     LLVM_UNREACHABLE("Unsupported global");
132
133   return NULL;
134 }
135
136 const Section*
137 ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
138   // FIXME: Support data.rel stuff someday
139   return MergeableConstSection(Ty);
140 }
141
142 const Section*
143 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
144   Constant *C = GV->getInitializer();
145   return MergeableConstSection(C->getType());
146 }
147
148 inline const Section*
149 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
150   const TargetData *TD = TM.getTargetData();
151
152   // FIXME: string here is temporary, until stuff will fully land in.
153   // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
154   // currently directly used by asmprinter.
155   unsigned Size = TD->getTypeAllocSize(Ty);
156   if (Size == 4 || Size == 8 || Size == 16) {
157     std::string Name =  ".rodata.cst" + utostr(Size);
158
159     return getNamedSection(Name.c_str(),
160                            SectionFlags::setEntitySize(SectionFlags::Mergeable,
161                                                        Size));
162   }
163
164   return getReadOnlySection();
165 }
166
167 const Section*
168 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
169   const TargetData *TD = TM.getTargetData();
170   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
171   const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
172
173   unsigned Size = TD->getTypeAllocSize(Ty);
174   if (Size <= 16) {
175     assert(getCStringSection() && "Should have string section prefix");
176
177     // We also need alignment here
178     unsigned Align = TD->getPrefTypeAlignment(Ty);
179     if (Align < Size)
180       Align = Size;
181
182     std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
183     unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
184                                                  SectionFlags::Strings,
185                                                  Size);
186     return getNamedSection(Name.c_str(), Flags);
187   }
188
189   return getReadOnlySection();
190 }
191
192 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
193   std::string Flags = ",\"";
194
195   if (!(flags & SectionFlags::Debug))
196     Flags += 'a';
197   if (flags & SectionFlags::Code)
198     Flags += 'x';
199   if (flags & SectionFlags::Writeable)
200     Flags += 'w';
201   if (flags & SectionFlags::Mergeable)
202     Flags += 'M';
203   if (flags & SectionFlags::Strings)
204     Flags += 'S';
205   if (flags & SectionFlags::TLS)
206     Flags += 'T';
207   if (flags & SectionFlags::Small)
208     Flags += 's';
209
210   Flags += "\",";
211
212   // If comment string is '@', e.g. as on ARM - use '%' instead
213   if (strcmp(CommentString, "@") == 0)
214     Flags += '%';
215   else
216     Flags += '@';
217
218   // FIXME: There can be exceptions here
219   if (flags & SectionFlags::BSS)
220     Flags += "nobits";
221   else
222     Flags += "progbits";
223
224   if (unsigned entitySize = SectionFlags::getEntitySize(flags))
225     Flags += "," + utostr(entitySize);
226
227   return Flags;
228 }