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