#!/bin/sh12# SPDX-License-Identifier: BSD-2-Clause3#4# Copyright (c) 2000, Bruce Evans <[email protected]>5# Copyright (c) 2018, Jeff Roberson <[email protected]>6#7# Redistribution and use in source and binary forms, with or without8# modification, are permitted provided that the following conditions9# are met:10# 1. Redistributions of source code must retain the above copyright11# notice, this list of conditions and the following disclaimer.12# 2. Redistributions in binary form must reproduce the above copyright13# notice, this list of conditions and the following disclaimer in the14# documentation and/or other materials provided with the distribution.15#16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26# SUCH DAMAGE.27#2829usage()30{31echo "usage: genoffset [-o outfile] objfile"32exit 133}3435work()36(37local last off x1 x2 x3 struct field type lastoff lasttype asserts3839echo "#ifndef _OFFSET_INC_"40echo "#define _OFFSET_INC_"41echo "#if !defined(GENOFFSET) && (!defined(KLD_MODULE) || defined(KLD_TIED))"42last=43asserts=44while read off x1 x2 struct field type x3; do45off=$(echo "$off" | sed -E 's/^0+//')46if [ "$last" != "$struct" ]; then47if [ -n "$last" ]; then48echo "};"49fi50echo "struct ${struct}_lite {"51last=$struct52printf "%b" "\tu_char\tpad_${field}[${off}];\n"53else54printf "%b" "\tu_char\tpad_${field}[${off} - (${lastoff} + sizeof(${lasttype}))];\n"55fi56printf "%b" "\t${type}\t${field};\n"57lastoff="$off"58lasttype="$type"59asserts="${asserts}_SA(${struct}, ${field}, ${off});\n"60done <<EOT61$(${NM:='nm'} ${NMFLAGS} -t d "$1" | grep __assym_offset__ | sed -e 's/__/ /g' | sort -k 4 -k 1 -n)62EOT63echo "};"64echo "#define _SA(s,f,o) _Static_assert(__builtin_offsetof(struct s ## _lite, f) == o, \\"65printf '\t"struct "#s"_lite field "#f" not at offset "#o)\n'66printf "${asserts}"67echo "#undef _SA"68echo "#endif"69echo "#endif"70)717273#74#MAIN PROGGRAM75#76use_outfile="no"77while getopts "o:" option78do79case "$option" in80o) outfile="$OPTARG"81use_outfile="yes";;82*) usage;;83esac84done85shift $((OPTIND - 1))86case $# in871) ;;88*) usage;;89esac9091if [ "$use_outfile" = "yes" ]92then93work "$1" 3>"$outfile" >&3 3>&-94else95work "$1"96fi979899100