Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Intel 386 target-dependent stuff. |
b6ba6518 KB |
2 | Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
3 | 1998, 1999, 2000, 2001 | |
c906108c SS |
4 | Free Software Foundation, Inc. |
5 | ||
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
24 | #include "gdb_string.h" | |
25 | #include "frame.h" | |
26 | #include "inferior.h" | |
27 | #include "gdbcore.h" | |
28 | #include "target.h" | |
29 | #include "floatformat.h" | |
30 | #include "symtab.h" | |
31 | #include "gdbcmd.h" | |
32 | #include "command.h" | |
b4a20239 | 33 | #include "arch-utils.h" |
4e052eda | 34 | #include "regcache.h" |
d16aafd8 | 35 | #include "doublest.h" |
fd0407d6 | 36 | #include "value.h" |
a62cc96e | 37 | #include "i386-tdep.h" |
3d261580 MK |
38 | #include "gdb_assert.h" |
39 | ||
a62cc96e AC |
40 | #undef XMALLOC |
41 | #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE))) | |
42 | ||
fc633446 MK |
43 | /* Names of the registers. The first 10 registers match the register |
44 | numbering scheme used by GCC for stabs and DWARF. */ | |
45 | static char *i386_register_names[] = | |
46 | { | |
47 | "eax", "ecx", "edx", "ebx", | |
48 | "esp", "ebp", "esi", "edi", | |
49 | "eip", "eflags", "cs", "ss", | |
50 | "ds", "es", "fs", "gs", | |
51 | "st0", "st1", "st2", "st3", | |
52 | "st4", "st5", "st6", "st7", | |
53 | "fctrl", "fstat", "ftag", "fiseg", | |
54 | "fioff", "foseg", "fooff", "fop", | |
55 | "xmm0", "xmm1", "xmm2", "xmm3", | |
56 | "xmm4", "xmm5", "xmm6", "xmm7", | |
57 | "mxcsr" | |
58 | }; | |
59 | ||
1a11ba71 | 60 | /* i386_register_offset[i] is the offset into the register file of the |
917317f4 | 61 | start of register number i. We initialize this from |
1a11ba71 MK |
62 | i386_register_size. */ |
63 | static int i386_register_offset[MAX_NUM_REGS]; | |
917317f4 | 64 | |
1a11ba71 MK |
65 | /* i386_register_size[i] is the number of bytes of storage in GDB's |
66 | register array occupied by register i. */ | |
67 | static int i386_register_size[MAX_NUM_REGS] = { | |
917317f4 JM |
68 | 4, 4, 4, 4, |
69 | 4, 4, 4, 4, | |
70 | 4, 4, 4, 4, | |
71 | 4, 4, 4, 4, | |
72 | 10, 10, 10, 10, | |
73 | 10, 10, 10, 10, | |
74 | 4, 4, 4, 4, | |
75 | 4, 4, 4, 4, | |
76 | 16, 16, 16, 16, | |
77 | 16, 16, 16, 16, | |
78 | 4 | |
79 | }; | |
80 | ||
fc633446 MK |
81 | /* Return the name of register REG. */ |
82 | ||
83 | char * | |
84 | i386_register_name (int reg) | |
85 | { | |
86 | if (reg < 0) | |
87 | return NULL; | |
88 | if (reg >= sizeof (i386_register_names) / sizeof (*i386_register_names)) | |
89 | return NULL; | |
90 | ||
91 | return i386_register_names[reg]; | |
92 | } | |
93 | ||
1a11ba71 MK |
94 | /* Return the offset into the register array of the start of register |
95 | number REG. */ | |
96 | int | |
97 | i386_register_byte (int reg) | |
98 | { | |
99 | return i386_register_offset[reg]; | |
100 | } | |
101 | ||
102 | /* Return the number of bytes of storage in GDB's register array | |
103 | occupied by register REG. */ | |
104 | ||
105 | int | |
106 | i386_register_raw_size (int reg) | |
107 | { | |
108 | return i386_register_size[reg]; | |
109 | } | |
110 | ||
111 | /* Return the size in bytes of the virtual type of register REG. */ | |
112 | ||
113 | int | |
114 | i386_register_virtual_size (int reg) | |
115 | { | |
116 | return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (reg)); | |
117 | } | |
118 | ||
85540d8c MK |
119 | /* Convert stabs register number REG to the appropriate register |
120 | number used by GDB. */ | |
121 | ||
122 | int | |
123 | i386_stab_reg_to_regnum (int reg) | |
124 | { | |
125 | /* This implements what GCC calls the "default" register map. */ | |
126 | if (reg >= 0 && reg <= 7) | |
127 | { | |
128 | /* General registers. */ | |
129 | return reg; | |
130 | } | |
131 | else if (reg >= 12 && reg <= 19) | |
132 | { | |
133 | /* Floating-point registers. */ | |
134 | return reg - 12 + FP0_REGNUM; | |
135 | } | |
136 | else if (reg >= 21 && reg <= 28) | |
137 | { | |
138 | /* SSE registers. */ | |
139 | return reg - 21 + XMM0_REGNUM; | |
140 | } | |
141 | else if (reg >= 29 && reg <= 36) | |
142 | { | |
143 | /* MMX registers. */ | |
144 | /* FIXME: kettenis/2001-07-28: Should we have the MMX registers | |
145 | as pseudo-registers? */ | |
146 | return reg - 29 + FP0_REGNUM; | |
147 | } | |
148 | ||
149 | /* This will hopefully provoke a warning. */ | |
150 | return NUM_REGS + NUM_PSEUDO_REGS; | |
151 | } | |
152 | ||
153 | /* Convert Dwarf register number REG to the appropriate register | |
154 | number used by GDB. */ | |
155 | ||
156 | int | |
157 | i386_dwarf_reg_to_regnum (int reg) | |
158 | { | |
159 | /* The DWARF register numbering includes %eip and %eflags, and | |
160 | numbers the floating point registers differently. */ | |
161 | if (reg >= 0 && reg <= 9) | |
162 | { | |
163 | /* General registers. */ | |
164 | return reg; | |
165 | } | |
166 | else if (reg >= 11 && reg <= 18) | |
167 | { | |
168 | /* Floating-point registers. */ | |
169 | return reg - 11 + FP0_REGNUM; | |
170 | } | |
171 | else if (reg >= 21) | |
172 | { | |
173 | /* The SSE and MMX registers have identical numbers as in stabs. */ | |
174 | return i386_stab_reg_to_regnum (reg); | |
175 | } | |
176 | ||
177 | /* This will hopefully provoke a warning. */ | |
178 | return NUM_REGS + NUM_PSEUDO_REGS; | |
179 | } | |
fc338970 | 180 | \f |
917317f4 | 181 | |
fc338970 MK |
182 | /* This is the variable that is set with "set disassembly-flavor", and |
183 | its legitimate values. */ | |
53904c9e AC |
184 | static const char att_flavor[] = "att"; |
185 | static const char intel_flavor[] = "intel"; | |
186 | static const char *valid_flavors[] = | |
c5aa993b | 187 | { |
c906108c SS |
188 | att_flavor, |
189 | intel_flavor, | |
190 | NULL | |
191 | }; | |
53904c9e | 192 | static const char *disassembly_flavor = att_flavor; |
c906108c | 193 | |
fc338970 MK |
194 | /* Stdio style buffering was used to minimize calls to ptrace, but |
195 | this buffering did not take into account that the code section | |
196 | being accessed may not be an even number of buffers long (even if | |
197 | the buffer is only sizeof(int) long). In cases where the code | |
198 | section size happened to be a non-integral number of buffers long, | |
199 | attempting to read the last buffer would fail. Simply using | |
200 | target_read_memory and ignoring errors, rather than read_memory, is | |
201 | not the correct solution, since legitimate access errors would then | |
202 | be totally ignored. To properly handle this situation and continue | |
203 | to use buffering would require that this code be able to determine | |
204 | the minimum code section size granularity (not the alignment of the | |
205 | section itself, since the actual failing case that pointed out this | |
206 | problem had a section alignment of 4 but was not a multiple of 4 | |
207 | bytes long), on a target by target basis, and then adjust it's | |
208 | buffer size accordingly. This is messy, but potentially feasible. | |
209 | It probably needs the bfd library's help and support. For now, the | |
210 | buffer size is set to 1. (FIXME -fnf) */ | |
211 | ||
212 | #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */ | |
c906108c SS |
213 | static CORE_ADDR codestream_next_addr; |
214 | static CORE_ADDR codestream_addr; | |
215 | static unsigned char codestream_buf[CODESTREAM_BUFSIZ]; | |
216 | static int codestream_off; | |
217 | static int codestream_cnt; | |
218 | ||
219 | #define codestream_tell() (codestream_addr + codestream_off) | |
fc338970 MK |
220 | #define codestream_peek() \ |
221 | (codestream_cnt == 0 ? \ | |
222 | codestream_fill(1) : codestream_buf[codestream_off]) | |
223 | #define codestream_get() \ | |
224 | (codestream_cnt-- == 0 ? \ | |
225 | codestream_fill(0) : codestream_buf[codestream_off++]) | |
c906108c | 226 | |
c5aa993b | 227 | static unsigned char |
fba45db2 | 228 | codestream_fill (int peek_flag) |
c906108c SS |
229 | { |
230 | codestream_addr = codestream_next_addr; | |
231 | codestream_next_addr += CODESTREAM_BUFSIZ; | |
232 | codestream_off = 0; | |
233 | codestream_cnt = CODESTREAM_BUFSIZ; | |
234 | read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ); | |
c5aa993b | 235 | |
c906108c | 236 | if (peek_flag) |
c5aa993b | 237 | return (codestream_peek ()); |
c906108c | 238 | else |
c5aa993b | 239 | return (codestream_get ()); |
c906108c SS |
240 | } |
241 | ||
242 | static void | |
fba45db2 | 243 | codestream_seek (CORE_ADDR place) |
c906108c SS |
244 | { |
245 | codestream_next_addr = place / CODESTREAM_BUFSIZ; | |
246 | codestream_next_addr *= CODESTREAM_BUFSIZ; | |
247 | codestream_cnt = 0; | |
248 | codestream_fill (1); | |
c5aa993b | 249 | while (codestream_tell () != place) |
c906108c SS |
250 | codestream_get (); |
251 | } | |
252 | ||
253 | static void | |
fba45db2 | 254 | codestream_read (unsigned char *buf, int count) |
c906108c SS |
255 | { |
256 | unsigned char *p; | |
257 | int i; | |
258 | p = buf; | |
259 | for (i = 0; i < count; i++) | |
260 | *p++ = codestream_get (); | |
261 | } | |
fc338970 | 262 | \f |
c906108c | 263 | |
fc338970 | 264 | /* If the next instruction is a jump, move to its target. */ |
c906108c SS |
265 | |
266 | static void | |
fba45db2 | 267 | i386_follow_jump (void) |
c906108c SS |
268 | { |
269 | unsigned char buf[4]; | |
270 | long delta; | |
271 | ||
272 | int data16; | |
273 | CORE_ADDR pos; | |
274 | ||
275 | pos = codestream_tell (); | |
276 | ||
277 | data16 = 0; | |
278 | if (codestream_peek () == 0x66) | |
279 | { | |
280 | codestream_get (); | |
281 | data16 = 1; | |
282 | } | |
283 | ||
284 | switch (codestream_get ()) | |
285 | { | |
286 | case 0xe9: | |
fc338970 | 287 | /* Relative jump: if data16 == 0, disp32, else disp16. */ |
c906108c SS |
288 | if (data16) |
289 | { | |
290 | codestream_read (buf, 2); | |
291 | delta = extract_signed_integer (buf, 2); | |
292 | ||
fc338970 MK |
293 | /* Include the size of the jmp instruction (including the |
294 | 0x66 prefix). */ | |
c5aa993b | 295 | pos += delta + 4; |
c906108c SS |
296 | } |
297 | else | |
298 | { | |
299 | codestream_read (buf, 4); | |
300 | delta = extract_signed_integer (buf, 4); | |
301 | ||
302 | pos += delta + 5; | |
303 | } | |
304 | break; | |
305 | case 0xeb: | |
fc338970 | 306 | /* Relative jump, disp8 (ignore data16). */ |
c906108c SS |
307 | codestream_read (buf, 1); |
308 | /* Sign-extend it. */ | |
309 | delta = extract_signed_integer (buf, 1); | |
310 | ||
311 | pos += delta + 2; | |
312 | break; | |
313 | } | |
314 | codestream_seek (pos); | |
315 | } | |
316 | ||
fc338970 MK |
317 | /* Find & return the amount a local space allocated, and advance the |
318 | codestream to the first register push (if any). | |
319 | ||
320 | If the entry sequence doesn't make sense, return -1, and leave | |
321 | codestream pointer at a random spot. */ | |
c906108c SS |
322 | |
323 | static long | |
fba45db2 | 324 | i386_get_frame_setup (CORE_ADDR pc) |
c906108c SS |
325 | { |
326 | unsigned char op; | |
327 | ||
328 | codestream_seek (pc); | |
329 | ||
330 | i386_follow_jump (); | |
331 | ||
332 | op = codestream_get (); | |
333 | ||
334 | if (op == 0x58) /* popl %eax */ | |
335 | { | |
fc338970 MK |
336 | /* This function must start with |
337 | ||
338 | popl %eax 0x58 | |
339 | xchgl %eax, (%esp) 0x87 0x04 0x24 | |
340 | or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00 | |
341 | ||
342 | (the System V compiler puts out the second `xchg' | |
343 | instruction, and the assembler doesn't try to optimize it, so | |
344 | the 'sib' form gets generated). This sequence is used to get | |
345 | the address of the return buffer for a function that returns | |
346 | a structure. */ | |
c906108c SS |
347 | int pos; |
348 | unsigned char buf[4]; | |
fc338970 MK |
349 | static unsigned char proto1[3] = { 0x87, 0x04, 0x24 }; |
350 | static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 }; | |
351 | ||
c906108c SS |
352 | pos = codestream_tell (); |
353 | codestream_read (buf, 4); | |
354 | if (memcmp (buf, proto1, 3) == 0) | |
355 | pos += 3; | |
356 | else if (memcmp (buf, proto2, 4) == 0) | |
357 | pos += 4; | |
358 | ||
359 | codestream_seek (pos); | |
fc338970 | 360 | op = codestream_get (); /* Update next opcode. */ |
c906108c SS |
361 | } |
362 | ||
363 | if (op == 0x68 || op == 0x6a) | |
364 | { | |
fc338970 MK |
365 | /* This function may start with |
366 | ||
367 | pushl constant | |
368 | call _probe | |
369 | addl $4, %esp | |
370 | ||
371 | followed by | |
372 | ||
373 | pushl %ebp | |
374 | ||
375 | etc. */ | |
c906108c SS |
376 | int pos; |
377 | unsigned char buf[8]; | |
378 | ||
fc338970 | 379 | /* Skip past the `pushl' instruction; it has either a one-byte |
c906108c SS |
380 | or a four-byte operand, depending on the opcode. */ |
381 | pos = codestream_tell (); | |
382 | if (op == 0x68) | |
383 | pos += 4; | |
384 | else | |
385 | pos += 1; | |
386 | codestream_seek (pos); | |
387 | ||
fc338970 MK |
388 | /* Read the following 8 bytes, which should be "call _probe" (6 |
389 | bytes) followed by "addl $4,%esp" (2 bytes). */ | |
c906108c SS |
390 | codestream_read (buf, sizeof (buf)); |
391 | if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4) | |
392 | pos += sizeof (buf); | |
393 | codestream_seek (pos); | |
fc338970 | 394 | op = codestream_get (); /* Update next opcode. */ |
c906108c SS |
395 | } |
396 | ||
397 | if (op == 0x55) /* pushl %ebp */ | |
c5aa993b | 398 | { |
fc338970 | 399 | /* Check for "movl %esp, %ebp" -- can be written in two ways. */ |
c906108c SS |
400 | switch (codestream_get ()) |
401 | { | |
402 | case 0x8b: | |
403 | if (codestream_get () != 0xec) | |
fc338970 | 404 | return -1; |
c906108c SS |
405 | break; |
406 | case 0x89: | |
407 | if (codestream_get () != 0xe5) | |
fc338970 | 408 | return -1; |
c906108c SS |
409 | break; |
410 | default: | |
fc338970 | 411 | return -1; |
c906108c | 412 | } |
fc338970 MK |
413 | /* Check for stack adjustment |
414 | ||
415 | subl $XXX, %esp | |
416 | ||
417 | NOTE: You can't subtract a 16 bit immediate from a 32 bit | |
418 | reg, so we don't have to worry about a data16 prefix. */ | |
c906108c SS |
419 | op = codestream_peek (); |
420 | if (op == 0x83) | |
421 | { | |
fc338970 | 422 | /* `subl' with 8 bit immediate. */ |
c906108c SS |
423 | codestream_get (); |
424 | if (codestream_get () != 0xec) | |
fc338970 | 425 | /* Some instruction starting with 0x83 other than `subl'. */ |
c906108c SS |
426 | { |
427 | codestream_seek (codestream_tell () - 2); | |
428 | return 0; | |
429 | } | |
fc338970 MK |
430 | /* `subl' with signed byte immediate (though it wouldn't |
431 | make sense to be negative). */ | |
c5aa993b | 432 | return (codestream_get ()); |
c906108c SS |
433 | } |
434 | else if (op == 0x81) | |
435 | { | |
436 | char buf[4]; | |
fc338970 | 437 | /* Maybe it is `subl' with a 32 bit immedediate. */ |
c5aa993b | 438 | codestream_get (); |
c906108c | 439 | if (codestream_get () != 0xec) |
fc338970 | 440 | /* Some instruction starting with 0x81 other than `subl'. */ |
c906108c SS |
441 | { |
442 | codestream_seek (codestream_tell () - 2); | |
443 | return 0; | |
444 | } | |
fc338970 | 445 | /* It is `subl' with a 32 bit immediate. */ |
c5aa993b | 446 | codestream_read ((unsigned char *) buf, 4); |
c906108c SS |
447 | return extract_signed_integer (buf, 4); |
448 | } | |
449 | else | |
450 | { | |
fc338970 | 451 | return 0; |
c906108c SS |
452 | } |
453 | } | |
454 | else if (op == 0xc8) | |
455 | { | |
456 | char buf[2]; | |
fc338970 | 457 | /* `enter' with 16 bit unsigned immediate. */ |
c5aa993b | 458 | codestream_read ((unsigned char *) buf, 2); |
fc338970 | 459 | codestream_get (); /* Flush final byte of enter instruction. */ |
c906108c SS |
460 | return extract_unsigned_integer (buf, 2); |
461 | } | |
462 | return (-1); | |
463 | } | |
464 | ||
c833a37e MK |
465 | /* Return the chain-pointer for FRAME. In the case of the i386, the |
466 | frame's nominal address is the address of a 4-byte word containing | |
467 | the calling frame's address. */ | |
468 | ||
469 | CORE_ADDR | |
470 | i386_frame_chain (struct frame_info *frame) | |
471 | { | |
472 | if (frame->signal_handler_caller) | |
473 | return frame->frame; | |
474 | ||
475 | if (! inside_entry_file (frame->pc)) | |
476 | return read_memory_unsigned_integer (frame->frame, 4); | |
477 | ||
478 | return 0; | |
479 | } | |
480 | ||
539ffe0b MK |
481 | /* Determine whether the function invocation represented by FRAME does |
482 | not have a from on the stack associated with it. If it does not, | |
483 | return non-zero, otherwise return zero. */ | |
484 | ||
485 | int | |
486 | i386_frameless_function_invocation (struct frame_info *frame) | |
487 | { | |
488 | if (frame->signal_handler_caller) | |
489 | return 0; | |
490 | ||
491 | return frameless_look_for_prologue (frame); | |
492 | } | |
493 | ||
0d17c81d MK |
494 | /* Return the saved program counter for FRAME. */ |
495 | ||
496 | CORE_ADDR | |
497 | i386_frame_saved_pc (struct frame_info *frame) | |
498 | { | |
499 | /* FIXME: kettenis/2001-05-09: Conditionalizing the next bit of code | |
500 | on SIGCONTEXT_PC_OFFSET and I386V4_SIGTRAMP_SAVED_PC should be | |
501 | considered a temporary hack. I plan to come up with something | |
502 | better when we go multi-arch. */ | |
503 | #if defined (SIGCONTEXT_PC_OFFSET) || defined (I386V4_SIGTRAMP_SAVED_PC) | |
504 | if (frame->signal_handler_caller) | |
505 | return sigtramp_saved_pc (frame); | |
506 | #endif | |
507 | ||
508 | return read_memory_unsigned_integer (frame->frame + 4, 4); | |
509 | } | |
510 | ||
ed84f6c1 MK |
511 | /* Immediately after a function call, return the saved pc. */ |
512 | ||
513 | CORE_ADDR | |
514 | i386_saved_pc_after_call (struct frame_info *frame) | |
515 | { | |
516 | return read_memory_unsigned_integer (read_register (SP_REGNUM), 4); | |
517 | } | |
518 | ||
c906108c SS |
519 | /* Return number of args passed to a frame. |
520 | Can return -1, meaning no way to tell. */ | |
521 | ||
522 | int | |
fba45db2 | 523 | i386_frame_num_args (struct frame_info *fi) |
c906108c SS |
524 | { |
525 | #if 1 | |
526 | return -1; | |
527 | #else | |
528 | /* This loses because not only might the compiler not be popping the | |
fc338970 MK |
529 | args right after the function call, it might be popping args from |
530 | both this call and a previous one, and we would say there are | |
531 | more args than there really are. */ | |
c906108c | 532 | |
c5aa993b JM |
533 | int retpc; |
534 | unsigned char op; | |
c906108c SS |
535 | struct frame_info *pfi; |
536 | ||
fc338970 | 537 | /* On the i386, the instruction following the call could be: |
c906108c SS |
538 | popl %ecx - one arg |
539 | addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits | |
fc338970 | 540 | anything else - zero args. */ |
c906108c SS |
541 | |
542 | int frameless; | |
543 | ||
392a587b | 544 | frameless = FRAMELESS_FUNCTION_INVOCATION (fi); |
c906108c | 545 | if (frameless) |
fc338970 MK |
546 | /* In the absence of a frame pointer, GDB doesn't get correct |
547 | values for nameless arguments. Return -1, so it doesn't print | |
548 | any nameless arguments. */ | |
c906108c SS |
549 | return -1; |
550 | ||
c5aa993b | 551 | pfi = get_prev_frame (fi); |
c906108c SS |
552 | if (pfi == 0) |
553 | { | |
fc338970 MK |
554 | /* NOTE: This can happen if we are looking at the frame for |
555 | main, because FRAME_CHAIN_VALID won't let us go into start. | |
556 | If we have debugging symbols, that's not really a big deal; | |
557 | it just means it will only show as many arguments to main as | |
558 | are declared. */ | |
c906108c SS |
559 | return -1; |
560 | } | |
561 | else | |
562 | { | |
c5aa993b JM |
563 | retpc = pfi->pc; |
564 | op = read_memory_integer (retpc, 1); | |
fc338970 | 565 | if (op == 0x59) /* pop %ecx */ |
c5aa993b | 566 | return 1; |
c906108c SS |
567 | else if (op == 0x83) |
568 | { | |
c5aa993b JM |
569 | op = read_memory_integer (retpc + 1, 1); |
570 | if (op == 0xc4) | |
571 | /* addl $<signed imm 8 bits>, %esp */ | |
572 | return (read_memory_integer (retpc + 2, 1) & 0xff) / 4; | |
c906108c SS |
573 | else |
574 | return 0; | |
575 | } | |
fc338970 MK |
576 | else if (op == 0x81) /* `add' with 32 bit immediate. */ |
577 | { | |
c5aa993b JM |
578 | op = read_memory_integer (retpc + 1, 1); |
579 | if (op == 0xc4) | |
580 | /* addl $<imm 32>, %esp */ | |
581 | return read_memory_integer (retpc + 2, 4) / 4; | |
c906108c SS |
582 | else |
583 | return 0; | |
584 | } | |
585 | else | |
586 | { | |
587 | return 0; | |
588 | } | |
589 | } | |
590 | #endif | |
591 | } | |
592 | ||
fc338970 MK |
593 | /* Parse the first few instructions the function to see what registers |
594 | were stored. | |
595 | ||
596 | We handle these cases: | |
597 | ||
598 | The startup sequence can be at the start of the function, or the | |
599 | function can start with a branch to startup code at the end. | |
600 | ||
601 | %ebp can be set up with either the 'enter' instruction, or "pushl | |
602 | %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was | |
603 | once used in the System V compiler). | |
604 | ||
605 | Local space is allocated just below the saved %ebp by either the | |
606 | 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16 | |
607 | bit unsigned argument for space to allocate, and the 'addl' | |
608 | instruction could have either a signed byte, or 32 bit immediate. | |
609 | ||
610 | Next, the registers used by this function are pushed. With the | |
611 | System V compiler they will always be in the order: %edi, %esi, | |
612 | %ebx (and sometimes a harmless bug causes it to also save but not | |
613 | restore %eax); however, the code below is willing to see the pushes | |
614 | in any order, and will handle up to 8 of them. | |
615 | ||
616 | If the setup sequence is at the end of the function, then the next | |
617 | instruction will be a branch back to the start. */ | |
c906108c SS |
618 | |
619 | void | |
fba45db2 | 620 | i386_frame_init_saved_regs (struct frame_info *fip) |
c906108c SS |
621 | { |
622 | long locals = -1; | |
623 | unsigned char op; | |
624 | CORE_ADDR dummy_bottom; | |
fc338970 | 625 | CORE_ADDR addr; |
c906108c SS |
626 | CORE_ADDR pc; |
627 | int i; | |
c5aa993b | 628 | |
1211c4e4 AC |
629 | if (fip->saved_regs) |
630 | return; | |
631 | ||
632 | frame_saved_regs_zalloc (fip); | |
c5aa993b | 633 | |
fc338970 MK |
634 | /* If the frame is the end of a dummy, compute where the beginning |
635 | would be. */ | |
c906108c | 636 | dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH; |
c5aa993b | 637 | |
fc338970 | 638 | /* Check if the PC points in the stack, in a dummy frame. */ |
c5aa993b | 639 | if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) |
c906108c | 640 | { |
fc338970 MK |
641 | /* All registers were saved by push_call_dummy. */ |
642 | addr = fip->frame; | |
c5aa993b | 643 | for (i = 0; i < NUM_REGS; i++) |
c906108c | 644 | { |
fc338970 MK |
645 | addr -= REGISTER_RAW_SIZE (i); |
646 | fip->saved_regs[i] = addr; | |
c906108c SS |
647 | } |
648 | return; | |
649 | } | |
c5aa993b | 650 | |
c906108c SS |
651 | pc = get_pc_function_start (fip->pc); |
652 | if (pc != 0) | |
653 | locals = i386_get_frame_setup (pc); | |
c5aa993b JM |
654 | |
655 | if (locals >= 0) | |
c906108c | 656 | { |
fc338970 | 657 | addr = fip->frame - 4 - locals; |
c5aa993b | 658 | for (i = 0; i < 8; i++) |
c906108c SS |
659 | { |
660 | op = codestream_get (); | |
661 | if (op < 0x50 || op > 0x57) | |
662 | break; | |
663 | #ifdef I386_REGNO_TO_SYMMETRY | |
664 | /* Dynix uses different internal numbering. Ick. */ | |
fc338970 | 665 | fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr; |
c906108c | 666 | #else |
fc338970 | 667 | fip->saved_regs[op - 0x50] = addr; |
c906108c | 668 | #endif |
fc338970 | 669 | addr -= 4; |
c906108c SS |
670 | } |
671 | } | |
c5aa993b | 672 | |
1211c4e4 AC |
673 | fip->saved_regs[PC_REGNUM] = fip->frame + 4; |
674 | fip->saved_regs[FP_REGNUM] = fip->frame; | |
c906108c SS |
675 | } |
676 | ||
fc338970 | 677 | /* Return PC of first real instruction. */ |
c906108c SS |
678 | |
679 | int | |
fba45db2 | 680 | i386_skip_prologue (int pc) |
c906108c SS |
681 | { |
682 | unsigned char op; | |
683 | int i; | |
c5aa993b | 684 | static unsigned char pic_pat[6] = |
fc338970 MK |
685 | { 0xe8, 0, 0, 0, 0, /* call 0x0 */ |
686 | 0x5b, /* popl %ebx */ | |
c5aa993b | 687 | }; |
c906108c | 688 | CORE_ADDR pos; |
c5aa993b | 689 | |
c906108c SS |
690 | if (i386_get_frame_setup (pc) < 0) |
691 | return (pc); | |
c5aa993b | 692 | |
fc338970 MK |
693 | /* Found valid frame setup -- codestream now points to start of push |
694 | instructions for saving registers. */ | |
c5aa993b | 695 | |
fc338970 | 696 | /* Skip over register saves. */ |
c906108c SS |
697 | for (i = 0; i < 8; i++) |
698 | { | |
699 | op = codestream_peek (); | |
fc338970 | 700 | /* Break if not `pushl' instrunction. */ |
c5aa993b | 701 | if (op < 0x50 || op > 0x57) |
c906108c SS |
702 | break; |
703 | codestream_get (); | |
704 | } | |
705 | ||
fc338970 MK |
706 | /* The native cc on SVR4 in -K PIC mode inserts the following code |
707 | to get the address of the global offset table (GOT) into register | |
708 | %ebx | |
709 | ||
710 | call 0x0 | |
711 | popl %ebx | |
712 | movl %ebx,x(%ebp) (optional) | |
713 | addl y,%ebx | |
714 | ||
c906108c SS |
715 | This code is with the rest of the prologue (at the end of the |
716 | function), so we have to skip it to get to the first real | |
717 | instruction at the start of the function. */ | |
c5aa993b | 718 | |
c906108c SS |
719 | pos = codestream_tell (); |
720 | for (i = 0; i < 6; i++) | |
721 | { | |
722 | op = codestream_get (); | |
c5aa993b | 723 | if (pic_pat[i] != op) |
c906108c SS |
724 | break; |
725 | } | |
726 | if (i == 6) | |
727 | { | |
728 | unsigned char buf[4]; | |
729 | long delta = 6; | |
730 | ||
731 | op = codestream_get (); | |
c5aa993b | 732 | if (op == 0x89) /* movl %ebx, x(%ebp) */ |
c906108c SS |
733 | { |
734 | op = codestream_get (); | |
fc338970 | 735 | if (op == 0x5d) /* One byte offset from %ebp. */ |
c906108c SS |
736 | { |
737 | delta += 3; | |
738 | codestream_read (buf, 1); | |
739 | } | |
fc338970 | 740 | else if (op == 0x9d) /* Four byte offset from %ebp. */ |
c906108c SS |
741 | { |
742 | delta += 6; | |
743 | codestream_read (buf, 4); | |
744 | } | |
fc338970 | 745 | else /* Unexpected instruction. */ |
c5aa993b JM |
746 | delta = -1; |
747 | op = codestream_get (); | |
c906108c | 748 | } |
c5aa993b JM |
749 | /* addl y,%ebx */ |
750 | if (delta > 0 && op == 0x81 && codestream_get () == 0xc3) | |
c906108c | 751 | { |
c5aa993b | 752 | pos += delta + 6; |
c906108c SS |
753 | } |
754 | } | |
755 | codestream_seek (pos); | |
c5aa993b | 756 | |
c906108c | 757 | i386_follow_jump (); |
c5aa993b | 758 | |
c906108c SS |
759 | return (codestream_tell ()); |
760 | } | |
761 | ||
762 | void | |
fba45db2 | 763 | i386_push_dummy_frame (void) |
c906108c SS |
764 | { |
765 | CORE_ADDR sp = read_register (SP_REGNUM); | |
766 | int regnum; | |
767 | char regbuf[MAX_REGISTER_RAW_SIZE]; | |
c5aa993b | 768 | |
c906108c SS |
769 | sp = push_word (sp, read_register (PC_REGNUM)); |
770 | sp = push_word (sp, read_register (FP_REGNUM)); | |
771 | write_register (FP_REGNUM, sp); | |
772 | for (regnum = 0; regnum < NUM_REGS; regnum++) | |
773 | { | |
774 | read_register_gen (regnum, regbuf); | |
775 | sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum)); | |
776 | } | |
777 | write_register (SP_REGNUM, sp); | |
778 | } | |
779 | ||
a7769679 MK |
780 | /* Insert the (relative) function address into the call sequence |
781 | stored at DYMMY. */ | |
782 | ||
783 | void | |
784 | i386_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs, | |
ea7c478f | 785 | struct value **args, struct type *type, int gcc_p) |
a7769679 MK |
786 | { |
787 | int from, to, delta, loc; | |
788 | ||
789 | loc = (int)(read_register (SP_REGNUM) - CALL_DUMMY_LENGTH); | |
790 | from = loc + 5; | |
791 | to = (int)(fun); | |
792 | delta = to - from; | |
793 | ||
794 | *((char *)(dummy) + 1) = (delta & 0xff); | |
795 | *((char *)(dummy) + 2) = ((delta >> 8) & 0xff); | |
796 | *((char *)(dummy) + 3) = ((delta >> 16) & 0xff); | |
797 | *((char *)(dummy) + 4) = ((delta >> 24) & 0xff); | |
798 | } | |
799 | ||
c906108c | 800 | void |
fba45db2 | 801 | i386_pop_frame (void) |
c906108c SS |
802 | { |
803 | struct frame_info *frame = get_current_frame (); | |
804 | CORE_ADDR fp; | |
805 | int regnum; | |
c906108c | 806 | char regbuf[MAX_REGISTER_RAW_SIZE]; |
c5aa993b | 807 | |
c906108c | 808 | fp = FRAME_FP (frame); |
1211c4e4 AC |
809 | i386_frame_init_saved_regs (frame); |
810 | ||
c5aa993b | 811 | for (regnum = 0; regnum < NUM_REGS; regnum++) |
c906108c | 812 | { |
fc338970 MK |
813 | CORE_ADDR addr; |
814 | addr = frame->saved_regs[regnum]; | |
815 | if (addr) | |
c906108c | 816 | { |
fc338970 | 817 | read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum)); |
c906108c SS |
818 | write_register_bytes (REGISTER_BYTE (regnum), regbuf, |
819 | REGISTER_RAW_SIZE (regnum)); | |
820 | } | |
821 | } | |
822 | write_register (FP_REGNUM, read_memory_integer (fp, 4)); | |
823 | write_register (PC_REGNUM, read_memory_integer (fp + 4, 4)); | |
824 | write_register (SP_REGNUM, fp + 8); | |
825 | flush_cached_frames (); | |
826 | } | |
fc338970 | 827 | \f |
c906108c SS |
828 | |
829 | #ifdef GET_LONGJMP_TARGET | |
830 | ||
fc338970 MK |
831 | /* Figure out where the longjmp will land. Slurp the args out of the |
832 | stack. We expect the first arg to be a pointer to the jmp_buf | |
833 | structure from which we extract the pc (JB_PC) that we will land | |
834 | at. The pc is copied into PC. This routine returns true on | |
835 | success. */ | |
c906108c SS |
836 | |
837 | int | |
fba45db2 | 838 | get_longjmp_target (CORE_ADDR *pc) |
c906108c SS |
839 | { |
840 | char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT]; | |
841 | CORE_ADDR sp, jb_addr; | |
842 | ||
843 | sp = read_register (SP_REGNUM); | |
844 | ||
fc338970 | 845 | if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack. */ |
c906108c SS |
846 | buf, |
847 | TARGET_PTR_BIT / TARGET_CHAR_BIT)) | |
848 | return 0; | |
849 | ||
850 | jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT); | |
851 | ||
852 | if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf, | |
853 | TARGET_PTR_BIT / TARGET_CHAR_BIT)) | |
854 | return 0; | |
855 | ||
856 | *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT); | |
857 | ||
858 | return 1; | |
859 | } | |
860 | ||
861 | #endif /* GET_LONGJMP_TARGET */ | |
fc338970 | 862 | \f |
c906108c | 863 | |
22f8ba57 | 864 | CORE_ADDR |
ea7c478f | 865 | i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp, |
22f8ba57 MK |
866 | int struct_return, CORE_ADDR struct_addr) |
867 | { | |
868 | sp = default_push_arguments (nargs, args, sp, struct_return, struct_addr); | |
869 | ||
870 | if (struct_return) | |
871 | { | |
872 | char buf[4]; | |
873 | ||
874 | sp -= 4; | |
875 | store_address (buf, 4, struct_addr); | |
876 | write_memory (sp, buf, 4); | |
877 | } | |
878 | ||
879 | return sp; | |
880 | } | |
881 | ||
882 | void | |
883 | i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp) | |
884 | { | |
885 | /* Do nothing. Everything was already done by i386_push_arguments. */ | |
886 | } | |
887 | ||
1a309862 MK |
888 | /* These registers are used for returning integers (and on some |
889 | targets also for returning `struct' and `union' values when their | |
ef9dff19 | 890 | size and alignment match an integer type). */ |
1a309862 MK |
891 | #define LOW_RETURN_REGNUM 0 /* %eax */ |
892 | #define HIGH_RETURN_REGNUM 2 /* %edx */ | |
893 | ||
894 | /* Extract from an array REGBUF containing the (raw) register state, a | |
895 | function return value of TYPE, and copy that, in virtual format, | |
896 | into VALBUF. */ | |
897 | ||
c906108c | 898 | void |
1a309862 | 899 | i386_extract_return_value (struct type *type, char *regbuf, char *valbuf) |
c906108c | 900 | { |
1a309862 MK |
901 | int len = TYPE_LENGTH (type); |
902 | ||
1e8d0a7b MK |
903 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT |
904 | && TYPE_NFIELDS (type) == 1) | |
3df1b9b4 MK |
905 | { |
906 | i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regbuf, valbuf); | |
907 | return; | |
908 | } | |
1e8d0a7b MK |
909 | |
910 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
c906108c | 911 | { |
1a309862 MK |
912 | if (NUM_FREGS == 0) |
913 | { | |
914 | warning ("Cannot find floating-point return value."); | |
915 | memset (valbuf, 0, len); | |
ef9dff19 | 916 | return; |
1a309862 MK |
917 | } |
918 | ||
c6ba6f0d MK |
919 | /* Floating-point return values can be found in %st(0). Convert |
920 | its contents to the desired type. This is probably not | |
921 | exactly how it would happen on the target itself, but it is | |
922 | the best we can do. */ | |
923 | convert_typed_floating (®buf[REGISTER_BYTE (FP0_REGNUM)], | |
924 | builtin_type_i387_ext, valbuf, type); | |
c906108c SS |
925 | } |
926 | else | |
c5aa993b | 927 | { |
d4f3574e SS |
928 | int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM); |
929 | int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM); | |
930 | ||
931 | if (len <= low_size) | |
1a309862 | 932 | memcpy (valbuf, ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len); |
d4f3574e SS |
933 | else if (len <= (low_size + high_size)) |
934 | { | |
935 | memcpy (valbuf, | |
1a309862 | 936 | ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size); |
d4f3574e | 937 | memcpy (valbuf + low_size, |
1a309862 | 938 | ®buf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size); |
d4f3574e SS |
939 | } |
940 | else | |
8e65ff28 AC |
941 | internal_error (__FILE__, __LINE__, |
942 | "Cannot extract return value of %d bytes long.", len); | |
c906108c SS |
943 | } |
944 | } | |
945 | ||
ef9dff19 MK |
946 | /* Write into the appropriate registers a function return value stored |
947 | in VALBUF of type TYPE, given in virtual format. */ | |
948 | ||
949 | void | |
950 | i386_store_return_value (struct type *type, char *valbuf) | |
951 | { | |
952 | int len = TYPE_LENGTH (type); | |
953 | ||
1e8d0a7b MK |
954 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT |
955 | && TYPE_NFIELDS (type) == 1) | |
3df1b9b4 MK |
956 | { |
957 | i386_store_return_value (TYPE_FIELD_TYPE (type, 0), valbuf); | |
958 | return; | |
959 | } | |
1e8d0a7b MK |
960 | |
961 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
ef9dff19 | 962 | { |
ccb945b8 | 963 | unsigned int fstat; |
c6ba6f0d | 964 | char buf[FPU_REG_RAW_SIZE]; |
ccb945b8 | 965 | |
ef9dff19 MK |
966 | if (NUM_FREGS == 0) |
967 | { | |
968 | warning ("Cannot set floating-point return value."); | |
969 | return; | |
970 | } | |
971 | ||
635b0cc1 MK |
972 | /* Returning floating-point values is a bit tricky. Apart from |
973 | storing the return value in %st(0), we have to simulate the | |
974 | state of the FPU at function return point. */ | |
975 | ||
c6ba6f0d MK |
976 | /* Convert the value found in VALBUF to the extended |
977 | floating-point format used by the FPU. This is probably | |
978 | not exactly how it would happen on the target itself, but | |
979 | it is the best we can do. */ | |
980 | convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext); | |
981 | write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf, | |
982 | FPU_REG_RAW_SIZE); | |
ccb945b8 | 983 | |
635b0cc1 MK |
984 | /* Set the top of the floating-point register stack to 7. The |
985 | actual value doesn't really matter, but 7 is what a normal | |
986 | function return would end up with if the program started out | |
987 | with a freshly initialized FPU. */ | |
ccb945b8 MK |
988 | fstat = read_register (FSTAT_REGNUM); |
989 | fstat |= (7 << 11); | |
990 | write_register (FSTAT_REGNUM, fstat); | |
991 | ||
635b0cc1 MK |
992 | /* Mark %st(1) through %st(7) as empty. Since we set the top of |
993 | the floating-point register stack to 7, the appropriate value | |
994 | for the tag word is 0x3fff. */ | |
ccb945b8 | 995 | write_register (FTAG_REGNUM, 0x3fff); |
ef9dff19 MK |
996 | } |
997 | else | |
998 | { | |
999 | int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM); | |
1000 | int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM); | |
1001 | ||
1002 | if (len <= low_size) | |
1003 | write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), valbuf, len); | |
1004 | else if (len <= (low_size + high_size)) | |
1005 | { | |
1006 | write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), | |
1007 | valbuf, low_size); | |
1008 | write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM), | |
1009 | valbuf + low_size, len - low_size); | |
1010 | } | |
1011 | else | |
8e65ff28 AC |
1012 | internal_error (__FILE__, __LINE__, |
1013 | "Cannot store return value of %d bytes long.", len); | |
ef9dff19 MK |
1014 | } |
1015 | } | |
f7af9647 MK |
1016 | |
1017 | /* Extract from an array REGBUF containing the (raw) register state | |
1018 | the address in which a function should return its structure value, | |
1019 | as a CORE_ADDR. */ | |
1020 | ||
1021 | CORE_ADDR | |
1022 | i386_extract_struct_value_address (char *regbuf) | |
1023 | { | |
1024 | return extract_address (®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], | |
1025 | REGISTER_RAW_SIZE (LOW_RETURN_REGNUM)); | |
1026 | } | |
fc338970 | 1027 | \f |
ef9dff19 | 1028 | |
d7a0d72c MK |
1029 | /* Return the GDB type object for the "standard" data type of data in |
1030 | register REGNUM. Perhaps %esi and %edi should go here, but | |
1031 | potentially they could be used for things other than address. */ | |
1032 | ||
1033 | struct type * | |
1034 | i386_register_virtual_type (int regnum) | |
1035 | { | |
1036 | if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM) | |
1037 | return lookup_pointer_type (builtin_type_void); | |
1038 | ||
1039 | if (IS_FP_REGNUM (regnum)) | |
c6ba6f0d | 1040 | return builtin_type_i387_ext; |
d7a0d72c MK |
1041 | |
1042 | if (IS_SSE_REGNUM (regnum)) | |
1043 | return builtin_type_v4sf; | |
1044 | ||
1045 | return builtin_type_int; | |
1046 | } | |
1047 | ||
1048 | /* Return true iff register REGNUM's virtual format is different from | |
1049 | its raw format. Note that this definition assumes that the host | |
1050 | supports IEEE 32-bit floats, since it doesn't say that SSE | |
1051 | registers need conversion. Even if we can't find a counterexample, | |
1052 | this is still sloppy. */ | |
1053 | ||
1054 | int | |
1055 | i386_register_convertible (int regnum) | |
1056 | { | |
1057 | return IS_FP_REGNUM (regnum); | |
1058 | } | |
1059 | ||
ac27f131 | 1060 | /* Convert data from raw format for register REGNUM in buffer FROM to |
3d261580 | 1061 | virtual format with type TYPE in buffer TO. */ |
ac27f131 MK |
1062 | |
1063 | void | |
1064 | i386_register_convert_to_virtual (int regnum, struct type *type, | |
1065 | char *from, char *to) | |
1066 | { | |
c6ba6f0d | 1067 | gdb_assert (IS_FP_REGNUM (regnum)); |
3d261580 MK |
1068 | |
1069 | /* We only support floating-point values. */ | |
8d7f6b4a MK |
1070 | if (TYPE_CODE (type) != TYPE_CODE_FLT) |
1071 | { | |
1072 | warning ("Cannot convert floating-point register value " | |
1073 | "to non-floating-point type."); | |
1074 | memset (to, 0, TYPE_LENGTH (type)); | |
1075 | return; | |
1076 | } | |
3d261580 | 1077 | |
c6ba6f0d MK |
1078 | /* Convert to TYPE. This should be a no-op if TYPE is equivalent to |
1079 | the extended floating-point format used by the FPU. */ | |
1080 | convert_typed_floating (from, builtin_type_i387_ext, to, type); | |
ac27f131 MK |
1081 | } |
1082 | ||
1083 | /* Convert data from virtual format with type TYPE in buffer FROM to | |
3d261580 | 1084 | raw format for register REGNUM in buffer TO. */ |
ac27f131 MK |
1085 | |
1086 | void | |
1087 | i386_register_convert_to_raw (struct type *type, int regnum, | |
1088 | char *from, char *to) | |
1089 | { | |
c6ba6f0d MK |
1090 | gdb_assert (IS_FP_REGNUM (regnum)); |
1091 | ||
1092 | /* We only support floating-point values. */ | |
1093 | if (TYPE_CODE (type) != TYPE_CODE_FLT) | |
1094 | { | |
1095 | warning ("Cannot convert non-floating-point type " | |
1096 | "to floating-point register value."); | |
1097 | memset (to, 0, TYPE_LENGTH (type)); | |
1098 | return; | |
1099 | } | |
3d261580 | 1100 | |
c6ba6f0d MK |
1101 | /* Convert from TYPE. This should be a no-op if TYPE is equivalent |
1102 | to the extended floating-point format used by the FPU. */ | |
1103 | convert_typed_floating (from, type, to, builtin_type_i387_ext); | |
ac27f131 | 1104 | } |
ac27f131 | 1105 | \f |
fc338970 | 1106 | |
c906108c | 1107 | #ifdef I386V4_SIGTRAMP_SAVED_PC |
fc338970 MK |
1108 | /* Get saved user PC for sigtramp from the pushed ucontext on the |
1109 | stack for all three variants of SVR4 sigtramps. */ | |
c906108c SS |
1110 | |
1111 | CORE_ADDR | |
fba45db2 | 1112 | i386v4_sigtramp_saved_pc (struct frame_info *frame) |
c906108c SS |
1113 | { |
1114 | CORE_ADDR saved_pc_offset = 4; | |
1115 | char *name = NULL; | |
1116 | ||
1117 | find_pc_partial_function (frame->pc, &name, NULL, NULL); | |
1118 | if (name) | |
1119 | { | |
1120 | if (STREQ (name, "_sigreturn")) | |
1121 | saved_pc_offset = 132 + 14 * 4; | |
1122 | else if (STREQ (name, "_sigacthandler")) | |
1123 | saved_pc_offset = 80 + 14 * 4; | |
1124 | else if (STREQ (name, "sigvechandler")) | |
1125 | saved_pc_offset = 120 + 14 * 4; | |
1126 | } | |
1127 | ||
1128 | if (frame->next) | |
1129 | return read_memory_integer (frame->next->frame + saved_pc_offset, 4); | |
1130 | return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4); | |
1131 | } | |
1132 | #endif /* I386V4_SIGTRAMP_SAVED_PC */ | |
fc338970 | 1133 | \f |
a0b3c4fd | 1134 | |
c906108c | 1135 | #ifdef STATIC_TRANSFORM_NAME |
fc338970 MK |
1136 | /* SunPRO encodes the static variables. This is not related to C++ |
1137 | mangling, it is done for C too. */ | |
c906108c SS |
1138 | |
1139 | char * | |
fba45db2 | 1140 | sunpro_static_transform_name (char *name) |
c906108c SS |
1141 | { |
1142 | char *p; | |
1143 | if (IS_STATIC_TRANSFORM_NAME (name)) | |
1144 | { | |
fc338970 MK |
1145 | /* For file-local statics there will be a period, a bunch of |
1146 | junk (the contents of which match a string given in the | |
c5aa993b JM |
1147 | N_OPT), a period and the name. For function-local statics |
1148 | there will be a bunch of junk (which seems to change the | |
1149 | second character from 'A' to 'B'), a period, the name of the | |
1150 | function, and the name. So just skip everything before the | |
1151 | last period. */ | |
c906108c SS |
1152 | p = strrchr (name, '.'); |
1153 | if (p != NULL) | |
1154 | name = p + 1; | |
1155 | } | |
1156 | return name; | |
1157 | } | |
1158 | #endif /* STATIC_TRANSFORM_NAME */ | |
fc338970 | 1159 | \f |
c906108c | 1160 | |
fc338970 | 1161 | /* Stuff for WIN32 PE style DLL's but is pretty generic really. */ |
c906108c SS |
1162 | |
1163 | CORE_ADDR | |
fba45db2 | 1164 | skip_trampoline_code (CORE_ADDR pc, char *name) |
c906108c | 1165 | { |
fc338970 | 1166 | if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */ |
c906108c | 1167 | { |
c5aa993b | 1168 | unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4); |
c906108c | 1169 | struct minimal_symbol *indsym = |
fc338970 | 1170 | indirect ? lookup_minimal_symbol_by_pc (indirect) : 0; |
c5aa993b | 1171 | char *symname = indsym ? SYMBOL_NAME (indsym) : 0; |
c906108c | 1172 | |
c5aa993b | 1173 | if (symname) |
c906108c | 1174 | { |
c5aa993b JM |
1175 | if (strncmp (symname, "__imp_", 6) == 0 |
1176 | || strncmp (symname, "_imp_", 5) == 0) | |
c906108c SS |
1177 | return name ? 1 : read_memory_unsigned_integer (indirect, 4); |
1178 | } | |
1179 | } | |
fc338970 | 1180 | return 0; /* Not a trampoline. */ |
c906108c | 1181 | } |
fc338970 MK |
1182 | \f |
1183 | ||
1184 | /* We have two flavours of disassembly. The machinery on this page | |
1185 | deals with switching between those. */ | |
c906108c SS |
1186 | |
1187 | static int | |
fba45db2 | 1188 | gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info) |
c906108c SS |
1189 | { |
1190 | if (disassembly_flavor == att_flavor) | |
1191 | return print_insn_i386_att (memaddr, info); | |
1192 | else if (disassembly_flavor == intel_flavor) | |
1193 | return print_insn_i386_intel (memaddr, info); | |
fc338970 MK |
1194 | /* Never reached -- disassembly_flavour is always either att_flavor |
1195 | or intel_flavor. */ | |
e1e9e218 | 1196 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); |
7a292a7a SS |
1197 | } |
1198 | ||
fc338970 | 1199 | \f |
2acceee2 | 1200 | |
a62cc96e AC |
1201 | struct gdbarch * |
1202 | i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) | |
1203 | { | |
cd3c07fc | 1204 | struct gdbarch_tdep *tdep; |
a62cc96e AC |
1205 | struct gdbarch *gdbarch; |
1206 | ||
1207 | /* For the moment there is only one i386 architecture. */ | |
1208 | if (arches != NULL) | |
1209 | return arches->gdbarch; | |
1210 | ||
1211 | /* Allocate space for the new architecture. */ | |
1212 | tdep = XMALLOC (struct gdbarch_tdep); | |
1213 | gdbarch = gdbarch_alloc (&info, tdep); | |
1214 | ||
1215 | set_gdbarch_use_generic_dummy_frames (gdbarch, 0); | |
1216 | ||
1217 | /* Call dummy code. */ | |
1218 | set_gdbarch_call_dummy_location (gdbarch, ON_STACK); | |
1219 | set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 5); | |
1220 | set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1); | |
1221 | set_gdbarch_call_dummy_p (gdbarch, 1); | |
1222 | set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0); | |
1223 | ||
1224 | set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register); | |
1225 | set_gdbarch_push_arguments (gdbarch, i386_push_arguments); | |
1226 | ||
1227 | set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_on_stack); | |
1228 | ||
1229 | /* NOTE: tm-i386nw.h and tm-i386v4.h override this. */ | |
1230 | set_gdbarch_frame_chain_valid (gdbarch, file_frame_chain_valid); | |
1231 | ||
1232 | return gdbarch; | |
1233 | } | |
1234 | ||
28e9e0f0 MK |
1235 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
1236 | void _initialize_i386_tdep (void); | |
1237 | ||
c906108c | 1238 | void |
fba45db2 | 1239 | _initialize_i386_tdep (void) |
c906108c | 1240 | { |
a62cc96e AC |
1241 | register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init); |
1242 | ||
917317f4 JM |
1243 | /* Initialize the table saying where each register starts in the |
1244 | register file. */ | |
1245 | { | |
1246 | int i, offset; | |
1247 | ||
1248 | offset = 0; | |
1249 | for (i = 0; i < MAX_NUM_REGS; i++) | |
1250 | { | |
1a11ba71 MK |
1251 | i386_register_offset[i] = offset; |
1252 | offset += i386_register_size[i]; | |
917317f4 JM |
1253 | } |
1254 | } | |
1255 | ||
c906108c SS |
1256 | tm_print_insn = gdb_print_insn_i386; |
1257 | tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach; | |
1258 | ||
fc338970 | 1259 | /* Add the variable that controls the disassembly flavor. */ |
917317f4 JM |
1260 | { |
1261 | struct cmd_list_element *new_cmd; | |
7a292a7a | 1262 | |
917317f4 JM |
1263 | new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class, |
1264 | valid_flavors, | |
1ed2a135 | 1265 | &disassembly_flavor, |
fc338970 MK |
1266 | "\ |
1267 | Set the disassembly flavor, the valid values are \"att\" and \"intel\", \ | |
c906108c | 1268 | and the default value is \"att\".", |
917317f4 | 1269 | &setlist); |
917317f4 JM |
1270 | add_show_from_set (new_cmd, &showlist); |
1271 | } | |
c906108c | 1272 | } |