    {-
      This module handles generation of position independent code and
      dynamic-linking related issues for the native code generator.

      This depends on both the architecture and OS, so we define it here
      instead of in one of the architecture specific modules.

      Things outside this module which are related to this:

      + module CLabel
        - PIC base label (pretty printed as local label 1)
        - DynamicLinkerLabels - several kinds:
            CodeStub, SymbolPtr, GotSymbolPtr, GotSymbolOffset
        - labelDynamic predicate
      + module Cmm
        - The GlobalReg datatype has a PicBaseReg constructor
        - The CmmLit datatype has a CmmLabelDiffOff constructor
      + codeGen & RTS
        - When tablesNextToCode, no absolute addresses are stored in info tables
          any more. Instead, offsets from the info label are used.
        - For Win32 only, SRTs might contain addresses of __imp_ symbol pointers
          because Win32 doesn't support external references in data sections.
          TODO: make sure this still works, it might be bitrotted
      + NCG
        - The cmmToCmm pass in AsmCodeGen calls cmmMakeDynamicReference for all
          labels.
        - nativeCodeGen calls pprImportedSymbol and pprGotDeclaration to output
          all the necessary stuff for imported symbols.
        - The NCG monad keeps track of a list of imported symbols.
        - MachCodeGen invokes initializePicBase to generate code to initialize
          the PIC base register when needed.
        - MachCodeGen calls cmmMakeDynamicReference whenever it uses a CLabel
          that wasn't in the original Cmm code (e.g. floating point literals).
    -}

    module GHC.CmmToAsm.PIC (
            cmmMakeDynamicReference,
            CmmMakeDynamicReferenceM(..),
            ReferenceKind(..),
            needImportedSymbols,
            pprImportedSymbol,
            pprGotDeclaration,

            initializePicBase_ppc,
            initializePicBase_x86
    )

    where

    import GHC.Prelude

    import qualified GHC.CmmToAsm.PPC.Instr as PPC
    import qualified GHC.CmmToAsm.PPC.Regs  as PPC
    import qualified GHC.CmmToAsm.X86.Instr as X86

    import GHC.Platform
    import GHC.Platform.Reg
    import GHC.CmmToAsm.Monad
    import GHC.CmmToAsm.Config
    import GHC.CmmToAsm.Types

    import GHC.Cmm.Dataflow.Label
    import GHC.Cmm
    import GHC.Cmm.CLabel
    import GHC.Cmm.Utils (cmmLoadBWord)

    import GHC.Types.Basic

    import GHC.Utils.Outputable
    import GHC.Utils.Panic

    import GHC.Data.FastString

    --------------------------------------------------------------------------------
    -- It gets called by the cmmToCmm pass for every CmmLabel in the Cmm
    -- code. It does The Right Thing(tm) to convert the CmmLabel into a
    -- position-independent, dynamic-linking-aware reference to the thing
    -- in question.
    -- Note that this also has to be called from MachCodeGen in order to
    -- access static data like floating point literals (labels that were
    -- created after the cmmToCmm pass).
    -- The function must run in a monad that can keep track of imported symbols
    -- A function for recording an imported symbol must be passed in:
    -- - addImportCmmOpt for the CmmOptM monad
    -- - addImportNat for the NatM monad.

    data ReferenceKind
            = DataReference
            | CallReference
            | JumpReference
            deriving(ReferenceKind -> ReferenceKind -> Bool
    (ReferenceKind -> ReferenceKind -> Bool)
    -> (ReferenceKind -> ReferenceKind -> Bool) -> Eq ReferenceKind
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: ReferenceKind -> ReferenceKind -> Bool
    == :: ReferenceKind -> ReferenceKind -> Bool
    $c/= :: ReferenceKind -> ReferenceKind -> Bool
    /= :: ReferenceKind -> ReferenceKind -> Bool
    Eq)

    class Monad m => CmmMakeDynamicReferenceM m where
        addImport :: CLabel -> m ()

    instance CmmMakeDynamicReferenceM NatM where
        addImport :: CLabel -> NatM ()
    addImport = CLabel -> NatM ()
    addImportNat

    cmmMakeDynamicReference
      :: CmmMakeDynamicReferenceM m
      => NCGConfig
      -> ReferenceKind     -- whether this is the target of a jump
      -> CLabel            -- the label
      -> m CmmExpr

    cmmMakeDynamicReference :: forall (m :: * -> *).
    CmmMakeDynamicReferenceM m =>
    NCGConfig -> ReferenceKind -> CLabel -> m CmmExpr
    cmmMakeDynamicReference NCGConfig
    config ReferenceKind
    referenceKind CLabel
    lbl
      | Just (DynamicLinkerLabelInfo, CLabel)
    _ <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
    dynamicLinkerLabelInfo CLabel
    lbl
      = CmmExpr -> m CmmExpr
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
    forall a b. (a -> b) -> a -> b
    $ CmmLit -> CmmExpr
    CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
    forall a b. (a -> b) -> a -> b
    $ CLabel -> CmmLit
    CmmLabel CLabel
    lbl   -- already processed it, pass through

      | Bool
    otherwise
      = do let platform :: Platform
    platform = NCGConfig -> Platform
    ncgPlatform NCGConfig
    config
           case NCGConfig
    -> Arch -> OS -> ReferenceKind -> CLabel -> LabelAccessStyle
    howToAccessLabel
                    NCGConfig
    config
                    (Platform -> Arch
    platformArch Platform
    platform)
                    (Platform -> OS
    platformOS   Platform
    platform)
                    ReferenceKind
    referenceKind CLabel
    lbl of

            LabelAccessStyle
    AccessViaStub -> do
                  let stub :: CLabel
    stub = DynamicLinkerLabelInfo -> CLabel -> CLabel
    mkDynamicLinkerLabel DynamicLinkerLabelInfo
    CodeStub CLabel
    lbl
                  CLabel -> m ()
    forall (m :: * -> *). CmmMakeDynamicReferenceM m => CLabel -> m ()
    addImport CLabel
    stub
                  CmmExpr -> m CmmExpr
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
    forall a b. (a -> b) -> a -> b
    $ CmmLit -> CmmExpr
    CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
    forall a b. (a -> b) -> a -> b
    $ CLabel -> CmmLit
    CmmLabel CLabel
    stub

            -- GOT relative loads work differently on AArch64.  We don't do two
            -- step loads. The got symbol is loaded directly, and not through an
            -- additional load. Thus we do not need the CmmLoad decoration we have
            -- on other platforms.
            LabelAccessStyle
    AccessViaSymbolPtr | Arch
    ArchAArch64 <- Platform -> Arch
    platformArch Platform
    platform -> do
                  let symbolPtr :: CLabel
    symbolPtr = DynamicLinkerLabelInfo -> CLabel -> CLabel
    mkDynamicLinkerLabel DynamicLinkerLabelInfo
    SymbolPtr CLabel
    lbl
                  CLabel -> m ()
    forall (m :: * -> *). CmmMakeDynamicReferenceM m => CLabel -> m ()
    addImport CLabel
    symbolPtr
                  CmmExpr -> m CmmExpr
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
    forall a b. (a -> b) -> a -> b
    $ NCGConfig -> CLabel -> CmmExpr
    cmmMakePicReference NCGConfig
    config CLabel
    symbolPtr

            LabelAccessStyle
    AccessViaSymbolPtr -> do
                  let symbolPtr :: CLabel
    symbolPtr = DynamicLinkerLabelInfo -> CLabel -> CLabel
    mkDynamicLinkerLabel DynamicLinkerLabelInfo
    SymbolPtr CLabel
    lbl
                  CLabel -> m ()
    forall (m :: * -> *). CmmMakeDynamicReferenceM m => CLabel -> m ()
    addImport CLabel
    symbolPtr
                  CmmExpr -> m CmmExpr
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
    forall a b. (a -> b) -> a -> b
    $ Platform -> CmmExpr -> CmmExpr
    cmmLoadBWord Platform
    platform (NCGConfig -> CLabel -> CmmExpr
    cmmMakePicReference NCGConfig
    config CLabel
    symbolPtr)

            LabelAccessStyle
    AccessDirectly -> case ReferenceKind
    referenceKind of
                    -- for data, we might have to make some calculations:
                  ReferenceKind
    DataReference -> CmmExpr -> m CmmExpr
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
    forall a b. (a -> b) -> a -> b
    $ NCGConfig -> CLabel -> CmmExpr
    cmmMakePicReference NCGConfig
    config CLabel
    lbl
                    -- all currently supported processors support
                    -- PC-relative branch and call instructions,
                    -- so just jump there if it's a call or a jump
                  ReferenceKind
    _ -> CmmExpr -> m CmmExpr
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (CmmExpr -> m CmmExpr) -> CmmExpr -> m CmmExpr
    forall a b. (a -> b) -> a -> b
    $ CmmLit -> CmmExpr
    CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
    forall a b. (a -> b) -> a -> b
    $ CLabel -> CmmLit
    CmmLabel CLabel
    lbl

    -- -----------------------------------------------------------------------------
    -- Create a position independent reference to a label.
    -- (but do not bother with dynamic linking).
    -- We calculate the label's address by adding some (platform-dependent)
    -- offset to our base register; this offset is calculated by
    -- the function picRelative in the platform-dependent part below.

    cmmMakePicReference :: NCGConfig -> CLabel -> CmmExpr
    cmmMakePicReference :: NCGConfig -> CLabel -> CmmExpr
    cmmMakePicReference NCGConfig
    config CLabel
    lbl
      -- Windows doesn't need PIC,
      -- everything gets relocated at runtime
      | OS
    OSMinGW32 <- Platform -> OS
    platformOS Platform
    platform
      = CmmLit -> CmmExpr
    CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
    forall a b. (a -> b) -> a -> b
    $ CLabel -> CmmLit
    CmmLabel CLabel
    lbl

      -- no pic base reg on AArch64, however indicate this symbol should go through
      -- the global offset table (GOT).
      | Arch
    ArchAArch64 <- Platform -> Arch
    platformArch Platform
    platform
      = CmmLit -> CmmExpr
    CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
    forall a b. (a -> b) -> a -> b
    $ CLabel -> CmmLit
    CmmLabel CLabel
    lbl

      | OS
    OSAIX <- Platform -> OS
    platformOS Platform
    platform
      = MachOp -> [CmmExpr] -> CmmExpr
    CmmMachOp (Width -> MachOp
    MO_Add Width
    W32)
              [ CmmReg -> CmmExpr
    CmmReg (GlobalRegUse -> CmmReg
    CmmGlobal (GlobalRegUse -> CmmReg) -> GlobalRegUse -> CmmReg
    forall a b. (a -> b) -> a -> b
    $ GlobalReg -> CmmType -> GlobalRegUse
    GlobalRegUse GlobalReg
    PicBaseReg (Platform -> CmmType
    bWord Platform
    platform))
              , CmmLit -> CmmExpr
    CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
    forall a b. (a -> b) -> a -> b
    $ Width -> Arch -> OS -> CLabel -> CmmLit
    picRelative (Platform -> Width
    wordWidth Platform
    platform)
                              (Platform -> Arch
    platformArch Platform
    platform)
                              (Platform -> OS
    platformOS   Platform
    platform)
                              CLabel
    lbl ]

      -- both ABI versions default to medium code model
      | ArchPPC_64 PPC_64ABI
    _ <- Platform -> Arch
    platformArch Platform
    platform
      = MachOp -> [CmmExpr] -> CmmExpr
    CmmMachOp (Width -> MachOp
    MO_Add Width
    W32) -- code model medium
              [ CmmReg -> CmmExpr
    CmmReg (GlobalRegUse -> CmmReg
    CmmGlobal (GlobalRegUse -> CmmReg) -> GlobalRegUse -> CmmReg
    forall a b. (a -> b) -> a -> b
    $ GlobalReg -> CmmType -> GlobalRegUse
    GlobalRegUse GlobalReg
    PicBaseReg (Platform -> CmmType
    bWord Platform
    platform))
              , CmmLit -> CmmExpr
    CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
    forall a b. (a -> b) -> a -> b
    $ Width -> Arch -> OS -> CLabel -> CmmLit
    picRelative (Platform -> Width
    wordWidth Platform
    platform)
                              (Platform -> Arch
    platformArch Platform
    platform)
                              (Platform -> OS
    platformOS   Platform
    platform)
                              CLabel
    lbl ]

      | (NCGConfig -> Bool
    ncgPIC NCGConfig
    config Bool -> Bool -> Bool
    || NCGConfig -> Bool
    ncgExternalDynamicRefs NCGConfig
    config)
          Bool -> Bool -> Bool
    && CLabel -> Bool
    absoluteLabel CLabel
    lbl
      = MachOp -> [CmmExpr] -> CmmExpr
    CmmMachOp (Width -> MachOp
    MO_Add (Platform -> Width
    wordWidth Platform
    platform))
              [ CmmReg -> CmmExpr
    CmmReg (GlobalRegUse -> CmmReg
    CmmGlobal (GlobalRegUse -> CmmReg) -> GlobalRegUse -> CmmReg
    forall a b. (a -> b) -> a -> b
    $ GlobalReg -> CmmType -> GlobalRegUse
    GlobalRegUse GlobalReg
    PicBaseReg (Platform -> CmmType
    bWord Platform
    platform))
              , CmmLit -> CmmExpr
    CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
    forall a b. (a -> b) -> a -> b
    $ Width -> Arch -> OS -> CLabel -> CmmLit
    picRelative (Platform -> Width
    wordWidth Platform
    platform)
                              (Platform -> Arch
    platformArch Platform
    platform)
                              (Platform -> OS
    platformOS   Platform
    platform)
                              CLabel
    lbl ]

      | Bool
    otherwise
      = CmmLit -> CmmExpr
    CmmLit (CmmLit -> CmmExpr) -> CmmLit -> CmmExpr
    forall a b. (a -> b) -> a -> b
    $ CLabel -> CmmLit
    CmmLabel CLabel
    lbl
      where
        platform :: Platform
    platform = NCGConfig -> Platform
    ncgPlatform NCGConfig
    config

    absoluteLabel :: CLabel -> Bool
    absoluteLabel :: CLabel -> Bool
    absoluteLabel CLabel
    lbl
     = case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
    dynamicLinkerLabelInfo CLabel
    lbl of
            Just (DynamicLinkerLabelInfo
    GotSymbolPtr, CLabel
    _)    -> Bool
    False
            Just (DynamicLinkerLabelInfo
    GotSymbolOffset, CLabel
    _) -> Bool
    False
            Maybe (DynamicLinkerLabelInfo, CLabel)
    _                         -> Bool
    True

    --------------------------------------------------------------------------------
    -- Knowledge about how special dynamic linker labels like symbol
    -- pointers, code stubs and GOT offsets look like is located in the
    -- module CLabel.

    -- | Helper to check whether the data resides in a DLL or not, see @labelDynamic@
    ncgLabelDynamic :: NCGConfig -> CLabel -> Bool
    ncgLabelDynamic :: NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config = Module -> Platform -> Bool -> CLabel -> Bool
    labelDynamic (NCGConfig -> Module
    ncgThisModule NCGConfig
    config)
                                          (NCGConfig -> Platform
    ncgPlatform NCGConfig
    config)
                                          (NCGConfig -> Bool
    ncgExternalDynamicRefs NCGConfig
    config)

    -- We have to decide which labels need to be accessed
    -- indirectly or via a piece of stub code.
    data LabelAccessStyle
            = AccessViaStub
            | AccessViaSymbolPtr
            | AccessDirectly

    howToAccessLabel :: NCGConfig -> Arch -> OS -> ReferenceKind -> CLabel -> LabelAccessStyle

    -- Windows
    -- In Windows speak, a "module" is a set of objects linked into the
    -- same Portable Executable (PE) file. (both .exe and .dll files are PEs).
    --
    -- If we're compiling a multi-module program then symbols from other modules
    -- are accessed by a symbol pointer named __imp_SYMBOL. At runtime we have the
    -- following.
    --
    --   (in the local module)
    --     __imp_SYMBOL: addr of SYMBOL
    --
    --   (in the other module)
    --     SYMBOL: the real function / data.
    --
    -- To access the function at SYMBOL from our local module, we just need to
    -- dereference the local __imp_SYMBOL.
    --
    -- If not compiling with -dynamic we assume that all our code will be linked
    -- into the same .exe file. In this case we always access symbols directly,
    -- and never use __imp_SYMBOL.
    --
    howToAccessLabel :: NCGConfig
    -> Arch -> OS -> ReferenceKind -> CLabel -> LabelAccessStyle
    howToAccessLabel NCGConfig
    config Arch
    _arch OS
    OSMinGW32 ReferenceKind
    _kind CLabel
    lbl

            -- Assume all symbols will be in the same PE, so just access them directly.
            | Bool -> Bool
    not (NCGConfig -> Bool
    ncgExternalDynamicRefs NCGConfig
    config)
            = LabelAccessStyle
    AccessDirectly

            -- If the target symbol is in another PE we need to access it via the
            --      appropriate __imp_SYMBOL pointer.
            | NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config CLabel
    lbl
            = LabelAccessStyle
    AccessViaSymbolPtr

            -- Target symbol is in the same PE as the caller, so just access it directly.
            | Bool
    otherwise
            = LabelAccessStyle
    AccessDirectly

    -- On AArch64, relocations for JUMP and CALL will be emitted with 26bits, this
    -- is enough for ~64MB of range. Anything else will need to go through a veneer,
    -- which is the job of the linker to build.  We might only want to lookup
    -- Data References through the GOT.
    howToAccessLabel NCGConfig
    config Arch
    ArchAArch64 OS
    _os ReferenceKind
    _kind CLabel
    lbl
            | Bool -> Bool
    not (NCGConfig -> Bool
    ncgExternalDynamicRefs NCGConfig
    config)
            = LabelAccessStyle
    AccessDirectly

            | NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config CLabel
    lbl
            = LabelAccessStyle
    AccessViaSymbolPtr

            | Bool
    otherwise
            = LabelAccessStyle
    AccessDirectly

    -- Mach-O (Darwin, Mac OS X)
    --
    -- Indirect access is required in the following cases:
    --  * things imported from a dynamic library
    --  * (not on x86_64) data from a different module, if we're generating PIC code
    -- It is always possible to access something indirectly,
    -- even when it's not necessary.
    --
    howToAccessLabel NCGConfig
    config Arch
    arch OS
    OSDarwin ReferenceKind
    DataReference CLabel
    lbl
            -- data access to a dynamic library goes via a symbol pointer
            | NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config CLabel
    lbl
            = LabelAccessStyle
    AccessViaSymbolPtr

            -- when generating PIC code, all cross-module data references must
            -- must go via a symbol pointer, too, because the assembler
            -- cannot generate code for a label difference where one
            -- label is undefined. Doesn't apply to x86_64 (why?).
            | Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Arch
    ArchX86_64
            , Bool -> Bool
    not (Module -> CLabel -> Bool
    isLocalCLabel (NCGConfig -> Module
    ncgThisModule NCGConfig
    config) CLabel
    lbl)
            , NCGConfig -> Bool
    ncgPIC NCGConfig
    config
            , CLabel -> Bool
    externallyVisibleCLabel CLabel
    lbl
            = LabelAccessStyle
    AccessViaSymbolPtr

            | Bool
    otherwise
            = LabelAccessStyle
    AccessDirectly

    howToAccessLabel NCGConfig
    config Arch
    arch OS
    OSDarwin ReferenceKind
    JumpReference CLabel
    lbl
            -- dyld code stubs don't work for tailcalls because the
            -- stack alignment is only right for regular calls.
            -- Therefore, we have to go via a symbol pointer:
            | Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    == Arch
    ArchX86 Bool -> Bool -> Bool
    || Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    == Arch
    ArchX86_64 Bool -> Bool -> Bool
    || Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    == Arch
    ArchAArch64
            , NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config CLabel
    lbl
            = LabelAccessStyle
    AccessViaSymbolPtr

    howToAccessLabel NCGConfig
    config Arch
    arch OS
    OSDarwin ReferenceKind
    _kind CLabel
    lbl
            -- Code stubs are the usual method of choice for imported code;
            -- not needed on x86_64 because Apple's new linker, ld64, generates
            -- them automatically, neither on Aarch64 (arm64).
            | Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Arch
    ArchX86_64
            , Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Arch
    ArchAArch64
            , NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config CLabel
    lbl
            = LabelAccessStyle
    AccessViaStub

            | Bool
    otherwise
            = LabelAccessStyle
    AccessDirectly

    ----------------------------------------------------------------------------
    -- AIX

    -- quite simple (for now)
    howToAccessLabel NCGConfig
    _config Arch
    _arch OS
    OSAIX ReferenceKind
    kind CLabel
    _lbl
            = case ReferenceKind
    kind of
                ReferenceKind
    DataReference -> LabelAccessStyle
    AccessViaSymbolPtr
                ReferenceKind
    CallReference -> LabelAccessStyle
    AccessDirectly
                ReferenceKind
    JumpReference -> LabelAccessStyle
    AccessDirectly

    -- ELF (Linux)
    --
    -- ELF tries to pretend to the main application code that dynamic linking does
    -- not exist. While this may sound convenient, it tends to mess things up in
    -- very bad ways, so we have to be careful when we generate code for a non-PIE
    -- main program (-dynamic but no -fPIC).
    --
    -- Indirect access is required for references to imported symbols
    -- from position independent code. It is also required from the main program
    -- when dynamic libraries containing Haskell code are used.

    howToAccessLabel NCGConfig
    _config (ArchPPC_64 PPC_64ABI
    _) OS
    os ReferenceKind
    kind CLabel
    _lbl
            | OS -> Bool
    osElfTarget OS
    os
            = case ReferenceKind
    kind of
              -- ELF PPC64 (powerpc64-linux), AIX, MacOS 9, BeOS/PPC
              ReferenceKind
    DataReference -> LabelAccessStyle
    AccessViaSymbolPtr
              -- RTLD does not generate stubs for function descriptors
              -- in tail calls. Create a symbol pointer and generate
              -- the code to load the function descriptor at the call site.
              ReferenceKind
    JumpReference -> LabelAccessStyle
    AccessViaSymbolPtr
              -- regular calls are handled by the runtime linker
              ReferenceKind
    _             -> LabelAccessStyle
    AccessDirectly

    howToAccessLabel NCGConfig
    config Arch
    _arch OS
    os ReferenceKind
    _kind CLabel
    _lbl
            -- no PIC -> the dynamic linker does everything for us;
            --           if we don't dynamically link to Haskell code,
            --           it actually manages to do so without messing things up.
            | OS -> Bool
    osElfTarget OS
    os
            , Bool -> Bool
    not (NCGConfig -> Bool
    ncgPIC NCGConfig
    config) Bool -> Bool -> Bool
    &&
              Bool -> Bool
    not (NCGConfig -> Bool
    ncgExternalDynamicRefs NCGConfig
    config)
            = LabelAccessStyle
    AccessDirectly

    howToAccessLabel NCGConfig
    config Arch
    arch OS
    os ReferenceKind
    DataReference CLabel
    lbl
            | OS -> Bool
    osElfTarget OS
    os
            = case () of
                -- A dynamic label needs to be accessed via a symbol pointer.
              ()
    _ | NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config CLabel
    lbl
                -> LabelAccessStyle
    AccessViaSymbolPtr

                -- For PowerPC32 -fPIC, we have to access even static data
                -- via a symbol pointer (see below for an explanation why
                -- PowerPC32 Linux is especially broken).
                | Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    == Arch
    ArchPPC
                , NCGConfig -> Bool
    ncgPIC NCGConfig
    config
                -> LabelAccessStyle
    AccessViaSymbolPtr

                | Bool
    otherwise
                -> LabelAccessStyle
    AccessDirectly

            -- In most cases, we have to avoid symbol stubs on ELF, for the following reasons:
            --   on i386, the position-independent symbol stubs in the Procedure Linkage Table
            --   require the address of the GOT to be loaded into register %ebx on entry.
            --   The linker will take any reference to the symbol stub as a hint that
            --   the label in question is a code label. When linking executables, this
            --   will cause the linker to replace even data references to the label with
            --   references to the symbol stub.

            -- This leaves calling a (foreign) function from non-PIC code
            -- (AccessDirectly, because we get an implicit symbol stub)
            -- and calling functions from PIC code on non-i386 platforms (via a symbol stub)

    howToAccessLabel NCGConfig
    config Arch
    arch OS
    os ReferenceKind
    CallReference CLabel
    lbl
            | OS -> Bool
    osElfTarget OS
    os
            , NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config CLabel
    lbl
            , Bool -> Bool
    not (NCGConfig -> Bool
    ncgPIC NCGConfig
    config)
            = LabelAccessStyle
    AccessDirectly

            | OS -> Bool
    osElfTarget OS
    os
            , Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Arch
    ArchX86
            , NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config CLabel
    lbl
            , NCGConfig -> Bool
    ncgPIC NCGConfig
    config
            = LabelAccessStyle
    AccessViaStub

    howToAccessLabel NCGConfig
    config Arch
    _arch OS
    os ReferenceKind
    _kind CLabel
    lbl
            | OS -> Bool
    osElfTarget OS
    os
            = if NCGConfig -> CLabel -> Bool
    ncgLabelDynamic NCGConfig
    config CLabel
    lbl
                then LabelAccessStyle
    AccessViaSymbolPtr
                else LabelAccessStyle
    AccessDirectly

    -- all other platforms
    howToAccessLabel NCGConfig
    config Arch
    _arch OS
    _os ReferenceKind
    _kind CLabel
    _lbl
            | Bool -> Bool
    not (NCGConfig -> Bool
    ncgPIC NCGConfig
    config)
            = LabelAccessStyle
    AccessDirectly

            | Bool
    otherwise
            = String -> LabelAccessStyle
    forall a. HasCallStack => String -> a
    panic String
    "howToAccessLabel: PIC not defined for this platform"

    -- -------------------------------------------------------------------
    -- | Says what we have to add to our 'PIC base register' in order to
    --      get the address of a label.

    picRelative :: Width -> Arch -> OS -> CLabel -> CmmLit

    -- Darwin, but not x86_64:
    -- The PIC base register points to the PIC base label at the beginning
    -- of the current CmmDecl. We just have to use a label difference to
    -- get the offset.
    -- We have already made sure that all labels that are not from the current
    -- module are accessed indirectly ('as' can't calculate differences between
    -- undefined labels).
    picRelative :: Width -> Arch -> OS -> CLabel -> CmmLit
    picRelative Width
    width Arch
    arch OS
    OSDarwin CLabel
    lbl
            | Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Arch
    ArchX86_64
            = CLabel -> CLabel -> Int -> Width -> CmmLit
    CmmLabelDiffOff CLabel
    lbl CLabel
    mkPicBaseLabel Int
    0 Width
    width

    -- On AIX we use an indirect local TOC anchored by 'gotLabel'.
    -- This way we use up only one global TOC entry per compilation-unit
    -- (this is quite similar to GCC's @-mminimal-toc@ compilation mode)
    picRelative Width
    width Arch
    _ OS
    OSAIX CLabel
    lbl
            = CLabel -> CLabel -> Int -> Width -> CmmLit
    CmmLabelDiffOff CLabel
    lbl CLabel
    gotLabel Int
    0 Width
    width

    -- PowerPC Linux:
    -- The PIC base register points to our fake GOT. Use a label difference
    -- to get the offset.
    -- We have made sure that *everything* is accessed indirectly, so this
    -- is only used for offsets from the GOT to symbol pointers inside the
    -- GOT.
    picRelative Width
    width Arch
    ArchPPC OS
    os CLabel
    lbl
            | OS -> Bool
    osElfTarget OS
    os
            = CLabel -> CLabel -> Int -> Width -> CmmLit
    CmmLabelDiffOff CLabel
    lbl CLabel
    gotLabel Int
    0 Width
    width

    -- Most Linux versions:
    -- The PIC base register points to the GOT. Use foo@got for symbol
    -- pointers, and foo@gotoff for everything else.
    -- Linux and Darwin on x86_64:
    -- The PIC base register is %rip, we use foo@gotpcrel for symbol pointers,
    -- and a GotSymbolOffset label for other things.
    -- For reasons of tradition, the symbol offset label is written as a plain label.
    picRelative Width
    _ Arch
    arch OS
    os CLabel
    lbl
            | OS -> Bool
    osElfTarget OS
    os Bool -> Bool -> Bool
    || (OS
    os OS -> OS -> Bool
    forall a. Eq a => a -> a -> Bool
    == OS
    OSDarwin Bool -> Bool -> Bool
    && Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    == Arch
    ArchX86_64)
            = let   result :: CmmLit
    result
                            | Just (DynamicLinkerLabelInfo
    SymbolPtr, CLabel
    lbl') <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
    dynamicLinkerLabelInfo CLabel
    lbl
                            = CLabel -> CmmLit
    CmmLabel (CLabel -> CmmLit) -> CLabel -> CmmLit
    forall a b. (a -> b) -> a -> b
    $ DynamicLinkerLabelInfo -> CLabel -> CLabel
    mkDynamicLinkerLabel DynamicLinkerLabelInfo
    GotSymbolPtr CLabel
    lbl'

                            | Bool
    otherwise
                            = CLabel -> CmmLit
    CmmLabel (CLabel -> CmmLit) -> CLabel -> CmmLit
    forall a b. (a -> b) -> a -> b
    $ DynamicLinkerLabelInfo -> CLabel -> CLabel
    mkDynamicLinkerLabel DynamicLinkerLabelInfo
    GotSymbolOffset CLabel
    lbl

              in    CmmLit
    result

    picRelative Width
    _ Arch
    _ OS
    _ CLabel
    _
            = String -> CmmLit
    forall a. HasCallStack => String -> a
    panic String
    "GHC.CmmToAsm.PIC.picRelative undefined for this platform"

    --------------------------------------------------------------------------------

    needImportedSymbols :: NCGConfig -> Bool
    needImportedSymbols :: NCGConfig -> Bool
    needImportedSymbols NCGConfig
    config
            | OS
    os    OS -> OS -> Bool
    forall a. Eq a => a -> a -> Bool
    == OS
    OSDarwin
            , Arch
    arch  Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Arch
    ArchX86_64
            = Bool
    True

            | OS
    os    OS -> OS -> Bool
    forall a. Eq a => a -> a -> Bool
    == OS
    OSAIX
            = Bool
    True

            -- PowerPC Linux: -fPIC or -dynamic
            | OS -> Bool
    osElfTarget OS
    os
            , Arch
    arch  Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    == Arch
    ArchPPC
            = NCGConfig -> Bool
    ncgPIC NCGConfig
    config Bool -> Bool -> Bool
    || NCGConfig -> Bool
    ncgExternalDynamicRefs NCGConfig
    config

            -- PowerPC 64 Linux: always
            | OS -> Bool
    osElfTarget OS
    os
            , Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    == PPC_64ABI -> Arch
    ArchPPC_64 PPC_64ABI
    ELF_V1 Bool -> Bool -> Bool
    || Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    == PPC_64ABI -> Arch
    ArchPPC_64 PPC_64ABI
    ELF_V2
            = Bool
    True

            -- i386 (and others?): -dynamic but not -fPIC
            | OS -> Bool
    osElfTarget OS
    os
            , Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= PPC_64ABI -> Arch
    ArchPPC_64 PPC_64ABI
    ELF_V1 Bool -> Bool -> Bool
    && Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= PPC_64ABI -> Arch
    ArchPPC_64 PPC_64ABI
    ELF_V2
            = NCGConfig -> Bool
    ncgExternalDynamicRefs NCGConfig
    config Bool -> Bool -> Bool
    &&
              Bool -> Bool
    not (NCGConfig -> Bool
    ncgPIC NCGConfig
    config)

            | Bool
    otherwise
            = Bool
    False
       where
          platform :: Platform
    platform = NCGConfig -> Platform
    ncgPlatform NCGConfig
    config
          arch :: Arch
    arch     = Platform -> Arch
    platformArch Platform
    platform
          os :: OS
    os       = Platform -> OS
    platformOS   Platform
    platform

    -- gotLabel
    -- The label used to refer to our "fake GOT" from
    -- position-independent code.
    gotLabel :: CLabel
    gotLabel :: CLabel
    gotLabel
            -- HACK: this label isn't really foreign
            = FastString
    -> Maybe Int -> ForeignLabelSource -> FunctionOrData -> CLabel
    mkForeignLabel
                    (String -> FastString
    fsLit String
    ".LCTOC1")
                    Maybe Int
    forall a. Maybe a
    Nothing ForeignLabelSource
    ForeignLabelInThisPackage FunctionOrData
    IsData

    -- Emit GOT declaration
    -- Output whatever needs to be output once per .s file.
    --
    -- We don't need to declare any offset tables.
    -- However, for PIC on x86, we need a small helper function.
    pprGotDeclaration :: NCGConfig -> HDoc
    pprGotDeclaration :: NCGConfig -> HDoc
    pprGotDeclaration NCGConfig
    config = case (Arch
    arch,OS
    os) of
       (Arch
    ArchX86, OS
    OSDarwin)
            | NCGConfig -> Bool
    ncgPIC NCGConfig
    config
            -> [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".section __TEXT,__textcoal_nt,coalesced,no_toc",
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".weak_definition ___i686.get_pc_thunk.ax",
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".private_extern ___i686.get_pc_thunk.ax",
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "___i686.get_pc_thunk.ax:",
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tmovl (%esp), %eax",
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tret" ]

       (Arch
    _, OS
    OSDarwin) -> HDoc
    forall doc. IsOutput doc => doc
    empty

       -- Emit XCOFF TOC section
       (Arch
    _, OS
    OSAIX)
            -> [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [ String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".toc"
                      , String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".tc ghc_toc_table[TC],.LCTOC1"
                      , String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".csect ghc_toc_table[RW]"
                        -- See Note [.LCTOC1 in PPC PIC code]
                      , String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".set .LCTOC1,$+0x8000"
                      ]

       -- PPC 64 ELF v1 needs a Table Of Contents (TOC)
       (ArchPPC_64 PPC_64ABI
    ELF_V1, OS
    _)
            -> Line HDoc -> HDoc
    forall doc. IsDoc doc => Line doc -> doc
    line (Line HDoc -> HDoc) -> Line HDoc -> HDoc
    forall a b. (a -> b) -> a -> b
    $ String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".section \".toc\",\"aw\""

       -- In ELF v2 we also need to tell the assembler that we want ABI
       -- version 2. This would normally be done at the top of the file
       -- right after a file directive, but I could not figure out how
       -- to do that.
       (ArchPPC_64 PPC_64ABI
    ELF_V2, OS
    _)
            -> [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [ String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".abiversion 2",
                        String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".section \".toc\",\"aw\""
                      ]

       (Arch
    arch, OS
    os)
            | OS -> Bool
    osElfTarget OS
    os
            , Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= PPC_64ABI -> Arch
    ArchPPC_64 PPC_64ABI
    ELF_V1 Bool -> Bool -> Bool
    && Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= PPC_64ABI -> Arch
    ArchPPC_64 PPC_64ABI
    ELF_V2
            , Bool -> Bool
    not (NCGConfig -> Bool
    ncgPIC NCGConfig
    config)
            -> HDoc
    forall doc. IsOutput doc => doc
    empty

            | OS -> Bool
    osElfTarget OS
    os
            , Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= PPC_64ABI -> Arch
    ArchPPC_64 PPC_64ABI
    ELF_V1 Bool -> Bool -> Bool
    && Arch
    arch Arch -> Arch -> Bool
    forall a. Eq a => a -> a -> Bool
    /= PPC_64ABI -> Arch
    ArchPPC_64 PPC_64ABI
    ELF_V2
            -> [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [
                    -- See Note [.LCTOC1 in PPC PIC code]
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".section \".got2\",\"aw\"",
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".LCTOC1 = .+32768" ]

       (Arch, OS)
    _ -> String -> HDoc
    forall a. HasCallStack => String -> a
    panic String
    "pprGotDeclaration: no match"
     where
       platform :: Platform
    platform = NCGConfig -> Platform
    ncgPlatform NCGConfig
    config
       arch :: Arch
    arch     = Platform -> Arch
    platformArch Platform
    platform
       os :: OS
    os       = Platform -> OS
    platformOS   Platform
    platform

    --------------------------------------------------------------------------------
    -- On Darwin, we have to generate our own stub code for lazy binding..
    -- For each processor architecture, there are two versions, one for PIC
    -- and one for non-PIC.
    --

    pprImportedSymbol :: NCGConfig -> CLabel -> HDoc
    pprImportedSymbol :: NCGConfig -> CLabel -> HDoc
    pprImportedSymbol NCGConfig
    config CLabel
    importedLbl = case (Arch
    arch,OS
    os) of
       (Arch
    ArchX86, OS
    OSDarwin)
            | Just (DynamicLinkerLabelInfo
    CodeStub, CLabel
    lbl) <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
    dynamicLinkerLabelInfo CLabel
    importedLbl
            -> if Bool -> Bool
    not Bool
    pic
                 then
                  [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [
                      String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".symbol_stub",
                      String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$stub:",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.indirect_symbol" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <+> CLabel -> HLine
    ppr_lbl CLabel
    lbl,
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tjmp *L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl
                              HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$lazy_ptr",
                      String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl
                          HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$stub_binder:",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tpushl $L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl
                              HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$lazy_ptr",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tjmp dyld_stub_binding_helper"
                  ]
                 else
                  [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [
                      String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".section __TEXT,__picsymbolstub2,"
                          HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "symbol_stubs,pure_instructions,25",
                      String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$stub:",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.indirect_symbol" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <+> CLabel -> HLine
    ppr_lbl CLabel
    lbl,
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tcall ___i686.get_pc_thunk.ax",
                      String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "1:",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tmovl L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl
                              HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$lazy_ptr-1b(%eax),%edx",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tjmp *%edx",
                      String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl
                          HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$stub_binder:",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tlea L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl
                              HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$lazy_ptr-1b(%eax),%eax",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tpushl %eax",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\tjmp dyld_stub_binding_helper"
                  ]
               HDoc -> HDoc -> HDoc
    forall doc. IsDoc doc => doc -> doc -> doc
    $$ [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".section __DATA, __la_sym_ptr"
                        HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> (if Bool
    pic then Int -> HLine
    forall doc. IsLine doc => Int -> doc
    int Int
    2 else Int -> HLine
    forall doc. IsLine doc => Int -> doc
    int Int
    3)
                        HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ",lazy_symbol_pointers",
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$lazy_ptr:",
                        String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.indirect_symbol" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <+> CLabel -> HLine
    ppr_lbl CLabel
    lbl,
                        String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.long L" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl
                        HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$stub_binder"]

            | Just (DynamicLinkerLabelInfo
    SymbolPtr, CLabel
    lbl) <- CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
    dynamicLinkerLabelInfo CLabel
    importedLbl
            -> [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".non_lazy_symbol_pointer",
                    Char -> HLine
    forall doc. IsLine doc => Char -> doc
    char Char
    'L' HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "$non_lazy_ptr:",
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.indirect_symbol" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <+> CLabel -> HLine
    ppr_lbl CLabel
    lbl,
                    String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.long\t0"]

            | Bool
    otherwise
            -> HDoc
    forall doc. IsOutput doc => doc
    empty

       (Arch
    ArchAArch64, OS
    OSDarwin)
            -> HDoc
    forall doc. IsOutput doc => doc
    empty

       -- XCOFF / AIX
       --
       -- Similar to PPC64 ELF v1, there's dedicated TOC register (r2). To
       -- workaround the limitation of a global TOC we use an indirect TOC
       -- with the label `ghc_toc_table`.
       --
       -- See also GCC's `-mminimal-toc` compilation mode or
       -- http://www.ibm.com/developerworks/rational/library/overview-toc-aix/
       --
       -- NB: No DSO-support yet

       (Arch
    _, OS
    OSAIX) -> case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
    dynamicLinkerLabelInfo CLabel
    importedLbl of
                Just (DynamicLinkerLabelInfo
    SymbolPtr, CLabel
    lbl)
                  -> [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [
                       String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "LC.." HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> Char -> HLine
    forall doc. IsLine doc => Char -> doc
    char Char
    ':',
                       String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.long" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <+> CLabel -> HLine
    ppr_lbl CLabel
    lbl ]
                Maybe (DynamicLinkerLabelInfo, CLabel)
    _ -> HDoc
    forall doc. IsOutput doc => doc
    empty

       -- ELF / Linux
       --
       -- In theory, we don't need to generate any stubs or symbol pointers
       -- by hand for Linux.
       --
       -- Reality differs from this in two areas.
       --
       -- 1) If we just use a dynamically imported symbol directly in a read-only
       --    section of the main executable (as GCC does), ld generates R_*_COPY
       --    relocations, which are fundamentally incompatible with reversed info
       --    tables. Therefore, we need a table of imported addresses in a writable
       --    section.
       --    The "official" GOT mechanism (label@got) isn't intended to be used
       --    in position dependent code, so we have to create our own "fake GOT"
       --    when not Opt_PIC && WayDyn `elem` ways dflags.
       --
       -- 2) PowerPC Linux is just plain broken.
       --    While it's theoretically possible to use GOT offsets larger
       --    than 16 bit, the standard crt*.o files don't, which leads to
       --    linker errors as soon as the GOT size exceeds 16 bit.
       --    Also, the assembler doesn't support @gotoff labels.
       --    In order to be able to use a larger GOT, we have to circumvent the
       --    entire GOT mechanism and do it ourselves (this is also what GCC does).

       -- When needImportedSymbols is defined,
       -- the NCG will keep track of all DynamicLinkerLabels it uses
       -- and output each of them using pprImportedSymbol.

       (ArchPPC_64 PPC_64ABI
    _, OS
    _)
            | OS -> Bool
    osElfTarget OS
    os
            -> case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
    dynamicLinkerLabelInfo CLabel
    importedLbl of
                Just (DynamicLinkerLabelInfo
    SymbolPtr, CLabel
    lbl)
                  -> [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [
                       String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".LC_" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> Char -> HLine
    forall doc. IsLine doc => Char -> doc
    char Char
    ':',
                       String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.quad" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <+> CLabel -> HLine
    ppr_lbl CLabel
    lbl ]
                Maybe (DynamicLinkerLabelInfo, CLabel)
    _ -> HDoc
    forall doc. IsOutput doc => doc
    empty

       (Arch, OS)
    _ | OS -> Bool
    osElfTarget OS
    os
         -> case CLabel -> Maybe (DynamicLinkerLabelInfo, CLabel)
    dynamicLinkerLabelInfo CLabel
    importedLbl of
                Just (DynamicLinkerLabelInfo
    SymbolPtr, CLabel
    lbl)
                  -> let symbolSize :: HLine
    symbolSize = case NCGConfig -> Width
    ncgWordWidth NCGConfig
    config of
                             Width
    W32 -> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.long"
                             Width
    W64 -> String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    "\t.quad"
                             Width
    _ -> String -> HLine
    forall a. HasCallStack => String -> a
    panic String
    "Unknown wordRep in pprImportedSymbol"

                     in [Line HDoc] -> HDoc
    forall doc. IsDoc doc => [Line doc] -> doc
    lines_ [
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".section \".got2\", \"aw\"",
                          String -> HLine
    forall doc. IsLine doc => String -> doc
    text String
    ".LC_" HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> CLabel -> HLine
    ppr_lbl CLabel
    lbl HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <> Char -> HLine
    forall doc. IsLine doc => Char -> doc
    char Char
    ':',
                          HLine
    symbolSize HLine -> HLine -> HLine
    forall doc. IsLine doc => doc -> doc -> doc
    <+> CLabel -> HLine
    ppr_lbl CLabel
    lbl ]

                -- PLT code stubs are generated automatically by the dynamic linker.
                Maybe (DynamicLinkerLabelInfo, CLabel)
    _ -> HDoc
    forall doc. IsOutput doc => doc
    empty

       (Arch, OS)
    _ -> String -> HDoc
    forall a. HasCallStack => String -> a
    panic String
    "PIC.pprImportedSymbol: no match"
     where
       platform :: Platform
    platform = NCGConfig -> Platform
    ncgPlatform NCGConfig
    config
       ppr_lbl :: CLabel -> HLine
       ppr_lbl :: CLabel -> HLine
    ppr_lbl  = Platform -> CLabel -> HLine
    forall doc. IsLine doc => Platform -> CLabel -> doc
    pprAsmLabel   Platform
    platform
       arch :: Arch
    arch     = Platform -> Arch
    platformArch  Platform
    platform
       os :: OS
    os       = Platform -> OS
    platformOS    Platform
    platform
       pic :: Bool
    pic      = NCGConfig -> Bool
    ncgPIC NCGConfig
    config

    --------------------------------------------------------------------------------
    -- Generate code to calculate the address that should be put in the
    -- PIC base register.
    -- This is called by MachCodeGen for every CmmProc that accessed the
    -- PIC base register. It adds the appropriate instructions to the
    -- top of the CmmProc.

    -- It is assumed that the first NatCmmDecl in the input list is a Proc
    -- and the rest are CmmDatas.

    -- Darwin is simple: just fetch the address of a local label.
    -- The FETCHPC pseudo-instruction is expanded to multiple instructions
    -- during pretty-printing so that we don't have to deal with the
    -- local label:

    -- PowerPC version:
    --          bcl 20,31,1f.
    --      1:  mflr picReg

    -- i386 version:
    --          call 1f
    --      1:  popl %picReg

    -- Get a pointer to our own fake GOT, which is defined on a per-module basis.
    -- This is exactly how GCC does it in linux.

    initializePicBase_ppc
            :: Arch -> OS -> Reg
            -> [NatCmmDecl RawCmmStatics PPC.Instr]
            -> NatM [NatCmmDecl RawCmmStatics PPC.Instr]

    initializePicBase_ppc :: Arch
    -> OS
    -> Reg
    -> [NatCmmDecl RawCmmStatics Instr]
    -> NatM [NatCmmDecl RawCmmStatics Instr]
    initializePicBase_ppc Arch
    ArchPPC OS
    os Reg
    picReg
        (CmmProc LabelMap RawCmmStatics
    info CLabel
    lab [GlobalReg]
    live (ListGraph [GenBasicBlock Instr]
    blocks) : [NatCmmDecl RawCmmStatics Instr]
    statics)
        | OS -> Bool
    osElfTarget OS
    os
        = do
            let
                gotOffset :: Imm
    gotOffset = Imm -> Imm -> Imm
    PPC.ImmConstantDiff
                                    (CLabel -> Imm
    PPC.ImmCLbl CLabel
    gotLabel)
                                    (CLabel -> Imm
    PPC.ImmCLbl CLabel
    mkPicBaseLabel)

                blocks' :: [GenBasicBlock Instr]
    blocks' = case [GenBasicBlock Instr]
    blocks of
                           [] -> []
                           (GenBasicBlock Instr
    b:[GenBasicBlock Instr]
    bs) -> GenBasicBlock Instr -> GenBasicBlock Instr
    fetchPC GenBasicBlock Instr
    b GenBasicBlock Instr
    -> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
    forall a. a -> [a] -> [a]
    : (GenBasicBlock Instr -> GenBasicBlock Instr)
    -> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
    forall a b. (a -> b) -> [a] -> [b]
    map GenBasicBlock Instr -> GenBasicBlock Instr
    maybeFetchPC [GenBasicBlock Instr]
    bs

                maybeFetchPC :: GenBasicBlock Instr -> GenBasicBlock Instr
    maybeFetchPC b :: GenBasicBlock Instr
    b@(BasicBlock BlockId
    bID [Instr]
    _)
                  | BlockId
    bID BlockId -> LabelMap RawCmmStatics -> Bool
    forall a. BlockId -> LabelMap a -> Bool
    `mapMember` LabelMap RawCmmStatics
    info = GenBasicBlock Instr -> GenBasicBlock Instr
    fetchPC GenBasicBlock Instr
    b
                  | Bool
    otherwise            = GenBasicBlock Instr
    b

                -- GCC does PIC prologs thusly:
                --     bcl 20,31,.L1
                -- .L1:
                --     mflr 30
                --     addis 30,30,.LCTOC1-.L1@ha
                --     addi 30,30,.LCTOC1-.L1@l
                -- TODO: below we use it over temporary register,
                -- it can and should be optimised by picking
                -- correct PIC reg.
                fetchPC :: GenBasicBlock Instr -> GenBasicBlock Instr
    fetchPC (BasicBlock BlockId
    bID [Instr]
    insns) =
                  BlockId -> [Instr] -> GenBasicBlock Instr
    forall i. BlockId -> [i] -> GenBasicBlock i
    BasicBlock BlockId
    bID (Reg -> Instr
    PPC.FETCHPC Reg
    picReg
                                  Instr -> [Instr] -> [Instr]
    forall a. a -> [a] -> [a]
    : Reg -> Reg -> Imm -> Instr
    PPC.ADDIS Reg
    picReg Reg
    picReg (Imm -> Imm
    PPC.HA Imm
    gotOffset)
                                  Instr -> [Instr] -> [Instr]
    forall a. a -> [a] -> [a]
    : Reg -> Reg -> RI -> Instr
    PPC.ADD Reg
    picReg Reg
    picReg
                                            (Imm -> RI
    PPC.RIImm (Imm -> Imm
    PPC.LO Imm
    gotOffset))
                                  Instr -> [Instr] -> [Instr]
    forall a. a -> [a] -> [a]
    : Reg -> Reg -> Instr
    PPC.MR Reg
    PPC.r30 Reg
    picReg
                                  Instr -> [Instr] -> [Instr]
    forall a. a -> [a] -> [a]
    : [Instr]
    insns)

            [NatCmmDecl RawCmmStatics Instr]
    -> NatM [NatCmmDecl RawCmmStatics Instr]
    forall a. a -> NatM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (LabelMap RawCmmStatics
    -> CLabel
    -> [GlobalReg]
    -> ListGraph Instr
    -> NatCmmDecl RawCmmStatics Instr
    forall d h g. h -> CLabel -> [GlobalReg] -> g -> GenCmmDecl d h g
    CmmProc LabelMap RawCmmStatics
    info CLabel
    lab [GlobalReg]
    live ([GenBasicBlock Instr] -> ListGraph Instr
    forall i. [GenBasicBlock i] -> ListGraph i
    ListGraph [GenBasicBlock Instr]
    blocks') NatCmmDecl RawCmmStatics Instr
    -> [NatCmmDecl RawCmmStatics Instr]
    -> [NatCmmDecl RawCmmStatics Instr]
    forall a. a -> [a] -> [a]
    : [NatCmmDecl RawCmmStatics Instr]
    statics)

    -------------------------------------------------------------------------
    -- Load TOC into register 2
    -- PowerPC 64-bit ELF ABI 2.0 requires the address of the callee
    -- in register 12.
    -- We pass the label to FETCHTOC and create a .localentry too.
    -- TODO: Explain this better and refer to ABI spec!
    {-
    We would like to do approximately this, but spill slot allocation
    might be added before the first BasicBlock. That violates the ABI.

    For now we will emit the prologue code in the pretty printer,
    which is also what we do for ELF v1.
    initializePicBase_ppc (ArchPPC_64 ELF_V2) OSLinux picReg
            (CmmProc info lab live (ListGraph (entry:blocks)) : statics)
            = do
               bID <-getUniqueM
               return (CmmProc info lab live (ListGraph (b':entry:blocks))
                                             : statics)
            where   BasicBlock entryID _ = entry
                    b' = BasicBlock bID [PPC.FETCHTOC picReg lab,
                                         PPC.BCC PPC.ALWAYS entryID]
    -}

    initializePicBase_ppc Arch
    _ OS
    _ Reg
    _ [NatCmmDecl RawCmmStatics Instr]
    _
            = String -> NatM [NatCmmDecl RawCmmStatics Instr]
    forall a. HasCallStack => String -> a
    panic String
    "initializePicBase_ppc: not needed"

    -- We cheat a bit here by defining a pseudo-instruction named FETCHGOT
    -- which pretty-prints as:
    --              call 1f
    -- 1:           popl %picReg
    --              addl __GLOBAL_OFFSET_TABLE__+.-1b, %picReg
    -- (See PprMach.hs)

    initializePicBase_x86
            :: Arch -> OS -> Reg
            -> [NatCmmDecl (Alignment, RawCmmStatics) X86.Instr]
            -> NatM [NatCmmDecl (Alignment, RawCmmStatics) X86.Instr]

    initializePicBase_x86 :: Arch
    -> OS
    -> Reg
    -> [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    -> NatM [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    initializePicBase_x86 Arch
    ArchX86 OS
    os Reg
    picReg
            (CmmProc LabelMap RawCmmStatics
    info CLabel
    lab [GlobalReg]
    live (ListGraph [GenBasicBlock Instr]
    blocks) : [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    statics)
        | OS -> Bool
    osElfTarget OS
    os
        = [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    -> NatM [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    forall a. a -> NatM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (LabelMap RawCmmStatics
    -> CLabel
    -> [GlobalReg]
    -> ListGraph Instr
    -> NatCmmDecl (Alignment, RawCmmStatics) Instr
    forall d h g. h -> CLabel -> [GlobalReg] -> g -> GenCmmDecl d h g
    CmmProc LabelMap RawCmmStatics
    info CLabel
    lab [GlobalReg]
    live ([GenBasicBlock Instr] -> ListGraph Instr
    forall i. [GenBasicBlock i] -> ListGraph i
    ListGraph [GenBasicBlock Instr]
    blocks') NatCmmDecl (Alignment, RawCmmStatics) Instr
    -> [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    -> [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    forall a. a -> [a] -> [a]
    : [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    statics)
        where blocks' :: [GenBasicBlock Instr]
    blocks' = case [GenBasicBlock Instr]
    blocks of
                         [] -> []
                         (GenBasicBlock Instr
    b:[GenBasicBlock Instr]
    bs) -> GenBasicBlock Instr -> GenBasicBlock Instr
    fetchGOT GenBasicBlock Instr
    b GenBasicBlock Instr
    -> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
    forall a. a -> [a] -> [a]
    : (GenBasicBlock Instr -> GenBasicBlock Instr)
    -> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
    forall a b. (a -> b) -> [a] -> [b]
    map GenBasicBlock Instr -> GenBasicBlock Instr
    maybeFetchGOT [GenBasicBlock Instr]
    bs

              -- we want to add a FETCHGOT instruction to the beginning of
              -- every block that is an entry point, which corresponds to
              -- the blocks that have entries in the info-table mapping.
              maybeFetchGOT :: GenBasicBlock Instr -> GenBasicBlock Instr
    maybeFetchGOT b :: GenBasicBlock Instr
    b@(BasicBlock BlockId
    bID [Instr]
    _)
                | BlockId
    bID BlockId -> LabelMap RawCmmStatics -> Bool
    forall a. BlockId -> LabelMap a -> Bool
    `mapMember` LabelMap RawCmmStatics
    info = GenBasicBlock Instr -> GenBasicBlock Instr
    fetchGOT GenBasicBlock Instr
    b
                | Bool
    otherwise            = GenBasicBlock Instr
    b

              fetchGOT :: GenBasicBlock Instr -> GenBasicBlock Instr
    fetchGOT (BasicBlock BlockId
    bID [Instr]
    insns) =
                 BlockId -> [Instr] -> GenBasicBlock Instr
    forall i. BlockId -> [i] -> GenBasicBlock i
    BasicBlock BlockId
    bID (Reg -> Instr
    X86.FETCHGOT Reg
    picReg Instr -> [Instr] -> [Instr]
    forall a. a -> [a] -> [a]
    : [Instr]
    insns)

    initializePicBase_x86 Arch
    ArchX86 OS
    OSDarwin Reg
    picReg
            (CmmProc LabelMap RawCmmStatics
    info CLabel
    lab [GlobalReg]
    live (ListGraph (GenBasicBlock Instr
    entry:[GenBasicBlock Instr]
    blocks)) : [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    statics)
            = [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    -> NatM [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    forall a. a -> NatM a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (LabelMap RawCmmStatics
    -> CLabel
    -> [GlobalReg]
    -> ListGraph Instr
    -> NatCmmDecl (Alignment, RawCmmStatics) Instr
    forall d h g. h -> CLabel -> [GlobalReg] -> g -> GenCmmDecl d h g
    CmmProc LabelMap RawCmmStatics
    info CLabel
    lab [GlobalReg]
    live ([GenBasicBlock Instr] -> ListGraph Instr
    forall i. [GenBasicBlock i] -> ListGraph i
    ListGraph (GenBasicBlock Instr
    block'GenBasicBlock Instr
    -> [GenBasicBlock Instr] -> [GenBasicBlock Instr]
    forall a. a -> [a] -> [a]
    :[GenBasicBlock Instr]
    blocks)) NatCmmDecl (Alignment, RawCmmStatics) Instr
    -> [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    -> [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    forall a. a -> [a] -> [a]
    : [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    statics)

        where BasicBlock BlockId
    bID [Instr]
    insns = GenBasicBlock Instr
    entry
              block' :: GenBasicBlock Instr
    block' = BlockId -> [Instr] -> GenBasicBlock Instr
    forall i. BlockId -> [i] -> GenBasicBlock i
    BasicBlock BlockId
    bID (Reg -> Instr
    X86.FETCHPC Reg
    picReg Instr -> [Instr] -> [Instr]
    forall a. a -> [a] -> [a]
    : [Instr]
    insns)

    initializePicBase_x86 Arch
    _ OS
    _ Reg
    _ [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    _
            = String -> NatM [NatCmmDecl (Alignment, RawCmmStatics) Instr]
    forall a. HasCallStack => String -> a
    panic String
    "initializePicBase_x86: not needed"