Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 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 |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// This file implements ObjDumper. |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ObjDumper.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 16 | #include "Error.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 17 | #include "llvm/Object/ObjectFile.h" |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ScopedPrinter.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
Zachary Turner | 88bb163 | 2016-05-03 00:28:04 +0000 | [diff] [blame] | 23 | ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {} |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 24 | |
| 25 | ObjDumper::~ObjDumper() { |
| 26 | } |
| 27 | |
Paul Semel | b98f504 | 2018-07-11 10:00:29 +0000 | [diff] [blame] | 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 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 70 | } // namespace llvm |