blob: 2fb744f5bf4e2074506a44381657fb02db5762e9 [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;
44 InitMCObjectFileInfo(TM.getTargetTriple(), TM.getRelocationModel(), *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
50static bool isSuitableForBSS(const GlobalVariable *GV) {
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
75/// nul value and contains no other nuls in it.
76static bool IsNullTerminatedString(const Constant *C) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000077 ArrayType *ATy = cast<ArrayType>(C->getType());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000078
Chris Lattnerf0144122009-07-28 03:13:23 +000079 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000080 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
81 if (ATy->getNumElements() == 0) return false;
82
83 ConstantInt *Null =
84 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
Dan Gohmane368b462010-06-18 14:22:04 +000085 if (Null == 0 || !Null->isZero())
Chris Lattner1850e5a2009-08-04 16:13:09 +000086 return false; // Not null terminated.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000087
Chris Lattner1850e5a2009-08-04 16:13:09 +000088 // Verify that the null doesn't occur anywhere else in the string.
89 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
90 // Reject constantexpr elements etc.
91 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
92 CVA->getOperand(i) == Null)
93 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +000094 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +000095 }
Chris Lattnerf0144122009-07-28 03:13:23 +000096
97 // Another possibility: [1 x i8] zeroinitializer
98 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +000099 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000100
101 return false;
102}
103
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000104MCSymbol *TargetLoweringObjectFile::
Rafael Espindola60246a92011-04-27 23:17:57 +0000105getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
Rafael Espindola7afec9c2011-04-27 23:08:15 +0000106 MachineModuleInfo *MMI) const {
107 return Mang->getSymbol(GV);
Rafael Espindola30deafc2011-04-16 03:51:21 +0000108}
109
110void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
111 const TargetMachine &TM,
112 const MCSymbol *Sym) const {
Rafael Espindola30deafc2011-04-16 03:51:21 +0000113}
114
115
Chris Lattner58bed8f2009-08-05 04:25:40 +0000116/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000117/// a global variable. Given an global variable and information from TM, it
118/// classifies the global in a variety of ways that make various target
119/// implementations simpler. The target implementation is free to ignore this
120/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000121SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
122 const TargetMachine &TM){
123 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
124 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000125
Chris Lattnerf0144122009-07-28 03:13:23 +0000126 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000127
Chris Lattnerf0144122009-07-28 03:13:23 +0000128 // Early exit - functions should be always in text sections.
129 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
130 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000131 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000132
Chris Lattnerf0144122009-07-28 03:13:23 +0000133 // Handle thread-local data first.
134 if (GVar->isThreadLocal()) {
135 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000136 return SectionKind::getThreadBSS();
137 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000138 }
139
Chris Lattnera3839bc2010-01-19 02:48:26 +0000140 // Variables with common linkage always get classified as common.
141 if (GVar->hasCommonLinkage())
142 return SectionKind::getCommon();
143
Chris Lattnerf0144122009-07-28 03:13:23 +0000144 // Variable can be easily put to BSS section.
Chris Lattnerce8749e2010-01-19 04:15:51 +0000145 if (isSuitableForBSS(GVar)) {
146 if (GVar->hasLocalLinkage())
147 return SectionKind::getBSSLocal();
148 else if (GVar->hasExternalLinkage())
149 return SectionKind::getBSSExtern();
Chris Lattner27981192009-08-01 23:57:16 +0000150 return SectionKind::getBSS();
Chris Lattnerce8749e2010-01-19 04:15:51 +0000151 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000152
Jay Foad7d715df2011-06-19 18:37:11 +0000153 const Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000154
Chris Lattnerf0144122009-07-28 03:13:23 +0000155 // If the global is marked constant, we can put it into a mergable section,
156 // a mergable string section, or general .data if it contains relocations.
Chris Lattner32899192011-01-18 01:23:44 +0000157 if (GVar->isConstant()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000158 // If the initializer for the global contains something that requires a
159 // relocation, then we may have to drop this into a wriable data section
160 // even though it is marked const.
161 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000162 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000163 case Constant::NoRelocation:
Chris Lattner32899192011-01-18 01:23:44 +0000164 // If the global is required to have a unique address, it can't be put
165 // into a mergable section: just drop it into the general read-only
166 // section instead.
167 if (!GVar->hasUnnamedAddr())
168 return SectionKind::getReadOnly();
169
Chris Lattnerf0144122009-07-28 03:13:23 +0000170 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000171 // section of the right width.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000172 if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
173 if (IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000174 dyn_cast<IntegerType>(ATy->getElementType())) {
175 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
176 ITy->getBitWidth() == 32) &&
177 IsNullTerminatedString(C)) {
178 if (ITy->getBitWidth() == 8)
179 return SectionKind::getMergeable1ByteCString();
180 if (ITy->getBitWidth() == 16)
181 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000182
Chris Lattner1850e5a2009-08-04 16:13:09 +0000183 assert(ITy->getBitWidth() == 32 && "Unknown width");
184 return SectionKind::getMergeable4ByteCString();
185 }
186 }
187 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000188
Chris Lattnerf0144122009-07-28 03:13:23 +0000189 // Otherwise, just drop it into a mergable constant section. If we have
190 // a section for this size, use it, otherwise use the arbitrary sized
191 // mergable section.
192 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000193 case 4: return SectionKind::getMergeableConst4();
194 case 8: return SectionKind::getMergeableConst8();
195 case 16: return SectionKind::getMergeableConst16();
196 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000197 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000198
Chris Lattnerf0144122009-07-28 03:13:23 +0000199 case Constant::LocalRelocation:
200 // In static relocation model, the linker will resolve all addresses, so
201 // the relocation entries will actually be constants by the time the app
202 // starts up. However, we can't put this into a mergable section, because
203 // the linker doesn't take relocations into consideration when it tries to
204 // merge entries in the section.
205 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000206 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000207
Chris Lattnerf0144122009-07-28 03:13:23 +0000208 // Otherwise, the dynamic linker needs to fix it up, put it in the
209 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000210 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000211
Chris Lattnerf0144122009-07-28 03:13:23 +0000212 case Constant::GlobalRelocations:
213 // In static relocation model, the linker will resolve all addresses, so
214 // the relocation entries will actually be constants by the time the app
215 // starts up. However, we can't put this into a mergable section, because
216 // the linker doesn't take relocations into consideration when it tries to
217 // merge entries in the section.
218 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000219 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000220
Chris Lattnerf0144122009-07-28 03:13:23 +0000221 // Otherwise, the dynamic linker needs to fix it up, put it in the
222 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000223 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000224 }
225 }
226
227 // Okay, this isn't a constant. If the initializer for the global is going
228 // to require a runtime relocation by the dynamic linker, put it into a more
229 // specific section to improve startup time of the app. This coalesces these
230 // globals together onto fewer pages, improving the locality of the dynamic
231 // linker.
232 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000233 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000234
235 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000236 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000237 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000238 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000239 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000240 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000241 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000242 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000243 }
244}
245
246/// SectionForGlobal - This method computes the appropriate section to emit
247/// the specified global variable or function definition. This should not
248/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000249const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000250SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000251 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000252 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000253 if (GV->hasSection())
254 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000255
256
Chris Lattnerf0144122009-07-28 03:13:23 +0000257 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000258 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000259}
260
Chris Lattner58bed8f2009-08-05 04:25:40 +0000261
Chris Lattnerf0144122009-07-28 03:13:23 +0000262// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000263const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000264TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000265 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000266 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000267 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000268 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000269
Chris Lattnerf9650c02009-08-01 21:46:23 +0000270 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000271 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000272
Chris Lattner82458382009-08-01 21:56:13 +0000273 if (Kind.isBSS() && BSSSection != 0)
274 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000275
Chris Lattnerf9650c02009-08-01 21:46:23 +0000276 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000277 return ReadOnlySection;
278
279 return getDataSection();
280}
281
Chris Lattner83d77fa2009-08-01 23:46:12 +0000282/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000283/// specified size and relocation information, return a section that it
284/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000285const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000286TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000287 if (Kind.isReadOnly() && ReadOnlySection != 0)
288 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000289
Chris Lattnerf0144122009-07-28 03:13:23 +0000290 return DataSection;
291}
292
Chris Lattner3192d142010-03-11 19:41:58 +0000293/// getExprForDwarfGlobalReference - Return an MCExpr to use for a
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000294/// reference to the specified global variable from exception
295/// handling information.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000296const MCExpr *TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000297getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
298 MachineModuleInfo *MMI, unsigned Encoding,
299 MCStreamer &Streamer) const {
Chris Lattner73ff5642010-03-12 18:55:20 +0000300 const MCSymbol *Sym = Mang->getSymbol(GV);
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000301 return getExprForDwarfReference(Sym, Encoding, Streamer);
Chris Lattner8c6ed052009-09-16 01:46:41 +0000302}
Chris Lattnerf0144122009-07-28 03:13:23 +0000303
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000304const MCExpr *TargetLoweringObjectFile::
Rafael Espindola4788c3e2011-04-20 03:08:09 +0000305getExprForDwarfReference(const MCSymbol *Sym, unsigned Encoding,
Chris Lattner42263e22010-03-11 21:55:20 +0000306 MCStreamer &Streamer) const {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000307 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
308
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000309 switch (Encoding & 0x70) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000310 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000311 report_fatal_error("We do not support this DWARF encoding yet!");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000312 case dwarf::DW_EH_PE_absptr:
313 // Do nothing special
Chris Lattner42263e22010-03-11 21:55:20 +0000314 return Res;
315 case dwarf::DW_EH_PE_pcrel: {
316 // Emit a label to the streamer for the current position. This gives us
317 // .-foo addressing.
Chris Lattner77e76942010-03-17 05:41:18 +0000318 MCSymbol *PCSym = getContext().CreateTempSymbol();
Chris Lattner42263e22010-03-11 21:55:20 +0000319 Streamer.EmitLabel(PCSym);
320 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
321 return MCBinaryExpr::CreateSub(Res, PC, getContext());
322 }
323 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000324}
325
326unsigned TargetLoweringObjectFile::getPersonalityEncoding() const {
327 return dwarf::DW_EH_PE_absptr;
328}
329
330unsigned TargetLoweringObjectFile::getLSDAEncoding() const {
331 return dwarf::DW_EH_PE_absptr;
332}
333
Rafael Espindola5426a9e2011-05-01 04:49:54 +0000334unsigned TargetLoweringObjectFile::getFDEEncoding(bool CFI) const {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000335 return dwarf::DW_EH_PE_absptr;
336}
337
338unsigned TargetLoweringObjectFile::getTTypeEncoding() const {
339 return dwarf::DW_EH_PE_absptr;
340}
Chris Lattnerf0144122009-07-28 03:13:23 +0000341