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