shithub: rgbds

Download patch

ref: 74f43d4e094d6ed6048d784a9275f0a7a26a7097
parent: 54ed050ecf9a164ecb9cf368bbdedd524f23560a
author: ISSOtm <[email protected]>
date: Sun Sep 8 19:40:16 EDT 2019

Add a way to seek a SECTION by name without creating one

--- a/include/asm/output.h
+++ b/include/asm/output.h
@@ -29,6 +29,7 @@
 
 void out_PrepPass2(void);
 void out_SetFileName(char *s);
+struct Section *out_FindSectionByName(const char *pzName);
 void out_NewSection(char *pzName, uint32_t secttype);
 void out_NewAbsSection(char *pzName, uint32_t secttype, int32_t org,
 		       int32_t bank);
--- a/src/asm/output.c
+++ b/src/asm/output.c
@@ -599,6 +599,20 @@
 		printf("Output filename %s\n", s);
 }
 
+struct Section *out_FindSectionByName(const char *pzName)
+{
+	struct Section *pSect = pSectionList;
+
+	while (pSect) {
+		if (strcmp(pzName, pSect->pzName) == 0)
+			return pSect;
+
+		pSect = pSect->pNext;
+	}
+
+	return NULL;
+}
+
 /*
  * Find a section by name and type. If it doesn't exist, create it
  */
@@ -605,42 +619,32 @@
 struct Section *out_FindSection(char *pzName, uint32_t secttype, int32_t org,
 				int32_t bank, int32_t alignment)
 {
-	struct Section *pSect, **ppSect;
+	struct Section *pSect = out_FindSectionByName(pzName);
 
-	ppSect = &pSectionList;
-	pSect = pSectionList;
-
-	while (pSect) {
-		if (strcmp(pzName, pSect->pzName) == 0) {
-			if (secttype == pSect->nType
-			    && ((uint32_t)org) == pSect->nOrg
-			    && ((uint32_t)bank) == pSect->nBank
-			    && ((uint32_t)alignment == pSect->nAlign)) {
-				return pSect;
-			}
-
-			fatalerror("Section already exists but with a different type");
+	if (pSect) {
+		if (secttype == pSect->nType
+			&& ((uint32_t)org) == pSect->nOrg
+			&& ((uint32_t)bank) == pSect->nBank
+			&& ((uint32_t)alignment == pSect->nAlign)) {
+			return pSect;
 		}
-		ppSect = &(pSect->pNext);
-		pSect = pSect->pNext;
+		fatalerror("Section already exists but with a different type");
 	}
 
 	pSect = malloc(sizeof(struct Section));
-	*ppSect = pSect;
 	if (pSect == NULL)
 		fatalerror("Not enough memory for section");
 
-	pSect->pzName = malloc(strlen(pzName) + 1);
+	pSect->pzName = strdup(pzName);
 	if (pSect->pzName == NULL)
 		fatalerror("Not enough memory for sectionname");
 
-	strcpy(pSect->pzName, pzName);
 	pSect->nType = secttype;
 	pSect->nPC = 0;
 	pSect->nOrg = org;
 	pSect->nBank = bank;
 	pSect->nAlign = alignment;
-	pSect->pNext = NULL;
+	pSect->pNext = pSectionList;
 	pSect->pPatches = NULL;
 
 	/* It is only needed to allocate memory for ROM sections. */
@@ -655,7 +659,13 @@
 		pSect->tData = NULL;
 	}
 
-	return (pSect);
+	/*
+	 * Add the new section to the list
+	 * at the beginning because order doesn't matter
+	 */
+	pSectionList = pSect;
+
+	return pSect;
 }
 
 /*