]> git.saurik.com Git - cycript.git/blob - trampoline.sh
Remove XXX as cur_term is not part of readline.
[cycript.git] / trampoline.sh
1 #!/bin/bash
2
3 set -e
4
5 hpp=$1
6 object=$2
7 name=$3
8 sed=$4
9 lipo=$5
10 nm=$6
11 otool=$7
12
13 exec >"${hpp}"
14
15 detailed=$("${lipo}" -detailed_info "${object}")
16
17 regex=$'\nNon-fat file: .* is architecture: (.*)'
18 if [[ ${detailed} =~ ${regex} ]]; then
19 archs=(${BASH_REMATCH[1]})
20 unset detailed
21 else
22 archs=($(echo "${detailed}" | "${sed}" -e '/^architecture / { s/^architecture //; p; }; d;'))
23 fi
24
25 echo '#include "Trampoline.hpp"'
26
27 for arch in "${archs[@]}"; do
28 if [[ "${detailed+@}" ]]; then
29 offset=$(echo "${detailed}" | "${sed}" -e '
30 /^architecture / { x; s/.*/0/; x; };
31 /^architecture '${arch}'$/ { x; s/.*/1/; x; };
32 x; /^1$/ { x; /^ *offset / { s/^ *offset //; p; }; x; }; x;
33 d;
34 ')
35 else
36 offset=0
37 fi
38
39 file=($("${otool}" -arch "${arch}" -l "${object}" | "${sed}" -e '
40 x; /^1$/ { x;
41 /^ *fileoff / { s/^.* //; p; };
42 /^ *filesize / { s/^.* //; p; };
43 x; }; x;
44
45 /^ *cmd LC_SEGMENT/ { x; s/.*/1/; x; };
46
47 d;
48 '))
49
50 fileoff=${file[0]}
51 filesize=${file[1]}
52
53 echo
54 echo "static const char ${name}_${arch}_data_[] = {"
55
56 od -v -t x1 -t c -j "$((offset + fileoff))" -N "${filesize}" "${object}" | "${sed}" -e '
57 /^[0-7]/ ! {
58 s@^ @// @;
59 s/\(....\)/ \1/g;
60 s@^ // @//@;
61 s/ *$/,/;
62 };
63
64 /^[0-7]/ {
65 s/^[^ ]*//;
66 s/ */ /g;
67 s/^ *//;
68 s/ $//;
69 s/ /,/g;
70 s/\([^,][^,]\)/0x\1/g;
71 s/$/,/;
72 /^,$/ ! { s/^/ /g; p; }; d;
73 };
74 '
75
76 echo "};"
77
78 echo
79 entry=$("${nm}" -arch "${arch}" "${object}" | "${sed}" -e '/ _Start$/ { s/ .*//; p; }; d;')
80 entry=${entry##*(0)}
81 echo "static size_t ${name}_${arch}_entry_ = 0x${entry:=0};"
82
83 echo
84 echo "/*"
85 "${otool}" -arch "${arch}" -vVt "${object}"
86 echo "*/"
87
88 echo
89 echo "static Trampoline ${name}_${arch}_ = {"
90 echo " ${name}_${arch}_data_,"
91 echo " sizeof(${name}_${arch}_data_),"
92 echo " ${name}_${arch}_entry_,"
93 echo "};"
94
95 done