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