PR3478: raw_ostream should not buffer stderr
[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/Target/ELFTargetAsmInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetData.h"
24
25 using namespace llvm;
26
27 ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM)
28   : TargetAsmInfo(TM) {
29
30   BSSSection_  = getUnnamedSection("\t.bss",
31                                    SectionFlags::Writeable | SectionFlags::BSS);
32   ReadOnlySection = getNamedSection("\t.rodata", SectionFlags::None);
33   TLSDataSection = getNamedSection("\t.tdata",
34                                    SectionFlags::Writeable | SectionFlags::TLS);
35   TLSBSSSection = getNamedSection("\t.tbss",
36                 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
37
38 }
39
40 const Section*
41 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
42   SectionKind::Kind Kind = SectionKindForGlobal(GV);
43
44   if (const Function *F = dyn_cast<Function>(GV)) {
45     switch (F->getLinkage()) {
46      default: assert(0 && "Unknown linkage type!");
47      case Function::PrivateLinkage:
48      case Function::InternalLinkage:
49      case Function::DLLExportLinkage:
50      case Function::ExternalLinkage:
51       return TextSection;
52      case Function::WeakAnyLinkage:
53      case Function::WeakODRLinkage:
54      case Function::LinkOnceAnyLinkage:
55      case Function::LinkOnceODRLinkage:
56       std::string Name = UniqueSectionForGlobal(GV, Kind);
57       unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
58       return getNamedSection(Name.c_str(), Flags);
59     }
60   } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
61     if (GVar->isWeakForLinker()) {
62       std::string Name = UniqueSectionForGlobal(GVar, Kind);
63       unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
64       return getNamedSection(Name.c_str(), Flags);
65     } else {
66       switch (Kind) {
67        case SectionKind::Data:
68        case SectionKind::SmallData:
69         return DataSection;
70        case SectionKind::BSS:
71        case SectionKind::SmallBSS:
72         // ELF targets usually have BSS sections
73         return getBSSSection_();
74        case SectionKind::ROData:
75        case SectionKind::SmallROData:
76         return getReadOnlySection();
77        case SectionKind::RODataMergeStr:
78         return MergeableStringSection(GVar);
79        case SectionKind::RODataMergeConst:
80         return MergeableConstSection(GVar);
81        case SectionKind::ThreadData:
82         // ELF targets usually support TLS stuff
83         return TLSDataSection;
84        case SectionKind::ThreadBSS:
85         return TLSBSSSection;
86        default:
87         assert(0 && "Unsuported section kind for global");
88       }
89     }
90   } else
91     assert(0 && "Unsupported global");
92
93   return NULL;
94 }
95
96 const Section*
97 ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
98   // FIXME: Support data.rel stuff someday
99   return MergeableConstSection(Ty);
100 }
101
102 const Section*
103 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
104   Constant *C = GV->getInitializer();
105   return MergeableConstSection(C->getType());
106 }
107
108 inline const Section*
109 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
110   const TargetData *TD = TM.getTargetData();
111
112   // FIXME: string here is temporary, until stuff will fully land in.
113   // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
114   // currently directly used by asmprinter.
115   unsigned Size = TD->getTypePaddedSize(Ty);
116   if (Size == 4 || Size == 8 || Size == 16) {
117     std::string Name =  ".rodata.cst" + utostr(Size);
118
119     return getNamedSection(Name.c_str(),
120                            SectionFlags::setEntitySize(SectionFlags::Mergeable,
121                                                        Size));
122   }
123
124   return getReadOnlySection();
125 }
126
127 const Section*
128 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
129   const TargetData *TD = TM.getTargetData();
130   Constant *C = cast<GlobalVariable>(GV)->getInitializer();
131   const Type *Ty = cast<ArrayType>(C->getType())->getElementType();
132
133   unsigned Size = TD->getTypePaddedSize(Ty);
134   if (Size <= 16) {
135     assert(getCStringSection() && "Should have string section prefix");
136
137     // We also need alignment here
138     unsigned Align = TD->getPrefTypeAlignment(Ty);
139     if (Align < Size)
140       Align = Size;
141
142     std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
143     unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
144                                                  SectionFlags::Strings,
145                                                  Size);
146     return getNamedSection(Name.c_str(), Flags);
147   }
148
149   return getReadOnlySection();
150 }
151
152 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
153   std::string Flags = ",\"";
154
155   if (!(flags & SectionFlags::Debug))
156     Flags += 'a';
157   if (flags & SectionFlags::Code)
158     Flags += 'x';
159   if (flags & SectionFlags::Writeable)
160     Flags += 'w';
161   if (flags & SectionFlags::Mergeable)
162     Flags += 'M';
163   if (flags & SectionFlags::Strings)
164     Flags += 'S';
165   if (flags & SectionFlags::TLS)
166     Flags += 'T';
167   if (flags & SectionFlags::Small)
168     Flags += 's';
169
170   Flags += "\",";
171
172   // If comment string is '@', e.g. as on ARM - use '%' instead
173   if (strcmp(CommentString, "@") == 0)
174     Flags += '%';
175   else
176     Flags += '@';
177
178   // FIXME: There can be exceptions here
179   if (flags & SectionFlags::BSS)
180     Flags += "nobits";
181   else
182     Flags += "progbits";
183
184   if (unsigned entitySize = SectionFlags::getEntitySize(flags))
185     Flags += "," + utostr(entitySize);
186
187   return Flags;
188 }