* i386-tdep.h (struct gdbarch_tdep): Add `os_ident' member.
[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 /* Figure out where the longjmp will land. Slurp the args out of the
843 stack. We expect the first arg to be a pointer to the jmp_buf
844 structure from which we extract the pc (JB_PC) that we will land
845 at. The pc is copied into PC. This routine returns true on
846 success. */
847
848 int
849 get_longjmp_target (CORE_ADDR *pc)
850 {
851 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
852 CORE_ADDR sp, jb_addr;
853
854 sp = read_register (SP_REGNUM);
855
856 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack. */
857 buf,
858 TARGET_PTR_BIT / TARGET_CHAR_BIT))
859 return 0;
860
861 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
862
863 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
864 TARGET_PTR_BIT / TARGET_CHAR_BIT))
865 return 0;
866
867 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
868
869 return 1;
870 }
871
872 #endif /* GET_LONGJMP_TARGET */
873 \f
874
875 CORE_ADDR
876 i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
877 int struct_return, CORE_ADDR struct_addr)
878 {
879 sp = default_push_arguments (nargs, args, sp, struct_return, struct_addr);
880
881 if (struct_return)
882 {
883 char buf[4];
884
885 sp -= 4;
886 store_address (buf, 4, struct_addr);
887 write_memory (sp, buf, 4);
888 }
889
890 return sp;
891 }
892
893 void
894 i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
895 {
896 /* Do nothing. Everything was already done by i386_push_arguments. */
897 }
898
899 /* These registers are used for returning integers (and on some
900 targets also for returning `struct' and `union' values when their
901 size and alignment match an integer type). */
902 #define LOW_RETURN_REGNUM 0 /* %eax */
903 #define HIGH_RETURN_REGNUM 2 /* %edx */
904
905 /* Extract from an array REGBUF containing the (raw) register state, a
906 function return value of TYPE, and copy that, in virtual format,
907 into VALBUF. */
908
909 void
910 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
911 {
912 int len = TYPE_LENGTH (type);
913
914 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
915 && TYPE_NFIELDS (type) == 1)
916 {
917 i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regbuf, valbuf);
918 return;
919 }
920
921 if (TYPE_CODE (type) == TYPE_CODE_FLT)
922 {
923 if (NUM_FREGS == 0)
924 {
925 warning ("Cannot find floating-point return value.");
926 memset (valbuf, 0, len);
927 return;
928 }
929
930 /* Floating-point return values can be found in %st(0). Convert
931 its contents to the desired type. This is probably not
932 exactly how it would happen on the target itself, but it is
933 the best we can do. */
934 convert_typed_floating (&regbuf[REGISTER_BYTE (FP0_REGNUM)],
935 builtin_type_i387_ext, valbuf, type);
936 }
937 else
938 {
939 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
940 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
941
942 if (len <= low_size)
943 memcpy (valbuf, &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
944 else if (len <= (low_size + high_size))
945 {
946 memcpy (valbuf,
947 &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
948 memcpy (valbuf + low_size,
949 &regbuf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
950 }
951 else
952 internal_error (__FILE__, __LINE__,
953 "Cannot extract return value of %d bytes long.", len);
954 }
955 }
956
957 /* Write into the appropriate registers a function return value stored
958 in VALBUF of type TYPE, given in virtual format. */
959
960 void
961 i386_store_return_value (struct type *type, char *valbuf)
962 {
963 int len = TYPE_LENGTH (type);
964
965 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
966 && TYPE_NFIELDS (type) == 1)
967 {
968 i386_store_return_value (TYPE_FIELD_TYPE (type, 0), valbuf);
969 return;
970 }
971
972 if (TYPE_CODE (type) == TYPE_CODE_FLT)
973 {
974 unsigned int fstat;
975 char buf[FPU_REG_RAW_SIZE];
976
977 if (NUM_FREGS == 0)
978 {
979 warning ("Cannot set floating-point return value.");
980 return;
981 }
982
983 /* Returning floating-point values is a bit tricky. Apart from
984 storing the return value in %st(0), we have to simulate the
985 state of the FPU at function return point. */
986
987 /* Convert the value found in VALBUF to the extended
988 floating-point format used by the FPU. This is probably
989 not exactly how it would happen on the target itself, but
990 it is the best we can do. */
991 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
992 write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
993 FPU_REG_RAW_SIZE);
994
995 /* Set the top of the floating-point register stack to 7. The
996 actual value doesn't really matter, but 7 is what a normal
997 function return would end up with if the program started out
998 with a freshly initialized FPU. */
999 fstat = read_register (FSTAT_REGNUM);
1000 fstat |= (7 << 11);
1001 write_register (FSTAT_REGNUM, fstat);
1002
1003 /* Mark %st(1) through %st(7) as empty. Since we set the top of
1004 the floating-point register stack to 7, the appropriate value
1005 for the tag word is 0x3fff. */
1006 write_register (FTAG_REGNUM, 0x3fff);
1007 }
1008 else
1009 {
1010 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
1011 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
1012
1013 if (len <= low_size)
1014 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), valbuf, len);
1015 else if (len <= (low_size + high_size))
1016 {
1017 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM),
1018 valbuf, low_size);
1019 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM),
1020 valbuf + low_size, len - low_size);
1021 }
1022 else
1023 internal_error (__FILE__, __LINE__,
1024 "Cannot store return value of %d bytes long.", len);
1025 }
1026 }
1027
1028 /* Extract from an array REGBUF containing the (raw) register state
1029 the address in which a function should return its structure value,
1030 as a CORE_ADDR. */
1031
1032 CORE_ADDR
1033 i386_extract_struct_value_address (char *regbuf)
1034 {
1035 return extract_address (&regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)],
1036 REGISTER_RAW_SIZE (LOW_RETURN_REGNUM));
1037 }
1038 \f
1039
1040 /* Return the GDB type object for the "standard" data type of data in
1041 register REGNUM. Perhaps %esi and %edi should go here, but
1042 potentially they could be used for things other than address. */
1043
1044 struct type *
1045 i386_register_virtual_type (int regnum)
1046 {
1047 if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM)
1048 return lookup_pointer_type (builtin_type_void);
1049
1050 if (IS_FP_REGNUM (regnum))
1051 return builtin_type_i387_ext;
1052
1053 if (IS_SSE_REGNUM (regnum))
1054 return builtin_type_v4sf;
1055
1056 return builtin_type_int;
1057 }
1058
1059 /* Return true iff register REGNUM's virtual format is different from
1060 its raw format. Note that this definition assumes that the host
1061 supports IEEE 32-bit floats, since it doesn't say that SSE
1062 registers need conversion. Even if we can't find a counterexample,
1063 this is still sloppy. */
1064
1065 int
1066 i386_register_convertible (int regnum)
1067 {
1068 return IS_FP_REGNUM (regnum);
1069 }
1070
1071 /* Convert data from raw format for register REGNUM in buffer FROM to
1072 virtual format with type TYPE in buffer TO. */
1073
1074 void
1075 i386_register_convert_to_virtual (int regnum, struct type *type,
1076 char *from, char *to)
1077 {
1078 gdb_assert (IS_FP_REGNUM (regnum));
1079
1080 /* We only support floating-point values. */
1081 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1082 {
1083 warning ("Cannot convert floating-point register value "
1084 "to non-floating-point type.");
1085 memset (to, 0, TYPE_LENGTH (type));
1086 return;
1087 }
1088
1089 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1090 the extended floating-point format used by the FPU. */
1091 convert_typed_floating (from, builtin_type_i387_ext, to, type);
1092 }
1093
1094 /* Convert data from virtual format with type TYPE in buffer FROM to
1095 raw format for register REGNUM in buffer TO. */
1096
1097 void
1098 i386_register_convert_to_raw (struct type *type, int regnum,
1099 char *from, char *to)
1100 {
1101 gdb_assert (IS_FP_REGNUM (regnum));
1102
1103 /* We only support floating-point values. */
1104 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1105 {
1106 warning ("Cannot convert non-floating-point type "
1107 "to floating-point register value.");
1108 memset (to, 0, TYPE_LENGTH (type));
1109 return;
1110 }
1111
1112 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1113 to the extended floating-point format used by the FPU. */
1114 convert_typed_floating (from, type, to, builtin_type_i387_ext);
1115 }
1116 \f
1117
1118 #ifdef I386V4_SIGTRAMP_SAVED_PC
1119 /* Get saved user PC for sigtramp from the pushed ucontext on the
1120 stack for all three variants of SVR4 sigtramps. */
1121
1122 CORE_ADDR
1123 i386v4_sigtramp_saved_pc (struct frame_info *frame)
1124 {
1125 CORE_ADDR saved_pc_offset = 4;
1126 char *name = NULL;
1127
1128 find_pc_partial_function (frame->pc, &name, NULL, NULL);
1129 if (name)
1130 {
1131 if (STREQ (name, "_sigreturn"))
1132 saved_pc_offset = 132 + 14 * 4;
1133 else if (STREQ (name, "_sigacthandler"))
1134 saved_pc_offset = 80 + 14 * 4;
1135 else if (STREQ (name, "sigvechandler"))
1136 saved_pc_offset = 120 + 14 * 4;
1137 }
1138
1139 if (frame->next)
1140 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
1141 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
1142 }
1143 #endif /* I386V4_SIGTRAMP_SAVED_PC */
1144 \f
1145
1146 #ifdef STATIC_TRANSFORM_NAME
1147 /* SunPRO encodes the static variables. This is not related to C++
1148 mangling, it is done for C too. */
1149
1150 char *
1151 sunpro_static_transform_name (char *name)
1152 {
1153 char *p;
1154 if (IS_STATIC_TRANSFORM_NAME (name))
1155 {
1156 /* For file-local statics there will be a period, a bunch of
1157 junk (the contents of which match a string given in the
1158 N_OPT), a period and the name. For function-local statics
1159 there will be a bunch of junk (which seems to change the
1160 second character from 'A' to 'B'), a period, the name of the
1161 function, and the name. So just skip everything before the
1162 last period. */
1163 p = strrchr (name, '.');
1164 if (p != NULL)
1165 name = p + 1;
1166 }
1167 return name;
1168 }
1169 #endif /* STATIC_TRANSFORM_NAME */
1170 \f
1171
1172 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1173
1174 CORE_ADDR
1175 skip_trampoline_code (CORE_ADDR pc, char *name)
1176 {
1177 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
1178 {
1179 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
1180 struct minimal_symbol *indsym =
1181 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
1182 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
1183
1184 if (symname)
1185 {
1186 if (strncmp (symname, "__imp_", 6) == 0
1187 || strncmp (symname, "_imp_", 5) == 0)
1188 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1189 }
1190 }
1191 return 0; /* Not a trampoline. */
1192 }
1193 \f
1194
1195 /* We have two flavours of disassembly. The machinery on this page
1196 deals with switching between those. */
1197
1198 static int
1199 gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
1200 {
1201 if (disassembly_flavor == att_flavor)
1202 return print_insn_i386_att (memaddr, info);
1203 else if (disassembly_flavor == intel_flavor)
1204 return print_insn_i386_intel (memaddr, info);
1205 /* Never reached -- disassembly_flavour is always either att_flavor
1206 or intel_flavor. */
1207 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1208 }
1209
1210 \f
1211 static void
1212 process_note_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
1213 {
1214 int *os_ident_ptr = obj;
1215 const char *name;
1216 unsigned int sect_size;
1217
1218 name = bfd_get_section_name (abfd, sect);
1219 sect_size = bfd_section_size (abfd, sect);
1220 if (strcmp (name, ".note.ABI-tag") == 0 && sect_size > 0)
1221 {
1222 unsigned int name_length, data_length, note_type;
1223 char *note = alloca (sect_size);
1224
1225 bfd_get_section_contents (abfd, sect, note,
1226 (file_ptr) 0, (bfd_size_type) sect_size);
1227
1228 name_length = bfd_h_get_32 (abfd, note);
1229 data_length = bfd_h_get_32 (abfd, note + 4);
1230 note_type = bfd_h_get_32 (abfd, note + 8);
1231
1232 if (name_length == 4 && data_length == 16 && note_type == 1
1233 && strcmp (note + 12, "GNU") == 0)
1234 {
1235 int os_number = bfd_h_get_32 (abfd, note + 16);
1236
1237 /* The case numbers are from abi-tags in glibc. */
1238 switch (os_number)
1239 {
1240 case 0:
1241 *os_ident_ptr = ELFOSABI_LINUX;
1242 break;
1243 case 1:
1244 *os_ident_ptr = ELFOSABI_HURD;
1245 break;
1246 case 2:
1247 *os_ident_ptr = ELFOSABI_SOLARIS;
1248 break;
1249 default:
1250 internal_error (__FILE__, __LINE__,
1251 "process_note_abi_sections: "
1252 "unknown OS number %d", os_number);
1253 break;
1254 }
1255 }
1256 }
1257 }
1258
1259 struct gdbarch *
1260 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1261 {
1262 struct gdbarch_tdep *tdep;
1263 struct gdbarch *gdbarch;
1264 int os_ident;
1265
1266 if (info.abfd != NULL
1267 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
1268 {
1269 os_ident = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
1270
1271 /* If os_ident is 0, it is not necessarily the case that we're
1272 on a SYSV system. (ELFOSABI_NONE is defined to be 0.)
1273 GNU/Linux uses a note section to record OS/ABI info, but
1274 leaves e_ident[EI_OSABI] zero. So we have to check for note
1275 sections too. */
1276 if (os_ident == ELFOSABI_NONE)
1277 bfd_map_over_sections (info.abfd,
1278 process_note_abi_tag_sections,
1279 &os_ident);
1280
1281 /* If that didn't help us, revert to some non-standard checks. */
1282 if (os_ident == ELFOSABI_NONE)
1283 {
1284 /* FreeBSD folks are naughty; they stored the string
1285 "FreeBSD" in the padding of the e_ident field of the ELF
1286 header. */
1287 if (strcmp (&elf_elfheader (info.abfd)->e_ident[8], "FreeBSD") == 0)
1288 os_ident = ELFOSABI_FREEBSD;
1289 }
1290 }
1291 else
1292 os_ident = -1;
1293
1294 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1295 arches != NULL;
1296 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1297 {
1298 if (gdbarch_tdep (current_gdbarch)->os_ident != os_ident)
1299 continue;
1300 return arches->gdbarch;
1301 }
1302
1303 /* Allocate space for the new architecture. */
1304 tdep = XMALLOC (struct gdbarch_tdep);
1305 gdbarch = gdbarch_alloc (&info, tdep);
1306
1307 tdep->os_ident = os_ident;
1308
1309 /* FIXME: kettenis/2001-11-24: Although not all IA-32 processors
1310 have the SSE registers, it's easier to set the default to 8. */
1311 tdep->num_xmm_regs = 8;
1312
1313 set_gdbarch_use_generic_dummy_frames (gdbarch, 0);
1314
1315 /* Call dummy code. */
1316 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1317 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 5);
1318 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1319 set_gdbarch_call_dummy_p (gdbarch, 1);
1320 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1321
1322 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1323 set_gdbarch_push_arguments (gdbarch, i386_push_arguments);
1324
1325 set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_on_stack);
1326
1327 /* NOTE: tm-i386nw.h and tm-i386v4.h override this. */
1328 set_gdbarch_frame_chain_valid (gdbarch, file_frame_chain_valid);
1329
1330 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-linux.h,
1331 tm-ptx.h, tm-symmetry.h currently override this. Sigh. */
1332 set_gdbarch_num_regs (gdbarch, NUM_GREGS + NUM_FREGS + NUM_SSE_REGS);
1333
1334 return gdbarch;
1335 }
1336
1337 /* Provide a prototype to silence -Wmissing-prototypes. */
1338 void _initialize_i386_tdep (void);
1339
1340 void
1341 _initialize_i386_tdep (void)
1342 {
1343 register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
1344
1345 /* Initialize the table saying where each register starts in the
1346 register file. */
1347 {
1348 int i, offset;
1349
1350 offset = 0;
1351 for (i = 0; i < MAX_NUM_REGS; i++)
1352 {
1353 i386_register_offset[i] = offset;
1354 offset += i386_register_size[i];
1355 }
1356 }
1357
1358 tm_print_insn = gdb_print_insn_i386;
1359 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1360
1361 /* Add the variable that controls the disassembly flavor. */
1362 {
1363 struct cmd_list_element *new_cmd;
1364
1365 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1366 valid_flavors,
1367 &disassembly_flavor,
1368 "\
1369 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1370 and the default value is \"att\".",
1371 &setlist);
1372 add_show_from_set (new_cmd, &showlist);
1373 }
1374 }
This page took 0.061139 seconds and 5 git commands to generate.