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