Skip to content

Commit d2d682f

Browse files
committed
Auto merge of rust-lang#127173 - bjorn3:mangle_rustc_std_internal_symbol, r=wesleywiser,jieyouxu
Mangle rustc_std_internal_symbols functions This reduces the risk of issues when using a staticlib or rust dylib compiled with a different rustc version in a rust program. Currently this will either (in the case of staticlib) cause a linker error due to duplicate symbol definitions, or (in the case of rust dylibs) cause rustc_std_internal_symbols functions to be silently overridden. As rust gets more commonly used inside the implementation of libraries consumed with a C interface (like Spidermonkey, Ruby YJIT (curently has to do partial linking of all rust code to hide all symbols not part of the C api), the Rusticl OpenCL implementation in mesa) this is becoming much more of an issue. With this PR the only symbols remaining with an unmangled name are rust_eh_personality (LLVM doesn't allow renaming it) and `__rust_no_alloc_shim_is_unstable`. Helps mitigate rust-lang#104707 try-job: aarch64-gnu-debug try-job: aarch64-apple try-job: x86_64-apple-1 try-job: x86_64-mingw-1 try-job: i686-mingw-1 try-job: x86_64-msvc-1 try-job: i686-msvc-1 try-job: test-various try-job: armhf-gnu
2 parents 8902bd3 + cbaf83b commit d2d682f

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

alloc/src/alloc.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,28 @@ use core::ptr::{self, NonNull};
1010

1111
unsafe extern "Rust" {
1212
// These are the magic symbols to call the global allocator. rustc generates
13-
// them to call `__rg_alloc` etc. if there is a `#[global_allocator]` attribute
13+
// them to call the global allocator if there is a `#[global_allocator]` attribute
1414
// (the code expanding that attribute macro generates those functions), or to call
1515
// the default implementations in std (`__rdl_alloc` etc. in `library/std/src/alloc.rs`)
1616
// otherwise.
17-
// The rustc fork of LLVM 14 and earlier also special-cases these function names to be able to optimize them
18-
// like `malloc`, `realloc`, and `free`, respectively.
1917
#[rustc_allocator]
2018
#[rustc_nounwind]
19+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
2120
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
2221
#[rustc_deallocator]
2322
#[rustc_nounwind]
23+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
2424
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
2525
#[rustc_reallocator]
2626
#[rustc_nounwind]
27+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
2728
fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
2829
#[rustc_allocator_zeroed]
2930
#[rustc_nounwind]
31+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
3032
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
3133

34+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
3235
static __rust_no_alloc_shim_is_unstable: u8;
3336
}
3437

@@ -357,6 +360,7 @@ unsafe extern "Rust" {
357360
// This is the magic symbol to call the global alloc error handler. rustc generates
358361
// it to call `__rg_oom` if there is a `#[alloc_error_handler]`, or to call the
359362
// default implementations below (`__rdl_oom`) otherwise.
363+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
360364
fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
361365
}
362366

@@ -423,6 +427,7 @@ pub mod __alloc_error_handler {
423427
unsafe extern "Rust" {
424428
// This symbol is emitted by rustc next to __rust_alloc_error_handler.
425429
// Its value depends on the -Zoom={panic,abort} compiler option.
430+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
426431
static __rust_alloc_error_handler_should_panic: u8;
427432
}
428433

panic_unwind/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ cfg_if::cfg_if! {
7979
unsafe extern "C" {
8080
/// Handler in std called when a panic object is dropped outside of
8181
/// `catch_unwind`.
82+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
8283
fn __rust_drop_panic() -> !;
8384

8485
/// Handler in std called when a foreign exception is caught.
86+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
8587
fn __rust_foreign_exception() -> !;
8688
}
8789

std/src/alloc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ fn default_alloc_error_hook(layout: Layout) {
348348
unsafe extern "Rust" {
349349
// This symbol is emitted by rustc next to __rust_alloc_error_handler.
350350
// Its value depends on the -Zoom={panic,abort} compiler option.
351+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
351352
static __rust_alloc_error_handler_should_panic: u8;
352353
}
353354

std/src/panicking.rs

+2
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ pub static EMPTY_PANIC: fn(&'static str) -> ! =
5555
// hook up these functions, but it is not this day!
5656
#[allow(improper_ctypes)]
5757
unsafe extern "C" {
58+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
5859
fn __rust_panic_cleanup(payload: *mut u8) -> *mut (dyn Any + Send + 'static);
5960
}
6061

6162
unsafe extern "Rust" {
6263
/// `PanicPayload` lazily performs allocation only when needed (this avoids
6364
/// allocations when using the "abort" panic runtime).
65+
#[cfg_attr(not(bootstrap), rustc_std_internal_symbol)]
6466
fn __rust_start_panic(payload: &mut dyn PanicPayload) -> u32;
6567
}
6668

0 commit comments

Comments
 (0)