NetBSD Problem Report #56031

From silas_netbsd_gnats@nocafe.net  Wed Mar  3 01:36:00 2021
Return-Path: <silas_netbsd_gnats@nocafe.net>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
	(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
	by mollari.NetBSD.org (Postfix) with ESMTPS id 32A241A921F
	for <gnats-bugs@gnats.NetBSD.org>; Wed,  3 Mar 2021 01:36:00 +0000 (UTC)
Message-Id: <20210303013549.E79BD21C55@hope.nocafe.net>
Date: Tue,  2 Mar 2021 22:35:49 -0300 (-03)
From: silas_netbsd_gnats@nocafe.net
Reply-To: silas_netbsd_gnats@nocafe.net
To: gnats-bugs@NetBSD.org
Subject: binary compiled with -pg (profiling) segfaults if linked against shared library
X-Send-Pr-Version: 3.95

>Number:         56031
>Category:       bin
>Synopsis:       binary compiled with -pg (profiling) segfaults if linked against shared library
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Mar 03 01:40:00 +0000 2021
>Last-Modified:  Wed Mar 03 10:15:01 +0000 2021
>Originator:     silas_netbsd_gnats@nocafe.net
>Release:        NetBSD 9.0
>Organization:
>Environment:
System: NetBSD xyz 9.0 NetBSD 9.0 (GENERIC) #0: Fri Feb 14 00:06:28 UTC 2020 mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/amd64/compile/GENERIC amd64
Architecture: x86_64
Machine: amd64
>Description:
When compiling a binary file with gcc's -pg flag and a shared library, the
binary segfaults.

See this simple example:

$ cat test.c
#include <stdio.h>
int main(int argc, char *argv[]) {
	printf("hello world\n");
	return 0;
}

$ cc -pg -o test test.c
./test
hello world

$ cc -pg -lcurses -o test test.c
ld: /usr/lib/libc.so.12: warning: warning: __findenv is an internal obsolete function.

$ ./test
[1]   Segmentation fault (core dumped) ./test

Later, I decided to add the -g flag to get debug symbols and see if I'd get something useful from gdb:

$ cc -g -pg -lcurses -o test test.c
ld: /usr/lib/libc.so.12: warning: warning: __findenv is an internal obsolete function.

$ ./test
[1]   Segmentation fault (core dumped) ./test

$ gdb -c test.core -ex bt -ex q test
GNU gdb (GDB) 8.3
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64--netbsd".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
[New process 1]
Core was generated by `test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000433e10 in dl_iterate_phdr ()
#0  0x0000000000433e10 in dl_iterate_phdr ()
#1  0x0000000000409e05 in __libc_static_tls_setup ()
#2  0x0000000000494487 in _libc_init ()
#3  0x00006fb919441d14 in ?? () from /usr/lib/libc.so.12
#4  0x00006fb91a0e0200 in ?? ()
#5  0x00006fb91943cdf9 in _init () from /usr/lib/libc.so.12
#6  0x0000000000000000 in ?? ()

Curious fact: I can profile (generate binary, run the program and use gprof) the
same program normally in NetBSD 8.0 armv7hf (Raspberry Pi 2).

For reference, I started a thread about this in netbsd-users@: http://mail-index.netbsd.org/netbsd-users/2021/02/28/msg026655.html
>How-To-Repeat:
$ cat test.c
#include <stdio.h>
int main(int argc, char *argv[]) {
	printf("hello world\n");
	return 0;
}

$ cc -pg -lcurses -o test test.c
ld: /usr/lib/libc.so.12: warning: warning: __findenv is an internal obsolete function.

$ ./test
[1]   Segmentation fault (core dumped) ./test
>Fix:

>Audit-Trail:
From: matthew green <mrg@eterna.com.au>
To: gnats-bugs@netbsd.org
Cc: gnats-admin@netbsd.org, netbsd-bugs@netbsd.org
Subject: re: bin/56031: binary compiled with -pg (profiling) segfaults if linked against shared library
Date: Wed, 03 Mar 2021 21:13:40 +1100

 this is a GCC specs issue.  profiling needs static, and it
 is not getting static due to missing spec.

 you should be able to work around this with explicit -static,
 but you can't use -pg and shlibs.  this is not supported.


 .mrg.

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.46 2020/01/03 16:35:01 leot Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2020 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.