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