llvm-readobj: fix MachO relocatoin printing a bit.

There were two issues here:
1. At the very least, scattered relocations cannot use the same code to
   determine the corresponding symbol being referred to. For some reason we
   pretend there is no symbol, even when one actually exists in the symtab, so to
   match this behaviour getRelocationSymbol should simply return symbols_end for
   scattered relocations.
2. Printing "-" when we can't get a symbol (including the scattered case, but
   not exclusively), isn't that helpful. In both cases there *is* interesting
   information in that field, so we should print it. As hex will do.

Small part of rdar://problem/17553104

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212332 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Tim Northover
2014-07-04 10:57:56 +00:00
parent 9c1df9164c
commit 5b003bb869
7 changed files with 88 additions and 73 deletions

View File

@ -16,6 +16,7 @@
#include "ObjDumper.h"
#include "StreamWriter.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Object/MachO.h"
#include "llvm/Support/Casting.h"
@ -309,18 +310,29 @@ void MachODumper::printRelocation(const MachOObjectFile *Obj,
const RelocationRef &Reloc) {
uint64_t Offset;
SmallString<32> RelocName;
StringRef SymbolName;
if (error(Reloc.getOffset(Offset)))
return;
if (error(Reloc.getTypeName(RelocName)))
return;
symbol_iterator Symbol = Reloc.getSymbol();
if (Symbol != Obj->symbol_end() && error(Symbol->getName(SymbolName)))
return;
DataRefImpl DR = Reloc.getRawDataRefImpl();
MachO::any_relocation_info RE = Obj->getRelocation(DR);
bool IsScattered = Obj->isRelocationScattered(RE);
SmallString<32> SymbolNameOrOffset("0x");
if (IsScattered) {
// Scattered relocations don't really have an associated symbol
// for some reason, even if one exists in the symtab at the correct address.
SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE));
} else {
symbol_iterator Symbol = Reloc.getSymbol();
if (Symbol != Obj->symbol_end()) {
StringRef SymbolName;
if (error(Symbol->getName(SymbolName)))
return;
SymbolNameOrOffset = SymbolName;
} else
SymbolNameOrOffset += utohexstr(Obj->getPlainRelocationSymbolNum(RE));
}
if (opts::ExpandRelocs) {
DictScope Group(W, "Relocation");
@ -332,7 +344,7 @@ void MachODumper::printRelocation(const MachOObjectFile *Obj,
else
W.printNumber("Extern", Obj->getPlainRelocationExternal(RE));
W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
W.printString("Symbol", SymbolName.size() > 0 ? SymbolName : "-");
W.printString("Symbol", SymbolNameOrOffset);
W.printNumber("Scattered", IsScattered);
} else {
raw_ostream& OS = W.startLine();
@ -345,7 +357,7 @@ void MachODumper::printRelocation(const MachOObjectFile *Obj,
OS << " " << Obj->getPlainRelocationExternal(RE);
OS << " " << RelocName
<< " " << IsScattered
<< " " << (SymbolName.size() > 0 ? SymbolName : "-")
<< " " << SymbolNameOrOffset
<< "\n";
}
}