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