blob: be82631b07d4ec3aefb24140f9cdc5d7b35b6ec5 [file] [log] [blame]
Adam Snaider1c095c92023-07-08 02:09:58 -04001#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
26extern "C" uint8_t *__rdl_alloc(uintptr_t size, uintptr_t align);
27extern "C" __attribute__((weak))
28uint8_t *__rust_alloc(uintptr_t size, uintptr_t align) {
29 return __rdl_alloc(size, align);
30}
31extern "C" void __rdl_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align);
32extern "C" __attribute__((weak))
33void __rust_dealloc(uint8_t *ptr, uintptr_t size, uintptr_t align) {
34 __rdl_dealloc(ptr, size, align);
35}
36extern "C" uint8_t *__rdl_realloc(uint8_t *ptr, uintptr_t old_size, uintptr_t align,
37 uintptr_t new_size);
38extern "C" __attribute__((weak))
39uint8_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}
43extern "C" uint8_t *__rdl_alloc_zeroed(uintptr_t size, uintptr_t align);
44extern "C" __attribute__((weak))
45uint8_t *__rust_alloc_zeroed(uintptr_t size, uintptr_t align) {
46 return __rdl_alloc_zeroed(size, align);
47}
48extern "C" void __rdl_oom(uintptr_t size, uintptr_t align);
49extern "C" __attribute__((weak))
50void __rust_alloc_error_handler(uintptr_t size, uintptr_t align) {
51 __rdl_oom(size, align);
52}