FreeBSD/Linux Kernel Cross Reference
sys/kern/genoffset.sh
1 #!/bin/sh
2
3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 #
5 # Copyright (c) 2000, Bruce Evans <bde@freebsd.org>
6 # Copyright (c) 2018, Jeff Roberson <jeff@freebsd.org>
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
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.
16 #
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
27 # SUCH DAMAGE.
28 #
29 # $FreeBSD$
30
31 usage()
32 {
33 echo "usage: genoffset [-o outfile] objfile"
34 exit 1
35 }
36
37 work()
38 (
39 local last off x1 x2 x3 struct field type lastoff lasttype
40
41 echo "#ifndef _OFFSET_INC_"
42 echo "#define _OFFSET_INC_"
43 echo "#if !defined(GENOFFSET) && (!defined(KLD_MODULE) || defined(KLD_TIED))"
44 last=
45 temp=$(mktemp -d genoffset.XXXXXXXXXX)
46 trap "rm -rf ${temp}" EXIT
47 # Note: we need to print symbol values in decimal so the numeric sort works
48 ${NM:='nm'} ${NMFLAGS} -t d "$1" | grep __assym_offset__ | sed -e 's/__/ /g' | sort -k 4 -k 1 -n |
49 while read off x1 x2 struct field type x3; do
50 off=$(echo "$off" | sed -E 's/^0+//')
51 if [ "$last" != "$struct" ]; then
52 if [ -n "$last" ]; then
53 echo "};"
54 fi
55 echo "struct ${struct}_lite {"
56 last=$struct
57 printf "%b" "\tu_char\tpad_${field}[${off}];\n"
58 else
59 printf "%b" "\tu_char\tpad_${field}[${off} - (${lastoff} + sizeof(${lasttype}))];\n"
60 fi
61 printf "%b" "\t${type}\t${field};\n"
62 lastoff="$off"
63 lasttype="$type"
64 echo "_SA(${struct}, ${field}, ${off});" >> "$temp/asserts"
65 done
66 echo "};"
67 echo "#define _SA(s,f,o) _Static_assert(__builtin_offsetof(struct s ## _lite, f) == o, \\"
68 printf '\t"struct "#s"_lite field "#f" not at offset "#o)\n'
69 cat "$temp/asserts"
70 echo "#undef _SA"
71 echo "#endif"
72 echo "#endif"
73 )
74
75
76 #
77 #MAIN PROGGRAM
78 #
79 use_outfile="no"
80 while getopts "o:" option
81 do
82 case "$option" in
83 o) outfile="$OPTARG"
84 use_outfile="yes";;
85 *) usage;;
86 esac
87 done
88 shift $((OPTIND - 1))
89 case $# in
90 1) ;;
91 *) usage;;
92 esac
93
94 if [ "$use_outfile" = "yes" ]
95 then
96 work "$1" 3>"$outfile" >&3 3>&-
97 else
98 work "$1"
99 fi
100
Cache object: 50e3319bd86fa762867e128b2130a6b6
|