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