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