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