diff lwlink/output.c @ 139:106c2fe3c9d9

repo reorg
author lost
date Wed, 28 Jan 2009 05:59:14 +0000
parents lwlink-old/trunk/src/output.c@050864a47b38
children 302b8db5fd89
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/output.c	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,127 @@
+/*
+output.c
+Copyright © 2009 William Astle
+
+This file is part of LWLINK.
+
+LWLINK is free software: you can redistribute it and/or modify it under the
+terms of the GNU General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any later
+version.
+
+This program 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 General Public License for
+more details.
+
+You should have received a copy of the GNU General Public License along with
+this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+Actually output the binary
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "lwlink.h"
+
+// this prevents warnings about not using the return value of fwrite()
+// and, theoretically, can be replaced with a function that handles things
+// better in the future
+#define writebytes(s, l, c, f)	do { int r; r = fwrite((s), (l), (c), (f)); } while (0)
+
+void do_output_decb(FILE *of);
+void do_output_raw(FILE *of);
+
+void do_output(void)
+{
+	FILE *of;
+	
+	of = fopen(outfile, "wb");
+	if (!of)
+	{
+		fprintf(stderr, "Cannot open output file %s: ", outfile);
+		perror("");
+		exit(1);
+	}
+	
+	switch (outformat)
+	{
+	case OUTPUT_DECB:
+		do_output_decb(of);
+		break;
+	
+	case OUTPUT_RAW:
+		do_output_raw(of);
+		break;
+	
+	default:
+		fprintf(stderr, "Unknown output format doing output!\n");
+		exit(111);
+	}
+	
+	fclose(of);
+}
+
+void do_output_decb(FILE *of)
+{
+	int sn;
+	unsigned char buf[5];
+	
+	for (sn = 0; sn < nsects; sn++)
+	{
+		if (sectlist[sn].ptr -> flags & SECTION_BSS)
+		{
+			// no output for a BSS section
+			continue;
+		}
+		if (sectlist[sn].ptr -> codesize == 0)
+		{
+			// don't generate output for a zero size section
+			continue;
+		}
+		// write a preamble
+		buf[0] = 0x00;
+		buf[1] = sectlist[sn].ptr -> codesize >> 8;
+		buf[2] = sectlist[sn].ptr -> codesize & 0xff;
+		buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
+		buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
+		writebytes(buf, 1, 5, of);
+		writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
+	}
+	// write a postamble
+	buf[0] = 0xff;
+	buf[1] = 0x00;
+	buf[2] = 0x00;
+	buf[3] = linkscript.execaddr >> 8;
+	buf[4] = linkscript.execaddr & 0xff;
+	writebytes(buf, 1, 5, of);
+}
+
+void do_output_raw(FILE *of)
+{
+	int nskips = 0;		// used to output blanks for BSS inline
+	int sn;
+	
+	for (sn = 0; sn < nsects; sn++)
+	{
+		if (sectlist[sn].ptr -> flags & SECTION_BSS)
+		{
+			// no output for a BSS section
+			nskips += sectlist[sn].ptr -> codesize;
+			continue;
+		}
+		while (nskips > 0)
+		{
+			// the "" is not an error - it turns into a single NUL byte!
+			writebytes("", 1, 1, of);
+			nskips--;
+		}
+		writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
+	}
+}