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