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