xref: /xnu-8792.41.9/libsyscall/xcodescripts/create-syscalls.pl (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions#!/usr/bin/perl
2*5c2921b0SApple OSS Distributions#
3*5c2921b0SApple OSS Distributions# Copyright (c) 2006-2014 Apple Inc. All rights reserved.
4*5c2921b0SApple OSS Distributions#
5*5c2921b0SApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6*5c2921b0SApple OSS Distributions#
7*5c2921b0SApple OSS Distributions# This file contains Original Code and/or Modifications of Original Code
8*5c2921b0SApple OSS Distributions# as defined in and that are subject to the Apple Public Source License
9*5c2921b0SApple OSS Distributions# Version 2.0 (the 'License'). You may not use this file except in
10*5c2921b0SApple OSS Distributions# compliance with the License. Please obtain a copy of the License at
11*5c2921b0SApple OSS Distributions# http://www.opensource.apple.com/apsl/ and read it before using this
12*5c2921b0SApple OSS Distributions# file.
13*5c2921b0SApple OSS Distributions#
14*5c2921b0SApple OSS Distributions# The Original Code and all software distributed under the License are
15*5c2921b0SApple OSS Distributions# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16*5c2921b0SApple OSS Distributions# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17*5c2921b0SApple OSS Distributions# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18*5c2921b0SApple OSS Distributions# FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19*5c2921b0SApple OSS Distributions# Please see the License for the specific language governing rights and
20*5c2921b0SApple OSS Distributions# limitations under the License.
21*5c2921b0SApple OSS Distributions#
22*5c2921b0SApple OSS Distributions# @APPLE_OSREFERENCE_LICENSE_HEADER_END@
23*5c2921b0SApple OSS Distributions#
24*5c2921b0SApple OSS Distributions##########################################################################
25*5c2921b0SApple OSS Distributions#
26*5c2921b0SApple OSS Distributions# % create-syscalls.pl syscalls.master custom-directory platforms-directory platform-name out-directory
27*5c2921b0SApple OSS Distributions#
28*5c2921b0SApple OSS Distributions# This script fills the the out-directory with a Makefile.inc and *.s
29*5c2921b0SApple OSS Distributions# files to create the double-underbar syscall stubs.  It reads the
30*5c2921b0SApple OSS Distributions# syscall.master file to get the symbol names and number of arguments,
31*5c2921b0SApple OSS Distributions# and whether Libsystem should automatically create the (non-double-underbar)
32*5c2921b0SApple OSS Distributions# stubs if Libc doesn't provide a wrapper.  Which system calls will get
33*5c2921b0SApple OSS Distributions# the automatic treatment is writen to the libsyscall.list file, also
34*5c2921b0SApple OSS Distributions# written to the out-directory.
35*5c2921b0SApple OSS Distributions#
36*5c2921b0SApple OSS Distributions# The custom-directory contains:
37*5c2921b0SApple OSS Distributions# 1. SYS.h - used by the automatically created *.s and custom files
38*5c2921b0SApple OSS Distributions# 2. custom.s - contains architecture specific additional system calls and
39*5c2921b0SApple OSS Distributions#    auxilliary routines (like cerror)
40*5c2921b0SApple OSS Distributions# 3. special case double-underbar stub files - which are copied into
41*5c2921b0SApple OSS Distributions#    the out-directory
42*5c2921b0SApple OSS Distributions#
43*5c2921b0SApple OSS Distributions##########################################################################
44*5c2921b0SApple OSS Distributions
45*5c2921b0SApple OSS Distributionsuse strict;
46*5c2921b0SApple OSS Distributionsuse File::Basename ();
47*5c2921b0SApple OSS Distributionsuse File::Copy ();
48*5c2921b0SApple OSS Distributionsuse File::Spec;
49*5c2921b0SApple OSS Distributionsuse IO::File;
50*5c2921b0SApple OSS Distributions
51*5c2921b0SApple OSS Distributionsmy $MyName = File::Basename::basename($0);
52*5c2921b0SApple OSS Distributions
53*5c2921b0SApple OSS Distributionsmy @CustomSrc = qw(custom.s);
54*5c2921b0SApple OSS Distributions
55*5c2921b0SApple OSS Distributionsmy @Architectures = split /\s/, $ENV{"ARCHS"};
56*5c2921b0SApple OSS Distributionsmy @Copy = (qw(SYS.h), @CustomSrc);
57*5c2921b0SApple OSS Distributionsmy $CustomDir;
58*5c2921b0SApple OSS Distributionsmy $PlatformsDir;
59*5c2921b0SApple OSS Distributionsmy $PlatformName;
60*5c2921b0SApple OSS Distributionsmy $OutDir;
61*5c2921b0SApple OSS Distributions# size in bytes of known types (only used for i386)
62*5c2921b0SApple OSS Distributionsmy %TypeBytes = (
63*5c2921b0SApple OSS Distributions    'au_asid_t'		=> 4,
64*5c2921b0SApple OSS Distributions    'sae_associd_t'	=> 4,
65*5c2921b0SApple OSS Distributions    'caddr_t'		=> 4,
66*5c2921b0SApple OSS Distributions    'sae_connid_t'	=> 4,
67*5c2921b0SApple OSS Distributions    'gid_t'		=> 4,
68*5c2921b0SApple OSS Distributions    'id_t'		=> 4,
69*5c2921b0SApple OSS Distributions    'idtype_t'		=> 4,
70*5c2921b0SApple OSS Distributions    'int'		=> 4,
71*5c2921b0SApple OSS Distributions    'int32_t'		=> 4,
72*5c2921b0SApple OSS Distributions    'int64_t'		=> 8,
73*5c2921b0SApple OSS Distributions    'key_t'		=> 4,
74*5c2921b0SApple OSS Distributions    'long'		=> 4,
75*5c2921b0SApple OSS Distributions    'mach_port_name_t'	=> 4,
76*5c2921b0SApple OSS Distributions    'mode_t'		=> 4,
77*5c2921b0SApple OSS Distributions    'off_t'		=> 8,
78*5c2921b0SApple OSS Distributions    'pid_t'		=> 4,
79*5c2921b0SApple OSS Distributions    'semun_t'		=> 4,
80*5c2921b0SApple OSS Distributions    'sigset_t'		=> 4,
81*5c2921b0SApple OSS Distributions    'size_t'		=> 4,
82*5c2921b0SApple OSS Distributions    'socklen_t'		=> 4,
83*5c2921b0SApple OSS Distributions    'ssize_t'		=> 4,
84*5c2921b0SApple OSS Distributions    'u_int'		=> 4,
85*5c2921b0SApple OSS Distributions    'u_long'		=> 4,
86*5c2921b0SApple OSS Distributions    'uid_t'		=> 4,
87*5c2921b0SApple OSS Distributions    'uint32_t'		=> 4,
88*5c2921b0SApple OSS Distributions    'uint64_t'		=> 8,
89*5c2921b0SApple OSS Distributions    'user_addr_t'	=> 4,
90*5c2921b0SApple OSS Distributions    'user_long_t'	=> 4,
91*5c2921b0SApple OSS Distributions    'user_size_t'	=> 4,
92*5c2921b0SApple OSS Distributions    'user_ssize_t'	=> 4,
93*5c2921b0SApple OSS Distributions    'user_ulong_t'	=> 4,
94*5c2921b0SApple OSS Distributions    'uuid_t'		=> 4,
95*5c2921b0SApple OSS Distributions);
96*5c2921b0SApple OSS Distributions
97*5c2921b0SApple OSS Distributions# Types that potentially have different sizes in user-space compared to
98*5c2921b0SApple OSS Distributions# kernel-space as well as whether the value should be sign/zero-extended when
99*5c2921b0SApple OSS Distributions# passing the user/kernel boundary.
100*5c2921b0SApple OSS Distributionsmy %UserKernelMismatchTypes = (
101*5c2921b0SApple OSS Distributions    'long'          => 'SIGN_EXTEND',
102*5c2921b0SApple OSS Distributions    'size_t'        => 'ZERO_EXTEND',
103*5c2921b0SApple OSS Distributions    'u_long'        => 'ZERO_EXTEND',
104*5c2921b0SApple OSS Distributions    'user_size_t'   => 'ZERO_EXTEND',
105*5c2921b0SApple OSS Distributions    'user_ssize_t'  => 'SIGN_EXTEND'
106*5c2921b0SApple OSS Distributions);
107*5c2921b0SApple OSS Distributions
108*5c2921b0SApple OSS Distributions# Moving towards storing all data in this hash, then we always know
109*5c2921b0SApple OSS Distributions# if data is aliased or not, or promoted or not.
110*5c2921b0SApple OSS Distributionsmy %Symbols = (
111*5c2921b0SApple OSS Distributions    "syscall" => {
112*5c2921b0SApple OSS Distributions        c_sym => "syscall",
113*5c2921b0SApple OSS Distributions        syscall => "syscall",
114*5c2921b0SApple OSS Distributions        asm_sym => "_syscall",
115*5c2921b0SApple OSS Distributions        is_private => undef,
116*5c2921b0SApple OSS Distributions        is_custom => undef,
117*5c2921b0SApple OSS Distributions        nargs => 0,
118*5c2921b0SApple OSS Distributions        bytes => 0,
119*5c2921b0SApple OSS Distributions        aliases => {},
120*5c2921b0SApple OSS Distributions        mismatch_args => {}, # Arguments that might need to be zero/sign-extended
121*5c2921b0SApple OSS Distributions    },
122*5c2921b0SApple OSS Distributions);
123*5c2921b0SApple OSS Distributions
124*5c2921b0SApple OSS Distributions# An explicit list of cancelable syscalls. For creating stubs that call the
125*5c2921b0SApple OSS Distributions# cancellable version of cerror.
126*5c2921b0SApple OSS Distributionsmy @Cancelable = qw/
127*5c2921b0SApple OSS Distributions	accept access aio_suspend
128*5c2921b0SApple OSS Distributions	close connect connectx
129*5c2921b0SApple OSS Distributions	disconnectx
130*5c2921b0SApple OSS Distributions	faccessat fcntl fdatasync fpathconf fstat fstatat fsync
131*5c2921b0SApple OSS Distributions	getlogin
132*5c2921b0SApple OSS Distributions	ioctl
133*5c2921b0SApple OSS Distributions	link linkat lseek lstat
134*5c2921b0SApple OSS Distributions	msgrcv msgsnd msync
135*5c2921b0SApple OSS Distributions	open openat
136*5c2921b0SApple OSS Distributions	pathconf peeloff poll posix_spawn pread preadv pselect pwrite pwritev
137*5c2921b0SApple OSS Distributions	read readv recvfrom recvmsg rename renameat
138*5c2921b0SApple OSS Distributions	rename_ext
139*5c2921b0SApple OSS Distributions	__semwait_signal __sigwait
140*5c2921b0SApple OSS Distributions	select sem_wait semop sendmsg sendto sigsuspend stat symlink symlinkat sync
141*5c2921b0SApple OSS Distributions	unlink unlinkat
142*5c2921b0SApple OSS Distributions	wait4 waitid write writev
143*5c2921b0SApple OSS Distributions/;
144*5c2921b0SApple OSS Distributions
145*5c2921b0SApple OSS Distributionssub usage {
146*5c2921b0SApple OSS Distributions    die "Usage: $MyName syscalls.master custom-directory platforms-directory platform-name out-directory\n";
147*5c2921b0SApple OSS Distributions}
148*5c2921b0SApple OSS Distributions
149*5c2921b0SApple OSS Distributions##########################################################################
150*5c2921b0SApple OSS Distributions# Read the syscall.master file and collect the system call names and number
151*5c2921b0SApple OSS Distributions# of arguments.  It looks for the NO_SYSCALL_STUB quailifier following the
152*5c2921b0SApple OSS Distributions# prototype to determine if no automatic stub should be created by Libsystem.
153*5c2921b0SApple OSS Distributions#
154*5c2921b0SApple OSS Distributions# The `sys_` prefix is stripped from syscall names, and is only kept for
155*5c2921b0SApple OSS Distributions# the kernel symbol in order to avoid namespace clashes and identify
156*5c2921b0SApple OSS Distributions# syscalls more easily.
157*5c2921b0SApple OSS Distributions#
158*5c2921b0SApple OSS Distributions# For the #if lines in syscall.master, all macros are assumed to be defined,
159*5c2921b0SApple OSS Distributions# except COMPAT_GETFSSTAT (assumed undefined).
160*5c2921b0SApple OSS Distributions##########################################################################
161*5c2921b0SApple OSS Distributionssub readMaster {
162*5c2921b0SApple OSS Distributions    my $file = shift;
163*5c2921b0SApple OSS Distributions    local $_;
164*5c2921b0SApple OSS Distributions    my $f = IO::File->new($file, 'r');
165*5c2921b0SApple OSS Distributions    die "$MyName: $file: $!\n" unless defined($f);
166*5c2921b0SApple OSS Distributions    my $line = 0;
167*5c2921b0SApple OSS Distributions    my $skip = 0;
168*5c2921b0SApple OSS Distributions    my $allow_missing = 0;
169*5c2921b0SApple OSS Distributions    while(<$f>) {
170*5c2921b0SApple OSS Distributions        $line++;
171*5c2921b0SApple OSS Distributions        if(/^#\s*endif/) {
172*5c2921b0SApple OSS Distributions            $skip = 0;
173*5c2921b0SApple OSS Distributions            $allow_missing = 0;
174*5c2921b0SApple OSS Distributions            next;
175*5c2921b0SApple OSS Distributions        }
176*5c2921b0SApple OSS Distributions        if(/^#\s*else/) {
177*5c2921b0SApple OSS Distributions            $skip = -$skip;
178*5c2921b0SApple OSS Distributions            $allow_missing = 0;
179*5c2921b0SApple OSS Distributions            next;
180*5c2921b0SApple OSS Distributions        }
181*5c2921b0SApple OSS Distributions        chomp;
182*5c2921b0SApple OSS Distributions        if(/^#\s*ifndef\s+(RC_HIDE\S+)$/) {
183*5c2921b0SApple OSS Distributions            $skip = 1;
184*5c2921b0SApple OSS Distributions            $allow_missing = 1;
185*5c2921b0SApple OSS Distributions        }
186*5c2921b0SApple OSS Distributions        if(/^#\s*if\s+(\S+)$/) {
187*5c2921b0SApple OSS Distributions            $skip = ($1 eq 'COMPAT_GETFSSTAT') ? -1 : 1;
188*5c2921b0SApple OSS Distributions            next;
189*5c2921b0SApple OSS Distributions        }
190*5c2921b0SApple OSS Distributions        next if $skip < 0;
191*5c2921b0SApple OSS Distributions        next unless /^\d/;
192*5c2921b0SApple OSS Distributions        s/^[^{]*{\s*//;
193*5c2921b0SApple OSS Distributions        s/\s*}.*$//; # }
194*5c2921b0SApple OSS Distributions        die "$MyName: no function prototype on line $line\n" unless length($_) > 0 && /;$/;
195*5c2921b0SApple OSS Distributions        my $no_syscall_stub = /\)\s*NO_SYSCALL_STUB\s*;/;
196*5c2921b0SApple OSS Distributions        my($name, $args) = /\s(\S+)\s*\(([^)]*)\)/;
197*5c2921b0SApple OSS Distributions        next if $name =~ /e?nosys/;
198*5c2921b0SApple OSS Distributions        $name =~ s/^sys_//;
199*5c2921b0SApple OSS Distributions        $args =~ s/^\s+//;
200*5c2921b0SApple OSS Distributions        $args =~ s/\s+$//;
201*5c2921b0SApple OSS Distributions        my $argbytes = 0;
202*5c2921b0SApple OSS Distributions        my $nargs = 0;
203*5c2921b0SApple OSS Distributions        my %mismatch_args;
204*5c2921b0SApple OSS Distributions        if($args ne '' && $args ne 'void') {
205*5c2921b0SApple OSS Distributions            my @a = split(',', $args);
206*5c2921b0SApple OSS Distributions            $nargs = scalar(@a);
207*5c2921b0SApple OSS Distributions            my $index = 0;
208*5c2921b0SApple OSS Distributions            for my $type (@a) {
209*5c2921b0SApple OSS Distributions                $type =~ s/\s*\w+$//; # remove the argument name
210*5c2921b0SApple OSS Distributions
211*5c2921b0SApple OSS Distributions                # Calculate the size of all the arguments (only used for i386)
212*5c2921b0SApple OSS Distributions                if($type =~ /\*$/) {
213*5c2921b0SApple OSS Distributions                    $argbytes += 4; # a pointer type
214*5c2921b0SApple OSS Distributions                } else {
215*5c2921b0SApple OSS Distributions                    $type =~ s/^.*\s//; # remove any type qualifier, like unsigned
216*5c2921b0SApple OSS Distributions                    my $b = $TypeBytes{$type};
217*5c2921b0SApple OSS Distributions                    die "$MyName: $name: unknown type '$type'\n" unless defined($b);
218*5c2921b0SApple OSS Distributions                    $argbytes += $b;
219*5c2921b0SApple OSS Distributions                }
220*5c2921b0SApple OSS Distributions                # Determine which arguments might need to be zero/sign-extended
221*5c2921b0SApple OSS Distributions                if(exists $UserKernelMismatchTypes{$type}) {
222*5c2921b0SApple OSS Distributions                    $mismatch_args{$index} = $UserKernelMismatchTypes{$type};
223*5c2921b0SApple OSS Distributions                }
224*5c2921b0SApple OSS Distributions
225*5c2921b0SApple OSS Distributions                $index++;
226*5c2921b0SApple OSS Distributions            }
227*5c2921b0SApple OSS Distributions        }
228*5c2921b0SApple OSS Distributions        $Symbols{$name} = {
229*5c2921b0SApple OSS Distributions            c_sym => $name,
230*5c2921b0SApple OSS Distributions            syscall => $name,
231*5c2921b0SApple OSS Distributions            asm_sym => $no_syscall_stub ? "___$name" : "_$name",
232*5c2921b0SApple OSS Distributions            is_private => $no_syscall_stub,
233*5c2921b0SApple OSS Distributions            is_custom => undef,
234*5c2921b0SApple OSS Distributions            nargs => $nargs,
235*5c2921b0SApple OSS Distributions            bytes => $argbytes,
236*5c2921b0SApple OSS Distributions            aliases => {},
237*5c2921b0SApple OSS Distributions            mismatch_args => \%mismatch_args, # Arguments that might need to be zero/sign-extended
238*5c2921b0SApple OSS Distributions            except => [],
239*5c2921b0SApple OSS Distributions            allow_missing => $allow_missing,
240*5c2921b0SApple OSS Distributions        };
241*5c2921b0SApple OSS Distributions    }
242*5c2921b0SApple OSS Distributions}
243*5c2921b0SApple OSS Distributions
244*5c2921b0SApple OSS Distributionssub checkForCustomStubs {
245*5c2921b0SApple OSS Distributions    my ($dir) = @_;
246*5c2921b0SApple OSS Distributions
247*5c2921b0SApple OSS Distributions    my ($c_sym_name, $sym);
248*5c2921b0SApple OSS Distributions    while (($c_sym_name, $sym) = each %Symbols) {
249*5c2921b0SApple OSS Distributions        my $source = "__".$$sym{c_sym}.".s";
250*5c2921b0SApple OSS Distributions        my $custom = File::Spec->join($dir, $source);
251*5c2921b0SApple OSS Distributions        next unless -f $custom;
252*5c2921b0SApple OSS Distributions
253*5c2921b0SApple OSS Distributions        $$sym{is_custom} = $source;
254*5c2921b0SApple OSS Distributions        if (!$$sym{is_private}) {
255*5c2921b0SApple OSS Distributions            foreach my $subarch (@Architectures) {
256*5c2921b0SApple OSS Distributions                (my $arch = $subarch) =~ s/arm(v.*)/arm/;
257*5c2921b0SApple OSS Distributions                $arch =~ s/x86_64(.*)/x86_64/;
258*5c2921b0SApple OSS Distributions                $arch =~ s/arm64(.*)/arm64/;
259*5c2921b0SApple OSS Distributions                $$sym{aliases}{$arch} = [] unless $$sym{aliases}{$arch};
260*5c2921b0SApple OSS Distributions                push(@{$$sym{aliases}{$arch}}, $$sym{asm_sym});
261*5c2921b0SApple OSS Distributions            }
262*5c2921b0SApple OSS Distributions            $$sym{asm_sym} = "__".$$sym{asm_sym};
263*5c2921b0SApple OSS Distributions            $$sym{is_private} = 1;
264*5c2921b0SApple OSS Distributions        }
265*5c2921b0SApple OSS Distributions    }
266*5c2921b0SApple OSS Distributions}
267*5c2921b0SApple OSS Distributions
268*5c2921b0SApple OSS Distributionssub readAliases {
269*5c2921b0SApple OSS Distributions    my ($platformDir, $platformName) = @_;
270*5c2921b0SApple OSS Distributions    my $genericMap = File::Spec->join($platformDir, "syscall.map");
271*5c2921b0SApple OSS Distributions
272*5c2921b0SApple OSS Distributions    my %sym_to_c;
273*5c2921b0SApple OSS Distributions    foreach my $k (keys %Symbols) {
274*5c2921b0SApple OSS Distributions        $sym_to_c{$Symbols{$k}{asm_sym}} = $k;
275*5c2921b0SApple OSS Distributions    }
276*5c2921b0SApple OSS Distributions
277*5c2921b0SApple OSS Distributions    my @a = ();
278*5c2921b0SApple OSS Distributions    for my $arch (@Architectures) {
279*5c2921b0SApple OSS Distributions        (my $new_arch = $arch) =~ s/arm(v.*)/arm/g;
280*5c2921b0SApple OSS Distributions        $new_arch =~ s/x86_64(.*)/x86_64/g;
281*5c2921b0SApple OSS Distributions        $new_arch =~ s/arm64(.*)/arm64/g;
282*5c2921b0SApple OSS Distributions        push(@a, $new_arch) unless grep { $_ eq $new_arch } @a;
283*5c2921b0SApple OSS Distributions    }
284*5c2921b0SApple OSS Distributions
285*5c2921b0SApple OSS Distributions    foreach my $arch (@a) {
286*5c2921b0SApple OSS Distributions        my $syscallFile = File::Spec->join($platformDir, $platformName, $arch, "syscall.map");
287*5c2921b0SApple OSS Distributions
288*5c2921b0SApple OSS Distributions        my @files = ();
289*5c2921b0SApple OSS Distributions        push(@files, IO::File->new($syscallFile, 'r'));
290*5c2921b0SApple OSS Distributions        die "$MyName: $syscallFile: $!\n" unless defined($files[$#files]);
291*5c2921b0SApple OSS Distributions        push(@files, IO::File->new($genericMap, 'r'));
292*5c2921b0SApple OSS Distributions        die "$MyName: $genericMap: $!\n" unless defined($files[$#files]);
293*5c2921b0SApple OSS Distributions
294*5c2921b0SApple OSS Distributions        foreach my $f (@files) {
295*5c2921b0SApple OSS Distributions            while (<$f>) {
296*5c2921b0SApple OSS Distributions                next if /^#/;
297*5c2921b0SApple OSS Distributions                chomp;
298*5c2921b0SApple OSS Distributions
299*5c2921b0SApple OSS Distributions                my ($alias, $target_symbol) = split;
300*5c2921b0SApple OSS Distributions                if (defined($target_symbol)) {
301*5c2921b0SApple OSS Distributions                    foreach my $sym (values %Symbols) {
302*5c2921b0SApple OSS Distributions                        # I've eliminated most of the ugly from this script except
303*5c2921b0SApple OSS Distributions                        # the need to try stripping underbars here.
304*5c2921b0SApple OSS Distributions                        if ($$sym{is_private}) {
305*5c2921b0SApple OSS Distributions                            next unless $$sym{asm_sym} eq $target_symbol;
306*5c2921b0SApple OSS Distributions                        } else {
307*5c2921b0SApple OSS Distributions                            (my $target = $target_symbol) =~ s/^__//;
308*5c2921b0SApple OSS Distributions                            next unless ($$sym{asm_sym} eq $target || $$sym{asm_sym} eq $target_symbol);
309*5c2921b0SApple OSS Distributions                        }
310*5c2921b0SApple OSS Distributions                        $$sym{aliases}{$arch} = [] unless $$sym{aliases}{$arch};
311*5c2921b0SApple OSS Distributions
312*5c2921b0SApple OSS Distributions                        die "$MyName: $arch $$sym{asm_sym} -> $alias: Duplicate alias.\n" if grep { $_ eq $alias } @{$$sym{aliases}{$arch}};
313*5c2921b0SApple OSS Distributions                        push(@{$$sym{aliases}{$arch}}, $alias);
314*5c2921b0SApple OSS Distributions
315*5c2921b0SApple OSS Distributions                        # last thing to do, if we aliased over a first class symbol, we need
316*5c2921b0SApple OSS Distributions                        # to mark it
317*5c2921b0SApple OSS Distributions                        my $c = $sym_to_c{$alias};
318*5c2921b0SApple OSS Distributions                        if ($Symbols{$c}) {
319*5c2921b0SApple OSS Distributions                            push(@{$Symbols{$c}{except}}, $arch);
320*5c2921b0SApple OSS Distributions                        }
321*5c2921b0SApple OSS Distributions                    }
322*5c2921b0SApple OSS Distributions                }
323*5c2921b0SApple OSS Distributions            }
324*5c2921b0SApple OSS Distributions        }
325*5c2921b0SApple OSS Distributions    }
326*5c2921b0SApple OSS Distributions}
327*5c2921b0SApple OSS Distributions
328*5c2921b0SApple OSS Distributions##########################################################################
329*5c2921b0SApple OSS Distributions# Make a __xxx.s file: if it exists in the $CustomDir, just copy it, otherwise
330*5c2921b0SApple OSS Distributions# create one.  We define the macro __SYSCALL_32BIT_ARG_BYTES so that SYS.h could
331*5c2921b0SApple OSS Distributions# use that to define __SYSCALL dependent on the arguments' total size.
332*5c2921b0SApple OSS Distributions##########################################################################
333*5c2921b0SApple OSS Distributionssub writeStubForSymbol {
334*5c2921b0SApple OSS Distributions    my ($f, $symbol) = @_;
335*5c2921b0SApple OSS Distributions
336*5c2921b0SApple OSS Distributions    my @conditions;
337*5c2921b0SApple OSS Distributions    my $has_arm64 = 0;
338*5c2921b0SApple OSS Distributions    for my $subarch (@Architectures) {
339*5c2921b0SApple OSS Distributions        (my $arch = $subarch) =~ s/arm(v.*)/arm/;
340*5c2921b0SApple OSS Distributions        $arch =~ s/x86_64(.*)/x86_64/;
341*5c2921b0SApple OSS Distributions        $arch =~ s/arm64(.*)/arm64/;
342*5c2921b0SApple OSS Distributions        push(@conditions, "defined(__${arch}__)") unless grep { $_ eq $arch } @{$$symbol{except}};
343*5c2921b0SApple OSS Distributions
344*5c2921b0SApple OSS Distributions        if($arch eq "arm64") {
345*5c2921b0SApple OSS Distributions            $has_arm64 = 1 unless grep { $_ eq $arch } @{$$symbol{except}};
346*5c2921b0SApple OSS Distributions        }
347*5c2921b0SApple OSS Distributions    }
348*5c2921b0SApple OSS Distributions
349*5c2921b0SApple OSS Distributions    my %is_cancel;
350*5c2921b0SApple OSS Distributions    for (@Cancelable) { $is_cancel{$_} = 1 };
351*5c2921b0SApple OSS Distributions
352*5c2921b0SApple OSS Distributions    print $f "#define __SYSCALL_32BIT_ARG_BYTES $$symbol{bytes}\n";
353*5c2921b0SApple OSS Distributions    print $f "#include \"SYS.h\"\n\n";
354*5c2921b0SApple OSS Distributions    if ($$symbol{allow_missing}) {
355*5c2921b0SApple OSS Distributions        printf $f "#ifdef SYS_%s\n", $$symbol{syscall};
356*5c2921b0SApple OSS Distributions    }
357*5c2921b0SApple OSS Distributions
358*5c2921b0SApple OSS Distributions    if (scalar(@conditions)) {
359*5c2921b0SApple OSS Distributions        printf $f "#ifndef SYS_%s\n", $$symbol{syscall};
360*5c2921b0SApple OSS Distributions        printf $f "#error \"SYS_%s not defined. The header files libsyscall is building against do not match syscalls.master.\"\n", $$symbol{syscall};
361*5c2921b0SApple OSS Distributions        printf $f "#endif\n\n";
362*5c2921b0SApple OSS Distributions    }
363*5c2921b0SApple OSS Distributions
364*5c2921b0SApple OSS Distributions    my $nc = ($is_cancel{$$symbol{syscall}} ? "cerror" : "cerror_nocancel");
365*5c2921b0SApple OSS Distributions
366*5c2921b0SApple OSS Distributions    if($has_arm64) {
367*5c2921b0SApple OSS Distributions        printf $f "#if defined(__arm64__)\n";
368*5c2921b0SApple OSS Distributions        printf $f "MI_ENTRY_POINT(%s)\n", $$symbol{asm_sym};
369*5c2921b0SApple OSS Distributions        if(keys %{$$symbol{mismatch_args}}) {
370*5c2921b0SApple OSS Distributions            while(my($argnum, $extend) = each %{$$symbol{mismatch_args}}) {
371*5c2921b0SApple OSS Distributions                printf $f "%s(%d)\n", $extend, $argnum;
372*5c2921b0SApple OSS Distributions            }
373*5c2921b0SApple OSS Distributions        }
374*5c2921b0SApple OSS Distributions
375*5c2921b0SApple OSS Distributions        printf $f "SYSCALL_NONAME(%s, %d, %s)\n", $$symbol{syscall}, $$symbol{nargs}, $nc;
376*5c2921b0SApple OSS Distributions        printf $f "ret\n";
377*5c2921b0SApple OSS Distributions        printf $f "#else\n";
378*5c2921b0SApple OSS Distributions    }
379*5c2921b0SApple OSS Distributions
380*5c2921b0SApple OSS Distributions    if (scalar(@conditions)) {
381*5c2921b0SApple OSS Distributions        printf $f "#if " . join(" || ", @conditions) . "\n";
382*5c2921b0SApple OSS Distributions        printf $f "__SYSCALL2(%s, %s, %d, %s)\n", $$symbol{asm_sym}, $$symbol{syscall}, $$symbol{nargs}, $nc;
383*5c2921b0SApple OSS Distributions        if (!$$symbol{is_private} && (scalar(@conditions) < scalar(@Architectures))) {
384*5c2921b0SApple OSS Distributions            printf $f "#else\n";
385*5c2921b0SApple OSS Distributions            printf $f "__SYSCALL2(%s, %s, %d, %s)\n", "__".$$symbol{asm_sym}, $$symbol{syscall}, $$symbol{nargs}, $nc;
386*5c2921b0SApple OSS Distributions        }
387*5c2921b0SApple OSS Distributions        printf $f "#endif\n\n";
388*5c2921b0SApple OSS Distributions    } else {
389*5c2921b0SApple OSS Distributions        # actually this isnt an inconsistency. kernel can expose what it wants but if all our arches
390*5c2921b0SApple OSS Distributions        # override it we need to honour that.
391*5c2921b0SApple OSS Distributions    }
392*5c2921b0SApple OSS Distributions
393*5c2921b0SApple OSS Distributions    if ($$symbol{allow_missing}) {
394*5c2921b0SApple OSS Distributions        printf $f "#endif\n";
395*5c2921b0SApple OSS Distributions    }
396*5c2921b0SApple OSS Distributions
397*5c2921b0SApple OSS Distributions    if($has_arm64) {
398*5c2921b0SApple OSS Distributions        printf $f "#endif\n\n";
399*5c2921b0SApple OSS Distributions    }
400*5c2921b0SApple OSS Distributions}
401*5c2921b0SApple OSS Distributions
402*5c2921b0SApple OSS Distributionssub writeAliasesForSymbol {
403*5c2921b0SApple OSS Distributions    my ($f, $symbol) = @_;
404*5c2921b0SApple OSS Distributions
405*5c2921b0SApple OSS Distributions    if ($$symbol{allow_missing}) {
406*5c2921b0SApple OSS Distributions        printf $f "#ifdef SYS_%s\n", $$symbol{syscall};
407*5c2921b0SApple OSS Distributions    }
408*5c2921b0SApple OSS Distributions
409*5c2921b0SApple OSS Distributions    foreach my $subarch (@Architectures) {
410*5c2921b0SApple OSS Distributions        (my $arch = $subarch) =~ s/arm(v.*)/arm/;
411*5c2921b0SApple OSS Distributions        $arch =~ s/x86_64(.*)/x86_64/;
412*5c2921b0SApple OSS Distributions        $arch =~ s/arm64(.*)/arm64/;
413*5c2921b0SApple OSS Distributions
414*5c2921b0SApple OSS Distributions        next unless scalar($$symbol{aliases}{$arch});
415*5c2921b0SApple OSS Distributions
416*5c2921b0SApple OSS Distributions				printf $f "#if defined(__${arch}__)\n";
417*5c2921b0SApple OSS Distributions        foreach my $alias_sym (@{$$symbol{aliases}{$arch}}) {
418*5c2921b0SApple OSS Distributions            my $sym = (grep { $_ eq $arch } @{$$symbol{except}}) ? "__".$$symbol{asm_sym} : $$symbol{asm_sym};
419*5c2921b0SApple OSS Distributions
420*5c2921b0SApple OSS Distributions						printf $f "\t.globl\t$alias_sym\n";
421*5c2921b0SApple OSS Distributions						printf $f "\t.set\t$alias_sym, $sym\n";
422*5c2921b0SApple OSS Distributions        }
423*5c2921b0SApple OSS Distributions				printf $f "#endif\n\n";
424*5c2921b0SApple OSS Distributions    }
425*5c2921b0SApple OSS Distributions    if ($$symbol{allow_missing}) {
426*5c2921b0SApple OSS Distributions        printf $f "#endif\n";
427*5c2921b0SApple OSS Distributions    }
428*5c2921b0SApple OSS Distributions}
429*5c2921b0SApple OSS Distributions
430*5c2921b0SApple OSS Distributionsusage() unless scalar(@ARGV) == 5;
431*5c2921b0SApple OSS Distributions$CustomDir = $ARGV[1];
432*5c2921b0SApple OSS Distributionsdie "$MyName: $CustomDir: No such directory\n" unless -d $CustomDir;
433*5c2921b0SApple OSS Distributions$PlatformsDir = $ARGV[2];
434*5c2921b0SApple OSS Distributionsdie "$MyName: $PlatformsDir: No such directory\n" unless -d $PlatformsDir;
435*5c2921b0SApple OSS Distributions$PlatformName = $ARGV[3];
436*5c2921b0SApple OSS Distributionsdie "$MyName: $PlatformsDir/$PlatformName: No such directory\n" unless -d "$PlatformsDir/$PlatformName";
437*5c2921b0SApple OSS Distributions$OutDir = $ARGV[4];
438*5c2921b0SApple OSS Distributionsdie "$MyName: $OutDir: No such directory\n" unless -d $OutDir;
439*5c2921b0SApple OSS Distributions
440*5c2921b0SApple OSS DistributionsreadMaster($ARGV[0]);
441*5c2921b0SApple OSS DistributionscheckForCustomStubs($CustomDir);
442*5c2921b0SApple OSS DistributionsreadAliases($PlatformsDir, $PlatformName);
443*5c2921b0SApple OSS Distributions
444*5c2921b0SApple OSS Distributions##########################################################################
445*5c2921b0SApple OSS Distributions# copy the files specified in @Copy from the $CustomDir to $OutDir
446*5c2921b0SApple OSS Distributions##########################################################################
447*5c2921b0SApple OSS Distributionsfor(@Copy) {
448*5c2921b0SApple OSS Distributions    my $custom = File::Spec->join($CustomDir, $_);
449*5c2921b0SApple OSS Distributions    my $path = File::Spec->join($OutDir, $_);
450*5c2921b0SApple OSS Distributions    print "Copy $custom -> $path\n";
451*5c2921b0SApple OSS Distributions    File::Copy::copy($custom, $path) || die "$MyName: copy($custom, $path): $!\n";
452*5c2921b0SApple OSS Distributions}
453*5c2921b0SApple OSS Distributions
454*5c2921b0SApple OSS Distributions##########################################################################
455*5c2921b0SApple OSS Distributions# make all the *.s files
456*5c2921b0SApple OSS Distributions##########################################################################
457*5c2921b0SApple OSS Distributionsmy @src;
458*5c2921b0SApple OSS Distributionsmy($k, $sym);
459*5c2921b0SApple OSS Distributionswhile (($k, $sym) = each %Symbols)
460*5c2921b0SApple OSS Distributions{
461*5c2921b0SApple OSS Distributions	my $srcname = $$sym{asm_sym} . ".s";
462*5c2921b0SApple OSS Distributions	my $outpath = File::Spec->join($OutDir, $srcname);
463*5c2921b0SApple OSS Distributions
464*5c2921b0SApple OSS Distributions	if ($$sym{is_custom}) {
465*5c2921b0SApple OSS Distributions		my $custom = File::Spec->join($CustomDir, $$sym{is_custom});
466*5c2921b0SApple OSS Distributions		File::Copy::copy($custom, $outpath);
467*5c2921b0SApple OSS Distributions		print "Copied $outpath\n";
468*5c2921b0SApple OSS Distributions
469*5c2921b0SApple OSS Distributions		print "Writing aliases for $srcname\n";
470*5c2921b0SApple OSS Distributions		my $f = IO::File->new($outpath, 'a');
471*5c2921b0SApple OSS Distributions		die "$MyName: $outpath: $!\n" unless defined($f);
472*5c2921b0SApple OSS Distributions		writeAliasesForSymbol($f, $sym);
473*5c2921b0SApple OSS Distributions		undef $f;
474*5c2921b0SApple OSS Distributions	} else {
475*5c2921b0SApple OSS Distributions		my $f = IO::File->new($outpath, 'w');
476*5c2921b0SApple OSS Distributions		die "$MyName: $outpath: $!\n" unless defined($f);
477*5c2921b0SApple OSS Distributions
478*5c2921b0SApple OSS Distributions		printf "Creating $outpath\n";
479*5c2921b0SApple OSS Distributions		writeStubForSymbol($f, $sym);
480*5c2921b0SApple OSS Distributions		writeAliasesForSymbol($f, $sym);
481*5c2921b0SApple OSS Distributions		undef $f;
482*5c2921b0SApple OSS Distributions	}
483*5c2921b0SApple OSS Distributions	push(@src, $srcname);
484*5c2921b0SApple OSS Distributions}
485*5c2921b0SApple OSS Distributions
486*5c2921b0SApple OSS Distributions##########################################################################
487*5c2921b0SApple OSS Distributions# create the Makefile.inc file from the list for files in @src and @CustomSrc
488*5c2921b0SApple OSS Distributions##########################################################################
489*5c2921b0SApple OSS Distributionsmy $path = File::Spec->join($OutDir, 'stubs.list');
490*5c2921b0SApple OSS Distributionsmy $f = IO::File->new($path, 'w');
491*5c2921b0SApple OSS Distributionsmy @sources = sort(@src, @CustomSrc);
492*5c2921b0SApple OSS Distributionsfor my $s (@sources) {
493*5c2921b0SApple OSS Distributions	printf $f File::Spec->join($OutDir, $s) . "\n";
494*5c2921b0SApple OSS Distributions}
495*5c2921b0SApple OSS Distributionsundef $f;
496*5c2921b0SApple OSS Distributionsundef $path;
497*5c2921b0SApple OSS Distributions
498