[mpich-devel] Weak symbols, clang, and users compiling with -fvisibility=hidden

Jed Brown jedbrown at mcs.anl.gov
Mon Jan 6 12:14:27 CST 2014

MPICH's test for weak symbols is:

  int foo(int) __attribute__((weak,alias("__foo")));

where __foo is not defined in the same compilation unit.  This fails
with gcc (tested with 4.8 and 4.4):

conftest.c:37:5: error: 'foo' aliased to undefined symbol '__foo'
 int foo(int) __attribute__((weak,alias("__foo")));

This is (evidently) a change from gcc-3.x, but is intentional because
knowledge of the weak alias is not desirable/meaningful in other
compilation units.


My understanding of gcc's intent is that the attribute only be stated in
the implementation.  The caller can't do anything with the information
that foo is a weak alias; it still needs to generate a PLT entry and the
loader will need to perform a relocation.

Now my problem is that clang accepts the test code above (i.e., it does
not insist that __foo be defined in the same compilation unit) which causes

  #define MPICH_ATTR_WEAK_ALIAS(fname) __attribute__((weak,alias(fname)))

but this breaks PMPI and when -fvisibility=hidden is used to compile
user code, it generates (objdump -r)

OFFSET           TYPE              VALUE 
0000000000000005 R_X86_64_PC32     __foo-0x0000000000000004

instead of the linkable (but not desired because it prevents interposing

OFFSET           TYPE              VALUE 
0000000000000005 R_X86_64_PLT32    __foo-0x0000000000000004

What we actually want is an honest relocation for "foo":

OFFSET           TYPE              VALUE 
0000000000000005 R_X86_64_PLT32    foo-0x0000000000000004

When you try to link the first version later, you get

/usr/bin/ld.gold: error: bar.o: requires dynamic R_X86_64_PC32 reloc against '__foo' which may overflow at runtime; recompile with -fPIC

which is not actually an -fPIC error, but rather a result of clang using
R_X86_64_PC32 for a symbol that we really want to be R_X86_64_PLT32.  I
think this should probably be reported to Clang/LLVM (because it delays
a confusing error condition), but I also think that MPICH should fix its

So in summary, we can't have __attribute__((weak,alias("PMPI_..."))) in
the header because:

  1. it does not compile with gcc
  2. it breaks PMPI interposition with clang
  3. it breaks -fvisibility=hidden with clang

Instead, it should go in the implementation files.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://lists.mpich.org/pipermail/devel/attachments/20140106/64cb11cd/attachment-0001.pgp>

More information about the devel mailing list