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