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