Link command line libs to temp so
authorPeter Trommler <ptrommler@acm.org>
Fri, 15 Jan 2016 12:25:38 +0000 (13:25 +0100)
committerBen Gamari <ben@smart-cactus.org>
Fri, 15 Jan 2016 12:25:53 +0000 (13:25 +0100)
Symbols in libraries specified on the GHCis command line are
not available to compiled modules because shared libraries
are loaded with local scope. So we link all libraries specified
on the command line into each temporary shared library.

Test Plan: validate

Reviewers: simonmar, hvr, austin, bgamari

Reviewed By: bgamari

Subscribers: thomie

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

GHC Trac Issues: #10458

compiler/ghci/Linker.hs
testsuite/tests/ghci/linking/dyn/Makefile
testsuite/tests/ghci/linking/dyn/T10458.hs [new file with mode: 0644]
testsuite/tests/ghci/linking/dyn/T10458.script [new file with mode: 0644]
testsuite/tests/ghci/linking/dyn/T10458.stdout [new file with mode: 0644]
testsuite/tests/ghci/linking/dyn/all.T

index 8f1107f..44d272e 100644 (file)
@@ -846,6 +846,8 @@ dynLoadObjs _       pls []   = return pls
 dynLoadObjs hsc_env pls objs = do
     let dflags = hsc_dflags hsc_env
     let platform = targetPlatform dflags
+    let minus_ls = [ lib | Option ('-':'l':lib) <- ldInputs dflags ]
+    let minus_big_ls = [ lib | Option ('-':'L':lib) <- ldInputs dflags ]
     (soFile, libPath , libName) <- newTempLibName dflags (soExt platform)
     let
         dflags2 = dflags {
@@ -863,7 +865,17 @@ dynLoadObjs hsc_env pls objs = do
                                  , Option ("-Wl," ++ lp)
                                  , Option ("-l" ++  l)
                                  ])
-                            (temp_sos pls),
+                            (temp_sos pls)
+                        ++ concatMap
+                             (\lp ->
+                                 [ Option ("-L" ++ lp)
+                                 , Option ("-Wl,-rpath")
+                                 , Option ("-Wl," ++ lp)
+                                 ])
+                             minus_big_ls
+                        ++ map (\l -> Option ("-l" ++ l)) minus_ls,
+                      -- Add -l options and -L options from dflags.
+                      --
                       -- When running TH for a non-dynamic way, we still
                       -- need to make -l flags to link against the dynamic
                       -- libraries, so we need to add WayDyn to ways.
index bc33048..0f2d6e1 100644 (file)
@@ -39,6 +39,13 @@ compile_libAS:
        '$(TEST_HC)' $(MY_TEST_HC_OPTS) -shared A.c -o $(call DLL,AS)
        rm -f libAS*.a
 
+.PHONY: compile_libT10458
+compile_libT10458:
+       rm -rf T10458dir
+       mkdir T10458dir
+       '$(TEST_HC)' $(MY_TEST_HC_OPTS) -odir "T10458dir" -shared A.c -o "T10458dir/$(call DLL,AS)"
+       rm -f T10458dir/libAS*.a
+
 .PHONY: compile_libAB_dep
 compile_libAB_dep:
        rm -rf bin_dep
diff --git a/testsuite/tests/ghci/linking/dyn/T10458.hs b/testsuite/tests/ghci/linking/dyn/T10458.hs
new file mode 100644 (file)
index 0000000..98fc4e4
--- /dev/null
@@ -0,0 +1,8 @@
+module T10458 (callSO) where
+
+import Foreign
+import Foreign.C.Types
+foreign import ccall "foo" dle :: IO CInt
+
+callSO :: IO CInt
+callSO = dle
diff --git a/testsuite/tests/ghci/linking/dyn/T10458.script b/testsuite/tests/ghci/linking/dyn/T10458.script
new file mode 100644 (file)
index 0000000..15ecb22
--- /dev/null
@@ -0,0 +1,4 @@
+:set -fobject-code
+:load T10458
+
+callSO
diff --git a/testsuite/tests/ghci/linking/dyn/T10458.stdout b/testsuite/tests/ghci/linking/dyn/T10458.stdout
new file mode 100644 (file)
index 0000000..0cfbf08
--- /dev/null
@@ -0,0 +1 @@
+2
index e5b40d4..eb044fc 100644 (file)
@@ -27,3 +27,10 @@ test('T10955dyn',
      [extra_clean(['bin_dyn/*', 'bin_dyn'])],
      run_command,
      ['$MAKE -s --no-print-directory compile_libAB_dyn'])
+
+test('T10458',
+     [unless(doing_ghci, skip),
+     extra_clean(['libAS.*']),
+     pre_cmd('$MAKE -s --no-print-directory compile_libT10458'),
+     extra_hc_opts('-L$PWD/T10458dir -lAS')],
+     ghci_script, ['T10458.script'])