This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit b65361f40ab02644d8e932f4acaa40c080008259 Author: Marco Casaroli <[email protected]> AuthorDate: Wed Nov 25 15:52:57 2026 +0100 libs/libc/machine/arm/armv7-a: Make a branch to an undefined weak symbol a NOP. The ELF loader gives an undefined weak symbol the value 0, as in a static link. A B or BL instruction of a program at a high address cannot reach address 0, so up_relocate() failed with an out of range R_ARM_CALL, R_ARM_JUMP24, R_ARM_THM_CALL or R_ARM_THM_JUMP24 relocation, and the program did not load. Since 5acb1a3dd82 getopt_common() calls the weak getopt_long_option(), so in a kernel build with relocatable ELF programs every program that uses getopt() and not getopt_long() has such a call, and init does not start. Write a NOP in place of the branch, as the GNU linker does in a static link. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- libs/libc/machine/arm/armv7-a/arch_elf.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/libs/libc/machine/arm/armv7-a/arch_elf.c b/libs/libc/machine/arm/armv7-a/arch_elf.c index 3e7c731cdd1..f83f8162cb6 100644 --- a/libs/libc/machine/arm/armv7-a/arch_elf.c +++ b/libs/libc/machine/arm/armv7-a/arch_elf.c @@ -33,6 +33,25 @@ #include <nuttx/elf.h> +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* NOP instruction encodings: ARM and 32-bit Thumb (nop.w) */ + +#define ARM_INSN_NOP 0xe320f000 +#define THUMB2_INSN_NOP_UPPER 0xf3af +#define THUMB2_INSN_NOP_LOWER 0x8000 + +/* A branch to an undefined weak symbol becomes a NOP, as in a static link. + * The symbol has the value 0, which is out of the branch range of a + * program at a high address. + */ + +#define IS_UNDEF_WEAK(sym) \ + ((sym)->st_shndx == SHN_UNDEF && (sym)->st_value == 0 && \ + ELF32_ST_BIND((sym)->st_info) == STB_WEAK) + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -166,6 +185,12 @@ int up_relocate(const Elf32_Rel *rel, const Elf32_Sym *sym, uintptr_t addr, (long)(*(uint32_t *)addr), sym, (long)sym->st_value); + if (IS_UNDEF_WEAK(sym)) + { + *(uint32_t *)addr = ARM_INSN_NOP; + break; + } + offset = (*(uint32_t *)addr & 0x00ffffff) << 2; if (offset & 0x02000000) { @@ -384,6 +409,13 @@ int up_relocate(const Elf32_Rel *rel, const Elf32_Sym *sym, uintptr_t addr, (int)upper_insn, (int)lower_insn, sym, (long)sym->st_value); + if (IS_UNDEF_WEAK(sym)) + { + *(uint16_t *)addr = THUMB2_INSN_NOP_UPPER; + *(uint16_t *)(addr + 2) = THUMB2_INSN_NOP_LOWER; + break; + } + /* Extract the 25-bit offset from the 32-bit instruction: * * offset[24] = S
