* config/obj-elf.c (obj_elf_symver): Temporarily modify lex_type
[binutils-gdb.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "gdbcore.h"
28 #include "target.h"
29 #include "floatformat.h"
30 #include "symtab.h"
31 #include "gdbcmd.h"
32 #include "command.h"
33 #include "arch-utils.h"
34 #include "regcache.h"
35
36 #include "gdb_assert.h"
37
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. */
41 int i386_register_byte[MAX_NUM_REGS];
42
43 /* i386_register_raw_size[i] is the number of bytes of storage in
44 GDB's register array occupied by register i. */
45 int 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. */
61 int i386_register_virtual_size[MAX_NUM_REGS];
62
63 /* Convert stabs register number REG to the appropriate register
64 number used by GDB. */
65
66 int
67 i386_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
100 int
101 i386_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 }
124 \f
125
126 /* This is the variable that is set with "set disassembly-flavor", and
127 its legitimate values. */
128 static const char att_flavor[] = "att";
129 static const char intel_flavor[] = "intel";
130 static const char *valid_flavors[] =
131 {
132 att_flavor,
133 intel_flavor,
134 NULL
135 };
136 static const char *disassembly_flavor = att_flavor;
137
138 /* This is used to keep the bfd arch_info in sync with the disassembly
139 flavor. */
140 static void set_disassembly_flavor_sfunc (char *, int,
141 struct cmd_list_element *);
142 static void set_disassembly_flavor (void);
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. */
164 static CORE_ADDR codestream_next_addr;
165 static CORE_ADDR codestream_addr;
166 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
167 static int codestream_off;
168 static int codestream_cnt;
169
170 #define codestream_tell() (codestream_addr + codestream_off)
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++])
177
178 static unsigned char
179 codestream_fill (int peek_flag)
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);
186
187 if (peek_flag)
188 return (codestream_peek ());
189 else
190 return (codestream_get ());
191 }
192
193 static void
194 codestream_seek (CORE_ADDR place)
195 {
196 codestream_next_addr = place / CODESTREAM_BUFSIZ;
197 codestream_next_addr *= CODESTREAM_BUFSIZ;
198 codestream_cnt = 0;
199 codestream_fill (1);
200 while (codestream_tell () != place)
201 codestream_get ();
202 }
203
204 static void
205 codestream_read (unsigned char *buf, int count)
206 {
207 unsigned char *p;
208 int i;
209 p = buf;
210 for (i = 0; i < count; i++)
211 *p++ = codestream_get ();
212 }
213 \f
214
215 /* If the next instruction is a jump, move to its target. */
216
217 static void
218 i386_follow_jump (void)
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:
238 /* Relative jump: if data16 == 0, disp32, else disp16. */
239 if (data16)
240 {
241 codestream_read (buf, 2);
242 delta = extract_signed_integer (buf, 2);
243
244 /* Include the size of the jmp instruction (including the
245 0x66 prefix). */
246 pos += delta + 4;
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:
257 /* Relative jump, disp8 (ignore data16). */
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
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. */
273
274 static long
275 i386_get_frame_setup (CORE_ADDR pc)
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 {
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. */
298 int pos;
299 unsigned char buf[4];
300 static unsigned char proto1[3] = { 0x87, 0x04, 0x24 };
301 static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
302
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);
311 op = codestream_get (); /* Update next opcode. */
312 }
313
314 if (op == 0x68 || op == 0x6a)
315 {
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. */
327 int pos;
328 unsigned char buf[8];
329
330 /* Skip past the `pushl' instruction; it has either a one-byte
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
339 /* Read the following 8 bytes, which should be "call _probe" (6
340 bytes) followed by "addl $4,%esp" (2 bytes). */
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);
345 op = codestream_get (); /* Update next opcode. */
346 }
347
348 if (op == 0x55) /* pushl %ebp */
349 {
350 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
351 switch (codestream_get ())
352 {
353 case 0x8b:
354 if (codestream_get () != 0xec)
355 return -1;
356 break;
357 case 0x89:
358 if (codestream_get () != 0xe5)
359 return -1;
360 break;
361 default:
362 return -1;
363 }
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. */
370 op = codestream_peek ();
371 if (op == 0x83)
372 {
373 /* `subl' with 8 bit immediate. */
374 codestream_get ();
375 if (codestream_get () != 0xec)
376 /* Some instruction starting with 0x83 other than `subl'. */
377 {
378 codestream_seek (codestream_tell () - 2);
379 return 0;
380 }
381 /* `subl' with signed byte immediate (though it wouldn't
382 make sense to be negative). */
383 return (codestream_get ());
384 }
385 else if (op == 0x81)
386 {
387 char buf[4];
388 /* Maybe it is `subl' with a 32 bit immedediate. */
389 codestream_get ();
390 if (codestream_get () != 0xec)
391 /* Some instruction starting with 0x81 other than `subl'. */
392 {
393 codestream_seek (codestream_tell () - 2);
394 return 0;
395 }
396 /* It is `subl' with a 32 bit immediate. */
397 codestream_read ((unsigned char *) buf, 4);
398 return extract_signed_integer (buf, 4);
399 }
400 else
401 {
402 return 0;
403 }
404 }
405 else if (op == 0xc8)
406 {
407 char buf[2];
408 /* `enter' with 16 bit unsigned immediate. */
409 codestream_read ((unsigned char *) buf, 2);
410 codestream_get (); /* Flush final byte of enter instruction. */
411 return extract_unsigned_integer (buf, 2);
412 }
413 return (-1);
414 }
415
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
420 CORE_ADDR
421 i386_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
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
436 int
437 i386_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
445 /* Return the saved program counter for FRAME. */
446
447 CORE_ADDR
448 i386_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
462 /* Immediately after a function call, return the saved pc. */
463
464 CORE_ADDR
465 i386_saved_pc_after_call (struct frame_info *frame)
466 {
467 return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
468 }
469
470 /* Return number of args passed to a frame.
471 Can return -1, meaning no way to tell. */
472
473 int
474 i386_frame_num_args (struct frame_info *fi)
475 {
476 #if 1
477 return -1;
478 #else
479 /* This loses because not only might the compiler not be popping the
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. */
483
484 int retpc;
485 unsigned char op;
486 struct frame_info *pfi;
487
488 /* On the i386, the instruction following the call could be:
489 popl %ecx - one arg
490 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
491 anything else - zero args. */
492
493 int frameless;
494
495 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
496 if (frameless)
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. */
500 return -1;
501
502 pfi = get_prev_frame (fi);
503 if (pfi == 0)
504 {
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. */
510 return -1;
511 }
512 else
513 {
514 retpc = pfi->pc;
515 op = read_memory_integer (retpc, 1);
516 if (op == 0x59) /* pop %ecx */
517 return 1;
518 else if (op == 0x83)
519 {
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;
524 else
525 return 0;
526 }
527 else if (op == 0x81) /* `add' with 32 bit immediate. */
528 {
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;
533 else
534 return 0;
535 }
536 else
537 {
538 return 0;
539 }
540 }
541 #endif
542 }
543
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. */
569
570 void
571 i386_frame_init_saved_regs (struct frame_info *fip)
572 {
573 long locals = -1;
574 unsigned char op;
575 CORE_ADDR dummy_bottom;
576 CORE_ADDR addr;
577 CORE_ADDR pc;
578 int i;
579
580 if (fip->saved_regs)
581 return;
582
583 frame_saved_regs_zalloc (fip);
584
585 /* If the frame is the end of a dummy, compute where the beginning
586 would be. */
587 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
588
589 /* Check if the PC points in the stack, in a dummy frame. */
590 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
591 {
592 /* All registers were saved by push_call_dummy. */
593 addr = fip->frame;
594 for (i = 0; i < NUM_REGS; i++)
595 {
596 addr -= REGISTER_RAW_SIZE (i);
597 fip->saved_regs[i] = addr;
598 }
599 return;
600 }
601
602 pc = get_pc_function_start (fip->pc);
603 if (pc != 0)
604 locals = i386_get_frame_setup (pc);
605
606 if (locals >= 0)
607 {
608 addr = fip->frame - 4 - locals;
609 for (i = 0; i < 8; i++)
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. */
616 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr;
617 #else
618 fip->saved_regs[op - 0x50] = addr;
619 #endif
620 addr -= 4;
621 }
622 }
623
624 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
625 fip->saved_regs[FP_REGNUM] = fip->frame;
626 }
627
628 /* Return PC of first real instruction. */
629
630 int
631 i386_skip_prologue (int pc)
632 {
633 unsigned char op;
634 int i;
635 static unsigned char pic_pat[6] =
636 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
637 0x5b, /* popl %ebx */
638 };
639 CORE_ADDR pos;
640
641 if (i386_get_frame_setup (pc) < 0)
642 return (pc);
643
644 /* Found valid frame setup -- codestream now points to start of push
645 instructions for saving registers. */
646
647 /* Skip over register saves. */
648 for (i = 0; i < 8; i++)
649 {
650 op = codestream_peek ();
651 /* Break if not `pushl' instrunction. */
652 if (op < 0x50 || op > 0x57)
653 break;
654 codestream_get ();
655 }
656
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
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. */
669
670 pos = codestream_tell ();
671 for (i = 0; i < 6; i++)
672 {
673 op = codestream_get ();
674 if (pic_pat[i] != op)
675 break;
676 }
677 if (i == 6)
678 {
679 unsigned char buf[4];
680 long delta = 6;
681
682 op = codestream_get ();
683 if (op == 0x89) /* movl %ebx, x(%ebp) */
684 {
685 op = codestream_get ();
686 if (op == 0x5d) /* One byte offset from %ebp. */
687 {
688 delta += 3;
689 codestream_read (buf, 1);
690 }
691 else if (op == 0x9d) /* Four byte offset from %ebp. */
692 {
693 delta += 6;
694 codestream_read (buf, 4);
695 }
696 else /* Unexpected instruction. */
697 delta = -1;
698 op = codestream_get ();
699 }
700 /* addl y,%ebx */
701 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
702 {
703 pos += delta + 6;
704 }
705 }
706 codestream_seek (pos);
707
708 i386_follow_jump ();
709
710 return (codestream_tell ());
711 }
712
713 void
714 i386_push_dummy_frame (void)
715 {
716 CORE_ADDR sp = read_register (SP_REGNUM);
717 int regnum;
718 char regbuf[MAX_REGISTER_RAW_SIZE];
719
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
731 /* Insert the (relative) function address into the call sequence
732 stored at DYMMY. */
733
734 void
735 i386_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
736 struct value **args, struct type *type, int gcc_p)
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
751 void
752 i386_pop_frame (void)
753 {
754 struct frame_info *frame = get_current_frame ();
755 CORE_ADDR fp;
756 int regnum;
757 char regbuf[MAX_REGISTER_RAW_SIZE];
758
759 fp = FRAME_FP (frame);
760 i386_frame_init_saved_regs (frame);
761
762 for (regnum = 0; regnum < NUM_REGS; regnum++)
763 {
764 CORE_ADDR addr;
765 addr = frame->saved_regs[regnum];
766 if (addr)
767 {
768 read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum));
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 }
778 \f
779
780 #ifdef GET_LONGJMP_TARGET
781
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. */
787
788 int
789 get_longjmp_target (CORE_ADDR *pc)
790 {
791 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
792 CORE_ADDR sp, jb_addr;
793
794 sp = read_register (SP_REGNUM);
795
796 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack. */
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 */
813 \f
814
815 CORE_ADDR
816 i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
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
833 void
834 i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
835 {
836 /* Do nothing. Everything was already done by i386_push_arguments. */
837 }
838
839 /* These registers are used for returning integers (and on some
840 targets also for returning `struct' and `union' values when their
841 size and alignment match an integer type). */
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
849 void
850 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
851 {
852 int len = TYPE_LENGTH (type);
853
854 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
855 && TYPE_NFIELDS (type) == 1)
856 {
857 i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regbuf, valbuf);
858 return;
859 }
860
861 if (TYPE_CODE (type) == TYPE_CODE_FLT)
862 {
863 if (NUM_FREGS == 0)
864 {
865 warning ("Cannot find floating-point return value.");
866 memset (valbuf, 0, len);
867 return;
868 }
869
870 /* Floating-point return values can be found in %st(0). */
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 }
890 }
891 else
892 {
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)
897 memcpy (valbuf, &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
898 else if (len <= (low_size + high_size))
899 {
900 memcpy (valbuf,
901 &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
902 memcpy (valbuf + low_size,
903 &regbuf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
904 }
905 else
906 internal_error (__FILE__, __LINE__,
907 "Cannot extract return value of %d bytes long.", len);
908 }
909 }
910
911 /* Write into the appropriate registers a function return value stored
912 in VALBUF of type TYPE, given in virtual format. */
913
914 void
915 i386_store_return_value (struct type *type, char *valbuf)
916 {
917 int len = TYPE_LENGTH (type);
918
919 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
920 && TYPE_NFIELDS (type) == 1)
921 {
922 i386_store_return_value (TYPE_FIELD_TYPE (type, 0), valbuf);
923 return;
924 }
925
926 if (TYPE_CODE (type) == TYPE_CODE_FLT)
927 {
928 unsigned int fstat;
929
930 if (NUM_FREGS == 0)
931 {
932 warning ("Cannot set floating-point return value.");
933 return;
934 }
935
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
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
953 floating-point format used by the FPU. This is probably
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 }
961
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. */
966 fstat = read_register (FSTAT_REGNUM);
967 fstat |= (7 << 11);
968 write_register (FSTAT_REGNUM, fstat);
969
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. */
973 write_register (FTAG_REGNUM, 0x3fff);
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
990 internal_error (__FILE__, __LINE__,
991 "Cannot store return value of %d bytes long.", len);
992 }
993 }
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
999 CORE_ADDR
1000 i386_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 }
1005 \f
1006
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
1011 struct type *
1012 i386_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
1032 int
1033 i386_register_convertible (int regnum)
1034 {
1035 return IS_FP_REGNUM (regnum);
1036 }
1037
1038 /* Convert data from raw format for register REGNUM in buffer FROM to
1039 virtual format with type TYPE in buffer TO. */
1040
1041 void
1042 i386_register_convert_to_virtual (int regnum, struct type *type,
1043 char *from, char *to)
1044 {
1045 char buf[12];
1046 DOUBLEST d;
1047
1048 /* We only support floating-point values. */
1049 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
1050
1051 /* First add the necessary padding. */
1052 memcpy (buf, from, FPU_REG_RAW_SIZE);
1053 memset (buf + FPU_REG_RAW_SIZE, 0, sizeof buf - FPU_REG_RAW_SIZE);
1054
1055 /* Convert to TYPE. This should be a no-op, if TYPE is equivalent
1056 to the extended floating-point format used by the FPU. */
1057 d = extract_floating (buf, sizeof buf);
1058 store_floating (to, TYPE_LENGTH (type), d);
1059 }
1060
1061 /* Convert data from virtual format with type TYPE in buffer FROM to
1062 raw format for register REGNUM in buffer TO. */
1063
1064 void
1065 i386_register_convert_to_raw (struct type *type, int regnum,
1066 char *from, char *to)
1067 {
1068 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT
1069 && TYPE_LENGTH (type) == 12);
1070
1071 /* Simply omit the two unused bytes. */
1072 memcpy (to, from, FPU_REG_RAW_SIZE);
1073 }
1074 \f
1075
1076 #ifdef I386V4_SIGTRAMP_SAVED_PC
1077 /* Get saved user PC for sigtramp from the pushed ucontext on the
1078 stack for all three variants of SVR4 sigtramps. */
1079
1080 CORE_ADDR
1081 i386v4_sigtramp_saved_pc (struct frame_info *frame)
1082 {
1083 CORE_ADDR saved_pc_offset = 4;
1084 char *name = NULL;
1085
1086 find_pc_partial_function (frame->pc, &name, NULL, NULL);
1087 if (name)
1088 {
1089 if (STREQ (name, "_sigreturn"))
1090 saved_pc_offset = 132 + 14 * 4;
1091 else if (STREQ (name, "_sigacthandler"))
1092 saved_pc_offset = 80 + 14 * 4;
1093 else if (STREQ (name, "sigvechandler"))
1094 saved_pc_offset = 120 + 14 * 4;
1095 }
1096
1097 if (frame->next)
1098 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
1099 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
1100 }
1101 #endif /* I386V4_SIGTRAMP_SAVED_PC */
1102 \f
1103
1104 #ifdef STATIC_TRANSFORM_NAME
1105 /* SunPRO encodes the static variables. This is not related to C++
1106 mangling, it is done for C too. */
1107
1108 char *
1109 sunpro_static_transform_name (char *name)
1110 {
1111 char *p;
1112 if (IS_STATIC_TRANSFORM_NAME (name))
1113 {
1114 /* For file-local statics there will be a period, a bunch of
1115 junk (the contents of which match a string given in the
1116 N_OPT), a period and the name. For function-local statics
1117 there will be a bunch of junk (which seems to change the
1118 second character from 'A' to 'B'), a period, the name of the
1119 function, and the name. So just skip everything before the
1120 last period. */
1121 p = strrchr (name, '.');
1122 if (p != NULL)
1123 name = p + 1;
1124 }
1125 return name;
1126 }
1127 #endif /* STATIC_TRANSFORM_NAME */
1128 \f
1129
1130 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1131
1132 CORE_ADDR
1133 skip_trampoline_code (CORE_ADDR pc, char *name)
1134 {
1135 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
1136 {
1137 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
1138 struct minimal_symbol *indsym =
1139 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
1140 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
1141
1142 if (symname)
1143 {
1144 if (strncmp (symname, "__imp_", 6) == 0
1145 || strncmp (symname, "_imp_", 5) == 0)
1146 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1147 }
1148 }
1149 return 0; /* Not a trampoline. */
1150 }
1151 \f
1152
1153 /* We have two flavours of disassembly. The machinery on this page
1154 deals with switching between those. */
1155
1156 static int
1157 gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
1158 {
1159 if (disassembly_flavor == att_flavor)
1160 return print_insn_i386_att (memaddr, info);
1161 else if (disassembly_flavor == intel_flavor)
1162 return print_insn_i386_intel (memaddr, info);
1163 /* Never reached -- disassembly_flavour is always either att_flavor
1164 or intel_flavor. */
1165 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1166 }
1167
1168 /* If the disassembly mode is intel, we have to also switch the bfd
1169 mach_type. This function is run in the set disassembly_flavor
1170 command, and does that. */
1171
1172 static void
1173 set_disassembly_flavor_sfunc (char *args, int from_tty,
1174 struct cmd_list_element *c)
1175 {
1176 set_disassembly_flavor ();
1177 }
1178
1179 static void
1180 set_disassembly_flavor (void)
1181 {
1182 if (disassembly_flavor == att_flavor)
1183 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386);
1184 else if (disassembly_flavor == intel_flavor)
1185 set_architecture_from_arch_mach (bfd_arch_i386,
1186 bfd_mach_i386_i386_intel_syntax);
1187 }
1188 \f
1189
1190 /* Provide a prototype to silence -Wmissing-prototypes. */
1191 void _initialize_i386_tdep (void);
1192
1193 void
1194 _initialize_i386_tdep (void)
1195 {
1196 /* Initialize the table saying where each register starts in the
1197 register file. */
1198 {
1199 int i, offset;
1200
1201 offset = 0;
1202 for (i = 0; i < MAX_NUM_REGS; i++)
1203 {
1204 i386_register_byte[i] = offset;
1205 offset += i386_register_raw_size[i];
1206 }
1207 }
1208
1209 /* Initialize the table of virtual register sizes. */
1210 {
1211 int i;
1212
1213 for (i = 0; i < MAX_NUM_REGS; i++)
1214 i386_register_virtual_size[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
1215 }
1216
1217 tm_print_insn = gdb_print_insn_i386;
1218 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1219
1220 /* Add the variable that controls the disassembly flavor. */
1221 {
1222 struct cmd_list_element *new_cmd;
1223
1224 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1225 valid_flavors,
1226 &disassembly_flavor,
1227 "\
1228 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1229 and the default value is \"att\".",
1230 &setlist);
1231 new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
1232 add_show_from_set (new_cmd, &showlist);
1233 }
1234
1235 /* Finally, initialize the disassembly flavor to the default given
1236 in the disassembly_flavor variable. */
1237 set_disassembly_flavor ();
1238 }