<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 - llvm-objcopy outputs broken ELF image on large object files built with -ffunction-sections"
   href="https://bugs.llvm.org/show_bug.cgi?id=41461">41461</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>llvm-objcopy outputs broken ELF image on large object files built with -ffunction-sections
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>release blocker
          </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>eleviant@accesssoftek.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>Steps to reproduce:

1. Generate source file with this bash script:

#!/bin/bash
echo "extern int baz();"
count=0
while [ $count -le 65280 ]; do
  echo "int fun_$count() { return baz(); }"
  ((count++))
done

This will generate C source file with large number of functions (65281),
forcing some of symbol section indexes to become SHN_XINDEX.

2. Compile source 

clang <file> -c -ffunction-sections -o output.o

This will generate object file with each of 'fun_[0-9]+' residing in it's own
section followed by relocation section '.rela.fun_[0-9]+'. It's important that
sections in output.o are not sorted by offset, so llvm-objcopy will reorder
them in sortSection changing indexes.

3. Run llvm-objcopy

llvm-objcopy output.o output-bad.o

Now the bug can be observed using readelf:

readelf -sW output-bad.o | grep   'fun_65278'
126706: 0000000000000000    13 FUNC    GLOBAL DEFAULT  UND fun_65278

readelf -sW output-bad.o | grep '65280:'
65280: 0000000000000000     0 SECTION LOCAL  DEFAULT  UND

====================
Explanation:
The problem lies in .symtab_shndx section being filled too early (in
prepareForLayout) before indexes are assigned to the sections in
layoutSections. Those indexes may not match original indexes because
llvm-objcopy sorts sections by OriginalOffset. This causes symbol section
indexes in result image to not match sections those symbols are defined in
(when section index exceeds SHN_LORESERVE).</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>