* bcache.c, bcache.h: New files to implement a byte cache.
[deliverable/binutils-gdb.git] / gdb / i386-tdep.c
CommitLineData
f2ebc25f 1/* Intel 386 target-dependent stuff.
28ee4b42 2 Copyright (C) 1988, 1989, 1991, 1994 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
7d9884b9 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
7d9884b9
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
7d9884b9 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
7d9884b9
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
2b576293 20#include "gdb_string.h"
bd5635a1 21#include "defs.h"
bd5635a1
RP
22#include "frame.h"
23#include "inferior.h"
24#include "gdbcore.h"
51b57ded 25#include "target.h"
eae3f093 26#include "floatformat.h"
28ee4b42 27#include "symtab.h"
bd5635a1 28
1a494973 29static long i386_get_frame_setup PARAMS ((int));
d747e0af 30
1a494973 31static void i386_follow_jump PARAMS ((void));
d747e0af 32
1a494973 33static void codestream_read PARAMS ((unsigned char *, int));
d747e0af 34
1a494973 35static void codestream_seek PARAMS ((int));
d747e0af 36
1a494973 37static unsigned char codestream_fill PARAMS ((int));
bd5635a1 38
d747e0af
MT
39/* Stdio style buffering was used to minimize calls to ptrace, but this
40 buffering did not take into account that the code section being accessed
41 may not be an even number of buffers long (even if the buffer is only
42 sizeof(int) long). In cases where the code section size happened to
43 be a non-integral number of buffers long, attempting to read the last
44 buffer would fail. Simply using target_read_memory and ignoring errors,
45 rather than read_memory, is not the correct solution, since legitimate
46 access errors would then be totally ignored. To properly handle this
47 situation and continue to use buffering would require that this code
48 be able to determine the minimum code section size granularity (not the
49 alignment of the section itself, since the actual failing case that
50 pointed out this problem had a section alignment of 4 but was not a
51 multiple of 4 bytes long), on a target by target basis, and then
52 adjust it's buffer size accordingly. This is messy, but potentially
53 feasible. It probably needs the bfd library's help and support. For
54 now, the buffer size is set to 1. (FIXME -fnf) */
55
56#define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
bd5635a1
RP
57static CORE_ADDR codestream_next_addr;
58static CORE_ADDR codestream_addr;
d747e0af 59static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
bd5635a1
RP
60static int codestream_off;
61static int codestream_cnt;
62
63#define codestream_tell() (codestream_addr + codestream_off)
64#define codestream_peek() (codestream_cnt == 0 ? \
65 codestream_fill(1): codestream_buf[codestream_off])
66#define codestream_get() (codestream_cnt-- == 0 ? \
67 codestream_fill(0) : codestream_buf[codestream_off++])
68
69static unsigned char
70codestream_fill (peek_flag)
d747e0af 71 int peek_flag;
bd5635a1
RP
72{
73 codestream_addr = codestream_next_addr;
d747e0af 74 codestream_next_addr += CODESTREAM_BUFSIZ;
bd5635a1 75 codestream_off = 0;
d747e0af 76 codestream_cnt = CODESTREAM_BUFSIZ;
34df79fc 77 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
bd5635a1
RP
78
79 if (peek_flag)
80 return (codestream_peek());
81 else
82 return (codestream_get());
83}
84
85static void
86codestream_seek (place)
d747e0af 87 int place;
bd5635a1 88{
d747e0af
MT
89 codestream_next_addr = place / CODESTREAM_BUFSIZ;
90 codestream_next_addr *= CODESTREAM_BUFSIZ;
bd5635a1
RP
91 codestream_cnt = 0;
92 codestream_fill (1);
93 while (codestream_tell() != place)
94 codestream_get ();
95}
96
97static void
98codestream_read (buf, count)
99 unsigned char *buf;
d747e0af 100 int count;
bd5635a1
RP
101{
102 unsigned char *p;
103 int i;
104 p = buf;
105 for (i = 0; i < count; i++)
106 *p++ = codestream_get ();
107}
108
109/* next instruction is a jump, move to target */
d747e0af
MT
110
111static void
bd5635a1
RP
112i386_follow_jump ()
113{
28ee4b42
PS
114 unsigned char buf[4];
115 long delta;
116
bd5635a1 117 int data16;
28ee4b42
PS
118 CORE_ADDR pos;
119
bd5635a1 120 pos = codestream_tell ();
28ee4b42 121
bd5635a1
RP
122 data16 = 0;
123 if (codestream_peek () == 0x66)
124 {
125 codestream_get ();
126 data16 = 1;
127 }
28ee4b42 128
bd5635a1
RP
129 switch (codestream_get ())
130 {
131 case 0xe9:
132 /* relative jump: if data16 == 0, disp32, else disp16 */
133 if (data16)
134 {
28ee4b42
PS
135 codestream_read (buf, 2);
136 delta = extract_signed_integer (buf, 2);
f2ebc25f
JK
137
138 /* include size of jmp inst (including the 0x66 prefix). */
28ee4b42 139 pos += delta + 4;
bd5635a1
RP
140 }
141 else
142 {
28ee4b42
PS
143 codestream_read (buf, 4);
144 delta = extract_signed_integer (buf, 4);
145
146 pos += delta + 5;
bd5635a1
RP
147 }
148 break;
149 case 0xeb:
150 /* relative jump, disp8 (ignore data16) */
28ee4b42
PS
151 codestream_read (buf, 1);
152 /* Sign-extend it. */
153 delta = extract_signed_integer (buf, 1);
154
155 pos += delta + 2;
bd5635a1
RP
156 break;
157 }
f2ebc25f 158 codestream_seek (pos);
bd5635a1
RP
159}
160
161/*
162 * find & return amound a local space allocated, and advance codestream to
163 * first register push (if any)
164 *
165 * if entry sequence doesn't make sense, return -1, and leave
166 * codestream pointer random
167 */
d747e0af 168
bd5635a1
RP
169static long
170i386_get_frame_setup (pc)
d747e0af 171 int pc;
bd5635a1
RP
172{
173 unsigned char op;
28ee4b42 174
bd5635a1 175 codestream_seek (pc);
28ee4b42 176
bd5635a1 177 i386_follow_jump ();
28ee4b42 178
bd5635a1 179 op = codestream_get ();
28ee4b42 180
bd5635a1
RP
181 if (op == 0x58) /* popl %eax */
182 {
183 /*
184 * this function must start with
185 *
186 * popl %eax 0x58
187 * xchgl %eax, (%esp) 0x87 0x04 0x24
188 * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
189 *
190 * (the system 5 compiler puts out the second xchg
191 * inst, and the assembler doesn't try to optimize it,
192 * so the 'sib' form gets generated)
193 *
194 * this sequence is used to get the address of the return
195 * buffer for a function that returns a structure
196 */
197 int pos;
198 unsigned char buf[4];
199 static unsigned char proto1[3] = { 0x87,0x04,0x24 };
200 static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
201 pos = codestream_tell ();
202 codestream_read (buf, 4);
51b57ded 203 if (memcmp (buf, proto1, 3) == 0)
bd5635a1 204 pos += 3;
51b57ded 205 else if (memcmp (buf, proto2, 4) == 0)
bd5635a1 206 pos += 4;
28ee4b42 207
bd5635a1
RP
208 codestream_seek (pos);
209 op = codestream_get (); /* update next opcode */
210 }
28ee4b42 211
bd5635a1
RP
212 if (op == 0x55) /* pushl %ebp */
213 {
214 /* check for movl %esp, %ebp - can be written two ways */
215 switch (codestream_get ())
216 {
217 case 0x8b:
218 if (codestream_get () != 0xec)
219 return (-1);
220 break;
221 case 0x89:
222 if (codestream_get () != 0xe5)
223 return (-1);
224 break;
225 default:
226 return (-1);
227 }
228 /* check for stack adjustment
229 *
230 * subl $XXX, %esp
231 *
232 * note: you can't subtract a 16 bit immediate
233 * from a 32 bit reg, so we don't have to worry
234 * about a data16 prefix
235 */
236 op = codestream_peek ();
237 if (op == 0x83)
238 {
239 /* subl with 8 bit immed */
240 codestream_get ();
241 if (codestream_get () != 0xec)
242 /* Some instruction starting with 0x83 other than subl. */
243 {
244 codestream_seek (codestream_tell () - 2);
245 return 0;
246 }
247 /* subl with signed byte immediate
248 * (though it wouldn't make sense to be negative)
249 */
250 return (codestream_get());
251 }
252 else if (op == 0x81)
253 {
34df79fc
JK
254 char buf[4];
255 /* Maybe it is subl with 32 bit immedediate. */
bd5635a1
RP
256 codestream_get();
257 if (codestream_get () != 0xec)
258 /* Some instruction starting with 0x81 other than subl. */
259 {
260 codestream_seek (codestream_tell () - 2);
261 return 0;
262 }
34df79fc
JK
263 /* It is subl with 32 bit immediate. */
264 codestream_read ((unsigned char *)buf, 4);
265 return extract_signed_integer (buf, 4);
bd5635a1
RP
266 }
267 else
268 {
269 return (0);
270 }
271 }
272 else if (op == 0xc8)
273 {
34df79fc 274 char buf[2];
bd5635a1 275 /* enter instruction: arg is 16 bit unsigned immed */
34df79fc 276 codestream_read ((unsigned char *)buf, 2);
bd5635a1 277 codestream_get (); /* flush final byte of enter instruction */
34df79fc 278 return extract_unsigned_integer (buf, 2);
bd5635a1
RP
279 }
280 return (-1);
281}
282
283/* Return number of args passed to a frame.
284 Can return -1, meaning no way to tell. */
285
bd5635a1
RP
286int
287i386_frame_num_args (fi)
d747e0af 288 struct frame_info *fi;
bd5635a1 289{
34df79fc
JK
290#if 1
291 return -1;
292#else
293 /* This loses because not only might the compiler not be popping the
294 args right after the function call, it might be popping args from both
295 this call and a previous one, and we would say there are more args
296 than there really are. */
297
bd5635a1
RP
298 int retpc;
299 unsigned char op;
300 struct frame_info *pfi;
301
34df79fc
JK
302 /* on the 386, the instruction following the call could be:
303 popl %ecx - one arg
304 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
305 anything else - zero args */
306
bd5635a1
RP
307 int frameless;
308
309 FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
310 if (frameless)
311 /* In the absence of a frame pointer, GDB doesn't get correct values
312 for nameless arguments. Return -1, so it doesn't print any
313 nameless arguments. */
314 return -1;
315
d747e0af 316 pfi = get_prev_frame_info (fi);
bd5635a1
RP
317 if (pfi == 0)
318 {
319 /* Note: this can happen if we are looking at the frame for
320 main, because FRAME_CHAIN_VALID won't let us go into
321 start. If we have debugging symbols, that's not really
322 a big deal; it just means it will only show as many arguments
323 to main as are declared. */
324 return -1;
325 }
326 else
327 {
328 retpc = pfi->pc;
329 op = read_memory_integer (retpc, 1);
330 if (op == 0x59)
331 /* pop %ecx */
332 return 1;
333 else if (op == 0x83)
334 {
335 op = read_memory_integer (retpc+1, 1);
336 if (op == 0xc4)
337 /* addl $<signed imm 8 bits>, %esp */
338 return (read_memory_integer (retpc+2,1)&0xff)/4;
339 else
340 return 0;
341 }
342 else if (op == 0x81)
343 { /* add with 32 bit immediate */
344 op = read_memory_integer (retpc+1, 1);
345 if (op == 0xc4)
346 /* addl $<imm 32>, %esp */
347 return read_memory_integer (retpc+2, 4) / 4;
348 else
349 return 0;
350 }
351 else
352 {
353 return 0;
354 }
355 }
34df79fc 356#endif
bd5635a1
RP
357}
358
359/*
360 * parse the first few instructions of the function to see
361 * what registers were stored.
362 *
363 * We handle these cases:
364 *
365 * The startup sequence can be at the start of the function,
366 * or the function can start with a branch to startup code at the end.
367 *
368 * %ebp can be set up with either the 'enter' instruction, or
369 * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
370 * but was once used in the sys5 compiler)
371 *
372 * Local space is allocated just below the saved %ebp by either the
373 * 'enter' instruction, or by 'subl $<size>, %esp'. 'enter' has
374 * a 16 bit unsigned argument for space to allocate, and the
375 * 'addl' instruction could have either a signed byte, or
376 * 32 bit immediate.
377 *
378 * Next, the registers used by this function are pushed. In
379 * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
380 * (and sometimes a harmless bug causes it to also save but not restore %eax);
381 * however, the code below is willing to see the pushes in any order,
382 * and will handle up to 8 of them.
383 *
384 * If the setup sequence is at the end of the function, then the
385 * next instruction will be a branch back to the start.
386 */
387
d747e0af 388void
bd5635a1
RP
389i386_frame_find_saved_regs (fip, fsrp)
390 struct frame_info *fip;
391 struct frame_saved_regs *fsrp;
392{
393 long locals;
bd5635a1
RP
394 unsigned char op;
395 CORE_ADDR dummy_bottom;
396 CORE_ADDR adr;
397 int i;
398
34df79fc 399 memset (fsrp, 0, sizeof *fsrp);
bd5635a1
RP
400
401 /* if frame is the end of a dummy, compute where the
402 * beginning would be
403 */
404 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
405
406 /* check if the PC is in the stack, in a dummy frame */
407 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
408 {
409 /* all regs were saved by push_call_dummy () */
410 adr = fip->frame;
411 for (i = 0; i < NUM_REGS; i++)
412 {
413 adr -= REGISTER_RAW_SIZE (i);
414 fsrp->regs[i] = adr;
415 }
416 return;
417 }
418
419 locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
420
421 if (locals >= 0)
422 {
423 adr = fip->frame - 4 - locals;
424 for (i = 0; i < 8; i++)
425 {
426 op = codestream_get ();
427 if (op < 0x50 || op > 0x57)
428 break;
1a494973
C
429#ifdef I386_REGNO_TO_SYMMETRY
430 /* Dynix uses different internal numbering. Ick. */
431 fsrp->regs[I386_REGNO_TO_SYMMETRY(op - 0x50)] = adr;
432#else
bd5635a1 433 fsrp->regs[op - 0x50] = adr;
1a494973 434#endif
bd5635a1
RP
435 adr -= 4;
436 }
437 }
438
439 fsrp->regs[PC_REGNUM] = fip->frame + 4;
440 fsrp->regs[FP_REGNUM] = fip->frame;
441}
442
443/* return pc of first real instruction */
d747e0af
MT
444
445int
bd5635a1 446i386_skip_prologue (pc)
d747e0af 447 int pc;
bd5635a1
RP
448{
449 unsigned char op;
450 int i;
28ee4b42
PS
451 static unsigned char pic_pat[6] = { 0xe8, 0, 0, 0, 0, /* call 0x0 */
452 0x5b, /* popl %ebx */
453 };
454 CORE_ADDR pos;
bd5635a1
RP
455
456 if (i386_get_frame_setup (pc) < 0)
457 return (pc);
458
459 /* found valid frame setup - codestream now points to
460 * start of push instructions for saving registers
461 */
462
463 /* skip over register saves */
464 for (i = 0; i < 8; i++)
465 {
466 op = codestream_peek ();
467 /* break if not pushl inst */
468 if (op < 0x50 || op > 0x57)
469 break;
470 codestream_get ();
471 }
28ee4b42
PS
472
473 /* The native cc on SVR4 in -K PIC mode inserts the following code to get
474 the address of the global offset table (GOT) into register %ebx.
475 call 0x0
476 popl %ebx
477 movl %ebx,x(%ebp) (optional)
478 addl y,%ebx
479 This code is with the rest of the prologue (at the end of the
480 function), so we have to skip it to get to the first real
481 instruction at the start of the function. */
482
483 pos = codestream_tell ();
484 for (i = 0; i < 6; i++)
485 {
486 op = codestream_get ();
487 if (pic_pat [i] != op)
488 break;
489 }
490 if (i == 6)
491 {
492 unsigned char buf[4];
493 long delta = 6;
494
495 op = codestream_get ();
496 if (op == 0x89) /* movl %ebx, x(%ebp) */
497 {
498 op = codestream_get ();
499 if (op == 0x5d) /* one byte offset from %ebp */
500 {
501 delta += 3;
502 codestream_read (buf, 1);
503 }
504 else if (op == 0x9d) /* four byte offset from %ebp */
505 {
506 delta += 6;
507 codestream_read (buf, 4);
508 }
509 else /* unexpected instruction */
510 delta = -1;
511 op = codestream_get ();
512 }
513 /* addl y,%ebx */
514 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
515 {
516 pos += delta + 6;
517 }
518 }
519 codestream_seek (pos);
bd5635a1
RP
520
521 i386_follow_jump ();
522
523 return (codestream_tell ());
524}
525
d747e0af 526void
bd5635a1
RP
527i386_push_dummy_frame ()
528{
529 CORE_ADDR sp = read_register (SP_REGNUM);
530 int regnum;
531 char regbuf[MAX_REGISTER_RAW_SIZE];
532
533 sp = push_word (sp, read_register (PC_REGNUM));
534 sp = push_word (sp, read_register (FP_REGNUM));
535 write_register (FP_REGNUM, sp);
536 for (regnum = 0; regnum < NUM_REGS; regnum++)
537 {
538 read_register_gen (regnum, regbuf);
539 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
540 }
541 write_register (SP_REGNUM, sp);
542}
543
d747e0af 544void
bd5635a1
RP
545i386_pop_frame ()
546{
1a494973 547 struct frame_info *frame = get_current_frame ();
bd5635a1
RP
548 CORE_ADDR fp;
549 int regnum;
550 struct frame_saved_regs fsr;
bd5635a1
RP
551 char regbuf[MAX_REGISTER_RAW_SIZE];
552
1a494973
C
553 fp = FRAME_FP (frame);
554 get_frame_saved_regs (frame, &fsr);
bd5635a1
RP
555 for (regnum = 0; regnum < NUM_REGS; regnum++)
556 {
557 CORE_ADDR adr;
558 adr = fsr.regs[regnum];
559 if (adr)
560 {
561 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
562 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
563 REGISTER_RAW_SIZE (regnum));
564 }
565 }
566 write_register (FP_REGNUM, read_memory_integer (fp, 4));
567 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
568 write_register (SP_REGNUM, fp + 8);
569 flush_cached_frames ();
bd5635a1 570}
d747e0af 571
51b57ded
FF
572#ifdef GET_LONGJMP_TARGET
573
574/* Figure out where the longjmp will land. Slurp the args out of the stack.
575 We expect the first arg to be a pointer to the jmp_buf structure from which
576 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
577 This routine returns true on success. */
578
579int
580get_longjmp_target(pc)
581 CORE_ADDR *pc;
582{
34df79fc 583 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
51b57ded
FF
584 CORE_ADDR sp, jb_addr;
585
34df79fc 586 sp = read_register (SP_REGNUM);
51b57ded 587
34df79fc
JK
588 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
589 buf,
590 TARGET_PTR_BIT / TARGET_CHAR_BIT))
51b57ded
FF
591 return 0;
592
34df79fc 593 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
51b57ded 594
34df79fc
JK
595 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
596 TARGET_PTR_BIT / TARGET_CHAR_BIT))
51b57ded
FF
597 return 0;
598
34df79fc 599 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
51b57ded
FF
600
601 return 1;
602}
603
604#endif /* GET_LONGJMP_TARGET */
34df79fc
JK
605
606#ifdef I386_AIX_TARGET
607/* On AIX, floating point values are returned in floating point registers. */
608
609void
610i386_extract_return_value(type, regbuf, valbuf)
611 struct type *type;
612 char regbuf[REGISTER_BYTES];
613 char *valbuf;
614{
615 if (TYPE_CODE_FLT == TYPE_CODE(type))
616 {
34df79fc
JK
617 double d;
618 /* 387 %st(0), gcc uses this */
eae3f093
JK
619 floatformat_to_double (&floatformat_i387_ext,
620 &regbuf[REGISTER_BYTE(FP0_REGNUM)],
621 &d);
28ee4b42 622 store_floating (valbuf, TYPE_LENGTH (type), d);
34df79fc
JK
623 }
624 else
625 {
626 memcpy (valbuf, regbuf, TYPE_LENGTH (type));
627 }
628}
629#endif /* I386_AIX_TARGET */
28ee4b42
PS
630
631#ifdef I386V4_SIGTRAMP_SAVED_PC
632/* Get saved user PC for sigtramp from the pushed ucontext on the stack
633 for all three variants of SVR4 sigtramps. */
634
635CORE_ADDR
636i386v4_sigtramp_saved_pc (frame)
1a494973 637 struct frame_info *frame;
28ee4b42
PS
638{
639 CORE_ADDR saved_pc_offset = 4;
640 char *name = NULL;
641
1a494973 642 find_pc_partial_function (frame->pc, &name, NULL, NULL);
28ee4b42
PS
643 if (name)
644 {
645 if (STREQ (name, "_sigreturn"))
646 saved_pc_offset = 132 + 14 * 4;
137b6849 647 else if (STREQ (name, "_sigacthandler"))
28ee4b42 648 saved_pc_offset = 80 + 14 * 4;
137b6849 649 else if (STREQ (name, "sigvechandler"))
28ee4b42
PS
650 saved_pc_offset = 120 + 14 * 4;
651 }
652
653 if (frame->next)
654 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
655 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
656}
657#endif /* I386V4_SIGTRAMP_SAVED_PC */
1a494973
C
658
659void
660_initialize_i386_tdep ()
661{
662 tm_print_insn = print_insn_i386;
663}
This page took 0.248892 seconds and 4 git commands to generate.