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