blob: b74a0bd25d72fd75a42a302375ce696583b29516 [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 Lattnerf0144122009-07-28 03:13:23 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// Generic Code
35//===----------------------------------------------------------------------===//
36
Evan Chenge76a33b2011-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 Cheng203576a2011-07-20 19:50:42 +000043 InitMCObjectFileInfo(TM.getTargetTriple(),
44 TM.getRelocationModel(), TM.getCodeModel(), *Ctx);
Chris Lattnerf0144122009-07-28 03:13:23 +000045}
Evan Chenge76a33b2011-07-20 05:58:47 +000046
Chris Lattnerf0144122009-07-28 03:13:23 +000047TargetLoweringObjectFile::~TargetLoweringObjectFile() {
48}
49
Nick Lewycky8a8d4792011-12-02 22:16:29 +000050static bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) {
Jay Foad7d715df2011-06-19 18:37:11 +000051 const Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000052
Chris Lattnerf0144122009-07-28 03:13:23 +000053 // Must have zero initializer.
54 if (!C->isNullValue())
55 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000056
Chris Lattnerf0144122009-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 Korobeynikov8ddb5692009-09-09 08:41:20 +000060
Chris Lattnerf0144122009-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 Korobeynikov8ddb5692009-09-09 08:41:20 +000064
Chris Lattnerf0144122009-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 Korobeynikov8ddb5692009-09-09 08:41:20 +000068
Chris Lattnerf0144122009-07-28 03:13:23 +000069 // Otherwise, put it in BSS!
70 return true;
71}
72
Chris Lattner1850e5a2009-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 Lattner29cc6cb2012-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 Lattner1850e5a2009-08-04 16:13:09 +000077static bool IsNullTerminatedString(const Constant *C) {
Chris Lattner29cc6cb2012-01-24 14:17:05 +000078 // First check: is we have constant array terminated with zero
Chris Lattner29cc6cb2012-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 Lattnerf0144122009-07-28 03:13:23 +000092
93 // Another possibility: [1 x i8] zeroinitializer
94 if (isa<ConstantAggregateZero>(C))
Chris Lattner29cc6cb2012-01-24 14:17:05 +000095 return cast<ArrayType>(C->getType())->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +000096
97 return false;
98}
99
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000100MCSymbol *TargetLoweringObjectFile::
Rafael Espindola60246a92011-04-27 23:17:57 +0000101getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000102 MachineModuleInfo *MMI) const {
103 return Mang->getSymbol(GV);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000104}
105
106void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
107 const TargetMachine &TM,
108 const MCSymbol *Sym) const {
Rafael Espindola30deafc2011-04-16 03:51:21 +0000109}
110
111
Chris Lattner58bed8f2009-08-05 04:25:40 +0000112/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000113/// a global variable. Given an global variable and information from TM, it
114/// classifies the global in a variety of ways that make various target
115/// implementations simpler. The target implementation is free to ignore this
116/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000117SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
118 const TargetMachine &TM){
119 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
120 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000121
Chris Lattnerf0144122009-07-28 03:13:23 +0000122 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000123
Chris Lattnerf0144122009-07-28 03:13:23 +0000124 // Early exit - functions should be always in text sections.
125 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
126 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000127 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000128
Chris Lattnerf0144122009-07-28 03:13:23 +0000129 // Handle thread-local data first.
130 if (GVar->isThreadLocal()) {
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000131 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS))
Chris Lattner27981192009-08-01 23:57:16 +0000132 return SectionKind::getThreadBSS();
133 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000134 }
135
Chris Lattnera3839bc2010-01-19 02:48:26 +0000136 // Variables with common linkage always get classified as common.
137 if (GVar->hasCommonLinkage())
138 return SectionKind::getCommon();
139
Chris Lattnerf0144122009-07-28 03:13:23 +0000140 // Variable can be easily put to BSS section.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000141 if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) {
Chris Lattnerce8749e2010-01-19 04:15:51 +0000142 if (GVar->hasLocalLinkage())
143 return SectionKind::getBSSLocal();
144 else if (GVar->hasExternalLinkage())
145 return SectionKind::getBSSExtern();
Chris Lattner27981192009-08-01 23:57:16 +0000146 return SectionKind::getBSS();
Chris Lattnerce8749e2010-01-19 04:15:51 +0000147 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000148
Jay Foad7d715df2011-06-19 18:37:11 +0000149 const Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000150
Chris Lattnerf0144122009-07-28 03:13:23 +0000151 // If the global is marked constant, we can put it into a mergable section,
152 // a mergable string section, or general .data if it contains relocations.
Chris Lattner32899192011-01-18 01:23:44 +0000153 if (GVar->isConstant()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000154 // If the initializer for the global contains something that requires a
Eric Christopherf2eed382012-05-05 01:16:06 +0000155 // relocation, then we may have to drop this into a writable data section
Chris Lattnerf0144122009-07-28 03:13:23 +0000156 // even though it is marked const.
157 switch (C->getRelocationInfo()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000158 case Constant::NoRelocation:
Chris Lattner32899192011-01-18 01:23:44 +0000159 // If the global is required to have a unique address, it can't be put
160 // into a mergable section: just drop it into the general read-only
161 // section instead.
162 if (!GVar->hasUnnamedAddr())
163 return SectionKind::getReadOnly();
164
Chris Lattnerf0144122009-07-28 03:13:23 +0000165 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000166 // section of the right width.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000167 if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
168 if (IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000169 dyn_cast<IntegerType>(ATy->getElementType())) {
170 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
171 ITy->getBitWidth() == 32) &&
172 IsNullTerminatedString(C)) {
173 if (ITy->getBitWidth() == 8)
174 return SectionKind::getMergeable1ByteCString();
175 if (ITy->getBitWidth() == 16)
176 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000177
Chris Lattner1850e5a2009-08-04 16:13:09 +0000178 assert(ITy->getBitWidth() == 32 && "Unknown width");
179 return SectionKind::getMergeable4ByteCString();
180 }
181 }
182 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000183
Chris Lattnerf0144122009-07-28 03:13:23 +0000184 // Otherwise, just drop it into a mergable constant section. If we have
185 // a section for this size, use it, otherwise use the arbitrary sized
186 // mergable section.
187 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000188 case 4: return SectionKind::getMergeableConst4();
189 case 8: return SectionKind::getMergeableConst8();
190 case 16: return SectionKind::getMergeableConst16();
191 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000192 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000193
Chris Lattnerf0144122009-07-28 03:13:23 +0000194 case Constant::LocalRelocation:
195 // In static relocation model, the linker will resolve all addresses, so
196 // the relocation entries will actually be constants by the time the app
197 // starts up. However, we can't put this into a mergable section, because
198 // the linker doesn't take relocations into consideration when it tries to
199 // merge entries in the section.
200 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000201 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000202
Chris Lattnerf0144122009-07-28 03:13:23 +0000203 // Otherwise, the dynamic linker needs to fix it up, put it in the
204 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000205 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000206
Chris Lattnerf0144122009-07-28 03:13:23 +0000207 case Constant::GlobalRelocations:
208 // In static relocation model, the linker will resolve all addresses, so
209 // the relocation entries will actually be constants by the time the app
210 // starts up. However, we can't put this into a mergable section, because
211 // the linker doesn't take relocations into consideration when it tries to
212 // merge entries in the section.
213 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000214 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000215
Chris Lattnerf0144122009-07-28 03:13:23 +0000216 // Otherwise, the dynamic linker needs to fix it up, put it in the
217 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000218 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000219 }
220 }
221
222 // Okay, this isn't a constant. If the initializer for the global is going
223 // to require a runtime relocation by the dynamic linker, put it into a more
224 // specific section to improve startup time of the app. This coalesces these
225 // globals together onto fewer pages, improving the locality of the dynamic
226 // linker.
227 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000228 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000229
230 switch (C->getRelocationInfo()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000231 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000232 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000233 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000234 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000235 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000236 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000237 }
Chandler Carruth732f05c2012-01-10 18:08:01 +0000238 llvm_unreachable("Invalid relocation");
Chris Lattnerf0144122009-07-28 03:13:23 +0000239}
240
241/// SectionForGlobal - This method computes the appropriate section to emit
242/// the specified global variable or function definition. This should not
243/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000244const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000245SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000246 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000247 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000248 if (GV->hasSection())
249 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000250
251
Chris Lattnerf0144122009-07-28 03:13:23 +0000252 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000253 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000254}
255
Chris Lattner58bed8f2009-08-05 04:25:40 +0000256
Chris Lattnerf0144122009-07-28 03:13:23 +0000257// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000258const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000259TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000260 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000261 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000262 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000263 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000264
Chris Lattnerf9650c02009-08-01 21:46:23 +0000265 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000266 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000267
Chris Lattner82458382009-08-01 21:56:13 +0000268 if (Kind.isBSS() && BSSSection != 0)
269 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000270
Chris Lattnerf9650c02009-08-01 21:46:23 +0000271 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000272 return ReadOnlySection;
273
274 return getDataSection();
275}
276
Chris Lattner83d77fa2009-08-01 23:46:12 +0000277/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000278/// specified size and relocation information, return a section that it
279/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000280const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000281TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000282 if (Kind.isReadOnly() && ReadOnlySection != 0)
283 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000284
Chris Lattnerf0144122009-07-28 03:13:23 +0000285 return DataSection;
286}
287
Chris Lattner3192d142010-03-11 19:41:58 +0000288/// getExprForDwarfGlobalReference - Return an MCExpr to use for a
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000289/// reference to the specified global variable from exception
290/// handling information.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000291const MCExpr *TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000292getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
293 MachineModuleInfo *MMI, unsigned Encoding,
294 MCStreamer &Streamer) const {
Chris Lattner73ff5642010-03-12 18:55:20 +0000295 const MCSymbol *Sym = Mang->getSymbol(GV);
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000296 return getExprForDwarfReference(Sym, Encoding, Streamer);
Chris Lattner8c6ed052009-09-16 01:46:41 +0000297}
Chris Lattnerf0144122009-07-28 03:13:23 +0000298
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000299const MCExpr *TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000300getExprForDwarfReference(const MCSymbol *Sym, unsigned Encoding,
Chris Lattner42263e22010-03-11 21:55:20 +0000301 MCStreamer &Streamer) const {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000302 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
303
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000304 switch (Encoding & 0x70) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000305 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000306 report_fatal_error("We do not support this DWARF encoding yet!");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000307 case dwarf::DW_EH_PE_absptr:
308 // Do nothing special
Chris Lattner42263e22010-03-11 21:55:20 +0000309 return Res;
310 case dwarf::DW_EH_PE_pcrel: {
311 // Emit a label to the streamer for the current position. This gives us
312 // .-foo addressing.
Chris Lattner77e76942010-03-17 05:41:18 +0000313 MCSymbol *PCSym = getContext().CreateTempSymbol();
Chris Lattner42263e22010-03-11 21:55:20 +0000314 Streamer.EmitLabel(PCSym);
315 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
316 return MCBinaryExpr::CreateSub(Res, PC, getContext());
317 }
318 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000319}