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