From 9bcbfee5f3e7d69b59c985404b3e4e0788f4a607 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 14 May 2021 12:23:33 +0100 Subject: Add FNV-128 hash for legacy symbol mangling Rustc uses a SIP128 hash for the legacy symbol mangling but an FNV hash is simpler to implement this is a port of the implementation from golang stdlib hash package. The fingerprint for the hash is simple the function signiture for now. Rustc takes into account options such as -Cmetadata to generate uniqueness. We still need to implement an SIP128 and the V0 symbol mangling but this will do in the interim. Addresses: #305 Fixes: #428 --- gcc/rust/backend/rust-compile.cc | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'gcc/rust/backend/rust-compile.cc') diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 9dd3b57..480afc8b 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -20,6 +20,7 @@ #include "rust-compile-item.h" #include "rust-compile-expr.h" #include "rust-compile-struct-field-expr.h" +#include "fnv-hash.h" namespace Rust { namespace Compile { @@ -427,10 +428,23 @@ mangle_name (const std::string &name) return std::to_string (name.size ()) + name; } +// rustc uses a sip128 hash for legacy mangling, but an fnv 128 was quicker to +// implement for now static std::string -dummy_hash () +legacy_hash (const std::string &fingerprint) { - return "h0123456789abcdef"; + Hash::FNV128 hasher; + hasher.write ((const unsigned char *) fingerprint.c_str (), + fingerprint.size ()); + + uint64_t hi, lo; + hasher.sum (&hi, &lo); + + char hex[16 + 1]; + memset (hex, 0, sizeof hex); + snprintf (hex, sizeof hex, "%08lx%08lx", lo, hi); + + return "h" + std::string (hex, sizeof (hex) - 1); } static std::string @@ -464,21 +478,28 @@ mangle_self (const TyTy::BaseType *self) } std::string -Context::mangle_item (const std::string &name) const +Context::mangle_item (const TyTy::BaseType *ty, const std::string &name) const { const std::string &crate_name = mappings->get_current_crate_name (); + + const std::string hash = legacy_hash (ty->as_string ()); + const std::string hash_sig = mangle_name (hash); + return kMangledSymbolPrefix + mangle_name (crate_name) + mangle_name (name) - + mangle_name (dummy_hash ()) + kMangledSymbolDelim; + + hash_sig + kMangledSymbolDelim; } std::string -Context::mangle_impl_item (const TyTy::BaseType *self, +Context::mangle_impl_item (const TyTy::BaseType *self, const TyTy::BaseType *ty, const std::string &name) const { const std::string &crate_name = mappings->get_current_crate_name (); + + const std::string hash = legacy_hash (ty->as_string ()); + const std::string hash_sig = mangle_name (hash); + return kMangledSymbolPrefix + mangle_name (crate_name) + mangle_self (self) - + mangle_name (name) + mangle_name (dummy_hash ()) - + kMangledSymbolDelim; + + mangle_name (name) + hash_sig + kMangledSymbolDelim; } } // namespace Compile -- cgit v1.1