casaroli opened a new pull request, #21394: URL: https://github.com/apache/nuttx/pull/21394
## Summary `libs/libc/elf/gnu-elf.ld.in` aligns `.init_array` and `.fini_array` only at their end. Their start is where the section before them ended. On RISC-V the small data sections (`.sdata.*`) are not in `.data`, so the linker places them after `.data` as orphans, and `.init_array` can start at an odd address. A table without constructors is then not empty, and `crt0.c` (`CONFIG_HAVE_CXXINITIALIZE`) calls bytes of other data as a constructor. Example, rv-virt:knsh64 with `CONFIG_HAVE_CXX` and `CONFIG_HAVE_CXXINITIALIZE` on master: the small data of `/system/bin/init` ends with the 1-byte `.sdata.g_syslog_mask` at 0xc0101280, so `.init_array` starts at 0xc0101281 (`_sctors`) and ends at 0xc0101284 (`_ectors`, after `ALIGN`). `crt0.c` calls each pointer from `_sctors` until it is equal to `_ectors`, so init jumps to address 0 before `main()` and the system does not start: ``` riscv_exception: EXCEPTION: Instruction page fault. MCAUSE: 000000000000000c, EPC: 0000000000000000, MTVAL: 0000000000000000 riscv_fault_handler: Segmentation fault in /system/bin/init (PID 3: /system/bin/init) ``` This PR aligns `.init_array` and `.fini_array` at their start too, and puts the small data sections of RISC-V (`.sdata`, `.srodata`) in `.data` and `.rodata`, where the other data of the same kind is. ## Impact Programs and modules that are linked with `libs/libc/elf/gnu-elf.ld` (all architectures; boards with their own `scripts/gnu-elf.ld` do not use it). A table without entries now has the same start and end, and a table always starts aligned. On RISC-V, `.sdata`/`.srodata` are now part of `.data`/`.rodata` instead of separate orphan sections; no code uses them by name (the script defines no `__global_pointer$`, so there is no gp-relative access). No change for programs whose tables were already aligned. The board scripts `boards/*/scripts/gnu-elf.ld` have no `ALIGN` in their tables, so an empty table has the same start and end there: they do not need the change. No documentation describes this script's sections. ## Testing Test programs: `ctoralign.c` (no constructor, small data of odd sizes: the table addresses are aligned and both tables are empty) and `ctorrun.c` (one constructor and one destructor: each runs once). | Configuration | master 3ec6adc63f5 | this PR | |---|---|---| | rv-virt:knsh64 + `HAVE_CXX` + `HAVE_CXXINITIALIZE` (QEMU TCG, apps 48598ce51) | init: `.init_array` at 0xc0101281 (3 bytes), SIGSEGV at EPC 0, no NSH | init and sh: `.init_array` at 0xc0101284 (0 bytes); ctoralign PASS, ctorrun PASS, ostest status 0 | | qemu-armv8a:knsh + `HAVE_CXXINITIALIZE` (HVF, apps b017038e5; uses its board script, so the same with and without this PR) | ctoralign PASS, ctorrun PASS, ostest status 0 | ctoralign PASS, ctorrun PASS, ostest status 0 | | qemu-armv8a:nsh (flat, HVF) | ostest status 0 | ostest status 0 | Builds with `EXTRAFLAGS="-Wno-cpp -Werror"`, no new warnings: qemu-armv8a:knsh (`HAVE_CXXINITIALIZE`), qemu-armv8a:nsh, rv-virt:knsh (32 bits, `HAVE_CXX`, `HAVE_CXXINITIALIZE`), rv-virt:nsh64, qemu-armv7a:knsh, mps2-an500:knsh (protected), qemu-intel64:knsh_romfs (`HAVE_CXXINITIALIZE`; `-Wno-error=shadow` for a libm warning of this host compiler), esp32s3-devkit:kernel_oct, and pimoroni-pico-2-plus:xipfs-fdpic (`CONFIG_FDPIC`: its apps link FDPIC modules with this script). The tests ran on master 3ec6adc63f5; the branch is rebased on 0e22bd5bd98 (the new commits touch no file of this PR). `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]
