GetFirefly / firefly

An alternative BEAM implementation, designed for WebAssembly
Apache License 2.0
3.61k stars 104 forks source link

`firefly compile` fails during linking due to undefined reference to `init:boot/1` #718

Closed tatsuya6502 closed 2 years ago

tatsuya6502 commented 2 years ago

When trying to compile an Erlang source file from a test, init:boot/1 symbol cannot be found during linking. Tried on Linux x86_64 and macOS arm64 and got the same result.

init.erl

native_implemented/otp/tests/internal/lib/erlang/display_1/with_atom/init.erl

Terminal Log

Linux x86_64

$ ./bin/firefly compile native_implemented/otp/tests/internal/lib/erlang/display_1/with_atom/init.erl
   Compiling native_implemented/otp/tests/internal/lib/erlang/display_1/with_atom/init.erl
    Compiled init
error: linking with `cc` failed: exit status: 1
 = "cc" "-m64" "init.o" "-Wl,--as-needed" "-L" ".../firefly/bin/x86_64-unknown-linux-gnu/lib/fireflylib/x86_64-unknown-linux-gnu/lib" "-Wl,--whole-archive" "-lfirefly_rt_tiny" "-Wl,--no-whole-archive" ".../firefly/bin/x86_64-unknown-linux-gnu/lib/fireflylib/x86_64-unknown-linux-gnu/lib/libpanic_unwind.rlib" ".../firefly/bin/x86_64-unknown-linux-gnu/lib/fireflylib/x86_64-unknown-linux-gnu/lib/libpanic.rlib" ".../firefly/bin/x86_64-unknown-linux-gnu/lib/fireflylib/x86_64-unknown-linux-gnu/lib/libunwind.rlib" "-lc" "-lm" "-ldl" "-lpthread" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" ".../firefly/bin/x86_64-unknown-linux-gnu/lib/fireflylib/x86_64-unknown-linux-gnu/lib" "-o" ".../firefly/_build/firefly/x86_64-unknown-linux-gnu/init" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
 = /usr/bin/ld: .../firefly/bin/x86_64-unknown-linux-gnu/lib/fireflylib/x86_64-unknown-linux-gnu/lib/libfirefly_rt_tiny.a(firefly_rt_tiny-5890d8766d6fd245.4wup9wghl2ttk4tp.rcgu.o): in function `firefly_rt_tiny::init::start::{{closure}}':
   .../firefly/runtimes/tiny/src/init/mod.rs:35: undefined reference to `init:boot/1'
   collect2: error: ld returned 1 exit status
 = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
 = use the `-l` flag to specify native libraries to link

      Linker generated executable to .../firefly/_build/firefly/x86_64-unknown-linux-gnu/init
    Finished built init in 2s

$ ls -l _build/firefly/x86_64-unknown-linux-gnu/
total 0
$

Environment

bitwalker commented 2 years ago

Currently, Erlang modules must export an init:boot/1 symbol that defines the entry point for executables. I'm currently working on a set of changes that ships a builtin init and treats Erlang sources more like erlc does, but this is just a side effect of building things up incrementally.

NOTE: The runtime is quite limited at the moment while I finalize the design for the GC and scheduler integrations, so you should expect that many basic things are missing. While you can compile pretty much any Erlang sources, linking will likely fail due to missing symbols as there is no standard library being built right now, it is truly bare bones Erlang. That won't be the case much longer, but just want to give you some context for your experimentation 😃

tatsuya6502 commented 2 years ago

Thank you for sharing the current project status and the plan 🙂 It was really helpful to me to understand what I can do now.

Currently, Erlang modules must export an init:boot/1 symbol that defines the entry point for executables.

I see. So, I edited the init.erl file to rename start/0 function to boot/1. And it worked!

--- a/native_implemented/otp/tests/internal/lib/erlang/display_1/with_atom/init.erl
+++ b/native_implemented/otp/tests/internal/lib/erlang/display_1/with_atom/init.erl
@@ -1,6 +1,6 @@
 -module(init).
--export([start/0]).
+-export([boot/1]).
 -import(erlang, [display/1]).

-start() ->
+boot(_) ->
   display(atom).
$ ./bin/firefly compile native_implemented/otp/tests/internal/lib/erlang/display_1/with_atom/init.erl
   Compiling native_implemented/otp/tests/internal/lib/erlang/display_1/with_atom/init.erl
    Compiled init
      Linker generated executable to ... /_build/firefly/x86_64-unknown-linux-gnu/init
    Finished built init in 1s
​
$ file ./_build/firefly/x86_64-unknown-linux-gnu/init
./_build/firefly/x86_64-unknown-linux-gnu/init: ELF 64-bit LSB pie executable, 
x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, 
BuildID[sha1]=5ea7145898756dc05bdcf6de3fcf2a40affc1e84, 
for GNU/Linux 3.2.0, with debug_info, not stripped
​
$ ./_build/firefly/x86_64-unknown-linux-gnu/init
atom

I also realized that the CI job x86_64-unknown-linux-gnu-compiler is running cargo make test-lit regularly, and its test files in tests/lit directory are up to date.

NOTE: The runtime is quite limited at the moment while I finalize the design for the GC and scheduler integrations, so you should expect that many basic things are missing.

I will be playing around with Firefly a bit more but I will try not to bother you until your work is done. Thank you for your hard work!

bitwalker commented 2 years ago

I'm glad you were able to get the sample program running! Always good to have someone else reproduce a successful build :)

I expect to have some updates soon that will open up the scope of what is possible quite a bit, but feel free to ask any questions you have in the meantime!