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