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