mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 02:32:06 +00:00
Escape characters in section names in map files
This commit is contained in:
@@ -266,15 +266,15 @@ static bool isLegalForSymName(char c) {
|
|||||||
|| c == '@' || c == '#' || c == '$' || c == '.';
|
|| c == '@' || c == '#' || c == '$' || c == '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints a symbol's name to `symFile`, assuming that the first character is legal.
|
// Prints a symbol's name to a file, assuming that the first character is legal.
|
||||||
// Illegal characters are UTF-8-decoded (errors are replaced by U+FFFD) and emitted as `\u`/`\U`.
|
// Illegal characters are UTF-8-decoded (errors are replaced by U+FFFD) and emitted as `\u`/`\U`.
|
||||||
static void printSymName(char const *name) {
|
static void printSymName(std::string const &name, FILE *file) {
|
||||||
for (char const *ptr = name; *ptr != '\0';) {
|
for (char const *ptr = name.c_str(); *ptr != '\0';) {
|
||||||
char c = *ptr;
|
char c = *ptr;
|
||||||
|
|
||||||
if (isLegalForSymName(c)) {
|
if (isLegalForSymName(c)) {
|
||||||
// Output legal ASCII characters as-is
|
// Output legal ASCII characters as-is
|
||||||
putc(c, symFile);
|
putc(c, file);
|
||||||
++ptr;
|
++ptr;
|
||||||
} else {
|
} else {
|
||||||
// Output illegal characters using Unicode escapes
|
// Output illegal characters using Unicode escapes
|
||||||
@@ -295,7 +295,7 @@ static void printSymName(char const *name) {
|
|||||||
++ptr;
|
++ptr;
|
||||||
} while (state != 0);
|
} while (state != 0);
|
||||||
|
|
||||||
fprintf(symFile, codepoint <= 0xFFFF ? "\\u%04" PRIx32 : "\\U%08" PRIx32, codepoint);
|
fprintf(file, codepoint <= 0xFFFF ? "\\u%04" PRIx32 : "\\U%08" PRIx32, codepoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -376,7 +376,7 @@ static void writeSymBank(SortedSections const &bankSections, SectionType type, u
|
|||||||
|
|
||||||
for (SortedSymbol &sym : symList) {
|
for (SortedSymbol &sym : symList) {
|
||||||
fprintf(symFile, "%02" PRIx32 ":%04" PRIx16 " ", symBank, sym.addr);
|
fprintf(symFile, "%02" PRIx32 ":%04" PRIx16 " ", symBank, sym.addr);
|
||||||
printSymName(sym.sym->name.c_str());
|
printSymName(sym.sym->name, symFile);
|
||||||
putc('\n', symFile);
|
putc('\n', symFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -396,6 +396,31 @@ static void writeEmptySpace(uint16_t begin, uint16_t end) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prints a section's name to a file.
|
||||||
|
static void printSectionName(std::string const &name, FILE *file) {
|
||||||
|
for (char c : name) {
|
||||||
|
// Escape characters that need escaping
|
||||||
|
switch (c) {
|
||||||
|
case '\n':
|
||||||
|
fputs("\\n", file);
|
||||||
|
break;
|
||||||
|
case '\r':
|
||||||
|
fputs("\\r", file);
|
||||||
|
break;
|
||||||
|
case '\t':
|
||||||
|
fputs("\\t", file);
|
||||||
|
break;
|
||||||
|
case '\\':
|
||||||
|
case '"':
|
||||||
|
putc('\\', file);
|
||||||
|
[[fallthrough]];
|
||||||
|
default:
|
||||||
|
putc(c, file);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Write a bank's contents to the map file
|
* Write a bank's contents to the map file
|
||||||
*/
|
*/
|
||||||
@@ -430,32 +455,26 @@ static void writeMapBank(SortedSections const §List, SectionType type, uint3
|
|||||||
if (sect->size != 0)
|
if (sect->size != 0)
|
||||||
fprintf(
|
fprintf(
|
||||||
mapFile,
|
mapFile,
|
||||||
"\tSECTION: $%04" PRIx16 "-$%04x ($%04" PRIx16 " byte%s) [\"%s\"]\n",
|
"\tSECTION: $%04" PRIx16 "-$%04x ($%04" PRIx16 " byte%s) [\"",
|
||||||
sect->org,
|
sect->org,
|
||||||
prevEndAddr - 1,
|
prevEndAddr - 1,
|
||||||
sect->size,
|
sect->size,
|
||||||
sect->size == 1 ? "" : "s",
|
sect->size == 1 ? "" : "s"
|
||||||
sect->name.c_str()
|
|
||||||
);
|
);
|
||||||
else
|
else
|
||||||
fprintf(
|
fprintf(mapFile, "\tSECTION: $%04" PRIx16 " (0 bytes) [\"", sect->org);
|
||||||
mapFile,
|
printSectionName(sect->name, mapFile);
|
||||||
"\tSECTION: $%04" PRIx16 " (0 bytes) [\"%s\"]\n",
|
fputs("\"]\n", mapFile);
|
||||||
sect->org,
|
|
||||||
sect->name.c_str()
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!noSymInMap) {
|
if (!noSymInMap) {
|
||||||
// Also print symbols in the following "pieces"
|
// Also print symbols in the following "pieces"
|
||||||
for (uint16_t org = sect->org; sect; sect = sect->nextu.get()) {
|
for (uint16_t org = sect->org; sect; sect = sect->nextu.get()) {
|
||||||
for (Symbol *sym : sect->symbols)
|
for (Symbol *sym : sect->symbols) {
|
||||||
// Space matches "\tSECTION: $xxxx ..."
|
// Space matches "\tSECTION: $xxxx ..."
|
||||||
fprintf(
|
fprintf(mapFile, "\t $%04" PRIx32 " = ", sym->label().offset + org);
|
||||||
mapFile,
|
printSymName(sym->name, mapFile);
|
||||||
"\t $%04" PRIx32 " = %s\n",
|
putc('\n', mapFile);
|
||||||
sym->label().offset + org,
|
}
|
||||||
sym->name.c_str()
|
|
||||||
);
|
|
||||||
|
|
||||||
if (sect->nextu) {
|
if (sect->nextu) {
|
||||||
// Announce the following "piece"
|
// Announce the following "piece"
|
||||||
@@ -579,7 +598,9 @@ static void writeSym() {
|
|||||||
for (Symbol *sym : constants) {
|
for (Symbol *sym : constants) {
|
||||||
int32_t val = sym->data.get<int32_t>();
|
int32_t val = sym->data.get<int32_t>();
|
||||||
int width = val < 0x100 ? 2 : val < 0x10000 ? 4 : 8;
|
int width = val < 0x100 ? 2 : val < 0x10000 ? 4 : 8;
|
||||||
fprintf(symFile, "%0*" PRIx32 " %s\n", width, val, sym->name.c_str());
|
fprintf(symFile, "%0*" PRIx32 " ", width, val);
|
||||||
|
printSymName(sym->name, symFile);
|
||||||
|
putc('\n', symFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user