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