1 /* i386.c -- Assemble code for the Intel 80386
2 Copyright 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* Intel 80386 machine specific gas.
26 Bugs & suggestions are completely welcome. This is free software.
27 Please help us make it better. */
30 #include "safe-ctype.h"
32 #include "dwarf2dbg.h"
33 #include "opcode/i386.h"
35 #ifndef REGISTER_WARNINGS
36 #define REGISTER_WARNINGS 1
39 #ifndef INFER_ADDR_PREFIX
40 #define INFER_ADDR_PREFIX 1
43 #ifndef SCALE1_WHEN_NO_INDEX
44 /* Specifying a scale factor besides 1 when there is no index is
45 futile. eg. `mov (%ebx,2),%al' does exactly the same as
46 `mov (%ebx),%al'. To slavishly follow what the programmer
47 specified, set SCALE1_WHEN_NO_INDEX to 0. */
48 #define SCALE1_WHEN_NO_INDEX 1
52 #define RELOC_ENUM enum bfd_reloc_code_real
54 #define RELOC_ENUM int
58 #define DEFAULT_ARCH "i386"
63 #define INLINE __inline__
69 static INLINE unsigned int mode_from_disp_size PARAMS ((unsigned int));
70 static INLINE int fits_in_signed_byte PARAMS ((offsetT));
71 static INLINE int fits_in_unsigned_byte PARAMS ((offsetT));
72 static INLINE int fits_in_unsigned_word PARAMS ((offsetT));
73 static INLINE int fits_in_signed_word PARAMS ((offsetT));
74 static INLINE int fits_in_unsigned_long PARAMS ((offsetT));
75 static INLINE int fits_in_signed_long PARAMS ((offsetT));
76 static int smallest_imm_type PARAMS ((offsetT));
77 static offsetT offset_in_range PARAMS ((offsetT, int));
78 static int add_prefix PARAMS ((unsigned int));
79 static void set_code_flag PARAMS ((int));
80 static void set_16bit_gcc_code_flag PARAMS ((int));
81 static void set_intel_syntax PARAMS ((int));
82 static void set_cpu_arch PARAMS ((int));
83 static char *output_invalid PARAMS ((int c));
84 static int i386_operand PARAMS ((char *operand_string));
85 static int i386_intel_operand PARAMS ((char *operand_string, int got_a_float));
86 static const reg_entry *parse_register PARAMS ((char *reg_string,
88 static char *parse_insn PARAMS ((char *, char *));
89 static char *parse_operands PARAMS ((char *, const char *));
90 static void swap_operands PARAMS ((void));
91 static void optimize_imm PARAMS ((void));
92 static void optimize_disp PARAMS ((void));
93 static int match_template PARAMS ((void));
94 static int check_string PARAMS ((void));
95 static int process_suffix PARAMS ((void));
96 static int check_byte_reg PARAMS ((void));
97 static int check_long_reg PARAMS ((void));
98 static int check_qword_reg PARAMS ((void));
99 static int check_word_reg PARAMS ((void));
100 static int finalize_imm PARAMS ((void));
101 static int process_operands PARAMS ((void));
102 static const seg_entry *build_modrm_byte PARAMS ((void));
103 static void output_insn PARAMS ((void));
104 static void output_branch PARAMS ((void));
105 static void output_jump PARAMS ((void));
106 static void output_interseg_jump PARAMS ((void));
107 static void output_imm PARAMS ((void));
108 static void output_disp PARAMS ((void));
110 static void s_bss PARAMS ((int));
113 static const char *default_arch = DEFAULT_ARCH;
115 /* 'md_assemble ()' gathers together information and puts it into a
122 const reg_entry *regs;
127 /* TM holds the template for the insn were currently assembling. */
130 /* SUFFIX holds the instruction mnemonic suffix if given.
131 (e.g. 'l' for 'movl') */
134 /* OPERANDS gives the number of given operands. */
135 unsigned int operands;
137 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
138 of given register, displacement, memory operands and immediate
140 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
142 /* TYPES [i] is the type (see above #defines) which tells us how to
143 use OP[i] for the corresponding operand. */
144 unsigned int types[MAX_OPERANDS];
146 /* Displacement expression, immediate expression, or register for each
148 union i386_op op[MAX_OPERANDS];
150 /* Flags for operands. */
151 unsigned int flags[MAX_OPERANDS];
152 #define Operand_PCrel 1
154 /* Relocation type for operand */
155 RELOC_ENUM reloc[MAX_OPERANDS];
157 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
158 the base index byte below. */
159 const reg_entry *base_reg;
160 const reg_entry *index_reg;
161 unsigned int log2_scale_factor;
163 /* SEG gives the seg_entries of this insn. They are zero unless
164 explicit segment overrides are given. */
165 const seg_entry *seg[2];
167 /* PREFIX holds all the given prefix opcodes (usually null).
168 PREFIXES is the number of prefix opcodes. */
169 unsigned int prefixes;
170 unsigned char prefix[MAX_PREFIXES];
172 /* RM and SIB are the modrm byte and the sib byte where the
173 addressing modes of this insn are encoded. */
180 typedef struct _i386_insn i386_insn;
182 /* List of chars besides those in app.c:symbol_chars that can start an
183 operand. Used to prevent the scrubber eating vital white-space. */
185 const char extra_symbol_chars[] = "*%-(@[";
187 const char extra_symbol_chars[] = "*%-([";
190 #if (defined (TE_I386AIX) \
191 || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
192 && !defined (TE_LINUX) \
193 && !defined (TE_FreeBSD) \
194 && !defined (TE_NetBSD)))
195 /* This array holds the chars that always start a comment. If the
196 pre-processor is disabled, these aren't very useful. */
197 const char comment_chars[] = "#/";
198 #define PREFIX_SEPARATOR '\\'
200 /* This array holds the chars that only start a comment at the beginning of
201 a line. If the line seems to have the form '# 123 filename'
202 .line and .file directives will appear in the pre-processed output.
203 Note that input_file.c hand checks for '#' at the beginning of the
204 first line of the input file. This is because the compiler outputs
205 #NO_APP at the beginning of its output.
206 Also note that comments started like this one will always work if
207 '/' isn't otherwise defined. */
208 const char line_comment_chars[] = "";
211 /* Putting '/' here makes it impossible to use the divide operator.
212 However, we need it for compatibility with SVR4 systems. */
213 const char comment_chars[] = "#";
214 #define PREFIX_SEPARATOR '/'
216 const char line_comment_chars[] = "/";
219 const char line_separator_chars[] = ";";
221 /* Chars that can be used to separate mant from exp in floating point
223 const char EXP_CHARS[] = "eE";
225 /* Chars that mean this number is a floating point constant
228 const char FLT_CHARS[] = "fFdDxX";
230 /* Tables for lexical analysis. */
231 static char mnemonic_chars[256];
232 static char register_chars[256];
233 static char operand_chars[256];
234 static char identifier_chars[256];
235 static char digit_chars[256];
237 /* Lexical macros. */
238 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
239 #define is_operand_char(x) (operand_chars[(unsigned char) x])
240 #define is_register_char(x) (register_chars[(unsigned char) x])
241 #define is_space_char(x) ((x) == ' ')
242 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
243 #define is_digit_char(x) (digit_chars[(unsigned char) x])
245 /* All non-digit non-letter charcters that may occur in an operand. */
246 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
248 /* md_assemble() always leaves the strings it's passed unaltered. To
249 effect this we maintain a stack of saved characters that we've smashed
250 with '\0's (indicating end of strings for various sub-fields of the
251 assembler instruction). */
252 static char save_stack[32];
253 static char *save_stack_p;
254 #define END_STRING_AND_SAVE(s) \
255 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
256 #define RESTORE_END_STRING(s) \
257 do { *(s) = *--save_stack_p; } while (0)
259 /* The instruction we're assembling. */
262 /* Possible templates for current insn. */
263 static const templates *current_templates;
265 /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
266 static expressionS disp_expressions[2], im_expressions[2];
268 /* Current operand we are working on. */
269 static int this_operand;
271 /* We support four different modes. FLAG_CODE variable is used to distinguish
278 #define NUM_FLAG_CODE ((int) CODE_64BIT + 1)
280 static enum flag_code flag_code;
281 static int use_rela_relocations = 0;
283 /* The names used to print error messages. */
284 static const char *flag_code_names[] =
291 /* 1 for intel syntax,
293 static int intel_syntax = 0;
295 /* 1 if register prefix % not required. */
296 static int allow_naked_reg = 0;
298 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
299 leave, push, and pop instructions so that gcc has the same stack
300 frame as in 32 bit mode. */
301 static char stackop_size = '\0';
303 /* Non-zero to quieten some warnings. */
304 static int quiet_warnings = 0;
307 static const char *cpu_arch_name = NULL;
309 /* CPU feature flags. */
310 static unsigned int cpu_arch_flags = CpuUnknownFlags | CpuNo64;
312 /* If set, conditional jumps are not automatically promoted to handle
313 larger than a byte offset. */
314 static unsigned int no_cond_jump_promotion = 0;
316 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
319 /* Interface to relax_segment.
320 There are 3 major relax states for 386 jump insns because the
321 different types of jumps add different sizes to frags when we're
322 figuring out what sort of jump to choose to reach a given label. */
325 #define UNCOND_JUMP 0
327 #define COND_JUMP86 2
332 #define SMALL16 (SMALL | CODE16)
334 #define BIG16 (BIG | CODE16)
338 #define INLINE __inline__
344 #define ENCODE_RELAX_STATE(type, size) \
345 ((relax_substateT) (((type) << 2) | (size)))
346 #define TYPE_FROM_RELAX_STATE(s) \
348 #define DISP_SIZE_FROM_RELAX_STATE(s) \
349 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
351 /* This table is used by relax_frag to promote short jumps to long
352 ones where necessary. SMALL (short) jumps may be promoted to BIG
353 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
354 don't allow a short jump in a 32 bit code segment to be promoted to
355 a 16 bit offset jump because it's slower (requires data size
356 prefix), and doesn't work, unless the destination is in the bottom
357 64k of the code segment (The top 16 bits of eip are zeroed). */
359 const relax_typeS md_relax_table[] =
362 1) most positive reach of this state,
363 2) most negative reach of this state,
364 3) how many bytes this mode will have in the variable part of the frag
365 4) which index into the table to try if we can't fit into this one. */
367 /* UNCOND_JUMP states. */
368 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
369 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
370 /* dword jmp adds 4 bytes to frag:
371 0 extra opcode bytes, 4 displacement bytes. */
373 /* word jmp adds 2 byte2 to frag:
374 0 extra opcode bytes, 2 displacement bytes. */
377 /* COND_JUMP states. */
378 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
379 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
380 /* dword conditionals adds 5 bytes to frag:
381 1 extra opcode byte, 4 displacement bytes. */
383 /* word conditionals add 3 bytes to frag:
384 1 extra opcode byte, 2 displacement bytes. */
387 /* COND_JUMP86 states. */
388 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
389 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
390 /* dword conditionals adds 5 bytes to frag:
391 1 extra opcode byte, 4 displacement bytes. */
393 /* word conditionals add 4 bytes to frag:
394 1 displacement byte and a 3 byte long branch insn. */
398 static const arch_entry cpu_arch[] = {
400 {"i186", Cpu086|Cpu186 },
401 {"i286", Cpu086|Cpu186|Cpu286 },
402 {"i386", Cpu086|Cpu186|Cpu286|Cpu386 },
403 {"i486", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486 },
404 {"i586", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuMMX },
405 {"i686", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|CpuSSE },
406 {"pentium", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuMMX },
407 {"pentiumpro",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|CpuSSE },
408 {"pentium4", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuSSE|CpuSSE2 },
409 {"k6", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX|Cpu3dnow },
410 {"athlon", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuMMX|Cpu3dnow },
411 {"sledgehammer",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuSledgehammer|CpuMMX|Cpu3dnow|CpuSSE|CpuSSE2 },
415 const pseudo_typeS md_pseudo_table[] =
417 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
418 {"align", s_align_bytes, 0},
420 {"align", s_align_ptwo, 0},
422 {"arch", set_cpu_arch, 0},
426 {"ffloat", float_cons, 'f'},
427 {"dfloat", float_cons, 'd'},
428 {"tfloat", float_cons, 'x'},
430 {"noopt", s_ignore, 0},
431 {"optim", s_ignore, 0},
432 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
433 {"code16", set_code_flag, CODE_16BIT},
434 {"code32", set_code_flag, CODE_32BIT},
435 {"code64", set_code_flag, CODE_64BIT},
436 {"intel_syntax", set_intel_syntax, 1},
437 {"att_syntax", set_intel_syntax, 0},
438 {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
439 {"loc", dwarf2_directive_loc, 0},
443 /* For interface with expression (). */
444 extern char *input_line_pointer;
446 /* Hash table for instruction mnemonic lookup. */
447 static struct hash_control *op_hash;
449 /* Hash table for register lookup. */
450 static struct hash_control *reg_hash;
453 i386_align_code (fragP, count)
457 /* Various efficient no-op patterns for aligning code labels.
458 Note: Don't try to assemble the instructions in the comments.
459 0L and 0w are not legal. */
460 static const char f32_1[] =
462 static const char f32_2[] =
463 {0x89,0xf6}; /* movl %esi,%esi */
464 static const char f32_3[] =
465 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
466 static const char f32_4[] =
467 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
468 static const char f32_5[] =
470 0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
471 static const char f32_6[] =
472 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
473 static const char f32_7[] =
474 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
475 static const char f32_8[] =
477 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
478 static const char f32_9[] =
479 {0x89,0xf6, /* movl %esi,%esi */
480 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
481 static const char f32_10[] =
482 {0x8d,0x76,0x00, /* leal 0(%esi),%esi */
483 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
484 static const char f32_11[] =
485 {0x8d,0x74,0x26,0x00, /* leal 0(%esi,1),%esi */
486 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
487 static const char f32_12[] =
488 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
489 0x8d,0xbf,0x00,0x00,0x00,0x00}; /* leal 0L(%edi),%edi */
490 static const char f32_13[] =
491 {0x8d,0xb6,0x00,0x00,0x00,0x00, /* leal 0L(%esi),%esi */
492 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
493 static const char f32_14[] =
494 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00, /* leal 0L(%esi,1),%esi */
495 0x8d,0xbc,0x27,0x00,0x00,0x00,0x00}; /* leal 0L(%edi,1),%edi */
496 static const char f32_15[] =
497 {0xeb,0x0d,0x90,0x90,0x90,0x90,0x90, /* jmp .+15; lotsa nops */
498 0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x90};
499 static const char f16_3[] =
500 {0x8d,0x74,0x00}; /* lea 0(%esi),%esi */
501 static const char f16_4[] =
502 {0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
503 static const char f16_5[] =
505 0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
506 static const char f16_6[] =
507 {0x89,0xf6, /* mov %si,%si */
508 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
509 static const char f16_7[] =
510 {0x8d,0x74,0x00, /* lea 0(%si),%si */
511 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
512 static const char f16_8[] =
513 {0x8d,0xb4,0x00,0x00, /* lea 0w(%si),%si */
514 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
515 static const char *const f32_patt[] = {
516 f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8,
517 f32_9, f32_10, f32_11, f32_12, f32_13, f32_14, f32_15
519 static const char *const f16_patt[] = {
520 f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8,
521 f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
524 if (count <= 0 || count > 15)
527 /* The recommended way to pad 64bit code is to use NOPs preceded by
528 maximally four 0x66 prefixes. Balance the size of nops. */
529 if (flag_code == CODE_64BIT)
532 int nnops = (count + 3) / 4;
533 int len = count / nnops;
534 int remains = count - nnops * len;
537 for (i = 0; i < remains; i++)
539 memset (fragP->fr_literal + fragP->fr_fix + pos, 0x66, len);
540 fragP->fr_literal[fragP->fr_fix + pos + len] = 0x90;
543 for (; i < nnops; i++)
545 memset (fragP->fr_literal + fragP->fr_fix + pos, 0x66, len - 1);
546 fragP->fr_literal[fragP->fr_fix + pos + len - 1] = 0x90;
551 if (flag_code == CODE_16BIT)
553 memcpy (fragP->fr_literal + fragP->fr_fix,
554 f16_patt[count - 1], count);
556 /* Adjust jump offset. */
557 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
560 memcpy (fragP->fr_literal + fragP->fr_fix,
561 f32_patt[count - 1], count);
562 fragP->fr_var = count;
565 static INLINE unsigned int
566 mode_from_disp_size (t)
569 return (t & Disp8) ? 1 : (t & (Disp16 | Disp32 | Disp32S)) ? 2 : 0;
573 fits_in_signed_byte (num)
576 return (num >= -128) && (num <= 127);
580 fits_in_unsigned_byte (num)
583 return (num & 0xff) == num;
587 fits_in_unsigned_word (num)
590 return (num & 0xffff) == num;
594 fits_in_signed_word (num)
597 return (-32768 <= num) && (num <= 32767);
600 fits_in_signed_long (num)
601 offsetT num ATTRIBUTE_UNUSED;
606 return (!(((offsetT) -1 << 31) & num)
607 || (((offsetT) -1 << 31) & num) == ((offsetT) -1 << 31));
609 } /* fits_in_signed_long() */
611 fits_in_unsigned_long (num)
612 offsetT num ATTRIBUTE_UNUSED;
617 return (num & (((offsetT) 2 << 31) - 1)) == num;
619 } /* fits_in_unsigned_long() */
622 smallest_imm_type (num)
625 if (cpu_arch_flags != (Cpu086 | Cpu186 | Cpu286 | Cpu386 | Cpu486 | CpuNo64))
627 /* This code is disabled on the 486 because all the Imm1 forms
628 in the opcode table are slower on the i486. They're the
629 versions with the implicitly specified single-position
630 displacement, which has another syntax if you really want to
633 return Imm1 | Imm8 | Imm8S | Imm16 | Imm32 | Imm32S | Imm64;
635 return (fits_in_signed_byte (num)
636 ? (Imm8S | Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
637 : fits_in_unsigned_byte (num)
638 ? (Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
639 : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
640 ? (Imm16 | Imm32 | Imm32S | Imm64)
641 : fits_in_signed_long (num)
642 ? (Imm32 | Imm32S | Imm64)
643 : fits_in_unsigned_long (num)
649 offset_in_range (val, size)
657 case 1: mask = ((addressT) 1 << 8) - 1; break;
658 case 2: mask = ((addressT) 1 << 16) - 1; break;
659 case 4: mask = ((addressT) 2 << 31) - 1; break;
661 case 8: mask = ((addressT) 2 << 63) - 1; break;
666 /* If BFD64, sign extend val. */
667 if (!use_rela_relocations)
668 if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
669 val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
671 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
673 char buf1[40], buf2[40];
675 sprint_value (buf1, val);
676 sprint_value (buf2, val & mask);
677 as_warn (_("%s shortened to %s"), buf1, buf2);
682 /* Returns 0 if attempting to add a prefix where one from the same
683 class already exists, 1 if non rep/repne added, 2 if rep/repne
692 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
693 && flag_code == CODE_64BIT)
701 case CS_PREFIX_OPCODE:
702 case DS_PREFIX_OPCODE:
703 case ES_PREFIX_OPCODE:
704 case FS_PREFIX_OPCODE:
705 case GS_PREFIX_OPCODE:
706 case SS_PREFIX_OPCODE:
710 case REPNE_PREFIX_OPCODE:
711 case REPE_PREFIX_OPCODE:
714 case LOCK_PREFIX_OPCODE:
722 case ADDR_PREFIX_OPCODE:
726 case DATA_PREFIX_OPCODE:
731 if (i.prefix[q] != 0)
733 as_bad (_("same type of prefix used twice"));
738 i.prefix[q] = prefix;
743 set_code_flag (value)
747 cpu_arch_flags &= ~(Cpu64 | CpuNo64);
748 cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
749 if (value == CODE_64BIT && !(cpu_arch_flags & CpuSledgehammer))
751 as_bad (_("64bit mode not supported on this CPU."));
753 if (value == CODE_32BIT && !(cpu_arch_flags & Cpu386))
755 as_bad (_("32bit mode not supported on this CPU."));
761 set_16bit_gcc_code_flag (new_code_flag)
764 flag_code = new_code_flag;
765 cpu_arch_flags &= ~(Cpu64 | CpuNo64);
766 cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
771 set_intel_syntax (syntax_flag)
774 /* Find out if register prefixing is specified. */
775 int ask_naked_reg = 0;
778 if (!is_end_of_line[(unsigned char) *input_line_pointer])
780 char *string = input_line_pointer;
781 int e = get_symbol_end ();
783 if (strcmp (string, "prefix") == 0)
785 else if (strcmp (string, "noprefix") == 0)
788 as_bad (_("bad argument to syntax directive."));
789 *input_line_pointer = e;
791 demand_empty_rest_of_line ();
793 intel_syntax = syntax_flag;
795 if (ask_naked_reg == 0)
798 allow_naked_reg = (intel_syntax
799 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
801 /* Conservative default. */
806 allow_naked_reg = (ask_naked_reg < 0);
811 int dummy ATTRIBUTE_UNUSED;
815 if (!is_end_of_line[(unsigned char) *input_line_pointer])
817 char *string = input_line_pointer;
818 int e = get_symbol_end ();
821 for (i = 0; cpu_arch[i].name; i++)
823 if (strcmp (string, cpu_arch[i].name) == 0)
825 cpu_arch_name = cpu_arch[i].name;
826 cpu_arch_flags = (cpu_arch[i].flags
827 | (flag_code == CODE_64BIT ? Cpu64 : CpuNo64));
831 if (!cpu_arch[i].name)
832 as_bad (_("no such architecture: `%s'"), string);
834 *input_line_pointer = e;
837 as_bad (_("missing cpu architecture"));
839 no_cond_jump_promotion = 0;
840 if (*input_line_pointer == ','
841 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
843 char *string = ++input_line_pointer;
844 int e = get_symbol_end ();
846 if (strcmp (string, "nojumps") == 0)
847 no_cond_jump_promotion = 1;
848 else if (strcmp (string, "jumps") == 0)
851 as_bad (_("no such architecture modifier: `%s'"), string);
853 *input_line_pointer = e;
856 demand_empty_rest_of_line ();
863 if (!strcmp (default_arch, "x86_64"))
864 return bfd_mach_x86_64;
865 else if (!strcmp (default_arch, "i386"))
866 return bfd_mach_i386_i386;
868 as_fatal (_("Unknown architecture"));
875 const char *hash_err;
877 /* Initialize op_hash hash table. */
878 op_hash = hash_new ();
881 const template *optab;
882 templates *core_optab;
884 /* Setup for loop. */
886 core_optab = (templates *) xmalloc (sizeof (templates));
887 core_optab->start = optab;
892 if (optab->name == NULL
893 || strcmp (optab->name, (optab - 1)->name) != 0)
895 /* different name --> ship out current template list;
896 add to hash table; & begin anew. */
897 core_optab->end = optab;
898 hash_err = hash_insert (op_hash,
903 as_fatal (_("Internal Error: Can't hash %s: %s"),
907 if (optab->name == NULL)
909 core_optab = (templates *) xmalloc (sizeof (templates));
910 core_optab->start = optab;
915 /* Initialize reg_hash hash table. */
916 reg_hash = hash_new ();
918 const reg_entry *regtab;
920 for (regtab = i386_regtab;
921 regtab < i386_regtab + sizeof (i386_regtab) / sizeof (i386_regtab[0]);
924 hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
926 as_fatal (_("Internal Error: Can't hash %s: %s"),
932 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
937 for (c = 0; c < 256; c++)
942 mnemonic_chars[c] = c;
943 register_chars[c] = c;
944 operand_chars[c] = c;
946 else if (ISLOWER (c))
948 mnemonic_chars[c] = c;
949 register_chars[c] = c;
950 operand_chars[c] = c;
952 else if (ISUPPER (c))
954 mnemonic_chars[c] = TOLOWER (c);
955 register_chars[c] = mnemonic_chars[c];
956 operand_chars[c] = c;
959 if (ISALPHA (c) || ISDIGIT (c))
960 identifier_chars[c] = c;
963 identifier_chars[c] = c;
964 operand_chars[c] = c;
969 identifier_chars['@'] = '@';
971 digit_chars['-'] = '-';
972 identifier_chars['_'] = '_';
973 identifier_chars['.'] = '.';
975 for (p = operand_special_chars; *p != '\0'; p++)
976 operand_chars[(unsigned char) *p] = *p;
979 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
980 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
982 record_alignment (text_section, 2);
983 record_alignment (data_section, 2);
984 record_alignment (bss_section, 2);
990 i386_print_statistics (file)
993 hash_print_statistics (file, "i386 opcode", op_hash);
994 hash_print_statistics (file, "i386 register", reg_hash);
999 /* Debugging routines for md_assemble. */
1000 static void pi PARAMS ((char *, i386_insn *));
1001 static void pte PARAMS ((template *));
1002 static void pt PARAMS ((unsigned int));
1003 static void pe PARAMS ((expressionS *));
1004 static void ps PARAMS ((symbolS *));
1013 fprintf (stdout, "%s: template ", line);
1015 fprintf (stdout, " address: base %s index %s scale %x\n",
1016 x->base_reg ? x->base_reg->reg_name : "none",
1017 x->index_reg ? x->index_reg->reg_name : "none",
1018 x->log2_scale_factor);
1019 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
1020 x->rm.mode, x->rm.reg, x->rm.regmem);
1021 fprintf (stdout, " sib: base %x index %x scale %x\n",
1022 x->sib.base, x->sib.index, x->sib.scale);
1023 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
1024 (x->rex & REX_MODE64) != 0,
1025 (x->rex & REX_EXTX) != 0,
1026 (x->rex & REX_EXTY) != 0,
1027 (x->rex & REX_EXTZ) != 0);
1028 for (i = 0; i < x->operands; i++)
1030 fprintf (stdout, " #%d: ", i + 1);
1032 fprintf (stdout, "\n");
1034 & (Reg | SReg2 | SReg3 | Control | Debug | Test | RegMMX | RegXMM))
1035 fprintf (stdout, "%s\n", x->op[i].regs->reg_name);
1036 if (x->types[i] & Imm)
1038 if (x->types[i] & Disp)
1039 pe (x->op[i].disps);
1048 fprintf (stdout, " %d operands ", t->operands);
1049 fprintf (stdout, "opcode %x ", t->base_opcode);
1050 if (t->extension_opcode != None)
1051 fprintf (stdout, "ext %x ", t->extension_opcode);
1052 if (t->opcode_modifier & D)
1053 fprintf (stdout, "D");
1054 if (t->opcode_modifier & W)
1055 fprintf (stdout, "W");
1056 fprintf (stdout, "\n");
1057 for (i = 0; i < t->operands; i++)
1059 fprintf (stdout, " #%d type ", i + 1);
1060 pt (t->operand_types[i]);
1061 fprintf (stdout, "\n");
1069 fprintf (stdout, " operation %d\n", e->X_op);
1070 fprintf (stdout, " add_number %ld (%lx)\n",
1071 (long) e->X_add_number, (long) e->X_add_number);
1072 if (e->X_add_symbol)
1074 fprintf (stdout, " add_symbol ");
1075 ps (e->X_add_symbol);
1076 fprintf (stdout, "\n");
1080 fprintf (stdout, " op_symbol ");
1081 ps (e->X_op_symbol);
1082 fprintf (stdout, "\n");
1090 fprintf (stdout, "%s type %s%s",
1092 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
1093 segment_name (S_GET_SEGMENT (s)));
1102 static const type_names[] =
1115 { BaseIndex, "BaseIndex" },
1119 { Disp32S, "d32s" },
1121 { InOutPortReg, "InOutPortReg" },
1122 { ShiftCount, "ShiftCount" },
1123 { Control, "control reg" },
1124 { Test, "test reg" },
1125 { Debug, "debug reg" },
1126 { FloatReg, "FReg" },
1127 { FloatAcc, "FAcc" },
1131 { JumpAbsolute, "Jump Absolute" },
1142 const struct type_name *ty;
1144 for (ty = type_names; ty->mask; ty++)
1146 fprintf (stdout, "%s, ", ty->tname);
1150 #endif /* DEBUG386 */
1152 #ifdef BFD_ASSEMBLER
1153 static bfd_reloc_code_real_type reloc
1154 PARAMS ((int, int, int, bfd_reloc_code_real_type));
1156 static bfd_reloc_code_real_type
1157 reloc (size, pcrel, sign, other)
1161 bfd_reloc_code_real_type other;
1163 if (other != NO_RELOC)
1169 as_bad (_("There are no unsigned pc-relative relocations"));
1172 case 1: return BFD_RELOC_8_PCREL;
1173 case 2: return BFD_RELOC_16_PCREL;
1174 case 4: return BFD_RELOC_32_PCREL;
1176 as_bad (_("can not do %d byte pc-relative relocation"), size);
1183 case 4: return BFD_RELOC_X86_64_32S;
1188 case 1: return BFD_RELOC_8;
1189 case 2: return BFD_RELOC_16;
1190 case 4: return BFD_RELOC_32;
1191 case 8: return BFD_RELOC_64;
1193 as_bad (_("can not do %s %d byte relocation"),
1194 sign ? "signed" : "unsigned", size);
1198 return BFD_RELOC_NONE;
1201 /* Here we decide which fixups can be adjusted to make them relative to
1202 the beginning of the section instead of the symbol. Basically we need
1203 to make sure that the dynamic relocations are done correctly, so in
1204 some cases we force the original symbol to be used. */
1207 tc_i386_fix_adjustable (fixP)
1210 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1211 /* Prevent all adjustments to global symbols, or else dynamic
1212 linking will not work correctly. */
1213 if (S_IS_EXTERNAL (fixP->fx_addsy)
1214 || S_IS_WEAK (fixP->fx_addsy)
1215 /* Don't adjust pc-relative references to merge sections in 64-bit
1217 || (use_rela_relocations
1218 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
1222 /* adjust_reloc_syms doesn't know about the GOT. */
1223 if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
1224 || fixP->fx_r_type == BFD_RELOC_386_PLT32
1225 || fixP->fx_r_type == BFD_RELOC_386_GOT32
1226 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
1227 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
1228 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
1229 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
1230 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
1231 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
1232 || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
1233 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
1234 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
1235 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1236 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1241 #define reloc(SIZE,PCREL,SIGN,OTHER) 0
1242 #define BFD_RELOC_8 0
1243 #define BFD_RELOC_16 0
1244 #define BFD_RELOC_32 0
1245 #define BFD_RELOC_8_PCREL 0
1246 #define BFD_RELOC_16_PCREL 0
1247 #define BFD_RELOC_32_PCREL 0
1248 #define BFD_RELOC_386_PLT32 0
1249 #define BFD_RELOC_386_GOT32 0
1250 #define BFD_RELOC_386_GOTOFF 0
1251 #define BFD_RELOC_386_TLS_GD 0
1252 #define BFD_RELOC_386_TLS_LDM 0
1253 #define BFD_RELOC_386_TLS_LDO_32 0
1254 #define BFD_RELOC_386_TLS_IE_32 0
1255 #define BFD_RELOC_386_TLS_LE_32 0
1256 #define BFD_RELOC_386_TLS_LE 0
1257 #define BFD_RELOC_X86_64_PLT32 0
1258 #define BFD_RELOC_X86_64_GOT32 0
1259 #define BFD_RELOC_X86_64_GOTPCREL 0
1262 static int intel_float_operand PARAMS ((const char *mnemonic));
1265 intel_float_operand (mnemonic)
1266 const char *mnemonic;
1268 if (mnemonic[0] == 'f' && mnemonic[1] == 'i')
1271 if (mnemonic[0] == 'f')
1277 /* This is the guts of the machine-dependent assembler. LINE points to a
1278 machine dependent instruction. This function is supposed to emit
1279 the frags/bytes it assembles to. */
1286 char mnemonic[MAX_MNEM_SIZE];
1288 /* Initialize globals. */
1289 memset (&i, '\0', sizeof (i));
1290 for (j = 0; j < MAX_OPERANDS; j++)
1291 i.reloc[j] = NO_RELOC;
1292 memset (disp_expressions, '\0', sizeof (disp_expressions));
1293 memset (im_expressions, '\0', sizeof (im_expressions));
1294 save_stack_p = save_stack;
1296 /* First parse an instruction mnemonic & call i386_operand for the operands.
1297 We assume that the scrubber has arranged it so that line[0] is the valid
1298 start of a (possibly prefixed) mnemonic. */
1300 line = parse_insn (line, mnemonic);
1304 line = parse_operands (line, mnemonic);
1308 /* Now we've parsed the mnemonic into a set of templates, and have the
1309 operands at hand. */
1311 /* All intel opcodes have reversed operands except for "bound" and
1312 "enter". We also don't reverse intersegment "jmp" and "call"
1313 instructions with 2 immediate operands so that the immediate segment
1314 precedes the offset, as it does when in AT&T mode. "enter" and the
1315 intersegment "jmp" and "call" instructions are the only ones that
1316 have two immediate operands. */
1317 if (intel_syntax && i.operands > 1
1318 && (strcmp (mnemonic, "bound") != 0)
1319 && !((i.types[0] & Imm) && (i.types[1] & Imm)))
1325 if (i.disp_operands)
1328 /* Next, we find a template that matches the given insn,
1329 making sure the overlap of the given operands types is consistent
1330 with the template operand types. */
1332 if (!match_template ())
1337 /* Undo SYSV386_COMPAT brokenness when in Intel mode. See i386.h */
1339 && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
1340 i.tm.base_opcode ^= FloatR;
1342 /* Zap movzx and movsx suffix. The suffix may have been set from
1343 "word ptr" or "byte ptr" on the source operand, but we'll use
1344 the suffix later to choose the destination register. */
1345 if ((i.tm.base_opcode & ~9) == 0x0fb6)
1349 if (i.tm.opcode_modifier & FWait)
1350 if (!add_prefix (FWAIT_OPCODE))
1353 /* Check string instruction segment overrides. */
1354 if ((i.tm.opcode_modifier & IsString) != 0 && i.mem_operands != 0)
1356 if (!check_string ())
1360 if (!process_suffix ())
1363 /* Make still unresolved immediate matches conform to size of immediate
1364 given in i.suffix. */
1365 if (!finalize_imm ())
1368 if (i.types[0] & Imm1)
1369 i.imm_operands = 0; /* kludge for shift insns. */
1370 if (i.types[0] & ImplicitRegister)
1372 if (i.types[1] & ImplicitRegister)
1374 if (i.types[2] & ImplicitRegister)
1377 if (i.tm.opcode_modifier & ImmExt)
1379 /* These AMD 3DNow! and Intel Katmai New Instructions have an
1380 opcode suffix which is coded in the same place as an 8-bit
1381 immediate field would be. Here we fake an 8-bit immediate
1382 operand from the opcode suffix stored in tm.extension_opcode. */
1386 assert (i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
1388 exp = &im_expressions[i.imm_operands++];
1389 i.op[i.operands].imms = exp;
1390 i.types[i.operands++] = Imm8;
1391 exp->X_op = O_constant;
1392 exp->X_add_number = i.tm.extension_opcode;
1393 i.tm.extension_opcode = None;
1396 /* For insns with operands there are more diddles to do to the opcode. */
1399 if (!process_operands ())
1402 else if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
1404 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
1405 as_warn (_("translating to `%sp'"), i.tm.name);
1408 /* Handle conversion of 'int $3' --> special int3 insn. */
1409 if (i.tm.base_opcode == INT_OPCODE && i.op[0].imms->X_add_number == 3)
1411 i.tm.base_opcode = INT3_OPCODE;
1415 if ((i.tm.opcode_modifier & (Jump | JumpByte | JumpDword))
1416 && i.op[0].disps->X_op == O_constant)
1418 /* Convert "jmp constant" (and "call constant") to a jump (call) to
1419 the absolute address given by the constant. Since ix86 jumps and
1420 calls are pc relative, we need to generate a reloc. */
1421 i.op[0].disps->X_add_symbol = &abs_symbol;
1422 i.op[0].disps->X_op = O_symbol;
1425 if ((i.tm.opcode_modifier & Rex64) != 0)
1426 i.rex |= REX_MODE64;
1428 /* For 8 bit registers we need an empty rex prefix. Also if the
1429 instruction already has a prefix, we need to convert old
1430 registers to new ones. */
1432 if (((i.types[0] & Reg8) != 0
1433 && (i.op[0].regs->reg_flags & RegRex64) != 0)
1434 || ((i.types[1] & Reg8) != 0
1435 && (i.op[1].regs->reg_flags & RegRex64) != 0)
1436 || (((i.types[0] & Reg8) != 0 || (i.types[1] & Reg8) != 0)
1441 i.rex |= REX_OPCODE;
1442 for (x = 0; x < 2; x++)
1444 /* Look for 8 bit operand that uses old registers. */
1445 if ((i.types[x] & Reg8) != 0
1446 && (i.op[x].regs->reg_flags & RegRex64) == 0)
1448 /* In case it is "hi" register, give up. */
1449 if (i.op[x].regs->reg_num > 3)
1450 as_bad (_("can't encode register '%%%s' in an instruction requiring REX prefix.\n"),
1451 i.op[x].regs->reg_name);
1453 /* Otherwise it is equivalent to the extended register.
1454 Since the encoding doesn't change this is merely
1455 cosmetic cleanup for debug output. */
1457 i.op[x].regs = i.op[x].regs + 8;
1463 add_prefix (REX_OPCODE | i.rex);
1465 /* We are ready to output the insn. */
1470 parse_insn (line, mnemonic)
1475 char *token_start = l;
1478 /* Non-zero if we found a prefix only acceptable with string insns. */
1479 const char *expecting_string_instruction = NULL;
1484 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
1487 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
1489 as_bad (_("no such instruction: `%s'"), token_start);
1494 if (!is_space_char (*l)
1495 && *l != END_OF_INSN
1496 && *l != PREFIX_SEPARATOR
1499 as_bad (_("invalid character %s in mnemonic"),
1500 output_invalid (*l));
1503 if (token_start == l)
1505 if (*l == PREFIX_SEPARATOR)
1506 as_bad (_("expecting prefix; got nothing"));
1508 as_bad (_("expecting mnemonic; got nothing"));
1512 /* Look up instruction (or prefix) via hash table. */
1513 current_templates = hash_find (op_hash, mnemonic);
1515 if (*l != END_OF_INSN
1516 && (!is_space_char (*l) || l[1] != END_OF_INSN)
1517 && current_templates
1518 && (current_templates->start->opcode_modifier & IsPrefix))
1520 /* If we are in 16-bit mode, do not allow addr16 or data16.
1521 Similarly, in 32-bit mode, do not allow addr32 or data32. */
1522 if ((current_templates->start->opcode_modifier & (Size16 | Size32))
1523 && flag_code != CODE_64BIT
1524 && (((current_templates->start->opcode_modifier & Size32) != 0)
1525 ^ (flag_code == CODE_16BIT)))
1527 as_bad (_("redundant %s prefix"),
1528 current_templates->start->name);
1531 /* Add prefix, checking for repeated prefixes. */
1532 switch (add_prefix (current_templates->start->base_opcode))
1537 expecting_string_instruction = current_templates->start->name;
1540 /* Skip past PREFIX_SEPARATOR and reset token_start. */
1547 if (!current_templates)
1549 /* See if we can get a match by trimming off a suffix. */
1552 case WORD_MNEM_SUFFIX:
1553 case BYTE_MNEM_SUFFIX:
1554 case QWORD_MNEM_SUFFIX:
1555 i.suffix = mnem_p[-1];
1557 current_templates = hash_find (op_hash, mnemonic);
1559 case SHORT_MNEM_SUFFIX:
1560 case LONG_MNEM_SUFFIX:
1563 i.suffix = mnem_p[-1];
1565 current_templates = hash_find (op_hash, mnemonic);
1573 if (intel_float_operand (mnemonic))
1574 i.suffix = SHORT_MNEM_SUFFIX;
1576 i.suffix = LONG_MNEM_SUFFIX;
1578 current_templates = hash_find (op_hash, mnemonic);
1582 if (!current_templates)
1584 as_bad (_("no such instruction: `%s'"), token_start);
1589 if (current_templates->start->opcode_modifier & (Jump | JumpByte))
1591 /* Check for a branch hint. We allow ",pt" and ",pn" for
1592 predict taken and predict not taken respectively.
1593 I'm not sure that branch hints actually do anything on loop
1594 and jcxz insns (JumpByte) for current Pentium4 chips. They
1595 may work in the future and it doesn't hurt to accept them
1597 if (l[0] == ',' && l[1] == 'p')
1601 if (!add_prefix (DS_PREFIX_OPCODE))
1605 else if (l[2] == 'n')
1607 if (!add_prefix (CS_PREFIX_OPCODE))
1613 /* Any other comma loses. */
1616 as_bad (_("invalid character %s in mnemonic"),
1617 output_invalid (*l));
1621 /* Check if instruction is supported on specified architecture. */
1622 if ((current_templates->start->cpu_flags & ~(Cpu64 | CpuNo64))
1623 & ~(cpu_arch_flags & ~(Cpu64 | CpuNo64)))
1625 as_warn (_("`%s' is not supported on `%s'"),
1626 current_templates->start->name, cpu_arch_name);
1628 else if ((Cpu386 & ~cpu_arch_flags) && (flag_code != CODE_16BIT))
1630 as_warn (_("use .code16 to ensure correct addressing mode"));
1633 /* Check for rep/repne without a string instruction. */
1634 if (expecting_string_instruction
1635 && !(current_templates->start->opcode_modifier & IsString))
1637 as_bad (_("expecting string instruction after `%s'"),
1638 expecting_string_instruction);
1646 parse_operands (l, mnemonic)
1648 const char *mnemonic;
1652 /* 1 if operand is pending after ','. */
1653 unsigned int expecting_operand = 0;
1655 /* Non-zero if operand parens not balanced. */
1656 unsigned int paren_not_balanced;
1658 while (*l != END_OF_INSN)
1660 /* Skip optional white space before operand. */
1661 if (is_space_char (*l))
1663 if (!is_operand_char (*l) && *l != END_OF_INSN)
1665 as_bad (_("invalid character %s before operand %d"),
1666 output_invalid (*l),
1670 token_start = l; /* after white space */
1671 paren_not_balanced = 0;
1672 while (paren_not_balanced || *l != ',')
1674 if (*l == END_OF_INSN)
1676 if (paren_not_balanced)
1679 as_bad (_("unbalanced parenthesis in operand %d."),
1682 as_bad (_("unbalanced brackets in operand %d."),
1687 break; /* we are done */
1689 else if (!is_operand_char (*l) && !is_space_char (*l))
1691 as_bad (_("invalid character %s in operand %d"),
1692 output_invalid (*l),
1699 ++paren_not_balanced;
1701 --paren_not_balanced;
1706 ++paren_not_balanced;
1708 --paren_not_balanced;
1712 if (l != token_start)
1713 { /* Yes, we've read in another operand. */
1714 unsigned int operand_ok;
1715 this_operand = i.operands++;
1716 if (i.operands > MAX_OPERANDS)
1718 as_bad (_("spurious operands; (%d operands/instruction max)"),
1722 /* Now parse operand adding info to 'i' as we go along. */
1723 END_STRING_AND_SAVE (l);
1727 i386_intel_operand (token_start,
1728 intel_float_operand (mnemonic));
1730 operand_ok = i386_operand (token_start);
1732 RESTORE_END_STRING (l);
1738 if (expecting_operand)
1740 expecting_operand_after_comma:
1741 as_bad (_("expecting operand after ','; got nothing"));
1746 as_bad (_("expecting operand before ','; got nothing"));
1751 /* Now *l must be either ',' or END_OF_INSN. */
1754 if (*++l == END_OF_INSN)
1756 /* Just skip it, if it's \n complain. */
1757 goto expecting_operand_after_comma;
1759 expecting_operand = 1;
1768 union i386_op temp_op;
1769 unsigned int temp_type;
1770 RELOC_ENUM temp_reloc;
1774 if (i.operands == 2)
1779 else if (i.operands == 3)
1784 temp_type = i.types[xchg2];
1785 i.types[xchg2] = i.types[xchg1];
1786 i.types[xchg1] = temp_type;
1787 temp_op = i.op[xchg2];
1788 i.op[xchg2] = i.op[xchg1];
1789 i.op[xchg1] = temp_op;
1790 temp_reloc = i.reloc[xchg2];
1791 i.reloc[xchg2] = i.reloc[xchg1];
1792 i.reloc[xchg1] = temp_reloc;
1794 if (i.mem_operands == 2)
1796 const seg_entry *temp_seg;
1797 temp_seg = i.seg[0];
1798 i.seg[0] = i.seg[1];
1799 i.seg[1] = temp_seg;
1803 /* Try to ensure constant immediates are represented in the smallest
1808 char guess_suffix = 0;
1812 guess_suffix = i.suffix;
1813 else if (i.reg_operands)
1815 /* Figure out a suffix from the last register operand specified.
1816 We can't do this properly yet, ie. excluding InOutPortReg,
1817 but the following works for instructions with immediates.
1818 In any case, we can't set i.suffix yet. */
1819 for (op = i.operands; --op >= 0;)
1820 if (i.types[op] & Reg)
1822 if (i.types[op] & Reg8)
1823 guess_suffix = BYTE_MNEM_SUFFIX;
1824 else if (i.types[op] & Reg16)
1825 guess_suffix = WORD_MNEM_SUFFIX;
1826 else if (i.types[op] & Reg32)
1827 guess_suffix = LONG_MNEM_SUFFIX;
1828 else if (i.types[op] & Reg64)
1829 guess_suffix = QWORD_MNEM_SUFFIX;
1833 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
1834 guess_suffix = WORD_MNEM_SUFFIX;
1836 for (op = i.operands; --op >= 0;)
1837 if (i.types[op] & Imm)
1839 switch (i.op[op].imms->X_op)
1842 /* If a suffix is given, this operand may be shortened. */
1843 switch (guess_suffix)
1845 case LONG_MNEM_SUFFIX:
1846 i.types[op] |= Imm32 | Imm64;
1848 case WORD_MNEM_SUFFIX:
1849 i.types[op] |= Imm16 | Imm32S | Imm32 | Imm64;
1851 case BYTE_MNEM_SUFFIX:
1852 i.types[op] |= Imm16 | Imm8 | Imm8S | Imm32S | Imm32 | Imm64;
1856 /* If this operand is at most 16 bits, convert it
1857 to a signed 16 bit number before trying to see
1858 whether it will fit in an even smaller size.
1859 This allows a 16-bit operand such as $0xffe0 to
1860 be recognised as within Imm8S range. */
1861 if ((i.types[op] & Imm16)
1862 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
1864 i.op[op].imms->X_add_number =
1865 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
1867 if ((i.types[op] & Imm32)
1868 && ((i.op[op].imms->X_add_number & ~(((offsetT) 2 << 31) - 1))
1871 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
1872 ^ ((offsetT) 1 << 31))
1873 - ((offsetT) 1 << 31));
1875 i.types[op] |= smallest_imm_type (i.op[op].imms->X_add_number);
1877 /* We must avoid matching of Imm32 templates when 64bit
1878 only immediate is available. */
1879 if (guess_suffix == QWORD_MNEM_SUFFIX)
1880 i.types[op] &= ~Imm32;
1887 /* Symbols and expressions. */
1889 /* Convert symbolic operand to proper sizes for matching. */
1890 switch (guess_suffix)
1892 case QWORD_MNEM_SUFFIX:
1893 i.types[op] = Imm64 | Imm32S;
1895 case LONG_MNEM_SUFFIX:
1896 i.types[op] = Imm32 | Imm64;
1898 case WORD_MNEM_SUFFIX:
1899 i.types[op] = Imm16 | Imm32 | Imm64;
1902 case BYTE_MNEM_SUFFIX:
1903 i.types[op] = Imm8 | Imm8S | Imm16 | Imm32S | Imm32;
1912 /* Try to use the smallest displacement type too. */
1918 for (op = i.operands; --op >= 0;)
1919 if ((i.types[op] & Disp) && i.op[op].disps->X_op == O_constant)
1921 offsetT disp = i.op[op].disps->X_add_number;
1923 if (i.types[op] & Disp16)
1925 /* We know this operand is at most 16 bits, so
1926 convert to a signed 16 bit number before trying
1927 to see whether it will fit in an even smaller
1930 disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
1932 else if (i.types[op] & Disp32)
1934 /* We know this operand is at most 32 bits, so convert to a
1935 signed 32 bit number before trying to see whether it will
1936 fit in an even smaller size. */
1937 disp &= (((offsetT) 2 << 31) - 1);
1938 disp = (disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
1940 if (flag_code == CODE_64BIT)
1942 if (fits_in_signed_long (disp))
1943 i.types[op] |= Disp32S;
1944 if (fits_in_unsigned_long (disp))
1945 i.types[op] |= Disp32;
1947 if ((i.types[op] & (Disp32 | Disp32S | Disp16))
1948 && fits_in_signed_byte (disp))
1949 i.types[op] |= Disp8;
1956 /* Points to template once we've found it. */
1958 unsigned int overlap0, overlap1, overlap2;
1959 unsigned int found_reverse_match;
1962 #define MATCH(overlap, given, template) \
1963 ((overlap & ~JumpAbsolute) \
1964 && (((given) & (BaseIndex | JumpAbsolute)) \
1965 == ((overlap) & (BaseIndex | JumpAbsolute))))
1967 /* If given types r0 and r1 are registers they must be of the same type
1968 unless the expected operand type register overlap is null.
1969 Note that Acc in a template matches every size of reg. */
1970 #define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1) \
1971 (((g0) & Reg) == 0 || ((g1) & Reg) == 0 \
1972 || ((g0) & Reg) == ((g1) & Reg) \
1973 || ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
1978 found_reverse_match = 0;
1979 suffix_check = (i.suffix == BYTE_MNEM_SUFFIX
1981 : (i.suffix == WORD_MNEM_SUFFIX
1983 : (i.suffix == SHORT_MNEM_SUFFIX
1985 : (i.suffix == LONG_MNEM_SUFFIX
1987 : (i.suffix == QWORD_MNEM_SUFFIX
1989 : (i.suffix == LONG_DOUBLE_MNEM_SUFFIX
1990 ? No_xSuf : 0))))));
1992 for (t = current_templates->start;
1993 t < current_templates->end;
1996 /* Must have right number of operands. */
1997 if (i.operands != t->operands)
2000 /* Check the suffix, except for some instructions in intel mode. */
2001 if ((t->opcode_modifier & suffix_check)
2003 && (t->opcode_modifier & IgnoreSize))
2005 && t->base_opcode == 0xd9
2006 && (t->extension_opcode == 5 /* 0xd9,5 "fldcw" */
2007 || t->extension_opcode == 7))) /* 0xd9,7 "f{n}stcw" */
2010 /* Do not verify operands when there are none. */
2011 else if (!t->operands)
2013 if (t->cpu_flags & ~cpu_arch_flags)
2015 /* We've found a match; break out of loop. */
2019 overlap0 = i.types[0] & t->operand_types[0];
2020 switch (t->operands)
2023 if (!MATCH (overlap0, i.types[0], t->operand_types[0]))
2028 overlap1 = i.types[1] & t->operand_types[1];
2029 if (!MATCH (overlap0, i.types[0], t->operand_types[0])
2030 || !MATCH (overlap1, i.types[1], t->operand_types[1])
2031 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2032 t->operand_types[0],
2033 overlap1, i.types[1],
2034 t->operand_types[1]))
2036 /* Check if other direction is valid ... */
2037 if ((t->opcode_modifier & (D | FloatD)) == 0)
2040 /* Try reversing direction of operands. */
2041 overlap0 = i.types[0] & t->operand_types[1];
2042 overlap1 = i.types[1] & t->operand_types[0];
2043 if (!MATCH (overlap0, i.types[0], t->operand_types[1])
2044 || !MATCH (overlap1, i.types[1], t->operand_types[0])
2045 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2046 t->operand_types[1],
2047 overlap1, i.types[1],
2048 t->operand_types[0]))
2050 /* Does not match either direction. */
2053 /* found_reverse_match holds which of D or FloatDR
2055 found_reverse_match = t->opcode_modifier & (D | FloatDR);
2057 /* Found a forward 2 operand match here. */
2058 else if (t->operands == 3)
2060 /* Here we make use of the fact that there are no
2061 reverse match 3 operand instructions, and all 3
2062 operand instructions only need to be checked for
2063 register consistency between operands 2 and 3. */
2064 overlap2 = i.types[2] & t->operand_types[2];
2065 if (!MATCH (overlap2, i.types[2], t->operand_types[2])
2066 || !CONSISTENT_REGISTER_MATCH (overlap1, i.types[1],
2067 t->operand_types[1],
2068 overlap2, i.types[2],
2069 t->operand_types[2]))
2073 /* Found either forward/reverse 2 or 3 operand match here:
2074 slip through to break. */
2076 if (t->cpu_flags & ~cpu_arch_flags)
2078 found_reverse_match = 0;
2081 /* We've found a match; break out of loop. */
2085 if (t == current_templates->end)
2087 /* We found no match. */
2088 as_bad (_("suffix or operands invalid for `%s'"),
2089 current_templates->start->name);
2093 if (!quiet_warnings)
2096 && ((i.types[0] & JumpAbsolute)
2097 != (t->operand_types[0] & JumpAbsolute)))
2099 as_warn (_("indirect %s without `*'"), t->name);
2102 if ((t->opcode_modifier & (IsPrefix | IgnoreSize))
2103 == (IsPrefix | IgnoreSize))
2105 /* Warn them that a data or address size prefix doesn't
2106 affect assembly of the next line of code. */
2107 as_warn (_("stand-alone `%s' prefix"), t->name);
2111 /* Copy the template we found. */
2113 if (found_reverse_match)
2115 /* If we found a reverse match we must alter the opcode
2116 direction bit. found_reverse_match holds bits to change
2117 (different for int & float insns). */
2119 i.tm.base_opcode ^= found_reverse_match;
2121 i.tm.operand_types[0] = t->operand_types[1];
2122 i.tm.operand_types[1] = t->operand_types[0];
2131 int mem_op = (i.types[0] & AnyMem) ? 0 : 1;
2132 if ((i.tm.operand_types[mem_op] & EsSeg) != 0)
2134 if (i.seg[0] != NULL && i.seg[0] != &es)
2136 as_bad (_("`%s' operand %d must use `%%es' segment"),
2141 /* There's only ever one segment override allowed per instruction.
2142 This instruction possibly has a legal segment override on the
2143 second operand, so copy the segment to where non-string
2144 instructions store it, allowing common code. */
2145 i.seg[0] = i.seg[1];
2147 else if ((i.tm.operand_types[mem_op + 1] & EsSeg) != 0)
2149 if (i.seg[1] != NULL && i.seg[1] != &es)
2151 as_bad (_("`%s' operand %d must use `%%es' segment"),
2163 /* If matched instruction specifies an explicit instruction mnemonic
2165 if (i.tm.opcode_modifier & (Size16 | Size32 | Size64))
2167 if (i.tm.opcode_modifier & Size16)
2168 i.suffix = WORD_MNEM_SUFFIX;
2169 else if (i.tm.opcode_modifier & Size64)
2170 i.suffix = QWORD_MNEM_SUFFIX;
2172 i.suffix = LONG_MNEM_SUFFIX;
2174 else if (i.reg_operands)
2176 /* If there's no instruction mnemonic suffix we try to invent one
2177 based on register operands. */
2180 /* We take i.suffix from the last register operand specified,
2181 Destination register type is more significant than source
2184 for (op = i.operands; --op >= 0;)
2185 if ((i.types[op] & Reg)
2186 && !(i.tm.operand_types[op] & InOutPortReg))
2188 i.suffix = ((i.types[op] & Reg8) ? BYTE_MNEM_SUFFIX :
2189 (i.types[op] & Reg16) ? WORD_MNEM_SUFFIX :
2190 (i.types[op] & Reg64) ? QWORD_MNEM_SUFFIX :
2195 else if (i.suffix == BYTE_MNEM_SUFFIX)
2197 if (!check_byte_reg ())
2200 else if (i.suffix == LONG_MNEM_SUFFIX)
2202 if (!check_long_reg ())
2205 else if (i.suffix == QWORD_MNEM_SUFFIX)
2207 if (!check_qword_reg ())
2210 else if (i.suffix == WORD_MNEM_SUFFIX)
2212 if (!check_word_reg ())
2215 else if (intel_syntax && (i.tm.opcode_modifier & IgnoreSize))
2216 /* Do nothing if the instruction is going to ignore the prefix. */
2221 else if ((i.tm.opcode_modifier & DefaultSize) && !i.suffix)
2223 i.suffix = stackop_size;
2226 /* Change the opcode based on the operand size given by i.suffix;
2227 We need not change things for byte insns. */
2229 if (!i.suffix && (i.tm.opcode_modifier & W))
2231 as_bad (_("no instruction mnemonic suffix given and no register operands; can't size instruction"));
2235 if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
2237 /* It's not a byte, select word/dword operation. */
2238 if (i.tm.opcode_modifier & W)
2240 if (i.tm.opcode_modifier & ShortForm)
2241 i.tm.base_opcode |= 8;
2243 i.tm.base_opcode |= 1;
2246 /* Now select between word & dword operations via the operand
2247 size prefix, except for instructions that will ignore this
2249 if (i.suffix != QWORD_MNEM_SUFFIX
2250 && !(i.tm.opcode_modifier & IgnoreSize)
2251 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
2252 || (flag_code == CODE_64BIT
2253 && (i.tm.opcode_modifier & JumpByte))))
2255 unsigned int prefix = DATA_PREFIX_OPCODE;
2256 if (i.tm.opcode_modifier & JumpByte) /* jcxz, loop */
2257 prefix = ADDR_PREFIX_OPCODE;
2259 if (!add_prefix (prefix))
2263 /* Set mode64 for an operand. */
2264 if (i.suffix == QWORD_MNEM_SUFFIX
2265 && flag_code == CODE_64BIT
2266 && (i.tm.opcode_modifier & NoRex64) == 0)
2267 i.rex |= REX_MODE64;
2269 /* Size floating point instruction. */
2270 if (i.suffix == LONG_MNEM_SUFFIX)
2272 if (i.tm.opcode_modifier & FloatMF)
2273 i.tm.base_opcode ^= 4;
2284 for (op = i.operands; --op >= 0;)
2286 /* If this is an eight bit register, it's OK. If it's the 16 or
2287 32 bit version of an eight bit register, we will just use the
2288 low portion, and that's OK too. */
2289 if (i.types[op] & Reg8)
2292 /* movzx and movsx should not generate this warning. */
2294 && (i.tm.base_opcode == 0xfb7
2295 || i.tm.base_opcode == 0xfb6
2296 || i.tm.base_opcode == 0x63
2297 || i.tm.base_opcode == 0xfbe
2298 || i.tm.base_opcode == 0xfbf))
2301 if ((i.types[op] & WordReg) && i.op[op].regs->reg_num < 4
2303 /* Check that the template allows eight bit regs. This
2304 kills insns such as `orb $1,%edx', which maybe should be
2306 && (i.tm.operand_types[op] & (Reg8 | InOutPortReg))
2310 /* Prohibit these changes in the 64bit mode, since the
2311 lowering is more complicated. */
2312 if (flag_code == CODE_64BIT
2313 && (i.tm.operand_types[op] & InOutPortReg) == 0)
2315 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2316 i.op[op].regs->reg_name,
2320 #if REGISTER_WARNINGS
2322 && (i.tm.operand_types[op] & InOutPortReg) == 0)
2323 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2324 (i.op[op].regs + (i.types[op] & Reg16
2325 ? REGNAM_AL - REGNAM_AX
2326 : REGNAM_AL - REGNAM_EAX))->reg_name,
2327 i.op[op].regs->reg_name,
2332 /* Any other register is bad. */
2333 if (i.types[op] & (Reg | RegMMX | RegXMM
2335 | Control | Debug | Test
2336 | FloatReg | FloatAcc))
2338 as_bad (_("`%%%s' not allowed with `%s%c'"),
2339 i.op[op].regs->reg_name,
2353 for (op = i.operands; --op >= 0;)
2354 /* Reject eight bit registers, except where the template requires
2355 them. (eg. movzb) */
2356 if ((i.types[op] & Reg8) != 0
2357 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2359 as_bad (_("`%%%s' not allowed with `%s%c'"),
2360 i.op[op].regs->reg_name,
2365 /* Warn if the e prefix on a general reg is missing. */
2366 else if ((!quiet_warnings || flag_code == CODE_64BIT)
2367 && (i.types[op] & Reg16) != 0
2368 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2370 /* Prohibit these changes in the 64bit mode, since the
2371 lowering is more complicated. */
2372 if (flag_code == CODE_64BIT)
2374 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2375 i.op[op].regs->reg_name,
2379 #if REGISTER_WARNINGS
2381 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2382 (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
2383 i.op[op].regs->reg_name,
2387 /* Warn if the r prefix on a general reg is missing. */
2388 else if ((i.types[op] & Reg64) != 0
2389 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2391 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2392 i.op[op].regs->reg_name,
2404 for (op = i.operands; --op >= 0; )
2405 /* Reject eight bit registers, except where the template requires
2406 them. (eg. movzb) */
2407 if ((i.types[op] & Reg8) != 0
2408 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2410 as_bad (_("`%%%s' not allowed with `%s%c'"),
2411 i.op[op].regs->reg_name,
2416 /* Warn if the e prefix on a general reg is missing. */
2417 else if (((i.types[op] & Reg16) != 0
2418 || (i.types[op] & Reg32) != 0)
2419 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2421 /* Prohibit these changes in the 64bit mode, since the
2422 lowering is more complicated. */
2423 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2424 i.op[op].regs->reg_name,
2435 for (op = i.operands; --op >= 0;)
2436 /* Reject eight bit registers, except where the template requires
2437 them. (eg. movzb) */
2438 if ((i.types[op] & Reg8) != 0
2439 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2441 as_bad (_("`%%%s' not allowed with `%s%c'"),
2442 i.op[op].regs->reg_name,
2447 /* Warn if the e prefix on a general reg is present. */
2448 else if ((!quiet_warnings || flag_code == CODE_64BIT)
2449 && (i.types[op] & Reg32) != 0
2450 && (i.tm.operand_types[op] & (Reg16 | Acc)) != 0)
2452 /* Prohibit these changes in the 64bit mode, since the
2453 lowering is more complicated. */
2454 if (flag_code == CODE_64BIT)
2456 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2457 i.op[op].regs->reg_name,
2462 #if REGISTER_WARNINGS
2463 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2464 (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
2465 i.op[op].regs->reg_name,
2475 unsigned int overlap0, overlap1, overlap2;
2477 overlap0 = i.types[0] & i.tm.operand_types[0];
2478 if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32 | Imm32S))
2479 && overlap0 != Imm8 && overlap0 != Imm8S
2480 && overlap0 != Imm16 && overlap0 != Imm32S
2481 && overlap0 != Imm32 && overlap0 != Imm64)
2485 overlap0 &= (i.suffix == BYTE_MNEM_SUFFIX
2487 : (i.suffix == WORD_MNEM_SUFFIX
2489 : (i.suffix == QWORD_MNEM_SUFFIX
2493 else if (overlap0 == (Imm16 | Imm32S | Imm32)
2494 || overlap0 == (Imm16 | Imm32)
2495 || overlap0 == (Imm16 | Imm32S))
2497 overlap0 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
2500 if (overlap0 != Imm8 && overlap0 != Imm8S
2501 && overlap0 != Imm16 && overlap0 != Imm32S
2502 && overlap0 != Imm32 && overlap0 != Imm64)
2504 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
2508 i.types[0] = overlap0;
2510 overlap1 = i.types[1] & i.tm.operand_types[1];
2511 if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32S | Imm32))
2512 && overlap1 != Imm8 && overlap1 != Imm8S
2513 && overlap1 != Imm16 && overlap1 != Imm32S
2514 && overlap1 != Imm32 && overlap1 != Imm64)
2518 overlap1 &= (i.suffix == BYTE_MNEM_SUFFIX
2520 : (i.suffix == WORD_MNEM_SUFFIX
2522 : (i.suffix == QWORD_MNEM_SUFFIX
2526 else if (overlap1 == (Imm16 | Imm32 | Imm32S)
2527 || overlap1 == (Imm16 | Imm32)
2528 || overlap1 == (Imm16 | Imm32S))
2530 overlap1 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
2533 if (overlap1 != Imm8 && overlap1 != Imm8S
2534 && overlap1 != Imm16 && overlap1 != Imm32S
2535 && overlap1 != Imm32 && overlap1 != Imm64)
2537 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size %x %c"),overlap1, i.suffix);
2541 i.types[1] = overlap1;
2543 overlap2 = i.types[2] & i.tm.operand_types[2];
2544 assert ((overlap2 & Imm) == 0);
2545 i.types[2] = overlap2;
2553 /* Default segment register this instruction will use for memory
2554 accesses. 0 means unknown. This is only for optimizing out
2555 unnecessary segment overrides. */
2556 const seg_entry *default_seg = 0;
2558 /* The imul $imm, %reg instruction is converted into
2559 imul $imm, %reg, %reg, and the clr %reg instruction
2560 is converted into xor %reg, %reg. */
2561 if (i.tm.opcode_modifier & regKludge)
2563 unsigned int first_reg_op = (i.types[0] & Reg) ? 0 : 1;
2564 /* Pretend we saw the extra register operand. */
2565 assert (i.op[first_reg_op + 1].regs == 0);
2566 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
2567 i.types[first_reg_op + 1] = i.types[first_reg_op];
2571 if (i.tm.opcode_modifier & ShortForm)
2573 /* The register or float register operand is in operand 0 or 1. */
2574 unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
2575 /* Register goes in low 3 bits of opcode. */
2576 i.tm.base_opcode |= i.op[op].regs->reg_num;
2577 if ((i.op[op].regs->reg_flags & RegRex) != 0)
2579 if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
2581 /* Warn about some common errors, but press on regardless.
2582 The first case can be generated by gcc (<= 2.8.1). */
2583 if (i.operands == 2)
2585 /* Reversed arguments on faddp, fsubp, etc. */
2586 as_warn (_("translating to `%s %%%s,%%%s'"), i.tm.name,
2587 i.op[1].regs->reg_name,
2588 i.op[0].regs->reg_name);
2592 /* Extraneous `l' suffix on fp insn. */
2593 as_warn (_("translating to `%s %%%s'"), i.tm.name,
2594 i.op[0].regs->reg_name);
2598 else if (i.tm.opcode_modifier & Modrm)
2600 /* The opcode is completed (modulo i.tm.extension_opcode which
2601 must be put into the modrm byte).
2602 Now, we make the modrm & index base bytes based on all the
2603 info we've collected. */
2605 default_seg = build_modrm_byte ();
2607 else if (i.tm.opcode_modifier & (Seg2ShortForm | Seg3ShortForm))
2609 if (i.tm.base_opcode == POP_SEG_SHORT
2610 && i.op[0].regs->reg_num == 1)
2612 as_bad (_("you can't `pop %%cs'"));
2615 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
2616 if ((i.op[0].regs->reg_flags & RegRex) != 0)
2619 else if ((i.tm.base_opcode & ~(D | W)) == MOV_AX_DISP32)
2623 else if ((i.tm.opcode_modifier & IsString) != 0)
2625 /* For the string instructions that allow a segment override
2626 on one of their operands, the default segment is ds. */
2630 /* If a segment was explicitly specified,
2631 and the specified segment is not the default,
2632 use an opcode prefix to select it.
2633 If we never figured out what the default segment is,
2634 then default_seg will be zero at this point,
2635 and the specified segment prefix will always be used. */
2636 if ((i.seg[0]) && (i.seg[0] != default_seg))
2638 if (!add_prefix (i.seg[0]->seg_prefix))
2644 static const seg_entry *
2647 const seg_entry *default_seg = 0;
2649 /* i.reg_operands MUST be the number of real register operands;
2650 implicit registers do not count. */
2651 if (i.reg_operands == 2)
2653 unsigned int source, dest;
2654 source = ((i.types[0]
2655 & (Reg | RegMMX | RegXMM
2657 | Control | Debug | Test))
2662 /* One of the register operands will be encoded in the i.tm.reg
2663 field, the other in the combined i.tm.mode and i.tm.regmem
2664 fields. If no form of this instruction supports a memory
2665 destination operand, then we assume the source operand may
2666 sometimes be a memory operand and so we need to store the
2667 destination in the i.rm.reg field. */
2668 if ((i.tm.operand_types[dest] & AnyMem) == 0)
2670 i.rm.reg = i.op[dest].regs->reg_num;
2671 i.rm.regmem = i.op[source].regs->reg_num;
2672 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
2674 if ((i.op[source].regs->reg_flags & RegRex) != 0)
2679 i.rm.reg = i.op[source].regs->reg_num;
2680 i.rm.regmem = i.op[dest].regs->reg_num;
2681 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
2683 if ((i.op[source].regs->reg_flags & RegRex) != 0)
2688 { /* If it's not 2 reg operands... */
2691 unsigned int fake_zero_displacement = 0;
2692 unsigned int op = ((i.types[0] & AnyMem)
2694 : (i.types[1] & AnyMem) ? 1 : 2);
2698 if (i.base_reg == 0)
2701 if (!i.disp_operands)
2702 fake_zero_displacement = 1;
2703 if (i.index_reg == 0)
2705 /* Operand is just <disp> */
2706 if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0)
2707 && (flag_code != CODE_64BIT))
2709 i.rm.regmem = NO_BASE_REGISTER_16;
2710 i.types[op] &= ~Disp;
2711 i.types[op] |= Disp16;
2713 else if (flag_code != CODE_64BIT
2714 || (i.prefix[ADDR_PREFIX] != 0))
2716 i.rm.regmem = NO_BASE_REGISTER;
2717 i.types[op] &= ~Disp;
2718 i.types[op] |= Disp32;
2722 /* 64bit mode overwrites the 32bit absolute
2723 addressing by RIP relative addressing and
2724 absolute addressing is encoded by one of the
2725 redundant SIB forms. */
2726 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2727 i.sib.base = NO_BASE_REGISTER;
2728 i.sib.index = NO_INDEX_REGISTER;
2729 i.types[op] &= ~Disp;
2730 i.types[op] |= Disp32S;
2733 else /* !i.base_reg && i.index_reg */
2735 i.sib.index = i.index_reg->reg_num;
2736 i.sib.base = NO_BASE_REGISTER;
2737 i.sib.scale = i.log2_scale_factor;
2738 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2739 i.types[op] &= ~Disp;
2740 if (flag_code != CODE_64BIT)
2741 i.types[op] |= Disp32; /* Must be 32 bit */
2743 i.types[op] |= Disp32S;
2744 if ((i.index_reg->reg_flags & RegRex) != 0)
2748 /* RIP addressing for 64bit mode. */
2749 else if (i.base_reg->reg_type == BaseIndex)
2751 i.rm.regmem = NO_BASE_REGISTER;
2752 i.types[op] &= ~Disp;
2753 i.types[op] |= Disp32S;
2754 i.flags[op] = Operand_PCrel;
2756 else if (i.base_reg->reg_type & Reg16)
2758 switch (i.base_reg->reg_num)
2761 if (i.index_reg == 0)
2763 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
2764 i.rm.regmem = i.index_reg->reg_num - 6;
2768 if (i.index_reg == 0)
2771 if ((i.types[op] & Disp) == 0)
2773 /* fake (%bp) into 0(%bp) */
2774 i.types[op] |= Disp8;
2775 fake_zero_displacement = 1;
2778 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
2779 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
2781 default: /* (%si) -> 4 or (%di) -> 5 */
2782 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
2784 i.rm.mode = mode_from_disp_size (i.types[op]);
2786 else /* i.base_reg and 32/64 bit mode */
2788 if (flag_code == CODE_64BIT
2789 && (i.types[op] & Disp))
2791 if (i.types[op] & Disp8)
2792 i.types[op] = Disp8 | Disp32S;
2794 i.types[op] = Disp32S;
2796 i.rm.regmem = i.base_reg->reg_num;
2797 if ((i.base_reg->reg_flags & RegRex) != 0)
2799 i.sib.base = i.base_reg->reg_num;
2800 /* x86-64 ignores REX prefix bit here to avoid decoder
2802 if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
2805 if (i.disp_operands == 0)
2807 fake_zero_displacement = 1;
2808 i.types[op] |= Disp8;
2811 else if (i.base_reg->reg_num == ESP_REG_NUM)
2815 i.sib.scale = i.log2_scale_factor;
2816 if (i.index_reg == 0)
2818 /* <disp>(%esp) becomes two byte modrm with no index
2819 register. We've already stored the code for esp
2820 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
2821 Any base register besides %esp will not use the
2822 extra modrm byte. */
2823 i.sib.index = NO_INDEX_REGISTER;
2824 #if !SCALE1_WHEN_NO_INDEX
2825 /* Another case where we force the second modrm byte. */
2826 if (i.log2_scale_factor)
2827 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2832 i.sib.index = i.index_reg->reg_num;
2833 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2834 if ((i.index_reg->reg_flags & RegRex) != 0)
2837 i.rm.mode = mode_from_disp_size (i.types[op]);
2840 if (fake_zero_displacement)
2842 /* Fakes a zero displacement assuming that i.types[op]
2843 holds the correct displacement size. */
2846 assert (i.op[op].disps == 0);
2847 exp = &disp_expressions[i.disp_operands++];
2848 i.op[op].disps = exp;
2849 exp->X_op = O_constant;
2850 exp->X_add_number = 0;
2851 exp->X_add_symbol = (symbolS *) 0;
2852 exp->X_op_symbol = (symbolS *) 0;
2856 /* Fill in i.rm.reg or i.rm.regmem field with register operand
2857 (if any) based on i.tm.extension_opcode. Again, we must be
2858 careful to make sure that segment/control/debug/test/MMX
2859 registers are coded into the i.rm.reg field. */
2864 & (Reg | RegMMX | RegXMM
2866 | Control | Debug | Test))
2869 & (Reg | RegMMX | RegXMM
2871 | Control | Debug | Test))
2874 /* If there is an extension opcode to put here, the register
2875 number must be put into the regmem field. */
2876 if (i.tm.extension_opcode != None)
2878 i.rm.regmem = i.op[op].regs->reg_num;
2879 if ((i.op[op].regs->reg_flags & RegRex) != 0)
2884 i.rm.reg = i.op[op].regs->reg_num;
2885 if ((i.op[op].regs->reg_flags & RegRex) != 0)
2889 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
2890 must set it to 3 to indicate this is a register operand
2891 in the regmem field. */
2892 if (!i.mem_operands)
2896 /* Fill in i.rm.reg field with extension opcode (if any). */
2897 if (i.tm.extension_opcode != None)
2898 i.rm.reg = i.tm.extension_opcode;
2909 relax_substateT subtype;
2914 if (flag_code == CODE_16BIT)
2918 if (i.prefix[DATA_PREFIX] != 0)
2924 /* Pentium4 branch hints. */
2925 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
2926 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2931 if (i.prefix[REX_PREFIX] != 0)
2937 if (i.prefixes != 0 && !intel_syntax)
2938 as_warn (_("skipping prefixes on this instruction"));
2940 /* It's always a symbol; End frag & setup for relax.
2941 Make sure there is enough room in this frag for the largest
2942 instruction we may generate in md_convert_frag. This is 2
2943 bytes for the opcode and room for the prefix and largest
2945 frag_grow (prefix + 2 + 4);
2946 /* Prefix and 1 opcode byte go in fr_fix. */
2947 p = frag_more (prefix + 1);
2948 if (i.prefix[DATA_PREFIX] != 0)
2949 *p++ = DATA_PREFIX_OPCODE;
2950 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
2951 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
2952 *p++ = i.prefix[SEG_PREFIX];
2953 if (i.prefix[REX_PREFIX] != 0)
2954 *p++ = i.prefix[REX_PREFIX];
2955 *p = i.tm.base_opcode;
2957 if ((unsigned char) *p == JUMP_PC_RELATIVE)
2958 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL);
2959 else if ((cpu_arch_flags & Cpu386) != 0)
2960 subtype = ENCODE_RELAX_STATE (COND_JUMP, SMALL);
2962 subtype = ENCODE_RELAX_STATE (COND_JUMP86, SMALL);
2965 sym = i.op[0].disps->X_add_symbol;
2966 off = i.op[0].disps->X_add_number;
2968 if (i.op[0].disps->X_op != O_constant
2969 && i.op[0].disps->X_op != O_symbol)
2971 /* Handle complex expressions. */
2972 sym = make_expr_symbol (i.op[0].disps);
2976 /* 1 possible extra opcode + 4 byte displacement go in var part.
2977 Pass reloc in fr_var. */
2978 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
2987 if (i.tm.opcode_modifier & JumpByte)
2989 /* This is a loop or jecxz type instruction. */
2991 if (i.prefix[ADDR_PREFIX] != 0)
2993 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
2996 /* Pentium4 branch hints. */
2997 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
2998 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
3000 FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
3009 if (flag_code == CODE_16BIT)
3012 if (i.prefix[DATA_PREFIX] != 0)
3014 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
3024 if (i.prefix[REX_PREFIX] != 0)
3026 FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
3030 if (i.prefixes != 0 && !intel_syntax)
3031 as_warn (_("skipping prefixes on this instruction"));
3033 p = frag_more (1 + size);
3034 *p++ = i.tm.base_opcode;
3036 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3037 i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
3041 output_interseg_jump ()
3049 if (flag_code == CODE_16BIT)
3053 if (i.prefix[DATA_PREFIX] != 0)
3059 if (i.prefix[REX_PREFIX] != 0)
3069 if (i.prefixes != 0 && !intel_syntax)
3070 as_warn (_("skipping prefixes on this instruction"));
3072 /* 1 opcode; 2 segment; offset */
3073 p = frag_more (prefix + 1 + 2 + size);
3075 if (i.prefix[DATA_PREFIX] != 0)
3076 *p++ = DATA_PREFIX_OPCODE;
3078 if (i.prefix[REX_PREFIX] != 0)
3079 *p++ = i.prefix[REX_PREFIX];
3081 *p++ = i.tm.base_opcode;
3082 if (i.op[1].imms->X_op == O_constant)
3084 offsetT n = i.op[1].imms->X_add_number;
3087 && !fits_in_unsigned_word (n)
3088 && !fits_in_signed_word (n))
3090 as_bad (_("16-bit jump out of range"));
3093 md_number_to_chars (p, n, size);
3096 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3097 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
3098 if (i.op[0].imms->X_op != O_constant)
3099 as_bad (_("can't handle non absolute segment in `%s'"),
3101 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
3107 /* Tie dwarf2 debug info to the address at the start of the insn.
3108 We can't do this after the insn has been output as the current
3109 frag may have been closed off. eg. by frag_var. */
3110 dwarf2_emit_insn (0);
3113 if (i.tm.opcode_modifier & Jump)
3115 else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
3117 else if (i.tm.opcode_modifier & JumpInterSegment)
3118 output_interseg_jump ();
3121 /* Output normal instructions here. */
3125 /* All opcodes on i386 have either 1 or 2 bytes. We may use third
3126 byte for the SSE instructions to specify a prefix they require. */
3127 if (i.tm.base_opcode & 0xff0000)
3128 add_prefix ((i.tm.base_opcode >> 16) & 0xff);
3130 /* The prefix bytes. */
3132 q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
3138 md_number_to_chars (p, (valueT) *q, 1);
3142 /* Now the opcode; be careful about word order here! */
3143 if (fits_in_unsigned_byte (i.tm.base_opcode))
3145 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
3150 /* Put out high byte first: can't use md_number_to_chars! */
3151 *p++ = (i.tm.base_opcode >> 8) & 0xff;
3152 *p = i.tm.base_opcode & 0xff;
3155 /* Now the modrm byte and sib byte (if present). */
3156 if (i.tm.opcode_modifier & Modrm)
3159 md_number_to_chars (p,
3160 (valueT) (i.rm.regmem << 0
3164 /* If i.rm.regmem == ESP (4)
3165 && i.rm.mode != (Register mode)
3167 ==> need second modrm byte. */
3168 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
3170 && !(i.base_reg && (i.base_reg->reg_type & Reg16) != 0))
3173 md_number_to_chars (p,
3174 (valueT) (i.sib.base << 0
3176 | i.sib.scale << 6),
3181 if (i.disp_operands)
3193 #endif /* DEBUG386 */
3202 for (n = 0; n < i.operands; n++)
3204 if (i.types[n] & Disp)
3206 if (i.op[n].disps->X_op == O_constant)
3212 if (i.types[n] & (Disp8 | Disp16 | Disp64))
3215 if (i.types[n] & Disp8)
3217 if (i.types[n] & Disp64)
3220 val = offset_in_range (i.op[n].disps->X_add_number,
3222 p = frag_more (size);
3223 md_number_to_chars (p, val, size);
3229 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
3231 /* The PC relative address is computed relative
3232 to the instruction boundary, so in case immediate
3233 fields follows, we need to adjust the value. */
3234 if (pcrel && i.imm_operands)
3239 for (n1 = 0; n1 < i.operands; n1++)
3240 if (i.types[n1] & Imm)
3242 if (i.types[n1] & (Imm8 | Imm8S | Imm16 | Imm64))
3245 if (i.types[n1] & (Imm8 | Imm8S))
3247 if (i.types[n1] & Imm64)
3252 /* We should find the immediate. */
3253 if (n1 == i.operands)
3255 i.op[n].disps->X_add_number -= imm_size;
3258 if (i.types[n] & Disp32S)
3261 if (i.types[n] & (Disp16 | Disp64))
3264 if (i.types[n] & Disp64)
3268 p = frag_more (size);
3269 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3270 i.op[n].disps, pcrel,
3271 reloc (size, pcrel, sign, i.reloc[n]));
3283 for (n = 0; n < i.operands; n++)
3285 if (i.types[n] & Imm)
3287 if (i.op[n].imms->X_op == O_constant)
3293 if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
3296 if (i.types[n] & (Imm8 | Imm8S))
3298 else if (i.types[n] & Imm64)
3301 val = offset_in_range (i.op[n].imms->X_add_number,
3303 p = frag_more (size);
3304 md_number_to_chars (p, val, size);
3308 /* Not absolute_section.
3309 Need a 32-bit fixup (don't support 8bit
3310 non-absolute imms). Try to support other
3312 RELOC_ENUM reloc_type;
3316 if ((i.types[n] & (Imm32S))
3317 && i.suffix == QWORD_MNEM_SUFFIX)
3319 if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
3322 if (i.types[n] & (Imm8 | Imm8S))
3324 if (i.types[n] & Imm64)
3328 p = frag_more (size);
3329 reloc_type = reloc (size, 0, sign, i.reloc[n]);
3330 #ifdef BFD_ASSEMBLER
3331 if (reloc_type == BFD_RELOC_32
3333 && GOT_symbol == i.op[n].imms->X_add_symbol
3334 && (i.op[n].imms->X_op == O_symbol
3335 || (i.op[n].imms->X_op == O_add
3336 && ((symbol_get_value_expression
3337 (i.op[n].imms->X_op_symbol)->X_op)
3340 /* We don't support dynamic linking on x86-64 yet. */
3341 if (flag_code == CODE_64BIT)
3343 reloc_type = BFD_RELOC_386_GOTPC;
3344 i.op[n].imms->X_add_number += 3;
3347 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3348 i.op[n].imms, 0, reloc_type);
3355 static char *lex_got PARAMS ((RELOC_ENUM *, int *));
3357 /* Parse operands of the form
3358 <symbol>@GOTOFF+<nnn>
3359 and similar .plt or .got references.
3361 If we find one, set up the correct relocation in RELOC and copy the
3362 input string, minus the `@GOTOFF' into a malloc'd buffer for
3363 parsing by the calling routine. Return this buffer, and if ADJUST
3364 is non-null set it to the length of the string we removed from the
3365 input line. Otherwise return NULL. */
3367 lex_got (reloc, adjust)
3371 static const char * const mode_name[NUM_FLAG_CODE] = { "32", "16", "64" };
3372 static const struct {
3374 const RELOC_ENUM rel[NUM_FLAG_CODE];
3376 { "PLT", { BFD_RELOC_386_PLT32, 0, BFD_RELOC_X86_64_PLT32 } },
3377 { "GOTOFF", { BFD_RELOC_386_GOTOFF, 0, 0 } },
3378 { "GOTPCREL", { 0, 0, BFD_RELOC_X86_64_GOTPCREL } },
3379 { "TLSGD", { BFD_RELOC_386_TLS_GD, 0, 0 } },
3380 { "TLSLDM", { BFD_RELOC_386_TLS_LDM, 0, 0 } },
3381 { "GOTTPOFF", { BFD_RELOC_386_TLS_IE_32, 0, 0 } },
3382 { "TPOFF", { BFD_RELOC_386_TLS_LE_32, 0, 0 } },
3383 { "NTPOFF", { BFD_RELOC_386_TLS_LE, 0, 0 } },
3384 { "DTPOFF", { BFD_RELOC_386_TLS_LDO_32, 0, 0 } },
3385 { "GOT", { BFD_RELOC_386_GOT32, 0, BFD_RELOC_X86_64_GOT32 } }
3390 for (cp = input_line_pointer; *cp != '@'; cp++)
3391 if (is_end_of_line[(unsigned char) *cp])
3394 for (j = 0; j < sizeof (gotrel) / sizeof (gotrel[0]); j++)
3398 len = strlen (gotrel[j].str);
3399 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
3401 if (gotrel[j].rel[(unsigned int) flag_code] != 0)
3404 char *tmpbuf, *past_reloc;
3406 *reloc = gotrel[j].rel[(unsigned int) flag_code];
3410 if (GOT_symbol == NULL)
3411 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
3413 /* Replace the relocation token with ' ', so that
3414 errors like foo@GOTOFF1 will be detected. */
3416 /* The length of the first part of our input line. */
3417 first = cp - input_line_pointer;
3419 /* The second part goes from after the reloc token until
3420 (and including) an end_of_line char. Don't use strlen
3421 here as the end_of_line char may not be a NUL. */
3422 past_reloc = cp + 1 + len;
3423 for (cp = past_reloc; !is_end_of_line[(unsigned char) *cp++]; )
3425 second = cp - past_reloc;
3427 /* Allocate and copy string. The trailing NUL shouldn't
3428 be necessary, but be safe. */
3429 tmpbuf = xmalloc (first + second + 2);
3430 memcpy (tmpbuf, input_line_pointer, first);
3431 tmpbuf[first] = ' ';
3432 memcpy (tmpbuf + first + 1, past_reloc, second);
3433 tmpbuf[first + second + 1] = '\0';
3437 as_bad (_("@%s reloc is not supported in %s bit mode"),
3438 gotrel[j].str, mode_name[(unsigned int) flag_code]);
3443 /* Might be a symbol version string. Don't as_bad here. */
3447 /* x86_cons_fix_new is called via the expression parsing code when a
3448 reloc is needed. We use this hook to get the correct .got reloc. */
3449 static RELOC_ENUM got_reloc = NO_RELOC;
3452 x86_cons_fix_new (frag, off, len, exp)
3458 RELOC_ENUM r = reloc (len, 0, 0, got_reloc);
3459 got_reloc = NO_RELOC;
3460 fix_new_exp (frag, off, len, exp, 0, r);
3464 x86_cons (exp, size)
3470 /* Handle @GOTOFF and the like in an expression. */
3472 char *gotfree_input_line;
3475 save = input_line_pointer;
3476 gotfree_input_line = lex_got (&got_reloc, &adjust);
3477 if (gotfree_input_line)
3478 input_line_pointer = gotfree_input_line;
3482 if (gotfree_input_line)
3484 /* expression () has merrily parsed up to the end of line,
3485 or a comma - in the wrong buffer. Transfer how far
3486 input_line_pointer has moved to the right buffer. */
3487 input_line_pointer = (save
3488 + (input_line_pointer - gotfree_input_line)
3490 free (gotfree_input_line);
3498 static int i386_immediate PARAMS ((char *));
3501 i386_immediate (imm_start)
3504 char *save_input_line_pointer;
3506 char *gotfree_input_line;
3511 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
3513 as_bad (_("only 1 or 2 immediate operands are allowed"));
3517 exp = &im_expressions[i.imm_operands++];
3518 i.op[this_operand].imms = exp;
3520 if (is_space_char (*imm_start))
3523 save_input_line_pointer = input_line_pointer;
3524 input_line_pointer = imm_start;
3527 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL);
3528 if (gotfree_input_line)
3529 input_line_pointer = gotfree_input_line;
3532 exp_seg = expression (exp);
3535 if (*input_line_pointer)
3536 as_bad (_("junk `%s' after expression"), input_line_pointer);
3538 input_line_pointer = save_input_line_pointer;
3540 if (gotfree_input_line)
3541 free (gotfree_input_line);
3544 if (exp->X_op == O_absent || exp->X_op == O_big)
3546 /* Missing or bad expr becomes absolute 0. */
3547 as_bad (_("missing or invalid immediate expression `%s' taken as 0"),
3549 exp->X_op = O_constant;
3550 exp->X_add_number = 0;
3551 exp->X_add_symbol = (symbolS *) 0;
3552 exp->X_op_symbol = (symbolS *) 0;
3554 else if (exp->X_op == O_constant)
3556 /* Size it properly later. */
3557 i.types[this_operand] |= Imm64;
3558 /* If BFD64, sign extend val. */
3559 if (!use_rela_relocations)
3560 if ((exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
3561 exp->X_add_number = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
3563 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3565 #ifdef BFD_ASSEMBLER
3566 && OUTPUT_FLAVOR == bfd_target_aout_flavour
3568 && exp_seg != text_section
3569 && exp_seg != data_section
3570 && exp_seg != bss_section
3571 && exp_seg != undefined_section
3572 #ifdef BFD_ASSEMBLER
3573 && !bfd_is_com_section (exp_seg)
3577 #ifdef BFD_ASSEMBLER
3578 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3580 as_bad (_("unimplemented segment type %d in operand"), exp_seg);
3587 /* This is an address. The size of the address will be
3588 determined later, depending on destination register,
3589 suffix, or the default for the section. */
3590 i.types[this_operand] |= Imm8 | Imm16 | Imm32 | Imm32S | Imm64;
3596 static char *i386_scale PARAMS ((char *));
3603 char *save = input_line_pointer;
3605 input_line_pointer = scale;
3606 val = get_absolute_expression ();
3612 i.log2_scale_factor = 0;
3615 i.log2_scale_factor = 1;
3618 i.log2_scale_factor = 2;
3621 i.log2_scale_factor = 3;
3624 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
3626 input_line_pointer = save;
3629 if (i.log2_scale_factor != 0 && i.index_reg == 0)
3631 as_warn (_("scale factor of %d without an index register"),
3632 1 << i.log2_scale_factor);
3633 #if SCALE1_WHEN_NO_INDEX
3634 i.log2_scale_factor = 0;
3637 scale = input_line_pointer;
3638 input_line_pointer = save;
3642 static int i386_displacement PARAMS ((char *, char *));
3645 i386_displacement (disp_start, disp_end)
3651 char *save_input_line_pointer;
3653 char *gotfree_input_line;
3655 int bigdisp = Disp32;
3657 if (flag_code == CODE_64BIT)
3659 if (i.prefix[ADDR_PREFIX] == 0)
3662 else if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
3664 i.types[this_operand] |= bigdisp;
3666 exp = &disp_expressions[i.disp_operands];
3667 i.op[this_operand].disps = exp;
3669 save_input_line_pointer = input_line_pointer;
3670 input_line_pointer = disp_start;
3671 END_STRING_AND_SAVE (disp_end);
3673 #ifndef GCC_ASM_O_HACK
3674 #define GCC_ASM_O_HACK 0
3677 END_STRING_AND_SAVE (disp_end + 1);
3678 if ((i.types[this_operand] & BaseIndex) != 0
3679 && displacement_string_end[-1] == '+')
3681 /* This hack is to avoid a warning when using the "o"
3682 constraint within gcc asm statements.
3685 #define _set_tssldt_desc(n,addr,limit,type) \
3686 __asm__ __volatile__ ( \
3688 "movw %w1,2+%0\n\t" \
3690 "movb %b1,4+%0\n\t" \
3691 "movb %4,5+%0\n\t" \
3692 "movb $0,6+%0\n\t" \
3693 "movb %h1,7+%0\n\t" \
3695 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
3697 This works great except that the output assembler ends
3698 up looking a bit weird if it turns out that there is
3699 no offset. You end up producing code that looks like:
3712 So here we provide the missing zero. */
3714 *displacement_string_end = '0';
3718 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL);
3719 if (gotfree_input_line)
3720 input_line_pointer = gotfree_input_line;
3723 exp_seg = expression (exp);
3726 if (*input_line_pointer)
3727 as_bad (_("junk `%s' after expression"), input_line_pointer);
3729 RESTORE_END_STRING (disp_end + 1);
3731 RESTORE_END_STRING (disp_end);
3732 input_line_pointer = save_input_line_pointer;
3734 if (gotfree_input_line)
3735 free (gotfree_input_line);
3738 #ifdef BFD_ASSEMBLER
3739 /* We do this to make sure that the section symbol is in
3740 the symbol table. We will ultimately change the relocation
3741 to be relative to the beginning of the section. */
3742 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
3743 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
3745 if (exp->X_op != O_symbol)
3747 as_bad (_("bad expression used with @%s"),
3748 (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
3754 if (S_IS_LOCAL (exp->X_add_symbol)
3755 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
3756 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
3757 exp->X_op = O_subtract;
3758 exp->X_op_symbol = GOT_symbol;
3759 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
3760 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
3762 i.reloc[this_operand] = BFD_RELOC_32;
3766 if (exp->X_op == O_absent || exp->X_op == O_big)
3768 /* Missing or bad expr becomes absolute 0. */
3769 as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
3771 exp->X_op = O_constant;
3772 exp->X_add_number = 0;
3773 exp->X_add_symbol = (symbolS *) 0;
3774 exp->X_op_symbol = (symbolS *) 0;
3777 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
3778 if (exp->X_op != O_constant
3779 #ifdef BFD_ASSEMBLER
3780 && OUTPUT_FLAVOR == bfd_target_aout_flavour
3782 && exp_seg != text_section
3783 && exp_seg != data_section
3784 && exp_seg != bss_section
3785 && exp_seg != undefined_section)
3787 #ifdef BFD_ASSEMBLER
3788 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3790 as_bad (_("unimplemented segment type %d in operand"), exp_seg);
3795 else if (flag_code == CODE_64BIT)
3796 i.types[this_operand] |= Disp32S | Disp32;
3800 static int i386_index_check PARAMS ((const char *));
3802 /* Make sure the memory operand we've been dealt is valid.
3803 Return 1 on success, 0 on a failure. */
3806 i386_index_check (operand_string)
3807 const char *operand_string;
3810 #if INFER_ADDR_PREFIX
3816 if (flag_code == CODE_64BIT)
3818 if (i.prefix[ADDR_PREFIX] == 0)
3822 && ((i.base_reg->reg_type & Reg64) == 0)
3823 && (i.base_reg->reg_type != BaseIndex
3826 && ((i.index_reg->reg_type & (Reg64 | BaseIndex))
3827 != (Reg64 | BaseIndex))))
3834 && (i.base_reg->reg_type & (Reg32 | RegRex)) != Reg32)
3836 && ((i.index_reg->reg_type & (Reg32 | BaseIndex | RegRex))
3837 != (Reg32 | BaseIndex))))
3843 if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
3847 && ((i.base_reg->reg_type & (Reg16 | BaseIndex | RegRex))
3848 != (Reg16 | BaseIndex)))
3850 && (((i.index_reg->reg_type & (Reg16 | BaseIndex))
3851 != (Reg16 | BaseIndex))
3853 && i.base_reg->reg_num < 6
3854 && i.index_reg->reg_num >= 6
3855 && i.log2_scale_factor == 0))))
3862 && (i.base_reg->reg_type & (Reg32 | RegRex)) != Reg32)
3864 && ((i.index_reg->reg_type & (Reg32 | BaseIndex | RegRex))
3865 != (Reg32 | BaseIndex))))
3871 #if INFER_ADDR_PREFIX
3872 if (flag_code != CODE_64BIT
3873 && i.prefix[ADDR_PREFIX] == 0 && stackop_size != '\0')
3875 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
3877 /* Change the size of any displacement too. At most one of
3878 Disp16 or Disp32 is set.
3879 FIXME. There doesn't seem to be any real need for separate
3880 Disp16 and Disp32 flags. The same goes for Imm16 and Imm32.
3881 Removing them would probably clean up the code quite a lot. */
3882 if (i.types[this_operand] & (Disp16 | Disp32))
3883 i.types[this_operand] ^= (Disp16 | Disp32);
3888 as_bad (_("`%s' is not a valid base/index expression"),
3892 as_bad (_("`%s' is not a valid %s bit base/index expression"),
3894 flag_code_names[flag_code]);
3900 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
3904 i386_operand (operand_string)
3905 char *operand_string;
3909 char *op_string = operand_string;
3911 if (is_space_char (*op_string))
3914 /* We check for an absolute prefix (differentiating,
3915 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
3916 if (*op_string == ABSOLUTE_PREFIX)
3919 if (is_space_char (*op_string))
3921 i.types[this_operand] |= JumpAbsolute;
3924 /* Check if operand is a register. */
3925 if ((*op_string == REGISTER_PREFIX || allow_naked_reg)
3926 && (r = parse_register (op_string, &end_op)) != NULL)
3928 /* Check for a segment override by searching for ':' after a
3929 segment register. */
3931 if (is_space_char (*op_string))
3933 if (*op_string == ':' && (r->reg_type & (SReg2 | SReg3)))
3938 i.seg[i.mem_operands] = &es;
3941 i.seg[i.mem_operands] = &cs;
3944 i.seg[i.mem_operands] = &ss;
3947 i.seg[i.mem_operands] = &ds;
3950 i.seg[i.mem_operands] = &fs;
3953 i.seg[i.mem_operands] = &gs;
3957 /* Skip the ':' and whitespace. */
3959 if (is_space_char (*op_string))
3962 if (!is_digit_char (*op_string)
3963 && !is_identifier_char (*op_string)
3964 && *op_string != '('
3965 && *op_string != ABSOLUTE_PREFIX)
3967 as_bad (_("bad memory operand `%s'"), op_string);
3970 /* Handle case of %es:*foo. */
3971 if (*op_string == ABSOLUTE_PREFIX)
3974 if (is_space_char (*op_string))
3976 i.types[this_operand] |= JumpAbsolute;
3978 goto do_memory_reference;
3982 as_bad (_("junk `%s' after register"), op_string);
3985 i.types[this_operand] |= r->reg_type & ~BaseIndex;
3986 i.op[this_operand].regs = r;
3989 else if (*op_string == REGISTER_PREFIX)
3991 as_bad (_("bad register name `%s'"), op_string);
3994 else if (*op_string == IMMEDIATE_PREFIX)
3997 if (i.types[this_operand] & JumpAbsolute)
3999 as_bad (_("immediate operand illegal with absolute jump"));
4002 if (!i386_immediate (op_string))
4005 else if (is_digit_char (*op_string)
4006 || is_identifier_char (*op_string)
4007 || *op_string == '(')
4009 /* This is a memory reference of some sort. */
4012 /* Start and end of displacement string expression (if found). */
4013 char *displacement_string_start;
4014 char *displacement_string_end;
4016 do_memory_reference:
4017 if ((i.mem_operands == 1
4018 && (current_templates->start->opcode_modifier & IsString) == 0)
4019 || i.mem_operands == 2)
4021 as_bad (_("too many memory references for `%s'"),
4022 current_templates->start->name);
4026 /* Check for base index form. We detect the base index form by
4027 looking for an ')' at the end of the operand, searching
4028 for the '(' matching it, and finding a REGISTER_PREFIX or ','
4030 base_string = op_string + strlen (op_string);
4033 if (is_space_char (*base_string))
4036 /* If we only have a displacement, set-up for it to be parsed later. */
4037 displacement_string_start = op_string;
4038 displacement_string_end = base_string + 1;
4040 if (*base_string == ')')
4043 unsigned int parens_balanced = 1;
4044 /* We've already checked that the number of left & right ()'s are
4045 equal, so this loop will not be infinite. */
4049 if (*base_string == ')')
4051 if (*base_string == '(')
4054 while (parens_balanced);
4056 temp_string = base_string;
4058 /* Skip past '(' and whitespace. */
4060 if (is_space_char (*base_string))
4063 if (*base_string == ','
4064 || ((*base_string == REGISTER_PREFIX || allow_naked_reg)
4065 && (i.base_reg = parse_register (base_string, &end_op)) != NULL))
4067 displacement_string_end = temp_string;
4069 i.types[this_operand] |= BaseIndex;
4073 base_string = end_op;
4074 if (is_space_char (*base_string))
4078 /* There may be an index reg or scale factor here. */
4079 if (*base_string == ',')
4082 if (is_space_char (*base_string))
4085 if ((*base_string == REGISTER_PREFIX || allow_naked_reg)
4086 && (i.index_reg = parse_register (base_string, &end_op)) != NULL)
4088 base_string = end_op;
4089 if (is_space_char (*base_string))
4091 if (*base_string == ',')
4094 if (is_space_char (*base_string))
4097 else if (*base_string != ')')
4099 as_bad (_("expecting `,' or `)' after index register in `%s'"),
4104 else if (*base_string == REGISTER_PREFIX)
4106 as_bad (_("bad register name `%s'"), base_string);
4110 /* Check for scale factor. */
4111 if (*base_string != ')')
4113 char *end_scale = i386_scale (base_string);
4118 base_string = end_scale;
4119 if (is_space_char (*base_string))
4121 if (*base_string != ')')
4123 as_bad (_("expecting `)' after scale factor in `%s'"),
4128 else if (!i.index_reg)
4130 as_bad (_("expecting index register or scale factor after `,'; got '%c'"),
4135 else if (*base_string != ')')
4137 as_bad (_("expecting `,' or `)' after base register in `%s'"),
4142 else if (*base_string == REGISTER_PREFIX)
4144 as_bad (_("bad register name `%s'"), base_string);
4149 /* If there's an expression beginning the operand, parse it,
4150 assuming displacement_string_start and
4151 displacement_string_end are meaningful. */
4152 if (displacement_string_start != displacement_string_end)
4154 if (!i386_displacement (displacement_string_start,
4155 displacement_string_end))
4159 /* Special case for (%dx) while doing input/output op. */
4161 && i.base_reg->reg_type == (Reg16 | InOutPortReg)
4163 && i.log2_scale_factor == 0
4164 && i.seg[i.mem_operands] == 0
4165 && (i.types[this_operand] & Disp) == 0)
4167 i.types[this_operand] = InOutPortReg;
4171 if (i386_index_check (operand_string) == 0)
4177 /* It's not a memory operand; argh! */
4178 as_bad (_("invalid char %s beginning operand %d `%s'"),
4179 output_invalid (*op_string),
4184 return 1; /* Normal return. */
4187 /* md_estimate_size_before_relax()
4189 Called just before relax() for rs_machine_dependent frags. The x86
4190 assembler uses these frags to handle variable size jump
4193 Any symbol that is now undefined will not become defined.
4194 Return the correct fr_subtype in the frag.
4195 Return the initial "guess for variable size of frag" to caller.
4196 The guess is actually the growth beyond the fixed part. Whatever
4197 we do to grow the fixed or variable part contributes to our
4201 md_estimate_size_before_relax (fragP, segment)
4205 /* We've already got fragP->fr_subtype right; all we have to do is
4206 check for un-relaxable symbols. On an ELF system, we can't relax
4207 an externally visible symbol, because it may be overridden by a
4209 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
4210 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4211 || S_IS_EXTERNAL (fragP->fr_symbol)
4212 || S_IS_WEAK (fragP->fr_symbol)
4216 /* Symbol is undefined in this segment, or we need to keep a
4217 reloc so that weak symbols can be overridden. */
4218 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
4219 RELOC_ENUM reloc_type;
4220 unsigned char *opcode;
4223 if (fragP->fr_var != NO_RELOC)
4224 reloc_type = fragP->fr_var;
4226 reloc_type = BFD_RELOC_16_PCREL;
4228 reloc_type = BFD_RELOC_32_PCREL;
4230 old_fr_fix = fragP->fr_fix;
4231 opcode = (unsigned char *) fragP->fr_opcode;
4233 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
4236 /* Make jmp (0xeb) a (d)word displacement jump. */
4238 fragP->fr_fix += size;
4239 fix_new (fragP, old_fr_fix, size,
4241 fragP->fr_offset, 1,
4247 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
4249 /* Negate the condition, and branch past an
4250 unconditional jump. */
4253 /* Insert an unconditional jump. */
4255 /* We added two extra opcode bytes, and have a two byte
4257 fragP->fr_fix += 2 + 2;
4258 fix_new (fragP, old_fr_fix + 2, 2,
4260 fragP->fr_offset, 1,
4267 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
4270 fix_new (fragP, old_fr_fix, 1,
4272 fragP->fr_offset, 1,
4277 /* This changes the byte-displacement jump 0x7N
4278 to the (d)word-displacement jump 0x0f,0x8N. */
4279 opcode[1] = opcode[0] + 0x10;
4280 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4281 /* We've added an opcode byte. */
4282 fragP->fr_fix += 1 + size;
4283 fix_new (fragP, old_fr_fix + 1, size,
4285 fragP->fr_offset, 1,
4290 BAD_CASE (fragP->fr_subtype);
4294 return fragP->fr_fix - old_fr_fix;
4297 /* Guess size depending on current relax state. Initially the relax
4298 state will correspond to a short jump and we return 1, because
4299 the variable part of the frag (the branch offset) is one byte
4300 long. However, we can relax a section more than once and in that
4301 case we must either set fr_subtype back to the unrelaxed state,
4302 or return the value for the appropriate branch. */
4303 return md_relax_table[fragP->fr_subtype].rlx_length;
4306 /* Called after relax() is finished.
4308 In: Address of frag.
4309 fr_type == rs_machine_dependent.
4310 fr_subtype is what the address relaxed to.
4312 Out: Any fixSs and constants are set up.
4313 Caller will turn frag into a ".space 0". */
4315 #ifndef BFD_ASSEMBLER
4317 md_convert_frag (headers, sec, fragP)
4318 object_headers *headers ATTRIBUTE_UNUSED;
4319 segT sec ATTRIBUTE_UNUSED;
4323 md_convert_frag (abfd, sec, fragP)
4324 bfd *abfd ATTRIBUTE_UNUSED;
4325 segT sec ATTRIBUTE_UNUSED;
4329 unsigned char *opcode;
4330 unsigned char *where_to_put_displacement = NULL;
4331 offsetT target_address;
4332 offsetT opcode_address;
4333 unsigned int extension = 0;
4334 offsetT displacement_from_opcode_start;
4336 opcode = (unsigned char *) fragP->fr_opcode;
4338 /* Address we want to reach in file space. */
4339 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
4341 /* Address opcode resides at in file space. */
4342 opcode_address = fragP->fr_address + fragP->fr_fix;
4344 /* Displacement from opcode start to fill into instruction. */
4345 displacement_from_opcode_start = target_address - opcode_address;
4347 if ((fragP->fr_subtype & BIG) == 0)
4349 /* Don't have to change opcode. */
4350 extension = 1; /* 1 opcode + 1 displacement */
4351 where_to_put_displacement = &opcode[1];
4355 if (no_cond_jump_promotion
4356 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4357 as_warn_where (fragP->fr_file, fragP->fr_line, _("long jump required"));
4359 switch (fragP->fr_subtype)
4361 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
4362 extension = 4; /* 1 opcode + 4 displacement */
4364 where_to_put_displacement = &opcode[1];
4367 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
4368 extension = 2; /* 1 opcode + 2 displacement */
4370 where_to_put_displacement = &opcode[1];
4373 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
4374 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
4375 extension = 5; /* 2 opcode + 4 displacement */
4376 opcode[1] = opcode[0] + 0x10;
4377 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4378 where_to_put_displacement = &opcode[2];
4381 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
4382 extension = 3; /* 2 opcode + 2 displacement */
4383 opcode[1] = opcode[0] + 0x10;
4384 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4385 where_to_put_displacement = &opcode[2];
4388 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
4393 where_to_put_displacement = &opcode[3];
4397 BAD_CASE (fragP->fr_subtype);
4402 /* Now put displacement after opcode. */
4403 md_number_to_chars ((char *) where_to_put_displacement,
4404 (valueT) (displacement_from_opcode_start - extension),
4405 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
4406 fragP->fr_fix += extension;
4409 /* Size of byte displacement jmp. */
4410 int md_short_jump_size = 2;
4412 /* Size of dword displacement jmp. */
4413 int md_long_jump_size = 5;
4415 /* Size of relocation record. */
4416 const int md_reloc_size = 8;
4419 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
4421 addressT from_addr, to_addr;
4422 fragS *frag ATTRIBUTE_UNUSED;
4423 symbolS *to_symbol ATTRIBUTE_UNUSED;
4427 offset = to_addr - (from_addr + 2);
4428 /* Opcode for byte-disp jump. */
4429 md_number_to_chars (ptr, (valueT) 0xeb, 1);
4430 md_number_to_chars (ptr + 1, (valueT) offset, 1);
4434 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
4436 addressT from_addr, to_addr;
4437 fragS *frag ATTRIBUTE_UNUSED;
4438 symbolS *to_symbol ATTRIBUTE_UNUSED;
4442 offset = to_addr - (from_addr + 5);
4443 md_number_to_chars (ptr, (valueT) 0xe9, 1);
4444 md_number_to_chars (ptr + 1, (valueT) offset, 4);
4447 /* Apply a fixup (fixS) to segment data, once it has been determined
4448 by our caller that we have all the info we need to fix it up.
4450 On the 386, immediates, displacements, and data pointers are all in
4451 the same (little-endian) format, so we don't need to care about which
4455 md_apply_fix3 (fixP, valP, seg)
4456 /* The fix we're to put in. */
4458 /* Pointer to the value of the bits. */
4460 /* Segment fix is from. */
4461 segT seg ATTRIBUTE_UNUSED;
4463 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
4464 valueT value = *valP;
4466 #if defined (BFD_ASSEMBLER) && !defined (TE_Mach)
4469 switch (fixP->fx_r_type)
4475 fixP->fx_r_type = BFD_RELOC_32_PCREL;
4478 fixP->fx_r_type = BFD_RELOC_16_PCREL;
4481 fixP->fx_r_type = BFD_RELOC_8_PCREL;
4486 /* This is a hack. There should be a better way to handle this.
4487 This covers for the fact that bfd_install_relocation will
4488 subtract the current location (for partial_inplace, PC relative
4489 relocations); see more below. */
4490 if ((fixP->fx_r_type == BFD_RELOC_32_PCREL
4491 || fixP->fx_r_type == BFD_RELOC_16_PCREL
4492 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
4493 && fixP->fx_addsy && !use_rela_relocations)
4496 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
4498 || OUTPUT_FLAVOR == bfd_target_coff_flavour
4501 value += fixP->fx_where + fixP->fx_frag->fr_address;
4503 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4504 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
4506 segT fseg = S_GET_SEGMENT (fixP->fx_addsy);
4509 || (symbol_section_p (fixP->fx_addsy)
4510 && fseg != absolute_section))
4511 && !S_IS_EXTERNAL (fixP->fx_addsy)
4512 && !S_IS_WEAK (fixP->fx_addsy)
4513 && S_IS_DEFINED (fixP->fx_addsy)
4514 && !S_IS_COMMON (fixP->fx_addsy))
4516 /* Yes, we add the values in twice. This is because
4517 bfd_perform_relocation subtracts them out again. I think
4518 bfd_perform_relocation is broken, but I don't dare change
4520 value += fixP->fx_where + fixP->fx_frag->fr_address;
4524 #if defined (OBJ_COFF) && defined (TE_PE)
4525 /* For some reason, the PE format does not store a section
4526 address offset for a PC relative symbol. */
4527 if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
4528 value += md_pcrel_from (fixP);
4532 /* Fix a few things - the dynamic linker expects certain values here,
4533 and we must not dissappoint it. */
4534 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4535 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
4537 switch (fixP->fx_r_type)
4539 case BFD_RELOC_386_PLT32:
4540 case BFD_RELOC_X86_64_PLT32:
4541 /* Make the jump instruction point to the address of the operand. At
4542 runtime we merely add the offset to the actual PLT entry. */
4545 case BFD_RELOC_386_GOTPC:
4547 /* This is tough to explain. We end up with this one if we have
4548 * operands that look like "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal
4549 * here is to obtain the absolute address of the GOT, and it is strongly
4550 * preferable from a performance point of view to avoid using a runtime
4551 * relocation for this. The actual sequence of instructions often look
4557 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
4559 * The call and pop essentially return the absolute address of
4560 * the label .L66 and store it in %ebx. The linker itself will
4561 * ultimately change the first operand of the addl so that %ebx points to
4562 * the GOT, but to keep things simple, the .o file must have this operand
4563 * set so that it generates not the absolute address of .L66, but the
4564 * absolute address of itself. This allows the linker itself simply
4565 * treat a GOTPC relocation as asking for a pcrel offset to the GOT to be
4566 * added in, and the addend of the relocation is stored in the operand
4567 * field for the instruction itself.
4569 * Our job here is to fix the operand so that it would add the correct
4570 * offset so that %ebx would point to itself. The thing that is tricky is
4571 * that .-.L66 will point to the beginning of the instruction, so we need
4572 * to further modify the operand so that it will point to itself.
4573 * There are other cases where you have something like:
4575 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
4577 * and here no correction would be required. Internally in the assembler
4578 * we treat operands of this form as not being pcrel since the '.' is
4579 * explicitly mentioned, and I wonder whether it would simplify matters
4580 * to do it this way. Who knows. In earlier versions of the PIC patches,
4581 * the pcrel_adjust field was used to store the correction, but since the
4582 * expression is not pcrel, I felt it would be confusing to do it this
4587 case BFD_RELOC_386_GOT32:
4588 case BFD_RELOC_386_TLS_GD:
4589 case BFD_RELOC_386_TLS_LDM:
4590 case BFD_RELOC_386_TLS_LDO_32:
4591 case BFD_RELOC_386_TLS_IE_32:
4592 case BFD_RELOC_386_TLS_LE_32:
4593 case BFD_RELOC_386_TLS_LE:
4594 case BFD_RELOC_X86_64_GOT32:
4595 value = 0; /* Fully resolved at runtime. No addend. */
4597 case BFD_RELOC_386_GOTOFF:
4598 case BFD_RELOC_X86_64_GOTPCREL:
4601 case BFD_RELOC_VTABLE_INHERIT:
4602 case BFD_RELOC_VTABLE_ENTRY:
4609 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
4611 #endif /* defined (BFD_ASSEMBLER) && !defined (TE_Mach) */
4613 /* Are we finished with this relocation now? */
4614 if (fixP->fx_addsy == NULL)
4616 #ifdef BFD_ASSEMBLER
4617 else if (use_rela_relocations)
4619 fixP->fx_no_overflow = 1;
4620 /* Remember value for tc_gen_reloc. */
4621 fixP->fx_addnumber = value;
4625 md_number_to_chars (p, value, fixP->fx_size);
4628 #define MAX_LITTLENUMS 6
4630 /* Turn the string pointed to by litP into a floating point constant
4631 of type TYPE, and emit the appropriate bytes. The number of
4632 LITTLENUMS emitted is stored in *SIZEP. An error message is
4633 returned, or NULL on OK. */
4636 md_atof (type, litP, sizeP)
4642 LITTLENUM_TYPE words[MAX_LITTLENUMS];
4643 LITTLENUM_TYPE *wordP;
4665 return _("Bad call to md_atof ()");
4667 t = atof_ieee (input_line_pointer, type, words);
4669 input_line_pointer = t;
4671 *sizeP = prec * sizeof (LITTLENUM_TYPE);
4672 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
4673 the bigendian 386. */
4674 for (wordP = words + prec - 1; prec--;)
4676 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
4677 litP += sizeof (LITTLENUM_TYPE);
4682 char output_invalid_buf[8];
4689 sprintf (output_invalid_buf, "'%c'", c);
4691 sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
4692 return output_invalid_buf;
4695 /* REG_STRING starts *before* REGISTER_PREFIX. */
4697 static const reg_entry *
4698 parse_register (reg_string, end_op)
4702 char *s = reg_string;
4704 char reg_name_given[MAX_REG_NAME_SIZE + 1];
4707 /* Skip possible REGISTER_PREFIX and possible whitespace. */
4708 if (*s == REGISTER_PREFIX)
4711 if (is_space_char (*s))
4715 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
4717 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
4718 return (const reg_entry *) NULL;
4722 /* For naked regs, make sure that we are not dealing with an identifier.
4723 This prevents confusing an identifier like `eax_var' with register
4725 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
4726 return (const reg_entry *) NULL;
4730 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
4732 /* Handle floating point regs, allowing spaces in the (i) part. */
4733 if (r == i386_regtab /* %st is first entry of table */)
4735 if (is_space_char (*s))
4740 if (is_space_char (*s))
4742 if (*s >= '0' && *s <= '7')
4744 r = &i386_float_regtab[*s - '0'];
4746 if (is_space_char (*s))
4754 /* We have "%st(" then garbage. */
4755 return (const reg_entry *) NULL;
4760 && (r->reg_flags & (RegRex64 | RegRex)) != 0
4761 && flag_code != CODE_64BIT)
4763 return (const reg_entry *) NULL;
4769 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4770 const char *md_shortopts = "kVQ:sq";
4772 const char *md_shortopts = "q";
4775 struct option md_longopts[] = {
4776 #define OPTION_32 (OPTION_MD_BASE + 0)
4777 {"32", no_argument, NULL, OPTION_32},
4778 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4779 #define OPTION_64 (OPTION_MD_BASE + 1)
4780 {"64", no_argument, NULL, OPTION_64},
4782 {NULL, no_argument, NULL, 0}
4784 size_t md_longopts_size = sizeof (md_longopts);
4787 md_parse_option (c, arg)
4789 char *arg ATTRIBUTE_UNUSED;
4797 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4798 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
4799 should be emitted or not. FIXME: Not implemented. */
4803 /* -V: SVR4 argument to print version ID. */
4805 print_version_id ();
4808 /* -k: Ignore for FreeBSD compatibility. */
4813 /* -s: On i386 Solaris, this tells the native assembler to use
4814 .stab instead of .stab.excl. We always use .stab anyhow. */
4819 const char **list, **l;
4821 list = bfd_target_list ();
4822 for (l = list; *l != NULL; l++)
4823 if (strcmp (*l, "elf64-x86-64") == 0)
4825 default_arch = "x86_64";
4829 as_fatal (_("No compiled in support for x86_64"));
4836 default_arch = "i386";
4846 md_show_usage (stream)
4849 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4850 fprintf (stream, _("\
4852 -V print assembler version number\n\
4854 -q quieten some warnings\n\
4857 fprintf (stream, _("\
4858 -q quieten some warnings\n"));
4862 #ifdef BFD_ASSEMBLER
4863 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
4864 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
4866 /* Pick the target format to use. */
4869 i386_target_format ()
4871 if (!strcmp (default_arch, "x86_64"))
4872 set_code_flag (CODE_64BIT);
4873 else if (!strcmp (default_arch, "i386"))
4874 set_code_flag (CODE_32BIT);
4876 as_fatal (_("Unknown architecture"));
4877 switch (OUTPUT_FLAVOR)
4879 #ifdef OBJ_MAYBE_AOUT
4880 case bfd_target_aout_flavour:
4881 return AOUT_TARGET_FORMAT;
4883 #ifdef OBJ_MAYBE_COFF
4884 case bfd_target_coff_flavour:
4887 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
4888 case bfd_target_elf_flavour:
4890 if (flag_code == CODE_64BIT)
4891 use_rela_relocations = 1;
4892 return flag_code == CODE_64BIT ? "elf64-x86-64" : "elf32-i386";
4901 #endif /* OBJ_MAYBE_ more than one */
4903 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
4904 void i386_elf_emit_arch_note ()
4906 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
4907 && cpu_arch_name != NULL)
4910 asection *seg = now_seg;
4911 subsegT subseg = now_subseg;
4912 Elf_Internal_Note i_note;
4913 Elf_External_Note e_note;
4914 asection *note_secp;
4917 /* Create the .note section. */
4918 note_secp = subseg_new (".note", 0);
4919 bfd_set_section_flags (stdoutput,
4921 SEC_HAS_CONTENTS | SEC_READONLY);
4923 /* Process the arch string. */
4924 len = strlen (cpu_arch_name);
4926 i_note.namesz = len + 1;
4928 i_note.type = NT_ARCH;
4929 p = frag_more (sizeof (e_note.namesz));
4930 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
4931 p = frag_more (sizeof (e_note.descsz));
4932 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
4933 p = frag_more (sizeof (e_note.type));
4934 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
4935 p = frag_more (len + 1);
4936 strcpy (p, cpu_arch_name);
4938 frag_align (2, 0, 0);
4940 subseg_set (seg, subseg);
4944 #endif /* BFD_ASSEMBLER */
4947 md_undefined_symbol (name)
4950 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
4951 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
4952 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
4953 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
4957 if (symbol_find (name))
4958 as_bad (_("GOT already in symbol table"));
4959 GOT_symbol = symbol_new (name, undefined_section,
4960 (valueT) 0, &zero_address_frag);
4967 /* Round up a section size to the appropriate boundary. */
4970 md_section_align (segment, size)
4971 segT segment ATTRIBUTE_UNUSED;
4974 #ifdef BFD_ASSEMBLER
4975 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
4976 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
4978 /* For a.out, force the section size to be aligned. If we don't do
4979 this, BFD will align it for us, but it will not write out the
4980 final bytes of the section. This may be a bug in BFD, but it is
4981 easier to fix it here since that is how the other a.out targets
4985 align = bfd_get_section_alignment (stdoutput, segment);
4986 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
4994 /* On the i386, PC-relative offsets are relative to the start of the
4995 next instruction. That is, the address of the offset, plus its
4996 size, since the offset is always the last part of the insn. */
4999 md_pcrel_from (fixP)
5002 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
5009 int ignore ATTRIBUTE_UNUSED;
5013 temp = get_absolute_expression ();
5014 subseg_set (bss_section, (subsegT) temp);
5015 demand_empty_rest_of_line ();
5020 #ifdef BFD_ASSEMBLER
5023 i386_validate_fix (fixp)
5026 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
5028 /* GOTOFF relocation are nonsense in 64bit mode. */
5029 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
5031 if (flag_code != CODE_64BIT)
5033 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
5037 if (flag_code == CODE_64BIT)
5039 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
5046 tc_gen_reloc (section, fixp)
5047 asection *section ATTRIBUTE_UNUSED;
5051 bfd_reloc_code_real_type code;
5053 switch (fixp->fx_r_type)
5055 case BFD_RELOC_X86_64_PLT32:
5056 case BFD_RELOC_X86_64_GOT32:
5057 case BFD_RELOC_X86_64_GOTPCREL:
5058 case BFD_RELOC_386_PLT32:
5059 case BFD_RELOC_386_GOT32:
5060 case BFD_RELOC_386_GOTOFF:
5061 case BFD_RELOC_386_GOTPC:
5062 case BFD_RELOC_386_TLS_GD:
5063 case BFD_RELOC_386_TLS_LDM:
5064 case BFD_RELOC_386_TLS_LDO_32:
5065 case BFD_RELOC_386_TLS_IE_32:
5066 case BFD_RELOC_386_TLS_LE_32:
5067 case BFD_RELOC_386_TLS_LE:
5068 case BFD_RELOC_X86_64_32S:
5070 case BFD_RELOC_VTABLE_ENTRY:
5071 case BFD_RELOC_VTABLE_INHERIT:
5072 code = fixp->fx_r_type;
5077 switch (fixp->fx_size)
5080 as_bad_where (fixp->fx_file, fixp->fx_line,
5081 _("can not do %d byte pc-relative relocation"),
5083 code = BFD_RELOC_32_PCREL;
5085 case 1: code = BFD_RELOC_8_PCREL; break;
5086 case 2: code = BFD_RELOC_16_PCREL; break;
5087 case 4: code = BFD_RELOC_32_PCREL; break;
5092 switch (fixp->fx_size)
5095 as_bad_where (fixp->fx_file, fixp->fx_line,
5096 _("can not do %d byte relocation"),
5098 code = BFD_RELOC_32;
5100 case 1: code = BFD_RELOC_8; break;
5101 case 2: code = BFD_RELOC_16; break;
5102 case 4: code = BFD_RELOC_32; break;
5104 case 8: code = BFD_RELOC_64; break;
5111 if (code == BFD_RELOC_32
5113 && fixp->fx_addsy == GOT_symbol)
5115 /* We don't support GOTPC on 64bit targets. */
5116 if (flag_code == CODE_64BIT)
5118 code = BFD_RELOC_386_GOTPC;
5121 rel = (arelent *) xmalloc (sizeof (arelent));
5122 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5123 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5125 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
5126 if (!use_rela_relocations)
5128 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
5129 vtable entry to be used in the relocation's section offset. */
5130 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5131 rel->address = fixp->fx_offset;
5135 /* Use the rela in 64bit mode. */
5138 if (!fixp->fx_pcrel)
5139 rel->addend = fixp->fx_offset;
5143 case BFD_RELOC_X86_64_PLT32:
5144 case BFD_RELOC_X86_64_GOT32:
5145 case BFD_RELOC_X86_64_GOTPCREL:
5146 rel->addend = fixp->fx_offset - fixp->fx_size;
5149 rel->addend = (section->vma
5151 + fixp->fx_addnumber
5152 + md_pcrel_from (fixp));
5157 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
5158 if (rel->howto == NULL)
5160 as_bad_where (fixp->fx_file, fixp->fx_line,
5161 _("cannot represent relocation type %s"),
5162 bfd_get_reloc_code_name (code));
5163 /* Set howto to a garbage value so that we can keep going. */
5164 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
5165 assert (rel->howto != NULL);
5171 #else /* !BFD_ASSEMBLER */
5173 #if (defined(OBJ_AOUT) | defined(OBJ_BOUT))
5175 tc_aout_fix_to_chars (where, fixP, segment_address_in_file)
5178 relax_addressT segment_address_in_file;
5180 /* In: length of relocation (or of address) in chars: 1, 2 or 4.
5181 Out: GNU LD relocation length code: 0, 1, or 2. */
5183 static const unsigned char nbytes_r_length[] = { 42, 0, 1, 42, 2 };
5186 know (fixP->fx_addsy != NULL);
5188 md_number_to_chars (where,
5189 (valueT) (fixP->fx_frag->fr_address
5190 + fixP->fx_where - segment_address_in_file),
5193 r_symbolnum = (S_IS_DEFINED (fixP->fx_addsy)
5194 ? S_GET_TYPE (fixP->fx_addsy)
5195 : fixP->fx_addsy->sy_number);
5197 where[6] = (r_symbolnum >> 16) & 0x0ff;
5198 where[5] = (r_symbolnum >> 8) & 0x0ff;
5199 where[4] = r_symbolnum & 0x0ff;
5200 where[7] = ((((!S_IS_DEFINED (fixP->fx_addsy)) << 3) & 0x08)
5201 | ((nbytes_r_length[fixP->fx_size] << 1) & 0x06)
5202 | (((fixP->fx_pcrel << 0) & 0x01) & 0x0f));
5205 #endif /* OBJ_AOUT or OBJ_BOUT. */
5207 #if defined (I386COFF)
5210 tc_coff_fix2rtype (fixP)
5213 if (fixP->fx_r_type == R_IMAGEBASE)
5216 return (fixP->fx_pcrel ?
5217 (fixP->fx_size == 1 ? R_PCRBYTE :
5218 fixP->fx_size == 2 ? R_PCRWORD :
5220 (fixP->fx_size == 1 ? R_RELBYTE :
5221 fixP->fx_size == 2 ? R_RELWORD :
5226 tc_coff_sizemachdep (frag)
5230 return (frag->fr_next->fr_address - frag->fr_address);
5235 #endif /* I386COFF */
5237 #endif /* !BFD_ASSEMBLER */
5239 /* Parse operands using Intel syntax. This implements a recursive descent
5240 parser based on the BNF grammar published in Appendix B of the MASM 6.1
5243 FIXME: We do not recognize the full operand grammar defined in the MASM
5244 documentation. In particular, all the structure/union and
5245 high-level macro operands are missing.
5247 Uppercase words are terminals, lower case words are non-terminals.
5248 Objects surrounded by double brackets '[[' ']]' are optional. Vertical
5249 bars '|' denote choices. Most grammar productions are implemented in
5250 functions called 'intel_<production>'.
5252 Initial production is 'expr'.
5258 byteRegister AL | AH | BL | BH | CL | CH | DL | DH
5260 constant digits [[ radixOverride ]]
5262 dataType BYTE | WORD | DWORD | QWORD | XWORD
5295 gpRegister AX | EAX | BX | EBX | CX | ECX | DX | EDX
5296 | BP | EBP | SP | ESP | DI | EDI | SI | ESI
5298 hexdigit a | b | c | d | e | f
5299 | A | B | C | D | E | F
5309 register specialRegister
5313 segmentRegister CS | DS | ES | FS | GS | SS
5315 specialRegister CR0 | CR2 | CR3
5316 | DR0 | DR1 | DR2 | DR3 | DR6 | DR7
5317 | TR3 | TR4 | TR5 | TR6 | TR7
5319 We simplify the grammar in obvious places (e.g., register parsing is
5320 done by calling parse_register) and eliminate immediate left recursion
5321 to implement a recursive-descent parser.
5361 /* Parsing structure for the intel syntax parser. Used to implement the
5362 semantic actions for the operand grammar. */
5363 struct intel_parser_s
5365 char *op_string; /* The string being parsed. */
5366 int got_a_float; /* Whether the operand is a float. */
5367 int op_modifier; /* Operand modifier. */
5368 int is_mem; /* 1 if operand is memory reference. */
5369 const reg_entry *reg; /* Last register reference found. */
5370 char *disp; /* Displacement string being built. */
5373 static struct intel_parser_s intel_parser;
5375 /* Token structure for parsing intel syntax. */
5378 int code; /* Token code. */
5379 const reg_entry *reg; /* Register entry for register tokens. */
5380 char *str; /* String representation. */
5383 static struct intel_token cur_token, prev_token;
5385 /* Token codes for the intel parser. Since T_SHORT is already used
5386 by COFF, undefine it first to prevent a warning. */
5401 /* Prototypes for intel parser functions. */
5402 static int intel_match_token PARAMS ((int code));
5403 static void intel_get_token PARAMS ((void));
5404 static void intel_putback_token PARAMS ((void));
5405 static int intel_expr PARAMS ((void));
5406 static int intel_e05 PARAMS ((void));
5407 static int intel_e05_1 PARAMS ((void));
5408 static int intel_e06 PARAMS ((void));
5409 static int intel_e06_1 PARAMS ((void));
5410 static int intel_e09 PARAMS ((void));
5411 static int intel_e09_1 PARAMS ((void));
5412 static int intel_e10 PARAMS ((void));
5413 static int intel_e10_1 PARAMS ((void));
5414 static int intel_e11 PARAMS ((void));
5417 i386_intel_operand (operand_string, got_a_float)
5418 char *operand_string;
5424 /* Initialize token holders. */
5425 cur_token.code = prev_token.code = T_NIL;
5426 cur_token.reg = prev_token.reg = NULL;
5427 cur_token.str = prev_token.str = NULL;
5429 /* Initialize parser structure. */
5430 p = intel_parser.op_string = (char *) malloc (strlen (operand_string) + 1);
5433 strcpy (intel_parser.op_string, operand_string);
5434 intel_parser.got_a_float = got_a_float;
5435 intel_parser.op_modifier = -1;
5436 intel_parser.is_mem = 0;
5437 intel_parser.reg = NULL;
5438 intel_parser.disp = (char *) malloc (strlen (operand_string) + 1);
5439 if (intel_parser.disp == NULL)
5441 intel_parser.disp[0] = '\0';
5443 /* Read the first token and start the parser. */
5445 ret = intel_expr ();
5449 /* If we found a memory reference, hand it over to i386_displacement
5450 to fill in the rest of the operand fields. */
5451 if (intel_parser.is_mem)
5453 if ((i.mem_operands == 1
5454 && (current_templates->start->opcode_modifier & IsString) == 0)
5455 || i.mem_operands == 2)
5457 as_bad (_("too many memory references for '%s'"),
5458 current_templates->start->name);
5463 char *s = intel_parser.disp;
5466 /* Add the displacement expression. */
5468 ret = i386_displacement (s, s + strlen (s))
5469 && i386_index_check (s);
5473 /* Constant and OFFSET expressions are handled by i386_immediate. */
5474 else if (intel_parser.op_modifier == OFFSET_FLAT
5475 || intel_parser.reg == NULL)
5476 ret = i386_immediate (intel_parser.disp);
5480 free (intel_parser.disp);
5490 /* expr SHORT e05 */
5491 if (cur_token.code == T_SHORT)
5493 intel_parser.op_modifier = SHORT;
5494 intel_match_token (T_SHORT);
5496 return (intel_e05 ());
5501 return intel_e05 ();
5511 return (intel_e06 () && intel_e05_1 ());
5517 /* e05' addOp e06 e05' */
5518 if (cur_token.code == '+' || cur_token.code == '-')
5520 strcat (intel_parser.disp, cur_token.str);
5521 intel_match_token (cur_token.code);
5523 return (intel_e06 () && intel_e05_1 ());
5538 return (intel_e09 () && intel_e06_1 ());
5544 /* e06' mulOp e09 e06' */
5545 if (cur_token.code == '*' || cur_token.code == '/')
5547 strcat (intel_parser.disp, cur_token.str);
5548 intel_match_token (cur_token.code);
5550 return (intel_e09 () && intel_e06_1 ());
5558 /* e09 OFFSET e10 e09'
5567 /* e09 OFFSET e10 e09' */
5568 if (cur_token.code == T_OFFSET)
5570 intel_parser.is_mem = 0;
5571 intel_parser.op_modifier = OFFSET_FLAT;
5572 intel_match_token (T_OFFSET);
5574 return (intel_e10 () && intel_e09_1 ());
5579 return (intel_e10 () && intel_e09_1 ());
5585 /* e09' PTR e10 e09' */
5586 if (cur_token.code == T_PTR)
5588 if (prev_token.code == T_BYTE)
5589 i.suffix = BYTE_MNEM_SUFFIX;
5591 else if (prev_token.code == T_WORD)
5593 if (intel_parser.got_a_float == 2) /* "fi..." */
5594 i.suffix = SHORT_MNEM_SUFFIX;
5596 i.suffix = WORD_MNEM_SUFFIX;
5599 else if (prev_token.code == T_DWORD)
5601 if (intel_parser.got_a_float == 1) /* "f..." */
5602 i.suffix = SHORT_MNEM_SUFFIX;
5604 i.suffix = LONG_MNEM_SUFFIX;
5607 else if (prev_token.code == T_QWORD)
5609 if (intel_parser.got_a_float == 1) /* "f..." */
5610 i.suffix = LONG_MNEM_SUFFIX;
5612 i.suffix = QWORD_MNEM_SUFFIX;
5615 else if (prev_token.code == T_XWORD)
5616 i.suffix = LONG_DOUBLE_MNEM_SUFFIX;
5620 as_bad (_("Unknown operand modifier `%s'\n"), prev_token.str);
5624 intel_match_token (T_PTR);
5626 return (intel_e10 () && intel_e09_1 ());
5629 /* e09 : e10 e09' */
5630 else if (cur_token.code == ':')
5632 /* Mark as a memory operand only if it's not already known to be an
5633 offset expression. */
5634 if (intel_parser.op_modifier != OFFSET_FLAT)
5635 intel_parser.is_mem = 1;
5637 return (intel_match_token (':') && intel_e10 () && intel_e09_1 ());
5652 return (intel_e11 () && intel_e10_1 ());
5658 /* e10' [ expr ] e10' */
5659 if (cur_token.code == '[')
5661 intel_match_token ('[');
5663 /* Mark as a memory operand only if it's not already known to be an
5664 offset expression. If it's an offset expression, we need to keep
5666 if (intel_parser.op_modifier != OFFSET_FLAT)
5667 intel_parser.is_mem = 1;
5669 strcat (intel_parser.disp, "[");
5671 /* Add a '+' to the displacement string if necessary. */
5672 if (*intel_parser.disp != '\0'
5673 && *(intel_parser.disp + strlen (intel_parser.disp) - 1) != '+')
5674 strcat (intel_parser.disp, "+");
5676 if (intel_expr () && intel_match_token (']'))
5678 /* Preserve brackets when the operand is an offset expression. */
5679 if (intel_parser.op_modifier == OFFSET_FLAT)
5680 strcat (intel_parser.disp, "]");
5682 return intel_e10_1 ();
5709 if (cur_token.code == '(')
5711 intel_match_token ('(');
5712 strcat (intel_parser.disp, "(");
5714 if (intel_expr () && intel_match_token (')'))
5716 strcat (intel_parser.disp, ")");
5724 else if (cur_token.code == '[')
5726 intel_match_token ('[');
5728 /* Mark as a memory operand only if it's not already known to be an
5729 offset expression. If it's an offset expression, we need to keep
5731 if (intel_parser.op_modifier != OFFSET_FLAT)
5732 intel_parser.is_mem = 1;
5734 strcat (intel_parser.disp, "[");
5736 /* Operands for jump/call inside brackets denote absolute addresses. */
5737 if (current_templates->start->opcode_modifier & Jump
5738 || current_templates->start->opcode_modifier & JumpDword
5739 || current_templates->start->opcode_modifier & JumpByte
5740 || current_templates->start->opcode_modifier & JumpInterSegment)
5741 i.types[this_operand] |= JumpAbsolute;
5743 /* Add a '+' to the displacement string if necessary. */
5744 if (*intel_parser.disp != '\0'
5745 && *(intel_parser.disp + strlen (intel_parser.disp) - 1) != '+')
5746 strcat (intel_parser.disp, "+");
5748 if (intel_expr () && intel_match_token (']'))
5750 /* Preserve brackets when the operand is an offset expression. */
5751 if (intel_parser.op_modifier == OFFSET_FLAT)
5752 strcat (intel_parser.disp, "]");
5765 else if (cur_token.code == T_BYTE
5766 || cur_token.code == T_WORD
5767 || cur_token.code == T_DWORD
5768 || cur_token.code == T_QWORD
5769 || cur_token.code == T_XWORD)
5771 intel_match_token (cur_token.code);
5778 else if (cur_token.code == '$' || cur_token.code == '.')
5780 strcat (intel_parser.disp, cur_token.str);
5781 intel_match_token (cur_token.code);
5783 /* Mark as a memory operand only if it's not already known to be an
5784 offset expression. */
5785 if (intel_parser.op_modifier != OFFSET_FLAT)
5786 intel_parser.is_mem = 1;
5792 else if (cur_token.code == T_REG)
5794 const reg_entry *reg = intel_parser.reg = cur_token.reg;
5796 intel_match_token (T_REG);
5798 /* Check for segment change. */
5799 if (cur_token.code == ':')
5801 if (reg->reg_type & (SReg2 | SReg3))
5803 switch (reg->reg_num)
5806 i.seg[i.mem_operands] = &es;
5809 i.seg[i.mem_operands] = &cs;
5812 i.seg[i.mem_operands] = &ss;
5815 i.seg[i.mem_operands] = &ds;
5818 i.seg[i.mem_operands] = &fs;
5821 i.seg[i.mem_operands] = &gs;
5827 as_bad (_("`%s' is not a valid segment register"), reg->reg_name);
5832 /* Not a segment register. Check for register scaling. */
5833 else if (cur_token.code == '*')
5835 if (!intel_parser.is_mem)
5837 as_bad (_("Register scaling only allowed in memory operands."));
5841 /* What follows must be a valid scale. */
5842 if (intel_match_token ('*')
5843 && strchr ("01248", *cur_token.str))
5846 i.types[this_operand] |= BaseIndex;
5848 /* Set the scale after setting the register (otherwise,
5849 i386_scale will complain) */
5850 i386_scale (cur_token.str);
5851 intel_match_token (T_CONST);
5855 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
5861 /* No scaling. If this is a memory operand, the register is either a
5862 base register (first occurrence) or an index register (second
5864 else if (intel_parser.is_mem && !(reg->reg_type & (SReg2 | SReg3)))
5866 if (i.base_reg && i.index_reg)
5868 as_bad (_("Too many register references in memory operand.\n"));
5872 if (i.base_reg == NULL)
5877 i.types[this_operand] |= BaseIndex;
5880 /* Offset modifier. Add the register to the displacement string to be
5881 parsed as an immediate expression after we're done. */
5882 else if (intel_parser.op_modifier == OFFSET_FLAT)
5883 strcat (intel_parser.disp, reg->reg_name);
5885 /* It's neither base nor index nor offset. */
5888 i.types[this_operand] |= reg->reg_type & ~BaseIndex;
5889 i.op[this_operand].regs = reg;
5893 /* Since registers are not part of the displacement string (except
5894 when we're parsing offset operands), we may need to remove any
5895 preceding '+' from the displacement string. */
5896 if (*intel_parser.disp != '\0'
5897 && intel_parser.op_modifier != OFFSET_FLAT)
5899 char *s = intel_parser.disp;
5900 s += strlen (s) - 1;
5909 else if (cur_token.code == T_ID)
5911 /* Add the identifier to the displacement string. */
5912 strcat (intel_parser.disp, cur_token.str);
5913 intel_match_token (T_ID);
5915 /* The identifier represents a memory reference only if it's not
5916 preceded by an offset modifier. */
5917 if (intel_parser.op_modifier != OFFSET_FLAT)
5918 intel_parser.is_mem = 1;
5924 else if (cur_token.code == T_CONST
5925 || cur_token.code == '-'
5926 || cur_token.code == '+')
5930 /* Allow constants that start with `+' or `-'. */
5931 if (cur_token.code == '-' || cur_token.code == '+')
5933 strcat (intel_parser.disp, cur_token.str);
5934 intel_match_token (cur_token.code);
5935 if (cur_token.code != T_CONST)
5937 as_bad (_("Syntax error. Expecting a constant. Got `%s'.\n"),
5943 save_str = (char *) malloc (strlen (cur_token.str) + 1);
5944 if (save_str == NULL)
5946 strcpy (save_str, cur_token.str);
5948 /* Get the next token to check for register scaling. */
5949 intel_match_token (cur_token.code);
5951 /* Check if this constant is a scaling factor for an index register. */
5952 if (cur_token.code == '*')
5954 if (intel_match_token ('*') && cur_token.code == T_REG)
5956 if (!intel_parser.is_mem)
5958 as_bad (_("Register scaling only allowed in memory operands."));
5962 /* The constant is followed by `* reg', so it must be
5964 if (strchr ("01248", *save_str))
5966 i.index_reg = cur_token.reg;
5967 i.types[this_operand] |= BaseIndex;
5969 /* Set the scale after setting the register (otherwise,
5970 i386_scale will complain) */
5971 i386_scale (save_str);
5972 intel_match_token (T_REG);
5974 /* Since registers are not part of the displacement
5975 string, we may need to remove any preceding '+' from
5976 the displacement string. */
5977 if (*intel_parser.disp != '\0')
5979 char *s = intel_parser.disp;
5980 s += strlen (s) - 1;
5993 /* The constant was not used for register scaling. Since we have
5994 already consumed the token following `*' we now need to put it
5995 back in the stream. */
5997 intel_putback_token ();
6000 /* Add the constant to the displacement string. */
6001 strcat (intel_parser.disp, save_str);
6007 as_bad (_("Unrecognized token '%s'"), cur_token.str);
6011 /* Match the given token against cur_token. If they match, read the next
6012 token from the operand string. */
6014 intel_match_token (code)
6017 if (cur_token.code == code)
6024 as_bad (_("Unexpected token `%s'\n"), cur_token.str);
6029 /* Read a new token from intel_parser.op_string and store it in cur_token. */
6034 const reg_entry *reg;
6035 struct intel_token new_token;
6037 new_token.code = T_NIL;
6038 new_token.reg = NULL;
6039 new_token.str = NULL;
6041 /* Free the memory allocated to the previous token and move
6042 cur_token to prev_token. */
6044 free (prev_token.str);
6046 prev_token = cur_token;
6048 /* Skip whitespace. */
6049 while (is_space_char (*intel_parser.op_string))
6050 intel_parser.op_string++;
6052 /* Return an empty token if we find nothing else on the line. */
6053 if (*intel_parser.op_string == '\0')
6055 cur_token = new_token;
6059 /* The new token cannot be larger than the remainder of the operand
6061 new_token.str = (char *) malloc (strlen (intel_parser.op_string) + 1);
6062 if (new_token.str == NULL)
6064 new_token.str[0] = '\0';
6066 if (strchr ("0123456789", *intel_parser.op_string))
6068 char *p = new_token.str;
6069 char *q = intel_parser.op_string;
6070 new_token.code = T_CONST;
6072 /* Allow any kind of identifier char to encompass floating point and
6073 hexadecimal numbers. */
6074 while (is_identifier_char (*q))
6078 /* Recognize special symbol names [0-9][bf]. */
6079 if (strlen (intel_parser.op_string) == 2
6080 && (intel_parser.op_string[1] == 'b'
6081 || intel_parser.op_string[1] == 'f'))
6082 new_token.code = T_ID;
6085 else if (strchr ("+-/*:[]()", *intel_parser.op_string))
6087 new_token.code = *intel_parser.op_string;
6088 new_token.str[0] = *intel_parser.op_string;
6089 new_token.str[1] = '\0';
6092 else if ((*intel_parser.op_string == REGISTER_PREFIX || allow_naked_reg)
6093 && ((reg = parse_register (intel_parser.op_string, &end_op)) != NULL))
6095 new_token.code = T_REG;
6096 new_token.reg = reg;
6098 if (*intel_parser.op_string == REGISTER_PREFIX)
6100 new_token.str[0] = REGISTER_PREFIX;
6101 new_token.str[1] = '\0';
6104 strcat (new_token.str, reg->reg_name);
6107 else if (is_identifier_char (*intel_parser.op_string))
6109 char *p = new_token.str;
6110 char *q = intel_parser.op_string;
6112 /* A '.' or '$' followed by an identifier char is an identifier.
6113 Otherwise, it's operator '.' followed by an expression. */
6114 if ((*q == '.' || *q == '$') && !is_identifier_char (*(q + 1)))
6116 new_token.code = *q;
6117 new_token.str[0] = *q;
6118 new_token.str[1] = '\0';
6122 while (is_identifier_char (*q) || *q == '@')
6126 if (strcasecmp (new_token.str, "BYTE") == 0)
6127 new_token.code = T_BYTE;
6129 else if (strcasecmp (new_token.str, "WORD") == 0)
6130 new_token.code = T_WORD;
6132 else if (strcasecmp (new_token.str, "DWORD") == 0)
6133 new_token.code = T_DWORD;
6135 else if (strcasecmp (new_token.str, "QWORD") == 0)
6136 new_token.code = T_QWORD;
6138 else if (strcasecmp (new_token.str, "XWORD") == 0)
6139 new_token.code = T_XWORD;
6141 else if (strcasecmp (new_token.str, "PTR") == 0)
6142 new_token.code = T_PTR;
6144 else if (strcasecmp (new_token.str, "SHORT") == 0)
6145 new_token.code = T_SHORT;
6147 else if (strcasecmp (new_token.str, "OFFSET") == 0)
6149 new_token.code = T_OFFSET;
6151 /* ??? This is not mentioned in the MASM grammar but gcc
6152 makes use of it with -mintel-syntax. OFFSET may be
6153 followed by FLAT: */
6154 if (strncasecmp (q, " FLAT:", 6) == 0)
6155 strcat (new_token.str, " FLAT:");
6158 /* ??? This is not mentioned in the MASM grammar. */
6159 else if (strcasecmp (new_token.str, "FLAT") == 0)
6160 new_token.code = T_OFFSET;
6163 new_token.code = T_ID;
6168 as_bad (_("Unrecognized token `%s'\n"), intel_parser.op_string);
6170 intel_parser.op_string += strlen (new_token.str);
6171 cur_token = new_token;
6174 /* Put cur_token back into the token stream and make cur_token point to
6177 intel_putback_token ()
6179 intel_parser.op_string -= strlen (cur_token.str);
6180 free (cur_token.str);
6181 cur_token = prev_token;
6183 /* Forget prev_token. */
6184 prev_token.code = T_NIL;
6185 prev_token.reg = NULL;
6186 prev_token.str = NULL;