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