shithub: rgbds

Download patch

ref: e27a6d53a0219deda031496767e73999bb988b74
parent: d17e9c663e8b52e32c50b22e298a4240c7951b3d
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Mon Apr 26 07:40:58 EDT 2021

Support character escapes in linkerscript strings

This allows linkerscripts to refer to section names even if
they contain special characters: '\r' '\n' '\t' '"' '\\'.

--- a/src/link/script.c
+++ b/src/link/script.c
@@ -19,7 +19,7 @@
 
 #include "extern/err.h"
 
-FILE * linkerScript;
+FILE *linkerScript;
 char *includeFileName;
 
 static uint32_t lineNo;
@@ -223,12 +223,28 @@
 
 		do {
 			curchar = readChar(linkerScript);
-			if (curchar == EOF || isNewline(curchar))
+			if (curchar == EOF || isNewline(curchar)) {
 				errx(1, "%s(%" PRIu32 "): Unterminated string",
 				     linkerScriptName, lineNo);
-			else if (curchar == '"')
+			} else if (curchar == '"') {
 				/* Quotes force a string termination */
 				curchar = '\0';
+			} else if (curchar == '\\') {
+				/* Backslashes are escape sequences */
+				curchar = readChar(linkerScript);
+				if (curchar == EOF || isNewline(curchar))
+					errx(1, "%s(%" PRIu32 "): Unterminated string",
+					     linkerScriptName, lineNo);
+				else if (curchar == 'n')
+					curchar = '\n';
+				else if (curchar == 'r')
+					curchar = '\r';
+				else if (curchar == 't')
+					curchar = '\t';
+				else if (curchar != '\\' && curchar != '"')
+					errx(1, "%s(%" PRIu32 "): Illegal character escape",
+					     linkerScriptName, lineNo);
+			}
 
 			if (size >= capacity || token.attr.string == NULL) {
 				capacity *= 2;
--- /dev/null
+++ b/test/link/linkerscript-escapes-test.link
@@ -1,0 +1,3 @@
+ROM0
+	"A\"B\tC\rD\nE"
+	"in\{valid"
--- /dev/null
+++ b/test/link/linkerscript-escapes-test.out
@@ -1,0 +1,1 @@
+error: ./linkerscript-escapes-test.link(3): Illegal character escape
--- /dev/null
+++ b/test/link/linkerscript-escapes.asm
@@ -1,0 +1,4 @@
+SECTION "A\"B\tC\rD\nE", ROM0
+DS $1000
+SECTION "in\{valid", ROM0
+DS $1000