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 uint16_t PLTRelativeSpecifier = 0; 41 42 public: 43 ~TargetLoweringObjectFileELF() override = default; 44 45 void Initialize(MCContext &Ctx, const TargetMachine &TM) override; 46 47 void getModuleMetadata(Module &M) override; 48 49 /// Emit Obj-C garbage collection and linker options. 50 void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override; 51 52 void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &DL, 53 const MCSymbol *Sym, 54 const MachineModuleInfo *MMI) const override; 55 56 virtual void emitPersonalityValueImpl(MCStreamer &Streamer, 57 const DataLayout &DL, 58 const MCSymbol *Sym, 59 const MachineModuleInfo *MMI) const; 60 61 void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const override; 62 63 /// Given a constant with the SectionKind, return a section that it should be 64 /// placed in. 65 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind, 66 const Constant *C, 67 Align &Alignment) const override; 68 69 /// Similar to the function above, but append \p SectionSuffix to the section 70 /// name. 71 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind, 72 const Constant *C, Align &Alignment, 73 StringRef SectionSuffix) const override; 74 75 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 76 const TargetMachine &TM) const override; 77 78 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 79 const TargetMachine &TM) const override; 80 81 MCSection *getSectionForJumpTable(const Function &F, 82 const TargetMachine &TM) const override; 83 MCSection * 84 getSectionForJumpTable(const Function &F, const TargetMachine &TM, 85 const MachineJumpTableEntry *JTE) const override; 86 MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym, 87 const TargetMachine &TM) const override; 88 89 MCSection * 90 getSectionForMachineBasicBlock(const Function &F, 91 const MachineBasicBlock &MBB, 92 const TargetMachine &TM) const override; 93 94 MCSection * 95 getUniqueSectionForFunction(const Function &F, 96 const TargetMachine &TM) const override; 97 98 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, 99 const Function &F) const override; 100 101 /// Return an MCExpr to use for a reference to the specified type info global 102 /// variable from exception handling information. 103 const MCExpr *getTTypeGlobalReference(const GlobalValue *GV, 104 unsigned Encoding, 105 const TargetMachine &TM, 106 MachineModuleInfo *MMI, 107 MCStreamer &Streamer) const override; 108 109 // The symbol that gets passed to .cfi_personality. 110 MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV, 111 const TargetMachine &TM, 112 MachineModuleInfo *MMI) const override; 113 114 void InitializeELF(bool UseInitArray_); 115 MCSection *getStaticCtorSection(unsigned Priority, 116 const MCSymbol *KeySym) const override; 117 MCSection *getStaticDtorSection(unsigned Priority, 118 const MCSymbol *KeySym) const override; 119 120 const MCExpr * 121 lowerSymbolDifference(const MCSymbol *LHS, const MCSymbol *RHS, 122 int64_t Addend, 123 std::optional<int64_t> PCRelativeOffset) const; 124 125 const MCExpr *lowerDSOLocalEquivalent(const MCSymbol *LHS, 126 const MCSymbol *RHS, int64_t Addend, 127 std::optional<int64_t> PCRelativeOffset, 128 const TargetMachine &TM) const override; 129 130 MCSection *getSectionForCommandLines() const override; 131 }; 132 133 class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile { 134 public: 135 TargetLoweringObjectFileMachO(); 136 ~TargetLoweringObjectFileMachO() override = default; 137 138 void Initialize(MCContext &Ctx, const TargetMachine &TM) override; 139 140 MCSection *getStaticDtorSection(unsigned Priority, 141 const MCSymbol *KeySym) const override; 142 143 /// Emit the module flags that specify the garbage collection information. 144 void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override; 145 146 void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const override; 147 148 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 149 const TargetMachine &TM) const override; 150 151 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 152 const TargetMachine &TM) const override; 153 154 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind, 155 const Constant *C, 156 Align &Alignment) const override; 157 158 /// The mach-o version of this method defaults to returning a stub reference. 159 const MCExpr *getTTypeGlobalReference(const GlobalValue *GV, 160 unsigned Encoding, 161 const TargetMachine &TM, 162 MachineModuleInfo *MMI, 163 MCStreamer &Streamer) const override; 164 165 // The symbol that gets passed to .cfi_personality. 166 MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV, 167 const TargetMachine &TM, 168 MachineModuleInfo *MMI) const override; 169 170 /// Get MachO PC relative GOT entry relocation 171 const MCExpr *getIndirectSymViaGOTPCRel(const GlobalValue *GV, 172 const MCSymbol *Sym, 173 const MCValue &MV, int64_t Offset, 174 MachineModuleInfo *MMI, 175 MCStreamer &Streamer) const override; 176 177 void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV, 178 const TargetMachine &TM) const override; 179 180 MCSection *getSectionForCommandLines() const override; 181 }; 182 183 class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile { 184 mutable unsigned NextUniqueID = 0; 185 const TargetMachine *TM = nullptr; 186 187 public: 188 ~TargetLoweringObjectFileCOFF() override = default; 189 190 void Initialize(MCContext &Ctx, const TargetMachine &TM) override; 191 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 192 const TargetMachine &TM) const override; 193 194 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 195 const TargetMachine &TM) const override; 196 197 void getNameWithPrefix(SmallVectorImpl<char> &OutName, const GlobalValue *GV, 198 const TargetMachine &TM) const override; 199 200 MCSection *getSectionForJumpTable(const Function &F, 201 const TargetMachine &TM) const override; 202 203 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, 204 const Function &F) const override; 205 206 /// Emit Obj-C garbage collection and linker options. 207 void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override; 208 209 void emitLinkerDirectives(MCStreamer &Streamer, Module &M) const override; 210 211 MCSection *getStaticCtorSection(unsigned Priority, 212 const MCSymbol *KeySym) const override; 213 MCSection *getStaticDtorSection(unsigned Priority, 214 const MCSymbol *KeySym) const override; 215 216 const MCExpr *lowerRelativeReference(const GlobalValue *LHS, 217 const GlobalValue *RHS, int64_t Addend, 218 std::optional<int64_t> PCRelativeOffset, 219 const TargetMachine &TM) const override; 220 221 /// Given a mergeable constant with the specified size and relocation 222 /// information, return a section that it should be placed in. 223 MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind, 224 const Constant *C, 225 Align &Alignment) const override; 226 }; 227 228 class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile { 229 mutable unsigned NextUniqueID = 0; 230 SmallPtrSet<GlobalObject *, 2> Used; 231 232 public: 233 TargetLoweringObjectFileWasm() = default; 234 ~TargetLoweringObjectFileWasm() override = default; 235 236 void getModuleMetadata(Module &M) override; 237 238 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 239 const TargetMachine &TM) const override; 240 241 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 242 const TargetMachine &TM) const override; 243 244 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, 245 const Function &F) const override; 246 247 void InitializeWasm(); 248 MCSection *getStaticCtorSection(unsigned Priority, 249 const MCSymbol *KeySym) const override; 250 MCSection *getStaticDtorSection(unsigned Priority, 251 const MCSymbol *KeySym) const override; 252 }; 253 254 class TargetLoweringObjectFileXCOFF : public TargetLoweringObjectFile { 255 public: 256 TargetLoweringObjectFileXCOFF() = default; 257 ~TargetLoweringObjectFileXCOFF() override = default; 258 259 static bool ShouldEmitEHBlock(const MachineFunction *MF); 260 static bool ShouldSetSSPCanaryBitInTB(const MachineFunction *MF); 261 262 static MCSymbol *getEHInfoTableSymbol(const MachineFunction *MF); 263 264 void Initialize(MCContext &Ctx, const TargetMachine &TM) override; 265 266 bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, 267 const Function &F) const override; 268 269 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 270 const TargetMachine &TM) const override; 271 272 MCSection *getStaticCtorSection(unsigned Priority, 273 const MCSymbol *KeySym) const override; 274 MCSection *getStaticDtorSection(unsigned Priority, 275 const MCSymbol *KeySym) 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 std::string DefaultRootSDName; 320 std::string DefaultADAPRName; 321 322 public: 323 TargetLoweringObjectFileGOFF(); 324 ~TargetLoweringObjectFileGOFF() override = default; 325 326 void getModuleMetadata(Module &M) override; 327 328 MCSection *SelectSectionForGlobal(const GlobalObject *GO, SectionKind Kind, 329 const TargetMachine &TM) const override; 330 MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, 331 const TargetMachine &TM) const override; 332 MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym, 333 const TargetMachine &TM) const override; 334 }; 335 336 } // end namespace llvm 337 338 #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H 339