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