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