blob: ead4f8e21cc093a4d9db8516693431bcd2733fd4 [file] [log] [blame]
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +01001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Memory layout.
16
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010017use core::ops::Range;
18use core::ptr::addr_of;
19
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010020/// Get an address from a linker-defined symbol.
21#[macro_export]
22macro_rules! linker_addr {
23 ($symbol:ident) => {{
24 unsafe { addr_of!($crate::linker::$symbol) as usize }
25 }};
26}
27
28/// Get the address range between a pair of linker-defined symbols.
29#[macro_export]
30macro_rules! linker_region {
31 ($begin:ident,$end:ident) => {{
32 let start = linker_addr!($begin);
33 let end = linker_addr!($end);
34
35 start..end
36 }};
37}
38
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010039/// Memory reserved for the DTB.
40pub fn dtb_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010041 linker_region!(dtb_begin, dtb_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010042}
43
44/// Executable code.
45pub fn text_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010046 linker_region!(text_begin, text_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010047}
48
49/// Read-only data.
50pub fn rodata_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010051 linker_region!(rodata_begin, rodata_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010052}
53
54/// Initialised writable data.
55pub fn data_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010056 linker_region!(data_begin, data_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010057}
58
59/// Zero-initialised writable data.
60pub fn bss_range() -> Range<usize> {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010061 linker_region!(bss_begin, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010062}
63
64/// Writable data region for the stack.
Pierre-Clément Tosi23aba522023-04-21 17:03:50 +010065pub fn stack_range(stack_size: usize) -> Range<usize> {
66 let end = linker_addr!(init_stack_pointer);
67 let start = end.checked_sub(stack_size).unwrap();
68 assert!(start >= linker_addr!(stack_limit));
69
70 start..end
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010071}
72
Pierre-Clément Tosi8bb3d722023-04-21 16:10:56 +010073/// All writable sections, excluding the stack.
74pub fn scratch_range() -> Range<usize> {
Pierre-Clément Tosiccc11382023-04-21 16:44:53 +010075 linker_region!(eh_stack_limit, bss_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010076}
77
78/// Read-write data (original).
79pub fn data_load_address() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010080 linker_addr!(data_lma)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010081}
82
83/// End of the binary image.
84pub fn binary_end() -> usize {
Pierre-Clément Tosi5835d342023-04-21 18:02:32 +010085 linker_addr!(bin_end)
Pierre-Clément Tosi9a658f72022-10-10 15:18:54 +010086}