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