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