Skip to content

feat: Support expressions in --defsym and linker script symbol assignment#1418

Merged
lapla-cogito merged 2 commits into
wild-linker:mainfrom
lapla-cogito:defsym_expr
Jan 6, 2026
Merged

feat: Support expressions in --defsym and linker script symbol assignment#1418
lapla-cogito merged 2 commits into
wild-linker:mainfrom
lapla-cogito:defsym_expr

Conversation

@lapla-cogito
Copy link
Copy Markdown
Member

Previously, symbol assignments in the --defsym option or in linker scripts could only contain absolute addresses or symbol names on the right-hand side of the assignment. However, other existing linkers allow well-formed expressions on the right-hand side.

The following statements are from the description of the --defsym option in the GNU ld man page:

A limited form of arithmetic is supported for the expression in this context: you may specify a hexadecimal constant or the name of an existing symbol, or use "+" and "-" to add or subtract hexadecimal constants or symbols. For more complex expressions, consider using the linker command language from a script. Note: there should be no whitespace between the symbol, the equals sign ("="), and the expression.

Additionally, documentation on symbol assignments in linker scripts can be found here:
https://sourceware.org/binutils/docs-2.17/ld/Simple-Assignments.html#Simple-Assignments

While this documentation mentions expressions such as symbol += expression, it is unclear whether these are widely used in practice. Therefore, I plan to implement this support in a separate PR.

Comment thread libwild/src/parsing.rs
}
}

pub fn parse_symbol_expression(s: &str) -> ParsedSymbolExpression<'_> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any parse errors that we should report?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, having multiple symbols on the right side of an expression is problematic. While we could consider implementing an error check for this, at least GNU ld appears to simply ignore such cases:

(▰╹◡╹)❯  cat main.c

int test_func(void) {
    return 1;
}

int main(void) {
}

(▰╹◡╹)❯  gcc main.c -Wl,--defsym=foo=test_func+test_func

(▰╹◡╹)❯  readelf -s a.out
...
Symbol table '.symtab' contains 37 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
...
    23: 0000000000001129    15 FUNC    GLOBAL DEFAULT   12 test_func
...
    28: 00000000000001d2     0 NOTYPE  GLOBAL DEFAULT  ABS foo

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably be inclined to report errors for such cases, but up to you. It seems unlikely that anyone would make such a mistake. I guess other errors could be numbers that don't parse correctly. e.g. if someone were to write a hex number without the "0x" prefix, although if the first digit is a letter then it'd be ambiguous anyway.

Comment thread wild/tests/sources/linker-script.ld Outdated
Copy link
Copy Markdown
Member

@davidlattimore davidlattimore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for implementing this!

Comment thread libwild/src/parsing.rs
}
}

pub fn parse_symbol_expression(s: &str) -> ParsedSymbolExpression<'_> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably be inclined to report errors for such cases, but up to you. It seems unlikely that anyone would make such a mistake. I guess other errors could be numbers that don't parse correctly. e.g. if someone were to write a hex number without the "0x" prefix, although if the first digit is a letter then it'd be ambiguous anyway.

@lapla-cogito lapla-cogito merged commit cca7a5b into wild-linker:main Jan 6, 2026
21 checks passed
@lapla-cogito lapla-cogito deleted the defsym_expr branch January 6, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants