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