1 //===-- ObjDumper.cpp - Base dumper class -----------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// This file implements ObjDumper. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "ObjDumper.h" 16 #include "Error.h" 17 #include "llvm/Object/ObjectFile.h" 18 #include "llvm/Support/ScopedPrinter.h" 19 #include "llvm/Support/raw_ostream.h" 20 21 namespace llvm { 22 23 ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {} 24 25 ObjDumper::~ObjDumper() { 26 } 27 28 void ObjDumper::SectionHexDump(StringRef SecName, const uint8_t *Section, 29 size_t Size) { 30 const uint8_t *SecContent = Section; 31 const uint8_t *SecEnd = Section + Size; 32 W.startLine() << "Hex dump of section '" << SecName << "':\n"; 33 34 for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) { 35 const uint8_t *TmpSecPtr = SecPtr; 36 uint8_t i; 37 uint8_t k; 38 39 W.startLine() << format_hex(SecPtr - SecContent, 10); 40 W.startLine() << ' '; 41 for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) { 42 for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) { 43 uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr)); 44 W.startLine() << format_hex_no_prefix(Val, 2); 45 } 46 W.startLine() << ' '; 47 } 48 49 // We need to print the correct amount of spaces to match the format. 50 // We are adding the (4 - i) last rows that are 8 characters each. 51 // Then, the (4 - i) spaces that are in between the rows. 52 // Least, if we cut in a middle of a row, we add the remaining characters, 53 // which is (8 - (k * 2)) 54 if (i < 4) 55 W.startLine() << format("%*c", (4 - i) * 8 + (4 - i) + (8 - (k * 2)), 56 ' '); 57 58 TmpSecPtr = SecPtr; 59 for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) { 60 if (isprint(TmpSecPtr[i])) 61 W.startLine() << TmpSecPtr[i]; 62 else 63 W.startLine() << '.'; 64 } 65 66 W.startLine() << '\n'; 67 } 68 } 69 70 } // namespace llvm 71