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