blob: cea35084826d1bdbacf0ab583903db44f214b823 [file] [log] [blame]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07001#!/usr/bin/python
Pavel Chupinf12a18b2012-12-12 13:11:48 +04002
3# This tool is used to generate the assembler system call stubs,
4# the header files listing all available system calls, and the
5# makefiles used to build all the stubs.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -07006
Elliott Hughes103ccde2013-10-16 14:27:59 -07007import commands
8import filecmp
9import glob
10import os.path
11import re
12import shutil
13import stat
14import sys
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070015
16from bionic_utils import *
17
Elliott Hughes18bc9752013-06-17 10:26:10 -070018bionic_libc_root = os.environ["ANDROID_BUILD_TOP"] + "/bionic/libc/"
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070019
20# temp directory where we store all intermediate files
21bionic_temp = "/tmp/bionic_gensyscalls/"
22
Elliott Hughes103ccde2013-10-16 14:27:59 -070023warning = "Generated by gensyscalls.py. Do not edit."
24
Pavel Chupinf12a18b2012-12-12 13:11:48 +040025DRY_RUN = False
26
27def make_dir(path):
Raghu Gandham1fa0d842012-01-27 17:51:42 -080028 path = os.path.abspath(path)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070029 if not os.path.exists(path):
30 parent = os.path.dirname(path)
31 if parent:
32 make_dir(parent)
33 os.mkdir(path)
34
Elliott Hughes0437f3f2013-10-07 23:53:13 -070035
Pavel Chupinf12a18b2012-12-12 13:11:48 +040036def create_file(relpath):
37 dir = os.path.dirname(bionic_temp + relpath)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070038 make_dir(dir)
Pavel Chupinf12a18b2012-12-12 13:11:48 +040039 return open(bionic_temp + relpath, "w")
40
41
Elliott Hughes103ccde2013-10-16 14:27:59 -070042syscall_stub_header = "/* " + warning + " */\n" + \
43"""
Elliott Hughesed744842013-11-07 10:31:05 -080044#include <private/bionic_asm.h>
Pavel Chupinf12a18b2012-12-12 13:11:48 +040045
Elliott Hughes0437f3f2013-10-07 23:53:13 -070046ENTRY(%(func)s)
Pavel Chupinf12a18b2012-12-12 13:11:48 +040047"""
48
Elliott Hughes0437f3f2013-10-07 23:53:13 -070049
H.J. Lu6fe4e872013-10-04 10:03:17 -070050function_alias = """
51 .globl _C_LABEL(%(alias)s)
Elliott Hughes0437f3f2013-10-07 23:53:13 -070052 .equ _C_LABEL(%(alias)s), _C_LABEL(%(func)s)
H.J. Lu6fe4e872013-10-04 10:03:17 -070053"""
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -070054
Elliott Hughes0437f3f2013-10-07 23:53:13 -070055
56#
57# ARM assembler templates for each syscall stub
58#
59
60arm_eabi_call_default = syscall_stub_header + """\
61 mov ip, r7
62 ldr r7, =%(__NR_name)s
63 swi #0
64 mov r7, ip
65 cmn r0, #(MAX_ERRNO + 1)
66 bxls lr
67 neg r0, r0
68 b __set_errno
69END(%(func)s)
70"""
71
72arm_eabi_call_long = syscall_stub_header + """\
73 mov ip, sp
Elliott Hughes0437f3f2013-10-07 23:53:13 -070074 stmfd sp!, {r4, r5, r6, r7}
Christopher Ferrised459702013-12-02 17:44:53 -080075 .cfi_def_cfa_offset 16
76 .cfi_rel_offset r4, 0
77 .cfi_rel_offset r5, 4
78 .cfi_rel_offset r6, 8
79 .cfi_rel_offset r7, 12
Elliott Hughes0437f3f2013-10-07 23:53:13 -070080 ldmfd ip, {r4, r5, r6}
81 ldr r7, =%(__NR_name)s
82 swi #0
83 ldmfd sp!, {r4, r5, r6, r7}
Christopher Ferrised459702013-12-02 17:44:53 -080084 .cfi_def_cfa_offset 0
Elliott Hughes0437f3f2013-10-07 23:53:13 -070085 cmn r0, #(MAX_ERRNO + 1)
86 bxls lr
87 neg r0, r0
88 b __set_errno
89END(%(func)s)
90"""
91
92
93#
Colin Crossd1973ca2014-01-21 19:50:58 -080094# Arm64 assembler templates for each syscall stub
95#
96
97arm64_call = syscall_stub_header + """\
98 stp x29, x30, [sp, #-16]!
99 mov x29, sp
100 str x8, [sp, #-16]!
101
102 mov x8, %(__NR_name)s
103 svc #0
104
105 ldr x8, [sp], #16
106 ldp x29, x30, [sp], #16
107
108 cmn x0, #(MAX_ERRNO + 1)
109 cneg x0, x0, hi
110 b.hi __set_errno
111
112 ret
113END(%(func)s)
114"""
115
116
117#
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700118# MIPS assembler templates for each syscall stub
119#
120
Elliott Hughes103ccde2013-10-16 14:27:59 -0700121mips_call = "/* " + warning + " */\n" + \
122"""
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700123#include <asm/unistd.h>
124 .text
125 .globl %(func)s
126 .align 4
127 .ent %(func)s
128
129%(func)s:
130 .set noreorder
131 .cpload $t9
132 li $v0, %(__NR_name)s
133 syscall
134 bnez $a3, 1f
135 move $a0, $v0
136 j $ra
137 nop
1381:
139 la $t9,__set_errno
140 j $t9
141 nop
142 .set reorder
143 .end %(func)s
144"""
145
146
Elliott Hughescd6780b2013-02-07 14:07:00 -0800147#
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700148# x86 assembler templates for each syscall stub
149#
150
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800151x86_registers = [ "ebx", "ecx", "edx", "esi", "edi", "ebp" ]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700152
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700153x86_call = """\
154 movl $%(__NR_name)s, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700155 int $0x80
Elliott Hughes9aceab52013-03-12 14:57:30 -0700156 cmpl $-MAX_ERRNO, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700157 jb 1f
158 negl %%eax
159 pushl %%eax
160 call __set_errno
161 addl $4, %%esp
162 orl $-1, %%eax
1631:
164"""
165
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700166x86_return = """\
167 ret
168END(%(func)s)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700169"""
170
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700171
Elliott Hughescd6780b2013-02-07 14:07:00 -0800172#
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400173# x86_64 assembler templates for each syscall stub
174#
175
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700176x86_64_call = """\
177 movl $%(__NR_name)s, %%eax
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400178 syscall
179 cmpq $-MAX_ERRNO, %%rax
180 jb 1f
181 negl %%eax
182 movl %%eax, %%edi
183 call __set_errno
184 orq $-1, %%rax
1851:
186 ret
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700187END(%(func)s)
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400188"""
189
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800190
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100191def param_uses_64bits(param):
192 """Returns True iff a syscall parameter description corresponds
193 to a 64-bit type."""
194 param = param.strip()
195 # First, check that the param type begins with one of the known
196 # 64-bit types.
197 if not ( \
198 param.startswith("int64_t") or param.startswith("uint64_t") or \
199 param.startswith("loff_t") or param.startswith("off64_t") or \
200 param.startswith("long long") or param.startswith("unsigned long long") or
201 param.startswith("signed long long") ):
202 return False
203
204 # Second, check that there is no pointer type here
205 if param.find("*") >= 0:
206 return False
207
208 # Ok
209 return True
210
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700211
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100212def count_arm_param_registers(params):
213 """This function is used to count the number of register used
Elliott Hughescd6780b2013-02-07 14:07:00 -0800214 to pass parameters when invoking an ARM system call.
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100215 This is because the ARM EABI mandates that 64-bit quantities
216 must be passed in an even+odd register pair. So, for example,
217 something like:
218
219 foo(int fd, off64_t pos)
220
221 would actually need 4 registers:
222 r0 -> int
223 r1 -> unused
224 r2-r3 -> pos
225 """
226 count = 0
227 for param in params:
228 if param_uses_64bits(param):
229 if (count & 1) != 0:
230 count += 1
231 count += 2
232 else:
233 count += 1
234 return count
235
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700236
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100237def count_generic_param_registers(params):
238 count = 0
239 for param in params:
240 if param_uses_64bits(param):
241 count += 2
242 else:
243 count += 1
244 return count
245
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700246
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400247def count_generic_param_registers64(params):
248 count = 0
249 for param in params:
250 count += 1
251 return count
252
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700253
Elliott Hughescda62092013-03-22 13:50:44 -0700254# This lets us support regular system calls like __NR_write and also weird
255# ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
256def make__NR_name(name):
257 if name.startswith("__"):
258 return name
259 else:
260 return "__NR_%s" % (name)
261
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700262
Elliott Hughesfff6e272013-10-24 17:03:20 -0700263def add_footer(pointer_length, stub, syscall):
264 # Add any aliases for this syscall.
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700265 aliases = syscall["aliases"]
266 for alias in aliases:
267 stub += function_alias % { "func" : syscall["func"], "alias" : alias }
Elliott Hughesfff6e272013-10-24 17:03:20 -0700268
269 # Use hidden visibility for any functions beginning with underscores.
Elliott Hughesfff6e272013-10-24 17:03:20 -0700270 if pointer_length == 64 and syscall["func"].startswith("__"):
271 stub += '.hidden _C_LABEL(' + syscall["func"] + ')\n'
272
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700273 return stub
274
275
276def arm_eabi_genstub(syscall):
277 num_regs = count_arm_param_registers(syscall["params"])
278 if num_regs > 4:
279 return arm_eabi_call_long % syscall
280 return arm_eabi_call_default % syscall
281
282
Colin Crossd1973ca2014-01-21 19:50:58 -0800283def arm64_genstub(syscall):
284 return arm64_call % syscall
285
286
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700287def mips_genstub(syscall):
288 return mips_call % syscall
289
290
291def x86_genstub(syscall):
292 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700293
294 numparams = count_generic_param_registers(syscall["params"])
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800295 stack_bias = numparams*4 + 4
296 offset = 0
297 mov_result = ""
298 cfi_result = " .cfi_def_cfa_offset %d\n" % (numparams*4)
299 for register in x86_registers[:numparams]:
300 result += " pushl %%%s\n" % register
301 mov_result += " mov %d(%%esp), %%%s\n" % (stack_bias+offset, register)
302 cfi_result += " .cfi_rel_offset %s, %d\n" % (register, offset)
303 offset += 4
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700304
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800305 if numparams:
306 result += cfi_result
307 result += mov_result
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700308
309 result += x86_call % syscall
310
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800311 for register in reversed(x86_registers[:numparams]):
312 result += " popl %%%s\n" % register
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700313
314 result += x86_return % syscall
315 return result
316
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100317
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700318def x86_genstub_socketcall(syscall):
319 # %ebx <--- Argument 1 - The call id of the needed vectored
320 # syscall (socket, bind, recv, etc)
321 # %ecx <--- Argument 2 - Pointer to the rest of the arguments
322 # from the original function called (socket())
323
324 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700325
326 # save the regs we need
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800327 result += " pushl %ebx\n"
328 result += " pushl %ecx\n"
329 result += " .cfi_def_cfa_offset 8\n"
330 result += " .cfi_rel_offset ebx, 0\n"
331 result += " .cfi_rel_offset ecx, 4\n"
332 stack_bias = 12
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700333
334 # set the call id (%ebx)
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800335 result += " mov $%d, %%ebx\n" % syscall["socketcall_id"]
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700336
337 # set the pointer to the rest of the args into %ecx
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800338 result += " mov %esp, %ecx\n"
339 result += " addl $%d, %%ecx\n" % (stack_bias)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700340
341 # now do the syscall code itself
342 result += x86_call % syscall
343
344 # now restore the saved regs
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800345 result += " popl %ecx\n"
346 result += " popl %ebx\n"
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700347
348 # epilog
349 result += x86_return % syscall
350 return result
351
352
353def x86_64_genstub(syscall):
354 result = syscall_stub_header % syscall
355 num_regs = count_generic_param_registers64(syscall["params"])
356 if (num_regs > 3):
357 # rcx is used as 4th argument. Kernel wants it at r10.
358 result += " movq %rcx, %r10\n"
359
360 result += x86_64_call % syscall
361 return result
362
363
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700364class State:
365 def __init__(self):
366 self.old_stubs = []
367 self.new_stubs = []
368 self.other_files = []
369 self.syscalls = []
370
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400371
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700372 def process_file(self, input):
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700373 parser = SysCallsTxtParser()
374 parser.parse_file(input)
375 self.syscalls = parser.syscalls
376 parser = None
377
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700378 for syscall in self.syscalls:
379 syscall["__NR_name"] = make__NR_name(syscall["name"])
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700380
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700381 if syscall.has_key("arm"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700382 syscall["asm-arm"] = add_footer(32, arm_eabi_genstub(syscall), syscall)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700383
Colin Crossd1973ca2014-01-21 19:50:58 -0800384 if syscall.has_key("arm64"):
385 syscall["asm-arm64"] = add_footer(64, arm64_genstub(syscall), syscall)
386
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700387 if syscall.has_key("x86"):
388 if syscall["socketcall_id"] >= 0:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700389 syscall["asm-x86"] = add_footer(32, x86_genstub_socketcall(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800390 else:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700391 syscall["asm-x86"] = add_footer(32, x86_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700392 elif syscall["socketcall_id"] >= 0:
Elliott Hughesd6121652013-09-25 22:43:36 -0700393 E("socketcall_id for dispatch syscalls is only supported for x86 in '%s'" % t)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800394 return
Elliott Hughescd6780b2013-02-07 14:07:00 -0800395
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700396 if syscall.has_key("mips"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700397 syscall["asm-mips"] = add_footer(32, mips_genstub(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800398
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700399 if syscall.has_key("x86_64"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700400 syscall["asm-x86_64"] = add_footer(64, x86_64_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700401
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700402 # Scan a Linux kernel asm/unistd.h file containing __NR_* constants
403 # and write out equivalent SYS_* constants for glibc source compatibility.
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700404 def scan_linux_unistd_h(self, fp, path):
405 pattern = re.compile(r'^#define __NR_([a-z]\S+) .*')
406 syscalls = set() # MIPS defines everything three times; work around that.
407 for line in open(path):
408 m = re.search(pattern, line)
409 if m:
410 syscalls.add(m.group(1))
411 for syscall in sorted(syscalls):
Elliott Hughescda62092013-03-22 13:50:44 -0700412 fp.write("#define SYS_%s %s\n" % (syscall, make__NR_name(syscall)))
Elliott Hughes8ecf2252013-03-21 18:06:55 -0700413
414
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700415 def gen_glibc_syscalls_h(self):
Elliott Hughescda62092013-03-22 13:50:44 -0700416 # TODO: generate a separate file for each architecture, like glibc's bits/syscall.h.
Elliott Hughes9724ce32013-03-21 19:43:54 -0700417 glibc_syscalls_h_path = "include/sys/glibc-syscalls.h"
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700418 D("generating " + glibc_syscalls_h_path)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700419 glibc_fp = create_file(glibc_syscalls_h_path)
Elliott Hughes103ccde2013-10-16 14:27:59 -0700420 glibc_fp.write("/* %s */\n" % warning)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700421 glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n")
422 glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n")
423
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100424 glibc_fp.write("#if defined(__aarch64__)\n")
425 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-generic/unistd.h")
426 glibc_fp.write("#elif defined(__arm__)\n")
Elliott Hughes887e1142014-01-02 12:05:50 -0800427 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-arm/asm/unistd.h")
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700428 glibc_fp.write("#elif defined(__mips__)\n")
Christopher Ferrised459702013-12-02 17:44:53 -0800429 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-mips/asm/unistd.h")
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700430 glibc_fp.write("#elif defined(__i386__)\n")
Christopher Ferrised459702013-12-02 17:44:53 -0800431 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-x86/asm/unistd_32.h")
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400432 glibc_fp.write("#elif defined(__x86_64__)\n")
Christopher Ferrised459702013-12-02 17:44:53 -0800433 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-x86/asm/unistd_64.h")
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700434 glibc_fp.write("#endif\n")
435
436 glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n")
437 glibc_fp.close()
438 self.other_files.append(glibc_syscalls_h_path)
439
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700440
Elliott Hughesd6121652013-09-25 22:43:36 -0700441 # Write the contents of syscalls.mk.
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800442 def gen_arch_syscalls_mk(self, arch):
443 path = "arch-%s/syscalls.mk" % arch
Elliott Hughesd6121652013-09-25 22:43:36 -0700444 D("generating " + path)
445 fp = create_file(path)
Elliott Hughes103ccde2013-10-16 14:27:59 -0700446 fp.write("# %s\n" % warning)
Elliott Hughesd6121652013-09-25 22:43:36 -0700447 fp.write("syscall_src :=\n")
Elliott Hughes103ccde2013-10-16 14:27:59 -0700448 for syscall in sorted(self.syscalls, key=lambda syscall: syscall["func"]):
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700449 if syscall.has_key("asm-%s" % arch):
450 fp.write("syscall_src += arch-%s/syscalls/%s.S\n" % (arch, syscall["func"]))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700451 fp.close()
Elliott Hughesd6121652013-09-25 22:43:36 -0700452 self.other_files.append(path)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700453
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800454
Elliott Hughesd6121652013-09-25 22:43:36 -0700455 # Write each syscall stub.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700456 def gen_syscall_stubs(self):
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700457 for syscall in self.syscalls:
Elliott Hughesd6121652013-09-25 22:43:36 -0700458 for arch in all_arches:
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700459 if syscall.has_key("asm-%s" % arch):
460 filename = "arch-%s/syscalls/%s.S" % (arch, syscall["func"])
Elliott Hughesd6121652013-09-25 22:43:36 -0700461 D2(">>> generating " + filename)
462 fp = create_file(filename)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700463 fp.write(syscall["asm-%s" % arch])
Elliott Hughesd6121652013-09-25 22:43:36 -0700464 fp.close()
465 self.new_stubs.append(filename)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700466
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700467
Elliott Hughesd6121652013-09-25 22:43:36 -0700468 def regenerate(self):
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400469 D("scanning for existing architecture-specific stub files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700470
Elliott Hughes18bc9752013-06-17 10:26:10 -0700471 bionic_libc_root_len = len(bionic_libc_root)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700472
Elliott Hughesd6121652013-09-25 22:43:36 -0700473 for arch in all_arches:
Elliott Hughes18bc9752013-06-17 10:26:10 -0700474 arch_path = bionic_libc_root + "arch-" + arch
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400475 D("scanning " + arch_path)
476 files = glob.glob(arch_path + "/syscalls/*.S")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700477 for f in files:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400478 self.old_stubs.append(f[bionic_libc_root_len:])
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700479
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400480 D("found %d stub files" % len(self.old_stubs))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700481
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400482 if not os.path.exists(bionic_temp):
483 D("creating %s..." % bionic_temp)
484 make_dir(bionic_temp)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700485
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400486 D("re-generating stubs and support files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700487
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700488 self.gen_glibc_syscalls_h()
Elliott Hughesd6121652013-09-25 22:43:36 -0700489 for arch in all_arches:
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800490 self.gen_arch_syscalls_mk(arch)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700491 self.gen_syscall_stubs()
492
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400493 D("comparing files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700494 adds = []
495 edits = []
496
497 for stub in self.new_stubs + self.other_files:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400498 if not os.path.exists(bionic_libc_root + stub):
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200499 # new file, git add it
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400500 D("new file: " + stub)
501 adds.append(bionic_libc_root + stub)
502 shutil.copyfile(bionic_temp + stub, bionic_libc_root + stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700503
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400504 elif not filecmp.cmp(bionic_temp + stub, bionic_libc_root + stub):
505 D("changed file: " + stub)
506 edits.append(stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700507
508 deletes = []
509 for stub in self.old_stubs:
510 if not stub in self.new_stubs:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400511 D("deleted file: " + stub)
512 deletes.append(bionic_libc_root + stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700513
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400514 if not DRY_RUN:
515 if adds:
516 commands.getoutput("git add " + " ".join(adds))
517 if deletes:
518 commands.getoutput("git rm " + " ".join(deletes))
519 if edits:
520 for file in edits:
521 shutil.copyfile(bionic_temp + file, bionic_libc_root + file)
522 commands.getoutput("git add " + " ".join((bionic_libc_root + file) for file in edits))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700523
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400524 commands.getoutput("git add %s%s" % (bionic_libc_root,"SYSCALLS.TXT"))
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200525
526 if (not adds) and (not deletes) and (not edits):
527 D("no changes detected!")
528 else:
529 D("ready to go!!")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700530
531D_setlevel(1)
532
533state = State()
Elliott Hughes18bc9752013-06-17 10:26:10 -0700534state.process_file(bionic_libc_root+"SYSCALLS.TXT")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700535state.regenerate()