blob: 4529d0158d9cafbb18bc55242a99dfb6ae5b48a9 [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
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000030 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*
Evan Cheng42ccc212008-08-08 17:56:50 +000042ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000043 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:
Anton Korobeynikovd7ca4162008-09-24 22:15:21 +000051 return getTextSection();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000052 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*
Anton Korobeynikov93cacf12008-08-07 09:50:34 +000093ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
94 // FIXME: Support data.rel stuff someday
95 return MergeableConstSection(Ty);
96}
97
98const Section*
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000099ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000100 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000101 return MergeableConstSection(C->getType());
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000102}
103
104inline const Section*
105ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
106 const TargetData *TD = ETM->getTargetData();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000107
108 // FIXME: string here is temporary, until stuff will fully land in.
109 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
110 // currently directly used by asmprinter.
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000111 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000112 if (Size == 4 || Size == 8 || Size == 16) {
113 std::string Name = ".rodata.cst" + utostr(Size);
114
115 return getNamedSection(Name.c_str(),
116 SectionFlags::setEntitySize(SectionFlags::Mergeable,
117 Size));
118 }
119
120 return getReadOnlySection_();
121}
122
123const Section*
124ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
125 const TargetData *TD = ETM->getTargetData();
126 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
127 const ConstantArray *CVA = cast<ConstantArray>(C);
Anton Korobeynikov96855062008-07-21 18:29:23 +0000128 const Type *Ty = CVA->getType()->getElementType();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000129
Anton Korobeynikov96855062008-07-21 18:29:23 +0000130 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000131 if (Size <= 16) {
Anton Korobeynikov1e27da32008-08-07 09:54:40 +0000132 assert(getCStringSection() && "Should have string section prefix");
133
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000134 // We also need alignment here
135 const TargetData *TD = ETM->getTargetData();
Anton Korobeynikov96855062008-07-21 18:29:23 +0000136 unsigned Align = TD->getPrefTypeAlignment(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000137 if (Align < Size)
138 Align = Size;
139
140 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
141 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
142 SectionFlags::Strings,
143 Size);
144 return getNamedSection(Name.c_str(), Flags);
145 }
146
147 return getReadOnlySection_();
148}
149
Anton Korobeynikovd0c1e292008-08-16 12:57:07 +0000150std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000151 std::string Flags = ",\"";
152
153 if (!(flags & SectionFlags::Debug))
154 Flags += 'a';
155 if (flags & SectionFlags::Code)
156 Flags += 'x';
157 if (flags & SectionFlags::Writeable)
158 Flags += 'w';
159 if (flags & SectionFlags::Mergeable)
160 Flags += 'M';
161 if (flags & SectionFlags::Strings)
162 Flags += 'S';
163 if (flags & SectionFlags::TLS)
164 Flags += 'T';
Anton Korobeynikov09809802008-07-22 17:09:41 +0000165 if (flags & SectionFlags::Small)
166 Flags += 's';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000167
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000168 Flags += "\",";
169
170 // If comment string is '@', e.g. as on ARM - use '%' instead
171 if (strcmp(CommentString, "@") == 0)
172 Flags += '%';
173 else
174 Flags += '@';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000175
176 // FIXME: There can be exceptions here
177 if (flags & SectionFlags::BSS)
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000178 Flags += "nobits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000179 else
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000180 Flags += "progbits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000181
182 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
183 Flags += "," + utostr(entitySize);
184
185 return Flags;
186}
187