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