#!/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="2.0"

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

if [ "$1" = "-a" ]; then
    asm_export=AE=1
    shift
else
    asm_export=AE=0
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
	isos-nm -g $1 | grep "_GLOBAL_[_\.]${FTYPE}" | sed 's/.* _/_/'
	shift
done
} | awk -v $asm_export '
BEGIN	{	nElements = 0; }
	{
		Symbols [nElements++] = $0;
	}
END		{

        printf ("#include \"config.s\"\n");
	printf ("\n.data\n");
	printf ("\t.align\t0\n");

        if ( AE ) {
	    printf ("\t.global\tASM_EXPORT(__%s_LIST__)\n", FNAME);
            printf ("ASM_EXPORT(__%s_LIST__):\n", FNAME);
        } else {
	    printf ("\t.global\t___%s_LIST__\n", FNAME);
            printf ("___%s_LIST__:\n", FNAME);
        }

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

#
# End....
exit 0
