Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#10442 closed bug (fixed)

Loading of shared libraries is problematic in ghc 7.10.1

Reported by: artella.coding Owned by:
Priority: high Milestone: 7.10.2
Component: Compiler Version: 7.10.1
Keywords: Cc: adamgundry, hvr, thoughtpolice, trommler
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: Runtime crash Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

Suppose that I have :

//test.h
int add(int a, int b);
//test.c
int add(int a, int b){
  return (a + b);
}
//mylib.c
#include "test.h"

int testAdd(){
  return add(2,3);
}

Then I compile via :

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 :

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 :

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) :

*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 :

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.

Change History (11)

comment:1 Changed 3 years ago by artella.coding

Cc: thoughtpolice added

comment:2 Changed 3 years ago by artella.coding

Cc: adamgundry added

comment:3 Changed 3 years ago 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?

comment:4 Changed 3 years ago by trommler

Cc: trommler added

comment:5 in reply to:  3 ; Changed 3 years ago by trommler

Replying to rwbarton:

Isn't this another consequence of the fix for #8935 (opening shared 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.

comment:6 in reply to:  5 Changed 3 years ago by artella.coding

Last edited 3 years ago by artella.coding (previous) (diff)

comment:7 in reply to:  5 ; Changed 3 years ago by artella.coding

Replying to trommler:

Replying to rwbarton:

Isn't this another consequence of the fix for #8935 (opening shared 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

comment:8 in reply to:  7 ; Changed 3 years ago by trommler

Replying to 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

comment:9 in reply to:  8 Changed 3 years ago by artella.coding

Hi I created a new ticket at https://ghc.haskell.org/trac/ghc/ticket/10492#ticket . Will close this ticket.

comment:10 Changed 3 years ago by artella.coding

Resolution: fixed
Status: newclosed

comment:11 Changed 3 years ago 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.

Note: See TracTickets for help on using tickets.