1 //===-- ELFTargetAsmInfo.cpp - ELF asm properties ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines target asm properties related what form asm statements
11 // should take in general on ELF-based targets
13 //===----------------------------------------------------------------------===//
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/Target/ELFTargetAsmInfo.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/Target/TargetData.h"
26 ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) {
29 TextSection_ = getUnnamedSection("\t.text", SectionFlags::Code);
30 DataSection_ = getUnnamedSection("\t.data", SectionFlags::Writeable);
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);
42 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
43 SectionKind::Kind Kind = SectionKindForGlobal(GV);
45 if (const Function *F = dyn_cast<Function>(GV)) {
46 switch (F->getLinkage()) {
47 default: assert(0 && "Unknown linkage type!");
48 case Function::InternalLinkage:
49 case Function::DLLExportLinkage:
50 case Function::ExternalLinkage:
51 return getTextSection_();
52 case Function::WeakLinkage:
53 case Function::LinkOnceLinkage:
54 std::string Name = UniqueSectionForGlobal(GV, Kind);
55 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
56 return getNamedSection(Name.c_str(), Flags);
58 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
59 if (GVar->isWeakForLinker()) {
60 std::string Name = UniqueSectionForGlobal(GVar, Kind);
61 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
62 return getNamedSection(Name.c_str(), Flags);
65 case SectionKind::Data:
66 return getDataSection_();
67 case SectionKind::BSS:
68 // ELF targets usually have BSS sections
69 return getBSSSection_();
70 case SectionKind::ROData:
71 return getReadOnlySection_();
72 case SectionKind::RODataMergeStr:
73 return MergeableStringSection(GVar);
74 case SectionKind::RODataMergeConst:
75 return MergeableConstSection(GVar);
76 case SectionKind::ThreadData:
77 // ELF targets usually support TLS stuff
78 return getTLSDataSection_();
79 case SectionKind::ThreadBSS:
80 return getTLSBSSSection_();
82 assert(0 && "Unsuported section kind for global");
86 assert(0 && "Unsupported global");
90 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
91 const TargetData *TD = ETM->getTargetData();
92 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
93 const Type *Type = C->getType();
95 // FIXME: string here is temporary, until stuff will fully land in.
96 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
97 // currently directly used by asmprinter.
98 unsigned Size = TD->getABITypeSize(Type);
99 if (Size == 4 || Size == 8 || Size == 16) {
100 std::string Name = ".rodata.cst" + utostr(Size);
102 return getNamedSection(Name.c_str(),
103 SectionFlags::setEntitySize(SectionFlags::Mergeable,
107 return getReadOnlySection_();
111 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
112 const TargetData *TD = ETM->getTargetData();
113 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
114 const ConstantArray *CVA = cast<ConstantArray>(C);
115 const Type *Type = CVA->getType()->getElementType();
117 unsigned Size = TD->getABITypeSize(Type);
119 // We also need alignment here
120 const TargetData *TD = ETM->getTargetData();
121 unsigned Align = TD->getPrefTypeAlignment(Type);
125 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
126 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
127 SectionFlags::Strings,
129 return getNamedSection(Name.c_str(), Flags);
132 return getReadOnlySection_();
135 std::string ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
136 std::string Flags = ",\"";
138 if (!(flags & SectionFlags::Debug))
140 if (flags & SectionFlags::Code)
142 if (flags & SectionFlags::Writeable)
144 if (flags & SectionFlags::Mergeable)
146 if (flags & SectionFlags::Strings)
148 if (flags & SectionFlags::TLS)
153 // FIXME: There can be exceptions here
154 if (flags & SectionFlags::BSS)
157 Flags += ",@progbits";
159 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
160 Flags += "," + utostr(entitySize);