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