How to solve the invalid ELF header probelm in ghci

I use 64bit Gentoo Linux. Sometimes when using ghci I get an error like this:

Loading package pcre-light ... can't load .so/.DLL for: c
(/usr/lib64/libpcre.so: invalid ELF header)

For reasons that I don’t fully understand for some libraries Gentoo places GNU linker files in /usr/lib64 instead of the libraries themselves. The files look like this:

/* GNU ld script
   Since Gentoo has critical dynamic libraries in /lib, and the static versions
   in /usr/lib, we need to have a "fake" dynamic lib in /usr/lib, otherwise we
   run into linking problems.  This "fake" dynamic lib is a linker script that
   redirects the linker to the real lib.  And yes, this works in the cross-
   compiling scenario as the sysroot-ed linker will prepend the real path.

   See bug http://bugs.gentoo.org/4411 for more info.
 */
OUTPUT_FORMAT ( elf64-x86-64 )
GROUP ( /lib64/libpcre.so.0 )

The problem occures (again for reasons I don’t fully understand) when ghci tries to access this library.

This is bug 2615 on the ghc bug tracker so people are working on it. As you might have been able to tell, I don’t know nearly enough to help fix this problem the sensible way but I have come up with the following solution:

  1. Backup your existing library/GNU ld file

    sudo cp /usr/lib64/libpcre.so /usr/lib64/libpcre.so.backup
  2. Make a symlink to the actual library file describe in the GNU ld file (in my case this is /lib64/libpcre.so.0)

    sudo ln /lib64/libpcre.so.0 /usr/lib64/libpcre.so

This works for me. So far.