Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.

Bug 1633721

Summary: dlopen reports undefined symbol with RTLD_LAZY flag on ppc64le
Product: Red Hat Developer Toolset Reporter: Divya <dbasant>
Component: binutilsAssignee: Nick Clifton <nickc>
Status: CLOSED ERRATA QA Contact: Miloš Prchlík <mprchlik>
Severity: urgent Docs Contact:
Priority: urgent    
Version: DTS 9.1 RHEL 7CC: alanm, ashankar, casantos, codonell, cww, dbasant, dj, fweimer, jcrumple, jwright, knakai, law, mcermak, mkolbas, mnewsome, mpolacek, mprchlik, nickc, ohudlick, pfrankli, rogerio.souza, takirby, tborcin
Target Milestone: alphaKeywords: Reopened
Target Release: 9.0   
Hardware: ppc64le   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: No Doc Update
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2019-12-13 12:55:19 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
Reproducer
none
LD_DEBUG all output from ppc64le with new test case none

Description Divya 2018-09-27 14:57:00 UTC
Created attachment 1487816 [details]
Reproducer

Description of problem:
dlopen returns undefined symbol with RTLD_LAZY flag on ppc64le. Whereas same code 
return NULL on x86_64 and aarch64. 


Version-Release number of selected component (if applicable):
Customer has glibc-2.17-105.el7.ppc64le. 
I tested with glibc-2.17-196.el7.ppc64le and result remain the same.

How reproducible:
Always

Steps to Reproduce:
1. In the attached testcase archive, lop is executable for ppc64le. Execute it and notice the value of dlerror.
2. For comparision, run the x86 binary on x86_64 system and dlerror holds null.


Actual results:
dlerror=./libuvmpli.lnppc.so: undefined symbol: vpi_put_value

Expected results:
dlerror=(null)
Additional info:

Comment 1 Carlos O'Donell 2018-09-27 16:02:56 UTC
The test case is not valid.

The test has the following code:

        void *ret = dlopen(library_name, RTLD_LAZY);
        printf ("dlerror=%s\n", dlerror());

You fail to clear an old error by calling dlerror() before dlopen(). Old errors may be generated by constructors specific to the libraries loaded for the program.

In general you should only inspect the return of dlerror() if ret is NULL, since at that point you know dlopen() updated the error diagnostic message. You should not unconditionally check the result of dlerror() without first clearing it.

If dlopen() does not fail the value of dlerror() is unspecified and may reference an old error. This is exactly the reason it is recommended to call dlerror() after dlopen and *before* dlsym(), specifically to clear any previous errors not related to your current dlsym().

A valid test would be:

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int
main()
{
  const char *library_name = "./libuvmpli.lnx86.so";
  void *ret;
  /* Clear any errors generated at startup by constructors,
     initializers, or library startup.  */
  dlerror ();
  /* Open the library.  */
  ret  = dlopen(library_name, RTLD_LAZY);
  /* Check if the dlopen failed.  */
  if (ret == NULL)
    {
      /* Print the error related to the dlopen failure.  */
      printf ("FAIL: %s\n", dlerror());
      exit (1);
    }
  /* Clear any existing error message not related to the
     dlsym() we are about to call.  */
  dlerror ();
  /* ... code using dlsym () etc.  */
  printf ("PASS: No errors uding dlopen.\n");
  exit (0);
}

If this test still fails, please return the result of running the test program with LD_DEBUG=all to provide dynamic loader debug information e.g.

LD_DEBUG=all ./x86

I'll leave this bug open for a few days to let you update the results based on customer feedback, but I expect we'll close this CLOSED/NOTABUG since the reproducer is not valid.

Comment 2 Rogerio Moraes 2018-09-27 19:24:16 UTC
Hi Carlos,

thank you very much for your support. We run your testcase, here are the results:

vlch-kaiser [100] gcc -g -ldl x86-new.c -o x86
vlch-kaiser [101] ./x86
PASS: No errors uding dlopen.
vlch-kaiser [102]

chfclop01 [168] gcc -g -ldl lop-new.c -o lop
chfclop01 [169] lop
FAIL: ./libuvmpli.lnppc.so: undefined symbol: vpi_put_value

Regards,
--
Rogerio

Comment 3 Carlos O'Donell 2018-09-27 19:56:29 UTC
(In reply to Rogerio Moraes from comment #2)
> thank you very much for your support. We run your testcase, here are the
> results:

Thank you for the quick feedback with the improved reproducer. The new test definitely indicates something is wrong with your ppc64le build.
 
> vlch-kaiser [100] gcc -g -ldl x86-new.c -o x86
> vlch-kaiser [101] ./x86
> PASS: No errors uding dlopen.
> vlch-kaiser [102]

Good, this is expected. The vpi_put_value call has a relocation of type R_X86_64_JUMP_SLOT which is deferred until the call is made to the function.

> chfclop01 [168] gcc -g -ldl lop-new.c -o lop
> chfclop01 [169] lop
> FAIL: ./libuvmpli.lnppc.so: undefined symbol: vpi_put_value

The shared object has a non-PLT relocation e.g. R_PPC64_ADDR64 against the vpi_put_value symbol, this means it must be resolved immediately at load time, and so no matter what you do with dlopen that symbol must be present. That relocation should have been a R_PPC64_JMP_SLOT relocation, but it wasn't.

The R_PPC64_ADDR64 relocation is against a GOT slot. So it looks like binutils elided the PLT, and that meant that it *expected* vpi_plt_value to always be available at startup, but that isn't true since DT_NEEDED shows only libc.so.6 being needed (and glibc doesn't provide a vpi_put_value).

How did you link libuvmpli.lnppc.so?

Did you alter the DT_NEEDED entries in .dynamic for libuvmpli.lnppc.so in any way post-build? Did you alter the DSO in any way with a post-processing tool?

Did you build all of the objects that went into the DSO with -fPIC?

Can you share all the object files that went into libuvmpli.lnppc.so and the link invocation line? That would allow me to look at how the call to vpi_put_value was processed and why it doesn't end up as a R_PPC64_JMP_SLOT which can be deferred without problem.

If not can you provide a test case that shows how libuvmpli.lnppc.so was built?

Comment 4 Divya 2018-09-28 10:48:36 UTC
Created attachment 1488037 [details]
LD_DEBUG all output from ppc64le with new test case

Comment 6 Divya 2018-10-02 07:18:10 UTC
Source files

====================================
File svdpi.h:

#ifdef __cplusplus
extern "C" {
#endif

/* a handle to a scope (an instance of a module or interface) */
typedef void* svScope;

void svSetScope(svScope);


#ifdef __cplusplus
}
#endif
====================================
File lib.c

#include <stdio.h>
#include "svdpi.h"


void uvm_pli_boot()
{
    svScope s = NULL;

    svSetScope(s);

}
====================================
File test.c:

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int
main()
{
  const char *library_name = "./lib.so";
  void *ret;
  /* Clear any errors generated at startup by constructors,
     initializers, or library startup.  */
  dlerror ();
  /* Open the library.  */
  ret  = dlopen(library_name, RTLD_LAZY);
  /* Check if the dlopen failed.  */
  if (ret == NULL)
    {
      /* Print the error related to the dlopen failure.  */
      printf ("FAIL: %s\n", dlerror());
      exit (1);
    }
  /* Clear any existing error message not related to the
     dlsym() we are about to call.  */
  dlerror ();
  /* ... code using dlsym () etc.  */
  printf ("PASS: No errors using dlopen.\n");
  exit (0);
}

Testcase below:
===================

File test.sh:

#!/bin/csh -f
gcc -g -fdebug-types-section -shared -fuse-ld=gold -fPIC -funit-at-a-time -c -o lib_pic.o lib.c

gcc lib_pic.o -fuse-ld=gold -shared -Bsymbolic -o lib.so

gcc -g -ldl test.c -o test

# Execute test.sh
./test.sh

# ./test
PASS: No errors using dlopen.

==================

File test2.sh:

gcc -g -fdebug-types-section -shared -mlongcall -fuse-ld=gold -fPIC -funit-at-a-time -c -o lib_pic.o lib.c

gcc lib_pic.o -fuse-ld=gold -shared -Bsymbolic -o lib.so


# Execute test2.sh
./test2.sh

# ./test
FAIL: ./lib.so: undefined symbol: svSetScope


It appears that -mlongcall is causing the problem here.

Comment 10 Carlos O'Donell 2018-11-01 21:19:40 UTC
Can the customer try the workaround of linking with the bfd linker and drop the -mlongcalls options?

Comment 12 Florian Weimer 2018-11-02 07:43:36 UTC
(In reply to Divya from comment #6)
> gcc lib_pic.o -fuse-ld=gold -shared -Bsymbolic -o lib.so

Rogerio, where did you get the gold linker for ppc64le?  I don't think we are shipping the gold linker on ppc64le, only the BFD linker.

If you need lazy binding for only a small set of service registration routines, you can manually disable longcalls for them using the shortcall attribute (in the svdpi.h header file):

void svSetScope(svScope) __attribute__ ((shortcall));

Since the call is external, the shortcall restriction does not apply to it.  (There is also #pragma longcall, which can be used with similar effect.)

We would also like to know why you have to use -mlongcall for your builds.  For our own builds, we use the BFD linker (not gold), and have not run into the need for -mlongcall, even with large objects.  The BFD linker will perform the shortcall/longcall switch automatically, which is why there is no need for the shortcall function attribute.

Comment 13 Rogerio Moraes 2018-11-02 14:32:04 UTC
Hi Florian,

thank you very much for your support.

the GOLD linker was built by me. I used the same configurations we use fox aarch64 and x86_64.

He had some issues using BFD linker for very large files in LoP, which were fixed when using GOLD linker from Binutils v2.28. Also we are using parallel linker feature “--enable-threads”, to speed up the linking time.

Without -mlongcall our compilation hangs trying to link our largest executables. 

We will perform some tests as you suggested. As soon as possible we will give you feedback.

Regards,
--
Rogerio

Comment 17 Nick Clifton 2018-11-06 11:40:13 UTC
(In Rogerio Moraes from comment #13)

Hi Rogerio,

> Without -mlongcall our compilation hangs trying to link our largest
> executables. 

Is it possible for you to build only the plugins with the -mlongcall 
option omitted, but leaving it enabled for the main executable(s) ?

The issue here appears to be that there is a conflict between how long
calls are implemented by gcc and how dynamically loaded shared libraries
(aka plugins) have external function calls resolved.  Currently I do not
think that there is going to be a quick fix for this problem, but if you
are able to build plugins without -mlongcall, then that might act as a
workaround.

Cheers
  Nick

Comment 18 Nick Clifton 2018-11-06 14:07:52 UTC
Hi Rogerio,

  The -mlongcall vs dlopen problem has been confirmed as a known gcc bug 
  for which there is currently no fix.  (One of the GCC developers is 
  working on a patch series to fix the problem, but it is not yet ready).

  In the meantime I would like to ask about the problems that you have
  building your executable without -mlongcall.  If these could be fixed
  then I think that you would have a way forward.  You say that the
  compilation hangs if you compile without -mlongcall.  Is this during
  the final linking stage, or during a gcc compilation ?  If it is the
  link stage, then have you tried reducing memory consumption via the
  -Wl,--reduce-memory-overheads and/or -Wl,--no-keep-memory options ?

Cheers
  Nick

Comment 19 Rogerio Moraes 2018-11-06 15:18:40 UTC
(In reply to Nick Clifton from comment #18)
> Hi Rogerio,
> 
>   The -mlongcall vs dlopen problem has been confirmed as a known gcc bug 
>   for which there is currently no fix.  (One of the GCC developers is 
>   working on a patch series to fix the problem, but it is not yet ready).
> 
>   In the meantime I would like to ask about the problems that you have
>   building your executable without -mlongcall.  If these could be fixed
>   then I think that you would have a way forward.  You say that the
>   compilation hangs if you compile without -mlongcall.  Is this during
>   the final linking stage, or during a gcc compilation ?  If it is the
>   link stage, then have you tried reducing memory consumption via the
>   -Wl,--reduce-memory-overheads and/or -Wl,--no-keep-memory options ?
> 
> Cheers
>   Nick

Hi Nick, thank you very much for your support.

About the workaround that you suggested in comment 17, it works and we are using it.

About the issues when we do not use -mlongcall, the hang is during the final links, where we are combining .o’s and libraries.

We will try to use the parameters that you suggested for us and give feedback as soon as possible.

Regards,
--
Rogerio

Comment 24 Rogerio Moraes 2018-11-13 17:55:29 UTC
Hi Nick,

we have tried the options that you suggested, but it also failed.

Regards,
--
Rogerio

Comment 25 Nick Clifton 2018-11-15 16:00:48 UTC
Hi Rogerio,

(In reply to Rogerio Moraes from comment #24)
> we have tried the options that you suggested, but it also failed.

Oh dear.  I assume that the failure is the same as before ?

Can you give me some more information about how the linker is failing ?
Is it running out of memory ?  Causing a seg-fault or triggering some
kind of internal error ?  Creating a binary that does not work ?

It would be really helpful if there was a way for me to reproduce the
problem locally, so that I could investigate it further.  I suspect
however that this would be very hard for you to provide, since it will
presumably involve a lot of object files and libraries, most of them
proprietary ?

Given that you have already tried building and using your own version
of the gold linker, would you be able to build and test a newer version ?
The RHEL 7.5 bfd linker is based upon the 2.27 release of the GNU Binutils
from the FSF.  The latest release from them is 2.31.1, and it may well be
that this version will be able to link your applications.

Cheers
  Nick

Comment 48 Jeff Law 2019-09-10 14:49:15 UTC
The GCC and binutils engineering teams have determined that this bug is not going to be fixable in the RHEL 7 system tools as well as DTS 7.  Evaluation for fixing in the RHEL 8 system tools as well as DTS 8 is still underway


This bug has been moved to DTS 9 where this bug has been fixed for the upcoming fall release.

Additionally, fixing this issue requires fixing both binutils and gcc.  This bug will track the binutils component.  A separate bug will be opened to track the gcc component of the fixes for DTS 9.

Comment 51 Miloš Prchlík 2019-11-05 13:30:13 UTC
Verified using reproducer from comment #39.

Comment 53 errata-xmlrpc 2019-12-10 07:50:11 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHEA-2019:4139