1 //===-- ObjDumper.cpp - Base dumper class -----------------------*- C++ -*-===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file implements ObjDumper. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "ObjDumper.h" 15 #include "Error.h" 16 #include "llvm-readobj.h" 17 #include "llvm/Object/ObjectFile.h" 18 #include "llvm/Support/Error.h" 19 #include "llvm/Support/FormatVariadic.h" 20 #include "llvm/Support/ScopedPrinter.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include <map> 23 24 namespace llvm { 25 26 ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {} 27 28 ObjDumper::~ObjDumper() { 29 } 30 31 static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) { 32 for (size_t i = 0; i < Len; i++) 33 W << (isPrint(Start[i]) ? static_cast<char>(Start[i]) : '.'); 34 } 35 36 static std::vector<object::SectionRef> 37 getSectionRefsByNameOrIndex(const object::ObjectFile *Obj, 38 ArrayRef<std::string> Sections) { 39 std::vector<object::SectionRef> Ret; 40 std::map<std::string, bool> SecNames; 41 std::map<unsigned, bool> SecIndices; 42 unsigned SecIndex; 43 for (StringRef Section : Sections) { 44 if (!Section.getAsInteger(0, SecIndex)) 45 SecIndices.emplace(SecIndex, false); 46 else 47 SecNames.emplace(Section, false); 48 } 49 50 SecIndex = Obj->isELF() ? 0 : 1; 51 for (object::SectionRef SecRef : Obj->sections()) { 52 StringRef SecName; 53 error(SecRef.getName(SecName)); 54 auto NameIt = SecNames.find(SecName); 55 if (NameIt != SecNames.end()) 56 NameIt->second = true; 57 auto IndexIt = SecIndices.find(SecIndex); 58 if (IndexIt != SecIndices.end()) 59 IndexIt->second = true; 60 if (NameIt != SecNames.end() || IndexIt != SecIndices.end()) 61 Ret.push_back(SecRef); 62 SecIndex++; 63 } 64 65 for (const std::pair<std::string, bool> &S : SecNames) 66 if (!S.second) 67 reportWarning(formatv("could not find section '{0}'", S.first).str()); 68 for (std::pair<unsigned, bool> S : SecIndices) 69 if (!S.second) 70 reportWarning(formatv("could not find section {0}", S.first).str()); 71 72 return Ret; 73 } 74 75 void ObjDumper::printSectionsAsString(const object::ObjectFile *Obj, 76 ArrayRef<std::string> Sections) { 77 bool First = true; 78 for (object::SectionRef Section : 79 getSectionRefsByNameOrIndex(Obj, Sections)) { 80 StringRef SectionName; 81 error(Section.getName(SectionName)); 82 if (!First) 83 W.startLine() << '\n'; 84 First = false; 85 W.startLine() << "String dump of section '" << SectionName << "':\n"; 86 87 StringRef SectionContent = unwrapOrError(Section.getContents()); 88 89 const uint8_t *SecContent = SectionContent.bytes_begin(); 90 const uint8_t *CurrentWord = SecContent; 91 const uint8_t *SecEnd = SectionContent.bytes_end(); 92 93 while (CurrentWord <= SecEnd) { 94 size_t WordSize = strnlen(reinterpret_cast<const char *>(CurrentWord), 95 SecEnd - CurrentWord); 96 if (!WordSize) { 97 CurrentWord++; 98 continue; 99 } 100 W.startLine() << format("[%6tx] ", CurrentWord - SecContent); 101 printAsPrintable(W.startLine(), CurrentWord, WordSize); 102 W.startLine() << '\n'; 103 CurrentWord += WordSize + 1; 104 } 105 } 106 } 107 108 void ObjDumper::printSectionsAsHex(const object::ObjectFile *Obj, 109 ArrayRef<std::string> Sections) { 110 bool First = true; 111 for (object::SectionRef Section : 112 getSectionRefsByNameOrIndex(Obj, Sections)) { 113 StringRef SectionName; 114 error(Section.getName(SectionName)); 115 if (!First) 116 W.startLine() << '\n'; 117 First = false; 118 W.startLine() << "Hex dump of section '" << SectionName << "':\n"; 119 120 StringRef SectionContent = unwrapOrError(Section.getContents()); 121 const uint8_t *SecContent = SectionContent.bytes_begin(); 122 const uint8_t *SecEnd = SecContent + SectionContent.size(); 123 124 for (const uint8_t *SecPtr = SecContent; SecPtr < SecEnd; SecPtr += 16) { 125 const uint8_t *TmpSecPtr = SecPtr; 126 uint8_t i; 127 uint8_t k; 128 129 W.startLine() << format_hex(Section.getAddress() + (SecPtr - SecContent), 130 10); 131 W.startLine() << ' '; 132 for (i = 0; TmpSecPtr < SecEnd && i < 4; ++i) { 133 for (k = 0; TmpSecPtr < SecEnd && k < 4; k++, TmpSecPtr++) { 134 uint8_t Val = *(reinterpret_cast<const uint8_t *>(TmpSecPtr)); 135 W.startLine() << format_hex_no_prefix(Val, 2); 136 } 137 W.startLine() << ' '; 138 } 139 140 // We need to print the correct amount of spaces to match the format. 141 // We are adding the (4 - i) last rows that are 8 characters each. 142 // Then, the (4 - i) spaces that are in between the rows. 143 // Least, if we cut in a middle of a row, we add the remaining characters, 144 // which is (8 - (k * 2)). 145 if (i < 4) 146 W.startLine() << format("%*c", (4 - i) * 8 + (4 - i) + (8 - (k * 2)), 147 ' '); 148 149 TmpSecPtr = SecPtr; 150 for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) 151 W.startLine() << (isPrint(TmpSecPtr[i]) 152 ? static_cast<char>(TmpSecPtr[i]) 153 : '.'); 154 155 W.startLine() << '\n'; 156 } 157 } 158 } 159 160 } // namespace llvm 161