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