blob: 7c9e4546d252f9f388260044c9a38122fec8be90 [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 BSSSection_ = getUnnamedSection("\t.bss",
31 SectionFlags::Writeable | SectionFlags::BSS);
32 ReadOnlySection_ = getNamedSection("\t.rodata", SectionFlags::None);
Anton Korobeynikov36133dd2008-09-24 22:17:06 +000033 TLSDataSection = getNamedSection("\t.tdata",
34 SectionFlags::Writeable | SectionFlags::TLS);
35 TLSBSSSection = getNamedSection("\t.tbss",
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000036 SectionFlags::Writeable | SectionFlags::TLS | SectionFlags::BSS);
37
38}
39
40const Section*
Evan Cheng42ccc212008-08-08 17:56:50 +000041ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000042 SectionKind::Kind Kind = SectionKindForGlobal(GV);
43
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:
Anton Korobeynikov0b501d12008-09-24 22:16:33 +000050 return TextSection;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000051 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);
56 }
57 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
58 if (GVar->isWeakForLinker()) {
59 std::string Name = UniqueSectionForGlobal(GVar, Kind);
60 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
61 return getNamedSection(Name.c_str(), Flags);
62 } else {
63 switch (Kind) {
64 case SectionKind::Data:
Anton Korobeynikov09809802008-07-22 17:09:41 +000065 case SectionKind::SmallData:
Anton Korobeynikov0b501d12008-09-24 22:16:33 +000066 return DataSection;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000067 case SectionKind::BSS:
Anton Korobeynikov09809802008-07-22 17:09:41 +000068 case SectionKind::SmallBSS:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000069 // ELF targets usually have BSS sections
70 return getBSSSection_();
71 case SectionKind::ROData:
Anton Korobeynikov09809802008-07-22 17:09:41 +000072 case SectionKind::SmallROData:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000073 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
Anton Korobeynikov36133dd2008-09-24 22:17:06 +000080 return TLSDataSection;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000081 case SectionKind::ThreadBSS:
Anton Korobeynikov36133dd2008-09-24 22:17:06 +000082 return TLSBSSSection;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000083 default:
84 assert(0 && "Unsuported section kind for global");
85 }
86 }
87 } else
88 assert(0 && "Unsupported global");
89}
90
91const Section*
Anton Korobeynikov93cacf12008-08-07 09:50:34 +000092ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
93 // FIXME: Support data.rel stuff someday
94 return MergeableConstSection(Ty);
95}
96
97const Section*
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000098ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikovdbab2d22008-09-24 22:17:27 +000099 Constant *C = GV->getInitializer();
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000100 return MergeableConstSection(C->getType());
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000101}
102
103inline const Section*
104ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
105 const TargetData *TD = ETM->getTargetData();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000106
107 // FIXME: string here is temporary, until stuff will fully land in.
108 // We cannot use {Four,Eight,Sixteen}ByteConstantSection here, since it's
109 // currently directly used by asmprinter.
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000110 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000111 if (Size == 4 || Size == 8 || Size == 16) {
112 std::string Name = ".rodata.cst" + utostr(Size);
113
114 return getNamedSection(Name.c_str(),
115 SectionFlags::setEntitySize(SectionFlags::Mergeable,
116 Size));
117 }
118
119 return getReadOnlySection_();
120}
121
122const Section*
123ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
124 const TargetData *TD = ETM->getTargetData();
125 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
126 const ConstantArray *CVA = cast<ConstantArray>(C);
Anton Korobeynikov96855062008-07-21 18:29:23 +0000127 const Type *Ty = CVA->getType()->getElementType();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000128
Anton Korobeynikov96855062008-07-21 18:29:23 +0000129 unsigned Size = TD->getABITypeSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000130 if (Size <= 16) {
Anton Korobeynikov1e27da32008-08-07 09:54:40 +0000131 assert(getCStringSection() && "Should have string section prefix");
132
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000133 // We also need alignment here
134 const TargetData *TD = ETM->getTargetData();
Anton Korobeynikov96855062008-07-21 18:29:23 +0000135 unsigned Align = TD->getPrefTypeAlignment(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000136 if (Align < Size)
137 Align = Size;
138
139 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
140 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
141 SectionFlags::Strings,
142 Size);
143 return getNamedSection(Name.c_str(), Flags);
144 }
145
146 return getReadOnlySection_();
147}
148
Anton Korobeynikovd0c1e292008-08-16 12:57:07 +0000149std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000150 std::string Flags = ",\"";
151
152 if (!(flags & SectionFlags::Debug))
153 Flags += 'a';
154 if (flags & SectionFlags::Code)
155 Flags += 'x';
156 if (flags & SectionFlags::Writeable)
157 Flags += 'w';
158 if (flags & SectionFlags::Mergeable)
159 Flags += 'M';
160 if (flags & SectionFlags::Strings)
161 Flags += 'S';
162 if (flags & SectionFlags::TLS)
163 Flags += 'T';
Anton Korobeynikov09809802008-07-22 17:09:41 +0000164 if (flags & SectionFlags::Small)
165 Flags += 's';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000166
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000167 Flags += "\",";
168
169 // If comment string is '@', e.g. as on ARM - use '%' instead
170 if (strcmp(CommentString, "@") == 0)
171 Flags += '%';
172 else
173 Flags += '@';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000174
175 // FIXME: There can be exceptions here
176 if (flags & SectionFlags::BSS)
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000177 Flags += "nobits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000178 else
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000179 Flags += "progbits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000180
181 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
182 Flags += "," + utostr(entitySize);
183
184 return Flags;
185}
186