···9696 char *nl, *interp, *arg;
9797 char** modargvp;
9898 int i, j;
9999-9999+100100 nl = memchr(m.magic_array, '\n', sizeof(m.magic_array));
101101 if (nl == NULL)
102102 return -ENOEXEC;
103103-103103+104104 *nl = '\0';
105105-105105+106106 for (i = 2; isspace(m.magic_array[i]); i++);
107107-107107+108108 interp = &m.magic_array[i];
109109-109109+110110 for (i = 0; !isspace(interp[i]) && interp[i]; i++);
111111-111111+112112 if (interp[i] == '\0')
113113 arg = NULL;
114114 else
115115 arg = &interp[i];
116116-116116+117117 if (arg != NULL)
118118 {
119119 *arg = '\0'; // terminate interp
···124124 if (*arg == '\0')
125125 arg = NULL; // no argument, just whitespace
126126 }
127127-127127+128128 // Count original arguments
129129 while (argvp[len++]);
130130-130130+131131 // Allocate a new argvp
132132 modargvp = (char**) __builtin_alloca(sizeof(void*) * (len+3));
133133-133133+134134 i = 0;
135135136136 modargvp[i++] = mldr_path;
···138138 if (arg != NULL)
139139 modargvp[i++] = arg;
140140 modargvp[i] = fname;
141141-141141+142142 // Append original arguments
143143 for (j = 1; j < len+1; j++)
144144 modargvp[i+j] = argvp[j];
145145-145145+146146 argvp = modargvp;
147147 fname = mldr_path;
148148 }
149149- else
150150- {
151151- return -ENOEXEC;
152152- }
149149+150150+ // otherwise, it's a native Linux executable (ELF)
153151154152 ret = LINUX_SYSCALL(__NR_execve, fname, argvp, envp);
155153 if (ret < 0)
+76
tools/stub-gen-c-func.py
···11+# This file is part of Darling.
22+#
33+# Copyright (C) 2017 Lubos Dolezel
44+#
55+# Darling is free software: you can redistribute it and/or modify
66+# it under the terms of the GNU General Public License as published by
77+# the Free Software Foundation, either version 3 of the License, or
88+# (at your option) any later version.
99+#
1010+# Darling is distributed in the hope that it will be useful,
1111+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1212+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313+# GNU General Public License for more details.
1414+#
1515+# You should have received a copy of the GNU General Public License
1616+# along with Darling. If not, see <http://www.gnu.org/licenses/>.
1717+1818+import sys, subprocess
1919+2020+def usage():
2121+ print "Usage: %s <Mach-O> <output directory>" % sys.argv[0]
2222+2323+if len(sys.argv) != 3:
2424+ usage()
2525+2626+macho = sys.argv[1]
2727+dest = sys.argv[2]
2828+2929+out = subprocess.check_output(["nm", "-Ug", macho])
3030+3131+functions = []
3232+for line in out.split("\n"):
3333+ if line == "":
3434+ continue
3535+ components = line.split(" ")
3636+ id = components[1]
3737+ name = components[2]
3838+ # Remove the underscore
3939+ name = name[1 : len(name)]
4040+4141+ if id == "T":
4242+ functions.append(name)
4343+4444+header = open(dest + "/functions.h", "w")
4545+source = open(dest + "/functions.c", "w")
4646+4747+copyright ="""/*
4848+This file is part of Darling.
4949+5050+Copyright (C) 2017 Lubos Dolezel
5151+5252+Darling is free software: you can redistribute it and/or modify
5353+it under the terms of the GNU General Public License as published by
5454+the Free Software Foundation, either version 3 of the License, or
5555+(at your option) any later version.
5656+5757+Darling is distributed in the hope that it will be useful,
5858+but WITHOUT ANY WARRANTY; without even the implied warranty of
5959+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6060+GNU General Public License for more details.
6161+6262+You should have received a copy of the GNU General Public License
6363+along with Darling. If not, see <http://www.gnu.org/licenses/>.
6464+*/
6565+6666+"""
6767+6868+header.write(copyright)
6969+source.write(copyright)
7070+7171+for funcname in functions:
7272+ header.write("void %s(void);\n" % funcname)
7373+ source.write("void %s(void) { }\n" % funcname)
7474+7575+header.close()
7676+source.close()