blob: eab43fb4276498e1ad269e3d755df6621e682da1 [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"
Chris Lattnera87dea42009-07-31 18:48:30 +000020#include "llvm/MC/MCContext.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "llvm/MC/MCExpr.h"
Chris Lattner42263e22010-03-11 21:55:20 +000022#include "llvm/MC/MCStreamer.h"
Chris Lattner8da8d4b2010-01-13 21:29:21 +000023#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000024#include "llvm/Target/Mangler.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000025#include "llvm/Target/TargetData.h"
Chris Lattner5277b222009-08-08 20:43:12 +000026#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000027#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000028#include "llvm/Support/Dwarf.h"
Chris Lattner8f9b0f62009-11-07 09:20:54 +000029#include "llvm/Support/ErrorHandling.h"
Chris Lattner8da8d4b2010-01-13 21:29:21 +000030#include "llvm/Support/raw_ostream.h"
Chris Lattner5dc47ff2009-08-12 23:55:02 +000031#include "llvm/ADT/SmallString.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// Generic Code
36//===----------------------------------------------------------------------===//
37
Chris Lattnera87dea42009-07-31 18:48:30 +000038TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000039 TextSection = 0;
40 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000041 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000042 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000043 StaticCtorSection = 0;
44 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000045 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000046 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000047
Chris Lattner8048ebe2010-09-27 06:44:54 +000048 CommDirectiveSupportsAlignment = true;
Chris Lattner18a4c162009-08-02 07:24:22 +000049 DwarfAbbrevSection = 0;
50 DwarfInfoSection = 0;
51 DwarfLineSection = 0;
52 DwarfFrameSection = 0;
53 DwarfPubNamesSection = 0;
54 DwarfPubTypesSection = 0;
55 DwarfDebugInlineSection = 0;
56 DwarfStrSection = 0;
57 DwarfLocSection = 0;
58 DwarfARangesSection = 0;
59 DwarfRangesSection = 0;
60 DwarfMacroInfoSection = 0;
Chris Lattner09d53fe2010-03-10 07:20:42 +000061
62 IsFunctionEHSymbolGlobal = false;
63 IsFunctionEHFrameSymbolPrivate = true;
64 SupportsWeakOmittedEHFrame = true;
Chris Lattnerf0144122009-07-28 03:13:23 +000065}
66
67TargetLoweringObjectFile::~TargetLoweringObjectFile() {
68}
69
70static bool isSuitableForBSS(const GlobalVariable *GV) {
71 Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000072
Chris Lattnerf0144122009-07-28 03:13:23 +000073 // Must have zero initializer.
74 if (!C->isNullValue())
75 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000076
Chris Lattnerf0144122009-07-28 03:13:23 +000077 // Leave constant zeros in readonly constant sections, so they can be shared.
78 if (GV->isConstant())
79 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000080
Chris Lattnerf0144122009-07-28 03:13:23 +000081 // If the global has an explicit section specified, don't put it in BSS.
82 if (!GV->getSection().empty())
83 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000084
Chris Lattnerf0144122009-07-28 03:13:23 +000085 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
86 if (NoZerosInBSS)
87 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000088
Chris Lattnerf0144122009-07-28 03:13:23 +000089 // Otherwise, put it in BSS!
90 return true;
91}
92
Chris Lattner1850e5a2009-08-04 16:13:09 +000093/// IsNullTerminatedString - Return true if the specified constant (which is
94/// known to have a type that is an array of 1/2/4 byte elements) ends with a
95/// nul value and contains no other nuls in it.
96static bool IsNullTerminatedString(const Constant *C) {
97 const ArrayType *ATy = cast<ArrayType>(C->getType());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000098
Chris Lattnerf0144122009-07-28 03:13:23 +000099 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +0000100 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
101 if (ATy->getNumElements() == 0) return false;
102
103 ConstantInt *Null =
104 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
Dan Gohmane368b462010-06-18 14:22:04 +0000105 if (Null == 0 || !Null->isZero())
Chris Lattner1850e5a2009-08-04 16:13:09 +0000106 return false; // Not null terminated.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000107
Chris Lattner1850e5a2009-08-04 16:13:09 +0000108 // Verify that the null doesn't occur anywhere else in the string.
109 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
110 // Reject constantexpr elements etc.
111 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
112 CVA->getOperand(i) == Null)
113 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000114 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000115 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000116
117 // Another possibility: [1 x i8] zeroinitializer
118 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000119 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000120
121 return false;
122}
123
Chris Lattner58bed8f2009-08-05 04:25:40 +0000124/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000125/// a global variable. Given an global variable and information from TM, it
126/// classifies the global in a variety of ways that make various target
127/// implementations simpler. The target implementation is free to ignore this
128/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000129SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
130 const TargetMachine &TM){
131 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
132 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000133
Chris Lattnerf0144122009-07-28 03:13:23 +0000134 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000135
Chris Lattnerf0144122009-07-28 03:13:23 +0000136 // Early exit - functions should be always in text sections.
137 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
138 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000139 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000140
Chris Lattnerf0144122009-07-28 03:13:23 +0000141 // Handle thread-local data first.
142 if (GVar->isThreadLocal()) {
143 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000144 return SectionKind::getThreadBSS();
145 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000146 }
147
Chris Lattnera3839bc2010-01-19 02:48:26 +0000148 // Variables with common linkage always get classified as common.
149 if (GVar->hasCommonLinkage())
150 return SectionKind::getCommon();
151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152 // Variable can be easily put to BSS section.
Chris Lattnerce8749e2010-01-19 04:15:51 +0000153 if (isSuitableForBSS(GVar)) {
154 if (GVar->hasLocalLinkage())
155 return SectionKind::getBSSLocal();
156 else if (GVar->hasExternalLinkage())
157 return SectionKind::getBSSExtern();
Chris Lattner27981192009-08-01 23:57:16 +0000158 return SectionKind::getBSS();
Chris Lattnerce8749e2010-01-19 04:15:51 +0000159 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000160
161 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000162
Chris Lattnerf0144122009-07-28 03:13:23 +0000163 // If the global is marked constant, we can put it into a mergable section,
164 // a mergable string section, or general .data if it contains relocations.
Rafael Espindola70104012011-01-16 17:19:34 +0000165 if (GVar->isConstant() && GVar->hasUnnamedAddr()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000166 // If the initializer for the global contains something that requires a
167 // relocation, then we may have to drop this into a wriable data section
168 // even though it is marked const.
169 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000170 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000171 case Constant::NoRelocation:
172 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000173 // section of the right width.
174 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000175 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000176 dyn_cast<IntegerType>(ATy->getElementType())) {
177 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
178 ITy->getBitWidth() == 32) &&
179 IsNullTerminatedString(C)) {
180 if (ITy->getBitWidth() == 8)
181 return SectionKind::getMergeable1ByteCString();
182 if (ITy->getBitWidth() == 16)
183 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000184
Chris Lattner1850e5a2009-08-04 16:13:09 +0000185 assert(ITy->getBitWidth() == 32 && "Unknown width");
186 return SectionKind::getMergeable4ByteCString();
187 }
188 }
189 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000190
Chris Lattnerf0144122009-07-28 03:13:23 +0000191 // Otherwise, just drop it into a mergable constant section. If we have
192 // a section for this size, use it, otherwise use the arbitrary sized
193 // mergable section.
194 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000195 case 4: return SectionKind::getMergeableConst4();
196 case 8: return SectionKind::getMergeableConst8();
197 case 16: return SectionKind::getMergeableConst16();
198 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000199 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000200
Chris Lattnerf0144122009-07-28 03:13:23 +0000201 case Constant::LocalRelocation:
202 // In static relocation model, the linker will resolve all addresses, so
203 // the relocation entries will actually be constants by the time the app
204 // starts up. However, we can't put this into a mergable section, because
205 // the linker doesn't take relocations into consideration when it tries to
206 // merge entries in the section.
207 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000208 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000209
Chris Lattnerf0144122009-07-28 03:13:23 +0000210 // Otherwise, the dynamic linker needs to fix it up, put it in the
211 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000212 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000213
Chris Lattnerf0144122009-07-28 03:13:23 +0000214 case Constant::GlobalRelocations:
215 // In static relocation model, the linker will resolve all addresses, so
216 // the relocation entries will actually be constants by the time the app
217 // starts up. However, we can't put this into a mergable section, because
218 // the linker doesn't take relocations into consideration when it tries to
219 // merge entries in the section.
220 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000221 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000222
Chris Lattnerf0144122009-07-28 03:13:23 +0000223 // Otherwise, the dynamic linker needs to fix it up, put it in the
224 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000225 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000226 }
227 }
228
229 // Okay, this isn't a constant. If the initializer for the global is going
230 // to require a runtime relocation by the dynamic linker, put it into a more
231 // specific section to improve startup time of the app. This coalesces these
232 // globals together onto fewer pages, improving the locality of the dynamic
233 // linker.
234 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000235 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000236
237 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000238 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000239 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000240 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000241 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000242 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000243 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000244 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000245 }
246}
247
248/// SectionForGlobal - This method computes the appropriate section to emit
249/// the specified global variable or function definition. This should not
250/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000251const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000252SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000253 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000254 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000255 if (GV->hasSection())
256 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000257
258
Chris Lattnerf0144122009-07-28 03:13:23 +0000259 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000260 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000261}
262
Chris Lattner58bed8f2009-08-05 04:25:40 +0000263
Chris Lattnerf0144122009-07-28 03:13:23 +0000264// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000265const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000266TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000267 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000268 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000269 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000270 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000271
Chris Lattnerf9650c02009-08-01 21:46:23 +0000272 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000273 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000274
Chris Lattner82458382009-08-01 21:56:13 +0000275 if (Kind.isBSS() && BSSSection != 0)
276 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000277
Chris Lattnerf9650c02009-08-01 21:46:23 +0000278 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000279 return ReadOnlySection;
280
281 return getDataSection();
282}
283
Chris Lattner83d77fa2009-08-01 23:46:12 +0000284/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000285/// specified size and relocation information, return a section that it
286/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000287const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000288TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000289 if (Kind.isReadOnly() && ReadOnlySection != 0)
290 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000291
Chris Lattnerf0144122009-07-28 03:13:23 +0000292 return DataSection;
293}
294
Chris Lattner3192d142010-03-11 19:41:58 +0000295/// getExprForDwarfGlobalReference - Return an MCExpr to use for a
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000296/// reference to the specified global variable from exception
297/// handling information.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000298const MCExpr *TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000299getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
300 MachineModuleInfo *MMI, unsigned Encoding,
301 MCStreamer &Streamer) const {
Chris Lattner73ff5642010-03-12 18:55:20 +0000302 const MCSymbol *Sym = Mang->getSymbol(GV);
Chris Lattner42263e22010-03-11 21:55:20 +0000303 return getExprForDwarfReference(Sym, Mang, MMI, Encoding, Streamer);
Chris Lattner8c6ed052009-09-16 01:46:41 +0000304}
Chris Lattnerf0144122009-07-28 03:13:23 +0000305
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000306const MCExpr *TargetLoweringObjectFile::
Chris Lattner42263e22010-03-11 21:55:20 +0000307getExprForDwarfReference(const MCSymbol *Sym, Mangler *Mang,
308 MachineModuleInfo *MMI, unsigned Encoding,
309 MCStreamer &Streamer) const {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000310 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
311
312 switch (Encoding & 0xF0) {
313 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000314 report_fatal_error("We do not support this DWARF encoding yet!");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000315 case dwarf::DW_EH_PE_absptr:
316 // Do nothing special
Chris Lattner42263e22010-03-11 21:55:20 +0000317 return Res;
318 case dwarf::DW_EH_PE_pcrel: {
319 // Emit a label to the streamer for the current position. This gives us
320 // .-foo addressing.
Chris Lattner77e76942010-03-17 05:41:18 +0000321 MCSymbol *PCSym = getContext().CreateTempSymbol();
Chris Lattner42263e22010-03-11 21:55:20 +0000322 Streamer.EmitLabel(PCSym);
323 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
324 return MCBinaryExpr::CreateSub(Res, PC, getContext());
325 }
326 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000327}
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