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