Home > Software engineering >  OSError: /usr/lib/ghc/ghc-prim-0.5.2.0/libHSghc-prim-0.5.2.0-ghc8.4.4.so: undefined symbol: stg_gc_u
OSError: /usr/lib/ghc/ghc-prim-0.5.2.0/libHSghc-prim-0.5.2.0-ghc8.4.4.so: undefined symbol: stg_gc_u

Time:01-01

I am trying to run this example from github: https://github.com/nh2/haskell-from-python/blob/master/Makefile I wanted to get an introduction to running one language from another language. I'm not sure if FFI is playing a role here somehow, I just don't know enough to tell.

I am running the code on WSL - debian. I also tried running it on windows, but I get the same issue. My error is after running 'make' and then 'python program.py' I get:

Traceback (most recent call last):
  File "program.py", line 5, in <module>
    lib = cdll.LoadLibrary("./libffi-example.so")
  File "/usr/lib/python2.7/ctypes/__init__.py", line 444, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 366, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /usr/lib/ghc/ghc-prim-0.5.2.0/libHSghc-prim-0.5.2.0-ghc8.4.4.so: undefined symbol: stg_gc_unpt_r1

I don't know what stg_gc_unput_r1 is, and it's not a variable in any of the code. I also looked through some of the GHC documentation, and couldn't find anything on it. My issue on the repository hasn't gotten any attention, and although it's only been a day since I posted it, the code is several years old.

I have no experience with Haskell and ghc, and very little with Python, so any help and patience would be appreciated.

After checking that the file /usr/lib/ghc/rts/libHSrts-ghc8.4.4.so does in fact exist, here is the output from running ldd libffi-example.so:

linux-vdso.so.1 (0x00007ffd5f5ee000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcc439c7000)
        libHSbase-4.11.1.0-ghc8.4.4.so => /usr/lib/ghc/base-4.11.1.0/libHSbase-4.11.1.0-ghc8.4.4.so (0x00007fcc4304c000)
        libHSinteger-gmp-1.0.2.0-ghc8.4.4.so => /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.4.4.so (0x00007fcc43009000)
        libHSghc-prim-0.5.2.0-ghc8.4.4.so => /usr/lib/ghc/ghc-prim-0.5.2.0/libHSghc-prim-0.5.2.0-ghc8.4.4.so (0x00007fcc42b7b000)
        libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007fcc42af8000)
        libatomic.so.1 => /usr/lib/x86_64-linux-gnu/libatomic.so.1 (0x00007fcc42aec000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcc4292b000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fcc43b58000)
        libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007fcc42921000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcc42900000)

Things I have tried:

  • running with -no-hs-main option
  • reading through ghc docs
  • posting github issue
  • changing version of ghc in the GHC_RUNTIME_LINKER_FLAG to 8.4.4, my current version

CodePudding user response:

I've tried to duplicate your problem with a fresh Debian under WSL install. I'm running Debian "bullseye" under WSL 1 under Windows 10. That Debian version must be a little newer than yours, since the GHC packages are version 8.8.4 instead of 8.4.4, but that seems to be the only difference.

Using a clean copy of that Git repository (commit 9c3b6315) with only the Makefile changed (exactly as below except with usual "tab" indentation):

GHC=ghc
GHC_RUNTIME_LINKER_FLAG=-L/usr/lib/ghc/rts -lHSrts-ghc8.8.4

libffi-example.so: Example.o wrapper.o
    $(GHC) -o $@ -shared -dynamic -fPIC $^ $(GHC_RUNTIME_LINKER_FLAG)

Example_stub.h Example.o: Example.hs
    $(GHC) -c -dynamic -fPIC Example.hs

wrapper.o: wrapper.c Example_stub.h
    $(GHC) -c -dynamic -fPIC wrapper.c

clean:
    rm -f *.hi *.o *_stub.[ch]

clean-all:
    rm -f *.hi *.o *_stub.[ch] *.so


# Runs the example Python program
example: libffi-example.so
    python program.py

I can run the following commands:

buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ make clean-all
rm -f *.hi *.o *_stub.[ch] *.so
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ make example
ghc -c -dynamic -fPIC Example.hs
ghc -c -dynamic -fPIC wrapper.c
ghc -o libffi-example.so -shared -dynamic -fPIC Example.o wrapper.o -L/usr/lib/ghc/rts -lHSrts-ghc8.8.4
python program.py
Haskell fib(0): 0
Haskell fib(1): 1
Haskell fib(2): 1
Haskell fib(3): 2
Haskell fib(4): 3
Haskell fib(5): 5
Haskell fib(6): 8
Haskell fib(7): 13
Haskell fib(8): 21
Haskell fib(9): 34
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ ldd libffi-example.so 
    linux-vdso.so.1 (0x00007ffff6925000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7c7b810000)
    libHSrts-ghc8.8.4.so => /usr/lib/ghc/rts/libHSrts-ghc8.8.4.so (0x00007f7c7b7a0000)
    libHSbase-4.13.0.0-ghc8.8.4.so => /usr/lib/ghc/base-4.13.0.0/libHSbase-4.13.0.0-ghc8.8.4.so (0x00007f7c7ae00000)
    libHSinteger-gmp-1.0.2.0-ghc8.8.4.so => /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.8.4.so (0x00007f7c7adc0000)
    libHSghc-prim-0.5.3-ghc8.8.4.so => /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.8.4.so (0x00007f7c7a920000)
    libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007f7c7a880000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7c7a6b0000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f7c7b973000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7c7a6a0000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7c7a690000)
    libffi.so.7 => /usr/lib/x86_64-linux-gnu/libffi.so.7 (0x00007f7c7a680000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7c7a65e000)

buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ 

If I intentionally break the Makefile, say by commenting out the GHC_RUNTIME_LINKER_FLAG link or misspelling it so it isn't actually included in the libffi-example.so link command, then I can duplicate your problem exactly. Note the _MISTAKE in the second line of the Makefile in the following transcript, which stops the RTS library from actually being linked:

buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ head Makefile
GHC=ghc
GHC_RUNTIME_LINKER_FLAG_MISTAKE=-L/usr/lib/ghc/rts -lHSrts-ghc8.8.4

libffi-example.so: Example.o wrapper.o
    $(GHC) -o $@ -shared -dynamic -fPIC $^ $(GHC_RUNTIME_LINKER_FLAG)

Example_stub.h Example.o: Example.hs
    $(GHC) -c -dynamic -fPIC Example.hs

wrapper.o: wrapper.c Example_stub.h
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ make clean-all
rm -f *.hi *.o *_stub.[ch] *.so
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ make example
ghc -c -dynamic -fPIC Example.hs
ghc -c -dynamic -fPIC wrapper.c
ghc -o libffi-example.so -shared -dynamic -fPIC Example.o wrapper.o 
python program.py
Traceback (most recent call last):
  File "program.py", line 4, in <module>
    lib = cdll.LoadLibrary("./libffi-example.so")
  File "/usr/lib/python2.7/ctypes/__init__.py", line 444, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 366, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.8.4.so: undefined symbol: stg_gc_unpt_r1
make: *** [Makefile:22: example] Error 1
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ ldd libffi-example.so
    linux-vdso.so.1 (0x00007fffea75d000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f83175e0000)
    libHSbase-4.13.0.0-ghc8.8.4.so => /usr/lib/ghc/base-4.13.0.0/libHSbase-4.13.0.0-ghc8.8.4.so (0x00007f8316c40000)
    libHSinteger-gmp-1.0.2.0-ghc8.8.4.so => /usr/lib/ghc/integer-gmp-1.0.2.0/libHSinteger-gmp-1.0.2.0-ghc8.8.4.so (0x00007f8316c00000)
    libHSghc-prim-0.5.3-ghc8.8.4.so => /usr/lib/ghc/ghc-prim-0.5.3/libHSghc-prim-0.5.3-ghc8.8.4.so (0x00007f8316760000)
    libgmp.so.10 => /usr/lib/x86_64-linux-gnu/libgmp.so.10 (0x00007f83166d0000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f83164f0000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f8317741000)
    libffi.so.7 => /usr/lib/x86_64-linux-gnu/libffi.so.7 (0x00007f83164e0000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f83164be000)
buhr@DESKTOP-585NUKU:~/src/haskell-from-python$ 

It looks to me like, while experimenting, you somehow broke your Makefile so it's not actually linking against /usr/lib/ghc/libHSrts-ghc8.4.4.so.

  • Related