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