Add plumbing for the `linker_private' linkage type. This type is meant for
[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::LinkerPrivateLinkage:
81      case Function::InternalLinkage:
82      case Function::DLLExportLinkage:
83      case Function::ExternalLinkage:
84       return TextSection;
85      case Function::WeakAnyLinkage:
86      case Function::WeakODRLinkage:
87      case Function::LinkOnceAnyLinkage:
88      case Function::LinkOnceODRLinkage:
89       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)) {
94     if (GVar->isWeakForLinker()) {
95       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) {
100        case SectionKind::Data:
101        case SectionKind::SmallData:
102         return DataSection;
103        case SectionKind::DataRel:
104         return DataRelSection;
105        case SectionKind::DataRelLocal:
106         return DataRelLocalSection;
107        case SectionKind::DataRelRO:
108         return DataRelROSection;
109        case SectionKind::DataRelROLocal:
110         return DataRelROLocalSection;
111        case SectionKind::BSS:
112        case SectionKind::SmallBSS:
113         // ELF targets usually have BSS sections
114         return getBSSSection_();
115        case SectionKind::ROData:
116        case SectionKind::SmallROData:
117         return getReadOnlySection();
118        case SectionKind::RODataMergeStr:
119         return MergeableStringSection(GVar);
120        case SectionKind::RODataMergeConst:
121         return MergeableConstSection(GVar);
122        case SectionKind::ThreadData:
123         // ELF targets usually support TLS stuff
124         return TLSDataSection;
125        case SectionKind::ThreadBSS:
126         return TLSBSSSection;
127        default:
128         llvm_unreachable("Unsuported section kind for global");
129       }
130     }
131   } else
132     llvm_unreachable("Unsupported global");
133
134   return NULL;
135 }
136
137 const Section*
138 ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
139   // FIXME: Support data.rel stuff someday
140   return MergeableConstSection(Ty);
141 }
142
143 const Section*
144 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
145   Constant *C = GV->getInitializer();
146   return MergeableConstSection(C->getType());
147 }
148
149 inline const Section*
150 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
151   const TargetData *TD = TM.getTargetData();
152
153   // FIXME: string here is temporary, until stuff will fully land in.
154   // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
155   // currently directly used by asmprinter.
156   unsigned Size = TD->getTypeAllocSize(Ty);
157   if (Size == 4 || Size == 8 || Size == 16) {
158     std::string Name =  ".rodata.cst" + utostr(Size);
159
160     return getNamedSection(Name.c_str(),
161                            SectionFlags::setEntitySize(SectionFlags::Mergeable,
162                                                        Size));
163   }
164
165   return getReadOnlySection();
166 }
167
168 const Section*
169 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
170   const TargetData *TD = TM.getTargetData();
171   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
172   const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
173
174   unsigned Size = TD->getTypeAllocSize(Ty);
175   if (Size <= 16) {
176     assert(getCStringSection() && "Should have string section prefix");
177
178     // We also need alignment here
179     unsigned Align = TD->getPrefTypeAlignment(Ty);
180     if (Align < Size)
181       Align = Size;
182
183     std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
184     unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
185                                                  SectionFlags::Strings,
186                                                  Size);
187     return getNamedSection(Name.c_str(), Flags);
188   }
189
190   return getReadOnlySection();
191 }
192
193 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
194   std::string Flags = ",\"";
195
196   if (!(flags & SectionFlags::Debug))
197     Flags += 'a';
198   if (flags & SectionFlags::Code)
199     Flags += 'x';
200   if (flags & SectionFlags::Writeable)
201     Flags += 'w';
202   if (flags & SectionFlags::Mergeable)
203     Flags += 'M';
204   if (flags & SectionFlags::Strings)
205     Flags += 'S';
206   if (flags & SectionFlags::TLS)
207     Flags += 'T';
208   if (flags & SectionFlags::Small)
209     Flags += 's';
210
211   Flags += "\",";
212
213   // If comment string is '@', e.g. as on ARM - use '%' instead
214   if (strcmp(CommentString, "@") == 0)
215     Flags += '%';
216   else
217     Flags += '@';
218
219   // FIXME: There can be exceptions here
220   if (flags & SectionFlags::BSS)
221     Flags += "nobits";
222   else
223     Flags += "progbits";
224
225   if (unsigned entitySize = SectionFlags::getEntitySize(flags))
226     Flags += "," + utostr(entitySize);
227
228   return Flags;
229 }