1 //===-- llvm/Target/TargetLoweringObjectFile.cpp - Object File Info -------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements classes used to handle lowerings specific to common
10 // object file formats.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/TargetLoweringObjectFile.h"
15 #include "llvm/BinaryFormat/Dwarf.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/GlobalVariable.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/SectionKind.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetOptions.h"
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
37 /// Initialize - this method must be called before any actual lowering is
38 /// done. This specifies the current context for codegen, and gives the
39 /// lowering implementations a chance to set up their default sections.
40 void TargetLoweringObjectFile::Initialize(MCContext
&ctx
,
41 const TargetMachine
&TM
) {
42 // `Initialize` can be called more than once.
45 initMCObjectFileInfo(ctx
, TM
.isPositionIndependent(),
46 TM
.getCodeModel() == CodeModel::Large
);
48 // Reset various EH DWARF encodings.
49 PersonalityEncoding
= LSDAEncoding
= TTypeEncoding
= dwarf::DW_EH_PE_absptr
;
50 CallSiteEncoding
= dwarf::DW_EH_PE_uleb128
;
55 TargetLoweringObjectFile::~TargetLoweringObjectFile() {
59 unsigned TargetLoweringObjectFile::getCallSiteEncoding() const {
60 // If target does not have LEB128 directives, we would need the
61 // call site encoding to be udata4 so that the alternative path
62 // for not having LEB128 directives could work.
63 if (!getContext().getAsmInfo()->hasLEB128Directives())
64 return dwarf::DW_EH_PE_udata4
;
65 return CallSiteEncoding
;
68 static bool isNullOrUndef(const Constant
*C
) {
69 // Check that the constant isn't all zeros or undefs.
70 if (C
->isNullValue() || isa
<UndefValue
>(C
))
72 if (!isa
<ConstantAggregate
>(C
))
74 for (const auto *Operand
: C
->operand_values()) {
75 if (!isNullOrUndef(cast
<Constant
>(Operand
)))
81 static bool isSuitableForBSS(const GlobalVariable
*GV
) {
82 const Constant
*C
= GV
->getInitializer();
84 // Must have zero initializer.
85 if (!isNullOrUndef(C
))
88 // Leave constant zeros in readonly constant sections, so they can be shared.
92 // If the global has an explicit section specified, don't put it in BSS.
96 // Otherwise, put it in BSS!
100 /// IsNullTerminatedString - Return true if the specified constant (which is
101 /// known to have a type that is an array of 1/2/4 byte elements) ends with a
102 /// nul value and contains no other nuls in it. Note that this is more general
103 /// than ConstantDataSequential::isString because we allow 2 & 4 byte strings.
104 static bool IsNullTerminatedString(const Constant
*C
) {
105 // First check: is we have constant array terminated with zero
106 if (const ConstantDataSequential
*CDS
= dyn_cast
<ConstantDataSequential
>(C
)) {
107 unsigned NumElts
= CDS
->getNumElements();
108 assert(NumElts
!= 0 && "Can't have an empty CDS");
110 if (CDS
->getElementAsInteger(NumElts
-1) != 0)
111 return false; // Not null terminated.
113 // Verify that the null doesn't occur anywhere else in the string.
114 for (unsigned i
= 0; i
!= NumElts
-1; ++i
)
115 if (CDS
->getElementAsInteger(i
) == 0)
120 // Another possibility: [1 x i8] zeroinitializer
121 if (isa
<ConstantAggregateZero
>(C
))
122 return cast
<ArrayType
>(C
->getType())->getNumElements() == 1;
127 MCSymbol
*TargetLoweringObjectFile::getSymbolWithGlobalValueBase(
128 const GlobalValue
*GV
, StringRef Suffix
, const TargetMachine
&TM
) const {
129 assert(!Suffix
.empty());
131 SmallString
<60> NameStr
;
132 NameStr
+= GV
->getParent()->getDataLayout().getPrivateGlobalPrefix();
133 TM
.getNameWithPrefix(NameStr
, GV
, *Mang
);
134 NameStr
.append(Suffix
.begin(), Suffix
.end());
135 return getContext().getOrCreateSymbol(NameStr
);
138 MCSymbol
*TargetLoweringObjectFile::getCFIPersonalitySymbol(
139 const GlobalValue
*GV
, const TargetMachine
&TM
,
140 MachineModuleInfo
*MMI
) const {
141 return TM
.getSymbol(GV
);
144 void TargetLoweringObjectFile::emitPersonalityValue(MCStreamer
&Streamer
,
146 const MCSymbol
*Sym
) const {
149 void TargetLoweringObjectFile::emitCGProfileMetadata(MCStreamer
&Streamer
,
151 MCContext
&C
= getContext();
152 SmallVector
<Module::ModuleFlagEntry
, 8> ModuleFlags
;
153 M
.getModuleFlagsMetadata(ModuleFlags
);
155 MDNode
*CFGProfile
= nullptr;
157 for (const auto &MFE
: ModuleFlags
) {
158 StringRef Key
= MFE
.Key
->getString();
159 if (Key
== "CG Profile") {
160 CFGProfile
= cast
<MDNode
>(MFE
.Val
);
168 auto GetSym
= [this](const MDOperand
&MDO
) -> MCSymbol
* {
171 auto *V
= cast
<ValueAsMetadata
>(MDO
);
172 const Function
*F
= cast
<Function
>(V
->getValue()->stripPointerCasts());
173 if (F
->hasDLLImportStorageClass())
175 return TM
->getSymbol(F
);
178 for (const auto &Edge
: CFGProfile
->operands()) {
179 MDNode
*E
= cast
<MDNode
>(Edge
);
180 const MCSymbol
*From
= GetSym(E
->getOperand(0));
181 const MCSymbol
*To
= GetSym(E
->getOperand(1));
182 // Skip null functions. This can happen if functions are dead stripped after
183 // the CGProfile pass has been run.
186 uint64_t Count
= cast
<ConstantAsMetadata
>(E
->getOperand(2))
190 Streamer
.emitCGProfileEntry(
191 MCSymbolRefExpr::create(From
, MCSymbolRefExpr::VK_None
, C
),
192 MCSymbolRefExpr::create(To
, MCSymbolRefExpr::VK_None
, C
), Count
);
196 /// getKindForGlobal - This is a top-level target-independent classifier for
197 /// a global object. Given a global variable and information from the TM, this
198 /// function classifies the global in a target independent manner. This function
199 /// may be overridden by the target implementation.
200 SectionKind
TargetLoweringObjectFile::getKindForGlobal(const GlobalObject
*GO
,
201 const TargetMachine
&TM
){
202 assert(!GO
->isDeclarationForLinker() &&
203 "Can only be used for global definitions");
205 // Functions are classified as text sections.
206 if (isa
<Function
>(GO
))
207 return SectionKind::getText();
209 // Basic blocks are classified as text sections.
210 if (isa
<BasicBlock
>(GO
))
211 return SectionKind::getText();
213 // Global variables require more detailed analysis.
214 const auto *GVar
= cast
<GlobalVariable
>(GO
);
216 // Handle thread-local data first.
217 if (GVar
->isThreadLocal()) {
218 if (isSuitableForBSS(GVar
) && !TM
.Options
.NoZerosInBSS
) {
219 // Zero-initialized TLS variables with local linkage always get classified
220 // as ThreadBSSLocal.
221 if (GVar
->hasLocalLinkage()) {
222 return SectionKind::getThreadBSSLocal();
224 return SectionKind::getThreadBSS();
226 return SectionKind::getThreadData();
229 // Variables with common linkage always get classified as common.
230 if (GVar
->hasCommonLinkage())
231 return SectionKind::getCommon();
233 // Most non-mergeable zero data can be put in the BSS section unless otherwise
235 if (isSuitableForBSS(GVar
) && !TM
.Options
.NoZerosInBSS
) {
236 if (GVar
->hasLocalLinkage())
237 return SectionKind::getBSSLocal();
238 else if (GVar
->hasExternalLinkage())
239 return SectionKind::getBSSExtern();
240 return SectionKind::getBSS();
243 // Global variables with '!exclude' should get the exclude section kind if
244 // they have an explicit section and no other metadata.
245 if (GVar
->hasSection())
246 if (MDNode
*MD
= GVar
->getMetadata(LLVMContext::MD_exclude
))
247 if (!MD
->getNumOperands())
248 return SectionKind::getExclude();
250 // If the global is marked constant, we can put it into a mergable section,
251 // a mergable string section, or general .data if it contains relocations.
252 if (GVar
->isConstant()) {
253 // If the initializer for the global contains something that requires a
254 // relocation, then we may have to drop this into a writable data section
255 // even though it is marked const.
256 const Constant
*C
= GVar
->getInitializer();
257 if (!C
->needsRelocation()) {
258 // If the global is required to have a unique address, it can't be put
259 // into a mergable section: just drop it into the general read-only
261 if (!GVar
->hasGlobalUnnamedAddr())
262 return SectionKind::getReadOnly();
264 // If initializer is a null-terminated string, put it in a "cstring"
265 // section of the right width.
266 if (ArrayType
*ATy
= dyn_cast
<ArrayType
>(C
->getType())) {
267 if (IntegerType
*ITy
=
268 dyn_cast
<IntegerType
>(ATy
->getElementType())) {
269 if ((ITy
->getBitWidth() == 8 || ITy
->getBitWidth() == 16 ||
270 ITy
->getBitWidth() == 32) &&
271 IsNullTerminatedString(C
)) {
272 if (ITy
->getBitWidth() == 8)
273 return SectionKind::getMergeable1ByteCString();
274 if (ITy
->getBitWidth() == 16)
275 return SectionKind::getMergeable2ByteCString();
277 assert(ITy
->getBitWidth() == 32 && "Unknown width");
278 return SectionKind::getMergeable4ByteCString();
283 // Otherwise, just drop it into a mergable constant section. If we have
284 // a section for this size, use it, otherwise use the arbitrary sized
287 GVar
->getParent()->getDataLayout().getTypeAllocSize(C
->getType())) {
288 case 4: return SectionKind::getMergeableConst4();
289 case 8: return SectionKind::getMergeableConst8();
290 case 16: return SectionKind::getMergeableConst16();
291 case 32: return SectionKind::getMergeableConst32();
293 return SectionKind::getReadOnly();
297 // In static, ROPI and RWPI relocation models, the linker will resolve
298 // all addresses, so the relocation entries will actually be constants by
299 // the time the app starts up. However, we can't put this into a
300 // mergable section, because the linker doesn't take relocations into
301 // consideration when it tries to merge entries in the section.
302 Reloc::Model ReloModel
= TM
.getRelocationModel();
303 if (ReloModel
== Reloc::Static
|| ReloModel
== Reloc::ROPI
||
304 ReloModel
== Reloc::RWPI
|| ReloModel
== Reloc::ROPI_RWPI
||
305 !C
->needsDynamicRelocation())
306 return SectionKind::getReadOnly();
308 // Otherwise, the dynamic linker needs to fix it up, put it in the
309 // writable data.rel section.
310 return SectionKind::getReadOnlyWithRel();
314 // Okay, this isn't a constant.
315 return SectionKind::getData();
318 /// This method computes the appropriate section to emit the specified global
319 /// variable or function definition. This should not be passed external (or
320 /// available externally) globals.
321 MCSection
*TargetLoweringObjectFile::SectionForGlobal(
322 const GlobalObject
*GO
, SectionKind Kind
, const TargetMachine
&TM
) const {
323 // Select section name.
324 if (GO
->hasSection())
325 return getExplicitSectionGlobal(GO
, Kind
, TM
);
327 if (auto *GVar
= dyn_cast
<GlobalVariable
>(GO
)) {
328 auto Attrs
= GVar
->getAttributes();
329 if ((Attrs
.hasAttribute("bss-section") && Kind
.isBSS()) ||
330 (Attrs
.hasAttribute("data-section") && Kind
.isData()) ||
331 (Attrs
.hasAttribute("relro-section") && Kind
.isReadOnlyWithRel()) ||
332 (Attrs
.hasAttribute("rodata-section") && Kind
.isReadOnly())) {
333 return getExplicitSectionGlobal(GO
, Kind
, TM
);
337 if (auto *F
= dyn_cast
<Function
>(GO
)) {
338 if (F
->hasFnAttribute("implicit-section-name"))
339 return getExplicitSectionGlobal(GO
, Kind
, TM
);
342 // Use default section depending on the 'type' of global
343 return SelectSectionForGlobal(GO
, Kind
, TM
);
346 /// This method computes the appropriate section to emit the specified global
347 /// variable or function definition. This should not be passed external (or
348 /// available externally) globals.
350 TargetLoweringObjectFile::SectionForGlobal(const GlobalObject
*GO
,
351 const TargetMachine
&TM
) const {
352 return SectionForGlobal(GO
, getKindForGlobal(GO
, TM
), TM
);
355 MCSection
*TargetLoweringObjectFile::getSectionForJumpTable(
356 const Function
&F
, const TargetMachine
&TM
) const {
358 return getSectionForConstant(F
.getParent()->getDataLayout(),
359 SectionKind::getReadOnly(), /*C=*/nullptr,
363 bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
364 bool UsesLabelDifference
, const Function
&F
) const {
365 // In PIC mode, we need to emit the jump table to the same section as the
366 // function body itself, otherwise the label differences won't make sense.
367 // FIXME: Need a better predicate for this: what about custom entries?
368 if (UsesLabelDifference
)
371 // We should also do if the section name is NULL or function is declared
372 // in discardable section
373 // FIXME: this isn't the right predicate, should be based on the MCSection
375 return F
.isWeakForLinker();
378 /// Given a mergable constant with the specified size and relocation
379 /// information, return a section that it should be placed in.
380 MCSection
*TargetLoweringObjectFile::getSectionForConstant(
381 const DataLayout
&DL
, SectionKind Kind
, const Constant
*C
,
382 Align
&Alignment
) const {
383 if (Kind
.isReadOnly() && ReadOnlySection
!= nullptr)
384 return ReadOnlySection
;
389 MCSection
*TargetLoweringObjectFile::getSectionForMachineBasicBlock(
390 const Function
&F
, const MachineBasicBlock
&MBB
,
391 const TargetMachine
&TM
) const {
395 MCSection
*TargetLoweringObjectFile::getUniqueSectionForFunction(
396 const Function
&F
, const TargetMachine
&TM
) const {
400 /// getTTypeGlobalReference - Return an MCExpr to use for a
401 /// reference to the specified global variable from exception
402 /// handling information.
403 const MCExpr
*TargetLoweringObjectFile::getTTypeGlobalReference(
404 const GlobalValue
*GV
, unsigned Encoding
, const TargetMachine
&TM
,
405 MachineModuleInfo
*MMI
, MCStreamer
&Streamer
) const {
406 const MCSymbolRefExpr
*Ref
=
407 MCSymbolRefExpr::create(TM
.getSymbol(GV
), getContext());
409 return getTTypeReference(Ref
, Encoding
, Streamer
);
412 const MCExpr
*TargetLoweringObjectFile::
413 getTTypeReference(const MCSymbolRefExpr
*Sym
, unsigned Encoding
,
414 MCStreamer
&Streamer
) const {
415 switch (Encoding
& 0x70) {
417 report_fatal_error("We do not support this DWARF encoding yet!");
418 case dwarf::DW_EH_PE_absptr
:
419 // Do nothing special
421 case dwarf::DW_EH_PE_pcrel
: {
422 // Emit a label to the streamer for the current position. This gives us
424 MCSymbol
*PCSym
= getContext().createTempSymbol();
425 Streamer
.emitLabel(PCSym
);
426 const MCExpr
*PC
= MCSymbolRefExpr::create(PCSym
, getContext());
427 return MCBinaryExpr::createSub(Sym
, PC
, getContext());
432 const MCExpr
*TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol
*Sym
) const {
433 // FIXME: It's not clear what, if any, default this should have - perhaps a
434 // null return could mean 'no location' & we should just do that here.
435 return MCSymbolRefExpr::create(Sym
, getContext());
438 void TargetLoweringObjectFile::getNameWithPrefix(
439 SmallVectorImpl
<char> &OutName
, const GlobalValue
*GV
,
440 const TargetMachine
&TM
) const {
441 Mang
->getNameWithPrefix(OutName
, GV
, /*CannotUsePrivateLabel=*/false);