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