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