diff lwlink/output.c @ 183:302b8db5fd89

modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
author lost
date Sat, 21 Mar 2009 17:03:42 +0000
parents 106c2fe3c9d9
children 857cb407229e
line wrap: on
line diff
--- a/lwlink/output.c	Fri Mar 06 01:35:40 2009 +0000
+++ b/lwlink/output.c	Sat Mar 21 17:03:42 2009 +0000
@@ -70,7 +70,8 @@
 
 void do_output_decb(FILE *of)
 {
-	int sn;
+	int sn, sn2;
+	int cloc, olen;
 	unsigned char buf[5];
 	
 	for (sn = 0; sn < nsects; sn++)
@@ -85,14 +86,40 @@
 			// don't generate output for a zero size section
 			continue;
 		}
+		
+		// calculate the length of this output block
+		cloc = sectlist[sn].ptr -> loadaddress;
+		olen = 0;
+		for (sn2 = sn; sn2 < nsects; sn2++)
+		{
+			// ignore BSS sections
+			if (sectlist[sn2].ptr -> flags & SECTION_BSS)
+				continue;
+			// ignore zero length sections
+			if (sectlist[sn2].ptr -> codesize == 0)
+				continue;
+			if (cloc != sectlist[sn2].ptr -> loadaddress)
+				break;
+			olen += sectlist[sn2].ptr -> codesize;
+			cloc += sectlist[sn2].ptr -> codesize;
+		}
+		
 		// write a preamble
 		buf[0] = 0x00;
-		buf[1] = sectlist[sn].ptr -> codesize >> 8;
-		buf[2] = sectlist[sn].ptr -> codesize & 0xff;
+		buf[1] = olen >> 8;
+		buf[2] = olen & 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);
+		for (; sn < sn2; sn++)
+		{
+			if (sectlist[sn].ptr -> flags & SECTION_BSS)
+				continue;
+			if (sectlist[sn].ptr -> codesize == 0)
+				continue;
+			writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
+		}
+		sn--;
 	}
 	// write a postamble
 	buf[0] = 0xff;