Skip to content

Commit 56e5121

Browse files
jholfabiobaltieri
authored andcommitted
code_relocation: Handle relocation of noinit sections
The code relocation feature allows code and data section to be located inside a designated RAM region. Currently, this feature supports relocation of code (text) and data including mutable data (data), read-only data (rodata), and zero-initialized data (bss). However, relocation of non-initialized data sections was not previously supported, meaning that any data annotated with the __noinit attribute could not be relocated into the desired RAM region. This patch adds a NOINIT memory-type which can be used implicitly or explictly in the zephyr_code_relocate() CMake function. This causes the build system to generate additional linker-script section-matching rules. By the nature of noinit data, no action is required by Zephyr at boot. Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
1 parent 5dbd364 commit 56e5121

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

scripts/build/gen_relocate_app.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class SectionKind(Enum):
6262
RODATA = "rodata"
6363
DATA = "data"
6464
BSS = "bss"
65+
NOINIT = "noinit"
6566
LITERAL = "literal"
6667

6768
def __str__(self):
@@ -85,6 +86,8 @@ def for_section_named(cls, name: str):
8586
return cls.DATA
8687
elif ".bss." in name:
8788
return cls.BSS
89+
elif ".noinit." in name:
90+
return cls.NOINIT
8891
elif ".literal." in name:
8992
return cls.LITERAL
9093
else:
@@ -123,6 +126,8 @@ class OutputSection(NamedTuple):
123126

124127
LOAD_ADDRESS_LOCATION_BSS = "GROUP_LINK_IN({0})"
125128

129+
LOAD_ADDRESS_LOCATION_NOLOAD = "GROUP_NOLOAD_LINK_IN({0}, {0})"
130+
126131
MPU_RO_REGION_START = """
127132
128133
_{mem}_mpu_ro_region_start = ORIGIN({mem_upper});
@@ -140,7 +145,7 @@ class OutputSection(NamedTuple):
140145
141146
/* Linker section for memory region {mem_upper} for {kind_name} section */
142147
143-
SECTION_PROLOGUE(.{mem}_{kind}_reloc,,)
148+
SECTION_PROLOGUE(.{mem}_{kind}_reloc,{options},)
144149
{{
145150
. = ALIGN(4);
146151
{linker_sections}
@@ -155,7 +160,7 @@ class OutputSection(NamedTuple):
155160
156161
/* Linker section for memory region {mem_upper} for {kind_name} section */
157162
158-
SECTION_PROLOGUE(.{mem}_{kind}_reloc,,)
163+
SECTION_PROLOGUE(.{mem}_{kind}_reloc,{options},)
159164
{{
160165
__{mem}_{kind}_reloc_start = .;
161166
{linker_sections}
@@ -346,19 +351,28 @@ def string_create_helper(
346351
):
347352
linker_string = ''
348353

349-
if not load_address_in_flash:
350-
phdr_template = LOAD_ADDRESS_LOCATION_BSS
351-
elif is_copy:
352-
phdr_template = LOAD_ADDRESS_LOCATION_FLASH
354+
if load_address_in_flash:
355+
if is_copy:
356+
phdr_template = LOAD_ADDRESS_LOCATION_FLASH
357+
else:
358+
phdr_template = LOAD_ADDRESS_LOCATION_FLASH_NOCOPY
353359
else:
354-
phdr_template = LOAD_ADDRESS_LOCATION_FLASH_NOCOPY
360+
if kind is SectionKind.NOINIT:
361+
phdr_template = LOAD_ADDRESS_LOCATION_NOLOAD
362+
else:
363+
phdr_template = LOAD_ADDRESS_LOCATION_BSS
364+
355365
load_address_string = phdr_template.format(add_phdr(memory_type, phdrs))
356366

357367
if full_list_of_sections[kind]:
358368
# Create a complete list of funcs/ variables that goes in for this
359369
# memory type
360370
tmp = print_linker_sections(full_list_of_sections[kind])
361-
if region_is_default_ram(memory_type) and kind in (SectionKind.DATA, SectionKind.BSS):
371+
if region_is_default_ram(memory_type) and kind in (
372+
SectionKind.DATA,
373+
SectionKind.BSS,
374+
SectionKind.NOINIT,
375+
):
362376
linker_string += tmp
363377
else:
364378
fields = {
@@ -368,6 +382,7 @@ def string_create_helper(
368382
"kind_name": kind,
369383
"linker_sections": tmp,
370384
"load_address": load_address_string,
385+
"options": "(NOLOAD)" if kind is SectionKind.NOINIT else "",
371386
}
372387

373388
if not region_is_default_ram(memory_type) and kind is SectionKind.RODATA:
@@ -422,10 +437,31 @@ def generate_linker_script(
422437
if region_is_default_ram(memory_type) and is_copy:
423438
gen_string += MPU_RO_REGION_END.format(mem=memory_type.lower())
424439

425-
data_sections = string_create_helper(
426-
SectionKind.DATA, memory_type, full_list_of_sections, True, True, phdrs
427-
) + string_create_helper(
428-
SectionKind.BSS, memory_type, full_list_of_sections, False, True, phdrs
440+
data_sections = (
441+
string_create_helper(
442+
SectionKind.DATA,
443+
memory_type,
444+
full_list_of_sections,
445+
True,
446+
True,
447+
phdrs,
448+
)
449+
+ string_create_helper(
450+
SectionKind.BSS,
451+
memory_type,
452+
full_list_of_sections,
453+
False,
454+
True,
455+
phdrs,
456+
)
457+
+ string_create_helper(
458+
SectionKind.NOINIT,
459+
memory_type,
460+
full_list_of_sections,
461+
False,
462+
False,
463+
phdrs,
464+
)
429465
)
430466

431467
if region_is_default_ram(memory_type):

0 commit comments

Comments
 (0)