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