Skip to content

Commit 3a2f859

Browse files
committed
v0.1.1
1 parent 7b6c4b5 commit 3a2f859

File tree

3 files changed

+60
-45
lines changed

3 files changed

+60
-45
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sharun"
3-
version = "0.0.9"
3+
version = "0.1.1"
44
readme = "README.md"
55
license = "MIT"
66
repository = "https://github.com/VHSgunzo/sharun"

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ cp ./target/$(uname -m)-unknown-linux-musl/release/sharun .
4848

4949
## Usage lib4bin:
5050
```
51-
[ Usage ]: lib4bin [options] /path/executable
51+
[ Usage ]: lib4bin [OPTIONS] /path/executable -- [STRACE CMD ARGS]
5252
5353
[ Options ]:
54-
-s, --strip Strip binaries and libraries (env: STRIP=1)
55-
-v, --verbose Verbose mode (env: VERBOSE=1)
54+
-a, --any-executable Pack any executable (env: ANY_EXECUTABLE=1)
5655
-d, --dst-dir '/path' Destination directory (env: DST_DIR=/path)
57-
-n, --not-one-dir Separate directories for each executable (env: ONE_DIR=0)
58-
-l, --libs-only Pack only libraries (env: LIBS_ONLY=1)
59-
-p, --hard-links Create hard links to sharun (env: HARD_LINKS=1)
60-
-r, --patch-rpath Patch RPATH to a relative path (env: PATCH_RPATH=1)
56+
-e, --strace-mode Use strace for get libs (env: STRACE_MODE=1, STRACE_TIME=5)
6157
-g, --gen-lib-path Generate a lib.path file (env: GEN_LIB_PATH=1)
62-
-a, --any-executable Pack any executable (env: ANY_EXECUTABLE=1)
58+
-h, --help Show this message
6359
-i, --patch-interpreter Patch INTERPRETER to a relative path (env: PATCH_INTERPRETER=1)
60+
-l, --libs-only Pack only libraries (env: LIBS_ONLY=1)
61+
-n, --not-one-dir Separate directories for each executable (env: ONE_DIR=0)
62+
-p, --hard-links Pack sharun and create hard links (env: HARD_LINKS=1)
6463
-q, --quiet-mode Show only errors (env: QUIET_MODE=1)
65-
-e, --strace-mode Use strace for get libs (env: STRACE_MODE=1, STRACE_TIME=5)
66-
-h, --help Show this message
64+
-r, --patch-rpath Patch RPATH to a relative path (env: PATCH_RPATH=1)
65+
-s, --strip Strip binaries and libraries (env: STRIP=1)
66+
-v, --verbose Verbose mode (env: VERBOSE=1)
6767
-w, --with-sharun Pack sharun from PATH or env or download
6868
(env: WITH_SHARUN=1, SHARUN=/path|URL, SHARUN_URL=URL, UPX_SHARUN=1)
6969
```
@@ -74,7 +74,7 @@ cp ./target/$(uname -m)-unknown-linux-musl/release/sharun .
7474
./sharun lib4bin --with-sharun --dst-dir test /bin/bash
7575
7676
# or for correct /proc/self/exe you can use --hard-links flag
77-
./sharun lib4bin --hard-links --with-sharun --dst-dir test /bin/bash
77+
./sharun lib4bin --hard-links --dst-dir test /bin/bash
7878
# this will create hard links from 'test/sharun' in the 'test/bin' directory
7979
8080
# now you can move 'test' dir to other linux system and run binaries from the 'bin' dir

lib4bin

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ PATCH_INTERPRETER=${PATCH_INTERPRETER:=0}
3333
GIT_SHARUN_RELEASES='https://github.com/VHSgunzo/sharun/releases'
3434

3535
usage() {
36-
echo -e "[ Usage ]: lib4bin [options] /path/executable
36+
echo -e "[ Usage ]: lib4bin [OPTIONS] /path/executable -- [STRACE CMD ARGS]
3737
3838
[ Options ]:
39-
-s, --strip Strip binaries and libraries (env: STRIP=1)
40-
-v, --verbose Verbose mode (env: VERBOSE=1)
39+
-a, --any-executable Pack any executable (env: ANY_EXECUTABLE=1)
4140
-d, --dst-dir '/path' Destination directory (env: DST_DIR=/path)
42-
-n, --not-one-dir Separate directories for each executable (env: ONE_DIR=0)
43-
-l, --libs-only Pack only libraries (env: LIBS_ONLY=1)
44-
-p, --hard-links Create hard links to sharun (env: HARD_LINKS=1)
45-
-r, --patch-rpath Patch RPATH to a relative path (env: PATCH_RPATH=1)
41+
-e, --strace-mode Use strace for get libs (env: STRACE_MODE=1, STRACE_TIME=5)
4642
-g, --gen-lib-path Generate a lib.path file (env: GEN_LIB_PATH=1)
47-
-a, --any-executable Pack any executable (env: ANY_EXECUTABLE=1)
43+
-h, --help Show this message
4844
-i, --patch-interpreter Patch INTERPRETER to a relative path (env: PATCH_INTERPRETER=1)
45+
-l, --libs-only Pack only libraries (env: LIBS_ONLY=1)
46+
-n, --not-one-dir Separate directories for each executable (env: ONE_DIR=0)
47+
-p, --hard-links Pack sharun and create hard links (env: HARD_LINKS=1)
4948
-q, --quiet-mode Show only errors (env: QUIET_MODE=1)
50-
-e, --strace-mode Use strace for get libs (env: STRACE_MODE=1, STRACE_TIME=5)
51-
-h, --help Show this message
49+
-r, --patch-rpath Patch RPATH to a relative path (env: PATCH_RPATH=1)
50+
-s, --strip Strip binaries and libraries (env: STRIP=1)
51+
-v, --verbose Verbose mode (env: VERBOSE=1)
5252
-w, --with-sharun Pack sharun from PATH or env or download
5353
(env: WITH_SHARUN=1, SHARUN=/path|URL, SHARUN_URL=URL, UPX_SHARUN=1)"
5454
exit 1
@@ -122,7 +122,7 @@ get_libs() {
122122
if [ "$STRACE_MODE" == 1 ]
123123
then
124124
local libs_file="/tmp/libs.$$"
125-
strace -f -e trace=openat --always-show-pid -o "$libs_file" "$1" &>/dev/null &
125+
strace -f -e trace=openat --always-show-pid -o "$libs_file" "$1" "${STRACE_CMD_ARGS[@]}" &>/dev/null &
126126
sleep $STRACE_TIME
127127
local pids="$(cut -d ' ' -f1<"$libs_file"|sort -u)"
128128
kill $pids 2>/dev/null
@@ -308,6 +308,13 @@ while [[ "$#" -gt 0 ]]; do
308308
esac
309309
done
310310
311+
if [ "$2" == '--' ]
312+
then
313+
STRACE_MODE=1
314+
BINARY_LIST=("$1"); shift 2
315+
STRACE_CMD_ARGS=("$@")
316+
fi
317+
311318
check_deps
312319
313320
if [ "$VERBOSE" == 1 ]
@@ -468,30 +475,36 @@ for binary in "${BINARY_LIST[@]}"
468475
lib_src_name="$(basename "$lib_src_pth")"
469476
grep -qE '/lib32|/i386-linux-gnu'<<<"$lib_src_dirname_pth" && \
470477
lib_dir="lib32"||lib_dir="lib"
471-
lib_dst_dir_pth="${dst_dir_pth}/${lib_dir}$(sed 's|^/usr||;s|^/lib64||;s|^/lib32||;s|^/lib||;s|^/i386-linux-gnu||;s|^/x86_64-linux-gnu||'<<<"$lib_src_dirname_pth")"
478+
lib_dst_dir_pth="${dst_dir_pth}/${lib_dir}$(sed 's|^/usr||;s|^/opt||;s|^/lib64||;s|^/lib32||;s|^/lib||;s|^/i386-linux-gnu||;s|^/x86_64-linux-gnu||'<<<"$lib_src_dirname_pth")"
472479
lib_dst_pth="$lib_dst_dir_pth/$lib_src_name"
473-
try_mkdir "$lib_dst_dir_pth"
474-
if [[ "$HARD_LINKS" == 1 && ! -L "${dst_dir}/${lib_dir}" ]]
480+
if [[ "${LIBRARIES["$lib_dst_pth"]}" != 1 ]]||\
481+
[[ -n "$lib_src_real_pth" && "${LIBRARIES["$lib_src_real_pth"]}" != 1 ]]
475482
then
476-
(try_cd "$dst_dir"
477-
try_ln shared/$lib_dir $lib_dir)||exit 1
478-
fi
479-
if [ -n "$lib_src_real_pth" ]
480-
then
481-
lib_src_real_name="$(basename "$lib_src_real_pth")"
482-
try_cp "$lib_src_real_pth" "$lib_dst_dir_pth/$lib_src_real_name"
483-
if [[ "$lib_src_name" != "$lib_src_real_name" && ! -L "$lib_dst_pth" ]]
483+
try_mkdir "$lib_dst_dir_pth"
484+
if [[ "$HARD_LINKS" == 1 && ! -L "${dst_dir}/${lib_dir}" ]]
485+
then
486+
(try_cd "$dst_dir"
487+
try_ln shared/$lib_dir $lib_dir)||exit 1
488+
fi
489+
if [ -n "$lib_src_real_pth" ]
484490
then
485-
(try_cd "$lib_dst_dir_pth"
486-
try_ln "$lib_src_real_name" "$lib_src_name")||exit 1
487-
fi
488-
else
489-
try_cp "$lib_src_pth" "$lib_dst_dir_pth/$lib_src_name"
490-
fi
491-
repath_needed_libs "$lib_dst_pth"
492-
try_strip "$lib_dst_pth"
493-
if [[ ! "$lib_dst_pth" =~ "$INTERPRETER" ]]
494-
then try_set_rpath "$lib_dst_pth"
491+
lib_src_real_name="$(basename "$lib_src_real_pth")"
492+
try_cp "$lib_src_real_pth" "$lib_dst_dir_pth/$lib_src_real_name"
493+
if [[ "$lib_src_name" != "$lib_src_real_name" && ! -L "$lib_dst_pth" ]]
494+
then
495+
(try_cd "$lib_dst_dir_pth"
496+
try_ln "$lib_src_real_name" "$lib_src_name")||exit 1
497+
fi
498+
LIBRARIES["$lib_src_real_pth"]=1
499+
else
500+
try_cp "$lib_src_pth" "$lib_dst_dir_pth/$lib_src_name"
501+
fi
502+
repath_needed_libs "$lib_dst_pth"
503+
try_strip "$lib_dst_pth"
504+
if [[ ! "$lib_dst_pth" =~ "$INTERPRETER" ]]
505+
then try_set_rpath "$lib_dst_pth"
506+
fi
507+
LIBRARIES["$lib_dst_pth"]=1
495508
fi
496509
LIBRARIES["$lib_src_pth"]=1
497510
fi
@@ -507,7 +520,9 @@ for binary in "${BINARY_LIST[@]}"
507520
patchelf $pvarg --set-interpreter "../$lib_dir/$INTERPRETER" "$binary_name"||exit 1
508521
fi)||exit 1
509522
fi
510-
# sed -i 's|/usr|/xxx|g;s|/lib|/XXX|g;s|/etc|/EEE|g' "${dst_dir}/shared/$lib_dir/$INTERPRETER"||exit 1
523+
interpreter_pth="${dst_dir}/shared/$lib_dir/$INTERPRETER"
524+
info_msg "$YELLOW[ PATCH INTERPRETER ]: $BLUE[$interpreter_pth]"
525+
sed -i 's|/usr|/xxx|g;s|/lib|/XXX|g;s|/etc|/EEE|g' "$interpreter_pth"||exit 1
511526
fi
512527
info_msg "[ DONE ]"
513528
binary_number=$(( $binary_number + 1 ))

0 commit comments

Comments
 (0)