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