forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi386-tdep.c
8950 lines (7727 loc) · 243 KB
/
i386-tdep.c
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
/* Intel 386 target-dependent stuff.
Copyright (C) 1988-2015 Free Software Foundation, Inc.
This file is part of GDB.
This program 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; either version 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "opcode/i386.h"
#include "arch-utils.h"
#include "command.h"
#include "dummy-frame.h"
#include "dwarf2-frame.h"
#include "doublest.h"
#include "frame.h"
#include "frame-base.h"
#include "frame-unwind.h"
#include "inferior.h"
#include "infrun.h"
#include "gdbcmd.h"
#include "gdbcore.h"
#include "gdbtypes.h"
#include "objfiles.h"
#include "osabi.h"
#include "regcache.h"
#include "reggroups.h"
#include "regset.h"
#include "symfile.h"
#include "symtab.h"
#include "target.h"
#include "value.h"
#include "dis-asm.h"
#include "disasm.h"
#include "remote.h"
#include "i386-tdep.h"
#include "i387-tdep.h"
#include "x86-xstate.h"
#include "record.h"
#include "record-full.h"
#include "features/i386/i386.c"
#include "features/i386/i386-avx.c"
#include "features/i386/i386-mpx.c"
#include "features/i386/i386-avx512.c"
#include "features/i386/i386-mmx.c"
#include "ax.h"
#include "ax-gdb.h"
#include "stap-probe.h"
#include "user-regs.h"
#include "cli/cli-utils.h"
#include "expression.h"
#include "parser-defs.h"
#include <ctype.h>
/* Register names. */
static const char *i386_register_names[] =
{
"eax", "ecx", "edx", "ebx",
"esp", "ebp", "esi", "edi",
"eip", "eflags", "cs", "ss",
"ds", "es", "fs", "gs",
"st0", "st1", "st2", "st3",
"st4", "st5", "st6", "st7",
"fctrl", "fstat", "ftag", "fiseg",
"fioff", "foseg", "fooff", "fop",
"xmm0", "xmm1", "xmm2", "xmm3",
"xmm4", "xmm5", "xmm6", "xmm7",
"mxcsr"
};
static const char *i386_zmm_names[] =
{
"zmm0", "zmm1", "zmm2", "zmm3",
"zmm4", "zmm5", "zmm6", "zmm7"
};
static const char *i386_zmmh_names[] =
{
"zmm0h", "zmm1h", "zmm2h", "zmm3h",
"zmm4h", "zmm5h", "zmm6h", "zmm7h"
};
static const char *i386_k_names[] =
{
"k0", "k1", "k2", "k3",
"k4", "k5", "k6", "k7"
};
static const char *i386_ymm_names[] =
{
"ymm0", "ymm1", "ymm2", "ymm3",
"ymm4", "ymm5", "ymm6", "ymm7",
};
static const char *i386_ymmh_names[] =
{
"ymm0h", "ymm1h", "ymm2h", "ymm3h",
"ymm4h", "ymm5h", "ymm6h", "ymm7h",
};
static const char *i386_mpx_names[] =
{
"bnd0raw", "bnd1raw", "bnd2raw", "bnd3raw", "bndcfgu", "bndstatus"
};
/* Register names for MPX pseudo-registers. */
static const char *i386_bnd_names[] =
{
"bnd0", "bnd1", "bnd2", "bnd3"
};
/* Register names for MMX pseudo-registers. */
static const char *i386_mmx_names[] =
{
"mm0", "mm1", "mm2", "mm3",
"mm4", "mm5", "mm6", "mm7"
};
/* Register names for byte pseudo-registers. */
static const char *i386_byte_names[] =
{
"al", "cl", "dl", "bl",
"ah", "ch", "dh", "bh"
};
/* Register names for word pseudo-registers. */
static const char *i386_word_names[] =
{
"ax", "cx", "dx", "bx",
"", "bp", "si", "di"
};
/* Constant used for reading/writing pseudo registers. In 64-bit mode, we have
16 lower ZMM regs that extend corresponding xmm/ymm registers. In addition,
we have 16 upper ZMM regs that have to be handled differently. */
const int num_lower_zmm_regs = 16;
/* MMX register? */
static int
i386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int mm0_regnum = tdep->mm0_regnum;
if (mm0_regnum < 0)
return 0;
regnum -= mm0_regnum;
return regnum >= 0 && regnum < tdep->num_mmx_regs;
}
/* Byte register? */
int
i386_byte_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
regnum -= tdep->al_regnum;
return regnum >= 0 && regnum < tdep->num_byte_regs;
}
/* Word register? */
int
i386_word_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
regnum -= tdep->ax_regnum;
return regnum >= 0 && regnum < tdep->num_word_regs;
}
/* Dword register? */
int
i386_dword_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int eax_regnum = tdep->eax_regnum;
if (eax_regnum < 0)
return 0;
regnum -= eax_regnum;
return regnum >= 0 && regnum < tdep->num_dword_regs;
}
/* AVX512 register? */
int
i386_zmmh_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int zmm0h_regnum = tdep->zmm0h_regnum;
if (zmm0h_regnum < 0)
return 0;
regnum -= zmm0h_regnum;
return regnum >= 0 && regnum < tdep->num_zmm_regs;
}
int
i386_zmm_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int zmm0_regnum = tdep->zmm0_regnum;
if (zmm0_regnum < 0)
return 0;
regnum -= zmm0_regnum;
return regnum >= 0 && regnum < tdep->num_zmm_regs;
}
int
i386_k_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int k0_regnum = tdep->k0_regnum;
if (k0_regnum < 0)
return 0;
regnum -= k0_regnum;
return regnum >= 0 && regnum < I387_NUM_K_REGS;
}
static int
i386_ymmh_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int ymm0h_regnum = tdep->ymm0h_regnum;
if (ymm0h_regnum < 0)
return 0;
regnum -= ymm0h_regnum;
return regnum >= 0 && regnum < tdep->num_ymm_regs;
}
/* AVX register? */
int
i386_ymm_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int ymm0_regnum = tdep->ymm0_regnum;
if (ymm0_regnum < 0)
return 0;
regnum -= ymm0_regnum;
return regnum >= 0 && regnum < tdep->num_ymm_regs;
}
static int
i386_ymmh_avx512_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int ymm16h_regnum = tdep->ymm16h_regnum;
if (ymm16h_regnum < 0)
return 0;
regnum -= ymm16h_regnum;
return regnum >= 0 && regnum < tdep->num_ymm_avx512_regs;
}
int
i386_ymm_avx512_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int ymm16_regnum = tdep->ymm16_regnum;
if (ymm16_regnum < 0)
return 0;
regnum -= ymm16_regnum;
return regnum >= 0 && regnum < tdep->num_ymm_avx512_regs;
}
/* BND register? */
int
i386_bnd_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int bnd0_regnum = tdep->bnd0_regnum;
if (bnd0_regnum < 0)
return 0;
regnum -= bnd0_regnum;
return regnum >= 0 && regnum < I387_NUM_BND_REGS;
}
/* SSE register? */
int
i386_xmm_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int num_xmm_regs = I387_NUM_XMM_REGS (tdep);
if (num_xmm_regs == 0)
return 0;
regnum -= I387_XMM0_REGNUM (tdep);
return regnum >= 0 && regnum < num_xmm_regs;
}
/* XMM_512 register? */
int
i386_xmm_avx512_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
int num_xmm_avx512_regs = I387_NUM_XMM_AVX512_REGS (tdep);
if (num_xmm_avx512_regs == 0)
return 0;
regnum -= I387_XMM16_REGNUM (tdep);
return regnum >= 0 && regnum < num_xmm_avx512_regs;
}
static int
i386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
if (I387_NUM_XMM_REGS (tdep) == 0)
return 0;
return (regnum == I387_MXCSR_REGNUM (tdep));
}
/* FP register? */
int
i386_fp_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
if (I387_ST0_REGNUM (tdep) < 0)
return 0;
return (I387_ST0_REGNUM (tdep) <= regnum
&& regnum < I387_FCTRL_REGNUM (tdep));
}
int
i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
if (I387_ST0_REGNUM (tdep) < 0)
return 0;
return (I387_FCTRL_REGNUM (tdep) <= regnum
&& regnum < I387_XMM0_REGNUM (tdep));
}
/* BNDr (raw) register? */
static int
i386_bndr_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
if (I387_BND0R_REGNUM (tdep) < 0)
return 0;
regnum -= tdep->bnd0r_regnum;
return regnum >= 0 && regnum < I387_NUM_BND_REGS;
}
/* BND control register? */
static int
i386_mpx_ctrl_regnum_p (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
if (I387_BNDCFGU_REGNUM (tdep) < 0)
return 0;
regnum -= I387_BNDCFGU_REGNUM (tdep);
return regnum >= 0 && regnum < I387_NUM_MPX_CTRL_REGS;
}
/* Return the name of register REGNUM, or the empty string if it is
an anonymous register. */
static const char *
i386_register_name (struct gdbarch *gdbarch, int regnum)
{
/* Hide the upper YMM registers. */
if (i386_ymmh_regnum_p (gdbarch, regnum))
return "";
/* Hide the upper YMM16-31 registers. */
if (i386_ymmh_avx512_regnum_p (gdbarch, regnum))
return "";
/* Hide the upper ZMM registers. */
if (i386_zmmh_regnum_p (gdbarch, regnum))
return "";
return tdesc_register_name (gdbarch, regnum);
}
/* Return the name of register REGNUM. */
const char *
i386_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
if (i386_bnd_regnum_p (gdbarch, regnum))
return i386_bnd_names[regnum - tdep->bnd0_regnum];
if (i386_mmx_regnum_p (gdbarch, regnum))
return i386_mmx_names[regnum - I387_MM0_REGNUM (tdep)];
else if (i386_ymm_regnum_p (gdbarch, regnum))
return i386_ymm_names[regnum - tdep->ymm0_regnum];
else if (i386_zmm_regnum_p (gdbarch, regnum))
return i386_zmm_names[regnum - tdep->zmm0_regnum];
else if (i386_byte_regnum_p (gdbarch, regnum))
return i386_byte_names[regnum - tdep->al_regnum];
else if (i386_word_regnum_p (gdbarch, regnum))
return i386_word_names[regnum - tdep->ax_regnum];
internal_error (__FILE__, __LINE__, _("invalid regnum"));
}
/* Convert a dbx register number REG to the appropriate register
number used by GDB. */
static int
i386_dbx_reg_to_regnum (struct gdbarch *gdbarch, int reg)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
/* This implements what GCC calls the "default" register map
(dbx_register_map[]). */
if (reg >= 0 && reg <= 7)
{
/* General-purpose registers. The debug info calls %ebp
register 4, and %esp register 5. */
if (reg == 4)
return 5;
else if (reg == 5)
return 4;
else return reg;
}
else if (reg >= 12 && reg <= 19)
{
/* Floating-point registers. */
return reg - 12 + I387_ST0_REGNUM (tdep);
}
else if (reg >= 21 && reg <= 28)
{
/* SSE registers. */
int ymm0_regnum = tdep->ymm0_regnum;
if (ymm0_regnum >= 0
&& i386_xmm_regnum_p (gdbarch, reg))
return reg - 21 + ymm0_regnum;
else
return reg - 21 + I387_XMM0_REGNUM (tdep);
}
else if (reg >= 29 && reg <= 36)
{
/* MMX registers. */
return reg - 29 + I387_MM0_REGNUM (tdep);
}
/* This will hopefully provoke a warning. */
return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
}
/* Convert SVR4 register number REG to the appropriate register number
used by GDB. */
static int
i386_svr4_reg_to_regnum (struct gdbarch *gdbarch, int reg)
{
struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
/* This implements the GCC register map that tries to be compatible
with the SVR4 C compiler for DWARF (svr4_dbx_register_map[]). */
/* The SVR4 register numbering includes %eip and %eflags, and
numbers the floating point registers differently. */
if (reg >= 0 && reg <= 9)
{
/* General-purpose registers. */
return reg;
}
else if (reg >= 11 && reg <= 18)
{
/* Floating-point registers. */
return reg - 11 + I387_ST0_REGNUM (tdep);
}
else if (reg >= 21 && reg <= 36)
{
/* The SSE and MMX registers have the same numbers as with dbx. */
return i386_dbx_reg_to_regnum (gdbarch, reg);
}
switch (reg)
{
case 37: return I387_FCTRL_REGNUM (tdep);
case 38: return I387_FSTAT_REGNUM (tdep);
case 39: return I387_MXCSR_REGNUM (tdep);
case 40: return I386_ES_REGNUM;
case 41: return I386_CS_REGNUM;
case 42: return I386_SS_REGNUM;
case 43: return I386_DS_REGNUM;
case 44: return I386_FS_REGNUM;
case 45: return I386_GS_REGNUM;
}
/* This will hopefully provoke a warning. */
return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
}
/* This is the variable that is set with "set disassembly-flavor", and
its legitimate values. */
static const char att_flavor[] = "att";
static const char intel_flavor[] = "intel";
static const char *const valid_flavors[] =
{
att_flavor,
intel_flavor,
NULL
};
static const char *disassembly_flavor = att_flavor;
/* Use the program counter to determine the contents and size of a
breakpoint instruction. Return a pointer to a string of bytes that
encode a breakpoint instruction, store the length of the string in
*LEN and optionally adjust *PC to point to the correct memory
location for inserting the breakpoint.
On the i386 we have a single breakpoint that fits in a single byte
and can be inserted anywhere.
This function is 64-bit safe. */
static const gdb_byte *
i386_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
{
static gdb_byte break_insn[] = { 0xcc }; /* int 3 */
*len = sizeof (break_insn);
return break_insn;
}
/* Displaced instruction handling. */
/* Skip the legacy instruction prefixes in INSN.
Not all prefixes are valid for any particular insn
but we needn't care, the insn will fault if it's invalid.
The result is a pointer to the first opcode byte,
or NULL if we run off the end of the buffer. */
static gdb_byte *
i386_skip_prefixes (gdb_byte *insn, size_t max_len)
{
gdb_byte *end = insn + max_len;
while (insn < end)
{
switch (*insn)
{
case DATA_PREFIX_OPCODE:
case ADDR_PREFIX_OPCODE:
case CS_PREFIX_OPCODE:
case DS_PREFIX_OPCODE:
case ES_PREFIX_OPCODE:
case FS_PREFIX_OPCODE:
case GS_PREFIX_OPCODE:
case SS_PREFIX_OPCODE:
case LOCK_PREFIX_OPCODE:
case REPE_PREFIX_OPCODE:
case REPNE_PREFIX_OPCODE:
++insn;
continue;
default:
return insn;
}
}
return NULL;
}
static int
i386_absolute_jmp_p (const gdb_byte *insn)
{
/* jmp far (absolute address in operand). */
if (insn[0] == 0xea)
return 1;
if (insn[0] == 0xff)
{
/* jump near, absolute indirect (/4). */
if ((insn[1] & 0x38) == 0x20)
return 1;
/* jump far, absolute indirect (/5). */
if ((insn[1] & 0x38) == 0x28)
return 1;
}
return 0;
}
/* Return non-zero if INSN is a jump, zero otherwise. */
static int
i386_jmp_p (const gdb_byte *insn)
{
/* jump short, relative. */
if (insn[0] == 0xeb)
return 1;
/* jump near, relative. */
if (insn[0] == 0xe9)
return 1;
return i386_absolute_jmp_p (insn);
}
static int
i386_absolute_call_p (const gdb_byte *insn)
{
/* call far, absolute. */
if (insn[0] == 0x9a)
return 1;
if (insn[0] == 0xff)
{
/* Call near, absolute indirect (/2). */
if ((insn[1] & 0x38) == 0x10)
return 1;
/* Call far, absolute indirect (/3). */
if ((insn[1] & 0x38) == 0x18)
return 1;
}
return 0;
}
static int
i386_ret_p (const gdb_byte *insn)
{
switch (insn[0])
{
case 0xc2: /* ret near, pop N bytes. */
case 0xc3: /* ret near */
case 0xca: /* ret far, pop N bytes. */
case 0xcb: /* ret far */
case 0xcf: /* iret */
return 1;
default:
return 0;
}
}
static int
i386_call_p (const gdb_byte *insn)
{
if (i386_absolute_call_p (insn))
return 1;
/* call near, relative. */
if (insn[0] == 0xe8)
return 1;
return 0;
}
/* Return non-zero if INSN is a system call, and set *LENGTHP to its
length in bytes. Otherwise, return zero. */
static int
i386_syscall_p (const gdb_byte *insn, int *lengthp)
{
/* Is it 'int $0x80'? */
if ((insn[0] == 0xcd && insn[1] == 0x80)
/* Or is it 'sysenter'? */
|| (insn[0] == 0x0f && insn[1] == 0x34)
/* Or is it 'syscall'? */
|| (insn[0] == 0x0f && insn[1] == 0x05))
{
*lengthp = 2;
return 1;
}
return 0;
}
/* The gdbarch insn_is_call method. */
static int
i386_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
{
gdb_byte buf[I386_MAX_INSN_LEN], *insn;
read_code (addr, buf, I386_MAX_INSN_LEN);
insn = i386_skip_prefixes (buf, I386_MAX_INSN_LEN);
return i386_call_p (insn);
}
/* The gdbarch insn_is_ret method. */
static int
i386_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
{
gdb_byte buf[I386_MAX_INSN_LEN], *insn;
read_code (addr, buf, I386_MAX_INSN_LEN);
insn = i386_skip_prefixes (buf, I386_MAX_INSN_LEN);
return i386_ret_p (insn);
}
/* The gdbarch insn_is_jump method. */
static int
i386_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
{
gdb_byte buf[I386_MAX_INSN_LEN], *insn;
read_code (addr, buf, I386_MAX_INSN_LEN);
insn = i386_skip_prefixes (buf, I386_MAX_INSN_LEN);
return i386_jmp_p (insn);
}
/* Some kernels may run one past a syscall insn, so we have to cope.
Otherwise this is just simple_displaced_step_copy_insn. */
struct displaced_step_closure *
i386_displaced_step_copy_insn (struct gdbarch *gdbarch,
CORE_ADDR from, CORE_ADDR to,
struct regcache *regs)
{
size_t len = gdbarch_max_insn_length (gdbarch);
gdb_byte *buf = xmalloc (len);
read_memory (from, buf, len);
/* GDB may get control back after the insn after the syscall.
Presumably this is a kernel bug.
If this is a syscall, make sure there's a nop afterwards. */
{
int syscall_length;
gdb_byte *insn;
insn = i386_skip_prefixes (buf, len);
if (insn != NULL && i386_syscall_p (insn, &syscall_length))
insn[syscall_length] = NOP_OPCODE;
}
write_memory (to, buf, len);
if (debug_displaced)
{
fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
paddress (gdbarch, from), paddress (gdbarch, to));
displaced_step_dump_bytes (gdb_stdlog, buf, len);
}
return (struct displaced_step_closure *) buf;
}
/* Fix up the state of registers and memory after having single-stepped
a displaced instruction. */
void
i386_displaced_step_fixup (struct gdbarch *gdbarch,
struct displaced_step_closure *closure,
CORE_ADDR from, CORE_ADDR to,
struct regcache *regs)
{
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
/* The offset we applied to the instruction's address.
This could well be negative (when viewed as a signed 32-bit
value), but ULONGEST won't reflect that, so take care when
applying it. */
ULONGEST insn_offset = to - from;
/* Since we use simple_displaced_step_copy_insn, our closure is a
copy of the instruction. */
gdb_byte *insn = (gdb_byte *) closure;
/* The start of the insn, needed in case we see some prefixes. */
gdb_byte *insn_start = insn;
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog,
"displaced: fixup (%s, %s), "
"insn = 0x%02x 0x%02x ...\n",
paddress (gdbarch, from), paddress (gdbarch, to),
insn[0], insn[1]);
/* The list of issues to contend with here is taken from
resume_execution in arch/i386/kernel/kprobes.c, Linux 2.6.20.
Yay for Free Software! */
/* Relocate the %eip, if necessary. */
/* The instruction recognizers we use assume any leading prefixes
have been skipped. */
{
/* This is the size of the buffer in closure. */
size_t max_insn_len = gdbarch_max_insn_length (gdbarch);
gdb_byte *opcode = i386_skip_prefixes (insn, max_insn_len);
/* If there are too many prefixes, just ignore the insn.
It will fault when run. */
if (opcode != NULL)
insn = opcode;
}
/* Except in the case of absolute or indirect jump or call
instructions, or a return instruction, the new eip is relative to
the displaced instruction; make it relative. Well, signal
handler returns don't need relocation either, but we use the
value of %eip to recognize those; see below. */
if (! i386_absolute_jmp_p (insn)
&& ! i386_absolute_call_p (insn)
&& ! i386_ret_p (insn))
{
ULONGEST orig_eip;
int insn_len;
regcache_cooked_read_unsigned (regs, I386_EIP_REGNUM, &orig_eip);
/* A signal trampoline system call changes the %eip, resuming
execution of the main program after the signal handler has
returned. That makes them like 'return' instructions; we
shouldn't relocate %eip.
But most system calls don't, and we do need to relocate %eip.
Our heuristic for distinguishing these cases: if stepping
over the system call instruction left control directly after
the instruction, the we relocate --- control almost certainly
doesn't belong in the displaced copy. Otherwise, we assume
the instruction has put control where it belongs, and leave
it unrelocated. Goodness help us if there are PC-relative
system calls. */
if (i386_syscall_p (insn, &insn_len)
&& orig_eip != to + (insn - insn_start) + insn_len
/* GDB can get control back after the insn after the syscall.
Presumably this is a kernel bug.
i386_displaced_step_copy_insn ensures its a nop,
we add one to the length for it. */
&& orig_eip != to + (insn - insn_start) + insn_len + 1)
{
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog,
"displaced: syscall changed %%eip; "
"not relocating\n");
}
else
{
ULONGEST eip = (orig_eip - insn_offset) & 0xffffffffUL;
/* If we just stepped over a breakpoint insn, we don't backup
the pc on purpose; this is to match behaviour without
stepping. */
regcache_cooked_write_unsigned (regs, I386_EIP_REGNUM, eip);
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog,
"displaced: "
"relocated %%eip from %s to %s\n",
paddress (gdbarch, orig_eip),
paddress (gdbarch, eip));
}
}
/* If the instruction was PUSHFL, then the TF bit will be set in the
pushed value, and should be cleared. We'll leave this for later,
since GDB already messes up the TF flag when stepping over a
pushfl. */
/* If the instruction was a call, the return address now atop the
stack is the address following the copied instruction. We need
to make it the address following the original instruction. */
if (i386_call_p (insn))
{
ULONGEST esp;
ULONGEST retaddr;
const ULONGEST retaddr_len = 4;
regcache_cooked_read_unsigned (regs, I386_ESP_REGNUM, &esp);
retaddr = read_memory_unsigned_integer (esp, retaddr_len, byte_order);
retaddr = (retaddr - insn_offset) & 0xffffffffUL;
write_memory_unsigned_integer (esp, retaddr_len, byte_order, retaddr);
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog,
"displaced: relocated return addr at %s to %s\n",
paddress (gdbarch, esp),
paddress (gdbarch, retaddr));
}
}
static void
append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
{
target_write_memory (*to, buf, len);
*to += len;
}
static void
i386_relocate_instruction (struct gdbarch *gdbarch,
CORE_ADDR *to, CORE_ADDR oldloc)
{
enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
gdb_byte buf[I386_MAX_INSN_LEN];
int offset = 0, rel32, newrel;
int insn_length;
gdb_byte *insn = buf;
read_memory (oldloc, buf, I386_MAX_INSN_LEN);
insn_length = gdb_buffered_insn_length (gdbarch, insn,
I386_MAX_INSN_LEN, oldloc);
/* Get past the prefixes. */
insn = i386_skip_prefixes (insn, I386_MAX_INSN_LEN);
/* Adjust calls with 32-bit relative addresses as push/jump, with
the address pushed being the location where the original call in
the user program would return to. */
if (insn[0] == 0xe8)
{
gdb_byte push_buf[16];
unsigned int ret_addr;
/* Where "ret" in the original code will return to. */
ret_addr = oldloc + insn_length;
push_buf[0] = 0x68; /* pushq $... */
store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
/* Push the push. */
append_insns (to, 5, push_buf);
/* Convert the relative call to a relative jump. */
insn[0] = 0xe9;
/* Adjust the destination offset. */
rel32 = extract_signed_integer (insn + 1, 4, byte_order);
newrel = (oldloc - *to) + rel32;
store_signed_integer (insn + 1, 4, byte_order, newrel);
if (debug_displaced)
fprintf_unfiltered (gdb_stdlog,
"Adjusted insn rel32=%s at %s to"
" rel32=%s at %s\n",
hex_string (rel32), paddress (gdbarch, oldloc),
hex_string (newrel), paddress (gdbarch, *to));
/* Write the adjusted jump into its displaced location. */
append_insns (to, 5, insn);
return;