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