16443773dSMatt Macy#!/bin/sh 26443773dSMatt Macy 34d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 46443773dSMatt Macy# 56443773dSMatt Macy# Copyright (c) 2000, Bruce Evans <bde@freebsd.org> 66443773dSMatt Macy# Copyright (c) 2018, Jeff Roberson <jeff@freebsd.org> 76443773dSMatt Macy# 86443773dSMatt Macy# Redistribution and use in source and binary forms, with or without 96443773dSMatt Macy# modification, are permitted provided that the following conditions 106443773dSMatt Macy# are met: 116443773dSMatt Macy# 1. Redistributions of source code must retain the above copyright 126443773dSMatt Macy# notice, this list of conditions and the following disclaimer. 136443773dSMatt Macy# 2. Redistributions in binary form must reproduce the above copyright 146443773dSMatt Macy# notice, this list of conditions and the following disclaimer in the 156443773dSMatt Macy# documentation and/or other materials provided with the distribution. 166443773dSMatt Macy# 176443773dSMatt Macy# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 186443773dSMatt Macy# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 196443773dSMatt Macy# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 206443773dSMatt Macy# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 216443773dSMatt Macy# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 226443773dSMatt Macy# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 236443773dSMatt Macy# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 246443773dSMatt Macy# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 256443773dSMatt Macy# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 266443773dSMatt Macy# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 276443773dSMatt Macy# SUCH DAMAGE. 286443773dSMatt Macy# 296443773dSMatt Macy 306443773dSMatt Macyusage() 316443773dSMatt Macy{ 326443773dSMatt Macy echo "usage: genoffset [-o outfile] objfile" 336443773dSMatt Macy exit 1 346443773dSMatt Macy} 356443773dSMatt Macy 366443773dSMatt Macywork() 37824897a3SWarner Losh( 38accfb4ccSJonathan T. Looney local last off x1 x2 x3 struct field type lastoff lasttype asserts 39824897a3SWarner Losh 406443773dSMatt Macy echo "#ifndef _OFFSET_INC_" 416443773dSMatt Macy echo "#define _OFFSET_INC_" 428bedbb4dSMatt Macy echo "#if !defined(GENOFFSET) && (!defined(KLD_MODULE) || defined(KLD_TIED))" 43824897a3SWarner Losh last= 44accfb4ccSJonathan T. Looney asserts= 45824897a3SWarner Losh while read off x1 x2 struct field type x3; do 46824897a3SWarner Losh off=$(echo "$off" | sed -E 's/^0+//') 47824897a3SWarner Losh if [ "$last" != "$struct" ]; then 48824897a3SWarner Losh if [ -n "$last" ]; then 49824897a3SWarner Losh echo "};" 50824897a3SWarner Losh fi 51824897a3SWarner Losh echo "struct ${struct}_lite {" 52824897a3SWarner Losh last=$struct 53824897a3SWarner Losh printf "%b" "\tu_char\tpad_${field}[${off}];\n" 54824897a3SWarner Losh else 55824897a3SWarner Losh printf "%b" "\tu_char\tpad_${field}[${off} - (${lastoff} + sizeof(${lasttype}))];\n" 56824897a3SWarner Losh fi 57824897a3SWarner Losh printf "%b" "\t${type}\t${field};\n" 58824897a3SWarner Losh lastoff="$off" 59824897a3SWarner Losh lasttype="$type" 60accfb4ccSJonathan T. Looney asserts="${asserts}_SA(${struct}, ${field}, ${off});\n" 61accfb4ccSJonathan T. Looney done <<EOT 62accfb4ccSJonathan T. Looney$(${NM:='nm'} ${NMFLAGS} -t d "$1" | grep __assym_offset__ | sed -e 's/__/ /g' | sort -k 4 -k 1 -n) 63accfb4ccSJonathan T. LooneyEOT 64824897a3SWarner Losh echo "};" 65824897a3SWarner Losh echo "#define _SA(s,f,o) _Static_assert(__builtin_offsetof(struct s ## _lite, f) == o, \\" 66824897a3SWarner Losh printf '\t"struct "#s"_lite field "#f" not at offset "#o)\n' 67*884eeff2SJonathan T. Looney printf "${asserts}" 68824897a3SWarner Losh echo "#undef _SA" 696443773dSMatt Macy echo "#endif" 706443773dSMatt Macy echo "#endif" 71824897a3SWarner Losh) 726443773dSMatt Macy 736443773dSMatt Macy 746443773dSMatt Macy# 756443773dSMatt Macy#MAIN PROGGRAM 766443773dSMatt Macy# 776443773dSMatt Macyuse_outfile="no" 786443773dSMatt Macywhile getopts "o:" option 796443773dSMatt Macydo 806443773dSMatt Macy case "$option" in 816443773dSMatt Macy o) outfile="$OPTARG" 826443773dSMatt Macy use_outfile="yes";; 836443773dSMatt Macy *) usage;; 846443773dSMatt Macy esac 856443773dSMatt Macydone 86824897a3SWarner Loshshift $((OPTIND - 1)) 876443773dSMatt Macycase $# in 886443773dSMatt Macy1) ;; 896443773dSMatt Macy*) usage;; 906443773dSMatt Macyesac 916443773dSMatt Macy 926443773dSMatt Macyif [ "$use_outfile" = "yes" ] 936443773dSMatt Macythen 94824897a3SWarner Losh work "$1" 3>"$outfile" >&3 3>&- 956443773dSMatt Macyelse 96824897a3SWarner Losh work "$1" 976443773dSMatt Macyfi 986443773dSMatt Macy 99