blob: 6ce01a836b400f1760a0d76d17c0089f99ef903d [file] [log] [blame]
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +00001//===-- 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/Target/ELFTargetAsmInfo.h"
21#include "llvm/Target/TargetMachine.h"
22#include "llvm/Target/TargetData.h"
23
24using namespace llvm;
25
26ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) {
27 ETM = &TM;
28
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);
38
39}
40
41const Section*
42ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
43 SectionKind::Kind Kind = SectionKindForGlobal(GV);
44
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);
57 }
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);
63 } else {
64 switch (Kind) {
65 case SectionKind::Data:
Anton Korobeynikov09809802008-07-22 17:09:41 +000066 case SectionKind::SmallData:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000067 return getDataSection_();
68 case SectionKind::BSS:
Anton Korobeynikov09809802008-07-22 17:09:41 +000069 case SectionKind::SmallBSS:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000070 // ELF targets usually have BSS sections
71 return getBSSSection_();
72 case SectionKind::ROData:
Anton Korobeynikov09809802008-07-22 17:09:41 +000073 case SectionKind::SmallROData:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000074 return getReadOnlySection_();
75 case SectionKind::RODataMergeStr:
76 return MergeableStringSection(GVar);
77 case SectionKind::RODataMergeConst:
78 return MergeableConstSection(GVar);
79 case SectionKind::ThreadData:
80 // ELF targets usually support TLS stuff
81 return getTLSDataSection_();
82 case SectionKind::ThreadBSS:
83 return getTLSBSSSection_();
84 default:
85 assert(0 && "Unsuported section kind for global");
86 }
87 }
88 } else
89 assert(0 && "Unsupported global");
90}
91
92const Section*
93ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
94 const TargetData *TD = ETM->getTargetData();
95 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
96 const Type *Type = C->getType();
97
98 // FIXME: string here is temporary, until stuff will fully land in.
99 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
100 // currently directly used by asmprinter.
101 unsigned Size = TD->getABITypeSize(Type);
102 if (Size == 4 || Size == 8 || Size == 16) {
103 std::string Name = ".rodata.cst" + utostr(Size);
104
105 return getNamedSection(Name.c_str(),
106 SectionFlags::setEntitySize(SectionFlags::Mergeable,
107 Size));
108 }
109
110 return getReadOnlySection_();
111}
112
113const Section*
114ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
115 const TargetData *TD = ETM->getTargetData();
116 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
117 const ConstantArray *CVA = cast<ConstantArray>(C);
Anton Korobeynikov96855062008-07-21 18:29:23 +0000118 const Type *Ty = CVA->getType()->getElementType();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000119
Anton Korobeynikov96855062008-07-21 18:29:23 +0000120 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000121 if (Size <= 16) {
122 // We also need alignment here
123 const TargetData *TD = ETM->getTargetData();
Anton Korobeynikov96855062008-07-21 18:29:23 +0000124 unsigned Align = TD->getPrefTypeAlignment(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000125 if (Align < Size)
126 Align = Size;
127
128 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
129 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
130 SectionFlags::Strings,
131 Size);
132 return getNamedSection(Name.c_str(), Flags);
133 }
134
135 return getReadOnlySection_();
136}
137
138std::string ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
139 std::string Flags = ",\"";
140
141 if (!(flags & SectionFlags::Debug))
142 Flags += 'a';
143 if (flags & SectionFlags::Code)
144 Flags += 'x';
145 if (flags & SectionFlags::Writeable)
146 Flags += 'w';
147 if (flags & SectionFlags::Mergeable)
148 Flags += 'M';
149 if (flags & SectionFlags::Strings)
150 Flags += 'S';
151 if (flags & SectionFlags::TLS)
152 Flags += 'T';
Anton Korobeynikov09809802008-07-22 17:09:41 +0000153 if (flags & SectionFlags::Small)
154 Flags += 's';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000155
156 Flags += "\"";
157
158 // FIXME: There can be exceptions here
159 if (flags & SectionFlags::BSS)
160 Flags += ",@nobits";
161 else
162 Flags += ",@progbits";
163
164 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
165 Flags += "," + utostr(entitySize);
166
167 return Flags;
168}
169