ers35 / luastatic

Build a standalone executable from a Lua program.
Creative Commons Zero v1.0 Universal
775 stars 41 forks source link

Adding shared object to static binary, compiled with musl #45

Open ghost opened 3 months ago

ghost commented 3 months ago

How would I go about adding these to a static binary, compiled with musl?

Just playing around with luastatic, and I wanted to compile a file together with luaossl. After compiling luaossl, I have the Lua bindings, a openssl.o file and an openssl.so file.

So far I've tried creating an archive using ar rcs openssl.a openssl.so, which gives me a static archive. Then, doing lua luastatic.lua test.lua ../luaossl/src/openssl.lua ../luaossl/src/5.3/openssl.a /usr/lib/lua5.3/liblua.a -I/usr/include/lua5.3 -s -static

This results in module '_openssl' not found. require "openssl" works fine, because that is included in the luastatic bundle. But the actual openssl.so is not, so it throws that error.

Cheers in advance!


The test.lua file:

local ssl = require "openssl"
print(ssl.version())
print(_VERSION)
ghost commented 3 months ago

Using Alpine Linux v3.20, kernel 6.6.32-0-virt on x86_64

ers35 commented 3 months ago

This looks like a bug in luastatic where it incorrectly interprets the _openssl module as .openssl.

Try this:

# Install luastatic.
apk add lua5.4-luastatic --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/

# Install required packages for building.
apk add gcc musl-dev openssl-dev openssl-libs-static lua5.4-dev

# Download and build luaossl.
wget https://github.com/wahern/luaossl/archive/refs/tags/rel-20220711.tar.gz
tar -xf rel-20220711.tar.gz
cd luaossl-rel-20220711/src
cc -c openssl.c -I/usr/include/lua5.4

# Create test.luastatic.c but don't compile it.
CC="" luastatic test.lua openssl*.lua /usr/lib/lua5.4/liblua.a openssl.o /usr/lib/libssl.a /usr/lib/libcrypto.a -I/usr/include/lua5.4

# Replace ".openssl" with "_openssl" to workaround the bug.
sed -i -e 's/\.openssl/_openssl/g' test.luastatic.c

# Build the static binary.
cc -Os test.luastatic.c /usr/lib/lua5.4/liblua.a openssl.o /usr/lib/libssl.a /usr/lib/libcrypto.a -I/usr/include/lua5.4 -o test
ghost commented 3 months ago

Did some more experimenting: Moved the openssl.lua file from the luaossl directory to the luastatic directory

Using openssl.so instead of openssl.a gives me attempted static link of dynamic object. lua luastatic.lua test.lua openssl.lua ../luaossl/src/5.3/openssl.so /usr/lib/lua5.3/liblua.a -I/usr/include/lua5.3 -s -static

Using openssl.o instead of openssl.so or openssl.a gives me a ton of undefined reference errors, all from luaossl. lua luastatic.lua test.lua openssl.lua ../luaossl/src/5.3/openssl.so /usr/lib/lua5.3/liblua.a -I/usr/include/lua5.3 -s -static

ghost commented 3 months ago

This looks like a bug in luastatic where it incorrectly interprets the _openssl module as .openssl.

Try this:

# Install luastatic.
apk add lua5.4-luastatic --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/

# Install required packages for building.
apk add gcc musl-dev openssl-dev openssl-libs-static lua5.4-dev

# Download and build luaossl.
wget https://github.com/wahern/luaossl/archive/refs/tags/rel-20220711.tar.gz
tar -xf rel-20220711.tar.gz
cd luaossl-rel-20220711/src
cc -c openssl.c -I/usr/include/lua5.4

# Create test.luastatic.c but don't compile it.
CC="" luastatic test.lua openssl*.lua /usr/lib/lua5.4/liblua.a openssl.o /usr/lib/libssl.a /usr/lib/libcrypto.a -I/usr/include/lua5.4

# Replace ".openssl" with "_openssl" to workaround the bug.
sed -i -e 's/\.openssl/_openssl/g' test.luastatic.c

# Build the static binary.
cc -Os test.luastatic.c /usr/lib/lua5.4/liblua.a openssl.o /usr/lib/libssl.a /usr/lib/libcrypto.a -I/usr/include/lua5.4 -o test

Yup, that seems to work. Compiled a working static executable that has luaossl and OpenSSL in it. Thanks for the quick help, I was beginning to lose hope I was ever gonna get this working.

ghost commented 3 months ago

Should probably keep this open until the bug is fixed lol