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