<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 - Bad interaction with llvm-objcopy + gold"
   href="https://bugs.llvm.org/show_bug.cgi?id=42052">42052</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Bad interaction with llvm-objcopy + gold
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>tools
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>llvm-objcopy/strip
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>rupprecht@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>alexander.v.shaposhnikov@gmail.com, jake.h.ehrlich@gmail.com, jh7370.2008@my.bristol.ac.uk, llvm-bugs@lists.llvm.org, rupprecht@google.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Before llvm-objcopy writes out, it sorts sections by offset:
<a href="https://github.com/llvm/llvm-project/blob/master/llvm/tools/llvm-objcopy/ELF/Object.cpp#L1480">https://github.com/llvm/llvm-project/blob/master/llvm/tools/llvm-objcopy/ELF/Object.cpp#L1480</a>

However, gold expects that the index of section groups is smaller than the
indices of sections that it contains:
<a href="https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=gold/object.cc;h=689448f50c8a784d1142032875203ffc1d28661f;hb=HEAD#l1090">https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;a=blob;f=gold/object.cc;h=689448f50c8a784d1142032875203ffc1d28661f;hb=HEAD#l1090</a>

This does seem to be required by the ELF spec: "The section header table entry
for a group section must appear in the section header table before the entries
for any of the sections that are members of the group." Only gold complains
though -- ld and lld work fine (accepting invalid ELF, I guess).

Because llvm-objcopy only looks at the offsets, if an SHT_GROUP section has a
greater offset than the sections it contains (while still having the section
header come first), llvm-objcopy can rearrange it into an invalid ordering.

Small-ish repro:
$ cat repro.cc
#include <string>
std::string foo(std::string x) { return x + x; }
int main() {
  foo("x");
  return 0;
}

# Normal link w/ gold (passes)
$ clang++ -c group.cc -o /tmp/group.o
$ clang++ -fuse-ld=gold -o /tmp/group /tmp/group.o

# Invoke GNU objcopy & link w/ gold (passes)
$ objcopy /tmp/group.o /tmp/group.gnu.o
$ clang++ -fuse-ld=gold -o /tmp/group /tmp/group.gnu.o

# Invoke llvm-objcopy & link w/ gold (fails)
$ llvm-objcopy /tmp/group.o /tmp/group.llvm.o
$ clang++ -fuse-ld=gold -o /tmp/group /tmp/group.llvm.o
/usr/bin/ld.gold: error: /tmp/group.llvm.o: invalid section group 8 refers to
earlier section 2

The simplest thing is probably to change that comparator to always put
SHT_GROUP first, which I believe is effectively what GNU objcopy does.</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>