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 49a9218ac19f67eeac89d513feed052dccc71699
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 24 18:30:00 2026 +0100

    libs/libc/machine/arm64: 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_relocateadd() failed with an overflow in
    the R_AARCH64_CALL26 or R_AARCH64_JUMP26 relocation, and the program did
    not load.  Since 5acb1a3dd82 getopt_common() calls the weak
    getopt_long_option(), so every program that uses getopt() and not
    getopt_long() has such a call: qemu-armv8a:knsh does not start init.
    
    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/arm64/arch_elf.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/libs/libc/machine/arm64/arch_elf.c 
b/libs/libc/machine/arm64/arch_elf.c
index 99a0758f3b7..cd85b5b4169 100644
--- a/libs/libc/machine/arm64/arch_elf.c
+++ b/libs/libc/machine/arm64/arch_elf.c
@@ -57,6 +57,10 @@
 
 #define AARCH64_BREAK_FAULT     (AARCH64_BREAK_MON | (FAULT_BRK_IMM << 5))
 
+/* NOP instruction encoding */
+
+#define AARCH64_INSN_NOP        0xd503201f
+
 #define ADR_IMM_HILOSPLIT       2
 #define ADR_IMM_SIZE            (2 * 1024 * 1024)
 #define ADR_IMM_LOMASK          ((1 << ADR_IMM_HILOSPLIT) - 1)
@@ -799,6 +803,18 @@ int up_relocateadd(const Elf64_Rela *rel, const Elf64_Sym 
*sym,
       case R_AARCH64_JUMP26:
       case R_AARCH64_CALL26:
         {
+          /* 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.
+           */
+
+          if (sym->st_shndx == SHN_UNDEF && sym->st_value == 0 &&
+              ELF64_ST_BIND(sym->st_info) == STB_WEAK)
+            {
+              *(uint32_t *)addr = htole32(AARCH64_INSN_NOP);
+              break;
+            }
+
           ret = reloc_insn_imm(RELOC_OP_PREL, addr, val, 2, 26,
                                INSN_IMM_26);
         }

Reply via email to