blob: 88a978ac623ac9ac57b7e0f88f316a9fd0f8825a [file] [log] [blame]
Chris Lattnerf0144122009-07-28 03:13:23 +00001//===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
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 implements classes used to handle lowerings specific to common
11// object file formats.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Target/TargetLoweringObjectFile.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
Dan Gohmanffef8ac2009-08-11 16:02:12 +000018#include "llvm/Function.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000019#include "llvm/GlobalVariable.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000020#include "llvm/CodeGen/MachineModuleInfoImpls.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000021#include "llvm/MC/MCContext.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000022#include "llvm/MC/MCExpr.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000023#include "llvm/MC/MCSectionMachO.h"
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000024#include "llvm/MC/MCSectionELF.h"
Chris Lattner8da8d4b2010-01-13 21:29:21 +000025#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000026#include "llvm/Target/Mangler.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000027#include "llvm/Target/TargetData.h"
Chris Lattner5277b222009-08-08 20:43:12 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000029#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000030#include "llvm/Support/Dwarf.h"
Chris Lattner8f9b0f62009-11-07 09:20:54 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattner8da8d4b2010-01-13 21:29:21 +000032#include "llvm/Support/raw_ostream.h"
Chris Lattner5dc47ff2009-08-12 23:55:02 +000033#include "llvm/ADT/SmallString.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000034#include "llvm/ADT/StringExtras.h"
35using namespace llvm;
36
37//===----------------------------------------------------------------------===//
38// Generic Code
39//===----------------------------------------------------------------------===//
40
Chris Lattnera87dea42009-07-31 18:48:30 +000041TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000042 TextSection = 0;
43 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000044 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000045 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000046 StaticCtorSection = 0;
47 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000048 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000049 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000050
51 DwarfAbbrevSection = 0;
52 DwarfInfoSection = 0;
53 DwarfLineSection = 0;
54 DwarfFrameSection = 0;
55 DwarfPubNamesSection = 0;
56 DwarfPubTypesSection = 0;
57 DwarfDebugInlineSection = 0;
58 DwarfStrSection = 0;
59 DwarfLocSection = 0;
60 DwarfARangesSection = 0;
61 DwarfRangesSection = 0;
62 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000063}
64
65TargetLoweringObjectFile::~TargetLoweringObjectFile() {
66}
67
68static bool isSuitableForBSS(const GlobalVariable *GV) {
69 Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000070
Chris Lattnerf0144122009-07-28 03:13:23 +000071 // Must have zero initializer.
72 if (!C->isNullValue())
73 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000074
Chris Lattnerf0144122009-07-28 03:13:23 +000075 // Leave constant zeros in readonly constant sections, so they can be shared.
76 if (GV->isConstant())
77 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000078
Chris Lattnerf0144122009-07-28 03:13:23 +000079 // If the global has an explicit section specified, don't put it in BSS.
80 if (!GV->getSection().empty())
81 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000082
Chris Lattnerf0144122009-07-28 03:13:23 +000083 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
84 if (NoZerosInBSS)
85 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000086
Chris Lattnerf0144122009-07-28 03:13:23 +000087 // Otherwise, put it in BSS!
88 return true;
89}
90
Chris Lattner1850e5a2009-08-04 16:13:09 +000091/// IsNullTerminatedString - Return true if the specified constant (which is
92/// known to have a type that is an array of 1/2/4 byte elements) ends with a
93/// nul value and contains no other nuls in it.
94static bool IsNullTerminatedString(const Constant *C) {
95 const ArrayType *ATy = cast<ArrayType>(C->getType());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000096
Chris Lattnerf0144122009-07-28 03:13:23 +000097 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000098 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
99 if (ATy->getNumElements() == 0) return false;
100
101 ConstantInt *Null =
102 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
103 if (Null == 0 || Null->getZExtValue() != 0)
104 return false; // Not null terminated.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000105
Chris Lattner1850e5a2009-08-04 16:13:09 +0000106 // Verify that the null doesn't occur anywhere else in the string.
107 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
108 // Reject constantexpr elements etc.
109 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
110 CVA->getOperand(i) == Null)
111 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000112 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000113 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000114
115 // Another possibility: [1 x i8] zeroinitializer
116 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000117 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000118
119 return false;
120}
121
Chris Lattner58bed8f2009-08-05 04:25:40 +0000122/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000123/// a global variable. Given an global variable and information from TM, it
124/// classifies the global in a variety of ways that make various target
125/// implementations simpler. The target implementation is free to ignore this
126/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000127SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
128 const TargetMachine &TM){
129 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
130 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000131
Chris Lattnerf0144122009-07-28 03:13:23 +0000132 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000133
Chris Lattnerf0144122009-07-28 03:13:23 +0000134 // Early exit - functions should be always in text sections.
135 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
136 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000137 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000138
Chris Lattnerf0144122009-07-28 03:13:23 +0000139 // Handle thread-local data first.
140 if (GVar->isThreadLocal()) {
141 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000142 return SectionKind::getThreadBSS();
143 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000144 }
145
Chris Lattnera3839bc2010-01-19 02:48:26 +0000146 // Variables with common linkage always get classified as common.
147 if (GVar->hasCommonLinkage())
148 return SectionKind::getCommon();
149
Chris Lattnerf0144122009-07-28 03:13:23 +0000150 // Variable can be easily put to BSS section.
Chris Lattnerce8749e2010-01-19 04:15:51 +0000151 if (isSuitableForBSS(GVar)) {
152 if (GVar->hasLocalLinkage())
153 return SectionKind::getBSSLocal();
154 else if (GVar->hasExternalLinkage())
155 return SectionKind::getBSSExtern();
Chris Lattner27981192009-08-01 23:57:16 +0000156 return SectionKind::getBSS();
Chris Lattnerce8749e2010-01-19 04:15:51 +0000157 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000158
159 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000160
Chris Lattnerf0144122009-07-28 03:13:23 +0000161 // If the global is marked constant, we can put it into a mergable section,
162 // a mergable string section, or general .data if it contains relocations.
163 if (GVar->isConstant()) {
164 // If the initializer for the global contains something that requires a
165 // relocation, then we may have to drop this into a wriable data section
166 // even though it is marked const.
167 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000168 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000169 case Constant::NoRelocation:
170 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000171 // section of the right width.
172 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000173 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000174 dyn_cast<IntegerType>(ATy->getElementType())) {
175 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
176 ITy->getBitWidth() == 32) &&
177 IsNullTerminatedString(C)) {
178 if (ITy->getBitWidth() == 8)
179 return SectionKind::getMergeable1ByteCString();
180 if (ITy->getBitWidth() == 16)
181 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000182
Chris Lattner1850e5a2009-08-04 16:13:09 +0000183 assert(ITy->getBitWidth() == 32 && "Unknown width");
184 return SectionKind::getMergeable4ByteCString();
185 }
186 }
187 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000188
Chris Lattnerf0144122009-07-28 03:13:23 +0000189 // Otherwise, just drop it into a mergable constant section. If we have
190 // a section for this size, use it, otherwise use the arbitrary sized
191 // mergable section.
192 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000193 case 4: return SectionKind::getMergeableConst4();
194 case 8: return SectionKind::getMergeableConst8();
195 case 16: return SectionKind::getMergeableConst16();
196 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000197 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000198
Chris Lattnerf0144122009-07-28 03:13:23 +0000199 case Constant::LocalRelocation:
200 // In static relocation model, the linker will resolve all addresses, so
201 // the relocation entries will actually be constants by the time the app
202 // starts up. However, we can't put this into a mergable section, because
203 // the linker doesn't take relocations into consideration when it tries to
204 // merge entries in the section.
205 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000206 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000207
Chris Lattnerf0144122009-07-28 03:13:23 +0000208 // Otherwise, the dynamic linker needs to fix it up, put it in the
209 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000210 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000211
Chris Lattnerf0144122009-07-28 03:13:23 +0000212 case Constant::GlobalRelocations:
213 // In static relocation model, the linker will resolve all addresses, so
214 // the relocation entries will actually be constants by the time the app
215 // starts up. However, we can't put this into a mergable section, because
216 // the linker doesn't take relocations into consideration when it tries to
217 // merge entries in the section.
218 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000219 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000220
Chris Lattnerf0144122009-07-28 03:13:23 +0000221 // Otherwise, the dynamic linker needs to fix it up, put it in the
222 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000223 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000224 }
225 }
226
227 // Okay, this isn't a constant. If the initializer for the global is going
228 // to require a runtime relocation by the dynamic linker, put it into a more
229 // specific section to improve startup time of the app. This coalesces these
230 // globals together onto fewer pages, improving the locality of the dynamic
231 // linker.
232 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000233 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000234
235 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000236 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000237 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000238 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000239 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000240 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000241 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000242 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000243 }
244}
245
246/// SectionForGlobal - This method computes the appropriate section to emit
247/// the specified global variable or function definition. This should not
248/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000249const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000250SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000251 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000252 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000253 if (GV->hasSection())
254 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000255
256
Chris Lattnerf0144122009-07-28 03:13:23 +0000257 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000258 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000259}
260
Chris Lattner58bed8f2009-08-05 04:25:40 +0000261
Chris Lattnerf0144122009-07-28 03:13:23 +0000262// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000263const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000264TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000265 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000266 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000267 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000268 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000269
Chris Lattnerf9650c02009-08-01 21:46:23 +0000270 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000271 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000272
Chris Lattner82458382009-08-01 21:56:13 +0000273 if (Kind.isBSS() && BSSSection != 0)
274 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000275
Chris Lattnerf9650c02009-08-01 21:46:23 +0000276 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000277 return ReadOnlySection;
278
279 return getDataSection();
280}
281
Chris Lattner83d77fa2009-08-01 23:46:12 +0000282/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000283/// specified size and relocation information, return a section that it
284/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000285const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000286TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000287 if (Kind.isReadOnly() && ReadOnlySection != 0)
288 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000289
Chris Lattnerf0144122009-07-28 03:13:23 +0000290 return DataSection;
291}
292
Chris Lattner8c6ed052009-09-16 01:46:41 +0000293/// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000294/// reference to the specified global variable from exception
295/// handling information.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000296const MCExpr *TargetLoweringObjectFile::
297getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000298 MachineModuleInfo *MMI, unsigned Encoding) const {
Chris Lattner45111d12010-01-16 21:57:06 +0000299 // FIXME: Use GetGlobalValueSymbol.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000300 SmallString<128> Name;
301 Mang->getNameWithPrefix(Name, GV, false);
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000302 const MCSymbol *Sym = getContext().GetOrCreateSymbol(Name.str());
303
304 return getSymbolForDwarfReference(Sym, MMI, Encoding);
Chris Lattner8c6ed052009-09-16 01:46:41 +0000305}
Chris Lattnerf0144122009-07-28 03:13:23 +0000306
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000307const MCExpr *TargetLoweringObjectFile::
308getSymbolForDwarfReference(const MCSymbol *Sym, MachineModuleInfo *MMI,
309 unsigned Encoding) const {
310 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
311
312 switch (Encoding & 0xF0) {
313 default:
314 llvm_report_error("Do not support this DWARF encoding yet!");
315 break;
316 case dwarf::DW_EH_PE_absptr:
317 // Do nothing special
318 break;
319 case dwarf::DW_EH_PE_pcrel:
320 // FIXME: PCSymbol
321 const MCExpr *PC = MCSymbolRefExpr::Create(".", getContext());
322 Res = MCBinaryExpr::CreateSub(Res, PC, getContext());
323 break;
324 }
325
326 return Res;
327}
328
329unsigned TargetLoweringObjectFile::getPersonalityEncoding() const {
330 return dwarf::DW_EH_PE_absptr;
331}
332
333unsigned TargetLoweringObjectFile::getLSDAEncoding() const {
334 return dwarf::DW_EH_PE_absptr;
335}
336
337unsigned TargetLoweringObjectFile::getFDEEncoding() const {
338 return dwarf::DW_EH_PE_absptr;
339}
340
341unsigned TargetLoweringObjectFile::getTTypeEncoding() const {
342 return dwarf::DW_EH_PE_absptr;
343}
Chris Lattnerf0144122009-07-28 03:13:23 +0000344
345//===----------------------------------------------------------------------===//
346// ELF
347//===----------------------------------------------------------------------===//
Chris Lattner38cff382009-08-13 00:37:15 +0000348typedef StringMap<const MCSectionELF*> ELFUniqueMapTy;
349
350TargetLoweringObjectFileELF::~TargetLoweringObjectFileELF() {
351 // If we have the section uniquing map, free it.
352 delete (ELFUniqueMapTy*)UniquingMap;
353}
Chris Lattnerf0144122009-07-28 03:13:23 +0000354
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000355const MCSection *TargetLoweringObjectFileELF::
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000356getELFSection(StringRef Section, unsigned Type, unsigned Flags,
357 SectionKind Kind, bool IsExplicit) const {
Chris Lattner38cff382009-08-13 00:37:15 +0000358 if (UniquingMap == 0)
359 UniquingMap = new ELFUniqueMapTy();
360 ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000361
Chris Lattner38cff382009-08-13 00:37:15 +0000362 // Do the lookup, if we have a hit, return it.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000363 const MCSectionELF *&Entry = Map[Section];
Chris Lattner38cff382009-08-13 00:37:15 +0000364 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000365
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +0000366 return Entry = MCSectionELF::Create(Section, Type, Flags, Kind, IsExplicit,
367 getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000368}
369
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000370void TargetLoweringObjectFileELF::Initialize(MCContext &Ctx,
371 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000372 if (UniquingMap != 0)
373 ((ELFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000374 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000375
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000376 BSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000377 getELFSection(".bss", MCSectionELF::SHT_NOBITS,
378 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
379 SectionKind::getBSS());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000380
381 TextSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000382 getELFSection(".text", MCSectionELF::SHT_PROGBITS,
383 MCSectionELF::SHF_EXECINSTR | MCSectionELF::SHF_ALLOC,
384 SectionKind::getText());
385
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000386 DataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000387 getELFSection(".data", MCSectionELF::SHT_PROGBITS,
388 MCSectionELF::SHF_WRITE | MCSectionELF::SHF_ALLOC,
389 SectionKind::getDataRel());
390
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000391 ReadOnlySection =
392 getELFSection(".rodata", MCSectionELF::SHT_PROGBITS,
393 MCSectionELF::SHF_ALLOC,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000394 SectionKind::getReadOnly());
395
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000396 TLSDataSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000397 getELFSection(".tdata", MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000398 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000399 MCSectionELF::SHF_WRITE, SectionKind::getThreadData());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000400
401 TLSBSSSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000402 getELFSection(".tbss", MCSectionELF::SHT_NOBITS,
403 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_TLS |
404 MCSectionELF::SHF_WRITE, SectionKind::getThreadBSS());
Chris Lattnerf0144122009-07-28 03:13:23 +0000405
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000406 DataRelSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000407 getELFSection(".data.rel", MCSectionELF::SHT_PROGBITS,
408 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
409 SectionKind::getDataRel());
410
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000411 DataRelLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000412 getELFSection(".data.rel.local", MCSectionELF::SHT_PROGBITS,
413 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
414 SectionKind::getDataRelLocal());
415
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000416 DataRelROSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000417 getELFSection(".data.rel.ro", MCSectionELF::SHT_PROGBITS,
418 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
419 SectionKind::getReadOnlyWithRel());
420
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000421 DataRelROLocalSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000422 getELFSection(".data.rel.ro.local", MCSectionELF::SHT_PROGBITS,
423 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
424 SectionKind::getReadOnlyWithRelLocal());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000425
426 MergeableConst4Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000427 getELFSection(".rodata.cst4", MCSectionELF::SHT_PROGBITS,
428 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
429 SectionKind::getMergeableConst4());
430
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000431 MergeableConst8Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000432 getELFSection(".rodata.cst8", MCSectionELF::SHT_PROGBITS,
433 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
434 SectionKind::getMergeableConst8());
435
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000436 MergeableConst16Section =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000437 getELFSection(".rodata.cst16", MCSectionELF::SHT_PROGBITS,
438 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_MERGE,
439 SectionKind::getMergeableConst16());
Chris Lattner80ec2792009-08-02 00:34:36 +0000440
441 StaticCtorSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000442 getELFSection(".ctors", MCSectionELF::SHT_PROGBITS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000443 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
444 SectionKind::getDataRel());
445
Chris Lattner80ec2792009-08-02 00:34:36 +0000446 StaticDtorSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000447 getELFSection(".dtors", MCSectionELF::SHT_PROGBITS,
448 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
449 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000450
Chris Lattner18a4c162009-08-02 07:24:22 +0000451 // Exception Handling Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000452
Chris Lattnerd5bbb072009-08-02 01:34:32 +0000453 // FIXME: We're emitting LSDA info into a readonly section on ELF, even though
454 // it contains relocatable pointers. In PIC mode, this is probably a big
455 // runtime hit for C++ apps. Either the contents of the LSDA need to be
456 // adjusted or this should be a data section.
457 LSDASection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000458 getELFSection(".gcc_except_table", MCSectionELF::SHT_PROGBITS,
459 MCSectionELF::SHF_ALLOC, SectionKind::getReadOnly());
Chris Lattner35039ac2009-08-02 06:52:36 +0000460 EHFrameSection =
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000461 getELFSection(".eh_frame", MCSectionELF::SHT_PROGBITS,
Chris Lattnerb6ab2992009-08-15 16:54:02 +0000462 MCSectionELF::SHF_ALLOC | MCSectionELF::SHF_WRITE,
463 SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000464
Chris Lattner18a4c162009-08-02 07:24:22 +0000465 // Debug Info Sections.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000466 DwarfAbbrevSection =
467 getELFSection(".debug_abbrev", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000468 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000469 DwarfInfoSection =
470 getELFSection(".debug_info", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000471 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000472 DwarfLineSection =
473 getELFSection(".debug_line", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000474 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000475 DwarfFrameSection =
476 getELFSection(".debug_frame", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000477 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000478 DwarfPubNamesSection =
479 getELFSection(".debug_pubnames", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000480 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000481 DwarfPubTypesSection =
482 getELFSection(".debug_pubtypes", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000483 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000484 DwarfStrSection =
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000485 getELFSection(".debug_str", MCSectionELF::SHT_PROGBITS, 0,
486 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000487 DwarfLocSection =
488 getELFSection(".debug_loc", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000489 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000490 DwarfARangesSection =
491 getELFSection(".debug_aranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000492 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000493 DwarfRangesSection =
494 getELFSection(".debug_ranges", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000495 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000496 DwarfMacroInfoSection =
497 getELFSection(".debug_macinfo", MCSectionELF::SHT_PROGBITS, 0,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000498 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000499}
500
501
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000502static SectionKind
Benjamin Kramera46918d2010-01-22 18:21:23 +0000503getELFKindForNamedSection(StringRef Name, SectionKind K) {
504 if (Name.empty() || Name[0] != '.') return K;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000505
Chris Lattnerf0144122009-07-28 03:13:23 +0000506 // Some lame default implementation based on some magic section names.
Benjamin Kramera46918d2010-01-22 18:21:23 +0000507 if (Name == ".bss" ||
508 Name.startswith(".bss.") ||
509 Name.startswith(".gnu.linkonce.b.") ||
510 Name.startswith(".llvm.linkonce.b.") ||
511 Name == ".sbss" ||
512 Name.startswith(".sbss.") ||
513 Name.startswith(".gnu.linkonce.sb.") ||
514 Name.startswith(".llvm.linkonce.sb."))
Chris Lattner27981192009-08-01 23:57:16 +0000515 return SectionKind::getBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000516
Benjamin Kramera46918d2010-01-22 18:21:23 +0000517 if (Name == ".tdata" ||
518 Name.startswith(".tdata.") ||
519 Name.startswith(".gnu.linkonce.td.") ||
520 Name.startswith(".llvm.linkonce.td."))
Chris Lattner27981192009-08-01 23:57:16 +0000521 return SectionKind::getThreadData();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000522
Benjamin Kramera46918d2010-01-22 18:21:23 +0000523 if (Name == ".tbss" ||
524 Name.startswith(".tbss.") ||
525 Name.startswith(".gnu.linkonce.tb.") ||
526 Name.startswith(".llvm.linkonce.tb."))
Chris Lattner27981192009-08-01 23:57:16 +0000527 return SectionKind::getThreadBSS();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000528
Chris Lattnerf0144122009-07-28 03:13:23 +0000529 return K;
530}
531
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000532
Chris Lattner48130352010-01-13 06:38:18 +0000533static unsigned getELFSectionType(StringRef Name, SectionKind K) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000534
Chris Lattner48130352010-01-13 06:38:18 +0000535 if (Name == ".init_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000536 return MCSectionELF::SHT_INIT_ARRAY;
537
Chris Lattner48130352010-01-13 06:38:18 +0000538 if (Name == ".fini_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000539 return MCSectionELF::SHT_FINI_ARRAY;
540
Chris Lattner48130352010-01-13 06:38:18 +0000541 if (Name == ".preinit_array")
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000542 return MCSectionELF::SHT_PREINIT_ARRAY;
543
544 if (K.isBSS() || K.isThreadBSS())
545 return MCSectionELF::SHT_NOBITS;
546
547 return MCSectionELF::SHT_PROGBITS;
548}
549
550
551static unsigned
552getELFSectionFlags(SectionKind K) {
553 unsigned Flags = 0;
554
555 if (!K.isMetadata())
556 Flags |= MCSectionELF::SHF_ALLOC;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000557
Anton Korobeynikov848c2932009-08-18 14:06:12 +0000558 if (K.isText())
559 Flags |= MCSectionELF::SHF_EXECINSTR;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000560
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000561 if (K.isWriteable())
562 Flags |= MCSectionELF::SHF_WRITE;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000563
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000564 if (K.isThreadLocal())
565 Flags |= MCSectionELF::SHF_TLS;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000566
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000567 // K.isMergeableConst() is left out to honour PR4650
568 if (K.isMergeableCString() || K.isMergeableConst4() ||
569 K.isMergeableConst8() || K.isMergeableConst16())
570 Flags |= MCSectionELF::SHF_MERGE;
571
572 if (K.isMergeableCString())
573 Flags |= MCSectionELF::SHF_STRINGS;
574
575 return Flags;
576}
577
578
Chris Lattner24f654c2009-08-06 16:39:58 +0000579const MCSection *TargetLoweringObjectFileELF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000580getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000581 Mangler *Mang, const TargetMachine &TM) const {
Benjamin Kramera46918d2010-01-22 18:21:23 +0000582 StringRef SectionName = GV->getSection();
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000583
Chris Lattner24f654c2009-08-06 16:39:58 +0000584 // Infer section flags from the section name if we can.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000585 Kind = getELFKindForNamedSection(SectionName, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000586
587 return getELFSection(SectionName,
588 getELFSectionType(SectionName, Kind),
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000589 getELFSectionFlags(Kind), Kind, true);
Chris Lattner24f654c2009-08-06 16:39:58 +0000590}
Chris Lattnerf0144122009-07-28 03:13:23 +0000591
592static const char *getSectionPrefixForUniqueGlobal(SectionKind Kind) {
593 if (Kind.isText()) return ".gnu.linkonce.t.";
594 if (Kind.isReadOnly()) return ".gnu.linkonce.r.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000595
Chris Lattnerf0144122009-07-28 03:13:23 +0000596 if (Kind.isThreadData()) return ".gnu.linkonce.td.";
597 if (Kind.isThreadBSS()) return ".gnu.linkonce.tb.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000598
Chris Lattnerf0144122009-07-28 03:13:23 +0000599 if (Kind.isDataNoRel()) return ".gnu.linkonce.d.";
600 if (Kind.isDataRelLocal()) return ".gnu.linkonce.d.rel.local.";
601 if (Kind.isDataRel()) return ".gnu.linkonce.d.rel.";
602 if (Kind.isReadOnlyWithRelLocal()) return ".gnu.linkonce.d.rel.ro.local.";
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000603
Chris Lattnerf0144122009-07-28 03:13:23 +0000604 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
605 return ".gnu.linkonce.d.rel.ro.";
606}
607
Chris Lattnera87dea42009-07-31 18:48:30 +0000608const MCSection *TargetLoweringObjectFileELF::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000609SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000610 Mangler *Mang, const TargetMachine &TM) const {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000611
Chris Lattnerf0144122009-07-28 03:13:23 +0000612 // If this global is linkonce/weak and the target handles this by emitting it
613 // into a 'uniqued' section name, create and return the section now.
Chris Lattnerc7fbe902010-01-19 03:06:01 +0000614 if (GV->isWeakForLinker() && !Kind.isCommon() && !Kind.isBSS()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000615 const char *Prefix = getSectionPrefixForUniqueGlobal(Kind);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000616 SmallString<128> Name;
Chris Lattner48130352010-01-13 06:38:18 +0000617 Name.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner8da8d4b2010-01-13 21:29:21 +0000618 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattneracd03ae2010-01-17 19:23:46 +0000619 return getELFSection(Name.str(), getELFSectionType(Name.str(), Kind),
620 getELFSectionFlags(Kind), Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000621 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000622
Chris Lattnerf9650c02009-08-01 21:46:23 +0000623 if (Kind.isText()) return TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000624
Chris Lattner3b24c012009-08-04 05:35:56 +0000625 if (Kind.isMergeable1ByteCString() ||
626 Kind.isMergeable2ByteCString() ||
627 Kind.isMergeable4ByteCString()) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000628
Chris Lattner067fe1a2009-07-29 04:54:38 +0000629 // We also need alignment here.
630 // FIXME: this is getting the alignment of the character, not the
631 // alignment of the global!
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000632 unsigned Align =
Chris Lattner067fe1a2009-07-29 04:54:38 +0000633 TM.getTargetData()->getPreferredAlignment(cast<GlobalVariable>(GV));
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000634
Chris Lattner7e88a502009-08-04 16:19:50 +0000635 const char *SizeSpec = ".rodata.str1.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000636 if (Kind.isMergeable2ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000637 SizeSpec = ".rodata.str2.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000638 else if (Kind.isMergeable4ByteCString())
Chris Lattner7e88a502009-08-04 16:19:50 +0000639 SizeSpec = ".rodata.str4.";
Chris Lattner3b24c012009-08-04 05:35:56 +0000640 else
641 assert(Kind.isMergeable1ByteCString() && "unknown string width");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000642
643
Chris Lattner7e88a502009-08-04 16:19:50 +0000644 std::string Name = SizeSpec + utostr(Align);
Benjamin Kramera46918d2010-01-22 18:21:23 +0000645 return getELFSection(Name, MCSectionELF::SHT_PROGBITS,
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000646 MCSectionELF::SHF_ALLOC |
647 MCSectionELF::SHF_MERGE |
648 MCSectionELF::SHF_STRINGS,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000649 Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +0000650 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000651
Chris Lattnerf9650c02009-08-01 21:46:23 +0000652 if (Kind.isMergeableConst()) {
Chris Lattner203b3e92009-08-15 06:08:34 +0000653 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000654 return MergeableConst4Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000655 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000656 return MergeableConst8Section;
Chris Lattner203b3e92009-08-15 06:08:34 +0000657 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000658 return MergeableConst16Section;
659 return ReadOnlySection; // .const
660 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000661
Chris Lattnerf9650c02009-08-01 21:46:23 +0000662 if (Kind.isReadOnly()) return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000663
Chris Lattnerf9650c02009-08-01 21:46:23 +0000664 if (Kind.isThreadData()) return TLSDataSection;
665 if (Kind.isThreadBSS()) return TLSBSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000666
Chris Lattnerc7fbe902010-01-19 03:06:01 +0000667 // Note: we claim that common symbols are put in BSSSection, but they are
668 // really emitted with the magic .comm directive, which creates a symbol table
669 // entry but not a section.
Chris Lattnera3839bc2010-01-19 02:48:26 +0000670 if (Kind.isBSS() || Kind.isCommon()) return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000671
Chris Lattnerf9650c02009-08-01 21:46:23 +0000672 if (Kind.isDataNoRel()) return DataSection;
673 if (Kind.isDataRelLocal()) return DataRelLocalSection;
674 if (Kind.isDataRel()) return DataRelSection;
675 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000676
Chris Lattnerf9650c02009-08-01 21:46:23 +0000677 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000678 return DataRelROSection;
679}
680
Chris Lattner83d77fa2009-08-01 23:46:12 +0000681/// getSectionForConstant - Given a mergeable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000682/// specified size and relocation information, return a section that it
683/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000684const MCSection *TargetLoweringObjectFileELF::
Chris Lattner83d77fa2009-08-01 23:46:12 +0000685getSectionForConstant(SectionKind Kind) const {
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000686 if (Kind.isMergeableConst4() && MergeableConst4Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000687 return MergeableConst4Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000688 if (Kind.isMergeableConst8() && MergeableConst8Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000689 return MergeableConst8Section;
Richard Osborne2a5e23b2009-08-17 16:37:11 +0000690 if (Kind.isMergeableConst16() && MergeableConst16Section)
Chris Lattnerf0144122009-07-28 03:13:23 +0000691 return MergeableConst16Section;
692 if (Kind.isReadOnly())
693 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000694
Chris Lattnerf0144122009-07-28 03:13:23 +0000695 if (Kind.isReadOnlyWithRelLocal()) return DataRelROLocalSection;
696 assert(Kind.isReadOnlyWithRel() && "Unknown section kind");
697 return DataRelROSection;
698}
699
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000700const MCExpr *TargetLoweringObjectFileELF::
701getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
702 MachineModuleInfo *MMI, unsigned Encoding) const {
703
704 if (Encoding & dwarf::DW_EH_PE_indirect) {
705 MachineModuleInfoELF &ELFMMI = MMI->getObjFileInfo<MachineModuleInfoELF>();
706
707 SmallString<128> Name;
708 Mang->getNameWithPrefix(Name, GV, true);
709
710 // Add information about the stub reference to ELFMMI so that the stub
711 // gets emitted by the asmprinter.
712 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name.str());
713 MCSymbol *&StubSym = ELFMMI.getGVStubEntry(Sym);
714 if (StubSym == 0) {
715 Name.clear();
716 Mang->getNameWithPrefix(Name, GV, false);
717 StubSym = getContext().GetOrCreateSymbol(Name.str());
718 }
719
720 return TargetLoweringObjectFile::
721 getSymbolForDwarfReference(Sym, MMI,
722 Encoding & ~dwarf::DW_EH_PE_indirect);
723 }
724
725 return TargetLoweringObjectFile::
726 getSymbolForDwarfGlobalReference(GV, Mang, MMI, Encoding);
727}
728
Chris Lattnerf0144122009-07-28 03:13:23 +0000729//===----------------------------------------------------------------------===//
730// MachO
731//===----------------------------------------------------------------------===//
732
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000733typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
Chris Lattner0c0cb712009-08-08 20:22:20 +0000734
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000735TargetLoweringObjectFileMachO::~TargetLoweringObjectFileMachO() {
736 // If we have the MachO uniquing map, free it.
737 delete (MachOUniqueMapTy*)UniquingMap;
738}
739
740
741const MCSectionMachO *TargetLoweringObjectFileMachO::
Daniel Dunbar2928c832009-11-06 10:58:06 +0000742getMachOSection(StringRef Segment, StringRef Section,
Chris Lattnerff4bc462009-08-10 01:39:42 +0000743 unsigned TypeAndAttributes,
744 unsigned Reserved2, SectionKind Kind) const {
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000745 // We unique sections by their segment/section pair. The returned section
746 // may not have the same flags as the requested section, if so this should be
747 // diagnosed by the client as an error.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000748
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000749 // Create the map if it doesn't already exist.
750 if (UniquingMap == 0)
751 UniquingMap = new MachOUniqueMapTy();
752 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000753
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000754 // Form the name to look up.
755 SmallString<64> Name;
Daniel Dunbar16df2082009-08-26 23:12:33 +0000756 Name += Segment;
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000757 Name.push_back(',');
Daniel Dunbar16df2082009-08-26 23:12:33 +0000758 Name += Section;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000759
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000760 // Do the lookup, if we have a hit, return it.
Daniel Dunbar16df2082009-08-26 23:12:33 +0000761 const MCSectionMachO *&Entry = Map[Name.str()];
Chris Lattner5dc47ff2009-08-12 23:55:02 +0000762 if (Entry) return Entry;
763
764 // Otherwise, return a new section.
765 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
766 Reserved2, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +0000767}
768
Chris Lattner11e96572009-08-03 21:53:27 +0000769
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000770void TargetLoweringObjectFileMachO::Initialize(MCContext &Ctx,
771 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +0000772 if (UniquingMap != 0)
773 ((MachOUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +0000774 TargetLoweringObjectFile::Initialize(Ctx, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000775
Chris Lattnerff4bc462009-08-10 01:39:42 +0000776 TextSection // .text
777 = getMachOSection("__TEXT", "__text",
778 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
779 SectionKind::getText());
780 DataSection // .data
781 = getMachOSection("__DATA", "__data", 0, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000782
Chris Lattnerff4bc462009-08-10 01:39:42 +0000783 CStringSection // .cstring
784 = getMachOSection("__TEXT", "__cstring", MCSectionMachO::S_CSTRING_LITERALS,
785 SectionKind::getMergeable1ByteCString());
786 UStringSection
787 = getMachOSection("__TEXT","__ustring", 0,
788 SectionKind::getMergeable2ByteCString());
789 FourByteConstantSection // .literal4
790 = getMachOSection("__TEXT", "__literal4", MCSectionMachO::S_4BYTE_LITERALS,
791 SectionKind::getMergeableConst4());
792 EightByteConstantSection // .literal8
793 = getMachOSection("__TEXT", "__literal8", MCSectionMachO::S_8BYTE_LITERALS,
794 SectionKind::getMergeableConst8());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000795
Chris Lattner4bb253c2009-07-28 17:50:28 +0000796 // ld_classic doesn't support .literal16 in 32-bit mode, and ld64 falls back
797 // to using it in -static mode.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000798 SixteenByteConstantSection = 0;
Chris Lattner4bb253c2009-07-28 17:50:28 +0000799 if (TM.getRelocationModel() != Reloc::Static &&
800 TM.getTargetData()->getPointerSize() == 32)
Chris Lattnerff4bc462009-08-10 01:39:42 +0000801 SixteenByteConstantSection = // .literal16
802 getMachOSection("__TEXT", "__literal16",MCSectionMachO::S_16BYTE_LITERALS,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000803 SectionKind::getMergeableConst16());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000804
Chris Lattnerff4bc462009-08-10 01:39:42 +0000805 ReadOnlySection // .const
806 = getMachOSection("__TEXT", "__const", 0, SectionKind::getReadOnly());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000807
Chris Lattnerff4bc462009-08-10 01:39:42 +0000808 TextCoalSection
809 = getMachOSection("__TEXT", "__textcoal_nt",
810 MCSectionMachO::S_COALESCED |
811 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
812 SectionKind::getText());
813 ConstTextCoalSection
814 = getMachOSection("__TEXT", "__const_coal", MCSectionMachO::S_COALESCED,
815 SectionKind::getText());
816 ConstDataCoalSection
817 = getMachOSection("__DATA","__const_coal", MCSectionMachO::S_COALESCED,
818 SectionKind::getText());
819 ConstDataSection // .const_data
820 = getMachOSection("__DATA", "__const", 0,
821 SectionKind::getReadOnlyWithRel());
822 DataCoalSection
823 = getMachOSection("__DATA","__datacoal_nt", MCSectionMachO::S_COALESCED,
824 SectionKind::getDataRel());
Chris Lattner814819f2010-01-19 06:25:51 +0000825 DataCommonSection
826 = getMachOSection("__DATA","__common", MCSectionMachO::S_ZEROFILL,
827 SectionKind::getBSS());
828 DataBSSSection
829 = getMachOSection("__DATA","__bss", MCSectionMachO::S_ZEROFILL,
830 SectionKind::getBSS());
831
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000832
Chris Lattnere309cfa2009-08-13 00:05:07 +0000833 LazySymbolPointerSection
834 = getMachOSection("__DATA", "__la_symbol_ptr",
835 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
836 SectionKind::getMetadata());
837 NonLazySymbolPointerSection
838 = getMachOSection("__DATA", "__nl_symbol_ptr",
839 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
840 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000841
Chris Lattner80ec2792009-08-02 00:34:36 +0000842 if (TM.getRelocationModel() == Reloc::Static) {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000843 StaticCtorSection
844 = getMachOSection("__TEXT", "__constructor", 0,SectionKind::getDataRel());
845 StaticDtorSection
846 = getMachOSection("__TEXT", "__destructor", 0, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000847 } else {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000848 StaticCtorSection
849 = getMachOSection("__DATA", "__mod_init_func",
850 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
851 SectionKind::getDataRel());
852 StaticDtorSection
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000853 = getMachOSection("__DATA", "__mod_term_func",
Chris Lattnerff4bc462009-08-10 01:39:42 +0000854 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
855 SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +0000856 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000857
Chris Lattner18a4c162009-08-02 07:24:22 +0000858 // Exception Handling.
Bill Wendlingfb7634f2009-11-19 19:21:09 +0000859 LSDASection = getMachOSection("__DATA", "__gcc_except_tab", 0,
860 SectionKind::getDataRel());
Chris Lattner35039ac2009-08-02 06:52:36 +0000861 EHFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000862 getMachOSection("__TEXT", "__eh_frame",
863 MCSectionMachO::S_COALESCED |
864 MCSectionMachO::S_ATTR_NO_TOC |
865 MCSectionMachO::S_ATTR_STRIP_STATIC_SYMS |
866 MCSectionMachO::S_ATTR_LIVE_SUPPORT,
867 SectionKind::getReadOnly());
Chris Lattner18a4c162009-08-02 07:24:22 +0000868
869 // Debug Information.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000870 DwarfAbbrevSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000871 getMachOSection("__DWARF", "__debug_abbrev", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000872 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000873 DwarfInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000874 getMachOSection("__DWARF", "__debug_info", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000875 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000876 DwarfLineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000877 getMachOSection("__DWARF", "__debug_line", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000878 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000879 DwarfFrameSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000880 getMachOSection("__DWARF", "__debug_frame", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000881 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000882 DwarfPubNamesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000883 getMachOSection("__DWARF", "__debug_pubnames", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000884 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000885 DwarfPubTypesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000886 getMachOSection("__DWARF", "__debug_pubtypes", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000887 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000888 DwarfStrSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000889 getMachOSection("__DWARF", "__debug_str", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000890 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000891 DwarfLocSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000892 getMachOSection("__DWARF", "__debug_loc", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000893 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000894 DwarfARangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000895 getMachOSection("__DWARF", "__debug_aranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000896 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000897 DwarfRangesSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000898 getMachOSection("__DWARF", "__debug_ranges", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000899 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000900 DwarfMacroInfoSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000901 getMachOSection("__DWARF", "__debug_macinfo", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000902 SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000903 DwarfDebugInlineSection =
Chris Lattnerff4bc462009-08-10 01:39:42 +0000904 getMachOSection("__DWARF", "__debug_inlined", MCSectionMachO::S_ATTR_DEBUG,
Chris Lattner0c0cb712009-08-08 20:22:20 +0000905 SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +0000906}
907
Chris Lattnera87dea42009-07-31 18:48:30 +0000908const MCSection *TargetLoweringObjectFileMachO::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000909getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +0000910 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerff4bc462009-08-10 01:39:42 +0000911 // Parse the section specifier and create it if valid.
912 StringRef Segment, Section;
913 unsigned TAA, StubSize;
914 std::string ErrorCode =
915 MCSectionMachO::ParseSectionSpecifier(GV->getSection(), Segment, Section,
916 TAA, StubSize);
Chris Lattnere309cfa2009-08-13 00:05:07 +0000917 if (!ErrorCode.empty()) {
918 // If invalid, report the error with llvm_report_error.
919 llvm_report_error("Global variable '" + GV->getNameStr() +
920 "' has an invalid section specifier '" + GV->getSection()+
921 "': " + ErrorCode + ".");
922 // Fall back to dropping it into the data section.
923 return DataSection;
924 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000925
Chris Lattnere309cfa2009-08-13 00:05:07 +0000926 // Get the section.
927 const MCSectionMachO *S =
928 getMachOSection(Segment, Section, TAA, StubSize, Kind);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000929
Chris Lattnere309cfa2009-08-13 00:05:07 +0000930 // Okay, now that we got the section, verify that the TAA & StubSize agree.
931 // If the user declared multiple globals with different section flags, we need
932 // to reject it here.
933 if (S->getTypeAndAttributes() != TAA || S->getStubSize() != StubSize) {
934 // If invalid, report the error with llvm_report_error.
935 llvm_report_error("Global variable '" + GV->getNameStr() +
936 "' section type or attributes does not match previous"
937 " section specifier");
938 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000939
Chris Lattnere309cfa2009-08-13 00:05:07 +0000940 return S;
Chris Lattner24f654c2009-08-06 16:39:58 +0000941}
942
943const MCSection *TargetLoweringObjectFileMachO::
Chris Lattnerf9650c02009-08-01 21:46:23 +0000944SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000945 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000946 assert(!Kind.isThreadLocal() && "Darwin doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000947
Chris Lattnerf9650c02009-08-01 21:46:23 +0000948 if (Kind.isText())
Chris Lattner27602b82009-08-01 21:42:58 +0000949 return GV->isWeakForLinker() ? TextCoalSection : TextSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000950
Chris Lattnerf0144122009-07-28 03:13:23 +0000951 // If this is weak/linkonce, put this in a coalescable section, either in text
952 // or data depending on if it is writable.
Chris Lattner27602b82009-08-01 21:42:58 +0000953 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +0000954 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000955 return ConstTextCoalSection;
956 return DataCoalSection;
957 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000958
Chris Lattnerf0144122009-07-28 03:13:23 +0000959 // FIXME: Alignment check should be handled by section classifier.
Chris Lattnerec409752009-08-04 16:27:13 +0000960 if (Kind.isMergeable1ByteCString() ||
961 Kind.isMergeable2ByteCString()) {
962 if (TM.getTargetData()->getPreferredAlignment(
963 cast<GlobalVariable>(GV)) < 32) {
964 if (Kind.isMergeable1ByteCString())
Chris Lattner82458382009-08-01 21:56:13 +0000965 return CStringSection;
Chris Lattnerec409752009-08-04 16:27:13 +0000966 assert(Kind.isMergeable2ByteCString());
967 return UStringSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000968 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000969 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000970
Chris Lattnerf9650c02009-08-01 21:46:23 +0000971 if (Kind.isMergeableConst()) {
972 if (Kind.isMergeableConst4())
Chris Lattnerf0144122009-07-28 03:13:23 +0000973 return FourByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000974 if (Kind.isMergeableConst8())
Chris Lattnerf0144122009-07-28 03:13:23 +0000975 return EightByteConstantSection;
Chris Lattnerf9650c02009-08-01 21:46:23 +0000976 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +0000977 return SixteenByteConstantSection;
Chris Lattnerf0144122009-07-28 03:13:23 +0000978 }
Chris Lattnerec409752009-08-04 16:27:13 +0000979
980 // Otherwise, if it is readonly, but not something we can specially optimize,
981 // just drop it in .const.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000982 if (Kind.isReadOnly())
Chris Lattnerf0144122009-07-28 03:13:23 +0000983 return ReadOnlySection;
984
985 // If this is marked const, put it into a const section. But if the dynamic
986 // linker needs to write to it, put it in the data segment.
Chris Lattnerf9650c02009-08-01 21:46:23 +0000987 if (Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +0000988 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000989
Chris Lattneraac138e2010-01-19 02:09:44 +0000990 // Put zero initialized globals with strong external linkage in the
991 // DATA, __common section with the .zerofill directive.
Chris Lattnerce8749e2010-01-19 04:15:51 +0000992 if (Kind.isBSSExtern())
Chris Lattneraac138e2010-01-19 02:09:44 +0000993 return DataCommonSection;
Chris Lattner814819f2010-01-19 06:25:51 +0000994
995 // Put zero initialized globals with local linkage in __DATA,__bss directive
996 // with the .zerofill directive (aka .lcomm).
997 if (Kind.isBSSLocal())
998 return DataBSSSection;
Chris Lattneraac138e2010-01-19 02:09:44 +0000999
Chris Lattnerf0144122009-07-28 03:13:23 +00001000 // Otherwise, just drop the variable in the normal data section.
1001 return DataSection;
1002}
1003
Chris Lattnera87dea42009-07-31 18:48:30 +00001004const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +00001005TargetLoweringObjectFileMachO::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +00001006 // If this constant requires a relocation, we have to put it in the data
1007 // segment, not in the text segment.
Eric Christophera07b7502010-01-07 19:44:05 +00001008 if (Kind.isDataRel() || Kind.isReadOnlyWithRel())
Chris Lattnerf0144122009-07-28 03:13:23 +00001009 return ConstDataSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001010
Chris Lattnerf0144122009-07-28 03:13:23 +00001011 if (Kind.isMergeableConst4())
1012 return FourByteConstantSection;
1013 if (Kind.isMergeableConst8())
1014 return EightByteConstantSection;
Chris Lattner4bb253c2009-07-28 17:50:28 +00001015 if (Kind.isMergeableConst16() && SixteenByteConstantSection)
Chris Lattnerf0144122009-07-28 03:13:23 +00001016 return SixteenByteConstantSection;
1017 return ReadOnlySection; // .const
1018}
1019
Chris Lattner26630c12009-07-31 20:52:39 +00001020/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively decide
1021/// not to emit the UsedDirective for some symbols in llvm.used.
1022// FIXME: REMOVE this (rdar://7071300)
1023bool TargetLoweringObjectFileMachO::
1024shouldEmitUsedDirectiveFor(const GlobalValue *GV, Mangler *Mang) const {
1025 /// On Darwin, internally linked data beginning with "L" or "l" does not have
1026 /// the directive emitted (this occurs in ObjC metadata).
1027 if (!GV) return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001028
Chris Lattner26630c12009-07-31 20:52:39 +00001029 // Check whether the mangled name has the "Private" or "LinkerPrivate" prefix.
1030 if (GV->hasLocalLinkage() && !isa<Function>(GV)) {
1031 // FIXME: ObjC metadata is currently emitted as internal symbols that have
1032 // \1L and \0l prefixes on them. Fix them to be Private/LinkerPrivate and
1033 // this horrible hack can go away.
Chris Lattner0b3735e2010-01-16 02:16:09 +00001034 SmallString<64> Name;
Chris Lattner036d8f92010-01-16 03:38:27 +00001035 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner26630c12009-07-31 20:52:39 +00001036 if (Name[0] == 'L' || Name[0] == 'l')
1037 return false;
1038 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001039
Chris Lattner26630c12009-07-31 20:52:39 +00001040 return true;
1041}
1042
Chris Lattner8c6ed052009-09-16 01:46:41 +00001043const MCExpr *TargetLoweringObjectFileMachO::
1044getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Anton Korobeynikov9184b252010-02-15 22:35:59 +00001045 MachineModuleInfo *MMI, unsigned Encoding) const {
Chris Lattner8c6ed052009-09-16 01:46:41 +00001046 // The mach-o version of this method defaults to returning a stub reference.
Anton Korobeynikov9184b252010-02-15 22:35:59 +00001047
1048 if (Encoding & dwarf::DW_EH_PE_indirect) {
1049 SmallString<128> Name;
1050 Mang->getNameWithPrefix(Name, GV, true);
1051 Name += "$non_lazy_ptr";
1052 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name.str());
1053
1054 return TargetLoweringObjectFile::
1055 getSymbolForDwarfReference(Sym, MMI,
1056 Encoding & ~dwarf::DW_EH_PE_indirect);
1057 }
1058
1059 return TargetLoweringObjectFile::
1060 getSymbolForDwarfGlobalReference(GV, Mang, MMI, Encoding);
Chris Lattner8c6ed052009-09-16 01:46:41 +00001061}
1062
Chris Lattner26630c12009-07-31 20:52:39 +00001063
Chris Lattnerf0144122009-07-28 03:13:23 +00001064//===----------------------------------------------------------------------===//
1065// COFF
1066//===----------------------------------------------------------------------===//
1067
Chris Lattner38cff382009-08-13 00:37:15 +00001068typedef StringMap<const MCSectionCOFF*> COFFUniqueMapTy;
1069
1070TargetLoweringObjectFileCOFF::~TargetLoweringObjectFileCOFF() {
1071 delete (COFFUniqueMapTy*)UniquingMap;
1072}
1073
Chris Lattner0c0cb712009-08-08 20:22:20 +00001074
Chris Lattner11e96572009-08-03 21:53:27 +00001075const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattner48130352010-01-13 06:38:18 +00001076getCOFFSection(StringRef Name, bool isDirective, SectionKind Kind) const {
Chris Lattner38cff382009-08-13 00:37:15 +00001077 // Create the map if it doesn't already exist.
1078 if (UniquingMap == 0)
1079 UniquingMap = new MachOUniqueMapTy();
1080 COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)UniquingMap;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001081
Chris Lattner38cff382009-08-13 00:37:15 +00001082 // Do the lookup, if we have a hit, return it.
1083 const MCSectionCOFF *&Entry = Map[Name];
1084 if (Entry) return Entry;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001085
Chris Lattner38cff382009-08-13 00:37:15 +00001086 return Entry = MCSectionCOFF::Create(Name, isDirective, Kind, getContext());
Chris Lattnerfbf1d272009-08-08 20:14:13 +00001087}
1088
Chris Lattnerf26e03b2009-07-31 17:42:42 +00001089void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
1090 const TargetMachine &TM) {
Benjamin Kramer76d5ccf2009-08-17 17:05:44 +00001091 if (UniquingMap != 0)
1092 ((COFFUniqueMapTy*)UniquingMap)->clear();
Chris Lattnera87dea42009-07-31 18:48:30 +00001093 TargetLoweringObjectFile::Initialize(Ctx, TM);
Chris Lattner0c0cb712009-08-08 20:22:20 +00001094 TextSection = getCOFFSection("\t.text", true, SectionKind::getText());
1095 DataSection = getCOFFSection("\t.data", true, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001096 StaticCtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001097 getCOFFSection(".ctors", false, SectionKind::getDataRel());
Chris Lattner80ec2792009-08-02 00:34:36 +00001098 StaticDtorSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001099 getCOFFSection(".dtors", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001100
Chris Lattner35c35312009-08-18 16:56:17 +00001101 // FIXME: We're emitting LSDA info into a readonly section on COFF, even
1102 // though it contains relocatable pointers. In PIC mode, this is probably a
1103 // big runtime hit for C++ apps. Either the contents of the LSDA need to be
1104 // adjusted or this should be a data section.
1105 LSDASection =
1106 getCOFFSection(".gcc_except_table", false, SectionKind::getReadOnly());
1107 EHFrameSection =
1108 getCOFFSection(".eh_frame", false, SectionKind::getDataRel());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001109
Chris Lattner18a4c162009-08-02 07:24:22 +00001110 // Debug info.
1111 // FIXME: Don't use 'directive' mode here.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001112 DwarfAbbrevSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001113 getCOFFSection("\t.section\t.debug_abbrev,\"dr\"",
1114 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001115 DwarfInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001116 getCOFFSection("\t.section\t.debug_info,\"dr\"",
1117 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001118 DwarfLineSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001119 getCOFFSection("\t.section\t.debug_line,\"dr\"",
1120 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001121 DwarfFrameSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001122 getCOFFSection("\t.section\t.debug_frame,\"dr\"",
1123 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001124 DwarfPubNamesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001125 getCOFFSection("\t.section\t.debug_pubnames,\"dr\"",
1126 true, SectionKind::getMetadata());
Chris Lattner18a4c162009-08-02 07:24:22 +00001127 DwarfPubTypesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001128 getCOFFSection("\t.section\t.debug_pubtypes,\"dr\"",
1129 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001130 DwarfStrSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001131 getCOFFSection("\t.section\t.debug_str,\"dr\"",
1132 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001133 DwarfLocSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001134 getCOFFSection("\t.section\t.debug_loc,\"dr\"",
1135 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001136 DwarfARangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001137 getCOFFSection("\t.section\t.debug_aranges,\"dr\"",
1138 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001139 DwarfRangesSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001140 getCOFFSection("\t.section\t.debug_ranges,\"dr\"",
1141 true, SectionKind::getMetadata());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001142 DwarfMacroInfoSection =
Chris Lattner0c0cb712009-08-08 20:22:20 +00001143 getCOFFSection("\t.section\t.debug_macinfo,\"dr\"",
1144 true, SectionKind::getMetadata());
Chris Lattnerf0144122009-07-28 03:13:23 +00001145}
1146
Chris Lattner24f654c2009-08-06 16:39:58 +00001147const MCSection *TargetLoweringObjectFileCOFF::
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001148getExplicitSectionGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattner24f654c2009-08-06 16:39:58 +00001149 Mangler *Mang, const TargetMachine &TM) const {
Benjamin Kramera46918d2010-01-22 18:21:23 +00001150 return getCOFFSection(GV->getSection(), false, Kind);
Chris Lattner24f654c2009-08-06 16:39:58 +00001151}
1152
Chris Lattnerf0144122009-07-28 03:13:23 +00001153static const char *getCOFFSectionPrefixForUniqueGlobal(SectionKind Kind) {
1154 if (Kind.isText())
1155 return ".text$linkonce";
1156 if (Kind.isWriteable())
1157 return ".data$linkonce";
1158 return ".rdata$linkonce";
1159}
1160
1161
Chris Lattnera87dea42009-07-31 18:48:30 +00001162const MCSection *TargetLoweringObjectFileCOFF::
Chris Lattnerf9650c02009-08-01 21:46:23 +00001163SelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +00001164 Mangler *Mang, const TargetMachine &TM) const {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001165 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001166
Chris Lattnerf0144122009-07-28 03:13:23 +00001167 // If this global is linkonce/weak and the target handles this by emitting it
1168 // into a 'uniqued' section name, create and return the section now.
Chris Lattner27602b82009-08-01 21:42:58 +00001169 if (GV->isWeakForLinker()) {
Chris Lattnerf9650c02009-08-01 21:46:23 +00001170 const char *Prefix = getCOFFSectionPrefixForUniqueGlobal(Kind);
Chris Lattner48130352010-01-13 06:38:18 +00001171 SmallString<128> Name(Prefix, Prefix+strlen(Prefix));
Chris Lattnercab16cc2010-01-13 19:19:17 +00001172 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner48130352010-01-13 06:38:18 +00001173 return getCOFFSection(Name.str(), false, Kind);
Chris Lattnerf0144122009-07-28 03:13:23 +00001174 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001175
Chris Lattnerf9650c02009-08-01 21:46:23 +00001176 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +00001177 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +00001178
Chris Lattnerf0144122009-07-28 03:13:23 +00001179 return getDataSection();
1180}
Chris Lattner8c6ed052009-09-16 01:46:41 +00001181