blob: 15896043466a534ab3d240f4e9a8b3003999f0e1 [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
Evan Chenge76a33b2011-07-20 05:58:47 +000038/// Initialize - this method must be called before any actual lowering is
39/// done. This specifies the current context for codegen, and gives the
40/// lowering implementations a chance to set up their default sections.
41void TargetLoweringObjectFile::Initialize(MCContext &ctx,
42 const TargetMachine &TM) {
43 Ctx = &ctx;
Evan Cheng203576a2011-07-20 19:50:42 +000044 InitMCObjectFileInfo(TM.getTargetTriple(),
45 TM.getRelocationModel(), TM.getCodeModel(), *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +000046}
Evan Chenge76a33b2011-07-20 05:58:47 +000047
Chris Lattnerf0144122009-07-28 03:13:23 +000048TargetLoweringObjectFile::~TargetLoweringObjectFile() {
49}
50
Nick Lewycky8a8d4792011-12-02 22:16:29 +000051static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) {
Jay Foad7d715df2011-06-19 18:37:11 +000052 const Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000053
Chris Lattnerf0144122009-07-28 03:13:23 +000054 // Must have zero initializer.
55 if (!C->isNullValue())
56 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000057
Chris Lattnerf0144122009-07-28 03:13:23 +000058 // Leave constant zeros in readonly constant sections, so they can be shared.
59 if (GV->isConstant())
60 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000061
Chris Lattnerf0144122009-07-28 03:13:23 +000062 // If the global has an explicit section specified, don't put it in BSS.
63 if (!GV->getSection().empty())
64 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000065
Chris Lattnerf0144122009-07-28 03:13:23 +000066 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
67 if (NoZerosInBSS)
68 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000069
Chris Lattnerf0144122009-07-28 03:13:23 +000070 // Otherwise, put it in BSS!
71 return true;
72}
73
Chris Lattner1850e5a2009-08-04 16:13:09 +000074/// IsNullTerminatedString - Return true if the specified constant (which is
75/// known to have a type that is an array of 1/2/4 byte elements) ends with a
Chris Lattner29cc6cb2012-01-24 14:17:05 +000076/// nul value and contains no other nuls in it. Note that this is more general
77/// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
Chris Lattner1850e5a2009-08-04 16:13:09 +000078static bool IsNullTerminatedString(const Constant *C) {
Chris Lattner29cc6cb2012-01-24 14:17:05 +000079 // First check: is we have constant array terminated with zero
Chris Lattner29cc6cb2012-01-24 14:17:05 +000080 if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
81 unsigned NumElts = CDS->getNumElements();
82 assert(NumElts != 0 && "Can't have an empty CDS");
83
84 if (CDS->getElementAsInteger(NumElts-1) != 0)
85 return false; // Not null terminated.
86
87 // Verify that the null doesn't occur anywhere else in the string.
88 for (unsigned i = 0; i != NumElts-1; ++i)
89 if (CDS->getElementAsInteger(i) == 0)
90 return false;
91 return true;
92 }
Chris Lattnerf0144122009-07-28 03:13:23 +000093
94 // Another possibility: [1 x i8] zeroinitializer
95 if (isa<ConstantAggregateZero>(C))
Chris Lattner29cc6cb2012-01-24 14:17:05 +000096 return cast<ArrayType>(C->getType())->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +000097
98 return false;
99}
100
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000101MCSymbol *TargetLoweringObjectFile::
Rafael Espindola60246a92011-04-27 23:17:57 +0000102getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000103 MachineModuleInfo *MMI) const {
104 return Mang->getSymbol(GV);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000105}
106
107void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
108 const TargetMachine &TM,
109 const MCSymbol *Sym) const {
Rafael Espindola30deafc2011-04-16 03:51:21 +0000110}
111
112
Chris Lattner58bed8f2009-08-05 04:25:40 +0000113/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000114/// a global variable. Given an global variable and information from TM, it
115/// classifies the global in a variety of ways that make various target
116/// implementations simpler. The target implementation is free to ignore this
117/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000118SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
119 const TargetMachine &TM){
120 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
121 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000122
Chris Lattnerf0144122009-07-28 03:13:23 +0000123 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000124
Chris Lattnerf0144122009-07-28 03:13:23 +0000125 // Early exit - functions should be always in text sections.
126 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
127 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000128 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000129
Chris Lattnerf0144122009-07-28 03:13:23 +0000130 // Handle thread-local data first.
131 if (GVar->isThreadLocal()) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000132 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS))
Chris Lattner27981192009-08-01 23:57:16 +0000133 return SectionKind::getThreadBSS();
134 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000135 }
136
Chris Lattnera3839bc2010-01-19 02:48:26 +0000137 // Variables with common linkage always get classified as common.
138 if (GVar->hasCommonLinkage())
139 return SectionKind::getCommon();
140
Chris Lattnerf0144122009-07-28 03:13:23 +0000141 // Variable can be easily put to BSS section.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000142 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) {
Chris Lattnerce8749e2010-01-19 04:15:51 +0000143 if (GVar->hasLocalLinkage())
144 return SectionKind::getBSSLocal();
145 else if (GVar->hasExternalLinkage())
146 return SectionKind::getBSSExtern();
Chris Lattner27981192009-08-01 23:57:16 +0000147 return SectionKind::getBSS();
Chris Lattnerce8749e2010-01-19 04:15:51 +0000148 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000149
Jay Foad7d715df2011-06-19 18:37:11 +0000150 const Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000151
Chris Lattnerf0144122009-07-28 03:13:23 +0000152 // If the global is marked constant, we can put it into a mergable section,
153 // a mergable string section, or general .data if it contains relocations.
Chris Lattner32899192011-01-18 01:23:44 +0000154 if (GVar->isConstant()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000155 // If the initializer for the global contains something that requires a
156 // relocation, then we may have to drop this into a wriable data section
157 // even though it is marked const.
158 switch (C->getRelocationInfo()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000159 case Constant::NoRelocation:
Chris Lattner32899192011-01-18 01:23:44 +0000160 // If the global is required to have a unique address, it can't be put
161 // into a mergable section: just drop it into the general read-only
162 // section instead.
163 if (!GVar->hasUnnamedAddr())
164 return SectionKind::getReadOnly();
165
Chris Lattnerf0144122009-07-28 03:13:23 +0000166 // 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.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000168 if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
169 if (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 Lattnerf0144122009-07-28 03:13:23 +0000232 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000233 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000234 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000235 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000236 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000237 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000238 }
Chandler Carruth732f05c2012-01-10 18:08:01 +0000239 llvm_unreachable("Invalid relocation");
Chris Lattnerf0144122009-07-28 03:13:23 +0000240}
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 Lattner3192d142010-03-11 19:41:58 +0000289/// getExprForDwarfGlobalReference - 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::
Chris Lattner3192d142010-03-11 19:41:58 +0000293getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
294 MachineModuleInfo *MMI, unsigned Encoding,
295 MCStreamer &Streamer) const {
Chris Lattner73ff5642010-03-12 18:55:20 +0000296 const MCSymbol *Sym = Mang->getSymbol(GV);
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000297 return getExprForDwarfReference(Sym, Encoding, Streamer);
Chris Lattner8c6ed052009-09-16 01:46:41 +0000298}
Chris Lattnerf0144122009-07-28 03:13:23 +0000299
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000300const MCExpr *TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000301getExprForDwarfReference(const MCSymbol *Sym, unsigned Encoding,
Chris Lattner42263e22010-03-11 21:55:20 +0000302 MCStreamer &Streamer) const {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000303 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
304
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000305 switch (Encoding & 0x70) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000306 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000307 report_fatal_error("We do not support this DWARF encoding yet!");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000308 case dwarf::DW_EH_PE_absptr:
309 // Do nothing special
Chris Lattner42263e22010-03-11 21:55:20 +0000310 return Res;
311 case dwarf::DW_EH_PE_pcrel: {
312 // Emit a label to the streamer for the current position. This gives us
313 // .-foo addressing.
Chris Lattner77e76942010-03-17 05:41:18 +0000314 MCSymbol *PCSym = getContext().CreateTempSymbol();
Chris Lattner42263e22010-03-11 21:55:20 +0000315 Streamer.EmitLabel(PCSym);
316 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
317 return MCBinaryExpr::CreateSub(Res, PC, getContext());
318 }
319 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000320}