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