#!/bin/ksh

# Copyright (C) 2000 Free Software Foundation, Inc.
# This file is part of the GNU C Library.

# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.

# The GNU C Library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.

# You should have received a copy of the GNU Library General Public
# License along with the GNU C Library; see the file COPYING.LIB.  If
# not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

if [ $# -lt 4 ]
then
   echo "usage: picdll <source archive> <member name> <newname> <names...>"
   exit 1
fi

#
# There are two ways to do this; this one works for 2.8*, and there's
# another one that works for newer (gcc 3.1+).  Use the old one if
# we are in that world.
#
if [ "$($X/local_bin/ld -v)" = "GNU ld version cygnus-2.8.1 (with BFD cygnus-2.8.1)" ]
then
   echo "Using the old picdll solution"
   exec i386/picdll.old $@
fi

echo "Using the new picdll solution"

ARCHIVE=$1
MEMBER=$2
RESULT=$3

shift 3
NAMES=$*

# Make in $TMPDIR and remove, someday
WORKDIR=workdir

PERLFIND='
$target=$ARGV[0];
shift;
$slot = 0;
while(<>) {
   if (/^.*:/) {
       $slot++;
   }
   if (/$target/) {
       printf "%d\n", $slot;
       exit 0;
   }
}'

PERLEXT='
<>; <>; <>; <>; 
$s=<>;
$a=substr($s,6,2);
$b=substr($s,8,2);
print "0x$b$a\n"; '

# We rely on the latest and greatest, so
AR=$X/local_bin/ar
AS=$X/local_bin/as
OBJDUMP=$X/local_bin/objdump

echo $NAMES

mkdir -p ${WORKDIR} ${WORKDIR}
rm -f ${WORKDIR}/*
rm -f ${WORKDIR}/*

DLL_DESC=$(nm $ARCHIVE | head -n 20 | grep __IMPORT_DESCR | head -n 1 | cut -d" " -f3)
echo $DLL_DESC

for i in $NAMES
do
    slot=$(nm $ARCHIVE | perl -e "$PERLFIND" "_${i}")
    rm -f $MEMBER
    ${AR} -xN $slot $ARCHIVE $MEMBER
    chmod 755 $MEMBER

    ordinal=$($OBJDUMP --full-contents --section=.idata\$6 $MEMBER | perl -e "$PERLEXT")
    ordinal=$(printf "%d" $ordinal)

    echo extracting $i slot $slot ordinal $ordinal

    searchtag=${i%%@*}

    cat <<-EOF >t.s
	 .text
	 .globl __imp__${i}
	 .globl _${i}
	 .globl ${DLL_DESC}
	_${i}:
	 jmp *__imp__${i}@GOTOFF(%ebx)
	 .linkonce one_only

	 .section .idata\$5,"d"
	 .linkonce one_only
	__imp__${i}:
	 .rva L${ordinal}

	 .section .idata\$4,"d"
	 # .linkonce discard -- should be associative
	 .rva L${ordinal}

	 .section .idata\$6,"d"
	L${ordinal}: .short ${ordinal}
	 .string "${searchtag}"
	 # .linkonce discard -- should be associative
EOF

    $AS t.s -o ${WORKDIR}/2${slot}${i}.o
done

# We can "almost" use the remaining chunks of the library, but not quite.
# Reason: ld sorts the .idata sections (for dlltool archives) and
#         doesn't "float" the .idata$4 and .idata$5 symbols to the
#         top.  Making ld (or bfd) do that is non-obvious (to me
#         anyway).  It has some special magic applicable to lib.exe-
#         generated .lib files, but that doesn't work because it relies
#         on all the member names being the same (which ar can't do, yet,
#         anyway).  So, we build a initial chunk that's more like what 
#         dlltool generates, and that works for us.  Ugly, but effective.
#
#         The initial chunk contains .idata$2 and the "beginnings" of
#         .idata$4 and .idata$6, and the first part of .idata$6.  To
#         put the end of the .idata$4 and .idata$5 sections in place,
#         (trailing 0s), it needs to load the last archive member, which
#         lib.exe does by using a special symbol contiaining 0x7f as the
#         first character.  (Don't ask me why.)  Since we can't get that
#         character past the assembler, we can't use the "last" chunk.
#         We have to build that for ourselves as well.  The chunk containing
#         idata$3 (the flag for end of the import table) isn't needed with 
#         gnu ld, because the linker already provides it.
#                 
#         But we do have to be sure that the members go into the archive
#         in the right order, thus the silly names.

DLLNAME=${MEMBER%.dll}          # get the DLL base name
DLLNAME=${DLLNAME##*/}          # ditch any pathname (probably not there)

cat - >t.s <<EOF
	.globl ${DLL_DESC}
        .globl ${DLL_DESC}_END

${DLL_DESC}:
	.section .idata\$2
	.rva hname
	.long 0
	.long 0
	.rva dllname
	.rva fthunk

	.section .idata\$5
	.align 0
fthunk:

	.section .idata\$4
	.align 0
hname:

	.section .idata\$6
dllname:
	.string "$DLLNAME.dll"
EOF

$AS t.s -o ${WORKDIR}/1001.o

# This crosses the line all the way to icky...  ld injects a word of
# zeros at the point of the label, if the name is .idata$4 (probably
# "if its a section name", but not if it's an ordinary name.
#
# This is really someone else's ld bug.
#
$X/local_bin/objcopy --redefine-sym .idata\$4=hname --redefine-sym .idata\$5=fthunk ${WORKDIR}/1001.o

cat - >t.s <<EOF
	.globl ${DLL_DESC}_END

${DLL_DESC}_END:
	.section .idata\$4
	.long 0

	.section .idata\$5
	.long 0
EOF

$AS t.s -o ${WORKDIR}/3001.o

rm -f ${RESULT}
${AR} -crv ${RESULT} ${WORKDIR}/*
