The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 2 | |
| 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 Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 6 | |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 7 | import atexit |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 8 | import commands |
| 9 | import filecmp |
| 10 | import glob |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 11 | import logging |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 12 | import os.path |
| 13 | import re |
| 14 | import shutil |
| 15 | import stat |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 16 | import string |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 17 | import sys |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 18 | import tempfile |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 20 | |
| 21 | all_arches = [ "arm", "arm64", "mips", "mips64", "x86", "x86_64" ] |
| 22 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | # temp directory where we store all intermediate files |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 25 | bionic_temp = tempfile.mkdtemp(prefix="bionic_gensyscalls"); |
| 26 | # Make sure the directory is deleted when the script exits. |
| 27 | atexit.register(shutil.rmtree, bionic_temp) |
| 28 | |
| 29 | bionic_libc_root = os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 31 | warning = "Generated by gensyscalls.py. Do not edit." |
| 32 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 33 | DRY_RUN = False |
| 34 | |
| 35 | def make_dir(path): |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 36 | path = os.path.abspath(path) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 | if not os.path.exists(path): |
| 38 | parent = os.path.dirname(path) |
| 39 | if parent: |
| 40 | make_dir(parent) |
| 41 | os.mkdir(path) |
| 42 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 43 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 44 | def create_file(relpath): |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 45 | full_path = os.path.join(bionic_temp, relpath) |
| 46 | dir = os.path.dirname(full_path) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | make_dir(dir) |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 48 | return open(full_path, "w") |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 49 | |
| 50 | |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 51 | syscall_stub_header = "/* " + warning + " */\n" + \ |
| 52 | """ |
Elliott Hughes | ed74484 | 2013-11-07 10:31:05 -0800 | [diff] [blame] | 53 | #include <private/bionic_asm.h> |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 54 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 55 | ENTRY(%(func)s) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 56 | """ |
| 57 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 58 | |
H.J. Lu | 6fe4e87 | 2013-10-04 10:03:17 -0700 | [diff] [blame] | 59 | function_alias = """ |
Elliott Hughes | 986f906 | 2014-02-18 16:42:36 -0800 | [diff] [blame] | 60 | .globl %(alias)s |
| 61 | .equ %(alias)s, %(func)s |
H.J. Lu | 6fe4e87 | 2013-10-04 10:03:17 -0700 | [diff] [blame] | 62 | """ |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 64 | |
| 65 | # |
| 66 | # ARM assembler templates for each syscall stub |
| 67 | # |
| 68 | |
| 69 | arm_eabi_call_default = syscall_stub_header + """\ |
| 70 | mov ip, r7 |
| 71 | ldr r7, =%(__NR_name)s |
| 72 | swi #0 |
| 73 | mov r7, ip |
| 74 | cmn r0, #(MAX_ERRNO + 1) |
| 75 | bxls lr |
| 76 | neg r0, r0 |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 77 | b __set_errno_internal |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 78 | END(%(func)s) |
| 79 | """ |
| 80 | |
| 81 | arm_eabi_call_long = syscall_stub_header + """\ |
| 82 | mov ip, sp |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 83 | stmfd sp!, {r4, r5, r6, r7} |
Christopher Ferris | ed45970 | 2013-12-02 17:44:53 -0800 | [diff] [blame] | 84 | .cfi_def_cfa_offset 16 |
| 85 | .cfi_rel_offset r4, 0 |
| 86 | .cfi_rel_offset r5, 4 |
| 87 | .cfi_rel_offset r6, 8 |
| 88 | .cfi_rel_offset r7, 12 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 89 | ldmfd ip, {r4, r5, r6} |
| 90 | ldr r7, =%(__NR_name)s |
| 91 | swi #0 |
| 92 | ldmfd sp!, {r4, r5, r6, r7} |
Christopher Ferris | ed45970 | 2013-12-02 17:44:53 -0800 | [diff] [blame] | 93 | .cfi_def_cfa_offset 0 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 94 | cmn r0, #(MAX_ERRNO + 1) |
| 95 | bxls lr |
| 96 | neg r0, r0 |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 97 | b __set_errno_internal |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 98 | END(%(func)s) |
| 99 | """ |
| 100 | |
| 101 | |
| 102 | # |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 103 | # Arm64 assembler templates for each syscall stub |
| 104 | # |
| 105 | |
| 106 | arm64_call = syscall_stub_header + """\ |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 107 | mov x8, %(__NR_name)s |
| 108 | svc #0 |
| 109 | |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 110 | cmn x0, #(MAX_ERRNO + 1) |
| 111 | cneg x0, x0, hi |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 112 | b.hi __set_errno_internal |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 113 | |
| 114 | ret |
| 115 | END(%(func)s) |
| 116 | """ |
| 117 | |
| 118 | |
| 119 | # |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 120 | # MIPS assembler templates for each syscall stub |
| 121 | # |
| 122 | |
Elliott Hughes | 9abbbdc | 2014-02-19 14:54:31 -0800 | [diff] [blame] | 123 | mips_call = syscall_stub_header + """\ |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 124 | .set noreorder |
Elliott Hughes | eae27dc | 2014-02-19 12:20:00 -0800 | [diff] [blame] | 125 | .cpload t9 |
| 126 | li v0, %(__NR_name)s |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 127 | syscall |
Elliott Hughes | eae27dc | 2014-02-19 12:20:00 -0800 | [diff] [blame] | 128 | bnez a3, 1f |
| 129 | move a0, v0 |
| 130 | j ra |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 131 | nop |
| 132 | 1: |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 133 | la t9,__set_errno_internal |
Elliott Hughes | eae27dc | 2014-02-19 12:20:00 -0800 | [diff] [blame] | 134 | j t9 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 135 | nop |
| 136 | .set reorder |
Elliott Hughes | 9abbbdc | 2014-02-19 14:54:31 -0800 | [diff] [blame] | 137 | END(%(func)s) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 138 | """ |
| 139 | |
| 140 | |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 141 | # |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 142 | # MIPS64 assembler templates for each syscall stub |
| 143 | # |
| 144 | |
Elliott Hughes | 9abbbdc | 2014-02-19 14:54:31 -0800 | [diff] [blame] | 145 | mips64_call = syscall_stub_header + """\ |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 146 | .set push |
| 147 | .set noreorder |
| 148 | li v0, %(__NR_name)s |
| 149 | syscall |
| 150 | bnez a3, 1f |
| 151 | move a0, v0 |
| 152 | j ra |
| 153 | nop |
| 154 | 1: |
| 155 | move t0, ra |
| 156 | bal 2f |
| 157 | nop |
| 158 | 2: |
| 159 | .cpsetup ra, t1, 2b |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 160 | LA t9,__set_errno_internal |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 161 | .cpreturn |
| 162 | j t9 |
| 163 | move ra, t0 |
| 164 | .set pop |
Elliott Hughes | 9abbbdc | 2014-02-19 14:54:31 -0800 | [diff] [blame] | 165 | END(%(func)s) |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 166 | """ |
| 167 | |
| 168 | |
| 169 | # |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 170 | # x86 assembler templates for each syscall stub |
| 171 | # |
| 172 | |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 173 | x86_registers = [ "ebx", "ecx", "edx", "esi", "edi", "ebp" ] |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 174 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 175 | x86_call = """\ |
| 176 | movl $%(__NR_name)s, %%eax |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | int $0x80 |
Elliott Hughes | 9aceab5 | 2013-03-12 14:57:30 -0700 | [diff] [blame] | 178 | cmpl $-MAX_ERRNO, %%eax |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 179 | jb 1f |
| 180 | negl %%eax |
| 181 | pushl %%eax |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 182 | call __set_errno_internal |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 183 | addl $4, %%esp |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | 1: |
| 185 | """ |
| 186 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 187 | x86_return = """\ |
| 188 | ret |
| 189 | END(%(func)s) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 190 | """ |
| 191 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 192 | |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 193 | # |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 194 | # x86_64 assembler templates for each syscall stub |
| 195 | # |
| 196 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 197 | x86_64_call = """\ |
| 198 | movl $%(__NR_name)s, %%eax |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 199 | syscall |
| 200 | cmpq $-MAX_ERRNO, %%rax |
| 201 | jb 1f |
| 202 | negl %%eax |
| 203 | movl %%eax, %%edi |
Elliott Hughes | 011e111 | 2014-09-08 15:25:01 -0700 | [diff] [blame] | 204 | call __set_errno_internal |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 205 | 1: |
| 206 | ret |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 207 | END(%(func)s) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 208 | """ |
| 209 | |
Raghu Gandham | 1fa0d84 | 2012-01-27 17:51:42 -0800 | [diff] [blame] | 210 | |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 211 | def 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 Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 231 | |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 232 | def count_arm_param_registers(params): |
| 233 | """This function is used to count the number of register used |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 234 | to pass parameters when invoking an ARM system call. |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 235 | 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 Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 256 | |
David 'Digit' Turner | 95d751f | 2010-12-16 16:47:14 +0100 | [diff] [blame] | 257 | def 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 Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 266 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 267 | def count_generic_param_registers64(params): |
| 268 | count = 0 |
| 269 | for param in params: |
| 270 | count += 1 |
| 271 | return count |
| 272 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 273 | |
Elliott Hughes | cda6209 | 2013-03-22 13:50:44 -0700 | [diff] [blame] | 274 | # 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. |
| 276 | def make__NR_name(name): |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 277 | if name.startswith("__ARM_NR_"): |
Elliott Hughes | cda6209 | 2013-03-22 13:50:44 -0700 | [diff] [blame] | 278 | return name |
| 279 | else: |
| 280 | return "__NR_%s" % (name) |
| 281 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 282 | |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 283 | def add_footer(pointer_length, stub, syscall): |
| 284 | # Add any aliases for this syscall. |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 285 | aliases = syscall["aliases"] |
| 286 | for alias in aliases: |
| 287 | stub += function_alias % { "func" : syscall["func"], "alias" : alias } |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 288 | |
Nick Kralevich | 00490ae | 2015-02-03 11:27:25 -0800 | [diff] [blame^] | 289 | # Use hidden visibility on LP64 for any functions beginning with underscores. |
| 290 | # Force hidden visibility for any functions which begin with 3 underscores |
| 291 | if (pointer_length == 64 and syscall["func"].startswith("__")) or syscall["func"].startswith("___"): |
Elliott Hughes | d465eb4 | 2014-02-19 18:59:19 -0800 | [diff] [blame] | 292 | stub += '.hidden ' + syscall["func"] + '\n' |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 293 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 294 | return stub |
| 295 | |
| 296 | |
| 297 | def arm_eabi_genstub(syscall): |
| 298 | num_regs = count_arm_param_registers(syscall["params"]) |
| 299 | if num_regs > 4: |
| 300 | return arm_eabi_call_long % syscall |
| 301 | return arm_eabi_call_default % syscall |
| 302 | |
| 303 | |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 304 | def arm64_genstub(syscall): |
| 305 | return arm64_call % syscall |
| 306 | |
| 307 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 308 | def mips_genstub(syscall): |
| 309 | return mips_call % syscall |
| 310 | |
| 311 | |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 312 | def mips64_genstub(syscall): |
| 313 | return mips64_call % syscall |
| 314 | |
| 315 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 316 | def x86_genstub(syscall): |
| 317 | result = syscall_stub_header % syscall |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 318 | |
| 319 | numparams = count_generic_param_registers(syscall["params"]) |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 320 | stack_bias = numparams*4 + 4 |
| 321 | offset = 0 |
| 322 | mov_result = "" |
Christopher Ferris | 15b91e9 | 2014-05-29 18:17:09 -0700 | [diff] [blame] | 323 | first_push = True |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 324 | for register in x86_registers[:numparams]: |
| 325 | result += " pushl %%%s\n" % register |
Christopher Ferris | 15b91e9 | 2014-05-29 18:17:09 -0700 | [diff] [blame] | 326 | if first_push: |
| 327 | result += " .cfi_def_cfa_offset 8\n" |
| 328 | result += " .cfi_rel_offset %s, 0\n" % register |
| 329 | first_push = False |
| 330 | else: |
| 331 | result += " .cfi_adjust_cfa_offset 4\n" |
| 332 | result += " .cfi_rel_offset %s, 0\n" % register |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 333 | mov_result += " mov %d(%%esp), %%%s\n" % (stack_bias+offset, register) |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 334 | offset += 4 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 335 | |
Christopher Ferris | 15b91e9 | 2014-05-29 18:17:09 -0700 | [diff] [blame] | 336 | result += mov_result |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 337 | result += x86_call % syscall |
| 338 | |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 339 | for register in reversed(x86_registers[:numparams]): |
| 340 | result += " popl %%%s\n" % register |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 341 | |
| 342 | result += x86_return % syscall |
| 343 | return result |
| 344 | |
Serban Constantinescu | feaa89a | 2013-10-07 16:49:09 +0100 | [diff] [blame] | 345 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 346 | def x86_genstub_socketcall(syscall): |
| 347 | # %ebx <--- Argument 1 - The call id of the needed vectored |
| 348 | # syscall (socket, bind, recv, etc) |
| 349 | # %ecx <--- Argument 2 - Pointer to the rest of the arguments |
| 350 | # from the original function called (socket()) |
| 351 | |
| 352 | result = syscall_stub_header % syscall |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 353 | |
| 354 | # save the regs we need |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 355 | result += " pushl %ebx\n" |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 356 | result += " .cfi_def_cfa_offset 8\n" |
| 357 | result += " .cfi_rel_offset ebx, 0\n" |
Christopher Ferris | 15b91e9 | 2014-05-29 18:17:09 -0700 | [diff] [blame] | 358 | result += " pushl %ecx\n" |
| 359 | result += " .cfi_adjust_cfa_offset 4\n" |
| 360 | result += " .cfi_rel_offset ecx, 0\n" |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 361 | stack_bias = 12 |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 362 | |
| 363 | # set the call id (%ebx) |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 364 | result += " mov $%d, %%ebx\n" % syscall["socketcall_id"] |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 365 | |
| 366 | # set the pointer to the rest of the args into %ecx |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 367 | result += " mov %esp, %ecx\n" |
| 368 | result += " addl $%d, %%ecx\n" % (stack_bias) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 369 | |
| 370 | # now do the syscall code itself |
| 371 | result += x86_call % syscall |
| 372 | |
| 373 | # now restore the saved regs |
Christopher Ferris | e4bc756 | 2014-01-06 16:39:10 -0800 | [diff] [blame] | 374 | result += " popl %ecx\n" |
| 375 | result += " popl %ebx\n" |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 376 | |
| 377 | # epilog |
| 378 | result += x86_return % syscall |
| 379 | return result |
| 380 | |
| 381 | |
| 382 | def x86_64_genstub(syscall): |
| 383 | result = syscall_stub_header % syscall |
| 384 | num_regs = count_generic_param_registers64(syscall["params"]) |
| 385 | if (num_regs > 3): |
| 386 | # rcx is used as 4th argument. Kernel wants it at r10. |
| 387 | result += " movq %rcx, %r10\n" |
| 388 | |
| 389 | result += x86_64_call % syscall |
| 390 | return result |
| 391 | |
| 392 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 393 | class SysCallsTxtParser: |
| 394 | def __init__(self): |
| 395 | self.syscalls = [] |
| 396 | self.lineno = 0 |
| 397 | |
| 398 | def E(self, msg): |
| 399 | print "%d: %s" % (self.lineno, msg) |
| 400 | |
| 401 | def parse_line(self, line): |
| 402 | """ parse a syscall spec line. |
| 403 | |
| 404 | line processing, format is |
| 405 | return type func_name[|alias_list][:syscall_name[:socketcall_id]] ( [paramlist] ) architecture_list |
| 406 | """ |
| 407 | pos_lparen = line.find('(') |
| 408 | E = self.E |
| 409 | if pos_lparen < 0: |
| 410 | E("missing left parenthesis in '%s'" % line) |
| 411 | return |
| 412 | |
| 413 | pos_rparen = line.rfind(')') |
| 414 | if pos_rparen < 0 or pos_rparen <= pos_lparen: |
| 415 | E("missing or misplaced right parenthesis in '%s'" % line) |
| 416 | return |
| 417 | |
| 418 | return_type = line[:pos_lparen].strip().split() |
| 419 | if len(return_type) < 2: |
| 420 | E("missing return type in '%s'" % line) |
| 421 | return |
| 422 | |
| 423 | syscall_func = return_type[-1] |
| 424 | return_type = string.join(return_type[:-1],' ') |
| 425 | socketcall_id = -1 |
| 426 | |
| 427 | pos_colon = syscall_func.find(':') |
| 428 | if pos_colon < 0: |
| 429 | syscall_name = syscall_func |
| 430 | else: |
| 431 | if pos_colon == 0 or pos_colon+1 >= len(syscall_func): |
| 432 | E("misplaced colon in '%s'" % line) |
| 433 | return |
| 434 | |
| 435 | # now find if there is a socketcall_id for a dispatch-type syscall |
| 436 | # after the optional 2nd colon |
| 437 | pos_colon2 = syscall_func.find(':', pos_colon + 1) |
| 438 | if pos_colon2 < 0: |
| 439 | syscall_name = syscall_func[pos_colon+1:] |
| 440 | syscall_func = syscall_func[:pos_colon] |
| 441 | else: |
| 442 | if pos_colon2+1 >= len(syscall_func): |
| 443 | E("misplaced colon2 in '%s'" % line) |
| 444 | return |
| 445 | syscall_name = syscall_func[(pos_colon+1):pos_colon2] |
| 446 | socketcall_id = int(syscall_func[pos_colon2+1:]) |
| 447 | syscall_func = syscall_func[:pos_colon] |
| 448 | |
| 449 | alias_delim = syscall_func.find('|') |
| 450 | if alias_delim > 0: |
| 451 | alias_list = syscall_func[alias_delim+1:].strip() |
| 452 | syscall_func = syscall_func[:alias_delim] |
| 453 | alias_delim = syscall_name.find('|') |
| 454 | if alias_delim > 0: |
| 455 | syscall_name = syscall_name[:alias_delim] |
| 456 | syscall_aliases = string.split(alias_list, ',') |
| 457 | else: |
| 458 | syscall_aliases = [] |
| 459 | |
| 460 | if pos_rparen > pos_lparen+1: |
| 461 | syscall_params = line[pos_lparen+1:pos_rparen].split(',') |
| 462 | params = string.join(syscall_params,',') |
| 463 | else: |
| 464 | syscall_params = [] |
| 465 | params = "void" |
| 466 | |
| 467 | t = { |
| 468 | "name" : syscall_name, |
| 469 | "func" : syscall_func, |
| 470 | "aliases" : syscall_aliases, |
| 471 | "params" : syscall_params, |
| 472 | "decl" : "%-15s %s (%s);" % (return_type, syscall_func, params), |
| 473 | "socketcall_id" : socketcall_id |
| 474 | } |
| 475 | |
| 476 | # Parse the architecture list. |
| 477 | arch_list = line[pos_rparen+1:].strip() |
| 478 | if arch_list == "all": |
| 479 | for arch in all_arches: |
| 480 | t[arch] = True |
| 481 | else: |
| 482 | for arch in string.split(arch_list, ','): |
| 483 | if arch in all_arches: |
| 484 | t[arch] = True |
| 485 | else: |
| 486 | E("invalid syscall architecture '%s' in '%s'" % (arch, line)) |
| 487 | return |
| 488 | |
| 489 | self.syscalls.append(t) |
| 490 | |
| 491 | logging.debug(t) |
| 492 | |
| 493 | |
| 494 | def parse_file(self, file_path): |
| 495 | logging.debug("parse_file: %s" % file_path) |
| 496 | fp = open(file_path) |
| 497 | for line in fp.xreadlines(): |
| 498 | self.lineno += 1 |
| 499 | line = line.strip() |
| 500 | if not line: continue |
| 501 | if line[0] == '#': continue |
| 502 | self.parse_line(line) |
| 503 | |
| 504 | fp.close() |
| 505 | |
| 506 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 507 | class State: |
| 508 | def __init__(self): |
| 509 | self.old_stubs = [] |
| 510 | self.new_stubs = [] |
| 511 | self.other_files = [] |
| 512 | self.syscalls = [] |
| 513 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 514 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 515 | def process_file(self, input): |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 516 | parser = SysCallsTxtParser() |
| 517 | parser.parse_file(input) |
| 518 | self.syscalls = parser.syscalls |
| 519 | parser = None |
| 520 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 521 | for syscall in self.syscalls: |
| 522 | syscall["__NR_name"] = make__NR_name(syscall["name"]) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 523 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 524 | if syscall.has_key("arm"): |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 525 | syscall["asm-arm"] = add_footer(32, arm_eabi_genstub(syscall), syscall) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 526 | |
Colin Cross | d1973ca | 2014-01-21 19:50:58 -0800 | [diff] [blame] | 527 | if syscall.has_key("arm64"): |
| 528 | syscall["asm-arm64"] = add_footer(64, arm64_genstub(syscall), syscall) |
| 529 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 530 | if syscall.has_key("x86"): |
| 531 | if syscall["socketcall_id"] >= 0: |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 532 | syscall["asm-x86"] = add_footer(32, x86_genstub_socketcall(syscall), syscall) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 533 | else: |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 534 | syscall["asm-x86"] = add_footer(32, x86_genstub(syscall), syscall) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 535 | elif syscall["socketcall_id"] >= 0: |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 536 | E("socketcall_id for dispatch syscalls is only supported for x86 in '%s'" % t) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 537 | return |
Elliott Hughes | cd6780b | 2013-02-07 14:07:00 -0800 | [diff] [blame] | 538 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 539 | if syscall.has_key("mips"): |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 540 | syscall["asm-mips"] = add_footer(32, mips_genstub(syscall), syscall) |
The Android Open Source Project | 4e468ed | 2008-12-17 18:03:48 -0800 | [diff] [blame] | 541 | |
Chris Dearman | 5043212 | 2014-02-05 16:59:23 -0800 | [diff] [blame] | 542 | if syscall.has_key("mips64"): |
| 543 | syscall["asm-mips64"] = add_footer(64, mips64_genstub(syscall), syscall) |
| 544 | |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 545 | if syscall.has_key("x86_64"): |
Elliott Hughes | fff6e27 | 2013-10-24 17:03:20 -0700 | [diff] [blame] | 546 | syscall["asm-x86_64"] = add_footer(64, x86_64_genstub(syscall), syscall) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 547 | |
Elliott Hughes | 1b91c6c | 2013-03-22 18:56:24 -0700 | [diff] [blame] | 548 | # Scan a Linux kernel asm/unistd.h file containing __NR_* constants |
| 549 | # and write out equivalent SYS_* constants for glibc source compatibility. |
Elliott Hughes | 5c2772f | 2013-03-21 22:15:06 -0700 | [diff] [blame] | 550 | def scan_linux_unistd_h(self, fp, path): |
| 551 | pattern = re.compile(r'^#define __NR_([a-z]\S+) .*') |
| 552 | syscalls = set() # MIPS defines everything three times; work around that. |
| 553 | for line in open(path): |
| 554 | m = re.search(pattern, line) |
| 555 | if m: |
| 556 | syscalls.add(m.group(1)) |
| 557 | for syscall in sorted(syscalls): |
Elliott Hughes | cda6209 | 2013-03-22 13:50:44 -0700 | [diff] [blame] | 558 | fp.write("#define SYS_%s %s\n" % (syscall, make__NR_name(syscall))) |
Elliott Hughes | 8ecf225 | 2013-03-21 18:06:55 -0700 | [diff] [blame] | 559 | |
| 560 | |
Elliott Hughes | 1b91c6c | 2013-03-22 18:56:24 -0700 | [diff] [blame] | 561 | def gen_glibc_syscalls_h(self): |
Elliott Hughes | cda6209 | 2013-03-22 13:50:44 -0700 | [diff] [blame] | 562 | # TODO: generate a separate file for each architecture, like glibc's bits/syscall.h. |
Elliott Hughes | 9724ce3 | 2013-03-21 19:43:54 -0700 | [diff] [blame] | 563 | glibc_syscalls_h_path = "include/sys/glibc-syscalls.h" |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 564 | logging.info("generating " + glibc_syscalls_h_path) |
Elliott Hughes | 9724ce3 | 2013-03-21 19:43:54 -0700 | [diff] [blame] | 565 | glibc_fp = create_file(glibc_syscalls_h_path) |
Elliott Hughes | 103ccde | 2013-10-16 14:27:59 -0700 | [diff] [blame] | 566 | glibc_fp.write("/* %s */\n" % warning) |
Elliott Hughes | 9724ce3 | 2013-03-21 19:43:54 -0700 | [diff] [blame] | 567 | glibc_fp.write("#ifndef _BIONIC_GLIBC_SYSCALLS_H_\n") |
| 568 | glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n") |
| 569 | |
Serban Constantinescu | feaa89a | 2013-10-07 16:49:09 +0100 | [diff] [blame] | 570 | glibc_fp.write("#if defined(__aarch64__)\n") |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 571 | self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-generic/unistd.h")) |
Serban Constantinescu | feaa89a | 2013-10-07 16:49:09 +0100 | [diff] [blame] | 572 | glibc_fp.write("#elif defined(__arm__)\n") |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 573 | self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-arm/asm/unistd.h")) |
Elliott Hughes | 5c2772f | 2013-03-21 22:15:06 -0700 | [diff] [blame] | 574 | glibc_fp.write("#elif defined(__mips__)\n") |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 575 | self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-mips/asm/unistd.h")) |
Elliott Hughes | 5c2772f | 2013-03-21 22:15:06 -0700 | [diff] [blame] | 576 | glibc_fp.write("#elif defined(__i386__)\n") |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 577 | self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-x86/asm/unistd_32.h")) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 578 | glibc_fp.write("#elif defined(__x86_64__)\n") |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 579 | self.scan_linux_unistd_h(glibc_fp, os.path.join(bionic_libc_root, "kernel/uapi/asm-x86/asm/unistd_64.h")) |
Elliott Hughes | 5c2772f | 2013-03-21 22:15:06 -0700 | [diff] [blame] | 580 | glibc_fp.write("#endif\n") |
| 581 | |
| 582 | glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n") |
| 583 | glibc_fp.close() |
| 584 | self.other_files.append(glibc_syscalls_h_path) |
| 585 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 586 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 587 | # Write each syscall stub. |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 588 | def gen_syscall_stubs(self): |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 589 | for syscall in self.syscalls: |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 590 | for arch in all_arches: |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 591 | if syscall.has_key("asm-%s" % arch): |
| 592 | filename = "arch-%s/syscalls/%s.S" % (arch, syscall["func"]) |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 593 | logging.info(">>> generating " + filename) |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 594 | fp = create_file(filename) |
Elliott Hughes | 0437f3f | 2013-10-07 23:53:13 -0700 | [diff] [blame] | 595 | fp.write(syscall["asm-%s" % arch]) |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 596 | fp.close() |
| 597 | self.new_stubs.append(filename) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 598 | |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 599 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 600 | def regenerate(self): |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 601 | logging.info("scanning for existing architecture-specific stub files...") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 602 | |
Elliott Hughes | d612165 | 2013-09-25 22:43:36 -0700 | [diff] [blame] | 603 | for arch in all_arches: |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 604 | arch_dir = "arch-" + arch |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 605 | logging.info("scanning " + os.path.join(bionic_libc_root, arch_dir)) |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 606 | rel_path = os.path.join(arch_dir, "syscalls") |
| 607 | for file in os.listdir(os.path.join(bionic_libc_root, rel_path)): |
| 608 | if file.endswith(".S"): |
| 609 | self.old_stubs.append(os.path.join(rel_path, file)) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 610 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 611 | logging.info("found %d stub files" % len(self.old_stubs)) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 612 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 613 | if not os.path.exists(bionic_temp): |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 614 | logging.info("creating %s..." % bionic_temp) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 615 | make_dir(bionic_temp) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 616 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 617 | logging.info("re-generating stubs and support files...") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 618 | |
Elliott Hughes | 1b91c6c | 2013-03-22 18:56:24 -0700 | [diff] [blame] | 619 | self.gen_glibc_syscalls_h() |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 620 | self.gen_syscall_stubs() |
| 621 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 622 | logging.info("comparing files...") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 623 | adds = [] |
| 624 | edits = [] |
| 625 | |
| 626 | for stub in self.new_stubs + self.other_files: |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 627 | tmp_file = os.path.join(bionic_temp, stub) |
| 628 | libc_file = os.path.join(bionic_libc_root, stub) |
| 629 | if not os.path.exists(libc_file): |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 630 | # new file, git add it |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 631 | logging.info("new file: " + stub) |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 632 | adds.append(libc_file) |
| 633 | shutil.copyfile(tmp_file, libc_file) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 634 | |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 635 | elif not filecmp.cmp(tmp_file, libc_file): |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 636 | logging.info("changed file: " + stub) |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 637 | edits.append(stub) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 638 | |
| 639 | deletes = [] |
| 640 | for stub in self.old_stubs: |
| 641 | if not stub in self.new_stubs: |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 642 | logging.info("deleted file: " + stub) |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 643 | deletes.append(os.path.join(bionic_libc_root, stub)) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 644 | |
Pavel Chupin | f12a18b | 2012-12-12 13:11:48 +0400 | [diff] [blame] | 645 | if not DRY_RUN: |
| 646 | if adds: |
| 647 | commands.getoutput("git add " + " ".join(adds)) |
| 648 | if deletes: |
| 649 | commands.getoutput("git rm " + " ".join(deletes)) |
| 650 | if edits: |
| 651 | for file in edits: |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 652 | shutil.copyfile(os.path.join(bionic_temp, file), |
| 653 | os.path.join(bionic_libc_root, file)) |
| 654 | commands.getoutput("git add " + " ".join((os.path.join(bionic_libc_root, file)) for file in edits)) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 655 | |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 656 | commands.getoutput("git add %s" % (os.path.join(bionic_libc_root, "SYSCALLS.TXT"))) |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 657 | |
| 658 | if (not adds) and (not deletes) and (not edits): |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 659 | logging.info("no changes detected!") |
David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 660 | else: |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 661 | logging.info("ready to go!!") |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 662 | |
Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 663 | logging.basicConfig(level=logging.INFO) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 664 | |
| 665 | state = State() |
Christopher Ferris | 01bd32e | 2014-08-05 12:19:27 -0700 | [diff] [blame] | 666 | state.process_file(os.path.join(bionic_libc_root, "SYSCALLS.TXT")) |
The Android Open Source Project | a27d2ba | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 667 | state.regenerate() |