#!/bin/sh

# bin2o (heh)
# script to convert a binary data to an elf object file
# (c)2000 Dan Potter
# $Id: bin2o,v 1.3 2002/11/03 04:57:39 bardtx Exp $

if [ $# != 3 ]; then
	echo usage: $0 \<input file\> \<symbol\> \<output file\>
	exit 0
fi

TMPFILE1=/tmp/script$$.ld
TMPFILE2=/tmp/obja$$.o
TMPFILE3=/tmp/objb$$.o

# Gotta do a different binary target here depending on the target
if [ $KOS_ARCH = "dreamcast" ]; then
	echo ".section .rodata; .align 2; " | $KOS_AS $KOS_AFLAGS -o $TMPFILE3
	if [ $? -ne 0 ]; then exit 1; fi
	echo "SECTIONS { .rodata : { _$2 = .; *(.data); _$2_end = .; } }" > $TMPFILE1
	$KOS_LD --no-warn-mismatch --format binary --oformat elf32-shl $1 --format elf32-shl $TMPFILE3 -o $TMPFILE2 -r -EL -T $TMPFILE1
	if [ $? -ne 0 ]; then exit 1; fi
	$KOS_OBJCOPY --set-section-flags .rodata=alloc,load,data,readonly $TMPFILE2 $3
	if [ $? -ne 0 ]; then exit 1; fi
	rm -f $TMPFILE1 $TMPFILE2 $TMPFILE3
fi

if [ $KOS_ARCH = "gba" ]; then
	echo "SECTIONS { .rodata : { $2 = .; *(.data); $2_end = .; } }" > /tmp/script.ld
	$KOS_LD --no-warn-mismatch --format binary --oformat elf32-littlearm $1 -o $3 -r -EL -T /tmp/script.ld
fi

if [ $KOS_ARCH = "ps2" ]; then
	echo "OUTPUT_ARCH(mips:5900) SECTIONS { .rodata : { _$2 = .; *(.data); _$2_end = .; } }" > /tmp/script.ld
	$KOS_LD --no-warn-mismatch --format binary --oformat elf64-littlemips -mips3 $1 -o $3 -r -EL -T /tmp/script.ld
fi

rm -f /tmp/script.ld

