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