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