feat: Support expressions in --defsym and linker script symbol assignment#1418
Conversation
6d039c6 to
b726ed3
Compare
| } | ||
| } | ||
|
|
||
| pub fn parse_symbol_expression(s: &str) -> ParsedSymbolExpression<'_> { |
There was a problem hiding this comment.
Are there any parse errors that we should report?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
davidlattimore
left a comment
There was a problem hiding this comment.
Thanks for implementing this!
| } | ||
| } | ||
|
|
||
| pub fn parse_symbol_expression(s: &str) -> ParsedSymbolExpression<'_> { |
There was a problem hiding this comment.
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.
Previously, symbol assignments in the
--defsymoption 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
--defsymoption in the GNU ld man page: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.