Brian Silverman | 8751d48 | 2022-05-18 23:28:44 -0700 | [diff] [blame] | 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 |
Brian Silverman | a8ad1af | 2022-07-23 16:05:12 -0700 | [diff] [blame^] | 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. |
Brian Silverman | 8751d48 | 2022-05-18 23:28:44 -0700 | [diff] [blame] | 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. |
Brian Silverman | a8ad1af | 2022-07-23 16:05:12 -0700 | [diff] [blame^] | 24 | __attribute__((weak)) uint8_t __rust_alloc_error_handler_should_panic = 0; |
Brian Silverman | 8751d48 | 2022-05-18 23:28:44 -0700 | [diff] [blame] | 25 | |
| 26 | uint8_t *__rdl_alloc(uintptr_t size, uintptr_t align); |
Brian Silverman | a8ad1af | 2022-07-23 16:05:12 -0700 | [diff] [blame^] | 27 | __attribute__((weak)) |
Brian Silverman | 8751d48 | 2022-05-18 23:28:44 -0700 | [diff] [blame] | 28 | uint8_t *__rust_alloc(uintptr_t size, uintptr_t align) { |
| 29 | return __rdl_alloc(size, align); |
| 30 | } |
| 31 | void __rdl_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align); |
Brian Silverman | a8ad1af | 2022-07-23 16:05:12 -0700 | [diff] [blame^] | 32 | __attribute__((weak)) |
Brian Silverman | 8751d48 | 2022-05-18 23:28:44 -0700 | [diff] [blame] | 33 | void __rust_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align) { |
| 34 | __rdl_dealloc(ptr, size, align); |
| 35 | } |
| 36 | uint8_t *__rdl_realloc(uint8_t *ptr, uintptr_t old_size, uintptr_t align, |
| 37 | uintptr_t new_size); |
Brian Silverman | a8ad1af | 2022-07-23 16:05:12 -0700 | [diff] [blame^] | 38 | __attribute__((weak)) |
Brian Silverman | 8751d48 | 2022-05-18 23:28:44 -0700 | [diff] [blame] | 39 | uint8_t *__rust_realloc(uint8_t *ptr, uintptr_t old_size, uintptr_t align, |
| 40 | uintptr_t new_size) { |
| 41 | return __rdl_realloc(ptr, old_size, align, new_size); |
| 42 | } |
| 43 | uint8_t *__rdl_alloc_zeroed(uintptr_t size, uintptr_t align); |
Brian Silverman | a8ad1af | 2022-07-23 16:05:12 -0700 | [diff] [blame^] | 44 | __attribute__((weak)) |
Brian Silverman | 8751d48 | 2022-05-18 23:28:44 -0700 | [diff] [blame] | 45 | uint8_t *__rust_alloc_zeroed(uintptr_t size, uintptr_t align) { |
| 46 | return __rdl_alloc_zeroed(size, align); |
| 47 | } |
| 48 | void __rdl_oom(uintptr_t size, uintptr_t align); |
Brian Silverman | a8ad1af | 2022-07-23 16:05:12 -0700 | [diff] [blame^] | 49 | __attribute__((weak)) |
Brian Silverman | 8751d48 | 2022-05-18 23:28:44 -0700 | [diff] [blame] | 50 | void __rust_alloc_error_handler(uintptr_t size, uintptr_t align) { |
| 51 | __rdl_oom(size, align); |
| 52 | } |