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