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