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