• XSS.stack #1 – первый литературный журнал от юзеров форума

Static compiled XMRig (Musl)

magic

HDD-drive
Пользователь
Регистрация
24.01.2023
Сообщения
22
Реакции
24
Someday I faced a problem with static compiling xmrig(no dynamic libs). I need to execute my xmrig on almost all x86_64 processors without any problems(like SIGSEVs or old system glibc)
You can also cross compile xmrig for x86, arm, aarch64

As issued here https://github.com/xmrig/xmrig/issues/238
XMRig is written on C++ with GNU glibc, that has a lot of problems with static compiling.

That's why we need to use musl. Lets compile it
First of all, we need to install musl. There is three ways:
1. The easiest: apt-get
2. crosstool-ng or musl-cross-make: You need to build your own musl. (that's cross tool - you can build for x86_64, i686(x86), aarch64, arm and so on processors).
3. use alpine-linux (dark souls mode)
I used musl-cross-make without any problems.

For example, I I have in /opt/cross my x86_64-linux-musl compiler.
Grab the last version xmrig from github

Now we need no compile deps for xmrig.
Need to change scripts for building xmrig. Write scripts/mybuild_deps.sh (example for x86_64 compiler musl) and execute it:
Bash:
#!/bin/bash -e
GCCPATH="/opt/cross/x86_64-linux-musl/bin"
it=x86_64-linux-musl
openssl_it=linux-x86_64
SCRIPTS_PATH=$PWD
export PATH=$GCCPATH:$PATH
export CHOST=$it
export CC=$it-gcc
export AR=$it-ar
export RANLIB=$it-ranlib

echo "=================================== BUILDING HWLOC ========================================"
HWLOC_VERSION_MAJOR="2"
HWLOC_VERSION_MINOR="9"
HWLOC_VERSION_PATCH="0"
HWLOC_VERSION="${HWLOC_VERSION_MAJOR}.${HWLOC_VERSION_MINOR}.${HWLOC_VERSION_PATCH}"
mkdir -p deps
mkdir -p deps/include
mkdir -p deps/lib
mkdir -p build && cd build
wget https://download.open-mpi.org/release/hwloc/v${HWLOC_VERSION_MAJOR}.${HWLOC_VERSION_MINOR}/hwloc-${HWLOC_VERSION}.tar.gz -O hwloc-${HWLOC_VERSION}.tar.gz
tar -xzf hwloc-${HWLOC_VERSION}.tar.gz
cd hwloc-${HWLOC_VERSION}
./configure --disable-shared --enable-static --disable-io --disable-libudev --disable-libxml2 --host=$CHOST
make -j$(nproc || sysctl -n hw.ncpu || sysctl -n hw.logicalcpu)
if [ $? -ne 0 ]; then
  echo "===WAS ERROR==="
  exit
fi
cp -fr include ../../deps
cp hwloc/.libs/libhwloc.a ../../deps/lib
cd $SCRIPTS_PATH

echo "=================================== BUILDING UV ========================================"
UV_VERSION="1.44.2"
mkdir -p build && cd build
wget https://github.com/libuv/libuv/archive/v${UV_VERSION}.tar.gz -O v${UV_VERSION}.tar.gz
tar -xzf v${UV_VERSION}.tar.gz
cd libuv-${UV_VERSION}
sh autogen.sh
./configure --disable-shared --host=$CHOST
make -j$(nproc || sysctl -n hw.ncpu || sysctl -n hw.logicalcpu)
cp -fr include ../../deps
cp .libs/libuv.a ../../deps/lib
cd $SCRIPTS_PATH

echo "=================================== BUILDING OPENSSL ========================================"
OPENSSL_VERSION="1.1.1s"
mkdir -p build && cd build
wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz -O openssl-${OPENSSL_VERSION}.tar.gz
tar -xzf openssl-${OPENSSL_VERSION}.tar.gz
cd openssl-${OPENSSL_VERSION}
./Configure -no-shared -no-asm -no-zlib -no-comp -no-dgram -no-filenames -no-cms $openssl_it
make -j$(nproc || sysctl -n hw.ncpu || sysctl -n hw.logicalcpu)
cp -fr include ../../deps
cp libcrypto.a ../../deps/lib
cp libssl.a ../../deps/lib
cd $SCRIPTS_PATH
This script building openssl, hwloc and uv. These built libs are in scripts/deps/
openssl_it - that's variable for openssl Configure(it require host, but openSSL require different name, not that ./configure uses)

Now, let's build xmrig:
Bash:
#!/bin/bash
LPWD=$PWD/scripts/deps/lib
LPWDI=$PWD/scripts/deps/include
rm -rf build; mkdir -p build; cd build
it="x86_64-linux-musl"
GCCPATH="/opt/cross/x86_64-linux-musl/bin"
ls $LPWDI
cmake -DOPENSSL_CRYPTO_LIBRARY=$LPWD/libcrypto.a \
  -DOPENSSL_SSL_LIBRARY=$LPWD/libssl.a \
  -DOPENSSL_INCLUDE_DIR=$LPWDI -DHWLOC_INCLUDE_DIR=$LPWDI -DUV_INCLUDE_DIR=$LPWDI \
  -DHWLOC_LIBRARY=$LPWD/libhwloc.a -DUV_LIBRARY=$LPWD/libuv.a -DBUILD_STATIC=ON \
  -DCMAKE_CXX_COMPILER=$GCCPATH/$it-g++ -DCMAKE_C_COMPILER=$GCCPATH/$it-gcc \
  -DCMAKE_C_FLAGS="-s" -DCMAKE_CXX_FLAGS="-s" -DWITH_OPENCL=OFF -DWITH_HTTP=OFF -DWITH_CUDA=OFF \
  -DWITH_NVML=OFF -DWITH_ADL=OFF -DWITH_BENCHMARK=OFF ..
make -j4
openSSL library - DOPENSSL_CRYPTO_LIBRARY, DOPENSSL_SSL_LIBRARY, DOPENSSL_INCLUDE_DIR
hwloc library - DHWLOC_LIBRARY, DDHWLOC_INCLUDE_DIR
UV library - DUV_LIBRARY, DUV_INCLUDE_DIR
I turned of this flags, bcs I dont need them and I need small xmrig: DWITH_HTTP, DWITH_CUDA, DWITH_NVML, DWITH_ADL, DWITH_BENCHMARK

So I build this magic:
Bash:
~/xmrig/build/>  file xmrig
xmrig: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped
~/xmrig/build/>  ldd xmrig
    not a dynamic executable
~/xmrig/build/>  ls -lah xmrig
-rwxrwxr-x 1 cock cock 6,2M bbb  6 25:04 xmrig

It can be executed without any SIGSEV:
Bash:
[2023-02-06 21:20:53.473]  net      use pool 127.0.0.1:6666  127.0.0.1
[2023-02-06 21:20:53.473]  net      new job from 127.0.0.1:6666 diff  algo rx/0 height  (6 tx)
[2023-02-06 21:20:53.473]  cpu      use argon2 implementation AVX2
[2023-02-06 21:20:53.482]  msr      cannot read MSR 0x000001a4
[2023-02-06 21:20:53.482]  msr      FAILED TO APPLY MSR MOD, HASHRATE WILL BE LOW
[2023-02-06 21:20:53.482]  randomx  init dataset algo rx/0 (4 threads) seed de19fe83addd186a...
[2023-02-06 21:20:53.483]  randomx  allocated 2336 MB (2080+256) huge pages 0% 0/1168 +JIT (1 ms)
[2023-02-06 21:20:57.966]  net      new job from 127.0.0.1:6666 diff  algo rx/0 height  (12 tx)
[2023-02-06 21:20:59.433]  randomx  dataset ready (5950 ms)
[2023-02-06 21:20:59.433]  cpu      use profile  *  (1 thread) scratchpad 2048 KB
[2023-02-06 21:20:59.434]  cpu      READY threads 1/1 (1) huge pages 0% 0/1 memory 2048 KB (0 ms)
[2023-02-06 21:21:01.062]  net      new job from 127.0.0.1:6666 diff  algo rx/0 height  (12 tx)
[2023-02-06 21:21:05.695]  cpu      accepted (1/0) diff  (76 ms)
[2023-02-06 21:21:20.130]  net      new job from 127.0.0.1:6666 diff  algo rx/0 height  (18 tx)
 
Последнее редактирование:


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх