home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / gas / config / tc-i386.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  89KB  |  3,169 lines

  1. /* i386.c -- Assemble code for the Intel 80386
  2.    Copyright (C) 1989, 1991, 1992, 1993 Free Software Foundation.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  19.  
  20. /*
  21.   Intel 80386 machine specific gas.
  22.   Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
  23.   Bugs & suggestions are completely welcome.  This is free software.
  24.   Please help us make it better.
  25.   */
  26.  
  27. #include <ctype.h>
  28.  
  29. #include "as.h"
  30. #include "subsegs.h"
  31.  
  32. #include "obstack.h"
  33. #include "opcode/i386.h"
  34.  
  35. #ifndef TC_RELOC
  36. #define TC_RELOC(X,Y) (Y)
  37. #endif
  38.  
  39. /* 'md_assemble ()' gathers together information and puts it into a
  40.    i386_insn. */
  41.  
  42. struct _i386_insn
  43.   {
  44.     /* TM holds the template for the insn were currently assembling. */
  45.     template tm;
  46.     /* SUFFIX holds the opcode suffix (e.g. 'l' for 'movl') if given. */
  47.     char suffix;
  48.     /* Operands are coded with OPERANDS, TYPES, DISPS, IMMS, and REGS. */
  49.  
  50.     /* OPERANDS gives the number of given operands. */
  51.     unsigned int operands;
  52.  
  53.     /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
  54.        of given register, displacement, memory operands and immediate
  55.        operands. */
  56.     unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
  57.  
  58.     /* TYPES [i] is the type (see above #defines) which tells us how to
  59.        search through DISPS [i] & IMMS [i] & REGS [i] for the required
  60.        operand.  */
  61.     unsigned int types[MAX_OPERANDS];
  62.  
  63.     /* Displacements (if given) for each operand. */
  64.     expressionS *disps[MAX_OPERANDS];
  65.  
  66.     /* Relocation type for operand */
  67. #ifdef BFD_ASSEMBLER
  68.     enum bfd_reloc_code_real disp_reloc[MAX_OPERANDS];
  69. #else
  70.     int disp_reloc[MAX_OPERANDS];
  71. #endif
  72.  
  73.     /* Immediate operands (if given) for each operand. */
  74.     expressionS *imms[MAX_OPERANDS];
  75.  
  76.     /* Register operands (if given) for each operand. */
  77.     reg_entry *regs[MAX_OPERANDS];
  78.  
  79.     /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
  80.        the base index byte below.  */
  81.     reg_entry *base_reg;
  82.     reg_entry *index_reg;
  83.     unsigned int log2_scale_factor;
  84.  
  85.     /* SEG gives the seg_entry of this insn.  It is equal to zero unless
  86.        an explicit segment override is given. */
  87.     const seg_entry *seg;    /* segment for memory operands (if given) */
  88.  
  89.     /* PREFIX holds all the given prefix opcodes (usually null).
  90.        PREFIXES is the size of PREFIX. */
  91.     /* richfix: really unsigned? */
  92.     unsigned char prefix[MAX_PREFIXES];
  93.     unsigned int prefixes;
  94.  
  95.     /* RM and IB are the modrm byte and the base index byte where the
  96.        addressing modes of this insn are encoded. */
  97.  
  98.     modrm_byte rm;
  99.     base_index_byte bi;
  100.   };
  101.  
  102. typedef struct _i386_insn i386_insn;
  103.  
  104. /* This array holds the chars that always start a comment.  If the
  105.    pre-processor is disabled, these aren't very useful */
  106. #if defined (TE_I386AIX) || defined (OBJ_ELF)
  107. const char comment_chars[] = "#/";
  108. #else
  109. const char comment_chars[] = "#";
  110. #endif
  111.  
  112. /* This array holds the chars that only start a comment at the beginning of
  113.    a line.  If the line seems to have the form '# 123 filename'
  114.    .line and .file directives will appear in the pre-processed output */
  115. /* Note that input_file.c hand checks for '#' at the beginning of the
  116.    first line of the input file.  This is because the compiler outputs
  117.    #NO_APP at the beginning of its output. */
  118. /* Also note that comments started like this one will always work if
  119.    '/' isn't otherwise defined.  */
  120. #if defined (TE_I386AIX) || defined (OBJ_ELF)
  121. const char line_comment_chars[] = "";
  122. #else
  123. const char line_comment_chars[] = "/";
  124. #endif
  125. const char line_separator_chars[] = "";
  126.  
  127. /* Chars that can be used to separate mant from exp in floating point nums */
  128. const char EXP_CHARS[] = "eE";
  129.  
  130. /* Chars that mean this number is a floating point constant */
  131. /* As in 0f12.456 */
  132. /* or    0d1.2345e12 */
  133. const char FLT_CHARS[] = "fFdDxX";
  134.  
  135. /* tables for lexical analysis */
  136. static char opcode_chars[256];
  137. static char register_chars[256];
  138. static char operand_chars[256];
  139. static char space_chars[256];
  140. static char identifier_chars[256];
  141. static char digit_chars[256];
  142.  
  143. /* lexical macros */
  144. #define is_opcode_char(x) (opcode_chars[(unsigned char) x])
  145. #define is_operand_char(x) (operand_chars[(unsigned char) x])
  146. #define is_register_char(x) (register_chars[(unsigned char) x])
  147. #define is_space_char(x) (space_chars[(unsigned char) x])
  148. #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
  149. #define is_digit_char(x) (digit_chars[(unsigned char) x])
  150.  
  151. /* put here all non-digit non-letter charcters that may occur in an operand */
  152. static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
  153.  
  154. static char *ordinal_names[] = {"first", "second", "third"}; /* for printfs */
  155.  
  156. /* md_assemble() always leaves the strings it's passed unaltered.  To
  157.    effect this we maintain a stack of saved characters that we've smashed
  158.    with '\0's (indicating end of strings for various sub-fields of the
  159.    assembler instruction). */
  160. static char save_stack[32];
  161. static char *save_stack_p;    /* stack pointer */
  162. #define END_STRING_AND_SAVE(s)      *save_stack_p++ = *s; *s = '\0'
  163. #define RESTORE_END_STRING(s)       *s = *--save_stack_p
  164.  
  165. /* The instruction we're assembling. */
  166. static i386_insn i;
  167.  
  168. /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
  169. static expressionS disp_expressions[2], im_expressions[2];
  170.  
  171. /* pointers to ebp & esp entries in reg_hash hash table */
  172. static reg_entry *ebp, *esp;
  173.  
  174. static int this_operand;    /* current operand we are working on */
  175.  
  176. static int flag_do_long_jump;    /* FIXME what does this do? */
  177.  
  178. static int flag_16bit_code;    /* 1 if we're writing 16-bit code, 0 if 32-bit */
  179.  
  180. /* Interface to relax_segment.
  181.    There are 2 relax states for 386 jump insns: one for conditional &
  182.    one for unconditional jumps.  This is because the these two types
  183.    of jumps add different sizes to frags when we're figuring out what
  184.    sort of jump to choose to reach a given label.  */
  185.  
  186. /* types */
  187. #define COND_JUMP 1        /* conditional jump */
  188. #define UNCOND_JUMP 2        /* unconditional jump */
  189. /* sizes */
  190. #define BYTE 0
  191. #define WORD 1
  192. #define DWORD 2
  193. #define UNKNOWN_SIZE 3
  194.  
  195. #ifndef INLINE
  196. #ifdef __GNUC__
  197. #define INLINE __inline__
  198. #else
  199. #define INLINE
  200. #endif
  201. #endif
  202.  
  203. #define ENCODE_RELAX_STATE(type,size) \
  204.   ((relax_substateT)((type<<2) | (size)))
  205. #define SIZE_FROM_RELAX_STATE(s) \
  206.     ( (((s) & 0x3) == BYTE ? 1 : (((s) & 0x3) == WORD ? 2 : 4)) )
  207.  
  208. const relax_typeS md_relax_table[] =
  209. {
  210. /* The fields are:
  211.    1) most positive reach of this state,
  212.    2) most negative reach of this state,
  213.    3) how many bytes this mode will add to the size of the current frag
  214.    4) which index into the table to try if we can't fit into this one.
  215.    */
  216.   {1, 1, 0, 0},
  217.   {1, 1, 0, 0},
  218.   {1, 1, 0, 0},
  219.   {1, 1, 0, 0},
  220.  
  221.   /* For now we don't use word displacement jumps; they may be
  222.      untrustworthy. */
  223.   {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
  224.   /* word conditionals add 3 bytes to frag:
  225.      2 opcode prefix; 1 displacement bytes */
  226.   {32767 + 2, -32768 + 2, 3, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
  227.   /* dword conditionals adds 4 bytes to frag:
  228.      1 opcode prefix; 3 displacement bytes */
  229.   {0, 0, 4, 0},
  230.   {1, 1, 0, 0},
  231.  
  232.   {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
  233.   /* word jmp adds 2 bytes to frag:
  234.      1 opcode prefix; 1 displacement bytes */
  235.   {32767 + 2, -32768 + 2, 2, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
  236.   /* dword jmp adds 3 bytes to frag:
  237.      0 opcode prefix; 3 displacement bytes */
  238.   {0, 0, 3, 0},
  239.   {1, 1, 0, 0},
  240.  
  241. };
  242.  
  243.  
  244. void
  245. i386_align_code (fragP, count)
  246.      fragS *fragP;
  247.      int count;
  248. {
  249.   /* Various efficient no-op patterns for aligning code labels.  */
  250.   static const char f32_1[] = {0x90};
  251.   static const char f32_2[] = {0x8d,0x36};
  252.   static const char f32_3[] = {0x8d,0x76,0x00};
  253.   static const char f32_4[] = {0x8d,0x74,0x26,0x00};
  254.   static const char f32_5[] = {0x90,
  255.                    0x8d,0x74,0x26,0x00};
  256.   static const char f32_6[] = {0x8d,0xb6,0x00,0x00,0x00,0x00};
  257.   static const char f32_7[] = {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};
  258.   static const char f32_8[] = {0x90,
  259.                    0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};
  260.   static const char f32_9[] = {0x8d,0x36,
  261.                    0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};
  262.   static const char f32_10[] = {0x8d,0x76,0x00,
  263.                 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};
  264.   static const char f32_11[] = {0x8d,0x74,0x26,0x00,
  265.                 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};
  266.   static const char f32_12[] = {0x8d,0xb6,0x00,0x00,0x00,0x00,
  267.                 0x8d,0xb6,0x00,0x00,0x00,0x00};
  268.   static const char f32_13[] = {0x8d,0xb6,0x00,0x00,0x00,0x00,
  269.                 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};
  270.   static const char f32_14[] = {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00,
  271.                 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00};
  272.   static const char f32_15[] = {0xeb,0x0d,0x90,0x90,0x90,0x90,0x90,
  273.                 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
  274.   static const char f16_4[] = {0x8d,0xb6,0x00,0x00};
  275.   static const char f16_5[] = {0x90,
  276.                    0x8d,0xb6,0x00,0x00};
  277.   static const char f16_6[] = {0x8d,0x36,
  278.                    0x8d,0xb6,0x00,0x00};
  279.   static const char f16_7[] = {0x8d,0x76,0x00,
  280.                    0x8d,0xb6,0x00,0x00};
  281.   static const char f16_8[] = {0x8d,0xb6,0x00,0x00,
  282.                    0x8d,0xb6,0x00,0x00};
  283.   static const char *const f32_patt[] = {
  284.     f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
  285.     f32_9, f32_10, f32_11, f32_12, f32_13, f32_14, f32_15
  286.   };
  287.   static const char *const f16_patt[] = {
  288.     f32_1, f32_2, f32_3, f16_4, f16_5, f16_6, f16_7, f16_8,
  289.     f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
  290.   };
  291.  
  292.   if (count > 0 && count <= 15)
  293.     {
  294.       if (flag_16bit_code)
  295.     {
  296.       memcpy(fragP->fr_literal + fragP->fr_fix,
  297.          f16_patt[count - 1], count);
  298.       if (count > 8) /* adjust jump offset */
  299.         fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
  300.     }
  301.       else
  302.     memcpy(fragP->fr_literal + fragP->fr_fix,
  303.            f32_patt[count - 1], count);
  304.       fragP->fr_var = count;
  305.     }
  306. }
  307.  
  308. static char *output_invalid PARAMS ((int c));
  309. static int i386_operand PARAMS ((char *operand_string));
  310. static reg_entry *parse_register PARAMS ((char *reg_string));
  311. #ifndef I386COFF
  312. static void s_bss PARAMS ((int));
  313. #endif
  314.  
  315. symbolS *GOT_symbol;        /* Pre-defined "__GLOBAL_OFFSET_TABLE" */
  316.  
  317. static INLINE unsigned long
  318. mode_from_disp_size (t)
  319.      unsigned long t;
  320. {
  321.   return (t & Disp8) ? 1 : (t & Disp32) ? 2 : 0;
  322. }
  323.  
  324. #if 0
  325. /* Not used.  */
  326. /* convert opcode suffix ('b' 'w' 'l' typically) into type specifier */
  327.  
  328. static INLINE unsigned long
  329. opcode_suffix_to_type (s)
  330.      unsigned long s;
  331. {
  332.   return (s == BYTE_OPCODE_SUFFIX
  333.       ? Byte : (s == WORD_OPCODE_SUFFIX
  334.             ? Word : DWord));
  335. }                /* opcode_suffix_to_type() */
  336. #endif
  337.  
  338. static INLINE int
  339. fits_in_signed_byte (num)
  340.      long num;
  341. {
  342.   return (num >= -128) && (num <= 127);
  343. }                /* fits_in_signed_byte() */
  344.  
  345. static INLINE int
  346. fits_in_unsigned_byte (num)
  347.      long num;
  348. {
  349.   return (num & 0xff) == num;
  350. }                /* fits_in_unsigned_byte() */
  351.  
  352. static INLINE int
  353. fits_in_unsigned_word (num)
  354.      long num;
  355. {
  356.   return (num & 0xffff) == num;
  357. }                /* fits_in_unsigned_word() */
  358.  
  359. static INLINE int
  360. fits_in_signed_word (num)
  361.      long num;
  362. {
  363.   return (-32768 <= num) && (num <= 32767);
  364. }                /* fits_in_signed_word() */
  365.  
  366. static int
  367. smallest_imm_type (num)
  368.      long num;
  369. {
  370. #if 0
  371.   /* This code is disabled because all the Imm1 forms in the opcode table
  372.      are slower on the i486, and they're the versions with the implicitly
  373.      specified single-position displacement, which has another syntax if
  374.      you really want to use that form.  If you really prefer to have the
  375.      one-byte-shorter Imm1 form despite these problems, re-enable this
  376.      code.  */
  377.   if (num == 1)
  378.     return Imm1 | Imm8 | Imm8S | Imm16 | Imm32;
  379. #endif
  380.   return (fits_in_signed_byte (num)
  381.       ? (Imm8S | Imm8 | Imm16 | Imm32)
  382.       : fits_in_unsigned_byte (num)
  383.       ? (Imm8 | Imm16 | Imm32)
  384.       : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
  385.       ? (Imm16 | Imm32)
  386.       : (Imm32));
  387. }                /* smallest_imm_type() */
  388.  
  389. void set_16bit_code_flag(new_16bit_code_flag)
  390.     int new_16bit_code_flag;
  391. {
  392.   flag_16bit_code = new_16bit_code_flag;
  393. }
  394.  
  395. const pseudo_typeS md_pseudo_table[] =
  396. {
  397. #ifndef I386COFF
  398.   {"bss", s_bss, 0},
  399. #endif
  400. #ifndef OBJ_AOUT
  401.   {"align", s_align_bytes, 0},
  402. #else
  403.   {"align", s_align_ptwo, 0},
  404. #endif
  405.   {"ffloat", float_cons, 'f'},
  406.   {"dfloat", float_cons, 'd'},
  407.   {"tfloat", float_cons, 'x'},
  408.   {"value", cons, 2},
  409.   {"noopt", s_ignore, 0},
  410.   {"optim", s_ignore, 0},
  411.   {"code16", set_16bit_code_flag, 1},
  412.   {"code32", set_16bit_code_flag, 0},
  413.   {0, 0, 0}
  414. };
  415.  
  416. /* for interface with expression () */
  417. extern char *input_line_pointer;
  418.  
  419. /* obstack for constructing various things in md_begin */
  420. struct obstack o;
  421.  
  422. /* hash table for opcode lookup */
  423. static struct hash_control *op_hash;
  424. /* hash table for register lookup */
  425. static struct hash_control *reg_hash;
  426. /* hash table for prefix lookup */
  427. static struct hash_control *prefix_hash;
  428.  
  429.  
  430. void
  431. md_begin ()
  432. {
  433.   const char *hash_err;
  434.  
  435.   obstack_begin (&o, 4096);
  436.  
  437.   /* initialize op_hash hash table */
  438.   op_hash = hash_new ();
  439.  
  440.   {
  441.     register const template *optab;
  442.     register templates *core_optab;
  443.     char *prev_name;
  444.  
  445.     optab = i386_optab;        /* setup for loop */
  446.     prev_name = optab->name;
  447.     obstack_grow (&o, optab, sizeof (template));
  448.     core_optab = (templates *) xmalloc (sizeof (templates));
  449.  
  450.     for (optab++; optab < i386_optab_end; optab++)
  451.       {
  452.     if (!strcmp (optab->name, prev_name))
  453.       {
  454.         /* same name as before --> append to current template list */
  455.         obstack_grow (&o, optab, sizeof (template));
  456.       }
  457.     else
  458.       {
  459.         /* different name --> ship out current template list;
  460.            add to hash table; & begin anew */
  461.         /* Note: end must be set before start! since obstack_next_free
  462.            changes upon opstack_finish */
  463.         core_optab->end = (template *) obstack_next_free (&o);
  464.         core_optab->start = (template *) obstack_finish (&o);
  465.         hash_err = hash_insert (op_hash, prev_name, (char *) core_optab);
  466.         if (hash_err)
  467.           {
  468.           hash_error:
  469.         as_fatal ("Internal Error:  Can't hash %s: %s", prev_name,
  470.               hash_err);
  471.           }
  472.         prev_name = optab->name;
  473.         core_optab = (templates *) xmalloc (sizeof (templates));
  474.         obstack_grow (&o, optab, sizeof (template));
  475.       }
  476.       }
  477.   }
  478.  
  479.   /* initialize reg_hash hash table */
  480.   reg_hash = hash_new ();
  481.   {
  482.     register const reg_entry *regtab;
  483.  
  484.     for (regtab = i386_regtab; regtab < i386_regtab_end; regtab++)
  485.       {
  486.     hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
  487.     if (hash_err)
  488.       goto hash_error;
  489.       }
  490.   }
  491.  
  492.   esp = (reg_entry *) hash_find (reg_hash, "esp");
  493.   ebp = (reg_entry *) hash_find (reg_hash, "ebp");
  494.  
  495.   /* initialize reg_hash hash table */
  496.   prefix_hash = hash_new ();
  497.   {
  498.     register const prefix_entry *prefixtab;
  499.  
  500.     for (prefixtab = i386_prefixtab;
  501.      prefixtab < i386_prefixtab_end; prefixtab++)
  502.       {
  503.     hash_err = hash_insert (prefix_hash, prefixtab->prefix_name,
  504.                 (PTR) prefixtab);
  505.     if (hash_err)
  506.       goto hash_error;
  507.       }
  508.   }
  509.  
  510.   /* fill in lexical tables:  opcode_chars, operand_chars, space_chars */
  511.   {
  512.     register int c;
  513.     register char *p;
  514.  
  515.     for (c = 0; c < 256; c++)
  516.       {
  517.     if (islower (c) || isdigit (c))
  518.       {
  519.         opcode_chars[c] = c;
  520.         register_chars[c] = c;
  521.       }
  522.     else if (isupper (c))
  523.       {
  524.         opcode_chars[c] = tolower (c);
  525.         register_chars[c] = opcode_chars[c];
  526.       }
  527.     else if (c == PREFIX_SEPERATOR)
  528.       {
  529.         opcode_chars[c] = c;
  530.       }
  531.     else if (c == ')' || c == '(')
  532.       {
  533.         register_chars[c] = c;
  534.       }
  535.  
  536.     if (isupper (c) || islower (c) || isdigit (c))
  537.       operand_chars[c] = c;
  538.  
  539.     if (isdigit (c) || c == '-')
  540.       digit_chars[c] = c;
  541.  
  542.     if (isalpha (c) || c == '_' || c == '.' || isdigit (c))
  543.       identifier_chars[c] = c;
  544.  
  545. #ifdef LEX_AT
  546.     identifier_chars['@'] = '@';
  547. #endif
  548.  
  549.     if (c == ' ' || c == '\t')
  550.       space_chars[c] = c;
  551.       }
  552.  
  553.     for (p = operand_special_chars; *p != '\0'; p++)
  554.       operand_chars[(unsigned char) *p] = *p;
  555.   }
  556.  
  557. #ifdef OBJ_ELF
  558.   record_alignment (text_section, 2);
  559.   record_alignment (data_section, 2);
  560.   record_alignment (bss_section, 2);
  561. #endif
  562. }
  563.  
  564. void
  565. i386_print_statistics (file)
  566.      FILE *file;
  567. {
  568.   hash_print_statistics (file, "i386 opcode", op_hash);
  569.   hash_print_statistics (file, "i386 register", reg_hash);
  570.   hash_print_statistics (file, "i386 prefix", prefix_hash);
  571. }
  572.  
  573.  
  574. #ifdef DEBUG386
  575.  
  576. /* debugging routines for md_assemble */
  577. static void pi PARAMS ((char *, i386_insn *));
  578. static void pte PARAMS ((template *));
  579. static void pt PARAMS ((unsigned int));
  580. static void pe PARAMS ((expressionS *));
  581. static void ps PARAMS ((symbolS *));
  582.  
  583. static void
  584. pi (line, x)
  585.      char *line;
  586.      i386_insn *x;
  587. {
  588.   register template *p;
  589.   int i;
  590.  
  591.   fprintf (stdout, "%s: template ", line);
  592.   pte (&x->tm);
  593.   fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x",
  594.        x->rm.mode, x->rm.reg, x->rm.regmem);
  595.   fprintf (stdout, " base %x  index %x  scale %x\n",
  596.        x->bi.base, x->bi.index, x->bi.scale);
  597.   for (i = 0; i < x->operands; i++)
  598.     {
  599.       fprintf (stdout, "    #%d:  ", i + 1);
  600.       pt (x->types[i]);
  601.       fprintf (stdout, "\n");
  602.       if (x->types[i] & Reg)
  603.     fprintf (stdout, "%s\n", x->regs[i]->reg_name);
  604.       if (x->types[i] & Imm)
  605.     pe (x->imms[i]);
  606.       if (x->types[i] & (Disp | Abs))
  607.     pe (x->disps[i]);
  608.     }
  609. }
  610.  
  611. static void
  612. pte (t)
  613.      template *t;
  614. {
  615.   int i;
  616.   fprintf (stdout, " %d operands ", t->operands);
  617.   fprintf (stdout, "opcode %x ",
  618.        t->base_opcode);
  619.   if (t->extension_opcode != None)
  620.     fprintf (stdout, "ext %x ", t->extension_opcode);
  621.   if (t->opcode_modifier & D)
  622.     fprintf (stdout, "D");
  623.   if (t->opcode_modifier & W)
  624.     fprintf (stdout, "W");
  625.   fprintf (stdout, "\n");
  626.   for (i = 0; i < t->operands; i++)
  627.     {
  628.       fprintf (stdout, "    #%d type ", i + 1);
  629.       pt (t->operand_types[i]);
  630.       fprintf (stdout, "\n");
  631.     }
  632. }
  633.  
  634. static void
  635. pe (e)
  636.      expressionS *e;
  637. {
  638.   fprintf (stdout, "    operation       %d\n", e->X_op);
  639.   fprintf (stdout, "    add_number    %d (%x)\n",
  640.        e->X_add_number, e->X_add_number);
  641.   if (e->X_add_symbol)
  642.     {
  643.       fprintf (stdout, "    add_symbol    ");
  644.       ps (e->X_add_symbol);
  645.       fprintf (stdout, "\n");
  646.     }
  647.   if (e->X_op_symbol)
  648.     {
  649.       fprintf (stdout, "    op_symbol    ");
  650.       ps (e->X_op_symbol);
  651.       fprintf (stdout, "\n");
  652.     }
  653. }
  654.  
  655. static void
  656. ps (s)
  657.      symbolS *s;
  658. {
  659.   fprintf (stdout, "%s type %s%s",
  660.        S_GET_NAME (s),
  661.        S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
  662.        segment_name (S_GET_SEGMENT (s)));
  663. }
  664.  
  665. struct type_name
  666.   {
  667.     unsigned int mask;
  668.     char *tname;
  669.   }
  670.  
  671. type_names[] =
  672. {
  673.   { Reg8, "r8" },
  674.   { Reg16, "r16" },
  675.   { Reg32, "r32" },
  676.   { Imm8, "i8" },
  677.   { Imm8S, "i8s" },
  678.   { Imm16, "i16" },
  679.   { Imm32, "i32" },
  680.   { Mem8, "Mem8" },
  681.   { Mem16, "Mem16" },
  682.   { Mem32, "Mem32" },
  683.   { BaseIndex, "BaseIndex" },
  684.   { Abs8, "Abs8" },
  685.   { Abs16, "Abs16" },
  686.   { Abs32, "Abs32" },
  687.   { Disp8, "d8" },
  688.   { Disp16, "d16" },
  689.   { Disp32, "d32" },
  690.   { SReg2, "SReg2" },
  691.   { SReg3, "SReg3" },
  692.   { Acc, "Acc" },
  693.   { InOutPortReg, "InOutPortReg" },
  694.   { ShiftCount, "ShiftCount" },
  695.   { Imm1, "i1" },
  696.   { Control, "control reg" },
  697.   { Test, "test reg" },
  698.   { FloatReg, "FReg" },
  699.   { FloatAcc, "FAcc" },
  700.   { JumpAbsolute, "Jump Absolute" },
  701.   { 0, "" }
  702. };
  703.  
  704. static void
  705. pt (t)
  706.      unsigned int t;
  707. {
  708.   register struct type_name *ty;
  709.  
  710.   if (t == Unknown)
  711.     {
  712.       fprintf (stdout, "Unknown");
  713.     }
  714.   else
  715.     {
  716.       for (ty = type_names; ty->mask; ty++)
  717.     if (t & ty->mask)
  718.       fprintf (stdout, "%s, ", ty->tname);
  719.     }
  720.   fflush (stdout);
  721. }
  722.  
  723. #endif /* DEBUG386 */
  724.  
  725. #ifdef BFD_ASSEMBLER
  726. static bfd_reloc_code_real_type
  727. reloc (size, pcrel, other)
  728.      int size;
  729.      int pcrel;
  730.      bfd_reloc_code_real_type other;
  731. {
  732.   if (other != NO_RELOC) return other;
  733.  
  734.   if (pcrel)
  735.     switch (size)
  736.       {
  737.       case 1: return BFD_RELOC_8_PCREL;
  738.       case 2: return BFD_RELOC_16_PCREL;
  739.       case 4: return BFD_RELOC_32_PCREL;
  740.       }
  741.   else
  742.     switch (size)
  743.       {
  744.       case 1: return BFD_RELOC_8;
  745.       case 2: return BFD_RELOC_16;
  746.       case 4: return BFD_RELOC_32;
  747.       }
  748.  
  749.   as_bad ("Can not do %d byte %srelocation", size,
  750.       pcrel ? "pc-relative " : "");
  751.   return BFD_RELOC_NONE;
  752. }
  753. #else
  754. #define reloc(SIZE,PCREL,OTHER)    0
  755. #define BFD_RELOC_32        0
  756. #define BFD_RELOC_32_PCREL    0
  757. #define BFD_RELOC_386_PLT32    0
  758. #define BFD_RELOC_386_GOT32    0
  759. #define BFD_RELOC_386_GOTOFF    0
  760. #endif
  761.  
  762. /*
  763.  * Here we decide which fixups can be adjusted to make them relative to
  764.  * the beginning of the section instead of the symbol.  Basically we need
  765.  * to make sure that the dynamic relocations are done correctly, so in
  766.  * some cases we force the original symbol to be used.
  767.  */
  768. int
  769. tc_i386_fix_adjustable(fixP)
  770.      fixS * fixP;
  771. {
  772. #ifndef OBJ_AOUT
  773.   /* Prevent all adjustments to global symbols. */
  774.   if (S_IS_EXTERN (fixP->fx_addsy))
  775.     return 0;
  776. #endif
  777. #ifdef BFD_ASSEMBLER
  778.   /* adjust_reloc_syms doesn't know about the GOT */
  779.   if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
  780.       || fixP->fx_r_type == BFD_RELOC_386_PLT32
  781.       || fixP->fx_r_type == BFD_RELOC_386_GOT32)
  782.     return 0;
  783. #endif
  784.   return 1;
  785. }
  786.  
  787. /* This is the guts of the machine-dependent assembler.  LINE points to a
  788.    machine dependent instruction.  This function is supposed to emit
  789.    the frags/bytes it assembles to.  */
  790.  
  791. void
  792. md_assemble (line)
  793.      char *line;
  794. {
  795.   /* Holds template once we've found it. */
  796.   template *t;
  797.  
  798.   /* Count the size of the instruction generated.  */
  799.   int insn_size = 0;
  800.  
  801.   /* Possible templates for current insn */
  802.   templates *current_templates = (templates *) 0;
  803.  
  804.   int j;
  805.  
  806.   /* Initialize globals. */
  807.   memset (&i, '\0', sizeof (i));
  808.   for (j = 0; j < MAX_OPERANDS; j++)
  809.     i.disp_reloc[j] = NO_RELOC;
  810.   memset (disp_expressions, '\0', sizeof (disp_expressions));
  811.   memset (im_expressions, '\0', sizeof (im_expressions));
  812.   save_stack_p = save_stack;    /* reset stack pointer */
  813.  
  814.   /* Fist parse an opcode & call i386_operand for the operands.
  815.      We assume that the scrubber has arranged it so that line[0] is the valid
  816.      start of a (possibly prefixed) opcode. */
  817.   {
  818.     char *l = line;
  819.  
  820.     /* 1 if operand is pending after ','. */
  821.     unsigned int expecting_operand = 0;
  822.     /* 1 if we found a prefix only acceptable with string insns. */
  823.     unsigned int expecting_string_instruction = 0;
  824.     /* Non-zero if operand parens not balenced. */
  825.     unsigned int paren_not_balenced;
  826.     char *token_start = l;
  827.  
  828.     while (!is_space_char (*l) && *l != END_OF_INSN)
  829.       {
  830.     if (!is_opcode_char (*l))
  831.       {
  832.         as_bad ("invalid character %s in opcode", output_invalid (*l));
  833.         return;
  834.       }
  835.     else if (*l != PREFIX_SEPERATOR)
  836.       {
  837.         *l = opcode_chars[(unsigned char) *l];    /* fold case of opcodes */
  838.         l++;
  839.       }
  840.     else
  841.       {
  842.         /* This opcode's got a prefix.  */
  843.         unsigned int q;
  844.         prefix_entry *prefix;
  845.  
  846.         if (l == token_start)
  847.           {
  848.         as_bad ("expecting prefix; got nothing");
  849.         return;
  850.           }
  851.         END_STRING_AND_SAVE (l);
  852.         prefix = (prefix_entry *) hash_find (prefix_hash, token_start);
  853.         if (!prefix)
  854.           {
  855.         as_bad ("no such opcode prefix ('%s')", token_start);
  856.         return;
  857.           }
  858.         RESTORE_END_STRING (l);
  859.         /* check for repeated prefix */
  860.         for (q = 0; q < i.prefixes; q++)
  861.           if (i.prefix[q] == prefix->prefix_code)
  862.         {
  863.           as_bad ("same prefix used twice; you don't really want this!");
  864.           return;
  865.         }
  866.         if (i.prefixes == MAX_PREFIXES)
  867.           {
  868.         as_bad ("too many opcode prefixes");
  869.         return;
  870.           }
  871.         i.prefix[i.prefixes++] = prefix->prefix_code;
  872.         if (prefix->prefix_code == REPE || prefix->prefix_code == REPNE)
  873.           expecting_string_instruction = 1;
  874.         /* skip past PREFIX_SEPERATOR and reset token_start */
  875.         token_start = ++l;
  876.       }
  877.       }
  878.     END_STRING_AND_SAVE (l);
  879.     if (token_start == l)
  880.       {
  881.     as_bad ("expecting opcode; got nothing");
  882.     return;
  883.       }
  884.  
  885.     /* Lookup insn in hash; try intel & att naming conventions if appropriate;
  886.        that is:  we only use the opcode suffix 'b' 'w' or 'l' if we need to. */
  887.     current_templates = (templates *) hash_find (op_hash, token_start);
  888.     if (!current_templates)
  889.       {
  890.     int last_index = strlen (token_start) - 1;
  891.     char last_char = token_start[last_index];
  892.     switch (last_char)
  893.       {
  894.       case DWORD_OPCODE_SUFFIX:
  895.       case WORD_OPCODE_SUFFIX:
  896.       case BYTE_OPCODE_SUFFIX:
  897.         token_start[last_index] = '\0';
  898.         current_templates = (templates *) hash_find (op_hash, token_start);
  899.         token_start[last_index] = last_char;
  900.         i.suffix = last_char;
  901.       }
  902.     if (!current_templates)
  903.       {
  904.         as_bad ("no such 386 instruction: `%s'", token_start);
  905.         return;
  906.       }
  907.       }
  908.     RESTORE_END_STRING (l);
  909.  
  910.     /* check for rep/repne without a string instruction */
  911.     if (expecting_string_instruction &&
  912.     !IS_STRING_INSTRUCTION (current_templates->
  913.                 start->base_opcode))
  914.       {
  915.     as_bad ("expecting string instruction after rep/repne");
  916.     return;
  917.       }
  918.  
  919.     /* There may be operands to parse. */
  920.     if (*l != END_OF_INSN &&
  921.     /* For string instructions, we ignore any operands if given.  This
  922.        kludges, for example, 'rep/movsb %ds:(%esi), %es:(%edi)' where
  923.        the operands are always going to be the same, and are not really
  924.        encoded in machine code. */
  925.     !IS_STRING_INSTRUCTION (current_templates->
  926.                 start->base_opcode))
  927.       {
  928.     /* parse operands */
  929.     do
  930.       {
  931.         /* skip optional white space before operand */
  932.         while (!is_operand_char (*l) && *l != END_OF_INSN)
  933.           {
  934.         if (!is_space_char (*l))
  935.           {
  936.             as_bad ("invalid character %s before %s operand",
  937.                 output_invalid (*l),
  938.                 ordinal_names[i.operands]);
  939.             return;
  940.           }
  941.         l++;
  942.           }
  943.         token_start = l;    /* after white space */
  944.         paren_not_balenced = 0;
  945.         while (paren_not_balenced || *l != ',')
  946.           {
  947.         if (*l == END_OF_INSN)
  948.           {
  949.             if (paren_not_balenced)
  950.               {
  951.             as_bad ("unbalenced parenthesis in %s operand.",
  952.                 ordinal_names[i.operands]);
  953.             return;
  954.               }
  955.             else
  956.               break;    /* we are done */
  957.           }
  958.         else if (!is_operand_char (*l) && !is_space_char (*l))
  959.           {
  960.             as_bad ("invalid character %s in %s operand",
  961.                 output_invalid (*l),
  962.                 ordinal_names[i.operands]);
  963.             return;
  964.           }
  965.         if (*l == '(')
  966.           ++paren_not_balenced;
  967.         if (*l == ')')
  968.           --paren_not_balenced;
  969.         l++;
  970.           }
  971.         if (l != token_start)
  972.           {            /* yes, we've read in another operand */
  973.         unsigned int operand_ok;
  974.         this_operand = i.operands++;
  975.         if (i.operands > MAX_OPERANDS)
  976.           {
  977.             as_bad ("spurious operands; (%d operands/instruction max)",
  978.                 MAX_OPERANDS);
  979.             return;
  980.           }
  981.         /* now parse operand adding info to 'i' as we go along */
  982.         END_STRING_AND_SAVE (l);
  983.         operand_ok = i386_operand (token_start);
  984.         RESTORE_END_STRING (l);    /* restore old contents */
  985.         if (!operand_ok)
  986.           return;
  987.           }
  988.         else
  989.           {
  990.         if (expecting_operand)
  991.           {
  992.           expecting_operand_after_comma:
  993.             as_bad ("expecting operand after ','; got nothing");
  994.             return;
  995.           }
  996.         if (*l == ',')
  997.           {
  998.             as_bad ("expecting operand before ','; got nothing");
  999.             return;
  1000.           }
  1001.           }
  1002.  
  1003.         /* now *l must be either ',' or END_OF_INSN */
  1004.         if (*l == ',')
  1005.           {
  1006.         if (*++l == END_OF_INSN)
  1007.           {        /* just skip it, if it's \n complain */
  1008.             goto expecting_operand_after_comma;
  1009.           }
  1010.         expecting_operand = 1;
  1011.           }
  1012.       }
  1013.     while (*l != END_OF_INSN);    /* until we get end of insn */
  1014.       }
  1015.   }
  1016.  
  1017.   /* Now we've parsed the opcode into a set of templates, and have the
  1018.      operands at hand.
  1019.  
  1020.      Next, we find a template that matches the given insn,
  1021.      making sure the overlap of the given operands types is consistent
  1022.      with the template operand types. */
  1023.  
  1024. #define MATCH(overlap,given_type) \
  1025.     (overlap && \
  1026.      (((overlap & (JumpAbsolute|BaseIndex|Mem8)) \
  1027.        == (given_type & (JumpAbsolute|BaseIndex|Mem8))) \
  1028.       || (overlap == InOutPortReg)))
  1029.  
  1030.  
  1031.   /* If m0 and m1 are register matches they must be consistent
  1032.      with the expected operand types t0 and t1.
  1033.      That is, if both m0 & m1 are register matches
  1034.      i.e. ( ((m0 & (Reg)) && (m1 & (Reg)) ) ?
  1035.      then, either 1. or 2. must be true:
  1036.      1. the expected operand type register overlap is null:
  1037.      (t0 & t1 & Reg) == 0
  1038.      AND
  1039.      the given register overlap is null:
  1040.      (m0 & m1 & Reg) == 0
  1041.      2. the expected operand type register overlap == the given
  1042.      operand type overlap:  (t0 & t1 & m0 & m1 & Reg).
  1043.      */
  1044. #define CONSISTENT_REGISTER_MATCH(m0, m1, t0, t1) \
  1045.         ( ((m0 & (Reg)) && (m1 & (Reg))) ? \
  1046.          ( ((t0 & t1 & (Reg)) == 0 && (m0 & m1 & (Reg)) == 0) || \
  1047.           ((t0 & t1) & (m0 & m1) & (Reg)) \
  1048.           ) : 1)
  1049.   {
  1050.     register unsigned int overlap0, overlap1;
  1051.     expressionS *exp;
  1052.     unsigned int overlap2;
  1053.     unsigned int found_reverse_match;
  1054.  
  1055.     overlap0 = overlap1 = overlap2 = found_reverse_match = 0;
  1056.     for (t = current_templates->start;
  1057.      t < current_templates->end;
  1058.      t++)
  1059.       {
  1060.     /* must have right number of operands */
  1061.     if (i.operands != t->operands)
  1062.       continue;
  1063.     else if (!t->operands)
  1064.       break;        /* 0 operands always matches */
  1065.  
  1066.     overlap0 = i.types[0] & t->operand_types[0];
  1067.     switch (t->operands)
  1068.       {
  1069.       case 1:
  1070.         if (!MATCH (overlap0, i.types[0]))
  1071.           continue;
  1072.         break;
  1073.       case 2:
  1074.       case 3:
  1075.         overlap1 = i.types[1] & t->operand_types[1];
  1076.         if (!MATCH (overlap0, i.types[0]) ||
  1077.         !MATCH (overlap1, i.types[1]) ||
  1078.         !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
  1079.                         t->operand_types[0],
  1080.                         t->operand_types[1]))
  1081.           {
  1082.  
  1083.         /* check if other direction is valid ... */
  1084.         if (!(t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS))
  1085.           continue;
  1086.  
  1087.         /* try reversing direction of operands */
  1088.         overlap0 = i.types[0] & t->operand_types[1];
  1089.         overlap1 = i.types[1] & t->operand_types[0];
  1090.         if (!MATCH (overlap0, i.types[0]) ||
  1091.             !MATCH (overlap1, i.types[1]) ||
  1092.             !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
  1093.                         t->operand_types[0],
  1094.                         t->operand_types[1]))
  1095.           {
  1096.             /* does not match either direction */
  1097.             continue;
  1098.           }
  1099.         /* found a reverse match here -- slip through */
  1100.         /* found_reverse_match holds which of D or FloatD we've found */
  1101.         found_reverse_match = t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS;
  1102.           }            /* endif: not forward match */
  1103.         /* found either forward/reverse 2 operand match here */
  1104.         if (t->operands == 3)
  1105.           {
  1106.         overlap2 = i.types[2] & t->operand_types[2];
  1107.         if (!MATCH (overlap2, i.types[2]) ||
  1108.             !CONSISTENT_REGISTER_MATCH (overlap0, overlap2,
  1109.                         t->operand_types[0],
  1110.                         t->operand_types[2]) ||
  1111.             !CONSISTENT_REGISTER_MATCH (overlap1, overlap2,
  1112.                         t->operand_types[1],
  1113.                         t->operand_types[2]))
  1114.           continue;
  1115.           }
  1116.         /* found either forward/reverse 2 or 3 operand match here:
  1117.            slip through to break */
  1118.       }
  1119.     break;            /* we've found a match; break out of loop */
  1120.       }                /* for (t = ... */
  1121.     if (t == current_templates->end)
  1122.       {                /* we found no match */
  1123.     as_bad ("operands given don't match any known 386 instruction");
  1124.     return;
  1125.       }
  1126.  
  1127.     /* Copy the template we found (we may change it!). */
  1128.     i.tm = *t;
  1129.     t = &i.tm;            /* alter new copy of template */
  1130.  
  1131.     /* If the matched instruction specifies an explicit opcode suffix,
  1132.        use it - and make sure none has already been specified.  */
  1133.     if (t->opcode_modifier & (Data16|Data32))
  1134.       {
  1135.     if (i.suffix)
  1136.       {
  1137.         as_bad ("extraneous opcode suffix given");
  1138.         return;
  1139.       }
  1140.     if (t->opcode_modifier & Data16)
  1141.       i.suffix = WORD_OPCODE_SUFFIX;
  1142.     else
  1143.       i.suffix = DWORD_OPCODE_SUFFIX;
  1144.       }
  1145.  
  1146.     /* If there's no opcode suffix we try to invent one based on register
  1147.        operands. */
  1148.     if (!i.suffix && i.reg_operands)
  1149.       {
  1150.     /* We take i.suffix from the LAST register operand specified.  This
  1151.        assumes that the last register operands is the destination register
  1152.        operand. */
  1153.     int op;
  1154.     for (op = 0; op < MAX_OPERANDS; op++)
  1155.       if (i.types[op] & Reg)
  1156.         {
  1157.           i.suffix = ((i.types[op] & Reg8) ? BYTE_OPCODE_SUFFIX :
  1158.               (i.types[op] & Reg16) ? WORD_OPCODE_SUFFIX :
  1159.               DWORD_OPCODE_SUFFIX);
  1160.         }
  1161.       }
  1162.     else if (i.suffix != 0
  1163.          && i.reg_operands != 0
  1164.          && (i.types[i.operands - 1] & Reg) != 0)
  1165.       {
  1166.     int bad;
  1167.  
  1168.     /* If the last operand is a register, make sure it is
  1169.            compatible with the suffix.  */
  1170.  
  1171.     bad = 0;
  1172.     switch (i.suffix)
  1173.       {
  1174.       default:
  1175.         abort ();
  1176.       case BYTE_OPCODE_SUFFIX:
  1177.         /* If this is an eight bit register, it's OK.  If it's the
  1178.                16 or 32 bit version of an eight bit register, we will
  1179.                just use the low portion, and that's OK too.  */
  1180.         if ((i.types[i.operands - 1] & Reg8) == 0
  1181.         && i.regs[i.operands - 1]->reg_num >= 4)
  1182.           bad = 1;
  1183.         break;
  1184.       case WORD_OPCODE_SUFFIX:
  1185.       case DWORD_OPCODE_SUFFIX:
  1186.         /* We don't insist on the presence or absence of the e
  1187.                prefix on the register, but we reject eight bit
  1188.                registers.  */
  1189.         if ((i.types[i.operands - 1] & Reg8) != 0)
  1190.           bad = 1;
  1191.       }
  1192.     if (bad)
  1193.       as_bad ("register does not match opcode suffix");
  1194.       }
  1195.  
  1196.     /* Make still unresolved immediate matches conform to size of immediate
  1197.        given in i.suffix. Note:  overlap2 cannot be an immediate!
  1198.        We assume this. */
  1199.     if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32))
  1200.     && overlap0 != Imm8 && overlap0 != Imm8S
  1201.     && overlap0 != Imm16 && overlap0 != Imm32)
  1202.       {
  1203.     if (!i.suffix)
  1204.       {
  1205.         as_bad ("no opcode suffix given; can't determine immediate size");
  1206.         return;
  1207.       }
  1208.     overlap0 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
  1209.              (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  1210.       }
  1211.     if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32))
  1212.     && overlap1 != Imm8 && overlap1 != Imm8S
  1213.     && overlap1 != Imm16 && overlap1 != Imm32)
  1214.       {
  1215.     if (!i.suffix)
  1216.       {
  1217.         as_bad ("no opcode suffix given; can't determine immediate size");
  1218.         return;
  1219.       }
  1220.     overlap1 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
  1221.              (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
  1222.       }
  1223.  
  1224.     i.types[0] = overlap0;
  1225.     i.types[1] = overlap1;
  1226.     i.types[2] = overlap2;
  1227.  
  1228.     if (overlap0 & ImplicitRegister)
  1229.       i.reg_operands--;
  1230.     if (overlap1 & ImplicitRegister)
  1231.       i.reg_operands--;
  1232.     if (overlap2 & ImplicitRegister)
  1233.       i.reg_operands--;
  1234.     if (overlap0 & Imm1)
  1235.       i.imm_operands = 0;    /* kludge for shift insns */
  1236.  
  1237.     if (found_reverse_match)
  1238.       {
  1239.     unsigned int save;
  1240.     save = t->operand_types[0];
  1241.     t->operand_types[0] = t->operand_types[1];
  1242.     t->operand_types[1] = save;
  1243.       }
  1244.  
  1245.     /* Finalize opcode.  First, we change the opcode based on the operand
  1246.        size given by i.suffix: we never have to change things for byte insns,
  1247.        or when no opcode suffix is need to size the operands. */
  1248.  
  1249.     if (!i.suffix && (t->opcode_modifier & W))
  1250.       {
  1251.     as_bad ("no opcode suffix given and no register operands; can't size instruction");
  1252.     return;
  1253.       }
  1254.  
  1255.     if (i.suffix && i.suffix != BYTE_OPCODE_SUFFIX)
  1256.       {
  1257.     /* Select between byte and word/dword operations. */
  1258.     if (t->opcode_modifier & W)
  1259.       t->base_opcode |= W;
  1260.     /* Now select between word & dword operations via the
  1261.                    operand size prefix. */
  1262.     if ((i.suffix == WORD_OPCODE_SUFFIX) ^ flag_16bit_code)
  1263.       {
  1264.         if (i.prefixes == MAX_PREFIXES)
  1265.           {
  1266.         as_bad ("%d prefixes given and 'w' opcode suffix gives too many prefixes",
  1267.             MAX_PREFIXES);
  1268.         return;
  1269.           }
  1270.         i.prefix[i.prefixes++] = WORD_PREFIX_OPCODE;
  1271.       }
  1272.       }
  1273.  
  1274.     /* For insns with operands there are more diddles to do to the opcode. */
  1275.     if (i.operands)
  1276.       {
  1277.         /* Default segment register this instruction will use
  1278.        for memory accesses.  0 means unknown.
  1279.        This is only for optimizing out unnecessary segment overrides.  */
  1280.     const seg_entry *default_seg = 0;
  1281.  
  1282.     /* True if this instruction uses a memory addressing mode,
  1283.        and therefore may need an address-size prefix.  */
  1284.     int uses_mem_addrmode = 0;
  1285.  
  1286.  
  1287.     /* If we found a reverse match we must alter the opcode direction bit
  1288.        found_reverse_match holds bit to set (different for int &
  1289.        float insns). */
  1290.  
  1291.     if (found_reverse_match)
  1292.       {
  1293.         t->base_opcode |= found_reverse_match;
  1294.       }
  1295.  
  1296.     /* The imul $imm, %reg instruction is converted into
  1297.        imul $imm, %reg, %reg. */
  1298.     if (t->opcode_modifier & imulKludge)
  1299.       {
  1300.         /* Pretend we saw the 3 operand case. */
  1301.         i.regs[2] = i.regs[1];
  1302.         i.reg_operands = 2;
  1303.       }
  1304.  
  1305.     /* Certain instructions expect the destination to be in the i.rm.reg
  1306.        field.  This is by far the exceptional case.  For these
  1307.        instructions, if the source operand is a register, we must reverse
  1308.        the i.rm.reg and i.rm.regmem fields.  We accomplish this by faking
  1309.        that the two register operands were given in the reverse order. */
  1310.     if ((t->opcode_modifier & ReverseRegRegmem) && i.reg_operands == 2)
  1311.       {
  1312.         unsigned int first_reg_operand = (i.types[0] & Reg) ? 0 : 1;
  1313.         unsigned int second_reg_operand = first_reg_operand + 1;
  1314.         reg_entry *tmp = i.regs[first_reg_operand];
  1315.         i.regs[first_reg_operand] = i.regs[second_reg_operand];
  1316.         i.regs[second_reg_operand] = tmp;
  1317.       }
  1318.  
  1319.     if (t->opcode_modifier & ShortForm)
  1320.       {
  1321.         /* The register or float register operand is in operand 0 or 1. */
  1322.         unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
  1323.         /* Register goes in low 3 bits of opcode. */
  1324.         t->base_opcode |= i.regs[op]->reg_num;
  1325.       }
  1326.     else if (t->opcode_modifier & ShortFormW)
  1327.       {
  1328.         /* Short form with 0x8 width bit.  Register is always dest. operand */
  1329.         t->base_opcode |= i.regs[1]->reg_num;
  1330.         if (i.suffix == WORD_OPCODE_SUFFIX ||
  1331.         i.suffix == DWORD_OPCODE_SUFFIX)
  1332.           t->base_opcode |= 0x8;
  1333.       }
  1334.     else if (t->opcode_modifier & Seg2ShortForm)
  1335.       {
  1336.         if (t->base_opcode == POP_SEG_SHORT && i.regs[0]->reg_num == 1)
  1337.           {
  1338.         as_bad ("you can't 'pop cs' on the 386.");
  1339.         return;
  1340.           }
  1341.         t->base_opcode |= (i.regs[0]->reg_num << 3);
  1342.       }
  1343.     else if (t->opcode_modifier & Seg3ShortForm)
  1344.       {
  1345.         /* 'push %fs' is 0x0fa0; 'pop %fs' is 0x0fa1.
  1346.            'push %gs' is 0x0fa8; 'pop %fs' is 0x0fa9.
  1347.            So, only if i.regs[0]->reg_num == 5 (%gs) do we need
  1348.            to change the opcode. */
  1349.         if (i.regs[0]->reg_num == 5)
  1350.           t->base_opcode |= 0x08;
  1351.       }
  1352.     else if ((t->base_opcode & ~DW) == MOV_AX_DISP32)
  1353.       {
  1354.         /* This is a special non-modrm instruction
  1355.            that addresses memory with a 32-bit displacement mode anyway,
  1356.            and thus requires an address-size prefix if in 16-bit mode.  */
  1357.         uses_mem_addrmode = 1;
  1358.         default_seg = &ds;
  1359.       }
  1360.     else if (t->opcode_modifier & Modrm)
  1361.       {
  1362.         /* The opcode is completed (modulo t->extension_opcode which must
  1363.            be put into the modrm byte.
  1364.            Now, we make the modrm & index base bytes based on all the info
  1365.            we've collected. */
  1366.  
  1367.         /* i.reg_operands MUST be the number of real register operands;
  1368.            implicit registers do not count. */
  1369.         if (i.reg_operands == 2)
  1370.           {
  1371.         unsigned int source, dest;
  1372.         source = (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 : 1;
  1373.         dest = source + 1;
  1374.         i.rm.mode = 3;
  1375.         /* We must be careful to make sure that all
  1376.            segment/control/test/debug registers go into the i.rm.reg
  1377.            field (despite the whether they are source or destination
  1378.            operands). */
  1379.         if (i.regs[dest]->reg_type & (SReg2 | SReg3 | Control | Debug | Test))
  1380.           {
  1381.             i.rm.reg = i.regs[dest]->reg_num;
  1382.             i.rm.regmem = i.regs[source]->reg_num;
  1383.           }
  1384.         else
  1385.           {
  1386.             i.rm.reg = i.regs[source]->reg_num;
  1387.             i.rm.regmem = i.regs[dest]->reg_num;
  1388.           }
  1389.           }
  1390.         else
  1391.           {            /* if it's not 2 reg operands... */
  1392.         if (i.mem_operands)
  1393.           {
  1394.             unsigned int fake_zero_displacement = 0;
  1395.             unsigned int op = (i.types[0] & Mem) ? 0 : ((i.types[1] & Mem) ? 1 : 2);
  1396.  
  1397.             /* Encode memory operand into modrm byte and base index
  1398.                byte. */
  1399.  
  1400.             if (i.base_reg == esp && !i.index_reg)
  1401.               {
  1402.             /* <disp>(%esp) becomes two byte modrm with no index
  1403.                register. */
  1404.             i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  1405.             i.rm.mode = mode_from_disp_size (i.types[op]);
  1406.             i.bi.base = ESP_REG_NUM;
  1407.             i.bi.index = NO_INDEX_REGISTER;
  1408.             i.bi.scale = 0;    /* Must be zero! */
  1409.               }
  1410.             else if (i.base_reg == ebp && !i.index_reg)
  1411.               {
  1412.             if (!(i.types[op] & Disp))
  1413.               {
  1414.                 /* Must fake a zero byte displacement.  There is
  1415.                    no direct way to code '(%ebp)' directly. */
  1416.                 fake_zero_displacement = 1;
  1417.                 /* fake_zero_displacement code does not set this. */
  1418.                 i.types[op] |= Disp8;
  1419.               }
  1420.             i.rm.mode = mode_from_disp_size (i.types[op]);
  1421.             i.rm.regmem = EBP_REG_NUM;
  1422.               }
  1423.             else if (!i.base_reg && (i.types[op] & BaseIndex))
  1424.               {
  1425.             /* There are three cases here.
  1426.                Case 1:  '<32bit disp>(,1)' -- indirect absolute.
  1427.                (Same as cases 2 & 3 with NO index register)
  1428.                Case 2:  <32bit disp> (,<index>) -- no base register with disp
  1429.                Case 3:  (, <index>)       --- no base register;
  1430.                no disp (must add 32bit 0 disp). */
  1431.             i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  1432.             i.rm.mode = 0;    /* 32bit mode */
  1433.             i.bi.base = NO_BASE_REGISTER;
  1434.             i.types[op] &= ~Disp;
  1435.             i.types[op] |= Disp32;    /* Must be 32bit! */
  1436.             if (i.index_reg)
  1437.               {    /* case 2 or case 3 */
  1438.                 i.bi.index = i.index_reg->reg_num;
  1439.                 i.bi.scale = i.log2_scale_factor;
  1440.                 if (i.disp_operands == 0)
  1441.                   fake_zero_displacement = 1;    /* case 3 */
  1442.               }
  1443.             else
  1444.               {
  1445.                 i.bi.index = NO_INDEX_REGISTER;
  1446.                 i.bi.scale = 0;
  1447.               }
  1448.               }
  1449.             else if (i.disp_operands && !i.base_reg && !i.index_reg)
  1450.               {
  1451.             /* Operand is just <32bit disp> */
  1452.             i.rm.regmem = EBP_REG_NUM;
  1453.             i.rm.mode = 0;
  1454.             i.types[op] &= ~Disp;
  1455.             i.types[op] |= Disp32;
  1456.               }
  1457.             else
  1458.               {
  1459.             /* It's not a special case; rev'em up. */
  1460.             i.rm.regmem = i.base_reg->reg_num;
  1461.             i.rm.mode = mode_from_disp_size (i.types[op]);
  1462.             if (i.index_reg)
  1463.               {
  1464.                 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
  1465.                 i.bi.base = i.base_reg->reg_num;
  1466.                 i.bi.index = i.index_reg->reg_num;
  1467.                 i.bi.scale = i.log2_scale_factor;
  1468.                 if (i.base_reg == ebp && i.disp_operands == 0)
  1469.                   {    /* pace */
  1470.                 fake_zero_displacement = 1;
  1471.                 i.types[op] |= Disp8;
  1472.                 i.rm.mode = mode_from_disp_size (i.types[op]);
  1473.                   }
  1474.               }
  1475.               }
  1476.             if (fake_zero_displacement)
  1477.               {
  1478.             /* Fakes a zero displacement assuming that i.types[op]
  1479.                holds the correct displacement size. */
  1480.             exp = &disp_expressions[i.disp_operands++];
  1481.             i.disps[op] = exp;
  1482.             exp->X_op = O_constant;
  1483.             exp->X_add_number = 0;
  1484.             exp->X_add_symbol = (symbolS *) 0;
  1485.             exp->X_op_symbol = (symbolS *) 0;
  1486.               }
  1487.  
  1488.             /* Find the default segment for the memory operand.
  1489.                Used to optimize out explicit segment specifications.  */
  1490.             if (i.seg)
  1491.               {
  1492.             unsigned int seg_index;
  1493.  
  1494.             if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING)
  1495.               {
  1496.                 seg_index = (i.rm.mode << 3) | i.bi.base;
  1497.                 default_seg = two_byte_segment_defaults[seg_index];
  1498.               }
  1499.             else
  1500.               {
  1501.                 seg_index = (i.rm.mode << 3) | i.rm.regmem;
  1502.                 default_seg = one_byte_segment_defaults[seg_index];
  1503.               }
  1504.               }
  1505.           }
  1506.  
  1507.         /* Fill in i.rm.reg or i.rm.regmem field with register operand
  1508.            (if any) based on t->extension_opcode. Again, we must be
  1509.            careful to make sure that segment/control/debug/test
  1510.            registers are coded into the i.rm.reg field. */
  1511.         if (i.reg_operands)
  1512.           {
  1513.             unsigned int op =
  1514.             (i.types[0] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 0 :
  1515.             (i.types[1] & (Reg | SReg2 | SReg3 | Control | Debug | Test)) ? 1 : 2;
  1516.             /* If there is an extension opcode to put here, the
  1517.                register number must be put into the regmem field. */
  1518.             if (t->extension_opcode != None)
  1519.               i.rm.regmem = i.regs[op]->reg_num;
  1520.             else
  1521.               i.rm.reg = i.regs[op]->reg_num;
  1522.  
  1523.             /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
  1524.                we must set it to 3 to indicate this is a register
  1525.                operand int the regmem field */
  1526.             if (!i.mem_operands)
  1527.               i.rm.mode = 3;
  1528.           }
  1529.  
  1530.         /* Fill in i.rm.reg field with extension opcode (if any). */
  1531.         if (t->extension_opcode != None)
  1532.           i.rm.reg = t->extension_opcode;
  1533.           }
  1534.  
  1535.         if (i.rm.mode != 3)
  1536.           uses_mem_addrmode = 1;
  1537.       }
  1538.  
  1539.     /* GAS currently doesn't support 16-bit memory addressing modes at all,
  1540.        so if we're writing 16-bit code and using a memory addressing mode,
  1541.        always spew out an address size prefix.  */
  1542.     if (uses_mem_addrmode && flag_16bit_code)
  1543.       {
  1544.         if (i.prefixes == MAX_PREFIXES)
  1545.           {
  1546.             as_bad ("%d prefixes given and address size override gives too many prefixes",
  1547.                 MAX_PREFIXES);
  1548.             return;
  1549.           }
  1550.         i.prefix[i.prefixes++] = ADDR_PREFIX_OPCODE;
  1551.       }
  1552.  
  1553.     /* If a segment was explicitly specified,
  1554.        and the specified segment is not the default,
  1555.        use an opcode prefix to select it.
  1556.        If we never figured out what the default segment is,
  1557.        then default_seg will be zero at this point,
  1558.        and the specified segment prefix will always be used.  */
  1559.     if ((i.seg) && (i.seg != default_seg))
  1560.       {
  1561.         if (i.prefixes == MAX_PREFIXES)
  1562.           {
  1563.             as_bad ("%d prefixes given and %s segment override gives too many prefixes",
  1564.                 MAX_PREFIXES, i.seg->seg_name);
  1565.             return;
  1566.           }
  1567.         i.prefix[i.prefixes++] = i.seg->seg_prefix;
  1568.       }
  1569.       }
  1570.   }
  1571.  
  1572.   /* Handle conversion of 'int $3' --> special int3 insn. */
  1573.   if (t->base_opcode == INT_OPCODE && i.imms[0]->X_add_number == 3)
  1574.     {
  1575.       t->base_opcode = INT3_OPCODE;
  1576.       i.imm_operands = 0;
  1577.     }
  1578.  
  1579.   /* We are ready to output the insn. */
  1580.   {
  1581.     register char *p;
  1582.  
  1583.     /* Output jumps. */
  1584.     if (t->opcode_modifier & Jump)
  1585.       {
  1586.     unsigned long n = i.disps[0]->X_add_number;
  1587.  
  1588.     if (i.disps[0]->X_op == O_constant)
  1589.       {
  1590.         if (fits_in_signed_byte (n))
  1591.           {
  1592.         p = frag_more (2);
  1593.         insn_size += 2;
  1594.         p[0] = t->base_opcode;
  1595.         p[1] = n;
  1596.           }
  1597.         else
  1598.           {    /* It's an absolute word/dword displacement. */
  1599.  
  1600.             /* Use only 16-bit jumps for 16-bit code,
  1601.            because text segments are limited to 64K anyway;
  1602.                use only 32-bit jumps for 32-bit code,
  1603.            because they're faster.  */
  1604.         int jmp_size = flag_16bit_code ? 2 : 4;
  1605.               if (flag_16bit_code && !fits_in_signed_word (n))
  1606.           {
  1607.             as_bad ("16-bit jump out of range");
  1608.             return;
  1609.           }
  1610.  
  1611.         if (t->base_opcode == JUMP_PC_RELATIVE)
  1612.           {        /* pace */
  1613.             /* unconditional jump */
  1614.             p = frag_more (1 + jmp_size);
  1615.             insn_size += 1 + jmp_size;
  1616.             p[0] = (char) 0xe9;
  1617.             md_number_to_chars (&p[1], (valueT) n, jmp_size);
  1618.           }
  1619.         else
  1620.           {
  1621.             /* conditional jump */
  1622.             p = frag_more (2 + jmp_size);
  1623.             insn_size += 2 + jmp_size;
  1624.             p[0] = TWO_BYTE_OPCODE_ESCAPE;
  1625.             p[1] = t->base_opcode + 0x10;
  1626.             md_number_to_chars (&p[2], (valueT) n, jmp_size);
  1627.           }
  1628.           }
  1629.       }
  1630.     else
  1631.       {
  1632.         if (flag_16bit_code)
  1633.           {
  1634.             FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
  1635.         insn_size += 1;
  1636.           }
  1637.  
  1638.         /* It's a symbol; end frag & setup for relax.
  1639.            Make sure there are more than 6 chars left in the current frag;
  1640.            if not we'll have to start a new one. */
  1641.         frag_grow (7);
  1642.         p = frag_more (1);
  1643.         insn_size += 1;
  1644.         p[0] = t->base_opcode;
  1645.         frag_var (rs_machine_dependent,
  1646.               6,    /* 2 opcode/prefix + 4 displacement */
  1647.               1,
  1648.               ((unsigned char) *p == JUMP_PC_RELATIVE
  1649.                ? ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE)
  1650.                : ENCODE_RELAX_STATE (COND_JUMP, BYTE)),
  1651.               i.disps[0]->X_add_symbol,
  1652.               (long) n, p);
  1653.       }
  1654.       }
  1655.     else if (t->opcode_modifier & (JumpByte | JumpDword))
  1656.       {
  1657.     int size = (t->opcode_modifier & JumpByte) ? 1 : 4;
  1658.     unsigned long n = i.disps[0]->X_add_number;
  1659.     unsigned char *q;
  1660.  
  1661.     /* The jcx/jecx instruction might need a data size prefix.  */
  1662.     for (q = i.prefix; q < i.prefix + i.prefixes; q++)
  1663.       {
  1664.         if (*q == WORD_PREFIX_OPCODE)
  1665.           {
  1666.             FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
  1667.             insn_size += 1;
  1668.         break;
  1669.           }
  1670.       }
  1671.  
  1672.     if ((size == 4) && (flag_16bit_code))
  1673.       {
  1674.         FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
  1675.         insn_size += 1;
  1676.       }
  1677.  
  1678.     if (fits_in_unsigned_byte (t->base_opcode))
  1679.       {
  1680.         FRAG_APPEND_1_CHAR (t->base_opcode);
  1681.         insn_size += 1;
  1682.       }
  1683.     else
  1684.       {
  1685.         p = frag_more (2);    /* opcode can be at most two bytes */
  1686.         insn_size += 2;
  1687.         /* put out high byte first: can't use md_number_to_chars! */
  1688.         *p++ = (t->base_opcode >> 8) & 0xff;
  1689.         *p = t->base_opcode & 0xff;
  1690.       }
  1691.  
  1692.     p = frag_more (size);
  1693.     insn_size += size;
  1694.     if (i.disps[0]->X_op == O_constant)
  1695.       {
  1696.         md_number_to_chars (p, (valueT) n, size);
  1697.         if (size == 1 && !fits_in_signed_byte (n))
  1698.           {
  1699.         as_bad ("loop/jecx only takes byte displacement; %lu shortened to %d",
  1700.             n, *p);
  1701.           }
  1702.       }
  1703.     else
  1704.       {
  1705.         fix_new_exp (frag_now, p - frag_now->fr_literal, size,
  1706.              i.disps[0], 1, reloc (size, 1, i.disp_reloc[0]));
  1707.  
  1708.       }
  1709.       }
  1710.     else if (t->opcode_modifier & JumpInterSegment)
  1711.       {
  1712.     if (flag_16bit_code)
  1713.       {
  1714.         FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
  1715.         insn_size += 1;
  1716.       }
  1717.  
  1718.     p = frag_more (1 + 2 + 4);    /* 1 opcode; 2 segment; 4 offset */
  1719.     insn_size += 1 + 2 + 4;
  1720.     p[0] = t->base_opcode;
  1721.     if (i.imms[1]->X_op == O_constant)
  1722.       md_number_to_chars (p + 1, (valueT) i.imms[1]->X_add_number, 4);
  1723.     else
  1724.       fix_new_exp (frag_now, p + 1 - frag_now->fr_literal, 4,
  1725.                i.imms[1], 0, BFD_RELOC_32);
  1726.     if (i.imms[0]->X_op != O_constant)
  1727.       as_bad ("can't handle non absolute segment in long call/jmp");
  1728.     md_number_to_chars (p + 5, (valueT) i.imms[0]->X_add_number, 2);
  1729.       }
  1730.     else
  1731.       {
  1732.     /* Output normal instructions here. */
  1733.     unsigned char *q;
  1734.  
  1735.     /* First the prefix bytes. */
  1736.     for (q = i.prefix; q < i.prefix + i.prefixes; q++)
  1737.       {
  1738.         p = frag_more (1);
  1739.         insn_size += 1;
  1740.         md_number_to_chars (p, (valueT) *q, 1);
  1741.       }
  1742.  
  1743.     /* Now the opcode; be careful about word order here! */
  1744.     if (fits_in_unsigned_byte (t->base_opcode))
  1745.       {
  1746.         FRAG_APPEND_1_CHAR (t->base_opcode);
  1747.         insn_size += 1;
  1748.       }
  1749.     else if (fits_in_unsigned_word (t->base_opcode))
  1750.       {
  1751.         p = frag_more (2);
  1752.         insn_size += 2;
  1753.         /* put out high byte first: can't use md_number_to_chars! */
  1754.         *p++ = (t->base_opcode >> 8) & 0xff;
  1755.         *p = t->base_opcode & 0xff;
  1756.       }
  1757.     else
  1758.       {            /* opcode is either 3 or 4 bytes */
  1759.         if (t->base_opcode & 0xff000000)
  1760.           {
  1761.         p = frag_more (4);
  1762.         insn_size += 4;
  1763.         *p++ = (t->base_opcode >> 24) & 0xff;
  1764.           }
  1765.         else
  1766.           {
  1767.         p = frag_more (3);
  1768.         insn_size += 3;
  1769.           }
  1770.         *p++ = (t->base_opcode >> 16) & 0xff;
  1771.         *p++ = (t->base_opcode >> 8) & 0xff;
  1772.         *p = (t->base_opcode) & 0xff;
  1773.       }
  1774.  
  1775.     /* Now the modrm byte and base index byte (if present). */
  1776.     if (t->opcode_modifier & Modrm)
  1777.       {
  1778.         p = frag_more (1);
  1779.         insn_size += 1;
  1780.         /* md_number_to_chars (p, i.rm, 1); */
  1781.         md_number_to_chars (p,
  1782.                 (valueT) (i.rm.regmem << 0
  1783.                       | i.rm.reg << 3
  1784.                       | i.rm.mode << 6),
  1785.                 1);
  1786.         /* If i.rm.regmem == ESP (4) && i.rm.mode != Mode 3 (Register mode)
  1787.                    ==> need second modrm byte. */
  1788.         if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING && i.rm.mode != 3)
  1789.           {
  1790.         p = frag_more (1);
  1791.         insn_size += 1;
  1792.         /* md_number_to_chars (p, i.bi, 1); */
  1793.         md_number_to_chars (p, (valueT) (i.bi.base << 0
  1794.                          | i.bi.index << 3
  1795.                          | i.bi.scale << 6),
  1796.                     1);
  1797.           }
  1798.       }
  1799.  
  1800.     if (i.disp_operands)
  1801.       {
  1802.         register unsigned int n;
  1803.  
  1804.         for (n = 0; n < i.operands; n++)
  1805.           {
  1806.         if (i.disps[n])
  1807.           {
  1808.             if (i.disps[n]->X_op == O_constant)
  1809.               {
  1810.             if (i.types[n] & (Disp8 | Abs8))
  1811.               {
  1812.                 p = frag_more (1);
  1813.                 insn_size += 1;
  1814.                 md_number_to_chars (p,
  1815.                         (valueT) i.disps[n]->X_add_number,
  1816.                         1);
  1817.               }
  1818.             else if (i.types[n] & (Disp16 | Abs16))
  1819.               {
  1820.                 p = frag_more (2);
  1821.                 insn_size += 2;
  1822.                 md_number_to_chars (p,
  1823.                         (valueT) i.disps[n]->X_add_number,
  1824.                         2);
  1825.               }
  1826.             else
  1827.               {    /* Disp32|Abs32 */
  1828.                 p = frag_more (4);
  1829.                 insn_size += 4;
  1830.                 md_number_to_chars (p,
  1831.                         (valueT) i.disps[n]->X_add_number,
  1832.                         4);
  1833.               }
  1834.               }
  1835.             else
  1836.               {        /* not absolute_section */
  1837.             /* need a 32-bit fixup (don't support 8bit non-absolute disps) */
  1838.             p = frag_more (4);
  1839.             insn_size += 4;
  1840.             fix_new_exp (frag_now, p - frag_now->fr_literal, 4,
  1841.                         i.disps[n], 0, 
  1842.                         TC_RELOC(i.disp_reloc[n], BFD_RELOC_32));
  1843.               }
  1844.           }
  1845.           }
  1846.       }            /* end displacement output */
  1847.  
  1848.     /* output immediate */
  1849.     if (i.imm_operands)
  1850.       {
  1851.         register unsigned int n;
  1852.  
  1853.         for (n = 0; n < i.operands; n++)
  1854.           {
  1855.         if (i.imms[n])
  1856.           {
  1857.             if (i.imms[n]->X_op == O_constant)
  1858.               {
  1859.             if (i.types[n] & (Imm8 | Imm8S))
  1860.               {
  1861.                 p = frag_more (1);
  1862.                 insn_size += 1;
  1863.                 md_number_to_chars (p,
  1864.                         (valueT) i.imms[n]->X_add_number,
  1865.                         1);
  1866.               }
  1867.             else if (i.types[n] & Imm16)
  1868.               {
  1869.                 p = frag_more (2);
  1870.                 insn_size += 2;
  1871.                 md_number_to_chars (p,
  1872.                         (valueT) i.imms[n]->X_add_number,
  1873.                         2);
  1874.               }
  1875.             else
  1876.               {
  1877.                 p = frag_more (4);
  1878.                 insn_size += 4;
  1879.                 md_number_to_chars (p,
  1880.                         (valueT) i.imms[n]->X_add_number,
  1881.                         4);
  1882.               }
  1883.               }
  1884.             else
  1885.               {        /* not absolute_section */
  1886.             /* Need a 32-bit fixup (don't support 8bit
  1887.                non-absolute ims).  Try to support other
  1888.                sizes ... */
  1889.             int r_type;
  1890.             int size;
  1891.             int pcrel = 0;
  1892.  
  1893.             if (i.types[n] & (Imm8 | Imm8S))
  1894.               size = 1;
  1895.             else if (i.types[n] & Imm16)
  1896.               size = 2;
  1897.             else
  1898.               size = 4;
  1899.             r_type = reloc (size, 0, i.disp_reloc[0]);
  1900.             p = frag_more (size);
  1901.             insn_size += size;
  1902. #ifdef BFD_ASSEMBLER
  1903.             if (r_type == BFD_RELOC_32
  1904.                 && GOT_symbol
  1905.                 && GOT_symbol == i.imms[n]->X_add_symbol
  1906.                 && (i.imms[n]->X_op == O_symbol
  1907.                 || (i.imms[n]->X_op == O_add
  1908.                     && (i.imms[n]->X_op_symbol->sy_value.X_op
  1909.                     == O_subtract))))
  1910.               {
  1911.                 r_type = BFD_RELOC_386_GOTPC;
  1912.                 i.imms[n]->X_add_number += 3;
  1913.               }
  1914. #endif
  1915.             fix_new_exp (frag_now, p - frag_now->fr_literal, size,
  1916.                      i.imms[n], pcrel, r_type);
  1917.               }
  1918.           }
  1919.           }
  1920.       }            /* end immediate output */
  1921.       }
  1922.  
  1923. #ifdef DEBUG386
  1924.     if (flag_debug)
  1925.       {
  1926.     pi (line, &i);
  1927.       }
  1928. #endif /* DEBUG386 */
  1929.   }
  1930. }
  1931.  
  1932. /* Parse OPERAND_STRING into the i386_insn structure I.  Returns non-zero
  1933.    on error. */
  1934.  
  1935. static int
  1936. i386_operand (operand_string)
  1937.      char *operand_string;
  1938. {
  1939.   register char *op_string = operand_string;
  1940.  
  1941.   /* Address of '\0' at end of operand_string. */
  1942.   char *end_of_operand_string = operand_string + strlen (operand_string);
  1943.  
  1944.   /* Start and end of displacement string expression (if found). */
  1945.   char *displacement_string_start = NULL;
  1946.   char *displacement_string_end = NULL;
  1947.  
  1948.   /* We check for an absolute prefix (differentiating,
  1949.      for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
  1950.   if (*op_string == ABSOLUTE_PREFIX)
  1951.     {
  1952.       op_string++;
  1953.       i.types[this_operand] |= JumpAbsolute;
  1954.     }
  1955.  
  1956.   /* Check if operand is a register. */
  1957.   if (*op_string == REGISTER_PREFIX)
  1958.     {
  1959.       register reg_entry *r;
  1960.       if (!(r = parse_register (op_string)))
  1961.     {
  1962.       as_bad ("bad register name ('%s')", op_string);
  1963.       return 0;
  1964.     }
  1965.       /* Check for segment override, rather than segment register by
  1966.      searching for ':' after %<x>s where <x> = s, c, d, e, f, g. */
  1967.       if ((r->reg_type & (SReg2 | SReg3)) && op_string[3] == ':')
  1968.     {
  1969.       switch (r->reg_num)
  1970.         {
  1971.         case 0:
  1972.           i.seg = (seg_entry *) & es;
  1973.           break;
  1974.         case 1:
  1975.           i.seg = (seg_entry *) & cs;
  1976.           break;
  1977.         case 2:
  1978.           i.seg = (seg_entry *) & ss;
  1979.           break;
  1980.         case 3:
  1981.           i.seg = (seg_entry *) & ds;
  1982.           break;
  1983.         case 4:
  1984.           i.seg = (seg_entry *) & fs;
  1985.           break;
  1986.         case 5:
  1987.           i.seg = (seg_entry *) & gs;
  1988.           break;
  1989.         }
  1990.       op_string += 4;    /* skip % <x> s : */
  1991.       operand_string = op_string;    /* Pretend given string starts here. */
  1992.       if (!is_digit_char (*op_string) && !is_identifier_char (*op_string)
  1993.           && *op_string != '(' && *op_string != ABSOLUTE_PREFIX)
  1994.         {
  1995.           as_bad ("bad memory operand after segment override");
  1996.           return 0;
  1997.         }
  1998.       /* Handle case of %es:*foo. */
  1999.       if (*op_string == ABSOLUTE_PREFIX)
  2000.         {
  2001.           op_string++;
  2002.           i.types[this_operand] |= JumpAbsolute;
  2003.         }
  2004.       goto do_memory_reference;
  2005.     }
  2006.       i.types[this_operand] |= r->reg_type;
  2007.       i.regs[this_operand] = r;
  2008.       i.reg_operands++;
  2009.     }
  2010.   else if (*op_string == IMMEDIATE_PREFIX)
  2011.     {                /* ... or an immediate */
  2012.       char *save_input_line_pointer;
  2013.       segT exp_seg = 0;
  2014.       expressionS *exp;
  2015.  
  2016.       if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
  2017.     {
  2018.       as_bad ("only 1 or 2 immediate operands are allowed");
  2019.       return 0;
  2020.     }
  2021.  
  2022.       exp = &im_expressions[i.imm_operands++];
  2023.       i.imms[this_operand] = exp;
  2024.       save_input_line_pointer = input_line_pointer;
  2025.       input_line_pointer = ++op_string;    /* must advance op_string! */
  2026.       SKIP_WHITESPACE ();
  2027.       exp_seg = expression (exp);
  2028.       input_line_pointer = save_input_line_pointer;
  2029.  
  2030.       if (exp->X_op == O_absent)
  2031.     {
  2032.       /* missing or bad expr becomes absolute 0 */
  2033.       as_bad ("missing or invalid immediate expression '%s' taken as 0",
  2034.           operand_string);
  2035.       exp->X_op = O_constant;
  2036.       exp->X_add_number = 0;
  2037.       exp->X_add_symbol = (symbolS *) 0;
  2038.       exp->X_op_symbol = (symbolS *) 0;
  2039.       i.types[this_operand] |= Imm;
  2040.     }
  2041.       else if (exp->X_op == O_constant)
  2042.     {
  2043.       i.types[this_operand] |=
  2044.         smallest_imm_type ((unsigned long) exp->X_add_number);
  2045.     }
  2046. #ifdef OBJ_AOUT
  2047.       else if (exp_seg != text_section
  2048.            && exp_seg != data_section
  2049.            && exp_seg != bss_section
  2050.            && exp_seg != undefined_section
  2051. #ifdef BFD_ASSEMBLER
  2052.            && ! bfd_is_com_section (exp_seg)
  2053. #endif
  2054.            )
  2055.     {
  2056.     seg_unimplemented:
  2057.       as_bad ("Unimplemented segment type %d in parse_operand", exp_seg);
  2058.       return 0;
  2059.     }
  2060. #endif
  2061.       else
  2062.     {
  2063.       /* this is an address ==> 32bit */
  2064.       i.types[this_operand] |= Imm32;
  2065.     }
  2066.       /* shorten this type of this operand if the instruction wants
  2067.        * fewer bits than are present in the immediate.  The bit field
  2068.        * code can put out 'andb $0xffffff, %al', for example.   pace
  2069.        * also 'movw $foo,(%eax)'
  2070.        */
  2071.       switch (i.suffix)
  2072.     {
  2073.     case WORD_OPCODE_SUFFIX:
  2074.       i.types[this_operand] |= Imm16;
  2075.       break;
  2076.     case BYTE_OPCODE_SUFFIX:
  2077.       i.types[this_operand] |= Imm16 | Imm8 | Imm8S;
  2078.       break;
  2079.     }
  2080.     }
  2081.   else if (is_digit_char (*op_string) || is_identifier_char (*op_string)
  2082.        || *op_string == '(')
  2083.     {
  2084.       /* This is a memory reference of some sort. */
  2085.       register char *base_string;
  2086.       unsigned int found_base_index_form;
  2087.  
  2088.     do_memory_reference:
  2089.       if (i.mem_operands == MAX_MEMORY_OPERANDS)
  2090.     {
  2091.       as_bad ("more than 1 memory reference in instruction");
  2092.       return 0;
  2093.     }
  2094.       i.mem_operands++;
  2095.  
  2096.       /* Determine type of memory operand from opcode_suffix;
  2097.            no opcode suffix implies general memory references. */
  2098.       switch (i.suffix)
  2099.     {
  2100.     case BYTE_OPCODE_SUFFIX:
  2101.       i.types[this_operand] |= Mem8;
  2102.       break;
  2103.     case WORD_OPCODE_SUFFIX:
  2104.       i.types[this_operand] |= Mem16;
  2105.       break;
  2106.     case DWORD_OPCODE_SUFFIX:
  2107.     default:
  2108.       i.types[this_operand] |= Mem32;
  2109.     }
  2110.  
  2111.       /* Check for base index form.  We detect the base index form by
  2112.      looking for an ')' at the end of the operand, searching
  2113.      for the '(' matching it, and finding a REGISTER_PREFIX or ','
  2114.      after it. */
  2115.       base_string = end_of_operand_string - 1;
  2116.       found_base_index_form = 0;
  2117.       if (*base_string == ')')
  2118.     {
  2119.       unsigned int parens_balenced = 1;
  2120.       /* We've already checked that the number of left & right ()'s are
  2121.          equal, so this loop will not be infinite. */
  2122.       do
  2123.         {
  2124.           base_string--;
  2125.           if (*base_string == ')')
  2126.         parens_balenced++;
  2127.           if (*base_string == '(')
  2128.         parens_balenced--;
  2129.         }
  2130.       while (parens_balenced);
  2131.       base_string++;    /* Skip past '('. */
  2132.       if (*base_string == REGISTER_PREFIX || *base_string == ',')
  2133.         found_base_index_form = 1;
  2134.     }
  2135.  
  2136.       /* If we can't parse a base index register expression, we've found
  2137.      a pure displacement expression.  We set up displacement_string_start
  2138.      and displacement_string_end for the code below. */
  2139.       if (!found_base_index_form)
  2140.     {
  2141.       displacement_string_start = op_string;
  2142.       displacement_string_end = end_of_operand_string;
  2143.     }
  2144.       else
  2145.     {
  2146.       char *base_reg_name, *index_reg_name, *num_string;
  2147.       int num;
  2148.  
  2149.       i.types[this_operand] |= BaseIndex;
  2150.  
  2151.       /* If there is a displacement set-up for it to be parsed later. */
  2152.       if (base_string != op_string + 1)
  2153.         {
  2154.           displacement_string_start = op_string;
  2155.           displacement_string_end = base_string - 1;
  2156.         }
  2157.  
  2158.       /* Find base register (if any). */
  2159.       if (*base_string != ',')
  2160.         {
  2161.           base_reg_name = base_string++;
  2162.           /* skip past register name & parse it */
  2163.           while (isalpha (*base_string))
  2164.         base_string++;
  2165.           if (base_string == base_reg_name + 1)
  2166.         {
  2167.           as_bad ("can't find base register name after '(%c'",
  2168.               REGISTER_PREFIX);
  2169.           return 0;
  2170.         }
  2171.           END_STRING_AND_SAVE (base_string);
  2172.           if (!(i.base_reg = parse_register (base_reg_name)))
  2173.         {
  2174.           as_bad ("bad base register name ('%s')", base_reg_name);
  2175.           return 0;
  2176.         }
  2177.           RESTORE_END_STRING (base_string);
  2178.         }
  2179.  
  2180.       /* Now check seperator; must be ',' ==> index reg
  2181.                OR num ==> no index reg. just scale factor
  2182.                OR ')' ==> end. (scale factor = 1) */
  2183.       if (*base_string != ',' && *base_string != ')')
  2184.         {
  2185.           as_bad ("expecting ',' or ')' after base register in `%s'",
  2186.               operand_string);
  2187.           return 0;
  2188.         }
  2189.  
  2190.       /* There may index reg here; and there may be a scale factor. */
  2191.       if (*base_string == ',' && *(base_string + 1) == REGISTER_PREFIX)
  2192.         {
  2193.           index_reg_name = ++base_string;
  2194.           while (isalpha (*++base_string));
  2195.           END_STRING_AND_SAVE (base_string);
  2196.           if (!(i.index_reg = parse_register (index_reg_name)))
  2197.         {
  2198.           as_bad ("bad index register name ('%s')", index_reg_name);
  2199.           return 0;
  2200.         }
  2201.           RESTORE_END_STRING (base_string);
  2202.         }
  2203.  
  2204.       /* Check for scale factor. */
  2205.       if (*base_string == ',' && isdigit (*(base_string + 1)))
  2206.         {
  2207.           num_string = ++base_string;
  2208.           while (is_digit_char (*base_string))
  2209.         base_string++;
  2210.           if (base_string == num_string)
  2211.         {
  2212.           as_bad ("can't find a scale factor after ','");
  2213.           return 0;
  2214.         }
  2215.           END_STRING_AND_SAVE (base_string);
  2216.           /* We've got a scale factor. */
  2217.           if (!sscanf (num_string, "%d", &num))
  2218.         {
  2219.           as_bad ("can't parse scale factor from '%s'", num_string);
  2220.           return 0;
  2221.         }
  2222.           RESTORE_END_STRING (base_string);
  2223.           switch (num)
  2224.         {        /* must be 1 digit scale */
  2225.         case 1:
  2226.           i.log2_scale_factor = 0;
  2227.           break;
  2228.         case 2:
  2229.           i.log2_scale_factor = 1;
  2230.           break;
  2231.         case 4:
  2232.           i.log2_scale_factor = 2;
  2233.           break;
  2234.         case 8:
  2235.           i.log2_scale_factor = 3;
  2236.           break;
  2237.         default:
  2238.           as_bad ("expecting scale factor of 1, 2, 4, 8; got %d", num);
  2239.           return 0;
  2240.         }
  2241.         }
  2242.       else
  2243.         {
  2244.           if (!i.index_reg && *base_string == ',')
  2245.         {
  2246.           as_bad ("expecting index register or scale factor after ','; got '%c'",
  2247.               *(base_string + 1));
  2248.           return 0;
  2249.         }
  2250.         }
  2251.     }
  2252.  
  2253.       /* If there's an expression begining the operand, parse it,
  2254.      assuming displacement_string_start and displacement_string_end
  2255.      are meaningful. */
  2256.       if (displacement_string_start)
  2257.     {
  2258.       register expressionS *exp;
  2259.       segT exp_seg = 0;
  2260.       char *save_input_line_pointer;
  2261.       exp = &disp_expressions[i.disp_operands];
  2262.       i.disps[this_operand] = exp;
  2263.       i.disp_reloc[this_operand] = NO_RELOC;
  2264.       i.disp_operands++;
  2265.       save_input_line_pointer = input_line_pointer;
  2266.       input_line_pointer = displacement_string_start;
  2267.       END_STRING_AND_SAVE (displacement_string_end);
  2268. #ifndef LEX_AT
  2269.       {
  2270.         /*
  2271.          * We can have operands of the form
  2272.          *   <symbol>@GOTOFF+<nnn>
  2273.          * Take the easy way out here and copy everything
  2274.          * into a temporary buffer...
  2275.          */
  2276.         register char *cp;
  2277.         if ((cp = strchr (input_line_pointer,'@')) != NULL) {
  2278.           char tmpbuf[BUFSIZ];
  2279.           
  2280.           if(!GOT_symbol)
  2281.         GOT_symbol = symbol_find_or_make(GLOBAL_OFFSET_TABLE_NAME);
  2282.  
  2283.           if (strncmp(cp+1, "PLT", 3) == 0) {
  2284.         i.disp_reloc[this_operand] = BFD_RELOC_386_PLT32;
  2285.         *cp = '\0';
  2286.         strcpy(tmpbuf, input_line_pointer);
  2287.         strcat(tmpbuf, cp+1+3);
  2288.         *cp = '@';
  2289.           } else if (strncmp(cp+1, "GOTOFF", 6) == 0) {
  2290.         i.disp_reloc[this_operand] = BFD_RELOC_386_GOTOFF;
  2291.         *cp = '\0';
  2292.         strcpy(tmpbuf, input_line_pointer);
  2293.         strcat(tmpbuf, cp+1+6);
  2294.         *cp = '@';
  2295.           } else if (strncmp(cp+1, "GOT", 3) == 0) {
  2296.         i.disp_reloc[this_operand] = BFD_RELOC_386_GOT32;
  2297.         *cp = '\0';
  2298.         strcpy(tmpbuf, input_line_pointer);
  2299.         strcat(tmpbuf, cp+1+3);
  2300.         *cp = '@';
  2301.           } else
  2302.         as_bad("Bad reloc specifier '%s' in expression", cp+1);
  2303.           input_line_pointer = tmpbuf;
  2304.         }
  2305.       }
  2306. #endif
  2307.       exp_seg = expression (exp);
  2308.  
  2309. #ifdef BFD_ASSEMBLER
  2310.       /* We do this to make sure that the section symbol is in
  2311.          the symbol table.  We will ultimately change the relocation
  2312.          to be relative to the beginning of the section */
  2313.       if (i.disp_reloc[this_operand] == BFD_RELOC_386_GOTOFF)
  2314.         {
  2315.           if (S_IS_LOCAL(exp->X_add_symbol)
  2316.           && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
  2317.         section_symbol(exp->X_add_symbol->bsym->section);
  2318.           assert (exp->X_op == O_symbol);
  2319.           exp->X_op = O_subtract;
  2320.           exp->X_op_symbol = GOT_symbol;
  2321.           i.disp_reloc[this_operand] = BFD_RELOC_32;
  2322.         }
  2323. #endif
  2324.  
  2325.       if (*input_line_pointer)
  2326.         as_bad ("Ignoring junk '%s' after expression", input_line_pointer);
  2327.       RESTORE_END_STRING (displacement_string_end);
  2328.       input_line_pointer = save_input_line_pointer;
  2329.       if (exp->X_op == O_absent)
  2330.         {
  2331.           /* missing expr becomes absolute 0 */
  2332.           as_bad ("missing or invalid displacement '%s' taken as 0",
  2333.               operand_string);
  2334.           i.types[this_operand] |= (Disp | Abs);
  2335.           exp->X_op = O_constant;
  2336.           exp->X_add_number = 0;
  2337.           exp->X_add_symbol = (symbolS *) 0;
  2338.           exp->X_op_symbol = (symbolS *) 0;
  2339.         }
  2340.       else if (exp->X_op == O_constant)
  2341.         {
  2342.           i.types[this_operand] |= SMALLEST_DISP_TYPE (exp->X_add_number);
  2343.         }
  2344.       else if (exp_seg == text_section
  2345.            || exp_seg == data_section
  2346.            || exp_seg == bss_section
  2347.            || exp_seg == undefined_section)
  2348.         {
  2349.           i.types[this_operand] |= Disp32;
  2350.         }
  2351.       else
  2352.         {
  2353. #ifndef OBJ_AOUT
  2354.           i.types[this_operand] |= Disp32;
  2355. #else
  2356.           goto seg_unimplemented;
  2357. #endif
  2358.         }
  2359.     }
  2360.  
  2361.       /* Make sure the memory operand we've been dealt is valid. */
  2362.       if (i.base_reg && i.index_reg &&
  2363.       !(i.base_reg->reg_type & i.index_reg->reg_type & Reg))
  2364.     {
  2365.       as_bad ("register size mismatch in (base,index,scale) expression");
  2366.       return 0;
  2367.     }
  2368.       /*
  2369.        * special case for (%dx) while doing input/output op
  2370.        */
  2371.       if ((i.base_reg &&
  2372.        (i.base_reg->reg_type == (Reg16 | InOutPortReg)) &&
  2373.        (i.index_reg == 0)))
  2374.     {
  2375.       i.types[this_operand] |= InOutPortReg;
  2376.       return 1;
  2377.     }
  2378.       if ((i.base_reg && (i.base_reg->reg_type & Reg32) == 0) ||
  2379.       (i.index_reg && (i.index_reg->reg_type & Reg32) == 0))
  2380.     {
  2381.       as_bad ("base/index register must be 32 bit register");
  2382.       return 0;
  2383.     }
  2384.       if (i.index_reg && i.index_reg == esp)
  2385.     {
  2386.       as_bad ("%s may not be used as an index register", esp->reg_name);
  2387.       return 0;
  2388.     }
  2389.     }
  2390.   else
  2391.     {                /* it's not a memory operand; argh! */
  2392.       as_bad ("invalid char %s begining %s operand '%s'",
  2393.           output_invalid (*op_string), ordinal_names[this_operand],
  2394.           op_string);
  2395.       return 0;
  2396.     }
  2397.   return 1;            /* normal return */
  2398. }
  2399.  
  2400. /*
  2401.  *            md_estimate_size_before_relax()
  2402.  *
  2403.  * Called just before relax().
  2404.  * Any symbol that is now undefined will not become defined.
  2405.  * Return the correct fr_subtype in the frag.
  2406.  * Return the initial "guess for fr_var" to caller.
  2407.  * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
  2408.  * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
  2409.  * Although it may not be explicit in the frag, pretend fr_var starts with a
  2410.  * 0 value.
  2411.  */
  2412. int
  2413. md_estimate_size_before_relax (fragP, segment)
  2414.      register fragS *fragP;
  2415.      register segT segment;
  2416. {
  2417.   register unsigned char *opcode;
  2418.   register int old_fr_fix;
  2419.  
  2420.   old_fr_fix = fragP->fr_fix;
  2421.   opcode = (unsigned char *) fragP->fr_opcode;
  2422.   /* We've already got fragP->fr_subtype right;  all we have to do is check
  2423.        for un-relaxable symbols. */
  2424.   if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
  2425.     {
  2426.       /* symbol is undefined in this segment */
  2427.       switch (opcode[0])
  2428.     {
  2429.     case JUMP_PC_RELATIVE:    /* make jmp (0xeb) a dword displacement jump */
  2430.       opcode[0] = 0xe9;    /* dword disp jmp */
  2431.       fragP->fr_fix += 4;
  2432.       fix_new (fragP, old_fr_fix, 4,
  2433.                  fragP->fr_symbol,
  2434.            fragP->fr_offset, 1,
  2435.            (GOT_symbol && /* Not quite right - we should switch on
  2436.                      presence of @PLT, but I cannot see how
  2437.                      to get to that from here.  We should have
  2438.                      done this in md_assemble to really
  2439.                      get it right all of the time, but I
  2440.                      think it does not matter that much, as
  2441.                      this will be right most of the time. ERY*/
  2442.             S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
  2443.            BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
  2444.       break;
  2445.  
  2446.     default:
  2447.       /* This changes the byte-displacement jump 0x7N -->
  2448.                the dword-displacement jump 0x0f8N */
  2449.       opcode[1] = opcode[0] + 0x10;
  2450.       opcode[0] = TWO_BYTE_OPCODE_ESCAPE;    /* two-byte escape */
  2451.       fragP->fr_fix += 1 + 4;    /* we've added an opcode byte */
  2452.       fix_new (fragP, old_fr_fix + 1, 4,
  2453.            fragP->fr_symbol,
  2454.            fragP->fr_offset, 1, 
  2455.            (GOT_symbol &&  /* Not quite right - we should switch on
  2456.                      presence of @PLT, but I cannot see how
  2457.                      to get to that from here.  ERY */
  2458.             S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
  2459.            BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
  2460.       break;
  2461.     }
  2462.       frag_wane (fragP);
  2463.     }
  2464.   return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
  2465. }                /* md_estimate_size_before_relax() */
  2466.  
  2467. /*
  2468.  *            md_convert_frag();
  2469.  *
  2470.  * Called after relax() is finished.
  2471.  * In:    Address of frag.
  2472.  *    fr_type == rs_machine_dependent.
  2473.  *    fr_subtype is what the address relaxed to.
  2474.  *
  2475.  * Out:    Any fixSs and constants are set up.
  2476.  *    Caller will turn frag into a ".space 0".
  2477.  */
  2478. #ifndef BFD_ASSEMBLER
  2479. void
  2480. md_convert_frag (headers, sec, fragP)
  2481.      object_headers *headers;
  2482.      segT sec;
  2483.      register fragS *fragP;
  2484. #else
  2485. void
  2486. md_convert_frag (abfd, sec, fragP)
  2487.      bfd *abfd;
  2488.      segT sec;
  2489.      register fragS *fragP;
  2490. #endif
  2491. {
  2492.   register unsigned char *opcode;
  2493.   unsigned char *where_to_put_displacement = NULL;
  2494.   unsigned int target_address;
  2495.   unsigned int opcode_address;
  2496.   unsigned int extension = 0;
  2497.   int displacement_from_opcode_start;
  2498.  
  2499.   opcode = (unsigned char *) fragP->fr_opcode;
  2500.  
  2501.   /* Address we want to reach in file space. */
  2502.   target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
  2503. #ifdef BFD_ASSEMBLER /* not needed otherwise? */
  2504.   target_address += fragP->fr_symbol->sy_frag->fr_address;
  2505. #endif
  2506.  
  2507.   /* Address opcode resides at in file space. */
  2508.   opcode_address = fragP->fr_address + fragP->fr_fix;
  2509.  
  2510.   /* Displacement from opcode start to fill into instruction. */
  2511.   displacement_from_opcode_start = target_address - opcode_address;
  2512.  
  2513.   switch (fragP->fr_subtype)
  2514.     {
  2515.     case ENCODE_RELAX_STATE (COND_JUMP, BYTE):
  2516.     case ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE):
  2517.       /* don't have to change opcode */
  2518.       extension = 1;        /* 1 opcode + 1 displacement */
  2519.       where_to_put_displacement = &opcode[1];
  2520.       break;
  2521.  
  2522.     case ENCODE_RELAX_STATE (COND_JUMP, WORD):
  2523.       opcode[1] = TWO_BYTE_OPCODE_ESCAPE;
  2524.       opcode[2] = opcode[0] + 0x10;
  2525.       opcode[0] = WORD_PREFIX_OPCODE;
  2526.       extension = 4;        /* 3 opcode + 2 displacement */
  2527.       where_to_put_displacement = &opcode[3];
  2528.       break;
  2529.  
  2530.     case ENCODE_RELAX_STATE (UNCOND_JUMP, WORD):
  2531.       opcode[1] = 0xe9;
  2532.       opcode[0] = WORD_PREFIX_OPCODE;
  2533.       extension = 3;        /* 2 opcode + 2 displacement */
  2534.       where_to_put_displacement = &opcode[2];
  2535.       break;
  2536.  
  2537.     case ENCODE_RELAX_STATE (COND_JUMP, DWORD):
  2538.       opcode[1] = opcode[0] + 0x10;
  2539.       opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
  2540.       extension = 5;        /* 2 opcode + 4 displacement */
  2541.       where_to_put_displacement = &opcode[2];
  2542.       break;
  2543.  
  2544.     case ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD):
  2545.       opcode[0] = 0xe9;
  2546.       extension = 4;        /* 1 opcode + 4 displacement */
  2547.       where_to_put_displacement = &opcode[1];
  2548.       break;
  2549.  
  2550.     default:
  2551.       BAD_CASE (fragP->fr_subtype);
  2552.       break;
  2553.     }
  2554.   /* now put displacement after opcode */
  2555.   md_number_to_chars ((char *) where_to_put_displacement,
  2556.               (valueT) (displacement_from_opcode_start - extension),
  2557.               SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
  2558.   fragP->fr_fix += extension;
  2559. }
  2560.  
  2561.  
  2562. int md_short_jump_size = 2;    /* size of byte displacement jmp */
  2563. int md_long_jump_size = 5;    /* size of dword displacement jmp */
  2564. const int md_reloc_size = 8;    /* Size of relocation record */
  2565.  
  2566. void
  2567. md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
  2568.      char *ptr;
  2569.      addressT from_addr, to_addr;
  2570.      fragS *frag;
  2571.      symbolS *to_symbol;
  2572. {
  2573.   long offset;
  2574.  
  2575.   offset = to_addr - (from_addr + 2);
  2576.   md_number_to_chars (ptr, (valueT) 0xeb, 1);    /* opcode for byte-disp jump */
  2577.   md_number_to_chars (ptr + 1, (valueT) offset, 1);
  2578. }
  2579.  
  2580. void
  2581. md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
  2582.      char *ptr;
  2583.      addressT from_addr, to_addr;
  2584.      fragS *frag;
  2585.      symbolS *to_symbol;
  2586. {
  2587.   long offset;
  2588.  
  2589.   if (flag_do_long_jump)
  2590.     {
  2591.       offset = to_addr - S_GET_VALUE (to_symbol);
  2592.       md_number_to_chars (ptr, (valueT) 0xe9, 1);/* opcode for long jmp */
  2593.       md_number_to_chars (ptr + 1, (valueT) offset, 4);
  2594.       fix_new (frag, (ptr + 1) - frag->fr_literal, 4,
  2595.            to_symbol, (offsetT) 0, 0, BFD_RELOC_32);
  2596.     }
  2597.   else
  2598.     {
  2599.       offset = to_addr - (from_addr + 5);
  2600.       md_number_to_chars (ptr, (valueT) 0xe9, 1);
  2601.       md_number_to_chars (ptr + 1, (valueT) offset, 4);
  2602.     }
  2603. }
  2604.  
  2605. /* Apply a fixup (fixS) to segment data, once it has been determined
  2606.    by our caller that we have all the info we need to fix it up.
  2607.  
  2608.    On the 386, immediates, displacements, and data pointers are all in
  2609.    the same (little-endian) format, so we don't need to care about which
  2610.    we are handling.  */
  2611.  
  2612. int
  2613. md_apply_fix3 (fixP, valp, seg)
  2614.      fixS *fixP;        /* The fix we're to put in.  */
  2615.      valueT *valp;        /* Pointer to the value of the bits.  */
  2616.      segT seg;            /* Segment fix is from.  */
  2617. {
  2618.   register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
  2619.   valueT value = *valp;
  2620.  
  2621. #if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
  2622.   /*
  2623.    * This is a hack.  There should be a better way to
  2624.    * handle this.
  2625.    */
  2626.   if (fixP->fx_r_type == BFD_RELOC_32_PCREL && fixP->fx_addsy)
  2627.     {
  2628.       value += fixP->fx_where + fixP->fx_frag->fr_address;
  2629. #ifdef OBJ_ELF
  2630.       if (S_GET_SEGMENT (fixP->fx_addsy) == seg
  2631.       || (fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0)
  2632.     {
  2633.       /* Yes, we add the values in twice.  This is because
  2634.          bfd_perform_relocation subtracts them out again.  I think
  2635.          bfd_perform_relocation is broken, but I don't dare change
  2636.          it.  FIXME.  */
  2637.       value += fixP->fx_where + fixP->fx_frag->fr_address;
  2638.     }
  2639. #endif
  2640.     }
  2641.  
  2642.   /* Fix a few things - the dynamic linker expects certain values here,
  2643.      and we must not dissappoint it. */
  2644. #ifdef OBJ_ELF
  2645.   if (fixP->fx_addsy)
  2646.     switch(fixP->fx_r_type) {
  2647.     case BFD_RELOC_386_PLT32:
  2648.       /* Make the jump instruction point to the address of the operand.  At
  2649.      runtime we merely add the offset to the actual PLT entry. */
  2650.       value = 0xfffffffc;
  2651.       break;
  2652.     case BFD_RELOC_386_GOTPC:
  2653. /*
  2654.  *  This is tough to explain.  We end up with this one if we have
  2655.  * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal
  2656.  * here is to obtain the absolute address of the GOT, and it is strongly
  2657.  * preferable from a performance point of view to avoid using a runtime
  2658.  * relocation for this.  The actual sequence of instructions often look 
  2659.  * something like:
  2660.  * 
  2661.  *     call    .L66
  2662.  * .L66:
  2663.  *     popl    %ebx
  2664.  *     addl    $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
  2665.  * 
  2666.  *     The call and pop essentially return the absolute address of
  2667.  * the label .L66 and store it in %ebx.  The linker itself will
  2668.  * ultimately change the first operand of the addl so that %ebx points to
  2669.  * the GOT, but to keep things simple, the .o file must have this operand
  2670.  * set so that it generates not the absolute address of .L66, but the
  2671.  * absolute address of itself.  This allows the linker itself simply
  2672.  * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
  2673.  * added in, and the addend of the relocation is stored in the operand
  2674.  * field for the instruction itself.
  2675.  * 
  2676.  *     Our job here is to fix the operand so that it would add the correct
  2677.  * offset so that %ebx would point to itself.  The thing that is tricky is
  2678.  * that .-.L66 will point to the beginning of the instruction, so we need
  2679.  * to further modify the operand so that it will point to itself.
  2680.  * There are other cases where you have something like:
  2681.  * 
  2682.  *     .long    $_GLOBAL_OFFSET_TABLE_+[.-.L66]
  2683.  * 
  2684.  * and here no correction would be required.  Internally in the assembler
  2685.  * we treat operands of this form as not being pcrel since the '.' is 
  2686.  * explicitly mentioned, and I wonder whether it would simplify matters
  2687.  * to do it this way.  Who knows.  In earlier versions of the PIC patches,
  2688.  * the pcrel_adjust field was used to store the correction, but since the
  2689.  * expression is not pcrel, I felt it would be confusing to do it this way.
  2690.  */
  2691.       value -= 1;
  2692.       break;
  2693.     case BFD_RELOC_386_GOT32:
  2694.       value = 0; /* Fully resolved at runtime.  No addend. */
  2695.       break;
  2696.     case BFD_RELOC_386_GOTOFF:
  2697.       break;
  2698.  
  2699.     default:
  2700.       break;
  2701.     }
  2702. #endif
  2703.  
  2704. #endif
  2705.   md_number_to_chars (p, value, fixP->fx_size);
  2706.  
  2707.   return 1;
  2708. }
  2709.  
  2710. #if 0
  2711. /* This is never used.  */
  2712. long                /* Knows about the byte order in a word. */
  2713. md_chars_to_number (con, nbytes)
  2714.      unsigned char con[];    /* Low order byte 1st. */
  2715.      int nbytes;        /* Number of bytes in the input. */
  2716. {
  2717.   long retval;
  2718.   for (retval = 0, con += nbytes - 1; nbytes--; con--)
  2719.     {
  2720.       retval <<= BITS_PER_CHAR;
  2721.       retval |= *con;
  2722.     }
  2723.   return retval;
  2724. }
  2725. #endif /* 0 */
  2726.  
  2727.  
  2728. #define MAX_LITTLENUMS 6
  2729.  
  2730. /* Turn the string pointed to by litP into a floating point constant of type
  2731.    type, and emit the appropriate bytes.  The number of LITTLENUMS emitted
  2732.    is stored in *sizeP .  An error message is returned, or NULL on OK.  */
  2733. char *
  2734. md_atof (type, litP, sizeP)
  2735.      char type;
  2736.      char *litP;
  2737.      int *sizeP;
  2738. {
  2739.   int prec;
  2740.   LITTLENUM_TYPE words[MAX_LITTLENUMS];
  2741.   LITTLENUM_TYPE *wordP;
  2742.   char *t;
  2743.  
  2744.   switch (type)
  2745.     {
  2746.     case 'f':
  2747.     case 'F':
  2748.       prec = 2;
  2749.       break;
  2750.  
  2751.     case 'd':
  2752.     case 'D':
  2753.       prec = 4;
  2754.       break;
  2755.  
  2756.     case 'x':
  2757.     case 'X':
  2758.       prec = 5;
  2759.       break;
  2760.  
  2761.     default:
  2762.       *sizeP = 0;
  2763.       return "Bad call to md_atof ()";
  2764.     }
  2765.   t = atof_ieee (input_line_pointer, type, words);
  2766.   if (t)
  2767.     input_line_pointer = t;
  2768.  
  2769.   *sizeP = prec * sizeof (LITTLENUM_TYPE);
  2770.   /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
  2771.      the bigendian 386.  */
  2772.   for (wordP = words + prec - 1; prec--;)
  2773.     {
  2774.       md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
  2775.       litP += sizeof (LITTLENUM_TYPE);
  2776.     }
  2777.   return 0;
  2778. }
  2779.  
  2780. char output_invalid_buf[8];
  2781.  
  2782. static char *
  2783. output_invalid (c)
  2784.      char c;
  2785. {
  2786.   if (isprint (c))
  2787.     sprintf (output_invalid_buf, "'%c'", c);
  2788.   else
  2789.     sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
  2790.   return output_invalid_buf;
  2791. }
  2792.  
  2793. /* reg_string starts *before* REGISTER_PREFIX */
  2794. static reg_entry *
  2795. parse_register (reg_string)
  2796.      char *reg_string;
  2797. {
  2798.   register char *s = reg_string;
  2799.   register char *p;
  2800.   char reg_name_given[MAX_REG_NAME_SIZE];
  2801.  
  2802.   s++;                /* skip REGISTER_PREFIX */
  2803.   for (p = reg_name_given; is_register_char (*s); p++, s++)
  2804.     {
  2805.       *p = register_chars[(unsigned char) *s];
  2806.       if (p >= reg_name_given + MAX_REG_NAME_SIZE)
  2807.     return (reg_entry *) 0;
  2808.     }
  2809.   *p = '\0';
  2810.   return (reg_entry *) hash_find (reg_hash, reg_name_given);
  2811. }
  2812.  
  2813. #ifdef OBJ_ELF
  2814. CONST char *md_shortopts = "kmVQ:";
  2815. #else
  2816. CONST char *md_shortopts = "m";
  2817. #endif
  2818. struct option md_longopts[] = {
  2819.   {NULL, no_argument, NULL, 0}
  2820. };
  2821. size_t md_longopts_size = sizeof(md_longopts);
  2822.  
  2823. int
  2824. md_parse_option (c, arg)
  2825.      int c;
  2826.      char *arg;
  2827. {
  2828.   switch (c)
  2829.     {
  2830.     case 'm':
  2831.       flag_do_long_jump = 1;
  2832.       break;
  2833.  
  2834. #ifdef OBJ_ELF
  2835.       /* -k: Ignore for FreeBSD compatibility.  */
  2836.     case 'k':
  2837.       break;
  2838.  
  2839.       /* -V: SVR4 argument to print version ID.  */
  2840.     case 'V':
  2841.       print_version_id ();
  2842.       break;
  2843.  
  2844.       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
  2845.      should be emitted or not.  FIXME: Not implemented.  */
  2846.     case 'Q':
  2847.       break;
  2848. #endif
  2849.  
  2850.     default:
  2851.       return 0;
  2852.     }
  2853.   return 1;
  2854. }
  2855.  
  2856. void
  2857. md_show_usage (stream)
  2858.      FILE *stream;
  2859. {
  2860.   fprintf (stream, "\
  2861. -m            do long jump\n");
  2862. }
  2863.  
  2864. /* We have no need to default values of symbols.  */
  2865.  
  2866. /* ARGSUSED */
  2867. symbolS *
  2868. md_undefined_symbol (name)
  2869.      char *name;
  2870. {
  2871.     if (*name == '_' && *(name+1) == 'G'
  2872.         && strcmp(name, GLOBAL_OFFSET_TABLE_NAME) == 0)
  2873.       {
  2874.         if(!GOT_symbol)
  2875.           {
  2876.         if(symbol_find(name)) 
  2877.           as_bad("GOT already in symbol table");
  2878.         GOT_symbol = symbol_new (name, undefined_section, 
  2879.                      (valueT) 0, &zero_address_frag);
  2880.           };
  2881.         return GOT_symbol;
  2882.       }
  2883.   return 0;
  2884. }
  2885.  
  2886. /* Round up a section size to the appropriate boundary.  */
  2887. valueT
  2888. md_section_align (segment, size)
  2889.      segT segment;
  2890.      valueT size;
  2891. {
  2892.   return size;            /* Byte alignment is fine */
  2893. }
  2894.  
  2895. /* Exactly what point is a PC-relative offset relative TO?  On the
  2896.    i386, they're relative to the address of the offset, plus its
  2897.    size. (??? Is this right?  FIXME-SOON!) */
  2898. long
  2899. md_pcrel_from (fixP)
  2900.      fixS *fixP;
  2901. {
  2902.   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
  2903. }
  2904.  
  2905. #ifndef I386COFF
  2906.  
  2907. static void
  2908. s_bss (ignore)
  2909.      int ignore;
  2910. {
  2911.   register int temp;
  2912.  
  2913.   temp = get_absolute_expression ();
  2914.   subseg_set (bss_section, (subsegT) temp);
  2915.   demand_empty_rest_of_line ();
  2916. }
  2917.  
  2918. #endif
  2919.  
  2920.  
  2921. #ifdef BFD_ASSEMBLER
  2922.  
  2923. void
  2924. i386_validate_fix (fixp)
  2925.      fixS *fixp;
  2926. {
  2927.   if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
  2928.     {
  2929.       fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
  2930.       fixp->fx_subsy = 0;
  2931.     }
  2932. }
  2933.  
  2934. #define F(SZ,PCREL)        (((SZ) << 1) + (PCREL))
  2935. #define MAP(SZ,PCREL,TYPE)    case F(SZ,PCREL): code = (TYPE); break
  2936.  
  2937. arelent *
  2938. tc_gen_reloc (section, fixp)
  2939.      asection *section;
  2940.      fixS *fixp;
  2941. {
  2942.   arelent *rel;
  2943.   bfd_reloc_code_real_type code;
  2944.  
  2945.   switch(fixp->fx_r_type)
  2946.     {
  2947.     case BFD_RELOC_386_PLT32:
  2948.     case BFD_RELOC_386_GOT32:
  2949.     case BFD_RELOC_386_GOTOFF:
  2950.     case BFD_RELOC_386_GOTPC:
  2951.       code = fixp->fx_r_type;
  2952.       break;
  2953.     default:
  2954.       switch (F (fixp->fx_size, fixp->fx_pcrel))
  2955.     {
  2956. #ifndef OBJ_ELF
  2957.       MAP (1, 0, BFD_RELOC_8);
  2958.       MAP (2, 0, BFD_RELOC_16);
  2959. #endif
  2960.       MAP (4, 0, BFD_RELOC_32);
  2961. #ifndef OBJ_ELF
  2962.       MAP (1, 1, BFD_RELOC_8_PCREL);
  2963.       MAP (2, 1, BFD_RELOC_16_PCREL);
  2964. #endif
  2965.       MAP (4, 1, BFD_RELOC_32_PCREL);
  2966.     default:
  2967.       as_bad ("Can not do %d byte %srelocation", fixp->fx_size,
  2968.           fixp->fx_pcrel ? "pc-relative " : "");
  2969.     }
  2970.     }
  2971. #undef MAP
  2972. #undef F
  2973.  
  2974.   if (code == BFD_RELOC_32
  2975.       && GOT_symbol
  2976.       && fixp->fx_addsy == GOT_symbol)
  2977.     code = BFD_RELOC_386_GOTPC;
  2978.  
  2979.   rel = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
  2980.   assert (rel != 0);
  2981.   rel->sym_ptr_ptr = &fixp->fx_addsy->bsym;
  2982.   rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
  2983.   if (fixp->fx_pcrel)
  2984.     rel->addend = fixp->fx_addnumber;
  2985.   else
  2986.     rel->addend = 0;
  2987.  
  2988.   rel->howto = bfd_reloc_type_lookup (stdoutput, code);
  2989.   if (!rel->howto)
  2990.     {
  2991.       const char *name;
  2992.  
  2993.       name = S_GET_NAME (fixp->fx_addsy);
  2994.       if (name == NULL)
  2995.     name = "<unknown>";
  2996.       as_fatal ("Cannot generate relocation type for symbol %s, code %s",
  2997.         name, bfd_get_reloc_code_name (code));
  2998.     }
  2999.  
  3000.   return rel;
  3001. }
  3002.  
  3003. #else /* ! BFD_ASSEMBLER */
  3004.  
  3005. #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
  3006. void
  3007. tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
  3008.      char *where;
  3009.      fixS *fixP;
  3010.      relax_addressT segment_address_in_file;
  3011. {
  3012.   /*
  3013.    * In: length of relocation (or of address) in chars: 1, 2 or 4.
  3014.    * Out: GNU LD relocation length code: 0, 1, or 2.
  3015.    */
  3016.  
  3017.   static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
  3018.   long r_symbolnum;
  3019.  
  3020.   know (fixP->fx_addsy != NULL);
  3021.  
  3022.   md_number_to_chars (where,
  3023.               (valueT) (fixP->fx_frag->fr_address
  3024.                 + fixP->fx_where - segment_address_in_file),
  3025.               4);
  3026.  
  3027.   r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
  3028.          ? S_GET_TYPE (fixP->fx_addsy)
  3029.          : fixP->fx_addsy->sy_number);
  3030.  
  3031.   where[6] = (r_symbolnum >> 16) & 0x0ff;
  3032.   where[5] = (r_symbolnum >> 8) & 0x0ff;
  3033.   where[4] = r_symbolnum & 0x0ff;
  3034.   where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
  3035.           | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
  3036.           | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
  3037. }
  3038.  
  3039. #endif /* OBJ_AOUT or OBJ_BOUT */
  3040.  
  3041. #if defined (I386COFF)
  3042.  
  3043. short
  3044. tc_coff_fix2rtype (fixP)
  3045.      fixS *fixP;
  3046. {
  3047.   if (fixP->fx_r_type == R_IMAGEBASE)
  3048.     return R_IMAGEBASE;
  3049.  
  3050.   return (fixP->fx_pcrel ?
  3051.       (fixP->fx_size == 1 ? R_PCRBYTE :
  3052.        fixP->fx_size == 2 ? R_PCRWORD :
  3053.        R_PCRLONG) :
  3054.       (fixP->fx_size == 1 ? R_RELBYTE :
  3055.        fixP->fx_size == 2 ? R_RELWORD :
  3056.        R_DIR32));
  3057. }
  3058.  
  3059. int
  3060. tc_coff_sizemachdep (frag)
  3061.      fragS *frag;
  3062. {
  3063.   if (frag->fr_next)
  3064.     return (frag->fr_next->fr_address - frag->fr_address);
  3065.   else
  3066.     return 0;
  3067. }
  3068.  
  3069. #endif /* I386COFF */
  3070.  
  3071. #endif /* BFD_ASSEMBLER? */
  3072.  
  3073. #ifdef SCO_ELF
  3074.  
  3075. /* Heavily plagarized from obj_elf_version.  The idea is to emit the
  3076.    SCO specific identifier in the .notes section to satisfy the SCO
  3077.    linker.
  3078.  
  3079.    This looks more complicated than it really is.  As opposed to the
  3080.    "obvious" solution, this should handle the cross dev cases
  3081.    correctly.  (i.e, hosting on a 64 bit big endian processor, but
  3082.    generating SCO Elf code) Efficiency isn't a concern, as there
  3083.    should be exactly one of these sections per object module.
  3084.  
  3085.    SCO OpenServer 5 identifies it's ELF modules with a standard ELF
  3086.    .note section.
  3087.  
  3088.    int_32 namesz  = 4 ;  Name size 
  3089.    int_32 descsz  = 12 ; Descriptive information 
  3090.    int_32 type    = 1 ;  
  3091.    char   name[4] = "SCO" ; Originator name ALWAYS SCO + NULL 
  3092.    int_32 version = (major ver # << 16)  | version of tools ;
  3093.    int_32 source  = (tool_id << 16 ) | 1 ;
  3094.    int_32 info    = 0 ;    These are set by the SCO tools, but we
  3095.                            don't know enough about the source 
  3096.                environment to set them.  SCO ld currently
  3097.                ignores them, and recommends we set them
  3098.                to zero.  */
  3099.  
  3100. #define SCO_MAJOR_VERSION 0x1
  3101. #define SCO_MINOR_VERSION 0x1
  3102.  
  3103. void
  3104. sco_id ()
  3105. {
  3106.   char *name;
  3107.   unsigned int c;
  3108.   char ch;
  3109.   char *p;
  3110.   asection *seg = now_seg;
  3111.   subsegT subseg = now_subseg;
  3112.   Elf_Internal_Note i_note;
  3113.   Elf_External_Note e_note;
  3114.   asection *note_secp = (asection *) NULL;
  3115.   int i, len;
  3116.  
  3117.   /* create the .note section */
  3118.  
  3119.   note_secp = subseg_new (".note", 0);
  3120.   bfd_set_section_flags (stdoutput,
  3121.              note_secp,
  3122.              SEC_HAS_CONTENTS | SEC_READONLY);
  3123.  
  3124.   /* process the version string */
  3125.  
  3126.   i_note.namesz = 4; 
  3127.   i_note.descsz = 12;        /* 12 descriptive bytes */
  3128.   i_note.type = NT_VERSION;    /* Contains a version string */
  3129.  
  3130.   p = frag_more (sizeof (i_note.namesz));
  3131.   md_number_to_chars (p, (valueT) i_note.namesz, 4);
  3132.  
  3133.   p = frag_more (sizeof (i_note.descsz));
  3134.   md_number_to_chars (p, (valueT) i_note.descsz, 4);
  3135.  
  3136.   p = frag_more (sizeof (i_note.type));
  3137.   md_number_to_chars (p, (valueT) i_note.type, 4);
  3138.  
  3139.   p = frag_more (4);
  3140.   strcpy (p, "SCO"); 
  3141.  
  3142.   /* Note: this is the version number of the ELF we're representing */
  3143.   p = frag_more (4);
  3144.   md_number_to_chars (p, (SCO_MAJOR_VERSION << 16) | (SCO_MINOR_VERSION), 4);
  3145.  
  3146.   /* Here, we pick a magic number for ourselves (yes, I "registered"
  3147.      it with SCO.  The bottom bit shows that we are compat with the
  3148.      SCO ABI.  */
  3149.   p = frag_more (4);
  3150.   md_number_to_chars (p, 0x4c520000 | 0x0001, 4);
  3151.  
  3152.   /* If we knew (or cared) what the source language options were, we'd
  3153.      fill them in here.  SCO has given us permission to ignore these
  3154.      and just set them to zero.  */
  3155.   p = frag_more (4);
  3156.   md_number_to_chars (p, 0x0000, 4);
  3157.  
  3158.   frag_align (2, 0); 
  3159.  
  3160.   /* We probably can't restore the current segment, for there likely
  3161.      isn't one yet...  */
  3162.   if (seg && subseg)
  3163.     subseg_set (seg, subseg);
  3164. }
  3165.  
  3166. #endif /* SCO_ELF */
  3167.  
  3168. /* end of tc-i386.c */
  3169.