#!/bin/sh
# ############################################################################
# (c) Copyright Virata Limited 1998
#
# Virata Limited Confidential and Proprietary
#
# The following software shell script ("Software") is strictly confidential and
# is proprietary to Virata Limited ("Virata").  It may only be read, used,
# copied, adapted, modified or otherwise dealt with by you if you have
# entered into a confidentiality agreement with Virata and then subject to the
# terms of that confidentiality agreement and any other applicable agreement
# between you and Virata.  If you are in any doubt as to whether you are
# entitled to access, read, use, copy, adapt, modify or otherwise deal with
# the Software or whether you are entitled to disclose the Software to any
# other person you should contact Virata.  If you have not entered into a
# confidentiality agreement with Virata granting access to this Software you
# should forthwith return all media, copies and printed listings containing
# the Software to Virata.
#
# Virata reserves the right to take legal action against you should you breach
# the above provisions.
#
# If you are unsure, or to report violations, please contact
# support@virata.com
# ############################################################################

# Author:   A L Jackson
# Date:     08 August 1996
# Version:  001
#
# Form constructor, or destructor, list for C++ objects given a list
# of C++ object files.
#
# Revision History:
#  001 08/08/96 ALJ  Created
#

#set version
CCFIX_VER="1.0"

# Validate arguments
if [ $# -lt 2 ]
then
        echo ccfix version $CCFIX_VER
	cat <<EOF
 Usage: ccfix [D|I] {objectlist}
Form constructor, or destructor, list for C++ objects given a list
of C++ object files.
EOF
	exit	1
fi

if [ $1 != I -a $1 != D ]
then
	echo "ccfix: expected I or D as first argument"
	exit	1
fi

#
# Get internal names of function tables, etc.
#
if [ $1 = I ]
then
FNAME=CTOR
else
FNAME=DTOR
fi
FTYPE=$1

#
# Process object files, then produce a small, stand-alone assembler file.
#
shift
{
while [ $# -gt 0 ]
do
	nm $1 | grep " T " | grep _GLOBAL_.${FTYPE} | sed 's/.* _/_/'
	shift
done
} | awk  '
BEGIN	{	nElements = 0; }
	{
		Symbols [nElements++] = $0;
	}
END		{
	printf (".data\n");
	printf ("\t.align\t0\n");
	printf ("\t.global\t__%s_LIST__\n", FNAME);
	printf ("__%s_LIST__:\n", FNAME);

	printf ("\t.long\t%d\n", nElements);
	for (i = 0; i < nElements; i++)
	{
		printf ("\t.long\t%s\n", Symbols [i]);
	}
	printf ("\t.long\t0\n");
	} ' FNAME=$FNAME

#
# End....
exit 0
