Skip to content

Commit 6413369

Browse files
committed
scripts: code_relocate: add option to inhibit .data relocation
This is activated by passing NOCOPY_DATA to zephyr_code_relocate(). It helps when building firmwares for coprocessors, where the image is loaded completely into SRAM from Linux-land using the remoteproc framework. Fixes: #86530 Signed-off-by: Robin Haberkorn <haberkorn@metratec.com>
1 parent 2e23dfd commit 6413369

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

cmake/modules/extensions.cmake

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,13 +1446,15 @@ endmacro()
14461446
# zephyr_code_relocate(LIBRARY my_lib SRAM)
14471447
#
14481448
# The following optional arguments are supported:
1449-
# - NOCOPY: this flag indicates that the file data does not need to be copied
1449+
# - NOCOPY: this flag indicates that the file's text section does not need to be copied
14501450
# at boot time (For example, for flash XIP).
1451+
# - NOCOPY_DATA: this flag indicates that the file's data section (read-write data)
1452+
# does not need to be copied at boot time (For example, if it is directly loaded into SRAM).
14511453
# - NOKEEP: suppress the generation of KEEP() statements in the linker script,
14521454
# to allow any unused code in the given files/library to be discarded.
14531455
# - PHDR [program_header]: add program header. Used on Xtensa platforms.
14541456
function(zephyr_code_relocate)
1455-
set(options NOCOPY NOKEEP)
1457+
set(options NOCOPY NOCOPY_DATA NOKEEP)
14561458
set(single_args LIBRARY LOCATION PHDR FILTER)
14571459
set(multi_args FILES)
14581460
cmake_parse_arguments(CODE_REL "${options}" "${single_args}"
@@ -1516,6 +1518,11 @@ function(zephyr_code_relocate)
15161518
else()
15171519
set(flag_list NOCOPY)
15181520
endif()
1521+
if(NOT CODE_REL_NOCOPY_DATA)
1522+
list(APPEND flag_list COPYDATA)
1523+
else()
1524+
list(APPEND flag_list NOCOPYDATA)
1525+
endif()
15191526
if(CODE_REL_NOKEEP)
15201527
list(APPEND flag_list NOKEEP)
15211528
endif()

doc/kernel/code-relocation.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ you can pass ``NOKEEP`` to your ``zephyr_code_relocate()`` call.
139139
The example above will help ensure that any unused code found in the .text
140140
sections of ``file1.c`` will not stick to SRAM2.
141141

142-
NOCOPY flag
143-
===========
142+
NOCOPY and NOCOPY_DATA flags
143+
============================
144144

145145
When a ``NOCOPY`` option is passed to the ``zephyr_code_relocate()`` function,
146146
the relocation code is not generated in ``code_relocation.c``. This flag can be
@@ -149,13 +149,18 @@ XIP area.
149149

150150
This example will place the .text section of the ``xip_external_flash.c`` file
151151
to the ``EXTFLASH`` memory region where it will be executed from (XIP). The
152-
.data will be relocated as usual into SRAM.
152+
.data will be relocated as usual into SRAM, since flash in XIP mode does not
153+
allow random write access and variables do not have to persist.
153154

154155
.. code-block:: none
155156
156157
zephyr_code_relocate(FILES src/xip_external_flash.c LOCATION EXTFLASH_TEXT NOCOPY)
157158
zephyr_code_relocate(FILES src/xip_external_flash.c LOCATION SRAM_DATA)
158159
160+
You can additionally specify ``NOCOPY_DATA`` if you do not want .data to be relocated into SRAM.
161+
This can be useful if it is already loaded into SRAM, e.g. by the Linux kernel
162+
using the `remoteproc framework <https://docs.kernel.org/staging/remoteproc.html>`_.
163+
159164
Relocating libraries
160165
====================
161166

scripts/build/gen_relocate_app.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
- If the memory type is appended with _DATA / _TEXT/ _RODATA/ _BSS only the
3333
selected memory is placed in the required memory region. Others are
3434
ignored.
35-
- COPY/NOCOPY defines whether the script should generate the relocation code in
36-
code_relocation.c or not
35+
- COPY/NOCOPY defines whether the script should generate the relocation code for text segment
36+
symbols in code_relocation.c or not
37+
- COPYDATA/NOCOPYDATA defines whether the script should generate the relocation code for data segment
38+
symbols in code_relocation.c or not
3739
- NOKEEP will suppress the default behavior of marking every relocated symbol
3840
with KEEP() in the generated linker script.
3941
@@ -389,9 +391,10 @@ def generate_linker_script(linker_file, sram_data_linker_file, sram_bss_linker_f
389391

390392
for memory_type, full_list_of_sections in \
391393
sorted(complete_list_of_sections.items()):
392-
393-
is_copy = bool("|COPY" in memory_type)
394-
memory_type = memory_type.split("|", 1)[0]
394+
memory_type = memory_type.split("|")
395+
is_copy = bool("COPY" in memory_type)
396+
is_copy_data = bool("COPYDATA" in memory_type)
397+
memory_type = memory_type[0]
395398

396399
if region_is_default_ram(memory_type) and is_copy:
397400
gen_string += MPU_RO_REGION_START.format(memory_type.lower(), memory_type.upper())
@@ -404,10 +407,10 @@ def generate_linker_script(linker_file, sram_data_linker_file, sram_bss_linker_f
404407
gen_string += MPU_RO_REGION_END.format(memory_type.lower())
405408

406409
if region_is_default_ram(memory_type):
407-
gen_string_sram_data += string_create_helper(SectionKind.DATA, memory_type, full_list_of_sections, 1, 1, phdrs)
410+
gen_string_sram_data += string_create_helper(SectionKind.DATA, memory_type, full_list_of_sections, 1, is_copy_data, phdrs)
408411
gen_string_sram_bss += string_create_helper(SectionKind.BSS, memory_type, full_list_of_sections, 0, 1, phdrs)
409412
else:
410-
gen_string += string_create_helper(SectionKind.DATA, memory_type, full_list_of_sections, 1, 1, phdrs)
413+
gen_string += string_create_helper(SectionKind.DATA, memory_type, full_list_of_sections, 1, is_copy_data, phdrs)
411414
gen_string += string_create_helper(SectionKind.BSS, memory_type, full_list_of_sections, 0, 1, phdrs)
412415

413416
# finally writing to the linker file
@@ -623,10 +626,9 @@ def main():
623626

624627
code_generation = {"copy_code": '', "zero_code": '', "extern": ''}
625628
for mem_type, list_of_sections in sorted(complete_list_of_sections.items()):
626-
627-
if "|COPY" in mem_type:
628-
mem_type = mem_type.split("|", 1)[0]
629-
code_generation = generate_memcpy_code(mem_type,
629+
mem_type = mem_type.split("|")
630+
if "COPY" in mem_type:
631+
code_generation = generate_memcpy_code(mem_type[0],
630632
list_of_sections, code_generation)
631633

632634
dump_header_file(args.output_code, code_generation)

0 commit comments

Comments
 (0)