1f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===// 2f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// 3f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// The LLVM Compiler Infrastructure 4f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// 5f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// This file is distributed under the University of Illinois Open Source 6f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// License. See LICENSE.TXT for details. 7f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// 8f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===// 9f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// 10f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// This file implements classes used to handle lowerings specific to common 11f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// object file formats. 12f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// 13f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===// 14f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 15f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner#include "llvm/Target/TargetLoweringObjectFile.h" 160b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h" 170b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h" 180b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h" 190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h" 200b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/GlobalVariable.h" 2136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/Mangler.h" 2236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/MC/MCAsmInfo.h" 23a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattner#include "llvm/MC/MCContext.h" 248c6ed05157e9c97ff8f3ccb211dd797e53228da1Chris Lattner#include "llvm/MC/MCExpr.h" 2542263e2e407ab7d1d805e7b41cffd7217134d3b6Chris Lattner#include "llvm/MC/MCStreamer.h" 268da8d4b12a7e36e219894c256f545ddea66a9c49Chris Lattner#include "llvm/MC/MCSymbol.h" 279184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov#include "llvm/Support/Dwarf.h" 288f9b0f6e881a63875e7c41319eca31751588799aChris Lattner#include "llvm/Support/ErrorHandling.h" 298da8d4b12a7e36e219894c256f545ddea66a9c49Chris Lattner#include "llvm/Support/raw_ostream.h" 3036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/Target/TargetLowering.h" 31d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetMachine.h" 32d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Target/TargetOptions.h" 33f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattnerusing namespace llvm; 34f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 35f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===// 36f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// Generic Code 37f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner//===----------------------------------------------------------------------===// 38f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 39e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng/// Initialize - this method must be called before any actual lowering is 40e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng/// done. This specifies the current context for codegen, and gives the 41e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng/// lowering implementations a chance to set up their default sections. 42e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Chengvoid TargetLoweringObjectFile::Initialize(MCContext &ctx, 43e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng const TargetMachine &TM) { 44e76a33b9567d78a5744dc52fcec3a6056d6fb576Evan Cheng Ctx = &ctx; 4536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines DL = TM.getDataLayout(); 46203576aa0cb9d8bf2d2e4d910ebab4b7a63262aeEvan Cheng InitMCObjectFileInfo(TM.getTargetTriple(), 47203576aa0cb9d8bf2d2e4d910ebab4b7a63262aeEvan Cheng TM.getRelocationModel(), TM.getCodeModel(), *Ctx); 48f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner} 49dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 50f0144127b98425d214e59e4a1a4b342b78e3642bChris LattnerTargetLoweringObjectFile::~TargetLoweringObjectFile() { 51f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner} 52f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 538a8d479214745c82ef00f08d4e4f1c173b5f9ce2Nick Lewyckystatic bool isSuitableForBSS(const GlobalVariable *GV, bool NoZerosInBSS) { 547d715dfe6d66be257926f626df96a0e2bd38dc1fJay Foad const Constant *C = GV->getInitializer(); 558ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 56f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Must have zero initializer. 57f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner if (!C->isNullValue()) 58f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return false; 598ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 60f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Leave constant zeros in readonly constant sections, so they can be shared. 61f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner if (GV->isConstant()) 62f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return false; 638ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 64f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // If the global has an explicit section specified, don't put it in BSS. 65dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (GV->hasSection()) 66f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return false; 678ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 68f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // If -nozero-initialized-in-bss is specified, don't ever use BSS. 69f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner if (NoZerosInBSS) 70f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return false; 718ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 72f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Otherwise, put it in BSS! 73f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return true; 74f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner} 75f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 761850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner/// IsNullTerminatedString - Return true if the specified constant (which is 771850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner/// known to have a type that is an array of 1/2/4 byte elements) ends with a 7829cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner/// nul value and contains no other nuls in it. Note that this is more general 7929cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner/// than ConstantDataSequential::isString because we allow 2 & 4 byte strings. 801850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattnerstatic bool IsNullTerminatedString(const Constant *C) { 8129cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner // First check: is we have constant array terminated with zero 8229cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) { 8329cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner unsigned NumElts = CDS->getNumElements(); 8429cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner assert(NumElts != 0 && "Can't have an empty CDS"); 8529cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner 8629cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner if (CDS->getElementAsInteger(NumElts-1) != 0) 8729cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner return false; // Not null terminated. 8829cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner 8929cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner // Verify that the null doesn't occur anywhere else in the string. 9029cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner for (unsigned i = 0; i != NumElts-1; ++i) 9129cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner if (CDS->getElementAsInteger(i) == 0) 9229cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner return false; 9329cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner return true; 9429cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner } 95f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 96f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Another possibility: [1 x i8] zeroinitializer 97f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner if (isa<ConstantAggregateZero>(C)) 9829cc6cb4d1aa22f0a27edf4e5b363071a83a65d8Chris Lattner return cast<ArrayType>(C->getType())->getNumElements() == 1; 99f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 100f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return false; 101f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner} 102f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 10336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesMCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase( 10436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const GlobalValue *GV, StringRef Suffix, Mangler &Mang, 10536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const TargetMachine &TM) const { 10636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines assert(!Suffix.empty()); 10736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 10893cf0939f95b3d580d9c05375a7c84164e1ba72eRafael Espindola SmallString<60> NameStr; 10936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines NameStr += DL->getPrivateGlobalPrefix(); 11036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines TM.getNameWithPrefix(NameStr, GV, Mang); 11136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines NameStr.append(Suffix.begin(), Suffix.end()); 11293cf0939f95b3d580d9c05375a7c84164e1ba72eRafael Espindola return Ctx->GetOrCreateSymbol(NameStr.str()); 11393cf0939f95b3d580d9c05375a7c84164e1ba72eRafael Espindola} 11493cf0939f95b3d580d9c05375a7c84164e1ba72eRafael Espindola 11536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesMCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol( 11636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, 11736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines MachineModuleInfo *MMI) const { 11836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return TM.getSymbol(GV, Mang); 11930deafc84adf88f643cdc39dc97a37537155347fRafael Espindola} 12030deafc84adf88f643cdc39dc97a37537155347fRafael Espindola 12130deafc84adf88f643cdc39dc97a37537155347fRafael Espindolavoid TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer, 12230deafc84adf88f643cdc39dc97a37537155347fRafael Espindola const TargetMachine &TM, 12330deafc84adf88f643cdc39dc97a37537155347fRafael Espindola const MCSymbol *Sym) const { 12430deafc84adf88f643cdc39dc97a37537155347fRafael Espindola} 12530deafc84adf88f643cdc39dc97a37537155347fRafael Espindola 12630deafc84adf88f643cdc39dc97a37537155347fRafael Espindola 12758bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner/// getKindForGlobal - This is a top-level target-independent classifier for 128968ff1196768c0b6dbcc5508025a2923bfa73fabChris Lattner/// a global variable. Given an global variable and information from TM, it 129968ff1196768c0b6dbcc5508025a2923bfa73fabChris Lattner/// classifies the global in a variety of ways that make various target 130968ff1196768c0b6dbcc5508025a2923bfa73fabChris Lattner/// implementations simpler. The target implementation is free to ignore this 131968ff1196768c0b6dbcc5508025a2923bfa73fabChris Lattner/// extra info of course. 13258bed8fc29b6e55e7014dcb537808043c946cd73Chris LattnerSectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalValue *GV, 13358bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner const TargetMachine &TM){ 13458bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner assert(!GV->isDeclaration() && !GV->hasAvailableExternallyLinkage() && 13558bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner "Can only be used for global definitions"); 1368ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 137f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner Reloc::Model ReloModel = TM.getRelocationModel(); 1388ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 139f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Early exit - functions should be always in text sections. 140f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); 141dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (!GVar) 1422798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getText(); 1438ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 144f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Handle thread-local data first. 145f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner if (GVar->isThreadLocal()) { 1468a8d479214745c82ef00f08d4e4f1c173b5f9ce2Nick Lewycky if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) 1472798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getThreadBSS(); 1482798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getThreadData(); 149f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner } 150f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 151a3839bc3714e6a84222f45cf4c0f1a20a88b10cdChris Lattner // Variables with common linkage always get classified as common. 152a3839bc3714e6a84222f45cf4c0f1a20a88b10cdChris Lattner if (GVar->hasCommonLinkage()) 153a3839bc3714e6a84222f45cf4c0f1a20a88b10cdChris Lattner return SectionKind::getCommon(); 154a3839bc3714e6a84222f45cf4c0f1a20a88b10cdChris Lattner 155f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Variable can be easily put to BSS section. 1568a8d479214745c82ef00f08d4e4f1c173b5f9ce2Nick Lewycky if (isSuitableForBSS(GVar, TM.Options.NoZerosInBSS)) { 157ce8749e445fdd0493758932874bad50293647df9Chris Lattner if (GVar->hasLocalLinkage()) 158ce8749e445fdd0493758932874bad50293647df9Chris Lattner return SectionKind::getBSSLocal(); 159ce8749e445fdd0493758932874bad50293647df9Chris Lattner else if (GVar->hasExternalLinkage()) 160ce8749e445fdd0493758932874bad50293647df9Chris Lattner return SectionKind::getBSSExtern(); 1612798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getBSS(); 162ce8749e445fdd0493758932874bad50293647df9Chris Lattner } 163f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 1647d715dfe6d66be257926f626df96a0e2bd38dc1fJay Foad const Constant *C = GVar->getInitializer(); 1658ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 166f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // If the global is marked constant, we can put it into a mergable section, 167f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // a mergable string section, or general .data if it contains relocations. 16832899199c043269b4dfab7f0429cc946e67bd54dChris Lattner if (GVar->isConstant()) { 169f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // If the initializer for the global contains something that requires a 170f2eed387d48e36f12a9945c6b71b20bdc7f46e3aEric Christopher // relocation, then we may have to drop this into a writable data section 171f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // even though it is marked const. 172f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner switch (C->getRelocationInfo()) { 173f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner case Constant::NoRelocation: 17432899199c043269b4dfab7f0429cc946e67bd54dChris Lattner // If the global is required to have a unique address, it can't be put 17532899199c043269b4dfab7f0429cc946e67bd54dChris Lattner // into a mergable section: just drop it into the general read-only 17632899199c043269b4dfab7f0429cc946e67bd54dChris Lattner // section instead. 17732899199c043269b4dfab7f0429cc946e67bd54dChris Lattner if (!GVar->hasUnnamedAddr()) 17832899199c043269b4dfab7f0429cc946e67bd54dChris Lattner return SectionKind::getReadOnly(); 17932899199c043269b4dfab7f0429cc946e67bd54dChris Lattner 180f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // If initializer is a null-terminated string, put it in a "cstring" 1811850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner // section of the right width. 182db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { 183db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (IntegerType *ITy = 1841850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner dyn_cast<IntegerType>(ATy->getElementType())) { 1851850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 || 1861850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner ITy->getBitWidth() == 32) && 1871850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner IsNullTerminatedString(C)) { 1881850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner if (ITy->getBitWidth() == 8) 1891850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner return SectionKind::getMergeable1ByteCString(); 1901850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner if (ITy->getBitWidth() == 16) 1911850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner return SectionKind::getMergeable2ByteCString(); 1928ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 1931850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner assert(ITy->getBitWidth() == 32 && "Unknown width"); 1941850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner return SectionKind::getMergeable4ByteCString(); 1951850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner } 1961850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner } 1971850e5add1516e945bbabf7e456c305f4bd5cc6fChris Lattner } 1988ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 199f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Otherwise, just drop it into a mergable constant section. If we have 200f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // a section for this size, use it, otherwise use the arbitrary sized 201f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // mergable section. 2023574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow switch (TM.getDataLayout()->getTypeAllocSize(C->getType())) { 2032798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner case 4: return SectionKind::getMergeableConst4(); 2042798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner case 8: return SectionKind::getMergeableConst8(); 2052798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner case 16: return SectionKind::getMergeableConst16(); 2062798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner default: return SectionKind::getMergeableConst(); 207f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner } 2088ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 209f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner case Constant::LocalRelocation: 210f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // In static relocation model, the linker will resolve all addresses, so 211f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // the relocation entries will actually be constants by the time the app 212f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // starts up. However, we can't put this into a mergable section, because 213f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // the linker doesn't take relocations into consideration when it tries to 214f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // merge entries in the section. 215f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner if (ReloModel == Reloc::Static) 2162798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getReadOnly(); 2178ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 218f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Otherwise, the dynamic linker needs to fix it up, put it in the 219f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // writable data.rel.local section. 2202798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getReadOnlyWithRelLocal(); 2218ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 222f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner case Constant::GlobalRelocations: 223f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // In static relocation model, the linker will resolve all addresses, so 224f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // the relocation entries will actually be constants by the time the app 225f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // starts up. However, we can't put this into a mergable section, because 226f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // the linker doesn't take relocations into consideration when it tries to 227f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // merge entries in the section. 228f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner if (ReloModel == Reloc::Static) 2292798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getReadOnly(); 2308ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 231f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Otherwise, the dynamic linker needs to fix it up, put it in the 232f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // writable data.rel section. 2332798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getReadOnlyWithRel(); 234f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner } 235f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner } 236f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 237f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Okay, this isn't a constant. If the initializer for the global is going 238f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // to require a runtime relocation by the dynamic linker, put it into a more 239f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // specific section to improve startup time of the app. This coalesces these 240f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // globals together onto fewer pages, improving the locality of the dynamic 241f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // linker. 242f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner if (ReloModel == Reloc::Static) 2432798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getDataNoRel(); 244f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 245f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner switch (C->getRelocationInfo()) { 246f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner case Constant::NoRelocation: 2472798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getDataNoRel(); 248f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner case Constant::LocalRelocation: 2492798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getDataRelLocal(); 250f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner case Constant::GlobalRelocations: 2512798119ab4d7e0b42812b3acdf37821f40dee627Chris Lattner return SectionKind::getDataRel(); 252f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner } 253732f05c41f177a0bc4d47e93a5d02120f146cb4cChandler Carruth llvm_unreachable("Invalid relocation"); 254f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner} 255f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 256f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// SectionForGlobal - This method computes the appropriate section to emit 257f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// the specified global variable or function definition. This should not 258f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// be passed external (or available externally) globals. 259a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection *TargetLoweringObjectFile:: 26036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen HinesSectionForGlobal(const GlobalValue *GV, SectionKind Kind, Mangler &Mang, 261e53a600f065075731d0aeb9dc8f4f3d75f5a05f8Chris Lattner const TargetMachine &TM) const { 262f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Select section name. 26324f654c8a4d14066233480f683d3b0dececf374aChris Lattner if (GV->hasSection()) 26424f654c8a4d14066233480f683d3b0dececf374aChris Lattner return getExplicitSectionGlobal(GV, Kind, Mang, TM); 2658ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 2668ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 267f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner // Use default section depending on the 'type' of global 268f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner return SelectSectionForGlobal(GV, Kind, Mang, TM); 269f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner} 270f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 27136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesbool TargetLoweringObjectFile::isSectionAtomizableBySymbols( 27236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const MCSection &Section) const { 27336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return false; 27436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 27558bed8fc29b6e55e7014dcb537808043c946cd73Chris Lattner 276f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner// Lame default implementation. Calculate the section name for global. 277a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection * 278f0144127b98425d214e59e4a1a4b342b78e3642bChris LattnerTargetLoweringObjectFile::SelectSectionForGlobal(const GlobalValue *GV, 279f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner SectionKind Kind, 28036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines Mangler &Mang, 281f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner const TargetMachine &TM) const{ 282f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner assert(!Kind.isThreadLocal() && "Doesn't support TLS"); 2838ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 284f9650c061ee89ac55740555530ca5c2842c28738Chris Lattner if (Kind.isText()) 285f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return getTextSection(); 2868ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 287dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (Kind.isBSS() && BSSSection != nullptr) 288824583844a8f334dd261894a3fac7ad476531667Chris Lattner return BSSSection; 2898ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 290dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (Kind.isReadOnly() && ReadOnlySection != nullptr) 291f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return ReadOnlySection; 292f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 293f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return getDataSection(); 294f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner} 295f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 29683d77faf6e8fc2c1c2377d037283dc162d8667a1Chris Lattner/// getSectionForConstant - Given a mergable constant with the 297f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// specified size and relocation information, return a section that it 298f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner/// should be placed in. 299a87dea4f8c546ca748f1777a8d1cabcc06515d91Chris Lattnerconst MCSection * 30083d77faf6e8fc2c1c2377d037283dc162d8667a1Chris LattnerTargetLoweringObjectFile::getSectionForConstant(SectionKind Kind) const { 301dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines if (Kind.isReadOnly() && ReadOnlySection != nullptr) 302f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return ReadOnlySection; 3038ddb569d8a1831f0e061e2f490d61bccd4166ec4Anton Korobeynikov 304f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner return DataSection; 305f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner} 306f0144127b98425d214e59e4a1a4b342b78e3642bChris Lattner 30725efd6d556718295a63d37f5294985746af354f6Anton Korobeynikov/// getTTypeGlobalReference - Return an MCExpr to use for a 3089184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov/// reference to the specified global variable from exception 3099184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov/// handling information. 31036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hinesconst MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference( 31136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const GlobalValue *GV, unsigned Encoding, Mangler &Mang, 31236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines const TargetMachine &TM, MachineModuleInfo *MMI, 31336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines MCStreamer &Streamer) const { 31425efd6d556718295a63d37f5294985746af354f6Anton Korobeynikov const MCSymbolRefExpr *Ref = 31536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines MCSymbolRefExpr::Create(TM.getSymbol(GV, Mang), getContext()); 31625efd6d556718295a63d37f5294985746af354f6Anton Korobeynikov 31725efd6d556718295a63d37f5294985746af354f6Anton Korobeynikov return getTTypeReference(Ref, Encoding, Streamer); 3189184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov} 3199184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov 3209184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikovconst MCExpr *TargetLoweringObjectFile:: 32125efd6d556718295a63d37f5294985746af354f6Anton KorobeynikovgetTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, 32225efd6d556718295a63d37f5294985746af354f6Anton Korobeynikov MCStreamer &Streamer) const { 323f0adba9a7ec8a3031876575a6ffb7db5f1b6f855Rafael Espindola switch (Encoding & 0x70) { 3249184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov default: 32575361b69f3f327842b9dad69fa7f28ae3b688412Chris Lattner report_fatal_error("We do not support this DWARF encoding yet!"); 3269184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov case dwarf::DW_EH_PE_absptr: 3279184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov // Do nothing special 32825efd6d556718295a63d37f5294985746af354f6Anton Korobeynikov return Sym; 32942263e2e407ab7d1d805e7b41cffd7217134d3b6Chris Lattner case dwarf::DW_EH_PE_pcrel: { 33042263e2e407ab7d1d805e7b41cffd7217134d3b6Chris Lattner // Emit a label to the streamer for the current position. This gives us 33142263e2e407ab7d1d805e7b41cffd7217134d3b6Chris Lattner // .-foo addressing. 33277e76940269b1bed36bc31ee5139b5c90fd13836Chris Lattner MCSymbol *PCSym = getContext().CreateTempSymbol(); 33342263e2e407ab7d1d805e7b41cffd7217134d3b6Chris Lattner Streamer.EmitLabel(PCSym); 33442263e2e407ab7d1d805e7b41cffd7217134d3b6Chris Lattner const MCExpr *PC = MCSymbolRefExpr::Create(PCSym, getContext()); 33525efd6d556718295a63d37f5294985746af354f6Anton Korobeynikov return MCBinaryExpr::CreateSub(Sym, PC, getContext()); 33642263e2e407ab7d1d805e7b41cffd7217134d3b6Chris Lattner } 3379184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov } 3389184b25fa543a900463215c11635c2c014ddb623Anton Korobeynikov} 33959eaa3874663f80ce111a4781b8f1db82995210cDavid Blaikie 340716a94f0c96d6bef575cd286bafb2cc507adc6b0Ulrich Weigandconst MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const { 34159eaa3874663f80ce111a4781b8f1db82995210cDavid Blaikie // FIXME: It's not clear what, if any, default this should have - perhaps a 34259eaa3874663f80ce111a4781b8f1db82995210cDavid Blaikie // null return could mean 'no location' & we should just do that here. 34359eaa3874663f80ce111a4781b8f1db82995210cDavid Blaikie return MCSymbolRefExpr::Create(Sym, *Ctx); 34459eaa3874663f80ce111a4781b8f1db82995210cDavid Blaikie} 345