Include Makefile.in to drop automake dependency.
[cycript.git] / trampoline.sh
CommitLineData
eed4f174
JF
1#!/bin/bash
2
eaf660f1
JF
3set -e
4
eed4f174
JF
5hpp=$1
6object=$2
7name=$3
8sed=$4
a243b558
JF
9lipo=$5
10nm=$6
11otool=$7
eed4f174 12
a243b558 13exec >"${hpp}"
eed4f174 14
fae1d9be 15detailed=$("${lipo}" -detailed_info "${object}")
eed4f174 16
6d3af51e
JF
17regex=$'\nNon-fat file: .* is architecture: (.*)'
18if [[ ${detailed} =~ ${regex} ]]; then
19 archs=(${BASH_REMATCH[1]})
20 unset detailed
21else
22 archs=($(echo "${detailed}" | "${sed}" -e '/^architecture / { s/^architecture //; p; }; d;'))
23fi
24
eed4f174
JF
25echo '#include "Trampoline.hpp"'
26
6d3af51e
JF
27for 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
eed4f174
JF
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; };
a243b558 46
eed4f174
JF
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@^ // @//@;
73d3a679 61 s/ *$/,/;
eed4f174
JF
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 "/*"
a243b558 85 "${otool}" -arch "${arch}" -vVt "${object}"
eed4f174
JF
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 "};"
eed4f174 94
a243b558 95done