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