Add support for shared library import/export symbol tracking and thunk generation#1703
Open
chrislimpach wants to merge 7 commits intodtolnay:masterfrom
Open
Add support for shared library import/export symbol tracking and thunk generation#1703chrislimpach wants to merge 7 commits intodtolnay:masterfrom
chrislimpach wants to merge 7 commits intodtolnay:masterfrom
Conversation
The new `IMPLEMENTATION` constant provides the complete content of the "src/cxx.cc" implementation file. This `#include "../include/cxx.h"` is changed to avoid imposing a directory structure on users of `cxx_gen`.
otherwise calling Content::flush leaves blocks_pending set, resulting in incorrect behaviour when Content::flush_blocks is called again
OutFile::with_buffer allows using write!(out, ...) and the various write_*(out, ...) functions to generate output into a String, which can then be used when generating more complex output.
Rafactor with the aim to make imported and exported symbols more accessible, as well as function signatures. Extracted `get_extern_function_signature_parts`, which returns `return_type`, `signature_args` and `noexcept` used to output the function signature. Extracted symbol name generation from writeln! calls, i.e. store the symbol name in a variable which is then used in the writeln! call.
Introduced `format_symbols_for_linker` function to generate OS-specific linker file content. Added public `format_import_symbols_for_linker` and `format_export_symbols_for_linker` functions for handling import/export symbols using `format_symbols_for_linker`. Enhanced `GeneratedCode` struct with public methods to retrieve import/export symbols and generate import thunks (Windows-specific, no-op on other platforms). Updated `OutFile` to manage import/export symbols, including storing data to generate import thunks. Extended `write.rs` to record import/export symbols for function shims and all type support shims.
3768101 to
0d1163e
Compare
0d1163e to
d37b8e3
Compare
+ Introduced a new integration test for validating shared library functionality. + Added a new workspace member `tests/shared_library` to the `Cargo.toml`. + Implemented a shared library test suite to verify `cxx-gen`'s ability to handle DLL scenarios with bidirectional function calls. + Created `tests/shared_library/library` with `lib.rs`, `build.rs`, and `Cargo.toml` for the shared library. + Added `tests/main.cc` and `tests/test_shared_library.rs` for testing the shared library's interaction with a host executable. + Updated `.gitignore` to exclude generated files from the shared library tests.
rustc <1.90.0 does not handle the version_script used in the Linux test code -- skip using a version script with rustc <1.90.0 since this is not testing essential functionality. this cset can be reverted once the msrv for cxx moves past 1.89.0
d37b8e3 to
24ff6d4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In this PR, I've added functionality to
cxx-genfor building better shared libraries (DLLs/SOs) which can bidirectionally call functions between the library and the executable loading it.I've focused on building the bridge code as part of the C++ code, since in my project the C++ code doesn't compile using the system C++ standard library. This also avoids having to deal with C++ symbol name mangling across the executable/shared-library boundary, since the import/export symbols are all generated by cxx using its own deterministic naming scheme.
All platforms (Windows, macOS, Linux) use the concept of import libraries to declare symbols imported from shared libraries into executables.
Windows has more specific requirements for importing/exporting symbols:
On other platforms (macOS, Linux), these requirements are less strict since it's possible and common to export/import all symbols, and the dynamic linker supports bidirectional symbol resolution.
With these changes, it's easier to better constrain import/export symbol handling:
-U _symbolinstead of-export_dynamic--dynamic-listinstead of--export-dynamicImplementation
Main commit (021bee3) adds three new public APIs to
cxx-gen:format_import_symbols_for_linker(symbols, target_os)- Formats symbols the library imports from the executable:.deffile format (EXPORTSsection)-U _symbollinker arguments for dynamic lookup{ symbol; }) for--dynamic-listformat_export_symbols_for_linker(symbols, target_os)- Formats symbols the library exports (Windows.defonly)GeneratedCodemethods:export_symbols()- List of mangled symbols the library exportsimport_symbols()- List of mangled symbols imported from executablegenerate_import_thunks(target_os)- Generates C++ thunk implementations for Windows that useGetProcAddressto resolve symbols at runtimeSupporting Refactorings
The following preparatory commits enabled this functionality:
cxx_gen::IMPLEMENTATIONconstant and adjust include pathsContent::flush_blocksto properly clearblocks_pendingOutFile::with_buffermethod for capturing generated output into stringsTesting
65f27b8 adds a test in shared_library demonstrating the complete workflow on Windows, Linux, and macOS, including:
24ff6d4 adds a work-around for an incompatibility in the Linux test code with rustc versions <1.90.0. I have not investigated the root cause of this issue since the version-script use in the test is not testing essential functionality of this PR and the incompatibility is resolved in newer rustc versions.
This cset can be reverted once the msrv for cxx moves past 1.89.0