blob: 4084693187402a0038d44124b83e5bc4c160cc09 [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"
Paul Semel6e137902018-07-18 18:00:41 +000017#include "llvm-readobj.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000018#include "llvm/Object/ObjectFile.h"
Paul Semel6e137902018-07-18 18:00:41 +000019#include "llvm/Support/Error.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000020#include "llvm/Support/ScopedPrinter.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000021#include "llvm/Support/raw_ostream.h"
22
23namespace llvm {
24
Zachary Turner88bb1632016-05-03 00:28:04 +000025ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {}
Eric Christopher9cad53c2013-04-03 18:31:38 +000026
27ObjDumper::~ObjDumper() {
28}
29
Paul Semel6e137902018-07-18 18:00:41 +000030static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
31 for (size_t i = 0; i < Len; i++)
32 W << (isprint(Start[i]) ? static_cast<char>(Start[i]) : '.');
33}
34
35static Expected<object::SectionRef>
36getSecNameOrIndexAsSecRef(const object::ObjectFile *Obj, StringRef SecName) {
37 char *StrPtr;
38 long SectionIndex = strtol(SecName.data(), &StrPtr, 10);
39 object::SectionRef Section;
40 unsigned SecIndex = 0;
41 for (object::SectionRef SecRef : Obj->sections()) {
42 if (*StrPtr) {
43 StringRef SectionName;
44
45 if (std::error_code E = SecRef.getName(SectionName))
46 return errorCodeToError(E);
47
48 if (SectionName == SecName)
49 return SecRef;
50 } else if (SecIndex == SectionIndex)
51 return SecRef;
52
53 SecIndex++;
54 }
55 return make_error<StringError>("invalid section reference",
56 object::object_error::parse_failed);
57}
58
59void ObjDumper::printSectionAsString(const object::ObjectFile *Obj,
60 StringRef SecName) {
61 Expected<object::SectionRef> SectionRefOrError =
62 getSecNameOrIndexAsSecRef(Obj, SecName);
63 if (!SectionRefOrError)
64 error(std::move(SectionRefOrError));
65 object::SectionRef Section = *SectionRefOrError;
66 StringRef SectionName;
67
68 if (std::error_code E = Section.getName(SectionName))
69 error(E);
70 W.startLine() << "String dump of section '" << SectionName << "':\n";
71
72 StringRef SectionContent;
73 Section.getContents(SectionContent);
74
75 const uint8_t *SecContent = SectionContent.bytes_begin();
76 const uint8_t *CurrentWord = SecContent;
77 const uint8_t *SecEnd = SectionContent.bytes_end();
78
79 while (CurrentWord <= SecEnd) {
80 size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
81 SecEnd - CurrentWord);
82 if (!WordSize) {
83 CurrentWord++;
84 continue;
85 }
86 W.startLine() << format("[%6tx] ", CurrentWord - SecContent);
87 printAsPrintable(W.startLine(), CurrentWord, WordSize);
88 W.startLine() << '\n';
89 CurrentWord += WordSize + 1;
90 }
91}
92
Paul Semelb98f5042018-07-11 10:00:29 +000093void ObjDumper::SectionHexDump(StringRef SecName, const uint8_t *Section,
94 size_t Size) {
95 const uint8_t *SecContent = Section;
96 const uint8_t *SecEnd = Section + Size;
97 W.startLine() << "Hex dump of section '" << SecName << "':\n";
98
99 for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {
100 const uint8_t *TmpSecPtr = SecPtr;
101 uint8_t i;
102 uint8_t k;
103
104 W.startLine() << format_hex(SecPtr - SecContent, 10);
105 W.startLine() << ' ';
106 for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) {
107 for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) {
108 uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr));
109 W.startLine() << format_hex_no_prefix(Val, 2);
110 }
111 W.startLine() << ' ';
112 }
113
114 // We need to print the correct amount of spaces to match the format.
115 // We are adding the (4 - i) last rows that are 8 characters each.
116 // Then, the (4 - i) spaces that are in between the rows.
117 // Least, if we cut in a middle of a row, we add the remaining characters,
118 // which is (8 - (k * 2))
119 if (i < 4)
120 W.startLine() << format("%*c", (4 - i) * 8 + (4 - i) + (8 - (k * 2)),
121 ' ');
122
123 TmpSecPtr = SecPtr;
124 for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) {
125 if (isprint(TmpSecPtr[i]))
126 W.startLine() << TmpSecPtr[i];
127 else
128 W.startLine() << '.';
129 }
130
131 W.startLine() << '\n';
132 }
133}
134
Eric Christopher9cad53c2013-04-03 18:31:38 +0000135} // namespace llvm