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