<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 - PowerPC64: function calls missing relocations in PIC codegen when the callee is defined in same CU."
   href="https://bugs.llvm.org/show_bug.cgi?id=39080">39080</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>PowerPC64: function calls missing relocations in PIC codegen when the callee is defined in same CU.
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </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>Backend: PowerPC
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>sfertile@ca.ibm.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When compiling a file for a shared object (-fpic and not -fPIE) we need to emit
a relocation for calls where the callee is interposable. When the callee is
defined in the same translation unit we fail to do this.

eg:
test.c:
__attribute__((noinline))
int defined(int i) {
  return i;
}

int a = 55;

int caller(void) {
  return defined (a);
}

clang -O2 -c -fpic test.c                                                       
objdump -Dr test.o | less 

0000000000000000 <defined>:
   0:   20 00 80 4e     blr
        ...

0000000000000010 <caller>:
  10:   00 00 4c 3c     addis   r2,r12,0
                        10: R_PPC64_REL16_HA    .TOC.
  14:   00 00 42 38     addi    r2,r2,0
                        14: R_PPC64_REL16_LO    .TOC.+0x4
  18:   a6 02 08 7c     mflr    r0
  1c:   10 00 01 f8     std     r0,16(r1)
  20:   e1 ff 21 f8     stdu    r1,-32(r1)
  24:   00 00 62 3c     addis   r3,r2,0
                        24: R_PPC64_TOC16_HA    .toc
  28:   00 00 63 e8     ld      r3,0(r3)
                        28: R_PPC64_TOC16_LO_DS .toc
  2c:   02 00 63 e8     lwa     r3,0(r3)
  30:   d1 ff ff 4b     bl      0 <defined>
  34:   00 00 00 60     nop
  38:   20 00 21 38     addi    r1,r1,32
  3c:   10 00 01 e8     ld      r0,16(r1)
  40:   a6 03 08 7c     mtlr    r0
  44:   20 00 80 4e     blr


Instead of having a relocation to defined, the bl instruction already has the
offset immediate encoded in it `4bffffd1` = `bl -0x30`. This means the linker
will never create a plt stub for the call and if the dynamic linker resolves a
different definition of `defined` at runtime some calls will be redirect to
that definition while others will still call this definition. 

It can be worked around by using the `--function-sections` option as we emit
the relocation in that case:

  20:   01 00 00 48     bl      20 <caller+0x20>
                        20: R_PPC64_REL24       defined</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>