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