Don't update ``set architecture'' when ``set disassembly-flavor[sic]''
[binutils-gdb.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "floatformat.h"
30 #include "symtab.h"
31 #include "gdbcmd.h"
32 #include "command.h"
33 #include "arch-utils.h"
34 #include "regcache.h"
35 #include "doublest.h"
36 #include "value.h"
37
38 #include "gdb_assert.h"
39
40 /* Names of the registers. The first 10 registers match the register
41 numbering scheme used by GCC for stabs and DWARF. */
42 static char *i386_register_names[] =
43 {
44 "eax", "ecx", "edx", "ebx",
45 "esp", "ebp", "esi", "edi",
46 "eip", "eflags", "cs", "ss",
47 "ds", "es", "fs", "gs",
48 "st0", "st1", "st2", "st3",
49 "st4", "st5", "st6", "st7",
50 "fctrl", "fstat", "ftag", "fiseg",
51 "fioff", "foseg", "fooff", "fop",
52 "xmm0", "xmm1", "xmm2", "xmm3",
53 "xmm4", "xmm5", "xmm6", "xmm7",
54 "mxcsr"
55 };
56
57 /* i386_register_offset[i] is the offset into the register file of the
58 start of register number i. We initialize this from
59 i386_register_size. */
60 static int i386_register_offset[MAX_NUM_REGS];
61
62 /* i386_register_size[i] is the number of bytes of storage in GDB's
63 register array occupied by register i. */
64 static int i386_register_size[MAX_NUM_REGS] = {
65 4, 4, 4, 4,
66 4, 4, 4, 4,
67 4, 4, 4, 4,
68 4, 4, 4, 4,
69 10, 10, 10, 10,
70 10, 10, 10, 10,
71 4, 4, 4, 4,
72 4, 4, 4, 4,
73 16, 16, 16, 16,
74 16, 16, 16, 16,
75 4
76 };
77
78 /* Return the name of register REG. */
79
80 char *
81 i386_register_name (int reg)
82 {
83 if (reg < 0)
84 return NULL;
85 if (reg >= sizeof (i386_register_names) / sizeof (*i386_register_names))
86 return NULL;
87
88 return i386_register_names[reg];
89 }
90
91 /* Return the offset into the register array of the start of register
92 number REG. */
93 int
94 i386_register_byte (int reg)
95 {
96 return i386_register_offset[reg];
97 }
98
99 /* Return the number of bytes of storage in GDB's register array
100 occupied by register REG. */
101
102 int
103 i386_register_raw_size (int reg)
104 {
105 return i386_register_size[reg];
106 }
107
108 /* Return the size in bytes of the virtual type of register REG. */
109
110 int
111 i386_register_virtual_size (int reg)
112 {
113 return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (reg));
114 }
115
116 /* Convert stabs register number REG to the appropriate register
117 number used by GDB. */
118
119 int
120 i386_stab_reg_to_regnum (int reg)
121 {
122 /* This implements what GCC calls the "default" register map. */
123 if (reg >= 0 && reg <= 7)
124 {
125 /* General registers. */
126 return reg;
127 }
128 else if (reg >= 12 && reg <= 19)
129 {
130 /* Floating-point registers. */
131 return reg - 12 + FP0_REGNUM;
132 }
133 else if (reg >= 21 && reg <= 28)
134 {
135 /* SSE registers. */
136 return reg - 21 + XMM0_REGNUM;
137 }
138 else if (reg >= 29 && reg <= 36)
139 {
140 /* MMX registers. */
141 /* FIXME: kettenis/2001-07-28: Should we have the MMX registers
142 as pseudo-registers? */
143 return reg - 29 + FP0_REGNUM;
144 }
145
146 /* This will hopefully provoke a warning. */
147 return NUM_REGS + NUM_PSEUDO_REGS;
148 }
149
150 /* Convert Dwarf register number REG to the appropriate register
151 number used by GDB. */
152
153 int
154 i386_dwarf_reg_to_regnum (int reg)
155 {
156 /* The DWARF register numbering includes %eip and %eflags, and
157 numbers the floating point registers differently. */
158 if (reg >= 0 && reg <= 9)
159 {
160 /* General registers. */
161 return reg;
162 }
163 else if (reg >= 11 && reg <= 18)
164 {
165 /* Floating-point registers. */
166 return reg - 11 + FP0_REGNUM;
167 }
168 else if (reg >= 21)
169 {
170 /* The SSE and MMX registers have identical numbers as in stabs. */
171 return i386_stab_reg_to_regnum (reg);
172 }
173
174 /* This will hopefully provoke a warning. */
175 return NUM_REGS + NUM_PSEUDO_REGS;
176 }
177 \f
178
179 /* This is the variable that is set with "set disassembly-flavor", and
180 its legitimate values. */
181 static const char att_flavor[] = "att";
182 static const char intel_flavor[] = "intel";
183 static const char *valid_flavors[] =
184 {
185 att_flavor,
186 intel_flavor,
187 NULL
188 };
189 static const char *disassembly_flavor = att_flavor;
190
191 /* Stdio style buffering was used to minimize calls to ptrace, but
192 this buffering did not take into account that the code section
193 being accessed may not be an even number of buffers long (even if
194 the buffer is only sizeof(int) long). In cases where the code
195 section size happened to be a non-integral number of buffers long,
196 attempting to read the last buffer would fail. Simply using
197 target_read_memory and ignoring errors, rather than read_memory, is
198 not the correct solution, since legitimate access errors would then
199 be totally ignored. To properly handle this situation and continue
200 to use buffering would require that this code be able to determine
201 the minimum code section size granularity (not the alignment of the
202 section itself, since the actual failing case that pointed out this
203 problem had a section alignment of 4 but was not a multiple of 4
204 bytes long), on a target by target basis, and then adjust it's
205 buffer size accordingly. This is messy, but potentially feasible.
206 It probably needs the bfd library's help and support. For now, the
207 buffer size is set to 1. (FIXME -fnf) */
208
209 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
210 static CORE_ADDR codestream_next_addr;
211 static CORE_ADDR codestream_addr;
212 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
213 static int codestream_off;
214 static int codestream_cnt;
215
216 #define codestream_tell() (codestream_addr + codestream_off)
217 #define codestream_peek() \
218 (codestream_cnt == 0 ? \
219 codestream_fill(1) : codestream_buf[codestream_off])
220 #define codestream_get() \
221 (codestream_cnt-- == 0 ? \
222 codestream_fill(0) : codestream_buf[codestream_off++])
223
224 static unsigned char
225 codestream_fill (int peek_flag)
226 {
227 codestream_addr = codestream_next_addr;
228 codestream_next_addr += CODESTREAM_BUFSIZ;
229 codestream_off = 0;
230 codestream_cnt = CODESTREAM_BUFSIZ;
231 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
232
233 if (peek_flag)
234 return (codestream_peek ());
235 else
236 return (codestream_get ());
237 }
238
239 static void
240 codestream_seek (CORE_ADDR place)
241 {
242 codestream_next_addr = place / CODESTREAM_BUFSIZ;
243 codestream_next_addr *= CODESTREAM_BUFSIZ;
244 codestream_cnt = 0;
245 codestream_fill (1);
246 while (codestream_tell () != place)
247 codestream_get ();
248 }
249
250 static void
251 codestream_read (unsigned char *buf, int count)
252 {
253 unsigned char *p;
254 int i;
255 p = buf;
256 for (i = 0; i < count; i++)
257 *p++ = codestream_get ();
258 }
259 \f
260
261 /* If the next instruction is a jump, move to its target. */
262
263 static void
264 i386_follow_jump (void)
265 {
266 unsigned char buf[4];
267 long delta;
268
269 int data16;
270 CORE_ADDR pos;
271
272 pos = codestream_tell ();
273
274 data16 = 0;
275 if (codestream_peek () == 0x66)
276 {
277 codestream_get ();
278 data16 = 1;
279 }
280
281 switch (codestream_get ())
282 {
283 case 0xe9:
284 /* Relative jump: if data16 == 0, disp32, else disp16. */
285 if (data16)
286 {
287 codestream_read (buf, 2);
288 delta = extract_signed_integer (buf, 2);
289
290 /* Include the size of the jmp instruction (including the
291 0x66 prefix). */
292 pos += delta + 4;
293 }
294 else
295 {
296 codestream_read (buf, 4);
297 delta = extract_signed_integer (buf, 4);
298
299 pos += delta + 5;
300 }
301 break;
302 case 0xeb:
303 /* Relative jump, disp8 (ignore data16). */
304 codestream_read (buf, 1);
305 /* Sign-extend it. */
306 delta = extract_signed_integer (buf, 1);
307
308 pos += delta + 2;
309 break;
310 }
311 codestream_seek (pos);
312 }
313
314 /* Find & return the amount a local space allocated, and advance the
315 codestream to the first register push (if any).
316
317 If the entry sequence doesn't make sense, return -1, and leave
318 codestream pointer at a random spot. */
319
320 static long
321 i386_get_frame_setup (CORE_ADDR pc)
322 {
323 unsigned char op;
324
325 codestream_seek (pc);
326
327 i386_follow_jump ();
328
329 op = codestream_get ();
330
331 if (op == 0x58) /* popl %eax */
332 {
333 /* This function must start with
334
335 popl %eax 0x58
336 xchgl %eax, (%esp) 0x87 0x04 0x24
337 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
338
339 (the System V compiler puts out the second `xchg'
340 instruction, and the assembler doesn't try to optimize it, so
341 the 'sib' form gets generated). This sequence is used to get
342 the address of the return buffer for a function that returns
343 a structure. */
344 int pos;
345 unsigned char buf[4];
346 static unsigned char proto1[3] = { 0x87, 0x04, 0x24 };
347 static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
348
349 pos = codestream_tell ();
350 codestream_read (buf, 4);
351 if (memcmp (buf, proto1, 3) == 0)
352 pos += 3;
353 else if (memcmp (buf, proto2, 4) == 0)
354 pos += 4;
355
356 codestream_seek (pos);
357 op = codestream_get (); /* Update next opcode. */
358 }
359
360 if (op == 0x68 || op == 0x6a)
361 {
362 /* This function may start with
363
364 pushl constant
365 call _probe
366 addl $4, %esp
367
368 followed by
369
370 pushl %ebp
371
372 etc. */
373 int pos;
374 unsigned char buf[8];
375
376 /* Skip past the `pushl' instruction; it has either a one-byte
377 or a four-byte operand, depending on the opcode. */
378 pos = codestream_tell ();
379 if (op == 0x68)
380 pos += 4;
381 else
382 pos += 1;
383 codestream_seek (pos);
384
385 /* Read the following 8 bytes, which should be "call _probe" (6
386 bytes) followed by "addl $4,%esp" (2 bytes). */
387 codestream_read (buf, sizeof (buf));
388 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
389 pos += sizeof (buf);
390 codestream_seek (pos);
391 op = codestream_get (); /* Update next opcode. */
392 }
393
394 if (op == 0x55) /* pushl %ebp */
395 {
396 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
397 switch (codestream_get ())
398 {
399 case 0x8b:
400 if (codestream_get () != 0xec)
401 return -1;
402 break;
403 case 0x89:
404 if (codestream_get () != 0xe5)
405 return -1;
406 break;
407 default:
408 return -1;
409 }
410 /* Check for stack adjustment
411
412 subl $XXX, %esp
413
414 NOTE: You can't subtract a 16 bit immediate from a 32 bit
415 reg, so we don't have to worry about a data16 prefix. */
416 op = codestream_peek ();
417 if (op == 0x83)
418 {
419 /* `subl' with 8 bit immediate. */
420 codestream_get ();
421 if (codestream_get () != 0xec)
422 /* Some instruction starting with 0x83 other than `subl'. */
423 {
424 codestream_seek (codestream_tell () - 2);
425 return 0;
426 }
427 /* `subl' with signed byte immediate (though it wouldn't
428 make sense to be negative). */
429 return (codestream_get ());
430 }
431 else if (op == 0x81)
432 {
433 char buf[4];
434 /* Maybe it is `subl' with a 32 bit immedediate. */
435 codestream_get ();
436 if (codestream_get () != 0xec)
437 /* Some instruction starting with 0x81 other than `subl'. */
438 {
439 codestream_seek (codestream_tell () - 2);
440 return 0;
441 }
442 /* It is `subl' with a 32 bit immediate. */
443 codestream_read ((unsigned char *) buf, 4);
444 return extract_signed_integer (buf, 4);
445 }
446 else
447 {
448 return 0;
449 }
450 }
451 else if (op == 0xc8)
452 {
453 char buf[2];
454 /* `enter' with 16 bit unsigned immediate. */
455 codestream_read ((unsigned char *) buf, 2);
456 codestream_get (); /* Flush final byte of enter instruction. */
457 return extract_unsigned_integer (buf, 2);
458 }
459 return (-1);
460 }
461
462 /* Return the chain-pointer for FRAME. In the case of the i386, the
463 frame's nominal address is the address of a 4-byte word containing
464 the calling frame's address. */
465
466 CORE_ADDR
467 i386_frame_chain (struct frame_info *frame)
468 {
469 if (frame->signal_handler_caller)
470 return frame->frame;
471
472 if (! inside_entry_file (frame->pc))
473 return read_memory_unsigned_integer (frame->frame, 4);
474
475 return 0;
476 }
477
478 /* Determine whether the function invocation represented by FRAME does
479 not have a from on the stack associated with it. If it does not,
480 return non-zero, otherwise return zero. */
481
482 int
483 i386_frameless_function_invocation (struct frame_info *frame)
484 {
485 if (frame->signal_handler_caller)
486 return 0;
487
488 return frameless_look_for_prologue (frame);
489 }
490
491 /* Return the saved program counter for FRAME. */
492
493 CORE_ADDR
494 i386_frame_saved_pc (struct frame_info *frame)
495 {
496 /* FIXME: kettenis/2001-05-09: Conditionalizing the next bit of code
497 on SIGCONTEXT_PC_OFFSET and I386V4_SIGTRAMP_SAVED_PC should be
498 considered a temporary hack. I plan to come up with something
499 better when we go multi-arch. */
500 #if defined (SIGCONTEXT_PC_OFFSET) || defined (I386V4_SIGTRAMP_SAVED_PC)
501 if (frame->signal_handler_caller)
502 return sigtramp_saved_pc (frame);
503 #endif
504
505 return read_memory_unsigned_integer (frame->frame + 4, 4);
506 }
507
508 /* Immediately after a function call, return the saved pc. */
509
510 CORE_ADDR
511 i386_saved_pc_after_call (struct frame_info *frame)
512 {
513 return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
514 }
515
516 /* Return number of args passed to a frame.
517 Can return -1, meaning no way to tell. */
518
519 int
520 i386_frame_num_args (struct frame_info *fi)
521 {
522 #if 1
523 return -1;
524 #else
525 /* This loses because not only might the compiler not be popping the
526 args right after the function call, it might be popping args from
527 both this call and a previous one, and we would say there are
528 more args than there really are. */
529
530 int retpc;
531 unsigned char op;
532 struct frame_info *pfi;
533
534 /* On the i386, the instruction following the call could be:
535 popl %ecx - one arg
536 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
537 anything else - zero args. */
538
539 int frameless;
540
541 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
542 if (frameless)
543 /* In the absence of a frame pointer, GDB doesn't get correct
544 values for nameless arguments. Return -1, so it doesn't print
545 any nameless arguments. */
546 return -1;
547
548 pfi = get_prev_frame (fi);
549 if (pfi == 0)
550 {
551 /* NOTE: This can happen if we are looking at the frame for
552 main, because FRAME_CHAIN_VALID won't let us go into start.
553 If we have debugging symbols, that's not really a big deal;
554 it just means it will only show as many arguments to main as
555 are declared. */
556 return -1;
557 }
558 else
559 {
560 retpc = pfi->pc;
561 op = read_memory_integer (retpc, 1);
562 if (op == 0x59) /* pop %ecx */
563 return 1;
564 else if (op == 0x83)
565 {
566 op = read_memory_integer (retpc + 1, 1);
567 if (op == 0xc4)
568 /* addl $<signed imm 8 bits>, %esp */
569 return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
570 else
571 return 0;
572 }
573 else if (op == 0x81) /* `add' with 32 bit immediate. */
574 {
575 op = read_memory_integer (retpc + 1, 1);
576 if (op == 0xc4)
577 /* addl $<imm 32>, %esp */
578 return read_memory_integer (retpc + 2, 4) / 4;
579 else
580 return 0;
581 }
582 else
583 {
584 return 0;
585 }
586 }
587 #endif
588 }
589
590 /* Parse the first few instructions the function to see what registers
591 were stored.
592
593 We handle these cases:
594
595 The startup sequence can be at the start of the function, or the
596 function can start with a branch to startup code at the end.
597
598 %ebp can be set up with either the 'enter' instruction, or "pushl
599 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
600 once used in the System V compiler).
601
602 Local space is allocated just below the saved %ebp by either the
603 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16
604 bit unsigned argument for space to allocate, and the 'addl'
605 instruction could have either a signed byte, or 32 bit immediate.
606
607 Next, the registers used by this function are pushed. With the
608 System V compiler they will always be in the order: %edi, %esi,
609 %ebx (and sometimes a harmless bug causes it to also save but not
610 restore %eax); however, the code below is willing to see the pushes
611 in any order, and will handle up to 8 of them.
612
613 If the setup sequence is at the end of the function, then the next
614 instruction will be a branch back to the start. */
615
616 void
617 i386_frame_init_saved_regs (struct frame_info *fip)
618 {
619 long locals = -1;
620 unsigned char op;
621 CORE_ADDR dummy_bottom;
622 CORE_ADDR addr;
623 CORE_ADDR pc;
624 int i;
625
626 if (fip->saved_regs)
627 return;
628
629 frame_saved_regs_zalloc (fip);
630
631 /* If the frame is the end of a dummy, compute where the beginning
632 would be. */
633 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
634
635 /* Check if the PC points in the stack, in a dummy frame. */
636 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
637 {
638 /* All registers were saved by push_call_dummy. */
639 addr = fip->frame;
640 for (i = 0; i < NUM_REGS; i++)
641 {
642 addr -= REGISTER_RAW_SIZE (i);
643 fip->saved_regs[i] = addr;
644 }
645 return;
646 }
647
648 pc = get_pc_function_start (fip->pc);
649 if (pc != 0)
650 locals = i386_get_frame_setup (pc);
651
652 if (locals >= 0)
653 {
654 addr = fip->frame - 4 - locals;
655 for (i = 0; i < 8; i++)
656 {
657 op = codestream_get ();
658 if (op < 0x50 || op > 0x57)
659 break;
660 #ifdef I386_REGNO_TO_SYMMETRY
661 /* Dynix uses different internal numbering. Ick. */
662 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr;
663 #else
664 fip->saved_regs[op - 0x50] = addr;
665 #endif
666 addr -= 4;
667 }
668 }
669
670 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
671 fip->saved_regs[FP_REGNUM] = fip->frame;
672 }
673
674 /* Return PC of first real instruction. */
675
676 int
677 i386_skip_prologue (int pc)
678 {
679 unsigned char op;
680 int i;
681 static unsigned char pic_pat[6] =
682 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
683 0x5b, /* popl %ebx */
684 };
685 CORE_ADDR pos;
686
687 if (i386_get_frame_setup (pc) < 0)
688 return (pc);
689
690 /* Found valid frame setup -- codestream now points to start of push
691 instructions for saving registers. */
692
693 /* Skip over register saves. */
694 for (i = 0; i < 8; i++)
695 {
696 op = codestream_peek ();
697 /* Break if not `pushl' instrunction. */
698 if (op < 0x50 || op > 0x57)
699 break;
700 codestream_get ();
701 }
702
703 /* The native cc on SVR4 in -K PIC mode inserts the following code
704 to get the address of the global offset table (GOT) into register
705 %ebx
706
707 call 0x0
708 popl %ebx
709 movl %ebx,x(%ebp) (optional)
710 addl y,%ebx
711
712 This code is with the rest of the prologue (at the end of the
713 function), so we have to skip it to get to the first real
714 instruction at the start of the function. */
715
716 pos = codestream_tell ();
717 for (i = 0; i < 6; i++)
718 {
719 op = codestream_get ();
720 if (pic_pat[i] != op)
721 break;
722 }
723 if (i == 6)
724 {
725 unsigned char buf[4];
726 long delta = 6;
727
728 op = codestream_get ();
729 if (op == 0x89) /* movl %ebx, x(%ebp) */
730 {
731 op = codestream_get ();
732 if (op == 0x5d) /* One byte offset from %ebp. */
733 {
734 delta += 3;
735 codestream_read (buf, 1);
736 }
737 else if (op == 0x9d) /* Four byte offset from %ebp. */
738 {
739 delta += 6;
740 codestream_read (buf, 4);
741 }
742 else /* Unexpected instruction. */
743 delta = -1;
744 op = codestream_get ();
745 }
746 /* addl y,%ebx */
747 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
748 {
749 pos += delta + 6;
750 }
751 }
752 codestream_seek (pos);
753
754 i386_follow_jump ();
755
756 return (codestream_tell ());
757 }
758
759 void
760 i386_push_dummy_frame (void)
761 {
762 CORE_ADDR sp = read_register (SP_REGNUM);
763 int regnum;
764 char regbuf[MAX_REGISTER_RAW_SIZE];
765
766 sp = push_word (sp, read_register (PC_REGNUM));
767 sp = push_word (sp, read_register (FP_REGNUM));
768 write_register (FP_REGNUM, sp);
769 for (regnum = 0; regnum < NUM_REGS; regnum++)
770 {
771 read_register_gen (regnum, regbuf);
772 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
773 }
774 write_register (SP_REGNUM, sp);
775 }
776
777 /* Insert the (relative) function address into the call sequence
778 stored at DYMMY. */
779
780 void
781 i386_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
782 struct value **args, struct type *type, int gcc_p)
783 {
784 int from, to, delta, loc;
785
786 loc = (int)(read_register (SP_REGNUM) - CALL_DUMMY_LENGTH);
787 from = loc + 5;
788 to = (int)(fun);
789 delta = to - from;
790
791 *((char *)(dummy) + 1) = (delta & 0xff);
792 *((char *)(dummy) + 2) = ((delta >> 8) & 0xff);
793 *((char *)(dummy) + 3) = ((delta >> 16) & 0xff);
794 *((char *)(dummy) + 4) = ((delta >> 24) & 0xff);
795 }
796
797 void
798 i386_pop_frame (void)
799 {
800 struct frame_info *frame = get_current_frame ();
801 CORE_ADDR fp;
802 int regnum;
803 char regbuf[MAX_REGISTER_RAW_SIZE];
804
805 fp = FRAME_FP (frame);
806 i386_frame_init_saved_regs (frame);
807
808 for (regnum = 0; regnum < NUM_REGS; regnum++)
809 {
810 CORE_ADDR addr;
811 addr = frame->saved_regs[regnum];
812 if (addr)
813 {
814 read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum));
815 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
816 REGISTER_RAW_SIZE (regnum));
817 }
818 }
819 write_register (FP_REGNUM, read_memory_integer (fp, 4));
820 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
821 write_register (SP_REGNUM, fp + 8);
822 flush_cached_frames ();
823 }
824 \f
825
826 #ifdef GET_LONGJMP_TARGET
827
828 /* Figure out where the longjmp will land. Slurp the args out of the
829 stack. We expect the first arg to be a pointer to the jmp_buf
830 structure from which we extract the pc (JB_PC) that we will land
831 at. The pc is copied into PC. This routine returns true on
832 success. */
833
834 int
835 get_longjmp_target (CORE_ADDR *pc)
836 {
837 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
838 CORE_ADDR sp, jb_addr;
839
840 sp = read_register (SP_REGNUM);
841
842 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack. */
843 buf,
844 TARGET_PTR_BIT / TARGET_CHAR_BIT))
845 return 0;
846
847 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
848
849 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
850 TARGET_PTR_BIT / TARGET_CHAR_BIT))
851 return 0;
852
853 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
854
855 return 1;
856 }
857
858 #endif /* GET_LONGJMP_TARGET */
859 \f
860
861 CORE_ADDR
862 i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
863 int struct_return, CORE_ADDR struct_addr)
864 {
865 sp = default_push_arguments (nargs, args, sp, struct_return, struct_addr);
866
867 if (struct_return)
868 {
869 char buf[4];
870
871 sp -= 4;
872 store_address (buf, 4, struct_addr);
873 write_memory (sp, buf, 4);
874 }
875
876 return sp;
877 }
878
879 void
880 i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
881 {
882 /* Do nothing. Everything was already done by i386_push_arguments. */
883 }
884
885 /* These registers are used for returning integers (and on some
886 targets also for returning `struct' and `union' values when their
887 size and alignment match an integer type). */
888 #define LOW_RETURN_REGNUM 0 /* %eax */
889 #define HIGH_RETURN_REGNUM 2 /* %edx */
890
891 /* Extract from an array REGBUF containing the (raw) register state, a
892 function return value of TYPE, and copy that, in virtual format,
893 into VALBUF. */
894
895 void
896 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
897 {
898 int len = TYPE_LENGTH (type);
899
900 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
901 && TYPE_NFIELDS (type) == 1)
902 {
903 i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regbuf, valbuf);
904 return;
905 }
906
907 if (TYPE_CODE (type) == TYPE_CODE_FLT)
908 {
909 if (NUM_FREGS == 0)
910 {
911 warning ("Cannot find floating-point return value.");
912 memset (valbuf, 0, len);
913 return;
914 }
915
916 /* Floating-point return values can be found in %st(0). Convert
917 its contents to the desired type. This is probably not
918 exactly how it would happen on the target itself, but it is
919 the best we can do. */
920 convert_typed_floating (&regbuf[REGISTER_BYTE (FP0_REGNUM)],
921 builtin_type_i387_ext, valbuf, type);
922 }
923 else
924 {
925 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
926 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
927
928 if (len <= low_size)
929 memcpy (valbuf, &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
930 else if (len <= (low_size + high_size))
931 {
932 memcpy (valbuf,
933 &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
934 memcpy (valbuf + low_size,
935 &regbuf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
936 }
937 else
938 internal_error (__FILE__, __LINE__,
939 "Cannot extract return value of %d bytes long.", len);
940 }
941 }
942
943 /* Write into the appropriate registers a function return value stored
944 in VALBUF of type TYPE, given in virtual format. */
945
946 void
947 i386_store_return_value (struct type *type, char *valbuf)
948 {
949 int len = TYPE_LENGTH (type);
950
951 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
952 && TYPE_NFIELDS (type) == 1)
953 {
954 i386_store_return_value (TYPE_FIELD_TYPE (type, 0), valbuf);
955 return;
956 }
957
958 if (TYPE_CODE (type) == TYPE_CODE_FLT)
959 {
960 unsigned int fstat;
961 char buf[FPU_REG_RAW_SIZE];
962
963 if (NUM_FREGS == 0)
964 {
965 warning ("Cannot set floating-point return value.");
966 return;
967 }
968
969 /* Returning floating-point values is a bit tricky. Apart from
970 storing the return value in %st(0), we have to simulate the
971 state of the FPU at function return point. */
972
973 /* Convert the value found in VALBUF to the extended
974 floating-point format used by the FPU. This is probably
975 not exactly how it would happen on the target itself, but
976 it is the best we can do. */
977 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
978 write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
979 FPU_REG_RAW_SIZE);
980
981 /* Set the top of the floating-point register stack to 7. The
982 actual value doesn't really matter, but 7 is what a normal
983 function return would end up with if the program started out
984 with a freshly initialized FPU. */
985 fstat = read_register (FSTAT_REGNUM);
986 fstat |= (7 << 11);
987 write_register (FSTAT_REGNUM, fstat);
988
989 /* Mark %st(1) through %st(7) as empty. Since we set the top of
990 the floating-point register stack to 7, the appropriate value
991 for the tag word is 0x3fff. */
992 write_register (FTAG_REGNUM, 0x3fff);
993 }
994 else
995 {
996 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
997 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
998
999 if (len <= low_size)
1000 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), valbuf, len);
1001 else if (len <= (low_size + high_size))
1002 {
1003 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM),
1004 valbuf, low_size);
1005 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM),
1006 valbuf + low_size, len - low_size);
1007 }
1008 else
1009 internal_error (__FILE__, __LINE__,
1010 "Cannot store return value of %d bytes long.", len);
1011 }
1012 }
1013
1014 /* Extract from an array REGBUF containing the (raw) register state
1015 the address in which a function should return its structure value,
1016 as a CORE_ADDR. */
1017
1018 CORE_ADDR
1019 i386_extract_struct_value_address (char *regbuf)
1020 {
1021 return extract_address (&regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)],
1022 REGISTER_RAW_SIZE (LOW_RETURN_REGNUM));
1023 }
1024 \f
1025
1026 /* Return the GDB type object for the "standard" data type of data in
1027 register REGNUM. Perhaps %esi and %edi should go here, but
1028 potentially they could be used for things other than address. */
1029
1030 struct type *
1031 i386_register_virtual_type (int regnum)
1032 {
1033 if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM)
1034 return lookup_pointer_type (builtin_type_void);
1035
1036 if (IS_FP_REGNUM (regnum))
1037 return builtin_type_i387_ext;
1038
1039 if (IS_SSE_REGNUM (regnum))
1040 return builtin_type_v4sf;
1041
1042 return builtin_type_int;
1043 }
1044
1045 /* Return true iff register REGNUM's virtual format is different from
1046 its raw format. Note that this definition assumes that the host
1047 supports IEEE 32-bit floats, since it doesn't say that SSE
1048 registers need conversion. Even if we can't find a counterexample,
1049 this is still sloppy. */
1050
1051 int
1052 i386_register_convertible (int regnum)
1053 {
1054 return IS_FP_REGNUM (regnum);
1055 }
1056
1057 /* Convert data from raw format for register REGNUM in buffer FROM to
1058 virtual format with type TYPE in buffer TO. */
1059
1060 void
1061 i386_register_convert_to_virtual (int regnum, struct type *type,
1062 char *from, char *to)
1063 {
1064 gdb_assert (IS_FP_REGNUM (regnum));
1065
1066 /* We only support floating-point values. */
1067 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1068 {
1069 warning ("Cannot convert floating-point register value "
1070 "to non-floating-point type.");
1071 memset (to, 0, TYPE_LENGTH (type));
1072 return;
1073 }
1074
1075 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1076 the extended floating-point format used by the FPU. */
1077 convert_typed_floating (from, builtin_type_i387_ext, to, type);
1078 }
1079
1080 /* Convert data from virtual format with type TYPE in buffer FROM to
1081 raw format for register REGNUM in buffer TO. */
1082
1083 void
1084 i386_register_convert_to_raw (struct type *type, int regnum,
1085 char *from, char *to)
1086 {
1087 gdb_assert (IS_FP_REGNUM (regnum));
1088
1089 /* We only support floating-point values. */
1090 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1091 {
1092 warning ("Cannot convert non-floating-point type "
1093 "to floating-point register value.");
1094 memset (to, 0, TYPE_LENGTH (type));
1095 return;
1096 }
1097
1098 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1099 to the extended floating-point format used by the FPU. */
1100 convert_typed_floating (from, type, to, builtin_type_i387_ext);
1101 }
1102 \f
1103
1104 #ifdef I386V4_SIGTRAMP_SAVED_PC
1105 /* Get saved user PC for sigtramp from the pushed ucontext on the
1106 stack for all three variants of SVR4 sigtramps. */
1107
1108 CORE_ADDR
1109 i386v4_sigtramp_saved_pc (struct frame_info *frame)
1110 {
1111 CORE_ADDR saved_pc_offset = 4;
1112 char *name = NULL;
1113
1114 find_pc_partial_function (frame->pc, &name, NULL, NULL);
1115 if (name)
1116 {
1117 if (STREQ (name, "_sigreturn"))
1118 saved_pc_offset = 132 + 14 * 4;
1119 else if (STREQ (name, "_sigacthandler"))
1120 saved_pc_offset = 80 + 14 * 4;
1121 else if (STREQ (name, "sigvechandler"))
1122 saved_pc_offset = 120 + 14 * 4;
1123 }
1124
1125 if (frame->next)
1126 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
1127 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
1128 }
1129 #endif /* I386V4_SIGTRAMP_SAVED_PC */
1130 \f
1131
1132 #ifdef STATIC_TRANSFORM_NAME
1133 /* SunPRO encodes the static variables. This is not related to C++
1134 mangling, it is done for C too. */
1135
1136 char *
1137 sunpro_static_transform_name (char *name)
1138 {
1139 char *p;
1140 if (IS_STATIC_TRANSFORM_NAME (name))
1141 {
1142 /* For file-local statics there will be a period, a bunch of
1143 junk (the contents of which match a string given in the
1144 N_OPT), a period and the name. For function-local statics
1145 there will be a bunch of junk (which seems to change the
1146 second character from 'A' to 'B'), a period, the name of the
1147 function, and the name. So just skip everything before the
1148 last period. */
1149 p = strrchr (name, '.');
1150 if (p != NULL)
1151 name = p + 1;
1152 }
1153 return name;
1154 }
1155 #endif /* STATIC_TRANSFORM_NAME */
1156 \f
1157
1158 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1159
1160 CORE_ADDR
1161 skip_trampoline_code (CORE_ADDR pc, char *name)
1162 {
1163 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
1164 {
1165 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
1166 struct minimal_symbol *indsym =
1167 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
1168 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
1169
1170 if (symname)
1171 {
1172 if (strncmp (symname, "__imp_", 6) == 0
1173 || strncmp (symname, "_imp_", 5) == 0)
1174 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1175 }
1176 }
1177 return 0; /* Not a trampoline. */
1178 }
1179 \f
1180
1181 /* We have two flavours of disassembly. The machinery on this page
1182 deals with switching between those. */
1183
1184 static int
1185 gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
1186 {
1187 if (disassembly_flavor == att_flavor)
1188 return print_insn_i386_att (memaddr, info);
1189 else if (disassembly_flavor == intel_flavor)
1190 return print_insn_i386_intel (memaddr, info);
1191 /* Never reached -- disassembly_flavour is always either att_flavor
1192 or intel_flavor. */
1193 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1194 }
1195
1196 \f
1197
1198 /* Provide a prototype to silence -Wmissing-prototypes. */
1199 void _initialize_i386_tdep (void);
1200
1201 void
1202 _initialize_i386_tdep (void)
1203 {
1204 /* Initialize the table saying where each register starts in the
1205 register file. */
1206 {
1207 int i, offset;
1208
1209 offset = 0;
1210 for (i = 0; i < MAX_NUM_REGS; i++)
1211 {
1212 i386_register_offset[i] = offset;
1213 offset += i386_register_size[i];
1214 }
1215 }
1216
1217 tm_print_insn = gdb_print_insn_i386;
1218 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1219
1220 /* Add the variable that controls the disassembly flavor. */
1221 {
1222 struct cmd_list_element *new_cmd;
1223
1224 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1225 valid_flavors,
1226 &disassembly_flavor,
1227 "\
1228 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1229 and the default value is \"att\".",
1230 &setlist);
1231 add_show_from_set (new_cmd, &showlist);
1232 }
1233 }