* config/tc-s390.c (md_parse_option): Add cpu type z990.
[external/binutils.git] / gas / config / tc-s390.c
1 /* tc-s390.c -- Assemble for the S390
2    Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3    Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include <stdio.h>
23 #include "as.h"
24 #include "safe-ctype.h"
25 #include "subsegs.h"
26 #include "struc-symbol.h"
27 #include "dwarf2dbg.h"
28
29 #include "opcode/s390.h"
30 #include "elf/s390.h"
31
32 /* The default architecture.  */
33 #ifndef DEFAULT_ARCH
34 #define DEFAULT_ARCH "s390"
35 #endif
36 static char *default_arch = DEFAULT_ARCH;
37 /* Either 32 or 64, selects file format.  */
38 static int s390_arch_size = 0;
39
40 static unsigned int current_mode_mask = 0;
41 static unsigned int current_cpu = -1U;
42
43 /* Whether to use user friendly register names. Default is TRUE.  */
44 #ifndef TARGET_REG_NAMES_P
45 #define TARGET_REG_NAMES_P TRUE
46 #endif
47
48 static bfd_boolean reg_names_p = TARGET_REG_NAMES_P;
49
50 /* Set to TRUE if we want to warn about zero base/index registers.  */
51 static bfd_boolean warn_areg_zero = FALSE;
52
53 /* Generic assembler global variables which must be defined by all
54    targets.  */
55
56 const char comment_chars[] = "#";
57
58 /* Characters which start a comment at the beginning of a line.  */
59 const char line_comment_chars[] = "#";
60
61 /* Characters which may be used to separate multiple commands on a
62    single line.  */
63 const char line_separator_chars[] = ";";
64
65 /* Characters which are used to indicate an exponent in a floating
66    point number.  */
67 const char EXP_CHARS[] = "eE";
68
69 /* Characters which mean that a number is a floating point constant,
70    as in 0d1.0.  */
71 const char FLT_CHARS[] = "dD";
72
73 /* The target specific pseudo-ops which we support.  */
74
75 /* Define the prototypes for the pseudo-ops */
76 static void s390_byte PARAMS ((int));
77 static void s390_elf_cons PARAMS ((int));
78 static void s390_bss PARAMS ((int));
79 static void s390_insn PARAMS ((int));
80 static void s390_literals PARAMS ((int));
81
82 const pseudo_typeS md_pseudo_table[] =
83 {
84   { "align", s_align_bytes, 0 },
85   /* Pseudo-ops which must be defined.  */
86   { "bss",      s390_bss,       0 },
87   { "insn",     s390_insn,      0 },
88   /* Pseudo-ops which must be overridden.  */
89   { "byte",     s390_byte,      0 },
90   { "short",    s390_elf_cons,  2 },
91   { "long",     s390_elf_cons,  4 },
92   { "quad",     s390_elf_cons,  8 },
93   { "ltorg",    s390_literals,  0 },
94   { "string",   stringer,       2 },
95   { NULL,       NULL,           0 }
96 };
97
98
99 /* Structure to hold information about predefined registers.  */
100 struct pd_reg
101   {
102     char *name;
103     int value;
104   };
105
106 /* List of registers that are pre-defined:
107
108    Each access register has a predefined name of the form:
109      a<reg_num> which has the value <reg_num>.
110
111    Each control register has a predefined name of the form:
112      c<reg_num> which has the value <reg_num>.
113
114    Each general register has a predefined name of the form:
115      r<reg_num> which has the value <reg_num>.
116
117    Each floating point register a has predefined name of the form:
118      f<reg_num> which has the value <reg_num>.
119
120    There are individual registers as well:
121      sp     has the value 15
122      lit    has the value 12
123
124    The table is sorted. Suitable for searching by a binary search.  */
125
126 static const struct pd_reg pre_defined_registers[] =
127 {
128   { "a0", 0 },     /* Access registers */
129   { "a1", 1 },
130   { "a10", 10 },
131   { "a11", 11 },
132   { "a12", 12 },
133   { "a13", 13 },
134   { "a14", 14 },
135   { "a15", 15 },
136   { "a2", 2 },
137   { "a3", 3 },
138   { "a4", 4 },
139   { "a5", 5 },
140   { "a6", 6 },
141   { "a7", 7 },
142   { "a8", 8 },
143   { "a9", 9 },
144
145   { "c0", 0 },     /* Control registers */
146   { "c1", 1 },
147   { "c10", 10 },
148   { "c11", 11 },
149   { "c12", 12 },
150   { "c13", 13 },
151   { "c14", 14 },
152   { "c15", 15 },
153   { "c2", 2 },
154   { "c3", 3 },
155   { "c4", 4 },
156   { "c5", 5 },
157   { "c6", 6 },
158   { "c7", 7 },
159   { "c8", 8 },
160   { "c9", 9 },
161
162   { "f0", 0 },     /* Floating point registers */
163   { "f1", 1 },
164   { "f10", 10 },
165   { "f11", 11 },
166   { "f12", 12 },
167   { "f13", 13 },
168   { "f14", 14 },
169   { "f15", 15 },
170   { "f2", 2 },
171   { "f3", 3 },
172   { "f4", 4 },
173   { "f5", 5 },
174   { "f6", 6 },
175   { "f7", 7 },
176   { "f8", 8 },
177   { "f9", 9 },
178
179   { "lit", 13 },   /* Pointer to literal pool */
180
181   { "r0", 0 },     /* General purpose registers */
182   { "r1", 1 },
183   { "r10", 10 },
184   { "r11", 11 },
185   { "r12", 12 },
186   { "r13", 13 },
187   { "r14", 14 },
188   { "r15", 15 },
189   { "r2", 2 },
190   { "r3", 3 },
191   { "r4", 4 },
192   { "r5", 5 },
193   { "r6", 6 },
194   { "r7", 7 },
195   { "r8", 8 },
196   { "r9", 9 },
197
198   { "sp", 15 },   /* Stack pointer */
199
200 };
201
202 #define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct pd_reg))
203
204 static int reg_name_search
205   PARAMS ((const struct pd_reg *, int, const char *));
206 static bfd_boolean register_name PARAMS ((expressionS *));
207 static void init_default_arch PARAMS ((void));
208 static void s390_insert_operand
209   PARAMS ((unsigned char *, const struct s390_operand *, offsetT, char *,
210            unsigned int));
211 static char *md_gather_operands
212   PARAMS ((char *, unsigned char *, const struct s390_opcode *));
213
214 /* Given NAME, find the register number associated with that name, return
215    the integer value associated with the given name or -1 on failure.  */
216
217 static int
218 reg_name_search (regs, regcount, name)
219      const struct pd_reg *regs;
220      int regcount;
221      const char *name;
222 {
223   int middle, low, high;
224   int cmp;
225
226   low = 0;
227   high = regcount - 1;
228
229   do
230     {
231       middle = (low + high) / 2;
232       cmp = strcasecmp (name, regs[middle].name);
233       if (cmp < 0)
234         high = middle - 1;
235       else if (cmp > 0)
236         low = middle + 1;
237       else
238         return regs[middle].value;
239     }
240   while (low <= high);
241
242   return -1;
243 }
244
245
246 /*
247  * Summary of register_name().
248  *
249  * in:  Input_line_pointer points to 1st char of operand.
250  *
251  * out: A expressionS.
252  *      The operand may have been a register: in this case, X_op == O_register,
253  *      X_add_number is set to the register number, and truth is returned.
254  *      Input_line_pointer->(next non-blank) char after operand, or is in its
255  *      original state.
256  */
257
258 static bfd_boolean
259 register_name (expressionP)
260      expressionS *expressionP;
261 {
262   int reg_number;
263   char *name;
264   char *start;
265   char c;
266
267   /* Find the spelling of the operand.  */
268   start = name = input_line_pointer;
269   if (name[0] == '%' && ISALPHA (name[1]))
270     name = ++input_line_pointer;
271   else
272     return FALSE;
273
274   c = get_symbol_end ();
275   reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
276
277   /* Put back the delimiting char.  */
278   *input_line_pointer = c;
279
280   /* Look to see if it's in the register table.  */
281   if (reg_number >= 0)
282     {
283       expressionP->X_op = O_register;
284       expressionP->X_add_number = reg_number;
285
286       /* Make the rest nice.  */
287       expressionP->X_add_symbol = NULL;
288       expressionP->X_op_symbol = NULL;
289       return TRUE;
290     }
291
292   /* Reset the line as if we had not done anything.  */
293   input_line_pointer = start;
294   return FALSE;
295 }
296
297 /* Local variables.  */
298
299 /* Opformat hash table.  */
300 static struct hash_control *s390_opformat_hash;
301
302 /* Opcode hash table.  */
303 static struct hash_control *s390_opcode_hash;
304
305 /* Flags to set in the elf header */
306 static flagword s390_flags = 0;
307
308 symbolS *GOT_symbol;            /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
309
310 #ifndef WORKING_DOT_WORD
311 const int md_short_jump_size = 4;
312 const int md_long_jump_size = 4;
313 #endif
314
315 const char *md_shortopts = "A:m:kVQ:";
316 struct option md_longopts[] = {
317   {NULL, no_argument, NULL, 0}
318 };
319 size_t md_longopts_size = sizeof (md_longopts);
320
321 /* Initialize the default opcode arch and word size from the default
322    architecture name if not specified by an option.  */
323 static void
324 init_default_arch ()
325 {
326   if (strcmp (default_arch, "s390") == 0)
327     {
328       if (s390_arch_size == 0)
329         s390_arch_size = 32;
330     }
331   else if (strcmp (default_arch, "s390x") == 0)
332     {
333       if (s390_arch_size == 0)
334         s390_arch_size = 64;
335     }
336   else
337     as_fatal ("Invalid default architecture, broken assembler.");
338
339   if (current_mode_mask == 0)
340     {
341       if (s390_arch_size == 32)
342         current_mode_mask = 1 << S390_OPCODE_ESA;
343       else
344         current_mode_mask = 1 << S390_OPCODE_ZARCH;
345     }
346   if (current_cpu == -1U)
347     {
348       if (current_mode_mask == (1 << S390_OPCODE_ESA))
349         current_cpu = S390_OPCODE_G5;
350       else
351         current_cpu = S390_OPCODE_Z900;
352     }
353 }
354
355 /* Called by TARGET_FORMAT.  */
356 const char *
357 s390_target_format ()
358 {
359   /* We don't get a chance to initialize anything before we're called,
360      so handle that now.  */
361   init_default_arch ();
362
363   return s390_arch_size == 64 ? "elf64-s390" : "elf32-s390";
364 }
365
366 int
367 md_parse_option (c, arg)
368      int c;
369      char *arg;
370 {
371   switch (c)
372     {
373       /* -k: Ignore for FreeBSD compatibility.  */
374     case 'k':
375       break;
376     case 'm':
377       if (arg != NULL && strcmp (arg, "regnames") == 0)
378         reg_names_p = TRUE;
379
380       else if (arg != NULL && strcmp (arg, "no-regnames") == 0)
381         reg_names_p = FALSE;
382
383       else if (arg != NULL && strcmp (arg, "warn-areg-zero") == 0)
384         warn_areg_zero = TRUE;
385
386       else if (arg != NULL && strcmp (arg, "31") == 0)
387         s390_arch_size = 32;
388
389       else if (arg != NULL && strcmp (arg, "64") == 0)
390         s390_arch_size = 64;
391
392       else if (arg != NULL && strcmp (arg, "esa") == 0)
393         current_mode_mask = 1 << S390_OPCODE_ESA;
394
395       else if (arg != NULL && strcmp (arg, "zarch") == 0)
396         current_mode_mask = 1 << S390_OPCODE_ZARCH;
397
398       else if (arg != NULL && strncmp (arg, "arch=", 5) == 0)
399         {
400           if (strcmp (arg + 5, "g5") == 0)
401             current_cpu = S390_OPCODE_G5;
402           else if (strcmp (arg + 5, "g6") == 0)
403             current_cpu = S390_OPCODE_G6;
404           else if (strcmp (arg + 5, "z900") == 0)
405             current_cpu = S390_OPCODE_Z900;
406           else if (strcmp (arg + 5, "z990") == 0)
407             current_cpu = S390_OPCODE_Z990;
408           else
409             {
410               as_bad (_("invalid switch -m%s"), arg);
411               return 0;
412             }
413         }
414
415       else
416         {
417           as_bad (_("invalid switch -m%s"), arg);
418           return 0;
419         }
420       break;
421
422     case 'A':
423       /* Option -A is deprecated. Still available for compatability.  */
424       if (arg != NULL && strcmp (arg, "esa") == 0)
425         current_cpu = S390_OPCODE_G5;
426       else if (arg != NULL && strcmp (arg, "esame") == 0)
427         current_cpu = S390_OPCODE_Z900;
428       else
429         as_bad ("invalid architecture -A%s", arg);
430       break;
431
432       /* -V: SVR4 argument to print version ID.  */
433     case 'V':
434       print_version_id ();
435       break;
436
437       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
438          should be emitted or not.  FIXME: Not implemented.  */
439     case 'Q':
440       break;
441
442     default:
443       return 0;
444     }
445
446   return 1;
447 }
448
449 void
450 md_show_usage (stream)
451      FILE *stream;
452 {
453   fprintf (stream, _("\
454         S390 options:\n\
455         -mregnames        Allow symbolic names for registers\n\
456         -mwarn-areg-zero  Warn about zero base/index registers\n\
457         -mno-regnames     Do not allow symbolic names for registers\n\
458         -m31              Set file format to 31 bit format\n\
459         -m64              Set file format to 64 bit format\n"));
460   fprintf (stream, _("\
461         -V                print assembler version number\n\
462         -Qy, -Qn          ignored\n"));
463 }
464
465 /* This function is called when the assembler starts up.  It is called
466    after the options have been parsed and the output file has been
467    opened.  */
468
469 void
470 md_begin ()
471 {
472   register const struct s390_opcode *op;
473   const struct s390_opcode *op_end;
474   bfd_boolean dup_insn = FALSE;
475   const char *retval;
476
477   /* Give a warning if the combination -m64-bit and -Aesa is used.  */
478   if (s390_arch_size == 64 && current_cpu < S390_OPCODE_Z900)
479     as_warn ("The 64 bit file format is used without esame instructions.");
480
481   /* Set the ELF flags if desired.  */
482   if (s390_flags)
483     bfd_set_private_flags (stdoutput, s390_flags);
484
485   /* Insert the opcode formats into a hash table.  */
486   s390_opformat_hash = hash_new ();
487
488   op_end = s390_opformats + s390_num_opformats;
489   for (op = s390_opformats; op < op_end; op++)
490     {
491       retval = hash_insert (s390_opformat_hash, op->name, (PTR) op);
492       if (retval != (const char *) NULL)
493         {
494           as_bad (_("Internal assembler error for instruction format %s"),
495                   op->name);
496           dup_insn = TRUE;
497         }
498     }
499
500   /* Insert the opcodes into a hash table.  */
501   s390_opcode_hash = hash_new ();
502
503   op_end = s390_opcodes + s390_num_opcodes;
504   for (op = s390_opcodes; op < op_end; op++)
505     if (op->min_cpu <= current_cpu)
506       {
507         retval = hash_insert (s390_opcode_hash, op->name, (PTR) op);
508         if (retval != (const char *) NULL)
509           {
510             as_bad (_("Internal assembler error for instruction %s"),
511                     op->name);
512             dup_insn = TRUE;
513           }
514         while (op < op_end - 1 && strcmp (op->name, op[1].name) == 0)
515           op++;
516       }
517
518   if (dup_insn)
519     abort ();
520
521   record_alignment (text_section, 2);
522   record_alignment (data_section, 2);
523   record_alignment (bss_section, 2);
524
525 }
526
527 /* Called after all assembly has been done.  */
528 void
529 s390_md_end ()
530 {
531   if (s390_arch_size == 64)
532     bfd_set_arch_mach (stdoutput, bfd_arch_s390, bfd_mach_s390_64);
533   else
534     bfd_set_arch_mach (stdoutput, bfd_arch_s390, bfd_mach_s390_31);
535 }
536
537 void
538 s390_align_code (fragP, count)
539      fragS *fragP;
540      int count;
541 {
542   /* We use nop pattern 0x0707.  */
543   if (count > 0)
544     {
545       memset (fragP->fr_literal + fragP->fr_fix, 0x07, count);
546       fragP->fr_var = count;
547     }
548 }
549
550 /* Insert an operand value into an instruction.  */
551
552 static void
553 s390_insert_operand (insn, operand, val, file, line)
554      unsigned char *insn;
555      const struct s390_operand *operand;
556      offsetT val;
557      char *file;
558      unsigned int line;
559 {
560   addressT uval;
561   int offset;
562
563   if (operand->flags & (S390_OPERAND_SIGNED|S390_OPERAND_PCREL))
564     {
565       offsetT min, max;
566
567       max = ((offsetT) 1 << (operand->bits - 1)) - 1;
568       min = - ((offsetT) 1 << (operand->bits - 1));
569       /* Halve PCREL operands.  */
570       if (operand->flags & S390_OPERAND_PCREL)
571         val >>= 1;
572       /* Check for underflow / overflow.  */
573       if (val < min || val > max)
574         {
575           const char *err =
576             "operand out of range (%s not between %ld and %ld)";
577           char buf[100];
578
579           if (operand->flags & S390_OPERAND_PCREL)
580             {
581               val <<= 1;
582               min <<= 1;
583               max <<= 1;
584             }
585           sprint_value (buf, val);
586           if (file == (char *) NULL)
587             as_bad (err, buf, (int) min, (int) max);
588           else
589             as_bad_where (file, line, err, buf, (int) min, (int) max);
590           return;
591         }
592       /* val is ok, now restrict it to operand->bits bits.  */
593       uval = (addressT) val & ((((addressT) 1 << (operand->bits-1)) << 1) - 1);
594       /* val is restrict, now check for special case.  */
595       if (operand->bits == 20 && operand->shift == 20)
596         uval = (uval >> 12) | ((uval & 0xfff) << 8);
597     }
598   else
599     {
600       addressT min, max;
601
602       max = (((addressT) 1 << (operand->bits - 1)) << 1) - 1;
603       min = (offsetT) 0;
604       uval = (addressT) val;
605       /* Length x in an instructions has real length x+1.  */
606       if (operand->flags & S390_OPERAND_LENGTH)
607         uval--;
608       /* Check for underflow / overflow.  */
609       if (uval < min || uval > max)
610         {
611           const char *err =
612             "operand out of range (%s not between %ld and %ld)";
613           char buf[100];
614
615           if (operand->flags & S390_OPERAND_LENGTH)
616             {
617               uval++;
618               min++;
619               max++;
620             }
621           sprint_value (buf, uval);
622           if (file == (char *) NULL)
623             as_bad (err, buf, (int) min, (int) max);
624           else
625             as_bad_where (file, line, err, buf, (int) min, (int) max);
626           return;
627         }
628     }
629
630   /* Insert fragments of the operand byte for byte.  */
631   offset = operand->shift + operand->bits;
632   uval <<= (-offset) & 7;
633   insn += (offset - 1) / 8;
634   while (uval != 0)
635     {
636       *insn-- |= uval;
637       uval >>= 8;
638     }
639 }
640
641 struct map_tls
642   {
643     char *string;
644     int length;
645     bfd_reloc_code_real_type reloc;
646   };
647
648 static bfd_reloc_code_real_type s390_tls_suffix
649   PARAMS ((char **, expressionS *));
650
651 /* Parse tls marker and return the desired relocation.  */
652 static bfd_reloc_code_real_type
653 s390_tls_suffix (str_p, exp_p)
654      char **str_p;
655      expressionS *exp_p;
656 {
657   static struct map_tls mapping[] =
658   {
659     { "tls_load", 8, BFD_RELOC_390_TLS_LOAD },
660     { "tls_gdcall", 10, BFD_RELOC_390_TLS_GDCALL  },
661     { "tls_ldcall", 10, BFD_RELOC_390_TLS_LDCALL  },
662     { NULL,  0, BFD_RELOC_UNUSED }
663   };
664   struct map_tls *ptr;
665   char *orig_line;
666   char *str;
667   char *ident;
668   int len;
669
670   str = *str_p;
671   if (*str++ != ':')
672     return BFD_RELOC_UNUSED;
673
674   ident = str;
675   while (ISIDNUM (*str))
676     str++;
677   len = str - ident;
678   if (*str++ != ':')
679     return BFD_RELOC_UNUSED;
680
681   orig_line = input_line_pointer;
682   input_line_pointer = str;
683   expression (exp_p);
684   str = input_line_pointer;
685   if (&input_line_pointer != str_p)
686     input_line_pointer = orig_line;
687
688   if (exp_p->X_op != O_symbol)
689     return BFD_RELOC_UNUSED;
690
691   for (ptr = &mapping[0]; ptr->length > 0; ptr++)
692     if (len == ptr->length
693         && strncasecmp (ident, ptr->string, ptr->length) == 0)
694       {
695         /* Found a matching tls suffix.  */
696         *str_p = str;
697         return ptr->reloc;
698       }
699   return BFD_RELOC_UNUSED;
700 }
701
702 /* Structure used to hold suffixes.  */
703 typedef enum
704   {
705     ELF_SUFFIX_NONE = 0,
706     ELF_SUFFIX_GOT,
707     ELF_SUFFIX_PLT,
708     ELF_SUFFIX_GOTENT,
709     ELF_SUFFIX_GOTOFF,
710     ELF_SUFFIX_GOTPLT,
711     ELF_SUFFIX_PLTOFF,
712     ELF_SUFFIX_TLS_GD,
713     ELF_SUFFIX_TLS_GOTIE,
714     ELF_SUFFIX_TLS_IE,
715     ELF_SUFFIX_TLS_LDM,
716     ELF_SUFFIX_TLS_LDO,
717     ELF_SUFFIX_TLS_LE
718   }
719 elf_suffix_type;
720
721 struct map_bfd
722   {
723     char *string;
724     int length;
725     elf_suffix_type suffix;
726   };
727
728 static elf_suffix_type s390_elf_suffix PARAMS ((char **, expressionS *));
729 static int s390_exp_compare PARAMS ((expressionS *exp1, expressionS *exp2));
730 static elf_suffix_type s390_lit_suffix
731   PARAMS ((char **, expressionS *, elf_suffix_type));
732
733
734 /* Parse @got/@plt/@gotoff. and return the desired relocation.  */
735 static elf_suffix_type
736 s390_elf_suffix (str_p, exp_p)
737      char **str_p;
738      expressionS *exp_p;
739 {
740   static struct map_bfd mapping[] =
741   {
742     { "got", 3, ELF_SUFFIX_GOT  },
743     { "got12", 5, ELF_SUFFIX_GOT  },
744     { "plt", 3, ELF_SUFFIX_PLT  },
745     { "gotent", 6, ELF_SUFFIX_GOTENT },
746     { "gotoff", 6, ELF_SUFFIX_GOTOFF },
747     { "gotplt", 6, ELF_SUFFIX_GOTPLT },
748     { "pltoff", 6, ELF_SUFFIX_PLTOFF },
749     { "tlsgd", 5, ELF_SUFFIX_TLS_GD },
750     { "gotntpoff", 9, ELF_SUFFIX_TLS_GOTIE },
751     { "indntpoff", 9, ELF_SUFFIX_TLS_IE },
752     { "tlsldm", 6, ELF_SUFFIX_TLS_LDM },
753     { "dtpoff", 6, ELF_SUFFIX_TLS_LDO },
754     { "ntpoff", 6, ELF_SUFFIX_TLS_LE },
755     { NULL,  0, ELF_SUFFIX_NONE }
756   };
757
758   struct map_bfd *ptr;
759   char *str = *str_p;
760   char *ident;
761   int len;
762
763   if (*str++ != '@')
764     return ELF_SUFFIX_NONE;
765
766   ident = str;
767   while (ISALNUM (*str))
768     str++;
769   len = str - ident;
770
771   for (ptr = &mapping[0]; ptr->length > 0; ptr++)
772     if (len == ptr->length
773         && strncasecmp (ident, ptr->string, ptr->length) == 0)
774       {
775         if (exp_p->X_add_number != 0)
776           as_warn (_("identifier+constant@%s means identifier@%s+constant"),
777                    ptr->string, ptr->string);
778         /* Now check for identifier@suffix+constant.  */
779         if (*str == '-' || *str == '+')
780           {
781             char *orig_line = input_line_pointer;
782             expressionS new_exp;
783
784             input_line_pointer = str;
785             expression (&new_exp);
786
787             switch (new_exp.X_op)
788               {
789               case O_constant: /* X_add_number (a constant expression).  */
790                 exp_p->X_add_number += new_exp.X_add_number;
791                 str = input_line_pointer;
792                 break;
793               case O_symbol:   /* X_add_symbol + X_add_number.  */
794                 /* this case is used for e.g. xyz@PLT+.Label.  */
795                 exp_p->X_add_number += new_exp.X_add_number;
796                 exp_p->X_op_symbol = new_exp.X_add_symbol;
797                 exp_p->X_op = O_add;
798                 str = input_line_pointer;
799                 break;
800               case O_uminus:   /* (- X_add_symbol) + X_add_number.  */
801                 /* this case is used for e.g. xyz@PLT-.Label.  */
802                 exp_p->X_add_number += new_exp.X_add_number;
803                 exp_p->X_op_symbol = new_exp.X_add_symbol;
804                 exp_p->X_op = O_subtract;
805                 str = input_line_pointer;
806                 break;
807               default:
808                 break;
809               }
810
811             /* If s390_elf_suffix has not been called with
812                &input_line_pointer as first parameter, we have
813                clobbered the input_line_pointer. We have to
814                undo that.  */
815             if (&input_line_pointer != str_p)
816               input_line_pointer = orig_line;
817           }
818         *str_p = str;
819         return ptr->suffix;
820       }
821
822   return BFD_RELOC_UNUSED;
823 }
824
825 /* Structure used to hold a literal pool entry.  */
826 struct s390_lpe
827   {
828     struct s390_lpe *next;
829     expressionS ex;
830     FLONUM_TYPE floatnum;     /* used if X_op == O_big && X_add_number <= 0 */
831     LITTLENUM_TYPE bignum[4]; /* used if X_op == O_big && X_add_number > 0  */
832     int nbytes;
833     bfd_reloc_code_real_type reloc;
834     symbolS *sym;
835   };
836
837 static struct s390_lpe *lpe_free_list = NULL;
838 static struct s390_lpe *lpe_list = NULL;
839 static struct s390_lpe *lpe_list_tail = NULL;
840 static symbolS *lp_sym = NULL;
841 static int lp_count = 0;
842 static int lpe_count = 0;
843
844 static int
845 s390_exp_compare (exp1, exp2)
846      expressionS *exp1;
847      expressionS *exp2;
848 {
849   if (exp1->X_op != exp2->X_op)
850     return 0;
851
852   switch (exp1->X_op)
853     {
854     case O_constant:   /* X_add_number must be equal.  */
855     case O_register:
856       return exp1->X_add_number == exp2->X_add_number;
857
858     case O_big:
859       as_bad (_("Can't handle O_big in s390_exp_compare"));
860
861     case O_symbol:     /* X_add_symbol & X_add_number must be equal.  */
862     case O_symbol_rva:
863     case O_uminus:
864     case O_bit_not:
865     case O_logical_not:
866       return (exp1->X_add_symbol == exp2->X_add_symbol)
867         &&   (exp1->X_add_number == exp2->X_add_number);
868
869     case O_multiply:   /* X_add_symbol,X_op_symbol&X_add_number must be equal.  */
870     case O_divide:
871     case O_modulus:
872     case O_left_shift:
873     case O_right_shift:
874     case O_bit_inclusive_or:
875     case O_bit_or_not:
876     case O_bit_exclusive_or:
877     case O_bit_and:
878     case O_add:
879     case O_subtract:
880     case O_eq:
881     case O_ne:
882     case O_lt:
883     case O_le:
884     case O_ge:
885     case O_gt:
886     case O_logical_and:
887     case O_logical_or:
888       return (exp1->X_add_symbol == exp2->X_add_symbol)
889         &&   (exp1->X_op_symbol  == exp2->X_op_symbol)
890         &&   (exp1->X_add_number == exp2->X_add_number);
891     default:
892       return 0;
893     }
894 }
895
896 /* Test for @lit and if its present make an entry in the literal pool and
897    modify the current expression to be an offset into the literal pool.  */
898 static elf_suffix_type
899 s390_lit_suffix (str_p, exp_p, suffix)
900      char **str_p;
901      expressionS *exp_p;
902      elf_suffix_type suffix;
903 {
904   bfd_reloc_code_real_type reloc;
905   char tmp_name[64];
906   char *str = *str_p;
907   char *ident;
908   struct s390_lpe *lpe;
909   int nbytes, len;
910
911   if (*str++ != ':')
912     return suffix;       /* No modification.  */
913
914   /* We look for a suffix of the form "@lit1", "@lit2", "@lit4" or "@lit8".  */
915   ident = str;
916   while (ISALNUM (*str))
917     str++;
918   len = str - ident;
919   if (len != 4 || strncasecmp (ident, "lit", 3) != 0
920       || (ident[3]!='1' && ident[3]!='2' && ident[3]!='4' && ident[3]!='8'))
921     return suffix;      /* no modification */
922   nbytes = ident[3] - '0';
923
924   reloc = BFD_RELOC_UNUSED;
925   if (suffix == ELF_SUFFIX_GOT)
926     {
927       if (nbytes == 2)
928         reloc = BFD_RELOC_390_GOT16;
929       else if (nbytes == 4)
930         reloc = BFD_RELOC_32_GOT_PCREL;
931       else if (nbytes == 8)
932         reloc = BFD_RELOC_390_GOT64;
933     }
934   else if (suffix == ELF_SUFFIX_PLT)
935     {
936       if (nbytes == 4)
937         reloc = BFD_RELOC_390_PLT32;
938       else if (nbytes == 8)
939         reloc = BFD_RELOC_390_PLT64;
940     }
941
942   if (suffix != ELF_SUFFIX_NONE && reloc == BFD_RELOC_UNUSED)
943     as_bad (_("Invalid suffix for literal pool entry"));
944
945   /* Search the pool if the new entry is a duplicate.  */
946   if (exp_p->X_op == O_big)
947     {
948       /* Special processing for big numbers.  */
949       for (lpe = lpe_list; lpe != NULL; lpe = lpe->next)
950         {
951           if (lpe->ex.X_op == O_big)
952             {
953               if (exp_p->X_add_number <= 0 && lpe->ex.X_add_number <= 0)
954                 {
955                   if (memcmp (&generic_floating_point_number, &lpe->floatnum,
956                               sizeof (FLONUM_TYPE)) == 0)
957                     break;
958                 }
959               else if (exp_p->X_add_number == lpe->ex.X_add_number)
960                 {
961                   if (memcmp (generic_bignum, lpe->bignum,
962                               sizeof (LITTLENUM_TYPE)*exp_p->X_add_number) == 0)
963                     break;
964                 }
965             }
966         }
967     }
968   else
969     {
970       /* Processing for 'normal' data types.  */
971       for (lpe = lpe_list; lpe != NULL; lpe = lpe->next)
972         if (lpe->nbytes == nbytes && lpe->reloc == reloc
973             && s390_exp_compare (exp_p, &lpe->ex) != 0)
974           break;
975     }
976
977   if (lpe == NULL)
978     {
979       /* A new literal.  */
980       if (lpe_free_list != NULL)
981         {
982           lpe = lpe_free_list;
983           lpe_free_list = lpe_free_list->next;
984         }
985       else
986         {
987           lpe = (struct s390_lpe *) xmalloc (sizeof (struct s390_lpe));
988         }
989
990       lpe->ex = *exp_p;
991
992       if (exp_p->X_op == O_big)
993         {
994           if (exp_p->X_add_number <= 0)
995             lpe->floatnum = generic_floating_point_number;
996           else if (exp_p->X_add_number <= 4)
997             memcpy (lpe->bignum, generic_bignum,
998                     exp_p->X_add_number * sizeof (LITTLENUM_TYPE));
999           else
1000             as_bad (_("Big number is too big"));
1001         }
1002
1003       lpe->nbytes = nbytes;
1004       lpe->reloc = reloc;
1005       /* Literal pool name defined ?  */
1006       if (lp_sym == NULL)
1007         {
1008           sprintf (tmp_name, ".L\001%i", lp_count);
1009           lp_sym = symbol_make (tmp_name);
1010         }
1011
1012       /* Make name for literal pool entry.  */
1013       sprintf (tmp_name, ".L\001%i\002%i", lp_count, lpe_count);
1014       lpe_count++;
1015       lpe->sym = symbol_make (tmp_name);
1016
1017       /* Add to literal pool list.  */
1018       lpe->next = NULL;
1019       if (lpe_list_tail != NULL)
1020         {
1021           lpe_list_tail->next = lpe;
1022           lpe_list_tail = lpe;
1023         }
1024       else
1025         lpe_list = lpe_list_tail = lpe;
1026     }
1027
1028   /* Now change exp_p to the offset into the literal pool.
1029      Thats the expression: .L^Ax^By-.L^Ax   */
1030   exp_p->X_add_symbol = lpe->sym;
1031   exp_p->X_op_symbol = lp_sym;
1032   exp_p->X_op = O_subtract;
1033   exp_p->X_add_number = 0;
1034
1035   *str_p = str;
1036
1037   /* We change the suffix type to ELF_SUFFIX_NONE, because
1038      the difference of two local labels is just a number.  */
1039   return ELF_SUFFIX_NONE;
1040 }
1041
1042 /* Like normal .long/.short/.word, except support @got, etc.
1043    clobbers input_line_pointer, checks end-of-line.  */
1044 static void
1045 s390_elf_cons (nbytes)
1046      register int nbytes;       /* 1=.byte, 2=.word, 4=.long */
1047 {
1048   expressionS exp;
1049   elf_suffix_type suffix;
1050
1051   if (is_it_end_of_statement ())
1052     {
1053       demand_empty_rest_of_line ();
1054       return;
1055     }
1056
1057   do
1058     {
1059       expression (&exp);
1060
1061       if (exp.X_op == O_symbol
1062           && *input_line_pointer == '@'
1063           && (suffix = s390_elf_suffix (&input_line_pointer, &exp)) != ELF_SUFFIX_NONE)
1064         {
1065           bfd_reloc_code_real_type reloc;
1066           reloc_howto_type *reloc_howto;
1067           int size;
1068           char *where;
1069
1070           if (nbytes == 2)
1071             {
1072               static bfd_reloc_code_real_type tab2[] =
1073                 {
1074                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_NONE  */
1075                   BFD_RELOC_390_GOT16,          /* ELF_SUFFIX_GOT  */
1076                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_PLT  */
1077                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_GOTENT  */
1078                   BFD_RELOC_16_GOTOFF,          /* ELF_SUFFIX_GOTOFF  */
1079                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_GOTPLT  */
1080                   BFD_RELOC_390_PLTOFF16,       /* ELF_SUFFIX_PLTOFF  */
1081                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_GD  */
1082                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_GOTIE  */
1083                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_IE  */
1084                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_LDM  */
1085                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_LDO  */
1086                   BFD_RELOC_UNUSED              /* ELF_SUFFIX_TLS_LE  */
1087                 };
1088               reloc = tab2[suffix];
1089             }
1090           else if (nbytes == 4)
1091             {
1092               static bfd_reloc_code_real_type tab4[] =
1093                 {
1094                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_NONE  */
1095                   BFD_RELOC_32_GOT_PCREL,       /* ELF_SUFFIX_GOT  */
1096                   BFD_RELOC_390_PLT32,          /* ELF_SUFFIX_PLT  */
1097                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_GOTENT  */
1098                   BFD_RELOC_32_GOTOFF,          /* ELF_SUFFIX_GOTOFF  */
1099                   BFD_RELOC_390_GOTPLT32,       /* ELF_SUFFIX_GOTPLT  */
1100                   BFD_RELOC_390_PLTOFF32,       /* ELF_SUFFIX_PLTOFF  */
1101                   BFD_RELOC_390_TLS_GD32,       /* ELF_SUFFIX_TLS_GD  */
1102                   BFD_RELOC_390_TLS_GOTIE32,    /* ELF_SUFFIX_TLS_GOTIE  */
1103                   BFD_RELOC_390_TLS_IE32,       /* ELF_SUFFIX_TLS_IE  */
1104                   BFD_RELOC_390_TLS_LDM32,      /* ELF_SUFFIX_TLS_LDM  */
1105                   BFD_RELOC_390_TLS_LDO32,      /* ELF_SUFFIX_TLS_LDO  */
1106                   BFD_RELOC_390_TLS_LE32        /* ELF_SUFFIX_TLS_LE  */
1107                 };
1108               reloc = tab4[suffix];
1109             }
1110           else if (nbytes == 8)
1111             {
1112               static bfd_reloc_code_real_type tab8[] =
1113                 {
1114                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_NONE  */
1115                   BFD_RELOC_390_GOT64,          /* ELF_SUFFIX_GOT  */
1116                   BFD_RELOC_390_PLT64,          /* ELF_SUFFIX_PLT  */
1117                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_GOTENT  */
1118                   BFD_RELOC_390_GOTOFF64,       /* ELF_SUFFIX_GOTOFF  */
1119                   BFD_RELOC_390_GOTPLT64,       /* ELF_SUFFIX_GOTPLT  */
1120                   BFD_RELOC_390_PLTOFF64,       /* ELF_SUFFIX_PLTOFF  */
1121                   BFD_RELOC_390_TLS_GD64,       /* ELF_SUFFIX_TLS_GD  */
1122                   BFD_RELOC_390_TLS_GOTIE64,    /* ELF_SUFFIX_TLS_GOTIE  */
1123                   BFD_RELOC_390_TLS_IE64,       /* ELF_SUFFIX_TLS_IE  */
1124                   BFD_RELOC_390_TLS_LDM64,      /* ELF_SUFFIX_TLS_LDM  */
1125                   BFD_RELOC_390_TLS_LDO64,      /* ELF_SUFFIX_TLS_LDO  */
1126                   BFD_RELOC_390_TLS_LE64        /* ELF_SUFFIX_TLS_LE  */
1127                 };
1128               reloc = tab8[suffix];
1129             }
1130           else
1131             reloc = BFD_RELOC_UNUSED;
1132
1133           if (reloc != BFD_RELOC_UNUSED
1134               && (reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc)))
1135             {
1136               size = bfd_get_reloc_size (reloc_howto);
1137               if (size > nbytes)
1138                 as_bad (_("%s relocations do not fit in %d bytes"),
1139                         reloc_howto->name, nbytes);
1140               where = frag_more (nbytes);
1141               md_number_to_chars (where, 0, size);
1142               /* To make fixup_segment do the pc relative conversion the
1143                  pcrel parameter on the fix_new_exp call needs to be FALSE.  */
1144               fix_new_exp (frag_now, where - frag_now->fr_literal,
1145                            size, &exp, FALSE, reloc);
1146             }
1147           else
1148             as_bad (_("relocation not applicable"));
1149         }
1150       else
1151         emit_expr (&exp, (unsigned int) nbytes);
1152     }
1153   while (*input_line_pointer++ == ',');
1154
1155   input_line_pointer--;         /* Put terminator back into stream.  */
1156   demand_empty_rest_of_line ();
1157 }
1158
1159 /* We need to keep a list of fixups.  We can't simply generate them as
1160    we go, because that would require us to first create the frag, and
1161    that would screw up references to ``.''.  */
1162
1163 struct s390_fixup
1164   {
1165     expressionS exp;
1166     int opindex;
1167     bfd_reloc_code_real_type reloc;
1168   };
1169
1170 #define MAX_INSN_FIXUPS (4)
1171
1172 /* This routine is called for each instruction to be assembled.  */
1173
1174 static char *
1175 md_gather_operands (str, insn, opcode)
1176      char *str;
1177      unsigned char *insn;
1178      const struct s390_opcode *opcode;
1179 {
1180   struct s390_fixup fixups[MAX_INSN_FIXUPS];
1181   const struct s390_operand *operand;
1182   const unsigned char *opindex_ptr;
1183   expressionS ex;
1184   elf_suffix_type suffix;
1185   bfd_reloc_code_real_type reloc;
1186   int skip_optional;
1187   int parentheses;
1188   char *f;
1189   int fc, i;
1190
1191   while (ISSPACE (*str))
1192     str++;
1193
1194   parentheses = 0;
1195   skip_optional = 0;
1196
1197   /* Gather the operands.  */
1198   fc = 0;
1199   for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1200     {
1201       char *hold;
1202
1203       operand = s390_operands + *opindex_ptr;
1204
1205       if (skip_optional && (operand->flags & S390_OPERAND_INDEX))
1206         {
1207           /* We do an early skip. For D(X,B) constructions the index
1208              register is skipped (X is optional). For D(L,B) the base
1209              register will be the skipped operand, because L is NOT
1210              optional.  */
1211           skip_optional = 0;
1212           continue;
1213         }
1214
1215       /* Gather the operand.  */
1216       hold = input_line_pointer;
1217       input_line_pointer = str;
1218
1219       /* Parse the operand.  */
1220       if (! register_name (&ex))
1221         expression (&ex);
1222
1223       str = input_line_pointer;
1224       input_line_pointer = hold;
1225
1226       /* Write the operand to the insn.  */
1227       if (ex.X_op == O_illegal)
1228         as_bad (_("illegal operand"));
1229       else if (ex.X_op == O_absent)
1230         as_bad (_("missing operand"));
1231       else if (ex.X_op == O_register || ex.X_op == O_constant)
1232         {
1233           s390_lit_suffix (&str, &ex, ELF_SUFFIX_NONE);
1234
1235           if (ex.X_op != O_register && ex.X_op != O_constant)
1236             {
1237               /* We need to generate a fixup for the
1238                  expression returned by s390_lit_suffix.  */
1239               if (fc >= MAX_INSN_FIXUPS)
1240                 as_fatal (_("too many fixups"));
1241               fixups[fc].exp = ex;
1242               fixups[fc].opindex = *opindex_ptr;
1243               fixups[fc].reloc = BFD_RELOC_UNUSED;
1244               ++fc;
1245             }
1246           else
1247             {
1248               if ((operand->flags & S390_OPERAND_INDEX)
1249                   && ex.X_add_number == 0
1250                   && warn_areg_zero)
1251                 as_warn ("index register specified but zero");
1252               if ((operand->flags & S390_OPERAND_BASE)
1253                   && ex.X_add_number == 0
1254                   && warn_areg_zero)
1255                 as_warn ("base register specified but zero");
1256               s390_insert_operand (insn, operand, ex.X_add_number, NULL, 0);
1257             }
1258         }
1259       else
1260         {
1261           suffix = s390_elf_suffix (&str, &ex);
1262           suffix = s390_lit_suffix (&str, &ex, suffix);
1263           reloc = BFD_RELOC_UNUSED;
1264
1265           if (suffix == ELF_SUFFIX_GOT)
1266             {
1267               if ((operand->flags & S390_OPERAND_DISP) &&
1268                   (operand->bits == 12))
1269                 reloc = BFD_RELOC_390_GOT12;
1270               else if ((operand->flags & S390_OPERAND_DISP) &&
1271                        (operand->bits == 20))
1272                 reloc = BFD_RELOC_390_GOT20;
1273               else if ((operand->flags & S390_OPERAND_SIGNED)
1274                        && (operand->bits == 16))
1275                 reloc = BFD_RELOC_390_GOT16;
1276               else if ((operand->flags & S390_OPERAND_PCREL)
1277                        && (operand->bits == 32))
1278                 reloc = BFD_RELOC_390_GOTENT;
1279             }
1280           else if (suffix == ELF_SUFFIX_PLT)
1281             {
1282               if ((operand->flags & S390_OPERAND_PCREL)
1283                   && (operand->bits == 16))
1284                 reloc = BFD_RELOC_390_PLT16DBL;
1285               else if ((operand->flags & S390_OPERAND_PCREL)
1286                        && (operand->bits == 32))
1287                 reloc = BFD_RELOC_390_PLT32DBL;
1288             }
1289           else if (suffix == ELF_SUFFIX_GOTENT)
1290             {
1291               if ((operand->flags & S390_OPERAND_PCREL)
1292                   && (operand->bits == 32))
1293                 reloc = BFD_RELOC_390_GOTENT;
1294             }
1295           else if (suffix == ELF_SUFFIX_GOTOFF)
1296             {
1297               if ((operand->flags & S390_OPERAND_SIGNED)
1298                   && (operand->bits == 16))
1299                 reloc = BFD_RELOC_16_GOTOFF;
1300             }
1301           else if (suffix == ELF_SUFFIX_PLTOFF)
1302             {
1303               if ((operand->flags & S390_OPERAND_SIGNED)
1304                   && (operand->bits == 16))
1305                 reloc = BFD_RELOC_390_PLTOFF16;
1306             }
1307           else if (suffix == ELF_SUFFIX_GOTPLT)
1308             {
1309               if ((operand->flags & S390_OPERAND_DISP)
1310                   && (operand->bits == 12))
1311                 reloc = BFD_RELOC_390_GOTPLT12;
1312               else if ((operand->flags & S390_OPERAND_SIGNED)
1313                        && (operand->bits == 16))
1314                 reloc = BFD_RELOC_390_GOTPLT16;
1315               else if ((operand->flags & S390_OPERAND_PCREL)
1316                        && (operand->bits == 32))
1317                 reloc = BFD_RELOC_390_GOTPLTENT;
1318             }
1319           else if (suffix == ELF_SUFFIX_TLS_GOTIE)
1320             {
1321               if ((operand->flags & S390_OPERAND_DISP)
1322                   && (operand->bits == 12))
1323                 reloc = BFD_RELOC_390_TLS_GOTIE12;
1324               else if ((operand->flags & S390_OPERAND_DISP)
1325                        && (operand->bits == 20))
1326                 reloc = BFD_RELOC_390_TLS_GOTIE20;
1327             }
1328           else if (suffix == ELF_SUFFIX_TLS_IE)
1329             {
1330               if ((operand->flags & S390_OPERAND_PCREL)
1331                        && (operand->bits == 32))
1332                 reloc = BFD_RELOC_390_TLS_IEENT;
1333             }
1334
1335           if (suffix != ELF_SUFFIX_NONE && reloc == BFD_RELOC_UNUSED)
1336             as_bad (_("invalid operand suffix"));
1337           /* We need to generate a fixup of type 'reloc' for this
1338              expression.  */
1339           if (fc >= MAX_INSN_FIXUPS)
1340             as_fatal (_("too many fixups"));
1341           fixups[fc].exp = ex;
1342           fixups[fc].opindex = *opindex_ptr;
1343           fixups[fc].reloc = reloc;
1344           ++fc;
1345         }
1346
1347       /* Check the next character. The call to expression has advanced
1348          str past any whitespace.  */
1349       if (operand->flags & S390_OPERAND_DISP)
1350         {
1351           /* After a displacement a block in parentheses can start.  */
1352           if (*str != '(')
1353             {
1354               /* Check if parethesed block can be skipped. If the next
1355                  operand is neiter an optional operand nor a base register
1356                  then we have a syntax error.  */
1357               operand = s390_operands + *(++opindex_ptr);
1358               if (!(operand->flags & (S390_OPERAND_INDEX|S390_OPERAND_BASE)))
1359                 as_bad (_("syntax error; missing '(' after displacement"));
1360
1361               /* Ok, skip all operands until S390_OPERAND_BASE.  */
1362               while (!(operand->flags & S390_OPERAND_BASE))
1363                 operand = s390_operands + *(++opindex_ptr);
1364
1365               /* If there is a next operand it must be seperated by a comma.  */
1366               if (opindex_ptr[1] != '\0')
1367                 {
1368                   if (*str++ != ',')
1369                     as_bad (_("syntax error; expected ,"));
1370                 }
1371             }
1372           else
1373             {
1374               /* We found an opening parentheses.  */
1375               str++;
1376               for (f = str; *f != '\0'; f++)
1377                 if (*f == ',' || *f == ')')
1378                   break;
1379               /* If there is no comma until the closing parentheses OR
1380                  there is a comma right after the opening parentheses,
1381                  we have to skip optional operands.  */
1382               if (*f == ',' && f == str)
1383                 {
1384                   /* comma directly after '(' ? */
1385                   skip_optional = 1;
1386                   str++;
1387                 }
1388               else
1389                 skip_optional = (*f != ',');
1390             }
1391         }
1392       else if (operand->flags & S390_OPERAND_BASE)
1393         {
1394           /* After the base register the parenthesed block ends.  */
1395           if (*str++ != ')')
1396             as_bad (_("syntax error; missing ')' after base register"));
1397           skip_optional = 0;
1398           /* If there is a next operand it must be seperated by a comma.  */
1399           if (opindex_ptr[1] != '\0')
1400             {
1401               if (*str++ != ',')
1402                 as_bad (_("syntax error; expected ,"));
1403             }
1404         }
1405       else
1406         {
1407           /* We can find an 'early' closing parentheses in e.g. D(L) instead
1408              of D(L,B).  In this case the base register has to be skipped.  */
1409           if (*str == ')')
1410             {
1411               operand = s390_operands + *(++opindex_ptr);
1412
1413               if (!(operand->flags & S390_OPERAND_BASE))
1414                 as_bad (_("syntax error; ')' not allowed here"));
1415               str++;
1416             }
1417           /* If there is a next operand it must be seperated by a comma.  */
1418           if (opindex_ptr[1] != '\0')
1419             {
1420               if (*str++ != ',')
1421                 as_bad (_("syntax error; expected ,"));
1422             }
1423         }
1424     }
1425
1426   while (ISSPACE (*str))
1427     ++str;
1428
1429   /* Check for tls instruction marker.  */
1430   reloc = s390_tls_suffix (&str, &ex);
1431   if (reloc != BFD_RELOC_UNUSED)
1432     {
1433       /* We need to generate a fixup of type 'reloc' for this
1434          instruction.  */
1435       if (fc >= MAX_INSN_FIXUPS)
1436         as_fatal (_("too many fixups"));
1437       fixups[fc].exp = ex;
1438       fixups[fc].opindex = -1;
1439       fixups[fc].reloc = reloc;
1440       ++fc;
1441     }
1442
1443   if (*str != '\0')
1444     {
1445       char *linefeed;
1446
1447       if ((linefeed = strchr (str, '\n')) != NULL)
1448         *linefeed = '\0';
1449       as_bad (_("junk at end of line: `%s'"), str);
1450       if (linefeed != NULL)
1451         *linefeed = '\n';
1452     }
1453
1454   /* Write out the instruction.  */
1455   f = frag_more (opcode->oplen);
1456   memcpy (f, insn, opcode->oplen);
1457   dwarf2_emit_insn (opcode->oplen);
1458
1459   /* Create any fixups.  At this point we do not use a
1460      bfd_reloc_code_real_type, but instead just use the
1461      BFD_RELOC_UNUSED plus the operand index.  This lets us easily
1462      handle fixups for any operand type, although that is admittedly
1463      not a very exciting feature.  We pick a BFD reloc type in
1464      md_apply_fix3.  */
1465   for (i = 0; i < fc; i++)
1466     {
1467
1468       if (fixups[i].opindex < 0)
1469         {
1470           /* Create tls instruction marker relocation.  */
1471           fix_new_exp (frag_now, f - frag_now->fr_literal, opcode->oplen,
1472                        &fixups[i].exp, 0, fixups[i].reloc);
1473           continue;
1474         }
1475
1476       operand = s390_operands + fixups[i].opindex;
1477
1478       if (fixups[i].reloc != BFD_RELOC_UNUSED)
1479         {
1480           reloc_howto_type *reloc_howto;
1481           fixS *fixP;
1482           int size;
1483
1484           reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1485           if (!reloc_howto)
1486             abort ();
1487
1488           size = bfd_get_reloc_size (reloc_howto);
1489
1490           if (size < 1 || size > 4)
1491             abort ();
1492
1493           fixP = fix_new_exp (frag_now,
1494                               f - frag_now->fr_literal + (operand->shift/8),
1495                               size, &fixups[i].exp, reloc_howto->pc_relative,
1496                               fixups[i].reloc);
1497           /* Turn off overflow checking in fixup_segment. This is necessary
1498              because fixup_segment will signal an overflow for large 4 byte
1499              quantities for GOT12 relocations.  */
1500           if (   fixups[i].reloc == BFD_RELOC_390_GOT12
1501               || fixups[i].reloc == BFD_RELOC_390_GOT20
1502               || fixups[i].reloc == BFD_RELOC_390_GOT16)
1503             fixP->fx_no_overflow = 1;
1504         }
1505       else
1506         fix_new_exp (frag_now, f - frag_now->fr_literal, 4, &fixups[i].exp,
1507                      (operand->flags & S390_OPERAND_PCREL) != 0,
1508                      ((bfd_reloc_code_real_type)
1509                       (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
1510     }
1511   return str;
1512 }
1513
1514 /* This routine is called for each instruction to be assembled.  */
1515
1516 void
1517 md_assemble (str)
1518      char *str;
1519 {
1520   const struct s390_opcode *opcode;
1521   unsigned char insn[6];
1522   char *s;
1523
1524   /* Get the opcode.  */
1525   for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
1526     ;
1527   if (*s != '\0')
1528     *s++ = '\0';
1529
1530   /* Look up the opcode in the hash table.  */
1531   opcode = (struct s390_opcode *) hash_find (s390_opcode_hash, str);
1532   if (opcode == (const struct s390_opcode *) NULL)
1533     {
1534       as_bad (_("Unrecognized opcode: `%s'"), str);
1535       return;
1536     }
1537   else if (!(opcode->modes & current_mode_mask))
1538     {
1539       as_bad ("Opcode %s not available in this mode", str);
1540       return;
1541     }
1542   memcpy (insn, opcode->opcode, sizeof (insn));
1543   md_gather_operands (s, insn, opcode);
1544 }
1545
1546 #ifndef WORKING_DOT_WORD
1547 /* Handle long and short jumps. We don't support these */
1548 void
1549 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1550      char *ptr;
1551      addressT from_addr, to_addr;
1552      fragS *frag;
1553      symbolS *to_symbol;
1554 {
1555   abort ();
1556 }
1557
1558 void
1559 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1560      char *ptr;
1561      addressT from_addr, to_addr;
1562      fragS *frag;
1563      symbolS *to_symbol;
1564 {
1565   abort ();
1566 }
1567 #endif
1568
1569 void
1570 s390_bss (ignore)
1571      int ignore ATTRIBUTE_UNUSED;
1572 {
1573   /* We don't support putting frags in the BSS segment, we fake it
1574      by marking in_bss, then looking at s_skip for clues.  */
1575
1576   subseg_set (bss_section, 0);
1577   demand_empty_rest_of_line ();
1578 }
1579
1580 /* Pseudo-op handling.  */
1581
1582 void
1583 s390_insn (ignore)
1584      int ignore ATTRIBUTE_UNUSED;
1585 {
1586   expressionS exp;
1587   const struct s390_opcode *opformat;
1588   unsigned char insn[6];
1589   char *s;
1590
1591   /* Get the opcode format.  */
1592   s = input_line_pointer;
1593   while (*s != '\0' && *s != ',' && ! ISSPACE (*s))
1594     s++;
1595   if (*s != ',')
1596     as_bad (_("Invalid .insn format\n"));
1597   *s++ = '\0';
1598
1599   /* Look up the opcode in the hash table.  */
1600   opformat = (struct s390_opcode *)
1601     hash_find (s390_opformat_hash, input_line_pointer);
1602   if (opformat == (const struct s390_opcode *) NULL)
1603     {
1604       as_bad (_("Unrecognized opcode format: `%s'"), input_line_pointer);
1605       return;
1606     }
1607   input_line_pointer = s;
1608   expression (&exp);
1609   if (exp.X_op == O_constant)
1610     {
1611       if (   (opformat->oplen == 6 && exp.X_op > 0 && exp.X_op < (1ULL << 48))
1612           || (opformat->oplen == 4 && exp.X_op > 0 && exp.X_op < (1ULL << 32))
1613           || (opformat->oplen == 2 && exp.X_op > 0 && exp.X_op < (1ULL << 16)))
1614         md_number_to_chars (insn, exp.X_add_number, opformat->oplen);
1615       else
1616         as_bad (_("Invalid .insn format\n"));
1617     }
1618   else if (exp.X_op == O_big)
1619     {
1620       if (exp.X_add_number > 0
1621           && opformat->oplen == 6
1622           && generic_bignum[3] == 0)
1623         {
1624           md_number_to_chars (insn, generic_bignum[2], 2);
1625           md_number_to_chars (&insn[2], generic_bignum[1], 2);
1626           md_number_to_chars (&insn[4], generic_bignum[0], 2);
1627         }
1628       else
1629         as_bad (_("Invalid .insn format\n"));
1630     }
1631   else
1632     as_bad (_("second operand of .insn not a constant\n"));
1633
1634   if (strcmp (opformat->name, "e") != 0 && *input_line_pointer++ != ',')
1635     as_bad (_("missing comma after insn constant\n"));
1636
1637   if ((s = strchr (input_line_pointer, '\n')) != NULL)
1638     *s = '\0';
1639   input_line_pointer = md_gather_operands (input_line_pointer, insn,
1640                                            opformat);
1641   if (s != NULL)
1642     *s = '\n';
1643   demand_empty_rest_of_line ();
1644 }
1645
1646 /* The .byte pseudo-op.  This is similar to the normal .byte
1647    pseudo-op, but it can also take a single ASCII string.  */
1648
1649 static void
1650 s390_byte (ignore)
1651      int ignore ATTRIBUTE_UNUSED;
1652 {
1653   if (*input_line_pointer != '\"')
1654     {
1655       cons (1);
1656       return;
1657     }
1658
1659   /* Gather characters.  A real double quote is doubled.  Unusual
1660      characters are not permitted.  */
1661   ++input_line_pointer;
1662   while (1)
1663     {
1664       char c;
1665
1666       c = *input_line_pointer++;
1667
1668       if (c == '\"')
1669         {
1670           if (*input_line_pointer != '\"')
1671             break;
1672           ++input_line_pointer;
1673         }
1674
1675       FRAG_APPEND_1_CHAR (c);
1676     }
1677
1678   demand_empty_rest_of_line ();
1679 }
1680
1681 /* The .ltorg pseudo-op.This emits all literals defined since the last
1682    .ltorg or the invocation of gas. Literals are defined with the
1683    @lit suffix.  */
1684
1685 static void
1686 s390_literals (ignore)
1687      int ignore ATTRIBUTE_UNUSED;
1688 {
1689   struct s390_lpe *lpe;
1690
1691   if (lp_sym == NULL || lpe_count == 0)
1692     return;     /* Nothing to be done.  */
1693
1694   /* Emit symbol for start of literal pool.  */
1695   S_SET_SEGMENT (lp_sym, now_seg);
1696   S_SET_VALUE (lp_sym, (valueT) frag_now_fix ());
1697   lp_sym->sy_frag = frag_now;
1698
1699   while (lpe_list)
1700     {
1701       lpe = lpe_list;
1702       lpe_list = lpe_list->next;
1703       S_SET_SEGMENT (lpe->sym, now_seg);
1704       S_SET_VALUE (lpe->sym, (valueT) frag_now_fix ());
1705       lpe->sym->sy_frag = frag_now;
1706
1707       /* Emit literal pool entry.  */
1708       if (lpe->reloc != BFD_RELOC_UNUSED)
1709         {
1710           reloc_howto_type *reloc_howto =
1711             bfd_reloc_type_lookup (stdoutput, lpe->reloc);
1712           int size = bfd_get_reloc_size (reloc_howto);
1713           char *where;
1714
1715           if (size > lpe->nbytes)
1716             as_bad (_("%s relocations do not fit in %d bytes"),
1717                     reloc_howto->name, lpe->nbytes);
1718           where = frag_more (lpe->nbytes);
1719           md_number_to_chars (where, 0, size);
1720           fix_new_exp (frag_now, where - frag_now->fr_literal,
1721                        size, &lpe->ex, reloc_howto->pc_relative, lpe->reloc);
1722         }
1723       else
1724         {
1725           if (lpe->ex.X_op == O_big)
1726             {
1727               if (lpe->ex.X_add_number <= 0)
1728                 generic_floating_point_number = lpe->floatnum;
1729               else
1730                 memcpy (generic_bignum, lpe->bignum,
1731                         lpe->ex.X_add_number * sizeof (LITTLENUM_TYPE));
1732             }
1733           emit_expr (&lpe->ex, lpe->nbytes);
1734         }
1735
1736       lpe->next = lpe_free_list;
1737       lpe_free_list = lpe;
1738     }
1739   lpe_list_tail = NULL;
1740   lp_sym = NULL;
1741   lp_count++;
1742   lpe_count = 0;
1743 }
1744
1745 /* Turn a string in input_line_pointer into a floating point constant
1746    of type type, and store the appropriate bytes in *litp.  The number
1747    of LITTLENUMS emitted is stored in *sizep .  An error message is
1748    returned, or NULL on OK.  */
1749
1750 char *
1751 md_atof (type, litp, sizep)
1752      int type;
1753      char *litp;
1754      int *sizep;
1755 {
1756   int prec;
1757   LITTLENUM_TYPE words[4];
1758   char *t;
1759   int i;
1760
1761   switch (type)
1762     {
1763     case 'f':
1764       prec = 2;
1765       break;
1766
1767     case 'd':
1768       prec = 4;
1769       break;
1770
1771     default:
1772       *sizep = 0;
1773       return "bad call to md_atof";
1774     }
1775
1776   t = atof_ieee (input_line_pointer, type, words);
1777   if (t)
1778     input_line_pointer = t;
1779
1780   *sizep = prec * 2;
1781
1782   for (i = 0; i < prec; i++)
1783     {
1784       md_number_to_chars (litp, (valueT) words[i], 2);
1785       litp += 2;
1786     }
1787
1788   return NULL;
1789 }
1790
1791 /* Align a section (I don't know why this is machine dependent).  */
1792
1793 valueT
1794 md_section_align (seg, addr)
1795      asection *seg;
1796      valueT addr;
1797 {
1798   int align = bfd_get_section_alignment (stdoutput, seg);
1799
1800   return ((addr + (1 << align) - 1) & (-1 << align));
1801 }
1802
1803 /* We don't have any form of relaxing.  */
1804
1805 int
1806 md_estimate_size_before_relax (fragp, seg)
1807      fragS *fragp ATTRIBUTE_UNUSED;
1808      asection *seg ATTRIBUTE_UNUSED;
1809 {
1810   abort ();
1811   return 0;
1812 }
1813
1814 /* Convert a machine dependent frag.  We never generate these.  */
1815
1816 void
1817 md_convert_frag (abfd, sec, fragp)
1818      bfd *abfd ATTRIBUTE_UNUSED;
1819      asection *sec ATTRIBUTE_UNUSED;
1820      fragS *fragp ATTRIBUTE_UNUSED;
1821 {
1822   abort ();
1823 }
1824
1825 symbolS *
1826 md_undefined_symbol (name)
1827      char *name;
1828 {
1829   if (*name == '_' && *(name + 1) == 'G'
1830       && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
1831     {
1832       if (!GOT_symbol)
1833         {
1834           if (symbol_find (name))
1835             as_bad (_("GOT already in symbol table"));
1836           GOT_symbol = symbol_new (name, undefined_section,
1837                                    (valueT) 0, &zero_address_frag);
1838         }
1839       return GOT_symbol;
1840     }
1841   return 0;
1842 }
1843
1844 /* Functions concerning relocs.  */
1845
1846 /* The location from which a PC relative jump should be calculated,
1847    given a PC relative reloc.  */
1848
1849 long
1850 md_pcrel_from_section (fixp, sec)
1851      fixS *fixp;
1852      segT sec ATTRIBUTE_UNUSED;
1853 {
1854   return fixp->fx_frag->fr_address + fixp->fx_where;
1855 }
1856
1857 /* Here we decide which fixups can be adjusted to make them relative to
1858    the beginning of the section instead of the symbol.  Basically we need
1859    to make sure that the dynamic relocations are done correctly, so in
1860    some cases we force the original symbol to be used.  */
1861 int
1862 tc_s390_fix_adjustable (fixP)
1863      fixS *fixP;
1864 {
1865   /* Don't adjust references to merge sections.  */
1866   if ((S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0)
1867     return 0;
1868   /* adjust_reloc_syms doesn't know about the GOT.  */
1869   if (   fixP->fx_r_type == BFD_RELOC_16_GOTOFF
1870       || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
1871       || fixP->fx_r_type == BFD_RELOC_390_GOTOFF64
1872       || fixP->fx_r_type == BFD_RELOC_390_PLTOFF16
1873       || fixP->fx_r_type == BFD_RELOC_390_PLTOFF32
1874       || fixP->fx_r_type == BFD_RELOC_390_PLTOFF64
1875       || fixP->fx_r_type == BFD_RELOC_390_PLT16DBL
1876       || fixP->fx_r_type == BFD_RELOC_390_PLT32
1877       || fixP->fx_r_type == BFD_RELOC_390_PLT32DBL
1878       || fixP->fx_r_type == BFD_RELOC_390_PLT64
1879       || fixP->fx_r_type == BFD_RELOC_390_GOT12
1880       || fixP->fx_r_type == BFD_RELOC_390_GOT20
1881       || fixP->fx_r_type == BFD_RELOC_390_GOT16
1882       || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
1883       || fixP->fx_r_type == BFD_RELOC_390_GOT64
1884       || fixP->fx_r_type == BFD_RELOC_390_GOTENT
1885       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT12
1886       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT16
1887       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT20
1888       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT32
1889       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT64
1890       || fixP->fx_r_type == BFD_RELOC_390_GOTPLTENT
1891       || fixP->fx_r_type == BFD_RELOC_390_TLS_LOAD
1892       || fixP->fx_r_type == BFD_RELOC_390_TLS_GDCALL
1893       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDCALL
1894       || fixP->fx_r_type == BFD_RELOC_390_TLS_GD32
1895       || fixP->fx_r_type == BFD_RELOC_390_TLS_GD64
1896       || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE12
1897       || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE20
1898       || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE32
1899       || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE64
1900       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDM32
1901       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDM64
1902       || fixP->fx_r_type == BFD_RELOC_390_TLS_IE32
1903       || fixP->fx_r_type == BFD_RELOC_390_TLS_IE64
1904       || fixP->fx_r_type == BFD_RELOC_390_TLS_IEENT
1905       || fixP->fx_r_type == BFD_RELOC_390_TLS_LE32
1906       || fixP->fx_r_type == BFD_RELOC_390_TLS_LE64
1907       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDO32
1908       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDO64
1909       || fixP->fx_r_type == BFD_RELOC_390_TLS_DTPMOD
1910       || fixP->fx_r_type == BFD_RELOC_390_TLS_DTPOFF
1911       || fixP->fx_r_type == BFD_RELOC_390_TLS_TPOFF
1912       || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1913       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1914     return 0;
1915   return 1;
1916 }
1917
1918 /* Return true if we must always emit a reloc for a type and false if
1919    there is some hope of resolving it at assembly time.  */
1920 int
1921 tc_s390_force_relocation (fixp)
1922      struct fix *fixp;
1923 {
1924   /* Ensure we emit a relocation for every reference to the global
1925      offset table or to the procedure link table.  */
1926   switch (fixp->fx_r_type)
1927     {
1928     case BFD_RELOC_390_GOT12:
1929     case BFD_RELOC_390_GOT20:
1930     case BFD_RELOC_32_GOT_PCREL:
1931     case BFD_RELOC_32_GOTOFF:
1932     case BFD_RELOC_390_GOTOFF64:
1933     case BFD_RELOC_390_PLTOFF16:
1934     case BFD_RELOC_390_PLTOFF32:
1935     case BFD_RELOC_390_PLTOFF64:
1936     case BFD_RELOC_390_GOTPC:
1937     case BFD_RELOC_390_GOT16:
1938     case BFD_RELOC_390_GOTPCDBL:
1939     case BFD_RELOC_390_GOT64:
1940     case BFD_RELOC_390_GOTENT:
1941     case BFD_RELOC_390_PLT32:
1942     case BFD_RELOC_390_PLT16DBL:
1943     case BFD_RELOC_390_PLT32DBL:
1944     case BFD_RELOC_390_PLT64:
1945     case BFD_RELOC_390_GOTPLT12:
1946     case BFD_RELOC_390_GOTPLT16:
1947     case BFD_RELOC_390_GOTPLT20:
1948     case BFD_RELOC_390_GOTPLT32:
1949     case BFD_RELOC_390_GOTPLT64:
1950     case BFD_RELOC_390_GOTPLTENT:
1951       return 1;
1952     default:
1953       break;;
1954     }
1955
1956   return generic_force_reloc (fixp);
1957 }
1958
1959 /* Apply a fixup to the object code.  This is called for all the
1960    fixups we generated by the call to fix_new_exp, above.  In the call
1961    above we used a reloc code which was the largest legal reloc code
1962    plus the operand index.  Here we undo that to recover the operand
1963    index.  At this point all symbol values should be fully resolved,
1964    and we attempt to completely resolve the reloc.  If we can not do
1965    that, we determine the correct reloc code and put it back in the
1966    fixup.  */
1967
1968 void
1969 md_apply_fix3 (fixP, valP, seg)
1970      fixS *fixP;
1971      valueT *valP;
1972      segT seg ATTRIBUTE_UNUSED;
1973 {
1974   char *where;
1975   valueT value = *valP;
1976
1977   where = fixP->fx_frag->fr_literal + fixP->fx_where;
1978
1979   if (fixP->fx_subsy != NULL)
1980     as_bad_where (fixP->fx_file, fixP->fx_line,
1981                   "cannot emit relocation %s against subsy symbol %s",
1982                   bfd_get_reloc_code_name (fixP->fx_r_type),
1983                   S_GET_NAME (fixP->fx_subsy));
1984
1985   if (fixP->fx_addsy != NULL)
1986     {
1987       if (fixP->fx_pcrel)
1988         value += fixP->fx_frag->fr_address + fixP->fx_where;
1989     }
1990   else
1991     fixP->fx_done = 1;
1992
1993   if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
1994     {
1995       const struct s390_operand *operand;
1996       int opindex;
1997
1998       opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
1999       operand = &s390_operands[opindex];
2000
2001       if (fixP->fx_done)
2002         {
2003           /* Insert the fully resolved operand value.  */
2004           s390_insert_operand (where, operand, (offsetT) value,
2005                                fixP->fx_file, fixP->fx_line);
2006           return;
2007         }
2008
2009       /* Determine a BFD reloc value based on the operand information.
2010          We are only prepared to turn a few of the operands into
2011          relocs.  */
2012       fixP->fx_offset = value;
2013       if (operand->bits == 12 && operand->shift == 20)
2014         {
2015           fixP->fx_size = 2;
2016           fixP->fx_where += 2;
2017           fixP->fx_r_type = BFD_RELOC_390_12;
2018         }
2019       else if (operand->bits == 12 && operand->shift == 36)
2020         {
2021           fixP->fx_size = 2;
2022           fixP->fx_where += 4;
2023           fixP->fx_r_type = BFD_RELOC_390_12;
2024         }
2025       else if (operand->bits == 20 && operand->shift == 20)
2026         {
2027           fixP->fx_size = 2;
2028           fixP->fx_where += 2;
2029           fixP->fx_r_type = BFD_RELOC_390_20;
2030         }
2031       else if (operand->bits == 8 && operand->shift == 8)
2032         {
2033           fixP->fx_size = 1;
2034           fixP->fx_where += 1;
2035           fixP->fx_r_type = BFD_RELOC_8;
2036         }
2037       else if (operand->bits == 16 && operand->shift == 16)
2038         {
2039           fixP->fx_size = 2;
2040           fixP->fx_where += 2;
2041           if (operand->flags & S390_OPERAND_PCREL)
2042             {
2043               fixP->fx_r_type = BFD_RELOC_390_PC16DBL;
2044               fixP->fx_offset += 2;
2045             }
2046           else
2047             fixP->fx_r_type = BFD_RELOC_16;
2048         }
2049       else if (operand->bits == 32 && operand->shift == 16
2050                && (operand->flags & S390_OPERAND_PCREL))
2051         {
2052           fixP->fx_size = 4;
2053           fixP->fx_where += 2;
2054           fixP->fx_offset += 2;
2055           fixP->fx_r_type = BFD_RELOC_390_PC32DBL;
2056         }
2057       else
2058         {
2059           char *sfile;
2060           unsigned int sline;
2061
2062           /* Use expr_symbol_where to see if this is an expression
2063              symbol.  */
2064           if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
2065             as_bad_where (fixP->fx_file, fixP->fx_line,
2066                           _("unresolved expression that must be resolved"));
2067           else
2068             as_bad_where (fixP->fx_file, fixP->fx_line,
2069                           _("unsupported relocation type"));
2070           fixP->fx_done = 1;
2071           return;
2072         }
2073     }
2074   else
2075     {
2076       switch (fixP->fx_r_type)
2077         {
2078         case BFD_RELOC_8:
2079           if (fixP->fx_pcrel)
2080             abort ();
2081           if (fixP->fx_done)
2082             md_number_to_chars (where, value, 1);
2083           break;
2084         case BFD_RELOC_390_12:
2085         case BFD_RELOC_390_GOT12:
2086         case BFD_RELOC_390_GOTPLT12:
2087           if (fixP->fx_done)
2088             {
2089               unsigned short mop;
2090
2091               mop = bfd_getb16 ((unsigned char *) where);
2092               mop |= (unsigned short) (value & 0xfff);
2093               bfd_putb16 ((bfd_vma) mop, (unsigned char *) where);
2094             }
2095           break;
2096
2097         case BFD_RELOC_390_20:
2098         case BFD_RELOC_390_GOT20:
2099         case BFD_RELOC_390_GOTPLT20:
2100           if (fixP->fx_done)
2101             {
2102               unsigned int mop;
2103               mop = bfd_getb32 ((unsigned char *) where);
2104               mop |= (unsigned int) ((value & 0xfff) << 8 |
2105                                      (value & 0xff000) >> 12);
2106               bfd_putb32 ((bfd_vma) mop, (unsigned char *) where);
2107             } 
2108           break;
2109
2110         case BFD_RELOC_16:
2111         case BFD_RELOC_GPREL16:
2112         case BFD_RELOC_16_GOT_PCREL:
2113         case BFD_RELOC_16_GOTOFF:
2114           if (fixP->fx_pcrel)
2115             as_bad_where (fixP->fx_file, fixP->fx_line,
2116                           "cannot emit PC relative %s relocation%s%s",
2117                           bfd_get_reloc_code_name (fixP->fx_r_type),
2118                           fixP->fx_addsy != NULL ? " against " : "",
2119                           (fixP->fx_addsy != NULL
2120                            ? S_GET_NAME (fixP->fx_addsy)
2121                            : ""));
2122           if (fixP->fx_done)
2123             md_number_to_chars (where, value, 2);
2124           break;
2125         case BFD_RELOC_390_GOT16:
2126         case BFD_RELOC_390_PLTOFF16:
2127         case BFD_RELOC_390_GOTPLT16:
2128           if (fixP->fx_done)
2129             md_number_to_chars (where, value, 2);
2130           break;
2131         case BFD_RELOC_390_PC16DBL:
2132         case BFD_RELOC_390_PLT16DBL:
2133           value += 2;
2134           if (fixP->fx_done)
2135             md_number_to_chars (where, (offsetT) value >> 1, 2);
2136           break;
2137
2138         case BFD_RELOC_32:
2139           if (fixP->fx_pcrel)
2140             fixP->fx_r_type = BFD_RELOC_32_PCREL;
2141           else
2142             fixP->fx_r_type = BFD_RELOC_32;
2143           if (fixP->fx_done)
2144             md_number_to_chars (where, value, 4);
2145           break;
2146         case BFD_RELOC_32_PCREL:
2147         case BFD_RELOC_32_BASEREL:
2148           fixP->fx_r_type = BFD_RELOC_32_PCREL;
2149           if (fixP->fx_done)
2150             md_number_to_chars (where, value, 4);
2151           break;
2152         case BFD_RELOC_32_GOT_PCREL:
2153         case BFD_RELOC_390_PLTOFF32:
2154         case BFD_RELOC_390_PLT32:
2155         case BFD_RELOC_390_GOTPLT32:
2156           if (fixP->fx_done)
2157             md_number_to_chars (where, value, 4);
2158           break;
2159         case BFD_RELOC_390_PC32DBL:
2160         case BFD_RELOC_390_PLT32DBL:
2161         case BFD_RELOC_390_GOTPCDBL:
2162         case BFD_RELOC_390_GOTENT:
2163         case BFD_RELOC_390_GOTPLTENT:
2164           value += 2;
2165           if (fixP->fx_done)
2166             md_number_to_chars (where, (offsetT) value >> 1, 4);
2167           break;
2168
2169         case BFD_RELOC_32_GOTOFF:
2170           if (fixP->fx_done)
2171             md_number_to_chars (where, value, sizeof (int));
2172           break;
2173
2174         case BFD_RELOC_390_GOTOFF64:
2175           if (fixP->fx_done)
2176             md_number_to_chars (where, value, 8);
2177           break;
2178
2179         case BFD_RELOC_390_GOT64:
2180         case BFD_RELOC_390_PLTOFF64:
2181         case BFD_RELOC_390_PLT64:
2182         case BFD_RELOC_390_GOTPLT64:
2183           if (fixP->fx_done)
2184             md_number_to_chars (where, value, 8);
2185           break;
2186
2187         case BFD_RELOC_64:
2188           if (fixP->fx_pcrel)
2189             fixP->fx_r_type = BFD_RELOC_64_PCREL;
2190           else
2191             fixP->fx_r_type = BFD_RELOC_64;
2192           if (fixP->fx_done)
2193             md_number_to_chars (where, value, 8);
2194           break;
2195
2196         case BFD_RELOC_64_PCREL:
2197           fixP->fx_r_type = BFD_RELOC_64_PCREL;
2198           if (fixP->fx_done)
2199             md_number_to_chars (where, value, 8);
2200           break;
2201
2202         case BFD_RELOC_VTABLE_INHERIT:
2203         case BFD_RELOC_VTABLE_ENTRY:
2204           fixP->fx_done = 0;
2205           return;
2206
2207         case BFD_RELOC_390_TLS_LOAD:
2208         case BFD_RELOC_390_TLS_GDCALL:
2209         case BFD_RELOC_390_TLS_LDCALL:
2210         case BFD_RELOC_390_TLS_GD32:
2211         case BFD_RELOC_390_TLS_GD64:
2212         case BFD_RELOC_390_TLS_GOTIE12:
2213         case BFD_RELOC_390_TLS_GOTIE20:
2214         case BFD_RELOC_390_TLS_GOTIE32:
2215         case BFD_RELOC_390_TLS_GOTIE64:
2216         case BFD_RELOC_390_TLS_LDM32:
2217         case BFD_RELOC_390_TLS_LDM64:
2218         case BFD_RELOC_390_TLS_IE32:
2219         case BFD_RELOC_390_TLS_IE64:
2220         case BFD_RELOC_390_TLS_LE32:
2221         case BFD_RELOC_390_TLS_LE64:
2222         case BFD_RELOC_390_TLS_LDO32:
2223         case BFD_RELOC_390_TLS_LDO64:
2224         case BFD_RELOC_390_TLS_DTPMOD:
2225         case BFD_RELOC_390_TLS_DTPOFF:
2226         case BFD_RELOC_390_TLS_TPOFF:
2227           /* Fully resolved at link time.  */
2228           break;
2229         case BFD_RELOC_390_TLS_IEENT:
2230           /* Fully resolved at link time.  */
2231           value += 2;
2232           break;
2233
2234         default:
2235           {
2236             const char *reloc_name = bfd_get_reloc_code_name (fixP->fx_r_type);
2237
2238             if (reloc_name != NULL)
2239               fprintf (stderr, "Gas failure, reloc type %s\n", reloc_name);
2240             else
2241               fprintf (stderr, "Gas failure, reloc type #%i\n", fixP->fx_r_type);
2242             fflush (stderr);
2243             abort ();
2244           }
2245         }
2246
2247       fixP->fx_offset = value;
2248     }
2249 }
2250
2251 /* Generate a reloc for a fixup.  */
2252
2253 arelent *
2254 tc_gen_reloc (seg, fixp)
2255      asection *seg ATTRIBUTE_UNUSED;
2256      fixS *fixp;
2257 {
2258   bfd_reloc_code_real_type code;
2259   arelent *reloc;
2260
2261   code = fixp->fx_r_type;
2262   if (GOT_symbol && fixp->fx_addsy == GOT_symbol)
2263     {
2264       if (   (s390_arch_size == 32 && code == BFD_RELOC_32_PCREL)
2265           || (s390_arch_size == 64 && code == BFD_RELOC_64_PCREL))
2266         code = BFD_RELOC_390_GOTPC;
2267       if (code == BFD_RELOC_390_PC32DBL)
2268         code = BFD_RELOC_390_GOTPCDBL;
2269     }
2270
2271   reloc = (arelent *) xmalloc (sizeof (arelent));
2272   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2273   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2274   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2275   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2276   if (reloc->howto == NULL)
2277     {
2278       as_bad_where (fixp->fx_file, fixp->fx_line,
2279                     _("cannot represent relocation type %s"),
2280                     bfd_get_reloc_code_name (code));
2281       /* Set howto to a garbage value so that we can keep going.  */
2282       reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2283       assert (reloc->howto != NULL);
2284     }
2285   reloc->addend = fixp->fx_offset;
2286
2287   return reloc;
2288 }