Skip to content

Hint can't find symbol #156

Closed
Closed

Description

@polymorphicengine

Hello,
some days ago tidal was updated to have link support, this is done by the following package https://hackage.haskell.org/package/tidal-link.

unfortunately this seems to break running tidal inside a hint interpreter. Here is a minimal example:

module Main where

import Language.Haskell.Interpreter

main :: IO ()
main = do
  x <- runInterpreter interpreter
  case x of
    Left e -> putStrLn (show e)
    Right _ -> return ()

interpreter :: Interpreter ()
interpreter = do
         setImportsF [ModuleImport "Sound.Tidal.Context" NotQualified NoImportList, ModuleImport "Prelude" NotQualified NoImportList]
         runStmt "tidal <- startTidal (superdirtTarget {oLatency = 0.1, oAddress = \"127.0.0.1\", oPort = 57120}) (defaultConfig {cVerbose = True, cFrameTimespan = 1/20})"

it throws the following errors:

hint-try: /home/martin/.cabal/store/ghc-9.2.3/tidal-link-1.0.1-e904e203bf8f1a5046be19d3e454931fcff763f9b9920cad70d59e090bc3569f/lib/libHStidal-link-1.0.1-e904e203bf8f1a5046be19d3e454931fcff763f9b9920cad70d59e090bc3569f.a: ELF TLSGD relocation for symbol `_ZN4asio6detail15keyword_tss_ptrINS0_10call_stackINS0_14thread_contextENS0_16thread_info_baseEE7contextEE6value_E' not supported on the target platform
hint-try: /home/martin/.cabal/store/ghc-9.2.3/tidal-1.9.2-54c2e1be48d3c91b38aa9169eaa072a94363b15bbbc85dac3185686222ca970e/lib/libHStidal-1.9.2-54c2e1be48d3c91b38aa9169eaa072a94363b15bbbc85dac3185686222ca970e.a: unknown symbol `abl_link_create_session_state'
hint-try: ^^ Could not load 'tidalzm1zi9zi2zm54c2e1be48d3c91b38aa9169eaa072a94363b15bbbc85dac3185686222ca970e_SoundziTidalziStream_Target_con_info', dependency unresolved. See top entry above.

hint-try: ^^ Could not load 'tidalzm1zi9zi2zm54c2e1be48d3c91b38aa9169eaa072a94363b15bbbc85dac3185686222ca970e_SoundziTidalziStream_Target_static_info', dependency unresolved. See top entry above.

GhcException "\nGHC.ByteCode.Linker.lookupIE\nDuring interactive linking, GHCi couldn't find the following symbol:\n  tidalzm1zi9zi2zm54c2e1be48d3c91b38aa9169eaa072a94363b15bbbc85dac3185686222ca970e_SoundziTidalziStream_Target_con_info or tidalzm1zi9zi2zm54c2e1be48d3c91b38aa9169eaa072a94363b15bbbc85dac3185686222ca970e_SoundziTidalziStream_Target_static_info\nThis may be due to you not asking GHCi to load extra object files,\narchives or DLLs needed by your current session.  Restart GHCi, specifying\nthe missing library using the -L/path/to/object/dir and -lmissinglibname\nflags, or simply by naming the relevant files on the GHCi command line.\nAlternatively, this link failure might indicate a bug in GHCi.\nIf you suspect the latter, please report this as a GHC bug:\n  https://www.haskell.org/ghc/reportabug\n"

note that running the same line of code in a ghci session works fine.
i'm really lost here, so i hope someone can help!

Activity

  1. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    I need more information.

    1. What does "link support" mean, is this music lingo or do you mean linking in the sense of ld and .so/.dll files?
    2. Would it be difficult to further minimize the example by importing a minimal module rather than all of Tidal?
    3. Am I correct that those hint-try: ... errors are from cabal, not hint, and thus the problem occurs before hint can do anything about it?
  2. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    note that running the same line of code in a ghci session works fine.

    Do you start your ghci session in a way which also causes those hint-try: ... errors to appear?

  3. polymorphicengine commented on Sep 16, 2022

    @polymorphicengine
    Author

    thanks for the quick reply,

    1. Here 'link' is indeed in reference to a music protocol and has nothing to do with linking in the sense of ld
    2. I can give it a try, maybe by just importing tidal-link itself
    3. Yes, those errors seem to come from cabal (but only occur once running the interpreter), hint throws the last GHCExpception error

    Do you start your ghci session in a way which also causes those hint-try: ... errors to appear?

    no, in ghci i just import Sound.Tidal.Context and run

    tidal <- startTidal (superdirtTarget {oLatency = 0.1, oAddress = "127.0.0.1", oPort = 57120}) (defaultConfig {cVerbose = True, cFrameTimespan = 1/20})

    which throws no errors and starts the tidal stream as expected

  4. polymorphicengine commented on Sep 16, 2022

    @polymorphicengine
    Author

    the following only imports tidal-link and fails with basically the same error:

    module Main where
    
    import Language.Haskell.Interpreter
    
    main :: IO ()
    main = do
      x <- runInterpreter interpreter
      case x of
        Left e -> putStrLn (show e)
        Right _ -> return ()
    
    interpreter :: Interpreter ()
    interpreter = do
             setImportsF [ModuleImport "Sound.Tidal.Link" NotQualified NoImportList, ModuleImport "Prelude" NotQualified NoImportList]
             runStmt "s <- createSessionState"
    

    only importing tidal-link, without executing any code related to it works fine.

    i tried some other functions from https://hackage.haskell.org/package/tidal-link-1.0.1/docs/Sound-Tidal-Link.html aswell and all seem to fail..

    zalastax, author of tidal-link suggested to call ghc with argument "-Lstdc++", so i tried using unsafeRunInterpreterWithArgs ["-Lstdc++"], but it results in the same error message too

  5. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    Let's take a closer look at the error messages.

    The hint-try: ... messages complain about the following symbols:

    1. _ZN4asio6detail15keyword_tss_ptrINS0_10call_stackINS0_14thread_contextENS0_16thread_info_baseEE7contextEE6value_E
    2. abl_link_create_session_state
    3. tidalzm1zi9zi2zm54c2e1be48d3c91b38aa9169eaa072a94363b15bbbc85dac3185686222ca970e_SoundziTidalziStream_Target_con_info
    4. tidalzm1zi9zi2zm54c2e1be48d3c91b38aa9169eaa072a94363b15bbbc85dac3185686222ca970e_SoundziTidalziStream_Target_static_info

    The shape of these symbols tell us that they are, respectively:

    1. a C++ mangled name
    2. a C function name
    3. a ghc z-encoded name
    4. a ghc z-encoded name

    And once we decode those names, we get something a lot more readable:

    1. asio::detail::keyword_tss_ptr<asio::detail::call_stack<asio::detail::thread_contextasio::detail::thread_info_base>::context>::value_
    2. abl_link_create_session_state
      3.tidal-1.9.2-012aac824dcb46a140aefc0b0d9e9c6e5504cdb417cbaf42de9ebe02634045b9_Sound.Tidal.Stream_Target_con_info
    3. tidal-1.9.2-012aac824dcb46a140aefc0b0d9e9c6e5504cdb417cbaf42de9ebe02634045b9_Sound.Tidal.Stream_Target_static_info
  6. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor
    1. Yes, those errors seem to come from cabal (but only occur once running the interpreter), hint throws the last GHCExpception error

    By adding a print statement before the runInterpreter call, or simply by compiling the program with cabal and then running the executable manually afterwards, I have determined that on the contrary, the errors are thrown by the executable, not by cabal, somewhere during the execution of the startTidal line.

  7. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    The error occurs even if the interpreted code is simplified to

    runStmt "let x = superdirtTarget"
    

    but not if it is further simplified to

    runStmt "let x = defaultConfig"
    

    Therefore, the issue doesn't happen when the startTidal or createSessionState IO action is executed, but rather, when the module which provides superdirtTarget is loaded. Note that it is lazily loaded, which is why importing that module is insufficient to trigger the error.

  8. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    I see you've added a tidal-link-1.0.1 exporting an extra hello :: IO () action. Thanks, this helps to eliminate a whole bunch of code, as the following still fails:

    module Main where
    import Language.Haskell.Interpreter
    
    main :: IO ()
    main = do
      x <- runInterpreter interpreter
      case x of
        Left e -> do
          -- GhcException "\nByteCodeLink.lookupCE\nDuring interactive linking, GHCi couldn't find the following symbol:\n  tidalzmlinkzm1zi0zi1zm0358d4e416ad055e1e148187987824589541f00f08ddb5855faa8ff4af619c66_SoundziTidalziLink_hello_closure\nThis may be due to you not asking GHCi to load extra object files,\narchives or DLLs needed by your current session.  Restart GHCi, specifying\nthe missing library using the -L/path/to/object/dir and -lmissinglibname\nflags, or simply by naming the relevant files on the GHCi command line.\nAlternatively, this link failure might indicate a bug in GHCi.\nIf you suspect the latter, please report this as a GHC bug:\n  https://www.haskell.org/ghc/reportabug\n"
          putStrLn (show e)
        Right _ -> do
          return ()
    
    interpreter :: Interpreter ()
    interpreter = do
      setImports ["Prelude", "Sound.Tidal.Link"]
      -- toy: /home/gelisam/.cabal/store/ghc-8.10.7/tidal-link-1.0.1-0358d4e416ad055e1e148187987824589541f00f08ddb5855faa8ff4af619c66/lib/libHStidal-link-1.0.1-0358d4e416ad055e1e148187987824589541f00f08ddb5855faa8ff4af619c66.a: unknown symbol `_ZN4asio6detail15keyword_tss_ptrINS0_10call_stackINS0_14thread_contextENS0_16thread_info_baseEE7contextEE6value_E'
      -- toy: /home/gelisam/.cabal/store/ghc-8.10.7/tidal-link-1.0.1-0358d4e416ad055e1e148187987824589541f00f08ddb5855faa8ff4af619c66/lib/libHStidal-link-1.0.1-0358d4e416ad055e1e148187987824589541f00f08ddb5855faa8ff4af619c66.a: unknown symbol `abl_link_create'
      -- toy: ^^ Could not load 'tidalzmlinkzm1zi0zi1zm0358d4e416ad055e1e148187987824589541f00f08ddb5855faa8ff4af619c66_SoundziTidalziLink_hello_closure', dependency unresolved. See top entry above.
      runStmt "hello"

    This time the unmangled symbols are:

    • asio::detail::keyword_tss_ptr<asio::detail::call_stack<asio::detail::thread_contextasio::detail::thread_info_base>::context>::value_
    • abl_link_create
    • tidal-link-1.0.1-0358d4e416ad055e1e148187987824589541f00f08ddb5855faa8ff4af619c66_Sound.Tidal.Link_hello_closure

    The asio::...::value_ symbol is the same, which makes me think that this is the cause of all the other missing symbols.

  9. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    I see you've added a tidal-link-1.0.1 exporting an extra hello :: IO () action. Thanks, this helps to eliminate a whole bunch of code

    My mistake, now that I am taking a look at the source for hello, I see that it's actually exercising the library's other functions, it's not just hello = putStrLn "hello" as I expected from the name.

  10. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    If I change the implementation to hello = putStrLn "hello", I still get the error, so we're back to "Thanks, this helps to eliminate a whole bunch of code" :)

  11. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    Deleting the rest of the hsc file makes the error disappear, so it's definitely the lazy-loading of this module which is triggering the errors.

  12. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    Here's a minimal hsc module which still reproduces the problem:

    {-# LANGUAGE ForeignFunctionInterface #-}
    
    module Sound.Tidal.Link where
    
    foreign import ccall "abl_link.h abl_link_create"
      create :: IO ()
    
    hello :: IO ()
    hello = print "hello"

    I'm lying about the type of abl_link_create in order to minimize the imports, so create would fail at runtime when the IO action is called, but here we're not even calling it, we're calling hello. It doesn't matter whether I keep abl_link_create or any of the other foreign imports of this file. I suspect the problem is in the linked abl_link.h file.

  13. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    I've narrowed it down to this line:

    https://github.com/tidalcycles/Tidal/blob/a9208569cbd86ee0c0c39052f354ee4984d701d9/tidal-link/link/extensions/abl_link/src/abl_link.cpp#L21

    I have simplified the .h, .cpp, and .hsc files to their bare minimum:

    -- tidal-link/src/hs/Sound/Tidal/Link.hsc
    {-# LANGUAGE ForeignFunctionInterface #-}
    
    module Sound.Tidal.Link where
    
    foreign import ccall "abl_link.h abl_dummy"
      dummy :: IO ()
    
    hello :: IO ()
    hello = print "hello"
    // tidal-link/link/extensions/abl_link/include/abl_link.h
    #ifdef __cplusplus
    extern "C"
    {
    #endif // __cplusplus
    
      void abl_dummy();
    
    #ifdef __cplusplus
    } // extern "C"
    #endif // __cplusplus
    // tidal-link/link/extensions/abl_link/src/abl_link.cpp
    #include <abl_link.h>
    #include <ableton/Link.hpp>
    
    extern "C"
    {
      void abl_dummy()
      {
        return;
      }
    }

    The error disappears if I remove the #include <ableton/Link.hpp> line.

    Note that cabal doesn't automatically recompile when I change those C++ files, so I had to delete the dist-newstyle folder after each change.

  14. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    Narrowed it down further to

    https://github.com/tidalcycles/Tidal/blob/a9208569cbd86ee0c0c39052f354ee4984d701d9/tidal-link/link/include/ableton/Link.hpp#L25

    That is, I'm now hopping from ableton/Link.hpp to ableton/platforms/Config.hpp...

  15. gelisam commented on Sep 16, 2022

    @gelisam
    Contributor

    ...to tidal-link/link/include/ableton/platforms/asio/Context.hpp...

  16. 16 remaining items

  17. gelisam commented on Sep 20, 2022

    @gelisam
    Contributor

    Do you have a file which looks like ~/.ghc/x86_64-linux-8.10.7/environments/default?

    You mentioned that you did not include cabal repl's package-db arguments, and that surprised me since they seem pretty important. On my side, I was using write-ghc-environment-files: always in my cabal.project in order for cabal.build to generate a .ghc.environment.x86_64-linux-8.10.7 file listing all the package databases and package ids, so even when unsafeRunInterpreterWithArgs was receiving zero arguments, hint was still getting its package-db information from that file. I tried deleting it in order to make sure I was using cabal repl's package-db flags and only that package-db information, but something surprising happened instead: I started getting errors about megaparsec?? I got that error with both cabal repl and cabal run. It turns out that in the absence of a .ghc.environment.x86_64-linux-8.10.7, ghc is using a file ~/.ghc/x86_64-linux-8.10.7/environments/default instead, and I had a stale line about megaparsec in there. I deleted the file, and the megaparsec error went away.

    With neither ~/.ghc/x86_64-linux-8.10.7/environments/default nor .ghc.environment.x86_64-linux-8.10.7 nor passing the package-db flags to unsafeRunInterpreterWithArgs, both cabal repl and cabal run fail with Could not find module Sound.Tidal.Link.

    With either a ~/.ghc/x86_64-linux-8.10.7/environments/default which includes tidal-link (by installing it globally via cabal install --lib tidal-link), a .ghc.environment.x86_64-linux-8.10.7 file which includes tidal-link (by including it in hint-try.cabal's list of dependencies), or by passing package-db flags which include -package-id tidal-link-1.0.1-[...] to unsafeRunInterpreterWithArgs (as obtained from cabal repl -v when tidal-link is included in hint-try.cabal's list of dependencies), cabal repl succeeds while cabal run fails with unknown symbol blah.

  18. gelisam commented on Sep 20, 2022

    @gelisam
    Contributor

    In particular, you mentioned -Lstdc++ early on, and I see an extra-libraries: stdc++ line in my package database's tidal-link-1.0.1-[...].conf file, so if hint doesn't get to see this file, that could explain the error. I don't think so though, because as the above comment explains, hint would fail earlier with Sound.Tidal.Link if it did not see that file.

  19. gelisam commented on Sep 20, 2022

    @gelisam
    Contributor

    That was a lot of information, so, to summarize: I was wrong about the difference between cabal repl and cabal run being the arguments being passed to unsafeRunInterpreterWithArgs. We're back to square one: trying to minimize the repro steps to something smaller than all of boost::asio.

  20. gelisam commented on Sep 20, 2022

    @gelisam
    Contributor

    The other obvious difference between cabal repl and cabal run is the environment variables. Here are the differences I see:

    Only with cabal repl:

    • HASKELL_DIST_DIR="/.../dist-newstyle/build/x86_64-linux/ghc-8.10.7/toy-0.1.0.0/x/hint-try"
    • PATH="...:$HOME/.cabal/bin"
    • SHLVL=1

    Only with cabal run:

    • _="$HOME//.ghcup/bin/cabal
    • PATH="..."
    • SHLVL=2

    But this is a red herring too, because if I add a couple of setEnv calls at the beginning of main to force the environment to match the cabal repl values in both runs, I still see a success with cabal repl and a failure with cabal run. What else could it be?

  21. gelisam commented on Sep 20, 2022

    @gelisam
    Contributor

    Interesting. If I try to run createSessionState in main, before running it inside hint, the one in hint fails with a different error:

    toy: internal error: /home/gelisam/.cabal/store/ghc-8.10.7/tidal-link-1.0.1-0358d4e416ad055e1e148187987824589541f00f08ddb5855faa8ff4af619c66/lib/libHStidal-link-1.0.1-0358d4e416ad055e1e148187987824589541f00f08ddb5855faa8ff4af619c66.a: unhandled ELF relocation(RelA) type 19
    

    I actually recognize that error message: deep in the bowels of ghc, there's some custom linking code handling various types of "relocations", whatever that is, and ghc doesn't support all the types of relocations which can be found in the wild. So it could be that the root of the issue is that boost::asio, as compiled by g++, produces a symbol which needs a relocation code which ghc does not support. It still doesn't explain why cabal run needs to relocate the symbol while cabal repl doesn't.

  22. gelisam commented on Sep 20, 2022

    @gelisam
    Contributor

    Interestingly, when I upgrade to ghc-9.2, I no longer get unhandled ELF relocation(RelA) type 19, I get the usual ELF TLSGD relocation for symbol blah. Those two messages are awfully similar, but it's only the first message which I recognize.

  23. polymorphicengine commented on Sep 21, 2022

    @polymorphicengine
    Author

    I'm still struggling to understand what is really going on here but i tried cabal run --ghc-options=-dynamic which fixes the error (with the original tidal-link library).. note that when using -dynamic as an argument for unsafeRunInterpreterWithArgs the following error is thrown (by hint and not the binary):

    GhcException "<interactive>: fatal:\n Cannot load -dynamic objects when GHC is built the normal way\n To fix this, either:\n (1) Use -fexternal-interpreter, or\n (2) Build the program twice: once the normal way, and then\n with -dynamic using -osuf to set a different object file suffix."

    While it is great that this fixes the problem, I strongly suspect that this will not really work when I'm trying to package everything so that tidal can be run on systems without an installation of ghc (which is kind of the whole point of this!), so it would be great to understand the problem a bit better and see what is actually going on

  24. polymorphicengine commented on Oct 19, 2022

    @polymorphicengine
    Author

    coming back to this, i am still very lost.. hope someone can find some time to help me out!

  25. yaxu commented on Dec 19, 2022

    @yaxu

    Just a bump on this as it would be lovely to resolve our very long running aim to have an easy binary distribution of tidal !

  26. gelisam commented on Dec 19, 2022

    @gelisam
    Contributor

    Sorry, I don't have much time for open source work these days. Keep reminding me though, so that once I do find time I can make sure I spend it on something useful!

  27. polymorphicengine commented on Jun 16, 2023

    @polymorphicengine
    Author

    great to see that hint is now working with ghc 9.6.2!
    unfortunately, this error still persists with the new version. it would be great if we could fix it!

  28. gelisam commented on Jun 16, 2023

    @gelisam
    Contributor

    cabal run --ghc-options=-dynamic fixes the error

    You have once again solved the case!

    While googling for -dynamic, I found this wiki page. It explains that the GHC runtime contains a custom linker, and that maintaining it is a pain. Indeed, that linker is missing some of the more exotic types of relocation, and that's why we see a relocation error for asio::...::value_.

    To work around these holes in the custom linker, ghci is compiled using -dynamic, which allows it to reuse the system's linker instead of relying on its custom linker. This explains why ghci is able to load sio::...::value_ and why cabal run -dynamic can load it too.

    Thus, the next steps are: figure out whether you're okay with using -dynamic, and if not, figure out how to add the missing relocation implementation to GHC's custom linker.

    Feel free to continue the discussion on this thread if you have more questions, but now that I understand the problem, I no longer think that there is a bug in hint's codebase, so I will mark this ticket as closed.

  29. polymorphicengine commented on Jun 18, 2023

    @polymorphicengine
    Author

    alright, so i tried the following things:

    • pass -dynamic and -fexternal-interpreter to hints run unsafeRunInterpreterWithArgsLibdir and compile the usual way
      this results in the following error:
      /home/martin/Downloads/artifact/linux/tidal/haskell-libs/bin/ghc-iserv-dyn: error while loading shared libraries: libHSlibiserv-9.4.2-ghc9.4.2.so: cannot open shared object file: No such file or directory

    if i do ldd ghc-iserv-dyn i can see that it can't find the shared libraries, so then i tried to link to all of the neccessary .so files via symbolic links and export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:../ghc-packages, now i see with ldd that all libs are found but it still fails with the same error.

    • compile with --ghc-options=-dynamic, this will produce a binary that is, similar to above can't find the shared libraries, and if i try to link them the issue also persists.

    is there something else i could try?

    you can have a look at the workflow and produced artifact here: https://github.com/polymorphicengine/Tidal/actions/runs/5303831109

  30. polymorphicengine commented on Jun 19, 2023

    @polymorphicengine
    Author
    ok, i forgot to add the `ld-options` field in the .cabal file. now it seems to find the shared libraries, but the following error is thrown:

    tidal-listener: /home/martin/Downloads/tidal-release-linux/tidal-listener-linux/binary/haskell-libs/bin/ghc-iserv-dyn: createProcess: posix_spawnp: does not exist (No such file or directory)

    very cryptic. what exactly does not exist?

    ok this was just me messing up where i put the bin folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions