1 /* i386.c -- Assemble code for the Intel 80386
2 Copyright (C) 1989, 91, 92, 93, 94, 95, 96, 1997 Free Software Foundation.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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 the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 Intel 80386 machine specific gas.
24 Bugs & suggestions are completely welcome. This is free software.
25 Please help us make it better.
34 #include "opcode/i386.h"
37 #define TC_RELOC(X,Y) (Y)
40 static unsigned long mode_from_disp_size PARAMS ((unsigned long));
41 static int fits_in_signed_byte PARAMS ((long));
42 static int fits_in_unsigned_byte PARAMS ((long));
43 static int fits_in_unsigned_word PARAMS ((long));
44 static int fits_in_signed_word PARAMS ((long));
45 static int smallest_imm_type PARAMS ((long));
46 static void set_16bit_code_flag PARAMS ((int));
48 static bfd_reloc_code_real_type reloc
49 PARAMS ((int, int, bfd_reloc_code_real_type));
52 /* 'md_assemble ()' gathers together information and puts it into a
57 /* TM holds the template for the insn were currently assembling. */
59 /* SUFFIX holds the opcode suffix (e.g. 'l' for 'movl') if given. */
61 /* Operands are coded with OPERANDS, TYPES, DISPS, IMMS, and REGS. */
63 /* OPERANDS gives the number of given operands. */
64 unsigned int operands;
66 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
67 of given register, displacement, memory operands and immediate
69 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
71 /* TYPES [i] is the type (see above #defines) which tells us how to
72 search through DISPS [i] & IMMS [i] & REGS [i] for the required
74 unsigned int types[MAX_OPERANDS];
76 /* Displacements (if given) for each operand. */
77 expressionS *disps[MAX_OPERANDS];
79 /* Relocation type for operand */
81 enum bfd_reloc_code_real disp_reloc[MAX_OPERANDS];
83 int disp_reloc[MAX_OPERANDS];
86 /* Immediate operands (if given) for each operand. */
87 expressionS *imms[MAX_OPERANDS];
89 /* Register operands (if given) for each operand. */
90 reg_entry *regs[MAX_OPERANDS];
92 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
93 the base index byte below. */
96 unsigned int log2_scale_factor;
98 /* SEG gives the seg_entry of this insn. It is equal to zero unless
99 an explicit segment override is given. */
100 const seg_entry *seg; /* segment for memory operands (if given) */
102 /* PREFIX holds all the given prefix opcodes (usually null).
103 PREFIXES is the size of PREFIX. */
104 /* richfix: really unsigned? */
105 unsigned char prefix[MAX_PREFIXES];
106 unsigned int prefixes;
108 /* RM and IB are the modrm byte and the base index byte where the
109 addressing modes of this insn are encoded. */
115 typedef struct _i386_insn i386_insn;
117 /* This array holds the chars that always start a comment. If the
118 pre-processor is disabled, these aren't very useful */
119 #if defined (TE_I386AIX) || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
120 const char comment_chars[] = "#/";
122 const char comment_chars[] = "#";
125 /* This array holds the chars that only start a comment at the beginning of
126 a line. If the line seems to have the form '# 123 filename'
127 .line and .file directives will appear in the pre-processed output */
128 /* Note that input_file.c hand checks for '#' at the beginning of the
129 first line of the input file. This is because the compiler outputs
130 #NO_APP at the beginning of its output. */
131 /* Also note that comments started like this one will always work if
132 '/' isn't otherwise defined. */
133 #if defined (TE_I386AIX) || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
134 const char line_comment_chars[] = "";
136 const char line_comment_chars[] = "/";
138 const char line_separator_chars[] = "";
140 /* Chars that can be used to separate mant from exp in floating point nums */
141 const char EXP_CHARS[] = "eE";
143 /* Chars that mean this number is a floating point constant */
146 const char FLT_CHARS[] = "fFdDxX";
148 /* tables for lexical analysis */
149 static char opcode_chars[256];
150 static char register_chars[256];
151 static char operand_chars[256];
152 static char space_chars[256];
153 static char identifier_chars[256];
154 static char digit_chars[256];
157 #define is_opcode_char(x) (opcode_chars[(unsigned char) x])
158 #define is_operand_char(x) (operand_chars[(unsigned char) x])
159 #define is_register_char(x) (register_chars[(unsigned char) x])
160 #define is_space_char(x) (space_chars[(unsigned char) x])
161 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
162 #define is_digit_char(x) (digit_chars[(unsigned char) x])
164 /* put here all non-digit non-letter charcters that may occur in an operand */
165 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
167 static char *ordinal_names[] = {"first", "second", "third"}; /* for printfs */
169 /* md_assemble() always leaves the strings it's passed unaltered. To
170 effect this we maintain a stack of saved characters that we've smashed
171 with '\0's (indicating end of strings for various sub-fields of the
172 assembler instruction). */
173 static char save_stack[32];
174 static char *save_stack_p; /* stack pointer */
175 #define END_STRING_AND_SAVE(s) *save_stack_p++ = *s; *s = '\0'
176 #define RESTORE_END_STRING(s) *s = *--save_stack_p
178 /* The instruction we're assembling. */
181 /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
182 static expressionS disp_expressions[2], im_expressions[2];
184 /* pointers to ebp & esp entries in reg_hash hash table */
185 static reg_entry *ebp, *esp;
187 static int this_operand; /* current operand we are working on */
189 static int flag_do_long_jump; /* FIXME what does this do? */
191 static int flag_16bit_code; /* 1 if we're writing 16-bit code, 0 if 32-bit */
193 /* Interface to relax_segment.
194 There are 2 relax states for 386 jump insns: one for conditional &
195 one for unconditional jumps. This is because the these two types
196 of jumps add different sizes to frags when we're figuring out what
197 sort of jump to choose to reach a given label. */
200 #define COND_JUMP 1 /* conditional jump */
201 #define UNCOND_JUMP 2 /* unconditional jump */
206 #define UNKNOWN_SIZE 3
210 #define INLINE __inline__
216 #define ENCODE_RELAX_STATE(type,size) \
217 ((relax_substateT)((type<<2) | (size)))
218 #define SIZE_FROM_RELAX_STATE(s) \
219 ( (((s) & 0x3) == BYTE ? 1 : (((s) & 0x3) == WORD ? 2 : 4)) )
221 const relax_typeS md_relax_table[] =
224 1) most positive reach of this state,
225 2) most negative reach of this state,
226 3) how many bytes this mode will add to the size of the current frag
227 4) which index into the table to try if we can't fit into this one.
234 /* For now we don't use word displacement jumps; they may be
236 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
237 /* word conditionals add 3 bytes to frag:
238 2 opcode prefix; 1 displacement bytes */
239 {32767 + 2, -32768 + 2, 3, ENCODE_RELAX_STATE (COND_JUMP, DWORD)},
240 /* dword conditionals adds 4 bytes to frag:
241 1 opcode prefix; 3 displacement bytes */
245 {127 + 1, -128 + 1, 0, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
246 /* word jmp adds 2 bytes to frag:
247 1 opcode prefix; 1 displacement bytes */
248 {32767 + 2, -32768 + 2, 2, ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD)},
249 /* dword jmp adds 3 bytes to frag:
250 0 opcode prefix; 3 displacement bytes */
258 i386_align_code (fragP, count)
262 /* Various efficient no-op patterns for aligning code labels. */
263 /* Note: Don't try to assemble the instructions in the comments. */
264 /* 0L and 0w are not legal */
265 static const char f32_1[] =
267 static const char f32_2[] =
268 {0x89,0xf6}; /* movl %esi,%esi */
269 static const char f32_3[] =
270 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
271 static const char f32_4[] =
272 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
273 static const char f32_5[] =
275 0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
276 static const char f32_6[] =
277 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
278 static const char f32_7[] =
279 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
280 static const char f32_8[] =
282 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
283 static const char f32_9[] =
284 {0x89,0xf6, /* movl %esi,%esi */
285 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
286 static const char f32_10[] =
287 {0x8d,0x76,0x00, /* leal 0(%esi),%esi */
288 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
289 static const char f32_11[] =
290 {0x8d,0x74,0x26,0x00, /* leal 0(%esi,1),%esi */
291 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
292 static const char f32_12[] =
293 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
294 0x8d,0xbf,0x00,0x00,0x00,0x00}; /* leal 0L(%edi),%edi */
295 static const char f32_13[] =
296 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
297 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
298 static const char f32_14[] =
299 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00, /* leal 0L(%esi,1),%esi */
300 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
301 static const char f32_15[] =
302 {0xeb,0x0d,0x90,0x90,0x90,0x90,0x90, /* jmp .+15; lotsa nops */
303 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
304 static const char f16_4[] =
305 {0x8d,0xb6,0x00,0x00}; /* lea 0w(%si),%si */
306 static const char f16_5[] =
308 0x8d,0xb6,0x00,0x00}; /* lea 0w(%si),%si */
309 static const char f16_6[] =
310 {0x89,0xf6, /* mov %si,%si */
311 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
312 static const char f16_7[] =
313 {0x8d,0x76,0x00, /* lea 0(%si),%si */
314 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
315 static const char f16_8[] =
316 {0x8d,0xb6,0x00,0x00, /* lea 0w(%si),%si */
317 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
318 static const char *const f32_patt[] = {
319 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
320 f32_9, f32_10, f32_11, f32_12, f32_13, f32_14, f32_15
322 static const char *const f16_patt[] = {
323 f32_1, f32_2, f32_3, f16_4, f16_5, f16_6, f16_7, f16_8,
324 f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
327 if (count > 0 && count <= 15)
331 memcpy(fragP->fr_literal + fragP->fr_fix,
332 f16_patt[count - 1], count);
333 if (count > 8) /* adjust jump offset */
334 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
337 memcpy(fragP->fr_literal + fragP->fr_fix,
338 f32_patt[count - 1], count);
339 fragP->fr_var = count;
343 static char *output_invalid PARAMS ((int c));
344 static int i386_operand PARAMS ((char *operand_string));
345 static reg_entry *parse_register PARAMS ((char *reg_string));
347 static void s_bss PARAMS ((int));
350 symbolS *GOT_symbol; /* Pre-defined "__GLOBAL_OFFSET_TABLE" */
352 static INLINE unsigned long
353 mode_from_disp_size (t)
356 return (t & Disp8) ? 1 : (t & Disp32) ? 2 : 0;
361 /* convert opcode suffix ('b' 'w' 'l' typically) into type specifier */
363 static INLINE unsigned long
364 opcode_suffix_to_type (s)
367 return (s == BYTE_OPCODE_SUFFIX
368 ? Byte : (s == WORD_OPCODE_SUFFIX
370 } /* opcode_suffix_to_type() */
374 fits_in_signed_byte (num)
377 return (num >= -128) && (num <= 127);
378 } /* fits_in_signed_byte() */
381 fits_in_unsigned_byte (num)
384 return (num & 0xff) == num;
385 } /* fits_in_unsigned_byte() */
388 fits_in_unsigned_word (num)
391 return (num & 0xffff) == num;
392 } /* fits_in_unsigned_word() */
395 fits_in_signed_word (num)
398 return (-32768 <= num) && (num <= 32767);
399 } /* fits_in_signed_word() */
402 smallest_imm_type (num)
406 /* This code is disabled because all the Imm1 forms in the opcode table
407 are slower on the i486, and they're the versions with the implicitly
408 specified single-position displacement, which has another syntax if
409 you really want to use that form. If you really prefer to have the
410 one-byte-shorter Imm1 form despite these problems, re-enable this
413 return Imm1 | Imm8 | Imm8S | Imm16 | Imm32;
415 return (fits_in_signed_byte (num)
416 ? (Imm8S | Imm8 | Imm16 | Imm32)
417 : fits_in_unsigned_byte (num)
418 ? (Imm8 | Imm16 | Imm32)
419 : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
422 } /* smallest_imm_type() */
425 set_16bit_code_flag (new_16bit_code_flag)
426 int new_16bit_code_flag;
428 flag_16bit_code = new_16bit_code_flag;
431 const pseudo_typeS md_pseudo_table[] =
436 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
437 {"align", s_align_bytes, 0},
439 {"align", s_align_ptwo, 0},
441 {"ffloat", float_cons, 'f'},
442 {"dfloat", float_cons, 'd'},
443 {"tfloat", float_cons, 'x'},
445 {"noopt", s_ignore, 0},
446 {"optim", s_ignore, 0},
447 {"code16", set_16bit_code_flag, 1},
448 {"code32", set_16bit_code_flag, 0},
452 /* for interface with expression () */
453 extern char *input_line_pointer;
455 /* obstack for constructing various things in md_begin */
458 /* hash table for opcode lookup */
459 static struct hash_control *op_hash;
460 /* hash table for register lookup */
461 static struct hash_control *reg_hash;
462 /* hash table for prefix lookup */
463 static struct hash_control *prefix_hash;
469 const char *hash_err;
471 obstack_begin (&o, 4096);
473 /* initialize op_hash hash table */
474 op_hash = hash_new ();
477 register const template *optab;
478 register templates *core_optab;
481 optab = i386_optab; /* setup for loop */
482 prev_name = optab->name;
483 obstack_grow (&o, optab, sizeof (template));
484 core_optab = (templates *) xmalloc (sizeof (templates));
486 for (optab++; optab < i386_optab_end; optab++)
488 if (!strcmp (optab->name, prev_name))
490 /* same name as before --> append to current template list */
491 obstack_grow (&o, optab, sizeof (template));
495 /* different name --> ship out current template list;
496 add to hash table; & begin anew */
497 /* Note: end must be set before start! since obstack_next_free
498 changes upon opstack_finish */
499 core_optab->end = (template *) obstack_next_free (&o);
500 core_optab->start = (template *) obstack_finish (&o);
501 hash_err = hash_insert (op_hash, prev_name, (char *) core_optab);
505 as_fatal ("Internal Error: Can't hash %s: %s", prev_name,
508 prev_name = optab->name;
509 core_optab = (templates *) xmalloc (sizeof (templates));
510 obstack_grow (&o, optab, sizeof (template));
515 /* initialize reg_hash hash table */
516 reg_hash = hash_new ();
518 register const reg_entry *regtab;
520 for (regtab = i386_regtab; regtab < i386_regtab_end; regtab++)
522 hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
528 esp = (reg_entry *) hash_find (reg_hash, "esp");
529 ebp = (reg_entry *) hash_find (reg_hash, "ebp");
531 /* initialize reg_hash hash table */
532 prefix_hash = hash_new ();
534 register const prefix_entry *prefixtab;
536 for (prefixtab = i386_prefixtab;
537 prefixtab < i386_prefixtab_end; prefixtab++)
539 hash_err = hash_insert (prefix_hash, prefixtab->prefix_name,
546 /* fill in lexical tables: opcode_chars, operand_chars, space_chars */
551 for (c = 0; c < 256; c++)
553 if (islower (c) || isdigit (c))
556 register_chars[c] = c;
558 else if (isupper (c))
560 opcode_chars[c] = tolower (c);
561 register_chars[c] = opcode_chars[c];
563 else if (c == PREFIX_SEPERATOR)
567 else if (c == ')' || c == '(')
569 register_chars[c] = c;
572 if (isupper (c) || islower (c) || isdigit (c))
573 operand_chars[c] = c;
575 if (isdigit (c) || c == '-')
578 if (isalpha (c) || c == '_' || c == '.' || isdigit (c))
579 identifier_chars[c] = c;
582 identifier_chars['@'] = '@';
585 if (c == ' ' || c == '\t')
589 for (p = operand_special_chars; *p != '\0'; p++)
590 operand_chars[(unsigned char) *p] = *p;
593 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
594 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
596 record_alignment (text_section, 2);
597 record_alignment (data_section, 2);
598 record_alignment (bss_section, 2);
604 i386_print_statistics (file)
607 hash_print_statistics (file, "i386 opcode", op_hash);
608 hash_print_statistics (file, "i386 register", reg_hash);
609 hash_print_statistics (file, "i386 prefix", prefix_hash);
615 /* debugging routines for md_assemble */
616 static void pi PARAMS ((char *, i386_insn *));
617 static void pte PARAMS ((template *));
618 static void pt PARAMS ((unsigned int));
619 static void pe PARAMS ((expressionS *));
620 static void ps PARAMS ((symbolS *));
627 register template *p;
630 fprintf (stdout, "%s: template ", line);
632 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x",
633 x->rm.mode, x->rm.reg, x->rm.regmem);
634 fprintf (stdout, " base %x index %x scale %x\n",
635 x->bi.base, x->bi.index, x->bi.scale);
636 for (i = 0; i < x->operands; i++)
638 fprintf (stdout, " #%d: ", i + 1);
640 fprintf (stdout, "\n");
642 & (Reg | SReg2 | SReg3 | Control | Debug | Test | RegMMX))
643 fprintf (stdout, "%s\n", x->regs[i]->reg_name);
644 if (x->types[i] & Imm)
646 if (x->types[i] & (Disp | Abs))
656 fprintf (stdout, " %d operands ", t->operands);
657 fprintf (stdout, "opcode %x ",
659 if (t->extension_opcode != None)
660 fprintf (stdout, "ext %x ", t->extension_opcode);
661 if (t->opcode_modifier & D)
662 fprintf (stdout, "D");
663 if (t->opcode_modifier & W)
664 fprintf (stdout, "W");
665 fprintf (stdout, "\n");
666 for (i = 0; i < t->operands; i++)
668 fprintf (stdout, " #%d type ", i + 1);
669 pt (t->operand_types[i]);
670 fprintf (stdout, "\n");
678 fprintf (stdout, " operation %d\n", e->X_op);
679 fprintf (stdout, " add_number %d (%x)\n",
680 e->X_add_number, e->X_add_number);
683 fprintf (stdout, " add_symbol ");
684 ps (e->X_add_symbol);
685 fprintf (stdout, "\n");
689 fprintf (stdout, " op_symbol ");
691 fprintf (stdout, "\n");
699 fprintf (stdout, "%s type %s%s",
701 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
702 segment_name (S_GET_SEGMENT (s)));
723 { BaseIndex, "BaseIndex" },
733 { InOutPortReg, "InOutPortReg" },
734 { ShiftCount, "ShiftCount" },
736 { Control, "control reg" },
737 { Test, "test reg" },
738 { FloatReg, "FReg" },
739 { FloatAcc, "FAcc" },
740 { JumpAbsolute, "Jump Absolute" },
749 register struct type_name *ty;
753 fprintf (stdout, "Unknown");
757 for (ty = type_names; ty->mask; ty++)
759 fprintf (stdout, "%s, ", ty->tname);
764 #endif /* DEBUG386 */
767 static bfd_reloc_code_real_type
768 reloc (size, pcrel, other)
771 bfd_reloc_code_real_type other;
773 if (other != NO_RELOC) return other;
778 case 1: return BFD_RELOC_8_PCREL;
779 case 2: return BFD_RELOC_16_PCREL;
780 case 4: return BFD_RELOC_32_PCREL;
785 case 1: return BFD_RELOC_8;
786 case 2: return BFD_RELOC_16;
787 case 4: return BFD_RELOC_32;
790 as_bad ("Can not do %d byte %srelocation", size,
791 pcrel ? "pc-relative " : "");
792 return BFD_RELOC_NONE;
796 * Here we decide which fixups can be adjusted to make them relative to
797 * the beginning of the section instead of the symbol. Basically we need
798 * to make sure that the dynamic relocations are done correctly, so in
799 * some cases we force the original symbol to be used.
802 tc_i386_fix_adjustable(fixP)
806 /* Prevent all adjustments to global symbols. */
807 if (S_IS_EXTERN (fixP->fx_addsy))
809 if (S_IS_WEAK (fixP->fx_addsy))
811 #endif /* ! defined (OBJ_AOUT) */
812 /* adjust_reloc_syms doesn't know about the GOT */
813 if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
814 || fixP->fx_r_type == BFD_RELOC_386_PLT32
815 || fixP->fx_r_type == BFD_RELOC_386_GOT32)
820 #define reloc(SIZE,PCREL,OTHER) 0
821 #define BFD_RELOC_32 0
822 #define BFD_RELOC_32_PCREL 0
823 #define BFD_RELOC_386_PLT32 0
824 #define BFD_RELOC_386_GOT32 0
825 #define BFD_RELOC_386_GOTOFF 0
828 /* This is the guts of the machine-dependent assembler. LINE points to a
829 machine dependent instruction. This function is supposed to emit
830 the frags/bytes it assembles to. */
836 /* Holds template once we've found it. */
839 /* Count the size of the instruction generated. */
842 /* Possible templates for current insn */
843 templates *current_templates = (templates *) 0;
847 /* Initialize globals. */
848 memset (&i, '\0', sizeof (i));
849 for (j = 0; j < MAX_OPERANDS; j++)
850 i.disp_reloc[j] = NO_RELOC;
851 memset (disp_expressions, '\0', sizeof (disp_expressions));
852 memset (im_expressions, '\0', sizeof (im_expressions));
853 save_stack_p = save_stack; /* reset stack pointer */
855 /* Fist parse an opcode & call i386_operand for the operands.
856 We assume that the scrubber has arranged it so that line[0] is the valid
857 start of a (possibly prefixed) opcode. */
861 /* 1 if operand is pending after ','. */
862 unsigned int expecting_operand = 0;
863 /* 1 if we found a prefix only acceptable with string insns. */
864 unsigned int expecting_string_instruction = 0;
865 /* Non-zero if operand parens not balanced. */
866 unsigned int paren_not_balanced;
867 char *token_start = l;
869 while (!is_space_char (*l) && *l != END_OF_INSN)
871 if (!is_opcode_char (*l))
873 as_bad ("invalid character %s in opcode", output_invalid (*l));
876 else if (*l != PREFIX_SEPERATOR)
878 *l = opcode_chars[(unsigned char) *l]; /* fold case of opcodes */
883 /* This opcode's got a prefix. */
885 prefix_entry *prefix;
887 if (l == token_start)
889 as_bad ("expecting prefix; got nothing");
892 END_STRING_AND_SAVE (l);
893 prefix = (prefix_entry *) hash_find (prefix_hash, token_start);
896 as_bad ("no such opcode prefix ('%s')", token_start);
899 RESTORE_END_STRING (l);
900 /* check for repeated prefix */
901 for (q = 0; q < i.prefixes; q++)
902 if (i.prefix[q] == prefix->prefix_code)
904 as_bad ("same prefix used twice; you don't really want this!");
907 if (i.prefixes == MAX_PREFIXES)
909 as_bad ("too many opcode prefixes");
912 i.prefix[i.prefixes++] = prefix->prefix_code;
913 if (prefix->prefix_code == REPE || prefix->prefix_code == REPNE)
914 expecting_string_instruction = 1;
915 /* skip past PREFIX_SEPERATOR and reset token_start */
919 END_STRING_AND_SAVE (l);
920 if (token_start == l)
922 as_bad ("expecting opcode; got nothing");
926 /* Lookup insn in hash; try intel & att naming conventions if appropriate;
927 that is: we only use the opcode suffix 'b' 'w' or 'l' if we need to. */
928 current_templates = (templates *) hash_find (op_hash, token_start);
929 if (!current_templates)
931 int last_index = strlen (token_start) - 1;
932 char last_char = token_start[last_index];
935 case DWORD_OPCODE_SUFFIX:
936 case WORD_OPCODE_SUFFIX:
937 case BYTE_OPCODE_SUFFIX:
938 token_start[last_index] = '\0';
939 current_templates = (templates *) hash_find (op_hash, token_start);
940 token_start[last_index] = last_char;
941 i.suffix = last_char;
943 if (!current_templates)
945 as_bad ("no such 386 instruction: `%s'", token_start);
949 RESTORE_END_STRING (l);
951 /* check for rep/repne without a string instruction */
952 if (expecting_string_instruction &&
953 !IS_STRING_INSTRUCTION (current_templates->
956 as_bad ("expecting string instruction after rep/repne");
960 /* There may be operands to parse. */
961 if (*l != END_OF_INSN &&
962 /* For string instructions, we ignore any operands if given. This
963 kludges, for example, 'rep/movsb %ds:(%esi), %es:(%edi)' where
964 the operands are always going to be the same, and are not really
965 encoded in machine code. */
966 !IS_STRING_INSTRUCTION (current_templates->
972 /* skip optional white space before operand */
973 while (!is_operand_char (*l) && *l != END_OF_INSN)
975 if (!is_space_char (*l))
977 as_bad ("invalid character %s before %s operand",
979 ordinal_names[i.operands]);
984 token_start = l; /* after white space */
985 paren_not_balanced = 0;
986 while (paren_not_balanced || *l != ',')
988 if (*l == END_OF_INSN)
990 if (paren_not_balanced)
992 as_bad ("unbalanced parenthesis in %s operand.",
993 ordinal_names[i.operands]);
997 break; /* we are done */
999 else if (!is_operand_char (*l) && !is_space_char (*l))
1001 as_bad ("invalid character %s in %s operand",
1002 output_invalid (*l),
1003 ordinal_names[i.operands]);
1007 ++paren_not_balanced;
1009 --paren_not_balanced;
1012 if (l != token_start)
1013 { /* yes, we've read in another operand */
1014 unsigned int operand_ok;
1015 this_operand = i.operands++;
1016 if (i.operands > MAX_OPERANDS)
1018 as_bad ("spurious operands; (%d operands/instruction max)",
1022 /* now parse operand adding info to 'i' as we go along */
1023 END_STRING_AND_SAVE (l);
1024 operand_ok = i386_operand (token_start);
1025 RESTORE_END_STRING (l); /* restore old contents */
1031 if (expecting_operand)
1033 expecting_operand_after_comma:
1034 as_bad ("expecting operand after ','; got nothing");
1039 as_bad ("expecting operand before ','; got nothing");
1044 /* now *l must be either ',' or END_OF_INSN */
1047 if (*++l == END_OF_INSN)
1048 { /* just skip it, if it's \n complain */
1049 goto expecting_operand_after_comma;
1051 expecting_operand = 1;
1054 while (*l != END_OF_INSN); /* until we get end of insn */
1058 /* Now we've parsed the opcode into a set of templates, and have the
1061 Next, we find a template that matches the given insn,
1062 making sure the overlap of the given operands types is consistent
1063 with the template operand types. */
1065 #define MATCH(overlap,given_type) \
1067 (((overlap & (JumpAbsolute|BaseIndex|Mem8)) \
1068 == (given_type & (JumpAbsolute|BaseIndex|Mem8))) \
1069 || (overlap == InOutPortReg)))
1072 /* If m0 and m1 are register matches they must be consistent
1073 with the expected operand types t0 and t1.
1074 That is, if both m0 & m1 are register matches
1075 i.e. ( ((m0 & (Reg)) && (m1 & (Reg)) ) ?
1076 then, either 1. or 2. must be true:
1077 1. the expected operand type register overlap is null:
1078 (t0 & t1 & Reg) == 0
1080 the given register overlap is null:
1081 (m0 & m1 & Reg) == 0
1082 2. the expected operand type register overlap == the given
1083 operand type overlap: (t0 & t1 & m0 & m1 & Reg).
1085 #define CONSISTENT_REGISTER_MATCH(m0, m1, t0, t1) \
1086 ( ((m0 & (Reg)) && (m1 & (Reg))) ? \
1087 ( ((t0 & t1 & (Reg)) == 0 && (m0 & m1 & (Reg)) == 0) || \
1088 ((t0 & t1) & (m0 & m1) & (Reg)) \
1091 register unsigned int overlap0, overlap1;
1093 unsigned int overlap2;
1094 unsigned int found_reverse_match;
1096 overlap0 = overlap1 = overlap2 = found_reverse_match = 0;
1097 for (t = current_templates->start;
1098 t < current_templates->end;
1101 /* must have right number of operands */
1102 if (i.operands != t->operands)
1104 else if (!t->operands)
1105 break; /* 0 operands always matches */
1107 overlap0 = i.types[0] & t->operand_types[0];
1108 switch (t->operands)
1111 if (!MATCH (overlap0, i.types[0]))
1116 overlap1 = i.types[1] & t->operand_types[1];
1117 if (!MATCH (overlap0, i.types[0]) ||
1118 !MATCH (overlap1, i.types[1]) ||
1119 !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
1120 t->operand_types[0],
1121 t->operand_types[1]))
1124 /* check if other direction is valid ... */
1125 if (!(t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS))
1128 /* try reversing direction of operands */
1129 overlap0 = i.types[0] & t->operand_types[1];
1130 overlap1 = i.types[1] & t->operand_types[0];
1131 if (!MATCH (overlap0, i.types[0]) ||
1132 !MATCH (overlap1, i.types[1]) ||
1133 !CONSISTENT_REGISTER_MATCH (overlap0, overlap1,
1134 t->operand_types[0],
1135 t->operand_types[1]))
1137 /* does not match either direction */
1140 /* found a reverse match here -- slip through */
1141 /* found_reverse_match holds which of D or FloatD we've found */
1142 found_reverse_match = t->opcode_modifier & COMES_IN_BOTH_DIRECTIONS;
1143 } /* endif: not forward match */
1144 /* found either forward/reverse 2 operand match here */
1145 if (t->operands == 3)
1147 overlap2 = i.types[2] & t->operand_types[2];
1148 if (!MATCH (overlap2, i.types[2]) ||
1149 !CONSISTENT_REGISTER_MATCH (overlap0, overlap2,
1150 t->operand_types[0],
1151 t->operand_types[2]) ||
1152 !CONSISTENT_REGISTER_MATCH (overlap1, overlap2,
1153 t->operand_types[1],
1154 t->operand_types[2]))
1157 /* found either forward/reverse 2 or 3 operand match here:
1158 slip through to break */
1160 break; /* we've found a match; break out of loop */
1161 } /* for (t = ... */
1162 if (t == current_templates->end)
1163 { /* we found no match */
1164 as_bad ("operands given don't match any known 386 instruction");
1168 /* Copy the template we found (we may change it!). */
1170 t = &i.tm; /* alter new copy of template */
1172 /* If the matched instruction specifies an explicit opcode suffix,
1173 use it - and make sure none has already been specified. */
1174 if (t->opcode_modifier & (Data16|Data32))
1178 as_bad ("extraneous opcode suffix given");
1181 if (t->opcode_modifier & Data16)
1182 i.suffix = WORD_OPCODE_SUFFIX;
1184 i.suffix = DWORD_OPCODE_SUFFIX;
1187 /* If there's no opcode suffix we try to invent one based on register
1189 if (!i.suffix && i.reg_operands)
1191 /* We take i.suffix from the LAST register operand specified. This
1192 assumes that the last register operands is the destination register
1195 for (op = 0; op < MAX_OPERANDS; op++)
1196 if (i.types[op] & Reg)
1198 i.suffix = ((i.types[op] & Reg8) ? BYTE_OPCODE_SUFFIX :
1199 (i.types[op] & Reg16) ? WORD_OPCODE_SUFFIX :
1200 DWORD_OPCODE_SUFFIX);
1203 else if (i.suffix != 0
1204 && i.reg_operands != 0
1205 && (i.types[i.operands - 1] & Reg) != 0)
1209 /* If the last operand is a register, make sure it is
1210 compatible with the suffix. */
1217 case BYTE_OPCODE_SUFFIX:
1218 /* If this is an eight bit register, it's OK. If it's the
1219 16 or 32 bit version of an eight bit register, we will
1220 just use the low portion, and that's OK too. */
1221 if ((i.types[i.operands - 1] & Reg8) == 0
1222 && i.regs[i.operands - 1]->reg_num >= 4)
1225 case WORD_OPCODE_SUFFIX:
1226 case DWORD_OPCODE_SUFFIX:
1227 /* We don't insist on the presence or absence of the e
1228 prefix on the register, but we reject eight bit
1230 if ((i.types[i.operands - 1] & Reg8) != 0)
1234 as_bad ("register does not match opcode suffix");
1237 /* Make still unresolved immediate matches conform to size of immediate
1238 given in i.suffix. Note: overlap2 cannot be an immediate!
1240 if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32))
1241 && overlap0 != Imm8 && overlap0 != Imm8S
1242 && overlap0 != Imm16 && overlap0 != Imm32)
1246 as_bad ("no opcode suffix given; can't determine immediate size");
1249 overlap0 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
1250 (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
1252 if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32))
1253 && overlap1 != Imm8 && overlap1 != Imm8S
1254 && overlap1 != Imm16 && overlap1 != Imm32)
1258 as_bad ("no opcode suffix given; can't determine immediate size");
1261 overlap1 &= (i.suffix == BYTE_OPCODE_SUFFIX ? (Imm8 | Imm8S) :
1262 (i.suffix == WORD_OPCODE_SUFFIX ? Imm16 : Imm32));
1265 i.types[0] = overlap0;
1266 i.types[1] = overlap1;
1267 i.types[2] = overlap2;
1269 if (overlap0 & ImplicitRegister)
1271 if (overlap1 & ImplicitRegister)
1273 if (overlap2 & ImplicitRegister)
1275 if (overlap0 & Imm1)
1276 i.imm_operands = 0; /* kludge for shift insns */
1278 if (found_reverse_match)
1281 save = t->operand_types[0];
1282 t->operand_types[0] = t->operand_types[1];
1283 t->operand_types[1] = save;
1286 /* Finalize opcode. First, we change the opcode based on the operand
1287 size given by i.suffix: we never have to change things for byte insns,
1288 or when no opcode suffix is need to size the operands. */
1290 if (!i.suffix && (t->opcode_modifier & W))
1292 as_bad ("no opcode suffix given and no register operands; can't size instruction");
1296 if (i.suffix && i.suffix != BYTE_OPCODE_SUFFIX)
1298 /* Select between byte and word/dword operations. */
1299 if (t->opcode_modifier & W)
1300 t->base_opcode |= W;
1301 /* Now select between word & dword operations via the
1302 operand size prefix. */
1303 if ((i.suffix == WORD_OPCODE_SUFFIX) ^ flag_16bit_code)
1305 if (i.prefixes == MAX_PREFIXES)
1307 as_bad ("%d prefixes given and 'w' opcode suffix gives too many prefixes",
1311 i.prefix[i.prefixes++] = WORD_PREFIX_OPCODE;
1315 /* For insns with operands there are more diddles to do to the opcode. */
1318 /* Default segment register this instruction will use
1319 for memory accesses. 0 means unknown.
1320 This is only for optimizing out unnecessary segment overrides. */
1321 const seg_entry *default_seg = 0;
1323 /* True if this instruction uses a memory addressing mode,
1324 and therefore may need an address-size prefix. */
1325 int uses_mem_addrmode = 0;
1328 /* If we found a reverse match we must alter the opcode direction bit
1329 found_reverse_match holds bit to set (different for int &
1332 if (found_reverse_match)
1334 t->base_opcode |= found_reverse_match;
1337 /* The imul $imm, %reg instruction is converted into
1338 imul $imm, %reg, %reg. */
1339 if (t->opcode_modifier & imulKludge)
1341 /* Pretend we saw the 3 operand case. */
1342 i.regs[2] = i.regs[1];
1346 /* The clr %reg instruction is converted into xor %reg, %reg. */
1347 if (t->opcode_modifier & iclrKludge)
1349 i.regs[1] = i.regs[0];
1353 /* Certain instructions expect the destination to be in the i.rm.reg
1354 field. This is by far the exceptional case. For these
1355 instructions, if the source operand is a register, we must reverse
1356 the i.rm.reg and i.rm.regmem fields. We accomplish this by faking
1357 that the two register operands were given in the reverse order. */
1358 if ((t->opcode_modifier & ReverseRegRegmem) && i.reg_operands == 2)
1360 unsigned int first_reg_operand = (i.types[0] & Reg) ? 0 : 1;
1361 unsigned int second_reg_operand = first_reg_operand + 1;
1362 reg_entry *tmp = i.regs[first_reg_operand];
1363 i.regs[first_reg_operand] = i.regs[second_reg_operand];
1364 i.regs[second_reg_operand] = tmp;
1367 if (t->opcode_modifier & ShortForm)
1369 /* The register or float register operand is in operand 0 or 1. */
1370 unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
1371 /* Register goes in low 3 bits of opcode. */
1372 t->base_opcode |= i.regs[op]->reg_num;
1374 else if (t->opcode_modifier & ShortFormW)
1376 /* Short form with 0x8 width bit. Register is always dest. operand */
1377 t->base_opcode |= i.regs[1]->reg_num;
1378 if (i.suffix == WORD_OPCODE_SUFFIX ||
1379 i.suffix == DWORD_OPCODE_SUFFIX)
1380 t->base_opcode |= 0x8;
1382 else if (t->opcode_modifier & Seg2ShortForm)
1384 if (t->base_opcode == POP_SEG_SHORT && i.regs[0]->reg_num == 1)
1386 as_bad ("you can't 'pop cs' on the 386.");
1389 t->base_opcode |= (i.regs[0]->reg_num << 3);
1391 else if (t->opcode_modifier & Seg3ShortForm)
1393 /* 'push %fs' is 0x0fa0; 'pop %fs' is 0x0fa1.
1394 'push %gs' is 0x0fa8; 'pop %fs' is 0x0fa9.
1395 So, only if i.regs[0]->reg_num == 5 (%gs) do we need
1396 to change the opcode. */
1397 if (i.regs[0]->reg_num == 5)
1398 t->base_opcode |= 0x08;
1400 else if ((t->base_opcode & ~DW) == MOV_AX_DISP32)
1402 /* This is a special non-modrm instruction
1403 that addresses memory with a 32-bit displacement mode anyway,
1404 and thus requires an address-size prefix if in 16-bit mode. */
1405 uses_mem_addrmode = 1;
1408 else if (t->opcode_modifier & Modrm)
1410 /* The opcode is completed (modulo t->extension_opcode which must
1411 be put into the modrm byte.
1412 Now, we make the modrm & index base bytes based on all the info
1415 /* i.reg_operands MUST be the number of real register operands;
1416 implicit registers do not count. */
1417 if (i.reg_operands == 2)
1419 unsigned int source, dest;
1420 source = ((i.types[0]
1431 /* We must be careful to make sure that all
1432 segment/control/test/debug/MMX registers go into
1433 the i.rm.reg field (despite the whether they are
1434 source or destination operands). */
1435 if (i.regs[dest]->reg_type
1436 & (SReg2 | SReg3 | Control | Debug | Test | RegMMX))
1438 i.rm.reg = i.regs[dest]->reg_num;
1439 i.rm.regmem = i.regs[source]->reg_num;
1443 i.rm.reg = i.regs[source]->reg_num;
1444 i.rm.regmem = i.regs[dest]->reg_num;
1448 { /* if it's not 2 reg operands... */
1451 unsigned int fake_zero_displacement = 0;
1452 unsigned int op = (i.types[0] & Mem) ? 0 : ((i.types[1] & Mem) ? 1 : 2);
1454 /* Encode memory operand into modrm byte and base index
1457 if (i.base_reg == esp && !i.index_reg)
1459 /* <disp>(%esp) becomes two byte modrm with no index
1461 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1462 i.rm.mode = mode_from_disp_size (i.types[op]);
1463 i.bi.base = ESP_REG_NUM;
1464 i.bi.index = NO_INDEX_REGISTER;
1465 i.bi.scale = 0; /* Must be zero! */
1467 else if (i.base_reg == ebp && !i.index_reg)
1469 if (!(i.types[op] & Disp))
1471 /* Must fake a zero byte displacement. There is
1472 no direct way to code '(%ebp)' directly. */
1473 fake_zero_displacement = 1;
1474 /* fake_zero_displacement code does not set this. */
1475 i.types[op] |= Disp8;
1477 i.rm.mode = mode_from_disp_size (i.types[op]);
1478 i.rm.regmem = EBP_REG_NUM;
1480 else if (!i.base_reg && (i.types[op] & BaseIndex))
1482 /* There are three cases here.
1483 Case 1: '<32bit disp>(,1)' -- indirect absolute.
1484 (Same as cases 2 & 3 with NO index register)
1485 Case 2: <32bit disp> (,<index>) -- no base register with disp
1486 Case 3: (, <index>) --- no base register;
1487 no disp (must add 32bit 0 disp). */
1488 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1489 i.rm.mode = 0; /* 32bit mode */
1490 i.bi.base = NO_BASE_REGISTER;
1491 i.types[op] &= ~Disp;
1492 i.types[op] |= Disp32; /* Must be 32bit! */
1494 { /* case 2 or case 3 */
1495 i.bi.index = i.index_reg->reg_num;
1496 i.bi.scale = i.log2_scale_factor;
1497 if (i.disp_operands == 0)
1498 fake_zero_displacement = 1; /* case 3 */
1502 i.bi.index = NO_INDEX_REGISTER;
1506 else if (i.disp_operands && !i.base_reg && !i.index_reg)
1508 /* Operand is just <32bit disp> */
1509 i.rm.regmem = EBP_REG_NUM;
1511 i.types[op] &= ~Disp;
1512 i.types[op] |= Disp32;
1516 /* It's not a special case; rev'em up. */
1517 i.rm.regmem = i.base_reg->reg_num;
1518 i.rm.mode = mode_from_disp_size (i.types[op]);
1521 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
1522 i.bi.base = i.base_reg->reg_num;
1523 i.bi.index = i.index_reg->reg_num;
1524 i.bi.scale = i.log2_scale_factor;
1525 if (i.base_reg == ebp && i.disp_operands == 0)
1527 fake_zero_displacement = 1;
1528 i.types[op] |= Disp8;
1529 i.rm.mode = mode_from_disp_size (i.types[op]);
1533 if (fake_zero_displacement)
1535 /* Fakes a zero displacement assuming that i.types[op]
1536 holds the correct displacement size. */
1537 exp = &disp_expressions[i.disp_operands++];
1539 exp->X_op = O_constant;
1540 exp->X_add_number = 0;
1541 exp->X_add_symbol = (symbolS *) 0;
1542 exp->X_op_symbol = (symbolS *) 0;
1545 /* Find the default segment for the memory operand.
1546 Used to optimize out explicit segment specifications. */
1549 unsigned int seg_index;
1551 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING)
1553 seg_index = (i.rm.mode << 3) | i.bi.base;
1554 default_seg = two_byte_segment_defaults[seg_index];
1558 seg_index = (i.rm.mode << 3) | i.rm.regmem;
1559 default_seg = one_byte_segment_defaults[seg_index];
1564 /* Fill in i.rm.reg or i.rm.regmem field with register
1565 operand (if any) based on
1566 t->extension_opcode. Again, we must be careful to
1567 make sure that segment/control/debug/test/MMX
1568 registers are coded into the i.rm.reg field. */
1573 & (Reg | SReg2 | SReg3 | Control | Debug
1577 & (Reg | SReg2 | SReg3 | Control | Debug
1581 /* If there is an extension opcode to put here, the
1582 register number must be put into the regmem field. */
1583 if (t->extension_opcode != None)
1584 i.rm.regmem = i.regs[op]->reg_num;
1586 i.rm.reg = i.regs[op]->reg_num;
1588 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2
1589 we must set it to 3 to indicate this is a register
1590 operand int the regmem field */
1591 if (!i.mem_operands)
1595 /* Fill in i.rm.reg field with extension opcode (if any). */
1596 if (t->extension_opcode != None)
1597 i.rm.reg = t->extension_opcode;
1601 uses_mem_addrmode = 1;
1604 /* GAS currently doesn't support 16-bit memory addressing modes at all,
1605 so if we're writing 16-bit code and using a memory addressing mode,
1606 always spew out an address size prefix. */
1607 if (uses_mem_addrmode && flag_16bit_code)
1609 if (i.prefixes == MAX_PREFIXES)
1611 as_bad ("%d prefixes given and address size override gives too many prefixes",
1615 i.prefix[i.prefixes++] = ADDR_PREFIX_OPCODE;
1618 /* If a segment was explicitly specified,
1619 and the specified segment is not the default,
1620 use an opcode prefix to select it.
1621 If we never figured out what the default segment is,
1622 then default_seg will be zero at this point,
1623 and the specified segment prefix will always be used. */
1624 if ((i.seg) && (i.seg != default_seg))
1626 if (i.prefixes == MAX_PREFIXES)
1628 as_bad ("%d prefixes given and %s segment override gives too many prefixes",
1629 MAX_PREFIXES, i.seg->seg_name);
1632 i.prefix[i.prefixes++] = i.seg->seg_prefix;
1637 /* Handle conversion of 'int $3' --> special int3 insn. */
1638 if (t->base_opcode == INT_OPCODE && i.imms[0]->X_add_number == 3)
1640 t->base_opcode = INT3_OPCODE;
1644 /* We are ready to output the insn. */
1649 if (t->opcode_modifier & Jump)
1651 unsigned long n = i.disps[0]->X_add_number;
1653 if (i.disps[0]->X_op == O_constant)
1655 if (fits_in_signed_byte (n))
1659 p[0] = t->base_opcode;
1663 { /* It's an absolute word/dword displacement. */
1665 /* Use only 16-bit jumps for 16-bit code,
1666 because text segments are limited to 64K anyway;
1667 use only 32-bit jumps for 32-bit code,
1668 because they're faster. */
1669 int jmp_size = flag_16bit_code ? 2 : 4;
1670 if (flag_16bit_code && !fits_in_signed_word (n))
1672 as_bad ("16-bit jump out of range");
1676 if (t->base_opcode == JUMP_PC_RELATIVE)
1678 /* unconditional jump */
1679 p = frag_more (1 + jmp_size);
1680 insn_size += 1 + jmp_size;
1682 md_number_to_chars (&p[1], (valueT) n, jmp_size);
1686 /* conditional jump */
1687 p = frag_more (2 + jmp_size);
1688 insn_size += 2 + jmp_size;
1689 p[0] = TWO_BYTE_OPCODE_ESCAPE;
1690 p[1] = t->base_opcode + 0x10;
1691 md_number_to_chars (&p[2], (valueT) n, jmp_size);
1697 if (flag_16bit_code)
1699 FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
1703 /* It's a symbol; end frag & setup for relax.
1704 Make sure there are more than 6 chars left in the current frag;
1705 if not we'll have to start a new one. */
1709 p[0] = t->base_opcode;
1710 frag_var (rs_machine_dependent,
1711 6, /* 2 opcode/prefix + 4 displacement */
1713 ((unsigned char) *p == JUMP_PC_RELATIVE
1714 ? ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE)
1715 : ENCODE_RELAX_STATE (COND_JUMP, BYTE)),
1716 i.disps[0]->X_add_symbol,
1720 else if (t->opcode_modifier & (JumpByte | JumpDword))
1722 int size = (t->opcode_modifier & JumpByte) ? 1 : 4;
1723 unsigned long n = i.disps[0]->X_add_number;
1726 /* The jcx/jecx instruction might need a data size prefix. */
1727 for (q = i.prefix; q < i.prefix + i.prefixes; q++)
1729 if (*q == WORD_PREFIX_OPCODE)
1731 /* The jcxz/jecxz instructions are marked with Data16
1732 and Data32, which means that they may get
1733 WORD_PREFIX_OPCODE added to the list of prefixes.
1734 However, the are correctly distinguished using
1735 ADDR_PREFIX_OPCODE. Here we look for
1736 WORD_PREFIX_OPCODE, and actually emit
1737 ADDR_PREFIX_OPCODE. This is a hack, but, then, so
1738 is the instruction itself.
1740 If an explicit suffix is used for the loop
1741 instruction, that actually controls whether we use
1742 cx vs. ecx. This is also controlled by
1745 I don't know if there is any valid case in which we
1746 want to emit WORD_PREFIX_OPCODE, but I am keeping
1747 the old behaviour for safety. */
1749 if (IS_JUMP_ON_CX_ZERO (t->base_opcode)
1750 || IS_LOOP_ECX_TIMES (t->base_opcode))
1751 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
1753 FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
1759 if ((size == 4) && (flag_16bit_code))
1761 FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
1765 if (fits_in_unsigned_byte (t->base_opcode))
1767 FRAG_APPEND_1_CHAR (t->base_opcode);
1772 p = frag_more (2); /* opcode can be at most two bytes */
1774 /* put out high byte first: can't use md_number_to_chars! */
1775 *p++ = (t->base_opcode >> 8) & 0xff;
1776 *p = t->base_opcode & 0xff;
1779 p = frag_more (size);
1781 if (i.disps[0]->X_op == O_constant)
1783 md_number_to_chars (p, (valueT) n, size);
1784 if (size == 1 && !fits_in_signed_byte (n))
1786 as_bad ("loop/jecx only takes byte displacement; %lu shortened to %d",
1792 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
1793 i.disps[0], 1, reloc (size, 1, i.disp_reloc[0]));
1797 else if (t->opcode_modifier & JumpInterSegment)
1799 if (flag_16bit_code)
1801 FRAG_APPEND_1_CHAR (WORD_PREFIX_OPCODE);
1805 p = frag_more (1 + 2 + 4); /* 1 opcode; 2 segment; 4 offset */
1806 insn_size += 1 + 2 + 4;
1807 p[0] = t->base_opcode;
1808 if (i.imms[1]->X_op == O_constant)
1809 md_number_to_chars (p + 1, (valueT) i.imms[1]->X_add_number, 4);
1811 fix_new_exp (frag_now, p + 1 - frag_now->fr_literal, 4,
1812 i.imms[1], 0, BFD_RELOC_32);
1813 if (i.imms[0]->X_op != O_constant)
1814 as_bad ("can't handle non absolute segment in long call/jmp");
1815 md_number_to_chars (p + 5, (valueT) i.imms[0]->X_add_number, 2);
1819 /* Output normal instructions here. */
1822 /* First the prefix bytes. */
1823 for (q = i.prefix; q < i.prefix + i.prefixes; q++)
1827 md_number_to_chars (p, (valueT) *q, 1);
1830 /* Now the opcode; be careful about word order here! */
1831 if (fits_in_unsigned_byte (t->base_opcode))
1833 FRAG_APPEND_1_CHAR (t->base_opcode);
1836 else if (fits_in_unsigned_word (t->base_opcode))
1840 /* put out high byte first: can't use md_number_to_chars! */
1841 *p++ = (t->base_opcode >> 8) & 0xff;
1842 *p = t->base_opcode & 0xff;
1845 { /* opcode is either 3 or 4 bytes */
1846 if (t->base_opcode & 0xff000000)
1850 *p++ = (t->base_opcode >> 24) & 0xff;
1857 *p++ = (t->base_opcode >> 16) & 0xff;
1858 *p++ = (t->base_opcode >> 8) & 0xff;
1859 *p = (t->base_opcode) & 0xff;
1862 /* Now the modrm byte and base index byte (if present). */
1863 if (t->opcode_modifier & Modrm)
1867 /* md_number_to_chars (p, i.rm, 1); */
1868 md_number_to_chars (p,
1869 (valueT) (i.rm.regmem << 0
1873 /* If i.rm.regmem == ESP (4) && i.rm.mode != Mode 3 (Register mode)
1874 ==> need second modrm byte. */
1875 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING && i.rm.mode != 3)
1879 /* md_number_to_chars (p, i.bi, 1); */
1880 md_number_to_chars (p, (valueT) (i.bi.base << 0
1887 if (i.disp_operands)
1889 register unsigned int n;
1891 for (n = 0; n < i.operands; n++)
1895 if (i.disps[n]->X_op == O_constant)
1897 if (i.types[n] & (Disp8 | Abs8))
1901 md_number_to_chars (p,
1902 (valueT) i.disps[n]->X_add_number,
1905 else if (i.types[n] & (Disp16 | Abs16))
1909 md_number_to_chars (p,
1910 (valueT) i.disps[n]->X_add_number,
1914 { /* Disp32|Abs32 */
1917 md_number_to_chars (p,
1918 (valueT) i.disps[n]->X_add_number,
1923 { /* not absolute_section */
1924 /* need a 32-bit fixup (don't support 8bit non-absolute disps) */
1927 fix_new_exp (frag_now, p - frag_now->fr_literal, 4,
1929 TC_RELOC(i.disp_reloc[n], BFD_RELOC_32));
1933 } /* end displacement output */
1935 /* output immediate */
1938 register unsigned int n;
1940 for (n = 0; n < i.operands; n++)
1944 if (i.imms[n]->X_op == O_constant)
1946 if (i.types[n] & (Imm8 | Imm8S))
1950 md_number_to_chars (p,
1951 (valueT) i.imms[n]->X_add_number,
1954 else if (i.types[n] & Imm16)
1958 md_number_to_chars (p,
1959 (valueT) i.imms[n]->X_add_number,
1966 md_number_to_chars (p,
1967 (valueT) i.imms[n]->X_add_number,
1972 { /* not absolute_section */
1973 /* Need a 32-bit fixup (don't support 8bit
1974 non-absolute ims). Try to support other
1980 if (i.types[n] & (Imm8 | Imm8S))
1982 else if (i.types[n] & Imm16)
1986 r_type = reloc (size, 0, i.disp_reloc[0]);
1987 p = frag_more (size);
1989 #ifdef BFD_ASSEMBLER
1990 if (r_type == BFD_RELOC_32
1992 && GOT_symbol == i.imms[n]->X_add_symbol
1993 && (i.imms[n]->X_op == O_symbol
1994 || (i.imms[n]->X_op == O_add
1995 && (i.imms[n]->X_op_symbol->sy_value.X_op
1998 r_type = BFD_RELOC_386_GOTPC;
1999 i.imms[n]->X_add_number += 3;
2002 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
2003 i.imms[n], pcrel, r_type);
2007 } /* end immediate output */
2015 #endif /* DEBUG386 */
2019 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
2023 i386_operand (operand_string)
2024 char *operand_string;
2026 register char *op_string = operand_string;
2028 /* Address of '\0' at end of operand_string. */
2029 char *end_of_operand_string = operand_string + strlen (operand_string);
2031 /* Start and end of displacement string expression (if found). */
2032 char *displacement_string_start = NULL;
2033 char *displacement_string_end = NULL;
2035 /* We check for an absolute prefix (differentiating,
2036 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
2037 if (*op_string == ABSOLUTE_PREFIX)
2040 i.types[this_operand] |= JumpAbsolute;
2043 /* Check if operand is a register. */
2044 if (*op_string == REGISTER_PREFIX)
2046 register reg_entry *r;
2047 if (!(r = parse_register (op_string)))
2049 as_bad ("bad register name ('%s')", op_string);
2052 /* Check for segment override, rather than segment register by
2053 searching for ':' after %<x>s where <x> = s, c, d, e, f, g. */
2054 if ((r->reg_type & (SReg2 | SReg3)) && op_string[3] == ':')
2059 i.seg = (seg_entry *) & es;
2062 i.seg = (seg_entry *) & cs;
2065 i.seg = (seg_entry *) & ss;
2068 i.seg = (seg_entry *) & ds;
2071 i.seg = (seg_entry *) & fs;
2074 i.seg = (seg_entry *) & gs;
2077 op_string += 4; /* skip % <x> s : */
2078 operand_string = op_string; /* Pretend given string starts here. */
2079 if (!is_digit_char (*op_string) && !is_identifier_char (*op_string)
2080 && *op_string != '(' && *op_string != ABSOLUTE_PREFIX)
2082 as_bad ("bad memory operand after segment override");
2085 /* Handle case of %es:*foo. */
2086 if (*op_string == ABSOLUTE_PREFIX)
2089 i.types[this_operand] |= JumpAbsolute;
2091 goto do_memory_reference;
2093 i.types[this_operand] |= r->reg_type;
2094 i.regs[this_operand] = r;
2097 else if (*op_string == IMMEDIATE_PREFIX)
2098 { /* ... or an immediate */
2099 char *save_input_line_pointer;
2103 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
2105 as_bad ("only 1 or 2 immediate operands are allowed");
2109 exp = &im_expressions[i.imm_operands++];
2110 i.imms[this_operand] = exp;
2111 save_input_line_pointer = input_line_pointer;
2112 input_line_pointer = ++op_string; /* must advance op_string! */
2114 exp_seg = expression (exp);
2115 input_line_pointer = save_input_line_pointer;
2117 if (exp->X_op == O_absent)
2119 /* missing or bad expr becomes absolute 0 */
2120 as_bad ("missing or invalid immediate expression '%s' taken as 0",
2122 exp->X_op = O_constant;
2123 exp->X_add_number = 0;
2124 exp->X_add_symbol = (symbolS *) 0;
2125 exp->X_op_symbol = (symbolS *) 0;
2126 i.types[this_operand] |= Imm;
2128 else if (exp->X_op == O_constant)
2130 i.types[this_operand] |=
2131 smallest_imm_type ((unsigned long) exp->X_add_number);
2134 else if (exp_seg != text_section
2135 && exp_seg != data_section
2136 && exp_seg != bss_section
2137 && exp_seg != undefined_section
2138 #ifdef BFD_ASSEMBLER
2139 && ! bfd_is_com_section (exp_seg)
2144 as_bad ("Unimplemented segment type %d in parse_operand", exp_seg);
2150 /* this is an address ==> 32bit */
2151 i.types[this_operand] |= Imm32;
2153 /* shorten this type of this operand if the instruction wants
2154 * fewer bits than are present in the immediate. The bit field
2155 * code can put out 'andb $0xffffff, %al', for example. pace
2156 * also 'movw $foo,(%eax)'
2160 case WORD_OPCODE_SUFFIX:
2161 i.types[this_operand] |= Imm16;
2163 case BYTE_OPCODE_SUFFIX:
2164 i.types[this_operand] |= Imm16 | Imm8 | Imm8S;
2168 else if (is_digit_char (*op_string) || is_identifier_char (*op_string)
2169 || *op_string == '(')
2171 /* This is a memory reference of some sort. */
2172 register char *base_string;
2173 unsigned int found_base_index_form;
2175 do_memory_reference:
2176 if (i.mem_operands == MAX_MEMORY_OPERANDS)
2178 as_bad ("more than 1 memory reference in instruction");
2183 /* Determine type of memory operand from opcode_suffix;
2184 no opcode suffix implies general memory references. */
2187 case BYTE_OPCODE_SUFFIX:
2188 i.types[this_operand] |= Mem8;
2190 case WORD_OPCODE_SUFFIX:
2191 i.types[this_operand] |= Mem16;
2193 case DWORD_OPCODE_SUFFIX:
2195 i.types[this_operand] |= Mem32;
2198 /* Check for base index form. We detect the base index form by
2199 looking for an ')' at the end of the operand, searching
2200 for the '(' matching it, and finding a REGISTER_PREFIX or ','
2202 base_string = end_of_operand_string - 1;
2203 found_base_index_form = 0;
2204 if (*base_string == ')')
2206 unsigned int parens_balanced = 1;
2207 /* We've already checked that the number of left & right ()'s are
2208 equal, so this loop will not be infinite. */
2212 if (*base_string == ')')
2214 if (*base_string == '(')
2217 while (parens_balanced);
2218 base_string++; /* Skip past '('. */
2219 if (*base_string == REGISTER_PREFIX || *base_string == ',')
2220 found_base_index_form = 1;
2223 /* If we can't parse a base index register expression, we've found
2224 a pure displacement expression. We set up displacement_string_start
2225 and displacement_string_end for the code below. */
2226 if (!found_base_index_form)
2228 displacement_string_start = op_string;
2229 displacement_string_end = end_of_operand_string;
2233 char *base_reg_name, *index_reg_name, *num_string;
2236 i.types[this_operand] |= BaseIndex;
2238 /* If there is a displacement set-up for it to be parsed later. */
2239 if (base_string != op_string + 1)
2241 displacement_string_start = op_string;
2242 displacement_string_end = base_string - 1;
2245 /* Find base register (if any). */
2246 if (*base_string != ',')
2248 base_reg_name = base_string++;
2249 /* skip past register name & parse it */
2250 while (isalpha (*base_string))
2252 if (base_string == base_reg_name + 1)
2254 as_bad ("can't find base register name after '(%c'",
2258 END_STRING_AND_SAVE (base_string);
2259 if (!(i.base_reg = parse_register (base_reg_name)))
2261 as_bad ("bad base register name ('%s')", base_reg_name);
2264 RESTORE_END_STRING (base_string);
2267 /* Now check seperator; must be ',' ==> index reg
2268 OR num ==> no index reg. just scale factor
2269 OR ')' ==> end. (scale factor = 1) */
2270 if (*base_string != ',' && *base_string != ')')
2272 as_bad ("expecting ',' or ')' after base register in `%s'",
2277 /* There may index reg here; and there may be a scale factor. */
2278 if (*base_string == ',' && *(base_string + 1) == REGISTER_PREFIX)
2280 index_reg_name = ++base_string;
2281 while (isalpha (*++base_string));
2282 END_STRING_AND_SAVE (base_string);
2283 if (!(i.index_reg = parse_register (index_reg_name)))
2285 as_bad ("bad index register name ('%s')", index_reg_name);
2288 RESTORE_END_STRING (base_string);
2291 /* Check for scale factor. */
2292 if (*base_string == ',' && isdigit (*(base_string + 1)))
2294 num_string = ++base_string;
2295 while (is_digit_char (*base_string))
2297 if (base_string == num_string)
2299 as_bad ("can't find a scale factor after ','");
2302 END_STRING_AND_SAVE (base_string);
2303 /* We've got a scale factor. */
2304 if (!sscanf (num_string, "%d", &num))
2306 as_bad ("can't parse scale factor from '%s'", num_string);
2309 RESTORE_END_STRING (base_string);
2311 { /* must be 1 digit scale */
2313 i.log2_scale_factor = 0;
2316 i.log2_scale_factor = 1;
2319 i.log2_scale_factor = 2;
2322 i.log2_scale_factor = 3;
2325 as_bad ("expecting scale factor of 1, 2, 4, 8; got %d", num);
2331 if (!i.index_reg && *base_string == ',')
2333 as_bad ("expecting index register or scale factor after ','; got '%c'",
2334 *(base_string + 1));
2340 /* If there's an expression begining the operand, parse it,
2341 assuming displacement_string_start and displacement_string_end
2343 if (displacement_string_start)
2345 register expressionS *exp;
2347 char *save_input_line_pointer;
2348 exp = &disp_expressions[i.disp_operands];
2349 i.disps[this_operand] = exp;
2350 i.disp_reloc[this_operand] = NO_RELOC;
2352 save_input_line_pointer = input_line_pointer;
2353 input_line_pointer = displacement_string_start;
2354 END_STRING_AND_SAVE (displacement_string_end);
2359 * We can have operands of the form
2360 * <symbol>@GOTOFF+<nnn>
2361 * Take the easy way out here and copy everything
2362 * into a temporary buffer...
2366 cp = strchr (input_line_pointer, '@');
2371 if (GOT_symbol == NULL)
2372 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
2374 tmpbuf = (char *) alloca ((cp - input_line_pointer) + 20);
2376 if (strncmp (cp + 1, "PLT", 3) == 0)
2378 i.disp_reloc[this_operand] = BFD_RELOC_386_PLT32;
2380 strcpy (tmpbuf, input_line_pointer);
2381 strcat (tmpbuf, cp + 1 + 3);
2384 else if (strncmp (cp + 1, "GOTOFF", 6) == 0)
2386 i.disp_reloc[this_operand] = BFD_RELOC_386_GOTOFF;
2388 strcpy (tmpbuf, input_line_pointer);
2389 strcat (tmpbuf, cp + 1 + 6);
2392 else if (strncmp (cp + 1, "GOT", 3) == 0)
2394 i.disp_reloc[this_operand] = BFD_RELOC_386_GOT32;
2396 strcpy (tmpbuf, input_line_pointer);
2397 strcat (tmpbuf, cp + 1 + 3);
2401 as_bad ("Bad reloc specifier '%s' in expression", cp + 1);
2403 input_line_pointer = tmpbuf;
2408 exp_seg = expression (exp);
2410 #ifdef BFD_ASSEMBLER
2411 /* We do this to make sure that the section symbol is in
2412 the symbol table. We will ultimately change the relocation
2413 to be relative to the beginning of the section */
2414 if (i.disp_reloc[this_operand] == BFD_RELOC_386_GOTOFF)
2416 if (S_IS_LOCAL(exp->X_add_symbol)
2417 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
2418 section_symbol(exp->X_add_symbol->bsym->section);
2419 assert (exp->X_op == O_symbol);
2420 exp->X_op = O_subtract;
2421 exp->X_op_symbol = GOT_symbol;
2422 i.disp_reloc[this_operand] = BFD_RELOC_32;
2426 if (*input_line_pointer)
2427 as_bad ("Ignoring junk '%s' after expression", input_line_pointer);
2428 RESTORE_END_STRING (displacement_string_end);
2429 input_line_pointer = save_input_line_pointer;
2430 if (exp->X_op == O_absent)
2432 /* missing expr becomes absolute 0 */
2433 as_bad ("missing or invalid displacement '%s' taken as 0",
2435 i.types[this_operand] |= (Disp | Abs);
2436 exp->X_op = O_constant;
2437 exp->X_add_number = 0;
2438 exp->X_add_symbol = (symbolS *) 0;
2439 exp->X_op_symbol = (symbolS *) 0;
2441 else if (exp->X_op == O_constant)
2443 i.types[this_operand] |= SMALLEST_DISP_TYPE (exp->X_add_number);
2445 else if (exp_seg == text_section
2446 || exp_seg == data_section
2447 || exp_seg == bss_section
2448 || exp_seg == undefined_section)
2450 i.types[this_operand] |= Disp32;
2455 i.types[this_operand] |= Disp32;
2457 goto seg_unimplemented;
2462 /* Make sure the memory operand we've been dealt is valid. */
2463 if (i.base_reg && i.index_reg &&
2464 !(i.base_reg->reg_type & i.index_reg->reg_type & Reg))
2466 as_bad ("register size mismatch in (base,index,scale) expression");
2470 * special case for (%dx) while doing input/output op
2473 (i.base_reg->reg_type == (Reg16 | InOutPortReg)) &&
2474 (i.index_reg == 0)))
2476 i.types[this_operand] |= InOutPortReg;
2479 if ((i.base_reg && (i.base_reg->reg_type & Reg32) == 0) ||
2480 (i.index_reg && (i.index_reg->reg_type & Reg32) == 0))
2482 as_bad ("base/index register must be 32 bit register");
2485 if (i.index_reg && i.index_reg == esp)
2487 as_bad ("%s may not be used as an index register", esp->reg_name);
2492 { /* it's not a memory operand; argh! */
2493 as_bad ("invalid char %s begining %s operand '%s'",
2494 output_invalid (*op_string), ordinal_names[this_operand],
2498 return 1; /* normal return */
2502 * md_estimate_size_before_relax()
2504 * Called just before relax().
2505 * Any symbol that is now undefined will not become defined.
2506 * Return the correct fr_subtype in the frag.
2507 * Return the initial "guess for fr_var" to caller.
2508 * The guess for fr_var is ACTUALLY the growth beyond fr_fix.
2509 * Whatever we do to grow fr_fix or fr_var contributes to our returned value.
2510 * Although it may not be explicit in the frag, pretend fr_var starts with a
2514 md_estimate_size_before_relax (fragP, segment)
2515 register fragS *fragP;
2516 register segT segment;
2518 register unsigned char *opcode;
2519 register int old_fr_fix;
2521 old_fr_fix = fragP->fr_fix;
2522 opcode = (unsigned char *) fragP->fr_opcode;
2523 /* We've already got fragP->fr_subtype right; all we have to do is check
2524 for un-relaxable symbols. */
2525 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
2527 /* symbol is undefined in this segment */
2530 case JUMP_PC_RELATIVE: /* make jmp (0xeb) a dword displacement jump */
2531 opcode[0] = 0xe9; /* dword disp jmp */
2533 fix_new (fragP, old_fr_fix, 4,
2535 fragP->fr_offset, 1,
2536 (GOT_symbol && /* Not quite right - we should switch on
2537 presence of @PLT, but I cannot see how
2538 to get to that from here. We should have
2539 done this in md_assemble to really
2540 get it right all of the time, but I
2541 think it does not matter that much, as
2542 this will be right most of the time. ERY*/
2543 S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
2544 BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
2548 /* This changes the byte-displacement jump 0x7N -->
2549 the dword-displacement jump 0x0f8N */
2550 opcode[1] = opcode[0] + 0x10;
2551 opcode[0] = TWO_BYTE_OPCODE_ESCAPE; /* two-byte escape */
2552 fragP->fr_fix += 1 + 4; /* we've added an opcode byte */
2553 fix_new (fragP, old_fr_fix + 1, 4,
2555 fragP->fr_offset, 1,
2556 (GOT_symbol && /* Not quite right - we should switch on
2557 presence of @PLT, but I cannot see how
2558 to get to that from here. ERY */
2559 S_GET_SEGMENT(fragP->fr_symbol) == undefined_section)?
2560 BFD_RELOC_386_PLT32 : BFD_RELOC_32_PCREL);
2565 return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
2566 } /* md_estimate_size_before_relax() */
2569 * md_convert_frag();
2571 * Called after relax() is finished.
2572 * In: Address of frag.
2573 * fr_type == rs_machine_dependent.
2574 * fr_subtype is what the address relaxed to.
2576 * Out: Any fixSs and constants are set up.
2577 * Caller will turn frag into a ".space 0".
2579 #ifndef BFD_ASSEMBLER
2581 md_convert_frag (headers, sec, fragP)
2582 object_headers *headers;
2584 register fragS *fragP;
2587 md_convert_frag (abfd, sec, fragP)
2590 register fragS *fragP;
2593 register unsigned char *opcode;
2594 unsigned char *where_to_put_displacement = NULL;
2595 unsigned int target_address;
2596 unsigned int opcode_address;
2597 unsigned int extension = 0;
2598 int displacement_from_opcode_start;
2600 opcode = (unsigned char *) fragP->fr_opcode;
2602 /* Address we want to reach in file space. */
2603 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
2604 #ifdef BFD_ASSEMBLER /* not needed otherwise? */
2605 target_address += fragP->fr_symbol->sy_frag->fr_address;
2608 /* Address opcode resides at in file space. */
2609 opcode_address = fragP->fr_address + fragP->fr_fix;
2611 /* Displacement from opcode start to fill into instruction. */
2612 displacement_from_opcode_start = target_address - opcode_address;
2614 switch (fragP->fr_subtype)
2616 case ENCODE_RELAX_STATE (COND_JUMP, BYTE):
2617 case ENCODE_RELAX_STATE (UNCOND_JUMP, BYTE):
2618 /* don't have to change opcode */
2619 extension = 1; /* 1 opcode + 1 displacement */
2620 where_to_put_displacement = &opcode[1];
2623 case ENCODE_RELAX_STATE (COND_JUMP, WORD):
2624 opcode[1] = TWO_BYTE_OPCODE_ESCAPE;
2625 opcode[2] = opcode[0] + 0x10;
2626 opcode[0] = WORD_PREFIX_OPCODE;
2627 extension = 4; /* 3 opcode + 2 displacement */
2628 where_to_put_displacement = &opcode[3];
2631 case ENCODE_RELAX_STATE (UNCOND_JUMP, WORD):
2633 opcode[0] = WORD_PREFIX_OPCODE;
2634 extension = 3; /* 2 opcode + 2 displacement */
2635 where_to_put_displacement = &opcode[2];
2638 case ENCODE_RELAX_STATE (COND_JUMP, DWORD):
2639 opcode[1] = opcode[0] + 0x10;
2640 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
2641 extension = 5; /* 2 opcode + 4 displacement */
2642 where_to_put_displacement = &opcode[2];
2645 case ENCODE_RELAX_STATE (UNCOND_JUMP, DWORD):
2647 extension = 4; /* 1 opcode + 4 displacement */
2648 where_to_put_displacement = &opcode[1];
2652 BAD_CASE (fragP->fr_subtype);
2655 /* now put displacement after opcode */
2656 md_number_to_chars ((char *) where_to_put_displacement,
2657 (valueT) (displacement_from_opcode_start - extension),
2658 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
2659 fragP->fr_fix += extension;
2663 int md_short_jump_size = 2; /* size of byte displacement jmp */
2664 int md_long_jump_size = 5; /* size of dword displacement jmp */
2665 const int md_reloc_size = 8; /* Size of relocation record */
2668 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
2670 addressT from_addr, to_addr;
2676 offset = to_addr - (from_addr + 2);
2677 md_number_to_chars (ptr, (valueT) 0xeb, 1); /* opcode for byte-disp jump */
2678 md_number_to_chars (ptr + 1, (valueT) offset, 1);
2682 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
2684 addressT from_addr, to_addr;
2690 if (flag_do_long_jump)
2692 offset = to_addr - S_GET_VALUE (to_symbol);
2693 md_number_to_chars (ptr, (valueT) 0xe9, 1);/* opcode for long jmp */
2694 md_number_to_chars (ptr + 1, (valueT) offset, 4);
2695 fix_new (frag, (ptr + 1) - frag->fr_literal, 4,
2696 to_symbol, (offsetT) 0, 0, BFD_RELOC_32);
2700 offset = to_addr - (from_addr + 5);
2701 md_number_to_chars (ptr, (valueT) 0xe9, 1);
2702 md_number_to_chars (ptr + 1, (valueT) offset, 4);
2706 /* Apply a fixup (fixS) to segment data, once it has been determined
2707 by our caller that we have all the info we need to fix it up.
2709 On the 386, immediates, displacements, and data pointers are all in
2710 the same (little-endian) format, so we don't need to care about which
2714 md_apply_fix3 (fixP, valp, seg)
2715 fixS *fixP; /* The fix we're to put in. */
2716 valueT *valp; /* Pointer to the value of the bits. */
2717 segT seg; /* Segment fix is from. */
2719 register char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
2720 valueT value = *valp;
2722 #if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
2724 * This is a hack. There should be a better way to
2727 if (fixP->fx_r_type == BFD_RELOC_32_PCREL && fixP->fx_addsy)
2730 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
2731 || OUTPUT_FLAVOR == bfd_target_coff_flavour)
2732 value += fixP->fx_where + fixP->fx_frag->fr_address;
2734 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2735 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
2736 && (S_GET_SEGMENT (fixP->fx_addsy) == seg
2737 || (fixP->fx_addsy->bsym->flags & BSF_SECTION_SYM) != 0))
2739 /* Yes, we add the values in twice. This is because
2740 bfd_perform_relocation subtracts them out again. I think
2741 bfd_perform_relocation is broken, but I don't dare change
2743 value += fixP->fx_where + fixP->fx_frag->fr_address;
2746 #if defined (OBJ_COFF) && defined (TE_PE)
2747 /* For some reason, the PE format does not store a section
2748 address offset for a PC relative symbol. */
2749 if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
2750 value += md_pcrel_from (fixP);
2754 /* Fix a few things - the dynamic linker expects certain values here,
2755 and we must not dissappoint it. */
2756 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2757 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
2759 switch(fixP->fx_r_type) {
2760 case BFD_RELOC_386_PLT32:
2761 /* Make the jump instruction point to the address of the operand. At
2762 runtime we merely add the offset to the actual PLT entry. */
2765 case BFD_RELOC_386_GOTPC:
2767 * This is tough to explain. We end up with this one if we have
2768 * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal
2769 * here is to obtain the absolute address of the GOT, and it is strongly
2770 * preferable from a performance point of view to avoid using a runtime
2771 * relocation for this. The actual sequence of instructions often look
2777 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
2779 * The call and pop essentially return the absolute address of
2780 * the label .L66 and store it in %ebx. The linker itself will
2781 * ultimately change the first operand of the addl so that %ebx points to
2782 * the GOT, but to keep things simple, the .o file must have this operand
2783 * set so that it generates not the absolute address of .L66, but the
2784 * absolute address of itself. This allows the linker itself simply
2785 * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
2786 * added in, and the addend of the relocation is stored in the operand
2787 * field for the instruction itself.
2789 * Our job here is to fix the operand so that it would add the correct
2790 * offset so that %ebx would point to itself. The thing that is tricky is
2791 * that .-.L66 will point to the beginning of the instruction, so we need
2792 * to further modify the operand so that it will point to itself.
2793 * There are other cases where you have something like:
2795 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
2797 * and here no correction would be required. Internally in the assembler
2798 * we treat operands of this form as not being pcrel since the '.' is
2799 * explicitly mentioned, and I wonder whether it would simplify matters
2800 * to do it this way. Who knows. In earlier versions of the PIC patches,
2801 * the pcrel_adjust field was used to store the correction, but since the
2802 * expression is not pcrel, I felt it would be confusing to do it this way.
2806 case BFD_RELOC_386_GOT32:
2807 value = 0; /* Fully resolved at runtime. No addend. */
2809 case BFD_RELOC_386_GOTOFF:
2818 md_number_to_chars (p, value, fixP->fx_size);
2824 /* This is never used. */
2825 long /* Knows about the byte order in a word. */
2826 md_chars_to_number (con, nbytes)
2827 unsigned char con[]; /* Low order byte 1st. */
2828 int nbytes; /* Number of bytes in the input. */
2831 for (retval = 0, con += nbytes - 1; nbytes--; con--)
2833 retval <<= BITS_PER_CHAR;
2841 #define MAX_LITTLENUMS 6
2843 /* Turn the string pointed to by litP into a floating point constant of type
2844 type, and emit the appropriate bytes. The number of LITTLENUMS emitted
2845 is stored in *sizeP . An error message is returned, or NULL on OK. */
2847 md_atof (type, litP, sizeP)
2853 LITTLENUM_TYPE words[MAX_LITTLENUMS];
2854 LITTLENUM_TYPE *wordP;
2876 return "Bad call to md_atof ()";
2878 t = atof_ieee (input_line_pointer, type, words);
2880 input_line_pointer = t;
2882 *sizeP = prec * sizeof (LITTLENUM_TYPE);
2883 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
2884 the bigendian 386. */
2885 for (wordP = words + prec - 1; prec--;)
2887 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
2888 litP += sizeof (LITTLENUM_TYPE);
2893 char output_invalid_buf[8];
2900 sprintf (output_invalid_buf, "'%c'", c);
2902 sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
2903 return output_invalid_buf;
2906 /* reg_string starts *before* REGISTER_PREFIX */
2908 parse_register (reg_string)
2911 register char *s = reg_string;
2913 char reg_name_given[MAX_REG_NAME_SIZE];
2915 s++; /* skip REGISTER_PREFIX */
2916 for (p = reg_name_given; is_register_char (*s); p++, s++)
2918 *p = register_chars[(unsigned char) *s];
2919 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
2920 return (reg_entry *) 0;
2923 return (reg_entry *) hash_find (reg_hash, reg_name_given);
2927 CONST char *md_shortopts = "kmVQ:";
2929 CONST char *md_shortopts = "m";
2931 struct option md_longopts[] = {
2932 {NULL, no_argument, NULL, 0}
2934 size_t md_longopts_size = sizeof(md_longopts);
2937 md_parse_option (c, arg)
2944 flag_do_long_jump = 1;
2947 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2948 /* -k: Ignore for FreeBSD compatibility. */
2952 /* -V: SVR4 argument to print version ID. */
2954 print_version_id ();
2957 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
2958 should be emitted or not. FIXME: Not implemented. */
2970 md_show_usage (stream)
2974 -m do long jump\n");
2977 #ifdef BFD_ASSEMBLER
2978 #ifdef OBJ_MAYBE_ELF
2979 #ifdef OBJ_MAYBE_COFF
2981 /* Pick the target format to use. */
2984 i386_target_format ()
2986 switch (OUTPUT_FLAVOR)
2988 case bfd_target_coff_flavour:
2990 case bfd_target_elf_flavour:
2991 return "elf32-i386";
2998 #endif /* OBJ_MAYBE_COFF */
2999 #endif /* OBJ_MAYBE_ELF */
3000 #endif /* BFD_ASSEMBLER */
3004 md_undefined_symbol (name)
3007 if (*name == '_' && *(name+1) == 'G'
3008 && strcmp(name, GLOBAL_OFFSET_TABLE_NAME) == 0)
3012 if(symbol_find(name))
3013 as_bad("GOT already in symbol table");
3014 GOT_symbol = symbol_new (name, undefined_section,
3015 (valueT) 0, &zero_address_frag);
3022 /* Round up a section size to the appropriate boundary. */
3024 md_section_align (segment, size)
3029 #ifdef BFD_ASSEMBLER
3030 /* For a.out, force the section size to be aligned. If we don't do
3031 this, BFD will align it for us, but it will not write out the
3032 final bytes of the section. This may be a bug in BFD, but it is
3033 easier to fix it here since that is how the other a.out targets
3037 align = bfd_get_section_alignment (stdoutput, segment);
3038 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
3045 /* Exactly what point is a PC-relative offset relative TO? On the
3046 i386, they're relative to the address of the offset, plus its
3047 size. (??? Is this right? FIXME-SOON!) */
3049 md_pcrel_from (fixP)
3052 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
3063 temp = get_absolute_expression ();
3064 subseg_set (bss_section, (subsegT) temp);
3065 demand_empty_rest_of_line ();
3071 #ifdef BFD_ASSEMBLER
3074 i386_validate_fix (fixp)
3077 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
3079 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
3084 #define F(SZ,PCREL) (((SZ) << 1) + (PCREL))
3085 #define MAP(SZ,PCREL,TYPE) case F(SZ,PCREL): code = (TYPE); break
3088 tc_gen_reloc (section, fixp)
3093 bfd_reloc_code_real_type code;
3095 switch(fixp->fx_r_type)
3097 case BFD_RELOC_386_PLT32:
3098 case BFD_RELOC_386_GOT32:
3099 case BFD_RELOC_386_GOTOFF:
3100 case BFD_RELOC_386_GOTPC:
3102 code = fixp->fx_r_type;
3105 switch (F (fixp->fx_size, fixp->fx_pcrel))
3107 MAP (1, 0, BFD_RELOC_8);
3108 MAP (2, 0, BFD_RELOC_16);
3109 MAP (4, 0, BFD_RELOC_32);
3110 MAP (1, 1, BFD_RELOC_8_PCREL);
3111 MAP (2, 1, BFD_RELOC_16_PCREL);
3112 MAP (4, 1, BFD_RELOC_32_PCREL);
3114 as_bad ("Can not do %d byte %srelocation", fixp->fx_size,
3115 fixp->fx_pcrel ? "pc-relative " : "");
3121 if (code == BFD_RELOC_32
3123 && fixp->fx_addsy == GOT_symbol)
3124 code = BFD_RELOC_386_GOTPC;
3126 rel = (arelent *) xmalloc (sizeof (arelent));
3127 rel->sym_ptr_ptr = &fixp->fx_addsy->bsym;
3128 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
3130 rel->addend = fixp->fx_addnumber;
3134 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
3135 if (rel->howto == NULL)
3137 as_bad_where (fixp->fx_file, fixp->fx_line,
3138 "Cannot represent relocation type %s",
3139 bfd_get_reloc_code_name (code));
3140 /* Set howto to a garbage value so that we can keep going. */
3141 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
3142 assert (rel->howto != NULL);
3148 #else /* ! BFD_ASSEMBLER */
3150 #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
3152 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
3155 relax_addressT segment_address_in_file;
3158 * In: length of relocation (or of address) in chars: 1, 2 or 4.
3159 * Out: GNU LD relocation length code: 0, 1, or 2.
3162 static const unsigned char nbytes_r_length[] = {42, 0, 1, 42, 2};
3165 know (fixP->fx_addsy != NULL);
3167 md_number_to_chars (where,
3168 (valueT) (fixP->fx_frag->fr_address
3169 + fixP->fx_where - segment_address_in_file),
3172 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
3173 ? S_GET_TYPE (fixP->fx_addsy)
3174 : fixP->fx_addsy->sy_number);
3176 where[6] = (r_symbolnum >> 16) & 0x0ff;
3177 where[5] = (r_symbolnum >> 8) & 0x0ff;
3178 where[4] = r_symbolnum & 0x0ff;
3179 where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
3180 | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
3181 | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
3184 #endif /* OBJ_AOUT or OBJ_BOUT */
3186 #if defined (I386COFF)
3189 tc_coff_fix2rtype (fixP)
3192 if (fixP->fx_r_type == R_IMAGEBASE)
3195 return (fixP->fx_pcrel ?
3196 (fixP->fx_size == 1 ? R_PCRBYTE :
3197 fixP->fx_size == 2 ? R_PCRWORD :
3199 (fixP->fx_size == 1 ? R_RELBYTE :
3200 fixP->fx_size == 2 ? R_RELWORD :
3205 tc_coff_sizemachdep (frag)
3209 return (frag->fr_next->fr_address - frag->fr_address);
3214 #endif /* I386COFF */
3216 #endif /* BFD_ASSEMBLER? */
3218 /* end of tc-i386.c */