shithub: rgbds

Download patch

ref: 1c2965467d266a8eddc7eb4e69c415148a6bebe9
parent: d243e5039077c44cb9c813e4e63166b36932259c
author: ISSOtm <eldredhabert0@gmail.com>
date: Fri Jul 8 15:28:05 EDT 2022

Process linker script before doing sanity checks

--- a/src/link/assign.c
+++ b/src/link/assign.c
@@ -17,7 +17,6 @@
 #include "link/symbol.h"
 #include "link/object.h"
 #include "link/main.h"
-#include "link/script.h"
 #include "link/output.h"
 
 #include "error.h"
@@ -64,45 +63,6 @@
 }
 
 /**
- * Alter sections' attributes based on the linker script
- */
-static void processLinkerScript(void)
-{
-	if (!linkerScriptName)
-		return;
-	verbosePrint("Reading linker script...\n");
-
-	linkerScript = openFile(linkerScriptName, "r");
-
-	/* Modify all sections according to the linker script */
-	struct SectionPlacement *placement;
-
-	while ((placement = script_NextSection())) {
-		struct Section *section = placement->section;
-
-		/* Check if this doesn't conflict with what the code says */
-		if (section->isBankFixed && placement->bank != section->bank)
-			error(NULL, 0, "Linker script contradicts \"%s\"'s bank placement",
-			      section->name);
-		if (section->isAddressFixed && placement->org != section->org)
-			error(NULL, 0, "Linker script contradicts \"%s\"'s address placement",
-			      section->name);
-		if (section->isAlignFixed
-		 && (placement->org & section->alignMask) != 0)
-			error(NULL, 0, "Linker script contradicts \"%s\"'s alignment",
-			      section->name);
-
-		section->isAddressFixed = true;
-		section->org = placement->org;
-		section->isBankFixed = true;
-		section->bank = placement->bank;
-		section->isAlignFixed = false; /* The alignment is satisfied */
-	}
-
-	fclose(linkerScript);
-}
-
-/**
  * Assigns a section to a given memory location
  * @param section The section to assign
  * @param location The location to assign the section to
@@ -423,9 +383,6 @@
 
 	initFreeSpace();
 
-	/* Process linker script, if any */
-	processLinkerScript();
-
 	nbSectionsToAssign = 0;
 	sect_ForEach(categorizeSection, NULL);
 
@@ -505,6 +462,4 @@
 	}
 
 	free(sections);
-
-	script_Cleanup();
 }
--- a/src/link/main.c
+++ b/src/link/main.c
@@ -18,12 +18,13 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#include "link/object.h"
-#include "link/symbol.h"
-#include "link/section.h"
 #include "link/assign.h"
-#include "link/patch.h"
+#include "link/object.h"
 #include "link/output.h"
+#include "link/patch.h"
+#include "link/section.h"
+#include "link/script.h"
+#include "link/symbol.h"
 
 #include "extern/getopt.h"
 
@@ -443,6 +444,43 @@
 	/* Read all object files first, */
 	for (obj_Setup(argc - curArgIndex); curArgIndex < argc; curArgIndex++)
 		obj_ReadFile(argv[curArgIndex], argc - curArgIndex - 1);
+
+	/* apply the linker script's modifications, */
+	if (linkerScriptName) {
+		verbosePrint("Reading linker script...\n");
+
+		linkerScript = openFile(linkerScriptName, "r");
+
+		/* Modify all sections according to the linker script */
+		struct SectionPlacement *placement;
+
+		while ((placement = script_NextSection())) {
+			struct Section *section = placement->section;
+
+			/* Check if this doesn't conflict with what the code says */
+			if (section->isBankFixed && placement->bank != section->bank)
+				error(NULL, 0, "Linker script contradicts \"%s\"'s bank placement",
+				      section->name);
+			if (section->isAddressFixed && placement->org != section->org)
+				error(NULL, 0, "Linker script contradicts \"%s\"'s address placement",
+				      section->name);
+			if (section->isAlignFixed
+			 && (placement->org & section->alignMask) != 0)
+				error(NULL, 0, "Linker script contradicts \"%s\"'s alignment",
+				      section->name);
+
+			section->isAddressFixed = true;
+			section->org = placement->org;
+			section->isBankFixed = true;
+			section->bank = placement->bank;
+			section->isAlignFixed = false; /* The alignment is satisfied */
+		}
+
+		fclose(linkerScript);
+
+		script_Cleanup();
+	}
+
 
 	/* then process them, */
 	obj_DoSanityChecks();