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