Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===-- llvm/Target/TargetLoweringObjectFile.h - Object Info ----*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/IR/Module.h" |
| 20 | #include "llvm/MC/MCObjectFileInfo.h" |
| 21 | #include "llvm/MC/SectionKind.h" |
| 22 | #include <cstdint> |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class GlobalValue; |
| 27 | class MachineModuleInfo; |
| 28 | class Mangler; |
| 29 | class MCContext; |
| 30 | class MCExpr; |
| 31 | class MCSection; |
| 32 | class MCSymbol; |
| 33 | class MCSymbolRefExpr; |
| 34 | class MCStreamer; |
| 35 | class MCValue; |
| 36 | class TargetMachine; |
| 37 | |
| 38 | class TargetLoweringObjectFile : public MCObjectFileInfo { |
| 39 | MCContext *Ctx = nullptr; |
| 40 | |
| 41 | /// Name-mangler for global names. |
| 42 | Mangler *Mang = nullptr; |
| 43 | |
| 44 | protected: |
| 45 | bool SupportIndirectSymViaGOTPCRel = false; |
| 46 | bool SupportGOTPCRelWithOffset = true; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 47 | bool SupportDebugThreadLocalLocation = true; |
| 48 | |
| 49 | /// PersonalityEncoding, LSDAEncoding, TTypeEncoding - Some encoding values |
| 50 | /// for EH. |
| 51 | unsigned PersonalityEncoding = 0; |
| 52 | unsigned LSDAEncoding = 0; |
| 53 | unsigned TTypeEncoding = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 54 | |
| 55 | /// This section contains the static constructor pointer list. |
| 56 | MCSection *StaticCtorSection = nullptr; |
| 57 | |
| 58 | /// This section contains the static destructor pointer list. |
| 59 | MCSection *StaticDtorSection = nullptr; |
| 60 | |
| 61 | public: |
| 62 | TargetLoweringObjectFile() = default; |
| 63 | TargetLoweringObjectFile(const TargetLoweringObjectFile &) = delete; |
| 64 | TargetLoweringObjectFile & |
| 65 | operator=(const TargetLoweringObjectFile &) = delete; |
| 66 | virtual ~TargetLoweringObjectFile(); |
| 67 | |
| 68 | MCContext &getContext() const { return *Ctx; } |
| 69 | Mangler &getMangler() const { return *Mang; } |
| 70 | |
| 71 | /// This method must be called before any actual lowering is done. This |
| 72 | /// specifies the current context for codegen, and gives the lowering |
| 73 | /// implementations a chance to set up their default sections. |
| 74 | virtual void Initialize(MCContext &ctx, const TargetMachine &TM); |
| 75 | |
| 76 | virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, |
| 77 | const MCSymbol *Sym) const; |
| 78 | |
| 79 | /// Emit the module-level metadata that the platform cares about. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 80 | virtual void emitModuleMetadata(MCStreamer &Streamer, Module &M) const {} |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 81 | |
| 82 | /// Given a constant with the SectionKind, return a section that it should be |
| 83 | /// placed in. |
| 84 | virtual MCSection *getSectionForConstant(const DataLayout &DL, |
| 85 | SectionKind Kind, |
| 86 | const Constant *C, |
| 87 | unsigned &Align) const; |
| 88 | |
| 89 | /// Classify the specified global variable into a set of target independent |
| 90 | /// categories embodied in SectionKind. |
| 91 | static SectionKind getKindForGlobal(const GlobalObject *GO, |
| 92 | const TargetMachine &TM); |
| 93 | |
| 94 | /// This method computes the appropriate section to emit the specified global |
| 95 | /// variable or function definition. This should not be passed external (or |
| 96 | /// available externally) globals. |
| 97 | MCSection *SectionForGlobal(const GlobalObject *GO, SectionKind Kind, |
| 98 | const TargetMachine &TM) const; |
| 99 | |
| 100 | /// This method computes the appropriate section to emit the specified global |
| 101 | /// variable or function definition. This should not be passed external (or |
| 102 | /// available externally) globals. |
| 103 | MCSection *SectionForGlobal(const GlobalObject *GO, |
| 104 | const TargetMachine &TM) const { |
| 105 | return SectionForGlobal(GO, getKindForGlobal(GO, TM), TM); |
| 106 | } |
| 107 | |
| 108 | virtual void getNameWithPrefix(SmallVectorImpl<char> &OutName, |
| 109 | const GlobalValue *GV, |
| 110 | const TargetMachine &TM) const; |
| 111 | |
| 112 | virtual MCSection *getSectionForJumpTable(const Function &F, |
| 113 | const TargetMachine &TM) const; |
| 114 | |
| 115 | virtual bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference, |
| 116 | const Function &F) const; |
| 117 | |
| 118 | /// Targets should implement this method to assign a section to globals with |
| 119 | /// an explicit section specfied. The implementation of this method can |
| 120 | /// assume that GO->hasSection() is true. |
| 121 | virtual MCSection * |
| 122 | getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind, |
| 123 | const TargetMachine &TM) const = 0; |
| 124 | |
| 125 | /// Return an MCExpr to use for a reference to the specified global variable |
| 126 | /// from exception handling information. |
| 127 | virtual const MCExpr *getTTypeGlobalReference(const GlobalValue *GV, |
| 128 | unsigned Encoding, |
| 129 | const TargetMachine &TM, |
| 130 | MachineModuleInfo *MMI, |
| 131 | MCStreamer &Streamer) const; |
| 132 | |
| 133 | /// Return the MCSymbol for a private symbol with global value name as its |
| 134 | /// base, with the specified suffix. |
| 135 | MCSymbol *getSymbolWithGlobalValueBase(const GlobalValue *GV, |
| 136 | StringRef Suffix, |
| 137 | const TargetMachine &TM) const; |
| 138 | |
| 139 | // The symbol that gets passed to .cfi_personality. |
| 140 | virtual MCSymbol *getCFIPersonalitySymbol(const GlobalValue *GV, |
| 141 | const TargetMachine &TM, |
| 142 | MachineModuleInfo *MMI) const; |
| 143 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 144 | unsigned getPersonalityEncoding() const { return PersonalityEncoding; } |
| 145 | unsigned getLSDAEncoding() const { return LSDAEncoding; } |
| 146 | unsigned getTTypeEncoding() const { return TTypeEncoding; } |
| 147 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 148 | const MCExpr *getTTypeReference(const MCSymbolRefExpr *Sym, unsigned Encoding, |
| 149 | MCStreamer &Streamer) const; |
| 150 | |
| 151 | virtual MCSection *getStaticCtorSection(unsigned Priority, |
| 152 | const MCSymbol *KeySym) const { |
| 153 | return StaticCtorSection; |
| 154 | } |
| 155 | |
| 156 | virtual MCSection *getStaticDtorSection(unsigned Priority, |
| 157 | const MCSymbol *KeySym) const { |
| 158 | return StaticDtorSection; |
| 159 | } |
| 160 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 161 | /// Create a symbol reference to describe the given TLS variable when |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 162 | /// emitting the address in debug info. |
| 163 | virtual const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const; |
| 164 | |
| 165 | virtual const MCExpr *lowerRelativeReference(const GlobalValue *LHS, |
| 166 | const GlobalValue *RHS, |
| 167 | const TargetMachine &TM) const { |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 171 | /// Target supports replacing a data "PC"-relative access to a symbol |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 172 | /// through another symbol, by accessing the later via a GOT entry instead? |
| 173 | bool supportIndirectSymViaGOTPCRel() const { |
| 174 | return SupportIndirectSymViaGOTPCRel; |
| 175 | } |
| 176 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 177 | /// Target GOT "PC"-relative relocation supports encoding an additional |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 178 | /// binary expression with an offset? |
| 179 | bool supportGOTPCRelWithOffset() const { |
| 180 | return SupportGOTPCRelWithOffset; |
| 181 | } |
| 182 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 183 | /// Target supports TLS offset relocation in debug section? |
| 184 | bool supportDebugThreadLocalLocation() const { |
| 185 | return SupportDebugThreadLocalLocation; |
| 186 | } |
| 187 | |
| 188 | /// Get the target specific PC relative GOT entry relocation |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 189 | virtual const MCExpr *getIndirectSymViaGOTPCRel(const MCSymbol *Sym, |
| 190 | const MCValue &MV, |
| 191 | int64_t Offset, |
| 192 | MachineModuleInfo *MMI, |
| 193 | MCStreamer &Streamer) const { |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
| 197 | virtual void emitLinkerFlagsForGlobal(raw_ostream &OS, |
| 198 | const GlobalValue *GV) const {} |
| 199 | |
| 200 | virtual void emitLinkerFlagsForUsed(raw_ostream &OS, |
| 201 | const GlobalValue *GV) const {} |
| 202 | |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 203 | /// If supported, return the section to use for the llvm.commandline |
| 204 | /// metadata. Otherwise, return nullptr. |
| 205 | virtual MCSection *getSectionForCommandLines() const { |
| 206 | return nullptr; |
| 207 | } |
| 208 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 209 | protected: |
| 210 | virtual MCSection *SelectSectionForGlobal(const GlobalObject *GO, |
| 211 | SectionKind Kind, |
| 212 | const TargetMachine &TM) const = 0; |
| 213 | }; |
| 214 | |
| 215 | } // end namespace llvm |
| 216 | |
| 217 | #endif // LLVM_CODEGEN_TARGETLOWERINGOBJECTFILE_H |