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