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