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