Skip to content

feat: Support symbol definitions in linker script#1307

Merged
lapla-cogito merged 7 commits into
wild-linker:mainfrom
lapla-cogito:defsym_in_linker_script
Nov 16, 2025
Merged

feat: Support symbol definitions in linker script#1307
lapla-cogito merged 7 commits into
wild-linker:mainfrom
lapla-cogito:defsym_in_linker_script

Conversation

@lapla-cogito

Copy link
Copy Markdown
Member

With --defsym support added in #1300, we can extend this to support top-level symbol definitions in linker scripts as well.

Comment on lines +10 to +11
defsym_start_aaa = start_aaa;
defsym_addr = 0x1234;

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.

Sample output from the tested binary:

(▰╹◡╹)❯  readelf -s wild/tests/build/linker-script.c-default-host.ld

Symbol table '.dynsym' contains 6 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000150     0 NOTYPE  GLOBAL DEFAULT    6 start_aaa
     2: 0000000000000150     0 NOTYPE  GLOBAL DEFAULT    6 defsym_start_aaa
     3: 0000000000000154     0 NOTYPE  GLOBAL DEFAULT    6 end_bar
     4: 0000000000000148     0 NOTYPE  GLOBAL DEFAULT    6 start_bar
     5: 0000000000001234     0 NOTYPE  GLOBAL DEFAULT  ABS defsym_addr

Symbol table '.symtab' contains 12 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS linker-script.c
     2: 0000000000000148     4 OBJECT  LOCAL  DEFAULT    6 foo1
     3: 000000000000014c     4 OBJECT  LOCAL  DEFAULT    6 baz1
     4: 0000000000000150     4 OBJECT  LOCAL  DEFAULT    6 aaa1
     5: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS
     6: 0000000000000158     0 OBJECT  LOCAL  DEFAULT    7 _DYNAMIC
     7: 0000000000000148     0 NOTYPE  GLOBAL DEFAULT    6 start_bar
     8: 0000000000001234     0 NOTYPE  GLOBAL DEFAULT  ABS defsym_addr
     9: 0000000000000150     0 NOTYPE  GLOBAL DEFAULT    6 start_aaa
    10: 0000000000000150     0 NOTYPE  GLOBAL DEFAULT    6 defsym_start_aaa
    11: 0000000000000154     0 NOTYPE  GLOBAL DEFAULT    6 end_bar

(▰╹◡╹)❯  readelf -s wild/tests/build/linker-script.c-default-host.wild

Symbol table '.dynsym' contains 6 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000002838     0 NOTYPE  GLOBAL DEFAULT    7 defsym_start_aaa
     2: 000000000000283c     0 NOTYPE  GLOBAL DEFAULT    7 end_bar
     3: 0000000000001234     0 NOTYPE  GLOBAL DEFAULT  ABS defsym_addr
     4: 0000000000002838     0 NOTYPE  GLOBAL DEFAULT    7 start_aaa
     5: 0000000000002830     0 NOTYPE  GLOBAL DEFAULT    7 start_bar

Symbol table '.symtab' contains 12 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS linker-script.c
     2: 0000000000002830     4 OBJECT  LOCAL  DEFAULT    7 foo1
     3: 0000000000002834     4 OBJECT  LOCAL  DEFAULT    7 baz1
     4: 0000000000002838     4 OBJECT  LOCAL  DEFAULT    7 aaa1
     5: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT    1 __ehdr_start
     6: 0000000000002620     0 NOTYPE  GLOBAL DEFAULT    6 _DYNAMIC
     7: 0000000000002830     0 NOTYPE  GLOBAL DEFAULT    7 start_bar
     8: 0000000000002838     0 NOTYPE  GLOBAL DEFAULT    7 start_aaa
     9: 000000000000283c     0 NOTYPE  GLOBAL DEFAULT    7 end_bar
    10: 0000000000002838     0 NOTYPE  GLOBAL DEFAULT    7 defsym_start_aaa
    11: 0000000000001234     0 NOTYPE  GLOBAL DEFAULT  ABS defsym_addr

@davidlattimore davidlattimore left a comment

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.

Great to have this!

At the moment, the distinction between --symdef and the equivalent from a linker script is maintained all the way through to the write phase. Would it be possible to add the relevant symbols to the prelude earlier on, then treat them the same from then on? i.e have the code that deals with these symbols in layout and writing be the same regardless of whether the symbol came from --symdef or a linker script.

Comment thread libwild/src/layout.rs Outdated
@lapla-cogito

Copy link
Copy Markdown
Member Author

Thanks, top-level symbol definitions specified in linker scripts are now merged during the prelude phase. This also simplifies the processing performed by update_defsym_symbol_resolutions.

@lapla-cogito lapla-cogito merged commit cebadf1 into wild-linker:main Nov 16, 2025
20 checks passed
@lapla-cogito lapla-cogito deleted the defsym_in_linker_script branch November 16, 2025 01:49
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