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

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

Open
ghost opened this issue Jun 8, 2024 · 5 comments
Open

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

ghost opened this issue Jun 8, 2024 · 5 comments
Labels

Comments

@ghost
Copy link

ghost commented Jun 8, 2024

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
Copy link
Author

ghost commented Jun 8, 2024

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

@ers35
Copy link
Owner

ers35 commented Jun 8, 2024

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
Copy link
Author

ghost commented Jun 8, 2024

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

@ers35 ers35 added the bug label Jun 8, 2024
@ghost
Copy link
Author

ghost commented Jun 8, 2024

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 ghost closed this as completed Jun 8, 2024
@ghost
Copy link
Author

ghost commented Jun 8, 2024

Should probably keep this open until the bug is fixed lol

@ghost ghost reopened this Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant