shithub: rgbds

Download patch

ref: ea0c5581a5598bbea541de1d569c5851a90544d0
parent: 2ea329c920e20fbbdf9ccb5039c1ce3a929b34ca
author: ISSOtm <[email protected]>
date: Tue Mar 10 12:55:50 EDT 2020

Prevent deletion of built-in symbols

--- a/include/asm/symbol.h
+++ b/include/asm/symbol.h
@@ -32,6 +32,7 @@
 	enum SymbolType type;
 	bool isConstant; /* Whether the symbol's value is currently known */
 	bool isExported; /* Whether the symbol is to be exported */
+	bool isBuiltin;  /* Whether the symbol is a built-in */
 	struct sSymbol *pScope;
 	struct sSymbol *pNext;
 	struct Section *pSection;
--- a/src/asm/symbol.c
+++ b/src/asm/symbol.c
@@ -132,6 +132,7 @@
 
 	(*ppsym)->isConstant = false;
 	(*ppsym)->isExported = false;
+	(*ppsym)->isBuiltin = false;
 	(*ppsym)->pScope = NULL;
 	(*ppsym)->pNext = NULL;
 	(*ppsym)->pSection = NULL;
@@ -231,7 +232,11 @@
 
 	ppSym = findpsymbol(tzName, pscope);
 
-	if (ppSym) {
+	if (!ppSym) {
+		yyerror("'%s' not defined", tzName);
+	} else if ((*ppSym)->isBuiltin) {
+		yyerror("Built-in symbol '%s' cannot be purged", tzName);
+	} else {
 		struct sSymbol *pSym;
 
 		pSym = *ppSym;
@@ -241,8 +246,6 @@
 			free(pSym->pMacro);
 
 		free(pSym);
-	} else {
-		yyerror("'%s' not defined", tzName);
 	}
 }
 
@@ -736,12 +739,17 @@
 
 	pPCSymbol = sym_AddReloc("@");
 	pPCSymbol->Callback = CallbackPC;
+	pPCSymbol->isBuiltin = true;
 	p_NARGSymbol = sym_AddEqu("_NARG", 0);
 	p_NARGSymbol->Callback = Callback_NARG;
+	p_NARGSymbol->isBuiltin = true;
 	p__LINE__Symbol = sym_AddEqu("__LINE__", 0);
 	p__LINE__Symbol->Callback = Callback__LINE__;
-	sym_AddSet("_RS", 0);
+	p__LINE__Symbol->isBuiltin = true;
+	struct sSymbol *_RSSymbol = sym_AddSet("_RS", 0);
 
+	_RSSymbol->isBuiltin = true;
+
 	time_t now = time(NULL);
 
 	if (now == (time_t)-1) {
@@ -772,17 +780,22 @@
 	strftime(SavedMINUTE, sizeof(SavedMINUTE), "%M", time_utc);
 	strftime(SavedSECOND, sizeof(SavedSECOND), "%S", time_utc);
 
-	sym_AddString("__TIME__", SavedTIME);
-	sym_AddString("__DATE__", SavedDATE);
-	sym_AddString("__ISO_8601_LOCAL__", SavedTIMESTAMP_ISO8601_LOCAL);
-	sym_AddString("__ISO_8601_UTC__", SavedTIMESTAMP_ISO8601_UTC);
+#define addString(name, val) do { \
+	struct sSymbol *symbol = sym_AddString(name, val); \
+	symbol->isBuiltin = true; \
+} while (0)
+	addString("__TIME__", SavedTIME);
+	addString("__DATE__", SavedDATE);
+	addString("__ISO_8601_LOCAL__", SavedTIMESTAMP_ISO8601_LOCAL);
+	addString("__ISO_8601_UTC__", SavedTIMESTAMP_ISO8601_UTC);
 	/* This cannot start with zeros */
-	sym_AddString("__UTC_YEAR__", SavedYEAR);
-	sym_AddString("__UTC_MONTH__", removeLeadingZeros(SavedMONTH));
-	sym_AddString("__UTC_DAY__", removeLeadingZeros(SavedDAY));
-	sym_AddString("__UTC_HOUR__", removeLeadingZeros(SavedHOUR));
-	sym_AddString("__UTC_MINUTE__", removeLeadingZeros(SavedMINUTE));
-	sym_AddString("__UTC_SECOND__", removeLeadingZeros(SavedSECOND));
+	addString("__UTC_YEAR__", SavedYEAR);
+	addString("__UTC_MONTH__", removeLeadingZeros(SavedMONTH));
+	addString("__UTC_DAY__", removeLeadingZeros(SavedDAY));
+	addString("__UTC_HOUR__", removeLeadingZeros(SavedHOUR));
+	addString("__UTC_MINUTE__", removeLeadingZeros(SavedMINUTE));
+	addString("__UTC_SECOND__", removeLeadingZeros(SavedSECOND));
+#undef addString
 
 	pScope = NULL;