Skip to content

Adding an option to force compiling a crate as PIE (instead of PIC) #87934

Closed
@hlopko

Description

@hlopko

Hi all,

AFAIK we always compile crates (--crate-type=lib) in relocation-model=pic as PIC (we call LLVMRustSetModulePICLevel in https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/context.rs#L183). Only when we are building a binary crate we compile using PIE (https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/context.rs#L187).

This is a reasonable default, PIC objects can be used for both executables and shared libraries. PIE objects can only be used for executables, but PIE code can be faster than PIC code.

We would like introduce a way to build crates as PIE in performance-critical scenarios where we know we won't need to produce shared libraries. Even further, cargo could detect that it's only building an executable and it could choose to build crates as PIE by default (but this is outside of the scope of this issue).

An obvious solution is to add a command line flag and check its value in https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/context.rs#L186. Would that be the best way to implement this? Would it be a welcomed change?

Thank you and have a lovely day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions