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