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