<html>
    <head>
      <base href="https://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 --- - lld-linked FreeBSD libc contains .text relocations"
   href="https://llvm.org/bugs/show_bug.cgi?id=26813">26813</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>lld-linked FreeBSD libc contains .text relocations
          </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>FreeBSD
          </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>All Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>emaste@freebsd.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Blocks</th>
          <td>23214
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Found during ongoing attempts to link the FreeBSD base system with lld.

For processing .text relocations the runtime loader temporarily maps the
segment with write permission. I discovered that we have an issue in FreeBSD's
runtime loader: it expects the first PT_LOAD segment to be .text, and
unprotects only that one. (That issue is tracked in
<a href="http://bugs.freebsd.org/207631">http://bugs.freebsd.org/207631</a>.)

This uncovered two lld issues, however.

First, lld should set DT_TEXTREL or DF_TEXTREL if the output contains
relocations in non-writable segments.

DF_TEXTREL
    If this flag is not set, no relocation entry should cause a modification to
a non-writable segment, as specified by the segment permissions in the program
header table. If this flag is set, one or more relocation entries might request
modifications to a non-writable segment, and the dynamic linker can prepare
accordingly.

The real issue here though is that the relocation exists at all.

feynman% findtextrel lib/libc.so.7 | head
lib/libc.so.7: ELF object contains text relocation records:
lib/libc.so.7:   off: 0x9d0ac, func: __sys_numa_setaffinity
lib/libc.so.7:   off: 0x9d0cc, func: __sys_numa_getaffinity
lib/libc.so.7:   off: 0x9d0ec, func: _procctl
...

feynman% readelf -r lib/libc.so.7 | grep 9d0ac
00000009d0ac  09ab00000002 R_X86_64_PC32     00000000001a62c4 .cerror +
fffffffffffffffc

These all come from the autogenerated libc syscall wrappers.

#define RSYSCALL(name)  ENTRY(__sys_##name);                            \      
                        WEAK_REFERENCE(__sys_##name, name);             \      
                        WEAK_REFERENCE(__sys_##name, _##name);          \      
                        mov $SYS_##name,%eax; KERNCALL;                 \      
                        jb HIDENAME(cerror); ret;                       \      
                        END(__sys_##name)</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>