this repo has no description
1#!/usr/bin/env python
2# This file is part of Darling.
3#
4# Copyright (C) 2017 Lubos Dolezel
5#
6# Darling is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# Darling is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with Darling. If not, see <http://www.gnu.org/licenses/>.
18
19import sys
20import subprocess
21
22def usage():
23 print("Usage: %s <Mach-O> <output directory>" % sys.argv[0])
24
25if len(sys.argv) != 3:
26 usage()
27
28macho = sys.argv[1]
29dest = sys.argv[2]
30
31out = subprocess.check_output(["nm", "-Ug", macho])
32
33functions = []
34for line in out.splitlines():
35 if line == "":
36 continue
37 address, id, name = line.split(" ")
38 # Remove the underscore
39 name = name[1 : ]
40
41 if id == "T":
42 functions.append(name)
43
44header = open(dest + "/functions.h", "w")
45source = open(dest + "/functions.c", "w")
46
47copyright ="""/*
48This file is part of Darling.
49
50Copyright (C) 2018 Lubos Dolezel
51
52Darling is free software: you can redistribute it and/or modify
53it under the terms of the GNU General Public License as published by
54the Free Software Foundation, either version 3 of the License, or
55(at your option) any later version.
56
57Darling is distributed in the hope that it will be useful,
58but WITHOUT ANY WARRANTY; without even the implied warranty of
59MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60GNU General Public License for more details.
61
62You should have received a copy of the GNU General Public License
63along with Darling. If not, see <http://www.gnu.org/licenses/>.
64*/
65
66"""
67
68header.write(copyright)
69source.write(copyright)
70
71source.write("""
72#include <stdlib.h>
73static int verbose = 0;
74__attribute__((constructor)) static void initme(void) {
75 verbose = getenv("STUB_VERBOSE") != NULL;
76}
77""")
78
79for funcname in functions:
80 header.write("void* %s(void);\n" % funcname)
81 source.write("void* %s(void) { if (verbose) puts(\"STUB: %s called\"); return NULL; }\n" % (funcname, funcname))
82