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