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