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
Created attachment 460508 [details, diff] adds volatile
Please attach the full build log and emerge --info.
Created attachment 461360 [details] build.log
Created attachment 461362 [details] config.log
Created attachment 461364 [details] emerge --info
log provided. please check
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.
*** Bug 644562 has been marked as a duplicate of this bug. ***
*** Bug 578548 has been marked as a duplicate of this bug. ***
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?
I wrote an email to the community, waiting for an answer
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
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.
*** Bug 685186 has been marked as a duplicate of this bug. ***
Seems obsolete given we're on 6.2 now?