changeset 139:106c2fe3c9d9

repo reorg
author lost
date Wed, 28 Jan 2009 05:59:14 +0000
parents 050864a47b38
children 8d65ef71fc01
files doc/lwlink.txt doc/scripts.txt lwlink-old/trunk/doc/lwlink.txt lwlink-old/trunk/doc/scripts.txt lwlink-old/trunk/src/Makefile.am lwlink-old/trunk/src/expr.c lwlink-old/trunk/src/expr.h lwlink-old/trunk/src/link.c lwlink-old/trunk/src/lwlink.c lwlink-old/trunk/src/lwlink.h lwlink-old/trunk/src/main.c lwlink-old/trunk/src/objdump.c lwlink-old/trunk/src/output.c lwlink-old/trunk/src/readfiles.c lwlink-old/trunk/src/script.c lwlink-old/trunk/src/util.c lwlink-old/trunk/src/util.h lwlink/Makefile.am lwlink/expr.c lwlink/expr.h lwlink/link.c lwlink/lwlink.c lwlink/lwlink.h lwlink/main.c lwlink/objdump.c lwlink/output.c lwlink/readfiles.c lwlink/script.c lwlink/util.c lwlink/util.h
diffstat 30 files changed, 2305 insertions(+), 2305 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/lwlink.txt	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,51 @@
+This is the companion linker to LWASM. It reads object files generated by
+LWASM and combines them into an actual binary.
+
+During linking, each file is read into memory. A list of externally
+referenced symbols is made along with where these symbols are referenced.
+Each external reference is checked against all previously loaded files (in
+order of loading) and if a match is found, a note of that fact is made and a
+link between the previously loaded file and the current reference.
+
+Once all files are loaded, the symbol table is checked for any symbols which
+are still unresolved. If any are found, the linking process complains and
+bails out.
+
+Once all the object files have been read, the linker follows a
+pre-determined script for the specified target or a script supplied by the
+user to lay out the binary. The instructions from the script are followed
+blindly as it is assumed the user knows what he is doing.
+
+For each defined section, the linker begins constructing the section data by
+resolving each instance of that section in the order it was encountered. All
+symbols defined by that section (local or exported) are assigned addresses.
+The exact offset into the final section data is recorded for any incomplete
+references in that section. All section base address references are resolved
+to actual addresses at this stage.
+
+Once all sections have been laid out and addresses assigned to all symbols,
+all incomplete references are resolved and the resulting value placed into
+the appropriate data stream. If any references cannot be resolved at this
+stage, the linker will complain and bail out.
+
+Once all sections, symbols, and incomplete references have been resolved,
+the binary will output as appropriate for the specified target.
+
+See the file "scripts.txt" for information about linker scripts and the
+restrictions based on the output target.
+
+The following output targets are supported:
+
+Raw: this is a raw binary with no header information, etc. Suitable for ROM
+images, etc. By default, the raw target starts the binary at address 0, puts
+any section named "init" first, then "code", then all other non-bss
+sections, then all bss sections. Note that any "bss" type section that
+exists anywhere but at the end of the binary (i.e. is between or before one
+or more non-bss sections) will be included as a series of NUL bytes.
+
+DECB: this creates a LOADM style binary according to the linker script. By
+default, this target places the sections in the same order as the raw target
+but implements a load address of $2000. bss sections will not be included in
+the actual output. If a bss section appears between two non-bss sections, a
+new output block will be created in the output file.
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/doc/scripts.txt	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,63 @@
+LWLINK linker scripts
+
+A linker script is used to instruct the linker about how to assemble the
+various sections into a completed binary. It consists of a series of
+directives which are considered in the order they are encountered. Any
+section not referenced by a directive is assumed to be loaded after the
+final section explicitly referenced.
+
+The sections will appear in the resulting binary in the order they are
+specified in the script file.
+
+If a referenced section is not found, the linker will behave as though the
+section did exist but had a zero size, no relocations, and no exports.
+
+A section may only be referenced once. Any subsequent references will have
+no effect.
+
+All numbers are hexadecimal.
+
+section <name> load <addr>
+
+This causes the section <name> to load at <addr>. For raw target, only one
+"load at" entry is allowed for non-bss sections and it must be the first
+one. For raw targets, it affects the addresses the linker assigns to symbols
+but has no other affect on the output. bss sections may all have separate
+load addresses but since they will not appear in the binary anyway, this is
+okay.
+
+For the DECB target, each "load" entry will cause a new "block" to be
+output to the binary which will contain the load address. It is legal for
+sections to overlap in this manner - the linker assumes the loader will sort
+everything out.
+
+section <name>
+
+This will cause the section <name> to load after the previously listed
+section.
+
+exec <addr or sym>
+
+This will cause the execution address (entry point) to be the address
+specified (in hex) *or* the specified symbol name. The symbol name must
+match a symbol that is exported by one of the object files being linked.
+This has no effect for targets that do not encode the entry point into the
+resulting file. If not specified, the entry point is assumed to be address 0
+which is probably not what you want. The default link scripts for targets
+that support this directive automatically starts at the beginning of the
+first section (usually "init" or "code") that is emitted in the binary.
+
+pad <size>
+
+This will cause the output file to be padded with NUL bytes to be exactly
+<size> bytes in length. This only makes sense for a raw target.
+
+
+If <name> is "*", then any section not already matched by the script will be
+matched. For format *,<flags> can be used to select sections which have
+particular flags set (or not set). For instance:
+
+*,!bss		This would match all sections that do not have the bss flag set
+*,bss		this would match all sections that do have the bss flag set
+
+
--- a/lwlink-old/trunk/doc/lwlink.txt	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-This is the companion linker to LWASM. It reads object files generated by
-LWASM and combines them into an actual binary.
-
-During linking, each file is read into memory. A list of externally
-referenced symbols is made along with where these symbols are referenced.
-Each external reference is checked against all previously loaded files (in
-order of loading) and if a match is found, a note of that fact is made and a
-link between the previously loaded file and the current reference.
-
-Once all files are loaded, the symbol table is checked for any symbols which
-are still unresolved. If any are found, the linking process complains and
-bails out.
-
-Once all the object files have been read, the linker follows a
-pre-determined script for the specified target or a script supplied by the
-user to lay out the binary. The instructions from the script are followed
-blindly as it is assumed the user knows what he is doing.
-
-For each defined section, the linker begins constructing the section data by
-resolving each instance of that section in the order it was encountered. All
-symbols defined by that section (local or exported) are assigned addresses.
-The exact offset into the final section data is recorded for any incomplete
-references in that section. All section base address references are resolved
-to actual addresses at this stage.
-
-Once all sections have been laid out and addresses assigned to all symbols,
-all incomplete references are resolved and the resulting value placed into
-the appropriate data stream. If any references cannot be resolved at this
-stage, the linker will complain and bail out.
-
-Once all sections, symbols, and incomplete references have been resolved,
-the binary will output as appropriate for the specified target.
-
-See the file "scripts.txt" for information about linker scripts and the
-restrictions based on the output target.
-
-The following output targets are supported:
-
-Raw: this is a raw binary with no header information, etc. Suitable for ROM
-images, etc. By default, the raw target starts the binary at address 0, puts
-any section named "init" first, then "code", then all other non-bss
-sections, then all bss sections. Note that any "bss" type section that
-exists anywhere but at the end of the binary (i.e. is between or before one
-or more non-bss sections) will be included as a series of NUL bytes.
-
-DECB: this creates a LOADM style binary according to the linker script. By
-default, this target places the sections in the same order as the raw target
-but implements a load address of $2000. bss sections will not be included in
-the actual output. If a bss section appears between two non-bss sections, a
-new output block will be created in the output file.
-
--- a/lwlink-old/trunk/doc/scripts.txt	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,63 +0,0 @@
-LWLINK linker scripts
-
-A linker script is used to instruct the linker about how to assemble the
-various sections into a completed binary. It consists of a series of
-directives which are considered in the order they are encountered. Any
-section not referenced by a directive is assumed to be loaded after the
-final section explicitly referenced.
-
-The sections will appear in the resulting binary in the order they are
-specified in the script file.
-
-If a referenced section is not found, the linker will behave as though the
-section did exist but had a zero size, no relocations, and no exports.
-
-A section may only be referenced once. Any subsequent references will have
-no effect.
-
-All numbers are hexadecimal.
-
-section <name> load <addr>
-
-This causes the section <name> to load at <addr>. For raw target, only one
-"load at" entry is allowed for non-bss sections and it must be the first
-one. For raw targets, it affects the addresses the linker assigns to symbols
-but has no other affect on the output. bss sections may all have separate
-load addresses but since they will not appear in the binary anyway, this is
-okay.
-
-For the DECB target, each "load" entry will cause a new "block" to be
-output to the binary which will contain the load address. It is legal for
-sections to overlap in this manner - the linker assumes the loader will sort
-everything out.
-
-section <name>
-
-This will cause the section <name> to load after the previously listed
-section.
-
-exec <addr or sym>
-
-This will cause the execution address (entry point) to be the address
-specified (in hex) *or* the specified symbol name. The symbol name must
-match a symbol that is exported by one of the object files being linked.
-This has no effect for targets that do not encode the entry point into the
-resulting file. If not specified, the entry point is assumed to be address 0
-which is probably not what you want. The default link scripts for targets
-that support this directive automatically starts at the beginning of the
-first section (usually "init" or "code") that is emitted in the binary.
-
-pad <size>
-
-This will cause the output file to be padded with NUL bytes to be exactly
-<size> bytes in length. This only makes sense for a raw target.
-
-
-If <name> is "*", then any section not already matched by the script will be
-matched. For format *,<flags> can be used to select sections which have
-particular flags set (or not set). For instance:
-
-*,!bss		This would match all sections that do not have the bss flag set
-*,bss		this would match all sections that do have the bss flag set
-
-
--- a/lwlink-old/trunk/src/Makefile.am	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,4 +0,0 @@
-bin_PROGRAMS = lwlink lwobjdump
-lwlink_SOURCES = main.c lwlink.c util.c readfiles.c expr.c script.c link.c output.c
-lwobjdump_SOURCES = objdump.c util.c
-EXTRA_DIST = lwlink.h util.h expr.h
--- a/lwlink-old/trunk/src/expr.c	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,356 +0,0 @@
-/*
-expr.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/>.
-*/
-
-/*
-This file contains the actual expression evaluator
-*/
-
-#define __expr_c_seen__
-
-#include <ctype.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "expr.h"
-#include "util.h"
-
-lw_expr_stack_t *lw_expr_stack_create(void)
-{
-	lw_expr_stack_t *s;
-	
-	s = lw_malloc(sizeof(lw_expr_stack_t));
-	s -> head = NULL;
-	s -> tail = NULL;
-	return s;
-}
-
-void lw_expr_stack_free(lw_expr_stack_t *s)
-{
-	while (s -> head)
-	{
-		s -> tail = s -> head;
-		s -> head = s -> head -> next;
-		lw_expr_term_free(s -> tail -> term);
-		lw_free(s -> tail);
-	}
-	lw_free(s);
-}
-
-void lw_expr_term_free(lw_expr_term_t *t)
-{
-	if (t)
-	{
-		if (t -> term_type == LW_TERM_SYM)
-			lw_free(t -> symbol);
-		lw_free(t);
-	}
-}
-
-lw_expr_term_t *lw_expr_term_create_oper(int oper)
-{
-	lw_expr_term_t *t;
-
-	t = lw_malloc(sizeof(lw_expr_term_t));
-	t -> term_type = LW_TERM_OPER;
-	t -> value = oper;
-	return t;
-}
-
-lw_expr_term_t *lw_expr_term_create_int(int val)
-{
-	lw_expr_term_t *t;
-	
-	t = lw_malloc(sizeof(lw_expr_term_t));
-	t -> term_type = LW_TERM_INT;
-	t -> value = val;
-	return t;
-}
-
-lw_expr_term_t *lw_expr_term_create_sym(char *sym, int symtype)
-{
-	lw_expr_term_t *t;
-	
-	t = lw_malloc(sizeof(lw_expr_term_t));
-	t -> term_type = LW_TERM_SYM;
-	t -> symbol = lw_strdup(sym);
-	t -> value = symtype;
-	return t;
-}
-
-lw_expr_term_t *lw_expr_term_dup(lw_expr_term_t *t)
-{
-	switch (t -> term_type)
-	{
-	case LW_TERM_INT:
-		return lw_expr_term_create_int(t -> value);
-		
-	case LW_TERM_OPER:
-		return lw_expr_term_create_oper(t -> value);
-		
-	case LW_TERM_SYM:
-		return lw_expr_term_create_sym(t -> symbol, t -> value);
-	
-	default:
-		exit(1);
-	}
-// can't get here
-}
-
-void lw_expr_stack_push(lw_expr_stack_t *s, lw_expr_term_t *t)
-{
-	lw_expr_stack_node_t *n;
-
-	if (!s)
-	{
-		exit(1);
-	}
-	
-	n = lw_malloc(sizeof(lw_expr_stack_node_t));
-	n -> next = NULL;
-	n -> prev = s -> tail;
-	n -> term = lw_expr_term_dup(t);
-	
-	if (s -> head)
-	{
-		s -> tail -> next = n;
-		s -> tail = n;
-	}
-	else
-	{
-		s -> head = n;
-		s -> tail = n;
-	}
-}
-
-lw_expr_term_t *lw_expr_stack_pop(lw_expr_stack_t *s)
-{
-	lw_expr_term_t *t;
-	lw_expr_stack_node_t *n;
-	
-	if (!(s -> tail))
-		return NULL;
-	
-	n = s -> tail;
-	s -> tail = n -> prev;
-	if (!(n -> prev))
-	{
-		s -> head = NULL;
-	}
-	
-	t = n -> term;
-	n -> term = NULL;
-	
-	lw_free(n);
-	
-	return t;
-}
-
-/*
-take an expression stack s and scan for operations that can be completed
-
-return -1 on error, 0 on no error
-
-possible errors are: division by zero or unknown operator
-
-theory of operation:
-
-scan the stack for an operator which has two constants preceding it (binary)
-or 1 constant preceding it (unary) and if found, perform the calculation
-and replace the operator and its operands with the result
-
-repeat the scan until no futher simplications are found or if there are no
-further operators or only a single term remains
-
-*/
-int lw_expr_reval(lw_expr_stack_t *s, lw_expr_stack_t *(*sfunc)(char *sym, int stype, void *state), void *state)
-{
-	lw_expr_stack_node_t *n, *n2;
-	lw_expr_stack_t *ss;
-	int c;
-	
-next_iter_sym:
-	// resolve symbols
-	// symbols that do not resolve to an expression are left alone
-	for (c = 0, n = s -> head; n; n = n -> next)
-	{
-		if (n -> term -> term_type == LW_TERM_SYM)
-		{
-			ss = sfunc(n -> term -> symbol, n -> term -> value, state);
-			if (ss)
-			{
-				c++;
-				// splice in the result stack
-				if (n -> prev)
-				{
-					n -> prev -> next = ss -> head;
-				}
-				else
-				{
-					s -> head = ss -> head;
-				}
-				ss -> head -> prev = n -> prev;
-				ss -> tail -> next = n -> next;
-				if (n -> next)
-				{
-					n -> next -> prev = ss -> tail;
-				}
-				else
-				{
-					s -> tail = ss -> tail;
-				}
-				lw_expr_term_free(n -> term);
-				lw_free(n);
-				n = ss -> tail;
-				
-				ss -> head = NULL;
-				ss -> tail = NULL;
-				lw_expr_stack_free(ss);
-			}
-		}
-	}
-	if (c)
-		goto next_iter_sym;
-
-next_iter:	
-	// a single term
-	if (s -> head == s -> tail)
-		return 0;
-	
-	// search for an operator
-	for (n = s -> head; n; n = n -> next)
-	{
-		if (n -> term -> term_type == LW_TERM_OPER)
-		{
-			if (n -> term -> value == LW_OPER_NEG
-				|| n -> term -> value == LW_OPER_COM
-				)
-			{
-				// unary operator
-				if (n -> prev && n -> prev -> term -> term_type == LW_TERM_INT)
-				{
-					// a unary operator we can resolve
-					// we do the op then remove the term "n" is pointing at
-					if (n -> term -> value == LW_OPER_NEG)
-					{
-						n -> prev -> term -> value = -(n -> prev -> term -> value);
-					}
-					else if (n -> term -> value == LW_OPER_COM)
-					{
-						n -> prev -> term -> value = ~(n -> prev -> term -> value);
-					}
-					n -> prev -> next = n -> next;
-					if (n -> next)
-						n -> next -> prev = n -> prev;
-					else
-						s -> tail = n -> prev;	
-					
-					lw_expr_term_free(n -> term);
-					lw_free(n);
-					break;
-				}
-			}
-			else
-			{
-				// binary operator
-				if (n -> prev && n -> prev -> prev && n -> prev -> term -> term_type == LW_TERM_INT && n -> prev -> prev -> term -> term_type == LW_TERM_INT)
-				{
-					// a binary operator we can resolve
-					switch (n -> term -> value)
-					{
-					case LW_OPER_PLUS:
-						n -> prev -> prev -> term -> value += n -> prev -> term -> value;
-						break;
-
-					case LW_OPER_MINUS:
-						n -> prev -> prev -> term -> value -= n -> prev -> term -> value;
-						break;
-
-					case LW_OPER_TIMES:
-						n -> prev -> prev -> term -> value *= n -> prev -> term -> value;
-						break;
-
-					case LW_OPER_DIVIDE:
-						if (n -> prev -> term -> value == 0)
-							return -1;
-						n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
-						break;
-
-					case LW_OPER_MOD:
-						if (n -> prev -> term -> value == 0)
-							return -1;
-						n -> prev -> prev -> term -> value %= n -> prev -> term -> value;
-						break;
-
-					case LW_OPER_INTDIV:
-						if (n -> prev -> term -> value == 0)
-							return -1;
-						n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
-						break;
-
-					case LW_OPER_BWAND:
-						n -> prev -> prev -> term -> value &= n -> prev -> term -> value;
-						break;
-
-					case LW_OPER_BWOR:
-						n -> prev -> prev -> term -> value |= n -> prev -> term -> value;
-						break;
-
-					case LW_OPER_BWXOR:
-						n -> prev -> prev -> term -> value ^= n -> prev -> term -> value;
-						break;
-
-					case LW_OPER_AND:
-						n -> prev -> prev -> term -> value = (n -> prev -> term -> value && n -> prev -> prev -> term -> value) ? 1 : 0;
-						break;
-
-					case LW_OPER_OR:
-						n -> prev -> prev -> term -> value = (n -> prev -> term -> value || n -> prev -> prev -> term -> value) ? 1 : 0;
-						break;
-
-					default:
-						// return error if unknown operator!
-						return -1;
-					}
-
-					// now remove the two unneeded entries from the stack
-					n -> prev -> prev -> next = n -> next;
-					if (n -> next)
-						n -> next -> prev = n -> prev -> prev;
-					else
-						s -> tail = n -> prev -> prev;	
-					
-					lw_expr_term_free(n -> term);
-					lw_expr_term_free(n -> prev -> term);
-					lw_free(n -> prev);
-					lw_free(n);
-					break;
-				}
-			}
-		}
-	}
-	// note for the terminally confused about dynamic memory and pointers:
-	// n will not be NULL even after the lw_free calls above so
-	// this test will still work (n will be a dangling pointer)
-	// (n will only be NULL if we didn't find any operators to simplify)
-	if (n)
-		goto next_iter;
-	
-	return 0;
-}
--- a/lwlink-old/trunk/src/expr.h	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,107 +0,0 @@
-/*
-expr.h
-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/>.
-*/
-
-/*
-Definitions for expression evaluator
-*/
-
-#ifndef __expr_h_seen__
-#define __expr_h_seen__
-
-#ifndef __expr_c_seen__
-#define __expr_E__ extern
-#else
-#define __expr_E__
-#endif
-
-// term types
-#define LW_TERM_NONE		0
-#define LW_TERM_OPER		1	// an operator
-#define LW_TERM_INT		2	// 32 bit signed integer
-#define LW_TERM_SYM		3	// symbol reference
-
-// operator types
-#define LW_OPER_NONE		0
-#define LW_OPER_PLUS		1	// +
-#define LW_OPER_MINUS	2	// -
-#define LW_OPER_TIMES	3	// *
-#define LW_OPER_DIVIDE	4	// /
-#define LW_OPER_MOD		5	// %
-#define LW_OPER_INTDIV	6	// \ (don't end line with \)
-#define LW_OPER_BWAND	7	// bitwise AND
-#define LW_OPER_BWOR		8	// bitwise OR
-#define LW_OPER_BWXOR	9	// bitwise XOR
-#define LW_OPER_AND		10	// boolean AND
-#define LW_OPER_OR		11	// boolean OR
-#define LW_OPER_NEG		12	// - unary negation (2's complement)
-#define LW_OPER_COM		13	// ^ unary 1's complement
-
-
-// term structure
-typedef struct lw_expr_term_s
-{
-	int term_type;		// type of term (see above)
-	char *symbol;		// name of a symbol
-	int value;			// value of the term (int) or operator number (OPER)
-} lw_expr_term_t;
-
-// type for an expression evaluation stack
-typedef struct lw_expr_stack_node_s lw_expr_stack_node_t;
-struct lw_expr_stack_node_s
-{
-	lw_expr_term_t		*term;
-	lw_expr_stack_node_t	*prev;
-	lw_expr_stack_node_t	*next;	
-};
-
-typedef struct lw_expr_stack_s
-{
-	lw_expr_stack_node_t *head;
-	lw_expr_stack_node_t *tail;
-} lw_expr_stack_t;
-
-__expr_E__ void lw_expr_term_free(lw_expr_term_t *t);
-__expr_E__ lw_expr_term_t *lw_expr_term_create_oper(int oper);
-__expr_E__ lw_expr_term_t *lw_expr_term_create_sym(char *sym, int symtype);
-__expr_E__ lw_expr_term_t *lw_expr_term_create_int(int val);
-__expr_E__ lw_expr_term_t *lw_expr_term_dup(lw_expr_term_t *t);
-
-__expr_E__ void lw_expr_stack_free(lw_expr_stack_t *s);
-__expr_E__ lw_expr_stack_t *lw_expr_stack_create(void);
-
-__expr_E__ void lw_expr_stack_push(lw_expr_stack_t *s, lw_expr_term_t *t);
-__expr_E__ lw_expr_term_t *lw_expr_stack_pop(lw_expr_stack_t *s);
-
-// simplify expression
-__expr_E__ int lw_expr_reval(lw_expr_stack_t *s, lw_expr_stack_t *(*sfunc)(char *sym, int symtype, void *state), void *state);
-
-// useful macros
-// is the expression "simple" (one term)?
-#define lw_expr_is_simple(s) ((s) -> head == (s) -> tail)
-
-// is the expression constant?
-#define lw_expr_is_constant(s) (lw_expr_is_simple(s) && (!((s) -> head) || (s) -> head -> term -> term_type == LW_TERM_INT))
-
-// get the constant value of an expression or 0 if not constant
-#define lw_expr_get_value(s) (lw_expr_is_constant(s) ? ((s) -> head ? (s) -> head -> term -> value : 0) : 0)
-
-#undef __expr_E__
-
-#endif // __expr_h_seen__
--- a/lwlink-old/trunk/src/link.c	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,265 +0,0 @@
-/*
-link.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/>.
-
-
-Resolve section and symbol addresses; handle incomplete references
-*/
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "expr.h"
-#include "lwlink.h"
-#include "util.h"
-
-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
-void resolve_sections(void)
-{
-	int laddr = 0;
-	int ln, sn, fn;
-	
-	for (ln = 0; ln < linkscript.nlines; ln++)
-	{
-//	printf("Linker script line %d: '%s', %04X, %d, %d\n", ln, linkscript.lines[ln].sectname, linkscript.lines[ln].loadat, linkscript.lines[ln].yesflags, linkscript.lines[ln].noflags);
-		if (linkscript.lines[ln].sectname)
-		{
-			int f = 0;
-			// named section
-			// look for all instances of a section by the specified name
-			// and resolve base addresses and add to the list
-			for (fn = 0; fn < ninputfiles; fn++)
-			{
-				for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
-				{
-//				printf("  Considering %s:%s\n", inputfiles[fn]->filename, inputfiles[fn]->sections[sn].name);
-					if (!strcmp(linkscript.lines[ln].sectname, inputfiles[fn] -> sections[sn].name))
-					{
-						// we have a match
-						sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
-						sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
-						
-						inputfiles[fn] -> sections[sn].processed = 1;
-						if (!f && linkscript.lines[ln].loadat >= 0)
-						{
-							f = 1;
-							sectlist[nsects].forceaddr = 1;
-							laddr = linkscript.lines[ln].loadat;
-						}
-						else
-						{
-							sectlist[nsects].forceaddr = 0;
-						}
-						inputfiles[fn] -> sections[sn].loadaddress = laddr;
-						laddr += inputfiles[fn] -> sections[sn].codesize;
-						nsects++;
-					}
-				}
-			}
-		}
-		else
-		{
-			// wildcard section
-			// look for all sections not yet processed that match flags
-
-			int f = 0;
-			int fn0, sn0;
-			char *sname;
-			
-			// named section
-			// look for all instances of a section by the specified name
-			// and resolve base addresses and add to the list
-			for (fn0 = 0; fn0 < ninputfiles; fn0++)
-			{
-				for (sn0 = 0; sn0 < inputfiles[fn0] -> nsections; sn0++)
-				{
-					// ignore if the "no flags" bit says to
-					if (linkscript.lines[ln].noflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].noflags))
-						continue;
-					// ignore unless the yes flags tell us not to
-					if (linkscript.lines[ln].yesflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].yesflags == 0))
-						continue;
-					if (inputfiles[fn0] -> sections[sn0].processed == 0)
-					{
-						sname = inputfiles[fn0] -> sections[sn0].name;
-						for (fn = 0; fn < ninputfiles; fn++)
-						{
-							for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
-							{
-								if (!strcmp(sname, inputfiles[fn] -> sections[sn].name))
-								{
-									// we have a match
-									sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
-									sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
-						
-									inputfiles[fn] -> sections[sn].processed = 1;
-									if (!f && linkscript.lines[ln].loadat >= 0)
-									{
-										f = 1;
-										sectlist[nsects].forceaddr = 1;
-										laddr = linkscript.lines[ln].loadat;
-									}
-									else
-									{
-										sectlist[nsects].forceaddr = 0;
-									}
-									inputfiles[fn] -> sections[sn].loadaddress = laddr;
-									laddr += inputfiles[fn] -> sections[sn].codesize;
-									nsects++;
-								}
-							}
-						}
-					}
-				}
-			}
-		}
-	}
-	
-	// theoretically, all the base addresses are set now
-}
-
-// resolve all incomplete references now
-// anything that is unresolvable at this stage will throw an error
-// because we know the load address of every section now
-lw_expr_stack_t *resolve_sym(char *sym, int symtype, void *state)
-{
-	section_t *sect = state;
-	lw_expr_term_t *term;
-	int val = 0, i, fn;
-	lw_expr_stack_t *s;
-	symtab_t *se;
-	
-	if (symtype == 1)
-	{
-		// local symbol
-		if (!sym)
-		{
-			val = sect -> loadaddress;
-			goto out;
-		}
-		
-		// start with this section
-		for (se = sect -> localsyms; se; se = se -> next)
-		{
-			if (!strcmp(se -> sym, sym))
-			{
-				val = se -> offset + sect -> loadaddress;
-				goto out;
-			}
-		}
-		// not in this section - check all sections in this file
-		for (i = 0; i < sect -> file -> nsections; i++)
-		{
-			for (se = sect -> file -> sections[i].localsyms; se; se = se -> next)
-			{
-				if (!strcmp(se -> sym, sym))
-				{
-					val = se -> offset + sect -> file -> sections[i].loadaddress;
-					goto out;
-				}
-			}
-		}
-		// not found
-		fprintf(stderr, "Local symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
-		exit(1);
-	}
-	else
-	{
-		// external symbol
-		// read all files in order until found (or not found)
-		for (fn = 0; fn < ninputfiles; fn++)
-		{
-			for (i = 0; i < inputfiles[fn] -> nsections; i++)
-			{
-				for (se = inputfiles[fn] -> sections[i].exportedsyms; se; se = se -> next)
-				{
-					if (!strcmp(sym, se -> sym))
-					{
-						val = se -> offset + inputfiles[fn] -> sections[i].loadaddress;
-						goto out;
-					}
-				}
-			}
-		}
-		if (sect)
-		{
-			fprintf(stderr, "External symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
-		}
-		else
-		{
-			fprintf(stderr, "External symbol %s not found\n", sym);
-		}
-		exit(1);
-	}
-	fprintf(stderr, "Shouldn't ever get here!!!\n");
-	exit(88);
-out:
-	s = lw_expr_stack_create();
-	term = lw_expr_term_create_int(val & 0xffff);
-	lw_expr_stack_push(s, term);
-	lw_expr_term_free(term);
-	return s;
-}
-
-void resolve_references(void)
-{
-	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; sn < nsects; sn++)
-	{
-		for (rl = sectlist[sn].ptr -> incompletes; rl; rl = rl -> next)
-		{
-			// do a "simplify" on the expression
-			rval = lw_expr_reval(rl -> expr, resolve_sym, sectlist[sn].ptr);
-
-			// is it constant? error out if not
-			if (rval != 0 || !lw_expr_is_constant(rl -> expr))
-			{
-				fprintf(stderr, "Incomplete reference at %s:%s:%02X\n", sectlist[sn].ptr -> file -> filename, sectlist[sn].ptr -> name, rl -> offset);
-				exit(1);
-			}
-			
-			// put the value into the relocation address
-			rval = lw_expr_get_value(rl -> expr);
-			sectlist[sn].ptr -> code[rl -> offset] = (rval >> 8) & 0xff;
-			sectlist[sn].ptr -> code[rl -> offset + 1] = rval & 0xff;
-		}
-	}
-}
--- a/lwlink-old/trunk/src/lwlink.c	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,52 +0,0 @@
-/*
-lwlink.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/>.
-
-
-
-*/
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#define __lwlink_c_seen__
-
-#include <argp.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "lwlink.h"
-#include "util.h"
-
-int debug_level = 0;
-int outformat = OUTPUT_DECB;
-char *outfile = NULL;
-char *scriptfile = NULL;
-
-fileinfo_t **inputfiles = NULL;
-int ninputfiles = 0;
-
-void add_input_file(char *fn)
-{
-	inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
-	inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
-	inputfiles[ninputfiles++] -> filename = lw_strdup(fn);
-}
-
--- a/lwlink-old/trunk/src/lwlink.h	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,130 +0,0 @@
-/*
-lwlink.h
-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/>.
-
-Contains the main defs used by the linker
-*/
-
-
-#ifndef __lwlink_h_seen__
-#define __lwlink_h_seen__
-
-#include "expr.h"
-
-#define OUTPUT_DECB		0	// DECB multirecord format
-#define OUTPUT_RAW		1	// raw sequence of bytes
-
-typedef struct symtab_s symtab_t;
-struct symtab_s
-{
-	unsigned char *sym;		// symbol name
-	int offset;				// local offset
-//	int realval;			// resolved value
-	symtab_t *next;			// next symbol
-};
-
-typedef struct reloc_s reloc_t;
-struct reloc_s
-{
-	int offset;				// where in the section
-	lw_expr_stack_t *expr;	// the expression to calculate it
-	reloc_t *next;			// ptr to next relocation
-};
-
-typedef struct fileinfo_s fileinfo_t;
-
-#define SECTION_BSS		1
-typedef struct
-{
-	unsigned char *name;	// name of the section
-	int flags;				// section flags
-	int codesize;			// size of the code
-	unsigned char *code;	// pointer to the code
-	int loadaddress;		// the actual load address of the section
-	int processed;			// was the section processed yet?
-		
-	symtab_t *localsyms;	// local symbol table
-	symtab_t *exportedsyms;	// exported symbols table
-	
-	reloc_t *incompletes;	// table of incomplete references
-	
-	fileinfo_t *file;		// the file we are in
-} section_t;
-
-struct fileinfo_s
-{
-	char *filename;
-	unsigned char *filedata;
-	long filesize;
-	section_t *sections;
-	int nsections;
-	
-};
-
-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__
-
-extern int debug_level;
-extern int outformat;
-extern char *outfile;
-extern int ninputfiles;
-extern fileinfo_t **inputfiles;
-extern char *scriptfile;
-
-#define __lwlink_E__ extern
-#else
-#define __lwlink_E__
-#endif // __lwlink_c_seen__
-
-__lwlink_E__ void add_input_file(char *fn);
-
-#undef __lwlink_E__
-
-struct scriptline_s
-{
-	char *sectname;				// name of section, NULL for wildcard
-	int loadat;					// address to load at (or -1)
-	int noflags;				// flags to NOT have
-	int yesflags;				// flags to HAVE
-};
-
-typedef struct
-{
-	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__
-extern linkscript_t linkscript;
-#endif
-
-#endif //__lwlink_h_seen__
--- a/lwlink-old/trunk/src/main.c	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,155 +0,0 @@
-/*
-main.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/>.
-
-
-Implements the program startup code
-
-*/
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <argp.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "lwlink.h"
-
-// command line option handling
-const char *argp_program_version = PACKAGE_STRING;
-const char *argp_program_bug_address = PACKAGE_BUGREPORT;
-
-static error_t parse_opts(int key, char *arg, struct argp_state *state)
-{
-	switch (key)
-	{
-	case 'o':
-		// output
-		outfile = arg;
-		break;
-	
-	case 's':
-		// script file
-		scriptfile = arg;
-		break;
-
-	case 'd':
-		// debug
-		debug_level++;
-		break;
-	
-	case 'b':
-		// decb output
-		outformat = OUTPUT_DECB;
-		break;
-	
-	case 'r':
-		// raw binary output
-		outformat = OUTPUT_RAW;
-		break;
-	
-	case 'f':
-		// output format
-		if (!strcasecmp(arg, "decb"))
-			outformat = OUTPUT_DECB;
-		else if (!strcasecmp(arg, "raw"))
-			outformat = OUTPUT_RAW;
-		else
-		{
-			fprintf(stderr, "Invalid output format: %s\n", arg);
-			exit(1);
-		}
-		break;
-	case ARGP_KEY_END:
-		// done; sanity check
-		if (!outfile)
-			outfile = "a.out";
-		break;
-	
-	case ARGP_KEY_ARG:
-		add_input_file(arg);
-		break;
-		
-	default:
-		return ARGP_ERR_UNKNOWN;
-	}
-	return 0;
-}
-
-static struct argp_option options[] =
-{
-	{ "output",		'o',	"FILE",	0,
-				"Output to FILE"},
-	{ "debug",		'd',	0,		0,
-				"Set debug mode"},
-	{ "format",		'f',	"TYPE",	0,
-				"Select output format: decb, raw, obj"},
-	{ "decb",		'b',	0,		0,
-				"Generate DECB .bin format output, equivalent of --format=decb"},
-	{ "raw",		'r',	0,		0,
-				"Generate raw binary format output, equivalent of --format=raw"},
-	{ "script",		's',	"FILE",		0,
-				"Specify the linking script (overrides the build in defaults)"},
-	{ 0 }
-};
-
-static struct argp argp =
-{
-	options,
-	parse_opts,
-	"<input file> ...",
-	"LWLINK, a HD6309 and MC6809 cross-linker"
-};
-
-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
-int main(int argc, char **argv)
-{
-	argp_parse(&argp, argc, argv, 0, 0, NULL);
-	if (ninputfiles == 0)
-	{
-		fprintf(stderr, "No input files\n");
-		exit(1);
-	}
-
-	// handle the linker script
-	setup_script();
-
-	// read the input files
-	read_files();
-	
-	// resolve section bases and section order
-	resolve_sections();
-	
-	// resolve incomplete references
-	resolve_references();
-	
-	// do the actual output
-	do_output();
-	
-	exit(0);
-}
--- a/lwlink-old/trunk/src/objdump.c	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,303 +0,0 @@
-/*
-objdump.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/>.
-
-
-A standalone program to dump an object file in a text form to stdout
-
-*/
-
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "util.h"
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-void read_lwobj16v0(unsigned char *filedata, long filesize);
-
-/*
-The logic of reading the entire file into memory is simple. All the symbol
-names in the file are NUL terminated strings and can be used directly without
-making additional copies.
-*/
-int main(int argc, char **argv)
-{
-	int i;
-	long size;
-	FILE *f;
-	long bread;
-	unsigned char *filedata;
-	
-	if (argc != 2)
-	{
-		fprintf(stderr, "Must specify exactly one input file.\n");
-		exit(1);
-	}
-
-	f = fopen(argv[1], "rb");
-	if (!f)
-	{
-		fprintf(stderr, "Can't open file %s:", argv[1]);
-		perror("");
-		exit(1);
-	}
-	fseek(f, 0, SEEK_END);
-	size = ftell(f);
-	rewind(f);
-		
-	filedata = lw_malloc(size);
-		
-	bread = fread(filedata, 1, size, f);
-	if (bread < size)
-	{
-		fprintf(stderr, "Short read on file %s (%ld/%ld):", argv[1], bread, size);
-		perror("");
-		exit(1);
-	}
-			
-	fclose(f);
-		
-	if (!memcmp(filedata, "LWOBJ16", 8))
-	{
-		// read v0 LWOBJ16 file
-		read_lwobj16v0(filedata, size);
-	}
-	else
-	{
-		fprintf(stderr, "%s: unknown file format\n", argv[1]);
-		exit(1);
-	}
-	exit(0);
-}
-
-// this macro is used to bail out if we run off the end of the file data
-// while parsing - it keeps the code below cleaner
-#define NEXTBYTE()	do { cc++; if (cc > filesize) { fprintf(stderr, "***invalid file format\n"); exit(1); } } while (0)
-// this macro is used to refer to the current byte in the stream
-#define CURBYTE()	(filedata[cc < filesize ? cc : filesize - 1])
-// this one will leave the input pointer past the trailing NUL
-#define CURSTR()	read_lwobj16v0_str(&cc, &filedata, filesize)
-unsigned char *read_lwobj16v0_str(long *cc1, unsigned char **filedata1, long filesize)
-{
-	int cc = *cc1;
-	unsigned char *filedata = *filedata1;
-	unsigned char *fp;
-	fp = &CURBYTE();
-	while (CURBYTE())
-		NEXTBYTE();
-	NEXTBYTE();
-	*cc1 = cc;
-	*filedata1 = filedata;
-	return fp;
-}
-// the function below can be switched to dealing with data coming from a
-// source other than an in-memory byte pool by adjusting the input data
-// in "fn" and the above two macros
-void read_lwobj16v0(unsigned char *filedata, long filesize)
-{
-	unsigned char *fp;
-	long cc;
-	int val;
-	int bss;
-
-	static char *opernames[] = {
-		"?",
-		"PLUS",
-		"MINUS",
-		"TIMES",
-		"DIVIDE",
-		"MOD",
-		"INTDIV",
-		"BWAND",
-		"BWOR",
-		"BWXOR",
-		"AND",
-		"OR",
-		"NEG",
-		"COM"
-	};
-	static const int numopers = 13;
-		
-	// start reading *after* the magic number
-	cc = 8;
-	
-	while (1)
-	{
-		bss = 0;
-		
-		// bail out if no more sections
-		if (!(CURBYTE()))
-			break;
-		
-		fp = CURSTR();
-		
-		printf("SECTION %s\n", fp);
-		
-		// read flags
-		while (CURBYTE())
-		{
-			switch (CURBYTE())
-			{
-			case 0x01:
-				printf("    FLAG: BSS\n");
-				bss = 1;
-				break;
-
-			default:
-				printf("    FLAG: %02X (unknown)\n", CURBYTE());
-				break;
-			}
-			NEXTBYTE();
-		}
-		// skip NUL terminating flags
-		NEXTBYTE();
-		
-		printf("    Local symbols:\n");
-		// now parse the local symbol table
-		while (CURBYTE())
-		{
-			fp = CURSTR();
-
-			// fp is the symbol name
-			val = (CURBYTE()) << 8;
-			NEXTBYTE();
-			val |= (CURBYTE());
-			NEXTBYTE();
-			// val is now the symbol value
-			
-			printf("        %s=%04X\n", fp, val);
-			
-		}
-		// skip terminating NUL
-		NEXTBYTE();
-
-		printf("    Exported symbols\n");
-				
-		// now parse the exported symbol table
-		while (CURBYTE())
-		{
-			fp = CURSTR();
-
-			// fp is the symbol name
-			val = (CURBYTE()) << 8;
-			NEXTBYTE();
-			val |= (CURBYTE());
-			NEXTBYTE();
-			// val is now the symbol value
-			
-			printf("        %s=%04X\n", fp, val);
-		}
-		// skip terminating NUL
-		NEXTBYTE();
-		
-		// now parse the incomplete references and make a list of
-		// external references that need resolution
-		printf("    Incomplete references\n");
-		while (CURBYTE())
-		{
-			printf("        (");
-			// parse the expression
-			while (CURBYTE())
-			{
-				int tt = CURBYTE();
-				NEXTBYTE();
-				switch (tt)
-				{
-				case 0x01:
-					// 16 bit integer
-					tt = CURBYTE() << 8;
-					NEXTBYTE();
-					tt |= CURBYTE();
-					NEXTBYTE();
-					// normalize for negatives...
-					if (tt > 0x7fff)
-						tt -= 0x10000;
-					printf(" I16=%d", tt);
-					break;
-				
-				case 0x02:
-					// external symbol reference
-					printf(" ES=%s", CURSTR());
-					break;
-					
-				case 0x03:
-					// internal symbol reference
-					printf(" IS=%s", CURSTR());
-					break;
-				
-				case 0x04:
-					// operator
-					if (CURBYTE() > 0 && CURBYTE() <= numopers)
-						printf(" OP=%s", opernames[CURBYTE()]);
-					else
-						printf(" OP=?");
-					NEXTBYTE();
-					break;
-
-				case 0x05:
-					// section base reference (NULL internal reference is
-					// the section base address
-					printf(" SB");
-					break;
-					
-				default:
-					printf(" ERR");
-				}
-			}
-			// skip the NUL
-			NEXTBYTE();
-			
-			// fetch the offset
-			val = CURBYTE() << 8;
-			NEXTBYTE();
-			val |= CURBYTE() & 0xff;
-			NEXTBYTE();
-			printf(" ) @ %04X\n", val);
-		}
-		// skip the NUL terminating the relocations
-		NEXTBYTE();
-				
-		// now set code location and size and verify that the file
-		// contains data going to the end of the code (if !SECTION_BSS)
-		val = CURBYTE() << 8;
-		NEXTBYTE();
-		val |= CURBYTE();
-		NEXTBYTE();
-		
-		printf("    CODE %04X bytes", val);
-		
-		// skip the code if we're not in a BSS section
-		if (!bss)
-		{
-			int i;
-			for (i = 0; i < val; i++)
-			{
-				if (! (i % 16))
-				{
-					printf("\n    %04X ", i);
-				}
-				printf("%02X", CURBYTE());
-				NEXTBYTE();
-			}
-		}
-		printf("\n");
-	}
-}
--- a/lwlink-old/trunk/src/output.c	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,127 +0,0 @@
-/*
-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);
-	}
-}
--- a/lwlink-old/trunk/src/readfiles.c	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,304 +0,0 @@
-/*
-readfiles.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/>.
-
-
-Reads input files
-
-*/
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <argp.h>
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "lwlink.h"
-#include "util.h"
-
-void read_lwobj16v0(fileinfo_t *fn);
-
-/*
-The logic of reading the entire file into memory is simple. All the symbol
-names in the file are NUL terminated strings and can be used directly without
-making additional copies.
-*/
-void read_files(void)
-{
-	int i;
-	long size;
-	FILE *f;
-	long bread;
-	for (i = 0; i < ninputfiles; i++)
-	{
-		f = fopen(inputfiles[i] -> filename, "rb");
-		if (!f)
-		{
-			fprintf(stderr, "Can't open file %s:", inputfiles[i] -> filename);
-			perror("");
-			exit(1);
-		}
-		fseek(f, 0, SEEK_END);
-		size = ftell(f);
-		rewind(f);
-		
-		inputfiles[i] -> filedata = lw_malloc(size);
-		inputfiles[i] -> filesize = size;
-		
-		bread = fread(inputfiles[i] -> filedata, 1, size, f);
-		if (bread < size)
-		{
-			fprintf(stderr, "Short read on file %s (%ld/%ld):", inputfiles[i] -> filename, bread, size);
-			perror("");
-			exit(1);
-		}
-			
-		fclose(f);
-		
-		if (!memcmp(inputfiles[i] -> filedata, "LWOBJ16", 8))
-		{
-			// read v0 LWOBJ16 file
-			read_lwobj16v0(inputfiles[i]);
-		}
-		else
-		{
-			fprintf(stderr, "%s: unknown file format\n", inputfiles[i] -> filename);
-			exit(1);
-		}
-	}
-}
-
-// this macro is used to bail out if we run off the end of the file data
-// while parsing - it keeps the code below cleaner
-#define NEXTBYTE()	do { cc++; if (cc > fn -> filesize) { fprintf(stderr, "%s: invalid file format\n", fn -> filename); exit(1); } } while (0)
-// this macro is used to refer to the current byte in the stream
-#define CURBYTE()	(fn -> filedata[cc < fn -> filesize ? cc : fn -> filesize - 1])
-// this one will leave the input pointer past the trailing NUL
-#define CURSTR()	read_lwobj16v0_str(&cc, fn)
-unsigned char *read_lwobj16v0_str(long *cc1, fileinfo_t *fn)
-{
-	int cc = *cc1;
-	unsigned char *fp;
-	fp = &CURBYTE();
-	while (CURBYTE())
-		NEXTBYTE();
-	NEXTBYTE();
-	*cc1 = cc;
-	return fp;
-}
-// the function below can be switched to dealing with data coming from a
-// source other than an in-memory byte pool by adjusting the input data
-// in "fn" and the above two macros
-void read_lwobj16v0(fileinfo_t *fn)
-{
-	unsigned char *fp;
-	long cc;
-	section_t *s;
-	int val;
-	symtab_t *se;
-	
-	// start reading *after* the magic number
-	cc = 8;
-	
-	// init data
-	fn -> sections = NULL;
-	fn -> nsections = 0;
-
-	while (1)
-	{
-//		NEXTBYTE();
-		// bail out if no more sections
-		if (!(CURBYTE()))
-			break;
-		
-		fp = CURSTR();
-		
-		// we now have a section name in fp
-		// create new section entry
-		fn -> sections = lw_realloc(fn -> sections, sizeof(section_t) * (fn -> nsections + 1));
-		s = &(fn -> sections[fn -> nsections]);
-		fn -> nsections += 1;
-		
-		s -> localsyms = NULL;
-		s -> flags = 0;
-		s -> codesize = 0;
-		s -> name = fp;
-		s -> loadaddress = 0;
-		s -> localsyms = NULL;
-		s -> exportedsyms = NULL;
-		s -> incompletes = NULL;
-		s -> processed = 0;
-		s -> file = fn;
-		
-		// read flags
-		while (CURBYTE())
-		{
-			switch (CURBYTE())
-			{
-			case 0x01:
-				s -> flags |= SECTION_BSS;
-				break;
-
-			default:
-				fprintf(stderr, "%s (%s): unrecognized section flag %02X\n", fn -> filename, s -> name, (int)(CURBYTE()));
-				exit(1);
-			}
-			NEXTBYTE();
-		}
-		// skip NUL terminating flags
-		NEXTBYTE();
-		
-		// now parse the local symbol table
-		while (CURBYTE())
-		{
-			fp = CURSTR();
-
-			// fp is the symbol name
-			val = (CURBYTE()) << 8;
-			NEXTBYTE();
-			val |= (CURBYTE());
-			NEXTBYTE();
-			// val is now the symbol value
-			
-			// create symbol table entry
-			se = lw_malloc(sizeof(symtab_t));
-			se -> next = s -> localsyms;
-			s -> localsyms = se;
-			se -> sym = fp;
-			se -> offset = val;
-		}
-		// skip terminating NUL
-		NEXTBYTE();
-		
-		// now parse the exported symbol table
-		while (CURBYTE())
-		{
-			fp = CURSTR();
-
-			// fp is the symbol name
-			val = (CURBYTE()) << 8;
-			NEXTBYTE();
-			val |= (CURBYTE());
-			NEXTBYTE();
-			// val is now the symbol value
-			
-			// create symbol table entry
-			se = lw_malloc(sizeof(symtab_t));
-			se -> next = s -> exportedsyms;
-			s -> exportedsyms = se;
-			se -> sym = fp;
-			se -> offset = val;
-		}
-		// skip terminating NUL
-		NEXTBYTE();
-		
-		// now parse the incomplete references and make a list of
-		// external references that need resolution
-		while (CURBYTE())
-		{
-			reloc_t *rp;
-			lw_expr_term_t *term;
-			
-			// we have a reference
-			rp = lw_malloc(sizeof(reloc_t));
-			rp -> next = s -> incompletes;
-			s -> incompletes = rp;
-			rp -> offset = 0;
-			rp -> expr = lw_expr_stack_create();
-			
-			// parse the expression
-			while (CURBYTE())
-			{
-				int tt = CURBYTE();
-				NEXTBYTE();
-				switch (tt)
-				{
-				case 0x01:
-					// 16 bit integer
-					tt = CURBYTE() << 8;
-					NEXTBYTE();
-					tt |= CURBYTE();
-					NEXTBYTE();
-					// normalize for negatives...
-					if (tt > 0x7fff)
-						tt -= 0x10000;
-					term = lw_expr_term_create_int(tt);
-					break;
-				
-				case 0x02:
-					// external symbol reference
-					term = lw_expr_term_create_sym(CURSTR(), 0);
-					break;
-					
-				case 0x03:
-					// internal symbol reference
-					term = lw_expr_term_create_sym(CURSTR(), 1);
-					break;
-				
-				case 0x04:
-					// operator
-					term = lw_expr_term_create_oper(CURBYTE());
-					NEXTBYTE();
-					break;
-
-				case 0x05:
-					// section base reference (NULL internal reference is
-					// the section base address
-					term = lw_expr_term_create_sym(NULL, 1);
-					break;
-					
-				default:
-					fprintf(stderr, "%s (%s): bad relocation expression\n", fn -> filename, s -> name);
-					exit(1);
-				}
-				lw_expr_stack_push(rp -> expr, term);
-				lw_expr_term_free(term);
-			}
-			// skip the NUL
-			NEXTBYTE();
-			
-			// fetch the offset
-			rp -> offset = CURBYTE() << 8;
-			NEXTBYTE();
-			rp -> offset |= CURBYTE() & 0xff;
-			NEXTBYTE();
-		}
-		// skip the NUL terminating the relocations
-		NEXTBYTE();
-				
-		// now set code location and size and verify that the file
-		// contains data going to the end of the code (if !SECTION_BSS)
-		s -> codesize = CURBYTE() << 8;
-		NEXTBYTE();
-		s -> codesize |= CURBYTE();
-		NEXTBYTE();
-		
-		s -> code = &(CURBYTE());
-		
-		// skip the code if we're not in a BSS section
-		if (!(s -> flags & SECTION_BSS))
-		{
-			int i;
-			for (i = 0; i < s -> codesize; i++)
-				NEXTBYTE();
-		}
-	}
-}
--- a/lwlink-old/trunk/src/script.c	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,257 +0,0 @@
-/*
-script.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/>.
-
-
-Read and parse linker scripts
-*/
-
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "lwlink.h"
-#include "util.h"
-
-// the built-in DECB target linker script
-static char *decb_script =
-	"section init load 2000\n"
-	"section code\n"
-	"section *,!bss\n"
-	"section *,bss\n"
-	"entry 2000\n"
-	;
-
-// the built-in RAW target linker script
-static char *raw_script = 
-	"section init load 0000\n"
-	"section code\n"
-	"section *,!bss\n"
-	"section *,bss\n"
-	;
-
-// the "simple" script
-static char *simple_script = 
-	"section *,!bss\n"
-	"section *,bss\n"
-	;
-
-linkscript_t linkscript = { 0, NULL, -1 };
-
-void setup_script()
-{
-	char *script, *oscript;
-	long size;
-
-	// read the file if needed
-	if (scriptfile)
-	{
-		FILE *f;
-		long bread;
-		
-		f = fopen(scriptfile, "rb");
-		if (!f)
-		{
-			fprintf(stderr, "Can't open file %s:", scriptfile);
-			perror("");
-			exit(1);
-		}
-		fseek(f, 0, SEEK_END);
-		size = ftell(f);
-		rewind(f);
-		
-		script = lw_malloc(size + 2);
-		
-		bread = fread(script, 1, size, f);
-		if (bread < size)
-		{
-			fprintf(stderr, "Short read on file %s (%ld/%ld):", scriptfile, bread, size);
-			perror("");
-			exit(1);
-		}
-		fclose(f);
-		
-		script[size] = '\n';
-		script[size + 1] = '\0';
-	}
-	else
-	{
-		// fetch defaults based on output mode
-		switch (outformat)
-		{
-		case OUTPUT_RAW:
-			script = raw_script;
-			break;
-		
-		case OUTPUT_DECB:
-			script = decb_script;
-			break;
-		
-		default:
-			script = simple_script;
-			break;
-		}
-		
-		size = strlen(script);
-	}
-
-	oscript = script;
-	// now parse the script file
-	while (*script)
-	{
-		char *ptr, *ptr2, *line;
-
-		for (ptr = script; *ptr && *ptr != '\n' && *ptr != '\r'; ptr++)
-			/* do nothing */ ;
-		
-		line = lw_malloc(ptr - script + 1);
-		memcpy(line, script, ptr - script);
-		line[ptr - script] = '\0';
-
-		// skip line terms
-		for (script = ptr + 1; *script == '\n' || *script == '\r'; script++)
-			/* do nothing */ ;
-		
-		// ignore leading whitespace
-		for (ptr = line; *ptr && isspace(*ptr); ptr++)
-			/* do nothing */ ;
-		
-		// ignore blank lines
-		if (!*ptr)
-			continue;
-		
-		for (ptr = line; *ptr && !isspace(*ptr); ptr++)
-			/* do nothing */ ;
-		
-		// now ptr points to the char past the first word
-		// NUL it out
-		if (*ptr)
-			*ptr++ = '\0';
-		
-		// skip spaces after the first word
-		for ( ; *ptr && isspace(*ptr); ptr++)
-			/* do nothing */ ;
-		
-		if (!strcmp(line, "pad"))
-		{
-			// padding
-			// parse the hex number and stow it
-			linkscript.padsize = strtol(ptr, NULL, 16);
-			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
-			// parse out the section name and flags
-			for (ptr2 = ptr; *ptr2 && !isspace(*ptr2); ptr2++)
-				/* do nothing */ ;
-			
-			if (*ptr2)
-				*ptr2++ = '\0';
-			
-			while (*ptr2 && isspace(*ptr2))
-				ptr2++;
-				
-			// ptr now points to the section name and flags and ptr2
-			// to the first non-space character following
-			
-			// then look for "load <addr>" clause
-			if (*ptr2)
-			{
-				if (!strncmp(ptr2, "load", 4))
-				{
-					ptr2 += 4;
-					while (*ptr2 && isspace(*ptr2))
-						ptr2++;
-					
-				}
-				else
-				{
-					fprintf(stderr, "%s: bad script\n", scriptfile);
-					exit(1);
-				}
-			}
-			
-			// now ptr2 points to the load address if there is one
-			// or NUL if not
-			linkscript.lines = lw_realloc(linkscript.lines, sizeof(struct scriptline_s) * (linkscript.nlines + 1));
-
-			linkscript.lines[linkscript.nlines].noflags = 0;
-			linkscript.lines[linkscript.nlines].yesflags = 0;
-			if (*ptr2)
-				linkscript.lines[linkscript.nlines].loadat = strtol(ptr2, NULL, 16);
-			else
-				linkscript.lines[linkscript.nlines].loadat = -1;
-			for (ptr2 = ptr; *ptr2 && *ptr2 != ','; ptr2++)
-				/* do nothing */ ;
-			if (*ptr2)
-			{
-				*ptr2++ = '\0';
-				if (!strcmp(ptr2, "!bss"))
-				{
-					linkscript.lines[linkscript.nlines].noflags = SECTION_BSS;
-				}
-				else if (!strcmp(ptr2, "bss"))
-				{
-					linkscript.lines[linkscript.nlines].yesflags = SECTION_BSS;
-				}
-				else
-				{
-					fprintf(stderr, "%s: bad script\n", scriptfile);
-					exit(1);
-				}
-			}
-			if (ptr[0] == '*' && ptr[1] == '\0')
-				linkscript.lines[linkscript.nlines].sectname = NULL;
-			else
-				linkscript.lines[linkscript.nlines].sectname = lw_strdup(ptr);
-			linkscript.nlines++;
-		}
-		else
-		{
-			fprintf(stderr, "%s: bad script\n", scriptfile);
-			exit(1);
-		}
-		lw_free(line);
-	}
-	
-	if (scriptfile)
-		lw_free(oscript);
-}
--- a/lwlink-old/trunk/src/util.c	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,87 +0,0 @@
-/*
-util.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/>.
-*/
-
-/*
-Utility functions
-*/
-
-#define __util_c_seen__
-
-#include <malloc.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "util.h"
-
-void *lw_malloc(int size)
-{
-	void *ptr;
-	
-	ptr = malloc(size);
-	if (!ptr)
-	{
-		// bail out; memory allocation error
-		fprintf(stderr, "lw_malloc(): Memory allocation error\n");
-		exit(1);
-	}
-	return ptr;
-}
-
-void *lw_realloc(void *optr, int size)
-{
-	void *ptr;
-	
-	if (size == 0)
-	{
-		lw_free(optr);
-		return;
-	}
-	
-	ptr = realloc(optr, size);
-	if (!ptr)
-	{
-		fprintf(stderr, "lw_realloc(): memory allocation error\n");
-		exit(1);
-	}
-}
-
-void lw_free(void *ptr)
-{
-	if (ptr)
-		free(ptr);
-}
-
-char *lw_strdup(const char *s)
-{
-	char *d;
-	
-	if (!s)
-		return NULL;
-
-	d = strdup(s);
-	if (!d)
-	{
-		fprintf(stderr, "lw_strdup(): memory allocation error\n");
-		exit(1);
-	}
-	
-	return d;
-}
--- a/lwlink-old/trunk/src/util.h	Wed Jan 28 05:58:32 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-/*
-util.h
-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/>.
-*/
-
-/*
-Utility functions
-*/
-
-#ifndef __util_h_seen__
-#define __util_h_seen__
-
-#ifndef __util_c_seen__
-#define __util_E__ extern
-#else
-#define __util_E__
-#endif
-
-// allocate memory
-__util_E__ void *lw_malloc(int size);
-__util_E__ void lw_free(void *ptr);
-__util_E__ void *lw_realloc(void *optr, int size);
-
-// string stuff
-__util_E__ char *lw_strdup(const char *s);
-
-#undef __util_E__
-
-#endif // __util_h_seen__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/Makefile.am	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,4 @@
+bin_PROGRAMS = lwlink lwobjdump
+lwlink_SOURCES = main.c lwlink.c util.c readfiles.c expr.c script.c link.c output.c
+lwobjdump_SOURCES = objdump.c util.c
+EXTRA_DIST = lwlink.h util.h expr.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/expr.c	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,356 @@
+/*
+expr.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/>.
+*/
+
+/*
+This file contains the actual expression evaluator
+*/
+
+#define __expr_c_seen__
+
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "expr.h"
+#include "util.h"
+
+lw_expr_stack_t *lw_expr_stack_create(void)
+{
+	lw_expr_stack_t *s;
+	
+	s = lw_malloc(sizeof(lw_expr_stack_t));
+	s -> head = NULL;
+	s -> tail = NULL;
+	return s;
+}
+
+void lw_expr_stack_free(lw_expr_stack_t *s)
+{
+	while (s -> head)
+	{
+		s -> tail = s -> head;
+		s -> head = s -> head -> next;
+		lw_expr_term_free(s -> tail -> term);
+		lw_free(s -> tail);
+	}
+	lw_free(s);
+}
+
+void lw_expr_term_free(lw_expr_term_t *t)
+{
+	if (t)
+	{
+		if (t -> term_type == LW_TERM_SYM)
+			lw_free(t -> symbol);
+		lw_free(t);
+	}
+}
+
+lw_expr_term_t *lw_expr_term_create_oper(int oper)
+{
+	lw_expr_term_t *t;
+
+	t = lw_malloc(sizeof(lw_expr_term_t));
+	t -> term_type = LW_TERM_OPER;
+	t -> value = oper;
+	return t;
+}
+
+lw_expr_term_t *lw_expr_term_create_int(int val)
+{
+	lw_expr_term_t *t;
+	
+	t = lw_malloc(sizeof(lw_expr_term_t));
+	t -> term_type = LW_TERM_INT;
+	t -> value = val;
+	return t;
+}
+
+lw_expr_term_t *lw_expr_term_create_sym(char *sym, int symtype)
+{
+	lw_expr_term_t *t;
+	
+	t = lw_malloc(sizeof(lw_expr_term_t));
+	t -> term_type = LW_TERM_SYM;
+	t -> symbol = lw_strdup(sym);
+	t -> value = symtype;
+	return t;
+}
+
+lw_expr_term_t *lw_expr_term_dup(lw_expr_term_t *t)
+{
+	switch (t -> term_type)
+	{
+	case LW_TERM_INT:
+		return lw_expr_term_create_int(t -> value);
+		
+	case LW_TERM_OPER:
+		return lw_expr_term_create_oper(t -> value);
+		
+	case LW_TERM_SYM:
+		return lw_expr_term_create_sym(t -> symbol, t -> value);
+	
+	default:
+		exit(1);
+	}
+// can't get here
+}
+
+void lw_expr_stack_push(lw_expr_stack_t *s, lw_expr_term_t *t)
+{
+	lw_expr_stack_node_t *n;
+
+	if (!s)
+	{
+		exit(1);
+	}
+	
+	n = lw_malloc(sizeof(lw_expr_stack_node_t));
+	n -> next = NULL;
+	n -> prev = s -> tail;
+	n -> term = lw_expr_term_dup(t);
+	
+	if (s -> head)
+	{
+		s -> tail -> next = n;
+		s -> tail = n;
+	}
+	else
+	{
+		s -> head = n;
+		s -> tail = n;
+	}
+}
+
+lw_expr_term_t *lw_expr_stack_pop(lw_expr_stack_t *s)
+{
+	lw_expr_term_t *t;
+	lw_expr_stack_node_t *n;
+	
+	if (!(s -> tail))
+		return NULL;
+	
+	n = s -> tail;
+	s -> tail = n -> prev;
+	if (!(n -> prev))
+	{
+		s -> head = NULL;
+	}
+	
+	t = n -> term;
+	n -> term = NULL;
+	
+	lw_free(n);
+	
+	return t;
+}
+
+/*
+take an expression stack s and scan for operations that can be completed
+
+return -1 on error, 0 on no error
+
+possible errors are: division by zero or unknown operator
+
+theory of operation:
+
+scan the stack for an operator which has two constants preceding it (binary)
+or 1 constant preceding it (unary) and if found, perform the calculation
+and replace the operator and its operands with the result
+
+repeat the scan until no futher simplications are found or if there are no
+further operators or only a single term remains
+
+*/
+int lw_expr_reval(lw_expr_stack_t *s, lw_expr_stack_t *(*sfunc)(char *sym, int stype, void *state), void *state)
+{
+	lw_expr_stack_node_t *n, *n2;
+	lw_expr_stack_t *ss;
+	int c;
+	
+next_iter_sym:
+	// resolve symbols
+	// symbols that do not resolve to an expression are left alone
+	for (c = 0, n = s -> head; n; n = n -> next)
+	{
+		if (n -> term -> term_type == LW_TERM_SYM)
+		{
+			ss = sfunc(n -> term -> symbol, n -> term -> value, state);
+			if (ss)
+			{
+				c++;
+				// splice in the result stack
+				if (n -> prev)
+				{
+					n -> prev -> next = ss -> head;
+				}
+				else
+				{
+					s -> head = ss -> head;
+				}
+				ss -> head -> prev = n -> prev;
+				ss -> tail -> next = n -> next;
+				if (n -> next)
+				{
+					n -> next -> prev = ss -> tail;
+				}
+				else
+				{
+					s -> tail = ss -> tail;
+				}
+				lw_expr_term_free(n -> term);
+				lw_free(n);
+				n = ss -> tail;
+				
+				ss -> head = NULL;
+				ss -> tail = NULL;
+				lw_expr_stack_free(ss);
+			}
+		}
+	}
+	if (c)
+		goto next_iter_sym;
+
+next_iter:	
+	// a single term
+	if (s -> head == s -> tail)
+		return 0;
+	
+	// search for an operator
+	for (n = s -> head; n; n = n -> next)
+	{
+		if (n -> term -> term_type == LW_TERM_OPER)
+		{
+			if (n -> term -> value == LW_OPER_NEG
+				|| n -> term -> value == LW_OPER_COM
+				)
+			{
+				// unary operator
+				if (n -> prev && n -> prev -> term -> term_type == LW_TERM_INT)
+				{
+					// a unary operator we can resolve
+					// we do the op then remove the term "n" is pointing at
+					if (n -> term -> value == LW_OPER_NEG)
+					{
+						n -> prev -> term -> value = -(n -> prev -> term -> value);
+					}
+					else if (n -> term -> value == LW_OPER_COM)
+					{
+						n -> prev -> term -> value = ~(n -> prev -> term -> value);
+					}
+					n -> prev -> next = n -> next;
+					if (n -> next)
+						n -> next -> prev = n -> prev;
+					else
+						s -> tail = n -> prev;	
+					
+					lw_expr_term_free(n -> term);
+					lw_free(n);
+					break;
+				}
+			}
+			else
+			{
+				// binary operator
+				if (n -> prev && n -> prev -> prev && n -> prev -> term -> term_type == LW_TERM_INT && n -> prev -> prev -> term -> term_type == LW_TERM_INT)
+				{
+					// a binary operator we can resolve
+					switch (n -> term -> value)
+					{
+					case LW_OPER_PLUS:
+						n -> prev -> prev -> term -> value += n -> prev -> term -> value;
+						break;
+
+					case LW_OPER_MINUS:
+						n -> prev -> prev -> term -> value -= n -> prev -> term -> value;
+						break;
+
+					case LW_OPER_TIMES:
+						n -> prev -> prev -> term -> value *= n -> prev -> term -> value;
+						break;
+
+					case LW_OPER_DIVIDE:
+						if (n -> prev -> term -> value == 0)
+							return -1;
+						n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
+						break;
+
+					case LW_OPER_MOD:
+						if (n -> prev -> term -> value == 0)
+							return -1;
+						n -> prev -> prev -> term -> value %= n -> prev -> term -> value;
+						break;
+
+					case LW_OPER_INTDIV:
+						if (n -> prev -> term -> value == 0)
+							return -1;
+						n -> prev -> prev -> term -> value /= n -> prev -> term -> value;
+						break;
+
+					case LW_OPER_BWAND:
+						n -> prev -> prev -> term -> value &= n -> prev -> term -> value;
+						break;
+
+					case LW_OPER_BWOR:
+						n -> prev -> prev -> term -> value |= n -> prev -> term -> value;
+						break;
+
+					case LW_OPER_BWXOR:
+						n -> prev -> prev -> term -> value ^= n -> prev -> term -> value;
+						break;
+
+					case LW_OPER_AND:
+						n -> prev -> prev -> term -> value = (n -> prev -> term -> value && n -> prev -> prev -> term -> value) ? 1 : 0;
+						break;
+
+					case LW_OPER_OR:
+						n -> prev -> prev -> term -> value = (n -> prev -> term -> value || n -> prev -> prev -> term -> value) ? 1 : 0;
+						break;
+
+					default:
+						// return error if unknown operator!
+						return -1;
+					}
+
+					// now remove the two unneeded entries from the stack
+					n -> prev -> prev -> next = n -> next;
+					if (n -> next)
+						n -> next -> prev = n -> prev -> prev;
+					else
+						s -> tail = n -> prev -> prev;	
+					
+					lw_expr_term_free(n -> term);
+					lw_expr_term_free(n -> prev -> term);
+					lw_free(n -> prev);
+					lw_free(n);
+					break;
+				}
+			}
+		}
+	}
+	// note for the terminally confused about dynamic memory and pointers:
+	// n will not be NULL even after the lw_free calls above so
+	// this test will still work (n will be a dangling pointer)
+	// (n will only be NULL if we didn't find any operators to simplify)
+	if (n)
+		goto next_iter;
+	
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/expr.h	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,107 @@
+/*
+expr.h
+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/>.
+*/
+
+/*
+Definitions for expression evaluator
+*/
+
+#ifndef __expr_h_seen__
+#define __expr_h_seen__
+
+#ifndef __expr_c_seen__
+#define __expr_E__ extern
+#else
+#define __expr_E__
+#endif
+
+// term types
+#define LW_TERM_NONE		0
+#define LW_TERM_OPER		1	// an operator
+#define LW_TERM_INT		2	// 32 bit signed integer
+#define LW_TERM_SYM		3	// symbol reference
+
+// operator types
+#define LW_OPER_NONE		0
+#define LW_OPER_PLUS		1	// +
+#define LW_OPER_MINUS	2	// -
+#define LW_OPER_TIMES	3	// *
+#define LW_OPER_DIVIDE	4	// /
+#define LW_OPER_MOD		5	// %
+#define LW_OPER_INTDIV	6	// \ (don't end line with \)
+#define LW_OPER_BWAND	7	// bitwise AND
+#define LW_OPER_BWOR		8	// bitwise OR
+#define LW_OPER_BWXOR	9	// bitwise XOR
+#define LW_OPER_AND		10	// boolean AND
+#define LW_OPER_OR		11	// boolean OR
+#define LW_OPER_NEG		12	// - unary negation (2's complement)
+#define LW_OPER_COM		13	// ^ unary 1's complement
+
+
+// term structure
+typedef struct lw_expr_term_s
+{
+	int term_type;		// type of term (see above)
+	char *symbol;		// name of a symbol
+	int value;			// value of the term (int) or operator number (OPER)
+} lw_expr_term_t;
+
+// type for an expression evaluation stack
+typedef struct lw_expr_stack_node_s lw_expr_stack_node_t;
+struct lw_expr_stack_node_s
+{
+	lw_expr_term_t		*term;
+	lw_expr_stack_node_t	*prev;
+	lw_expr_stack_node_t	*next;	
+};
+
+typedef struct lw_expr_stack_s
+{
+	lw_expr_stack_node_t *head;
+	lw_expr_stack_node_t *tail;
+} lw_expr_stack_t;
+
+__expr_E__ void lw_expr_term_free(lw_expr_term_t *t);
+__expr_E__ lw_expr_term_t *lw_expr_term_create_oper(int oper);
+__expr_E__ lw_expr_term_t *lw_expr_term_create_sym(char *sym, int symtype);
+__expr_E__ lw_expr_term_t *lw_expr_term_create_int(int val);
+__expr_E__ lw_expr_term_t *lw_expr_term_dup(lw_expr_term_t *t);
+
+__expr_E__ void lw_expr_stack_free(lw_expr_stack_t *s);
+__expr_E__ lw_expr_stack_t *lw_expr_stack_create(void);
+
+__expr_E__ void lw_expr_stack_push(lw_expr_stack_t *s, lw_expr_term_t *t);
+__expr_E__ lw_expr_term_t *lw_expr_stack_pop(lw_expr_stack_t *s);
+
+// simplify expression
+__expr_E__ int lw_expr_reval(lw_expr_stack_t *s, lw_expr_stack_t *(*sfunc)(char *sym, int symtype, void *state), void *state);
+
+// useful macros
+// is the expression "simple" (one term)?
+#define lw_expr_is_simple(s) ((s) -> head == (s) -> tail)
+
+// is the expression constant?
+#define lw_expr_is_constant(s) (lw_expr_is_simple(s) && (!((s) -> head) || (s) -> head -> term -> term_type == LW_TERM_INT))
+
+// get the constant value of an expression or 0 if not constant
+#define lw_expr_get_value(s) (lw_expr_is_constant(s) ? ((s) -> head ? (s) -> head -> term -> value : 0) : 0)
+
+#undef __expr_E__
+
+#endif // __expr_h_seen__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/link.c	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,265 @@
+/*
+link.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/>.
+
+
+Resolve section and symbol addresses; handle incomplete references
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "expr.h"
+#include "lwlink.h"
+#include "util.h"
+
+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
+void resolve_sections(void)
+{
+	int laddr = 0;
+	int ln, sn, fn;
+	
+	for (ln = 0; ln < linkscript.nlines; ln++)
+	{
+//	printf("Linker script line %d: '%s', %04X, %d, %d\n", ln, linkscript.lines[ln].sectname, linkscript.lines[ln].loadat, linkscript.lines[ln].yesflags, linkscript.lines[ln].noflags);
+		if (linkscript.lines[ln].sectname)
+		{
+			int f = 0;
+			// named section
+			// look for all instances of a section by the specified name
+			// and resolve base addresses and add to the list
+			for (fn = 0; fn < ninputfiles; fn++)
+			{
+				for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
+				{
+//				printf("  Considering %s:%s\n", inputfiles[fn]->filename, inputfiles[fn]->sections[sn].name);
+					if (!strcmp(linkscript.lines[ln].sectname, inputfiles[fn] -> sections[sn].name))
+					{
+						// we have a match
+						sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
+						sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
+						
+						inputfiles[fn] -> sections[sn].processed = 1;
+						if (!f && linkscript.lines[ln].loadat >= 0)
+						{
+							f = 1;
+							sectlist[nsects].forceaddr = 1;
+							laddr = linkscript.lines[ln].loadat;
+						}
+						else
+						{
+							sectlist[nsects].forceaddr = 0;
+						}
+						inputfiles[fn] -> sections[sn].loadaddress = laddr;
+						laddr += inputfiles[fn] -> sections[sn].codesize;
+						nsects++;
+					}
+				}
+			}
+		}
+		else
+		{
+			// wildcard section
+			// look for all sections not yet processed that match flags
+
+			int f = 0;
+			int fn0, sn0;
+			char *sname;
+			
+			// named section
+			// look for all instances of a section by the specified name
+			// and resolve base addresses and add to the list
+			for (fn0 = 0; fn0 < ninputfiles; fn0++)
+			{
+				for (sn0 = 0; sn0 < inputfiles[fn0] -> nsections; sn0++)
+				{
+					// ignore if the "no flags" bit says to
+					if (linkscript.lines[ln].noflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].noflags))
+						continue;
+					// ignore unless the yes flags tell us not to
+					if (linkscript.lines[ln].yesflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].yesflags == 0))
+						continue;
+					if (inputfiles[fn0] -> sections[sn0].processed == 0)
+					{
+						sname = inputfiles[fn0] -> sections[sn0].name;
+						for (fn = 0; fn < ninputfiles; fn++)
+						{
+							for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
+							{
+								if (!strcmp(sname, inputfiles[fn] -> sections[sn].name))
+								{
+									// we have a match
+									sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
+									sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
+						
+									inputfiles[fn] -> sections[sn].processed = 1;
+									if (!f && linkscript.lines[ln].loadat >= 0)
+									{
+										f = 1;
+										sectlist[nsects].forceaddr = 1;
+										laddr = linkscript.lines[ln].loadat;
+									}
+									else
+									{
+										sectlist[nsects].forceaddr = 0;
+									}
+									inputfiles[fn] -> sections[sn].loadaddress = laddr;
+									laddr += inputfiles[fn] -> sections[sn].codesize;
+									nsects++;
+								}
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+	
+	// theoretically, all the base addresses are set now
+}
+
+// resolve all incomplete references now
+// anything that is unresolvable at this stage will throw an error
+// because we know the load address of every section now
+lw_expr_stack_t *resolve_sym(char *sym, int symtype, void *state)
+{
+	section_t *sect = state;
+	lw_expr_term_t *term;
+	int val = 0, i, fn;
+	lw_expr_stack_t *s;
+	symtab_t *se;
+	
+	if (symtype == 1)
+	{
+		// local symbol
+		if (!sym)
+		{
+			val = sect -> loadaddress;
+			goto out;
+		}
+		
+		// start with this section
+		for (se = sect -> localsyms; se; se = se -> next)
+		{
+			if (!strcmp(se -> sym, sym))
+			{
+				val = se -> offset + sect -> loadaddress;
+				goto out;
+			}
+		}
+		// not in this section - check all sections in this file
+		for (i = 0; i < sect -> file -> nsections; i++)
+		{
+			for (se = sect -> file -> sections[i].localsyms; se; se = se -> next)
+			{
+				if (!strcmp(se -> sym, sym))
+				{
+					val = se -> offset + sect -> file -> sections[i].loadaddress;
+					goto out;
+				}
+			}
+		}
+		// not found
+		fprintf(stderr, "Local symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
+		exit(1);
+	}
+	else
+	{
+		// external symbol
+		// read all files in order until found (or not found)
+		for (fn = 0; fn < ninputfiles; fn++)
+		{
+			for (i = 0; i < inputfiles[fn] -> nsections; i++)
+			{
+				for (se = inputfiles[fn] -> sections[i].exportedsyms; se; se = se -> next)
+				{
+					if (!strcmp(sym, se -> sym))
+					{
+						val = se -> offset + inputfiles[fn] -> sections[i].loadaddress;
+						goto out;
+					}
+				}
+			}
+		}
+		if (sect)
+		{
+			fprintf(stderr, "External symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
+		}
+		else
+		{
+			fprintf(stderr, "External symbol %s not found\n", sym);
+		}
+		exit(1);
+	}
+	fprintf(stderr, "Shouldn't ever get here!!!\n");
+	exit(88);
+out:
+	s = lw_expr_stack_create();
+	term = lw_expr_term_create_int(val & 0xffff);
+	lw_expr_stack_push(s, term);
+	lw_expr_term_free(term);
+	return s;
+}
+
+void resolve_references(void)
+{
+	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; sn < nsects; sn++)
+	{
+		for (rl = sectlist[sn].ptr -> incompletes; rl; rl = rl -> next)
+		{
+			// do a "simplify" on the expression
+			rval = lw_expr_reval(rl -> expr, resolve_sym, sectlist[sn].ptr);
+
+			// is it constant? error out if not
+			if (rval != 0 || !lw_expr_is_constant(rl -> expr))
+			{
+				fprintf(stderr, "Incomplete reference at %s:%s:%02X\n", sectlist[sn].ptr -> file -> filename, sectlist[sn].ptr -> name, rl -> offset);
+				exit(1);
+			}
+			
+			// put the value into the relocation address
+			rval = lw_expr_get_value(rl -> expr);
+			sectlist[sn].ptr -> code[rl -> offset] = (rval >> 8) & 0xff;
+			sectlist[sn].ptr -> code[rl -> offset + 1] = rval & 0xff;
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/lwlink.c	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,52 @@
+/*
+lwlink.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/>.
+
+
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#define __lwlink_c_seen__
+
+#include <argp.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "lwlink.h"
+#include "util.h"
+
+int debug_level = 0;
+int outformat = OUTPUT_DECB;
+char *outfile = NULL;
+char *scriptfile = NULL;
+
+fileinfo_t **inputfiles = NULL;
+int ninputfiles = 0;
+
+void add_input_file(char *fn)
+{
+	inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
+	inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
+	inputfiles[ninputfiles++] -> filename = lw_strdup(fn);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/lwlink.h	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,130 @@
+/*
+lwlink.h
+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/>.
+
+Contains the main defs used by the linker
+*/
+
+
+#ifndef __lwlink_h_seen__
+#define __lwlink_h_seen__
+
+#include "expr.h"
+
+#define OUTPUT_DECB		0	// DECB multirecord format
+#define OUTPUT_RAW		1	// raw sequence of bytes
+
+typedef struct symtab_s symtab_t;
+struct symtab_s
+{
+	unsigned char *sym;		// symbol name
+	int offset;				// local offset
+//	int realval;			// resolved value
+	symtab_t *next;			// next symbol
+};
+
+typedef struct reloc_s reloc_t;
+struct reloc_s
+{
+	int offset;				// where in the section
+	lw_expr_stack_t *expr;	// the expression to calculate it
+	reloc_t *next;			// ptr to next relocation
+};
+
+typedef struct fileinfo_s fileinfo_t;
+
+#define SECTION_BSS		1
+typedef struct
+{
+	unsigned char *name;	// name of the section
+	int flags;				// section flags
+	int codesize;			// size of the code
+	unsigned char *code;	// pointer to the code
+	int loadaddress;		// the actual load address of the section
+	int processed;			// was the section processed yet?
+		
+	symtab_t *localsyms;	// local symbol table
+	symtab_t *exportedsyms;	// exported symbols table
+	
+	reloc_t *incompletes;	// table of incomplete references
+	
+	fileinfo_t *file;		// the file we are in
+} section_t;
+
+struct fileinfo_s
+{
+	char *filename;
+	unsigned char *filedata;
+	long filesize;
+	section_t *sections;
+	int nsections;
+	
+};
+
+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__
+
+extern int debug_level;
+extern int outformat;
+extern char *outfile;
+extern int ninputfiles;
+extern fileinfo_t **inputfiles;
+extern char *scriptfile;
+
+#define __lwlink_E__ extern
+#else
+#define __lwlink_E__
+#endif // __lwlink_c_seen__
+
+__lwlink_E__ void add_input_file(char *fn);
+
+#undef __lwlink_E__
+
+struct scriptline_s
+{
+	char *sectname;				// name of section, NULL for wildcard
+	int loadat;					// address to load at (or -1)
+	int noflags;				// flags to NOT have
+	int yesflags;				// flags to HAVE
+};
+
+typedef struct
+{
+	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__
+extern linkscript_t linkscript;
+#endif
+
+#endif //__lwlink_h_seen__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/main.c	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,155 @@
+/*
+main.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/>.
+
+
+Implements the program startup code
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <argp.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "lwlink.h"
+
+// command line option handling
+const char *argp_program_version = PACKAGE_STRING;
+const char *argp_program_bug_address = PACKAGE_BUGREPORT;
+
+static error_t parse_opts(int key, char *arg, struct argp_state *state)
+{
+	switch (key)
+	{
+	case 'o':
+		// output
+		outfile = arg;
+		break;
+	
+	case 's':
+		// script file
+		scriptfile = arg;
+		break;
+
+	case 'd':
+		// debug
+		debug_level++;
+		break;
+	
+	case 'b':
+		// decb output
+		outformat = OUTPUT_DECB;
+		break;
+	
+	case 'r':
+		// raw binary output
+		outformat = OUTPUT_RAW;
+		break;
+	
+	case 'f':
+		// output format
+		if (!strcasecmp(arg, "decb"))
+			outformat = OUTPUT_DECB;
+		else if (!strcasecmp(arg, "raw"))
+			outformat = OUTPUT_RAW;
+		else
+		{
+			fprintf(stderr, "Invalid output format: %s\n", arg);
+			exit(1);
+		}
+		break;
+	case ARGP_KEY_END:
+		// done; sanity check
+		if (!outfile)
+			outfile = "a.out";
+		break;
+	
+	case ARGP_KEY_ARG:
+		add_input_file(arg);
+		break;
+		
+	default:
+		return ARGP_ERR_UNKNOWN;
+	}
+	return 0;
+}
+
+static struct argp_option options[] =
+{
+	{ "output",		'o',	"FILE",	0,
+				"Output to FILE"},
+	{ "debug",		'd',	0,		0,
+				"Set debug mode"},
+	{ "format",		'f',	"TYPE",	0,
+				"Select output format: decb, raw, obj"},
+	{ "decb",		'b',	0,		0,
+				"Generate DECB .bin format output, equivalent of --format=decb"},
+	{ "raw",		'r',	0,		0,
+				"Generate raw binary format output, equivalent of --format=raw"},
+	{ "script",		's',	"FILE",		0,
+				"Specify the linking script (overrides the build in defaults)"},
+	{ 0 }
+};
+
+static struct argp argp =
+{
+	options,
+	parse_opts,
+	"<input file> ...",
+	"LWLINK, a HD6309 and MC6809 cross-linker"
+};
+
+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
+int main(int argc, char **argv)
+{
+	argp_parse(&argp, argc, argv, 0, 0, NULL);
+	if (ninputfiles == 0)
+	{
+		fprintf(stderr, "No input files\n");
+		exit(1);
+	}
+
+	// handle the linker script
+	setup_script();
+
+	// read the input files
+	read_files();
+	
+	// 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/objdump.c	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,303 @@
+/*
+objdump.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/>.
+
+
+A standalone program to dump an object file in a text form to stdout
+
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "util.h"
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+void read_lwobj16v0(unsigned char *filedata, long filesize);
+
+/*
+The logic of reading the entire file into memory is simple. All the symbol
+names in the file are NUL terminated strings and can be used directly without
+making additional copies.
+*/
+int main(int argc, char **argv)
+{
+	int i;
+	long size;
+	FILE *f;
+	long bread;
+	unsigned char *filedata;
+	
+	if (argc != 2)
+	{
+		fprintf(stderr, "Must specify exactly one input file.\n");
+		exit(1);
+	}
+
+	f = fopen(argv[1], "rb");
+	if (!f)
+	{
+		fprintf(stderr, "Can't open file %s:", argv[1]);
+		perror("");
+		exit(1);
+	}
+	fseek(f, 0, SEEK_END);
+	size = ftell(f);
+	rewind(f);
+		
+	filedata = lw_malloc(size);
+		
+	bread = fread(filedata, 1, size, f);
+	if (bread < size)
+	{
+		fprintf(stderr, "Short read on file %s (%ld/%ld):", argv[1], bread, size);
+		perror("");
+		exit(1);
+	}
+			
+	fclose(f);
+		
+	if (!memcmp(filedata, "LWOBJ16", 8))
+	{
+		// read v0 LWOBJ16 file
+		read_lwobj16v0(filedata, size);
+	}
+	else
+	{
+		fprintf(stderr, "%s: unknown file format\n", argv[1]);
+		exit(1);
+	}
+	exit(0);
+}
+
+// this macro is used to bail out if we run off the end of the file data
+// while parsing - it keeps the code below cleaner
+#define NEXTBYTE()	do { cc++; if (cc > filesize) { fprintf(stderr, "***invalid file format\n"); exit(1); } } while (0)
+// this macro is used to refer to the current byte in the stream
+#define CURBYTE()	(filedata[cc < filesize ? cc : filesize - 1])
+// this one will leave the input pointer past the trailing NUL
+#define CURSTR()	read_lwobj16v0_str(&cc, &filedata, filesize)
+unsigned char *read_lwobj16v0_str(long *cc1, unsigned char **filedata1, long filesize)
+{
+	int cc = *cc1;
+	unsigned char *filedata = *filedata1;
+	unsigned char *fp;
+	fp = &CURBYTE();
+	while (CURBYTE())
+		NEXTBYTE();
+	NEXTBYTE();
+	*cc1 = cc;
+	*filedata1 = filedata;
+	return fp;
+}
+// the function below can be switched to dealing with data coming from a
+// source other than an in-memory byte pool by adjusting the input data
+// in "fn" and the above two macros
+void read_lwobj16v0(unsigned char *filedata, long filesize)
+{
+	unsigned char *fp;
+	long cc;
+	int val;
+	int bss;
+
+	static char *opernames[] = {
+		"?",
+		"PLUS",
+		"MINUS",
+		"TIMES",
+		"DIVIDE",
+		"MOD",
+		"INTDIV",
+		"BWAND",
+		"BWOR",
+		"BWXOR",
+		"AND",
+		"OR",
+		"NEG",
+		"COM"
+	};
+	static const int numopers = 13;
+		
+	// start reading *after* the magic number
+	cc = 8;
+	
+	while (1)
+	{
+		bss = 0;
+		
+		// bail out if no more sections
+		if (!(CURBYTE()))
+			break;
+		
+		fp = CURSTR();
+		
+		printf("SECTION %s\n", fp);
+		
+		// read flags
+		while (CURBYTE())
+		{
+			switch (CURBYTE())
+			{
+			case 0x01:
+				printf("    FLAG: BSS\n");
+				bss = 1;
+				break;
+
+			default:
+				printf("    FLAG: %02X (unknown)\n", CURBYTE());
+				break;
+			}
+			NEXTBYTE();
+		}
+		// skip NUL terminating flags
+		NEXTBYTE();
+		
+		printf("    Local symbols:\n");
+		// now parse the local symbol table
+		while (CURBYTE())
+		{
+			fp = CURSTR();
+
+			// fp is the symbol name
+			val = (CURBYTE()) << 8;
+			NEXTBYTE();
+			val |= (CURBYTE());
+			NEXTBYTE();
+			// val is now the symbol value
+			
+			printf("        %s=%04X\n", fp, val);
+			
+		}
+		// skip terminating NUL
+		NEXTBYTE();
+
+		printf("    Exported symbols\n");
+				
+		// now parse the exported symbol table
+		while (CURBYTE())
+		{
+			fp = CURSTR();
+
+			// fp is the symbol name
+			val = (CURBYTE()) << 8;
+			NEXTBYTE();
+			val |= (CURBYTE());
+			NEXTBYTE();
+			// val is now the symbol value
+			
+			printf("        %s=%04X\n", fp, val);
+		}
+		// skip terminating NUL
+		NEXTBYTE();
+		
+		// now parse the incomplete references and make a list of
+		// external references that need resolution
+		printf("    Incomplete references\n");
+		while (CURBYTE())
+		{
+			printf("        (");
+			// parse the expression
+			while (CURBYTE())
+			{
+				int tt = CURBYTE();
+				NEXTBYTE();
+				switch (tt)
+				{
+				case 0x01:
+					// 16 bit integer
+					tt = CURBYTE() << 8;
+					NEXTBYTE();
+					tt |= CURBYTE();
+					NEXTBYTE();
+					// normalize for negatives...
+					if (tt > 0x7fff)
+						tt -= 0x10000;
+					printf(" I16=%d", tt);
+					break;
+				
+				case 0x02:
+					// external symbol reference
+					printf(" ES=%s", CURSTR());
+					break;
+					
+				case 0x03:
+					// internal symbol reference
+					printf(" IS=%s", CURSTR());
+					break;
+				
+				case 0x04:
+					// operator
+					if (CURBYTE() > 0 && CURBYTE() <= numopers)
+						printf(" OP=%s", opernames[CURBYTE()]);
+					else
+						printf(" OP=?");
+					NEXTBYTE();
+					break;
+
+				case 0x05:
+					// section base reference (NULL internal reference is
+					// the section base address
+					printf(" SB");
+					break;
+					
+				default:
+					printf(" ERR");
+				}
+			}
+			// skip the NUL
+			NEXTBYTE();
+			
+			// fetch the offset
+			val = CURBYTE() << 8;
+			NEXTBYTE();
+			val |= CURBYTE() & 0xff;
+			NEXTBYTE();
+			printf(" ) @ %04X\n", val);
+		}
+		// skip the NUL terminating the relocations
+		NEXTBYTE();
+				
+		// now set code location and size and verify that the file
+		// contains data going to the end of the code (if !SECTION_BSS)
+		val = CURBYTE() << 8;
+		NEXTBYTE();
+		val |= CURBYTE();
+		NEXTBYTE();
+		
+		printf("    CODE %04X bytes", val);
+		
+		// skip the code if we're not in a BSS section
+		if (!bss)
+		{
+			int i;
+			for (i = 0; i < val; i++)
+			{
+				if (! (i % 16))
+				{
+					printf("\n    %04X ", i);
+				}
+				printf("%02X", CURBYTE());
+				NEXTBYTE();
+			}
+		}
+		printf("\n");
+	}
+}
--- /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);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/readfiles.c	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,304 @@
+/*
+readfiles.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/>.
+
+
+Reads input files
+
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <argp.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "lwlink.h"
+#include "util.h"
+
+void read_lwobj16v0(fileinfo_t *fn);
+
+/*
+The logic of reading the entire file into memory is simple. All the symbol
+names in the file are NUL terminated strings and can be used directly without
+making additional copies.
+*/
+void read_files(void)
+{
+	int i;
+	long size;
+	FILE *f;
+	long bread;
+	for (i = 0; i < ninputfiles; i++)
+	{
+		f = fopen(inputfiles[i] -> filename, "rb");
+		if (!f)
+		{
+			fprintf(stderr, "Can't open file %s:", inputfiles[i] -> filename);
+			perror("");
+			exit(1);
+		}
+		fseek(f, 0, SEEK_END);
+		size = ftell(f);
+		rewind(f);
+		
+		inputfiles[i] -> filedata = lw_malloc(size);
+		inputfiles[i] -> filesize = size;
+		
+		bread = fread(inputfiles[i] -> filedata, 1, size, f);
+		if (bread < size)
+		{
+			fprintf(stderr, "Short read on file %s (%ld/%ld):", inputfiles[i] -> filename, bread, size);
+			perror("");
+			exit(1);
+		}
+			
+		fclose(f);
+		
+		if (!memcmp(inputfiles[i] -> filedata, "LWOBJ16", 8))
+		{
+			// read v0 LWOBJ16 file
+			read_lwobj16v0(inputfiles[i]);
+		}
+		else
+		{
+			fprintf(stderr, "%s: unknown file format\n", inputfiles[i] -> filename);
+			exit(1);
+		}
+	}
+}
+
+// this macro is used to bail out if we run off the end of the file data
+// while parsing - it keeps the code below cleaner
+#define NEXTBYTE()	do { cc++; if (cc > fn -> filesize) { fprintf(stderr, "%s: invalid file format\n", fn -> filename); exit(1); } } while (0)
+// this macro is used to refer to the current byte in the stream
+#define CURBYTE()	(fn -> filedata[cc < fn -> filesize ? cc : fn -> filesize - 1])
+// this one will leave the input pointer past the trailing NUL
+#define CURSTR()	read_lwobj16v0_str(&cc, fn)
+unsigned char *read_lwobj16v0_str(long *cc1, fileinfo_t *fn)
+{
+	int cc = *cc1;
+	unsigned char *fp;
+	fp = &CURBYTE();
+	while (CURBYTE())
+		NEXTBYTE();
+	NEXTBYTE();
+	*cc1 = cc;
+	return fp;
+}
+// the function below can be switched to dealing with data coming from a
+// source other than an in-memory byte pool by adjusting the input data
+// in "fn" and the above two macros
+void read_lwobj16v0(fileinfo_t *fn)
+{
+	unsigned char *fp;
+	long cc;
+	section_t *s;
+	int val;
+	symtab_t *se;
+	
+	// start reading *after* the magic number
+	cc = 8;
+	
+	// init data
+	fn -> sections = NULL;
+	fn -> nsections = 0;
+
+	while (1)
+	{
+//		NEXTBYTE();
+		// bail out if no more sections
+		if (!(CURBYTE()))
+			break;
+		
+		fp = CURSTR();
+		
+		// we now have a section name in fp
+		// create new section entry
+		fn -> sections = lw_realloc(fn -> sections, sizeof(section_t) * (fn -> nsections + 1));
+		s = &(fn -> sections[fn -> nsections]);
+		fn -> nsections += 1;
+		
+		s -> localsyms = NULL;
+		s -> flags = 0;
+		s -> codesize = 0;
+		s -> name = fp;
+		s -> loadaddress = 0;
+		s -> localsyms = NULL;
+		s -> exportedsyms = NULL;
+		s -> incompletes = NULL;
+		s -> processed = 0;
+		s -> file = fn;
+		
+		// read flags
+		while (CURBYTE())
+		{
+			switch (CURBYTE())
+			{
+			case 0x01:
+				s -> flags |= SECTION_BSS;
+				break;
+
+			default:
+				fprintf(stderr, "%s (%s): unrecognized section flag %02X\n", fn -> filename, s -> name, (int)(CURBYTE()));
+				exit(1);
+			}
+			NEXTBYTE();
+		}
+		// skip NUL terminating flags
+		NEXTBYTE();
+		
+		// now parse the local symbol table
+		while (CURBYTE())
+		{
+			fp = CURSTR();
+
+			// fp is the symbol name
+			val = (CURBYTE()) << 8;
+			NEXTBYTE();
+			val |= (CURBYTE());
+			NEXTBYTE();
+			// val is now the symbol value
+			
+			// create symbol table entry
+			se = lw_malloc(sizeof(symtab_t));
+			se -> next = s -> localsyms;
+			s -> localsyms = se;
+			se -> sym = fp;
+			se -> offset = val;
+		}
+		// skip terminating NUL
+		NEXTBYTE();
+		
+		// now parse the exported symbol table
+		while (CURBYTE())
+		{
+			fp = CURSTR();
+
+			// fp is the symbol name
+			val = (CURBYTE()) << 8;
+			NEXTBYTE();
+			val |= (CURBYTE());
+			NEXTBYTE();
+			// val is now the symbol value
+			
+			// create symbol table entry
+			se = lw_malloc(sizeof(symtab_t));
+			se -> next = s -> exportedsyms;
+			s -> exportedsyms = se;
+			se -> sym = fp;
+			se -> offset = val;
+		}
+		// skip terminating NUL
+		NEXTBYTE();
+		
+		// now parse the incomplete references and make a list of
+		// external references that need resolution
+		while (CURBYTE())
+		{
+			reloc_t *rp;
+			lw_expr_term_t *term;
+			
+			// we have a reference
+			rp = lw_malloc(sizeof(reloc_t));
+			rp -> next = s -> incompletes;
+			s -> incompletes = rp;
+			rp -> offset = 0;
+			rp -> expr = lw_expr_stack_create();
+			
+			// parse the expression
+			while (CURBYTE())
+			{
+				int tt = CURBYTE();
+				NEXTBYTE();
+				switch (tt)
+				{
+				case 0x01:
+					// 16 bit integer
+					tt = CURBYTE() << 8;
+					NEXTBYTE();
+					tt |= CURBYTE();
+					NEXTBYTE();
+					// normalize for negatives...
+					if (tt > 0x7fff)
+						tt -= 0x10000;
+					term = lw_expr_term_create_int(tt);
+					break;
+				
+				case 0x02:
+					// external symbol reference
+					term = lw_expr_term_create_sym(CURSTR(), 0);
+					break;
+					
+				case 0x03:
+					// internal symbol reference
+					term = lw_expr_term_create_sym(CURSTR(), 1);
+					break;
+				
+				case 0x04:
+					// operator
+					term = lw_expr_term_create_oper(CURBYTE());
+					NEXTBYTE();
+					break;
+
+				case 0x05:
+					// section base reference (NULL internal reference is
+					// the section base address
+					term = lw_expr_term_create_sym(NULL, 1);
+					break;
+					
+				default:
+					fprintf(stderr, "%s (%s): bad relocation expression\n", fn -> filename, s -> name);
+					exit(1);
+				}
+				lw_expr_stack_push(rp -> expr, term);
+				lw_expr_term_free(term);
+			}
+			// skip the NUL
+			NEXTBYTE();
+			
+			// fetch the offset
+			rp -> offset = CURBYTE() << 8;
+			NEXTBYTE();
+			rp -> offset |= CURBYTE() & 0xff;
+			NEXTBYTE();
+		}
+		// skip the NUL terminating the relocations
+		NEXTBYTE();
+				
+		// now set code location and size and verify that the file
+		// contains data going to the end of the code (if !SECTION_BSS)
+		s -> codesize = CURBYTE() << 8;
+		NEXTBYTE();
+		s -> codesize |= CURBYTE();
+		NEXTBYTE();
+		
+		s -> code = &(CURBYTE());
+		
+		// skip the code if we're not in a BSS section
+		if (!(s -> flags & SECTION_BSS))
+		{
+			int i;
+			for (i = 0; i < s -> codesize; i++)
+				NEXTBYTE();
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/script.c	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,257 @@
+/*
+script.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/>.
+
+
+Read and parse linker scripts
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "lwlink.h"
+#include "util.h"
+
+// the built-in DECB target linker script
+static char *decb_script =
+	"section init load 2000\n"
+	"section code\n"
+	"section *,!bss\n"
+	"section *,bss\n"
+	"entry 2000\n"
+	;
+
+// the built-in RAW target linker script
+static char *raw_script = 
+	"section init load 0000\n"
+	"section code\n"
+	"section *,!bss\n"
+	"section *,bss\n"
+	;
+
+// the "simple" script
+static char *simple_script = 
+	"section *,!bss\n"
+	"section *,bss\n"
+	;
+
+linkscript_t linkscript = { 0, NULL, -1 };
+
+void setup_script()
+{
+	char *script, *oscript;
+	long size;
+
+	// read the file if needed
+	if (scriptfile)
+	{
+		FILE *f;
+		long bread;
+		
+		f = fopen(scriptfile, "rb");
+		if (!f)
+		{
+			fprintf(stderr, "Can't open file %s:", scriptfile);
+			perror("");
+			exit(1);
+		}
+		fseek(f, 0, SEEK_END);
+		size = ftell(f);
+		rewind(f);
+		
+		script = lw_malloc(size + 2);
+		
+		bread = fread(script, 1, size, f);
+		if (bread < size)
+		{
+			fprintf(stderr, "Short read on file %s (%ld/%ld):", scriptfile, bread, size);
+			perror("");
+			exit(1);
+		}
+		fclose(f);
+		
+		script[size] = '\n';
+		script[size + 1] = '\0';
+	}
+	else
+	{
+		// fetch defaults based on output mode
+		switch (outformat)
+		{
+		case OUTPUT_RAW:
+			script = raw_script;
+			break;
+		
+		case OUTPUT_DECB:
+			script = decb_script;
+			break;
+		
+		default:
+			script = simple_script;
+			break;
+		}
+		
+		size = strlen(script);
+	}
+
+	oscript = script;
+	// now parse the script file
+	while (*script)
+	{
+		char *ptr, *ptr2, *line;
+
+		for (ptr = script; *ptr && *ptr != '\n' && *ptr != '\r'; ptr++)
+			/* do nothing */ ;
+		
+		line = lw_malloc(ptr - script + 1);
+		memcpy(line, script, ptr - script);
+		line[ptr - script] = '\0';
+
+		// skip line terms
+		for (script = ptr + 1; *script == '\n' || *script == '\r'; script++)
+			/* do nothing */ ;
+		
+		// ignore leading whitespace
+		for (ptr = line; *ptr && isspace(*ptr); ptr++)
+			/* do nothing */ ;
+		
+		// ignore blank lines
+		if (!*ptr)
+			continue;
+		
+		for (ptr = line; *ptr && !isspace(*ptr); ptr++)
+			/* do nothing */ ;
+		
+		// now ptr points to the char past the first word
+		// NUL it out
+		if (*ptr)
+			*ptr++ = '\0';
+		
+		// skip spaces after the first word
+		for ( ; *ptr && isspace(*ptr); ptr++)
+			/* do nothing */ ;
+		
+		if (!strcmp(line, "pad"))
+		{
+			// padding
+			// parse the hex number and stow it
+			linkscript.padsize = strtol(ptr, NULL, 16);
+			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
+			// parse out the section name and flags
+			for (ptr2 = ptr; *ptr2 && !isspace(*ptr2); ptr2++)
+				/* do nothing */ ;
+			
+			if (*ptr2)
+				*ptr2++ = '\0';
+			
+			while (*ptr2 && isspace(*ptr2))
+				ptr2++;
+				
+			// ptr now points to the section name and flags and ptr2
+			// to the first non-space character following
+			
+			// then look for "load <addr>" clause
+			if (*ptr2)
+			{
+				if (!strncmp(ptr2, "load", 4))
+				{
+					ptr2 += 4;
+					while (*ptr2 && isspace(*ptr2))
+						ptr2++;
+					
+				}
+				else
+				{
+					fprintf(stderr, "%s: bad script\n", scriptfile);
+					exit(1);
+				}
+			}
+			
+			// now ptr2 points to the load address if there is one
+			// or NUL if not
+			linkscript.lines = lw_realloc(linkscript.lines, sizeof(struct scriptline_s) * (linkscript.nlines + 1));
+
+			linkscript.lines[linkscript.nlines].noflags = 0;
+			linkscript.lines[linkscript.nlines].yesflags = 0;
+			if (*ptr2)
+				linkscript.lines[linkscript.nlines].loadat = strtol(ptr2, NULL, 16);
+			else
+				linkscript.lines[linkscript.nlines].loadat = -1;
+			for (ptr2 = ptr; *ptr2 && *ptr2 != ','; ptr2++)
+				/* do nothing */ ;
+			if (*ptr2)
+			{
+				*ptr2++ = '\0';
+				if (!strcmp(ptr2, "!bss"))
+				{
+					linkscript.lines[linkscript.nlines].noflags = SECTION_BSS;
+				}
+				else if (!strcmp(ptr2, "bss"))
+				{
+					linkscript.lines[linkscript.nlines].yesflags = SECTION_BSS;
+				}
+				else
+				{
+					fprintf(stderr, "%s: bad script\n", scriptfile);
+					exit(1);
+				}
+			}
+			if (ptr[0] == '*' && ptr[1] == '\0')
+				linkscript.lines[linkscript.nlines].sectname = NULL;
+			else
+				linkscript.lines[linkscript.nlines].sectname = lw_strdup(ptr);
+			linkscript.nlines++;
+		}
+		else
+		{
+			fprintf(stderr, "%s: bad script\n", scriptfile);
+			exit(1);
+		}
+		lw_free(line);
+	}
+	
+	if (scriptfile)
+		lw_free(oscript);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/util.c	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,87 @@
+/*
+util.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/>.
+*/
+
+/*
+Utility functions
+*/
+
+#define __util_c_seen__
+
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "util.h"
+
+void *lw_malloc(int size)
+{
+	void *ptr;
+	
+	ptr = malloc(size);
+	if (!ptr)
+	{
+		// bail out; memory allocation error
+		fprintf(stderr, "lw_malloc(): Memory allocation error\n");
+		exit(1);
+	}
+	return ptr;
+}
+
+void *lw_realloc(void *optr, int size)
+{
+	void *ptr;
+	
+	if (size == 0)
+	{
+		lw_free(optr);
+		return;
+	}
+	
+	ptr = realloc(optr, size);
+	if (!ptr)
+	{
+		fprintf(stderr, "lw_realloc(): memory allocation error\n");
+		exit(1);
+	}
+}
+
+void lw_free(void *ptr)
+{
+	if (ptr)
+		free(ptr);
+}
+
+char *lw_strdup(const char *s)
+{
+	char *d;
+	
+	if (!s)
+		return NULL;
+
+	d = strdup(s);
+	if (!d)
+	{
+		fprintf(stderr, "lw_strdup(): memory allocation error\n");
+		exit(1);
+	}
+	
+	return d;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwlink/util.h	Wed Jan 28 05:59:14 2009 +0000
@@ -0,0 +1,44 @@
+/*
+util.h
+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/>.
+*/
+
+/*
+Utility functions
+*/
+
+#ifndef __util_h_seen__
+#define __util_h_seen__
+
+#ifndef __util_c_seen__
+#define __util_E__ extern
+#else
+#define __util_E__
+#endif
+
+// allocate memory
+__util_E__ void *lw_malloc(int size);
+__util_E__ void lw_free(void *ptr);
+__util_E__ void *lw_realloc(void *optr, int size);
+
+// string stuff
+__util_E__ char *lw_strdup(const char *s);
+
+#undef __util_E__
+
+#endif // __util_h_seen__