blob: e0e6b6b0245f2a883c31126d1b296adbc3fc78c0 [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
Chris Lattnera87dea42009-07-31 18:48:30 +000038TargetLoweringObjectFile::TargetLoweringObjectFile() : Ctx(0) {
Chris Lattnerf0144122009-07-28 03:13:23 +000039 TextSection = 0;
40 DataSection = 0;
Chris Lattner82458382009-08-01 21:56:13 +000041 BSSSection = 0;
Chris Lattnerf0144122009-07-28 03:13:23 +000042 ReadOnlySection = 0;
Chris Lattner80ec2792009-08-02 00:34:36 +000043 StaticCtorSection = 0;
44 StaticDtorSection = 0;
Chris Lattnerd5bbb072009-08-02 01:34:32 +000045 LSDASection = 0;
Chris Lattner18a4c162009-08-02 07:24:22 +000046
Chris Lattner8048ebe2010-09-27 06:44:54 +000047 CommDirectiveSupportsAlignment = true;
Chris Lattner18a4c162009-08-02 07:24:22 +000048 DwarfAbbrevSection = 0;
49 DwarfInfoSection = 0;
50 DwarfLineSection = 0;
51 DwarfFrameSection = 0;
52 DwarfPubNamesSection = 0;
53 DwarfPubTypesSection = 0;
54 DwarfDebugInlineSection = 0;
55 DwarfStrSection = 0;
56 DwarfLocSection = 0;
57 DwarfARangesSection = 0;
58 DwarfRangesSection = 0;
59 DwarfMacroInfoSection = 0;
Chris Lattner09d53fe2010-03-10 07:20:42 +000060
61 IsFunctionEHSymbolGlobal = false;
62 IsFunctionEHFrameSymbolPrivate = true;
63 SupportsWeakOmittedEHFrame = true;
Chris Lattnerf0144122009-07-28 03:13:23 +000064}
65
66TargetLoweringObjectFile::~TargetLoweringObjectFile() {
67}
68
69static bool isSuitableForBSS(const GlobalVariable *GV) {
70 Constant *C = GV->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000071
Chris Lattnerf0144122009-07-28 03:13:23 +000072 // Must have zero initializer.
73 if (!C->isNullValue())
74 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000075
Chris Lattnerf0144122009-07-28 03:13:23 +000076 // Leave constant zeros in readonly constant sections, so they can be shared.
77 if (GV->isConstant())
78 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000079
Chris Lattnerf0144122009-07-28 03:13:23 +000080 // If the global has an explicit section specified, don't put it in BSS.
81 if (!GV->getSection().empty())
82 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000083
Chris Lattnerf0144122009-07-28 03:13:23 +000084 // If -nozero-initialized-in-bss is specified, don't ever use BSS.
85 if (NoZerosInBSS)
86 return false;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000087
Chris Lattnerf0144122009-07-28 03:13:23 +000088 // Otherwise, put it in BSS!
89 return true;
90}
91
Chris Lattner1850e5a2009-08-04 16:13:09 +000092/// IsNullTerminatedString - Return true if the specified constant (which is
93/// known to have a type that is an array of 1/2/4 byte elements) ends with a
94/// nul value and contains no other nuls in it.
95static bool IsNullTerminatedString(const Constant *C) {
96 const ArrayType *ATy = cast<ArrayType>(C->getType());
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +000097
Chris Lattnerf0144122009-07-28 03:13:23 +000098 // First check: is we have constant array of i8 terminated with zero
Chris Lattner1850e5a2009-08-04 16:13:09 +000099 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(C)) {
100 if (ATy->getNumElements() == 0) return false;
101
102 ConstantInt *Null =
103 dyn_cast<ConstantInt>(CVA->getOperand(ATy->getNumElements()-1));
Dan Gohmane368b462010-06-18 14:22:04 +0000104 if (Null == 0 || !Null->isZero())
Chris Lattner1850e5a2009-08-04 16:13:09 +0000105 return false; // Not null terminated.
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000106
Chris Lattner1850e5a2009-08-04 16:13:09 +0000107 // Verify that the null doesn't occur anywhere else in the string.
108 for (unsigned i = 0, e = ATy->getNumElements()-1; i != e; ++i)
109 // Reject constantexpr elements etc.
110 if (!isa<ConstantInt>(CVA->getOperand(i)) ||
111 CVA->getOperand(i) == Null)
112 return false;
Chris Lattnerf0144122009-07-28 03:13:23 +0000113 return true;
Chris Lattner1850e5a2009-08-04 16:13:09 +0000114 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000115
116 // Another possibility: [1 x i8] zeroinitializer
117 if (isa<ConstantAggregateZero>(C))
Chris Lattner1850e5a2009-08-04 16:13:09 +0000118 return ATy->getNumElements() == 1;
Chris Lattnerf0144122009-07-28 03:13:23 +0000119
120 return false;
121}
122
Rafael Espindola30deafc2011-04-16 03:51:21 +0000123MCSymbol *
124TargetLoweringObjectFile::getPersonalityPICSymbol(StringRef Name) const {
125 assert(0 && "Not Available in this format.");
Francois Pichet61446862011-04-16 13:59:23 +0000126 return 0;
Rafael Espindola30deafc2011-04-16 03:51:21 +0000127}
128
129void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
130 const TargetMachine &TM,
131 const MCSymbol *Sym) const {
132 assert(0 && "Not Available in this format.");
133}
134
135
Chris Lattner58bed8f2009-08-05 04:25:40 +0000136/// getKindForGlobal - This is a top-level target-independent classifier for
Chris Lattner968ff112009-08-01 21:11:14 +0000137/// a global variable. Given an global variable and information from TM, it
138/// classifies the global in a variety of ways that make various target
139/// implementations simpler. The target implementation is free to ignore this
140/// extra info of course.
Chris Lattner58bed8f2009-08-05 04:25:40 +0000141SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV,
142 const TargetMachine &TM){
143 assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() &&
144 "Can only be used for global definitions");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000145
Chris Lattnerf0144122009-07-28 03:13:23 +0000146 Reloc::Model ReloModel = TM.getRelocationModel();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000147
Chris Lattnerf0144122009-07-28 03:13:23 +0000148 // Early exit - functions should be always in text sections.
149 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
150 if (GVar == 0)
Chris Lattner27981192009-08-01 23:57:16 +0000151 return SectionKind::getText();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000152
Chris Lattnerf0144122009-07-28 03:13:23 +0000153 // Handle thread-local data first.
154 if (GVar->isThreadLocal()) {
155 if (isSuitableForBSS(GVar))
Chris Lattner27981192009-08-01 23:57:16 +0000156 return SectionKind::getThreadBSS();
157 return SectionKind::getThreadData();
Chris Lattnerf0144122009-07-28 03:13:23 +0000158 }
159
Chris Lattnera3839bc2010-01-19 02:48:26 +0000160 // Variables with common linkage always get classified as common.
161 if (GVar->hasCommonLinkage())
162 return SectionKind::getCommon();
163
Chris Lattnerf0144122009-07-28 03:13:23 +0000164 // Variable can be easily put to BSS section.
Chris Lattnerce8749e2010-01-19 04:15:51 +0000165 if (isSuitableForBSS(GVar)) {
166 if (GVar->hasLocalLinkage())
167 return SectionKind::getBSSLocal();
168 else if (GVar->hasExternalLinkage())
169 return SectionKind::getBSSExtern();
Chris Lattner27981192009-08-01 23:57:16 +0000170 return SectionKind::getBSS();
Chris Lattnerce8749e2010-01-19 04:15:51 +0000171 }
Chris Lattnerf0144122009-07-28 03:13:23 +0000172
173 Constant *C = GVar->getInitializer();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000174
Chris Lattnerf0144122009-07-28 03:13:23 +0000175 // If the global is marked constant, we can put it into a mergable section,
176 // a mergable string section, or general .data if it contains relocations.
Chris Lattner32899192011-01-18 01:23:44 +0000177 if (GVar->isConstant()) {
Chris Lattnerf0144122009-07-28 03:13:23 +0000178 // If the initializer for the global contains something that requires a
179 // relocation, then we may have to drop this into a wriable data section
180 // even though it is marked const.
181 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000182 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000183 case Constant::NoRelocation:
Chris Lattner32899192011-01-18 01:23:44 +0000184 // If the global is required to have a unique address, it can't be put
185 // into a mergable section: just drop it into the general read-only
186 // section instead.
187 if (!GVar->hasUnnamedAddr())
188 return SectionKind::getReadOnly();
189
Chris Lattnerf0144122009-07-28 03:13:23 +0000190 // If initializer is a null-terminated string, put it in a "cstring"
Chris Lattner1850e5a2009-08-04 16:13:09 +0000191 // section of the right width.
192 if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000193 if (const IntegerType *ITy =
Chris Lattner1850e5a2009-08-04 16:13:09 +0000194 dyn_cast<IntegerType>(ATy->getElementType())) {
195 if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
196 ITy->getBitWidth() == 32) &&
197 IsNullTerminatedString(C)) {
198 if (ITy->getBitWidth() == 8)
199 return SectionKind::getMergeable1ByteCString();
200 if (ITy->getBitWidth() == 16)
201 return SectionKind::getMergeable2ByteCString();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000202
Chris Lattner1850e5a2009-08-04 16:13:09 +0000203 assert(ITy->getBitWidth() == 32 && "Unknown width");
204 return SectionKind::getMergeable4ByteCString();
205 }
206 }
207 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000208
Chris Lattnerf0144122009-07-28 03:13:23 +0000209 // Otherwise, just drop it into a mergable constant section. If we have
210 // a section for this size, use it, otherwise use the arbitrary sized
211 // mergable section.
212 switch (TM.getTargetData()->getTypeAllocSize(C->getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000213 case 4: return SectionKind::getMergeableConst4();
214 case 8: return SectionKind::getMergeableConst8();
215 case 16: return SectionKind::getMergeableConst16();
216 default: return SectionKind::getMergeableConst();
Chris Lattnerf0144122009-07-28 03:13:23 +0000217 }
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000218
Chris Lattnerf0144122009-07-28 03:13:23 +0000219 case Constant::LocalRelocation:
220 // In static relocation model, the linker will resolve all addresses, so
221 // the relocation entries will actually be constants by the time the app
222 // starts up. However, we can't put this into a mergable section, because
223 // the linker doesn't take relocations into consideration when it tries to
224 // merge entries in the section.
225 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000226 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000227
Chris Lattnerf0144122009-07-28 03:13:23 +0000228 // Otherwise, the dynamic linker needs to fix it up, put it in the
229 // writable data.rel.local section.
Chris Lattner27981192009-08-01 23:57:16 +0000230 return SectionKind::getReadOnlyWithRelLocal();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000231
Chris Lattnerf0144122009-07-28 03:13:23 +0000232 case Constant::GlobalRelocations:
233 // In static relocation model, the linker will resolve all addresses, so
234 // the relocation entries will actually be constants by the time the app
235 // starts up. However, we can't put this into a mergable section, because
236 // the linker doesn't take relocations into consideration when it tries to
237 // merge entries in the section.
238 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000239 return SectionKind::getReadOnly();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000240
Chris Lattnerf0144122009-07-28 03:13:23 +0000241 // Otherwise, the dynamic linker needs to fix it up, put it in the
242 // writable data.rel section.
Chris Lattner27981192009-08-01 23:57:16 +0000243 return SectionKind::getReadOnlyWithRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000244 }
245 }
246
247 // Okay, this isn't a constant. If the initializer for the global is going
248 // to require a runtime relocation by the dynamic linker, put it into a more
249 // specific section to improve startup time of the app. This coalesces these
250 // globals together onto fewer pages, improving the locality of the dynamic
251 // linker.
252 if (ReloModel == Reloc::Static)
Chris Lattner27981192009-08-01 23:57:16 +0000253 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000254
255 switch (C->getRelocationInfo()) {
Chris Lattner8f9b0f62009-11-07 09:20:54 +0000256 default: assert(0 && "unknown relocation info kind");
Chris Lattnerf0144122009-07-28 03:13:23 +0000257 case Constant::NoRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000258 return SectionKind::getDataNoRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000259 case Constant::LocalRelocation:
Chris Lattner27981192009-08-01 23:57:16 +0000260 return SectionKind::getDataRelLocal();
Chris Lattnerf0144122009-07-28 03:13:23 +0000261 case Constant::GlobalRelocations:
Chris Lattner27981192009-08-01 23:57:16 +0000262 return SectionKind::getDataRel();
Chris Lattnerf0144122009-07-28 03:13:23 +0000263 }
264}
265
266/// SectionForGlobal - This method computes the appropriate section to emit
267/// the specified global variable or function definition. This should not
268/// be passed external (or available externally) globals.
Chris Lattnera87dea42009-07-31 18:48:30 +0000269const MCSection *TargetLoweringObjectFile::
Chris Lattner58bed8f2009-08-05 04:25:40 +0000270SectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler *Mang,
Chris Lattnere53a6002009-07-29 05:09:30 +0000271 const TargetMachine &TM) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000272 // Select section name.
Chris Lattner24f654c2009-08-06 16:39:58 +0000273 if (GV->hasSection())
274 return getExplicitSectionGlobal(GV, Kind, Mang, TM);
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000275
276
Chris Lattnerf0144122009-07-28 03:13:23 +0000277 // Use default section depending on the 'type' of global
Chris Lattnerf9650c02009-08-01 21:46:23 +0000278 return SelectSectionForGlobal(GV, Kind, Mang, TM);
Chris Lattnerf0144122009-07-28 03:13:23 +0000279}
280
Chris Lattner58bed8f2009-08-05 04:25:40 +0000281
Chris Lattnerf0144122009-07-28 03:13:23 +0000282// Lame default implementation. Calculate the section name for global.
Chris Lattnera87dea42009-07-31 18:48:30 +0000283const MCSection *
Chris Lattnerf0144122009-07-28 03:13:23 +0000284TargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV,
Chris Lattnerf9650c02009-08-01 21:46:23 +0000285 SectionKind Kind,
Chris Lattnere53a6002009-07-29 05:09:30 +0000286 Mangler *Mang,
Chris Lattnerf0144122009-07-28 03:13:23 +0000287 const TargetMachine &TM) const{
Chris Lattnerf9650c02009-08-01 21:46:23 +0000288 assert(!Kind.isThreadLocal() && "Doesn't support TLS");
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000289
Chris Lattnerf9650c02009-08-01 21:46:23 +0000290 if (Kind.isText())
Chris Lattnerf0144122009-07-28 03:13:23 +0000291 return getTextSection();
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000292
Chris Lattner82458382009-08-01 21:56:13 +0000293 if (Kind.isBSS() && BSSSection != 0)
294 return BSSSection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000295
Chris Lattnerf9650c02009-08-01 21:46:23 +0000296 if (Kind.isReadOnly() && ReadOnlySection != 0)
Chris Lattnerf0144122009-07-28 03:13:23 +0000297 return ReadOnlySection;
298
299 return getDataSection();
300}
301
Chris Lattner83d77fa2009-08-01 23:46:12 +0000302/// getSectionForConstant - Given a mergable constant with the
Chris Lattnerf0144122009-07-28 03:13:23 +0000303/// specified size and relocation information, return a section that it
304/// should be placed in.
Chris Lattnera87dea42009-07-31 18:48:30 +0000305const MCSection *
Chris Lattner83d77fa2009-08-01 23:46:12 +0000306TargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const {
Chris Lattnerf0144122009-07-28 03:13:23 +0000307 if (Kind.isReadOnly() && ReadOnlySection != 0)
308 return ReadOnlySection;
Anton Korobeynikov8ddb5692009-09-09 08:41:20 +0000309
Chris Lattnerf0144122009-07-28 03:13:23 +0000310 return DataSection;
311}
312
Chris Lattner3192d142010-03-11 19:41:58 +0000313/// getExprForDwarfGlobalReference - Return an MCExpr to use for a
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000314/// reference to the specified global variable from exception
315/// handling information.
Chris Lattner8c6ed052009-09-16 01:46:41 +0000316const MCExpr *TargetLoweringObjectFile::
Chris Lattner3192d142010-03-11 19:41:58 +0000317getExprForDwarfGlobalReference(const GlobalValue *GV, Mangler *Mang,
318 MachineModuleInfo *MMI, unsigned Encoding,
319 MCStreamer &Streamer) const {
Chris Lattner73ff5642010-03-12 18:55:20 +0000320 const MCSymbol *Sym = Mang->getSymbol(GV);
Chris Lattner42263e22010-03-11 21:55:20 +0000321 return getExprForDwarfReference(Sym, Mang, MMI, Encoding, Streamer);
Chris Lattner8c6ed052009-09-16 01:46:41 +0000322}
Chris Lattnerf0144122009-07-28 03:13:23 +0000323
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000324const MCExpr *TargetLoweringObjectFile::
Chris Lattner42263e22010-03-11 21:55:20 +0000325getExprForDwarfReference(const MCSymbol *Sym, Mangler *Mang,
326 MachineModuleInfo *MMI, unsigned Encoding,
327 MCStreamer &Streamer) const {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000328 const MCExpr *Res = MCSymbolRefExpr::Create(Sym, getContext());
329
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000330 switch (Encoding & 0x70) {
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000331 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000332 report_fatal_error("We do not support this DWARF encoding yet!");
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000333 case dwarf::DW_EH_PE_absptr:
334 // Do nothing special
Chris Lattner42263e22010-03-11 21:55:20 +0000335 return Res;
336 case dwarf::DW_EH_PE_pcrel: {
337 // Emit a label to the streamer for the current position. This gives us
338 // .-foo addressing.
Chris Lattner77e76942010-03-17 05:41:18 +0000339 MCSymbol *PCSym = getContext().CreateTempSymbol();
Chris Lattner42263e22010-03-11 21:55:20 +0000340 Streamer.EmitLabel(PCSym);
341 const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext());
342 return MCBinaryExpr::CreateSub(Res, PC, getContext());
343 }
344 }
Anton Korobeynikov9184b252010-02-15 22:35:59 +0000345}
346
347unsigned TargetLoweringObjectFile::getPersonalityEncoding() const {
348 return dwarf::DW_EH_PE_absptr;
349}
350
351unsigned TargetLoweringObjectFile::getLSDAEncoding() const {
352 return dwarf::DW_EH_PE_absptr;
353}
354
355unsigned TargetLoweringObjectFile::getFDEEncoding() const {
356 return dwarf::DW_EH_PE_absptr;
357}
358
359unsigned TargetLoweringObjectFile::getTTypeEncoding() const {
360 return dwarf::DW_EH_PE_absptr;
361}
Chris Lattnerf0144122009-07-28 03:13:23 +0000362