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