Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 606142 - sys-libs/ncurses-6.0-r1 failed to build with -flto -O2
Summary: sys-libs/ncurses-6.0-r1 failed to build with -flto -O2
Status: RESOLVED OBSOLETE
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: x86 Linux
: Normal normal
Assignee: Gentoo's Team for Core System packages
URL:
Whiteboard:
Keywords: PATCH
: 578548 644562 685186 (view as bug list)
Depends on:
Blocks: lto
  Show dependency tree
 
Reported: 2017-01-18 03:49 UTC by Xuefer
Modified: 2021-11-03 10:55 UTC (History)
6 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
adds volatile (ncursesfix.diff,402 bytes, patch)
2017-01-18 03:51 UTC, Xuefer
Details | Diff
build.log (build.log,34.98 KB, text/plain)
2017-01-25 09:11 UTC, Xuefer
Details
config.log (config.log,19.94 KB, text/plain)
2017-01-25 09:11 UTC, Xuefer
Details
emerge --info (emerge.info,6.50 KB, text/plain)
2017-01-25 09:12 UTC, Xuefer
Details
ncurses-6.1-configure-link-test.patch (ncurses-6.1-configure-link-test.patch,3.88 KB, patch)
2018-04-01 20:04 UTC, Alexander Miller
Details | Diff
ncurses-6.1-configure-link-test-upstream.patch (0001-Backport-configure-changes-for-new-optimization.patch,19.78 KB, patch)
2019-04-08 00:34 UTC, Jory A. Pratt
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Xuefer 2017-01-18 03:49:18 UTC
without -flto or without -O2 produce good (expected) result:
================================
configure:5332: checking for dlsym
configure:5369: clang -o conftest -O2 -pipe -march=ivybridge -O2 -pipe
  conftest.c  >&5
/tmp/conftest-597c62.o:conftest.c:function main: error: undefined
reference to 'dlsym'
clang-3.9: error: linker command failed with exit code 1 (use -v to
see invocation)
configure:5372: $? = 1
configure: failed program was:
#line 5338 "configure"
#include "confdefs.h"
/* System header to define __stub macros and hopefully few prototypes,
    which can conflict with char dlsym (); below.  */
#include <assert.h>
/* Override any gcc2 internal prototype to avoid an error.  */
#ifdef __cplusplus
extern "C"
#endif
/* We use char because int might match the return type of a gcc2
   builtin and then its argument prototype would still apply.  */
char dlsym ();
char (*f) ();

int
main ()
{
/* The GNU C library defines this for functions which it implements
    to always fail with ENOSYS.  Some functions are actually named
    something starting with __ and the normal name is an alias.  */
#if defined (__stub_dlsym) || defined (__stub___dlsym)
choke me
#else
f = dlsym; /* workaround for ICC 12.0.3 */ if (f == 0) return 1;
#endif

  ;
  return 0;
}
================================

 with both -flto and -O2 produce bad result (tested with clang and
gcc), this is reproduced when i build ncurses which have strange
configure script built with many workaround, unlike other configure
script

================================
configure:5332: checking for dlsym
configure:5369: clang -o conftest -flto -O2 -pipe -march=ivybridge -O2
-pipe   conftest.c  >&5
configure:5372: $? = 0
configure:5375: test -s conftest
configure:5378: $? = 0
configure:5388: result: yes
================================

Reproducible: Always

Steps to Reproduce:
1. use -flto -O2 CFLAGS in make.conf
2. ebuild ncurses-6.0-r1.ebuild clean compile

Actual Results:  
checking for dlsym... checking for dlsym... yes
checking whether able to link to dl*() functions... yes
checking whether able to link to dl*() functions... yes
checking whether able to link to dl*() functions... yes
checking whether able to link to dl*() functions... configure: error: Cannot link test program for libdl
configure: error: Cannot link test program for libdl
configure: error: Cannot link test program for libdl
configure: error: Cannot link test program for libdl

Expected Results:  
checking for dlsym... checking for dlsym... no (yes, it's should be no, without -ldl)

suggested fix is to have char (*f) (); change into char (*volatile f) (); so it won't get optimized out
Comment 1 Xuefer 2017-01-18 03:51:27 UTC
Created attachment 460508 [details, diff]
adds volatile
Comment 2 Michael Palimaka (kensington) gentoo-dev 2017-01-18 17:05:27 UTC
Please attach the full build log and emerge --info.
Comment 3 Xuefer 2017-01-25 09:11:24 UTC
Created attachment 461360 [details]
build.log
Comment 4 Xuefer 2017-01-25 09:11:58 UTC
Created attachment 461362 [details]
config.log
Comment 5 Xuefer 2017-01-25 09:12:04 UTC
Created attachment 461364 [details]
emerge --info
Comment 6 Xuefer 2017-01-25 09:12:41 UTC
log provided. please check
Comment 7 Alexander Miller 2018-04-01 20:04:58 UTC
Created attachment 526274 [details, diff]
ncurses-6.1-configure-link-test.patch

Version 6.1-r2 still fails with -O2 -flto:
  [...]
  checking for gpm.h... yes
  checking if you want to load GPM dynamically... yes
  checking for dlsym... yes
  checking whether able to link to dl*() functions... configure: error: Cannot link test program for libdl

Actually, "checking for dlsym" should fail, but the configure test succeeds although the symbol is not available. Then, the check which is supposed to succeed ("checking for dlsym in -ldl") is skipped, and the last test which tries to use the dl* functions fails because it isn't linked with -ldl.

The reason can be seen in the conftest source cited in comment #0. The important part is the following line:
  f = dlsym; /* workaround for ICC 12.0.3 */ if (f == 0) return 1;
The workaround doesn't work for gcc -O2 -flto because it's smart enough to know that the function's address must be non-null. So it optimizes out both the "if" and the assignment, and the symbol to be testes becomes unused!

Upstream should really rebuild the configure script with a newer version of autotools to fix the issue. Until that happend we need yet another workaround. The attached patch works for me. I didn't add "volatile" as Xuefer suggested, but used the address as return value to ensure it won't get optimized out.
Comment 8 SpanKY gentoo-dev 2018-06-25 21:43:25 UTC
*** Bug 644562 has been marked as a duplicate of this bug. ***
Comment 9 SpanKY gentoo-dev 2018-06-25 21:56:01 UTC
*** Bug 578548 has been marked as a duplicate of this bug. ***
Comment 10 Denis Pronin 2018-10-03 20:28:52 UTC
I also faced the issue and found out that the reason is 'dlsym' optimized out by link time optimization

Did you write an email to ncurses.org?
Comment 11 Denis Pronin 2018-10-03 20:48:46 UTC
I wrote an email to the community, waiting for an answer
Comment 12 Denis Pronin 2018-10-07 11:03:39 UTC
Guys, 
Thanks to Thomas E. Dickey, he has fixed the issue and provided a new version of ncurses
Here the changelog is https://invisible-island.net/ncurses/NEWS.html#t20181006
We need to update ncurses in order to obtain a new fixed version
Comment 13 Jory A. Pratt gentoo-dev 2019-04-08 00:34:00 UTC
Created attachment 572212 [details, diff]
ncurses-6.1-configure-link-test-upstream.patch

This is the back port of the upstream patch. I have not tested it yet. I wanted to make it available so we can get it tested and possibly added to a -r4 ebuild.
Comment 14 Mike Gilbert gentoo-dev 2019-05-06 14:26:33 UTC
*** Bug 685186 has been marked as a duplicate of this bug. ***
Comment 15 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2021-08-25 02:24:47 UTC
Seems obsolete given we're on 6.2 now?