rust-lua53 icon indicating copy to clipboard operation
rust-lua53 copied to clipboard

Error loading Lua C Modules on Linux and OS X

Open deepakjois opened this issue 7 years ago • 5 comments

This is a separate error from #84. This occurs on both OS X and Linux. In the code snippet below: serpent is a regular Lua module and lfs (luafilesystem) is a C module.

extern crate lua;

fn main() {
  let mut state = lua::State::new();
    state.open_libs();
    state.do_string("
    print('hello')

    if not pcall(function() require('serpent') end) then
      print('error loading serpent')
    else
        spt = require('serpent')
        print('serpent successfully loaded')
    end

    local status, err = pcall(function() require('lfs') end)
    print(err)


    ");
}

On Linux it gives the following error:

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/rlua`
hello
serpent successfully loaded
error loading module 'lfs' from file '/home/deepak/lua53/lib/lua/5.3/lfs.so':
        /home/deepak/lua53/lib/lua/5.3/lfs.so: undefined symbol: lua_gettop

On OS X (after fixing #84), it gives the following error:

  Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/rlua`
hello
serpent successfully loaded
error loading module 'lfs' from file '/Users/deepak/Downloads/lua53/lib/lua/5.3/lfs.so':
        dlopen(/Users/deepak/Downloads/lua53/lib/lua/5.3/lfs.so, 6): Symbol not found: _lua_settable
  Referenced from: /Users/deepak/Downloads/lua53/lib/lua/5.3/lfs.so
  Expected in: flat namespace
 in /Users/deepak/Downloads/lua53/lib/lua/5.3/lfs.so

deepakjois avatar Jun 21 '17 06:06 deepakjois

@Parakleta, it seems you posted something similar here: https://users.rust-lang.org/t/exporting-dynamic-symbols-from-executable/6103/3

Do you have any idea what’s going on here?

deepakjois avatar Jun 21 '17 07:06 deepakjois

What I posted there is probably exactly the issue you're having. Essentially the lfs.so module needs the be able to see the Lua API functions in the executable, and a chain of compile options are required to keep them visible to the end. I haven't compiled this stuff in ages so I don't know exactly what is still relevant.

You'll need the use the nm utility on the output of the various compile stages to work out where the symbols are being lost. Have you tried creating the dynsyms.txt file and the custom build rule from the linked post? Also try setting the LUA_API and LUALIB_API to set default visibility in luaconf.h I think.

Parakleta avatar Jun 21 '17 14:06 Parakleta

On Linux, when I put this is my .cargo/config file, things work fine and the Lua C module loads successfully:

[build]
rustflags = ["-C", "link-args=-Wl,-export-dynamic"]

deepakjois avatar Jun 22 '17 04:06 deepakjois

On OS X, I have to tweak the linker flags slightly:

[build]
rustflags = ["-C", "link-args=-Wl,-export_dynamic"]

See the underscore instead of the dash, i.e. export_dynamic instead of export-dynamic.

deepakjois avatar Jun 22 '17 04:06 deepakjois

After doing a little bit more research it appears that a more cross-platform solution is putting this in .cargo/config

[build]
rustflags = ["-C", "link-args=-rdynamic"]

Discovered this via a tip here: https://stackoverflow.com/questions/34082636/expose-symbols-to-dynamic-linker-when-linking-with-native-library-in-rust

deepakjois avatar Jun 22 '17 04:06 deepakjois