<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 - get symbol from relocation returns an iterator"
   href="https://bugs.llvm.org/show_bug.cgi?id=45765">45765</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>get symbol from relocation returns an iterator
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </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>Object
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>nick@wasmer.io
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>ObjectFile's RelocationRef::getSymbol() returns a symbol_iterator. What can you
do with this symbol iterator?

In practice, it appears that if you increment it, it will eventually go off
then end without ever equalling Binary->symbol_end().

Here's a short program that uses lib/Object C API to read through the symbols
associated with a relocation:

#include <iostream>

#include "llvm-c/Core.h"
#include "llvm-c/Object.h"

int main(void) {
  LLVMContextRef Context = LLVMContextCreate();

  LLVMMemoryBufferRef MemoryBuffer;
  char *OutMessage;
  LLVMCreateMemoryBufferWithContentsOfFile("test.o", &MemoryBuffer,
                                           &OutMessage);

  char *ErrorMessage;
  LLVMBinaryRef Binary = LLVMCreateBinary(MemoryBuffer, Context,
&ErrorMessage);

  LLVMSectionIteratorRef SI = LLVMObjectFileCopySectionIterator(Binary);
  if (LLVMObjectFileIsSectionIteratorAtEnd(Binary, SI)) {
    std::cerr << "section started at end\n";
    abort();
  }

  for (; !LLVMObjectFileIsSectionIteratorAtEnd(Binary, SI);
       LLVMMoveToNextSection(SI)) {
    LLVMRelocationIteratorRef RI = LLVMGetRelocations(SI);
    for (; !LLVMIsRelocationIteratorAtEnd(SI, RI);
         LLVMMoveToNextRelocation(RI)) {
      LLVMSymbolIteratorRef SymI = LLVMGetRelocationSymbol(RI);
      while (!LLVMObjectFileIsSymbolIteratorAtEnd(Binary, SymI)) {
        uint64_t addr = LLVMGetSymbolAddress(SymI);
        int size = LLVMGetSymbolSize(SymI);
        std::cerr << addr << " " << size << "\n";
        LLVMMoveToNextSymbol(SymI);
      }
      LLVMDisposeSymbolIterator(SymI);
    }
    LLVMDisposeRelocationIterator(RI);
  }

  LLVMDisposeSectionIterator(SI);

  LLVMDisposeBinary(Binary);

  LLVMDisposeMemoryBuffer(MemoryBuffer);
  LLVMContextDispose(Context);
}

If you cp /bin/ls to test.o and run it, you'll get output like this:

0 0
140832 8
140832 8
91040 41
140768 8
90544 17
140864 8
90928 101
140800 8
90864 55
140824 8
90576 21
0 0
90608 251
0 0
140808 8
3543892497278727534 1414094592
6081388296670498132 1701601889
5572159311341117300 1734701663
7450643069141147988 1768322149
7596447145782371692 1868785004

This is because the iterator has gone off the end. Eventually the program dies
with:

LLVM ERROR: Invalid data was encountered while parsing the file

. Is the symbol_iterator returned here ever supposed to be incremented? Or
should it only be used to refer to the one symbol? If we are supposed to be
able to increment, why doesn't LLVMObjectFileIsSymbolIteratorAtEnd work
reliably? And if we can increment it, what does it mean semantically, is it
just going through symbols in the section or is it supposed to be future-proof
to a world where relocations may refer to more than one symbol?</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>