Change runtime linker to perform lazy loading of symbols/sections
authorTamar Christina <tamar@zhox.com>
Sun, 10 Apr 2016 22:38:42 +0000 (00:38 +0200)
committerBen Gamari <ben@smart-cactus.org>
Sun, 10 Apr 2016 23:45:14 +0000 (01:45 +0200)
commit068d9273f0a427cbab4ea95cfca211ec127dc785
treed81283d9ce429b374d306efb5980ce075bfca0e2
parent5a361d89cc457c798e0992cb8fddd16a3ecb9218
Change runtime linker to perform lazy loading of symbols/sections

The Runtime Linker is currently eagerly loading all object files on all
platforms which do not use the system linker for `GHCi`.

The problem with this approach is that it requires all symbols to be
found.  Even those of functions never used/called. This makes the number
of libraries required to link things like `mingwex` quite high.

To work around this the `rts` was relying on a trick. It itself was
compiled with `MingW64-w`'s `GCC`. So it was already linked against
`mingwex`.  As such, it re-exported the symbols from itself.

While this worked it made it impossible to link against `mingwex` in
user libraries. And with this means no `C99` code could ever run in
`GHCi` on Windows without having the required symbols re-exported from
the rts.

Consequently this rules out a large number of packages on Windows.
SDL2, HMatrix etc.

After talking with @rwbarton I have taken the approach of loading entire
object files when a symbol is needed instead of doing the dependency
tracking on a per symbol basis. This is a lot less fragile and a lot
less complicated to implement.

The changes come down to the following steps:

1) modify the linker to and introduce a new state for ObjectCode:
   `Needed`.  A Needed object is one that is required for the linking to
   succeed.  The initial set consists of all Object files passed as
   arguments to the link.

2) Change `ObjectCode`'s to be indexed but not initialized or resolved.
   This means we know where we would load the symbols,
   but haven't actually done so.

3) Mark any `ObjectCode` belonging to `.o` passed as argument
   as required: ObjectState `NEEDED`.

4) During `Resolve` object calls, mark all `ObjectCode`
   containing the required symbols as `NEEDED`

5) During `lookupSymbol` lookups, (which is called from `linkExpr`
   and `linkDecl` in `GHCI.hs`) is the symbol is in a not-yet-loaded
   `ObjectCode` then load the `ObjectCode` on demand and return the
   address of the symbol. Otherwise produce an unresolved symbols error
   as expected.

6) On `unloadObj` we then change the state of the object and remove
   it's symbols from the `reqSymHash` table so it can be reloaded.

This change affects all platforms and OSes which use the runtime linker.
It seems there are no real perf tests for `GHCi`, but performance
shouldn't be impacted much. We gain a lot of time not loading all `obj`
files, and we lose some time in `lookupSymbol` when we're finding
sections that have to be loaded. The actual finding itself is O(1)
(Assuming the hashtnl is perfect)

It also consumes slighly more memory as instead of storing just the
address of a symbol I also store some other information, like if the
symbol is weak or not.

This change will break any packages relying on renamed POSIX functions
that were re-named and re-exported by the rts. Any packages following
the proper naming for functions as found on MSDN will work fine.

Test Plan: ./validate on all platforms which use the Runtime linker.

Reviewers: thomie, rwbarton, simonmar, erikd, bgamari, austin, hvr

Reviewed By: erikd

Subscribers: kgardas, gridaphobe, RyanGlScott, simonmar,
             rwbarton, #ghc_windows_task_force

Differential Revision: https://phabricator.haskell.org/D1805

GHC Trac Issues: #11223

(cherry picked from commit 90538d86af579595987826cd893828d6f379f35a)
33 files changed:
compiler/main/SysTools.hs
configure.ac
docs/users_guide/8.0.1-notes.rst
libraries/base/System/Posix/Internals.hs
libraries/base/base.cabal
libraries/base/include/HsBase.h
libraries/ghc-prim/ghc-prim.cabal
rts/Linker.c
rts/LinkerInternals.h
rts/RtsSymbols.c
testsuite/tests/ghci/linking/dyn/all.T
testsuite/tests/rts/T11223/Makefile [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_link_order_a_b_2_fail.stderr [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_link_order_a_b_2_fail.stderr-mingw32 [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_link_order_a_b_succeed.stdout [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_link_order_b_a_2_succeed.stdout [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_link_order_b_a_succeed.stdout [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_simple_duplicate_lib.stderr [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_simple_duplicate_lib.stderr-mingw32 [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_simple_duplicate_lib.stderr.normalised-mingw32 [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_simple_link.stdout [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_simple_link_lib.stdout [new file with mode: 0644]
testsuite/tests/rts/T11223/T11223_simple_unused_duplicate_lib.stdout [new file with mode: 0644]
testsuite/tests/rts/T11223/all.T [new file with mode: 0644]
testsuite/tests/rts/T11223/bar.c [new file with mode: 0644]
testsuite/tests/rts/T11223/foo.c [new file with mode: 0644]
testsuite/tests/rts/T11223/foo.hs [new file with mode: 0644]
testsuite/tests/rts/T11223/foo2.hs [new file with mode: 0644]
testsuite/tests/rts/T11223/foo3.hs [new file with mode: 0644]
testsuite/tests/rts/T11223/power.c [new file with mode: 0644]
testsuite/tests/rts/T11223/power.hs [new file with mode: 0644]
testsuite/tests/rts/T11223/power3.hs [new file with mode: 0644]
testsuite/tests/rts/T11223/power_slow.c [new file with mode: 0644]