blob: a725140c9d33f11e2249bbc4e2204f567d8e54f8 [file] [log] [blame]
Eric Christopher76e70f32013-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 Prantl26b584c2018-05-01 15:54:18 +000011/// This file implements ObjDumper.
Eric Christopher76e70f32013-04-03 18:31:38 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "ObjDumper.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000016#include "Error.h"
Paul Semel9f006d32018-07-18 18:00:41 +000017#include "llvm-readobj.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000018#include "llvm/Object/ObjectFile.h"
Paul Semel9f006d32018-07-18 18:00:41 +000019#include "llvm/Support/Error.h"
Zachary Turner2448e9f2016-05-03 00:28:04 +000020#include "llvm/Support/ScopedPrinter.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000021#include "llvm/Support/raw_ostream.h"
22
23namespace llvm {
24
Zachary Turner2448e9f2016-05-03 00:28:04 +000025ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {}
Eric Christopher76e70f32013-04-03 18:31:38 +000026
27ObjDumper::~ObjDumper() {
28}
29
Paul Semel9f006d32018-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++)
Michael Kruse6069e662018-07-26 15:31:41 +000032 W << (isPrint(Start[i]) ? static_cast<char>(Start[i]) : '.');
Paul Semel9f006d32018-07-18 18:00:41 +000033}
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;
Paul Semelcac968e2018-07-25 10:04:37 +000040 long SecIndex;
41 if (Obj->isELF())
42 SecIndex = 0;
43 else
44 SecIndex = 1;
Paul Semel9f006d32018-07-18 18:00:41 +000045 for (object::SectionRef SecRef : Obj->sections()) {
46 if (*StrPtr) {
47 StringRef SectionName;
48
49 if (std::error_code E = SecRef.getName(SectionName))
50 return errorCodeToError(E);
51
52 if (SectionName == SecName)
53 return SecRef;
54 } else if (SecIndex == SectionIndex)
55 return SecRef;
56
57 SecIndex++;
58 }
59 return make_error<StringError>("invalid section reference",
60 object::object_error::parse_failed);
61}
62
63void ObjDumper::printSectionAsString(const object::ObjectFile *Obj,
64 StringRef SecName) {
65 Expected<object::SectionRef> SectionRefOrError =
66 getSecNameOrIndexAsSecRef(Obj, SecName);
67 if (!SectionRefOrError)
68 error(std::move(SectionRefOrError));
69 object::SectionRef Section = *SectionRefOrError;
70 StringRef SectionName;
71
72 if (std::error_code E = Section.getName(SectionName))
73 error(E);
74 W.startLine() << "String dump of section '" << SectionName << "':\n";
75
76 StringRef SectionContent;
77 Section.getContents(SectionContent);
78
79 const uint8_t *SecContent = SectionContent.bytes_begin();
80 const uint8_t *CurrentWord = SecContent;
81 const uint8_t *SecEnd = SectionContent.bytes_end();
82
83 while (CurrentWord <= SecEnd) {
84 size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
85 SecEnd - CurrentWord);
86 if (!WordSize) {
87 CurrentWord++;
88 continue;
89 }
90 W.startLine() << format("[%6tx] ", CurrentWord - SecContent);
91 printAsPrintable(W.startLine(), CurrentWord, WordSize);
92 W.startLine() << '\n';
93 CurrentWord += WordSize + 1;
94 }
95}
96
Paul Semelcac968e2018-07-25 10:04:37 +000097void ObjDumper::printSectionAsHex(const object::ObjectFile *Obj,
98 StringRef SecName) {
99 Expected<object::SectionRef> SectionRefOrError =
100 getSecNameOrIndexAsSecRef(Obj, SecName);
101 if (!SectionRefOrError)
102 error(std::move(SectionRefOrError));
103 object::SectionRef Section = *SectionRefOrError;
104 StringRef SectionName;
105
106 if (std::error_code E = Section.getName(SectionName))
107 error(E);
108 W.startLine() << "Hex dump of section '" << SectionName << "':\n";
109
110 StringRef SectionContent;
111 Section.getContents(SectionContent);
112 const uint8_t *SecContent = SectionContent.bytes_begin();
113 const uint8_t *SecEnd = SecContent + SectionContent.size();
Paul Semel94b09402018-07-11 10:00:29 +0000114
115 for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {
116 const uint8_t *TmpSecPtr = SecPtr;
117 uint8_t i;
118 uint8_t k;
119
120 W.startLine() << format_hex(SecPtr - SecContent, 10);
121 W.startLine() << ' ';
122 for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) {
123 for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) {
124 uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr));
125 W.startLine() << format_hex_no_prefix(Val, 2);
126 }
127 W.startLine() << ' ';
128 }
129
130 // We need to print the correct amount of spaces to match the format.
131 // We are adding the (4 - i) last rows that are 8 characters each.
132 // Then, the (4 - i) spaces that are in between the rows.
133 // Least, if we cut in a middle of a row, we add the remaining characters,
134 // which is (8 - (k * 2))
135 if (i < 4)
136 W.startLine() << format("%*c", (4 - i) * 8 + (4 - i) + (8 - (k * 2)),
137 ' ');
138
139 TmpSecPtr = SecPtr;
Paul Semelcac968e2018-07-25 10:04:37 +0000140 for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)
Michael Kruse6069e662018-07-26 15:31:41 +0000141 W.startLine() << (isPrint(TmpSecPtr[i]) ? static_cast<char>(TmpSecPtr[i])
Paul Semelcac968e2018-07-25 10:04:37 +0000142 : '.');
Paul Semel94b09402018-07-11 10:00:29 +0000143
144 W.startLine() << '\n';
145 }
146}
147
Eric Christopher76e70f32013-04-03 18:31:38 +0000148} // namespace llvm