blob: bf96d9bfa8e5d0740f656c03465505fb933fca7b [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
Dan Gohman8f092252008-11-03 18:22:42 +000027ELFTargetAsmInfo::ELFTargetAsmInfo(const TargetMachine &TM)
28 : TargetAsmInfo(TM) {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000029
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000030 BSSSection_ = getUnnamedSection("\t.bss",
31 SectionFlags::Writeable | SectionFlags::BSS);
Anton Korobeynikov00181a32008-09-24 22:20:27 +000032 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)) {
Duncan Sands5df31862008-09-29 11:25:42 +000058 if (GVar->mayBeOverridden()) {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000059 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 Korobeynikov00181a32008-09-24 22:20:27 +000073 return getReadOnlySection();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000074 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");
Devang Patel8a84e442009-01-05 17:31:22 +000089
90 return NULL;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000091}
92
93const Section*
Anton Korobeynikov93cacf12008-08-07 09:50:34 +000094ELFTargetAsmInfo::SelectSectionForMachineConst(const Type *Ty) const {
95 // FIXME: Support data.rel stuff someday
96 return MergeableConstSection(Ty);
97}
98
99const Section*
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000100ELFTargetAsmInfo::MergeableConstSection(const GlobalVariable *GV) const {
Anton Korobeynikovdbab2d22008-09-24 22:17:27 +0000101 Constant *C = GV->getInitializer();
Anton Korobeynikov84e160e2008-08-07 09:51:02 +0000102 return MergeableConstSection(C->getType());
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000103}
104
105inline const Section*
106ELFTargetAsmInfo::MergeableConstSection(const Type *Ty) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000107 const TargetData *TD = TM.getTargetData();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000108
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.
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000112 unsigned Size = TD->getTypePaddedSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000113 if (Size == 4 || Size == 8 || Size == 16) {
114 std::string Name = ".rodata.cst" + utostr(Size);
115
116 return getNamedSection(Name.c_str(),
117 SectionFlags::setEntitySize(SectionFlags::Mergeable,
118 Size));
119 }
120
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000121 return getReadOnlySection();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000122}
123
124const Section*
125ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000126 const TargetData *TD = TM.getTargetData();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000127 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
128 const ConstantArray *CVA = cast<ConstantArray>(C);
Anton Korobeynikov96855062008-07-21 18:29:23 +0000129 const Type *Ty = CVA->getType()->getElementType();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000130
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000131 unsigned Size = TD->getTypePaddedSize(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000132 if (Size <= 16) {
Anton Korobeynikov1e27da32008-08-07 09:54:40 +0000133 assert(getCStringSection() && "Should have string section prefix");
134
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000135 // We also need alignment here
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
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000147 return getReadOnlySection();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000148}
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