]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-s390.c
opcodes/
[thirdparty/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++)
933fbc29
MS
515 if (op->min_cpu <= current_cpu)
516 {
517 retval = hash_insert (s390_opcode_hash, op->name, (PTR) op);
518 if (retval != (const char *) NULL)
519 {
520 as_bad (_("Internal assembler error for instruction %s"),
521 op->name);
522 dup_insn = TRUE;
523 }
524 while (op < op_end - 1 && strcmp (op->name, op[1].name) == 0)
525 op++;
526 }
07855bec 527
a85d7ed0
NC
528 if (dup_insn)
529 abort ();
530
531 record_alignment (text_section, 2);
532 record_alignment (data_section, 2);
533 record_alignment (bss_section, 2);
534
535}
536
537/* Called after all assembly has been done. */
538void
539s390_md_end ()
540{
07855bec 541 if (s390_arch_size == 64)
ff0fb565 542 bfd_set_arch_mach (stdoutput, bfd_arch_s390, bfd_mach_s390_64);
07855bec 543 else
ff0fb565 544 bfd_set_arch_mach (stdoutput, bfd_arch_s390, bfd_mach_s390_31);
a85d7ed0
NC
545}
546
a85d7ed0
NC
547/* Insert an operand value into an instruction. */
548
549static void
550s390_insert_operand (insn, operand, val, file, line)
551 unsigned char *insn;
552 const struct s390_operand *operand;
553 offsetT val;
554 char *file;
555 unsigned int line;
556{
557 addressT uval;
558 int offset;
559
07855bec
NC
560 if (operand->flags & (S390_OPERAND_SIGNED|S390_OPERAND_PCREL))
561 {
562 offsetT min, max;
563
564 max = ((offsetT) 1 << (operand->bits - 1)) - 1;
565 min = - ((offsetT) 1 << (operand->bits - 1));
566 /* Halve PCREL operands. */
567 if (operand->flags & S390_OPERAND_PCREL)
568 val >>= 1;
569 /* Check for underflow / overflow. */
570 if (val < min || val > max)
571 {
572 const char *err =
573 "operand out of range (%s not between %ld and %ld)";
574 char buf[100];
575
576 if (operand->flags & S390_OPERAND_PCREL)
577 {
578 val <<= 1;
579 min <<= 1;
580 max <<= 1;
581 }
582 sprint_value (buf, val);
583 if (file == (char *) NULL)
584 as_bad (err, buf, (int) min, (int) max);
585 else
586 as_bad_where (file, line, err, buf, (int) min, (int) max);
587 return;
588 }
589 /* val is ok, now restrict it to operand->bits bits. */
590 uval = (addressT) val & ((((addressT) 1 << (operand->bits-1)) << 1) - 1);
933fbc29
MS
591 /* val is restrict, now check for special case. */
592 if (operand->bits == 20 && operand->shift == 20)
593 uval = (uval >> 12) | ((uval & 0xfff) << 8);
a85d7ed0 594 }
07855bec
NC
595 else
596 {
597 addressT min, max;
198ce79b 598
98d3f06f 599 max = (((addressT) 1 << (operand->bits - 1)) << 1) - 1;
07855bec
NC
600 min = (offsetT) 0;
601 uval = (addressT) val;
602 /* Length x in an instructions has real length x+1. */
603 if (operand->flags & S390_OPERAND_LENGTH)
604 uval--;
605 /* Check for underflow / overflow. */
606 if (uval < min || uval > max)
607 {
07855bec
NC
608 if (operand->flags & S390_OPERAND_LENGTH)
609 {
610 uval++;
611 min++;
612 max++;
613 }
e5976317
NC
614
615 as_bad_value_out_of_range (_("operand"), uval, (offsetT) min, (offsetT) max, file, line);
616
07855bec
NC
617 return;
618 }
a85d7ed0 619 }
a85d7ed0
NC
620
621 /* Insert fragments of the operand byte for byte. */
622 offset = operand->shift + operand->bits;
623 uval <<= (-offset) & 7;
98d3f06f 624 insn += (offset - 1) / 8;
07855bec
NC
625 while (uval != 0)
626 {
627 *insn-- |= uval;
628 uval >>= 8;
629 }
a85d7ed0
NC
630}
631
1971b29d
MS
632struct map_tls
633 {
634 char *string;
635 int length;
636 bfd_reloc_code_real_type reloc;
637 };
638
639static bfd_reloc_code_real_type s390_tls_suffix
640 PARAMS ((char **, expressionS *));
641
642/* Parse tls marker and return the desired relocation. */
643static bfd_reloc_code_real_type
644s390_tls_suffix (str_p, exp_p)
645 char **str_p;
646 expressionS *exp_p;
647{
648 static struct map_tls mapping[] =
649 {
650 { "tls_load", 8, BFD_RELOC_390_TLS_LOAD },
651 { "tls_gdcall", 10, BFD_RELOC_390_TLS_GDCALL },
652 { "tls_ldcall", 10, BFD_RELOC_390_TLS_LDCALL },
653 { NULL, 0, BFD_RELOC_UNUSED }
654 };
655 struct map_tls *ptr;
656 char *orig_line;
657 char *str;
658 char *ident;
659 int len;
660
661 str = *str_p;
662 if (*str++ != ':')
663 return BFD_RELOC_UNUSED;
664
665 ident = str;
666 while (ISIDNUM (*str))
667 str++;
668 len = str - ident;
669 if (*str++ != ':')
670 return BFD_RELOC_UNUSED;
671
672 orig_line = input_line_pointer;
673 input_line_pointer = str;
674 expression (exp_p);
675 str = input_line_pointer;
676 if (&input_line_pointer != str_p)
677 input_line_pointer = orig_line;
678
679 if (exp_p->X_op != O_symbol)
680 return BFD_RELOC_UNUSED;
681
682 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
683 if (len == ptr->length
684 && strncasecmp (ident, ptr->string, ptr->length) == 0)
685 {
686 /* Found a matching tls suffix. */
687 *str_p = str;
688 return ptr->reloc;
689 }
690 return BFD_RELOC_UNUSED;
691}
692
a85d7ed0 693/* Structure used to hold suffixes. */
07855bec
NC
694typedef enum
695 {
696 ELF_SUFFIX_NONE = 0,
697 ELF_SUFFIX_GOT,
698 ELF_SUFFIX_PLT,
2a19f73f
MS
699 ELF_SUFFIX_GOTENT,
700 ELF_SUFFIX_GOTOFF,
701 ELF_SUFFIX_GOTPLT,
1971b29d
MS
702 ELF_SUFFIX_PLTOFF,
703 ELF_SUFFIX_TLS_GD,
704 ELF_SUFFIX_TLS_GOTIE,
705 ELF_SUFFIX_TLS_IE,
706 ELF_SUFFIX_TLS_LDM,
707 ELF_SUFFIX_TLS_LDO,
708 ELF_SUFFIX_TLS_LE
07855bec
NC
709 }
710elf_suffix_type;
711
712struct map_bfd
713 {
714 char *string;
715 int length;
716 elf_suffix_type suffix;
717 };
a85d7ed0 718
9d654c1c
AJ
719static elf_suffix_type s390_elf_suffix PARAMS ((char **, expressionS *));
720static int s390_exp_compare PARAMS ((expressionS *exp1, expressionS *exp2));
721static elf_suffix_type s390_lit_suffix
722 PARAMS ((char **, expressionS *, elf_suffix_type));
723
724
a85d7ed0
NC
725/* Parse @got/@plt/@gotoff. and return the desired relocation. */
726static elf_suffix_type
727s390_elf_suffix (str_p, exp_p)
728 char **str_p;
729 expressionS *exp_p;
730{
07855bec
NC
731 static struct map_bfd mapping[] =
732 {
a85d7ed0
NC
733 { "got", 3, ELF_SUFFIX_GOT },
734 { "got12", 5, ELF_SUFFIX_GOT },
735 { "plt", 3, ELF_SUFFIX_PLT },
736 { "gotent", 6, ELF_SUFFIX_GOTENT },
2a19f73f
MS
737 { "gotoff", 6, ELF_SUFFIX_GOTOFF },
738 { "gotplt", 6, ELF_SUFFIX_GOTPLT },
739 { "pltoff", 6, ELF_SUFFIX_PLTOFF },
1971b29d
MS
740 { "tlsgd", 5, ELF_SUFFIX_TLS_GD },
741 { "gotntpoff", 9, ELF_SUFFIX_TLS_GOTIE },
742 { "indntpoff", 9, ELF_SUFFIX_TLS_IE },
743 { "tlsldm", 6, ELF_SUFFIX_TLS_LDM },
744 { "dtpoff", 6, ELF_SUFFIX_TLS_LDO },
745 { "ntpoff", 6, ELF_SUFFIX_TLS_LE },
a85d7ed0
NC
746 { NULL, 0, ELF_SUFFIX_NONE }
747 };
748
749 struct map_bfd *ptr;
750 char *str = *str_p;
751 char *ident;
752 int len;
753
754 if (*str++ != '@')
755 return ELF_SUFFIX_NONE;
756
757 ident = str;
3882b010 758 while (ISALNUM (*str))
a85d7ed0
NC
759 str++;
760 len = str - ident;
761
762 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
16a419ba
NC
763 if (len == ptr->length
764 && strncasecmp (ident, ptr->string, ptr->length) == 0)
07855bec
NC
765 {
766 if (exp_p->X_add_number != 0)
767 as_warn (_("identifier+constant@%s means identifier@%s+constant"),
768 ptr->string, ptr->string);
769 /* Now check for identifier@suffix+constant. */
770 if (*str == '-' || *str == '+')
771 {
772 char *orig_line = input_line_pointer;
773 expressionS new_exp;
774
775 input_line_pointer = str;
776 expression (&new_exp);
777
778 switch (new_exp.X_op)
779 {
780 case O_constant: /* X_add_number (a constant expression). */
781 exp_p->X_add_number += new_exp.X_add_number;
782 str = input_line_pointer;
783 break;
784 case O_symbol: /* X_add_symbol + X_add_number. */
785 /* this case is used for e.g. xyz@PLT+.Label. */
786 exp_p->X_add_number += new_exp.X_add_number;
787 exp_p->X_op_symbol = new_exp.X_add_symbol;
788 exp_p->X_op = O_add;
789 str = input_line_pointer;
790 break;
791 case O_uminus: /* (- X_add_symbol) + X_add_number. */
792 /* this case is used for e.g. xyz@PLT-.Label. */
793 exp_p->X_add_number += new_exp.X_add_number;
794 exp_p->X_op_symbol = new_exp.X_add_symbol;
795 exp_p->X_op = O_subtract;
796 str = input_line_pointer;
797 break;
798 default:
799 break;
800 }
801
802 /* If s390_elf_suffix has not been called with
803 &input_line_pointer as first parameter, we have
804 clobbered the input_line_pointer. We have to
805 undo that. */
806 if (&input_line_pointer != str_p)
807 input_line_pointer = orig_line;
a85d7ed0 808 }
07855bec
NC
809 *str_p = str;
810 return ptr->suffix;
a85d7ed0 811 }
a85d7ed0
NC
812
813 return BFD_RELOC_UNUSED;
814}
815
816/* Structure used to hold a literal pool entry. */
07855bec
NC
817struct s390_lpe
818 {
819 struct s390_lpe *next;
820 expressionS ex;
821 FLONUM_TYPE floatnum; /* used if X_op == O_big && X_add_number <= 0 */
822 LITTLENUM_TYPE bignum[4]; /* used if X_op == O_big && X_add_number > 0 */
823 int nbytes;
824 bfd_reloc_code_real_type reloc;
825 symbolS *sym;
826 };
a85d7ed0
NC
827
828static struct s390_lpe *lpe_free_list = NULL;
829static struct s390_lpe *lpe_list = NULL;
830static struct s390_lpe *lpe_list_tail = NULL;
831static symbolS *lp_sym = NULL;
832static int lp_count = 0;
833static int lpe_count = 0;
834
835static int
98d3f06f 836s390_exp_compare (exp1, exp2)
a85d7ed0
NC
837 expressionS *exp1;
838 expressionS *exp2;
839{
840 if (exp1->X_op != exp2->X_op)
841 return 0;
842
07855bec
NC
843 switch (exp1->X_op)
844 {
845 case O_constant: /* X_add_number must be equal. */
846 case O_register:
847 return exp1->X_add_number == exp2->X_add_number;
848
849 case O_big:
198ce79b 850 as_bad (_("Can't handle O_big in s390_exp_compare"));
07855bec
NC
851
852 case O_symbol: /* X_add_symbol & X_add_number must be equal. */
853 case O_symbol_rva:
854 case O_uminus:
855 case O_bit_not:
856 case O_logical_not:
16a419ba
NC
857 return (exp1->X_add_symbol == exp2->X_add_symbol)
858 && (exp1->X_add_number == exp2->X_add_number);
07855bec
NC
859
860 case O_multiply: /* X_add_symbol,X_op_symbol&X_add_number must be equal. */
861 case O_divide:
862 case O_modulus:
863 case O_left_shift:
864 case O_right_shift:
865 case O_bit_inclusive_or:
866 case O_bit_or_not:
867 case O_bit_exclusive_or:
868 case O_bit_and:
869 case O_add:
870 case O_subtract:
871 case O_eq:
872 case O_ne:
873 case O_lt:
874 case O_le:
875 case O_ge:
876 case O_gt:
877 case O_logical_and:
878 case O_logical_or:
16a419ba
NC
879 return (exp1->X_add_symbol == exp2->X_add_symbol)
880 && (exp1->X_op_symbol == exp2->X_op_symbol)
881 && (exp1->X_add_number == exp2->X_add_number);
07855bec 882 default:
98d3f06f
KH
883 return 0;
884 }
a85d7ed0
NC
885}
886
887/* Test for @lit and if its present make an entry in the literal pool and
888 modify the current expression to be an offset into the literal pool. */
889static elf_suffix_type
890s390_lit_suffix (str_p, exp_p, suffix)
891 char **str_p;
892 expressionS *exp_p;
893 elf_suffix_type suffix;
894{
895 bfd_reloc_code_real_type reloc;
896 char tmp_name[64];
897 char *str = *str_p;
898 char *ident;
899 struct s390_lpe *lpe;
900 int nbytes, len;
901
902 if (*str++ != ':')
903 return suffix; /* No modification. */
198ce79b 904
a85d7ed0
NC
905 /* We look for a suffix of the form "@lit1", "@lit2", "@lit4" or "@lit8". */
906 ident = str;
3882b010 907 while (ISALNUM (*str))
a85d7ed0
NC
908 str++;
909 len = str - ident;
16a419ba
NC
910 if (len != 4 || strncasecmp (ident, "lit", 3) != 0
911 || (ident[3]!='1' && ident[3]!='2' && ident[3]!='4' && ident[3]!='8'))
a85d7ed0
NC
912 return suffix; /* no modification */
913 nbytes = ident[3] - '0';
914
915 reloc = BFD_RELOC_UNUSED;
07855bec
NC
916 if (suffix == ELF_SUFFIX_GOT)
917 {
918 if (nbytes == 2)
919 reloc = BFD_RELOC_390_GOT16;
920 else if (nbytes == 4)
921 reloc = BFD_RELOC_32_GOT_PCREL;
922 else if (nbytes == 8)
923 reloc = BFD_RELOC_390_GOT64;
924 }
925 else if (suffix == ELF_SUFFIX_PLT)
926 {
927 if (nbytes == 4)
928 reloc = BFD_RELOC_390_PLT32;
929 else if (nbytes == 8)
930 reloc = BFD_RELOC_390_PLT64;
931 }
a85d7ed0 932
07855bec 933 if (suffix != ELF_SUFFIX_NONE && reloc == BFD_RELOC_UNUSED)
a85d7ed0 934 as_bad (_("Invalid suffix for literal pool entry"));
a85d7ed0
NC
935
936 /* Search the pool if the new entry is a duplicate. */
07855bec
NC
937 if (exp_p->X_op == O_big)
938 {
939 /* Special processing for big numbers. */
940 for (lpe = lpe_list; lpe != NULL; lpe = lpe->next)
941 {
942 if (lpe->ex.X_op == O_big)
943 {
944 if (exp_p->X_add_number <= 0 && lpe->ex.X_add_number <= 0)
945 {
946 if (memcmp (&generic_floating_point_number, &lpe->floatnum,
947 sizeof (FLONUM_TYPE)) == 0)
948 break;
949 }
950 else if (exp_p->X_add_number == lpe->ex.X_add_number)
951 {
952 if (memcmp (generic_bignum, lpe->bignum,
953 sizeof (LITTLENUM_TYPE)*exp_p->X_add_number) == 0)
954 break;
955 }
956 }
957 }
a85d7ed0 958 }
07855bec
NC
959 else
960 {
961 /* Processing for 'normal' data types. */
962 for (lpe = lpe_list; lpe != NULL; lpe = lpe->next)
16a419ba 963 if (lpe->nbytes == nbytes && lpe->reloc == reloc
98d3f06f 964 && s390_exp_compare (exp_p, &lpe->ex) != 0)
07855bec 965 break;
a85d7ed0 966 }
07855bec
NC
967
968 if (lpe == NULL)
969 {
970 /* A new literal. */
971 if (lpe_free_list != NULL)
972 {
973 lpe = lpe_free_list;
974 lpe_free_list = lpe_free_list->next;
975 }
a85d7ed0 976 else
07855bec 977 {
98d3f06f 978 lpe = (struct s390_lpe *) xmalloc (sizeof (struct s390_lpe));
07855bec
NC
979 }
980
981 lpe->ex = *exp_p;
982
983 if (exp_p->X_op == O_big)
984 {
985 if (exp_p->X_add_number <= 0)
986 lpe->floatnum = generic_floating_point_number;
987 else if (exp_p->X_add_number <= 4)
988 memcpy (lpe->bignum, generic_bignum,
98d3f06f 989 exp_p->X_add_number * sizeof (LITTLENUM_TYPE));
07855bec
NC
990 else
991 as_bad (_("Big number is too big"));
992 }
993
994 lpe->nbytes = nbytes;
995 lpe->reloc = reloc;
996 /* Literal pool name defined ? */
997 if (lp_sym == NULL)
998 {
07726851 999 sprintf (tmp_name, ".L\001%i", lp_count);
98d3f06f 1000 lp_sym = symbol_make (tmp_name);
07855bec 1001 }
a85d7ed0 1002
07855bec 1003 /* Make name for literal pool entry. */
07726851 1004 sprintf (tmp_name, ".L\001%i\002%i", lp_count, lpe_count);
07855bec 1005 lpe_count++;
98d3f06f 1006 lpe->sym = symbol_make (tmp_name);
07855bec
NC
1007
1008 /* Add to literal pool list. */
1009 lpe->next = NULL;
1010 if (lpe_list_tail != NULL)
1011 {
1012 lpe_list_tail->next = lpe;
1013 lpe_list_tail = lpe;
1014 }
1015 else
1016 lpe_list = lpe_list_tail = lpe;
1017 }
198ce79b 1018
a85d7ed0
NC
1019 /* Now change exp_p to the offset into the literal pool.
1020 Thats the expression: .L^Ax^By-.L^Ax */
1021 exp_p->X_add_symbol = lpe->sym;
1022 exp_p->X_op_symbol = lp_sym;
1023 exp_p->X_op = O_subtract;
1024 exp_p->X_add_number = 0;
1025
1026 *str_p = str;
1027
1028 /* We change the suffix type to ELF_SUFFIX_NONE, because
1029 the difference of two local labels is just a number. */
1030 return ELF_SUFFIX_NONE;
1031}
1032
1033/* Like normal .long/.short/.word, except support @got, etc.
1034 clobbers input_line_pointer, checks end-of-line. */
1035static void
1036s390_elf_cons (nbytes)
1037 register int nbytes; /* 1=.byte, 2=.word, 4=.long */
1038{
1039 expressionS exp;
1040 elf_suffix_type suffix;
1041
07855bec
NC
1042 if (is_it_end_of_statement ())
1043 {
1044 demand_empty_rest_of_line ();
1045 return;
1046 }
a85d7ed0 1047
07855bec
NC
1048 do
1049 {
1050 expression (&exp);
1051
1052 if (exp.X_op == O_symbol
1053 && *input_line_pointer == '@'
1054 && (suffix = s390_elf_suffix (&input_line_pointer, &exp)) != ELF_SUFFIX_NONE)
1055 {
1056 bfd_reloc_code_real_type reloc;
1057 reloc_howto_type *reloc_howto;
1058 int size;
1059 char *where;
1060
1971b29d
MS
1061 if (nbytes == 2)
1062 {
1063 static bfd_reloc_code_real_type tab2[] =
1064 {
5cfbfc2b
MS
1065 BFD_RELOC_UNUSED, /* ELF_SUFFIX_NONE */
1066 BFD_RELOC_390_GOT16, /* ELF_SUFFIX_GOT */
1067 BFD_RELOC_UNUSED, /* ELF_SUFFIX_PLT */
1068 BFD_RELOC_UNUSED, /* ELF_SUFFIX_GOTENT */
1069 BFD_RELOC_16_GOTOFF, /* ELF_SUFFIX_GOTOFF */
1070 BFD_RELOC_UNUSED, /* ELF_SUFFIX_GOTPLT */
1071 BFD_RELOC_390_PLTOFF16, /* ELF_SUFFIX_PLTOFF */
1072 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_GD */
1073 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_GOTIE */
1074 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_IE */
1075 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_LDM */
1076 BFD_RELOC_UNUSED, /* ELF_SUFFIX_TLS_LDO */
1077 BFD_RELOC_UNUSED /* ELF_SUFFIX_TLS_LE */
1971b29d
MS
1078 };
1079 reloc = tab2[suffix];
1080 }
1081 else if (nbytes == 4)
1082 {
1083 static bfd_reloc_code_real_type tab4[] =
1084 {
5cfbfc2b
MS
1085 BFD_RELOC_UNUSED, /* ELF_SUFFIX_NONE */
1086 BFD_RELOC_32_GOT_PCREL, /* ELF_SUFFIX_GOT */
1087 BFD_RELOC_390_PLT32, /* ELF_SUFFIX_PLT */
1088 BFD_RELOC_UNUSED, /* ELF_SUFFIX_GOTENT */
1089 BFD_RELOC_32_GOTOFF, /* ELF_SUFFIX_GOTOFF */
1090 BFD_RELOC_390_GOTPLT32, /* ELF_SUFFIX_GOTPLT */
1091 BFD_RELOC_390_PLTOFF32, /* ELF_SUFFIX_PLTOFF */
1092 BFD_RELOC_390_TLS_GD32, /* ELF_SUFFIX_TLS_GD */
1093 BFD_RELOC_390_TLS_GOTIE32, /* ELF_SUFFIX_TLS_GOTIE */
1094 BFD_RELOC_390_TLS_IE32, /* ELF_SUFFIX_TLS_IE */
1095 BFD_RELOC_390_TLS_LDM32, /* ELF_SUFFIX_TLS_LDM */
1096 BFD_RELOC_390_TLS_LDO32, /* ELF_SUFFIX_TLS_LDO */
1097 BFD_RELOC_390_TLS_LE32 /* ELF_SUFFIX_TLS_LE */
1971b29d
MS
1098 };
1099 reloc = tab4[suffix];
1100 }
1101 else if (nbytes == 8)
1102 {
1103 static bfd_reloc_code_real_type tab8[] =
1104 {
5cfbfc2b
MS
1105 BFD_RELOC_UNUSED, /* ELF_SUFFIX_NONE */
1106 BFD_RELOC_390_GOT64, /* ELF_SUFFIX_GOT */
1107 BFD_RELOC_390_PLT64, /* ELF_SUFFIX_PLT */
1108 BFD_RELOC_UNUSED, /* ELF_SUFFIX_GOTENT */
1109 BFD_RELOC_390_GOTOFF64, /* ELF_SUFFIX_GOTOFF */
1110 BFD_RELOC_390_GOTPLT64, /* ELF_SUFFIX_GOTPLT */
1111 BFD_RELOC_390_PLTOFF64, /* ELF_SUFFIX_PLTOFF */
1112 BFD_RELOC_390_TLS_GD64, /* ELF_SUFFIX_TLS_GD */
1113 BFD_RELOC_390_TLS_GOTIE64, /* ELF_SUFFIX_TLS_GOTIE */
1114 BFD_RELOC_390_TLS_IE64, /* ELF_SUFFIX_TLS_IE */
1115 BFD_RELOC_390_TLS_LDM64, /* ELF_SUFFIX_TLS_LDM */
1116 BFD_RELOC_390_TLS_LDO64, /* ELF_SUFFIX_TLS_LDO */
1117 BFD_RELOC_390_TLS_LE64 /* ELF_SUFFIX_TLS_LE */
1971b29d
MS
1118 };
1119 reloc = tab8[suffix];
1120 }
07855bec
NC
1121 else
1122 reloc = BFD_RELOC_UNUSED;
1123
1971b29d
MS
1124 if (reloc != BFD_RELOC_UNUSED
1125 && (reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc)))
07855bec 1126 {
07855bec
NC
1127 size = bfd_get_reloc_size (reloc_howto);
1128 if (size > nbytes)
1129 as_bad (_("%s relocations do not fit in %d bytes"),
1130 reloc_howto->name, nbytes);
07726851 1131 where = frag_more (nbytes);
07855bec 1132 md_number_to_chars (where, 0, size);
198ce79b 1133 /* To make fixup_segment do the pc relative conversion the
b34976b6 1134 pcrel parameter on the fix_new_exp call needs to be FALSE. */
198ce79b 1135 fix_new_exp (frag_now, where - frag_now->fr_literal,
b34976b6 1136 size, &exp, FALSE, reloc);
07855bec
NC
1137 }
1138 else
1139 as_bad (_("relocation not applicable"));
1140 }
a85d7ed0 1141 else
07855bec
NC
1142 emit_expr (&exp, (unsigned int) nbytes);
1143 }
1144 while (*input_line_pointer++ == ',');
a85d7ed0 1145
98d3f06f 1146 input_line_pointer--; /* Put terminator back into stream. */
a85d7ed0
NC
1147 demand_empty_rest_of_line ();
1148}
1149
1150/* We need to keep a list of fixups. We can't simply generate them as
1151 we go, because that would require us to first create the frag, and
1152 that would screw up references to ``.''. */
1153
1154struct s390_fixup
07855bec
NC
1155 {
1156 expressionS exp;
1157 int opindex;
1158 bfd_reloc_code_real_type reloc;
1159 };
a85d7ed0
NC
1160
1161#define MAX_INSN_FIXUPS (4)
1162
1163/* This routine is called for each instruction to be assembled. */
1164
9d654c1c 1165static char *
a85d7ed0
NC
1166md_gather_operands (str, insn, opcode)
1167 char *str;
1168 unsigned char *insn;
1169 const struct s390_opcode *opcode;
1170{
1171 struct s390_fixup fixups[MAX_INSN_FIXUPS];
1172 const struct s390_operand *operand;
1173 const unsigned char *opindex_ptr;
1971b29d 1174 expressionS ex;
a85d7ed0
NC
1175 elf_suffix_type suffix;
1176 bfd_reloc_code_real_type reloc;
1177 int skip_optional;
1178 int parentheses;
1179 char *f;
1180 int fc, i;
1181
98d3f06f
KH
1182 while (ISSPACE (*str))
1183 str++;
a85d7ed0
NC
1184
1185 parentheses = 0;
1186 skip_optional = 0;
1187
1188 /* Gather the operands. */
1189 fc = 0;
07855bec
NC
1190 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1191 {
07855bec
NC
1192 char *hold;
1193
1194 operand = s390_operands + *opindex_ptr;
198ce79b 1195
07855bec
NC
1196 if (skip_optional && (operand->flags & S390_OPERAND_INDEX))
1197 {
1198 /* We do an early skip. For D(X,B) constructions the index
198ce79b 1199 register is skipped (X is optional). For D(L,B) the base
07855bec
NC
1200 register will be the skipped operand, because L is NOT
1201 optional. */
1202 skip_optional = 0;
1203 continue;
1204 }
198ce79b 1205
07855bec
NC
1206 /* Gather the operand. */
1207 hold = input_line_pointer;
1208 input_line_pointer = str;
1209
98d3f06f
KH
1210 /* Parse the operand. */
1211 if (! register_name (&ex))
07855bec 1212 expression (&ex);
198ce79b 1213
07855bec
NC
1214 str = input_line_pointer;
1215 input_line_pointer = hold;
198ce79b 1216
07855bec
NC
1217 /* Write the operand to the insn. */
1218 if (ex.X_op == O_illegal)
1219 as_bad (_("illegal operand"));
1220 else if (ex.X_op == O_absent)
1221 as_bad (_("missing operand"));
1222 else if (ex.X_op == O_register || ex.X_op == O_constant)
1223 {
1224 s390_lit_suffix (&str, &ex, ELF_SUFFIX_NONE);
1225
1226 if (ex.X_op != O_register && ex.X_op != O_constant)
1227 {
1228 /* We need to generate a fixup for the
1229 expression returned by s390_lit_suffix. */
1230 if (fc >= MAX_INSN_FIXUPS)
1231 as_fatal (_("too many fixups"));
1232 fixups[fc].exp = ex;
1233 fixups[fc].opindex = *opindex_ptr;
1234 fixups[fc].reloc = BFD_RELOC_UNUSED;
1235 ++fc;
1236 }
1237 else
1238 {
16a419ba
NC
1239 if ((operand->flags & S390_OPERAND_INDEX)
1240 && ex.X_add_number == 0
b34976b6 1241 && warn_areg_zero)
07726851 1242 as_warn ("index register specified but zero");
16a419ba
NC
1243 if ((operand->flags & S390_OPERAND_BASE)
1244 && ex.X_add_number == 0
b34976b6 1245 && warn_areg_zero)
07726851 1246 as_warn ("base register specified but zero");
07855bec
NC
1247 s390_insert_operand (insn, operand, ex.X_add_number, NULL, 0);
1248 }
1249 }
1250 else
1251 {
1252 suffix = s390_elf_suffix (&str, &ex);
1253 suffix = s390_lit_suffix (&str, &ex, suffix);
1254 reloc = BFD_RELOC_UNUSED;
1255
1256 if (suffix == ELF_SUFFIX_GOT)
1257 {
933fbc29
MS
1258 if ((operand->flags & S390_OPERAND_DISP) &&
1259 (operand->bits == 12))
07855bec 1260 reloc = BFD_RELOC_390_GOT12;
933fbc29
MS
1261 else if ((operand->flags & S390_OPERAND_DISP) &&
1262 (operand->bits == 20))
1263 reloc = BFD_RELOC_390_GOT20;
16a419ba
NC
1264 else if ((operand->flags & S390_OPERAND_SIGNED)
1265 && (operand->bits == 16))
07855bec 1266 reloc = BFD_RELOC_390_GOT16;
16a419ba
NC
1267 else if ((operand->flags & S390_OPERAND_PCREL)
1268 && (operand->bits == 32))
07855bec
NC
1269 reloc = BFD_RELOC_390_GOTENT;
1270 }
1271 else if (suffix == ELF_SUFFIX_PLT)
1272 {
16a419ba
NC
1273 if ((operand->flags & S390_OPERAND_PCREL)
1274 && (operand->bits == 16))
07855bec 1275 reloc = BFD_RELOC_390_PLT16DBL;
16a419ba
NC
1276 else if ((operand->flags & S390_OPERAND_PCREL)
1277 && (operand->bits == 32))
07855bec
NC
1278 reloc = BFD_RELOC_390_PLT32DBL;
1279 }
1280 else if (suffix == ELF_SUFFIX_GOTENT)
1281 {
16a419ba
NC
1282 if ((operand->flags & S390_OPERAND_PCREL)
1283 && (operand->bits == 32))
07855bec
NC
1284 reloc = BFD_RELOC_390_GOTENT;
1285 }
2a19f73f
MS
1286 else if (suffix == ELF_SUFFIX_GOTOFF)
1287 {
1288 if ((operand->flags & S390_OPERAND_SIGNED)
1289 && (operand->bits == 16))
1290 reloc = BFD_RELOC_16_GOTOFF;
1291 }
1292 else if (suffix == ELF_SUFFIX_PLTOFF)
1293 {
1294 if ((operand->flags & S390_OPERAND_SIGNED)
1295 && (operand->bits == 16))
1296 reloc = BFD_RELOC_390_PLTOFF16;
1297 }
1298 else if (suffix == ELF_SUFFIX_GOTPLT)
1299 {
1300 if ((operand->flags & S390_OPERAND_DISP)
1301 && (operand->bits == 12))
1302 reloc = BFD_RELOC_390_GOTPLT12;
1303 else if ((operand->flags & S390_OPERAND_SIGNED)
1304 && (operand->bits == 16))
1305 reloc = BFD_RELOC_390_GOTPLT16;
1306 else if ((operand->flags & S390_OPERAND_PCREL)
1307 && (operand->bits == 32))
1308 reloc = BFD_RELOC_390_GOTPLTENT;
1309 }
1971b29d
MS
1310 else if (suffix == ELF_SUFFIX_TLS_GOTIE)
1311 {
1312 if ((operand->flags & S390_OPERAND_DISP)
1313 && (operand->bits == 12))
1314 reloc = BFD_RELOC_390_TLS_GOTIE12;
933fbc29
MS
1315 else if ((operand->flags & S390_OPERAND_DISP)
1316 && (operand->bits == 20))
1317 reloc = BFD_RELOC_390_TLS_GOTIE20;
1971b29d
MS
1318 }
1319 else if (suffix == ELF_SUFFIX_TLS_IE)
1320 {
1321 if ((operand->flags & S390_OPERAND_PCREL)
1322 && (operand->bits == 32))
1323 reloc = BFD_RELOC_390_TLS_IEENT;
1324 }
07855bec
NC
1325
1326 if (suffix != ELF_SUFFIX_NONE && reloc == BFD_RELOC_UNUSED)
1327 as_bad (_("invalid operand suffix"));
1328 /* We need to generate a fixup of type 'reloc' for this
1329 expression. */
a85d7ed0
NC
1330 if (fc >= MAX_INSN_FIXUPS)
1331 as_fatal (_("too many fixups"));
1332 fixups[fc].exp = ex;
1333 fixups[fc].opindex = *opindex_ptr;
07855bec 1334 fixups[fc].reloc = reloc;
a85d7ed0 1335 ++fc;
07855bec 1336 }
198ce79b 1337
07855bec
NC
1338 /* Check the next character. The call to expression has advanced
1339 str past any whitespace. */
1340 if (operand->flags & S390_OPERAND_DISP)
1341 {
1342 /* After a displacement a block in parentheses can start. */
1343 if (*str != '(')
1344 {
67c1ffbe 1345 /* Check if parenthesized block can be skipped. If the next
07855bec
NC
1346 operand is neiter an optional operand nor a base register
1347 then we have a syntax error. */
1348 operand = s390_operands + *(++opindex_ptr);
1349 if (!(operand->flags & (S390_OPERAND_INDEX|S390_OPERAND_BASE)))
1350 as_bad (_("syntax error; missing '(' after displacement"));
1351
1352 /* Ok, skip all operands until S390_OPERAND_BASE. */
1353 while (!(operand->flags & S390_OPERAND_BASE))
1354 operand = s390_operands + *(++opindex_ptr);
198ce79b 1355
67c1ffbe 1356 /* If there is a next operand it must be separated by a comma. */
07855bec
NC
1357 if (opindex_ptr[1] != '\0')
1358 {
6c639ef9
MS
1359 if (*str != ',')
1360 {
1361 while (opindex_ptr[1] != '\0')
1362 {
1363 operand = s390_operands + *(++opindex_ptr);
1364 if (operand->flags & S390_OPERAND_OPTIONAL)
1365 continue;
1366 as_bad (_("syntax error; expected ,"));
1367 break;
1368 }
1369 }
1370 else
1371 str++;
07855bec
NC
1372 }
1373 }
1374 else
1375 {
1376 /* We found an opening parentheses. */
1377 str++;
1378 for (f = str; *f != '\0'; f++)
1379 if (*f == ',' || *f == ')')
1380 break;
1381 /* If there is no comma until the closing parentheses OR
1382 there is a comma right after the opening parentheses,
1383 we have to skip optional operands. */
1384 if (*f == ',' && f == str)
1385 {
1386 /* comma directly after '(' ? */
1387 skip_optional = 1;
1388 str++;
1389 }
1390 else
1391 skip_optional = (*f != ',');
1392 }
1393 }
1394 else if (operand->flags & S390_OPERAND_BASE)
1395 {
1396 /* After the base register the parenthesed block ends. */
1397 if (*str++ != ')')
1398 as_bad (_("syntax error; missing ')' after base register"));
1399 skip_optional = 0;
67c1ffbe 1400 /* If there is a next operand it must be separated by a comma. */
07855bec
NC
1401 if (opindex_ptr[1] != '\0')
1402 {
6c639ef9
MS
1403 if (*str != ',')
1404 {
1405 while (opindex_ptr[1] != '\0')
1406 {
1407 operand = s390_operands + *(++opindex_ptr);
1408 if (operand->flags & S390_OPERAND_OPTIONAL)
1409 continue;
1410 as_bad (_("syntax error; expected ,"));
1411 break;
1412 }
1413 }
1414 else
1415 str++;
07855bec
NC
1416 }
1417 }
1418 else
1419 {
1420 /* We can find an 'early' closing parentheses in e.g. D(L) instead
1421 of D(L,B). In this case the base register has to be skipped. */
1422 if (*str == ')')
1423 {
1424 operand = s390_operands + *(++opindex_ptr);
1425
1426 if (!(operand->flags & S390_OPERAND_BASE))
1427 as_bad (_("syntax error; ')' not allowed here"));
1428 str++;
1429 }
67c1ffbe 1430 /* If there is a next operand it must be separated by a comma. */
07855bec
NC
1431 if (opindex_ptr[1] != '\0')
1432 {
6c639ef9
MS
1433 if (*str != ',')
1434 {
1435 while (opindex_ptr[1] != '\0')
1436 {
1437 operand = s390_operands + *(++opindex_ptr);
1438 if (operand->flags & S390_OPERAND_OPTIONAL)
1439 continue;
1440 as_bad (_("syntax error; expected ,"));
1441 break;
1442 }
1443 }
1444 else
1445 str++;
07855bec 1446 }
a85d7ed0 1447 }
a85d7ed0 1448 }
a85d7ed0 1449
3882b010 1450 while (ISSPACE (*str))
a85d7ed0
NC
1451 ++str;
1452
1971b29d
MS
1453 /* Check for tls instruction marker. */
1454 reloc = s390_tls_suffix (&str, &ex);
1455 if (reloc != BFD_RELOC_UNUSED)
1456 {
1457 /* We need to generate a fixup of type 'reloc' for this
1458 instruction. */
1459 if (fc >= MAX_INSN_FIXUPS)
1460 as_fatal (_("too many fixups"));
1461 fixups[fc].exp = ex;
1462 fixups[fc].opindex = -1;
1463 fixups[fc].reloc = reloc;
1464 ++fc;
1465 }
1466
07855bec
NC
1467 if (*str != '\0')
1468 {
1469 char *linefeed;
a85d7ed0 1470
07726851 1471 if ((linefeed = strchr (str, '\n')) != NULL)
07855bec
NC
1472 *linefeed = '\0';
1473 as_bad (_("junk at end of line: `%s'"), str);
1474 if (linefeed != NULL)
1475 *linefeed = '\n';
1476 }
a85d7ed0
NC
1477
1478 /* Write out the instruction. */
1479 f = frag_more (opcode->oplen);
07855bec 1480 memcpy (f, insn, opcode->oplen);
8f5b2891 1481 dwarf2_emit_insn (opcode->oplen);
a85d7ed0
NC
1482
1483 /* Create any fixups. At this point we do not use a
1484 bfd_reloc_code_real_type, but instead just use the
1485 BFD_RELOC_UNUSED plus the operand index. This lets us easily
1486 handle fixups for any operand type, although that is admittedly
1487 not a very exciting feature. We pick a BFD reloc type in
55cf6793 1488 md_apply_fix. */
07855bec
NC
1489 for (i = 0; i < fc; i++)
1490 {
1971b29d
MS
1491
1492 if (fixups[i].opindex < 0)
1493 {
1494 /* Create tls instruction marker relocation. */
1495 fix_new_exp (frag_now, f - frag_now->fr_literal, opcode->oplen,
1496 &fixups[i].exp, 0, fixups[i].reloc);
1497 continue;
1498 }
1499
07855bec 1500 operand = s390_operands + fixups[i].opindex;
a85d7ed0 1501
07855bec
NC
1502 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1503 {
1504 reloc_howto_type *reloc_howto;
1505 fixS *fixP;
1506 int size;
198ce79b 1507
07855bec
NC
1508 reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1509 if (!reloc_howto)
1510 abort ();
198ce79b 1511
07855bec
NC
1512 size = bfd_get_reloc_size (reloc_howto);
1513
1514 if (size < 1 || size > 4)
1515 abort ();
198ce79b
AJ
1516
1517 fixP = fix_new_exp (frag_now,
1518 f - frag_now->fr_literal + (operand->shift/8),
07855bec
NC
1519 size, &fixups[i].exp, reloc_howto->pc_relative,
1520 fixups[i].reloc);
1521 /* Turn off overflow checking in fixup_segment. This is necessary
1522 because fixup_segment will signal an overflow for large 4 byte
1523 quantities for GOT12 relocations. */
16a419ba 1524 if ( fixups[i].reloc == BFD_RELOC_390_GOT12
933fbc29 1525 || fixups[i].reloc == BFD_RELOC_390_GOT20
16a419ba 1526 || fixups[i].reloc == BFD_RELOC_390_GOT16)
07855bec
NC
1527 fixP->fx_no_overflow = 1;
1528 }
1529 else
1530 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, &fixups[i].exp,
1531 (operand->flags & S390_OPERAND_PCREL) != 0,
1532 ((bfd_reloc_code_real_type)
1533 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
1534 }
a85d7ed0
NC
1535 return str;
1536}
1537
1538/* This routine is called for each instruction to be assembled. */
1539
1540void
1541md_assemble (str)
1542 char *str;
1543{
1544 const struct s390_opcode *opcode;
1545 unsigned char insn[6];
1546 char *s;
1547
1548 /* Get the opcode. */
3882b010 1549 for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
a85d7ed0
NC
1550 ;
1551 if (*s != '\0')
1552 *s++ = '\0';
1553
1554 /* Look up the opcode in the hash table. */
1555 opcode = (struct s390_opcode *) hash_find (s390_opcode_hash, str);
07855bec
NC
1556 if (opcode == (const struct s390_opcode *) NULL)
1557 {
1558 as_bad (_("Unrecognized opcode: `%s'"), str);
1559 return;
1560 }
37a58793
MS
1561 else if (!(opcode->modes & current_mode_mask))
1562 {
1563 as_bad ("Opcode %s not available in this mode", str);
1564 return;
1565 }
07726851 1566 memcpy (insn, opcode->opcode, sizeof (insn));
07855bec 1567 md_gather_operands (s, insn, opcode);
a85d7ed0
NC
1568}
1569
1570#ifndef WORKING_DOT_WORD
1571/* Handle long and short jumps. We don't support these */
1572void
1573md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1574 char *ptr;
1575 addressT from_addr, to_addr;
1576 fragS *frag;
1577 symbolS *to_symbol;
1578{
1579 abort ();
1580}
1581
1582void
1583md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1584 char *ptr;
1585 addressT from_addr, to_addr;
1586 fragS *frag;
1587 symbolS *to_symbol;
1588{
1589 abort ();
1590}
1591#endif
1592
1593void
1594s390_bss (ignore)
1595 int ignore ATTRIBUTE_UNUSED;
1596{
1597 /* We don't support putting frags in the BSS segment, we fake it
1598 by marking in_bss, then looking at s_skip for clues. */
1599
1600 subseg_set (bss_section, 0);
1601 demand_empty_rest_of_line ();
1602}
1603
1604/* Pseudo-op handling. */
1605
1606void
07726851 1607s390_insn (ignore)
a85d7ed0
NC
1608 int ignore ATTRIBUTE_UNUSED;
1609{
1610 expressionS exp;
1611 const struct s390_opcode *opformat;
1612 unsigned char insn[6];
1613 char *s;
1614
1615 /* Get the opcode format. */
1616 s = input_line_pointer;
3882b010 1617 while (*s != '\0' && *s != ',' && ! ISSPACE (*s))
a85d7ed0
NC
1618 s++;
1619 if (*s != ',')
1620 as_bad (_("Invalid .insn format\n"));
1621 *s++ = '\0';
1622
1623 /* Look up the opcode in the hash table. */
1624 opformat = (struct s390_opcode *)
1625 hash_find (s390_opformat_hash, input_line_pointer);
07855bec
NC
1626 if (opformat == (const struct s390_opcode *) NULL)
1627 {
1628 as_bad (_("Unrecognized opcode format: `%s'"), input_line_pointer);
1629 return;
1630 }
a85d7ed0
NC
1631 input_line_pointer = s;
1632 expression (&exp);
07855bec
NC
1633 if (exp.X_op == O_constant)
1634 {
1d6d62a4 1635 if ( ( opformat->oplen == 6
1d6d62a4
MS
1636 && (addressT) exp.X_add_number < (1ULL << 48))
1637 || ( opformat->oplen == 4
1d6d62a4
MS
1638 && (addressT) exp.X_add_number < (1ULL << 32))
1639 || ( opformat->oplen == 2
1d6d62a4 1640 && (addressT) exp.X_add_number < (1ULL << 16)))
2132e3a3 1641 md_number_to_chars ((char *) insn, exp.X_add_number, opformat->oplen);
07855bec 1642 else
07726851 1643 as_bad (_("Invalid .insn format\n"));
07855bec
NC
1644 }
1645 else if (exp.X_op == O_big)
1646 {
16a419ba
NC
1647 if (exp.X_add_number > 0
1648 && opformat->oplen == 6
1649 && generic_bignum[3] == 0)
07855bec 1650 {
2132e3a3
AM
1651 md_number_to_chars ((char *) insn, generic_bignum[2], 2);
1652 md_number_to_chars ((char *) &insn[2], generic_bignum[1], 2);
1653 md_number_to_chars ((char *) &insn[4], generic_bignum[0], 2);
07855bec
NC
1654 }
1655 else
07726851 1656 as_bad (_("Invalid .insn format\n"));
07855bec
NC
1657 }
1658 else
a85d7ed0 1659 as_bad (_("second operand of .insn not a constant\n"));
a85d7ed0 1660
b6849f55
NC
1661 if (strcmp (opformat->name, "e") != 0 && *input_line_pointer++ != ',')
1662 as_bad (_("missing comma after insn constant\n"));
98d3f06f 1663
07726851 1664 if ((s = strchr (input_line_pointer, '\n')) != NULL)
a85d7ed0 1665 *s = '\0';
b6849f55
NC
1666 input_line_pointer = md_gather_operands (input_line_pointer, insn,
1667 opformat);
a85d7ed0
NC
1668 if (s != NULL)
1669 *s = '\n';
1670 demand_empty_rest_of_line ();
1671}
1672
1673/* The .byte pseudo-op. This is similar to the normal .byte
1674 pseudo-op, but it can also take a single ASCII string. */
1675
1676static void
1677s390_byte (ignore)
1678 int ignore ATTRIBUTE_UNUSED;
1679{
1680 if (*input_line_pointer != '\"')
1681 {
1682 cons (1);
1683 return;
1684 }
1685
1686 /* Gather characters. A real double quote is doubled. Unusual
1687 characters are not permitted. */
1688 ++input_line_pointer;
1689 while (1)
1690 {
1691 char c;
1692
1693 c = *input_line_pointer++;
1694
1695 if (c == '\"')
1696 {
1697 if (*input_line_pointer != '\"')
1698 break;
1699 ++input_line_pointer;
1700 }
1701
1702 FRAG_APPEND_1_CHAR (c);
1703 }
1704
1705 demand_empty_rest_of_line ();
1706}
1707
1708/* The .ltorg pseudo-op.This emits all literals defined since the last
198ce79b 1709 .ltorg or the invocation of gas. Literals are defined with the
a85d7ed0
NC
1710 @lit suffix. */
1711
1712static void
1713s390_literals (ignore)
1714 int ignore ATTRIBUTE_UNUSED;
1715{
1716 struct s390_lpe *lpe;
1717
1718 if (lp_sym == NULL || lpe_count == 0)
98d3f06f 1719 return; /* Nothing to be done. */
a85d7ed0
NC
1720
1721 /* Emit symbol for start of literal pool. */
1722 S_SET_SEGMENT (lp_sym, now_seg);
1723 S_SET_VALUE (lp_sym, (valueT) frag_now_fix ());
1724 lp_sym->sy_frag = frag_now;
1725
07855bec
NC
1726 while (lpe_list)
1727 {
1728 lpe = lpe_list;
1729 lpe_list = lpe_list->next;
1730 S_SET_SEGMENT (lpe->sym, now_seg);
1731 S_SET_VALUE (lpe->sym, (valueT) frag_now_fix ());
1732 lpe->sym->sy_frag = frag_now;
1733
1734 /* Emit literal pool entry. */
1735 if (lpe->reloc != BFD_RELOC_UNUSED)
1736 {
198ce79b 1737 reloc_howto_type *reloc_howto =
07855bec
NC
1738 bfd_reloc_type_lookup (stdoutput, lpe->reloc);
1739 int size = bfd_get_reloc_size (reloc_howto);
1740 char *where;
1741
1742 if (size > lpe->nbytes)
1743 as_bad (_("%s relocations do not fit in %d bytes"),
1744 reloc_howto->name, lpe->nbytes);
07726851 1745 where = frag_more (lpe->nbytes);
07855bec
NC
1746 md_number_to_chars (where, 0, size);
1747 fix_new_exp (frag_now, where - frag_now->fr_literal,
1748 size, &lpe->ex, reloc_howto->pc_relative, lpe->reloc);
1749 }
1750 else
1751 {
1752 if (lpe->ex.X_op == O_big)
1753 {
1754 if (lpe->ex.X_add_number <= 0)
1755 generic_floating_point_number = lpe->floatnum;
1756 else
1757 memcpy (generic_bignum, lpe->bignum,
98d3f06f 1758 lpe->ex.X_add_number * sizeof (LITTLENUM_TYPE));
07855bec
NC
1759 }
1760 emit_expr (&lpe->ex, lpe->nbytes);
1761 }
a85d7ed0 1762
07855bec
NC
1763 lpe->next = lpe_free_list;
1764 lpe_free_list = lpe;
1765 }
a85d7ed0
NC
1766 lpe_list_tail = NULL;
1767 lp_sym = NULL;
1768 lp_count++;
1769 lpe_count = 0;
1770}
1771
1772/* Turn a string in input_line_pointer into a floating point constant
1773 of type type, and store the appropriate bytes in *litp. The number
1774 of LITTLENUMS emitted is stored in *sizep . An error message is
1775 returned, or NULL on OK. */
1776
1777char *
1778md_atof (type, litp, sizep)
1779 int type;
1780 char *litp;
1781 int *sizep;
1782{
1783 int prec;
1784 LITTLENUM_TYPE words[4];
1785 char *t;
1786 int i;
1787
1788 switch (type)
1789 {
1790 case 'f':
1791 prec = 2;
1792 break;
1793
1794 case 'd':
1795 prec = 4;
1796 break;
1797
1798 default:
1799 *sizep = 0;
1800 return "bad call to md_atof";
1801 }
1802
1803 t = atof_ieee (input_line_pointer, type, words);
1804 if (t)
1805 input_line_pointer = t;
1806
1807 *sizep = prec * 2;
1808
1809 for (i = 0; i < prec; i++)
1810 {
1811 md_number_to_chars (litp, (valueT) words[i], 2);
1812 litp += 2;
1813 }
198ce79b 1814
a85d7ed0
NC
1815 return NULL;
1816}
1817
1818/* Align a section (I don't know why this is machine dependent). */
1819
1820valueT
1821md_section_align (seg, addr)
1822 asection *seg;
1823 valueT addr;
1824{
1825 int align = bfd_get_section_alignment (stdoutput, seg);
1826
1827 return ((addr + (1 << align) - 1) & (-1 << align));
1828}
1829
1830/* We don't have any form of relaxing. */
1831
1832int
1833md_estimate_size_before_relax (fragp, seg)
1834 fragS *fragp ATTRIBUTE_UNUSED;
1835 asection *seg ATTRIBUTE_UNUSED;
1836{
1837 abort ();
1838 return 0;
1839}
1840
1841/* Convert a machine dependent frag. We never generate these. */
1842
1843void
1844md_convert_frag (abfd, sec, fragp)
1845 bfd *abfd ATTRIBUTE_UNUSED;
1846 asection *sec ATTRIBUTE_UNUSED;
1847 fragS *fragp ATTRIBUTE_UNUSED;
1848{
1849 abort ();
1850}
1851
1852symbolS *
1853md_undefined_symbol (name)
1854 char *name;
1855{
98d3f06f 1856 if (*name == '_' && *(name + 1) == 'G'
07726851 1857 && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
98d3f06f
KH
1858 {
1859 if (!GOT_symbol)
1860 {
1861 if (symbol_find (name))
1862 as_bad (_("GOT already in symbol table"));
1863 GOT_symbol = symbol_new (name, undefined_section,
1864 (valueT) 0, &zero_address_frag);
1865 }
1866 return GOT_symbol;
1867 }
a85d7ed0
NC
1868 return 0;
1869}
1870
1871/* Functions concerning relocs. */
1872
1873/* The location from which a PC relative jump should be calculated,
1874 given a PC relative reloc. */
1875
1876long
1877md_pcrel_from_section (fixp, sec)
1878 fixS *fixp;
1879 segT sec ATTRIBUTE_UNUSED;
1880{
1881 return fixp->fx_frag->fr_address + fixp->fx_where;
1882}
1883
1884/* Here we decide which fixups can be adjusted to make them relative to
1885 the beginning of the section instead of the symbol. Basically we need
1886 to make sure that the dynamic relocations are done correctly, so in
1887 some cases we force the original symbol to be used. */
1888int
98d3f06f
KH
1889tc_s390_fix_adjustable (fixP)
1890 fixS *fixP;
a85d7ed0 1891{
0a00dd48
MS
1892 /* Don't adjust references to merge sections. */
1893 if ((S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0)
1894 return 0;
a85d7ed0 1895 /* adjust_reloc_syms doesn't know about the GOT. */
2a19f73f
MS
1896 if ( fixP->fx_r_type == BFD_RELOC_16_GOTOFF
1897 || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
1898 || fixP->fx_r_type == BFD_RELOC_390_GOTOFF64
1899 || fixP->fx_r_type == BFD_RELOC_390_PLTOFF16
1900 || fixP->fx_r_type == BFD_RELOC_390_PLTOFF32
1901 || fixP->fx_r_type == BFD_RELOC_390_PLTOFF64
a85d7ed0
NC
1902 || fixP->fx_r_type == BFD_RELOC_390_PLT16DBL
1903 || fixP->fx_r_type == BFD_RELOC_390_PLT32
1904 || fixP->fx_r_type == BFD_RELOC_390_PLT32DBL
1905 || fixP->fx_r_type == BFD_RELOC_390_PLT64
1906 || fixP->fx_r_type == BFD_RELOC_390_GOT12
933fbc29 1907 || fixP->fx_r_type == BFD_RELOC_390_GOT20
a85d7ed0
NC
1908 || fixP->fx_r_type == BFD_RELOC_390_GOT16
1909 || fixP->fx_r_type == BFD_RELOC_32_GOT_PCREL
1910 || fixP->fx_r_type == BFD_RELOC_390_GOT64
07855bec 1911 || fixP->fx_r_type == BFD_RELOC_390_GOTENT
2a19f73f
MS
1912 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT12
1913 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT16
933fbc29 1914 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT20
2a19f73f
MS
1915 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT32
1916 || fixP->fx_r_type == BFD_RELOC_390_GOTPLT64
1917 || fixP->fx_r_type == BFD_RELOC_390_GOTPLTENT
1971b29d
MS
1918 || fixP->fx_r_type == BFD_RELOC_390_TLS_LOAD
1919 || fixP->fx_r_type == BFD_RELOC_390_TLS_GDCALL
1920 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDCALL
1921 || fixP->fx_r_type == BFD_RELOC_390_TLS_GD32
1922 || fixP->fx_r_type == BFD_RELOC_390_TLS_GD64
1923 || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE12
933fbc29 1924 || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE20
1971b29d
MS
1925 || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE32
1926 || fixP->fx_r_type == BFD_RELOC_390_TLS_GOTIE64
1927 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDM32
1928 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDM64
1929 || fixP->fx_r_type == BFD_RELOC_390_TLS_IE32
1930 || fixP->fx_r_type == BFD_RELOC_390_TLS_IE64
1931 || fixP->fx_r_type == BFD_RELOC_390_TLS_IEENT
1932 || fixP->fx_r_type == BFD_RELOC_390_TLS_LE32
1933 || fixP->fx_r_type == BFD_RELOC_390_TLS_LE64
1934 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDO32
1935 || fixP->fx_r_type == BFD_RELOC_390_TLS_LDO64
1936 || fixP->fx_r_type == BFD_RELOC_390_TLS_DTPMOD
1937 || fixP->fx_r_type == BFD_RELOC_390_TLS_DTPOFF
1938 || fixP->fx_r_type == BFD_RELOC_390_TLS_TPOFF
a85d7ed0
NC
1939 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1940 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1941 return 0;
1942 return 1;
1943}
1944
b8edc45c 1945/* Return true if we must always emit a reloc for a type and false if
c03099e6 1946 there is some hope of resolving it at assembly time. */
b8edc45c
MS
1947int
1948tc_s390_force_relocation (fixp)
1949 struct fix *fixp;
1950{
1951 /* Ensure we emit a relocation for every reference to the global
1952 offset table or to the procedure link table. */
1953 switch (fixp->fx_r_type)
1954 {
1955 case BFD_RELOC_390_GOT12:
933fbc29 1956 case BFD_RELOC_390_GOT20:
b8edc45c
MS
1957 case BFD_RELOC_32_GOT_PCREL:
1958 case BFD_RELOC_32_GOTOFF:
2a19f73f
MS
1959 case BFD_RELOC_390_GOTOFF64:
1960 case BFD_RELOC_390_PLTOFF16:
1961 case BFD_RELOC_390_PLTOFF32:
1962 case BFD_RELOC_390_PLTOFF64:
b8edc45c
MS
1963 case BFD_RELOC_390_GOTPC:
1964 case BFD_RELOC_390_GOT16:
1965 case BFD_RELOC_390_GOTPCDBL:
1966 case BFD_RELOC_390_GOT64:
1967 case BFD_RELOC_390_GOTENT:
1968 case BFD_RELOC_390_PLT32:
1969 case BFD_RELOC_390_PLT16DBL:
1970 case BFD_RELOC_390_PLT32DBL:
1971 case BFD_RELOC_390_PLT64:
2a19f73f
MS
1972 case BFD_RELOC_390_GOTPLT12:
1973 case BFD_RELOC_390_GOTPLT16:
933fbc29 1974 case BFD_RELOC_390_GOTPLT20:
2a19f73f
MS
1975 case BFD_RELOC_390_GOTPLT32:
1976 case BFD_RELOC_390_GOTPLT64:
1977 case BFD_RELOC_390_GOTPLTENT:
b8edc45c
MS
1978 return 1;
1979 default:
a161fe53 1980 break;;
b8edc45c 1981 }
a161fe53 1982
ae6063d4 1983 return generic_force_reloc (fixp);
b8edc45c
MS
1984}
1985
a85d7ed0
NC
1986/* Apply a fixup to the object code. This is called for all the
1987 fixups we generated by the call to fix_new_exp, above. In the call
1988 above we used a reloc code which was the largest legal reloc code
1989 plus the operand index. Here we undo that to recover the operand
1990 index. At this point all symbol values should be fully resolved,
1991 and we attempt to completely resolve the reloc. If we can not do
1992 that, we determine the correct reloc code and put it back in the
1993 fixup. */
1994
94f592af 1995void
55cf6793 1996md_apply_fix (fixP, valP, seg)
94f592af
NC
1997 fixS *fixP;
1998 valueT *valP;
a161fe53 1999 segT seg ATTRIBUTE_UNUSED;
a85d7ed0
NC
2000{
2001 char *where;
98d3f06f 2002 valueT value = *valP;
a85d7ed0 2003
94f592af 2004 where = fixP->fx_frag->fr_literal + fixP->fx_where;
a85d7ed0 2005
98d3f06f 2006 if (fixP->fx_subsy != NULL)
f1fc51da
MS
2007 as_bad_where (fixP->fx_file, fixP->fx_line,
2008 "cannot emit relocation %s against subsy symbol %s",
2009 bfd_get_reloc_code_name (fixP->fx_r_type),
2010 S_GET_NAME (fixP->fx_subsy));
98d3f06f
KH
2011
2012 if (fixP->fx_addsy != NULL)
07855bec 2013 {
94f592af
NC
2014 if (fixP->fx_pcrel)
2015 value += fixP->fx_frag->fr_address + fixP->fx_where;
07855bec
NC
2016 }
2017 else
94f592af 2018 fixP->fx_done = 1;
a85d7ed0 2019
94f592af 2020 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
07855bec
NC
2021 {
2022 const struct s390_operand *operand;
2023 int opindex;
198ce79b 2024
94f592af 2025 opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
07855bec 2026 operand = &s390_operands[opindex];
198ce79b 2027
94f592af 2028 if (fixP->fx_done)
07855bec
NC
2029 {
2030 /* Insert the fully resolved operand value. */
2132e3a3
AM
2031 s390_insert_operand ((unsigned char *) where, operand,
2032 (offsetT) value, fixP->fx_file, fixP->fx_line);
94f592af 2033 return;
07855bec 2034 }
198ce79b 2035
07855bec
NC
2036 /* Determine a BFD reloc value based on the operand information.
2037 We are only prepared to turn a few of the operands into
2038 relocs. */
94f592af 2039 fixP->fx_offset = value;
07855bec
NC
2040 if (operand->bits == 12 && operand->shift == 20)
2041 {
94f592af
NC
2042 fixP->fx_size = 2;
2043 fixP->fx_where += 2;
2044 fixP->fx_r_type = BFD_RELOC_390_12;
07855bec
NC
2045 }
2046 else if (operand->bits == 12 && operand->shift == 36)
2047 {
94f592af
NC
2048 fixP->fx_size = 2;
2049 fixP->fx_where += 4;
2050 fixP->fx_r_type = BFD_RELOC_390_12;
07855bec 2051 }
933fbc29
MS
2052 else if (operand->bits == 20 && operand->shift == 20)
2053 {
2054 fixP->fx_size = 2;
2055 fixP->fx_where += 2;
2056 fixP->fx_r_type = BFD_RELOC_390_20;
2057 }
07855bec
NC
2058 else if (operand->bits == 8 && operand->shift == 8)
2059 {
94f592af
NC
2060 fixP->fx_size = 1;
2061 fixP->fx_where += 1;
2062 fixP->fx_r_type = BFD_RELOC_8;
07855bec
NC
2063 }
2064 else if (operand->bits == 16 && operand->shift == 16)
2065 {
94f592af
NC
2066 fixP->fx_size = 2;
2067 fixP->fx_where += 2;
07855bec
NC
2068 if (operand->flags & S390_OPERAND_PCREL)
2069 {
94f592af
NC
2070 fixP->fx_r_type = BFD_RELOC_390_PC16DBL;
2071 fixP->fx_offset += 2;
07855bec
NC
2072 }
2073 else
94f592af 2074 fixP->fx_r_type = BFD_RELOC_16;
07855bec 2075 }
16a419ba
NC
2076 else if (operand->bits == 32 && operand->shift == 16
2077 && (operand->flags & S390_OPERAND_PCREL))
07855bec 2078 {
94f592af
NC
2079 fixP->fx_size = 4;
2080 fixP->fx_where += 2;
2081 fixP->fx_offset += 2;
2082 fixP->fx_r_type = BFD_RELOC_390_PC32DBL;
07855bec 2083 }
a85d7ed0 2084 else
07855bec
NC
2085 {
2086 char *sfile;
2087 unsigned int sline;
198ce79b 2088
07855bec
NC
2089 /* Use expr_symbol_where to see if this is an expression
2090 symbol. */
94f592af
NC
2091 if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
2092 as_bad_where (fixP->fx_file, fixP->fx_line,
07855bec
NC
2093 _("unresolved expression that must be resolved"));
2094 else
94f592af 2095 as_bad_where (fixP->fx_file, fixP->fx_line,
07855bec 2096 _("unsupported relocation type"));
94f592af
NC
2097 fixP->fx_done = 1;
2098 return;
07855bec 2099 }
a85d7ed0 2100 }
07855bec
NC
2101 else
2102 {
94f592af 2103 switch (fixP->fx_r_type)
07855bec
NC
2104 {
2105 case BFD_RELOC_8:
94f592af 2106 if (fixP->fx_pcrel)
07855bec 2107 abort ();
94f592af 2108 if (fixP->fx_done)
07855bec
NC
2109 md_number_to_chars (where, value, 1);
2110 break;
2111 case BFD_RELOC_390_12:
2112 case BFD_RELOC_390_GOT12:
2a19f73f 2113 case BFD_RELOC_390_GOTPLT12:
94f592af 2114 if (fixP->fx_done)
07855bec
NC
2115 {
2116 unsigned short mop;
2117
2118 mop = bfd_getb16 ((unsigned char *) where);
2119 mop |= (unsigned short) (value & 0xfff);
2120 bfd_putb16 ((bfd_vma) mop, (unsigned char *) where);
198ce79b 2121 }
07855bec 2122 break;
198ce79b 2123
933fbc29
MS
2124 case BFD_RELOC_390_20:
2125 case BFD_RELOC_390_GOT20:
2126 case BFD_RELOC_390_GOTPLT20:
2127 if (fixP->fx_done)
2128 {
2129 unsigned int mop;
2130 mop = bfd_getb32 ((unsigned char *) where);
2131 mop |= (unsigned int) ((value & 0xfff) << 8 |
2132 (value & 0xff000) >> 12);
2133 bfd_putb32 ((bfd_vma) mop, (unsigned char *) where);
2134 }
2135 break;
2136
07855bec
NC
2137 case BFD_RELOC_16:
2138 case BFD_RELOC_GPREL16:
2139 case BFD_RELOC_16_GOT_PCREL:
2140 case BFD_RELOC_16_GOTOFF:
94f592af
NC
2141 if (fixP->fx_pcrel)
2142 as_bad_where (fixP->fx_file, fixP->fx_line,
07855bec 2143 "cannot emit PC relative %s relocation%s%s",
94f592af
NC
2144 bfd_get_reloc_code_name (fixP->fx_r_type),
2145 fixP->fx_addsy != NULL ? " against " : "",
2146 (fixP->fx_addsy != NULL
2147 ? S_GET_NAME (fixP->fx_addsy)
07855bec 2148 : ""));
94f592af 2149 if (fixP->fx_done)
07855bec
NC
2150 md_number_to_chars (where, value, 2);
2151 break;
2152 case BFD_RELOC_390_GOT16:
2a19f73f
MS
2153 case BFD_RELOC_390_PLTOFF16:
2154 case BFD_RELOC_390_GOTPLT16:
94f592af 2155 if (fixP->fx_done)
07855bec
NC
2156 md_number_to_chars (where, value, 2);
2157 break;
2158 case BFD_RELOC_390_PC16DBL:
2159 case BFD_RELOC_390_PLT16DBL:
2160 value += 2;
94f592af 2161 if (fixP->fx_done)
07855bec
NC
2162 md_number_to_chars (where, (offsetT) value >> 1, 2);
2163 break;
2164
2165 case BFD_RELOC_32:
94f592af
NC
2166 if (fixP->fx_pcrel)
2167 fixP->fx_r_type = BFD_RELOC_32_PCREL;
07855bec 2168 else
94f592af
NC
2169 fixP->fx_r_type = BFD_RELOC_32;
2170 if (fixP->fx_done)
07855bec
NC
2171 md_number_to_chars (where, value, 4);
2172 break;
2173 case BFD_RELOC_32_PCREL:
2174 case BFD_RELOC_32_BASEREL:
94f592af
NC
2175 fixP->fx_r_type = BFD_RELOC_32_PCREL;
2176 if (fixP->fx_done)
07855bec
NC
2177 md_number_to_chars (where, value, 4);
2178 break;
2179 case BFD_RELOC_32_GOT_PCREL:
2a19f73f 2180 case BFD_RELOC_390_PLTOFF32:
07855bec 2181 case BFD_RELOC_390_PLT32:
2a19f73f 2182 case BFD_RELOC_390_GOTPLT32:
94f592af 2183 if (fixP->fx_done)
07855bec
NC
2184 md_number_to_chars (where, value, 4);
2185 break;
2186 case BFD_RELOC_390_PC32DBL:
2187 case BFD_RELOC_390_PLT32DBL:
2188 case BFD_RELOC_390_GOTPCDBL:
2189 case BFD_RELOC_390_GOTENT:
2a19f73f 2190 case BFD_RELOC_390_GOTPLTENT:
07855bec 2191 value += 2;
94f592af 2192 if (fixP->fx_done)
07855bec
NC
2193 md_number_to_chars (where, (offsetT) value >> 1, 4);
2194 break;
2195
2196 case BFD_RELOC_32_GOTOFF:
94f592af 2197 if (fixP->fx_done)
07726851 2198 md_number_to_chars (where, value, sizeof (int));
07855bec
NC
2199 break;
2200
2a19f73f
MS
2201 case BFD_RELOC_390_GOTOFF64:
2202 if (fixP->fx_done)
2203 md_number_to_chars (where, value, 8);
2204 break;
2205
07855bec 2206 case BFD_RELOC_390_GOT64:
2a19f73f 2207 case BFD_RELOC_390_PLTOFF64:
07855bec 2208 case BFD_RELOC_390_PLT64:
2a19f73f 2209 case BFD_RELOC_390_GOTPLT64:
94f592af 2210 if (fixP->fx_done)
07855bec
NC
2211 md_number_to_chars (where, value, 8);
2212 break;
2213
2214 case BFD_RELOC_64:
94f592af
NC
2215 if (fixP->fx_pcrel)
2216 fixP->fx_r_type = BFD_RELOC_64_PCREL;
07855bec 2217 else
94f592af
NC
2218 fixP->fx_r_type = BFD_RELOC_64;
2219 if (fixP->fx_done)
07855bec
NC
2220 md_number_to_chars (where, value, 8);
2221 break;
2222
2223 case BFD_RELOC_64_PCREL:
94f592af
NC
2224 fixP->fx_r_type = BFD_RELOC_64_PCREL;
2225 if (fixP->fx_done)
07855bec
NC
2226 md_number_to_chars (where, value, 8);
2227 break;
2228
2229 case BFD_RELOC_VTABLE_INHERIT:
2230 case BFD_RELOC_VTABLE_ENTRY:
94f592af
NC
2231 fixP->fx_done = 0;
2232 return;
07855bec 2233
1971b29d
MS
2234 case BFD_RELOC_390_TLS_LOAD:
2235 case BFD_RELOC_390_TLS_GDCALL:
2236 case BFD_RELOC_390_TLS_LDCALL:
2237 case BFD_RELOC_390_TLS_GD32:
2238 case BFD_RELOC_390_TLS_GD64:
2239 case BFD_RELOC_390_TLS_GOTIE12:
933fbc29 2240 case BFD_RELOC_390_TLS_GOTIE20:
1971b29d
MS
2241 case BFD_RELOC_390_TLS_GOTIE32:
2242 case BFD_RELOC_390_TLS_GOTIE64:
2243 case BFD_RELOC_390_TLS_LDM32:
2244 case BFD_RELOC_390_TLS_LDM64:
2245 case BFD_RELOC_390_TLS_IE32:
2246 case BFD_RELOC_390_TLS_IE64:
2247 case BFD_RELOC_390_TLS_LE32:
2248 case BFD_RELOC_390_TLS_LE64:
2249 case BFD_RELOC_390_TLS_LDO32:
2250 case BFD_RELOC_390_TLS_LDO64:
2251 case BFD_RELOC_390_TLS_DTPMOD:
2252 case BFD_RELOC_390_TLS_DTPOFF:
2253 case BFD_RELOC_390_TLS_TPOFF:
7c1d0959 2254 S_SET_THREAD_LOCAL (fixP->fx_addsy);
1971b29d
MS
2255 /* Fully resolved at link time. */
2256 break;
2257 case BFD_RELOC_390_TLS_IEENT:
2258 /* Fully resolved at link time. */
7c1d0959 2259 S_SET_THREAD_LOCAL (fixP->fx_addsy);
1971b29d
MS
2260 value += 2;
2261 break;
2262
07855bec
NC
2263 default:
2264 {
94f592af 2265 const char *reloc_name = bfd_get_reloc_code_name (fixP->fx_r_type);
198ce79b 2266
07855bec
NC
2267 if (reloc_name != NULL)
2268 fprintf (stderr, "Gas failure, reloc type %s\n", reloc_name);
2269 else
94f592af 2270 fprintf (stderr, "Gas failure, reloc type #%i\n", fixP->fx_r_type);
07855bec
NC
2271 fflush (stderr);
2272 abort ();
2273 }
2274 }
a85d7ed0 2275
94f592af 2276 fixP->fx_offset = value;
07855bec 2277 }
a85d7ed0
NC
2278}
2279
2280/* Generate a reloc for a fixup. */
2281
2282arelent *
2283tc_gen_reloc (seg, fixp)
2284 asection *seg ATTRIBUTE_UNUSED;
2285 fixS *fixp;
2286{
2287 bfd_reloc_code_real_type code;
2288 arelent *reloc;
2289
2290 code = fixp->fx_r_type;
07855bec
NC
2291 if (GOT_symbol && fixp->fx_addsy == GOT_symbol)
2292 {
16a419ba
NC
2293 if ( (s390_arch_size == 32 && code == BFD_RELOC_32_PCREL)
2294 || (s390_arch_size == 64 && code == BFD_RELOC_64_PCREL))
07855bec
NC
2295 code = BFD_RELOC_390_GOTPC;
2296 if (code == BFD_RELOC_390_PC32DBL)
2297 code = BFD_RELOC_390_GOTPCDBL;
2298 }
a85d7ed0
NC
2299
2300 reloc = (arelent *) xmalloc (sizeof (arelent));
2301 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2302 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2303 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2304 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
2305 if (reloc->howto == NULL)
2306 {
2307 as_bad_where (fixp->fx_file, fixp->fx_line,
98d3f06f
KH
2308 _("cannot represent relocation type %s"),
2309 bfd_get_reloc_code_name (code));
a85d7ed0
NC
2310 /* Set howto to a garbage value so that we can keep going. */
2311 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
2312 assert (reloc->howto != NULL);
2313 }
2314 reloc->addend = fixp->fx_offset;
2315
2316 return reloc;
2317}
75e21f08
JJ
2318
2319void
2320s390_cfi_frame_initial_instructions ()
2321{
2322 cfi_add_CFA_def_cfa (15, s390_arch_size == 64 ? 160 : 96);
2323}
2324
2325int
1df69f4f 2326tc_s390_regname_to_dw2regnum (char *regname)
75e21f08
JJ
2327{
2328 int regnum = -1;
2329
2330 if (regname[0] != 'c' && regname[0] != 'a')
2331 {
2332 regnum = reg_name_search (pre_defined_registers, REG_NAME_CNT, regname);
2333 if (regname[0] == 'f' && regnum != -1)
2334 regnum += 16;
2335 }
2336 else if (strcmp (regname, "ap") == 0)
2337 regnum = 32;
2338 else if (strcmp (regname, "cc") == 0)
2339 regnum = 33;
2340 return regnum;
2341}