[llvm-bugs] [Bug 45765] New: get symbol from relocation returns an iterator

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Apr 30 16:57:23 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=45765

            Bug ID: 45765
           Summary: get symbol from relocation returns an iterator
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Object
          Assignee: unassignedbugs at nondot.org
          Reporter: nick at wasmer.io
                CC: llvm-bugs at lists.llvm.org

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?

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200430/7aa3ee49/attachment-0001.html>


More information about the llvm-bugs mailing list