changeset 121:7bc853cb2ca9

Added simplistic output module for DECB target
author lost
date Thu, 22 Jan 2009 02:05:08 +0000
parents 36ce328df3c3
children 2ece9adb4e4b
files lwlink/trunk/src/Makefile.am lwlink/trunk/src/link.c lwlink/trunk/src/lwlink.h lwlink/trunk/src/main.c lwlink/trunk/src/output.c lwlink/trunk/src/script.c
diffstat 6 files changed, 155 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/lwlink/trunk/src/Makefile.am	Wed Jan 21 06:14:08 2009 +0000
+++ b/lwlink/trunk/src/Makefile.am	Thu Jan 22 02:05:08 2009 +0000
@@ -1,3 +1,3 @@
 bin_PROGRAMS = lwlink
-lwlink_SOURCES = main.c lwlink.c util.c readfiles.c expr.c script.c link.c
+lwlink_SOURCES = main.c lwlink.c util.c readfiles.c expr.c script.c link.c output.c
 EXTRA_DIST = lwlink.h util.h expr.h
--- a/lwlink/trunk/src/link.c	Wed Jan 21 06:14:08 2009 +0000
+++ b/lwlink/trunk/src/link.c	Thu Jan 22 02:05:08 2009 +0000
@@ -32,14 +32,8 @@
 #include "lwlink.h"
 #include "util.h"
 
-struct section_list
-{
-	section_t *ptr;		// ptr to section structure
-	int forceaddr;		// was this force to an address by the link script?
-};
-
-static struct section_list *sectlist = NULL;
-static int nsects = 0;
+struct section_list *sectlist = NULL;
+int nsects = 0;
 
 // work out section load order and resolve base addresses for each section
 // make a list of sections to load in order
@@ -224,6 +218,18 @@
 	int sn;
 	reloc_t *rl;
 	int rval;
+
+	// resolve entry point if required
+	// this must resolve to an *exported* symbol and will resolve to the
+	// first instance of that symbol
+	if (linkscript.execsym)
+	{
+		lw_expr_stack_t *s;
+		
+		s = resolve_sym(linkscript.execsym, 0, NULL);
+		linkscript.execaddr = lw_expr_get_value(s);
+		lw_expr_stack_free(s);
+	}
 	
 	for (sn = 0; nsects; sn++)
 	{
--- a/lwlink/trunk/src/lwlink.h	Wed Jan 21 06:14:08 2009 +0000
+++ b/lwlink/trunk/src/lwlink.h	Thu Jan 22 02:05:08 2009 +0000
@@ -76,6 +76,16 @@
 	
 };
 
+struct section_list
+{
+	section_t *ptr;		// ptr to section structure
+	int forceaddr;		// was this force to an address by the link script?
+};
+
+#ifndef __link_c_seen__
+extern struct section_list *sectlist;
+extern int nsects;
+#endif
 
 
 #ifndef __lwlink_c_seen__
@@ -109,6 +119,8 @@
 	int nlines;					// number of lines in the script
 	struct scriptline_s *lines;	// the parsed script lines (section)
 	int padsize;				// the size to pad to, -1 for none
+	char *execsym;				// entry point symbol
+	int execaddr;				// execution address (entry point)
 } linkscript_t;
 
 #ifndef __script_c_seen__
--- a/lwlink/trunk/src/main.c	Wed Jan 21 06:14:08 2009 +0000
+++ b/lwlink/trunk/src/main.c	Thu Jan 22 02:05:08 2009 +0000
@@ -122,6 +122,8 @@
 extern void read_files(void);
 extern void setup_script(void);
 extern void resolve_sections(void);
+extern void resolve_references(void);
+extern void do_output(void);
 
 // main function; parse command line, set up assembler state, and run the
 // assembler on the first file
@@ -143,5 +145,11 @@
 	// resolve section bases and section order
 	resolve_sections();
 	
+	// resolve incomplete references
+	resolve_references();
+	
+	// do the actual output
+	do_output();
+	
 	exit(0);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/trunk/src/output.c	Thu Jan 22 02:05:08 2009 +0000
@@ -0,0 +1,103 @@
+/*
+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;
+		}
+		// 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)
+{
+}
--- a/lwlink/trunk/src/script.c	Wed Jan 21 06:14:08 2009 +0000
+++ b/lwlink/trunk/src/script.c	Thu Jan 22 02:05:08 2009 +0000
@@ -39,6 +39,7 @@
 	"section code\n"
 	"section *,!bss\n"
 	"section *,bss\n"
+	"entry 2000\n"
 	;
 
 // the built-in RAW target linker script
@@ -158,6 +159,22 @@
 			if (linkscript.padsize < 0)
 				linkscript.padsize = 0;
 		}
+		else if (!strcmp(line, "entry"))
+		{
+			int eaddr;
+			
+			eaddr = strtol(ptr, &ptr2, 16);
+			if (*ptr2)
+			{
+				linkscript.execaddr = -1;
+				linkscript.execsym = lw_strdup(ptr);
+			}
+			else
+			{
+				linkscript.execaddr = eaddr;
+				linkscript.execsym = NULL;
+			}
+		}
 		else if (!strcmp(line, "section"))
 		{
 			// section