changeset 510:3425005b9980

Fix segfault if no instances of a section are in the final result If no instances of a section are found to be included in the final linked binary, print a warning instead of crashing with a segfault.
author William Astle <lost@l-w.ca>
date Tue, 02 Jun 2020 21:28:47 -0600
parents bab891d85a53
children 4f6b2c615818
files lwlink/link.c
diffstat 1 files changed, 20 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/lwlink/link.c	Sat May 16 17:27:42 2020 -0600
+++ b/lwlink/link.c	Tue Jun 02 21:28:47 2020 -0600
@@ -21,6 +21,8 @@
 Resolve section and symbol addresses; handle incomplete references
 */
 
+#define __link_c_seen__
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -53,7 +55,7 @@
 	int sn;
 	sectopt_t *so;
 	
-//	fprintf(stderr, "Considering sections in %s (%d) for %s\n", fn -> filename, fn -> forced, name);
+	//fprintf(stderr, "Considering sections in %s (%d) for %s\n", fn -> filename, fn -> forced, name);
 	if (fn -> forced == 0)
 		return;
 
@@ -63,13 +65,13 @@
 
 	for (sn = 0; sn < fn -> nsections; sn++)
 	{
-//		fprintf(stderr, "    Considering section %s\n", fn -> sections[sn].name);
+		//fprintf(stderr, "    Considering section %s\n", fn -> sections[sn].name);
 		if (!strcmp(name, (char *)(fn -> sections[sn].name)))
 		{
 			if (fn -> sections[sn].flags & SECTION_CONST)
 				continue;
 			// we have a match
-//			fprintf(stderr, "    Found\n");
+			//fprintf(stderr, "    Found\n");
 			sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
 			sectlist[nsects].ptr = &(fn -> sections[sn]);
 			
@@ -94,7 +96,7 @@
 				so -> aftersize = 0;
 			}
 			nsects++;
-//			fprintf(stderr, "Adding section %s (%s)\n",fn -> sections[sn].name, fn -> filename);
+			//fprintf(stderr, "Adding section %s (%s) %p %d\n",fn -> sections[sn].name, fn -> filename, sectlist, nsects);
 		}
 	}
 	for (sn = 0; sn < fn -> nsubs; sn++)
@@ -207,7 +209,7 @@
 			laddr = linkscript.lines[ln].loadat;
 			growdown = linkscript.lines[ln].growsdown;
 		}
-		//fprintf(stderr, "Adding section %s\n", linkscript.lines[ln].sectname);
+		fprintf(stderr, "Adding section %s\n", linkscript.lines[ln].sectname);
 		add_matching_sections(linkscript.lines[ln].sectname, linkscript.lines[ln].yesflags, linkscript.lines[ln].noflags, &laddr, growdown);
 		
 		if (linkscript.lines[ln].sectname)
@@ -221,16 +223,23 @@
 			{
 				if (so -> aftersize)
 				{
-					sectlist[nsects - 1].ptr -> afterbytes = so -> afterbytes;
-					sectlist[nsects - 1].ptr -> aftersize = so -> aftersize;
-					if (growdown)
+					if (nsects == 0)
 					{
-						sectlist[nsects-1].ptr -> loadaddress -= so -> aftersize;
-						laddr -= so -> aftersize;
+						fprintf(stderr, "Warning: no instances of section listed in link script: %s\n", sname);
 					}
 					else
 					{
-						laddr += so -> aftersize;
+						sectlist[nsects - 1].ptr -> afterbytes = so -> afterbytes;
+						sectlist[nsects - 1].ptr -> aftersize = so -> aftersize;
+						if (growdown)
+						{
+							if (sectlist) sectlist[nsects-1].ptr -> loadaddress -= so -> aftersize;
+							laddr -= so -> aftersize;
+						}
+						else
+						{
+							laddr += so -> aftersize;
+						}
 					}
 				}
 			}