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 918a99ede6da51a5c863e1479d71435651e0e706
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 25 15:49:04 2026 +0100

    libs/libc/machine/risc-v: Make a call to an undefined weak symbol two NOPs.
    
    The ELF loader gives an undefined weak symbol the value 0, as in a
    static link.  The AUIPC and JALR pair of a program at a high address
    cannot reach address 0, so up_relocateadd() failed with an invalid
    R_RISCV_CALL or R_RISCV_CALL_PLT 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 two NOPs in place of the AUIPC and JALR pair.  A program does
    not call a weak function that is not defined.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/machine/risc-v/arch_elf.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/libs/libc/machine/risc-v/arch_elf.c 
b/libs/libc/machine/risc-v/arch_elf.c
index 5b02617dfd6..22d75036ee8 100644
--- a/libs/libc/machine/risc-v/arch_elf.c
+++ b/libs/libc/machine/risc-v/arch_elf.c
@@ -44,6 +44,8 @@
 
 #define RVI_OPCODE_MASK 0x7F
 
+#define RVI_INSN_NOP    0x00000013  /* addi x0, x0, 0 */
+
 /* ELF32 and ELF64 definitions */
 
 #ifdef CONFIG_ARCH_64BIT
@@ -531,6 +533,19 @@ int up_relocateadd(const Elf_Rela *rel, const Elf_Sym *sym,
                 addr, _get_val((uint16_t *)addr),
                 sym, (uintptr_t)sym->st_value);
 
+          /* An undefined weak symbol has the value 0, which is out of the
+           * AUIPC range of a program at a high address.  The program does
+           * not call it, so make the AUIPC and JALR pair two NOPs.
+           */
+
+          if (sym->st_shndx == SHN_UNDEF && sym->st_value == 0 &&
+              ELF_ST_BIND(sym->st_info) == STB_WEAK)
+            {
+              _set_val((uint16_t *)addr, RVI_INSN_NOP);
+              _set_val((uint16_t *)(addr + 4), RVI_INSN_NOP);
+              break;
+            }
+
           offset = (long)sym->st_value + (long)rel->r_addend - (long)addr;
 
           _calc_imm(offset, &imm_hi, &imm_lo);

Reply via email to