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