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