blob: f762ff8a844979116703e081233b171f432bd60a [file] [log] [blame]
Eric Christopher9cad53c2013-04-03 18:31:38 +00001//===-- ObjDumper.cpp - Base dumper class -----------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Eric Christopher9cad53c2013-04-03 18:31:38 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file implements ObjDumper.
Eric Christopher9cad53c2013-04-03 18:31:38 +000011///
12//===----------------------------------------------------------------------===//
13
14#include "ObjDumper.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000015#include "Error.h"
Paul Semel6e137902018-07-18 18:00:41 +000016#include "llvm-readobj.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000017#include "llvm/Object/ObjectFile.h"
Paul Semel6e137902018-07-18 18:00:41 +000018#include "llvm/Support/Error.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000019#include "llvm/Support/ScopedPrinter.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000020#include "llvm/Support/raw_ostream.h"
21
22namespace llvm {
23
Zachary Turner88bb1632016-05-03 00:28:04 +000024ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {}
Eric Christopher9cad53c2013-04-03 18:31:38 +000025
26ObjDumper::~ObjDumper() {
27}
28
Paul Semel6e137902018-07-18 18:00:41 +000029static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
30 for (size_t i = 0; i < Len; i++)
Michael Kruse6f1da6e2018-07-26 15:31:41 +000031 W << (isPrint(Start[i]) ? static_cast<char>(Start[i]) : '.');
Paul Semel6e137902018-07-18 18:00:41 +000032}
33
34static Expected<object::SectionRef>
35getSecNameOrIndexAsSecRef(const object::ObjectFile *Obj, StringRef SecName) {
36 char *StrPtr;
37 long SectionIndex = strtol(SecName.data(), &StrPtr, 10);
Paul Semel5ce8f152018-07-25 10:04:37 +000038 long SecIndex;
39 if (Obj->isELF())
40 SecIndex = 0;
41 else
42 SecIndex = 1;
Paul Semel6e137902018-07-18 18:00:41 +000043 for (object::SectionRef SecRef : Obj->sections()) {
44 if (*StrPtr) {
45 StringRef SectionName;
46
47 if (std::error_code E = SecRef.getName(SectionName))
48 return errorCodeToError(E);
49
50 if (SectionName == SecName)
51 return SecRef;
52 } else if (SecIndex == SectionIndex)
53 return SecRef;
54
55 SecIndex++;
56 }
57 return make_error<StringError>("invalid section reference",
58 object::object_error::parse_failed);
59}
60
61void ObjDumper::printSectionAsString(const object::ObjectFile *Obj,
62 StringRef SecName) {
63 Expected<object::SectionRef> SectionRefOrError =
64 getSecNameOrIndexAsSecRef(Obj, SecName);
65 if (!SectionRefOrError)
66 error(std::move(SectionRefOrError));
67 object::SectionRef Section = *SectionRefOrError;
68 StringRef SectionName;
69
70 if (std::error_code E = Section.getName(SectionName))
71 error(E);
72 W.startLine() << "String dump of section '" << SectionName << "':\n";
73
74 StringRef SectionContent;
75 Section.getContents(SectionContent);
76
77 const uint8_t *SecContent = SectionContent.bytes_begin();
78 const uint8_t *CurrentWord = SecContent;
79 const uint8_t *SecEnd = SectionContent.bytes_end();
80
81 while (CurrentWord <= SecEnd) {
82 size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord),
83 SecEnd - CurrentWord);
84 if (!WordSize) {
85 CurrentWord++;
86 continue;
87 }
88 W.startLine() << format("[%6tx] ", CurrentWord - SecContent);
89 printAsPrintable(W.startLine(), CurrentWord, WordSize);
90 W.startLine() << '\n';
91 CurrentWord += WordSize + 1;
92 }
93}
94
Paul Semel5ce8f152018-07-25 10:04:37 +000095void ObjDumper::printSectionAsHex(const object::ObjectFile *Obj,
96 StringRef SecName) {
97 Expected<object::SectionRef> SectionRefOrError =
98 getSecNameOrIndexAsSecRef(Obj, SecName);
99 if (!SectionRefOrError)
100 error(std::move(SectionRefOrError));
101 object::SectionRef Section = *SectionRefOrError;
102 StringRef SectionName;
103
104 if (std::error_code E = Section.getName(SectionName))
105 error(E);
106 W.startLine() << "Hex dump of section '" << SectionName << "':\n";
107
108 StringRef SectionContent;
109 Section.getContents(SectionContent);
110 const uint8_t *SecContent = SectionContent.bytes_begin();
111 const uint8_t *SecEnd = SecContent + SectionContent.size();
Paul Semelb98f5042018-07-11 10:00:29 +0000112
113 for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) {
114 const uint8_t *TmpSecPtr = SecPtr;
115 uint8_t i;
116 uint8_t k;
117
Sid Manning26238702019-01-16 16:17:19 +0000118 W.startLine() << format_hex(Section.getAddress() + (SecPtr - SecContent),
119 10);
Paul Semelb98f5042018-07-11 10:00:29 +0000120 W.startLine() << ' ';
121 for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) {
122 for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) {
123 uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr));
124 W.startLine() << format_hex_no_prefix(Val, 2);
125 }
126 W.startLine() << ' ';
127 }
128
129 // We need to print the correct amount of spaces to match the format.
130 // We are adding the (4 - i) last rows that are 8 characters each.
131 // Then, the (4 - i) spaces that are in between the rows.
132 // Least, if we cut in a middle of a row, we add the remaining characters,
133 // which is (8 - (k * 2))
134 if (i < 4)
135 W.startLine() << format("%*c", (4 - i) * 8 + (4 - i) + (8 - (k * 2)),
136 ' ');
137
138 TmpSecPtr = SecPtr;
Paul Semel5ce8f152018-07-25 10:04:37 +0000139 for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)
Michael Kruse6f1da6e2018-07-26 15:31:41 +0000140 W.startLine() << (isPrint(TmpSecPtr[i]) ? static_cast<char>(TmpSecPtr[i])
Paul Semel5ce8f152018-07-25 10:04:37 +0000141 : '.');
Paul Semelb98f5042018-07-11 10:00:29 +0000142
143 W.startLine() << '\n';
144 }
145}
146
Eric Christopher9cad53c2013-04-03 18:31:38 +0000147} // namespace llvm