1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 03:02:36 +01:00
llvm-mirror/tools/llvm-readobj/StreamWriter.cpp
Eric Christopher df46cef31b Implements low-level object file format specific output for COFF and
ELF with support for:

- File headers
- Section headers + data
- Relocations
- Symbols
- Unwind data (only COFF/Win64)

The output format follows a few rules:
- Values are almost always output one per line (as elf-dump/coff-dump already do). - Many values are translated to something readable (like enum names), with the raw value in parentheses.
- Hex numbers are output in uppercase, prefixed with "0x".
- Flags are sorted alphabetically.
- Lists and groups are always delimited.

Example output:
---------- snip ----------
Sections [
  Section {
    Index: 1
    Name: .text (5)
    Type: SHT_PROGBITS (0x1)
    Flags [ (0x6)
      SHF_ALLOC (0x2)
      SHF_EXECINSTR (0x4)
    ]
    Address: 0x0
    Offset: 0x40
    Size: 33
    Link: 0
    Info: 0
    AddressAlignment: 16
    EntrySize: 0
    Relocations [
      0x6 R_386_32 .rodata.str1.1 0x0
      0xB R_386_PC32 puts 0x0
      0x12 R_386_32 .rodata.str1.1 0x0
      0x17 R_386_PC32 puts 0x0
    ]
    SectionData (
      0000: 83EC04C7 04240000 0000E8FC FFFFFFC7  |.....$..........|
      0010: 04240600 0000E8FC FFFFFF31 C083C404  |.$.........1....|
      0020: C3                                   |.|
    )
  }
]
---------- snip ----------

Relocations and symbols can be output standalone or together with the section header as displayed in the example.
This feature set supports all tests in test/MC/COFF and test/MC/ELF (and I suspect all additional tests using elf-dump), making elf-dump and coff-dump deprecated.

Patch by Nico Rieck!

llvm-svn: 178679
2013-04-03 18:31:38 +00:00

80 lines
1.9 KiB
C++

#include "StreamWriter.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Format.h"
#include <cctype>
using namespace llvm::support;
namespace llvm {
raw_ostream &operator<<(raw_ostream &OS, const HexNumber& Value) {
uint64_t N = Value.Value;
// Zero is a special case.
if (N == 0)
return OS << "0x0";
char NumberBuffer[20];
char *EndPtr = NumberBuffer + sizeof(NumberBuffer);
char *CurPtr = EndPtr;
while (N) {
uintptr_t X = N % 16;
*--CurPtr = (X < 10 ? '0' + X : 'A' + X - 10);
N /= 16;
}
OS << "0x";
return OS.write(CurPtr, EndPtr - CurPtr);
}
void StreamWriter::printBinaryImpl(StringRef Label, StringRef Str,
ArrayRef<uint8_t> Data, bool Block) {
if (Data.size() > 16)
Block = true;
if (Block) {
startLine() << Label;
if (Str.size() > 0)
OS << ": " << Str;
OS << " (\n";
for (size_t addr = 0, end = Data.size(); addr < end; addr += 16) {
startLine() << format(" %04" PRIX64 ": ", uint64_t(addr));
// Dump line of hex.
for (size_t i = 0; i < 16; ++i) {
if (i != 0 && i % 4 == 0)
OS << ' ';
if (addr + i < end)
OS << hexdigit((Data[addr + i] >> 4) & 0xF, false)
<< hexdigit(Data[addr + i] & 0xF, false);
else
OS << " ";
}
// Print ascii.
OS << " |";
for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
if (std::isprint(Data[addr + i] & 0xFF))
OS << Data[addr + i];
else
OS << ".";
}
OS << "|\n";
}
startLine() << ")\n";
} else {
startLine() << Label << ":";
if (Str.size() > 0)
OS << " " << Str;
OS << " (";
for (size_t i = 0; i < Data.size(); ++i) {
if (i > 0)
OS << " ";
OS << format("%02X", static_cast<int>(Data[i]));
}
OS << ")\n";
}
}
} // namespace llvm