Use ``set remote memory-read-packet-size'' instead of ``set endian
[deliverable/binutils-gdb.git] / gdb / i386-tdep.c
CommitLineData
c906108c
SS
1/* Intel 386 target-dependent stuff.
2 Copyright (C) 1988, 1989, 1991, 1994, 1995, 1996, 1998
3 Free Software Foundation, Inc.
4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "gdb_string.h"
24#include "frame.h"
25#include "inferior.h"
26#include "gdbcore.h"
27#include "target.h"
28#include "floatformat.h"
29#include "symtab.h"
30#include "gdbcmd.h"
31#include "command.h"
32
a14ed312 33static long i386_get_frame_setup (CORE_ADDR);
c906108c 34
a14ed312 35static void i386_follow_jump (void);
c906108c 36
a14ed312 37static void codestream_read (unsigned char *, int);
c906108c 38
a14ed312 39static void codestream_seek (CORE_ADDR);
c906108c 40
a14ed312 41static unsigned char codestream_fill (int);
c906108c 42
a14ed312 43CORE_ADDR skip_trampoline_code (CORE_ADDR, char *);
c906108c
SS
44
45static int gdb_print_insn_i386 (bfd_vma, disassemble_info *);
46
a14ed312 47void _initialize_i386_tdep (void);
c906108c 48
917317f4
JM
49/* i386_register_byte[i] is the offset into the register file of the
50 start of register number i. We initialize this from
51 i386_register_raw_size. */
52int i386_register_byte[MAX_NUM_REGS];
53
ceb4951f
JB
54/* i386_register_raw_size[i] is the number of bytes of storage in
55 GDB's register array occupied by register i. */
917317f4
JM
56int i386_register_raw_size[MAX_NUM_REGS] = {
57 4, 4, 4, 4,
58 4, 4, 4, 4,
59 4, 4, 4, 4,
60 4, 4, 4, 4,
61 10, 10, 10, 10,
62 10, 10, 10, 10,
63 4, 4, 4, 4,
64 4, 4, 4, 4,
65 16, 16, 16, 16,
66 16, 16, 16, 16,
67 4
68};
69
70/* i386_register_virtual_size[i] is the size in bytes of the virtual
71 type of register i. */
72int i386_register_virtual_size[MAX_NUM_REGS];
73
74
c906108c 75/* This is the variable the is set with "set disassembly-flavor",
c5aa993b 76 and its legitimate values. */
c906108c
SS
77static char att_flavor[] = "att";
78static char intel_flavor[] = "intel";
c5aa993b
JM
79static char *valid_flavors[] =
80{
c906108c
SS
81 att_flavor,
82 intel_flavor,
83 NULL
84};
85static char *disassembly_flavor = att_flavor;
86
a14ed312 87static void i386_print_register (char *, int, int);
d4f3574e 88
7a292a7a 89/* This is used to keep the bfd arch_info in sync with the disassembly flavor. */
a14ed312
KB
90static void set_disassembly_flavor_sfunc (char *, int,
91 struct cmd_list_element *);
92static void set_disassembly_flavor (void);
7a292a7a 93
c906108c
SS
94/* Stdio style buffering was used to minimize calls to ptrace, but this
95 buffering did not take into account that the code section being accessed
96 may not be an even number of buffers long (even if the buffer is only
97 sizeof(int) long). In cases where the code section size happened to
98 be a non-integral number of buffers long, attempting to read the last
99 buffer would fail. Simply using target_read_memory and ignoring errors,
100 rather than read_memory, is not the correct solution, since legitimate
101 access errors would then be totally ignored. To properly handle this
102 situation and continue to use buffering would require that this code
103 be able to determine the minimum code section size granularity (not the
104 alignment of the section itself, since the actual failing case that
105 pointed out this problem had a section alignment of 4 but was not a
106 multiple of 4 bytes long), on a target by target basis, and then
107 adjust it's buffer size accordingly. This is messy, but potentially
108 feasible. It probably needs the bfd library's help and support. For
109 now, the buffer size is set to 1. (FIXME -fnf) */
110
111#define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
112static CORE_ADDR codestream_next_addr;
113static CORE_ADDR codestream_addr;
114static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
115static int codestream_off;
116static int codestream_cnt;
117
118#define codestream_tell() (codestream_addr + codestream_off)
119#define codestream_peek() (codestream_cnt == 0 ? \
120 codestream_fill(1): codestream_buf[codestream_off])
121#define codestream_get() (codestream_cnt-- == 0 ? \
122 codestream_fill(0) : codestream_buf[codestream_off++])
123
c5aa993b 124static unsigned char
c906108c 125codestream_fill (peek_flag)
c5aa993b 126 int peek_flag;
c906108c
SS
127{
128 codestream_addr = codestream_next_addr;
129 codestream_next_addr += CODESTREAM_BUFSIZ;
130 codestream_off = 0;
131 codestream_cnt = CODESTREAM_BUFSIZ;
132 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
c5aa993b 133
c906108c 134 if (peek_flag)
c5aa993b 135 return (codestream_peek ());
c906108c 136 else
c5aa993b 137 return (codestream_get ());
c906108c
SS
138}
139
140static void
141codestream_seek (place)
c5aa993b 142 CORE_ADDR place;
c906108c
SS
143{
144 codestream_next_addr = place / CODESTREAM_BUFSIZ;
145 codestream_next_addr *= CODESTREAM_BUFSIZ;
146 codestream_cnt = 0;
147 codestream_fill (1);
c5aa993b 148 while (codestream_tell () != place)
c906108c
SS
149 codestream_get ();
150}
151
152static void
153codestream_read (buf, count)
154 unsigned char *buf;
155 int count;
156{
157 unsigned char *p;
158 int i;
159 p = buf;
160 for (i = 0; i < count; i++)
161 *p++ = codestream_get ();
162}
163
164/* next instruction is a jump, move to target */
165
166static void
167i386_follow_jump ()
168{
169 unsigned char buf[4];
170 long delta;
171
172 int data16;
173 CORE_ADDR pos;
174
175 pos = codestream_tell ();
176
177 data16 = 0;
178 if (codestream_peek () == 0x66)
179 {
180 codestream_get ();
181 data16 = 1;
182 }
183
184 switch (codestream_get ())
185 {
186 case 0xe9:
187 /* relative jump: if data16 == 0, disp32, else disp16 */
188 if (data16)
189 {
190 codestream_read (buf, 2);
191 delta = extract_signed_integer (buf, 2);
192
193 /* include size of jmp inst (including the 0x66 prefix). */
c5aa993b 194 pos += delta + 4;
c906108c
SS
195 }
196 else
197 {
198 codestream_read (buf, 4);
199 delta = extract_signed_integer (buf, 4);
200
201 pos += delta + 5;
202 }
203 break;
204 case 0xeb:
205 /* relative jump, disp8 (ignore data16) */
206 codestream_read (buf, 1);
207 /* Sign-extend it. */
208 delta = extract_signed_integer (buf, 1);
209
210 pos += delta + 2;
211 break;
212 }
213 codestream_seek (pos);
214}
215
216/*
217 * find & return amound a local space allocated, and advance codestream to
218 * first register push (if any)
219 *
220 * if entry sequence doesn't make sense, return -1, and leave
221 * codestream pointer random
222 */
223
224static long
225i386_get_frame_setup (pc)
226 CORE_ADDR pc;
227{
228 unsigned char op;
229
230 codestream_seek (pc);
231
232 i386_follow_jump ();
233
234 op = codestream_get ();
235
236 if (op == 0x58) /* popl %eax */
237 {
238 /*
239 * this function must start with
240 *
c5aa993b 241 * popl %eax 0x58
c906108c
SS
242 * xchgl %eax, (%esp) 0x87 0x04 0x24
243 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
244 *
245 * (the system 5 compiler puts out the second xchg
246 * inst, and the assembler doesn't try to optimize it,
247 * so the 'sib' form gets generated)
248 *
249 * this sequence is used to get the address of the return
250 * buffer for a function that returns a structure
251 */
252 int pos;
253 unsigned char buf[4];
c5aa993b
JM
254 static unsigned char proto1[3] =
255 {0x87, 0x04, 0x24};
256 static unsigned char proto2[4] =
257 {0x87, 0x44, 0x24, 0x00};
c906108c
SS
258 pos = codestream_tell ();
259 codestream_read (buf, 4);
260 if (memcmp (buf, proto1, 3) == 0)
261 pos += 3;
262 else if (memcmp (buf, proto2, 4) == 0)
263 pos += 4;
264
265 codestream_seek (pos);
c5aa993b 266 op = codestream_get (); /* update next opcode */
c906108c
SS
267 }
268
269 if (op == 0x68 || op == 0x6a)
270 {
271 /*
272 * this function may start with
273 *
274 * pushl constant
275 * call _probe
276 * addl $4, %esp
277 * followed by
278 * pushl %ebp
279 * etc.
280 */
281 int pos;
282 unsigned char buf[8];
283
284 /* Skip past the pushl instruction; it has either a one-byte
285 or a four-byte operand, depending on the opcode. */
286 pos = codestream_tell ();
287 if (op == 0x68)
288 pos += 4;
289 else
290 pos += 1;
291 codestream_seek (pos);
292
293 /* Read the following 8 bytes, which should be "call _probe" (6 bytes)
294 followed by "addl $4,%esp" (2 bytes). */
295 codestream_read (buf, sizeof (buf));
296 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
297 pos += sizeof (buf);
298 codestream_seek (pos);
c5aa993b 299 op = codestream_get (); /* update next opcode */
c906108c
SS
300 }
301
302 if (op == 0x55) /* pushl %ebp */
c5aa993b 303 {
c906108c
SS
304 /* check for movl %esp, %ebp - can be written two ways */
305 switch (codestream_get ())
306 {
307 case 0x8b:
308 if (codestream_get () != 0xec)
309 return (-1);
310 break;
311 case 0x89:
312 if (codestream_get () != 0xe5)
313 return (-1);
314 break;
315 default:
316 return (-1);
317 }
318 /* check for stack adjustment
c5aa993b 319
c906108c
SS
320 * subl $XXX, %esp
321 *
322 * note: you can't subtract a 16 bit immediate
323 * from a 32 bit reg, so we don't have to worry
324 * about a data16 prefix
325 */
326 op = codestream_peek ();
327 if (op == 0x83)
328 {
329 /* subl with 8 bit immed */
330 codestream_get ();
331 if (codestream_get () != 0xec)
332 /* Some instruction starting with 0x83 other than subl. */
333 {
334 codestream_seek (codestream_tell () - 2);
335 return 0;
336 }
337 /* subl with signed byte immediate
338 * (though it wouldn't make sense to be negative)
339 */
c5aa993b 340 return (codestream_get ());
c906108c
SS
341 }
342 else if (op == 0x81)
343 {
344 char buf[4];
345 /* Maybe it is subl with 32 bit immedediate. */
c5aa993b 346 codestream_get ();
c906108c
SS
347 if (codestream_get () != 0xec)
348 /* Some instruction starting with 0x81 other than subl. */
349 {
350 codestream_seek (codestream_tell () - 2);
351 return 0;
352 }
353 /* It is subl with 32 bit immediate. */
c5aa993b 354 codestream_read ((unsigned char *) buf, 4);
c906108c
SS
355 return extract_signed_integer (buf, 4);
356 }
357 else
358 {
359 return (0);
360 }
361 }
362 else if (op == 0xc8)
363 {
364 char buf[2];
365 /* enter instruction: arg is 16 bit unsigned immed */
c5aa993b
JM
366 codestream_read ((unsigned char *) buf, 2);
367 codestream_get (); /* flush final byte of enter instruction */
c906108c
SS
368 return extract_unsigned_integer (buf, 2);
369 }
370 return (-1);
371}
372
373/* Return number of args passed to a frame.
374 Can return -1, meaning no way to tell. */
375
376int
377i386_frame_num_args (fi)
378 struct frame_info *fi;
379{
380#if 1
381 return -1;
382#else
383 /* This loses because not only might the compiler not be popping the
384 args right after the function call, it might be popping args from both
385 this call and a previous one, and we would say there are more args
386 than there really are. */
387
c5aa993b
JM
388 int retpc;
389 unsigned char op;
c906108c
SS
390 struct frame_info *pfi;
391
392 /* on the 386, the instruction following the call could be:
393 popl %ecx - one arg
394 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
395 anything else - zero args */
396
397 int frameless;
398
392a587b 399 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
c906108c
SS
400 if (frameless)
401 /* In the absence of a frame pointer, GDB doesn't get correct values
402 for nameless arguments. Return -1, so it doesn't print any
403 nameless arguments. */
404 return -1;
405
c5aa993b 406 pfi = get_prev_frame (fi);
c906108c
SS
407 if (pfi == 0)
408 {
409 /* Note: this can happen if we are looking at the frame for
c5aa993b
JM
410 main, because FRAME_CHAIN_VALID won't let us go into
411 start. If we have debugging symbols, that's not really
412 a big deal; it just means it will only show as many arguments
413 to main as are declared. */
c906108c
SS
414 return -1;
415 }
416 else
417 {
c5aa993b
JM
418 retpc = pfi->pc;
419 op = read_memory_integer (retpc, 1);
420 if (op == 0x59)
421 /* pop %ecx */
422 return 1;
c906108c
SS
423 else if (op == 0x83)
424 {
c5aa993b
JM
425 op = read_memory_integer (retpc + 1, 1);
426 if (op == 0xc4)
427 /* addl $<signed imm 8 bits>, %esp */
428 return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
c906108c
SS
429 else
430 return 0;
431 }
432 else if (op == 0x81)
c5aa993b
JM
433 { /* add with 32 bit immediate */
434 op = read_memory_integer (retpc + 1, 1);
435 if (op == 0xc4)
436 /* addl $<imm 32>, %esp */
437 return read_memory_integer (retpc + 2, 4) / 4;
c906108c
SS
438 else
439 return 0;
440 }
441 else
442 {
443 return 0;
444 }
445 }
446#endif
447}
448
449/*
450 * parse the first few instructions of the function to see
451 * what registers were stored.
452 *
453 * We handle these cases:
454 *
455 * The startup sequence can be at the start of the function,
456 * or the function can start with a branch to startup code at the end.
457 *
458 * %ebp can be set up with either the 'enter' instruction, or
459 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
460 * but was once used in the sys5 compiler)
461 *
462 * Local space is allocated just below the saved %ebp by either the
463 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
464 * a 16 bit unsigned argument for space to allocate, and the
465 * 'addl' instruction could have either a signed byte, or
466 * 32 bit immediate.
467 *
468 * Next, the registers used by this function are pushed. In
469 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
470 * (and sometimes a harmless bug causes it to also save but not restore %eax);
471 * however, the code below is willing to see the pushes in any order,
472 * and will handle up to 8 of them.
473 *
474 * If the setup sequence is at the end of the function, then the
475 * next instruction will be a branch back to the start.
476 */
477
478void
1211c4e4 479i386_frame_init_saved_regs (fip)
c906108c 480 struct frame_info *fip;
c906108c
SS
481{
482 long locals = -1;
483 unsigned char op;
484 CORE_ADDR dummy_bottom;
485 CORE_ADDR adr;
486 CORE_ADDR pc;
487 int i;
c5aa993b 488
1211c4e4
AC
489 if (fip->saved_regs)
490 return;
491
492 frame_saved_regs_zalloc (fip);
c5aa993b 493
c906108c
SS
494 /* if frame is the end of a dummy, compute where the
495 * beginning would be
496 */
497 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
c5aa993b 498
c906108c 499 /* check if the PC is in the stack, in a dummy frame */
c5aa993b 500 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
c906108c
SS
501 {
502 /* all regs were saved by push_call_dummy () */
503 adr = fip->frame;
c5aa993b 504 for (i = 0; i < NUM_REGS; i++)
c906108c
SS
505 {
506 adr -= REGISTER_RAW_SIZE (i);
1211c4e4 507 fip->saved_regs[i] = adr;
c906108c
SS
508 }
509 return;
510 }
c5aa993b 511
c906108c
SS
512 pc = get_pc_function_start (fip->pc);
513 if (pc != 0)
514 locals = i386_get_frame_setup (pc);
c5aa993b
JM
515
516 if (locals >= 0)
c906108c
SS
517 {
518 adr = fip->frame - 4 - locals;
c5aa993b 519 for (i = 0; i < 8; i++)
c906108c
SS
520 {
521 op = codestream_get ();
522 if (op < 0x50 || op > 0x57)
523 break;
524#ifdef I386_REGNO_TO_SYMMETRY
525 /* Dynix uses different internal numbering. Ick. */
1211c4e4 526 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = adr;
c906108c 527#else
1211c4e4 528 fip->saved_regs[op - 0x50] = adr;
c906108c
SS
529#endif
530 adr -= 4;
531 }
532 }
c5aa993b 533
1211c4e4
AC
534 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
535 fip->saved_regs[FP_REGNUM] = fip->frame;
c906108c
SS
536}
537
538/* return pc of first real instruction */
539
540int
541i386_skip_prologue (pc)
542 int pc;
543{
544 unsigned char op;
545 int i;
c5aa993b
JM
546 static unsigned char pic_pat[6] =
547 {0xe8, 0, 0, 0, 0, /* call 0x0 */
548 0x5b, /* popl %ebx */
549 };
c906108c 550 CORE_ADDR pos;
c5aa993b 551
c906108c
SS
552 if (i386_get_frame_setup (pc) < 0)
553 return (pc);
c5aa993b 554
c906108c
SS
555 /* found valid frame setup - codestream now points to
556 * start of push instructions for saving registers
557 */
c5aa993b 558
c906108c
SS
559 /* skip over register saves */
560 for (i = 0; i < 8; i++)
561 {
562 op = codestream_peek ();
563 /* break if not pushl inst */
c5aa993b 564 if (op < 0x50 || op > 0x57)
c906108c
SS
565 break;
566 codestream_get ();
567 }
568
569 /* The native cc on SVR4 in -K PIC mode inserts the following code to get
570 the address of the global offset table (GOT) into register %ebx.
c5aa993b
JM
571 call 0x0
572 popl %ebx
573 movl %ebx,x(%ebp) (optional)
574 addl y,%ebx
c906108c
SS
575 This code is with the rest of the prologue (at the end of the
576 function), so we have to skip it to get to the first real
577 instruction at the start of the function. */
c5aa993b 578
c906108c
SS
579 pos = codestream_tell ();
580 for (i = 0; i < 6; i++)
581 {
582 op = codestream_get ();
c5aa993b 583 if (pic_pat[i] != op)
c906108c
SS
584 break;
585 }
586 if (i == 6)
587 {
588 unsigned char buf[4];
589 long delta = 6;
590
591 op = codestream_get ();
c5aa993b 592 if (op == 0x89) /* movl %ebx, x(%ebp) */
c906108c
SS
593 {
594 op = codestream_get ();
c5aa993b 595 if (op == 0x5d) /* one byte offset from %ebp */
c906108c
SS
596 {
597 delta += 3;
598 codestream_read (buf, 1);
599 }
c5aa993b 600 else if (op == 0x9d) /* four byte offset from %ebp */
c906108c
SS
601 {
602 delta += 6;
603 codestream_read (buf, 4);
604 }
c5aa993b
JM
605 else /* unexpected instruction */
606 delta = -1;
607 op = codestream_get ();
c906108c 608 }
c5aa993b
JM
609 /* addl y,%ebx */
610 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
c906108c 611 {
c5aa993b 612 pos += delta + 6;
c906108c
SS
613 }
614 }
615 codestream_seek (pos);
c5aa993b 616
c906108c 617 i386_follow_jump ();
c5aa993b 618
c906108c
SS
619 return (codestream_tell ());
620}
621
622void
623i386_push_dummy_frame ()
624{
625 CORE_ADDR sp = read_register (SP_REGNUM);
626 int regnum;
627 char regbuf[MAX_REGISTER_RAW_SIZE];
c5aa993b 628
c906108c
SS
629 sp = push_word (sp, read_register (PC_REGNUM));
630 sp = push_word (sp, read_register (FP_REGNUM));
631 write_register (FP_REGNUM, sp);
632 for (regnum = 0; regnum < NUM_REGS; regnum++)
633 {
634 read_register_gen (regnum, regbuf);
635 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
636 }
637 write_register (SP_REGNUM, sp);
638}
639
640void
641i386_pop_frame ()
642{
643 struct frame_info *frame = get_current_frame ();
644 CORE_ADDR fp;
645 int regnum;
c906108c 646 char regbuf[MAX_REGISTER_RAW_SIZE];
c5aa993b 647
c906108c 648 fp = FRAME_FP (frame);
1211c4e4
AC
649 i386_frame_init_saved_regs (frame);
650
c5aa993b 651 for (regnum = 0; regnum < NUM_REGS; regnum++)
c906108c
SS
652 {
653 CORE_ADDR adr;
1211c4e4 654 adr = frame->saved_regs[regnum];
c906108c
SS
655 if (adr)
656 {
657 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
658 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
659 REGISTER_RAW_SIZE (regnum));
660 }
661 }
662 write_register (FP_REGNUM, read_memory_integer (fp, 4));
663 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
664 write_register (SP_REGNUM, fp + 8);
665 flush_cached_frames ();
666}
667
668#ifdef GET_LONGJMP_TARGET
669
670/* Figure out where the longjmp will land. Slurp the args out of the stack.
671 We expect the first arg to be a pointer to the jmp_buf structure from which
672 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
673 This routine returns true on success. */
674
675int
c5aa993b 676get_longjmp_target (pc)
c906108c
SS
677 CORE_ADDR *pc;
678{
679 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
680 CORE_ADDR sp, jb_addr;
681
682 sp = read_register (SP_REGNUM);
683
c5aa993b 684 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
c906108c
SS
685 buf,
686 TARGET_PTR_BIT / TARGET_CHAR_BIT))
687 return 0;
688
689 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
690
691 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
692 TARGET_PTR_BIT / TARGET_CHAR_BIT))
693 return 0;
694
695 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
696
697 return 1;
698}
699
700#endif /* GET_LONGJMP_TARGET */
701
1a309862
MK
702/* These registers are used for returning integers (and on some
703 targets also for returning `struct' and `union' values when their
704 size and alignment match an integer type. */
705#define LOW_RETURN_REGNUM 0 /* %eax */
706#define HIGH_RETURN_REGNUM 2 /* %edx */
707
708/* Extract from an array REGBUF containing the (raw) register state, a
709 function return value of TYPE, and copy that, in virtual format,
710 into VALBUF. */
711
c906108c 712void
1a309862 713i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
c906108c 714{
1a309862
MK
715 int len = TYPE_LENGTH (type);
716
c5aa993b 717 if (TYPE_CODE_FLT == TYPE_CODE (type))
c906108c 718 {
1a309862
MK
719 if (NUM_FREGS == 0)
720 {
721 warning ("Cannot find floating-point return value.");
722 memset (valbuf, 0, len);
723 }
724
725 /* Floating-point return values can be found in %st(0). */
726 if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
727 && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
728 {
729 /* Copy straight over, but take care of the padding. */
730 memcpy (valbuf, &regbuf[REGISTER_BYTE (FP0_REGNUM)],
731 FPU_REG_RAW_SIZE);
732 memset (valbuf + FPU_REG_RAW_SIZE, 0, len - FPU_REG_RAW_SIZE);
733 }
734 else
735 {
736 /* Convert the extended floating-point number found in
737 %st(0) to the desired type. This is probably not exactly
738 how it would happen on the target itself, but it is the
739 best we can do. */
740 DOUBLEST val;
741 floatformat_to_doublest (&floatformat_i387_ext,
742 &regbuf[REGISTER_BYTE (FP0_REGNUM)], &val);
743 store_floating (valbuf, TYPE_LENGTH (type), val);
744 }
c906108c
SS
745 }
746 else
c5aa993b 747 {
d4f3574e
SS
748 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
749 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
750
751 if (len <= low_size)
1a309862 752 memcpy (valbuf, &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
d4f3574e
SS
753 else if (len <= (low_size + high_size))
754 {
755 memcpy (valbuf,
1a309862 756 &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
d4f3574e 757 memcpy (valbuf + low_size,
1a309862 758 &regbuf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
d4f3574e
SS
759 }
760 else
1a309862 761 internal_error ("Cannot extract return value of %d bytes long.", len);
c906108c
SS
762 }
763}
764
ac27f131
MK
765/* Convert data from raw format for register REGNUM in buffer FROM to
766 virtual format with type TYPE in buffer TO. In principle both
767 formats are identical except that the virtual format has two extra
768 bytes appended that aren't used. We set these to zero. */
769
770void
771i386_register_convert_to_virtual (int regnum, struct type *type,
772 char *from, char *to)
773{
774 /* Copy straight over, but take care of the padding. */
775 memcpy (to, from, FPU_REG_RAW_SIZE);
776 memset (to + FPU_REG_RAW_SIZE, 0, TYPE_LENGTH (type) - FPU_REG_RAW_SIZE);
777}
778
779/* Convert data from virtual format with type TYPE in buffer FROM to
780 raw format for register REGNUM in buffer TO. Simply omit the two
781 unused bytes. */
782
783void
784i386_register_convert_to_raw (struct type *type, int regnum,
785 char *from, char *to)
786{
787 memcpy (to, from, FPU_REG_RAW_SIZE);
788}
789
790\f
c906108c
SS
791#ifdef I386V4_SIGTRAMP_SAVED_PC
792/* Get saved user PC for sigtramp from the pushed ucontext on the stack
793 for all three variants of SVR4 sigtramps. */
794
795CORE_ADDR
796i386v4_sigtramp_saved_pc (frame)
797 struct frame_info *frame;
798{
799 CORE_ADDR saved_pc_offset = 4;
800 char *name = NULL;
801
802 find_pc_partial_function (frame->pc, &name, NULL, NULL);
803 if (name)
804 {
805 if (STREQ (name, "_sigreturn"))
806 saved_pc_offset = 132 + 14 * 4;
807 else if (STREQ (name, "_sigacthandler"))
808 saved_pc_offset = 80 + 14 * 4;
809 else if (STREQ (name, "sigvechandler"))
810 saved_pc_offset = 120 + 14 * 4;
811 }
812
813 if (frame->next)
814 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
815 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
816}
817#endif /* I386V4_SIGTRAMP_SAVED_PC */
818
a0b3c4fd 819
c906108c
SS
820#ifdef STATIC_TRANSFORM_NAME
821/* SunPRO encodes the static variables. This is not related to C++ mangling,
822 it is done for C too. */
823
824char *
825sunpro_static_transform_name (name)
826 char *name;
827{
828 char *p;
829 if (IS_STATIC_TRANSFORM_NAME (name))
830 {
831 /* For file-local statics there will be a period, a bunch
c5aa993b
JM
832 of junk (the contents of which match a string given in the
833 N_OPT), a period and the name. For function-local statics
834 there will be a bunch of junk (which seems to change the
835 second character from 'A' to 'B'), a period, the name of the
836 function, and the name. So just skip everything before the
837 last period. */
c906108c
SS
838 p = strrchr (name, '.');
839 if (p != NULL)
840 name = p + 1;
841 }
842 return name;
843}
844#endif /* STATIC_TRANSFORM_NAME */
845
846
847
848/* Stuff for WIN32 PE style DLL's but is pretty generic really. */
849
850CORE_ADDR
851skip_trampoline_code (pc, name)
852 CORE_ADDR pc;
853 char *name;
854{
c5aa993b 855 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
c906108c 856 {
c5aa993b 857 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
c906108c 858 struct minimal_symbol *indsym =
c5aa993b
JM
859 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
860 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
c906108c 861
c5aa993b 862 if (symname)
c906108c 863 {
c5aa993b
JM
864 if (strncmp (symname, "__imp_", 6) == 0
865 || strncmp (symname, "_imp_", 5) == 0)
c906108c
SS
866 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
867 }
868 }
869 return 0; /* not a trampoline */
870}
871
872static int
873gdb_print_insn_i386 (memaddr, info)
874 bfd_vma memaddr;
c5aa993b 875 disassemble_info *info;
c906108c
SS
876{
877 if (disassembly_flavor == att_flavor)
878 return print_insn_i386_att (memaddr, info);
879 else if (disassembly_flavor == intel_flavor)
880 return print_insn_i386_intel (memaddr, info);
7a292a7a
SS
881 /* Never reached - disassembly_flavour is always either att_flavor
882 or intel_flavor */
883 abort ();
884}
885
886/* If the disassembly mode is intel, we have to also switch the
887 bfd mach_type. This function is run in the set disassembly_flavor
888 command, and does that. */
889
890static void
891set_disassembly_flavor_sfunc (args, from_tty, c)
892 char *args;
893 int from_tty;
894 struct cmd_list_element *c;
895{
896 set_disassembly_flavor ();
7a292a7a
SS
897}
898
899static void
900set_disassembly_flavor ()
901{
902 if (disassembly_flavor == att_flavor)
903 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386);
904 else if (disassembly_flavor == intel_flavor)
905 set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386_intel_syntax);
c906108c
SS
906}
907
2acceee2 908
c906108c
SS
909void
910_initialize_i386_tdep ()
911{
917317f4
JM
912 /* Initialize the table saying where each register starts in the
913 register file. */
914 {
915 int i, offset;
916
917 offset = 0;
918 for (i = 0; i < MAX_NUM_REGS; i++)
919 {
920 i386_register_byte[i] = offset;
921 offset += i386_register_raw_size[i];
922 }
923 }
924
925 /* Initialize the table of virtual register sizes. */
926 {
927 int i;
928
929 for (i = 0; i < MAX_NUM_REGS; i++)
930 i386_register_virtual_size[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
931 }
c5aa993b 932
c906108c
SS
933 tm_print_insn = gdb_print_insn_i386;
934 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
935
936 /* Add the variable that controls the disassembly flavor */
917317f4
JM
937 {
938 struct cmd_list_element *new_cmd;
7a292a7a 939
917317f4
JM
940 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
941 valid_flavors,
1ed2a135 942 &disassembly_flavor,
917317f4 943 "Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
c906108c 944and the default value is \"att\".",
917317f4
JM
945 &setlist);
946 new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
947 add_show_from_set (new_cmd, &showlist);
948 }
c5aa993b 949
7a292a7a
SS
950 /* Finally, initialize the disassembly flavor to the default given
951 in the disassembly_flavor variable */
c906108c 952
7a292a7a 953 set_disassembly_flavor ();
c906108c 954}
This page took 0.115128 seconds and 4 git commands to generate.