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