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