159850d08SRoman Divacky //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
259850d08SRoman Divacky //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
659850d08SRoman Divacky //
759850d08SRoman Divacky //===----------------------------------------------------------------------===//
859850d08SRoman Divacky //
959850d08SRoman Divacky // This file implements classes used to handle lowerings specific to common
1059850d08SRoman Divacky // object file formats.
1159850d08SRoman Divacky //
1259850d08SRoman Divacky //===----------------------------------------------------------------------===//
1359850d08SRoman Divacky
14eb11fae6SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
157ab83427SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
164a16efa3SDimitry Andric #include "llvm/IR/Constants.h"
174a16efa3SDimitry Andric #include "llvm/IR/DataLayout.h"
184a16efa3SDimitry Andric #include "llvm/IR/DerivedTypes.h"
194a16efa3SDimitry Andric #include "llvm/IR/Function.h"
204a16efa3SDimitry Andric #include "llvm/IR/GlobalVariable.h"
215ca98fd9SDimitry Andric #include "llvm/IR/Mangler.h"
22cfca06d7SDimitry Andric #include "llvm/IR/Module.h"
23b60736ecSDimitry Andric #include "llvm/MC/MCAsmInfo.h"
2459850d08SRoman Divacky #include "llvm/MC/MCContext.h"
2559850d08SRoman Divacky #include "llvm/MC/MCExpr.h"
26c6910277SRoman Divacky #include "llvm/MC/MCStreamer.h"
27cfca06d7SDimitry Andric #include "llvm/MC/SectionKind.h"
28907da171SRoman Divacky #include "llvm/Support/ErrorHandling.h"
294a16efa3SDimitry Andric #include "llvm/Target/TargetMachine.h"
304a16efa3SDimitry Andric #include "llvm/Target/TargetOptions.h"
3159850d08SRoman Divacky using namespace llvm;
3259850d08SRoman Divacky
3359850d08SRoman Divacky //===----------------------------------------------------------------------===//
3459850d08SRoman Divacky // Generic Code
3559850d08SRoman Divacky //===----------------------------------------------------------------------===//
3659850d08SRoman Divacky
3730815c53SDimitry Andric /// Initialize - this method must be called before any actual lowering is
3830815c53SDimitry Andric /// done. This specifies the current context for codegen, and gives the
3930815c53SDimitry Andric /// lowering implementations a chance to set up their default sections.
Initialize(MCContext & ctx,const TargetMachine & TM)4030815c53SDimitry Andric void TargetLoweringObjectFile::Initialize(MCContext &ctx,
4130815c53SDimitry Andric const TargetMachine &TM) {
42b915e9e0SDimitry Andric // `Initialize` can be called more than once.
43a303c417SDimitry Andric delete Mang;
44b915e9e0SDimitry Andric Mang = new Mangler();
45344a3780SDimitry Andric initMCObjectFileInfo(ctx, TM.isPositionIndependent(),
46044eb2f6SDimitry Andric TM.getCodeModel() == CodeModel::Large);
47d8e91e46SDimitry Andric
48d8e91e46SDimitry Andric // Reset various EH DWARF encodings.
49d8e91e46SDimitry Andric PersonalityEncoding = LSDAEncoding = TTypeEncoding = dwarf::DW_EH_PE_absptr;
50e6d15924SDimitry Andric CallSiteEncoding = dwarf::DW_EH_PE_uleb128;
51b60736ecSDimitry Andric
52b60736ecSDimitry Andric this->TM = &TM;
5359850d08SRoman Divacky }
5459850d08SRoman Divacky
~TargetLoweringObjectFile()5559850d08SRoman Divacky TargetLoweringObjectFile::~TargetLoweringObjectFile() {
56b915e9e0SDimitry Andric delete Mang;
5759850d08SRoman Divacky }
5859850d08SRoman Divacky
getCallSiteEncoding() const59b60736ecSDimitry Andric unsigned TargetLoweringObjectFile::getCallSiteEncoding() const {
60b60736ecSDimitry Andric // If target does not have LEB128 directives, we would need the
61b60736ecSDimitry Andric // call site encoding to be udata4 so that the alternative path
62b60736ecSDimitry Andric // for not having LEB128 directives could work.
63b60736ecSDimitry Andric if (!getContext().getAsmInfo()->hasLEB128Directives())
64b60736ecSDimitry Andric return dwarf::DW_EH_PE_udata4;
65b60736ecSDimitry Andric return CallSiteEncoding;
66b60736ecSDimitry Andric }
67b60736ecSDimitry Andric
isNullOrUndef(const Constant * C)68eb11fae6SDimitry Andric static bool isNullOrUndef(const Constant *C) {
69eb11fae6SDimitry Andric // Check that the constant isn't all zeros or undefs.
70eb11fae6SDimitry Andric if (C->isNullValue() || isa<UndefValue>(C))
71eb11fae6SDimitry Andric return true;
72eb11fae6SDimitry Andric if (!isa<ConstantAggregate>(C))
73eb11fae6SDimitry Andric return false;
74e3b55780SDimitry Andric for (const auto *Operand : C->operand_values()) {
75eb11fae6SDimitry Andric if (!isNullOrUndef(cast<Constant>(Operand)))
76eb11fae6SDimitry Andric return false;
77eb11fae6SDimitry Andric }
78eb11fae6SDimitry Andric return true;
79eb11fae6SDimitry Andric }
80eb11fae6SDimitry Andric
isSuitableForBSS(const GlobalVariable * GV)81eb11fae6SDimitry Andric static bool isSuitableForBSS(const GlobalVariable *GV) {
82411bd29eSDimitry Andric const Constant *C = GV->getInitializer();
8359850d08SRoman Divacky
8459850d08SRoman Divacky // Must have zero initializer.
85eb11fae6SDimitry Andric if (!isNullOrUndef(C))
8659850d08SRoman Divacky return false;
8759850d08SRoman Divacky
8859850d08SRoman Divacky // Leave constant zeros in readonly constant sections, so they can be shared.
8959850d08SRoman Divacky if (GV->isConstant())
9059850d08SRoman Divacky return false;
9159850d08SRoman Divacky
9259850d08SRoman Divacky // If the global has an explicit section specified, don't put it in BSS.
935ca98fd9SDimitry Andric if (GV->hasSection())
9459850d08SRoman Divacky return false;
9559850d08SRoman Divacky
9659850d08SRoman Divacky // Otherwise, put it in BSS!
9759850d08SRoman Divacky return true;
9859850d08SRoman Divacky }
9959850d08SRoman Divacky
10059850d08SRoman Divacky /// IsNullTerminatedString - Return true if the specified constant (which is
10159850d08SRoman Divacky /// known to have a type that is an array of 1/2/4 byte elements) ends with a
10263faed5bSDimitry Andric /// nul value and contains no other nuls in it. Note that this is more general
10363faed5bSDimitry Andric /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
IsNullTerminatedString(const Constant * C)10459850d08SRoman Divacky static bool IsNullTerminatedString(const Constant *C) {
10563faed5bSDimitry Andric // First check: is we have constant array terminated with zero
10663faed5bSDimitry Andric if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
10763faed5bSDimitry Andric unsigned NumElts = CDS->getNumElements();
10863faed5bSDimitry Andric assert(NumElts != 0 && "Can't have an empty CDS");
10959850d08SRoman Divacky
11063faed5bSDimitry Andric if (CDS->getElementAsInteger(NumElts-1) != 0)
11159850d08SRoman Divacky return false; // Not null terminated.
11259850d08SRoman Divacky
11359850d08SRoman Divacky // Verify that the null doesn't occur anywhere else in the string.
11463faed5bSDimitry Andric for (unsigned i = 0; i != NumElts-1; ++i)
11563faed5bSDimitry Andric if (CDS->getElementAsInteger(i) == 0)
11659850d08SRoman Divacky return false;
11759850d08SRoman Divacky return true;
11859850d08SRoman Divacky }
11959850d08SRoman Divacky
12059850d08SRoman Divacky // Another possibility: [1 x i8] zeroinitializer
12159850d08SRoman Divacky if (isa<ConstantAggregateZero>(C))
12263faed5bSDimitry Andric return cast<ArrayType>(C->getType())->getNumElements() == 1;
12359850d08SRoman Divacky
12459850d08SRoman Divacky return false;
12559850d08SRoman Divacky }
12659850d08SRoman Divacky
getSymbolWithGlobalValueBase(const GlobalValue * GV,StringRef Suffix,const TargetMachine & TM) const1275ca98fd9SDimitry Andric MCSymbol *TargetLoweringObjectFile::getSymbolWithGlobalValueBase(
128b915e9e0SDimitry Andric const GlobalValue *GV, StringRef Suffix, const TargetMachine &TM) const {
1295ca98fd9SDimitry Andric assert(!Suffix.empty());
1305ca98fd9SDimitry Andric
131f8af5cf6SDimitry Andric SmallString<60> NameStr;
132ac9a064cSDimitry Andric NameStr += GV->getDataLayout().getPrivateGlobalPrefix();
133b915e9e0SDimitry Andric TM.getNameWithPrefix(NameStr, GV, *Mang);
1345ca98fd9SDimitry Andric NameStr.append(Suffix.begin(), Suffix.end());
135cfca06d7SDimitry Andric return getContext().getOrCreateSymbol(NameStr);
136f8af5cf6SDimitry Andric }
137f8af5cf6SDimitry Andric
getCFIPersonalitySymbol(const GlobalValue * GV,const TargetMachine & TM,MachineModuleInfo * MMI) const1385ca98fd9SDimitry Andric MCSymbol *TargetLoweringObjectFile::getCFIPersonalitySymbol(
139b915e9e0SDimitry Andric const GlobalValue *GV, const TargetMachine &TM,
1406b943ff3SDimitry Andric MachineModuleInfo *MMI) const {
141b915e9e0SDimitry Andric return TM.getSymbol(GV);
1426b943ff3SDimitry Andric }
1436b943ff3SDimitry Andric
emitPersonalityValue(MCStreamer & Streamer,const DataLayout &,const MCSymbol * Sym) const1446b943ff3SDimitry Andric void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer &Streamer,
145dd58ef01SDimitry Andric const DataLayout &,
1466b943ff3SDimitry Andric const MCSymbol *Sym) const {
1476b943ff3SDimitry Andric }
1486b943ff3SDimitry Andric
emitCGProfileMetadata(MCStreamer & Streamer,Module & M) const149b60736ecSDimitry Andric void TargetLoweringObjectFile::emitCGProfileMetadata(MCStreamer &Streamer,
150b60736ecSDimitry Andric Module &M) const {
151b60736ecSDimitry Andric MCContext &C = getContext();
152b60736ecSDimitry Andric SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
153b60736ecSDimitry Andric M.getModuleFlagsMetadata(ModuleFlags);
154b60736ecSDimitry Andric
155b60736ecSDimitry Andric MDNode *CFGProfile = nullptr;
156b60736ecSDimitry Andric
157b60736ecSDimitry Andric for (const auto &MFE : ModuleFlags) {
158b60736ecSDimitry Andric StringRef Key = MFE.Key->getString();
159b60736ecSDimitry Andric if (Key == "CG Profile") {
160b60736ecSDimitry Andric CFGProfile = cast<MDNode>(MFE.Val);
161b60736ecSDimitry Andric break;
162b60736ecSDimitry Andric }
163b60736ecSDimitry Andric }
164b60736ecSDimitry Andric
165b60736ecSDimitry Andric if (!CFGProfile)
166b60736ecSDimitry Andric return;
167b60736ecSDimitry Andric
168b60736ecSDimitry Andric auto GetSym = [this](const MDOperand &MDO) -> MCSymbol * {
169b60736ecSDimitry Andric if (!MDO)
170b60736ecSDimitry Andric return nullptr;
171b60736ecSDimitry Andric auto *V = cast<ValueAsMetadata>(MDO);
172b60736ecSDimitry Andric const Function *F = cast<Function>(V->getValue()->stripPointerCasts());
173b60736ecSDimitry Andric if (F->hasDLLImportStorageClass())
174b60736ecSDimitry Andric return nullptr;
175b60736ecSDimitry Andric return TM->getSymbol(F);
176b60736ecSDimitry Andric };
177b60736ecSDimitry Andric
178b60736ecSDimitry Andric for (const auto &Edge : CFGProfile->operands()) {
179b60736ecSDimitry Andric MDNode *E = cast<MDNode>(Edge);
180b60736ecSDimitry Andric const MCSymbol *From = GetSym(E->getOperand(0));
181b60736ecSDimitry Andric const MCSymbol *To = GetSym(E->getOperand(1));
182b60736ecSDimitry Andric // Skip null functions. This can happen if functions are dead stripped after
183b60736ecSDimitry Andric // the CGProfile pass has been run.
184b60736ecSDimitry Andric if (!From || !To)
185b60736ecSDimitry Andric continue;
186b60736ecSDimitry Andric uint64_t Count = cast<ConstantAsMetadata>(E->getOperand(2))
187b60736ecSDimitry Andric ->getValue()
188b60736ecSDimitry Andric ->getUniqueInteger()
189b60736ecSDimitry Andric .getZExtValue();
190b60736ecSDimitry Andric Streamer.emitCGProfileEntry(
191b60736ecSDimitry Andric MCSymbolRefExpr::create(From, MCSymbolRefExpr::VK_None, C),
192b60736ecSDimitry Andric MCSymbolRefExpr::create(To, MCSymbolRefExpr::VK_None, C), Count);
193b60736ecSDimitry Andric }
194b60736ecSDimitry Andric }
1956b943ff3SDimitry Andric
19659850d08SRoman Divacky /// getKindForGlobal - This is a top-level target-independent classifier for
197eb11fae6SDimitry Andric /// a global object. Given a global variable and information from the TM, this
198eb11fae6SDimitry Andric /// function classifies the global in a target independent manner. This function
199eb11fae6SDimitry Andric /// may be overridden by the target implementation.
getKindForGlobal(const GlobalObject * GO,const TargetMachine & TM)200b915e9e0SDimitry Andric SectionKind TargetLoweringObjectFile::getKindForGlobal(const GlobalObject *GO,
20159850d08SRoman Divacky const TargetMachine &TM){
202cfca06d7SDimitry Andric assert(!GO->isDeclarationForLinker() &&
20359850d08SRoman Divacky "Can only be used for global definitions");
20459850d08SRoman Divacky
205eb11fae6SDimitry Andric // Functions are classified as text sections.
206eb11fae6SDimitry Andric if (isa<Function>(GO))
20759850d08SRoman Divacky return SectionKind::getText();
20859850d08SRoman Divacky
209cfca06d7SDimitry Andric // Basic blocks are classified as text sections.
210cfca06d7SDimitry Andric if (isa<BasicBlock>(GO))
211cfca06d7SDimitry Andric return SectionKind::getText();
212cfca06d7SDimitry Andric
213eb11fae6SDimitry Andric // Global variables require more detailed analysis.
214eb11fae6SDimitry Andric const auto *GVar = cast<GlobalVariable>(GO);
215eb11fae6SDimitry Andric
21659850d08SRoman Divacky // Handle thread-local data first.
21759850d08SRoman Divacky if (GVar->isThreadLocal()) {
218344a3780SDimitry Andric if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
219344a3780SDimitry Andric // Zero-initialized TLS variables with local linkage always get classified
220344a3780SDimitry Andric // as ThreadBSSLocal.
221344a3780SDimitry Andric if (GVar->hasLocalLinkage()) {
222344a3780SDimitry Andric return SectionKind::getThreadBSSLocal();
223344a3780SDimitry Andric }
22459850d08SRoman Divacky return SectionKind::getThreadBSS();
225344a3780SDimitry Andric }
22659850d08SRoman Divacky return SectionKind::getThreadData();
22759850d08SRoman Divacky }
22859850d08SRoman Divacky
229989df958SRoman Divacky // Variables with common linkage always get classified as common.
230989df958SRoman Divacky if (GVar->hasCommonLinkage())
231989df958SRoman Divacky return SectionKind::getCommon();
232989df958SRoman Divacky
233eb11fae6SDimitry Andric // Most non-mergeable zero data can be put in the BSS section unless otherwise
234eb11fae6SDimitry Andric // specified.
235eb11fae6SDimitry Andric if (isSuitableForBSS(GVar) && !TM.Options.NoZerosInBSS) {
236989df958SRoman Divacky if (GVar->hasLocalLinkage())
237989df958SRoman Divacky return SectionKind::getBSSLocal();
238989df958SRoman Divacky else if (GVar->hasExternalLinkage())
239989df958SRoman Divacky return SectionKind::getBSSExtern();
24059850d08SRoman Divacky return SectionKind::getBSS();
241989df958SRoman Divacky }
24259850d08SRoman Divacky
2431f917f69SDimitry Andric // Global variables with '!exclude' should get the exclude section kind if
2441f917f69SDimitry Andric // they have an explicit section and no other metadata.
2451f917f69SDimitry Andric if (GVar->hasSection())
2461f917f69SDimitry Andric if (MDNode *MD = GVar->getMetadata(LLVMContext::MD_exclude))
2471f917f69SDimitry Andric if (!MD->getNumOperands())
2481f917f69SDimitry Andric return SectionKind::getExclude();
2491f917f69SDimitry Andric
25059850d08SRoman Divacky // If the global is marked constant, we can put it into a mergable section,
25159850d08SRoman Divacky // a mergable string section, or general .data if it contains relocations.
25259850d08SRoman Divacky if (GVar->isConstant()) {
25359850d08SRoman Divacky // If the initializer for the global contains something that requires a
25458b69754SDimitry Andric // relocation, then we may have to drop this into a writable data section
25559850d08SRoman Divacky // even though it is marked const.
256eb11fae6SDimitry Andric const Constant *C = GVar->getInitializer();
257dd58ef01SDimitry Andric if (!C->needsRelocation()) {
258cf099d11SDimitry Andric // If the global is required to have a unique address, it can't be put
259cf099d11SDimitry Andric // into a mergable section: just drop it into the general read-only
260cf099d11SDimitry Andric // section instead.
26101095a5dSDimitry Andric if (!GVar->hasGlobalUnnamedAddr())
262cf099d11SDimitry Andric return SectionKind::getReadOnly();
263cf099d11SDimitry Andric
26459850d08SRoman Divacky // If initializer is a null-terminated string, put it in a "cstring"
26559850d08SRoman Divacky // section of the right width.
26630815c53SDimitry Andric if (ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) {
26730815c53SDimitry Andric if (IntegerType *ITy =
26859850d08SRoman Divacky dyn_cast<IntegerType>(ATy->getElementType())) {
26959850d08SRoman Divacky if ((ITy->getBitWidth() == 8 || ITy->getBitWidth() == 16 ||
27059850d08SRoman Divacky ITy->getBitWidth() == 32) &&
27159850d08SRoman Divacky IsNullTerminatedString(C)) {
27259850d08SRoman Divacky if (ITy->getBitWidth() == 8)
27359850d08SRoman Divacky return SectionKind::getMergeable1ByteCString();
27459850d08SRoman Divacky if (ITy->getBitWidth() == 16)
27559850d08SRoman Divacky return SectionKind::getMergeable2ByteCString();
27659850d08SRoman Divacky
27759850d08SRoman Divacky assert(ITy->getBitWidth() == 32 && "Unknown width");
27859850d08SRoman Divacky return SectionKind::getMergeable4ByteCString();
27959850d08SRoman Divacky }
28059850d08SRoman Divacky }
28159850d08SRoman Divacky }
28259850d08SRoman Divacky
28359850d08SRoman Divacky // Otherwise, just drop it into a mergable constant section. If we have
28459850d08SRoman Divacky // a section for this size, use it, otherwise use the arbitrary sized
28559850d08SRoman Divacky // mergable section.
286b915e9e0SDimitry Andric switch (
287ac9a064cSDimitry Andric GVar->getDataLayout().getTypeAllocSize(C->getType())) {
28859850d08SRoman Divacky case 4: return SectionKind::getMergeableConst4();
28959850d08SRoman Divacky case 8: return SectionKind::getMergeableConst8();
29059850d08SRoman Divacky case 16: return SectionKind::getMergeableConst16();
29101095a5dSDimitry Andric case 32: return SectionKind::getMergeableConst32();
2925a5ac124SDimitry Andric default:
2935a5ac124SDimitry Andric return SectionKind::getReadOnly();
29459850d08SRoman Divacky }
29559850d08SRoman Divacky
296dd58ef01SDimitry Andric } else {
297b915e9e0SDimitry Andric // In static, ROPI and RWPI relocation models, the linker will resolve
298b915e9e0SDimitry Andric // all addresses, so the relocation entries will actually be constants by
299b915e9e0SDimitry Andric // the time the app starts up. However, we can't put this into a
300b915e9e0SDimitry Andric // mergable section, because the linker doesn't take relocations into
301b915e9e0SDimitry Andric // consideration when it tries to merge entries in the section.
302eb11fae6SDimitry Andric Reloc::Model ReloModel = TM.getRelocationModel();
303b915e9e0SDimitry Andric if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
304344a3780SDimitry Andric ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
305344a3780SDimitry Andric !C->needsDynamicRelocation())
30659850d08SRoman Divacky return SectionKind::getReadOnly();
30759850d08SRoman Divacky
30859850d08SRoman Divacky // Otherwise, the dynamic linker needs to fix it up, put it in the
30959850d08SRoman Divacky // writable data.rel section.
31059850d08SRoman Divacky return SectionKind::getReadOnlyWithRel();
31159850d08SRoman Divacky }
31259850d08SRoman Divacky }
31359850d08SRoman Divacky
31401095a5dSDimitry Andric // Okay, this isn't a constant.
315dd58ef01SDimitry Andric return SectionKind::getData();
31659850d08SRoman Divacky }
31759850d08SRoman Divacky
3185a5ac124SDimitry Andric /// This method computes the appropriate section to emit the specified global
3195a5ac124SDimitry Andric /// variable or function definition. This should not be passed external (or
3205a5ac124SDimitry Andric /// available externally) globals.
SectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const321b915e9e0SDimitry Andric MCSection *TargetLoweringObjectFile::SectionForGlobal(
322b915e9e0SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
32359850d08SRoman Divacky // Select section name.
324b915e9e0SDimitry Andric if (GO->hasSection())
325b915e9e0SDimitry Andric return getExplicitSectionGlobal(GO, Kind, TM);
32659850d08SRoman Divacky
3277ab83427SDimitry Andric if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
3287ab83427SDimitry Andric auto Attrs = GVar->getAttributes();
3297ab83427SDimitry Andric if ((Attrs.hasAttribute("bss-section") && Kind.isBSS()) ||
3307ab83427SDimitry Andric (Attrs.hasAttribute("data-section") && Kind.isData()) ||
3311d5ae102SDimitry Andric (Attrs.hasAttribute("relro-section") && Kind.isReadOnlyWithRel()) ||
3327ab83427SDimitry Andric (Attrs.hasAttribute("rodata-section") && Kind.isReadOnly())) {
3337ab83427SDimitry Andric return getExplicitSectionGlobal(GO, Kind, TM);
3347ab83427SDimitry Andric }
3357ab83427SDimitry Andric }
3367ab83427SDimitry Andric
33759850d08SRoman Divacky // Use default section depending on the 'type' of global
338b915e9e0SDimitry Andric return SelectSectionForGlobal(GO, Kind, TM);
33959850d08SRoman Divacky }
34059850d08SRoman Divacky
341cfca06d7SDimitry Andric /// This method computes the appropriate section to emit the specified global
342cfca06d7SDimitry Andric /// variable or function definition. This should not be passed external (or
343cfca06d7SDimitry Andric /// available externally) globals.
344cfca06d7SDimitry Andric MCSection *
SectionForGlobal(const GlobalObject * GO,const TargetMachine & TM) const345cfca06d7SDimitry Andric TargetLoweringObjectFile::SectionForGlobal(const GlobalObject *GO,
346cfca06d7SDimitry Andric const TargetMachine &TM) const {
347cfca06d7SDimitry Andric return SectionForGlobal(GO, getKindForGlobal(GO, TM), TM);
348cfca06d7SDimitry Andric }
349cfca06d7SDimitry Andric
getSectionForJumpTable(const Function & F,const TargetMachine & TM) const3505a5ac124SDimitry Andric MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
351b915e9e0SDimitry Andric const Function &F, const TargetMachine &TM) const {
352cfca06d7SDimitry Andric Align Alignment(1);
353ac9a064cSDimitry Andric return getSectionForConstant(F.getDataLayout(),
35401095a5dSDimitry Andric SectionKind::getReadOnly(), /*C=*/nullptr,
355cfca06d7SDimitry Andric Alignment);
3565a5ac124SDimitry Andric }
3575a5ac124SDimitry Andric
shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,const Function & F) const3585a5ac124SDimitry Andric bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
3595a5ac124SDimitry Andric bool UsesLabelDifference, const Function &F) const {
3605a5ac124SDimitry Andric // In PIC mode, we need to emit the jump table to the same section as the
3615a5ac124SDimitry Andric // function body itself, otherwise the label differences won't make sense.
3625a5ac124SDimitry Andric // FIXME: Need a better predicate for this: what about custom entries?
3635a5ac124SDimitry Andric if (UsesLabelDifference)
3645a5ac124SDimitry Andric return true;
3655a5ac124SDimitry Andric
3665a5ac124SDimitry Andric // We should also do if the section name is NULL or function is declared
3675a5ac124SDimitry Andric // in discardable section
3685a5ac124SDimitry Andric // FIXME: this isn't the right predicate, should be based on the MCSection
3695a5ac124SDimitry Andric // for the function.
37071d5a254SDimitry Andric return F.isWeakForLinker();
3715a5ac124SDimitry Andric }
3725a5ac124SDimitry Andric
3735a5ac124SDimitry Andric /// Given a mergable constant with the specified size and relocation
3745a5ac124SDimitry Andric /// information, return a section that it should be placed in.
getSectionForConstant(const DataLayout & DL,SectionKind Kind,const Constant * C,Align & Alignment) const375dd58ef01SDimitry Andric MCSection *TargetLoweringObjectFile::getSectionForConstant(
37601095a5dSDimitry Andric const DataLayout &DL, SectionKind Kind, const Constant *C,
377cfca06d7SDimitry Andric Align &Alignment) const {
3785ca98fd9SDimitry Andric if (Kind.isReadOnly() && ReadOnlySection != nullptr)
37959850d08SRoman Divacky return ReadOnlySection;
38059850d08SRoman Divacky
38159850d08SRoman Divacky return DataSection;
38259850d08SRoman Divacky }
38359850d08SRoman Divacky
getSectionForMachineBasicBlock(const Function & F,const MachineBasicBlock & MBB,const TargetMachine & TM) const384cfca06d7SDimitry Andric MCSection *TargetLoweringObjectFile::getSectionForMachineBasicBlock(
385cfca06d7SDimitry Andric const Function &F, const MachineBasicBlock &MBB,
386cfca06d7SDimitry Andric const TargetMachine &TM) const {
387cfca06d7SDimitry Andric return nullptr;
388cfca06d7SDimitry Andric }
389cfca06d7SDimitry Andric
getUniqueSectionForFunction(const Function & F,const TargetMachine & TM) const390344a3780SDimitry Andric MCSection *TargetLoweringObjectFile::getUniqueSectionForFunction(
391344a3780SDimitry Andric const Function &F, const TargetMachine &TM) const {
392344a3780SDimitry Andric return nullptr;
393344a3780SDimitry Andric }
394344a3780SDimitry Andric
3954a16efa3SDimitry Andric /// getTTypeGlobalReference - Return an MCExpr to use for a
3966fe5c7aaSRoman Divacky /// reference to the specified global variable from exception
3976fe5c7aaSRoman Divacky /// handling information.
getTTypeGlobalReference(const GlobalValue * GV,unsigned Encoding,const TargetMachine & TM,MachineModuleInfo * MMI,MCStreamer & Streamer) const3985ca98fd9SDimitry Andric const MCExpr *TargetLoweringObjectFile::getTTypeGlobalReference(
399b915e9e0SDimitry Andric const GlobalValue *GV, unsigned Encoding, const TargetMachine &TM,
400b915e9e0SDimitry Andric MachineModuleInfo *MMI, MCStreamer &Streamer) const {
4014a16efa3SDimitry Andric const MCSymbolRefExpr *Ref =
402b915e9e0SDimitry Andric MCSymbolRefExpr::create(TM.getSymbol(GV), getContext());
4034a16efa3SDimitry Andric
4044a16efa3SDimitry Andric return getTTypeReference(Ref, Encoding, Streamer);
40559850d08SRoman Divacky }
40659850d08SRoman Divacky
4076fe5c7aaSRoman Divacky const MCExpr *TargetLoweringObjectFile::
getTTypeReference(const MCSymbolRefExpr * Sym,unsigned Encoding,MCStreamer & Streamer) const4084a16efa3SDimitry Andric getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding,
409c6910277SRoman Divacky MCStreamer &Streamer) const {
4106b943ff3SDimitry Andric switch (Encoding & 0x70) {
4116fe5c7aaSRoman Divacky default:
412d7f7719eSRoman Divacky report_fatal_error("We do not support this DWARF encoding yet!");
4136fe5c7aaSRoman Divacky case dwarf::DW_EH_PE_absptr:
4146fe5c7aaSRoman Divacky // Do nothing special
4154a16efa3SDimitry Andric return Sym;
416c6910277SRoman Divacky case dwarf::DW_EH_PE_pcrel: {
417c6910277SRoman Divacky // Emit a label to the streamer for the current position. This gives us
418c6910277SRoman Divacky // .-foo addressing.
4195a5ac124SDimitry Andric MCSymbol *PCSym = getContext().createTempSymbol();
420cfca06d7SDimitry Andric Streamer.emitLabel(PCSym);
42185d8b2bbSDimitry Andric const MCExpr *PC = MCSymbolRefExpr::create(PCSym, getContext());
42285d8b2bbSDimitry Andric return MCBinaryExpr::createSub(Sym, PC, getContext());
423c6910277SRoman Divacky }
424c6910277SRoman Divacky }
42559850d08SRoman Divacky }
426f8af5cf6SDimitry Andric
getDebugThreadLocalSymbol(const MCSymbol * Sym) const427f8af5cf6SDimitry Andric const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
428f8af5cf6SDimitry Andric // FIXME: It's not clear what, if any, default this should have - perhaps a
429f8af5cf6SDimitry Andric // null return could mean 'no location' & we should just do that here.
430cfca06d7SDimitry Andric return MCSymbolRefExpr::create(Sym, getContext());
431f8af5cf6SDimitry Andric }
4325a5ac124SDimitry Andric
getNameWithPrefix(SmallVectorImpl<char> & OutName,const GlobalValue * GV,const TargetMachine & TM) const4335a5ac124SDimitry Andric void TargetLoweringObjectFile::getNameWithPrefix(
434b915e9e0SDimitry Andric SmallVectorImpl<char> &OutName, const GlobalValue *GV,
435dd58ef01SDimitry Andric const TargetMachine &TM) const {
436b915e9e0SDimitry Andric Mang->getNameWithPrefix(OutName, GV, /*CannotUsePrivateLabel=*/false);
4375a5ac124SDimitry Andric }
438