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