Skip to content

feat: support matching based on linker script input filenames#1596

Merged
lapla-cogito merged 6 commits into
wild-linker:mainfrom
levietduc0712:test_input_filename_matching
Feb 26, 2026
Merged

feat: support matching based on linker script input filenames#1596
lapla-cogito merged 6 commits into
wild-linker:mainfrom
levietduc0712:test_input_filename_matching

Conversation

@levietduc0712

Copy link
Copy Markdown
Contributor

This pull request adds support for matching input sections based on input file names (including glob patterns) in linker scripts. Now, section mapping rules can specify not only section name patterns but also which input files (or file patterns) they apply to, enabling more precise control over section placement. The implementation includes parsing, rule matching, and comprehensive tests for this new feature.

Enhancements to section rule matching:

  • Added an input_file_pattern field to SectionRule and Matcher, allowing rules to optionally specify a glob pattern for matching input filenames. If None, the rule matches all files.
  • Updated the SectionRule::matches method and section rule lookup logic to consider both section name and input file name, using a new glob_match_bytes function that supports * and ? wildcards.

Parsing and propagation of file patterns:

  • Modified the linker script parser to support file patterns in section commands, correctly populating the input_file_pattern field in Matcher.
  • Updated the section rule builder and related code to propagate file patterns from parsed matchers to section rules.

Integration and testing:

  • Updated the section resolution logic to pass the input file name to rule lookup, ensuring correct rule selection based on both section and file name.
  • Added extensive tests for parsing file patterns, glob matching logic, and section command parsing with file-specific and glob-specific matchers.

@lapla-cogito

Copy link
Copy Markdown
Member

How about adding test linker scripts to integration tests?

@levietduc0712

Copy link
Copy Markdown
Contributor Author

@lapla-cogito
Quick clarification: are you suggesting moving test_section_command_with_filename() to the integration tests, or adding a new integration test that includes a test linker script?

@levietduc0712 levietduc0712 force-pushed the test_input_filename_matching branch from 00ef5b7 to 9a4855e Compare February 26, 2026 07:40
@lapla-cogito

Copy link
Copy Markdown
Member

I intend the latter. Place the test code (in this case, likely a linker script and a C source file) under wild/tests/sources. After that, add the necessary test directives to the source file (see the beginning of wild/tests/integration_tests.rs for details), and then add the name of the newly added source file to integration_test(). Once this is done, running cargo test will execute the newly added test.

@levietduc0712 levietduc0712 marked this pull request as draft February 26, 2026 07:42
@levietduc0712 levietduc0712 marked this pull request as ready for review February 26, 2026 07:53
@levietduc0712

Copy link
Copy Markdown
Contributor Author

Is it okay now? Since it’s my first time, there might still be some changes needed.

@lapla-cogito lapla-cogito 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.

Thanks for working on this! I've added some comments. Also, CI seems to be failing checks for test code formatting and cargo-minimal-versions.

Comment thread libwild/src/resolution.rs
Comment thread wild/tests/sources/linker-script-filename-match.c
Comment thread wild/tests/sources/linker-script-filename-match.c
Comment thread libwild/src/layout_rules.rs Outdated

/// Performs glob-style matching of `pattern` against `text`. Supports `*` (matches any sequence of
/// characters) and `?` (matches any single character).
fn glob_match_bytes(pattern: &[u8], text: &[u8]) -> bool {

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.

Wild already has a dependency on the glob crate. It's used in version_script.rs. Can you look and see if that'd be suitable to use here? Probably call Pattern::new just once for each pattern, since I think that part of the process is slightly expensive, so it's good to not repeat it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion! I've replaced the custom glob_match_bytes with glob::Pattern from the existing dependency. The pattern is now compiled once in SectionRule::new() and stored as Option, so the parsing cost isn't repeated on every match. The matches() method now delegates to pattern.matches() with a UTF-8 conversion of the filename.

Comment thread wild/tests/integration_tests.rs Outdated
@davidlattimore

Copy link
Copy Markdown
Member

Might be worthwhile installing typos so that you can run it before you upload the change. I have a script that gets run whenever I'm uploading a change. It runs typos, runs the tests in a few different modes etc.

@davidlattimore

Copy link
Copy Markdown
Member

My script, in case you're wondering currently looks like the following.

#!/usr/bin/bash
set -e
typos
cargo +nightly fmt --check
cargo clippy --all-targets --features mold_tests -- -D warnings
cargo clippy --no-default-features --all-targets -- -D warnings
cargo clippy --features plugins
cargo test --features plugins
if [ -z "$SKIP_MOLD_TESTS" ]; then
    WILD_TEST_CROSS=aarch64 cargo nextest run -v --features mold_tests
fi

Or at least that's the wild-specific part of the script. I also have other more generic scripts that grep the diff of changes for things like "DO NOT SUBMIT", notify me of any added files, etc.

Comment thread wild/tests/sources/linker-script-filename-match.c Outdated
@davidlattimore

Copy link
Copy Markdown
Member

Where possible, it's good if you can make the title of the PR what will go into the release notes. If you prefix it with "feat:" then it'll go into the features section. I'll usually edit it when I'm merging the change if it doesn't seem quite right, but I sometimes forget.

@levietduc0712 levietduc0712 changed the title Add support for optional glob patterns in filename matching feat: support matching based on input filenames Feb 26, 2026
@lapla-cogito lapla-cogito changed the title feat: support matching based on input filenames feat: support matching based on linker script input filenames Feb 26, 2026

@lapla-cogito lapla-cogito 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.

I've modified the PR title to clearly indicate it relates to linker scripts (since this is typically documented in release notes, I think the relevant section should be more obvious). Otherwise, LGTM.

@lapla-cogito lapla-cogito merged commit 090746f into wild-linker:main Feb 26, 2026
20 checks passed
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.

3 participants