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