PR 10437
[deliverable/binutils-gdb.git] / gas / config / tc-i370.c
CommitLineData
5b93d8bb
AM
1/* tc-i370.c -- Assembler for the IBM 360/370/390 instruction set.
2 Loosely based on the ppc files by Linas Vepstas <linas@linas.org> 1998, 99
2132e3a3 3 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
20203fb9 4 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5b93d8bb
AM
5 Written by Ian Lance Taylor, Cygnus Support.
6
7 This file is part of GAS, the GNU Assembler.
8
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
ec2655a6 11 the Free Software Foundation; either version 3, or (at your option)
5b93d8bb
AM
12 any later version.
13
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
21 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
5b93d8bb
AM
23
24/* This assembler implements a very hacked version of an elf-like thing
ea1562b3
NC
25 that gcc emits (when gcc is suitably hacked). To make it behave more
26 HLASM-like, try turning on the -M or --mri flag (as there are various
27 similarities between HLASM and the MRI assemblers, such as section
28 names, lack of leading . in pseudo-ops, DC and DS, etc. */
5b93d8bb 29
5b93d8bb 30#include "as.h"
44addf7f 31#include "safe-ctype.h"
5b93d8bb
AM
32#include "subsegs.h"
33#include "struc-symbol.h"
34
35#include "opcode/i370.h"
36
37#ifdef OBJ_ELF
38#include "elf/i370.h"
39#endif
40
ea1562b3 41/* This is the assembler for the System/390 Architecture. */
5b93d8bb
AM
42
43/* Tell the main code what the endianness is. */
44extern int target_big_endian;
45
46\f
47/* Generic assembler global variables which must be defined by all
48 targets. */
49
50#ifdef OBJ_ELF
51/* This string holds the chars that always start a comment. If the
52 pre-processor is disabled, these aren't very useful. The macro
53 tc_comment_chars points to this. We use this, rather than the
54 usual comment_chars, so that we can switch for Solaris conventions. */
55static const char i370_eabi_comment_chars[] = "#";
56
57const char *i370_comment_chars = i370_eabi_comment_chars;
58#else
59const char comment_chars[] = "#";
60#endif
61
62/* Characters which start a comment at the beginning of a line. */
63const char line_comment_chars[] = "#*";
64
65/* Characters which may be used to separate multiple commands on a
66 single line. */
67const char line_separator_chars[] = ";";
68
69/* Characters which are used to indicate an exponent in a floating
70 point number. */
71const char EXP_CHARS[] = "eE";
72
73/* Characters which mean that a number is a floating point constant,
74 as in 0d1.0. */
75const char FLT_CHARS[] = "dD";
76
5b93d8bb 77void
ea1562b3 78md_show_usage (FILE *stream)
5b93d8bb 79{
bc805888 80 fprintf (stream, "\
5b93d8bb
AM
81S/370 options: (these have not yet been tested and may not work) \n\
82-u ignored\n\
83-mregnames Allow symbolic names for registers\n\
84-mno-regnames Do not allow symbolic names for registers\n");
85#ifdef OBJ_ELF
bc805888 86 fprintf (stream, "\
5b93d8bb
AM
87-mrelocatable support for GCC's -mrelocatble option\n\
88-mrelocatable-lib support for GCC's -mrelocatble-lib option\n\
89-V print assembler version number\n");
90#endif
91}
92
5b93d8bb 93/* Whether to use user friendly register names. */
b34976b6 94#define TARGET_REG_NAMES_P TRUE
5b93d8bb 95
b34976b6 96static bfd_boolean reg_names_p = TARGET_REG_NAMES_P;
5b93d8bb 97
5b93d8bb 98\f
ea1562b3
NC
99/* Predefined register names if -mregnames
100 In general, there are lots of them, in an attempt to be compatible
101 with a number of assemblers. */
5b93d8bb
AM
102
103/* Structure to hold information about predefined registers. */
104struct pd_reg
105 {
106 char *name;
107 int value;
108 };
109
110/* List of registers that are pre-defined:
111
112 Each general register has predefined names of the form:
113 1. r<reg_num> which has the value <reg_num>.
114 2. r.<reg_num> which has the value <reg_num>.
115
5b93d8bb
AM
116 Each floating point register has predefined names of the form:
117 1. f<reg_num> which has the value <reg_num>.
118 2. f.<reg_num> which has the value <reg_num>.
119
120 There are only four floating point registers, and these are
121 commonly labelled 0,2,4 and 6. Thus, there is no f1, f3, etc.
122
5b93d8bb
AM
123 There are individual registers as well:
124 rbase or r.base has the value 3 (base register)
125 rpgt or r.pgt has the value 4 (page origin table pointer)
126 rarg or r.arg has the value 11 (argument pointer)
127 rtca or r.tca has the value 12 (table of contents pointer)
128 rtoc or r.toc has the value 12 (table of contents pointer)
129 sp or r.sp has the value 13 (stack pointer)
130 dsa or r.dsa has the value 13 (stack pointer)
131 lr has the value 14 (link reg)
132
92774660 133 The table is sorted. Suitable for searching by a binary search. */
5b93d8bb
AM
134
135static const struct pd_reg pre_defined_registers[] =
136{
ea1562b3
NC
137 { "arg", 11 }, /* Argument Pointer. */
138 { "base", 3 }, /* Base Reg. */
5b93d8bb 139
ea1562b3 140 { "f.0", 0 }, /* Floating point registers. */
5b93d8bb
AM
141 { "f.2", 2 },
142 { "f.4", 4 },
143 { "f.6", 6 },
144
145 { "f0", 0 },
146 { "f2", 2 },
147 { "f4", 4 },
148 { "f6", 6 },
149
ea1562b3
NC
150 { "dsa",13 }, /* Stack pointer. */
151 { "lr", 14 }, /* Link Register. */
152 { "pgt", 4 }, /* Page Origin Table Pointer. */
5b93d8bb 153
ea1562b3 154 { "r.0", 0 }, /* General Purpose Registers. */
5b93d8bb
AM
155 { "r.1", 1 },
156 { "r.10", 10 },
157 { "r.11", 11 },
158 { "r.12", 12 },
159 { "r.13", 13 },
160 { "r.14", 14 },
161 { "r.15", 15 },
162 { "r.2", 2 },
163 { "r.3", 3 },
164 { "r.4", 4 },
165 { "r.5", 5 },
166 { "r.6", 6 },
167 { "r.7", 7 },
168 { "r.8", 8 },
169 { "r.9", 9 },
170
ea1562b3
NC
171 { "r.arg", 11 }, /* Argument Pointer. */
172 { "r.base", 3 }, /* Base Reg. */
173 { "r.dsa", 13 }, /* Stack Pointer. */
174 { "r.pgt", 4 }, /* Page Origin Table Pointer. */
175 { "r.sp", 13 }, /* Stack Pointer. */
5b93d8bb 176
ea1562b3
NC
177 { "r.tca", 12 }, /* Pointer to the table of contents. */
178 { "r.toc", 12 }, /* Pointer to the table of contents. */
5b93d8bb 179
ea1562b3 180 { "r0", 0 }, /* More general purpose registers. */
5b93d8bb
AM
181 { "r1", 1 },
182 { "r10", 10 },
183 { "r11", 11 },
184 { "r12", 12 },
185 { "r13", 13 },
186 { "r14", 14 },
187 { "r15", 15 },
188 { "r2", 2 },
189 { "r3", 3 },
190 { "r4", 4 },
191 { "r5", 5 },
192 { "r6", 6 },
193 { "r7", 7 },
194 { "r8", 8 },
195 { "r9", 9 },
196
ea1562b3 197 { "rbase", 3 }, /* Base Reg. */
5b93d8bb 198
ea1562b3
NC
199 { "rtca", 12 }, /* Pointer to the table of contents. */
200 { "rtoc", 12 }, /* Pointer to the table of contents. */
5b93d8bb 201
ea1562b3 202 { "sp", 13 }, /* Stack Pointer. */
5b93d8bb
AM
203
204};
205
bc805888 206#define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct pd_reg))
5b93d8bb
AM
207
208/* Given NAME, find the register number associated with that name, return
209 the integer value associated with the given name or -1 on failure. */
210
5b93d8bb 211static int
ea1562b3
NC
212reg_name_search (const struct pd_reg *regs,
213 int regcount,
214 const char *name)
5b93d8bb
AM
215{
216 int middle, low, high;
217 int cmp;
218
219 low = 0;
220 high = regcount - 1;
221
222 do
223 {
224 middle = (low + high) / 2;
225 cmp = strcasecmp (name, regs[middle].name);
226 if (cmp < 0)
227 high = middle - 1;
228 else if (cmp > 0)
229 low = middle + 1;
230 else
231 return regs[middle].value;
232 }
233 while (low <= high);
234
235 return -1;
236}
237
ea1562b3
NC
238/* Summary of register_name().
239
240 in: Input_line_pointer points to 1st char of operand.
241
242 out: An expressionS.
243 The operand may have been a register: in this case, X_op == O_register,
244 X_add_number is set to the register number, and truth is returned.
245 Input_line_pointer->(next non-blank) char after operand, or is in its
246 original state. */
5b93d8bb 247
b34976b6 248static bfd_boolean
ea1562b3 249register_name (expressionS *expressionP)
5b93d8bb
AM
250{
251 int reg_number;
252 char *name;
253 char *start;
254 char c;
255
468cced8 256 /* Find the spelling of the operand. */
5b93d8bb 257 start = name = input_line_pointer;
3882b010 258 if (name[0] == '%' && ISALPHA (name[1]))
5b93d8bb
AM
259 name = ++input_line_pointer;
260
261 else if (!reg_names_p)
b34976b6 262 return FALSE;
5b93d8bb
AM
263
264 while (' ' == *name)
265 name = ++input_line_pointer;
266
468cced8
AM
267 /* If it's a number, treat it as a number. If it's alpha, look to
268 see if it's in the register table. */
3882b010 269 if (!ISALPHA (name[0]))
ea1562b3 270 reg_number = get_single_number ();
5b93d8bb
AM
271 else
272 {
273 c = get_symbol_end ();
274 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
468cced8
AM
275
276 /* Put back the delimiting char. */
277 *input_line_pointer = c;
5b93d8bb
AM
278 }
279
468cced8 280 /* If numeric, make sure its not out of bounds. */
5b93d8bb
AM
281 if ((0 <= reg_number) && (16 >= reg_number))
282 {
283 expressionP->X_op = O_register;
284 expressionP->X_add_number = reg_number;
285
468cced8 286 /* Make the rest nice. */
5b93d8bb
AM
287 expressionP->X_add_symbol = NULL;
288 expressionP->X_op_symbol = NULL;
b34976b6 289 return TRUE;
5b93d8bb
AM
290 }
291
468cced8
AM
292 /* Reset the line as if we had not done anything. */
293 input_line_pointer = start;
b34976b6 294 return FALSE;
5b93d8bb
AM
295}
296\f
297/* Local variables. */
298
299/* The type of processor we are assembling for. This is one or more
300 of the I370_OPCODE flags defined in opcode/i370.h. */
301static int i370_cpu = 0;
302
303/* The base register to use for opcode with optional operands.
ea1562b3
NC
304 We define two of these: "text" and "other". Normally, "text"
305 would get used in the .text section for branches, while "other"
306 gets used in the .data section for address constants.
307
308 The idea of a second base register in a different section
309 is foreign to the usual HLASM-style semantics; however, it
310 allows us to provide support for dynamically loaded libraries,
311 by allowing us to place address constants in a section other
312 than the text section. The "other" section need not be the
313 .data section, it can be any section that isn't the .text section.
314
315 Note that HLASM defines a multiple, concurrent .using semantic
316 that we do not: in calculating offsets, it uses either the most
317 recent .using directive, or the one with the smallest displacement.
318 This allows HLASM to support a quasi-block-scope-like behaviour.
319 Handy for people writing assembly by hand ... but not supported
320 by us. */
5b93d8bb
AM
321static int i370_using_text_regno = -1;
322static int i370_using_other_regno = -1;
323
ea1562b3 324/* The base address for address literals. */
5b93d8bb
AM
325static expressionS i370_using_text_baseaddr;
326static expressionS i370_using_other_baseaddr;
327
ea1562b3 328/* the "other" section, used only for syntax error detection. */
5b93d8bb
AM
329static segT i370_other_section = undefined_section;
330
331/* Opcode hash table. */
332static struct hash_control *i370_hash;
333
334/* Macro hash table. */
335static struct hash_control *i370_macro_hash;
336
337#ifdef OBJ_ELF
ea1562b3 338/* What type of shared library support to use. */
5b93d8bb
AM
339static enum { SHLIB_NONE, SHLIB_PIC, SHILB_MRELOCATABLE } shlib = SHLIB_NONE;
340#endif
341
ea1562b3 342/* Flags to set in the elf header. */
5b93d8bb
AM
343static flagword i370_flags = 0;
344
345#ifndef WORKING_DOT_WORD
2b4f075a
HPN
346int md_short_jump_size = 4;
347int md_long_jump_size = 4;
5b93d8bb
AM
348#endif
349\f
350#ifdef OBJ_ELF
5a38dc70 351const char *md_shortopts = "l:um:K:VQ:";
5b93d8bb 352#else
5a38dc70 353const char *md_shortopts = "um:";
5b93d8bb
AM
354#endif
355struct option md_longopts[] =
356{
357 {NULL, no_argument, NULL, 0}
358};
bc805888 359size_t md_longopts_size = sizeof (md_longopts);
5b93d8bb
AM
360
361int
ea1562b3 362md_parse_option (int c, char *arg)
5b93d8bb
AM
363{
364 switch (c)
365 {
366 case 'u':
367 /* -u means that any undefined symbols should be treated as
368 external, which is the default for gas anyhow. */
369 break;
370
371#ifdef OBJ_ELF
372 case 'K':
373 /* Recognize -K PIC */
374 if (strcmp (arg, "PIC") == 0 || strcmp (arg, "pic") == 0)
375 {
376 shlib = SHLIB_PIC;
377 i370_flags |= EF_I370_RELOCATABLE_LIB;
378 }
379 else
380 return 0;
381
382 break;
383#endif
384
385 case 'm':
386
ea1562b3 387 /* -m360 mean to assemble for the ancient 360 architecture. */
5b93d8bb
AM
388 if (strcmp (arg, "360") == 0 || strcmp (arg, "i360") == 0)
389 i370_cpu = I370_OPCODE_360;
ea1562b3 390 /* -mxa means to assemble for the IBM 370 XA. */
5b93d8bb
AM
391 else if (strcmp (arg, "xa") == 0)
392 i370_cpu = I370_OPCODE_370_XA;
393 /* -many means to assemble for any architecture (370/XA). */
394 else if (strcmp (arg, "any") == 0)
395 i370_cpu = I370_OPCODE_370;
396
397 else if (strcmp (arg, "regnames") == 0)
b34976b6 398 reg_names_p = TRUE;
5b93d8bb
AM
399
400 else if (strcmp (arg, "no-regnames") == 0)
b34976b6 401 reg_names_p = FALSE;
5b93d8bb
AM
402
403#ifdef OBJ_ELF
ea1562b3
NC
404 /* -mrelocatable/-mrelocatable-lib -- warn about
405 initializations that require relocation. */
5b93d8bb
AM
406 else if (strcmp (arg, "relocatable") == 0)
407 {
408 shlib = SHILB_MRELOCATABLE;
409 i370_flags |= EF_I370_RELOCATABLE;
410 }
5b93d8bb
AM
411 else if (strcmp (arg, "relocatable-lib") == 0)
412 {
413 shlib = SHILB_MRELOCATABLE;
414 i370_flags |= EF_I370_RELOCATABLE_LIB;
415 }
5b93d8bb
AM
416#endif
417 else
418 {
20203fb9 419 as_bad (_("invalid switch -m%s"), arg);
5b93d8bb
AM
420 return 0;
421 }
422 break;
423
424#ifdef OBJ_ELF
425 /* -V: SVR4 argument to print version ID. */
426 case 'V':
427 print_version_id ();
428 break;
429
430 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
431 should be emitted or not. FIXME: Not implemented. */
432 case 'Q':
433 break;
434
435#endif
436
437 default:
438 return 0;
439 }
440
441 return 1;
442}
443
444\f
445/* Set i370_cpu if it is not already set.
446 Currently defaults to the reasonable superset;
92774660 447 but can be made more fine grained if desred. */
5b93d8bb
AM
448
449static void
ea1562b3 450i370_set_cpu (void)
5b93d8bb
AM
451{
452 const char *default_os = TARGET_OS;
453 const char *default_cpu = TARGET_CPU;
454
ea1562b3 455 /* Override with the superset for the moment. */
5b93d8bb
AM
456 i370_cpu = I370_OPCODE_ESA390_SUPERSET;
457 if (i370_cpu == 0)
458 {
459 if (strcmp (default_cpu, "i360") == 0)
460 i370_cpu = I370_OPCODE_360;
461 else if (strcmp (default_cpu, "i370") == 0)
462 i370_cpu = I370_OPCODE_370;
463 else if (strcmp (default_cpu, "XA") == 0)
464 i370_cpu = I370_OPCODE_370_XA;
465 else
466 as_fatal ("Unknown default cpu = %s, os = %s", default_cpu, default_os);
467 }
468}
469
ea1562b3
NC
470/* Figure out the BFD architecture to use.
471 FIXME: specify the different 370 architectures. */
5b93d8bb
AM
472
473enum bfd_architecture
ea1562b3 474i370_arch (void)
5b93d8bb
AM
475{
476 return bfd_arch_i370;
477}
478
479/* This function is called when the assembler starts up. It is called
480 after the options have been parsed and the output file has been
481 opened. */
482
483void
ea1562b3 484md_begin (void)
5b93d8bb 485{
ea1562b3 486 const struct i370_opcode *op;
5b93d8bb
AM
487 const struct i370_opcode *op_end;
488 const struct i370_macro *macro;
489 const struct i370_macro *macro_end;
b34976b6 490 bfd_boolean dup_insn = FALSE;
5b93d8bb
AM
491
492 i370_set_cpu ();
493
494#ifdef OBJ_ELF
92774660 495 /* Set the ELF flags if desired. */
5b93d8bb
AM
496 if (i370_flags)
497 bfd_set_private_flags (stdoutput, i370_flags);
498#endif
499
500 /* Insert the opcodes into a hash table. */
501 i370_hash = hash_new ();
502
503 op_end = i370_opcodes + i370_num_opcodes;
504 for (op = i370_opcodes; op < op_end; op++)
505 {
c9dea48b
AM
506 know ((op->opcode.i[0] & op->mask.i[0]) == op->opcode.i[0]
507 && (op->opcode.i[1] & op->mask.i[1]) == op->opcode.i[1]);
5b93d8bb
AM
508
509 if ((op->flags & i370_cpu) != 0)
510 {
511 const char *retval;
512
ea1562b3 513 retval = hash_insert (i370_hash, op->name, (void *) op);
5b93d8bb
AM
514 if (retval != (const char *) NULL)
515 {
20203fb9 516 as_bad (_("Internal assembler error for instruction %s"), op->name);
b34976b6 517 dup_insn = TRUE;
5b93d8bb
AM
518 }
519 }
520 }
521
522 /* Insert the macros into a hash table. */
523 i370_macro_hash = hash_new ();
524
525 macro_end = i370_macros + i370_num_macros;
526 for (macro = i370_macros; macro < macro_end; macro++)
527 {
528 if ((macro->flags & i370_cpu) != 0)
529 {
530 const char *retval;
531
ea1562b3 532 retval = hash_insert (i370_macro_hash, macro->name, (void *) macro);
5b93d8bb
AM
533 if (retval != (const char *) NULL)
534 {
20203fb9 535 as_bad (_("Internal assembler error for macro %s"), macro->name);
b34976b6 536 dup_insn = TRUE;
5b93d8bb
AM
537 }
538 }
539 }
540
541 if (dup_insn)
542 abort ();
543}
544
545/* Insert an operand value into an instruction. */
546
547static i370_insn_t
ea1562b3
NC
548i370_insert_operand (i370_insn_t insn,
549 const struct i370_operand *operand,
550 offsetT val)
5b93d8bb
AM
551{
552 if (operand->insert)
553 {
554 const char *errmsg;
555
ea1562b3 556 /* Used for 48-bit insn's. */
5b93d8bb
AM
557 errmsg = NULL;
558 insn = (*operand->insert) (insn, (long) val, &errmsg);
559 if (errmsg)
560 as_bad ("%s", errmsg);
561 }
562 else
ea1562b3
NC
563 /* This is used only for 16, 32 bit insn's. */
564 insn.i[0] |= (((long) val & ((1 << operand->bits) - 1))
565 << operand->shift);
5b93d8bb
AM
566
567 return insn;
568}
569
570\f
571#ifdef OBJ_ELF
572/* Parse @got, etc. and return the desired relocation.
56d27c17
AM
573 Currently, i370 does not support (don't really need to support) any
574 of these fancier markups ... for example, no one is going to
575 write 'L 6,=V(bogus)@got' it just doesn't make sense (at least to me).
576 So basically, we could get away with this routine returning
577 BFD_RELOC_UNUSED in all circumstances. However, I'll leave
578 in for now in case someone ambitious finds a good use for this stuff ...
579 this routine was pretty much just copied from the powerpc code ... */
ea1562b3 580
5b93d8bb 581static bfd_reloc_code_real_type
ea1562b3 582i370_elf_suffix (char **str_p, expressionS *exp_p)
5b93d8bb
AM
583{
584 struct map_bfd
585 {
586 char *string;
587 int length;
588 bfd_reloc_code_real_type reloc;
589 };
590
591 char ident[20];
592 char *str = *str_p;
593 char *str2;
594 int ch;
595 int len;
596 struct map_bfd *ptr;
597
ea1562b3 598#define MAP(str,reloc) { str, sizeof (str) - 1, reloc }
5b93d8bb
AM
599
600 static struct map_bfd mapping[] =
601 {
56d27c17
AM
602 /* warnings with -mrelocatable. */
603 MAP ("fixup", BFD_RELOC_CTOR),
604 { (char *)0, 0, BFD_RELOC_UNUSED }
5b93d8bb
AM
605 };
606
607 if (*str++ != '@')
608 return BFD_RELOC_UNUSED;
609
610 for (ch = *str, str2 = ident;
611 (str2 < ident + sizeof (ident) - 1
3882b010 612 && (ISALNUM (ch) || ch == '@'));
5b93d8bb 613 ch = *++str)
ea1562b3 614 *str2++ = TOLOWER (ch);
5b93d8bb
AM
615
616 *str2 = '\0';
617 len = str2 - ident;
618
619 ch = ident[0];
620 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
621 if (ch == ptr->string[0]
622 && len == ptr->length
623 && memcmp (ident, ptr->string, ptr->length) == 0)
624 {
625 if (exp_p->X_add_number != 0
626 && (ptr->reloc == BFD_RELOC_16_GOTOFF
627 || ptr->reloc == BFD_RELOC_LO16_GOTOFF
628 || ptr->reloc == BFD_RELOC_HI16_GOTOFF
629 || ptr->reloc == BFD_RELOC_HI16_S_GOTOFF))
20203fb9 630 as_warn (_("identifier+constant@got means identifier@got+constant"));
5b93d8bb
AM
631
632 /* Now check for identifier@suffix+constant */
633 if (*str == '-' || *str == '+')
634 {
635 char *orig_line = input_line_pointer;
636 expressionS new_exp;
637
638 input_line_pointer = str;
639 expression (&new_exp);
640 if (new_exp.X_op == O_constant)
641 {
642 exp_p->X_add_number += new_exp.X_add_number;
643 str = input_line_pointer;
644 }
645
646 if (&input_line_pointer != str_p)
647 input_line_pointer = orig_line;
648 }
649
650 *str_p = str;
651 return ptr->reloc;
652 }
653
654 return BFD_RELOC_UNUSED;
655}
656
ea1562b3
NC
657/* Like normal .long/.short/.word, except support @got, etc.
658 Clobbers input_line_pointer, checks end-of-line. */
659
5b93d8bb 660static void
ea1562b3 661i370_elf_cons (int nbytes) /* 1=.byte, 2=.word, 4=.long. */
5b93d8bb
AM
662{
663 expressionS exp;
664 bfd_reloc_code_real_type reloc;
665
666 if (is_it_end_of_statement ())
667 {
668 demand_empty_rest_of_line ();
669 return;
670 }
671
672 do
673 {
674 expression (&exp);
ea1562b3 675
5b93d8bb
AM
676 if (exp.X_op == O_symbol
677 && *input_line_pointer == '@'
678 && (reloc = i370_elf_suffix (&input_line_pointer, &exp)) != BFD_RELOC_UNUSED)
679 {
680 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
681 int size = bfd_get_reloc_size (reloc_howto);
682
683 if (size > nbytes)
20203fb9
NC
684 as_bad (_("%s relocations do not fit in %d bytes\n"),
685 reloc_howto->name, nbytes);
5b93d8bb
AM
686 else
687 {
ea1562b3 688 char *p = frag_more ((int) nbytes);
5b93d8bb
AM
689 int offset = nbytes - size;
690
691 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size, &exp, 0, reloc);
692 }
693 }
694 else
695 emit_expr (&exp, (unsigned int) nbytes);
696 }
697 while (*input_line_pointer++ == ',');
698
92774660 699 input_line_pointer--; /* Put terminator back into stream. */
5b93d8bb
AM
700 demand_empty_rest_of_line ();
701}
702
703\f
704/* ASCII to EBCDIC conversion table. */
705static unsigned char ascebc[256] =
706{
707 /*00 NL SH SX EX ET NQ AK BL */
708 0x00, 0x01, 0x02, 0x03, 0x37, 0x2D, 0x2E, 0x2F,
709 /*08 BS HT LF VT FF CR SO SI */
710 0x16, 0x05, 0x15, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
711 /*10 DL D1 D2 D3 D4 NK SN EB */
712 0x10, 0x11, 0x12, 0x13, 0x3C, 0x3D, 0x32, 0x26,
713 /*18 CN EM SB EC FS GS RS US */
714 0x18, 0x19, 0x3F, 0x27, 0x1C, 0x1D, 0x1E, 0x1F,
715 /*20 SP ! " # $ % & ' */
716 0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D,
717 /*28 ( ) * + , - . / */
718 0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
719 /*30 0 1 2 3 4 5 6 7 */
720 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
721 /*38 8 9 : ; < = > ? */
722 0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
723 /*40 @ A B C D E F G */
724 0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
725 /*48 H I J K L M N O */
726 0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
727 /*50 P Q R S T U V W */
728 0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
729 /*58 X Y Z [ \ ] ^ _ */
730 0xE7, 0xE8, 0xE9, 0xAD, 0xE0, 0xBD, 0x5F, 0x6D,
731 /*60 ` a b c d e f g */
732 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
733 /*68 h i j k l m n o */
734 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
735 /*70 p q r s t u v w */
736 0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
737 /*78 x y z { | } ~ DL */
738 0xA7, 0xA8, 0xA9, 0xC0, 0x4F, 0xD0, 0xA1, 0x07,
739 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
740 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
741 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
742 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
743 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
744 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
745 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
746 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
747 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
748 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
749 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
750 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
751 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
752 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
753 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
754 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0xFF
755};
756
757/* EBCDIC to ASCII conversion table. */
758unsigned char ebcasc[256] =
759{
760 /*00 NU SH SX EX PF HT LC DL */
761 0x00, 0x01, 0x02, 0x03, 0x00, 0x09, 0x00, 0x7F,
762 /*08 SM VT FF CR SO SI */
763 0x00, 0x00, 0x00, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
764 /*10 DE D1 D2 TM RS NL BS IL */
765 0x10, 0x11, 0x12, 0x13, 0x14, 0x0A, 0x08, 0x00,
766 /*18 CN EM CC C1 FS GS RS US */
767 0x18, 0x19, 0x00, 0x00, 0x1C, 0x1D, 0x1E, 0x1F,
768 /*20 DS SS FS BP LF EB EC */
769 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x17, 0x1B,
770 /*28 SM C2 EQ AK BL */
771 0x00, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07, 0x00,
772 /*30 SY PN RS UC ET */
773 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
774 /*38 C3 D4 NK SU */
775 0x00, 0x00, 0x00, 0x00, 0x14, 0x15, 0x00, 0x1A,
776 /*40 SP */
777 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
778 /*48 . < ( + | */
779 0x00, 0x00, 0x00, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
780 /*50 & */
781 0x26, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
782 /*58 ! $ * ) ; ^ */
783 0x00, 0x00, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0x5E,
784 /*60 - / */
785 0x2D, 0x2F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
786 /*68 , % _ > ? */
787 0x00, 0x00, 0x00, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
788 /*70 */
789 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
790 /*78 ` : # @ ' = " */
791 0x00, 0x60, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
792 /*80 a b c d e f g */
793 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
794 /*88 h i { */
795 0x68, 0x69, 0x00, 0x7B, 0x00, 0x00, 0x00, 0x00,
796 /*90 j k l m n o p */
797 0x00, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70,
798 /*98 q r } */
799 0x71, 0x72, 0x00, 0x7D, 0x00, 0x00, 0x00, 0x00,
800 /*A0 ~ s t u v w x */
801 0x00, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
802 /*A8 y z [ */
803 0x79, 0x7A, 0x00, 0x00, 0x00, 0x5B, 0x00, 0x00,
804 /*B0 */
805 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
806 /*B8 ] */
807 0x00, 0x00, 0x00, 0x00, 0x00, 0x5D, 0x00, 0x00,
808 /*C0 { A B C D E F G */
809 0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
810 /*C8 H I */
811 0x48, 0x49, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
812 /*D0 } J K L M N O P */
813 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
814 /*D8 Q R */
815 0x51, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
816 /*E0 \ S T U V W X */
817 0x5C, 0x00, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
818 /*E8 Y Z */
819 0x59, 0x5A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
820 /*F0 0 1 2 3 4 5 6 7 */
821 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
822 /*F8 8 9 */
823 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF
824};
825
ea1562b3
NC
826/* EBCDIC translation tables needed for 3270 support. */
827
5b93d8bb 828static void
ea1562b3 829i370_ebcdic (int unused ATTRIBUTE_UNUSED)
5b93d8bb
AM
830{
831 char *p, *end;
832 char delim = 0;
833 size_t nbytes;
834
835 nbytes = strlen (input_line_pointer);
836 end = input_line_pointer + nbytes;
837 while ('\r' == *end) end --;
838 while ('\n' == *end) end --;
839
840 delim = *input_line_pointer;
ea1562b3
NC
841 if (('\'' == delim) || ('\"' == delim))
842 {
843 input_line_pointer ++;
844 end = rindex (input_line_pointer, delim);
845 }
5b93d8bb
AM
846
847 if (end > input_line_pointer)
848 {
849 nbytes = end - input_line_pointer +1;
850 p = frag_more (nbytes);
851 while (end > input_line_pointer)
852 {
bc805888 853 *p = ascebc [(unsigned char) (*input_line_pointer)];
5b93d8bb
AM
854 ++p; ++input_line_pointer;
855 }
856 *p = '\0';
857 }
858 if (delim == *input_line_pointer) ++input_line_pointer;
859}
860
861\f
ea1562b3
NC
862/* Stub out a couple of routines. */
863
5b93d8bb 864static void
ea1562b3 865i370_rmode (int unused ATTRIBUTE_UNUSED)
5b93d8bb
AM
866{
867 as_tsktsk ("rmode ignored");
868}
869
870static void
ea1562b3 871i370_dsect (int sect)
5b93d8bb
AM
872{
873 char *save_line = input_line_pointer;
874 static char section[] = ".data\n";
875
ea1562b3 876 /* Just pretend this is .section .data. */
5b93d8bb
AM
877 input_line_pointer = section;
878 obj_elf_section (sect);
879
880 input_line_pointer = save_line;
881}
882
883static void
ea1562b3 884i370_csect (int unused ATTRIBUTE_UNUSED)
5b93d8bb
AM
885{
886 as_tsktsk ("csect not supported");
887}
888
889\f
890/* DC Define Const is only partially supported.
ea1562b3
NC
891 For samplecode on what to do, look at i370_elf_cons() above.
892 This code handles pseudoops of the style
893 DC D'3.141592653' # in sysv4, .double 3.14159265
894 DC F'1' # in sysv4, .long 1. */
895
5b93d8bb 896static void
ea1562b3 897i370_dc (int unused ATTRIBUTE_UNUSED)
5b93d8bb
AM
898{
899 char * p, tmp[50];
900 int nbytes=0;
901 expressionS exp;
902 char type=0;
903
904 if (is_it_end_of_statement ())
905 {
906 demand_empty_rest_of_line ();
907 return;
908 }
909
ea1562b3 910 /* Figure out the size. */
5b93d8bb
AM
911 type = *input_line_pointer++;
912 switch (type)
913 {
914 case 'H': /* 16-bit */
915 nbytes = 2;
916 break;
917 case 'E': /* 32-bit */
918 case 'F': /* 32-bit */
919 nbytes = 4;
920 break;
921 case 'D': /* 64-bit */
922 nbytes = 8;
923 break;
924 default:
20203fb9 925 as_bad (_("unsupported DC type"));
5b93d8bb
AM
926 return;
927 }
928
ea1562b3 929 /* Get rid of pesky quotes. */
5b93d8bb
AM
930 if ('\'' == *input_line_pointer)
931 {
932 char * close;
ea1562b3 933
5b93d8bb
AM
934 ++input_line_pointer;
935 close = strchr (input_line_pointer, '\'');
936 if (close)
937 *close= ' ';
938 else
20203fb9 939 as_bad (_("missing end-quote"));
5b93d8bb 940 }
ea1562b3 941
5b93d8bb
AM
942 if ('\"' == *input_line_pointer)
943 {
944 char * close;
ea1562b3 945
5b93d8bb
AM
946 ++input_line_pointer;
947 close = strchr (input_line_pointer, '\"');
948 if (close)
949 *close= ' ';
950 else
20203fb9 951 as_bad (_("missing end-quote"));
5b93d8bb
AM
952 }
953
954 switch (type)
955 {
956 case 'H': /* 16-bit */
957 case 'F': /* 32-bit */
958 expression (&exp);
959 emit_expr (&exp, nbytes);
960 break;
961 case 'E': /* 32-bit */
499ac353 962 type = 'f';
5b93d8bb
AM
963 case 'D': /* 64-bit */
964 md_atof (type, tmp, &nbytes);
965 p = frag_more (nbytes);
966 memcpy (p, tmp, nbytes);
967 break;
968 default:
20203fb9 969 as_bad (_("unsupported DC type"));
5b93d8bb
AM
970 return;
971 }
972
973 demand_empty_rest_of_line ();
974}
975
976\f
ea1562b3
NC
977/* Provide minimal support for DS Define Storage. */
978
5b93d8bb 979static void
ea1562b3 980i370_ds (int unused ATTRIBUTE_UNUSED)
5b93d8bb 981{
ea1562b3 982 /* DS 0H or DS 0F or DS 0D. */
5b93d8bb
AM
983 if ('0' == *input_line_pointer)
984 {
ea1562b3 985 int alignment = 0; /* Left shift 1 << align. */
5b93d8bb
AM
986 input_line_pointer ++;
987 switch (*input_line_pointer++)
988 {
989 case 'H': /* 16-bit */
990 alignment = 1;
991 break;
992 case 'F': /* 32-bit */
993 alignment = 2;
994 break;
995 case 'D': /* 64-bit */
996 alignment = 3;
997 break;
998 default:
20203fb9 999 as_bad (_("unsupported alignment"));
5b93d8bb
AM
1000 return;
1001 }
1002 frag_align (alignment, 0, 0);
1003 record_alignment (now_seg, alignment);
1004 }
1005 else
20203fb9 1006 as_bad (_("this DS form not yet supported"));
5b93d8bb
AM
1007}
1008
1009/* Solaris pseudo op to change to the .rodata section. */
ea1562b3 1010
5b93d8bb 1011static void
ea1562b3 1012i370_elf_rdata (int sect)
5b93d8bb
AM
1013{
1014 char *save_line = input_line_pointer;
1015 static char section[] = ".rodata\n";
1016
ea1562b3 1017 /* Just pretend this is .section .rodata. */
5b93d8bb
AM
1018 input_line_pointer = section;
1019 obj_elf_section (sect);
1020
1021 input_line_pointer = save_line;
1022}
1023
ea1562b3
NC
1024/* Pseudo op to make file scope bss items. */
1025
5b93d8bb 1026static void
ea1562b3 1027i370_elf_lcomm (int unused ATTRIBUTE_UNUSED)
5b93d8bb 1028{
ea1562b3
NC
1029 char *name;
1030 char c;
1031 char *p;
5b93d8bb 1032 offsetT size;
ea1562b3 1033 symbolS *symbolP;
5b93d8bb
AM
1034 offsetT align;
1035 segT old_sec;
1036 int old_subsec;
1037 char *pfrag;
1038 int align2;
1039
1040 name = input_line_pointer;
1041 c = get_symbol_end ();
1042
ea1562b3 1043 /* Just after name is now '\0'. */
5b93d8bb
AM
1044 p = input_line_pointer;
1045 *p = c;
1046 SKIP_WHITESPACE ();
1047 if (*input_line_pointer != ',')
1048 {
20203fb9 1049 as_bad (_("Expected comma after symbol-name: rest of line ignored."));
5b93d8bb
AM
1050 ignore_rest_of_line ();
1051 return;
1052 }
1053
ea1562b3
NC
1054 /* Skip ','. */
1055 input_line_pointer++;
5b93d8bb
AM
1056 if ((size = get_absolute_expression ()) < 0)
1057 {
20203fb9 1058 as_warn (_(".COMMon length (%ld.) <0! Ignored."), (long) size);
5b93d8bb
AM
1059 ignore_rest_of_line ();
1060 return;
1061 }
1062
1063 /* The third argument to .lcomm is the alignment. */
1064 if (*input_line_pointer != ',')
1065 align = 8;
1066 else
1067 {
1068 ++input_line_pointer;
1069 align = get_absolute_expression ();
1070 if (align <= 0)
1071 {
20203fb9 1072 as_warn (_("ignoring bad alignment"));
5b93d8bb
AM
1073 align = 8;
1074 }
1075 }
1076
1077 *p = 0;
1078 symbolP = symbol_find_or_make (name);
1079 *p = c;
1080
1081 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
1082 {
20203fb9 1083 as_bad (_("Ignoring attempt to re-define symbol `%s'."),
5b93d8bb
AM
1084 S_GET_NAME (symbolP));
1085 ignore_rest_of_line ();
1086 return;
1087 }
1088
1089 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
1090 {
20203fb9 1091 as_bad (_("Length of .lcomm \"%s\" is already %ld. Not changed to %ld."),
5b93d8bb
AM
1092 S_GET_NAME (symbolP),
1093 (long) S_GET_VALUE (symbolP),
1094 (long) size);
1095
1096 ignore_rest_of_line ();
1097 return;
1098 }
1099
ea1562b3 1100 /* Allocate_bss: */
5b93d8bb
AM
1101 old_sec = now_seg;
1102 old_subsec = now_subseg;
1103 if (align)
1104 {
ea1562b3 1105 /* Convert to a power of 2 alignment. */
5b93d8bb
AM
1106 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2)
1107 ;
1108 if (align != 1)
1109 {
20203fb9 1110 as_bad (_("Common alignment not a power of 2"));
5b93d8bb
AM
1111 ignore_rest_of_line ();
1112 return;
1113 }
1114 }
1115 else
1116 align2 = 0;
1117
1118 record_alignment (bss_section, align2);
1119 subseg_set (bss_section, 0);
1120 if (align2)
1121 frag_align (align2, 0, 0);
1122 if (S_GET_SEGMENT (symbolP) == bss_section)
1123 symbol_get_frag (symbolP)->fr_symbol = 0;
1124 symbol_set_frag (symbolP, frag_now);
1125 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
1126 (char *) 0);
1127 *pfrag = 0;
1128 S_SET_SIZE (symbolP, size);
1129 S_SET_SEGMENT (symbolP, bss_section);
1130 subseg_set (old_sec, old_subsec);
1131 demand_empty_rest_of_line ();
1132}
1133
1134/* Validate any relocations emitted for -mrelocatable, possibly adding
1135 fixups for word relocations in writable segments, so we can adjust
1136 them at runtime. */
ea1562b3 1137
5b93d8bb 1138static void
ea1562b3 1139i370_elf_validate_fix (fixS *fixp, segT seg)
5b93d8bb
AM
1140{
1141 if (fixp->fx_done || fixp->fx_pcrel)
1142 return;
1143
1144 switch (shlib)
1145 {
1146 case SHLIB_NONE:
1147 case SHLIB_PIC:
1148 return;
1149
1150 case SHILB_MRELOCATABLE:
1151 if (fixp->fx_r_type <= BFD_RELOC_UNUSED
1152 && fixp->fx_r_type != BFD_RELOC_16_GOTOFF
1153 && fixp->fx_r_type != BFD_RELOC_HI16_GOTOFF
1154 && fixp->fx_r_type != BFD_RELOC_LO16_GOTOFF
1155 && fixp->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
1156 && fixp->fx_r_type != BFD_RELOC_32_BASEREL
1157 && fixp->fx_r_type != BFD_RELOC_LO16_BASEREL
1158 && fixp->fx_r_type != BFD_RELOC_HI16_BASEREL
1159 && fixp->fx_r_type != BFD_RELOC_HI16_S_BASEREL
1160 && strcmp (segment_name (seg), ".got2") != 0
1161 && strcmp (segment_name (seg), ".dtors") != 0
1162 && strcmp (segment_name (seg), ".ctors") != 0
1163 && strcmp (segment_name (seg), ".fixup") != 0
1164 && strcmp (segment_name (seg), ".stab") != 0
1165 && strcmp (segment_name (seg), ".gcc_except_table") != 0
1166 && strcmp (segment_name (seg), ".ex_shared") != 0)
1167 {
1168 if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
1169 || fixp->fx_r_type != BFD_RELOC_CTOR)
ea1562b3
NC
1170 as_bad_where (fixp->fx_file, fixp->fx_line,
1171 "Relocation cannot be done when using -mrelocatable");
5b93d8bb
AM
1172 }
1173 return;
ea1562b3
NC
1174 default:
1175 break;
5b93d8bb
AM
1176 }
1177}
1178#endif /* OBJ_ELF */
1179
5b93d8bb
AM
1180\f
1181#define LITERAL_POOL_SUPPORT
1182#ifdef LITERAL_POOL_SUPPORT
ea1562b3
NC
1183/* Provide support for literal pools within the text section.
1184 Loosely based on similar code from tc-arm.c.
1185 We will use four symbols to locate four parts of the literal pool.
1186 These four sections contain 64,32,16 and 8-bit constants; we use
1187 four sections so that all memory access can be appropriately aligned.
1188 That is, we want to avoid mixing these together so that we don't
1189 waste space padding out to alignments. The four pointers
1190 longlong_poolP, word_poolP, etc. point to a symbol labeling the
1191 start of each pool part.
1192
1193 lit_pool_num increments from zero to infinity and uniquely id's
1194 -- its used to generate the *_poolP symbol name. */
5b93d8bb
AM
1195
1196#define MAX_LITERAL_POOL_SIZE 1024
1197
1198typedef struct literalS
1199{
1200 struct expressionS exp;
1201 char * sym_name;
1202 char size; /* 1,2,4 or 8 */
1203 short offset;
1204} literalT;
1205
1206literalT literals[MAX_LITERAL_POOL_SIZE];
ea1562b3 1207int next_literal_pool_place = 0; /* Next free entry in the pool. */
5b93d8bb 1208
ea1562b3
NC
1209static symbolS *longlong_poolP = NULL; /* 64-bit pool entries. */
1210static symbolS *word_poolP = NULL; /* 32-bit pool entries. */
1211static symbolS *short_poolP = NULL; /* 16-bit pool entries. */
1212static symbolS *byte_poolP = NULL; /* 8-bit pool entries. */
5b93d8bb
AM
1213
1214static int lit_pool_num = 1;
1215
ea1562b3 1216/* Create a new, empty symbol. */
5b93d8bb
AM
1217static symbolS *
1218symbol_make_empty (void)
1219{
1220 return symbol_create (FAKE_LABEL_NAME, undefined_section,
1221 (valueT) 0, &zero_address_frag);
1222}
1223
ea1562b3
NC
1224/* Make the first argument an address-relative expression
1225 by subtracting the second argument. */
1226
1227static void
1228i370_make_relative (expressionS *exx, expressionS *baseaddr)
1229{
1230 if (O_constant == baseaddr->X_op)
1231 {
1232 exx->X_op = O_symbol;
1233 exx->X_add_number -= baseaddr->X_add_number;
1234 }
1235 else if (O_symbol == baseaddr->X_op)
1236 {
1237 exx->X_op = O_subtract;
1238 exx->X_op_symbol = baseaddr->X_add_symbol;
1239 exx->X_add_number -= baseaddr->X_add_number;
1240 }
1241 else if (O_uminus == baseaddr->X_op)
1242 {
1243 exx->X_op = O_add;
1244 exx->X_op_symbol = baseaddr->X_add_symbol;
1245 exx->X_add_number += baseaddr->X_add_number;
1246 }
1247 else
20203fb9 1248 as_bad (_("Missing or bad .using directive"));
ea1562b3
NC
1249}
1250/* Add an expression to the literal pool. */
1251
5b93d8bb
AM
1252static void
1253add_to_lit_pool (expressionS *exx, char *name, int sz)
1254{
1255 int lit_count = 0;
1256 int offset_in_pool = 0;
1257
ea1562b3 1258 /* Start a new pool, if necessary. */
5b93d8bb 1259 if (8 == sz && NULL == longlong_poolP)
07726851 1260 longlong_poolP = symbol_make_empty ();
5b93d8bb 1261 else if (4 == sz && NULL == word_poolP)
07726851 1262 word_poolP = symbol_make_empty ();
5b93d8bb 1263 else if (2 == sz && NULL == short_poolP)
07726851 1264 short_poolP = symbol_make_empty ();
5b93d8bb 1265 else if (1 == sz && NULL == byte_poolP)
07726851 1266 byte_poolP = symbol_make_empty ();
5b93d8bb 1267
ea1562b3
NC
1268 /* Check if this literal value is already in the pool.
1269 FIXME: We should probably be checking expressions
1270 of type O_symbol as well.
1271 FIXME: This is probably(certainly?) broken for O_big,
1272 which includes 64-bit long-longs. */
5b93d8bb
AM
1273 while (lit_count < next_literal_pool_place)
1274 {
1275 if (exx->X_op == O_constant
1276 && literals[lit_count].exp.X_op == exx->X_op
1277 && literals[lit_count].exp.X_add_number == exx->X_add_number
1278 && literals[lit_count].exp.X_unsigned == exx->X_unsigned
1279 && literals[lit_count].size == sz)
1280 break;
1281 else if (literals[lit_count].sym_name
1282 && name
1283 && !strcmp (name, literals[lit_count].sym_name))
1284 break;
1285 if (sz == literals[lit_count].size)
1286 offset_in_pool += sz;
1287 lit_count ++;
1288 }
1289
1290 if (lit_count == next_literal_pool_place) /* new entry */
1291 {
1292 if (next_literal_pool_place > MAX_LITERAL_POOL_SIZE)
20203fb9 1293 as_bad (_("Literal Pool Overflow"));
5b93d8bb
AM
1294
1295 literals[next_literal_pool_place].exp = *exx;
1296 literals[next_literal_pool_place].size = sz;
1297 literals[next_literal_pool_place].offset = offset_in_pool;
1298 if (name)
ea1562b3 1299 literals[next_literal_pool_place].sym_name = strdup (name);
5b93d8bb 1300 else
ea1562b3 1301 literals[next_literal_pool_place].sym_name = NULL;
5b93d8bb
AM
1302 next_literal_pool_place++;
1303 }
1304
0234cb7c 1305 /* ???_poolP points to the beginning of the literal pool.
ea1562b3
NC
1306 X_add_number is the offset from the beginning of the
1307 literal pool to this expr minus the location of the most
1308 recent .using directive. Thus, the grand total value of the
1309 expression is the distance from .using to the literal. */
5b93d8bb
AM
1310 if (8 == sz)
1311 exx->X_add_symbol = longlong_poolP;
1312 else if (4 == sz)
1313 exx->X_add_symbol = word_poolP;
1314 else if (2 == sz)
1315 exx->X_add_symbol = short_poolP;
1316 else if (1 == sz)
1317 exx->X_add_symbol = byte_poolP;
1318 exx->X_add_number = offset_in_pool;
1319 exx->X_op_symbol = NULL;
1320
1321 /* If the user has set up a base reg in another section,
ea1562b3 1322 use that; otherwise use the text section. */
5b93d8bb 1323 if (0 < i370_using_other_regno)
ea1562b3 1324 i370_make_relative (exx, &i370_using_other_baseaddr);
5b93d8bb 1325 else
ea1562b3 1326 i370_make_relative (exx, &i370_using_text_baseaddr);
5b93d8bb
AM
1327}
1328
1329/* The symbol setup for the literal pool is done in two steps. First,
ea1562b3
NC
1330 a symbol that represents the start of the literal pool is created,
1331 above, in the add_to_pool() routine. This sym ???_poolP.
1332 However, we don't know what fragment its in until a bit later.
1333 So we defer the frag_now thing, and the symbol name, until .ltorg time. */
5b93d8bb
AM
1334
1335/* Can't use symbol_new here, so have to create a symbol and then at
ea1562b3 1336 a later date assign it a value. Thats what these functions do. */
650f02e9 1337
5b93d8bb 1338static void
ea1562b3
NC
1339symbol_locate (symbolS *symbolP,
1340 const char *name, /* It is copied, the caller can modify. */
1341 segT segment, /* Segment identifier (SEG_<something>). */
1342 valueT valu, /* Symbol value. */
1343 fragS *frag) /* Associated fragment. */
5b93d8bb
AM
1344{
1345 size_t name_length;
1346 char *preserved_copy_of_name;
1347
1348 name_length = strlen (name) + 1; /* +1 for \0 */
1349 obstack_grow (&notes, name, name_length);
1350 preserved_copy_of_name = obstack_finish (&notes);
1351
1352 S_SET_NAME (symbolP, preserved_copy_of_name);
1353
1354 S_SET_SEGMENT (symbolP, segment);
1355 S_SET_VALUE (symbolP, valu);
07726851 1356 symbol_clear_list_pointers (symbolP);
5b93d8bb
AM
1357
1358 symbol_set_frag (symbolP, frag);
1359
ea1562b3 1360 /* Link to end of symbol chain. */
5b93d8bb
AM
1361 {
1362 extern int symbol_table_frozen;
ea1562b3 1363
5b93d8bb
AM
1364 if (symbol_table_frozen)
1365 abort ();
1366 }
1367
1368 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
1369
1370 obj_symbol_new_hook (symbolP);
1371
1372#ifdef tc_symbol_new_hook
1373 tc_symbol_new_hook (symbolP);
1374#endif
1375
1376#define DEBUG_SYMS
1377#ifdef DEBUG_SYMS
1378 verify_symbol_chain(symbol_rootP, symbol_lastP);
1379#endif /* DEBUG_SYMS */
1380}
1381
1382/* i370_addr_offset() will convert operand expressions
ea1562b3
NC
1383 that appear to be absolute into thier base-register
1384 relative form. These expressions come in two types:
1385
1386 (1) of the form "* + const" * where "*" means
1387 relative offset since the last using
1388 i.e. "*" means ".-using_baseaddr"
1389
1390 (2) labels, which are never absolute, but are always
1391 relative to the last "using". Anything with an alpha
1392 character is considered to be a label (since symbols
1393 can never be operands), and since we've already handled
1394 register operands. For example, "BL .L33" branch low
1395 to .L33 RX form insn frequently terminates for-loops. */
1396
b34976b6 1397static bfd_boolean
5b93d8bb
AM
1398i370_addr_offset (expressionS *exx)
1399{
1400 char *dot, *lab;
1401 int islabel = 0;
1402 int all_digits = 0;
1403
ea1562b3
NC
1404 /* Search for a label; anything with an alpha char will do.
1405 Local labels consist of N digits followed by either b or f. */
5b93d8bb
AM
1406 lab = input_line_pointer;
1407 while (*lab && (',' != *lab) && ('(' != *lab))
1408 {
3882b010 1409 if (ISDIGIT (*lab))
ea1562b3 1410 all_digits = 1;
3882b010 1411 else if (ISALPHA (*lab))
5b93d8bb
AM
1412 {
1413 if (!all_digits)
1414 {
1415 islabel = 1;
1416 break;
1417 }
1418 else if (('f' == *lab) || ('b' == *lab))
1419 {
1420 islabel = 1;
1421 break;
1422 }
1423 if (all_digits)
1424 break;
1425 }
1426 else if ('.' != *lab)
1427 break;
1428 ++lab;
1429 }
1430
ea1562b3 1431 /* See if operand has a * in it. */
5b93d8bb
AM
1432 dot = strchr (input_line_pointer, '*');
1433
1434 if (!dot && !islabel)
b34976b6 1435 return FALSE;
5b93d8bb 1436
ea1562b3 1437 /* Replace * with . and let expr munch on it. */
5b93d8bb
AM
1438 if (dot)
1439 *dot = '.';
1440 expression (exx);
1441
ea1562b3
NC
1442 /* OK, now we have to subtract the "using" location.
1443 Normally branches appear in the text section only. */
5b93d8bb 1444 if (0 == strncmp (now_seg->name, ".text", 5) || 0 > i370_using_other_regno)
ea1562b3 1445 i370_make_relative (exx, &i370_using_text_baseaddr);
5b93d8bb 1446 else
ea1562b3 1447 i370_make_relative (exx, &i370_using_other_baseaddr);
5b93d8bb 1448
ea1562b3 1449 /* Put the * back. */
5b93d8bb
AM
1450 if (dot)
1451 *dot = '*';
1452
b34976b6 1453 return TRUE;
5b93d8bb
AM
1454}
1455
ea1562b3 1456/* Handle address constants of various sorts. */
5b93d8bb 1457/* The currently supported types are
ea1562b3
NC
1458 =A(some_symb)
1459 =V(some_extern)
1460 =X'deadbeef' hexadecimal
1461 =F'1234' 32-bit const int
1462 =H'1234' 16-bit const int. */
1463
b34976b6 1464static bfd_boolean
5b93d8bb
AM
1465i370_addr_cons (expressionS *exp)
1466{
1467 char *name;
1468 char *sym_name, delim;
1469 int name_len;
ea1562b3
NC
1470 int hex_len = 0;
1471 int cons_len = 0;
5b93d8bb
AM
1472
1473 name = input_line_pointer;
1474 sym_name = input_line_pointer;
ea1562b3 1475 /* Find the spelling of the operand. */
3882b010 1476 if (name[0] == '=' && ISALPHA (name[1]))
ea1562b3 1477 name = ++input_line_pointer;
5b93d8bb 1478 else
ea1562b3
NC
1479 return FALSE;
1480
5b93d8bb
AM
1481 switch (name[0])
1482 {
ea1562b3
NC
1483 case 'A': /* A == address-of. */
1484 case 'V': /* V == extern. */
5b93d8bb
AM
1485 ++input_line_pointer;
1486 expression (exp);
1487
ea1562b3
NC
1488 /* We use a simple string name to collapse together
1489 multiple refrences to the same address literal. */
5b93d8bb
AM
1490 name_len = strcspn (sym_name, ", ");
1491 delim = *(sym_name + name_len);
1492 *(sym_name + name_len) = 0x0;
1493 add_to_lit_pool (exp, sym_name, 4);
1494 *(sym_name + name_len) = delim;
1495
1496 break;
1497 case 'H':
1498 case 'F':
1499 case 'X':
ea1562b3
NC
1500 case 'E': /* Single-precision float point. */
1501 case 'D': /* Double-precision float point. */
5b93d8bb 1502
ea1562b3
NC
1503 /* H == 16-bit fixed-point const; expression must be const. */
1504 /* F == fixed-point const; expression must be const. */
1505 /* X == fixed-point const; expression must be const. */
5b93d8bb
AM
1506 if ('H' == name[0]) cons_len = 2;
1507 else if ('F' == name[0]) cons_len = 4;
1508 else if ('X' == name[0]) cons_len = -1;
1509 else if ('E' == name[0]) cons_len = 4;
1510 else if ('D' == name[0]) cons_len = 8;
1511
ea1562b3
NC
1512 /* Extract length, if it is present;
1513 FIXME: assume single-digit length. */
5b93d8bb
AM
1514 if ('L' == name[1])
1515 {
ea1562b3
NC
1516 /* Should work for ASCII and EBCDIC. */
1517 cons_len = name[2] - '0';
5b93d8bb
AM
1518 input_line_pointer += 2;
1519 }
1520
1521 ++input_line_pointer;
1522
ea1562b3 1523 /* Get rid of pesky quotes. */
5b93d8bb
AM
1524 if ('\'' == *input_line_pointer)
1525 {
1526 char * close;
1527 ++input_line_pointer;
1528 close = strchr (input_line_pointer, '\'');
1529 if (close)
1530 *close= ' ';
1531 else
20203fb9 1532 as_bad (_("missing end-quote"));
5b93d8bb
AM
1533 }
1534 if ('\"' == *input_line_pointer)
1535 {
1536 char * close;
1537 ++input_line_pointer;
1538 close = strchr (input_line_pointer, '\"');
1539 if (close)
1540 *close= ' ';
1541 else
20203fb9 1542 as_bad (_("missing end-quote"));
5b93d8bb
AM
1543 }
1544 if (('X' == name[0]) || ('E' == name[0]) || ('D' == name[0]))
1545 {
1546 char tmp[50];
1547 char *save;
1548
1549 /* The length of hex constants is specified directly with L,
ea1562b3
NC
1550 or implied through the number of hex digits. For example:
1551 =X'AB' one byte
1552 =X'abcd' two bytes
1553 =X'000000AB' four bytes
1554 =XL4'AB' four bytes, left-padded withn zero. */
5b93d8bb
AM
1555 if (('X' == name[0]) && (0 > cons_len))
1556 {
1557 save = input_line_pointer;
1558 while (*save)
1559 {
3882b010 1560 if (ISXDIGIT (*save))
5b93d8bb
AM
1561 hex_len++;
1562 save++;
1563 }
1564 cons_len = (hex_len+1) /2;
1565 }
0234cb7c 1566 /* I believe this works even for =XL8'dada0000beeebaaa'
ea1562b3
NC
1567 which should parse out to X_op == O_big
1568 Note that floats and doubles get represented as
1569 0d3.14159265358979 or 0f 2.7. */
5b93d8bb
AM
1570 tmp[0] = '0';
1571 tmp[1] = name[0];
1572 tmp[2] = 0;
1573 strcat (tmp, input_line_pointer);
1574 save = input_line_pointer;
1575 input_line_pointer = tmp;
1576 expression (exp);
1577 input_line_pointer = save + (input_line_pointer-tmp-2);
1578
ea1562b3 1579 /* Fix up lengths for floats and doubles. */
5b93d8bb 1580 if (O_big == exp->X_op)
ea1562b3 1581 exp->X_add_number = cons_len / CHARS_PER_LITTLENUM;
5b93d8bb
AM
1582 }
1583 else
ea1562b3
NC
1584 expression (exp);
1585
1586 /* O_big occurs when more than 4 bytes worth gets parsed. */
5b93d8bb
AM
1587 if ((exp->X_op != O_constant) && (exp->X_op != O_big))
1588 {
20203fb9 1589 as_bad (_("expression not a constant"));
b34976b6 1590 return FALSE;
5b93d8bb
AM
1591 }
1592 add_to_lit_pool (exp, 0x0, cons_len);
1593 break;
1594
1595 default:
20203fb9 1596 as_bad (_("Unknown/unsupported address literal type"));
b34976b6 1597 return FALSE;
5b93d8bb
AM
1598 }
1599
b34976b6 1600 return TRUE;
5b93d8bb
AM
1601}
1602
1603\f
1604/* Dump the contents of the literal pool that we've accumulated so far.
ea1562b3 1605 This aligns the pool to the size of the largest literal in the pool. */
5b93d8bb
AM
1606
1607static void
ea1562b3 1608i370_ltorg (int ignore ATTRIBUTE_UNUSED)
5b93d8bb
AM
1609{
1610 int litsize;
1611 int lit_count = 0;
1612 int biggest_literal_size = 0;
1613 int biggest_align = 0;
1614 char pool_name[20];
1615
1616 if (strncmp (now_seg->name, ".text", 5))
1617 {
1618 if (i370_other_section == undefined_section)
20203fb9 1619 as_bad (_(".ltorg without prior .using in section %s"),
ea1562b3
NC
1620 now_seg->name);
1621
5b93d8bb 1622 if (i370_other_section != now_seg)
20203fb9 1623 as_bad (_(".ltorg in section %s paired to .using in section %s"),
ea1562b3 1624 now_seg->name, i370_other_section->name);
5b93d8bb 1625 }
ea1562b3 1626
5b93d8bb
AM
1627 if (! longlong_poolP
1628 && ! word_poolP
1629 && ! short_poolP
1630 && ! byte_poolP)
ea1562b3
NC
1631 /* Nothing to do. */
1632 return;
5b93d8bb 1633
ea1562b3 1634 /* Find largest literal .. 2 4 or 8. */
5b93d8bb
AM
1635 lit_count = 0;
1636 while (lit_count < next_literal_pool_place)
1637 {
1638 if (biggest_literal_size < literals[lit_count].size)
1639 biggest_literal_size = literals[lit_count].size;
1640 lit_count ++;
1641 }
1642 if (1 == biggest_literal_size) biggest_align = 0;
1643 else if (2 == biggest_literal_size) biggest_align = 1;
1644 else if (4 == biggest_literal_size) biggest_align = 2;
1645 else if (8 == biggest_literal_size) biggest_align = 3;
20203fb9 1646 else as_bad (_("bad alignment of %d bytes in literal pool"), biggest_literal_size);
5b93d8bb
AM
1647 if (0 == biggest_align) biggest_align = 1;
1648
ea1562b3 1649 /* Align pool for short, word, double word accesses. */
5b93d8bb
AM
1650 frag_align (biggest_align, 0, 0);
1651 record_alignment (now_seg, biggest_align);
1652
1653 /* Note that the gas listing will print only the first five
ea1562b3
NC
1654 entries in the pool .... wonder how to make it print more. */
1655 /* Output largest literals first, then the smaller ones. */
5b93d8bb
AM
1656 for (litsize=8; litsize; litsize /=2)
1657 {
1658 symbolS *current_poolP = NULL;
1659 switch (litsize)
1660 {
1661 case 8:
1662 current_poolP = longlong_poolP; break;
1663 case 4:
1664 current_poolP = word_poolP; break;
1665 case 2:
1666 current_poolP = short_poolP; break;
1667 case 1:
1668 current_poolP = byte_poolP; break;
1669 default:
20203fb9 1670 as_bad (_("bad literal size\n"));
5b93d8bb
AM
1671 }
1672 if (NULL == current_poolP)
1673 continue;
1674 sprintf (pool_name, ".LITP%01d%06d", litsize, lit_pool_num);
1675 symbol_locate (current_poolP, pool_name, now_seg,
1676 (valueT) frag_now_fix (), frag_now);
1677 symbol_table_insert (current_poolP);
1678
1679 lit_count = 0;
1680 while (lit_count < next_literal_pool_place)
1681 {
1682 if (litsize == literals[lit_count].size)
1683 {
1684#define EMIT_ADDR_CONS_SYMBOLS
1685#ifdef EMIT_ADDR_CONS_SYMBOLS
ea1562b3
NC
1686 /* Create a bogus symbol, add it to the pool ...
1687 For the most part, I think this is a useless exercise,
1688 except that having these symbol names in the objects
1689 is vaguely useful for debugging. */
5b93d8bb
AM
1690 if (literals[lit_count].sym_name)
1691 {
07726851 1692 symbolS * symP = symbol_make_empty ();
5b93d8bb
AM
1693 symbol_locate (symP, literals[lit_count].sym_name, now_seg,
1694 (valueT) frag_now_fix (), frag_now);
1695 symbol_table_insert (symP);
1696 }
1697#endif /* EMIT_ADDR_CONS_SYMBOLS */
1698
1699 emit_expr (&(literals[lit_count].exp), literals[lit_count].size);
1700 }
1701 lit_count ++;
1702 }
1703 }
1704
1705 next_literal_pool_place = 0;
1706 longlong_poolP = NULL;
1707 word_poolP = NULL;
1708 short_poolP = NULL;
1709 byte_poolP = NULL;
1710 lit_pool_num++;
1711}
1712
1713#endif /* LITERAL_POOL_SUPPORT */
1714
1715\f
ea1562b3
NC
1716/* Add support for the HLASM-like USING directive to indicate
1717 the base register to use ... we don't support the full
1718 hlasm semantics for this ... we merely pluck a base address
1719 and a register number out. We print a warning if using is
1720 called multiple times. I suppose we should check to see
1721 if the regno is valid. */
1722
5b93d8bb 1723static void
ea1562b3 1724i370_using (int ignore ATTRIBUTE_UNUSED)
5b93d8bb
AM
1725{
1726 expressionS ex, baseaddr;
1727 int iregno;
1728 char *star;
1729
ea1562b3
NC
1730 /* If "*" appears in a using, it means "."
1731 replace it with "." so that expr doesn't get confused. */
5b93d8bb
AM
1732 star = strchr (input_line_pointer, '*');
1733 if (star)
1734 *star = '.';
1735
ea1562b3
NC
1736 /* The first arg to using will usually be ".", but it can
1737 be a more complex expression too. */
5b93d8bb
AM
1738 expression (&baseaddr);
1739 if (star)
1740 *star = '*';
1741 if (O_constant != baseaddr.X_op
1742 && O_symbol != baseaddr.X_op
1743 && O_uminus != baseaddr.X_op)
20203fb9 1744 as_bad (_(".using: base address expression illegal or too complex"));
5b93d8bb
AM
1745
1746 if (*input_line_pointer != '\0') ++input_line_pointer;
1747
ea1562b3 1748 /* The second arg to using had better be a register. */
5b93d8bb
AM
1749 register_name (&ex);
1750 demand_empty_rest_of_line ();
1751 iregno = ex.X_add_number;
1752
1753 if (0 == strncmp (now_seg->name, ".text", 5))
1754 {
1755 i370_using_text_baseaddr = baseaddr;
1756 i370_using_text_regno = iregno;
1757 }
1758 else
1759 {
1760 i370_using_other_baseaddr = baseaddr;
1761 i370_using_other_regno = iregno;
1762 i370_other_section = now_seg;
1763 }
1764}
1765
1766static void
ea1562b3 1767i370_drop (int ignore ATTRIBUTE_UNUSED)
5b93d8bb
AM
1768{
1769 expressionS ex;
1770 int iregno;
1771
1772 register_name (&ex);
1773 demand_empty_rest_of_line ();
1774 iregno = ex.X_add_number;
1775
1776 if (0 == strncmp (now_seg->name, ".text", 5))
1777 {
1778 if (iregno != i370_using_text_regno)
20203fb9 1779 as_bad (_("droping register %d in section %s does not match using register %d"),
ea1562b3
NC
1780 iregno, now_seg->name, i370_using_text_regno);
1781
5b93d8bb
AM
1782 i370_using_text_regno = -1;
1783 i370_using_text_baseaddr.X_op = O_absent;
1784 }
1785 else
1786 {
1787 if (iregno != i370_using_other_regno)
20203fb9 1788 as_bad (_("droping register %d in section %s does not match using register %d"),
ea1562b3
NC
1789 iregno, now_seg->name, i370_using_other_regno);
1790
5b93d8bb 1791 if (i370_other_section != now_seg)
20203fb9 1792 as_bad (_("droping register %d in section %s previously used in section %s"),
ea1562b3
NC
1793 iregno, now_seg->name, i370_other_section->name);
1794
5b93d8bb
AM
1795 i370_using_other_regno = -1;
1796 i370_using_other_baseaddr.X_op = O_absent;
1797 i370_other_section = undefined_section;
1798 }
1799}
1800
5b93d8bb
AM
1801\f
1802/* We need to keep a list of fixups. We can't simply generate them as
1803 we go, because that would require us to first create the frag, and
1804 that would screw up references to ``.''. */
1805
1806struct i370_fixup
1807{
1808 expressionS exp;
1809 int opindex;
1810 bfd_reloc_code_real_type reloc;
1811};
1812
ea1562b3
NC
1813#define MAX_INSN_FIXUPS 5
1814
1815/* Handle a macro. Gather all the operands, transform them as
1816 described by the macro, and call md_assemble recursively. All the
1817 operands are separated by commas; we don't accept parentheses
1818 around operands here. */
1819
1820static void
1821i370_macro (char *str, const struct i370_macro *macro)
1822{
1823 char *operands[10];
1824 unsigned int count;
1825 char *s;
1826 unsigned int len;
1827 const char *format;
1828 int arg;
1829 char *send;
1830 char *complete;
1831
1832 /* Gather the users operands into the operands array. */
1833 count = 0;
1834 s = str;
1835 while (1)
1836 {
1837 if (count >= sizeof operands / sizeof operands[0])
1838 break;
1839 operands[count++] = s;
1840 s = strchr (s, ',');
1841 if (s == (char *) NULL)
1842 break;
1843 *s++ = '\0';
1844 }
1845
1846 if (count != macro->operands)
1847 {
20203fb9 1848 as_bad (_("wrong number of operands"));
ea1562b3
NC
1849 return;
1850 }
1851
1852 /* Work out how large the string must be (the size is unbounded
1853 because it includes user input). */
1854 len = 0;
1855 format = macro->format;
1856 while (*format != '\0')
1857 {
1858 if (*format != '%')
1859 {
1860 ++len;
1861 ++format;
1862 }
1863 else
1864 {
1865 arg = strtol (format + 1, &send, 10);
1866 know (send != format && arg >= 0 && (unsigned) arg < count);
1867 len += strlen (operands[arg]);
1868 format = send;
1869 }
1870 }
1871
1872 /* Put the string together. */
1873 complete = s = alloca (len + 1);
1874 format = macro->format;
1875 while (*format != '\0')
1876 {
1877 if (*format != '%')
1878 *s++ = *format++;
1879 else
1880 {
1881 arg = strtol (format + 1, &send, 10);
1882 strcpy (s, operands[arg]);
1883 s += strlen (s);
1884 format = send;
1885 }
1886 }
1887 *s = '\0';
1888
1889 /* Assemble the constructed instruction. */
1890 md_assemble (complete);
1891}
5b93d8bb
AM
1892
1893/* This routine is called for each instruction to be assembled. */
1894
1895void
ea1562b3 1896md_assemble (char *str)
5b93d8bb
AM
1897{
1898 char *s, *opcode_str;
1899 const struct i370_opcode *opcode;
1900 i370_insn_t insn;
1901 const unsigned char *opindex_ptr;
1902 int have_optional_index, have_optional_basereg, have_optional_reg;
1903 int skip_optional_index, skip_optional_basereg, skip_optional_reg;
1904 int use_text=0, use_other=0;
1905 int off_by_one;
1906 struct i370_fixup fixups[MAX_INSN_FIXUPS];
1907 int fc;
1908 char *f;
1909 int i;
1910#ifdef OBJ_ELF
1911 bfd_reloc_code_real_type reloc;
1912#endif
1913
1914 /* Get the opcode. */
3882b010 1915 for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
5b93d8bb
AM
1916 ;
1917 if (*s != '\0')
1918 *s++ = '\0';
1919 opcode_str = str;
1920
1921 /* Look up the opcode in the hash table. */
1922 opcode = (const struct i370_opcode *) hash_find (i370_hash, str);
1923 if (opcode == (const struct i370_opcode *) NULL)
1924 {
1925 const struct i370_macro *macro;
1926
9c2799c2 1927 gas_assert (i370_macro_hash);
5b93d8bb
AM
1928 macro = (const struct i370_macro *) hash_find (i370_macro_hash, str);
1929 if (macro == (const struct i370_macro *) NULL)
20203fb9 1930 as_bad (_("Unrecognized opcode: `%s'"), str);
5b93d8bb
AM
1931 else
1932 i370_macro (s, macro);
1933
1934 return;
1935 }
1936
1937 insn = opcode->opcode;
1938
1939 str = s;
3882b010 1940 while (ISSPACE (*str))
5b93d8bb
AM
1941 ++str;
1942
1943 /* I370 operands are either expressions or address constants.
1944 Many operand types are optional. The optional operands
1945 are always surrounded by parens, and are used to denote the base
1946 register ... e.g. "A R1, D2" or "A R1, D2(,B2) as opposed to
1947 the fully-formed "A R1, D2(X2,B2)". Note also the = sign,
1948 such as A R1,=A(i) where the address-of operator =A implies
1949 use of both a base register, and a missing index register.
1950
1951 So, before we start seriously parsing the operands, we check
1952 to see if we have an optional operand, and, if we do, we count
1953 the number of commas to see which operand should be omitted. */
1954
1955 have_optional_index = have_optional_basereg = have_optional_reg = 0;
1956 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1957 {
1958 const struct i370_operand *operand;
ea1562b3 1959
5b93d8bb
AM
1960 operand = &i370_operands[*opindex_ptr];
1961 if ((operand->flags & I370_OPERAND_INDEX) != 0)
1962 have_optional_index = 1;
1963 if ((operand->flags & I370_OPERAND_BASE) != 0)
1964 have_optional_basereg = 1;
1965 if ((operand->flags & I370_OPERAND_OPTIONAL) != 0)
1966 have_optional_reg = 1;
1967 }
1968
1969 skip_optional_index = skip_optional_basereg = skip_optional_reg = 0;
1970 if (have_optional_index || have_optional_basereg)
1971 {
1972 unsigned int opcount, nwanted;
1973
1974 /* There is an optional operand. Count the number of
1975 commas and open-parens in the input line. */
1976 if (*str == '\0')
1977 opcount = 0;
1978 else
1979 {
1980 opcount = 1;
1981 s = str;
1982 while ((s = strpbrk (s, ",(=")) != (char *) NULL)
1983 {
1984 ++opcount;
1985 ++s;
1986 if (',' == *s) ++s; /* avoid counting things like (, */
1987 if ('=' == *s) { ++s; --opcount; }
1988 }
1989 }
1990
1991 /* If there are fewer operands in the line then are called
1992 for by the instruction, we want to skip the optional
1993 operand. */
2132e3a3 1994 nwanted = strlen ((char *) opcode->operands);
5b93d8bb
AM
1995 if (have_optional_index)
1996 {
1997 if (opcount < nwanted)
1998 skip_optional_index = 1;
1999 if (have_optional_basereg && ((opcount+1) < nwanted))
2000 skip_optional_basereg = 1;
2001 if (have_optional_reg && ((opcount+1) < nwanted))
2002 skip_optional_reg = 1;
2003 }
2004 else
2005 {
2006 if (have_optional_basereg && (opcount < nwanted))
2007 skip_optional_basereg = 1;
2008 if (have_optional_reg && (opcount < nwanted))
2009 skip_optional_reg = 1;
2010 }
2011 }
2012
2013 /* Perform some off-by-one hacks on the length field of certain instructions.
ea1562b3
NC
2014 Its such a shame to have to do this, but the problem is that HLASM got
2015 defined so that the lengths differ by one from the actual machine instructions.
2016 this code should probably be moved to a special inster-operand routine.
2017 Sigh. Affected instructions are Compare Logical, Move and Exclusive OR
2018 hack alert -- aren't *all* SS instructions affected ?? */
5b93d8bb
AM
2019 off_by_one = 0;
2020 if (0 == strcasecmp ("CLC", opcode->name)
2021 || 0 == strcasecmp ("ED", opcode->name)
2022 || 0 == strcasecmp ("EDMK", opcode->name)
2023 || 0 == strcasecmp ("MVC", opcode->name)
2024 || 0 == strcasecmp ("MVCIN", opcode->name)
2025 || 0 == strcasecmp ("MVN", opcode->name)
2026 || 0 == strcasecmp ("MVZ", opcode->name)
2027 || 0 == strcasecmp ("NC", opcode->name)
2028 || 0 == strcasecmp ("OC", opcode->name)
2029 || 0 == strcasecmp ("XC", opcode->name))
2030 off_by_one = 1;
2031
2032 /* Gather the operands. */
2033 fc = 0;
2034 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
2035 {
2036 const struct i370_operand *operand;
2037 const char *errmsg;
2038 char *hold;
2039 expressionS ex;
2040
2041 operand = &i370_operands[*opindex_ptr];
2042 errmsg = NULL;
2043
2044 /* If this is an index operand, and we are skipping it,
2045 just insert a zero. */
2046 if (skip_optional_index &&
2047 ((operand->flags & I370_OPERAND_INDEX) != 0))
2048 {
2049 insn = i370_insert_operand (insn, operand, 0);
2050 continue;
2051 }
2052
2053 /* If this is the base operand, and we are skipping it,
2054 just insert the current using basreg. */
2055 if (skip_optional_basereg &&
2056 ((operand->flags & I370_OPERAND_BASE) != 0))
2057 {
2058 int basereg = -1;
2059 if (use_text)
2060 {
2061 if (0 == strncmp (now_seg->name, ".text", 5)
2062 || 0 > i370_using_other_regno)
ea1562b3 2063 basereg = i370_using_text_regno;
5b93d8bb 2064 else
ea1562b3 2065 basereg = i370_using_other_regno;
5b93d8bb
AM
2066 }
2067 else if (use_other)
2068 {
2069 if (0 > i370_using_other_regno)
ea1562b3 2070 basereg = i370_using_text_regno;
5b93d8bb 2071 else
ea1562b3 2072 basereg = i370_using_other_regno;
5b93d8bb
AM
2073 }
2074 if (0 > basereg)
20203fb9 2075 as_bad (_("not using any base register"));
ea1562b3 2076
5b93d8bb
AM
2077 insn = i370_insert_operand (insn, operand, basereg);
2078 continue;
2079 }
2080
2081 /* If this is an optional operand, and we are skipping it,
2082 Use zero (since a non-zero value would denote a register) */
2083 if (skip_optional_reg
2084 && ((operand->flags & I370_OPERAND_OPTIONAL) != 0))
2085 {
2086 insn = i370_insert_operand (insn, operand, 0);
2087 continue;
2088 }
2089
2090 /* Gather the operand. */
2091 hold = input_line_pointer;
2092 input_line_pointer = str;
2093
ea1562b3 2094 /* Register names are only allowed where there are registers. */
5b93d8bb
AM
2095 if ((operand->flags & I370_OPERAND_GPR) != 0)
2096 {
ea1562b3 2097 /* Quickie hack to get past things like (,r13). */
5b93d8bb
AM
2098 if (skip_optional_index && (',' == *input_line_pointer))
2099 {
2100 *input_line_pointer = ' ';
2101 input_line_pointer ++;
2102 }
ea1562b3 2103
5b93d8bb 2104 if (! register_name (&ex))
20203fb9 2105 as_bad (_("expecting a register for operand %d"),
8ad7c533 2106 (int) (opindex_ptr - opcode->operands + 1));
5b93d8bb
AM
2107 }
2108
b6ff326e 2109 /* Check for an address constant expression. */
5b93d8bb 2110 /* We will put PSW-relative addresses in the text section,
ea1562b3 2111 and address literals in the .data (or other) section. */
5b93d8bb 2112 else if (i370_addr_cons (&ex))
ea1562b3 2113 use_other = 1;
5b93d8bb 2114 else if (i370_addr_offset (&ex))
ea1562b3 2115 use_text = 1;
5b93d8bb
AM
2116 else expression (&ex);
2117
2118 str = input_line_pointer;
2119 input_line_pointer = hold;
2120
ea1562b3
NC
2121 /* Perform some off-by-one hacks on the length field of certain instructions.
2122 Its such a shame to have to do this, but the problem is that HLASM got
2123 defined so that the programmer specifies a length that is one greater
2124 than what the machine instruction wants. Sigh. */
5b93d8bb 2125 if (off_by_one && (0 == strcasecmp ("SS L", operand->name)))
ea1562b3 2126 ex.X_add_number --;
5b93d8bb
AM
2127
2128 if (ex.X_op == O_illegal)
20203fb9 2129 as_bad (_("illegal operand"));
5b93d8bb 2130 else if (ex.X_op == O_absent)
20203fb9 2131 as_bad (_("missing operand"));
5b93d8bb 2132 else if (ex.X_op == O_register)
ea1562b3 2133 insn = i370_insert_operand (insn, operand, ex.X_add_number);
5b93d8bb
AM
2134 else if (ex.X_op == O_constant)
2135 {
2136#ifdef OBJ_ELF
2137 /* Allow @HA, @L, @H on constants.
ea1562b3
NC
2138 Well actually, no we don't; there really don't make sense
2139 (at least not to me) for the i370. However, this code is
2140 left here for any dubious future expansion reasons. */
5b93d8bb
AM
2141 char *orig_str = str;
2142
2143 if ((reloc = i370_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
2144 switch (reloc)
2145 {
2146 default:
2147 str = orig_str;
2148 break;
2149
2150 case BFD_RELOC_LO16:
2151 /* X_unsigned is the default, so if the user has done
2152 something which cleared it, we always produce a
2153 signed value. */
2154 ex.X_add_number = (((ex.X_add_number & 0xffff)
2155 ^ 0x8000)
2156 - 0x8000);
2157 break;
2158
2159 case BFD_RELOC_HI16:
2160 ex.X_add_number = (ex.X_add_number >> 16) & 0xffff;
2161 break;
2162
2163 case BFD_RELOC_HI16_S:
2164 ex.X_add_number = (((ex.X_add_number >> 16) & 0xffff)
2165 + ((ex.X_add_number >> 15) & 1));
2166 break;
2167 }
2168#endif
2169 insn = i370_insert_operand (insn, operand, ex.X_add_number);
2170 }
2171#ifdef OBJ_ELF
2172 else if ((reloc = i370_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
2173 {
2174 as_tsktsk ("md_assemble(): suffixed relocations not supported\n");
2175
2176 /* We need to generate a fixup for this expression. */
2177 if (fc >= MAX_INSN_FIXUPS)
2178 as_fatal ("too many fixups");
2179 fixups[fc].exp = ex;
2180 fixups[fc].opindex = 0;
2181 fixups[fc].reloc = reloc;
2182 ++fc;
2183 }
2184#endif /* OBJ_ELF */
5b93d8bb
AM
2185 else
2186 {
2187 /* We need to generate a fixup for this expression. */
2188 /* Typically, the expression will just be a symbol ...
ea1562b3
NC
2189 printf ("insn %s needs fixup for %s \n",
2190 opcode->name, ex.X_add_symbol->bsym->name); */
5b93d8bb
AM
2191
2192 if (fc >= MAX_INSN_FIXUPS)
2193 as_fatal ("too many fixups");
2194 fixups[fc].exp = ex;
2195 fixups[fc].opindex = *opindex_ptr;
2196 fixups[fc].reloc = BFD_RELOC_UNUSED;
2197 ++fc;
2198 }
2199
ea1562b3 2200 /* Skip over delimiter (close paren, or comma). */
5b93d8bb
AM
2201 if ((')' == *str) && (',' == *(str+1)))
2202 ++str;
2203 if (*str != '\0')
2204 ++str;
2205 }
2206
3882b010 2207 while (ISSPACE (*str))
5b93d8bb
AM
2208 ++str;
2209
2210 if (*str != '\0')
20203fb9 2211 as_bad (_("junk at end of line: `%s'"), str);
5b93d8bb
AM
2212
2213 /* Write out the instruction. */
2214 f = frag_more (opcode->len);
2215 if (4 >= opcode->len)
ea1562b3 2216 md_number_to_chars (f, insn.i[0], opcode->len);
5b93d8bb
AM
2217 else
2218 {
2219 md_number_to_chars (f, insn.i[0], 4);
ea1562b3 2220
5b93d8bb 2221 if (6 == opcode->len)
ea1562b3 2222 md_number_to_chars ((f + 4), ((insn.i[1])>>16), 2);
5b93d8bb
AM
2223 else
2224 {
ea1562b3 2225 /* Not used --- don't have any 8 byte instructions. */
20203fb9 2226 as_bad (_("Internal Error: bad instruction length"));
ea1562b3 2227 md_number_to_chars ((f + 4), insn.i[1], opcode->len -4);
5b93d8bb
AM
2228 }
2229 }
2230
2231 /* Create any fixups. At this point we do not use a
2232 bfd_reloc_code_real_type, but instead just use the
2233 BFD_RELOC_UNUSED plus the operand index. This lets us easily
2234 handle fixups for any operand type, although that is admittedly
2235 not a very exciting feature. We pick a BFD reloc type in
55cf6793 2236 md_apply_fix. */
5b93d8bb
AM
2237 for (i = 0; i < fc; i++)
2238 {
2239 const struct i370_operand *operand;
2240
2241 operand = &i370_operands[fixups[i].opindex];
2242 if (fixups[i].reloc != BFD_RELOC_UNUSED)
2243 {
2244 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
2245 int size;
2246 fixS *fixP;
2247
2248 if (!reloc_howto)
2249 abort ();
2250
2251 size = bfd_get_reloc_size (reloc_howto);
2252
2253 if (size < 1 || size > 4)
bc805888 2254 abort ();
5b93d8bb
AM
2255
2256 printf (" gwana doo fixup %d \n", i);
2257 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal, size,
2258 &fixups[i].exp, reloc_howto->pc_relative,
2259 fixups[i].reloc);
2260
2261 /* Turn off complaints that the addend is too large for things like
2262 foo+100000@ha. */
2263 switch (fixups[i].reloc)
2264 {
2265 case BFD_RELOC_16_GOTOFF:
2266 case BFD_RELOC_LO16:
2267 case BFD_RELOC_HI16:
2268 case BFD_RELOC_HI16_S:
2269 fixP->fx_no_overflow = 1;
2270 break;
2271 default:
2272 break;
2273 }
2274 }
2275 else
2276 {
2277 fix_new_exp (frag_now, f - frag_now->fr_literal, opcode->len,
2278 &fixups[i].exp,
2279 (operand->flags & I370_OPERAND_RELATIVE) != 0,
2280 ((bfd_reloc_code_real_type)
2281 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
2282 }
2283 }
2284}
2285
5b93d8bb
AM
2286\f
2287/* Pseudo-op handling. */
2288
2289/* The .byte pseudo-op. This is similar to the normal .byte
2290 pseudo-op, but it can also take a single ASCII string. */
2291
2292static void
ea1562b3 2293i370_byte (int ignore ATTRIBUTE_UNUSED)
5b93d8bb
AM
2294{
2295 if (*input_line_pointer != '\"')
2296 {
2297 cons (1);
2298 return;
2299 }
2300
2301 /* Gather characters. A real double quote is doubled. Unusual
2302 characters are not permitted. */
2303 ++input_line_pointer;
2304 while (1)
2305 {
2306 char c;
2307
2308 c = *input_line_pointer++;
2309
2310 if (c == '\"')
2311 {
2312 if (*input_line_pointer != '\"')
2313 break;
2314 ++input_line_pointer;
2315 }
2316
2317 FRAG_APPEND_1_CHAR (c);
2318 }
2319
2320 demand_empty_rest_of_line ();
2321}
2322\f
2323/* The .tc pseudo-op. This is used when generating XCOFF and ELF.
2324 This takes two or more arguments.
2325
2326 When generating XCOFF output, the first argument is the name to
2327 give to this location in the toc; this will be a symbol with class
2328 TC. The rest of the arguments are 4 byte values to actually put at
2329 this location in the TOC; often there is just one more argument, a
1049f94e 2330 relocatable symbol reference.
5b93d8bb
AM
2331
2332 When not generating XCOFF output, the arguments are the same, but
2333 the first argument is simply ignored. */
2334
2335static void
ea1562b3 2336i370_tc (int ignore ATTRIBUTE_UNUSED)
5b93d8bb
AM
2337{
2338
2339 /* Skip the TOC symbol name. */
2340 while (is_part_of_name (*input_line_pointer)
2341 || *input_line_pointer == '['
2342 || *input_line_pointer == ']'
2343 || *input_line_pointer == '{'
2344 || *input_line_pointer == '}')
2345 ++input_line_pointer;
2346
2347 /* Align to a four byte boundary. */
2348 frag_align (2, 0, 0);
2349 record_alignment (now_seg, 2);
2350
2351 if (*input_line_pointer != ',')
2352 demand_empty_rest_of_line ();
2353 else
2354 {
2355 ++input_line_pointer;
2356 cons (4);
2357 }
2358}
2359\f
5b93d8bb 2360char *
ea1562b3 2361md_atof (int type, char *litp, int *sizep)
5b93d8bb 2362{
5b93d8bb 2363 /* 360/370/390 have two float formats: an old, funky 360 single-precision
499ac353
NC
2364 format, and the ieee format. Support only the ieee format. */
2365 return ieee_md_atof (type, litp, sizep, TRUE);
5b93d8bb
AM
2366}
2367
2368/* Write a value out to the object file, using the appropriate
2369 endianness. */
2370
2371void
ea1562b3 2372md_number_to_chars (char *buf, valueT val, int n)
5b93d8bb 2373{
ea1562b3 2374 number_to_chars_bigendian (buf, val, n);
5b93d8bb
AM
2375}
2376
2377/* Align a section (I don't know why this is machine dependent). */
2378
2379valueT
ea1562b3 2380md_section_align (asection *seg, valueT addr)
5b93d8bb
AM
2381{
2382 int align = bfd_get_section_alignment (stdoutput, seg);
2383
2384 return (addr + (1 << align) - 1) & (-1 << align);
2385}
2386
2387/* We don't have any form of relaxing. */
2388
2389int
ea1562b3
NC
2390md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
2391 asection *seg ATTRIBUTE_UNUSED)
5b93d8bb
AM
2392{
2393 abort ();
2394 return 0;
2395}
2396
2397/* Convert a machine dependent frag. We never generate these. */
2398
2399void
ea1562b3
NC
2400md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
2401 asection *sec ATTRIBUTE_UNUSED,
2402 fragS *fragp ATTRIBUTE_UNUSED)
5b93d8bb
AM
2403{
2404 abort ();
2405}
2406
2407/* We have no need to default values of symbols. */
2408
5b93d8bb 2409symbolS *
ea1562b3 2410md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
5b93d8bb
AM
2411{
2412 return 0;
2413}
2414\f
2415/* Functions concerning relocs. */
2416
2417/* The location from which a PC relative jump should be calculated,
2418 given a PC relative reloc. */
2419
2420long
ea1562b3 2421md_pcrel_from_section (fixS *fixp, segT sec ATTRIBUTE_UNUSED)
5b93d8bb
AM
2422{
2423 return fixp->fx_frag->fr_address + fixp->fx_where;
2424}
2425
5b93d8bb
AM
2426/* Apply a fixup to the object code. This is called for all the
2427 fixups we generated by the call to fix_new_exp, above. In the call
2428 above we used a reloc code which was the largest legal reloc code
2429 plus the operand index. Here we undo that to recover the operand
2430 index. At this point all symbol values should be fully resolved,
2431 and we attempt to completely resolve the reloc. If we can not do
2432 that, we determine the correct reloc code and put it back in the
2433 fixup.
2434
2435 See gas/cgen.c for more sample code and explanations of what's
ea1562b3 2436 going on here. */
5b93d8bb 2437
94f592af 2438void
55cf6793 2439md_apply_fix (fixS *fixP, valueT * valP, segT seg)
5b93d8bb 2440{
94f592af 2441 valueT value = * valP;
5b93d8bb 2442
94f592af 2443 if (fixP->fx_addsy != NULL)
5b93d8bb 2444 {
5b93d8bb 2445#ifdef DEBUG
55cf6793 2446 printf ("\nmd_apply_fix: symbol %s at 0x%x (%s:%d) val=0x%x addend=0x%x\n",
94f592af
NC
2447 S_GET_NAME (fixP->fx_addsy),
2448 fixP->fx_frag->fr_address + fixP->fx_where,
2449 fixP->fx_file, fixP->fx_line,
2450 S_GET_VALUE (fixP->fx_addsy), value);
5b93d8bb
AM
2451#endif
2452 }
2453 else
a161fe53 2454 fixP->fx_done = 1;
5b93d8bb
AM
2455
2456 /* Apply fixups to operands. Note that there should be no relocations
2457 for any operands, since no instruction ever takes an operand
2458 that requires reloc. */
94f592af 2459 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
5b93d8bb
AM
2460 {
2461 int opindex;
2462 const struct i370_operand *operand;
2463 char *where;
2464 i370_insn_t insn;
2465
94f592af 2466 opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
5b93d8bb
AM
2467
2468 operand = &i370_operands[opindex];
2469
2470#ifdef DEBUG
55cf6793 2471 printf ("\nmd_apply_fix: fixup operand %s at 0x%x in %s:%d addend=0x%x\n",
5b93d8bb 2472 operand->name,
94f592af
NC
2473 fixP->fx_frag->fr_address + fixP->fx_where,
2474 fixP->fx_file, fixP->fx_line,
5b93d8bb
AM
2475 value);
2476#endif
2477 /* Fetch the instruction, insert the fully resolved operand
2478 value, and stuff the instruction back again.
92774660 2479 fisxp->fx_size is the length of the instruction. */
94f592af 2480 where = fixP->fx_frag->fr_literal + fixP->fx_where;
5b93d8bb 2481 insn.i[0] = bfd_getb32 ((unsigned char *) where);
94f592af
NC
2482
2483 if (6 <= fixP->fx_size)
2484 /* Deal with 48-bit insn's. */
2485 insn.i[1] = bfd_getb32 (((unsigned char *) where)+4);
2486
5b93d8bb
AM
2487 insn = i370_insert_operand (insn, operand, (offsetT) value);
2488 bfd_putb32 ((bfd_vma) insn.i[0], (unsigned char *) where);
5b93d8bb 2489
94f592af
NC
2490 if (6 <= fixP->fx_size)
2491 /* Deal with 48-bit insn's. */
2492 bfd_putb32 ((bfd_vma) insn.i[1], (((unsigned char *) where)+4));
2493
2494 /* We are done, right? right !! */
2495 fixP->fx_done = 1;
2496 if (fixP->fx_done)
2497 /* Nothing else to do here. */
2498 return;
5b93d8bb
AM
2499
2500 /* Determine a BFD reloc value based on the operand information.
2501 We are only prepared to turn a few of the operands into
2502 relocs. In fact, we support *zero* operand relocations ...
2503 Why? Because we are not expecting the compiler to generate
2504 any operands that need relocation. Due to the 12-bit naturew of
92774660 2505 i370 addressing, this would be unusual. */
5b93d8bb
AM
2506 {
2507 char *sfile;
2508 unsigned int sline;
2509
2510 /* Use expr_symbol_where to see if this is an expression
2511 symbol. */
94f592af
NC
2512 if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
2513 as_bad_where (fixP->fx_file, fixP->fx_line,
5b93d8bb
AM
2514 "unresolved expression that must be resolved");
2515 else
94f592af 2516 as_bad_where (fixP->fx_file, fixP->fx_line,
5b93d8bb 2517 "unsupported relocation type");
94f592af
NC
2518 fixP->fx_done = 1;
2519 return;
5b93d8bb
AM
2520 }
2521 }
2522 else
2523 {
2524 /* We branch to here if the fixup is not to a symbol that
94f592af
NC
2525 appears in an instruction operand, but is rather some
2526 declared storage. */
5b93d8bb 2527#ifdef OBJ_ELF
94f592af 2528 i370_elf_validate_fix (fixP, seg);
5b93d8bb
AM
2529#endif
2530#ifdef DEBUG
55cf6793 2531 printf ("md_apply_fix: reloc case %d in segment %s %s:%d\n",
94f592af 2532 fixP->fx_r_type, segment_name (seg), fixP->fx_file, fixP->fx_line);
5b93d8bb
AM
2533 printf ("\tcurrent fixup value is 0x%x \n", value);
2534#endif
94f592af 2535 switch (fixP->fx_r_type)
5b93d8bb
AM
2536 {
2537 case BFD_RELOC_32:
2538 case BFD_RELOC_CTOR:
94f592af
NC
2539 if (fixP->fx_pcrel)
2540 fixP->fx_r_type = BFD_RELOC_32_PCREL;
2541 /* Fall through. */
5b93d8bb
AM
2542
2543 case BFD_RELOC_RVA:
2544 case BFD_RELOC_32_PCREL:
2545 case BFD_RELOC_32_BASEREL:
2546#ifdef DEBUG
2547 printf ("\t32 bit relocation at 0x%x\n",
94f592af 2548 fixP->fx_frag->fr_address + fixP->fx_where);
5b93d8bb 2549#endif
94f592af 2550 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
5b93d8bb
AM
2551 value, 4);
2552 break;
2553
2554 case BFD_RELOC_LO16:
2555 case BFD_RELOC_16:
94f592af
NC
2556 if (fixP->fx_pcrel)
2557 as_bad_where (fixP->fx_file, fixP->fx_line,
5b93d8bb 2558 "cannot emit PC relative %s relocation%s%s",
94f592af
NC
2559 bfd_get_reloc_code_name (fixP->fx_r_type),
2560 fixP->fx_addsy != NULL ? " against " : "",
2561 (fixP->fx_addsy != NULL
2562 ? S_GET_NAME (fixP->fx_addsy)
5b93d8bb
AM
2563 : ""));
2564
94f592af 2565 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
5b93d8bb
AM
2566 value, 2);
2567 break;
2568
2569 /* This case happens when you write, for example,
2570 lis %r3,(L1-L2)@ha
2571 where L1 and L2 are defined later. */
2572 case BFD_RELOC_HI16:
94f592af 2573 if (fixP->fx_pcrel)
5b93d8bb 2574 abort ();
94f592af 2575 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
5b93d8bb
AM
2576 value >> 16, 2);
2577 break;
2578 case BFD_RELOC_HI16_S:
94f592af 2579 if (fixP->fx_pcrel)
5b93d8bb 2580 abort ();
94f592af 2581 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
5b93d8bb
AM
2582 (value + 0x8000) >> 16, 2);
2583 break;
2584
2585 case BFD_RELOC_8:
94f592af 2586 if (fixP->fx_pcrel)
5b93d8bb
AM
2587 abort ();
2588
94f592af 2589 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
5b93d8bb
AM
2590 value, 1);
2591 break;
2592
2593 default:
bc805888 2594 fprintf (stderr,
94f592af 2595 "Gas failure, reloc value %d\n", fixP->fx_r_type);
07726851 2596 fflush (stderr);
5b93d8bb
AM
2597 abort ();
2598 }
2599 }
2600
94f592af 2601 fixP->fx_addnumber = value;
5b93d8bb
AM
2602}
2603
2604/* Generate a reloc for a fixup. */
2605
2606arelent *
ea1562b3 2607tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
5b93d8bb
AM
2608{
2609 arelent *reloc;
2610
ea1562b3 2611 reloc = xmalloc (sizeof (arelent));
5b93d8bb 2612
ea1562b3 2613 reloc->sym_ptr_ptr = xmalloc (sizeof (asymbol *));
5b93d8bb
AM
2614 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2615 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2616 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2617 if (reloc->howto == (reloc_howto_type *) NULL)
2618 {
2619 as_bad_where (fixp->fx_file, fixp->fx_line,
2620 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
2621 return NULL;
2622 }
2623 reloc->addend = fixp->fx_addnumber;
2624
2625#ifdef DEBUG
2626 printf ("\ngen_reloc(): sym %s (%s:%d) at addr 0x%x addend=0x%x\n",
2627 fixp->fx_addsy->bsym->name,
2628 fixp->fx_file, fixp->fx_line,
2629 reloc->address, reloc->addend);
2630#endif
2631
2632 return reloc;
2633}
ea1562b3
NC
2634
2635/* The target specific pseudo-ops which we support. */
2636
2637const pseudo_typeS md_pseudo_table[] =
2638{
2639 /* Pseudo-ops which must be overridden. */
2640 { "byte", i370_byte, 0 },
2641
2642 { "dc", i370_dc, 0 },
2643 { "ds", i370_ds, 0 },
2644 { "rmode", i370_rmode, 0 },
2645 { "csect", i370_csect, 0 },
2646 { "dsect", i370_dsect, 0 },
2647
2648 /* enable ebcdic strings e.g. for 3270 support */
2649 { "ebcdic", i370_ebcdic, 0 },
2650
2651#ifdef OBJ_ELF
2652 { "long", i370_elf_cons, 4 },
2653 { "word", i370_elf_cons, 4 },
2654 { "short", i370_elf_cons, 2 },
2655 { "rdata", i370_elf_rdata, 0 },
2656 { "rodata", i370_elf_rdata, 0 },
2657 { "lcomm", i370_elf_lcomm, 0 },
2658#endif
2659
2660 /* This pseudo-op is used even when not generating XCOFF output. */
2661 { "tc", i370_tc, 0 },
2662
2663 /* dump the literal pool */
2664 { "ltorg", i370_ltorg, 0 },
2665
2666 /* support the hlasm-style USING directive */
2667 { "using", i370_using, 0 },
2668 { "drop", i370_drop, 0 },
2669
2670 { NULL, NULL, 0 }
2671};
This page took 0.578277 seconds and 4 git commands to generate.