Allow @HA, @L, and @H on constant expressions.
[deliverable/binutils-gdb.git] / gas / config / tc-ppc.c
1 /* tc-ppc.c -- Assemble for the PowerPC or POWER (RS/6000)
2 Copyright (C) 1994 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include <stdio.h>
22 #include <ctype.h>
23 #include "as.h"
24 #include "subsegs.h"
25
26 #include "opcode/ppc.h"
27
28 #ifdef OBJ_ELF
29 #include "elf/ppc.h"
30 #endif
31
32 /* This is the assembler for the PowerPC or POWER (RS/6000) chips. */
33
34 /* FIXME: This should be handled in a different way. */
35 extern int target_big_endian;
36
37 static void ppc_set_cpu PARAMS ((void));
38 static unsigned long ppc_insert_operand
39 PARAMS ((unsigned long insn, const struct powerpc_operand *operand,
40 offsetT val, char *file, unsigned int line));
41 static void ppc_macro PARAMS ((char *str, const struct powerpc_macro *macro));
42 static void ppc_byte PARAMS ((int));
43 static int ppc_is_toc_sym PARAMS ((symbolS *sym));
44 static void ppc_tc PARAMS ((int));
45 #ifdef OBJ_COFF
46 static void ppc_comm PARAMS ((int));
47 static void ppc_bb PARAMS ((int));
48 static void ppc_bf PARAMS ((int));
49 static void ppc_biei PARAMS ((int));
50 static void ppc_bs PARAMS ((int));
51 static void ppc_eb PARAMS ((int));
52 static void ppc_ef PARAMS ((int));
53 static void ppc_es PARAMS ((int));
54 static void ppc_csect PARAMS ((int));
55 static void ppc_function PARAMS ((int));
56 static void ppc_extern PARAMS ((int));
57 static void ppc_lglobl PARAMS ((int));
58 static void ppc_stabx PARAMS ((int));
59 static void ppc_rename PARAMS ((int));
60 static void ppc_toc PARAMS ((int));
61 #endif
62 #ifdef OBJ_ELF
63 static bfd_reloc_code_real_type ppc_elf_suffix PARAMS ((char **));
64 static void ppc_elf_cons PARAMS ((int));
65 static void ppc_elf_validate_fix PARAMS ((fixS *, segT));
66 #endif
67 \f
68 /* Generic assembler global variables which must be defined by all
69 targets. */
70
71 /* Characters which always start a comment. */
72 const char comment_chars[] = "#";
73
74 /* Characters which start a comment at the beginning of a line. */
75 const char line_comment_chars[] = "#";
76
77 /* Characters which may be used to separate multiple commands on a
78 single line. */
79 const char line_separator_chars[] = ";";
80
81 /* Characters which are used to indicate an exponent in a floating
82 point number. */
83 const char EXP_CHARS[] = "eE";
84
85 /* Characters which mean that a number is a floating point constant,
86 as in 0d1.0. */
87 const char FLT_CHARS[] = "dD";
88 \f
89 /* The target specific pseudo-ops which we support. */
90
91 const pseudo_typeS md_pseudo_table[] =
92 {
93 /* Pseudo-ops which must be overridden. */
94 { "byte", ppc_byte, 0 },
95
96 #ifdef OBJ_COFF
97 /* Pseudo-ops specific to the RS/6000 XCOFF format. Some of these
98 legitimately belong in the obj-*.c file. However, XCOFF is based
99 on COFF, and is only implemented for the RS/6000. We just use
100 obj-coff.c, and add what we need here. */
101 { "comm", ppc_comm, 0 },
102 { "lcomm", ppc_comm, 1 },
103 { "bb", ppc_bb, 0 },
104 { "bf", ppc_bf, 0 },
105 { "bi", ppc_biei, 0 },
106 { "bs", ppc_bs, 0 },
107 { "csect", ppc_csect, 0 },
108 { "eb", ppc_eb, 0 },
109 { "ef", ppc_ef, 0 },
110 { "ei", ppc_biei, 1 },
111 { "es", ppc_es, 0 },
112 { "extern", ppc_extern, 0 },
113 { "function", ppc_function, 0 },
114 { "lglobl", ppc_lglobl, 0 },
115 { "rename", ppc_rename, 0 },
116 { "stabx", ppc_stabx, 0 },
117 { "toc", ppc_toc, 0 },
118 #endif
119 #ifdef OBJ_ELF
120 { "long", ppc_elf_cons, 4 },
121 { "word", ppc_elf_cons, 2 },
122 { "short", ppc_elf_cons, 2 },
123 #endif
124
125 /* This pseudo-op is used even when not generating XCOFF output. */
126 { "tc", ppc_tc, 0 },
127
128 { NULL, NULL, 0 }
129 };
130 \f
131 /* Local variables. */
132
133 /* The type of processor we are assembling for. This is one or more
134 of the PPC_OPCODE flags defined in opcode/ppc.h. */
135 static int ppc_cpu = 0;
136
137 /* The size of the processor we are assembling for. This is either
138 PPC_OPCODE_32 or PPC_OPCODE_64. */
139 static int ppc_size = PPC_OPCODE_32;
140
141 /* Opcode hash table. */
142 static struct hash_control *ppc_hash;
143
144 /* Macro hash table. */
145 static struct hash_control *ppc_macro_hash;
146
147 #ifdef OBJ_ELF
148 /* Whether to warn about non PC relative relocations that aren't
149 in the .got2 section. */
150 static boolean mrelocatable = false;
151 #endif
152
153 #ifdef OBJ_COFF
154
155 /* The RS/6000 assembler uses the .csect pseudo-op to generate code
156 using a bunch of different sections. These assembler sections,
157 however, are all encompassed within the .text or .data sections of
158 the final output file. We handle this by using different
159 subsegments within these main segments. */
160
161 /* Next subsegment to allocate within the .text segment. */
162 static subsegT ppc_text_subsegment = 2;
163
164 /* Linked list of csects in the text section. */
165 static symbolS *ppc_text_csects;
166
167 /* Next subsegment to allocate within the .data segment. */
168 static subsegT ppc_data_subsegment = 2;
169
170 /* Linked list of csects in the data section. */
171 static symbolS *ppc_data_csects;
172
173 /* The current csect. */
174 static symbolS *ppc_current_csect;
175
176 /* The RS/6000 assembler uses a TOC which holds addresses of functions
177 and variables. Symbols are put in the TOC with the .tc pseudo-op.
178 A special relocation is used when accessing TOC entries. We handle
179 the TOC as a subsegment within the .data segment. We set it up if
180 we see a .toc pseudo-op, and save the csect symbol here. */
181 static symbolS *ppc_toc_csect;
182
183 /* The first frag in the TOC subsegment. */
184 static fragS *ppc_toc_frag;
185
186 /* The first frag in the first subsegment after the TOC in the .data
187 segment. NULL if there are no subsegments after the TOC. */
188 static fragS *ppc_after_toc_frag;
189
190 /* The current static block. */
191 static symbolS *ppc_current_block;
192
193 /* The COFF debugging section; set by md_begin. This is not the
194 .debug section, but is instead the secret BFD section which will
195 cause BFD to set the section number of a symbol to N_DEBUG. */
196 static asection *ppc_coff_debug_section;
197
198 /* The size of the .debug section. */
199 static bfd_size_type ppc_debug_name_section_size;
200
201 #endif /* OBJ_COFF */
202
203 #ifdef OBJ_ELF
204 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE" */
205 #endif /* OBJ_ELF */
206
207 #ifndef WORKING_DOT_WORD
208 const int md_short_jump_size = 4;
209 const int md_long_jump_size = 4;
210 #endif
211 \f
212 #ifdef OBJ_ELF
213 CONST char *md_shortopts = "um:VQ:";
214 #else
215 CONST char *md_shortopts = "um:";
216 #endif
217 struct option md_longopts[] = {
218 {NULL, no_argument, NULL, 0}
219 };
220 size_t md_longopts_size = sizeof(md_longopts);
221
222 int
223 md_parse_option (c, arg)
224 int c;
225 char *arg;
226 {
227 switch (c)
228 {
229 case 'u':
230 /* -u means that any undefined symbols should be treated as
231 external, which is the default for gas anyhow. */
232 break;
233
234 case 'm':
235 /* -mpwrx and -mpwr2 mean to assemble for the IBM POWER/2
236 (RIOS2). */
237 if (strcmp (arg, "pwrx") == 0 || strcmp (arg, "pwr2") == 0)
238 ppc_cpu = PPC_OPCODE_POWER | PPC_OPCODE_POWER2;
239 /* -mpwr means to assemble for the IBM POWER (RIOS1). */
240 else if (strcmp (arg, "pwr") == 0)
241 ppc_cpu = PPC_OPCODE_POWER;
242 /* -m601 means to assemble for the Motorola PowerPC 601. FIXME: We
243 ignore the option for now, but we should really use it to permit
244 instructions defined on the 601 that are not part of the standard
245 PowerPC architecture (mostly holdovers from the POWER). */
246 else if (strcmp (arg, "601") == 0)
247 ppc_cpu = PPC_OPCODE_PPC | PPC_OPCODE_601;
248 /* -mppc, -mppc32, -m603, and -m604 mean to assemble for the
249 Motorola PowerPC 603/604. */
250 else if (strcmp (arg, "ppc") == 0
251 || strcmp (arg, "ppc32") == 0
252 || strcmp (arg, "403") == 0
253 || strcmp (arg, "603") == 0
254 || strcmp (arg, "604") == 0)
255 ppc_cpu = PPC_OPCODE_PPC;
256 /* -mppc64 and -m620 mean to assemble for the 64-bit PowerPC
257 620. */
258 else if (strcmp (arg, "ppc64") == 0 || strcmp (arg, "620") == 0)
259 {
260 ppc_cpu = PPC_OPCODE_PPC;
261 ppc_size = PPC_OPCODE_64;
262 }
263 /* -many means to assemble for any architecture (PWR/PWRX/PPC). */
264 else if (strcmp (arg, "any") == 0)
265 ppc_cpu = PPC_OPCODE_POWER | PPC_OPCODE_POWER2 | PPC_OPCODE_PPC;
266
267 #ifdef OBJ_ELF
268 /* -mrelocatable -- warn about initializations that require relocation */
269 else if (strcmp (arg, "relocatable") == 0)
270 mrelocatable = true;
271
272 /* -mlittle/-mbig set the endianess */
273 else if (strcmp (arg, "little") == 0 || strcmp (arg, "little-endian") == 0)
274 target_big_endian = 0;
275
276 else if (strcmp (arg, "big") == 0 || strcmp (arg, "big-endian") == 0)
277 target_big_endian = 1;
278 #endif
279 else
280 {
281 as_bad ("invalid switch -m%s", arg);
282 return 0;
283 }
284 break;
285
286 #ifdef OBJ_ELF
287 /* -V: SVR4 argument to print version ID. */
288 case 'V':
289 print_version_id ();
290 break;
291
292 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
293 should be emitted or not. FIXME: Not implemented. */
294 case 'Q':
295 break;
296 #endif
297
298 default:
299 return 0;
300 }
301
302 return 1;
303 }
304
305 void
306 md_show_usage (stream)
307 FILE *stream;
308 {
309 fprintf(stream, "\
310 PowerPC options:\n\
311 -u ignored\n\
312 -mpwrx, -mpwr2 generate code for IBM POWER/2 (RIOS2)\n\
313 -mpwr generate code for IBM POWER (RIOS1)\n\
314 -m601 generate code for Motorola PowerPC 601\n\
315 -mppc, -mppc32, -m403, -m603, -m604\n\
316 generate code for Motorola PowerPC 603/604\n\
317 -mppc64, -m620 generate code for Motorola PowerPC 620\n\
318 -many generate code for any architecture (PWR/PWRX/PPC)\n");
319 #ifdef OBJ_ELF
320 fprintf(stream, "\
321 -mrelocatable support for GCC's -mrelocatble option\n\
322 -mlittle, -mlittle-endian\n\
323 generate code for a little endian machine\n\
324 -mbig, -mbig-endian generate code for a big endian machine\n\
325 -V print assembler version number\n\
326 -Qy, -Qn ignored\n");
327 #endif
328 }
329 \f
330 /* Set ppc_cpu if it is not already set. */
331
332 static void
333 ppc_set_cpu ()
334 {
335 const char *default_cpu = TARGET_CPU;
336
337 if (ppc_cpu == 0)
338 {
339 if (strcmp (default_cpu, "rs6000") == 0)
340 ppc_cpu = PPC_OPCODE_POWER;
341 else if (strcmp (default_cpu, "powerpc") == 0
342 || strcmp (default_cpu, "powerpcle") == 0)
343 ppc_cpu = PPC_OPCODE_PPC;
344 else
345 as_fatal ("Unknown default cpu = %s", default_cpu);
346 }
347 }
348
349 /* Figure out the BFD architecture to use. */
350
351 enum bfd_architecture
352 ppc_arch ()
353 {
354 ppc_set_cpu ();
355
356 if ((ppc_cpu & PPC_OPCODE_PPC) != 0)
357 return bfd_arch_powerpc;
358 else if ((ppc_cpu & PPC_OPCODE_POWER) != 0)
359 return bfd_arch_rs6000;
360 else
361 as_fatal ("Neither Power nor PowerPC opcodes were selected.");
362 }
363
364 /* This function is called when the assembler starts up. It is called
365 after the options have been parsed and the output file has been
366 opened. */
367
368 void
369 md_begin ()
370 {
371 register const struct powerpc_opcode *op;
372 const struct powerpc_opcode *op_end;
373 const struct powerpc_macro *macro;
374 const struct powerpc_macro *macro_end;
375
376 ppc_set_cpu ();
377
378 #ifdef OBJ_ELF
379 /* Set the -mrelocatable flag bit */
380 if (mrelocatable)
381 bfd_set_private_flags (stdoutput, EF_PPC_RELOCATABLE);
382 #endif
383
384 /* Insert the opcodes into a hash table. */
385 ppc_hash = hash_new ();
386
387 op_end = powerpc_opcodes + powerpc_num_opcodes;
388 for (op = powerpc_opcodes; op < op_end; op++)
389 {
390 know ((op->opcode & op->mask) == op->opcode);
391
392 if ((op->flags & ppc_cpu) != 0
393 && ((op->flags & (PPC_OPCODE_32 | PPC_OPCODE_64)) == 0
394 || (op->flags & (PPC_OPCODE_32 | PPC_OPCODE_64)) == ppc_size))
395 {
396 const char *retval;
397
398 retval = hash_insert (ppc_hash, op->name, (PTR) op);
399 if (retval != (const char *) NULL)
400 {
401 /* We permit a duplication of the mfdec instruction on
402 the 601, because it seems to have one value on the
403 601 and a different value on other PowerPC
404 processors. It's easier to permit a duplication than
405 to define a new instruction type flag. When using
406 -many, the comparison instructions are a harmless
407 special case. */
408 if (strcmp (retval, "exists") != 0
409 || (((ppc_cpu & PPC_OPCODE_601) == 0
410 || strcmp (op->name, "mfdec") != 0)
411 && (ppc_cpu != (PPC_OPCODE_POWER
412 | PPC_OPCODE_POWER2
413 | PPC_OPCODE_PPC)
414 || (strcmp (op->name, "cmpli") != 0
415 && strcmp (op->name, "cmpi") != 0
416 && strcmp (op->name, "cmp") != 0
417 && strcmp (op->name, "cmpl") != 0))))
418 abort ();
419 }
420 }
421 }
422
423 /* Insert the macros into a hash table. */
424 ppc_macro_hash = hash_new ();
425
426 macro_end = powerpc_macros + powerpc_num_macros;
427 for (macro = powerpc_macros; macro < macro_end; macro++)
428 {
429 if ((macro->flags & ppc_cpu) != 0)
430 {
431 const char *retval;
432
433 retval = hash_insert (ppc_macro_hash, macro->name, (PTR) macro);
434 if (retval != (const char *) NULL)
435 abort ();
436 }
437 }
438
439 /* Tell the main code what the endianness is. */
440 target_big_endian = PPC_BIG_ENDIAN;
441
442 #ifdef OBJ_COFF
443 ppc_coff_debug_section = coff_section_from_bfd_index (stdoutput, N_DEBUG);
444
445 /* Create dummy symbols to serve as initial csects. This forces the
446 text csects to precede the data csects. These symbols will not
447 be output. */
448 ppc_text_csects = symbol_make ("dummy\001");
449 ppc_text_csects->sy_tc.within = ppc_text_csects;
450 ppc_data_csects = symbol_make ("dummy\001");
451 ppc_data_csects->sy_tc.within = ppc_data_csects;
452 #endif
453 }
454
455 /* Insert an operand value into an instruction. */
456
457 static unsigned long
458 ppc_insert_operand (insn, operand, val, file, line)
459 unsigned long insn;
460 const struct powerpc_operand *operand;
461 offsetT val;
462 char *file;
463 unsigned int line;
464 {
465 if (operand->bits != 32)
466 {
467 long min, max;
468 offsetT test;
469
470 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
471 {
472 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0
473 && ppc_size == PPC_OPCODE_32)
474 max = (1 << operand->bits) - 1;
475 else
476 max = (1 << (operand->bits - 1)) - 1;
477 min = - (1 << (operand->bits - 1));
478 }
479 else
480 {
481 max = (1 << operand->bits) - 1;
482 min = 0;
483 }
484
485 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
486 test = - val;
487 else
488 test = val;
489
490 if (test < (offsetT) min || test > (offsetT) max)
491 {
492 const char *err =
493 "operand out of range (%s not between %ld and %ld)";
494 char buf[100];
495
496 sprint_value (buf, test);
497 if (file == (char *) NULL)
498 as_warn (err, buf, min, max);
499 else
500 as_warn_where (file, line, err, buf, min, max);
501 }
502 }
503
504 if (operand->insert)
505 {
506 const char *errmsg;
507
508 errmsg = NULL;
509 insn = (*operand->insert) (insn, (long) val, &errmsg);
510 if (errmsg != (const char *) NULL)
511 as_warn (errmsg);
512 }
513 else
514 insn |= (((long) val & ((1 << operand->bits) - 1))
515 << operand->shift);
516
517 return insn;
518 }
519
520 #ifdef OBJ_ELF
521 /* Parse @got, etc. and return the desired relocation. */
522 static bfd_reloc_code_real_type
523 ppc_elf_suffix (str_p)
524 char **str_p;
525 {
526 char *str = *str_p;
527
528 if (*str != '@')
529 return BFD_RELOC_UNUSED;
530
531 if (strncmp (str, "@GOT", 4) == 0 || strncmp (str, "@got", 4) == 0)
532 {
533 *str_p += 4;
534 return BFD_RELOC_PPC_TOC16;
535 }
536 else if (strncmp (str, "@L", 2) == 0 || strncmp (str, "@l", 2) == 0)
537 {
538 *str_p += 2;
539 return BFD_RELOC_LO16;
540 }
541 else if (strncmp (str, "@HA", 3) == 0 || strncmp (str, "@ha", 3) == 0)
542 {
543 *str_p += 3;
544 return BFD_RELOC_HI16_S;
545 }
546 else if (strncmp (str, "@H", 2) == 0 || strncmp (str, "@h", 2) == 0)
547 {
548 *str_p += 2;
549 return BFD_RELOC_HI16;
550 }
551 else if (strncmp (str, "@sdarel", 7) == 0 || strncmp (str, "@sdarel", 7) == 0)
552 {
553 *str_p += 7;
554 return BFD_RELOC_GPREL16;
555 }
556 else if (strncmp (str, "@FIXUP", 6) == 0 || strncmp (str, "@fixup", 6) == 0)
557 {
558 *str_p += 6;
559 return BFD_RELOC_CTOR; /* synonym for BFD_RELOC_32 that doesn't get */
560 } /* warnings with -mrelocatable */
561
562 return BFD_RELOC_UNUSED;
563 }
564
565 /* Like normal .long/.short/.word, except support @got, etc. */
566 /* clobbers input_line_pointer, checks */
567 /* end-of-line. */
568 static void
569 ppc_elf_cons (nbytes)
570 register int nbytes; /* 1=.byte, 2=.word, 4=.long */
571 {
572 expressionS exp;
573 bfd_reloc_code_real_type reloc;
574
575 if (is_it_end_of_statement ())
576 {
577 demand_empty_rest_of_line ();
578 return;
579 }
580
581 do
582 {
583 expression (&exp);
584 if (nbytes == 4
585 && exp.X_op == O_symbol
586 && *input_line_pointer == '@'
587 && (reloc = ppc_elf_suffix (&input_line_pointer)) != BFD_RELOC_UNUSED)
588 {
589 register char *p = frag_more ((int) nbytes);
590 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
591 int offset = (!reloc_howto) ? 0 : (nbytes - bfd_get_reloc_size (reloc_howto));
592
593 if (offset < 0)
594 offset = 0;
595
596 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, (int) nbytes - offset, &exp, 0, reloc);
597 }
598 else
599 emit_expr (&exp, (unsigned int) nbytes);
600 }
601 while (*input_line_pointer++ == ',');
602
603 input_line_pointer--; /* Put terminator back into stream. */
604 demand_empty_rest_of_line ();
605 }
606
607 /* Validate any relocations emitted for -mrelocatable, possibly adding
608 fixups for word relocations in writable segments, so we can adjust
609 them at runtime. */
610 static void
611 ppc_elf_validate_fix (fixp, seg)
612 fixS *fixp;
613 segT seg;
614 {
615 if (mrelocatable
616 && !fixp->fx_done
617 && !fixp->fx_pcrel
618 && fixp->fx_r_type <= BFD_RELOC_UNUSED
619 && strcmp (segment_name (seg), ".got2") != 0
620 && strcmp (segment_name (seg), ".dtors") != 0
621 && strcmp (segment_name (seg), ".ctors") != 0
622 && strcmp (segment_name (seg), ".fixup") != 0
623 && strcmp (segment_name (seg), ".stab") != 0)
624 {
625 if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
626 || fixp->fx_r_type != BFD_RELOC_CTOR)
627 {
628 as_warn_where (fixp->fx_file, fixp->fx_line,
629 "Relocation cannot be done when using -mrelocatable");
630 }
631 }
632 }
633
634 #endif /* OBJ_ELF */
635
636 /* We need to keep a list of fixups. We can't simply generate them as
637 we go, because that would require us to first create the frag, and
638 that would screw up references to ``.''. */
639
640 struct ppc_fixup
641 {
642 expressionS exp;
643 int opindex;
644 bfd_reloc_code_real_type reloc;
645 };
646
647 #define MAX_INSN_FIXUPS (5)
648
649 /* This routine is called for each instruction to be assembled. */
650
651 void
652 md_assemble (str)
653 char *str;
654 {
655 char *s;
656 const struct powerpc_opcode *opcode;
657 unsigned long insn;
658 const unsigned char *opindex_ptr;
659 int skip_optional;
660 int need_paren;
661 int next_opindex;
662 struct ppc_fixup fixups[MAX_INSN_FIXUPS];
663 int fc;
664 char *f;
665 int i;
666 bfd_reloc_code_real_type reloc;
667
668 /* Get the opcode. */
669 for (s = str; *s != '\0' && ! isspace (*s); s++)
670 ;
671 if (*s != '\0')
672 *s++ = '\0';
673
674 /* Look up the opcode in the hash table. */
675 opcode = (const struct powerpc_opcode *) hash_find (ppc_hash, str);
676 if (opcode == (const struct powerpc_opcode *) NULL)
677 {
678 const struct powerpc_macro *macro;
679
680 macro = (const struct powerpc_macro *) hash_find (ppc_macro_hash, str);
681 if (macro == (const struct powerpc_macro *) NULL)
682 as_bad ("Unrecognized opcode: `%s'", str);
683 else
684 ppc_macro (s, macro);
685
686 return;
687 }
688
689 insn = opcode->opcode;
690
691 str = s;
692 while (isspace (*str))
693 ++str;
694
695 /* PowerPC operands are just expressions. The only real issue is
696 that a few operand types are optional. All cases which might use
697 an optional operand separate the operands only with commas (in
698 some cases parentheses are used, as in ``lwz 1,0(1)'' but such
699 cases never have optional operands). There is never more than
700 one optional operand for an instruction. So, before we start
701 seriously parsing the operands, we check to see if we have an
702 optional operand, and, if we do, we count the number of commas to
703 see whether the operand should be omitted. */
704 skip_optional = 0;
705 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
706 {
707 const struct powerpc_operand *operand;
708
709 operand = &powerpc_operands[*opindex_ptr];
710 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
711 {
712 unsigned int opcount;
713
714 /* There is an optional operand. Count the number of
715 commas in the input line. */
716 if (*str == '\0')
717 opcount = 0;
718 else
719 {
720 opcount = 1;
721 s = str;
722 while ((s = strchr (s, ',')) != (char *) NULL)
723 {
724 ++opcount;
725 ++s;
726 }
727 }
728
729 /* If there are fewer operands in the line then are called
730 for by the instruction, we want to skip the optional
731 operand. */
732 if (opcount < strlen (opcode->operands))
733 skip_optional = 1;
734
735 break;
736 }
737 }
738
739 /* Gather the operands. */
740 need_paren = 0;
741 next_opindex = 0;
742 fc = 0;
743 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
744 {
745 const struct powerpc_operand *operand;
746 const char *errmsg;
747 char *hold;
748 expressionS ex;
749 char endc;
750
751 if (next_opindex == 0)
752 operand = &powerpc_operands[*opindex_ptr];
753 else
754 {
755 operand = &powerpc_operands[next_opindex];
756 next_opindex = 0;
757 }
758
759 errmsg = NULL;
760
761 /* If this is a fake operand, then we do not expect anything
762 from the input. */
763 if ((operand->flags & PPC_OPERAND_FAKE) != 0)
764 {
765 insn = (*operand->insert) (insn, 0L, &errmsg);
766 if (errmsg != (const char *) NULL)
767 as_warn (errmsg);
768 continue;
769 }
770
771 /* If this is an optional operand, and we are skipping it, just
772 insert a zero. */
773 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
774 && skip_optional)
775 {
776 if (operand->insert)
777 {
778 insn = (*operand->insert) (insn, 0L, &errmsg);
779 if (errmsg != (const char *) NULL)
780 as_warn (errmsg);
781 }
782 if ((operand->flags & PPC_OPERAND_NEXT) != 0)
783 next_opindex = *opindex_ptr + 1;
784 continue;
785 }
786
787 /* Gather the operand. */
788 hold = input_line_pointer;
789 input_line_pointer = str;
790 expression (&ex);
791 str = input_line_pointer;
792 input_line_pointer = hold;
793
794 if (ex.X_op == O_illegal)
795 as_bad ("illegal operand");
796 else if (ex.X_op == O_absent)
797 as_bad ("missing operand");
798 else if (ex.X_op == O_constant)
799 {
800 #ifdef OBJ_ELF
801 /* Allow @HA, @L, @H on constants. */
802 char *orig_str = str;
803 if ((reloc = ppc_elf_suffix (&str)) != BFD_RELOC_UNUSED)
804 switch (reloc)
805 {
806 default:
807 str = orig_str;
808 break;
809
810 case BFD_RELOC_LO16:
811 ex.X_add_number = ((ex.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
812 break;
813
814 case BFD_RELOC_HI16:
815 ex.X_add_number = (ex.X_add_number >> 16) & 0xffff;
816 break;
817
818 case BFD_RELOC_HI16_S:
819 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff)
820 + ((ex.X_add_number >> 15) & 1);
821 break;
822 }
823 #endif
824 insn = ppc_insert_operand (insn, operand, ex.X_add_number,
825 (char *) NULL, 0);
826 }
827
828 #ifdef OBJ_ELF
829 else if ((reloc = ppc_elf_suffix (&str)) != BFD_RELOC_UNUSED)
830 {
831 /* We need to generate a fixup for this expression. */
832 if (fc >= MAX_INSN_FIXUPS)
833 as_fatal ("too many fixups");
834 fixups[fc].exp = ex;
835 fixups[fc].opindex = 0;
836 fixups[fc].reloc = reloc;
837 ++fc;
838 }
839 #endif /* OBJ_ELF */
840
841 else
842 {
843 /* We need to generate a fixup for this expression. */
844 if (fc >= MAX_INSN_FIXUPS)
845 as_fatal ("too many fixups");
846 fixups[fc].exp = ex;
847 fixups[fc].opindex = *opindex_ptr;
848 fixups[fc].reloc = BFD_RELOC_UNUSED;
849 ++fc;
850 }
851
852 if (need_paren)
853 {
854 endc = ')';
855 need_paren = 0;
856 }
857 else if ((operand->flags & PPC_OPERAND_PARENS) != 0)
858 {
859 endc = '(';
860 need_paren = 1;
861 }
862 else
863 endc = ',';
864
865 /* The call to expression should have advanced str past any
866 whitespace. */
867 if (*str != endc
868 && (endc != ',' || *str != '\0'))
869 {
870 as_bad ("syntax error; found `%c' but expected `%c'", *str, endc);
871 break;
872 }
873
874 if (*str != '\0')
875 ++str;
876 }
877
878 while (isspace (*str))
879 ++str;
880
881 if (*str != '\0')
882 as_bad ("junk at end of line: `%s'", str);
883
884 /* Write out the instruction. */
885 f = frag_more (4);
886 md_number_to_chars (f, insn, 4);
887
888 /* Create any fixups. At this point we do not use a
889 bfd_reloc_code_real_type, but instead just use the
890 BFD_RELOC_UNUSED plus the operand index. This lets us easily
891 handle fixups for any operand type, although that is admittedly
892 not a very exciting feature. We pick a BFD reloc type in
893 md_apply_fix. */
894 for (i = 0; i < fc; i++)
895 {
896 const struct powerpc_operand *operand;
897
898 operand = &powerpc_operands[fixups[i].opindex];
899 if (fixups[i].reloc != BFD_RELOC_UNUSED)
900 {
901 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
902 int size = (!reloc_howto) ? 0 : bfd_get_reloc_size (reloc_howto);
903 int offset = target_big_endian ? (4 - size) : 0;
904 fixS *fixP;
905
906 if (size > 4)
907 abort();
908
909 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
910 &fixups[i].exp, (reloc_howto && reloc_howto->pc_relative),
911 fixups[i].reloc);
912
913 /* Turn off complaints that the addend is too large for things like
914 foo+100000@ha. */
915 switch (fixups[i].reloc)
916 {
917 case BFD_RELOC_LO16:
918 case BFD_RELOC_HI16:
919 case BFD_RELOC_HI16_S:
920 fixP->fx_no_overflow = 1;
921 break;
922 }
923 }
924 else
925 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
926 &fixups[i].exp,
927 (operand->flags & PPC_OPERAND_RELATIVE) != 0,
928 ((bfd_reloc_code_real_type)
929 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
930 }
931 }
932
933 #ifndef WORKING_DOT_WORD
934 /* Handle long and short jumps */
935 void
936 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
937 char *ptr;
938 addressT from_addr, to_addr;
939 fragS *frag;
940 symbolS *to_symbol;
941 {
942 abort ();
943 }
944
945 void
946 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
947 char *ptr;
948 addressT from_addr, to_addr;
949 fragS *frag;
950 symbolS *to_symbol;
951 {
952 abort ();
953 }
954 #endif
955
956 /* Handle a macro. Gather all the operands, transform them as
957 described by the macro, and call md_assemble recursively. All the
958 operands are separated by commas; we don't accept parentheses
959 around operands here. */
960
961 static void
962 ppc_macro (str, macro)
963 char *str;
964 const struct powerpc_macro *macro;
965 {
966 char *operands[10];
967 int count;
968 char *s;
969 unsigned int len;
970 const char *format;
971 int arg;
972 char *send;
973 char *complete;
974
975 /* Gather the users operands into the operands array. */
976 count = 0;
977 s = str;
978 while (1)
979 {
980 if (count >= sizeof operands / sizeof operands[0])
981 break;
982 operands[count++] = s;
983 s = strchr (s, ',');
984 if (s == (char *) NULL)
985 break;
986 *s++ = '\0';
987 }
988
989 if (count != macro->operands)
990 {
991 as_bad ("wrong number of operands");
992 return;
993 }
994
995 /* Work out how large the string must be (the size is unbounded
996 because it includes user input). */
997 len = 0;
998 format = macro->format;
999 while (*format != '\0')
1000 {
1001 if (*format != '%')
1002 {
1003 ++len;
1004 ++format;
1005 }
1006 else
1007 {
1008 arg = strtol (format + 1, &send, 10);
1009 know (send != format && arg >= 0 && arg < count);
1010 len += strlen (operands[arg]);
1011 format = send;
1012 }
1013 }
1014
1015 /* Put the string together. */
1016 complete = s = (char *) alloca (len + 1);
1017 format = macro->format;
1018 while (*format != '\0')
1019 {
1020 if (*format != '%')
1021 *s++ = *format++;
1022 else
1023 {
1024 arg = strtol (format + 1, &send, 10);
1025 strcpy (s, operands[arg]);
1026 s += strlen (s);
1027 format = send;
1028 }
1029 }
1030 *s = '\0';
1031
1032 /* Assemble the constructed instruction. */
1033 md_assemble (complete);
1034 }
1035 \f
1036 /* Pseudo-op handling. */
1037
1038 /* The .byte pseudo-op. This is similar to the normal .byte
1039 pseudo-op, but it can also take a single ASCII string. */
1040
1041 static void
1042 ppc_byte (ignore)
1043 int ignore;
1044 {
1045 if (*input_line_pointer != '\"')
1046 {
1047 cons (1);
1048 return;
1049 }
1050
1051 /* Gather characters. A real double quote is doubled. Unusual
1052 characters are not permitted. */
1053 ++input_line_pointer;
1054 while (1)
1055 {
1056 char c;
1057
1058 c = *input_line_pointer++;
1059
1060 if (c == '\"')
1061 {
1062 if (*input_line_pointer != '\"')
1063 break;
1064 ++input_line_pointer;
1065 }
1066
1067 FRAG_APPEND_1_CHAR (c);
1068 }
1069
1070 demand_empty_rest_of_line ();
1071 }
1072 \f
1073 #ifdef OBJ_COFF
1074
1075 /* XCOFF specific pseudo-op handling. */
1076
1077 /* The .comm and .lcomm pseudo-ops for XCOFF. XCOFF puts common
1078 symbols in the .bss segment as though they were local common
1079 symbols, and uses a different smclas. */
1080
1081 static void
1082 ppc_comm (lcomm)
1083 int lcomm;
1084 {
1085 asection *current_seg = now_seg;
1086 subsegT current_subseg = now_subseg;
1087 char *name;
1088 char endc;
1089 char *end_name;
1090 offsetT size;
1091 offsetT align;
1092 symbolS *lcomm_sym = NULL;
1093 symbolS *sym;
1094 char *pfrag;
1095
1096 name = input_line_pointer;
1097 endc = get_symbol_end ();
1098 end_name = input_line_pointer;
1099 *end_name = endc;
1100
1101 if (*input_line_pointer != ',')
1102 {
1103 as_bad ("missing size");
1104 ignore_rest_of_line ();
1105 return;
1106 }
1107 ++input_line_pointer;
1108
1109 size = get_absolute_expression ();
1110 if (size < 0)
1111 {
1112 as_bad ("negative size");
1113 ignore_rest_of_line ();
1114 return;
1115 }
1116
1117 if (! lcomm)
1118 {
1119 /* The third argument to .comm is the alignment. */
1120 if (*input_line_pointer != ',')
1121 align = 3;
1122 else
1123 {
1124 ++input_line_pointer;
1125 align = get_absolute_expression ();
1126 if (align <= 0)
1127 {
1128 as_warn ("ignoring bad alignment");
1129 align = 3;
1130 }
1131 }
1132 }
1133 else
1134 {
1135 char *lcomm_name;
1136 char lcomm_endc;
1137
1138 if (size <= 1)
1139 align = 0;
1140 else if (size <= 2)
1141 align = 1;
1142 else if (size <= 4)
1143 align = 2;
1144 else
1145 align = 3;
1146
1147 /* The third argument to .lcomm appears to be the real local
1148 common symbol to create. References to the symbol named in
1149 the first argument are turned into references to the third
1150 argument. */
1151 if (*input_line_pointer != ',')
1152 {
1153 as_bad ("missing real symbol name");
1154 ignore_rest_of_line ();
1155 return;
1156 }
1157 ++input_line_pointer;
1158
1159 lcomm_name = input_line_pointer;
1160 lcomm_endc = get_symbol_end ();
1161
1162 lcomm_sym = symbol_find_or_make (lcomm_name);
1163
1164 *input_line_pointer = lcomm_endc;
1165 }
1166
1167 *end_name = '\0';
1168 sym = symbol_find_or_make (name);
1169 *end_name = endc;
1170
1171 if (S_IS_DEFINED (sym)
1172 || S_GET_VALUE (sym) != 0)
1173 {
1174 as_bad ("attempt to redefine symbol");
1175 ignore_rest_of_line ();
1176 return;
1177 }
1178
1179 record_alignment (bss_section, align);
1180
1181 if (! lcomm
1182 || ! S_IS_DEFINED (lcomm_sym))
1183 {
1184 symbolS *def_sym;
1185 offsetT def_size;
1186
1187 if (! lcomm)
1188 {
1189 def_sym = sym;
1190 def_size = size;
1191 S_SET_EXTERNAL (sym);
1192 }
1193 else
1194 {
1195 lcomm_sym->sy_tc.output = 1;
1196 def_sym = lcomm_sym;
1197 def_size = 0;
1198 }
1199
1200 subseg_set (bss_section, 1);
1201 frag_align (align, 0);
1202
1203 def_sym->sy_frag = frag_now;
1204 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, def_sym,
1205 def_size, (char *) NULL);
1206 *pfrag = 0;
1207 S_SET_SEGMENT (def_sym, bss_section);
1208 def_sym->sy_tc.align = align;
1209 }
1210 else if (lcomm)
1211 {
1212 /* Align the size of lcomm_sym. */
1213 lcomm_sym->sy_frag->fr_offset =
1214 ((lcomm_sym->sy_frag->fr_offset + (1 << align) - 1)
1215 &~ ((1 << align) - 1));
1216 if (align > lcomm_sym->sy_tc.align)
1217 lcomm_sym->sy_tc.align = align;
1218 }
1219
1220 if (lcomm)
1221 {
1222 /* Make sym an offset from lcomm_sym. */
1223 S_SET_SEGMENT (sym, bss_section);
1224 sym->sy_frag = lcomm_sym->sy_frag;
1225 S_SET_VALUE (sym, lcomm_sym->sy_frag->fr_offset);
1226 lcomm_sym->sy_frag->fr_offset += size;
1227 }
1228
1229 subseg_set (current_seg, current_subseg);
1230
1231 demand_empty_rest_of_line ();
1232 }
1233
1234 /* The .csect pseudo-op. This switches us into a different
1235 subsegment. The first argument is a symbol whose value is the
1236 start of the .csect. In COFF, csect symbols get special aux
1237 entries defined by the x_csect field of union internal_auxent. The
1238 optional second argument is the alignment (the default is 2). */
1239
1240 static void
1241 ppc_csect (ignore)
1242 int ignore;
1243 {
1244 char *name;
1245 char endc;
1246 symbolS *sym;
1247
1248 name = input_line_pointer;
1249 endc = get_symbol_end ();
1250
1251 sym = symbol_find_or_make (name);
1252
1253 *input_line_pointer = endc;
1254
1255 if (S_IS_DEFINED (sym))
1256 subseg_set (S_GET_SEGMENT (sym), sym->sy_tc.subseg);
1257 else
1258 {
1259 symbolS **list_ptr;
1260 int after_toc;
1261 symbolS *list;
1262
1263 /* This is a new csect. We need to look at the symbol class to
1264 figure out whether it should go in the text section or the
1265 data section. */
1266 after_toc = 0;
1267 switch (sym->sy_tc.class)
1268 {
1269 case XMC_PR:
1270 case XMC_RO:
1271 case XMC_DB:
1272 case XMC_GL:
1273 case XMC_XO:
1274 case XMC_SV:
1275 case XMC_TI:
1276 case XMC_TB:
1277 S_SET_SEGMENT (sym, text_section);
1278 sym->sy_tc.subseg = ppc_text_subsegment;
1279 ++ppc_text_subsegment;
1280 list_ptr = &ppc_text_csects;
1281 break;
1282 case XMC_RW:
1283 case XMC_TC0:
1284 case XMC_TC:
1285 case XMC_DS:
1286 case XMC_UA:
1287 case XMC_BS:
1288 case XMC_UC:
1289 if (ppc_toc_csect->sy_tc.subseg + 1 == ppc_data_subsegment)
1290 after_toc = 1;
1291 S_SET_SEGMENT (sym, data_section);
1292 sym->sy_tc.subseg = ppc_data_subsegment;
1293 ++ppc_data_subsegment;
1294 list_ptr = &ppc_data_csects;
1295 break;
1296 default:
1297 abort ();
1298 }
1299
1300 subseg_new (segment_name (S_GET_SEGMENT (sym)), sym->sy_tc.subseg);
1301 if (after_toc)
1302 ppc_after_toc_frag = frag_now;
1303
1304 sym->sy_frag = frag_now;
1305 S_SET_VALUE (sym, (valueT) frag_now_fix ());
1306
1307 sym->sy_tc.align = 2;
1308 sym->sy_tc.output = 1;
1309 sym->sy_tc.within = sym;
1310
1311 for (list = *list_ptr;
1312 list->sy_tc.next != (symbolS *) NULL;
1313 list = list->sy_tc.next)
1314 ;
1315 list->sy_tc.next = sym;
1316
1317 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
1318 symbol_append (sym, list->sy_tc.within, &symbol_rootP, &symbol_lastP);
1319 }
1320
1321 if (*input_line_pointer == ',')
1322 {
1323 ++input_line_pointer;
1324 sym->sy_tc.align = get_absolute_expression ();
1325 }
1326
1327 ppc_current_csect = sym;
1328
1329 demand_empty_rest_of_line ();
1330 }
1331
1332 /* The .extern pseudo-op. We create an undefined symbol. */
1333
1334 static void
1335 ppc_extern (ignore)
1336 int ignore;
1337 {
1338 char *name;
1339 char endc;
1340
1341 name = input_line_pointer;
1342 endc = get_symbol_end ();
1343
1344 (void) symbol_find_or_make (name);
1345
1346 *input_line_pointer = endc;
1347
1348 demand_empty_rest_of_line ();
1349 }
1350
1351 /* The .lglobl pseudo-op. I think the RS/6000 assembler only needs
1352 this because it can't handle undefined symbols. I think we can
1353 just ignore it. */
1354
1355 static void
1356 ppc_lglobl (ignore)
1357 int ignore;
1358 {
1359 s_ignore (0);
1360 }
1361
1362 /* The .rename pseudo-op. The RS/6000 assembler can rename symbols,
1363 although I don't know why it bothers. */
1364
1365 static void
1366 ppc_rename (ignore)
1367 int ignore;
1368 {
1369 char *name;
1370 char endc;
1371 symbolS *sym;
1372 int len;
1373
1374 name = input_line_pointer;
1375 endc = get_symbol_end ();
1376
1377 sym = symbol_find_or_make (name);
1378
1379 *input_line_pointer = endc;
1380
1381 if (*input_line_pointer != ',')
1382 {
1383 as_bad ("missing rename string");
1384 ignore_rest_of_line ();
1385 return;
1386 }
1387 ++input_line_pointer;
1388
1389 sym->sy_tc.real_name = demand_copy_C_string (&len);
1390
1391 demand_empty_rest_of_line ();
1392 }
1393
1394 /* The .stabx pseudo-op. This is similar to a normal .stabs
1395 pseudo-op, but slightly different. A sample is
1396 .stabx "main:F-1",.main,142,0
1397 The first argument is the symbol name to create. The second is the
1398 value, and the third is the storage class. The fourth seems to be
1399 always zero, and I am assuming it is the type. */
1400
1401 static void
1402 ppc_stabx (ignore)
1403 int ignore;
1404 {
1405 char *name;
1406 int len;
1407 symbolS *sym;
1408 expressionS exp;
1409
1410 name = demand_copy_C_string (&len);
1411
1412 if (*input_line_pointer != ',')
1413 {
1414 as_bad ("missing value");
1415 return;
1416 }
1417 ++input_line_pointer;
1418
1419 sym = symbol_make (name);
1420
1421 (void) expression (&exp);
1422
1423 switch (exp.X_op)
1424 {
1425 case O_illegal:
1426 case O_absent:
1427 case O_big:
1428 as_bad ("illegal .stabx expression; zero assumed");
1429 exp.X_add_number = 0;
1430 /* Fall through. */
1431 case O_constant:
1432 S_SET_VALUE (sym, (valueT) exp.X_add_number);
1433 sym->sy_frag = &zero_address_frag;
1434 break;
1435
1436 case O_symbol:
1437 if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section)
1438 sym->sy_value = exp;
1439 else
1440 {
1441 S_SET_VALUE (sym,
1442 exp.X_add_number + S_GET_VALUE (exp.X_add_symbol));
1443 sym->sy_frag = exp.X_add_symbol->sy_frag;
1444 }
1445 break;
1446
1447 default:
1448 /* The value is some complex expression. This will probably
1449 fail at some later point, but this is probably the right
1450 thing to do here. */
1451 sym->sy_value = exp;
1452 break;
1453 }
1454
1455 S_SET_SEGMENT (sym, ppc_coff_debug_section);
1456 sym->bsym->flags |= BSF_DEBUGGING;
1457
1458 if (*input_line_pointer != ',')
1459 {
1460 as_bad ("missing class");
1461 return;
1462 }
1463 ++input_line_pointer;
1464
1465 S_SET_STORAGE_CLASS (sym, get_absolute_expression ());
1466
1467 if (*input_line_pointer != ',')
1468 {
1469 as_bad ("missing type");
1470 return;
1471 }
1472 ++input_line_pointer;
1473
1474 S_SET_DATA_TYPE (sym, get_absolute_expression ());
1475
1476 sym->sy_tc.output = 1;
1477
1478 if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
1479 sym->sy_tc.within = ppc_current_block;
1480
1481 if (exp.X_op != O_symbol
1482 || ! S_IS_EXTERNAL (exp.X_add_symbol)
1483 || S_GET_SEGMENT (exp.X_add_symbol) != bss_section)
1484 ppc_frob_label (sym);
1485 else
1486 {
1487 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
1488 symbol_append (sym, exp.X_add_symbol, &symbol_rootP, &symbol_lastP);
1489 if (ppc_current_csect->sy_tc.within == exp.X_add_symbol)
1490 ppc_current_csect->sy_tc.within = sym;
1491 }
1492
1493 if (strlen (name) > SYMNMLEN)
1494 {
1495 /* For some reason, each name is preceded by a two byte length
1496 and followed by a null byte. */
1497 ppc_debug_name_section_size += strlen (name) + 3;
1498 }
1499
1500 demand_empty_rest_of_line ();
1501 }
1502
1503 /* The .function pseudo-op. This takes several arguments. The first
1504 argument seems to be the external name of the symbol. The second
1505 argment seems to be the label for the start of the function. gcc
1506 uses the same name for both. I have no idea what the third and
1507 fourth arguments are meant to be. The optional fifth argument is
1508 an expression for the size of the function. In COFF this symbol
1509 gets an aux entry like that used for a csect. */
1510
1511 static void
1512 ppc_function (ignore)
1513 int ignore;
1514 {
1515 char *name;
1516 char endc;
1517 char *s;
1518 symbolS *ext_sym;
1519 symbolS *lab_sym;
1520
1521 name = input_line_pointer;
1522 endc = get_symbol_end ();
1523
1524 /* Ignore any [PR] suffix. */
1525 name = ppc_canonicalize_symbol_name (name);
1526 s = strchr (name, '[');
1527 if (s != (char *) NULL
1528 && strcmp (s + 1, "PR]") == 0)
1529 *s = '\0';
1530
1531 ext_sym = symbol_find_or_make (name);
1532
1533 *input_line_pointer = endc;
1534
1535 if (*input_line_pointer != ',')
1536 {
1537 as_bad ("missing symbol name");
1538 ignore_rest_of_line ();
1539 return;
1540 }
1541 ++input_line_pointer;
1542
1543 name = input_line_pointer;
1544 endc = get_symbol_end ();
1545
1546 lab_sym = symbol_find_or_make (name);
1547
1548 *input_line_pointer = endc;
1549
1550 if (ext_sym != lab_sym)
1551 {
1552 ext_sym->sy_value.X_op = O_symbol;
1553 ext_sym->sy_value.X_add_symbol = lab_sym;
1554 ext_sym->sy_value.X_op_symbol = NULL;
1555 ext_sym->sy_value.X_add_number = 0;
1556 }
1557
1558 if (ext_sym->sy_tc.class == -1)
1559 ext_sym->sy_tc.class = XMC_PR;
1560 ext_sym->sy_tc.output = 1;
1561
1562 if (*input_line_pointer == ',')
1563 {
1564 expressionS ignore;
1565
1566 /* Ignore the third argument. */
1567 ++input_line_pointer;
1568 expression (&ignore);
1569 if (*input_line_pointer == ',')
1570 {
1571 /* Ignore the fourth argument. */
1572 ++input_line_pointer;
1573 expression (&ignore);
1574 if (*input_line_pointer == ',')
1575 {
1576 /* The fifth argument is the function size. */
1577 ++input_line_pointer;
1578 ext_sym->sy_tc.size = symbol_new ("L0\001",
1579 absolute_section,
1580 (valueT) 0,
1581 &zero_address_frag);
1582 pseudo_set (ext_sym->sy_tc.size);
1583 }
1584 }
1585 }
1586
1587 S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
1588 SF_SET_FUNCTION (ext_sym);
1589 SF_SET_PROCESS (ext_sym);
1590 coff_add_linesym (ext_sym);
1591
1592 demand_empty_rest_of_line ();
1593 }
1594
1595 /* The .bf pseudo-op. This is just like a COFF C_FCN symbol named
1596 ".bf". */
1597
1598 static void
1599 ppc_bf (ignore)
1600 int ignore;
1601 {
1602 symbolS *sym;
1603
1604 sym = symbol_make (".bf");
1605 S_SET_SEGMENT (sym, text_section);
1606 sym->sy_frag = frag_now;
1607 S_SET_VALUE (sym, frag_now_fix ());
1608 S_SET_STORAGE_CLASS (sym, C_FCN);
1609
1610 coff_line_base = get_absolute_expression ();
1611
1612 S_SET_NUMBER_AUXILIARY (sym, 1);
1613 SA_SET_SYM_LNNO (sym, coff_line_base);
1614
1615 sym->sy_tc.output = 1;
1616
1617 ppc_frob_label (sym);
1618
1619 demand_empty_rest_of_line ();
1620 }
1621
1622 /* The .ef pseudo-op. This is just like a COFF C_FCN symbol named
1623 ".ef", except that the line number is absolute, not relative to the
1624 most recent ".bf" symbol. */
1625
1626 static void
1627 ppc_ef (ignore)
1628 int ignore;
1629 {
1630 symbolS *sym;
1631
1632 sym = symbol_make (".ef");
1633 S_SET_SEGMENT (sym, text_section);
1634 sym->sy_frag = frag_now;
1635 S_SET_VALUE (sym, frag_now_fix ());
1636 S_SET_STORAGE_CLASS (sym, C_FCN);
1637 S_SET_NUMBER_AUXILIARY (sym, 1);
1638 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
1639 sym->sy_tc.output = 1;
1640
1641 ppc_frob_label (sym);
1642
1643 demand_empty_rest_of_line ();
1644 }
1645
1646 /* The .bi and .ei pseudo-ops. These take a string argument and
1647 generates a C_BINCL or C_EINCL symbol, which goes at the start of
1648 the symbol list. */
1649
1650 static void
1651 ppc_biei (ei)
1652 int ei;
1653 {
1654 char *name;
1655 int len;
1656 symbolS *sym;
1657 symbolS *look;
1658
1659 name = demand_copy_C_string (&len);
1660
1661 sym = symbol_make (name);
1662 S_SET_SEGMENT (sym, ppc_coff_debug_section);
1663 sym->bsym->flags |= BSF_DEBUGGING;
1664
1665 /* FIXME: The value of the .bi or .ei symbol is supposed to be the
1666 offset in the file to the line number entry to use. That is
1667 quite difficult to implement using BFD, so I'm just not doing it.
1668 Sorry. Please add it if you can figure out how. Note that this
1669 approach is the only way to support multiple files in COFF, since
1670 line numbers are associated with function symbols. Note further
1671 that it still doesn't work, since the line numbers are stored as
1672 offsets from a base line number. */
1673
1674 S_SET_STORAGE_CLASS (sym, ei ? C_EINCL : C_BINCL);
1675 sym->sy_tc.output = 1;
1676
1677 for (look = symbol_rootP;
1678 (look != (symbolS *) NULL
1679 && (S_GET_STORAGE_CLASS (look) == C_FILE
1680 || S_GET_STORAGE_CLASS (look) == C_BINCL
1681 || S_GET_STORAGE_CLASS (look) == C_EINCL));
1682 look = symbol_next (look))
1683 ;
1684 if (look != (symbolS *) NULL)
1685 {
1686 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
1687 symbol_insert (sym, look, &symbol_rootP, &symbol_lastP);
1688 }
1689
1690 demand_empty_rest_of_line ();
1691 }
1692
1693 /* The .bs pseudo-op. This generates a C_BSTAT symbol named ".bs".
1694 There is one argument, which is a csect symbol. The value of the
1695 .bs symbol is the index of this csect symbol. */
1696
1697 static void
1698 ppc_bs (ignore)
1699 int ignore;
1700 {
1701 char *name;
1702 char endc;
1703 symbolS *csect;
1704 symbolS *sym;
1705
1706 if (ppc_current_block != NULL)
1707 as_bad ("nested .bs blocks");
1708
1709 name = input_line_pointer;
1710 endc = get_symbol_end ();
1711
1712 csect = symbol_find_or_make (name);
1713
1714 *input_line_pointer = endc;
1715
1716 sym = symbol_make (".bs");
1717 S_SET_SEGMENT (sym, now_seg);
1718 S_SET_STORAGE_CLASS (sym, C_BSTAT);
1719 sym->bsym->flags |= BSF_DEBUGGING;
1720 sym->sy_tc.output = 1;
1721
1722 sym->sy_tc.within = csect;
1723
1724 ppc_frob_label (sym);
1725
1726 ppc_current_block = sym;
1727
1728 demand_empty_rest_of_line ();
1729 }
1730
1731 /* The .es pseudo-op. Generate a C_ESTART symbol named .es. */
1732
1733 static void
1734 ppc_es (ignore)
1735 int ignore;
1736 {
1737 symbolS *sym;
1738
1739 if (ppc_current_block == NULL)
1740 as_bad (".es without preceding .bs");
1741
1742 sym = symbol_make (".es");
1743 S_SET_SEGMENT (sym, now_seg);
1744 S_SET_STORAGE_CLASS (sym, C_ESTAT);
1745 sym->bsym->flags |= BSF_DEBUGGING;
1746 sym->sy_tc.output = 1;
1747
1748 ppc_frob_label (sym);
1749
1750 ppc_current_block = NULL;
1751
1752 demand_empty_rest_of_line ();
1753 }
1754
1755 /* The .bb pseudo-op. Generate a C_BLOCK symbol named .bb, with a
1756 line number. */
1757
1758 static void
1759 ppc_bb (ignore)
1760 int ignore;
1761 {
1762 symbolS *sym;
1763
1764 sym = symbol_make (".bb");
1765 S_SET_SEGMENT (sym, text_section);
1766 sym->sy_frag = frag_now;
1767 S_SET_VALUE (sym, frag_now_fix ());
1768 S_SET_STORAGE_CLASS (sym, C_BLOCK);
1769
1770 S_SET_NUMBER_AUXILIARY (sym, 1);
1771 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
1772
1773 sym->sy_tc.output = 1;
1774
1775 ppc_frob_label (sym);
1776
1777 demand_empty_rest_of_line ();
1778 }
1779
1780 /* The .eb pseudo-op. Generate a C_BLOCK symbol named .eb, with a
1781 line number. */
1782
1783 static void
1784 ppc_eb (ignore)
1785 int ignore;
1786 {
1787 symbolS *sym;
1788
1789 sym = symbol_make (".eb");
1790 S_SET_SEGMENT (sym, text_section);
1791 sym->sy_frag = frag_now;
1792 S_SET_VALUE (sym, frag_now_fix ());
1793 S_SET_STORAGE_CLASS (sym, C_FCN);
1794 S_SET_NUMBER_AUXILIARY (sym, 1);
1795 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
1796 sym->sy_tc.output = 1;
1797
1798 ppc_frob_label (sym);
1799
1800 demand_empty_rest_of_line ();
1801 }
1802
1803 /* The .toc pseudo-op. Switch to the .toc subsegment. */
1804
1805 static void
1806 ppc_toc (ignore)
1807 int ignore;
1808 {
1809 if (ppc_toc_csect != (symbolS *) NULL)
1810 subseg_set (data_section, ppc_toc_csect->sy_tc.subseg);
1811 else
1812 {
1813 subsegT subseg;
1814 symbolS *sym;
1815 symbolS *list;
1816
1817 subseg = ppc_data_subsegment;
1818 ++ppc_data_subsegment;
1819
1820 subseg_new (segment_name (data_section), subseg);
1821 ppc_toc_frag = frag_now;
1822
1823 sym = symbol_find_or_make ("TOC[TC0]");
1824 sym->sy_frag = frag_now;
1825 S_SET_SEGMENT (sym, data_section);
1826 S_SET_VALUE (sym, (valueT) frag_now_fix ());
1827 sym->sy_tc.subseg = subseg;
1828 sym->sy_tc.output = 1;
1829 sym->sy_tc.within = sym;
1830
1831 ppc_toc_csect = sym;
1832
1833 for (list = ppc_data_csects;
1834 list->sy_tc.next != (symbolS *) NULL;
1835 list = list->sy_tc.next)
1836 ;
1837 list->sy_tc.next = sym;
1838
1839 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
1840 symbol_append (sym, list->sy_tc.within, &symbol_rootP, &symbol_lastP);
1841 }
1842
1843 ppc_current_csect = ppc_toc_csect;
1844
1845 demand_empty_rest_of_line ();
1846 }
1847
1848 #endif /* OBJ_COFF */
1849 \f
1850 /* The .tc pseudo-op. This is used when generating either XCOFF or
1851 ELF. This takes two or more arguments.
1852
1853 When generating XCOFF output, the first argument is the name to
1854 give to this location in the toc; this will be a symbol with class
1855 TC. The rest of the arguments are 4 byte values to actually put at
1856 this location in the TOC; often there is just one more argument, a
1857 relocateable symbol reference.
1858
1859 When not generating XCOFF output, the arguments are the same, but
1860 the first argument is simply ignored. */
1861
1862 static void
1863 ppc_tc (ignore)
1864 int ignore;
1865 {
1866 #ifdef OBJ_COFF
1867
1868 /* Define the TOC symbol name. */
1869 {
1870 char *name;
1871 char endc;
1872 symbolS *sym;
1873
1874 if (ppc_toc_csect == (symbolS *) NULL
1875 || ppc_toc_csect != ppc_current_csect)
1876 {
1877 as_bad (".tc not in .toc section");
1878 ignore_rest_of_line ();
1879 return;
1880 }
1881
1882 name = input_line_pointer;
1883 endc = get_symbol_end ();
1884
1885 sym = symbol_find_or_make (name);
1886
1887 *input_line_pointer = endc;
1888
1889 if (S_IS_DEFINED (sym))
1890 {
1891 symbolS *label;
1892
1893 label = ppc_current_csect->sy_tc.within;
1894 if (label->sy_tc.class != XMC_TC0)
1895 {
1896 as_warn (".tc with no label");
1897 ignore_rest_of_line ();
1898 return;
1899 }
1900
1901 S_SET_SEGMENT (label, S_GET_SEGMENT (sym));
1902 label->sy_frag = sym->sy_frag;
1903 S_SET_VALUE (label, S_GET_VALUE (sym));
1904
1905 while (! is_end_of_line[(unsigned char) *input_line_pointer])
1906 ++input_line_pointer;
1907
1908 return;
1909 }
1910
1911 S_SET_SEGMENT (sym, now_seg);
1912 sym->sy_frag = frag_now;
1913 S_SET_VALUE (sym, (valueT) frag_now_fix ());
1914 sym->sy_tc.class = XMC_TC;
1915 sym->sy_tc.output = 1;
1916
1917 ppc_frob_label (sym);
1918 }
1919
1920 #else /* ! defined (OBJ_COFF) */
1921
1922 /* Skip the TOC symbol name. */
1923 while (is_part_of_name (*input_line_pointer)
1924 || *input_line_pointer == '['
1925 || *input_line_pointer == ']'
1926 || *input_line_pointer == '{'
1927 || *input_line_pointer == '}')
1928 ++input_line_pointer;
1929
1930 /* Align to a four byte boundary. */
1931 frag_align (2, 0);
1932 record_alignment (now_seg, 2);
1933
1934 #endif /* ! defined (OBJ_COFF) */
1935
1936 if (*input_line_pointer != ',')
1937 demand_empty_rest_of_line ();
1938 else
1939 {
1940 ++input_line_pointer;
1941 cons (4);
1942 }
1943 }
1944 \f
1945 #ifdef OBJ_COFF
1946
1947 /* XCOFF specific symbol and file handling. */
1948
1949 /* Canonicalize the symbol name. We use the to force the suffix, if
1950 any, to use square brackets, and to be in upper case. */
1951
1952 char *
1953 ppc_canonicalize_symbol_name (name)
1954 char *name;
1955 {
1956 char *s;
1957
1958 for (s = name; *s != '\0' && *s != '{' && *s != '['; s++)
1959 ;
1960 if (*s != '\0')
1961 {
1962 char brac;
1963
1964 if (*s == '[')
1965 brac = ']';
1966 else
1967 {
1968 *s = '[';
1969 brac = '}';
1970 }
1971
1972 for (s++; *s != '\0' && *s != brac; s++)
1973 if (islower (*s))
1974 *s = toupper (*s);
1975
1976 if (*s == '\0' || s[1] != '\0')
1977 as_bad ("bad symbol suffix");
1978
1979 *s = ']';
1980 }
1981
1982 return name;
1983 }
1984
1985 /* Set the class of a symbol based on the suffix, if any. This is
1986 called whenever a new symbol is created. */
1987
1988 void
1989 ppc_symbol_new_hook (sym)
1990 symbolS *sym;
1991 {
1992 const char *s;
1993
1994 sym->sy_tc.next = NULL;
1995 sym->sy_tc.output = 0;
1996 sym->sy_tc.class = -1;
1997 sym->sy_tc.real_name = NULL;
1998 sym->sy_tc.subseg = 0;
1999 sym->sy_tc.align = 0;
2000 sym->sy_tc.size = NULL;
2001 sym->sy_tc.within = NULL;
2002
2003 s = strchr (S_GET_NAME (sym), '[');
2004 if (s == (const char *) NULL)
2005 {
2006 /* There is no suffix. */
2007 return;
2008 }
2009
2010 ++s;
2011
2012 switch (s[0])
2013 {
2014 case 'B':
2015 if (strcmp (s, "BS]") == 0)
2016 sym->sy_tc.class = XMC_BS;
2017 break;
2018 case 'D':
2019 if (strcmp (s, "DB]") == 0)
2020 sym->sy_tc.class = XMC_DB;
2021 else if (strcmp (s, "DS]") == 0)
2022 sym->sy_tc.class = XMC_DS;
2023 break;
2024 case 'G':
2025 if (strcmp (s, "GL]") == 0)
2026 sym->sy_tc.class = XMC_GL;
2027 break;
2028 case 'P':
2029 if (strcmp (s, "PR]") == 0)
2030 sym->sy_tc.class = XMC_PR;
2031 break;
2032 case 'R':
2033 if (strcmp (s, "RO]") == 0)
2034 sym->sy_tc.class = XMC_RO;
2035 else if (strcmp (s, "RW]") == 0)
2036 sym->sy_tc.class = XMC_RW;
2037 break;
2038 case 'S':
2039 if (strcmp (s, "SV]") == 0)
2040 sym->sy_tc.class = XMC_SV;
2041 break;
2042 case 'T':
2043 if (strcmp (s, "TC]") == 0)
2044 sym->sy_tc.class = XMC_TC;
2045 else if (strcmp (s, "TI]") == 0)
2046 sym->sy_tc.class = XMC_TI;
2047 else if (strcmp (s, "TB]") == 0)
2048 sym->sy_tc.class = XMC_TB;
2049 else if (strcmp (s, "TC0]") == 0 || strcmp (s, "T0]") == 0)
2050 sym->sy_tc.class = XMC_TC0;
2051 break;
2052 case 'U':
2053 if (strcmp (s, "UA]") == 0)
2054 sym->sy_tc.class = XMC_UA;
2055 else if (strcmp (s, "UC]") == 0)
2056 sym->sy_tc.class = XMC_UC;
2057 break;
2058 case 'X':
2059 if (strcmp (s, "XO]") == 0)
2060 sym->sy_tc.class = XMC_XO;
2061 break;
2062 }
2063
2064 if (sym->sy_tc.class == -1)
2065 as_bad ("Unrecognized symbol suffix");
2066 }
2067
2068 /* Set the class of a label based on where it is defined. This
2069 handles symbols without suffixes. Also, move the symbol so that it
2070 follows the csect symbol. */
2071
2072 void
2073 ppc_frob_label (sym)
2074 symbolS *sym;
2075 {
2076 if (ppc_current_csect != (symbolS *) NULL)
2077 {
2078 if (sym->sy_tc.class == -1)
2079 sym->sy_tc.class = ppc_current_csect->sy_tc.class;
2080
2081 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2082 symbol_append (sym, ppc_current_csect->sy_tc.within, &symbol_rootP,
2083 &symbol_lastP);
2084 ppc_current_csect->sy_tc.within = sym;
2085 }
2086 }
2087
2088 /* Change the name of a symbol just before writing it out. Set the
2089 real name if the .rename pseudo-op was used. Otherwise, remove any
2090 class suffix. Return 1 if the symbol should not be included in the
2091 symbol table. */
2092
2093 int
2094 ppc_frob_symbol (sym)
2095 symbolS *sym;
2096 {
2097 static symbolS *ppc_last_function;
2098 static symbolS *set_end;
2099
2100 /* Discard symbols that should not be included in the output symbol
2101 table. */
2102 if (! sym->sy_used_in_reloc
2103 && ((sym->bsym->flags & BSF_SECTION_SYM) != 0
2104 || (! S_IS_EXTERNAL (sym)
2105 && ! sym->sy_tc.output
2106 && S_GET_STORAGE_CLASS (sym) != C_FILE)))
2107 return 1;
2108
2109 if (sym->sy_tc.real_name != (char *) NULL)
2110 S_SET_NAME (sym, sym->sy_tc.real_name);
2111 else
2112 {
2113 const char *name;
2114 const char *s;
2115
2116 name = S_GET_NAME (sym);
2117 s = strchr (name, '[');
2118 if (s != (char *) NULL)
2119 {
2120 unsigned int len;
2121 char *snew;
2122
2123 len = s - name;
2124 snew = xmalloc (len + 1);
2125 memcpy (snew, name, len);
2126 snew[len] = '\0';
2127
2128 S_SET_NAME (sym, snew);
2129 }
2130 }
2131
2132 if (set_end != (symbolS *) NULL)
2133 {
2134 SA_SET_SYM_ENDNDX (set_end, sym);
2135 set_end = NULL;
2136 }
2137
2138 if (SF_GET_FUNCTION (sym))
2139 {
2140 if (ppc_last_function != (symbolS *) NULL)
2141 as_warn ("two .function pseudo-ops with no intervening .ef");
2142 ppc_last_function = sym;
2143 if (sym->sy_tc.size != (symbolS *) NULL)
2144 {
2145 resolve_symbol_value (sym->sy_tc.size);
2146 SA_SET_SYM_FSIZE (sym, (long) S_GET_VALUE (sym->sy_tc.size));
2147 }
2148 }
2149 else if (S_GET_STORAGE_CLASS (sym) == C_FCN
2150 && strcmp (S_GET_NAME (sym), ".ef") == 0)
2151 {
2152 if (ppc_last_function == (symbolS *) NULL)
2153 as_warn (".ef with no preceding .function");
2154 else
2155 {
2156 set_end = ppc_last_function;
2157 ppc_last_function = NULL;
2158
2159 /* We don't have a C_EFCN symbol, but we need to force the
2160 COFF backend to believe that it has seen one. */
2161 coff_last_function = NULL;
2162 }
2163 }
2164
2165 if (! S_IS_EXTERNAL (sym)
2166 && (sym->bsym->flags & BSF_SECTION_SYM) == 0
2167 && S_GET_STORAGE_CLASS (sym) != C_FILE
2168 && S_GET_STORAGE_CLASS (sym) != C_FCN
2169 && S_GET_STORAGE_CLASS (sym) != C_BSTAT
2170 && S_GET_STORAGE_CLASS (sym) != C_ESTAT
2171 && S_GET_SEGMENT (sym) != ppc_coff_debug_section)
2172 S_SET_STORAGE_CLASS (sym, C_HIDEXT);
2173
2174 if (S_GET_STORAGE_CLASS (sym) == C_EXT
2175 || S_GET_STORAGE_CLASS (sym) == C_HIDEXT)
2176 {
2177 int i;
2178 union internal_auxent *a;
2179
2180 /* Create a csect aux. */
2181 i = S_GET_NUMBER_AUXILIARY (sym);
2182 S_SET_NUMBER_AUXILIARY (sym, i + 1);
2183 a = &coffsymbol (sym->bsym)->native[i + 1].u.auxent;
2184 if (sym->sy_tc.class == XMC_TC0)
2185 {
2186 /* This is the TOC table. */
2187 know (strcmp (S_GET_NAME (sym), "TOC") == 0);
2188 a->x_csect.x_scnlen.l = 0;
2189 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
2190 }
2191 else if (sym->sy_tc.subseg != 0)
2192 {
2193 /* This is a csect symbol. x_scnlen is the size of the
2194 csect. */
2195 if (sym->sy_tc.next == (symbolS *) NULL)
2196 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
2197 S_GET_SEGMENT (sym))
2198 - S_GET_VALUE (sym));
2199 else
2200 {
2201 resolve_symbol_value (sym->sy_tc.next);
2202 a->x_csect.x_scnlen.l = (S_GET_VALUE (sym->sy_tc.next)
2203 - S_GET_VALUE (sym));
2204 }
2205 a->x_csect.x_smtyp = (sym->sy_tc.align << 3) | XTY_SD;
2206 }
2207 else if (S_GET_SEGMENT (sym) == bss_section)
2208 {
2209 /* This is a common symbol. */
2210 a->x_csect.x_scnlen.l = sym->sy_frag->fr_offset;
2211 a->x_csect.x_smtyp = (sym->sy_tc.align << 3) | XTY_CM;
2212 if (S_IS_EXTERNAL (sym))
2213 sym->sy_tc.class = XMC_RW;
2214 else
2215 sym->sy_tc.class = XMC_BS;
2216 }
2217 else if (! S_IS_DEFINED (sym))
2218 {
2219 /* This is an external symbol. */
2220 a->x_csect.x_scnlen.l = 0;
2221 a->x_csect.x_smtyp = XTY_ER;
2222 }
2223 else if (sym->sy_tc.class == XMC_TC)
2224 {
2225 symbolS *next;
2226
2227 /* This is a TOC definition. x_scnlen is the size of the
2228 TOC entry. */
2229 next = symbol_next (sym);
2230 while (next->sy_tc.class == XMC_TC0)
2231 next = symbol_next (next);
2232 if (next == (symbolS *) NULL
2233 || next->sy_tc.class != XMC_TC)
2234 {
2235 if (ppc_after_toc_frag == (fragS *) NULL)
2236 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
2237 data_section)
2238 - S_GET_VALUE (sym));
2239 else
2240 a->x_csect.x_scnlen.l = (ppc_after_toc_frag->fr_address
2241 - S_GET_VALUE (sym));
2242 }
2243 else
2244 {
2245 resolve_symbol_value (next);
2246 a->x_csect.x_scnlen.l = (S_GET_VALUE (next)
2247 - S_GET_VALUE (sym));
2248 }
2249 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
2250 }
2251 else
2252 {
2253 symbolS *csect;
2254
2255 /* This is a normal symbol definition. x_scnlen is the
2256 symbol index of the containing csect. */
2257 if (S_GET_SEGMENT (sym) == text_section)
2258 csect = ppc_text_csects;
2259 else if (S_GET_SEGMENT (sym) == data_section)
2260 csect = ppc_data_csects;
2261 else
2262 abort ();
2263
2264 /* Skip the initial dummy symbol. */
2265 csect = csect->sy_tc.next;
2266
2267 if (csect == (symbolS *) NULL)
2268 a->x_csect.x_scnlen.l = 0;
2269 else
2270 {
2271 while (csect->sy_tc.next != (symbolS *) NULL)
2272 {
2273 resolve_symbol_value (csect->sy_tc.next);
2274 if (S_GET_VALUE (csect->sy_tc.next) > S_GET_VALUE (sym))
2275 break;
2276 csect = csect->sy_tc.next;
2277 }
2278
2279 a->x_csect.x_scnlen.p = coffsymbol (csect->bsym)->native;
2280 coffsymbol (sym->bsym)->native[i + 1].fix_scnlen = 1;
2281 }
2282 a->x_csect.x_smtyp = XTY_LD;
2283 }
2284
2285 a->x_csect.x_parmhash = 0;
2286 a->x_csect.x_snhash = 0;
2287 if (sym->sy_tc.class == -1)
2288 a->x_csect.x_smclas = XMC_PR;
2289 else
2290 a->x_csect.x_smclas = sym->sy_tc.class;
2291 a->x_csect.x_stab = 0;
2292 a->x_csect.x_snstab = 0;
2293 }
2294 else if (S_GET_STORAGE_CLASS (sym) == C_BSTAT)
2295 {
2296 /* We want the value to be the symbol index of the referenced
2297 csect symbol. BFD will do that for us if we set the right
2298 flags. */
2299 S_SET_VALUE (sym,
2300 (valueT) coffsymbol (sym->sy_tc.within->bsym)->native);
2301 coffsymbol (sym->bsym)->native->fix_value = 1;
2302 }
2303 else if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
2304 {
2305 symbolS *block;
2306 symbolS *csect;
2307
2308 /* The value is the offset from the enclosing csect. */
2309 block = sym->sy_tc.within;
2310 csect = block->sy_tc.within;
2311 resolve_symbol_value (csect);
2312 S_SET_VALUE (sym, S_GET_VALUE (sym) - S_GET_VALUE (csect));
2313 }
2314
2315 return 0;
2316 }
2317
2318 /* Set the VMA for a section. This is called on all the sections in
2319 turn. */
2320
2321 void
2322 ppc_frob_section (sec)
2323 asection *sec;
2324 {
2325 static bfd_size_type vma = 0;
2326
2327 bfd_set_section_vma (stdoutput, sec, vma);
2328 vma += bfd_section_size (stdoutput, sec);
2329 }
2330
2331 /* Adjust the file by adding a .debug section if needed. */
2332
2333 void
2334 ppc_frob_file ()
2335 {
2336 if (ppc_debug_name_section_size > 0)
2337 {
2338 asection *sec;
2339
2340 sec = bfd_make_section (stdoutput, ".debug");
2341 if (sec == (asection *) NULL
2342 || ! bfd_set_section_size (stdoutput, sec,
2343 ppc_debug_name_section_size)
2344 || ! bfd_set_section_flags (stdoutput, sec,
2345 SEC_HAS_CONTENTS | SEC_LOAD))
2346 as_fatal ("can't make .debug section");
2347 }
2348 }
2349
2350 #endif /* OBJ_COFF */
2351 \f
2352 /* Turn a string in input_line_pointer into a floating point constant
2353 of type type, and store the appropriate bytes in *litp. The number
2354 of LITTLENUMS emitted is stored in *sizep . An error message is
2355 returned, or NULL on OK. */
2356
2357 char *
2358 md_atof (type, litp, sizep)
2359 int type;
2360 char *litp;
2361 int *sizep;
2362 {
2363 int prec;
2364 LITTLENUM_TYPE words[4];
2365 char *t;
2366 int i;
2367
2368 switch (type)
2369 {
2370 case 'f':
2371 prec = 2;
2372 break;
2373
2374 case 'd':
2375 prec = 4;
2376 break;
2377
2378 default:
2379 *sizep = 0;
2380 return "bad call to md_atof";
2381 }
2382
2383 t = atof_ieee (input_line_pointer, type, words);
2384 if (t)
2385 input_line_pointer = t;
2386
2387 *sizep = prec * 2;
2388
2389 if (target_big_endian)
2390 {
2391 for (i = 0; i < prec; i++)
2392 {
2393 md_number_to_chars (litp, (valueT) words[i], 2);
2394 litp += 2;
2395 }
2396 }
2397 else
2398 {
2399 for (i = prec - 1; i >= 0; i--)
2400 {
2401 md_number_to_chars (litp, (valueT) words[i], 2);
2402 litp += 2;
2403 }
2404 }
2405
2406 return NULL;
2407 }
2408
2409 /* Write a value out to the object file, using the appropriate
2410 endianness. */
2411
2412 void
2413 md_number_to_chars (buf, val, n)
2414 char *buf;
2415 valueT val;
2416 int n;
2417 {
2418 if (target_big_endian)
2419 number_to_chars_bigendian (buf, val, n);
2420 else
2421 number_to_chars_littleendian (buf, val, n);
2422 }
2423
2424 /* Align a section (I don't know why this is machine dependent). */
2425
2426 valueT
2427 md_section_align (seg, addr)
2428 asection *seg;
2429 valueT addr;
2430 {
2431 int align = bfd_get_section_alignment (stdoutput, seg);
2432
2433 return ((addr + (1 << align) - 1) & (-1 << align));
2434 }
2435
2436 /* We don't have any form of relaxing. */
2437
2438 int
2439 md_estimate_size_before_relax (fragp, seg)
2440 fragS *fragp;
2441 asection *seg;
2442 {
2443 abort ();
2444 }
2445
2446 /* Convert a machine dependent frag. We never generate these. */
2447
2448 void
2449 md_convert_frag (abfd, sec, fragp)
2450 bfd *abfd;
2451 asection *sec;
2452 fragS *fragp;
2453 {
2454 abort ();
2455 }
2456
2457 /* We have no need to default values of symbols. */
2458
2459 /*ARGSUSED*/
2460 symbolS *
2461 md_undefined_symbol (name)
2462 char *name;
2463 {
2464 return 0;
2465 }
2466 \f
2467 /* Functions concerning relocs. */
2468
2469 /* The location from which a PC relative jump should be calculated,
2470 given a PC relative reloc. */
2471
2472 long
2473 md_pcrel_from (fixp)
2474 fixS *fixp;
2475 {
2476 #ifdef OBJ_ELF
2477 if (fixp->fx_addsy != (symbolS *) NULL
2478 && ! S_IS_DEFINED (fixp->fx_addsy))
2479 return 0;
2480 #endif
2481
2482 return fixp->fx_frag->fr_address + fixp->fx_where;
2483 }
2484
2485 #ifdef OBJ_COFF
2486
2487 /* This is called to see whether a fixup should be adjusted to use a
2488 section symbol. We take the opportunity to change a fixup against
2489 a symbol in the TOC subsegment into a reloc against the
2490 corresponding .tc symbol. */
2491
2492 int
2493 ppc_fix_adjustable (fix)
2494 fixS *fix;
2495 {
2496 valueT val;
2497
2498 resolve_symbol_value (fix->fx_addsy);
2499 val = S_GET_VALUE (fix->fx_addsy);
2500 if (ppc_toc_csect != (symbolS *) NULL
2501 && fix->fx_addsy != (symbolS *) NULL
2502 && fix->fx_addsy != ppc_toc_csect
2503 && S_GET_SEGMENT (fix->fx_addsy) == data_section
2504 && val >= ppc_toc_frag->fr_address
2505 && (ppc_after_toc_frag == (fragS *) NULL
2506 || val < ppc_after_toc_frag->fr_address))
2507 {
2508 symbolS *sy;
2509
2510 for (sy = symbol_next (ppc_toc_csect);
2511 sy != (symbolS *) NULL;
2512 sy = symbol_next (sy))
2513 {
2514 if (sy->sy_tc.class == XMC_TC0)
2515 continue;
2516 if (sy->sy_tc.class != XMC_TC)
2517 break;
2518 resolve_symbol_value (sy);
2519 if (val == S_GET_VALUE (sy))
2520 {
2521 fix->fx_addsy = sy;
2522 fix->fx_addnumber = val - ppc_toc_frag->fr_address;
2523 return 0;
2524 }
2525 }
2526
2527 as_bad_where (fix->fx_file, fix->fx_line,
2528 "symbol in .toc does not match any .tc");
2529 }
2530
2531 /* Possibly adjust the reloc to be against the csect. */
2532 if (fix->fx_addsy != (symbolS *) NULL
2533 && fix->fx_addsy->sy_tc.subseg == 0
2534 && fix->fx_addsy->sy_tc.class != XMC_TC0
2535 && fix->fx_addsy->sy_tc.class != XMC_TC
2536 && S_GET_SEGMENT (fix->fx_addsy) != bss_section)
2537 {
2538 symbolS *csect;
2539
2540 if (S_GET_SEGMENT (fix->fx_addsy) == text_section)
2541 csect = ppc_text_csects;
2542 else if (S_GET_SEGMENT (fix->fx_addsy) == data_section)
2543 csect = ppc_data_csects;
2544 else
2545 abort ();
2546
2547 /* Skip the initial dummy symbol. */
2548 csect = csect->sy_tc.next;
2549
2550 if (csect != (symbolS *) NULL)
2551 {
2552 while (csect->sy_tc.next != (symbolS *) NULL
2553 && (csect->sy_tc.next->sy_frag->fr_address
2554 <= fix->fx_addsy->sy_frag->fr_address))
2555 csect = csect->sy_tc.next;
2556
2557 fix->fx_offset += (S_GET_VALUE (fix->fx_addsy)
2558 - csect->sy_frag->fr_address);
2559 fix->fx_addsy = csect;
2560 }
2561 }
2562
2563 /* Adjust a reloc against a .lcomm symbol to be against the base
2564 .lcomm. */
2565 if (fix->fx_addsy != (symbolS *) NULL
2566 && S_GET_SEGMENT (fix->fx_addsy) == bss_section
2567 && ! S_IS_EXTERNAL (fix->fx_addsy))
2568 {
2569 resolve_symbol_value (fix->fx_addsy->sy_frag->fr_symbol);
2570 fix->fx_offset += (S_GET_VALUE (fix->fx_addsy)
2571 - S_GET_VALUE (fix->fx_addsy->sy_frag->fr_symbol));
2572 fix->fx_addsy = fix->fx_addsy->sy_frag->fr_symbol;
2573 }
2574
2575 return 0;
2576 }
2577
2578 #endif
2579
2580 /* See whether a symbol is in the TOC section. */
2581
2582 static int
2583 ppc_is_toc_sym (sym)
2584 symbolS *sym;
2585 {
2586 #ifdef OBJ_COFF
2587 return sym->sy_tc.class == XMC_TC;
2588 #else
2589 return strcmp (segment_name (S_GET_SEGMENT (sym)), ".got") == 0;
2590 #endif
2591 }
2592
2593 /* Apply a fixup to the object code. This is called for all the
2594 fixups we generated by the call to fix_new_exp, above. In the call
2595 above we used a reloc code which was the largest legal reloc code
2596 plus the operand index. Here we undo that to recover the operand
2597 index. At this point all symbol values should be fully resolved,
2598 and we attempt to completely resolve the reloc. If we can not do
2599 that, we determine the correct reloc code and put it back in the
2600 fixup. */
2601
2602 int
2603 md_apply_fix3 (fixp, valuep, seg)
2604 fixS *fixp;
2605 valueT *valuep;
2606 segT seg;
2607 {
2608 valueT value;
2609
2610 /* FIXME FIXME FIXME: The value we are passed in *valuep includes
2611 the symbol values. Since we are using BFD_ASSEMBLER, if we are
2612 doing this relocation the code in write.c is going to call
2613 bfd_perform_relocation, which is also going to use the symbol
2614 value. That means that if the reloc is fully resolved we want to
2615 use *valuep since bfd_perform_relocation is not being used.
2616 However, if the reloc is not fully resolved we do not want to use
2617 *valuep, and must use fx_offset instead. However, if the reloc
2618 is PC relative, we do want to use *valuep since it includes the
2619 result of md_pcrel_from. This is confusing. */
2620
2621 if (fixp->fx_addsy == (symbolS *) NULL)
2622 {
2623 value = *valuep;
2624 fixp->fx_done = 1;
2625 }
2626 else if (fixp->fx_pcrel)
2627 value = *valuep;
2628 else
2629 {
2630 value = fixp->fx_offset;
2631 if (fixp->fx_subsy != (symbolS *) NULL)
2632 {
2633 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
2634 value -= S_GET_VALUE (fixp->fx_subsy);
2635 else
2636 {
2637 /* We can't actually support subtracting a symbol. */
2638 as_bad_where (fixp->fx_file, fixp->fx_line,
2639 "expression too complex");
2640 }
2641 }
2642 }
2643
2644 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
2645 {
2646 int opindex;
2647 const struct powerpc_operand *operand;
2648 char *where;
2649 unsigned long insn;
2650
2651 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
2652
2653 operand = &powerpc_operands[opindex];
2654
2655 #ifdef OBJ_COFF
2656 /* It appears that an instruction like
2657 l 9,LC..1(30)
2658 when LC..1 is not a TOC symbol does not generate a reloc. It
2659 uses the offset of LC..1 within its csect. However, .long
2660 LC..1 will generate a reloc. I can't find any documentation
2661 on how these cases are to be distinguished, so this is a wild
2662 guess. These cases are generated by gcc -mminimal-toc. */
2663 if ((operand->flags & PPC_OPERAND_PARENS) != 0
2664 && operand->bits == 16
2665 && operand->shift == 0
2666 && operand->insert == NULL
2667 && fixp->fx_addsy != NULL
2668 && fixp->fx_addsy->sy_tc.subseg != 0
2669 && fixp->fx_addsy->sy_tc.class != XMC_TC
2670 && fixp->fx_addsy->sy_tc.class != XMC_TC0
2671 && S_GET_SEGMENT (fixp->fx_addsy) != bss_section)
2672 {
2673 value = fixp->fx_offset;
2674 fixp->fx_done = 1;
2675 }
2676 #endif
2677
2678 /* Fetch the instruction, insert the fully resolved operand
2679 value, and stuff the instruction back again. */
2680 where = fixp->fx_frag->fr_literal + fixp->fx_where;
2681 if (target_big_endian)
2682 insn = bfd_getb32 ((unsigned char *) where);
2683 else
2684 insn = bfd_getl32 ((unsigned char *) where);
2685 insn = ppc_insert_operand (insn, operand, (offsetT) value,
2686 fixp->fx_file, fixp->fx_line);
2687 if (target_big_endian)
2688 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
2689 else
2690 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
2691
2692 if (fixp->fx_done)
2693 {
2694 /* Nothing else to do here. */
2695 return 1;
2696 }
2697
2698 /* Determine a BFD reloc value based on the operand information.
2699 We are only prepared to turn a few of the operands into
2700 relocs.
2701 FIXME: We need to handle the DS field at the very least.
2702 FIXME: Handling 16 bit branches would also be reasonable.
2703 FIXME: Selecting the reloc type is a bit haphazard; perhaps
2704 there should be a new field in the operand table. */
2705 if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
2706 && operand->bits == 26
2707 && operand->shift == 0)
2708 fixp->fx_r_type = BFD_RELOC_PPC_B26;
2709 else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0
2710 && operand->bits == 26
2711 && operand->shift == 0)
2712 fixp->fx_r_type = BFD_RELOC_PPC_BA26;
2713 else if ((operand->flags & PPC_OPERAND_PARENS) != 0
2714 && operand->bits == 16
2715 && operand->shift == 0
2716 && operand->insert == NULL
2717 && fixp->fx_addsy != NULL
2718 && ppc_is_toc_sym (fixp->fx_addsy))
2719 {
2720 fixp->fx_size = 2;
2721 if (target_big_endian)
2722 fixp->fx_where += 2;
2723 fixp->fx_r_type = BFD_RELOC_PPC_TOC16;
2724 }
2725 else
2726 {
2727 as_bad_where (fixp->fx_file, fixp->fx_line,
2728 "unresolved expression that must be resolved");
2729 fixp->fx_done = 1;
2730 return 1;
2731 }
2732 }
2733 else
2734 {
2735 #ifdef OBJ_ELF
2736 ppc_elf_validate_fix (fixp, seg);
2737 #endif
2738 switch (fixp->fx_r_type)
2739 {
2740 case BFD_RELOC_32:
2741 case BFD_RELOC_CTOR:
2742 if (fixp->fx_pcrel)
2743 {
2744 fixp->fx_r_type = BFD_RELOC_32_PCREL;
2745 value += fixp->fx_frag->fr_address + fixp->fx_where;
2746 } /* fall through */
2747
2748 case BFD_RELOC_32_PCREL:
2749 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
2750 value, 4);
2751 break;
2752
2753 case BFD_RELOC_LO16:
2754 case BFD_RELOC_HI16:
2755 case BFD_RELOC_HI16_S:
2756 case BFD_RELOC_PPC_TOC16:
2757 case BFD_RELOC_16:
2758 case BFD_RELOC_GPREL16:
2759 if (fixp->fx_pcrel)
2760 abort ();
2761
2762 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
2763 value, 2);
2764 break;
2765
2766 case BFD_RELOC_8:
2767 if (fixp->fx_pcrel)
2768 abort ();
2769
2770 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
2771 value, 1);
2772 break;
2773
2774 default:
2775 abort ();
2776 }
2777 }
2778
2779 #ifdef OBJ_ELF
2780 fixp->fx_addnumber = value;
2781 #else
2782 if (fixp->fx_r_type != BFD_RELOC_PPC_TOC16)
2783 fixp->fx_addnumber = 0;
2784 else
2785 {
2786 /* We want to use the offset within the data segment of the
2787 symbol, not the actual VMA of the symbol. */
2788 fixp->fx_addnumber =
2789 - bfd_get_section_vma (stdoutput, S_GET_SEGMENT (fixp->fx_addsy));
2790 }
2791 #endif
2792
2793 return 1;
2794 }
2795
2796 /* Generate a reloc for a fixup. */
2797
2798 arelent *
2799 tc_gen_reloc (seg, fixp)
2800 asection *seg;
2801 fixS *fixp;
2802 {
2803 arelent *reloc;
2804
2805 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
2806
2807 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
2808 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2809 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2810 if (reloc->howto == (reloc_howto_type *) NULL)
2811 {
2812 as_bad_where (fixp->fx_file, fixp->fx_line,
2813 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
2814 return NULL;
2815 }
2816 reloc->addend = fixp->fx_addnumber;
2817
2818 return reloc;
2819 }
This page took 0.111126 seconds and 5 git commands to generate.