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