Ticket #2841 (new bug)

Opened 3 years ago

Last modified 3 years ago

Ghci + foreign export declarations result in undefined symbols

Reported by: fasta Owned by:
Priority: normal Milestone: _|_
Component: Compiler (FFI) Version: 6.10.1
Keywords: Cc:
Operating System: Linux Architecture: Unknown/Multiple
Type of failure: Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

When using ghci and foreign export declarations, calling any function, _even_ test, will result in an unknown symbol error.

Another bug is that the compiler is not purely functional in the sense that without history (see below) it is possible for it to work in one instance, but even then it doesn't. GHCi should have the property that if X works in a state S of the OS (collection of files, etc), then it should work in all states S', obtained by adding extra stuff to S, but not modifying anything. Currently, GHCi is breaking this fundamental property.

$ ghci New.hs
*Main> foo 1
<interactive>: New_stub.o: unknown symbol `Main_zdffoozuavi_closure'
{-# LANGUAGE ForeignFunctionInterface #-}

-- save in a file New.hs

foreign export ccall foo :: Int -> IO Int -- add this line and ghci will return something like New_stub.o: unknown symbol `Main_zdffoozuaIc_closure'

-- if you first try without the line, it works. If you still have stub files in the current directory, it will fail with the same message as before. 

foo :: Int -> IO Int
foo = return . length .  f

f :: Int -> [Int]
f 0 = []
f n = n:(f (n-1))

test = 1

Change History

Changed 3 years ago by igloo

  • difficulty set to Unknown
  • milestone set to _|_

This is documented as not working:

but I can't find a bug report for it, so I'll leave this one open.

Changed 3 years ago by fasta

From the FAQ

Unfortunately not. We haven't implemented it yet. Please compile any offending modules by hand before loading them into GHCi.

This information is not correct. Recompiling the offending modules by hand and then loading them in ghci does work, but calling one of the exported functions results in unknown symbol `_GLOBAL_OFFSET_TABLE_'.

Changed 3 years ago by simonmar

please create a new ticket describing exactly the sequence of actions that lead to the error. I haven't been able to reproduce the error you mentioned from the information you give above.

~/scratch > cat 2841.hs
{-# LANGUAGE ForeignFunctionInterface #-}
module M2841 where

-- save in a file New.hs

foreign export ccall foo :: Int -> IO Int -- add this line and ghci will return something like New_stub.o: unknown symbol `Main_zdffoozuaIc_closure'

-- if you first try without the line, it works. If you still have stub files in the current directory, it will fail with the same message as before. 

foo :: Int -> IO Int
foo = return . length .  f

f :: Int -> [Int]
f 0 = []
f n = n:(f (n-1))

test = 1
~/scratch > ghc -c 2841.hs
compilation IS NOT required
~/scratch > ghci 2841.hs
GHCi, version 6.10.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
$HOME/.ghci
Ok, modules loaded: M2841.
Prelude M2841> foo 3
3
Prelude M2841> f 1
[1]
Prelude M2841> test
1
Prelude M2841> 

Changed 3 years ago by fasta

I wrongly assumed that one only needs the stub files in a compiled state. If one has <module>.o then indeed the workaround works.

Note: See TracTickets for help on using tickets.