Bug 29823 - ld riscv: undefined elf_backend_obj_attrs_handle_unknown causes segfault when merging .riscv.attributes
Summary: ld riscv: undefined elf_backend_obj_attrs_handle_unknown causes segfault when...
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.40
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-11-23 17:42 UTC by Fangrui Song
Modified: 2024-04-30 06:46 UTC (History)
4 users (show)

See Also:
Host:
Target: riscv64-*-*
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2022-11-23 17:42:13 UTC
% cat a.s
.attribute 9, "0"
% riscv64-linux-gnu-gcc -c a.s
% ~/Dev/binutils-gdb/out/riscv64/ld/ld-new a.o a.o
[1]    3982286 segmentation fault  ~/Dev/binutils-gdb/out/riscv64/ld/ld-new a.o a.o

lang_check=>_bfd_riscv_elf_merge_private_bfd_data=>riscv_merge_attributes => `result &= _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);` => `get_elf_backend_data (err_bfd)->obj_attrs_handle_unknown (err_bfd, tag);` => null pointer dereference
Comment 1 Paul Kirth 2024-04-26 21:14:23 UTC
This issue is now preventing us from landing the psABI mandated attribute merging in LLVM for Atomics and the X3 register.

https://github.com/llvm/llvm-project/pull/90266

It was first reported to us here: https://github.com/llvm/llvm-project/pull/84597#issuecomment-2079128332, after we had landed those changes. 

Is there someone on the binutils side we can work with to address this? Android and other platforms will require support for this in their toolchains to prevent mixing objects w/ incompatible Atomics ABIs or incompatible stack ABIs(e.g., Android ships with shadow call stack enabled, which uses the cp/X3 register).
Comment 2 Sourceware Commits 2024-04-30 05:49:35 UTC
The master branch has been updated by Nelson Chu <nelsonc1225@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7bd696ef499176253bfca0c1d16c4b8164ae836d

commit 7bd696ef499176253bfca0c1d16c4b8164ae836d
Author: Nelson Chu <nelson@rivosinc.com>
Date:   Fri Jun 9 08:47:17 2023 +0800

    RISC-V: PR29823, defined the missing elf_backend_obj_attrs_handle_unknown.
    
    bfd/
            PR 29823
            * elfnn-riscv.c (riscv_elf_obj_attrs_handle_unknown): New function.
            (elf_backend_obj_attrs_handle_unknown): Defined to
            riscv_elf_obj_attrs_handle_unknown.
Comment 3 Nelson Chu 2024-04-30 06:46:25 UTC
There may be a TODO that handle vendor elf attributes,

According to the psabi, the vendor attribute number is from 32768, so need to use elf_other_obj_attributes_proc rather than elf_known_obj_attributes_proc to access, and may need to handle the merge rules in the riscv_elf_obj_attrs_handle_unknown.  I had tried the similar support in the pr31179, even that we choose another way to fix problem there, maybe the proposed patch will help to support vendor attributes.