clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ObjDumper.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn337657/build-llvm/tools/llvm-readobj -I /build/llvm-toolchain-snapshot-7~svn337657/tools/llvm-readobj -I /build/llvm-toolchain-snapshot-7~svn337657/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn337657/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/lib/gcc/x86_64-linux-gnu/8/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn337657/build-llvm/tools/llvm-readobj -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-07-23-043044-26795-1 -x c++ /build/llvm-toolchain-snapshot-7~svn337657/tools/llvm-readobj/ObjDumper.cpp -faddrsig
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
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 | long 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) { |
| 1 | Loop condition is true. Entering loop body | |
|
100 | const uint8_t *TmpSecPtr = SecPtr; |
101 | uint8_t i; |
102 | uint8_t k; |
| 2 | | 'k' declared without an initial value | |
|
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 | |
115 | |
116 | |
117 | |
118 | |
119 | if (i < 4) |
| |
120 | W.startLine() << format("%*c", (4 - i) * 8 + (4 - i) + (8 - (k * 2)), |
| 4 | | The left operand of '*' is a garbage value |
|
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 | } |