shithub: rgbds

Download patch

ref: b44f5825a57942f7ceb29d02fca9f9ea35e5f4f1
parent: 631910bd670cf46663d68717e73d504cf8d469c2
author: ISSOtm <[email protected]>
date: Fri Aug 30 18:06:41 EDT 2019

Make RGBDS behave identically whether writing a .o
Some errors are only tripped in `out_WriteObject`, which was
basically a stub when `-o` wasn't specified. Now, instead,
errors are checked in a separate function before out_WriteFile

--- a/include/asm/output.h
+++ b/include/asm/output.h
@@ -40,6 +40,7 @@
 void out_RelByte(struct Expression *expr);
 void out_RelWord(struct Expression *expr);
 void out_PCRelByte(struct Expression *expr);
+void out_CheckErrors(void);
 void out_WriteObject(void);
 void out_Skip(int32_t skip);
 void out_BinaryFile(char *s);
--- a/src/asm/main.c
+++ b/src/asm/main.c
@@ -477,6 +477,10 @@
 			printf("(%d lines/minute)\n",
 			       (int)(60 / timespent * nTotalLines));
 	}
-	out_WriteObject();
+
+	out_CheckErrors();
+	/* If no path specified, don't write file */
+	if (tzObjectname != NULL)
+		out_WriteObject();
 	return 0;
 }
--- a/src/asm/output.c
+++ b/src/asm/output.c
@@ -265,15 +265,6 @@
 	int32_t sectid;
 
 	if (!(pSym->nType & SYMF_DEFINED)) {
-		if (pSym->nType & SYMF_LOCAL) {
-			char *name = pSym->tzName;
-			char *localPtr = strchr(name, '.');
-
-			if (localPtr)
-				name = localPtr;
-			errx(1, "%s(%u) : '%s' not defined",
-			     pSym->tzFileName, pSym->nFileLine, name);
-		}
 		type = SYM_IMPORT;
 	} else if (pSym->nType & SYMF_EXPORT) {
 		type = SYM_EXPORT;
@@ -537,24 +528,47 @@
 }
 
 /*
+ * Check for errors that could happen while writing an object file
+ * This is important as out_WriteObject is skipped entirely when `-o` is omitted
+ * Therefore, errors such as memory allocations still should be handled in
+ * out_WriteObject and not here
+ */
+void out_CheckErrors(void)
+{
+	/* Local symbols cannot be imported from elsewhere */
+	struct PatchSymbol *pSym = pPatchSymbols;
+
+	while (pSym) {
+		struct sSymbol *pSymbol = pSym->pSymbol;
+
+		if (!(pSymbol->nType & SYMF_DEFINED)
+		   && pSymbol->nType & SYMF_LOCAL) {
+			char *name = pSymbol->tzName;
+			char *localPtr = strchr(name, '.');
+
+			if (localPtr)
+				name = localPtr;
+			errx(1, "%s(%u) : '%s' not defined",
+			     pSymbol->tzFileName, pSymbol->nFileLine, name);
+		}
+		pSym = pSym->pNext;
+	}
+}
+
+/*
  * Write an objectfile
  */
 void out_WriteObject(void)
 {
 	FILE *f;
+	struct PatchSymbol *pSym;
+	struct Section *pSect;
 
 	addexports();
 
-	/* If no path specified, don't write file */
-	if (tzObjectname == NULL)
-		return;
-
 	f = fopen(tzObjectname, "wb");
 	if (f == NULL)
 		fatalerror("Couldn't write file '%s'\n", tzObjectname);
-
-	struct PatchSymbol *pSym;
-	struct Section *pSect;
 
 	fwrite(RGBDS_OBJECT_VERSION_STRING, 1,
 	       strlen(RGBDS_OBJECT_VERSION_STRING), f);