1 /* i386.c -- Assemble code for the Intel 80386
2 Copyright 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
5
6 This file is part of GAS, the GNU Assembler.
7
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)
11 any later version.
12
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.
17
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, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 /* Intel 80386 machine specific gas.
24 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
25 x86_64 support by Jan Hubicka (jh@suse.cz)
26 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
27 Bugs & suggestions are completely welcome. This is free software.
28 Please help us make it better. */
29
30 #include "as.h"
31 #include "safe-ctype.h"
32 #include "subsegs.h"
33 #include "dwarf2dbg.h"
34 #include "dw2gencfi.h"
35 #include "opcode/i386.h"
36 #include "elf/x86-64.h"
37
38 #ifndef REGISTER_WARNINGS
39 #define REGISTER_WARNINGS 1
40 #endif
41
42 #ifndef INFER_ADDR_PREFIX
43 #define INFER_ADDR_PREFIX 1
44 #endif
45
46 #ifndef SCALE1_WHEN_NO_INDEX
47 /* Specifying a scale factor besides 1 when there is no index is
48 futile. eg. `mov (%ebx,2),%al' does exactly the same as
49 `mov (%ebx),%al'. To slavishly follow what the programmer
50 specified, set SCALE1_WHEN_NO_INDEX to 0. */
51 #define SCALE1_WHEN_NO_INDEX 1
52 #endif
53
54 #ifndef DEFAULT_ARCH
55 #define DEFAULT_ARCH "i386"
56 #endif
57
58 #ifndef INLINE
59 #if __GNUC__ >= 2
60 #define INLINE __inline__
61 #else
62 #define INLINE
63 #endif
64 #endif
65
66 static INLINE unsigned int mode_from_disp_size PARAMS ((unsigned int));
67 static INLINE int fits_in_signed_byte PARAMS ((offsetT));
68 static INLINE int fits_in_unsigned_byte PARAMS ((offsetT));
69 static INLINE int fits_in_unsigned_word PARAMS ((offsetT));
70 static INLINE int fits_in_signed_word PARAMS ((offsetT));
71 static INLINE int fits_in_unsigned_long PARAMS ((offsetT));
72 static INLINE int fits_in_signed_long PARAMS ((offsetT));
73 static int smallest_imm_type PARAMS ((offsetT));
74 static offsetT offset_in_range PARAMS ((offsetT, int));
75 static int add_prefix PARAMS ((unsigned int));
76 static void set_code_flag PARAMS ((int));
77 static void set_16bit_gcc_code_flag PARAMS ((int));
78 static void set_intel_syntax PARAMS ((int));
79 static void set_cpu_arch PARAMS ((int));
80 #ifdef TE_PE
81 static void pe_directive_secrel PARAMS ((int));
82 #endif
83 static void signed_cons PARAMS ((int));
84 static char *output_invalid PARAMS ((int c));
85 static int i386_operand PARAMS ((char *operand_string));
86 static int i386_intel_operand PARAMS ((char *operand_string, int got_a_float));
87 static const reg_entry *parse_register PARAMS ((char *reg_string,
88 char **end_op));
89 static char *parse_insn PARAMS ((char *, char *));
90 static char *parse_operands PARAMS ((char *, const char *));
91 static void swap_operands PARAMS ((void));
92 static void optimize_imm PARAMS ((void));
93 static void optimize_disp PARAMS ((void));
94 static int match_template PARAMS ((void));
95 static int check_string PARAMS ((void));
96 static int process_suffix PARAMS ((void));
97 static int check_byte_reg PARAMS ((void));
98 static int check_long_reg PARAMS ((void));
99 static int check_qword_reg PARAMS ((void));
100 static int check_word_reg PARAMS ((void));
101 static int finalize_imm PARAMS ((void));
102 static int process_operands PARAMS ((void));
103 static const seg_entry *build_modrm_byte PARAMS ((void));
104 static void output_insn PARAMS ((void));
105 static void output_branch PARAMS ((void));
106 static void output_jump PARAMS ((void));
107 static void output_interseg_jump PARAMS ((void));
108 static void output_imm PARAMS ((fragS *insn_start_frag,
109 offsetT insn_start_off));
110 static void output_disp PARAMS ((fragS *insn_start_frag,
111 offsetT insn_start_off));
112 #ifndef I386COFF
113 static void s_bss PARAMS ((int));
114 #endif
115 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
116 static void handle_large_common (int small ATTRIBUTE_UNUSED);
117 #endif
118
119 static const char *default_arch = DEFAULT_ARCH;
120
121 /* 'md_assemble ()' gathers together information and puts it into a
122 i386_insn. */
123
124 union i386_op
125 {
126 expressionS *disps;
127 expressionS *imms;
128 const reg_entry *regs;
129 };
130
131 struct _i386_insn
132 {
133 /* TM holds the template for the insn were currently assembling. */
134 template tm;
135
136 /* SUFFIX holds the instruction mnemonic suffix if given.
137 (e.g. 'l' for 'movl') */
138 char suffix;
139
140 /* OPERANDS gives the number of given operands. */
141 unsigned int operands;
142
143 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
144 of given register, displacement, memory operands and immediate
145 operands. */
146 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
147
148 /* TYPES [i] is the type (see above #defines) which tells us how to
149 use OP[i] for the corresponding operand. */
150 unsigned int types[MAX_OPERANDS];
151
152 /* Displacement expression, immediate expression, or register for each
153 operand. */
154 union i386_op op[MAX_OPERANDS];
155
156 /* Flags for operands. */
157 unsigned int flags[MAX_OPERANDS];
158 #define Operand_PCrel 1
159
160 /* Relocation type for operand */
161 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
162
163 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
164 the base index byte below. */
165 const reg_entry *base_reg;
166 const reg_entry *index_reg;
167 unsigned int log2_scale_factor;
168
169 /* SEG gives the seg_entries of this insn. They are zero unless
170 explicit segment overrides are given. */
171 const seg_entry *seg[2];
172
173 /* PREFIX holds all the given prefix opcodes (usually null).
174 PREFIXES is the number of prefix opcodes. */
175 unsigned int prefixes;
176 unsigned char prefix[MAX_PREFIXES];
177
178 /* RM and SIB are the modrm byte and the sib byte where the
179 addressing modes of this insn are encoded. */
180
181 modrm_byte rm;
182 rex_byte rex;
183 sib_byte sib;
184 };
185
186 typedef struct _i386_insn i386_insn;
187
188 /* List of chars besides those in app.c:symbol_chars that can start an
189 operand. Used to prevent the scrubber eating vital white-space. */
190 const char extra_symbol_chars[] = "*%-(["
191 #ifdef LEX_AT
192 "@"
193 #endif
194 #ifdef LEX_QM
195 "?"
196 #endif
197 ;
198
199 #if (defined (TE_I386AIX) \
200 || ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
201 && !defined (TE_GNU) \
202 && !defined (TE_LINUX) \
203 && !defined (TE_NETWARE) \
204 && !defined (TE_FreeBSD) \
205 && !defined (TE_NetBSD) \
206 && !defined (TE_OpenBSD)))
207 /* This array holds the chars that always start a comment. If the
208 pre-processor is disabled, these aren't very useful. The option
209 --divide will remove '/' from this list. */
210 const char *i386_comment_chars = "#/";
211 #define SVR4_COMMENT_CHARS 1
212 #define PREFIX_SEPARATOR '\\'
213
214 #else
215 const char *i386_comment_chars = "#";
216 #define PREFIX_SEPARATOR '/'
217 #endif
218
219 /* This array holds the chars that only start a comment at the beginning of
220 a line. If the line seems to have the form '# 123 filename'
221 .line and .file directives will appear in the pre-processed output.
222 Note that input_file.c hand checks for '#' at the beginning of the
223 first line of the input file. This is because the compiler outputs
224 #NO_APP at the beginning of its output.
225 Also note that comments started like this one will always work if
226 '/' isn't otherwise defined. */
227 const char line_comment_chars[] = "#/";
228
229 const char line_separator_chars[] = ";";
230
231 /* Chars that can be used to separate mant from exp in floating point
232 nums. */
233 const char EXP_CHARS[] = "eE";
234
235 /* Chars that mean this number is a floating point constant
236 As in 0f12.456
237 or 0d1.2345e12. */
238 const char FLT_CHARS[] = "fFdDxX";
239
240 /* Tables for lexical analysis. */
241 static char mnemonic_chars[256];
242 static char register_chars[256];
243 static char operand_chars[256];
244 static char identifier_chars[256];
245 static char digit_chars[256];
246
247 /* Lexical macros. */
248 #define is_mnemonic_char(x) (mnemonic_chars[(unsigned char) x])
249 #define is_operand_char(x) (operand_chars[(unsigned char) x])
250 #define is_register_char(x) (register_chars[(unsigned char) x])
251 #define is_space_char(x) ((x) == ' ')
252 #define is_identifier_char(x) (identifier_chars[(unsigned char) x])
253 #define is_digit_char(x) (digit_chars[(unsigned char) x])
254
255 /* All non-digit non-letter characters that may occur in an operand. */
256 static char operand_special_chars[] = "%$-+(,)*._~/<>|&^!:[@]";
257
258 /* md_assemble() always leaves the strings it's passed unaltered. To
259 effect this we maintain a stack of saved characters that we've smashed
260 with '\0's (indicating end of strings for various sub-fields of the
261 assembler instruction). */
262 static char save_stack[32];
263 static char *save_stack_p;
264 #define END_STRING_AND_SAVE(s) \
265 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
266 #define RESTORE_END_STRING(s) \
267 do { *(s) = *--save_stack_p; } while (0)
268
269 /* The instruction we're assembling. */
270 static i386_insn i;
271
272 /* Possible templates for current insn. */
273 static const templates *current_templates;
274
275 /* Per instruction expressionS buffers: 2 displacements & 2 immediate max. */
276 static expressionS disp_expressions[2], im_expressions[2];
277
278 /* Current operand we are working on. */
279 static int this_operand;
280
281 /* We support four different modes. FLAG_CODE variable is used to distinguish
282 these. */
283
284 enum flag_code {
285 CODE_32BIT,
286 CODE_16BIT,
287 CODE_64BIT };
288 #define NUM_FLAG_CODE ((int) CODE_64BIT + 1)
289
290 static enum flag_code flag_code;
291 static unsigned int object_64bit;
292 static int use_rela_relocations = 0;
293
294 /* The names used to print error messages. */
295 static const char *flag_code_names[] =
296 {
297 "32",
298 "16",
299 "64"
300 };
301
302 /* 1 for intel syntax,
303 0 if att syntax. */
304 static int intel_syntax = 0;
305
306 /* 1 if register prefix % not required. */
307 static int allow_naked_reg = 0;
308
309 /* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
310 leave, push, and pop instructions so that gcc has the same stack
311 frame as in 32 bit mode. */
312 static char stackop_size = '\0';
313
314 /* Non-zero to optimize code alignment. */
315 int optimize_align_code = 1;
316
317 /* Non-zero to quieten some warnings. */
318 static int quiet_warnings = 0;
319
320 /* CPU name. */
321 static const char *cpu_arch_name = NULL;
322 static const char *cpu_sub_arch_name = NULL;
323
324 /* CPU feature flags. */
325 static unsigned int cpu_arch_flags = CpuUnknownFlags | CpuNo64;
326
327 /* If set, conditional jumps are not automatically promoted to handle
328 larger than a byte offset. */
329 static unsigned int no_cond_jump_promotion = 0;
330
331 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
332 static symbolS *GOT_symbol;
333
334 /* The dwarf2 return column, adjusted for 32 or 64 bit. */
335 unsigned int x86_dwarf2_return_column;
336
337 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
338 int x86_cie_data_alignment;
339
340 /* Interface to relax_segment.
341 There are 3 major relax states for 386 jump insns because the
342 different types of jumps add different sizes to frags when we're
343 figuring out what sort of jump to choose to reach a given label. */
344
345 /* Types. */
346 #define UNCOND_JUMP 0
347 #define COND_JUMP 1
348 #define COND_JUMP86 2
349
350 /* Sizes. */
351 #define CODE16 1
352 #define SMALL 0
353 #define SMALL16 (SMALL | CODE16)
354 #define BIG 2
355 #define BIG16 (BIG | CODE16)
356
357 #ifndef INLINE
358 #ifdef __GNUC__
359 #define INLINE __inline__
360 #else
361 #define INLINE
362 #endif
363 #endif
364
365 #define ENCODE_RELAX_STATE(type, size) \
366 ((relax_substateT) (((type) << 2) | (size)))
367 #define TYPE_FROM_RELAX_STATE(s) \
368 ((s) >> 2)
369 #define DISP_SIZE_FROM_RELAX_STATE(s) \
370 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
371
372 /* This table is used by relax_frag to promote short jumps to long
373 ones where necessary. SMALL (short) jumps may be promoted to BIG
374 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
375 don't allow a short jump in a 32 bit code segment to be promoted to
376 a 16 bit offset jump because it's slower (requires data size
377 prefix), and doesn't work, unless the destination is in the bottom
378 64k of the code segment (The top 16 bits of eip are zeroed). */
379
380 const relax_typeS md_relax_table[] =
381 {
382 /* The fields are:
383 1) most positive reach of this state,
384 2) most negative reach of this state,
385 3) how many bytes this mode will have in the variable part of the frag
386 4) which index into the table to try if we can't fit into this one. */
387
388 /* UNCOND_JUMP states. */
389 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
390 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
391 /* dword jmp adds 4 bytes to frag:
392 0 extra opcode bytes, 4 displacement bytes. */
393 {0, 0, 4, 0},
394 /* word jmp adds 2 byte2 to frag:
395 0 extra opcode bytes, 2 displacement bytes. */
396 {0, 0, 2, 0},
397
398 /* COND_JUMP states. */
399 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
400 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
401 /* dword conditionals adds 5 bytes to frag:
402 1 extra opcode byte, 4 displacement bytes. */
403 {0, 0, 5, 0},
404 /* word conditionals add 3 bytes to frag:
405 1 extra opcode byte, 2 displacement bytes. */
406 {0, 0, 3, 0},
407
408 /* COND_JUMP86 states. */
409 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
410 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
411 /* dword conditionals adds 5 bytes to frag:
412 1 extra opcode byte, 4 displacement bytes. */
413 {0, 0, 5, 0},
414 /* word conditionals add 4 bytes to frag:
415 1 displacement byte and a 3 byte long branch insn. */
416 {0, 0, 4, 0}
417 };
418
419 static const arch_entry cpu_arch[] = {
420 {"i8086", Cpu086 },
421 {"i186", Cpu086|Cpu186 },
422 {"i286", Cpu086|Cpu186|Cpu286 },
423 {"i386", Cpu086|Cpu186|Cpu286|Cpu386 },
424 {"i486", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486 },
425 {"i586", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586 },
426 {"i686", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686 },
427 {"pentium", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586 },
428 {"pentiumpro",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686 },
429 {"pentiumii", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX },
430 {"pentiumiii",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuMMX|CpuMMX2|CpuSSE },
431 {"pentium4", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2 },
432 {"prescott", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuP4|CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuPNI },
433 {"k6", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX },
434 {"k6_2", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|CpuK6|CpuMMX|Cpu3dnow },
435 {"athlon", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA },
436 {"sledgehammer",Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuSledgehammer|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2 },
437 {"opteron", Cpu086|Cpu186|Cpu286|Cpu386|Cpu486|Cpu586|Cpu686|CpuK6|CpuAthlon|CpuSledgehammer|CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA|CpuSSE|CpuSSE2 },
438 {".mmx", CpuMMX },
439 {".sse", CpuMMX|CpuMMX2|CpuSSE },
440 {".sse2", CpuMMX|CpuMMX2|CpuSSE|CpuSSE2 },
441 {".sse3", CpuMMX|CpuMMX2|CpuSSE|CpuSSE2|CpuSSE3 },
442 {".3dnow", CpuMMX|Cpu3dnow },
443 {".3dnowa", CpuMMX|CpuMMX2|Cpu3dnow|Cpu3dnowA },
444 {".padlock", CpuPadLock },
445 {".pacifica", CpuSVME },
446 {".svme", CpuSVME },
447 {NULL, 0 }
448 };
449
450 const pseudo_typeS md_pseudo_table[] =
451 {
452 #if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
453 {"align", s_align_bytes, 0},
454 #else
455 {"align", s_align_ptwo, 0},
456 #endif
457 {"arch", set_cpu_arch, 0},
458 #ifndef I386COFF
459 {"bss", s_bss, 0},
460 #endif
461 {"ffloat", float_cons, 'f'},
462 {"dfloat", float_cons, 'd'},
463 {"tfloat", float_cons, 'x'},
464 {"value", cons, 2},
465 {"slong", signed_cons, 4},
466 {"noopt", s_ignore, 0},
467 {"optim", s_ignore, 0},
468 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
469 {"code16", set_code_flag, CODE_16BIT},
470 {"code32", set_code_flag, CODE_32BIT},
471 {"code64", set_code_flag, CODE_64BIT},
472 {"intel_syntax", set_intel_syntax, 1},
473 {"att_syntax", set_intel_syntax, 0},
474 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
475 {"largecomm", handle_large_common, 0},
476 #else
477 {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
478 {"loc", dwarf2_directive_loc, 0},
479 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
480 #endif
481 #ifdef TE_PE
482 {"secrel32", pe_directive_secrel, 0},
483 #endif
484 {0, 0, 0}
485 };
486
487 /* For interface with expression (). */
488 extern char *input_line_pointer;
489
490 /* Hash table for instruction mnemonic lookup. */
491 static struct hash_control *op_hash;
492
493 /* Hash table for register lookup. */
494 static struct hash_control *reg_hash;
495
496 void
i386_align_code(fragP,count)497 i386_align_code (fragP, count)
498 fragS *fragP;
499 int count;
500 {
501 /* Various efficient no-op patterns for aligning code labels.
502 Note: Don't try to assemble the instructions in the comments.
503 0L and 0w are not legal. */
504 static const char f32_1[] =
505 {0x90}; /* nop */
506 static const char f32_2[] =
507 {0x89,0xf6}; /* movl %esi,%esi */
508 static const char f32_15[] =
509 {0xeb,0x0d,0xCC,0xCC,0xCC,0xCC,0xCC, /* jmp .+15; lotsa int3 */
510 0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC};
511 static const char f16_3[] =
512 {0x8d,0x74,0x00}; /* lea 0(%esi),%esi */
513 static const char f16_4[] =
514 {0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
515 static const char f16_5[] =
516 {0x90, /* nop */
517 0x8d,0xb4,0x00,0x00}; /* lea 0w(%si),%si */
518 static const char f16_6[] =
519 {0x89,0xf6, /* mov %si,%si */
520 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
521 static const char f16_7[] =
522 {0x8d,0x74,0x00, /* lea 0(%si),%si */
523 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
524 static const char f16_8[] =
525 {0x8d,0xb4,0x00,0x00, /* lea 0w(%si),%si */
526 0x8d,0xbd,0x00,0x00}; /* lea 0w(%di),%di */
527 static const char f64_2[] =
528 {0x66,0x90}; /* data16, nop*/
529 static const char *const f32_patt[] = {
530 f32_1, f32_2, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15,
531 f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
532 };
533 static const char *const f16_patt[] = {
534 f32_1, f32_2, f16_3, f16_4, f16_5, f16_6, f16_7, f16_8,
535 f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
536 };
537 static const char *const f64_patt[] = {
538 f32_1, f64_2, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15,
539 f32_15, f32_15, f32_15, f32_15, f32_15, f32_15, f32_15
540 };
541
542 if (count <= 0 || count > 15)
543 return;
544
545 if (flag_code == CODE_64BIT)
546 {
547 memcpy(fragP->fr_literal + fragP->fr_fix,
548 f64_patt[count -1], count);
549 if (count > 2)
550 /* Adjust jump offset */
551 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
552 }
553 else
554 if (flag_code == CODE_16BIT)
555 {
556 memcpy (fragP->fr_literal + fragP->fr_fix,
557 f16_patt[count - 1], count);
558 if (count > 8)
559 /* Adjust jump offset. */
560 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
561 }
562 else
563 {
564 memcpy (fragP->fr_literal + fragP->fr_fix,
565 f32_patt[count - 1], count);
566 if (count > 2)
567 /* Adjust jump offset */
568 fragP->fr_literal[fragP->fr_fix + 1] = count - 2;
569 }
570 fragP->fr_var = count;
571 }
572
573 static INLINE unsigned int
mode_from_disp_size(t)574 mode_from_disp_size (t)
575 unsigned int t;
576 {
577 return (t & Disp8) ? 1 : (t & (Disp16 | Disp32 | Disp32S)) ? 2 : 0;
578 }
579
580 static INLINE int
fits_in_signed_byte(num)581 fits_in_signed_byte (num)
582 offsetT num;
583 {
584 return (num >= -128) && (num <= 127);
585 }
586
587 static INLINE int
fits_in_unsigned_byte(num)588 fits_in_unsigned_byte (num)
589 offsetT num;
590 {
591 return (num & 0xff) == num;
592 }
593
594 static INLINE int
fits_in_unsigned_word(num)595 fits_in_unsigned_word (num)
596 offsetT num;
597 {
598 return (num & 0xffff) == num;
599 }
600
601 static INLINE int
fits_in_signed_word(num)602 fits_in_signed_word (num)
603 offsetT num;
604 {
605 return (-32768 <= num) && (num <= 32767);
606 }
607 static INLINE int
fits_in_signed_long(num)608 fits_in_signed_long (num)
609 offsetT num ATTRIBUTE_UNUSED;
610 {
611 #ifndef BFD64
612 return 1;
613 #else
614 return (!(-((offsetT) 1 << 31) & num)
615 || (-((offsetT) 1 << 31) & num) == -((offsetT) 1 << 31));
616 #endif
617 } /* fits_in_signed_long() */
618 static INLINE int
fits_in_unsigned_long(num)619 fits_in_unsigned_long (num)
620 offsetT num ATTRIBUTE_UNUSED;
621 {
622 #ifndef BFD64
623 return 1;
624 #else
625 return (num & (((offsetT) 2 << 31) - 1)) == num;
626 #endif
627 } /* fits_in_unsigned_long() */
628
629 static int
smallest_imm_type(num)630 smallest_imm_type (num)
631 offsetT num;
632 {
633 if (cpu_arch_flags != (Cpu086 | Cpu186 | Cpu286 | Cpu386 | Cpu486 | CpuNo64))
634 {
635 /* This code is disabled on the 486 because all the Imm1 forms
636 in the opcode table are slower on the i486. They're the
637 versions with the implicitly specified single-position
638 displacement, which has another syntax if you really want to
639 use that form. */
640 if (num == 1)
641 return Imm1 | Imm8 | Imm8S | Imm16 | Imm32 | Imm32S | Imm64;
642 }
643 return (fits_in_signed_byte (num)
644 ? (Imm8S | Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
645 : fits_in_unsigned_byte (num)
646 ? (Imm8 | Imm16 | Imm32 | Imm32S | Imm64)
647 : (fits_in_signed_word (num) || fits_in_unsigned_word (num))
648 ? (Imm16 | Imm32 | Imm32S | Imm64)
649 : fits_in_signed_long (num)
650 ? (Imm32 | Imm32S | Imm64)
651 : fits_in_unsigned_long (num)
652 ? (Imm32 | Imm64)
653 : Imm64);
654 }
655
656 static offsetT
offset_in_range(val,size)657 offset_in_range (val, size)
658 offsetT val;
659 int size;
660 {
661 addressT mask;
662
663 switch (size)
664 {
665 case 1: mask = ((addressT) 1 << 8) - 1; break;
666 case 2: mask = ((addressT) 1 << 16) - 1; break;
667 case 4: mask = ((addressT) 2 << 31) - 1; break;
668 #ifdef BFD64
669 case 8: mask = ((addressT) 2 << 63) - 1; break;
670 #endif
671 default: abort ();
672 }
673
674 /* If BFD64, sign extend val. */
675 if (!use_rela_relocations)
676 if ((val & ~(((addressT) 2 << 31) - 1)) == 0)
677 val = (val ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
678
679 if ((val & ~mask) != 0 && (val & ~mask) != ~mask)
680 {
681 char buf1[40], buf2[40];
682
683 sprint_value (buf1, val);
684 sprint_value (buf2, val & mask);
685 as_warn (_("%s shortened to %s"), buf1, buf2);
686 }
687 return val & mask;
688 }
689
690 /* Returns 0 if attempting to add a prefix where one from the same
691 class already exists, 1 if non rep/repne added, 2 if rep/repne
692 added. */
693 static int
add_prefix(prefix)694 add_prefix (prefix)
695 unsigned int prefix;
696 {
697 int ret = 1;
698 unsigned int q;
699
700 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
701 && flag_code == CODE_64BIT)
702 {
703 if ((i.prefix[REX_PREFIX] & prefix & REX_MODE64)
704 || ((i.prefix[REX_PREFIX] & (REX_EXTX | REX_EXTY | REX_EXTZ))
705 && (prefix & (REX_EXTX | REX_EXTY | REX_EXTZ))))
706 ret = 0;
707 q = REX_PREFIX;
708 }
709 else
710 {
711 switch (prefix)
712 {
713 default:
714 abort ();
715
716 case CS_PREFIX_OPCODE:
717 case DS_PREFIX_OPCODE:
718 case ES_PREFIX_OPCODE:
719 case FS_PREFIX_OPCODE:
720 case GS_PREFIX_OPCODE:
721 case SS_PREFIX_OPCODE:
722 q = SEG_PREFIX;
723 break;
724
725 case REPNE_PREFIX_OPCODE:
726 case REPE_PREFIX_OPCODE:
727 ret = 2;
728 /* fall thru */
729 case LOCK_PREFIX_OPCODE:
730 q = LOCKREP_PREFIX;
731 break;
732
733 case FWAIT_OPCODE:
734 q = WAIT_PREFIX;
735 break;
736
737 case ADDR_PREFIX_OPCODE:
738 q = ADDR_PREFIX;
739 break;
740
741 case DATA_PREFIX_OPCODE:
742 q = DATA_PREFIX;
743 break;
744 }
745 if (i.prefix[q] != 0)
746 ret = 0;
747 }
748
749 if (ret)
750 {
751 if (!i.prefix[q])
752 ++i.prefixes;
753 i.prefix[q] |= prefix;
754 }
755 else
756 as_bad (_("same type of prefix used twice"));
757
758 return ret;
759 }
760
761 static void
set_code_flag(value)762 set_code_flag (value)
763 int value;
764 {
765 flag_code = value;
766 cpu_arch_flags &= ~(Cpu64 | CpuNo64);
767 cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
768 if (value == CODE_64BIT && !(cpu_arch_flags & CpuSledgehammer))
769 {
770 as_bad (_("64bit mode not supported on this CPU."));
771 }
772 if (value == CODE_32BIT && !(cpu_arch_flags & Cpu386))
773 {
774 as_bad (_("32bit mode not supported on this CPU."));
775 }
776 stackop_size = '\0';
777 }
778
779 static void
set_16bit_gcc_code_flag(new_code_flag)780 set_16bit_gcc_code_flag (new_code_flag)
781 int new_code_flag;
782 {
783 flag_code = new_code_flag;
784 cpu_arch_flags &= ~(Cpu64 | CpuNo64);
785 cpu_arch_flags |= (flag_code == CODE_64BIT ? Cpu64 : CpuNo64);
786 stackop_size = LONG_MNEM_SUFFIX;
787 }
788
789 static void
set_intel_syntax(syntax_flag)790 set_intel_syntax (syntax_flag)
791 int syntax_flag;
792 {
793 /* Find out if register prefixing is specified. */
794 int ask_naked_reg = 0;
795
796 SKIP_WHITESPACE ();
797 if (!is_end_of_line[(unsigned char) *input_line_pointer])
798 {
799 char *string = input_line_pointer;
800 int e = get_symbol_end ();
801
802 if (strcmp (string, "prefix") == 0)
803 ask_naked_reg = 1;
804 else if (strcmp (string, "noprefix") == 0)
805 ask_naked_reg = -1;
806 else
807 as_bad (_("bad argument to syntax directive."));
808 *input_line_pointer = e;
809 }
810 demand_empty_rest_of_line ();
811
812 intel_syntax = syntax_flag;
813
814 if (ask_naked_reg == 0)
815 allow_naked_reg = (intel_syntax
816 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
817 else
818 allow_naked_reg = (ask_naked_reg < 0);
819
820 identifier_chars['%'] = intel_syntax && allow_naked_reg ? '%' : 0;
821 identifier_chars['$'] = intel_syntax ? '$' : 0;
822 }
823
824 static void
set_cpu_arch(dummy)825 set_cpu_arch (dummy)
826 int dummy ATTRIBUTE_UNUSED;
827 {
828 SKIP_WHITESPACE ();
829
830 if (!is_end_of_line[(unsigned char) *input_line_pointer])
831 {
832 char *string = input_line_pointer;
833 int e = get_symbol_end ();
834 int i;
835
836 for (i = 0; cpu_arch[i].name; i++)
837 {
838 if (strcmp (string, cpu_arch[i].name) == 0)
839 {
840 if (*string != '.')
841 {
842 cpu_arch_name = cpu_arch[i].name;
843 cpu_sub_arch_name = NULL;
844 cpu_arch_flags = (cpu_arch[i].flags
845 | (flag_code == CODE_64BIT ? Cpu64 : CpuNo64));
846 break;
847 }
848 if ((cpu_arch_flags | cpu_arch[i].flags) != cpu_arch_flags)
849 {
850 cpu_sub_arch_name = cpu_arch[i].name;
851 cpu_arch_flags |= cpu_arch[i].flags;
852 }
853 *input_line_pointer = e;
854 demand_empty_rest_of_line ();
855 return;
856 }
857 }
858 if (!cpu_arch[i].name)
859 as_bad (_("no such architecture: `%s'"), string);
860
861 *input_line_pointer = e;
862 }
863 else
864 as_bad (_("missing cpu architecture"));
865
866 no_cond_jump_promotion = 0;
867 if (*input_line_pointer == ','
868 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
869 {
870 char *string = ++input_line_pointer;
871 int e = get_symbol_end ();
872
873 if (strcmp (string, "nojumps") == 0)
874 no_cond_jump_promotion = 1;
875 else if (strcmp (string, "jumps") == 0)
876 ;
877 else
878 as_bad (_("no such architecture modifier: `%s'"), string);
879
880 *input_line_pointer = e;
881 }
882
883 demand_empty_rest_of_line ();
884 }
885
886 unsigned long
i386_mach()887 i386_mach ()
888 {
889 if (!strcmp (default_arch, "x86_64"))
890 return bfd_mach_x86_64;
891 else if (!strcmp (default_arch, "i386"))
892 return bfd_mach_i386_i386;
893 else
894 as_fatal (_("Unknown architecture"));
895 }
896
897 void
md_begin()898 md_begin ()
899 {
900 const char *hash_err;
901
902 /* Initialize op_hash hash table. */
903 op_hash = hash_new ();
904
905 {
906 const template *optab;
907 templates *core_optab;
908
909 /* Setup for loop. */
910 optab = i386_optab;
911 core_optab = (templates *) xmalloc (sizeof (templates));
912 core_optab->start = optab;
913
914 while (1)
915 {
916 ++optab;
917 if (optab->name == NULL
918 || strcmp (optab->name, (optab - 1)->name) != 0)
919 {
920 /* different name --> ship out current template list;
921 add to hash table; & begin anew. */
922 core_optab->end = optab;
923 hash_err = hash_insert (op_hash,
924 (optab - 1)->name,
925 (PTR) core_optab);
926 if (hash_err)
927 {
928 as_fatal (_("Internal Error: Can't hash %s: %s"),
929 (optab - 1)->name,
930 hash_err);
931 }
932 if (optab->name == NULL)
933 break;
934 core_optab = (templates *) xmalloc (sizeof (templates));
935 core_optab->start = optab;
936 }
937 }
938 }
939
940 /* Initialize reg_hash hash table. */
941 reg_hash = hash_new ();
942 {
943 const reg_entry *regtab;
944
945 for (regtab = i386_regtab;
946 regtab < i386_regtab + sizeof (i386_regtab) / sizeof (i386_regtab[0]);
947 regtab++)
948 {
949 hash_err = hash_insert (reg_hash, regtab->reg_name, (PTR) regtab);
950 if (hash_err)
951 as_fatal (_("Internal Error: Can't hash %s: %s"),
952 regtab->reg_name,
953 hash_err);
954 }
955 }
956
957 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
958 {
959 int c;
960 char *p;
961
962 for (c = 0; c < 256; c++)
963 {
964 if (ISDIGIT (c))
965 {
966 digit_chars[c] = c;
967 mnemonic_chars[c] = c;
968 register_chars[c] = c;
969 operand_chars[c] = c;
970 }
971 else if (ISLOWER (c))
972 {
973 mnemonic_chars[c] = c;
974 register_chars[c] = c;
975 operand_chars[c] = c;
976 }
977 else if (ISUPPER (c))
978 {
979 mnemonic_chars[c] = TOLOWER (c);
980 register_chars[c] = mnemonic_chars[c];
981 operand_chars[c] = c;
982 }
983
984 if (ISALPHA (c) || ISDIGIT (c))
985 identifier_chars[c] = c;
986 else if (c >= 128)
987 {
988 identifier_chars[c] = c;
989 operand_chars[c] = c;
990 }
991 }
992
993 #ifdef LEX_AT
994 identifier_chars['@'] = '@';
995 #endif
996 #ifdef LEX_QM
997 identifier_chars['?'] = '?';
998 operand_chars['?'] = '?';
999 #endif
1000 digit_chars['-'] = '-';
1001 mnemonic_chars['-'] = '-';
1002 identifier_chars['_'] = '_';
1003 identifier_chars['.'] = '.';
1004
1005 for (p = operand_special_chars; *p != '\0'; p++)
1006 operand_chars[(unsigned char) *p] = *p;
1007 }
1008
1009 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1010 if (IS_ELF)
1011 {
1012 record_alignment (text_section, 2);
1013 record_alignment (data_section, 2);
1014 record_alignment (bss_section, 2);
1015 }
1016 #endif
1017
1018 if (flag_code == CODE_64BIT)
1019 {
1020 x86_dwarf2_return_column = 16;
1021 x86_cie_data_alignment = -8;
1022 }
1023 else
1024 {
1025 x86_dwarf2_return_column = 8;
1026 x86_cie_data_alignment = -4;
1027 }
1028 }
1029
1030 void
i386_print_statistics(file)1031 i386_print_statistics (file)
1032 FILE *file;
1033 {
1034 hash_print_statistics (file, "i386 opcode", op_hash);
1035 hash_print_statistics (file, "i386 register", reg_hash);
1036 }
1037
1038 #ifdef DEBUG386
1039
1040 /* Debugging routines for md_assemble. */
1041 static void pi PARAMS ((char *, i386_insn *));
1042 static void pte PARAMS ((template *));
1043 static void pt PARAMS ((unsigned int));
1044 static void pe PARAMS ((expressionS *));
1045 static void ps PARAMS ((symbolS *));
1046
1047 static void
pi(line,x)1048 pi (line, x)
1049 char *line;
1050 i386_insn *x;
1051 {
1052 unsigned int i;
1053
1054 fprintf (stdout, "%s: template ", line);
1055 pte (&x->tm);
1056 fprintf (stdout, " address: base %s index %s scale %x\n",
1057 x->base_reg ? x->base_reg->reg_name : "none",
1058 x->index_reg ? x->index_reg->reg_name : "none",
1059 x->log2_scale_factor);
1060 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
1061 x->rm.mode, x->rm.reg, x->rm.regmem);
1062 fprintf (stdout, " sib: base %x index %x scale %x\n",
1063 x->sib.base, x->sib.index, x->sib.scale);
1064 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
1065 (x->rex & REX_MODE64) != 0,
1066 (x->rex & REX_EXTX) != 0,
1067 (x->rex & REX_EXTY) != 0,
1068 (x->rex & REX_EXTZ) != 0);
1069 for (i = 0; i < x->operands; i++)
1070 {
1071 fprintf (stdout, " #%d: ", i + 1);
1072 pt (x->types[i]);
1073 fprintf (stdout, "\n");
1074 if (x->types[i]
1075 & (Reg | SReg2 | SReg3 | Control | Debug | Test | RegMMX | RegXMM))
1076 fprintf (stdout, "%s\n", x->op[i].regs->reg_name);
1077 if (x->types[i] & Imm)
1078 pe (x->op[i].imms);
1079 if (x->types[i] & Disp)
1080 pe (x->op[i].disps);
1081 }
1082 }
1083
1084 static void
pte(t)1085 pte (t)
1086 template *t;
1087 {
1088 unsigned int i;
1089 fprintf (stdout, " %d operands ", t->operands);
1090 fprintf (stdout, "opcode %x ", t->base_opcode);
1091 if (t->extension_opcode != None)
1092 fprintf (stdout, "ext %x ", t->extension_opcode);
1093 if (t->opcode_modifier & D)
1094 fprintf (stdout, "D");
1095 if (t->opcode_modifier & W)
1096 fprintf (stdout, "W");
1097 fprintf (stdout, "\n");
1098 for (i = 0; i < t->operands; i++)
1099 {
1100 fprintf (stdout, " #%d type ", i + 1);
1101 pt (t->operand_types[i]);
1102 fprintf (stdout, "\n");
1103 }
1104 }
1105
1106 static void
pe(e)1107 pe (e)
1108 expressionS *e;
1109 {
1110 fprintf (stdout, " operation %d\n", e->X_op);
1111 fprintf (stdout, " add_number %ld (%lx)\n",
1112 (long) e->X_add_number, (long) e->X_add_number);
1113 if (e->X_add_symbol)
1114 {
1115 fprintf (stdout, " add_symbol ");
1116 ps (e->X_add_symbol);
1117 fprintf (stdout, "\n");
1118 }
1119 if (e->X_op_symbol)
1120 {
1121 fprintf (stdout, " op_symbol ");
1122 ps (e->X_op_symbol);
1123 fprintf (stdout, "\n");
1124 }
1125 }
1126
1127 static void
ps(s)1128 ps (s)
1129 symbolS *s;
1130 {
1131 fprintf (stdout, "%s type %s%s",
1132 S_GET_NAME (s),
1133 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
1134 segment_name (S_GET_SEGMENT (s)));
1135 }
1136
1137 static struct type_name
1138 {
1139 unsigned int mask;
1140 char *tname;
1141 }
1142 const type_names[] =
1143 {
1144 { Reg8, "r8" },
1145 { Reg16, "r16" },
1146 { Reg32, "r32" },
1147 { Reg64, "r64" },
1148 { Imm8, "i8" },
1149 { Imm8S, "i8s" },
1150 { Imm16, "i16" },
1151 { Imm32, "i32" },
1152 { Imm32S, "i32s" },
1153 { Imm64, "i64" },
1154 { Imm1, "i1" },
1155 { BaseIndex, "BaseIndex" },
1156 { Disp8, "d8" },
1157 { Disp16, "d16" },
1158 { Disp32, "d32" },
1159 { Disp32S, "d32s" },
1160 { Disp64, "d64" },
1161 { InOutPortReg, "InOutPortReg" },
1162 { ShiftCount, "ShiftCount" },
1163 { Control, "control reg" },
1164 { Test, "test reg" },
1165 { Debug, "debug reg" },
1166 { FloatReg, "FReg" },
1167 { FloatAcc, "FAcc" },
1168 { SReg2, "SReg2" },
1169 { SReg3, "SReg3" },
1170 { Acc, "Acc" },
1171 { JumpAbsolute, "Jump Absolute" },
1172 { RegMMX, "rMMX" },
1173 { RegXMM, "rXMM" },
1174 { EsSeg, "es" },
1175 { 0, "" }
1176 };
1177
1178 static void
pt(t)1179 pt (t)
1180 unsigned int t;
1181 {
1182 const struct type_name *ty;
1183
1184 for (ty = type_names; ty->mask; ty++)
1185 if (t & ty->mask)
1186 fprintf (stdout, "%s, ", ty->tname);
1187 fflush (stdout);
1188 }
1189
1190 #endif /* DEBUG386 */
1191
1192 static bfd_reloc_code_real_type
reloc(unsigned int size,int pcrel,int sign,bfd_reloc_code_real_type other)1193 reloc (unsigned int size,
1194 int pcrel,
1195 int sign,
1196 bfd_reloc_code_real_type other)
1197 {
1198 if (other != NO_RELOC)
1199 {
1200 reloc_howto_type *reloc;
1201
1202 if (size == 8)
1203 switch (other)
1204 {
1205 case BFD_RELOC_X86_64_GOT32:
1206 return BFD_RELOC_X86_64_GOT64;
1207 break;
1208 case BFD_RELOC_X86_64_PLTOFF64:
1209 return BFD_RELOC_X86_64_PLTOFF64;
1210 break;
1211 case BFD_RELOC_X86_64_GOTPC32:
1212 other = BFD_RELOC_X86_64_GOTPC64;
1213 break;
1214 case BFD_RELOC_X86_64_GOTPCREL:
1215 other = BFD_RELOC_X86_64_GOTPCREL64;
1216 break;
1217 case BFD_RELOC_X86_64_TPOFF32:
1218 other = BFD_RELOC_X86_64_TPOFF64;
1219 break;
1220 case BFD_RELOC_X86_64_DTPOFF32:
1221 other = BFD_RELOC_X86_64_DTPOFF64;
1222 break;
1223 default:
1224 break;
1225 }
1226
1227 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
1228 if (size == 4 && flag_code != CODE_64BIT)
1229 sign = -1;
1230
1231 reloc = bfd_reloc_type_lookup (stdoutput, other);
1232 if (!reloc)
1233 as_bad (_("unknown relocation (%u)"), other);
1234 else if (size != bfd_get_reloc_size (reloc))
1235 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
1236 bfd_get_reloc_size (reloc),
1237 size);
1238 else if (pcrel && !reloc->pc_relative)
1239 as_bad (_("non-pc-relative relocation for pc-relative field"));
1240 else if ((reloc->complain_on_overflow == complain_overflow_signed
1241 && !sign)
1242 || (reloc->complain_on_overflow == complain_overflow_unsigned
1243 && sign > 0))
1244 as_bad (_("relocated field and relocation type differ in signedness"));
1245 else
1246 return other;
1247 return NO_RELOC;
1248 }
1249
1250 if (pcrel)
1251 {
1252 if (!sign)
1253 as_bad (_("there are no unsigned pc-relative relocations"));
1254 switch (size)
1255 {
1256 case 1: return BFD_RELOC_8_PCREL;
1257 case 2: return BFD_RELOC_16_PCREL;
1258 case 4: return BFD_RELOC_32_PCREL;
1259 case 8: return BFD_RELOC_64_PCREL;
1260 }
1261 as_bad (_("cannot do %u byte pc-relative relocation"), size);
1262 }
1263 else
1264 {
1265 if (sign > 0)
1266 switch (size)
1267 {
1268 case 4: return BFD_RELOC_X86_64_32S;
1269 }
1270 else
1271 switch (size)
1272 {
1273 case 1: return BFD_RELOC_8;
1274 case 2: return BFD_RELOC_16;
1275 case 4: return BFD_RELOC_32;
1276 case 8: return BFD_RELOC_64;
1277 }
1278 as_bad (_("cannot do %s %u byte relocation"),
1279 sign > 0 ? "signed" : "unsigned", size);
1280 }
1281
1282 abort ();
1283 return BFD_RELOC_NONE;
1284 }
1285
1286 /* Here we decide which fixups can be adjusted to make them relative to
1287 the beginning of the section instead of the symbol. Basically we need
1288 to make sure that the dynamic relocations are done correctly, so in
1289 some cases we force the original symbol to be used. */
1290
1291 int
tc_i386_fix_adjustable(fixP)1292 tc_i386_fix_adjustable (fixP)
1293 fixS *fixP ATTRIBUTE_UNUSED;
1294 {
1295 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1296 if (!IS_ELF)
1297 return 1;
1298
1299 /* Don't adjust pc-relative references to merge sections in 64-bit
1300 mode. */
1301 if (use_rela_relocations
1302 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
1303 && fixP->fx_pcrel)
1304 return 0;
1305
1306 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
1307 and changed later by validate_fix. */
1308 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
1309 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
1310 return 0;
1311
1312 /* adjust_reloc_syms doesn't know about the GOT. */
1313 if (fixP->fx_r_type == BFD_RELOC_386_GOTOFF
1314 || fixP->fx_r_type == BFD_RELOC_386_PLT32
1315 || fixP->fx_r_type == BFD_RELOC_386_GOT32
1316 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
1317 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
1318 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
1319 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
1320 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
1321 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
1322 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
1323 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
1324 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
1325 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
1326 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
1327 || fixP->fx_r_type == BFD_RELOC_X86_64_PLT32
1328 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
1329 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
1330 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
1331 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
1332 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
1333 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
1334 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
1335 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
1336 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
1337 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
1338 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
1339 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
1340 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1341 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1342 return 0;
1343 #endif
1344 return 1;
1345 }
1346
1347 static int intel_float_operand PARAMS ((const char *mnemonic));
1348
1349 static int
intel_float_operand(mnemonic)1350 intel_float_operand (mnemonic)
1351 const char *mnemonic;
1352 {
1353 /* Note that the value returned is meaningful only for opcodes with (memory)
1354 operands, hence the code here is free to improperly handle opcodes that
1355 have no operands (for better performance and smaller code). */
1356
1357 if (mnemonic[0] != 'f')
1358 return 0; /* non-math */
1359
1360 switch (mnemonic[1])
1361 {
1362 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
1363 the fs segment override prefix not currently handled because no
1364 call path can make opcodes without operands get here */
1365 case 'i':
1366 return 2 /* integer op */;
1367 case 'l':
1368 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
1369 return 3; /* fldcw/fldenv */
1370 break;
1371 case 'n':
1372 if (mnemonic[2] != 'o' /* fnop */)
1373 return 3; /* non-waiting control op */
1374 break;
1375 case 'r':
1376 if (mnemonic[2] == 's')
1377 return 3; /* frstor/frstpm */
1378 break;
1379 case 's':
1380 if (mnemonic[2] == 'a')
1381 return 3; /* fsave */
1382 if (mnemonic[2] == 't')
1383 {
1384 switch (mnemonic[3])
1385 {
1386 case 'c': /* fstcw */
1387 case 'd': /* fstdw */
1388 case 'e': /* fstenv */
1389 case 's': /* fsts[gw] */
1390 return 3;
1391 }
1392 }
1393 break;
1394 case 'x':
1395 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
1396 return 0; /* fxsave/fxrstor are not really math ops */
1397 break;
1398 }
1399
1400 return 1;
1401 }
1402
1403 /* This is the guts of the machine-dependent assembler. LINE points to a
1404 machine dependent instruction. This function is supposed to emit
1405 the frags/bytes it assembles to. */
1406
1407 void
md_assemble(line)1408 md_assemble (line)
1409 char *line;
1410 {
1411 int j;
1412 char mnemonic[MAX_MNEM_SIZE];
1413
1414 /* Initialize globals. */
1415 memset (&i, '\0', sizeof (i));
1416 for (j = 0; j < MAX_OPERANDS; j++)
1417 i.reloc[j] = NO_RELOC;
1418 memset (disp_expressions, '\0', sizeof (disp_expressions));
1419 memset (im_expressions, '\0', sizeof (im_expressions));
1420 save_stack_p = save_stack;
1421
1422 /* First parse an instruction mnemonic & call i386_operand for the operands.
1423 We assume that the scrubber has arranged it so that line[0] is the valid
1424 start of a (possibly prefixed) mnemonic. */
1425
1426 line = parse_insn (line, mnemonic);
1427 if (line == NULL)
1428 return;
1429
1430 line = parse_operands (line, mnemonic);
1431 if (line == NULL)
1432 return;
1433
1434 /* Now we've parsed the mnemonic into a set of templates, and have the
1435 operands at hand. */
1436
1437 /* All intel opcodes have reversed operands except for "bound" and
1438 "enter". We also don't reverse intersegment "jmp" and "call"
1439 instructions with 2 immediate operands so that the immediate segment
1440 precedes the offset, as it does when in AT&T mode. "enter" and the
1441 intersegment "jmp" and "call" instructions are the only ones that
1442 have two immediate operands. */
1443 if (intel_syntax && i.operands > 1
1444 && (strcmp (mnemonic, "bound") != 0)
1445 && (strcmp (mnemonic, "invlpga") != 0)
1446 && !((i.types[0] & Imm) && (i.types[1] & Imm)))
1447 swap_operands ();
1448
1449 if (i.imm_operands)
1450 optimize_imm ();
1451
1452 /* Don't optimize displacement for movabs since it only takes 64bit
1453 displacement. */
1454 if (i.disp_operands
1455 && (flag_code != CODE_64BIT
1456 || strcmp (mnemonic, "movabs") != 0))
1457 optimize_disp ();
1458
1459 /* Next, we find a template that matches the given insn,
1460 making sure the overlap of the given operands types is consistent
1461 with the template operand types. */
1462
1463 if (!match_template ())
1464 return;
1465
1466 if (intel_syntax)
1467 {
1468 /* Undo SYSV386_COMPAT brokenness when in Intel mode. See i386.h */
1469 if (SYSV386_COMPAT
1470 && (i.tm.base_opcode & 0xfffffde0) == 0xdce0)
1471 i.tm.base_opcode ^= FloatR;
1472
1473 /* Zap movzx and movsx suffix. The suffix may have been set from
1474 "word ptr" or "byte ptr" on the source operand, but we'll use
1475 the suffix later to choose the destination register. */
1476 if ((i.tm.base_opcode & ~9) == 0x0fb6)
1477 {
1478 if (i.reg_operands < 2
1479 && !i.suffix
1480 && (~i.tm.opcode_modifier
1481 & (No_bSuf
1482 | No_wSuf
1483 | No_lSuf
1484 | No_sSuf
1485 | No_xSuf
1486 | No_qSuf)))
1487 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
1488
1489 i.suffix = 0;
1490 }
1491 }
1492
1493 if (i.tm.opcode_modifier & FWait)
1494 if (!add_prefix (FWAIT_OPCODE))
1495 return;
1496
1497 /* Check string instruction segment overrides. */
1498 if ((i.tm.opcode_modifier & IsString) != 0 && i.mem_operands != 0)
1499 {
1500 if (!check_string ())
1501 return;
1502 }
1503
1504 if (!process_suffix ())
1505 return;
1506
1507 /* Make still unresolved immediate matches conform to size of immediate
1508 given in i.suffix. */
1509 if (!finalize_imm ())
1510 return;
1511
1512 if (i.types[0] & Imm1)
1513 i.imm_operands = 0; /* kludge for shift insns. */
1514 if (i.types[0] & ImplicitRegister)
1515 i.reg_operands--;
1516 if (i.types[1] & ImplicitRegister)
1517 i.reg_operands--;
1518 if (i.types[2] & ImplicitRegister)
1519 i.reg_operands--;
1520
1521 if (i.tm.opcode_modifier & ImmExt)
1522 {
1523 expressionS *exp;
1524
1525 if ((i.tm.cpu_flags & (CpuPNI|CpuXSAVE|CpuSMAP)) && i.operands > 0)
1526 {
1527 /* These Intel Prescott New Instructions have the fixed
1528 operands with an opcode suffix which is coded in the same
1529 place as an 8-bit immediate field would be. Here we check
1530 those operands and remove them afterwards. */
1531 unsigned int x;
1532
1533 for (x = 0; x < i.operands; x++)
1534 if (i.op[x].regs->reg_num != x)
1535 as_bad (_("can't use register '%%%s' as operand %d in '%s'."),
1536 i.op[x].regs->reg_name, x + 1, i.tm.name);
1537 i.operands = 0;
1538 }
1539
1540 /* These AMD 3DNow! and Intel Katmai New Instructions have an
1541 opcode suffix which is coded in the same place as an 8-bit
1542 immediate field would be. Here we fake an 8-bit immediate
1543 operand from the opcode suffix stored in tm.extension_opcode. */
1544
1545 assert (i.imm_operands == 0 && i.operands <= 2 && 2 < MAX_OPERANDS);
1546
1547 exp = &im_expressions[i.imm_operands++];
1548 i.op[i.operands].imms = exp;
1549 i.types[i.operands++] = Imm8;
1550 exp->X_op = O_constant;
1551 exp->X_add_number = i.tm.extension_opcode;
1552 i.tm.extension_opcode = None;
1553 }
1554
1555 /* For insns with operands there are more diddles to do to the opcode. */
1556 if (i.operands)
1557 {
1558 if (!process_operands ())
1559 return;
1560 }
1561 else if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
1562 {
1563 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
1564 as_warn (_("translating to `%sp'"), i.tm.name);
1565 }
1566
1567 /* Handle conversion of 'int $3' --> special int3 insn. */
1568 if (i.tm.base_opcode == INT_OPCODE && i.op[0].imms->X_add_number == 3)
1569 {
1570 i.tm.base_opcode = INT3_OPCODE;
1571 i.imm_operands = 0;
1572 }
1573
1574 if ((i.tm.opcode_modifier & (Jump | JumpByte | JumpDword))
1575 && i.op[0].disps->X_op == O_constant)
1576 {
1577 /* Convert "jmp constant" (and "call constant") to a jump (call) to
1578 the absolute address given by the constant. Since ix86 jumps and
1579 calls are pc relative, we need to generate a reloc. */
1580 i.op[0].disps->X_add_symbol = &abs_symbol;
1581 i.op[0].disps->X_op = O_symbol;
1582 }
1583
1584 if ((i.tm.opcode_modifier & Rex64) != 0)
1585 i.rex |= REX_MODE64;
1586
1587 /* For 8 bit registers we need an empty rex prefix. Also if the
1588 instruction already has a prefix, we need to convert old
1589 registers to new ones. */
1590
1591 if (((i.types[0] & Reg8) != 0
1592 && (i.op[0].regs->reg_flags & RegRex64) != 0)
1593 || ((i.types[1] & Reg8) != 0
1594 && (i.op[1].regs->reg_flags & RegRex64) != 0)
1595 || (((i.types[0] & Reg8) != 0 || (i.types[1] & Reg8) != 0)
1596 && i.rex != 0))
1597 {
1598 int x;
1599
1600 i.rex |= REX_OPCODE;
1601 for (x = 0; x < 2; x++)
1602 {
1603 /* Look for 8 bit operand that uses old registers. */
1604 if ((i.types[x] & Reg8) != 0
1605 && (i.op[x].regs->reg_flags & RegRex64) == 0)
1606 {
1607 /* In case it is "hi" register, give up. */
1608 if (i.op[x].regs->reg_num > 3)
1609 as_bad (_("can't encode register '%%%s' in an instruction requiring REX prefix."),
1610 i.op[x].regs->reg_name);
1611
1612 /* Otherwise it is equivalent to the extended register.
1613 Since the encoding doesn't change this is merely
1614 cosmetic cleanup for debug output. */
1615
1616 i.op[x].regs = i.op[x].regs + 8;
1617 }
1618 }
1619 }
1620
1621 if (i.rex != 0)
1622 add_prefix (REX_OPCODE | i.rex);
1623
1624 /* We are ready to output the insn. */
1625 output_insn ();
1626 }
1627
1628 static char *
parse_insn(line,mnemonic)1629 parse_insn (line, mnemonic)
1630 char *line;
1631 char *mnemonic;
1632 {
1633 char *l = line;
1634 char *token_start = l;
1635 char *mnem_p;
1636 int supported;
1637 const template *t;
1638
1639 /* Non-zero if we found a prefix only acceptable with string insns. */
1640 const char *expecting_string_instruction = NULL;
1641
1642 while (1)
1643 {
1644 mnem_p = mnemonic;
1645 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
1646 {
1647 mnem_p++;
1648 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
1649 {
1650 as_bad (_("no such instruction: `%s'"), token_start);
1651 return NULL;
1652 }
1653 l++;
1654 }
1655 if (!is_space_char (*l)
1656 && *l != END_OF_INSN
1657 && (intel_syntax
1658 || (*l != PREFIX_SEPARATOR
1659 && *l != ',')))
1660 {
1661 as_bad (_("invalid character %s in mnemonic"),
1662 output_invalid (*l));
1663 return NULL;
1664 }
1665 if (token_start == l)
1666 {
1667 if (!intel_syntax && *l == PREFIX_SEPARATOR)
1668 as_bad (_("expecting prefix; got nothing"));
1669 else
1670 as_bad (_("expecting mnemonic; got nothing"));
1671 return NULL;
1672 }
1673
1674 /* Look up instruction (or prefix) via hash table. */
1675 current_templates = hash_find (op_hash, mnemonic);
1676
1677 if (*l != END_OF_INSN
1678 && (!is_space_char (*l) || l[1] != END_OF_INSN)
1679 && current_templates
1680 && (current_templates->start->opcode_modifier & IsPrefix))
1681 {
1682 if (current_templates->start->cpu_flags
1683 & (flag_code != CODE_64BIT ? Cpu64 : CpuNo64))
1684 {
1685 as_bad ((flag_code != CODE_64BIT
1686 ? _("`%s' is only supported in 64-bit mode")
1687 : _("`%s' is not supported in 64-bit mode")),
1688 current_templates->start->name);
1689 return NULL;
1690 }
1691 /* If we are in 16-bit mode, do not allow addr16 or data16.
1692 Similarly, in 32-bit mode, do not allow addr32 or data32. */
1693 if ((current_templates->start->opcode_modifier & (Size16 | Size32))
1694 && flag_code != CODE_64BIT
1695 && (((current_templates->start->opcode_modifier & Size32) != 0)
1696 ^ (flag_code == CODE_16BIT)))
1697 {
1698 as_bad (_("redundant %s prefix"),
1699 current_templates->start->name);
1700 return NULL;
1701 }
1702 /* Add prefix, checking for repeated prefixes. */
1703 switch (add_prefix (current_templates->start->base_opcode))
1704 {
1705 case 0:
1706 return NULL;
1707 case 2:
1708 expecting_string_instruction = current_templates->start->name;
1709 break;
1710 }
1711 /* Skip past PREFIX_SEPARATOR and reset token_start. */
1712 token_start = ++l;
1713 }
1714 else
1715 break;
1716 }
1717
1718 if (!current_templates)
1719 {
1720 /* See if we can get a match by trimming off a suffix. */
1721 switch (mnem_p[-1])
1722 {
1723 case WORD_MNEM_SUFFIX:
1724 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
1725 i.suffix = SHORT_MNEM_SUFFIX;
1726 else
1727 case BYTE_MNEM_SUFFIX:
1728 case QWORD_MNEM_SUFFIX:
1729 i.suffix = mnem_p[-1];
1730 mnem_p[-1] = '\0';
1731 current_templates = hash_find (op_hash, mnemonic);
1732 break;
1733 case SHORT_MNEM_SUFFIX:
1734 case LONG_MNEM_SUFFIX:
1735 if (!intel_syntax)
1736 {
1737 i.suffix = mnem_p[-1];
1738 mnem_p[-1] = '\0';
1739 current_templates = hash_find (op_hash, mnemonic);
1740 }
1741 break;
1742
1743 /* Intel Syntax. */
1744 case 'd':
1745 if (intel_syntax)
1746 {
1747 if (intel_float_operand (mnemonic) == 1)
1748 i.suffix = SHORT_MNEM_SUFFIX;
1749 else
1750 i.suffix = LONG_MNEM_SUFFIX;
1751 mnem_p[-1] = '\0';
1752 current_templates = hash_find (op_hash, mnemonic);
1753 }
1754 break;
1755 }
1756 if (!current_templates)
1757 {
1758 as_bad (_("no such instruction: `%s'"), token_start);
1759 return NULL;
1760 }
1761 }
1762
1763 if (current_templates->start->opcode_modifier & (Jump | JumpByte))
1764 {
1765 /* Check for a branch hint. We allow ",pt" and ",pn" for
1766 predict taken and predict not taken respectively.
1767 I'm not sure that branch hints actually do anything on loop
1768 and jcxz insns (JumpByte) for current Pentium4 chips. They
1769 may work in the future and it doesn't hurt to accept them
1770 now. */
1771 if (l[0] == ',' && l[1] == 'p')
1772 {
1773 if (l[2] == 't')
1774 {
1775 if (!add_prefix (DS_PREFIX_OPCODE))
1776 return NULL;
1777 l += 3;
1778 }
1779 else if (l[2] == 'n')
1780 {
1781 if (!add_prefix (CS_PREFIX_OPCODE))
1782 return NULL;
1783 l += 3;
1784 }
1785 }
1786 }
1787 /* Any other comma loses. */
1788 if (*l == ',')
1789 {
1790 as_bad (_("invalid character %s in mnemonic"),
1791 output_invalid (*l));
1792 return NULL;
1793 }
1794
1795 /* Check if instruction is supported on specified architecture. */
1796 supported = 0;
1797 for (t = current_templates->start; t < current_templates->end; ++t)
1798 {
1799 if (!((t->cpu_flags & ~(Cpu64 | CpuNo64))
1800 & ~(cpu_arch_flags & ~(Cpu64 | CpuNo64))))
1801 supported |= 1;
1802 if (!(t->cpu_flags & (flag_code == CODE_64BIT ? CpuNo64 : Cpu64)))
1803 supported |= 2;
1804 }
1805 if (!(supported & 2))
1806 {
1807 as_bad (flag_code == CODE_64BIT
1808 ? _("`%s' is not supported in 64-bit mode")
1809 : _("`%s' is only supported in 64-bit mode"),
1810 current_templates->start->name);
1811 return NULL;
1812 }
1813 if (!(supported & 1))
1814 {
1815 as_warn (_("`%s' is not supported on `%s%s'"),
1816 current_templates->start->name,
1817 cpu_arch_name,
1818 cpu_sub_arch_name ? cpu_sub_arch_name : "");
1819 }
1820 else if ((Cpu386 & ~cpu_arch_flags) && (flag_code != CODE_16BIT))
1821 {
1822 as_warn (_("use .code16 to ensure correct addressing mode"));
1823 }
1824
1825 /* Check for rep/repne without a string instruction. */
1826 if (expecting_string_instruction)
1827 {
1828 static templates override;
1829
1830 for (t = current_templates->start; t < current_templates->end; ++t)
1831 if (t->opcode_modifier & IsString)
1832 break;
1833 if (t >= current_templates->end)
1834 {
1835 as_bad (_("expecting string instruction after `%s'"),
1836 expecting_string_instruction);
1837 return NULL;
1838 }
1839 for (override.start = t; t < current_templates->end; ++t)
1840 if (!(t->opcode_modifier & IsString))
1841 break;
1842 override.end = t;
1843 current_templates = &override;
1844 }
1845
1846 return l;
1847 }
1848
1849 static char *
parse_operands(l,mnemonic)1850 parse_operands (l, mnemonic)
1851 char *l;
1852 const char *mnemonic;
1853 {
1854 char *token_start;
1855
1856 /* 1 if operand is pending after ','. */
1857 unsigned int expecting_operand = 0;
1858
1859 /* Non-zero if operand parens not balanced. */
1860 unsigned int paren_not_balanced;
1861
1862 while (*l != END_OF_INSN)
1863 {
1864 /* Skip optional white space before operand. */
1865 if (is_space_char (*l))
1866 ++l;
1867 if (!is_operand_char (*l) && *l != END_OF_INSN)
1868 {
1869 as_bad (_("invalid character %s before operand %d"),
1870 output_invalid (*l),
1871 i.operands + 1);
1872 return NULL;
1873 }
1874 token_start = l; /* after white space */
1875 paren_not_balanced = 0;
1876 while (paren_not_balanced || *l != ',')
1877 {
1878 if (*l == END_OF_INSN)
1879 {
1880 if (paren_not_balanced)
1881 {
1882 if (!intel_syntax)
1883 as_bad (_("unbalanced parenthesis in operand %d."),
1884 i.operands + 1);
1885 else
1886 as_bad (_("unbalanced brackets in operand %d."),
1887 i.operands + 1);
1888 return NULL;
1889 }
1890 else
1891 break; /* we are done */
1892 }
1893 else if (!is_operand_char (*l) && !is_space_char (*l))
1894 {
1895 as_bad (_("invalid character %s in operand %d"),
1896 output_invalid (*l),
1897 i.operands + 1);
1898 return NULL;
1899 }
1900 if (!intel_syntax)
1901 {
1902 if (*l == '(')
1903 ++paren_not_balanced;
1904 if (*l == ')')
1905 --paren_not_balanced;
1906 }
1907 else
1908 {
1909 if (*l == '[')
1910 ++paren_not_balanced;
1911 if (*l == ']')
1912 --paren_not_balanced;
1913 }
1914 l++;
1915 }
1916 if (l != token_start)
1917 { /* Yes, we've read in another operand. */
1918 unsigned int operand_ok;
1919 this_operand = i.operands++;
1920 if (i.operands > MAX_OPERANDS)
1921 {
1922 as_bad (_("spurious operands; (%d operands/instruction max)"),
1923 MAX_OPERANDS);
1924 return NULL;
1925 }
1926 /* Now parse operand adding info to 'i' as we go along. */
1927 END_STRING_AND_SAVE (l);
1928
1929 if (intel_syntax)
1930 operand_ok =
1931 i386_intel_operand (token_start,
1932 intel_float_operand (mnemonic));
1933 else
1934 operand_ok = i386_operand (token_start);
1935
1936 RESTORE_END_STRING (l);
1937 if (!operand_ok)
1938 return NULL;
1939 }
1940 else
1941 {
1942 if (expecting_operand)
1943 {
1944 expecting_operand_after_comma:
1945 as_bad (_("expecting operand after ','; got nothing"));
1946 return NULL;
1947 }
1948 if (*l == ',')
1949 {
1950 as_bad (_("expecting operand before ','; got nothing"));
1951 return NULL;
1952 }
1953 }
1954
1955 /* Now *l must be either ',' or END_OF_INSN. */
1956 if (*l == ',')
1957 {
1958 if (*++l == END_OF_INSN)
1959 {
1960 /* Just skip it, if it's \n complain. */
1961 goto expecting_operand_after_comma;
1962 }
1963 expecting_operand = 1;
1964 }
1965 }
1966 return l;
1967 }
1968
1969 static void
swap_operands()1970 swap_operands ()
1971 {
1972 union i386_op temp_op;
1973 unsigned int temp_type;
1974 enum bfd_reloc_code_real temp_reloc;
1975 int xchg1 = 0;
1976 int xchg2 = 0;
1977
1978 if (i.operands == 2)
1979 {
1980 xchg1 = 0;
1981 xchg2 = 1;
1982 }
1983 else if (i.operands == 3)
1984 {
1985 xchg1 = 0;
1986 xchg2 = 2;
1987 }
1988 temp_type = i.types[xchg2];
1989 i.types[xchg2] = i.types[xchg1];
1990 i.types[xchg1] = temp_type;
1991 temp_op = i.op[xchg2];
1992 i.op[xchg2] = i.op[xchg1];
1993 i.op[xchg1] = temp_op;
1994 temp_reloc = i.reloc[xchg2];
1995 i.reloc[xchg2] = i.reloc[xchg1];
1996 i.reloc[xchg1] = temp_reloc;
1997
1998 if (i.mem_operands == 2)
1999 {
2000 const seg_entry *temp_seg;
2001 temp_seg = i.seg[0];
2002 i.seg[0] = i.seg[1];
2003 i.seg[1] = temp_seg;
2004 }
2005 }
2006
2007 /* Try to ensure constant immediates are represented in the smallest
2008 opcode possible. */
2009 static void
optimize_imm()2010 optimize_imm ()
2011 {
2012 char guess_suffix = 0;
2013 int op;
2014
2015 if (i.suffix)
2016 guess_suffix = i.suffix;
2017 else if (i.reg_operands)
2018 {
2019 /* Figure out a suffix from the last register operand specified.
2020 We can't do this properly yet, ie. excluding InOutPortReg,
2021 but the following works for instructions with immediates.
2022 In any case, we can't set i.suffix yet. */
2023 for (op = i.operands; --op >= 0;)
2024 if (i.types[op] & Reg)
2025 {
2026 if (i.types[op] & Reg8)
2027 guess_suffix = BYTE_MNEM_SUFFIX;
2028 else if (i.types[op] & Reg16)
2029 guess_suffix = WORD_MNEM_SUFFIX;
2030 else if (i.types[op] & Reg32)
2031 guess_suffix = LONG_MNEM_SUFFIX;
2032 else if (i.types[op] & Reg64)
2033 guess_suffix = QWORD_MNEM_SUFFIX;
2034 break;
2035 }
2036 }
2037 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
2038 guess_suffix = WORD_MNEM_SUFFIX;
2039
2040 for (op = i.operands; --op >= 0;)
2041 if (i.types[op] & Imm)
2042 {
2043 switch (i.op[op].imms->X_op)
2044 {
2045 case O_constant:
2046 /* If a suffix is given, this operand may be shortened. */
2047 switch (guess_suffix)
2048 {
2049 case LONG_MNEM_SUFFIX:
2050 i.types[op] |= Imm32 | Imm64;
2051 break;
2052 case WORD_MNEM_SUFFIX:
2053 i.types[op] |= Imm16 | Imm32S | Imm32 | Imm64;
2054 break;
2055 case BYTE_MNEM_SUFFIX:
2056 i.types[op] |= Imm16 | Imm8 | Imm8S | Imm32S | Imm32 | Imm64;
2057 break;
2058 }
2059
2060 /* If this operand is at most 16 bits, convert it
2061 to a signed 16 bit number before trying to see
2062 whether it will fit in an even smaller size.
2063 This allows a 16-bit operand such as $0xffe0 to
2064 be recognised as within Imm8S range. */
2065 if ((i.types[op] & Imm16)
2066 && (i.op[op].imms->X_add_number & ~(offsetT) 0xffff) == 0)
2067 {
2068 i.op[op].imms->X_add_number =
2069 (((i.op[op].imms->X_add_number & 0xffff) ^ 0x8000) - 0x8000);
2070 }
2071 if ((i.types[op] & Imm32)
2072 && ((i.op[op].imms->X_add_number & ~(offsetT) 0xffffffffL)
2073 == 0))
2074 {
2075 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
2076 ^ ((offsetT) 1 << 31))
2077 - ((offsetT) 1 << 31));
2078 }
2079 i.types[op] |= smallest_imm_type (i.op[op].imms->X_add_number);
2080
2081 /* We must avoid matching of Imm32 templates when 64bit
2082 only immediate is available. */
2083 if (guess_suffix == QWORD_MNEM_SUFFIX)
2084 i.types[op] &= ~Imm32;
2085 break;
2086
2087 case O_absent:
2088 case O_register:
2089 abort ();
2090
2091 /* Symbols and expressions. */
2092 default:
2093 /* Convert symbolic operand to proper sizes for matching, but don't
2094 prevent matching a set of insns that only supports sizes other
2095 than those matching the insn suffix. */
2096 {
2097 unsigned int mask, allowed = 0;
2098 const template *t;
2099
2100 for (t = current_templates->start; t < current_templates->end; ++t)
2101 allowed |= t->operand_types[op];
2102 switch (guess_suffix)
2103 {
2104 case QWORD_MNEM_SUFFIX:
2105 mask = Imm64 | Imm32S;
2106 break;
2107 case LONG_MNEM_SUFFIX:
2108 mask = Imm32;
2109 break;
2110 case WORD_MNEM_SUFFIX:
2111 mask = Imm16;
2112 break;
2113 case BYTE_MNEM_SUFFIX:
2114 mask = Imm8;
2115 break;
2116 default:
2117 mask = 0;
2118 break;
2119 }
2120 if (mask & allowed)
2121 i.types[op] &= mask;
2122 }
2123 break;
2124 }
2125 }
2126 }
2127
2128 /* Try to use the smallest displacement type too. */
2129 static void
optimize_disp()2130 optimize_disp ()
2131 {
2132 int op;
2133
2134 for (op = i.operands; --op >= 0;)
2135 if (i.types[op] & Disp)
2136 {
2137 if (i.op[op].disps->X_op == O_constant)
2138 {
2139 offsetT disp = i.op[op].disps->X_add_number;
2140
2141 if ((i.types[op] & Disp16)
2142 && (disp & ~(offsetT) 0xffff) == 0)
2143 {
2144 /* If this operand is at most 16 bits, convert
2145 to a signed 16 bit number and don't use 64bit
2146 displacement. */
2147 disp = (((disp & 0xffff) ^ 0x8000) - 0x8000);
2148 i.types[op] &= ~Disp64;
2149 }
2150 if ((i.types[op] & Disp32)
2151 && (disp & ~(offsetT) 0xffffffffL) == 0)
2152 {
2153 /* If this operand is at most 32 bits, convert
2154 to a signed 32 bit number and don't use 64bit
2155 displacement. */
2156 disp &= (offsetT) 0xffffffffL;
2157 disp = (disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
2158 i.types[op] &= ~Disp64;
2159 }
2160 if (!disp && (i.types[op] & BaseIndex))
2161 {
2162 i.types[op] &= ~Disp;
2163 i.op[op].disps = 0;
2164 i.disp_operands--;
2165 }
2166 else if (flag_code == CODE_64BIT)
2167 {
2168 if (fits_in_signed_long (disp))
2169 {
2170 i.types[op] &= ~Disp64;
2171 i.types[op] |= Disp32S;
2172 }
2173 if (fits_in_unsigned_long (disp))
2174 i.types[op] |= Disp32;
2175 }
2176 if ((i.types[op] & (Disp32 | Disp32S | Disp16))
2177 && fits_in_signed_byte (disp))
2178 i.types[op] |= Disp8;
2179 }
2180 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
2181 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
2182 {
2183 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
2184 i.op[op].disps, 0, i.reloc[op]);
2185 i.types[op] &= ~Disp;
2186 }
2187 else
2188 /* We only support 64bit displacement on constants. */
2189 i.types[op] &= ~Disp64;
2190 }
2191 }
2192
2193 static int
match_template()2194 match_template ()
2195 {
2196 /* Points to template once we've found it. */
2197 const template *t;
2198 unsigned int overlap0, overlap1, overlap2;
2199 unsigned int found_reverse_match;
2200 int suffix_check;
2201
2202 #define MATCH(overlap, given, template) \
2203 ((overlap & ~JumpAbsolute) \
2204 && (((given) & (BaseIndex | JumpAbsolute)) \
2205 == ((overlap) & (BaseIndex | JumpAbsolute))))
2206
2207 /* If given types r0 and r1 are registers they must be of the same type
2208 unless the expected operand type register overlap is null.
2209 Note that Acc in a template matches every size of reg. */
2210 #define CONSISTENT_REGISTER_MATCH(m0, g0, t0, m1, g1, t1) \
2211 (((g0) & Reg) == 0 || ((g1) & Reg) == 0 \
2212 || ((g0) & Reg) == ((g1) & Reg) \
2213 || ((((m0) & Acc) ? Reg : (t0)) & (((m1) & Acc) ? Reg : (t1)) & Reg) == 0 )
2214
2215 overlap0 = 0;
2216 overlap1 = 0;
2217 overlap2 = 0;
2218 found_reverse_match = 0;
2219 suffix_check = (i.suffix == BYTE_MNEM_SUFFIX
2220 ? No_bSuf
2221 : (i.suffix == WORD_MNEM_SUFFIX
2222 ? No_wSuf
2223 : (i.suffix == SHORT_MNEM_SUFFIX
2224 ? No_sSuf
2225 : (i.suffix == LONG_MNEM_SUFFIX
2226 ? No_lSuf
2227 : (i.suffix == QWORD_MNEM_SUFFIX
2228 ? No_qSuf
2229 : (i.suffix == LONG_DOUBLE_MNEM_SUFFIX
2230 ? No_xSuf : 0))))));
2231
2232 for (t = current_templates->start; t < current_templates->end; t++)
2233 {
2234 /* Must have right number of operands. */
2235 if (i.operands != t->operands)
2236 continue;
2237
2238 /* Check the suffix, except for some instructions in intel mode. */
2239 if ((t->opcode_modifier & suffix_check)
2240 && !(intel_syntax
2241 && (t->opcode_modifier & IgnoreSize)))
2242 continue;
2243
2244 /* In general, don't allow 64-bit operands in 32-bit mode. */
2245 if (i.suffix == QWORD_MNEM_SUFFIX
2246 && flag_code != CODE_64BIT
2247 && (intel_syntax
2248 ? (!(t->opcode_modifier & IgnoreSize)
2249 && !intel_float_operand (t->name))
2250 : intel_float_operand (t->name) != 2)
2251 && (!(t->operand_types[0] & (RegMMX | RegXMM))
2252 || !(t->operand_types[t->operands > 1] & (RegMMX | RegXMM)))
2253 && (t->base_opcode != 0x0fc7
2254 || t->extension_opcode != 1 /* cmpxchg8b */))
2255 continue;
2256
2257 /* Do not verify operands when there are none. */
2258 else if (!t->operands)
2259 {
2260 if (t->cpu_flags & ~cpu_arch_flags)
2261 continue;
2262 /* We've found a match; break out of loop. */
2263 break;
2264 }
2265
2266 overlap0 = i.types[0] & t->operand_types[0];
2267 switch (t->operands)
2268 {
2269 case 1:
2270 if (!MATCH (overlap0, i.types[0], t->operand_types[0]))
2271 continue;
2272 break;
2273 case 2:
2274 case 3:
2275 overlap1 = i.types[1] & t->operand_types[1];
2276 if (!MATCH (overlap0, i.types[0], t->operand_types[0])
2277 || !MATCH (overlap1, i.types[1], t->operand_types[1])
2278 /* monitor in SSE3 is a very special case. The first
2279 register and the second register may have differnet
2280 sizes. */
2281 || !((t->base_opcode == 0x0f01
2282 && t->extension_opcode == 0xc8)
2283 || CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2284 t->operand_types[0],
2285 overlap1, i.types[1],
2286 t->operand_types[1])))
2287 {
2288 /* Check if other direction is valid ... */
2289 if ((t->opcode_modifier & (D | FloatD)) == 0)
2290 continue;
2291
2292 /* Try reversing direction of operands. */
2293 overlap0 = i.types[0] & t->operand_types[1];
2294 overlap1 = i.types[1] & t->operand_types[0];
2295 if (!MATCH (overlap0, i.types[0], t->operand_types[1])
2296 || !MATCH (overlap1, i.types[1], t->operand_types[0])
2297 || !CONSISTENT_REGISTER_MATCH (overlap0, i.types[0],
2298 t->operand_types[1],
2299 overlap1, i.types[1],
2300 t->operand_types[0]))
2301 {
2302 /* Does not match either direction. */
2303 continue;
2304 }
2305 /* found_reverse_match holds which of D or FloatDR
2306 we've found. */
2307 found_reverse_match = t->opcode_modifier & (D | FloatDR);
2308 }
2309 /* Found a forward 2 operand match here. */
2310 else if (t->operands == 3)
2311 {
2312 /* Here we make use of the fact that there are no
2313 reverse match 3 operand instructions, and all 3
2314 operand instructions only need to be checked for
2315 register consistency between operands 2 and 3. */
2316 overlap2 = i.types[2] & t->operand_types[2];
2317 if (!MATCH (overlap2, i.types[2], t->operand_types[2])
2318 || !CONSISTENT_REGISTER_MATCH (overlap1, i.types[1],
2319 t->operand_types[1],
2320 overlap2, i.types[2],
2321 t->operand_types[2]))
2322
2323 continue;
2324 }
2325 /* Found either forward/reverse 2 or 3 operand match here:
2326 slip through to break. */
2327 }
2328 if (t->cpu_flags & ~cpu_arch_flags)
2329 {
2330 found_reverse_match = 0;
2331 continue;
2332 }
2333 /* We've found a match; break out of loop. */
2334 break;
2335 }
2336
2337 if (t == current_templates->end)
2338 {
2339 /* We found no match. */
2340 as_bad (_("suffix or operands invalid for `%s'"),
2341 current_templates->start->name);
2342 return 0;
2343 }
2344
2345 if (!quiet_warnings)
2346 {
2347 if (!intel_syntax
2348 && ((i.types[0] & JumpAbsolute)
2349 != (t->operand_types[0] & JumpAbsolute)))
2350 {
2351 as_warn (_("indirect %s without `*'"), t->name);
2352 }
2353
2354 if ((t->opcode_modifier & (IsPrefix | IgnoreSize))
2355 == (IsPrefix | IgnoreSize))
2356 {
2357 /* Warn them that a data or address size prefix doesn't
2358 affect assembly of the next line of code. */
2359 as_warn (_("stand-alone `%s' prefix"), t->name);
2360 }
2361 }
2362
2363 /* Copy the template we found. */
2364 i.tm = *t;
2365 if (found_reverse_match)
2366 {
2367 /* If we found a reverse match we must alter the opcode
2368 direction bit. found_reverse_match holds bits to change
2369 (different for int & float insns). */
2370
2371 i.tm.base_opcode ^= found_reverse_match;
2372
2373 i.tm.operand_types[0] = t->operand_types[1];
2374 i.tm.operand_types[1] = t->operand_types[0];
2375 }
2376
2377 return 1;
2378 }
2379
2380 static int
check_string()2381 check_string ()
2382 {
2383 int mem_op = (i.types[0] & AnyMem) ? 0 : 1;
2384 if ((i.tm.operand_types[mem_op] & EsSeg) != 0)
2385 {
2386 if (i.seg[0] != NULL && i.seg[0] != &es)
2387 {
2388 as_bad (_("`%s' operand %d must use `%%es' segment"),
2389 i.tm.name,
2390 mem_op + 1);
2391 return 0;
2392 }
2393 /* There's only ever one segment override allowed per instruction.
2394 This instruction possibly has a legal segment override on the
2395 second operand, so copy the segment to where non-string
2396 instructions store it, allowing common code. */
2397 i.seg[0] = i.seg[1];
2398 }
2399 else if ((i.tm.operand_types[mem_op + 1] & EsSeg) != 0)
2400 {
2401 if (i.seg[1] != NULL && i.seg[1] != &es)
2402 {
2403 as_bad (_("`%s' operand %d must use `%%es' segment"),
2404 i.tm.name,
2405 mem_op + 2);
2406 return 0;
2407 }
2408 }
2409 return 1;
2410 }
2411
2412 static int
process_suffix(void)2413 process_suffix (void)
2414 {
2415 /* If matched instruction specifies an explicit instruction mnemonic
2416 suffix, use it. */
2417 if (i.tm.opcode_modifier & (Size16 | Size32 | Size64))
2418 {
2419 if (i.tm.opcode_modifier & Size16)
2420 i.suffix = WORD_MNEM_SUFFIX;
2421 else if (i.tm.opcode_modifier & Size64)
2422 i.suffix = QWORD_MNEM_SUFFIX;
2423 else
2424 i.suffix = LONG_MNEM_SUFFIX;
2425 }
2426 else if (i.reg_operands)
2427 {
2428 /* If there's no instruction mnemonic suffix we try to invent one
2429 based on register operands. */
2430 if (!i.suffix)
2431 {
2432 /* We take i.suffix from the last register operand specified,
2433 Destination register type is more significant than source
2434 register type. */
2435 int op;
2436
2437 for (op = i.operands; --op >= 0;)
2438 if ((i.types[op] & Reg)
2439 && !(i.tm.operand_types[op] & InOutPortReg))
2440 {
2441 i.suffix = ((i.types[op] & Reg8) ? BYTE_MNEM_SUFFIX :
2442 (i.types[op] & Reg16) ? WORD_MNEM_SUFFIX :
2443 (i.types[op] & Reg64) ? QWORD_MNEM_SUFFIX :
2444 LONG_MNEM_SUFFIX);
2445 break;
2446 }
2447 }
2448 else if (i.suffix == BYTE_MNEM_SUFFIX)
2449 {
2450 if (!check_byte_reg ())
2451 return 0;
2452 }
2453 else if (i.suffix == LONG_MNEM_SUFFIX)
2454 {
2455 if (!check_long_reg ())
2456 return 0;
2457 }
2458 else if (i.suffix == QWORD_MNEM_SUFFIX)
2459 {
2460 if (!check_qword_reg ())
2461 return 0;
2462 }
2463 else if (i.suffix == WORD_MNEM_SUFFIX)
2464 {
2465 if (!check_word_reg ())
2466 return 0;
2467 }
2468 else if (intel_syntax && (i.tm.opcode_modifier & IgnoreSize))
2469 /* Do nothing if the instruction is going to ignore the prefix. */
2470 ;
2471 else
2472 abort ();
2473 }
2474 else if ((i.tm.opcode_modifier & DefaultSize)
2475 && !i.suffix
2476 /* exclude fldenv/frstor/fsave/fstenv */
2477 && (i.tm.opcode_modifier & No_sSuf))
2478 {
2479 i.suffix = stackop_size;
2480 }
2481 else if (intel_syntax
2482 && !i.suffix
2483 && ((i.tm.operand_types[0] & JumpAbsolute)
2484 || (i.tm.opcode_modifier & (JumpByte|JumpInterSegment))
2485 || (i.tm.base_opcode == 0x0f01 /* [ls][gi]dt */
2486 && i.tm.extension_opcode <= 3)))
2487 {
2488 switch (flag_code)
2489 {
2490 case CODE_64BIT:
2491 if (!(i.tm.opcode_modifier & No_qSuf))
2492 {
2493 i.suffix = QWORD_MNEM_SUFFIX;
2494 break;
2495 }
2496 case CODE_32BIT:
2497 if (!(i.tm.opcode_modifier & No_lSuf))
2498 i.suffix = LONG_MNEM_SUFFIX;
2499 break;
2500 case CODE_16BIT:
2501 if (!(i.tm.opcode_modifier & No_wSuf))
2502 i.suffix = WORD_MNEM_SUFFIX;
2503 break;
2504 }
2505 }
2506
2507 if (!i.suffix)
2508 {
2509 if (!intel_syntax)
2510 {
2511 if (i.tm.opcode_modifier & W)
2512 {
2513 as_bad (_("no instruction mnemonic suffix given and no register operands; can't size instruction"));
2514 return 0;
2515 }
2516 }
2517 else
2518 {
2519 unsigned int suffixes = ~i.tm.opcode_modifier
2520 & (No_bSuf
2521 | No_wSuf
2522 | No_lSuf
2523 | No_sSuf
2524 | No_xSuf
2525 | No_qSuf);
2526
2527 if ((i.tm.opcode_modifier & W)
2528 || ((suffixes & (suffixes - 1))
2529 && !(i.tm.opcode_modifier & (DefaultSize | IgnoreSize))))
2530 {
2531 as_bad (_("ambiguous operand size for `%s'"), i.tm.name);
2532 return 0;
2533 }
2534 }
2535 }
2536
2537 /* Change the opcode based on the operand size given by i.suffix;
2538 We don't need to change things for byte insns. */
2539
2540 if (i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
2541 {
2542 /* It's not a byte, select word/dword operation. */
2543 if (i.tm.opcode_modifier & W)
2544 {
2545 if (i.tm.opcode_modifier & ShortForm)
2546 i.tm.base_opcode |= 8;
2547 else
2548 i.tm.base_opcode |= 1;
2549 }
2550
2551 /* Now select between word & dword operations via the operand
2552 size prefix, except for instructions that will ignore this
2553 prefix anyway. */
2554 if (i.tm.base_opcode == 0x0f01 && i.tm.extension_opcode == 0xc8)
2555 {
2556 /* monitor in SSE3 is a very special case. The default size
2557 of AX is the size of mode. The address size override
2558 prefix will change the size of AX. */
2559 if (i.op->regs[0].reg_type &
2560 (flag_code == CODE_32BIT ? Reg16 : Reg32))
2561 if (!add_prefix (ADDR_PREFIX_OPCODE))
2562 return 0;
2563 }
2564 else if (i.suffix != QWORD_MNEM_SUFFIX
2565 && i.suffix != LONG_DOUBLE_MNEM_SUFFIX
2566 && !(i.tm.opcode_modifier & (IgnoreSize | FloatMF))
2567 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
2568 || (flag_code == CODE_64BIT
2569 && (i.tm.opcode_modifier & JumpByte))))
2570 {
2571 unsigned int prefix = DATA_PREFIX_OPCODE;
2572
2573 if (i.tm.opcode_modifier & JumpByte) /* jcxz, loop */
2574 prefix = ADDR_PREFIX_OPCODE;
2575
2576 if (!add_prefix (prefix))
2577 return 0;
2578 }
2579
2580 /* Set mode64 for an operand. */
2581 if (i.suffix == QWORD_MNEM_SUFFIX
2582 && flag_code == CODE_64BIT
2583 && (i.tm.opcode_modifier & NoRex64) == 0)
2584 i.rex |= REX_MODE64;
2585
2586 /* Size floating point instruction. */
2587 if (i.suffix == LONG_MNEM_SUFFIX)
2588 if (i.tm.opcode_modifier & FloatMF)
2589 i.tm.base_opcode ^= 4;
2590 }
2591
2592 return 1;
2593 }
2594
2595 static int
check_byte_reg(void)2596 check_byte_reg (void)
2597 {
2598 int op;
2599
2600 for (op = i.operands; --op >= 0;)
2601 {
2602 /* If this is an eight bit register, it's OK. If it's the 16 or
2603 32 bit version of an eight bit register, we will just use the
2604 low portion, and that's OK too. */
2605 if (i.types[op] & Reg8)
2606 continue;
2607
2608 /* movzx and movsx should not generate this warning. */
2609 if (intel_syntax
2610 && (i.tm.base_opcode == 0xfb7
2611 || i.tm.base_opcode == 0xfb6
2612 || i.tm.base_opcode == 0x63
2613 || i.tm.base_opcode == 0xfbe
2614 || i.tm.base_opcode == 0xfbf))
2615 continue;
2616
2617 if ((i.types[op] & WordReg) && i.op[op].regs->reg_num < 4)
2618 {
2619 /* Prohibit these changes in the 64bit mode, since the
2620 lowering is more complicated. */
2621 if (flag_code == CODE_64BIT
2622 && (i.tm.operand_types[op] & InOutPortReg) == 0)
2623 {
2624 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2625 i.op[op].regs->reg_name,
2626 i.suffix);
2627 return 0;
2628 }
2629 #if REGISTER_WARNINGS
2630 if (!quiet_warnings
2631 && (i.tm.operand_types[op] & InOutPortReg) == 0)
2632 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2633 (i.op[op].regs + (i.types[op] & Reg16
2634 ? REGNAM_AL - REGNAM_AX
2635 : REGNAM_AL - REGNAM_EAX))->reg_name,
2636 i.op[op].regs->reg_name,
2637 i.suffix);
2638 #endif
2639 continue;
2640 }
2641 /* Any other register is bad. */
2642 if (i.types[op] & (Reg | RegMMX | RegXMM
2643 | SReg2 | SReg3
2644 | Control | Debug | Test
2645 | FloatReg | FloatAcc))
2646 {
2647 as_bad (_("`%%%s' not allowed with `%s%c'"),
2648 i.op[op].regs->reg_name,
2649 i.tm.name,
2650 i.suffix);
2651 return 0;
2652 }
2653 }
2654 return 1;
2655 }
2656
2657 static int
check_long_reg()2658 check_long_reg ()
2659 {
2660 int op;
2661
2662 for (op = i.operands; --op >= 0;)
2663 /* Reject eight bit registers, except where the template requires
2664 them. (eg. movzb) */
2665 if ((i.types[op] & Reg8) != 0
2666 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2667 {
2668 as_bad (_("`%%%s' not allowed with `%s%c'"),
2669 i.op[op].regs->reg_name,
2670 i.tm.name,
2671 i.suffix);
2672 return 0;
2673 }
2674 /* Warn if the e prefix on a general reg is missing. */
2675 else if ((!quiet_warnings || flag_code == CODE_64BIT)
2676 && (i.types[op] & Reg16) != 0
2677 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2678 {
2679 /* Prohibit these changes in the 64bit mode, since the
2680 lowering is more complicated. */
2681 if (flag_code == CODE_64BIT)
2682 {
2683 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2684 i.op[op].regs->reg_name,
2685 i.suffix);
2686 return 0;
2687 }
2688 #if REGISTER_WARNINGS
2689 else
2690 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2691 (i.op[op].regs + REGNAM_EAX - REGNAM_AX)->reg_name,
2692 i.op[op].regs->reg_name,
2693 i.suffix);
2694 #endif
2695 }
2696 /* Warn if the r prefix on a general reg is missing. */
2697 else if ((i.types[op] & Reg64) != 0
2698 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2699 {
2700 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2701 i.op[op].regs->reg_name,
2702 i.suffix);
2703 return 0;
2704 }
2705 return 1;
2706 }
2707
2708 static int
check_qword_reg()2709 check_qword_reg ()
2710 {
2711 int op;
2712
2713 for (op = i.operands; --op >= 0; )
2714 /* Reject eight bit registers, except where the template requires
2715 them. (eg. movzb) */
2716 if ((i.types[op] & Reg8) != 0
2717 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2718 {
2719 as_bad (_("`%%%s' not allowed with `%s%c'"),
2720 i.op[op].regs->reg_name,
2721 i.tm.name,
2722 i.suffix);
2723 return 0;
2724 }
2725 /* Warn if the e prefix on a general reg is missing. */
2726 else if (((i.types[op] & Reg16) != 0
2727 || (i.types[op] & Reg32) != 0)
2728 && (i.tm.operand_types[op] & (Reg32 | Acc)) != 0)
2729 {
2730 /* Prohibit these changes in the 64bit mode, since the
2731 lowering is more complicated. */
2732 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2733 i.op[op].regs->reg_name,
2734 i.suffix);
2735 return 0;
2736 }
2737 return 1;
2738 }
2739
2740 static int
check_word_reg()2741 check_word_reg ()
2742 {
2743 int op;
2744 for (op = i.operands; --op >= 0;)
2745 /* Reject eight bit registers, except where the template requires
2746 them. (eg. movzb) */
2747 if ((i.types[op] & Reg8) != 0
2748 && (i.tm.operand_types[op] & (Reg16 | Reg32 | Acc)) != 0)
2749 {
2750 as_bad (_("`%%%s' not allowed with `%s%c'"),
2751 i.op[op].regs->reg_name,
2752 i.tm.name,
2753 i.suffix);
2754 return 0;
2755 }
2756 /* Warn if the e prefix on a general reg is present. */
2757 else if ((!quiet_warnings || flag_code == CODE_64BIT)
2758 && (i.types[op] & Reg32) != 0
2759 && (i.tm.operand_types[op] & (Reg16 | Acc)) != 0)
2760 {
2761 /* Prohibit these changes in the 64bit mode, since the
2762 lowering is more complicated. */
2763 if (flag_code == CODE_64BIT)
2764 {
2765 as_bad (_("Incorrect register `%%%s' used with `%c' suffix"),
2766 i.op[op].regs->reg_name,
2767 i.suffix);
2768 return 0;
2769 }
2770 else
2771 #if REGISTER_WARNINGS
2772 as_warn (_("using `%%%s' instead of `%%%s' due to `%c' suffix"),
2773 (i.op[op].regs + REGNAM_AX - REGNAM_EAX)->reg_name,
2774 i.op[op].regs->reg_name,
2775 i.suffix);
2776 #endif
2777 }
2778 return 1;
2779 }
2780
2781 static int
finalize_imm()2782 finalize_imm ()
2783 {
2784 unsigned int overlap0, overlap1, overlap2;
2785
2786 overlap0 = i.types[0] & i.tm.operand_types[0];
2787 if ((overlap0 & (Imm8 | Imm8S | Imm16 | Imm32 | Imm32S | Imm64))
2788 && overlap0 != Imm8 && overlap0 != Imm8S
2789 && overlap0 != Imm16 && overlap0 != Imm32S
2790 && overlap0 != Imm32 && overlap0 != Imm64)
2791 {
2792 if (i.suffix)
2793 {
2794 overlap0 &= (i.suffix == BYTE_MNEM_SUFFIX
2795 ? Imm8 | Imm8S
2796 : (i.suffix == WORD_MNEM_SUFFIX
2797 ? Imm16
2798 : (i.suffix == QWORD_MNEM_SUFFIX
2799 ? Imm64 | Imm32S
2800 : Imm32)));
2801 }
2802 else if (overlap0 == (Imm16 | Imm32S | Imm32)
2803 || overlap0 == (Imm16 | Imm32)
2804 || overlap0 == (Imm16 | Imm32S))
2805 {
2806 overlap0 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
2807 ? Imm16 : Imm32S);
2808 }
2809 if (overlap0 != Imm8 && overlap0 != Imm8S
2810 && overlap0 != Imm16 && overlap0 != Imm32S
2811 && overlap0 != Imm32 && overlap0 != Imm64)
2812 {
2813 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size"));
2814 return 0;
2815 }
2816 }
2817 i.types[0] = overlap0;
2818
2819 overlap1 = i.types[1] & i.tm.operand_types[1];
2820 if ((overlap1 & (Imm8 | Imm8S | Imm16 | Imm32S | Imm32 | Imm64))
2821 && overlap1 != Imm8 && overlap1 != Imm8S
2822 && overlap1 != Imm16 && overlap1 != Imm32S
2823 && overlap1 != Imm32 && overlap1 != Imm64)
2824 {
2825 if (i.suffix)
2826 {
2827 overlap1 &= (i.suffix == BYTE_MNEM_SUFFIX
2828 ? Imm8 | Imm8S
2829 : (i.suffix == WORD_MNEM_SUFFIX
2830 ? Imm16
2831 : (i.suffix == QWORD_MNEM_SUFFIX
2832 ? Imm64 | Imm32S
2833 : Imm32)));
2834 }
2835 else if (overlap1 == (Imm16 | Imm32 | Imm32S)
2836 || overlap1 == (Imm16 | Imm32)
2837 || overlap1 == (Imm16 | Imm32S))
2838 {
2839 overlap1 = ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0)
2840 ? Imm16 : Imm32S);
2841 }
2842 if (overlap1 != Imm8 && overlap1 != Imm8S
2843 && overlap1 != Imm16 && overlap1 != Imm32S
2844 && overlap1 != Imm32 && overlap1 != Imm64)
2845 {
2846 as_bad (_("no instruction mnemonic suffix given; can't determine immediate size %x %c"),overlap1, i.suffix);
2847 return 0;
2848 }
2849 }
2850 i.types[1] = overlap1;
2851
2852 overlap2 = i.types[2] & i.tm.operand_types[2];
2853 assert ((overlap2 & Imm) == 0);
2854 i.types[2] = overlap2;
2855
2856 return 1;
2857 }
2858
2859 static int
process_operands()2860 process_operands ()
2861 {
2862 /* Default segment register this instruction will use for memory
2863 accesses. 0 means unknown. This is only for optimizing out
2864 unnecessary segment overrides. */
2865 const seg_entry *default_seg = 0;
2866
2867 /* The imul $imm, %reg instruction is converted into
2868 imul $imm, %reg, %reg, and the clr %reg instruction
2869 is converted into xor %reg, %reg. */
2870 if (i.tm.opcode_modifier & regKludge)
2871 {
2872 unsigned int first_reg_op = (i.types[0] & Reg) ? 0 : 1;
2873 /* Pretend we saw the extra register operand. */
2874 assert (i.op[first_reg_op + 1].regs == 0);
2875 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
2876 i.types[first_reg_op + 1] = i.types[first_reg_op];
2877 i.reg_operands = 2;
2878 }
2879
2880 if (i.tm.opcode_modifier & ShortForm)
2881 {
2882 /* The register or float register operand is in operand 0 or 1. */
2883 unsigned int op = (i.types[0] & (Reg | FloatReg)) ? 0 : 1;
2884 /* Register goes in low 3 bits of opcode. */
2885 i.tm.base_opcode |= i.op[op].regs->reg_num;
2886 if ((i.op[op].regs->reg_flags & RegRex) != 0)
2887 i.rex |= REX_EXTZ;
2888 if (!quiet_warnings && (i.tm.opcode_modifier & Ugh) != 0)
2889 {
2890 /* Warn about some common errors, but press on regardless.
2891 The first case can be generated by gcc (<= 2.8.1). */
2892 if (i.operands == 2)
2893 {
2894 /* Reversed arguments on faddp, fsubp, etc. */
2895 as_warn (_("translating to `%s %%%s,%%%s'"), i.tm.name,
2896 i.op[1].regs->reg_name,
2897 i.op[0].regs->reg_name);
2898 }
2899 else
2900 {
2901 /* Extraneous `l' suffix on fp insn. */
2902 as_warn (_("translating to `%s %%%s'"), i.tm.name,
2903 i.op[0].regs->reg_name);
2904 }
2905 }
2906 }
2907 else if (i.tm.opcode_modifier & Modrm)
2908 {
2909 /* The opcode is completed (modulo i.tm.extension_opcode which
2910 must be put into the modrm byte). Now, we make the modrm and
2911 index base bytes based on all the info we've collected. */
2912
2913 default_seg = build_modrm_byte ();
2914 }
2915 else if (i.tm.opcode_modifier & (Seg2ShortForm | Seg3ShortForm))
2916 {
2917 if (i.tm.base_opcode == POP_SEG_SHORT
2918 && i.op[0].regs->reg_num == 1)
2919 {
2920 as_bad (_("you can't `pop %%cs'"));
2921 return 0;
2922 }
2923 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
2924 if ((i.op[0].regs->reg_flags & RegRex) != 0)
2925 i.rex |= REX_EXTZ;
2926 }
2927 else if ((i.tm.base_opcode & ~(D | W)) == MOV_AX_DISP32)
2928 {
2929 default_seg = &ds;
2930 }
2931 else if ((i.tm.opcode_modifier & IsString) != 0)
2932 {
2933 /* For the string instructions that allow a segment override
2934 on one of their operands, the default segment is ds. */
2935 default_seg = &ds;
2936 }
2937
2938 if ((i.tm.base_opcode == 0x8d /* lea */
2939 || (i.tm.cpu_flags & CpuSVME))
2940 && i.seg[0] && !quiet_warnings)
2941 as_warn (_("segment override on `%s' is ineffectual"), i.tm.name);
2942
2943 /* If a segment was explicitly specified, and the specified segment
2944 is not the default, use an opcode prefix to select it. If we
2945 never figured out what the default segment is, then default_seg
2946 will be zero at this point, and the specified segment prefix will
2947 always be used. */
2948 if ((i.seg[0]) && (i.seg[0] != default_seg))
2949 {
2950 if (!add_prefix (i.seg[0]->seg_prefix))
2951 return 0;
2952 }
2953 return 1;
2954 }
2955
2956 static const seg_entry *
build_modrm_byte()2957 build_modrm_byte ()
2958 {
2959 const seg_entry *default_seg = 0;
2960
2961 /* i.reg_operands MUST be the number of real register operands;
2962 implicit registers do not count. */
2963 if (i.reg_operands == 2)
2964 {
2965 unsigned int source, dest;
2966 source = ((i.types[0]
2967 & (Reg | RegMMX | RegXMM
2968 | SReg2 | SReg3
2969 | Control | Debug | Test))
2970 ? 0 : 1);
2971 dest = source + 1;
2972
2973 i.rm.mode = 3;
2974 /* One of the register operands will be encoded in the i.tm.reg
2975 field, the other in the combined i.tm.mode and i.tm.regmem
2976 fields. If no form of this instruction supports a memory
2977 destination operand, then we assume the source operand may
2978 sometimes be a memory operand and so we need to store the
2979 destination in the i.rm.reg field. */
2980 if ((i.tm.operand_types[dest] & AnyMem) == 0)
2981 {
2982 i.rm.reg = i.op[dest].regs->reg_num;
2983 i.rm.regmem = i.op[source].regs->reg_num;
2984 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
2985 i.rex |= REX_EXTX;
2986 if ((i.op[source].regs->reg_flags & RegRex) != 0)
2987 i.rex |= REX_EXTZ;
2988 }
2989 else
2990 {
2991 i.rm.reg = i.op[source].regs->reg_num;
2992 i.rm.regmem = i.op[dest].regs->reg_num;
2993 if ((i.op[dest].regs->reg_flags & RegRex) != 0)
2994 i.rex |= REX_EXTZ;
2995 if ((i.op[source].regs->reg_flags & RegRex) != 0)
2996 i.rex |= REX_EXTX;
2997 }
2998 if (flag_code != CODE_64BIT && (i.rex & (REX_EXTX | REX_EXTZ)))
2999 {
3000 if (!((i.types[0] | i.types[1]) & Control))
3001 abort ();
3002 i.rex &= ~(REX_EXTX | REX_EXTZ);
3003 add_prefix (LOCK_PREFIX_OPCODE);
3004 }
3005 }
3006 else
3007 { /* If it's not 2 reg operands... */
3008 if (i.mem_operands)
3009 {
3010 unsigned int fake_zero_displacement = 0;
3011 unsigned int op = ((i.types[0] & AnyMem)
3012 ? 0
3013 : (i.types[1] & AnyMem) ? 1 : 2);
3014
3015 default_seg = &ds;
3016
3017 if (i.base_reg == 0)
3018 {
3019 i.rm.mode = 0;
3020 if (!i.disp_operands)
3021 fake_zero_displacement = 1;
3022 if (i.index_reg == 0)
3023 {
3024 /* Operand is just <disp> */
3025 if (flag_code == CODE_64BIT)
3026 {
3027 /* 64bit mode overwrites the 32bit absolute
3028 addressing by RIP relative addressing and
3029 absolute addressing is encoded by one of the
3030 redundant SIB forms. */
3031 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3032 i.sib.base = NO_BASE_REGISTER;
3033 i.sib.index = NO_INDEX_REGISTER;
3034 i.types[op] = ((i.prefix[ADDR_PREFIX] == 0) ? Disp32S : Disp32);
3035 }
3036 else if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
3037 {
3038 i.rm.regmem = NO_BASE_REGISTER_16;
3039 i.types[op] = Disp16;
3040 }
3041 else
3042 {
3043 i.rm.regmem = NO_BASE_REGISTER;
3044 i.types[op] = Disp32;
3045 }
3046 }
3047 else /* !i.base_reg && i.index_reg */
3048 {
3049 i.sib.index = i.index_reg->reg_num;
3050 i.sib.base = NO_BASE_REGISTER;
3051 i.sib.scale = i.log2_scale_factor;
3052 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3053 i.types[op] &= ~Disp;
3054 if (flag_code != CODE_64BIT)
3055 i.types[op] |= Disp32; /* Must be 32 bit */
3056 else
3057 i.types[op] |= Disp32S;
3058 if ((i.index_reg->reg_flags & RegRex) != 0)
3059 i.rex |= REX_EXTY;
3060 }
3061 }
3062 /* RIP addressing for 64bit mode. */
3063 else if (i.base_reg->reg_type == BaseIndex)
3064 {
3065 i.rm.regmem = NO_BASE_REGISTER;
3066 i.types[op] &= ~ Disp;
3067 i.types[op] |= Disp32S;
3068 i.flags[op] = Operand_PCrel;
3069 if (! i.disp_operands)
3070 fake_zero_displacement = 1;
3071 }
3072 else if (i.base_reg->reg_type & Reg16)
3073 {
3074 switch (i.base_reg->reg_num)
3075 {
3076 case 3: /* (%bx) */
3077 if (i.index_reg == 0)
3078 i.rm.regmem = 7;
3079 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
3080 i.rm.regmem = i.index_reg->reg_num - 6;
3081 break;
3082 case 5: /* (%bp) */
3083 default_seg = &ss;
3084 if (i.index_reg == 0)
3085 {
3086 i.rm.regmem = 6;
3087 if ((i.types[op] & Disp) == 0)
3088 {
3089 /* fake (%bp) into 0(%bp) */
3090 i.types[op] |= Disp8;
3091 fake_zero_displacement = 1;
3092 }
3093 }
3094 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
3095 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
3096 break;
3097 default: /* (%si) -> 4 or (%di) -> 5 */
3098 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
3099 }
3100 i.rm.mode = mode_from_disp_size (i.types[op]);
3101 }
3102 else /* i.base_reg and 32/64 bit mode */
3103 {
3104 if (flag_code == CODE_64BIT
3105 && (i.types[op] & Disp))
3106 i.types[op] = (i.types[op] & Disp8) | (i.prefix[ADDR_PREFIX] == 0 ? Disp32S : Disp32);
3107
3108 i.rm.regmem = i.base_reg->reg_num;
3109 if ((i.base_reg->reg_flags & RegRex) != 0)
3110 i.rex |= REX_EXTZ;
3111 i.sib.base = i.base_reg->reg_num;
3112 /* x86-64 ignores REX prefix bit here to avoid decoder
3113 complications. */
3114 if ((i.base_reg->reg_num & 7) == EBP_REG_NUM)
3115 {
3116 default_seg = &ss;
3117 if (i.disp_operands == 0)
3118 {
3119 fake_zero_displacement = 1;
3120 i.types[op] |= Disp8;
3121 }
3122 }
3123 else if (i.base_reg->reg_num == ESP_REG_NUM)
3124 {
3125 default_seg = &ss;
3126 }
3127 i.sib.scale = i.log2_scale_factor;
3128 if (i.index_reg == 0)
3129 {
3130 /* <disp>(%esp) becomes two byte modrm with no index
3131 register. We've already stored the code for esp
3132 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
3133 Any base register besides %esp will not use the
3134 extra modrm byte. */
3135 i.sib.index = NO_INDEX_REGISTER;
3136 #if !SCALE1_WHEN_NO_INDEX
3137 /* Another case where we force the second modrm byte. */
3138 if (i.log2_scale_factor)
3139 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3140 #endif
3141 }
3142 else
3143 {
3144 i.sib.index = i.index_reg->reg_num;
3145 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
3146 if ((i.index_reg->reg_flags & RegRex) != 0)
3147 i.rex |= REX_EXTY;
3148 }
3149
3150 if (i.disp_operands
3151 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
3152 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
3153 i.rm.mode = 0;
3154 else
3155 i.rm.mode = mode_from_disp_size (i.types[op]);
3156 }
3157
3158 if (fake_zero_displacement)
3159 {
3160 /* Fakes a zero displacement assuming that i.types[op]
3161 holds the correct displacement size. */
3162 expressionS *exp;
3163
3164 assert (i.op[op].disps == 0);
3165 exp = &disp_expressions[i.disp_operands++];
3166 i.op[op].disps = exp;
3167 exp->X_op = O_constant;
3168 exp->X_add_number = 0;
3169 exp->X_add_symbol = (symbolS *) 0;
3170 exp->X_op_symbol = (symbolS *) 0;
3171 }
3172 }
3173
3174 /* Fill in i.rm.reg or i.rm.regmem field with register operand
3175 (if any) based on i.tm.extension_opcode. Again, we must be
3176 careful to make sure that segment/control/debug/test/MMX
3177 registers are coded into the i.rm.reg field. */
3178 if (i.reg_operands)
3179 {
3180 unsigned int op =
3181 ((i.types[0]
3182 & (Reg | RegMMX | RegXMM
3183 | SReg2 | SReg3
3184 | Control | Debug | Test))
3185 ? 0
3186 : ((i.types[1]
3187 & (Reg | RegMMX | RegXMM
3188 | SReg2 | SReg3
3189 | Control | Debug | Test))
3190 ? 1
3191 : 2));
3192 /* If there is an extension opcode to put here, the register
3193 number must be put into the regmem field. */
3194 if (i.tm.extension_opcode != None)
3195 {
3196 i.rm.regmem = i.op[op].regs->reg_num;
3197 if ((i.op[op].regs->reg_flags & RegRex) != 0)
3198 i.rex |= REX_EXTZ;
3199 }
3200 else
3201 {
3202 i.rm.reg = i.op[op].regs->reg_num;
3203 if ((i.op[op].regs->reg_flags & RegRex) != 0)
3204 i.rex |= REX_EXTX;
3205 }
3206
3207 /* Now, if no memory operand has set i.rm.mode = 0, 1, 2 we
3208 must set it to 3 to indicate this is a register operand
3209 in the regmem field. */
3210 if (!i.mem_operands)
3211 i.rm.mode = 3;
3212 }
3213
3214 /* Fill in i.rm.reg field with extension opcode (if any). */
3215 if (i.tm.extension_opcode != None)
3216 i.rm.reg = i.tm.extension_opcode;
3217 }
3218 return default_seg;
3219 }
3220
3221 static void
output_branch()3222 output_branch ()
3223 {
3224 char *p;
3225 int code16;
3226 int prefix;
3227 relax_substateT subtype;
3228 symbolS *sym;
3229 offsetT off;
3230
3231 code16 = 0;
3232 if (flag_code == CODE_16BIT)
3233 code16 = CODE16;
3234
3235 prefix = 0;
3236 if (i.prefix[DATA_PREFIX] != 0)
3237 {
3238 prefix = 1;
3239 i.prefixes -= 1;
3240 code16 ^= CODE16;
3241 }
3242 /* Pentium4 branch hints. */
3243 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
3244 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
3245 {
3246 prefix++;
3247 i.prefixes--;
3248 }
3249 if (i.prefix[REX_PREFIX] != 0)
3250 {
3251 prefix++;
3252 i.prefixes--;
3253 }
3254
3255 if (i.prefixes != 0 && !intel_syntax)
3256 as_warn (_("skipping prefixes on this instruction"));
3257
3258 /* It's always a symbol; End frag & setup for relax.
3259 Make sure there is enough room in this frag for the largest
3260 instruction we may generate in md_convert_frag. This is 2
3261 bytes for the opcode and room for the prefix and largest
3262 displacement. */
3263 frag_grow (prefix + 2 + 4);
3264 /* Prefix and 1 opcode byte go in fr_fix. */
3265 p = frag_more (prefix + 1);
3266 if (i.prefix[DATA_PREFIX] != 0)
3267 *p++ = DATA_PREFIX_OPCODE;
3268 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
3269 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
3270 *p++ = i.prefix[SEG_PREFIX];
3271 if (i.prefix[REX_PREFIX] != 0)
3272 *p++ = i.prefix[REX_PREFIX];
3273 *p = i.tm.base_opcode;
3274
3275 if ((unsigned char) *p == JUMP_PC_RELATIVE)
3276 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, SMALL);
3277 else if ((cpu_arch_flags & Cpu386) != 0)
3278 subtype = ENCODE_RELAX_STATE (COND_JUMP, SMALL);
3279 else
3280 subtype = ENCODE_RELAX_STATE (COND_JUMP86, SMALL);
3281 subtype |= code16;
3282
3283 sym = i.op[0].disps->X_add_symbol;
3284 off = i.op[0].disps->X_add_number;
3285
3286 if (i.op[0].disps->X_op != O_constant
3287 && i.op[0].disps->X_op != O_symbol)
3288 {
3289 /* Handle complex expressions. */
3290 sym = make_expr_symbol (i.op[0].disps);
3291 off = 0;
3292 }
3293
3294 /* 1 possible extra opcode + 4 byte displacement go in var part.
3295 Pass reloc in fr_var. */
3296 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
3297 }
3298
3299 static void
output_jump()3300 output_jump ()
3301 {
3302 char *p;
3303 int size;
3304 fixS *fixP;
3305
3306 if (i.tm.opcode_modifier & JumpByte)
3307 {
3308 /* This is a loop or jecxz type instruction. */
3309 size = 1;
3310 if (i.prefix[ADDR_PREFIX] != 0)
3311 {
3312 FRAG_APPEND_1_CHAR (ADDR_PREFIX_OPCODE);
3313 i.prefixes -= 1;
3314 }
3315 /* Pentium4 branch hints. */
3316 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
3317 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
3318 {
3319 FRAG_APPEND_1_CHAR (i.prefix[SEG_PREFIX]);
3320 i.prefixes--;
3321 }
3322 }
3323 else
3324 {
3325 int code16;
3326
3327 code16 = 0;
3328 if (flag_code == CODE_16BIT)
3329 code16 = CODE16;
3330
3331 if (i.prefix[DATA_PREFIX] != 0)
3332 {
3333 FRAG_APPEND_1_CHAR (DATA_PREFIX_OPCODE);
3334 i.prefixes -= 1;
3335 code16 ^= CODE16;
3336 }
3337
3338 size = 4;
3339 if (code16)
3340 size = 2;
3341 }
3342
3343 if (i.prefix[REX_PREFIX] != 0)
3344 {
3345 FRAG_APPEND_1_CHAR (i.prefix[REX_PREFIX]);
3346 i.prefixes -= 1;
3347 }
3348
3349 if (i.prefixes != 0 && !intel_syntax)
3350 as_warn (_("skipping prefixes on this instruction"));
3351
3352 p = frag_more (1 + size);
3353 *p++ = i.tm.base_opcode;
3354
3355 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3356 i.op[0].disps, 1, reloc (size, 1, 1, i.reloc[0]));
3357
3358 /* All jumps handled here are signed, but don't use a signed limit
3359 check for 32 and 16 bit jumps as we want to allow wrap around at
3360 4G and 64k respectively. */
3361 if (size == 1)
3362 fixP->fx_signed = 1;
3363 }
3364
3365 static void
output_interseg_jump()3366 output_interseg_jump ()
3367 {
3368 char *p;
3369 int size;
3370 int prefix;
3371 int code16;
3372
3373 code16 = 0;
3374 if (flag_code == CODE_16BIT)
3375 code16 = CODE16;
3376
3377 prefix = 0;
3378 if (i.prefix[DATA_PREFIX] != 0)
3379 {
3380 prefix = 1;
3381 i.prefixes -= 1;
3382 code16 ^= CODE16;
3383 }
3384 if (i.prefix[REX_PREFIX] != 0)
3385 {
3386 prefix++;
3387 i.prefixes -= 1;
3388 }
3389
3390 size = 4;
3391 if (code16)
3392 size = 2;
3393
3394 if (i.prefixes != 0 && !intel_syntax)
3395 as_warn (_("skipping prefixes on this instruction"));
3396
3397 /* 1 opcode; 2 segment; offset */
3398 p = frag_more (prefix + 1 + 2 + size);
3399
3400 if (i.prefix[DATA_PREFIX] != 0)
3401 *p++ = DATA_PREFIX_OPCODE;
3402
3403 if (i.prefix[REX_PREFIX] != 0)
3404 *p++ = i.prefix[REX_PREFIX];
3405
3406 *p++ = i.tm.base_opcode;
3407 if (i.op[1].imms->X_op == O_constant)
3408 {
3409 offsetT n = i.op[1].imms->X_add_number;
3410
3411 if (size == 2
3412 && !fits_in_unsigned_word (n)
3413 && !fits_in_signed_word (n))
3414 {
3415 as_bad (_("16-bit jump out of range"));
3416 return;
3417 }
3418 md_number_to_chars (p, n, size);
3419 }
3420 else
3421 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3422 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
3423 if (i.op[0].imms->X_op != O_constant)
3424 as_bad (_("can't handle non absolute segment in `%s'"),
3425 i.tm.name);
3426 md_number_to_chars (p + size, (valueT) i.op[0].imms->X_add_number, 2);
3427 }
3428
3429 static void
output_insn()3430 output_insn ()
3431 {
3432 fragS *insn_start_frag;
3433 offsetT insn_start_off;
3434
3435 /* Tie dwarf2 debug info to the address at the start of the insn.
3436 We can't do this after the insn has been output as the current
3437 frag may have been closed off. eg. by frag_var. */
3438 dwarf2_emit_insn (0);
3439
3440 insn_start_frag = frag_now;
3441 insn_start_off = frag_now_fix ();
3442
3443 /* Output jumps. */
3444 if (i.tm.opcode_modifier & Jump)
3445 output_branch ();
3446 else if (i.tm.opcode_modifier & (JumpByte | JumpDword))
3447 output_jump ();
3448 else if (i.tm.opcode_modifier & JumpInterSegment)
3449 output_interseg_jump ();
3450 else
3451 {
3452 /* Output normal instructions here. */
3453 char *p;
3454 unsigned char *q;
3455 unsigned int prefix;
3456
3457 /* All opcodes on i386 have either 1 or 2 bytes. Merom New
3458 Instructions have 3 bytes. We may use one more higher byte
3459 to specify a prefix the instruction requires. */
3460 if ((i.tm.cpu_flags & (CpuMNI|CpuAES|CpuPCLMUL)) != 0)
3461 {
3462 if (i.tm.base_opcode & 0xff000000)
3463 {
3464 prefix = (i.tm.base_opcode >> 24) & 0xff;
3465 goto check_prefix;
3466 }
3467 }
3468 else if ((i.tm.base_opcode & ~3) == 0x660f3880) {
3469 /* INVEPT, INVVPID, and INVPCID are 3 byte instructions with a
3470 mandatory prefix. */
3471 prefix = (i.tm.base_opcode >> 24) & 0xff;
3472 add_prefix (prefix);
3473 }
3474 else if ((i.tm.base_opcode & 0xff0000) != 0)
3475 {
3476 prefix = (i.tm.base_opcode >> 16) & 0xff;
3477 if ((i.tm.cpu_flags & CpuPadLock) != 0)
3478 {
3479 check_prefix:
3480 if (prefix != REPE_PREFIX_OPCODE
3481 || i.prefix[LOCKREP_PREFIX] != REPE_PREFIX_OPCODE)
3482 add_prefix (prefix);
3483 }
3484 else
3485 add_prefix (prefix);
3486 }
3487
3488 /* The prefix bytes. */
3489 for (q = i.prefix;
3490 q < i.prefix + sizeof (i.prefix) / sizeof (i.prefix[0]);
3491 q++)
3492 {
3493 if (*q)
3494 {
3495 p = frag_more (1);
3496 md_number_to_chars (p, (valueT) *q, 1);
3497 }
3498 }
3499
3500 /* Now the opcode; be careful about word order here! */
3501 if (fits_in_unsigned_byte (i.tm.base_opcode))
3502 {
3503 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
3504 }
3505 else
3506 {
3507 if ((i.tm.cpu_flags & (CpuMNI|CpuAES|CpuPCLMUL)) != 0)
3508 {
3509 p = frag_more (3);
3510 *p++ = (i.tm.base_opcode >> 16) & 0xff;
3511 }
3512 else if ((i.tm.base_opcode & ~3) == 0x660f3880)
3513 {
3514 p = frag_more (3);
3515 *p++ = (i.tm.base_opcode >> 16) & 0xff;
3516 }
3517 else
3518 p = frag_more (2);
3519
3520 /* Put out high byte first: can't use md_number_to_chars! */
3521 *p++ = (i.tm.base_opcode >> 8) & 0xff;
3522 *p = i.tm.base_opcode & 0xff;
3523 }
3524
3525 /* Now the modrm byte and sib byte (if present). */
3526 if (i.tm.opcode_modifier & Modrm)
3527 {
3528 p = frag_more (1);
3529 md_number_to_chars (p,
3530 (valueT) (i.rm.regmem << 0
3531 | i.rm.reg << 3
3532 | i.rm.mode << 6),
3533 1);
3534 /* If i.rm.regmem == ESP (4)
3535 && i.rm.mode != (Register mode)
3536 && not 16 bit
3537 ==> need second modrm byte. */
3538 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
3539 && i.rm.mode != 3
3540 && !(i.base_reg && (i.base_reg->reg_type & Reg16) != 0))
3541 {
3542 p = frag_more (1);
3543 md_number_to_chars (p,
3544 (valueT) (i.sib.base << 0
3545 | i.sib.index << 3
3546 | i.sib.scale << 6),
3547 1);
3548 }
3549 }
3550
3551 if (i.disp_operands)
3552 output_disp (insn_start_frag, insn_start_off);
3553
3554 if (i.imm_operands)
3555 output_imm (insn_start_frag, insn_start_off);
3556 }
3557
3558 #ifdef DEBUG386
3559 if (flag_debug)
3560 {
3561 pi ("" /*line*/, &i);
3562 }
3563 #endif /* DEBUG386 */
3564 }
3565
3566 static void
output_disp(insn_start_frag,insn_start_off)3567 output_disp (insn_start_frag, insn_start_off)
3568 fragS *insn_start_frag;
3569 offsetT insn_start_off;
3570 {
3571 char *p;
3572 unsigned int n;
3573
3574 for (n = 0; n < i.operands; n++)
3575 {
3576 if (i.types[n] & Disp)
3577 {
3578 if (i.op[n].disps->X_op == O_constant)
3579 {
3580 int size;
3581 offsetT val;
3582
3583 size = 4;
3584 if (i.types[n] & (Disp8 | Disp16 | Disp64))
3585 {
3586 size = 2;
3587 if (i.types[n] & Disp8)
3588 size = 1;
3589 if (i.types[n] & Disp64)
3590 size = 8;
3591 }
3592 val = offset_in_range (i.op[n].disps->X_add_number,
3593 size);
3594 p = frag_more (size);
3595 md_number_to_chars (p, val, size);
3596 }
3597 else
3598 {
3599 enum bfd_reloc_code_real reloc_type;
3600 int size = 4;
3601 int sign = 0;
3602 int pcrel = (i.flags[n] & Operand_PCrel) != 0;
3603
3604 /* The PC relative address is computed relative
3605 to the instruction boundary, so in case immediate
3606 fields follows, we need to adjust the value. */
3607 if (pcrel && i.imm_operands)
3608 {
3609 int imm_size = 4;
3610 unsigned int n1;
3611
3612 for (n1 = 0; n1 < i.operands; n1++)
3613 if (i.types[n1] & Imm)
3614 {
3615 if (i.types[n1] & (Imm8 | Imm8S | Imm16 | Imm64))
3616 {
3617 imm_size = 2;
3618 if (i.types[n1] & (Imm8 | Imm8S))
3619 imm_size = 1;
3620 if (i.types[n1] & Imm64)
3621 imm_size = 8;
3622 }
3623 break;
3624 }
3625 /* We should find the immediate. */
3626 if (n1 == i.operands)
3627 abort ();
3628 i.op[n].disps->X_add_number -= imm_size;
3629 }
3630
3631 if (i.types[n] & Disp32S)
3632 sign = 1;
3633
3634 if (i.types[n] & (Disp16 | Disp64))
3635 {
3636 size = 2;
3637 if (i.types[n] & Disp64)
3638 size = 8;
3639 }
3640
3641 p = frag_more (size);
3642 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
3643 if (GOT_symbol
3644 && GOT_symbol == i.op[n].disps->X_add_symbol
3645 && (((reloc_type == BFD_RELOC_32
3646 || reloc_type == BFD_RELOC_X86_64_32S
3647 || (reloc_type == BFD_RELOC_64
3648 && object_64bit))
3649 && (i.op[n].disps->X_op == O_symbol
3650 || (i.op[n].disps->X_op == O_add
3651 && ((symbol_get_value_expression
3652 (i.op[n].disps->X_op_symbol)->X_op)
3653 == O_subtract))))
3654 || reloc_type == BFD_RELOC_32_PCREL))
3655 {
3656 offsetT add;
3657
3658 if (insn_start_frag == frag_now)
3659 add = (p - frag_now->fr_literal) - insn_start_off;
3660 else
3661 {
3662 fragS *fr;
3663
3664 add = insn_start_frag->fr_fix - insn_start_off;
3665 for (fr = insn_start_frag->fr_next;
3666 fr && fr != frag_now; fr = fr->fr_next)
3667 add += fr->fr_fix;
3668 add += p - frag_now->fr_literal;
3669 }
3670
3671 if (!object_64bit)
3672 {
3673 reloc_type = BFD_RELOC_386_GOTPC;
3674 i.op[n].imms->X_add_number += add;
3675 }
3676 else if (reloc_type == BFD_RELOC_64)
3677 reloc_type = BFD_RELOC_X86_64_GOTPC64;
3678 else
3679 /* Don't do the adjustment for x86-64, as there
3680 the pcrel addressing is relative to the _next_
3681 insn, and that is taken care of in other code. */
3682 reloc_type = BFD_RELOC_X86_64_GOTPC32;
3683 }
3684 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3685 i.op[n].disps, pcrel, reloc_type);
3686 }
3687 }
3688 }
3689 }
3690
3691 static void
output_imm(insn_start_frag,insn_start_off)3692 output_imm (insn_start_frag, insn_start_off)
3693 fragS *insn_start_frag;
3694 offsetT insn_start_off;
3695 {
3696 char *p;
3697 unsigned int n;
3698
3699 for (n = 0; n < i.operands; n++)
3700 {
3701 if (i.types[n] & Imm)
3702 {
3703 if (i.op[n].imms->X_op == O_constant)
3704 {
3705 int size;
3706 offsetT val;
3707
3708 size = 4;
3709 if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
3710 {
3711 size = 2;
3712 if (i.types[n] & (Imm8 | Imm8S))
3713 size = 1;
3714 else if (i.types[n] & Imm64)
3715 size = 8;
3716 }
3717 val = offset_in_range (i.op[n].imms->X_add_number,
3718 size);
3719 p = frag_more (size);
3720 md_number_to_chars (p, val, size);
3721 }
3722 else
3723 {
3724 /* Not absolute_section.
3725 Need a 32-bit fixup (don't support 8bit
3726 non-absolute imms). Try to support other
3727 sizes ... */
3728 enum bfd_reloc_code_real reloc_type;
3729 int size = 4;
3730 int sign = 0;
3731
3732 if ((i.types[n] & (Imm32S))
3733 && (i.suffix == QWORD_MNEM_SUFFIX
3734 || (!i.suffix && (i.tm.opcode_modifier & No_lSuf))))
3735 sign = 1;
3736 if (i.types[n] & (Imm8 | Imm8S | Imm16 | Imm64))
3737 {
3738 size = 2;
3739 if (i.types[n] & (Imm8 | Imm8S))
3740 size = 1;
3741 if (i.types[n] & Imm64)
3742 size = 8;
3743 }
3744
3745 p = frag_more (size);
3746 reloc_type = reloc (size, 0, sign, i.reloc[n]);
3747
3748 /* This is tough to explain. We end up with this one if we
3749 * have operands that look like
3750 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
3751 * obtain the absolute address of the GOT, and it is strongly
3752 * preferable from a performance point of view to avoid using
3753 * a runtime relocation for this. The actual sequence of
3754 * instructions often look something like:
3755 *
3756 * call .L66
3757 * .L66:
3758 * popl %ebx
3759 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
3760 *
3761 * The call and pop essentially return the absolute address
3762 * of the label .L66 and store it in %ebx. The linker itself
3763 * will ultimately change the first operand of the addl so
3764 * that %ebx points to the GOT, but to keep things simple, the
3765 * .o file must have this operand set so that it generates not
3766 * the absolute address of .L66, but the absolute address of
3767 * itself. This allows the linker itself simply treat a GOTPC
3768 * relocation as asking for a pcrel offset to the GOT to be
3769 * added in, and the addend of the relocation is stored in the
3770 * operand field for the instruction itself.
3771 *
3772 * Our job here is to fix the operand so that it would add
3773 * the correct offset so that %ebx would point to itself. The
3774 * thing that is tricky is that .-.L66 will point to the
3775 * beginning of the instruction, so we need to further modify
3776 * the operand so that it will point to itself. There are
3777 * other cases where you have something like:
3778 *
3779 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
3780 *
3781 * and here no correction would be required. Internally in
3782 * the assembler we treat operands of this form as not being
3783 * pcrel since the '.' is explicitly mentioned, and I wonder
3784 * whether it would simplify matters to do it this way. Who
3785 * knows. In earlier versions of the PIC patches, the
3786 * pcrel_adjust field was used to store the correction, but
3787 * since the expression is not pcrel, I felt it would be
3788 * confusing to do it this way. */
3789
3790 if ((reloc_type == BFD_RELOC_32
3791 || reloc_type == BFD_RELOC_X86_64_32S
3792 || reloc_type == BFD_RELOC_64)
3793 && GOT_symbol
3794 && GOT_symbol == i.op[n].imms->X_add_symbol
3795 && (i.op[n].imms->X_op == O_symbol
3796 || (i.op[n].imms->X_op == O_add
3797 && ((symbol_get_value_expression
3798 (i.op[n].imms->X_op_symbol)->X_op)
3799 == O_subtract))))
3800 {
3801 offsetT add;
3802
3803 if (insn_start_frag == frag_now)
3804 add = (p - frag_now->fr_literal) - insn_start_off;
3805 else
3806 {
3807 fragS *fr;
3808
3809 add = insn_start_frag->fr_fix - insn_start_off;
3810 for (fr = insn_start_frag->fr_next;
3811 fr && fr != frag_now; fr = fr->fr_next)
3812 add += fr->fr_fix;
3813 add += p - frag_now->fr_literal;
3814 }
3815
3816 if (!object_64bit)
3817 reloc_type = BFD_RELOC_386_GOTPC;
3818 else if (size == 4)
3819 reloc_type = BFD_RELOC_X86_64_GOTPC32;
3820 else if (size == 8)
3821 reloc_type = BFD_RELOC_X86_64_GOTPC64;
3822 i.op[n].imms->X_add_number += add;
3823 }
3824 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
3825 i.op[n].imms, 0, reloc_type);
3826 }
3827 }
3828 }
3829 }
3830
3831 /* x86_cons_fix_new is called via the expression parsing code when a
3832 reloc is needed. We use this hook to get the correct .got reloc. */
3833 static enum bfd_reloc_code_real got_reloc = NO_RELOC;
3834 static int cons_sign = -1;
3835
3836 void
x86_cons_fix_new(fragS * frag,unsigned int off,unsigned int len,expressionS * exp)3837 x86_cons_fix_new (fragS *frag,
3838 unsigned int off,
3839 unsigned int len,
3840 expressionS *exp)
3841 {
3842 enum bfd_reloc_code_real r = reloc (len, 0, cons_sign, got_reloc);
3843
3844 got_reloc = NO_RELOC;
3845
3846 #ifdef TE_PE
3847 if (exp->X_op == O_secrel)
3848 {
3849 exp->X_op = O_symbol;
3850 r = BFD_RELOC_32_SECREL;
3851 }
3852 #endif
3853
3854 fix_new_exp (frag, off, len, exp, 0, r);
3855 }
3856
3857 #if (!defined (OBJ_ELF) && !defined (OBJ_MAYBE_ELF)) || defined (LEX_AT)
3858 # define lex_got(reloc, adjust, types) NULL
3859 #else
3860 /* Parse operands of the form
3861 <symbol>@GOTOFF+<nnn>
3862 and similar .plt or .got references.
3863
3864 If we find one, set up the correct relocation in RELOC and copy the
3865 input string, minus the `@GOTOFF' into a malloc'd buffer for
3866 parsing by the calling routine. Return this buffer, and if ADJUST
3867 is non-null set it to the length of the string we removed from the
3868 input line. Otherwise return NULL. */
3869 static char *
lex_got(enum bfd_reloc_code_real * reloc,int * adjust,unsigned int * types)3870 lex_got (enum bfd_reloc_code_real *reloc,
3871 int *adjust,
3872 unsigned int *types)
3873 {
3874 /* Some of the relocations depend on the size of what field is to
3875 be relocated. But in our callers i386_immediate and i386_displacement
3876 we don't yet know the operand size (this will be set by insn
3877 matching). Hence we record the word32 relocation here,
3878 and adjust the reloc according to the real size in reloc(). */
3879 static const struct {
3880 const char *str;
3881 const enum bfd_reloc_code_real rel[2];
3882 const unsigned int types64;
3883 } gotrel[] = {
3884 { "PLTOFF", { 0, BFD_RELOC_X86_64_PLTOFF64 }, Imm64 },
3885 { "PLT", { BFD_RELOC_386_PLT32, BFD_RELOC_X86_64_PLT32 }, Imm32|Imm32S|Disp32 },
3886 { "GOTPLT", { 0, BFD_RELOC_X86_64_GOTPLT64 }, Imm64|Disp64 },
3887 { "GOTOFF", { BFD_RELOC_386_GOTOFF, BFD_RELOC_X86_64_GOTOFF64 }, Imm64|Disp64 },
3888 { "GOTPCREL", { 0, BFD_RELOC_X86_64_GOTPCREL }, Imm32|Imm32S|Disp32 },
3889 { "TLSGD", { BFD_RELOC_386_TLS_GD, BFD_RELOC_X86_64_TLSGD }, Imm32|Imm32S|Disp32 },
3890 { "TLSLDM", { BFD_RELOC_386_TLS_LDM, 0 }, 0 },
3891 { "TLSLD", { 0, BFD_RELOC_X86_64_TLSLD }, Imm32|Imm32S|Disp32 },
3892 { "GOTTPOFF", { BFD_RELOC_386_TLS_IE_32, BFD_RELOC_X86_64_GOTTPOFF }, Imm32|Imm32S|Disp32 },
3893 { "TPOFF", { BFD_RELOC_386_TLS_LE_32, BFD_RELOC_X86_64_TPOFF32 }, Imm32|Imm32S|Imm64|Disp32|Disp64 },
3894 { "NTPOFF", { BFD_RELOC_386_TLS_LE, 0 }, 0 },
3895 { "DTPOFF", { BFD_RELOC_386_TLS_LDO_32, BFD_RELOC_X86_64_DTPOFF32 }, Imm32|Imm32S|Imm64|Disp32|Disp64 },
3896 { "GOTNTPOFF",{ BFD_RELOC_386_TLS_GOTIE, 0 }, 0 },
3897 { "INDNTPOFF",{ BFD_RELOC_386_TLS_IE, 0 }, 0 },
3898 { "GOT", { BFD_RELOC_386_GOT32, BFD_RELOC_X86_64_GOT32 }, Imm32|Imm32S|Disp32|Imm64 },
3899 { "TLSDESC", { BFD_RELOC_386_TLS_GOTDESC, BFD_RELOC_X86_64_GOTPC32_TLSDESC }, Imm32|Imm32S|Disp32 },
3900 { "TLSCALL", { BFD_RELOC_386_TLS_DESC_CALL, BFD_RELOC_X86_64_TLSDESC_CALL }, Imm32|Imm32S|Disp32 }
3901 };
3902 char *cp;
3903 unsigned int j;
3904
3905 if (!IS_ELF)
3906 return NULL;
3907
3908 for (cp = input_line_pointer; *cp != '@'; cp++)
3909 if (is_end_of_line[(unsigned char) *cp])
3910 return NULL;
3911
3912 for (j = 0; j < sizeof (gotrel) / sizeof (gotrel[0]); j++)
3913 {
3914 int len;
3915
3916 len = strlen (gotrel[j].str);
3917 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
3918 {
3919 if (gotrel[j].rel[object_64bit] != 0)
3920 {
3921 int first, second;
3922 char *tmpbuf, *past_reloc;
3923
3924 *reloc = gotrel[j].rel[object_64bit];
3925 if (adjust)
3926 *adjust = len;
3927
3928 if (types)
3929 {
3930 if (flag_code != CODE_64BIT)
3931 *types = Imm32|Disp32;
3932 else
3933 *types = gotrel[j].types64;
3934 }
3935
3936 if (GOT_symbol == NULL)
3937 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
3938
3939 /* Replace the relocation token with ' ', so that
3940 errors like foo@GOTOFF1 will be detected. */
3941
3942 /* The length of the first part of our input line. */
3943 first = cp - input_line_pointer;
3944
3945 /* The second part goes from after the reloc token until
3946 (and including) an end_of_line char. Don't use strlen
3947 here as the end_of_line char may not be a NUL. */
3948 past_reloc = cp + 1 + len;
3949 for (cp = past_reloc; !is_end_of_line[(unsigned char) *cp++]; )
3950 ;
3951 second = cp - past_reloc;
3952
3953 /* Allocate and copy string. The trailing NUL shouldn't
3954 be necessary, but be safe. */
3955 tmpbuf = xmalloc (first + second + 2);
3956 memcpy (tmpbuf, input_line_pointer, first);
3957 tmpbuf[first] = ' ';
3958 memcpy (tmpbuf + first + 1, past_reloc, second);
3959 tmpbuf[first + second + 1] = '\0';
3960 return tmpbuf;
3961 }
3962
3963 as_bad (_("@%s reloc is not supported with %d-bit output format"),
3964 gotrel[j].str, 1 << (5 + object_64bit));
3965 return NULL;
3966 }
3967 }
3968
3969 /* Might be a symbol version string. Don't as_bad here. */
3970 return NULL;
3971 }
3972
3973 void
x86_cons(exp,size)3974 x86_cons (exp, size)
3975 expressionS *exp;
3976 int size;
3977 {
3978 if (size == 4 || (object_64bit && size == 8))
3979 {
3980 /* Handle @GOTOFF and the like in an expression. */
3981 char *save;
3982 char *gotfree_input_line;
3983 int adjust;
3984
3985 save = input_line_pointer;
3986 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
3987 if (gotfree_input_line)
3988 input_line_pointer = gotfree_input_line;
3989
3990 expression (exp);
3991
3992 if (gotfree_input_line)
3993 {
3994 /* expression () has merrily parsed up to the end of line,
3995 or a comma - in the wrong buffer. Transfer how far
3996 input_line_pointer has moved to the right buffer. */
3997 input_line_pointer = (save
3998 + (input_line_pointer - gotfree_input_line)
3999 + adjust);
4000 free (gotfree_input_line);
4001 }
4002 }
4003 else
4004 expression (exp);
4005 }
4006 #endif
4007
signed_cons(int size)4008 static void signed_cons (int size)
4009 {
4010 if (flag_code == CODE_64BIT)
4011 cons_sign = 1;
4012 cons (size);
4013 cons_sign = -1;
4014 }
4015
4016 #ifdef TE_PE
4017 static void
pe_directive_secrel(dummy)4018 pe_directive_secrel (dummy)
4019 int dummy ATTRIBUTE_UNUSED;
4020 {
4021 expressionS exp;
4022
4023 do
4024 {
4025 expression (&exp);
4026 if (exp.X_op == O_symbol)
4027 exp.X_op = O_secrel;
4028
4029 emit_expr (&exp, 4);
4030 }
4031 while (*input_line_pointer++ == ',');
4032
4033 input_line_pointer--;
4034 demand_empty_rest_of_line ();
4035 }
4036 #endif
4037
4038 static int i386_immediate PARAMS ((char *));
4039
4040 static int
i386_immediate(imm_start)4041 i386_immediate (imm_start)
4042 char *imm_start;
4043 {
4044 char *save_input_line_pointer;
4045 char *gotfree_input_line;
4046 segT exp_seg = 0;
4047 expressionS *exp;
4048 unsigned int types = ~0U;
4049
4050 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
4051 {
4052 as_bad (_("only 1 or 2 immediate operands are allowed"));
4053 return 0;
4054 }
4055
4056 exp = &im_expressions[i.imm_operands++];
4057 i.op[this_operand].imms = exp;
4058
4059 if (is_space_char (*imm_start))
4060 ++imm_start;
4061
4062 save_input_line_pointer = input_line_pointer;
4063 input_line_pointer = imm_start;
4064
4065 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
4066 if (gotfree_input_line)
4067 input_line_pointer = gotfree_input_line;
4068
4069 exp_seg = expression (exp);
4070
4071 SKIP_WHITESPACE ();
4072 if (*input_line_pointer)
4073 as_bad (_("junk `%s' after expression"), input_line_pointer);
4074
4075 input_line_pointer = save_input_line_pointer;
4076 if (gotfree_input_line)
4077 free (gotfree_input_line);
4078
4079 if (exp->X_op == O_absent || exp->X_op == O_big)
4080 {
4081 /* Missing or bad expr becomes absolute 0. */
4082 as_bad (_("missing or invalid immediate expression `%s' taken as 0"),
4083 imm_start);
4084 exp->X_op = O_constant;
4085 exp->X_add_number = 0;
4086 exp->X_add_symbol = (symbolS *) 0;
4087 exp->X_op_symbol = (symbolS *) 0;
4088 }
4089 else if (exp->X_op == O_constant)
4090 {
4091 /* Size it properly later. */
4092 i.types[this_operand] |= Imm64;
4093 /* If BFD64, sign extend val. */
4094 if (!use_rela_relocations)
4095 if ((exp->X_add_number & ~(((addressT) 2 << 31) - 1)) == 0)
4096 exp->X_add_number = (exp->X_add_number ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
4097 }
4098 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
4099 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
4100 && exp_seg != absolute_section
4101 && exp_seg != text_section
4102 && exp_seg != data_section
4103 && exp_seg != bss_section
4104 && exp_seg != undefined_section
4105 && !bfd_is_com_section (exp_seg))
4106 {
4107 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
4108 return 0;
4109 }
4110 #endif
4111 else
4112 {
4113 /* This is an address. The size of the address will be
4114 determined later, depending on destination register,
4115 suffix, or the default for the section. */
4116 i.types[this_operand] |= Imm8 | Imm16 | Imm32 | Imm32S | Imm64;
4117 i.types[this_operand] &= types;
4118 }
4119
4120 return 1;
4121 }
4122
4123 static char *i386_scale PARAMS ((char *));
4124
4125 static char *
i386_scale(scale)4126 i386_scale (scale)
4127 char *scale;
4128 {
4129 offsetT val;
4130 char *save = input_line_pointer;
4131
4132 input_line_pointer = scale;
4133 val = get_absolute_expression ();
4134
4135 switch (val)
4136 {
4137 case 1:
4138 i.log2_scale_factor = 0;
4139 break;
4140 case 2:
4141 i.log2_scale_factor = 1;
4142 break;
4143 case 4:
4144 i.log2_scale_factor = 2;
4145 break;
4146 case 8:
4147 i.log2_scale_factor = 3;
4148 break;
4149 default:
4150 {
4151 char sep = *input_line_pointer;
4152
4153 *input_line_pointer = '\0';
4154 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
4155 scale);
4156 *input_line_pointer = sep;
4157 input_line_pointer = save;
4158 return NULL;
4159 }
4160 }
4161 if (i.log2_scale_factor != 0 && i.index_reg == 0)
4162 {
4163 as_warn (_("scale factor of %d without an index register"),
4164 1 << i.log2_scale_factor);
4165 #if SCALE1_WHEN_NO_INDEX
4166 i.log2_scale_factor = 0;
4167 #endif
4168 }
4169 scale = input_line_pointer;
4170 input_line_pointer = save;
4171 return scale;
4172 }
4173
4174 static int i386_displacement PARAMS ((char *, char *));
4175
4176 static int
i386_displacement(disp_start,disp_end)4177 i386_displacement (disp_start, disp_end)
4178 char *disp_start;
4179 char *disp_end;
4180 {
4181 expressionS *exp;
4182 segT exp_seg = 0;
4183 char *save_input_line_pointer;
4184 char *gotfree_input_line;
4185 int bigdisp, override;
4186 unsigned int types = Disp;
4187
4188 if ((i.types[this_operand] & JumpAbsolute)
4189 || !(current_templates->start->opcode_modifier & (Jump | JumpDword)))
4190 {
4191 bigdisp = Disp32;
4192 override = (i.prefix[ADDR_PREFIX] != 0);
4193 }
4194 else
4195 {
4196 /* For PC-relative branches, the width of the displacement
4197 is dependent upon data size, not address size. */
4198 bigdisp = 0;
4199 override = (i.prefix[DATA_PREFIX] != 0);
4200 }
4201 if (flag_code == CODE_64BIT)
4202 {
4203 if (!bigdisp)
4204 bigdisp = (override || i.suffix == WORD_MNEM_SUFFIX)
4205 ? Disp16
4206 : Disp32S | Disp32;
4207 else if (!override)
4208 bigdisp = Disp64 | Disp32S | Disp32;
4209 }
4210 else
4211 {
4212 if (!bigdisp)
4213 {
4214 if (!override)
4215 override = (i.suffix == (flag_code != CODE_16BIT
4216 ? WORD_MNEM_SUFFIX
4217 : LONG_MNEM_SUFFIX));
4218 bigdisp = Disp32;
4219 }
4220 if ((flag_code == CODE_16BIT) ^ override)
4221 bigdisp = Disp16;
4222 }
4223 i.types[this_operand] |= bigdisp;
4224
4225 exp = &disp_expressions[i.disp_operands];
4226 i.op[this_operand].disps = exp;
4227 i.disp_operands++;
4228 save_input_line_pointer = input_line_pointer;
4229 input_line_pointer = disp_start;
4230 END_STRING_AND_SAVE (disp_end);
4231
4232 #ifndef GCC_ASM_O_HACK
4233 #define GCC_ASM_O_HACK 0
4234 #endif
4235 #if GCC_ASM_O_HACK
4236 END_STRING_AND_SAVE (disp_end + 1);
4237 if ((i.types[this_operand] & BaseIndex) != 0
4238 && displacement_string_end[-1] == '+')
4239 {
4240 /* This hack is to avoid a warning when using the "o"
4241 constraint within gcc asm statements.
4242 For instance:
4243
4244 #define _set_tssldt_desc(n,addr,limit,type) \
4245 __asm__ __volatile__ ( \
4246 "movw %w2,%0\n\t" \
4247 "movw %w1,2+%0\n\t" \
4248 "rorl $16,%1\n\t" \
4249 "movb %b1,4+%0\n\t" \
4250 "movb %4,5+%0\n\t" \
4251 "movb $0,6+%0\n\t" \
4252 "movb %h1,7+%0\n\t" \
4253 "rorl $16,%1" \
4254 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
4255
4256 This works great except that the output assembler ends
4257 up looking a bit weird if it turns out that there is
4258 no offset. You end up producing code that looks like:
4259
4260 #APP
4261 movw $235,(%eax)
4262 movw %dx,2+(%eax)
4263 rorl $16,%edx
4264 movb %dl,4+(%eax)
4265 movb $137,5+(%eax)
4266 movb $0,6+(%eax)
4267 movb %dh,7+(%eax)
4268 rorl $16,%edx
4269 #NO_APP
4270
4271 So here we provide the missing zero. */
4272
4273 *displacement_string_end = '0';
4274 }
4275 #endif
4276 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
4277 if (gotfree_input_line)
4278 input_line_pointer = gotfree_input_line;
4279
4280 exp_seg = expression (exp);
4281
4282 SKIP_WHITESPACE ();
4283 if (*input_line_pointer)
4284 as_bad (_("junk `%s' after expression"), input_line_pointer);
4285 #if GCC_ASM_O_HACK
4286 RESTORE_END_STRING (disp_end + 1);
4287 #endif
4288 RESTORE_END_STRING (disp_end);
4289 input_line_pointer = save_input_line_pointer;
4290 if (gotfree_input_line)
4291 free (gotfree_input_line);
4292
4293 /* We do this to make sure that the section symbol is in
4294 the symbol table. We will ultimately change the relocation
4295 to be relative to the beginning of the section. */
4296 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
4297 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
4298 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
4299 {
4300 if (exp->X_op != O_symbol)
4301 {
4302 as_bad (_("bad expression used with @%s"),
4303 (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
4304 ? "GOTPCREL"
4305 : "GOTOFF"));
4306 return 0;
4307 }
4308
4309 if (S_IS_LOCAL (exp->X_add_symbol)
4310 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section)
4311 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
4312 exp->X_op = O_subtract;
4313 exp->X_op_symbol = GOT_symbol;
4314 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
4315 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
4316 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
4317 i.reloc[this_operand] = BFD_RELOC_64;
4318 else
4319 i.reloc[this_operand] = BFD_RELOC_32;
4320 }
4321
4322 if (exp->X_op == O_absent || exp->X_op == O_big)
4323 {
4324 /* Missing or bad expr becomes absolute 0. */
4325 as_bad (_("missing or invalid displacement expression `%s' taken as 0"),
4326 disp_start);
4327 exp->X_op = O_constant;
4328 exp->X_add_number = 0;
4329 exp->X_add_symbol = (symbolS *) 0;
4330 exp->X_op_symbol = (symbolS *) 0;
4331 }
4332
4333 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
4334 if (exp->X_op != O_constant
4335 && OUTPUT_FLAVOR == bfd_target_aout_flavour
4336 && exp_seg != absolute_section
4337 && exp_seg != text_section
4338 && exp_seg != data_section
4339 && exp_seg != bss_section
4340 && exp_seg != undefined_section
4341 && !bfd_is_com_section (exp_seg))
4342 {
4343 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
4344 return 0;
4345 }
4346 #endif
4347
4348 if (!(i.types[this_operand] & ~Disp))
4349 i.types[this_operand] &= types;
4350
4351 return 1;
4352 }
4353
4354 static int i386_index_check PARAMS ((const char *));
4355
4356 /* Make sure the memory operand we've been dealt is valid.
4357 Return 1 on success, 0 on a failure. */
4358
4359 static int
i386_index_check(operand_string)4360 i386_index_check (operand_string)
4361 const char *operand_string;
4362 {
4363 int ok;
4364 #if INFER_ADDR_PREFIX
4365 int fudged = 0;
4366
4367 tryprefix:
4368 #endif
4369 ok = 1;
4370 if ((current_templates->start->cpu_flags & CpuSVME)
4371 && current_templates->end[-1].operand_types[0] == AnyMem)
4372 {
4373 /* Memory operands of SVME insns are special in that they only allow
4374 rAX as their memory address and ignore any segment override. */
4375 unsigned RegXX;
4376
4377 /* SKINIT is even more restrictive: it always requires EAX. */
4378 if (strcmp (current_templates->start->name, "skinit") == 0)
4379 RegXX = Reg32;
4380 else if (flag_code == CODE_64BIT)
4381 RegXX = i.prefix[ADDR_PREFIX] == 0 ? Reg64 : Reg32;
4382 else
4383 RegXX = (flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0)
4384 ? Reg16
4385 : Reg32;
4386 if (!i.base_reg
4387 || !(i.base_reg->reg_type & Acc)
4388 || !(i.base_reg->reg_type & RegXX)
4389 || i.index_reg
4390 || (i.types[0] & Disp))
4391 ok = 0;
4392 }
4393 else if (flag_code == CODE_64BIT)
4394 {
4395 unsigned RegXX = (i.prefix[ADDR_PREFIX] == 0 ? Reg64 : Reg32);
4396
4397 if ((i.base_reg
4398 && ((i.base_reg->reg_type & RegXX) == 0)
4399 && (i.base_reg->reg_type != BaseIndex
4400 || i.index_reg))
4401 || (i.index_reg
4402 && ((i.index_reg->reg_type & (RegXX | BaseIndex))
4403 != (RegXX | BaseIndex))))
4404 ok = 0;
4405 }
4406 else
4407 {
4408 if ((flag_code == CODE_16BIT) ^ (i.prefix[ADDR_PREFIX] != 0))
4409 {
4410 /* 16bit checks. */
4411 if ((i.base_reg
4412 && ((i.base_reg->reg_type & (Reg16 | BaseIndex | RegRex))
4413 != (Reg16 | BaseIndex)))
4414 || (i.index_reg
4415 && (((i.index_reg->reg_type & (Reg16 | BaseIndex))
4416 != (Reg16 | BaseIndex))
4417 || !(i.base_reg
4418 && i.base_reg->reg_num < 6
4419 && i.index_reg->reg_num >= 6
4420 && i.log2_scale_factor == 0))))
4421 ok = 0;
4422 }
4423 else
4424 {
4425 /* 32bit checks. */
4426 if ((i.base_reg
4427 && (i.base_reg->reg_type & (Reg32 | RegRex)) != Reg32)
4428 || (i.index_reg
4429 && ((i.index_reg->reg_type & (Reg32 | BaseIndex | RegRex))
4430 != (Reg32 | BaseIndex))))
4431 ok = 0;
4432 }
4433 }
4434 if (!ok)
4435 {
4436 #if INFER_ADDR_PREFIX
4437 if (i.prefix[ADDR_PREFIX] == 0)
4438 {
4439 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
4440 i.prefixes += 1;
4441 /* Change the size of any displacement too. At most one of
4442 Disp16 or Disp32 is set.
4443 FIXME. There doesn't seem to be any real need for separate
4444 Disp16 and Disp32 flags. The same goes for Imm16 and Imm32.
4445 Removing them would probably clean up the code quite a lot. */
4446 if (flag_code != CODE_64BIT && (i.types[this_operand] & (Disp16 | Disp32)))
4447 i.types[this_operand] ^= (Disp16 | Disp32);
4448 fudged = 1;
4449 goto tryprefix;
4450 }
4451 if (fudged)
4452 as_bad (_("`%s' is not a valid base/index expression"),
4453 operand_string);
4454 else
4455 #endif
4456 as_bad (_("`%s' is not a valid %s bit base/index expression"),
4457 operand_string,
4458 flag_code_names[flag_code]);
4459 }
4460 return ok;
4461 }
4462
4463 /* Parse OPERAND_STRING into the i386_insn structure I. Returns non-zero
4464 on error. */
4465
4466 static int
i386_operand(operand_string)4467 i386_operand (operand_string)
4468 char *operand_string;
4469 {
4470 const reg_entry *r;
4471 char *end_op;
4472 char *op_string = operand_string;
4473
4474 if (is_space_char (*op_string))
4475 ++op_string;
4476
4477 /* We check for an absolute prefix (differentiating,
4478 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
4479 if (*op_string == ABSOLUTE_PREFIX)
4480 {
4481 ++op_string;
4482 if (is_space_char (*op_string))
4483 ++op_string;
4484 i.types[this_operand] |= JumpAbsolute;
4485 }
4486
4487 /* Check if operand is a register. */
4488 if ((r = parse_register (op_string, &end_op)) != NULL)
4489 {
4490 /* Check for a segment override by searching for ':' after a
4491 segment register. */
4492 op_string = end_op;
4493 if (is_space_char (*op_string))
4494 ++op_string;
4495 if (*op_string == ':' && (r->reg_type & (SReg2 | SReg3)))
4496 {
4497 switch (r->reg_num)
4498 {
4499 case 0:
4500 i.seg[i.mem_operands] = &es;
4501 break;
4502 case 1:
4503 i.seg[i.mem_operands] = &cs;
4504 break;
4505 case 2:
4506 i.seg[i.mem_operands] = &ss;
4507 break;
4508 case 3:
4509 i.seg[i.mem_operands] = &ds;
4510 break;
4511 case 4:
4512 i.seg[i.mem_operands] = &fs;
4513 break;
4514 case 5:
4515 i.seg[i.mem_operands] = &gs;
4516 break;
4517 }
4518
4519 /* Skip the ':' and whitespace. */
4520 ++op_string;
4521 if (is_space_char (*op_string))
4522 ++op_string;
4523
4524 if (!is_digit_char (*op_string)
4525 && !is_identifier_char (*op_string)
4526 && *op_string != '('
4527 && *op_string != ABSOLUTE_PREFIX)
4528 {
4529 as_bad (_("bad memory operand `%s'"), op_string);
4530 return 0;
4531 }
4532 /* Handle case of %es:*foo. */
4533 if (*op_string == ABSOLUTE_PREFIX)
4534 {
4535 ++op_string;
4536 if (is_space_char (*op_string))
4537 ++op_string;
4538 i.types[this_operand] |= JumpAbsolute;
4539 }
4540 goto do_memory_reference;
4541 }
4542 if (*op_string)
4543 {
4544 as_bad (_("junk `%s' after register"), op_string);
4545 return 0;
4546 }
4547 i.types[this_operand] |= r->reg_type & ~BaseIndex;
4548 i.op[this_operand].regs = r;
4549 i.reg_operands++;
4550 }
4551 else if (*op_string == REGISTER_PREFIX)
4552 {
4553 as_bad (_("bad register name `%s'"), op_string);
4554 return 0;
4555 }
4556 else if (*op_string == IMMEDIATE_PREFIX)
4557 {
4558 ++op_string;
4559 if (i.types[this_operand] & JumpAbsolute)
4560 {
4561 as_bad (_("immediate operand illegal with absolute jump"));
4562 return 0;
4563 }
4564 if (!i386_immediate (op_string))
4565 return 0;
4566 }
4567 else if (is_digit_char (*op_string)
4568 || is_identifier_char (*op_string)
4569 || *op_string == '(')
4570 {
4571 /* This is a memory reference of some sort. */
4572 char *base_string;
4573
4574 /* Start and end of displacement string expression (if found). */
4575 char *displacement_string_start;
4576 char *displacement_string_end;
4577
4578 do_memory_reference:
4579 if ((i.mem_operands == 1
4580 && (current_templates->start->opcode_modifier & IsString) == 0)
4581 || i.mem_operands == 2)
4582 {
4583 as_bad (_("too many memory references for `%s'"),
4584 current_templates->start->name);
4585 return 0;
4586 }
4587
4588 /* Check for base index form. We detect the base index form by
4589 looking for an ')' at the end of the operand, searching
4590 for the '(' matching it, and finding a REGISTER_PREFIX or ','
4591 after the '('. */
4592 base_string = op_string + strlen (op_string);
4593
4594 --base_string;
4595 if (is_space_char (*base_string))
4596 --base_string;
4597
4598 /* If we only have a displacement, set-up for it to be parsed later. */
4599 displacement_string_start = op_string;
4600 displacement_string_end = base_string + 1;
4601
4602 if (*base_string == ')')
4603 {
4604 char *temp_string;
4605 unsigned int parens_balanced = 1;
4606 /* We've already checked that the number of left & right ()'s are
4607 equal, so this loop will not be infinite. */
4608 do
4609 {
4610 base_string--;
4611 if (*base_string == ')')
4612 parens_balanced++;
4613 if (*base_string == '(')
4614 parens_balanced--;
4615 }
4616 while (parens_balanced);
4617
4618 temp_string = base_string;
4619
4620 /* Skip past '(' and whitespace. */
4621 ++base_string;
4622 if (is_space_char (*base_string))
4623 ++base_string;
4624
4625 if (*base_string == ','
4626 || ((i.base_reg = parse_register (base_string, &end_op)) != NULL))
4627 {
4628 displacement_string_end = temp_string;
4629
4630 i.types[this_operand] |= BaseIndex;
4631
4632 if (i.base_reg)
4633 {
4634 base_string = end_op;
4635 if (is_space_char (*base_string))
4636 ++base_string;
4637 }
4638
4639 /* There may be an index reg or scale factor here. */
4640 if (*base_string == ',')
4641 {
4642 ++base_string;
4643 if (is_space_char (*base_string))
4644 ++base_string;
4645
4646 if ((i.index_reg = parse_register (base_string, &end_op)) != NULL)
4647 {
4648 base_string = end_op;
4649 if (is_space_char (*base_string))
4650 ++base_string;
4651 if (*base_string == ',')
4652 {
4653 ++base_string;
4654 if (is_space_char (*base_string))
4655 ++base_string;
4656 }
4657 else if (*base_string != ')')
4658 {
4659 as_bad (_("expecting `,' or `)' after index register in `%s'"),
4660 operand_string);
4661 return 0;
4662 }
4663 }
4664 else if (*base_string == REGISTER_PREFIX)
4665 {
4666 as_bad (_("bad register name `%s'"), base_string);
4667 return 0;
4668 }
4669
4670 /* Check for scale factor. */
4671 if (*base_string != ')')
4672 {
4673 char *end_scale = i386_scale (base_string);
4674
4675 if (!end_scale)
4676 return 0;
4677
4678 base_string = end_scale;
4679 if (is_space_char (*base_string))
4680 ++base_string;
4681 if (*base_string != ')')
4682 {
4683 as_bad (_("expecting `)' after scale factor in `%s'"),
4684 operand_string);
4685 return 0;
4686 }
4687 }
4688 else if (!i.index_reg)
4689 {
4690 as_bad (_("expecting index register or scale factor after `,'; got '%c'"),
4691 *base_string);
4692 return 0;
4693 }
4694 }
4695 else if (*base_string != ')')
4696 {
4697 as_bad (_("expecting `,' or `)' after base register in `%s'"),
4698 operand_string);
4699 return 0;
4700 }
4701 }
4702 else if (*base_string == REGISTER_PREFIX)
4703 {
4704 as_bad (_("bad register name `%s'"), base_string);
4705 return 0;
4706 }
4707 }
4708
4709 /* If there's an expression beginning the operand, parse it,
4710 assuming displacement_string_start and
4711 displacement_string_end are meaningful. */
4712 if (displacement_string_start != displacement_string_end)
4713 {
4714 if (!i386_displacement (displacement_string_start,
4715 displacement_string_end))
4716 return 0;
4717 }
4718
4719 /* Special case for (%dx) while doing input/output op. */
4720 if (i.base_reg
4721 && i.base_reg->reg_type == (Reg16 | InOutPortReg)
4722 && i.index_reg == 0
4723 && i.log2_scale_factor == 0
4724 && i.seg[i.mem_operands] == 0
4725 && (i.types[this_operand] & Disp) == 0)
4726 {
4727 i.types[this_operand] = InOutPortReg;
4728 return 1;
4729 }
4730
4731 if (i386_index_check (operand_string) == 0)
4732 return 0;
4733 i.mem_operands++;
4734 }
4735 else
4736 {
4737 /* It's not a memory operand; argh! */
4738 as_bad (_("invalid char %s beginning operand %d `%s'"),
4739 output_invalid (*op_string),
4740 this_operand + 1,
4741 op_string);
4742 return 0;
4743 }
4744 return 1; /* Normal return. */
4745 }
4746
4747 /* md_estimate_size_before_relax()
4748
4749 Called just before relax() for rs_machine_dependent frags. The x86
4750 assembler uses these frags to handle variable size jump
4751 instructions.
4752
4753 Any symbol that is now undefined will not become defined.
4754 Return the correct fr_subtype in the frag.
4755 Return the initial "guess for variable size of frag" to caller.
4756 The guess is actually the growth beyond the fixed part. Whatever
4757 we do to grow the fixed or variable part contributes to our
4758 returned value. */
4759
4760 int
md_estimate_size_before_relax(fragP,segment)4761 md_estimate_size_before_relax (fragP, segment)
4762 fragS *fragP;
4763 segT segment;
4764 {
4765 /* We've already got fragP->fr_subtype right; all we have to do is
4766 check for un-relaxable symbols. On an ELF system, we can't relax
4767 an externally visible symbol, because it may be overridden by a
4768 shared library. */
4769 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
4770 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
4771 || (IS_ELF
4772 && (S_IS_EXTERNAL (fragP->fr_symbol)
4773 || S_IS_WEAK (fragP->fr_symbol)))
4774 #endif
4775 )
4776 {
4777 /* Symbol is undefined in this segment, or we need to keep a
4778 reloc so that weak symbols can be overridden. */
4779 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
4780 enum bfd_reloc_code_real reloc_type;
4781 unsigned char *opcode;
4782 int old_fr_fix;
4783
4784 if (fragP->fr_var != NO_RELOC)
4785 reloc_type = fragP->fr_var;
4786 else if (size == 2)
4787 reloc_type = BFD_RELOC_16_PCREL;
4788 else
4789 reloc_type = BFD_RELOC_32_PCREL;
4790
4791 old_fr_fix = fragP->fr_fix;
4792 opcode = (unsigned char *) fragP->fr_opcode;
4793
4794 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
4795 {
4796 case UNCOND_JUMP:
4797 /* Make jmp (0xeb) a (d)word displacement jump. */
4798 opcode[0] = 0xe9;
4799 fragP->fr_fix += size;
4800 fix_new (fragP, old_fr_fix, size,
4801 fragP->fr_symbol,
4802 fragP->fr_offset, 1,
4803 reloc_type);
4804 break;
4805
4806 case COND_JUMP86:
4807 if (size == 2
4808 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
4809 {
4810 /* Negate the condition, and branch past an
4811 unconditional jump. */
4812 opcode[0] ^= 1;
4813 opcode[1] = 3;
4814 /* Insert an unconditional jump. */
4815 opcode[2] = 0xe9;
4816 /* We added two extra opcode bytes, and have a two byte
4817 offset. */
4818 fragP->fr_fix += 2 + 2;
4819 fix_new (fragP, old_fr_fix + 2, 2,
4820 fragP->fr_symbol,
4821 fragP->fr_offset, 1,
4822 reloc_type);
4823 break;
4824 }
4825 /* Fall through. */
4826
4827 case COND_JUMP:
4828 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
4829 {
4830 fixS *fixP;
4831
4832 fragP->fr_fix += 1;
4833 fixP = fix_new (fragP, old_fr_fix, 1,
4834 fragP->fr_symbol,
4835 fragP->fr_offset, 1,
4836 BFD_RELOC_8_PCREL);
4837 fixP->fx_signed = 1;
4838 break;
4839 }
4840
4841 /* This changes the byte-displacement jump 0x7N
4842 to the (d)word-displacement jump 0x0f,0x8N. */
4843 opcode[1] = opcode[0] + 0x10;
4844 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4845 /* We've added an opcode byte. */
4846 fragP->fr_fix += 1 + size;
4847 fix_new (fragP, old_fr_fix + 1, size,
4848 fragP->fr_symbol,
4849 fragP->fr_offset, 1,
4850 reloc_type);
4851 break;
4852
4853 default:
4854 BAD_CASE (fragP->fr_subtype);
4855 break;
4856 }
4857 frag_wane (fragP);
4858 return fragP->fr_fix - old_fr_fix;
4859 }
4860
4861 /* Guess size depending on current relax state. Initially the relax
4862 state will correspond to a short jump and we return 1, because
4863 the variable part of the frag (the branch offset) is one byte
4864 long. However, we can relax a section more than once and in that
4865 case we must either set fr_subtype back to the unrelaxed state,
4866 or return the value for the appropriate branch. */
4867 return md_relax_table[fragP->fr_subtype].rlx_length;
4868 }
4869
4870 /* Called after relax() is finished.
4871
4872 In: Address of frag.
4873 fr_type == rs_machine_dependent.
4874 fr_subtype is what the address relaxed to.
4875
4876 Out: Any fixSs and constants are set up.
4877 Caller will turn frag into a ".space 0". */
4878
4879 void
md_convert_frag(abfd,sec,fragP)4880 md_convert_frag (abfd, sec, fragP)
4881 bfd *abfd ATTRIBUTE_UNUSED;
4882 segT sec ATTRIBUTE_UNUSED;
4883 fragS *fragP;
4884 {
4885 unsigned char *opcode;
4886 unsigned char *where_to_put_displacement = NULL;
4887 offsetT target_address;
4888 offsetT opcode_address;
4889 unsigned int extension = 0;
4890 offsetT displacement_from_opcode_start;
4891
4892 opcode = (unsigned char *) fragP->fr_opcode;
4893
4894 /* Address we want to reach in file space. */
4895 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
4896
4897 /* Address opcode resides at in file space. */
4898 opcode_address = fragP->fr_address + fragP->fr_fix;
4899
4900 /* Displacement from opcode start to fill into instruction. */
4901 displacement_from_opcode_start = target_address - opcode_address;
4902
4903 if ((fragP->fr_subtype & BIG) == 0)
4904 {
4905 /* Don't have to change opcode. */
4906 extension = 1; /* 1 opcode + 1 displacement */
4907 where_to_put_displacement = &opcode[1];
4908 }
4909 else
4910 {
4911 if (no_cond_jump_promotion
4912 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4913 as_warn_where (fragP->fr_file, fragP->fr_line, _("long jump required"));
4914
4915 switch (fragP->fr_subtype)
4916 {
4917 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
4918 extension = 4; /* 1 opcode + 4 displacement */
4919 opcode[0] = 0xe9;
4920 where_to_put_displacement = &opcode[1];
4921 break;
4922
4923 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
4924 extension = 2; /* 1 opcode + 2 displacement */
4925 opcode[0] = 0xe9;
4926 where_to_put_displacement = &opcode[1];
4927 break;
4928
4929 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
4930 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
4931 extension = 5; /* 2 opcode + 4 displacement */
4932 opcode[1] = opcode[0] + 0x10;
4933 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4934 where_to_put_displacement = &opcode[2];
4935 break;
4936
4937 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
4938 extension = 3; /* 2 opcode + 2 displacement */
4939 opcode[1] = opcode[0] + 0x10;
4940 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
4941 where_to_put_displacement = &opcode[2];
4942 break;
4943
4944 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
4945 extension = 4;
4946 opcode[0] ^= 1;
4947 opcode[1] = 3;
4948 opcode[2] = 0xe9;
4949 where_to_put_displacement = &opcode[3];
4950 break;
4951
4952 default:
4953 BAD_CASE (fragP->fr_subtype);
4954 break;
4955 }
4956 }
4957
4958 /* If size if less then four we are sure that the operand fits,
4959 but if it's 4, then it could be that the displacement is larger
4960 then -/+ 2GB. */
4961 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
4962 && object_64bit
4963 && ((addressT) (displacement_from_opcode_start - extension
4964 + ((addressT) 1 << 31))
4965 > (((addressT) 2 << 31) - 1)))
4966 {
4967 as_bad_where (fragP->fr_file, fragP->fr_line,
4968 _("jump target out of range"));
4969 /* Make us emit 0. */
4970 displacement_from_opcode_start = extension;
4971 }
4972 /* Now put displacement after opcode. */
4973 md_number_to_chars ((char *) where_to_put_displacement,
4974 (valueT) (displacement_from_opcode_start - extension),
4975 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
4976 fragP->fr_fix += extension;
4977 }
4978
4979 /* Size of byte displacement jmp. */
4980 int md_short_jump_size = 2;
4981
4982 /* Size of dword displacement jmp. */
4983 int md_long_jump_size = 5;
4984
4985 void
md_create_short_jump(ptr,from_addr,to_addr,frag,to_symbol)4986 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
4987 char *ptr;
4988 addressT from_addr, to_addr;
4989 fragS *frag ATTRIBUTE_UNUSED;
4990 symbolS *to_symbol ATTRIBUTE_UNUSED;
4991 {
4992 offsetT offset;
4993
4994 offset = to_addr - (from_addr + 2);
4995 /* Opcode for byte-disp jump. */
4996 md_number_to_chars (ptr, (valueT) 0xeb, 1);
4997 md_number_to_chars (ptr + 1, (valueT) offset, 1);
4998 }
4999
5000 void
md_create_long_jump(ptr,from_addr,to_addr,frag,to_symbol)5001 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
5002 char *ptr;
5003 addressT from_addr, to_addr;
5004 fragS *frag ATTRIBUTE_UNUSED;
5005 symbolS *to_symbol ATTRIBUTE_UNUSED;
5006 {
5007 offsetT offset;
5008
5009 offset = to_addr - (from_addr + 5);
5010 md_number_to_chars (ptr, (valueT) 0xe9, 1);
5011 md_number_to_chars (ptr + 1, (valueT) offset, 4);
5012 }
5013
5014 /* Apply a fixup (fixS) to segment data, once it has been determined
5015 by our caller that we have all the info we need to fix it up.
5016
5017 On the 386, immediates, displacements, and data pointers are all in
5018 the same (little-endian) format, so we don't need to care about which
5019 we are handling. */
5020
5021 void
md_apply_fix(fixP,valP,seg)5022 md_apply_fix (fixP, valP, seg)
5023 /* The fix we're to put in. */
5024 fixS *fixP;
5025 /* Pointer to the value of the bits. */
5026 valueT *valP;
5027 /* Segment fix is from. */
5028 segT seg ATTRIBUTE_UNUSED;
5029 {
5030 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
5031 valueT value = *valP;
5032
5033 #if !defined (TE_Mach)
5034 if (fixP->fx_pcrel)
5035 {
5036 switch (fixP->fx_r_type)
5037 {
5038 default:
5039 break;
5040
5041 case BFD_RELOC_64:
5042 fixP->fx_r_type = BFD_RELOC_64_PCREL;
5043 break;
5044 case BFD_RELOC_32:
5045 case BFD_RELOC_X86_64_32S:
5046 fixP->fx_r_type = BFD_RELOC_32_PCREL;
5047 break;
5048 case BFD_RELOC_16:
5049 fixP->fx_r_type = BFD_RELOC_16_PCREL;
5050 break;
5051 case BFD_RELOC_8:
5052 fixP->fx_r_type = BFD_RELOC_8_PCREL;
5053 break;
5054 }
5055 }
5056
5057 if (fixP->fx_addsy != NULL
5058 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
5059 || fixP->fx_r_type == BFD_RELOC_64_PCREL
5060 || fixP->fx_r_type == BFD_RELOC_16_PCREL
5061 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
5062 && !use_rela_relocations)
5063 {
5064 /* This is a hack. There should be a better way to handle this.
5065 This covers for the fact that bfd_install_relocation will
5066 subtract the current location (for partial_inplace, PC relative
5067 relocations); see more below. */
5068 #ifndef OBJ_AOUT
5069 if (IS_ELF
5070 #ifdef TE_PE
5071 || OUTPUT_FLAVOR == bfd_target_coff_flavour
5072 #endif
5073 )
5074 value += fixP->fx_where + fixP->fx_frag->fr_address;
5075 #endif
5076 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5077 if (IS_ELF)
5078 {
5079 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
5080
5081 if ((sym_seg == seg
5082 || (symbol_section_p (fixP->fx_addsy)
5083 && sym_seg != absolute_section))
5084 && !generic_force_reloc (fixP))
5085 {
5086 /* Yes, we add the values in twice. This is because
5087 bfd_install_relocation subtracts them out again. I think
5088 bfd_install_relocation is broken, but I don't dare change
5089 it. FIXME. */
5090 value += fixP->fx_where + fixP->fx_frag->fr_address;
5091 }
5092 }
5093 #endif
5094 #if defined (OBJ_COFF) && defined (TE_PE)
5095 /* For some reason, the PE format does not store a
5096 section address offset for a PC relative symbol. */
5097 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
5098 || S_IS_WEAK (fixP->fx_addsy))
5099 value += md_pcrel_from (fixP);
5100 #endif
5101 }
5102
5103 /* Fix a few things - the dynamic linker expects certain values here,
5104 and we must not disappoint it. */
5105 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5106 if (IS_ELF && fixP->fx_addsy)
5107 switch (fixP->fx_r_type)
5108 {
5109 case BFD_RELOC_386_PLT32:
5110 case BFD_RELOC_X86_64_PLT32:
5111 /* Make the jump instruction point to the address of the operand. At
5112 runtime we merely add the offset to the actual PLT entry. */
5113 value = -4;
5114 break;
5115
5116 case BFD_RELOC_386_TLS_GD:
5117 case BFD_RELOC_386_TLS_LDM:
5118 case BFD_RELOC_386_TLS_IE_32:
5119 case BFD_RELOC_386_TLS_IE:
5120 case BFD_RELOC_386_TLS_GOTIE:
5121 case BFD_RELOC_386_TLS_GOTDESC:
5122 case BFD_RELOC_X86_64_TLSGD:
5123 case BFD_RELOC_X86_64_TLSLD:
5124 case BFD_RELOC_X86_64_GOTTPOFF:
5125 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
5126 value = 0; /* Fully resolved at runtime. No addend. */
5127 /* Fallthrough */
5128 case BFD_RELOC_386_TLS_LE:
5129 case BFD_RELOC_386_TLS_LDO_32:
5130 case BFD_RELOC_386_TLS_LE_32:
5131 case BFD_RELOC_X86_64_DTPOFF32:
5132 case BFD_RELOC_X86_64_DTPOFF64:
5133 case BFD_RELOC_X86_64_TPOFF32:
5134 case BFD_RELOC_X86_64_TPOFF64:
5135 S_SET_THREAD_LOCAL (fixP->fx_addsy);
5136 break;
5137
5138 case BFD_RELOC_386_TLS_DESC_CALL:
5139 case BFD_RELOC_X86_64_TLSDESC_CALL:
5140 value = 0; /* Fully resolved at runtime. No addend. */
5141 S_SET_THREAD_LOCAL (fixP->fx_addsy);
5142 fixP->fx_done = 0;
5143 return;
5144
5145 case BFD_RELOC_386_GOT32:
5146 case BFD_RELOC_386_GOT32X:
5147 case BFD_RELOC_X86_64_GOT32:
5148 value = 0; /* Fully resolved at runtime. No addend. */
5149 break;
5150
5151 case BFD_RELOC_VTABLE_INHERIT:
5152 case BFD_RELOC_VTABLE_ENTRY:
5153 fixP->fx_done = 0;
5154 return;
5155
5156 default:
5157 break;
5158 }
5159 #endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
5160 *valP = value;
5161 #endif /* !defined (TE_Mach) */
5162
5163 /* Are we finished with this relocation now? */
5164 if (fixP->fx_addsy == NULL)
5165 fixP->fx_done = 1;
5166 else if (use_rela_relocations)
5167 {
5168 fixP->fx_no_overflow = 1;
5169 /* Remember value for tc_gen_reloc. */
5170 fixP->fx_addnumber = value;
5171 value = 0;
5172 }
5173
5174 md_number_to_chars (p, value, fixP->fx_size);
5175 }
5176
5177 #define MAX_LITTLENUMS 6
5178
5179 /* Turn the string pointed to by litP into a floating point constant
5180 of type TYPE, and emit the appropriate bytes. The number of
5181 LITTLENUMS emitted is stored in *SIZEP. An error message is
5182 returned, or NULL on OK. */
5183
5184 char *
md_atof(type,litP,sizeP)5185 md_atof (type, litP, sizeP)
5186 int type;
5187 char *litP;
5188 int *sizeP;
5189 {
5190 int prec;
5191 LITTLENUM_TYPE words[MAX_LITTLENUMS];
5192 LITTLENUM_TYPE *wordP;
5193 char *t;
5194
5195 switch (type)
5196 {
5197 case 'f':
5198 case 'F':
5199 prec = 2;
5200 break;
5201
5202 case 'd':
5203 case 'D':
5204 prec = 4;
5205 break;
5206
5207 case 'x':
5208 case 'X':
5209 prec = 5;
5210 break;
5211
5212 default:
5213 *sizeP = 0;
5214 return _("Bad call to md_atof ()");
5215 }
5216 t = atof_ieee (input_line_pointer, type, words);
5217 if (t)
5218 input_line_pointer = t;
5219
5220 *sizeP = prec * sizeof (LITTLENUM_TYPE);
5221 /* This loops outputs the LITTLENUMs in REVERSE order; in accord with
5222 the bigendian 386. */
5223 for (wordP = words + prec - 1; prec--;)
5224 {
5225 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
5226 litP += sizeof (LITTLENUM_TYPE);
5227 }
5228 return 0;
5229 }
5230
5231 static char output_invalid_buf[8];
5232
5233 static char *
output_invalid(c)5234 output_invalid (c)
5235 int c;
5236 {
5237 if (ISPRINT (c))
5238 sprintf (output_invalid_buf, "'%c'", c);
5239 else
5240 sprintf (output_invalid_buf, "(0x%x)", (unsigned) c);
5241 return output_invalid_buf;
5242 }
5243
5244 /* REG_STRING starts *before* REGISTER_PREFIX. */
5245
5246 static const reg_entry *
parse_real_register(char * reg_string,char ** end_op)5247 parse_real_register (char *reg_string, char **end_op)
5248 {
5249 char *s = reg_string;
5250 char *p;
5251 char reg_name_given[MAX_REG_NAME_SIZE + 1];
5252 const reg_entry *r;
5253
5254 /* Skip possible REGISTER_PREFIX and possible whitespace. */
5255 if (*s == REGISTER_PREFIX)
5256 ++s;
5257
5258 if (is_space_char (*s))
5259 ++s;
5260
5261 p = reg_name_given;
5262 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
5263 {
5264 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
5265 return (const reg_entry *) NULL;
5266 s++;
5267 }
5268
5269 /* For naked regs, make sure that we are not dealing with an identifier.
5270 This prevents confusing an identifier like `eax_var' with register
5271 `eax'. */
5272 if (allow_naked_reg && identifier_chars[(unsigned char) *s])
5273 return (const reg_entry *) NULL;
5274
5275 *end_op = s;
5276
5277 r = (const reg_entry *) hash_find (reg_hash, reg_name_given);
5278
5279 /* Handle floating point regs, allowing spaces in the (i) part. */
5280 if (r == i386_regtab /* %st is first entry of table */)
5281 {
5282 if (is_space_char (*s))
5283 ++s;
5284 if (*s == '(')
5285 {
5286 ++s;
5287 if (is_space_char (*s))
5288 ++s;
5289 if (*s >= '0' && *s <= '7')
5290 {
5291 r = &i386_float_regtab[*s - '0'];
5292 ++s;
5293 if (is_space_char (*s))
5294 ++s;
5295 if (*s == ')')
5296 {
5297 *end_op = s + 1;
5298 return r;
5299 }
5300 }
5301 /* We have "%st(" then garbage. */
5302 return (const reg_entry *) NULL;
5303 }
5304 }
5305
5306 if (r != NULL
5307 && ((r->reg_flags & (RegRex64 | RegRex)) | (r->reg_type & Reg64)) != 0
5308 && (r->reg_type != Control || !(cpu_arch_flags & CpuSledgehammer))
5309 && flag_code != CODE_64BIT)
5310 return (const reg_entry *) NULL;
5311
5312 return r;
5313 }
5314
5315 /* REG_STRING starts *before* REGISTER_PREFIX. */
5316
5317 static const reg_entry *
parse_register(char * reg_string,char ** end_op)5318 parse_register (char *reg_string, char **end_op)
5319 {
5320 const reg_entry *r;
5321
5322 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
5323 r = parse_real_register (reg_string, end_op);
5324 else
5325 r = NULL;
5326 if (!r)
5327 {
5328 char *save = input_line_pointer;
5329 char c;
5330 symbolS *symbolP;
5331
5332 input_line_pointer = reg_string;
5333 c = get_symbol_end ();
5334 symbolP = symbol_find (reg_string);
5335 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
5336 {
5337 const expressionS *e = symbol_get_value_expression (symbolP);
5338
5339 know (e->X_op == O_register);
5340 know (e->X_add_number >= 0 && (valueT) e->X_add_number < ARRAY_SIZE (i386_regtab));
5341 r = i386_regtab + e->X_add_number;
5342 *end_op = input_line_pointer;
5343 }
5344 *input_line_pointer = c;
5345 input_line_pointer = save;
5346 }
5347 return r;
5348 }
5349
5350 int
i386_parse_name(char * name,expressionS * e,char * nextcharP)5351 i386_parse_name (char *name, expressionS *e, char *nextcharP)
5352 {
5353 const reg_entry *r;
5354 char *end = input_line_pointer;
5355
5356 *end = *nextcharP;
5357 r = parse_register (name, &input_line_pointer);
5358 if (r && end <= input_line_pointer)
5359 {
5360 *nextcharP = *input_line_pointer;
5361 *input_line_pointer = 0;
5362 e->X_op = O_register;
5363 e->X_add_number = r - i386_regtab;
5364 return 1;
5365 }
5366 input_line_pointer = end;
5367 *end = 0;
5368 return 0;
5369 }
5370
5371 void
md_operand(expressionS * e)5372 md_operand (expressionS *e)
5373 {
5374 if (*input_line_pointer == REGISTER_PREFIX)
5375 {
5376 char *end;
5377 const reg_entry *r = parse_real_register (input_line_pointer, &end);
5378
5379 if (r)
5380 {
5381 e->X_op = O_register;
5382 e->X_add_number = r - i386_regtab;
5383 input_line_pointer = end;
5384 }
5385 }
5386 }
5387
5388
5389 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5390 const char *md_shortopts = "kVQ:sqn";
5391 #else
5392 const char *md_shortopts = "qn";
5393 #endif
5394
5395 #define OPTION_32 (OPTION_MD_BASE + 0)
5396 #define OPTION_64 (OPTION_MD_BASE + 1)
5397 #define OPTION_DIVIDE (OPTION_MD_BASE + 2)
5398
5399 struct option md_longopts[] = {
5400 {"32", no_argument, NULL, OPTION_32},
5401 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5402 {"64", no_argument, NULL, OPTION_64},
5403 #endif
5404 {"divide", no_argument, NULL, OPTION_DIVIDE},
5405 {NULL, no_argument, NULL, 0}
5406 };
5407 size_t md_longopts_size = sizeof (md_longopts);
5408
5409 int
md_parse_option(c,arg)5410 md_parse_option (c, arg)
5411 int c;
5412 char *arg ATTRIBUTE_UNUSED;
5413 {
5414 switch (c)
5415 {
5416 case 'n':
5417 optimize_align_code = 0;
5418 break;
5419
5420 case 'q':
5421 quiet_warnings = 1;
5422 break;
5423
5424 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5425 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
5426 should be emitted or not. FIXME: Not implemented. */
5427 case 'Q':
5428 break;
5429
5430 /* -V: SVR4 argument to print version ID. */
5431 case 'V':
5432 print_version_id ();
5433 break;
5434
5435 /* -k: Ignore for FreeBSD compatibility. */
5436 case 'k':
5437 break;
5438
5439 case 's':
5440 /* -s: On i386 Solaris, this tells the native assembler to use
5441 .stab instead of .stab.excl. We always use .stab anyhow. */
5442 break;
5443
5444 case OPTION_64:
5445 {
5446 const char **list, **l;
5447
5448 list = bfd_target_list ();
5449 for (l = list; *l != NULL; l++)
5450 if (strcmp (*l, "elf64-x86-64") == 0)
5451 {
5452 default_arch = "x86_64";
5453 break;
5454 }
5455 if (*l == NULL)
5456 as_fatal (_("No compiled in support for x86_64"));
5457 free (list);
5458 }
5459 break;
5460 #endif
5461
5462 case OPTION_32:
5463 default_arch = "i386";
5464 break;
5465
5466 case OPTION_DIVIDE:
5467 #ifdef SVR4_COMMENT_CHARS
5468 {
5469 char *n, *t;
5470 const char *s;
5471
5472 n = (char *) xmalloc (strlen (i386_comment_chars) + 1);
5473 t = n;
5474 for (s = i386_comment_chars; *s != '\0'; s++)
5475 if (*s != '/')
5476 *t++ = *s;
5477 *t = '\0';
5478 i386_comment_chars = n;
5479 }
5480 #endif
5481 break;
5482
5483 default:
5484 return 0;
5485 }
5486 return 1;
5487 }
5488
5489 void
md_show_usage(stream)5490 md_show_usage (stream)
5491 FILE *stream;
5492 {
5493 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5494 fprintf (stream, _("\
5495 -Q ignored\n\
5496 -V print assembler version number\n\
5497 -k ignored\n"));
5498 #endif
5499 fprintf (stream, _("\
5500 -n Do not optimize code alignment\n\
5501 -q quieten some warnings\n"));
5502 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5503 fprintf (stream, _("\
5504 -s ignored\n"));
5505 #endif
5506 #ifdef SVR4_COMMENT_CHARS
5507 fprintf (stream, _("\
5508 --divide do not treat `/' as a comment character\n"));
5509 #else
5510 fprintf (stream, _("\
5511 --divide ignored\n"));
5512 #endif
5513 }
5514
5515 #if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
5516 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
5517
5518 /* Pick the target format to use. */
5519
5520 const char *
i386_target_format()5521 i386_target_format ()
5522 {
5523 if (!strcmp (default_arch, "x86_64"))
5524 set_code_flag (CODE_64BIT);
5525 else if (!strcmp (default_arch, "i386"))
5526 set_code_flag (CODE_32BIT);
5527 else
5528 as_fatal (_("Unknown architecture"));
5529 switch (OUTPUT_FLAVOR)
5530 {
5531 #ifdef OBJ_MAYBE_AOUT
5532 case bfd_target_aout_flavour:
5533 return AOUT_TARGET_FORMAT;
5534 #endif
5535 #ifdef OBJ_MAYBE_COFF
5536 case bfd_target_coff_flavour:
5537 return "coff-i386";
5538 #endif
5539 #if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
5540 case bfd_target_elf_flavour:
5541 {
5542 if (flag_code == CODE_64BIT)
5543 {
5544 object_64bit = 1;
5545 use_rela_relocations = 1;
5546 }
5547 return flag_code == CODE_64BIT ? "elf64-x86-64" : ELF_TARGET_FORMAT;
5548 }
5549 #endif
5550 default:
5551 abort ();
5552 return NULL;
5553 }
5554 }
5555
5556 #endif /* OBJ_MAYBE_ more than one */
5557
5558 #if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF))
i386_elf_emit_arch_note()5559 void i386_elf_emit_arch_note ()
5560 {
5561 if (IS_ELF && cpu_arch_name != NULL)
5562 {
5563 char *p;
5564 asection *seg = now_seg;
5565 subsegT subseg = now_subseg;
5566 Elf_Internal_Note i_note;
5567 Elf_External_Note e_note;
5568 asection *note_secp;
5569 int len;
5570
5571 /* Create the .note section. */
5572 note_secp = subseg_new (".note", 0);
5573 bfd_set_section_flags (stdoutput,
5574 note_secp,
5575 SEC_HAS_CONTENTS | SEC_READONLY);
5576
5577 /* Process the arch string. */
5578 len = strlen (cpu_arch_name);
5579
5580 i_note.namesz = len + 1;
5581 i_note.descsz = 0;
5582 i_note.type = NT_ARCH;
5583 p = frag_more (sizeof (e_note.namesz));
5584 md_number_to_chars (p, (valueT) i_note.namesz, sizeof (e_note.namesz));
5585 p = frag_more (sizeof (e_note.descsz));
5586 md_number_to_chars (p, (valueT) i_note.descsz, sizeof (e_note.descsz));
5587 p = frag_more (sizeof (e_note.type));
5588 md_number_to_chars (p, (valueT) i_note.type, sizeof (e_note.type));
5589 p = frag_more (len + 1);
5590 strcpy (p, cpu_arch_name);
5591
5592 frag_align (2, 0, 0);
5593
5594 subseg_set (seg, subseg);
5595 }
5596 }
5597 #endif
5598
5599 symbolS *
md_undefined_symbol(name)5600 md_undefined_symbol (name)
5601 char *name;
5602 {
5603 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
5604 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
5605 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
5606 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
5607 {
5608 if (!GOT_symbol)
5609 {
5610 if (symbol_find (name))
5611 as_bad (_("GOT already in symbol table"));
5612 GOT_symbol = symbol_new (name, undefined_section,
5613 (valueT) 0, &zero_address_frag);
5614 };
5615 return GOT_symbol;
5616 }
5617 return 0;
5618 }
5619
5620 /* Round up a section size to the appropriate boundary. */
5621
5622 valueT
md_section_align(segment,size)5623 md_section_align (segment, size)
5624 segT segment ATTRIBUTE_UNUSED;
5625 valueT size;
5626 {
5627 #if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
5628 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
5629 {
5630 /* For a.out, force the section size to be aligned. If we don't do
5631 this, BFD will align it for us, but it will not write out the
5632 final bytes of the section. This may be a bug in BFD, but it is
5633 easier to fix it here since that is how the other a.out targets
5634 work. */
5635 int align;
5636
5637 align = bfd_get_section_alignment (stdoutput, segment);
5638 size = ((size + (1 << align) - 1) & ((valueT) -1 << align));
5639 }
5640 #endif
5641
5642 return size;
5643 }
5644
5645 /* On the i386, PC-relative offsets are relative to the start of the
5646 next instruction. That is, the address of the offset, plus its
5647 size, since the offset is always the last part of the insn. */
5648
5649 long
md_pcrel_from(fixP)5650 md_pcrel_from (fixP)
5651 fixS *fixP;
5652 {
5653 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
5654 }
5655
5656 #ifndef I386COFF
5657
5658 static void
s_bss(ignore)5659 s_bss (ignore)
5660 int ignore ATTRIBUTE_UNUSED;
5661 {
5662 int temp;
5663
5664 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
5665 if (IS_ELF)
5666 obj_elf_section_change_hook ();
5667 #endif
5668 temp = get_absolute_expression ();
5669 subseg_set (bss_section, (subsegT) temp);
5670 demand_empty_rest_of_line ();
5671 }
5672
5673 #endif
5674
5675 void
i386_validate_fix(fixp)5676 i386_validate_fix (fixp)
5677 fixS *fixp;
5678 {
5679 if (fixp->fx_subsy && fixp->fx_subsy == GOT_symbol)
5680 {
5681 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
5682 {
5683 if (!object_64bit)
5684 abort ();
5685 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
5686 }
5687 else
5688 {
5689 if (!object_64bit)
5690 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
5691 else
5692 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
5693 }
5694 fixp->fx_subsy = 0;
5695 }
5696 }
5697
5698 arelent *
tc_gen_reloc(section,fixp)5699 tc_gen_reloc (section, fixp)
5700 asection *section ATTRIBUTE_UNUSED;
5701 fixS *fixp;
5702 {
5703 arelent *rel;
5704 bfd_reloc_code_real_type code;
5705
5706 switch (fixp->fx_r_type)
5707 {
5708 case BFD_RELOC_X86_64_PLT32:
5709 case BFD_RELOC_X86_64_GOT32:
5710 case BFD_RELOC_X86_64_GOTPCREL:
5711 case BFD_RELOC_386_PLT32:
5712 case BFD_RELOC_386_GOT32:
5713 case BFD_RELOC_386_GOT32X:
5714 case BFD_RELOC_386_GOTOFF:
5715 case BFD_RELOC_386_GOTPC:
5716 case BFD_RELOC_386_TLS_GD:
5717 case BFD_RELOC_386_TLS_LDM:
5718 case BFD_RELOC_386_TLS_LDO_32:
5719 case BFD_RELOC_386_TLS_IE_32:
5720 case BFD_RELOC_386_TLS_IE:
5721 case BFD_RELOC_386_TLS_GOTIE:
5722 case BFD_RELOC_386_TLS_LE_32:
5723 case BFD_RELOC_386_TLS_LE:
5724 case BFD_RELOC_386_TLS_GOTDESC:
5725 case BFD_RELOC_386_TLS_DESC_CALL:
5726 case BFD_RELOC_X86_64_TLSGD:
5727 case BFD_RELOC_X86_64_TLSLD:
5728 case BFD_RELOC_X86_64_DTPOFF32:
5729 case BFD_RELOC_X86_64_DTPOFF64:
5730 case BFD_RELOC_X86_64_GOTTPOFF:
5731 case BFD_RELOC_X86_64_TPOFF32:
5732 case BFD_RELOC_X86_64_TPOFF64:
5733 case BFD_RELOC_X86_64_GOTOFF64:
5734 case BFD_RELOC_X86_64_GOTPC32:
5735 case BFD_RELOC_X86_64_GOT64:
5736 case BFD_RELOC_X86_64_GOTPCREL64:
5737 case BFD_RELOC_X86_64_GOTPC64:
5738 case BFD_RELOC_X86_64_GOTPLT64:
5739 case BFD_RELOC_X86_64_PLTOFF64:
5740 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
5741 case BFD_RELOC_X86_64_TLSDESC_CALL:
5742 case BFD_RELOC_RVA:
5743 case BFD_RELOC_VTABLE_ENTRY:
5744 case BFD_RELOC_VTABLE_INHERIT:
5745 #ifdef TE_PE
5746 case BFD_RELOC_32_SECREL:
5747 #endif
5748 code = fixp->fx_r_type;
5749 break;
5750 case BFD_RELOC_X86_64_32S:
5751 if (!fixp->fx_pcrel)
5752 {
5753 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
5754 code = fixp->fx_r_type;
5755 break;
5756 }
5757 default:
5758 if (fixp->fx_pcrel)
5759 {
5760 switch (fixp->fx_size)
5761 {
5762 default:
5763 as_bad_where (fixp->fx_file, fixp->fx_line,
5764 _("can not do %d byte pc-relative relocation"),
5765 fixp->fx_size);
5766 code = BFD_RELOC_32_PCREL;
5767 break;
5768 case 1: code = BFD_RELOC_8_PCREL; break;
5769 case 2: code = BFD_RELOC_16_PCREL; break;
5770 case 4: code = BFD_RELOC_32_PCREL; break;
5771 #ifdef BFD64
5772 case 8: code = BFD_RELOC_64_PCREL; break;
5773 #endif
5774 }
5775 }
5776 else
5777 {
5778 switch (fixp->fx_size)
5779 {
5780 default:
5781 as_bad_where (fixp->fx_file, fixp->fx_line,
5782 _("can not do %d byte relocation"),
5783 fixp->fx_size);
5784 code = BFD_RELOC_32;
5785 break;
5786 case 1: code = BFD_RELOC_8; break;
5787 case 2: code = BFD_RELOC_16; break;
5788 case 4: code = BFD_RELOC_32; break;
5789 #ifdef BFD64
5790 case 8: code = BFD_RELOC_64; break;
5791 #endif
5792 }
5793 }
5794 break;
5795 }
5796
5797 if ((code == BFD_RELOC_32
5798 || code == BFD_RELOC_32_PCREL
5799 || code == BFD_RELOC_X86_64_32S)
5800 && GOT_symbol
5801 && fixp->fx_addsy == GOT_symbol)
5802 {
5803 if (!object_64bit)
5804 code = BFD_RELOC_386_GOTPC;
5805 else
5806 code = BFD_RELOC_X86_64_GOTPC32;
5807 }
5808 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
5809 && GOT_symbol
5810 && fixp->fx_addsy == GOT_symbol)
5811 {
5812 code = BFD_RELOC_X86_64_GOTPC64;
5813 }
5814
5815 rel = (arelent *) xmalloc (sizeof (arelent));
5816 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5817 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5818
5819 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
5820
5821 if (!use_rela_relocations)
5822 {
5823 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
5824 vtable entry to be used in the relocation's section offset. */
5825 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
5826 rel->address = fixp->fx_offset;
5827
5828 rel->addend = 0;
5829 }
5830 /* Use the rela in 64bit mode. */
5831 else
5832 {
5833 if (!fixp->fx_pcrel)
5834 rel->addend = fixp->fx_offset;
5835 else
5836 switch (code)
5837 {
5838 case BFD_RELOC_X86_64_PLT32:
5839 case BFD_RELOC_X86_64_GOT32:
5840 case BFD_RELOC_X86_64_GOTPCREL:
5841 case BFD_RELOC_X86_64_TLSGD:
5842 case BFD_RELOC_X86_64_TLSLD:
5843 case BFD_RELOC_X86_64_GOTTPOFF:
5844 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
5845 case BFD_RELOC_X86_64_TLSDESC_CALL:
5846 rel->addend = fixp->fx_offset - fixp->fx_size;
5847 break;
5848 default:
5849 rel->addend = (section->vma
5850 - fixp->fx_size
5851 + fixp->fx_addnumber
5852 + md_pcrel_from (fixp));
5853 break;
5854 }
5855 }
5856
5857 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
5858 if (rel->howto == NULL)
5859 {
5860 as_bad_where (fixp->fx_file, fixp->fx_line,
5861 _("cannot represent relocation type %s"),
5862 bfd_get_reloc_code_name (code));
5863 /* Set howto to a garbage value so that we can keep going. */
5864 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
5865 assert (rel->howto != NULL);
5866 }
5867
5868 return rel;
5869 }
5870
5871
5872 /* Parse operands using Intel syntax. This implements a recursive descent
5873 parser based on the BNF grammar published in Appendix B of the MASM 6.1
5874 Programmer's Guide.
5875
5876 FIXME: We do not recognize the full operand grammar defined in the MASM
5877 documentation. In particular, all the structure/union and
5878 high-level macro operands are missing.
5879
5880 Uppercase words are terminals, lower case words are non-terminals.
5881 Objects surrounded by double brackets '[[' ']]' are optional. Vertical
5882 bars '|' denote choices. Most grammar productions are implemented in
5883 functions called 'intel_<production>'.
5884
5885 Initial production is 'expr'.
5886
5887 addOp + | -
5888
5889 alpha [a-zA-Z]
5890
5891 binOp & | AND | \| | OR | ^ | XOR
5892
5893 byteRegister AL | AH | BL | BH | CL | CH | DL | DH
5894
5895 constant digits [[ radixOverride ]]
5896
5897 dataType BYTE | WORD | DWORD | FWORD | QWORD | TBYTE | OWORD | XMMWORD
5898
5899 digits decdigit
5900 | digits decdigit
5901 | digits hexdigit
5902
5903 decdigit [0-9]
5904
5905 e04 e04 addOp e05
5906 | e05
5907
5908 e05 e05 binOp e06
5909 | e06
5910
5911 e06 e06 mulOp e09
5912 | e09
5913
5914 e09 OFFSET e10
5915 | SHORT e10
5916 | + e10
5917 | - e10
5918 | ~ e10
5919 | NOT e10
5920 | e09 PTR e10
5921 | e09 : e10
5922 | e10
5923
5924 e10 e10 [ expr ]
5925 | e11
5926
5927 e11 ( expr )
5928 | [ expr ]
5929 | constant
5930 | dataType
5931 | id
5932 | $
5933 | register
5934
5935 => expr expr cmpOp e04
5936 | e04
5937
5938 gpRegister AX | EAX | BX | EBX | CX | ECX | DX | EDX
5939 | BP | EBP | SP | ESP | DI | EDI | SI | ESI
5940
5941 hexdigit a | b | c | d | e | f
5942 | A | B | C | D | E | F
5943
5944 id alpha
5945 | id alpha
5946 | id decdigit
5947
5948 mulOp * | / | % | MOD | << | SHL | >> | SHR
5949
5950 quote " | '
5951
5952 register specialRegister
5953 | gpRegister
5954 | byteRegister
5955
5956 segmentRegister CS | DS | ES | FS | GS | SS
5957
5958 specialRegister CR0 | CR2 | CR3 | CR4
5959 | DR0 | DR1 | DR2 | DR3 | DR6 | DR7
5960 | TR3 | TR4 | TR5 | TR6 | TR7
5961
5962 We simplify the grammar in obvious places (e.g., register parsing is
5963 done by calling parse_register) and eliminate immediate left recursion
5964 to implement a recursive-descent parser.
5965
5966 expr e04 expr'
5967
5968 expr' cmpOp e04 expr'
5969 | Empty
5970
5971 e04 e05 e04'
5972
5973 e04' addOp e05 e04'
5974 | Empty
5975
5976 e05 e06 e05'
5977
5978 e05' binOp e06 e05'
5979 | Empty
5980
5981 e06 e09 e06'
5982
5983 e06' mulOp e09 e06'
5984 | Empty
5985
5986 e09 OFFSET e10 e09'
5987 | SHORT e10'
5988 | + e10'
5989 | - e10'
5990 | ~ e10'
5991 | NOT e10'
5992 | e10 e09'
5993
5994 e09' PTR e10 e09'
5995 | : e10 e09'
5996 | Empty
5997
5998 e10 e11 e10'
5999
6000 e10' [ expr ] e10'
6001 | Empty
6002
6003 e11 ( expr )
6004 | [ expr ]
6005 | BYTE
6006 | WORD
6007 | DWORD
6008 | FWORD
6009 | QWORD
6010 | TBYTE
6011 | OWORD
6012 | XMMWORD
6013 | .
6014 | $
6015 | register
6016 | id
6017 | constant */
6018
6019 /* Parsing structure for the intel syntax parser. Used to implement the
6020 semantic actions for the operand grammar. */
6021 struct intel_parser_s
6022 {
6023 char *op_string; /* The string being parsed. */
6024 int got_a_float; /* Whether the operand is a float. */
6025 int op_modifier; /* Operand modifier. */
6026 int is_mem; /* 1 if operand is memory reference. */
6027 int in_offset; /* >=1 if parsing operand of offset. */
6028 int in_bracket; /* >=1 if parsing operand in brackets. */
6029 const reg_entry *reg; /* Last register reference found. */
6030 char *disp; /* Displacement string being built. */
6031 char *next_operand; /* Resume point when splitting operands. */
6032 };
6033
6034 static struct intel_parser_s intel_parser;
6035
6036 /* Token structure for parsing intel syntax. */
6037 struct intel_token
6038 {
6039 int code; /* Token code. */
6040 const reg_entry *reg; /* Register entry for register tokens. */
6041 char *str; /* String representation. */
6042 };
6043
6044 static struct intel_token cur_token, prev_token;
6045
6046 /* Token codes for the intel parser. Since T_SHORT is already used
6047 by COFF, undefine it first to prevent a warning. */
6048 #define T_NIL -1
6049 #define T_CONST 1
6050 #define T_REG 2
6051 #define T_BYTE 3
6052 #define T_WORD 4
6053 #define T_DWORD 5
6054 #define T_FWORD 6
6055 #define T_QWORD 7
6056 #define T_TBYTE 8
6057 #define T_XMMWORD 9
6058 #undef T_SHORT
6059 #define T_SHORT 10
6060 #define T_OFFSET 11
6061 #define T_PTR 12
6062 #define T_ID 13
6063 #define T_SHL 14
6064 #define T_SHR 15
6065
6066 /* Prototypes for intel parser functions. */
6067 static int intel_match_token PARAMS ((int code));
6068 static void intel_get_token PARAMS ((void));
6069 static void intel_putback_token PARAMS ((void));
6070 static int intel_expr PARAMS ((void));
6071 static int intel_e04 PARAMS ((void));
6072 static int intel_e05 PARAMS ((void));
6073 static int intel_e06 PARAMS ((void));
6074 static int intel_e09 PARAMS ((void));
6075 static int intel_bracket_expr PARAMS ((void));
6076 static int intel_e10 PARAMS ((void));
6077 static int intel_e11 PARAMS ((void));
6078
6079 static int
i386_intel_operand(operand_string,got_a_float)6080 i386_intel_operand (operand_string, got_a_float)
6081 char *operand_string;
6082 int got_a_float;
6083 {
6084 int ret;
6085 char *p;
6086
6087 p = intel_parser.op_string = xstrdup (operand_string);
6088 intel_parser.disp = (char *) xmalloc (strlen (operand_string) + 1);
6089
6090 for (;;)
6091 {
6092 /* Initialize token holders. */
6093 cur_token.code = prev_token.code = T_NIL;
6094 cur_token.reg = prev_token.reg = NULL;
6095 cur_token.str = prev_token.str = NULL;
6096
6097 /* Initialize parser structure. */
6098 intel_parser.got_a_float = got_a_float;
6099 intel_parser.op_modifier = 0;
6100 intel_parser.is_mem = 0;
6101 intel_parser.in_offset = 0;
6102 intel_parser.in_bracket = 0;
6103 intel_parser.reg = NULL;
6104 intel_parser.disp[0] = '\0';
6105 intel_parser.next_operand = NULL;
6106
6107 /* Read the first token and start the parser. */
6108 intel_get_token ();
6109 ret = intel_expr ();
6110
6111 if (!ret)
6112 break;
6113
6114 if (cur_token.code != T_NIL)
6115 {
6116 as_bad (_("invalid operand for '%s' ('%s' unexpected)"),
6117 current_templates->start->name, cur_token.str);
6118 ret = 0;
6119 }
6120 /* If we found a memory reference, hand it over to i386_displacement
6121 to fill in the rest of the operand fields. */
6122 else if (intel_parser.is_mem)
6123 {
6124 if ((i.mem_operands == 1
6125 && (current_templates->start->opcode_modifier & IsString) == 0)
6126 || i.mem_operands == 2)
6127 {
6128 as_bad (_("too many memory references for '%s'"),
6129 current_templates->start->name);
6130 ret = 0;
6131 }
6132 else
6133 {
6134 char *s = intel_parser.disp;
6135 i.mem_operands++;
6136
6137 if (!quiet_warnings && intel_parser.is_mem < 0)
6138 /* See the comments in intel_bracket_expr. */
6139 as_warn (_("Treating `%s' as memory reference"), operand_string);
6140
6141 /* Add the displacement expression. */
6142 if (*s != '\0')
6143 ret = i386_displacement (s, s + strlen (s));
6144 if (ret)
6145 {
6146 /* Swap base and index in 16-bit memory operands like
6147 [si+bx]. Since i386_index_check is also used in AT&T
6148 mode we have to do that here. */
6149 if (i.base_reg
6150 && i.index_reg
6151 && (i.base_reg->reg_type & Reg16)
6152 && (i.index_reg->reg_type & Reg16)
6153 && i.base_reg->reg_num >= 6
6154 && i.index_reg->reg_num < 6)
6155 {
6156 const reg_entry *base = i.index_reg;
6157
6158 i.index_reg = i.base_reg;
6159 i.base_reg = base;
6160 }
6161 ret = i386_index_check (operand_string);
6162 }
6163 }
6164 }
6165
6166 /* Constant and OFFSET expressions are handled by i386_immediate. */
6167 else if ((intel_parser.op_modifier & (1 << T_OFFSET))
6168 || intel_parser.reg == NULL)
6169 ret = i386_immediate (intel_parser.disp);
6170
6171 if (intel_parser.next_operand && this_operand >= MAX_OPERANDS - 1)
6172 ret = 0;
6173 if (!ret || !intel_parser.next_operand)
6174 break;
6175 intel_parser.op_string = intel_parser.next_operand;
6176 this_operand = i.operands++;
6177 }
6178
6179 free (p);
6180 free (intel_parser.disp);
6181
6182 return ret;
6183 }
6184
6185 #define NUM_ADDRESS_REGS (!!i.base_reg + !!i.index_reg)
6186
6187 /* expr e04 expr'
6188
6189 expr' cmpOp e04 expr'
6190 | Empty */
6191 static int
intel_expr()6192 intel_expr ()
6193 {
6194 /* XXX Implement the comparison operators. */
6195 return intel_e04 ();
6196 }
6197
6198 /* e04 e05 e04'
6199
6200 e04' addOp e05 e04'
6201 | Empty */
6202 static int
intel_e04()6203 intel_e04 ()
6204 {
6205 int nregs = -1;
6206
6207 for (;;)
6208 {
6209 if (!intel_e05())
6210 return 0;
6211
6212 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
6213 i.base_reg = i386_regtab + REGNAM_AL; /* al is invalid as base */
6214
6215 if (cur_token.code == '+')
6216 nregs = -1;
6217 else if (cur_token.code == '-')
6218 nregs = NUM_ADDRESS_REGS;
6219 else
6220 return 1;
6221
6222 strcat (intel_parser.disp, cur_token.str);
6223 intel_match_token (cur_token.code);
6224 }
6225 }
6226
6227 /* e05 e06 e05'
6228
6229 e05' binOp e06 e05'
6230 | Empty */
6231 static int
intel_e05()6232 intel_e05 ()
6233 {
6234 int nregs = ~NUM_ADDRESS_REGS;
6235
6236 for (;;)
6237 {
6238 if (!intel_e06())
6239 return 0;
6240
6241 if (cur_token.code == '&' || cur_token.code == '|' || cur_token.code == '^')
6242 {
6243 char str[2];
6244
6245 str[0] = cur_token.code;
6246 str[1] = 0;
6247 strcat (intel_parser.disp, str);
6248 }
6249 else
6250 break;
6251
6252 intel_match_token (cur_token.code);
6253
6254 if (nregs < 0)
6255 nregs = ~nregs;
6256 }
6257 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
6258 i.base_reg = i386_regtab + REGNAM_AL + 1; /* cl is invalid as base */
6259 return 1;
6260 }
6261
6262 /* e06 e09 e06'
6263
6264 e06' mulOp e09 e06'
6265 | Empty */
6266 static int
intel_e06()6267 intel_e06 ()
6268 {
6269 int nregs = ~NUM_ADDRESS_REGS;
6270
6271 for (;;)
6272 {
6273 if (!intel_e09())
6274 return 0;
6275
6276 if (cur_token.code == '*' || cur_token.code == '/' || cur_token.code == '%')
6277 {
6278 char str[2];
6279
6280 str[0] = cur_token.code;
6281 str[1] = 0;
6282 strcat (intel_parser.disp, str);
6283 }
6284 else if (cur_token.code == T_SHL)
6285 strcat (intel_parser.disp, "<<");
6286 else if (cur_token.code == T_SHR)
6287 strcat (intel_parser.disp, ">>");
6288 else
6289 break;
6290
6291 intel_match_token (cur_token.code);
6292
6293 if (nregs < 0)
6294 nregs = ~nregs;
6295 }
6296 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
6297 i.base_reg = i386_regtab + REGNAM_AL + 2; /* dl is invalid as base */
6298 return 1;
6299 }
6300
6301 /* e09 OFFSET e09
6302 | SHORT e09
6303 | + e09
6304 | - e09
6305 | ~ e09
6306 | NOT e09
6307 | e10 e09'
6308
6309 e09' PTR e10 e09'
6310 | : e10 e09'
6311 | Empty */
6312 static int
intel_e09()6313 intel_e09 ()
6314 {
6315 int nregs = ~NUM_ADDRESS_REGS;
6316 int in_offset = 0;
6317
6318 for (;;)
6319 {
6320 /* Don't consume constants here. */
6321 if (cur_token.code == '+' || cur_token.code == '-')
6322 {
6323 /* Need to look one token ahead - if the next token
6324 is a constant, the current token is its sign. */
6325 int next_code;
6326
6327 intel_match_token (cur_token.code);
6328 next_code = cur_token.code;
6329 intel_putback_token ();
6330 if (next_code == T_CONST)
6331 break;
6332 }
6333
6334 /* e09 OFFSET e09 */
6335 if (cur_token.code == T_OFFSET)
6336 {
6337 if (!in_offset++)
6338 ++intel_parser.in_offset;
6339 }
6340
6341 /* e09 SHORT e09 */
6342 else if (cur_token.code == T_SHORT)
6343 intel_parser.op_modifier |= 1 << T_SHORT;
6344
6345 /* e09 + e09 */
6346 else if (cur_token.code == '+')
6347 strcat (intel_parser.disp, "+");
6348
6349 /* e09 - e09
6350 | ~ e09
6351 | NOT e09 */
6352 else if (cur_token.code == '-' || cur_token.code == '~')
6353 {
6354 char str[2];
6355
6356 if (nregs < 0)
6357 nregs = ~nregs;
6358 str[0] = cur_token.code;
6359 str[1] = 0;
6360 strcat (intel_parser.disp, str);
6361 }
6362
6363 /* e09 e10 e09' */
6364 else
6365 break;
6366
6367 intel_match_token (cur_token.code);
6368 }
6369
6370 for (;;)
6371 {
6372 if (!intel_e10 ())
6373 return 0;
6374
6375 /* e09' PTR e10 e09' */
6376 if (cur_token.code == T_PTR)
6377 {
6378 char suffix;
6379
6380 if (prev_token.code == T_BYTE)
6381 suffix = BYTE_MNEM_SUFFIX;
6382
6383 else if (prev_token.code == T_WORD)
6384 {
6385 if (current_templates->start->name[0] == 'l'
6386 && current_templates->start->name[2] == 's'
6387 && current_templates->start->name[3] == 0)
6388 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
6389 else if (intel_parser.got_a_float == 2) /* "fi..." */
6390 suffix = SHORT_MNEM_SUFFIX;
6391 else
6392 suffix = WORD_MNEM_SUFFIX;
6393 }
6394
6395 else if (prev_token.code == T_DWORD)
6396 {
6397 if (current_templates->start->name[0] == 'l'
6398 && current_templates->start->name[2] == 's'
6399 && current_templates->start->name[3] == 0)
6400 suffix = WORD_MNEM_SUFFIX;
6401 else if (flag_code == CODE_16BIT
6402 && (current_templates->start->opcode_modifier
6403 & (Jump | JumpDword)))
6404 suffix = LONG_DOUBLE_MNEM_SUFFIX;
6405 else if (intel_parser.got_a_float == 1) /* "f..." */
6406 suffix = SHORT_MNEM_SUFFIX;
6407 else
6408 suffix = LONG_MNEM_SUFFIX;
6409 }
6410
6411 else if (prev_token.code == T_FWORD)
6412 {
6413 if (current_templates->start->name[0] == 'l'
6414 && current_templates->start->name[2] == 's'
6415 && current_templates->start->name[3] == 0)
6416 suffix = LONG_MNEM_SUFFIX;
6417 else if (!intel_parser.got_a_float)
6418 {
6419 if (flag_code == CODE_16BIT)
6420 add_prefix (DATA_PREFIX_OPCODE);
6421 suffix = LONG_DOUBLE_MNEM_SUFFIX;
6422 }
6423 else
6424 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
6425 }
6426
6427 else if (prev_token.code == T_QWORD)
6428 {
6429 if (intel_parser.got_a_float == 1) /* "f..." */
6430 suffix = LONG_MNEM_SUFFIX;
6431 else
6432 suffix = QWORD_MNEM_SUFFIX;
6433 }
6434
6435 else if (prev_token.code == T_TBYTE)
6436 {
6437 if (intel_parser.got_a_float == 1)
6438 suffix = LONG_DOUBLE_MNEM_SUFFIX;
6439 else
6440 suffix = BYTE_MNEM_SUFFIX; /* so it will cause an error */
6441 }
6442
6443 else if (prev_token.code == T_XMMWORD)
6444 {
6445 /* XXX ignored for now, but accepted since gcc uses it */
6446 suffix = 0;
6447 }
6448
6449 else
6450 {
6451 as_bad (_("Unknown operand modifier `%s'"), prev_token.str);
6452 return 0;
6453 }
6454
6455 /* Operands for jump/call using 'ptr' notation denote absolute
6456 addresses. */
6457 if (current_templates->start->opcode_modifier & (Jump | JumpDword))
6458 i.types[this_operand] |= JumpAbsolute;
6459
6460 if (current_templates->start->base_opcode == 0x8d /* lea */)
6461 ;
6462 else if (!i.suffix)
6463 i.suffix = suffix;
6464 else if (i.suffix != suffix)
6465 {
6466 as_bad (_("Conflicting operand modifiers"));
6467 return 0;
6468 }
6469
6470 }
6471
6472 /* e09' : e10 e09' */
6473 else if (cur_token.code == ':')
6474 {
6475 if (prev_token.code != T_REG)
6476 {
6477 /* While {call,jmp} SSSS:OOOO is MASM syntax only when SSSS is a
6478 segment/group identifier (which we don't have), using comma
6479 as the operand separator there is even less consistent, since
6480 there all branches only have a single operand. */
6481 if (this_operand != 0
6482 || intel_parser.in_offset
6483 || intel_parser.in_bracket
6484 || (!(current_templates->start->opcode_modifier
6485 & (Jump|JumpDword|JumpInterSegment))
6486 && !(current_templates->start->operand_types[0]
6487 & JumpAbsolute)))
6488 return intel_match_token (T_NIL);
6489 /* Remember the start of the 2nd operand and terminate 1st
6490 operand here.
6491 XXX This isn't right, yet (when SSSS:OOOO is right operand of
6492 another expression), but it gets at least the simplest case
6493 (a plain number or symbol on the left side) right. */
6494 intel_parser.next_operand = intel_parser.op_string;
6495 *--intel_parser.op_string = '\0';
6496 return intel_match_token (':');
6497 }
6498 }
6499
6500 /* e09' Empty */
6501 else
6502 break;
6503
6504 intel_match_token (cur_token.code);
6505
6506 }
6507
6508 if (in_offset)
6509 {
6510 --intel_parser.in_offset;
6511 if (nregs < 0)
6512 nregs = ~nregs;
6513 if (NUM_ADDRESS_REGS > nregs)
6514 {
6515 as_bad (_("Invalid operand to `OFFSET'"));
6516 return 0;
6517 }
6518 intel_parser.op_modifier |= 1 << T_OFFSET;
6519 }
6520
6521 if (nregs >= 0 && NUM_ADDRESS_REGS > nregs)
6522 i.base_reg = i386_regtab + REGNAM_AL + 3; /* bl is invalid as base */
6523 return 1;
6524 }
6525
6526 static int
intel_bracket_expr()6527 intel_bracket_expr ()
6528 {
6529 int was_offset = intel_parser.op_modifier & (1 << T_OFFSET);
6530 const char *start = intel_parser.op_string;
6531 int len;
6532
6533 if (i.op[this_operand].regs)
6534 return intel_match_token (T_NIL);
6535
6536 intel_match_token ('[');
6537
6538 /* Mark as a memory operand only if it's not already known to be an
6539 offset expression. If it's an offset expression, we need to keep
6540 the brace in. */
6541 if (!intel_parser.in_offset)
6542 {
6543 ++intel_parser.in_bracket;
6544
6545 /* Operands for jump/call inside brackets denote absolute addresses. */
6546 if (current_templates->start->opcode_modifier & (Jump | JumpDword))
6547 i.types[this_operand] |= JumpAbsolute;
6548
6549 /* Unfortunately gas always diverged from MASM in a respect that can't
6550 be easily fixed without risking to break code sequences likely to be
6551 encountered (the testsuite even check for this): MASM doesn't consider
6552 an expression inside brackets unconditionally as a memory reference.
6553 When that is e.g. a constant, an offset expression, or the sum of the
6554 two, this is still taken as a constant load. gas, however, always
6555 treated these as memory references. As a compromise, we'll try to make
6556 offset expressions inside brackets work the MASM way (since that's
6557 less likely to be found in real world code), but make constants alone
6558 continue to work the traditional gas way. In either case, issue a
6559 warning. */
6560 intel_parser.op_modifier &= ~was_offset;
6561 }
6562 else
6563 strcat (intel_parser.disp, "[");
6564
6565 /* Add a '+' to the displacement string if necessary. */
6566 if (*intel_parser.disp != '\0'
6567 && *(intel_parser.disp + strlen (intel_parser.disp) - 1) != '+')
6568 strcat (intel_parser.disp, "+");
6569
6570 if (intel_expr ()
6571 && (len = intel_parser.op_string - start - 1,
6572 intel_match_token (']')))
6573 {
6574 /* Preserve brackets when the operand is an offset expression. */
6575 if (intel_parser.in_offset)
6576 strcat (intel_parser.disp, "]");
6577 else
6578 {
6579 --intel_parser.in_bracket;
6580 if (i.base_reg || i.index_reg)
6581 intel_parser.is_mem = 1;
6582 if (!intel_parser.is_mem)
6583 {
6584 if (!(intel_parser.op_modifier & (1 << T_OFFSET)))
6585 /* Defer the warning until all of the operand was parsed. */
6586 intel_parser.is_mem = -1;
6587 else if (!quiet_warnings)
6588 as_warn (_("`[%.*s]' taken to mean just `%.*s'"), len, start, len, start);
6589 }
6590 }
6591 intel_parser.op_modifier |= was_offset;
6592
6593 return 1;
6594 }
6595 return 0;
6596 }
6597
6598 /* e10 e11 e10'
6599
6600 e10' [ expr ] e10'
6601 | Empty */
6602 static int
intel_e10()6603 intel_e10 ()
6604 {
6605 if (!intel_e11 ())
6606 return 0;
6607
6608 while (cur_token.code == '[')
6609 {
6610 if (!intel_bracket_expr ())
6611 return 0;
6612 }
6613
6614 return 1;
6615 }
6616
6617 /* e11 ( expr )
6618 | [ expr ]
6619 | BYTE
6620 | WORD
6621 | DWORD
6622 | FWORD
6623 | QWORD
6624 | TBYTE
6625 | OWORD
6626 | XMMWORD
6627 | $
6628 | .
6629 | register
6630 | id
6631 | constant */
6632 static int
intel_e11()6633 intel_e11 ()
6634 {
6635 switch (cur_token.code)
6636 {
6637 /* e11 ( expr ) */
6638 case '(':
6639 intel_match_token ('(');
6640 strcat (intel_parser.disp, "(");
6641
6642 if (intel_expr () && intel_match_token (')'))
6643 {
6644 strcat (intel_parser.disp, ")");
6645 return 1;
6646 }
6647 return 0;
6648
6649 /* e11 [ expr ] */
6650 case '[':
6651 return intel_bracket_expr ();
6652
6653 /* e11 $
6654 | . */
6655 case '.':
6656 strcat (intel_parser.disp, cur_token.str);
6657 intel_match_token (cur_token.code);
6658
6659 /* Mark as a memory operand only if it's not already known to be an
6660 offset expression. */
6661 if (!intel_parser.in_offset)
6662 intel_parser.is_mem = 1;
6663
6664 return 1;
6665
6666 /* e11 register */
6667 case T_REG:
6668 {
6669 const reg_entry *reg = intel_parser.reg = cur_token.reg;
6670
6671 intel_match_token (T_REG);
6672
6673 /* Check for segment change. */
6674 if (cur_token.code == ':')
6675 {
6676 if (!(reg->reg_type & (SReg2 | SReg3)))
6677 {
6678 as_bad (_("`%s' is not a valid segment register"), reg->reg_name);
6679 return 0;
6680 }
6681 else if (i.seg[i.mem_operands])
6682 as_warn (_("Extra segment override ignored"));
6683 else
6684 {
6685 if (!intel_parser.in_offset)
6686 intel_parser.is_mem = 1;
6687 switch (reg->reg_num)
6688 {
6689 case 0:
6690 i.seg[i.mem_operands] = &es;
6691 break;
6692 case 1:
6693 i.seg[i.mem_operands] = &cs;
6694 break;
6695 case 2:
6696 i.seg[i.mem_operands] = &ss;
6697 break;
6698 case 3:
6699 i.seg[i.mem_operands] = &ds;
6700 break;
6701 case 4:
6702 i.seg[i.mem_operands] = &fs;
6703 break;
6704 case 5:
6705 i.seg[i.mem_operands] = &gs;
6706 break;
6707 }
6708 }
6709 }
6710
6711 /* Not a segment register. Check for register scaling. */
6712 else if (cur_token.code == '*')
6713 {
6714 if (!intel_parser.in_bracket)
6715 {
6716 as_bad (_("Register scaling only allowed in memory operands"));
6717 return 0;
6718 }
6719
6720 if (reg->reg_type & Reg16) /* Disallow things like [si*1]. */
6721 reg = i386_regtab + REGNAM_AX + 4; /* sp is invalid as index */
6722 else if (i.index_reg)
6723 reg = i386_regtab + REGNAM_EAX + 4; /* esp is invalid as index */
6724
6725 /* What follows must be a valid scale. */
6726 intel_match_token ('*');
6727 i.index_reg = reg;
6728 i.types[this_operand] |= BaseIndex;
6729
6730 /* Set the scale after setting the register (otherwise,
6731 i386_scale will complain) */
6732 if (cur_token.code == '+' || cur_token.code == '-')
6733 {
6734 char *str, sign = cur_token.code;
6735 intel_match_token (cur_token.code);
6736 if (cur_token.code != T_CONST)
6737 {
6738 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
6739 cur_token.str);
6740 return 0;
6741 }
6742 str = (char *) xmalloc (strlen (cur_token.str) + 2);
6743 strcpy (str + 1, cur_token.str);
6744 *str = sign;
6745 if (!i386_scale (str))
6746 return 0;
6747 free (str);
6748 }
6749 else if (!i386_scale (cur_token.str))
6750 return 0;
6751 intel_match_token (cur_token.code);
6752 }
6753
6754 /* No scaling. If this is a memory operand, the register is either a
6755 base register (first occurrence) or an index register (second
6756 occurrence). */
6757 else if (intel_parser.in_bracket)
6758 {
6759
6760 if (!i.base_reg)
6761 i.base_reg = reg;
6762 else if (!i.index_reg)
6763 i.index_reg = reg;
6764 else
6765 {
6766 as_bad (_("Too many register references in memory operand"));
6767 return 0;
6768 }
6769
6770 i.types[this_operand] |= BaseIndex;
6771 }
6772
6773 /* It's neither base nor index. */
6774 else if (!intel_parser.in_offset && !intel_parser.is_mem)
6775 {
6776 i.types[this_operand] |= reg->reg_type & ~BaseIndex;
6777 i.op[this_operand].regs = reg;
6778 i.reg_operands++;
6779 }
6780 else
6781 {
6782 as_bad (_("Invalid use of register"));
6783 return 0;
6784 }
6785
6786 /* Since registers are not part of the displacement string (except
6787 when we're parsing offset operands), we may need to remove any
6788 preceding '+' from the displacement string. */
6789 if (*intel_parser.disp != '\0'
6790 && !intel_parser.in_offset)
6791 {
6792 char *s = intel_parser.disp;
6793 s += strlen (s) - 1;
6794 if (*s == '+')
6795 *s = '\0';
6796 }
6797
6798 return 1;
6799 }
6800
6801 /* e11 BYTE
6802 | WORD
6803 | DWORD
6804 | FWORD
6805 | QWORD
6806 | TBYTE
6807 | OWORD
6808 | XMMWORD */
6809 case T_BYTE:
6810 case T_WORD:
6811 case T_DWORD:
6812 case T_FWORD:
6813 case T_QWORD:
6814 case T_TBYTE:
6815 case T_XMMWORD:
6816 intel_match_token (cur_token.code);
6817
6818 if (cur_token.code == T_PTR)
6819 return 1;
6820
6821 /* It must have been an identifier. */
6822 intel_putback_token ();
6823 cur_token.code = T_ID;
6824 /* FALLTHRU */
6825
6826 /* e11 id
6827 | constant */
6828 case T_ID:
6829 if (!intel_parser.in_offset && intel_parser.is_mem <= 0)
6830 {
6831 symbolS *symbolP;
6832
6833 /* The identifier represents a memory reference only if it's not
6834 preceded by an offset modifier and if it's not an equate. */
6835 symbolP = symbol_find(cur_token.str);
6836 if (!symbolP || S_GET_SEGMENT(symbolP) != absolute_section)
6837 intel_parser.is_mem = 1;
6838 }
6839 /* FALLTHRU */
6840
6841 case T_CONST:
6842 case '-':
6843 case '+':
6844 {
6845 char *save_str, sign = 0;
6846
6847 /* Allow constants that start with `+' or `-'. */
6848 if (cur_token.code == '-' || cur_token.code == '+')
6849 {
6850 sign = cur_token.code;
6851 intel_match_token (cur_token.code);
6852 if (cur_token.code != T_CONST)
6853 {
6854 as_bad (_("Syntax error: Expecting a constant, got `%s'"),
6855 cur_token.str);
6856 return 0;
6857 }
6858 }
6859
6860 save_str = (char *) xmalloc (strlen (cur_token.str) + 2);
6861 strcpy (save_str + !!sign, cur_token.str);
6862 if (sign)
6863 *save_str = sign;
6864
6865 /* Get the next token to check for register scaling. */
6866 intel_match_token (cur_token.code);
6867
6868 /* Check if this constant is a scaling factor for an index register. */
6869 if (cur_token.code == '*')
6870 {
6871 if (intel_match_token ('*') && cur_token.code == T_REG)
6872 {
6873 const reg_entry *reg = cur_token.reg;
6874
6875 if (!intel_parser.in_bracket)
6876 {
6877 as_bad (_("Register scaling only allowed in memory operands"));
6878 return 0;
6879 }
6880
6881 if (reg->reg_type & Reg16) /* Disallow things like [1*si]. */
6882 reg = i386_regtab + REGNAM_AX + 4; /* sp is invalid as index */
6883 else if (i.index_reg)
6884 reg = i386_regtab + REGNAM_EAX + 4; /* esp is invalid as index */
6885
6886 /* The constant is followed by `* reg', so it must be
6887 a valid scale. */
6888 i.index_reg = reg;
6889 i.types[this_operand] |= BaseIndex;
6890
6891 /* Set the scale after setting the register (otherwise,
6892 i386_scale will complain) */
6893 if (!i386_scale (save_str))
6894 return 0;
6895 intel_match_token (T_REG);
6896
6897 /* Since registers are not part of the displacement
6898 string, we may need to remove any preceding '+' from
6899 the displacement string. */
6900 if (*intel_parser.disp != '\0')
6901 {
6902 char *s = intel_parser.disp;
6903 s += strlen (s) - 1;
6904 if (*s == '+')
6905 *s = '\0';
6906 }
6907
6908 free (save_str);
6909
6910 return 1;
6911 }
6912
6913 /* The constant was not used for register scaling. Since we have
6914 already consumed the token following `*' we now need to put it
6915 back in the stream. */
6916 intel_putback_token ();
6917 }
6918
6919 /* Add the constant to the displacement string. */
6920 strcat (intel_parser.disp, save_str);
6921 free (save_str);
6922
6923 return 1;
6924 }
6925 }
6926
6927 as_bad (_("Unrecognized token '%s'"), cur_token.str);
6928 return 0;
6929 }
6930
6931 /* Match the given token against cur_token. If they match, read the next
6932 token from the operand string. */
6933 static int
intel_match_token(code)6934 intel_match_token (code)
6935 int code;
6936 {
6937 if (cur_token.code == code)
6938 {
6939 intel_get_token ();
6940 return 1;
6941 }
6942 else
6943 {
6944 as_bad (_("Unexpected token `%s'"), cur_token.str);
6945 return 0;
6946 }
6947 }
6948
6949 /* Read a new token from intel_parser.op_string and store it in cur_token. */
6950 static void
intel_get_token()6951 intel_get_token ()
6952 {
6953 char *end_op;
6954 const reg_entry *reg;
6955 struct intel_token new_token;
6956
6957 new_token.code = T_NIL;
6958 new_token.reg = NULL;
6959 new_token.str = NULL;
6960
6961 /* Free the memory allocated to the previous token and move
6962 cur_token to prev_token. */
6963 if (prev_token.str)
6964 free (prev_token.str);
6965
6966 prev_token = cur_token;
6967
6968 /* Skip whitespace. */
6969 while (is_space_char (*intel_parser.op_string))
6970 intel_parser.op_string++;
6971
6972 /* Return an empty token if we find nothing else on the line. */
6973 if (*intel_parser.op_string == '\0')
6974 {
6975 cur_token = new_token;
6976 return;
6977 }
6978
6979 /* The new token cannot be larger than the remainder of the operand
6980 string. */
6981 new_token.str = (char *) xmalloc (strlen (intel_parser.op_string) + 1);
6982 new_token.str[0] = '\0';
6983
6984 if (strchr ("0123456789", *intel_parser.op_string))
6985 {
6986 char *p = new_token.str;
6987 char *q = intel_parser.op_string;
6988 new_token.code = T_CONST;
6989
6990 /* Allow any kind of identifier char to encompass floating point and
6991 hexadecimal numbers. */
6992 while (is_identifier_char (*q))
6993 *p++ = *q++;
6994 *p = '\0';
6995
6996 /* Recognize special symbol names [0-9][bf]. */
6997 if (strlen (intel_parser.op_string) == 2
6998 && (intel_parser.op_string[1] == 'b'
6999 || intel_parser.op_string[1] == 'f'))
7000 new_token.code = T_ID;
7001 }
7002
7003 else if ((reg = parse_register (intel_parser.op_string, &end_op)) != NULL)
7004 {
7005 size_t len = end_op - intel_parser.op_string;
7006
7007 new_token.code = T_REG;
7008 new_token.reg = reg;
7009
7010 memcpy (new_token.str, intel_parser.op_string, len);
7011 new_token.str[len] = '\0';
7012 }
7013
7014 else if (is_identifier_char (*intel_parser.op_string))
7015 {
7016 char *p = new_token.str;
7017 char *q = intel_parser.op_string;
7018
7019 /* A '.' or '$' followed by an identifier char is an identifier.
7020 Otherwise, it's operator '.' followed by an expression. */
7021 if ((*q == '.' || *q == '$') && !is_identifier_char (*(q + 1)))
7022 {
7023 new_token.code = '.';
7024 new_token.str[0] = '.';
7025 new_token.str[1] = '\0';
7026 }
7027 else
7028 {
7029 while (is_identifier_char (*q) || *q == '@')
7030 *p++ = *q++;
7031 *p = '\0';
7032
7033 if (strcasecmp (new_token.str, "NOT") == 0)
7034 new_token.code = '~';
7035
7036 else if (strcasecmp (new_token.str, "MOD") == 0)
7037 new_token.code = '%';
7038
7039 else if (strcasecmp (new_token.str, "AND") == 0)
7040 new_token.code = '&';
7041
7042 else if (strcasecmp (new_token.str, "OR") == 0)
7043 new_token.code = '|';
7044
7045 else if (strcasecmp (new_token.str, "XOR") == 0)
7046 new_token.code = '^';
7047
7048 else if (strcasecmp (new_token.str, "SHL") == 0)
7049 new_token.code = T_SHL;
7050
7051 else if (strcasecmp (new_token.str, "SHR") == 0)
7052 new_token.code = T_SHR;
7053
7054 else if (strcasecmp (new_token.str, "BYTE") == 0)
7055 new_token.code = T_BYTE;
7056
7057 else if (strcasecmp (new_token.str, "WORD") == 0)
7058 new_token.code = T_WORD;
7059
7060 else if (strcasecmp (new_token.str, "DWORD") == 0)
7061 new_token.code = T_DWORD;
7062
7063 else if (strcasecmp (new_token.str, "FWORD") == 0)
7064 new_token.code = T_FWORD;
7065
7066 else if (strcasecmp (new_token.str, "QWORD") == 0)
7067 new_token.code = T_QWORD;
7068
7069 else if (strcasecmp (new_token.str, "TBYTE") == 0
7070 /* XXX remove (gcc still uses it) */
7071 || strcasecmp (new_token.str, "XWORD") == 0)
7072 new_token.code = T_TBYTE;
7073
7074 else if (strcasecmp (new_token.str, "XMMWORD") == 0
7075 || strcasecmp (new_token.str, "OWORD") == 0)
7076 new_token.code = T_XMMWORD;
7077
7078 else if (strcasecmp (new_token.str, "PTR") == 0)
7079 new_token.code = T_PTR;
7080
7081 else if (strcasecmp (new_token.str, "SHORT") == 0)
7082 new_token.code = T_SHORT;
7083
7084 else if (strcasecmp (new_token.str, "OFFSET") == 0)
7085 {
7086 new_token.code = T_OFFSET;
7087
7088 /* ??? This is not mentioned in the MASM grammar but gcc
7089 makes use of it with -mintel-syntax. OFFSET may be
7090 followed by FLAT: */
7091 if (strncasecmp (q, " FLAT:", 6) == 0)
7092 strcat (new_token.str, " FLAT:");
7093 }
7094
7095 /* ??? This is not mentioned in the MASM grammar. */
7096 else if (strcasecmp (new_token.str, "FLAT") == 0)
7097 {
7098 new_token.code = T_OFFSET;
7099 if (*q == ':')
7100 strcat (new_token.str, ":");
7101 else
7102 as_bad (_("`:' expected"));
7103 }
7104
7105 else
7106 new_token.code = T_ID;
7107 }
7108 }
7109
7110 else if (strchr ("+-/*%|&^:[]()~", *intel_parser.op_string))
7111 {
7112 new_token.code = *intel_parser.op_string;
7113 new_token.str[0] = *intel_parser.op_string;
7114 new_token.str[1] = '\0';
7115 }
7116
7117 else if (strchr ("<>", *intel_parser.op_string)
7118 && *intel_parser.op_string == *(intel_parser.op_string + 1))
7119 {
7120 new_token.code = *intel_parser.op_string == '<' ? T_SHL : T_SHR;
7121 new_token.str[0] = *intel_parser.op_string;
7122 new_token.str[1] = *intel_parser.op_string;
7123 new_token.str[2] = '\0';
7124 }
7125
7126 else
7127 as_bad (_("Unrecognized token `%s'"), intel_parser.op_string);
7128
7129 intel_parser.op_string += strlen (new_token.str);
7130 cur_token = new_token;
7131 }
7132
7133 /* Put cur_token back into the token stream and make cur_token point to
7134 prev_token. */
7135 static void
intel_putback_token()7136 intel_putback_token ()
7137 {
7138 if (cur_token.code != T_NIL)
7139 {
7140 intel_parser.op_string -= strlen (cur_token.str);
7141 free (cur_token.str);
7142 }
7143 cur_token = prev_token;
7144
7145 /* Forget prev_token. */
7146 prev_token.code = T_NIL;
7147 prev_token.reg = NULL;
7148 prev_token.str = NULL;
7149 }
7150
7151 int
tc_x86_regname_to_dw2regnum(const char * regname)7152 tc_x86_regname_to_dw2regnum (const char *regname)
7153 {
7154 unsigned int regnum;
7155 unsigned int regnames_count;
7156 static const char *const regnames_32[] =
7157 {
7158 "eax", "ecx", "edx", "ebx",
7159 "esp", "ebp", "esi", "edi",
7160 "eip", "eflags", NULL,
7161 "st0", "st1", "st2", "st3",
7162 "st4", "st5", "st6", "st7",
7163 NULL, NULL,
7164 "xmm0", "xmm1", "xmm2", "xmm3",
7165 "xmm4", "xmm5", "xmm6", "xmm7",
7166 "mm0", "mm1", "mm2", "mm3",
7167 "mm4", "mm5", "mm6", "mm7",
7168 "fcw", "fsw", "mxcsr",
7169 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
7170 "tr", "ldtr"
7171 };
7172 static const char *const regnames_64[] =
7173 {
7174 "rax", "rdx", "rcx", "rbx",
7175 "rsi", "rdi", "rbp", "rsp",
7176 "r8", "r9", "r10", "r11",
7177 "r12", "r13", "r14", "r15",
7178 "rip",
7179 "xmm0", "xmm1", "xmm2", "xmm3",
7180 "xmm4", "xmm5", "xmm6", "xmm7",
7181 "xmm8", "xmm9", "xmm10", "xmm11",
7182 "xmm12", "xmm13", "xmm14", "xmm15",
7183 "st0", "st1", "st2", "st3",
7184 "st4", "st5", "st6", "st7",
7185 "mm0", "mm1", "mm2", "mm3",
7186 "mm4", "mm5", "mm6", "mm7",
7187 "rflags",
7188 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
7189 "fs.base", "gs.base", NULL, NULL,
7190 "tr", "ldtr",
7191 "mxcsr", "fcw", "fsw"
7192 };
7193 const char *const *regnames;
7194
7195 if (flag_code == CODE_64BIT)
7196 {
7197 regnames = regnames_64;
7198 regnames_count = ARRAY_SIZE (regnames_64);
7199 }
7200 else
7201 {
7202 regnames = regnames_32;
7203 regnames_count = ARRAY_SIZE (regnames_32);
7204 }
7205
7206 for (regnum = 0; regnum < regnames_count; regnum++)
7207 if (regnames[regnum] != NULL
7208 && strcmp (regname, regnames[regnum]) == 0)
7209 return regnum;
7210
7211 return -1;
7212 }
7213
7214 void
tc_x86_frame_initial_instructions(void)7215 tc_x86_frame_initial_instructions (void)
7216 {
7217 static unsigned int sp_regno;
7218
7219 if (!sp_regno)
7220 sp_regno = tc_x86_regname_to_dw2regnum (flag_code == CODE_64BIT
7221 ? "rsp" : "esp");
7222
7223 cfi_add_CFA_def_cfa (sp_regno, -x86_cie_data_alignment);
7224 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
7225 }
7226
7227 int
i386_elf_section_type(const char * str,size_t len)7228 i386_elf_section_type (const char *str, size_t len)
7229 {
7230 if (flag_code == CODE_64BIT
7231 && len == sizeof ("unwind") - 1
7232 && strncmp (str, "unwind", 6) == 0)
7233 return SHT_X86_64_UNWIND;
7234
7235 return -1;
7236 }
7237
7238 #ifdef TE_PE
7239 void
tc_pe_dwarf2_emit_offset(symbolS * symbol,unsigned int size)7240 tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
7241 {
7242 expressionS expr;
7243
7244 expr.X_op = O_secrel;
7245 expr.X_add_symbol = symbol;
7246 expr.X_add_number = 0;
7247 emit_expr (&expr, size);
7248 }
7249 #endif
7250
7251 #if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
7252 /* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
7253
7254 int
x86_64_section_letter(int letter,char ** ptr_msg)7255 x86_64_section_letter (int letter, char **ptr_msg)
7256 {
7257 if (flag_code == CODE_64BIT)
7258 {
7259 if (letter == 'l')
7260 return SHF_X86_64_LARGE;
7261
7262 *ptr_msg = _("Bad .section directive: want a,l,w,x,M,S,G,T in string");
7263 }
7264 else
7265 *ptr_msg = _("Bad .section directive: want a,w,x,M,S,G,T in string");
7266 return -1;
7267 }
7268
7269 int
x86_64_section_word(char * str,size_t len)7270 x86_64_section_word (char *str, size_t len)
7271 {
7272 if (len == 5 && flag_code == CODE_64BIT && strncmp (str, "large", 5) == 0)
7273 return SHF_X86_64_LARGE;
7274
7275 return -1;
7276 }
7277
7278 static void
handle_large_common(int small ATTRIBUTE_UNUSED)7279 handle_large_common (int small ATTRIBUTE_UNUSED)
7280 {
7281 if (flag_code != CODE_64BIT)
7282 {
7283 s_comm_internal (0, elf_common_parse);
7284 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
7285 }
7286 else
7287 {
7288 static segT lbss_section;
7289 asection *saved_com_section_ptr = elf_com_section_ptr;
7290 asection *saved_bss_section = bss_section;
7291
7292 if (lbss_section == NULL)
7293 {
7294 flagword applicable;
7295 segT seg = now_seg;
7296 subsegT subseg = now_subseg;
7297
7298 /* The .lbss section is for local .largecomm symbols. */
7299 lbss_section = subseg_new (".lbss", 0);
7300 applicable = bfd_applicable_section_flags (stdoutput);
7301 bfd_set_section_flags (stdoutput, lbss_section,
7302 applicable & SEC_ALLOC);
7303 seg_info (lbss_section)->bss = 1;
7304
7305 subseg_set (seg, subseg);
7306 }
7307
7308 elf_com_section_ptr = &_bfd_elf_large_com_section;
7309 bss_section = lbss_section;
7310
7311 s_comm_internal (0, elf_common_parse);
7312
7313 elf_com_section_ptr = saved_com_section_ptr;
7314 bss_section = saved_bss_section;
7315 }
7316 }
7317 #endif /* OBJ_ELF || OBJ_MAYBE_ELF */
7318