Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symbol Mangling #305

Open
2 of 3 tasks
philberty opened this issue Mar 26, 2021 · 9 comments · Fixed by #425
Open
2 of 3 tasks

Symbol Mangling #305

philberty opened this issue Mar 26, 2021 · 9 comments · Fixed by #425

Comments

@philberty
Copy link
Member

philberty commented Mar 26, 2021

Compiling Generics requires us to use symbol mangling to avoid the duplicate symbol error in linking.
This could also be part of a cleanup effort for the backend's handling for mangling in general.

@bjorn3
Copy link

bjorn3 commented Mar 26, 2021

The hash that is part of the symbol name includes both the crate name and value passed in using -Cmetadata if any (always done by cargo). This metadata value allows for multiple versions of the same crate without symbol conflicts. Rustc will error when trying to link multiple versions of the same crate with the same -Cmetadata but different SVH (semantic version hash, derived from all inputs of the compilation process) in a single crate graph. The crate types bin, staticlib and cdylib are always the root of a crate graph. The crate types rlib and dylib are not, but rustc still checks for conflicts as far as I know as any conflicts present there must also be present in the final crate graph.

@philberty
Copy link
Member Author

Thanks for the heads up I didn't realise about -Cmetadata which is a really neat idea.

@philberty
Copy link
Member Author

Rust has 2 forms of symbol mangling:

  1. Legacy which is what is in stable
  2. V0 which is unstable and defined in https://rust-lang.github.io/rfcs/2603-rust-symbol-name-mangling-v0.html

I think we will need to support both at some point for now lets implement legacy in the short term. Down the line when rust-lang/rust#60705 is stableized we can implement that which would be ideal.

@alex
Copy link

alex commented May 11, 2021

This is putting the cart way way way before the horse, but FYI: The Rust-for-Linux project uses the unstable v0 (though I have no recollection of why :-)). We're very interested in the progress of gccrs, for hopefully obvious reasons, so I figured I'd let you know.

@philberty
Copy link
Member Author

Thanks for the heads up @alex

@philberty
Copy link
Member Author

For the generics milestone right now I will be implementing basic legacy with a dummy hash for now.

philberty added a commit that referenced this issue May 11, 2021
Rust supports two different symbol mangling methods legacy and V0. V0 is
the goal but its not yet stable. This implements the legacy method but
with a dummy hash value since it requires a sip128 implementation to
generate the apropriate hash which can be done in a sperate change.

This change allows us to actually assemble generic functions and avoid
bad symbol duplications.

Addresses #305
@philberty
Copy link
Member Author

This PR implements the first part for legacy symbol mangling #425

@philberty philberty linked a pull request May 11, 2021 that will close this issue
@philberty
Copy link
Member Author

Moving this ticket out of the generics milestone, since we have a basic implementation of the legacy form will be enough for that milestone to be completed for now.

@philberty philberty removed this from the Data Structures 2 - Generics milestone May 11, 2021
bors bot added a commit that referenced this issue May 11, 2021
425: Basic implementation of legacy symbol mangling r=philberty a=philberty

    Implement basic rustc legacy symbol mangling
    
    Rust supports two different symbol mangling methods legacy and V0. V0 is
    the goal but its not yet stable. This implements the legacy method but
    with a dummy hash value since it requires a sip128 implementation to
    generate the apropriate hash which can be done in a sperate change.
    
    This change allows us to actually assemble generic functions and avoid
    bad symbol duplications.
    
    Addresses #305

Co-authored-by: Philip Herron <[email protected]>
@philberty philberty linked a pull request May 12, 2021 that will close this issue
@ojeda
Copy link
Contributor

ojeda commented May 12, 2021

The Rust-for-Linux project uses the unstable v0 (though I have no recollection of why :-)).

The reason is that the "legacy" mangling scheme uses invalid characters for C identifiers (like $) which is a problem when reusing the kernel's EXPORT_SYMBOL_* C macros. Since I noticed v0 does not, I simply went for that. :)

philberty added a commit that referenced this issue May 14, 2021
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
bors bot added a commit that referenced this issue May 14, 2021
430: Add FNV-128 hash for legacy symbol mangling r=philberty a=philberty

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 simply the function signature for now.
Rustc takes into account options such as -Cmetadata to generate uniqueness.

We still need to implement a SIP128 hasher and the V0 symbol mangling but this
will do in the interim.

Addresses: #305
Fixes: #428

Co-authored-by: Philip Herron <[email protected]>
@philberty philberty removed their assignment May 14, 2021
This was referenced Aug 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants