blob: 3eb24bdf791ec86be62ea8158c9fc40789268387 [file] [log] [blame]
Eric Christopher9cad53c2013-04-03 18:31:38 +00001//===-- 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 Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file implements ObjDumper.
Eric Christopher9cad53c2013-04-03 18:31:38 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "ObjDumper.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000016#include "Error.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000017#include "llvm/Object/ObjectFile.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000018#include "llvm/Support/ScopedPrinter.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000019#include "llvm/Support/raw_ostream.h"
20
21namespace llvm {
22
Zachary Turner88bb1632016-05-03 00:28:04 +000023ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {}
Eric Christopher9cad53c2013-04-03 18:31:38 +000024
25ObjDumper::~ObjDumper() {
26}
27
Paul Semelb98f5042018-07-11 10:00:29 +000028void 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 Christopher9cad53c2013-04-03 18:31:38 +000070} // namespace llvm