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