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