#!/bin/sh

INBASE=`basename "$1" .o`
OUTFILE="$INBASE".linked.o

if [ -f "$INBASE".bin ] 
then
	rm "$INBASE".bin
fi
if [ -f "$INBASE".map ] 
then
	rm "$INBASE".map
fi
if [ -f "$OUTFILE" ] 
then
	rm "$OUTFILE"
fi
if [ -f "$INBASE".got ] 
then
	rm "$INBASE".got
fi
		
arm-elf-ld --section-start .text=0 -Map "$INBASE".map --script armletldscript \
 -o "$INBASE".linked.o \
 $* \
 /cygdrive/c/archive/devo/toolkits/armletsdr2/sample_code/armletlib_public/armletlib.a \
 /usr/local/lib/gcc-lib/arm-elf/3.2/libgcc.a

if [ -f "$OUTFILE" ]
then
#       arm-elf-objdump --disassemble "$OUTFILE"

        SECTIONSIZE=`arm-elf-objdump -h "$OUTFILE" | grep " .data " | cut --characters=19-26`
#       echo $SECTIONSIZE
        if [ ! $SECTIONSIZE = "00000000" ]
        then
                echo "$OUTFILE: contains nonzero .data section"
        fi

        SECTIONSIZE=`arm-elf-objdump -h "$OUTFILE" | grep " .bss " | cut --characters=19-26`
        if [ ! $SECTIONSIZE = "00000000" ]
        then
                echo "$OUTFILE: contains nonzero .bss section"
        fi

        SECTIONSIZE=`arm-elf-objdump -h "$OUTFILE" | grep " .rodata " | cut --characters=19-26`
        if [ ! $SECTIONSIZE = "00000000" ]
        then
                echo "$OUTFILE: contains nonzero .rodata section"
        fi

        SECTIONSIZE=`arm-elf-objdump -h "$OUTFILE" | grep " .got " | cut --characters=19-26`
        if [ ! $SECTIONSIZE = "00000000" ]
        then
                echo "$OUTFILE: contains .got section"
        fi

	arm-elf-objcopy -j .text -O binary "$OUTFILE" "$INBASE".bin
	arm-elf-objcopy -j .got -O binary "$OUTFILE" "$INBASE".got

        echo "$1: ok"
fi
