blob: 421f1e8fa838dbe13695e57233858752a2e704ab [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 Lattner8da8d4b2010-01-13 21:29:21 +000022#include "llvm/MC/MCSymbol.h"
Chris Lattner45111d12010-01-16 21:57:06 +000023#include "llvm/Target/Mangler.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000024#include "llvm/Target/TargetData.h"
Chris Lattner5277b222009-08-08 20:43:12 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000026#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov9184b252010-02-15 22:35:59 +000027#include "llvm/Support/Dwarf.h"
Chris Lattner8f9b0f62009-11-07 09:20:54 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattner8da8d4b2010-01-13 21:29:21 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattner5dc47ff2009-08-12 23:55:02 +000030#include "llvm/ADT/SmallString.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// Generic Code
35//===----------------------------------------------------------------------===//
36
Chris Lattnera87dea42009-07-31 18:48:30 +000037TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000038 TextSection = 0;
39 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000040 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000041 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000042 StaticCtorSection = 0;
43 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000044 LSDASection = 0;
Chris Lattner35039ac2009-08-02 06:52:36 +000045 EHFrameSection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000046
47 DwarfAbbrevSection = 0;
48 DwarfInfoSection = 0;
49 DwarfLineSection = 0;
50 DwarfFrameSection = 0;
51 DwarfPubNamesSection = 0;
52 DwarfPubTypesSection = 0;
53 DwarfDebugInlineSection = 0;
54 DwarfStrSection = 0;
55 DwarfLocSection = 0;
56 DwarfARangesSection = 0;
57 DwarfRangesSection = 0;
58 DwarfMacroInfoSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000059}
60
61TargetLoweringObjectFile::~TargetLoweringObjectFile() {
62}
63
64static bool isSuitableForBSS(const GlobalVariable *GV) {
65 Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000066
Chris Lattnerf0144122009-07-28 03:13:23 +000067 // Must have zero initializer.
68 if (!C->isNullValue())
69 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000070
Chris Lattnerf0144122009-07-28 03:13:23 +000071 // Leave constant zeros in readonly constant sections, so they can be shared.
72 if (GV->isConstant())
73 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000074
Chris Lattnerf0144122009-07-28 03:13:23 +000075 // If the global has an explicit section specified, don't put it in BSS.
76 if (!GV->getSection().empty())
77 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000078
Chris Lattnerf0144122009-07-28 03:13:23 +000079 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
80 if (NoZerosInBSS)
81 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000082
Chris Lattnerf0144122009-07-28 03:13:23 +000083 // Otherwise, put it in BSS!
84 return true;
85}
86
Chris Lattner1850e5a2009-08-04 16:13:09 +000087/// IsNullTerminatedString - Return true if the specified constant (which is
88/// known to have a type that is an array of 1/2/4 byte elements) ends with a
89/// nul value and contains no other nuls in it.
90static bool IsNullTerminatedString(const Constant *C) {
91 const ArrayType *ATy = cast<ArrayType>(C->getType());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000092
Chris Lattnerf0144122009-07-28 03:13:23 +000093 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000094 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
95 if (ATy->getNumElements() == 0) return false;
96
97 ConstantInt *Null =
98 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
99 if (Null == 0 || Null->getZExtValue() != 0)
100 return false; // Not null terminated.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000101
Chris Lattner1850e5a2009-08-04 16:13:09 +0000102 // Verify that the null doesn't occur anywhere else in the string.
103 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
104 // Reject constantexpr elements etc.
105 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
106 CVA->getOperand(i) == Null)
107 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000108 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000109 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000110
111 // Another possibility: [1 x i8] zeroinitializer
112 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000113 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000114
115 return false;
116}
117
Chris Lattner58bed8f2009-08-05 04:25:40 +0000118/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000119/// a global variable. Given an global variable and information from TM, it
120/// classifies the global in a variety of ways that make various target
121/// implementations simpler. The target implementation is free to ignore this
122/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000123SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
124 const TargetMachine &TM){
125 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
126 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000127
Chris Lattnerf0144122009-07-28 03:13:23 +0000128 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000129
Chris Lattnerf0144122009-07-28 03:13:23 +0000130 // Early exit - functions should be always in text sections.
131 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
132 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000133 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000134
Chris Lattnerf0144122009-07-28 03:13:23 +0000135 // Handle thread-local data first.
136 if (GVar->isThreadLocal()) {
137 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000138 return SectionKind::getThreadBSS();
139 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000140 }
141
Chris Lattnera3839bc2010-01-19 02:48:26 +0000142 // Variables with common linkage always get classified as common.
143 if (GVar->hasCommonLinkage())
144 return SectionKind::getCommon();
145
Chris Lattnerf0144122009-07-28 03:13:23 +0000146 // Variable can be easily put to BSS section.
Chris Lattnerce8749e2010-01-19 04:15:51 +0000147 if (isSuitableForBSS(GVar)) {
148 if (GVar->hasLocalLinkage())
149 return SectionKind::getBSSLocal();
150 else if (GVar->hasExternalLinkage())
151 return SectionKind::getBSSExtern();
Chris Lattner27981192009-08-01 23:57:16 +0000152 return SectionKind::getBSS();
Chris Lattnerce8749e2010-01-19 04:15:51 +0000153 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000154
155 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000156
Chris Lattnerf0144122009-07-28 03:13:23 +0000157 // If the global is marked constant, we can put it into a mergable section,
158 // a mergable string section, or general .data if it contains relocations.
159 if (GVar->isConstant()) {
160 // If the initializer for the global contains something that requires a
161 // relocation, then we may have to drop this into a wriable data section
162 // even though it is marked const.
163 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000164 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000165 case Constant::NoRelocation:
166 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000167 // section of the right width.
168 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000169 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000170 dyn_cast<IntegerType>(ATy->getElementType())) {
171 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
172 ITy->getBitWidth() == 32) &&
173 IsNullTerminatedString(C)) {
174 if (ITy->getBitWidth() == 8)
175 return SectionKind::getMergeable1ByteCString();
176 if (ITy->getBitWidth() == 16)
177 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000178
Chris Lattner1850e5a2009-08-04 16:13:09 +0000179 assert(ITy->getBitWidth() == 32 && "Unknown width");
180 return SectionKind::getMergeable4ByteCString();
181 }
182 }
183 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000184
Chris Lattnerf0144122009-07-28 03:13:23 +0000185 // Otherwise, just drop it into a mergable constant section. If we have
186 // a section for this size, use it, otherwise use the arbitrary sized
187 // mergable section.
188 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000189 case 4: return SectionKind::getMergeableConst4();
190 case 8: return SectionKind::getMergeableConst8();
191 case 16: return SectionKind::getMergeableConst16();
192 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000193 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000194
Chris Lattnerf0144122009-07-28 03:13:23 +0000195 case Constant::LocalRelocation:
196 // In static relocation model, the linker will resolve all addresses, so
197 // the relocation entries will actually be constants by the time the app
198 // starts up. However, we can't put this into a mergable section, because
199 // the linker doesn't take relocations into consideration when it tries to
200 // merge entries in the section.
201 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000202 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000203
Chris Lattnerf0144122009-07-28 03:13:23 +0000204 // Otherwise, the dynamic linker needs to fix it up, put it in the
205 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000206 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000207
Chris Lattnerf0144122009-07-28 03:13:23 +0000208 case Constant::GlobalRelocations:
209 // In static relocation model, the linker will resolve all addresses, so
210 // the relocation entries will actually be constants by the time the app
211 // starts up. However, we can't put this into a mergable section, because
212 // the linker doesn't take relocations into consideration when it tries to
213 // merge entries in the section.
214 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000215 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000216
Chris Lattnerf0144122009-07-28 03:13:23 +0000217 // Otherwise, the dynamic linker needs to fix it up, put it in the
218 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000219 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000220 }
221 }
222
223 // Okay, this isn't a constant. If the initializer for the global is going
224 // to require a runtime relocation by the dynamic linker, put it into a more
225 // specific section to improve startup time of the app. This coalesces these
226 // globals together onto fewer pages, improving the locality of the dynamic
227 // linker.
228 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000229 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000230
231 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000232 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000233 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000234 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000235 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000236 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000237 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000238 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000239 }
240}
241
242/// SectionForGlobal - This method computes the appropriate section to emit
243/// the specified global variable or function definition. This should not
244/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000245const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000246SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000247 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000248 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000249 if (GV->hasSection())
250 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000251
252
Chris Lattnerf0144122009-07-28 03:13:23 +0000253 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000254 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000255}
256
Chris Lattner58bed8f2009-08-05 04:25:40 +0000257
Chris Lattnerf0144122009-07-28 03:13:23 +0000258// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000259const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000260TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000261 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000262 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000263 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000264 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000265
Chris Lattnerf9650c02009-08-01 21:46:23 +0000266 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000267 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000268
Chris Lattner82458382009-08-01 21:56:13 +0000269 if (Kind.isBSS() && BSSSection != 0)
270 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000271
Chris Lattnerf9650c02009-08-01 21:46:23 +0000272 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000273 return ReadOnlySection;
274
275 return getDataSection();
276}
277
Chris Lattner83d77fa2009-08-01 23:46:12 +0000278/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000279/// specified size and relocation information, return a section that it
280/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000281const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000282TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000283 if (Kind.isReadOnly() && ReadOnlySection != 0)
284 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000285
Chris Lattnerf0144122009-07-28 03:13:23 +0000286 return DataSection;
287}
288
Chris Lattner8c6ed052009-09-16 01:46:41 +0000289/// getSymbolForDwarfGlobalReference - Return an MCExpr to use for a
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000290/// reference to the specified global variable from exception
291/// handling information.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000292const MCExpr *TargetLoweringObjectFile::
293getSymbolForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000294 MachineModuleInfo *MMI, unsigned Encoding) const {
Chris Lattner45111d12010-01-16 21:57:06 +0000295 // FIXME: Use GetGlobalValueSymbol.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000296 SmallString<128> Name;
297 Mang->getNameWithPrefix(Name, GV, false);
Chris Lattner98cdab52010-03-10 02:25:11 +0000298 const MCSymbol *Sym;
299
300 if (GV->hasPrivateLinkage())
301 Sym = getContext().GetOrCreateTemporarySymbol(Name.str());
302 else
303 Sym = getContext().GetOrCreateSymbol(Name.str());
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000304
305 return getSymbolForDwarfReference(Sym, MMI, Encoding);
Chris Lattner8c6ed052009-09-16 01:46:41 +0000306}
Chris Lattnerf0144122009-07-28 03:13:23 +0000307
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000308const MCExpr *TargetLoweringObjectFile::
309getSymbolForDwarfReference(const MCSymbol *Sym, MachineModuleInfo *MMI,
310 unsigned Encoding) const {
311 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
312
313 switch (Encoding & 0xF0) {
314 default:
Bill Wendlingb76beda2010-02-16 22:47:14 +0000315 llvm_report_error("We do not support this DWARF encoding yet!");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000316 break;
317 case dwarf::DW_EH_PE_absptr:
318 // Do nothing special
319 break;
320 case dwarf::DW_EH_PE_pcrel:
321 // FIXME: PCSymbol
322 const MCExpr *PC = MCSymbolRefExpr::Create(".", getContext());
323 Res = MCBinaryExpr::CreateSub(Res, PC, getContext());
324 break;
325 }
326
327 return Res;
328}
329
330unsigned TargetLoweringObjectFile::getPersonalityEncoding() const {
331 return dwarf::DW_EH_PE_absptr;
332}
333
334unsigned TargetLoweringObjectFile::getLSDAEncoding() const {
335 return dwarf::DW_EH_PE_absptr;
336}
337
338unsigned TargetLoweringObjectFile::getFDEEncoding() const {
339 return dwarf::DW_EH_PE_absptr;
340}
341
342unsigned TargetLoweringObjectFile::getTTypeEncoding() const {
343 return dwarf::DW_EH_PE_absptr;
344}
Chris Lattnerf0144122009-07-28 03:13:23 +0000345