Bug 28348 - ld generates corrupted binaries with -lc
Summary: ld generates corrupted binaries with -lc
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.37
: P2 normal
Target Milestone: 2.38
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-09-17 17:17 UTC by Matthias Klose
Modified: 2021-09-26 17:41 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
files for linking (1.44 MB, application/x-xz)
2021-09-17 17:17 UTC, Matthias Klose
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Matthias Klose 2021-09-17 17:17:45 UTC
Created attachment 13668 [details]
files for linking

[forwarded from https://bugs.debian.org/994530]

"""
When clang is used with both -fsanitize=address and -lc, ld silently generates
binaries contains corrupted symbols as reported by readelf:

$ cat test_asan_lc.c 
// Test asan with lc https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=876973
//
// REQUIRES: clang
// RUN: %clang -fsanitize=address %s -o %t -lc
// RUN: %t
// XFAIL: i686, i386

#include <stdio.h>
int main(int argc, char **argv)
{
   printf("Hello world!\n");
   return 0;
}

$ clang-11 -fsanitize=address test_asan_lc.c -o test_asan_lc -lc

$ readelf -a test_asan_lc | grep '<corrupt>'
   634: 0000000000483090     9 FUNC    WEAK   DEFAULT   15 longjmp@@<corrupt>
  1905: 0000000000483120     9 FUNC    WEAK   DEFAULT   15 siglongjmp@@<corrupt>

This doesn't happen when not using -lc
$ clang-11 -fsanitize=address test_asan_lc.c -o test_asan_lc
$ readelf -a test_asan_lc | grep '<corrupt>'

The issues happens at least on amd64 and arm64. Up to know ld.so has
been able to work despite these broken symbols. Following the move of
the pthread_gettattr_np symbols from libpthread.so to libc.so [1], this
is not longer the case on arm64, where ld.so fails when looking up the
longjmp symbol.

This causes the llvm-toolchain-11 [2] and llvm-toolchain-12 [3] to fail,
blocking the glibc 2.32 transition.

Please find attached the test_asan_lc.c source code, as well as a
reproducer calling ld directly with all the arm64 objects included.
"""
Comment 1 H.J. Lu 2021-09-18 03:37:25 UTC
[hjl@gnu-gram-1 longjmp]$ cat x.c
#include <setjmp.h>

jmp_buf buf;

int
main ()
{
  longjmp (buf,1);
  return 0;
}
[hjl@gnu-gram-1 longjmp]$ cat y.c
#include <setjmp.h>

__attribute__ ((weak))
void
longjmp(jmp_buf env, int val)
{
}
[hjl@gnu-gram-1 longjmp]$ make
gcc    -c -o x.o x.c
gcc    -c -o y.o y.c
/usr/include/setjmp.h: In function ‘longjmp’:
y.c:7:1: warning: ‘noreturn’ function does return
    7 | }
      | ^
gcc -o x x.o y.o -lc -pthread
readelf --wide --dyn-syms x | grep longjmp
     3: 0000000000401119    15 FUNC    WEAK   DEFAULT   12 longjmp@@<corrupt>
[hjl@gnu-gram-1 longjmp]$
Comment 2 H.J. Lu 2021-09-18 16:23:08 UTC
A patch is posted at:

https://sourceware.org/pipermail/binutils/2021-September/117972.html
Comment 3 Sourceware Commits 2021-09-19 14:37:29 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

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

commit e4675a5810fc3d0a7d0b1c744f0a1837ccc0507e
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat Sep 18 09:12:27 2021 -0700

    elf: Don't set version info on unversioned symbols
    
    Don't set version info on unversioned symbols when seeing a hidden
    versioned symbol after an unversioned definition and the default
    versioned symbol.
    
    bfd/
    
            PR ld/28348
            * elflink.c (elf_link_add_object_symbols): Don't set version info
            on unversioned symbols.
    
    ld/
    
            PR ld/28348
            * testsuite/ld-elf/pr28348.rd: New file.
            * testsuite/ld-elf/pr28348.t: Likewise.
            * testsuite/ld-elf/pr28348a.c: Likewise.
            * testsuite/ld-elf/pr28348b.c: Likewise.
            * testsuite/ld-elf/pr28348c.c: Likewise.
            * testsuite/ld-elf/shared.exp: Run PR ld/28348 tests.
Comment 4 H.J. Lu 2021-09-19 14:39:19 UTC
Fixed for 2.38.