Skip to content

Commit 0f498b7

Browse files
committed
Fix the nr_from_src script to get ARM-specific syscalls
1 parent cee5f07 commit 0f498b7

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

src/platform/linux-armeabi/nr.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/* automatically generated by nr_from_src.py */
22

3+
pub const ARM_BREAKPOINT: usize = 983041;
4+
pub const ARM_CACHEFLUSH: usize = 983042;
5+
pub const ARM_SET_TLS: usize = 983045;
6+
pub const ARM_USR26: usize = 983043;
7+
pub const ARM_USR32: usize = 983044;
38
pub const _LLSEEK: usize = 140;
49
pub const _NEWSELECT: usize = 142;
510
pub const _SYSCTL: usize = 149;
@@ -309,6 +314,7 @@ pub const STAT: usize = 106;
309314
pub const STAT64: usize = 195;
310315
pub const STATFS: usize = 99;
311316
pub const STATFS64: usize = 266;
317+
pub const STATX: usize = 397;
312318
pub const SWAPOFF: usize = 115;
313319
pub const SWAPON: usize = 87;
314320
pub const SYMLINK: usize = 83;
@@ -352,8 +358,3 @@ pub const WAIT4: usize = 114;
352358
pub const WAITID: usize = 280;
353359
pub const WRITE: usize = 4;
354360
pub const WRITEV: usize = 146;
355-
pub const ARM_BREAKPOINT: usize = 0xf0001;
356-
pub const ARM_CACHEFLUSH: usize = 0xf0002;
357-
pub const ARM_SET_TLS: usize = 0xf0005;
358-
pub const ARM_USR26: usize = 0xf0003;
359-
pub const ARM_USR32: usize = 0xf0004;

src/platform/linux-x86/nr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub const ADD_KEY: usize = 286;
1010
pub const ADJTIMEX: usize = 124;
1111
pub const AFS_SYSCALL: usize = 137;
1212
pub const ALARM: usize = 27;
13+
pub const ARCH_PRCTL: usize = 384;
1314
pub const BDFLUSH: usize = 134;
1415
pub const BIND: usize = 361;
1516
pub const BPF: usize = 357;
@@ -326,6 +327,7 @@ pub const STAT: usize = 106;
326327
pub const STAT64: usize = 195;
327328
pub const STATFS: usize = 99;
328329
pub const STATFS64: usize = 268;
330+
pub const STATX: usize = 383;
329331
pub const STIME: usize = 25;
330332
pub const STTY: usize = 31;
331333
pub const SWAPOFF: usize = 115;

src/platform/linux-x86_64/nr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ pub const SOCKETPAIR: usize = 53;
287287
pub const SPLICE: usize = 275;
288288
pub const STAT: usize = 4;
289289
pub const STATFS: usize = 137;
290+
pub const STATX: usize = 332;
290291
pub const SWAPOFF: usize = 168;
291292
pub const SWAPON: usize = 167;
292293
pub const SYMLINK: usize = 88;

tools/nr_from_src.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/env python3
22

3+
import os
34
import re
45
import tempfile
56
import subprocess
67
import sys
78

8-
from typing import Iterable, Set
9+
from typing import Iterable, Set, Tuple
910

1011
linux_path = '.'
1112

12-
SIMPLE_MATH = re.compile('\((\d+)\s*\+\s*(\d+)\)')
13+
SIMPLE_MATH = re.compile('^[()+0-9a-fx\s]*$')
14+
NUMBER = re.compile('[0-9a-fx]+')
1315

1416
def load_table(path: str, arches: Set[str]):
1517
with open('{}/{}'.format(linux_path, path)) as f:
@@ -22,32 +24,39 @@ def load_table(path: str, arches: Set[str]):
2224
yield (name, int(nr))
2325

2426
def eval_expr(expr: str) -> int:
25-
ma = SIMPLE_MATH.match(expr)
26-
if ma:
27-
return int(ma.group(1)) + int(ma.group(2))
28-
raise Exception('"{}" looks like an expression, but not a supported one'.format(expr))
27+
if not SIMPLE_MATH.match(expr):
28+
raise Exception('"{}" looks like an expression, but not a supported one'.format(expr))
29+
return sum(int(x.group(0), 0) for x in NUMBER.finditer(expr))
2930

3031

31-
def load_headers(names: Iterable[str], arch: str, extra: str = ''):
32+
def load_headers(names: Iterable[Tuple[str, str]], arch: str, extra: str = ''):
3233
with tempfile.NamedTemporaryFile(mode='w+', suffix='.h') as f:
33-
f.write(extra)
34-
f.write('\n')
35-
f.write('#include <asm/unistd.h>\n')
36-
for name in names:
37-
f.write('gen_nr {0} __NR_{0}\n'.format(name))
38-
f.flush()
39-
lines = subprocess.check_output(['gcc', '-nostdinc',
40-
'-I', '{}/arch/{}/include/uapi'.format(linux_path, arch),
41-
'-I', '{}/include'.format(linux_path),
42-
'-P', # don't include line number markers, which make the output annoying to parse
43-
'-E', # only preprocess, don't compile
44-
f.name]).decode('utf-8').split('\n')
34+
with tempfile.TemporaryDirectory() as temp_include_dir:
35+
os.mkdir('{}/asm'.format(temp_include_dir))
36+
with open('{}/asm/unistd-eabi.h'.format(temp_include_dir), 'w'):
37+
pass
38+
with open('{}/asm/unistd-common.h'.format(temp_include_dir), 'w'):
39+
pass
40+
41+
f.write(extra)
42+
f.write('\n')
43+
f.write('#include <asm/unistd.h>\n')
44+
for prefix, name in names:
45+
f.write('gen_nr {prefix}{name} __{prefix}NR_{name}\n'.format(prefix=prefix, name=name))
46+
f.flush()
47+
lines = subprocess.check_output(['gcc', '-nostdinc',
48+
'-I', '{}/arch/{}/include/uapi'.format(linux_path, arch),
49+
'-I', '{}/include'.format(linux_path),
50+
'-I', temp_include_dir,
51+
'-P', # don't include line number markers, which make the output annoying to parse
52+
'-E', # only preprocess, don't compile
53+
f.name]).decode('utf-8').split('\n')
4554

4655
for line in lines:
4756
if not line.startswith('gen_nr '):
4857
continue
4958
_, name, nr = line.split(' ', 2)
50-
if nr.startswith('__NR_'):
59+
if nr.startswith('__'):
5160
# unsupported on this arch
5261
continue
5362
if nr.startswith('('):
@@ -56,15 +65,18 @@ def load_headers(names: Iterable[str], arch: str, extra: str = ''):
5665

5766

5867
def main():
59-
names = set(x.group(1) for x in re.finditer('\\b__NR_([a-z0-9_]+)\\b',
60-
subprocess.check_output(['git', '--no-pager', 'grep', '__NR_'], cwd=linux_path)
68+
RE_SYSCALL_NR=re.compile(r'\b__([A-Z]+_)?NR_([a-z0-9_]+)\b')
69+
names = set(x.groups() for x in RE_SYSCALL_NR.finditer(
70+
subprocess.check_output(['git', '--no-pager', 'grep', r'\<__\([A-Z]\+_\)\?NR_'], cwd=linux_path)
6171
.decode('utf-8')))
6272
if len(names) < 380:
6373
print("didn't find anywhere near enough syscalls; hack must have failed")
74+
subprocess.check_call(['git', '--no-pager', 'grep', r'\<__\([A-Z]\+_\)\?NR_'], cwd=linux_path)
6475
sys.exit(1)
76+
ARM_NAMES = ["breakpoint", "cacheflush", "usr26", "usr32", "set_tls"]
6577
numbers = {
6678
'linux-aarch64': dict(load_headers(names, 'arm64')),
67-
'linux-armeabi': dict(load_table('arch/arm/tools/syscall.tbl', {'common', 'eabi'})),
79+
'linux-armeabi': dict(list(load_table('arch/arm/tools/syscall.tbl', {'common', 'eabi'})) + list(load_headers(names, 'arm', '#define __ARM_EABI__'))),
6880
'linux-mips': dict(load_headers(names, 'mips',
6981
'#define _MIPS_SIM _MIPS_SIM_ABI32')),
7082
'linux-mips64': dict(load_headers(names, 'mips',

0 commit comments

Comments
 (0)