xref: /illumos-gate/usr/src/lib/libc/port/gen/errlist.awk (revision faadcf7e)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
22# Use is subject to license terms.
23#
24# Copyright 2024 Oxide Computer Company
25#
26# Create two files from a list of input strings;
27# new_list.c contains an array of characters indexed into by perror and
28# strerror and an array of strings for strerrorname_np;
29# errlst.c contains an array of pointers to strings for compatibility
30# with existing user programs that reference it directly;
31# errlst.c references the strings in new_list.c indirectly using a library
32# private symbol, __sys_errs[], in order to get relative relocations.
33#
34# Since the 64 bit ABI doesn't define the old symbols, the second file
35# should be left out 64 bit libraries.
36#
37# WARNING!
38#        Do NOT add entries to this list such that it grows the list
39#        beyond the last entry:
40#              151     Stale NFS file handle
41#        Growing this list may damage programs because this array is
42#        copied into a reserved array at runtime.  See bug 4097669.
43#
44#        If you need to add an entry please use one of the empty
45#        slots.
46#        The arrays _sys_errs[], accessible via perror(3C) and strerror(3C)
47#        interfaces, and sys_errlist[] are created from this list.
48#        It is the direct referencing of sys_errlist[] that is the problem.
49#        Your code should only use perror() or strerror().
50
51
52BEGIN	{
53		FS = "\t"
54		hi = 0
55
56		newfile = "new_list.c"
57		oldfile = "errlst.c"
58
59		print "#pragma weak _sys_errlist = sys_errlist\n" >oldfile
60		print "#include \"lint.h\"\n" >oldfile
61		# We need to include the errors strings proper in the
62		# C source for gettext; the macro C allows us to embed
63		# them as comment.
64		print "#define\tC(x)\n" >oldfile
65		print "extern const char __sys_errs[];\n" >oldfile
66		print "const char *sys_errlist[] = {" >oldfile
67
68		print "#include \"lint.h\"" >newfile
69		print "#include <sys/isa_defs.h>\n" >newfile
70		print "#include <errno.h>\n" >newfile
71		print "#pragma weak __sys_errs = _sys_errs\n" >newfile
72	}
73
74/^[0-9]+/ {
75		if ($1 > hi)
76			hi = $1
77		aname[$1] = $2
78		astr[$1] = $3
79	}
80
81END	{
82		print "const int _sys_index[] =\n{" >newfile
83		k = 0
84		mx = 151	# max number of entries for sys_errlist[]
85		if (hi > mx)
86		{
87			printf "awk: ERROR! sys_errlist[] > %d entries\n", mx
88			printf "Please read comments in"
89			printf " usr/src/lib/libc/port/gen/errlist\n"
90			exit 1
91		}
92		for (j = 0; j <= hi; ++j)
93		{
94			if (astr[j] == "")
95				astr[j] = sprintf("Error %d", j)
96			printf "\t%d,\n", k >newfile
97			printf "\t&__sys_errs[%d], C(\"%s\")\n", k, astr[j] \
98				>oldfile
99			k += length(astr[j]) + 1
100		}
101		print "};\n" >newfile
102
103		print "/* This is one long string */" >newfile
104		printf "const char _sys_errs[%d] =\n", k >newfile
105		for (j = 0; j <= hi; ++j)
106		{
107			printf "\t\"%s\\0\"\n", astr[j] >newfile
108		}
109		print ";\n" >newfile
110		print "};\n" >oldfile
111
112		#
113		# This stanza is used to generate the array of names for mapping
114		# an errno to its constant (e.g. "ENOENT").
115		#
116		printf "const char *_sys_err_names[%d] = {\n", hi + 1 >newfile
117		printf "\t[0] = \"0\",\n" >newfile
118		for (j = 1; j <= hi; ++j)
119		{
120			if (aname[j] != "" && aname[j] != "SKIP")
121				printf "\t[%s] = \"%s\",\n", aname[j], aname[j] \
122				>newfile
123		}
124		print "};\n" > newfile
125
126		print "const int _sys_num_err = " hi + 1 ";\n" >newfile
127		print "#undef sys_nerr" >newfile
128		print "#ifndef _LP64" >newfile
129		print "#pragma weak _sys_nerr = _sys_num_err" >newfile
130		print "#pragma weak sys_nerr = _sys_num_err" >newfile
131		print "#endif /* _LP64 */" >newfile
132	}
133