blob: 3c4810ad4bf0d5b82db25b4ef2bd7a4d20e19129 [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 = """
Elliott Hughes986f9062014-02-18 16:42:36 -080051 .globl %(alias)s
52 .equ %(alias)s, %(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>
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800124#include <machine/asm.h>
125#include <machine/regdef.h>
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700126 .text
127 .globl %(func)s
128 .align 4
129 .ent %(func)s
130
131%(func)s:
132 .set noreorder
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800133 .cpload t9
134 li v0, %(__NR_name)s
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700135 syscall
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800136 bnez a3, 1f
137 move a0, v0
138 j ra
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700139 nop
1401:
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800141 la t9,__set_errno
142 j t9
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700143 nop
144 .set reorder
145 .end %(func)s
146"""
147
148
Elliott Hughescd6780b2013-02-07 14:07:00 -0800149#
Chris Dearman50432122014-02-05 16:59:23 -0800150# MIPS64 assembler templates for each syscall stub
151#
152
153mips64_call = "/* " + warning + " */\n" + \
154"""
155#include <asm/unistd.h>
156#include <machine/asm.h>
157#include <machine/regdef.h>
158 .text
159 .globl %(func)s
160 .align 4
161 .ent %(func)s
162
163%(func)s:
164 .set push
165 .set noreorder
166 li v0, %(__NR_name)s
167 syscall
168 bnez a3, 1f
169 move a0, v0
170 j ra
171 nop
1721:
173 move t0, ra
174 bal 2f
175 nop
1762:
177 .cpsetup ra, t1, 2b
178 LA t9,__set_errno
179 .cpreturn
180 j t9
181 move ra, t0
182 .set pop
183 .end %(func)s
184"""
185
186
187#
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700188# x86 assembler templates for each syscall stub
189#
190
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800191x86_registers = [ "ebx", "ecx", "edx", "esi", "edi", "ebp" ]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700192
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700193x86_call = """\
194 movl $%(__NR_name)s, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700195 int $0x80
Elliott Hughes9aceab52013-03-12 14:57:30 -0700196 cmpl $-MAX_ERRNO, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700197 jb 1f
198 negl %%eax
199 pushl %%eax
200 call __set_errno
201 addl $4, %%esp
202 orl $-1, %%eax
2031:
204"""
205
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700206x86_return = """\
207 ret
208END(%(func)s)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700209"""
210
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700211
Elliott Hughescd6780b2013-02-07 14:07:00 -0800212#
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400213# x86_64 assembler templates for each syscall stub
214#
215
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700216x86_64_call = """\
217 movl $%(__NR_name)s, %%eax
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400218 syscall
219 cmpq $-MAX_ERRNO, %%rax
220 jb 1f
221 negl %%eax
222 movl %%eax, %%edi
223 call __set_errno
224 orq $-1, %%rax
2251:
226 ret
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700227END(%(func)s)
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400228"""
229
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800230
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100231def param_uses_64bits(param):
232 """Returns True iff a syscall parameter description corresponds
233 to a 64-bit type."""
234 param = param.strip()
235 # First, check that the param type begins with one of the known
236 # 64-bit types.
237 if not ( \
238 param.startswith("int64_t") or param.startswith("uint64_t") or \
239 param.startswith("loff_t") or param.startswith("off64_t") or \
240 param.startswith("long long") or param.startswith("unsigned long long") or
241 param.startswith("signed long long") ):
242 return False
243
244 # Second, check that there is no pointer type here
245 if param.find("*") >= 0:
246 return False
247
248 # Ok
249 return True
250
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700251
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100252def count_arm_param_registers(params):
253 """This function is used to count the number of register used
Elliott Hughescd6780b2013-02-07 14:07:00 -0800254 to pass parameters when invoking an ARM system call.
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100255 This is because the ARM EABI mandates that 64-bit quantities
256 must be passed in an even+odd register pair. So, for example,
257 something like:
258
259 foo(int fd, off64_t pos)
260
261 would actually need 4 registers:
262 r0 -> int
263 r1 -> unused
264 r2-r3 -> pos
265 """
266 count = 0
267 for param in params:
268 if param_uses_64bits(param):
269 if (count & 1) != 0:
270 count += 1
271 count += 2
272 else:
273 count += 1
274 return count
275
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700276
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100277def count_generic_param_registers(params):
278 count = 0
279 for param in params:
280 if param_uses_64bits(param):
281 count += 2
282 else:
283 count += 1
284 return count
285
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700286
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400287def count_generic_param_registers64(params):
288 count = 0
289 for param in params:
290 count += 1
291 return count
292
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700293
Elliott Hughescda62092013-03-22 13:50:44 -0700294# This lets us support regular system calls like __NR_write and also weird
295# ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
296def make__NR_name(name):
297 if name.startswith("__"):
298 return name
299 else:
300 return "__NR_%s" % (name)
301
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700302
Elliott Hughesfff6e272013-10-24 17:03:20 -0700303def add_footer(pointer_length, stub, syscall):
304 # Add any aliases for this syscall.
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700305 aliases = syscall["aliases"]
306 for alias in aliases:
307 stub += function_alias % { "func" : syscall["func"], "alias" : alias }
Elliott Hughesfff6e272013-10-24 17:03:20 -0700308
309 # Use hidden visibility for any functions beginning with underscores.
Elliott Hughesfff6e272013-10-24 17:03:20 -0700310 if pointer_length == 64 and syscall["func"].startswith("__"):
311 stub += '.hidden _C_LABEL(' + syscall["func"] + ')\n'
312
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700313 return stub
314
315
316def arm_eabi_genstub(syscall):
317 num_regs = count_arm_param_registers(syscall["params"])
318 if num_regs > 4:
319 return arm_eabi_call_long % syscall
320 return arm_eabi_call_default % syscall
321
322
Colin Crossd1973ca2014-01-21 19:50:58 -0800323def arm64_genstub(syscall):
324 return arm64_call % syscall
325
326
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700327def mips_genstub(syscall):
328 return mips_call % syscall
329
330
Chris Dearman50432122014-02-05 16:59:23 -0800331def mips64_genstub(syscall):
332 return mips64_call % syscall
333
334
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700335def x86_genstub(syscall):
336 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700337
338 numparams = count_generic_param_registers(syscall["params"])
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800339 stack_bias = numparams*4 + 4
340 offset = 0
341 mov_result = ""
342 cfi_result = " .cfi_def_cfa_offset %d\n" % (numparams*4)
343 for register in x86_registers[:numparams]:
344 result += " pushl %%%s\n" % register
345 mov_result += " mov %d(%%esp), %%%s\n" % (stack_bias+offset, register)
346 cfi_result += " .cfi_rel_offset %s, %d\n" % (register, offset)
347 offset += 4
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700348
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800349 if numparams:
350 result += cfi_result
351 result += mov_result
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700352
353 result += x86_call % syscall
354
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800355 for register in reversed(x86_registers[:numparams]):
356 result += " popl %%%s\n" % register
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700357
358 result += x86_return % syscall
359 return result
360
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100361
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700362def x86_genstub_socketcall(syscall):
363 # %ebx <--- Argument 1 - The call id of the needed vectored
364 # syscall (socket, bind, recv, etc)
365 # %ecx <--- Argument 2 - Pointer to the rest of the arguments
366 # from the original function called (socket())
367
368 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700369
370 # save the regs we need
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800371 result += " pushl %ebx\n"
372 result += " pushl %ecx\n"
373 result += " .cfi_def_cfa_offset 8\n"
374 result += " .cfi_rel_offset ebx, 0\n"
375 result += " .cfi_rel_offset ecx, 4\n"
376 stack_bias = 12
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700377
378 # set the call id (%ebx)
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800379 result += " mov $%d, %%ebx\n" % syscall["socketcall_id"]
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700380
381 # set the pointer to the rest of the args into %ecx
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800382 result += " mov %esp, %ecx\n"
383 result += " addl $%d, %%ecx\n" % (stack_bias)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700384
385 # now do the syscall code itself
386 result += x86_call % syscall
387
388 # now restore the saved regs
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800389 result += " popl %ecx\n"
390 result += " popl %ebx\n"
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700391
392 # epilog
393 result += x86_return % syscall
394 return result
395
396
397def x86_64_genstub(syscall):
398 result = syscall_stub_header % syscall
399 num_regs = count_generic_param_registers64(syscall["params"])
400 if (num_regs > 3):
401 # rcx is used as 4th argument. Kernel wants it at r10.
402 result += " movq %rcx, %r10\n"
403
404 result += x86_64_call % syscall
405 return result
406
407
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700408class State:
409 def __init__(self):
410 self.old_stubs = []
411 self.new_stubs = []
412 self.other_files = []
413 self.syscalls = []
414
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400415
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700416 def process_file(self, input):
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700417 parser = SysCallsTxtParser()
418 parser.parse_file(input)
419 self.syscalls = parser.syscalls
420 parser = None
421
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700422 for syscall in self.syscalls:
423 syscall["__NR_name"] = make__NR_name(syscall["name"])
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700424
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700425 if syscall.has_key("arm"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700426 syscall["asm-arm"] = add_footer(32, arm_eabi_genstub(syscall), syscall)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700427
Colin Crossd1973ca2014-01-21 19:50:58 -0800428 if syscall.has_key("arm64"):
429 syscall["asm-arm64"] = add_footer(64, arm64_genstub(syscall), syscall)
430
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700431 if syscall.has_key("x86"):
432 if syscall["socketcall_id"] >= 0:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700433 syscall["asm-x86"] = add_footer(32, x86_genstub_socketcall(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800434 else:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700435 syscall["asm-x86"] = add_footer(32, x86_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700436 elif syscall["socketcall_id"] >= 0:
Elliott Hughesd6121652013-09-25 22:43:36 -0700437 E("socketcall_id for dispatch syscalls is only supported for x86 in '%s'" % t)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800438 return
Elliott Hughescd6780b2013-02-07 14:07:00 -0800439
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700440 if syscall.has_key("mips"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700441 syscall["asm-mips"] = add_footer(32, mips_genstub(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800442
Chris Dearman50432122014-02-05 16:59:23 -0800443 if syscall.has_key("mips64"):
444 syscall["asm-mips64"] = add_footer(64, mips64_genstub(syscall), syscall)
445
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700446 if syscall.has_key("x86_64"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700447 syscall["asm-x86_64"] = add_footer(64, x86_64_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700448
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700449 # Scan a Linux kernel asm/unistd.h file containing __NR_* constants
450 # and write out equivalent SYS_* constants for glibc source compatibility.
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700451 def scan_linux_unistd_h(self, fp, path):
452 pattern = re.compile(r'^#define __NR_([a-z]\S+) .*')
453 syscalls = set() # MIPS defines everything three times; work around that.
454 for line in open(path):
455 m = re.search(pattern, line)
456 if m:
457 syscalls.add(m.group(1))
458 for syscall in sorted(syscalls):
Elliott Hughescda62092013-03-22 13:50:44 -0700459 fp.write("#define SYS_%s %s\n" % (syscall, make__NR_name(syscall)))
Elliott Hughes8ecf2252013-03-21 18:06:55 -0700460
461
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700462 def gen_glibc_syscalls_h(self):
Elliott Hughescda62092013-03-22 13:50:44 -0700463 # TODO: generate a separate file for each architecture, like glibc's bits/syscall.h.
Elliott Hughes9724ce32013-03-21 19:43:54 -0700464 glibc_syscalls_h_path = "include/sys/glibc-syscalls.h"
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700465 D("generating " + glibc_syscalls_h_path)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700466 glibc_fp = create_file(glibc_syscalls_h_path)
Elliott Hughes103ccde2013-10-16 14:27:59 -0700467 glibc_fp.write("/* %s */\n" % warning)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700468 glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n")
469 glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n")
470
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100471 glibc_fp.write("#if defined(__aarch64__)\n")
472 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-generic/unistd.h")
473 glibc_fp.write("#elif defined(__arm__)\n")
Elliott Hughes887e1142014-01-02 12:05:50 -0800474 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-arm/asm/unistd.h")
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700475 glibc_fp.write("#elif defined(__mips__)\n")
Christopher Ferrised459702013-12-02 17:44:53 -0800476 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-mips/asm/unistd.h")
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700477 glibc_fp.write("#elif defined(__i386__)\n")
Christopher Ferrised459702013-12-02 17:44:53 -0800478 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 +0400479 glibc_fp.write("#elif defined(__x86_64__)\n")
Christopher Ferrised459702013-12-02 17:44:53 -0800480 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 -0700481 glibc_fp.write("#endif\n")
482
483 glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n")
484 glibc_fp.close()
485 self.other_files.append(glibc_syscalls_h_path)
486
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700487
Elliott Hughesd6121652013-09-25 22:43:36 -0700488 # Write each syscall stub.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700489 def gen_syscall_stubs(self):
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700490 for syscall in self.syscalls:
Elliott Hughesd6121652013-09-25 22:43:36 -0700491 for arch in all_arches:
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700492 if syscall.has_key("asm-%s" % arch):
493 filename = "arch-%s/syscalls/%s.S" % (arch, syscall["func"])
Elliott Hughesd6121652013-09-25 22:43:36 -0700494 D2(">>> generating " + filename)
495 fp = create_file(filename)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700496 fp.write(syscall["asm-%s" % arch])
Elliott Hughesd6121652013-09-25 22:43:36 -0700497 fp.close()
498 self.new_stubs.append(filename)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700499
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700500
Elliott Hughesd6121652013-09-25 22:43:36 -0700501 def regenerate(self):
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400502 D("scanning for existing architecture-specific stub files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700503
Elliott Hughes18bc9752013-06-17 10:26:10 -0700504 bionic_libc_root_len = len(bionic_libc_root)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700505
Elliott Hughesd6121652013-09-25 22:43:36 -0700506 for arch in all_arches:
Elliott Hughes18bc9752013-06-17 10:26:10 -0700507 arch_path = bionic_libc_root + "arch-" + arch
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400508 D("scanning " + arch_path)
509 files = glob.glob(arch_path + "/syscalls/*.S")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700510 for f in files:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400511 self.old_stubs.append(f[bionic_libc_root_len:])
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700512
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400513 D("found %d stub files" % len(self.old_stubs))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700514
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400515 if not os.path.exists(bionic_temp):
516 D("creating %s..." % bionic_temp)
517 make_dir(bionic_temp)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700518
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400519 D("re-generating stubs and support files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700520
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700521 self.gen_glibc_syscalls_h()
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700522 self.gen_syscall_stubs()
523
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400524 D("comparing files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700525 adds = []
526 edits = []
527
528 for stub in self.new_stubs + self.other_files:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400529 if not os.path.exists(bionic_libc_root + stub):
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200530 # new file, git add it
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400531 D("new file: " + stub)
532 adds.append(bionic_libc_root + stub)
533 shutil.copyfile(bionic_temp + stub, bionic_libc_root + stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700534
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400535 elif not filecmp.cmp(bionic_temp + stub, bionic_libc_root + stub):
536 D("changed file: " + stub)
537 edits.append(stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700538
539 deletes = []
540 for stub in self.old_stubs:
541 if not stub in self.new_stubs:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400542 D("deleted file: " + stub)
543 deletes.append(bionic_libc_root + stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700544
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400545 if not DRY_RUN:
546 if adds:
547 commands.getoutput("git add " + " ".join(adds))
548 if deletes:
549 commands.getoutput("git rm " + " ".join(deletes))
550 if edits:
551 for file in edits:
552 shutil.copyfile(bionic_temp + file, bionic_libc_root + file)
553 commands.getoutput("git add " + " ".join((bionic_libc_root + file) for file in edits))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700554
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400555 commands.getoutput("git add %s%s" % (bionic_libc_root,"SYSCALLS.TXT"))
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200556
557 if (not adds) and (not deletes) and (not edits):
558 D("no changes detected!")
559 else:
560 D("ready to go!!")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700561
562D_setlevel(1)
563
564state = State()
Elliott Hughes18bc9752013-06-17 10:26:10 -0700565state.process_file(bionic_libc_root+"SYSCALLS.TXT")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700566state.regenerate()