casaroli opened a new pull request, #22329: URL: https://github.com/apache/nuttx/pull/22329
## Summary Since #22183 (5acb1a3dd82), `getopt_long_option()` is weak in `libs/libc/unistd/lib_getopt_common.c`. A program that uses `getopt()` and not `getopt_long()` keeps an undefined weak reference to it. Kernel builds link the programs as relocatable ELF (`ld -r`), so this reference reaches the ELF loader. The loader gives an undefined weak symbol the value 0, as a static link does. But the branch from `getopt_common()` to it cannot reach address 0 from a program at a high address: `up_relocateadd()`/`up_relocate()` fails with an out of range relocation and the program does not load. As a result, `qemu-armv8a:knsh` on master does not start init: `Assertion failed : at file: init/nx_bringup.c:398`. With the binfmt debug output, the console shows `up_relocateadd: ERROR: overflow in relocation type 283` (R_AARCH64_CALL26). This PR writes a NOP in place of a branch to an undefined weak symbol with the value 0, as the GNU linker does in a static link: - arm64: `R_AARCH64_CALL26` and `R_AARCH64_JUMP26`. - armv7-a: `R_ARM_CALL`, `R_ARM_JUMP24`, `R_ARM_THM_CALL` and `R_ARM_THM_JUMP24` (a `nop.w` for Thumb). The first commit is the nxstyle fix of the relocation switch in `up_relocate()` (indent and comments only, as 06d6e895da7 did for armv7-m and armv8-m): nxstyle reports 119 errors in this file on master, and the check job runs it on the whole file. - risc-v: `R_RISCV_CALL` and `R_RISCV_CALL_PLT` (the AUIPC and JALR pair become two NOPs). The fix is in the arch code because only the arch knows which relocations are branches and how a NOP is encoded. The common code (`libs/libc/elf/elf_symbols.c`) already gives the symbol the value 0. The check also tests `st_value == 0`: a weak symbol that an exporter defines keeps `SHN_UNDEF` and gets a value that is not 0. Other arches: x86_64 builds programs with `-mcmodel=large`, so the call uses an absolute `R_X86_64_64` relocation, which can hold 0. xtensa builds with `-mlongcalls`. The arm cores without MMU have no kernel build, and their ELF programs use `-mlong-calls`. ## Impact Only the ELF loader of arm64, armv7-a and risc-v. A program that does not have a branch to an undefined weak symbol loads as before. No new Kconfig option, no API change. The configs that load relocatable ELF programs in a kernel build are affected, for example `qemu-armv8a:knsh`, `imx93-evk:knsh`, `sabre-6quad:knsh`, `sama5d4-ek:knsh` and `icicle:knsh`. `qemu-armv7a:knsh` and `rv-virt:knsh64` use `CONFIG_BINFMT_ELF_EXECUTABLE`: the linker already makes the branch a NOP, and they boot on master. ## Testing QEMU on macOS. Each run boots to the NSH prompt, runs `/system/bin/hello` and `ostest`. Master is fc1f1d169d9, apps master 916424f03. | Config | Programs | master | this PR | |---|---|---|---| | qemu-armv8a:knsh (HVF, 1 CPU) | relocatable, `R_AARCH64_CALL26` | init does not start (nx_bringup.c:398) | ostest exits with status 0 | | qemu-armv8a:knsh + `CONFIG_SMP`, 4 CPUs (HVF) | relocatable | not run | ostest exits with status 0 | | qemu-armv7a:knsh + `CONFIG_BINFMT_ELF_RELOCATABLE` (Thumb) | relocatable, `R_ARM_THM_CALL` | init does not start (nx_bringup.c:398) | ostest exits with status 0 | | qemu-armv7a:knsh | executable | ostest exits with status 0 | ostest exits with status 0 | | rv-virt:knsh64 + `CONFIG_BINFMT_ELF_RELOCATABLE` | relocatable, `R_RISCV_CALL_PLT` | init does not start (nx_bringup.c:398) | ostest exits with status 0 | | rv-virt:knsh64 | executable | ostest exits with status 0 | ostest exits with status 0 | Build only, with `EXTRAFLAGS="-Wno-cpp -Werror"`: qemu-armv8a:nsh, qemu-armv7a:nsh, rv-virt:nsh64 (flat), sabre-6quad:knsh, icicle:knsh (kernel); the kernel builds above also use `-Werror`. The ARM-state path of armv7-a is checked only by reading: with `CONFIG_ARM_THUMB` disabled, master and this PR both stop earlier, at `up_relocate: ERROR: Unsupported relocation: 30` (`R_ARM_THM_JUMP24` from the Thumb libgcc, which the loader handles only with `CONFIG_ARM_THUMB`). `tools/checkpatch.sh -c -u -m -g` passes. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
