blob: 9ffc72d2340d5d98c4b0c586a7cc5d396c3177f2 [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 Cheng42ccc212008-08-08 17:56:50 +000043ELFTargetAsmInfo::SelectSectionForGlobal(const GlobalValue *GV) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000044 SectionKind::Kind Kind = SectionKindForGlobal(GV);
45
46 if (const Function *F = dyn_cast<Function>(GV)) {
47 switch (F->getLinkage()) {
48 default: assert(0 && "Unknown linkage type!");
49 case Function::InternalLinkage:
50 case Function::DLLExportLinkage:
51 case Function::ExternalLinkage:
52 return getTextSection_();
53 case Function::WeakLinkage:
54 case Function::LinkOnceLinkage:
55 std::string Name = UniqueSectionForGlobal(GV, Kind);
56 unsigned Flags = SectionFlagsForGlobal(GV, Name.c_str());
57 return getNamedSection(Name.c_str(), Flags);
58 }
59 } else if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) {
60 if (GVar->isWeakForLinker()) {
61 std::string Name = UniqueSectionForGlobal(GVar, Kind);
62 unsigned Flags = SectionFlagsForGlobal(GVar, Name.c_str());
63 return getNamedSection(Name.c_str(), Flags);
64 } else {
65 switch (Kind) {
66 case SectionKind::Data:
Anton Korobeynikov09809802008-07-22 17:09:41 +000067 case SectionKind::SmallData:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000068 return getDataSection_();
69 case SectionKind::BSS:
Anton Korobeynikov09809802008-07-22 17:09:41 +000070 case SectionKind::SmallBSS:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000071 // ELF targets usually have BSS sections
72 return getBSSSection_();
73 case SectionKind::ROData:
Anton Korobeynikov09809802008-07-22 17:09:41 +000074 case SectionKind::SmallROData:
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +000075 return getReadOnlySection_();
76 case SectionKind::RODataMergeStr:
77 return MergeableStringSection(GVar);
78 case SectionKind::RODataMergeConst:
79 return MergeableConstSection(GVar);
80 case SectionKind::ThreadData:
81 // ELF targets usually support TLS stuff
82 return getTLSDataSection_();
83 case SectionKind::ThreadBSS:
84 return getTLSBSSSection_();
85 default:
86 assert(0 && "Unsuported section kind for global");
87 }
88 }
89 } else
90 assert(0 && "Unsupported global");
91}
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 Korobeynikovdebe34b2008-07-19 13:14:11 +0000101 Constant *C = cast<GlobalVariable>(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 {
107 const TargetData *TD = ETM->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.
Anton Korobeynikov93cacf12008-08-07 09:50:34 +0000112 unsigned Size = TD->getABITypeSize(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
121 return getReadOnlySection_();
122}
123
124const Section*
125ELFTargetAsmInfo::MergeableStringSection(const GlobalVariable *GV) const {
126 const TargetData *TD = ETM->getTargetData();
127 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
Anton Korobeynikov96855062008-07-21 18:29:23 +0000131 unsigned Size = TD->getABITypeSize(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
136 const TargetData *TD = ETM->getTargetData();
Anton Korobeynikov96855062008-07-21 18:29:23 +0000137 unsigned Align = TD->getPrefTypeAlignment(Ty);
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000138 if (Align < Size)
139 Align = Size;
140
141 std::string Name = getCStringSection() + utostr(Size) + '.' + utostr(Align);
142 unsigned Flags = SectionFlags::setEntitySize(SectionFlags::Mergeable |
143 SectionFlags::Strings,
144 Size);
145 return getNamedSection(Name.c_str(), Flags);
146 }
147
148 return getReadOnlySection_();
149}
150
Anton Korobeynikovd0c1e292008-08-16 12:57:07 +0000151std::string ELFTargetAsmInfo::printSectionFlags(unsigned flags) const {
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000152 std::string Flags = ",\"";
153
154 if (!(flags & SectionFlags::Debug))
155 Flags += 'a';
156 if (flags & SectionFlags::Code)
157 Flags += 'x';
158 if (flags & SectionFlags::Writeable)
159 Flags += 'w';
160 if (flags & SectionFlags::Mergeable)
161 Flags += 'M';
162 if (flags & SectionFlags::Strings)
163 Flags += 'S';
164 if (flags & SectionFlags::TLS)
165 Flags += 'T';
Anton Korobeynikov09809802008-07-22 17:09:41 +0000166 if (flags & SectionFlags::Small)
167 Flags += 's';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000168
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000169 Flags += "\",";
170
171 // If comment string is '@', e.g. as on ARM - use '%' instead
172 if (strcmp(CommentString, "@") == 0)
173 Flags += '%';
174 else
175 Flags += '@';
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000176
177 // FIXME: There can be exceptions here
178 if (flags & SectionFlags::BSS)
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000179 Flags += "nobits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000180 else
Anton Korobeynikovfeac94b2008-08-07 09:55:06 +0000181 Flags += "progbits";
Anton Korobeynikovdebe34b2008-07-19 13:14:11 +0000182
183 if (unsigned entitySize = SectionFlags::getEntitySize(flags))
184 Flags += "," + utostr(entitySize);
185
186 return Flags;
187}
188