e2ebffa62a102486e80bcc2b79393ec6294a86fa
[binutils-gdb.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2 Copyright (C) 1988, 1989, 1991, 1994, 1995, 1996, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "floatformat.h"
29 #include "symtab.h"
30 #include "gdbcmd.h"
31 #include "command.h"
32
33 static long i386_get_frame_setup (CORE_ADDR);
34
35 static void i386_follow_jump (void);
36
37 static void codestream_read (unsigned char *, int);
38
39 static void codestream_seek (CORE_ADDR);
40
41 static unsigned char codestream_fill (int);
42
43 CORE_ADDR skip_trampoline_code (CORE_ADDR, char *);
44
45 static int gdb_print_insn_i386 (bfd_vma, disassemble_info *);
46
47 void _initialize_i386_tdep (void);
48
49 /* i386_register_byte[i] is the offset into the register file of the
50 start of register number i. We initialize this from
51 i386_register_raw_size. */
52 int i386_register_byte[MAX_NUM_REGS];
53
54 /* i386_register_raw_size[i] is the number of bytes of storage in
55 GDB's register array occupied by register i. */
56 int i386_register_raw_size[MAX_NUM_REGS] = {
57 4, 4, 4, 4,
58 4, 4, 4, 4,
59 4, 4, 4, 4,
60 4, 4, 4, 4,
61 10, 10, 10, 10,
62 10, 10, 10, 10,
63 4, 4, 4, 4,
64 4, 4, 4, 4,
65 16, 16, 16, 16,
66 16, 16, 16, 16,
67 4
68 };
69
70 /* i386_register_virtual_size[i] is the size in bytes of the virtual
71 type of register i. */
72 int i386_register_virtual_size[MAX_NUM_REGS];
73
74
75 /* This is the variable the is set with "set disassembly-flavor",
76 and its legitimate values. */
77 static char att_flavor[] = "att";
78 static char intel_flavor[] = "intel";
79 static char *valid_flavors[] =
80 {
81 att_flavor,
82 intel_flavor,
83 NULL
84 };
85 static char *disassembly_flavor = att_flavor;
86
87 static void i386_print_register (char *, int, int);
88
89 /* This is used to keep the bfd arch_info in sync with the disassembly flavor. */
90 static void set_disassembly_flavor_sfunc (char *, int,
91 struct cmd_list_element *);
92 static void set_disassembly_flavor (void);
93
94 /* Stdio style buffering was used to minimize calls to ptrace, but this
95 buffering did not take into account that the code section being accessed
96 may not be an even number of buffers long (even if the buffer is only
97 sizeof(int) long). In cases where the code section size happened to
98 be a non-integral number of buffers long, attempting to read the last
99 buffer would fail. Simply using target_read_memory and ignoring errors,
100 rather than read_memory, is not the correct solution, since legitimate
101 access errors would then be totally ignored. To properly handle this
102 situation and continue to use buffering would require that this code
103 be able to determine the minimum code section size granularity (not the
104 alignment of the section itself, since the actual failing case that
105 pointed out this problem had a section alignment of 4 but was not a
106 multiple of 4 bytes long), on a target by target basis, and then
107 adjust it's buffer size accordingly. This is messy, but potentially
108 feasible. It probably needs the bfd library's help and support. For
109 now, the buffer size is set to 1. (FIXME -fnf) */
110
111 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
112 static CORE_ADDR codestream_next_addr;
113 static CORE_ADDR codestream_addr;
114 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
115 static int codestream_off;
116 static int codestream_cnt;
117
118 #define codestream_tell() (codestream_addr + codestream_off)
119 #define codestream_peek() (codestream_cnt == 0 ? \
120 codestream_fill(1): codestream_buf[codestream_off])
121 #define codestream_get() (codestream_cnt-- == 0 ? \
122 codestream_fill(0) : codestream_buf[codestream_off++])
123
124 static unsigned char
125 codestream_fill (peek_flag)
126 int peek_flag;
127 {
128 codestream_addr = codestream_next_addr;
129 codestream_next_addr += CODESTREAM_BUFSIZ;
130 codestream_off = 0;
131 codestream_cnt = CODESTREAM_BUFSIZ;
132 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
133
134 if (peek_flag)
135 return (codestream_peek ());
136 else
137 return (codestream_get ());
138 }
139
140 static void
141 codestream_seek (place)
142 CORE_ADDR place;
143 {
144 codestream_next_addr = place / CODESTREAM_BUFSIZ;
145 codestream_next_addr *= CODESTREAM_BUFSIZ;
146 codestream_cnt = 0;
147 codestream_fill (1);
148 while (codestream_tell () != place)
149 codestream_get ();
150 }
151
152 static void
153 codestream_read (buf, count)
154 unsigned char *buf;
155 int count;
156 {
157 unsigned char *p;
158 int i;
159 p = buf;
160 for (i = 0; i < count; i++)
161 *p++ = codestream_get ();
162 }
163
164 /* next instruction is a jump, move to target */
165
166 static void
167 i386_follow_jump ()
168 {
169 unsigned char buf[4];
170 long delta;
171
172 int data16;
173 CORE_ADDR pos;
174
175 pos = codestream_tell ();
176
177 data16 = 0;
178 if (codestream_peek () == 0x66)
179 {
180 codestream_get ();
181 data16 = 1;
182 }
183
184 switch (codestream_get ())
185 {
186 case 0xe9:
187 /* relative jump: if data16 == 0, disp32, else disp16 */
188 if (data16)
189 {
190 codestream_read (buf, 2);
191 delta = extract_signed_integer (buf, 2);
192
193 /* include size of jmp inst (including the 0x66 prefix). */
194 pos += delta + 4;
195 }
196 else
197 {
198 codestream_read (buf, 4);
199 delta = extract_signed_integer (buf, 4);
200
201 pos += delta + 5;
202 }
203 break;
204 case 0xeb:
205 /* relative jump, disp8 (ignore data16) */
206 codestream_read (buf, 1);
207 /* Sign-extend it. */
208 delta = extract_signed_integer (buf, 1);
209
210 pos += delta + 2;
211 break;
212 }
213 codestream_seek (pos);
214 }
215
216 /*
217 * find & return amound a local space allocated, and advance codestream to
218 * first register push (if any)
219 *
220 * if entry sequence doesn't make sense, return -1, and leave
221 * codestream pointer random
222 */
223
224 static long
225 i386_get_frame_setup (pc)
226 CORE_ADDR pc;
227 {
228 unsigned char op;
229
230 codestream_seek (pc);
231
232 i386_follow_jump ();
233
234 op = codestream_get ();
235
236 if (op == 0x58) /* popl %eax */
237 {
238 /*
239 * this function must start with
240 *
241 * popl %eax 0x58
242 * xchgl %eax, (%esp) 0x87 0x04 0x24
243 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
244 *
245 * (the system 5 compiler puts out the second xchg
246 * inst, and the assembler doesn't try to optimize it,
247 * so the 'sib' form gets generated)
248 *
249 * this sequence is used to get the address of the return
250 * buffer for a function that returns a structure
251 */
252 int pos;
253 unsigned char buf[4];
254 static unsigned char proto1[3] =
255 {0x87, 0x04, 0x24};
256 static unsigned char proto2[4] =
257 {0x87, 0x44, 0x24, 0x00};
258 pos = codestream_tell ();
259 codestream_read (buf, 4);
260 if (memcmp (buf, proto1, 3) == 0)
261 pos += 3;
262 else if (memcmp (buf, proto2, 4) == 0)
263 pos += 4;
264
265 codestream_seek (pos);
266 op = codestream_get (); /* update next opcode */
267 }
268
269 if (op == 0x68 || op == 0x6a)
270 {
271 /*
272 * this function may start with
273 *
274 * pushl constant
275 * call _probe
276 * addl $4, %esp
277 * followed by
278 * pushl %ebp
279 * etc.
280 */
281 int pos;
282 unsigned char buf[8];
283
284 /* Skip past the pushl instruction; it has either a one-byte
285 or a four-byte operand, depending on the opcode. */
286 pos = codestream_tell ();
287 if (op == 0x68)
288 pos += 4;
289 else
290 pos += 1;
291 codestream_seek (pos);
292
293 /* Read the following 8 bytes, which should be "call _probe" (6 bytes)
294 followed by "addl $4,%esp" (2 bytes). */
295 codestream_read (buf, sizeof (buf));
296 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
297 pos += sizeof (buf);
298 codestream_seek (pos);
299 op = codestream_get (); /* update next opcode */
300 }
301
302 if (op == 0x55) /* pushl %ebp */
303 {
304 /* check for movl %esp, %ebp - can be written two ways */
305 switch (codestream_get ())
306 {
307 case 0x8b:
308 if (codestream_get () != 0xec)
309 return (-1);
310 break;
311 case 0x89:
312 if (codestream_get () != 0xe5)
313 return (-1);
314 break;
315 default:
316 return (-1);
317 }
318 /* check for stack adjustment
319
320 * subl $XXX, %esp
321 *
322 * note: you can't subtract a 16 bit immediate
323 * from a 32 bit reg, so we don't have to worry
324 * about a data16 prefix
325 */
326 op = codestream_peek ();
327 if (op == 0x83)
328 {
329 /* subl with 8 bit immed */
330 codestream_get ();
331 if (codestream_get () != 0xec)
332 /* Some instruction starting with 0x83 other than subl. */
333 {
334 codestream_seek (codestream_tell () - 2);
335 return 0;
336 }
337 /* subl with signed byte immediate
338 * (though it wouldn't make sense to be negative)
339 */
340 return (codestream_get ());
341 }
342 else if (op == 0x81)
343 {
344 char buf[4];
345 /* Maybe it is subl with 32 bit immedediate. */
346 codestream_get ();
347 if (codestream_get () != 0xec)
348 /* Some instruction starting with 0x81 other than subl. */
349 {
350 codestream_seek (codestream_tell () - 2);
351 return 0;
352 }
353 /* It is subl with 32 bit immediate. */
354 codestream_read ((unsigned char *) buf, 4);
355 return extract_signed_integer (buf, 4);
356 }
357 else
358 {
359 return (0);
360 }
361 }
362 else if (op == 0xc8)
363 {
364 char buf[2];
365 /* enter instruction: arg is 16 bit unsigned immed */
366 codestream_read ((unsigned char *) buf, 2);
367 codestream_get (); /* flush final byte of enter instruction */
368 return extract_unsigned_integer (buf, 2);
369 }
370 return (-1);
371 }
372
373 /* Return number of args passed to a frame.
374 Can return -1, meaning no way to tell. */
375
376 int
377 i386_frame_num_args (fi)
378 struct frame_info *fi;
379 {
380 #if 1
381 return -1;
382 #else
383 /* This loses because not only might the compiler not be popping the
384 args right after the function call, it might be popping args from both
385 this call and a previous one, and we would say there are more args
386 than there really are. */
387
388 int retpc;
389 unsigned char op;
390 struct frame_info *pfi;
391
392 /* on the 386, the instruction following the call could be:
393 popl %ecx - one arg
394 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
395 anything else - zero args */
396
397 int frameless;
398
399 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
400 if (frameless)
401 /* In the absence of a frame pointer, GDB doesn't get correct values
402 for nameless arguments. Return -1, so it doesn't print any
403 nameless arguments. */
404 return -1;
405
406 pfi = get_prev_frame (fi);
407 if (pfi == 0)
408 {
409 /* Note: this can happen if we are looking at the frame for
410 main, because FRAME_CHAIN_VALID won't let us go into
411 start. If we have debugging symbols, that's not really
412 a big deal; it just means it will only show as many arguments
413 to main as are declared. */
414 return -1;
415 }
416 else
417 {
418 retpc = pfi->pc;
419 op = read_memory_integer (retpc, 1);
420 if (op == 0x59)
421 /* pop %ecx */
422 return 1;
423 else if (op == 0x83)
424 {
425 op = read_memory_integer (retpc + 1, 1);
426 if (op == 0xc4)
427 /* addl $<signed imm 8 bits>, %esp */
428 return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
429 else
430 return 0;
431 }
432 else if (op == 0x81)
433 { /* add with 32 bit immediate */
434 op = read_memory_integer (retpc + 1, 1);
435 if (op == 0xc4)
436 /* addl $<imm 32>, %esp */
437 return read_memory_integer (retpc + 2, 4) / 4;
438 else
439 return 0;
440 }
441 else
442 {
443 return 0;
444 }
445 }
446 #endif
447 }
448
449 /*
450 * parse the first few instructions of the function to see
451 * what registers were stored.
452 *
453 * We handle these cases:
454 *
455 * The startup sequence can be at the start of the function,
456 * or the function can start with a branch to startup code at the end.
457 *
458 * %ebp can be set up with either the 'enter' instruction, or
459 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
460 * but was once used in the sys5 compiler)
461 *
462 * Local space is allocated just below the saved %ebp by either the
463 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
464 * a 16 bit unsigned argument for space to allocate, and the
465 * 'addl' instruction could have either a signed byte, or
466 * 32 bit immediate.
467 *
468 * Next, the registers used by this function are pushed. In
469 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
470 * (and sometimes a harmless bug causes it to also save but not restore %eax);
471 * however, the code below is willing to see the pushes in any order,
472 * and will handle up to 8 of them.
473 *
474 * If the setup sequence is at the end of the function, then the
475 * next instruction will be a branch back to the start.
476 */
477
478 void
479 i386_frame_init_saved_regs (fip)
480 struct frame_info *fip;
481 {
482 long locals = -1;
483 unsigned char op;
484 CORE_ADDR dummy_bottom;
485 CORE_ADDR adr;
486 CORE_ADDR pc;
487 int i;
488
489 if (fip->saved_regs)
490 return;
491
492 frame_saved_regs_zalloc (fip);
493
494 /* if frame is the end of a dummy, compute where the
495 * beginning would be
496 */
497 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
498
499 /* check if the PC is in the stack, in a dummy frame */
500 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
501 {
502 /* all regs were saved by push_call_dummy () */
503 adr = fip->frame;
504 for (i = 0; i < NUM_REGS; i++)
505 {
506 adr -= REGISTER_RAW_SIZE (i);
507 fip->saved_regs[i] = adr;
508 }
509 return;
510 }
511
512 pc = get_pc_function_start (fip->pc);
513 if (pc != 0)
514 locals = i386_get_frame_setup (pc);
515
516 if (locals >= 0)
517 {
518 adr = fip->frame - 4 - locals;
519 for (i = 0; i < 8; i++)
520 {
521 op = codestream_get ();
522 if (op < 0x50 || op > 0x57)
523 break;
524 #ifdef I386_REGNO_TO_SYMMETRY
525 /* Dynix uses different internal numbering. Ick. */
526 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = adr;
527 #else
528 fip->saved_regs[op - 0x50] = adr;
529 #endif
530 adr -= 4;
531 }
532 }
533
534 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
535 fip->saved_regs[FP_REGNUM] = fip->frame;
536 }
537
538 /* return pc of first real instruction */
539
540 int
541 i386_skip_prologue (pc)
542 int pc;
543 {
544 unsigned char op;
545 int i;
546 static unsigned char pic_pat[6] =
547 {0xe8, 0, 0, 0, 0, /* call 0x0 */
548 0x5b, /* popl %ebx */
549 };
550 CORE_ADDR pos;
551
552 if (i386_get_frame_setup (pc) < 0)
553 return (pc);
554
555 /* found valid frame setup - codestream now points to
556 * start of push instructions for saving registers
557 */
558
559 /* skip over register saves */
560 for (i = 0; i < 8; i++)
561 {
562 op = codestream_peek ();
563 /* break if not pushl inst */
564 if (op < 0x50 || op > 0x57)
565 break;
566 codestream_get ();
567 }
568
569 /* The native cc on SVR4 in -K PIC mode inserts the following code to get
570 the address of the global offset table (GOT) into register %ebx.
571 call 0x0
572 popl %ebx
573 movl %ebx,x(%ebp) (optional)
574 addl y,%ebx
575 This code is with the rest of the prologue (at the end of the
576 function), so we have to skip it to get to the first real
577 instruction at the start of the function. */
578
579 pos = codestream_tell ();
580 for (i = 0; i < 6; i++)
581 {
582 op = codestream_get ();
583 if (pic_pat[i] != op)
584 break;
585 }
586 if (i == 6)
587 {
588 unsigned char buf[4];
589 long delta = 6;
590
591 op = codestream_get ();
592 if (op == 0x89) /* movl %ebx, x(%ebp) */
593 {
594 op = codestream_get ();
595 if (op == 0x5d) /* one byte offset from %ebp */
596 {
597 delta += 3;
598 codestream_read (buf, 1);
599 }
600 else if (op == 0x9d) /* four byte offset from %ebp */
601 {
602 delta += 6;
603 codestream_read (buf, 4);
604 }
605 else /* unexpected instruction */
606 delta = -1;
607 op = codestream_get ();
608 }
609 /* addl y,%ebx */
610 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
611 {
612 pos += delta + 6;
613 }
614 }
615 codestream_seek (pos);
616
617 i386_follow_jump ();
618
619 return (codestream_tell ());
620 }
621
622 void
623 i386_push_dummy_frame ()
624 {
625 CORE_ADDR sp = read_register (SP_REGNUM);
626 int regnum;
627 char regbuf[MAX_REGISTER_RAW_SIZE];
628
629 sp = push_word (sp, read_register (PC_REGNUM));
630 sp = push_word (sp, read_register (FP_REGNUM));
631 write_register (FP_REGNUM, sp);
632 for (regnum = 0; regnum < NUM_REGS; regnum++)
633 {
634 read_register_gen (regnum, regbuf);
635 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
636 }
637 write_register (SP_REGNUM, sp);
638 }
639
640 void
641 i386_pop_frame ()
642 {
643 struct frame_info *frame = get_current_frame ();
644 CORE_ADDR fp;
645 int regnum;
646 char regbuf[MAX_REGISTER_RAW_SIZE];
647
648 fp = FRAME_FP (frame);
649 i386_frame_init_saved_regs (frame);
650
651 for (regnum = 0; regnum < NUM_REGS; regnum++)
652 {
653 CORE_ADDR adr;
654 adr = frame->saved_regs[regnum];
655 if (adr)
656 {
657 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
658 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
659 REGISTER_RAW_SIZE (regnum));
660 }
661 }
662 write_register (FP_REGNUM, read_memory_integer (fp, 4));
663 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
664 write_register (SP_REGNUM, fp + 8);
665 flush_cached_frames ();
666 }
667
668 #ifdef GET_LONGJMP_TARGET
669
670 /* Figure out where the longjmp will land. Slurp the args out of the stack.
671 We expect the first arg to be a pointer to the jmp_buf structure from which
672 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
673 This routine returns true on success. */
674
675 int
676 get_longjmp_target (pc)
677 CORE_ADDR *pc;
678 {
679 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
680 CORE_ADDR sp, jb_addr;
681
682 sp = read_register (SP_REGNUM);
683
684 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
685 buf,
686 TARGET_PTR_BIT / TARGET_CHAR_BIT))
687 return 0;
688
689 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
690
691 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
692 TARGET_PTR_BIT / TARGET_CHAR_BIT))
693 return 0;
694
695 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
696
697 return 1;
698 }
699
700 #endif /* GET_LONGJMP_TARGET */
701
702 /* These registers are used for returning integers (and on some
703 targets also for returning `struct' and `union' values when their
704 size and alignment match an integer type. */
705 #define LOW_RETURN_REGNUM 0 /* %eax */
706 #define HIGH_RETURN_REGNUM 2 /* %edx */
707
708 /* Extract from an array REGBUF containing the (raw) register state, a
709 function return value of TYPE, and copy that, in virtual format,
710 into VALBUF. */
711
712 void
713 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
714 {
715 int len = TYPE_LENGTH (type);
716
717 if (TYPE_CODE_FLT == TYPE_CODE (type))
718 {
719 if (NUM_FREGS == 0)
720 {
721 warning ("Cannot find floating-point return value.");
722 memset (valbuf, 0, len);
723 }
724
725 /* Floating-point return values can be found in %st(0). */
726 if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
727 && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
728 {
729 /* Copy straight over, but take care of the padding. */
730 memcpy (valbuf, &regbuf[REGISTER_BYTE (FP0_REGNUM)],
731 FPU_REG_RAW_SIZE);
732 memset (valbuf + FPU_REG_RAW_SIZE, 0, len - FPU_REG_RAW_SIZE);
733 }
734 else
735 {
736 /* Convert the extended floating-point number found in
737 %st(0) to the desired type. This is probably not exactly
738 how it would happen on the target itself, but it is the
739 best we can do. */
740 DOUBLEST val;
741 floatformat_to_doublest (&floatformat_i387_ext,
742 &regbuf[REGISTER_BYTE (FP0_REGNUM)], &val);
743 store_floating (valbuf, TYPE_LENGTH (type), val);
744 }
745 }
746 else
747 {
748 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
749 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
750
751 if (len <= low_size)
752 memcpy (valbuf, &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
753 else if (len <= (low_size + high_size))
754 {
755 memcpy (valbuf,
756 &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
757 memcpy (valbuf + low_size,
758 &regbuf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
759 }
760 else
761 internal_error ("Cannot extract return value of %d bytes long.", len);
762 }
763 }
764
765 /* Convert data from raw format for register REGNUM in buffer FROM to
766 virtual format with type TYPE in buffer TO. In principle both
767 formats are identical except that the virtual format has two extra
768 bytes appended that aren't used. We set these to zero. */
769
770 void
771 i386_register_convert_to_virtual (int regnum, struct type *type,
772 char *from, char *to)
773 {
774 /* Copy straight over, but take care of the padding. */
775 memcpy (to, from, FPU_REG_RAW_SIZE);
776 memset (to + FPU_REG_RAW_SIZE, 0, TYPE_LENGTH (type) - FPU_REG_RAW_SIZE);
777 }
778
779 /* Convert data from virtual format with type TYPE in buffer FROM to
780 raw format for register REGNUM in buffer TO. Simply omit the two
781 unused bytes. */
782
783 void
784 i386_register_convert_to_raw (struct type *type, int regnum,
785 char *from, char *to)
786 {
787 memcpy (to, from, FPU_REG_RAW_SIZE);
788 }
789
790 \f
791 #ifdef I386V4_SIGTRAMP_SAVED_PC
792 /* Get saved user PC for sigtramp from the pushed ucontext on the stack
793 for all three variants of SVR4 sigtramps. */
794
795 CORE_ADDR
796 i386v4_sigtramp_saved_pc (frame)
797 struct frame_info *frame;
798 {
799 CORE_ADDR saved_pc_offset = 4;
800 char *name = NULL;
801
802 find_pc_partial_function (frame->pc, &name, NULL, NULL);
803 if (name)
804 {
805 if (STREQ (name, "_sigreturn"))
806 saved_pc_offset = 132 + 14 * 4;
807 else if (STREQ (name, "_sigacthandler"))
808 saved_pc_offset = 80 + 14 * 4;
809 else if (STREQ (name, "sigvechandler"))
810 saved_pc_offset = 120 + 14 * 4;
811 }
812
813 if (frame->next)
814 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
815 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
816 }
817 #endif /* I386V4_SIGTRAMP_SAVED_PC */
818
819
820 #ifdef STATIC_TRANSFORM_NAME
821 /* SunPRO encodes the static variables. This is not related to C++ mangling,
822 it is done for C too. */
823
824 char *
825 sunpro_static_transform_name (name)
826 char *name;
827 {
828 char *p;
829 if (IS_STATIC_TRANSFORM_NAME (name))
830 {
831 /* For file-local statics there will be a period, a bunch
832 of junk (the contents of which match a string given in the
833 N_OPT), a period and the name. For function-local statics
834 there will be a bunch of junk (which seems to change the
835 second character from 'A' to 'B'), a period, the name of the
836 function, and the name. So just skip everything before the
837 last period. */
838 p = strrchr (name, '.');
839 if (p != NULL)
840 name = p + 1;
841 }
842 return name;
843 }
844 #endif /* STATIC_TRANSFORM_NAME */
845
846
847
848 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
849
850 CORE_ADDR
851 skip_trampoline_code (pc, name)
852 CORE_ADDR pc;
853 char *name;
854 {
855 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
856 {
857 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
858 struct minimal_symbol *indsym =
859 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
860 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
861
862 if (symname)
863 {
864 if (strncmp (symname, "__imp_", 6) == 0
865 || strncmp (symname, "_imp_", 5) == 0)
866 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
867 }
868 }
869 return 0; /* not a trampoline */
870 }
871
872 static int
873 gdb_print_insn_i386 (memaddr, info)
874 bfd_vma memaddr;
875 disassemble_info *info;
876 {
877 if (disassembly_flavor == att_flavor)
878 return print_insn_i386_att (memaddr, info);
879 else if (disassembly_flavor == intel_flavor)
880 return print_insn_i386_intel (memaddr, info);
881 /* Never reached - disassembly_flavour is always either att_flavor
882 or intel_flavor */
883 abort ();
884 }
885
886 /* If the disassembly mode is intel, we have to also switch the
887 bfd mach_type. This function is run in the set disassembly_flavor
888 command, and does that. */
889
890 static void
891 set_disassembly_flavor_sfunc (args, from_tty, c)
892 char *args;
893 int from_tty;
894 struct cmd_list_element *c;
895 {
896 set_disassembly_flavor ();
897 }
898
899 static void
900 set_disassembly_flavor ()
901 {
902 if (disassembly_flavor == att_flavor)
903 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386);
904 else if (disassembly_flavor == intel_flavor)
905 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386_intel_syntax);
906 }
907
908
909 void
910 _initialize_i386_tdep ()
911 {
912 /* Initialize the table saying where each register starts in the
913 register file. */
914 {
915 int i, offset;
916
917 offset = 0;
918 for (i = 0; i < MAX_NUM_REGS; i++)
919 {
920 i386_register_byte[i] = offset;
921 offset += i386_register_raw_size[i];
922 }
923 }
924
925 /* Initialize the table of virtual register sizes. */
926 {
927 int i;
928
929 for (i = 0; i < MAX_NUM_REGS; i++)
930 i386_register_virtual_size[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
931 }
932
933 tm_print_insn = gdb_print_insn_i386;
934 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
935
936 /* Add the variable that controls the disassembly flavor */
937 {
938 struct cmd_list_element *new_cmd;
939
940 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
941 valid_flavors,
942 &disassembly_flavor,
943 "Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
944 and the default value is \"att\".",
945 &setlist);
946 new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
947 add_show_from_set (new_cmd, &showlist);
948 }
949
950 /* Finally, initialize the disassembly flavor to the default given
951 in the disassembly_flavor variable */
952
953 set_disassembly_flavor ();
954 }