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,
44 bool NoCoalesce) const {
45 SectionKind::Kind Kind = SectionKindForGlobal(GV);
47 if (const Function *F = dyn_cast<Function>(GV)) {
48 switch (F->getLinkage()) {
49 default: assert(0 && "Unknown linkage type!");
50 case Function::InternalLinkage:
51 case Function::DLLExportLinkage:
52 case Function::ExternalLinkage:
53 return getTextSection_();
54 case Function::WeakLinkage:
55 case Function::LinkOnceLinkage:
56 std::string Name = UniqueSectionForGlobal(GV, Kind);
57 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
58 return getNamedSection(Name.c_str(), Flags);
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);
67 case SectionKind::Data:
68 case SectionKind::SmallData:
69 return getDataSection_();
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 getTLSDataSection_();
84 case SectionKind::ThreadBSS:
85 return getTLSBSSSection_();
87 assert(0 && "Unsuported section kind for global");
91 assert(0 && "Unsupported global");
95 ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
96 // FIXME: Support data.rel stuff someday
97 return MergeableConstSection(Ty);
101 ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
102 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
103 return MergeableConstSection(C->getType());
106 inline const Section*
107 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
108 const TargetData *TD = ETM->getTargetData();
110 // FIXME: string here is temporary, until stuff will fully land in.
111 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
112 // currently directly used by asmprinter.
113 unsigned Size = TD->getABITypeSize(Ty);
114 if (Size == 4 || Size == 8 || Size == 16) {
115 std::string Name = ".rodata.cst" + utostr(Size);
117 return getNamedSection(Name.c_str(),
118 SectionFlags::setEntitySize(SectionFlags::Mergeable,
122 return getReadOnlySection_();
126 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
127 const TargetData *TD = ETM->getTargetData();
128 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
129 const ConstantArray *CVA = cast<ConstantArray>(C);
130 const Type *Ty = CVA->getType()->getElementType();
132 unsigned Size = TD->getABITypeSize(Ty);
134 assert(getCStringSection() && "Should have string section prefix");
136 // We also need alignment here
137 const TargetData *TD = ETM->getTargetData();
138 unsigned Align = TD->getPrefTypeAlignment(Ty);
142 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
143 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
144 SectionFlags::Strings,
146 return getNamedSection(Name.c_str(), Flags);
149 return getReadOnlySection_();
152 std::string ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
153 std::string Flags = ",\"";
155 if (!(flags & SectionFlags::Debug))
157 if (flags & SectionFlags::Code)
159 if (flags & SectionFlags::Writeable)
161 if (flags & SectionFlags::Mergeable)
163 if (flags & SectionFlags::Strings)
165 if (flags & SectionFlags::TLS)
167 if (flags & SectionFlags::Small)
172 // If comment string is '@', e.g. as on ARM - use '%' instead
173 if (strcmp(CommentString, "@") == 0)
178 // FIXME: There can be exceptions here
179 if (flags & SectionFlags::BSS)
184 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
185 Flags += "," + utostr(entitySize);