blob: 7b8d1108f1bf86963066c9a5b86080814a0d962f [file] [log] [blame]
Chris Lattner5e693ed2009-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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Constants.h"
17#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/GlobalVariable.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000021#include "llvm/MC/MCContext.h"
Chris Lattnerb8666022009-09-16 01:46:41 +000022#include "llvm/MC/MCExpr.h"
Chris Lattner03627cb2010-03-11 21:55:20 +000023#include "llvm/MC/MCStreamer.h"
Chris Lattnerccbeed22010-01-13 21:29:21 +000024#include "llvm/MC/MCSymbol.h"
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +000025#include "llvm/Support/Dwarf.h"
Chris Lattnerd82510e2009-11-07 09:20:54 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattnerccbeed22010-01-13 21:29:21 +000027#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Target/Mangler.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetOptions.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// Generic Code
35//===----------------------------------------------------------------------===//
36
Evan Cheng76792992011-07-20 05:58:47 +000037/// Initialize - this method must be called before any actual lowering is
38/// done. This specifies the current context for codegen, and gives the
39/// lowering implementations a chance to set up their default sections.
40void TargetLoweringObjectFile::Initialize(MCContext &ctx,
41 const TargetMachine &TM) {
42 Ctx = &ctx;
Evan Chengbbf3b0d2011-07-20 19:50:42 +000043 InitMCObjectFileInfo(TM.getTargetTriple(),
44 TM.getRelocationModel(), TM.getCodeModel(), *Ctx);
Chris Lattner5e693ed2009-07-28 03:13:23 +000045}
Evan Cheng76792992011-07-20 05:58:47 +000046
Chris Lattner5e693ed2009-07-28 03:13:23 +000047TargetLoweringObjectFile::~TargetLoweringObjectFile() {
48}
49
Nick Lewycky50f02cb2011-12-02 22:16:29 +000050static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) {
Jay Foad60020682011-06-19 18:37:11 +000051 const Constant *C = GV->getInitializer();
Anton Korobeynikov1df58862009-09-09 08:41:20 +000052
Chris Lattner5e693ed2009-07-28 03:13:23 +000053 // Must have zero initializer.
54 if (!C->isNullValue())
55 return false;
Anton Korobeynikov1df58862009-09-09 08:41:20 +000056
Chris Lattner5e693ed2009-07-28 03:13:23 +000057 // Leave constant zeros in readonly constant sections, so they can be shared.
58 if (GV->isConstant())
59 return false;
Anton Korobeynikov1df58862009-09-09 08:41:20 +000060
Chris Lattner5e693ed2009-07-28 03:13:23 +000061 // If the global has an explicit section specified, don't put it in BSS.
62 if (!GV->getSection().empty())
63 return false;
Anton Korobeynikov1df58862009-09-09 08:41:20 +000064
Chris Lattner5e693ed2009-07-28 03:13:23 +000065 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
66 if (NoZerosInBSS)
67 return false;
Anton Korobeynikov1df58862009-09-09 08:41:20 +000068
Chris Lattner5e693ed2009-07-28 03:13:23 +000069 // Otherwise, put it in BSS!
70 return true;
71}
72
Chris Lattner81bbf442009-08-04 16:13:09 +000073/// IsNullTerminatedString - Return true if the specified constant (which is
74/// known to have a type that is an array of 1/2/4 byte elements) ends with a
Chris Lattner139822f2012-01-24 14:17:05 +000075/// nul value and contains no other nuls in it. Note that this is more general
76/// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
Chris Lattner81bbf442009-08-04 16:13:09 +000077static bool IsNullTerminatedString(const Constant *C) {
Chris Lattner139822f2012-01-24 14:17:05 +000078 // First check: is we have constant array terminated with zero
Chris Lattner139822f2012-01-24 14:17:05 +000079 if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
80 unsigned NumElts = CDS->getNumElements();
81 assert(NumElts != 0 && "Can't have an empty CDS");
82
83 if (CDS->getElementAsInteger(NumElts-1) != 0)
84 return false; // Not null terminated.
85
86 // Verify that the null doesn't occur anywhere else in the string.
87 for (unsigned i = 0; i != NumElts-1; ++i)
88 if (CDS->getElementAsInteger(i) == 0)
89 return false;
90 return true;
91 }
Chris Lattner5e693ed2009-07-28 03:13:23 +000092
93 // Another possibility: [1 x i8] zeroinitializer
94 if (isa<ConstantAggregateZero>(C))
Chris Lattner139822f2012-01-24 14:17:05 +000095 return cast<ArrayType>(C->getType())->getNumElements() == 1;
Chris Lattner5e693ed2009-07-28 03:13:23 +000096
97 return false;
98}
99
Rafael Espindolae133ed82013-10-29 17:28:26 +0000100/// Return the MCSymbol for the specified global value. This
101/// symbol is the main label that is the address of the global.
102MCSymbol *TargetLoweringObjectFile::getSymbol(Mangler &M,
103 const GlobalValue *GV) const {
104 SmallString<60> NameStr;
105 M.getNameWithPrefix(NameStr, GV, false);
106 return Ctx->GetOrCreateSymbol(NameStr.str());
107}
108
109
Rafael Espindola08704342011-04-27 23:08:15 +0000110MCSymbol *TargetLoweringObjectFile::
Rafael Espindolace83fc32011-04-27 23:17:57 +0000111getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola08704342011-04-27 23:08:15 +0000112 MachineModuleInfo *MMI) const {
Rafael Espindolae133ed82013-10-29 17:28:26 +0000113 return getSymbol(*Mang, GV);
Rafael Espindolaa83b1772011-04-16 03:51:21 +0000114}
115
116void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
117 const TargetMachine &TM,
118 const MCSymbol *Sym) const {
Rafael Espindolaa83b1772011-04-16 03:51:21 +0000119}
120
121
Chris Lattnercbc7b262009-08-05 04:25:40 +0000122/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattnerc9c277b2009-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 Lattnercbc7b262009-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 Korobeynikov1df58862009-09-09 08:41:20 +0000131
Chris Lattner5e693ed2009-07-28 03:13:23 +0000132 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000133
Chris Lattner5e693ed2009-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 Lattnerf8d97102009-08-01 23:57:16 +0000137 return SectionKind::getText();
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000138
Chris Lattner5e693ed2009-07-28 03:13:23 +0000139 // Handle thread-local data first.
140 if (GVar->isThreadLocal()) {
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000141 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS))
Chris Lattnerf8d97102009-08-01 23:57:16 +0000142 return SectionKind::getThreadBSS();
143 return SectionKind::getThreadData();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000144 }
145
Chris Lattner5b585f82010-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 Lattner5e693ed2009-07-28 03:13:23 +0000150 // Variable can be easily put to BSS section.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000151 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) {
Chris Lattnerb2534212010-01-19 04:15:51 +0000152 if (GVar->hasLocalLinkage())
153 return SectionKind::getBSSLocal();
154 else if (GVar->hasExternalLinkage())
155 return SectionKind::getBSSExtern();
Chris Lattnerf8d97102009-08-01 23:57:16 +0000156 return SectionKind::getBSS();
Chris Lattnerb2534212010-01-19 04:15:51 +0000157 }
Chris Lattner5e693ed2009-07-28 03:13:23 +0000158
Jay Foad60020682011-06-19 18:37:11 +0000159 const Constant *C = GVar->getInitializer();
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000160
Chris Lattner5e693ed2009-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.
Chris Lattnerea4e9832011-01-18 01:23:44 +0000163 if (GVar->isConstant()) {
Chris Lattner5e693ed2009-07-28 03:13:23 +0000164 // If the initializer for the global contains something that requires a
Eric Christopherde9e92e2012-05-05 01:16:06 +0000165 // relocation, then we may have to drop this into a writable data section
Chris Lattner5e693ed2009-07-28 03:13:23 +0000166 // even though it is marked const.
167 switch (C->getRelocationInfo()) {
Chris Lattner5e693ed2009-07-28 03:13:23 +0000168 case Constant::NoRelocation:
Chris Lattnerea4e9832011-01-18 01:23:44 +0000169 // If the global is required to have a unique address, it can't be put
170 // into a mergable section: just drop it into the general read-only
171 // section instead.
172 if (!GVar->hasUnnamedAddr())
173 return SectionKind::getReadOnly();
174
Chris Lattner5e693ed2009-07-28 03:13:23 +0000175 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner81bbf442009-08-04 16:13:09 +0000176 // section of the right width.
Chris Lattner229907c2011-07-18 04:54:35 +0000177 if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
178 if (IntegerType *ITy =
Chris Lattner81bbf442009-08-04 16:13:09 +0000179 dyn_cast<IntegerType>(ATy->getElementType())) {
180 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
181 ITy->getBitWidth() == 32) &&
182 IsNullTerminatedString(C)) {
183 if (ITy->getBitWidth() == 8)
184 return SectionKind::getMergeable1ByteCString();
185 if (ITy->getBitWidth() == 16)
186 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000187
Chris Lattner81bbf442009-08-04 16:13:09 +0000188 assert(ITy->getBitWidth() == 32 && "Unknown width");
189 return SectionKind::getMergeable4ByteCString();
190 }
191 }
192 }
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000193
Chris Lattner5e693ed2009-07-28 03:13:23 +0000194 // Otherwise, just drop it into a mergable constant section. If we have
195 // a section for this size, use it, otherwise use the arbitrary sized
196 // mergable section.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000197 switch (TM.getDataLayout()->getTypeAllocSize(C->getType())) {
Chris Lattnerf8d97102009-08-01 23:57:16 +0000198 case 4: return SectionKind::getMergeableConst4();
199 case 8: return SectionKind::getMergeableConst8();
200 case 16: return SectionKind::getMergeableConst16();
201 default: return SectionKind::getMergeableConst();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000202 }
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000203
Chris Lattner5e693ed2009-07-28 03:13:23 +0000204 case Constant::LocalRelocation:
205 // In static relocation model, the linker will resolve all addresses, so
206 // the relocation entries will actually be constants by the time the app
207 // starts up. However, we can't put this into a mergable section, because
208 // the linker doesn't take relocations into consideration when it tries to
209 // merge entries in the section.
210 if (ReloModel == Reloc::Static)
Chris Lattnerf8d97102009-08-01 23:57:16 +0000211 return SectionKind::getReadOnly();
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000212
Chris Lattner5e693ed2009-07-28 03:13:23 +0000213 // Otherwise, the dynamic linker needs to fix it up, put it in the
214 // writable data.rel.local section.
Chris Lattnerf8d97102009-08-01 23:57:16 +0000215 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000216
Chris Lattner5e693ed2009-07-28 03:13:23 +0000217 case Constant::GlobalRelocations:
218 // In static relocation model, the linker will resolve all addresses, so
219 // the relocation entries will actually be constants by the time the app
220 // starts up. However, we can't put this into a mergable section, because
221 // the linker doesn't take relocations into consideration when it tries to
222 // merge entries in the section.
223 if (ReloModel == Reloc::Static)
Chris Lattnerf8d97102009-08-01 23:57:16 +0000224 return SectionKind::getReadOnly();
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000225
Chris Lattner5e693ed2009-07-28 03:13:23 +0000226 // Otherwise, the dynamic linker needs to fix it up, put it in the
227 // writable data.rel section.
Chris Lattnerf8d97102009-08-01 23:57:16 +0000228 return SectionKind::getReadOnlyWithRel();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000229 }
230 }
231
232 // Okay, this isn't a constant. If the initializer for the global is going
233 // to require a runtime relocation by the dynamic linker, put it into a more
234 // specific section to improve startup time of the app. This coalesces these
235 // globals together onto fewer pages, improving the locality of the dynamic
236 // linker.
237 if (ReloModel == Reloc::Static)
Chris Lattnerf8d97102009-08-01 23:57:16 +0000238 return SectionKind::getDataNoRel();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000239
240 switch (C->getRelocationInfo()) {
Chris Lattner5e693ed2009-07-28 03:13:23 +0000241 case Constant::NoRelocation:
Chris Lattnerf8d97102009-08-01 23:57:16 +0000242 return SectionKind::getDataNoRel();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000243 case Constant::LocalRelocation:
Chris Lattnerf8d97102009-08-01 23:57:16 +0000244 return SectionKind::getDataRelLocal();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000245 case Constant::GlobalRelocations:
Chris Lattnerf8d97102009-08-01 23:57:16 +0000246 return SectionKind::getDataRel();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000247 }
Chandler Carruthf3e85022012-01-10 18:08:01 +0000248 llvm_unreachable("Invalid relocation");
Chris Lattner5e693ed2009-07-28 03:13:23 +0000249}
250
251/// SectionForGlobal - This method computes the appropriate section to emit
252/// the specified global variable or function definition. This should not
253/// be passed external (or available externally) globals.
Chris Lattner4d2c0f92009-07-31 18:48:30 +0000254const MCSection *TargetLoweringObjectFile::
Chris Lattnercbc7b262009-08-05 04:25:40 +0000255SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattner50343292009-07-29 05:09:30 +0000256 const TargetMachine &TM) const {
Chris Lattner5e693ed2009-07-28 03:13:23 +0000257 // Select section name.
Chris Lattner1ff90132009-08-06 16:39:58 +0000258 if (GV->hasSection())
259 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000260
261
Chris Lattner5e693ed2009-07-28 03:13:23 +0000262 // Use default section depending on the 'type' of global
Chris Lattner26fb2772009-08-01 21:46:23 +0000263 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattner5e693ed2009-07-28 03:13:23 +0000264}
265
Chris Lattnercbc7b262009-08-05 04:25:40 +0000266
Chris Lattner5e693ed2009-07-28 03:13:23 +0000267// Lame default implementation. Calculate the section name for global.
Chris Lattner4d2c0f92009-07-31 18:48:30 +0000268const MCSection *
Chris Lattner5e693ed2009-07-28 03:13:23 +0000269TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattner26fb2772009-08-01 21:46:23 +0000270 SectionKind Kind,
Chris Lattner50343292009-07-29 05:09:30 +0000271 Mangler *Mang,
Chris Lattner5e693ed2009-07-28 03:13:23 +0000272 const TargetMachine &TM) const{
Chris Lattner26fb2772009-08-01 21:46:23 +0000273 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000274
Chris Lattner26fb2772009-08-01 21:46:23 +0000275 if (Kind.isText())
Chris Lattner5e693ed2009-07-28 03:13:23 +0000276 return getTextSection();
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000277
Chris Lattnerd5c01132009-08-01 21:56:13 +0000278 if (Kind.isBSS() && BSSSection != 0)
279 return BSSSection;
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000280
Chris Lattner26fb2772009-08-01 21:46:23 +0000281 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattner5e693ed2009-07-28 03:13:23 +0000282 return ReadOnlySection;
283
284 return getDataSection();
285}
286
Chris Lattner0c402662009-08-01 23:46:12 +0000287/// getSectionForConstant - Given a mergable constant with the
Chris Lattner5e693ed2009-07-28 03:13:23 +0000288/// specified size and relocation information, return a section that it
289/// should be placed in.
Chris Lattner4d2c0f92009-07-31 18:48:30 +0000290const MCSection *
Chris Lattner0c402662009-08-01 23:46:12 +0000291TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattner5e693ed2009-07-28 03:13:23 +0000292 if (Kind.isReadOnly() && ReadOnlySection != 0)
293 return ReadOnlySection;
Anton Korobeynikov1df58862009-09-09 08:41:20 +0000294
Chris Lattner5e693ed2009-07-28 03:13:23 +0000295 return DataSection;
296}
297
Anton Korobeynikove42af362012-11-14 01:47:00 +0000298/// getTTypeGlobalReference - Return an MCExpr to use for a
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000299/// reference to the specified global variable from exception
300/// handling information.
Chris Lattnerb8666022009-09-16 01:46:41 +0000301const MCExpr *TargetLoweringObjectFile::
Anton Korobeynikove42af362012-11-14 01:47:00 +0000302getTTypeGlobalReference(const GlobalValue *GV, Mangler *Mang,
303 MachineModuleInfo *MMI, unsigned Encoding,
304 MCStreamer &Streamer) const {
305 const MCSymbolRefExpr *Ref =
Rafael Espindolae133ed82013-10-29 17:28:26 +0000306 MCSymbolRefExpr::Create(getSymbol(*Mang, GV), getContext());
Anton Korobeynikove42af362012-11-14 01:47:00 +0000307
308 return getTTypeReference(Ref, Encoding, Streamer);
Chris Lattnerb8666022009-09-16 01:46:41 +0000309}
Chris Lattner5e693ed2009-07-28 03:13:23 +0000310
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000311const MCExpr *TargetLoweringObjectFile::
Anton Korobeynikove42af362012-11-14 01:47:00 +0000312getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
313 MCStreamer &Streamer) const {
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000314 switch (Encoding & 0x70) {
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000315 default:
Chris Lattner2104b8d2010-04-07 22:58:41 +0000316 report_fatal_error("We do not support this DWARF encoding yet!");
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000317 case dwarf::DW_EH_PE_absptr:
318 // Do nothing special
Anton Korobeynikove42af362012-11-14 01:47:00 +0000319 return Sym;
Chris Lattner03627cb2010-03-11 21:55:20 +0000320 case dwarf::DW_EH_PE_pcrel: {
321 // Emit a label to the streamer for the current position. This gives us
322 // .-foo addressing.
Chris Lattneraed00fa2010-03-17 05:41:18 +0000323 MCSymbol *PCSym = getContext().CreateTempSymbol();
Chris Lattner03627cb2010-03-11 21:55:20 +0000324 Streamer.EmitLabel(PCSym);
325 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
Anton Korobeynikove42af362012-11-14 01:47:00 +0000326 return MCBinaryExpr::CreateSub(Sym, PC, getContext());
Chris Lattner03627cb2010-03-11 21:55:20 +0000327 }
328 }
Anton Korobeynikovae4ccc12010-02-15 22:35:59 +0000329}
David Blaikief2694972013-06-28 20:05:11 +0000330
Ulrich Weigand2b6fc8d2013-07-02 18:47:09 +0000331const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
David Blaikief2694972013-06-28 20:05:11 +0000332 // FIXME: It's not clear what, if any, default this should have - perhaps a
333 // null return could mean 'no location' & we should just do that here.
334 return MCSymbolRefExpr::Create(Sym, *Ctx);
335}