1 //===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements classes used to handle lowerings specific to common 10 // object file formats. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H 15 #define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H 16 17 #include "llvm/MC/MCObjectFileInfo.h" 18 #include <cstdint> 19 20 namespace llvm { 21 22 class Constant; 23 class DataLayout; 24 class Function; 25 class GlobalObject; 26 class GlobalValue; 27 class MachineBasicBlock; 28 class MachineModuleInfo; 29 class Mangler; 30 class MCContext; 31 class MCExpr; 32 class MCSection; 33 class MCSymbol; 34 class MCSymbolRefExpr; 35 class MCStreamer; 36 class MCValue; 37 class Module; 38 class SectionKind; 39 class StringRef; 40 class TargetMachine; 41 class DSOLocalEquivalent; 42 43 class TargetLoweringObjectFile : public MCObjectFileInfo { 44 /// Name-mangler for global names. 45 Mangler *Mang = nullptr; 46 47 protected: 48 bool SupportIndirectSymViaGOTPCRel = false; 49 bool SupportGOTPCRelWithOffset = true; 50 bool SupportDebugThreadLocalLocation = true; 51 bool SupportDSOLocalEquivalentLowering = false; 52 53 /// PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values 54 /// for EH. 55 unsigned PersonalityEncoding = 0; 56 unsigned LSDAEncoding = 0; 57 unsigned TTypeEncoding = 0; 58 unsigned CallSiteEncoding = 0; 59 60 /// This section contains the static constructor pointer list. 61 MCSection *StaticCtorSection = nullptr; 62 63 /// This section contains the static destructor pointer list. 64 MCSection *StaticDtorSection = nullptr; 65 66 public: 67 TargetLoweringObjectFile() = default; 68 TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete; 69 TargetLoweringObjectFile & 70 operator=(const TargetLoweringObjectFile &) = delete; 71 virtual ~TargetLoweringObjectFile(); 72 getMangler()73 Mangler &getMangler() const { return *Mang; } 74 75 /// This method must be called before any actual lowering is done. This 76 /// specifies the current context for codegen, and gives the lowering 77 /// implementations a chance to set up their default sections. 78 virtual void Initialize(MCContext &ctx, const TargetMachine &TM); 79 80 virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, 81 const MCSymbol *Sym) const; 82 83 /// Emit the module-level metadata that the platform cares about. emitModuleMetadata(MCStreamer & Streamer,Module & M)84 virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {} 85 86 /// Get the module-level metadata that the platform cares about. getModuleMetadata(Module & M)87 virtual void getModuleMetadata(Module &M) {} 88 89 /// Given a constant with the SectionKind, return a section that it should be 90 /// placed in. 91 virtual MCSection *getSectionForConstant(const DataLayout &DL, 92 SectionKind Kind, const Constant *C, 93 Align &Alignment) const; 94 95 virtual MCSection * 96 getSectionForMachineBasicBlock(const Function &F, 97 const MachineBasicBlock &MBB, 98 const TargetMachine &TM) const; 99 100 /// Classify the specified global variable into a set of target independent 101 /// categories embodied in SectionKind. 102 static SectionKind getKindForGlobal(const GlobalObject *GO, 103 const TargetMachine &TM); 104 105 /// This method computes the appropriate section to emit the specified global 106 /// variable or function definition. This should not be passed external (or 107 /// available externally) globals. 108 MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind, 109 const TargetMachine &TM) const; 110 111 /// This method computes the appropriate section to emit the specified global 112 /// variable or function definition. This should not be passed external (or 113 /// available externally) globals. 114 MCSection *SectionForGlobal(const GlobalObject *GO, 115 const TargetMachine &TM) const; 116 117 virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName, 118 const GlobalValue *GV, 119 const TargetMachine &TM) const; 120 121 virtual MCSection *getSectionForJumpTable(const Function &F, 122 const TargetMachine &TM) const; getSectionForLSDA(const Function & F,const TargetMachine & TM)123 virtual MCSection *getSectionForLSDA(const Function &F, 124 const TargetMachine &TM) const { 125 return LSDASection; 126 } 127 128 virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, 129 const Function &F) const; 130 131 /// Targets should implement this method to assign a section to globals with 132 /// an explicit section specfied. The implementation of this method can 133 /// assume that GO->hasSection() is true. 134 virtual MCSection * 135 getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 136 const TargetMachine &TM) const = 0; 137 138 /// Return an MCExpr to use for a reference to the specified global variable 139 /// from exception handling information. 140 virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV, 141 unsigned Encoding, 142 const TargetMachine &TM, 143 MachineModuleInfo *MMI, 144 MCStreamer &Streamer) const; 145 146 /// Return the MCSymbol for a private symbol with global value name as its 147 /// base, with the specified suffix. 148 MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV, 149 StringRef Suffix, 150 const TargetMachine &TM) const; 151 152 // The symbol that gets passed to .cfi_personality. 153 virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV, 154 const TargetMachine &TM, 155 MachineModuleInfo *MMI) const; 156 getPersonalityEncoding()157 unsigned getPersonalityEncoding() const { return PersonalityEncoding; } getLSDAEncoding()158 unsigned getLSDAEncoding() const { return LSDAEncoding; } getTTypeEncoding()159 unsigned getTTypeEncoding() const { return TTypeEncoding; } 160 unsigned getCallSiteEncoding() const; 161 162 const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, 163 MCStreamer &Streamer) const; 164 getStaticCtorSection(unsigned Priority,const MCSymbol * KeySym)165 virtual MCSection *getStaticCtorSection(unsigned Priority, 166 const MCSymbol *KeySym) const { 167 return StaticCtorSection; 168 } 169 getStaticDtorSection(unsigned Priority,const MCSymbol * KeySym)170 virtual MCSection *getStaticDtorSection(unsigned Priority, 171 const MCSymbol *KeySym) const { 172 return StaticDtorSection; 173 } 174 175 /// Create a symbol reference to describe the given TLS variable when 176 /// emitting the address in debug info. 177 virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const; 178 lowerRelativeReference(const GlobalValue * LHS,const GlobalValue * RHS,const TargetMachine & TM)179 virtual const MCExpr *lowerRelativeReference(const GlobalValue *LHS, 180 const GlobalValue *RHS, 181 const TargetMachine &TM) const { 182 return nullptr; 183 } 184 185 /// Target supports a native lowering of a dso_local_equivalent constant 186 /// without needing to replace it with equivalent IR. supportDSOLocalEquivalentLowering()187 bool supportDSOLocalEquivalentLowering() const { 188 return SupportDSOLocalEquivalentLowering; 189 } 190 lowerDSOLocalEquivalent(const DSOLocalEquivalent * Equiv,const TargetMachine & TM)191 virtual const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv, 192 const TargetMachine &TM) const { 193 return nullptr; 194 } 195 196 /// Target supports replacing a data "PC"-relative access to a symbol 197 /// through another symbol, by accessing the later via a GOT entry instead? supportIndirectSymViaGOTPCRel()198 bool supportIndirectSymViaGOTPCRel() const { 199 return SupportIndirectSymViaGOTPCRel; 200 } 201 202 /// Target GOT "PC"-relative relocation supports encoding an additional 203 /// binary expression with an offset? supportGOTPCRelWithOffset()204 bool supportGOTPCRelWithOffset() const { 205 return SupportGOTPCRelWithOffset; 206 } 207 208 /// Target supports TLS offset relocation in debug section? supportDebugThreadLocalLocation()209 bool supportDebugThreadLocalLocation() const { 210 return SupportDebugThreadLocalLocation; 211 } 212 213 /// Get the target specific PC relative GOT entry relocation getIndirectSymViaGOTPCRel(const GlobalValue * GV,const MCSymbol * Sym,const MCValue & MV,int64_t Offset,MachineModuleInfo * MMI,MCStreamer & Streamer)214 virtual const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV, 215 const MCSymbol *Sym, 216 const MCValue &MV, 217 int64_t Offset, 218 MachineModuleInfo *MMI, 219 MCStreamer &Streamer) const { 220 return nullptr; 221 } 222 223 /// If supported, return the section to use for the llvm.commandline 224 /// metadata. Otherwise, return nullptr. getSectionForCommandLines()225 virtual MCSection *getSectionForCommandLines() const { 226 return nullptr; 227 } 228 229 /// On targets that use separate function descriptor symbols, return a section 230 /// for the descriptor given its symbol. Use only with defined functions. 231 virtual MCSection * getSectionForFunctionDescriptor(const Function * F,const TargetMachine & TM)232 getSectionForFunctionDescriptor(const Function *F, 233 const TargetMachine &TM) const { 234 return nullptr; 235 } 236 237 /// On targets that support TOC entries, return a section for the entry given 238 /// the symbol it refers to. 239 /// TODO: Implement this interface for existing ELF targets. getSectionForTOCEntry(const MCSymbol * S,const TargetMachine & TM)240 virtual MCSection *getSectionForTOCEntry(const MCSymbol *S, 241 const TargetMachine &TM) const { 242 return nullptr; 243 } 244 245 /// On targets that associate external references with a section, return such 246 /// a section for the given external global. 247 virtual MCSection * getSectionForExternalReference(const GlobalObject * GO,const TargetMachine & TM)248 getSectionForExternalReference(const GlobalObject *GO, 249 const TargetMachine &TM) const { 250 return nullptr; 251 } 252 253 /// Targets that have a special convention for their symbols could use 254 /// this hook to return a specialized symbol. getTargetSymbol(const GlobalValue * GV,const TargetMachine & TM)255 virtual MCSymbol *getTargetSymbol(const GlobalValue *GV, 256 const TargetMachine &TM) const { 257 return nullptr; 258 } 259 260 /// If supported, return the function entry point symbol. 261 /// Otherwise, returns nulltpr. 262 /// Func must be a function or an alias which has a function as base object. getFunctionEntryPointSymbol(const GlobalValue * Func,const TargetMachine & TM)263 virtual MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func, 264 const TargetMachine &TM) const { 265 return nullptr; 266 } 267 268 protected: 269 virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO, 270 SectionKind Kind, 271 const TargetMachine &TM) const = 0; 272 }; 273 274 } // end namespace llvm 275 276 #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H 277