From 51c3f971a0a00f6ca458eb5a5306a0f703c6a58d Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Wed, 25 Mar 2026 09:03:35 -0600 Subject: [PATCH] [lld][Hexagon] Test undefined weak branches (#186613) Undefined weak branches do not needsThunk(). Add a test case to cover undef weak. --- lld/ELF/Arch/Hexagon.cpp | 5 ++++- lld/test/ELF/hexagon-undefined-weak.s | 28 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 lld/test/ELF/hexagon-undefined-weak.s diff --git a/lld/ELF/Arch/Hexagon.cpp b/lld/ELF/Arch/Hexagon.cpp index 4204e2f4e524..435ee8a4d4f8 100644 --- a/lld/ELF/Arch/Hexagon.cpp +++ b/lld/ELF/Arch/Hexagon.cpp @@ -370,7 +370,10 @@ bool Hexagon::inBranchRange(RelType type, uint64_t src, uint64_t dst) const { bool Hexagon::needsThunk(RelExpr expr, RelType type, const InputFile *file, uint64_t branchAddr, const Symbol &s, int64_t a) const { - // Only check branch range for supported branch relocation types + // Undefined weak symbols without PLT entries resolve to address zero. + // Thunks are not needed since the branch target is fixed. + if (s.isUndefined() && !s.isInPlt(ctx)) + return false; switch (type) { case R_HEX_B22_PCREL: case R_HEX_PLT_B22_PCREL: diff --git a/lld/test/ELF/hexagon-undefined-weak.s b/lld/test/ELF/hexagon-undefined-weak.s new file mode 100644 index 000000000000..8f4eae926f22 --- /dev/null +++ b/lld/test/ELF/hexagon-undefined-weak.s @@ -0,0 +1,28 @@ +# REQUIRES: hexagon +# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %s -o %t.o +# RUN: ld.lld %t.o -o %t +# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s + +## Undefined weak branch targets resolve to address zero. Verify that +## no thunks are created and that branches encode without error. + +.weak undefined_weak +.globl _start +.type _start, @function +_start: + ## Simple call -- single-word packet. + call undefined_weak + + ## Call in a two-word packet with an ALU op. + { r0 = #0 + call undefined_weak } + + jumpr r31 + +## All branches target address zero. +# CHECK: <_start>: +# CHECK-NEXT: { call 0x0 } +# CHECK-NEXT: { call 0x0 +# CHECK-NEXT: r0 = #0x0 } +# CHECK-NEXT: { jumpr r31 } +# CHECK-NOT: __hexagon_thunk