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

Add a way to generate a list of symbols for dynamic linkage #916

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TheElectronWill
Copy link

@TheElectronWill TheElectronWill commented Jan 8, 2024

This resolves #907 by adding the ability to generate the list of exportable symbols (i.e. functions and globals), in a format that the linker (ld) can understand. Like depfile, this new feature is available both as a CLI option (--symfile) and as a method in Bindings. Therefore, it's easy to use it in a build script.

Most of the new files are tests on the output of the new option --symfile.

@TheElectronWill TheElectronWill changed the title Add a way to generate a list of symbols for dynamic linkage, resolves… Add a way to generate a list of symbols for dynamic linkage Jan 8, 2024
Copy link
Contributor

@jschwe jschwe left a comment

Choose a reason for hiding this comment

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

Nice - I might need exactly something like this in a project of mine in the next few months.

in a format that the linker (ld) can understand.

Which linkers specifically support this option? Is the --dynamic-list option of the gold linker compatible with the format, or did you only test with ldd? Is there any documentation on the exact format?

src/bindgen/bindings.rs Outdated Show resolved Hide resolved
@TheElectronWill
Copy link
Author

TheElectronWill commented Jan 9, 2024

Which linkers specifically support this option? Is the --dynamic-list option of the gold linker compatible with the format, or did you only test with ldd? Is there any documentation on the exact format?

The manual doesn't clearly explain the format. Gnu linker's manual reads "The format of the dynamic list is the same as the version node without scope and node name. See VERSION", and that "version" thing is introduced here.

I would expect at least GNU ld, lld and gold to work with that (I have only tested ld for the moment, but I'm confident that it works with lld too since it's been implemented with the same format in 2016). It would be weird for a linker to offer the same option but with a different format IMO.

@TheElectronWill
Copy link
Author

rebased

Copy link
Collaborator

@emilio emilio left a comment

Choose a reason for hiding this comment

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

Thanks, this looks good. Probably needs a rebase (can get to it if you can't) and I have one suggestion to avoid a couple intermediary allocations. But lmk if it's unreasonable.

@@ -131,6 +131,28 @@ impl Bindings {
fields
}

/// Lists the exported symbols that can be dynamically linked, i.e. globals and functions.
pub fn dynamic_symbols_names(&self) -> Vec<String> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably:

fn dynamic_symbols_names(&self) -> impl Iterator<Item = &str>

And just chain the two things:

self.functions.iter().map(|f| f.path().name())
    .chain(self.globals.iter().map(|g| g.export_name()))

std::fs::create_dir_all(dir).unwrap();
}
let mut symfile = File::create(symfile_path).unwrap();
write!(&mut symfile, "{{\n{};\n}};\n", symbols.join(";\n")).expect("Writing symbol file failed");
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd probably write this iteratively using the above function rather than storing all the file contents in memory, but it's probably not a huge deal.

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.

Allow access to bindings symbols names
3 participants