1 //==- llvm/CodeGen/TargetLoweringObjectFileImpl.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_TARGETLOWERINGOBJECTFILEIMPL_H 15 #define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H 16 17 #include "llvm/ADT/SmallPtrSet.h" 18 #include "llvm/BinaryFormat/XCOFF.h" 19 #include "llvm/Target/TargetLoweringObjectFile.h" 20 21 namespace llvm { 22 23 class GlobalValue; 24 class MachineModuleInfo; 25 class MachineFunction; 26 class MCContext; 27 class MCExpr; 28 class MCSection; 29 class MCSymbol; 30 class Module; 31 class TargetMachine; 32 33 class TargetLoweringObjectFileELF : public TargetLoweringObjectFile { 34 bool UseInitArray = false; 35 mutable unsigned NextUniqueID = 1; // ID 0 is reserved for execute-only sections 36 SmallPtrSet<GlobalObject *, 2> Used; 37 38 protected: 39 MCSymbolRefExpr::VariantKind PLTRelativeVariantKind = 40 MCSymbolRefExpr::VK_None; 41 42 public: 43 TargetLoweringObjectFileELF(); 44 ~TargetLoweringObjectFileELF() override = default; 45 46 void Initialize(MCContext &Ctx, const TargetMachine &TM) override; 47 48 void getModuleMetadata(Module &M) override; 49 50 /// Emit Obj-C garbage collection and linker options. 51 void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override; 52 53 void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &DL, 54 const MCSymbol *Sym) const override; 55 56 /// Given a constant with the SectionKind, return a section that it should be 57 /// placed in. 58 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind, 59 const Constant *C, 60 Align &Alignment) const override; 61 62 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 63 const TargetMachine &TM) const override; 64 65 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 66 const TargetMachine &TM) const override; 67 68 MCSection *getSectionForJumpTable(const Function &F, 69 const TargetMachine &TM) const override; 70 MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym, 71 const TargetMachine &TM) const override; 72 73 MCSection * 74 getSectionForMachineBasicBlock(const Function &F, 75 const MachineBasicBlock &MBB, 76 const TargetMachine &TM) const override; 77 78 MCSection * 79 getUniqueSectionForFunction(const Function &F, 80 const TargetMachine &TM) const override; 81 82 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, 83 const Function &F) const override; 84 85 /// Return an MCExpr to use for a reference to the specified type info global 86 /// variable from exception handling information. 87 const MCExpr *getTTypeGlobalReference(const GlobalValue *GV, 88 unsigned Encoding, 89 const TargetMachine &TM, 90 MachineModuleInfo *MMI, 91 MCStreamer &Streamer) const override; 92 93 // The symbol that gets passed to .cfi_personality. 94 MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV, 95 const TargetMachine &TM, 96 MachineModuleInfo *MMI) const override; 97 98 void InitializeELF(bool UseInitArray_); 99 MCSection *getStaticCtorSection(unsigned Priority, 100 const MCSymbol *KeySym) const override; 101 MCSection *getStaticDtorSection(unsigned Priority, 102 const MCSymbol *KeySym) const override; 103 104 const MCExpr *lowerRelativeReference(const GlobalValue *LHS, 105 const GlobalValue *RHS, 106 const TargetMachine &TM) const override; 107 108 const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv, 109 const TargetMachine &TM) const override; 110 111 MCSection *getSectionForCommandLines() const override; 112 }; 113 114 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile { 115 public: 116 TargetLoweringObjectFileMachO(); 117 ~TargetLoweringObjectFileMachO() override = default; 118 119 void Initialize(MCContext &Ctx, const TargetMachine &TM) override; 120 121 /// Emit the module flags that specify the garbage collection information. 122 void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override; 123 124 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 125 const TargetMachine &TM) const override; 126 127 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 128 const TargetMachine &TM) const override; 129 130 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind, 131 const Constant *C, 132 Align &Alignment) const override; 133 134 /// The mach-o version of this method defaults to returning a stub reference. 135 const MCExpr *getTTypeGlobalReference(const GlobalValue *GV, 136 unsigned Encoding, 137 const TargetMachine &TM, 138 MachineModuleInfo *MMI, 139 MCStreamer &Streamer) const override; 140 141 // The symbol that gets passed to .cfi_personality. 142 MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV, 143 const TargetMachine &TM, 144 MachineModuleInfo *MMI) const override; 145 146 /// Get MachO PC relative GOT entry relocation 147 const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV, 148 const MCSymbol *Sym, 149 const MCValue &MV, int64_t Offset, 150 MachineModuleInfo *MMI, 151 MCStreamer &Streamer) const override; 152 153 void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV, 154 const TargetMachine &TM) const override; 155 }; 156 157 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile { 158 mutable unsigned NextUniqueID = 0; 159 const TargetMachine *TM = nullptr; 160 161 public: 162 ~TargetLoweringObjectFileCOFF() override = default; 163 164 void Initialize(MCContext &Ctx, const TargetMachine &TM) override; 165 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 166 const TargetMachine &TM) const override; 167 168 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 169 const TargetMachine &TM) const override; 170 171 void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV, 172 const TargetMachine &TM) const override; 173 174 MCSection *getSectionForJumpTable(const Function &F, 175 const TargetMachine &TM) const override; 176 177 /// Emit Obj-C garbage collection and linker options. 178 void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override; 179 180 MCSection *getStaticCtorSection(unsigned Priority, 181 const MCSymbol *KeySym) const override; 182 MCSection *getStaticDtorSection(unsigned Priority, 183 const MCSymbol *KeySym) const override; 184 185 const MCExpr *lowerRelativeReference(const GlobalValue *LHS, 186 const GlobalValue *RHS, 187 const TargetMachine &TM) const override; 188 189 /// Given a mergeable constant with the specified size and relocation 190 /// information, return a section that it should be placed in. 191 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind, 192 const Constant *C, 193 Align &Alignment) const override; 194 195 private: 196 void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const; 197 }; 198 199 class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile { 200 mutable unsigned NextUniqueID = 0; 201 202 public: 203 TargetLoweringObjectFileWasm() = default; 204 ~TargetLoweringObjectFileWasm() override = default; 205 206 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 207 const TargetMachine &TM) const override; 208 209 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 210 const TargetMachine &TM) const override; 211 212 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, 213 const Function &F) const override; 214 215 void InitializeWasm(); 216 MCSection *getStaticCtorSection(unsigned Priority, 217 const MCSymbol *KeySym) const override; 218 MCSection *getStaticDtorSection(unsigned Priority, 219 const MCSymbol *KeySym) const override; 220 221 const MCExpr *lowerRelativeReference(const GlobalValue *LHS, 222 const GlobalValue *RHS, 223 const TargetMachine &TM) const override; 224 }; 225 226 class TargetLoweringObjectFileXCOFF : public TargetLoweringObjectFile { 227 public: 228 TargetLoweringObjectFileXCOFF() = default; 229 ~TargetLoweringObjectFileXCOFF() override = default; 230 231 static bool ShouldEmitEHBlock(const MachineFunction *MF); 232 233 static MCSymbol *getEHInfoTableSymbol(const MachineFunction *MF); 234 235 void Initialize(MCContext &Ctx, const TargetMachine &TM) override; 236 237 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, 238 const Function &F) const override; 239 240 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 241 const TargetMachine &TM) const override; 242 243 MCSection *getStaticCtorSection(unsigned Priority, 244 const MCSymbol *KeySym) const override; 245 MCSection *getStaticDtorSection(unsigned Priority, 246 const MCSymbol *KeySym) const override; 247 248 const MCExpr *lowerRelativeReference(const GlobalValue *LHS, 249 const GlobalValue *RHS, 250 const TargetMachine &TM) const override; 251 252 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 253 const TargetMachine &TM) const override; 254 255 MCSection *getSectionForJumpTable(const Function &F, 256 const TargetMachine &TM) const override; 257 258 /// Given a constant with the SectionKind, return a section that it should be 259 /// placed in. 260 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind, 261 const Constant *C, 262 Align &Alignment) const override; 263 264 static XCOFF::StorageClass getStorageClassForGlobal(const GlobalValue *GV); 265 266 MCSection * 267 getSectionForFunctionDescriptor(const Function *F, 268 const TargetMachine &TM) const override; 269 MCSection *getSectionForTOCEntry(const MCSymbol *Sym, 270 const TargetMachine &TM) const override; 271 272 /// For external functions, this will always return a function descriptor 273 /// csect. 274 MCSection * 275 getSectionForExternalReference(const GlobalObject *GO, 276 const TargetMachine &TM) const override; 277 278 /// For functions, this will always return a function descriptor symbol. 279 MCSymbol *getTargetSymbol(const GlobalValue *GV, 280 const TargetMachine &TM) const override; 281 282 MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func, 283 const TargetMachine &TM) const override; 284 }; 285 286 } // end namespace llvm 287 288 #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H 289