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