-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfind_glibc_mapping.sh
executable file
·60 lines (49 loc) · 1.19 KB
/
find_glibc_mapping.sh
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
#!/bin/sh
die() {
echo "$0: ERROR - $*" 1>&2
exit 1
}
show_usage() {
echo "Usage: $0 [options] <pid>"
echo
echo "OPTIONS"
echo " -a, --add OFFSET Add the given hexidecimal offset"
echo " to the mapped base address."
}
if test $# -lt 1 ; then
show_usage
exit 1
fi
mode=print
offset=0
if type getopt 2>&1 >/dev/null ; then
# have GNU getopt (allows nicer options)
SOPT="ha:"
LOPT="help,add:"
OPTIONS=$(getopt -o "$SOPT" --long "$LOPT" -n "$0" -- "$@") || exit 1
eval set -- "$OPTIONS"
fi
while true ; do
case "$1" in
-h | --help) show_usage ; exit 0 ;;
-a | --add) mode=add ; offset=$2 ; shift 2 ;;
--) shift ; break ;;
-*) die "bad opt: $1" ;;
*) break ;;
esac
done
pid=$1
find_mapping_addr() {
grep '/libc-[0-9.]*\.so$' /proc/${pid}/maps |
grep ' r-xp ' |
cut -d- -f 1
}
mapping_addr="$(find_mapping_addr)"
sum_base_and_offset() {
echo "ibase = 16; obase = 10; ${offset} + ${mapping_addr}" | bc
}
case $mode in
print) echo "${mapping_addr}" ;;
add) sum_base_and_offset ;;
*) die "not a mode: \"${mode}\"" ;;
esac