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