Skip to content

Commit f20afb5

Browse files
tejlmandd3zd3z
authored andcommitted
cmake: linking app with runtime library together with rust app library
The Zephyr PR zephyrproject-rtos/zephyr#78320 clean up and improves link handling in Zephyr. One improvement is the deterministic and correct link location of runtime and C library linking at the end of link arguments to ensure all libraries are able to use standard functions. However the librustapp.a library created by the cargo tool also contains runtime functions. It would in most cases be considered correct that a library, such as librustapp.a, which provides functions are linked first so that those functions provided as part of rust toolchain takes precedence over the runtime library provided by the C / C++ toolchain. However on the riscv32 / riscv64 architecture the rust prebuilt libraries makes use of relocation types in the clzsi2 object which are unknown to the ld linker in use. Thus linking librustapp.a first, for example like `librustapp.a -lgcc` results in the following warning (and thus CI failures): > <path>/ld.bfd: <path>/librustapp.a(45c91108d938afe8-clzdi2.o): > unsupported relocation type 0x3d Therefore we ensure that the runtime library is linked first, for example `-lgcc librustapp.a`. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent fc16717 commit f20afb5

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,15 @@ ${config_paths}
179179
kobj_types_h_target
180180
)
181181

182-
target_link_libraries(app PUBLIC -Wl,--allow-multiple-definition ${RUST_LIBRARY})
182+
# Linking with the <rt_library> (`$<TARGET_PROPERTY:linker,rt_library>`).
183+
# -lgcc / -lcompiler_rt depending on toolchain, linker, and runtime library configuration.
184+
# In general this shouldn't be needed, as the runtime libary is generally linked late, but
185+
# librustapp.a includes it's own runtime functions, and on riscv (and potentially others) an
186+
# unrecognized / unknown type is used in the relocation section for clzsi2 object.
187+
# Thus we must for current time ensure that the runtime library is before librustapp.a.
188+
# Example of warning reported by ld when this fix is not in place:
189+
# <path>/ld.bfd: rust/target/riscv64imac-unknown-none-elf/debug/librustapp.a(45c91108d938afe8-clzdi2.o): unsupported relocation type 0x3d
190+
target_link_libraries(app PUBLIC $<TARGET_PROPERTY:linker,rt_library> -Wl,--allow-multiple-definition ${RUST_LIBRARY})
183191
add_dependencies(app librustapp)
184192

185193
# Presumably, Rust applications will have no C source files, but cmake will require them.

0 commit comments

Comments
 (0)