blob: 8a5f3d2e067415f5f99e5ee0e4cefa7803f3506e [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 Hughes9abbbdc2014-02-19 14:54:31 -0800121mips_call = syscall_stub_header + """\
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700122 .set noreorder
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800123 .cpload t9
124 li v0, %(__NR_name)s
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700125 syscall
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800126 bnez a3, 1f
127 move a0, v0
128 j ra
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700129 nop
1301:
Elliott Hugheseae27dc2014-02-19 12:20:00 -0800131 la t9,__set_errno
132 j t9
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700133 nop
134 .set reorder
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800135END(%(func)s)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700136"""
137
138
Elliott Hughescd6780b2013-02-07 14:07:00 -0800139#
Chris Dearman50432122014-02-05 16:59:23 -0800140# MIPS64 assembler templates for each syscall stub
141#
142
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800143mips64_call = syscall_stub_header + """\
Chris Dearman50432122014-02-05 16:59:23 -0800144 .set push
145 .set noreorder
146 li v0, %(__NR_name)s
147 syscall
148 bnez a3, 1f
149 move a0, v0
150 j ra
151 nop
1521:
153 move t0, ra
154 bal 2f
155 nop
1562:
157 .cpsetup ra, t1, 2b
158 LA t9,__set_errno
159 .cpreturn
160 j t9
161 move ra, t0
162 .set pop
Elliott Hughes9abbbdc2014-02-19 14:54:31 -0800163END(%(func)s)
Chris Dearman50432122014-02-05 16:59:23 -0800164"""
165
166
167#
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700168# x86 assembler templates for each syscall stub
169#
170
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800171x86_registers = [ "ebx", "ecx", "edx", "esi", "edi", "ebp" ]
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700172
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700173x86_call = """\
174 movl $%(__NR_name)s, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700175 int $0x80
Elliott Hughes9aceab52013-03-12 14:57:30 -0700176 cmpl $-MAX_ERRNO, %%eax
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700177 jb 1f
178 negl %%eax
179 pushl %%eax
180 call __set_errno
181 addl $4, %%esp
182 orl $-1, %%eax
1831:
184"""
185
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700186x86_return = """\
187 ret
188END(%(func)s)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700189"""
190
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700191
Elliott Hughescd6780b2013-02-07 14:07:00 -0800192#
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400193# x86_64 assembler templates for each syscall stub
194#
195
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700196x86_64_call = """\
197 movl $%(__NR_name)s, %%eax
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400198 syscall
199 cmpq $-MAX_ERRNO, %%rax
200 jb 1f
201 negl %%eax
202 movl %%eax, %%edi
203 call __set_errno
204 orq $-1, %%rax
2051:
206 ret
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700207END(%(func)s)
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400208"""
209
Raghu Gandham1fa0d842012-01-27 17:51:42 -0800210
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100211def param_uses_64bits(param):
212 """Returns True iff a syscall parameter description corresponds
213 to a 64-bit type."""
214 param = param.strip()
215 # First, check that the param type begins with one of the known
216 # 64-bit types.
217 if not ( \
218 param.startswith("int64_t") or param.startswith("uint64_t") or \
219 param.startswith("loff_t") or param.startswith("off64_t") or \
220 param.startswith("long long") or param.startswith("unsigned long long") or
221 param.startswith("signed long long") ):
222 return False
223
224 # Second, check that there is no pointer type here
225 if param.find("*") >= 0:
226 return False
227
228 # Ok
229 return True
230
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700231
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100232def count_arm_param_registers(params):
233 """This function is used to count the number of register used
Elliott Hughescd6780b2013-02-07 14:07:00 -0800234 to pass parameters when invoking an ARM system call.
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100235 This is because the ARM EABI mandates that 64-bit quantities
236 must be passed in an even+odd register pair. So, for example,
237 something like:
238
239 foo(int fd, off64_t pos)
240
241 would actually need 4 registers:
242 r0 -> int
243 r1 -> unused
244 r2-r3 -> pos
245 """
246 count = 0
247 for param in params:
248 if param_uses_64bits(param):
249 if (count & 1) != 0:
250 count += 1
251 count += 2
252 else:
253 count += 1
254 return count
255
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700256
David 'Digit' Turner95d751f2010-12-16 16:47:14 +0100257def count_generic_param_registers(params):
258 count = 0
259 for param in params:
260 if param_uses_64bits(param):
261 count += 2
262 else:
263 count += 1
264 return count
265
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700266
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400267def count_generic_param_registers64(params):
268 count = 0
269 for param in params:
270 count += 1
271 return count
272
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700273
Elliott Hughescda62092013-03-22 13:50:44 -0700274# This lets us support regular system calls like __NR_write and also weird
275# ones like __ARM_NR_cacheflush, where the NR doesn't come at the start.
276def make__NR_name(name):
277 if name.startswith("__"):
278 return name
279 else:
280 return "__NR_%s" % (name)
281
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700282
Elliott Hughesfff6e272013-10-24 17:03:20 -0700283def add_footer(pointer_length, stub, syscall):
284 # Add any aliases for this syscall.
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700285 aliases = syscall["aliases"]
286 for alias in aliases:
287 stub += function_alias % { "func" : syscall["func"], "alias" : alias }
Elliott Hughesfff6e272013-10-24 17:03:20 -0700288
289 # Use hidden visibility for any functions beginning with underscores.
Elliott Hughesfff6e272013-10-24 17:03:20 -0700290 if pointer_length == 64 and syscall["func"].startswith("__"):
Elliott Hughesd465eb42014-02-19 18:59:19 -0800291 stub += '.hidden ' + syscall["func"] + '\n'
Elliott Hughesfff6e272013-10-24 17:03:20 -0700292
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700293 return stub
294
295
296def arm_eabi_genstub(syscall):
297 num_regs = count_arm_param_registers(syscall["params"])
298 if num_regs > 4:
299 return arm_eabi_call_long % syscall
300 return arm_eabi_call_default % syscall
301
302
Colin Crossd1973ca2014-01-21 19:50:58 -0800303def arm64_genstub(syscall):
304 return arm64_call % syscall
305
306
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700307def mips_genstub(syscall):
308 return mips_call % syscall
309
310
Chris Dearman50432122014-02-05 16:59:23 -0800311def mips64_genstub(syscall):
312 return mips64_call % syscall
313
314
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700315def x86_genstub(syscall):
316 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700317
318 numparams = count_generic_param_registers(syscall["params"])
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800319 stack_bias = numparams*4 + 4
320 offset = 0
321 mov_result = ""
322 cfi_result = " .cfi_def_cfa_offset %d\n" % (numparams*4)
323 for register in x86_registers[:numparams]:
324 result += " pushl %%%s\n" % register
325 mov_result += " mov %d(%%esp), %%%s\n" % (stack_bias+offset, register)
326 cfi_result += " .cfi_rel_offset %s, %d\n" % (register, offset)
327 offset += 4
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700328
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800329 if numparams:
330 result += cfi_result
331 result += mov_result
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700332
333 result += x86_call % syscall
334
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800335 for register in reversed(x86_registers[:numparams]):
336 result += " popl %%%s\n" % register
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700337
338 result += x86_return % syscall
339 return result
340
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100341
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700342def x86_genstub_socketcall(syscall):
343 # %ebx <--- Argument 1 - The call id of the needed vectored
344 # syscall (socket, bind, recv, etc)
345 # %ecx <--- Argument 2 - Pointer to the rest of the arguments
346 # from the original function called (socket())
347
348 result = syscall_stub_header % syscall
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700349
350 # save the regs we need
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800351 result += " pushl %ebx\n"
352 result += " pushl %ecx\n"
353 result += " .cfi_def_cfa_offset 8\n"
354 result += " .cfi_rel_offset ebx, 0\n"
355 result += " .cfi_rel_offset ecx, 4\n"
356 stack_bias = 12
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700357
358 # set the call id (%ebx)
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800359 result += " mov $%d, %%ebx\n" % syscall["socketcall_id"]
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700360
361 # set the pointer to the rest of the args into %ecx
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800362 result += " mov %esp, %ecx\n"
363 result += " addl $%d, %%ecx\n" % (stack_bias)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700364
365 # now do the syscall code itself
366 result += x86_call % syscall
367
368 # now restore the saved regs
Christopher Ferrise4bc7562014-01-06 16:39:10 -0800369 result += " popl %ecx\n"
370 result += " popl %ebx\n"
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700371
372 # epilog
373 result += x86_return % syscall
374 return result
375
376
377def x86_64_genstub(syscall):
378 result = syscall_stub_header % syscall
379 num_regs = count_generic_param_registers64(syscall["params"])
380 if (num_regs > 3):
381 # rcx is used as 4th argument. Kernel wants it at r10.
382 result += " movq %rcx, %r10\n"
383
384 result += x86_64_call % syscall
385 return result
386
387
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700388class State:
389 def __init__(self):
390 self.old_stubs = []
391 self.new_stubs = []
392 self.other_files = []
393 self.syscalls = []
394
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400395
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700396 def process_file(self, input):
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700397 parser = SysCallsTxtParser()
398 parser.parse_file(input)
399 self.syscalls = parser.syscalls
400 parser = None
401
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700402 for syscall in self.syscalls:
403 syscall["__NR_name"] = make__NR_name(syscall["name"])
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700404
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700405 if syscall.has_key("arm"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700406 syscall["asm-arm"] = add_footer(32, arm_eabi_genstub(syscall), syscall)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700407
Colin Crossd1973ca2014-01-21 19:50:58 -0800408 if syscall.has_key("arm64"):
409 syscall["asm-arm64"] = add_footer(64, arm64_genstub(syscall), syscall)
410
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700411 if syscall.has_key("x86"):
412 if syscall["socketcall_id"] >= 0:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700413 syscall["asm-x86"] = add_footer(32, x86_genstub_socketcall(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800414 else:
Elliott Hughesfff6e272013-10-24 17:03:20 -0700415 syscall["asm-x86"] = add_footer(32, x86_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700416 elif syscall["socketcall_id"] >= 0:
Elliott Hughesd6121652013-09-25 22:43:36 -0700417 E("socketcall_id for dispatch syscalls is only supported for x86 in '%s'" % t)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800418 return
Elliott Hughescd6780b2013-02-07 14:07:00 -0800419
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700420 if syscall.has_key("mips"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700421 syscall["asm-mips"] = add_footer(32, mips_genstub(syscall), syscall)
The Android Open Source Project4e468ed2008-12-17 18:03:48 -0800422
Chris Dearman50432122014-02-05 16:59:23 -0800423 if syscall.has_key("mips64"):
424 syscall["asm-mips64"] = add_footer(64, mips64_genstub(syscall), syscall)
425
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700426 if syscall.has_key("x86_64"):
Elliott Hughesfff6e272013-10-24 17:03:20 -0700427 syscall["asm-x86_64"] = add_footer(64, x86_64_genstub(syscall), syscall)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700428
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700429 # Scan a Linux kernel asm/unistd.h file containing __NR_* constants
430 # and write out equivalent SYS_* constants for glibc source compatibility.
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700431 def scan_linux_unistd_h(self, fp, path):
432 pattern = re.compile(r'^#define __NR_([a-z]\S+) .*')
433 syscalls = set() # MIPS defines everything three times; work around that.
434 for line in open(path):
435 m = re.search(pattern, line)
436 if m:
437 syscalls.add(m.group(1))
438 for syscall in sorted(syscalls):
Elliott Hughescda62092013-03-22 13:50:44 -0700439 fp.write("#define SYS_%s %s\n" % (syscall, make__NR_name(syscall)))
Elliott Hughes8ecf2252013-03-21 18:06:55 -0700440
441
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700442 def gen_glibc_syscalls_h(self):
Elliott Hughescda62092013-03-22 13:50:44 -0700443 # TODO: generate a separate file for each architecture, like glibc's bits/syscall.h.
Elliott Hughes9724ce32013-03-21 19:43:54 -0700444 glibc_syscalls_h_path = "include/sys/glibc-syscalls.h"
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700445 D("generating " + glibc_syscalls_h_path)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700446 glibc_fp = create_file(glibc_syscalls_h_path)
Elliott Hughes103ccde2013-10-16 14:27:59 -0700447 glibc_fp.write("/* %s */\n" % warning)
Elliott Hughes9724ce32013-03-21 19:43:54 -0700448 glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n")
449 glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n")
450
Serban Constantinescufeaa89a2013-10-07 16:49:09 +0100451 glibc_fp.write("#if defined(__aarch64__)\n")
452 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-generic/unistd.h")
453 glibc_fp.write("#elif defined(__arm__)\n")
Elliott Hughes887e1142014-01-02 12:05:50 -0800454 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-arm/asm/unistd.h")
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700455 glibc_fp.write("#elif defined(__mips__)\n")
Christopher Ferrised459702013-12-02 17:44:53 -0800456 self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/uapi/asm-mips/asm/unistd.h")
Elliott Hughes5c2772f2013-03-21 22:15:06 -0700457 glibc_fp.write("#elif defined(__i386__)\n")
Christopher Ferrised459702013-12-02 17:44:53 -0800458 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 +0400459 glibc_fp.write("#elif defined(__x86_64__)\n")
Christopher Ferrised459702013-12-02 17:44:53 -0800460 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 -0700461 glibc_fp.write("#endif\n")
462
463 glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n")
464 glibc_fp.close()
465 self.other_files.append(glibc_syscalls_h_path)
466
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700467
Elliott Hughesd6121652013-09-25 22:43:36 -0700468 # Write each syscall stub.
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700469 def gen_syscall_stubs(self):
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700470 for syscall in self.syscalls:
Elliott Hughesd6121652013-09-25 22:43:36 -0700471 for arch in all_arches:
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700472 if syscall.has_key("asm-%s" % arch):
473 filename = "arch-%s/syscalls/%s.S" % (arch, syscall["func"])
Elliott Hughesd6121652013-09-25 22:43:36 -0700474 D2(">>> generating " + filename)
475 fp = create_file(filename)
Elliott Hughes0437f3f2013-10-07 23:53:13 -0700476 fp.write(syscall["asm-%s" % arch])
Elliott Hughesd6121652013-09-25 22:43:36 -0700477 fp.close()
478 self.new_stubs.append(filename)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700479
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700480
Elliott Hughesd6121652013-09-25 22:43:36 -0700481 def regenerate(self):
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400482 D("scanning for existing architecture-specific stub files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700483
Elliott Hughes18bc9752013-06-17 10:26:10 -0700484 bionic_libc_root_len = len(bionic_libc_root)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700485
Elliott Hughesd6121652013-09-25 22:43:36 -0700486 for arch in all_arches:
Elliott Hughes18bc9752013-06-17 10:26:10 -0700487 arch_path = bionic_libc_root + "arch-" + arch
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400488 D("scanning " + arch_path)
489 files = glob.glob(arch_path + "/syscalls/*.S")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700490 for f in files:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400491 self.old_stubs.append(f[bionic_libc_root_len:])
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700492
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400493 D("found %d stub files" % len(self.old_stubs))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700494
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400495 if not os.path.exists(bionic_temp):
496 D("creating %s..." % bionic_temp)
497 make_dir(bionic_temp)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700498
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400499 D("re-generating stubs and support files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700500
Elliott Hughes1b91c6c2013-03-22 18:56:24 -0700501 self.gen_glibc_syscalls_h()
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700502 self.gen_syscall_stubs()
503
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400504 D("comparing files...")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700505 adds = []
506 edits = []
507
508 for stub in self.new_stubs + self.other_files:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400509 if not os.path.exists(bionic_libc_root + stub):
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200510 # new file, git add it
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400511 D("new file: " + stub)
512 adds.append(bionic_libc_root + stub)
513 shutil.copyfile(bionic_temp + stub, bionic_libc_root + stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700514
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400515 elif not filecmp.cmp(bionic_temp + stub, bionic_libc_root + stub):
516 D("changed file: " + stub)
517 edits.append(stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700518
519 deletes = []
520 for stub in self.old_stubs:
521 if not stub in self.new_stubs:
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400522 D("deleted file: " + stub)
523 deletes.append(bionic_libc_root + stub)
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700524
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400525 if not DRY_RUN:
526 if adds:
527 commands.getoutput("git add " + " ".join(adds))
528 if deletes:
529 commands.getoutput("git rm " + " ".join(deletes))
530 if edits:
531 for file in edits:
532 shutil.copyfile(bionic_temp + file, bionic_libc_root + file)
533 commands.getoutput("git add " + " ".join((bionic_libc_root + file) for file in edits))
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700534
Pavel Chupinf12a18b2012-12-12 13:11:48 +0400535 commands.getoutput("git add %s%s" % (bionic_libc_root,"SYSCALLS.TXT"))
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200536
537 if (not adds) and (not deletes) and (not edits):
538 D("no changes detected!")
539 else:
540 D("ready to go!!")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700541
542D_setlevel(1)
543
544state = State()
Elliott Hughes18bc9752013-06-17 10:26:10 -0700545state.process_file(bionic_libc_root+"SYSCALLS.TXT")
The Android Open Source Projecta27d2ba2008-10-21 07:00:00 -0700546state.regenerate()