<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - Address of _DYNAMIC symbol is incorrect when creating a shared object"
   href="https://bugs.llvm.org/show_bug.cgi?id=33802">33802</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Address of _DYNAMIC symbol is incorrect when creating a shared object
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>lld
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </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>ELF
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>rink@rink.nu
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=18795" name="attach_18795" title="Small sample illustrating the problem">attachment 18795</a> <a href="attachment.cgi?id=18795&action=edit" title="Small sample illustrating the problem">[details]</a></span>
Small sample illustrating the problem

While working on the ELF rtld for my Ananas operating system, I found a rather
nasty problem: it appears that linking anything which uses _DYNAMIC will not
get the correct address (it should be the virtual address of the DYNAMIC ELF
section, but I always get zero)

I'm using LLD 5.0.0 (trunk 308091). 

A small example is attached, with m.sh to build it. As it is annoying to try to
debug such things on the fly, I resorted to looking up the offset in the output
ELF file. In case I use lld, I get the following:

$ objdump -d t.so
...snip...
    1004:       48 8b 05 95 10 00 00    mov    0x1095(%rip),%rax        # 20a0
<_DYNAMIC+0xa0>

So 20a0 should contain the offset of _DYNAMIC.

$ readelf -l t.so
...snip...
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000002000 0x0000000000002000 0x0000000000002000
                 0x00000000000000a8 0x00000000000000a8  RW     0x1000
  DYNAMIC        0x0000000000002000 0x0000000000002000 0x0000000000002000
                 0x00000000000000a0 0x00000000000000a0  RW     0x8
...snip...

So we know address 20a0 will be at offset 20a0 in the file.

$ hd t.so|grep 20a0
000020a0  00 00 00 00 00 00 00 00  00 63 6c 61 6e 67 20 76  |.........clang v|

So the value is zero.

In case I use GNU ld (I used 2.28 from Debian/amd64) I obtain the following:

$ objdump -d t.so
...snip...
 234:   48 8b 05 bd 0d 20 00    mov    0x200dbd(%rip),%rax        # 200ff8
<.got>

Now 200ff8 contains the offset of _DYNAMIC.

$ readelf -l t.so
...snip...
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000f18 0x0000000000200f18 0x0000000000200f18
                 0x0000000000000100 0x0000000000000100  RW     0x200000
  DYNAMIC        0x0000000000000f18 0x0000000000200f18 0x0000000000200f18
                 0x00000000000000e0 0x00000000000000e0  RW     0x8

200ff8 will be at offset ff8 in the file, so:

$ hd t.so|grep ff0 
00000ff0  00 00 00 00 00 00 00 00  18 0f 20 00 00 00 00 00  |.......... .....|

Which correctly yields offset 200f18 (as that corresponds with the VirtAddr of
DYNAMIC above)</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>