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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
|
/* Disassembler for x86.
Copyright (C) 2007, 2008 Red Hat, Inc.
This file is part of Red Hat elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 2007.
Red Hat elfutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation; version 2 of the License.
Red Hat elfutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with Red Hat elfutils; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Red Hat elfutils is an included package of the Open Invention Network.
An included package of the Open Invention Network is a package for which
Open Invention Network licensees cross-license their patents. No patent
license is granted, either expressly or impliedly, by designation as an
included package. Should you wish to participate in the Open Invention
Network licensing program, please visit www.openinventionnetwork.com
<http://www.openinventionnetwork.com>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <assert.h>
#include <config.h>
#include <ctype.h>
#include <endian.h>
#include <errno.h>
#include <gelf.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include "../libebl/libeblP.h"
#define MACHINE_ENCODING __LITTLE_ENDIAN
#include "memory-access.h"
#ifndef MNEFILE
# define MNEFILE "i386.mnemonics"
#endif
#define MNESTRFIELD(line) MNESTRFIELD1 (line)
#define MNESTRFIELD1(line) str##line
static const union mnestr_t
{
struct
{
#define MNE(name) char MNESTRFIELD (__LINE__)[sizeof (#name)];
#include MNEFILE
#undef MNE
};
char str[0];
} mnestr =
{
{
#define MNE(name) #name,
#include MNEFILE
#undef MNE
}
};
/* The index can be stored in the instrtab. */
enum
{
#define MNE(name) MNE_##name,
#include MNEFILE
#undef MNE
MNE_INVALID
};
static const unsigned short int mneidx[] =
{
#define MNE(name) \
[MNE_##name] = offsetof (union mnestr_t, MNESTRFIELD (__LINE__)),
#include MNEFILE
#undef MNE
};
enum
{
idx_rex_b = 0,
idx_rex_x,
idx_rex_r,
idx_rex_w,
idx_rex,
idx_cs,
idx_ds,
idx_es,
idx_fs,
idx_gs,
idx_ss,
idx_data16,
idx_addr16,
idx_rep,
idx_repne,
idx_lock
};
enum
{
#define prefbit(pref) has_##pref = 1 << idx_##pref
prefbit (rex_b),
prefbit (rex_x),
prefbit (rex_r),
prefbit (rex_w),
prefbit (rex),
prefbit (cs),
prefbit (ds),
prefbit (es),
prefbit (fs),
prefbit (gs),
prefbit (ss),
prefbit (data16),
prefbit (addr16),
prefbit (rep),
prefbit (repne),
prefbit (lock)
#undef prefbit
};
#define SEGMENT_PREFIXES \
(has_cs | has_ds | has_es | has_fs | has_gs | has_ss)
#define prefix_cs 0x2e
#define prefix_ds 0x3e
#define prefix_es 0x26
#define prefix_fs 0x64
#define prefix_gs 0x65
#define prefix_ss 0x36
#define prefix_data16 0x66
#define prefix_addr16 0x67
#define prefix_rep 0xf3
#define prefix_repne 0xf2
#define prefix_lock 0xf0
static const uint8_t known_prefixes[] =
{
#define newpref(pref) [idx_##pref] = prefix_##pref
newpref (cs),
newpref (ds),
newpref (es),
newpref (fs),
newpref (gs),
newpref (ss),
newpref (data16),
newpref (addr16),
newpref (rep),
newpref (repne),
newpref (lock)
#undef newpref
};
#define nknown_prefixes (sizeof (known_prefixes) / sizeof (known_prefixes[0]))
#if 0
static const char *prefix_str[] =
{
#define newpref(pref) [idx_##pref] = #pref
newpref (cs),
newpref (ds),
newpref (es),
newpref (fs),
newpref (gs),
newpref (ss),
newpref (data16),
newpref (addr16),
newpref (rep),
newpref (repne),
newpref (lock)
#undef newpref
};
#endif
static const char amd3dnowstr[] =
#define MNE_3DNOW_PAVGUSB 1
"pavgusb\0"
#define MNE_3DNOW_PFADD (MNE_3DNOW_PAVGUSB + 8)
"pfadd\0"
#define MNE_3DNOW_PFSUB (MNE_3DNOW_PFADD + 6)
"pfsub\0"
#define MNE_3DNOW_PFSUBR (MNE_3DNOW_PFSUB + 6)
"pfsubr\0"
#define MNE_3DNOW_PFACC (MNE_3DNOW_PFSUBR + 7)
"pfacc\0"
#define MNE_3DNOW_PFCMPGE (MNE_3DNOW_PFACC + 6)
"pfcmpge\0"
#define MNE_3DNOW_PFCMPGT (MNE_3DNOW_PFCMPGE + 8)
"pfcmpgt\0"
#define MNE_3DNOW_PFCMPEQ (MNE_3DNOW_PFCMPGT + 8)
"pfcmpeq\0"
#define MNE_3DNOW_PFMIN (MNE_3DNOW_PFCMPEQ + 8)
"pfmin\0"
#define MNE_3DNOW_PFMAX (MNE_3DNOW_PFMIN + 6)
"pfmax\0"
#define MNE_3DNOW_PI2FD (MNE_3DNOW_PFMAX + 6)
"pi2fd\0"
#define MNE_3DNOW_PF2ID (MNE_3DNOW_PI2FD + 6)
"pf2id\0"
#define MNE_3DNOW_PFRCP (MNE_3DNOW_PF2ID + 6)
"pfrcp\0"
#define MNE_3DNOW_PFRSQRT (MNE_3DNOW_PFRCP + 6)
"pfrsqrt\0"
#define MNE_3DNOW_PFMUL (MNE_3DNOW_PFRSQRT + 8)
"pfmul\0"
#define MNE_3DNOW_PFRCPIT1 (MNE_3DNOW_PFMUL + 6)
"pfrcpit1\0"
#define MNE_3DNOW_PFRSQIT1 (MNE_3DNOW_PFRCPIT1 + 9)
"pfrsqit1\0"
#define MNE_3DNOW_PFRCPIT2 (MNE_3DNOW_PFRSQIT1 + 9)
"pfrcpit2\0"
#define MNE_3DNOW_PMULHRW (MNE_3DNOW_PFRCPIT2 + 9)
"pmulhrw";
#define AMD3DNOW_LOW_IDX 0x0d
#define AMD3DNOW_HIGH_IDX (sizeof (amd3dnow) + AMD3DNOW_LOW_IDX - 1)
#define AMD3DNOW_IDX(val) ((val) - AMD3DNOW_LOW_IDX)
static const unsigned char amd3dnow[] =
{
[AMD3DNOW_IDX (0xbf)] = MNE_3DNOW_PAVGUSB,
[AMD3DNOW_IDX (0x9e)] = MNE_3DNOW_PFADD,
[AMD3DNOW_IDX (0x9a)] = MNE_3DNOW_PFSUB,
[AMD3DNOW_IDX (0xaa)] = MNE_3DNOW_PFSUBR,
[AMD3DNOW_IDX (0xae)] = MNE_3DNOW_PFACC,
[AMD3DNOW_IDX (0x90)] = MNE_3DNOW_PFCMPGE,
[AMD3DNOW_IDX (0xa0)] = MNE_3DNOW_PFCMPGT,
[AMD3DNOW_IDX (0xb0)] = MNE_3DNOW_PFCMPEQ,
[AMD3DNOW_IDX (0x94)] = MNE_3DNOW_PFMIN,
[AMD3DNOW_IDX (0xa4)] = MNE_3DNOW_PFMAX,
[AMD3DNOW_IDX (0x0d)] = MNE_3DNOW_PI2FD,
[AMD3DNOW_IDX (0x1d)] = MNE_3DNOW_PF2ID,
[AMD3DNOW_IDX (0x96)] = MNE_3DNOW_PFRCP,
[AMD3DNOW_IDX (0x97)] = MNE_3DNOW_PFRSQRT,
[AMD3DNOW_IDX (0xb4)] = MNE_3DNOW_PFMUL,
[AMD3DNOW_IDX (0xa6)] = MNE_3DNOW_PFRCPIT1,
[AMD3DNOW_IDX (0xa7)] = MNE_3DNOW_PFRSQIT1,
[AMD3DNOW_IDX (0xb6)] = MNE_3DNOW_PFRCPIT2,
[AMD3DNOW_IDX (0xb7)] = MNE_3DNOW_PMULHRW
};
struct output_data
{
GElf_Addr addr;
int *prefixes;
size_t opoff1;
size_t opoff2;
size_t opoff3;
char *bufp;
size_t *bufcntp;
size_t bufsize;
const uint8_t *data;
const uint8_t **param_start;
const uint8_t *end;
char *labelbuf;
size_t labelbufsize;
enum
{
addr_none = 0,
addr_abs_symbolic,
addr_abs_always,
addr_rel_symbolic,
addr_rel_always
} symaddr_use;
GElf_Addr symaddr;
};
#ifndef DISFILE
# define DISFILE "i386_dis.h"
#endif
#include DISFILE
#define ADD_CHAR(ch) \
do { \
if (unlikely (bufcnt == bufsize)) \
goto enomem; \
buf[bufcnt++] = (ch); \
} while (0)
#define ADD_STRING(str) \
do { \
const char *_str = (str); \
size_t _len = strlen (_str); \
if (unlikely (bufcnt + _len > bufsize)) \
goto enomem; \
memcpy (buf + bufcnt, str, _len); \
bufcnt += _len; \
} while (0)
int
i386_disasm (const uint8_t **startp, const uint8_t *end, GElf_Addr addr,
const char *fmt, DisasmOutputCB_t outcb, DisasmGetSymCB_t symcb,
void *outcbarg, void *symcbarg)
{
const char *save_fmt = fmt;
#define BUFSIZE 512
char initbuf[BUFSIZE];
int prefixes;
size_t bufcnt;
size_t bufsize = BUFSIZE;
char *buf = initbuf;
const uint8_t *param_start;
struct output_data output_data =
{
.prefixes = &prefixes,
.bufp = buf,
.bufsize = bufsize,
.bufcntp = &bufcnt,
.param_start = ¶m_start,
.end = end
};
int retval = 0;
while (1)
{
prefixes = 0;
const uint8_t *data = *startp;
const uint8_t *begin = data;
/* Recognize all prefixes. */
int last_prefix_bit = 0;
while (data < end)
{
unsigned int i;
for (i = idx_cs; i < nknown_prefixes; ++i)
if (known_prefixes[i] == *data)
break;
if (i == nknown_prefixes)
break;
prefixes |= last_prefix_bit = 1 << i;
++data;
}
#ifdef X86_64
if (data < end && (*data & 0xf0) == 0x40)
prefixes |= ((*data++) & 0xf) | has_rex;
#endif
bufcnt = 0;
size_t cnt = 0;
const uint8_t *curr = match_data;
const uint8_t *const match_end = match_data + sizeof (match_data);
assert (data <= end);
if (data == end)
{
if (prefixes != 0)
goto print_prefix;
retval = -1;
goto do_ret;
}
next_match:
while (curr < match_end)
{
uint_fast8_t len = *curr++;
const uint8_t *start = curr;
assert (len > 0);
assert (curr + 2 * len <= match_end);
const uint8_t *codep = data;
int correct_prefix = 0;
int opoff = 0;
if (data > begin && codep[-1] == curr[1] && curr[0] == 0xff)
{
/* We match a prefix byte. This is exactly one byte and
is matched exactly, without a mask. */
--len;
start += 2;
opoff = 8;
curr += 2;
assert (last_prefix_bit != 0);
correct_prefix = last_prefix_bit;
}
size_t avail = len;
while (avail > 0)
{
uint_fast8_t masked = *codep++ & *curr++;
if (masked != *curr++)
{
not:
curr = start + 2 * len;
++cnt;
goto next_match;
}
--avail;
if (codep == end && avail > 0)
goto do_ret;
}
if (len > end - data)
/* There is not enough data for the entire instruction. The
caller can figure this out by looking at the pointer into
the input data. */
goto do_ret;
assert (correct_prefix == 0
|| (prefixes & correct_prefix) != 0);
prefixes ^= correct_prefix;
if (0)
{
/* Resize the buffer. */
char *oldbuf;
enomem:
oldbuf = buf;
if (buf == initbuf)
buf = malloc (2 * bufsize);
else
buf = realloc (buf, 2 * bufsize);
if (buf == NULL)
{
buf = oldbuf;
retval = ENOMEM;
goto do_ret;
}
bufsize *= 2;
output_data.bufp = buf;
output_data.bufsize = bufsize;
bufcnt = 0;
if (data == end)
{
assert (prefixes != 0);
goto print_prefix;
}
/* gcc is not clever enough to see the following variables
are not used uninitialized. */
asm (""
: "=mr" (opoff), "=mr" (correct_prefix), "=mr" (codep),
"=mr" (start), "=mr" (len));
}
size_t prefix_size = 0;
// XXXonly print as prefix if valid?
if ((prefixes & has_lock) != 0)
{
ADD_STRING ("lock ");
prefix_size += 5;
}
if (instrtab[cnt].rep)
{
if ((prefixes & has_rep) != 0)
{
ADD_STRING ("rep ");
prefix_size += 4;
}
}
else if (instrtab[cnt].repe
&& (prefixes & (has_rep | has_repne)) != 0)
{
if ((prefixes & has_repne) != 0)
{
ADD_STRING ("repne ");
prefix_size += 6;
}
else if ((prefixes & has_rep) != 0)
{
ADD_STRING ("repe ");
prefix_size += 5;
}
}
else if ((prefixes & (has_rep | has_repne)) != 0)
{
uint_fast8_t byte;
print_prefix:
bufcnt = 0;
byte = *begin;
/* This is a prefix byte. Print it. */
switch (byte)
{
case prefix_rep:
ADD_STRING ("rep");
break;
case prefix_repne:
ADD_STRING ("repne");
break;
case prefix_cs:
ADD_STRING ("cs");
break;
case prefix_ds:
ADD_STRING ("ds");
break;
case prefix_es:
ADD_STRING ("es");
break;
case prefix_fs:
ADD_STRING ("fs");
break;
case prefix_gs:
ADD_STRING ("gs");
break;
case prefix_ss:
ADD_STRING ("ss");
break;
case prefix_data16:
ADD_STRING ("data16");
break;
case prefix_addr16:
ADD_STRING ("addr16");
break;
case prefix_lock:
ADD_STRING ("lock");
break;
#ifdef X86_64
case 0x40 ... 0x4f:
ADD_STRING ("rex");
if (byte != 0x40)
{
ADD_CHAR ('.');
if (byte & 0x8)
ADD_CHAR ('w');
if (byte & 0x4)
ADD_CHAR ('r');
if (byte & 0x3)
ADD_CHAR ('x');
if (byte & 0x1)
ADD_CHAR ('b');
}
break;
#endif
default:
/* Cannot happen. */
puts ("unknown prefix");
abort ();
}
data = begin + 1;
++addr;
goto out;
}
/* We have a match. First determine how many bytes are
needed for the adressing mode. */
param_start = codep;
if (instrtab[cnt].modrm)
{
uint_fast8_t modrm = codep[-1];
#ifndef X86_64
if (likely ((prefixes & has_addr16) != 0))
{
/* Account for displacement. */
if ((modrm & 0xc7) == 6 || (modrm & 0xc0) == 0x80)
param_start += 2;
else if ((modrm & 0xc0) == 0x40)
param_start += 1;
}
else
#endif
{
/* Account for SIB. */
if ((modrm & 0xc0) != 0xc0 && (modrm & 0x7) == 0x4)
param_start += 1;
/* Account for displacement. */
if ((modrm & 0xc7) == 5 || (modrm & 0xc0) == 0x80
|| ((modrm & 0xc7) == 0x4 && (codep[0] & 0x7) == 0x5))
param_start += 4;
else if ((modrm & 0xc0) == 0x40)
param_start += 1;
}
if (unlikely (param_start > end))
goto not;
}
output_data.addr = addr + (data - begin);
output_data.data = data;
unsigned long string_end_idx = 0;
fmt = save_fmt;
while (*fmt != '\0')
{
if (*fmt != '%')
{
char ch = *fmt++;
if (ch == '\\')
{
switch ((ch = *fmt++))
{
case '0' ... '7':
{
int val = ch - '0';
ch = *fmt;
if (ch >= '0' && ch <= '7')
{
val *= 8;
val += ch - '0';
ch = *++fmt;
if (ch >= '0' && ch <= '7' && val < 32)
{
val *= 8;
val += ch - '0';
++fmt;
}
}
ch = val;
}
break;
case 'n':
ch = '\n';
break;
case 't':
ch = '\t';
break;
default:
retval = EINVAL;
goto do_ret;
}
}
ADD_CHAR (ch);
continue;
}
++fmt;
int width = 0;
while (isdigit (*fmt))
width = width * 10 + (*fmt++ - '0');
int prec = 0;
if (*fmt == '.')
while (isdigit (*++fmt))
prec = prec * 10 + (*fmt - '0');
size_t start_idx = bufcnt;
switch (*fmt++)
{
char mnebuf[16];
const char *str;
case 'm':
/* Mnemonic. */
if (unlikely (instrtab[cnt].mnemonic == MNE_INVALID))
{
switch (*data)
{
#ifdef X86_64
case 0x90:
if (prefixes & has_rex_b)
goto not;
str = "nop";
break;
#endif
case 0x98:
#ifdef X86_64
if (prefixes == (has_rex_w | has_rex))
{
str = "cltq";
break;
}
#endif
if (prefixes & ~has_data16)
goto print_prefix;
str = prefixes & has_data16 ? "cbtw" : "cwtl";
break;
case 0x99:
#ifdef X86_64
if (prefixes == (has_rex_w | has_rex))
{
str = "cqto";
break;
}
#endif
if (prefixes & ~has_data16)
goto print_prefix;
str = prefixes & has_data16 ? "cwtd" : "cltd";
break;
case 0xe3:
if (prefixes & ~has_addr16)
goto print_prefix;
#ifdef X86_64
str = prefixes & has_addr16 ? "jecxz" : "jrcxz";
#else
str = prefixes & has_addr16 ? "jcxz" : "jecxz";
#endif
break;
case 0x0f:
if (data[1] == 0x0f)
{
/* AMD 3DNOW. We need one more byte. */
if (param_start >= end)
goto not;
if (*param_start < AMD3DNOW_LOW_IDX
|| *param_start > AMD3DNOW_HIGH_IDX)
goto not;
unsigned int idx
= amd3dnow[AMD3DNOW_IDX (*param_start)];
if (idx == 0)
goto not;
str = amd3dnowstr + idx - 1;
/* Eat the immediate byte indicating the
operation. */
++param_start;
break;
}
#ifdef X86_64
if (data[1] == 0xc7)
{
str = ((prefixes & has_rex_w)
? "cmpxchg16b" : "cmpxchg8b");
break;
}
#endif
if (data[1] == 0xc2)
{
if (param_start >= end)
goto not;
if (*param_start > 7)
goto not;
static const char cmpops[][9] =
{
[0] = "cmpeq",
[1] = "cmplt",
[2] = "cmple",
[3] = "cmpunord",
[4] = "cmpneq",
[5] = "cmpnlt",
[6] = "cmpnle",
[7] = "cmpord"
};
char *cp = stpcpy (mnebuf, cmpops[*param_start]);
if (correct_prefix & (has_rep | has_repne))
*cp++ = 's';
else
*cp++ = 'p';
if (correct_prefix & (has_data16 | has_repne))
*cp++ = 'd';
else
*cp++ = 's';
*cp = '\0';
str = mnebuf;
/* Eat the immediate byte indicating the
operation. */
++param_start;
break;
}
default:
assert (! "INVALID not handled");
}
}
else
str = mnestr.str + mneidx[instrtab[cnt].mnemonic];
ADD_STRING (str);
switch (instrtab[cnt].suffix)
{
case suffix_none:
break;
case suffix_w:
if ((codep[-1] & 0xc0) != 0xc0)
{
char ch;
if (data[0] & 1)
{
if (prefixes & has_data16)
ch = 'w';
#ifdef X86_64
else if (prefixes & has_rex_w)
ch = 'q';
#endif
else
ch = 'l';
}
else
ch = 'b';
ADD_CHAR (ch);
}
break;
case suffix_w0:
if ((codep[-1] & 0xc0) != 0xc0)
ADD_CHAR ('l');
break;
case suffix_w1:
if ((data[0] & 0x4) == 0)
ADD_CHAR ('l');
break;
case suffix_W:
if (prefixes & has_data16)
{
ADD_CHAR ('w');
prefixes &= ~has_data16;
}
#ifdef X86_64
else
ADD_CHAR ('q');
#endif
break;
case suffix_W1:
if (prefixes & has_data16)
{
ADD_CHAR ('w');
prefixes &= ~has_data16;
}
#ifdef X86_64
else if (prefixes & has_rex_w)
ADD_CHAR ('q');
#endif
break;
case suffix_tttn:;
static const char tttn[16][3] =
{
"o", "no", "b", "ae", "e", "ne", "be", "a",
"s", "ns", "p", "np", "l", "ge", "le", "g"
};
ADD_STRING (tttn[codep[-1 - instrtab[cnt].modrm] & 0x0f]);
break;
case suffix_D:
if ((codep[-1] & 0xc0) != 0xc0)
ADD_CHAR ((data[0] & 0x04) == 0 ? 's' : 'l');
break;
default:
printf("unknown suffix %d\n", instrtab[cnt].suffix);
abort ();
}
string_end_idx = bufcnt;
break;
case 'o':
if (prec == 1 && instrtab[cnt].fct1 != 0)
{
/* First parameter. */
if (instrtab[cnt].str1 != 0)
ADD_STRING (op1_str
+ op1_str_idx[instrtab[cnt].str1 - 1]);
output_data.opoff1 = (instrtab[cnt].off1_1
+ OFF1_1_BIAS - opoff);
output_data.opoff2 = (instrtab[cnt].off1_2
+ OFF1_2_BIAS - opoff);
output_data.opoff3 = (instrtab[cnt].off1_3
+ OFF1_3_BIAS - opoff);
int r = op1_fct[instrtab[cnt].fct1] (&output_data);
if (r < 0)
goto not;
if (r > 0)
goto enomem;
string_end_idx = bufcnt;
}
else if (prec == 2 && instrtab[cnt].fct2 != 0)
{
/* Second parameter. */
if (instrtab[cnt].str2 != 0)
ADD_STRING (op2_str
+ op2_str_idx[instrtab[cnt].str2 - 1]);
output_data.opoff1 = (instrtab[cnt].off2_1
+ OFF2_1_BIAS - opoff);
output_data.opoff2 = (instrtab[cnt].off2_2
+ OFF2_2_BIAS - opoff);
output_data.opoff3 = (instrtab[cnt].off2_3
+ OFF2_3_BIAS - opoff);
int r = op2_fct[instrtab[cnt].fct2] (&output_data);
if (r < 0)
goto not;
if (r > 0)
goto enomem;
string_end_idx = bufcnt;
}
else if (prec == 3 && instrtab[cnt].fct3 != 0)
{
/* Third parameter. */
if (instrtab[cnt].str3 != 0)
ADD_STRING (op3_str
+ op3_str_idx[instrtab[cnt].str3 - 1]);
output_data.opoff1 = (instrtab[cnt].off3_1
+ OFF3_1_BIAS - opoff);
output_data.opoff2 = (instrtab[cnt].off3_2
+ OFF3_2_BIAS - opoff);
#ifdef OFF3_3_BITS
output_data.opoff3 = (instrtab[cnt].off3_3
+ OFF3_3_BIAS - opoff);
#else
output_data.opoff3 = 0;
#endif
int r = op3_fct[instrtab[cnt].fct3] (&output_data);
if (r < 0)
goto not;
if (r > 0)
goto enomem;
string_end_idx = bufcnt;
}
else
bufcnt = string_end_idx;
break;
case 'e':
string_end_idx = bufcnt;
break;
case 'a':
/* Pad to requested column. */
while (bufcnt < (size_t) width)
ADD_CHAR (' ');
width = 0;
break;
case 'l':
if (output_data.labelbuf != NULL
&& output_data.labelbuf[0] != '\0')
{
ADD_STRING (output_data.labelbuf);
output_data.labelbuf[0] = '\0';
string_end_idx = bufcnt;
}
else if (output_data.symaddr_use != addr_none)
{
GElf_Addr symaddr = output_data.symaddr;
if (output_data.symaddr_use >= addr_rel_symbolic)
symaddr += addr + param_start - begin;
// XXX Lookup symbol based on symaddr
const char *symstr = NULL;
if (symcb != NULL
&& symcb (0 /* XXX */, 0 /* XXX */, symaddr,
&output_data.labelbuf,
&output_data.labelbufsize, symcbarg) == 0)
symstr = output_data.labelbuf;
size_t bufavail = bufsize - bufcnt;
int r = 0;
if (symstr != NULL)
r = snprintf (&buf[bufcnt], bufavail, "# <%s>",
symstr);
else if (output_data.symaddr_use == addr_abs_always
|| output_data.symaddr_use == addr_rel_always)
r = snprintf (&buf[bufcnt], bufavail, "# %#" PRIx64,
(uint64_t) symaddr);
if (r < 0)
goto not;
if ((size_t) r >= bufavail)
goto enomem;
bufcnt += r;
string_end_idx = bufcnt;
output_data.symaddr_use = addr_none;
}
break;
}
/* Pad according to the specified width. */
while (bufcnt + prefix_size < start_idx + width)
ADD_CHAR (' ');
prefix_size = 0;
}
if ((prefixes & SEGMENT_PREFIXES) != 0)
goto print_prefix;
assert (string_end_idx != ~0ul);
bufcnt = string_end_idx;
addr += param_start - begin;
data = param_start;
goto out;
}
/* Invalid (or at least unhandled) opcode. */
if (prefixes != 0)
goto print_prefix;
assert (*startp == data);
++data;
ADD_STRING ("(bad)");
addr += data - begin;
out:
if (bufcnt == bufsize)
goto enomem;
buf[bufcnt] = '\0';
*startp = data;
retval = outcb (buf, bufcnt, outcbarg);
if (retval != 0)
goto do_ret;
}
do_ret:
free (output_data.labelbuf);
if (buf != initbuf)
free (buf);
return retval;
}
|