Is there a way to save version information in an executable or Rust library?

When a Rust binary (executable or dylib) is created, the version information configured in Cargo.tomldoes not affect the binary structure, that is, the configured version is not saved inside the binary.

On Linux, when I use readelf -Vfor the .so file, you can see that the supported interface (SO Name) is stored in the Version Definition section .gnu.version_d ' ELF file. For example, the output readelf -V /lib/libnss_files-2.12.so:

Version definition section '.gnu.version_d' contains 2 entries:
   Addr: 0x0000000000001540  Offset: 0x001540  Link: 5 (.dynstr)
   000000: Rev: 1  Flags: BASE   Index: 1  Cnt: 1  Name: libnss_files.so.2
   0x001c: Rev: 1  Flags: none  Index: 2  Cnt: 1  Name: GLIBC_PRIVATE

The file /lib/libnss_files-2.12.soimplements an interface versionlibnss_files.so.2

The output readelf -Vfor a created Rust or Cargo dylib or executable does not have this version information. Version configuration in is Cargo.tomlused only by crates.io.

In addition, Windows DLLs support storing version information rather than the version name of the SONAME interface, such as Linux. The cross-compiled Windows DLL also has no version information. Perhaps this is another question, but I thought I would raise it here first.

+4
source share
1 answer

Although I don’t think it’s possible to use ELF tools for version control (they are still not cross-platform), you can use version information from Cargo:

const VERSION: &'static str = env!("CARGO_PKG_VERSION");

VERSIONwill now be equal to the version specified in the manifest when executed cargo build. In addition, you can use env_opt!()it if you want to create your own program without Cargo:

const VERSION: Option<&'static str> = env_opt!("CARGO_PKG_VERSION");
+2

Source: https://habr.com/ru/post/1622852/


All Articles