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