[GHC] #10442: Loading of shared libraries is problematic in ghc 7.10.1

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: | Owner: artella.coding | Status: new Type: bug | Milestone: 7.10.2 Priority: high | Version: 7.10.1 Component: Compiler | Operating System: Linux Keywords: | Type of failure: Runtime crash Architecture: x86_64 | Blocked By: (amd64) | Related Tickets: Test Case: | Blocking: | Differential Revisions: | -------------------------------------+------------------------------------- Suppose that I have : {{{#!hs //test.h int add(int a, int b); }}} {{{#!hs //test.c int add(int a, int b){ return (a + b); } }}} {{{#!hs //mylib.c #include "test.h" int testAdd(){ return add(2,3); } }}} Then I compile via : {{{#!hs gcc -shared -o libtest.so test.c gcc -fPIC -c mylib.c -o mylib.o gcc -shared -o libMy.so mylib.o }}} Then I have Main.hs with : {{{#!hs module Main where import Foreign.C.Types foreign import ccall "testAdd" c_testAdd :: CInt -> CInt -> IO (CInt) main = do result <- c_testAdd 3 4 print result }}} and I have the associated cabal file : {{{#!hs name: illustrate version: 0.1.0.0 build-type: Simple cabal-version: >=1.10 executable illustrate main-is: Main.hs build-depends: base >=4.8 && <4.9 default-language: Haskell2010 extra-lib-dirs: ./ ghc-options: -ltest -lMy }}} Then upon running "cabal repl" I get the following error message (in ghc 7.10.1) : {{{#!hs *Main> main /home/linux/programs/ghc-7.10.1/lib/ghc-7.10.1/bin/ghc: symbol lookup error: ./libMy.so: undefined symbol: add }}} If I do "cabal run" I get : {{{#!hs Preprocessing executable 'illustrate' for illustrate-0.1.0.0... [1 of 1] Compiling Main ( Main.hs, dist/build/illustrate /illustrate-tmp/Main.o ) Linking dist/build/illustrate/illustrate ... Running illustrate... /home/linux/Downloads/illustrate/dist/build/illustrate/illustrate: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory }}} Note that in ghc 7.8.3, cabal 1.22.0.0 it works fine. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: new Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Changes (by artella.coding): * cc: thoughtpolice (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: new Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Changes (by artella.coding): * cc: adamgundry (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: new Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by rwbarton): Isn't this another consequence of the fix for #8935 (opening shared libraries with RTLD_LOCAL)? So should we also link temporary object files against any libraries specified on the ghc(i) command line with `-lfoo`? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: new Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Changes (by trommler): * cc: trommler (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

Isn't this another consequence of the fix for #8935 (opening shared
#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: new Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by trommler): Replying to [comment:3 rwbarton]: libraries with RTLD_LOCAL)? Yes it is.
So should we also link temporary object files against any libraries specified on the ghc(i) command line with `-lfoo`?
Building libtest.so like so {{{ gcc -shared -o libMy.so mylib.o -L. -l test -Wl,-rpath=. }}} should fix the problem with `cabal repl`. I tried it with openSUSE's ghc 7.8.4 that has the fix for #8935 included. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: new Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by artella.coding): Hi your solution works in ghc 7.10.1. Thanks a lot & I will close the ticket. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

Replying to [comment:3 rwbarton]:
Isn't this another consequence of the fix for #8935 (opening shared
#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: new Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by artella.coding): Replying to [comment:5 trommler]: libraries with RTLD_LOCAL)?
Yes it is.
So should we also link temporary object files against any libraries specified on the ghc(i) command line with `-lfoo`?
Building libtest.so like so {{{ gcc -shared -o libMy.so mylib.o -L. -l test -Wl,-rpath=. }}}
should fix the problem with `cabal repl`. I tried it with openSUSE's ghc 7.8.4 that has the fix for #8935 included.
Hi thanks a lot, your solution works in ghc 7.10.1. But another problem persists. If I now reorganise such that the shared libraries are created in a subdirectory `libDir`, then I get an error upon a `cabal run`. That is if I compile via : {{{ gcc -shared -o libDir/libtest.so test.c gcc -fPIC -c mylib.c -o mylib.o gcc -shared -o libDir/libMy.so mylib.o -L./libDir -l test -Wl,-rpath=./libDir }}} this creates the following shared libraries : {{{ libDir/libMy.so libDir/libtest.so }}} Then if I modify the cabal file to be : {{{ name: illustrate version: 0.1.0.0 build-type: Simple cabal-version: >=1.10 executable illustrate main-is: Main.hs build-depends: base >=4.8 && <4.9 default-language: Haskell2010 extra-libraries: test My extra-lib-dirs: ./libDir }}} then `cabal repl` works fine, but `cabal run` gives the following error : {{{ cabal run Preprocessing executable 'illustrate' for illustrate-0.1.0.0... [1 of 1] Compiling Main ( Main.hs, dist/build/illustrate /illustrate-tmp/Main.o ) Linking dist/build/illustrate/illustrate ... Running illustrate... /home/linux/Downloads/illustrate_withDir/dist/build/illustrate/illustrate: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory }}} Why is this happening? Should I close this trac and post the problem above as a new Trac? Thanks -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: new Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by trommler): Replying to [comment:7 artella.coding]:
then `cabal repl` works fine, but `cabal run` gives the following error :
{{{ cabal run Preprocessing executable 'illustrate' for illustrate-0.1.0.0... [1 of 1] Compiling Main ( Main.hs, dist/build/illustrate /illustrate-tmp/Main.o ) Linking dist/build/illustrate/illustrate ... Running illustrate...
/home/linux/Downloads/illustrate_withDir/dist/build/illustrate/illustrate: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
}}}
Why is this happening?
The linker line does not contain a `-rpath` argument for `libtest.so`
Should I close this trac and post the problem above as a new Trac? Thanks
Yes, please create a new ticket. Thanks! As a workaround you can add the following to `illustrate.cabal`: {{{ ghc-options: -optl=-Wl,-rpath,./libDir }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: new Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by artella.coding): Hi I created a new ticket at https://ghc.haskell.org/trac/ghc/ticket/10492#ticket . Will close this ticket. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: closed Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: fixed | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Changes (by artella.coding): * status: new => closed * resolution: => fixed -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#10442: Loading of shared libraries is problematic in ghc 7.10.1 -------------------------------------+------------------------------------- Reporter: artella.coding | Owner: Type: bug | Status: closed Priority: high | Milestone: 7.10.2 Component: Compiler | Version: 7.10.1 Resolution: fixed | Keywords: Operating System: Linux | Architecture: x86_64 Type of failure: Runtime crash | (amd64) Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by rwbarton): Ah I see how I was confused about this ticket earlier. I was imagining that `Main` was loaded from an object file and then I didn't understand why the error was with finding `add` rather than `testAdd`. Now I agree the behavior described in this ticket makes sense and that setting an rpath when building the shared library is a logical solution. Running `cabal repl` with `--ghc-option=-fobject-code` does fail with {{{ Preprocessing executable 'illustrate' for illustrate-0.1.0.0... GHCi, version 7.10.1: http://www.haskell.org/ghc/ :? for help Ok, modules loaded: Main. Prelude Main> main ghc: panic! (the 'impossible' happened) (GHC version 7.10.1 for x86_64-unknown-linux): Loading temp shared object failed: /tmp/ghc4059_0/libghc4059_2.so: undefined symbol: testAdd }}} which is bad (it would link correctly with ghc, so it should just work under ghci), but that's #10458. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10442#comment:11 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC