mirror of https://github.com/pwndbg/pwndbg.git
Add new portable builder for macos + rewrite all (#2528)
parent
066352a37b
commit
1bafe22ae1
@ -1,178 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Original file copied from https://github.com/3noch/nix-bundle-exe
|
||||
# But it was modified/patched for pwndbg usecase!
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
out="$1"
|
||||
binary="$2"
|
||||
|
||||
: "${bin_dir:-}"
|
||||
: "${lib_dir:-}"
|
||||
: "${exe_dir:-}"
|
||||
|
||||
# Converts paths like "folder/bin" to "../.."
|
||||
relative_bin_to_lib=$(echo -n "$bin_dir" | sed 's|[^/]*|..|g')
|
||||
|
||||
clean_path() {
|
||||
echo -n "$1" | sed 's#//*#/#g'
|
||||
}
|
||||
|
||||
printNeeded() {
|
||||
print-needed-elf "$1" | grep '/nix/store/'
|
||||
}
|
||||
|
||||
finalizeBin() {
|
||||
nuke-refs "$1"
|
||||
}
|
||||
|
||||
relpathPathPrint() {
|
||||
relative-path "$1" "$2"
|
||||
}
|
||||
|
||||
bundleLib() {
|
||||
local file="$1"
|
||||
local install_dir="$out/$2"
|
||||
mkdir -p $install_dir
|
||||
|
||||
local real_file
|
||||
real_file=$(realpath "$file")
|
||||
|
||||
local file_name
|
||||
file_name=$(basename "$file")
|
||||
|
||||
local real_file_name
|
||||
real_file_name=$(basename "$real_file")
|
||||
|
||||
local copied_file
|
||||
copied_file="$install_dir/$real_file_name"
|
||||
|
||||
local already_bundled="1"
|
||||
if [ ! -f "$copied_file" ]; then
|
||||
already_bundled="0"
|
||||
cp "$real_file" "$copied_file"
|
||||
chmod +w "$copied_file"
|
||||
fi
|
||||
|
||||
if [ "$file_name" != "$real_file_name" ] && [ ! -f "$install_dir/$file_name" ]; then
|
||||
(cd "$install_dir" && ln -sf "$real_file_name" "$file_name")
|
||||
chmod +w "$install_dir/$file_name"
|
||||
fi
|
||||
|
||||
if [ "$already_bundled" = "1" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo "Bundling $real_file to $install_dir"
|
||||
|
||||
local linked_libs
|
||||
linked_libs=$(printNeeded "$real_file" || true)
|
||||
for linked_lib in $linked_libs; do
|
||||
bundleLib "$linked_lib" "lib"
|
||||
done
|
||||
|
||||
if [ -n "$linked_libs" ]; then
|
||||
relative_any_to_lib=$(relpathPathPrint "$out/$lib_dir" "$copied_file")
|
||||
rpath=$(clean_path "\$ORIGIN/$relative_any_to_lib/$lib_dir")
|
||||
patchelf --set-rpath "$rpath" "$copied_file"
|
||||
fi
|
||||
|
||||
finalizeBin "$copied_file"
|
||||
}
|
||||
|
||||
bundleExe() {
|
||||
local exe="$1"
|
||||
local interpreter="$2"
|
||||
local exe_name
|
||||
exe_name=$(basename "$exe")
|
||||
|
||||
local copied_exe="$out/$exe_dir/$exe_name"
|
||||
cp "$exe" "$copied_exe"
|
||||
chmod +w "$copied_exe"
|
||||
local rpath
|
||||
rpath=$(clean_path "\$ORIGIN/$relative_bin_to_lib/$lib_dir")
|
||||
patchelf --set-interpreter "$(basename "$interpreter")" --set-rpath "$rpath" "$copied_exe"
|
||||
finalizeBin "$copied_exe"
|
||||
|
||||
bundleLib "$interpreter" "lib"
|
||||
|
||||
local linked_libs
|
||||
linked_libs=$(printNeeded "$exe" || true)
|
||||
for linked_lib in $linked_libs; do
|
||||
bundleLib "$linked_lib" "lib"
|
||||
done
|
||||
|
||||
# shellcheck disable=SC2016
|
||||
printf '#!/bin/sh
|
||||
set -eu
|
||||
dir="$(cd -- "$(dirname "$(dirname "$(realpath "$0")")")" >/dev/null 2>&1 ; pwd -P)"
|
||||
exec "$dir"/%s "$dir"/%s "$@"' \
|
||||
"'$lib_dir/$(basename "$interpreter")'" \
|
||||
"'$exe_dir/$exe_name'" \
|
||||
> "$out/$bin_dir/$exe_name"
|
||||
chmod +x "$out/$bin_dir/$exe_name"
|
||||
}
|
||||
|
||||
remove_prefix() {
|
||||
local full_path="$1"
|
||||
local dynamic_prefix="$2"
|
||||
echo "${full_path#$dynamic_prefix}"
|
||||
}
|
||||
|
||||
bundleCustom() {
|
||||
local from_dir="$1"
|
||||
local to_dir="$out/$2"
|
||||
local files
|
||||
|
||||
files=$(find -L "$from_dir" -type f -regex '.*\(\.py\|\.pth\|\.asm\|__doc__\)$' || true)
|
||||
local real_file_dir
|
||||
local real_file_name
|
||||
local install_dir
|
||||
local install_path
|
||||
|
||||
for real_file in $files; do
|
||||
real_file_dir=$(dirname "$(remove_prefix "$real_file" "$from_dir")")
|
||||
real_file_name=$(basename "$real_file")
|
||||
install_dir="$to_dir/$real_file_dir"
|
||||
install_path="$install_dir/$real_file_name"
|
||||
|
||||
mkdir -p $install_dir
|
||||
echo "Copy $real_file to $install_dir"
|
||||
|
||||
# TODO: check symlink like in bundleLib
|
||||
if [ ! -f "$install_path" ]; then
|
||||
cp "$real_file" "$install_path"
|
||||
chmod +w "$install_path"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
bundleDirLib() {
|
||||
local from_dir="$1"
|
||||
local linked_libs_root
|
||||
local path_dir
|
||||
local linked_libs
|
||||
|
||||
bundleCustom "$from_dir" "lib"
|
||||
|
||||
linked_libs_root=$(find -L "$from_dir" -type f -regex '.*\.so\(\..*\|$\)' || true)
|
||||
for linked_lib_root in $linked_libs_root; do
|
||||
path_dir=$(dirname "$(remove_prefix "$linked_lib_root" "$from_dir")")
|
||||
|
||||
bundleLib "$linked_lib_root" "lib/$path_dir"
|
||||
|
||||
linked_libs=$(printNeeded "$linked_lib_root" || true)
|
||||
for linked_lib in $linked_libs; do
|
||||
bundleLib "$linked_lib" "lib"
|
||||
done
|
||||
done
|
||||
}
|
||||
|
||||
exe_interpreter=$(patchelf --print-interpreter "$binary" 2> /dev/null || true)
|
||||
if [ -n "$exe_interpreter" ]; then
|
||||
mkdir -p "$out/$exe_dir" "$out/$bin_dir" "$out/$lib_dir"
|
||||
bundleExe "$binary" "$exe_interpreter"
|
||||
else
|
||||
mkdir -p "$out/$exe_dir" "$out/$bin_dir" "$out/$lib_dir"
|
||||
bundleDirLib "$binary"
|
||||
fi
|
||||
@ -1,71 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
# Original file copied from https://github.com/3noch/nix-bundle-exe
|
||||
# But it was modified/patched for pwndbg usecase!
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
echo "darwin is not supported TODO, https://github.com/3noch/nix-bundle-exe/blob/main/bundle-macos.sh"
|
||||
exit 1
|
||||
#
|
||||
#out="$1"
|
||||
#binary="$2"
|
||||
#
|
||||
#: "${bin_dir:-}"
|
||||
#: "${lib_dir:-}"
|
||||
#
|
||||
## Converts paths like "folder/bin" to "../.."
|
||||
#relative_bin_to_lib=$(echo -n "$bin_dir" | sed 's|[^/]*|..|g')
|
||||
#
|
||||
#mkdir -p "$out/$bin_dir" "$out/$lib_dir"
|
||||
#
|
||||
#clean_path() {
|
||||
# echo -n "$1" | sed 's#//*#/#g'
|
||||
#}
|
||||
#
|
||||
#printNeeded() {
|
||||
# otool -L "$1" | tail -n +2 | grep '/nix/store/' | cut -d '(' -f -1
|
||||
#}
|
||||
#
|
||||
#finalizeBin() {
|
||||
# nuke-refs "$1"
|
||||
# codesign -f -s - "$1" || true
|
||||
#}
|
||||
#
|
||||
#bundleBin() {
|
||||
# local file="$1"
|
||||
# local file_type="$2"
|
||||
#
|
||||
# local real_file
|
||||
# real_file=$(realpath "$file")
|
||||
# local install_dir="$out/$lib_dir"
|
||||
# local rpath_prefix="@loader_path"
|
||||
# if [ "$file_type" == "exe" ]; then
|
||||
# install_dir="$out/$bin_dir"
|
||||
# rpath_prefix=$(clean_path "@executable_path/$relative_bin_to_lib/$lib_dir")
|
||||
# fi
|
||||
#
|
||||
# local copied_file
|
||||
# copied_file="$install_dir/$(basename "$real_file")"
|
||||
# if [ -f "$copied_file" ]; then
|
||||
# return
|
||||
# fi
|
||||
#
|
||||
# echo "Bundling $real_file to $install_dir"
|
||||
# cp "$real_file" "$copied_file"
|
||||
# chmod +w "$copied_file"
|
||||
#
|
||||
# local linked_libs
|
||||
# linked_libs=$(printNeeded "$real_file" || true)
|
||||
# for linked_lib in $linked_libs; do
|
||||
# local real_lib
|
||||
# real_lib=$(realpath "$linked_lib")
|
||||
# local real_lib_name
|
||||
# real_lib_name=$(basename "$real_lib")
|
||||
# install_name_tool -change "$linked_lib" "$rpath_prefix/$real_lib_name" "$copied_file"
|
||||
# bundleBin "$real_lib" "lib"
|
||||
# done
|
||||
#
|
||||
# finalizeBin "$copied_file"
|
||||
#}
|
||||
#
|
||||
#bundleBin "$binary" "exe"
|
||||
@ -1,63 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# Original file copied from https://github.com/3noch/nix-bundle-exe
|
||||
# But it was modified/patched for pwndbg usecase!
|
||||
|
||||
# Prints resolved paths to needed libraries for an ELF executable.
|
||||
# ldd also does this, but it segfaults in some odd scenarios so we avoid it.
|
||||
import sys
|
||||
import os
|
||||
import subprocess
|
||||
from typing import Any, Iterable, List
|
||||
|
||||
|
||||
def eprint(msg: Any):
|
||||
print(msg, file=sys.stderr)
|
||||
|
||||
|
||||
def run(args: List[str]) -> str:
|
||||
result = subprocess.run(args, capture_output=True)
|
||||
if result.returncode != 0:
|
||||
eprint(result.stderr)
|
||||
eprint("Command failed with return code {}: {}".format(result.returncode, args))
|
||||
sys.exit(result.returncode)
|
||||
return result.stdout.decode("utf-8")
|
||||
|
||||
|
||||
def stripped_strs(strs: Iterable[str]) -> Iterable[str]:
|
||||
return (cleaned for x in strs for cleaned in [x.strip()] if cleaned != "")
|
||||
|
||||
|
||||
def get_rpaths(exe: str) -> Iterable[str]:
|
||||
return stripped_strs(run(["patchelf", "--print-rpath", exe]).split(":"))
|
||||
|
||||
|
||||
def resolve_origin(origin: str, paths: Iterable[str]) -> Iterable[str]:
|
||||
return (path.replace("$ORIGIN", origin) for path in paths)
|
||||
|
||||
|
||||
def get_needed(exe: str) -> Iterable[str]:
|
||||
return stripped_strs(run(["patchelf", "--print-needed", exe]).splitlines())
|
||||
|
||||
|
||||
def resolve_paths(needed: Iterable[str], rpaths: List[str]) -> Iterable[str]:
|
||||
existing_paths = lambda lib, paths: (
|
||||
abs_path for path in paths for abs_path in [os.path.join(path, lib)]
|
||||
if os.path.exists(abs_path)
|
||||
)
|
||||
return (
|
||||
found if found is not None else eprint("Warning: can't find {} in {}".format(lib, rpaths))
|
||||
for lib in needed for found in [next(existing_paths(lib, rpaths), None)]
|
||||
)
|
||||
|
||||
|
||||
def main(exe: str):
|
||||
dirname = os.path.dirname(exe)
|
||||
rpaths_raw = list(get_rpaths(exe))
|
||||
rpaths_raw = [dirname] if rpaths_raw == [] else rpaths_raw
|
||||
rpaths = list(resolve_origin(dirname, rpaths_raw))
|
||||
for path in (x for x in resolve_paths(get_needed(exe), rpaths) if x is not None):
|
||||
print(path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(*sys.argv[1:])
|
||||
@ -1,4 +0,0 @@
|
||||
import os.path
|
||||
import sys
|
||||
|
||||
print(os.path.relpath(*sys.argv[1:]))
|
||||
Loading…
Reference in new issue