blob: 624b95c7b6c162e643545d7665d0c968a507c83a [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!");
Rafael Espindolabb46f522009-01-15 20:18:42 +000047 case Function::PrivateLinkage:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000048 case Function::InternalLinkage:
49 case Function::DLLExportLinkage:
50 case Function::ExternalLinkage:
Anton Korobeynikov0b501d12008-09-24 22:16:33 +000051 return TextSection;
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)) {
Duncan Sands5df31862008-09-29 11:25:42 +000059 if (GVar->mayBeOverridden()) {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000060 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 Korobeynikov0b501d12008-09-24 22:16:33 +000067 return DataSection;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000068 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 Korobeynikov00181a32008-09-24 22:20:27 +000074 return getReadOnlySection();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000075 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
Anton Korobeynikov36133dd2008-09-24 22:17:06 +000081 return TLSDataSection;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000082 case SectionKind::ThreadBSS:
Anton Korobeynikov36133dd2008-09-24 22:17:06 +000083 return TLSBSSSection;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000084 default:
85 assert(0 && "Unsuported section kind for global");
86 }
87 }
88 } else
89 assert(0 && "Unsupported global");
Devang Patel8a84e442009-01-05 17:31:22 +000090
91 return NULL;
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000092}
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 Korobeynikovdbab2d22008-09-24 22:17:27 +0000102 Constant *C = 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 {
Dan Gohman8f092252008-11-03 18:22:42 +0000108 const TargetData *TD = TM.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.
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000113 unsigned Size = TD->getTypePaddedSize(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
Anton Korobeynikov00181a32008-09-24 22:20:27 +0000122 return getReadOnlySection();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000123}
124
125const Section*
126ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
Dan Gohman8f092252008-11-03 18:22:42 +0000127 const TargetData *TD = TM.getTargetData();
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000128 Constant *C = cast<GlobalVariable>(GV)->getInitializer();
Anton Korobeynikov72bb4022009-01-27 22:29:24 +0000129 const Type *Ty = cast<ArrayType>(C->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}