view dspanal/char2coff.c @ 346:2aa78da4033b

fluid-mnf: compiles and links
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 13 Mar 2020 06:16:57 +0000
parents f8344bc4fd61
children
line wrap: on
line source

/*
 * This program will convert C char array files into COFF
 * for disassembly analysis.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "coffout.h"

extern unsigned read_char_file();

static u_char binbuffer[300000];
static unsigned array_size;
static u_char *array_ptr, *array_end;

#define	MAX_SECTIONS	256

static struct coff_section sections[MAX_SECTIONS];
static unsigned nsections;

static void
add_section(addr, size, data)
	uint32_t addr, size;
	u_char *data;
{
	struct coff_section *sec;

	if (nsections >= MAX_SECTIONS) {
		fprintf(stderr, "error: too many sections\n");
		exit(1);
	}
	sec = sections + nsections;
	sec->addr = addr;
	sec->size = size;
	sec->data = data;
	sec->flags = 0x0020;
	sec->mempage = 0x00;
	nsections++;
}

static uint32_t
get_longword()
{
	uint32_t datum;

	if (array_ptr + 4 > array_end) {
		fprintf(stderr, "error: parse spills past end of array\n");
		exit(1);
	}
	datum = (array_ptr[3] << 24) | (array_ptr[2] << 16) |
		(array_ptr[1] << 8) | array_ptr[0];
	array_ptr += 4;
	return datum;
}

main(argc, argv)
	char **argv;
{
	uint32_t addr, size;

	if (argc != 3) {
		fprintf(stderr, "usage: %s char-array-file coff-output-file\n",
			argv[0]);
		exit(1);
	}
	array_size = read_char_file(argv[1], binbuffer, sizeof binbuffer);
	array_ptr = binbuffer;
	array_end = array_ptr + array_size;
	if (get_longword() != 0) {
		fprintf(stderr, "error: initial tag 0 is missing\n");
		exit(1);
	}
	if (get_longword() != 3) {
		fprintf(stderr, "error: initial vers 3 is missing\n");
		exit(1);
	}
	for (;;) {
		size = get_longword();
		if (size > 0x10000) {
			fprintf(stderr, "error: section size unreasonable\n");
			exit(1);
		}
		addr = get_longword();
		if (!size)
			break;
		add_section(addr, size, array_ptr);
		array_ptr += size << 1;
	}
	emit_coff_output(argv[2], sections, nsections);
	exit(0);
}