wouterken/crystalruby

cannot dynamically load position-independent executable.

Closed this issue ยท 7 comments

On my laptop (Windows 11 + WSL 2 + Ubuntu22.04)

  • ruby 3.2.0 (2022-12-25 revision a528908271) [x86_64-linux]
  • Crystal 1.12.0 [aee9ab69e] (2024-04-09)

I created an empty folder code. run crystalruby init. Then created test.rb with the following contents:

require 'crystalruby'

module MyTestModule
  # The below method will be replaced by a compiled Crystal version
  # linked using FFI.
  crystalize [a: :int, b: :int] => :int
  def add(a, b)
    a + b
  end
end

# This method is run in Crystal, not Ruby!
MyTestModule.add(1, 2) # => 3

After execution the error is reported as follows:

/home/zbw/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/ffi-1.16.3/lib/ffi/dynamic_library.rb:65:in `load_library': Could not open library './crystalruby/lib/crlib': ./crystalruby/lib/crlib: cannot dynamically load position-independent executable. (LoadError)
Searched in <system library path>, /usr/lib, /usr/local/lib, /opt/local/lib
        from /home/zbw/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/ffi-1.16.3/lib/ffi/library.rb:95:in `block in ffi_lib'
        from /home/zbw/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/ffi-1.16.3/lib/ffi/library.rb:94:in `map'
        from /home/zbw/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/ffi-1.16.3/lib/ffi/library.rb:94:in `ffi_lib'
        from /home/zbw/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/crystalruby-0.1.10/lib/crystalruby.rb:243:in `attach_crystal_ruby_lib!'
        from /home/zbw/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/crystalruby-0.1.10/lib/crystalruby.rb:329:in `compile!'
        from /home/zbw/.rbenv/versions/3.2.0/lib/ruby/gems/3.2.0/gems/crystalruby-0.1.10/lib/crystalruby.rb:88:in `block (3 levels) in attach_crystalized_method'
        from test.rb:13:in `<main>'

I reproduced this issue on Ubuntu 24.04 with Ruby 3.1, 3.2, 3.3 and trunk alongside Crystal 1.11.2.

Could not open library './crystalruby/lib/crlib': ./crystalruby/lib/crlib: cannot dynamically load position-independent executable. (LoadError)

๐Ÿ‘ Thanks for your report. I can reproduce the above.
I believe I can manually circumvent this, by compiling to an object file first and then explicitly, manually linking as a shared library. After this it seems as if the library can be successfully loaded dynamically.

Will have a go at this soon.

Same error on Ubuntu 22.04.1, ruby 3.3.0

Ok, I've pushed v0.1.12 which I believe takes care of the issue! Please try it out and let me know if things are any better.
(If you encounter any issues, try and start with a clean build dir for good measure)

Confirmed this resolved the issue for me. Thank you!

Thanks for the confirmation @havenwood!
๐Ÿ™
I will close this issue now assuming this also takes care of the problem for @zhuangbiaowei.
Feel free to create a new issue, if the problem persists.

@wouterken
Thank you for your work on this.

Works for me as well.
I'll add that I had to delete the crystalruby folder and run crystalruby init again.