#!/bin/sh
#
# makeSymTbl - create a VxWorks symbol table struct
#
# modification history
# --------------------
# 01e,06jun88,dnw  Changed special case symCreate to symTblCreate.
# 01d,17nov87,gae  Now handles HP "nm" ouput. Added -n flag. Fixed include's.
# 01c,15mov87,dnw  Changed to create standAloneSymTbl instead of sysSymTbl.
# 01b,05nov87,jlf  Documentation update, mod history added
#
# SYNOPSIS: makeSymTbl [-n nnnn] objMod
#
# DESCRIPTION
# This tool writes to standard out the source code for a symbol table
# structure which contains the names, addresses,
# and types of all global symbols in the specified object module.
# This symbol table can be linked into a bootable VxWorks system.
# This is needed only when creating a standalone system that has 
# no other connection to the outside world after booting.
# Normally, this is NOT needed because the symbol table is constructed
# by reading and interpreting the symbol table contained in the system
# load module (a.out format), either from the local boot disk or from
# a host system over the network.
#
# The symbol table created by makeSymTbl is accessable through the
# global variable 'standAloneSymTbl' which is of type SYMTAB_ID.
# This is the only external declaration in the module created by
# makeSymTbl.
#
# The -n flag specifies the number of additional symbol table slots
# to be reserved in the array.  The default is 100.
#
# To see an example, look at the file vw/config/<cpu>/symTbl.c, which
# is generated by this tool for UniWorks.st in the same directory.
#*/

tmp=/tmp/mst$$
trap "rm -f $tmp; exit" 1 2 3 15

# check for "-n" flag

if (test x$1 = x-n -a $# -ge 2) then
    addsym=$2
    shift;shift
else
    addsym=100
fi

# check for correct number of arguments

if (test $# -ne 1) then
    echo "usage: makeSymTbl [-n nnnn] objMod"
    exit
fi

# output initial part of symbol table file

echo "/* stand-alone system symbol table */

/* CREATED BY makeSymTbl
 *       FROM $1
 *         ON `date`
 */
"'
#include "UniWorks.h"
#include "sysSymTbl.h"
#include "a.out.h"

'
# get list of symbols to be included in symbol table:
#    all global symbols in object module except those that are
#	undefined
#	don't start with '_'
#	_etext, _edata, _end

nm -g $1 | tr -s ' ' ' ' | sed -e '
  /^.* U/d
  /^.* . [^_]/d
  /^.* . _float$/d
  /^.* . _etext$/d
  /^.* . _edata$/d
  /^.* . _end$/d' >$tmp

# convert nm output to IMPORT list;
# some symbols that are declared in symLib.h, which is included in resulting
# symTbl.c, must be declared correctly or they cause mulitple definition
# complaints.

sed -e '
  s/^.* T _symTblCreate/IMPORT SYMTAB_ID symTblCreate ();/
  s/^.* . _sysSymTbl/IMPORT SYMTAB_ID sysSymTbl;/
  s/^.* . _standAloneSymTbl/IMPORT SYMTAB_ID standAloneSymTbl;/
  s/^.* T _\(.*\)/IMPORT \1 ();/
  s/^.* [ABCD] _\(.*\)/IMPORT int \1;/
  ' $tmp

nsyms=`wc -l $tmp | awk '{print $1}'`
maxsyms=`expr $nsyms + $addsym`

echo "
LOCAL SYMBOL symbols [$maxsyms] =
    {"

# convert nm output to symbol entries in array

sed -e '
  s/^.* T _\(.*\)/    {"_\1", (char*) \1,   N_EXT | N_TEXT},/
  s/^.* D _\(.*\)/    {"_\1", (char*) \&\1, N_EXT | N_DATA},/
  s/^.* [BC] _\(.*\)/    {"_\1", (char*) \&\1, N_EXT | N_BSS},/
  s/^.* A _\(.*\)/    {"_\1", (char*) \&\1, N_EXT | N_ABS},/
  ' $tmp

# output end of symbol table file

echo "    };

LOCAL SYMTAB symTbl =
    {
    $nsyms,		/* current number of symbols in table */
    $maxsyms,		/* maximum number of symbols in table */
    MAX_SYS_SYM_LEN,	/* maximum symbol length */
    symbols,		/* ptr to symbol array */
    };

SYMTAB_ID standAloneSymTbl = &symTbl;
"

rm $tmp
exit
