Allow for the possibility that the local labels won't be in the objdump output.
[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 number of args passed to a frame.
353 Can return -1, meaning no way to tell. */
354
355 int
356 i386_frame_num_args (struct frame_info *fi)
357 {
358 #if 1
359 return -1;
360 #else
361 /* This loses because not only might the compiler not be popping the
362 args right after the function call, it might be popping args from
363 both this call and a previous one, and we would say there are
364 more args than there really are. */
365
366 int retpc;
367 unsigned char op;
368 struct frame_info *pfi;
369
370 /* On the i386, the instruction following the call could be:
371 popl %ecx - one arg
372 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
373 anything else - zero args. */
374
375 int frameless;
376
377 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
378 if (frameless)
379 /* In the absence of a frame pointer, GDB doesn't get correct
380 values for nameless arguments. Return -1, so it doesn't print
381 any nameless arguments. */
382 return -1;
383
384 pfi = get_prev_frame (fi);
385 if (pfi == 0)
386 {
387 /* NOTE: This can happen if we are looking at the frame for
388 main, because FRAME_CHAIN_VALID won't let us go into start.
389 If we have debugging symbols, that's not really a big deal;
390 it just means it will only show as many arguments to main as
391 are declared. */
392 return -1;
393 }
394 else
395 {
396 retpc = pfi->pc;
397 op = read_memory_integer (retpc, 1);
398 if (op == 0x59) /* pop %ecx */
399 return 1;
400 else if (op == 0x83)
401 {
402 op = read_memory_integer (retpc + 1, 1);
403 if (op == 0xc4)
404 /* addl $<signed imm 8 bits>, %esp */
405 return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
406 else
407 return 0;
408 }
409 else if (op == 0x81) /* `add' with 32 bit immediate. */
410 {
411 op = read_memory_integer (retpc + 1, 1);
412 if (op == 0xc4)
413 /* addl $<imm 32>, %esp */
414 return read_memory_integer (retpc + 2, 4) / 4;
415 else
416 return 0;
417 }
418 else
419 {
420 return 0;
421 }
422 }
423 #endif
424 }
425
426 /* Parse the first few instructions the function to see what registers
427 were stored.
428
429 We handle these cases:
430
431 The startup sequence can be at the start of the function, or the
432 function can start with a branch to startup code at the end.
433
434 %ebp can be set up with either the 'enter' instruction, or "pushl
435 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
436 once used in the System V compiler).
437
438 Local space is allocated just below the saved %ebp by either the
439 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16
440 bit unsigned argument for space to allocate, and the 'addl'
441 instruction could have either a signed byte, or 32 bit immediate.
442
443 Next, the registers used by this function are pushed. With the
444 System V compiler they will always be in the order: %edi, %esi,
445 %ebx (and sometimes a harmless bug causes it to also save but not
446 restore %eax); however, the code below is willing to see the pushes
447 in any order, and will handle up to 8 of them.
448
449 If the setup sequence is at the end of the function, then the next
450 instruction will be a branch back to the start. */
451
452 void
453 i386_frame_init_saved_regs (struct frame_info *fip)
454 {
455 long locals = -1;
456 unsigned char op;
457 CORE_ADDR dummy_bottom;
458 CORE_ADDR addr;
459 CORE_ADDR pc;
460 int i;
461
462 if (fip->saved_regs)
463 return;
464
465 frame_saved_regs_zalloc (fip);
466
467 /* If the frame is the end of a dummy, compute where the beginning
468 would be. */
469 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
470
471 /* Check if the PC points in the stack, in a dummy frame. */
472 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
473 {
474 /* All registers were saved by push_call_dummy. */
475 addr = fip->frame;
476 for (i = 0; i < NUM_REGS; i++)
477 {
478 addr -= REGISTER_RAW_SIZE (i);
479 fip->saved_regs[i] = addr;
480 }
481 return;
482 }
483
484 pc = get_pc_function_start (fip->pc);
485 if (pc != 0)
486 locals = i386_get_frame_setup (pc);
487
488 if (locals >= 0)
489 {
490 addr = fip->frame - 4 - locals;
491 for (i = 0; i < 8; i++)
492 {
493 op = codestream_get ();
494 if (op < 0x50 || op > 0x57)
495 break;
496 #ifdef I386_REGNO_TO_SYMMETRY
497 /* Dynix uses different internal numbering. Ick. */
498 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr;
499 #else
500 fip->saved_regs[op - 0x50] = addr;
501 #endif
502 addr -= 4;
503 }
504 }
505
506 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
507 fip->saved_regs[FP_REGNUM] = fip->frame;
508 }
509
510 /* Return PC of first real instruction. */
511
512 int
513 i386_skip_prologue (int pc)
514 {
515 unsigned char op;
516 int i;
517 static unsigned char pic_pat[6] =
518 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
519 0x5b, /* popl %ebx */
520 };
521 CORE_ADDR pos;
522
523 if (i386_get_frame_setup (pc) < 0)
524 return (pc);
525
526 /* Found valid frame setup -- codestream now points to start of push
527 instructions for saving registers. */
528
529 /* Skip over register saves. */
530 for (i = 0; i < 8; i++)
531 {
532 op = codestream_peek ();
533 /* Break if not `pushl' instrunction. */
534 if (op < 0x50 || op > 0x57)
535 break;
536 codestream_get ();
537 }
538
539 /* The native cc on SVR4 in -K PIC mode inserts the following code
540 to get the address of the global offset table (GOT) into register
541 %ebx
542
543 call 0x0
544 popl %ebx
545 movl %ebx,x(%ebp) (optional)
546 addl y,%ebx
547
548 This code is with the rest of the prologue (at the end of the
549 function), so we have to skip it to get to the first real
550 instruction at the start of the function. */
551
552 pos = codestream_tell ();
553 for (i = 0; i < 6; i++)
554 {
555 op = codestream_get ();
556 if (pic_pat[i] != op)
557 break;
558 }
559 if (i == 6)
560 {
561 unsigned char buf[4];
562 long delta = 6;
563
564 op = codestream_get ();
565 if (op == 0x89) /* movl %ebx, x(%ebp) */
566 {
567 op = codestream_get ();
568 if (op == 0x5d) /* One byte offset from %ebp. */
569 {
570 delta += 3;
571 codestream_read (buf, 1);
572 }
573 else if (op == 0x9d) /* Four byte offset from %ebp. */
574 {
575 delta += 6;
576 codestream_read (buf, 4);
577 }
578 else /* Unexpected instruction. */
579 delta = -1;
580 op = codestream_get ();
581 }
582 /* addl y,%ebx */
583 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
584 {
585 pos += delta + 6;
586 }
587 }
588 codestream_seek (pos);
589
590 i386_follow_jump ();
591
592 return (codestream_tell ());
593 }
594
595 void
596 i386_push_dummy_frame (void)
597 {
598 CORE_ADDR sp = read_register (SP_REGNUM);
599 int regnum;
600 char regbuf[MAX_REGISTER_RAW_SIZE];
601
602 sp = push_word (sp, read_register (PC_REGNUM));
603 sp = push_word (sp, read_register (FP_REGNUM));
604 write_register (FP_REGNUM, sp);
605 for (regnum = 0; regnum < NUM_REGS; regnum++)
606 {
607 read_register_gen (regnum, regbuf);
608 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
609 }
610 write_register (SP_REGNUM, sp);
611 }
612
613 /* Insert the (relative) function address into the call sequence
614 stored at DYMMY. */
615
616 void
617 i386_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
618 value_ptr *args, struct type *type, int gcc_p)
619 {
620 int from, to, delta, loc;
621
622 loc = (int)(read_register (SP_REGNUM) - CALL_DUMMY_LENGTH);
623 from = loc + 5;
624 to = (int)(fun);
625 delta = to - from;
626
627 *((char *)(dummy) + 1) = (delta & 0xff);
628 *((char *)(dummy) + 2) = ((delta >> 8) & 0xff);
629 *((char *)(dummy) + 3) = ((delta >> 16) & 0xff);
630 *((char *)(dummy) + 4) = ((delta >> 24) & 0xff);
631 }
632
633 void
634 i386_pop_frame (void)
635 {
636 struct frame_info *frame = get_current_frame ();
637 CORE_ADDR fp;
638 int regnum;
639 char regbuf[MAX_REGISTER_RAW_SIZE];
640
641 fp = FRAME_FP (frame);
642 i386_frame_init_saved_regs (frame);
643
644 for (regnum = 0; regnum < NUM_REGS; regnum++)
645 {
646 CORE_ADDR addr;
647 addr = frame->saved_regs[regnum];
648 if (addr)
649 {
650 read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum));
651 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
652 REGISTER_RAW_SIZE (regnum));
653 }
654 }
655 write_register (FP_REGNUM, read_memory_integer (fp, 4));
656 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
657 write_register (SP_REGNUM, fp + 8);
658 flush_cached_frames ();
659 }
660 \f
661
662 #ifdef GET_LONGJMP_TARGET
663
664 /* Figure out where the longjmp will land. Slurp the args out of the
665 stack. We expect the first arg to be a pointer to the jmp_buf
666 structure from which we extract the pc (JB_PC) that we will land
667 at. The pc is copied into PC. This routine returns true on
668 success. */
669
670 int
671 get_longjmp_target (CORE_ADDR *pc)
672 {
673 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
674 CORE_ADDR sp, jb_addr;
675
676 sp = read_register (SP_REGNUM);
677
678 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack. */
679 buf,
680 TARGET_PTR_BIT / TARGET_CHAR_BIT))
681 return 0;
682
683 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
684
685 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
686 TARGET_PTR_BIT / TARGET_CHAR_BIT))
687 return 0;
688
689 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
690
691 return 1;
692 }
693
694 #endif /* GET_LONGJMP_TARGET */
695 \f
696
697 CORE_ADDR
698 i386_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
699 int struct_return, CORE_ADDR struct_addr)
700 {
701 sp = default_push_arguments (nargs, args, sp, struct_return, struct_addr);
702
703 if (struct_return)
704 {
705 char buf[4];
706
707 sp -= 4;
708 store_address (buf, 4, struct_addr);
709 write_memory (sp, buf, 4);
710 }
711
712 return sp;
713 }
714
715 void
716 i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
717 {
718 /* Do nothing. Everything was already done by i386_push_arguments. */
719 }
720
721 /* These registers are used for returning integers (and on some
722 targets also for returning `struct' and `union' values when their
723 size and alignment match an integer type). */
724 #define LOW_RETURN_REGNUM 0 /* %eax */
725 #define HIGH_RETURN_REGNUM 2 /* %edx */
726
727 /* Extract from an array REGBUF containing the (raw) register state, a
728 function return value of TYPE, and copy that, in virtual format,
729 into VALBUF. */
730
731 void
732 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
733 {
734 int len = TYPE_LENGTH (type);
735
736 if (TYPE_CODE_FLT == TYPE_CODE (type))
737 {
738 if (NUM_FREGS == 0)
739 {
740 warning ("Cannot find floating-point return value.");
741 memset (valbuf, 0, len);
742 return;
743 }
744
745 /* Floating-point return values can be found in %st(0). */
746 if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
747 && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
748 {
749 /* Copy straight over, but take care of the padding. */
750 memcpy (valbuf, &regbuf[REGISTER_BYTE (FP0_REGNUM)],
751 FPU_REG_RAW_SIZE);
752 memset (valbuf + FPU_REG_RAW_SIZE, 0, len - FPU_REG_RAW_SIZE);
753 }
754 else
755 {
756 /* Convert the extended floating-point number found in
757 %st(0) to the desired type. This is probably not exactly
758 how it would happen on the target itself, but it is the
759 best we can do. */
760 DOUBLEST val;
761 floatformat_to_doublest (&floatformat_i387_ext,
762 &regbuf[REGISTER_BYTE (FP0_REGNUM)], &val);
763 store_floating (valbuf, TYPE_LENGTH (type), val);
764 }
765 }
766 else
767 {
768 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
769 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
770
771 if (len <= low_size)
772 memcpy (valbuf, &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
773 else if (len <= (low_size + high_size))
774 {
775 memcpy (valbuf,
776 &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
777 memcpy (valbuf + low_size,
778 &regbuf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
779 }
780 else
781 internal_error (__FILE__, __LINE__,
782 "Cannot extract return value of %d bytes long.", len);
783 }
784 }
785
786 /* Write into the appropriate registers a function return value stored
787 in VALBUF of type TYPE, given in virtual format. */
788
789 void
790 i386_store_return_value (struct type *type, char *valbuf)
791 {
792 int len = TYPE_LENGTH (type);
793
794 if (TYPE_CODE_FLT == TYPE_CODE (type))
795 {
796 if (NUM_FREGS == 0)
797 {
798 warning ("Cannot set floating-point return value.");
799 return;
800 }
801
802 /* Floating-point return values can be found in %st(0). */
803 if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
804 && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
805 {
806 /* Copy straight over. */
807 write_register_bytes (REGISTER_BYTE (FP0_REGNUM), valbuf,
808 FPU_REG_RAW_SIZE);
809 }
810 else
811 {
812 char buf[FPU_REG_RAW_SIZE];
813 DOUBLEST val;
814
815 /* Convert the value found in VALBUF to the extended
816 floating point format used by the FPU. This is probably
817 not exactly how it would happen on the target itself, but
818 it is the best we can do. */
819 val = extract_floating (valbuf, TYPE_LENGTH (type));
820 floatformat_from_doublest (&floatformat_i387_ext, &val, buf);
821 write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
822 FPU_REG_RAW_SIZE);
823 }
824 }
825 else
826 {
827 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
828 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
829
830 if (len <= low_size)
831 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), valbuf, len);
832 else if (len <= (low_size + high_size))
833 {
834 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM),
835 valbuf, low_size);
836 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM),
837 valbuf + low_size, len - low_size);
838 }
839 else
840 internal_error (__FILE__, __LINE__,
841 "Cannot store return value of %d bytes long.", len);
842 }
843 }
844 \f
845
846 /* Convert data from raw format for register REGNUM in buffer FROM to
847 virtual format with type TYPE in buffer TO. In principle both
848 formats are identical except that the virtual format has two extra
849 bytes appended that aren't used. We set these to zero. */
850
851 void
852 i386_register_convert_to_virtual (int regnum, struct type *type,
853 char *from, char *to)
854 {
855 /* Copy straight over, but take care of the padding. */
856 memcpy (to, from, FPU_REG_RAW_SIZE);
857 memset (to + FPU_REG_RAW_SIZE, 0, TYPE_LENGTH (type) - FPU_REG_RAW_SIZE);
858 }
859
860 /* Convert data from virtual format with type TYPE in buffer FROM to
861 raw format for register REGNUM in buffer TO. Simply omit the two
862 unused bytes. */
863
864 void
865 i386_register_convert_to_raw (struct type *type, int regnum,
866 char *from, char *to)
867 {
868 memcpy (to, from, FPU_REG_RAW_SIZE);
869 }
870 \f
871
872 #ifdef I386V4_SIGTRAMP_SAVED_PC
873 /* Get saved user PC for sigtramp from the pushed ucontext on the
874 stack for all three variants of SVR4 sigtramps. */
875
876 CORE_ADDR
877 i386v4_sigtramp_saved_pc (struct frame_info *frame)
878 {
879 CORE_ADDR saved_pc_offset = 4;
880 char *name = NULL;
881
882 find_pc_partial_function (frame->pc, &name, NULL, NULL);
883 if (name)
884 {
885 if (STREQ (name, "_sigreturn"))
886 saved_pc_offset = 132 + 14 * 4;
887 else if (STREQ (name, "_sigacthandler"))
888 saved_pc_offset = 80 + 14 * 4;
889 else if (STREQ (name, "sigvechandler"))
890 saved_pc_offset = 120 + 14 * 4;
891 }
892
893 if (frame->next)
894 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
895 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
896 }
897 #endif /* I386V4_SIGTRAMP_SAVED_PC */
898 \f
899
900 #ifdef STATIC_TRANSFORM_NAME
901 /* SunPRO encodes the static variables. This is not related to C++
902 mangling, it is done for C too. */
903
904 char *
905 sunpro_static_transform_name (char *name)
906 {
907 char *p;
908 if (IS_STATIC_TRANSFORM_NAME (name))
909 {
910 /* For file-local statics there will be a period, a bunch of
911 junk (the contents of which match a string given in the
912 N_OPT), a period and the name. For function-local statics
913 there will be a bunch of junk (which seems to change the
914 second character from 'A' to 'B'), a period, the name of the
915 function, and the name. So just skip everything before the
916 last period. */
917 p = strrchr (name, '.');
918 if (p != NULL)
919 name = p + 1;
920 }
921 return name;
922 }
923 #endif /* STATIC_TRANSFORM_NAME */
924 \f
925
926 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
927
928 CORE_ADDR
929 skip_trampoline_code (CORE_ADDR pc, char *name)
930 {
931 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
932 {
933 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
934 struct minimal_symbol *indsym =
935 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
936 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
937
938 if (symname)
939 {
940 if (strncmp (symname, "__imp_", 6) == 0
941 || strncmp (symname, "_imp_", 5) == 0)
942 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
943 }
944 }
945 return 0; /* Not a trampoline. */
946 }
947 \f
948
949 /* We have two flavours of disassembly. The machinery on this page
950 deals with switching between those. */
951
952 static int
953 gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
954 {
955 if (disassembly_flavor == att_flavor)
956 return print_insn_i386_att (memaddr, info);
957 else if (disassembly_flavor == intel_flavor)
958 return print_insn_i386_intel (memaddr, info);
959 /* Never reached -- disassembly_flavour is always either att_flavor
960 or intel_flavor. */
961 internal_error (__FILE__, __LINE__, "failed internal consistency check");
962 }
963
964 /* If the disassembly mode is intel, we have to also switch the bfd
965 mach_type. This function is run in the set disassembly_flavor
966 command, and does that. */
967
968 static void
969 set_disassembly_flavor_sfunc (char *args, int from_tty,
970 struct cmd_list_element *c)
971 {
972 set_disassembly_flavor ();
973 }
974
975 static void
976 set_disassembly_flavor (void)
977 {
978 if (disassembly_flavor == att_flavor)
979 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386);
980 else if (disassembly_flavor == intel_flavor)
981 set_architecture_from_arch_mach (bfd_arch_i386,
982 bfd_mach_i386_i386_intel_syntax);
983 }
984 \f
985
986 /* Provide a prototype to silence -Wmissing-prototypes. */
987 void _initialize_i386_tdep (void);
988
989 void
990 _initialize_i386_tdep (void)
991 {
992 /* Initialize the table saying where each register starts in the
993 register file. */
994 {
995 int i, offset;
996
997 offset = 0;
998 for (i = 0; i < MAX_NUM_REGS; i++)
999 {
1000 i386_register_byte[i] = offset;
1001 offset += i386_register_raw_size[i];
1002 }
1003 }
1004
1005 /* Initialize the table of virtual register sizes. */
1006 {
1007 int i;
1008
1009 for (i = 0; i < MAX_NUM_REGS; i++)
1010 i386_register_virtual_size[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
1011 }
1012
1013 tm_print_insn = gdb_print_insn_i386;
1014 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1015
1016 /* Add the variable that controls the disassembly flavor. */
1017 {
1018 struct cmd_list_element *new_cmd;
1019
1020 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1021 valid_flavors,
1022 &disassembly_flavor,
1023 "\
1024 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1025 and the default value is \"att\".",
1026 &setlist);
1027 new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
1028 add_show_from_set (new_cmd, &showlist);
1029 }
1030
1031 /* Finally, initialize the disassembly flavor to the default given
1032 in the disassembly_flavor variable. */
1033 set_disassembly_flavor ();
1034 }
This page took 0.056302 seconds and 4 git commands to generate.