• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdint.h>
2 
3 // This file has some exciting magic to get Rust code linking in a cc_binary.
4 // The Rust compiler generates some similar symbol aliases when it links, so we
5 // have to do it manually. We mark all our symbols as weak so that linking this
6 // via Rust tooling to produce a binary with a Rust main works.
7 //
8 // It is intended to be used in rust_toolchain.allocator_library.
9 //
10 // https://github.com/rust-lang/rust/blob/master/library/alloc/src/alloc.rs
11 // and https://github.com/rust-lang/rust/blob/master/library/std/src/alloc.rs
12 // are the best source of docs I've found on these functions and variables.
13 // https://doc.rust-lang.org/std/alloc/index.html talks about how this is
14 // intended to be used.
15 //
16 // Also note
17 // https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html for
18 // the sizes of the various integer types.
19 //
20 // This file strongly assumes that the default allocator is used. It will
21 // not work with any other allocated switched in via `#[global_allocator]`.
22 
23 // New feature as of https://github.com/rust-lang/rust/pull/88098.
24 __attribute__((weak)) uint8_t __rust_alloc_error_handler_should_panic = 0;
25 
26 extern "C" uint8_t *__rdl_alloc(uintptr_t size, uintptr_t align);
__rust_alloc(uintptr_t size,uintptr_t align)27 extern "C" __attribute__((weak)) uint8_t *__rust_alloc(uintptr_t size,
28                                                        uintptr_t align) {
29     return __rdl_alloc(size, align);
30 }
31 extern "C" void __rdl_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align);
__rust_dealloc(uint8_t * ptr,uintptr_t size,uintptr_t align)32 extern "C" __attribute__((weak)) void __rust_dealloc(uint8_t *ptr,
33                                                      uintptr_t size,
34                                                      uintptr_t align) {
35     __rdl_dealloc(ptr, size, align);
36 }
37 extern "C" uint8_t *__rdl_realloc(uint8_t *ptr, uintptr_t old_size,
38                                   uintptr_t align, uintptr_t new_size);
__rust_realloc(uint8_t * ptr,uintptr_t old_size,uintptr_t align,uintptr_t new_size)39 extern "C" __attribute__((weak)) uint8_t *__rust_realloc(uint8_t *ptr,
40                                                          uintptr_t old_size,
41                                                          uintptr_t align,
42                                                          uintptr_t new_size) {
43     return __rdl_realloc(ptr, old_size, align, new_size);
44 }
45 extern "C" uint8_t *__rdl_alloc_zeroed(uintptr_t size, uintptr_t align);
__rust_alloc_zeroed(uintptr_t size,uintptr_t align)46 extern "C" __attribute__((weak)) uint8_t *__rust_alloc_zeroed(uintptr_t size,
47                                                               uintptr_t align) {
48     return __rdl_alloc_zeroed(size, align);
49 }
50 extern "C" void __rdl_oom(uintptr_t size, uintptr_t align);
__rust_alloc_error_handler(uintptr_t size,uintptr_t align)51 extern "C" __attribute__((weak)) void __rust_alloc_error_handler(
52     uintptr_t size, uintptr_t align) {
53     __rdl_oom(size, align);
54 }
55 
56 // New requirement as of Rust 1.71.0. For more details see
57 // https://github.com/rust-lang/rust/issues/73632.
58 __attribute__((weak)) uint8_t __rust_no_alloc_shim_is_unstable = 0;
59