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