<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - clang comparing symbols from linker script wrong"
   href="http://llvm.org/bugs/show_bug.cgi?id=21786">21786</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>clang comparing symbols from linker script wrong
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.5
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>bogachev.pa@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=13441" name="attach_13441" title="objdump outputs of second example compiled with clang and with gcc">attachment 13441</a> <a href="attachment.cgi?id=13441&action=edit" title="objdump outputs of second example compiled with clang and with gcc">[details]</a></span>
objdump outputs of second example compiled with clang and with gcc

I'm trying to compare symbols defined in default linker script. The symbols are
pointing to the same address.

Consider the following code.

foo.c:

#include <stdio.h>

int main(void)
{
    extern void (*__preinit_array_start) ();
    extern void (*__preinit_array_end) ();

    if (&__preinit_array_start != &__preinit_array_end) {
        printf("%p != %p\n", &__preinit_array_start, &__preinit_array_end);
    } else {
        printf("%p == %p\n", &__preinit_array_start, &__preinit_array_end);
    }

    return 0;
}

Now I compile it with clang and run.
clang foo.c -o test-clang.o
./test-clang.o emits the following output:

0x600e0c != 0x600e0c

If I compile it with gcc I get:

0x600e0c == 0x600e0c

Both clang and gcc are from Ubuntu repositories.
clang --version
Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)
Target: x86_64-pc-linux-gnu
gcc --version
gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2


Additional info:
I did a small research and it seems that clang optimizes out (even with -O0)
such comparison deciding that addresses of exported symbols can't be the same.

Consider the following simplification of the program above:

void foo(void);

void bar(void);

int main(void)
{
    extern void (*__preinit_array_start) ();
    extern void (*__preinit_array_end) ();

    if (&__preinit_array_start != &__preinit_array_end) {
        foo();
    } else {
        bar();
    }

    return 0;
}

Now I compile it (without linking) with clang and with gcc for arm (just
because I understand arm assembly slightly better) and compare objdumps (in
attachment).
You can clearly see that clang skipped the "bar" branch completely.

For that I used prebuilt clang and gcc from android ndk r10c.

android-ndk-r10c/toolchains/llvm-3.5/prebuilt/linux-x86_64/bin/clang -target
arm-linux-androideabi --version
clang version 3.5 
Target: arm--linux-androideabi

android-ndk-r10c/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc
--version
arm-linux-androideabi-gcc (GCC) 4.8</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>