2002-08-01 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002 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 "symfile.h"
31 #include "symtab.h"
32 #include "gdbcmd.h"
33 #include "command.h"
34 #include "arch-utils.h"
35 #include "regcache.h"
36 #include "doublest.h"
37 #include "value.h"
38 #include "gdb_assert.h"
39
40 #include "i386-tdep.h"
41
42 /* Names of the registers. The first 10 registers match the register
43 numbering scheme used by GCC for stabs and DWARF. */
44 static char *i386_register_names[] =
45 {
46 "eax", "ecx", "edx", "ebx",
47 "esp", "ebp", "esi", "edi",
48 "eip", "eflags", "cs", "ss",
49 "ds", "es", "fs", "gs",
50 "st0", "st1", "st2", "st3",
51 "st4", "st5", "st6", "st7",
52 "fctrl", "fstat", "ftag", "fiseg",
53 "fioff", "foseg", "fooff", "fop",
54 "xmm0", "xmm1", "xmm2", "xmm3",
55 "xmm4", "xmm5", "xmm6", "xmm7",
56 "mxcsr"
57 };
58
59 /* i386_register_offset[i] is the offset into the register file of the
60 start of register number i. We initialize this from
61 i386_register_size. */
62 static int i386_register_offset[I386_SSE_NUM_REGS];
63
64 /* i386_register_size[i] is the number of bytes of storage in GDB's
65 register array occupied by register i. */
66 static int i386_register_size[I386_SSE_NUM_REGS] = {
67 4, 4, 4, 4,
68 4, 4, 4, 4,
69 4, 4, 4, 4,
70 4, 4, 4, 4,
71 10, 10, 10, 10,
72 10, 10, 10, 10,
73 4, 4, 4, 4,
74 4, 4, 4, 4,
75 16, 16, 16, 16,
76 16, 16, 16, 16,
77 4
78 };
79
80 /* Return the name of register REG. */
81
82 const char *
83 i386_register_name (int reg)
84 {
85 if (reg < 0)
86 return NULL;
87 if (reg >= sizeof (i386_register_names) / sizeof (*i386_register_names))
88 return NULL;
89
90 return i386_register_names[reg];
91 }
92
93 /* Return the offset into the register array of the start of register
94 number REG. */
95 int
96 i386_register_byte (int reg)
97 {
98 return i386_register_offset[reg];
99 }
100
101 /* Return the number of bytes of storage in GDB's register array
102 occupied by register REG. */
103
104 int
105 i386_register_raw_size (int reg)
106 {
107 return i386_register_size[reg];
108 }
109
110 /* Convert stabs register number REG to the appropriate register
111 number used by GDB. */
112
113 static int
114 i386_stab_reg_to_regnum (int reg)
115 {
116 /* This implements what GCC calls the "default" register map. */
117 if (reg >= 0 && reg <= 7)
118 {
119 /* General registers. */
120 return reg;
121 }
122 else if (reg >= 12 && reg <= 19)
123 {
124 /* Floating-point registers. */
125 return reg - 12 + FP0_REGNUM;
126 }
127 else if (reg >= 21 && reg <= 28)
128 {
129 /* SSE registers. */
130 return reg - 21 + XMM0_REGNUM;
131 }
132 else if (reg >= 29 && reg <= 36)
133 {
134 /* MMX registers. */
135 /* FIXME: kettenis/2001-07-28: Should we have the MMX registers
136 as pseudo-registers? */
137 return reg - 29 + FP0_REGNUM;
138 }
139
140 /* This will hopefully provoke a warning. */
141 return NUM_REGS + NUM_PSEUDO_REGS;
142 }
143
144 /* Convert DWARF register number REG to the appropriate register
145 number used by GDB. */
146
147 static int
148 i386_dwarf_reg_to_regnum (int reg)
149 {
150 /* The DWARF register numbering includes %eip and %eflags, and
151 numbers the floating point registers differently. */
152 if (reg >= 0 && reg <= 9)
153 {
154 /* General registers. */
155 return reg;
156 }
157 else if (reg >= 11 && reg <= 18)
158 {
159 /* Floating-point registers. */
160 return reg - 11 + FP0_REGNUM;
161 }
162 else if (reg >= 21)
163 {
164 /* The SSE and MMX registers have identical numbers as in stabs. */
165 return i386_stab_reg_to_regnum (reg);
166 }
167
168 /* This will hopefully provoke a warning. */
169 return NUM_REGS + NUM_PSEUDO_REGS;
170 }
171 \f
172
173 /* This is the variable that is set with "set disassembly-flavor", and
174 its legitimate values. */
175 static const char att_flavor[] = "att";
176 static const char intel_flavor[] = "intel";
177 static const char *valid_flavors[] =
178 {
179 att_flavor,
180 intel_flavor,
181 NULL
182 };
183 static const char *disassembly_flavor = att_flavor;
184
185 /* Stdio style buffering was used to minimize calls to ptrace, but
186 this buffering did not take into account that the code section
187 being accessed may not be an even number of buffers long (even if
188 the buffer is only sizeof(int) long). In cases where the code
189 section size happened to be a non-integral number of buffers long,
190 attempting to read the last buffer would fail. Simply using
191 target_read_memory and ignoring errors, rather than read_memory, is
192 not the correct solution, since legitimate access errors would then
193 be totally ignored. To properly handle this situation and continue
194 to use buffering would require that this code be able to determine
195 the minimum code section size granularity (not the alignment of the
196 section itself, since the actual failing case that pointed out this
197 problem had a section alignment of 4 but was not a multiple of 4
198 bytes long), on a target by target basis, and then adjust it's
199 buffer size accordingly. This is messy, but potentially feasible.
200 It probably needs the bfd library's help and support. For now, the
201 buffer size is set to 1. (FIXME -fnf) */
202
203 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
204 static CORE_ADDR codestream_next_addr;
205 static CORE_ADDR codestream_addr;
206 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
207 static int codestream_off;
208 static int codestream_cnt;
209
210 #define codestream_tell() (codestream_addr + codestream_off)
211 #define codestream_peek() \
212 (codestream_cnt == 0 ? \
213 codestream_fill(1) : codestream_buf[codestream_off])
214 #define codestream_get() \
215 (codestream_cnt-- == 0 ? \
216 codestream_fill(0) : codestream_buf[codestream_off++])
217
218 static unsigned char
219 codestream_fill (int peek_flag)
220 {
221 codestream_addr = codestream_next_addr;
222 codestream_next_addr += CODESTREAM_BUFSIZ;
223 codestream_off = 0;
224 codestream_cnt = CODESTREAM_BUFSIZ;
225 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
226
227 if (peek_flag)
228 return (codestream_peek ());
229 else
230 return (codestream_get ());
231 }
232
233 static void
234 codestream_seek (CORE_ADDR place)
235 {
236 codestream_next_addr = place / CODESTREAM_BUFSIZ;
237 codestream_next_addr *= CODESTREAM_BUFSIZ;
238 codestream_cnt = 0;
239 codestream_fill (1);
240 while (codestream_tell () != place)
241 codestream_get ();
242 }
243
244 static void
245 codestream_read (unsigned char *buf, int count)
246 {
247 unsigned char *p;
248 int i;
249 p = buf;
250 for (i = 0; i < count; i++)
251 *p++ = codestream_get ();
252 }
253 \f
254
255 /* If the next instruction is a jump, move to its target. */
256
257 static void
258 i386_follow_jump (void)
259 {
260 unsigned char buf[4];
261 long delta;
262
263 int data16;
264 CORE_ADDR pos;
265
266 pos = codestream_tell ();
267
268 data16 = 0;
269 if (codestream_peek () == 0x66)
270 {
271 codestream_get ();
272 data16 = 1;
273 }
274
275 switch (codestream_get ())
276 {
277 case 0xe9:
278 /* Relative jump: if data16 == 0, disp32, else disp16. */
279 if (data16)
280 {
281 codestream_read (buf, 2);
282 delta = extract_signed_integer (buf, 2);
283
284 /* Include the size of the jmp instruction (including the
285 0x66 prefix). */
286 pos += delta + 4;
287 }
288 else
289 {
290 codestream_read (buf, 4);
291 delta = extract_signed_integer (buf, 4);
292
293 pos += delta + 5;
294 }
295 break;
296 case 0xeb:
297 /* Relative jump, disp8 (ignore data16). */
298 codestream_read (buf, 1);
299 /* Sign-extend it. */
300 delta = extract_signed_integer (buf, 1);
301
302 pos += delta + 2;
303 break;
304 }
305 codestream_seek (pos);
306 }
307
308 /* Find & return the amount a local space allocated, and advance the
309 codestream to the first register push (if any).
310
311 If the entry sequence doesn't make sense, return -1, and leave
312 codestream pointer at a random spot. */
313
314 static long
315 i386_get_frame_setup (CORE_ADDR pc)
316 {
317 unsigned char op;
318
319 codestream_seek (pc);
320
321 i386_follow_jump ();
322
323 op = codestream_get ();
324
325 if (op == 0x58) /* popl %eax */
326 {
327 /* This function must start with
328
329 popl %eax 0x58
330 xchgl %eax, (%esp) 0x87 0x04 0x24
331 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
332
333 (the System V compiler puts out the second `xchg'
334 instruction, and the assembler doesn't try to optimize it, so
335 the 'sib' form gets generated). This sequence is used to get
336 the address of the return buffer for a function that returns
337 a structure. */
338 int pos;
339 unsigned char buf[4];
340 static unsigned char proto1[3] = { 0x87, 0x04, 0x24 };
341 static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
342
343 pos = codestream_tell ();
344 codestream_read (buf, 4);
345 if (memcmp (buf, proto1, 3) == 0)
346 pos += 3;
347 else if (memcmp (buf, proto2, 4) == 0)
348 pos += 4;
349
350 codestream_seek (pos);
351 op = codestream_get (); /* Update next opcode. */
352 }
353
354 if (op == 0x68 || op == 0x6a)
355 {
356 /* This function may start with
357
358 pushl constant
359 call _probe
360 addl $4, %esp
361
362 followed by
363
364 pushl %ebp
365
366 etc. */
367 int pos;
368 unsigned char buf[8];
369
370 /* Skip past the `pushl' instruction; it has either a one-byte
371 or a four-byte operand, depending on the opcode. */
372 pos = codestream_tell ();
373 if (op == 0x68)
374 pos += 4;
375 else
376 pos += 1;
377 codestream_seek (pos);
378
379 /* Read the following 8 bytes, which should be "call _probe" (6
380 bytes) followed by "addl $4,%esp" (2 bytes). */
381 codestream_read (buf, sizeof (buf));
382 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
383 pos += sizeof (buf);
384 codestream_seek (pos);
385 op = codestream_get (); /* Update next opcode. */
386 }
387
388 if (op == 0x55) /* pushl %ebp */
389 {
390 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
391 switch (codestream_get ())
392 {
393 case 0x8b:
394 if (codestream_get () != 0xec)
395 return -1;
396 break;
397 case 0x89:
398 if (codestream_get () != 0xe5)
399 return -1;
400 break;
401 default:
402 return -1;
403 }
404 /* Check for stack adjustment
405
406 subl $XXX, %esp
407
408 NOTE: You can't subtract a 16 bit immediate from a 32 bit
409 reg, so we don't have to worry about a data16 prefix. */
410 op = codestream_peek ();
411 if (op == 0x83)
412 {
413 /* `subl' with 8 bit immediate. */
414 codestream_get ();
415 if (codestream_get () != 0xec)
416 /* Some instruction starting with 0x83 other than `subl'. */
417 {
418 codestream_seek (codestream_tell () - 2);
419 return 0;
420 }
421 /* `subl' with signed byte immediate (though it wouldn't
422 make sense to be negative). */
423 return (codestream_get ());
424 }
425 else if (op == 0x81)
426 {
427 char buf[4];
428 /* Maybe it is `subl' with a 32 bit immedediate. */
429 codestream_get ();
430 if (codestream_get () != 0xec)
431 /* Some instruction starting with 0x81 other than `subl'. */
432 {
433 codestream_seek (codestream_tell () - 2);
434 return 0;
435 }
436 /* It is `subl' with a 32 bit immediate. */
437 codestream_read ((unsigned char *) buf, 4);
438 return extract_signed_integer (buf, 4);
439 }
440 else
441 {
442 return 0;
443 }
444 }
445 else if (op == 0xc8)
446 {
447 char buf[2];
448 /* `enter' with 16 bit unsigned immediate. */
449 codestream_read ((unsigned char *) buf, 2);
450 codestream_get (); /* Flush final byte of enter instruction. */
451 return extract_unsigned_integer (buf, 2);
452 }
453 return (-1);
454 }
455
456 /* Signal trampolines don't have a meaningful frame. The frame
457 pointer value we use is actually the frame pointer of the calling
458 frame -- that is, the frame which was in progress when the signal
459 trampoline was entered. GDB mostly treats this frame pointer value
460 as a magic cookie. We detect the case of a signal trampoline by
461 looking at the SIGNAL_HANDLER_CALLER field, which is set based on
462 PC_IN_SIGTRAMP.
463
464 When a signal trampoline is invoked from a frameless function, we
465 essentially have two frameless functions in a row. In this case,
466 we use the same magic cookie for three frames in a row. We detect
467 this case by seeing whether the next frame has
468 SIGNAL_HANDLER_CALLER set, and, if it does, checking whether the
469 current frame is actually frameless. In this case, we need to get
470 the PC by looking at the SP register value stored in the signal
471 context.
472
473 This should work in most cases except in horrible situations where
474 a signal occurs just as we enter a function but before the frame
475 has been set up. Incidentally, that's just what happens when we
476 call a function from GDB with a signal pending (there's a test in
477 the testsuite that makes this happen). Therefore we pretend that
478 we have a frameless function if we're stopped at the start of a
479 function. */
480
481 /* Return non-zero if we're dealing with a frameless signal, that is,
482 a signal trampoline invoked from a frameless function. */
483
484 static int
485 i386_frameless_signal_p (struct frame_info *frame)
486 {
487 return (frame->next && frame->next->signal_handler_caller
488 && (frameless_look_for_prologue (frame)
489 || frame->pc == get_pc_function_start (frame->pc)));
490 }
491
492 /* Return the chain-pointer for FRAME. In the case of the i386, the
493 frame's nominal address is the address of a 4-byte word containing
494 the calling frame's address. */
495
496 static CORE_ADDR
497 i386_frame_chain (struct frame_info *frame)
498 {
499 if (PC_IN_CALL_DUMMY (frame->pc, 0, 0))
500 return frame->frame;
501
502 if (frame->signal_handler_caller
503 || i386_frameless_signal_p (frame))
504 return frame->frame;
505
506 if (! inside_entry_file (frame->pc))
507 return read_memory_unsigned_integer (frame->frame, 4);
508
509 return 0;
510 }
511
512 /* Determine whether the function invocation represented by FRAME does
513 not have a from on the stack associated with it. If it does not,
514 return non-zero, otherwise return zero. */
515
516 static int
517 i386_frameless_function_invocation (struct frame_info *frame)
518 {
519 if (frame->signal_handler_caller)
520 return 0;
521
522 return frameless_look_for_prologue (frame);
523 }
524
525 /* Assuming FRAME is for a sigtramp routine, return the saved program
526 counter. */
527
528 static CORE_ADDR
529 i386_sigtramp_saved_pc (struct frame_info *frame)
530 {
531 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
532 CORE_ADDR addr;
533
534 addr = tdep->sigcontext_addr (frame);
535 return read_memory_unsigned_integer (addr + tdep->sc_pc_offset, 4);
536 }
537
538 /* Assuming FRAME is for a sigtramp routine, return the saved stack
539 pointer. */
540
541 static CORE_ADDR
542 i386_sigtramp_saved_sp (struct frame_info *frame)
543 {
544 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
545 CORE_ADDR addr;
546
547 addr = tdep->sigcontext_addr (frame);
548 return read_memory_unsigned_integer (addr + tdep->sc_sp_offset, 4);
549 }
550
551 /* Return the saved program counter for FRAME. */
552
553 static CORE_ADDR
554 i386_frame_saved_pc (struct frame_info *frame)
555 {
556 if (PC_IN_CALL_DUMMY (frame->pc, 0, 0))
557 return generic_read_register_dummy (frame->pc, frame->frame,
558 PC_REGNUM);
559
560 if (frame->signal_handler_caller)
561 return i386_sigtramp_saved_pc (frame);
562
563 if (i386_frameless_signal_p (frame))
564 {
565 CORE_ADDR sp = i386_sigtramp_saved_sp (frame->next);
566 return read_memory_unsigned_integer (sp, 4);
567 }
568
569 return read_memory_unsigned_integer (frame->frame + 4, 4);
570 }
571
572 /* Immediately after a function call, return the saved pc. */
573
574 static CORE_ADDR
575 i386_saved_pc_after_call (struct frame_info *frame)
576 {
577 if (frame->signal_handler_caller)
578 return i386_sigtramp_saved_pc (frame);
579
580 return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
581 }
582
583 /* Return number of args passed to a frame.
584 Can return -1, meaning no way to tell. */
585
586 static int
587 i386_frame_num_args (struct frame_info *fi)
588 {
589 #if 1
590 return -1;
591 #else
592 /* This loses because not only might the compiler not be popping the
593 args right after the function call, it might be popping args from
594 both this call and a previous one, and we would say there are
595 more args than there really are. */
596
597 int retpc;
598 unsigned char op;
599 struct frame_info *pfi;
600
601 /* On the i386, the instruction following the call could be:
602 popl %ecx - one arg
603 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
604 anything else - zero args. */
605
606 int frameless;
607
608 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
609 if (frameless)
610 /* In the absence of a frame pointer, GDB doesn't get correct
611 values for nameless arguments. Return -1, so it doesn't print
612 any nameless arguments. */
613 return -1;
614
615 pfi = get_prev_frame (fi);
616 if (pfi == 0)
617 {
618 /* NOTE: This can happen if we are looking at the frame for
619 main, because FRAME_CHAIN_VALID won't let us go into start.
620 If we have debugging symbols, that's not really a big deal;
621 it just means it will only show as many arguments to main as
622 are declared. */
623 return -1;
624 }
625 else
626 {
627 retpc = pfi->pc;
628 op = read_memory_integer (retpc, 1);
629 if (op == 0x59) /* pop %ecx */
630 return 1;
631 else if (op == 0x83)
632 {
633 op = read_memory_integer (retpc + 1, 1);
634 if (op == 0xc4)
635 /* addl $<signed imm 8 bits>, %esp */
636 return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
637 else
638 return 0;
639 }
640 else if (op == 0x81) /* `add' with 32 bit immediate. */
641 {
642 op = read_memory_integer (retpc + 1, 1);
643 if (op == 0xc4)
644 /* addl $<imm 32>, %esp */
645 return read_memory_integer (retpc + 2, 4) / 4;
646 else
647 return 0;
648 }
649 else
650 {
651 return 0;
652 }
653 }
654 #endif
655 }
656
657 /* Parse the first few instructions the function to see what registers
658 were stored.
659
660 We handle these cases:
661
662 The startup sequence can be at the start of the function, or the
663 function can start with a branch to startup code at the end.
664
665 %ebp can be set up with either the 'enter' instruction, or "pushl
666 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
667 once used in the System V compiler).
668
669 Local space is allocated just below the saved %ebp by either the
670 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16
671 bit unsigned argument for space to allocate, and the 'addl'
672 instruction could have either a signed byte, or 32 bit immediate.
673
674 Next, the registers used by this function are pushed. With the
675 System V compiler they will always be in the order: %edi, %esi,
676 %ebx (and sometimes a harmless bug causes it to also save but not
677 restore %eax); however, the code below is willing to see the pushes
678 in any order, and will handle up to 8 of them.
679
680 If the setup sequence is at the end of the function, then the next
681 instruction will be a branch back to the start. */
682
683 static void
684 i386_frame_init_saved_regs (struct frame_info *fip)
685 {
686 long locals = -1;
687 unsigned char op;
688 CORE_ADDR addr;
689 CORE_ADDR pc;
690 int i;
691
692 if (fip->saved_regs)
693 return;
694
695 frame_saved_regs_zalloc (fip);
696
697 pc = get_pc_function_start (fip->pc);
698 if (pc != 0)
699 locals = i386_get_frame_setup (pc);
700
701 if (locals >= 0)
702 {
703 addr = fip->frame - 4 - locals;
704 for (i = 0; i < 8; i++)
705 {
706 op = codestream_get ();
707 if (op < 0x50 || op > 0x57)
708 break;
709 #ifdef I386_REGNO_TO_SYMMETRY
710 /* Dynix uses different internal numbering. Ick. */
711 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr;
712 #else
713 fip->saved_regs[op - 0x50] = addr;
714 #endif
715 addr -= 4;
716 }
717 }
718
719 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
720 fip->saved_regs[FP_REGNUM] = fip->frame;
721 }
722
723 /* Return PC of first real instruction. */
724
725 static CORE_ADDR
726 i386_skip_prologue (CORE_ADDR pc)
727 {
728 unsigned char op;
729 int i;
730 static unsigned char pic_pat[6] =
731 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
732 0x5b, /* popl %ebx */
733 };
734 CORE_ADDR pos;
735
736 if (i386_get_frame_setup (pc) < 0)
737 return (pc);
738
739 /* Found valid frame setup -- codestream now points to start of push
740 instructions for saving registers. */
741
742 /* Skip over register saves. */
743 for (i = 0; i < 8; i++)
744 {
745 op = codestream_peek ();
746 /* Break if not `pushl' instrunction. */
747 if (op < 0x50 || op > 0x57)
748 break;
749 codestream_get ();
750 }
751
752 /* The native cc on SVR4 in -K PIC mode inserts the following code
753 to get the address of the global offset table (GOT) into register
754 %ebx
755
756 call 0x0
757 popl %ebx
758 movl %ebx,x(%ebp) (optional)
759 addl y,%ebx
760
761 This code is with the rest of the prologue (at the end of the
762 function), so we have to skip it to get to the first real
763 instruction at the start of the function. */
764
765 pos = codestream_tell ();
766 for (i = 0; i < 6; i++)
767 {
768 op = codestream_get ();
769 if (pic_pat[i] != op)
770 break;
771 }
772 if (i == 6)
773 {
774 unsigned char buf[4];
775 long delta = 6;
776
777 op = codestream_get ();
778 if (op == 0x89) /* movl %ebx, x(%ebp) */
779 {
780 op = codestream_get ();
781 if (op == 0x5d) /* One byte offset from %ebp. */
782 {
783 delta += 3;
784 codestream_read (buf, 1);
785 }
786 else if (op == 0x9d) /* Four byte offset from %ebp. */
787 {
788 delta += 6;
789 codestream_read (buf, 4);
790 }
791 else /* Unexpected instruction. */
792 delta = -1;
793 op = codestream_get ();
794 }
795 /* addl y,%ebx */
796 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
797 {
798 pos += delta + 6;
799 }
800 }
801 codestream_seek (pos);
802
803 i386_follow_jump ();
804
805 return (codestream_tell ());
806 }
807
808 /* Use the program counter to determine the contents and size of a
809 breakpoint instruction. Return a pointer to a string of bytes that
810 encode a breakpoint instruction, store the length of the string in
811 *LEN and optionally adjust *PC to point to the correct memory
812 location for inserting the breakpoint.
813
814 On the i386 we have a single breakpoint that fits in a single byte
815 and can be inserted anywhere. */
816
817 static const unsigned char *
818 i386_breakpoint_from_pc (CORE_ADDR *pc, int *len)
819 {
820 static unsigned char break_insn[] = { 0xcc }; /* int 3 */
821
822 *len = sizeof (break_insn);
823 return break_insn;
824 }
825
826 /* Push the return address (pointing to the call dummy) onto the stack
827 and return the new value for the stack pointer. */
828
829 static CORE_ADDR
830 i386_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
831 {
832 char buf[4];
833
834 store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
835 write_memory (sp - 4, buf, 4);
836 return sp - 4;
837 }
838
839 static void
840 i386_do_pop_frame (struct frame_info *frame)
841 {
842 CORE_ADDR fp;
843 int regnum;
844 char regbuf[I386_MAX_REGISTER_SIZE];
845
846 fp = FRAME_FP (frame);
847 i386_frame_init_saved_regs (frame);
848
849 for (regnum = 0; regnum < NUM_REGS; regnum++)
850 {
851 CORE_ADDR addr;
852 addr = frame->saved_regs[regnum];
853 if (addr)
854 {
855 read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum));
856 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
857 REGISTER_RAW_SIZE (regnum));
858 }
859 }
860 write_register (FP_REGNUM, read_memory_integer (fp, 4));
861 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
862 write_register (SP_REGNUM, fp + 8);
863 flush_cached_frames ();
864 }
865
866 static void
867 i386_pop_frame (void)
868 {
869 generic_pop_current_frame (i386_do_pop_frame);
870 }
871 \f
872
873 /* Figure out where the longjmp will land. Slurp the args out of the
874 stack. We expect the first arg to be a pointer to the jmp_buf
875 structure from which we extract the address that we will land at.
876 This address is copied into PC. This routine returns true on
877 success. */
878
879 static int
880 i386_get_longjmp_target (CORE_ADDR *pc)
881 {
882 char buf[4];
883 CORE_ADDR sp, jb_addr;
884 int jb_pc_offset = gdbarch_tdep (current_gdbarch)->jb_pc_offset;
885
886 /* If JB_PC_OFFSET is -1, we have no way to find out where the
887 longjmp will land. */
888 if (jb_pc_offset == -1)
889 return 0;
890
891 sp = read_register (SP_REGNUM);
892 if (target_read_memory (sp + 4, buf, 4))
893 return 0;
894
895 jb_addr = extract_address (buf, 4);
896 if (target_read_memory (jb_addr + jb_pc_offset, buf, 4))
897 return 0;
898
899 *pc = extract_address (buf, 4);
900 return 1;
901 }
902 \f
903
904 static CORE_ADDR
905 i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
906 int struct_return, CORE_ADDR struct_addr)
907 {
908 sp = default_push_arguments (nargs, args, sp, struct_return, struct_addr);
909
910 if (struct_return)
911 {
912 char buf[4];
913
914 sp -= 4;
915 store_address (buf, 4, struct_addr);
916 write_memory (sp, buf, 4);
917 }
918
919 return sp;
920 }
921
922 static void
923 i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
924 {
925 /* Do nothing. Everything was already done by i386_push_arguments. */
926 }
927
928 /* These registers are used for returning integers (and on some
929 targets also for returning `struct' and `union' values when their
930 size and alignment match an integer type). */
931 #define LOW_RETURN_REGNUM 0 /* %eax */
932 #define HIGH_RETURN_REGNUM 2 /* %edx */
933
934 /* Extract from an array REGBUF containing the (raw) register state, a
935 function return value of TYPE, and copy that, in virtual format,
936 into VALBUF. */
937
938 static void
939 i386_extract_return_value (struct type *type, struct regcache *regcache,
940 char *valbuf)
941 {
942 int len = TYPE_LENGTH (type);
943 char buf[I386_MAX_REGISTER_SIZE];
944
945 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
946 && TYPE_NFIELDS (type) == 1)
947 {
948 i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regcache, valbuf);
949 return;
950 }
951
952 if (TYPE_CODE (type) == TYPE_CODE_FLT)
953 {
954 if (FP0_REGNUM == 0)
955 {
956 warning ("Cannot find floating-point return value.");
957 memset (valbuf, 0, len);
958 return;
959 }
960
961 /* Floating-point return values can be found in %st(0). Convert
962 its contents to the desired type. This is probably not
963 exactly how it would happen on the target itself, but it is
964 the best we can do. */
965 regcache_raw_read (regcache, FP0_REGNUM, buf);
966 convert_typed_floating (buf, builtin_type_i387_ext, valbuf, type);
967 }
968 else
969 {
970 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
971 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
972
973 if (len <= low_size)
974 {
975 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
976 memcpy (valbuf, buf, len);
977 }
978 else if (len <= (low_size + high_size))
979 {
980 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
981 memcpy (valbuf, buf, low_size);
982 regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
983 memcpy (valbuf + low_size, buf, len - low_size);
984 }
985 else
986 internal_error (__FILE__, __LINE__,
987 "Cannot extract return value of %d bytes long.", len);
988 }
989 }
990
991 /* Write into the appropriate registers a function return value stored
992 in VALBUF of type TYPE, given in virtual format. */
993
994 static void
995 i386_store_return_value (struct type *type, char *valbuf)
996 {
997 int len = TYPE_LENGTH (type);
998
999 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1000 && TYPE_NFIELDS (type) == 1)
1001 {
1002 i386_store_return_value (TYPE_FIELD_TYPE (type, 0), valbuf);
1003 return;
1004 }
1005
1006 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1007 {
1008 unsigned int fstat;
1009 char buf[FPU_REG_RAW_SIZE];
1010
1011 if (FP0_REGNUM == 0)
1012 {
1013 warning ("Cannot set floating-point return value.");
1014 return;
1015 }
1016
1017 /* Returning floating-point values is a bit tricky. Apart from
1018 storing the return value in %st(0), we have to simulate the
1019 state of the FPU at function return point. */
1020
1021 /* Convert the value found in VALBUF to the extended
1022 floating-point format used by the FPU. This is probably
1023 not exactly how it would happen on the target itself, but
1024 it is the best we can do. */
1025 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
1026 write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
1027 FPU_REG_RAW_SIZE);
1028
1029 /* Set the top of the floating-point register stack to 7. The
1030 actual value doesn't really matter, but 7 is what a normal
1031 function return would end up with if the program started out
1032 with a freshly initialized FPU. */
1033 fstat = read_register (FSTAT_REGNUM);
1034 fstat |= (7 << 11);
1035 write_register (FSTAT_REGNUM, fstat);
1036
1037 /* Mark %st(1) through %st(7) as empty. Since we set the top of
1038 the floating-point register stack to 7, the appropriate value
1039 for the tag word is 0x3fff. */
1040 write_register (FTAG_REGNUM, 0x3fff);
1041 }
1042 else
1043 {
1044 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
1045 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
1046
1047 if (len <= low_size)
1048 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), valbuf, len);
1049 else if (len <= (low_size + high_size))
1050 {
1051 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM),
1052 valbuf, low_size);
1053 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM),
1054 valbuf + low_size, len - low_size);
1055 }
1056 else
1057 internal_error (__FILE__, __LINE__,
1058 "Cannot store return value of %d bytes long.", len);
1059 }
1060 }
1061
1062 /* Extract from an array REGBUF containing the (raw) register state
1063 the address in which a function should return its structure value,
1064 as a CORE_ADDR. */
1065
1066 static CORE_ADDR
1067 i386_extract_struct_value_address (struct regcache *regcache)
1068 {
1069 return regcache_raw_read_as_address (regcache, LOW_RETURN_REGNUM);
1070 }
1071 \f
1072
1073 /* This is the variable that is set with "set struct-convention", and
1074 its legitimate values. */
1075 static const char default_struct_convention[] = "default";
1076 static const char pcc_struct_convention[] = "pcc";
1077 static const char reg_struct_convention[] = "reg";
1078 static const char *valid_conventions[] =
1079 {
1080 default_struct_convention,
1081 pcc_struct_convention,
1082 reg_struct_convention,
1083 NULL
1084 };
1085 static const char *struct_convention = default_struct_convention;
1086
1087 static int
1088 i386_use_struct_convention (int gcc_p, struct type *type)
1089 {
1090 enum struct_return struct_return;
1091
1092 if (struct_convention == default_struct_convention)
1093 struct_return = gdbarch_tdep (current_gdbarch)->struct_return;
1094 else if (struct_convention == pcc_struct_convention)
1095 struct_return = pcc_struct_return;
1096 else
1097 struct_return = reg_struct_return;
1098
1099 return generic_use_struct_convention (struct_return == reg_struct_return,
1100 type);
1101 }
1102 \f
1103
1104 /* Return the GDB type object for the "standard" data type of data in
1105 register REGNUM. Perhaps %esi and %edi should go here, but
1106 potentially they could be used for things other than address. */
1107
1108 static struct type *
1109 i386_register_virtual_type (int regnum)
1110 {
1111 if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM)
1112 return lookup_pointer_type (builtin_type_void);
1113
1114 if (IS_FP_REGNUM (regnum))
1115 return builtin_type_i387_ext;
1116
1117 if (IS_SSE_REGNUM (regnum))
1118 return builtin_type_vec128i;
1119
1120 return builtin_type_int;
1121 }
1122
1123 /* Return true iff register REGNUM's virtual format is different from
1124 its raw format. Note that this definition assumes that the host
1125 supports IEEE 32-bit floats, since it doesn't say that SSE
1126 registers need conversion. Even if we can't find a counterexample,
1127 this is still sloppy. */
1128
1129 static int
1130 i386_register_convertible (int regnum)
1131 {
1132 return IS_FP_REGNUM (regnum);
1133 }
1134
1135 /* Convert data from raw format for register REGNUM in buffer FROM to
1136 virtual format with type TYPE in buffer TO. */
1137
1138 static void
1139 i386_register_convert_to_virtual (int regnum, struct type *type,
1140 char *from, char *to)
1141 {
1142 gdb_assert (IS_FP_REGNUM (regnum));
1143
1144 /* We only support floating-point values. */
1145 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1146 {
1147 warning ("Cannot convert floating-point register value "
1148 "to non-floating-point type.");
1149 memset (to, 0, TYPE_LENGTH (type));
1150 return;
1151 }
1152
1153 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1154 the extended floating-point format used by the FPU. */
1155 convert_typed_floating (from, builtin_type_i387_ext, to, type);
1156 }
1157
1158 /* Convert data from virtual format with type TYPE in buffer FROM to
1159 raw format for register REGNUM in buffer TO. */
1160
1161 static void
1162 i386_register_convert_to_raw (struct type *type, int regnum,
1163 char *from, char *to)
1164 {
1165 gdb_assert (IS_FP_REGNUM (regnum));
1166
1167 /* We only support floating-point values. */
1168 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1169 {
1170 warning ("Cannot convert non-floating-point type "
1171 "to floating-point register value.");
1172 memset (to, 0, TYPE_LENGTH (type));
1173 return;
1174 }
1175
1176 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1177 to the extended floating-point format used by the FPU. */
1178 convert_typed_floating (from, type, to, builtin_type_i387_ext);
1179 }
1180 \f
1181
1182 #ifdef STATIC_TRANSFORM_NAME
1183 /* SunPRO encodes the static variables. This is not related to C++
1184 mangling, it is done for C too. */
1185
1186 char *
1187 sunpro_static_transform_name (char *name)
1188 {
1189 char *p;
1190 if (IS_STATIC_TRANSFORM_NAME (name))
1191 {
1192 /* For file-local statics there will be a period, a bunch of
1193 junk (the contents of which match a string given in the
1194 N_OPT), a period and the name. For function-local statics
1195 there will be a bunch of junk (which seems to change the
1196 second character from 'A' to 'B'), a period, the name of the
1197 function, and the name. So just skip everything before the
1198 last period. */
1199 p = strrchr (name, '.');
1200 if (p != NULL)
1201 name = p + 1;
1202 }
1203 return name;
1204 }
1205 #endif /* STATIC_TRANSFORM_NAME */
1206 \f
1207
1208 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1209
1210 CORE_ADDR
1211 skip_trampoline_code (CORE_ADDR pc, char *name)
1212 {
1213 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
1214 {
1215 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
1216 struct minimal_symbol *indsym =
1217 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
1218 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
1219
1220 if (symname)
1221 {
1222 if (strncmp (symname, "__imp_", 6) == 0
1223 || strncmp (symname, "_imp_", 5) == 0)
1224 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1225 }
1226 }
1227 return 0; /* Not a trampoline. */
1228 }
1229 \f
1230
1231 /* Return non-zero if PC and NAME show that we are in a signal
1232 trampoline. */
1233
1234 static int
1235 i386_pc_in_sigtramp (CORE_ADDR pc, char *name)
1236 {
1237 return (name && strcmp ("_sigtramp", name) == 0);
1238 }
1239 \f
1240
1241 /* We have two flavours of disassembly. The machinery on this page
1242 deals with switching between those. */
1243
1244 static int
1245 gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
1246 {
1247 if (disassembly_flavor == att_flavor)
1248 return print_insn_i386_att (memaddr, info);
1249 else if (disassembly_flavor == intel_flavor)
1250 return print_insn_i386_intel (memaddr, info);
1251 /* Never reached -- disassembly_flavour is always either att_flavor
1252 or intel_flavor. */
1253 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1254 }
1255 \f
1256
1257 /* There are a few i386 architecture variants that differ only
1258 slightly from the generic i386 target. For now, we don't give them
1259 their own source file, but include them here. As a consequence,
1260 they'll always be included. */
1261
1262 /* System V Release 4 (SVR4). */
1263
1264 static int
1265 i386_svr4_pc_in_sigtramp (CORE_ADDR pc, char *name)
1266 {
1267 return (name && (strcmp ("_sigreturn", name) == 0
1268 || strcmp ("_sigacthandler", name) == 0
1269 || strcmp ("sigvechandler", name) == 0));
1270 }
1271
1272 /* Get address of the pushed ucontext (sigcontext) on the stack for
1273 all three variants of SVR4 sigtramps. */
1274
1275 static CORE_ADDR
1276 i386_svr4_sigcontext_addr (struct frame_info *frame)
1277 {
1278 int sigcontext_offset = -1;
1279 char *name = NULL;
1280
1281 find_pc_partial_function (frame->pc, &name, NULL, NULL);
1282 if (name)
1283 {
1284 if (strcmp (name, "_sigreturn") == 0)
1285 sigcontext_offset = 132;
1286 else if (strcmp (name, "_sigacthandler") == 0)
1287 sigcontext_offset = 80;
1288 else if (strcmp (name, "sigvechandler") == 0)
1289 sigcontext_offset = 120;
1290 }
1291
1292 gdb_assert (sigcontext_offset != -1);
1293
1294 if (frame->next)
1295 return frame->next->frame + sigcontext_offset;
1296 return read_register (SP_REGNUM) + sigcontext_offset;
1297 }
1298 \f
1299
1300 /* DJGPP. */
1301
1302 static int
1303 i386_go32_pc_in_sigtramp (CORE_ADDR pc, char *name)
1304 {
1305 /* DJGPP doesn't have any special frames for signal handlers. */
1306 return 0;
1307 }
1308 \f
1309
1310 /* Generic ELF. */
1311
1312 void
1313 i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1314 {
1315 /* We typically use stabs-in-ELF with the DWARF register numbering. */
1316 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1317 }
1318
1319 /* System V Release 4 (SVR4). */
1320
1321 void
1322 i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1323 {
1324 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1325
1326 /* System V Release 4 uses ELF. */
1327 i386_elf_init_abi (info, gdbarch);
1328
1329 /* FIXME: kettenis/20020511: Why do we override this function here? */
1330 set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
1331
1332 set_gdbarch_pc_in_sigtramp (gdbarch, i386_svr4_pc_in_sigtramp);
1333 tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
1334 tdep->sc_pc_offset = 14 * 4;
1335 tdep->sc_sp_offset = 7 * 4;
1336
1337 tdep->jb_pc_offset = 20;
1338 }
1339
1340 /* DJGPP. */
1341
1342 static void
1343 i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1344 {
1345 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1346
1347 set_gdbarch_pc_in_sigtramp (gdbarch, i386_go32_pc_in_sigtramp);
1348
1349 tdep->jb_pc_offset = 36;
1350 }
1351
1352 /* NetWare. */
1353
1354 static void
1355 i386_nw_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1356 {
1357 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1358
1359 /* FIXME: kettenis/20020511: Why do we override this function here? */
1360 set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
1361
1362 tdep->jb_pc_offset = 24;
1363 }
1364 \f
1365
1366 static struct gdbarch *
1367 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1368 {
1369 struct gdbarch_tdep *tdep;
1370 struct gdbarch *gdbarch;
1371 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
1372
1373 /* Try to determine the OS ABI of the object we're loading. */
1374 if (info.abfd != NULL)
1375 osabi = gdbarch_lookup_osabi (info.abfd);
1376
1377 /* Find a candidate among extant architectures. */
1378 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1379 arches != NULL;
1380 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1381 {
1382 /* Make sure the OS ABI selection matches. */
1383 tdep = gdbarch_tdep (arches->gdbarch);
1384 if (tdep && tdep->osabi == osabi)
1385 return arches->gdbarch;
1386 }
1387
1388 /* Allocate space for the new architecture. */
1389 tdep = XMALLOC (struct gdbarch_tdep);
1390 gdbarch = gdbarch_alloc (&info, tdep);
1391
1392 tdep->osabi = osabi;
1393
1394 /* The i386 default settings don't include the SSE registers.
1395 FIXME: kettenis/20020614: They do include the FPU registers for
1396 now, which probably is not quite right. */
1397 tdep->num_xmm_regs = 0;
1398
1399 tdep->jb_pc_offset = -1;
1400 tdep->struct_return = pcc_struct_return;
1401 tdep->sigtramp_start = 0;
1402 tdep->sigtramp_end = 0;
1403 tdep->sigcontext_addr = NULL;
1404 tdep->sc_pc_offset = -1;
1405 tdep->sc_sp_offset = -1;
1406
1407 /* The format used for `long double' on almost all i386 targets is
1408 the i387 extended floating-point format. In fact, of all targets
1409 in the GCC 2.95 tree, only OSF/1 does it different, and insists
1410 on having a `long double' that's not `long' at all. */
1411 set_gdbarch_long_double_format (gdbarch, &floatformat_i387_ext);
1412
1413 /* Although the i386 extended floating-point has only 80 significant
1414 bits, a `long double' actually takes up 96, probably to enforce
1415 alignment. */
1416 set_gdbarch_long_double_bit (gdbarch, 96);
1417
1418 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-ptx.h,
1419 tm-symmetry.h currently override this. Sigh. */
1420 set_gdbarch_num_regs (gdbarch, I386_NUM_GREGS + I386_NUM_FREGS);
1421
1422 set_gdbarch_sp_regnum (gdbarch, 4);
1423 set_gdbarch_fp_regnum (gdbarch, 5);
1424 set_gdbarch_pc_regnum (gdbarch, 8);
1425 set_gdbarch_ps_regnum (gdbarch, 9);
1426 set_gdbarch_fp0_regnum (gdbarch, 16);
1427
1428 /* Use the "default" register numbering scheme for stabs and COFF. */
1429 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1430 set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1431
1432 /* Use the DWARF register numbering scheme for DWARF and DWARF 2. */
1433 set_gdbarch_dwarf_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1434 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1435
1436 /* We don't define ECOFF_REG_TO_REGNUM, since ECOFF doesn't seem to
1437 be in use on any of the supported i386 targets. */
1438
1439 set_gdbarch_register_name (gdbarch, i386_register_name);
1440 set_gdbarch_register_size (gdbarch, 4);
1441 set_gdbarch_register_bytes (gdbarch, I386_SIZEOF_GREGS + I386_SIZEOF_FREGS);
1442 set_gdbarch_register_byte (gdbarch, i386_register_byte);
1443 set_gdbarch_register_raw_size (gdbarch, i386_register_raw_size);
1444 set_gdbarch_max_register_raw_size (gdbarch, I386_MAX_REGISTER_SIZE);
1445 set_gdbarch_max_register_virtual_size (gdbarch, I386_MAX_REGISTER_SIZE);
1446 set_gdbarch_register_virtual_type (gdbarch, i386_register_virtual_type);
1447
1448 set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
1449
1450 set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
1451
1452 /* Call dummy code. */
1453 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1454 set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1455 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1456 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1457 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1458 set_gdbarch_call_dummy_length (gdbarch, 0);
1459 set_gdbarch_call_dummy_p (gdbarch, 1);
1460 set_gdbarch_call_dummy_words (gdbarch, NULL);
1461 set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
1462 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1463 set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1464
1465 set_gdbarch_register_convertible (gdbarch, i386_register_convertible);
1466 set_gdbarch_register_convert_to_virtual (gdbarch,
1467 i386_register_convert_to_virtual);
1468 set_gdbarch_register_convert_to_raw (gdbarch, i386_register_convert_to_raw);
1469
1470 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1471 set_gdbarch_push_arguments (gdbarch, i386_push_arguments);
1472
1473 set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_at_entry_point);
1474
1475 /* "An argument's size is increased, if necessary, to make it a
1476 multiple of [32-bit] words. This may require tail padding,
1477 depending on the size of the argument" -- from the x86 ABI. */
1478 set_gdbarch_parm_boundary (gdbarch, 32);
1479
1480 set_gdbarch_extract_return_value (gdbarch, i386_extract_return_value);
1481 set_gdbarch_push_arguments (gdbarch, i386_push_arguments);
1482 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1483 set_gdbarch_push_return_address (gdbarch, i386_push_return_address);
1484 set_gdbarch_pop_frame (gdbarch, i386_pop_frame);
1485 set_gdbarch_store_struct_return (gdbarch, i386_store_struct_return);
1486 set_gdbarch_store_return_value (gdbarch, i386_store_return_value);
1487 set_gdbarch_extract_struct_value_address (gdbarch,
1488 i386_extract_struct_value_address);
1489 set_gdbarch_use_struct_convention (gdbarch, i386_use_struct_convention);
1490
1491 set_gdbarch_frame_init_saved_regs (gdbarch, i386_frame_init_saved_regs);
1492 set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
1493
1494 /* Stack grows downward. */
1495 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1496
1497 set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc);
1498 set_gdbarch_decr_pc_after_break (gdbarch, 1);
1499 set_gdbarch_function_start_offset (gdbarch, 0);
1500
1501 /* The following redefines make backtracing through sigtramp work.
1502 They manufacture a fake sigtramp frame and obtain the saved pc in
1503 sigtramp from the sigcontext structure which is pushed by the
1504 kernel on the user stack, along with a pointer to it. */
1505
1506 set_gdbarch_frame_args_skip (gdbarch, 8);
1507 set_gdbarch_frameless_function_invocation (gdbarch,
1508 i386_frameless_function_invocation);
1509 set_gdbarch_frame_chain (gdbarch, i386_frame_chain);
1510 set_gdbarch_frame_chain_valid (gdbarch, generic_file_frame_chain_valid);
1511 set_gdbarch_frame_saved_pc (gdbarch, i386_frame_saved_pc);
1512 set_gdbarch_frame_args_address (gdbarch, default_frame_address);
1513 set_gdbarch_frame_locals_address (gdbarch, default_frame_address);
1514 set_gdbarch_saved_pc_after_call (gdbarch, i386_saved_pc_after_call);
1515 set_gdbarch_frame_num_args (gdbarch, i386_frame_num_args);
1516 set_gdbarch_pc_in_sigtramp (gdbarch, i386_pc_in_sigtramp);
1517
1518 /* Hook in ABI-specific overrides, if they have been registered. */
1519 gdbarch_init_osabi (info, gdbarch, osabi);
1520
1521 return gdbarch;
1522 }
1523
1524 static enum gdb_osabi
1525 i386_coff_osabi_sniffer (bfd *abfd)
1526 {
1527 if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
1528 || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
1529 return GDB_OSABI_GO32;
1530
1531 return GDB_OSABI_UNKNOWN;
1532 }
1533
1534 static enum gdb_osabi
1535 i386_nlm_osabi_sniffer (bfd *abfd)
1536 {
1537 return GDB_OSABI_NETWARE;
1538 }
1539 \f
1540
1541 /* Provide a prototype to silence -Wmissing-prototypes. */
1542 void _initialize_i386_tdep (void);
1543
1544 void
1545 _initialize_i386_tdep (void)
1546 {
1547 register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
1548
1549 /* Initialize the table saying where each register starts in the
1550 register file. */
1551 {
1552 int i, offset;
1553
1554 offset = 0;
1555 for (i = 0; i < I386_SSE_NUM_REGS; i++)
1556 {
1557 i386_register_offset[i] = offset;
1558 offset += i386_register_size[i];
1559 }
1560 }
1561
1562 tm_print_insn = gdb_print_insn_i386;
1563 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1564
1565 /* Add the variable that controls the disassembly flavor. */
1566 {
1567 struct cmd_list_element *new_cmd;
1568
1569 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1570 valid_flavors,
1571 &disassembly_flavor,
1572 "\
1573 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1574 and the default value is \"att\".",
1575 &setlist);
1576 add_show_from_set (new_cmd, &showlist);
1577 }
1578
1579 /* Add the variable that controls the convention for returning
1580 structs. */
1581 {
1582 struct cmd_list_element *new_cmd;
1583
1584 new_cmd = add_set_enum_cmd ("struct-convention", no_class,
1585 valid_conventions,
1586 &struct_convention, "\
1587 Set the convention for returning small structs, valid values \
1588 are \"default\", \"pcc\" and \"reg\", and the default value is \"default\".",
1589 &setlist);
1590 add_show_from_set (new_cmd, &showlist);
1591 }
1592
1593 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
1594 i386_coff_osabi_sniffer);
1595 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_nlm_flavour,
1596 i386_nlm_osabi_sniffer);
1597
1598 gdbarch_register_osabi (bfd_arch_i386, GDB_OSABI_SVR4,
1599 i386_svr4_init_abi);
1600 gdbarch_register_osabi (bfd_arch_i386, GDB_OSABI_GO32,
1601 i386_go32_init_abi);
1602 gdbarch_register_osabi (bfd_arch_i386, GDB_OSABI_NETWARE,
1603 i386_nw_init_abi);
1604 }
This page took 0.080141 seconds and 4 git commands to generate.