xref: /aosp_15_r20/external/pigweed/pw_bloat/bloat_macros.ld (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1/*
2 * Copyright 2022 The Pigweed Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 *     https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17/* Helper macros to define the symbols requires by pw_bloat to detect the
18 * utilization and memory regions of your program.
19 *
20 * Include this file into your pw_linker_script() as follows:
21 *
22 *   pw_linker_script("my_linker_script") {
23 *     includes = [ "$dir_pw_bloat/bloat_macros.ld" ]
24 *     linker_script = "my_project_linker_script.ld"
25 *   }
26 */
27
28/* Default alignment used when declaring new sections. In most cases the free
29 * space should be measured down to a multiple of 4 bytes, but this can be
30 * changed in needed. */
31#ifndef PW_BLOAT_SECTION_ALIGN
32#define PW_BLOAT_SECTION_ALIGN 4
33#endif
34
35/* Declares an unused_space section. Instantiate this macro from within the
36 * SECTIONS of your linker script, after every other section, for example:
37 *   PW_BLOAT_UNUSED_SPACE_SECTION(FLASH)
38 *   PW_BLOAT_UNUSED_SPACE_SECTION(RAM)
39 */
40#define PW_BLOAT_UNUSED_SPACE_SECTION(memory_region)                  \
41  .memory_region.unused_space(NOLOAD) : ALIGN(PW_BLOAT_SECTION_ALIGN) \
42  {                                                                   \
43    . = ABSOLUTE(ORIGIN(memory_region) + LENGTH(memory_region));      \
44  } > memory_region
45
46/* Declares a memory region in pw_bloat mapped to the same name. Example:
47 *   PW_BLOAT_MEMORY_REGION(FLASH)
48 */
49#define PW_BLOAT_MEMORY_REGION(memory_region) \
50  PW_BLOAT_MEMORY_REGION_MAP(memory_region, memory_region)
51
52/* Declares a memory region in pw_bloat mapped to a different name. Can be used
53 * multiple times to map multiple aliased memory regions to the same name.
54 *   PW_BLOAT_MEMORY_REGION_MAP(RAM, ITCM)
55 *   PW_BLOAT_MEMORY_REGION_MAP(RAM, DTCM)
56 */
57#define PW_BLOAT_MEMORY_REGION_MAP(name, memory_region) \
58  PW_BLOAT_MEMORY_REGION_MAP_N(name, memory_region, __COUNTER__)
59
60/* Alternative version of PW_BLOAT_MEMORY_REGION_MAP to also specify the index
61 * value. This index value is irrelevant for pw_bloat tools as long as it
62 * doesn't repeat for the same name. Use this macro if you need to specify the
63 * index value for other tools.
64 * Note: this uses two macros to expand __COUNTER__ in
65 *       PW_BLOAT_MEMORY_REGION_MAP. */
66#define PW_BLOAT_MEMORY_REGION_MAP_N(name, memory_region, index) \
67  _PW_BLOAT_MEMORY_REGION_MAP_N(name, memory_region, index)
68
69#define _PW_BLOAT_MEMORY_REGION_MAP_N(name, memory_region, index) \
70  pw_bloat_config_memory_region_##name##_start_##index =          \
71      ORIGIN(memory_region);                                      \
72  pw_bloat_config_memory_region_##name##_end_##index =            \
73      ORIGIN(memory_region) + LENGTH(memory_region);
74