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