blob: 523527c7b6d70438861513a326cef9b7fb7ad970 [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"
Anton Korobeynikov93cacf12008-08-07 09:50:34 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000021#include "llvm/Target/ELFTargetAsmInfo.h"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Target/TargetData.h"
24
25using namespace llvm;
26
27ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM) {
28 ETM = &TM;
29
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);
39
40}
41
42const Section*
Evan Cheng711b6dc2008-08-08 06:56:16 +000043ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV,
44 bool NoCoalesce) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000045 SectionKind::Kind Kind = SectionKindForGlobal(GV);
46
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);
59 }
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);
65 } else {
66 switch (Kind) {
67 case SectionKind::Data:
Anton Korobeynikov09809802008-07-22 17:09:41 +000068 case SectionKind::SmallData:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000069 return getDataSection_();
70 case SectionKind::BSS:
Anton Korobeynikov09809802008-07-22 17:09:41 +000071 case SectionKind::SmallBSS:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000072 // ELF targets usually have BSS sections
73 return getBSSSection_();
74 case SectionKind::ROData:
Anton Korobeynikov09809802008-07-22 17:09:41 +000075 case SectionKind::SmallROData:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000076 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_();
86 default:
87 assert(0 && "Unsuported section kind for global");
88 }
89 }
90 } else
91 assert(0 && "Unsupported global");
92}
93
94const Section*
Anton Korobeynikov93cacf12008-08-07 09:50:34 +000095ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
96 // FIXME: Support data.rel stuff someday
97 return MergeableConstSection(Ty);
98}
99
100const Section*
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000101ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000102 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000103 return MergeableConstSection(C->getType());
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000104}
105
106inline const Section*
107ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
108 const TargetData *TD = ETM->getTargetData();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000109
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.
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000113 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000114 if (Size == 4 || Size == 8 || Size == 16) {
115 std::string Name = ".rodata.cst" + utostr(Size);
116
117 return getNamedSection(Name.c_str(),
118 SectionFlags::setEntitySize(SectionFlags::Mergeable,
119 Size));
120 }
121
122 return getReadOnlySection_();
123}
124
125const Section*
126ELFTargetAsmInfo::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);
Anton Korobeynikov96855062008-07-21 18:29:23 +0000130 const Type *Ty = CVA->getType()->getElementType();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000131
Anton Korobeynikov96855062008-07-21 18:29:23 +0000132 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000133 if (Size <= 16) {
Anton Korobeynikov1e27da32008-08-07 09:54:40 +0000134 assert(getCStringSection() && "Should have string section prefix");
135
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000136 // We also need alignment here
137 const TargetData *TD = ETM->getTargetData();
Anton Korobeynikov96855062008-07-21 18:29:23 +0000138 unsigned Align = TD->getPrefTypeAlignment(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000139 if (Align < Size)
140 Align = Size;
141
142 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
143 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
144 SectionFlags::Strings,
145 Size);
146 return getNamedSection(Name.c_str(), Flags);
147 }
148
149 return getReadOnlySection_();
150}
151
152std::string ELFTargetAsmInfo::PrintSectionFlags(unsigned flags) const {
153 std::string Flags = ",\"";
154
155 if (!(flags & SectionFlags::Debug))
156 Flags += 'a';
157 if (flags & SectionFlags::Code)
158 Flags += 'x';
159 if (flags & SectionFlags::Writeable)
160 Flags += 'w';
161 if (flags & SectionFlags::Mergeable)
162 Flags += 'M';
163 if (flags & SectionFlags::Strings)
164 Flags += 'S';
165 if (flags & SectionFlags::TLS)
166 Flags += 'T';
Anton Korobeynikov09809802008-07-22 17:09:41 +0000167 if (flags & SectionFlags::Small)
168 Flags += 's';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000169
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000170 Flags += "\",";
171
172 // If comment string is '@', e.g. as on ARM - use '%' instead
173 if (strcmp(CommentString, "@") == 0)
174 Flags += '%';
175 else
176 Flags += '@';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000177
178 // FIXME: There can be exceptions here
179 if (flags & SectionFlags::BSS)
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000180 Flags += "nobits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000181 else
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000182 Flags += "progbits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000183
184 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
185 Flags += "," + utostr(entitySize);
186
187 return Flags;
188}
189