kernel/code-relocation.html · kernel/code-relocation.rst 공식 원문 ↗

Zephyr 3.7.0 · Kernel

코드와 데이터 Relocation

소스 수정 없이 선택한 file의 text·rodata·data·BSS를 원하는 memory region으로 옮깁니다.

Official pathkernel/code-relocation.html
Source filekernel/code-relocation.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

CMake가 relocation script와 linker fragment·초기화 코드를 생성하며, section suffix와 NOKEEP·NOCOPY flag로 배치와 복사 정책을 제어합니다.

Sections

.text · .rodata · .data · .bss

Generated

linker_relocate.ld · code_relocation.c

Control

NOKEEP · NOCOPY

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 84F41673A13027AD2792E12A67C679603BD32ACBD43AA4B4AC84D6DB19F1A450
.. _code_data_relocation:

Code And Data Relocation
########################

Overview
********
This feature will allow relocating .text, .rodata, .data, and .bss sections from
required files and place them in the required memory region. The memory region
and file are given to the :ref:`gen_relocate_app.py` script in the form
of a string. This script is always invoked from inside cmake.

This script provides a robust way to re-order the memory contents without
actually having to modify the code.  In simple terms this script will do the job
of ``__attribute__((section("name")))`` for a bunch of files together.

Details
*******
The memory region and file are given to the :ref:`gen_relocate_app.py` script in the form of a string.

An example of such a string is:
``SRAM2:/home/xyz/zephyr/samples/hello_world/src/main.c,SRAM1:/home/xyz/zephyr/samples/hello_world/src/main2.c``

This script is invoked with the following parameters:
``python3 gen_relocate_app.py -i input_string -o generated_linker -c generated_code``

Kconfig :kconfig:option:`CONFIG_CODE_DATA_RELOCATION` option,  when enabled in
``prj.conf``, will invoke the script and do the required relocation.

This script also trigger the generation of ``linker_relocate.ld`` and
``code_relocation.c`` files.  The ``linker_relocate.ld`` file creates
appropriate sections and links the required functions or variables from all the
selected files.

.. note::

   The text section is split into 2 parts in the main linker script. The first
   section will have some info regarding vector tables and other debug related
   info.  The second section will have the complete text section.  This is
   needed to force the required functions and data variables to the correct
   locations.  This is due to the behavior of the linker. The linker will only
   link once and hence this text section had to be split to make room for the
   generated linker script.

The ``code_relocation.c`` file has code that is needed for
initializing data sections, and a copy of the text sections (if XIP).
Also this contains code needed for bss zeroing and
for  data copy operations from ROM to required memory type.

**The procedure to invoke this feature is:**

* Enable :kconfig:option:`CONFIG_CODE_DATA_RELOCATION` in the ``prj.conf`` file

* Inside the ``CMakeLists.txt`` file in the project, mention
  all the files that need relocation.

  ``zephyr_code_relocate(FILES src/*.c LOCATION SRAM2)``

  Where the first argument is the file/files and the second
  argument is the memory where it must be placed.

  .. note::

     function zephyr_code_relocate() can be called  as many times as required.

Additional Configurations
=========================
This section shows additional configuration options that can be set in
``CMakeLists.txt``

* if the memory is SRAM1, SRAM2, CCD, or AON, then place the full object in the
  sections for example:

  .. code-block:: none

     zephyr_code_relocate(FILES src/file1.c LOCATION SRAM2)
     zephyr_code_relocate(FILES src/file2.c LOCATION SRAM)

* if the memory type is appended with _DATA, _TEXT, _RODATA or _BSS, only the
  selected memory is placed in the required memory region.
  for example:

  .. code-block:: none

     zephyr_code_relocate(FILES src/file1.c LOCATION SRAM2_DATA)
     zephyr_code_relocate(FILES src/file2.c LOCATION SRAM2_TEXT)

* Multiple regions can also be appended together such as: SRAM2_DATA_BSS.
  This will place data and bss inside SRAM2.

* Multiple files can be passed to the FILES argument, or CMake generator
  expressions can be used to relocate a comma-separated list of files

  .. code-block:: none

     file(GLOB sources "file*.c")
     zephyr_code_relocate(FILES ${sources} LOCATION SRAM)
     zephyr_code_relocate(FILES $<TARGET_PROPERTY:my_tgt,SOURCES> LOCATION SRAM)

NOKEEP flag
===========

By default, all relocated functions and variables will be marked with ``KEEP()``
when generating ``linker_relocate.ld``.  Therefore, if any input file happens to
contain unused symbols, then they will not be discarded by the linker, even when
it is invoked with ``--gc-sections``. If you'd like to override this behavior,
you can pass ``NOKEEP`` to your ``zephyr_code_relocate()`` call.

  .. code-block:: none

     zephyr_code_relocate(FILES src/file1.c LOCATION SRAM2_TEXT NOKEEP)

The example above will help ensure that any unused code found in the .text
sections of ``file1.c`` will not stick to SRAM2.

NOCOPY flag
===========

When a ``NOCOPY`` option is passed to the ``zephyr_code_relocate()`` function,
the relocation code is not generated in ``code_relocation.c``. This flag can be
used when we want to move the content of a specific file (or set of files) to a
XIP area.

This example will place the .text section of the ``xip_external_flash.c`` file
to the ``EXTFLASH`` memory region where it will be executed from (XIP). The
.data will be relocated as usual into SRAM.

  .. code-block:: none

     zephyr_code_relocate(FILES src/xip_external_flash.c LOCATION EXTFLASH_TEXT NOCOPY)
     zephyr_code_relocate(FILES src/xip_external_flash.c LOCATION SRAM_DATA)

Relocating libraries
====================

Libraries can be relocated using the LIBRARY argument to
``zephyr_code_relocation()`` with the library name. For example, the following
snippet will relocate serial drivers to SRAM2:

  .. code-block:: none

    zephyr_code_relocate(LIBRARY drivers__serial LOCATION SRAM2)

Tips
====

Take care if relocating kernel/arch files, some contain early initialization
code that executes before code relocation takes place.

Additional MPU/MMU configuration may be required to ensure that the
destination memory region is configured to allow code execution.

Samples/ Tests
==============

A test showcasing this feature is provided at
``$ZEPHYR_BASE/tests/application_development/code_relocation``

This test shows how the code relocation feature is used.

This test will place .text, .data, .bss from 3 files to various parts in the SRAM
using a custom linker file derived from ``include/zephyr/arch/arm/cortex_m/scripts/linker.ld``

A sample showcasing the NOCOPY flag is provided at
``$ZEPHYR_BASE/samples/application_development/code_relocation_nocopy/``

Part 3

한국어 전문 번역

개요

이 기능은 필요한 file의 .text, .rodata, .data, .bss section을 지정한 memory region으로 옮깁니다. Memory region과 file은 문자열 형태로 gen_relocate_app.py script에 전달되며, 이 script는 항상 CMake 내부에서 호출됩니다.

코드를 직접 수정하지 않고 memory content 순서를 바꾸는 견고한 방법입니다. 간단히 말하면 여러 file에 대해 __attribute__((section("name")))의 역할을 한꺼번에 수행합니다.

세부 내용

Memory region과 file을 gen_relocate_app.py에 다음과 같은 문자열로 전달합니다.

SRAM2:/home/xyz/zephyr/samples/hello_world/src/main.c,SRAM1:/home/xyz/zephyr/samples/hello_world/src/main2.c

Script 호출 매개변수는 다음과 같습니다.

python3 gen_relocate_app.py -i input_string -o generated_linker -c generated_code

prj.conf에서 Kconfig option CONFIG_CODE_DATA_RELOCATION을 활성화하면 script가 호출되어 필요한 relocation을 수행합니다.

Script는 linker_relocate.ld와 code_relocation.c도 생성합니다. linker_relocate.ld는 적절한 section을 만들고 선택한 모든 file의 필요한 function이나 variable을 연결합니다.

code_relocation.c에는 data section 초기화, XIP인 경우 text section 복사, BSS zeroing, ROM에서 지정 memory type으로 data를 복사하는 데 필요한 코드가 들어 있습니다.

이 기능을 호출하는 절차는 다음과 같습니다.

  1. prj.conf에서 CONFIG_CODE_DATA_RELOCATION을 활성화합니다.
  2. Project CMakeLists.txt에 relocation할 모든 file을 지정합니다: zephyr_code_relocate(FILES src/*.c LOCATION SRAM2). 첫 인자는 file 목록, 두 번째 인자는 배치할 memory입니다.

추가 구성

CMakeLists.txt에서 설정할 수 있는 추가 option입니다.

SRAM1, SRAM2, CCD, AON 같은 memory 이름을 사용하면 file의 전체 object를 해당 memory section에 배치합니다.

zephyr_code_relocate(FILES src/file1.c LOCATION SRAM2)
zephyr_code_relocate(FILES src/file2.c LOCATION SRAM)

Memory type 뒤에 _DATA, _TEXT, _RODATA, _BSS를 붙이면 선택한 section만 필요한 memory region에 배치합니다.

zephyr_code_relocate(FILES src/file1.c LOCATION SRAM2_DATA)
zephyr_code_relocate(FILES src/file2.c LOCATION SRAM2_TEXT)

SRAM2_DATA_BSS처럼 region suffix를 여러 개 이어 붙일 수도 있습니다. 이 예는 data와 BSS를 SRAM2에 배치합니다.

FILES 인자에 file 여러 개를 전달하거나 CMake generator expression으로 쉼표 구분 file 목록을 relocation할 수 있습니다.

file(GLOB sources "file*.c")
zephyr_code_relocate(FILES ${sources} LOCATION SRAM)
zephyr_code_relocate(FILES $<TARGET_PROPERTY:my_tgt,SOURCES> LOCATION SRAM)

NOKEEP flag

기본적으로 relocation한 모든 function과 variable은 linker_relocate.ld를 생성할 때 KEEP()으로 표시됩니다. 따라서 input file에 사용하지 않는 symbol이 있어도 linker를 --gc-sections와 함께 호출했을 때 제거되지 않습니다. 이 동작을 바꾸려면 zephyr_code_relocate() 호출에 NOKEEP을 전달합니다.

zephyr_code_relocate(FILES src/file1.c LOCATION SRAM2_TEXT NOKEEP)

위 예에서는 file1.c의 .text section에 있는 사용하지 않는 code가 SRAM2에 남지 않도록 합니다.

NOCOPY flag

zephyr_code_relocate()에 NOCOPY option을 전달하면 code_relocation.c에 relocation code를 생성하지 않습니다. 특정 file 또는 file 집합의 content를 XIP 영역으로 옮길 때 사용할 수 있습니다.

다음 예는 xip_external_flash.c의 .text section을 실행 위치인 EXTFLASH memory region에 배치합니다. .data는 평소처럼 SRAM으로 relocation합니다.

zephyr_code_relocate(FILES src/xip_external_flash.c LOCATION EXTFLASH_TEXT NOCOPY)
zephyr_code_relocate(FILES src/xip_external_flash.c LOCATION SRAM_DATA)

Library relocation

원문에 표기된 zephyr_code_relocation()의 LIBRARY 인자에 library 이름을 전달해 library를 relocation할 수 있습니다. 다음 예는 serial driver를 SRAM2로 옮깁니다.

zephyr_code_relocate(LIBRARY drivers__serial LOCATION SRAM2)

팁

Kernel 또는 architecture file을 relocation할 때 주의해야 합니다. 일부 file에는 code relocation이 수행되기 전에 실행되는 early initialization code가 있습니다.

목적지 memory region에서 code 실행을 허용하려면 추가 MPU/MMU 구성이 필요할 수 있습니다.

샘플과 테스트

이 기능을 보여 주는 test는 $ZEPHYR_BASE/tests/application_development/code_relocation에 있습니다.

이 test는 include/zephyr/arch/arm/cortex_m/scripts/linker.ld에서 파생한 custom linker file을 사용하여 file 세 개의 .text, .data, .bss를 SRAM의 여러 위치에 배치합니다.

NOCOPY flag를 보여 주는 sample은 $ZEPHYR_BASE/samples/application_development/code_relocation_nocopy/에 있습니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.