XCOMM!/bin/sh
XCOMM $Xorg: elistgen.ibm,v 1.3 2000/08/17 19:41:52 cpqbld Exp $
XCOMM
XCOMM #########################################################################
XCOMM Construct shared-library export lists for IBM-AIX based on standardized
XCOMM export list description file
XCOMM
XCOMM Usage: elistgen libfoo.sl libfoo.elist > libfoo.lopt
XCOMM
XCOMM	libfoo.a     => shared library of interest
XCOMM	libfoo.elist => Meta description of necessary export list.
XCOMM
XCOMM    The output file, "libfoo.lopt" may then be passed to the IBM linker to 
XCOMM    reconstruct the shared library, libfoo.sl.
XCOMM
XCOMM Author: Yanling Qi 10/03/94, Last updated 10/03/94
XCOMM (c) Copyright 1996 Digital Equipment Corporation.
XCOMM (c) Copyright 1996 Hewlett-Packard Company.
XCOMM (c) Copyright 1994,1996 International Business Machines Corp.
XCOMM (c) Copyright 1996 Sun Microsystems, Inc.
XCOMM (c) Copyright 1996 Novell, Inc. 
XCOMM (c) Copyright 1996 FUJITSU LIMITED.
XCOMM (c) Copyright 1996 Hitachi.
XCOMM
XCOMM #########################################################################

#define HASH #

XCOMM Utility programs
FILTER=CXXFILT			# C++ symbol demangler
AWK=awk				# awk

XCOMM For dump, cat, pr, expand, awk, c++filt
PATH=/usr/bin:/bin:/usr/ucb:/usr/lpp/xlC/bin

XCOMM Temporary files
EXPORTLIST=/tmp/elistgen1.$$	# export directives from "libfoo.list"
NMLIST=/tmp/elistgen2.$$	# name list from libfoo.sl
FILTLIST=/tmp/elistgen3.$$	# demangled (C++) version of above

XCOMM Print useful information
echo "*" `date`
echo "* This linker options list was produced by" $0
echo "* Input export list description taken from:" $2
echo "* Target library:" $1
echo "* Target Operating System:" `uname`
echo "*"

XCOMM Extract the globally visible symbols from target library
dump -g $1 | $AWK  '$1 ~ /[0-9][0-9]/ && $2 !~ /^[\.]/ {print $2}' > $NMLIST

XCOMM Demangle the global library symbols
${FILTER:-cat} $NMLIST > $FILTLIST

XCOMM
XCOMM Clean up the export-list description file.
XCOMM Note that C++ symbols may have embedded spaces in them.
XCOMM
$AWK '
    BEGIN {
	csyms     = 0;		# C   language symbols in libfoo.list
	cplusplus = 0;		# C++ language symbols in libfoo.list
	isyms     = 0;		# C   internal symbols in libfoo.list
	iplusplus = 0;		# C++ internal symbols in libfoo.list
	count     = 0;		# Running count of symbols
	implicit  = "";		# Handling of implicit symbols.
    }
    $1 == "default" {
	HASH A default clause suppresses warnings about implicit symbols.
	if ($2 != "" && $2 != "force" && $2 != "public" && 
	    $2 != "private" && $2 != "internal") {
	    print "# Warning: illegal default clause:", $2 | "cat 1>&2";
	    next;
	}
	if (implicit != "")
	    print "# Warning: multiple default clauses." | "cat 1>&2";
        implicit = $2;
	next;
    }
    $1 == "force" || $1 == "public" || $1 == "private" {
	csyms ++;
	symbol[count++] = $1 ";;" $2;
	next;
    }
    $1 == "publicC++" || $1 == "privateC++" {
	HASH forceC++ is unsupported because we only know how to demangle,
	HASH not how to mangle symbols, and the final ld input file must
	HASH be mangled.
	cplusplus ++;
	string = $2;
	for (n = 3; n <= NF; n++)
	    string = string " " $n;
	symbol[count++] = $1 ";;" string;
	next;
    }
    $1 == "internal" {
	isyms ++;
	symbol[count++] = $1 ";;" $2;
	next;
    }
    $1 == "internalC++" {
	iplusplus ++;
	string = ";;" $2;
	for (n = 3; n <= NF; n++)
	    string = string " " $n;
	symbol[count++] = $1 ";;" string;
	next;
    }
    $1 == "#line" || $1 == "#" {
	HASH cpp will have removed comments, but may have added other stuff.
	next;
    }
    NF > 0 {
	print "# Warning: unrecognized directive:", $0 | "cat 1>&2";
	next;
    }
    END  {
	printf("# Exporting %d C and %d C++ symbols, hiding %d and %d.\n",
	       csyms, cplusplus, isyms, iplusplus) | "cat 1>&2";
	if (implicit != "") {
	    print "# Unspecified symbols are " implicit "." | "cat 1>&2";
	    print "default;;" implicit;
	}
	for (i in symbol)
	    print symbol[i];
    }
' $2 > $EXPORTLIST

XCOMM
XCOMM Read in the above files and write result to stdout.  Use the
XCOMM delimiter ";" to identify the entries in the symbol lookup table.
XCOMM
(pr -m -s";" -t -w1024 $NMLIST $FILTLIST | expand -t 1; cat $EXPORTLIST) | $AWK '
    BEGIN {
	FS = ";";
	implicit = 0;
    }
    NF == 2 {
	HASH This is "pr" output, i.e., symbol translation table
	r2=$2
	gsub(/ /,"",r2);       # Remove spaces because c++filt is unpredictable
	syms[r2] = $1;
	r1=$1;
	gsub(/ /,"",r1);
	mangled[r1] = 1;       # Save the mangling because the export lists
			       HASH sometimes use it instead of a prototype.
	next;
    }
    NF == 3 && $1 == "default" {
	HASH Treatment of unspecified symbols.
	if ($3 == "internal")
	    implicit = 1;
	else if ($3 == "private" || $3 == "privateC++")
	    implicit = 2;
	else if ($3 == "public" || $3 == "publicC++")
	    implicit = 3;
	else # $3 == "force"
	    implicit = 4;
        next;
    }
    NF == 3 {
	HASH Generate canonical demangled form as an alternate symbol.
	alt=$3;
	gsub(/ \.\.\./,",...",alt); # change " ..." to ",..." to match c++filt
	gsub(/ /,"",alt);	    # remove all spaces
	gsub(/\(void\)/,"()",alt);  # change "(void)" to "()" to match c++filt

	HASH Parse our instructions for this symbol.
	if ($1 == "internal" || $1 == "internalC++")
	    export = 1;
	else if ($1 == "private" || $1 == "privateC++")
	    export = 2;
	else if ($1 == "public" || $1 == "publicC++")
	    export = 3;
	else # $1 == "force"
	    export = 4;

	HASH Process it.
	if ((length(syms[alt]) > 0) || mangled[alt]) {
	    HASH This symbol is present in the library.
	    if (donelist[alt])
		print "# Warning: Duplicate entry for", $3, 
		    "in export list" | "cat 1>&2";
	    if (donelist[alt] < export) {
		if (export > 1)
		    print syms[alt];
		donelist[alt] = export;
	    }
	} else { 
	    HASH Print forced-export symbols without complaining.
	    if (export == 4) {
		print alt;
		donelist[alt] = export;
	    } else {
		print "# Warning:", $3,
		    "was not in the nm list for this library" | "cat 1>&2";
	    }
	}

	next;
    }
    END { 
	HASH Process implicit symbols.
	for (i in syms) {
	    if ((! donelist[i]) && (length(syms[i]) > 0)) {
		HASH Do not complain about C++ virtual function tables.
		if (implicit == 0 && syms[i] !~ /^__vft[0-9]*_/)
		    print "# Warning:", syms[i],
			"was not in the export list" | "cat 1>&2";
		if (implicit > 1)
		    print syms[i];
	    }
	}
    }
'

XCOMM Clean up temporary files
rm $EXPORTLIST
rm $NMLIST
rm $FILTLIST