tc-sparc.c revision 1.4 1 /* tc-sparc.c -- Assemble for the SPARC
2 Copyright (C) 1989-2018 Free Software Foundation, Inc.
3 This file is part of GAS, the GNU Assembler.
4
5 GAS is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 GAS is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public
16 License along with GAS; see the file COPYING. If not, write
17 to the Free Software Foundation, 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
19
20 #include "as.h"
21 #include "safe-ctype.h"
22 #include "subsegs.h"
23
24 #include "opcode/sparc.h"
25 #include "dw2gencfi.h"
26
27 #ifdef OBJ_ELF
28 #include "elf/sparc.h"
29 #include "dwarf2dbg.h"
30 #endif
31
32 /* Some ancient Sun C compilers would not take such hex constants as
33 unsigned, and would end up sign-extending them to form an offsetT,
34 so use these constants instead. */
35 #define U0xffffffff ((((unsigned long) 1 << 16) << 16) - 1)
36 #define U0x80000000 ((((unsigned long) 1 << 16) << 15))
37
38 static int sparc_ip (char *, const struct sparc_opcode **);
39 static int parse_sparc_asi (char **, const sparc_asi **);
40 static int parse_keyword_arg (int (*) (const char *), char **, int *);
41 static int parse_const_expr_arg (char **, int *);
42 static int get_expression (char *);
43
44 /* Default architecture. */
45 /* ??? The default value should be V8, but sparclite support was added
46 by making it the default. GCC now passes -Asparclite, so maybe sometime in
47 the future we can set this to V8. */
48 #ifndef DEFAULT_ARCH
49 #define DEFAULT_ARCH "sparclite"
50 #endif
51 static const char *default_arch = DEFAULT_ARCH;
52
53 /* Non-zero if the initial values of `max_architecture' and `sparc_arch_size'
54 have been set. */
55 static int default_init_p;
56
57 /* Current architecture. We don't bump up unless necessary. */
58 static enum sparc_opcode_arch_val current_architecture = SPARC_OPCODE_ARCH_V6;
59
60 /* The maximum architecture level we can bump up to.
61 In a 32 bit environment, don't allow bumping up to v9 by default.
62 The native assembler works this way. The user is required to pass
63 an explicit argument before we'll create v9 object files. However, if
64 we don't see any v9 insns, a v8plus object file is not created. */
65 static enum sparc_opcode_arch_val max_architecture;
66
67 /* Either 32 or 64, selects file format. */
68 static int sparc_arch_size;
69 /* Initial (default) value, recorded separately in case a user option
70 changes the value before md_show_usage is called. */
71 static int default_arch_size;
72
73 #ifdef OBJ_ELF
74 /* The currently selected v9 memory model. Currently only used for
75 ELF. */
76 static enum { MM_TSO, MM_PSO, MM_RMO } sparc_memory_model = MM_RMO;
77
78 #ifndef TE_SOLARIS
79 /* Bitmask of instruction types seen so far, used to populate the
80 GNU attributes section with hwcap information. */
81 static bfd_uint64_t hwcap_seen;
82 #endif
83 #endif
84
85 static bfd_uint64_t hwcap_allowed;
86
87 static int architecture_requested;
88 static int warn_on_bump;
89
90 /* If warn_on_bump and the needed architecture is higher than this
91 architecture, issue a warning. */
92 static enum sparc_opcode_arch_val warn_after_architecture;
93
94 /* Non-zero if the assembler should generate error if an undeclared
95 g[23] register has been used in -64. */
96 static int no_undeclared_regs;
97
98 /* Non-zero if the assembler should generate a warning if an
99 unpredictable DCTI (delayed control transfer instruction) couple is
100 found. */
101 static int dcti_couples_detect;
102
103 /* Non-zero if we should try to relax jumps and calls. */
104 static int sparc_relax;
105
106 /* Non-zero if we are generating PIC code. */
107 int sparc_pic_code;
108
109 /* Non-zero if we should give an error when misaligned data is seen. */
110 static int enforce_aligned_data;
111
112 extern int target_big_endian;
113
114 static int target_little_endian_data;
115
116 /* Symbols for global registers on v9. */
117 static symbolS *globals[8];
118
119 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
120 int sparc_cie_data_alignment;
121
122 /* V9 and 86x have big and little endian data, but instructions are always big
123 endian. The sparclet has bi-endian support but both data and insns have
124 the same endianness. Global `target_big_endian' is used for data.
125 The following macro is used for instructions. */
126 #ifndef INSN_BIG_ENDIAN
127 #define INSN_BIG_ENDIAN (target_big_endian \
128 || default_arch_type == sparc86x \
129 || SPARC_OPCODE_ARCH_V9_P (max_architecture))
130 #endif
131
132 /* Handle of the OPCODE hash table. */
133 static struct hash_control *op_hash;
134
135 static void s_data1 (void);
136 static void s_seg (int);
137 static void s_proc (int);
138 static void s_reserve (int);
139 static void s_common (int);
140 static void s_empty (int);
141 static void s_uacons (int);
142 static void s_ncons (int);
143 #ifdef OBJ_ELF
144 static void s_register (int);
145 #endif
146
147 const pseudo_typeS md_pseudo_table[] =
148 {
149 {"align", s_align_bytes, 0}, /* Defaulting is invalid (0). */
150 {"common", s_common, 0},
151 {"empty", s_empty, 0},
152 {"global", s_globl, 0},
153 {"half", cons, 2},
154 {"nword", s_ncons, 0},
155 {"optim", s_ignore, 0},
156 {"proc", s_proc, 0},
157 {"reserve", s_reserve, 0},
158 {"seg", s_seg, 0},
159 {"skip", s_space, 0},
160 {"word", cons, 4},
161 {"xword", cons, 8},
162 {"uahalf", s_uacons, 2},
163 {"uaword", s_uacons, 4},
164 {"uaxword", s_uacons, 8},
165 #ifdef OBJ_ELF
166 /* These are specific to sparc/svr4. */
167 {"2byte", s_uacons, 2},
168 {"4byte", s_uacons, 4},
169 {"8byte", s_uacons, 8},
170 {"register", s_register, 0},
171 #endif
172 {NULL, 0, 0},
173 };
174
175 /* This array holds the chars that always start a comment. If the
176 pre-processor is disabled, these aren't very useful. */
177 const char comment_chars[] = "!"; /* JF removed '|' from
178 comment_chars. */
179
180 /* This array holds the chars that only start a comment at the beginning of
181 a line. If the line seems to have the form '# 123 filename'
182 .line and .file directives will appear in the pre-processed output. */
183 /* Note that input_file.c hand checks for '#' at the beginning of the
184 first line of the input file. This is because the compiler outputs
185 #NO_APP at the beginning of its output. */
186 /* Also note that comments started like this one will always
187 work if '/' isn't otherwise defined. */
188 const char line_comment_chars[] = "#";
189
190 const char line_separator_chars[] = ";";
191
192 /* Chars that can be used to separate mant from exp in floating point
193 nums. */
194 const char EXP_CHARS[] = "eE";
195
196 /* Chars that mean this number is a floating point constant.
197 As in 0f12.456
198 or 0d1.2345e12 */
199 const char FLT_CHARS[] = "rRsSfFdDxXpP";
200
201 /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
202 changed in read.c. Ideally it shouldn't have to know about it at all,
203 but nothing is ideal around here. */
204
205 #define isoctal(c) ((unsigned) ((c) - '0') < 8)
206
207 struct sparc_it
208 {
209 const char *error;
210 unsigned long opcode;
211 struct nlist *nlistp;
212 expressionS exp;
213 expressionS exp2;
214 int pcrel;
215 bfd_reloc_code_real_type reloc;
216 };
217
218 struct sparc_it the_insn, set_insn;
219
220 static void output_insn (const struct sparc_opcode *, struct sparc_it *);
221
222 /* Table of arguments to -A.
224 The sparc_opcode_arch table in sparc-opc.c is insufficient and incorrect
225 for this use. That table is for opcodes only. This table is for opcodes
226 and file formats. */
227
228 enum sparc_arch_types {v6, v7, v8, leon, sparclet, sparclite, sparc86x, v8plus,
229 v8plusa, v9, v9a, v9b, v9_64};
230
231 static struct sparc_arch {
232 const char *name;
233 const char *opcode_arch;
234 enum sparc_arch_types arch_type;
235 /* Default word size, as specified during configuration.
236 A value of zero means can't be used to specify default architecture. */
237 int default_arch_size;
238 /* Allowable arg to -A? */
239 int user_option_p;
240 /* Extra hardware capabilities allowed. These are added to the
241 hardware capabilities associated with the opcode
242 architecture. */
243 int hwcap_allowed;
244 int hwcap2_allowed;
245 } sparc_arch_table[] = {
246 { "v6", "v6", v6, 0, 1, 0, 0 },
247 { "v7", "v7", v7, 0, 1, 0, 0 },
248 { "v8", "v8", v8, 32, 1, 0, 0 },
249 { "v8a", "v8", v8, 32, 1, 0, 0 },
250 { "sparc", "v9", v9, 0, 1, HWCAP_V8PLUS, 0 },
251 { "sparcvis", "v9a", v9, 0, 1, 0, 0 },
252 { "sparcvis2", "v9b", v9, 0, 1, 0, 0 },
253 { "sparcfmaf", "v9b", v9, 0, 1, HWCAP_FMAF, 0 },
254 { "sparcima", "v9b", v9, 0, 1, HWCAP_FMAF|HWCAP_IMA, 0 },
255 { "sparcvis3", "v9b", v9, 0, 1, HWCAP_FMAF|HWCAP_VIS3|HWCAP_HPC, 0 },
256 { "sparcvis3r", "v9b", v9, 0, 1, HWCAP_FMAF|HWCAP_VIS3|HWCAP_HPC|HWCAP_FJFMAU, 0 },
257
258 { "sparc4", "v9v", v9, 0, 1, 0, 0 },
259 { "sparc5", "v9m", v9, 0, 1, 0, 0 },
260 { "sparc6", "m8", v9, 0, 1, 0, 0 },
261
262 { "leon", "leon", leon, 32, 1, 0, 0 },
263 { "sparclet", "sparclet", sparclet, 32, 1, 0, 0 },
264 { "sparclite", "sparclite", sparclite, 32, 1, 0, 0 },
265 { "sparc86x", "sparclite", sparc86x, 32, 1, 0, 0 },
266
267 { "v8plus", "v9", v9, 0, 1, HWCAP_V8PLUS, 0 },
268 { "v8plusa", "v9a", v9, 0, 1, HWCAP_V8PLUS, 0 },
269 { "v8plusb", "v9b", v9, 0, 1, HWCAP_V8PLUS, 0 },
270 { "v8plusc", "v9c", v9, 0, 1, HWCAP_V8PLUS, 0 },
271 { "v8plusd", "v9d", v9, 0, 1, HWCAP_V8PLUS, 0 },
272 { "v8pluse", "v9e", v9, 0, 1, HWCAP_V8PLUS, 0 },
273 { "v8plusv", "v9v", v9, 0, 1, HWCAP_V8PLUS, 0 },
274 { "v8plusm", "v9m", v9, 0, 1, HWCAP_V8PLUS, 0 },
275 { "v8plusm8", "m8", v9, 0, 1, HWCAP_V8PLUS, 0 },
276
277 { "v9", "v9", v9, 0, 1, 0, 0 },
278 { "v9a", "v9a", v9, 0, 1, 0, 0 },
279 { "v9b", "v9b", v9, 0, 1, 0, 0 },
280 { "v9c", "v9c", v9, 0, 1, 0, 0 },
281 { "v9d", "v9d", v9, 0, 1, 0, 0 },
282 { "v9e", "v9e", v9, 0, 1, 0, 0 },
283 { "v9v", "v9v", v9, 0, 1, 0, 0 },
284 { "v9m", "v9m", v9, 0, 1, 0, 0 },
285 { "v9m8", "m8", v9, 0, 1, 0, 0 },
286
287 /* This exists to allow configure.tgt to pass one
288 value to specify both the default machine and default word size. */
289 { "v9-64", "v9", v9, 64, 0, 0, 0 },
290 { NULL, NULL, v8, 0, 0, 0, 0 }
291 };
292
293 /* Variant of default_arch */
294 static enum sparc_arch_types default_arch_type;
295
296 static struct sparc_arch *
297 lookup_arch (const char *name)
298 {
299 struct sparc_arch *sa;
300
301 for (sa = &sparc_arch_table[0]; sa->name != NULL; sa++)
302 if (strcmp (sa->name, name) == 0)
303 break;
304 if (sa->name == NULL)
305 return NULL;
306 return sa;
307 }
308
309 /* Initialize the default opcode arch and word size from the default
310 architecture name. */
311
312 static void
313 init_default_arch (void)
314 {
315 struct sparc_arch *sa = lookup_arch (default_arch);
316
317 if (sa == NULL
318 || sa->default_arch_size == 0)
319 as_fatal (_("Invalid default architecture, broken assembler."));
320
321 max_architecture = sparc_opcode_lookup_arch (sa->opcode_arch);
322 if (max_architecture == SPARC_OPCODE_ARCH_BAD)
323 as_fatal (_("Bad opcode table, broken assembler."));
324 default_arch_size = sparc_arch_size = sa->default_arch_size;
325 default_init_p = 1;
326 default_arch_type = sa->arch_type;
327 }
328
329 /* Called by TARGET_FORMAT. */
330
331 const char *
332 sparc_target_format (void)
333 {
334 /* We don't get a chance to initialize anything before we're called,
335 so handle that now. */
336 if (! default_init_p)
337 init_default_arch ();
338
339 #ifdef OBJ_AOUT
340 #ifdef TE_NetBSD
341 return "a.out-sparc-netbsd";
342 #else
343 #ifdef TE_SPARCAOUT
344 if (target_big_endian)
345 return "a.out-sunos-big";
346 else if (default_arch_type == sparc86x && target_little_endian_data)
347 return "a.out-sunos-big";
348 else
349 return "a.out-sparc-little";
350 #else
351 return "a.out-sunos-big";
352 #endif
353 #endif
354 #endif
355
356 #ifdef OBJ_BOUT
357 return "b.out.big";
358 #endif
359
360 #ifdef OBJ_COFF
361 #ifdef TE_LYNX
362 return "coff-sparc-lynx";
363 #else
364 return "coff-sparc";
365 #endif
366 #endif
367
368 #ifdef TE_VXWORKS
369 return "elf32-sparc-vxworks";
370 #endif
371
372 #ifdef OBJ_ELF
373 return sparc_arch_size == 64 ? ELF64_TARGET_FORMAT : ELF_TARGET_FORMAT;
374 #endif
375
376 abort ();
377 }
378
379 /* md_parse_option
381 * Invocation line includes a switch not recognized by the base assembler.
382 * See if it's a processor-specific option. These are:
383 *
384 * -bump
385 * Warn on architecture bumps. See also -A.
386 *
387 * -Av6, -Av7, -Av8, -Aleon, -Asparclite, -Asparclet
388 * Standard 32 bit architectures.
389 * -Av9, -Av9a, -Av9b
390 * Sparc64 in either a 32 or 64 bit world (-32/-64 says which).
391 * This used to only mean 64 bits, but properly specifying it
392 * complicated gcc's ASM_SPECs, so now opcode selection is
393 * specified orthogonally to word size (except when specifying
394 * the default, but that is an internal implementation detail).
395 * -Av8plus, -Av8plusa, -Av8plusb
396 * Same as -Av9{,a,b}.
397 * -xarch=v8plus, -xarch=v8plusa, -xarch=v8plusb
398 * Same as -Av8plus{,a,b} -32, for compatibility with Sun's
399 * assembler.
400 * -xarch=v9, -xarch=v9a, -xarch=v9b
401 * Same as -Av9{,a,b} -64, for compatibility with Sun's
402 * assembler.
403 *
404 * Select the architecture and possibly the file format.
405 * Instructions or features not supported by the selected
406 * architecture cause fatal errors.
407 *
408 * The default is to start at v6, and bump the architecture up
409 * whenever an instruction is seen at a higher level. In 32 bit
410 * environments, v9 is not bumped up to, the user must pass
411 * -Av8plus{,a,b}.
412 *
413 * If -bump is specified, a warning is printing when bumping to
414 * higher levels.
415 *
416 * If an architecture is specified, all instructions must match
417 * that architecture. Any higher level instructions are flagged
418 * as errors. Note that in the 32 bit environment specifying
419 * -Av8plus does not automatically create a v8plus object file, a
420 * v9 insn must be seen.
421 *
422 * If both an architecture and -bump are specified, the
423 * architecture starts at the specified level, but bumps are
424 * warnings. Note that we can't set `current_architecture' to
425 * the requested level in this case: in the 32 bit environment,
426 * we still must avoid creating v8plus object files unless v9
427 * insns are seen.
428 *
429 * Note:
430 * Bumping between incompatible architectures is always an
431 * error. For example, from sparclite to v9.
432 */
433
434 #ifdef OBJ_ELF
435 const char *md_shortopts = "A:K:VQ:sq";
436 #else
437 #ifdef OBJ_AOUT
438 const char *md_shortopts = "A:k";
439 #else
440 const char *md_shortopts = "A:";
441 #endif
442 #endif
443 struct option md_longopts[] = {
444 #define OPTION_BUMP (OPTION_MD_BASE)
445 {"bump", no_argument, NULL, OPTION_BUMP},
446 #define OPTION_SPARC (OPTION_MD_BASE + 1)
447 {"sparc", no_argument, NULL, OPTION_SPARC},
448 #define OPTION_XARCH (OPTION_MD_BASE + 2)
449 {"xarch", required_argument, NULL, OPTION_XARCH},
450 #ifdef OBJ_ELF
451 #define OPTION_32 (OPTION_MD_BASE + 3)
452 {"32", no_argument, NULL, OPTION_32},
453 #define OPTION_64 (OPTION_MD_BASE + 4)
454 {"64", no_argument, NULL, OPTION_64},
455 #define OPTION_TSO (OPTION_MD_BASE + 5)
456 {"TSO", no_argument, NULL, OPTION_TSO},
457 #define OPTION_PSO (OPTION_MD_BASE + 6)
458 {"PSO", no_argument, NULL, OPTION_PSO},
459 #define OPTION_RMO (OPTION_MD_BASE + 7)
460 {"RMO", no_argument, NULL, OPTION_RMO},
461 #endif
462 #ifdef SPARC_BIENDIAN
463 #define OPTION_LITTLE_ENDIAN (OPTION_MD_BASE + 8)
464 {"EL", no_argument, NULL, OPTION_LITTLE_ENDIAN},
465 #define OPTION_BIG_ENDIAN (OPTION_MD_BASE + 9)
466 {"EB", no_argument, NULL, OPTION_BIG_ENDIAN},
467 #endif
468 #define OPTION_ENFORCE_ALIGNED_DATA (OPTION_MD_BASE + 10)
469 {"enforce-aligned-data", no_argument, NULL, OPTION_ENFORCE_ALIGNED_DATA},
470 #define OPTION_LITTLE_ENDIAN_DATA (OPTION_MD_BASE + 11)
471 {"little-endian-data", no_argument, NULL, OPTION_LITTLE_ENDIAN_DATA},
472 #ifdef OBJ_ELF
473 #define OPTION_NO_UNDECLARED_REGS (OPTION_MD_BASE + 12)
474 {"no-undeclared-regs", no_argument, NULL, OPTION_NO_UNDECLARED_REGS},
475 #define OPTION_UNDECLARED_REGS (OPTION_MD_BASE + 13)
476 {"undeclared-regs", no_argument, NULL, OPTION_UNDECLARED_REGS},
477 #endif
478 #define OPTION_RELAX (OPTION_MD_BASE + 14)
479 {"relax", no_argument, NULL, OPTION_RELAX},
480 #define OPTION_NO_RELAX (OPTION_MD_BASE + 15)
481 {"no-relax", no_argument, NULL, OPTION_NO_RELAX},
482 #define OPTION_DCTI_COUPLES_DETECT (OPTION_MD_BASE + 16)
483 {"dcti-couples-detect", no_argument, NULL, OPTION_DCTI_COUPLES_DETECT},
484 {NULL, no_argument, NULL, 0}
485 };
486
487 size_t md_longopts_size = sizeof (md_longopts);
488
489 int
490 md_parse_option (int c, const char *arg)
491 {
492 /* We don't get a chance to initialize anything before we're called,
493 so handle that now. */
494 if (! default_init_p)
495 init_default_arch ();
496
497 switch (c)
498 {
499 case OPTION_BUMP:
500 warn_on_bump = 1;
501 warn_after_architecture = SPARC_OPCODE_ARCH_V6;
502 break;
503
504 case OPTION_XARCH:
505 #ifdef OBJ_ELF
506 if (!strncmp (arg, "v9", 2))
507 md_parse_option (OPTION_64, NULL);
508 else
509 {
510 if (!strncmp (arg, "v8", 2)
511 || !strncmp (arg, "v7", 2)
512 || !strncmp (arg, "v6", 2)
513 || !strcmp (arg, "sparclet")
514 || !strcmp (arg, "sparclite")
515 || !strcmp (arg, "sparc86x"))
516 md_parse_option (OPTION_32, NULL);
517 }
518 #endif
519 /* Fall through. */
520
521 case 'A':
522 {
523 struct sparc_arch *sa;
524 enum sparc_opcode_arch_val opcode_arch;
525
526 sa = lookup_arch (arg);
527 if (sa == NULL
528 || ! sa->user_option_p)
529 {
530 if (c == OPTION_XARCH)
531 as_bad (_("invalid architecture -xarch=%s"), arg);
532 else
533 as_bad (_("invalid architecture -A%s"), arg);
534 return 0;
535 }
536
537 opcode_arch = sparc_opcode_lookup_arch (sa->opcode_arch);
538 if (opcode_arch == SPARC_OPCODE_ARCH_BAD)
539 as_fatal (_("Bad opcode table, broken assembler."));
540
541 if (!architecture_requested
542 || opcode_arch > max_architecture)
543 max_architecture = opcode_arch;
544
545 /* The allowed hardware capabilities are the implied by the
546 opcodes arch plus any extra capabilities defined in the GAS
547 arch. */
548 hwcap_allowed
549 = (hwcap_allowed
550 | (((bfd_uint64_t) sparc_opcode_archs[opcode_arch].hwcaps2) << 32)
551 | (((bfd_uint64_t) sa->hwcap2_allowed) << 32)
552 | sparc_opcode_archs[opcode_arch].hwcaps
553 | sa->hwcap_allowed);
554 architecture_requested = 1;
555 }
556 break;
557
558 case OPTION_SPARC:
559 /* Ignore -sparc, used by SunOS make default .s.o rule. */
560 break;
561
562 case OPTION_ENFORCE_ALIGNED_DATA:
563 enforce_aligned_data = 1;
564 break;
565
566 #ifdef SPARC_BIENDIAN
567 case OPTION_LITTLE_ENDIAN:
568 target_big_endian = 0;
569 if (default_arch_type != sparclet)
570 as_fatal ("This target does not support -EL");
571 break;
572 case OPTION_LITTLE_ENDIAN_DATA:
573 target_little_endian_data = 1;
574 target_big_endian = 0;
575 if (default_arch_type != sparc86x
576 && default_arch_type != v9)
577 as_fatal ("This target does not support --little-endian-data");
578 break;
579 case OPTION_BIG_ENDIAN:
580 target_big_endian = 1;
581 break;
582 #endif
583
584 #ifdef OBJ_AOUT
585 case 'k':
586 sparc_pic_code = 1;
587 break;
588 #endif
589
590 #ifdef OBJ_ELF
591 case OPTION_32:
592 case OPTION_64:
593 {
594 const char **list, **l;
595
596 sparc_arch_size = c == OPTION_32 ? 32 : 64;
597 list = bfd_target_list ();
598 for (l = list; *l != NULL; l++)
599 {
600 if (sparc_arch_size == 32)
601 {
602 if (CONST_STRNEQ (*l, "elf32-sparc"))
603 break;
604 }
605 else
606 {
607 if (CONST_STRNEQ (*l, "elf64-sparc"))
608 break;
609 }
610 }
611 if (*l == NULL)
612 as_fatal (_("No compiled in support for %d bit object file format"),
613 sparc_arch_size);
614 free (list);
615
616 if (sparc_arch_size == 64
617 && max_architecture < SPARC_OPCODE_ARCH_V9)
618 max_architecture = SPARC_OPCODE_ARCH_V9;
619 }
620 break;
621
622 case OPTION_TSO:
623 sparc_memory_model = MM_TSO;
624 break;
625
626 case OPTION_PSO:
627 sparc_memory_model = MM_PSO;
628 break;
629
630 case OPTION_RMO:
631 sparc_memory_model = MM_RMO;
632 break;
633
634 case 'V':
635 print_version_id ();
636 break;
637
638 case 'Q':
639 /* Qy - do emit .comment
640 Qn - do not emit .comment. */
641 break;
642
643 case 's':
644 /* Use .stab instead of .stab.excl. */
645 break;
646
647 case 'q':
648 /* quick -- Native assembler does fewer checks. */
649 break;
650
651 case 'K':
652 if (strcmp (arg, "PIC") != 0)
653 as_warn (_("Unrecognized option following -K"));
654 else
655 sparc_pic_code = 1;
656 break;
657
658 case OPTION_NO_UNDECLARED_REGS:
659 no_undeclared_regs = 1;
660 break;
661
662 case OPTION_UNDECLARED_REGS:
663 no_undeclared_regs = 0;
664 break;
665 #endif
666
667 case OPTION_RELAX:
668 sparc_relax = 1;
669 break;
670
671 case OPTION_NO_RELAX:
672 sparc_relax = 0;
673 break;
674
675 case OPTION_DCTI_COUPLES_DETECT:
676 dcti_couples_detect = 1;
677 break;
678
679 default:
680 return 0;
681 }
682
683 return 1;
684 }
685
686 void
687 md_show_usage (FILE *stream)
688 {
689 const struct sparc_arch *arch;
690 int column;
691
692 /* We don't get a chance to initialize anything before we're called,
693 so handle that now. */
694 if (! default_init_p)
695 init_default_arch ();
696
697 fprintf (stream, _("SPARC options:\n"));
698 column = 0;
699 for (arch = &sparc_arch_table[0]; arch->name; arch++)
700 {
701 if (!arch->user_option_p)
702 continue;
703 if (arch != &sparc_arch_table[0])
704 fprintf (stream, " | ");
705 if (column + strlen (arch->name) > 70)
706 {
707 column = 0;
708 fputc ('\n', stream);
709 }
710 column += 5 + 2 + strlen (arch->name);
711 fprintf (stream, "-A%s", arch->name);
712 }
713 for (arch = &sparc_arch_table[0]; arch->name; arch++)
714 {
715 if (!arch->user_option_p)
716 continue;
717 fprintf (stream, " | ");
718 if (column + strlen (arch->name) > 65)
719 {
720 column = 0;
721 fputc ('\n', stream);
722 }
723 column += 5 + 7 + strlen (arch->name);
724 fprintf (stream, "-xarch=%s", arch->name);
725 }
726 fprintf (stream, _("\n\
727 specify variant of SPARC architecture\n\
728 -bump warn when assembler switches architectures\n\
729 -sparc ignored\n\
730 --enforce-aligned-data force .long, etc., to be aligned correctly\n\
731 -relax relax jumps and branches (default)\n\
732 -no-relax avoid changing any jumps and branches\n"));
733 #ifdef OBJ_AOUT
734 fprintf (stream, _("\
735 -k generate PIC\n"));
736 #endif
737 #ifdef OBJ_ELF
738 fprintf (stream, _("\
739 -32 create 32 bit object file\n\
740 -64 create 64 bit object file\n"));
741 fprintf (stream, _("\
742 [default is %d]\n"), default_arch_size);
743 fprintf (stream, _("\
744 -TSO use Total Store Ordering\n\
745 -PSO use Partial Store Ordering\n\
746 -RMO use Relaxed Memory Ordering\n"));
747 fprintf (stream, _("\
748 [default is %s]\n"), (default_arch_size == 64) ? "RMO" : "TSO");
749 fprintf (stream, _("\
750 -KPIC generate PIC\n\
751 -V print assembler version number\n\
752 -undeclared-regs ignore application global register usage without\n\
753 appropriate .register directive (default)\n\
754 -no-undeclared-regs force error on application global register usage\n\
755 without appropriate .register directive\n\
756 --dcti-couples-detect warn when an unpredictable DCTI couple is found\n\
757 -q ignored\n\
758 -Qy, -Qn ignored\n\
759 -s ignored\n"));
760 #endif
761 #ifdef SPARC_BIENDIAN
762 fprintf (stream, _("\
763 -EL generate code for a little endian machine\n\
764 -EB generate code for a big endian machine\n\
765 --little-endian-data generate code for a machine having big endian\n\
766 instructions and little endian data.\n"));
767 #endif
768 }
769
770 /* Native operand size opcode translation. */
772 static struct
773 {
774 const char *name;
775 const char *name32;
776 const char *name64;
777 } native_op_table[] =
778 {
779 {"ldn", "ld", "ldx"},
780 {"ldna", "lda", "ldxa"},
781 {"stn", "st", "stx"},
782 {"stna", "sta", "stxa"},
783 {"slln", "sll", "sllx"},
784 {"srln", "srl", "srlx"},
785 {"sran", "sra", "srax"},
786 {"casn", "cas", "casx"},
787 {"casna", "casa", "casxa"},
788 {"clrn", "clr", "clrx"},
789 {NULL, NULL, NULL},
790 };
791
792 /* sparc64 privileged and hyperprivileged registers. */
794
795 struct priv_reg_entry
796 {
797 const char *name;
798 int regnum;
799 };
800
801 struct priv_reg_entry priv_reg_table[] =
802 {
803 {"tpc", 0},
804 {"tnpc", 1},
805 {"tstate", 2},
806 {"tt", 3},
807 {"tick", 4},
808 {"tba", 5},
809 {"pstate", 6},
810 {"tl", 7},
811 {"pil", 8},
812 {"cwp", 9},
813 {"cansave", 10},
814 {"canrestore", 11},
815 {"cleanwin", 12},
816 {"otherwin", 13},
817 {"wstate", 14},
818 {"fq", 15},
819 {"gl", 16},
820 {"pmcdper", 23},
821 {"ver", 31},
822 {NULL, -1}, /* End marker. */
823 };
824
825 struct priv_reg_entry hpriv_reg_table[] =
826 {
827 {"hpstate", 0},
828 {"htstate", 1},
829 {"hintp", 3},
830 {"htba", 5},
831 {"hver", 6},
832 {"hmcdper", 23},
833 {"hmcddfr", 24},
834 {"hva_mask_nz", 27},
835 {"hstick_offset", 28},
836 {"hstick_enable", 29},
837 {"hstick_cmpr", 31},
838 {NULL, -1}, /* End marker. */
839 };
840
841 /* v9a or later specific ancillary state registers. */
842
843 struct priv_reg_entry v9a_asr_table[] =
844 {
845 {"tick_cmpr", 23},
846 {"sys_tick_cmpr", 25},
847 {"sys_tick", 24},
848 {"stick_cmpr", 25},
849 {"stick", 24},
850 {"softint_clear", 21},
851 {"softint_set", 20},
852 {"softint", 22},
853 {"set_softint", 20},
854 {"pause", 27},
855 {"pic", 17},
856 {"pcr", 16},
857 {"mwait", 28},
858 {"gsr", 19},
859 {"dcr", 18},
860 {"cfr", 26},
861 {"clear_softint", 21},
862 {NULL, -1}, /* End marker. */
863 };
864
865 static int
866 cmp_reg_entry (const void *parg, const void *qarg)
867 {
868 const struct priv_reg_entry *p = (const struct priv_reg_entry *) parg;
869 const struct priv_reg_entry *q = (const struct priv_reg_entry *) qarg;
870
871 if (p->name == q->name)
872 return 0;
873 else if (p->name == NULL)
874 return 1;
875 else if (q->name == NULL)
876 return -1;
877 else
878 return strcmp (q->name, p->name);
879 }
880
881 /* sparc %-pseudo-operations. */
883
884
885 #define F_POP_V9 0x1 /* The pseudo-op is for v9 only. */
886 #define F_POP_PCREL 0x2 /* The pseudo-op can be used in pc-relative
887 contexts. */
888 #define F_POP_TLS_CALL 0x4 /* The pseudo-op marks a tls call. */
889 #define F_POP_POSTFIX 0x8 /* The pseudo-op should appear after the
890 last operand of an
891 instruction. (Generally they can appear
892 anywhere an immediate operand is
893 expected. */
894 struct pop_entry
895 {
896 /* The name as it appears in assembler. */
897 const char *name;
898 /* The reloc this pseudo-op translates to. */
899 bfd_reloc_code_real_type reloc;
900 /* Flags. See F_POP_* above. */
901 int flags;
902 };
903
904 struct pop_entry pop_table[] =
905 {
906 { "hix", BFD_RELOC_SPARC_HIX22, F_POP_V9 },
907 { "lox", BFD_RELOC_SPARC_LOX10, F_POP_V9 },
908 { "hi", BFD_RELOC_HI22, F_POP_PCREL },
909 { "lo", BFD_RELOC_LO10, F_POP_PCREL },
910 { "pc22", BFD_RELOC_SPARC_PC22, F_POP_PCREL },
911 { "pc10", BFD_RELOC_SPARC_PC10, F_POP_PCREL },
912 { "hh", BFD_RELOC_SPARC_HH22, F_POP_V9|F_POP_PCREL },
913 { "hm", BFD_RELOC_SPARC_HM10, F_POP_V9|F_POP_PCREL },
914 { "lm", BFD_RELOC_SPARC_LM22, F_POP_V9|F_POP_PCREL },
915 { "h34", BFD_RELOC_SPARC_H34, F_POP_V9 },
916 { "l34", BFD_RELOC_SPARC_L44, F_POP_V9 },
917 { "h44", BFD_RELOC_SPARC_H44, F_POP_V9 },
918 { "m44", BFD_RELOC_SPARC_M44, F_POP_V9 },
919 { "l44", BFD_RELOC_SPARC_L44, F_POP_V9 },
920 { "uhi", BFD_RELOC_SPARC_HH22, F_POP_V9 },
921 { "ulo", BFD_RELOC_SPARC_HM10, F_POP_V9 },
922 { "tgd_hi22", BFD_RELOC_SPARC_TLS_GD_HI22, 0 },
923 { "tgd_lo10", BFD_RELOC_SPARC_TLS_GD_LO10, 0 },
924 { "tldm_hi22", BFD_RELOC_SPARC_TLS_LDM_HI22, 0 },
925 { "tldm_lo10", BFD_RELOC_SPARC_TLS_LDM_LO10, 0 },
926 { "tldo_hix22", BFD_RELOC_SPARC_TLS_LDO_HIX22, 0 },
927 { "tldo_lox10", BFD_RELOC_SPARC_TLS_LDO_LOX10, 0 },
928 { "tie_hi22", BFD_RELOC_SPARC_TLS_IE_HI22, 0 },
929 { "tie_lo10", BFD_RELOC_SPARC_TLS_IE_LO10, 0 },
930 { "tle_hix22", BFD_RELOC_SPARC_TLS_LE_HIX22, 0 },
931 { "tle_lox10", BFD_RELOC_SPARC_TLS_LE_LOX10, 0 },
932 { "gdop_hix22", BFD_RELOC_SPARC_GOTDATA_OP_HIX22, 0 },
933 { "gdop_lox10", BFD_RELOC_SPARC_GOTDATA_OP_LOX10, 0 },
934 { "tgd_add", BFD_RELOC_SPARC_TLS_GD_ADD, F_POP_POSTFIX },
935 { "tgd_call", BFD_RELOC_SPARC_TLS_GD_CALL, F_POP_POSTFIX|F_POP_TLS_CALL },
936 { "tldm_add", BFD_RELOC_SPARC_TLS_LDM_ADD, F_POP_POSTFIX },
937 { "tldm_call", BFD_RELOC_SPARC_TLS_LDM_CALL, F_POP_POSTFIX|F_POP_TLS_CALL },
938 { "tldo_add", BFD_RELOC_SPARC_TLS_LDO_ADD, F_POP_POSTFIX },
939 { "tie_ldx", BFD_RELOC_SPARC_TLS_IE_LDX, F_POP_POSTFIX },
940 { "tie_ld", BFD_RELOC_SPARC_TLS_IE_LD, F_POP_POSTFIX },
941 { "tie_add", BFD_RELOC_SPARC_TLS_IE_ADD, F_POP_POSTFIX },
942 { "gdop", BFD_RELOC_SPARC_GOTDATA_OP, F_POP_POSTFIX }
943 };
944
945 /* Table of %-names that can appear in a sparc assembly program. This
947 table is initialized in md_begin and contains entries for each
948 privileged/hyperprivileged/alternate register and %-pseudo-op. */
949
950 enum perc_entry_type
951 {
952 perc_entry_none = 0,
953 perc_entry_reg,
954 perc_entry_post_pop,
955 perc_entry_imm_pop
956 };
957
958 struct perc_entry
959 {
960 /* Entry type. */
961 enum perc_entry_type type;
962 /* Name of the %-entity. */
963 const char *name;
964 /* strlen (name). */
965 int len;
966 /* Value. Either a pop or a reg depending on type.*/
967 union
968 {
969 struct pop_entry *pop;
970 struct priv_reg_entry *reg;
971 };
972 };
973
974 #define NUM_PERC_ENTRIES \
975 (((sizeof (priv_reg_table) / sizeof (priv_reg_table[0])) - 1) \
976 + ((sizeof (hpriv_reg_table) / sizeof (hpriv_reg_table[0])) - 1) \
977 + ((sizeof (v9a_asr_table) / sizeof (v9a_asr_table[0])) - 1) \
978 + ARRAY_SIZE (pop_table) \
979 + 1)
980
981 struct perc_entry perc_table[NUM_PERC_ENTRIES];
982
983 static int
984 cmp_perc_entry (const void *parg, const void *qarg)
985 {
986 const struct perc_entry *p = (const struct perc_entry *) parg;
987 const struct perc_entry *q = (const struct perc_entry *) qarg;
988
989 if (p->name == q->name)
990 return 0;
991 else if (p->name == NULL)
992 return 1;
993 else if (q->name == NULL)
994 return -1;
995 else
996 return strcmp (q->name, p->name);
997 }
998
999 /* This function is called once, at assembler startup time. It should
1001 set up all the tables, etc. that the MD part of the assembler will
1002 need. */
1003
1004 void
1005 md_begin (void)
1006 {
1007 const char *retval = NULL;
1008 int lose = 0;
1009 unsigned int i = 0;
1010
1011 /* We don't get a chance to initialize anything before md_parse_option
1012 is called, and it may not be called, so handle default initialization
1013 now if not already done. */
1014 if (! default_init_p)
1015 init_default_arch ();
1016
1017 sparc_cie_data_alignment = sparc_arch_size == 64 ? -8 : -4;
1018 op_hash = hash_new ();
1019
1020 while (i < (unsigned int) sparc_num_opcodes)
1021 {
1022 const char *name = sparc_opcodes[i].name;
1023 retval = hash_insert (op_hash, name, (void *) &sparc_opcodes[i]);
1024 if (retval != NULL)
1025 {
1026 as_bad (_("Internal error: can't hash `%s': %s\n"),
1027 sparc_opcodes[i].name, retval);
1028 lose = 1;
1029 }
1030 do
1031 {
1032 if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
1033 {
1034 as_bad (_("Internal error: losing opcode: `%s' \"%s\"\n"),
1035 sparc_opcodes[i].name, sparc_opcodes[i].args);
1036 lose = 1;
1037 }
1038 ++i;
1039 }
1040 while (i < (unsigned int) sparc_num_opcodes
1041 && !strcmp (sparc_opcodes[i].name, name));
1042 }
1043
1044 for (i = 0; native_op_table[i].name; i++)
1045 {
1046 const struct sparc_opcode *insn;
1047 const char *name = ((sparc_arch_size == 32)
1048 ? native_op_table[i].name32
1049 : native_op_table[i].name64);
1050 insn = (struct sparc_opcode *) hash_find (op_hash, name);
1051 if (insn == NULL)
1052 {
1053 as_bad (_("Internal error: can't find opcode `%s' for `%s'\n"),
1054 name, native_op_table[i].name);
1055 lose = 1;
1056 }
1057 else
1058 {
1059 retval = hash_insert (op_hash, native_op_table[i].name,
1060 (void *) insn);
1061 if (retval != NULL)
1062 {
1063 as_bad (_("Internal error: can't hash `%s': %s\n"),
1064 sparc_opcodes[i].name, retval);
1065 lose = 1;
1066 }
1067 }
1068 }
1069
1070 if (lose)
1071 as_fatal (_("Broken assembler. No assembly attempted."));
1072
1073 qsort (priv_reg_table, sizeof (priv_reg_table) / sizeof (priv_reg_table[0]),
1074 sizeof (priv_reg_table[0]), cmp_reg_entry);
1075 qsort (hpriv_reg_table, sizeof (hpriv_reg_table) / sizeof (hpriv_reg_table[0]),
1076 sizeof (hpriv_reg_table[0]), cmp_reg_entry);
1077 qsort (v9a_asr_table, sizeof (v9a_asr_table) / sizeof (v9a_asr_table[0]),
1078 sizeof (v9a_asr_table[0]), cmp_reg_entry);
1079
1080 /* If -bump, record the architecture level at which we start issuing
1081 warnings. The behaviour is different depending upon whether an
1082 architecture was explicitly specified. If it wasn't, we issue warnings
1083 for all upwards bumps. If it was, we don't start issuing warnings until
1084 we need to bump beyond the requested architecture or when we bump between
1085 conflicting architectures. */
1086
1087 if (warn_on_bump
1088 && architecture_requested)
1089 {
1090 /* `max_architecture' records the requested architecture.
1091 Issue warnings if we go above it. */
1092 warn_after_architecture = max_architecture;
1093 }
1094
1095 /* Find the highest architecture level that doesn't conflict with
1096 the requested one. */
1097
1098 if (warn_on_bump
1099 || !architecture_requested)
1100 {
1101 enum sparc_opcode_arch_val current_max_architecture
1102 = max_architecture;
1103
1104 for (max_architecture = SPARC_OPCODE_ARCH_MAX;
1105 max_architecture > warn_after_architecture;
1106 --max_architecture)
1107 if (! SPARC_OPCODE_CONFLICT_P (max_architecture,
1108 current_max_architecture))
1109 break;
1110 }
1111
1112 /* Prepare the tables of %-pseudo-ops. */
1113 {
1114 struct priv_reg_entry *reg_tables[]
1115 = {priv_reg_table, hpriv_reg_table, v9a_asr_table, NULL};
1116 struct priv_reg_entry **reg_table;
1117 int entry = 0;
1118
1119 /* Add registers. */
1120 for (reg_table = reg_tables; reg_table[0]; reg_table++)
1121 {
1122 struct priv_reg_entry *reg;
1123 for (reg = *reg_table; reg->name; reg++)
1124 {
1125 struct perc_entry *p = &perc_table[entry++];
1126 p->type = perc_entry_reg;
1127 p->name = reg->name;
1128 p->len = strlen (reg->name);
1129 p->reg = reg;
1130 }
1131 }
1132
1133 /* Add %-pseudo-ops. */
1134 for (i = 0; i < ARRAY_SIZE (pop_table); i++)
1135 {
1136 struct perc_entry *p = &perc_table[entry++];
1137 p->type = (pop_table[i].flags & F_POP_POSTFIX
1138 ? perc_entry_post_pop : perc_entry_imm_pop);
1139 p->name = pop_table[i].name;
1140 p->len = strlen (pop_table[i].name);
1141 p->pop = &pop_table[i];
1142 }
1143
1144 /* Last entry is the sentinel. */
1145 perc_table[entry].type = perc_entry_none;
1146
1147 qsort (perc_table, sizeof (perc_table) / sizeof (perc_table[0]),
1148 sizeof (perc_table[0]), cmp_perc_entry);
1149
1150 }
1151 }
1152
1153 /* Called after all assembly has been done. */
1154
1155 void
1156 sparc_md_end (void)
1157 {
1158 unsigned long mach = bfd_mach_sparc;
1159 #if defined(OBJ_ELF) && !defined(TE_SOLARIS)
1160 int hwcaps, hwcaps2;
1161 #endif
1162
1163 if (sparc_arch_size == 64)
1164 switch (current_architecture)
1165 {
1166 case SPARC_OPCODE_ARCH_V9A: mach = bfd_mach_sparc_v9a; break;
1167 case SPARC_OPCODE_ARCH_V9B: mach = bfd_mach_sparc_v9b; break;
1168 case SPARC_OPCODE_ARCH_V9C: mach = bfd_mach_sparc_v9c; break;
1169 case SPARC_OPCODE_ARCH_V9D: mach = bfd_mach_sparc_v9d; break;
1170 case SPARC_OPCODE_ARCH_V9E: mach = bfd_mach_sparc_v9e; break;
1171 case SPARC_OPCODE_ARCH_V9V: mach = bfd_mach_sparc_v9v; break;
1172 case SPARC_OPCODE_ARCH_V9M: mach = bfd_mach_sparc_v9m; break;
1173 case SPARC_OPCODE_ARCH_M8: mach = bfd_mach_sparc_v9m8; break;
1174 default: mach = bfd_mach_sparc_v9; break;
1175 }
1176 else
1177 switch (current_architecture)
1178 {
1179 case SPARC_OPCODE_ARCH_SPARCLET: mach = bfd_mach_sparc_sparclet; break;
1180 case SPARC_OPCODE_ARCH_V9: mach = bfd_mach_sparc_v8plus; break;
1181 case SPARC_OPCODE_ARCH_V9A: mach = bfd_mach_sparc_v8plusa; break;
1182 case SPARC_OPCODE_ARCH_V9B: mach = bfd_mach_sparc_v8plusb; break;
1183 case SPARC_OPCODE_ARCH_V9C: mach = bfd_mach_sparc_v8plusc; break;
1184 case SPARC_OPCODE_ARCH_V9D: mach = bfd_mach_sparc_v8plusd; break;
1185 case SPARC_OPCODE_ARCH_V9E: mach = bfd_mach_sparc_v8pluse; break;
1186 case SPARC_OPCODE_ARCH_V9V: mach = bfd_mach_sparc_v8plusv; break;
1187 case SPARC_OPCODE_ARCH_V9M: mach = bfd_mach_sparc_v8plusm; break;
1188 case SPARC_OPCODE_ARCH_M8: mach = bfd_mach_sparc_v8plusm8; break;
1189 /* The sparclite is treated like a normal sparc. Perhaps it shouldn't
1190 be but for now it is (since that's the way it's always been
1191 treated). */
1192 default: break;
1193 }
1194 bfd_set_arch_mach (stdoutput, bfd_arch_sparc, mach);
1195
1196 #if defined(OBJ_ELF) && !defined(TE_SOLARIS)
1197 hwcaps = hwcap_seen & U0xffffffff;
1198 hwcaps2 = hwcap_seen >> 32;
1199
1200 if (hwcaps)
1201 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU, Tag_GNU_Sparc_HWCAPS, hwcaps);
1202 if (hwcaps2)
1203 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU, Tag_GNU_Sparc_HWCAPS2, hwcaps2);
1204 #endif
1205 }
1206
1207 /* Return non-zero if VAL is in the range -(MAX+1) to MAX. */
1209
1210 static inline int
1211 in_signed_range (bfd_signed_vma val, bfd_signed_vma max)
1212 {
1213 if (max <= 0)
1214 abort ();
1215 /* Sign-extend the value from the architecture word size, so that
1216 0xffffffff is always considered -1 on sparc32. */
1217 if (sparc_arch_size == 32)
1218 {
1219 bfd_signed_vma sign = (bfd_signed_vma) 1 << 31;
1220 val = ((val & U0xffffffff) ^ sign) - sign;
1221 }
1222 if (val > max)
1223 return 0;
1224 if (val < ~max)
1225 return 0;
1226 return 1;
1227 }
1228
1229 /* Return non-zero if VAL is in the range 0 to MAX. */
1230
1231 static inline int
1232 in_unsigned_range (bfd_vma val, bfd_vma max)
1233 {
1234 if (val > max)
1235 return 0;
1236 return 1;
1237 }
1238
1239 /* Return non-zero if VAL is in the range -(MAX/2+1) to MAX.
1240 (e.g. -15 to +31). */
1241
1242 static inline int
1243 in_bitfield_range (bfd_signed_vma val, bfd_signed_vma max)
1244 {
1245 if (max <= 0)
1246 abort ();
1247 if (val > max)
1248 return 0;
1249 if (val < ~(max >> 1))
1250 return 0;
1251 return 1;
1252 }
1253
1254 static int
1255 sparc_ffs (unsigned int mask)
1256 {
1257 int i;
1258
1259 if (mask == 0)
1260 return -1;
1261
1262 for (i = 0; (mask & 1) == 0; ++i)
1263 mask >>= 1;
1264 return i;
1265 }
1266
1267 /* Implement big shift right. */
1268 static bfd_vma
1269 BSR (bfd_vma val, int amount)
1270 {
1271 if (sizeof (bfd_vma) <= 4 && amount >= 32)
1272 as_fatal (_("Support for 64-bit arithmetic not compiled in."));
1273 return val >> amount;
1274 }
1275
1276 /* For communication between sparc_ip and get_expression. */
1278 static char *expr_end;
1279
1280 /* Values for `special_case'.
1281 Instructions that require weird handling because they're longer than
1282 4 bytes. */
1283 #define SPECIAL_CASE_NONE 0
1284 #define SPECIAL_CASE_SET 1
1285 #define SPECIAL_CASE_SETSW 2
1286 #define SPECIAL_CASE_SETX 3
1287 /* FIXME: sparc-opc.c doesn't have necessary "S" trigger to enable this. */
1288 #define SPECIAL_CASE_FDIV 4
1289
1290 /* Bit masks of various insns. */
1291 #define NOP_INSN 0x01000000
1292 #define OR_INSN 0x80100000
1293 #define XOR_INSN 0x80180000
1294 #define FMOVS_INSN 0x81A00020
1295 #define SETHI_INSN 0x01000000
1296 #define SLLX_INSN 0x81281000
1297 #define SRA_INSN 0x81380000
1298
1299 /* The last instruction to be assembled. */
1300 static const struct sparc_opcode *last_insn;
1301 /* The assembled opcode of `last_insn'. */
1302 static unsigned long last_opcode;
1303
1304 /* Handle the set and setuw synthetic instructions. */
1306
1307 static void
1308 synthetize_setuw (const struct sparc_opcode *insn)
1309 {
1310 int need_hi22_p = 0;
1311 int rd = (the_insn.opcode & RD (~0)) >> 25;
1312
1313 if (the_insn.exp.X_op == O_constant)
1314 {
1315 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
1316 {
1317 if (sizeof (offsetT) > 4
1318 && (the_insn.exp.X_add_number < 0
1319 || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
1320 as_warn (_("set: number not in 0..4294967295 range"));
1321 }
1322 else
1323 {
1324 if (sizeof (offsetT) > 4
1325 && (the_insn.exp.X_add_number < -(offsetT) U0x80000000
1326 || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
1327 as_warn (_("set: number not in -2147483648..4294967295 range"));
1328 the_insn.exp.X_add_number = (int) the_insn.exp.X_add_number;
1329 }
1330 }
1331
1332 /* See if operand is absolute and small; skip sethi if so. */
1333 if (the_insn.exp.X_op != O_constant
1334 || the_insn.exp.X_add_number >= (1 << 12)
1335 || the_insn.exp.X_add_number < -(1 << 12))
1336 {
1337 the_insn.opcode = (SETHI_INSN | RD (rd)
1338 | ((the_insn.exp.X_add_number >> 10)
1339 & (the_insn.exp.X_op == O_constant
1340 ? 0x3fffff : 0)));
1341 the_insn.reloc = (the_insn.exp.X_op != O_constant
1342 ? BFD_RELOC_HI22 : BFD_RELOC_NONE);
1343 output_insn (insn, &the_insn);
1344 need_hi22_p = 1;
1345 }
1346
1347 /* See if operand has no low-order bits; skip OR if so. */
1348 if (the_insn.exp.X_op != O_constant
1349 || (need_hi22_p && (the_insn.exp.X_add_number & 0x3FF) != 0)
1350 || ! need_hi22_p)
1351 {
1352 the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (rd) : 0)
1353 | RD (rd) | IMMED
1354 | (the_insn.exp.X_add_number
1355 & (the_insn.exp.X_op != O_constant
1356 ? 0 : need_hi22_p ? 0x3ff : 0x1fff)));
1357 the_insn.reloc = (the_insn.exp.X_op != O_constant
1358 ? BFD_RELOC_LO10 : BFD_RELOC_NONE);
1359 output_insn (insn, &the_insn);
1360 }
1361 }
1362
1363 /* Handle the setsw synthetic instruction. */
1364
1365 static void
1366 synthetize_setsw (const struct sparc_opcode *insn)
1367 {
1368 int low32, rd, opc;
1369
1370 rd = (the_insn.opcode & RD (~0)) >> 25;
1371
1372 if (the_insn.exp.X_op != O_constant)
1373 {
1374 synthetize_setuw (insn);
1375
1376 /* Need to sign extend it. */
1377 the_insn.opcode = (SRA_INSN | RS1 (rd) | RD (rd));
1378 the_insn.reloc = BFD_RELOC_NONE;
1379 output_insn (insn, &the_insn);
1380 return;
1381 }
1382
1383 if (sizeof (offsetT) > 4
1384 && (the_insn.exp.X_add_number < -(offsetT) U0x80000000
1385 || the_insn.exp.X_add_number > (offsetT) U0xffffffff))
1386 as_warn (_("setsw: number not in -2147483648..4294967295 range"));
1387
1388 low32 = the_insn.exp.X_add_number;
1389
1390 if (low32 >= 0)
1391 {
1392 synthetize_setuw (insn);
1393 return;
1394 }
1395
1396 opc = OR_INSN;
1397
1398 the_insn.reloc = BFD_RELOC_NONE;
1399 /* See if operand is absolute and small; skip sethi if so. */
1400 if (low32 < -(1 << 12))
1401 {
1402 the_insn.opcode = (SETHI_INSN | RD (rd)
1403 | (((~the_insn.exp.X_add_number) >> 10) & 0x3fffff));
1404 output_insn (insn, &the_insn);
1405 low32 = 0x1c00 | (low32 & 0x3ff);
1406 opc = RS1 (rd) | XOR_INSN;
1407 }
1408
1409 the_insn.opcode = (opc | RD (rd) | IMMED
1410 | (low32 & 0x1fff));
1411 output_insn (insn, &the_insn);
1412 }
1413
1414 /* Handle the setx synthetic instruction. */
1415
1416 static void
1417 synthetize_setx (const struct sparc_opcode *insn)
1418 {
1419 int upper32, lower32;
1420 int tmpreg = (the_insn.opcode & RS1 (~0)) >> 14;
1421 int dstreg = (the_insn.opcode & RD (~0)) >> 25;
1422 int upper_dstreg;
1423 int need_hh22_p = 0, need_hm10_p = 0, need_hi22_p = 0, need_lo10_p = 0;
1424 int need_xor10_p = 0;
1425
1426 #define SIGNEXT32(x) ((((x) & U0xffffffff) ^ U0x80000000) - U0x80000000)
1427 lower32 = SIGNEXT32 (the_insn.exp.X_add_number);
1428 upper32 = SIGNEXT32 (BSR (the_insn.exp.X_add_number, 32));
1429 #undef SIGNEXT32
1430
1431 upper_dstreg = tmpreg;
1432 /* The tmp reg should not be the dst reg. */
1433 if (tmpreg == dstreg)
1434 as_warn (_("setx: temporary register same as destination register"));
1435
1436 /* ??? Obviously there are other optimizations we can do
1437 (e.g. sethi+shift for 0x1f0000000) and perhaps we shouldn't be
1438 doing some of these. Later. If you do change things, try to
1439 change all of this to be table driven as well. */
1440 /* What to output depends on the number if it's constant.
1441 Compute that first, then output what we've decided upon. */
1442 if (the_insn.exp.X_op != O_constant)
1443 {
1444 if (sparc_arch_size == 32)
1445 {
1446 /* When arch size is 32, we want setx to be equivalent
1447 to setuw for anything but constants. */
1448 the_insn.exp.X_add_number &= 0xffffffff;
1449 synthetize_setuw (insn);
1450 return;
1451 }
1452 need_hh22_p = need_hm10_p = need_hi22_p = need_lo10_p = 1;
1453 lower32 = 0;
1454 upper32 = 0;
1455 }
1456 else
1457 {
1458 /* Reset X_add_number, we've extracted it as upper32/lower32.
1459 Otherwise fixup_segment will complain about not being able to
1460 write an 8 byte number in a 4 byte field. */
1461 the_insn.exp.X_add_number = 0;
1462
1463 /* Only need hh22 if `or' insn can't handle constant. */
1464 if (upper32 < -(1 << 12) || upper32 >= (1 << 12))
1465 need_hh22_p = 1;
1466
1467 /* Does bottom part (after sethi) have bits? */
1468 if ((need_hh22_p && (upper32 & 0x3ff) != 0)
1469 /* No hh22, but does upper32 still have bits we can't set
1470 from lower32? */
1471 || (! need_hh22_p && upper32 != 0 && upper32 != -1))
1472 need_hm10_p = 1;
1473
1474 /* If the lower half is all zero, we build the upper half directly
1475 into the dst reg. */
1476 if (lower32 != 0
1477 /* Need lower half if number is zero or 0xffffffff00000000. */
1478 || (! need_hh22_p && ! need_hm10_p))
1479 {
1480 /* No need for sethi if `or' insn can handle constant. */
1481 if (lower32 < -(1 << 12) || lower32 >= (1 << 12)
1482 /* Note that we can't use a negative constant in the `or'
1483 insn unless the upper 32 bits are all ones. */
1484 || (lower32 < 0 && upper32 != -1)
1485 || (lower32 >= 0 && upper32 == -1))
1486 need_hi22_p = 1;
1487
1488 if (need_hi22_p && upper32 == -1)
1489 need_xor10_p = 1;
1490
1491 /* Does bottom part (after sethi) have bits? */
1492 else if ((need_hi22_p && (lower32 & 0x3ff) != 0)
1493 /* No sethi. */
1494 || (! need_hi22_p && (lower32 & 0x1fff) != 0)
1495 /* Need `or' if we didn't set anything else. */
1496 || (! need_hi22_p && ! need_hh22_p && ! need_hm10_p))
1497 need_lo10_p = 1;
1498 }
1499 else
1500 /* Output directly to dst reg if lower 32 bits are all zero. */
1501 upper_dstreg = dstreg;
1502 }
1503
1504 if (!upper_dstreg && dstreg)
1505 as_warn (_("setx: illegal temporary register g0"));
1506
1507 if (need_hh22_p)
1508 {
1509 the_insn.opcode = (SETHI_INSN | RD (upper_dstreg)
1510 | ((upper32 >> 10) & 0x3fffff));
1511 the_insn.reloc = (the_insn.exp.X_op != O_constant
1512 ? BFD_RELOC_SPARC_HH22 : BFD_RELOC_NONE);
1513 output_insn (insn, &the_insn);
1514 }
1515
1516 if (need_hi22_p)
1517 {
1518 the_insn.opcode = (SETHI_INSN | RD (dstreg)
1519 | (((need_xor10_p ? ~lower32 : lower32)
1520 >> 10) & 0x3fffff));
1521 the_insn.reloc = (the_insn.exp.X_op != O_constant
1522 ? BFD_RELOC_SPARC_LM22 : BFD_RELOC_NONE);
1523 output_insn (insn, &the_insn);
1524 }
1525
1526 if (need_hm10_p)
1527 {
1528 the_insn.opcode = (OR_INSN
1529 | (need_hh22_p ? RS1 (upper_dstreg) : 0)
1530 | RD (upper_dstreg)
1531 | IMMED
1532 | (upper32 & (need_hh22_p ? 0x3ff : 0x1fff)));
1533 the_insn.reloc = (the_insn.exp.X_op != O_constant
1534 ? BFD_RELOC_SPARC_HM10 : BFD_RELOC_NONE);
1535 output_insn (insn, &the_insn);
1536 }
1537
1538 if (need_lo10_p)
1539 {
1540 /* FIXME: One nice optimization to do here is to OR the low part
1541 with the highpart if hi22 isn't needed and the low part is
1542 positive. */
1543 the_insn.opcode = (OR_INSN | (need_hi22_p ? RS1 (dstreg) : 0)
1544 | RD (dstreg)
1545 | IMMED
1546 | (lower32 & (need_hi22_p ? 0x3ff : 0x1fff)));
1547 the_insn.reloc = (the_insn.exp.X_op != O_constant
1548 ? BFD_RELOC_LO10 : BFD_RELOC_NONE);
1549 output_insn (insn, &the_insn);
1550 }
1551
1552 /* If we needed to build the upper part, shift it into place. */
1553 if (need_hh22_p || need_hm10_p)
1554 {
1555 the_insn.opcode = (SLLX_INSN | RS1 (upper_dstreg) | RD (upper_dstreg)
1556 | IMMED | 32);
1557 the_insn.reloc = BFD_RELOC_NONE;
1558 output_insn (insn, &the_insn);
1559 }
1560
1561 /* To get -1 in upper32, we do sethi %hi(~x), r; xor r, -0x400 | x, r. */
1562 if (need_xor10_p)
1563 {
1564 the_insn.opcode = (XOR_INSN | RS1 (dstreg) | RD (dstreg) | IMMED
1565 | 0x1c00 | (lower32 & 0x3ff));
1566 the_insn.reloc = BFD_RELOC_NONE;
1567 output_insn (insn, &the_insn);
1568 }
1569
1570 /* If we needed to build both upper and lower parts, OR them together. */
1571 else if ((need_hh22_p || need_hm10_p) && (need_hi22_p || need_lo10_p))
1572 {
1573 the_insn.opcode = (OR_INSN | RS1 (dstreg) | RS2 (upper_dstreg)
1574 | RD (dstreg));
1575 the_insn.reloc = BFD_RELOC_NONE;
1576 output_insn (insn, &the_insn);
1577 }
1578 }
1579
1580 /* Main entry point to assemble one instruction. */
1582
1583 void
1584 md_assemble (char *str)
1585 {
1586 const struct sparc_opcode *insn;
1587 int special_case;
1588
1589 know (str);
1590 special_case = sparc_ip (str, &insn);
1591 if (insn == NULL)
1592 return;
1593
1594 /* Certain instructions may not appear on delay slots. Check for
1595 these situations. */
1596 if (last_insn != NULL
1597 && (last_insn->flags & F_DELAYED) != 0)
1598 {
1599 /* Before SPARC V9 the effect of having a delayed branch
1600 instruction in the delay slot of a conditional delayed branch
1601 was undefined.
1602
1603 In SPARC V9 DCTI couples are well defined.
1604
1605 However, starting with the UltraSPARC Architecture 2005, DCTI
1606 couples (of all kind) are deprecated and should not be used,
1607 as they may be slow or behave differently to what the
1608 programmer expects. */
1609 if (dcti_couples_detect
1610 && (insn->flags & F_DELAYED) != 0
1611 && ((max_architecture < SPARC_OPCODE_ARCH_V9
1612 && (last_insn->flags & F_CONDBR) != 0)
1613 || max_architecture >= SPARC_OPCODE_ARCH_V9C))
1614 as_warn (_("unpredictable DCTI couple"));
1615
1616
1617 /* We warn about attempts to put a floating point branch in a
1618 delay slot, unless the delay slot has been annulled. */
1619 if ((insn->flags & F_FBR) != 0
1620 /* ??? This test isn't completely accurate. We assume anything with
1621 F_{UNBR,CONDBR,FBR} set is annullable. */
1622 && ((last_insn->flags & (F_UNBR | F_CONDBR | F_FBR)) == 0
1623 || (last_opcode & ANNUL) == 0))
1624 as_warn (_("FP branch in delay slot"));
1625 }
1626
1627 /* SPARC before v8 requires a nop instruction between a floating
1628 point instruction and a floating point branch. SPARCv8 requires
1629 a nop only immediately after FPop2 (fcmp*) instructions.
1630 We insert one automatically, with a warning.
1631 */
1632 if (last_insn != NULL
1633 && (insn->flags & F_FBR) != 0
1634 && (last_insn->flags & F_FLOAT) != 0
1635 && (max_architecture < SPARC_OPCODE_ARCH_V8 ||
1636 (max_architecture < SPARC_OPCODE_ARCH_V9 &&
1637 strncmp(last_insn->name, "fcmp", 4) == 0)))
1638 {
1639 struct sparc_it nop_insn;
1640
1641 nop_insn.opcode = NOP_INSN;
1642 nop_insn.reloc = BFD_RELOC_NONE;
1643 output_insn (insn, &nop_insn);
1644 as_warn (_("FP branch preceded by FP instruction; NOP inserted"));
1645 }
1646
1647 switch (special_case)
1648 {
1649 case SPECIAL_CASE_NONE:
1650 /* Normal insn. */
1651 output_insn (insn, &the_insn);
1652 break;
1653
1654 case SPECIAL_CASE_SETSW:
1655 synthetize_setsw (insn);
1656 break;
1657
1658 case SPECIAL_CASE_SET:
1659 synthetize_setuw (insn);
1660 break;
1661
1662 case SPECIAL_CASE_SETX:
1663 synthetize_setx (insn);
1664 break;
1665
1666 case SPECIAL_CASE_FDIV:
1667 {
1668 int rd = (the_insn.opcode >> 25) & 0x1f;
1669
1670 output_insn (insn, &the_insn);
1671
1672 /* According to information leaked from Sun, the "fdiv" instructions
1673 on early SPARC machines would produce incorrect results sometimes.
1674 The workaround is to add an fmovs of the destination register to
1675 itself just after the instruction. This was true on machines
1676 with Weitek 1165 float chips, such as the Sun-4/260 and /280. */
1677 gas_assert (the_insn.reloc == BFD_RELOC_NONE);
1678 the_insn.opcode = FMOVS_INSN | rd | RD (rd);
1679 output_insn (insn, &the_insn);
1680 return;
1681 }
1682
1683 default:
1684 as_fatal (_("failed special case insn sanity check"));
1685 }
1686 }
1687
1688 static const char *
1689 get_hwcap_name (bfd_uint64_t mask)
1690 {
1691 if (mask & HWCAP_MUL32)
1692 return "mul32";
1693 if (mask & HWCAP_DIV32)
1694 return "div32";
1695 if (mask & HWCAP_FSMULD)
1696 return "fsmuld";
1697 if (mask & HWCAP_V8PLUS)
1698 return "v8plus";
1699 if (mask & HWCAP_POPC)
1700 return "popc";
1701 if (mask & HWCAP_VIS)
1702 return "vis";
1703 if (mask & HWCAP_VIS2)
1704 return "vis2";
1705 if (mask & HWCAP_ASI_BLK_INIT)
1706 return "ASIBlkInit";
1707 if (mask & HWCAP_FMAF)
1708 return "fmaf";
1709 if (mask & HWCAP_VIS3)
1710 return "vis3";
1711 if (mask & HWCAP_HPC)
1712 return "hpc";
1713 if (mask & HWCAP_RANDOM)
1714 return "random";
1715 if (mask & HWCAP_TRANS)
1716 return "trans";
1717 if (mask & HWCAP_FJFMAU)
1718 return "fjfmau";
1719 if (mask & HWCAP_IMA)
1720 return "ima";
1721 if (mask & HWCAP_ASI_CACHE_SPARING)
1722 return "cspare";
1723 if (mask & HWCAP_AES)
1724 return "aes";
1725 if (mask & HWCAP_DES)
1726 return "des";
1727 if (mask & HWCAP_KASUMI)
1728 return "kasumi";
1729 if (mask & HWCAP_CAMELLIA)
1730 return "camellia";
1731 if (mask & HWCAP_MD5)
1732 return "md5";
1733 if (mask & HWCAP_SHA1)
1734 return "sha1";
1735 if (mask & HWCAP_SHA256)
1736 return "sha256";
1737 if (mask & HWCAP_SHA512)
1738 return "sha512";
1739 if (mask & HWCAP_MPMUL)
1740 return "mpmul";
1741 if (mask & HWCAP_MONT)
1742 return "mont";
1743 if (mask & HWCAP_PAUSE)
1744 return "pause";
1745 if (mask & HWCAP_CBCOND)
1746 return "cbcond";
1747 if (mask & HWCAP_CRC32C)
1748 return "crc32c";
1749
1750 mask = mask >> 32;
1751 if (mask & HWCAP2_FJATHPLUS)
1752 return "fjathplus";
1753 if (mask & HWCAP2_VIS3B)
1754 return "vis3b";
1755 if (mask & HWCAP2_ADP)
1756 return "adp";
1757 if (mask & HWCAP2_SPARC5)
1758 return "sparc5";
1759 if (mask & HWCAP2_MWAIT)
1760 return "mwait";
1761 if (mask & HWCAP2_XMPMUL)
1762 return "xmpmul";
1763 if (mask & HWCAP2_XMONT)
1764 return "xmont";
1765 if (mask & HWCAP2_NSEC)
1766 return "nsec";
1767 if (mask & HWCAP2_SPARC6)
1768 return "sparc6";
1769 if (mask & HWCAP2_ONADDSUB)
1770 return "onaddsub";
1771 if (mask & HWCAP2_ONMUL)
1772 return "onmul";
1773 if (mask & HWCAP2_ONDIV)
1774 return "ondiv";
1775 if (mask & HWCAP2_DICTUNP)
1776 return "dictunp";
1777 if (mask & HWCAP2_FPCMPSHL)
1778 return "fpcmpshl";
1779 if (mask & HWCAP2_RLE)
1780 return "rle";
1781 if (mask & HWCAP2_SHA3)
1782 return "sha3";
1783
1784 return "UNKNOWN";
1785 }
1786
1787 /* Subroutine of md_assemble to do the actual parsing. */
1788
1789 static int
1790 sparc_ip (char *str, const struct sparc_opcode **pinsn)
1791 {
1792 const char *error_message = "";
1793 char *s;
1794 const char *args;
1795 char c;
1796 const struct sparc_opcode *insn;
1797 char *argsStart;
1798 unsigned long opcode;
1799 unsigned int mask = 0;
1800 int match = 0;
1801 int comma = 0;
1802 int v9_arg_p;
1803 int special_case = SPECIAL_CASE_NONE;
1804 const sparc_asi *sasi = NULL;
1805
1806 s = str;
1807 if (ISLOWER (*s))
1808 {
1809 do
1810 ++s;
1811 while (ISLOWER (*s) || ISDIGIT (*s) || *s == '_');
1812 }
1813
1814 switch (*s)
1815 {
1816 case '\0':
1817 break;
1818
1819 case ',':
1820 comma = 1;
1821 /* Fall through. */
1822
1823 case ' ':
1824 *s++ = '\0';
1825 break;
1826
1827 default:
1828 as_bad (_("Unknown opcode: `%s'"), str);
1829 *pinsn = NULL;
1830 return special_case;
1831 }
1832 insn = (struct sparc_opcode *) hash_find (op_hash, str);
1833 *pinsn = insn;
1834 if (insn == NULL)
1835 {
1836 as_bad (_("Unknown opcode: `%s'"), str);
1837 return special_case;
1838 }
1839 if (comma)
1840 {
1841 *--s = ',';
1842 }
1843
1844 argsStart = s;
1845 for (;;)
1846 {
1847 opcode = insn->match;
1848 memset (&the_insn, '\0', sizeof (the_insn));
1849 the_insn.reloc = BFD_RELOC_NONE;
1850 v9_arg_p = 0;
1851
1852 /* Build the opcode, checking as we go to make sure that the
1853 operands match. */
1854 for (args = insn->args;; ++args)
1855 {
1856 switch (*args)
1857 {
1858 case 'K':
1859 {
1860 int kmask = 0;
1861
1862 /* Parse a series of masks. */
1863 if (*s == '#')
1864 {
1865 while (*s == '#')
1866 {
1867 int jmask;
1868
1869 if (! parse_keyword_arg (sparc_encode_membar, &s,
1870 &jmask))
1871 {
1872 error_message = _(": invalid membar mask name");
1873 goto error;
1874 }
1875 kmask |= jmask;
1876 while (*s == ' ')
1877 ++s;
1878 if (*s == '|' || *s == '+')
1879 ++s;
1880 while (*s == ' ')
1881 ++s;
1882 }
1883 }
1884 else
1885 {
1886 if (! parse_const_expr_arg (&s, &kmask))
1887 {
1888 error_message = _(": invalid membar mask expression");
1889 goto error;
1890 }
1891 if (kmask < 0 || kmask > 127)
1892 {
1893 error_message = _(": invalid membar mask number");
1894 goto error;
1895 }
1896 }
1897
1898 opcode |= MEMBAR (kmask);
1899 continue;
1900 }
1901
1902 case '3':
1903 {
1904 int smask = 0;
1905
1906 if (! parse_const_expr_arg (&s, &smask))
1907 {
1908 error_message = _(": invalid siam mode expression");
1909 goto error;
1910 }
1911 if (smask < 0 || smask > 7)
1912 {
1913 error_message = _(": invalid siam mode number");
1914 goto error;
1915 }
1916 opcode |= smask;
1917 continue;
1918 }
1919
1920 case '*':
1921 {
1922 int fcn = 0;
1923
1924 /* Parse a prefetch function. */
1925 if (*s == '#')
1926 {
1927 if (! parse_keyword_arg (sparc_encode_prefetch, &s, &fcn))
1928 {
1929 error_message = _(": invalid prefetch function name");
1930 goto error;
1931 }
1932 }
1933 else
1934 {
1935 if (! parse_const_expr_arg (&s, &fcn))
1936 {
1937 error_message = _(": invalid prefetch function expression");
1938 goto error;
1939 }
1940 if (fcn < 0 || fcn > 31)
1941 {
1942 error_message = _(": invalid prefetch function number");
1943 goto error;
1944 }
1945 }
1946 opcode |= RD (fcn);
1947 continue;
1948 }
1949
1950 case '!':
1951 case '?':
1952 /* Parse a sparc64 privileged register. */
1953 if (*s == '%')
1954 {
1955 struct priv_reg_entry *p;
1956 unsigned int len = 9999999; /* Init to make gcc happy. */
1957
1958 s += 1;
1959 for (p = priv_reg_table; p->name; p++)
1960 if (p->name[0] == s[0])
1961 {
1962 len = strlen (p->name);
1963 if (strncmp (p->name, s, len) == 0)
1964 break;
1965 }
1966
1967 if (!p->name)
1968 {
1969 error_message = _(": unrecognizable privileged register");
1970 goto error;
1971 }
1972
1973 if (((opcode >> (*args == '?' ? 14 : 25)) & 0x1f) != (unsigned) p->regnum)
1974 {
1975 error_message = _(": unrecognizable privileged register");
1976 goto error;
1977 }
1978
1979 s += len;
1980 continue;
1981 }
1982 else
1983 {
1984 error_message = _(": unrecognizable privileged register");
1985 goto error;
1986 }
1987
1988 case '$':
1989 case '%':
1990 /* Parse a sparc64 hyperprivileged register. */
1991 if (*s == '%')
1992 {
1993 struct priv_reg_entry *p;
1994 unsigned int len = 9999999; /* Init to make gcc happy. */
1995
1996 s += 1;
1997 for (p = hpriv_reg_table; p->name; p++)
1998 if (p->name[0] == s[0])
1999 {
2000 len = strlen (p->name);
2001 if (strncmp (p->name, s, len) == 0)
2002 break;
2003 }
2004
2005 if (!p->name)
2006 {
2007 error_message = _(": unrecognizable hyperprivileged register");
2008 goto error;
2009 }
2010
2011 if (((opcode >> (*args == '$' ? 14 : 25)) & 0x1f) != (unsigned) p->regnum)
2012 {
2013 error_message = _(": unrecognizable hyperprivileged register");
2014 goto error;
2015 }
2016
2017 s += len;
2018 continue;
2019 }
2020 else
2021 {
2022 error_message = _(": unrecognizable hyperprivileged register");
2023 goto error;
2024 }
2025
2026 case '_':
2027 case '/':
2028 /* Parse a v9a or later ancillary state register. */
2029 if (*s == '%')
2030 {
2031 struct priv_reg_entry *p;
2032 unsigned int len = 9999999; /* Init to make gcc happy. */
2033
2034 s += 1;
2035 for (p = v9a_asr_table; p->name; p++)
2036 if (p->name[0] == s[0])
2037 {
2038 len = strlen (p->name);
2039 if (strncmp (p->name, s, len) == 0)
2040 break;
2041 }
2042
2043 if (!p->name)
2044 {
2045 error_message = _(": unrecognizable ancillary state register");
2046 goto error;
2047 }
2048
2049 if (((opcode >> (*args == '/' ? 14 : 25)) & 0x1f) != (unsigned) p->regnum)
2050 {
2051 error_message = _(": unrecognizable ancillary state register");
2052 goto error;
2053 }
2054
2055 s += len;
2056 continue;
2057 }
2058 else
2059 {
2060 error_message = _(": unrecognizable ancillary state register");
2061 goto error;
2062 }
2063
2064 case 'M':
2065 case 'm':
2066 if (strncmp (s, "%asr", 4) == 0)
2067 {
2068 s += 4;
2069
2070 if (ISDIGIT (*s))
2071 {
2072 long num = 0;
2073
2074 while (ISDIGIT (*s))
2075 {
2076 num = num * 10 + *s - '0';
2077 ++s;
2078 }
2079
2080 /* We used to check here for the asr number to
2081 be between 16 and 31 in V9 and later, as
2082 mandated by the section C.1.1 "Register
2083 Names" in the SPARC spec. However, we
2084 decided to remove this restriction as a) it
2085 introduces problems when new V9 asr registers
2086 are introduced, b) the Solaris assembler
2087 doesn't implement this restriction and c) the
2088 restriction will go away in future revisions
2089 of the Oracle SPARC Architecture. */
2090
2091 if (num < 0 || 31 < num)
2092 {
2093 error_message = _(": asr number must be between 0 and 31");
2094 goto error;
2095 }
2096
2097 opcode |= (*args == 'M' ? RS1 (num) : RD (num));
2098 continue;
2099 }
2100 else
2101 {
2102 error_message = _(": expecting %asrN");
2103 goto error;
2104 }
2105 } /* if %asr */
2106 break;
2107
2108 case 'I':
2109 the_insn.reloc = BFD_RELOC_SPARC_11;
2110 goto immediate;
2111
2112 case 'j':
2113 the_insn.reloc = BFD_RELOC_SPARC_10;
2114 goto immediate;
2115
2116 case ')':
2117 if (*s == ' ')
2118 s++;
2119 if ((s[0] == '0' && s[1] == 'x' && ISXDIGIT (s[2]))
2120 || ISDIGIT (*s))
2121 {
2122 long num = 0;
2123
2124 if (s[0] == '0' && s[1] == 'x')
2125 {
2126 s += 2;
2127 while (ISXDIGIT (*s))
2128 {
2129 num <<= 4;
2130 num |= hex_value (*s);
2131 ++s;
2132 }
2133 }
2134 else
2135 {
2136 while (ISDIGIT (*s))
2137 {
2138 num = num * 10 + *s - '0';
2139 ++s;
2140 }
2141 }
2142 if (num < 0 || num > 31)
2143 {
2144 error_message = _(": crypto immediate must be between 0 and 31");
2145 goto error;
2146 }
2147
2148 opcode |= RS3 (num);
2149 continue;
2150 }
2151 else
2152 {
2153 error_message = _(": expecting crypto immediate");
2154 goto error;
2155 }
2156
2157 case 'X':
2158 /* V8 systems don't understand BFD_RELOC_SPARC_5. */
2159 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2160 the_insn.reloc = BFD_RELOC_SPARC_5;
2161 else
2162 the_insn.reloc = BFD_RELOC_SPARC13;
2163 /* These fields are unsigned, but for upward compatibility,
2164 allow negative values as well. */
2165 goto immediate;
2166
2167 case 'Y':
2168 /* V8 systems don't understand BFD_RELOC_SPARC_6. */
2169 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2170 the_insn.reloc = BFD_RELOC_SPARC_6;
2171 else
2172 the_insn.reloc = BFD_RELOC_SPARC13;
2173 /* These fields are unsigned, but for upward compatibility,
2174 allow negative values as well. */
2175 goto immediate;
2176
2177 case 'k':
2178 the_insn.reloc = /* RELOC_WDISP2_14 */ BFD_RELOC_SPARC_WDISP16;
2179 the_insn.pcrel = 1;
2180 goto immediate;
2181
2182 case '=':
2183 the_insn.reloc = /* RELOC_WDISP2_8 */ BFD_RELOC_SPARC_WDISP10;
2184 the_insn.pcrel = 1;
2185 goto immediate;
2186
2187 case 'G':
2188 the_insn.reloc = BFD_RELOC_SPARC_WDISP19;
2189 the_insn.pcrel = 1;
2190 goto immediate;
2191
2192 case 'N':
2193 if (*s == 'p' && s[1] == 'n')
2194 {
2195 s += 2;
2196 continue;
2197 }
2198 break;
2199
2200 case 'T':
2201 if (*s == 'p' && s[1] == 't')
2202 {
2203 s += 2;
2204 continue;
2205 }
2206 break;
2207
2208 case 'z':
2209 if (*s == ' ')
2210 {
2211 ++s;
2212 }
2213 if ((strncmp (s, "%icc", 4) == 0)
2214 || (sparc_arch_size == 32 && strncmp (s, "%ncc", 4) == 0))
2215 {
2216 s += 4;
2217 continue;
2218 }
2219 break;
2220
2221 case 'Z':
2222 if (*s == ' ')
2223 {
2224 ++s;
2225 }
2226 if ((strncmp (s, "%xcc", 4) == 0)
2227 || (sparc_arch_size == 64 && strncmp (s, "%ncc", 4) == 0))
2228 {
2229 s += 4;
2230 continue;
2231 }
2232 break;
2233
2234 case '6':
2235 if (*s == ' ')
2236 {
2237 ++s;
2238 }
2239 if (strncmp (s, "%fcc0", 5) == 0)
2240 {
2241 s += 5;
2242 continue;
2243 }
2244 break;
2245
2246 case '7':
2247 if (*s == ' ')
2248 {
2249 ++s;
2250 }
2251 if (strncmp (s, "%fcc1", 5) == 0)
2252 {
2253 s += 5;
2254 continue;
2255 }
2256 break;
2257
2258 case '8':
2259 if (*s == ' ')
2260 {
2261 ++s;
2262 }
2263 if (strncmp (s, "%fcc2", 5) == 0)
2264 {
2265 s += 5;
2266 continue;
2267 }
2268 break;
2269
2270 case '9':
2271 if (*s == ' ')
2272 {
2273 ++s;
2274 }
2275 if (strncmp (s, "%fcc3", 5) == 0)
2276 {
2277 s += 5;
2278 continue;
2279 }
2280 break;
2281
2282 case 'P':
2283 if (strncmp (s, "%pc", 3) == 0)
2284 {
2285 s += 3;
2286 continue;
2287 }
2288 break;
2289
2290 case 'W':
2291 if (strncmp (s, "%tick", 5) == 0)
2292 {
2293 s += 5;
2294 continue;
2295 }
2296 break;
2297
2298 case '\0': /* End of args. */
2299 if (s[0] == ',' && s[1] == '%')
2300 {
2301 char *s1;
2302 int npar = 0;
2303 const struct perc_entry *p;
2304
2305 for (p = perc_table; p->type != perc_entry_none; p++)
2306 if ((p->type == perc_entry_post_pop || p->type == perc_entry_reg)
2307 && strncmp (s + 2, p->name, p->len) == 0)
2308 break;
2309 if (p->type == perc_entry_none || p->type == perc_entry_reg)
2310 break;
2311
2312 if (s[p->len + 2] != '(')
2313 {
2314 as_bad (_("Illegal operands: %%%s requires arguments in ()"), p->name);
2315 return special_case;
2316 }
2317
2318 if (! (p->pop->flags & F_POP_TLS_CALL)
2319 && the_insn.reloc != BFD_RELOC_NONE)
2320 {
2321 as_bad (_("Illegal operands: %%%s cannot be used together with other relocs in the insn ()"),
2322 p->name);
2323 return special_case;
2324 }
2325
2326 if ((p->pop->flags & F_POP_TLS_CALL)
2327 && (the_insn.reloc != BFD_RELOC_32_PCREL_S2
2328 || the_insn.exp.X_add_number != 0
2329 || the_insn.exp.X_add_symbol
2330 != symbol_find_or_make ("__tls_get_addr")))
2331 {
2332 as_bad (_("Illegal operands: %%%s can be only used with call __tls_get_addr"),
2333 p->name);
2334 return special_case;
2335 }
2336
2337 the_insn.reloc = p->pop->reloc;
2338 memset (&the_insn.exp, 0, sizeof (the_insn.exp));
2339 s += p->len + 3;
2340
2341 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++)
2342 if (*s1 == '(')
2343 npar++;
2344 else if (*s1 == ')')
2345 {
2346 if (!npar)
2347 break;
2348 npar--;
2349 }
2350
2351 if (*s1 != ')')
2352 {
2353 as_bad (_("Illegal operands: %%%s requires arguments in ()"), p->name);
2354 return special_case;
2355 }
2356
2357 *s1 = '\0';
2358 (void) get_expression (s);
2359 *s1 = ')';
2360 s = s1 + 1;
2361 }
2362 if (*s == '\0')
2363 match = 1;
2364 break;
2365
2366 case '+':
2367 if (*s == '+')
2368 {
2369 ++s;
2370 continue;
2371 }
2372 if (*s == '-')
2373 {
2374 continue;
2375 }
2376 break;
2377
2378 case '[': /* These must match exactly. */
2379 case ']':
2380 case ',':
2381 case ' ':
2382 if (*s++ == *args)
2383 continue;
2384 break;
2385
2386 case '#': /* Must be at least one digit. */
2387 if (ISDIGIT (*s++))
2388 {
2389 while (ISDIGIT (*s))
2390 {
2391 ++s;
2392 }
2393 continue;
2394 }
2395 break;
2396
2397 case 'C': /* Coprocessor state register. */
2398 if (strncmp (s, "%csr", 4) == 0)
2399 {
2400 s += 4;
2401 continue;
2402 }
2403 break;
2404
2405 case 'b': /* Next operand is a coprocessor register. */
2406 case 'c':
2407 case 'D':
2408 if (*s++ == '%' && *s++ == 'c' && ISDIGIT (*s))
2409 {
2410 mask = *s++;
2411 if (ISDIGIT (*s))
2412 {
2413 mask = 10 * (mask - '0') + (*s++ - '0');
2414 if (mask >= 32)
2415 {
2416 break;
2417 }
2418 }
2419 else
2420 {
2421 mask -= '0';
2422 }
2423 switch (*args)
2424 {
2425
2426 case 'b':
2427 opcode |= mask << 14;
2428 continue;
2429
2430 case 'c':
2431 opcode |= mask;
2432 continue;
2433
2434 case 'D':
2435 opcode |= mask << 25;
2436 continue;
2437 }
2438 }
2439 break;
2440
2441 case 'r': /* next operand must be a register */
2442 case 'O':
2443 case '1':
2444 case '2':
2445 case 'd':
2446 if (*s++ == '%')
2447 {
2448 switch (c = *s++)
2449 {
2450
2451 case 'f': /* frame pointer */
2452 if (*s++ == 'p')
2453 {
2454 mask = 0x1e;
2455 break;
2456 }
2457 goto error;
2458
2459 case 'g': /* global register */
2460 c = *s++;
2461 if (isoctal (c))
2462 {
2463 mask = c - '0';
2464 break;
2465 }
2466 goto error;
2467
2468 case 'i': /* in register */
2469 c = *s++;
2470 if (isoctal (c))
2471 {
2472 mask = c - '0' + 24;
2473 break;
2474 }
2475 goto error;
2476
2477 case 'l': /* local register */
2478 c = *s++;
2479 if (isoctal (c))
2480 {
2481 mask = (c - '0' + 16);
2482 break;
2483 }
2484 goto error;
2485
2486 case 'o': /* out register */
2487 c = *s++;
2488 if (isoctal (c))
2489 {
2490 mask = (c - '0' + 8);
2491 break;
2492 }
2493 goto error;
2494
2495 case 's': /* stack pointer */
2496 if (*s++ == 'p')
2497 {
2498 mask = 0xe;
2499 break;
2500 }
2501 goto error;
2502
2503 case 'r': /* any register */
2504 if (!ISDIGIT ((c = *s++)))
2505 {
2506 goto error;
2507 }
2508 /* FALLTHROUGH */
2509 case '0':
2510 case '1':
2511 case '2':
2512 case '3':
2513 case '4':
2514 case '5':
2515 case '6':
2516 case '7':
2517 case '8':
2518 case '9':
2519 if (ISDIGIT (*s))
2520 {
2521 if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32)
2522 {
2523 goto error;
2524 }
2525 }
2526 else
2527 {
2528 c -= '0';
2529 }
2530 mask = c;
2531 break;
2532
2533 default:
2534 goto error;
2535 }
2536
2537 if ((mask & ~1) == 2 && sparc_arch_size == 64
2538 && no_undeclared_regs && ! globals[mask])
2539 as_bad (_("detected global register use not covered by .register pseudo-op"));
2540
2541 /* Got the register, now figure out where
2542 it goes in the opcode. */
2543 switch (*args)
2544 {
2545 case '1':
2546 opcode |= mask << 14;
2547 continue;
2548
2549 case '2':
2550 opcode |= mask;
2551 continue;
2552
2553 case 'd':
2554 opcode |= mask << 25;
2555 continue;
2556
2557 case 'r':
2558 opcode |= (mask << 25) | (mask << 14);
2559 continue;
2560
2561 case 'O':
2562 opcode |= (mask << 25) | (mask << 0);
2563 continue;
2564 }
2565 }
2566 break;
2567
2568 case 'e': /* next operand is a floating point register */
2569 case 'v':
2570 case 'V':
2571 case ';':
2572
2573 case 'f':
2574 case 'B':
2575 case 'R':
2576 case ':':
2577 case '\'':
2578
2579 case '4':
2580 case '5':
2581
2582 case 'g':
2583 case 'H':
2584 case 'J':
2585 case '}':
2586 case '^':
2587 {
2588 char format;
2589
2590 if (*s++ == '%'
2591 && ((format = *s) == 'f'
2592 || format == 'd'
2593 || format == 'q')
2594 && ISDIGIT (*++s))
2595 {
2596 for (mask = 0; ISDIGIT (*s); ++s)
2597 {
2598 mask = 10 * mask + (*s - '0');
2599 } /* read the number */
2600
2601 if ((*args == 'v'
2602 || *args == 'B'
2603 || *args == '5'
2604 || *args == 'H'
2605 || *args == '\''
2606 || format == 'd')
2607 && (mask & 1))
2608 {
2609 /* register must be even numbered */
2610 break;
2611 }
2612
2613 if ((*args == 'V'
2614 || *args == 'R'
2615 || *args == 'J'
2616 || format == 'q')
2617 && (mask & 3))
2618 {
2619 /* register must be multiple of 4 */
2620 break;
2621 }
2622
2623 if ((*args == ':'
2624 || *args == ';'
2625 || *args == '^')
2626 && (mask & 7))
2627 {
2628 /* register must be multiple of 8 */
2629 break;
2630 }
2631
2632 if (*args == '\'' && mask < 48)
2633 {
2634 /* register must be higher or equal than %f48 */
2635 break;
2636 }
2637
2638 if (mask >= 64)
2639 {
2640 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2641 error_message = _(": There are only 64 f registers; [0-63]");
2642 else
2643 error_message = _(": There are only 32 f registers; [0-31]");
2644 goto error;
2645 } /* on error */
2646 else if (mask >= 32)
2647 {
2648 if (SPARC_OPCODE_ARCH_V9_P (max_architecture))
2649 {
2650 if (*args == 'e' || *args == 'f' || *args == 'g')
2651 {
2652 error_message
2653 = _(": There are only 32 single precision f registers; [0-31]");
2654 goto error;
2655 }
2656 v9_arg_p = 1;
2657 mask -= 31; /* wrap high bit */
2658 }
2659 else
2660 {
2661 error_message = _(": There are only 32 f registers; [0-31]");
2662 goto error;
2663 }
2664 }
2665 }
2666 else
2667 {
2668 break;
2669 } /* if not an 'f' register. */
2670
2671 if (*args == '}' && mask != RS2 (opcode))
2672 {
2673 error_message
2674 = _(": Instruction requires frs2 and frsd must be the same register");
2675 goto error;
2676 }
2677
2678 switch (*args)
2679 {
2680 case 'v':
2681 case 'V':
2682 case 'e':
2683 case ';':
2684 opcode |= RS1 (mask);
2685 continue;
2686
2687 case 'f':
2688 case 'B':
2689 case 'R':
2690 case ':':
2691 opcode |= RS2 (mask);
2692 continue;
2693
2694 case '\'':
2695 opcode |= RS2 (mask & 0xe);
2696 continue;
2697
2698 case '4':
2699 case '5':
2700 opcode |= RS3 (mask);
2701 continue;
2702
2703 case 'g':
2704 case 'H':
2705 case 'J':
2706 case '}':
2707 case '^':
2708 opcode |= RD (mask);
2709 continue;
2710 } /* Pack it in. */
2711
2712 know (0);
2713 break;
2714 } /* float arg */
2715
2716 case 'F':
2717 if (strncmp (s, "%fsr", 4) == 0)
2718 {
2719 s += 4;
2720 continue;
2721 }
2722 break;
2723
2724 case '(':
2725 if (strncmp (s, "%efsr", 5) == 0)
2726 {
2727 s += 5;
2728 continue;
2729 }
2730 break;
2731
2732 case '0': /* 64 bit immediate (set, setsw, setx insn) */
2733 the_insn.reloc = BFD_RELOC_NONE; /* reloc handled elsewhere */
2734 goto immediate;
2735
2736 case 'l': /* 22 bit PC relative immediate */
2737 the_insn.reloc = BFD_RELOC_SPARC_WDISP22;
2738 the_insn.pcrel = 1;
2739 goto immediate;
2740
2741 case 'L': /* 30 bit immediate */
2742 the_insn.reloc = BFD_RELOC_32_PCREL_S2;
2743 the_insn.pcrel = 1;
2744 goto immediate;
2745
2746 case 'h':
2747 case 'n': /* 22 bit immediate */
2748 the_insn.reloc = BFD_RELOC_SPARC22;
2749 goto immediate;
2750
2751 case 'i': /* 13 bit immediate */
2752 the_insn.reloc = BFD_RELOC_SPARC13;
2753
2754 /* fallthrough */
2755
2756 immediate:
2757 if (*s == ' ')
2758 s++;
2759
2760 {
2761 char *s1;
2762 const char *op_arg = NULL;
2763 static expressionS op_exp;
2764 bfd_reloc_code_real_type old_reloc = the_insn.reloc;
2765
2766 /* Check for %hi, etc. */
2767 if (*s == '%')
2768 {
2769 const struct perc_entry *p;
2770
2771 for (p = perc_table; p->type != perc_entry_none; p++)
2772 if ((p->type == perc_entry_imm_pop || p->type == perc_entry_reg)
2773 && strncmp (s + 1, p->name, p->len) == 0)
2774 break;
2775 if (p->type == perc_entry_none || p->type == perc_entry_reg)
2776 break;
2777
2778 if (s[p->len + 1] != '(')
2779 {
2780 as_bad (_("Illegal operands: %%%s requires arguments in ()"), p->name);
2781 return special_case;
2782 }
2783
2784 op_arg = p->name;
2785 the_insn.reloc = p->pop->reloc;
2786 s += p->len + 2;
2787 v9_arg_p = p->pop->flags & F_POP_V9;
2788 }
2789
2790 /* Note that if the get_expression() fails, we will still
2791 have created U entries in the symbol table for the
2792 'symbols' in the input string. Try not to create U
2793 symbols for registers, etc. */
2794
2795 /* This stuff checks to see if the expression ends in
2796 +%reg. If it does, it removes the register from
2797 the expression, and re-sets 's' to point to the
2798 right place. */
2799
2800 if (op_arg)
2801 {
2802 int npar = 0;
2803
2804 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++)
2805 if (*s1 == '(')
2806 npar++;
2807 else if (*s1 == ')')
2808 {
2809 if (!npar)
2810 break;
2811 npar--;
2812 }
2813
2814 if (*s1 != ')')
2815 {
2816 as_bad (_("Illegal operands: %%%s requires arguments in ()"), op_arg);
2817 return special_case;
2818 }
2819
2820 *s1 = '\0';
2821 (void) get_expression (s);
2822 *s1 = ')';
2823 if (expr_end != s1)
2824 {
2825 as_bad (_("Expression inside %%%s could not be parsed"), op_arg);
2826 return special_case;
2827 }
2828 s = s1 + 1;
2829 if (*s == ',' || *s == ']' || !*s)
2830 continue;
2831 if (*s != '+' && *s != '-')
2832 {
2833 as_bad (_("Illegal operands: Can't do arithmetics other than + and - involving %%%s()"), op_arg);
2834 return special_case;
2835 }
2836 *s1 = '0';
2837 s = s1;
2838 op_exp = the_insn.exp;
2839 memset (&the_insn.exp, 0, sizeof (the_insn.exp));
2840 }
2841
2842 for (s1 = s; *s1 && *s1 != ',' && *s1 != ']'; s1++)
2843 ;
2844
2845 if (s1 != s && ISDIGIT (s1[-1]))
2846 {
2847 if (s1[-2] == '%' && s1[-3] == '+')
2848 s1 -= 3;
2849 else if (strchr ("golir0123456789", s1[-2]) && s1[-3] == '%' && s1[-4] == '+')
2850 s1 -= 4;
2851 else if (s1[-3] == 'r' && s1[-4] == '%' && s1[-5] == '+')
2852 s1 -= 5;
2853 else
2854 s1 = NULL;
2855 if (s1)
2856 {
2857 *s1 = '\0';
2858 if (op_arg && s1 == s + 1)
2859 the_insn.exp.X_op = O_absent;
2860 else
2861 (void) get_expression (s);
2862 *s1 = '+';
2863 if (op_arg)
2864 *s = ')';
2865 s = s1;
2866 }
2867 }
2868 else
2869 s1 = NULL;
2870
2871 if (!s1)
2872 {
2873 (void) get_expression (s);
2874 if (op_arg)
2875 *s = ')';
2876 s = expr_end;
2877 }
2878
2879 if (op_arg)
2880 {
2881 the_insn.exp2 = the_insn.exp;
2882 the_insn.exp = op_exp;
2883 if (the_insn.exp2.X_op == O_absent)
2884 the_insn.exp2.X_op = O_illegal;
2885 else if (the_insn.exp.X_op == O_absent)
2886 {
2887 the_insn.exp = the_insn.exp2;
2888 the_insn.exp2.X_op = O_illegal;
2889 }
2890 else if (the_insn.exp.X_op == O_constant)
2891 {
2892 valueT val = the_insn.exp.X_add_number;
2893 switch (the_insn.reloc)
2894 {
2895 default:
2896 break;
2897
2898 case BFD_RELOC_SPARC_HH22:
2899 val = BSR (val, 32);
2900 /* Fall through. */
2901
2902 case BFD_RELOC_SPARC_LM22:
2903 case BFD_RELOC_HI22:
2904 val = (val >> 10) & 0x3fffff;
2905 break;
2906
2907 case BFD_RELOC_SPARC_HM10:
2908 val = BSR (val, 32);
2909 /* Fall through. */
2910
2911 case BFD_RELOC_LO10:
2912 val &= 0x3ff;
2913 break;
2914
2915 case BFD_RELOC_SPARC_H34:
2916 val >>= 12;
2917 val &= 0x3fffff;
2918 break;
2919
2920 case BFD_RELOC_SPARC_H44:
2921 val >>= 22;
2922 val &= 0x3fffff;
2923 break;
2924
2925 case BFD_RELOC_SPARC_M44:
2926 val >>= 12;
2927 val &= 0x3ff;
2928 break;
2929
2930 case BFD_RELOC_SPARC_L44:
2931 val &= 0xfff;
2932 break;
2933
2934 case BFD_RELOC_SPARC_HIX22:
2935 val = ~val;
2936 val = (val >> 10) & 0x3fffff;
2937 break;
2938
2939 case BFD_RELOC_SPARC_LOX10:
2940 val = (val & 0x3ff) | 0x1c00;
2941 break;
2942 }
2943 the_insn.exp = the_insn.exp2;
2944 the_insn.exp.X_add_number += val;
2945 the_insn.exp2.X_op = O_illegal;
2946 the_insn.reloc = old_reloc;
2947 }
2948 else if (the_insn.exp2.X_op != O_constant)
2949 {
2950 as_bad (_("Illegal operands: Can't add non-constant expression to %%%s()"), op_arg);
2951 return special_case;
2952 }
2953 else
2954 {
2955 if (old_reloc != BFD_RELOC_SPARC13
2956 || the_insn.reloc != BFD_RELOC_LO10
2957 || sparc_arch_size != 64
2958 || sparc_pic_code)
2959 {
2960 as_bad (_("Illegal operands: Can't do arithmetics involving %%%s() of a relocatable symbol"), op_arg);
2961 return special_case;
2962 }
2963 the_insn.reloc = BFD_RELOC_SPARC_OLO10;
2964 }
2965 }
2966 }
2967 /* Check for constants that don't require emitting a reloc. */
2968 if (the_insn.exp.X_op == O_constant
2969 && the_insn.exp.X_add_symbol == 0
2970 && the_insn.exp.X_op_symbol == 0)
2971 {
2972 /* For pc-relative call instructions, we reject
2973 constants to get better code. */
2974 if (the_insn.pcrel
2975 && the_insn.reloc == BFD_RELOC_32_PCREL_S2
2976 && in_signed_range (the_insn.exp.X_add_number, 0x3fff))
2977 {
2978 error_message = _(": PC-relative operand can't be a constant");
2979 goto error;
2980 }
2981
2982 if (the_insn.reloc >= BFD_RELOC_SPARC_TLS_GD_HI22
2983 && the_insn.reloc <= BFD_RELOC_SPARC_TLS_TPOFF64)
2984 {
2985 error_message = _(": TLS operand can't be a constant");
2986 goto error;
2987 }
2988
2989 /* Constants that won't fit are checked in md_apply_fix
2990 and bfd_install_relocation.
2991 ??? It would be preferable to install the constants
2992 into the insn here and save having to create a fixS
2993 for each one. There already exists code to handle
2994 all the various cases (e.g. in md_apply_fix and
2995 bfd_install_relocation) so duplicating all that code
2996 here isn't right. */
2997
2998 /* This is a special case to handle cbcond instructions
2999 properly, which can need two relocations. The first
3000 one is for the 5-bit immediate field and the latter
3001 is going to be for the WDISP10 branch part. We
3002 handle the R_SPARC_5 immediate directly here so that
3003 we don't need to add support for multiple relocations
3004 in one instruction just yet. */
3005 if (the_insn.reloc == BFD_RELOC_SPARC_5
3006 && ((insn->match & OP(0x3)) == 0))
3007 {
3008 valueT val = the_insn.exp.X_add_number;
3009
3010 the_insn.reloc = BFD_RELOC_NONE;
3011 if (! in_bitfield_range (val, 0x1f))
3012 {
3013 error_message = _(": Immediate value in cbcond is out of range.");
3014 goto error;
3015 }
3016 opcode |= val & 0x1f;
3017 }
3018 }
3019
3020 continue;
3021
3022 case 'a':
3023 if (*s++ == 'a')
3024 {
3025 opcode |= ANNUL;
3026 continue;
3027 }
3028 break;
3029
3030 case 'A':
3031 {
3032 int asi = 0;
3033
3034 /* Parse an asi. */
3035 if (*s == '#')
3036 {
3037 if (! parse_sparc_asi (&s, &sasi))
3038 {
3039 error_message = _(": invalid ASI name");
3040 goto error;
3041 }
3042 asi = sasi->value;
3043 }
3044 else
3045 {
3046 if (! parse_const_expr_arg (&s, &asi))
3047 {
3048 error_message = _(": invalid ASI expression");
3049 goto error;
3050 }
3051 if (asi < 0 || asi > 255)
3052 {
3053 error_message = _(": invalid ASI number");
3054 goto error;
3055 }
3056 }
3057 opcode |= ASI (asi);
3058 continue;
3059 } /* Alternate space. */
3060
3061 case 'p':
3062 if (strncmp (s, "%psr", 4) == 0)
3063 {
3064 s += 4;
3065 continue;
3066 }
3067 break;
3068
3069 case 'q': /* Floating point queue. */
3070 if (strncmp (s, "%fq", 3) == 0)
3071 {
3072 s += 3;
3073 continue;
3074 }
3075 break;
3076
3077 case 'Q': /* Coprocessor queue. */
3078 if (strncmp (s, "%cq", 3) == 0)
3079 {
3080 s += 3;
3081 continue;
3082 }
3083 break;
3084
3085 case 'S':
3086 if (strcmp (str, "set") == 0
3087 || strcmp (str, "setuw") == 0)
3088 {
3089 special_case = SPECIAL_CASE_SET;
3090 continue;
3091 }
3092 else if (strcmp (str, "setsw") == 0)
3093 {
3094 special_case = SPECIAL_CASE_SETSW;
3095 continue;
3096 }
3097 else if (strcmp (str, "setx") == 0)
3098 {
3099 special_case = SPECIAL_CASE_SETX;
3100 continue;
3101 }
3102 else if (strncmp (str, "fdiv", 4) == 0)
3103 {
3104 special_case = SPECIAL_CASE_FDIV;
3105 continue;
3106 }
3107 break;
3108
3109 case 'o':
3110 if (strncmp (s, "%asi", 4) != 0)
3111 break;
3112 s += 4;
3113 continue;
3114
3115 case 's':
3116 if (strncmp (s, "%fprs", 5) != 0)
3117 break;
3118 s += 5;
3119 continue;
3120
3121 case '{':
3122 if (strncmp (s, "%mcdper",7) != 0)
3123 break;
3124 s += 7;
3125 continue;
3126
3127 case '&':
3128 if (strncmp (s, "%entropy", 8) != 0)
3129 break;
3130 s += 8;
3131 continue;
3132
3133 case 'E':
3134 if (strncmp (s, "%ccr", 4) != 0)
3135 break;
3136 s += 4;
3137 continue;
3138
3139 case 't':
3140 if (strncmp (s, "%tbr", 4) != 0)
3141 break;
3142 s += 4;
3143 continue;
3144
3145 case 'w':
3146 if (strncmp (s, "%wim", 4) != 0)
3147 break;
3148 s += 4;
3149 continue;
3150
3151 case '|':
3152 {
3153 int imm2 = 0;
3154
3155 /* Parse a 2-bit immediate. */
3156 if (! parse_const_expr_arg (&s, &imm2))
3157 {
3158 error_message = _(": non-immdiate imm2 operand");
3159 goto error;
3160 }
3161 if ((imm2 & ~0x3) != 0)
3162 {
3163 error_message = _(": imm2 immediate operand out of range (0-3)");
3164 goto error;
3165 }
3166
3167 opcode |= ((imm2 & 0x2) << 3) | (imm2 & 0x1);
3168 continue;
3169 }
3170
3171 case 'x':
3172 {
3173 char *push = input_line_pointer;
3174 expressionS e;
3175
3176 input_line_pointer = s;
3177 expression (&e);
3178 if (e.X_op == O_constant)
3179 {
3180 int n = e.X_add_number;
3181 if (n != e.X_add_number || (n & ~0x1ff) != 0)
3182 as_bad (_("OPF immediate operand out of range (0-0x1ff)"));
3183 else
3184 opcode |= e.X_add_number << 5;
3185 }
3186 else
3187 as_bad (_("non-immediate OPF operand, ignored"));
3188 s = input_line_pointer;
3189 input_line_pointer = push;
3190 continue;
3191 }
3192
3193 case 'y':
3194 if (strncmp (s, "%y", 2) != 0)
3195 break;
3196 s += 2;
3197 continue;
3198
3199 case 'u':
3200 case 'U':
3201 {
3202 /* Parse a sparclet cpreg. */
3203 int cpreg;
3204 if (! parse_keyword_arg (sparc_encode_sparclet_cpreg, &s, &cpreg))
3205 {
3206 error_message = _(": invalid cpreg name");
3207 goto error;
3208 }
3209 opcode |= (*args == 'U' ? RS1 (cpreg) : RD (cpreg));
3210 continue;
3211 }
3212
3213 default:
3214 as_fatal (_("failed sanity check."));
3215 } /* switch on arg code. */
3216
3217 /* Break out of for() loop. */
3218 break;
3219 } /* For each arg that we expect. */
3220
3221 error:
3222 if (match == 0)
3223 {
3224 /* Args don't match. */
3225 if (&insn[1] - sparc_opcodes < sparc_num_opcodes
3226 && (insn->name == insn[1].name
3227 || !strcmp (insn->name, insn[1].name)))
3228 {
3229 ++insn;
3230 s = argsStart;
3231 continue;
3232 }
3233 else
3234 {
3235 as_bad (_("Illegal operands%s"), error_message);
3236 return special_case;
3237 }
3238 }
3239 else
3240 {
3241 /* We have a match. Now see if the architecture is OK. */
3242 /* String to use in case of architecture warning. */
3243 const char *msg_str = str;
3244 int needed_arch_mask = insn->architecture;
3245
3246 /* Include the ASI architecture needed as well */
3247 if (sasi && needed_arch_mask > sasi->architecture)
3248 {
3249 needed_arch_mask = sasi->architecture;
3250 msg_str = sasi->name;
3251 }
3252
3253 bfd_uint64_t hwcaps
3254 = (((bfd_uint64_t) insn->hwcaps2) << 32) | insn->hwcaps;
3255
3256 #if defined(OBJ_ELF) && !defined(TE_SOLARIS)
3257 if (hwcaps)
3258 hwcap_seen |= hwcaps;
3259 #endif
3260 if (v9_arg_p)
3261 {
3262 needed_arch_mask &=
3263 ~(SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9) - 1);
3264 if (! needed_arch_mask)
3265 needed_arch_mask =
3266 SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9);
3267 }
3268
3269 if (needed_arch_mask
3270 & SPARC_OPCODE_SUPPORTED (current_architecture))
3271 /* OK. */
3272 ;
3273 /* Can we bump up the architecture? */
3274 else if (needed_arch_mask
3275 & SPARC_OPCODE_SUPPORTED (max_architecture))
3276 {
3277 enum sparc_opcode_arch_val needed_architecture =
3278 sparc_ffs (SPARC_OPCODE_SUPPORTED (max_architecture)
3279 & needed_arch_mask);
3280
3281 gas_assert (needed_architecture <= SPARC_OPCODE_ARCH_MAX);
3282 if (warn_on_bump
3283 && needed_architecture > warn_after_architecture)
3284 {
3285 as_warn (_("architecture bumped from \"%s\" to \"%s\" on \"%s\""),
3286 sparc_opcode_archs[current_architecture].name,
3287 sparc_opcode_archs[needed_architecture].name,
3288 msg_str);
3289 warn_after_architecture = needed_architecture;
3290 }
3291 current_architecture = needed_architecture;
3292 hwcap_allowed
3293 = (hwcap_allowed
3294 | hwcaps
3295 | (((bfd_uint64_t) sparc_opcode_archs[current_architecture].hwcaps2) << 32)
3296 | sparc_opcode_archs[current_architecture].hwcaps);
3297 }
3298 /* Conflict. */
3299 /* ??? This seems to be a bit fragile. What if the next entry in
3300 the opcode table is the one we want and it is supported?
3301 It is possible to arrange the table today so that this can't
3302 happen but what about tomorrow? */
3303 else
3304 {
3305 int arch, printed_one_p = 0;
3306 char *p;
3307 char required_archs[SPARC_OPCODE_ARCH_MAX * 16];
3308
3309 /* Create a list of the architectures that support the insn. */
3310 needed_arch_mask &= ~SPARC_OPCODE_SUPPORTED (max_architecture);
3311 p = required_archs;
3312 arch = sparc_ffs (needed_arch_mask);
3313 while ((1 << arch) <= needed_arch_mask)
3314 {
3315 if ((1 << arch) & needed_arch_mask)
3316 {
3317 if (printed_one_p)
3318 *p++ = '|';
3319 strcpy (p, sparc_opcode_archs[arch].name);
3320 p += strlen (p);
3321 printed_one_p = 1;
3322 }
3323 ++arch;
3324 }
3325
3326 as_bad (_("Architecture mismatch on \"%s %s\"."), str, argsStart);
3327 as_tsktsk (_("(Requires %s; requested architecture is %s.)"),
3328 required_archs,
3329 sparc_opcode_archs[max_architecture].name);
3330 return special_case;
3331 }
3332
3333 /* Make sure the hwcaps used by the instruction are
3334 currently enabled. */
3335 if (hwcaps & ~hwcap_allowed)
3336 {
3337 const char *hwcap_name = get_hwcap_name(hwcaps & ~hwcap_allowed);
3338
3339 as_bad (_("Hardware capability \"%s\" not enabled for \"%s\"."),
3340 hwcap_name, str);
3341 return special_case;
3342 }
3343 } /* If no match. */
3344
3345 break;
3346 } /* Forever looking for a match. */
3347
3348 the_insn.opcode = opcode;
3349 return special_case;
3350 }
3351
3352 static char *
3353 skip_over_keyword (char *q)
3354 {
3355 for (q = q + (*q == '#' || *q == '%');
3356 ISALNUM (*q) || *q == '_';
3357 ++q)
3358 continue;
3359 return q;
3360 }
3361
3362 static int
3363 parse_sparc_asi (char **input_pointer_p, const sparc_asi **value_p)
3364 {
3365 const sparc_asi *value;
3366 char c, *p, *q;
3367
3368 p = *input_pointer_p;
3369 q = skip_over_keyword(p);
3370 c = *q;
3371 *q = 0;
3372 value = sparc_encode_asi (p);
3373 *q = c;
3374 if (value == NULL)
3375 return 0;
3376 *value_p = value;
3377 *input_pointer_p = q;
3378 return 1;
3379 }
3380
3381 /* Parse an argument that can be expressed as a keyword.
3382 (eg: #StoreStore or %ccfr).
3383 The result is a boolean indicating success.
3384 If successful, INPUT_POINTER is updated. */
3385
3386 static int
3387 parse_keyword_arg (int (*lookup_fn) (const char *),
3388 char **input_pointerP,
3389 int *valueP)
3390 {
3391 int value;
3392 char c, *p, *q;
3393
3394 p = *input_pointerP;
3395 q = skip_over_keyword(p);
3396 c = *q;
3397 *q = 0;
3398 value = (*lookup_fn) (p);
3399 *q = c;
3400 if (value == -1)
3401 return 0;
3402 *valueP = value;
3403 *input_pointerP = q;
3404 return 1;
3405 }
3406
3407 /* Parse an argument that is a constant expression.
3408 The result is a boolean indicating success. */
3409
3410 static int
3411 parse_const_expr_arg (char **input_pointerP, int *valueP)
3412 {
3413 char *save = input_line_pointer;
3414 expressionS exp;
3415
3416 input_line_pointer = *input_pointerP;
3417 /* The next expression may be something other than a constant
3418 (say if we're not processing the right variant of the insn).
3419 Don't call expression unless we're sure it will succeed as it will
3420 signal an error (which we want to defer until later). */
3421 /* FIXME: It might be better to define md_operand and have it recognize
3422 things like %asi, etc. but continuing that route through to the end
3423 is a lot of work. */
3424 if (*input_line_pointer == '%')
3425 {
3426 input_line_pointer = save;
3427 return 0;
3428 }
3429 expression (&exp);
3430 *input_pointerP = input_line_pointer;
3431 input_line_pointer = save;
3432 if (exp.X_op != O_constant)
3433 return 0;
3434 *valueP = exp.X_add_number;
3435 return 1;
3436 }
3437
3438 /* Subroutine of sparc_ip to parse an expression. */
3439
3440 static int
3441 get_expression (char *str)
3442 {
3443 char *save_in;
3444 segT seg;
3445
3446 save_in = input_line_pointer;
3447 input_line_pointer = str;
3448 seg = expression (&the_insn.exp);
3449 if (seg != absolute_section
3450 && seg != text_section
3451 && seg != data_section
3452 && seg != bss_section
3453 && seg != undefined_section)
3454 {
3455 the_insn.error = _("bad segment");
3456 expr_end = input_line_pointer;
3457 input_line_pointer = save_in;
3458 return 1;
3459 }
3460 expr_end = input_line_pointer;
3461 input_line_pointer = save_in;
3462 return 0;
3463 }
3464
3465 /* Subroutine of md_assemble to output one insn. */
3466
3467 static void
3468 output_insn (const struct sparc_opcode *insn, struct sparc_it *theinsn)
3469 {
3470 char *toP = frag_more (4);
3471
3472 /* Put out the opcode. */
3473 if (INSN_BIG_ENDIAN)
3474 number_to_chars_bigendian (toP, (valueT) theinsn->opcode, 4);
3475 else
3476 number_to_chars_littleendian (toP, (valueT) theinsn->opcode, 4);
3477
3478 /* Put out the symbol-dependent stuff. */
3479 if (theinsn->reloc != BFD_RELOC_NONE)
3480 {
3481 fixS *fixP = fix_new_exp (frag_now, /* Which frag. */
3482 (toP - frag_now->fr_literal), /* Where. */
3483 4, /* Size. */
3484 &theinsn->exp,
3485 theinsn->pcrel,
3486 theinsn->reloc);
3487 /* Turn off overflow checking in fixup_segment. We'll do our
3488 own overflow checking in md_apply_fix. This is necessary because
3489 the insn size is 4 and fixup_segment will signal an overflow for
3490 large 8 byte quantities. */
3491 fixP->fx_no_overflow = 1;
3492 if (theinsn->reloc == BFD_RELOC_SPARC_OLO10)
3493 fixP->tc_fix_data = theinsn->exp2.X_add_number;
3494 }
3495
3496 last_insn = insn;
3497 last_opcode = theinsn->opcode;
3498
3499 #ifdef OBJ_ELF
3500 dwarf2_emit_insn (4);
3501 #endif
3502 }
3503
3504 const char *
3506 md_atof (int type, char *litP, int *sizeP)
3507 {
3508 return ieee_md_atof (type, litP, sizeP, target_big_endian);
3509 }
3510
3511 /* Write a value out to the object file, using the appropriate
3512 endianness. */
3513
3514 void
3515 md_number_to_chars (char *buf, valueT val, int n)
3516 {
3517 if (target_big_endian)
3518 number_to_chars_bigendian (buf, val, n);
3519 else if (target_little_endian_data
3520 && ((n == 4 || n == 2) && ~now_seg->flags & SEC_ALLOC))
3521 /* Output debug words, which are not in allocated sections, as big
3522 endian. */
3523 number_to_chars_bigendian (buf, val, n);
3524 else if (target_little_endian_data || ! target_big_endian)
3525 number_to_chars_littleendian (buf, val, n);
3526 }
3527
3528 /* Apply a fixS to the frags, now that we know the value it ought to
3530 hold. */
3531
3532 void
3533 md_apply_fix (fixS *fixP, valueT *valP, segT segment ATTRIBUTE_UNUSED)
3534 {
3535 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
3536 offsetT val = * (offsetT *) valP;
3537 long insn;
3538
3539 gas_assert (fixP->fx_r_type < BFD_RELOC_UNUSED);
3540
3541 fixP->fx_addnumber = val; /* Remember value for emit_reloc. */
3542
3543 #ifdef OBJ_ELF
3544 /* SPARC ELF relocations don't use an addend in the data field. */
3545 if (fixP->fx_addsy != NULL)
3546 {
3547 switch (fixP->fx_r_type)
3548 {
3549 case BFD_RELOC_SPARC_TLS_GD_HI22:
3550 case BFD_RELOC_SPARC_TLS_GD_LO10:
3551 case BFD_RELOC_SPARC_TLS_GD_ADD:
3552 case BFD_RELOC_SPARC_TLS_GD_CALL:
3553 case BFD_RELOC_SPARC_TLS_LDM_HI22:
3554 case BFD_RELOC_SPARC_TLS_LDM_LO10:
3555 case BFD_RELOC_SPARC_TLS_LDM_ADD:
3556 case BFD_RELOC_SPARC_TLS_LDM_CALL:
3557 case BFD_RELOC_SPARC_TLS_LDO_HIX22:
3558 case BFD_RELOC_SPARC_TLS_LDO_LOX10:
3559 case BFD_RELOC_SPARC_TLS_LDO_ADD:
3560 case BFD_RELOC_SPARC_TLS_IE_HI22:
3561 case BFD_RELOC_SPARC_TLS_IE_LO10:
3562 case BFD_RELOC_SPARC_TLS_IE_LD:
3563 case BFD_RELOC_SPARC_TLS_IE_LDX:
3564 case BFD_RELOC_SPARC_TLS_IE_ADD:
3565 case BFD_RELOC_SPARC_TLS_LE_HIX22:
3566 case BFD_RELOC_SPARC_TLS_LE_LOX10:
3567 case BFD_RELOC_SPARC_TLS_DTPMOD32:
3568 case BFD_RELOC_SPARC_TLS_DTPMOD64:
3569 case BFD_RELOC_SPARC_TLS_DTPOFF32:
3570 case BFD_RELOC_SPARC_TLS_DTPOFF64:
3571 case BFD_RELOC_SPARC_TLS_TPOFF32:
3572 case BFD_RELOC_SPARC_TLS_TPOFF64:
3573 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3574
3575 default:
3576 break;
3577 }
3578
3579 return;
3580 }
3581 #endif
3582
3583 /* This is a hack. There should be a better way to
3584 handle this. Probably in terms of howto fields, once
3585 we can look at these fixups in terms of howtos. */
3586 if (fixP->fx_r_type == BFD_RELOC_32_PCREL_S2 && fixP->fx_addsy)
3587 val += fixP->fx_where + fixP->fx_frag->fr_address;
3588
3589 #ifdef OBJ_AOUT
3590 /* FIXME: More ridiculous gas reloc hacking. If we are going to
3591 generate a reloc, then we just want to let the reloc addend set
3592 the value. We do not want to also stuff the addend into the
3593 object file. Including the addend in the object file works when
3594 doing a static link, because the linker will ignore the object
3595 file contents. However, the dynamic linker does not ignore the
3596 object file contents. */
3597 if (fixP->fx_addsy != NULL
3598 && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2)
3599 val = 0;
3600
3601 /* When generating PIC code, we do not want an addend for a reloc
3602 against a local symbol. We adjust fx_addnumber to cancel out the
3603 value already included in val, and to also cancel out the
3604 adjustment which bfd_install_relocation will create. */
3605 if (sparc_pic_code
3606 && fixP->fx_r_type != BFD_RELOC_32_PCREL_S2
3607 && fixP->fx_addsy != NULL
3608 && ! S_IS_COMMON (fixP->fx_addsy)
3609 && symbol_section_p (fixP->fx_addsy))
3610 fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
3611
3612 /* When generating PIC code, we need to fiddle to get
3613 bfd_install_relocation to do the right thing for a PC relative
3614 reloc against a local symbol which we are going to keep. */
3615 if (sparc_pic_code
3616 && fixP->fx_r_type == BFD_RELOC_32_PCREL_S2
3617 && fixP->fx_addsy != NULL
3618 && (S_IS_EXTERNAL (fixP->fx_addsy)
3619 || S_IS_WEAK (fixP->fx_addsy))
3620 && S_IS_DEFINED (fixP->fx_addsy)
3621 && ! S_IS_COMMON (fixP->fx_addsy))
3622 {
3623 val = 0;
3624 fixP->fx_addnumber -= 2 * S_GET_VALUE (fixP->fx_addsy);
3625 }
3626 #endif
3627
3628 /* If this is a data relocation, just output VAL. */
3629
3630 if (fixP->fx_r_type == BFD_RELOC_8)
3631 {
3632 md_number_to_chars (buf, val, 1);
3633 }
3634 else if (fixP->fx_r_type == BFD_RELOC_16
3635 || fixP->fx_r_type == BFD_RELOC_SPARC_UA16)
3636 {
3637 md_number_to_chars (buf, val, 2);
3638 }
3639 else if (fixP->fx_r_type == BFD_RELOC_32
3640 || fixP->fx_r_type == BFD_RELOC_SPARC_UA32
3641 || fixP->fx_r_type == BFD_RELOC_SPARC_REV32)
3642 {
3643 md_number_to_chars (buf, val, 4);
3644 }
3645 else if (fixP->fx_r_type == BFD_RELOC_64
3646 || fixP->fx_r_type == BFD_RELOC_SPARC_UA64)
3647 {
3648 md_number_to_chars (buf, val, 8);
3649 }
3650 else if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3651 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3652 {
3653 fixP->fx_done = 0;
3654 return;
3655 }
3656 else
3657 {
3658 /* It's a relocation against an instruction. */
3659
3660 if (INSN_BIG_ENDIAN)
3661 insn = bfd_getb32 ((unsigned char *) buf);
3662 else
3663 insn = bfd_getl32 ((unsigned char *) buf);
3664
3665 switch (fixP->fx_r_type)
3666 {
3667 case BFD_RELOC_32_PCREL_S2:
3668 val = val >> 2;
3669 /* FIXME: This increment-by-one deserves a comment of why it's
3670 being done! */
3671 if (! sparc_pic_code
3672 || fixP->fx_addsy == NULL
3673 || symbol_section_p (fixP->fx_addsy))
3674 ++val;
3675
3676 insn |= val & 0x3fffffff;
3677
3678 /* See if we have a delay slot. In that case we attempt to
3679 optimize several cases transforming CALL instructions
3680 into branches. But we can only do that if the relocation
3681 can be completely resolved here, i.e. if no undefined
3682 symbol is associated with it. */
3683 if (sparc_relax && fixP->fx_addsy == NULL
3684 && fixP->fx_where + 8 <= fixP->fx_frag->fr_fix)
3685 {
3686 #define G0 0
3687 #define O7 15
3688 #define XCC (2 << 20)
3689 #define COND(x) (((x)&0xf)<<25)
3690 #define CONDA COND(0x8)
3691 #define INSN_BPA (F2(0,1) | CONDA | BPRED | XCC)
3692 #define INSN_BA (F2(0,2) | CONDA)
3693 #define INSN_OR F3(2, 0x2, 0)
3694 #define INSN_NOP F2(0,4)
3695
3696 long delay;
3697
3698 /* If the instruction is a call with either:
3699 restore
3700 arithmetic instruction with rd == %o7
3701 where rs1 != %o7 and rs2 if it is register != %o7
3702 then we can optimize if the call destination is near
3703 by changing the call into a branch always. */
3704 if (INSN_BIG_ENDIAN)
3705 delay = bfd_getb32 ((unsigned char *) buf + 4);
3706 else
3707 delay = bfd_getl32 ((unsigned char *) buf + 4);
3708 if ((insn & OP (~0)) != OP (1) || (delay & OP (~0)) != OP (2))
3709 break;
3710 if ((delay & OP3 (~0)) != OP3 (0x3d) /* Restore. */
3711 && ((delay & OP3 (0x28)) != 0 /* Arithmetic. */
3712 || ((delay & RD (~0)) != RD (O7))))
3713 break;
3714 if ((delay & RS1 (~0)) == RS1 (O7)
3715 || ((delay & F3I (~0)) == 0
3716 && (delay & RS2 (~0)) == RS2 (O7)))
3717 break;
3718 /* Ensure the branch will fit into simm22. */
3719 if ((val & 0x3fe00000)
3720 && (val & 0x3fe00000) != 0x3fe00000)
3721 break;
3722 /* Check if the arch is v9 and branch will fit
3723 into simm19. */
3724 if (((val & 0x3c0000) == 0
3725 || (val & 0x3c0000) == 0x3c0000)
3726 && (sparc_arch_size == 64
3727 || current_architecture >= SPARC_OPCODE_ARCH_V9))
3728 /* ba,pt %xcc */
3729 insn = INSN_BPA | (val & 0x7ffff);
3730 else
3731 /* ba */
3732 insn = INSN_BA | (val & 0x3fffff);
3733 if (fixP->fx_where >= 4
3734 && ((delay & (0xffffffff ^ RS1 (~0)))
3735 == (INSN_OR | RD (O7) | RS2 (G0))))
3736 {
3737 long setter;
3738 int reg;
3739
3740 if (INSN_BIG_ENDIAN)
3741 setter = bfd_getb32 ((unsigned char *) buf - 4);
3742 else
3743 setter = bfd_getl32 ((unsigned char *) buf - 4);
3744 if ((setter & (0xffffffff ^ RD (~0)))
3745 != (INSN_OR | RS1 (O7) | RS2 (G0)))
3746 break;
3747 /* The sequence was
3748 or %o7, %g0, %rN
3749 call foo
3750 or %rN, %g0, %o7
3751
3752 If call foo was replaced with ba, replace
3753 or %rN, %g0, %o7 with nop. */
3754 reg = (delay & RS1 (~0)) >> 14;
3755 if (reg != ((setter & RD (~0)) >> 25)
3756 || reg == G0 || reg == O7)
3757 break;
3758
3759 if (INSN_BIG_ENDIAN)
3760 bfd_putb32 (INSN_NOP, (unsigned char *) buf + 4);
3761 else
3762 bfd_putl32 (INSN_NOP, (unsigned char *) buf + 4);
3763 }
3764 }
3765 break;
3766
3767 case BFD_RELOC_SPARC_11:
3768 if (! in_signed_range (val, 0x7ff))
3769 as_bad_where (fixP->fx_file, fixP->fx_line,
3770 _("relocation overflow"));
3771 insn |= val & 0x7ff;
3772 break;
3773
3774 case BFD_RELOC_SPARC_10:
3775 if (! in_signed_range (val, 0x3ff))
3776 as_bad_where (fixP->fx_file, fixP->fx_line,
3777 _("relocation overflow"));
3778 insn |= val & 0x3ff;
3779 break;
3780
3781 case BFD_RELOC_SPARC_7:
3782 if (! in_bitfield_range (val, 0x7f))
3783 as_bad_where (fixP->fx_file, fixP->fx_line,
3784 _("relocation overflow"));
3785 insn |= val & 0x7f;
3786 break;
3787
3788 case BFD_RELOC_SPARC_6:
3789 if (! in_bitfield_range (val, 0x3f))
3790 as_bad_where (fixP->fx_file, fixP->fx_line,
3791 _("relocation overflow"));
3792 insn |= val & 0x3f;
3793 break;
3794
3795 case BFD_RELOC_SPARC_5:
3796 if (! in_bitfield_range (val, 0x1f))
3797 as_bad_where (fixP->fx_file, fixP->fx_line,
3798 _("relocation overflow"));
3799 insn |= val & 0x1f;
3800 break;
3801
3802 case BFD_RELOC_SPARC_WDISP10:
3803 if ((val & 3)
3804 || val >= 0x007fc
3805 || val <= -(offsetT) 0x808)
3806 as_bad_where (fixP->fx_file, fixP->fx_line,
3807 _("relocation overflow"));
3808 /* FIXME: The +1 deserves a comment. */
3809 val = (val >> 2) + 1;
3810 insn |= ((val & 0x300) << 11)
3811 | ((val & 0xff) << 5);
3812 break;
3813
3814 case BFD_RELOC_SPARC_WDISP16:
3815 if ((val & 3)
3816 || val >= 0x1fffc
3817 || val <= -(offsetT) 0x20008)
3818 as_bad_where (fixP->fx_file, fixP->fx_line,
3819 _("relocation overflow"));
3820 /* FIXME: The +1 deserves a comment. */
3821 val = (val >> 2) + 1;
3822 insn |= ((val & 0xc000) << 6) | (val & 0x3fff);
3823 break;
3824
3825 case BFD_RELOC_SPARC_WDISP19:
3826 if ((val & 3)
3827 || val >= 0xffffc
3828 || val <= -(offsetT) 0x100008)
3829 as_bad_where (fixP->fx_file, fixP->fx_line,
3830 _("relocation overflow"));
3831 /* FIXME: The +1 deserves a comment. */
3832 val = (val >> 2) + 1;
3833 insn |= val & 0x7ffff;
3834 break;
3835
3836 case BFD_RELOC_SPARC_HH22:
3837 val = BSR (val, 32);
3838 /* Fall through. */
3839
3840 case BFD_RELOC_SPARC_LM22:
3841 case BFD_RELOC_HI22:
3842 if (!fixP->fx_addsy)
3843 insn |= (val >> 10) & 0x3fffff;
3844 else
3845 /* FIXME: Need comment explaining why we do this. */
3846 insn &= ~0xffff;
3847 break;
3848
3849 case BFD_RELOC_SPARC22:
3850 if (val & ~0x003fffff)
3851 as_bad_where (fixP->fx_file, fixP->fx_line,
3852 _("relocation overflow"));
3853 insn |= (val & 0x3fffff);
3854 break;
3855
3856 case BFD_RELOC_SPARC_HM10:
3857 val = BSR (val, 32);
3858 /* Fall through. */
3859
3860 case BFD_RELOC_LO10:
3861 if (!fixP->fx_addsy)
3862 insn |= val & 0x3ff;
3863 else
3864 /* FIXME: Need comment explaining why we do this. */
3865 insn &= ~0xff;
3866 break;
3867
3868 case BFD_RELOC_SPARC_OLO10:
3869 val &= 0x3ff;
3870 val += fixP->tc_fix_data;
3871 /* Fall through. */
3872
3873 case BFD_RELOC_SPARC13:
3874 if (! in_signed_range (val, 0x1fff))
3875 as_bad_where (fixP->fx_file, fixP->fx_line,
3876 _("relocation overflow"));
3877 insn |= val & 0x1fff;
3878 break;
3879
3880 case BFD_RELOC_SPARC_WDISP22:
3881 val = (val >> 2) + 1;
3882 /* Fall through. */
3883 case BFD_RELOC_SPARC_BASE22:
3884 insn |= val & 0x3fffff;
3885 break;
3886
3887 case BFD_RELOC_SPARC_H34:
3888 if (!fixP->fx_addsy)
3889 {
3890 bfd_vma tval = val;
3891 tval >>= 12;
3892 insn |= tval & 0x3fffff;
3893 }
3894 break;
3895
3896 case BFD_RELOC_SPARC_H44:
3897 if (!fixP->fx_addsy)
3898 {
3899 bfd_vma tval = val;
3900 tval >>= 22;
3901 insn |= tval & 0x3fffff;
3902 }
3903 break;
3904
3905 case BFD_RELOC_SPARC_M44:
3906 if (!fixP->fx_addsy)
3907 insn |= (val >> 12) & 0x3ff;
3908 break;
3909
3910 case BFD_RELOC_SPARC_L44:
3911 if (!fixP->fx_addsy)
3912 insn |= val & 0xfff;
3913 break;
3914
3915 case BFD_RELOC_SPARC_HIX22:
3916 if (!fixP->fx_addsy)
3917 {
3918 val ^= ~(offsetT) 0;
3919 insn |= (val >> 10) & 0x3fffff;
3920 }
3921 break;
3922
3923 case BFD_RELOC_SPARC_LOX10:
3924 if (!fixP->fx_addsy)
3925 insn |= 0x1c00 | (val & 0x3ff);
3926 break;
3927
3928 case BFD_RELOC_NONE:
3929 default:
3930 as_bad_where (fixP->fx_file, fixP->fx_line,
3931 _("bad or unhandled relocation type: 0x%02x"),
3932 fixP->fx_r_type);
3933 break;
3934 }
3935
3936 if (INSN_BIG_ENDIAN)
3937 bfd_putb32 (insn, (unsigned char *) buf);
3938 else
3939 bfd_putl32 (insn, (unsigned char *) buf);
3940 }
3941
3942 /* Are we finished with this relocation now? */
3943 if (fixP->fx_addsy == 0 && !fixP->fx_pcrel)
3944 fixP->fx_done = 1;
3945 }
3946
3947 /* Translate internal representation of relocation info to BFD target
3948 format. */
3949
3950 arelent **
3951 tc_gen_reloc (asection *section, fixS *fixp)
3952 {
3953 static arelent *relocs[3];
3954 arelent *reloc;
3955 bfd_reloc_code_real_type code;
3956
3957 relocs[0] = reloc = XNEW (arelent);
3958 relocs[1] = NULL;
3959
3960 reloc->sym_ptr_ptr = XNEW (asymbol *);
3961 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
3962 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3963
3964 switch (fixp->fx_r_type)
3965 {
3966 case BFD_RELOC_8:
3967 case BFD_RELOC_16:
3968 case BFD_RELOC_32:
3969 case BFD_RELOC_64:
3970 if (fixp->fx_pcrel)
3971 {
3972 switch (fixp->fx_size)
3973 {
3974 default:
3975 as_bad_where (fixp->fx_file, fixp->fx_line,
3976 _("can not do %d byte pc-relative relocation"),
3977 fixp->fx_size);
3978 code = fixp->fx_r_type;
3979 fixp->fx_pcrel = 0;
3980 break;
3981 case 1: code = BFD_RELOC_8_PCREL; break;
3982 case 2: code = BFD_RELOC_16_PCREL; break;
3983 case 4: code = BFD_RELOC_32_PCREL; break;
3984 #ifdef BFD64
3985 case 8: code = BFD_RELOC_64_PCREL; break;
3986 #endif
3987 }
3988 if (fixp->fx_pcrel)
3989 fixp->fx_addnumber = fixp->fx_offset;
3990 break;
3991 }
3992 /* Fall through. */
3993 case BFD_RELOC_HI22:
3994 case BFD_RELOC_LO10:
3995 case BFD_RELOC_32_PCREL_S2:
3996 case BFD_RELOC_SPARC13:
3997 case BFD_RELOC_SPARC22:
3998 case BFD_RELOC_SPARC_PC22:
3999 case BFD_RELOC_SPARC_PC10:
4000 case BFD_RELOC_SPARC_BASE13:
4001 case BFD_RELOC_SPARC_WDISP10:
4002 case BFD_RELOC_SPARC_WDISP16:
4003 case BFD_RELOC_SPARC_WDISP19:
4004 case BFD_RELOC_SPARC_WDISP22:
4005 case BFD_RELOC_SPARC_5:
4006 case BFD_RELOC_SPARC_6:
4007 case BFD_RELOC_SPARC_7:
4008 case BFD_RELOC_SPARC_10:
4009 case BFD_RELOC_SPARC_11:
4010 case BFD_RELOC_SPARC_HH22:
4011 case BFD_RELOC_SPARC_HM10:
4012 case BFD_RELOC_SPARC_LM22:
4013 case BFD_RELOC_SPARC_PC_HH22:
4014 case BFD_RELOC_SPARC_PC_HM10:
4015 case BFD_RELOC_SPARC_PC_LM22:
4016 case BFD_RELOC_SPARC_H34:
4017 case BFD_RELOC_SPARC_H44:
4018 case BFD_RELOC_SPARC_M44:
4019 case BFD_RELOC_SPARC_L44:
4020 case BFD_RELOC_SPARC_HIX22:
4021 case BFD_RELOC_SPARC_LOX10:
4022 case BFD_RELOC_SPARC_REV32:
4023 case BFD_RELOC_SPARC_OLO10:
4024 case BFD_RELOC_SPARC_UA16:
4025 case BFD_RELOC_SPARC_UA32:
4026 case BFD_RELOC_SPARC_UA64:
4027 case BFD_RELOC_8_PCREL:
4028 case BFD_RELOC_16_PCREL:
4029 case BFD_RELOC_32_PCREL:
4030 case BFD_RELOC_64_PCREL:
4031 case BFD_RELOC_SPARC_PLT32:
4032 case BFD_RELOC_SPARC_PLT64:
4033 case BFD_RELOC_VTABLE_ENTRY:
4034 case BFD_RELOC_VTABLE_INHERIT:
4035 case BFD_RELOC_SPARC_TLS_GD_HI22:
4036 case BFD_RELOC_SPARC_TLS_GD_LO10:
4037 case BFD_RELOC_SPARC_TLS_GD_ADD:
4038 case BFD_RELOC_SPARC_TLS_GD_CALL:
4039 case BFD_RELOC_SPARC_TLS_LDM_HI22:
4040 case BFD_RELOC_SPARC_TLS_LDM_LO10:
4041 case BFD_RELOC_SPARC_TLS_LDM_ADD:
4042 case BFD_RELOC_SPARC_TLS_LDM_CALL:
4043 case BFD_RELOC_SPARC_TLS_LDO_HIX22:
4044 case BFD_RELOC_SPARC_TLS_LDO_LOX10:
4045 case BFD_RELOC_SPARC_TLS_LDO_ADD:
4046 case BFD_RELOC_SPARC_TLS_IE_HI22:
4047 case BFD_RELOC_SPARC_TLS_IE_LO10:
4048 case BFD_RELOC_SPARC_TLS_IE_LD:
4049 case BFD_RELOC_SPARC_TLS_IE_LDX:
4050 case BFD_RELOC_SPARC_TLS_IE_ADD:
4051 case BFD_RELOC_SPARC_TLS_LE_HIX22:
4052 case BFD_RELOC_SPARC_TLS_LE_LOX10:
4053 case BFD_RELOC_SPARC_TLS_DTPOFF32:
4054 case BFD_RELOC_SPARC_TLS_DTPOFF64:
4055 case BFD_RELOC_SPARC_GOTDATA_OP_HIX22:
4056 case BFD_RELOC_SPARC_GOTDATA_OP_LOX10:
4057 case BFD_RELOC_SPARC_GOTDATA_OP:
4058 code = fixp->fx_r_type;
4059 break;
4060 default:
4061 abort ();
4062 return NULL;
4063 }
4064
4065 #if defined (OBJ_ELF) || defined (OBJ_AOUT)
4066 /* If we are generating PIC code, we need to generate a different
4067 set of relocs. */
4068
4069 #ifdef OBJ_ELF
4070 #define GOT_NAME "_GLOBAL_OFFSET_TABLE_"
4071 #else
4072 #define GOT_NAME "__GLOBAL_OFFSET_TABLE_"
4073 #endif
4074 #ifdef TE_VXWORKS
4075 #define GOTT_BASE "__GOTT_BASE__"
4076 #define GOTT_INDEX "__GOTT_INDEX__"
4077 #endif
4078
4079 /* This code must be parallel to the OBJ_ELF tc_fix_adjustable. */
4080
4081 if (sparc_pic_code)
4082 {
4083 switch (code)
4084 {
4085 case BFD_RELOC_32_PCREL_S2:
4086 if (generic_force_reloc (fixp))
4087 code = BFD_RELOC_SPARC_WPLT30;
4088 break;
4089 case BFD_RELOC_HI22:
4090 code = BFD_RELOC_SPARC_GOT22;
4091 if (fixp->fx_addsy != NULL)
4092 {
4093 if (strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
4094 code = BFD_RELOC_SPARC_PC22;
4095 #ifdef TE_VXWORKS
4096 if (strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_BASE) == 0
4097 || strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_INDEX) == 0)
4098 code = BFD_RELOC_HI22; /* Unchanged. */
4099 #endif
4100 }
4101 break;
4102 case BFD_RELOC_LO10:
4103 code = BFD_RELOC_SPARC_GOT10;
4104 if (fixp->fx_addsy != NULL)
4105 {
4106 if (strcmp (S_GET_NAME (fixp->fx_addsy), GOT_NAME) == 0)
4107 code = BFD_RELOC_SPARC_PC10;
4108 #ifdef TE_VXWORKS
4109 if (strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_BASE) == 0
4110 || strcmp (S_GET_NAME (fixp->fx_addsy), GOTT_INDEX) == 0)
4111 code = BFD_RELOC_LO10; /* Unchanged. */
4112 #endif
4113 }
4114 break;
4115 case BFD_RELOC_SPARC13:
4116 code = BFD_RELOC_SPARC_GOT13;
4117 break;
4118 default:
4119 break;
4120 }
4121 }
4122 #endif /* defined (OBJ_ELF) || defined (OBJ_AOUT) */
4123
4124 /* Nothing is aligned in DWARF debugging sections. */
4125 if (bfd_get_section_flags (stdoutput, section) & SEC_DEBUGGING)
4126 switch (code)
4127 {
4128 case BFD_RELOC_16: code = BFD_RELOC_SPARC_UA16; break;
4129 case BFD_RELOC_32: code = BFD_RELOC_SPARC_UA32; break;
4130 case BFD_RELOC_64: code = BFD_RELOC_SPARC_UA64; break;
4131 default: break;
4132 }
4133
4134 if (code == BFD_RELOC_SPARC_OLO10)
4135 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO10);
4136 else
4137 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
4138 if (reloc->howto == 0)
4139 {
4140 as_bad_where (fixp->fx_file, fixp->fx_line,
4141 _("internal error: can't export reloc type %d (`%s')"),
4142 fixp->fx_r_type, bfd_get_reloc_code_name (code));
4143 xfree (reloc);
4144 relocs[0] = NULL;
4145 return relocs;
4146 }
4147
4148 /* @@ Why fx_addnumber sometimes and fx_offset other times? */
4149 #ifdef OBJ_AOUT
4150
4151 if (reloc->howto->pc_relative == 0
4152 || code == BFD_RELOC_SPARC_PC10
4153 || code == BFD_RELOC_SPARC_PC22)
4154 reloc->addend = fixp->fx_addnumber;
4155 else if (sparc_pic_code
4156 && fixp->fx_r_type == BFD_RELOC_32_PCREL_S2
4157 && fixp->fx_addsy != NULL
4158 && (S_IS_EXTERNAL (fixp->fx_addsy)
4159 || S_IS_WEAK (fixp->fx_addsy))
4160 && S_IS_DEFINED (fixp->fx_addsy)
4161 && ! S_IS_COMMON (fixp->fx_addsy))
4162 reloc->addend = fixp->fx_addnumber;
4163 else
4164 reloc->addend = fixp->fx_offset - reloc->address;
4165
4166 #else /* elf or coff */
4167
4168 if (code != BFD_RELOC_32_PCREL_S2
4169 && code != BFD_RELOC_SPARC_WDISP22
4170 && code != BFD_RELOC_SPARC_WDISP16
4171 && code != BFD_RELOC_SPARC_WDISP19
4172 && code != BFD_RELOC_SPARC_WDISP10
4173 && code != BFD_RELOC_SPARC_WPLT30
4174 && code != BFD_RELOC_SPARC_TLS_GD_CALL
4175 && code != BFD_RELOC_SPARC_TLS_LDM_CALL)
4176 reloc->addend = fixp->fx_addnumber;
4177 else if (symbol_section_p (fixp->fx_addsy))
4178 reloc->addend = (section->vma
4179 + fixp->fx_addnumber
4180 + md_pcrel_from (fixp));
4181 else
4182 reloc->addend = fixp->fx_offset;
4183 #endif
4184
4185 /* We expand R_SPARC_OLO10 to R_SPARC_LO10 and R_SPARC_13
4186 on the same location. */
4187 if (code == BFD_RELOC_SPARC_OLO10)
4188 {
4189 relocs[1] = reloc = XNEW (arelent);
4190 relocs[2] = NULL;
4191
4192 reloc->sym_ptr_ptr = XNEW (asymbol *);
4193 *reloc->sym_ptr_ptr
4194 = symbol_get_bfdsym (section_symbol (absolute_section));
4195 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
4196 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_SPARC13);
4197 reloc->addend = fixp->tc_fix_data;
4198 }
4199
4200 return relocs;
4201 }
4202
4203 /* We have no need to default values of symbols. */
4205
4206 symbolS *
4207 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
4208 {
4209 return 0;
4210 }
4211
4212 /* Round up a section size to the appropriate boundary. */
4213
4214 valueT
4215 md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
4216 {
4217 #ifndef OBJ_ELF
4218 /* This is not right for ELF; a.out wants it, and COFF will force
4219 the alignment anyways. */
4220 valueT align = ((valueT) 1
4221 << (valueT) bfd_get_section_alignment (stdoutput, segment));
4222 valueT newsize;
4223
4224 /* Turn alignment value into a mask. */
4225 align--;
4226 newsize = (size + align) & ~align;
4227 return newsize;
4228 #else
4229 return size;
4230 #endif
4231 }
4232
4233 /* Exactly what point is a PC-relative offset relative TO?
4234 On the sparc, they're relative to the address of the offset, plus
4235 its size. This gets us to the following instruction.
4236 (??? Is this right? FIXME-SOON) */
4237 long
4238 md_pcrel_from (fixS *fixP)
4239 {
4240 long ret;
4241
4242 ret = fixP->fx_where + fixP->fx_frag->fr_address;
4243 if (! sparc_pic_code
4244 || fixP->fx_addsy == NULL
4245 || symbol_section_p (fixP->fx_addsy))
4246 ret += fixP->fx_size;
4247 return ret;
4248 }
4249
4250 /* Return log2 (VALUE), or -1 if VALUE is not an exact positive power
4252 of two. */
4253
4254 static int
4255 mylog2 (int value)
4256 {
4257 int shift;
4258
4259 if (value <= 0)
4260 return -1;
4261
4262 for (shift = 0; (value & 1) == 0; value >>= 1)
4263 ++shift;
4264
4265 return (value == 1) ? shift : -1;
4266 }
4267
4268 /* Sort of like s_lcomm. */
4269
4270 #ifndef OBJ_ELF
4271 static int max_alignment = 15;
4272 #endif
4273
4274 static void
4275 s_reserve (int ignore ATTRIBUTE_UNUSED)
4276 {
4277 char *name;
4278 char *p;
4279 char c;
4280 int align;
4281 int size;
4282 int temp;
4283 symbolS *symbolP;
4284
4285 c = get_symbol_name (&name);
4286 p = input_line_pointer;
4287 *p = c;
4288 SKIP_WHITESPACE_AFTER_NAME ();
4289
4290 if (*input_line_pointer != ',')
4291 {
4292 as_bad (_("Expected comma after name"));
4293 ignore_rest_of_line ();
4294 return;
4295 }
4296
4297 ++input_line_pointer;
4298
4299 if ((size = get_absolute_expression ()) < 0)
4300 {
4301 as_bad (_("BSS length (%d.) <0! Ignored."), size);
4302 ignore_rest_of_line ();
4303 return;
4304 } /* Bad length. */
4305
4306 *p = 0;
4307 symbolP = symbol_find_or_make (name);
4308 *p = c;
4309
4310 if (strncmp (input_line_pointer, ",\"bss\"", 6) != 0
4311 && strncmp (input_line_pointer, ",\".bss\"", 7) != 0)
4312 {
4313 as_bad (_("bad .reserve segment -- expected BSS segment"));
4314 return;
4315 }
4316
4317 if (input_line_pointer[2] == '.')
4318 input_line_pointer += 7;
4319 else
4320 input_line_pointer += 6;
4321 SKIP_WHITESPACE ();
4322
4323 if (*input_line_pointer == ',')
4324 {
4325 ++input_line_pointer;
4326
4327 SKIP_WHITESPACE ();
4328 if (*input_line_pointer == '\n')
4329 {
4330 as_bad (_("missing alignment"));
4331 ignore_rest_of_line ();
4332 return;
4333 }
4334
4335 align = (int) get_absolute_expression ();
4336
4337 #ifndef OBJ_ELF
4338 if (align > max_alignment)
4339 {
4340 align = max_alignment;
4341 as_warn (_("alignment too large; assuming %d"), align);
4342 }
4343 #endif
4344
4345 if (align < 0)
4346 {
4347 as_bad (_("negative alignment"));
4348 ignore_rest_of_line ();
4349 return;
4350 }
4351
4352 if (align != 0)
4353 {
4354 temp = mylog2 (align);
4355 if (temp < 0)
4356 {
4357 as_bad (_("alignment not a power of 2"));
4358 ignore_rest_of_line ();
4359 return;
4360 }
4361
4362 align = temp;
4363 }
4364
4365 record_alignment (bss_section, align);
4366 }
4367 else
4368 align = 0;
4369
4370 if (!S_IS_DEFINED (symbolP)
4371 #ifdef OBJ_AOUT
4372 && S_GET_OTHER (symbolP) == 0
4373 && S_GET_DESC (symbolP) == 0
4374 #endif
4375 )
4376 {
4377 if (! need_pass_2)
4378 {
4379 char *pfrag;
4380 segT current_seg = now_seg;
4381 subsegT current_subseg = now_subseg;
4382
4383 /* Switch to bss. */
4384 subseg_set (bss_section, 1);
4385
4386 if (align)
4387 /* Do alignment. */
4388 frag_align (align, 0, 0);
4389
4390 /* Detach from old frag. */
4391 if (S_GET_SEGMENT (symbolP) == bss_section)
4392 symbol_get_frag (symbolP)->fr_symbol = NULL;
4393
4394 symbol_set_frag (symbolP, frag_now);
4395 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
4396 (offsetT) size, (char *) 0);
4397 *pfrag = 0;
4398
4399 S_SET_SEGMENT (symbolP, bss_section);
4400
4401 subseg_set (current_seg, current_subseg);
4402
4403 #ifdef OBJ_ELF
4404 S_SET_SIZE (symbolP, size);
4405 #endif
4406 }
4407 }
4408 else
4409 {
4410 as_warn (_("Ignoring attempt to re-define symbol %s"),
4411 S_GET_NAME (symbolP));
4412 }
4413
4414 demand_empty_rest_of_line ();
4415 }
4416
4417 static void
4418 s_common (int ignore ATTRIBUTE_UNUSED)
4419 {
4420 char *name;
4421 char c;
4422 char *p;
4423 offsetT temp, size;
4424 symbolS *symbolP;
4425
4426 c = get_symbol_name (&name);
4427 /* Just after name is now '\0'. */
4428 p = input_line_pointer;
4429 *p = c;
4430 SKIP_WHITESPACE_AFTER_NAME ();
4431 if (*input_line_pointer != ',')
4432 {
4433 as_bad (_("Expected comma after symbol-name"));
4434 ignore_rest_of_line ();
4435 return;
4436 }
4437
4438 /* Skip ','. */
4439 input_line_pointer++;
4440
4441 if ((temp = get_absolute_expression ()) < 0)
4442 {
4443 as_bad (_(".COMMon length (%lu) out of range ignored"),
4444 (unsigned long) temp);
4445 ignore_rest_of_line ();
4446 return;
4447 }
4448 size = temp;
4449 *p = 0;
4450 symbolP = symbol_find_or_make (name);
4451 *p = c;
4452 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
4453 {
4454 as_bad (_("Ignoring attempt to re-define symbol"));
4455 ignore_rest_of_line ();
4456 return;
4457 }
4458 if (S_GET_VALUE (symbolP) != 0)
4459 {
4460 if (S_GET_VALUE (symbolP) != (valueT) size)
4461 {
4462 as_warn (_("Length of .comm \"%s\" is already %ld. Not changed to %ld."),
4463 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), (long) size);
4464 }
4465 }
4466 else
4467 {
4468 #ifndef OBJ_ELF
4469 S_SET_VALUE (symbolP, (valueT) size);
4470 S_SET_EXTERNAL (symbolP);
4471 #endif
4472 }
4473 know (symbol_get_frag (symbolP) == &zero_address_frag);
4474 if (*input_line_pointer != ',')
4475 {
4476 as_bad (_("Expected comma after common length"));
4477 ignore_rest_of_line ();
4478 return;
4479 }
4480 input_line_pointer++;
4481 SKIP_WHITESPACE ();
4482 if (*input_line_pointer != '"')
4483 {
4484 temp = get_absolute_expression ();
4485
4486 #ifndef OBJ_ELF
4487 if (temp > max_alignment)
4488 {
4489 temp = max_alignment;
4490 as_warn (_("alignment too large; assuming %ld"), (long) temp);
4491 }
4492 #endif
4493
4494 if (temp < 0)
4495 {
4496 as_bad (_("negative alignment"));
4497 ignore_rest_of_line ();
4498 return;
4499 }
4500
4501 #ifdef OBJ_ELF
4502 if (symbol_get_obj (symbolP)->local)
4503 {
4504 segT old_sec;
4505 int old_subsec;
4506 int align;
4507
4508 old_sec = now_seg;
4509 old_subsec = now_subseg;
4510
4511 if (temp == 0)
4512 align = 0;
4513 else
4514 align = mylog2 (temp);
4515
4516 if (align < 0)
4517 {
4518 as_bad (_("alignment not a power of 2"));
4519 ignore_rest_of_line ();
4520 return;
4521 }
4522
4523 record_alignment (bss_section, align);
4524 subseg_set (bss_section, 0);
4525 if (align)
4526 frag_align (align, 0, 0);
4527 if (S_GET_SEGMENT (symbolP) == bss_section)
4528 symbol_get_frag (symbolP)->fr_symbol = 0;
4529 symbol_set_frag (symbolP, frag_now);
4530 p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
4531 (offsetT) size, (char *) 0);
4532 *p = 0;
4533 S_SET_SEGMENT (symbolP, bss_section);
4534 S_CLEAR_EXTERNAL (symbolP);
4535 S_SET_SIZE (symbolP, size);
4536 subseg_set (old_sec, old_subsec);
4537 }
4538 else
4539 #endif /* OBJ_ELF */
4540 {
4541 allocate_common:
4542 S_SET_VALUE (symbolP, (valueT) size);
4543 #ifdef OBJ_ELF
4544 S_SET_ALIGN (symbolP, temp);
4545 S_SET_SIZE (symbolP, size);
4546 #endif
4547 S_SET_EXTERNAL (symbolP);
4548 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
4549 }
4550 }
4551 else
4552 {
4553 input_line_pointer++;
4554 /* @@ Some use the dot, some don't. Can we get some consistency?? */
4555 if (*input_line_pointer == '.')
4556 input_line_pointer++;
4557 /* @@ Some say data, some say bss. */
4558 if (strncmp (input_line_pointer, "bss\"", 4)
4559 && strncmp (input_line_pointer, "data\"", 5))
4560 {
4561 while (*--input_line_pointer != '"')
4562 ;
4563 input_line_pointer--;
4564 goto bad_common_segment;
4565 }
4566 while (*input_line_pointer++ != '"')
4567 ;
4568 goto allocate_common;
4569 }
4570
4571 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
4572
4573 demand_empty_rest_of_line ();
4574 return;
4575
4576 {
4577 bad_common_segment:
4578 p = input_line_pointer;
4579 while (*p && *p != '\n')
4580 p++;
4581 c = *p;
4582 *p = '\0';
4583 as_bad (_("bad .common segment %s"), input_line_pointer + 1);
4584 *p = c;
4585 input_line_pointer = p;
4586 ignore_rest_of_line ();
4587 return;
4588 }
4589 }
4590
4591 /* Handle the .empty pseudo-op. This suppresses the warnings about
4592 invalid delay slot usage. */
4593
4594 static void
4595 s_empty (int ignore ATTRIBUTE_UNUSED)
4596 {
4597 /* The easy way to implement is to just forget about the last
4598 instruction. */
4599 last_insn = NULL;
4600 }
4601
4602 static void
4603 s_seg (int ignore ATTRIBUTE_UNUSED)
4604 {
4605
4606 if (strncmp (input_line_pointer, "\"text\"", 6) == 0)
4607 {
4608 input_line_pointer += 6;
4609 s_text (0);
4610 return;
4611 }
4612 if (strncmp (input_line_pointer, "\"data\"", 6) == 0)
4613 {
4614 input_line_pointer += 6;
4615 s_data (0);
4616 return;
4617 }
4618 if (strncmp (input_line_pointer, "\"data1\"", 7) == 0)
4619 {
4620 input_line_pointer += 7;
4621 s_data1 ();
4622 return;
4623 }
4624 if (strncmp (input_line_pointer, "\"bss\"", 5) == 0)
4625 {
4626 input_line_pointer += 5;
4627 /* We only support 2 segments -- text and data -- for now, so
4628 things in the "bss segment" will have to go into data for now.
4629 You can still allocate SEG_BSS stuff with .lcomm or .reserve. */
4630 subseg_set (data_section, 255); /* FIXME-SOMEDAY. */
4631 return;
4632 }
4633 as_bad (_("Unknown segment type"));
4634 demand_empty_rest_of_line ();
4635 }
4636
4637 static void
4638 s_data1 (void)
4639 {
4640 subseg_set (data_section, 1);
4641 demand_empty_rest_of_line ();
4642 }
4643
4644 static void
4645 s_proc (int ignore ATTRIBUTE_UNUSED)
4646 {
4647 while (!is_end_of_line[(unsigned char) *input_line_pointer])
4648 {
4649 ++input_line_pointer;
4650 }
4651 ++input_line_pointer;
4652 }
4653
4654 /* This static variable is set by s_uacons to tell sparc_cons_align
4655 that the expression does not need to be aligned. */
4656
4657 static int sparc_no_align_cons = 0;
4658
4659 /* This handles the unaligned space allocation pseudo-ops, such as
4660 .uaword. .uaword is just like .word, but the value does not need
4661 to be aligned. */
4662
4663 static void
4664 s_uacons (int bytes)
4665 {
4666 /* Tell sparc_cons_align not to align this value. */
4667 sparc_no_align_cons = 1;
4668 cons (bytes);
4669 sparc_no_align_cons = 0;
4670 }
4671
4672 /* This handles the native word allocation pseudo-op .nword.
4673 For sparc_arch_size 32 it is equivalent to .word, for
4674 sparc_arch_size 64 it is equivalent to .xword. */
4675
4676 static void
4677 s_ncons (int bytes ATTRIBUTE_UNUSED)
4678 {
4679 cons (sparc_arch_size == 32 ? 4 : 8);
4680 }
4681
4682 #ifdef OBJ_ELF
4683 /* Handle the SPARC ELF .register pseudo-op. This sets the binding of a
4684 global register.
4685 The syntax is:
4686
4687 .register %g[2367],{#scratch|symbolname|#ignore}
4688 */
4689
4690 static void
4691 s_register (int ignore ATTRIBUTE_UNUSED)
4692 {
4693 char c;
4694 int reg;
4695 int flags;
4696 char *regname;
4697
4698 if (input_line_pointer[0] != '%'
4699 || input_line_pointer[1] != 'g'
4700 || ((input_line_pointer[2] & ~1) != '2'
4701 && (input_line_pointer[2] & ~1) != '6')
4702 || input_line_pointer[3] != ',')
4703 as_bad (_("register syntax is .register %%g[2367],{#scratch|symbolname|#ignore}"));
4704 reg = input_line_pointer[2] - '0';
4705 input_line_pointer += 4;
4706
4707 if (*input_line_pointer == '#')
4708 {
4709 ++input_line_pointer;
4710 c = get_symbol_name (®name);
4711 if (strcmp (regname, "scratch") && strcmp (regname, "ignore"))
4712 as_bad (_("register syntax is .register %%g[2367],{#scratch|symbolname|#ignore}"));
4713 if (regname[0] == 'i')
4714 regname = NULL;
4715 else
4716 regname = (char *) "";
4717 }
4718 else
4719 {
4720 c = get_symbol_name (®name);
4721 }
4722
4723 if (sparc_arch_size == 64)
4724 {
4725 if (globals[reg])
4726 {
4727 if ((regname && globals[reg] != (symbolS *) 1
4728 && strcmp (S_GET_NAME (globals[reg]), regname))
4729 || ((regname != NULL) ^ (globals[reg] != (symbolS *) 1)))
4730 as_bad (_("redefinition of global register"));
4731 }
4732 else
4733 {
4734 if (regname == NULL)
4735 globals[reg] = (symbolS *) 1;
4736 else
4737 {
4738 if (*regname)
4739 {
4740 if (symbol_find (regname))
4741 as_bad (_("Register symbol %s already defined."),
4742 regname);
4743 }
4744 globals[reg] = symbol_make (regname);
4745 flags = symbol_get_bfdsym (globals[reg])->flags;
4746 if (! *regname)
4747 flags = flags & ~(BSF_GLOBAL|BSF_LOCAL|BSF_WEAK);
4748 if (! (flags & (BSF_GLOBAL|BSF_LOCAL|BSF_WEAK)))
4749 flags |= BSF_GLOBAL;
4750 symbol_get_bfdsym (globals[reg])->flags = flags;
4751 S_SET_VALUE (globals[reg], (valueT) reg);
4752 S_SET_ALIGN (globals[reg], reg);
4753 S_SET_SIZE (globals[reg], 0);
4754 /* Although we actually want undefined_section here,
4755 we have to use absolute_section, because otherwise
4756 generic as code will make it a COM section.
4757 We fix this up in sparc_adjust_symtab. */
4758 S_SET_SEGMENT (globals[reg], absolute_section);
4759 S_SET_OTHER (globals[reg], 0);
4760 elf_symbol (symbol_get_bfdsym (globals[reg]))
4761 ->internal_elf_sym.st_info =
4762 ELF_ST_INFO(STB_GLOBAL, STT_REGISTER);
4763 elf_symbol (symbol_get_bfdsym (globals[reg]))
4764 ->internal_elf_sym.st_shndx = SHN_UNDEF;
4765 }
4766 }
4767 }
4768
4769 (void) restore_line_pointer (c);
4770
4771 demand_empty_rest_of_line ();
4772 }
4773
4774 /* Adjust the symbol table. We set undefined sections for STT_REGISTER
4775 symbols which need it. */
4776
4777 void
4778 sparc_adjust_symtab (void)
4779 {
4780 symbolS *sym;
4781
4782 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
4783 {
4784 if (ELF_ST_TYPE (elf_symbol (symbol_get_bfdsym (sym))
4785 ->internal_elf_sym.st_info) != STT_REGISTER)
4786 continue;
4787
4788 if (ELF_ST_TYPE (elf_symbol (symbol_get_bfdsym (sym))
4789 ->internal_elf_sym.st_shndx != SHN_UNDEF))
4790 continue;
4791
4792 S_SET_SEGMENT (sym, undefined_section);
4793 }
4794 }
4795 #endif
4796
4797 /* If the --enforce-aligned-data option is used, we require .word,
4798 et. al., to be aligned correctly. We do it by setting up an
4799 rs_align_code frag, and checking in HANDLE_ALIGN to make sure that
4800 no unexpected alignment was introduced.
4801
4802 The SunOS and Solaris native assemblers enforce aligned data by
4803 default. We don't want to do that, because gcc can deliberately
4804 generate misaligned data if the packed attribute is used. Instead,
4805 we permit misaligned data by default, and permit the user to set an
4806 option to check for it. */
4807
4808 void
4809 sparc_cons_align (int nbytes)
4810 {
4811 int nalign;
4812
4813 /* Only do this if we are enforcing aligned data. */
4814 if (! enforce_aligned_data)
4815 return;
4816
4817 /* Don't align if this is an unaligned pseudo-op. */
4818 if (sparc_no_align_cons)
4819 return;
4820
4821 nalign = mylog2 (nbytes);
4822 if (nalign == 0)
4823 return;
4824
4825 gas_assert (nalign > 0);
4826
4827 if (now_seg == absolute_section)
4828 {
4829 if ((abs_section_offset & ((1 << nalign) - 1)) != 0)
4830 as_bad (_("misaligned data"));
4831 return;
4832 }
4833
4834 frag_var (rs_align_test, 1, 1, (relax_substateT) 0,
4835 (symbolS *) NULL, (offsetT) nalign, (char *) NULL);
4836
4837 record_alignment (now_seg, nalign);
4838 }
4839
4840 /* This is called from HANDLE_ALIGN in tc-sparc.h. */
4841
4842 void
4843 sparc_handle_align (fragS *fragp)
4844 {
4845 int count, fix;
4846 char *p;
4847
4848 count = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
4849
4850 switch (fragp->fr_type)
4851 {
4852 case rs_align_test:
4853 if (count != 0)
4854 as_bad_where (fragp->fr_file, fragp->fr_line, _("misaligned data"));
4855 break;
4856
4857 case rs_align_code:
4858 p = fragp->fr_literal + fragp->fr_fix;
4859 fix = 0;
4860
4861 if (count & 3)
4862 {
4863 fix = count & 3;
4864 memset (p, 0, fix);
4865 p += fix;
4866 count -= fix;
4867 }
4868
4869 if (SPARC_OPCODE_ARCH_V9_P (max_architecture) && count > 8)
4870 {
4871 unsigned wval = (0x30680000 | count >> 2); /* ba,a,pt %xcc, 1f */
4872 if (INSN_BIG_ENDIAN)
4873 number_to_chars_bigendian (p, wval, 4);
4874 else
4875 number_to_chars_littleendian (p, wval, 4);
4876 p += 4;
4877 count -= 4;
4878 fix += 4;
4879 }
4880
4881 if (INSN_BIG_ENDIAN)
4882 number_to_chars_bigendian (p, 0x01000000, 4);
4883 else
4884 number_to_chars_littleendian (p, 0x01000000, 4);
4885
4886 fragp->fr_fix += fix;
4887 fragp->fr_var = 4;
4888 break;
4889
4890 default:
4891 break;
4892 }
4893 }
4894
4895 #ifdef OBJ_ELF
4896 /* Some special processing for a Sparc ELF file. */
4897
4898 void
4899 sparc_elf_final_processing (void)
4900 {
4901 /* Set the Sparc ELF flag bits. FIXME: There should probably be some
4902 sort of BFD interface for this. */
4903 if (sparc_arch_size == 64)
4904 {
4905 switch (sparc_memory_model)
4906 {
4907 case MM_RMO:
4908 elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_RMO;
4909 break;
4910 case MM_PSO:
4911 elf_elfheader (stdoutput)->e_flags |= EF_SPARCV9_PSO;
4912 break;
4913 default:
4914 break;
4915 }
4916 }
4917 else if (current_architecture >= SPARC_OPCODE_ARCH_V9)
4918 elf_elfheader (stdoutput)->e_flags |= EF_SPARC_32PLUS;
4919 if (current_architecture == SPARC_OPCODE_ARCH_V9A)
4920 elf_elfheader (stdoutput)->e_flags |= EF_SPARC_SUN_US1;
4921 else if (current_architecture == SPARC_OPCODE_ARCH_V9B)
4922 elf_elfheader (stdoutput)->e_flags |= EF_SPARC_SUN_US1|EF_SPARC_SUN_US3;
4923 }
4924
4925 const char *
4926 sparc_cons (expressionS *exp, int size)
4927 {
4928 char *save;
4929 const char *sparc_cons_special_reloc = NULL;
4930
4931 SKIP_WHITESPACE ();
4932 save = input_line_pointer;
4933 if (input_line_pointer[0] == '%'
4934 && input_line_pointer[1] == 'r'
4935 && input_line_pointer[2] == '_')
4936 {
4937 if (strncmp (input_line_pointer + 3, "disp", 4) == 0)
4938 {
4939 input_line_pointer += 7;
4940 sparc_cons_special_reloc = "disp";
4941 }
4942 else if (strncmp (input_line_pointer + 3, "plt", 3) == 0)
4943 {
4944 if (size != 4 && size != 8)
4945 as_bad (_("Illegal operands: %%r_plt in %d-byte data field"), size);
4946 else
4947 {
4948 input_line_pointer += 6;
4949 sparc_cons_special_reloc = "plt";
4950 }
4951 }
4952 else if (strncmp (input_line_pointer + 3, "tls_dtpoff", 10) == 0)
4953 {
4954 if (size != 4 && size != 8)
4955 as_bad (_("Illegal operands: %%r_tls_dtpoff in %d-byte data field"), size);
4956 else
4957 {
4958 input_line_pointer += 13;
4959 sparc_cons_special_reloc = "tls_dtpoff";
4960 }
4961 }
4962 if (sparc_cons_special_reloc)
4963 {
4964 int bad = 0;
4965
4966 switch (size)
4967 {
4968 case 1:
4969 if (*input_line_pointer != '8')
4970 bad = 1;
4971 input_line_pointer--;
4972 break;
4973 case 2:
4974 if (input_line_pointer[0] != '1' || input_line_pointer[1] != '6')
4975 bad = 1;
4976 break;
4977 case 4:
4978 if (input_line_pointer[0] != '3' || input_line_pointer[1] != '2')
4979 bad = 1;
4980 break;
4981 case 8:
4982 if (input_line_pointer[0] != '6' || input_line_pointer[1] != '4')
4983 bad = 1;
4984 break;
4985 default:
4986 bad = 1;
4987 break;
4988 }
4989
4990 if (bad)
4991 {
4992 as_bad (_("Illegal operands: Only %%r_%s%d allowed in %d-byte data fields"),
4993 sparc_cons_special_reloc, size * 8, size);
4994 }
4995 else
4996 {
4997 input_line_pointer += 2;
4998 if (*input_line_pointer != '(')
4999 {
5000 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
5001 sparc_cons_special_reloc, size * 8);
5002 bad = 1;
5003 }
5004 }
5005
5006 if (bad)
5007 {
5008 input_line_pointer = save;
5009 sparc_cons_special_reloc = NULL;
5010 }
5011 else
5012 {
5013 int c;
5014 char *end = ++input_line_pointer;
5015 int npar = 0;
5016
5017 while (! is_end_of_line[(c = *end)])
5018 {
5019 if (c == '(')
5020 npar++;
5021 else if (c == ')')
5022 {
5023 if (!npar)
5024 break;
5025 npar--;
5026 }
5027 end++;
5028 }
5029
5030 if (c != ')')
5031 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
5032 sparc_cons_special_reloc, size * 8);
5033 else
5034 {
5035 *end = '\0';
5036 expression (exp);
5037 *end = c;
5038 if (input_line_pointer != end)
5039 {
5040 as_bad (_("Illegal operands: %%r_%s%d requires arguments in ()"),
5041 sparc_cons_special_reloc, size * 8);
5042 }
5043 else
5044 {
5045 input_line_pointer++;
5046 SKIP_WHITESPACE ();
5047 c = *input_line_pointer;
5048 if (! is_end_of_line[c] && c != ',')
5049 as_bad (_("Illegal operands: garbage after %%r_%s%d()"),
5050 sparc_cons_special_reloc, size * 8);
5051 }
5052 }
5053 }
5054 }
5055 }
5056 if (sparc_cons_special_reloc == NULL)
5057 expression (exp);
5058 return sparc_cons_special_reloc;
5059 }
5060
5061 #endif
5062
5063 /* This is called by emit_expr via TC_CONS_FIX_NEW when creating a
5064 reloc for a cons. We could use the definition there, except that
5065 we want to handle little endian relocs specially. */
5066
5067 void
5068 cons_fix_new_sparc (fragS *frag,
5069 int where,
5070 unsigned int nbytes,
5071 expressionS *exp,
5072 const char *sparc_cons_special_reloc)
5073 {
5074 bfd_reloc_code_real_type r;
5075
5076 r = (nbytes == 1 ? BFD_RELOC_8 :
5077 (nbytes == 2 ? BFD_RELOC_16 :
5078 (nbytes == 4 ? BFD_RELOC_32 : BFD_RELOC_64)));
5079
5080 if (target_little_endian_data
5081 && nbytes == 4
5082 && now_seg->flags & SEC_ALLOC)
5083 r = BFD_RELOC_SPARC_REV32;
5084
5085 #ifdef TE_SOLARIS
5086 /* The Solaris linker does not allow R_SPARC_UA64
5087 relocations for 32-bit executables. */
5088 if (!target_little_endian_data
5089 && sparc_arch_size != 64
5090 && r == BFD_RELOC_64)
5091 r = BFD_RELOC_32;
5092 #endif
5093
5094 if (sparc_cons_special_reloc)
5095 {
5096 if (*sparc_cons_special_reloc == 'd')
5097 switch (nbytes)
5098 {
5099 case 1: r = BFD_RELOC_8_PCREL; break;
5100 case 2: r = BFD_RELOC_16_PCREL; break;
5101 case 4: r = BFD_RELOC_32_PCREL; break;
5102 case 8: r = BFD_RELOC_64_PCREL; break;
5103 default: abort ();
5104 }
5105 else if (*sparc_cons_special_reloc == 'p')
5106 switch (nbytes)
5107 {
5108 case 4: r = BFD_RELOC_SPARC_PLT32; break;
5109 case 8: r = BFD_RELOC_SPARC_PLT64; break;
5110 }
5111 else
5112 switch (nbytes)
5113 {
5114 case 4: r = BFD_RELOC_SPARC_TLS_DTPOFF32; break;
5115 case 8: r = BFD_RELOC_SPARC_TLS_DTPOFF64; break;
5116 }
5117 }
5118 else if (sparc_no_align_cons
5119 || /* PR 20803 - relocs in the .eh_frame section
5120 need to support unaligned access. */
5121 strcmp (now_seg->name, ".eh_frame") == 0)
5122 {
5123 switch (nbytes)
5124 {
5125 case 2: r = BFD_RELOC_SPARC_UA16; break;
5126 case 4: r = BFD_RELOC_SPARC_UA32; break;
5127 #ifdef TE_SOLARIS
5128 /* The Solaris linker does not allow R_SPARC_UA64
5129 relocations for 32-bit executables. */
5130 case 8: r = sparc_arch_size == 64 ?
5131 BFD_RELOC_SPARC_UA64 : BFD_RELOC_SPARC_UA32; break;
5132 #else
5133 case 8: r = BFD_RELOC_SPARC_UA64; break;
5134 #endif
5135 default: abort ();
5136 }
5137 }
5138
5139 fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
5140 }
5141
5142 void
5143 sparc_cfi_frame_initial_instructions (void)
5144 {
5145 cfi_add_CFA_def_cfa (14, sparc_arch_size == 64 ? 0x7ff : 0);
5146 }
5147
5148 int
5149 sparc_regname_to_dw2regnum (char *regname)
5150 {
5151 char *q;
5152 int i;
5153
5154 if (!regname[0])
5155 return -1;
5156
5157 switch (regname[0])
5158 {
5159 case 'g': i = 0; break;
5160 case 'o': i = 1; break;
5161 case 'l': i = 2; break;
5162 case 'i': i = 3; break;
5163 default: i = -1; break;
5164 }
5165 if (i != -1)
5166 {
5167 if (regname[1] < '0' || regname[1] > '8' || regname[2])
5168 return -1;
5169 return i * 8 + regname[1] - '0';
5170 }
5171 if (regname[0] == 's' && regname[1] == 'p' && !regname[2])
5172 return 14;
5173 if (regname[0] == 'f' && regname[1] == 'p' && !regname[2])
5174 return 30;
5175 if (regname[0] == 'f' || regname[0] == 'r')
5176 {
5177 unsigned int regnum;
5178
5179 regnum = strtoul (regname + 1, &q, 10);
5180 if (q == NULL || *q)
5181 return -1;
5182 if (regnum >= ((regname[0] == 'f'
5183 && SPARC_OPCODE_ARCH_V9_P (max_architecture))
5184 ? 64 : 32))
5185 return -1;
5186 if (regname[0] == 'f')
5187 {
5188 regnum += 32;
5189 if (regnum >= 64 && (regnum & 1))
5190 return -1;
5191 }
5192 return regnum;
5193 }
5194 return -1;
5195 }
5196
5197 void
5198 sparc_cfi_emit_pcrel_expr (expressionS *exp, unsigned int nbytes)
5199 {
5200 sparc_no_align_cons = 1;
5201 emit_expr_with_reloc (exp, nbytes, "disp");
5202 sparc_no_align_cons = 0;
5203 }
5204