Skip to content

Commit 0ab6c1f

Browse files
committed
misc: Implement two-pass linking to separate rodata sections.
This patch changes the build to perform two-pass linking to separate rodata with relocations (copied to RAM) from rodata with no relocations (pure data) that can be kept in flash by llext. For dynamic linking mode: - First pass: Link with build-dynamic.ld to create temp ELF (unchanged). - Analyze the elf and extract, sort and write out the sections: - .rodata: sections WITH relocations → copied to RAM (LLEXT_MEM_RODATA) - .rodata_noreloc: sections WITHOUT relocations → kept in flash (LLEXT_MEM_RODATA_NO_RELOC) - Second pass: Link with generated rodata_split.ld + build-dynamic.ld. For static linking mode: - Generates empty rodata_split.ld (no section separation needed) Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 5f0e907 commit 0ab6c1f

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

platform.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ compiler.warning_flags.all=-Wall -Wextra
1717

1818
compiler.path={build.compiler_path}
1919
compiler.c.cmd={build.crossprefix}gcc
20-
compiler.c.flags=-g -c {compiler.warning_flags} {compiler.zephyr.macros} "@{compiler.zephyr.cflags_file}" -MMD -mcpu={build.mcu} {build.float-abi} {build.fpu}
20+
compiler.c.flags=-g -c {compiler.warning_flags} {compiler.zephyr.macros} "@{compiler.zephyr.cflags_file}" -fdata-sections -ffunction-sections -MMD -mcpu={build.mcu} {build.float-abi} {build.fpu}
2121
compiler.c.elf.cmd={build.crossprefix}g++
2222
compiler.c.elf.flags=-Wl,--gc-sections -mcpu={build.mcu} {build.float-abi} {build.fpu} -std=gnu++17
2323
compiler.S.cmd={build.crossprefix}g++
@@ -58,7 +58,8 @@ build.link_command="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" "-L{bu
5858
build.check_command-dynamic={build.link_command} {build.link_args.check-dynamic} -o "{build.path}/{build.project_name}_check.tmp"
5959
build.check_command-static=/bin/true
6060
build.check_command-static.windows=cmd /C cd .
61-
build.combine_command={build.link_command} {build.link_args.build-{build.link_mode}} {build.link_args.build-common}
61+
build.combine_command={build.link_command} {build.link_args.build-{build.link_mode}} "-Wl,-Map,{build.path}/{build.project_name}_temp.map" -o "{build.path}/{build.project_name}_temp.elf"
62+
build.combine_command_final={build.link_command} "-T{build.path}/rodata_split.ld" {build.link_args.build-{build.link_mode}} {build.link_args.build-common}
6263

6364
# link_args.* are included by any link_command depending on the link_mode
6465
build.link_args.dynamic=-e main
@@ -116,9 +117,11 @@ recipe.S.o.pattern="{compiler.path}{compiler.S.cmd}" {compiler.S.flags} -DARDUIN
116117
## Create archives
117118
recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}"
118119

119-
## Combine gc-sections, archives, and objects
120+
## Combine gc-sections, archives, and objects - Two-pass linking
120121
recipe.c.combine.1.pattern={build.check_command-{build.link_mode}}
121122
recipe.c.combine.2.pattern={build.combine_command}
123+
recipe.c.combine.3.pattern="{runtime.platform.path}/extra/gen_rodata_ld" "{build.path}/{build.project_name}_temp.elf" "{build.path}/rodata_split.ld" "{build.link_mode}"
124+
recipe.c.combine.4.pattern={build.combine_command_final}
122125
recipe.c.combine.pattern={build.combine_command}
123126
recipe.hooks.linking.postlink.1.pattern="{compiler.path}{build.crossprefix}strip" --strip-debug "{build.path}/{build.project_name}_debug.elf" -o "{build.path}/{build.project_name}.elf"
124127

variants/_ldscripts/build-dynamic.ld

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ SECTIONS {
4242
__static_thread_data_list_end = .;
4343
}
4444

45-
.rodata : {
46-
*(.rodata)
47-
*(.rodata1)
48-
*(.rodata.*)
49-
}
50-
5145
.data : {
5246
*(.data .data.*)
5347
}
@@ -82,10 +76,6 @@ SECTIONS {
8276
KEEP(*(.shstrtab))
8377
}
8478

85-
.rel : {
86-
KEEP(*(.rel .rel.*))
87-
}
88-
8979
.got : {
9080
KEEP(*(.got .got.* .got.plt .got.plt*))
9181
}

0 commit comments

Comments
 (0)