1 //===-- 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 11 /// This file implements ObjDumper. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "ObjDumper.h" 16 #include "Error.h" 17 #include "llvm-readobj.h" 18 #include "llvm/Object/ObjectFile.h" 19 #include "llvm/Support/Error.h" 20 #include "llvm/Support/ScopedPrinter.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 namespace llvm { 24 25 ObjDumper::ObjDumper(ScopedPrinter &Writer) : W(Writer) {} 26 27 ObjDumper::~ObjDumper() { 28 } 29 30 static 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 35 static Expected<object::SectionRef> 36 getSecNameOrIndexAsSecRef(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 59 void 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 93 void 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 135 } // namespace llvm 136