Skip to content

Commit

Permalink
Add & document rustc -C export-executable-symbols
Browse files Browse the repository at this point in the history
MsvcLinker:
- Give export_symbols a debug!() header to match GccLinker
- Make debug!() dumped symbols match actual .def symbols
- Export executable symbols when -C export-executable-symbols

GccLinker:
- Export executable symbols when -C export-executable-symbols
  • Loading branch information
MaulingMonkey committed Dec 29, 2019
1 parent f564c4d commit aa17dfb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/doc/rustc/src/codegen-options/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ for determining whether or not it is possible to statically or dynamically
link with a dependency. For example, `cdylib` crate types may only use static
linkage.

## export-executable-symbols

This flag causes `rustc` to export symbols from executables, as if they were dynamic libraries.

You might use this to allow the JVM or MSCLR to call back into your executable's
Rust code from Java/C# when embedding their runtimes into your Rust executable.

## no-integrated-as

`rustc` normally uses the LLVM internal assembler to create object code. This
Expand Down
9 changes: 7 additions & 2 deletions src/librustc_codegen_ssa/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ impl<'a> Linker for GccLinker<'a> {
// Symbol visibility in object files typically takes care of this.
if crate_type == CrateType::Executable
&& self.sess.target.target.options.override_export_symbols.is_none()
&& !self.sess.opts.cg.export_executable_symbols
{
return;
}
Expand Down Expand Up @@ -699,10 +700,14 @@ impl<'a> Linker for MsvcLinker<'a> {
// their symbols exported.
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
// Symbol visibility takes care of this typically
if crate_type == CrateType::Executable {
if crate_type == CrateType::Executable
&& !self.sess.opts.cg.export_executable_symbols
{
return;
}

debug!("EXPORTED SYMBOLS:");

let path = tmpdir.join("lib.def");
let res: io::Result<()> = try {
let mut f = BufWriter::new(File::create(&path)?);
Expand All @@ -712,7 +717,7 @@ impl<'a> Linker for MsvcLinker<'a> {
writeln!(f, "LIBRARY")?;
writeln!(f, "EXPORTS")?;
for symbol in self.info.exports[&crate_type].iter() {
debug!(" _{}", symbol);
debug!(" {}", symbol);
writeln!(f, " {}", symbol)?;
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_session/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
"use soft float ABI (*eabihf targets only)"),
prefer_dynamic: bool = (false, parse_bool, [TRACKED],
"prefer dynamic linking to static linking"),
export_executable_symbols: bool = (false, parse_bool, [UNTRACKED],
"export symbols from executables, as if they were dynamic libraries"),
no_integrated_as: bool = (false, parse_bool, [TRACKED],
"use an external assembler rather than LLVM's integrated one"),
no_redzone: Option<bool> = (None, parse_opt_bool, [TRACKED],
Expand Down

0 comments on commit aa17dfb

Please sign in to comment.