Commit d99a65a8 authored by Simon Marlow's avatar Simon Marlow Committed by Simon Marlow

Add -fexternal-dynamic-refs

Summary:
The `-dynamic` flag does two things:

* In the code generator, it generates code designed to link against
  external shared libraries.  References outside of the current module
  go through platform-specific indirection tables (e.g. the GOT on ELF).

* It enables a "way", which changes which hi files we look
  for (`Foo.dyn_hi`) and which libraries we link against.

Some specialised applications want the first of these without the
second. (I could go into detail here but it's probably not all that
important).

This diff splits out the code-generation effects of `-dynamic` from the
"way" parts of its behaviour, via a new flag `-fexternal-dynamic-refs`.

Test Plan: validate

Reviewers: niteria, bgamari, erikd

Subscribers: rwbarton, thomie, carter

Differential Revision: https://phabricator.haskell.org/D4477
parent 43fbb905
......@@ -982,17 +982,19 @@ labelDynamic :: DynFlags -> Module -> CLabel -> Bool
labelDynamic dflags this_mod lbl =
case lbl of
-- is the RTS in a DLL or not?
RtsLabel _ -> (WayDyn `elem` ways dflags) && (this_pkg /= rtsUnitId)
RtsLabel _ ->
(gopt Opt_ExternalDynamicRefs dflags) && (this_pkg /= rtsUnitId)
IdLabel n _ _ -> isDllName dflags this_mod n
IdLabel n _ _ ->
isDllName dflags this_mod n
-- When compiling in the "dyn" way, each package is to be linked into
-- its own shared library.
CmmLabel pkg _ _
| os == OSMinGW32 ->
(WayDyn `elem` ways dflags) && (this_pkg /= pkg)
(gopt Opt_ExternalDynamicRefs dflags) && (this_pkg /= pkg)
| otherwise ->
True
gopt Opt_ExternalDynamicRefs dflags
LocalBlockLabel _ -> False
......@@ -1010,14 +1012,15 @@ labelDynamic dflags this_mod lbl =
-- When compiling in the "dyn" way, each package is to be
-- linked into its own DLL.
ForeignLabelInPackage pkgId ->
(WayDyn `elem` ways dflags) && (this_pkg /= pkgId)
(gopt Opt_ExternalDynamicRefs dflags) && (this_pkg /= pkgId)
else -- On Mac OS X and on ELF platforms, false positives are OK,
-- so we claim that all foreign imports come from dynamic
-- libraries
True
HpcTicksLabel m -> (WayDyn `elem` ways dflags) && this_mod /= m
HpcTicksLabel m ->
(gopt Opt_ExternalDynamicRefs dflags) && this_mod /= m
-- Note that DynamicLinkerLabels do NOT require dynamic linking themselves.
_ -> False
......
......@@ -539,6 +539,7 @@ data GeneralFlag
| Opt_PIC -- ^ @-fPIC@
| Opt_PIE -- ^ @-fPIE@
| Opt_PICExecutable -- ^ @-pie@
| Opt_ExternalDynamicRefs
| Opt_SccProfilingOn
| Opt_Ticky
| Opt_Ticky_Allocd
......@@ -1564,7 +1565,7 @@ wayGeneralFlags :: Platform -> Way -> [GeneralFlag]
wayGeneralFlags _ (WayCustom {}) = []
wayGeneralFlags _ WayThreaded = []
wayGeneralFlags _ WayDebug = []
wayGeneralFlags _ WayDyn = [Opt_PIC]
wayGeneralFlags _ WayDyn = [Opt_PIC, Opt_ExternalDynamicRefs]
-- We could get away without adding -fPIC when compiling the
-- modules of a program that is to be linked with -dynamic; the
-- program itself does not need to be position-independent, only
......@@ -3905,6 +3906,7 @@ fFlagsDeps = [
flagSpec "error-spans" Opt_ErrorSpans,
flagSpec "excess-precision" Opt_ExcessPrecision,
flagSpec "expose-all-unfoldings" Opt_ExposeAllUnfoldings,
flagSpec "external-dynamic-refs" Opt_ExternalDynamicRefs,
flagSpec "external-interpreter" Opt_ExternalInterpreter,
flagSpec "flat-cache" Opt_FlatCache,
flagSpec "float-in" Opt_FloatIn,
......
......@@ -178,7 +178,8 @@ cmmMakePicReference dflags lbl
(platformOS $ targetPlatform dflags)
lbl ]
| (positionIndependent dflags || WayDyn `elem` ways dflags) && absoluteLabel lbl
| (positionIndependent dflags || gopt Opt_ExternalDynamicRefs dflags)
&& absoluteLabel lbl
= CmmMachOp (MO_Add (wordWidth dflags))
[ CmmReg (CmmGlobal PicBaseReg)
, CmmLit $ picRelative
......@@ -238,7 +239,7 @@ howToAccessLabel
howToAccessLabel dflags _ OSMinGW32 this_mod _ lbl
-- Assume all symbols will be in the same PE, so just access them directly.
| WayDyn `notElem` ways dflags
| not (gopt Opt_ExternalDynamicRefs dflags)
= AccessDirectly
-- If the target symbol is in another PE we need to access it via the
......@@ -339,7 +340,8 @@ howToAccessLabel dflags _ os _ _ _
-- if we don't dynamically link to Haskell code,
-- it actually manages to do so without messing things up.
| osElfTarget os
, not (positionIndependent dflags) && WayDyn `notElem` ways dflags
, not (positionIndependent dflags) &&
not (gopt Opt_ExternalDynamicRefs dflags)
= AccessDirectly
howToAccessLabel dflags arch os this_mod DataReference lbl
......@@ -470,7 +472,7 @@ needImportedSymbols dflags arch os
-- PowerPC Linux: -fPIC or -dynamic
| osElfTarget os
, arch == ArchPPC
= positionIndependent dflags || WayDyn `elem` ways dflags
= positionIndependent dflags || gopt Opt_ExternalDynamicRefs dflags
-- PowerPC 64 Linux: always
| osElfTarget os
......@@ -480,7 +482,8 @@ needImportedSymbols dflags arch os
-- i386 (and others?): -dynamic but not -fPIC
| osElfTarget os
, arch /= ArchPPC_64 ELF_V1 && arch /= ArchPPC_64 ELF_V2
= WayDyn `elem` ways dflags && not (positionIndependent dflags)
= gopt Opt_ExternalDynamicRefs dflags &&
not (positionIndependent dflags)
| otherwise
= False
......
......@@ -56,6 +56,11 @@ Compiler
- GHC now supports British spelling of :extension:`GeneralizedNewtypeDeriving`.
- The code-generation effects of :ghc-flag:`-dynamic` can now be
enabled independently by the flag
:ghc-flag:`-fexternal-dynamic-refs`. If you don't know why you might
need this, you don't need it.
Runtime system
~~~~~~~~~~~~~~
......
......@@ -580,6 +580,15 @@ Options affecting code generation
Windows, position-independent code is never used so the flag is a
no-op on that platform.
.. ghc-flag:: -fexternal-dynamic-refs
:shortdesc: Generate code for linking against dynamic libraries
:type: dynamic
:category: codegen
When generating code, assume that entities imported from a
different module might be dynamically linked. This flag is enabled
automatically by :ghc-flag:`-dynamic`.
.. ghc-flag:: -fPIE
:shortdesc: Generate code for a position-independent executable (where available)
:type: dynamic
......@@ -596,12 +605,11 @@ Options affecting code generation
:category: codegen
:noindex:
When generating code, assume that entities imported from a different
package will be dynamically linked. This can reduce code size
tremendously, but may slow-down cross-package calls of non-inlined
functions. There can be some complications combining :ghc-flag:`-shared`
with this flag relating to linking in the RTS under Linux. See
:ghc-ticket:`10352`.
Build code for dynamic linking. This can reduce code size
tremendously, but may slow-down cross-module calls of non-inlined
functions. There can be some complications combining
:ghc-flag:`-shared` with this flag relating to linking in the RTS
under Linux. See :ghc-ticket:`10352`.
Note that using this option when linking causes GHC to link against
shared libraries.
......
......@@ -6,6 +6,7 @@ with the following modifiers:
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fno-diagnostics-show-caret
-fexternal-dynamic-refs
-fignore-optim-changes
-fignore-hpc-changes
-fno-ghci-history
......@@ -24,6 +25,7 @@ with the following modifiers:
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fno-diagnostics-show-caret
-fexternal-dynamic-refs
-fignore-optim-changes
-fignore-hpc-changes
-fno-ghci-history
......@@ -41,6 +43,7 @@ with the following modifiers:
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fno-diagnostics-show-caret
-fexternal-dynamic-refs
-fignore-optim-changes
-fignore-hpc-changes
-fno-ghci-history
......@@ -60,6 +63,7 @@ with the following modifiers:
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fno-diagnostics-show-caret
-fexternal-dynamic-refs
-fignore-optim-changes
-fignore-hpc-changes
-fno-ghci-history
......
......@@ -7,6 +7,7 @@ with the following modifiers:
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fno-diagnostics-show-caret
-fexternal-dynamic-refs
-fignore-optim-changes
-fignore-hpc-changes
-fno-ghci-history
......
......@@ -6,6 +6,7 @@ with the following modifiers:
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fno-diagnostics-show-caret
-fexternal-dynamic-refs
-fignore-optim-changes
-fignore-hpc-changes
-fno-ghci-history
......@@ -24,6 +25,7 @@ with the following modifiers:
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fno-diagnostics-show-caret
-fexternal-dynamic-refs
-fignore-optim-changes
-fignore-hpc-changes
-fno-ghci-history
......@@ -41,6 +43,7 @@ with the following modifiers:
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fno-diagnostics-show-caret
-fexternal-dynamic-refs
-fignore-optim-changes
-fignore-hpc-changes
-fno-ghci-history
......@@ -60,6 +63,7 @@ with the following modifiers:
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fno-diagnostics-show-caret
-fexternal-dynamic-refs
-fignore-optim-changes
-fignore-hpc-changes
-fno-ghci-history
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment