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