Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static lib #49

Closed
wants to merge 4 commits into from
Closed

Static lib #49

wants to merge 4 commits into from

Conversation

ethanfrey
Copy link
Member

Builds on #48

Closes #45

This builds out a static library

@ethanfrey
Copy link
Member Author

ethanfrey commented Feb 26, 2020

This compiles with muslc now!!! (Thanks to wasmerio/wasmer#1173)

Here is the linking info. Master:

$ ldd api/libgo_cosmwasm.so
        linux-vdso.so.1 (0x00007ffc68589000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb3589f0000)
        librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fb3587e8000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb3585c9000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb3583b1000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb357fc0000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fb359074000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb357c22000)

After make build-rust (which is RUSTFLAGS="-C target-feature=-crt-static" rustup run nightly cargo build --release --features backtraces --target x86_64-unknown-linux-musl):

$ ldd api/libgo_cosmwasm.a
        not a dynamic executable

Looking good

@ethanfrey
Copy link
Member Author

However, running against go code shows one more problem:

Seems I need to make sure all the libs we statically linked to are -fPIC compatible

RUST_BACKTRACE=1 go test -v ./api .
# github.com/confio/go-cosmwasm/api
/usr/bin/ld: api/libgo_cosmwasm.a(fileline.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_pcinfo.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(posix.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_close.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(state.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_create_state.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(elf.o): relocation R_X86_64_32 against `.rodata.elf_nodebug.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(alloc.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_alloc.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(dwarf.o): relocation R_X86_64_32 against `.rodata.dwarf_buf_error.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(read.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_get_view.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
# github.com/confio/go-cosmwasm/api
/usr/bin/ld: api/libgo_cosmwasm.a(fileline.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_pcinfo.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(posix.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_close.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(state.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_create_state.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(elf.o): relocation R_X86_64_32 against `.rodata.elf_nodebug.str1.8' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(alloc.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_alloc.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(dwarf.o): relocation R_X86_64_32 against `.rodata.dwarf_buf_error.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: api/libgo_cosmwasm.a(read.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_get_view.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
FAIL    github.com/confio/go-cosmwasm/api [build failed]
Makefile:58: recipe for target 'test' failed
make: *** [test] Error 2

@ethanfrey
Copy link
Member Author

To reproduce (on Linux):

  • run make build-rust-static
  • ensure that there is a .a and not a .so file inside api/
  • add some whitespace to api/lib.go to force it to recompile/relink with libgo_cosmwasm.a
  • make test

You should see the above errors (which go away after repeating above steps with make build-rust-release)

@ethanfrey
Copy link
Member Author

ethanfrey commented Feb 26, 2020

Based on https://stackoverflow.com/questions/56825218/golang-gcc-c-existing-static-library-can-not-be-used-when-making-a-pie-object

I added CGO_LDFLAGS=-no-pie before make test, which removed the -fPIC issue, but came to yet another:

Errors seem (almost) all related to nix and backtrace libs

api/libgo_cosmwasm.a(std-bf8cf24011a6ae41.std.46iane2s-cgu.0.rcgu.o): In function `std::sys::unix::weak::fetch':
/rustc/6fd8798f4de63328d743eb2a9a9c12e202a4a182//src/libstd/sys/unix/weak.rs:61: undefined reference to `dlsym'
api/libgo_cosmwasm.a(backtrace-38ed14882379f56b.backtrace.e153ahi4-cgu.0.rcgu.o): In function `backtrace::symbolize::dladdr::resolve':
/cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/symbolize/dladdr.rs:68: undefined reference to `dladdr'
/cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/symbolize/dladdr.rs:68: undefined reference to `dladdr'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::AioCb::cancel':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5AioCb6cancel17h1e767155769d80f0E+0x8): undefined reference to `aio_cancel'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::AioCb::error':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5AioCb5error17h81044a17bdd83d30E+0x6): undefined reference to `aio_error'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::AioCb::fsync':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5AioCb5fsync17h19db3877bc32d555E+0xb): undefined reference to `aio_fsync'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::AioCb::read':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5AioCb4read17h1087bff7a4b41135E+0xf): undefined reference to `aio_read'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::AioCb::aio_return':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5AioCb10aio_return17h06e59254c10a753fE+0x10): undefined reference to `aio_return'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::AioCb::write':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5AioCb5write17hefd45425da618f33E+0x6): undefined reference to `aio_write'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::aio_cancel_all':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio14aio_cancel_all17h45165f8970d29bf6E+0x5): undefined reference to `aio_cancel'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::aio_suspend':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio11aio_suspend17hb785d2745fa24fa9E+0x15): undefined reference to `aio_suspend'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::LioCb::listio':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5LioCb6listio17ha8848efb4a0a45abE+0x13b): undefined reference to `lio_listio'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::LioCb::listio_resubmit':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5LioCb15listio_resubmit17hbe5a817a861669d5E+0x1a9): undefined reference to `aio_error'
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5LioCb15listio_resubmit17hbe5a817a861669d5E+0x1f7): undefined reference to `aio_return'
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5LioCb15listio_resubmit17hbe5a817a861669d5E+0x290): undefined reference to `lio_listio'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::LioCb::aio_return':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5LioCb10aio_return17h4fffaba5b98bd12bE+0x3e): undefined reference to `aio_return'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.13.rcgu.o): In function `nix::sys::aio::LioCb::error':
nix.cb7qzj33-cgu.13:(.text._ZN3nix3sys3aio5LioCb5error17hc027158ee796e2baE+0x33): undefined reference to `aio_error'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.3.rcgu.o): In function `nix::mqueue::mq_open':
nix.cb7qzj33-cgu.3:(.text._ZN3nix6mqueue7mq_open17h65345ac433a41656E+0xd): undefined reference to `mq_open'
nix.cb7qzj33-cgu.3:(.text._ZN3nix6mqueue7mq_open17h65345ac433a41656E+0x1e): undefined reference to `mq_open'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.3.rcgu.o): In function `nix::mqueue::mq_unlink':
nix.cb7qzj33-cgu.3:(.text._ZN3nix6mqueue9mq_unlink17ha09d15b94da06d69E+0x6): undefined reference to `mq_unlink'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.3.rcgu.o): In function `nix::mqueue::mq_close':
nix.cb7qzj33-cgu.3:(.text._ZN3nix6mqueue8mq_close17hf2c426251a4f6447E+0x3): undefined reference to `mq_close'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.3.rcgu.o): In function `nix::mqueue::mq_receive':
nix.cb7qzj33-cgu.3:(.text._ZN3nix6mqueue10mq_receive17h2f91c5a0f4494de8E+0x11): undefined reference to `mq_receive'
api/libgo_cosmwasm.a(nix-8b20c6ae38176a0c.nix.cb7qzj33-cgu.3.rcgu.o): In function `nix::mqueue::mq_send':
nix.cb7qzj33-cgu.3:(.text._ZN3nix6mqueue7mq_send17h0e887d0ced2a6720E+0x3): undefined reference to `mq_send'

@ethanfrey ethanfrey added this to Backlog in Blockchain Development Mar 10, 2020
@ethanfrey ethanfrey moved this from Backlog to Improved Ops in Blockchain Development Mar 10, 2020
@ethanfrey
Copy link
Member Author

Okay, so I realized I really do want cdylib not staticlib. Fixed some flags and getting a "better" issue. This is the output on make build-rust:

/usr/bin/ld: /tmp/rustcmQSGMY/libbacktrace_sys-d24d8b6e50111e4a.rlib(alloc.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_alloc.str1.1' can not be used when making a shared object; recompile with -fPIC
          /usr/bin/ld: /tmp/rustcmQSGMY/libbacktrace_sys-d24d8b6e50111e4a.rlib(dwarf.o): relocation R_X86_64_32 against `.rodata.dwarf_buf_error.str1.1' can not be used when making a shared object; recompile with -fPIC
          /usr/bin/ld: /tmp/rustcmQSGMY/libbacktrace_sys-d24d8b6e50111e4a.rlib(fileline.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_pcinfo.str1.8' can not be used when making a shared object; recompile with -fPIC
          /usr/bin/ld: /tmp/rustcmQSGMY/libbacktrace_sys-d24d8b6e50111e4a.rlib(posix.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_close.str1.1' can not be used when making a shared object; recompile with -fPIC
          /usr/bin/ld: /tmp/rustcmQSGMY/libbacktrace_sys-d24d8b6e50111e4a.rlib(read.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_get_view.str1.1' can not be used when making a shared object; recompile with -fPIC
          /usr/bin/ld: /tmp/rustcmQSGMY/libbacktrace_sys-d24d8b6e50111e4a.rlib(state.o): relocation R_X86_64_32 against `.rodata.__rbt_backtrace_create_state.str1.8' can not be used when making a shared object; recompile with -fPIC
          /usr/bin/ld: /tmp/rustcmQSGMY/libbacktrace_sys-d24d8b6e50111e4a.rlib(elf.o): relocation R_X86_64_32 against `.rodata.elf_nodebug.str1.8' can not be used when making a shared object; recompile with -fPIC
          /usr/bin/ld: final link failed: Nonrepresentable section on output
          collect2: error: ld returned 1 exit status

This may be a hint: onelson/jq-src@c37f1a9 but I give up. Happy for someone else to try.

@webmaster128
Copy link
Member

Okay, so I realized I really do want cdylib not staticlib.

Why do you think that? Wouln't it be desired to have an .a instead of a .so, that is linked statically into the final binary?

@ethanfrey
Copy link
Member Author

I guess I was trying for something easier.

I am happy if you check out this branch and see if you can get any further with the compile errors (using google or your own code-fu). I think I left build-rust-static and build-rust-muslc there (at least in an intermediate commit).

@ethanfrey
Copy link
Member Author

Replaced by #106 which shows how to compile a static *.a as well as a fully static go binary using the alpine docker images. (make test-alpine)

@ethanfrey ethanfrey closed this Jun 15, 2020
Blockchain Development automation moved this from Improved Ops to Done Jun 15, 2020
@webmaster128 webmaster128 deleted the static-lib branch June 15, 2020 21:55
tomtau pushed a commit to tomtau/wasmvm that referenced this pull request Oct 6, 2021
- change tag name: muslc -> static
- unify link_muslc.go and link_static.go -> link_static.go
- update build files: Dockerfile, Makefile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Try building static *.so file
2 participants