Giter Club home page Giter Club logo

Comments (2)

sunjay avatar sunjay commented on June 19, 2024

Name Mangling Scheme

The hash values below are made up, but should eventually come from some decent algorithm for short hashes.

  1. package/module
    • Fully qualified name of module is used, with each part mangled individually
    • Includes the mangled package name (e.g. std)
    • Hash for each part is created using "mod" + module name
    • :: becomes __
    • Example: std::num becomes std_493ef8a1__num_839a1b34
  2. type
    • Mangled name of containing module + __ + mangled name of type
    • Hash for type is created using "type" + type name
    • Example: std::num::NonZeroU32 becomes std_493ef8a1__num_839a1b34__NonZeroU32_6743fa12
  3. method
    • Mangled name of type + __ + unmangled name of method
    • Method names are unique for a given type so this works
    • Example: std::num::NonZeroU32::new becomes std_493ef8a1__num_839a1b34__NonZeroU32_6743fa12__new
  4. function
    • Mangled name of containing module/function + __ + mangled name of function
    • Hash for function name is created using "fn" + function name
    • Don't want type names and function names to be able to conflict, so both are mangled with a different prefix for the hash
    • Example: std::io::copy becomes std_493ef8a1__io_15ad8c91__copy_ab77d31a
  5. variable/function parameter
    • Hash of variable name
    • Needed to prevent collisions with temporaries

The extra string included in the hash is because different namespaces can contain the same names. That is, you can technically have a type and a function named Foo.

Short Hashes

use std::io::Read;

use adler32::adler32;

fn mangle_function_name(mod_mangled_name: &str, name: &str) -> String {
    let prefix = "fn".as_bytes();
    // https://doc.rust-lang.org/nightly/std/io/trait.Read.html#method.chain
    let mangle_input = prefix.chain(name.as_bytes());
    // unwrap() is safe because Read for &[u8] can never result in an error.
    let hash = adler32(mangle_input).unwrap();
    format!("{}__{}_{:x}", mod_mangled_name, name, hash)
}

Update: Apparently adler32 isn't great for short strings. Might just want to truncate md5 to 32-bits. This may have issues too.

from dino.

sunjay avatar sunjay commented on June 19, 2024

Even simpler scheme that just uses the length and not a hash: https://rust-lang.github.io/rfcs/2603-symbol-name-mangling-v2.html#the-mangling-scheme-by-example

from dino.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.