shithub: rgbds

Download patch

ref: 7233f568a7c8e7b991b9be91b7159c4d05c731f6
parent: 197f1e9b7ba9a0ad1c240db1a98bfab5cb7db703
author: ISSOtm <[email protected]>
date: Sun Nov 3 22:24:19 EST 2019

Report overlapping sections whenever possible

--- a/include/link/output.h
+++ b/include/link/output.h
@@ -21,6 +21,13 @@
 void out_AddSection(struct Section const *section);
 
 /**
+ * Finds an assigned section overlapping another one.
+ * @param section The section that is being overlapped
+ * @return A section overlapping it
+ */
+struct Section const *out_OverlappingSection(struct Section const *section);
+
+/**
  * Writes all output (bin, sym, map) files.
  */
 void out_WriteFiles(void);
--- a/src/link/assign.c
+++ b/src/link/assign.c
@@ -1,6 +1,7 @@
 
 #include <stdbool.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "link/assign.h"
 #include "link/section.h"
@@ -284,32 +285,41 @@
 		return;
 	}
 
+	/* Please adjust depending on longest message below */
+	char where[64];
+
 	if (section->isBankFixed && nbbanks(section->type) != 1) {
 		if (section->isAddressFixed)
-			errx(1, "Unable to place \"%s\" (%s section) at $%02x:%04x",
-			     section->name, typeNames[section->type],
-			     section->bank, section->org);
+			snprintf(where, 64, "at $%02x:%04x",
+				 section->bank, section->org);
 		else if (section->isAlignFixed)
-			errx(1, "Unable to place \"%s\" (%s section) in bank %u with align mask %x",
-			     section->name, typeNames[section->type],
-			     section->bank, ~section->alignMask);
+			snprintf(where, 64, "in bank %02x with align mask %x",
+				 section->bank, ~section->alignMask);
 		else
-			errx(1, "Unable to place \"%s\" (%s section) in bank %u",
-			     section->name, typeNames[section->type],
-			     section->bank);
+			snprintf(where, 64, "in bank %02x", section->bank);
 	} else {
 		if (section->isAddressFixed)
-			errx(1, "Unable to place \"%s\" (%s section) at address $%x",
-			     section->name, typeNames[section->type],
-			     section->org);
+			snprintf(where, 64, "at address $%04x", section->org);
 		else if (section->isAlignFixed)
-			errx(1, "Unable to place \"%s\" (%s section) with align mask %x",
-			     section->name, typeNames[section->type],
-			     ~section->alignMask);
+			snprintf(where, 64, "with align mask %x",
+				 ~section->alignMask);
 		else
-			errx(1, "Unable to place \"%s\" (%s section) anywhere",
-			     section->name, typeNames[section->type]);
+			strcpy(where, "anywhere");
 	}
+
+	/* If a section failed to go to several places, nothing we can report */
+	if (!section->isBankFixed || !section->isAddressFixed)
+		errx(1, "Unable to place \"%s\" (%s section) %s",
+		     section->name, typeNames[section->type], where);
+	/* If the section just can't fit the bank, report that */
+	else if (section->org + section->size > endaddr(section->type) + 1)
+		errx(1, "Unable to place \"%s\" (%s section) %s: section runs past end of region",
+		     section->name, typeNames[section->type], where);
+	/* Otherwise there is overlap with another section */
+	else
+		errx(1, "Unable to place \"%s\" (%s section) %s: section overlaps with \"%s\"",
+		     section->name, typeNames[section->type], where,
+		     out_OverlappingSection(section)->name);
 }
 
 struct UnassignedSection {
--- a/src/link/output.c
+++ b/src/link/output.c
@@ -78,6 +78,20 @@
 	*ptr = newSection;
 }
 
+struct Section const *out_OverlappingSection(struct Section const *section)
+{
+	struct SortedSection *ptr =
+		sections[section->type].banks[section->bank].sections;
+
+	while (ptr) {
+		if (ptr->section->org < section->org + section->size
+		 && section->org < ptr->section->org + ptr->section->size)
+			return ptr->section;
+		ptr = ptr->next;
+	}
+	return NULL;
+}
+
 /**
  * Performs sanity checks on the overlay file.
  */