3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2000, Bruce Evans <bde@freebsd.org>
6 # Copyright (c) 2018, Jeff Roberson <jeff@freebsd.org>
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 echo "usage: genoffset [-o outfile] objfile"
40 echo "#ifndef _OFFSET_INC_"
41 echo "#define _OFFSET_INC_"
42 echo "#if !defined(GENOFFSET) && (!defined(KLD_MODULE) || defined(KLD_TIED))"
43 ${NM:='nm'} ${NMFLAGS} "$1" | ${AWK:='awk'} '
45 type = substr($3, match($3, "_datatype_") + length("_datatype_"))
47 / C .*_parenttype_*/ {
48 parent = substr($3, match($3, "_parenttype_") + length("_parenttype_"))
51 sign = substr($1, length($1) - 3, 4)
57 w0 = substr($1, length($1) - 3, 4)
60 w1 = substr($1, length($1) - 3, 4)
63 w2 = substr($1, length($1) - 3, 4)
66 w3 = substr($1, length($1) - 3, 4)
76 # This still has minor problems representing INT_MIN, etc.
78 # with 32-bit 2''s complement ints, this prints -0x80000000,
79 # which has the wrong type (unsigned int).
80 offset = sprintf("%s%s%s", sign, hex, w)
82 structures[parent] = sprintf("%s%s %s %s\n",
83 structures[parent], offset, type, member)
86 for (struct in structures) {
87 printf("struct %s_lite {\n", struct);
88 n = split(structures[struct], members, "\n")
89 for (i = 1; i < n; i++) {
90 for (j = i + 1; j < n; j++) {
91 split(members[i], ivar, " ")
92 split(members[j], jvar, " ")
93 if (jvar[1] < ivar[1]) {
95 members[i] = members[j]
101 for (i = 1; i < n; i++) {
102 split(members[i], m, " ")
103 printf "\tu_char\tpad_%s[%s - %s];\n", m[3], m[1], off
104 printf "\t%s\t%s;\n", m[2], m[3]
105 off = sprintf("(%s + sizeof(%s))", m[1], m[2])
121 while getopts "o:" option
129 shift $(($OPTIND - 1))
135 if [ "$use_outfile" = "yes" ]
137 work $1 3>"$outfile" >&3 3>&-