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