1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/*
* Copyright 2016 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "src/common.h"
#include "src/option-parser.h"
#include "src/stream.h"
#include "src/binary-reader.h"
#include "src/binary-reader-objdump.h"
using namespace wabt;
static const char s_description[] =
R"( Print information about the contents of wasm binaries.
examples:
$ wasm-objdump test.wasm
)";
static ObjdumpOptions s_objdump_options;
static std::vector<const char*> s_infiles;
static std::unique_ptr<FileStream> s_log_stream;
static void ParseOptions(int argc, char** argv) {
OptionParser parser("wasm-objdump", s_description);
parser.AddOption('h', "headers", "Print headers",
[]() { s_objdump_options.headers = true; });
parser.AddOption(
'j', "section", "SECTION", "Select just one section",
[](const char* argument) { s_objdump_options.section_name = argument; });
parser.AddOption('s', "full-contents", "Print raw section contents",
[]() { s_objdump_options.raw = true; });
parser.AddOption('d', "disassemble", "Disassemble function bodies",
[]() { s_objdump_options.disassemble = true; });
parser.AddOption("debug", "Print extra debug information", []() {
s_objdump_options.debug = true;
s_log_stream = FileStream::CreateStderr();
s_objdump_options.log_stream = s_log_stream.get();
});
parser.AddOption('x', "details", "Show section details",
[]() { s_objdump_options.details = true; });
parser.AddOption('r', "reloc", "Show relocations inline with disassembly",
[]() { s_objdump_options.relocs = true; });
parser.AddOption(0, "section-offsets",
"Print section offsets instead of file offsets "
"in code disassembly",
[]() { s_objdump_options.section_offsets = true; });
parser.AddArgument(
"filename", OptionParser::ArgumentCount::OneOrMore,
[](const char* argument) { s_infiles.push_back(argument); });
parser.Parse(argc, argv);
}
Result dump_file(const char* filename) {
std::vector<uint8_t> file_data;
CHECK_RESULT(ReadFile(filename, &file_data));
uint8_t* data = file_data.data();
size_t size = file_data.size();
// Perform serveral passed over the binary in order to print out different
// types of information.
s_objdump_options.filename = filename;
printf("\n");
ObjdumpState state;
Result result = Result::Ok;
// Pass 0: Prepass
s_objdump_options.mode = ObjdumpMode::Prepass;
result |= ReadBinaryObjdump(data, size, &s_objdump_options, &state);
s_objdump_options.log_stream = nullptr;
// Pass 1: Print the section headers
if (s_objdump_options.headers) {
s_objdump_options.mode = ObjdumpMode::Headers;
result |= ReadBinaryObjdump(data, size, &s_objdump_options, &state);
}
// Pass 2: Print extra information based on section type
if (s_objdump_options.details) {
s_objdump_options.mode = ObjdumpMode::Details;
result |= ReadBinaryObjdump(data, size, &s_objdump_options, &state);
}
// Pass 3: Disassemble code section
if (s_objdump_options.disassemble) {
s_objdump_options.mode = ObjdumpMode::Disassemble;
result |= ReadBinaryObjdump(data, size, &s_objdump_options, &state);
}
// Pass 4: Dump to raw contents of the sections
if (s_objdump_options.raw) {
s_objdump_options.mode = ObjdumpMode::RawData;
result |= ReadBinaryObjdump(data, size, &s_objdump_options, &state);
}
return result;
}
int ProgramMain(int argc, char** argv) {
InitStdio();
ParseOptions(argc, argv);
if (!s_objdump_options.headers && !s_objdump_options.details &&
!s_objdump_options.disassemble && !s_objdump_options.raw) {
fprintf(stderr, "At least one of the following switches must be given:\n");
fprintf(stderr, " -d/--disassemble\n");
fprintf(stderr, " -h/--headers\n");
fprintf(stderr, " -x/--details\n");
fprintf(stderr, " -s/--full-contents\n");
return 1;
}
for (const char* filename: s_infiles) {
if (Failed(dump_file(filename))) {
return 1;
}
}
return 0;
}
int main(int argc, char** argv) {
WABT_TRY
return ProgramMain(argc, argv);
WABT_CATCH_BAD_ALLOC_AND_EXIT
}
|