This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpelf
309 lines (275 loc) · 11 KB
/
pelf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/bin/sh
# shellcheck disable=SC2034
# shellcheck disable=SC3028
# PELF. Pack an ELF.
# PELF receives a binary as its first argument and an output as its second argument. It will pack all the necessary libraries that it needs to run, except the libC and LD. PELFs may include optional files inside, be it a library, binary, folder or file.
LICENSE="BSD-3-Clause License\n
Copyright <2024> <[email protected]>\n
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.\n
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \“AS IS\” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n
"
# Check if the required arguments are provided
if [ "$#" -lt 2 ]; then
echo "Usage: $0 [ELF_SRC_PATH] [DST_PATH.blob] <--add-library [LIB_PATH]> <--add-binary [BIN_PATH]> <--add-metadata [icon128x128.xpm|icon128x128.png|app.desktop]> <--add-arbitrary [DIR|FILE]>" >&2
exit 1
fi
# Create a temporary directory for bundling the files
outer_tmp_dir="/tmp/pelf_$(date '+%s%M%S')_$RANDOM"
tmp_dir="$outer_tmp_dir/pelf_$(date '+%s%M%S')_$RANDOM"
mkdir -p "$tmp_dir/bin" "$tmp_dir/libs" || exit 1
trap 'rm -rf "$outer_tmp_dir"' EXIT
src="$1"
dst="$2"
# Function to figure out what libraries the binaries need/depend on.
add_thelibs() {
# Copy the libraries from the executable to the temporary directory
ldd "$1" | awk '
# Store the first word of the first line
NR == 1 { first_word = $1 }
# For lines with =>, check if the third word is not the same as the first word of the first line
/=>/ && $3 != first_word { print $3 }
' | while read -r lib; do
# Copy the library to the temporary directory
cp -L "$lib" "$tmp_dir/libs" || exit 1
done
}
# Function to handle adding libraries
add_library() {
lib="$1"
cp -L "$lib" "$tmp_dir/libs" || exit 1
add_thelibs "$lib" || exit 1
}
# Function to handle adding binaries
add_binary() {
binary="$1"
add_thelibs "$binary"
cp -L "$binary" "$tmp_dir/bin/$(basename "$binary")" || exit 1
}
add_arbitrary() {
cp -LR "$1" "$tmp_dir" || { echo "Failed to copy arbitrary files"; exit 1; }
}
add_metadata() {
case "$1" in
*.png | *.xpm | *.desktop)
mkdir -p "$tmp_dir"/bundledMetadata || { echo "Failed to create directory"; exit 1; }
cp -LR -- "$1" "$tmp_dir"/bundledMetadata/icon128x128."${1##*.}" || { echo "Failed to copy file"; exit 1; }
;;
*)
echo "File extension not supported. Must be .png, .xpm or .desktop. If you supply an icon, size must be 128x128"
exit 1
;;
esac
}
shift 2 # To access --(add)itional arguments
# Process the optional arguments --add-library, --add-binary, and --add-arbitrary
while [ "$#" -gt 0 ]; do
case "$1" in
--add-library)
add_library "$2"
shift 2
;;
--add-binary)
add_binary "$2"
shift 2
;;
--add-metadata)
add_metadata "$2"
shift 2
;;
--add-arbitrary)
add_arbitrary "$2"
shift 2
;;
*)
echo "Invalid argument: $1" >&2
exit 1
;;
esac
done
# Copy the executable to the temporary directory
add_binary "$src" || exit 1
# Create a tar archive of the executable, libraries and additional files.
if ! tar -C "$tmp_dir" -czf "$outer_tmp_dir/archive.tar.gz" .; then
echo "Extraction failed" >&2
exit 1
fi
# Create a self-extracting script
cat > "$dst" << 'SCRIPT'
#!/bin/sh
# Get the binary's name
rEXE_NAME="$(basename "$0" .blob)"
[ -n "$EXE_NAME" ] || EXE_NAME="$rEXE_NAME"
TMPDIR="/tmp/.pelfbundles/pbundle_$rEXE_NAME$(date '+%s%M%S')_$RANDOM"
LIBS_BULKDIR="/tmp/pelfbundle_libs-bulkdir"
cleanup() {
if [ -z "$found_runningInstance" ] || [ "$found_runningInstance" != "1" ]; then
# Delete individual files only if they are used exclusively by the current process
for file in $REM_AFTERUSE; do
if [ -z "$(fuser "$file" 2>/dev/null | grep "$EXE_NAME_PID")" ]; then
rm "$file"
fi
done
# Delete the directory
rm -rf "$TMPDIR"
fi
}
# Set up the trap
trap cleanup EXIT
###########################################################
# BASH version
#set_tmpdir_from_env() {
# # Construct the variable name by appending _bindir to $rEXENAME
# var_name="${rEXE_NAME}_bindir"
#
# # Check if the constructed variable name exists and is not empty
# if [ -n "${!var_name}" ]; then
# # Set TMPDIR to the directory name of the constructed variable
# TMPDIR="$(dirname "${!var_name}")"
# found_runningInstance=1
# return
# fi
#}
set_tmpdir_from_env() {
# Construct the variable name by appending _bindir to $rEXENAME
var_name="${rEXE_NAME}_bindir"
# Check if the constructed variable name exists and is not empty
eval "var_value=\${$var_name}"
if [ -d "$var_value" ]; then
# Set TMPDIR to the directory name of the constructed variable
TMPDIR="$(dirname "$var_value")"
found_runningInstance=1
return
fi
}
set_tmpdir_from_env
if [ -z "$found_runningInstance" ] || [ "$found_runningInstance" != "1" ]; then
# Find the start position of the archive
ARCHIVE_MARKER=$(awk '/^__ARCHIVE_MARKER__/ { print NR + 1; exit }' "$0")
# Construct the variable name by appending _bindir to $rEXENAME
var_name="${rEXE_NAME}_bindir"
# Decode the base64-encoded archive and extract it
mkdir -p "$TMPDIR" && tail -n +$ARCHIVE_MARKER "$0" | base64 -d | tar -xzf - -C "$TMPDIR" >/dev/null 2>&1 || {
# Use eval to check if the constructed variable name exists and is not empty
echo "Extraction failed" >&2
eval "var_value=\"\${$var_name}\""
exit 1
}
fi
# Function to check if a library is found in system paths
is_library_in_system() {
library=$1
if [ -e "/usr/lib/$library" ] || [ -e "/lib/$library" ] || [ -e "/lib64/$library" ]; then
return 0 # Library found in system
else
return 1 # Library not found in system
fi
}
# Check if USE_SYSTEM_LIBRARIES is set to 1 or doesn't exist
if [ "${USE_SYSTEM_LIBRARIES:-0}" -eq 1 ]; then
for lib_file in "$TMPDIR/libs/"*; do
lib_name=$(basename "$lib_file")
if is_library_in_system "$lib_name"; then
if [ "$SHOW_DISCARDPROCESS" -eq 1 ]; then
echo "$lib_name found in system. Using the system's library."
fi
rm "$lib_file"
else
if [ "$SHOW_DISCARDPROCESS" -eq 1 ]; then
echo "$lib_name not found in system. Using the bundl
ed library."
fi
fi
done 2>/dev/null
fi
mv_u() {
SRC_DIR="$1"
DEST_DIR="$2"
# Loop through each file in the source directory
for file in "$SRC_DIR"/*; do
# Check if the file is a regular file
[ -f "$file" ] || continue
# Extract the filename from the path
filename=$(basename "$file")
# Check if the file does not exist in the destination directory or is newer
if [ ! -e "$DEST_DIR/$file" ]; then
REM_AFTERUSE="$REM_AFTERUSE $DEST_DIR/$filename "
mv "$file" "$DEST_DIR/"
elif [ "$(find "$file" -newer "$DEST_DIR/$filename" 2>/dev/null)" ]; then
# Move the file to the destination directory
mv "$file" "$DEST_DIR/"
fi
done
}
# Add extra binaries to the PATH, if they are there.
if [ "$(ls -1 "$TMPDIR"/bin | wc -l)" -gt 1 ]; then
if [ -z "$found_runningInstance" ] || [ "$found_runningInstance" != "1" ]; then
export "$(echo "$rEXE_NAME" | sed -E 's/[-.]([a-zA-Z])/\U\1/g; s/[-.]//g')_bindir"="$TMPDIR/bin"
export "$(echo "$rEXE_NAME" | sed -E 's/[-.]([a-zA-Z])/\U\1/g; s/[-.]//g')_libs"="$TMPDIR/libs"
fi
xPATH="$TMPDIR/bin:$PATH"
USE_BULKLIBS=0
fi
# Figure out what we do
binDest="$TMPDIR/bin/$EXE_NAME"
case "$1" in
--pbundle_help)
printf "Description: Pack an ELF\n"
printf "Usage:\n <--pbundle_link <binary>|--pbundle_help|--pbundle_xpmIcon|--pbundle_pngIcon|--pbundle_desktop> <args...>\n"
printf "EnvVars:\n USE_BULKLIBS=[0,1]\n USE_SYSTEM_LIBRARIES=[1,0]\n SHOW_DISCARDPROCESS=[0,1]\n HELP_PAGE_LIST_PACKEDFILES=[0,1]\n"
if [ "$HELP_PAGE_LIST_PACKEDFILES" = "1" ]; then
ls "$TMPDIR"/*
fi
exit 1
;;
--pbundle_link)
binDest="$2"
shift 2
;;
--pbundle_xpmIcon)
icon_path="$TMPDIR/bundledMetadata/icon128x128.xpm"
if [ -f "$icon_path" ]; then
base64 "$icon_path"
exit 0
else
exit 1
fi
;;
--pbundle_pngIcon)
icon_path="$TMPDIR/bundledMetadata/icon128x128.png"
if [ -f "$icon_path" ]; then
base64 "$icon_path"
exit 0
else
exit 1
fi
;;
--pbundle_desktop)
desktop_path="$TMPDIR/bundledMetadata/app.desktop"
if [ -f "$desktop_path" ]; then
base64 "$desktop_path"
exit 0
else
exit 1
fi
;;
esac
# Execute the binary with extracted libraries using LD_LIBRARY_PATH
if [ "${USE_BULKLIBS:-0}" -eq 1 ]; then
mkdir -p "$LIBS_BULKDIR"
mv_u "$TMPDIR/libs" "$LIBS_BULKDIR"
PATH="$PATH:$xPATH" LD_LIBRARY_PATH="$LIBS_BULKDIR" SELF_TEMPDIR="$TMPDIR" "$binDest" "$@" || exit 1
EXE_NAME_PID="$!"
else
PATH="$PATH:$xPATH" LD_LIBRARY_PATH="$TMPDIR/libs" SELF_TEMPDIR="$TMPDIR" "$binDest" "$@" || exit 1
EXE_NAME_PID="$!"
fi
exit $?
__ARCHIVE_MARKER__
SCRIPT
# Append the base64-encoded archive to the self-extracting script
base64 <"$outer_tmp_dir/archive.tar.gz" >> "$dst" || exit 1
# Make the self-extracting script executable
chmod +x "$dst" || exit 1