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