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