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