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