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