9dd339ed0a299ed3c1991ca886e31f69a7fc3173
[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
49 const Section*
50 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
51                                          SectionKind::Kind Kind) const {
52   if (const Function *F = dyn_cast<Function>(GV)) {
53     switch (F->getLinkage()) {
54     default: llvm_unreachable("Unknown linkage type!");
55     case Function::PrivateLinkage:
56     case Function::LinkerPrivateLinkage:
57     case Function::InternalLinkage:
58     case Function::DLLExportLinkage:
59     case Function::ExternalLinkage:
60       return TextSection;
61     }
62   }
63   
64   const GlobalVariable *GVar = cast<GlobalVariable>(GV);
65   switch (Kind) {
66   default: llvm_unreachable("Unsuported section kind for global");
67   case SectionKind::BSS:
68     return getBSSSection_();
69   case SectionKind::Data:
70   case SectionKind::DataRel:
71     return DataRelSection;
72   case SectionKind::DataRelLocal:
73     return DataRelLocalSection;
74   case SectionKind::DataRelRO:
75     return DataRelROSection;
76   case SectionKind::DataRelROLocal:
77     return DataRelROLocalSection;
78   case SectionKind::ROData:
79     return getReadOnlySection();
80   case SectionKind::RODataMergeStr:
81     return MergeableStringSection(GVar);
82   case SectionKind::RODataMergeConst: {
83     const Type *Ty = GVar->getInitializer()->getType();
84     const TargetData *TD = TM.getTargetData();
85     return getSectionForMergableConstant(TD->getTypeAllocSize(Ty), 0);
86   }
87   case SectionKind::ThreadData:
88     // ELF targets usually support TLS stuff
89     return TLSDataSection;
90   case SectionKind::ThreadBSS:
91     return TLSBSSSection;
92   }
93 }
94
95 /// getSectionForMergableConstant - Given a mergable constant with the
96 /// specified size and relocation information, return a section that it
97 /// should be placed in.
98 const Section *
99 ELFTargetAsmInfo::getSectionForMergableConstant(uint64_t Size,
100                                                 unsigned ReloInfo) const {
101   // FIXME: IF this global requires a relocation, can we really put it in
102   // rodata???  This should check ReloInfo like darwin.
103   
104   const char *SecName = 0;
105   switch (Size) {
106   default: break;
107   case 4:  SecName = ".rodata.cst4"; break;
108   case 8:  SecName = ".rodata.cst8"; break;
109   case 16: SecName = ".rodata.cst16"; break;
110   }
111   
112   if (SecName)
113     return getNamedSection(SecName,
114                            SectionFlags::setEntitySize(SectionFlags::Mergeable,
115                                                        Size));
116   
117   return getReadOnlySection();  // .rodata
118 }
119
120 /// getFlagsForNamedSection - If this target wants to be able to infer
121 /// section flags based on the name of the section specified for a global
122 /// variable, it can implement this.
123 unsigned ELFTargetAsmInfo::getFlagsForNamedSection(const char *Name) const {
124   unsigned Flags = 0;
125   if (Name[0] != '.') return 0;
126   
127   // Some lame default implementation based on some magic section names.
128   if (strncmp(Name, ".gnu.linkonce.b.", 16) == 0 ||
129       strncmp(Name, ".llvm.linkonce.b.", 17) == 0 ||
130       strncmp(Name, ".gnu.linkonce.sb.", 17) == 0 ||
131       strncmp(Name, ".llvm.linkonce.sb.", 18) == 0)
132     Flags |= SectionFlags::BSS;
133   else if (strcmp(Name, ".tdata") == 0 ||
134            strncmp(Name, ".tdata.", 7) == 0 ||
135            strncmp(Name, ".gnu.linkonce.td.", 17) == 0 ||
136            strncmp(Name, ".llvm.linkonce.td.", 18) == 0)
137     Flags |= SectionFlags::TLS;
138   else if (strcmp(Name, ".tbss") == 0 ||
139            strncmp(Name, ".tbss.", 6) == 0 ||
140            strncmp(Name, ".gnu.linkonce.tb.", 17) == 0 ||
141            strncmp(Name, ".llvm.linkonce.tb.", 18) == 0)
142     Flags |= SectionFlags::BSS | SectionFlags::TLS;
143   
144   return Flags;
145 }
146
147
148 const Section*
149 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
150   const TargetData *TD = TM.getTargetData();
151   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
152   const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
153
154   unsigned Size = TD->getTypeAllocSize(Ty);
155   if (Size <= 16) {
156     assert(getCStringSection() && "Should have string section prefix");
157
158     // We also need alignment here
159     unsigned Align = TD->getPrefTypeAlignment(Ty);
160     if (Align < Size)
161       Align = Size;
162
163     std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
164     unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
165                                                  SectionFlags::Strings,
166                                                  Size);
167     return getNamedSection(Name.c_str(), Flags);
168   }
169
170   return getReadOnlySection();
171 }
172
173 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
174   std::string Flags = ",\"";
175
176   if (!(flags & SectionFlags::Debug))
177     Flags += 'a';
178   if (flags & SectionFlags::Code)
179     Flags += 'x';
180   if (flags & SectionFlags::Writeable)
181     Flags += 'w';
182   if (flags & SectionFlags::Mergeable)
183     Flags += 'M';
184   if (flags & SectionFlags::Strings)
185     Flags += 'S';
186   if (flags & SectionFlags::TLS)
187     Flags += 'T';
188
189   Flags += "\",";
190
191   // If comment string is '@', e.g. as on ARM - use '%' instead
192   if (strcmp(CommentString, "@") == 0)
193     Flags += '%';
194   else
195     Flags += '@';
196
197   // FIXME: There can be exceptions here
198   if (flags & SectionFlags::BSS)
199     Flags += "nobits";
200   else
201     Flags += "progbits";
202
203   if (unsigned entitySize = SectionFlags::getEntitySize(flags))
204     Flags += "," + utostr(entitySize);
205
206   return Flags;
207 }