* config/tc-mips.c (move_register): Fix formatting.
[binutils.git] / gas / config / tc-s390.c
blob9bc582faed929b3d2e20df54325cd042f2aefe90
1 /* tc-s390.c -- Assemble for the S390
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
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"
30 #include "opcode/s390.h"
31 #include "elf/s390.h"
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;
41 /* If no -march option was given default to the highest available CPU.
42 Since with S/390 a newer CPU always supports everything from its
43 predecessors this will accept every valid asm input. */
44 static unsigned int current_cpu = S390_OPCODE_MAXCPU - 1;
45 static unsigned int current_mode_mask = 0;
47 /* Whether to use user friendly register names. Default is TRUE. */
48 #ifndef TARGET_REG_NAMES_P
49 #define TARGET_REG_NAMES_P TRUE
50 #endif
52 static bfd_boolean reg_names_p = TARGET_REG_NAMES_P;
54 /* Set to TRUE if we want to warn about zero base/index registers. */
55 static bfd_boolean warn_areg_zero = FALSE;
57 /* Generic assembler global variables which must be defined by all
58 targets. */
60 const char comment_chars[] = "#";
62 /* Characters which start a comment at the beginning of a line. */
63 const char line_comment_chars[] = "#";
65 /* Characters which may be used to separate multiple commands on a
66 single line. */
67 const char line_separator_chars[] = ";";
69 /* Characters which are used to indicate an exponent in a floating
70 point number. */
71 const char EXP_CHARS[] = "eE";
73 /* Characters which mean that a number is a floating point constant,
74 as in 0d1.0. */
75 const char FLT_CHARS[] = "dD";
77 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
78 int s390_cie_data_alignment;
80 /* The target specific pseudo-ops which we support. */
82 /* Define the prototypes for the pseudo-ops */
83 static void s390_byte (int);
84 static void s390_elf_cons (int);
85 static void s390_bss (int);
86 static void s390_insn (int);
87 static void s390_literals (int);
88 static void s390_machine (int);
90 const pseudo_typeS md_pseudo_table[] =
92 { "align", s_align_bytes, 0 },
93 /* Pseudo-ops which must be defined. */
94 { "bss", s390_bss, 0 },
95 { "insn", s390_insn, 0 },
96 /* Pseudo-ops which must be overridden. */
97 { "byte", s390_byte, 0 },
98 { "short", s390_elf_cons, 2 },
99 { "long", s390_elf_cons, 4 },
100 { "quad", s390_elf_cons, 8 },
101 { "ltorg", s390_literals, 0 },
102 { "string", stringer, 8 + 1 },
103 { "machine", s390_machine, 0 },
104 { NULL, NULL, 0 }
108 /* Structure to hold information about predefined registers. */
109 struct pd_reg
111 char *name;
112 int value;
115 /* List of registers that are pre-defined:
117 Each access register has a predefined name of the form:
118 a<reg_num> which has the value <reg_num>.
120 Each control register has a predefined name of the form:
121 c<reg_num> which has the value <reg_num>.
123 Each general register has a predefined name of the form:
124 r<reg_num> which has the value <reg_num>.
126 Each floating point register a has predefined name of the form:
127 f<reg_num> which has the value <reg_num>.
129 There are individual registers as well:
130 sp has the value 15
131 lit has the value 12
133 The table is sorted. Suitable for searching by a binary search. */
135 static const struct pd_reg pre_defined_registers[] =
137 { "a0", 0 }, /* Access registers */
138 { "a1", 1 },
139 { "a10", 10 },
140 { "a11", 11 },
141 { "a12", 12 },
142 { "a13", 13 },
143 { "a14", 14 },
144 { "a15", 15 },
145 { "a2", 2 },
146 { "a3", 3 },
147 { "a4", 4 },
148 { "a5", 5 },
149 { "a6", 6 },
150 { "a7", 7 },
151 { "a8", 8 },
152 { "a9", 9 },
154 { "c0", 0 }, /* Control registers */
155 { "c1", 1 },
156 { "c10", 10 },
157 { "c11", 11 },
158 { "c12", 12 },
159 { "c13", 13 },
160 { "c14", 14 },
161 { "c15", 15 },
162 { "c2", 2 },
163 { "c3", 3 },
164 { "c4", 4 },
165 { "c5", 5 },
166 { "c6", 6 },
167 { "c7", 7 },
168 { "c8", 8 },
169 { "c9", 9 },
171 { "f0", 0 }, /* Floating point registers */
172 { "f1", 1 },
173 { "f10", 10 },
174 { "f11", 11 },
175 { "f12", 12 },
176 { "f13", 13 },
177 { "f14", 14 },
178 { "f15", 15 },
179 { "f2", 2 },
180 { "f3", 3 },
181 { "f4", 4 },
182 { "f5", 5 },
183 { "f6", 6 },
184 { "f7", 7 },
185 { "f8", 8 },
186 { "f9", 9 },
188 { "lit", 13 }, /* Pointer to literal pool */
190 { "r0", 0 }, /* General purpose registers */
191 { "r1", 1 },
192 { "r10", 10 },
193 { "r11", 11 },
194 { "r12", 12 },
195 { "r13", 13 },
196 { "r14", 14 },
197 { "r15", 15 },
198 { "r2", 2 },
199 { "r3", 3 },
200 { "r4", 4 },
201 { "r5", 5 },
202 { "r6", 6 },
203 { "r7", 7 },
204 { "r8", 8 },
205 { "r9", 9 },
207 { "sp", 15 }, /* Stack pointer */
211 #define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct pd_reg))
213 /* Given NAME, find the register number associated with that name, return
214 the integer value associated with the given name or -1 on failure. */
216 static int
217 reg_name_search (const struct pd_reg *regs, int regcount, const char *name)
219 int middle, low, high;
220 int cmp;
222 low = 0;
223 high = regcount - 1;
227 middle = (low + high) / 2;
228 cmp = strcasecmp (name, regs[middle].name);
229 if (cmp < 0)
230 high = middle - 1;
231 else if (cmp > 0)
232 low = middle + 1;
233 else
234 return regs[middle].value;
236 while (low <= high);
238 return -1;
243 * Summary of register_name().
245 * in: Input_line_pointer points to 1st char of operand.
247 * out: A expressionS.
248 * The operand may have been a register: in this case, X_op == O_register,
249 * X_add_number is set to the register number, and truth is returned.
250 * Input_line_pointer->(next non-blank) char after operand, or is in its
251 * original state.
254 static bfd_boolean
255 register_name (expressionS *expressionP)
257 int reg_number;
258 char *name;
259 char *start;
260 char c;
262 /* Find the spelling of the operand. */
263 start = name = input_line_pointer;
264 if (name[0] == '%' && ISALPHA (name[1]))
265 name = ++input_line_pointer;
266 else
267 return FALSE;
269 c = get_symbol_end ();
270 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
272 /* Put back the delimiting char. */
273 *input_line_pointer = c;
275 /* Look to see if it's in the register table. */
276 if (reg_number >= 0)
278 expressionP->X_op = O_register;
279 expressionP->X_add_number = reg_number;
281 /* Make the rest nice. */
282 expressionP->X_add_symbol = NULL;
283 expressionP->X_op_symbol = NULL;
284 return TRUE;
287 /* Reset the line as if we had not done anything. */
288 input_line_pointer = start;
289 return FALSE;
292 /* Local variables. */
294 /* Opformat hash table. */
295 static struct hash_control *s390_opformat_hash;
297 /* Opcode hash table. */
298 static struct hash_control *s390_opcode_hash = NULL;
300 /* Flags to set in the elf header */
301 static flagword s390_flags = 0;
303 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE_" */
305 #ifndef WORKING_DOT_WORD
306 int md_short_jump_size = 4;
307 int md_long_jump_size = 4;
308 #endif
310 const char *md_shortopts = "A:m:kVQ:";
311 struct option md_longopts[] = {
312 {NULL, no_argument, NULL, 0}
314 size_t md_longopts_size = sizeof (md_longopts);
316 /* Initialize the default opcode arch and word size from the default
317 architecture name if not specified by an option. */
318 static void
319 init_default_arch (void)
321 if (strcmp (default_arch, "s390") == 0)
323 if (s390_arch_size == 0)
324 s390_arch_size = 32;
326 else if (strcmp (default_arch, "s390x") == 0)
328 if (s390_arch_size == 0)
329 s390_arch_size = 64;
331 else
332 as_fatal (_("Invalid default architecture, broken assembler."));
334 if (current_mode_mask == 0)
336 /* Default to z/Architecture mode if the CPU supports it. */
337 if (current_cpu < S390_OPCODE_Z900)
338 current_mode_mask = 1 << S390_OPCODE_ESA;
339 else
340 current_mode_mask = 1 << S390_OPCODE_ZARCH;
344 /* Called by TARGET_FORMAT. */
345 const char *
346 s390_target_format (void)
348 /* We don't get a chance to initialize anything before we're called,
349 so handle that now. */
350 init_default_arch ();
352 return s390_arch_size == 64 ? "elf64-s390" : "elf32-s390";
355 /* Map a CPU string as given with -march= or .machine to the
356 respective enum s390_opcode_cpu_val value. 0xffffffff is returned
357 in case of an error. */
359 static unsigned int
360 s390_parse_cpu (char *arg)
362 if (strcmp (arg, "g5") == 0)
363 return S390_OPCODE_G5;
364 else if (strcmp (arg, "g6") == 0)
365 return S390_OPCODE_G6;
366 else if (strcmp (arg, "z900") == 0)
367 return S390_OPCODE_Z900;
368 else if (strcmp (arg, "z990") == 0)
369 return S390_OPCODE_Z990;
370 else if (strcmp (arg, "z9-109") == 0)
371 return S390_OPCODE_Z9_109;
372 else if (strcmp (arg, "z9-ec") == 0)
373 return S390_OPCODE_Z9_EC;
374 else if (strcmp (arg, "z10") == 0)
375 return S390_OPCODE_Z10;
376 else if (strcmp (arg, "z196") == 0)
377 return S390_OPCODE_Z196;
378 else if (strcmp (arg, "all") == 0)
379 return S390_OPCODE_MAXCPU - 1;
380 else
381 return -1;
385 md_parse_option (int c, char *arg)
387 switch (c)
389 /* -k: Ignore for FreeBSD compatibility. */
390 case 'k':
391 break;
392 case 'm':
393 if (arg != NULL && strcmp (arg, "regnames") == 0)
394 reg_names_p = TRUE;
396 else if (arg != NULL && strcmp (arg, "no-regnames") == 0)
397 reg_names_p = FALSE;
399 else if (arg != NULL && strcmp (arg, "warn-areg-zero") == 0)
400 warn_areg_zero = TRUE;
402 else if (arg != NULL && strcmp (arg, "31") == 0)
403 s390_arch_size = 32;
405 else if (arg != NULL && strcmp (arg, "64") == 0)
406 s390_arch_size = 64;
408 else if (arg != NULL && strcmp (arg, "esa") == 0)
409 current_mode_mask = 1 << S390_OPCODE_ESA;
411 else if (arg != NULL && strcmp (arg, "zarch") == 0)
412 current_mode_mask = 1 << S390_OPCODE_ZARCH;
414 else if (arg != NULL && strncmp (arg, "arch=", 5) == 0)
416 current_cpu = s390_parse_cpu (arg + 5);
418 if (current_cpu == (unsigned int)-1)
420 as_bad (_("invalid switch -m%s"), arg);
421 return 0;
425 else
427 as_bad (_("invalid switch -m%s"), arg);
428 return 0;
430 break;
432 case 'A':
433 /* Option -A is deprecated. Still available for compatibility. */
434 if (arg != NULL && strcmp (arg, "esa") == 0)
435 current_cpu = S390_OPCODE_G5;
436 else if (arg != NULL && strcmp (arg, "esame") == 0)
437 current_cpu = S390_OPCODE_Z900;
438 else
439 as_bad (_("invalid architecture -A%s"), arg);
440 break;
442 /* -V: SVR4 argument to print version ID. */
443 case 'V':
444 print_version_id ();
445 break;
447 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
448 should be emitted or not. FIXME: Not implemented. */
449 case 'Q':
450 break;
452 default:
453 return 0;
456 return 1;
459 void
460 md_show_usage (FILE *stream)
462 fprintf (stream, _("\
463 S390 options:\n\
464 -mregnames Allow symbolic names for registers\n\
465 -mwarn-areg-zero Warn about zero base/index registers\n\
466 -mno-regnames Do not allow symbolic names for registers\n\
467 -m31 Set file format to 31 bit format\n\
468 -m64 Set file format to 64 bit format\n"));
469 fprintf (stream, _("\
470 -V print assembler version number\n\
471 -Qy, -Qn ignored\n"));
474 /* Generate the hash table mapping mnemonics to struct s390_opcode.
475 This table is built at startup and whenever the CPU level is
476 changed using .machine. */
478 static void
479 s390_setup_opcodes (void)
481 register const struct s390_opcode *op;
482 const struct s390_opcode *op_end;
483 bfd_boolean dup_insn = FALSE;
484 const char *retval;
486 if (s390_opcode_hash != NULL)
487 hash_die (s390_opcode_hash);
489 /* Insert the opcodes into a hash table. */
490 s390_opcode_hash = hash_new ();
492 op_end = s390_opcodes + s390_num_opcodes;
493 for (op = s390_opcodes; op < op_end; op++)
495 while (op < op_end - 1 && strcmp(op->name, op[1].name) == 0)
497 if (op->min_cpu <= current_cpu && (op->modes & current_mode_mask))
498 break;
499 op++;
502 if (op->min_cpu <= current_cpu && (op->modes & current_mode_mask))
504 retval = hash_insert (s390_opcode_hash, op->name, (void *) op);
505 if (retval != (const char *) NULL)
507 as_bad (_("Internal assembler error for instruction %s"),
508 op->name);
509 dup_insn = TRUE;
513 while (op < op_end - 1 && strcmp (op->name, op[1].name) == 0)
514 op++;
517 if (dup_insn)
518 abort ();
521 /* This function is called when the assembler starts up. It is called
522 after the options have been parsed and the output file has been
523 opened. */
525 void
526 md_begin (void)
528 register const struct s390_opcode *op;
529 const struct s390_opcode *op_end;
530 const char *retval;
532 /* Give a warning if the combination -m64-bit and -Aesa is used. */
533 if (s390_arch_size == 64 && current_cpu < S390_OPCODE_Z900)
534 as_warn (_("The 64 bit file format is used without esame instructions."));
536 s390_cie_data_alignment = -s390_arch_size / 8;
538 /* Set the ELF flags if desired. */
539 if (s390_flags)
540 bfd_set_private_flags (stdoutput, s390_flags);
542 /* Insert the opcode formats into a hash table. */
543 s390_opformat_hash = hash_new ();
545 op_end = s390_opformats + s390_num_opformats;
546 for (op = s390_opformats; op < op_end; op++)
548 retval = hash_insert (s390_opformat_hash, op->name, (void *) op);
549 if (retval != (const char *) NULL)
550 as_bad (_("Internal assembler error for instruction format %s"),
551 op->name);
554 s390_setup_opcodes ();
556 record_alignment (text_section, 2);
557 record_alignment (data_section, 2);
558 record_alignment (bss_section, 2);
561 /* Called after all assembly has been done. */
562 void
563 s390_md_end (void)
565 if (s390_arch_size == 64)
566 bfd_set_arch_mach (stdoutput, bfd_arch_s390, bfd_mach_s390_64);
567 else
568 bfd_set_arch_mach (stdoutput, bfd_arch_s390, bfd_mach_s390_31);
571 /* Insert an operand value into an instruction. */
573 static void
574 s390_insert_operand (unsigned char *insn,
575 const struct s390_operand *operand,
576 offsetT val,
577 char *file,
578 unsigned int line)
580 addressT uval;
581 int offset;
583 if (operand->flags & (S390_OPERAND_SIGNED|S390_OPERAND_PCREL))
585 offsetT min, max;
587 max = ((offsetT) 1 << (operand->bits - 1)) - 1;
588 min = - ((offsetT) 1 << (operand->bits - 1));
589 /* Halve PCREL operands. */
590 if (operand->flags & S390_OPERAND_PCREL)
591 val >>= 1;
592 /* Check for underflow / overflow. */
593 if (val < min || val > max)
595 const char *err =
596 _("operand out of range (%s not between %ld and %ld)");
597 char buf[100];
599 if (operand->flags & S390_OPERAND_PCREL)
601 val <<= 1;
602 min <<= 1;
603 max <<= 1;
605 sprint_value (buf, val);
606 if (file == (char *) NULL)
607 as_bad (err, buf, (int) min, (int) max);
608 else
609 as_bad_where (file, line, err, buf, (int) min, (int) max);
610 return;
612 /* val is ok, now restrict it to operand->bits bits. */
613 uval = (addressT) val & ((((addressT) 1 << (operand->bits-1)) << 1) - 1);
614 /* val is restrict, now check for special case. */
615 if (operand->bits == 20 && operand->shift == 20)
616 uval = (uval >> 12) | ((uval & 0xfff) << 8);
618 else
620 addressT min, max;
622 max = (((addressT) 1 << (operand->bits - 1)) << 1) - 1;
623 min = (offsetT) 0;
624 uval = (addressT) val;
625 /* Length x in an instructions has real length x+1. */
626 if (operand->flags & S390_OPERAND_LENGTH)
627 uval--;
628 /* Check for underflow / overflow. */
629 if (uval < min || uval > max)
631 if (operand->flags & S390_OPERAND_LENGTH)
633 uval++;
634 min++;
635 max++;
638 as_bad_value_out_of_range (_("operand"), uval, (offsetT) min, (offsetT) max, file, line);
640 return;
644 /* Insert fragments of the operand byte for byte. */
645 offset = operand->shift + operand->bits;
646 uval <<= (-offset) & 7;
647 insn += (offset - 1) / 8;
648 while (uval != 0)
650 *insn-- |= uval;
651 uval >>= 8;
655 struct map_tls
657 char *string;
658 int length;
659 bfd_reloc_code_real_type reloc;
662 /* Parse tls marker and return the desired relocation. */
663 static bfd_reloc_code_real_type
664 s390_tls_suffix (char **str_p, expressionS *exp_p)
666 static struct map_tls mapping[] =
668 { "tls_load", 8, BFD_RELOC_390_TLS_LOAD },
669 { "tls_gdcall", 10, BFD_RELOC_390_TLS_GDCALL },
670 { "tls_ldcall", 10, BFD_RELOC_390_TLS_LDCALL },
671 { NULL, 0, BFD_RELOC_UNUSED }
673 struct map_tls *ptr;
674 char *orig_line;
675 char *str;
676 char *ident;
677 int len;
679 str = *str_p;
680 if (*str++ != ':')
681 return BFD_RELOC_UNUSED;
683 ident = str;
684 while (ISIDNUM (*str))
685 str++;
686 len = str - ident;
687 if (*str++ != ':')
688 return BFD_RELOC_UNUSED;
690 orig_line = input_line_pointer;
691 input_line_pointer = str;
692 expression (exp_p);
693 str = input_line_pointer;
694 if (&input_line_pointer != str_p)
695 input_line_pointer = orig_line;
697 if (exp_p->X_op != O_symbol)
698 return BFD_RELOC_UNUSED;
700 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
701 if (len == ptr->length
702 && strncasecmp (ident, ptr->string, ptr->length) == 0)
704 /* Found a matching tls suffix. */
705 *str_p = str;
706 return ptr->reloc;
708 return BFD_RELOC_UNUSED;
711 /* Structure used to hold suffixes. */
712 typedef enum
714 ELF_SUFFIX_NONE = 0,
715 ELF_SUFFIX_GOT,
716 ELF_SUFFIX_PLT,
717 ELF_SUFFIX_GOTENT,
718 ELF_SUFFIX_GOTOFF,
719 ELF_SUFFIX_GOTPLT,
720 ELF_SUFFIX_PLTOFF,
721 ELF_SUFFIX_TLS_GD,
722 ELF_SUFFIX_TLS_GOTIE,
723 ELF_SUFFIX_TLS_IE,
724 ELF_SUFFIX_TLS_LDM,
725 ELF_SUFFIX_TLS_LDO,
726 ELF_SUFFIX_TLS_LE
728 elf_suffix_type;
730 struct map_bfd
732 char *string;
733 int length;
734 elf_suffix_type suffix;
738 /* Parse @got/@plt/@gotoff. and return the desired relocation. */
739 static elf_suffix_type
740 s390_elf_suffix (char **str_p, expressionS *exp_p)
742 static struct map_bfd mapping[] =
744 { "got", 3, ELF_SUFFIX_GOT },
745 { "got12", 5, ELF_SUFFIX_GOT },
746 { "plt", 3, ELF_SUFFIX_PLT },
747 { "gotent", 6, ELF_SUFFIX_GOTENT },
748 { "gotoff", 6, ELF_SUFFIX_GOTOFF },
749 { "gotplt", 6, ELF_SUFFIX_GOTPLT },
750 { "pltoff", 6, ELF_SUFFIX_PLTOFF },
751 { "tlsgd", 5, ELF_SUFFIX_TLS_GD },
752 { "gotntpoff", 9, ELF_SUFFIX_TLS_GOTIE },
753 { "indntpoff", 9, ELF_SUFFIX_TLS_IE },
754 { "tlsldm", 6, ELF_SUFFIX_TLS_LDM },
755 { "dtpoff", 6, ELF_SUFFIX_TLS_LDO },
756 { "ntpoff", 6, ELF_SUFFIX_TLS_LE },
757 { NULL, 0, ELF_SUFFIX_NONE }
760 struct map_bfd *ptr;
761 char *str = *str_p;
762 char *ident;
763 int len;
765 if (*str++ != '@')
766 return ELF_SUFFIX_NONE;
768 ident = str;
769 while (ISALNUM (*str))
770 str++;
771 len = str - ident;
773 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
774 if (len == ptr->length
775 && strncasecmp (ident, ptr->string, ptr->length) == 0)
777 if (exp_p->X_add_number != 0)
778 as_warn (_("identifier+constant@%s means identifier@%s+constant"),
779 ptr->string, ptr->string);
780 /* Now check for identifier@suffix+constant. */
781 if (*str == '-' || *str == '+')
783 char *orig_line = input_line_pointer;
784 expressionS new_exp;
786 input_line_pointer = str;
787 expression (&new_exp);
789 switch (new_exp.X_op)
791 case O_constant: /* X_add_number (a constant expression). */
792 exp_p->X_add_number += new_exp.X_add_number;
793 str = input_line_pointer;
794 break;
795 case O_symbol: /* X_add_symbol + X_add_number. */
796 /* this case is used for e.g. xyz@PLT+.Label. */
797 exp_p->X_add_number += new_exp.X_add_number;
798 exp_p->X_op_symbol = new_exp.X_add_symbol;
799 exp_p->X_op = O_add;
800 str = input_line_pointer;
801 break;
802 case O_uminus: /* (- X_add_symbol) + X_add_number. */
803 /* this case is used for e.g. xyz@PLT-.Label. */
804 exp_p->X_add_number += new_exp.X_add_number;
805 exp_p->X_op_symbol = new_exp.X_add_symbol;
806 exp_p->X_op = O_subtract;
807 str = input_line_pointer;
808 break;
809 default:
810 break;
813 /* If s390_elf_suffix has not been called with
814 &input_line_pointer as first parameter, we have
815 clobbered the input_line_pointer. We have to
816 undo that. */
817 if (&input_line_pointer != str_p)
818 input_line_pointer = orig_line;
820 *str_p = str;
821 return ptr->suffix;
824 return BFD_RELOC_UNUSED;
827 /* Structure used to hold a literal pool entry. */
828 struct s390_lpe
830 struct s390_lpe *next;
831 expressionS ex;
832 FLONUM_TYPE floatnum; /* used if X_op == O_big && X_add_number <= 0 */
833 LITTLENUM_TYPE bignum[4]; /* used if X_op == O_big && X_add_number > 0 */
834 int nbytes;
835 bfd_reloc_code_real_type reloc;
836 symbolS *sym;
839 static struct s390_lpe *lpe_free_list = NULL;
840 static struct s390_lpe *lpe_list = NULL;
841 static struct s390_lpe *lpe_list_tail = NULL;
842 static symbolS *lp_sym = NULL;
843 static int lp_count = 0;
844 static int lpe_count = 0;
846 static int
847 s390_exp_compare (expressionS *exp1, expressionS *exp2)
849 if (exp1->X_op != exp2->X_op)
850 return 0;
852 switch (exp1->X_op)
854 case O_constant: /* X_add_number must be equal. */
855 case O_register:
856 return exp1->X_add_number == exp2->X_add_number;
858 case O_big:
859 as_bad (_("Can't handle O_big in s390_exp_compare"));
861 case O_symbol: /* X_add_symbol & X_add_number must be equal. */
862 case O_symbol_rva:
863 case O_uminus:
864 case O_bit_not:
865 case O_logical_not:
866 return (exp1->X_add_symbol == exp2->X_add_symbol)
867 && (exp1->X_add_number == exp2->X_add_number);
869 case O_multiply: /* X_add_symbol,X_op_symbol&X_add_number must be equal. */
870 case O_divide:
871 case O_modulus:
872 case O_left_shift:
873 case O_right_shift:
874 case O_bit_inclusive_or:
875 case O_bit_or_not:
876 case O_bit_exclusive_or:
877 case O_bit_and:
878 case O_add:
879 case O_subtract:
880 case O_eq:
881 case O_ne:
882 case O_lt:
883 case O_le:
884 case O_ge:
885 case O_gt:
886 case O_logical_and:
887 case O_logical_or:
888 return (exp1->X_add_symbol == exp2->X_add_symbol)
889 && (exp1->X_op_symbol == exp2->X_op_symbol)
890 && (exp1->X_add_number == exp2->X_add_number);
891 default:
892 return 0;
896 /* Test for @lit and if its present make an entry in the literal pool and
897 modify the current expression to be an offset into the literal pool. */
898 static elf_suffix_type
899 s390_lit_suffix (char **str_p, expressionS *exp_p, elf_suffix_type suffix)
901 bfd_reloc_code_real_type reloc;
902 char tmp_name[64];
903 char *str = *str_p;
904 char *ident;
905 struct s390_lpe *lpe;
906 int nbytes, len;
908 if (*str++ != ':')
909 return suffix; /* No modification. */
911 /* We look for a suffix of the form "@lit1", "@lit2", "@lit4" or "@lit8". */
912 ident = str;
913 while (ISALNUM (*str))
914 str++;
915 len = str - ident;
916 if (len != 4 || strncasecmp (ident, "lit", 3) != 0
917 || (ident[3]!='1' && ident[3]!='2' && ident[3]!='4' && ident[3]!='8'))
918 return suffix; /* no modification */
919 nbytes = ident[3] - '0';
921 reloc = BFD_RELOC_UNUSED;
922 if (suffix == ELF_SUFFIX_GOT)
924 if (nbytes == 2)
925 reloc = BFD_RELOC_390_GOT16;
926 else if (nbytes == 4)
927 reloc = BFD_RELOC_32_GOT_PCREL;
928 else if (nbytes == 8)
929 reloc = BFD_RELOC_390_GOT64;
931 else if (suffix == ELF_SUFFIX_PLT)
933 if (nbytes == 4)
934 reloc = BFD_RELOC_390_PLT32;
935 else if (nbytes == 8)
936 reloc = BFD_RELOC_390_PLT64;
939 if (suffix != ELF_SUFFIX_NONE && reloc == BFD_RELOC_UNUSED)
940 as_bad (_("Invalid suffix for literal pool entry"));
942 /* Search the pool if the new entry is a duplicate. */
943 if (exp_p->X_op == O_big)
945 /* Special processing for big numbers. */
946 for (lpe = lpe_list; lpe != NULL; lpe = lpe->next)
948 if (lpe->ex.X_op == O_big)
950 if (exp_p->X_add_number <= 0 && lpe->ex.X_add_number <= 0)
952 if (memcmp (&generic_floating_point_number, &lpe->floatnum,
953 sizeof (FLONUM_TYPE)) == 0)
954 break;
956 else if (exp_p->X_add_number == lpe->ex.X_add_number)
958 if (memcmp (generic_bignum, lpe->bignum,
959 sizeof (LITTLENUM_TYPE)*exp_p->X_add_number) == 0)
960 break;
965 else
967 /* Processing for 'normal' data types. */
968 for (lpe = lpe_list; lpe != NULL; lpe = lpe->next)
969 if (lpe->nbytes == nbytes && lpe->reloc == reloc
970 && s390_exp_compare (exp_p, &lpe->ex) != 0)
971 break;
974 if (lpe == NULL)
976 /* A new literal. */
977 if (lpe_free_list != NULL)
979 lpe = lpe_free_list;
980 lpe_free_list = lpe_free_list->next;
982 else
984 lpe = (struct s390_lpe *) xmalloc (sizeof (struct s390_lpe));
987 lpe->ex = *exp_p;
989 if (exp_p->X_op == O_big)
991 if (exp_p->X_add_number <= 0)
992 lpe->floatnum = generic_floating_point_number;
993 else if (exp_p->X_add_number <= 4)
994 memcpy (lpe->bignum, generic_bignum,
995 exp_p->X_add_number * sizeof (LITTLENUM_TYPE));
996 else
997 as_bad (_("Big number is too big"));
1000 lpe->nbytes = nbytes;
1001 lpe->reloc = reloc;
1002 /* Literal pool name defined ? */
1003 if (lp_sym == NULL)
1005 sprintf (tmp_name, ".L\001%i", lp_count);
1006 lp_sym = symbol_make (tmp_name);
1009 /* Make name for literal pool entry. */
1010 sprintf (tmp_name, ".L\001%i\002%i", lp_count, lpe_count);
1011 lpe_count++;
1012 lpe->sym = symbol_make (tmp_name);
1014 /* Add to literal pool list. */
1015 lpe->next = NULL;
1016 if (lpe_list_tail != NULL)
1018 lpe_list_tail->next = lpe;
1019 lpe_list_tail = lpe;
1021 else
1022 lpe_list = lpe_list_tail = lpe;
1025 /* Now change exp_p to the offset into the literal pool.
1026 Thats the expression: .L^Ax^By-.L^Ax */
1027 exp_p->X_add_symbol = lpe->sym;
1028 exp_p->X_op_symbol = lp_sym;
1029 exp_p->X_op = O_subtract;
1030 exp_p->X_add_number = 0;
1032 *str_p = str;
1034 /* We change the suffix type to ELF_SUFFIX_NONE, because
1035 the difference of two local labels is just a number. */
1036 return ELF_SUFFIX_NONE;
1039 /* Like normal .long/.short/.word, except support @got, etc.
1040 clobbers input_line_pointer, checks end-of-line. */
1041 static void
1042 s390_elf_cons (int nbytes /* 1=.byte, 2=.word, 4=.long */)
1044 expressionS exp;
1045 elf_suffix_type suffix;
1047 if (is_it_end_of_statement ())
1049 demand_empty_rest_of_line ();
1050 return;
1055 expression (&exp);
1057 if (exp.X_op == O_symbol
1058 && *input_line_pointer == '@'
1059 && (suffix = s390_elf_suffix (&input_line_pointer, &exp)) != ELF_SUFFIX_NONE)
1061 bfd_reloc_code_real_type reloc;
1062 reloc_howto_type *reloc_howto;
1063 int size;
1064 char *where;
1066 if (nbytes == 2)
1068 static bfd_reloc_code_real_type tab2[] =
1070 BFD_RELOC_UNUSED, /* ELF_SUFFIX_NONE */
1071 BFD_RELOC_390_GOT16, /* ELF_SUFFIX_GOT */
1072 BFD_RELOC_UNUSED, /* ELF_SUFFIX_PLT */
1073 BFD_RELOC_UNUSED, /* ELF_SUFFIX_GOTENT */
1074 BFD_RELOC_16_GOTOFF, /* ELF_SUFFIX_GOTOFF */
1075 BFD_RELOC_UNUSED, /* ELF_SUFFIX_GOTPLT */
1076 BFD_RELOC_390_PLTOFF16, /* ELF_SUFFIX_PLTOFF */
1077 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_GD */
1078 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_GOTIE */
1079 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_IE */
1080 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_LDM */
1081 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_LDO */
1082 BFD_RELOC_UNUSED /* ELF_SUFFIX_TLS_LE */
1084 reloc = tab2[suffix];
1086 else if (nbytes == 4)
1088 static bfd_reloc_code_real_type tab4[] =
1090 BFD_RELOC_UNUSED, /* ELF_SUFFIX_NONE */
1091 BFD_RELOC_32_GOT_PCREL, /* ELF_SUFFIX_GOT */
1092 BFD_RELOC_390_PLT32, /* ELF_SUFFIX_PLT */
1093 BFD_RELOC_UNUSED, /* ELF_SUFFIX_GOTENT */
1094 BFD_RELOC_32_GOTOFF, /* ELF_SUFFIX_GOTOFF */
1095 BFD_RELOC_390_GOTPLT32, /* ELF_SUFFIX_GOTPLT */
1096 BFD_RELOC_390_PLTOFF32, /* ELF_SUFFIX_PLTOFF */
1097 BFD_RELOC_390_TLS_GD32, /* ELF_SUFFIX_TLS_GD */
1098 BFD_RELOC_390_TLS_GOTIE32, /* ELF_SUFFIX_TLS_GOTIE */
1099 BFD_RELOC_390_TLS_IE32, /* ELF_SUFFIX_TLS_IE */
1100 BFD_RELOC_390_TLS_LDM32, /* ELF_SUFFIX_TLS_LDM */
1101 BFD_RELOC_390_TLS_LDO32, /* ELF_SUFFIX_TLS_LDO */
1102 BFD_RELOC_390_TLS_LE32 /* ELF_SUFFIX_TLS_LE */
1104 reloc = tab4[suffix];
1106 else if (nbytes == 8)
1108 static bfd_reloc_code_real_type tab8[] =
1110 BFD_RELOC_UNUSED, /* ELF_SUFFIX_NONE */
1111 BFD_RELOC_390_GOT64, /* ELF_SUFFIX_GOT */
1112 BFD_RELOC_390_PLT64, /* ELF_SUFFIX_PLT */
1113 BFD_RELOC_UNUSED, /* ELF_SUFFIX_GOTENT */
1114 BFD_RELOC_390_GOTOFF64, /* ELF_SUFFIX_GOTOFF */
1115 BFD_RELOC_390_GOTPLT64, /* ELF_SUFFIX_GOTPLT */
1116 BFD_RELOC_390_PLTOFF64, /* ELF_SUFFIX_PLTOFF */
1117 BFD_RELOC_390_TLS_GD64, /* ELF_SUFFIX_TLS_GD */
1118 BFD_RELOC_390_TLS_GOTIE64, /* ELF_SUFFIX_TLS_GOTIE */
1119 BFD_RELOC_390_TLS_IE64, /* ELF_SUFFIX_TLS_IE */
1120 BFD_RELOC_390_TLS_LDM64, /* ELF_SUFFIX_TLS_LDM */
1121 BFD_RELOC_390_TLS_LDO64, /* ELF_SUFFIX_TLS_LDO */
1122 BFD_RELOC_390_TLS_LE64 /* ELF_SUFFIX_TLS_LE */
1124 reloc = tab8[suffix];
1126 else
1127 reloc = BFD_RELOC_UNUSED;
1129 if (reloc != BFD_RELOC_UNUSED
1130 && (reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc)))
1132 size = bfd_get_reloc_size (reloc_howto);
1133 if (size > nbytes)
1134 as_bad (_("%s relocations do not fit in %d bytes"),
1135 reloc_howto->name, nbytes);
1136 where = frag_more (nbytes);
1137 md_number_to_chars (where, 0, size);
1138 /* To make fixup_segment do the pc relative conversion the
1139 pcrel parameter on the fix_new_exp call needs to be FALSE. */
1140 fix_new_exp (frag_now, where - frag_now->fr_literal,
1141 size, &exp, FALSE, reloc);
1143 else
1144 as_bad (_("relocation not applicable"));
1146 else
1147 emit_expr (&exp, (unsigned int) nbytes);
1149 while (*input_line_pointer++ == ',');
1151 input_line_pointer--; /* Put terminator back into stream. */
1152 demand_empty_rest_of_line ();
1155 /* We need to keep a list of fixups. We can't simply generate them as
1156 we go, because that would require us to first create the frag, and
1157 that would screw up references to ``.''. */
1159 struct s390_fixup
1161 expressionS exp;
1162 int opindex;
1163 bfd_reloc_code_real_type reloc;
1166 #define MAX_INSN_FIXUPS (4)
1168 /* This routine is called for each instruction to be assembled. */
1170 static char *
1171 md_gather_operands (char *str,
1172 unsigned char *insn,
1173 const struct s390_opcode *opcode)
1175 struct s390_fixup fixups[MAX_INSN_FIXUPS];
1176 const struct s390_operand *operand;
1177 const unsigned char *opindex_ptr;
1178 expressionS ex;
1179 elf_suffix_type suffix;
1180 bfd_reloc_code_real_type reloc;
1181 int skip_optional;
1182 char *f;
1183 int fc, i;
1185 while (ISSPACE (*str))
1186 str++;
1188 skip_optional = 0;
1190 /* Gather the operands. */
1191 fc = 0;
1192 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1194 char *hold;
1196 operand = s390_operands + *opindex_ptr;
1198 if (skip_optional && (operand->flags & S390_OPERAND_INDEX))
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;
1208 /* Gather the operand. */
1209 hold = input_line_pointer;
1210 input_line_pointer = str;
1212 /* Parse the operand. */
1213 if (! register_name (&ex))
1214 expression (&ex);
1216 str = input_line_pointer;
1217 input_line_pointer = hold;
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)
1224 /* No operands, check if all operands can be skipped. */
1225 while (*opindex_ptr != 0 && operand->flags & S390_OPERAND_OPTIONAL)
1227 if (operand->flags & S390_OPERAND_DISP)
1229 /* An optional displacement makes the whole D(X,B)
1230 D(L,B) or D(B) block optional. */
1231 do {
1232 operand = s390_operands + *(++opindex_ptr);
1233 } while (!(operand->flags & S390_OPERAND_BASE));
1235 operand = s390_operands + *(++opindex_ptr);
1237 if (opindex_ptr[0] == '\0')
1238 break;
1239 as_bad (_("missing operand"));
1241 else if (ex.X_op == O_register || ex.X_op == O_constant)
1243 s390_lit_suffix (&str, &ex, ELF_SUFFIX_NONE);
1245 if (ex.X_op != O_register && ex.X_op != O_constant)
1247 /* We need to generate a fixup for the
1248 expression returned by s390_lit_suffix. */
1249 if (fc >= MAX_INSN_FIXUPS)
1250 as_fatal (_("too many fixups"));
1251 fixups[fc].exp = ex;
1252 fixups[fc].opindex = *opindex_ptr;
1253 fixups[fc].reloc = BFD_RELOC_UNUSED;
1254 ++fc;
1256 else
1258 if ((operand->flags & S390_OPERAND_INDEX)
1259 && ex.X_add_number == 0
1260 && warn_areg_zero)
1261 as_warn (_("index register specified but zero"));
1262 if ((operand->flags & S390_OPERAND_BASE)
1263 && ex.X_add_number == 0
1264 && warn_areg_zero)
1265 as_warn (_("base register specified but zero"));
1266 if ((operand->flags & S390_OPERAND_GPR)
1267 && (operand->flags & S390_OPERAND_REG_PAIR)
1268 && (ex.X_add_number & 1))
1269 as_fatal (_("odd numbered general purpose register specified as "
1270 "register pair"));
1271 if ((operand->flags & S390_OPERAND_FPR)
1272 && (operand->flags & S390_OPERAND_REG_PAIR)
1273 && ex.X_add_number != 0 && ex.X_add_number != 1
1274 && ex.X_add_number != 4 && ex.X_add_number != 5
1275 && ex.X_add_number != 8 && ex.X_add_number != 9
1276 && ex.X_add_number != 12 && ex.X_add_number != 13)
1277 as_fatal (_("invalid floating point register pair. Valid fp "
1278 "register pair operands are 0, 1, 4, 5, 8, 9, "
1279 "12 or 13."));
1280 s390_insert_operand (insn, operand, ex.X_add_number, NULL, 0);
1283 else
1285 suffix = s390_elf_suffix (&str, &ex);
1286 suffix = s390_lit_suffix (&str, &ex, suffix);
1287 reloc = BFD_RELOC_UNUSED;
1289 if (suffix == ELF_SUFFIX_GOT)
1291 if ((operand->flags & S390_OPERAND_DISP) &&
1292 (operand->bits == 12))
1293 reloc = BFD_RELOC_390_GOT12;
1294 else if ((operand->flags & S390_OPERAND_DISP) &&
1295 (operand->bits == 20))
1296 reloc = BFD_RELOC_390_GOT20;
1297 else if ((operand->flags & S390_OPERAND_SIGNED)
1298 && (operand->bits == 16))
1299 reloc = BFD_RELOC_390_GOT16;
1300 else if ((operand->flags & S390_OPERAND_PCREL)
1301 && (operand->bits == 32))
1302 reloc = BFD_RELOC_390_GOTENT;
1304 else if (suffix == ELF_SUFFIX_PLT)
1306 if ((operand->flags & S390_OPERAND_PCREL)
1307 && (operand->bits == 16))
1308 reloc = BFD_RELOC_390_PLT16DBL;
1309 else if ((operand->flags & S390_OPERAND_PCREL)
1310 && (operand->bits == 32))
1311 reloc = BFD_RELOC_390_PLT32DBL;
1313 else if (suffix == ELF_SUFFIX_GOTENT)
1315 if ((operand->flags & S390_OPERAND_PCREL)
1316 && (operand->bits == 32))
1317 reloc = BFD_RELOC_390_GOTENT;
1319 else if (suffix == ELF_SUFFIX_GOTOFF)
1321 if ((operand->flags & S390_OPERAND_SIGNED)
1322 && (operand->bits == 16))
1323 reloc = BFD_RELOC_16_GOTOFF;
1325 else if (suffix == ELF_SUFFIX_PLTOFF)
1327 if ((operand->flags & S390_OPERAND_SIGNED)
1328 && (operand->bits == 16))
1329 reloc = BFD_RELOC_390_PLTOFF16;
1331 else if (suffix == ELF_SUFFIX_GOTPLT)
1333 if ((operand->flags & S390_OPERAND_DISP)
1334 && (operand->bits == 12))
1335 reloc = BFD_RELOC_390_GOTPLT12;
1336 else if ((operand->flags & S390_OPERAND_SIGNED)
1337 && (operand->bits == 16))
1338 reloc = BFD_RELOC_390_GOTPLT16;
1339 else if ((operand->flags & S390_OPERAND_PCREL)
1340 && (operand->bits == 32))
1341 reloc = BFD_RELOC_390_GOTPLTENT;
1343 else if (suffix == ELF_SUFFIX_TLS_GOTIE)
1345 if ((operand->flags & S390_OPERAND_DISP)
1346 && (operand->bits == 12))
1347 reloc = BFD_RELOC_390_TLS_GOTIE12;
1348 else if ((operand->flags & S390_OPERAND_DISP)
1349 && (operand->bits == 20))
1350 reloc = BFD_RELOC_390_TLS_GOTIE20;
1352 else if (suffix == ELF_SUFFIX_TLS_IE)
1354 if ((operand->flags & S390_OPERAND_PCREL)
1355 && (operand->bits == 32))
1356 reloc = BFD_RELOC_390_TLS_IEENT;
1359 if (suffix != ELF_SUFFIX_NONE && reloc == BFD_RELOC_UNUSED)
1360 as_bad (_("invalid operand suffix"));
1361 /* We need to generate a fixup of type 'reloc' for this
1362 expression. */
1363 if (fc >= MAX_INSN_FIXUPS)
1364 as_fatal (_("too many fixups"));
1365 fixups[fc].exp = ex;
1366 fixups[fc].opindex = *opindex_ptr;
1367 fixups[fc].reloc = reloc;
1368 ++fc;
1371 /* Check the next character. The call to expression has advanced
1372 str past any whitespace. */
1373 if (operand->flags & S390_OPERAND_DISP)
1375 /* After a displacement a block in parentheses can start. */
1376 if (*str != '(')
1378 /* Check if parenthesized block can be skipped. If the next
1379 operand is neiter an optional operand nor a base register
1380 then we have a syntax error. */
1381 operand = s390_operands + *(++opindex_ptr);
1382 if (!(operand->flags & (S390_OPERAND_INDEX|S390_OPERAND_BASE)))
1383 as_bad (_("syntax error; missing '(' after displacement"));
1385 /* Ok, skip all operands until S390_OPERAND_BASE. */
1386 while (!(operand->flags & S390_OPERAND_BASE))
1387 operand = s390_operands + *(++opindex_ptr);
1389 /* If there is a next operand it must be separated by a comma. */
1390 if (opindex_ptr[1] != '\0')
1392 if (*str != ',')
1394 while (opindex_ptr[1] != '\0')
1396 operand = s390_operands + *(++opindex_ptr);
1397 if (operand->flags & S390_OPERAND_OPTIONAL)
1398 continue;
1399 as_bad (_("syntax error; expected ,"));
1400 break;
1403 else
1404 str++;
1407 else
1409 /* We found an opening parentheses. */
1410 str++;
1411 for (f = str; *f != '\0'; f++)
1412 if (*f == ',' || *f == ')')
1413 break;
1414 /* If there is no comma until the closing parentheses OR
1415 there is a comma right after the opening parentheses,
1416 we have to skip optional operands. */
1417 if (*f == ',' && f == str)
1419 /* comma directly after '(' ? */
1420 skip_optional = 1;
1421 str++;
1423 else
1424 skip_optional = (*f != ',');
1427 else if (operand->flags & S390_OPERAND_BASE)
1429 /* After the base register the parenthesed block ends. */
1430 if (*str++ != ')')
1431 as_bad (_("syntax error; missing ')' after base register"));
1432 skip_optional = 0;
1433 /* If there is a next operand it must be separated by a comma. */
1434 if (opindex_ptr[1] != '\0')
1436 if (*str != ',')
1438 while (opindex_ptr[1] != '\0')
1440 operand = s390_operands + *(++opindex_ptr);
1441 if (operand->flags & S390_OPERAND_OPTIONAL)
1442 continue;
1443 as_bad (_("syntax error; expected ,"));
1444 break;
1447 else
1448 str++;
1451 else
1453 /* We can find an 'early' closing parentheses in e.g. D(L) instead
1454 of D(L,B). In this case the base register has to be skipped. */
1455 if (*str == ')')
1457 operand = s390_operands + *(++opindex_ptr);
1459 if (!(operand->flags & S390_OPERAND_BASE))
1460 as_bad (_("syntax error; ')' not allowed here"));
1461 str++;
1463 /* If there is a next operand it must be separated by a comma. */
1464 if (opindex_ptr[1] != '\0')
1466 if (*str != ',')
1468 while (opindex_ptr[1] != '\0')
1470 operand = s390_operands + *(++opindex_ptr);
1471 if (operand->flags & S390_OPERAND_OPTIONAL)
1472 continue;
1473 as_bad (_("syntax error; expected ,"));
1474 break;
1477 else
1478 str++;
1483 while (ISSPACE (*str))
1484 ++str;
1486 /* Check for tls instruction marker. */
1487 reloc = s390_tls_suffix (&str, &ex);
1488 if (reloc != BFD_RELOC_UNUSED)
1490 /* We need to generate a fixup of type 'reloc' for this
1491 instruction. */
1492 if (fc >= MAX_INSN_FIXUPS)
1493 as_fatal (_("too many fixups"));
1494 fixups[fc].exp = ex;
1495 fixups[fc].opindex = -1;
1496 fixups[fc].reloc = reloc;
1497 ++fc;
1500 if (*str != '\0')
1502 char *linefeed;
1504 if ((linefeed = strchr (str, '\n')) != NULL)
1505 *linefeed = '\0';
1506 as_bad (_("junk at end of line: `%s'"), str);
1507 if (linefeed != NULL)
1508 *linefeed = '\n';
1511 /* Write out the instruction. */
1512 f = frag_more (opcode->oplen);
1513 memcpy (f, insn, opcode->oplen);
1514 dwarf2_emit_insn (opcode->oplen);
1516 /* Create any fixups. At this point we do not use a
1517 bfd_reloc_code_real_type, but instead just use the
1518 BFD_RELOC_UNUSED plus the operand index. This lets us easily
1519 handle fixups for any operand type, although that is admittedly
1520 not a very exciting feature. We pick a BFD reloc type in
1521 md_apply_fix. */
1522 for (i = 0; i < fc; i++)
1525 if (fixups[i].opindex < 0)
1527 /* Create tls instruction marker relocation. */
1528 fix_new_exp (frag_now, f - frag_now->fr_literal, opcode->oplen,
1529 &fixups[i].exp, 0, fixups[i].reloc);
1530 continue;
1533 operand = s390_operands + fixups[i].opindex;
1535 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1537 reloc_howto_type *reloc_howto;
1538 fixS *fixP;
1539 int size;
1541 reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1542 if (!reloc_howto)
1543 abort ();
1545 size = bfd_get_reloc_size (reloc_howto);
1547 if (size < 1 || size > 4)
1548 abort ();
1550 fixP = fix_new_exp (frag_now,
1551 f - frag_now->fr_literal + (operand->shift/8),
1552 size, &fixups[i].exp, reloc_howto->pc_relative,
1553 fixups[i].reloc);
1554 /* Turn off overflow checking in fixup_segment. This is necessary
1555 because fixup_segment will signal an overflow for large 4 byte
1556 quantities for GOT12 relocations. */
1557 if ( fixups[i].reloc == BFD_RELOC_390_GOT12
1558 || fixups[i].reloc == BFD_RELOC_390_GOT20
1559 || fixups[i].reloc == BFD_RELOC_390_GOT16)
1560 fixP->fx_no_overflow = 1;
1562 else
1563 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, &fixups[i].exp,
1564 (operand->flags & S390_OPERAND_PCREL) != 0,
1565 ((bfd_reloc_code_real_type)
1566 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
1568 return str;
1571 /* This routine is called for each instruction to be assembled. */
1573 void
1574 md_assemble (char *str)
1576 const struct s390_opcode *opcode;
1577 unsigned char insn[6];
1578 char *s;
1580 /* Get the opcode. */
1581 for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
1583 if (*s != '\0')
1584 *s++ = '\0';
1586 /* Look up the opcode in the hash table. */
1587 opcode = (struct s390_opcode *) hash_find (s390_opcode_hash, str);
1588 if (opcode == (const struct s390_opcode *) NULL)
1590 as_bad (_("Unrecognized opcode: `%s'"), str);
1591 return;
1593 else if (!(opcode->modes & current_mode_mask))
1595 as_bad (_("Opcode %s not available in this mode"), str);
1596 return;
1598 memcpy (insn, opcode->opcode, sizeof (insn));
1599 md_gather_operands (s, insn, opcode);
1602 #ifndef WORKING_DOT_WORD
1603 /* Handle long and short jumps. We don't support these */
1604 void
1605 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1606 char *ptr;
1607 addressT from_addr, to_addr;
1608 fragS *frag;
1609 symbolS *to_symbol;
1611 abort ();
1614 void
1615 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1616 char *ptr;
1617 addressT from_addr, to_addr;
1618 fragS *frag;
1619 symbolS *to_symbol;
1621 abort ();
1623 #endif
1625 void
1626 s390_bss (int ignore ATTRIBUTE_UNUSED)
1628 /* We don't support putting frags in the BSS segment, we fake it
1629 by marking in_bss, then looking at s_skip for clues. */
1631 subseg_set (bss_section, 0);
1632 demand_empty_rest_of_line ();
1635 /* Pseudo-op handling. */
1637 void
1638 s390_insn (int ignore ATTRIBUTE_UNUSED)
1640 expressionS exp;
1641 const struct s390_opcode *opformat;
1642 unsigned char insn[6];
1643 char *s;
1645 /* Get the opcode format. */
1646 s = input_line_pointer;
1647 while (*s != '\0' && *s != ',' && ! ISSPACE (*s))
1648 s++;
1649 if (*s != ',')
1650 as_bad (_("Invalid .insn format\n"));
1651 *s++ = '\0';
1653 /* Look up the opcode in the hash table. */
1654 opformat = (struct s390_opcode *)
1655 hash_find (s390_opformat_hash, input_line_pointer);
1656 if (opformat == (const struct s390_opcode *) NULL)
1658 as_bad (_("Unrecognized opcode format: `%s'"), input_line_pointer);
1659 return;
1661 input_line_pointer = s;
1662 expression (&exp);
1663 if (exp.X_op == O_constant)
1665 if ( ( opformat->oplen == 6
1666 && (addressT) exp.X_add_number < (1ULL << 48))
1667 || ( opformat->oplen == 4
1668 && (addressT) exp.X_add_number < (1ULL << 32))
1669 || ( opformat->oplen == 2
1670 && (addressT) exp.X_add_number < (1ULL << 16)))
1671 md_number_to_chars ((char *) insn, exp.X_add_number, opformat->oplen);
1672 else
1673 as_bad (_("Invalid .insn format\n"));
1675 else if (exp.X_op == O_big)
1677 if (exp.X_add_number > 0
1678 && opformat->oplen == 6
1679 && generic_bignum[3] == 0)
1681 md_number_to_chars ((char *) insn, generic_bignum[2], 2);
1682 md_number_to_chars ((char *) &insn[2], generic_bignum[1], 2);
1683 md_number_to_chars ((char *) &insn[4], generic_bignum[0], 2);
1685 else
1686 as_bad (_("Invalid .insn format\n"));
1688 else
1689 as_bad (_("second operand of .insn not a constant\n"));
1691 if (strcmp (opformat->name, "e") != 0 && *input_line_pointer++ != ',')
1692 as_bad (_("missing comma after insn constant\n"));
1694 if ((s = strchr (input_line_pointer, '\n')) != NULL)
1695 *s = '\0';
1696 input_line_pointer = md_gather_operands (input_line_pointer, insn,
1697 opformat);
1698 if (s != NULL)
1699 *s = '\n';
1700 demand_empty_rest_of_line ();
1703 /* The .byte pseudo-op. This is similar to the normal .byte
1704 pseudo-op, but it can also take a single ASCII string. */
1706 static void
1707 s390_byte (int ignore ATTRIBUTE_UNUSED)
1709 if (*input_line_pointer != '\"')
1711 cons (1);
1712 return;
1715 /* Gather characters. A real double quote is doubled. Unusual
1716 characters are not permitted. */
1717 ++input_line_pointer;
1718 while (1)
1720 char c;
1722 c = *input_line_pointer++;
1724 if (c == '\"')
1726 if (*input_line_pointer != '\"')
1727 break;
1728 ++input_line_pointer;
1731 FRAG_APPEND_1_CHAR (c);
1734 demand_empty_rest_of_line ();
1737 /* The .ltorg pseudo-op.This emits all literals defined since the last
1738 .ltorg or the invocation of gas. Literals are defined with the
1739 @lit suffix. */
1741 static void
1742 s390_literals (int ignore ATTRIBUTE_UNUSED)
1744 struct s390_lpe *lpe;
1746 if (lp_sym == NULL || lpe_count == 0)
1747 return; /* Nothing to be done. */
1749 /* Emit symbol for start of literal pool. */
1750 S_SET_SEGMENT (lp_sym, now_seg);
1751 S_SET_VALUE (lp_sym, (valueT) frag_now_fix ());
1752 lp_sym->sy_frag = frag_now;
1754 while (lpe_list)
1756 lpe = lpe_list;
1757 lpe_list = lpe_list->next;
1758 S_SET_SEGMENT (lpe->sym, now_seg);
1759 S_SET_VALUE (lpe->sym, (valueT) frag_now_fix ());
1760 lpe->sym->sy_frag = frag_now;
1762 /* Emit literal pool entry. */
1763 if (lpe->reloc != BFD_RELOC_UNUSED)
1765 reloc_howto_type *reloc_howto =
1766 bfd_reloc_type_lookup (stdoutput, lpe->reloc);
1767 int size = bfd_get_reloc_size (reloc_howto);
1768 char *where;
1770 if (size > lpe->nbytes)
1771 as_bad (_("%s relocations do not fit in %d bytes"),
1772 reloc_howto->name, lpe->nbytes);
1773 where = frag_more (lpe->nbytes);
1774 md_number_to_chars (where, 0, size);
1775 fix_new_exp (frag_now, where - frag_now->fr_literal,
1776 size, &lpe->ex, reloc_howto->pc_relative, lpe->reloc);
1778 else
1780 if (lpe->ex.X_op == O_big)
1782 if (lpe->ex.X_add_number <= 0)
1783 generic_floating_point_number = lpe->floatnum;
1784 else
1785 memcpy (generic_bignum, lpe->bignum,
1786 lpe->ex.X_add_number * sizeof (LITTLENUM_TYPE));
1788 emit_expr (&lpe->ex, lpe->nbytes);
1791 lpe->next = lpe_free_list;
1792 lpe_free_list = lpe;
1794 lpe_list_tail = NULL;
1795 lp_sym = NULL;
1796 lp_count++;
1797 lpe_count = 0;
1800 /* The .machine pseudo op allows to switch to a different CPU level in
1801 the asm listing. The current CPU setting can be stored on a stack
1802 with .machine push and restored with .machined pop. */
1804 static void
1805 s390_machine (int ignore ATTRIBUTE_UNUSED)
1807 char *cpu_string;
1808 #define MAX_HISTORY 100
1809 static unsigned int *cpu_history;
1810 static int curr_hist;
1812 SKIP_WHITESPACE ();
1814 if (*input_line_pointer == '"')
1816 int len;
1817 cpu_string = demand_copy_C_string (&len);
1819 else
1821 char c;
1822 cpu_string = input_line_pointer;
1823 c = get_symbol_end ();
1824 cpu_string = xstrdup (cpu_string);
1825 *input_line_pointer = c;
1828 if (cpu_string != NULL)
1830 unsigned int old_cpu = current_cpu;
1831 unsigned int new_cpu;
1832 char *p;
1834 for (p = cpu_string; *p != 0; p++)
1835 *p = TOLOWER (*p);
1837 if (strcmp (cpu_string, "push") == 0)
1839 if (cpu_history == NULL)
1840 cpu_history = xmalloc (MAX_HISTORY * sizeof (*cpu_history));
1842 if (curr_hist >= MAX_HISTORY)
1843 as_bad (_(".machine stack overflow"));
1844 else
1845 cpu_history[curr_hist++] = current_cpu;
1847 else if (strcmp (cpu_string, "pop") == 0)
1849 if (curr_hist <= 0)
1850 as_bad (_(".machine stack underflow"));
1851 else
1852 current_cpu = cpu_history[--curr_hist];
1854 else if ((new_cpu = s390_parse_cpu (cpu_string)) != (unsigned int)-1)
1855 current_cpu = new_cpu;
1856 else
1857 as_bad (_("invalid machine `%s'"), cpu_string);
1859 if (current_cpu != old_cpu)
1860 s390_setup_opcodes ();
1863 demand_empty_rest_of_line ();
1866 char *
1867 md_atof (int type, char *litp, int *sizep)
1869 return ieee_md_atof (type, litp, sizep, TRUE);
1872 /* Align a section (I don't know why this is machine dependent). */
1874 valueT
1875 md_section_align (asection *seg, valueT addr)
1877 int align = bfd_get_section_alignment (stdoutput, seg);
1879 return ((addr + (1 << align) - 1) & (-1 << align));
1882 /* We don't have any form of relaxing. */
1885 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
1886 asection *seg ATTRIBUTE_UNUSED)
1888 abort ();
1889 return 0;
1892 /* Convert a machine dependent frag. We never generate these. */
1894 void
1895 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
1896 asection *sec ATTRIBUTE_UNUSED,
1897 fragS *fragp ATTRIBUTE_UNUSED)
1899 abort ();
1902 symbolS *
1903 md_undefined_symbol (char *name)
1905 if (*name == '_' && *(name + 1) == 'G'
1906 && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
1908 if (!GOT_symbol)
1910 if (symbol_find (name))
1911 as_bad (_("GOT already in symbol table"));
1912 GOT_symbol = symbol_new (name, undefined_section,
1913 (valueT) 0, &zero_address_frag);
1915 return GOT_symbol;
1917 return 0;
1920 /* Functions concerning relocs. */
1922 /* The location from which a PC relative jump should be calculated,
1923 given a PC relative reloc. */
1925 long
1926 md_pcrel_from_section (fixS *fixp, segT sec ATTRIBUTE_UNUSED)
1928 return fixp->fx_frag->fr_address + fixp->fx_where;
1931 /* Here we decide which fixups can be adjusted to make them relative to
1932 the beginning of the section instead of the symbol. Basically we need
1933 to make sure that the dynamic relocations are done correctly, so in
1934 some cases we force the original symbol to be used. */
1936 tc_s390_fix_adjustable (fixS *fixP)
1938 /* Don't adjust references to merge sections. */
1939 if ((S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0)
1940 return 0;
1941 /* adjust_reloc_syms doesn't know about the GOT. */
1942 if ( fixP->fx_r_type == BFD_RELOC_16_GOTOFF
1943 || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
1944 || fixP->fx_r_type == BFD_RELOC_390_GOTOFF64
1945 || fixP->fx_r_type == BFD_RELOC_390_PLTOFF16
1946 || fixP->fx_r_type == BFD_RELOC_390_PLTOFF32
1947 || fixP->fx_r_type == BFD_RELOC_390_PLTOFF64
1948 || fixP->fx_r_type == BFD_RELOC_390_PLT16DBL
1949 || fixP->fx_r_type == BFD_RELOC_390_PLT32
1950 || fixP->fx_r_type == BFD_RELOC_390_PLT32DBL
1951 || fixP->fx_r_type == BFD_RELOC_390_PLT64
1952 || fixP->fx_r_type == BFD_RELOC_390_GOT12
1953 || fixP->fx_r_type == BFD_RELOC_390_GOT20
1954 || fixP->fx_r_type == BFD_RELOC_390_GOT16
1955 || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
1956 || fixP->fx_r_type == BFD_RELOC_390_GOT64
1957 || fixP->fx_r_type == BFD_RELOC_390_GOTENT
1958 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT12
1959 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT16
1960 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT20
1961 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT32
1962 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT64
1963 || fixP->fx_r_type == BFD_RELOC_390_GOTPLTENT
1964 || fixP->fx_r_type == BFD_RELOC_390_TLS_LOAD
1965 || fixP->fx_r_type == BFD_RELOC_390_TLS_GDCALL
1966 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDCALL
1967 || fixP->fx_r_type == BFD_RELOC_390_TLS_GD32
1968 || fixP->fx_r_type == BFD_RELOC_390_TLS_GD64
1969 || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE12
1970 || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE20
1971 || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE32
1972 || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE64
1973 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDM32
1974 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDM64
1975 || fixP->fx_r_type == BFD_RELOC_390_TLS_IE32
1976 || fixP->fx_r_type == BFD_RELOC_390_TLS_IE64
1977 || fixP->fx_r_type == BFD_RELOC_390_TLS_IEENT
1978 || fixP->fx_r_type == BFD_RELOC_390_TLS_LE32
1979 || fixP->fx_r_type == BFD_RELOC_390_TLS_LE64
1980 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDO32
1981 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDO64
1982 || fixP->fx_r_type == BFD_RELOC_390_TLS_DTPMOD
1983 || fixP->fx_r_type == BFD_RELOC_390_TLS_DTPOFF
1984 || fixP->fx_r_type == BFD_RELOC_390_TLS_TPOFF
1985 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1986 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1987 return 0;
1988 return 1;
1991 /* Return true if we must always emit a reloc for a type and false if
1992 there is some hope of resolving it at assembly time. */
1994 tc_s390_force_relocation (struct fix *fixp)
1996 /* Ensure we emit a relocation for every reference to the global
1997 offset table or to the procedure link table. */
1998 switch (fixp->fx_r_type)
2000 case BFD_RELOC_390_GOT12:
2001 case BFD_RELOC_390_GOT20:
2002 case BFD_RELOC_32_GOT_PCREL:
2003 case BFD_RELOC_32_GOTOFF:
2004 case BFD_RELOC_390_GOTOFF64:
2005 case BFD_RELOC_390_PLTOFF16:
2006 case BFD_RELOC_390_PLTOFF32:
2007 case BFD_RELOC_390_PLTOFF64:
2008 case BFD_RELOC_390_GOTPC:
2009 case BFD_RELOC_390_GOT16:
2010 case BFD_RELOC_390_GOTPCDBL:
2011 case BFD_RELOC_390_GOT64:
2012 case BFD_RELOC_390_GOTENT:
2013 case BFD_RELOC_390_PLT32:
2014 case BFD_RELOC_390_PLT16DBL:
2015 case BFD_RELOC_390_PLT32DBL:
2016 case BFD_RELOC_390_PLT64:
2017 case BFD_RELOC_390_GOTPLT12:
2018 case BFD_RELOC_390_GOTPLT16:
2019 case BFD_RELOC_390_GOTPLT20:
2020 case BFD_RELOC_390_GOTPLT32:
2021 case BFD_RELOC_390_GOTPLT64:
2022 case BFD_RELOC_390_GOTPLTENT:
2023 return 1;
2024 default:
2025 break;;
2028 return generic_force_reloc (fixp);
2031 /* Apply a fixup to the object code. This is called for all the
2032 fixups we generated by the call to fix_new_exp, above. In the call
2033 above we used a reloc code which was the largest legal reloc code
2034 plus the operand index. Here we undo that to recover the operand
2035 index. At this point all symbol values should be fully resolved,
2036 and we attempt to completely resolve the reloc. If we can not do
2037 that, we determine the correct reloc code and put it back in the
2038 fixup. */
2040 void
2041 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
2043 char *where;
2044 valueT value = *valP;
2046 where = fixP->fx_frag->fr_literal + fixP->fx_where;
2048 if (fixP->fx_subsy != NULL)
2049 as_bad_where (fixP->fx_file, fixP->fx_line,
2050 _("cannot emit relocation %s against subsy symbol %s"),
2051 bfd_get_reloc_code_name (fixP->fx_r_type),
2052 S_GET_NAME (fixP->fx_subsy));
2054 if (fixP->fx_addsy != NULL)
2056 if (fixP->fx_pcrel)
2057 value += fixP->fx_frag->fr_address + fixP->fx_where;
2059 else
2060 fixP->fx_done = 1;
2062 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
2064 const struct s390_operand *operand;
2065 int opindex;
2067 opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
2068 operand = &s390_operands[opindex];
2070 if (fixP->fx_done)
2072 /* Insert the fully resolved operand value. */
2073 s390_insert_operand ((unsigned char *) where, operand,
2074 (offsetT) value, fixP->fx_file, fixP->fx_line);
2075 return;
2078 /* Determine a BFD reloc value based on the operand information.
2079 We are only prepared to turn a few of the operands into
2080 relocs. */
2081 fixP->fx_offset = value;
2082 if (operand->bits == 12 && operand->shift == 20)
2084 fixP->fx_size = 2;
2085 fixP->fx_where += 2;
2086 fixP->fx_r_type = BFD_RELOC_390_12;
2088 else if (operand->bits == 12 && operand->shift == 36)
2090 fixP->fx_size = 2;
2091 fixP->fx_where += 4;
2092 fixP->fx_r_type = BFD_RELOC_390_12;
2094 else if (operand->bits == 20 && operand->shift == 20)
2096 fixP->fx_size = 2;
2097 fixP->fx_where += 2;
2098 fixP->fx_r_type = BFD_RELOC_390_20;
2100 else if (operand->bits == 8 && operand->shift == 8)
2102 fixP->fx_size = 1;
2103 fixP->fx_where += 1;
2104 fixP->fx_r_type = BFD_RELOC_8;
2106 else if (operand->bits == 16 && operand->shift == 16)
2108 fixP->fx_size = 2;
2109 fixP->fx_where += 2;
2110 if (operand->flags & S390_OPERAND_PCREL)
2112 fixP->fx_r_type = BFD_RELOC_390_PC16DBL;
2113 fixP->fx_offset += 2;
2115 else
2116 fixP->fx_r_type = BFD_RELOC_16;
2118 else if (operand->bits == 32 && operand->shift == 16
2119 && (operand->flags & S390_OPERAND_PCREL))
2121 fixP->fx_size = 4;
2122 fixP->fx_where += 2;
2123 fixP->fx_offset += 2;
2124 fixP->fx_r_type = BFD_RELOC_390_PC32DBL;
2126 else
2128 char *sfile;
2129 unsigned int sline;
2131 /* Use expr_symbol_where to see if this is an expression
2132 symbol. */
2133 if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
2134 as_bad_where (fixP->fx_file, fixP->fx_line,
2135 _("unresolved expression that must be resolved"));
2136 else
2137 as_bad_where (fixP->fx_file, fixP->fx_line,
2138 _("unsupported relocation type"));
2139 fixP->fx_done = 1;
2140 return;
2143 else
2145 switch (fixP->fx_r_type)
2147 case BFD_RELOC_8:
2148 if (fixP->fx_pcrel)
2149 abort ();
2150 if (fixP->fx_done)
2151 md_number_to_chars (where, value, 1);
2152 break;
2153 case BFD_RELOC_390_12:
2154 case BFD_RELOC_390_GOT12:
2155 case BFD_RELOC_390_GOTPLT12:
2156 if (fixP->fx_done)
2158 unsigned short mop;
2160 mop = bfd_getb16 ((unsigned char *) where);
2161 mop |= (unsigned short) (value & 0xfff);
2162 bfd_putb16 ((bfd_vma) mop, (unsigned char *) where);
2164 break;
2166 case BFD_RELOC_390_20:
2167 case BFD_RELOC_390_GOT20:
2168 case BFD_RELOC_390_GOTPLT20:
2169 if (fixP->fx_done)
2171 unsigned int mop;
2172 mop = bfd_getb32 ((unsigned char *) where);
2173 mop |= (unsigned int) ((value & 0xfff) << 8 |
2174 (value & 0xff000) >> 12);
2175 bfd_putb32 ((bfd_vma) mop, (unsigned char *) where);
2177 break;
2179 case BFD_RELOC_16:
2180 case BFD_RELOC_GPREL16:
2181 case BFD_RELOC_16_GOT_PCREL:
2182 case BFD_RELOC_16_GOTOFF:
2183 if (fixP->fx_pcrel)
2184 as_bad_where (fixP->fx_file, fixP->fx_line,
2185 _("cannot emit PC relative %s relocation%s%s"),
2186 bfd_get_reloc_code_name (fixP->fx_r_type),
2187 fixP->fx_addsy != NULL ? " against " : "",
2188 (fixP->fx_addsy != NULL
2189 ? S_GET_NAME (fixP->fx_addsy)
2190 : ""));
2191 if (fixP->fx_done)
2192 md_number_to_chars (where, value, 2);
2193 break;
2194 case BFD_RELOC_390_GOT16:
2195 case BFD_RELOC_390_PLTOFF16:
2196 case BFD_RELOC_390_GOTPLT16:
2197 if (fixP->fx_done)
2198 md_number_to_chars (where, value, 2);
2199 break;
2200 case BFD_RELOC_390_PC16DBL:
2201 case BFD_RELOC_390_PLT16DBL:
2202 value += 2;
2203 if (fixP->fx_done)
2204 md_number_to_chars (where, (offsetT) value >> 1, 2);
2205 break;
2207 case BFD_RELOC_32:
2208 if (fixP->fx_pcrel)
2209 fixP->fx_r_type = BFD_RELOC_32_PCREL;
2210 else
2211 fixP->fx_r_type = BFD_RELOC_32;
2212 if (fixP->fx_done)
2213 md_number_to_chars (where, value, 4);
2214 break;
2215 case BFD_RELOC_32_PCREL:
2216 case BFD_RELOC_32_BASEREL:
2217 fixP->fx_r_type = BFD_RELOC_32_PCREL;
2218 if (fixP->fx_done)
2219 md_number_to_chars (where, value, 4);
2220 break;
2221 case BFD_RELOC_32_GOT_PCREL:
2222 case BFD_RELOC_390_PLTOFF32:
2223 case BFD_RELOC_390_PLT32:
2224 case BFD_RELOC_390_GOTPLT32:
2225 if (fixP->fx_done)
2226 md_number_to_chars (where, value, 4);
2227 break;
2228 case BFD_RELOC_390_PC32DBL:
2229 case BFD_RELOC_390_PLT32DBL:
2230 case BFD_RELOC_390_GOTPCDBL:
2231 case BFD_RELOC_390_GOTENT:
2232 case BFD_RELOC_390_GOTPLTENT:
2233 value += 2;
2234 if (fixP->fx_done)
2235 md_number_to_chars (where, (offsetT) value >> 1, 4);
2236 break;
2238 case BFD_RELOC_32_GOTOFF:
2239 if (fixP->fx_done)
2240 md_number_to_chars (where, value, sizeof (int));
2241 break;
2243 case BFD_RELOC_390_GOTOFF64:
2244 if (fixP->fx_done)
2245 md_number_to_chars (where, value, 8);
2246 break;
2248 case BFD_RELOC_390_GOT64:
2249 case BFD_RELOC_390_PLTOFF64:
2250 case BFD_RELOC_390_PLT64:
2251 case BFD_RELOC_390_GOTPLT64:
2252 if (fixP->fx_done)
2253 md_number_to_chars (where, value, 8);
2254 break;
2256 case BFD_RELOC_64:
2257 if (fixP->fx_pcrel)
2258 fixP->fx_r_type = BFD_RELOC_64_PCREL;
2259 else
2260 fixP->fx_r_type = BFD_RELOC_64;
2261 if (fixP->fx_done)
2262 md_number_to_chars (where, value, 8);
2263 break;
2265 case BFD_RELOC_64_PCREL:
2266 fixP->fx_r_type = BFD_RELOC_64_PCREL;
2267 if (fixP->fx_done)
2268 md_number_to_chars (where, value, 8);
2269 break;
2271 case BFD_RELOC_VTABLE_INHERIT:
2272 case BFD_RELOC_VTABLE_ENTRY:
2273 fixP->fx_done = 0;
2274 return;
2276 case BFD_RELOC_390_TLS_LOAD:
2277 case BFD_RELOC_390_TLS_GDCALL:
2278 case BFD_RELOC_390_TLS_LDCALL:
2279 case BFD_RELOC_390_TLS_GD32:
2280 case BFD_RELOC_390_TLS_GD64:
2281 case BFD_RELOC_390_TLS_GOTIE12:
2282 case BFD_RELOC_390_TLS_GOTIE20:
2283 case BFD_RELOC_390_TLS_GOTIE32:
2284 case BFD_RELOC_390_TLS_GOTIE64:
2285 case BFD_RELOC_390_TLS_LDM32:
2286 case BFD_RELOC_390_TLS_LDM64:
2287 case BFD_RELOC_390_TLS_IE32:
2288 case BFD_RELOC_390_TLS_IE64:
2289 case BFD_RELOC_390_TLS_LE32:
2290 case BFD_RELOC_390_TLS_LE64:
2291 case BFD_RELOC_390_TLS_LDO32:
2292 case BFD_RELOC_390_TLS_LDO64:
2293 case BFD_RELOC_390_TLS_DTPMOD:
2294 case BFD_RELOC_390_TLS_DTPOFF:
2295 case BFD_RELOC_390_TLS_TPOFF:
2296 S_SET_THREAD_LOCAL (fixP->fx_addsy);
2297 /* Fully resolved at link time. */
2298 break;
2299 case BFD_RELOC_390_TLS_IEENT:
2300 /* Fully resolved at link time. */
2301 S_SET_THREAD_LOCAL (fixP->fx_addsy);
2302 value += 2;
2303 break;
2305 default:
2307 const char *reloc_name = bfd_get_reloc_code_name (fixP->fx_r_type);
2309 if (reloc_name != NULL)
2310 as_fatal (_("Gas failure, reloc type %s\n"), reloc_name);
2311 else
2312 as_fatal (_("Gas failure, reloc type #%i\n"), fixP->fx_r_type);
2316 fixP->fx_offset = value;
2320 /* Generate a reloc for a fixup. */
2322 arelent *
2323 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
2325 bfd_reloc_code_real_type code;
2326 arelent *reloc;
2328 code = fixp->fx_r_type;
2329 if (GOT_symbol && fixp->fx_addsy == GOT_symbol)
2331 if ( (s390_arch_size == 32 && code == BFD_RELOC_32_PCREL)
2332 || (s390_arch_size == 64 && code == BFD_RELOC_64_PCREL))
2333 code = BFD_RELOC_390_GOTPC;
2334 if (code == BFD_RELOC_390_PC32DBL)
2335 code = BFD_RELOC_390_GOTPCDBL;
2338 reloc = (arelent *) xmalloc (sizeof (arelent));
2339 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2340 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2341 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2342 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2343 if (reloc->howto == NULL)
2345 as_bad_where (fixp->fx_file, fixp->fx_line,
2346 _("cannot represent relocation type %s"),
2347 bfd_get_reloc_code_name (code));
2348 /* Set howto to a garbage value so that we can keep going. */
2349 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2350 gas_assert (reloc->howto != NULL);
2352 reloc->addend = fixp->fx_offset;
2354 return reloc;
2357 void
2358 s390_cfi_frame_initial_instructions (void)
2360 cfi_add_CFA_def_cfa (15, s390_arch_size == 64 ? 160 : 96);
2364 tc_s390_regname_to_dw2regnum (char *regname)
2366 int regnum = -1;
2368 if (regname[0] != 'c' && regname[0] != 'a')
2370 regnum = reg_name_search (pre_defined_registers, REG_NAME_CNT, regname);
2371 if (regname[0] == 'f' && regnum != -1)
2372 regnum += 16;
2374 else if (strcmp (regname, "ap") == 0)
2375 regnum = 32;
2376 else if (strcmp (regname, "cc") == 0)
2377 regnum = 33;
2378 return regnum;
2381 void
2382 s390_elf_final_processing (void)
2384 if (s390_arch_size == 32 && (current_mode_mask & (1 << S390_OPCODE_ZARCH)))
2385 elf_elfheader (stdoutput)->e_flags |= EF_S390_HIGH_GPRS;