xref: /illumos-gate/usr/src/lib/libc/port/gen/errlist.awk (revision e4e529b2beb8a30e14beb0573c4a114275d157cc)
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 2025 Oxide Computer Company
25#
26# Create two files from a list of input strings.
27#
28# new_list.c:
29#     contains an array of characters indexed into by perror and
30#     strerror and an array of strings for strerrorname_np.
31#     -> _sys_nerrs, _sys_nindex and _sys_err_names
32#
33# errlst.c:
34#     contains an array of pointers to strings for compatibility
35#     with existing user programs that reference it directly;
36#     errlst.c references the strings indirectly using a library private symbol,
37#     __sys_errs[], in order to get relative relocations.
38#     -> _sys_errs, _sys_index, _sys_num_err, sys_errlist[]
39#
40# Since the 64 bit ABI doesn't define the old symbols, the second file is left
41# out of 64 bit libraries.
42
43BEGIN	{
44	FS = "\t"
45
46	# This is the number of entries for the legacy sys_errlist[].
47	# This cannot change as old binaries may reference the copy
48	# relocated sys_errlist, and the data to which it points, directly. New
49	# code should always use perror(), strerror() et al., and that is the
50	# only interface available in the 64-bit library. This is not the only
51	# check to guard against unintended changes, there are also constraints
52	# in the libc mapfile that will cause a link failure if the size
53	# changes.
54	legacynum = 151
55
56	newfile = "new_list.c"
57	oldfile = "errlst.c"
58
59	print "#pragma weak _sys_errlist = sys_errlist" >oldfile
60	print "#pragma weak __sys_errs = _sys_errs" >oldfile
61	print "#include \"lint.h\"\n" >oldfile
62	# We need to include the errors strings proper in the
63	# C source for gettext; the macro C allows us to embed
64	# them as a comment.
65	print "#define\tC(x)\n" >oldfile
66
67	print "#include \"lint.h\"" >newfile
68	print "#include <sys/isa_defs.h>" >newfile
69	print "#include <errno.h>" >newfile
70	print "" >newfile
71}
72
73/^[0-9]+/ {
74	aname[$1] = $2
75	astr[$1] = $3
76	if ($1 > max)
77		max = $1
78}
79
80function genlists(outfile, v_index, v_errs, v_num) {
81	for (j = 0; j <= max; ++j) {
82		if (astr[j] == "")
83			astr[j] = sprintf("Error %d", j)
84	}
85
86	k = 0
87	printf "const int %s[%s] = {\n", v_index, max + 1 >outfile
88	for (j = 0; j <= max; ++j) {
89		printf "\t%d,\n", k >outfile
90		k += length(astr[j]) + 1
91	}
92	print "};\n" >outfile
93
94	print "/* This is one long string */" >outfile
95	printf "const char %s[%d] =\n", v_errs, k >outfile
96	for (j = 0; j <= max; ++j)
97		printf "\t\"%s\\0\"\n", astr[j] >outfile
98	print ";\n" >outfile
99	printf "const int %s = %d;\n\n", v_num, max + 1 >outfile
100}
101
102/^== End of legacy/ {
103	#
104	# Generate the legacy lists that have become part of the ABI
105	# and must not change.
106	#
107
108	# Check that the legacy sys_errlist[] is the correct size.
109	if (max != legacynum) {
110		printf "awk: ERROR! sys_errlist[] != %d entries\n", legacynum
111		printf "Please read comments in"
112		printf " usr/src/lib/libc/port/gen/errlist\n"
113		exit 1
114	}
115
116
117	genlists(oldfile, "_sys_index", "_sys_errs", "_sys_num_err")
118	print "#undef sys_nerr" >oldfile
119	print "#pragma weak _sys_nerr = _sys_num_err" >oldfile
120	print "#pragma weak sys_nerr = _sys_num_err" >oldfile
121
122	k = 0
123	print "const char *sys_errlist[] = {" >oldfile
124	for (j = 0; j <= max; ++j) {
125		printf "\t&_sys_errs[%d], C(\"%s\")\n", k, astr[j] \
126		    >oldfile
127		k += length(astr[j]) + 1
128	}
129	print "};\n" >oldfile
130}
131
132END	{
133	#
134	# Generate the new lists that are used internally by libc and
135	# do not form part of the ABI.
136	#
137
138	genlists(newfile, "_sys_nindex", "_sys_nerrs", "_sys_num_nerr")
139
140	#
141	# This stanza is used to generate the array of names for mapping
142	# an errno to its constant (e.g. "ENOENT").
143	#
144
145	printf "const char *_sys_err_names[%d] = {\n", max + 1 >newfile
146	printf "\t[0] = \"0\",\n" >newfile
147	for (j = 1; j <= max; ++j)
148	{
149		if (aname[j] == "" || aname[j] == "SKIP")
150			continue
151		printf "\t[%s] = \"%s\",\n", aname[j], aname[j] >newfile
152	}
153	print "};\n" > newfile
154}
155