Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent non-numeric symbol from overriding refs #752

Merged
merged 1 commit into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/asm/symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,10 @@ void sym_SetCurrentSymbolScope(char const *newScope)
* Create a symbol that will be non-relocatable and ensure that it
* hasn't already been defined or referenced in a context that would
* require that it be relocatable
* @param symbolName The name of the symbol to create
* @param numeric If false, the symbol may not have been referenced earlier
*/
static struct Symbol *createNonrelocSymbol(char const *symbolName)
static struct Symbol *createNonrelocSymbol(char const *symbolName, bool numeric)
{
struct Symbol *symbol = sym_FindExactSymbol(symbolName);

Expand All @@ -376,6 +378,12 @@ static struct Symbol *createNonrelocSymbol(char const *symbolName)
error("'%s' already defined at ", symbolName);
dumpFilename(symbol);
putc('\n', stderr);
} else if (!numeric) {
// The symbol has already been referenced, but it's not allowed
error("'%s' already referenced at ", symbolName);
dumpFilename(symbol);
putc('\n', stderr);
return NULL; // Don't allow overriding the symbol, that'd be bad!
}

return symbol;
Expand All @@ -386,7 +394,10 @@ static struct Symbol *createNonrelocSymbol(char const *symbolName)
*/
struct Symbol *sym_AddEqu(char const *symName, int32_t value)
{
struct Symbol *sym = createNonrelocSymbol(symName);
struct Symbol *sym = createNonrelocSymbol(symName, true);

if (!sym)
return NULL;

sym->type = SYM_EQU;
sym->value = value;
Expand All @@ -408,10 +419,12 @@ struct Symbol *sym_AddEqu(char const *symName, int32_t value)
*/
struct Symbol *sym_AddString(char const *symName, char const *value)
{
struct Symbol *sym = createNonrelocSymbol(symName);
struct Symbol *sym = createNonrelocSymbol(symName, false);

assignStringSymbol(sym, value);
if (!sym)
return NULL;

assignStringSymbol(sym, value);
return sym;
}

Expand Down Expand Up @@ -614,7 +627,10 @@ void sym_Export(char const *symName)
*/
struct Symbol *sym_AddMacro(char const *symName, int32_t defLineNo, char *body, size_t size)
{
struct Symbol *sym = createNonrelocSymbol(symName);
struct Symbol *sym = createNonrelocSymbol(symName, false);

if (!sym)
return NULL;

sym->type = SYM_MACRO;
sym->macroSize = size;
Expand Down
13 changes: 13 additions & 0 deletions test/asm/ref-override-bad.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

SECTION "Bad!", ROM0

db W
W equ 0 ; OK

db X
X equs "0" ; Not OK

db Y
Y: macro ; Not ok
db 0
endm
5 changes: 5 additions & 0 deletions test/asm/ref-override-bad.err
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ERROR: ref-override-bad.asm(8):
'X' already referenced at ref-override-bad.asm(7)
ERROR: ref-override-bad.asm(13):
'Y' already referenced at ref-override-bad.asm(10)
error: Assembly aborted (2 errors)!
Empty file added test/asm/ref-override-bad.out
Empty file.