nuttx icon indicating copy to clipboard operation
nuttx copied to clipboard

CMake:switch CMake Linker from gcc/g++ to ld

Open xuxin930 opened this issue 1 year ago • 5 comments

Summary

the current CMake link stage uses gcc/g++ as the linker, which may cause unexpected issues such as link options do not taking effect, so it is better to be switched to ld. the behavior of link will be consistent with Makefile

aarch64-none-elf-g++ -Wl,--entry=__start -Wl,-nostdlib -Wl,--gc-sections -Wl,--cref -Wl,-Map=nuttx.map CMakeFiles/nuttx.dir/empty.cxx.obj -o nuttx -Wl,--script=/home/data/vela/vela-all/nuttx/build/dramboot.ld.tmp -Wl,--start-group arch/libarch.a binfmt/libbinfmt.a drivers/libdrivers.a fs/libfs.a libs/libc/libc.a mm/libmm.a sched/libsched.a boards/libboard.a libs/libxx/liblibcxxmini.a apps/libapps.a apps/builtin/libapps_builtin.a apps/system/nsh/libapps_nsh.a apps/system/nsh/libapps_sh.a  -Wl,--end-group
aarch64-none-elf-ld --entry=__start  -nostdlib --gc-sections --cref -Map=nuttx.map --nostartfiles  CMakeFiles/nuttx.dir/empty.cxx.obj -o nuttx --script=/home/data/vela/vela-all/nuttx/build/dramboot.ld.tmp --start-group arch/libarch.a binfmt/libbinfmt.a drivers/libdrivers.a fs/libfs.a libs/libc/libc.a mm/libmm.a sched/libsched.a boards/libboard.a libs/libxx/liblibcxxmini.a apps/libapps.a apps/builtin/libapps_builtin.a apps/system/nsh/libapps_nsh.a apps/system/nsh/libapps_sh.a --end-group 

Impact

Testing

CI build

xuxin930 avatar Dec 19 '23 09:12 xuxin930

hi @anchao @raiden00pl @no1wudi whats your opinion

xuxin930 avatar Dec 19 '23 11:12 xuxin930

Using ld may cause LTO optimization break, Link-Time-Optimization will only be executed in the link stage, and requires the support of GCC lto-wrapper. You can try replace gcc to ld and enable CONFIG_LTO_FULL on the Makefile base.

anchao avatar Dec 19 '23 12:12 anchao

Usage of LTO requires linking to be done by GCC. I think that was the main reason why we switched to this linking method from in Makefiles (from LD previously)

pkarashchenko avatar Dec 19 '23 21:12 pkarashchenko

Using ld may cause LTO optimization break, Link-Time-Optimization will only be executed in the link stage, and requires the support of GCC lto-wrapper. You can try replace gcc to ld and enable CONFIG_LTO_FULL on the Makefile base.

thank you all @anchao @pkarashchenko. I plan to try to swtiching the linker to gcc/g++ when enable LTO and handle the link options specially to see if it can work correctly. do I understand what you guys means correctly? or is it better to maintain the status quo in CMake

xuxin930 avatar Dec 20 '23 12:12 xuxin930

Using ld may cause LTO optimization break, Link-Time-Optimization will only be executed in the link stage, and requires the support of GCC lto-wrapper. You can try replace gcc to ld and enable CONFIG_LTO_FULL on the Makefile base.

thank you all @anchao @pkarashchenko. I plan to try to swtiching the linker to gcc/g++ when enable LTO and handle the link options specially to see if it can work correctly. do I understand what you guys means correctly? or is it better to maintain the status quo in CMake

Yep, I think you need to make a compatibility like the makefile base. If CONFIG_LTO_FULL is enabled, then switch to GCC to complete the link, otherwise use ld: https://github.com/apache/nuttx/blob/master/arch/arm/src/common/Toolchain.defs#L259-L266 https://github.com/apache/nuttx/blob/master/arch/arm/src/Makefile#L106-L133

anchao avatar Dec 22 '23 06:12 anchao