#! /bin/sh
# This script collects global constructor and destructor names from
# a shared library image file and builds an assembler file that
# contains the needed .ctors and .dtors sections for a library client.
# The library image is the first argument, the assembler output file,
# the second.
# Assembling and loading this file with an inlib directive for the shared
# image gives a searchable library that automatically loads the shared one.

if [ -r "$1"   -a   "x$2" != "x" ]; then
  tmpfile=/tmp/collect.tmp.$$
  nm -n -g $1 > $tmpfile
  echo '.section .ctors,"w"' > $2
  awk -e '/_GLOBAL_\$I\$/{print " .long", $3}' $tmpfile >> $2
  echo '.section .dtors,"w"' >> $2
  awk -e '/_GLOBAL_\$D\$/{print " .long", $3}' $tmpfile >> $2
  rm -f $tmpfile
else
  echo Usage:  $0 libraryfile asm_outfile
fi
