* config/tc-ppc.c (ppc_bb): Call SF_SET_PROCESS.
[platform/upstream/binutils.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 #ifdef TE_PE
33 #include "coff/pe.h"
34 #endif
35
36 /* This is the assembler for the PowerPC or POWER (RS/6000) chips.  */
37
38 /* Tell the main code what the endianness is.  */
39 extern int target_big_endian;
40
41 /* Whether or not, we've set target_big_endian.  */
42 static int set_target_endian = 0;
43
44 static void ppc_set_cpu PARAMS ((void));
45 static unsigned long ppc_insert_operand
46   PARAMS ((unsigned long insn, const struct powerpc_operand *operand,
47            offsetT val, char *file, unsigned int line));
48 static void ppc_macro PARAMS ((char *str, const struct powerpc_macro *macro));
49 static void ppc_byte PARAMS ((int));
50 static int ppc_is_toc_sym PARAMS ((symbolS *sym));
51 static void ppc_tc PARAMS ((int));
52
53 #ifdef OBJ_XCOFF
54 static void ppc_comm PARAMS ((int));
55 static void ppc_bb PARAMS ((int));
56 static void ppc_bc PARAMS ((int));
57 static void ppc_bf PARAMS ((int));
58 static void ppc_biei PARAMS ((int));
59 static void ppc_bs PARAMS ((int));
60 static void ppc_eb PARAMS ((int));
61 static void ppc_ec PARAMS ((int));
62 static void ppc_ef PARAMS ((int));
63 static void ppc_es PARAMS ((int));
64 static void ppc_csect PARAMS ((int));
65 static void ppc_change_csect PARAMS ((symbolS *));
66 static void ppc_function PARAMS ((int));
67 static void ppc_extern PARAMS ((int));
68 static void ppc_lglobl PARAMS ((int));
69 static void ppc_section PARAMS ((int));
70 static void ppc_stabx PARAMS ((int));
71 static void ppc_rename PARAMS ((int));
72 static void ppc_toc PARAMS ((int));
73 #endif
74
75 #ifdef OBJ_ELF
76 static bfd_reloc_code_real_type ppc_elf_suffix PARAMS ((char **));
77 static void ppc_elf_cons PARAMS ((int));
78 static void ppc_elf_validate_fix PARAMS ((fixS *, segT));
79 #endif
80
81 #ifdef TE_PE
82 static void ppc_set_current_section PARAMS ((segT));
83 static void ppc_previous PARAMS ((int));
84 static void ppc_pdata PARAMS ((int));
85 static void ppc_ydata PARAMS ((int));
86 static void ppc_reldata PARAMS ((int));
87 static void ppc_rdata PARAMS ((int));
88 static void ppc_ualong PARAMS ((int));
89 static void ppc_znop PARAMS ((int));
90 static void ppc_pe_comm PARAMS ((int));
91 static void ppc_pe_section PARAMS ((int));
92 static void ppc_pe_function PARAMS ((int));
93 static void ppc_pe_tocd PARAMS ((int));
94 #endif
95 \f
96 /* Generic assembler global variables which must be defined by all
97    targets.  */
98
99 /* Characters which always start a comment.  */
100 const char comment_chars[] = "#";
101
102 /* Characters which start a comment at the beginning of a line.  */
103 const char line_comment_chars[] = "#";
104
105 /* Characters which may be used to separate multiple commands on a
106    single line.  */
107 const char line_separator_chars[] = ";";
108
109 /* Characters which are used to indicate an exponent in a floating
110    point number.  */
111 const char EXP_CHARS[] = "eE";
112
113 /* Characters which mean that a number is a floating point constant,
114    as in 0d1.0.  */
115 const char FLT_CHARS[] = "dD";
116 \f
117 /* The target specific pseudo-ops which we support.  */
118
119 const pseudo_typeS md_pseudo_table[] =
120 {
121   /* Pseudo-ops which must be overridden.  */
122   { "byte",     ppc_byte,       0 },
123
124 #ifdef OBJ_XCOFF
125   /* Pseudo-ops specific to the RS/6000 XCOFF format.  Some of these
126      legitimately belong in the obj-*.c file.  However, XCOFF is based
127      on COFF, and is only implemented for the RS/6000.  We just use
128      obj-coff.c, and add what we need here.  */
129   { "comm",     ppc_comm,       0 },
130   { "lcomm",    ppc_comm,       1 },
131   { "bb",       ppc_bb,         0 },
132   { "bc",       ppc_bc,         0 },
133   { "bf",       ppc_bf,         0 },
134   { "bi",       ppc_biei,       0 },
135   { "bs",       ppc_bs,         0 },
136   { "csect",    ppc_csect,      0 },
137   { "data",     ppc_section,    'd' },
138   { "eb",       ppc_eb,         0 },
139   { "ec",       ppc_ec,         0 },
140   { "ef",       ppc_ef,         0 },
141   { "ei",       ppc_biei,       1 },
142   { "es",       ppc_es,         0 },
143   { "extern",   ppc_extern,     0 },
144   { "function", ppc_function,   0 },
145   { "lglobl",   ppc_lglobl,     0 },
146   { "rename",   ppc_rename,     0 },
147   { "stabx",    ppc_stabx,      0 },
148   { "text",     ppc_section,    't' },
149   { "toc",      ppc_toc,        0 },
150 #endif
151
152 #ifdef OBJ_ELF
153   { "long",     ppc_elf_cons,   4 },
154   { "word",     ppc_elf_cons,   2 },
155   { "short",    ppc_elf_cons,   2 },
156 #endif
157
158 #ifdef TE_PE
159   /* Pseudo-ops specific to the Windows NT PowerPC PE (coff) format */
160   { "previous", ppc_previous,   0 },
161   { "pdata",    ppc_pdata,      0 },
162   { "ydata",    ppc_ydata,      0 },
163   { "reldata",  ppc_reldata,    0 },
164   { "rdata",    ppc_rdata,      0 },
165   { "ualong",   ppc_ualong,     0 },
166   { "znop",     ppc_znop,       0 },
167   { "comm",     ppc_pe_comm,    0 },
168   { "lcomm",    ppc_pe_comm,    1 },
169   { "section",  ppc_pe_section, 0 },
170   { "function", ppc_pe_function,0 },
171   { "tocd",     ppc_pe_tocd,    0 },
172 #endif
173
174   /* This pseudo-op is used even when not generating XCOFF output.  */
175   { "tc",       ppc_tc,         0 },
176
177   { NULL,       NULL,           0 }
178 };
179
180 \f
181 #ifdef TE_PE
182 /* The Windows NT PowerPC assembler uses predefined names.            */
183
184 /* In general, there are lots of them, in an attempt to be compatible */
185 /* with a number of other Windows NT assemblers.                      */
186
187 /* Structure to hold information about predefined registers.  */
188 struct pd_reg
189   {
190     char *name;
191     int value;
192   };
193
194 /* List of registers that are pre-defined:
195
196    Each general register has predefined names of the form:
197    1. r<reg_num> which has the value <reg_num>.
198    2. r.<reg_num> which has the value <reg_num>.
199
200
201    Each floating point register has predefined names of the form:
202    1. f<reg_num> which has the value <reg_num>.
203    2. f.<reg_num> which has the value <reg_num>.
204
205    Each condition register has predefined names of the form:
206    1. cr<reg_num> which has the value <reg_num>.
207    2. cr.<reg_num> which has the value <reg_num>.
208
209    There are individual registers as well:
210    sp or r.sp     has the value 1
211    rtoc or r.toc  has the value 2
212    fpscr          has the value 0
213    xer            has the value 1
214    lr             has the value 8
215    ctr            has the value 9
216    pmr            has the value 0
217    dar            has the value 19
218    dsisr          has the value 18
219    dec            has the value 22
220    sdr1           has the value 25
221    srr0           has the value 26
222    srr1           has the value 27
223
224    The table is sorted. Suitable for searching by a binary search. */
225
226 static const struct pd_reg pre_defined_registers[] =
227 {
228   { "cr.0", 0 },    /* Condition Registers */
229   { "cr.1", 1 },
230   { "cr.2", 2 },
231   { "cr.3", 3 },
232   { "cr.4", 4 },
233   { "cr.5", 5 },
234   { "cr.6", 6 },
235   { "cr.7", 7 },
236
237   { "cr0", 0 },
238   { "cr1", 1 },
239   { "cr2", 2 },
240   { "cr3", 3 },
241   { "cr4", 4 },
242   { "cr5", 5 },
243   { "cr6", 6 },
244   { "cr7", 7 },
245
246   { "ctr", 9 },
247
248   { "dar", 19 },    /* Data Access Register */
249   { "dec", 22 },    /* Decrementer */
250   { "dsisr", 18 },  /* Data Storage Interrupt Status Register */
251
252   { "f.0", 0 },     /* Floating point registers */
253   { "f.1", 1 }, 
254   { "f.10", 10 }, 
255   { "f.11", 11 }, 
256   { "f.12", 12 }, 
257   { "f.13", 13 }, 
258   { "f.14", 14 }, 
259   { "f.15", 15 }, 
260   { "f.16", 16 }, 
261   { "f.17", 17 }, 
262   { "f.18", 18 }, 
263   { "f.19", 19 }, 
264   { "f.2", 2 }, 
265   { "f.20", 20 }, 
266   { "f.21", 21 }, 
267   { "f.22", 22 }, 
268   { "f.23", 23 }, 
269   { "f.24", 24 }, 
270   { "f.25", 25 }, 
271   { "f.26", 26 }, 
272   { "f.27", 27 }, 
273   { "f.28", 28 }, 
274   { "f.29", 29 }, 
275   { "f.3", 3 }, 
276   { "f.30", 30 },
277   { "f.31", 31 },
278   { "f.4", 4 }, 
279   { "f.5", 5 }, 
280   { "f.6", 6 }, 
281   { "f.7", 7 }, 
282   { "f.8", 8 }, 
283   { "f.9", 9 }, 
284
285   { "f0", 0 }, 
286   { "f1", 1 }, 
287   { "f10", 10 }, 
288   { "f11", 11 }, 
289   { "f12", 12 }, 
290   { "f13", 13 }, 
291   { "f14", 14 }, 
292   { "f15", 15 }, 
293   { "f16", 16 }, 
294   { "f17", 17 }, 
295   { "f18", 18 }, 
296   { "f19", 19 }, 
297   { "f2", 2 }, 
298   { "f20", 20 }, 
299   { "f21", 21 }, 
300   { "f22", 22 }, 
301   { "f23", 23 }, 
302   { "f24", 24 }, 
303   { "f25", 25 }, 
304   { "f26", 26 }, 
305   { "f27", 27 }, 
306   { "f28", 28 }, 
307   { "f29", 29 }, 
308   { "f3", 3 }, 
309   { "f30", 30 },
310   { "f31", 31 },
311   { "f4", 4 }, 
312   { "f5", 5 }, 
313   { "f6", 6 }, 
314   { "f7", 7 }, 
315   { "f8", 8 }, 
316   { "f9", 9 }, 
317
318   { "fpscr", 0 },
319
320   { "lr", 8 },     /* Link Register */
321
322   { "pmr", 0 },
323
324   { "r.0", 0 },    /* General Purpose Registers */
325   { "r.1", 1 },
326   { "r.10", 10 },
327   { "r.11", 11 },
328   { "r.12", 12 },
329   { "r.13", 13 },
330   { "r.14", 14 },
331   { "r.15", 15 },
332   { "r.16", 16 },
333   { "r.17", 17 },
334   { "r.18", 18 },
335   { "r.19", 19 },
336   { "r.2", 2 },
337   { "r.20", 20 },
338   { "r.21", 21 },
339   { "r.22", 22 },
340   { "r.23", 23 },
341   { "r.24", 24 },
342   { "r.25", 25 },
343   { "r.26", 26 },
344   { "r.27", 27 },
345   { "r.28", 28 },
346   { "r.29", 29 },
347   { "r.3", 3 },
348   { "r.30", 30 },
349   { "r.31", 31 },
350   { "r.4", 4 },
351   { "r.5", 5 },
352   { "r.6", 6 },
353   { "r.7", 7 },
354   { "r.8", 8 },
355   { "r.9", 9 },
356
357   { "r.sp", 1 },   /* Stack Pointer */
358
359   { "r.toc", 2 },  /* Pointer to the table of contents */
360
361   { "r0", 0 },     /* More general purpose registers */
362   { "r1", 1 },
363   { "r10", 10 },
364   { "r11", 11 },
365   { "r12", 12 },
366   { "r13", 13 },
367   { "r14", 14 },
368   { "r15", 15 },
369   { "r16", 16 },
370   { "r17", 17 },
371   { "r18", 18 },
372   { "r19", 19 },
373   { "r2", 2 },
374   { "r20", 20 },
375   { "r21", 21 },
376   { "r22", 22 },
377   { "r23", 23 },
378   { "r24", 24 },
379   { "r25", 25 },
380   { "r26", 26 },
381   { "r27", 27 },
382   { "r28", 28 },
383   { "r29", 29 },
384   { "r3", 3 },
385   { "r30", 30 },
386   { "r31", 31 },
387   { "r4", 4 },
388   { "r5", 5 },
389   { "r6", 6 },
390   { "r7", 7 },
391   { "r8", 8 },
392   { "r9", 9 },
393
394   { "rtoc", 2 },  /* Table of contents */
395
396   { "sdr1", 25 }, /* Storage Description Register 1 */
397
398   { "sp", 1 },
399
400   { "srr0", 26 }, /* Machine Status Save/Restore Register 0 */
401   { "srr1", 27 }, /* Machine Status Save/Restore Register 1 */
402
403   { "xer", 1 },
404
405 };
406
407 #define REG_NAME_CNT    (sizeof(pre_defined_registers) / sizeof(struct pd_reg))
408
409 /* Given NAME, find the register number associated with that name, return
410    the integer value associated with the given name or -1 on failure.  */
411
412 static int reg_name_search PARAMS ( (char * name) );
413
414 static int
415 reg_name_search (name)
416      char *name;
417 {
418   int middle, low, high;
419   int cmp;
420
421   low = 0;
422   high = REG_NAME_CNT - 1;
423
424   do
425     {
426       middle = (low + high) / 2;
427       cmp = strcasecmp (name, pre_defined_registers[middle].name);
428       if (cmp < 0)
429         high = middle - 1;
430       else if (cmp > 0)
431         low = middle + 1;
432       else
433         return pre_defined_registers[middle].value;
434     }
435   while (low <= high);
436
437   return -1;
438 }
439
440 #endif
441
442 \f
443 /* Local variables.  */
444
445 /* The type of processor we are assembling for.  This is one or more
446    of the PPC_OPCODE flags defined in opcode/ppc.h.  */
447 static int ppc_cpu = 0;
448
449 /* The size of the processor we are assembling for.  This is either
450    PPC_OPCODE_32 or PPC_OPCODE_64.  */
451 static int ppc_size = PPC_OPCODE_32;
452
453 /* Opcode hash table.  */
454 static struct hash_control *ppc_hash;
455
456 /* Macro hash table.  */
457 static struct hash_control *ppc_macro_hash;
458
459 #ifdef OBJ_ELF
460 /* Whether to warn about non PC relative relocations that aren't
461    in the .got2 section. */
462 static boolean mrelocatable = false;
463
464 /* Flags to set in the elf header */
465 static flagword ppc_flags = 0;
466 #endif
467
468 #ifdef OBJ_XCOFF
469
470 /* The RS/6000 assembler uses the .csect pseudo-op to generate code
471    using a bunch of different sections.  These assembler sections,
472    however, are all encompassed within the .text or .data sections of
473    the final output file.  We handle this by using different
474    subsegments within these main segments.  */
475
476 /* Next subsegment to allocate within the .text segment.  */
477 static subsegT ppc_text_subsegment = 2;
478
479 /* Linked list of csects in the text section.  */
480 static symbolS *ppc_text_csects;
481
482 /* Next subsegment to allocate within the .data segment.  */
483 static subsegT ppc_data_subsegment = 2;
484
485 /* Linked list of csects in the data section.  */
486 static symbolS *ppc_data_csects;
487
488 /* The current csect.  */
489 static symbolS *ppc_current_csect;
490
491 /* The RS/6000 assembler uses a TOC which holds addresses of functions
492    and variables.  Symbols are put in the TOC with the .tc pseudo-op.
493    A special relocation is used when accessing TOC entries.  We handle
494    the TOC as a subsegment within the .data segment.  We set it up if
495    we see a .toc pseudo-op, and save the csect symbol here.  */
496 static symbolS *ppc_toc_csect;
497
498 /* The first frag in the TOC subsegment.  */
499 static fragS *ppc_toc_frag;
500
501 /* The first frag in the first subsegment after the TOC in the .data
502    segment.  NULL if there are no subsegments after the TOC.  */
503 static fragS *ppc_after_toc_frag;
504
505 /* The current static block.  */
506 static symbolS *ppc_current_block;
507
508 /* The COFF debugging section; set by md_begin.  This is not the
509    .debug section, but is instead the secret BFD section which will
510    cause BFD to set the section number of a symbol to N_DEBUG.  */
511 static asection *ppc_coff_debug_section;
512
513 /* The size of the .debug section.  */
514 static bfd_size_type ppc_debug_name_section_size;
515
516 #endif /* OBJ_XCOFF */
517
518 #ifdef TE_PE
519
520 /* Various sections that we need for PE coff support.  */
521 static segT ydata_section;
522 static segT pdata_section;
523 static segT reldata_section;
524 static segT rdata_section;
525 static segT tocdata_section;
526
527 /* The current section and the previous section. See ppc_previous. */
528 static segT ppc_previous_section;
529 static segT ppc_current_section;
530
531 #endif /* TE_PE */
532
533 #ifdef OBJ_ELF
534 symbolS *GOT_symbol;            /* Pre-defined "_GLOBAL_OFFSET_TABLE" */
535 #endif /* OBJ_ELF */
536
537 #ifndef WORKING_DOT_WORD
538 const int md_short_jump_size = 4;
539 const int md_long_jump_size = 4;
540 #endif
541 \f
542 #ifdef OBJ_ELF
543 CONST char *md_shortopts = "um:VQ:";
544 #else
545 CONST char *md_shortopts = "um:";
546 #endif
547 struct option md_longopts[] = {
548   {NULL, no_argument, NULL, 0}
549 };
550 size_t md_longopts_size = sizeof(md_longopts);
551
552 int
553 md_parse_option (c, arg)
554      int c;
555      char *arg;
556 {
557   switch (c)
558     {
559     case 'u':
560       /* -u means that any undefined symbols should be treated as
561          external, which is the default for gas anyhow.  */
562       break;
563
564     case 'm':
565       /* -mpwrx and -mpwr2 mean to assemble for the IBM POWER/2
566          (RIOS2).  */
567       if (strcmp (arg, "pwrx") == 0 || strcmp (arg, "pwr2") == 0)
568         ppc_cpu = PPC_OPCODE_POWER | PPC_OPCODE_POWER2;
569       /* -mpwr means to assemble for the IBM POWER (RIOS1).  */
570       else if (strcmp (arg, "pwr") == 0)
571         ppc_cpu = PPC_OPCODE_POWER;
572       /* -m601 means to assemble for the Motorola PowerPC 601, which includes
573          instructions that are holdovers from the Power. */
574       else if (strcmp (arg, "601") == 0)
575         ppc_cpu = PPC_OPCODE_PPC | PPC_OPCODE_601;
576       /* -mppc, -mppc32, -m603, and -m604 mean to assemble for the
577          Motorola PowerPC 603/604.  */
578       else if (strcmp (arg, "ppc") == 0
579                || strcmp (arg, "ppc32") == 0
580                || strcmp (arg, "403") == 0
581                || strcmp (arg, "603") == 0
582                || strcmp (arg, "604") == 0)
583         ppc_cpu = PPC_OPCODE_PPC;
584       /* -mppc64 and -m620 mean to assemble for the 64-bit PowerPC
585          620.  */
586       else if (strcmp (arg, "ppc64") == 0 || strcmp (arg, "620") == 0)
587         {
588           ppc_cpu = PPC_OPCODE_PPC;
589           ppc_size = PPC_OPCODE_64;
590         }
591       /* -mcom means assemble for the common intersection between Power
592          and PowerPC.  At present, we just allow the union, rather
593          than the intersection.  */
594       else if (strcmp (arg, "com") == 0)
595         ppc_cpu = PPC_OPCODE_COMMON;
596       /* -many means to assemble for any architecture (PWR/PWRX/PPC).  */
597       else if (strcmp (arg, "any") == 0)
598         ppc_cpu = PPC_OPCODE_ANY;
599
600 #ifdef OBJ_ELF
601       /* -mrelocatable/-mrelocatable-lib -- warn about initializations that require relocation */
602       else if (strcmp (arg, "relocatable") == 0)
603         {
604           mrelocatable = true;
605           ppc_flags |= EF_PPC_RELOCATABLE;
606         }
607
608       else if (strcmp (arg, "relocatable-lib") == 0)
609         {
610           mrelocatable = true;
611           ppc_flags |= EF_PPC_RELOCATABLE_LIB;
612         }
613
614       /* -memb, set embedded bit */
615       else if (strcmp (arg, "emb") == 0)
616         ppc_flags |= EF_PPC_EMB;
617
618       /* -mlittle/-mbig set the endianess */
619       else if (strcmp (arg, "little") == 0 || strcmp (arg, "little-endian") == 0)
620         {
621           target_big_endian = 0;
622           set_target_endian = 1;
623         }
624
625       else if (strcmp (arg, "big") == 0 || strcmp (arg, "big-endian") == 0)
626         {
627           target_big_endian = 1;
628           set_target_endian = 1;
629         }
630 #endif
631       else
632         {
633           as_bad ("invalid switch -m%s", arg);
634           return 0;
635         }
636       break;
637
638 #ifdef OBJ_ELF
639       /* -V: SVR4 argument to print version ID.  */
640     case 'V':
641       print_version_id ();
642       break;
643
644       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
645          should be emitted or not.  FIXME: Not implemented.  */
646     case 'Q':
647       break;
648 #endif
649
650     default:
651       return 0;
652     }
653
654   return 1;
655 }
656
657 void
658 md_show_usage (stream)
659      FILE *stream;
660 {
661   fprintf(stream, "\
662 PowerPC options:\n\
663 -u                      ignored\n\
664 -mpwrx, -mpwr2          generate code for IBM POWER/2 (RIOS2)\n\
665 -mpwr                   generate code for IBM POWER (RIOS1)\n\
666 -m601                   generate code for Motorola PowerPC 601\n\
667 -mppc, -mppc32, -m403, -m603, -m604\n\
668                         generate code for Motorola PowerPC 603/604\n\
669 -mppc64, -m620          generate code for Motorola PowerPC 620\n\
670 -mcom                   generate code Power/PowerPC common instructions\n
671 -many                   generate code for any architecture (PWR/PWRX/PPC)\n");
672 #ifdef OBJ_ELF
673   fprintf(stream, "\
674 -mrelocatable           support for GCC's -mrelocatble option\n\
675 -mrelocatable-lib       support for GCC's -mrelocatble-lib option\n\
676 -memb                   set PPC_EMB bit in ELF flags\n\
677 -mlittle, -mlittle-endian\n\
678                         generate code for a little endian machine\n\
679 -mbig, -mbig-endian     generate code for a big endian machine\n\
680 -V                      print assembler version number\n\
681 -Qy, -Qn                ignored\n");
682 #endif
683 }
684 \f
685 /* Set ppc_cpu if it is not already set.  */
686
687 static void
688 ppc_set_cpu ()
689 {
690   const char *default_cpu = TARGET_CPU;
691
692   if (ppc_cpu == 0)
693     {
694       if (strcmp (default_cpu, "rs6000") == 0)
695         ppc_cpu = PPC_OPCODE_POWER;
696       else if (strcmp (default_cpu, "powerpc") == 0
697                || strcmp (default_cpu, "powerpcle") == 0)
698         ppc_cpu = PPC_OPCODE_PPC;
699       else
700         as_fatal ("Unknown default cpu = %s", default_cpu);
701     }
702 }
703
704 /* Figure out the BFD architecture to use.  */
705
706 enum bfd_architecture
707 ppc_arch ()
708 {
709   const char *default_cpu = TARGET_CPU;
710   ppc_set_cpu ();
711
712   if ((ppc_cpu & PPC_OPCODE_PPC) != 0)
713     return bfd_arch_powerpc;
714   else if ((ppc_cpu & PPC_OPCODE_POWER) != 0)
715     return bfd_arch_rs6000;
716   else if ((ppc_cpu & (PPC_OPCODE_COMMON | PPC_OPCODE_ANY)) != 0)
717     {
718       if (strcmp (default_cpu, "rs6000") == 0)
719         return bfd_arch_rs6000;
720       else if (strcmp (default_cpu, "powerpc") == 0
721                || strcmp (default_cpu, "powerpcle") == 0)
722         return bfd_arch_powerpc;
723     }
724
725   as_fatal ("Neither Power nor PowerPC opcodes were selected.");
726   return bfd_arch_unknown;
727 }
728
729 /* This function is called when the assembler starts up.  It is called
730    after the options have been parsed and the output file has been
731    opened.  */
732
733 void
734 md_begin ()
735 {
736   register const struct powerpc_opcode *op;
737   const struct powerpc_opcode *op_end;
738   const struct powerpc_macro *macro;
739   const struct powerpc_macro *macro_end;
740   boolean dup_insn = false;
741
742   ppc_set_cpu ();
743
744 #ifdef OBJ_ELF
745   /* Set the ELF flags if desired. */
746   if (ppc_flags)
747     bfd_set_private_flags (stdoutput, ppc_flags);
748 #endif
749
750   /* Insert the opcodes into a hash table.  */
751   ppc_hash = hash_new ();
752
753   op_end = powerpc_opcodes + powerpc_num_opcodes;
754   for (op = powerpc_opcodes; op < op_end; op++)
755     {
756       know ((op->opcode & op->mask) == op->opcode);
757
758       if ((op->flags & ppc_cpu) != 0
759           && ((op->flags & (PPC_OPCODE_32 | PPC_OPCODE_64)) == 0
760               || (op->flags & (PPC_OPCODE_32 | PPC_OPCODE_64)) == ppc_size))
761         {
762           const char *retval;
763
764           retval = hash_insert (ppc_hash, op->name, (PTR) op);
765           if (retval != (const char *) NULL)
766             {
767               /* Ignore Power duplicates for -m601 */
768               if ((ppc_cpu & PPC_OPCODE_601) != 0
769                   && (op->flags & PPC_OPCODE_POWER) != 0)
770                 continue;
771
772               as_bad ("Internal assembler error for instruction %s", op->name);
773               dup_insn = true;
774             }
775         }
776     }
777
778   /* Insert the macros into a hash table.  */
779   ppc_macro_hash = hash_new ();
780
781   macro_end = powerpc_macros + powerpc_num_macros;
782   for (macro = powerpc_macros; macro < macro_end; macro++)
783     {
784       if ((macro->flags & ppc_cpu) != 0)
785         {
786           const char *retval;
787
788           retval = hash_insert (ppc_macro_hash, macro->name, (PTR) macro);
789           if (retval != (const char *) NULL)
790             {
791               as_bad ("Internal assembler error for macro %s", macro->name);
792               dup_insn = true;
793             }
794         }
795     }
796
797   if (dup_insn)
798     abort ();
799
800   /* Tell the main code what the endianness is if it is not overidden by the user.  */
801   if (!set_target_endian)
802     {
803       set_target_endian = 1;
804       target_big_endian = PPC_BIG_ENDIAN;
805     }
806
807 #ifdef OBJ_XCOFF
808   ppc_coff_debug_section = coff_section_from_bfd_index (stdoutput, N_DEBUG);
809
810   /* Create dummy symbols to serve as initial csects.  This forces the
811      text csects to precede the data csects.  These symbols will not
812      be output.  */
813   ppc_text_csects = symbol_make ("dummy\001");
814   ppc_text_csects->sy_tc.within = ppc_text_csects;
815   ppc_data_csects = symbol_make ("dummy\001");
816   ppc_data_csects->sy_tc.within = ppc_data_csects;
817 #endif
818
819 #ifdef TE_PE
820
821   ppc_current_section = text_section;
822   ppc_previous_section = 0;  
823
824 #endif
825 }
826
827 /* Insert an operand value into an instruction.  */
828
829 static unsigned long
830 ppc_insert_operand (insn, operand, val, file, line)
831      unsigned long insn;
832      const struct powerpc_operand *operand;
833      offsetT val;
834      char *file;
835      unsigned int line;
836 {
837   if (operand->bits != 32)
838     {
839       long min, max;
840       offsetT test;
841
842       if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
843         {
844           if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0
845               && ppc_size == PPC_OPCODE_32)
846             max = (1 << operand->bits) - 1;
847           else
848             max = (1 << (operand->bits - 1)) - 1;
849           min = - (1 << (operand->bits - 1));
850         }
851       else
852         {
853           max = (1 << operand->bits) - 1;
854           min = 0;
855         }
856
857       if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
858         test = - val;
859       else
860         test = val;
861
862       if (test < (offsetT) min || test > (offsetT) max)
863         {
864           const char *err =
865             "operand out of range (%s not between %ld and %ld)";
866           char buf[100];
867
868           sprint_value (buf, test);
869           if (file == (char *) NULL)
870             as_warn (err, buf, min, max);
871           else
872             as_warn_where (file, line, err, buf, min, max);
873         }
874     }
875
876   if (operand->insert)
877     {
878       const char *errmsg;
879
880       errmsg = NULL;
881       insn = (*operand->insert) (insn, (long) val, &errmsg);
882       if (errmsg != (const char *) NULL)
883         as_warn (errmsg);
884     }
885   else
886     insn |= (((long) val & ((1 << operand->bits) - 1))
887              << operand->shift);
888
889   return insn;
890 }
891
892 #ifdef OBJ_ELF
893 /* Parse @got, etc. and return the desired relocation.  */
894 static bfd_reloc_code_real_type
895 ppc_elf_suffix (str_p)
896      char **str_p;
897 {
898   struct map_bfd {
899     char *string;
900     int length;
901     bfd_reloc_code_real_type reloc;
902   };
903
904   char ident[20];
905   char *str = *str_p;
906   char *str2;
907   int ch;
908   int len;
909   struct map_bfd *ptr;
910
911 #define MAP(str,reloc) { str, sizeof(str)-1, reloc }
912
913   static struct map_bfd mapping[] = {
914     MAP ("got",         BFD_RELOC_PPC_TOC16),
915     MAP ("l",           BFD_RELOC_LO16),
916     MAP ("h",           BFD_RELOC_HI16),
917     MAP ("ha",          BFD_RELOC_HI16_S),
918     MAP ("brtaken",     BFD_RELOC_PPC_B16_BRTAKEN),
919     MAP ("brntaken",    BFD_RELOC_PPC_B16_BRNTAKEN),
920     MAP ("got@l",       BFD_RELOC_LO16_GOTOFF),
921     MAP ("got@h",       BFD_RELOC_HI16_GOTOFF),
922     MAP ("got@ha",      BFD_RELOC_HI16_S_GOTOFF),
923     MAP ("fixup",       BFD_RELOC_CTOR),                /* warnings with -mrelocatable */
924     MAP ("pltrel24",    BFD_RELOC_24_PLT_PCREL),
925     MAP ("copy",        BFD_RELOC_PPC_COPY),
926     MAP ("globdat",     BFD_RELOC_PPC_GLOB_DAT),
927     MAP ("local24pc",   BFD_RELOC_PPC_LOCAL24PC),
928     MAP ("plt",         BFD_RELOC_32_PLTOFF),
929     MAP ("pltrel",      BFD_RELOC_32_PLT_PCREL),
930     MAP ("plt@l",       BFD_RELOC_LO16_PLTOFF),
931     MAP ("plt@h",       BFD_RELOC_HI16_PLTOFF),
932     MAP ("plt@ha",      BFD_RELOC_HI16_S_PLTOFF),
933     MAP ("sdarel",      BFD_RELOC_GPREL16),
934     MAP ("sectoff",     BFD_RELOC_32_BASEREL),
935     MAP ("sectoff@l",   BFD_RELOC_LO16_BASEREL),
936     MAP ("sectoff@h",   BFD_RELOC_HI16_BASEREL),
937     MAP ("sectoff@ha",  BFD_RELOC_HI16_S_BASEREL),
938
939     { (char *)0,        0,      BFD_RELOC_UNUSED }
940   };
941
942   if (*str++ != '@')
943     return BFD_RELOC_UNUSED;
944
945   for (ch = *str, str2 = ident;
946        str2 < ident + sizeof(ident) - 1 && isalnum (ch) || ch == '@';
947        ch = *++str)
948     {
949       *str2++ = (islower (ch)) ? ch : tolower (ch);
950     }
951
952   *str2 = '\0';
953   len = str2 - ident;
954
955   ch = ident[0];
956   for (ptr = &mapping[0]; ptr->length > 0; ptr++)
957     if (ch == ptr->string[0] && len == ptr->length && memcmp (ident, ptr->string, ptr->length) == 0)
958       {
959         *str_p = str;
960         return ptr->reloc;
961       }
962
963   return BFD_RELOC_UNUSED;
964 }
965
966 /* Like normal .long/.short/.word, except support @got, etc. */
967 /* clobbers input_line_pointer, checks */
968 /* end-of-line. */
969 static void
970 ppc_elf_cons (nbytes)
971      register int nbytes;       /* 1=.byte, 2=.word, 4=.long */
972 {
973   expressionS exp;
974   bfd_reloc_code_real_type reloc;
975
976   if (is_it_end_of_statement ())
977     {
978       demand_empty_rest_of_line ();
979       return;
980     }
981
982   do
983     {
984       expression (&exp);
985       if (exp.X_op == O_symbol
986           && *input_line_pointer == '@'
987           && (reloc = ppc_elf_suffix (&input_line_pointer)) != BFD_RELOC_UNUSED)
988         {
989           reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
990           int size = bfd_get_reloc_size (reloc_howto);
991
992           if (size > nbytes)
993             as_bad ("%s relocations do not fit in %d bytes\n", reloc_howto->name, nbytes);
994
995           else
996             {
997               register char *p = frag_more ((int) nbytes);
998               int offset = nbytes - size;
999
1000               fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size, &exp, 0, reloc);
1001             }
1002         }
1003       else
1004         emit_expr (&exp, (unsigned int) nbytes);
1005     }
1006   while (*input_line_pointer++ == ',');
1007
1008   input_line_pointer--;         /* Put terminator back into stream. */
1009   demand_empty_rest_of_line ();
1010 }
1011
1012 /* Validate any relocations emitted for -mrelocatable, possibly adding
1013    fixups for word relocations in writable segments, so we can adjust
1014    them at runtime.  */
1015 static void
1016 ppc_elf_validate_fix (fixp, seg)
1017      fixS *fixp;
1018      segT seg;
1019 {
1020   if (mrelocatable
1021       && !fixp->fx_done
1022       && !fixp->fx_pcrel
1023       && fixp->fx_r_type <= BFD_RELOC_UNUSED
1024       && strcmp (segment_name (seg), ".got2") != 0
1025       && strcmp (segment_name (seg), ".dtors") != 0
1026       && strcmp (segment_name (seg), ".ctors") != 0
1027       && strcmp (segment_name (seg), ".fixup") != 0
1028       && strcmp (segment_name (seg), ".stab") != 0)
1029     {
1030       if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
1031           || fixp->fx_r_type != BFD_RELOC_CTOR)
1032         {
1033           as_warn_where (fixp->fx_file, fixp->fx_line,
1034                          "Relocation cannot be done when using -mrelocatable");
1035         }
1036     }
1037 }
1038
1039 #endif /* OBJ_ELF */
1040
1041 #ifdef TE_PE
1042 /*
1043  * Summary of register_name().
1044  *
1045  * in:  Input_line_pointer points to 1st char of operand.
1046  *
1047  * out: A expressionS.
1048  *      The operand may have been a register: in this case, X_op == O_register,
1049  *      X_add_number is set to the register number, and truth is returned.
1050  *      Input_line_pointer->(next non-blank) char after operand, or is in its
1051  *      original state.
1052  */
1053
1054 static int
1055 register_name (expressionP)
1056      expressionS *expressionP;
1057 {
1058   int reg_number;
1059   char *name;
1060   char c;
1061
1062   /* Find the spelling of the operand */
1063   name = input_line_pointer;
1064   c = get_symbol_end ();
1065   reg_number = reg_name_search (name);
1066
1067   /* look to see if it's in the register table */
1068   if (reg_number >= 0) 
1069     {
1070       expressionP->X_op = O_register;
1071       expressionP->X_add_number = reg_number;
1072       
1073       /* make the rest nice */
1074       expressionP->X_add_symbol = NULL;
1075       expressionP->X_op_symbol = NULL;
1076       *input_line_pointer = c;   /* put back the delimiting char */
1077       return 1;
1078     }
1079   else
1080     {
1081       /* reset the line as if we had not done anything */
1082       *input_line_pointer = c;   /* put back the delimiting char */
1083       input_line_pointer = name; /* reset input_line pointer */
1084       return 0;
1085     }
1086 }
1087
1088 /*
1089  * Summary of parse_toc_entry().
1090  *
1091  * in:  Input_line_pointer points to the '[' in one of:
1092  *
1093  *        [toc] [tocv] [toc32] [toc64]
1094  *
1095  *      Anything else is an error of one kind or another.
1096  *
1097  * out: 
1098  *   return value: success or failure
1099  *   toc_kind:     kind of toc reference
1100  *   input_line_pointer:
1101  *     success: first char after the ']'
1102  *     failure: unchanged
1103  *
1104  * settings:
1105  *
1106  *     [toc]   - rv == success, toc_kind = default_toc
1107  *     [tocv]  - rv == success, toc_kind = data_in_toc
1108  *     [toc32] - rv == success, toc_kind = must_be_32
1109  *     [toc64] - rv == success, toc_kind = must_be_64
1110  *
1111  */
1112
1113 enum toc_size_qualifier 
1114
1115   default_toc, /* The toc cell constructed should be the system default size */
1116   data_in_toc, /* This is a direct reference to a toc cell                   */
1117   must_be_32,  /* The toc cell constructed must be 32 bits wide              */
1118   must_be_64   /* The toc cell constructed must be 64 bits wide              */
1119 };
1120
1121 static int
1122 parse_toc_entry(toc_kind)
1123      enum toc_size_qualifier *toc_kind;
1124 {
1125   char *start;
1126   char *toc_spec;
1127   char c;
1128   enum toc_size_qualifier t;
1129
1130   /* save the input_line_pointer */
1131   start = input_line_pointer;
1132
1133   /* skip over the '[' , and whitespace */
1134   ++input_line_pointer;
1135   SKIP_WHITESPACE ();
1136   
1137   /* find the spelling of the operand */
1138   toc_spec = input_line_pointer;
1139   c = get_symbol_end ();
1140
1141   if (strcmp(toc_spec, "toc") == 0) 
1142     {
1143       t = default_toc;
1144     }
1145   else if (strcmp(toc_spec, "tocv") == 0) 
1146     {
1147       t = data_in_toc;
1148     }
1149   else if (strcmp(toc_spec, "toc32") == 0) 
1150     {
1151       t = must_be_32;
1152     }
1153   else if (strcmp(toc_spec, "toc64") == 0) 
1154     {
1155       t = must_be_64;
1156     }
1157   else
1158     {
1159       as_bad ("syntax error: invalid toc specifier `%s'", toc_spec);
1160       *input_line_pointer = c;   /* put back the delimiting char */
1161       input_line_pointer = start; /* reset input_line pointer */
1162       return 0;
1163     }
1164
1165   /* now find the ']' */
1166   *input_line_pointer = c;   /* put back the delimiting char */
1167
1168   SKIP_WHITESPACE ();        /* leading whitespace could be there. */
1169   c = *input_line_pointer++; /* input_line_pointer->past char in c. */
1170
1171   if (c != ']')
1172     {
1173       as_bad ("syntax error: expected `]', found  `%c'", c);
1174       input_line_pointer = start; /* reset input_line pointer */
1175       return 0;
1176     }
1177
1178   *toc_kind = t;             /* set return value */
1179   return 1;
1180 }
1181
1182 #endif
1183
1184
1185 /* We need to keep a list of fixups.  We can't simply generate them as
1186    we go, because that would require us to first create the frag, and
1187    that would screw up references to ``.''.  */
1188
1189 struct ppc_fixup
1190 {
1191   expressionS exp;
1192   int opindex;
1193   bfd_reloc_code_real_type reloc;
1194 };
1195
1196 #define MAX_INSN_FIXUPS (5)
1197
1198 /* This routine is called for each instruction to be assembled.  */
1199
1200 void
1201 md_assemble (str)
1202      char *str;
1203 {
1204   char *s;
1205   const struct powerpc_opcode *opcode;
1206   unsigned long insn;
1207   const unsigned char *opindex_ptr;
1208   int skip_optional;
1209   int need_paren;
1210   int next_opindex;
1211   struct ppc_fixup fixups[MAX_INSN_FIXUPS];
1212   int fc;
1213   char *f;
1214   int i;
1215 #ifdef OBJ_ELF
1216   bfd_reloc_code_real_type reloc;
1217 #endif
1218
1219   /* Get the opcode.  */
1220   for (s = str; *s != '\0' && ! isspace (*s); s++)
1221     ;
1222   if (*s != '\0')
1223     *s++ = '\0';
1224
1225   /* Look up the opcode in the hash table.  */
1226   opcode = (const struct powerpc_opcode *) hash_find (ppc_hash, str);
1227   if (opcode == (const struct powerpc_opcode *) NULL)
1228     {
1229       const struct powerpc_macro *macro;
1230
1231       macro = (const struct powerpc_macro *) hash_find (ppc_macro_hash, str);
1232       if (macro == (const struct powerpc_macro *) NULL)
1233         as_bad ("Unrecognized opcode: `%s'", str);
1234       else
1235         ppc_macro (s, macro);
1236
1237       return;
1238     }
1239
1240   insn = opcode->opcode;
1241
1242   str = s;
1243   while (isspace (*str))
1244     ++str;
1245
1246   /* PowerPC operands are just expressions.  The only real issue is
1247      that a few operand types are optional.  All cases which might use
1248      an optional operand separate the operands only with commas (in
1249      some cases parentheses are used, as in ``lwz 1,0(1)'' but such
1250      cases never have optional operands).  There is never more than
1251      one optional operand for an instruction.  So, before we start
1252      seriously parsing the operands, we check to see if we have an
1253      optional operand, and, if we do, we count the number of commas to
1254      see whether the operand should be omitted.  */
1255   skip_optional = 0;
1256   for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1257     {
1258       const struct powerpc_operand *operand;
1259
1260       operand = &powerpc_operands[*opindex_ptr];
1261       if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
1262         {
1263           unsigned int opcount;
1264
1265           /* There is an optional operand.  Count the number of
1266              commas in the input line.  */
1267           if (*str == '\0')
1268             opcount = 0;
1269           else
1270             {
1271               opcount = 1;
1272               s = str;
1273               while ((s = strchr (s, ',')) != (char *) NULL)
1274                 {
1275                   ++opcount;
1276                   ++s;
1277                 }
1278             }
1279
1280           /* If there are fewer operands in the line then are called
1281              for by the instruction, we want to skip the optional
1282              operand.  */
1283           if (opcount < strlen (opcode->operands))
1284             skip_optional = 1;
1285
1286           break;
1287         }
1288     }
1289
1290   /* Gather the operands.  */
1291   need_paren = 0;
1292   next_opindex = 0;
1293   fc = 0;
1294   for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1295     {
1296       const struct powerpc_operand *operand;
1297       const char *errmsg;
1298       char *hold;
1299       expressionS ex;
1300       char endc;
1301
1302       if (next_opindex == 0)
1303         operand = &powerpc_operands[*opindex_ptr];
1304       else
1305         {
1306           operand = &powerpc_operands[next_opindex];
1307           next_opindex = 0;
1308         }
1309
1310       errmsg = NULL;
1311
1312       /* If this is a fake operand, then we do not expect anything
1313          from the input.  */
1314       if ((operand->flags & PPC_OPERAND_FAKE) != 0)
1315         {
1316           insn = (*operand->insert) (insn, 0L, &errmsg);
1317           if (errmsg != (const char *) NULL)
1318             as_warn (errmsg);
1319           continue;
1320         }
1321
1322       /* If this is an optional operand, and we are skipping it, just
1323          insert a zero.  */
1324       if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
1325           && skip_optional)
1326         {
1327           if (operand->insert)
1328             {
1329               insn = (*operand->insert) (insn, 0L, &errmsg);
1330               if (errmsg != (const char *) NULL)
1331                 as_warn (errmsg);
1332             }
1333           if ((operand->flags & PPC_OPERAND_NEXT) != 0)
1334             next_opindex = *opindex_ptr + 1;
1335           continue;
1336         }
1337
1338       /* Gather the operand.  */
1339       hold = input_line_pointer;
1340       input_line_pointer = str;
1341
1342 #ifdef TE_PE
1343       if (*input_line_pointer == '[') 
1344         {
1345           /* We are expecting something like the second argument here:
1346
1347                 lwz r4,[toc].GS.0.static_int(rtoc)
1348                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1349              The argument following the `]' must be a symbol name, and the 
1350              register must be the toc register: 'rtoc' or '2'
1351
1352              The effect is to 0 as the displacement field
1353              in the instruction, and issue an IMAGE_REL_PPC_TOCREL16 (or
1354              the appropriate variation) reloc against it based on the symbol.
1355              The linker will build the toc, and insert the resolved toc offset.
1356
1357              Note:
1358              o The size of the toc entry is currently assumed to be
1359                32 bits. This should not be assumed to be a hard coded
1360                number.
1361              o In an effort to cope with a change from 32 to 64 bits,
1362                there are also toc entries that are specified to be
1363                either 32 or 64 bits:
1364                  lwz r4,[toc32].GS.0.static_int(rtoc)
1365                  lwz r4,[toc64].GS.0.static_int(rtoc)
1366                These demand toc entries of the specified size, and the
1367                instruction probably requires it.
1368           */
1369
1370           int valid_toc;
1371           enum toc_size_qualifier toc_kind;
1372           bfd_reloc_code_real_type toc_reloc;
1373
1374           /* go parse off the [tocXX] part */
1375           valid_toc = parse_toc_entry(&toc_kind);
1376
1377           if (!valid_toc) 
1378             {
1379               /* Note: message has already been issued.     */
1380               /* FIXME: what sort of recovery should we do? */
1381               /*        demand_rest_of_line(); return; ?    */
1382             }
1383
1384           /* Now get the symbol following the ']' */
1385           expression(&ex);
1386
1387           switch (toc_kind)
1388             {
1389             case default_toc:
1390               /* In this case, we may not have seen the symbol yet, since  */
1391               /* it is allowed to appear on a .extern or .globl or just be */
1392               /* a label in the .data section.                             */
1393               toc_reloc = BFD_RELOC_PPC_TOC16;
1394               break;
1395             case data_in_toc:
1396               /* 1. The symbol must be defined and either in the toc        */
1397               /*    section, or a global.                                   */
1398               /* 2. The reloc generated must have the TOCDEFN flag set in   */
1399               /*    upper bit mess of the reloc type.                       */
1400               /* FIXME: It's a little confusing what the tocv qualifier can */
1401               /*        be used for. At the very least, I've seen three     */
1402               /*        uses, only one of which I'm sure I can explain.     */
1403               if (ex.X_op == O_symbol) 
1404                 {                 
1405                   assert (ex.X_add_symbol != NULL);
1406                   if (ex.X_add_symbol->bsym->section != tocdata_section)
1407                     {
1408                       as_warn("[tocv] symbol is not a toc symbol");
1409                     }
1410                 }
1411
1412               toc_reloc = BFD_RELOC_PPC_TOC16;
1413               break;
1414             case must_be_32:
1415               /* FIXME: these next two specifically specify 32/64 bit toc   */
1416               /*        entries. We don't support them today. Is this the   */
1417               /*        right way to say that?                              */
1418               toc_reloc = BFD_RELOC_UNUSED;
1419               as_bad ("Unimplemented toc32 expression modifier");
1420               break;
1421             case must_be_64:
1422               /* FIXME: see above */
1423               toc_reloc = BFD_RELOC_UNUSED;
1424               as_bad ("Unimplemented toc64 expression modifier");
1425               break;
1426             default:
1427               fprintf(stderr, 
1428                       "Unexpected return value [%d] from parse_toc_entry!\n",
1429                       toc_kind);
1430               abort();
1431               break;
1432             }
1433
1434           /* We need to generate a fixup for this expression.  */
1435           if (fc >= MAX_INSN_FIXUPS)
1436             as_fatal ("too many fixups");
1437
1438           fixups[fc].reloc = toc_reloc;
1439           fixups[fc].exp = ex;
1440           fixups[fc].opindex = *opindex_ptr;
1441           ++fc;
1442
1443           /* Ok. We've set up the fixup for the instruction. Now make it
1444              look like the constant 0 was found here */
1445           ex.X_unsigned = 1;
1446           ex.X_op = O_constant;
1447           ex.X_add_number = 0;
1448           ex.X_add_symbol = NULL;
1449           ex.X_op_symbol = NULL;
1450         }
1451       else
1452         {
1453           if (!register_name(&ex))
1454             {
1455               expression (&ex);
1456             }
1457         }
1458
1459       str = input_line_pointer;
1460       input_line_pointer = hold;
1461 #else
1462       expression (&ex);
1463       str = input_line_pointer;
1464       input_line_pointer = hold;
1465 #endif
1466
1467       if (ex.X_op == O_illegal)
1468         as_bad ("illegal operand");
1469       else if (ex.X_op == O_absent)
1470         as_bad ("missing operand");
1471       else if (ex.X_op == O_constant)
1472         {
1473 #ifdef OBJ_ELF
1474           /* Allow @HA, @L, @H on constants. */
1475           char *orig_str = str;
1476
1477           if ((reloc = ppc_elf_suffix (&str)) != BFD_RELOC_UNUSED)
1478             switch (reloc)
1479               {
1480               default:
1481                 str = orig_str;
1482                 break;
1483
1484               case BFD_RELOC_LO16:
1485                 ex.X_add_number = ((ex.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1486                 break;
1487
1488               case BFD_RELOC_HI16:
1489                 ex.X_add_number = (ex.X_add_number >> 16) & 0xffff;
1490                 break;
1491
1492               case BFD_RELOC_HI16_S:
1493                 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff)
1494                   + ((ex.X_add_number >> 15) & 1);
1495                 break;
1496               }
1497 #endif
1498           insn = ppc_insert_operand (insn, operand, ex.X_add_number,
1499                                      (char *) NULL, 0);
1500         }
1501 #ifdef TE_PE
1502       else if (ex.X_op == O_register)
1503         {
1504           insn = ppc_insert_operand (insn, operand, ex.X_add_number,
1505                                      (char *) NULL, 0);
1506         }
1507 #endif
1508 #ifdef OBJ_ELF
1509       else if ((reloc = ppc_elf_suffix (&str)) != BFD_RELOC_UNUSED)
1510         {
1511           /* For the absoulte forms of branchs, convert the PC relative form back into
1512              the absolute.  */
1513           if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
1514             switch (reloc)
1515               {
1516               case BFD_RELOC_PPC_B26:           reloc = BFD_RELOC_PPC_BA26;             break;
1517               case BFD_RELOC_PPC_B16:           reloc = BFD_RELOC_PPC_BA16;             break;
1518               case BFD_RELOC_PPC_B16_BRTAKEN:   reloc = BFD_RELOC_PPC_BA16_BRTAKEN;     break;
1519               case BFD_RELOC_PPC_B16_BRNTAKEN:  reloc = BFD_RELOC_PPC_BA16_BRNTAKEN;    break;
1520               }
1521
1522           /* We need to generate a fixup for this expression.  */
1523           if (fc >= MAX_INSN_FIXUPS)
1524             as_fatal ("too many fixups");
1525           fixups[fc].exp = ex;
1526           fixups[fc].opindex = 0;
1527           fixups[fc].reloc = reloc;
1528           ++fc;
1529         }
1530 #endif /* OBJ_ELF */
1531
1532       else
1533         {
1534           /* We need to generate a fixup for this expression.  */
1535           if (fc >= MAX_INSN_FIXUPS)
1536             as_fatal ("too many fixups");
1537           fixups[fc].exp = ex;
1538           fixups[fc].opindex = *opindex_ptr;
1539           fixups[fc].reloc = BFD_RELOC_UNUSED;
1540           ++fc;
1541         }
1542
1543       if (need_paren)
1544         {
1545           endc = ')';
1546           need_paren = 0;
1547         }
1548       else if ((operand->flags & PPC_OPERAND_PARENS) != 0)
1549         {
1550           endc = '(';
1551           need_paren = 1;
1552         }
1553       else
1554         endc = ',';
1555
1556       /* The call to expression should have advanced str past any
1557          whitespace.  */
1558       if (*str != endc
1559           && (endc != ',' || *str != '\0'))
1560         {
1561           as_bad ("syntax error; found `%c' but expected `%c'", *str, endc);
1562           break;
1563         }
1564
1565       if (*str != '\0')
1566         ++str;
1567     }
1568
1569   while (isspace (*str))
1570     ++str;
1571
1572   if (*str != '\0')
1573     as_bad ("junk at end of line: `%s'", str);
1574
1575   /* Write out the instruction.  */
1576   f = frag_more (4);
1577   md_number_to_chars (f, insn, 4);
1578
1579   /* Create any fixups.  At this point we do not use a
1580      bfd_reloc_code_real_type, but instead just use the
1581      BFD_RELOC_UNUSED plus the operand index.  This lets us easily
1582      handle fixups for any operand type, although that is admittedly
1583      not a very exciting feature.  We pick a BFD reloc type in
1584      md_apply_fix.  */
1585   for (i = 0; i < fc; i++)
1586     {
1587       const struct powerpc_operand *operand;
1588
1589       operand = &powerpc_operands[fixups[i].opindex];
1590       if (fixups[i].reloc != BFD_RELOC_UNUSED)
1591         {
1592           reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1593           int size;
1594           int offset;
1595           fixS *fixP;
1596
1597           if (!reloc_howto)
1598             abort ();
1599
1600           size = bfd_get_reloc_size (reloc_howto);
1601           offset = target_big_endian ? (4 - size) : 0;
1602
1603           if (size < 1 || size > 4)
1604             abort();
1605
1606           fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
1607                               &fixups[i].exp, reloc_howto->pc_relative,
1608                               fixups[i].reloc);
1609
1610           /* Turn off complaints that the addend is too large for things like
1611              foo+100000@ha.  */
1612           switch (fixups[i].reloc)
1613             {
1614             case BFD_RELOC_LO16:
1615             case BFD_RELOC_HI16:
1616             case BFD_RELOC_HI16_S:
1617               fixP->fx_no_overflow = 1;
1618               break;
1619             default:
1620               break;
1621             }
1622         }
1623       else
1624         fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1625                      &fixups[i].exp,
1626                      (operand->flags & PPC_OPERAND_RELATIVE) != 0,
1627                      ((bfd_reloc_code_real_type)
1628                        (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
1629     }
1630 }
1631
1632 #ifndef WORKING_DOT_WORD
1633 /* Handle long and short jumps */
1634 void
1635 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1636      char *ptr;
1637      addressT from_addr, to_addr;
1638      fragS *frag;
1639      symbolS *to_symbol;
1640 {
1641   abort ();
1642 }
1643
1644 void
1645 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1646      char *ptr;
1647      addressT from_addr, to_addr;
1648      fragS *frag;
1649      symbolS *to_symbol;
1650 {
1651   abort ();
1652 }
1653 #endif
1654
1655 /* Handle a macro.  Gather all the operands, transform them as
1656    described by the macro, and call md_assemble recursively.  All the
1657    operands are separated by commas; we don't accept parentheses
1658    around operands here.  */
1659
1660 static void
1661 ppc_macro (str, macro)
1662      char *str;
1663      const struct powerpc_macro *macro;
1664 {
1665   char *operands[10];
1666   unsigned int count;
1667   char *s;
1668   unsigned int len;
1669   const char *format;
1670   int arg;
1671   char *send;
1672   char *complete;
1673
1674   /* Gather the users operands into the operands array.  */
1675   count = 0;
1676   s = str;
1677   while (1)
1678     {
1679       if (count >= sizeof operands / sizeof operands[0])
1680         break;
1681       operands[count++] = s;
1682       s = strchr (s, ',');
1683       if (s == (char *) NULL)
1684         break;
1685       *s++ = '\0';
1686     }  
1687
1688   if (count != macro->operands)
1689     {
1690       as_bad ("wrong number of operands");
1691       return;
1692     }
1693
1694   /* Work out how large the string must be (the size is unbounded
1695      because it includes user input).  */
1696   len = 0;
1697   format = macro->format;
1698   while (*format != '\0')
1699     {
1700       if (*format != '%')
1701         {
1702           ++len;
1703           ++format;
1704         }
1705       else
1706         {
1707           arg = strtol (format + 1, &send, 10);
1708           know (send != format && arg >= 0 && arg < count);
1709           len += strlen (operands[arg]);
1710           format = send;
1711         }
1712     }
1713
1714   /* Put the string together.  */
1715   complete = s = (char *) alloca (len + 1);
1716   format = macro->format;
1717   while (*format != '\0')
1718     {
1719       if (*format != '%')
1720         *s++ = *format++;
1721       else
1722         {
1723           arg = strtol (format + 1, &send, 10);
1724           strcpy (s, operands[arg]);
1725           s += strlen (s);
1726           format = send;
1727         }
1728     }
1729   *s = '\0';
1730
1731   /* Assemble the constructed instruction.  */
1732   md_assemble (complete);
1733 }  
1734 \f
1735 /* Pseudo-op handling.  */
1736
1737 /* The .byte pseudo-op.  This is similar to the normal .byte
1738    pseudo-op, but it can also take a single ASCII string.  */
1739
1740 static void
1741 ppc_byte (ignore)
1742      int ignore;
1743 {
1744   if (*input_line_pointer != '\"')
1745     {
1746       cons (1);
1747       return;
1748     }
1749
1750   /* Gather characters.  A real double quote is doubled.  Unusual
1751      characters are not permitted.  */
1752   ++input_line_pointer;
1753   while (1)
1754     {
1755       char c;
1756
1757       c = *input_line_pointer++;
1758
1759       if (c == '\"')
1760         {
1761           if (*input_line_pointer != '\"')
1762             break;
1763           ++input_line_pointer;
1764         }
1765
1766       FRAG_APPEND_1_CHAR (c);
1767     }
1768
1769   demand_empty_rest_of_line ();
1770 }
1771 \f
1772 #ifdef OBJ_XCOFF
1773
1774 /* XCOFF specific pseudo-op handling.  */
1775
1776 /* This is set if we are creating a .stabx symbol, since we don't want
1777    to handle symbol suffixes for such symbols.  */
1778 static boolean ppc_stab_symbol;
1779
1780 /* The .comm and .lcomm pseudo-ops for XCOFF.  XCOFF puts common
1781    symbols in the .bss segment as though they were local common
1782    symbols, and uses a different smclas.  */
1783
1784 static void
1785 ppc_comm (lcomm)
1786      int lcomm;
1787 {
1788   asection *current_seg = now_seg;
1789   subsegT current_subseg = now_subseg;
1790   char *name;
1791   char endc;
1792   char *end_name;
1793   offsetT size;
1794   offsetT align;
1795   symbolS *lcomm_sym = NULL;
1796   symbolS *sym;
1797   char *pfrag;
1798
1799   name = input_line_pointer;
1800   endc = get_symbol_end ();
1801   end_name = input_line_pointer;
1802   *end_name = endc;
1803
1804   if (*input_line_pointer != ',')
1805     {
1806       as_bad ("missing size");
1807       ignore_rest_of_line ();
1808       return;
1809     }
1810   ++input_line_pointer;
1811
1812   size = get_absolute_expression ();
1813   if (size < 0)
1814     {
1815       as_bad ("negative size");
1816       ignore_rest_of_line ();
1817       return;
1818     }
1819
1820   if (! lcomm)
1821     {
1822       /* The third argument to .comm is the alignment.  */
1823       if (*input_line_pointer != ',')
1824         align = 3;
1825       else
1826         {
1827           ++input_line_pointer;
1828           align = get_absolute_expression ();
1829           if (align <= 0)
1830             {
1831               as_warn ("ignoring bad alignment");
1832               align = 3;
1833             }
1834         }
1835     }
1836   else
1837     {
1838       char *lcomm_name;
1839       char lcomm_endc;
1840
1841       if (size <= 1)
1842         align = 0;
1843       else if (size <= 2)
1844         align = 1;
1845       else if (size <= 4)
1846         align = 2;
1847       else
1848         align = 3;
1849
1850       /* The third argument to .lcomm appears to be the real local
1851          common symbol to create.  References to the symbol named in
1852          the first argument are turned into references to the third
1853          argument.  */
1854       if (*input_line_pointer != ',')
1855         {
1856           as_bad ("missing real symbol name");
1857           ignore_rest_of_line ();
1858           return;
1859         }
1860       ++input_line_pointer;
1861
1862       lcomm_name = input_line_pointer;
1863       lcomm_endc = get_symbol_end ();
1864       
1865       lcomm_sym = symbol_find_or_make (lcomm_name);
1866
1867       *input_line_pointer = lcomm_endc;
1868     }
1869
1870   *end_name = '\0';
1871   sym = symbol_find_or_make (name);
1872   *end_name = endc;
1873
1874   if (S_IS_DEFINED (sym)
1875       || S_GET_VALUE (sym) != 0)
1876     {
1877       as_bad ("attempt to redefine symbol");
1878       ignore_rest_of_line ();
1879       return;
1880     }
1881     
1882   record_alignment (bss_section, align);
1883           
1884   if (! lcomm
1885       || ! S_IS_DEFINED (lcomm_sym))
1886     {
1887       symbolS *def_sym;
1888       offsetT def_size;
1889
1890       if (! lcomm)
1891         {
1892           def_sym = sym;
1893           def_size = size;
1894           S_SET_EXTERNAL (sym);
1895         }
1896       else
1897         {
1898           lcomm_sym->sy_tc.output = 1;
1899           def_sym = lcomm_sym;
1900           def_size = 0;
1901         }
1902
1903       subseg_set (bss_section, 1);
1904       frag_align (align, 0);
1905   
1906       def_sym->sy_frag = frag_now;
1907       pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, def_sym,
1908                         def_size, (char *) NULL);
1909       *pfrag = 0;
1910       S_SET_SEGMENT (def_sym, bss_section);
1911       def_sym->sy_tc.align = align;
1912     }
1913   else if (lcomm)
1914     {
1915       /* Align the size of lcomm_sym.  */
1916       lcomm_sym->sy_frag->fr_offset =
1917         ((lcomm_sym->sy_frag->fr_offset + (1 << align) - 1)
1918          &~ ((1 << align) - 1));
1919       if (align > lcomm_sym->sy_tc.align)
1920         lcomm_sym->sy_tc.align = align;
1921     }
1922
1923   if (lcomm)
1924     {
1925       /* Make sym an offset from lcomm_sym.  */
1926       S_SET_SEGMENT (sym, bss_section);
1927       sym->sy_frag = lcomm_sym->sy_frag;
1928       S_SET_VALUE (sym, lcomm_sym->sy_frag->fr_offset);
1929       lcomm_sym->sy_frag->fr_offset += size;
1930     }
1931
1932   subseg_set (current_seg, current_subseg);
1933
1934   demand_empty_rest_of_line ();
1935 }
1936
1937 /* The .csect pseudo-op.  This switches us into a different
1938    subsegment.  The first argument is a symbol whose value is the
1939    start of the .csect.  In COFF, csect symbols get special aux
1940    entries defined by the x_csect field of union internal_auxent.  The
1941    optional second argument is the alignment (the default is 2).  */
1942
1943 static void
1944 ppc_csect (ignore)
1945      int ignore;
1946 {
1947   char *name;
1948   char endc;
1949   symbolS *sym;
1950
1951   name = input_line_pointer;
1952   endc = get_symbol_end ();
1953   
1954   sym = symbol_find_or_make (name);
1955
1956   *input_line_pointer = endc;
1957
1958   ppc_change_csect (sym);
1959
1960   if (*input_line_pointer == ',')
1961     {
1962       ++input_line_pointer;
1963       sym->sy_tc.align = get_absolute_expression ();
1964     }
1965
1966   demand_empty_rest_of_line ();
1967 }
1968
1969 /* Change to a different csect.  */
1970
1971 static void
1972 ppc_change_csect (sym)
1973      symbolS *sym;
1974 {
1975   if (S_IS_DEFINED (sym))
1976     subseg_set (S_GET_SEGMENT (sym), sym->sy_tc.subseg);
1977   else
1978     {
1979       symbolS **list_ptr;
1980       int after_toc;
1981       symbolS *list;
1982
1983       /* This is a new csect.  We need to look at the symbol class to
1984          figure out whether it should go in the text section or the
1985          data section.  */
1986       after_toc = 0;
1987       switch (sym->sy_tc.class)
1988         {
1989         case XMC_PR:
1990         case XMC_RO:
1991         case XMC_DB:
1992         case XMC_GL:
1993         case XMC_XO:
1994         case XMC_SV:
1995         case XMC_TI:
1996         case XMC_TB:
1997           S_SET_SEGMENT (sym, text_section);
1998           sym->sy_tc.subseg = ppc_text_subsegment;
1999           ++ppc_text_subsegment;
2000           list_ptr = &ppc_text_csects;
2001           break;
2002         case XMC_RW:
2003         case XMC_TC0:
2004         case XMC_TC:
2005         case XMC_DS:
2006         case XMC_UA:
2007         case XMC_BS:
2008         case XMC_UC:
2009           if (ppc_toc_csect != NULL
2010               && ppc_toc_csect->sy_tc.subseg + 1 == ppc_data_subsegment)
2011             after_toc = 1;
2012           S_SET_SEGMENT (sym, data_section);
2013           sym->sy_tc.subseg = ppc_data_subsegment;
2014           ++ppc_data_subsegment;
2015           list_ptr = &ppc_data_csects;
2016           break;
2017         default:
2018           abort ();
2019         }
2020
2021       subseg_new (segment_name (S_GET_SEGMENT (sym)), sym->sy_tc.subseg);
2022       if (after_toc)
2023         ppc_after_toc_frag = frag_now;
2024
2025       sym->sy_frag = frag_now;
2026       S_SET_VALUE (sym, (valueT) frag_now_fix ());
2027
2028       sym->sy_tc.align = 2;
2029       sym->sy_tc.output = 1;
2030       sym->sy_tc.within = sym;
2031           
2032       for (list = *list_ptr;
2033            list->sy_tc.next != (symbolS *) NULL;
2034            list = list->sy_tc.next)
2035         ;
2036       list->sy_tc.next = sym;
2037           
2038       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2039       symbol_append (sym, list->sy_tc.within, &symbol_rootP, &symbol_lastP);
2040     }
2041
2042   ppc_current_csect = sym;
2043 }
2044
2045 /* This function handles the .text and .data pseudo-ops.  These
2046    pseudo-ops aren't really used by XCOFF; we implement them for the
2047    convenience of people who aren't used to XCOFF.  */
2048
2049 static void
2050 ppc_section (type)
2051      int type;
2052 {
2053   const char *name;
2054   symbolS *sym;
2055
2056   if (type == 't')
2057     name = ".text[PR]";
2058   else if (type == 'd')
2059     name = ".data[RW]";
2060   else
2061     abort ();
2062
2063   sym = symbol_find_or_make (name);
2064
2065   ppc_change_csect (sym);
2066
2067   demand_empty_rest_of_line ();
2068 }
2069
2070 /* The .extern pseudo-op.  We create an undefined symbol.  */
2071
2072 static void
2073 ppc_extern (ignore)
2074      int ignore;
2075 {
2076   char *name;
2077   char endc;
2078
2079   name = input_line_pointer;
2080   endc = get_symbol_end ();
2081
2082   (void) symbol_find_or_make (name);
2083
2084   *input_line_pointer = endc;
2085
2086   demand_empty_rest_of_line ();
2087 }
2088
2089 /* The .lglobl pseudo-op.  I think the RS/6000 assembler only needs
2090    this because it can't handle undefined symbols.  I think we can
2091    just ignore it.  */
2092
2093 static void
2094 ppc_lglobl (ignore)
2095      int ignore;
2096 {
2097   s_ignore (0);
2098 }
2099
2100 /* The .rename pseudo-op.  The RS/6000 assembler can rename symbols,
2101    although I don't know why it bothers.  */
2102
2103 static void
2104 ppc_rename (ignore)
2105      int ignore;
2106 {
2107   char *name;
2108   char endc;
2109   symbolS *sym;
2110   int len;
2111
2112   name = input_line_pointer;
2113   endc = get_symbol_end ();
2114
2115   sym = symbol_find_or_make (name);
2116
2117   *input_line_pointer = endc;
2118
2119   if (*input_line_pointer != ',')
2120     {
2121       as_bad ("missing rename string");
2122       ignore_rest_of_line ();
2123       return;
2124     }
2125   ++input_line_pointer;
2126
2127   sym->sy_tc.real_name = demand_copy_C_string (&len);
2128
2129   demand_empty_rest_of_line ();
2130 }
2131
2132 /* The .stabx pseudo-op.  This is similar to a normal .stabs
2133    pseudo-op, but slightly different.  A sample is
2134        .stabx "main:F-1",.main,142,0
2135    The first argument is the symbol name to create.  The second is the
2136    value, and the third is the storage class.  The fourth seems to be
2137    always zero, and I am assuming it is the type.  */
2138
2139 static void
2140 ppc_stabx (ignore)
2141      int ignore;
2142 {
2143   char *name;
2144   int len;
2145   symbolS *sym;
2146   expressionS exp;
2147
2148   name = demand_copy_C_string (&len);
2149
2150   if (*input_line_pointer != ',')
2151     {
2152       as_bad ("missing value");
2153       return;
2154     }
2155   ++input_line_pointer;
2156
2157   ppc_stab_symbol = true;
2158   sym = symbol_make (name);
2159   ppc_stab_symbol = false;
2160
2161   sym->sy_tc.real_name = name;
2162
2163   (void) expression (&exp);
2164
2165   switch (exp.X_op)
2166     {
2167     case O_illegal:
2168     case O_absent:
2169     case O_big:
2170       as_bad ("illegal .stabx expression; zero assumed");
2171       exp.X_add_number = 0;
2172       /* Fall through.  */
2173     case O_constant:
2174       S_SET_VALUE (sym, (valueT) exp.X_add_number);
2175       sym->sy_frag = &zero_address_frag;
2176       break;
2177
2178     case O_symbol:
2179       if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section)
2180         sym->sy_value = exp;
2181       else
2182         {
2183           S_SET_VALUE (sym,
2184                        exp.X_add_number + S_GET_VALUE (exp.X_add_symbol));
2185           sym->sy_frag = exp.X_add_symbol->sy_frag;
2186         }
2187       break;
2188
2189     default:
2190       /* The value is some complex expression.  This will probably
2191          fail at some later point, but this is probably the right
2192          thing to do here.  */
2193       sym->sy_value = exp;
2194       break;
2195     }
2196
2197   S_SET_SEGMENT (sym, ppc_coff_debug_section);
2198   sym->bsym->flags |= BSF_DEBUGGING;
2199
2200   if (*input_line_pointer != ',')
2201     {
2202       as_bad ("missing class");
2203       return;
2204     }
2205   ++input_line_pointer;
2206
2207   S_SET_STORAGE_CLASS (sym, get_absolute_expression ());
2208
2209   if (*input_line_pointer != ',')
2210     {
2211       as_bad ("missing type");
2212       return;
2213     }
2214   ++input_line_pointer;
2215
2216   S_SET_DATA_TYPE (sym, get_absolute_expression ());
2217
2218   sym->sy_tc.output = 1;
2219
2220   if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
2221     sym->sy_tc.within = ppc_current_block;
2222
2223   if (exp.X_op != O_symbol
2224       || ! S_IS_EXTERNAL (exp.X_add_symbol)
2225       || S_GET_SEGMENT (exp.X_add_symbol) != bss_section)
2226     ppc_frob_label (sym);
2227   else
2228     {
2229       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2230       symbol_append (sym, exp.X_add_symbol, &symbol_rootP, &symbol_lastP);
2231       if (ppc_current_csect->sy_tc.within == exp.X_add_symbol)
2232         ppc_current_csect->sy_tc.within = sym;
2233     }
2234
2235   if (strlen (name) > SYMNMLEN)
2236     {
2237       /* For some reason, each name is preceded by a two byte length
2238          and followed by a null byte.  */
2239       ppc_debug_name_section_size += strlen (name) + 3;
2240     }
2241
2242   demand_empty_rest_of_line ();
2243 }
2244
2245 /* The .function pseudo-op.  This takes several arguments.  The first
2246    argument seems to be the external name of the symbol.  The second
2247    argment seems to be the label for the start of the function.  gcc
2248    uses the same name for both.  I have no idea what the third and
2249    fourth arguments are meant to be.  The optional fifth argument is
2250    an expression for the size of the function.  In COFF this symbol
2251    gets an aux entry like that used for a csect.  */
2252
2253 static void
2254 ppc_function (ignore)
2255      int ignore;
2256 {
2257   char *name;
2258   char endc;
2259   char *s;
2260   symbolS *ext_sym;
2261   symbolS *lab_sym;
2262
2263   name = input_line_pointer;
2264   endc = get_symbol_end ();
2265
2266   /* Ignore any [PR] suffix.  */
2267   name = ppc_canonicalize_symbol_name (name);
2268   s = strchr (name, '[');
2269   if (s != (char *) NULL
2270       && strcmp (s + 1, "PR]") == 0)
2271     *s = '\0';
2272
2273   ext_sym = symbol_find_or_make (name);
2274
2275   *input_line_pointer = endc;
2276
2277   if (*input_line_pointer != ',')
2278     {
2279       as_bad ("missing symbol name");
2280       ignore_rest_of_line ();
2281       return;
2282     }
2283   ++input_line_pointer;
2284
2285   name = input_line_pointer;
2286   endc = get_symbol_end ();
2287
2288   lab_sym = symbol_find_or_make (name);
2289
2290   *input_line_pointer = endc;
2291
2292   if (ext_sym != lab_sym)
2293     {
2294       ext_sym->sy_value.X_op = O_symbol;
2295       ext_sym->sy_value.X_add_symbol = lab_sym;
2296       ext_sym->sy_value.X_op_symbol = NULL;
2297       ext_sym->sy_value.X_add_number = 0;
2298     }
2299
2300   if (ext_sym->sy_tc.class == -1)
2301     ext_sym->sy_tc.class = XMC_PR;
2302   ext_sym->sy_tc.output = 1;
2303
2304   if (*input_line_pointer == ',')
2305     {
2306       expressionS ignore;
2307
2308       /* Ignore the third argument.  */
2309       ++input_line_pointer;
2310       expression (&ignore);
2311       if (*input_line_pointer == ',')
2312         {
2313           /* Ignore the fourth argument.  */
2314           ++input_line_pointer;
2315           expression (&ignore);
2316           if (*input_line_pointer == ',')
2317             {
2318               /* The fifth argument is the function size.  */
2319               ++input_line_pointer;
2320               ext_sym->sy_tc.size = symbol_new ("L0\001",
2321                                                 absolute_section,
2322                                                 (valueT) 0,
2323                                                 &zero_address_frag);
2324               pseudo_set (ext_sym->sy_tc.size);
2325             }
2326         }
2327     }
2328
2329   S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
2330   SF_SET_FUNCTION (ext_sym);
2331   SF_SET_PROCESS (ext_sym);
2332   coff_add_linesym (ext_sym);
2333
2334   demand_empty_rest_of_line ();
2335 }
2336
2337 /* The .bf pseudo-op.  This is just like a COFF C_FCN symbol named
2338    ".bf".  */
2339
2340 static void
2341 ppc_bf (ignore)
2342      int ignore;
2343 {
2344   symbolS *sym;
2345
2346   sym = symbol_make (".bf");
2347   S_SET_SEGMENT (sym, text_section);
2348   sym->sy_frag = frag_now;
2349   S_SET_VALUE (sym, frag_now_fix ());
2350   S_SET_STORAGE_CLASS (sym, C_FCN);
2351
2352   coff_line_base = get_absolute_expression ();
2353
2354   S_SET_NUMBER_AUXILIARY (sym, 1);
2355   SA_SET_SYM_LNNO (sym, coff_line_base);
2356
2357   sym->sy_tc.output = 1;
2358
2359   ppc_frob_label (sym);
2360
2361   demand_empty_rest_of_line ();
2362 }
2363
2364 /* The .ef pseudo-op.  This is just like a COFF C_FCN symbol named
2365    ".ef", except that the line number is absolute, not relative to the
2366    most recent ".bf" symbol.  */
2367
2368 static void
2369 ppc_ef (ignore)
2370      int ignore;
2371 {
2372   symbolS *sym;
2373
2374   sym = symbol_make (".ef");
2375   S_SET_SEGMENT (sym, text_section);
2376   sym->sy_frag = frag_now;
2377   S_SET_VALUE (sym, frag_now_fix ());
2378   S_SET_STORAGE_CLASS (sym, C_FCN);
2379   S_SET_NUMBER_AUXILIARY (sym, 1);
2380   SA_SET_SYM_LNNO (sym, get_absolute_expression ());
2381   sym->sy_tc.output = 1;
2382
2383   ppc_frob_label (sym);
2384
2385   demand_empty_rest_of_line ();
2386 }
2387
2388 /* The .bi and .ei pseudo-ops.  These take a string argument and
2389    generates a C_BINCL or C_EINCL symbol, which goes at the start of
2390    the symbol list.  */
2391
2392 static void
2393 ppc_biei (ei)
2394      int ei;
2395 {
2396   char *name;
2397   int len;
2398   symbolS *sym;
2399   symbolS *look;
2400
2401   name = demand_copy_C_string (&len);
2402
2403   /* The value of these symbols is actually file offset.  Here we set
2404      the value to the index into the line number entries.  In
2405      ppc_frob_symbols we set the fix_line field, which will cause BFD
2406      to do the right thing.  */
2407
2408   sym = symbol_make (name);
2409   S_SET_SEGMENT (sym, now_seg);
2410   S_SET_VALUE (sym, coff_n_line_nos);
2411   sym->bsym->flags |= BSF_DEBUGGING;
2412
2413   /* obj-coff.c currently only handles line numbers correctly in the
2414      .text section.  */
2415   assert (now_seg == text_section);
2416
2417   S_SET_STORAGE_CLASS (sym, ei ? C_EINCL : C_BINCL);
2418   sym->sy_tc.output = 1;
2419   
2420   for (look = symbol_rootP;
2421        (look != (symbolS *) NULL
2422         && (S_GET_STORAGE_CLASS (look) == C_FILE
2423             || S_GET_STORAGE_CLASS (look) == C_BINCL
2424             || S_GET_STORAGE_CLASS (look) == C_EINCL));
2425        look = symbol_next (look))
2426     ;
2427   if (look != (symbolS *) NULL)
2428     {
2429       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2430       symbol_insert (sym, look, &symbol_rootP, &symbol_lastP);
2431     }
2432
2433   demand_empty_rest_of_line ();
2434 }
2435
2436 /* The .bs pseudo-op.  This generates a C_BSTAT symbol named ".bs".
2437    There is one argument, which is a csect symbol.  The value of the
2438    .bs symbol is the index of this csect symbol.  */
2439
2440 static void
2441 ppc_bs (ignore)
2442      int ignore;
2443 {
2444   char *name;
2445   char endc;
2446   symbolS *csect;
2447   symbolS *sym;
2448
2449   if (ppc_current_block != NULL)
2450     as_bad ("nested .bs blocks");
2451
2452   name = input_line_pointer;
2453   endc = get_symbol_end ();
2454
2455   csect = symbol_find_or_make (name);
2456
2457   *input_line_pointer = endc;
2458
2459   sym = symbol_make (".bs");
2460   S_SET_SEGMENT (sym, now_seg);
2461   S_SET_STORAGE_CLASS (sym, C_BSTAT);
2462   sym->bsym->flags |= BSF_DEBUGGING;
2463   sym->sy_tc.output = 1;
2464
2465   sym->sy_tc.within = csect;
2466
2467   ppc_frob_label (sym);
2468
2469   ppc_current_block = sym;
2470
2471   demand_empty_rest_of_line ();
2472 }
2473
2474 /* The .es pseudo-op.  Generate a C_ESTART symbol named .es.  */
2475
2476 static void
2477 ppc_es (ignore)
2478      int ignore;
2479 {
2480   symbolS *sym;
2481
2482   if (ppc_current_block == NULL)
2483     as_bad (".es without preceding .bs");
2484
2485   sym = symbol_make (".es");
2486   S_SET_SEGMENT (sym, now_seg);
2487   S_SET_STORAGE_CLASS (sym, C_ESTAT);
2488   sym->bsym->flags |= BSF_DEBUGGING;
2489   sym->sy_tc.output = 1;
2490
2491   ppc_frob_label (sym);
2492
2493   ppc_current_block = NULL;
2494
2495   demand_empty_rest_of_line ();
2496 }
2497
2498 /* The .bb pseudo-op.  Generate a C_BLOCK symbol named .bb, with a
2499    line number.  */
2500
2501 static void
2502 ppc_bb (ignore)
2503      int ignore;
2504 {
2505   symbolS *sym;
2506
2507   sym = symbol_make (".bb");
2508   S_SET_SEGMENT (sym, text_section);
2509   sym->sy_frag = frag_now;
2510   S_SET_VALUE (sym, frag_now_fix ());
2511   S_SET_STORAGE_CLASS (sym, C_BLOCK);
2512
2513   S_SET_NUMBER_AUXILIARY (sym, 1);
2514   SA_SET_SYM_LNNO (sym, get_absolute_expression ());
2515
2516   sym->sy_tc.output = 1;
2517
2518   SF_SET_PROCESS (sym);
2519
2520   ppc_frob_label (sym);
2521
2522   demand_empty_rest_of_line ();
2523 }
2524
2525 /* The .eb pseudo-op.  Generate a C_BLOCK symbol named .eb, with a
2526    line number.  */
2527
2528 static void
2529 ppc_eb (ignore)
2530      int ignore;
2531 {
2532   symbolS *sym;
2533
2534   sym = symbol_make (".eb");
2535   S_SET_SEGMENT (sym, text_section);
2536   sym->sy_frag = frag_now;
2537   S_SET_VALUE (sym, frag_now_fix ());
2538   S_SET_STORAGE_CLASS (sym, C_BLOCK);
2539   S_SET_NUMBER_AUXILIARY (sym, 1);
2540   SA_SET_SYM_LNNO (sym, get_absolute_expression ());
2541   sym->sy_tc.output = 1;
2542
2543   SF_SET_PROCESS (sym);
2544
2545   ppc_frob_label (sym);
2546
2547   demand_empty_rest_of_line ();
2548 }
2549
2550 /* The .bc pseudo-op.  This just creates a C_BCOMM symbol with a
2551    specified name.  */
2552
2553 static void
2554 ppc_bc (ignore)
2555      int ignore;
2556 {
2557   char *name;
2558   int len;
2559   symbolS *sym;
2560
2561   name = demand_copy_C_string (&len);
2562   sym = symbol_make (name);
2563   S_SET_SEGMENT (sym, ppc_coff_debug_section);
2564   sym->bsym->flags |= BSF_DEBUGGING;
2565   S_SET_STORAGE_CLASS (sym, C_BCOMM);
2566   S_SET_VALUE (sym, 0);
2567   sym->sy_tc.output = 1;
2568
2569   ppc_frob_label (sym);
2570
2571   if (strlen (name) > SYMNMLEN)
2572     ppc_debug_name_section_size += strlen (name) + 3;
2573
2574   demand_empty_rest_of_line ();
2575 }
2576
2577 /* The .ec pseudo-op.  This just creates a C_ECOMM symbol.  */
2578
2579 static void
2580 ppc_ec (ignore)
2581      int ignore;
2582 {
2583   symbolS *sym;
2584
2585   sym = symbol_make (".ec");
2586   S_SET_SEGMENT (sym, ppc_coff_debug_section);
2587   sym->bsym->flags |= BSF_DEBUGGING;
2588   S_SET_STORAGE_CLASS (sym, C_ECOMM);
2589   S_SET_VALUE (sym, 0);
2590   sym->sy_tc.output = 1;
2591
2592   ppc_frob_label (sym);
2593
2594   demand_empty_rest_of_line ();
2595 }
2596
2597 /* The .toc pseudo-op.  Switch to the .toc subsegment.  */
2598
2599 static void
2600 ppc_toc (ignore)
2601      int ignore;
2602 {
2603   if (ppc_toc_csect != (symbolS *) NULL)
2604     subseg_set (data_section, ppc_toc_csect->sy_tc.subseg);
2605   else
2606     {
2607       subsegT subseg;
2608       symbolS *sym;
2609       symbolS *list;
2610     
2611       subseg = ppc_data_subsegment;
2612       ++ppc_data_subsegment;
2613
2614       subseg_new (segment_name (data_section), subseg);
2615       ppc_toc_frag = frag_now;
2616
2617       sym = symbol_find_or_make ("TOC[TC0]");
2618       sym->sy_frag = frag_now;
2619       S_SET_SEGMENT (sym, data_section);
2620       S_SET_VALUE (sym, (valueT) frag_now_fix ());
2621       sym->sy_tc.subseg = subseg;
2622       sym->sy_tc.output = 1;
2623       sym->sy_tc.within = sym;
2624
2625       ppc_toc_csect = sym;
2626           
2627       for (list = ppc_data_csects;
2628            list->sy_tc.next != (symbolS *) NULL;
2629            list = list->sy_tc.next)
2630         ;
2631       list->sy_tc.next = sym;
2632
2633       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2634       symbol_append (sym, list->sy_tc.within, &symbol_rootP, &symbol_lastP);
2635     }
2636
2637   ppc_current_csect = ppc_toc_csect;
2638
2639   demand_empty_rest_of_line ();
2640 }
2641
2642 #endif /* OBJ_XCOFF */
2643 \f
2644 /* The .tc pseudo-op.  This is used when generating either XCOFF or
2645    ELF.  This takes two or more arguments.
2646
2647    When generating XCOFF output, the first argument is the name to
2648    give to this location in the toc; this will be a symbol with class
2649    TC.  The rest of the arguments are 4 byte values to actually put at
2650    this location in the TOC; often there is just one more argument, a
2651    relocateable symbol reference.
2652
2653    When not generating XCOFF output, the arguments are the same, but
2654    the first argument is simply ignored.  */
2655
2656 static void
2657 ppc_tc (ignore)
2658      int ignore;
2659 {
2660 #ifdef OBJ_XCOFF
2661
2662   /* Define the TOC symbol name.  */
2663   {
2664     char *name;
2665     char endc;
2666     symbolS *sym;
2667
2668     if (ppc_toc_csect == (symbolS *) NULL
2669         || ppc_toc_csect != ppc_current_csect)
2670       {
2671         as_bad (".tc not in .toc section");
2672         ignore_rest_of_line ();
2673         return;
2674       }
2675
2676     name = input_line_pointer;
2677     endc = get_symbol_end ();
2678
2679     sym = symbol_find_or_make (name);
2680
2681     *input_line_pointer = endc;
2682
2683     if (S_IS_DEFINED (sym))
2684       {
2685         symbolS *label;
2686
2687         label = ppc_current_csect->sy_tc.within;
2688         if (label->sy_tc.class != XMC_TC0)
2689           {
2690             as_warn (".tc with no label");
2691             ignore_rest_of_line ();
2692             return;
2693           }
2694
2695         S_SET_SEGMENT (label, S_GET_SEGMENT (sym));
2696         label->sy_frag = sym->sy_frag;
2697         S_SET_VALUE (label, S_GET_VALUE (sym));
2698
2699         while (! is_end_of_line[(unsigned char) *input_line_pointer])
2700           ++input_line_pointer;
2701
2702         return;
2703       }
2704
2705     S_SET_SEGMENT (sym, now_seg);
2706     sym->sy_frag = frag_now;
2707     S_SET_VALUE (sym, (valueT) frag_now_fix ());
2708     sym->sy_tc.class = XMC_TC;
2709     sym->sy_tc.output = 1;
2710
2711     ppc_frob_label (sym);
2712   }
2713
2714 #else /* ! defined (OBJ_XCOFF) */
2715
2716   /* Skip the TOC symbol name.  */
2717   while (is_part_of_name (*input_line_pointer)
2718          || *input_line_pointer == '['
2719          || *input_line_pointer == ']'
2720          || *input_line_pointer == '{'
2721          || *input_line_pointer == '}')
2722     ++input_line_pointer;
2723
2724   /* Align to a four byte boundary.  */
2725   frag_align (2, 0);
2726   record_alignment (now_seg, 2);
2727
2728 #endif /* ! defined (OBJ_XCOFF) */
2729
2730   if (*input_line_pointer != ',')
2731     demand_empty_rest_of_line ();
2732   else
2733     {
2734       ++input_line_pointer;
2735       cons (4);
2736     }
2737 }
2738 \f
2739 #ifdef TE_PE
2740
2741 /* Pseudo-ops specific to the Windows NT PowerPC PE (coff) format */
2742
2743 /* Set the current section.  */
2744 static void
2745 ppc_set_current_section (new)
2746      segT new;
2747 {
2748   ppc_previous_section = ppc_current_section;
2749   ppc_current_section = new;
2750 }
2751
2752 /* pseudo-op: .previous
2753    behaviour: toggles the current section with the previous section.
2754    errors:    None
2755    warnings:  "No previous section"
2756 */
2757 static void
2758 ppc_previous(ignore)
2759      int ignore;
2760 {
2761   symbolS *tmp;
2762
2763   if (ppc_previous_section == NULL) 
2764     {
2765       as_warn("No previous section to return to. Directive ignored.");
2766       return;
2767     }
2768
2769   subseg_set(ppc_previous_section, 0);
2770
2771   ppc_set_current_section(ppc_previous_section);
2772 }
2773
2774 /* pseudo-op: .pdata
2775    behaviour: predefined read only data section
2776               double word aligned
2777    errors:    None
2778    warnings:  None
2779    initial:   .section .pdata "adr3"
2780               a - don't know -- maybe a misprint
2781               d - initialized data
2782               r - readable
2783               3 - double word aligned (that would be 4 byte boundary)
2784
2785    commentary:
2786    Tag index tables (also known as the function table) for exception
2787    handling, debugging, etc.
2788
2789 */
2790 static void
2791 ppc_pdata(ignore)
2792      int ignore;
2793 {
2794   if (pdata_section == 0) 
2795     {
2796       pdata_section = subseg_new (".pdata", 0);
2797       
2798       bfd_set_section_flags (stdoutput, pdata_section,
2799                              (SEC_ALLOC | SEC_LOAD | SEC_RELOC
2800                               | SEC_READONLY | SEC_DATA ));
2801       
2802       bfd_set_section_alignment (stdoutput, pdata_section, 3);
2803     }
2804   else
2805     {
2806       pdata_section = subseg_new(".pdata", 0);
2807     }
2808   ppc_set_current_section(pdata_section);
2809 }
2810
2811 /* pseudo-op: .ydata
2812    behaviour: predefined read only data section
2813               double word aligned
2814    errors:    None
2815    warnings:  None
2816    initial:   .section .ydata "drw3"
2817               a - don't know -- maybe a misprint
2818               d - initialized data
2819               r - readable
2820               3 - double word aligned (that would be 4 byte boundary)
2821    commentary:
2822    Tag tables (also known as the scope table) for exception handling,
2823    debugging, etc.
2824 */
2825 static void
2826 ppc_ydata(ignore)
2827      int ignore;
2828 {
2829   if (ydata_section == 0) 
2830     {
2831       ydata_section = subseg_new (".ydata", 0);
2832       bfd_set_section_flags (stdoutput, ydata_section,
2833                          (SEC_ALLOC | SEC_LOAD | SEC_RELOC
2834                                        | SEC_READONLY | SEC_DATA ));
2835
2836       bfd_set_section_alignment (stdoutput, ydata_section, 3);
2837     }
2838   else
2839     {
2840       ydata_section = subseg_new (".ydata", 0);
2841     }
2842   ppc_set_current_section(ydata_section);
2843 }
2844
2845 /* pseudo-op: .reldata
2846    behaviour: predefined read write data section
2847               double word aligned (4-byte)
2848               FIXME: relocation is applied to it
2849               FIXME: what's the difference between this and .data?
2850    errors:    None
2851    warnings:  None
2852    initial:   .section .reldata "drw3"
2853               d - initialized data
2854               r - readable
2855               w - writeable
2856               3 - double word aligned (that would be 8 byte boundary)
2857
2858    commentary:
2859    Like .data, but intended to hold data subject to relocation, such as
2860    function descriptors, etc.
2861 */
2862 static void
2863 ppc_reldata(ignore)
2864      int ignore;
2865 {
2866   if (reldata_section == 0)
2867     {
2868       reldata_section = subseg_new (".reldata", 0);
2869
2870       bfd_set_section_flags (stdoutput, reldata_section,
2871                              ( SEC_ALLOC | SEC_LOAD | SEC_RELOC 
2872                               | SEC_DATA ));
2873
2874       bfd_set_section_alignment (stdoutput, reldata_section, 3);
2875     }
2876   else
2877     {
2878       reldata_section = subseg_new (".reldata", 0);
2879     }
2880   ppc_set_current_section(reldata_section);
2881 }
2882
2883 /* pseudo-op: .rdata
2884    behaviour: predefined read only data section
2885               double word aligned
2886    errors:    None
2887    warnings:  None
2888    initial:   .section .rdata "dr3"
2889               d - initialized data
2890               r - readable
2891               3 - double word aligned (that would be 4 byte boundary)
2892 */
2893 static void
2894 ppc_rdata(ignore)
2895      int ignore;
2896 {
2897   if (rdata_section == 0)
2898     {
2899       rdata_section = subseg_new (".rdata", 0);
2900       bfd_set_section_flags (stdoutput, rdata_section,
2901                              (SEC_ALLOC | SEC_LOAD | SEC_RELOC
2902                               | SEC_READONLY | SEC_DATA ));
2903
2904       bfd_set_section_alignment (stdoutput, rdata_section, 2);
2905     }
2906   else
2907     {
2908       rdata_section = subseg_new (".rdata", 0);
2909     }
2910   ppc_set_current_section(rdata_section);
2911 }
2912
2913 /* pseudo-op: .ualong
2914    behaviour: much like .int, with the exception that no alignment is 
2915               performed.
2916               FIXME: test the alignment statement
2917    errors:    None
2918    warnings:  None
2919 */
2920 static void
2921 ppc_ualong(ignore)
2922      int ignore;
2923 {
2924   /* try for long */
2925   cons ( 4 );
2926 }
2927
2928 /* pseudo-op: .znop  <symbol name>
2929    behaviour: Issue a nop instruction
2930               Issue a IMAGE_REL_PPC_IFGLUE relocation against it, using
2931               the supplied symbol name.
2932    errors:    None
2933    warnings:  Missing symbol name
2934 */
2935 static void
2936 ppc_znop(ignore)
2937      int ignore;
2938 {
2939   unsigned long insn;
2940   const struct powerpc_opcode *opcode;
2941   expressionS ex;
2942   char *f;
2943
2944   symbolS *sym;
2945
2946   /* Strip out the symbol name */
2947   char *symbol_name;
2948   char c;
2949   char *name;
2950   unsigned int exp;
2951   flagword flags;
2952   asection *sec;
2953
2954   symbol_name = input_line_pointer;
2955   c = get_symbol_end ();
2956
2957   name = xmalloc (input_line_pointer - symbol_name + 1);
2958   strcpy (name, symbol_name);
2959
2960   sym = symbol_find_or_make (name);
2961
2962   *input_line_pointer = c;
2963
2964   SKIP_WHITESPACE ();
2965
2966   /* Look up the opcode in the hash table.  */
2967   opcode = (const struct powerpc_opcode *) hash_find (ppc_hash, "nop");
2968
2969   /* stick in the nop */
2970   insn = opcode->opcode;
2971
2972   /* Write out the instruction.  */
2973   f = frag_more (4);
2974   md_number_to_chars (f, insn, 4);
2975   fix_new (frag_now,
2976            f - frag_now->fr_literal,
2977            4,
2978            sym,
2979            0,
2980            0,
2981            BFD_RELOC_16_GOT_PCREL);
2982
2983 }
2984
2985 /* pseudo-op: 
2986    behaviour: 
2987    errors:    
2988    warnings:  
2989 */
2990 static void
2991 ppc_pe_comm(lcomm)
2992      int lcomm;
2993 {
2994   register char *name;
2995   register char c;
2996   register char *p;
2997   offsetT temp;
2998   register symbolS *symbolP;
2999   offsetT align;
3000
3001   name = input_line_pointer;
3002   c = get_symbol_end ();
3003
3004   /* just after name is now '\0' */
3005   p = input_line_pointer;
3006   *p = c;
3007   SKIP_WHITESPACE ();
3008   if (*input_line_pointer != ',')
3009     {
3010       as_bad ("Expected comma after symbol-name: rest of line ignored.");
3011       ignore_rest_of_line ();
3012       return;
3013     }
3014
3015   input_line_pointer++;         /* skip ',' */
3016   if ((temp = get_absolute_expression ()) < 0)
3017     {
3018       as_warn (".COMMon length (%ld.) <0! Ignored.", (long) temp);
3019       ignore_rest_of_line ();
3020       return;
3021     }
3022
3023   if (! lcomm)
3024     {
3025       /* The third argument to .comm is the alignment.  */
3026       if (*input_line_pointer != ',')
3027         align = 3;
3028       else
3029         {
3030           ++input_line_pointer;
3031           align = get_absolute_expression ();
3032           if (align <= 0)
3033             {
3034               as_warn ("ignoring bad alignment");
3035               align = 3;
3036             }
3037         }
3038     }
3039
3040   *p = 0;
3041   symbolP = symbol_find_or_make (name);
3042
3043   *p = c;
3044   if (S_IS_DEFINED (symbolP))
3045     {
3046       as_bad ("Ignoring attempt to re-define symbol `%s'.",
3047               S_GET_NAME (symbolP));
3048       ignore_rest_of_line ();
3049       return;
3050     }
3051
3052   if (S_GET_VALUE (symbolP))
3053     {
3054       if (S_GET_VALUE (symbolP) != (valueT) temp)
3055         as_bad ("Length of .comm \"%s\" is already %ld. Not changed to %ld.",
3056                 S_GET_NAME (symbolP),
3057                 (long) S_GET_VALUE (symbolP),
3058                 (long) temp);
3059     }
3060   else
3061     {
3062       S_SET_VALUE (symbolP, (valueT) temp);
3063       S_SET_EXTERNAL (symbolP);
3064     }
3065
3066   demand_empty_rest_of_line ();
3067 }
3068
3069 /*
3070  * implement the .section pseudo op:
3071  *      .section name {, "flags"}
3072  *                ^         ^
3073  *                |         +--- optional flags: 'b' for bss
3074  *                |                              'i' for info
3075  *                +-- section name               'l' for lib
3076  *                                               'n' for noload
3077  *                                               'o' for over
3078  *                                               'w' for data
3079  *                                               'd' (apparently m88k for data)
3080  *                                               'x' for text
3081  * But if the argument is not a quoted string, treat it as a
3082  * subsegment number.
3083  *
3084  * FIXME: this is a copy of the section processing from obj-coff.c, with
3085  * additions/changes for the moto-pas assembler support. There are three
3086  * categories:
3087  *
3088  * FIXME: I just noticed this. This doesn't work at all really. It it 
3089  *        setting bits that bfd probably neither understands or uses. The
3090  *        correct approach (?) will have to incorporate extra fields attached
3091  *        to the section to hold the system specific stuff. (krk)
3092  *
3093  * Section Contents:
3094  * 'a' - unknown - referred to in documentation, but no definition supplied
3095  * 'c' - section has code
3096  * 'd' - section has initialized data
3097  * 'u' - section has uninitialized data
3098  * 'i' - section contains directives (info)
3099  * 'n' - section can be discarded
3100  * 'R' - remove section at link time
3101  *
3102  * Section Protection:
3103  * 'r' - section is readable
3104  * 'w' - section is writeable
3105  * 'x' - section is executable
3106  * 's' - section is sharable
3107  *
3108  * Section Alignment:
3109  * '0' - align to byte boundary
3110  * '1' - align to halfword undary
3111  * '2' - align to word boundary
3112  * '3' - align to doubleword boundary
3113  * '4' - align to quadword boundary
3114  * '5' - align to 32 byte boundary
3115  * '6' - align to 64 byte boundary
3116  *
3117  */
3118
3119 void
3120 ppc_pe_section (ignore)
3121      int ignore;
3122 {
3123   /* Strip out the section name */
3124   char *section_name;
3125   char c;
3126   char *name;
3127   unsigned int exp;
3128   flagword flags;
3129   segT sec;
3130   int align;
3131
3132   align = 4; /* default alignment to 16 byte boundary */
3133
3134   section_name = input_line_pointer;
3135   c = get_symbol_end ();
3136
3137   name = xmalloc (input_line_pointer - section_name + 1);
3138   strcpy (name, section_name);
3139
3140   *input_line_pointer = c;
3141
3142   SKIP_WHITESPACE ();
3143
3144   exp = 0;
3145   flags = SEC_NO_FLAGS;
3146
3147   if (*input_line_pointer == ',')
3148     {
3149       ++input_line_pointer;
3150       SKIP_WHITESPACE ();
3151       if (*input_line_pointer != '"')
3152         exp = get_absolute_expression ();
3153       else
3154         {
3155           ++input_line_pointer;
3156           while (*input_line_pointer != '"'
3157                  && ! is_end_of_line[(unsigned char) *input_line_pointer])
3158             {
3159               switch (*input_line_pointer)
3160                 {
3161                   /* Section Contents */
3162                 case 'a': /* unknown */
3163                   as_warn ("Unsupported section attribute -- 'a'");
3164                   break;
3165                 case 'c': /* code section */
3166                   flags |= SEC_CODE; 
3167                   break;
3168                 case 'd': /* section has initialized data */
3169                   flags |= SEC_DATA;
3170                   break;
3171                 case 'u': /* section has uninitialized data */
3172                   /* FIXME: This is IMAGE_SCN_CNT_UNINITIALIZED_DATA
3173                      in winnt.h */
3174                   flags |= SEC_ROM;
3175                   break;
3176                 case 'i': /* section contains directives (info) */
3177                   /* FIXME: This is IMAGE_SCN_LNK_INFO
3178                      in winnt.h */
3179                   flags |= SEC_HAS_CONTENTS;
3180                   break;
3181                 case 'n': /* section can be discarded */
3182                   flags &=~ SEC_LOAD; 
3183                   break;
3184                 case 'R': /* Remove section at link time */
3185                   flags |= SEC_NEVER_LOAD;
3186                   break;
3187
3188                   /* Section Protection */
3189                 case 'r': /* section is readable */
3190                   flags |= IMAGE_SCN_MEM_READ;
3191                   break;
3192                 case 'w': /* section is writeable */
3193                   flags |= IMAGE_SCN_MEM_WRITE;
3194                   break;
3195                 case 'x': /* section is executable */
3196                   flags |= IMAGE_SCN_MEM_EXECUTE;
3197                   break;
3198                 case 's': /* section is sharable */
3199                   flags |= IMAGE_SCN_MEM_SHARED;
3200                   break;
3201
3202                   /* Section Alignment */
3203                 case '0': /* align to byte boundary */
3204                   flags |= IMAGE_SCN_ALIGN_1BYTES;
3205                   align = 0;
3206                   break;
3207                 case '1':  /* align to halfword boundary */
3208                   flags |= IMAGE_SCN_ALIGN_2BYTES;
3209                   align = 1;
3210                   break;
3211                 case '2':  /* align to word boundary */
3212                   flags |= IMAGE_SCN_ALIGN_4BYTES;
3213                   align = 2;
3214                   break;
3215                 case '3':  /* align to doubleword boundary */
3216                   flags |= IMAGE_SCN_ALIGN_8BYTES;
3217                   align = 3;
3218                   break;
3219                 case '4':  /* align to quadword boundary */
3220                   flags |= IMAGE_SCN_ALIGN_16BYTES;
3221                   align = 4;
3222                   break;
3223                 case '5':  /* align to 32 byte boundary */
3224                   flags |= IMAGE_SCN_ALIGN_32BYTES;
3225                   align = 5;
3226                   break;
3227                 case '6':  /* align to 64 byte boundary */
3228                   flags |= IMAGE_SCN_ALIGN_64BYTES;
3229                   align = 6;
3230                   break;
3231
3232                 default:
3233                   as_warn("unknown section attribute '%c'",
3234                           *input_line_pointer);
3235                   break;
3236                 }
3237               ++input_line_pointer;
3238             }
3239           if (*input_line_pointer == '"')
3240             ++input_line_pointer;
3241         }
3242     }
3243
3244   sec = subseg_new (name, (subsegT) exp);
3245
3246   ppc_set_current_section(sec);
3247
3248   if (flags != SEC_NO_FLAGS)
3249     {
3250       if (! bfd_set_section_flags (stdoutput, sec, flags))
3251         as_warn ("error setting flags for \"%s\": %s",
3252                  bfd_section_name (stdoutput, sec),
3253                  bfd_errmsg (bfd_get_error ()));
3254     }
3255
3256   bfd_set_section_alignment(stdoutput, sec, align);
3257
3258 }
3259
3260 static void
3261 ppc_pe_function (ignore)
3262      int ignore;
3263 {
3264   char *name;
3265   char endc;
3266   symbolS *ext_sym;
3267
3268   name = input_line_pointer;
3269   endc = get_symbol_end ();
3270
3271   ext_sym = symbol_find_or_make (name);
3272
3273   *input_line_pointer = endc;
3274
3275   S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
3276   SF_SET_FUNCTION (ext_sym);
3277   SF_SET_PROCESS (ext_sym);
3278   coff_add_linesym (ext_sym);
3279
3280   demand_empty_rest_of_line ();
3281 }
3282
3283 static void
3284 ppc_pe_tocd (ignore)
3285      int ignore;
3286 {
3287   if (tocdata_section == 0)
3288     {
3289       tocdata_section = subseg_new (".tocd", 0);
3290       /* FIXME: section flags won't work */
3291       bfd_set_section_flags (stdoutput, tocdata_section,
3292                              (SEC_ALLOC | SEC_LOAD | SEC_RELOC
3293                               | SEC_READONLY | SEC_DATA ));
3294
3295       bfd_set_section_alignment (stdoutput, tocdata_section, 2);
3296     }
3297   else
3298     {
3299       rdata_section = subseg_new (".tocd", 0);
3300     }
3301
3302   ppc_set_current_section(tocdata_section);
3303
3304   demand_empty_rest_of_line ();
3305 }
3306
3307 /* Don't adjust TOC relocs to use the section symbol.  */
3308
3309 int
3310 ppc_pe_fix_adjustable (fix)
3311      fixS *fix;
3312 {
3313   return fix->fx_r_type != BFD_RELOC_PPC_TOC16;
3314 }
3315
3316 #endif
3317 \f
3318 #ifdef OBJ_XCOFF
3319
3320 /* XCOFF specific symbol and file handling.  */
3321
3322 /* Canonicalize the symbol name.  We use the to force the suffix, if
3323    any, to use square brackets, and to be in upper case.  */
3324
3325 char *
3326 ppc_canonicalize_symbol_name (name)
3327      char *name;
3328 {
3329   char *s;
3330
3331   if (ppc_stab_symbol)
3332     return name;
3333
3334   for (s = name; *s != '\0' && *s != '{' && *s != '['; s++)
3335     ;
3336   if (*s != '\0')
3337     {
3338       char brac;
3339
3340       if (*s == '[')
3341         brac = ']';
3342       else
3343         {
3344           *s = '[';
3345           brac = '}';
3346         }
3347
3348       for (s++; *s != '\0' && *s != brac; s++)
3349         if (islower (*s))
3350           *s = toupper (*s);
3351
3352       if (*s == '\0' || s[1] != '\0')
3353         as_bad ("bad symbol suffix");
3354
3355       *s = ']';
3356     }
3357
3358   return name;
3359 }
3360
3361 /* Set the class of a symbol based on the suffix, if any.  This is
3362    called whenever a new symbol is created.  */
3363
3364 void
3365 ppc_symbol_new_hook (sym)
3366      symbolS *sym;
3367 {
3368   const char *s;
3369
3370   sym->sy_tc.next = NULL;
3371   sym->sy_tc.output = 0;
3372   sym->sy_tc.class = -1;
3373   sym->sy_tc.real_name = NULL;
3374   sym->sy_tc.subseg = 0;
3375   sym->sy_tc.align = 0;
3376   sym->sy_tc.size = NULL;
3377   sym->sy_tc.within = NULL;
3378
3379   if (ppc_stab_symbol)
3380     return;
3381
3382   s = strchr (S_GET_NAME (sym), '[');
3383   if (s == (const char *) NULL)
3384     {
3385       /* There is no suffix.  */
3386       return;
3387     }
3388
3389   ++s;
3390
3391   switch (s[0])
3392     {
3393     case 'B':
3394       if (strcmp (s, "BS]") == 0)
3395         sym->sy_tc.class = XMC_BS;
3396       break;
3397     case 'D':
3398       if (strcmp (s, "DB]") == 0)
3399         sym->sy_tc.class = XMC_DB;
3400       else if (strcmp (s, "DS]") == 0)
3401         sym->sy_tc.class = XMC_DS;
3402       break;
3403     case 'G':
3404       if (strcmp (s, "GL]") == 0)
3405         sym->sy_tc.class = XMC_GL;
3406       break;
3407     case 'P':
3408       if (strcmp (s, "PR]") == 0)
3409         sym->sy_tc.class = XMC_PR;
3410       break;
3411     case 'R':
3412       if (strcmp (s, "RO]") == 0)
3413         sym->sy_tc.class = XMC_RO;
3414       else if (strcmp (s, "RW]") == 0)
3415         sym->sy_tc.class = XMC_RW;
3416       break;
3417     case 'S':
3418       if (strcmp (s, "SV]") == 0)
3419         sym->sy_tc.class = XMC_SV;
3420       break;
3421     case 'T':
3422       if (strcmp (s, "TC]") == 0)
3423         sym->sy_tc.class = XMC_TC;
3424       else if (strcmp (s, "TI]") == 0)
3425         sym->sy_tc.class = XMC_TI;
3426       else if (strcmp (s, "TB]") == 0)
3427         sym->sy_tc.class = XMC_TB;
3428       else if (strcmp (s, "TC0]") == 0 || strcmp (s, "T0]") == 0)
3429         sym->sy_tc.class = XMC_TC0;
3430       break;
3431     case 'U':
3432       if (strcmp (s, "UA]") == 0)
3433         sym->sy_tc.class = XMC_UA;
3434       else if (strcmp (s, "UC]") == 0)
3435         sym->sy_tc.class = XMC_UC;
3436       break;
3437     case 'X':
3438       if (strcmp (s, "XO]") == 0)
3439         sym->sy_tc.class = XMC_XO;
3440       break;
3441     }
3442
3443   if (sym->sy_tc.class == -1)
3444     as_bad ("Unrecognized symbol suffix");
3445 }
3446
3447 /* Set the class of a label based on where it is defined.  This
3448    handles symbols without suffixes.  Also, move the symbol so that it
3449    follows the csect symbol.  */
3450
3451 void
3452 ppc_frob_label (sym)
3453      symbolS *sym;
3454 {
3455   if (ppc_current_csect != (symbolS *) NULL)
3456     {
3457       if (sym->sy_tc.class == -1)
3458         sym->sy_tc.class = ppc_current_csect->sy_tc.class;
3459
3460       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
3461       symbol_append (sym, ppc_current_csect->sy_tc.within, &symbol_rootP,
3462                      &symbol_lastP);
3463       ppc_current_csect->sy_tc.within = sym;
3464     }
3465 }
3466
3467 /* This variable is set by ppc_frob_symbol if any absolute symbols are
3468    seen.  It tells ppc_adjust_symtab whether it needs to look through
3469    the symbols.  */
3470
3471 static boolean ppc_saw_abs;
3472
3473 /* Change the name of a symbol just before writing it out.  Set the
3474    real name if the .rename pseudo-op was used.  Otherwise, remove any
3475    class suffix.  Return 1 if the symbol should not be included in the
3476    symbol table.  */
3477
3478 int
3479 ppc_frob_symbol (sym)
3480      symbolS *sym;
3481 {
3482   static symbolS *ppc_last_function;
3483   static symbolS *set_end;
3484
3485   /* Discard symbols that should not be included in the output symbol
3486      table.  */
3487   if (! sym->sy_used_in_reloc
3488       && ((sym->bsym->flags & BSF_SECTION_SYM) != 0
3489           || (! S_IS_EXTERNAL (sym)
3490               && ! sym->sy_tc.output
3491               && S_GET_STORAGE_CLASS (sym) != C_FILE)))
3492     return 1;
3493
3494   if (sym->sy_tc.real_name != (char *) NULL)
3495     S_SET_NAME (sym, sym->sy_tc.real_name);
3496   else
3497     {
3498       const char *name;
3499       const char *s;
3500
3501       name = S_GET_NAME (sym);
3502       s = strchr (name, '[');
3503       if (s != (char *) NULL)
3504         {
3505           unsigned int len;
3506           char *snew;
3507
3508           len = s - name;
3509           snew = xmalloc (len + 1);
3510           memcpy (snew, name, len);
3511           snew[len] = '\0';
3512
3513           S_SET_NAME (sym, snew);
3514         }
3515     }
3516
3517   if (set_end != (symbolS *) NULL)
3518     {
3519       SA_SET_SYM_ENDNDX (set_end, sym);
3520       set_end = NULL;
3521     }
3522
3523   if (SF_GET_FUNCTION (sym))
3524     {
3525       if (ppc_last_function != (symbolS *) NULL)
3526         as_warn ("two .function pseudo-ops with no intervening .ef");
3527       ppc_last_function = sym;
3528       if (sym->sy_tc.size != (symbolS *) NULL)
3529         {
3530           resolve_symbol_value (sym->sy_tc.size);
3531           SA_SET_SYM_FSIZE (sym, (long) S_GET_VALUE (sym->sy_tc.size));
3532         }
3533     }
3534   else if (S_GET_STORAGE_CLASS (sym) == C_FCN
3535            && strcmp (S_GET_NAME (sym), ".ef") == 0)
3536     {
3537       if (ppc_last_function == (symbolS *) NULL)
3538         as_warn (".ef with no preceding .function");
3539       else
3540         {
3541           set_end = ppc_last_function;
3542           ppc_last_function = NULL;
3543
3544           /* We don't have a C_EFCN symbol, but we need to force the
3545              COFF backend to believe that it has seen one.  */
3546           coff_last_function = NULL;
3547         }
3548     }
3549
3550   if (! S_IS_EXTERNAL (sym)
3551       && (sym->bsym->flags & BSF_SECTION_SYM) == 0
3552       && S_GET_STORAGE_CLASS (sym) != C_FILE
3553       && S_GET_STORAGE_CLASS (sym) != C_FCN
3554       && S_GET_STORAGE_CLASS (sym) != C_BLOCK
3555       && S_GET_STORAGE_CLASS (sym) != C_BSTAT
3556       && S_GET_STORAGE_CLASS (sym) != C_ESTAT
3557       && S_GET_STORAGE_CLASS (sym) != C_BINCL
3558       && S_GET_STORAGE_CLASS (sym) != C_EINCL
3559       && S_GET_SEGMENT (sym) != ppc_coff_debug_section)
3560     S_SET_STORAGE_CLASS (sym, C_HIDEXT);
3561
3562   if (S_GET_STORAGE_CLASS (sym) == C_EXT
3563       || S_GET_STORAGE_CLASS (sym) == C_HIDEXT)
3564     {
3565       int i;
3566       union internal_auxent *a;
3567
3568       /* Create a csect aux.  */
3569       i = S_GET_NUMBER_AUXILIARY (sym);
3570       S_SET_NUMBER_AUXILIARY (sym, i + 1);
3571       a = &coffsymbol (sym->bsym)->native[i + 1].u.auxent;
3572       if (sym->sy_tc.class == XMC_TC0)
3573         {
3574           /* This is the TOC table.  */
3575           know (strcmp (S_GET_NAME (sym), "TOC") == 0);
3576           a->x_csect.x_scnlen.l = 0;
3577           a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
3578         }
3579       else if (sym->sy_tc.subseg != 0)
3580         {
3581           /* This is a csect symbol.  x_scnlen is the size of the
3582              csect.  */
3583           if (sym->sy_tc.next == (symbolS *) NULL)
3584             a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
3585                                                        S_GET_SEGMENT (sym))
3586                                      - S_GET_VALUE (sym));
3587           else
3588             {
3589               resolve_symbol_value (sym->sy_tc.next);
3590               a->x_csect.x_scnlen.l = (S_GET_VALUE (sym->sy_tc.next)
3591                                        - S_GET_VALUE (sym));
3592             }
3593           a->x_csect.x_smtyp = (sym->sy_tc.align << 3) | XTY_SD;
3594         }
3595       else if (S_GET_SEGMENT (sym) == bss_section)
3596         {
3597           /* This is a common symbol.  */
3598           a->x_csect.x_scnlen.l = sym->sy_frag->fr_offset;
3599           a->x_csect.x_smtyp = (sym->sy_tc.align << 3) | XTY_CM;
3600           if (S_IS_EXTERNAL (sym))
3601             sym->sy_tc.class = XMC_RW;
3602           else
3603             sym->sy_tc.class = XMC_BS;
3604         }
3605       else if (S_GET_SEGMENT (sym) == absolute_section)
3606         {
3607           /* This is an absolute symbol.  The csect will be created by
3608              ppc_adjust_symtab.  */
3609           ppc_saw_abs = true;
3610           a->x_csect.x_smtyp = XTY_LD;
3611           if (sym->sy_tc.class == -1)
3612             sym->sy_tc.class = XMC_XO;
3613         }
3614       else if (! S_IS_DEFINED (sym))
3615         {
3616           /* This is an external symbol.  */
3617           a->x_csect.x_scnlen.l = 0;
3618           a->x_csect.x_smtyp = XTY_ER;
3619         }
3620       else if (sym->sy_tc.class == XMC_TC)
3621         {
3622           symbolS *next;
3623
3624           /* This is a TOC definition.  x_scnlen is the size of the
3625              TOC entry.  */
3626           next = symbol_next (sym);
3627           while (next->sy_tc.class == XMC_TC0)
3628             next = symbol_next (next);
3629           if (next == (symbolS *) NULL
3630               || next->sy_tc.class != XMC_TC)
3631             {
3632               if (ppc_after_toc_frag == (fragS *) NULL)
3633                 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
3634                                                            data_section)
3635                                          - S_GET_VALUE (sym));
3636               else
3637                 a->x_csect.x_scnlen.l = (ppc_after_toc_frag->fr_address
3638                                          - S_GET_VALUE (sym));
3639             }
3640           else
3641             {
3642               resolve_symbol_value (next);
3643               a->x_csect.x_scnlen.l = (S_GET_VALUE (next)
3644                                        - S_GET_VALUE (sym));
3645             }
3646           a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
3647         }
3648       else
3649         {
3650           symbolS *csect;
3651
3652           /* This is a normal symbol definition.  x_scnlen is the
3653              symbol index of the containing csect.  */
3654           if (S_GET_SEGMENT (sym) == text_section)
3655             csect = ppc_text_csects;
3656           else if (S_GET_SEGMENT (sym) == data_section)
3657             csect = ppc_data_csects;
3658           else
3659             abort ();
3660
3661           /* Skip the initial dummy symbol.  */
3662           csect = csect->sy_tc.next;
3663
3664           if (csect == (symbolS *) NULL)
3665             {
3666               as_warn ("warning: symbol %s has no csect", S_GET_NAME (sym));
3667               a->x_csect.x_scnlen.l = 0;
3668             }
3669           else
3670             {
3671               while (csect->sy_tc.next != (symbolS *) NULL)
3672                 {
3673                   resolve_symbol_value (csect->sy_tc.next);
3674                   if (S_GET_VALUE (csect->sy_tc.next) > S_GET_VALUE (sym))
3675                     break;
3676                   csect = csect->sy_tc.next;
3677                 }
3678
3679               a->x_csect.x_scnlen.p = coffsymbol (csect->bsym)->native;
3680               coffsymbol (sym->bsym)->native[i + 1].fix_scnlen = 1;
3681             }
3682           a->x_csect.x_smtyp = XTY_LD;
3683         }
3684         
3685       a->x_csect.x_parmhash = 0;
3686       a->x_csect.x_snhash = 0;
3687       if (sym->sy_tc.class == -1)
3688         a->x_csect.x_smclas = XMC_PR;
3689       else
3690         a->x_csect.x_smclas = sym->sy_tc.class;
3691       a->x_csect.x_stab = 0;
3692       a->x_csect.x_snstab = 0;
3693
3694       /* Don't let the COFF backend resort these symbols.  */
3695       sym->bsym->flags |= BSF_NOT_AT_END;
3696     }
3697   else if (S_GET_STORAGE_CLASS (sym) == C_BSTAT)
3698     {
3699       /* We want the value to be the symbol index of the referenced
3700          csect symbol.  BFD will do that for us if we set the right
3701          flags.  */
3702       S_SET_VALUE (sym,
3703                    (valueT) coffsymbol (sym->sy_tc.within->bsym)->native);
3704       coffsymbol (sym->bsym)->native->fix_value = 1;
3705     }
3706   else if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
3707     {
3708       symbolS *block;
3709       symbolS *csect;
3710
3711       /* The value is the offset from the enclosing csect.  */
3712       block = sym->sy_tc.within;
3713       csect = block->sy_tc.within;
3714       resolve_symbol_value (csect);
3715       S_SET_VALUE (sym, S_GET_VALUE (sym) - S_GET_VALUE (csect));
3716     }
3717   else if (S_GET_STORAGE_CLASS (sym) == C_BINCL
3718            || S_GET_STORAGE_CLASS (sym) == C_EINCL)
3719     {
3720       /* We want the value to be a file offset into the line numbers.
3721          BFD will do that for us if we set the right flags.  We have
3722          already set the value correctly.  */
3723       coffsymbol (sym->bsym)->native->fix_line = 1;
3724     }
3725
3726   return 0;
3727 }
3728
3729 /* Adjust the symbol table.  This creates csect symbols for all
3730    absolute symbols.  */
3731
3732 void
3733 ppc_adjust_symtab ()
3734 {
3735   symbolS *sym;
3736
3737   if (! ppc_saw_abs)
3738     return;
3739
3740   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
3741     {
3742       symbolS *csect;
3743       int i;
3744       union internal_auxent *a;
3745
3746       if (S_GET_SEGMENT (sym) != absolute_section)
3747         continue;
3748
3749       csect = symbol_create (".abs[XO]", absolute_section,
3750                              S_GET_VALUE (sym), &zero_address_frag);
3751       csect->bsym->value = S_GET_VALUE (sym);
3752       S_SET_STORAGE_CLASS (csect, C_HIDEXT);
3753       i = S_GET_NUMBER_AUXILIARY (csect);
3754       S_SET_NUMBER_AUXILIARY (csect, i + 1);
3755       a = &coffsymbol (csect->bsym)->native[i + 1].u.auxent;
3756       a->x_csect.x_scnlen.l = 0;
3757       a->x_csect.x_smtyp = XTY_SD;
3758       a->x_csect.x_parmhash = 0;
3759       a->x_csect.x_snhash = 0;
3760       a->x_csect.x_smclas = XMC_XO;
3761       a->x_csect.x_stab = 0;
3762       a->x_csect.x_snstab = 0;
3763
3764       symbol_insert (csect, sym, &symbol_rootP, &symbol_lastP);
3765
3766       i = S_GET_NUMBER_AUXILIARY (sym);
3767       a = &coffsymbol (sym->bsym)->native[i].u.auxent;
3768       a->x_csect.x_scnlen.p = coffsymbol (csect->bsym)->native;
3769       coffsymbol (sym->bsym)->native[i].fix_scnlen = 1;
3770     }
3771
3772   ppc_saw_abs = false;
3773 }
3774
3775 /* Set the VMA for a section.  This is called on all the sections in
3776    turn.  */
3777
3778 void
3779 ppc_frob_section (sec)
3780      asection *sec;
3781 {
3782   static bfd_size_type vma = 0;
3783
3784   bfd_set_section_vma (stdoutput, sec, vma);
3785   vma += bfd_section_size (stdoutput, sec);
3786 }
3787
3788 /* Adjust the file by adding a .debug section if needed.  */
3789
3790 void
3791 ppc_frob_file ()
3792 {
3793   if (ppc_debug_name_section_size > 0)
3794     {
3795       asection *sec;
3796
3797       sec = bfd_make_section (stdoutput, ".debug");
3798       if (sec == (asection *) NULL
3799           || ! bfd_set_section_size (stdoutput, sec,
3800                                      ppc_debug_name_section_size)
3801           || ! bfd_set_section_flags (stdoutput, sec,
3802                                       SEC_HAS_CONTENTS | SEC_LOAD))
3803         as_fatal ("can't make .debug section");
3804     }
3805 }
3806
3807 #endif /* OBJ_XCOFF */
3808 \f
3809 /* Turn a string in input_line_pointer into a floating point constant
3810    of type type, and store the appropriate bytes in *litp.  The number
3811    of LITTLENUMS emitted is stored in *sizep .  An error message is
3812    returned, or NULL on OK.  */
3813
3814 char *
3815 md_atof (type, litp, sizep)
3816      int type;
3817      char *litp;
3818      int *sizep;
3819 {
3820   int prec;
3821   LITTLENUM_TYPE words[4];
3822   char *t;
3823   int i;
3824
3825   switch (type)
3826     {
3827     case 'f':
3828       prec = 2;
3829       break;
3830
3831     case 'd':
3832       prec = 4;
3833       break;
3834
3835     default:
3836       *sizep = 0;
3837       return "bad call to md_atof";
3838     }
3839
3840   t = atof_ieee (input_line_pointer, type, words);
3841   if (t)
3842     input_line_pointer = t;
3843
3844   *sizep = prec * 2;
3845
3846   if (target_big_endian)
3847     {
3848       for (i = 0; i < prec; i++)
3849         {
3850           md_number_to_chars (litp, (valueT) words[i], 2);
3851           litp += 2;
3852         }
3853     }
3854   else
3855     {
3856       for (i = prec - 1; i >= 0; i--)
3857         {
3858           md_number_to_chars (litp, (valueT) words[i], 2);
3859           litp += 2;
3860         }
3861     }
3862      
3863   return NULL;
3864 }
3865
3866 /* Write a value out to the object file, using the appropriate
3867    endianness.  */
3868
3869 void
3870 md_number_to_chars (buf, val, n)
3871      char *buf;
3872      valueT val;
3873      int n;
3874 {
3875   if (target_big_endian)
3876     number_to_chars_bigendian (buf, val, n);
3877   else
3878     number_to_chars_littleendian (buf, val, n);
3879 }
3880
3881 /* Align a section (I don't know why this is machine dependent).  */
3882
3883 valueT
3884 md_section_align (seg, addr)
3885      asection *seg;
3886      valueT addr;
3887 {
3888   int align = bfd_get_section_alignment (stdoutput, seg);
3889
3890   return ((addr + (1 << align) - 1) & (-1 << align));
3891 }
3892
3893 /* We don't have any form of relaxing.  */
3894
3895 int
3896 md_estimate_size_before_relax (fragp, seg)
3897      fragS *fragp;
3898      asection *seg;
3899 {
3900   abort ();
3901   return 0;
3902 }
3903
3904 /* Convert a machine dependent frag.  We never generate these.  */
3905
3906 void
3907 md_convert_frag (abfd, sec, fragp)
3908      bfd *abfd;
3909      asection *sec;
3910      fragS *fragp;
3911 {
3912   abort ();
3913 }
3914
3915 /* We have no need to default values of symbols.  */
3916
3917 /*ARGSUSED*/
3918 symbolS *
3919 md_undefined_symbol (name)
3920      char *name;
3921 {
3922   return 0;
3923 }
3924 \f
3925 /* Functions concerning relocs.  */
3926
3927 /* The location from which a PC relative jump should be calculated,
3928    given a PC relative reloc.  */
3929
3930 long
3931 md_pcrel_from_section (fixp, sec)
3932      fixS *fixp;
3933      segT sec;
3934 {
3935 #ifdef OBJ_ELF
3936   if (fixp->fx_addsy != (symbolS *) NULL
3937       && (! S_IS_DEFINED (fixp->fx_addsy)
3938           || TC_FORCE_RELOCATION_SECTION (fixp, sec)))
3939     return 0;
3940 #endif
3941
3942   return fixp->fx_frag->fr_address + fixp->fx_where;
3943 }
3944
3945 #ifdef OBJ_XCOFF
3946
3947 /* This is called to see whether a fixup should be adjusted to use a
3948    section symbol.  We take the opportunity to change a fixup against
3949    a symbol in the TOC subsegment into a reloc against the
3950    corresponding .tc symbol.  */
3951
3952 int
3953 ppc_fix_adjustable (fix)
3954      fixS *fix;
3955 {
3956   valueT val;
3957
3958   resolve_symbol_value (fix->fx_addsy);
3959   val = S_GET_VALUE (fix->fx_addsy);
3960   if (ppc_toc_csect != (symbolS *) NULL
3961       && fix->fx_addsy != (symbolS *) NULL
3962       && fix->fx_addsy != ppc_toc_csect
3963       && S_GET_SEGMENT (fix->fx_addsy) == data_section
3964       && val >= ppc_toc_frag->fr_address
3965       && (ppc_after_toc_frag == (fragS *) NULL
3966           || val < ppc_after_toc_frag->fr_address))
3967     {
3968       symbolS *sy;
3969
3970       for (sy = symbol_next (ppc_toc_csect);
3971            sy != (symbolS *) NULL;
3972            sy = symbol_next (sy))
3973         {
3974           if (sy->sy_tc.class == XMC_TC0)
3975             continue;
3976           if (sy->sy_tc.class != XMC_TC)
3977             break;
3978           resolve_symbol_value (sy);
3979           if (val == S_GET_VALUE (sy))
3980             {
3981               fix->fx_addsy = sy;
3982               fix->fx_addnumber = val - ppc_toc_frag->fr_address;
3983               return 0;
3984             }
3985         }
3986
3987       as_bad_where (fix->fx_file, fix->fx_line,
3988                     "symbol in .toc does not match any .tc");
3989     }
3990
3991   /* Possibly adjust the reloc to be against the csect.  */
3992   if (fix->fx_addsy != (symbolS *) NULL
3993       && fix->fx_addsy->sy_tc.subseg == 0
3994       && fix->fx_addsy->sy_tc.class != XMC_TC0
3995       && fix->fx_addsy->sy_tc.class != XMC_TC
3996       && S_GET_SEGMENT (fix->fx_addsy) != bss_section)
3997     {
3998       symbolS *csect;
3999
4000       if (S_GET_SEGMENT (fix->fx_addsy) == text_section)
4001         csect = ppc_text_csects;
4002       else if (S_GET_SEGMENT (fix->fx_addsy) == data_section)
4003         csect = ppc_data_csects;
4004       else
4005         abort ();
4006
4007       /* Skip the initial dummy symbol.  */
4008       csect = csect->sy_tc.next;
4009
4010       if (csect != (symbolS *) NULL)
4011         {
4012           while (csect->sy_tc.next != (symbolS *) NULL
4013                  && (csect->sy_tc.next->sy_frag->fr_address
4014                      <= fix->fx_addsy->sy_frag->fr_address))
4015             csect = csect->sy_tc.next;
4016
4017           fix->fx_offset += (S_GET_VALUE (fix->fx_addsy)
4018                              - csect->sy_frag->fr_address);
4019           fix->fx_addsy = csect;
4020         }
4021     }
4022
4023   /* Adjust a reloc against a .lcomm symbol to be against the base
4024      .lcomm.  */
4025   if (fix->fx_addsy != (symbolS *) NULL
4026       && S_GET_SEGMENT (fix->fx_addsy) == bss_section
4027       && ! S_IS_EXTERNAL (fix->fx_addsy))
4028     {
4029       resolve_symbol_value (fix->fx_addsy->sy_frag->fr_symbol);
4030       fix->fx_offset += (S_GET_VALUE (fix->fx_addsy)
4031                          - S_GET_VALUE (fix->fx_addsy->sy_frag->fr_symbol));
4032       fix->fx_addsy = fix->fx_addsy->sy_frag->fr_symbol;
4033     }
4034
4035   return 0;
4036 }
4037
4038 #endif
4039
4040 /* See whether a symbol is in the TOC section.  */
4041
4042 static int
4043 ppc_is_toc_sym (sym)
4044      symbolS *sym;
4045 {
4046 #ifdef OBJ_XCOFF
4047   return sym->sy_tc.class == XMC_TC;
4048 #else
4049   return strcmp (segment_name (S_GET_SEGMENT (sym)), ".got") == 0;
4050 #endif
4051 }
4052
4053 /* Apply a fixup to the object code.  This is called for all the
4054    fixups we generated by the call to fix_new_exp, above.  In the call
4055    above we used a reloc code which was the largest legal reloc code
4056    plus the operand index.  Here we undo that to recover the operand
4057    index.  At this point all symbol values should be fully resolved,
4058    and we attempt to completely resolve the reloc.  If we can not do
4059    that, we determine the correct reloc code and put it back in the
4060    fixup.  */
4061
4062 int
4063 md_apply_fix3 (fixp, valuep, seg)
4064      fixS *fixp;
4065      valueT *valuep;
4066      segT seg;
4067 {
4068   valueT value;
4069
4070   /* FIXME FIXME FIXME: The value we are passed in *valuep includes
4071      the symbol values.  Since we are using BFD_ASSEMBLER, if we are
4072      doing this relocation the code in write.c is going to call
4073      bfd_perform_relocation, which is also going to use the symbol
4074      value.  That means that if the reloc is fully resolved we want to
4075      use *valuep since bfd_perform_relocation is not being used.
4076      However, if the reloc is not fully resolved we do not want to use
4077      *valuep, and must use fx_offset instead.  However, if the reloc
4078      is PC relative, we do want to use *valuep since it includes the
4079      result of md_pcrel_from.  This is confusing.  */
4080
4081   if (fixp->fx_addsy == (symbolS *) NULL)
4082     {
4083       value = *valuep;
4084       fixp->fx_done = 1;
4085     }
4086   else if (fixp->fx_pcrel)
4087     value = *valuep;
4088   else
4089     {
4090       value = fixp->fx_offset;
4091       if (fixp->fx_subsy != (symbolS *) NULL)
4092         {
4093           if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
4094             value -= S_GET_VALUE (fixp->fx_subsy);
4095           else
4096             {
4097               /* We can't actually support subtracting a symbol.  */
4098               as_bad_where (fixp->fx_file, fixp->fx_line,
4099                             "expression too complex");
4100             }
4101         }
4102     }
4103
4104   if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
4105     {
4106       int opindex;
4107       const struct powerpc_operand *operand;
4108       char *where;
4109       unsigned long insn;
4110
4111       opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
4112
4113       operand = &powerpc_operands[opindex];
4114
4115 #ifdef OBJ_XCOFF
4116       /* It appears that an instruction like
4117              l 9,LC..1(30)
4118          when LC..1 is not a TOC symbol does not generate a reloc.  It
4119          uses the offset of LC..1 within its csect.  However, .long
4120          LC..1 will generate a reloc.  I can't find any documentation
4121          on how these cases are to be distinguished, so this is a wild
4122          guess.  These cases are generated by gcc -mminimal-toc.  */
4123       if ((operand->flags & PPC_OPERAND_PARENS) != 0
4124           && operand->bits == 16
4125           && operand->shift == 0
4126           && operand->insert == NULL
4127           && fixp->fx_addsy != NULL
4128           && fixp->fx_addsy->sy_tc.subseg != 0
4129           && fixp->fx_addsy->sy_tc.class != XMC_TC
4130           && fixp->fx_addsy->sy_tc.class != XMC_TC0
4131           && S_GET_SEGMENT (fixp->fx_addsy) != bss_section)
4132         {
4133           value = fixp->fx_offset;
4134           fixp->fx_done = 1;
4135         }
4136 #endif
4137
4138       /* Fetch the instruction, insert the fully resolved operand
4139          value, and stuff the instruction back again.  */
4140       where = fixp->fx_frag->fr_literal + fixp->fx_where;
4141       if (target_big_endian)
4142         insn = bfd_getb32 ((unsigned char *) where);
4143       else
4144         insn = bfd_getl32 ((unsigned char *) where);
4145       insn = ppc_insert_operand (insn, operand, (offsetT) value,
4146                                  fixp->fx_file, fixp->fx_line);
4147       if (target_big_endian)
4148         bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
4149       else
4150         bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
4151
4152       if (fixp->fx_done)
4153         {
4154           /* Nothing else to do here.  */
4155           return 1;
4156         }
4157
4158       /* Determine a BFD reloc value based on the operand information.
4159          We are only prepared to turn a few of the operands into
4160          relocs.
4161          FIXME: We need to handle the DS field at the very least.
4162          FIXME: Selecting the reloc type is a bit haphazard; perhaps
4163          there should be a new field in the operand table.  */
4164       if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
4165           && operand->bits == 26
4166           && operand->shift == 0)
4167         fixp->fx_r_type = BFD_RELOC_PPC_B26;
4168       else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
4169           && operand->bits == 16
4170           && operand->shift == 0)
4171         fixp->fx_r_type = BFD_RELOC_PPC_B16;
4172       else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0
4173                && operand->bits == 26
4174                && operand->shift == 0)
4175         fixp->fx_r_type = BFD_RELOC_PPC_BA26;
4176       else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0
4177                && operand->bits == 16
4178                && operand->shift == 0)
4179         fixp->fx_r_type = BFD_RELOC_PPC_BA16;
4180       else if ((operand->flags & PPC_OPERAND_PARENS) != 0
4181                && operand->bits == 16
4182                && operand->shift == 0
4183                && operand->insert == NULL
4184                && fixp->fx_addsy != NULL
4185                && ppc_is_toc_sym (fixp->fx_addsy))
4186         {
4187           fixp->fx_size = 2;
4188           if (target_big_endian)
4189             fixp->fx_where += 2;
4190           fixp->fx_r_type = BFD_RELOC_PPC_TOC16;
4191         }
4192       else
4193         {
4194           as_bad_where (fixp->fx_file, fixp->fx_line,
4195                         "unresolved expression that must be resolved");
4196           fixp->fx_done = 1;
4197           return 1;
4198         }
4199     }
4200   else
4201     {
4202 #ifdef OBJ_ELF
4203       ppc_elf_validate_fix (fixp, seg);
4204 #endif
4205       switch (fixp->fx_r_type)
4206         {
4207         case BFD_RELOC_32:
4208         case BFD_RELOC_CTOR:
4209           if (fixp->fx_pcrel)
4210             {
4211               fixp->fx_r_type = BFD_RELOC_32_PCREL;
4212               value += fixp->fx_frag->fr_address + fixp->fx_where;
4213             }                   /* fall through */
4214
4215         case BFD_RELOC_32_PCREL:
4216           md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
4217                               value, 4);
4218           break;
4219
4220         case BFD_RELOC_LO16:
4221         case BFD_RELOC_HI16:
4222         case BFD_RELOC_HI16_S:
4223         case BFD_RELOC_PPC_TOC16:
4224         case BFD_RELOC_16:
4225         case BFD_RELOC_GPREL16:
4226         case BFD_RELOC_16_GOT_PCREL:
4227           if (fixp->fx_pcrel)
4228             abort ();
4229
4230           md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
4231                               value, 2);
4232           break;
4233
4234         case BFD_RELOC_8:
4235           if (fixp->fx_pcrel)
4236             abort ();
4237
4238           md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
4239                               value, 1);
4240           break;
4241
4242         default:
4243           abort ();
4244         }
4245     }
4246
4247 #ifdef OBJ_ELF
4248   fixp->fx_addnumber = value;
4249 #else
4250   if (fixp->fx_r_type != BFD_RELOC_PPC_TOC16)
4251     fixp->fx_addnumber = 0;
4252   else
4253     {
4254 #ifdef TE_PE
4255       fixp->fx_addnumber = 0;
4256 #else
4257       /* We want to use the offset within the data segment of the
4258          symbol, not the actual VMA of the symbol.  */
4259       fixp->fx_addnumber =
4260         - bfd_get_section_vma (stdoutput, S_GET_SEGMENT (fixp->fx_addsy));
4261 #endif
4262     }
4263 #endif
4264
4265   return 1;
4266 }
4267
4268 /* Generate a reloc for a fixup.  */
4269
4270 arelent *
4271 tc_gen_reloc (seg, fixp)
4272      asection *seg;
4273      fixS *fixp;
4274 {
4275   arelent *reloc;
4276
4277   reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
4278
4279   reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
4280   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
4281   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
4282   if (reloc->howto == (reloc_howto_type *) NULL)
4283     {
4284       as_bad_where (fixp->fx_file, fixp->fx_line,
4285                     "reloc %d not supported by object file format", (int)fixp->fx_r_type);
4286       return NULL;
4287     }
4288   reloc->addend = fixp->fx_addnumber;
4289
4290   return reloc;
4291 }