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