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