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/CodeGen/MachineConstantPool.h"
21 #include "llvm/Target/ELFTargetAsmInfo.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Target/TargetData.h"
27 ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) {
30 TextSection_ = getUnnamedSection("\t.text", SectionFlags::Code);
31 DataSection_ = getUnnamedSection("\t.data", SectionFlags::Writeable);
32 BSSSection_ = getUnnamedSection("\t.bss",
33 SectionFlags::Writeable | SectionFlags::BSS);
34 ReadOnlySection_ = getNamedSection("\t.rodata", SectionFlags::None);
35 TLSDataSection_ = getNamedSection("\t.tdata",
36 SectionFlags::Writeable | SectionFlags::TLS);
37 TLSBSSSection_ = getNamedSection("\t.tbss",
38 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
43 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
44 SectionKind::Kind Kind = SectionKindForGlobal(GV);
46 if (const Function *F = dyn_cast<Function>(GV)) {
47 switch (F->getLinkage()) {
48 default: assert(0 && "Unknown linkage type!");
49 case Function::InternalLinkage:
50 case Function::DLLExportLinkage:
51 case Function::ExternalLinkage:
52 return getTextSection_();
53 case Function::WeakLinkage:
54 case Function::LinkOnceLinkage:
55 std::string Name = UniqueSectionForGlobal(GV, Kind);
56 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
57 return getNamedSection(Name.c_str(), Flags);
59 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
60 if (GVar->isWeakForLinker()) {
61 std::string Name = UniqueSectionForGlobal(GVar, Kind);
62 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
63 return getNamedSection(Name.c_str(), Flags);
66 case SectionKind::Data:
67 case SectionKind::SmallData:
68 return getDataSection_();
69 case SectionKind::BSS:
70 case SectionKind::SmallBSS:
71 // ELF targets usually have BSS sections
72 return getBSSSection_();
73 case SectionKind::ROData:
74 case SectionKind::SmallROData:
75 return getReadOnlySection_();
76 case SectionKind::RODataMergeStr:
77 return MergeableStringSection(GVar);
78 case SectionKind::RODataMergeConst:
79 return MergeableConstSection(GVar);
80 case SectionKind::ThreadData:
81 // ELF targets usually support TLS stuff
82 return getTLSDataSection_();
83 case SectionKind::ThreadBSS:
84 return getTLSBSSSection_();
86 assert(0 && "Unsuported section kind for global");
90 assert(0 && "Unsupported global");
94 ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
95 // FIXME: Support data.rel stuff someday
96 return MergeableConstSection(Ty);
100 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
101 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
102 const Type *Ty = C->getType();
104 return MergeableConstSection(Ty);
107 inline const Section*
108 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
109 const TargetData *TD = ETM->getTargetData();
111 // FIXME: string here is temporary, until stuff will fully land in.
112 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
113 // currently directly used by asmprinter.
114 unsigned Size = TD->getABITypeSize(Ty);
115 if (Size == 4 || Size == 8 || Size == 16) {
116 std::string Name = ".rodata.cst" + utostr(Size);
118 return getNamedSection(Name.c_str(),
119 SectionFlags::setEntitySize(SectionFlags::Mergeable,
123 return getReadOnlySection_();
127 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
128 const TargetData *TD = ETM->getTargetData();
129 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
130 const ConstantArray *CVA = cast<ConstantArray>(C);
131 const Type *Ty = CVA->getType()->getElementType();
133 unsigned Size = TD->getABITypeSize(Ty);
135 // We also need alignment here
136 const TargetData *TD = ETM->getTargetData();
137 unsigned Align = TD->getPrefTypeAlignment(Ty);
141 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
142 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
143 SectionFlags::Strings,
145 return getNamedSection(Name.c_str(), Flags);
148 return getReadOnlySection_();
151 std::string ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
152 std::string Flags = ",\"";
154 if (!(flags & SectionFlags::Debug))
156 if (flags & SectionFlags::Code)
158 if (flags & SectionFlags::Writeable)
160 if (flags & SectionFlags::Mergeable)
162 if (flags & SectionFlags::Strings)
164 if (flags & SectionFlags::TLS)
166 if (flags & SectionFlags::Small)
171 // FIXME: There can be exceptions here
172 if (flags & SectionFlags::BSS)
175 Flags += ",@progbits";
177 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
178 Flags += "," + utostr(entitySize);