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