-
Notifications
You must be signed in to change notification settings - Fork 715
/
binary-reader-objdump.cc
2444 lines (2187 loc) · 78.8 KB
/
binary-reader-objdump.cc
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "wabt/binary-reader-objdump.h"
#include <algorithm>
#include <cassert>
#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <vector>
#if HAVE_STRCASECMP
#include <strings.h>
#endif
#include "wabt/binary-reader-nop.h"
#include "wabt/filenames.h"
#include "wabt/literal.h"
#include "wabt/string-util.h"
namespace wabt {
namespace {
class BinaryReaderObjdumpBase : public BinaryReaderNop {
public:
BinaryReaderObjdumpBase(const uint8_t* data,
size_t size,
ObjdumpOptions* options,
ObjdumpState* state);
bool OnError(const Error&) override;
Result BeginModule(uint32_t version) override;
Result BeginSection(Index section_index,
BinarySection section_type,
Offset size) override;
Result OnOpcode(Opcode Opcode) override;
Result OnRelocCount(Index count, Index section_index) override;
protected:
std::string_view GetTypeName(Index index) const;
std::string_view GetFunctionName(Index index) const;
std::string_view GetGlobalName(Index index) const;
std::string_view GetLocalName(Index function_index, Index local_index) const;
std::string_view GetSectionName(Index index) const;
std::string_view GetTagName(Index index) const;
std::string_view GetSymbolName(Index index) const;
std::string_view GetSegmentName(Index index) const;
std::string_view GetTableName(Index index) const;
void PrintRelocation(const Reloc& reloc, Offset offset) const;
Offset GetPrintOffset(Offset offset) const;
Offset GetSectionStart(BinarySection section_code) const {
return section_starts_[static_cast<size_t>(section_code)];
}
ObjdumpOptions* options_;
ObjdumpState* objdump_state_;
const uint8_t* data_;
size_t size_;
bool print_details_ = false;
BinarySection reloc_section_ = BinarySection::Invalid;
Offset section_starts_[kBinarySectionCount];
// Map of section index to section type
std::vector<BinarySection> section_types_;
bool section_found_ = false;
std::string module_name_;
Opcode current_opcode = Opcode::Unreachable;
std::unique_ptr<FileStream> err_stream_;
};
BinaryReaderObjdumpBase::BinaryReaderObjdumpBase(const uint8_t* data,
size_t size,
ObjdumpOptions* options,
ObjdumpState* objdump_state)
: options_(options),
objdump_state_(objdump_state),
data_(data),
size_(size),
err_stream_(FileStream::CreateStderr()) {
ZeroMemory(section_starts_);
}
Result BinaryReaderObjdumpBase::BeginSection(Index section_index,
BinarySection section_code,
Offset size) {
section_starts_[static_cast<size_t>(section_code)] = state->offset;
section_types_.push_back(section_code);
return Result::Ok;
}
bool BinaryReaderObjdumpBase::OnError(const Error&) {
// Tell the BinaryReader that this error is "handled" for all passes other
// than the prepass. When the error is handled the default message will be
// suppressed.
return options_->mode != ObjdumpMode::Prepass;
}
Result BinaryReaderObjdumpBase::BeginModule(uint32_t version) {
switch (options_->mode) {
case ObjdumpMode::Headers:
printf("\n");
printf("Sections:\n\n");
break;
case ObjdumpMode::Details:
printf("\n");
printf("Section Details:\n\n");
break;
case ObjdumpMode::Disassemble:
printf("\n");
printf("Code Disassembly:\n\n");
break;
case ObjdumpMode::Prepass: {
std::string_view basename = GetBasename(options_->filename);
if (basename == "-") {
basename = "<stdin>";
}
printf("%s:\tfile format wasm %#x\n", std::string(basename).c_str(),
version);
break;
}
case ObjdumpMode::RawData:
break;
}
return Result::Ok;
}
std::string_view BinaryReaderObjdumpBase::GetTypeName(Index index) const {
return objdump_state_->type_names.Get(index);
}
std::string_view BinaryReaderObjdumpBase::GetFunctionName(Index index) const {
return objdump_state_->function_names.Get(index);
}
std::string_view BinaryReaderObjdumpBase::GetGlobalName(Index index) const {
return objdump_state_->global_names.Get(index);
}
std::string_view BinaryReaderObjdumpBase::GetLocalName(
Index function_index,
Index local_index) const {
return objdump_state_->local_names.Get(function_index, local_index);
}
std::string_view BinaryReaderObjdumpBase::GetSectionName(Index index) const {
return objdump_state_->section_names.Get(index);
}
std::string_view BinaryReaderObjdumpBase::GetTagName(Index index) const {
return objdump_state_->tag_names.Get(index);
}
std::string_view BinaryReaderObjdumpBase::GetSegmentName(Index index) const {
return objdump_state_->segment_names.Get(index);
}
std::string_view BinaryReaderObjdumpBase::GetTableName(Index index) const {
return objdump_state_->table_names.Get(index);
}
std::string_view BinaryReaderObjdumpBase::GetSymbolName(
Index symbol_index) const {
if (symbol_index >= objdump_state_->symtab.size())
return "<illegal_symbol_index>";
ObjdumpSymbol& sym = objdump_state_->symtab[symbol_index];
switch (sym.kind) {
case SymbolType::Function:
return GetFunctionName(sym.index);
case SymbolType::Data:
return sym.name;
case SymbolType::Global:
return GetGlobalName(sym.index);
case SymbolType::Section:
return GetSectionName(sym.index);
case SymbolType::Tag:
return GetTagName(sym.index);
case SymbolType::Table:
return GetTableName(sym.index);
}
WABT_UNREACHABLE;
}
void BinaryReaderObjdumpBase::PrintRelocation(const Reloc& reloc,
Offset offset) const {
printf(" %06" PRIzx ": %-18s %" PRIindex, offset,
GetRelocTypeName(reloc.type), reloc.index);
if (reloc.addend) {
printf(" + %d", reloc.addend);
}
if (reloc.type != RelocType::TypeIndexLEB) {
printf(" <" PRIstringview ">",
WABT_PRINTF_STRING_VIEW_ARG(GetSymbolName(reloc.index)));
}
printf("\n");
}
Offset BinaryReaderObjdumpBase::GetPrintOffset(Offset offset) const {
return options_->section_offsets
? offset - GetSectionStart(BinarySection::Code)
: offset;
}
Result BinaryReaderObjdumpBase::OnOpcode(Opcode opcode) {
current_opcode = opcode;
return Result::Ok;
}
Result BinaryReaderObjdumpBase::OnRelocCount(Index count, Index section_index) {
if (section_index >= section_types_.size()) {
err_stream_->Writef("invalid relocation section index: %" PRIindex "\n",
section_index);
reloc_section_ = BinarySection::Invalid;
return Result::Error;
}
reloc_section_ = section_types_[section_index];
return Result::Ok;
}
class BinaryReaderObjdumpPrepass : public BinaryReaderObjdumpBase {
public:
using BinaryReaderObjdumpBase::BinaryReaderObjdumpBase;
Result BeginSection(Index section_index,
BinarySection section_code,
Offset size) override {
BinaryReaderObjdumpBase::BeginSection(section_index, section_code, size);
if (section_code != BinarySection::Custom) {
objdump_state_->section_names.Set(section_index,
wabt::GetSectionName(section_code));
}
return Result::Ok;
}
Result BeginCustomSection(Index section_index,
Offset size,
std::string_view section_name) override {
objdump_state_->section_names.Set(section_index, section_name);
return Result::Ok;
}
Result OnFunctionName(Index index, std::string_view name) override {
SetFunctionName(index, name);
return Result::Ok;
}
Result OnFuncType(Index index,
Index param_count,
Type* param_types,
Index result_count,
Type* result_types) override {
objdump_state_->function_param_counts[index] = param_count;
return Result::Ok;
}
Result OnNameEntry(NameSectionSubsection type,
Index index,
std::string_view name) override {
switch (type) {
// TODO(sbc): remove OnFunctionName in favor of just using
// OnNameEntry so that this works
/*
case NameSectionSubsection::Function:
SetFunctionName(index, name);
break;
*/
case NameSectionSubsection::Type:
SetTypeName(index, name);
break;
case NameSectionSubsection::Global:
SetGlobalName(index, name);
break;
case NameSectionSubsection::Table:
SetTableName(index, name);
break;
case NameSectionSubsection::DataSegment:
SetSegmentName(index, name);
break;
case NameSectionSubsection::Tag:
SetTagName(index, name);
break;
default:
break;
}
return Result::Ok;
}
Result OnLocalName(Index function_index,
Index local_index,
std::string_view local_name) override {
SetLocalName(function_index, local_index, local_name);
return Result::Ok;
}
Result OnSymbolCount(Index count) override {
objdump_state_->symtab.resize(count);
return Result::Ok;
}
Result OnDataSymbol(Index index,
uint32_t flags,
std::string_view name,
Index segment,
uint32_t offset,
uint32_t size) override {
objdump_state_->symtab[index] = {SymbolType::Data, std::string(name), 0};
return Result::Ok;
}
Result OnFunctionSymbol(Index index,
uint32_t flags,
std::string_view name,
Index func_index) override {
if (!name.empty()) {
SetFunctionName(func_index, name);
}
objdump_state_->symtab[index] = {SymbolType::Function, std::string(name),
func_index};
return Result::Ok;
}
Result OnGlobalSymbol(Index index,
uint32_t flags,
std::string_view name,
Index global_index) override {
if (!name.empty()) {
SetGlobalName(global_index, name);
}
objdump_state_->symtab[index] = {SymbolType::Global, std::string(name),
global_index};
return Result::Ok;
}
Result OnSectionSymbol(Index index,
uint32_t flags,
Index section_index) override {
objdump_state_->symtab[index] = {SymbolType::Section,
std::string(GetSectionName(section_index)),
section_index};
return Result::Ok;
}
Result OnTagSymbol(Index index,
uint32_t flags,
std::string_view name,
Index tag_index) override {
if (!name.empty()) {
SetTagName(tag_index, name);
}
objdump_state_->symtab[index] = {SymbolType::Tag, std::string(name),
tag_index};
return Result::Ok;
}
Result OnTableSymbol(Index index,
uint32_t flags,
std::string_view name,
Index table_index) override {
if (!name.empty()) {
SetTableName(table_index, name);
}
objdump_state_->symtab[index] = {SymbolType::Table, std::string(name),
table_index};
return Result::Ok;
}
Result OnImportFunc(Index import_index,
std::string_view module_name,
std::string_view field_name,
Index func_index,
Index sig_index) override {
SetFunctionName(func_index, module_name + "." + field_name);
return Result::Ok;
}
Result OnImportTag(Index import_index,
std::string_view module_name,
std::string_view field_name,
Index tag_index,
Index sig_index) override {
SetTagName(tag_index, module_name + "." + field_name);
return Result::Ok;
}
Result OnImportGlobal(Index import_index,
std::string_view module_name,
std::string_view field_name,
Index global_index,
Type type,
bool mutable_) override {
SetGlobalName(global_index, module_name + "." + field_name);
return Result::Ok;
}
Result OnImportTable(Index import_index,
std::string_view module_name,
std::string_view field_name,
Index table_index,
Type elem_type,
const Limits* elem_limits) override {
SetTableName(table_index, module_name + "." + field_name);
return Result::Ok;
}
Result OnExport(Index index,
ExternalKind kind,
Index item_index,
std::string_view name) override {
if (kind == ExternalKind::Func) {
SetFunctionName(item_index, name);
} else if (kind == ExternalKind::Global) {
SetGlobalName(item_index, name);
}
return Result::Ok;
}
Result OnReloc(RelocType type,
Offset offset,
Index index,
uint32_t addend) override;
Result OnModuleName(std::string_view name) override {
if (options_->mode == ObjdumpMode::Prepass) {
printf("module name: <" PRIstringview ">\n",
WABT_PRINTF_STRING_VIEW_ARG(name));
}
return Result::Ok;
}
Result OnSegmentInfo(Index index,
std::string_view name,
Address alignment_log2,
uint32_t flags) override {
SetSegmentName(index, name);
return Result::Ok;
}
protected:
void SetTypeName(Index index, std::string_view name);
void SetFunctionName(Index index, std::string_view name);
void SetGlobalName(Index index, std::string_view name);
void SetLocalName(Index function_index,
Index local_index,
std::string_view name);
void SetTagName(Index index, std::string_view name);
void SetTableName(Index index, std::string_view name);
void SetSegmentName(Index index, std::string_view name);
};
void BinaryReaderObjdumpPrepass::SetTypeName(Index index,
std::string_view name) {
objdump_state_->type_names.Set(index, name);
}
void BinaryReaderObjdumpPrepass::SetFunctionName(Index index,
std::string_view name) {
objdump_state_->function_names.Set(index, name);
}
void BinaryReaderObjdumpPrepass::SetGlobalName(Index index,
std::string_view name) {
objdump_state_->global_names.Set(index, name);
}
void BinaryReaderObjdumpPrepass::SetLocalName(Index function_index,
Index local_index,
std::string_view name) {
objdump_state_->local_names.Set(function_index, local_index, name);
}
void BinaryReaderObjdumpPrepass::SetTagName(Index index,
std::string_view name) {
objdump_state_->tag_names.Set(index, name);
}
void BinaryReaderObjdumpPrepass::SetTableName(Index index,
std::string_view name) {
objdump_state_->table_names.Set(index, name);
}
void BinaryReaderObjdumpPrepass::SetSegmentName(Index index,
std::string_view name) {
objdump_state_->segment_names.Set(index, name);
}
Result BinaryReaderObjdumpPrepass::OnReloc(RelocType type,
Offset offset,
Index index,
uint32_t addend) {
BinaryReaderObjdumpBase::OnReloc(type, offset, index, addend);
if (reloc_section_ == BinarySection::Code) {
objdump_state_->code_relocations.emplace_back(type, offset, index, addend);
} else if (reloc_section_ == BinarySection::Data) {
objdump_state_->data_relocations.emplace_back(type, offset, index, addend);
}
return Result::Ok;
}
class BinaryReaderObjdumpDisassemble : public BinaryReaderObjdumpBase {
public:
using BinaryReaderObjdumpBase::BinaryReaderObjdumpBase;
std::string BlockSigToString(Type type) const;
Result BeginFunctionBody(Index index, Offset size) override;
Result EndFunctionBody(Index index) override;
Result OnLocalDeclCount(Index count) override;
Result OnLocalDecl(Index decl_index, Index count, Type type) override;
Result OnOpcode(Opcode Opcode) override;
Result OnOpcodeBare() override;
Result OnOpcodeIndex(Index value) override;
Result OnOpcodeIndexIndex(Index value, Index value2) override;
Result OnOpcodeUint32(uint32_t value) override;
Result OnOpcodeUint32Uint32(uint32_t value, uint32_t value2) override;
Result OnCallIndirectExpr(uint32_t sig_indix, uint32_t table_index) override;
Result OnOpcodeUint32Uint32Uint32(uint32_t value,
uint32_t value2,
uint32_t value3) override;
Result OnOpcodeUint32Uint32Uint32Uint32(uint32_t value,
uint32_t value2,
uint32_t value3,
uint32_t value4) override;
Result OnOpcodeUint64(uint64_t value) override;
Result OnOpcodeF32(uint32_t value) override;
Result OnOpcodeF64(uint64_t value) override;
Result OnOpcodeV128(v128 value) override;
Result OnOpcodeBlockSig(Type sig_type) override;
Result OnOpcodeType(Type type) override;
Result OnBrTableExpr(Index num_targets,
Index* target_depths,
Index default_target_depth) override;
Result OnDelegateExpr(Index) override;
Result OnEndExpr() override;
private:
void LogOpcode(const char* fmt, ...);
Offset current_opcode_offset = 0;
Offset last_opcode_end = 0;
int indent_level = 0;
Index next_reloc = 0;
Index current_function_index = 0;
Index local_index_ = 0;
bool in_function_body = false;
bool skip_next_opcode_ = false;
};
std::string BinaryReaderObjdumpDisassemble::BlockSigToString(Type type) const {
if (type.IsIndex()) {
return StringPrintf("type[%d]", type.GetIndex());
} else if (type == Type::Void) {
return "";
} else {
return type.GetName();
}
}
Result BinaryReaderObjdumpDisassemble::OnOpcode(Opcode opcode) {
BinaryReaderObjdumpBase::OnOpcode(opcode);
if (!in_function_body) {
return Result::Ok;
}
if (options_->debug) {
const char* opcode_name = opcode.GetName();
err_stream_->Writef("on_opcode: %#" PRIzx ": %s\n", state->offset,
opcode_name);
}
if (last_opcode_end) {
// Takes care of cases where opcode's bytes was a non-canonical leb128
// encoding. In this case, opcode.GetLength() under-reports the length,
// since it canonicalizes the opcode.
if (state->offset < last_opcode_end + opcode.GetLength()) {
Opcode missing_opcode = Opcode::FromCode(data_[last_opcode_end]);
const char* opcode_name = missing_opcode.GetName();
fprintf(stderr,
"error: %#" PRIzx " missing opcode callback at %#" PRIzx
" (%#02x=%s)\n",
state->offset, last_opcode_end + 1, data_[last_opcode_end],
opcode_name);
return Result::Error;
}
}
current_opcode_offset = state->offset;
return Result::Ok;
}
#define IMMEDIATE_OCTET_COUNT 9
Result BinaryReaderObjdumpDisassemble::OnLocalDeclCount(Index count) {
if (!in_function_body) {
return Result::Ok;
}
current_opcode_offset = state->offset;
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnLocalDecl(Index decl_index,
Index count,
Type type) {
if (!in_function_body) {
return Result::Ok;
}
Offset offset = current_opcode_offset;
size_t data_size = state->offset - offset;
printf(" %06" PRIzx ":", GetPrintOffset(offset));
for (size_t i = 0; i < data_size && i < IMMEDIATE_OCTET_COUNT;
i++, offset++) {
printf(" %02x", data_[offset]);
}
for (size_t i = data_size; i < IMMEDIATE_OCTET_COUNT; i++) {
printf(" ");
}
printf(" | local[%" PRIindex, local_index_);
if (count != 1) {
printf("..%" PRIindex "", local_index_ + count - 1);
}
local_index_ += count;
printf("] type=%s\n", type.GetName().c_str());
last_opcode_end = current_opcode_offset + data_size;
current_opcode_offset = last_opcode_end;
return Result::Ok;
}
void BinaryReaderObjdumpDisassemble::LogOpcode(const char* fmt, ...) {
// BinaryReaderObjdumpDisassemble is only used to disassembly function bodies
// so this should never be called for instructions outside of function bodies
// (i.e. init expresions).
assert(in_function_body);
if (skip_next_opcode_) {
skip_next_opcode_ = false;
return;
}
const Offset immediate_len = state->offset - current_opcode_offset;
const Offset opcode_size = current_opcode.GetLength();
const Offset total_size = opcode_size + immediate_len;
// current_opcode_offset has already read past this opcode; rewind it by the
// size of this opcode, which may be more than one byte.
Offset offset = current_opcode_offset - opcode_size;
const Offset offset_end = offset + total_size;
bool first_line = true;
while (offset < offset_end) {
// Print bytes, but only display a maximum of IMMEDIATE_OCTET_COUNT on each
// line.
printf(" %06" PRIzx ":", GetPrintOffset(offset));
size_t i;
for (i = 0; offset < offset_end && i < IMMEDIATE_OCTET_COUNT;
++i, ++offset) {
printf(" %02x", data_[offset]);
}
// Fill the rest of the remaining space with spaces.
for (; i < IMMEDIATE_OCTET_COUNT; ++i) {
printf(" ");
}
printf(" | ");
if (first_line) {
first_line = false;
// Print disassembly.
int indent_level = this->indent_level;
switch (current_opcode) {
case Opcode::Else:
case Opcode::Catch:
case Opcode::CatchAll:
indent_level--;
break;
default:
break;
}
for (int j = 0; j < indent_level; j++) {
printf(" ");
}
const char* opcode_name = current_opcode.GetName();
printf("%s", opcode_name);
if (fmt) {
printf(" ");
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
}
printf("\n");
}
last_opcode_end = state->offset;
// Print relocation after then full (potentially multi-line) instruction.
if (options_->relocs &&
next_reloc < objdump_state_->code_relocations.size()) {
const Reloc& reloc = objdump_state_->code_relocations[next_reloc];
Offset code_start = GetSectionStart(BinarySection::Code);
Offset abs_offset = code_start + reloc.offset;
if (last_opcode_end > abs_offset) {
PrintRelocation(reloc, abs_offset);
next_reloc++;
}
}
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeBare() {
if (!in_function_body) {
return Result::Ok;
}
LogOpcode(0, nullptr);
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeIndex(Index value) {
if (!in_function_body) {
return Result::Ok;
}
std::string_view name;
if (current_opcode == Opcode::Call &&
!(name = GetFunctionName(value)).empty()) {
LogOpcode("%d <" PRIstringview ">", value,
WABT_PRINTF_STRING_VIEW_ARG(name));
} else if (current_opcode == Opcode::Throw &&
!(name = GetTagName(value)).empty()) {
LogOpcode("%d <" PRIstringview ">", value,
WABT_PRINTF_STRING_VIEW_ARG(name));
} else if ((current_opcode == Opcode::GlobalGet ||
current_opcode == Opcode::GlobalSet) &&
!(name = GetGlobalName(value)).empty()) {
LogOpcode("%d <" PRIstringview ">", value,
WABT_PRINTF_STRING_VIEW_ARG(name));
} else if ((current_opcode == Opcode::LocalGet ||
current_opcode == Opcode::LocalSet) &&
!(name = GetLocalName(current_function_index, value)).empty()) {
LogOpcode("%d <" PRIstringview ">", value,
WABT_PRINTF_STRING_VIEW_ARG(name));
} else {
LogOpcode("%d", value);
}
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeIndexIndex(Index value,
Index value2) {
if (!in_function_body) {
return Result::Ok;
}
LogOpcode("%" PRIindex " %" PRIindex, value, value2);
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeUint32(uint32_t value) {
if (!in_function_body) {
return Result::Ok;
}
std::string_view name;
if (current_opcode == Opcode::DataDrop &&
!(name = GetSegmentName(value)).empty()) {
LogOpcode("%d <" PRIstringview ">", value,
WABT_PRINTF_STRING_VIEW_ARG(name));
} else {
LogOpcode("%u", value);
}
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeUint32Uint32(uint32_t value,
uint32_t value2) {
if (!in_function_body)
return Result::Ok;
std::string_view name;
if (current_opcode == Opcode::MemoryInit &&
!(name = GetSegmentName(value)).empty()) {
LogOpcode("%u %u <" PRIstringview ">", value, value2,
WABT_PRINTF_STRING_VIEW_ARG(name));
} else {
LogOpcode("%u %u", value, value2);
}
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnCallIndirectExpr(
uint32_t sig_index,
uint32_t table_index) {
std::string_view table_name = GetTableName(table_index);
std::string_view type_name = GetTypeName(sig_index);
if (!type_name.empty() && !table_name.empty()) {
LogOpcode("%u <" PRIstringview "> (type %u <" PRIstringview ">)",
table_index, WABT_PRINTF_STRING_VIEW_ARG(table_name), sig_index,
WABT_PRINTF_STRING_VIEW_ARG(type_name));
} else if (!table_name.empty()) {
LogOpcode("%u <" PRIstringview "> (type %u)", table_index,
WABT_PRINTF_STRING_VIEW_ARG(table_name), sig_index);
} else if (!type_name.empty()) {
LogOpcode("%u (type %u <" PRIstringview ">)", table_index, sig_index,
WABT_PRINTF_STRING_VIEW_ARG(type_name));
} else {
LogOpcode("%u (type %u)", table_index, sig_index);
}
skip_next_opcode_ = true;
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeUint32Uint32Uint32(
uint32_t value,
uint32_t value2,
uint32_t value3) {
if (!in_function_body) {
return Result::Ok;
}
LogOpcode("%u %u %u", value, value2, value3);
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeUint32Uint32Uint32Uint32(
uint32_t value,
uint32_t value2,
uint32_t value3,
uint32_t value4) {
if (!in_function_body) {
return Result::Ok;
}
LogOpcode("%u %u %u %u", value, value2, value3, value4);
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeUint64(uint64_t value) {
if (!in_function_body) {
return Result::Ok;
}
LogOpcode("%" PRId64, value);
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeF32(uint32_t value) {
if (!in_function_body) {
return Result::Ok;
}
char buffer[WABT_MAX_FLOAT_HEX];
WriteFloatHex(buffer, sizeof(buffer), value);
LogOpcode(buffer);
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeF64(uint64_t value) {
if (!in_function_body) {
return Result::Ok;
}
char buffer[WABT_MAX_DOUBLE_HEX];
WriteDoubleHex(buffer, sizeof(buffer), value);
LogOpcode(buffer);
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeV128(v128 value) {
if (!in_function_body) {
return Result::Ok;
}
// v128 is always dumped as i32x4:
LogOpcode("0x%08x 0x%08x 0x%08x 0x%08x", value.u32(0), value.u32(1),
value.u32(2), value.u32(3));
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeType(Type type) {
if (!in_function_body) {
return Result::Ok;
}
if (current_opcode == Opcode::SelectT) {
LogOpcode(type.GetName().c_str());
} else {
LogOpcode(type.GetRefKindName());
}
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnBrTableExpr(
Index num_targets,
Index* target_depths,
Index default_target_depth) {
if (!in_function_body) {
return Result::Ok;
}
std::string buffer = std::string();
for (Index i = 0; i < num_targets; i++) {
buffer.append(std::to_string(target_depths[i])).append(" ");
}
buffer.append(std::to_string(default_target_depth));
LogOpcode("%s", buffer.c_str());
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnDelegateExpr(Index depth) {
if (!in_function_body) {
return Result::Ok;
}
// Because `delegate` ends the block we need to dedent here, and
// we don't need to dedent it in LogOpcode.
if (indent_level > 0) {
indent_level--;
}
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnEndExpr() {
if (!in_function_body) {
return Result::Ok;
}
if (indent_level > 0) {
indent_level--;
}
LogOpcode(0, nullptr);
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::BeginFunctionBody(Index index,
Offset size) {
printf("%06" PRIzx " func[%" PRIindex "]", GetPrintOffset(state->offset),
index);
auto name = GetFunctionName(index);
if (!name.empty()) {
printf(" <" PRIstringview ">", WABT_PRINTF_STRING_VIEW_ARG(name));
}
printf(":\n");
last_opcode_end = 0;
in_function_body = true;
current_function_index = index;
auto type_index = objdump_state_->function_types[index];
local_index_ = objdump_state_->function_param_counts[type_index];
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::EndFunctionBody(Index index) {
assert(in_function_body);
in_function_body = false;
return Result::Ok;
}
Result BinaryReaderObjdumpDisassemble::OnOpcodeBlockSig(Type sig_type) {
if (!in_function_body) {
return Result::Ok;
}
if (sig_type != Type::Void) {
LogOpcode("%s", BlockSigToString(sig_type).c_str());
} else {
LogOpcode(nullptr);
}
indent_level++;
return Result::Ok;
}
enum class InitExprType {
Invalid,
I32,
F32,
I64,
F64,
V128,
Global,
FuncRef,
// TODO: There isn't a nullref anymore, this just represents ref.null of some
// type T.
NullRef,
};
struct InitInst {
Opcode opcode;
union {
Index index;
uint32_t i32;
uint32_t f32;
uint64_t i64;