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