* config/tc-ppc.c (md_apply_fix): Fix xcoff build breakage from
[external/binutils.git] / gas / config / tc-ppc.c
1 /* tc-ppc.c -- Assemble for the PowerPC or POWER (RS/6000)
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4    Free Software Foundation, Inc.
5    Written by Ian Lance Taylor, Cygnus Support.
6
7    This file is part of GAS, the GNU Assembler.
8
9    GAS is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3, or (at your option)
12    any later version.
13
14    GAS is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with GAS; see the file COPYING.  If not, write to the Free
21    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22    02110-1301, USA.  */
23
24 #include "as.h"
25 #include "safe-ctype.h"
26 #include "subsegs.h"
27 #include "dw2gencfi.h"
28 #include "opcode/ppc.h"
29
30 #ifdef OBJ_ELF
31 #include "elf/ppc.h"
32 #include "dwarf2dbg.h"
33 #endif
34
35 #ifdef TE_PE
36 #include "coff/pe.h"
37 #endif
38
39 #ifdef OBJ_XCOFF
40 #include "coff/xcoff.h"
41 #include "libxcoff.h"
42 #endif
43
44 /* This is the assembler for the PowerPC or POWER (RS/6000) chips.  */
45
46 /* Tell the main code what the endianness is.  */
47 extern int target_big_endian;
48
49 /* Whether or not, we've set target_big_endian.  */
50 static int set_target_endian = 0;
51
52 /* Whether to use user friendly register names.  */
53 #ifndef TARGET_REG_NAMES_P
54 #ifdef TE_PE
55 #define TARGET_REG_NAMES_P TRUE
56 #else
57 #define TARGET_REG_NAMES_P FALSE
58 #endif
59 #endif
60
61 /* Macros for calculating LO, HI, HA, HIGHER, HIGHERA, HIGHEST,
62    HIGHESTA.  */
63
64 /* #lo(value) denotes the least significant 16 bits of the indicated.  */
65 #define PPC_LO(v) ((v) & 0xffff)
66
67 /* Split the indicated value with the msbs in bits 11-15
68    and the lsbs in bits 21-31.  */
69 #define PPC_VLE_SPLIT16A(v) ((v & 0xf800) << 11) | (v & 0x7ff)
70
71 /* Split the indicated value with the msbs in bits 6-10
72    and the lsbs in bits 21-31.  */
73 #define PPC_VLE_SPLIT16D(v) ((v & 0xf800) << 5) | (v & 0x7ff)
74
75 /* #lo(value) denotes the lsb 16 bits in split16a format.  */
76 #define PPC_VLE_LO16A(v) PPC_VLE_SPLIT16A(PPC_LO(v))
77
78 /* #lo(value) denotes the lsb 16 bits in split16d format.  */
79 #define PPC_VLE_LO16D(v) PPC_VLE_SPLIT16D(PPC_LO(v))
80
81 /* #hi(value) denotes bits 16 through 31 of the indicated value.  */
82 #define PPC_HI(v) (((v) >> 16) & 0xffff)
83
84 /* #lo(value) denotes the msb 16 bits in split16a format.  */
85 #define PPC_VLE_HI16A(v) PPC_VLE_SPLIT16A(PPC_HI(v))
86
87 /* #lo(value) denotes the msb 16 bits in split16d format.  */
88 #define PPC_VLE_HI16D(v) PPC_VLE_SPLIT16D(PPC_HI(v))
89
90 /* #ha(value) denotes the high adjusted value: bits 16 through 31 of
91   the indicated value, compensating for #lo() being treated as a
92   signed number.  */
93 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
94
95 /* #ha(value) denotes the high adjusted value in split16a format.  */
96 #define PPC_VLE_HA16A(v) PPC_VLE_SPLIT16A(PPC_HA(v))
97
98 /* #ha(value) denotes the high adjusted value in split16d format.  */
99 #define PPC_VLE_HA16D(v) PPC_VLE_SPLIT16D(PPC_HA(v))
100
101 /* #higher(value) denotes bits 32 through 47 of the indicated value.  */
102 #define PPC_HIGHER(v) (((v) >> 16 >> 16) & 0xffff)
103
104 /* #highera(value) denotes bits 32 through 47 of the indicated value,
105    compensating for #lo() being treated as a signed number.  */
106 #define PPC_HIGHERA(v) PPC_HIGHER ((v) + 0x8000)
107
108 /* #highest(value) denotes bits 48 through 63 of the indicated value.  */
109 #define PPC_HIGHEST(v) (((v) >> 24 >> 24) & 0xffff)
110
111 /* #highesta(value) denotes bits 48 through 63 of the indicated value,
112    compensating for #lo being treated as a signed number.  */
113 #define PPC_HIGHESTA(v) PPC_HIGHEST ((v) + 0x8000)
114
115 #define SEX16(val) ((((val) & 0xffff) ^ 0x8000) - 0x8000)
116
117 static bfd_boolean reg_names_p = TARGET_REG_NAMES_P;
118
119 static void ppc_macro (char *, const struct powerpc_macro *);
120 static void ppc_byte (int);
121
122 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
123 static void ppc_tc (int);
124 static void ppc_machine (int);
125 #endif
126
127 #ifdef OBJ_XCOFF
128 static void ppc_comm (int);
129 static void ppc_bb (int);
130 static void ppc_bc (int);
131 static void ppc_bf (int);
132 static void ppc_biei (int);
133 static void ppc_bs (int);
134 static void ppc_eb (int);
135 static void ppc_ec (int);
136 static void ppc_ef (int);
137 static void ppc_es (int);
138 static void ppc_csect (int);
139 static void ppc_dwsect (int);
140 static void ppc_change_csect (symbolS *, offsetT);
141 static void ppc_function (int);
142 static void ppc_extern (int);
143 static void ppc_lglobl (int);
144 static void ppc_ref (int);
145 static void ppc_section (int);
146 static void ppc_named_section (int);
147 static void ppc_stabx (int);
148 static void ppc_rename (int);
149 static void ppc_toc (int);
150 static void ppc_xcoff_cons (int);
151 static void ppc_vbyte (int);
152 #endif
153
154 #ifdef OBJ_ELF
155 static void ppc_elf_cons (int);
156 static void ppc_elf_rdata (int);
157 static void ppc_elf_lcomm (int);
158 #endif
159
160 #ifdef TE_PE
161 static void ppc_previous (int);
162 static void ppc_pdata (int);
163 static void ppc_ydata (int);
164 static void ppc_reldata (int);
165 static void ppc_rdata (int);
166 static void ppc_ualong (int);
167 static void ppc_znop (int);
168 static void ppc_pe_comm (int);
169 static void ppc_pe_section (int);
170 static void ppc_pe_function (int);
171 static void ppc_pe_tocd (int);
172 #endif
173 \f
174 /* Generic assembler global variables which must be defined by all
175    targets.  */
176
177 #ifdef OBJ_ELF
178 /* This string holds the chars that always start a comment.  If the
179    pre-processor is disabled, these aren't very useful.  The macro
180    tc_comment_chars points to this.  We use this, rather than the
181    usual comment_chars, so that we can switch for Solaris conventions.  */
182 static const char ppc_solaris_comment_chars[] = "#!";
183 static const char ppc_eabi_comment_chars[] = "#";
184
185 #ifdef TARGET_SOLARIS_COMMENT
186 const char *ppc_comment_chars = ppc_solaris_comment_chars;
187 #else
188 const char *ppc_comment_chars = ppc_eabi_comment_chars;
189 #endif
190 #else
191 const char comment_chars[] = "#";
192 #endif
193
194 /* Characters which start a comment at the beginning of a line.  */
195 const char line_comment_chars[] = "#";
196
197 /* Characters which may be used to separate multiple commands on a
198    single line.  */
199 const char line_separator_chars[] = ";";
200
201 /* Characters which are used to indicate an exponent in a floating
202    point number.  */
203 const char EXP_CHARS[] = "eE";
204
205 /* Characters which mean that a number is a floating point constant,
206    as in 0d1.0.  */
207 const char FLT_CHARS[] = "dD";
208
209 /* Anything that can start an operand needs to be mentioned here,
210    to stop the input scrubber eating whitespace.  */
211 const char ppc_symbol_chars[] = "%[";
212
213 /* The dwarf2 data alignment, adjusted for 32 or 64 bit.  */
214 int ppc_cie_data_alignment;
215
216 /* The dwarf2 minimum instruction length.  */
217 int ppc_dwarf2_line_min_insn_length;
218
219 /* More than this number of nops in an alignment op gets a branch
220    instead.  */
221 unsigned long nop_limit = 4;
222
223 /* The type of processor we are assembling for.  This is one or more
224    of the PPC_OPCODE flags defined in opcode/ppc.h.  */
225 ppc_cpu_t ppc_cpu = 0;
226
227 /* Flags set on encountering toc relocs.  */
228 enum {
229   has_large_toc_reloc = 1,
230   has_small_toc_reloc = 2
231 } toc_reloc_types;
232 \f
233 /* The target specific pseudo-ops which we support.  */
234
235 const pseudo_typeS md_pseudo_table[] =
236 {
237   /* Pseudo-ops which must be overridden.  */
238   { "byte",     ppc_byte,       0 },
239
240 #ifdef OBJ_XCOFF
241   /* Pseudo-ops specific to the RS/6000 XCOFF format.  Some of these
242      legitimately belong in the obj-*.c file.  However, XCOFF is based
243      on COFF, and is only implemented for the RS/6000.  We just use
244      obj-coff.c, and add what we need here.  */
245   { "comm",     ppc_comm,       0 },
246   { "lcomm",    ppc_comm,       1 },
247   { "bb",       ppc_bb,         0 },
248   { "bc",       ppc_bc,         0 },
249   { "bf",       ppc_bf,         0 },
250   { "bi",       ppc_biei,       0 },
251   { "bs",       ppc_bs,         0 },
252   { "csect",    ppc_csect,      0 },
253   { "dwsect",   ppc_dwsect,     0 },
254   { "data",     ppc_section,    'd' },
255   { "eb",       ppc_eb,         0 },
256   { "ec",       ppc_ec,         0 },
257   { "ef",       ppc_ef,         0 },
258   { "ei",       ppc_biei,       1 },
259   { "es",       ppc_es,         0 },
260   { "extern",   ppc_extern,     0 },
261   { "function", ppc_function,   0 },
262   { "lglobl",   ppc_lglobl,     0 },
263   { "ref",      ppc_ref,        0 },
264   { "rename",   ppc_rename,     0 },
265   { "section",  ppc_named_section, 0 },
266   { "stabx",    ppc_stabx,      0 },
267   { "text",     ppc_section,    't' },
268   { "toc",      ppc_toc,        0 },
269   { "long",     ppc_xcoff_cons, 2 },
270   { "llong",    ppc_xcoff_cons, 3 },
271   { "word",     ppc_xcoff_cons, 1 },
272   { "short",    ppc_xcoff_cons, 1 },
273   { "vbyte",    ppc_vbyte,      0 },
274 #endif
275
276 #ifdef OBJ_ELF
277   { "llong",    ppc_elf_cons,   8 },
278   { "quad",     ppc_elf_cons,   8 },
279   { "long",     ppc_elf_cons,   4 },
280   { "word",     ppc_elf_cons,   2 },
281   { "short",    ppc_elf_cons,   2 },
282   { "rdata",    ppc_elf_rdata,  0 },
283   { "rodata",   ppc_elf_rdata,  0 },
284   { "lcomm",    ppc_elf_lcomm,  0 },
285 #endif
286
287 #ifdef TE_PE
288   /* Pseudo-ops specific to the Windows NT PowerPC PE (coff) format.  */
289   { "previous", ppc_previous,   0 },
290   { "pdata",    ppc_pdata,      0 },
291   { "ydata",    ppc_ydata,      0 },
292   { "reldata",  ppc_reldata,    0 },
293   { "rdata",    ppc_rdata,      0 },
294   { "ualong",   ppc_ualong,     0 },
295   { "znop",     ppc_znop,       0 },
296   { "comm",     ppc_pe_comm,    0 },
297   { "lcomm",    ppc_pe_comm,    1 },
298   { "section",  ppc_pe_section, 0 },
299   { "function", ppc_pe_function,0 },
300   { "tocd",     ppc_pe_tocd,    0 },
301 #endif
302
303 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
304   { "tc",       ppc_tc,         0 },
305   { "machine",  ppc_machine,    0 },
306 #endif
307
308   { NULL,       NULL,           0 }
309 };
310
311 \f
312 /* Predefined register names if -mregnames (or default for Windows NT).
313    In general, there are lots of them, in an attempt to be compatible
314    with a number of other Windows NT assemblers.  */
315
316 /* Structure to hold information about predefined registers.  */
317 struct pd_reg
318   {
319     char *name;
320     int value;
321   };
322
323 /* List of registers that are pre-defined:
324
325    Each general register has predefined names of the form:
326    1. r<reg_num> which has the value <reg_num>.
327    2. r.<reg_num> which has the value <reg_num>.
328
329    Each floating point register has predefined names of the form:
330    1. f<reg_num> which has the value <reg_num>.
331    2. f.<reg_num> which has the value <reg_num>.
332
333    Each vector unit register has predefined names of the form:
334    1. v<reg_num> which has the value <reg_num>.
335    2. v.<reg_num> which has the value <reg_num>.
336
337    Each condition register has predefined names of the form:
338    1. cr<reg_num> which has the value <reg_num>.
339    2. cr.<reg_num> which has the value <reg_num>.
340
341    There are individual registers as well:
342    sp or r.sp     has the value 1
343    rtoc or r.toc  has the value 2
344    fpscr          has the value 0
345    xer            has the value 1
346    lr             has the value 8
347    ctr            has the value 9
348    pmr            has the value 0
349    dar            has the value 19
350    dsisr          has the value 18
351    dec            has the value 22
352    sdr1           has the value 25
353    srr0           has the value 26
354    srr1           has the value 27
355
356    The table is sorted. Suitable for searching by a binary search.  */
357
358 static const struct pd_reg pre_defined_registers[] =
359 {
360   { "cr.0", 0 },    /* Condition Registers */
361   { "cr.1", 1 },
362   { "cr.2", 2 },
363   { "cr.3", 3 },
364   { "cr.4", 4 },
365   { "cr.5", 5 },
366   { "cr.6", 6 },
367   { "cr.7", 7 },
368
369   { "cr0", 0 },
370   { "cr1", 1 },
371   { "cr2", 2 },
372   { "cr3", 3 },
373   { "cr4", 4 },
374   { "cr5", 5 },
375   { "cr6", 6 },
376   { "cr7", 7 },
377
378   { "ctr", 9 },
379
380   { "dar", 19 },    /* Data Access Register */
381   { "dec", 22 },    /* Decrementer */
382   { "dsisr", 18 },  /* Data Storage Interrupt Status Register */
383
384   { "f.0", 0 },     /* Floating point registers */
385   { "f.1", 1 },
386   { "f.10", 10 },
387   { "f.11", 11 },
388   { "f.12", 12 },
389   { "f.13", 13 },
390   { "f.14", 14 },
391   { "f.15", 15 },
392   { "f.16", 16 },
393   { "f.17", 17 },
394   { "f.18", 18 },
395   { "f.19", 19 },
396   { "f.2", 2 },
397   { "f.20", 20 },
398   { "f.21", 21 },
399   { "f.22", 22 },
400   { "f.23", 23 },
401   { "f.24", 24 },
402   { "f.25", 25 },
403   { "f.26", 26 },
404   { "f.27", 27 },
405   { "f.28", 28 },
406   { "f.29", 29 },
407   { "f.3", 3 },
408   { "f.30", 30 },
409   { "f.31", 31 },
410
411   { "f.32", 32 },    /* Extended floating point scalar registers (ISA 2.06).  */
412   { "f.33", 33 },
413   { "f.34", 34 },
414   { "f.35", 35 },
415   { "f.36", 36 },
416   { "f.37", 37 },
417   { "f.38", 38 },
418   { "f.39", 39 },
419   { "f.4", 4 },
420   { "f.40", 40 },
421   { "f.41", 41 },
422   { "f.42", 42 },
423   { "f.43", 43 },
424   { "f.44", 44 },
425   { "f.45", 45 },
426   { "f.46", 46 },
427   { "f.47", 47 },
428   { "f.48", 48 },
429   { "f.49", 49 },
430   { "f.5", 5 },
431   { "f.50", 50 },
432   { "f.51", 51 },
433   { "f.52", 52 },
434   { "f.53", 53 },
435   { "f.54", 54 },
436   { "f.55", 55 },
437   { "f.56", 56 },
438   { "f.57", 57 },
439   { "f.58", 58 },
440   { "f.59", 59 },
441   { "f.6", 6 },
442   { "f.60", 60 },
443   { "f.61", 61 },
444   { "f.62", 62 },
445   { "f.63", 63 },
446   { "f.7", 7 },
447   { "f.8", 8 },
448   { "f.9", 9 },
449
450   { "f0", 0 },
451   { "f1", 1 },
452   { "f10", 10 },
453   { "f11", 11 },
454   { "f12", 12 },
455   { "f13", 13 },
456   { "f14", 14 },
457   { "f15", 15 },
458   { "f16", 16 },
459   { "f17", 17 },
460   { "f18", 18 },
461   { "f19", 19 },
462   { "f2", 2 },
463   { "f20", 20 },
464   { "f21", 21 },
465   { "f22", 22 },
466   { "f23", 23 },
467   { "f24", 24 },
468   { "f25", 25 },
469   { "f26", 26 },
470   { "f27", 27 },
471   { "f28", 28 },
472   { "f29", 29 },
473   { "f3", 3 },
474   { "f30", 30 },
475   { "f31", 31 },
476
477   { "f32", 32 },    /* Extended floating point scalar registers (ISA 2.06).  */
478   { "f33", 33 },
479   { "f34", 34 },
480   { "f35", 35 },
481   { "f36", 36 },
482   { "f37", 37 },
483   { "f38", 38 },
484   { "f39", 39 },
485   { "f4", 4 },
486   { "f40", 40 },
487   { "f41", 41 },
488   { "f42", 42 },
489   { "f43", 43 },
490   { "f44", 44 },
491   { "f45", 45 },
492   { "f46", 46 },
493   { "f47", 47 },
494   { "f48", 48 },
495   { "f49", 49 },
496   { "f5", 5 },
497   { "f50", 50 },
498   { "f51", 51 },
499   { "f52", 52 },
500   { "f53", 53 },
501   { "f54", 54 },
502   { "f55", 55 },
503   { "f56", 56 },
504   { "f57", 57 },
505   { "f58", 58 },
506   { "f59", 59 },
507   { "f6", 6 },
508   { "f60", 60 },
509   { "f61", 61 },
510   { "f62", 62 },
511   { "f63", 63 },
512   { "f7", 7 },
513   { "f8", 8 },
514   { "f9", 9 },
515
516   { "fpscr", 0 },
517
518   /* Quantization registers used with pair single instructions.  */
519   { "gqr.0", 0 },
520   { "gqr.1", 1 },
521   { "gqr.2", 2 },
522   { "gqr.3", 3 },
523   { "gqr.4", 4 },
524   { "gqr.5", 5 },
525   { "gqr.6", 6 },
526   { "gqr.7", 7 },
527   { "gqr0", 0 },
528   { "gqr1", 1 },
529   { "gqr2", 2 },
530   { "gqr3", 3 },
531   { "gqr4", 4 },
532   { "gqr5", 5 },
533   { "gqr6", 6 },
534   { "gqr7", 7 },
535
536   { "lr", 8 },     /* Link Register */
537
538   { "pmr", 0 },
539
540   { "r.0", 0 },    /* General Purpose Registers */
541   { "r.1", 1 },
542   { "r.10", 10 },
543   { "r.11", 11 },
544   { "r.12", 12 },
545   { "r.13", 13 },
546   { "r.14", 14 },
547   { "r.15", 15 },
548   { "r.16", 16 },
549   { "r.17", 17 },
550   { "r.18", 18 },
551   { "r.19", 19 },
552   { "r.2", 2 },
553   { "r.20", 20 },
554   { "r.21", 21 },
555   { "r.22", 22 },
556   { "r.23", 23 },
557   { "r.24", 24 },
558   { "r.25", 25 },
559   { "r.26", 26 },
560   { "r.27", 27 },
561   { "r.28", 28 },
562   { "r.29", 29 },
563   { "r.3", 3 },
564   { "r.30", 30 },
565   { "r.31", 31 },
566   { "r.4", 4 },
567   { "r.5", 5 },
568   { "r.6", 6 },
569   { "r.7", 7 },
570   { "r.8", 8 },
571   { "r.9", 9 },
572
573   { "r.sp", 1 },   /* Stack Pointer */
574
575   { "r.toc", 2 },  /* Pointer to the table of contents */
576
577   { "r0", 0 },     /* More general purpose registers */
578   { "r1", 1 },
579   { "r10", 10 },
580   { "r11", 11 },
581   { "r12", 12 },
582   { "r13", 13 },
583   { "r14", 14 },
584   { "r15", 15 },
585   { "r16", 16 },
586   { "r17", 17 },
587   { "r18", 18 },
588   { "r19", 19 },
589   { "r2", 2 },
590   { "r20", 20 },
591   { "r21", 21 },
592   { "r22", 22 },
593   { "r23", 23 },
594   { "r24", 24 },
595   { "r25", 25 },
596   { "r26", 26 },
597   { "r27", 27 },
598   { "r28", 28 },
599   { "r29", 29 },
600   { "r3", 3 },
601   { "r30", 30 },
602   { "r31", 31 },
603   { "r4", 4 },
604   { "r5", 5 },
605   { "r6", 6 },
606   { "r7", 7 },
607   { "r8", 8 },
608   { "r9", 9 },
609
610   { "rtoc", 2 },  /* Table of contents */
611
612   { "sdr1", 25 }, /* Storage Description Register 1 */
613
614   { "sp", 1 },
615
616   { "srr0", 26 }, /* Machine Status Save/Restore Register 0 */
617   { "srr1", 27 }, /* Machine Status Save/Restore Register 1 */
618
619   { "v.0", 0 },     /* Vector (Altivec/VMX) registers */
620   { "v.1", 1 },
621   { "v.10", 10 },
622   { "v.11", 11 },
623   { "v.12", 12 },
624   { "v.13", 13 },
625   { "v.14", 14 },
626   { "v.15", 15 },
627   { "v.16", 16 },
628   { "v.17", 17 },
629   { "v.18", 18 },
630   { "v.19", 19 },
631   { "v.2", 2 },
632   { "v.20", 20 },
633   { "v.21", 21 },
634   { "v.22", 22 },
635   { "v.23", 23 },
636   { "v.24", 24 },
637   { "v.25", 25 },
638   { "v.26", 26 },
639   { "v.27", 27 },
640   { "v.28", 28 },
641   { "v.29", 29 },
642   { "v.3", 3 },
643   { "v.30", 30 },
644   { "v.31", 31 },
645   { "v.4", 4 },
646   { "v.5", 5 },
647   { "v.6", 6 },
648   { "v.7", 7 },
649   { "v.8", 8 },
650   { "v.9", 9 },
651
652   { "v0", 0 },
653   { "v1", 1 },
654   { "v10", 10 },
655   { "v11", 11 },
656   { "v12", 12 },
657   { "v13", 13 },
658   { "v14", 14 },
659   { "v15", 15 },
660   { "v16", 16 },
661   { "v17", 17 },
662   { "v18", 18 },
663   { "v19", 19 },
664   { "v2", 2 },
665   { "v20", 20 },
666   { "v21", 21 },
667   { "v22", 22 },
668   { "v23", 23 },
669   { "v24", 24 },
670   { "v25", 25 },
671   { "v26", 26 },
672   { "v27", 27 },
673   { "v28", 28 },
674   { "v29", 29 },
675   { "v3", 3 },
676   { "v30", 30 },
677   { "v31", 31 },
678   { "v4", 4 },
679   { "v5", 5 },
680   { "v6", 6 },
681   { "v7", 7 },
682   { "v8", 8 },
683   { "v9", 9 },
684
685   { "vs.0", 0 },     /* Vector Scalar (VSX) registers (ISA 2.06).  */
686   { "vs.1", 1 },
687   { "vs.10", 10 },
688   { "vs.11", 11 },
689   { "vs.12", 12 },
690   { "vs.13", 13 },
691   { "vs.14", 14 },
692   { "vs.15", 15 },
693   { "vs.16", 16 },
694   { "vs.17", 17 },
695   { "vs.18", 18 },
696   { "vs.19", 19 },
697   { "vs.2", 2 },
698   { "vs.20", 20 },
699   { "vs.21", 21 },
700   { "vs.22", 22 },
701   { "vs.23", 23 },
702   { "vs.24", 24 },
703   { "vs.25", 25 },
704   { "vs.26", 26 },
705   { "vs.27", 27 },
706   { "vs.28", 28 },
707   { "vs.29", 29 },
708   { "vs.3", 3 },
709   { "vs.30", 30 },
710   { "vs.31", 31 },
711   { "vs.32", 32 },
712   { "vs.33", 33 },
713   { "vs.34", 34 },
714   { "vs.35", 35 },
715   { "vs.36", 36 },
716   { "vs.37", 37 },
717   { "vs.38", 38 },
718   { "vs.39", 39 },
719   { "vs.4", 4 },
720   { "vs.40", 40 },
721   { "vs.41", 41 },
722   { "vs.42", 42 },
723   { "vs.43", 43 },
724   { "vs.44", 44 },
725   { "vs.45", 45 },
726   { "vs.46", 46 },
727   { "vs.47", 47 },
728   { "vs.48", 48 },
729   { "vs.49", 49 },
730   { "vs.5", 5 },
731   { "vs.50", 50 },
732   { "vs.51", 51 },
733   { "vs.52", 52 },
734   { "vs.53", 53 },
735   { "vs.54", 54 },
736   { "vs.55", 55 },
737   { "vs.56", 56 },
738   { "vs.57", 57 },
739   { "vs.58", 58 },
740   { "vs.59", 59 },
741   { "vs.6", 6 },
742   { "vs.60", 60 },
743   { "vs.61", 61 },
744   { "vs.62", 62 },
745   { "vs.63", 63 },
746   { "vs.7", 7 },
747   { "vs.8", 8 },
748   { "vs.9", 9 },
749
750   { "vs0", 0 },
751   { "vs1", 1 },
752   { "vs10", 10 },
753   { "vs11", 11 },
754   { "vs12", 12 },
755   { "vs13", 13 },
756   { "vs14", 14 },
757   { "vs15", 15 },
758   { "vs16", 16 },
759   { "vs17", 17 },
760   { "vs18", 18 },
761   { "vs19", 19 },
762   { "vs2", 2 },
763   { "vs20", 20 },
764   { "vs21", 21 },
765   { "vs22", 22 },
766   { "vs23", 23 },
767   { "vs24", 24 },
768   { "vs25", 25 },
769   { "vs26", 26 },
770   { "vs27", 27 },
771   { "vs28", 28 },
772   { "vs29", 29 },
773   { "vs3", 3 },
774   { "vs30", 30 },
775   { "vs31", 31 },
776   { "vs32", 32 },
777   { "vs33", 33 },
778   { "vs34", 34 },
779   { "vs35", 35 },
780   { "vs36", 36 },
781   { "vs37", 37 },
782   { "vs38", 38 },
783   { "vs39", 39 },
784   { "vs4", 4 },
785   { "vs40", 40 },
786   { "vs41", 41 },
787   { "vs42", 42 },
788   { "vs43", 43 },
789   { "vs44", 44 },
790   { "vs45", 45 },
791   { "vs46", 46 },
792   { "vs47", 47 },
793   { "vs48", 48 },
794   { "vs49", 49 },
795   { "vs5", 5 },
796   { "vs50", 50 },
797   { "vs51", 51 },
798   { "vs52", 52 },
799   { "vs53", 53 },
800   { "vs54", 54 },
801   { "vs55", 55 },
802   { "vs56", 56 },
803   { "vs57", 57 },
804   { "vs58", 58 },
805   { "vs59", 59 },
806   { "vs6", 6 },
807   { "vs60", 60 },
808   { "vs61", 61 },
809   { "vs62", 62 },
810   { "vs63", 63 },
811   { "vs7", 7 },
812   { "vs8", 8 },
813   { "vs9", 9 },
814
815   { "xer", 1 },
816
817 };
818
819 #define REG_NAME_CNT    (sizeof (pre_defined_registers) / sizeof (struct pd_reg))
820
821 /* Given NAME, find the register number associated with that name, return
822    the integer value associated with the given name or -1 on failure.  */
823
824 static int
825 reg_name_search (const struct pd_reg *regs, int regcount, const char *name)
826 {
827   int middle, low, high;
828   int cmp;
829
830   low = 0;
831   high = regcount - 1;
832
833   do
834     {
835       middle = (low + high) / 2;
836       cmp = strcasecmp (name, regs[middle].name);
837       if (cmp < 0)
838         high = middle - 1;
839       else if (cmp > 0)
840         low = middle + 1;
841       else
842         return regs[middle].value;
843     }
844   while (low <= high);
845
846   return -1;
847 }
848
849 /*
850  * Summary of register_name.
851  *
852  * in:  Input_line_pointer points to 1st char of operand.
853  *
854  * out: A expressionS.
855  *      The operand may have been a register: in this case, X_op == O_register,
856  *      X_add_number is set to the register number, and truth is returned.
857  *      Input_line_pointer->(next non-blank) char after operand, or is in its
858  *      original state.
859  */
860
861 static bfd_boolean
862 register_name (expressionS *expressionP)
863 {
864   int reg_number;
865   char *name;
866   char *start;
867   char c;
868
869   /* Find the spelling of the operand.  */
870   start = name = input_line_pointer;
871   if (name[0] == '%' && ISALPHA (name[1]))
872     name = ++input_line_pointer;
873
874   else if (!reg_names_p || !ISALPHA (name[0]))
875     return FALSE;
876
877   c = get_symbol_end ();
878   reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
879
880   /* Put back the delimiting char.  */
881   *input_line_pointer = c;
882
883   /* Look to see if it's in the register table.  */
884   if (reg_number >= 0)
885     {
886       expressionP->X_op = O_register;
887       expressionP->X_add_number = reg_number;
888
889       /* Make the rest nice.  */
890       expressionP->X_add_symbol = NULL;
891       expressionP->X_op_symbol = NULL;
892       return TRUE;
893     }
894
895   /* Reset the line as if we had not done anything.  */
896   input_line_pointer = start;
897   return FALSE;
898 }
899 \f
900 /* This function is called for each symbol seen in an expression.  It
901    handles the special parsing which PowerPC assemblers are supposed
902    to use for condition codes.  */
903
904 /* Whether to do the special parsing.  */
905 static bfd_boolean cr_operand;
906
907 /* Names to recognize in a condition code.  This table is sorted.  */
908 static const struct pd_reg cr_names[] =
909 {
910   { "cr0", 0 },
911   { "cr1", 1 },
912   { "cr2", 2 },
913   { "cr3", 3 },
914   { "cr4", 4 },
915   { "cr5", 5 },
916   { "cr6", 6 },
917   { "cr7", 7 },
918   { "eq", 2 },
919   { "gt", 1 },
920   { "lt", 0 },
921   { "so", 3 },
922   { "un", 3 }
923 };
924
925 /* Parsing function.  This returns non-zero if it recognized an
926    expression.  */
927
928 int
929 ppc_parse_name (const char *name, expressionS *exp)
930 {
931   int val;
932
933   if (! cr_operand)
934     return 0;
935
936   if (*name == '%')
937     ++name;
938   val = reg_name_search (cr_names, sizeof cr_names / sizeof cr_names[0],
939                          name);
940   if (val < 0)
941     return 0;
942
943   exp->X_op = O_constant;
944   exp->X_add_number = val;
945
946   return 1;
947 }
948 \f
949 /* Local variables.  */
950
951 /* Whether to target xcoff64/elf64.  */
952 static unsigned int ppc_obj64 = BFD_DEFAULT_TARGET_SIZE == 64;
953
954 /* Opcode hash table.  */
955 static struct hash_control *ppc_hash;
956
957 /* Macro hash table.  */
958 static struct hash_control *ppc_macro_hash;
959
960 #ifdef OBJ_ELF
961 /* What type of shared library support to use.  */
962 static enum { SHLIB_NONE, SHLIB_PIC, SHLIB_MRELOCATABLE } shlib = SHLIB_NONE;
963
964 /* Flags to set in the elf header.  */
965 static flagword ppc_flags = 0;
966
967 /* Whether this is Solaris or not.  */
968 #ifdef TARGET_SOLARIS_COMMENT
969 #define SOLARIS_P TRUE
970 #else
971 #define SOLARIS_P FALSE
972 #endif
973
974 static bfd_boolean msolaris = SOLARIS_P;
975 #endif
976
977 #ifdef OBJ_XCOFF
978
979 /* The RS/6000 assembler uses the .csect pseudo-op to generate code
980    using a bunch of different sections.  These assembler sections,
981    however, are all encompassed within the .text or .data sections of
982    the final output file.  We handle this by using different
983    subsegments within these main segments.  */
984
985 /* Next subsegment to allocate within the .text segment.  */
986 static subsegT ppc_text_subsegment = 2;
987
988 /* Linked list of csects in the text section.  */
989 static symbolS *ppc_text_csects;
990
991 /* Next subsegment to allocate within the .data segment.  */
992 static subsegT ppc_data_subsegment = 2;
993
994 /* Linked list of csects in the data section.  */
995 static symbolS *ppc_data_csects;
996
997 /* The current csect.  */
998 static symbolS *ppc_current_csect;
999
1000 /* The RS/6000 assembler uses a TOC which holds addresses of functions
1001    and variables.  Symbols are put in the TOC with the .tc pseudo-op.
1002    A special relocation is used when accessing TOC entries.  We handle
1003    the TOC as a subsegment within the .data segment.  We set it up if
1004    we see a .toc pseudo-op, and save the csect symbol here.  */
1005 static symbolS *ppc_toc_csect;
1006
1007 /* The first frag in the TOC subsegment.  */
1008 static fragS *ppc_toc_frag;
1009
1010 /* The first frag in the first subsegment after the TOC in the .data
1011    segment.  NULL if there are no subsegments after the TOC.  */
1012 static fragS *ppc_after_toc_frag;
1013
1014 /* The current static block.  */
1015 static symbolS *ppc_current_block;
1016
1017 /* The COFF debugging section; set by md_begin.  This is not the
1018    .debug section, but is instead the secret BFD section which will
1019    cause BFD to set the section number of a symbol to N_DEBUG.  */
1020 static asection *ppc_coff_debug_section;
1021
1022 /* Structure to set the length field of the dwarf sections.  */
1023 struct dw_subsection {
1024   /* Subsections are simply linked.  */
1025   struct dw_subsection *link;
1026
1027   /* The subsection number.  */
1028   subsegT subseg;
1029
1030   /* Expression to compute the length of the section.  */
1031   expressionS end_exp;
1032 };
1033
1034 static struct dw_section {
1035   /* Corresponding section.  */
1036   segT sect;
1037
1038   /* Simply linked list of subsections with a label.  */
1039   struct dw_subsection *list_subseg;
1040
1041   /* The anonymous subsection.  */
1042   struct dw_subsection *anon_subseg;
1043 } dw_sections[XCOFF_DWSECT_NBR_NAMES];
1044 #endif /* OBJ_XCOFF */
1045
1046 #ifdef TE_PE
1047
1048 /* Various sections that we need for PE coff support.  */
1049 static segT ydata_section;
1050 static segT pdata_section;
1051 static segT reldata_section;
1052 static segT rdata_section;
1053 static segT tocdata_section;
1054
1055 /* The current section and the previous section. See ppc_previous.  */
1056 static segT ppc_previous_section;
1057 static segT ppc_current_section;
1058
1059 #endif /* TE_PE */
1060
1061 #ifdef OBJ_ELF
1062 symbolS *GOT_symbol;            /* Pre-defined "_GLOBAL_OFFSET_TABLE" */
1063 #define PPC_APUINFO_ISEL        0x40
1064 #define PPC_APUINFO_PMR         0x41
1065 #define PPC_APUINFO_RFMCI       0x42
1066 #define PPC_APUINFO_CACHELCK    0x43
1067 #define PPC_APUINFO_SPE         0x100
1068 #define PPC_APUINFO_EFS         0x101
1069 #define PPC_APUINFO_BRLOCK      0x102
1070 #define PPC_APUINFO_VLE         0x104
1071
1072 /*
1073  * We keep a list of APUinfo
1074  */
1075 unsigned long *ppc_apuinfo_list;
1076 unsigned int ppc_apuinfo_num;
1077 unsigned int ppc_apuinfo_num_alloc;
1078 #endif /* OBJ_ELF */
1079 \f
1080 #ifdef OBJ_ELF
1081 const char *const md_shortopts = "b:l:usm:K:VQ:";
1082 #else
1083 const char *const md_shortopts = "um:";
1084 #endif
1085 #define OPTION_NOPS (OPTION_MD_BASE + 0)
1086 const struct option md_longopts[] = {
1087   {"nops", required_argument, NULL, OPTION_NOPS},
1088   {NULL, no_argument, NULL, 0}
1089 };
1090 const size_t md_longopts_size = sizeof (md_longopts);
1091
1092 int
1093 md_parse_option (int c, char *arg)
1094 {
1095   ppc_cpu_t new_cpu;
1096
1097   switch (c)
1098     {
1099     case 'u':
1100       /* -u means that any undefined symbols should be treated as
1101          external, which is the default for gas anyhow.  */
1102       break;
1103
1104 #ifdef OBJ_ELF
1105     case 'l':
1106       /* Solaris as takes -le (presumably for little endian).  For completeness
1107          sake, recognize -be also.  */
1108       if (strcmp (arg, "e") == 0)
1109         {
1110           target_big_endian = 0;
1111           set_target_endian = 1;
1112           if (ppc_cpu & PPC_OPCODE_VLE)
1113             as_bad (_("the use of -mvle requires big endian."));
1114         }
1115       else
1116         return 0;
1117
1118       break;
1119
1120     case 'b':
1121       if (strcmp (arg, "e") == 0)
1122         {
1123           target_big_endian = 1;
1124           set_target_endian = 1;
1125         }
1126       else
1127         return 0;
1128
1129       break;
1130
1131     case 'K':
1132       /* Recognize -K PIC.  */
1133       if (strcmp (arg, "PIC") == 0 || strcmp (arg, "pic") == 0)
1134         {
1135           shlib = SHLIB_PIC;
1136           ppc_flags |= EF_PPC_RELOCATABLE_LIB;
1137         }
1138       else
1139         return 0;
1140
1141       break;
1142 #endif
1143
1144       /* a64 and a32 determine whether to use XCOFF64 or XCOFF32.  */
1145     case 'a':
1146       if (strcmp (arg, "64") == 0)
1147         {
1148 #ifdef BFD64
1149           ppc_obj64 = 1;
1150           if (ppc_cpu & PPC_OPCODE_VLE)
1151             as_bad (_("the use of -mvle requires -a32."));
1152 #else
1153           as_fatal (_("%s unsupported"), "-a64");
1154 #endif
1155         }
1156       else if (strcmp (arg, "32") == 0)
1157         ppc_obj64 = 0;
1158       else
1159         return 0;
1160       break;
1161
1162     case 'm':
1163       new_cpu = ppc_parse_cpu (ppc_cpu, arg);
1164       if (new_cpu != 0)
1165         {
1166           ppc_cpu = new_cpu;
1167           if (strcmp (arg, "vle") == 0)
1168             {
1169               if (set_target_endian && target_big_endian == 0)
1170                 as_bad (_("the use of -mvle requires big endian."));
1171               if (ppc_obj64)
1172                 as_bad (_("the use of -mvle requires -a32."));
1173             }
1174         }
1175
1176       else if (strcmp (arg, "regnames") == 0)
1177         reg_names_p = TRUE;
1178
1179       else if (strcmp (arg, "no-regnames") == 0)
1180         reg_names_p = FALSE;
1181
1182 #ifdef OBJ_ELF
1183       /* -mrelocatable/-mrelocatable-lib -- warn about initializations
1184          that require relocation.  */
1185       else if (strcmp (arg, "relocatable") == 0)
1186         {
1187           shlib = SHLIB_MRELOCATABLE;
1188           ppc_flags |= EF_PPC_RELOCATABLE;
1189         }
1190
1191       else if (strcmp (arg, "relocatable-lib") == 0)
1192         {
1193           shlib = SHLIB_MRELOCATABLE;
1194           ppc_flags |= EF_PPC_RELOCATABLE_LIB;
1195         }
1196
1197       /* -memb, set embedded bit.  */
1198       else if (strcmp (arg, "emb") == 0)
1199         ppc_flags |= EF_PPC_EMB;
1200
1201       /* -mlittle/-mbig set the endianness.  */
1202       else if (strcmp (arg, "little") == 0
1203                || strcmp (arg, "little-endian") == 0)
1204         {
1205           target_big_endian = 0;
1206           set_target_endian = 1;
1207           if (ppc_cpu & PPC_OPCODE_VLE)
1208             as_bad (_("the use of -mvle requires big endian."));
1209         }
1210
1211       else if (strcmp (arg, "big") == 0 || strcmp (arg, "big-endian") == 0)
1212         {
1213           target_big_endian = 1;
1214           set_target_endian = 1;
1215         }
1216
1217       else if (strcmp (arg, "solaris") == 0)
1218         {
1219           msolaris = TRUE;
1220           ppc_comment_chars = ppc_solaris_comment_chars;
1221         }
1222
1223       else if (strcmp (arg, "no-solaris") == 0)
1224         {
1225           msolaris = FALSE;
1226           ppc_comment_chars = ppc_eabi_comment_chars;
1227         }
1228 #endif
1229       else
1230         {
1231           as_bad (_("invalid switch -m%s"), arg);
1232           return 0;
1233         }
1234       break;
1235
1236 #ifdef OBJ_ELF
1237       /* -V: SVR4 argument to print version ID.  */
1238     case 'V':
1239       print_version_id ();
1240       break;
1241
1242       /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
1243          should be emitted or not.  FIXME: Not implemented.  */
1244     case 'Q':
1245       break;
1246
1247       /* Solaris takes -s to specify that .stabs go in a .stabs section,
1248          rather than .stabs.excl, which is ignored by the linker.
1249          FIXME: Not implemented.  */
1250     case 's':
1251       if (arg)
1252         return 0;
1253
1254       break;
1255 #endif
1256
1257     case OPTION_NOPS:
1258       {
1259         char *end;
1260         nop_limit = strtoul (optarg, &end, 0);
1261         if (*end)
1262           as_bad (_("--nops needs a numeric argument"));
1263       }
1264       break;
1265
1266     default:
1267       return 0;
1268     }
1269
1270   return 1;
1271 }
1272
1273 void
1274 md_show_usage (FILE *stream)
1275 {
1276   fprintf (stream, _("\
1277 PowerPC options:\n\
1278 -a32                    generate ELF32/XCOFF32\n\
1279 -a64                    generate ELF64/XCOFF64\n\
1280 -u                      ignored\n\
1281 -mpwrx, -mpwr2          generate code for POWER/2 (RIOS2)\n\
1282 -mpwr                   generate code for POWER (RIOS1)\n\
1283 -m601                   generate code for PowerPC 601\n\
1284 -mppc, -mppc32, -m603, -m604\n\
1285                         generate code for PowerPC 603/604\n\
1286 -m403                   generate code for PowerPC 403\n\
1287 -m405                   generate code for PowerPC 405\n\
1288 -m440                   generate code for PowerPC 440\n\
1289 -m464                   generate code for PowerPC 464\n\
1290 -m476                   generate code for PowerPC 476\n\
1291 -m7400, -m7410, -m7450, -m7455\n\
1292                         generate code for PowerPC 7400/7410/7450/7455\n\
1293 -m750cl                 generate code for PowerPC 750cl\n"));
1294   fprintf (stream, _("\
1295 -mppc64, -m620          generate code for PowerPC 620/625/630\n\
1296 -mppc64bridge           generate code for PowerPC 64, including bridge insns\n\
1297 -mbooke                 generate code for 32-bit PowerPC BookE\n\
1298 -ma2                    generate code for A2 architecture\n\
1299 -mpower4, -mpwr4        generate code for Power4 architecture\n\
1300 -mpower5, -mpwr5, -mpwr5x\n\
1301                         generate code for Power5 architecture\n\
1302 -mpower6, -mpwr6        generate code for Power6 architecture\n\
1303 -mpower7, -mpwr7        generate code for Power7 architecture\n\
1304 -mcell                  generate code for Cell Broadband Engine architecture\n\
1305 -mcom                   generate code Power/PowerPC common instructions\n\
1306 -many                   generate code for any architecture (PWR/PWRX/PPC)\n"));
1307   fprintf (stream, _("\
1308 -maltivec               generate code for AltiVec\n\
1309 -mvsx                   generate code for Vector-Scalar (VSX) instructions\n\
1310 -me300                  generate code for PowerPC e300 family\n\
1311 -me500, -me500x2        generate code for Motorola e500 core complex\n\
1312 -me500mc,               generate code for Freescale e500mc core complex\n\
1313 -me500mc64,             generate code for Freescale e500mc64 core complex\n\
1314 -me5500,                generate code for Freescale e5500 core complex\n\
1315 -me6500,                generate code for Freescale e6500 core complex\n\
1316 -mspe                   generate code for Motorola SPE instructions\n\
1317 -mvle                   generate code for Freescale VLE instructions\n\
1318 -mtitan                 generate code for AppliedMicro Titan core complex\n\
1319 -mregnames              Allow symbolic names for registers\n\
1320 -mno-regnames           Do not allow symbolic names for registers\n"));
1321 #ifdef OBJ_ELF
1322   fprintf (stream, _("\
1323 -mrelocatable           support for GCC's -mrelocatble option\n\
1324 -mrelocatable-lib       support for GCC's -mrelocatble-lib option\n\
1325 -memb                   set PPC_EMB bit in ELF flags\n\
1326 -mlittle, -mlittle-endian, -le\n\
1327                         generate code for a little endian machine\n\
1328 -mbig, -mbig-endian, -be\n\
1329                         generate code for a big endian machine\n\
1330 -msolaris               generate code for Solaris\n\
1331 -mno-solaris            do not generate code for Solaris\n\
1332 -K PIC                  set EF_PPC_RELOCATABLE_LIB in ELF flags\n\
1333 -V                      print assembler version number\n\
1334 -Qy, -Qn                ignored\n"));
1335 #endif
1336   fprintf (stream, _("\
1337 -nops=count             when aligning, more than COUNT nops uses a branch\n"));
1338 }
1339 \f
1340 /* Set ppc_cpu if it is not already set.  */
1341
1342 static void
1343 ppc_set_cpu (void)
1344 {
1345   const char *default_os  = TARGET_OS;
1346   const char *default_cpu = TARGET_CPU;
1347
1348   if ((ppc_cpu & ~(ppc_cpu_t) PPC_OPCODE_ANY) == 0)
1349     {
1350       if (ppc_obj64)
1351         ppc_cpu |= PPC_OPCODE_PPC | PPC_OPCODE_64;
1352       else if (strncmp (default_os, "aix", 3) == 0
1353                && default_os[3] >= '4' && default_os[3] <= '9')
1354         ppc_cpu |= PPC_OPCODE_COMMON;
1355       else if (strncmp (default_os, "aix3", 4) == 0)
1356         ppc_cpu |= PPC_OPCODE_POWER;
1357       else if (strcmp (default_cpu, "rs6000") == 0)
1358         ppc_cpu |= PPC_OPCODE_POWER;
1359       else if (strncmp (default_cpu, "powerpc", 7) == 0)
1360         ppc_cpu |= PPC_OPCODE_PPC;
1361       else
1362         as_fatal (_("unknown default cpu = %s, os = %s"),
1363                   default_cpu, default_os);
1364     }
1365 }
1366
1367 /* Figure out the BFD architecture to use.  This function and ppc_mach
1368    are called well before md_begin, when the output file is opened.  */
1369
1370 enum bfd_architecture
1371 ppc_arch (void)
1372 {
1373   const char *default_cpu = TARGET_CPU;
1374   ppc_set_cpu ();
1375
1376   if ((ppc_cpu & PPC_OPCODE_PPC) != 0)
1377     return bfd_arch_powerpc;
1378   if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
1379     return bfd_arch_powerpc;
1380   if ((ppc_cpu & PPC_OPCODE_POWER) != 0)
1381     return bfd_arch_rs6000;
1382   if ((ppc_cpu & (PPC_OPCODE_COMMON | PPC_OPCODE_ANY)) != 0)
1383     {
1384       if (strcmp (default_cpu, "rs6000") == 0)
1385         return bfd_arch_rs6000;
1386       else if (strncmp (default_cpu, "powerpc", 7) == 0)
1387         return bfd_arch_powerpc;
1388     }
1389
1390   as_fatal (_("neither Power nor PowerPC opcodes were selected."));
1391   return bfd_arch_unknown;
1392 }
1393
1394 unsigned long
1395 ppc_mach (void)
1396 {
1397   if (ppc_obj64)
1398     return bfd_mach_ppc64;
1399   else if (ppc_arch () == bfd_arch_rs6000)
1400     return bfd_mach_rs6k;
1401   else if (ppc_cpu & PPC_OPCODE_TITAN)
1402     return bfd_mach_ppc_titan;
1403   else if (ppc_cpu & PPC_OPCODE_VLE)
1404     return bfd_mach_ppc_vle;
1405   else
1406     return bfd_mach_ppc;
1407 }
1408
1409 extern char*
1410 ppc_target_format (void)
1411 {
1412 #ifdef OBJ_COFF
1413 #ifdef TE_PE
1414   return target_big_endian ? "pe-powerpc" : "pe-powerpcle";
1415 #elif TE_POWERMAC
1416   return "xcoff-powermac";
1417 #else
1418 #  ifdef TE_AIX5
1419   return (ppc_obj64 ? "aix5coff64-rs6000" : "aixcoff-rs6000");
1420 #  else
1421   return (ppc_obj64 ? "aixcoff64-rs6000" : "aixcoff-rs6000");
1422 #  endif
1423 #endif
1424 #endif
1425 #ifdef OBJ_ELF
1426 # ifdef TE_FreeBSD
1427   return (ppc_obj64 ? "elf64-powerpc-freebsd" : "elf32-powerpc-freebsd");
1428 # elif defined (TE_VXWORKS)
1429   return "elf32-powerpc-vxworks";
1430 # else
1431   return (target_big_endian
1432           ? (ppc_obj64 ? "elf64-powerpc" : "elf32-powerpc")
1433           : (ppc_obj64 ? "elf64-powerpcle" : "elf32-powerpcle"));
1434 # endif
1435 #endif
1436 }
1437
1438 /* Validate one entry in powerpc_opcodes[] or vle_opcodes[].
1439    Return TRUE if there's a problem, otherwise FALSE.  */
1440
1441 static bfd_boolean
1442 insn_validate (const struct powerpc_opcode *op)
1443 {
1444   const unsigned char *o;
1445   unsigned long omask = op->mask;
1446
1447   /* The mask had better not trim off opcode bits.  */
1448   if ((op->opcode & omask) != op->opcode)
1449     {
1450       as_bad (_("mask trims opcode bits for %s"), op->name);
1451       return TRUE;
1452     }
1453
1454   /* The operands must not overlap the opcode or each other.  */
1455   for (o = op->operands; *o; ++o)
1456     {
1457       if (*o >= num_powerpc_operands)
1458         {
1459           as_bad (_("operand index error for %s"), op->name);
1460           return TRUE;
1461         }
1462       else
1463         {
1464           const struct powerpc_operand *operand = &powerpc_operands[*o];
1465           if (operand->shift != PPC_OPSHIFT_INV)
1466             {
1467               unsigned long mask;
1468
1469               if (operand->shift >= 0)
1470                 mask = operand->bitm << operand->shift;
1471               else
1472                 mask = operand->bitm >> -operand->shift;
1473               if (omask & mask)
1474                 {
1475                   as_bad (_("operand %d overlap in %s"),
1476                           (int) (o - op->operands), op->name);
1477                   return TRUE;
1478                 }
1479               omask |= mask;
1480             }
1481         }
1482     }
1483   return FALSE;
1484 }
1485
1486 /* Insert opcodes and macros into hash tables.  Called at startup and
1487    for .machine pseudo.  */
1488
1489 static void
1490 ppc_setup_opcodes (void)
1491 {
1492   const struct powerpc_opcode *op;
1493   const struct powerpc_opcode *op_end;
1494   const struct powerpc_macro *macro;
1495   const struct powerpc_macro *macro_end;
1496   bfd_boolean bad_insn = FALSE;
1497
1498   if (ppc_hash != NULL)
1499     hash_die (ppc_hash);
1500   if (ppc_macro_hash != NULL)
1501     hash_die (ppc_macro_hash);
1502
1503   /* Insert the opcodes into a hash table.  */
1504   ppc_hash = hash_new ();
1505
1506   if (ENABLE_CHECKING)
1507     {
1508       unsigned int i;
1509
1510       /* An index into powerpc_operands is stored in struct fix
1511          fx_pcrel_adjust which is 8 bits wide.  */
1512       gas_assert (num_powerpc_operands < 256);
1513
1514       /* Check operand masks.  Code here and in the disassembler assumes
1515          all the 1's in the mask are contiguous.  */
1516       for (i = 0; i < num_powerpc_operands; ++i)
1517         {
1518           unsigned long mask = powerpc_operands[i].bitm;
1519           unsigned long right_bit;
1520           unsigned int j;
1521
1522           right_bit = mask & -mask;
1523           mask += right_bit;
1524           right_bit = mask & -mask;
1525           if (mask != right_bit)
1526             {
1527               as_bad (_("powerpc_operands[%d].bitm invalid"), i);
1528               bad_insn = TRUE;
1529             }
1530           for (j = i + 1; j < num_powerpc_operands; ++j)
1531             if (memcmp (&powerpc_operands[i], &powerpc_operands[j],
1532                         sizeof (powerpc_operands[0])) == 0)
1533               {
1534                 as_bad (_("powerpc_operands[%d] duplicates powerpc_operands[%d]"),
1535                         j, i);
1536                 bad_insn = TRUE;
1537               }
1538         }
1539     }
1540
1541   op_end = powerpc_opcodes + powerpc_num_opcodes;
1542   for (op = powerpc_opcodes; op < op_end; op++)
1543     {
1544       if (ENABLE_CHECKING)
1545         {
1546           if (op != powerpc_opcodes)
1547             {
1548               int old_opcode = PPC_OP (op[-1].opcode);
1549               int new_opcode = PPC_OP (op[0].opcode);
1550
1551 #ifdef PRINT_OPCODE_TABLE
1552               printf ("%-14s\t#%04d\tmajor op: 0x%x\top: 0x%x\tmask: 0x%x\tflags: 0x%llx\n",
1553                       op->name, op - powerpc_opcodes, (unsigned int) new_opcode,
1554                       (unsigned int) op->opcode, (unsigned int) op->mask,
1555                       (unsigned long long) op->flags);
1556 #endif
1557
1558               /* The major opcodes had better be sorted.  Code in the
1559                  disassembler assumes the insns are sorted according to
1560                  major opcode.  */
1561               if (new_opcode < old_opcode)
1562                 {
1563                   as_bad (_("major opcode is not sorted for %s"),
1564                           op->name);
1565                   bad_insn = TRUE;
1566                 }
1567             }
1568           bad_insn |= insn_validate (op);
1569         }
1570
1571       if ((ppc_cpu & op->flags) != 0
1572           && !(ppc_cpu & op->deprecated))
1573         {
1574           const char *retval;
1575
1576           retval = hash_insert (ppc_hash, op->name, (void *) op);
1577           if (retval != NULL)
1578             {
1579               as_bad (_("duplicate instruction %s"),
1580                       op->name);
1581               bad_insn = TRUE;
1582             }
1583         }
1584     }
1585
1586   if ((ppc_cpu & PPC_OPCODE_ANY) != 0)
1587     for (op = powerpc_opcodes; op < op_end; op++)
1588       hash_insert (ppc_hash, op->name, (void *) op);
1589
1590   op_end = vle_opcodes + vle_num_opcodes;
1591   for (op = vle_opcodes; op < op_end; op++)
1592     {
1593       if (ENABLE_CHECKING)
1594         {
1595           if (op != vle_opcodes)
1596             {
1597               unsigned old_seg, new_seg;
1598
1599               old_seg = VLE_OP (op[-1].opcode, op[-1].mask);
1600               old_seg = VLE_OP_TO_SEG (old_seg);
1601               new_seg = VLE_OP (op[0].opcode, op[0].mask);
1602               new_seg = VLE_OP_TO_SEG (new_seg);
1603
1604 #ifdef PRINT_OPCODE_TABLE
1605               printf ("%-14s\t#%04d\tmajor op: 0x%x\top: 0x%x\tmask: 0x%x\tflags: 0x%llx\n",
1606                       op->name, op - powerpc_opcodes, (unsigned int) new_opcode,
1607                       (unsigned int) op->opcode, (unsigned int) op->mask,
1608                       (unsigned long long) op->flags);
1609 #endif
1610               /* The major opcodes had better be sorted.  Code in the
1611                  disassembler assumes the insns are sorted according to
1612                  major opcode.  */
1613               if (new_seg < old_seg)
1614                 {
1615                   as_bad (_("major opcode is not sorted for %s"),
1616                           op->name);
1617                   bad_insn = TRUE;
1618                 }
1619             }
1620
1621           bad_insn |= insn_validate (op);
1622         }
1623
1624       if ((ppc_cpu & op->flags) != 0
1625           && !(ppc_cpu & op->deprecated))
1626         {
1627           const char *retval;
1628
1629           retval = hash_insert (ppc_hash, op->name, (void *) op);
1630           if (retval != NULL)
1631             {
1632               as_bad (_("duplicate instruction %s"),
1633                       op->name);
1634               bad_insn = TRUE;
1635             }
1636         }
1637     }
1638
1639   if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
1640     for (op = vle_opcodes; op < op_end; op++)
1641       hash_insert (ppc_hash, op->name, (void *) op);
1642
1643   /* Insert the macros into a hash table.  */
1644   ppc_macro_hash = hash_new ();
1645
1646   macro_end = powerpc_macros + powerpc_num_macros;
1647   for (macro = powerpc_macros; macro < macro_end; macro++)
1648     {
1649       if ((macro->flags & ppc_cpu) != 0 || (ppc_cpu & PPC_OPCODE_ANY) != 0)
1650         {
1651           const char *retval;
1652
1653           retval = hash_insert (ppc_macro_hash, macro->name, (void *) macro);
1654           if (retval != (const char *) NULL)
1655             {
1656               as_bad (_("duplicate macro %s"), macro->name);
1657               bad_insn = TRUE;
1658             }
1659         }
1660     }
1661
1662   if (bad_insn)
1663     abort ();
1664 }
1665
1666 /* This function is called when the assembler starts up.  It is called
1667    after the options have been parsed and the output file has been
1668    opened.  */
1669
1670 void
1671 md_begin (void)
1672 {
1673   ppc_set_cpu ();
1674
1675   ppc_cie_data_alignment = ppc_obj64 ? -8 : -4;
1676   ppc_dwarf2_line_min_insn_length = (ppc_cpu & PPC_OPCODE_VLE) ? 2 : 4;
1677
1678 #ifdef OBJ_ELF
1679   /* Set the ELF flags if desired.  */
1680   if (ppc_flags && !msolaris)
1681     bfd_set_private_flags (stdoutput, ppc_flags);
1682 #endif
1683
1684   ppc_setup_opcodes ();
1685
1686   /* Tell the main code what the endianness is if it is not overridden
1687      by the user.  */
1688   if (!set_target_endian)
1689     {
1690       set_target_endian = 1;
1691       target_big_endian = PPC_BIG_ENDIAN;
1692     }
1693
1694 #ifdef OBJ_XCOFF
1695   ppc_coff_debug_section = coff_section_from_bfd_index (stdoutput, N_DEBUG);
1696
1697   /* Create dummy symbols to serve as initial csects.  This forces the
1698      text csects to precede the data csects.  These symbols will not
1699      be output.  */
1700   ppc_text_csects = symbol_make ("dummy\001");
1701   symbol_get_tc (ppc_text_csects)->within = ppc_text_csects;
1702   ppc_data_csects = symbol_make ("dummy\001");
1703   symbol_get_tc (ppc_data_csects)->within = ppc_data_csects;
1704 #endif
1705
1706 #ifdef TE_PE
1707
1708   ppc_current_section = text_section;
1709   ppc_previous_section = 0;
1710
1711 #endif
1712 }
1713
1714 void
1715 ppc_cleanup (void)
1716 {
1717 #ifdef OBJ_ELF
1718   if (ppc_apuinfo_list == NULL)
1719     return;
1720
1721   /* Ok, so write the section info out.  We have this layout:
1722
1723   byte  data            what
1724   ----  ----            ----
1725   0     8               length of "APUinfo\0"
1726   4     (n*4)           number of APU's (4 bytes each)
1727   8     2               note type 2
1728   12    "APUinfo\0"     name
1729   20    APU#1           first APU's info
1730   24    APU#2           second APU's info
1731   ...   ...
1732   */
1733   {
1734     char *p;
1735     asection *seg = now_seg;
1736     subsegT subseg = now_subseg;
1737     asection *apuinfo_secp = (asection *) NULL;
1738     unsigned int i;
1739
1740     /* Create the .PPC.EMB.apuinfo section.  */
1741     apuinfo_secp = subseg_new (".PPC.EMB.apuinfo", 0);
1742     bfd_set_section_flags (stdoutput,
1743                            apuinfo_secp,
1744                            SEC_HAS_CONTENTS | SEC_READONLY);
1745
1746     p = frag_more (4);
1747     md_number_to_chars (p, (valueT) 8, 4);
1748
1749     p = frag_more (4);
1750     md_number_to_chars (p, (valueT) ppc_apuinfo_num * 4, 4);
1751
1752     p = frag_more (4);
1753     md_number_to_chars (p, (valueT) 2, 4);
1754
1755     p = frag_more (8);
1756     strcpy (p, "APUinfo");
1757
1758     for (i = 0; i < ppc_apuinfo_num; i++)
1759       {
1760         p = frag_more (4);
1761         md_number_to_chars (p, (valueT) ppc_apuinfo_list[i], 4);
1762       }
1763
1764     frag_align (2, 0, 0);
1765
1766     /* We probably can't restore the current segment, for there likely
1767        isn't one yet...  */
1768     if (seg && subseg)
1769       subseg_set (seg, subseg);
1770   }
1771 #endif
1772 }
1773
1774 /* Insert an operand value into an instruction.  */
1775
1776 static unsigned long
1777 ppc_insert_operand (unsigned long insn,
1778                     const struct powerpc_operand *operand,
1779                     offsetT val,
1780                     ppc_cpu_t cpu,
1781                     char *file,
1782                     unsigned int line)
1783 {
1784   long min, max, right;
1785
1786   max = operand->bitm;
1787   right = max & -max;
1788   min = 0;
1789
1790   if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
1791     {
1792       if ((operand->flags & PPC_OPERAND_SIGNOPT) == 0)
1793         max = (max >> 1) & -right;
1794       min = ~max & -right;
1795     }
1796
1797   if ((operand->flags & PPC_OPERAND_PLUS1) != 0)
1798     max++;
1799
1800   if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
1801     {
1802       long tmp = min;
1803       min = -max;
1804       max = -tmp;
1805     }
1806
1807   if (min <= max)
1808     {
1809       /* Some people write constants with the sign extension done by
1810          hand but only up to 32 bits.  This shouldn't really be valid,
1811          but, to permit this code to assemble on a 64-bit host, we
1812          sign extend the 32-bit value to 64 bits if so doing makes the
1813          value valid.  */
1814       if (val > max
1815           && (offsetT) (val - 0x80000000 - 0x80000000) >= min
1816           && (offsetT) (val - 0x80000000 - 0x80000000) <= max
1817           && ((val - 0x80000000 - 0x80000000) & (right - 1)) == 0)
1818         val = val - 0x80000000 - 0x80000000;
1819
1820       /* Similarly, people write expressions like ~(1<<15), and expect
1821          this to be OK for a 32-bit unsigned value.  */
1822       else if (val < min
1823                && (offsetT) (val + 0x80000000 + 0x80000000) >= min
1824                && (offsetT) (val + 0x80000000 + 0x80000000) <= max
1825                && ((val + 0x80000000 + 0x80000000) & (right - 1)) == 0)
1826         val = val + 0x80000000 + 0x80000000;
1827
1828       else if (val < min
1829                || val > max
1830                || (val & (right - 1)) != 0)
1831         as_bad_value_out_of_range (_("operand"), val, min, max, file, line);
1832     }
1833
1834   if (operand->insert)
1835     {
1836       const char *errmsg;
1837
1838       errmsg = NULL;
1839       insn = (*operand->insert) (insn, (long) val, cpu, &errmsg);
1840       if (errmsg != (const char *) NULL)
1841         as_bad_where (file, line, "%s", errmsg);
1842     }
1843   else if (operand->shift >= 0)
1844     insn |= ((long) val & operand->bitm) << operand->shift;
1845   else
1846     insn |= ((long) val & operand->bitm) >> -operand->shift;
1847
1848   return insn;
1849 }
1850
1851 \f
1852 #ifdef OBJ_ELF
1853 /* Parse @got, etc. and return the desired relocation.  */
1854 static bfd_reloc_code_real_type
1855 ppc_elf_suffix (char **str_p, expressionS *exp_p)
1856 {
1857   struct map_bfd {
1858     char *string;
1859     unsigned int length : 8;
1860     unsigned int valid32 : 1;
1861     unsigned int valid64 : 1;
1862     unsigned int reloc;
1863   };
1864
1865   char ident[20];
1866   char *str = *str_p;
1867   char *str2;
1868   int ch;
1869   int len;
1870   const struct map_bfd *ptr;
1871
1872 #define MAP(str, reloc)   { str, sizeof (str) - 1, 1, 1, reloc }
1873 #define MAP32(str, reloc) { str, sizeof (str) - 1, 1, 0, reloc }
1874 #define MAP64(str, reloc) { str, sizeof (str) - 1, 0, 1, reloc }
1875
1876   static const struct map_bfd mapping[] = {
1877     MAP ("l",                   BFD_RELOC_LO16),
1878     MAP ("h",                   BFD_RELOC_HI16),
1879     MAP ("ha",                  BFD_RELOC_HI16_S),
1880     MAP ("brtaken",             BFD_RELOC_PPC_B16_BRTAKEN),
1881     MAP ("brntaken",            BFD_RELOC_PPC_B16_BRNTAKEN),
1882     MAP ("got",                 BFD_RELOC_16_GOTOFF),
1883     MAP ("got@l",               BFD_RELOC_LO16_GOTOFF),
1884     MAP ("got@h",               BFD_RELOC_HI16_GOTOFF),
1885     MAP ("got@ha",              BFD_RELOC_HI16_S_GOTOFF),
1886     MAP ("plt@l",               BFD_RELOC_LO16_PLTOFF),
1887     MAP ("plt@h",               BFD_RELOC_HI16_PLTOFF),
1888     MAP ("plt@ha",              BFD_RELOC_HI16_S_PLTOFF),
1889     MAP ("copy",                BFD_RELOC_PPC_COPY),
1890     MAP ("globdat",             BFD_RELOC_PPC_GLOB_DAT),
1891     MAP ("sectoff",             BFD_RELOC_16_BASEREL),
1892     MAP ("sectoff@l",           BFD_RELOC_LO16_BASEREL),
1893     MAP ("sectoff@h",           BFD_RELOC_HI16_BASEREL),
1894     MAP ("sectoff@ha",          BFD_RELOC_HI16_S_BASEREL),
1895     MAP ("tls",                 BFD_RELOC_PPC_TLS),
1896     MAP ("dtpmod",              BFD_RELOC_PPC_DTPMOD),
1897     MAP ("dtprel",              BFD_RELOC_PPC_DTPREL),
1898     MAP ("dtprel@l",            BFD_RELOC_PPC_DTPREL16_LO),
1899     MAP ("dtprel@h",            BFD_RELOC_PPC_DTPREL16_HI),
1900     MAP ("dtprel@ha",           BFD_RELOC_PPC_DTPREL16_HA),
1901     MAP ("tprel",               BFD_RELOC_PPC_TPREL),
1902     MAP ("tprel@l",             BFD_RELOC_PPC_TPREL16_LO),
1903     MAP ("tprel@h",             BFD_RELOC_PPC_TPREL16_HI),
1904     MAP ("tprel@ha",            BFD_RELOC_PPC_TPREL16_HA),
1905     MAP ("got@tlsgd",           BFD_RELOC_PPC_GOT_TLSGD16),
1906     MAP ("got@tlsgd@l",         BFD_RELOC_PPC_GOT_TLSGD16_LO),
1907     MAP ("got@tlsgd@h",         BFD_RELOC_PPC_GOT_TLSGD16_HI),
1908     MAP ("got@tlsgd@ha",        BFD_RELOC_PPC_GOT_TLSGD16_HA),
1909     MAP ("got@tlsld",           BFD_RELOC_PPC_GOT_TLSLD16),
1910     MAP ("got@tlsld@l",         BFD_RELOC_PPC_GOT_TLSLD16_LO),
1911     MAP ("got@tlsld@h",         BFD_RELOC_PPC_GOT_TLSLD16_HI),
1912     MAP ("got@tlsld@ha",        BFD_RELOC_PPC_GOT_TLSLD16_HA),
1913     MAP ("got@dtprel",          BFD_RELOC_PPC_GOT_DTPREL16),
1914     MAP ("got@dtprel@l",        BFD_RELOC_PPC_GOT_DTPREL16_LO),
1915     MAP ("got@dtprel@h",        BFD_RELOC_PPC_GOT_DTPREL16_HI),
1916     MAP ("got@dtprel@ha",       BFD_RELOC_PPC_GOT_DTPREL16_HA),
1917     MAP ("got@tprel",           BFD_RELOC_PPC_GOT_TPREL16),
1918     MAP ("got@tprel@l",         BFD_RELOC_PPC_GOT_TPREL16_LO),
1919     MAP ("got@tprel@h",         BFD_RELOC_PPC_GOT_TPREL16_HI),
1920     MAP ("got@tprel@ha",        BFD_RELOC_PPC_GOT_TPREL16_HA),
1921     MAP32 ("fixup",             BFD_RELOC_CTOR),
1922     MAP32 ("plt",               BFD_RELOC_24_PLT_PCREL),
1923     MAP32 ("pltrel24",          BFD_RELOC_24_PLT_PCREL),
1924     MAP32 ("local24pc",         BFD_RELOC_PPC_LOCAL24PC),
1925     MAP32 ("local",             BFD_RELOC_PPC_LOCAL24PC),
1926     MAP32 ("pltrel",            BFD_RELOC_32_PLT_PCREL),
1927     MAP32 ("sdarel",            BFD_RELOC_GPREL16),
1928     MAP32 ("sdarel@l",          BFD_RELOC_PPC_VLE_SDAREL_LO16A),
1929     MAP32 ("sdarel@h",          BFD_RELOC_PPC_VLE_SDAREL_HI16A),
1930     MAP32 ("sdarel@ha",         BFD_RELOC_PPC_VLE_SDAREL_HA16A),
1931     MAP32 ("naddr",             BFD_RELOC_PPC_EMB_NADDR32),
1932     MAP32 ("naddr16",           BFD_RELOC_PPC_EMB_NADDR16),
1933     MAP32 ("naddr@l",           BFD_RELOC_PPC_EMB_NADDR16_LO),
1934     MAP32 ("naddr@h",           BFD_RELOC_PPC_EMB_NADDR16_HI),
1935     MAP32 ("naddr@ha",          BFD_RELOC_PPC_EMB_NADDR16_HA),
1936     MAP32 ("sdai16",            BFD_RELOC_PPC_EMB_SDAI16),
1937     MAP32 ("sda2rel",           BFD_RELOC_PPC_EMB_SDA2REL),
1938     MAP32 ("sda2i16",           BFD_RELOC_PPC_EMB_SDA2I16),
1939     MAP32 ("sda21",             BFD_RELOC_PPC_EMB_SDA21),
1940     MAP32 ("sda21@l",           BFD_RELOC_PPC_VLE_SDA21_LO),
1941     MAP32 ("mrkref",            BFD_RELOC_PPC_EMB_MRKREF),
1942     MAP32 ("relsect",           BFD_RELOC_PPC_EMB_RELSEC16),
1943     MAP32 ("relsect@l",         BFD_RELOC_PPC_EMB_RELST_LO),
1944     MAP32 ("relsect@h",         BFD_RELOC_PPC_EMB_RELST_HI),
1945     MAP32 ("relsect@ha",        BFD_RELOC_PPC_EMB_RELST_HA),
1946     MAP32 ("bitfld",            BFD_RELOC_PPC_EMB_BIT_FLD),
1947     MAP32 ("relsda",            BFD_RELOC_PPC_EMB_RELSDA),
1948     MAP32 ("xgot",              BFD_RELOC_PPC_TOC16),
1949     MAP64 ("higher",            BFD_RELOC_PPC64_HIGHER),
1950     MAP64 ("highera",           BFD_RELOC_PPC64_HIGHER_S),
1951     MAP64 ("highest",           BFD_RELOC_PPC64_HIGHEST),
1952     MAP64 ("highesta",          BFD_RELOC_PPC64_HIGHEST_S),
1953     MAP64 ("tocbase",           BFD_RELOC_PPC64_TOC),
1954     MAP64 ("toc",               BFD_RELOC_PPC_TOC16),
1955     MAP64 ("toc@l",             BFD_RELOC_PPC64_TOC16_LO),
1956     MAP64 ("toc@h",             BFD_RELOC_PPC64_TOC16_HI),
1957     MAP64 ("toc@ha",            BFD_RELOC_PPC64_TOC16_HA),
1958     MAP64 ("dtprel@higher",     BFD_RELOC_PPC64_DTPREL16_HIGHER),
1959     MAP64 ("dtprel@highera",    BFD_RELOC_PPC64_DTPREL16_HIGHERA),
1960     MAP64 ("dtprel@highest",    BFD_RELOC_PPC64_DTPREL16_HIGHEST),
1961     MAP64 ("dtprel@highesta",   BFD_RELOC_PPC64_DTPREL16_HIGHESTA),
1962     MAP64 ("tprel@higher",      BFD_RELOC_PPC64_TPREL16_HIGHER),
1963     MAP64 ("tprel@highera",     BFD_RELOC_PPC64_TPREL16_HIGHERA),
1964     MAP64 ("tprel@highest",     BFD_RELOC_PPC64_TPREL16_HIGHEST),
1965     MAP64 ("tprel@highesta",    BFD_RELOC_PPC64_TPREL16_HIGHESTA),
1966     { (char *) 0, 0, 0, 0,      BFD_RELOC_UNUSED }
1967   };
1968
1969   if (*str++ != '@')
1970     return BFD_RELOC_UNUSED;
1971
1972   for (ch = *str, str2 = ident;
1973        (str2 < ident + sizeof (ident) - 1
1974         && (ISALNUM (ch) || ch == '@'));
1975        ch = *++str)
1976     {
1977       *str2++ = TOLOWER (ch);
1978     }
1979
1980   *str2 = '\0';
1981   len = str2 - ident;
1982
1983   ch = ident[0];
1984   for (ptr = &mapping[0]; ptr->length > 0; ptr++)
1985     if (ch == ptr->string[0]
1986         && len == ptr->length
1987         && memcmp (ident, ptr->string, ptr->length) == 0
1988         && (ppc_obj64 ? ptr->valid64 : ptr->valid32))
1989       {
1990         int reloc = ptr->reloc;
1991
1992         if (!ppc_obj64 && exp_p->X_add_number != 0)
1993           {
1994             switch (reloc)
1995               {
1996               case BFD_RELOC_16_GOTOFF:
1997               case BFD_RELOC_LO16_GOTOFF:
1998               case BFD_RELOC_HI16_GOTOFF:
1999               case BFD_RELOC_HI16_S_GOTOFF:
2000                 as_warn (_("identifier+constant@got means "
2001                            "identifier@got+constant"));
2002                 break;
2003
2004               case BFD_RELOC_PPC_GOT_TLSGD16:
2005               case BFD_RELOC_PPC_GOT_TLSGD16_LO:
2006               case BFD_RELOC_PPC_GOT_TLSGD16_HI:
2007               case BFD_RELOC_PPC_GOT_TLSGD16_HA:
2008               case BFD_RELOC_PPC_GOT_TLSLD16:
2009               case BFD_RELOC_PPC_GOT_TLSLD16_LO:
2010               case BFD_RELOC_PPC_GOT_TLSLD16_HI:
2011               case BFD_RELOC_PPC_GOT_TLSLD16_HA:
2012               case BFD_RELOC_PPC_GOT_DTPREL16:
2013               case BFD_RELOC_PPC_GOT_DTPREL16_LO:
2014               case BFD_RELOC_PPC_GOT_DTPREL16_HI:
2015               case BFD_RELOC_PPC_GOT_DTPREL16_HA:
2016               case BFD_RELOC_PPC_GOT_TPREL16:
2017               case BFD_RELOC_PPC_GOT_TPREL16_LO:
2018               case BFD_RELOC_PPC_GOT_TPREL16_HI:
2019               case BFD_RELOC_PPC_GOT_TPREL16_HA:
2020                 as_bad (_("symbol+offset not supported for got tls"));
2021                 break;
2022               }
2023           }
2024
2025         /* Now check for identifier@suffix+constant.  */
2026         if (*str == '-' || *str == '+')
2027           {
2028             char *orig_line = input_line_pointer;
2029             expressionS new_exp;
2030
2031             input_line_pointer = str;
2032             expression (&new_exp);
2033             if (new_exp.X_op == O_constant)
2034               {
2035                 exp_p->X_add_number += new_exp.X_add_number;
2036                 str = input_line_pointer;
2037               }
2038
2039             if (&input_line_pointer != str_p)
2040               input_line_pointer = orig_line;
2041           }
2042         *str_p = str;
2043
2044         if (reloc == (int) BFD_RELOC_PPC64_TOC
2045             && exp_p->X_op == O_symbol
2046             && strcmp (S_GET_NAME (exp_p->X_add_symbol), ".TOC.") == 0)
2047           {
2048             /* Change the symbol so that the dummy .TOC. symbol can be
2049                omitted from the object file.  */
2050             exp_p->X_add_symbol = &abs_symbol;
2051           }
2052
2053         return (bfd_reloc_code_real_type) reloc;
2054       }
2055
2056   return BFD_RELOC_UNUSED;
2057 }
2058
2059 /* Like normal .long/.short/.word, except support @got, etc.
2060    Clobbers input_line_pointer, checks end-of-line.  */
2061 static void
2062 ppc_elf_cons (int nbytes /* 1=.byte, 2=.word, 4=.long, 8=.llong */)
2063 {
2064   expressionS exp;
2065   bfd_reloc_code_real_type reloc;
2066
2067   if (is_it_end_of_statement ())
2068     {
2069       demand_empty_rest_of_line ();
2070       return;
2071     }
2072
2073   do
2074     {
2075       expression (&exp);
2076       if (exp.X_op == O_symbol
2077           && *input_line_pointer == '@'
2078           && (reloc = ppc_elf_suffix (&input_line_pointer,
2079                                       &exp)) != BFD_RELOC_UNUSED)
2080         {
2081           reloc_howto_type *reloc_howto;
2082           int size;
2083
2084           reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
2085           size = bfd_get_reloc_size (reloc_howto);
2086
2087           if (size > nbytes)
2088             {
2089               as_bad (_("%s relocations do not fit in %d bytes\n"),
2090                       reloc_howto->name, nbytes);
2091             }
2092           else
2093             {
2094               char *p;
2095               int offset;
2096
2097               p = frag_more (nbytes);
2098               memset (p, 0, nbytes);
2099               offset = 0;
2100               if (target_big_endian)
2101                 offset = nbytes - size;
2102               fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size,
2103                            &exp, 0, reloc);
2104             }
2105         }
2106       else
2107         emit_expr (&exp, (unsigned int) nbytes);
2108     }
2109   while (*input_line_pointer++ == ',');
2110
2111   /* Put terminator back into stream.  */
2112   input_line_pointer--;
2113   demand_empty_rest_of_line ();
2114 }
2115
2116 /* Solaris pseduo op to change to the .rodata section.  */
2117 static void
2118 ppc_elf_rdata (int xxx)
2119 {
2120   char *save_line = input_line_pointer;
2121   static char section[] = ".rodata\n";
2122
2123   /* Just pretend this is .section .rodata  */
2124   input_line_pointer = section;
2125   obj_elf_section (xxx);
2126
2127   input_line_pointer = save_line;
2128 }
2129
2130 /* Pseudo op to make file scope bss items.  */
2131 static void
2132 ppc_elf_lcomm (int xxx ATTRIBUTE_UNUSED)
2133 {
2134   char *name;
2135   char c;
2136   char *p;
2137   offsetT size;
2138   symbolS *symbolP;
2139   offsetT align;
2140   segT old_sec;
2141   int old_subsec;
2142   char *pfrag;
2143   int align2;
2144
2145   name = input_line_pointer;
2146   c = get_symbol_end ();
2147
2148   /* just after name is now '\0'.  */
2149   p = input_line_pointer;
2150   *p = c;
2151   SKIP_WHITESPACE ();
2152   if (*input_line_pointer != ',')
2153     {
2154       as_bad (_("expected comma after symbol-name: rest of line ignored."));
2155       ignore_rest_of_line ();
2156       return;
2157     }
2158
2159   input_line_pointer++;         /* skip ',' */
2160   if ((size = get_absolute_expression ()) < 0)
2161     {
2162       as_warn (_(".COMMon length (%ld.) <0! Ignored."), (long) size);
2163       ignore_rest_of_line ();
2164       return;
2165     }
2166
2167   /* The third argument to .lcomm is the alignment.  */
2168   if (*input_line_pointer != ',')
2169     align = 8;
2170   else
2171     {
2172       ++input_line_pointer;
2173       align = get_absolute_expression ();
2174       if (align <= 0)
2175         {
2176           as_warn (_("ignoring bad alignment"));
2177           align = 8;
2178         }
2179     }
2180
2181   *p = 0;
2182   symbolP = symbol_find_or_make (name);
2183   *p = c;
2184
2185   if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
2186     {
2187       as_bad (_("ignoring attempt to re-define symbol `%s'."),
2188               S_GET_NAME (symbolP));
2189       ignore_rest_of_line ();
2190       return;
2191     }
2192
2193   if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
2194     {
2195       as_bad (_("length of .lcomm \"%s\" is already %ld. Not changed to %ld."),
2196               S_GET_NAME (symbolP),
2197               (long) S_GET_VALUE (symbolP),
2198               (long) size);
2199
2200       ignore_rest_of_line ();
2201       return;
2202     }
2203
2204   /* Allocate_bss.  */
2205   old_sec = now_seg;
2206   old_subsec = now_subseg;
2207   if (align)
2208     {
2209       /* Convert to a power of 2 alignment.  */
2210       for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2);
2211       if (align != 1)
2212         {
2213           as_bad (_("common alignment not a power of 2"));
2214           ignore_rest_of_line ();
2215           return;
2216         }
2217     }
2218   else
2219     align2 = 0;
2220
2221   record_alignment (bss_section, align2);
2222   subseg_set (bss_section, 0);
2223   if (align2)
2224     frag_align (align2, 0, 0);
2225   if (S_GET_SEGMENT (symbolP) == bss_section)
2226     symbol_get_frag (symbolP)->fr_symbol = 0;
2227   symbol_set_frag (symbolP, frag_now);
2228   pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
2229                     (char *) 0);
2230   *pfrag = 0;
2231   S_SET_SIZE (symbolP, size);
2232   S_SET_SEGMENT (symbolP, bss_section);
2233   subseg_set (old_sec, old_subsec);
2234   demand_empty_rest_of_line ();
2235 }
2236
2237 /* Validate any relocations emitted for -mrelocatable, possibly adding
2238    fixups for word relocations in writable segments, so we can adjust
2239    them at runtime.  */
2240 static void
2241 ppc_elf_validate_fix (fixS *fixp, segT seg)
2242 {
2243   if (fixp->fx_done || fixp->fx_pcrel)
2244     return;
2245
2246   switch (shlib)
2247     {
2248     case SHLIB_NONE:
2249     case SHLIB_PIC:
2250       return;
2251
2252     case SHLIB_MRELOCATABLE:
2253       if (fixp->fx_r_type <= BFD_RELOC_UNUSED
2254           && fixp->fx_r_type != BFD_RELOC_16_GOTOFF
2255           && fixp->fx_r_type != BFD_RELOC_HI16_GOTOFF
2256           && fixp->fx_r_type != BFD_RELOC_LO16_GOTOFF
2257           && fixp->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
2258           && fixp->fx_r_type != BFD_RELOC_16_BASEREL
2259           && fixp->fx_r_type != BFD_RELOC_LO16_BASEREL
2260           && fixp->fx_r_type != BFD_RELOC_HI16_BASEREL
2261           && fixp->fx_r_type != BFD_RELOC_HI16_S_BASEREL
2262           && (seg->flags & SEC_LOAD) != 0
2263           && strcmp (segment_name (seg), ".got2") != 0
2264           && strcmp (segment_name (seg), ".dtors") != 0
2265           && strcmp (segment_name (seg), ".ctors") != 0
2266           && strcmp (segment_name (seg), ".fixup") != 0
2267           && strcmp (segment_name (seg), ".gcc_except_table") != 0
2268           && strcmp (segment_name (seg), ".eh_frame") != 0
2269           && strcmp (segment_name (seg), ".ex_shared") != 0)
2270         {
2271           if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
2272               || fixp->fx_r_type != BFD_RELOC_CTOR)
2273             {
2274               as_bad_where (fixp->fx_file, fixp->fx_line,
2275                             _("relocation cannot be done when using -mrelocatable"));
2276             }
2277         }
2278       return;
2279     }
2280 }
2281
2282 /* Prevent elf_frob_file_before_adjust removing a weak undefined
2283    function descriptor sym if the corresponding code sym is used.  */
2284
2285 void
2286 ppc_frob_file_before_adjust (void)
2287 {
2288   symbolS *symp;
2289   asection *toc;
2290
2291   if (!ppc_obj64)
2292     return;
2293
2294   for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2295     {
2296       const char *name;
2297       char *dotname;
2298       symbolS *dotsym;
2299       size_t len;
2300
2301       name = S_GET_NAME (symp);
2302       if (name[0] == '.')
2303         continue;
2304
2305       if (! S_IS_WEAK (symp)
2306           || S_IS_DEFINED (symp))
2307         continue;
2308
2309       len = strlen (name) + 1;
2310       dotname = xmalloc (len + 1);
2311       dotname[0] = '.';
2312       memcpy (dotname + 1, name, len);
2313       dotsym = symbol_find_noref (dotname, 1);
2314       free (dotname);
2315       if (dotsym != NULL && (symbol_used_p (dotsym)
2316                              || symbol_used_in_reloc_p (dotsym)))
2317         symbol_mark_used (symp);
2318
2319     }
2320
2321   toc = bfd_get_section_by_name (stdoutput, ".toc");
2322   if (toc != NULL
2323       && toc_reloc_types != has_large_toc_reloc
2324       && bfd_section_size (stdoutput, toc) > 0x10000)
2325     as_warn (_("TOC section size exceeds 64k"));
2326
2327   /* Don't emit .TOC. symbol.  */
2328   symp = symbol_find (".TOC.");
2329   if (symp != NULL)
2330     symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2331 }
2332 #endif /* OBJ_ELF */
2333 \f
2334 #ifdef TE_PE
2335
2336 /*
2337  * Summary of parse_toc_entry.
2338  *
2339  * in:  Input_line_pointer points to the '[' in one of:
2340  *
2341  *        [toc] [tocv] [toc32] [toc64]
2342  *
2343  *      Anything else is an error of one kind or another.
2344  *
2345  * out:
2346  *   return value: success or failure
2347  *   toc_kind:     kind of toc reference
2348  *   input_line_pointer:
2349  *     success: first char after the ']'
2350  *     failure: unchanged
2351  *
2352  * settings:
2353  *
2354  *     [toc]   - rv == success, toc_kind = default_toc
2355  *     [tocv]  - rv == success, toc_kind = data_in_toc
2356  *     [toc32] - rv == success, toc_kind = must_be_32
2357  *     [toc64] - rv == success, toc_kind = must_be_64
2358  *
2359  */
2360
2361 enum toc_size_qualifier
2362 {
2363   default_toc, /* The toc cell constructed should be the system default size */
2364   data_in_toc, /* This is a direct reference to a toc cell                   */
2365   must_be_32,  /* The toc cell constructed must be 32 bits wide              */
2366   must_be_64   /* The toc cell constructed must be 64 bits wide              */
2367 };
2368
2369 static int
2370 parse_toc_entry (enum toc_size_qualifier *toc_kind)
2371 {
2372   char *start;
2373   char *toc_spec;
2374   char c;
2375   enum toc_size_qualifier t;
2376
2377   /* Save the input_line_pointer.  */
2378   start = input_line_pointer;
2379
2380   /* Skip over the '[' , and whitespace.  */
2381   ++input_line_pointer;
2382   SKIP_WHITESPACE ();
2383
2384   /* Find the spelling of the operand.  */
2385   toc_spec = input_line_pointer;
2386   c = get_symbol_end ();
2387
2388   if (strcmp (toc_spec, "toc") == 0)
2389     {
2390       t = default_toc;
2391     }
2392   else if (strcmp (toc_spec, "tocv") == 0)
2393     {
2394       t = data_in_toc;
2395     }
2396   else if (strcmp (toc_spec, "toc32") == 0)
2397     {
2398       t = must_be_32;
2399     }
2400   else if (strcmp (toc_spec, "toc64") == 0)
2401     {
2402       t = must_be_64;
2403     }
2404   else
2405     {
2406       as_bad (_("syntax error: invalid toc specifier `%s'"), toc_spec);
2407       *input_line_pointer = c;
2408       input_line_pointer = start;
2409       return 0;
2410     }
2411
2412   /* Now find the ']'.  */
2413   *input_line_pointer = c;
2414
2415   SKIP_WHITESPACE ();        /* leading whitespace could be there.  */
2416   c = *input_line_pointer++; /* input_line_pointer->past char in c.  */
2417
2418   if (c != ']')
2419     {
2420       as_bad (_("syntax error: expected `]', found  `%c'"), c);
2421       input_line_pointer = start;
2422       return 0;
2423     }
2424
2425   *toc_kind = t;
2426   return 1;
2427 }
2428 #endif
2429
2430 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
2431 /* See whether a symbol is in the TOC section.  */
2432
2433 static int
2434 ppc_is_toc_sym (symbolS *sym)
2435 {
2436 #ifdef OBJ_XCOFF
2437   return symbol_get_tc (sym)->symbol_class == XMC_TC;
2438 #endif
2439 #ifdef OBJ_ELF
2440   const char *sname = segment_name (S_GET_SEGMENT (sym));
2441   if (ppc_obj64)
2442     return strcmp (sname, ".toc") == 0;
2443   else
2444     return strcmp (sname, ".got") == 0;
2445 #endif
2446 }
2447 #endif /* defined (OBJ_XCOFF) || defined (OBJ_ELF) */
2448 \f
2449
2450 #ifdef OBJ_ELF
2451 #define APUID(a,v)      ((((a) & 0xffff) << 16) | ((v) & 0xffff))
2452 static void
2453 ppc_apuinfo_section_add (unsigned int apu, unsigned int version)
2454 {
2455   unsigned int i;
2456
2457   /* Check we don't already exist.  */
2458   for (i = 0; i < ppc_apuinfo_num; i++)
2459     if (ppc_apuinfo_list[i] == APUID (apu, version))
2460       return;
2461
2462   if (ppc_apuinfo_num == ppc_apuinfo_num_alloc)
2463     {
2464       if (ppc_apuinfo_num_alloc == 0)
2465         {
2466           ppc_apuinfo_num_alloc = 4;
2467           ppc_apuinfo_list = (unsigned long *)
2468               xmalloc (sizeof (unsigned long) * ppc_apuinfo_num_alloc);
2469         }
2470       else
2471         {
2472           ppc_apuinfo_num_alloc += 4;
2473           ppc_apuinfo_list = (unsigned long *) xrealloc (ppc_apuinfo_list,
2474               sizeof (unsigned long) * ppc_apuinfo_num_alloc);
2475         }
2476     }
2477   ppc_apuinfo_list[ppc_apuinfo_num++] = APUID (apu, version);
2478 }
2479 #undef APUID
2480 #endif
2481 \f
2482
2483 /* We need to keep a list of fixups.  We can't simply generate them as
2484    we go, because that would require us to first create the frag, and
2485    that would screw up references to ``.''.  */
2486
2487 struct ppc_fixup
2488 {
2489   expressionS exp;
2490   int opindex;
2491   bfd_reloc_code_real_type reloc;
2492 };
2493
2494 #define MAX_INSN_FIXUPS (5)
2495
2496 /* Form I16L.  */
2497 #define E_OR2I_INSN             0x7000C000
2498 #define E_AND2I_DOT_INSN        0x7000C800
2499 #define E_OR2IS_INSN            0x7000D000
2500 #define E_LIS_INSN              0x7000E000
2501 #define E_AND2IS_DOT_INSN       0x7000E800
2502
2503 /* Form I16A.  */
2504 #define E_ADD2I_DOT_INSN        0x70008800
2505 #define E_ADD2IS_INSN           0x70009000
2506 #define E_CMP16I_INSN           0x70009800
2507 #define E_MULL2I_INSN           0x7000A000
2508 #define E_CMPL16I_INSN          0x7000A800
2509 #define E_CMPH16I_INSN          0x7000B000
2510 #define E_CMPHL16I_INSN         0x7000B800
2511
2512 /* This routine is called for each instruction to be assembled.  */
2513
2514 void
2515 md_assemble (char *str)
2516 {
2517   char *s;
2518   const struct powerpc_opcode *opcode;
2519   unsigned long insn;
2520   const unsigned char *opindex_ptr;
2521   int skip_optional;
2522   int need_paren;
2523   int next_opindex;
2524   struct ppc_fixup fixups[MAX_INSN_FIXUPS];
2525   int fc;
2526   char *f;
2527   int addr_mod;
2528   int i;
2529   unsigned int insn_length;
2530
2531   /* Get the opcode.  */
2532   for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
2533     ;
2534   if (*s != '\0')
2535     *s++ = '\0';
2536
2537   /* Look up the opcode in the hash table.  */
2538   opcode = (const struct powerpc_opcode *) hash_find (ppc_hash, str);
2539   if (opcode == (const struct powerpc_opcode *) NULL)
2540     {
2541       const struct powerpc_macro *macro;
2542
2543       macro = (const struct powerpc_macro *) hash_find (ppc_macro_hash, str);
2544       if (macro == (const struct powerpc_macro *) NULL)
2545         as_bad (_("unrecognized opcode: `%s'"), str);
2546       else
2547         ppc_macro (s, macro);
2548
2549       return;
2550     }
2551
2552   insn = opcode->opcode;
2553
2554   str = s;
2555   while (ISSPACE (*str))
2556     ++str;
2557
2558   /* PowerPC operands are just expressions.  The only real issue is
2559      that a few operand types are optional.  All cases which might use
2560      an optional operand separate the operands only with commas (in some
2561      cases parentheses are used, as in ``lwz 1,0(1)'' but such cases never
2562      have optional operands).  Most instructions with optional operands
2563      have only one.  Those that have more than one optional operand can
2564      take either all their operands or none.  So, before we start seriously
2565      parsing the operands, we check to see if we have optional operands,
2566      and if we do, we count the number of commas to see which operands
2567      have been omitted.  */
2568   skip_optional = 0;
2569   for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
2570     {
2571       const struct powerpc_operand *operand;
2572
2573       operand = &powerpc_operands[*opindex_ptr];
2574       if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
2575         {
2576           unsigned int opcount;
2577           unsigned int num_operands_expected;
2578
2579           /* There is an optional operand.  Count the number of
2580              commas in the input line.  */
2581           if (*str == '\0')
2582             opcount = 0;
2583           else
2584             {
2585               opcount = 1;
2586               s = str;
2587               while ((s = strchr (s, ',')) != (char *) NULL)
2588                 {
2589                   ++opcount;
2590                   ++s;
2591                 }
2592             }
2593
2594           /* Compute the number of expected operands.
2595              Do not count fake operands.  */
2596           for (num_operands_expected = 0, i = 0; opcode->operands[i]; i ++)
2597             if ((powerpc_operands [opcode->operands[i]].flags & PPC_OPERAND_FAKE) == 0)
2598               ++ num_operands_expected;
2599
2600           /* If there are fewer operands in the line then are called
2601              for by the instruction, we want to skip the optional
2602              operands.  */
2603           if (opcount < num_operands_expected)
2604             skip_optional = 1;
2605
2606           break;
2607         }
2608     }
2609
2610   /* Gather the operands.  */
2611   need_paren = 0;
2612   next_opindex = 0;
2613   fc = 0;
2614   for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
2615     {
2616       const struct powerpc_operand *operand;
2617       const char *errmsg;
2618       char *hold;
2619       expressionS ex;
2620       char endc;
2621
2622       if (next_opindex == 0)
2623         operand = &powerpc_operands[*opindex_ptr];
2624       else
2625         {
2626           operand = &powerpc_operands[next_opindex];
2627           next_opindex = 0;
2628         }
2629       errmsg = NULL;
2630
2631       /* If this is a fake operand, then we do not expect anything
2632          from the input.  */
2633       if ((operand->flags & PPC_OPERAND_FAKE) != 0)
2634         {
2635           insn = (*operand->insert) (insn, 0L, ppc_cpu, &errmsg);
2636           if (errmsg != (const char *) NULL)
2637             as_bad ("%s", errmsg);
2638           continue;
2639         }
2640
2641       /* If this is an optional operand, and we are skipping it, just
2642          insert a zero.  */
2643       if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
2644           && skip_optional)
2645         {
2646           if (operand->insert)
2647             {
2648               insn = (*operand->insert) (insn, 0L, ppc_cpu, &errmsg);
2649               if (errmsg != (const char *) NULL)
2650                 as_bad ("%s", errmsg);
2651             }
2652           if ((operand->flags & PPC_OPERAND_NEXT) != 0)
2653             next_opindex = *opindex_ptr + 1;
2654           continue;
2655         }
2656
2657       /* Gather the operand.  */
2658       hold = input_line_pointer;
2659       input_line_pointer = str;
2660
2661 #ifdef TE_PE
2662       if (*input_line_pointer == '[')
2663         {
2664           /* We are expecting something like the second argument here:
2665            *
2666            *    lwz r4,[toc].GS.0.static_int(rtoc)
2667            *           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2668            * The argument following the `]' must be a symbol name, and the
2669            * register must be the toc register: 'rtoc' or '2'
2670            *
2671            * The effect is to 0 as the displacement field
2672            * in the instruction, and issue an IMAGE_REL_PPC_TOCREL16 (or
2673            * the appropriate variation) reloc against it based on the symbol.
2674            * The linker will build the toc, and insert the resolved toc offset.
2675            *
2676            * Note:
2677            * o The size of the toc entry is currently assumed to be
2678            *   32 bits. This should not be assumed to be a hard coded
2679            *   number.
2680            * o In an effort to cope with a change from 32 to 64 bits,
2681            *   there are also toc entries that are specified to be
2682            *   either 32 or 64 bits:
2683            *     lwz r4,[toc32].GS.0.static_int(rtoc)
2684            *     lwz r4,[toc64].GS.0.static_int(rtoc)
2685            *   These demand toc entries of the specified size, and the
2686            *   instruction probably requires it.
2687            */
2688
2689           int valid_toc;
2690           enum toc_size_qualifier toc_kind;
2691           bfd_reloc_code_real_type toc_reloc;
2692
2693           /* Go parse off the [tocXX] part.  */
2694           valid_toc = parse_toc_entry (&toc_kind);
2695
2696           if (!valid_toc)
2697             {
2698               /* Note: message has already been issued.
2699                  FIXME: what sort of recovery should we do?
2700                  demand_rest_of_line (); return; ?  */
2701             }
2702
2703           /* Now get the symbol following the ']'.  */
2704           expression (&ex);
2705
2706           switch (toc_kind)
2707             {
2708             case default_toc:
2709               /* In this case, we may not have seen the symbol yet,
2710                  since  it is allowed to appear on a .extern or .globl
2711                  or just be a label in the .data section.  */
2712               toc_reloc = BFD_RELOC_PPC_TOC16;
2713               break;
2714             case data_in_toc:
2715               /* 1. The symbol must be defined and either in the toc
2716                  section, or a global.
2717                  2. The reloc generated must have the TOCDEFN flag set
2718                  in upper bit mess of the reloc type.
2719                  FIXME: It's a little confusing what the tocv
2720                  qualifier can be used for.  At the very least, I've
2721                  seen three uses, only one of which I'm sure I can
2722                  explain.  */
2723               if (ex.X_op == O_symbol)
2724                 {
2725                   gas_assert (ex.X_add_symbol != NULL);
2726                   if (symbol_get_bfdsym (ex.X_add_symbol)->section
2727                       != tocdata_section)
2728                     {
2729                       as_bad (_("[tocv] symbol is not a toc symbol"));
2730                     }
2731                 }
2732
2733               toc_reloc = BFD_RELOC_PPC_TOC16;
2734               break;
2735             case must_be_32:
2736               /* FIXME: these next two specifically specify 32/64 bit
2737                  toc entries.  We don't support them today.  Is this
2738                  the right way to say that?  */
2739               toc_reloc = BFD_RELOC_UNUSED;
2740               as_bad (_("unimplemented toc32 expression modifier"));
2741               break;
2742             case must_be_64:
2743               /* FIXME: see above.  */
2744               toc_reloc = BFD_RELOC_UNUSED;
2745               as_bad (_("unimplemented toc64 expression modifier"));
2746               break;
2747             default:
2748               fprintf (stderr,
2749                        _("Unexpected return value [%d] from parse_toc_entry!\n"),
2750                        toc_kind);
2751               abort ();
2752               break;
2753             }
2754
2755           /* We need to generate a fixup for this expression.  */
2756           if (fc >= MAX_INSN_FIXUPS)
2757             as_fatal (_("too many fixups"));
2758
2759           fixups[fc].reloc = toc_reloc;
2760           fixups[fc].exp = ex;
2761           fixups[fc].opindex = *opindex_ptr;
2762           ++fc;
2763
2764           /* Ok. We've set up the fixup for the instruction. Now make it
2765              look like the constant 0 was found here.  */
2766           ex.X_unsigned = 1;
2767           ex.X_op = O_constant;
2768           ex.X_add_number = 0;
2769           ex.X_add_symbol = NULL;
2770           ex.X_op_symbol = NULL;
2771         }
2772
2773       else
2774 #endif          /* TE_PE */
2775         {
2776           if ((reg_names_p
2777                && (((operand->flags & PPC_OPERAND_CR_BIT) != 0)
2778                    || ((operand->flags & PPC_OPERAND_CR_REG) != 0)))
2779               || !register_name (&ex))
2780             {
2781               char save_lex = lex_type['%'];
2782
2783               if (((operand->flags & PPC_OPERAND_CR_REG) != 0)
2784                   || (operand->flags & PPC_OPERAND_CR_BIT) != 0)
2785                 {
2786                   cr_operand = TRUE;
2787                   lex_type['%'] |= LEX_BEGIN_NAME;
2788                 }
2789               expression (&ex);
2790               cr_operand = FALSE;
2791               lex_type['%'] = save_lex;
2792             }
2793         }
2794
2795       str = input_line_pointer;
2796       input_line_pointer = hold;
2797
2798       if (ex.X_op == O_illegal)
2799         as_bad (_("illegal operand"));
2800       else if (ex.X_op == O_absent)
2801         as_bad (_("missing operand"));
2802       else if (ex.X_op == O_register)
2803         {
2804           insn = ppc_insert_operand (insn, operand, ex.X_add_number,
2805                                      ppc_cpu, (char *) NULL, 0);
2806         }
2807       else if (ex.X_op == O_constant)
2808         {
2809 #ifdef OBJ_ELF
2810           /* Allow @HA, @L, @H on constants.  */
2811           bfd_reloc_code_real_type reloc;
2812           char *orig_str = str;
2813
2814           if ((reloc = ppc_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
2815             switch (reloc)
2816               {
2817               default:
2818                 str = orig_str;
2819                 break;
2820
2821               case BFD_RELOC_LO16:
2822                 /* X_unsigned is the default, so if the user has done
2823                    something which cleared it, we always produce a
2824                    signed value.  */
2825                 if (ex.X_unsigned && ! (operand->flags & PPC_OPERAND_SIGNED))
2826                   ex.X_add_number &= 0xffff;
2827                 else
2828                   ex.X_add_number = SEX16 (ex.X_add_number);
2829                 break;
2830
2831               case BFD_RELOC_HI16:
2832                 if (ex.X_unsigned && ! (operand->flags & PPC_OPERAND_SIGNED))
2833                   ex.X_add_number = PPC_HI (ex.X_add_number);
2834                 else
2835                   ex.X_add_number = SEX16 (PPC_HI (ex.X_add_number));
2836                 break;
2837
2838               case BFD_RELOC_HI16_S:
2839                 if (ex.X_unsigned && ! (operand->flags & PPC_OPERAND_SIGNED))
2840                   ex.X_add_number = PPC_HA (ex.X_add_number);
2841                 else
2842                   ex.X_add_number = SEX16 (PPC_HA (ex.X_add_number));
2843                 break;
2844
2845               case BFD_RELOC_PPC64_HIGHER:
2846                 if (ex.X_unsigned && ! (operand->flags & PPC_OPERAND_SIGNED))
2847                   ex.X_add_number = PPC_HIGHER (ex.X_add_number);
2848                 else
2849                   ex.X_add_number = SEX16 (PPC_HIGHER (ex.X_add_number));
2850                 break;
2851
2852               case BFD_RELOC_PPC64_HIGHER_S:
2853                 if (ex.X_unsigned && ! (operand->flags & PPC_OPERAND_SIGNED))
2854                   ex.X_add_number = PPC_HIGHERA (ex.X_add_number);
2855                 else
2856                   ex.X_add_number = SEX16 (PPC_HIGHERA (ex.X_add_number));
2857                 break;
2858
2859               case BFD_RELOC_PPC64_HIGHEST:
2860                 if (ex.X_unsigned && ! (operand->flags & PPC_OPERAND_SIGNED))
2861                   ex.X_add_number = PPC_HIGHEST (ex.X_add_number);
2862                 else
2863                   ex.X_add_number = SEX16 (PPC_HIGHEST (ex.X_add_number));
2864                 break;
2865
2866               case BFD_RELOC_PPC64_HIGHEST_S:
2867                 if (ex.X_unsigned && ! (operand->flags & PPC_OPERAND_SIGNED))
2868                   ex.X_add_number = PPC_HIGHESTA (ex.X_add_number);
2869                 else
2870                   ex.X_add_number = SEX16 (PPC_HIGHESTA (ex.X_add_number));
2871                 break;
2872               }
2873 #endif /* OBJ_ELF */
2874           insn = ppc_insert_operand (insn, operand, ex.X_add_number,
2875                                      ppc_cpu, (char *) NULL, 0);
2876         }
2877       else
2878         {
2879           bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
2880 #ifdef OBJ_ELF
2881           if (ex.X_op == O_symbol && str[0] == '(')
2882             {
2883               const char *sym_name = S_GET_NAME (ex.X_add_symbol);
2884               if (sym_name[0] == '.')
2885                 ++sym_name;
2886
2887               if (strcasecmp (sym_name, "__tls_get_addr") == 0)
2888                 {
2889                   expressionS tls_exp;
2890
2891                   hold = input_line_pointer;
2892                   input_line_pointer = str + 1;
2893                   expression (&tls_exp);
2894                   if (tls_exp.X_op == O_symbol)
2895                     {
2896                       reloc = BFD_RELOC_UNUSED;
2897                       if (strncasecmp (input_line_pointer, "@tlsgd)", 7) == 0)
2898                         {
2899                           reloc = BFD_RELOC_PPC_TLSGD;
2900                           input_line_pointer += 7;
2901                         }
2902                       else if (strncasecmp (input_line_pointer, "@tlsld)", 7) == 0)
2903                         {
2904                           reloc = BFD_RELOC_PPC_TLSLD;
2905                           input_line_pointer += 7;
2906                         }
2907                       if (reloc != BFD_RELOC_UNUSED)
2908                         {
2909                           SKIP_WHITESPACE ();
2910                           str = input_line_pointer;
2911
2912                           if (fc >= MAX_INSN_FIXUPS)
2913                             as_fatal (_("too many fixups"));
2914                           fixups[fc].exp = tls_exp;
2915                           fixups[fc].opindex = *opindex_ptr;
2916                           fixups[fc].reloc = reloc;
2917                           ++fc;
2918                         }
2919                     }
2920                   input_line_pointer = hold;
2921                 }
2922             }
2923
2924           if ((reloc = ppc_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
2925             {
2926               /* Some TLS tweaks.  */
2927               switch (reloc)
2928                 {
2929                 default:
2930                   break;
2931
2932                 case BFD_RELOC_PPC_TLS:
2933                   if (!_bfd_elf_ppc_at_tls_transform (opcode->opcode, 0))
2934                     as_bad (_("@tls may not be used with \"%s\" operands"),
2935                             opcode->name);
2936                   else if (operand->shift != 11)
2937                     as_bad (_("@tls may only be used in last operand"));
2938                   else
2939                     insn = ppc_insert_operand (insn, operand,
2940                                                ppc_obj64 ? 13 : 2,
2941                                                ppc_cpu, (char *) NULL, 0);
2942                   break;
2943
2944                   /* We'll only use the 32 (or 64) bit form of these relocations
2945                      in constants.  Instructions get the 16 bit form.  */
2946                 case BFD_RELOC_PPC_DTPREL:
2947                   reloc = BFD_RELOC_PPC_DTPREL16;
2948                   break;
2949                 case BFD_RELOC_PPC_TPREL:
2950                   reloc = BFD_RELOC_PPC_TPREL16;
2951                   break;
2952                 }
2953
2954               /* If VLE-mode convert LO/HI/HA relocations.  */
2955               if (opcode->flags & PPC_OPCODE_VLE)
2956                 {
2957                   int tmp_insn = insn & opcode->mask;
2958                   
2959                   int use_d_reloc = (tmp_insn == E_OR2I_INSN
2960                                      || tmp_insn == E_AND2I_DOT_INSN
2961                                      || tmp_insn == E_OR2IS_INSN
2962                                      || tmp_insn == E_LIS_INSN
2963                                      || tmp_insn == E_AND2IS_DOT_INSN);
2964
2965
2966                   int use_a_reloc = (tmp_insn == E_ADD2I_DOT_INSN
2967                                      || tmp_insn == E_ADD2IS_INSN
2968                                      || tmp_insn == E_CMP16I_INSN
2969                                      || tmp_insn == E_MULL2I_INSN
2970                                      || tmp_insn == E_CMPL16I_INSN
2971                                      || tmp_insn == E_CMPH16I_INSN
2972                                      || tmp_insn == E_CMPHL16I_INSN);
2973
2974                   switch (reloc)
2975                     {
2976                     default:
2977                       break;
2978
2979                     case BFD_RELOC_PPC_EMB_SDA21:
2980                       reloc = BFD_RELOC_PPC_VLE_SDA21;
2981                       break;
2982
2983                     case BFD_RELOC_LO16:
2984                       if (use_d_reloc)
2985                         reloc = BFD_RELOC_PPC_VLE_LO16D;
2986                       else if (use_a_reloc)
2987                         reloc = BFD_RELOC_PPC_VLE_LO16A;
2988                       break;
2989
2990                     case BFD_RELOC_HI16:
2991                       if (use_d_reloc)
2992                         reloc = BFD_RELOC_PPC_VLE_HI16D;
2993                       else if (use_a_reloc)
2994                         reloc = BFD_RELOC_PPC_VLE_HI16A;
2995                       break;
2996          
2997                     case BFD_RELOC_HI16_S:
2998                       if (use_d_reloc)
2999                         reloc = BFD_RELOC_PPC_VLE_HA16D;
3000                       else if (use_a_reloc)
3001                         reloc = BFD_RELOC_PPC_VLE_HA16A;
3002                       break;
3003
3004                     case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
3005                       if (use_d_reloc)
3006                         reloc = BFD_RELOC_PPC_VLE_SDAREL_LO16D;
3007                       break;
3008
3009                     case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
3010                       if (use_d_reloc)
3011                         reloc = BFD_RELOC_PPC_VLE_SDAREL_HI16D;
3012                       break;
3013
3014                     case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
3015                       if (use_d_reloc)
3016                         reloc = BFD_RELOC_PPC_VLE_SDAREL_HA16D;
3017                       break;
3018                     }
3019                 }
3020
3021               /* For the absolute forms of branches, convert the PC
3022                  relative form back into the absolute.  */
3023               if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
3024                 {
3025                   switch (reloc)
3026                     {
3027                     case BFD_RELOC_PPC_B26:
3028                       reloc = BFD_RELOC_PPC_BA26;
3029                       break;
3030                     case BFD_RELOC_PPC_B16:
3031                       reloc = BFD_RELOC_PPC_BA16;
3032                       break;
3033                     case BFD_RELOC_PPC_B16_BRTAKEN:
3034                       reloc = BFD_RELOC_PPC_BA16_BRTAKEN;
3035                       break;
3036                     case BFD_RELOC_PPC_B16_BRNTAKEN:
3037                       reloc = BFD_RELOC_PPC_BA16_BRNTAKEN;
3038                       break;
3039                     default:
3040                       break;
3041                     }
3042                 }
3043
3044               switch (reloc)
3045                 {
3046                 case BFD_RELOC_PPC_TOC16:
3047                   toc_reloc_types |= has_small_toc_reloc;
3048                   break;
3049                 case BFD_RELOC_PPC64_TOC16_LO:
3050                 case BFD_RELOC_PPC64_TOC16_HI:
3051                 case BFD_RELOC_PPC64_TOC16_HA:
3052                   toc_reloc_types |= has_large_toc_reloc;
3053                   break;
3054                 default:
3055                   break;
3056                 }
3057
3058               if ((operand->flags & (PPC_OPERAND_DS | PPC_OPERAND_DQ)) != 0)
3059                 {
3060                   switch (reloc)
3061                     {
3062                     case BFD_RELOC_16:
3063                       reloc = BFD_RELOC_PPC64_ADDR16_DS;
3064                       break;
3065                     case BFD_RELOC_LO16:
3066                       reloc = BFD_RELOC_PPC64_ADDR16_LO_DS;
3067                       break;
3068                     case BFD_RELOC_16_GOTOFF:
3069                       reloc = BFD_RELOC_PPC64_GOT16_DS;
3070                       break;
3071                     case BFD_RELOC_LO16_GOTOFF:
3072                       reloc = BFD_RELOC_PPC64_GOT16_LO_DS;
3073                       break;
3074                     case BFD_RELOC_LO16_PLTOFF:
3075                       reloc = BFD_RELOC_PPC64_PLT16_LO_DS;
3076                       break;
3077                     case BFD_RELOC_16_BASEREL:
3078                       reloc = BFD_RELOC_PPC64_SECTOFF_DS;
3079                       break;
3080                     case BFD_RELOC_LO16_BASEREL:
3081                       reloc = BFD_RELOC_PPC64_SECTOFF_LO_DS;
3082                       break;
3083                     case BFD_RELOC_PPC_TOC16:
3084                       reloc = BFD_RELOC_PPC64_TOC16_DS;
3085                       break;
3086                     case BFD_RELOC_PPC64_TOC16_LO:
3087                       reloc = BFD_RELOC_PPC64_TOC16_LO_DS;
3088                       break;
3089                     case BFD_RELOC_PPC64_PLTGOT16:
3090                       reloc = BFD_RELOC_PPC64_PLTGOT16_DS;
3091                       break;
3092                     case BFD_RELOC_PPC64_PLTGOT16_LO:
3093                       reloc = BFD_RELOC_PPC64_PLTGOT16_LO_DS;
3094                       break;
3095                     case BFD_RELOC_PPC_DTPREL16:
3096                       reloc = BFD_RELOC_PPC64_DTPREL16_DS;
3097                       break;
3098                     case BFD_RELOC_PPC_DTPREL16_LO:
3099                       reloc = BFD_RELOC_PPC64_DTPREL16_LO_DS;
3100                       break;
3101                     case BFD_RELOC_PPC_TPREL16:
3102                       reloc = BFD_RELOC_PPC64_TPREL16_DS;
3103                       break;
3104                     case BFD_RELOC_PPC_TPREL16_LO:
3105                       reloc = BFD_RELOC_PPC64_TPREL16_LO_DS;
3106                       break;
3107                     case BFD_RELOC_PPC_GOT_DTPREL16:
3108                     case BFD_RELOC_PPC_GOT_DTPREL16_LO:
3109                     case BFD_RELOC_PPC_GOT_TPREL16:
3110                     case BFD_RELOC_PPC_GOT_TPREL16_LO:
3111                       break;
3112                     default:
3113                       as_bad (_("unsupported relocation for DS offset field"));
3114                       break;
3115                     }
3116                 }
3117             }
3118 #endif /* OBJ_ELF */
3119
3120           if (reloc != BFD_RELOC_UNUSED)
3121             ;
3122           /* Determine a BFD reloc value based on the operand information.
3123              We are only prepared to turn a few of the operands into
3124              relocs.  */
3125           else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3126                    && operand->bitm == 0x3fffffc
3127                    && operand->shift == 0)
3128             reloc = BFD_RELOC_PPC_B26;
3129           else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3130                    && operand->bitm == 0xfffc
3131                    && operand->shift == 0)
3132             reloc = BFD_RELOC_PPC_B16;
3133           else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3134                    && operand->bitm == 0x1fe
3135                    && operand->shift == -1)
3136             reloc = BFD_RELOC_PPC_VLE_REL8;
3137           else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3138                    && operand->bitm == 0xfffe
3139                    && operand->shift == 0)
3140             reloc = BFD_RELOC_PPC_VLE_REL15;
3141           else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3142                    && operand->bitm == 0x1fffffe
3143                    && operand->shift == 0)
3144             reloc = BFD_RELOC_PPC_VLE_REL24;
3145           else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0
3146                    && operand->bitm == 0x3fffffc
3147                    && operand->shift == 0)
3148             reloc = BFD_RELOC_PPC_BA26;
3149           else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0
3150                    && operand->bitm == 0xfffc
3151                    && operand->shift == 0)
3152             reloc = BFD_RELOC_PPC_BA16;
3153 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
3154           else if ((operand->flags & PPC_OPERAND_PARENS) != 0
3155                    && (operand->bitm & 0xfff0) == 0xfff0
3156                    && operand->shift == 0)
3157             {
3158               if (ppc_is_toc_sym (ex.X_add_symbol))
3159                 {
3160                   reloc = BFD_RELOC_PPC_TOC16;
3161 #ifdef OBJ_ELF
3162                   if (ppc_obj64
3163                       && (operand->flags & PPC_OPERAND_DS) != 0)
3164                     reloc = BFD_RELOC_PPC64_TOC16_DS;
3165 #endif
3166                 }
3167               else
3168                 {
3169                   reloc = BFD_RELOC_16;
3170 #ifdef OBJ_ELF
3171                   if (ppc_obj64
3172                       && (operand->flags & PPC_OPERAND_DS) != 0)
3173                     reloc = BFD_RELOC_PPC64_ADDR16_DS;
3174 #endif
3175                 }
3176             }
3177 #endif /* defined (OBJ_XCOFF) || defined (OBJ_ELF) */
3178
3179           /* We need to generate a fixup for this expression.  */
3180           if (fc >= MAX_INSN_FIXUPS)
3181             as_fatal (_("too many fixups"));
3182           fixups[fc].exp = ex;
3183           fixups[fc].opindex = *opindex_ptr;
3184           fixups[fc].reloc = reloc;
3185           ++fc;
3186         }
3187
3188       if (need_paren)
3189         {
3190           endc = ')';
3191           need_paren = 0;
3192           /* If expecting more operands, then we want to see "),".  */
3193           if (*str == endc && opindex_ptr[1] != 0)
3194             {
3195               do
3196                 ++str;
3197               while (ISSPACE (*str));
3198               endc = ',';
3199             }
3200         }
3201       else if ((operand->flags & PPC_OPERAND_PARENS) != 0)
3202         {
3203           endc = '(';
3204           need_paren = 1;
3205         }
3206       else
3207         endc = ',';
3208
3209       /* The call to expression should have advanced str past any
3210          whitespace.  */
3211       if (*str != endc
3212           && (endc != ',' || *str != '\0'))
3213         {
3214           if (*str == '\0')
3215             as_bad (_("syntax error; end of line, expected `%c'"), endc);
3216           else
3217             as_bad (_("syntax error; found `%c', expected `%c'"), *str, endc);
3218           break;
3219         }
3220
3221       if (*str != '\0')
3222         ++str;
3223     }
3224
3225   while (ISSPACE (*str))
3226     ++str;
3227
3228   if (*str != '\0')
3229     as_bad (_("junk at end of line: `%s'"), str);
3230
3231 #ifdef OBJ_ELF
3232   /* Do we need/want an APUinfo section? */
3233   if ((ppc_cpu & (PPC_OPCODE_E500 | PPC_OPCODE_E500MC | PPC_OPCODE_VLE)) != 0)
3234     {
3235       /* These are all version "1".  */
3236       if (opcode->flags & PPC_OPCODE_SPE)
3237         ppc_apuinfo_section_add (PPC_APUINFO_SPE, 1);
3238       if (opcode->flags & PPC_OPCODE_ISEL)
3239         ppc_apuinfo_section_add (PPC_APUINFO_ISEL, 1);
3240       if (opcode->flags & PPC_OPCODE_EFS)
3241         ppc_apuinfo_section_add (PPC_APUINFO_EFS, 1);
3242       if (opcode->flags & PPC_OPCODE_BRLOCK)
3243         ppc_apuinfo_section_add (PPC_APUINFO_BRLOCK, 1);
3244       if (opcode->flags & PPC_OPCODE_PMR)
3245         ppc_apuinfo_section_add (PPC_APUINFO_PMR, 1);
3246       if (opcode->flags & PPC_OPCODE_CACHELCK)
3247         ppc_apuinfo_section_add (PPC_APUINFO_CACHELCK, 1);
3248       if (opcode->flags & PPC_OPCODE_RFMCI)
3249         ppc_apuinfo_section_add (PPC_APUINFO_RFMCI, 1);
3250       if (opcode->flags & PPC_OPCODE_VLE)
3251         ppc_apuinfo_section_add (PPC_APUINFO_VLE, 1);
3252     }
3253 #endif
3254
3255   /* Write out the instruction.  */
3256   /* Differentiate between two and four byte insns.  */
3257   if (ppc_mach () == bfd_mach_ppc_vle)
3258     {
3259       if (PPC_OP_SE_VLE (insn))
3260         insn_length = 2;
3261       else
3262         insn_length = 4;
3263       addr_mod = frag_now_fix () & 1;
3264     }
3265   else
3266     {
3267       insn_length = 4;
3268       addr_mod = frag_now_fix () & 3;
3269     }
3270   /* All instructions can start on a 2 byte boundary for VLE.  */
3271   f = frag_more (insn_length);
3272   if (frag_now->has_code && frag_now->insn_addr != addr_mod)
3273     {
3274       if (ppc_mach() == bfd_mach_ppc_vle)
3275         as_bad (_("instruction address is not a multiple of 2"));
3276       else
3277         as_bad (_("instruction address is not a multiple of 4"));
3278     }
3279   frag_now->insn_addr = addr_mod;
3280   frag_now->has_code = 1;
3281   md_number_to_chars (f, insn, insn_length);
3282
3283 #ifdef OBJ_ELF
3284   dwarf2_emit_insn (insn_length);
3285 #endif
3286
3287   /* Create any fixups.  */
3288   for (i = 0; i < fc; i++)
3289     {
3290       fixS *fixP;
3291       if (fixups[i].reloc != BFD_RELOC_UNUSED)
3292         {
3293           reloc_howto_type *reloc_howto;
3294           int size;
3295           int offset;
3296
3297           reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
3298           if (!reloc_howto)
3299             abort ();
3300
3301           size = bfd_get_reloc_size (reloc_howto);
3302           offset = target_big_endian ? (insn_length - size) : 0;
3303
3304           if (size < 1 || size > 4)
3305             abort ();
3306
3307           fixP = fix_new_exp (frag_now,
3308                               f - frag_now->fr_literal + offset,
3309                               size,
3310                               &fixups[i].exp,
3311                               reloc_howto->pc_relative,
3312                               fixups[i].reloc);
3313         }
3314       else
3315         {
3316           const struct powerpc_operand *operand;
3317
3318           operand = &powerpc_operands[fixups[i].opindex];
3319           fixP = fix_new_exp (frag_now,
3320                               f - frag_now->fr_literal,
3321                               insn_length,
3322                               &fixups[i].exp,
3323                               (operand->flags & PPC_OPERAND_RELATIVE) != 0,
3324                               BFD_RELOC_UNUSED);
3325         }
3326       fixP->fx_pcrel_adjust = fixups[i].opindex;
3327     }
3328 }
3329
3330 /* Handle a macro.  Gather all the operands, transform them as
3331    described by the macro, and call md_assemble recursively.  All the
3332    operands are separated by commas; we don't accept parentheses
3333    around operands here.  */
3334
3335 static void
3336 ppc_macro (char *str, const struct powerpc_macro *macro)
3337 {
3338   char *operands[10];
3339   unsigned int count;
3340   char *s;
3341   unsigned int len;
3342   const char *format;
3343   unsigned int arg;
3344   char *send;
3345   char *complete;
3346
3347   /* Gather the users operands into the operands array.  */
3348   count = 0;
3349   s = str;
3350   while (1)
3351     {
3352       if (count >= sizeof operands / sizeof operands[0])
3353         break;
3354       operands[count++] = s;
3355       s = strchr (s, ',');
3356       if (s == (char *) NULL)
3357         break;
3358       *s++ = '\0';
3359     }
3360
3361   if (count != macro->operands)
3362     {
3363       as_bad (_("wrong number of operands"));
3364       return;
3365     }
3366
3367   /* Work out how large the string must be (the size is unbounded
3368      because it includes user input).  */
3369   len = 0;
3370   format = macro->format;
3371   while (*format != '\0')
3372     {
3373       if (*format != '%')
3374         {
3375           ++len;
3376           ++format;
3377         }
3378       else
3379         {
3380           arg = strtol (format + 1, &send, 10);
3381           know (send != format && arg < count);
3382           len += strlen (operands[arg]);
3383           format = send;
3384         }
3385     }
3386
3387   /* Put the string together.  */
3388   complete = s = (char *) alloca (len + 1);
3389   format = macro->format;
3390   while (*format != '\0')
3391     {
3392       if (*format != '%')
3393         *s++ = *format++;
3394       else
3395         {
3396           arg = strtol (format + 1, &send, 10);
3397           strcpy (s, operands[arg]);
3398           s += strlen (s);
3399           format = send;
3400         }
3401     }
3402   *s = '\0';
3403
3404   /* Assemble the constructed instruction.  */
3405   md_assemble (complete);
3406 }
3407 \f
3408 #ifdef OBJ_ELF
3409 /* For ELF, add support for SHT_ORDERED.  */
3410
3411 int
3412 ppc_section_type (char *str, size_t len)
3413 {
3414   if (len == 7 && strncmp (str, "ordered", 7) == 0)
3415     return SHT_ORDERED;
3416
3417   return -1;
3418 }
3419
3420 int
3421 ppc_section_flags (flagword flags, bfd_vma attr ATTRIBUTE_UNUSED, int type)
3422 {
3423   if (type == SHT_ORDERED)
3424     flags |= SEC_ALLOC | SEC_LOAD | SEC_SORT_ENTRIES;
3425
3426   return flags;
3427 }
3428 #endif /* OBJ_ELF */
3429
3430 \f
3431 /* Pseudo-op handling.  */
3432
3433 /* The .byte pseudo-op.  This is similar to the normal .byte
3434    pseudo-op, but it can also take a single ASCII string.  */
3435
3436 static void
3437 ppc_byte (int ignore ATTRIBUTE_UNUSED)
3438 {
3439   if (*input_line_pointer != '\"')
3440     {
3441       cons (1);
3442       return;
3443     }
3444
3445   /* Gather characters.  A real double quote is doubled.  Unusual
3446      characters are not permitted.  */
3447   ++input_line_pointer;
3448   while (1)
3449     {
3450       char c;
3451
3452       c = *input_line_pointer++;
3453
3454       if (c == '\"')
3455         {
3456           if (*input_line_pointer != '\"')
3457             break;
3458           ++input_line_pointer;
3459         }
3460
3461       FRAG_APPEND_1_CHAR (c);
3462     }
3463
3464   demand_empty_rest_of_line ();
3465 }
3466 \f
3467 #ifdef OBJ_XCOFF
3468
3469 /* XCOFF specific pseudo-op handling.  */
3470
3471 /* This is set if we are creating a .stabx symbol, since we don't want
3472    to handle symbol suffixes for such symbols.  */
3473 static bfd_boolean ppc_stab_symbol;
3474
3475 /* The .comm and .lcomm pseudo-ops for XCOFF.  XCOFF puts common
3476    symbols in the .bss segment as though they were local common
3477    symbols, and uses a different smclas.  The native Aix 4.3.3 assembler
3478    aligns .comm and .lcomm to 4 bytes.  */
3479
3480 static void
3481 ppc_comm (int lcomm)
3482 {
3483   asection *current_seg = now_seg;
3484   subsegT current_subseg = now_subseg;
3485   char *name;
3486   char endc;
3487   char *end_name;
3488   offsetT size;
3489   offsetT align;
3490   symbolS *lcomm_sym = NULL;
3491   symbolS *sym;
3492   char *pfrag;
3493
3494   name = input_line_pointer;
3495   endc = get_symbol_end ();
3496   end_name = input_line_pointer;
3497   *end_name = endc;
3498
3499   if (*input_line_pointer != ',')
3500     {
3501       as_bad (_("missing size"));
3502       ignore_rest_of_line ();
3503       return;
3504     }
3505   ++input_line_pointer;
3506
3507   size = get_absolute_expression ();
3508   if (size < 0)
3509     {
3510       as_bad (_("negative size"));
3511       ignore_rest_of_line ();
3512       return;
3513     }
3514
3515   if (! lcomm)
3516     {
3517       /* The third argument to .comm is the alignment.  */
3518       if (*input_line_pointer != ',')
3519         align = 2;
3520       else
3521         {
3522           ++input_line_pointer;
3523           align = get_absolute_expression ();
3524           if (align <= 0)
3525             {
3526               as_warn (_("ignoring bad alignment"));
3527               align = 2;
3528             }
3529         }
3530     }
3531   else
3532     {
3533       char *lcomm_name;
3534       char lcomm_endc;
3535
3536       if (size <= 4)
3537         align = 2;
3538       else
3539         align = 3;
3540
3541       /* The third argument to .lcomm appears to be the real local
3542          common symbol to create.  References to the symbol named in
3543          the first argument are turned into references to the third
3544          argument.  */
3545       if (*input_line_pointer != ',')
3546         {
3547           as_bad (_("missing real symbol name"));
3548           ignore_rest_of_line ();
3549           return;
3550         }
3551       ++input_line_pointer;
3552
3553       lcomm_name = input_line_pointer;
3554       lcomm_endc = get_symbol_end ();
3555
3556       lcomm_sym = symbol_find_or_make (lcomm_name);
3557
3558       *input_line_pointer = lcomm_endc;
3559     }
3560
3561   *end_name = '\0';
3562   sym = symbol_find_or_make (name);
3563   *end_name = endc;
3564
3565   if (S_IS_DEFINED (sym)
3566       || S_GET_VALUE (sym) != 0)
3567     {
3568       as_bad (_("attempt to redefine symbol"));
3569       ignore_rest_of_line ();
3570       return;
3571     }
3572
3573   record_alignment (bss_section, align);
3574
3575   if (! lcomm
3576       || ! S_IS_DEFINED (lcomm_sym))
3577     {
3578       symbolS *def_sym;
3579       offsetT def_size;
3580
3581       if (! lcomm)
3582         {
3583           def_sym = sym;
3584           def_size = size;
3585           S_SET_EXTERNAL (sym);
3586         }
3587       else
3588         {
3589           symbol_get_tc (lcomm_sym)->output = 1;
3590           def_sym = lcomm_sym;
3591           def_size = 0;
3592         }
3593
3594       subseg_set (bss_section, 1);
3595       frag_align (align, 0, 0);
3596
3597       symbol_set_frag (def_sym, frag_now);
3598       pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, def_sym,
3599                         def_size, (char *) NULL);
3600       *pfrag = 0;
3601       S_SET_SEGMENT (def_sym, bss_section);
3602       symbol_get_tc (def_sym)->align = align;
3603     }
3604   else if (lcomm)
3605     {
3606       /* Align the size of lcomm_sym.  */
3607       symbol_get_frag (lcomm_sym)->fr_offset =
3608         ((symbol_get_frag (lcomm_sym)->fr_offset + (1 << align) - 1)
3609          &~ ((1 << align) - 1));
3610       if (align > symbol_get_tc (lcomm_sym)->align)
3611         symbol_get_tc (lcomm_sym)->align = align;
3612     }
3613
3614   if (lcomm)
3615     {
3616       /* Make sym an offset from lcomm_sym.  */
3617       S_SET_SEGMENT (sym, bss_section);
3618       symbol_set_frag (sym, symbol_get_frag (lcomm_sym));
3619       S_SET_VALUE (sym, symbol_get_frag (lcomm_sym)->fr_offset);
3620       symbol_get_frag (lcomm_sym)->fr_offset += size;
3621     }
3622
3623   subseg_set (current_seg, current_subseg);
3624
3625   demand_empty_rest_of_line ();
3626 }
3627
3628 /* The .csect pseudo-op.  This switches us into a different
3629    subsegment.  The first argument is a symbol whose value is the
3630    start of the .csect.  In COFF, csect symbols get special aux
3631    entries defined by the x_csect field of union internal_auxent.  The
3632    optional second argument is the alignment (the default is 2).  */
3633
3634 static void
3635 ppc_csect (int ignore ATTRIBUTE_UNUSED)
3636 {
3637   char *name;
3638   char endc;
3639   symbolS *sym;
3640   offsetT align;
3641
3642   name = input_line_pointer;
3643   endc = get_symbol_end ();
3644
3645   sym = symbol_find_or_make (name);
3646
3647   *input_line_pointer = endc;
3648
3649   if (S_GET_NAME (sym)[0] == '\0')
3650     {
3651       /* An unnamed csect is assumed to be [PR].  */
3652       symbol_get_tc (sym)->symbol_class = XMC_PR;
3653     }
3654
3655   align = 2;
3656   if (*input_line_pointer == ',')
3657     {
3658       ++input_line_pointer;
3659       align = get_absolute_expression ();
3660     }
3661
3662   ppc_change_csect (sym, align);
3663
3664   demand_empty_rest_of_line ();
3665 }
3666
3667 /* Change to a different csect.  */
3668
3669 static void
3670 ppc_change_csect (symbolS *sym, offsetT align)
3671 {
3672   if (S_IS_DEFINED (sym))
3673     subseg_set (S_GET_SEGMENT (sym), symbol_get_tc (sym)->subseg);
3674   else
3675     {
3676       symbolS **list_ptr;
3677       int after_toc;
3678       int hold_chunksize;
3679       symbolS *list;
3680       int is_code;
3681       segT sec;
3682
3683       /* This is a new csect.  We need to look at the symbol class to
3684          figure out whether it should go in the text section or the
3685          data section.  */
3686       after_toc = 0;
3687       is_code = 0;
3688       switch (symbol_get_tc (sym)->symbol_class)
3689         {
3690         case XMC_PR:
3691         case XMC_RO:
3692         case XMC_DB:
3693         case XMC_GL:
3694         case XMC_XO:
3695         case XMC_SV:
3696         case XMC_TI:
3697         case XMC_TB:
3698           S_SET_SEGMENT (sym, text_section);
3699           symbol_get_tc (sym)->subseg = ppc_text_subsegment;
3700           ++ppc_text_subsegment;
3701           list_ptr = &ppc_text_csects;
3702           is_code = 1;
3703           break;
3704         case XMC_RW:
3705         case XMC_TC0:
3706         case XMC_TC:
3707         case XMC_DS:
3708         case XMC_UA:
3709         case XMC_BS:
3710         case XMC_UC:
3711           if (ppc_toc_csect != NULL
3712               && (symbol_get_tc (ppc_toc_csect)->subseg + 1
3713                   == ppc_data_subsegment))
3714             after_toc = 1;
3715           S_SET_SEGMENT (sym, data_section);
3716           symbol_get_tc (sym)->subseg = ppc_data_subsegment;
3717           ++ppc_data_subsegment;
3718           list_ptr = &ppc_data_csects;
3719           break;
3720         default:
3721           abort ();
3722         }
3723
3724       /* We set the obstack chunk size to a small value before
3725          changing subsegments, so that we don't use a lot of memory
3726          space for what may be a small section.  */
3727       hold_chunksize = chunksize;
3728       chunksize = 64;
3729
3730       sec = subseg_new (segment_name (S_GET_SEGMENT (sym)),
3731                         symbol_get_tc (sym)->subseg);
3732
3733       chunksize = hold_chunksize;
3734
3735       if (after_toc)
3736         ppc_after_toc_frag = frag_now;
3737
3738       record_alignment (sec, align);
3739       if (is_code)
3740         frag_align_code (align, 0);
3741       else
3742         frag_align (align, 0, 0);
3743
3744       symbol_set_frag (sym, frag_now);
3745       S_SET_VALUE (sym, (valueT) frag_now_fix ());
3746
3747       symbol_get_tc (sym)->align = align;
3748       symbol_get_tc (sym)->output = 1;
3749       symbol_get_tc (sym)->within = sym;
3750
3751       for (list = *list_ptr;
3752            symbol_get_tc (list)->next != (symbolS *) NULL;
3753            list = symbol_get_tc (list)->next)
3754         ;
3755       symbol_get_tc (list)->next = sym;
3756
3757       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
3758       symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
3759                      &symbol_lastP);
3760     }
3761
3762   ppc_current_csect = sym;
3763 }
3764
3765 static void
3766 ppc_change_debug_section (unsigned int idx, subsegT subseg)
3767 {
3768   segT sec;
3769   flagword oldflags;
3770   const struct xcoff_dwsect_name *dw = &xcoff_dwsect_names[idx];
3771
3772   sec = subseg_new (dw->name, subseg);
3773   oldflags = bfd_get_section_flags (stdoutput, sec);
3774   if (oldflags == SEC_NO_FLAGS)
3775     {
3776       /* Just created section.  */
3777       gas_assert (dw_sections[idx].sect == NULL);
3778
3779       bfd_set_section_flags (stdoutput, sec, SEC_DEBUGGING);
3780       bfd_set_section_alignment (stdoutput, sec, 0);
3781       dw_sections[idx].sect = sec;
3782     }
3783
3784   /* Not anymore in a csect.  */
3785   ppc_current_csect = NULL;
3786 }
3787
3788 /* The .dwsect pseudo-op.  Defines a DWARF section.  Syntax is:
3789      .dwsect flag [, opt-label ]
3790 */
3791
3792 static void
3793 ppc_dwsect (int ignore ATTRIBUTE_UNUSED)
3794 {
3795   offsetT flag;
3796   symbolS *opt_label;
3797   const struct xcoff_dwsect_name *dw;
3798   struct dw_subsection *subseg;
3799   struct dw_section *dws;
3800   int i;
3801
3802   /* Find section.  */
3803   flag = get_absolute_expression ();
3804   dw = NULL;
3805   for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
3806     if (xcoff_dwsect_names[i].flag == flag)
3807       {
3808         dw = &xcoff_dwsect_names[i];
3809         break;
3810       }
3811
3812   /* Parse opt-label.  */
3813   if (*input_line_pointer == ',')
3814     {
3815       const char *label;
3816       char c;
3817
3818       ++input_line_pointer;
3819
3820       label = input_line_pointer;
3821       c = get_symbol_end ();
3822       opt_label = symbol_find_or_make (label);
3823       *input_line_pointer = c;
3824     }
3825   else
3826     opt_label = NULL;
3827
3828   demand_empty_rest_of_line ();
3829
3830   /* Return now in case of unknown subsection.  */
3831   if (dw == NULL)
3832     {
3833       as_bad (_("no known dwarf XCOFF section for flag 0x%08x\n"),
3834               (unsigned)flag);
3835       return;
3836     }
3837
3838   /* Find the subsection.  */
3839   dws = &dw_sections[i];
3840   subseg = NULL;
3841   if (opt_label != NULL && S_IS_DEFINED (opt_label))
3842     {
3843       /* Sanity check (note that in theory S_GET_SEGMENT mustn't be null).  */
3844       if (dws->sect == NULL || S_GET_SEGMENT (opt_label) != dws->sect)
3845         {
3846           as_bad (_("label %s was not defined in this dwarf section"),
3847                   S_GET_NAME (opt_label));
3848           subseg = dws->anon_subseg;
3849           opt_label = NULL;
3850         }
3851       else
3852         subseg = symbol_get_tc (opt_label)->u.dw;
3853     }
3854
3855   if (subseg != NULL)
3856     {
3857       /* Switch to the subsection.  */
3858       ppc_change_debug_section (i, subseg->subseg);
3859     }
3860   else
3861     {
3862       /* Create a new dw subsection.  */
3863       subseg = (struct dw_subsection *)
3864         xmalloc (sizeof (struct dw_subsection));
3865
3866       if (opt_label == NULL)
3867         {
3868           /* The anonymous one.  */
3869           subseg->subseg = 0;
3870           subseg->link = NULL;
3871           dws->anon_subseg = subseg;
3872         }
3873       else
3874         {
3875           /* A named one.  */
3876           if (dws->list_subseg != NULL)
3877             subseg->subseg = dws->list_subseg->subseg + 1;
3878           else
3879             subseg->subseg = 1;
3880
3881           subseg->link = dws->list_subseg;
3882           dws->list_subseg = subseg;
3883           symbol_get_tc (opt_label)->u.dw = subseg;
3884         }
3885
3886       ppc_change_debug_section (i, subseg->subseg);
3887
3888       if (dw->def_size)
3889         {
3890           /* Add the length field.  */
3891           expressionS *exp = &subseg->end_exp;
3892           int sz;
3893
3894           if (opt_label != NULL)
3895             symbol_set_value_now (opt_label);
3896
3897           /* Add the length field.  Note that according to the AIX assembler
3898              manual, the size of the length field is 4 for powerpc32 but
3899              12 for powerpc64.  */
3900           if (ppc_obj64)
3901             {
3902               /* Write the 64bit marker.  */
3903               md_number_to_chars (frag_more (4), -1, 4);
3904             }
3905
3906           exp->X_op = O_subtract;
3907           exp->X_op_symbol = symbol_temp_new_now ();
3908           exp->X_add_symbol = symbol_temp_make ();
3909
3910           sz = ppc_obj64 ? 8 : 4;
3911           exp->X_add_number = -sz;
3912           emit_expr (exp, sz);
3913         }
3914     }
3915 }
3916
3917 /* This function handles the .text and .data pseudo-ops.  These
3918    pseudo-ops aren't really used by XCOFF; we implement them for the
3919    convenience of people who aren't used to XCOFF.  */
3920
3921 static void
3922 ppc_section (int type)
3923 {
3924   const char *name;
3925   symbolS *sym;
3926
3927   if (type == 't')
3928     name = ".text[PR]";
3929   else if (type == 'd')
3930     name = ".data[RW]";
3931   else
3932     abort ();
3933
3934   sym = symbol_find_or_make (name);
3935
3936   ppc_change_csect (sym, 2);
3937
3938   demand_empty_rest_of_line ();
3939 }
3940
3941 /* This function handles the .section pseudo-op.  This is mostly to
3942    give an error, since XCOFF only supports .text, .data and .bss, but
3943    we do permit the user to name the text or data section.  */
3944
3945 static void
3946 ppc_named_section (int ignore ATTRIBUTE_UNUSED)
3947 {
3948   char *user_name;
3949   const char *real_name;
3950   char c;
3951   symbolS *sym;
3952
3953   user_name = input_line_pointer;
3954   c = get_symbol_end ();
3955
3956   if (strcmp (user_name, ".text") == 0)
3957     real_name = ".text[PR]";
3958   else if (strcmp (user_name, ".data") == 0)
3959     real_name = ".data[RW]";
3960   else
3961     {
3962       as_bad (_("the XCOFF file format does not support arbitrary sections"));
3963       *input_line_pointer = c;
3964       ignore_rest_of_line ();
3965       return;
3966     }
3967
3968   *input_line_pointer = c;
3969
3970   sym = symbol_find_or_make (real_name);
3971
3972   ppc_change_csect (sym, 2);
3973
3974   demand_empty_rest_of_line ();
3975 }
3976
3977 /* The .extern pseudo-op.  We create an undefined symbol.  */
3978
3979 static void
3980 ppc_extern (int ignore ATTRIBUTE_UNUSED)
3981 {
3982   char *name;
3983   char endc;
3984
3985   name = input_line_pointer;
3986   endc = get_symbol_end ();
3987
3988   (void) symbol_find_or_make (name);
3989
3990   *input_line_pointer = endc;
3991
3992   demand_empty_rest_of_line ();
3993 }
3994
3995 /* The .lglobl pseudo-op.  Keep the symbol in the symbol table.  */
3996
3997 static void
3998 ppc_lglobl (int ignore ATTRIBUTE_UNUSED)
3999 {
4000   char *name;
4001   char endc;
4002   symbolS *sym;
4003
4004   name = input_line_pointer;
4005   endc = get_symbol_end ();
4006
4007   sym = symbol_find_or_make (name);
4008
4009   *input_line_pointer = endc;
4010
4011   symbol_get_tc (sym)->output = 1;
4012
4013   demand_empty_rest_of_line ();
4014 }
4015
4016 /* The .ref pseudo-op.  It takes a list of symbol names and inserts R_REF
4017    relocations at the beginning of the current csect.
4018
4019    (In principle, there's no reason why the relocations _have_ to be at
4020    the beginning.  Anywhere in the csect would do.  However, inserting
4021    at the beginning is what the native assmebler does, and it helps to
4022    deal with cases where the .ref statements follow the section contents.)
4023
4024    ??? .refs don't work for empty .csects.  However, the native assembler
4025    doesn't report an error in this case, and neither yet do we.  */
4026
4027 static void
4028 ppc_ref (int ignore ATTRIBUTE_UNUSED)
4029 {
4030   char *name;
4031   char c;
4032
4033   if (ppc_current_csect == NULL)
4034     {
4035       as_bad (_(".ref outside .csect"));
4036       ignore_rest_of_line ();
4037       return;
4038     }
4039
4040   do
4041     {
4042       name = input_line_pointer;
4043       c = get_symbol_end ();
4044
4045       fix_at_start (symbol_get_frag (ppc_current_csect), 0,
4046                     symbol_find_or_make (name), 0, FALSE, BFD_RELOC_NONE);
4047
4048       *input_line_pointer = c;
4049       SKIP_WHITESPACE ();
4050       c = *input_line_pointer;
4051       if (c == ',')
4052         {
4053           input_line_pointer++;
4054           SKIP_WHITESPACE ();
4055           if (is_end_of_line[(unsigned char) *input_line_pointer])
4056             {
4057               as_bad (_("missing symbol name"));
4058               ignore_rest_of_line ();
4059               return;
4060             }
4061         }
4062     }
4063   while (c == ',');
4064
4065   demand_empty_rest_of_line ();
4066 }
4067
4068 /* The .rename pseudo-op.  The RS/6000 assembler can rename symbols,
4069    although I don't know why it bothers.  */
4070
4071 static void
4072 ppc_rename (int ignore ATTRIBUTE_UNUSED)
4073 {
4074   char *name;
4075   char endc;
4076   symbolS *sym;
4077   int len;
4078
4079   name = input_line_pointer;
4080   endc = get_symbol_end ();
4081
4082   sym = symbol_find_or_make (name);
4083
4084   *input_line_pointer = endc;
4085
4086   if (*input_line_pointer != ',')
4087     {
4088       as_bad (_("missing rename string"));
4089       ignore_rest_of_line ();
4090       return;
4091     }
4092   ++input_line_pointer;
4093
4094   symbol_get_tc (sym)->real_name = demand_copy_C_string (&len);
4095
4096   demand_empty_rest_of_line ();
4097 }
4098
4099 /* The .stabx pseudo-op.  This is similar to a normal .stabs
4100    pseudo-op, but slightly different.  A sample is
4101        .stabx "main:F-1",.main,142,0
4102    The first argument is the symbol name to create.  The second is the
4103    value, and the third is the storage class.  The fourth seems to be
4104    always zero, and I am assuming it is the type.  */
4105
4106 static void
4107 ppc_stabx (int ignore ATTRIBUTE_UNUSED)
4108 {
4109   char *name;
4110   int len;
4111   symbolS *sym;
4112   expressionS exp;
4113
4114   name = demand_copy_C_string (&len);
4115
4116   if (*input_line_pointer != ',')
4117     {
4118       as_bad (_("missing value"));
4119       return;
4120     }
4121   ++input_line_pointer;
4122
4123   ppc_stab_symbol = TRUE;
4124   sym = symbol_make (name);
4125   ppc_stab_symbol = FALSE;
4126
4127   symbol_get_tc (sym)->real_name = name;
4128
4129   (void) expression (&exp);
4130
4131   switch (exp.X_op)
4132     {
4133     case O_illegal:
4134     case O_absent:
4135     case O_big:
4136       as_bad (_("illegal .stabx expression; zero assumed"));
4137       exp.X_add_number = 0;
4138       /* Fall through.  */
4139     case O_constant:
4140       S_SET_VALUE (sym, (valueT) exp.X_add_number);
4141       symbol_set_frag (sym, &zero_address_frag);
4142       break;
4143
4144     case O_symbol:
4145       if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section)
4146         symbol_set_value_expression (sym, &exp);
4147       else
4148         {
4149           S_SET_VALUE (sym,
4150                        exp.X_add_number + S_GET_VALUE (exp.X_add_symbol));
4151           symbol_set_frag (sym, symbol_get_frag (exp.X_add_symbol));
4152         }
4153       break;
4154
4155     default:
4156       /* The value is some complex expression.  This will probably
4157          fail at some later point, but this is probably the right
4158          thing to do here.  */
4159       symbol_set_value_expression (sym, &exp);
4160       break;
4161     }
4162
4163   S_SET_SEGMENT (sym, ppc_coff_debug_section);
4164   symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
4165
4166   if (*input_line_pointer != ',')
4167     {
4168       as_bad (_("missing class"));
4169       return;
4170     }
4171   ++input_line_pointer;
4172
4173   S_SET_STORAGE_CLASS (sym, get_absolute_expression ());
4174
4175   if (*input_line_pointer != ',')
4176     {
4177       as_bad (_("missing type"));
4178       return;
4179     }
4180   ++input_line_pointer;
4181
4182   S_SET_DATA_TYPE (sym, get_absolute_expression ());
4183
4184   symbol_get_tc (sym)->output = 1;
4185
4186   if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
4187     {
4188       /* In this case :
4189
4190          .bs name
4191          .stabx "z",arrays_,133,0
4192          .es
4193
4194          .comm arrays_,13768,3
4195
4196          resolve_symbol_value will copy the exp's "within" into sym's when the
4197          offset is 0.  Since this seems to be corner case problem,
4198          only do the correction for storage class C_STSYM.  A better solution
4199          would be to have the tc field updated in ppc_symbol_new_hook.  */
4200
4201       if (exp.X_op == O_symbol)
4202         {
4203           if (ppc_current_block == NULL)
4204             as_bad (_(".stabx of storage class stsym must be within .bs/.es"));
4205
4206           symbol_get_tc (sym)->within = ppc_current_block;
4207           symbol_get_tc (exp.X_add_symbol)->within = ppc_current_block;
4208         }
4209     }
4210
4211   if (exp.X_op != O_symbol
4212       || ! S_IS_EXTERNAL (exp.X_add_symbol)
4213       || S_GET_SEGMENT (exp.X_add_symbol) != bss_section)
4214     ppc_frob_label (sym);
4215   else
4216     {
4217       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
4218       symbol_append (sym, exp.X_add_symbol, &symbol_rootP, &symbol_lastP);
4219       if (symbol_get_tc (ppc_current_csect)->within == exp.X_add_symbol)
4220         symbol_get_tc (ppc_current_csect)->within = sym;
4221     }
4222
4223   demand_empty_rest_of_line ();
4224 }
4225
4226 /* The .function pseudo-op.  This takes several arguments.  The first
4227    argument seems to be the external name of the symbol.  The second
4228    argument seems to be the label for the start of the function.  gcc
4229    uses the same name for both.  I have no idea what the third and
4230    fourth arguments are meant to be.  The optional fifth argument is
4231    an expression for the size of the function.  In COFF this symbol
4232    gets an aux entry like that used for a csect.  */
4233
4234 static void
4235 ppc_function (int ignore ATTRIBUTE_UNUSED)
4236 {
4237   char *name;
4238   char endc;
4239   char *s;
4240   symbolS *ext_sym;
4241   symbolS *lab_sym;
4242
4243   name = input_line_pointer;
4244   endc = get_symbol_end ();
4245
4246   /* Ignore any [PR] suffix.  */
4247   name = ppc_canonicalize_symbol_name (name);
4248   s = strchr (name, '[');
4249   if (s != (char *) NULL
4250       && strcmp (s + 1, "PR]") == 0)
4251     *s = '\0';
4252
4253   ext_sym = symbol_find_or_make (name);
4254
4255   *input_line_pointer = endc;
4256
4257   if (*input_line_pointer != ',')
4258     {
4259       as_bad (_("missing symbol name"));
4260       ignore_rest_of_line ();
4261       return;
4262     }
4263   ++input_line_pointer;
4264
4265   name = input_line_pointer;
4266   endc = get_symbol_end ();
4267
4268   lab_sym = symbol_find_or_make (name);
4269
4270   *input_line_pointer = endc;
4271
4272   if (ext_sym != lab_sym)
4273     {
4274       expressionS exp;
4275
4276       exp.X_op = O_symbol;
4277       exp.X_add_symbol = lab_sym;
4278       exp.X_op_symbol = NULL;
4279       exp.X_add_number = 0;
4280       exp.X_unsigned = 0;
4281       symbol_set_value_expression (ext_sym, &exp);
4282     }
4283
4284   if (symbol_get_tc (ext_sym)->symbol_class == -1)
4285     symbol_get_tc (ext_sym)->symbol_class = XMC_PR;
4286   symbol_get_tc (ext_sym)->output = 1;
4287
4288   if (*input_line_pointer == ',')
4289     {
4290       expressionS exp;
4291
4292       /* Ignore the third argument.  */
4293       ++input_line_pointer;
4294       expression (& exp);
4295       if (*input_line_pointer == ',')
4296         {
4297           /* Ignore the fourth argument.  */
4298           ++input_line_pointer;
4299           expression (& exp);
4300           if (*input_line_pointer == ',')
4301             {
4302               /* The fifth argument is the function size.  */
4303               ++input_line_pointer;
4304               symbol_get_tc (ext_sym)->u.size = symbol_new
4305                 ("L0\001", absolute_section,(valueT) 0, &zero_address_frag);
4306               pseudo_set (symbol_get_tc (ext_sym)->u.size);
4307             }
4308         }
4309     }
4310
4311   S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
4312   SF_SET_FUNCTION (ext_sym);
4313   SF_SET_PROCESS (ext_sym);
4314   coff_add_linesym (ext_sym);
4315
4316   demand_empty_rest_of_line ();
4317 }
4318
4319 /* The .bf pseudo-op.  This is just like a COFF C_FCN symbol named
4320    ".bf".  If the pseudo op .bi was seen before .bf, patch the .bi sym
4321    with the correct line number */
4322
4323 static symbolS *saved_bi_sym = 0;
4324
4325 static void
4326 ppc_bf (int ignore ATTRIBUTE_UNUSED)
4327 {
4328   symbolS *sym;
4329
4330   sym = symbol_make (".bf");
4331   S_SET_SEGMENT (sym, text_section);
4332   symbol_set_frag (sym, frag_now);
4333   S_SET_VALUE (sym, frag_now_fix ());
4334   S_SET_STORAGE_CLASS (sym, C_FCN);
4335
4336   coff_line_base = get_absolute_expression ();
4337
4338   S_SET_NUMBER_AUXILIARY (sym, 1);
4339   SA_SET_SYM_LNNO (sym, coff_line_base);
4340
4341   /* Line number for bi.  */
4342   if (saved_bi_sym)
4343     {
4344       S_SET_VALUE (saved_bi_sym, coff_n_line_nos);
4345       saved_bi_sym = 0;
4346     }
4347
4348
4349   symbol_get_tc (sym)->output = 1;
4350
4351   ppc_frob_label (sym);
4352
4353   demand_empty_rest_of_line ();
4354 }
4355
4356 /* The .ef pseudo-op.  This is just like a COFF C_FCN symbol named
4357    ".ef", except that the line number is absolute, not relative to the
4358    most recent ".bf" symbol.  */
4359
4360 static void
4361 ppc_ef (int ignore ATTRIBUTE_UNUSED)
4362 {
4363   symbolS *sym;
4364
4365   sym = symbol_make (".ef");
4366   S_SET_SEGMENT (sym, text_section);
4367   symbol_set_frag (sym, frag_now);
4368   S_SET_VALUE (sym, frag_now_fix ());
4369   S_SET_STORAGE_CLASS (sym, C_FCN);
4370   S_SET_NUMBER_AUXILIARY (sym, 1);
4371   SA_SET_SYM_LNNO (sym, get_absolute_expression ());
4372   symbol_get_tc (sym)->output = 1;
4373
4374   ppc_frob_label (sym);
4375
4376   demand_empty_rest_of_line ();
4377 }
4378
4379 /* The .bi and .ei pseudo-ops.  These take a string argument and
4380    generates a C_BINCL or C_EINCL symbol, which goes at the start of
4381    the symbol list.  The value of .bi will be know when the next .bf
4382    is encountered.  */
4383
4384 static void
4385 ppc_biei (int ei)
4386 {
4387   static symbolS *last_biei;
4388
4389   char *name;
4390   int len;
4391   symbolS *sym;
4392   symbolS *look;
4393
4394   name = demand_copy_C_string (&len);
4395
4396   /* The value of these symbols is actually file offset.  Here we set
4397      the value to the index into the line number entries.  In
4398      ppc_frob_symbols we set the fix_line field, which will cause BFD
4399      to do the right thing.  */
4400
4401   sym = symbol_make (name);
4402   /* obj-coff.c currently only handles line numbers correctly in the
4403      .text section.  */
4404   S_SET_SEGMENT (sym, text_section);
4405   S_SET_VALUE (sym, coff_n_line_nos);
4406   symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
4407
4408   S_SET_STORAGE_CLASS (sym, ei ? C_EINCL : C_BINCL);
4409   symbol_get_tc (sym)->output = 1;
4410
4411   /* Save bi.  */
4412   if (ei)
4413     saved_bi_sym = 0;
4414   else
4415     saved_bi_sym = sym;
4416
4417   for (look = last_biei ? last_biei : symbol_rootP;
4418        (look != (symbolS *) NULL
4419         && (S_GET_STORAGE_CLASS (look) == C_FILE
4420             || S_GET_STORAGE_CLASS (look) == C_BINCL
4421             || S_GET_STORAGE_CLASS (look) == C_EINCL));
4422        look = symbol_next (look))
4423     ;
4424   if (look != (symbolS *) NULL)
4425     {
4426       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
4427       symbol_insert (sym, look, &symbol_rootP, &symbol_lastP);
4428       last_biei = sym;
4429     }
4430
4431   demand_empty_rest_of_line ();
4432 }
4433
4434 /* The .bs pseudo-op.  This generates a C_BSTAT symbol named ".bs".
4435    There is one argument, which is a csect symbol.  The value of the
4436    .bs symbol is the index of this csect symbol.  */
4437
4438 static void
4439 ppc_bs (int ignore ATTRIBUTE_UNUSED)
4440 {
4441   char *name;
4442   char endc;
4443   symbolS *csect;
4444   symbolS *sym;
4445
4446   if (ppc_current_block != NULL)
4447     as_bad (_("nested .bs blocks"));
4448
4449   name = input_line_pointer;
4450   endc = get_symbol_end ();
4451
4452   csect = symbol_find_or_make (name);
4453
4454   *input_line_pointer = endc;
4455
4456   sym = symbol_make (".bs");
4457   S_SET_SEGMENT (sym, now_seg);
4458   S_SET_STORAGE_CLASS (sym, C_BSTAT);
4459   symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
4460   symbol_get_tc (sym)->output = 1;
4461
4462   symbol_get_tc (sym)->within = csect;
4463
4464   ppc_frob_label (sym);
4465
4466   ppc_current_block = sym;
4467
4468   demand_empty_rest_of_line ();
4469 }
4470
4471 /* The .es pseudo-op.  Generate a C_ESTART symbol named .es.  */
4472
4473 static void
4474 ppc_es (int ignore ATTRIBUTE_UNUSED)
4475 {
4476   symbolS *sym;
4477
4478   if (ppc_current_block == NULL)
4479     as_bad (_(".es without preceding .bs"));
4480
4481   sym = symbol_make (".es");
4482   S_SET_SEGMENT (sym, now_seg);
4483   S_SET_STORAGE_CLASS (sym, C_ESTAT);
4484   symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
4485   symbol_get_tc (sym)->output = 1;
4486
4487   ppc_frob_label (sym);
4488
4489   ppc_current_block = NULL;
4490
4491   demand_empty_rest_of_line ();
4492 }
4493
4494 /* The .bb pseudo-op.  Generate a C_BLOCK symbol named .bb, with a
4495    line number.  */
4496
4497 static void
4498 ppc_bb (int ignore ATTRIBUTE_UNUSED)
4499 {
4500   symbolS *sym;
4501
4502   sym = symbol_make (".bb");
4503   S_SET_SEGMENT (sym, text_section);
4504   symbol_set_frag (sym, frag_now);
4505   S_SET_VALUE (sym, frag_now_fix ());
4506   S_SET_STORAGE_CLASS (sym, C_BLOCK);
4507
4508   S_SET_NUMBER_AUXILIARY (sym, 1);
4509   SA_SET_SYM_LNNO (sym, get_absolute_expression ());
4510
4511   symbol_get_tc (sym)->output = 1;
4512
4513   SF_SET_PROCESS (sym);
4514
4515   ppc_frob_label (sym);
4516
4517   demand_empty_rest_of_line ();
4518 }
4519
4520 /* The .eb pseudo-op.  Generate a C_BLOCK symbol named .eb, with a
4521    line number.  */
4522
4523 static void
4524 ppc_eb (int ignore ATTRIBUTE_UNUSED)
4525 {
4526   symbolS *sym;
4527
4528   sym = symbol_make (".eb");
4529   S_SET_SEGMENT (sym, text_section);
4530   symbol_set_frag (sym, frag_now);
4531   S_SET_VALUE (sym, frag_now_fix ());
4532   S_SET_STORAGE_CLASS (sym, C_BLOCK);
4533   S_SET_NUMBER_AUXILIARY (sym, 1);
4534   SA_SET_SYM_LNNO (sym, get_absolute_expression ());
4535   symbol_get_tc (sym)->output = 1;
4536
4537   SF_SET_PROCESS (sym);
4538
4539   ppc_frob_label (sym);
4540
4541   demand_empty_rest_of_line ();
4542 }
4543
4544 /* The .bc pseudo-op.  This just creates a C_BCOMM symbol with a
4545    specified name.  */
4546
4547 static void
4548 ppc_bc (int ignore ATTRIBUTE_UNUSED)
4549 {
4550   char *name;
4551   int len;
4552   symbolS *sym;
4553
4554   name = demand_copy_C_string (&len);
4555   sym = symbol_make (name);
4556   S_SET_SEGMENT (sym, ppc_coff_debug_section);
4557   symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
4558   S_SET_STORAGE_CLASS (sym, C_BCOMM);
4559   S_SET_VALUE (sym, 0);
4560   symbol_get_tc (sym)->output = 1;
4561
4562   ppc_frob_label (sym);
4563
4564   demand_empty_rest_of_line ();
4565 }
4566
4567 /* The .ec pseudo-op.  This just creates a C_ECOMM symbol.  */
4568
4569 static void
4570 ppc_ec (int ignore ATTRIBUTE_UNUSED)
4571 {
4572   symbolS *sym;
4573
4574   sym = symbol_make (".ec");
4575   S_SET_SEGMENT (sym, ppc_coff_debug_section);
4576   symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
4577   S_SET_STORAGE_CLASS (sym, C_ECOMM);
4578   S_SET_VALUE (sym, 0);
4579   symbol_get_tc (sym)->output = 1;
4580
4581   ppc_frob_label (sym);
4582
4583   demand_empty_rest_of_line ();
4584 }
4585
4586 /* The .toc pseudo-op.  Switch to the .toc subsegment.  */
4587
4588 static void
4589 ppc_toc (int ignore ATTRIBUTE_UNUSED)
4590 {
4591   if (ppc_toc_csect != (symbolS *) NULL)
4592     subseg_set (data_section, symbol_get_tc (ppc_toc_csect)->subseg);
4593   else
4594     {
4595       subsegT subseg;
4596       symbolS *sym;
4597       symbolS *list;
4598
4599       subseg = ppc_data_subsegment;
4600       ++ppc_data_subsegment;
4601
4602       subseg_new (segment_name (data_section), subseg);
4603       ppc_toc_frag = frag_now;
4604
4605       sym = symbol_find_or_make ("TOC[TC0]");
4606       symbol_set_frag (sym, frag_now);
4607       S_SET_SEGMENT (sym, data_section);
4608       S_SET_VALUE (sym, (valueT) frag_now_fix ());
4609       symbol_get_tc (sym)->subseg = subseg;
4610       symbol_get_tc (sym)->output = 1;
4611       symbol_get_tc (sym)->within = sym;
4612
4613       ppc_toc_csect = sym;
4614
4615       for (list = ppc_data_csects;
4616            symbol_get_tc (list)->next != (symbolS *) NULL;
4617            list = symbol_get_tc (list)->next)
4618         ;
4619       symbol_get_tc (list)->next = sym;
4620
4621       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
4622       symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
4623                      &symbol_lastP);
4624     }
4625
4626   ppc_current_csect = ppc_toc_csect;
4627
4628   demand_empty_rest_of_line ();
4629 }
4630
4631 /* The AIX assembler automatically aligns the operands of a .long or
4632    .short pseudo-op, and we want to be compatible.  */
4633
4634 static void
4635 ppc_xcoff_cons (int log_size)
4636 {
4637   frag_align (log_size, 0, 0);
4638   record_alignment (now_seg, log_size);
4639   cons (1 << log_size);
4640 }
4641
4642 static void
4643 ppc_vbyte (int dummy ATTRIBUTE_UNUSED)
4644 {
4645   expressionS exp;
4646   int byte_count;
4647
4648   (void) expression (&exp);
4649
4650   if (exp.X_op != O_constant)
4651     {
4652       as_bad (_("non-constant byte count"));
4653       return;
4654     }
4655
4656   byte_count = exp.X_add_number;
4657
4658   if (*input_line_pointer != ',')
4659     {
4660       as_bad (_("missing value"));
4661       return;
4662     }
4663
4664   ++input_line_pointer;
4665   cons (byte_count);
4666 }
4667
4668 void
4669 ppc_xcoff_end (void)
4670 {
4671   int i;
4672
4673   for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
4674     {
4675       struct dw_section *dws = &dw_sections[i];
4676       struct dw_subsection *dwss;
4677
4678       if (dws->anon_subseg)
4679         {
4680           dwss = dws->anon_subseg;
4681           dwss->link = dws->list_subseg;
4682         }
4683       else
4684         dwss = dws->list_subseg;
4685
4686       for (; dwss != NULL; dwss = dwss->link)
4687         if (dwss->end_exp.X_add_symbol != NULL)
4688           {
4689             subseg_set (dws->sect, dwss->subseg);
4690             symbol_set_value_now (dwss->end_exp.X_add_symbol);
4691           }
4692     }
4693 }
4694
4695 #endif /* OBJ_XCOFF */
4696 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
4697 \f
4698 /* The .tc pseudo-op.  This is used when generating either XCOFF or
4699    ELF.  This takes two or more arguments.
4700
4701    When generating XCOFF output, the first argument is the name to
4702    give to this location in the toc; this will be a symbol with class
4703    TC.  The rest of the arguments are N-byte values to actually put at
4704    this location in the TOC; often there is just one more argument, a
4705    relocatable symbol reference.  The size of the value to store
4706    depends on target word size.  A 32-bit target uses 4-byte values, a
4707    64-bit target uses 8-byte values.
4708
4709    When not generating XCOFF output, the arguments are the same, but
4710    the first argument is simply ignored.  */
4711
4712 static void
4713 ppc_tc (int ignore ATTRIBUTE_UNUSED)
4714 {
4715 #ifdef OBJ_XCOFF
4716
4717   /* Define the TOC symbol name.  */
4718   {
4719     char *name;
4720     char endc;
4721     symbolS *sym;
4722
4723     if (ppc_toc_csect == (symbolS *) NULL
4724         || ppc_toc_csect != ppc_current_csect)
4725       {
4726         as_bad (_(".tc not in .toc section"));
4727         ignore_rest_of_line ();
4728         return;
4729       }
4730
4731     name = input_line_pointer;
4732     endc = get_symbol_end ();
4733
4734     sym = symbol_find_or_make (name);
4735
4736     *input_line_pointer = endc;
4737
4738     if (S_IS_DEFINED (sym))
4739       {
4740         symbolS *label;
4741
4742         label = symbol_get_tc (ppc_current_csect)->within;
4743         if (symbol_get_tc (label)->symbol_class != XMC_TC0)
4744           {
4745             as_bad (_(".tc with no label"));
4746             ignore_rest_of_line ();
4747             return;
4748           }
4749
4750         S_SET_SEGMENT (label, S_GET_SEGMENT (sym));
4751         symbol_set_frag (label, symbol_get_frag (sym));
4752         S_SET_VALUE (label, S_GET_VALUE (sym));
4753
4754         while (! is_end_of_line[(unsigned char) *input_line_pointer])
4755           ++input_line_pointer;
4756
4757         return;
4758       }
4759
4760     S_SET_SEGMENT (sym, now_seg);
4761     symbol_set_frag (sym, frag_now);
4762     S_SET_VALUE (sym, (valueT) frag_now_fix ());
4763     symbol_get_tc (sym)->symbol_class = XMC_TC;
4764     symbol_get_tc (sym)->output = 1;
4765
4766     ppc_frob_label (sym);
4767   }
4768
4769 #endif /* OBJ_XCOFF */
4770 #ifdef OBJ_ELF
4771   int align;
4772
4773   /* Skip the TOC symbol name.  */
4774   while (is_part_of_name (*input_line_pointer)
4775          || *input_line_pointer == ' '
4776          || *input_line_pointer == '['
4777          || *input_line_pointer == ']'
4778          || *input_line_pointer == '{'
4779          || *input_line_pointer == '}')
4780     ++input_line_pointer;
4781
4782   /* Align to a four/eight byte boundary.  */
4783   align = ppc_obj64 ? 3 : 2;
4784   frag_align (align, 0, 0);
4785   record_alignment (now_seg, align);
4786 #endif /* OBJ_ELF */
4787
4788   if (*input_line_pointer != ',')
4789     demand_empty_rest_of_line ();
4790   else
4791     {
4792       ++input_line_pointer;
4793       cons (ppc_obj64 ? 8 : 4);
4794     }
4795 }
4796
4797 /* Pseudo-op .machine.  */
4798
4799 static void
4800 ppc_machine (int ignore ATTRIBUTE_UNUSED)
4801 {
4802   char *cpu_string;
4803 #define MAX_HISTORY 100
4804   static ppc_cpu_t *cpu_history;
4805   static int curr_hist;
4806
4807   SKIP_WHITESPACE ();
4808
4809   if (*input_line_pointer == '"')
4810     {
4811       int len;
4812       cpu_string = demand_copy_C_string (&len);
4813     }
4814   else
4815     {
4816       char c;
4817       cpu_string = input_line_pointer;
4818       c = get_symbol_end ();
4819       cpu_string = xstrdup (cpu_string);
4820       *input_line_pointer = c;
4821     }
4822
4823   if (cpu_string != NULL)
4824     {
4825       ppc_cpu_t old_cpu = ppc_cpu;
4826       ppc_cpu_t new_cpu;
4827       char *p;
4828
4829       for (p = cpu_string; *p != 0; p++)
4830         *p = TOLOWER (*p);
4831
4832       if (strcmp (cpu_string, "push") == 0)
4833         {
4834           if (cpu_history == NULL)
4835             cpu_history = xmalloc (MAX_HISTORY * sizeof (*cpu_history));
4836
4837           if (curr_hist >= MAX_HISTORY)
4838             as_bad (_(".machine stack overflow"));
4839           else
4840             cpu_history[curr_hist++] = ppc_cpu;
4841         }
4842       else if (strcmp (cpu_string, "pop") == 0)
4843         {
4844           if (curr_hist <= 0)
4845             as_bad (_(".machine stack underflow"));
4846           else
4847             ppc_cpu = cpu_history[--curr_hist];
4848         }
4849       else if ((new_cpu = ppc_parse_cpu (ppc_cpu, cpu_string)) != 0)
4850         ppc_cpu = new_cpu;
4851       else
4852         as_bad (_("invalid machine `%s'"), cpu_string);
4853
4854       if (ppc_cpu != old_cpu)
4855         ppc_setup_opcodes ();
4856     }
4857
4858   demand_empty_rest_of_line ();
4859 }
4860 #endif /* defined (OBJ_XCOFF) || defined (OBJ_ELF) */
4861 \f
4862 #ifdef TE_PE
4863
4864 /* Pseudo-ops specific to the Windows NT PowerPC PE (coff) format.  */
4865
4866 /* Set the current section.  */
4867 static void
4868 ppc_set_current_section (segT new)
4869 {
4870   ppc_previous_section = ppc_current_section;
4871   ppc_current_section = new;
4872 }
4873
4874 /* pseudo-op: .previous
4875    behaviour: toggles the current section with the previous section.
4876    errors:    None
4877    warnings:  "No previous section"  */
4878
4879 static void
4880 ppc_previous (int ignore ATTRIBUTE_UNUSED)
4881 {
4882   if (ppc_previous_section == NULL)
4883     {
4884       as_warn (_("no previous section to return to, ignored."));
4885       return;
4886     }
4887
4888   subseg_set (ppc_previous_section, 0);
4889
4890   ppc_set_current_section (ppc_previous_section);
4891 }
4892
4893 /* pseudo-op: .pdata
4894    behaviour: predefined read only data section
4895               double word aligned
4896    errors:    None
4897    warnings:  None
4898    initial:   .section .pdata "adr3"
4899               a - don't know -- maybe a misprint
4900               d - initialized data
4901               r - readable
4902               3 - double word aligned (that would be 4 byte boundary)
4903
4904    commentary:
4905    Tag index tables (also known as the function table) for exception
4906    handling, debugging, etc.  */
4907
4908 static void
4909 ppc_pdata (int ignore ATTRIBUTE_UNUSED)
4910 {
4911   if (pdata_section == 0)
4912     {
4913       pdata_section = subseg_new (".pdata", 0);
4914
4915       bfd_set_section_flags (stdoutput, pdata_section,
4916                              (SEC_ALLOC | SEC_LOAD | SEC_RELOC
4917                               | SEC_READONLY | SEC_DATA ));
4918
4919       bfd_set_section_alignment (stdoutput, pdata_section, 2);
4920     }
4921   else
4922     {
4923       pdata_section = subseg_new (".pdata", 0);
4924     }
4925   ppc_set_current_section (pdata_section);
4926 }
4927
4928 /* pseudo-op: .ydata
4929    behaviour: predefined read only data section
4930               double word aligned
4931    errors:    None
4932    warnings:  None
4933    initial:   .section .ydata "drw3"
4934               a - don't know -- maybe a misprint
4935               d - initialized data
4936               r - readable
4937               3 - double word aligned (that would be 4 byte boundary)
4938    commentary:
4939    Tag tables (also known as the scope table) for exception handling,
4940    debugging, etc.  */
4941
4942 static void
4943 ppc_ydata (int ignore ATTRIBUTE_UNUSED)
4944 {
4945   if (ydata_section == 0)
4946     {
4947       ydata_section = subseg_new (".ydata", 0);
4948       bfd_set_section_flags (stdoutput, ydata_section,
4949                              (SEC_ALLOC | SEC_LOAD | SEC_RELOC
4950                               | SEC_READONLY | SEC_DATA ));
4951
4952       bfd_set_section_alignment (stdoutput, ydata_section, 3);
4953     }
4954   else
4955     {
4956       ydata_section = subseg_new (".ydata", 0);
4957     }
4958   ppc_set_current_section (ydata_section);
4959 }
4960
4961 /* pseudo-op: .reldata
4962    behaviour: predefined read write data section
4963               double word aligned (4-byte)
4964               FIXME: relocation is applied to it
4965               FIXME: what's the difference between this and .data?
4966    errors:    None
4967    warnings:  None
4968    initial:   .section .reldata "drw3"
4969               d - initialized data
4970               r - readable
4971               w - writeable
4972               3 - double word aligned (that would be 8 byte boundary)
4973
4974    commentary:
4975    Like .data, but intended to hold data subject to relocation, such as
4976    function descriptors, etc.  */
4977
4978 static void
4979 ppc_reldata (int ignore ATTRIBUTE_UNUSED)
4980 {
4981   if (reldata_section == 0)
4982     {
4983       reldata_section = subseg_new (".reldata", 0);
4984
4985       bfd_set_section_flags (stdoutput, reldata_section,
4986                              (SEC_ALLOC | SEC_LOAD | SEC_RELOC
4987                               | SEC_DATA));
4988
4989       bfd_set_section_alignment (stdoutput, reldata_section, 2);
4990     }
4991   else
4992     {
4993       reldata_section = subseg_new (".reldata", 0);
4994     }
4995   ppc_set_current_section (reldata_section);
4996 }
4997
4998 /* pseudo-op: .rdata
4999    behaviour: predefined read only data section
5000               double word aligned
5001    errors:    None
5002    warnings:  None
5003    initial:   .section .rdata "dr3"
5004               d - initialized data
5005               r - readable
5006               3 - double word aligned (that would be 4 byte boundary)  */
5007
5008 static void
5009 ppc_rdata (int ignore ATTRIBUTE_UNUSED)
5010 {
5011   if (rdata_section == 0)
5012     {
5013       rdata_section = subseg_new (".rdata", 0);
5014       bfd_set_section_flags (stdoutput, rdata_section,
5015                              (SEC_ALLOC | SEC_LOAD | SEC_RELOC
5016                               | SEC_READONLY | SEC_DATA ));
5017
5018       bfd_set_section_alignment (stdoutput, rdata_section, 2);
5019     }
5020   else
5021     {
5022       rdata_section = subseg_new (".rdata", 0);
5023     }
5024   ppc_set_current_section (rdata_section);
5025 }
5026
5027 /* pseudo-op: .ualong
5028    behaviour: much like .int, with the exception that no alignment is
5029               performed.
5030               FIXME: test the alignment statement
5031    errors:    None
5032    warnings:  None  */
5033
5034 static void
5035 ppc_ualong (int ignore ATTRIBUTE_UNUSED)
5036 {
5037   /* Try for long.  */
5038   cons (4);
5039 }
5040
5041 /* pseudo-op: .znop  <symbol name>
5042    behaviour: Issue a nop instruction
5043               Issue a IMAGE_REL_PPC_IFGLUE relocation against it, using
5044               the supplied symbol name.
5045    errors:    None
5046    warnings:  Missing symbol name  */
5047
5048 static void
5049 ppc_znop (int ignore ATTRIBUTE_UNUSED)
5050 {
5051   unsigned long insn;
5052   const struct powerpc_opcode *opcode;
5053   char *f;
5054   symbolS *sym;
5055   char *symbol_name;
5056   char c;
5057   char *name;
5058
5059   /* Strip out the symbol name.  */
5060   symbol_name = input_line_pointer;
5061   c = get_symbol_end ();
5062
5063   name = xmalloc (input_line_pointer - symbol_name + 1);
5064   strcpy (name, symbol_name);
5065
5066   sym = symbol_find_or_make (name);
5067
5068   *input_line_pointer = c;
5069
5070   SKIP_WHITESPACE ();
5071
5072   /* Look up the opcode in the hash table.  */
5073   opcode = (const struct powerpc_opcode *) hash_find (ppc_hash, "nop");
5074
5075   /* Stick in the nop.  */
5076   insn = opcode->opcode;
5077
5078   /* Write out the instruction.  */
5079   f = frag_more (4);
5080   md_number_to_chars (f, insn, 4);
5081   fix_new (frag_now,
5082            f - frag_now->fr_literal,
5083            4,
5084            sym,
5085            0,
5086            0,
5087            BFD_RELOC_16_GOT_PCREL);
5088
5089 }
5090
5091 /* pseudo-op:
5092    behaviour:
5093    errors:
5094    warnings:  */
5095
5096 static void
5097 ppc_pe_comm (int lcomm)
5098 {
5099   char *name;
5100   char c;
5101   char *p;
5102   offsetT temp;
5103   symbolS *symbolP;
5104   offsetT align;
5105
5106   name = input_line_pointer;
5107   c = get_symbol_end ();
5108
5109   /* just after name is now '\0'.  */
5110   p = input_line_pointer;
5111   *p = c;
5112   SKIP_WHITESPACE ();
5113   if (*input_line_pointer != ',')
5114     {
5115       as_bad (_("expected comma after symbol-name: rest of line ignored."));
5116       ignore_rest_of_line ();
5117       return;
5118     }
5119
5120   input_line_pointer++;         /* skip ',' */
5121   if ((temp = get_absolute_expression ()) < 0)
5122     {
5123       as_warn (_(".COMMon length (%ld.) <0! Ignored."), (long) temp);
5124       ignore_rest_of_line ();
5125       return;
5126     }
5127
5128   if (! lcomm)
5129     {
5130       /* The third argument to .comm is the alignment.  */
5131       if (*input_line_pointer != ',')
5132         align = 3;
5133       else
5134         {
5135           ++input_line_pointer;
5136           align = get_absolute_expression ();
5137           if (align <= 0)
5138             {
5139               as_warn (_("ignoring bad alignment"));
5140               align = 3;
5141             }
5142         }
5143     }
5144
5145   *p = 0;
5146   symbolP = symbol_find_or_make (name);
5147
5148   *p = c;
5149   if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
5150     {
5151       as_bad (_("ignoring attempt to re-define symbol `%s'."),
5152               S_GET_NAME (symbolP));
5153       ignore_rest_of_line ();
5154       return;
5155     }
5156
5157   if (S_GET_VALUE (symbolP))
5158     {
5159       if (S_GET_VALUE (symbolP) != (valueT) temp)
5160         as_bad (_("length of .comm \"%s\" is already %ld. Not changed to %ld."),
5161                 S_GET_NAME (symbolP),
5162                 (long) S_GET_VALUE (symbolP),
5163                 (long) temp);
5164     }
5165   else
5166     {
5167       S_SET_VALUE (symbolP, (valueT) temp);
5168       S_SET_EXTERNAL (symbolP);
5169       S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
5170     }
5171
5172   demand_empty_rest_of_line ();
5173 }
5174
5175 /*
5176  * implement the .section pseudo op:
5177  *      .section name {, "flags"}
5178  *                ^         ^
5179  *                |         +--- optional flags: 'b' for bss
5180  *                |                              'i' for info
5181  *                +-- section name               'l' for lib
5182  *                                               'n' for noload
5183  *                                               'o' for over
5184  *                                               'w' for data
5185  *                                               'd' (apparently m88k for data)
5186  *                                               'x' for text
5187  * But if the argument is not a quoted string, treat it as a
5188  * subsegment number.
5189  *
5190  * FIXME: this is a copy of the section processing from obj-coff.c, with
5191  * additions/changes for the moto-pas assembler support. There are three
5192  * categories:
5193  *
5194  * FIXME: I just noticed this. This doesn't work at all really. It it
5195  *        setting bits that bfd probably neither understands or uses. The
5196  *        correct approach (?) will have to incorporate extra fields attached
5197  *        to the section to hold the system specific stuff. (krk)
5198  *
5199  * Section Contents:
5200  * 'a' - unknown - referred to in documentation, but no definition supplied
5201  * 'c' - section has code
5202  * 'd' - section has initialized data
5203  * 'u' - section has uninitialized data
5204  * 'i' - section contains directives (info)
5205  * 'n' - section can be discarded
5206  * 'R' - remove section at link time
5207  *
5208  * Section Protection:
5209  * 'r' - section is readable
5210  * 'w' - section is writeable
5211  * 'x' - section is executable
5212  * 's' - section is sharable
5213  *
5214  * Section Alignment:
5215  * '0' - align to byte boundary
5216  * '1' - align to halfword undary
5217  * '2' - align to word boundary
5218  * '3' - align to doubleword boundary
5219  * '4' - align to quadword boundary
5220  * '5' - align to 32 byte boundary
5221  * '6' - align to 64 byte boundary
5222  *
5223  */
5224
5225 void
5226 ppc_pe_section (int ignore ATTRIBUTE_UNUSED)
5227 {
5228   /* Strip out the section name.  */
5229   char *section_name;
5230   char c;
5231   char *name;
5232   unsigned int exp;
5233   flagword flags;
5234   segT sec;
5235   int align;
5236
5237   section_name = input_line_pointer;
5238   c = get_symbol_end ();
5239
5240   name = xmalloc (input_line_pointer - section_name + 1);
5241   strcpy (name, section_name);
5242
5243   *input_line_pointer = c;
5244
5245   SKIP_WHITESPACE ();
5246
5247   exp = 0;
5248   flags = SEC_NO_FLAGS;
5249
5250   if (strcmp (name, ".idata$2") == 0)
5251     {
5252       align = 0;
5253     }
5254   else if (strcmp (name, ".idata$3") == 0)
5255     {
5256       align = 0;
5257     }
5258   else if (strcmp (name, ".idata$4") == 0)
5259     {
5260       align = 2;
5261     }
5262   else if (strcmp (name, ".idata$5") == 0)
5263     {
5264       align = 2;
5265     }
5266   else if (strcmp (name, ".idata$6") == 0)
5267     {
5268       align = 1;
5269     }
5270   else
5271     /* Default alignment to 16 byte boundary.  */
5272     align = 4;
5273
5274   if (*input_line_pointer == ',')
5275     {
5276       ++input_line_pointer;
5277       SKIP_WHITESPACE ();
5278       if (*input_line_pointer != '"')
5279         exp = get_absolute_expression ();
5280       else
5281         {
5282           ++input_line_pointer;
5283           while (*input_line_pointer != '"'
5284                  && ! is_end_of_line[(unsigned char) *input_line_pointer])
5285             {
5286               switch (*input_line_pointer)
5287                 {
5288                   /* Section Contents */
5289                 case 'a': /* unknown */
5290                   as_bad (_("unsupported section attribute -- 'a'"));
5291                   break;
5292                 case 'c': /* code section */
5293                   flags |= SEC_CODE;
5294                   break;
5295                 case 'd': /* section has initialized data */
5296                   flags |= SEC_DATA;
5297                   break;
5298                 case 'u': /* section has uninitialized data */
5299                   /* FIXME: This is IMAGE_SCN_CNT_UNINITIALIZED_DATA
5300                      in winnt.h */
5301                   flags |= SEC_ROM;
5302                   break;
5303                 case 'i': /* section contains directives (info) */
5304                   /* FIXME: This is IMAGE_SCN_LNK_INFO
5305                      in winnt.h */
5306                   flags |= SEC_HAS_CONTENTS;
5307                   break;
5308                 case 'n': /* section can be discarded */
5309                   flags &=~ SEC_LOAD;
5310                   break;
5311                 case 'R': /* Remove section at link time */
5312                   flags |= SEC_NEVER_LOAD;
5313                   break;
5314 #if IFLICT_BRAIN_DAMAGE
5315                   /* Section Protection */
5316                 case 'r': /* section is readable */
5317                   flags |= IMAGE_SCN_MEM_READ;
5318                   break;
5319                 case 'w': /* section is writeable */
5320                   flags |= IMAGE_SCN_MEM_WRITE;
5321                   break;
5322                 case 'x': /* section is executable */
5323                   flags |= IMAGE_SCN_MEM_EXECUTE;
5324                   break;
5325                 case 's': /* section is sharable */
5326                   flags |= IMAGE_SCN_MEM_SHARED;
5327                   break;
5328
5329                   /* Section Alignment */
5330                 case '0': /* align to byte boundary */
5331                   flags |= IMAGE_SCN_ALIGN_1BYTES;
5332                   align = 0;
5333                   break;
5334                 case '1':  /* align to halfword boundary */
5335                   flags |= IMAGE_SCN_ALIGN_2BYTES;
5336                   align = 1;
5337                   break;
5338                 case '2':  /* align to word boundary */
5339                   flags |= IMAGE_SCN_ALIGN_4BYTES;
5340                   align = 2;
5341                   break;
5342                 case '3':  /* align to doubleword boundary */
5343                   flags |= IMAGE_SCN_ALIGN_8BYTES;
5344                   align = 3;
5345                   break;
5346                 case '4':  /* align to quadword boundary */
5347                   flags |= IMAGE_SCN_ALIGN_16BYTES;
5348                   align = 4;
5349                   break;
5350                 case '5':  /* align to 32 byte boundary */
5351                   flags |= IMAGE_SCN_ALIGN_32BYTES;
5352                   align = 5;
5353                   break;
5354                 case '6':  /* align to 64 byte boundary */
5355                   flags |= IMAGE_SCN_ALIGN_64BYTES;
5356                   align = 6;
5357                   break;
5358 #endif
5359                 default:
5360                   as_bad (_("unknown section attribute '%c'"),
5361                           *input_line_pointer);
5362                   break;
5363                 }
5364               ++input_line_pointer;
5365             }
5366           if (*input_line_pointer == '"')
5367             ++input_line_pointer;
5368         }
5369     }
5370
5371   sec = subseg_new (name, (subsegT) exp);
5372
5373   ppc_set_current_section (sec);
5374
5375   if (flags != SEC_NO_FLAGS)
5376     {
5377       if (! bfd_set_section_flags (stdoutput, sec, flags))
5378         as_bad (_("error setting flags for \"%s\": %s"),
5379                 bfd_section_name (stdoutput, sec),
5380                 bfd_errmsg (bfd_get_error ()));
5381     }
5382
5383   bfd_set_section_alignment (stdoutput, sec, align);
5384 }
5385
5386 static void
5387 ppc_pe_function (int ignore ATTRIBUTE_UNUSED)
5388 {
5389   char *name;
5390   char endc;
5391   symbolS *ext_sym;
5392
5393   name = input_line_pointer;
5394   endc = get_symbol_end ();
5395
5396   ext_sym = symbol_find_or_make (name);
5397
5398   *input_line_pointer = endc;
5399
5400   S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
5401   SF_SET_FUNCTION (ext_sym);
5402   SF_SET_PROCESS (ext_sym);
5403   coff_add_linesym (ext_sym);
5404
5405   demand_empty_rest_of_line ();
5406 }
5407
5408 static void
5409 ppc_pe_tocd (int ignore ATTRIBUTE_UNUSED)
5410 {
5411   if (tocdata_section == 0)
5412     {
5413       tocdata_section = subseg_new (".tocd", 0);
5414       /* FIXME: section flags won't work.  */
5415       bfd_set_section_flags (stdoutput, tocdata_section,
5416                              (SEC_ALLOC | SEC_LOAD | SEC_RELOC
5417                               | SEC_READONLY | SEC_DATA));
5418
5419       bfd_set_section_alignment (stdoutput, tocdata_section, 2);
5420     }
5421   else
5422     {
5423       rdata_section = subseg_new (".tocd", 0);
5424     }
5425
5426   ppc_set_current_section (tocdata_section);
5427
5428   demand_empty_rest_of_line ();
5429 }
5430
5431 /* Don't adjust TOC relocs to use the section symbol.  */
5432
5433 int
5434 ppc_pe_fix_adjustable (fixS *fix)
5435 {
5436   return fix->fx_r_type != BFD_RELOC_PPC_TOC16;
5437 }
5438
5439 #endif
5440 \f
5441 #ifdef OBJ_XCOFF
5442
5443 /* XCOFF specific symbol and file handling.  */
5444
5445 /* Canonicalize the symbol name.  We use the to force the suffix, if
5446    any, to use square brackets, and to be in upper case.  */
5447
5448 char *
5449 ppc_canonicalize_symbol_name (char *name)
5450 {
5451   char *s;
5452
5453   if (ppc_stab_symbol)
5454     return name;
5455
5456   for (s = name; *s != '\0' && *s != '{' && *s != '['; s++)
5457     ;
5458   if (*s != '\0')
5459     {
5460       char brac;
5461
5462       if (*s == '[')
5463         brac = ']';
5464       else
5465         {
5466           *s = '[';
5467           brac = '}';
5468         }
5469
5470       for (s++; *s != '\0' && *s != brac; s++)
5471         *s = TOUPPER (*s);
5472
5473       if (*s == '\0' || s[1] != '\0')
5474         as_bad (_("bad symbol suffix"));
5475
5476       *s = ']';
5477     }
5478
5479   return name;
5480 }
5481
5482 /* Set the class of a symbol based on the suffix, if any.  This is
5483    called whenever a new symbol is created.  */
5484
5485 void
5486 ppc_symbol_new_hook (symbolS *sym)
5487 {
5488   struct ppc_tc_sy *tc;
5489   const char *s;
5490
5491   tc = symbol_get_tc (sym);
5492   tc->next = NULL;
5493   tc->output = 0;
5494   tc->symbol_class = -1;
5495   tc->real_name = NULL;
5496   tc->subseg = 0;
5497   tc->align = 0;
5498   tc->u.size = NULL;
5499   tc->u.dw = NULL;
5500   tc->within = NULL;
5501
5502   if (ppc_stab_symbol)
5503     return;
5504
5505   s = strchr (S_GET_NAME (sym), '[');
5506   if (s == (const char *) NULL)
5507     {
5508       /* There is no suffix.  */
5509       return;
5510     }
5511
5512   ++s;
5513
5514   switch (s[0])
5515     {
5516     case 'B':
5517       if (strcmp (s, "BS]") == 0)
5518         tc->symbol_class = XMC_BS;
5519       break;
5520     case 'D':
5521       if (strcmp (s, "DB]") == 0)
5522         tc->symbol_class = XMC_DB;
5523       else if (strcmp (s, "DS]") == 0)
5524         tc->symbol_class = XMC_DS;
5525       break;
5526     case 'G':
5527       if (strcmp (s, "GL]") == 0)
5528         tc->symbol_class = XMC_GL;
5529       break;
5530     case 'P':
5531       if (strcmp (s, "PR]") == 0)
5532         tc->symbol_class = XMC_PR;
5533       break;
5534     case 'R':
5535       if (strcmp (s, "RO]") == 0)
5536         tc->symbol_class = XMC_RO;
5537       else if (strcmp (s, "RW]") == 0)
5538         tc->symbol_class = XMC_RW;
5539       break;
5540     case 'S':
5541       if (strcmp (s, "SV]") == 0)
5542         tc->symbol_class = XMC_SV;
5543       break;
5544     case 'T':
5545       if (strcmp (s, "TC]") == 0)
5546         tc->symbol_class = XMC_TC;
5547       else if (strcmp (s, "TI]") == 0)
5548         tc->symbol_class = XMC_TI;
5549       else if (strcmp (s, "TB]") == 0)
5550         tc->symbol_class = XMC_TB;
5551       else if (strcmp (s, "TC0]") == 0 || strcmp (s, "T0]") == 0)
5552         tc->symbol_class = XMC_TC0;
5553       break;
5554     case 'U':
5555       if (strcmp (s, "UA]") == 0)
5556         tc->symbol_class = XMC_UA;
5557       else if (strcmp (s, "UC]") == 0)
5558         tc->symbol_class = XMC_UC;
5559       break;
5560     case 'X':
5561       if (strcmp (s, "XO]") == 0)
5562         tc->symbol_class = XMC_XO;
5563       break;
5564     }
5565
5566   if (tc->symbol_class == -1)
5567     as_bad (_("unrecognized symbol suffix"));
5568 }
5569
5570 /* Set the class of a label based on where it is defined.  This
5571    handles symbols without suffixes.  Also, move the symbol so that it
5572    follows the csect symbol.  */
5573
5574 void
5575 ppc_frob_label (symbolS *sym)
5576 {
5577   if (ppc_current_csect != (symbolS *) NULL)
5578     {
5579       if (symbol_get_tc (sym)->symbol_class == -1)
5580         symbol_get_tc (sym)->symbol_class = symbol_get_tc (ppc_current_csect)->symbol_class;
5581
5582       symbol_remove (sym, &symbol_rootP, &symbol_lastP);
5583       symbol_append (sym, symbol_get_tc (ppc_current_csect)->within,
5584                      &symbol_rootP, &symbol_lastP);
5585       symbol_get_tc (ppc_current_csect)->within = sym;
5586       symbol_get_tc (sym)->within = ppc_current_csect;
5587     }
5588
5589 #ifdef OBJ_ELF
5590   dwarf2_emit_label (sym);
5591 #endif
5592 }
5593
5594 /* This variable is set by ppc_frob_symbol if any absolute symbols are
5595    seen.  It tells ppc_adjust_symtab whether it needs to look through
5596    the symbols.  */
5597
5598 static bfd_boolean ppc_saw_abs;
5599
5600 /* Change the name of a symbol just before writing it out.  Set the
5601    real name if the .rename pseudo-op was used.  Otherwise, remove any
5602    class suffix.  Return 1 if the symbol should not be included in the
5603    symbol table.  */
5604
5605 int
5606 ppc_frob_symbol (symbolS *sym)
5607 {
5608   static symbolS *ppc_last_function;
5609   static symbolS *set_end;
5610
5611   /* Discard symbols that should not be included in the output symbol
5612      table.  */
5613   if (! symbol_used_in_reloc_p (sym)
5614       && ((symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) != 0
5615           || (! (S_IS_EXTERNAL (sym) || S_IS_WEAK (sym))
5616               && ! symbol_get_tc (sym)->output
5617               && S_GET_STORAGE_CLASS (sym) != C_FILE)))
5618     return 1;
5619
5620   /* This one will disappear anyway.  Don't make a csect sym for it.  */
5621   if (sym == abs_section_sym)
5622     return 1;
5623
5624   if (symbol_get_tc (sym)->real_name != (char *) NULL)
5625     S_SET_NAME (sym, symbol_get_tc (sym)->real_name);
5626   else
5627     {
5628       const char *name;
5629       const char *s;
5630
5631       name = S_GET_NAME (sym);
5632       s = strchr (name, '[');
5633       if (s != (char *) NULL)
5634         {
5635           unsigned int len;
5636           char *snew;
5637
5638           len = s - name;
5639           snew = xmalloc (len + 1);
5640           memcpy (snew, name, len);
5641           snew[len] = '\0';
5642
5643           S_SET_NAME (sym, snew);
5644         }
5645     }
5646
5647   if (set_end != (symbolS *) NULL)
5648     {
5649       SA_SET_SYM_ENDNDX (set_end, sym);
5650       set_end = NULL;
5651     }
5652
5653   if (SF_GET_FUNCTION (sym))
5654     {
5655       if (ppc_last_function != (symbolS *) NULL)
5656         as_bad (_("two .function pseudo-ops with no intervening .ef"));
5657       ppc_last_function = sym;
5658       if (symbol_get_tc (sym)->u.size != (symbolS *) NULL)
5659         {
5660           resolve_symbol_value (symbol_get_tc (sym)->u.size);
5661           SA_SET_SYM_FSIZE (sym,
5662                             (long) S_GET_VALUE (symbol_get_tc (sym)->u.size));
5663         }
5664     }
5665   else if (S_GET_STORAGE_CLASS (sym) == C_FCN
5666            && strcmp (S_GET_NAME (sym), ".ef") == 0)
5667     {
5668       if (ppc_last_function == (symbolS *) NULL)
5669         as_bad (_(".ef with no preceding .function"));
5670       else
5671         {
5672           set_end = ppc_last_function;
5673           ppc_last_function = NULL;
5674
5675           /* We don't have a C_EFCN symbol, but we need to force the
5676              COFF backend to believe that it has seen one.  */
5677           coff_last_function = NULL;
5678         }
5679     }
5680
5681   if (! (S_IS_EXTERNAL (sym) || S_IS_WEAK (sym))
5682       && (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) == 0
5683       && S_GET_STORAGE_CLASS (sym) != C_FILE
5684       && S_GET_STORAGE_CLASS (sym) != C_FCN
5685       && S_GET_STORAGE_CLASS (sym) != C_BLOCK
5686       && S_GET_STORAGE_CLASS (sym) != C_BSTAT
5687       && S_GET_STORAGE_CLASS (sym) != C_ESTAT
5688       && S_GET_STORAGE_CLASS (sym) != C_BINCL
5689       && S_GET_STORAGE_CLASS (sym) != C_EINCL
5690       && S_GET_SEGMENT (sym) != ppc_coff_debug_section)
5691     S_SET_STORAGE_CLASS (sym, C_HIDEXT);
5692
5693   if (S_GET_STORAGE_CLASS (sym) == C_EXT
5694       || S_GET_STORAGE_CLASS (sym) == C_AIX_WEAKEXT
5695       || S_GET_STORAGE_CLASS (sym) == C_HIDEXT)
5696     {
5697       int i;
5698       union internal_auxent *a;
5699
5700       /* Create a csect aux.  */
5701       i = S_GET_NUMBER_AUXILIARY (sym);
5702       S_SET_NUMBER_AUXILIARY (sym, i + 1);
5703       a = &coffsymbol (symbol_get_bfdsym (sym))->native[i + 1].u.auxent;
5704       if (symbol_get_tc (sym)->symbol_class == XMC_TC0)
5705         {
5706           /* This is the TOC table.  */
5707           know (strcmp (S_GET_NAME (sym), "TOC") == 0);
5708           a->x_csect.x_scnlen.l = 0;
5709           a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
5710         }
5711       else if (symbol_get_tc (sym)->subseg != 0)
5712         {
5713           /* This is a csect symbol.  x_scnlen is the size of the
5714              csect.  */
5715           if (symbol_get_tc (sym)->next == (symbolS *) NULL)
5716             a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
5717                                                        S_GET_SEGMENT (sym))
5718                                      - S_GET_VALUE (sym));
5719           else
5720             {
5721               resolve_symbol_value (symbol_get_tc (sym)->next);
5722               a->x_csect.x_scnlen.l = (S_GET_VALUE (symbol_get_tc (sym)->next)
5723                                        - S_GET_VALUE (sym));
5724             }
5725           a->x_csect.x_smtyp = (symbol_get_tc (sym)->align << 3) | XTY_SD;
5726         }
5727       else if (S_GET_SEGMENT (sym) == bss_section)
5728         {
5729           /* This is a common symbol.  */
5730           a->x_csect.x_scnlen.l = symbol_get_frag (sym)->fr_offset;
5731           a->x_csect.x_smtyp = (symbol_get_tc (sym)->align << 3) | XTY_CM;
5732           if (S_IS_EXTERNAL (sym))
5733             symbol_get_tc (sym)->symbol_class = XMC_RW;
5734           else
5735             symbol_get_tc (sym)->symbol_class = XMC_BS;
5736         }
5737       else if (S_GET_SEGMENT (sym) == absolute_section)
5738         {
5739           /* This is an absolute symbol.  The csect will be created by
5740              ppc_adjust_symtab.  */
5741           ppc_saw_abs = TRUE;
5742           a->x_csect.x_smtyp = XTY_LD;
5743           if (symbol_get_tc (sym)->symbol_class == -1)
5744             symbol_get_tc (sym)->symbol_class = XMC_XO;
5745         }
5746       else if (! S_IS_DEFINED (sym))
5747         {
5748           /* This is an external symbol.  */
5749           a->x_csect.x_scnlen.l = 0;
5750           a->x_csect.x_smtyp = XTY_ER;
5751         }
5752       else if (symbol_get_tc (sym)->symbol_class == XMC_TC)
5753         {
5754           symbolS *next;
5755
5756           /* This is a TOC definition.  x_scnlen is the size of the
5757              TOC entry.  */
5758           next = symbol_next (sym);
5759           while (symbol_get_tc (next)->symbol_class == XMC_TC0)
5760             next = symbol_next (next);
5761           if (next == (symbolS *) NULL
5762               || symbol_get_tc (next)->symbol_class != XMC_TC)
5763             {
5764               if (ppc_after_toc_frag == (fragS *) NULL)
5765                 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
5766                                                            data_section)
5767                                          - S_GET_VALUE (sym));
5768               else
5769                 a->x_csect.x_scnlen.l = (ppc_after_toc_frag->fr_address
5770                                          - S_GET_VALUE (sym));
5771             }
5772           else
5773             {
5774               resolve_symbol_value (next);
5775               a->x_csect.x_scnlen.l = (S_GET_VALUE (next)
5776                                        - S_GET_VALUE (sym));
5777             }
5778           a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
5779         }
5780       else
5781         {
5782           symbolS *csect;
5783
5784           /* This is a normal symbol definition.  x_scnlen is the
5785              symbol index of the containing csect.  */
5786           if (S_GET_SEGMENT (sym) == text_section)
5787             csect = ppc_text_csects;
5788           else if (S_GET_SEGMENT (sym) == data_section)
5789             csect = ppc_data_csects;
5790           else
5791             abort ();
5792
5793           /* Skip the initial dummy symbol.  */
5794           csect = symbol_get_tc (csect)->next;
5795
5796           if (csect == (symbolS *) NULL)
5797             {
5798               as_warn (_("warning: symbol %s has no csect"), S_GET_NAME (sym));
5799               a->x_csect.x_scnlen.l = 0;
5800             }
5801           else
5802             {
5803               while (symbol_get_tc (csect)->next != (symbolS *) NULL)
5804                 {
5805                   resolve_symbol_value (symbol_get_tc (csect)->next);
5806                   if (S_GET_VALUE (symbol_get_tc (csect)->next)
5807                       > S_GET_VALUE (sym))
5808                     break;
5809                   csect = symbol_get_tc (csect)->next;
5810                 }
5811
5812               a->x_csect.x_scnlen.p =
5813                 coffsymbol (symbol_get_bfdsym (csect))->native;
5814               coffsymbol (symbol_get_bfdsym (sym))->native[i + 1].fix_scnlen =
5815                 1;
5816             }
5817           a->x_csect.x_smtyp = XTY_LD;
5818         }
5819
5820       a->x_csect.x_parmhash = 0;
5821       a->x_csect.x_snhash = 0;
5822       if (symbol_get_tc (sym)->symbol_class == -1)
5823         a->x_csect.x_smclas = XMC_PR;
5824       else
5825         a->x_csect.x_smclas = symbol_get_tc (sym)->symbol_class;
5826       a->x_csect.x_stab = 0;
5827       a->x_csect.x_snstab = 0;
5828
5829       /* Don't let the COFF backend resort these symbols.  */
5830       symbol_get_bfdsym (sym)->flags |= BSF_NOT_AT_END;
5831     }
5832   else if (S_GET_STORAGE_CLASS (sym) == C_BSTAT)
5833     {
5834       /* We want the value to be the symbol index of the referenced
5835          csect symbol.  BFD will do that for us if we set the right
5836          flags.  */
5837       asymbol *bsym = symbol_get_bfdsym (symbol_get_tc (sym)->within);
5838       combined_entry_type *c = coffsymbol (bsym)->native;
5839
5840       S_SET_VALUE (sym, (valueT) (size_t) c);
5841       coffsymbol (symbol_get_bfdsym (sym))->native->fix_value = 1;
5842     }
5843   else if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
5844     {
5845       symbolS *block;
5846       valueT base;
5847
5848       block = symbol_get_tc (sym)->within;
5849       if (block)
5850         {
5851           /* The value is the offset from the enclosing csect.  */
5852           symbolS *csect;
5853
5854           csect = symbol_get_tc (block)->within;
5855           resolve_symbol_value (csect);
5856           base = S_GET_VALUE (csect);
5857         }
5858       else
5859         base = 0;
5860
5861       S_SET_VALUE (sym, S_GET_VALUE (sym) - base);
5862     }
5863   else if (S_GET_STORAGE_CLASS (sym) == C_BINCL
5864            || S_GET_STORAGE_CLASS (sym) == C_EINCL)
5865     {
5866       /* We want the value to be a file offset into the line numbers.
5867          BFD will do that for us if we set the right flags.  We have
5868          already set the value correctly.  */
5869       coffsymbol (symbol_get_bfdsym (sym))->native->fix_line = 1;
5870     }
5871
5872   return 0;
5873 }
5874
5875 /* Adjust the symbol table.  This creates csect symbols for all
5876    absolute symbols.  */
5877
5878 void
5879 ppc_adjust_symtab (void)
5880 {
5881   symbolS *sym;
5882
5883   if (! ppc_saw_abs)
5884     return;
5885
5886   for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
5887     {
5888       symbolS *csect;
5889       int i;
5890       union internal_auxent *a;
5891
5892       if (S_GET_SEGMENT (sym) != absolute_section)
5893         continue;
5894
5895       csect = symbol_create (".abs[XO]", absolute_section,
5896                              S_GET_VALUE (sym), &zero_address_frag);
5897       symbol_get_bfdsym (csect)->value = S_GET_VALUE (sym);
5898       S_SET_STORAGE_CLASS (csect, C_HIDEXT);
5899       i = S_GET_NUMBER_AUXILIARY (csect);
5900       S_SET_NUMBER_AUXILIARY (csect, i + 1);
5901       a = &coffsymbol (symbol_get_bfdsym (csect))->native[i + 1].u.auxent;
5902       a->x_csect.x_scnlen.l = 0;
5903       a->x_csect.x_smtyp = XTY_SD;
5904       a->x_csect.x_parmhash = 0;
5905       a->x_csect.x_snhash = 0;
5906       a->x_csect.x_smclas = XMC_XO;
5907       a->x_csect.x_stab = 0;
5908       a->x_csect.x_snstab = 0;
5909
5910       symbol_insert (csect, sym, &symbol_rootP, &symbol_lastP);
5911
5912       i = S_GET_NUMBER_AUXILIARY (sym);
5913       a = &coffsymbol (symbol_get_bfdsym (sym))->native[i].u.auxent;
5914       a->x_csect.x_scnlen.p = coffsymbol (symbol_get_bfdsym (csect))->native;
5915       coffsymbol (symbol_get_bfdsym (sym))->native[i].fix_scnlen = 1;
5916     }
5917
5918   ppc_saw_abs = FALSE;
5919 }
5920
5921 /* Set the VMA for a section.  This is called on all the sections in
5922    turn.  */
5923
5924 void
5925 ppc_frob_section (asection *sec)
5926 {
5927   static bfd_vma vma = 0;
5928
5929   /* Dwarf sections start at 0.  */
5930   if (bfd_get_section_flags (NULL, sec) & SEC_DEBUGGING)
5931     return;
5932
5933   vma = md_section_align (sec, vma);
5934   bfd_set_section_vma (stdoutput, sec, vma);
5935   vma += bfd_section_size (stdoutput, sec);
5936 }
5937
5938 #endif /* OBJ_XCOFF */
5939 \f
5940 char *
5941 md_atof (int type, char *litp, int *sizep)
5942 {
5943   return ieee_md_atof (type, litp, sizep, target_big_endian);
5944 }
5945
5946 /* Write a value out to the object file, using the appropriate
5947    endianness.  */
5948
5949 void
5950 md_number_to_chars (char *buf, valueT val, int n)
5951 {
5952   if (target_big_endian)
5953     number_to_chars_bigendian (buf, val, n);
5954   else
5955     number_to_chars_littleendian (buf, val, n);
5956 }
5957
5958 /* Align a section (I don't know why this is machine dependent).  */
5959
5960 valueT
5961 md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT addr)
5962 {
5963 #ifdef OBJ_ELF
5964   return addr;
5965 #else
5966   int align = bfd_get_section_alignment (stdoutput, seg);
5967
5968   return ((addr + (1 << align) - 1) & (-1 << align));
5969 #endif
5970 }
5971
5972 /* We don't have any form of relaxing.  */
5973
5974 int
5975 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
5976                                asection *seg ATTRIBUTE_UNUSED)
5977 {
5978   abort ();
5979   return 0;
5980 }
5981
5982 /* Convert a machine dependent frag.  We never generate these.  */
5983
5984 void
5985 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
5986                  asection *sec ATTRIBUTE_UNUSED,
5987                  fragS *fragp ATTRIBUTE_UNUSED)
5988 {
5989   abort ();
5990 }
5991
5992 /* We have no need to default values of symbols.  */
5993
5994 symbolS *
5995 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
5996 {
5997   return 0;
5998 }
5999 \f
6000 /* Functions concerning relocs.  */
6001
6002 /* The location from which a PC relative jump should be calculated,
6003    given a PC relative reloc.  */
6004
6005 long
6006 md_pcrel_from_section (fixS *fixp, segT sec ATTRIBUTE_UNUSED)
6007 {
6008   return fixp->fx_frag->fr_address + fixp->fx_where;
6009 }
6010
6011 #ifdef OBJ_XCOFF
6012
6013 /* This is called to see whether a fixup should be adjusted to use a
6014    section symbol.  We take the opportunity to change a fixup against
6015    a symbol in the TOC subsegment into a reloc against the
6016    corresponding .tc symbol.  */
6017
6018 int
6019 ppc_fix_adjustable (fixS *fix)
6020 {
6021   valueT val = resolve_symbol_value (fix->fx_addsy);
6022   segT symseg = S_GET_SEGMENT (fix->fx_addsy);
6023   TC_SYMFIELD_TYPE *tc;
6024
6025   if (symseg == absolute_section)
6026     return 0;
6027
6028   /* Always adjust symbols in debugging sections.  */
6029   if (bfd_get_section_flags (stdoutput, symseg) & SEC_DEBUGGING)
6030     return 1;
6031
6032   if (ppc_toc_csect != (symbolS *) NULL
6033       && fix->fx_addsy != ppc_toc_csect
6034       && symseg == data_section
6035       && val >= ppc_toc_frag->fr_address
6036       && (ppc_after_toc_frag == (fragS *) NULL
6037           || val < ppc_after_toc_frag->fr_address))
6038     {
6039       symbolS *sy;
6040
6041       for (sy = symbol_next (ppc_toc_csect);
6042            sy != (symbolS *) NULL;
6043            sy = symbol_next (sy))
6044         {
6045           TC_SYMFIELD_TYPE *sy_tc = symbol_get_tc (sy);
6046
6047           if (sy_tc->symbol_class == XMC_TC0)
6048             continue;
6049           if (sy_tc->symbol_class != XMC_TC)
6050             break;
6051           if (val == resolve_symbol_value (sy))
6052             {
6053               fix->fx_addsy = sy;
6054               fix->fx_addnumber = val - ppc_toc_frag->fr_address;
6055               return 0;
6056             }
6057         }
6058
6059       as_bad_where (fix->fx_file, fix->fx_line,
6060                     _("symbol in .toc does not match any .tc"));
6061     }
6062
6063   /* Possibly adjust the reloc to be against the csect.  */
6064   tc = symbol_get_tc (fix->fx_addsy);
6065   if (tc->subseg == 0
6066       && tc->symbol_class != XMC_TC0
6067       && tc->symbol_class != XMC_TC
6068       && symseg != bss_section
6069       /* Don't adjust if this is a reloc in the toc section.  */
6070       && (symseg != data_section
6071           || ppc_toc_csect == NULL
6072           || val < ppc_toc_frag->fr_address
6073           || (ppc_after_toc_frag != NULL
6074               && val >= ppc_after_toc_frag->fr_address)))
6075     {
6076       symbolS *csect = tc->within;
6077
6078       /* If the symbol was not declared by a label (eg: a section symbol),
6079          use the section instead of the csect.  This doesn't happen in
6080          normal AIX assembly code.  */
6081       if (csect == NULL)
6082         csect = seg_info (symseg)->sym;
6083
6084       fix->fx_offset += val - symbol_get_frag (csect)->fr_address;
6085       fix->fx_addsy = csect;
6086
6087       return 0;
6088     }
6089
6090   /* Adjust a reloc against a .lcomm symbol to be against the base
6091      .lcomm.  */
6092   if (symseg == bss_section
6093       && ! S_IS_EXTERNAL (fix->fx_addsy))
6094     {
6095       symbolS *sy = symbol_get_frag (fix->fx_addsy)->fr_symbol;
6096
6097       fix->fx_offset += val - resolve_symbol_value (sy);
6098       fix->fx_addsy = sy;
6099     }
6100
6101   return 0;
6102 }
6103
6104 /* A reloc from one csect to another must be kept.  The assembler
6105    will, of course, keep relocs between sections, and it will keep
6106    absolute relocs, but we need to force it to keep PC relative relocs
6107    between two csects in the same section.  */
6108
6109 int
6110 ppc_force_relocation (fixS *fix)
6111 {
6112   /* At this point fix->fx_addsy should already have been converted to
6113      a csect symbol.  If the csect does not include the fragment, then
6114      we need to force the relocation.  */
6115   if (fix->fx_pcrel
6116       && fix->fx_addsy != NULL
6117       && symbol_get_tc (fix->fx_addsy)->subseg != 0
6118       && ((symbol_get_frag (fix->fx_addsy)->fr_address
6119            > fix->fx_frag->fr_address)
6120           || (symbol_get_tc (fix->fx_addsy)->next != NULL
6121               && (symbol_get_frag (symbol_get_tc (fix->fx_addsy)->next)->fr_address
6122                   <= fix->fx_frag->fr_address))))
6123     return 1;
6124
6125   return generic_force_reloc (fix);
6126 }
6127
6128 void
6129 ppc_new_dot_label (symbolS *sym)
6130 {
6131   /* Anchor this label to the current csect for relocations.  */
6132   symbol_get_tc (sym)->within = ppc_current_csect;
6133 }
6134
6135 #endif /* OBJ_XCOFF */
6136
6137 #ifdef OBJ_ELF
6138 /* If this function returns non-zero, it guarantees that a relocation
6139    will be emitted for a fixup.  */
6140
6141 int
6142 ppc_force_relocation (fixS *fix)
6143 {
6144   /* Branch prediction relocations must force a relocation, as must
6145      the vtable description relocs.  */
6146   switch (fix->fx_r_type)
6147     {
6148     case BFD_RELOC_PPC_B16_BRTAKEN:
6149     case BFD_RELOC_PPC_B16_BRNTAKEN:
6150     case BFD_RELOC_PPC_BA16_BRTAKEN:
6151     case BFD_RELOC_PPC_BA16_BRNTAKEN:
6152     case BFD_RELOC_24_PLT_PCREL:
6153     case BFD_RELOC_PPC64_TOC:
6154       return 1;
6155     default:
6156       break;
6157     }
6158
6159   if (fix->fx_r_type >= BFD_RELOC_PPC_TLS
6160       && fix->fx_r_type <= BFD_RELOC_PPC64_DTPREL16_HIGHESTA)
6161     return 1;
6162
6163   return generic_force_reloc (fix);
6164 }
6165
6166 int
6167 ppc_fix_adjustable (fixS *fix)
6168 {
6169   return (fix->fx_r_type != BFD_RELOC_16_GOTOFF
6170           && fix->fx_r_type != BFD_RELOC_LO16_GOTOFF
6171           && fix->fx_r_type != BFD_RELOC_HI16_GOTOFF
6172           && fix->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
6173           && fix->fx_r_type != BFD_RELOC_PPC64_GOT16_DS
6174           && fix->fx_r_type != BFD_RELOC_PPC64_GOT16_LO_DS
6175           && fix->fx_r_type != BFD_RELOC_GPREL16
6176           && fix->fx_r_type != BFD_RELOC_VTABLE_INHERIT
6177           && fix->fx_r_type != BFD_RELOC_VTABLE_ENTRY
6178           && !(fix->fx_r_type >= BFD_RELOC_PPC_TLS
6179                && fix->fx_r_type <= BFD_RELOC_PPC64_DTPREL16_HIGHESTA));
6180 }
6181 #endif
6182
6183 void
6184 ppc_frag_check (struct frag *fragP)
6185 {
6186   if (!fragP->has_code)
6187     return;
6188
6189   if (ppc_mach() == bfd_mach_ppc_vle)
6190     {
6191       if (((fragP->fr_address + fragP->insn_addr) & 1) != 0)
6192         as_bad (_("instruction address is not a multiple of 2"));
6193     }
6194   else
6195     {
6196       if (((fragP->fr_address + fragP->insn_addr) & 3) != 0)
6197         as_bad (_("instruction address is not a multiple of 4"));
6198     }
6199 }
6200
6201 /* Implement HANDLE_ALIGN.  This writes the NOP pattern into an
6202    rs_align_code frag.  */
6203
6204 void
6205 ppc_handle_align (struct frag *fragP)
6206 {
6207   valueT count = (fragP->fr_next->fr_address
6208                   - (fragP->fr_address + fragP->fr_fix));
6209
6210   if (ppc_mach() == bfd_mach_ppc_vle && count != 0 && (count & 1) == 0)
6211     {
6212       char *dest = fragP->fr_literal + fragP->fr_fix;
6213
6214       fragP->fr_var = 2;
6215       md_number_to_chars (dest, 0x4400, 2);
6216     }
6217   else if (count != 0 && (count & 3) == 0)
6218     {
6219       char *dest = fragP->fr_literal + fragP->fr_fix;
6220
6221       fragP->fr_var = 4;
6222
6223       if (count > 4 * nop_limit && count < 0x2000000)
6224         {
6225           struct frag *rest;
6226
6227           /* Make a branch, then follow with nops.  Insert another
6228              frag to handle the nops.  */
6229           md_number_to_chars (dest, 0x48000000 + count, 4);
6230           count -= 4;
6231           if (count == 0)
6232             return;
6233
6234           rest = xmalloc (SIZEOF_STRUCT_FRAG + 4);
6235           memcpy (rest, fragP, SIZEOF_STRUCT_FRAG);
6236           fragP->fr_next = rest;
6237           fragP = rest;
6238           rest->fr_address += rest->fr_fix + 4;
6239           rest->fr_fix = 0;
6240           /* If we leave the next frag as rs_align_code we'll come here
6241              again, resulting in a bunch of branches rather than a
6242              branch followed by nops.  */
6243           rest->fr_type = rs_align;
6244           dest = rest->fr_literal;
6245         }
6246
6247       md_number_to_chars (dest, 0x60000000, 4);
6248
6249       if ((ppc_cpu & PPC_OPCODE_POWER6) != 0
6250           || (ppc_cpu & PPC_OPCODE_POWER7) != 0)
6251         {
6252           /* For power6 and power7, we want the last nop to be a group
6253              terminating one.  Do this by inserting an rs_fill frag immediately
6254              after this one, with its address set to the last nop location.
6255              This will automatically reduce the number of nops in the current
6256              frag by one.  */
6257           if (count > 4)
6258             {
6259               struct frag *group_nop = xmalloc (SIZEOF_STRUCT_FRAG + 4);
6260
6261               memcpy (group_nop, fragP, SIZEOF_STRUCT_FRAG);
6262               group_nop->fr_address = group_nop->fr_next->fr_address - 4;
6263               group_nop->fr_fix = 0;
6264               group_nop->fr_offset = 1;
6265               group_nop->fr_type = rs_fill;
6266               fragP->fr_next = group_nop;
6267               dest = group_nop->fr_literal;
6268             }
6269
6270           if ((ppc_cpu & PPC_OPCODE_POWER7) != 0)
6271             {
6272               if (ppc_cpu & PPC_OPCODE_E500MC)
6273                 /* e500mc group terminating nop: "ori 0,0,0".  */
6274                 md_number_to_chars (dest, 0x60000000, 4);
6275               else
6276                 /* power7 group terminating nop: "ori 2,2,0".  */
6277                 md_number_to_chars (dest, 0x60420000, 4);
6278             }
6279           else
6280             /* power6 group terminating nop: "ori 1,1,0".  */
6281             md_number_to_chars (dest, 0x60210000, 4);
6282         }
6283     }
6284 }
6285
6286 /* Apply a fixup to the object code.  This is called for all the
6287    fixups we generated by the calls to fix_new_exp, above.  */
6288
6289 void
6290 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
6291 {
6292   valueT value = * valP;
6293
6294 #ifdef OBJ_ELF
6295   if (fixP->fx_addsy != NULL)
6296     {
6297       /* Hack around bfd_install_relocation brain damage.  */
6298       if (fixP->fx_pcrel)
6299         value += fixP->fx_frag->fr_address + fixP->fx_where;
6300     }
6301   else
6302     fixP->fx_done = 1;
6303 #else
6304   /* FIXME FIXME FIXME: The value we are passed in *valP includes
6305      the symbol values.  If we are doing this relocation the code in
6306      write.c is going to call bfd_install_relocation, which is also
6307      going to use the symbol value.  That means that if the reloc is
6308      fully resolved we want to use *valP since bfd_install_relocation is
6309      not being used.
6310      However, if the reloc is not fully resolved we do not want to
6311      use *valP, and must use fx_offset instead.  If the relocation
6312      is PC-relative, we then need to re-apply md_pcrel_from_section
6313      to this new relocation value.  */
6314   if (fixP->fx_addsy == (symbolS *) NULL)
6315     fixP->fx_done = 1;
6316
6317   else
6318     {
6319       value = fixP->fx_offset;
6320       if (fixP->fx_pcrel)
6321         value -= md_pcrel_from_section (fixP, seg);
6322     }
6323 #endif
6324
6325   if (fixP->fx_subsy != (symbolS *) NULL)
6326     {
6327       /* We can't actually support subtracting a symbol.  */
6328       as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
6329     }
6330
6331   if (fixP->fx_pcrel_adjust != 0)
6332     {
6333       /* Handle relocs in an insn.  */
6334
6335       int opindex = fixP->fx_pcrel_adjust & 0xff;
6336       const struct powerpc_operand *operand = &powerpc_operands[opindex];
6337       char *where;
6338       unsigned long insn;
6339       offsetT fieldval;
6340
6341 #ifdef OBJ_XCOFF
6342       /* An instruction like `lwz 9,sym(30)' when `sym' is not a TOC symbol
6343          does not generate a reloc.  It uses the offset of `sym' within its
6344          csect.  Other usages, such as `.long sym', generate relocs.  This
6345          is the documented behaviour of non-TOC symbols.  */
6346       if ((operand->flags & PPC_OPERAND_PARENS) != 0
6347           && (operand->bitm & 0xfff0) == 0xfff0
6348           && operand->shift == 0
6349           && (operand->insert == NULL || ppc_obj64)
6350           && fixP->fx_addsy != NULL
6351           && symbol_get_tc (fixP->fx_addsy)->subseg != 0
6352           && symbol_get_tc (fixP->fx_addsy)->symbol_class != XMC_TC
6353           && symbol_get_tc (fixP->fx_addsy)->symbol_class != XMC_TC0
6354           && S_GET_SEGMENT (fixP->fx_addsy) != bss_section)
6355         {
6356           value = fixP->fx_offset;
6357           fixP->fx_done = 1;
6358         }
6359 #endif
6360       fieldval = value;
6361       switch (fixP->fx_r_type)
6362         {
6363 #ifdef OBJ_ELF
6364         case BFD_RELOC_PPC64_ADDR16_LO_DS:
6365           if (fixP->fx_pcrel)
6366             goto bad_pcrel;
6367           /* fall through */
6368 #endif
6369         case BFD_RELOC_LO16:
6370           if (fixP->fx_pcrel)
6371             fixP->fx_r_type = BFD_RELOC_LO16_PCREL;
6372           /* fall through */
6373         case BFD_RELOC_LO16_PCREL:
6374           fieldval = SEX16 (value);
6375           break;
6376
6377         case BFD_RELOC_HI16:
6378           if (fixP->fx_pcrel)
6379             fixP->fx_r_type = BFD_RELOC_HI16_PCREL;
6380           /* fall through */
6381         case BFD_RELOC_HI16_PCREL:
6382           fieldval = SEX16 (PPC_HI (value));
6383           break;
6384
6385         case BFD_RELOC_HI16_S:
6386           if (fixP->fx_pcrel)
6387             fixP->fx_r_type = BFD_RELOC_HI16_S_PCREL;
6388           /* fall through */
6389         case BFD_RELOC_HI16_S_PCREL:
6390           fieldval = SEX16 (PPC_HA (value));
6391           break;
6392
6393 #ifdef OBJ_ELF
6394         case BFD_RELOC_PPC64_HIGHER:
6395           if (fixP->fx_pcrel)
6396             goto bad_pcrel;
6397           fieldval = SEX16 (PPC_HIGHER (value));
6398           break;
6399
6400         case BFD_RELOC_PPC64_HIGHER_S:
6401           if (fixP->fx_pcrel)
6402             goto bad_pcrel;
6403           fieldval = SEX16 (PPC_HIGHERA (value));
6404           break;
6405
6406         case BFD_RELOC_PPC64_HIGHEST:
6407           if (fixP->fx_pcrel)
6408             goto bad_pcrel;
6409           fieldval = SEX16 (PPC_HIGHEST (value));
6410           break;
6411
6412         case BFD_RELOC_PPC64_HIGHEST_S:
6413           if (fixP->fx_pcrel)
6414             goto bad_pcrel;
6415           fieldval = SEX16 (PPC_HIGHESTA (value));
6416           break;
6417
6418           /* The following relocs can't be calculated by the assembler.
6419              Leave the field zero.  */
6420         case BFD_RELOC_PPC_TPREL16:
6421         case BFD_RELOC_PPC_TPREL16_LO:
6422         case BFD_RELOC_PPC_TPREL16_HI:
6423         case BFD_RELOC_PPC_TPREL16_HA:
6424         case BFD_RELOC_PPC_DTPREL16:
6425         case BFD_RELOC_PPC_DTPREL16_LO:
6426         case BFD_RELOC_PPC_DTPREL16_HI:
6427         case BFD_RELOC_PPC_DTPREL16_HA:
6428         case BFD_RELOC_PPC_GOT_TLSGD16:
6429         case BFD_RELOC_PPC_GOT_TLSGD16_LO:
6430         case BFD_RELOC_PPC_GOT_TLSGD16_HI:
6431         case BFD_RELOC_PPC_GOT_TLSGD16_HA:
6432         case BFD_RELOC_PPC_GOT_TLSLD16:
6433         case BFD_RELOC_PPC_GOT_TLSLD16_LO:
6434         case BFD_RELOC_PPC_GOT_TLSLD16_HI:
6435         case BFD_RELOC_PPC_GOT_TLSLD16_HA:
6436         case BFD_RELOC_PPC_GOT_TPREL16:
6437         case BFD_RELOC_PPC_GOT_TPREL16_LO:
6438         case BFD_RELOC_PPC_GOT_TPREL16_HI:
6439         case BFD_RELOC_PPC_GOT_TPREL16_HA:
6440         case BFD_RELOC_PPC_GOT_DTPREL16:
6441         case BFD_RELOC_PPC_GOT_DTPREL16_LO:
6442         case BFD_RELOC_PPC_GOT_DTPREL16_HI:
6443         case BFD_RELOC_PPC_GOT_DTPREL16_HA:
6444         case BFD_RELOC_PPC64_TPREL16_DS:
6445         case BFD_RELOC_PPC64_TPREL16_LO_DS:
6446         case BFD_RELOC_PPC64_TPREL16_HIGHER:
6447         case BFD_RELOC_PPC64_TPREL16_HIGHERA:
6448         case BFD_RELOC_PPC64_TPREL16_HIGHEST:
6449         case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
6450         case BFD_RELOC_PPC64_DTPREL16_DS:
6451         case BFD_RELOC_PPC64_DTPREL16_LO_DS:
6452         case BFD_RELOC_PPC64_DTPREL16_HIGHER:
6453         case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
6454         case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
6455         case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
6456           gas_assert (fixP->fx_addsy != NULL);
6457           S_SET_THREAD_LOCAL (fixP->fx_addsy);
6458           fieldval = 0;
6459           if (fixP->fx_pcrel)
6460             goto bad_pcrel;
6461           break;
6462
6463           /* These also should leave the field zero for the same
6464              reason.  Note that older versions of gas wrote values
6465              here.  If we want to go back to the old behaviour, then
6466              all _LO and _LO_DS cases will need to be treated like
6467              BFD_RELOC_LO16_PCREL above.  Similarly for _HI etc.  */
6468         case BFD_RELOC_16_GOTOFF:
6469         case BFD_RELOC_LO16_GOTOFF:
6470         case BFD_RELOC_HI16_GOTOFF:
6471         case BFD_RELOC_HI16_S_GOTOFF:
6472         case BFD_RELOC_LO16_PLTOFF:
6473         case BFD_RELOC_HI16_PLTOFF:
6474         case BFD_RELOC_HI16_S_PLTOFF:
6475         case BFD_RELOC_GPREL16:
6476         case BFD_RELOC_16_BASEREL:
6477         case BFD_RELOC_LO16_BASEREL:
6478         case BFD_RELOC_HI16_BASEREL:
6479         case BFD_RELOC_HI16_S_BASEREL:
6480         case BFD_RELOC_PPC_TOC16:
6481         case BFD_RELOC_PPC64_TOC16_LO:
6482         case BFD_RELOC_PPC64_TOC16_HI:
6483         case BFD_RELOC_PPC64_TOC16_HA:
6484         case BFD_RELOC_PPC64_PLTGOT16:
6485         case BFD_RELOC_PPC64_PLTGOT16_LO:
6486         case BFD_RELOC_PPC64_PLTGOT16_HI:
6487         case BFD_RELOC_PPC64_PLTGOT16_HA:
6488         case BFD_RELOC_PPC64_GOT16_DS:
6489         case BFD_RELOC_PPC64_GOT16_LO_DS:
6490         case BFD_RELOC_PPC64_PLT16_LO_DS:
6491         case BFD_RELOC_PPC64_SECTOFF_DS:
6492         case BFD_RELOC_PPC64_SECTOFF_LO_DS:
6493         case BFD_RELOC_PPC64_TOC16_DS:
6494         case BFD_RELOC_PPC64_TOC16_LO_DS:
6495         case BFD_RELOC_PPC64_PLTGOT16_DS:
6496         case BFD_RELOC_PPC64_PLTGOT16_LO_DS:
6497         case BFD_RELOC_PPC_EMB_NADDR16:
6498         case BFD_RELOC_PPC_EMB_NADDR16_LO:
6499         case BFD_RELOC_PPC_EMB_NADDR16_HI:
6500         case BFD_RELOC_PPC_EMB_NADDR16_HA:
6501         case BFD_RELOC_PPC_EMB_SDAI16:
6502         case BFD_RELOC_PPC_EMB_SDA2I16:
6503         case BFD_RELOC_PPC_EMB_SDA2REL:
6504         case BFD_RELOC_PPC_EMB_SDA21:
6505         case BFD_RELOC_PPC_EMB_MRKREF:
6506         case BFD_RELOC_PPC_EMB_RELSEC16:
6507         case BFD_RELOC_PPC_EMB_RELST_LO:
6508         case BFD_RELOC_PPC_EMB_RELST_HI:
6509         case BFD_RELOC_PPC_EMB_RELST_HA:
6510         case BFD_RELOC_PPC_EMB_BIT_FLD:
6511         case BFD_RELOC_PPC_EMB_RELSDA:
6512         case BFD_RELOC_PPC_VLE_SDA21:
6513         case BFD_RELOC_PPC_VLE_SDA21_LO:
6514         case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
6515         case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
6516         case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
6517         case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
6518         case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
6519         case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
6520           gas_assert (fixP->fx_addsy != NULL);
6521           /* Fall thru */
6522
6523         case BFD_RELOC_PPC_TLS:
6524         case BFD_RELOC_PPC_TLSGD:
6525         case BFD_RELOC_PPC_TLSLD:
6526           fieldval = 0;
6527           if (fixP->fx_pcrel)
6528             goto bad_pcrel;
6529           break;
6530 #endif
6531
6532         default:
6533           break;
6534         }
6535
6536 #ifdef OBJ_ELF
6537 /* powerpc uses RELA style relocs, so if emitting a reloc the field
6538    contents can stay at zero.  */
6539 #define APPLY_RELOC fixP->fx_done
6540 #else
6541 #define APPLY_RELOC 1
6542 #endif
6543       if ((fieldval != 0 && APPLY_RELOC) || operand->insert != NULL)
6544         {
6545           /* Fetch the instruction, insert the fully resolved operand
6546              value, and stuff the instruction back again.  */
6547           where = fixP->fx_frag->fr_literal + fixP->fx_where;
6548           if (target_big_endian)
6549             {
6550               if (fixP->fx_size == 4)
6551                 insn = bfd_getb32 ((unsigned char *) where);
6552               else
6553                 insn = bfd_getb16 ((unsigned char *) where);
6554             }
6555           else
6556             {
6557               if (fixP->fx_size == 4)
6558                 insn = bfd_getl32 ((unsigned char *) where);
6559               else
6560                 insn = bfd_getl16 ((unsigned char *) where);
6561             }
6562           insn = ppc_insert_operand (insn, operand, fieldval,
6563                                      fixP->tc_fix_data.ppc_cpu,
6564                                      fixP->fx_file, fixP->fx_line);
6565           if (target_big_endian)
6566             {
6567               if (fixP->fx_size == 4)
6568                 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
6569               else
6570                 bfd_putb16 ((bfd_vma) insn, (unsigned char *) where);
6571             }
6572           else
6573             {
6574               if (fixP->fx_size == 4)
6575                 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
6576               else
6577                 bfd_putl16 ((bfd_vma) insn, (unsigned char *) where);
6578             }
6579         }
6580
6581       if (fixP->fx_done)
6582         /* Nothing else to do here.  */
6583         return;
6584
6585       gas_assert (fixP->fx_addsy != NULL);
6586       if (fixP->fx_r_type == BFD_RELOC_UNUSED)
6587         {
6588           char *sfile;
6589           unsigned int sline;
6590
6591           /* Use expr_symbol_where to see if this is an expression
6592              symbol.  */
6593           if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
6594             as_bad_where (fixP->fx_file, fixP->fx_line,
6595                           _("unresolved expression that must be resolved"));
6596           else
6597             as_bad_where (fixP->fx_file, fixP->fx_line,
6598                           _("unsupported relocation against %s"),
6599                           S_GET_NAME (fixP->fx_addsy));
6600           fixP->fx_done = 1;
6601           return;
6602         }
6603     }
6604   else
6605     {
6606       /* Handle relocs in data.  */
6607       switch (fixP->fx_r_type)
6608         {
6609         case BFD_RELOC_CTOR:
6610           if (ppc_obj64)
6611             goto ctor64;
6612           /* fall through */
6613
6614         case BFD_RELOC_32:
6615           if (fixP->fx_pcrel)
6616             fixP->fx_r_type = BFD_RELOC_32_PCREL;
6617           /* fall through */
6618
6619         case BFD_RELOC_32_PCREL:
6620         case BFD_RELOC_RVA:
6621           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6622                               value, 4);
6623           break;
6624
6625         case BFD_RELOC_64:
6626         ctor64:
6627           if (fixP->fx_pcrel)
6628             fixP->fx_r_type = BFD_RELOC_64_PCREL;
6629           /* fall through */
6630
6631         case BFD_RELOC_64_PCREL:
6632           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6633                               value, 8);
6634           break;
6635
6636         case BFD_RELOC_16:
6637           if (fixP->fx_pcrel)
6638             fixP->fx_r_type = BFD_RELOC_16_PCREL;
6639           /* fall through */
6640
6641         case BFD_RELOC_16_PCREL:
6642           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6643                               value, 2);
6644           break;
6645
6646         case BFD_RELOC_8:
6647           if (fixP->fx_pcrel)
6648             {
6649 #ifdef OBJ_ELF
6650             bad_pcrel:
6651 #endif
6652               if (fixP->fx_addsy)
6653                 {
6654                   char *sfile;
6655                   unsigned int sline;
6656
6657                   /* Use expr_symbol_where to see if this is an
6658                      expression symbol.  */
6659                   if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
6660                     as_bad_where (fixP->fx_file, fixP->fx_line,
6661                                   _("unresolved expression that must"
6662                                     " be resolved"));
6663                   else
6664                     as_bad_where (fixP->fx_file, fixP->fx_line,
6665                                   _("cannot emit PC relative %s relocation"
6666                                     " against %s"),
6667                                   bfd_get_reloc_code_name (fixP->fx_r_type),
6668                                   S_GET_NAME (fixP->fx_addsy));
6669                 }
6670               else
6671                 as_bad_where (fixP->fx_file, fixP->fx_line,
6672                               _("unable to resolve expression"));
6673               fixP->fx_done = 1;
6674             }
6675           else
6676             md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6677                                 value, 1);
6678           break;
6679
6680         case BFD_RELOC_VTABLE_INHERIT:
6681           if (fixP->fx_addsy
6682               && !S_IS_DEFINED (fixP->fx_addsy)
6683               && !S_IS_WEAK (fixP->fx_addsy))
6684             S_SET_WEAK (fixP->fx_addsy);
6685           /* Fall thru */
6686
6687         case BFD_RELOC_VTABLE_ENTRY:
6688           fixP->fx_done = 0;
6689           break;
6690
6691 #ifdef OBJ_ELF
6692           /* These can appear with @l etc. in data.  */
6693         case BFD_RELOC_LO16:
6694           if (fixP->fx_pcrel)
6695             fixP->fx_r_type = BFD_RELOC_LO16_PCREL;
6696         case BFD_RELOC_LO16_PCREL:
6697           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6698                               value, 2);
6699           break;
6700
6701         case BFD_RELOC_HI16:
6702           if (fixP->fx_pcrel)
6703             fixP->fx_r_type = BFD_RELOC_HI16_PCREL;
6704         case BFD_RELOC_HI16_PCREL:
6705           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6706                               PPC_HI (value), 2);
6707           break;
6708
6709         case BFD_RELOC_HI16_S:
6710           if (fixP->fx_pcrel)
6711             fixP->fx_r_type = BFD_RELOC_HI16_S_PCREL;
6712         case BFD_RELOC_HI16_S_PCREL:
6713           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6714                               PPC_HA (value), 2);
6715           break;
6716
6717         case BFD_RELOC_PPC64_HIGHER:
6718           if (fixP->fx_pcrel)
6719             goto bad_pcrel;
6720           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6721                               PPC_HIGHER (value), 2);
6722           break;
6723
6724         case BFD_RELOC_PPC64_HIGHER_S:
6725           if (fixP->fx_pcrel)
6726             goto bad_pcrel;
6727           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6728                               PPC_HIGHERA (value), 2);
6729           break;
6730
6731         case BFD_RELOC_PPC64_HIGHEST:
6732           if (fixP->fx_pcrel)
6733             goto bad_pcrel;
6734           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6735                               PPC_HIGHEST (value), 2);
6736           break;
6737
6738         case BFD_RELOC_PPC64_HIGHEST_S:
6739           if (fixP->fx_pcrel)
6740             goto bad_pcrel;
6741           md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
6742                               PPC_HIGHESTA (value), 2);
6743           break;
6744
6745         case BFD_RELOC_PPC_DTPMOD:
6746         case BFD_RELOC_PPC_TPREL:
6747         case BFD_RELOC_PPC_DTPREL:
6748           S_SET_THREAD_LOCAL (fixP->fx_addsy);
6749           break;
6750
6751           /* Just punt all of these to the linker.  */
6752         case BFD_RELOC_PPC_B16_BRTAKEN:
6753         case BFD_RELOC_PPC_B16_BRNTAKEN:
6754         case BFD_RELOC_16_GOTOFF:
6755         case BFD_RELOC_LO16_GOTOFF:
6756         case BFD_RELOC_HI16_GOTOFF:
6757         case BFD_RELOC_HI16_S_GOTOFF:
6758         case BFD_RELOC_LO16_PLTOFF:
6759         case BFD_RELOC_HI16_PLTOFF:
6760         case BFD_RELOC_HI16_S_PLTOFF:
6761         case BFD_RELOC_PPC_COPY:
6762         case BFD_RELOC_PPC_GLOB_DAT:
6763         case BFD_RELOC_16_BASEREL:
6764         case BFD_RELOC_LO16_BASEREL:
6765         case BFD_RELOC_HI16_BASEREL:
6766         case BFD_RELOC_HI16_S_BASEREL:
6767         case BFD_RELOC_PPC_TLS:
6768         case BFD_RELOC_PPC_DTPREL16_LO:
6769         case BFD_RELOC_PPC_DTPREL16_HI:
6770         case BFD_RELOC_PPC_DTPREL16_HA:
6771         case BFD_RELOC_PPC_TPREL16_LO:
6772         case BFD_RELOC_PPC_TPREL16_HI:
6773         case BFD_RELOC_PPC_TPREL16_HA:
6774         case BFD_RELOC_PPC_GOT_TLSGD16:
6775         case BFD_RELOC_PPC_GOT_TLSGD16_LO:
6776         case BFD_RELOC_PPC_GOT_TLSGD16_HI:
6777         case BFD_RELOC_PPC_GOT_TLSGD16_HA:
6778         case BFD_RELOC_PPC_GOT_TLSLD16:
6779         case BFD_RELOC_PPC_GOT_TLSLD16_LO:
6780         case BFD_RELOC_PPC_GOT_TLSLD16_HI:
6781         case BFD_RELOC_PPC_GOT_TLSLD16_HA:
6782         case BFD_RELOC_PPC_GOT_DTPREL16:
6783         case BFD_RELOC_PPC_GOT_DTPREL16_LO:
6784         case BFD_RELOC_PPC_GOT_DTPREL16_HI:
6785         case BFD_RELOC_PPC_GOT_DTPREL16_HA:
6786         case BFD_RELOC_PPC_GOT_TPREL16:
6787         case BFD_RELOC_PPC_GOT_TPREL16_LO:
6788         case BFD_RELOC_PPC_GOT_TPREL16_HI:
6789         case BFD_RELOC_PPC_GOT_TPREL16_HA:
6790         case BFD_RELOC_24_PLT_PCREL:
6791         case BFD_RELOC_PPC_LOCAL24PC:
6792         case BFD_RELOC_32_PLT_PCREL:
6793         case BFD_RELOC_GPREL16:
6794         case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
6795         case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
6796         case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
6797         case BFD_RELOC_PPC_EMB_NADDR32:
6798         case BFD_RELOC_PPC_EMB_NADDR16:
6799         case BFD_RELOC_PPC_EMB_NADDR16_LO:
6800         case BFD_RELOC_PPC_EMB_NADDR16_HI:
6801         case BFD_RELOC_PPC_EMB_NADDR16_HA:
6802         case BFD_RELOC_PPC_EMB_SDAI16:
6803         case BFD_RELOC_PPC_EMB_SDA2REL:
6804         case BFD_RELOC_PPC_EMB_SDA2I16:
6805         case BFD_RELOC_PPC_EMB_SDA21:
6806         case BFD_RELOC_PPC_VLE_SDA21_LO:
6807         case BFD_RELOC_PPC_EMB_MRKREF:
6808         case BFD_RELOC_PPC_EMB_RELSEC16:
6809         case BFD_RELOC_PPC_EMB_RELST_LO:
6810         case BFD_RELOC_PPC_EMB_RELST_HI:
6811         case BFD_RELOC_PPC_EMB_RELST_HA:
6812         case BFD_RELOC_PPC_EMB_BIT_FLD:
6813         case BFD_RELOC_PPC_EMB_RELSDA:
6814         case BFD_RELOC_PPC64_TOC:
6815         case BFD_RELOC_PPC_TOC16:
6816         case BFD_RELOC_PPC64_TOC16_LO:
6817         case BFD_RELOC_PPC64_TOC16_HI:
6818         case BFD_RELOC_PPC64_TOC16_HA:
6819         case BFD_RELOC_PPC64_DTPREL16_HIGHER:
6820         case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
6821         case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
6822         case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
6823         case BFD_RELOC_PPC64_TPREL16_HIGHER:
6824         case BFD_RELOC_PPC64_TPREL16_HIGHERA:
6825         case BFD_RELOC_PPC64_TPREL16_HIGHEST:
6826         case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
6827           fixP->fx_done = 0;
6828           break;
6829 #endif
6830
6831 #ifdef OBJ_XCOFF
6832         case BFD_RELOC_NONE:
6833           break;
6834 #endif
6835
6836         default:
6837           fprintf (stderr,
6838                    _("Gas failure, reloc value %d\n"), fixP->fx_r_type);
6839           fflush (stderr);
6840           abort ();
6841         }
6842     }
6843
6844 #ifdef OBJ_ELF
6845   ppc_elf_validate_fix (fixP, seg);
6846   fixP->fx_addnumber = value;
6847
6848   /* PowerPC uses RELA relocs, ie. the reloc addend is stored separately
6849      from the section contents.  If we are going to be emitting a reloc
6850      then the section contents are immaterial, so don't warn if they
6851      happen to overflow.  Leave such warnings to ld.  */
6852   if (!fixP->fx_done)
6853     fixP->fx_no_overflow = 1;
6854 #else
6855   if (fixP->fx_r_type != BFD_RELOC_PPC_TOC16)
6856     fixP->fx_addnumber = 0;
6857   else
6858     {
6859 #ifdef TE_PE
6860       fixP->fx_addnumber = 0;
6861 #else
6862       /* We want to use the offset within the toc, not the actual VMA
6863          of the symbol.  */
6864       fixP->fx_addnumber =
6865         - bfd_get_section_vma (stdoutput, S_GET_SEGMENT (fixP->fx_addsy))
6866         - S_GET_VALUE (ppc_toc_csect);
6867 #endif
6868     }
6869 #endif
6870 }
6871
6872 /* Generate a reloc for a fixup.  */
6873
6874 arelent *
6875 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
6876 {
6877   arelent *reloc;
6878
6879   reloc = (arelent *) xmalloc (sizeof (arelent));
6880
6881   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
6882   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
6883   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
6884   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
6885   if (reloc->howto == (reloc_howto_type *) NULL)
6886     {
6887       as_bad_where (fixp->fx_file, fixp->fx_line,
6888                     _("reloc %d not supported by object file format"),
6889                     (int) fixp->fx_r_type);
6890       return NULL;
6891     }
6892   reloc->addend = fixp->fx_addnumber;
6893
6894   return reloc;
6895 }
6896
6897 void
6898 ppc_cfi_frame_initial_instructions (void)
6899 {
6900   cfi_add_CFA_def_cfa (1, 0);
6901 }
6902
6903 int
6904 tc_ppc_regname_to_dw2regnum (char *regname)
6905 {
6906   unsigned int regnum = -1;
6907   unsigned int i;
6908   const char *p;
6909   char *q;
6910   static struct { char *name; int dw2regnum; } regnames[] =
6911     {
6912       { "sp", 1 }, { "r.sp", 1 }, { "rtoc", 2 }, { "r.toc", 2 },
6913       { "mq", 64 }, { "lr", 65 }, { "ctr", 66 }, { "ap", 67 },
6914       { "cr", 70 }, { "xer", 76 }, { "vrsave", 109 }, { "vscr", 110 },
6915       { "spe_acc", 111 }, { "spefscr", 112 }
6916     };
6917
6918   for (i = 0; i < ARRAY_SIZE (regnames); ++i)
6919     if (strcmp (regnames[i].name, regname) == 0)
6920       return regnames[i].dw2regnum;
6921
6922   if (regname[0] == 'r' || regname[0] == 'f' || regname[0] == 'v')
6923     {
6924       p = regname + 1 + (regname[1] == '.');
6925       regnum = strtoul (p, &q, 10);
6926       if (p == q || *q || regnum >= 32)
6927         return -1;
6928       if (regname[0] == 'f')
6929         regnum += 32;
6930       else if (regname[0] == 'v')
6931         regnum += 77;
6932     }
6933   else if (regname[0] == 'c' && regname[1] == 'r')
6934     {
6935       p = regname + 2 + (regname[2] == '.');
6936       if (p[0] < '0' || p[0] > '7' || p[1])
6937         return -1;
6938       regnum = p[0] - '0' + 68;
6939     }
6940   return regnum;
6941 }