]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/i386-tdep.c
s/@example/@smallexample/
[thirdparty/binutils-gdb.git] / gdb / i386-tdep.c
CommitLineData
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. */
48static 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. */
66static 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. */
70static 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
86char *
87i386_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. */
99int
100i386_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
108int
109i386_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
116int
117i386_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
125int
126i386_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
159int
160i386_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
187static const char att_flavor[] = "att";
188static const char intel_flavor[] = "intel";
189static const char *valid_flavors[] =
c5aa993b 190{
c906108c
SS
191 att_flavor,
192 intel_flavor,
193 NULL
194};
53904c9e 195static 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
216static CORE_ADDR codestream_next_addr;
217static CORE_ADDR codestream_addr;
218static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
219static int codestream_off;
220static 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 230static unsigned char
fba45db2 231codestream_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
245static void
fba45db2 246codestream_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
256static void
fba45db2 257codestream_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
269static void
fba45db2 270i386_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
326static long
fba45db2 327i386_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
472CORE_ADDR
473i386_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
488int
489i386_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
499CORE_ADDR
500i386_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
514CORE_ADDR
515i386go32_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
522CORE_ADDR
523i386_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
531int
fba45db2 532i386_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
628void
fba45db2 629i386_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
688int
fba45db2 689i386_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
771void
fba45db2 772i386_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
794void
795i386_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 811void
fba45db2 812i386_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
e41e6bbf
PM
842/* FIXME: Multi-arching does not set JB_PC and JB_ELEMENT_SIZE yet.
843 Fill in with dummy value to enable compilation. */
844#ifndef JB_PC
845#define JB_PC 0
846#endif /* JB_PC */
847
848#ifndef JB_ELEMENT_SIZE
849#define JB_ELEMENT_SIZE 4
850#endif /* JB_ELEMENT_SIZE */
851
fc338970
MK
852/* Figure out where the longjmp will land. Slurp the args out of the
853 stack. We expect the first arg to be a pointer to the jmp_buf
854 structure from which we extract the pc (JB_PC) that we will land
855 at. The pc is copied into PC. This routine returns true on
856 success. */
c906108c
SS
857
858int
fba45db2 859get_longjmp_target (CORE_ADDR *pc)
c906108c
SS
860{
861 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
862 CORE_ADDR sp, jb_addr;
863
864 sp = read_register (SP_REGNUM);
865
fc338970 866 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack. */
c906108c
SS
867 buf,
868 TARGET_PTR_BIT / TARGET_CHAR_BIT))
869 return 0;
870
871 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
872
873 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
874 TARGET_PTR_BIT / TARGET_CHAR_BIT))
875 return 0;
876
877 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
878
879 return 1;
880}
881
882#endif /* GET_LONGJMP_TARGET */
fc338970 883\f
c906108c 884
22f8ba57 885CORE_ADDR
ea7c478f 886i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
22f8ba57
MK
887 int struct_return, CORE_ADDR struct_addr)
888{
889 sp = default_push_arguments (nargs, args, sp, struct_return, struct_addr);
890
891 if (struct_return)
892 {
893 char buf[4];
894
895 sp -= 4;
896 store_address (buf, 4, struct_addr);
897 write_memory (sp, buf, 4);
898 }
899
900 return sp;
901}
902
903void
904i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
905{
906 /* Do nothing. Everything was already done by i386_push_arguments. */
907}
908
1a309862
MK
909/* These registers are used for returning integers (and on some
910 targets also for returning `struct' and `union' values when their
ef9dff19 911 size and alignment match an integer type). */
1a309862
MK
912#define LOW_RETURN_REGNUM 0 /* %eax */
913#define HIGH_RETURN_REGNUM 2 /* %edx */
914
915/* Extract from an array REGBUF containing the (raw) register state, a
916 function return value of TYPE, and copy that, in virtual format,
917 into VALBUF. */
918
c906108c 919void
1a309862 920i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
c906108c 921{
1a309862
MK
922 int len = TYPE_LENGTH (type);
923
1e8d0a7b
MK
924 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
925 && TYPE_NFIELDS (type) == 1)
3df1b9b4
MK
926 {
927 i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regbuf, valbuf);
928 return;
929 }
1e8d0a7b
MK
930
931 if (TYPE_CODE (type) == TYPE_CODE_FLT)
c906108c 932 {
1a309862
MK
933 if (NUM_FREGS == 0)
934 {
935 warning ("Cannot find floating-point return value.");
936 memset (valbuf, 0, len);
ef9dff19 937 return;
1a309862
MK
938 }
939
c6ba6f0d
MK
940 /* Floating-point return values can be found in %st(0). Convert
941 its contents to the desired type. This is probably not
942 exactly how it would happen on the target itself, but it is
943 the best we can do. */
944 convert_typed_floating (&regbuf[REGISTER_BYTE (FP0_REGNUM)],
945 builtin_type_i387_ext, valbuf, type);
c906108c
SS
946 }
947 else
c5aa993b 948 {
d4f3574e
SS
949 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
950 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
951
952 if (len <= low_size)
1a309862 953 memcpy (valbuf, &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
d4f3574e
SS
954 else if (len <= (low_size + high_size))
955 {
956 memcpy (valbuf,
1a309862 957 &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
d4f3574e 958 memcpy (valbuf + low_size,
1a309862 959 &regbuf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
d4f3574e
SS
960 }
961 else
8e65ff28
AC
962 internal_error (__FILE__, __LINE__,
963 "Cannot extract return value of %d bytes long.", len);
c906108c
SS
964 }
965}
966
ef9dff19
MK
967/* Write into the appropriate registers a function return value stored
968 in VALBUF of type TYPE, given in virtual format. */
969
970void
971i386_store_return_value (struct type *type, char *valbuf)
972{
973 int len = TYPE_LENGTH (type);
974
1e8d0a7b
MK
975 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
976 && TYPE_NFIELDS (type) == 1)
3df1b9b4
MK
977 {
978 i386_store_return_value (TYPE_FIELD_TYPE (type, 0), valbuf);
979 return;
980 }
1e8d0a7b
MK
981
982 if (TYPE_CODE (type) == TYPE_CODE_FLT)
ef9dff19 983 {
ccb945b8 984 unsigned int fstat;
c6ba6f0d 985 char buf[FPU_REG_RAW_SIZE];
ccb945b8 986
ef9dff19
MK
987 if (NUM_FREGS == 0)
988 {
989 warning ("Cannot set floating-point return value.");
990 return;
991 }
992
635b0cc1
MK
993 /* Returning floating-point values is a bit tricky. Apart from
994 storing the return value in %st(0), we have to simulate the
995 state of the FPU at function return point. */
996
c6ba6f0d
MK
997 /* Convert the value found in VALBUF to the extended
998 floating-point format used by the FPU. This is probably
999 not exactly how it would happen on the target itself, but
1000 it is the best we can do. */
1001 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
1002 write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
1003 FPU_REG_RAW_SIZE);
ccb945b8 1004
635b0cc1
MK
1005 /* Set the top of the floating-point register stack to 7. The
1006 actual value doesn't really matter, but 7 is what a normal
1007 function return would end up with if the program started out
1008 with a freshly initialized FPU. */
ccb945b8
MK
1009 fstat = read_register (FSTAT_REGNUM);
1010 fstat |= (7 << 11);
1011 write_register (FSTAT_REGNUM, fstat);
1012
635b0cc1
MK
1013 /* Mark %st(1) through %st(7) as empty. Since we set the top of
1014 the floating-point register stack to 7, the appropriate value
1015 for the tag word is 0x3fff. */
ccb945b8 1016 write_register (FTAG_REGNUM, 0x3fff);
ef9dff19
MK
1017 }
1018 else
1019 {
1020 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
1021 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
1022
1023 if (len <= low_size)
1024 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), valbuf, len);
1025 else if (len <= (low_size + high_size))
1026 {
1027 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM),
1028 valbuf, low_size);
1029 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM),
1030 valbuf + low_size, len - low_size);
1031 }
1032 else
8e65ff28
AC
1033 internal_error (__FILE__, __LINE__,
1034 "Cannot store return value of %d bytes long.", len);
ef9dff19
MK
1035 }
1036}
f7af9647
MK
1037
1038/* Extract from an array REGBUF containing the (raw) register state
1039 the address in which a function should return its structure value,
1040 as a CORE_ADDR. */
1041
1042CORE_ADDR
1043i386_extract_struct_value_address (char *regbuf)
1044{
1045 return extract_address (&regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)],
1046 REGISTER_RAW_SIZE (LOW_RETURN_REGNUM));
1047}
fc338970 1048\f
ef9dff19 1049
d7a0d72c
MK
1050/* Return the GDB type object for the "standard" data type of data in
1051 register REGNUM. Perhaps %esi and %edi should go here, but
1052 potentially they could be used for things other than address. */
1053
1054struct type *
1055i386_register_virtual_type (int regnum)
1056{
1057 if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM)
1058 return lookup_pointer_type (builtin_type_void);
1059
1060 if (IS_FP_REGNUM (regnum))
c6ba6f0d 1061 return builtin_type_i387_ext;
d7a0d72c
MK
1062
1063 if (IS_SSE_REGNUM (regnum))
1064 return builtin_type_v4sf;
1065
1066 return builtin_type_int;
1067}
1068
1069/* Return true iff register REGNUM's virtual format is different from
1070 its raw format. Note that this definition assumes that the host
1071 supports IEEE 32-bit floats, since it doesn't say that SSE
1072 registers need conversion. Even if we can't find a counterexample,
1073 this is still sloppy. */
1074
1075int
1076i386_register_convertible (int regnum)
1077{
1078 return IS_FP_REGNUM (regnum);
1079}
1080
ac27f131 1081/* Convert data from raw format for register REGNUM in buffer FROM to
3d261580 1082 virtual format with type TYPE in buffer TO. */
ac27f131
MK
1083
1084void
1085i386_register_convert_to_virtual (int regnum, struct type *type,
1086 char *from, char *to)
1087{
c6ba6f0d 1088 gdb_assert (IS_FP_REGNUM (regnum));
3d261580
MK
1089
1090 /* We only support floating-point values. */
8d7f6b4a
MK
1091 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1092 {
1093 warning ("Cannot convert floating-point register value "
1094 "to non-floating-point type.");
1095 memset (to, 0, TYPE_LENGTH (type));
1096 return;
1097 }
3d261580 1098
c6ba6f0d
MK
1099 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1100 the extended floating-point format used by the FPU. */
1101 convert_typed_floating (from, builtin_type_i387_ext, to, type);
ac27f131
MK
1102}
1103
1104/* Convert data from virtual format with type TYPE in buffer FROM to
3d261580 1105 raw format for register REGNUM in buffer TO. */
ac27f131
MK
1106
1107void
1108i386_register_convert_to_raw (struct type *type, int regnum,
1109 char *from, char *to)
1110{
c6ba6f0d
MK
1111 gdb_assert (IS_FP_REGNUM (regnum));
1112
1113 /* We only support floating-point values. */
1114 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1115 {
1116 warning ("Cannot convert non-floating-point type "
1117 "to floating-point register value.");
1118 memset (to, 0, TYPE_LENGTH (type));
1119 return;
1120 }
3d261580 1121
c6ba6f0d
MK
1122 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1123 to the extended floating-point format used by the FPU. */
1124 convert_typed_floating (from, type, to, builtin_type_i387_ext);
ac27f131 1125}
ac27f131 1126\f
fc338970 1127
c906108c 1128#ifdef I386V4_SIGTRAMP_SAVED_PC
fc338970
MK
1129/* Get saved user PC for sigtramp from the pushed ucontext on the
1130 stack for all three variants of SVR4 sigtramps. */
c906108c
SS
1131
1132CORE_ADDR
fba45db2 1133i386v4_sigtramp_saved_pc (struct frame_info *frame)
c906108c
SS
1134{
1135 CORE_ADDR saved_pc_offset = 4;
1136 char *name = NULL;
1137
1138 find_pc_partial_function (frame->pc, &name, NULL, NULL);
1139 if (name)
1140 {
1141 if (STREQ (name, "_sigreturn"))
1142 saved_pc_offset = 132 + 14 * 4;
1143 else if (STREQ (name, "_sigacthandler"))
1144 saved_pc_offset = 80 + 14 * 4;
1145 else if (STREQ (name, "sigvechandler"))
1146 saved_pc_offset = 120 + 14 * 4;
1147 }
1148
1149 if (frame->next)
1150 return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
1151 return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
1152}
1153#endif /* I386V4_SIGTRAMP_SAVED_PC */
fc338970 1154\f
a0b3c4fd 1155
c906108c 1156#ifdef STATIC_TRANSFORM_NAME
fc338970
MK
1157/* SunPRO encodes the static variables. This is not related to C++
1158 mangling, it is done for C too. */
c906108c
SS
1159
1160char *
fba45db2 1161sunpro_static_transform_name (char *name)
c906108c
SS
1162{
1163 char *p;
1164 if (IS_STATIC_TRANSFORM_NAME (name))
1165 {
fc338970
MK
1166 /* For file-local statics there will be a period, a bunch of
1167 junk (the contents of which match a string given in the
c5aa993b
JM
1168 N_OPT), a period and the name. For function-local statics
1169 there will be a bunch of junk (which seems to change the
1170 second character from 'A' to 'B'), a period, the name of the
1171 function, and the name. So just skip everything before the
1172 last period. */
c906108c
SS
1173 p = strrchr (name, '.');
1174 if (p != NULL)
1175 name = p + 1;
1176 }
1177 return name;
1178}
1179#endif /* STATIC_TRANSFORM_NAME */
fc338970 1180\f
c906108c 1181
fc338970 1182/* Stuff for WIN32 PE style DLL's but is pretty generic really. */
c906108c
SS
1183
1184CORE_ADDR
fba45db2 1185skip_trampoline_code (CORE_ADDR pc, char *name)
c906108c 1186{
fc338970 1187 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
c906108c 1188 {
c5aa993b 1189 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
c906108c 1190 struct minimal_symbol *indsym =
fc338970 1191 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
c5aa993b 1192 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
c906108c 1193
c5aa993b 1194 if (symname)
c906108c 1195 {
c5aa993b
JM
1196 if (strncmp (symname, "__imp_", 6) == 0
1197 || strncmp (symname, "_imp_", 5) == 0)
c906108c
SS
1198 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1199 }
1200 }
fc338970 1201 return 0; /* Not a trampoline. */
c906108c 1202}
fc338970
MK
1203\f
1204
1205/* We have two flavours of disassembly. The machinery on this page
1206 deals with switching between those. */
c906108c
SS
1207
1208static int
fba45db2 1209gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
c906108c
SS
1210{
1211 if (disassembly_flavor == att_flavor)
1212 return print_insn_i386_att (memaddr, info);
1213 else if (disassembly_flavor == intel_flavor)
1214 return print_insn_i386_intel (memaddr, info);
fc338970
MK
1215 /* Never reached -- disassembly_flavour is always either att_flavor
1216 or intel_flavor. */
e1e9e218 1217 internal_error (__FILE__, __LINE__, "failed internal consistency check");
7a292a7a
SS
1218}
1219
fc338970 1220\f
d2a7c97a
MK
1221static void
1222process_note_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
1223{
1224 int *os_ident_ptr = obj;
1225 const char *name;
1226 unsigned int sect_size;
1227
1228 name = bfd_get_section_name (abfd, sect);
1229 sect_size = bfd_section_size (abfd, sect);
1230 if (strcmp (name, ".note.ABI-tag") == 0 && sect_size > 0)
1231 {
1232 unsigned int name_length, data_length, note_type;
1233 char *note = alloca (sect_size);
1234
1235 bfd_get_section_contents (abfd, sect, note,
1236 (file_ptr) 0, (bfd_size_type) sect_size);
1237
1238 name_length = bfd_h_get_32 (abfd, note);
1239 data_length = bfd_h_get_32 (abfd, note + 4);
1240 note_type = bfd_h_get_32 (abfd, note + 8);
1241
1242 if (name_length == 4 && data_length == 16 && note_type == 1
1243 && strcmp (note + 12, "GNU") == 0)
1244 {
1245 int os_number = bfd_h_get_32 (abfd, note + 16);
1246
1247 /* The case numbers are from abi-tags in glibc. */
1248 switch (os_number)
1249 {
1250 case 0:
1251 *os_ident_ptr = ELFOSABI_LINUX;
1252 break;
1253 case 1:
1254 *os_ident_ptr = ELFOSABI_HURD;
1255 break;
1256 case 2:
1257 *os_ident_ptr = ELFOSABI_SOLARIS;
1258 break;
1259 default:
1260 internal_error (__FILE__, __LINE__,
1261 "process_note_abi_sections: "
1262 "unknown OS number %d", os_number);
1263 break;
1264 }
1265 }
1266 }
1267}
2acceee2 1268
a62cc96e
AC
1269struct gdbarch *
1270i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1271{
cd3c07fc 1272 struct gdbarch_tdep *tdep;
a62cc96e 1273 struct gdbarch *gdbarch;
d2a7c97a 1274 int os_ident;
a62cc96e 1275
d2a7c97a
MK
1276 if (info.abfd != NULL
1277 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
1278 {
1279 os_ident = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
1280
1281 /* If os_ident is 0, it is not necessarily the case that we're
1282 on a SYSV system. (ELFOSABI_NONE is defined to be 0.)
1283 GNU/Linux uses a note section to record OS/ABI info, but
1284 leaves e_ident[EI_OSABI] zero. So we have to check for note
1285 sections too. */
1286 if (os_ident == ELFOSABI_NONE)
1287 bfd_map_over_sections (info.abfd,
1288 process_note_abi_tag_sections,
1289 &os_ident);
1290
1291 /* If that didn't help us, revert to some non-standard checks. */
1292 if (os_ident == ELFOSABI_NONE)
1293 {
1294 /* FreeBSD folks are naughty; they stored the string
1295 "FreeBSD" in the padding of the e_ident field of the ELF
1296 header. */
1297 if (strcmp (&elf_elfheader (info.abfd)->e_ident[8], "FreeBSD") == 0)
1298 os_ident = ELFOSABI_FREEBSD;
1299 }
1300 }
1301 else
1302 os_ident = -1;
1303
1304 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1305 arches != NULL;
1306 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1307 {
65d6d66a
PS
1308 tdep = gdbarch_tdep (arches->gdbarch);
1309 if (tdep && tdep->os_ident == os_ident)
1310 return arches->gdbarch;
d2a7c97a 1311 }
a62cc96e
AC
1312
1313 /* Allocate space for the new architecture. */
1314 tdep = XMALLOC (struct gdbarch_tdep);
1315 gdbarch = gdbarch_alloc (&info, tdep);
1316
d2a7c97a
MK
1317 tdep->os_ident = os_ident;
1318
96297dab
MK
1319 /* FIXME: kettenis/2001-11-24: Although not all IA-32 processors
1320 have the SSE registers, it's easier to set the default to 8. */
1321 tdep->num_xmm_regs = 8;
1322
a62cc96e
AC
1323 set_gdbarch_use_generic_dummy_frames (gdbarch, 0);
1324
1325 /* Call dummy code. */
1326 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1327 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 5);
1328 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1329 set_gdbarch_call_dummy_p (gdbarch, 1);
1330 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1331
1332 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1333 set_gdbarch_push_arguments (gdbarch, i386_push_arguments);
1334
1335 set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_on_stack);
1336
1337 /* NOTE: tm-i386nw.h and tm-i386v4.h override this. */
1338 set_gdbarch_frame_chain_valid (gdbarch, file_frame_chain_valid);
1339
bd1ef5d2
AC
1340 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-linux.h,
1341 tm-ptx.h, tm-symmetry.h currently override this. Sigh. */
1342 set_gdbarch_num_regs (gdbarch, NUM_GREGS + NUM_FREGS + NUM_SSE_REGS);
1343
a62cc96e
AC
1344 return gdbarch;
1345}
1346
28e9e0f0
MK
1347/* Provide a prototype to silence -Wmissing-prototypes. */
1348void _initialize_i386_tdep (void);
1349
c906108c 1350void
fba45db2 1351_initialize_i386_tdep (void)
c906108c 1352{
a62cc96e
AC
1353 register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
1354
917317f4
JM
1355 /* Initialize the table saying where each register starts in the
1356 register file. */
1357 {
1358 int i, offset;
1359
1360 offset = 0;
1361 for (i = 0; i < MAX_NUM_REGS; i++)
1362 {
1a11ba71
MK
1363 i386_register_offset[i] = offset;
1364 offset += i386_register_size[i];
917317f4
JM
1365 }
1366 }
1367
c906108c
SS
1368 tm_print_insn = gdb_print_insn_i386;
1369 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1370
fc338970 1371 /* Add the variable that controls the disassembly flavor. */
917317f4
JM
1372 {
1373 struct cmd_list_element *new_cmd;
7a292a7a 1374
917317f4
JM
1375 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1376 valid_flavors,
1ed2a135 1377 &disassembly_flavor,
fc338970
MK
1378 "\
1379Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
c906108c 1380and the default value is \"att\".",
917317f4 1381 &setlist);
917317f4
JM
1382 add_show_from_set (new_cmd, &showlist);
1383 }
c906108c 1384}