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 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);
41 ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
42 SectionKind::Kind Kind = SectionKindForGlobal(GV);
44 if (const Function *F = dyn_cast<Function>(GV)) {
45 switch (F->getLinkage()) {
46 default: assert(0 && "Unknown linkage type!");
47 case Function::InternalLinkage:
48 case Function::DLLExportLinkage:
49 case Function::ExternalLinkage:
51 case Function::WeakLinkage:
52 case Function::LinkOnceLinkage:
53 std::string Name = UniqueSectionForGlobal(GV, Kind);
54 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
55 return getNamedSection(Name.c_str(), Flags);
57 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
58 if (GVar->mayBeOverridden()) {
59 std::string Name = UniqueSectionForGlobal(GVar, Kind);
60 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
61 return getNamedSection(Name.c_str(), Flags);
64 case SectionKind::Data:
65 case SectionKind::SmallData:
67 case SectionKind::BSS:
68 case SectionKind::SmallBSS:
69 // ELF targets usually have BSS sections
70 return getBSSSection_();
71 case SectionKind::ROData:
72 case SectionKind::SmallROData:
73 return getReadOnlySection();
74 case SectionKind::RODataMergeStr:
75 return MergeableStringSection(GVar);
76 case SectionKind::RODataMergeConst:
77 return MergeableConstSection(GVar);
78 case SectionKind::ThreadData:
79 // ELF targets usually support TLS stuff
80 return TLSDataSection;
81 case SectionKind::ThreadBSS:
84 assert(0 && "Unsuported section kind for global");
88 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 = GV->getInitializer();
102 return MergeableConstSection(C->getType());
105 inline const Section*
106 ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
107 const TargetData *TD = TM.getTargetData();
109 // FIXME: string here is temporary, until stuff will fully land in.
110 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
111 // currently directly used by asmprinter.
112 unsigned Size = TD->getTypePaddedSize(Ty);
113 if (Size == 4 || Size == 8 || Size == 16) {
114 std::string Name = ".rodata.cst" + utostr(Size);
116 return getNamedSection(Name.c_str(),
117 SectionFlags::setEntitySize(SectionFlags::Mergeable,
121 return getReadOnlySection();
125 ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
126 const TargetData *TD = TM.getTargetData();
127 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
128 const ConstantArray *CVA = cast<ConstantArray>(C);
129 const Type *Ty = CVA->getType()->getElementType();
131 unsigned Size = TD->getTypePaddedSize(Ty);
133 assert(getCStringSection() && "Should have string section prefix");
135 // We also need alignment here
136 unsigned Align = TD->getPrefTypeAlignment(Ty);
140 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
141 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
142 SectionFlags::Strings,
144 return getNamedSection(Name.c_str(), Flags);
147 return getReadOnlySection();
150 std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
151 std::string Flags = ",\"";
153 if (!(flags & SectionFlags::Debug))
155 if (flags & SectionFlags::Code)
157 if (flags & SectionFlags::Writeable)
159 if (flags & SectionFlags::Mergeable)
161 if (flags & SectionFlags::Strings)
163 if (flags & SectionFlags::TLS)
165 if (flags & SectionFlags::Small)
170 // If comment string is '@', e.g. as on ARM - use '%' instead
171 if (strcmp(CommentString, "@") == 0)
176 // FIXME: There can be exceptions here
177 if (flags & SectionFlags::BSS)
182 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
183 Flags += "," + utostr(entitySize);