]>
git.saurik.com Git - apple/xnu.git/blob - libsyscall/xcodescripts/create-syscalls.pl
3 # Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
5 # @APPLE_OSREFERENCE_LICENSE_HEADER_START@
7 # This file contains Original Code and/or Modifications of Original Code
8 # as defined in and that are subject to the Apple Public Source License
9 # Version 2.0 (the 'License'). You may not use this file except in
10 # compliance with the License. Please obtain a copy of the License at
11 # http://www.opensource.apple.com/apsl/ and read it before using this
14 # The Original Code and all software distributed under the License are
15 # distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 # EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 # INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 # Please see the License for the specific language governing rights and
20 # limitations under the License.
22 # @APPLE_OSREFERENCE_LICENSE_HEADER_END@
24 ##########################################################################
26 # % create-syscalls.pl syscalls.master custom-directory out-directory
28 # This script fills the the out-directory with a Makefile.inc and *.s
29 # files to create the double-underbar syscall stubs. It reads the
30 # syscall.master file to get the symbol names and number of arguments,
31 # and whether Libsystem should automatically create the (non-double-underbar)
32 # stubs if Libc doesn't provide a wrapper. Which system calls will get
33 # the automatic treatment is writen to the libsyscall.list file, also
34 # written to the out-directory.
36 # The custom-directory contains:
37 # 1. SYS.h - used by the automatically created *.s and custom files
38 # 2. custom.s - contains architecture specific additional system calls and
39 # auxilliary routines (like cerror)
40 # 3. special case double-underbar stub files - which are copied into
43 ##########################################################################
46 use File
::Basename
();
51 my $MyName = File
::Basename
::basename
($0);
53 my @CustomSrc = qw(custom.s);
55 my @Architectures = split /\s/, $ENV{"ARCHS"};
56 my @Copy = (qw(SYS.h), @CustomSrc);
61 # size in bytes of known types (only used for i386)
73 'mach_port_name_t' => 4,
94 # Moving towards storing all data in this hash, then we always know
95 # if data is aliased or not, or promoted or not.
109 syscall => "setquota",
110 asm_sym
=> "_setquota",
119 syscall => "syscall",
120 asm_sym
=> "_syscall",
130 die "Usage: $MyName syscalls.master custom-directory platforms-directory out-directory\n";
133 ##########################################################################
134 # Read the syscall.master file and collect the system call names and number
135 # of arguments. It looks for the NO_SYSCALL_STUB quailifier following the
136 # prototype to determine if no automatic stub should be created by Libsystem.
137 # System call name that are already prefixed with double-underbar are set as
138 # if the NO_SYSCALL_STUB qualifier were specified (whether it is or not).
140 # For the #if lines in syscall.master, all macros are assumed to be defined,
141 # except COMPAT_GETFSSTAT (assumed undefined).
142 ##########################################################################
146 my $f = IO
::File-
>new($file, 'r');
147 die "$MyName: $file: $!\n" unless defined($f);
161 if(/^#\s*if\s+(\S+)$/) {
162 $skip = ($1 eq 'COMPAT_GETFSSTAT') ? -1 : 1;
169 die "$MyName: no function prototype on line $line\n" unless length($_) > 0 && /;$/;
170 my $no_syscall_stub = /\)\s*NO_SYSCALL_STUB\s*;/;
171 my($name, $args) = /\s(\S+)\s*\(([^)]*)\)/;
172 next if $name =~ /e?nosys/;
177 if($args ne '' && $args ne 'void') {
178 my @a = split(',', $args);
180 # Calculate the size of all the arguments (only used for i386)
182 $type =~ s/\s*\w+$//; # remove the argument name
184 $argbytes += 4; # a pointer type
186 $type =~ s/^.*\s//; # remove any type qualifier, like unsigned
187 my $b = $TypeBytes{$type};
188 die "$MyName: $name: unknown type '$type'\n" unless defined($b);
196 asm_sym
=> $no_syscall_stub ? "___$name" : "_$name",
197 is_private
=> $no_syscall_stub,
207 sub checkForCustomStubs
{
210 my ($c_sym_name, $sym);
211 while (($c_sym_name, $sym) = each %Symbols) {
212 my $source = "__".$$sym{c_sym
}.".s";
213 my $custom = File
::Spec-
>join($dir, $source);
214 next unless -f
$custom;
216 $$sym{is_custom
} = $source;
217 if (!$$sym{is_private
}) {
218 foreach my $subarch (@Architectures) {
219 (my $arch = $subarch) =~ s/arm(.*)/arm/;
220 $$sym{aliases
}{$arch} = [] unless $$sym{aliases
}{$arch};
221 push(@{$$sym{aliases
}{$arch}}, $$sym{asm_sym
});
223 $$sym{asm_sym
} = "__".$$sym{asm_sym
};
224 $$sym{is_private
} = 1;
230 my ($platformDir, $platformName) = @_;
231 my $genericMap = File
::Spec-
>join($platformDir, "syscall.map");
234 foreach my $k (keys %Symbols) {
235 $sym_to_c{$Symbols{$k}{asm_sym
}} = $k;
239 for my $arch (@Architectures) {
240 (my $new_arch = $arch) =~ s/arm(.*)/arm/g;
241 push(@a, $new_arch) unless grep { $_ eq $new_arch } @a;
244 foreach my $arch (@a) {
245 my $syscallFile = File
::Spec-
>join($platformDir, $platformName, $arch, "syscall.map");
248 push(@files, IO
::File-
>new($syscallFile, 'r'));
249 die "$MyName: $syscallFile: $!\n" unless defined($files[$#files]);
250 push(@files, IO
::File-
>new($genericMap, 'r'));
251 die "$MyName: $genericMap: $!\n" unless defined($files[$#files]);
253 foreach my $f (@files) {
258 my ($alias, $target_symbol) = split;
259 if (defined($target_symbol)) {
260 foreach my $sym (values %Symbols) {
261 # I've eliminated most of the ugly from this script except
262 # the need to try stripping underbars here.
263 if ($$sym{is_private
}) {
264 next unless $$sym{asm_sym
} eq $target_symbol;
266 (my $target = $target_symbol) =~ s/^__//;
267 next unless ($$sym{asm_sym
} eq $target || $$sym{asm_sym
} eq $target_symbol);
269 $$sym{aliases
}{$arch} = [] unless $$sym{aliases
}{$arch};
271 die "$MyName: $arch $$sym{asm_sym} -> $alias: Duplicate alias.\n" if grep { $_ eq $alias } @{$$sym{aliases
}{$arch}};
272 push(@{$$sym{aliases
}{$arch}}, $alias);
274 # last thing to do, if we aliased over a first class symbol, we need
276 my $c = $sym_to_c{$alias};
278 push(@{$Symbols{$c}{except
}}, $arch);
287 ##########################################################################
288 # Make a __xxx.s file: if it exists in the $CustomDir, just copy it, otherwise
289 # create one. We define the macro __SYSCALL_32BIT_ARG_BYTES so that SYS.h could
290 # use that to define __SYSCALL dependent on the arguments' total size.
291 ##########################################################################
292 sub writeStubForSymbol
{
293 my ($f, $symbol) = @_;
296 for my $subarch (@Architectures) {
297 (my $arch = $subarch) =~ s/arm(.*)/arm/;
298 push(@conditions, "defined(__${arch}__)") unless grep { $_ eq $arch } @{$$symbol{except
}};
301 print $f "#define __SYSCALL_32BIT_ARG_BYTES $$symbol{bytes}\n";
302 print $f "#include \"SYS.h\"\n\n";
303 if (scalar(@conditions)) {
304 printf $f "#if " . join(" || ", @conditions) . "\n";
305 printf $f "__SYSCALL(%s, %s, %d)\n", $$symbol{asm_sym
}, $$symbol{syscall}, $$symbol{nargs
};
306 if (!$$symbol{is_private
} && (scalar(@conditions) < scalar(@Architectures))) {
308 printf $f "__SYSCALL(%s, %s, %d)\n", "__".$$symbol{asm_sym
}, $$symbol{syscall}, $$symbol{nargs
};
310 printf $f "#endif\n\n";
312 # actually this isnt an inconsistency. kernel can expose what it wants but if all our arches
313 # override it we need to honour that.
317 sub writeAliasesForSymbol
{
318 my ($f, $symbol) = @_;
320 foreach my $subarch (@Architectures) {
321 (my $arch = $subarch) =~ s/arm(.*)/arm/;
323 next unless scalar($$symbol{aliases
}{$arch});
325 printf $f "#if defined(__${arch}__)\n";
326 foreach my $alias_sym (@{$$symbol{aliases
}{$arch}}) {
327 my $sym = (grep { $_ eq $arch } @{$$symbol{except
}}) ? "__".$$symbol{asm_sym
} : $$symbol{asm_sym
};
329 printf $f "\t.globl\t$alias_sym\n";
330 printf $f "\t.set\t$alias_sym, $sym\n";
332 printf $f "#endif\n\n";
336 usage
() unless scalar(@ARGV) == 5;
337 $CustomDir = $ARGV[1];
338 die "$MyName: $CustomDir: No such directory\n" unless -d
$CustomDir;
339 $PlatformsDir = $ARGV[2];
340 die "$MyName: $PlatformsDir: No such directory\n" unless -d
$PlatformsDir;
341 $PlatformName = $ARGV[3];
342 die "$MyName: $PlatformsDir/$PlatformName: No such directory\n" unless -d
"$PlatformsDir/$PlatformName";
344 die "$MyName: $OutDir: No such directory\n" unless -d
$OutDir;
346 readMaster
($ARGV[0]);
347 checkForCustomStubs
($CustomDir);
348 readAliases
($PlatformsDir, $PlatformName);
350 ##########################################################################
351 # copy the files specified in @Copy from the $CustomDir to $OutDir
352 ##########################################################################
354 my $custom = File
::Spec-
>join($CustomDir, $_);
355 my $path = File
::Spec-
>join($OutDir, $_);
356 print "Copy $custom -> $path\n";
357 File
::Copy
::copy
($custom, $path) || die "$MyName: copy($custom, $path): $!\n";
360 ##########################################################################
361 # make all the *.s files
362 ##########################################################################
365 while (($k, $sym) = each %Symbols)
367 my $srcname = $$sym{asm_sym
} . ".s";
368 my $outpath = File
::Spec-
>join($OutDir, $srcname);
370 if ($$sym{is_custom
}) {
371 my $custom = File
::Spec-
>join($CustomDir, $$sym{is_custom
});
372 File
::Copy
::copy
($custom, $outpath);
373 print "Copied $outpath\n";
375 print "Writing aliases for $srcname\n";
376 my $f = IO
::File-
>new($outpath, 'a');
377 die "$MyName: $outpath: $!\n" unless defined($f);
378 writeAliasesForSymbol
($f, $sym);
381 my $f = IO
::File-
>new($outpath, 'w');
382 die "$MyName: $outpath: $!\n" unless defined($f);
384 printf "Creating $outpath\n";
385 writeStubForSymbol
($f, $sym);
386 writeAliasesForSymbol
($f, $sym);
389 push(@src, $srcname);
392 ##########################################################################
393 # create the Makefile.inc file from the list for files in @src and @CustomSrc
394 ##########################################################################
395 my $path = File
::Spec-
>join($OutDir, 'stubs.list');
396 my $f = IO
::File-
>new($path, 'w');
397 my @sources = sort(@src, @CustomSrc);
398 for my $s (@sources) {
399 printf $f File
::Spec-
>join($OutDir, $s) . "\n";