* config/tc-s390.c (s390_insn): Correct range check for opcode in
[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 #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 void
544 s390_align_code (fragP, count)
545      fragS *fragP;
546      int count;
547 {
548   /* We use nop pattern 0x0707.  */
549   if (count > 0)
550     {
551       memset (fragP->fr_literal + fragP->fr_fix, 0x07, count);
552       fragP->fr_var = count;
553     }
554 }
555
556 /* Insert an operand value into an instruction.  */
557
558 static void
559 s390_insert_operand (insn, operand, val, file, line)
560      unsigned char *insn;
561      const struct s390_operand *operand;
562      offsetT val;
563      char *file;
564      unsigned int line;
565 {
566   addressT uval;
567   int offset;
568
569   if (operand->flags & (S390_OPERAND_SIGNED|S390_OPERAND_PCREL))
570     {
571       offsetT min, max;
572
573       max = ((offsetT) 1 << (operand->bits - 1)) - 1;
574       min = - ((offsetT) 1 << (operand->bits - 1));
575       /* Halve PCREL operands.  */
576       if (operand->flags & S390_OPERAND_PCREL)
577         val >>= 1;
578       /* Check for underflow / overflow.  */
579       if (val < min || val > max)
580         {
581           const char *err =
582             "operand out of range (%s not between %ld and %ld)";
583           char buf[100];
584
585           if (operand->flags & S390_OPERAND_PCREL)
586             {
587               val <<= 1;
588               min <<= 1;
589               max <<= 1;
590             }
591           sprint_value (buf, val);
592           if (file == (char *) NULL)
593             as_bad (err, buf, (int) min, (int) max);
594           else
595             as_bad_where (file, line, err, buf, (int) min, (int) max);
596           return;
597         }
598       /* val is ok, now restrict it to operand->bits bits.  */
599       uval = (addressT) val & ((((addressT) 1 << (operand->bits-1)) << 1) - 1);
600       /* val is restrict, now check for special case.  */
601       if (operand->bits == 20 && operand->shift == 20)
602         uval = (uval >> 12) | ((uval & 0xfff) << 8);
603     }
604   else
605     {
606       addressT min, max;
607
608       max = (((addressT) 1 << (operand->bits - 1)) << 1) - 1;
609       min = (offsetT) 0;
610       uval = (addressT) val;
611       /* Length x in an instructions has real length x+1.  */
612       if (operand->flags & S390_OPERAND_LENGTH)
613         uval--;
614       /* Check for underflow / overflow.  */
615       if (uval < min || uval > max)
616         {
617           const char *err =
618             "operand out of range (%s not between %ld and %ld)";
619           char buf[100];
620
621           if (operand->flags & S390_OPERAND_LENGTH)
622             {
623               uval++;
624               min++;
625               max++;
626             }
627           sprint_value (buf, uval);
628           if (file == (char *) NULL)
629             as_bad (err, buf, (int) min, (int) max);
630           else
631             as_bad_where (file, line, err, buf, (int) min, (int) max);
632           return;
633         }
634     }
635
636   /* Insert fragments of the operand byte for byte.  */
637   offset = operand->shift + operand->bits;
638   uval <<= (-offset) & 7;
639   insn += (offset - 1) / 8;
640   while (uval != 0)
641     {
642       *insn-- |= uval;
643       uval >>= 8;
644     }
645 }
646
647 struct map_tls
648   {
649     char *string;
650     int length;
651     bfd_reloc_code_real_type reloc;
652   };
653
654 static bfd_reloc_code_real_type s390_tls_suffix
655   PARAMS ((char **, expressionS *));
656
657 /* Parse tls marker and return the desired relocation.  */
658 static bfd_reloc_code_real_type
659 s390_tls_suffix (str_p, exp_p)
660      char **str_p;
661      expressionS *exp_p;
662 {
663   static struct map_tls mapping[] =
664   {
665     { "tls_load", 8, BFD_RELOC_390_TLS_LOAD },
666     { "tls_gdcall", 10, BFD_RELOC_390_TLS_GDCALL  },
667     { "tls_ldcall", 10, BFD_RELOC_390_TLS_LDCALL  },
668     { NULL,  0, BFD_RELOC_UNUSED }
669   };
670   struct map_tls *ptr;
671   char *orig_line;
672   char *str;
673   char *ident;
674   int len;
675
676   str = *str_p;
677   if (*str++ != ':')
678     return BFD_RELOC_UNUSED;
679
680   ident = str;
681   while (ISIDNUM (*str))
682     str++;
683   len = str - ident;
684   if (*str++ != ':')
685     return BFD_RELOC_UNUSED;
686
687   orig_line = input_line_pointer;
688   input_line_pointer = str;
689   expression (exp_p);
690   str = input_line_pointer;
691   if (&input_line_pointer != str_p)
692     input_line_pointer = orig_line;
693
694   if (exp_p->X_op != O_symbol)
695     return BFD_RELOC_UNUSED;
696
697   for (ptr = &mapping[0]; ptr->length > 0; ptr++)
698     if (len == ptr->length
699         && strncasecmp (ident, ptr->string, ptr->length) == 0)
700       {
701         /* Found a matching tls suffix.  */
702         *str_p = str;
703         return ptr->reloc;
704       }
705   return BFD_RELOC_UNUSED;
706 }
707
708 /* Structure used to hold suffixes.  */
709 typedef enum
710   {
711     ELF_SUFFIX_NONE = 0,
712     ELF_SUFFIX_GOT,
713     ELF_SUFFIX_PLT,
714     ELF_SUFFIX_GOTENT,
715     ELF_SUFFIX_GOTOFF,
716     ELF_SUFFIX_GOTPLT,
717     ELF_SUFFIX_PLTOFF,
718     ELF_SUFFIX_TLS_GD,
719     ELF_SUFFIX_TLS_GOTIE,
720     ELF_SUFFIX_TLS_IE,
721     ELF_SUFFIX_TLS_LDM,
722     ELF_SUFFIX_TLS_LDO,
723     ELF_SUFFIX_TLS_LE
724   }
725 elf_suffix_type;
726
727 struct map_bfd
728   {
729     char *string;
730     int length;
731     elf_suffix_type suffix;
732   };
733
734 static elf_suffix_type s390_elf_suffix PARAMS ((char **, expressionS *));
735 static int s390_exp_compare PARAMS ((expressionS *exp1, expressionS *exp2));
736 static elf_suffix_type s390_lit_suffix
737   PARAMS ((char **, expressionS *, elf_suffix_type));
738
739
740 /* Parse @got/@plt/@gotoff. and return the desired relocation.  */
741 static elf_suffix_type
742 s390_elf_suffix (str_p, exp_p)
743      char **str_p;
744      expressionS *exp_p;
745 {
746   static struct map_bfd mapping[] =
747   {
748     { "got", 3, ELF_SUFFIX_GOT  },
749     { "got12", 5, ELF_SUFFIX_GOT  },
750     { "plt", 3, ELF_SUFFIX_PLT  },
751     { "gotent", 6, ELF_SUFFIX_GOTENT },
752     { "gotoff", 6, ELF_SUFFIX_GOTOFF },
753     { "gotplt", 6, ELF_SUFFIX_GOTPLT },
754     { "pltoff", 6, ELF_SUFFIX_PLTOFF },
755     { "tlsgd", 5, ELF_SUFFIX_TLS_GD },
756     { "gotntpoff", 9, ELF_SUFFIX_TLS_GOTIE },
757     { "indntpoff", 9, ELF_SUFFIX_TLS_IE },
758     { "tlsldm", 6, ELF_SUFFIX_TLS_LDM },
759     { "dtpoff", 6, ELF_SUFFIX_TLS_LDO },
760     { "ntpoff", 6, ELF_SUFFIX_TLS_LE },
761     { NULL,  0, ELF_SUFFIX_NONE }
762   };
763
764   struct map_bfd *ptr;
765   char *str = *str_p;
766   char *ident;
767   int len;
768
769   if (*str++ != '@')
770     return ELF_SUFFIX_NONE;
771
772   ident = str;
773   while (ISALNUM (*str))
774     str++;
775   len = str - ident;
776
777   for (ptr = &mapping[0]; ptr->length > 0; ptr++)
778     if (len == ptr->length
779         && strncasecmp (ident, ptr->string, ptr->length) == 0)
780       {
781         if (exp_p->X_add_number != 0)
782           as_warn (_("identifier+constant@%s means identifier@%s+constant"),
783                    ptr->string, ptr->string);
784         /* Now check for identifier@suffix+constant.  */
785         if (*str == '-' || *str == '+')
786           {
787             char *orig_line = input_line_pointer;
788             expressionS new_exp;
789
790             input_line_pointer = str;
791             expression (&new_exp);
792
793             switch (new_exp.X_op)
794               {
795               case O_constant: /* X_add_number (a constant expression).  */
796                 exp_p->X_add_number += new_exp.X_add_number;
797                 str = input_line_pointer;
798                 break;
799               case O_symbol:   /* X_add_symbol + X_add_number.  */
800                 /* this case is used for e.g. xyz@PLT+.Label.  */
801                 exp_p->X_add_number += new_exp.X_add_number;
802                 exp_p->X_op_symbol = new_exp.X_add_symbol;
803                 exp_p->X_op = O_add;
804                 str = input_line_pointer;
805                 break;
806               case O_uminus:   /* (- X_add_symbol) + X_add_number.  */
807                 /* this case is used for e.g. xyz@PLT-.Label.  */
808                 exp_p->X_add_number += new_exp.X_add_number;
809                 exp_p->X_op_symbol = new_exp.X_add_symbol;
810                 exp_p->X_op = O_subtract;
811                 str = input_line_pointer;
812                 break;
813               default:
814                 break;
815               }
816
817             /* If s390_elf_suffix has not been called with
818                &input_line_pointer as first parameter, we have
819                clobbered the input_line_pointer. We have to
820                undo that.  */
821             if (&input_line_pointer != str_p)
822               input_line_pointer = orig_line;
823           }
824         *str_p = str;
825         return ptr->suffix;
826       }
827
828   return BFD_RELOC_UNUSED;
829 }
830
831 /* Structure used to hold a literal pool entry.  */
832 struct s390_lpe
833   {
834     struct s390_lpe *next;
835     expressionS ex;
836     FLONUM_TYPE floatnum;     /* used if X_op == O_big && X_add_number <= 0 */
837     LITTLENUM_TYPE bignum[4]; /* used if X_op == O_big && X_add_number > 0  */
838     int nbytes;
839     bfd_reloc_code_real_type reloc;
840     symbolS *sym;
841   };
842
843 static struct s390_lpe *lpe_free_list = NULL;
844 static struct s390_lpe *lpe_list = NULL;
845 static struct s390_lpe *lpe_list_tail = NULL;
846 static symbolS *lp_sym = NULL;
847 static int lp_count = 0;
848 static int lpe_count = 0;
849
850 static int
851 s390_exp_compare (exp1, exp2)
852      expressionS *exp1;
853      expressionS *exp2;
854 {
855   if (exp1->X_op != exp2->X_op)
856     return 0;
857
858   switch (exp1->X_op)
859     {
860     case O_constant:   /* X_add_number must be equal.  */
861     case O_register:
862       return exp1->X_add_number == exp2->X_add_number;
863
864     case O_big:
865       as_bad (_("Can't handle O_big in s390_exp_compare"));
866
867     case O_symbol:     /* X_add_symbol & X_add_number must be equal.  */
868     case O_symbol_rva:
869     case O_uminus:
870     case O_bit_not:
871     case O_logical_not:
872       return (exp1->X_add_symbol == exp2->X_add_symbol)
873         &&   (exp1->X_add_number == exp2->X_add_number);
874
875     case O_multiply:   /* X_add_symbol,X_op_symbol&X_add_number must be equal.  */
876     case O_divide:
877     case O_modulus:
878     case O_left_shift:
879     case O_right_shift:
880     case O_bit_inclusive_or:
881     case O_bit_or_not:
882     case O_bit_exclusive_or:
883     case O_bit_and:
884     case O_add:
885     case O_subtract:
886     case O_eq:
887     case O_ne:
888     case O_lt:
889     case O_le:
890     case O_ge:
891     case O_gt:
892     case O_logical_and:
893     case O_logical_or:
894       return (exp1->X_add_symbol == exp2->X_add_symbol)
895         &&   (exp1->X_op_symbol  == exp2->X_op_symbol)
896         &&   (exp1->X_add_number == exp2->X_add_number);
897     default:
898       return 0;
899     }
900 }
901
902 /* Test for @lit and if its present make an entry in the literal pool and
903    modify the current expression to be an offset into the literal pool.  */
904 static elf_suffix_type
905 s390_lit_suffix (str_p, exp_p, suffix)
906      char **str_p;
907      expressionS *exp_p;
908      elf_suffix_type suffix;
909 {
910   bfd_reloc_code_real_type reloc;
911   char tmp_name[64];
912   char *str = *str_p;
913   char *ident;
914   struct s390_lpe *lpe;
915   int nbytes, len;
916
917   if (*str++ != ':')
918     return suffix;       /* No modification.  */
919
920   /* We look for a suffix of the form "@lit1", "@lit2", "@lit4" or "@lit8".  */
921   ident = str;
922   while (ISALNUM (*str))
923     str++;
924   len = str - ident;
925   if (len != 4 || strncasecmp (ident, "lit", 3) != 0
926       || (ident[3]!='1' && ident[3]!='2' && ident[3]!='4' && ident[3]!='8'))
927     return suffix;      /* no modification */
928   nbytes = ident[3] - '0';
929
930   reloc = BFD_RELOC_UNUSED;
931   if (suffix == ELF_SUFFIX_GOT)
932     {
933       if (nbytes == 2)
934         reloc = BFD_RELOC_390_GOT16;
935       else if (nbytes == 4)
936         reloc = BFD_RELOC_32_GOT_PCREL;
937       else if (nbytes == 8)
938         reloc = BFD_RELOC_390_GOT64;
939     }
940   else if (suffix == ELF_SUFFIX_PLT)
941     {
942       if (nbytes == 4)
943         reloc = BFD_RELOC_390_PLT32;
944       else if (nbytes == 8)
945         reloc = BFD_RELOC_390_PLT64;
946     }
947
948   if (suffix != ELF_SUFFIX_NONE && reloc == BFD_RELOC_UNUSED)
949     as_bad (_("Invalid suffix for literal pool entry"));
950
951   /* Search the pool if the new entry is a duplicate.  */
952   if (exp_p->X_op == O_big)
953     {
954       /* Special processing for big numbers.  */
955       for (lpe = lpe_list; lpe != NULL; lpe = lpe->next)
956         {
957           if (lpe->ex.X_op == O_big)
958             {
959               if (exp_p->X_add_number <= 0 && lpe->ex.X_add_number <= 0)
960                 {
961                   if (memcmp (&generic_floating_point_number, &lpe->floatnum,
962                               sizeof (FLONUM_TYPE)) == 0)
963                     break;
964                 }
965               else if (exp_p->X_add_number == lpe->ex.X_add_number)
966                 {
967                   if (memcmp (generic_bignum, lpe->bignum,
968                               sizeof (LITTLENUM_TYPE)*exp_p->X_add_number) == 0)
969                     break;
970                 }
971             }
972         }
973     }
974   else
975     {
976       /* Processing for 'normal' data types.  */
977       for (lpe = lpe_list; lpe != NULL; lpe = lpe->next)
978         if (lpe->nbytes == nbytes && lpe->reloc == reloc
979             && s390_exp_compare (exp_p, &lpe->ex) != 0)
980           break;
981     }
982
983   if (lpe == NULL)
984     {
985       /* A new literal.  */
986       if (lpe_free_list != NULL)
987         {
988           lpe = lpe_free_list;
989           lpe_free_list = lpe_free_list->next;
990         }
991       else
992         {
993           lpe = (struct s390_lpe *) xmalloc (sizeof (struct s390_lpe));
994         }
995
996       lpe->ex = *exp_p;
997
998       if (exp_p->X_op == O_big)
999         {
1000           if (exp_p->X_add_number <= 0)
1001             lpe->floatnum = generic_floating_point_number;
1002           else if (exp_p->X_add_number <= 4)
1003             memcpy (lpe->bignum, generic_bignum,
1004                     exp_p->X_add_number * sizeof (LITTLENUM_TYPE));
1005           else
1006             as_bad (_("Big number is too big"));
1007         }
1008
1009       lpe->nbytes = nbytes;
1010       lpe->reloc = reloc;
1011       /* Literal pool name defined ?  */
1012       if (lp_sym == NULL)
1013         {
1014           sprintf (tmp_name, ".L\001%i", lp_count);
1015           lp_sym = symbol_make (tmp_name);
1016         }
1017
1018       /* Make name for literal pool entry.  */
1019       sprintf (tmp_name, ".L\001%i\002%i", lp_count, lpe_count);
1020       lpe_count++;
1021       lpe->sym = symbol_make (tmp_name);
1022
1023       /* Add to literal pool list.  */
1024       lpe->next = NULL;
1025       if (lpe_list_tail != NULL)
1026         {
1027           lpe_list_tail->next = lpe;
1028           lpe_list_tail = lpe;
1029         }
1030       else
1031         lpe_list = lpe_list_tail = lpe;
1032     }
1033
1034   /* Now change exp_p to the offset into the literal pool.
1035      Thats the expression: .L^Ax^By-.L^Ax   */
1036   exp_p->X_add_symbol = lpe->sym;
1037   exp_p->X_op_symbol = lp_sym;
1038   exp_p->X_op = O_subtract;
1039   exp_p->X_add_number = 0;
1040
1041   *str_p = str;
1042
1043   /* We change the suffix type to ELF_SUFFIX_NONE, because
1044      the difference of two local labels is just a number.  */
1045   return ELF_SUFFIX_NONE;
1046 }
1047
1048 /* Like normal .long/.short/.word, except support @got, etc.
1049    clobbers input_line_pointer, checks end-of-line.  */
1050 static void
1051 s390_elf_cons (nbytes)
1052      register int nbytes;       /* 1=.byte, 2=.word, 4=.long */
1053 {
1054   expressionS exp;
1055   elf_suffix_type suffix;
1056
1057   if (is_it_end_of_statement ())
1058     {
1059       demand_empty_rest_of_line ();
1060       return;
1061     }
1062
1063   do
1064     {
1065       expression (&exp);
1066
1067       if (exp.X_op == O_symbol
1068           && *input_line_pointer == '@'
1069           && (suffix = s390_elf_suffix (&input_line_pointer, &exp)) != ELF_SUFFIX_NONE)
1070         {
1071           bfd_reloc_code_real_type reloc;
1072           reloc_howto_type *reloc_howto;
1073           int size;
1074           char *where;
1075
1076           if (nbytes == 2)
1077             {
1078               static bfd_reloc_code_real_type tab2[] =
1079                 {
1080                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_NONE  */
1081                   BFD_RELOC_390_GOT16,          /* ELF_SUFFIX_GOT  */
1082                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_PLT  */
1083                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_GOTENT  */
1084                   BFD_RELOC_16_GOTOFF,          /* ELF_SUFFIX_GOTOFF  */
1085                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_GOTPLT  */
1086                   BFD_RELOC_390_PLTOFF16,       /* ELF_SUFFIX_PLTOFF  */
1087                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_GD  */
1088                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_GOTIE  */
1089                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_IE  */
1090                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_LDM  */
1091                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_TLS_LDO  */
1092                   BFD_RELOC_UNUSED              /* ELF_SUFFIX_TLS_LE  */
1093                 };
1094               reloc = tab2[suffix];
1095             }
1096           else if (nbytes == 4)
1097             {
1098               static bfd_reloc_code_real_type tab4[] =
1099                 {
1100                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_NONE  */
1101                   BFD_RELOC_32_GOT_PCREL,       /* ELF_SUFFIX_GOT  */
1102                   BFD_RELOC_390_PLT32,          /* ELF_SUFFIX_PLT  */
1103                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_GOTENT  */
1104                   BFD_RELOC_32_GOTOFF,          /* ELF_SUFFIX_GOTOFF  */
1105                   BFD_RELOC_390_GOTPLT32,       /* ELF_SUFFIX_GOTPLT  */
1106                   BFD_RELOC_390_PLTOFF32,       /* ELF_SUFFIX_PLTOFF  */
1107                   BFD_RELOC_390_TLS_GD32,       /* ELF_SUFFIX_TLS_GD  */
1108                   BFD_RELOC_390_TLS_GOTIE32,    /* ELF_SUFFIX_TLS_GOTIE  */
1109                   BFD_RELOC_390_TLS_IE32,       /* ELF_SUFFIX_TLS_IE  */
1110                   BFD_RELOC_390_TLS_LDM32,      /* ELF_SUFFIX_TLS_LDM  */
1111                   BFD_RELOC_390_TLS_LDO32,      /* ELF_SUFFIX_TLS_LDO  */
1112                   BFD_RELOC_390_TLS_LE32        /* ELF_SUFFIX_TLS_LE  */
1113                 };
1114               reloc = tab4[suffix];
1115             }
1116           else if (nbytes == 8)
1117             {
1118               static bfd_reloc_code_real_type tab8[] =
1119                 {
1120                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_NONE  */
1121                   BFD_RELOC_390_GOT64,          /* ELF_SUFFIX_GOT  */
1122                   BFD_RELOC_390_PLT64,          /* ELF_SUFFIX_PLT  */
1123                   BFD_RELOC_UNUSED,             /* ELF_SUFFIX_GOTENT  */
1124                   BFD_RELOC_390_GOTOFF64,       /* ELF_SUFFIX_GOTOFF  */
1125                   BFD_RELOC_390_GOTPLT64,       /* ELF_SUFFIX_GOTPLT  */
1126                   BFD_RELOC_390_PLTOFF64,       /* ELF_SUFFIX_PLTOFF  */
1127                   BFD_RELOC_390_TLS_GD64,       /* ELF_SUFFIX_TLS_GD  */
1128                   BFD_RELOC_390_TLS_GOTIE64,    /* ELF_SUFFIX_TLS_GOTIE  */
1129                   BFD_RELOC_390_TLS_IE64,       /* ELF_SUFFIX_TLS_IE  */
1130                   BFD_RELOC_390_TLS_LDM64,      /* ELF_SUFFIX_TLS_LDM  */
1131                   BFD_RELOC_390_TLS_LDO64,      /* ELF_SUFFIX_TLS_LDO  */
1132                   BFD_RELOC_390_TLS_LE64        /* ELF_SUFFIX_TLS_LE  */
1133                 };
1134               reloc = tab8[suffix];
1135             }
1136           else
1137             reloc = BFD_RELOC_UNUSED;
1138
1139           if (reloc != BFD_RELOC_UNUSED
1140               && (reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc)))
1141             {
1142               size = bfd_get_reloc_size (reloc_howto);
1143               if (size > nbytes)
1144                 as_bad (_("%s relocations do not fit in %d bytes"),
1145                         reloc_howto->name, nbytes);
1146               where = frag_more (nbytes);
1147               md_number_to_chars (where, 0, size);
1148               /* To make fixup_segment do the pc relative conversion the
1149                  pcrel parameter on the fix_new_exp call needs to be FALSE.  */
1150               fix_new_exp (frag_now, where - frag_now->fr_literal,
1151                            size, &exp, FALSE, reloc);
1152             }
1153           else
1154             as_bad (_("relocation not applicable"));
1155         }
1156       else
1157         emit_expr (&exp, (unsigned int) nbytes);
1158     }
1159   while (*input_line_pointer++ == ',');
1160
1161   input_line_pointer--;         /* Put terminator back into stream.  */
1162   demand_empty_rest_of_line ();
1163 }
1164
1165 /* We need to keep a list of fixups.  We can't simply generate them as
1166    we go, because that would require us to first create the frag, and
1167    that would screw up references to ``.''.  */
1168
1169 struct s390_fixup
1170   {
1171     expressionS exp;
1172     int opindex;
1173     bfd_reloc_code_real_type reloc;
1174   };
1175
1176 #define MAX_INSN_FIXUPS (4)
1177
1178 /* This routine is called for each instruction to be assembled.  */
1179
1180 static char *
1181 md_gather_operands (str, insn, opcode)
1182      char *str;
1183      unsigned char *insn;
1184      const struct s390_opcode *opcode;
1185 {
1186   struct s390_fixup fixups[MAX_INSN_FIXUPS];
1187   const struct s390_operand *operand;
1188   const unsigned char *opindex_ptr;
1189   expressionS ex;
1190   elf_suffix_type suffix;
1191   bfd_reloc_code_real_type reloc;
1192   int skip_optional;
1193   int parentheses;
1194   char *f;
1195   int fc, i;
1196
1197   while (ISSPACE (*str))
1198     str++;
1199
1200   parentheses = 0;
1201   skip_optional = 0;
1202
1203   /* Gather the operands.  */
1204   fc = 0;
1205   for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1206     {
1207       char *hold;
1208
1209       operand = s390_operands + *opindex_ptr;
1210
1211       if (skip_optional && (operand->flags & S390_OPERAND_INDEX))
1212         {
1213           /* We do an early skip. For D(X,B) constructions the index
1214              register is skipped (X is optional). For D(L,B) the base
1215              register will be the skipped operand, because L is NOT
1216              optional.  */
1217           skip_optional = 0;
1218           continue;
1219         }
1220
1221       /* Gather the operand.  */
1222       hold = input_line_pointer;
1223       input_line_pointer = str;
1224
1225       /* Parse the operand.  */
1226       if (! register_name (&ex))
1227         expression (&ex);
1228
1229       str = input_line_pointer;
1230       input_line_pointer = hold;
1231
1232       /* Write the operand to the insn.  */
1233       if (ex.X_op == O_illegal)
1234         as_bad (_("illegal operand"));
1235       else if (ex.X_op == O_absent)
1236         as_bad (_("missing operand"));
1237       else if (ex.X_op == O_register || ex.X_op == O_constant)
1238         {
1239           s390_lit_suffix (&str, &ex, ELF_SUFFIX_NONE);
1240
1241           if (ex.X_op != O_register && ex.X_op != O_constant)
1242             {
1243               /* We need to generate a fixup for the
1244                  expression returned by s390_lit_suffix.  */
1245               if (fc >= MAX_INSN_FIXUPS)
1246                 as_fatal (_("too many fixups"));
1247               fixups[fc].exp = ex;
1248               fixups[fc].opindex = *opindex_ptr;
1249               fixups[fc].reloc = BFD_RELOC_UNUSED;
1250               ++fc;
1251             }
1252           else
1253             {
1254               if ((operand->flags & S390_OPERAND_INDEX)
1255                   && ex.X_add_number == 0
1256                   && warn_areg_zero)
1257                 as_warn ("index register specified but zero");
1258               if ((operand->flags & S390_OPERAND_BASE)
1259                   && ex.X_add_number == 0
1260                   && warn_areg_zero)
1261                 as_warn ("base register specified but zero");
1262               s390_insert_operand (insn, operand, ex.X_add_number, NULL, 0);
1263             }
1264         }
1265       else
1266         {
1267           suffix = s390_elf_suffix (&str, &ex);
1268           suffix = s390_lit_suffix (&str, &ex, suffix);
1269           reloc = BFD_RELOC_UNUSED;
1270
1271           if (suffix == ELF_SUFFIX_GOT)
1272             {
1273               if ((operand->flags & S390_OPERAND_DISP) &&
1274                   (operand->bits == 12))
1275                 reloc = BFD_RELOC_390_GOT12;
1276               else if ((operand->flags & S390_OPERAND_DISP) &&
1277                        (operand->bits == 20))
1278                 reloc = BFD_RELOC_390_GOT20;
1279               else if ((operand->flags & S390_OPERAND_SIGNED)
1280                        && (operand->bits == 16))
1281                 reloc = BFD_RELOC_390_GOT16;
1282               else if ((operand->flags & S390_OPERAND_PCREL)
1283                        && (operand->bits == 32))
1284                 reloc = BFD_RELOC_390_GOTENT;
1285             }
1286           else if (suffix == ELF_SUFFIX_PLT)
1287             {
1288               if ((operand->flags & S390_OPERAND_PCREL)
1289                   && (operand->bits == 16))
1290                 reloc = BFD_RELOC_390_PLT16DBL;
1291               else if ((operand->flags & S390_OPERAND_PCREL)
1292                        && (operand->bits == 32))
1293                 reloc = BFD_RELOC_390_PLT32DBL;
1294             }
1295           else if (suffix == ELF_SUFFIX_GOTENT)
1296             {
1297               if ((operand->flags & S390_OPERAND_PCREL)
1298                   && (operand->bits == 32))
1299                 reloc = BFD_RELOC_390_GOTENT;
1300             }
1301           else if (suffix == ELF_SUFFIX_GOTOFF)
1302             {
1303               if ((operand->flags & S390_OPERAND_SIGNED)
1304                   && (operand->bits == 16))
1305                 reloc = BFD_RELOC_16_GOTOFF;
1306             }
1307           else if (suffix == ELF_SUFFIX_PLTOFF)
1308             {
1309               if ((operand->flags & S390_OPERAND_SIGNED)
1310                   && (operand->bits == 16))
1311                 reloc = BFD_RELOC_390_PLTOFF16;
1312             }
1313           else if (suffix == ELF_SUFFIX_GOTPLT)
1314             {
1315               if ((operand->flags & S390_OPERAND_DISP)
1316                   && (operand->bits == 12))
1317                 reloc = BFD_RELOC_390_GOTPLT12;
1318               else if ((operand->flags & S390_OPERAND_SIGNED)
1319                        && (operand->bits == 16))
1320                 reloc = BFD_RELOC_390_GOTPLT16;
1321               else if ((operand->flags & S390_OPERAND_PCREL)
1322                        && (operand->bits == 32))
1323                 reloc = BFD_RELOC_390_GOTPLTENT;
1324             }
1325           else if (suffix == ELF_SUFFIX_TLS_GOTIE)
1326             {
1327               if ((operand->flags & S390_OPERAND_DISP)
1328                   && (operand->bits == 12))
1329                 reloc = BFD_RELOC_390_TLS_GOTIE12;
1330               else if ((operand->flags & S390_OPERAND_DISP)
1331                        && (operand->bits == 20))
1332                 reloc = BFD_RELOC_390_TLS_GOTIE20;
1333             }
1334           else if (suffix == ELF_SUFFIX_TLS_IE)
1335             {
1336               if ((operand->flags & S390_OPERAND_PCREL)
1337                        && (operand->bits == 32))
1338                 reloc = BFD_RELOC_390_TLS_IEENT;
1339             }
1340
1341           if (suffix != ELF_SUFFIX_NONE && reloc == BFD_RELOC_UNUSED)
1342             as_bad (_("invalid operand suffix"));
1343           /* We need to generate a fixup of type 'reloc' for this
1344              expression.  */
1345           if (fc >= MAX_INSN_FIXUPS)
1346             as_fatal (_("too many fixups"));
1347           fixups[fc].exp = ex;
1348           fixups[fc].opindex = *opindex_ptr;
1349           fixups[fc].reloc = reloc;
1350           ++fc;
1351         }
1352
1353       /* Check the next character. The call to expression has advanced
1354          str past any whitespace.  */
1355       if (operand->flags & S390_OPERAND_DISP)
1356         {
1357           /* After a displacement a block in parentheses can start.  */
1358           if (*str != '(')
1359             {
1360               /* Check if parenthesized block can be skipped. If the next
1361                  operand is neiter an optional operand nor a base register
1362                  then we have a syntax error.  */
1363               operand = s390_operands + *(++opindex_ptr);
1364               if (!(operand->flags & (S390_OPERAND_INDEX|S390_OPERAND_BASE)))
1365                 as_bad (_("syntax error; missing '(' after displacement"));
1366
1367               /* Ok, skip all operands until S390_OPERAND_BASE.  */
1368               while (!(operand->flags & S390_OPERAND_BASE))
1369                 operand = s390_operands + *(++opindex_ptr);
1370
1371               /* If there is a next operand it must be separated by a comma.  */
1372               if (opindex_ptr[1] != '\0')
1373                 {
1374                   if (*str++ != ',')
1375                     as_bad (_("syntax error; expected ,"));
1376                 }
1377             }
1378           else
1379             {
1380               /* We found an opening parentheses.  */
1381               str++;
1382               for (f = str; *f != '\0'; f++)
1383                 if (*f == ',' || *f == ')')
1384                   break;
1385               /* If there is no comma until the closing parentheses OR
1386                  there is a comma right after the opening parentheses,
1387                  we have to skip optional operands.  */
1388               if (*f == ',' && f == str)
1389                 {
1390                   /* comma directly after '(' ? */
1391                   skip_optional = 1;
1392                   str++;
1393                 }
1394               else
1395                 skip_optional = (*f != ',');
1396             }
1397         }
1398       else if (operand->flags & S390_OPERAND_BASE)
1399         {
1400           /* After the base register the parenthesed block ends.  */
1401           if (*str++ != ')')
1402             as_bad (_("syntax error; missing ')' after base register"));
1403           skip_optional = 0;
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       else
1412         {
1413           /* We can find an 'early' closing parentheses in e.g. D(L) instead
1414              of D(L,B).  In this case the base register has to be skipped.  */
1415           if (*str == ')')
1416             {
1417               operand = s390_operands + *(++opindex_ptr);
1418
1419               if (!(operand->flags & S390_OPERAND_BASE))
1420                 as_bad (_("syntax error; ')' not allowed here"));
1421               str++;
1422             }
1423           /* If there is a next operand it must be separated by a comma.  */
1424           if (opindex_ptr[1] != '\0')
1425             {
1426               if (*str++ != ',')
1427                 as_bad (_("syntax error; expected ,"));
1428             }
1429         }
1430     }
1431
1432   while (ISSPACE (*str))
1433     ++str;
1434
1435   /* Check for tls instruction marker.  */
1436   reloc = s390_tls_suffix (&str, &ex);
1437   if (reloc != BFD_RELOC_UNUSED)
1438     {
1439       /* We need to generate a fixup of type 'reloc' for this
1440          instruction.  */
1441       if (fc >= MAX_INSN_FIXUPS)
1442         as_fatal (_("too many fixups"));
1443       fixups[fc].exp = ex;
1444       fixups[fc].opindex = -1;
1445       fixups[fc].reloc = reloc;
1446       ++fc;
1447     }
1448
1449   if (*str != '\0')
1450     {
1451       char *linefeed;
1452
1453       if ((linefeed = strchr (str, '\n')) != NULL)
1454         *linefeed = '\0';
1455       as_bad (_("junk at end of line: `%s'"), str);
1456       if (linefeed != NULL)
1457         *linefeed = '\n';
1458     }
1459
1460   /* Write out the instruction.  */
1461   f = frag_more (opcode->oplen);
1462   memcpy (f, insn, opcode->oplen);
1463   dwarf2_emit_insn (opcode->oplen);
1464
1465   /* Create any fixups.  At this point we do not use a
1466      bfd_reloc_code_real_type, but instead just use the
1467      BFD_RELOC_UNUSED plus the operand index.  This lets us easily
1468      handle fixups for any operand type, although that is admittedly
1469      not a very exciting feature.  We pick a BFD reloc type in
1470      md_apply_fix3.  */
1471   for (i = 0; i < fc; i++)
1472     {
1473
1474       if (fixups[i].opindex < 0)
1475         {
1476           /* Create tls instruction marker relocation.  */
1477           fix_new_exp (frag_now, f - frag_now->fr_literal, opcode->oplen,
1478                        &fixups[i].exp, 0, fixups[i].reloc);
1479           continue;
1480         }
1481
1482       operand = s390_operands + fixups[i].opindex;
1483
1484       if (fixups[i].reloc != BFD_RELOC_UNUSED)
1485         {
1486           reloc_howto_type *reloc_howto;
1487           fixS *fixP;
1488           int size;
1489
1490           reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1491           if (!reloc_howto)
1492             abort ();
1493
1494           size = bfd_get_reloc_size (reloc_howto);
1495
1496           if (size < 1 || size > 4)
1497             abort ();
1498
1499           fixP = fix_new_exp (frag_now,
1500                               f - frag_now->fr_literal + (operand->shift/8),
1501                               size, &fixups[i].exp, reloc_howto->pc_relative,
1502                               fixups[i].reloc);
1503           /* Turn off overflow checking in fixup_segment. This is necessary
1504              because fixup_segment will signal an overflow for large 4 byte
1505              quantities for GOT12 relocations.  */
1506           if (   fixups[i].reloc == BFD_RELOC_390_GOT12
1507               || fixups[i].reloc == BFD_RELOC_390_GOT20
1508               || fixups[i].reloc == BFD_RELOC_390_GOT16)
1509             fixP->fx_no_overflow = 1;
1510         }
1511       else
1512         fix_new_exp (frag_now, f - frag_now->fr_literal, 4, &fixups[i].exp,
1513                      (operand->flags & S390_OPERAND_PCREL) != 0,
1514                      ((bfd_reloc_code_real_type)
1515                       (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
1516     }
1517   return str;
1518 }
1519
1520 /* This routine is called for each instruction to be assembled.  */
1521
1522 void
1523 md_assemble (str)
1524      char *str;
1525 {
1526   const struct s390_opcode *opcode;
1527   unsigned char insn[6];
1528   char *s;
1529
1530   /* Get the opcode.  */
1531   for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
1532     ;
1533   if (*s != '\0')
1534     *s++ = '\0';
1535
1536   /* Look up the opcode in the hash table.  */
1537   opcode = (struct s390_opcode *) hash_find (s390_opcode_hash, str);
1538   if (opcode == (const struct s390_opcode *) NULL)
1539     {
1540       as_bad (_("Unrecognized opcode: `%s'"), str);
1541       return;
1542     }
1543   else if (!(opcode->modes & current_mode_mask))
1544     {
1545       as_bad ("Opcode %s not available in this mode", str);
1546       return;
1547     }
1548   memcpy (insn, opcode->opcode, sizeof (insn));
1549   md_gather_operands (s, insn, opcode);
1550 }
1551
1552 #ifndef WORKING_DOT_WORD
1553 /* Handle long and short jumps. We don't support these */
1554 void
1555 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1556      char *ptr;
1557      addressT from_addr, to_addr;
1558      fragS *frag;
1559      symbolS *to_symbol;
1560 {
1561   abort ();
1562 }
1563
1564 void
1565 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1566      char *ptr;
1567      addressT from_addr, to_addr;
1568      fragS *frag;
1569      symbolS *to_symbol;
1570 {
1571   abort ();
1572 }
1573 #endif
1574
1575 void
1576 s390_bss (ignore)
1577      int ignore ATTRIBUTE_UNUSED;
1578 {
1579   /* We don't support putting frags in the BSS segment, we fake it
1580      by marking in_bss, then looking at s_skip for clues.  */
1581
1582   subseg_set (bss_section, 0);
1583   demand_empty_rest_of_line ();
1584 }
1585
1586 /* Pseudo-op handling.  */
1587
1588 void
1589 s390_insn (ignore)
1590      int ignore ATTRIBUTE_UNUSED;
1591 {
1592   expressionS exp;
1593   const struct s390_opcode *opformat;
1594   unsigned char insn[6];
1595   char *s;
1596
1597   /* Get the opcode format.  */
1598   s = input_line_pointer;
1599   while (*s != '\0' && *s != ',' && ! ISSPACE (*s))
1600     s++;
1601   if (*s != ',')
1602     as_bad (_("Invalid .insn format\n"));
1603   *s++ = '\0';
1604
1605   /* Look up the opcode in the hash table.  */
1606   opformat = (struct s390_opcode *)
1607     hash_find (s390_opformat_hash, input_line_pointer);
1608   if (opformat == (const struct s390_opcode *) NULL)
1609     {
1610       as_bad (_("Unrecognized opcode format: `%s'"), input_line_pointer);
1611       return;
1612     }
1613   input_line_pointer = s;
1614   expression (&exp);
1615   if (exp.X_op == O_constant)
1616     {
1617       if (   (   opformat->oplen == 6
1618               && exp.X_add_number >= 0
1619               && (addressT) exp.X_add_number < (1ULL << 48))
1620           || (   opformat->oplen == 4
1621               && exp.X_add_number >= 0
1622               && (addressT) exp.X_add_number < (1ULL << 32))
1623           || (   opformat->oplen == 2
1624               && exp.X_add_number >= 0
1625               && (addressT) exp.X_add_number < (1ULL << 16)))
1626         md_number_to_chars (insn, exp.X_add_number, opformat->oplen);
1627       else
1628         as_bad (_("Invalid .insn format\n"));
1629     }
1630   else if (exp.X_op == O_big)
1631     {
1632       if (exp.X_add_number > 0
1633           && opformat->oplen == 6
1634           && generic_bignum[3] == 0)
1635         {
1636           md_number_to_chars (insn, generic_bignum[2], 2);
1637           md_number_to_chars (&insn[2], generic_bignum[1], 2);
1638           md_number_to_chars (&insn[4], generic_bignum[0], 2);
1639         }
1640       else
1641         as_bad (_("Invalid .insn format\n"));
1642     }
1643   else
1644     as_bad (_("second operand of .insn not a constant\n"));
1645
1646   if (strcmp (opformat->name, "e") != 0 && *input_line_pointer++ != ',')
1647     as_bad (_("missing comma after insn constant\n"));
1648
1649   if ((s = strchr (input_line_pointer, '\n')) != NULL)
1650     *s = '\0';
1651   input_line_pointer = md_gather_operands (input_line_pointer, insn,
1652                                            opformat);
1653   if (s != NULL)
1654     *s = '\n';
1655   demand_empty_rest_of_line ();
1656 }
1657
1658 /* The .byte pseudo-op.  This is similar to the normal .byte
1659    pseudo-op, but it can also take a single ASCII string.  */
1660
1661 static void
1662 s390_byte (ignore)
1663      int ignore ATTRIBUTE_UNUSED;
1664 {
1665   if (*input_line_pointer != '\"')
1666     {
1667       cons (1);
1668       return;
1669     }
1670
1671   /* Gather characters.  A real double quote is doubled.  Unusual
1672      characters are not permitted.  */
1673   ++input_line_pointer;
1674   while (1)
1675     {
1676       char c;
1677
1678       c = *input_line_pointer++;
1679
1680       if (c == '\"')
1681         {
1682           if (*input_line_pointer != '\"')
1683             break;
1684           ++input_line_pointer;
1685         }
1686
1687       FRAG_APPEND_1_CHAR (c);
1688     }
1689
1690   demand_empty_rest_of_line ();
1691 }
1692
1693 /* The .ltorg pseudo-op.This emits all literals defined since the last
1694    .ltorg or the invocation of gas. Literals are defined with the
1695    @lit suffix.  */
1696
1697 static void
1698 s390_literals (ignore)
1699      int ignore ATTRIBUTE_UNUSED;
1700 {
1701   struct s390_lpe *lpe;
1702
1703   if (lp_sym == NULL || lpe_count == 0)
1704     return;     /* Nothing to be done.  */
1705
1706   /* Emit symbol for start of literal pool.  */
1707   S_SET_SEGMENT (lp_sym, now_seg);
1708   S_SET_VALUE (lp_sym, (valueT) frag_now_fix ());
1709   lp_sym->sy_frag = frag_now;
1710
1711   while (lpe_list)
1712     {
1713       lpe = lpe_list;
1714       lpe_list = lpe_list->next;
1715       S_SET_SEGMENT (lpe->sym, now_seg);
1716       S_SET_VALUE (lpe->sym, (valueT) frag_now_fix ());
1717       lpe->sym->sy_frag = frag_now;
1718
1719       /* Emit literal pool entry.  */
1720       if (lpe->reloc != BFD_RELOC_UNUSED)
1721         {
1722           reloc_howto_type *reloc_howto =
1723             bfd_reloc_type_lookup (stdoutput, lpe->reloc);
1724           int size = bfd_get_reloc_size (reloc_howto);
1725           char *where;
1726
1727           if (size > lpe->nbytes)
1728             as_bad (_("%s relocations do not fit in %d bytes"),
1729                     reloc_howto->name, lpe->nbytes);
1730           where = frag_more (lpe->nbytes);
1731           md_number_to_chars (where, 0, size);
1732           fix_new_exp (frag_now, where - frag_now->fr_literal,
1733                        size, &lpe->ex, reloc_howto->pc_relative, lpe->reloc);
1734         }
1735       else
1736         {
1737           if (lpe->ex.X_op == O_big)
1738             {
1739               if (lpe->ex.X_add_number <= 0)
1740                 generic_floating_point_number = lpe->floatnum;
1741               else
1742                 memcpy (generic_bignum, lpe->bignum,
1743                         lpe->ex.X_add_number * sizeof (LITTLENUM_TYPE));
1744             }
1745           emit_expr (&lpe->ex, lpe->nbytes);
1746         }
1747
1748       lpe->next = lpe_free_list;
1749       lpe_free_list = lpe;
1750     }
1751   lpe_list_tail = NULL;
1752   lp_sym = NULL;
1753   lp_count++;
1754   lpe_count = 0;
1755 }
1756
1757 /* Turn a string in input_line_pointer into a floating point constant
1758    of type type, and store the appropriate bytes in *litp.  The number
1759    of LITTLENUMS emitted is stored in *sizep .  An error message is
1760    returned, or NULL on OK.  */
1761
1762 char *
1763 md_atof (type, litp, sizep)
1764      int type;
1765      char *litp;
1766      int *sizep;
1767 {
1768   int prec;
1769   LITTLENUM_TYPE words[4];
1770   char *t;
1771   int i;
1772
1773   switch (type)
1774     {
1775     case 'f':
1776       prec = 2;
1777       break;
1778
1779     case 'd':
1780       prec = 4;
1781       break;
1782
1783     default:
1784       *sizep = 0;
1785       return "bad call to md_atof";
1786     }
1787
1788   t = atof_ieee (input_line_pointer, type, words);
1789   if (t)
1790     input_line_pointer = t;
1791
1792   *sizep = prec * 2;
1793
1794   for (i = 0; i < prec; i++)
1795     {
1796       md_number_to_chars (litp, (valueT) words[i], 2);
1797       litp += 2;
1798     }
1799
1800   return NULL;
1801 }
1802
1803 /* Align a section (I don't know why this is machine dependent).  */
1804
1805 valueT
1806 md_section_align (seg, addr)
1807      asection *seg;
1808      valueT addr;
1809 {
1810   int align = bfd_get_section_alignment (stdoutput, seg);
1811
1812   return ((addr + (1 << align) - 1) & (-1 << align));
1813 }
1814
1815 /* We don't have any form of relaxing.  */
1816
1817 int
1818 md_estimate_size_before_relax (fragp, seg)
1819      fragS *fragp ATTRIBUTE_UNUSED;
1820      asection *seg ATTRIBUTE_UNUSED;
1821 {
1822   abort ();
1823   return 0;
1824 }
1825
1826 /* Convert a machine dependent frag.  We never generate these.  */
1827
1828 void
1829 md_convert_frag (abfd, sec, fragp)
1830      bfd *abfd ATTRIBUTE_UNUSED;
1831      asection *sec ATTRIBUTE_UNUSED;
1832      fragS *fragp ATTRIBUTE_UNUSED;
1833 {
1834   abort ();
1835 }
1836
1837 symbolS *
1838 md_undefined_symbol (name)
1839      char *name;
1840 {
1841   if (*name == '_' && *(name + 1) == 'G'
1842       && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
1843     {
1844       if (!GOT_symbol)
1845         {
1846           if (symbol_find (name))
1847             as_bad (_("GOT already in symbol table"));
1848           GOT_symbol = symbol_new (name, undefined_section,
1849                                    (valueT) 0, &zero_address_frag);
1850         }
1851       return GOT_symbol;
1852     }
1853   return 0;
1854 }
1855
1856 /* Functions concerning relocs.  */
1857
1858 /* The location from which a PC relative jump should be calculated,
1859    given a PC relative reloc.  */
1860
1861 long
1862 md_pcrel_from_section (fixp, sec)
1863      fixS *fixp;
1864      segT sec ATTRIBUTE_UNUSED;
1865 {
1866   return fixp->fx_frag->fr_address + fixp->fx_where;
1867 }
1868
1869 /* Here we decide which fixups can be adjusted to make them relative to
1870    the beginning of the section instead of the symbol.  Basically we need
1871    to make sure that the dynamic relocations are done correctly, so in
1872    some cases we force the original symbol to be used.  */
1873 int
1874 tc_s390_fix_adjustable (fixP)
1875      fixS *fixP;
1876 {
1877   /* Don't adjust references to merge sections.  */
1878   if ((S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0)
1879     return 0;
1880   /* adjust_reloc_syms doesn't know about the GOT.  */
1881   if (   fixP->fx_r_type == BFD_RELOC_16_GOTOFF
1882       || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
1883       || fixP->fx_r_type == BFD_RELOC_390_GOTOFF64
1884       || fixP->fx_r_type == BFD_RELOC_390_PLTOFF16
1885       || fixP->fx_r_type == BFD_RELOC_390_PLTOFF32
1886       || fixP->fx_r_type == BFD_RELOC_390_PLTOFF64
1887       || fixP->fx_r_type == BFD_RELOC_390_PLT16DBL
1888       || fixP->fx_r_type == BFD_RELOC_390_PLT32
1889       || fixP->fx_r_type == BFD_RELOC_390_PLT32DBL
1890       || fixP->fx_r_type == BFD_RELOC_390_PLT64
1891       || fixP->fx_r_type == BFD_RELOC_390_GOT12
1892       || fixP->fx_r_type == BFD_RELOC_390_GOT20
1893       || fixP->fx_r_type == BFD_RELOC_390_GOT16
1894       || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
1895       || fixP->fx_r_type == BFD_RELOC_390_GOT64
1896       || fixP->fx_r_type == BFD_RELOC_390_GOTENT
1897       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT12
1898       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT16
1899       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT20
1900       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT32
1901       || fixP->fx_r_type == BFD_RELOC_390_GOTPLT64
1902       || fixP->fx_r_type == BFD_RELOC_390_GOTPLTENT
1903       || fixP->fx_r_type == BFD_RELOC_390_TLS_LOAD
1904       || fixP->fx_r_type == BFD_RELOC_390_TLS_GDCALL
1905       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDCALL
1906       || fixP->fx_r_type == BFD_RELOC_390_TLS_GD32
1907       || fixP->fx_r_type == BFD_RELOC_390_TLS_GD64
1908       || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE12
1909       || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE20
1910       || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE32
1911       || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE64
1912       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDM32
1913       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDM64
1914       || fixP->fx_r_type == BFD_RELOC_390_TLS_IE32
1915       || fixP->fx_r_type == BFD_RELOC_390_TLS_IE64
1916       || fixP->fx_r_type == BFD_RELOC_390_TLS_IEENT
1917       || fixP->fx_r_type == BFD_RELOC_390_TLS_LE32
1918       || fixP->fx_r_type == BFD_RELOC_390_TLS_LE64
1919       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDO32
1920       || fixP->fx_r_type == BFD_RELOC_390_TLS_LDO64
1921       || fixP->fx_r_type == BFD_RELOC_390_TLS_DTPMOD
1922       || fixP->fx_r_type == BFD_RELOC_390_TLS_DTPOFF
1923       || fixP->fx_r_type == BFD_RELOC_390_TLS_TPOFF
1924       || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1925       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1926     return 0;
1927   return 1;
1928 }
1929
1930 /* Return true if we must always emit a reloc for a type and false if
1931    there is some hope of resolving it at assembly time.  */
1932 int
1933 tc_s390_force_relocation (fixp)
1934      struct fix *fixp;
1935 {
1936   /* Ensure we emit a relocation for every reference to the global
1937      offset table or to the procedure link table.  */
1938   switch (fixp->fx_r_type)
1939     {
1940     case BFD_RELOC_390_GOT12:
1941     case BFD_RELOC_390_GOT20:
1942     case BFD_RELOC_32_GOT_PCREL:
1943     case BFD_RELOC_32_GOTOFF:
1944     case BFD_RELOC_390_GOTOFF64:
1945     case BFD_RELOC_390_PLTOFF16:
1946     case BFD_RELOC_390_PLTOFF32:
1947     case BFD_RELOC_390_PLTOFF64:
1948     case BFD_RELOC_390_GOTPC:
1949     case BFD_RELOC_390_GOT16:
1950     case BFD_RELOC_390_GOTPCDBL:
1951     case BFD_RELOC_390_GOT64:
1952     case BFD_RELOC_390_GOTENT:
1953     case BFD_RELOC_390_PLT32:
1954     case BFD_RELOC_390_PLT16DBL:
1955     case BFD_RELOC_390_PLT32DBL:
1956     case BFD_RELOC_390_PLT64:
1957     case BFD_RELOC_390_GOTPLT12:
1958     case BFD_RELOC_390_GOTPLT16:
1959     case BFD_RELOC_390_GOTPLT20:
1960     case BFD_RELOC_390_GOTPLT32:
1961     case BFD_RELOC_390_GOTPLT64:
1962     case BFD_RELOC_390_GOTPLTENT:
1963       return 1;
1964     default:
1965       break;;
1966     }
1967
1968   return generic_force_reloc (fixp);
1969 }
1970
1971 /* Apply a fixup to the object code.  This is called for all the
1972    fixups we generated by the call to fix_new_exp, above.  In the call
1973    above we used a reloc code which was the largest legal reloc code
1974    plus the operand index.  Here we undo that to recover the operand
1975    index.  At this point all symbol values should be fully resolved,
1976    and we attempt to completely resolve the reloc.  If we can not do
1977    that, we determine the correct reloc code and put it back in the
1978    fixup.  */
1979
1980 void
1981 md_apply_fix3 (fixP, valP, seg)
1982      fixS *fixP;
1983      valueT *valP;
1984      segT seg ATTRIBUTE_UNUSED;
1985 {
1986   char *where;
1987   valueT value = *valP;
1988
1989   where = fixP->fx_frag->fr_literal + fixP->fx_where;
1990
1991   if (fixP->fx_subsy != NULL)
1992     as_bad_where (fixP->fx_file, fixP->fx_line,
1993                   "cannot emit relocation %s against subsy symbol %s",
1994                   bfd_get_reloc_code_name (fixP->fx_r_type),
1995                   S_GET_NAME (fixP->fx_subsy));
1996
1997   if (fixP->fx_addsy != NULL)
1998     {
1999       if (fixP->fx_pcrel)
2000         value += fixP->fx_frag->fr_address + fixP->fx_where;
2001     }
2002   else
2003     fixP->fx_done = 1;
2004
2005   if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
2006     {
2007       const struct s390_operand *operand;
2008       int opindex;
2009
2010       opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
2011       operand = &s390_operands[opindex];
2012
2013       if (fixP->fx_done)
2014         {
2015           /* Insert the fully resolved operand value.  */
2016           s390_insert_operand (where, operand, (offsetT) value,
2017                                fixP->fx_file, fixP->fx_line);
2018           return;
2019         }
2020
2021       /* Determine a BFD reloc value based on the operand information.
2022          We are only prepared to turn a few of the operands into
2023          relocs.  */
2024       fixP->fx_offset = value;
2025       if (operand->bits == 12 && operand->shift == 20)
2026         {
2027           fixP->fx_size = 2;
2028           fixP->fx_where += 2;
2029           fixP->fx_r_type = BFD_RELOC_390_12;
2030         }
2031       else if (operand->bits == 12 && operand->shift == 36)
2032         {
2033           fixP->fx_size = 2;
2034           fixP->fx_where += 4;
2035           fixP->fx_r_type = BFD_RELOC_390_12;
2036         }
2037       else if (operand->bits == 20 && operand->shift == 20)
2038         {
2039           fixP->fx_size = 2;
2040           fixP->fx_where += 2;
2041           fixP->fx_r_type = BFD_RELOC_390_20;
2042         }
2043       else if (operand->bits == 8 && operand->shift == 8)
2044         {
2045           fixP->fx_size = 1;
2046           fixP->fx_where += 1;
2047           fixP->fx_r_type = BFD_RELOC_8;
2048         }
2049       else if (operand->bits == 16 && operand->shift == 16)
2050         {
2051           fixP->fx_size = 2;
2052           fixP->fx_where += 2;
2053           if (operand->flags & S390_OPERAND_PCREL)
2054             {
2055               fixP->fx_r_type = BFD_RELOC_390_PC16DBL;
2056               fixP->fx_offset += 2;
2057             }
2058           else
2059             fixP->fx_r_type = BFD_RELOC_16;
2060         }
2061       else if (operand->bits == 32 && operand->shift == 16
2062                && (operand->flags & S390_OPERAND_PCREL))
2063         {
2064           fixP->fx_size = 4;
2065           fixP->fx_where += 2;
2066           fixP->fx_offset += 2;
2067           fixP->fx_r_type = BFD_RELOC_390_PC32DBL;
2068         }
2069       else
2070         {
2071           char *sfile;
2072           unsigned int sline;
2073
2074           /* Use expr_symbol_where to see if this is an expression
2075              symbol.  */
2076           if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
2077             as_bad_where (fixP->fx_file, fixP->fx_line,
2078                           _("unresolved expression that must be resolved"));
2079           else
2080             as_bad_where (fixP->fx_file, fixP->fx_line,
2081                           _("unsupported relocation type"));
2082           fixP->fx_done = 1;
2083           return;
2084         }
2085     }
2086   else
2087     {
2088       switch (fixP->fx_r_type)
2089         {
2090         case BFD_RELOC_8:
2091           if (fixP->fx_pcrel)
2092             abort ();
2093           if (fixP->fx_done)
2094             md_number_to_chars (where, value, 1);
2095           break;
2096         case BFD_RELOC_390_12:
2097         case BFD_RELOC_390_GOT12:
2098         case BFD_RELOC_390_GOTPLT12:
2099           if (fixP->fx_done)
2100             {
2101               unsigned short mop;
2102
2103               mop = bfd_getb16 ((unsigned char *) where);
2104               mop |= (unsigned short) (value & 0xfff);
2105               bfd_putb16 ((bfd_vma) mop, (unsigned char *) where);
2106             }
2107           break;
2108
2109         case BFD_RELOC_390_20:
2110         case BFD_RELOC_390_GOT20:
2111         case BFD_RELOC_390_GOTPLT20:
2112           if (fixP->fx_done)
2113             {
2114               unsigned int mop;
2115               mop = bfd_getb32 ((unsigned char *) where);
2116               mop |= (unsigned int) ((value & 0xfff) << 8 |
2117                                      (value & 0xff000) >> 12);
2118               bfd_putb32 ((bfd_vma) mop, (unsigned char *) where);
2119             } 
2120           break;
2121
2122         case BFD_RELOC_16:
2123         case BFD_RELOC_GPREL16:
2124         case BFD_RELOC_16_GOT_PCREL:
2125         case BFD_RELOC_16_GOTOFF:
2126           if (fixP->fx_pcrel)
2127             as_bad_where (fixP->fx_file, fixP->fx_line,
2128                           "cannot emit PC relative %s relocation%s%s",
2129                           bfd_get_reloc_code_name (fixP->fx_r_type),
2130                           fixP->fx_addsy != NULL ? " against " : "",
2131                           (fixP->fx_addsy != NULL
2132                            ? S_GET_NAME (fixP->fx_addsy)
2133                            : ""));
2134           if (fixP->fx_done)
2135             md_number_to_chars (where, value, 2);
2136           break;
2137         case BFD_RELOC_390_GOT16:
2138         case BFD_RELOC_390_PLTOFF16:
2139         case BFD_RELOC_390_GOTPLT16:
2140           if (fixP->fx_done)
2141             md_number_to_chars (where, value, 2);
2142           break;
2143         case BFD_RELOC_390_PC16DBL:
2144         case BFD_RELOC_390_PLT16DBL:
2145           value += 2;
2146           if (fixP->fx_done)
2147             md_number_to_chars (where, (offsetT) value >> 1, 2);
2148           break;
2149
2150         case BFD_RELOC_32:
2151           if (fixP->fx_pcrel)
2152             fixP->fx_r_type = BFD_RELOC_32_PCREL;
2153           else
2154             fixP->fx_r_type = BFD_RELOC_32;
2155           if (fixP->fx_done)
2156             md_number_to_chars (where, value, 4);
2157           break;
2158         case BFD_RELOC_32_PCREL:
2159         case BFD_RELOC_32_BASEREL:
2160           fixP->fx_r_type = BFD_RELOC_32_PCREL;
2161           if (fixP->fx_done)
2162             md_number_to_chars (where, value, 4);
2163           break;
2164         case BFD_RELOC_32_GOT_PCREL:
2165         case BFD_RELOC_390_PLTOFF32:
2166         case BFD_RELOC_390_PLT32:
2167         case BFD_RELOC_390_GOTPLT32:
2168           if (fixP->fx_done)
2169             md_number_to_chars (where, value, 4);
2170           break;
2171         case BFD_RELOC_390_PC32DBL:
2172         case BFD_RELOC_390_PLT32DBL:
2173         case BFD_RELOC_390_GOTPCDBL:
2174         case BFD_RELOC_390_GOTENT:
2175         case BFD_RELOC_390_GOTPLTENT:
2176           value += 2;
2177           if (fixP->fx_done)
2178             md_number_to_chars (where, (offsetT) value >> 1, 4);
2179           break;
2180
2181         case BFD_RELOC_32_GOTOFF:
2182           if (fixP->fx_done)
2183             md_number_to_chars (where, value, sizeof (int));
2184           break;
2185
2186         case BFD_RELOC_390_GOTOFF64:
2187           if (fixP->fx_done)
2188             md_number_to_chars (where, value, 8);
2189           break;
2190
2191         case BFD_RELOC_390_GOT64:
2192         case BFD_RELOC_390_PLTOFF64:
2193         case BFD_RELOC_390_PLT64:
2194         case BFD_RELOC_390_GOTPLT64:
2195           if (fixP->fx_done)
2196             md_number_to_chars (where, value, 8);
2197           break;
2198
2199         case BFD_RELOC_64:
2200           if (fixP->fx_pcrel)
2201             fixP->fx_r_type = BFD_RELOC_64_PCREL;
2202           else
2203             fixP->fx_r_type = BFD_RELOC_64;
2204           if (fixP->fx_done)
2205             md_number_to_chars (where, value, 8);
2206           break;
2207
2208         case BFD_RELOC_64_PCREL:
2209           fixP->fx_r_type = BFD_RELOC_64_PCREL;
2210           if (fixP->fx_done)
2211             md_number_to_chars (where, value, 8);
2212           break;
2213
2214         case BFD_RELOC_VTABLE_INHERIT:
2215         case BFD_RELOC_VTABLE_ENTRY:
2216           fixP->fx_done = 0;
2217           return;
2218
2219         case BFD_RELOC_390_TLS_LOAD:
2220         case BFD_RELOC_390_TLS_GDCALL:
2221         case BFD_RELOC_390_TLS_LDCALL:
2222         case BFD_RELOC_390_TLS_GD32:
2223         case BFD_RELOC_390_TLS_GD64:
2224         case BFD_RELOC_390_TLS_GOTIE12:
2225         case BFD_RELOC_390_TLS_GOTIE20:
2226         case BFD_RELOC_390_TLS_GOTIE32:
2227         case BFD_RELOC_390_TLS_GOTIE64:
2228         case BFD_RELOC_390_TLS_LDM32:
2229         case BFD_RELOC_390_TLS_LDM64:
2230         case BFD_RELOC_390_TLS_IE32:
2231         case BFD_RELOC_390_TLS_IE64:
2232         case BFD_RELOC_390_TLS_LE32:
2233         case BFD_RELOC_390_TLS_LE64:
2234         case BFD_RELOC_390_TLS_LDO32:
2235         case BFD_RELOC_390_TLS_LDO64:
2236         case BFD_RELOC_390_TLS_DTPMOD:
2237         case BFD_RELOC_390_TLS_DTPOFF:
2238         case BFD_RELOC_390_TLS_TPOFF:
2239           /* Fully resolved at link time.  */
2240           break;
2241         case BFD_RELOC_390_TLS_IEENT:
2242           /* Fully resolved at link time.  */
2243           value += 2;
2244           break;
2245
2246         default:
2247           {
2248             const char *reloc_name = bfd_get_reloc_code_name (fixP->fx_r_type);
2249
2250             if (reloc_name != NULL)
2251               fprintf (stderr, "Gas failure, reloc type %s\n", reloc_name);
2252             else
2253               fprintf (stderr, "Gas failure, reloc type #%i\n", fixP->fx_r_type);
2254             fflush (stderr);
2255             abort ();
2256           }
2257         }
2258
2259       fixP->fx_offset = value;
2260     }
2261 }
2262
2263 /* Generate a reloc for a fixup.  */
2264
2265 arelent *
2266 tc_gen_reloc (seg, fixp)
2267      asection *seg ATTRIBUTE_UNUSED;
2268      fixS *fixp;
2269 {
2270   bfd_reloc_code_real_type code;
2271   arelent *reloc;
2272
2273   code = fixp->fx_r_type;
2274   if (GOT_symbol && fixp->fx_addsy == GOT_symbol)
2275     {
2276       if (   (s390_arch_size == 32 && code == BFD_RELOC_32_PCREL)
2277           || (s390_arch_size == 64 && code == BFD_RELOC_64_PCREL))
2278         code = BFD_RELOC_390_GOTPC;
2279       if (code == BFD_RELOC_390_PC32DBL)
2280         code = BFD_RELOC_390_GOTPCDBL;
2281     }
2282
2283   reloc = (arelent *) xmalloc (sizeof (arelent));
2284   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2285   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2286   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2287   reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2288   if (reloc->howto == NULL)
2289     {
2290       as_bad_where (fixp->fx_file, fixp->fx_line,
2291                     _("cannot represent relocation type %s"),
2292                     bfd_get_reloc_code_name (code));
2293       /* Set howto to a garbage value so that we can keep going.  */
2294       reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2295       assert (reloc->howto != NULL);
2296     }
2297   reloc->addend = fixp->fx_offset;
2298
2299   return reloc;
2300 }
2301
2302 void
2303 s390_cfi_frame_initial_instructions ()
2304 {
2305   cfi_add_CFA_def_cfa (15, s390_arch_size == 64 ? 160 : 96);
2306 }
2307
2308 int
2309 tc_s390_regname_to_dw2regnum (const char *regname)
2310 {
2311   int regnum = -1;
2312
2313   if (regname[0] != 'c' && regname[0] != 'a')
2314     {
2315       regnum = reg_name_search (pre_defined_registers, REG_NAME_CNT, regname);
2316       if (regname[0] == 'f' && regnum != -1)
2317         regnum += 16;
2318     }
2319   else if (strcmp (regname, "ap") == 0)
2320     regnum = 32;
2321   else if (strcmp (regname, "cc") == 0)
2322     regnum = 33;
2323   return regnum;
2324 }