2000-06-06 Michael Snyder <msnyder@seadog.cygnus.com>
[binutils.git] / gas / config / tc-ppc.c
blobb4e96688175ae31333be442f9f10e3123b1e511b
1 /* tc-ppc.c -- Assemble for the PowerPC or POWER (RS/6000)
2 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include <stdio.h>
24 #include <ctype.h>
25 #include "as.h"
26 #include "subsegs.h"
28 #include "opcode/ppc.h"
30 #ifdef OBJ_ELF
31 #include "elf/ppc.h"
32 #endif
34 #ifdef TE_PE
35 #include "coff/pe.h"
36 #endif
38 /* This is the assembler for the PowerPC or POWER (RS/6000) chips. */
40 /* Tell the main code what the endianness is. */
41 extern int target_big_endian;
43 /* Whether or not, we've set target_big_endian. */
44 static int set_target_endian = 0;
46 /* Whether to use user friendly register names. */
47 #ifndef TARGET_REG_NAMES_P
48 #ifdef TE_PE
49 #define TARGET_REG_NAMES_P true
50 #else
51 #define TARGET_REG_NAMES_P false
52 #endif
53 #endif
55 static boolean reg_names_p = TARGET_REG_NAMES_P;
57 static boolean register_name PARAMS ((expressionS *));
58 static void ppc_set_cpu PARAMS ((void));
59 static unsigned long ppc_insert_operand
60 PARAMS ((unsigned long insn, const struct powerpc_operand *operand,
61 offsetT val, char *file, unsigned int line));
62 static void ppc_macro PARAMS ((char *str, const struct powerpc_macro *macro));
63 static void ppc_byte PARAMS ((int));
64 static int ppc_is_toc_sym PARAMS ((symbolS *sym));
65 static void ppc_tc PARAMS ((int));
67 #ifdef OBJ_XCOFF
68 static void ppc_comm PARAMS ((int));
69 static void ppc_bb PARAMS ((int));
70 static void ppc_bc PARAMS ((int));
71 static void ppc_bf PARAMS ((int));
72 static void ppc_biei PARAMS ((int));
73 static void ppc_bs PARAMS ((int));
74 static void ppc_eb PARAMS ((int));
75 static void ppc_ec PARAMS ((int));
76 static void ppc_ef PARAMS ((int));
77 static void ppc_es PARAMS ((int));
78 static void ppc_csect PARAMS ((int));
79 static void ppc_change_csect PARAMS ((symbolS *));
80 static void ppc_function PARAMS ((int));
81 static void ppc_extern PARAMS ((int));
82 static void ppc_lglobl PARAMS ((int));
83 static void ppc_section PARAMS ((int));
84 static void ppc_named_section PARAMS ((int));
85 static void ppc_stabx PARAMS ((int));
86 static void ppc_rename PARAMS ((int));
87 static void ppc_toc PARAMS ((int));
88 static void ppc_xcoff_cons PARAMS ((int));
89 static void ppc_machine PARAMS ((int));
90 static void ppc_vbyte PARAMS ((int));
91 #endif
93 #ifdef OBJ_ELF
94 static bfd_reloc_code_real_type ppc_elf_suffix PARAMS ((char **, expressionS *));
95 static void ppc_elf_cons PARAMS ((int));
96 static void ppc_elf_rdata PARAMS ((int));
97 static void ppc_elf_lcomm PARAMS ((int));
98 static void ppc_elf_validate_fix PARAMS ((fixS *, segT));
99 #endif
101 #ifdef TE_PE
102 static void ppc_set_current_section PARAMS ((segT));
103 static void ppc_previous PARAMS ((int));
104 static void ppc_pdata PARAMS ((int));
105 static void ppc_ydata PARAMS ((int));
106 static void ppc_reldata PARAMS ((int));
107 static void ppc_rdata PARAMS ((int));
108 static void ppc_ualong PARAMS ((int));
109 static void ppc_znop PARAMS ((int));
110 static void ppc_pe_comm PARAMS ((int));
111 static void ppc_pe_section PARAMS ((int));
112 static void ppc_pe_function PARAMS ((int));
113 static void ppc_pe_tocd PARAMS ((int));
114 #endif
116 /* Generic assembler global variables which must be defined by all
117 targets. */
119 #ifdef OBJ_ELF
120 /* This string holds the chars that always start a comment. If the
121 pre-processor is disabled, these aren't very useful. The macro
122 tc_comment_chars points to this. We use this, rather than the
123 usual comment_chars, so that we can switch for Solaris conventions. */
124 static const char ppc_solaris_comment_chars[] = "#!";
125 static const char ppc_eabi_comment_chars[] = "#";
127 #ifdef TARGET_SOLARIS_COMMENT
128 const char *ppc_comment_chars = ppc_solaris_comment_chars;
129 #else
130 const char *ppc_comment_chars = ppc_eabi_comment_chars;
131 #endif
132 #else
133 const char comment_chars[] = "#";
134 #endif
136 /* Characters which start a comment at the beginning of a line. */
137 const char line_comment_chars[] = "#";
139 /* Characters which may be used to separate multiple commands on a
140 single line. */
141 const char line_separator_chars[] = ";";
143 /* Characters which are used to indicate an exponent in a floating
144 point number. */
145 const char EXP_CHARS[] = "eE";
147 /* Characters which mean that a number is a floating point constant,
148 as in 0d1.0. */
149 const char FLT_CHARS[] = "dD";
151 /* The target specific pseudo-ops which we support. */
153 const pseudo_typeS md_pseudo_table[] =
155 /* Pseudo-ops which must be overridden. */
156 { "byte", ppc_byte, 0 },
158 #ifdef OBJ_XCOFF
159 /* Pseudo-ops specific to the RS/6000 XCOFF format. Some of these
160 legitimately belong in the obj-*.c file. However, XCOFF is based
161 on COFF, and is only implemented for the RS/6000. We just use
162 obj-coff.c, and add what we need here. */
163 { "comm", ppc_comm, 0 },
164 { "lcomm", ppc_comm, 1 },
165 { "bb", ppc_bb, 0 },
166 { "bc", ppc_bc, 0 },
167 { "bf", ppc_bf, 0 },
168 { "bi", ppc_biei, 0 },
169 { "bs", ppc_bs, 0 },
170 { "csect", ppc_csect, 0 },
171 { "data", ppc_section, 'd' },
172 { "eb", ppc_eb, 0 },
173 { "ec", ppc_ec, 0 },
174 { "ef", ppc_ef, 0 },
175 { "ei", ppc_biei, 1 },
176 { "es", ppc_es, 0 },
177 { "extern", ppc_extern, 0 },
178 { "function", ppc_function, 0 },
179 { "lglobl", ppc_lglobl, 0 },
180 { "rename", ppc_rename, 0 },
181 { "section", ppc_named_section, 0 },
182 { "stabx", ppc_stabx, 0 },
183 { "text", ppc_section, 't' },
184 { "toc", ppc_toc, 0 },
185 { "long", ppc_xcoff_cons, 2 },
186 { "llong", ppc_xcoff_cons, 3 },
187 { "word", ppc_xcoff_cons, 1 },
188 { "short", ppc_xcoff_cons, 1 },
189 { "vbyte", ppc_vbyte, 0 },
190 { "machine", ppc_machine, 0 },
191 #endif
193 #ifdef OBJ_ELF
194 { "long", ppc_elf_cons, 4 },
195 { "word", ppc_elf_cons, 2 },
196 { "short", ppc_elf_cons, 2 },
197 { "rdata", ppc_elf_rdata, 0 },
198 { "rodata", ppc_elf_rdata, 0 },
199 { "lcomm", ppc_elf_lcomm, 0 },
200 #endif
202 #ifdef TE_PE
203 /* Pseudo-ops specific to the Windows NT PowerPC PE (coff) format */
204 { "previous", ppc_previous, 0 },
205 { "pdata", ppc_pdata, 0 },
206 { "ydata", ppc_ydata, 0 },
207 { "reldata", ppc_reldata, 0 },
208 { "rdata", ppc_rdata, 0 },
209 { "ualong", ppc_ualong, 0 },
210 { "znop", ppc_znop, 0 },
211 { "comm", ppc_pe_comm, 0 },
212 { "lcomm", ppc_pe_comm, 1 },
213 { "section", ppc_pe_section, 0 },
214 { "function", ppc_pe_function,0 },
215 { "tocd", ppc_pe_tocd, 0 },
216 #endif
218 /* This pseudo-op is used even when not generating XCOFF output. */
219 { "tc", ppc_tc, 0 },
221 { NULL, NULL, 0 }
225 /* Predefined register names if -mregnames (or default for Windows NT). */
226 /* In general, there are lots of them, in an attempt to be compatible */
227 /* with a number of other Windows NT assemblers. */
229 /* Structure to hold information about predefined registers. */
230 struct pd_reg
232 char *name;
233 int value;
236 /* List of registers that are pre-defined:
238 Each general register has predefined names of the form:
239 1. r<reg_num> which has the value <reg_num>.
240 2. r.<reg_num> which has the value <reg_num>.
242 Each floating point register has predefined names of the form:
243 1. f<reg_num> which has the value <reg_num>.
244 2. f.<reg_num> which has the value <reg_num>.
246 Each vector unit register has predefined names of the form:
247 1. v<reg_num> which has the value <reg_num>.
248 2. v.<reg_num> which has the value <reg_num>.
250 Each condition register has predefined names of the form:
251 1. cr<reg_num> which has the value <reg_num>.
252 2. cr.<reg_num> which has the value <reg_num>.
254 There are individual registers as well:
255 sp or r.sp has the value 1
256 rtoc or r.toc has the value 2
257 fpscr has the value 0
258 xer has the value 1
259 lr has the value 8
260 ctr has the value 9
261 pmr has the value 0
262 dar has the value 19
263 dsisr has the value 18
264 dec has the value 22
265 sdr1 has the value 25
266 srr0 has the value 26
267 srr1 has the value 27
269 The table is sorted. Suitable for searching by a binary search. */
271 static const struct pd_reg pre_defined_registers[] =
273 { "cr.0", 0 }, /* Condition Registers */
274 { "cr.1", 1 },
275 { "cr.2", 2 },
276 { "cr.3", 3 },
277 { "cr.4", 4 },
278 { "cr.5", 5 },
279 { "cr.6", 6 },
280 { "cr.7", 7 },
282 { "cr0", 0 },
283 { "cr1", 1 },
284 { "cr2", 2 },
285 { "cr3", 3 },
286 { "cr4", 4 },
287 { "cr5", 5 },
288 { "cr6", 6 },
289 { "cr7", 7 },
291 { "ctr", 9 },
293 { "dar", 19 }, /* Data Access Register */
294 { "dec", 22 }, /* Decrementer */
295 { "dsisr", 18 }, /* Data Storage Interrupt Status Register */
297 { "f.0", 0 }, /* Floating point registers */
298 { "f.1", 1 },
299 { "f.10", 10 },
300 { "f.11", 11 },
301 { "f.12", 12 },
302 { "f.13", 13 },
303 { "f.14", 14 },
304 { "f.15", 15 },
305 { "f.16", 16 },
306 { "f.17", 17 },
307 { "f.18", 18 },
308 { "f.19", 19 },
309 { "f.2", 2 },
310 { "f.20", 20 },
311 { "f.21", 21 },
312 { "f.22", 22 },
313 { "f.23", 23 },
314 { "f.24", 24 },
315 { "f.25", 25 },
316 { "f.26", 26 },
317 { "f.27", 27 },
318 { "f.28", 28 },
319 { "f.29", 29 },
320 { "f.3", 3 },
321 { "f.30", 30 },
322 { "f.31", 31 },
323 { "f.4", 4 },
324 { "f.5", 5 },
325 { "f.6", 6 },
326 { "f.7", 7 },
327 { "f.8", 8 },
328 { "f.9", 9 },
330 { "f0", 0 },
331 { "f1", 1 },
332 { "f10", 10 },
333 { "f11", 11 },
334 { "f12", 12 },
335 { "f13", 13 },
336 { "f14", 14 },
337 { "f15", 15 },
338 { "f16", 16 },
339 { "f17", 17 },
340 { "f18", 18 },
341 { "f19", 19 },
342 { "f2", 2 },
343 { "f20", 20 },
344 { "f21", 21 },
345 { "f22", 22 },
346 { "f23", 23 },
347 { "f24", 24 },
348 { "f25", 25 },
349 { "f26", 26 },
350 { "f27", 27 },
351 { "f28", 28 },
352 { "f29", 29 },
353 { "f3", 3 },
354 { "f30", 30 },
355 { "f31", 31 },
356 { "f4", 4 },
357 { "f5", 5 },
358 { "f6", 6 },
359 { "f7", 7 },
360 { "f8", 8 },
361 { "f9", 9 },
363 { "fpscr", 0 },
365 { "lr", 8 }, /* Link Register */
367 { "pmr", 0 },
369 { "r.0", 0 }, /* General Purpose Registers */
370 { "r.1", 1 },
371 { "r.10", 10 },
372 { "r.11", 11 },
373 { "r.12", 12 },
374 { "r.13", 13 },
375 { "r.14", 14 },
376 { "r.15", 15 },
377 { "r.16", 16 },
378 { "r.17", 17 },
379 { "r.18", 18 },
380 { "r.19", 19 },
381 { "r.2", 2 },
382 { "r.20", 20 },
383 { "r.21", 21 },
384 { "r.22", 22 },
385 { "r.23", 23 },
386 { "r.24", 24 },
387 { "r.25", 25 },
388 { "r.26", 26 },
389 { "r.27", 27 },
390 { "r.28", 28 },
391 { "r.29", 29 },
392 { "r.3", 3 },
393 { "r.30", 30 },
394 { "r.31", 31 },
395 { "r.4", 4 },
396 { "r.5", 5 },
397 { "r.6", 6 },
398 { "r.7", 7 },
399 { "r.8", 8 },
400 { "r.9", 9 },
402 { "r.sp", 1 }, /* Stack Pointer */
404 { "r.toc", 2 }, /* Pointer to the table of contents */
406 { "r0", 0 }, /* More general purpose registers */
407 { "r1", 1 },
408 { "r10", 10 },
409 { "r11", 11 },
410 { "r12", 12 },
411 { "r13", 13 },
412 { "r14", 14 },
413 { "r15", 15 },
414 { "r16", 16 },
415 { "r17", 17 },
416 { "r18", 18 },
417 { "r19", 19 },
418 { "r2", 2 },
419 { "r20", 20 },
420 { "r21", 21 },
421 { "r22", 22 },
422 { "r23", 23 },
423 { "r24", 24 },
424 { "r25", 25 },
425 { "r26", 26 },
426 { "r27", 27 },
427 { "r28", 28 },
428 { "r29", 29 },
429 { "r3", 3 },
430 { "r30", 30 },
431 { "r31", 31 },
432 { "r4", 4 },
433 { "r5", 5 },
434 { "r6", 6 },
435 { "r7", 7 },
436 { "r8", 8 },
437 { "r9", 9 },
439 { "rtoc", 2 }, /* Table of contents */
441 { "sdr1", 25 }, /* Storage Description Register 1 */
443 { "sp", 1 },
445 { "srr0", 26 }, /* Machine Status Save/Restore Register 0 */
446 { "srr1", 27 }, /* Machine Status Save/Restore Register 1 */
448 { "v.0", 0 }, /* Vector registers */
449 { "v.1", 1 },
450 { "v.10", 10 },
451 { "v.11", 11 },
452 { "v.12", 12 },
453 { "v.13", 13 },
454 { "v.14", 14 },
455 { "v.15", 15 },
456 { "v.16", 16 },
457 { "v.17", 17 },
458 { "v.18", 18 },
459 { "v.19", 19 },
460 { "v.2", 2 },
461 { "v.20", 20 },
462 { "v.21", 21 },
463 { "v.22", 22 },
464 { "v.23", 23 },
465 { "v.24", 24 },
466 { "v.25", 25 },
467 { "v.26", 26 },
468 { "v.27", 27 },
469 { "v.28", 28 },
470 { "v.29", 29 },
471 { "v.3", 3 },
472 { "v.30", 30 },
473 { "v.31", 31 },
474 { "v.4", 4 },
475 { "v.5", 5 },
476 { "v.6", 6 },
477 { "v.7", 7 },
478 { "v.8", 8 },
479 { "v.9", 9 },
481 { "v0", 0 },
482 { "v1", 1 },
483 { "v10", 10 },
484 { "v11", 11 },
485 { "v12", 12 },
486 { "v13", 13 },
487 { "v14", 14 },
488 { "v15", 15 },
489 { "v16", 16 },
490 { "v17", 17 },
491 { "v18", 18 },
492 { "v19", 19 },
493 { "v2", 2 },
494 { "v20", 20 },
495 { "v21", 21 },
496 { "v22", 22 },
497 { "v23", 23 },
498 { "v24", 24 },
499 { "v25", 25 },
500 { "v26", 26 },
501 { "v27", 27 },
502 { "v28", 28 },
503 { "v29", 29 },
504 { "v3", 3 },
505 { "v30", 30 },
506 { "v31", 31 },
507 { "v4", 4 },
508 { "v5", 5 },
509 { "v6", 6 },
510 { "v7", 7 },
511 { "v8", 8 },
512 { "v9", 9 },
514 { "xer", 1 },
518 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct pd_reg))
520 /* Given NAME, find the register number associated with that name, return
521 the integer value associated with the given name or -1 on failure. */
523 static int reg_name_search
524 PARAMS ((const struct pd_reg *, int, const char * name));
526 static int
527 reg_name_search (regs, regcount, name)
528 const struct pd_reg *regs;
529 int regcount;
530 const char *name;
532 int middle, low, high;
533 int cmp;
535 low = 0;
536 high = regcount - 1;
540 middle = (low + high) / 2;
541 cmp = strcasecmp (name, regs[middle].name);
542 if (cmp < 0)
543 high = middle - 1;
544 else if (cmp > 0)
545 low = middle + 1;
546 else
547 return regs[middle].value;
549 while (low <= high);
551 return -1;
555 * Summary of register_name().
557 * in: Input_line_pointer points to 1st char of operand.
559 * out: A expressionS.
560 * The operand may have been a register: in this case, X_op == O_register,
561 * X_add_number is set to the register number, and truth is returned.
562 * Input_line_pointer->(next non-blank) char after operand, or is in its
563 * original state.
566 static boolean
567 register_name (expressionP)
568 expressionS *expressionP;
570 int reg_number;
571 char *name;
572 char *start;
573 char c;
575 /* Find the spelling of the operand */
576 start = name = input_line_pointer;
577 if (name[0] == '%' && isalpha (name[1]))
578 name = ++input_line_pointer;
580 else if (!reg_names_p || !isalpha (name[0]))
581 return false;
583 c = get_symbol_end ();
584 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
586 /* look to see if it's in the register table */
587 if (reg_number >= 0)
589 expressionP->X_op = O_register;
590 expressionP->X_add_number = reg_number;
592 /* make the rest nice */
593 expressionP->X_add_symbol = NULL;
594 expressionP->X_op_symbol = NULL;
595 *input_line_pointer = c; /* put back the delimiting char */
596 return true;
598 else
600 /* reset the line as if we had not done anything */
601 *input_line_pointer = c; /* put back the delimiting char */
602 input_line_pointer = start; /* reset input_line pointer */
603 return false;
607 /* This function is called for each symbol seen in an expression. It
608 handles the special parsing which PowerPC assemblers are supposed
609 to use for condition codes. */
611 /* Whether to do the special parsing. */
612 static boolean cr_operand;
614 /* Names to recognize in a condition code. This table is sorted. */
615 static const struct pd_reg cr_names[] =
617 { "cr0", 0 },
618 { "cr1", 1 },
619 { "cr2", 2 },
620 { "cr3", 3 },
621 { "cr4", 4 },
622 { "cr5", 5 },
623 { "cr6", 6 },
624 { "cr7", 7 },
625 { "eq", 2 },
626 { "gt", 1 },
627 { "lt", 0 },
628 { "so", 3 },
629 { "un", 3 }
632 /* Parsing function. This returns non-zero if it recognized an
633 expression. */
636 ppc_parse_name (name, expr)
637 const char *name;
638 expressionS *expr;
640 int val;
642 if (! cr_operand)
643 return 0;
645 val = reg_name_search (cr_names, sizeof cr_names / sizeof cr_names[0],
646 name);
647 if (val < 0)
648 return 0;
650 expr->X_op = O_constant;
651 expr->X_add_number = val;
653 return 1;
656 /* Local variables. */
658 /* The type of processor we are assembling for. This is one or more
659 of the PPC_OPCODE flags defined in opcode/ppc.h. */
660 static int ppc_cpu = 0;
662 /* The size of the processor we are assembling for. This is either
663 PPC_OPCODE_32 or PPC_OPCODE_64. */
664 static unsigned long ppc_size = PPC_OPCODE_32;
666 /* Whether to target xcoff64 */
667 static int ppc_xcoff64 = 0;
669 /* Opcode hash table. */
670 static struct hash_control *ppc_hash;
672 /* Macro hash table. */
673 static struct hash_control *ppc_macro_hash;
675 #ifdef OBJ_ELF
676 /* What type of shared library support to use */
677 static enum { SHLIB_NONE, SHLIB_PIC, SHILB_MRELOCATABLE } shlib = SHLIB_NONE;
679 /* Flags to set in the elf header */
680 static flagword ppc_flags = 0;
682 /* Whether this is Solaris or not. */
683 #ifdef TARGET_SOLARIS_COMMENT
684 #define SOLARIS_P true
685 #else
686 #define SOLARIS_P false
687 #endif
689 static boolean msolaris = SOLARIS_P;
690 #endif
692 #ifdef OBJ_XCOFF
694 /* The RS/6000 assembler uses the .csect pseudo-op to generate code
695 using a bunch of different sections. These assembler sections,
696 however, are all encompassed within the .text or .data sections of
697 the final output file. We handle this by using different
698 subsegments within these main segments. */
700 /* Next subsegment to allocate within the .text segment. */
701 static subsegT ppc_text_subsegment = 2;
703 /* Linked list of csects in the text section. */
704 static symbolS *ppc_text_csects;
706 /* Next subsegment to allocate within the .data segment. */
707 static subsegT ppc_data_subsegment = 2;
709 /* Linked list of csects in the data section. */
710 static symbolS *ppc_data_csects;
712 /* The current csect. */
713 static symbolS *ppc_current_csect;
715 /* The RS/6000 assembler uses a TOC which holds addresses of functions
716 and variables. Symbols are put in the TOC with the .tc pseudo-op.
717 A special relocation is used when accessing TOC entries. We handle
718 the TOC as a subsegment within the .data segment. We set it up if
719 we see a .toc pseudo-op, and save the csect symbol here. */
720 static symbolS *ppc_toc_csect;
722 /* The first frag in the TOC subsegment. */
723 static fragS *ppc_toc_frag;
725 /* The first frag in the first subsegment after the TOC in the .data
726 segment. NULL if there are no subsegments after the TOC. */
727 static fragS *ppc_after_toc_frag;
729 /* The current static block. */
730 static symbolS *ppc_current_block;
732 /* The COFF debugging section; set by md_begin. This is not the
733 .debug section, but is instead the secret BFD section which will
734 cause BFD to set the section number of a symbol to N_DEBUG. */
735 static asection *ppc_coff_debug_section;
737 #endif /* OBJ_XCOFF */
739 #ifdef TE_PE
741 /* Various sections that we need for PE coff support. */
742 static segT ydata_section;
743 static segT pdata_section;
744 static segT reldata_section;
745 static segT rdata_section;
746 static segT tocdata_section;
748 /* The current section and the previous section. See ppc_previous. */
749 static segT ppc_previous_section;
750 static segT ppc_current_section;
752 #endif /* TE_PE */
754 #ifdef OBJ_ELF
755 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE" */
756 #endif /* OBJ_ELF */
758 #ifdef OBJ_ELF
759 CONST char *md_shortopts = "b:l:usm:K:VQ:";
760 #else
761 CONST char *md_shortopts = "um:";
762 #endif
763 struct option md_longopts[] = {
764 {NULL, no_argument, NULL, 0}
766 size_t md_longopts_size = sizeof(md_longopts);
769 md_parse_option (c, arg)
770 int c;
771 char *arg;
773 switch (c)
775 case 'u':
776 /* -u means that any undefined symbols should be treated as
777 external, which is the default for gas anyhow. */
778 break;
780 #ifdef OBJ_ELF
781 case 'l':
782 /* Solaris as takes -le (presumably for little endian). For completeness
783 sake, recognize -be also. */
784 if (strcmp (arg, "e") == 0)
786 target_big_endian = 0;
787 set_target_endian = 1;
789 else
790 return 0;
792 break;
794 case 'b':
795 if (strcmp (arg, "e") == 0)
797 target_big_endian = 1;
798 set_target_endian = 1;
800 else
801 return 0;
803 break;
805 case 'K':
806 /* Recognize -K PIC */
807 if (strcmp (arg, "PIC") == 0 || strcmp (arg, "pic") == 0)
809 shlib = SHLIB_PIC;
810 ppc_flags |= EF_PPC_RELOCATABLE_LIB;
812 else
813 return 0;
815 break;
816 #endif
818 /* a64 and a32 determine whether to use XCOFF64 or XCOFF32. */
819 case 'a':
820 if (strcmp (arg, "64") == 0)
821 ppc_xcoff64 = 1;
822 else if (strcmp (arg, "32") == 0)
823 ppc_xcoff64 = 0;
824 else
825 return 0;
826 break;
828 case 'm':
829 /* -mpwrx and -mpwr2 mean to assemble for the IBM POWER/2
830 (RIOS2). */
831 if (strcmp (arg, "pwrx") == 0 || strcmp (arg, "pwr2") == 0)
832 ppc_cpu = PPC_OPCODE_POWER | PPC_OPCODE_POWER2;
833 /* -mpwr means to assemble for the IBM POWER (RIOS1). */
834 else if (strcmp (arg, "pwr") == 0)
835 ppc_cpu = PPC_OPCODE_POWER;
836 /* -m601 means to assemble for the Motorola PowerPC 601, which includes
837 instructions that are holdovers from the Power. */
838 else if (strcmp (arg, "601") == 0)
839 ppc_cpu = PPC_OPCODE_PPC | PPC_OPCODE_601;
840 /* -mppc, -mppc32, -m603, and -m604 mean to assemble for the
841 Motorola PowerPC 603/604. */
842 else if (strcmp (arg, "ppc") == 0
843 || strcmp (arg, "ppc32") == 0
844 || strcmp (arg, "403") == 0
845 || strcmp (arg, "603") == 0
846 || strcmp (arg, "604") == 0)
847 ppc_cpu = PPC_OPCODE_PPC;
848 else if (strcmp (arg, "7400") == 0)
849 ppc_cpu = PPC_OPCODE_PPC | PPC_OPCODE_ALTIVEC;
850 /* -mppc64 and -m620 mean to assemble for the 64-bit PowerPC
851 620. */
852 else if (strcmp (arg, "ppc64") == 0 || strcmp (arg, "620") == 0)
854 ppc_cpu = PPC_OPCODE_PPC;
855 ppc_size = PPC_OPCODE_64;
857 else if (strcmp (arg, "ppc64bridge") == 0)
859 ppc_cpu = PPC_OPCODE_PPC | PPC_OPCODE_64_BRIDGE;
860 ppc_size = PPC_OPCODE_64;
862 /* -mcom means assemble for the common intersection between Power
863 and PowerPC. At present, we just allow the union, rather
864 than the intersection. */
865 else if (strcmp (arg, "com") == 0)
866 ppc_cpu = PPC_OPCODE_COMMON;
867 /* -many means to assemble for any architecture (PWR/PWRX/PPC). */
868 else if (strcmp (arg, "any") == 0)
869 ppc_cpu = PPC_OPCODE_ANY;
871 else if (strcmp (arg, "regnames") == 0)
872 reg_names_p = true;
874 else if (strcmp (arg, "no-regnames") == 0)
875 reg_names_p = false;
877 #ifdef OBJ_ELF
878 /* -mrelocatable/-mrelocatable-lib -- warn about initializations that require relocation */
879 else if (strcmp (arg, "relocatable") == 0)
881 shlib = SHILB_MRELOCATABLE;
882 ppc_flags |= EF_PPC_RELOCATABLE;
885 else if (strcmp (arg, "relocatable-lib") == 0)
887 shlib = SHILB_MRELOCATABLE;
888 ppc_flags |= EF_PPC_RELOCATABLE_LIB;
891 /* -memb, set embedded bit */
892 else if (strcmp (arg, "emb") == 0)
893 ppc_flags |= EF_PPC_EMB;
895 /* -mlittle/-mbig set the endianess */
896 else if (strcmp (arg, "little") == 0 || strcmp (arg, "little-endian") == 0)
898 target_big_endian = 0;
899 set_target_endian = 1;
902 else if (strcmp (arg, "big") == 0 || strcmp (arg, "big-endian") == 0)
904 target_big_endian = 1;
905 set_target_endian = 1;
908 else if (strcmp (arg, "solaris") == 0)
910 msolaris = true;
911 ppc_comment_chars = ppc_solaris_comment_chars;
914 else if (strcmp (arg, "no-solaris") == 0)
916 msolaris = false;
917 ppc_comment_chars = ppc_eabi_comment_chars;
919 #endif
920 else
922 as_bad (_("invalid switch -m%s"), arg);
923 return 0;
925 break;
927 #ifdef OBJ_ELF
928 /* -V: SVR4 argument to print version ID. */
929 case 'V':
930 print_version_id ();
931 break;
933 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
934 should be emitted or not. FIXME: Not implemented. */
935 case 'Q':
936 break;
938 /* Solaris takes -s to specify that .stabs go in a .stabs section,
939 rather than .stabs.excl, which is ignored by the linker.
940 FIXME: Not implemented. */
941 case 's':
942 if (arg)
943 return 0;
945 break;
946 #endif
948 default:
949 return 0;
952 return 1;
955 void
956 md_show_usage (stream)
957 FILE *stream;
959 fprintf(stream, _("\
960 PowerPC options:\n\
961 -u ignored\n\
962 -mpwrx, -mpwr2 generate code for IBM POWER/2 (RIOS2)\n\
963 -mpwr generate code for IBM POWER (RIOS1)\n\
964 -m601 generate code for Motorola PowerPC 601\n\
965 -mppc, -mppc32, -m403, -m603, -m604\n\
966 generate code for Motorola PowerPC 603/604\n\
967 -mppc64, -m620 generate code for Motorola PowerPC 620\n\
968 -mppc64bridge generate code for PowerPC 64, including bridge insns\n\
969 -mcom generate code Power/PowerPC common instructions\n\
970 -many generate code for any architecture (PWR/PWRX/PPC)\n\
971 -mregnames Allow symbolic names for registers\n\
972 -mno-regnames Do not allow symbolic names for registers\n"));
973 #ifdef OBJ_ELF
974 fprintf(stream, _("\
975 -mrelocatable support for GCC's -mrelocatble option\n\
976 -mrelocatable-lib support for GCC's -mrelocatble-lib option\n\
977 -memb set PPC_EMB bit in ELF flags\n\
978 -mlittle, -mlittle-endian\n\
979 generate code for a little endian machine\n\
980 -mbig, -mbig-endian generate code for a big endian machine\n\
981 -msolaris generate code for Solaris\n\
982 -mno-solaris do not generate code for Solaris\n\
983 -V print assembler version number\n\
984 -Qy, -Qn ignored\n"));
985 #endif
988 /* Set ppc_cpu if it is not already set. */
990 static void
991 ppc_set_cpu ()
993 const char *default_os = TARGET_OS;
994 const char *default_cpu = TARGET_CPU;
996 if (ppc_cpu == 0)
998 if (strncmp (default_os, "aix", 3) == 0
999 && default_os[3] >= '4' && default_os[3] <= '9')
1000 ppc_cpu = PPC_OPCODE_COMMON;
1001 else if (strncmp (default_os, "aix3", 4) == 0)
1002 ppc_cpu = PPC_OPCODE_POWER;
1003 else if (strcmp (default_cpu, "rs6000") == 0)
1004 ppc_cpu = PPC_OPCODE_POWER;
1005 else if (strcmp (default_cpu, "powerpc") == 0
1006 || strcmp (default_cpu, "powerpcle") == 0)
1007 ppc_cpu = PPC_OPCODE_PPC;
1008 else
1009 as_fatal (_("Unknown default cpu = %s, os = %s"), default_cpu, default_os);
1013 /* Figure out the BFD architecture to use. */
1015 enum bfd_architecture
1016 ppc_arch ()
1018 const char *default_cpu = TARGET_CPU;
1019 ppc_set_cpu ();
1021 if ((ppc_cpu & PPC_OPCODE_PPC) != 0)
1022 return bfd_arch_powerpc;
1023 else if ((ppc_cpu & PPC_OPCODE_POWER) != 0)
1024 return bfd_arch_rs6000;
1025 else if ((ppc_cpu & (PPC_OPCODE_COMMON | PPC_OPCODE_ANY)) != 0)
1027 if (strcmp (default_cpu, "rs6000") == 0)
1028 return bfd_arch_rs6000;
1029 else if (strcmp (default_cpu, "powerpc") == 0
1030 || strcmp (default_cpu, "powerpcle") == 0)
1031 return bfd_arch_powerpc;
1034 as_fatal (_("Neither Power nor PowerPC opcodes were selected."));
1035 return bfd_arch_unknown;
1038 unsigned long
1039 ppc_mach ()
1041 return (ppc_size == PPC_OPCODE_64) ? 620 : 0;
1045 ppc_subseg_align()
1047 return (ppc_xcoff64) ? 3 : 2;
1050 extern char*
1051 ppc_target_format()
1053 #ifdef OBJ_COFF
1054 #ifdef TE_PE
1055 return (target_big_endian ? "pe-powerpc" : "pe-powerpcle");
1056 #elif TE_POWERMAC
1057 #else
1058 return (ppc_xcoff64 ? "aixcoff64-rs6000" : "aixcoff-rs6000");
1059 #endif
1060 #ifdef TE_POWERMAC
1061 return "xcoff-powermac";
1062 #endif
1063 #endif
1064 #ifdef OBJ_ELF
1065 return (target_big_endian ? "elf32-powerpc" : "elf32-powerpcle");
1066 #endif
1069 /* This function is called when the assembler starts up. It is called
1070 after the options have been parsed and the output file has been
1071 opened. */
1073 void
1074 md_begin ()
1076 register const struct powerpc_opcode *op;
1077 const struct powerpc_opcode *op_end;
1078 const struct powerpc_macro *macro;
1079 const struct powerpc_macro *macro_end;
1080 boolean dup_insn = false;
1082 ppc_set_cpu ();
1084 #ifdef OBJ_ELF
1085 /* Set the ELF flags if desired. */
1086 if (ppc_flags && !msolaris)
1087 bfd_set_private_flags (stdoutput, ppc_flags);
1088 #endif
1090 /* Insert the opcodes into a hash table. */
1091 ppc_hash = hash_new ();
1093 op_end = powerpc_opcodes + powerpc_num_opcodes;
1094 for (op = powerpc_opcodes; op < op_end; op++)
1096 know ((op->opcode & op->mask) == op->opcode);
1098 if ((op->flags & ppc_cpu) != 0
1099 && ((op->flags & (PPC_OPCODE_32 | PPC_OPCODE_64)) == 0
1100 || (op->flags & (PPC_OPCODE_32 | PPC_OPCODE_64)) == ppc_size
1101 || (ppc_cpu & PPC_OPCODE_64_BRIDGE) != 0))
1103 const char *retval;
1105 retval = hash_insert (ppc_hash, op->name, (PTR) op);
1106 if (retval != (const char *) NULL)
1108 /* Ignore Power duplicates for -m601 */
1109 if ((ppc_cpu & PPC_OPCODE_601) != 0
1110 && (op->flags & PPC_OPCODE_POWER) != 0)
1111 continue;
1113 as_bad (_("Internal assembler error for instruction %s"), op->name);
1114 dup_insn = true;
1119 /* Insert the macros into a hash table. */
1120 ppc_macro_hash = hash_new ();
1122 macro_end = powerpc_macros + powerpc_num_macros;
1123 for (macro = powerpc_macros; macro < macro_end; macro++)
1125 if ((macro->flags & ppc_cpu) != 0)
1127 const char *retval;
1129 retval = hash_insert (ppc_macro_hash, macro->name, (PTR) macro);
1130 if (retval != (const char *) NULL)
1132 as_bad (_("Internal assembler error for macro %s"), macro->name);
1133 dup_insn = true;
1138 if (dup_insn)
1139 abort ();
1141 /* Tell the main code what the endianness is if it is not overidden by the user. */
1142 if (!set_target_endian)
1144 set_target_endian = 1;
1145 target_big_endian = PPC_BIG_ENDIAN;
1148 #ifdef OBJ_XCOFF
1149 ppc_coff_debug_section = coff_section_from_bfd_index (stdoutput, N_DEBUG);
1151 /* Create dummy symbols to serve as initial csects. This forces the
1152 text csects to precede the data csects. These symbols will not
1153 be output. */
1154 ppc_text_csects = symbol_make ("dummy\001");
1155 symbol_get_tc (ppc_text_csects)->within = ppc_text_csects;
1156 ppc_data_csects = symbol_make ("dummy\001");
1157 symbol_get_tc (ppc_data_csects)->within = ppc_data_csects;
1158 #endif
1160 #ifdef TE_PE
1162 ppc_current_section = text_section;
1163 ppc_previous_section = 0;
1165 #endif
1168 /* Insert an operand value into an instruction. */
1170 static unsigned long
1171 ppc_insert_operand (insn, operand, val, file, line)
1172 unsigned long insn;
1173 const struct powerpc_operand *operand;
1174 offsetT val;
1175 char *file;
1176 unsigned int line;
1178 if (operand->bits != 32)
1180 long min, max;
1181 offsetT test;
1183 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
1185 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0)
1186 max = (1 << operand->bits) - 1;
1187 else
1188 max = (1 << (operand->bits - 1)) - 1;
1189 min = - (1 << (operand->bits - 1));
1191 if (ppc_size == PPC_OPCODE_32)
1193 /* Some people write 32 bit hex constants with the sign
1194 extension done by hand. This shouldn't really be
1195 valid, but, to permit this code to assemble on a 64
1196 bit host, we sign extend the 32 bit value. */
1197 if (val > 0
1198 && (val & (offsetT) 0x80000000) != 0
1199 && (val & (offsetT) 0xffffffff) == val)
1201 val -= 0x80000000;
1202 val -= 0x80000000;
1206 else
1208 max = (1 << operand->bits) - 1;
1209 min = 0;
1212 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
1213 test = - val;
1214 else
1215 test = val;
1217 if (test < (offsetT) min || test > (offsetT) max)
1219 const char *err =
1220 _("operand out of range (%s not between %ld and %ld)");
1221 char buf[100];
1223 sprint_value (buf, test);
1224 if (file == (char *) NULL)
1225 as_bad (err, buf, min, max);
1226 else
1227 as_bad_where (file, line, err, buf, min, max);
1231 if (operand->insert)
1233 const char *errmsg;
1235 errmsg = NULL;
1236 insn = (*operand->insert) (insn, (long) val, &errmsg);
1237 if (errmsg != (const char *) NULL)
1238 as_bad (errmsg);
1240 else
1241 insn |= (((long) val & ((1 << operand->bits) - 1))
1242 << operand->shift);
1244 return insn;
1248 #ifdef OBJ_ELF
1249 /* Parse @got, etc. and return the desired relocation. */
1250 static bfd_reloc_code_real_type
1251 ppc_elf_suffix (str_p, exp_p)
1252 char **str_p;
1253 expressionS *exp_p;
1255 struct map_bfd {
1256 char *string;
1257 int length;
1258 bfd_reloc_code_real_type reloc;
1261 char ident[20];
1262 char *str = *str_p;
1263 char *str2;
1264 int ch;
1265 int len;
1266 struct map_bfd *ptr;
1268 #define MAP(str,reloc) { str, sizeof(str)-1, reloc }
1270 static struct map_bfd mapping[] = {
1271 MAP ("l", BFD_RELOC_LO16),
1272 MAP ("h", BFD_RELOC_HI16),
1273 MAP ("ha", BFD_RELOC_HI16_S),
1274 MAP ("brtaken", BFD_RELOC_PPC_B16_BRTAKEN),
1275 MAP ("brntaken", BFD_RELOC_PPC_B16_BRNTAKEN),
1276 MAP ("got", BFD_RELOC_16_GOTOFF),
1277 MAP ("got@l", BFD_RELOC_LO16_GOTOFF),
1278 MAP ("got@h", BFD_RELOC_HI16_GOTOFF),
1279 MAP ("got@ha", BFD_RELOC_HI16_S_GOTOFF),
1280 MAP ("fixup", BFD_RELOC_CTOR), /* warnings with -mrelocatable */
1281 MAP ("plt", BFD_RELOC_24_PLT_PCREL),
1282 MAP ("pltrel24", BFD_RELOC_24_PLT_PCREL),
1283 MAP ("copy", BFD_RELOC_PPC_COPY),
1284 MAP ("globdat", BFD_RELOC_PPC_GLOB_DAT),
1285 MAP ("local24pc", BFD_RELOC_PPC_LOCAL24PC),
1286 MAP ("local", BFD_RELOC_PPC_LOCAL24PC),
1287 MAP ("pltrel", BFD_RELOC_32_PLT_PCREL),
1288 MAP ("plt@l", BFD_RELOC_LO16_PLTOFF),
1289 MAP ("plt@h", BFD_RELOC_HI16_PLTOFF),
1290 MAP ("plt@ha", BFD_RELOC_HI16_S_PLTOFF),
1291 MAP ("sdarel", BFD_RELOC_GPREL16),
1292 MAP ("sectoff", BFD_RELOC_32_BASEREL),
1293 MAP ("sectoff@l", BFD_RELOC_LO16_BASEREL),
1294 MAP ("sectoff@h", BFD_RELOC_HI16_BASEREL),
1295 MAP ("sectoff@ha", BFD_RELOC_HI16_S_BASEREL),
1296 MAP ("naddr", BFD_RELOC_PPC_EMB_NADDR32),
1297 MAP ("naddr16", BFD_RELOC_PPC_EMB_NADDR16),
1298 MAP ("naddr@l", BFD_RELOC_PPC_EMB_NADDR16_LO),
1299 MAP ("naddr@h", BFD_RELOC_PPC_EMB_NADDR16_HI),
1300 MAP ("naddr@ha", BFD_RELOC_PPC_EMB_NADDR16_HA),
1301 MAP ("sdai16", BFD_RELOC_PPC_EMB_SDAI16),
1302 MAP ("sda2rel", BFD_RELOC_PPC_EMB_SDA2REL),
1303 MAP ("sda2i16", BFD_RELOC_PPC_EMB_SDA2I16),
1304 MAP ("sda21", BFD_RELOC_PPC_EMB_SDA21),
1305 MAP ("mrkref", BFD_RELOC_PPC_EMB_MRKREF),
1306 MAP ("relsect", BFD_RELOC_PPC_EMB_RELSEC16),
1307 MAP ("relsect@l", BFD_RELOC_PPC_EMB_RELST_LO),
1308 MAP ("relsect@h", BFD_RELOC_PPC_EMB_RELST_HI),
1309 MAP ("relsect@ha", BFD_RELOC_PPC_EMB_RELST_HA),
1310 MAP ("bitfld", BFD_RELOC_PPC_EMB_BIT_FLD),
1311 MAP ("relsda", BFD_RELOC_PPC_EMB_RELSDA),
1312 MAP ("xgot", BFD_RELOC_PPC_TOC16),
1314 { (char *)0, 0, BFD_RELOC_UNUSED }
1317 if (*str++ != '@')
1318 return BFD_RELOC_UNUSED;
1320 for (ch = *str, str2 = ident;
1321 (str2 < ident + sizeof (ident) - 1
1322 && (isalnum (ch) || ch == '@'));
1323 ch = *++str)
1325 *str2++ = (islower (ch)) ? ch : tolower (ch);
1328 *str2 = '\0';
1329 len = str2 - ident;
1331 ch = ident[0];
1332 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
1333 if (ch == ptr->string[0]
1334 && len == ptr->length
1335 && memcmp (ident, ptr->string, ptr->length) == 0)
1337 if (exp_p->X_add_number != 0
1338 && (ptr->reloc == BFD_RELOC_16_GOTOFF
1339 || ptr->reloc == BFD_RELOC_LO16_GOTOFF
1340 || ptr->reloc == BFD_RELOC_HI16_GOTOFF
1341 || ptr->reloc == BFD_RELOC_HI16_S_GOTOFF))
1342 as_warn (_("identifier+constant@got means identifier@got+constant"));
1344 /* Now check for identifier@suffix+constant */
1345 if (*str == '-' || *str == '+')
1347 char *orig_line = input_line_pointer;
1348 expressionS new_exp;
1350 input_line_pointer = str;
1351 expression (&new_exp);
1352 if (new_exp.X_op == O_constant)
1354 exp_p->X_add_number += new_exp.X_add_number;
1355 str = input_line_pointer;
1358 if (&input_line_pointer != str_p)
1359 input_line_pointer = orig_line;
1362 *str_p = str;
1363 return ptr->reloc;
1366 return BFD_RELOC_UNUSED;
1369 /* Like normal .long/.short/.word, except support @got, etc. */
1370 /* clobbers input_line_pointer, checks */
1371 /* end-of-line. */
1372 static void
1373 ppc_elf_cons (nbytes)
1374 register int nbytes; /* 1=.byte, 2=.word, 4=.long */
1376 expressionS exp;
1377 bfd_reloc_code_real_type reloc;
1379 if (is_it_end_of_statement ())
1381 demand_empty_rest_of_line ();
1382 return;
1387 expression (&exp);
1388 if (exp.X_op == O_symbol
1389 && *input_line_pointer == '@'
1390 && (reloc = ppc_elf_suffix (&input_line_pointer, &exp)) != BFD_RELOC_UNUSED)
1392 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
1393 int size = bfd_get_reloc_size (reloc_howto);
1395 if (size > nbytes)
1396 as_bad (_("%s relocations do not fit in %d bytes\n"), reloc_howto->name, nbytes);
1398 else
1400 register char *p = frag_more ((int) nbytes);
1401 int offset = nbytes - size;
1403 fix_new_exp (frag_now, p - frag_now->fr_literal + offset, size, &exp, 0, reloc);
1406 else
1407 emit_expr (&exp, (unsigned int) nbytes);
1409 while (*input_line_pointer++ == ',');
1411 input_line_pointer--; /* Put terminator back into stream. */
1412 demand_empty_rest_of_line ();
1415 /* Solaris pseduo op to change to the .rodata section. */
1416 static void
1417 ppc_elf_rdata (xxx)
1418 int xxx;
1420 char *save_line = input_line_pointer;
1421 static char section[] = ".rodata\n";
1423 /* Just pretend this is .section .rodata */
1424 input_line_pointer = section;
1425 obj_elf_section (xxx);
1427 input_line_pointer = save_line;
1430 /* Pseudo op to make file scope bss items */
1431 static void
1432 ppc_elf_lcomm(xxx)
1433 int xxx ATTRIBUTE_UNUSED;
1435 register char *name;
1436 register char c;
1437 register char *p;
1438 offsetT size;
1439 register symbolS *symbolP;
1440 offsetT align;
1441 segT old_sec;
1442 int old_subsec;
1443 char *pfrag;
1444 int align2;
1446 name = input_line_pointer;
1447 c = get_symbol_end ();
1449 /* just after name is now '\0' */
1450 p = input_line_pointer;
1451 *p = c;
1452 SKIP_WHITESPACE ();
1453 if (*input_line_pointer != ',')
1455 as_bad (_("Expected comma after symbol-name: rest of line ignored."));
1456 ignore_rest_of_line ();
1457 return;
1460 input_line_pointer++; /* skip ',' */
1461 if ((size = get_absolute_expression ()) < 0)
1463 as_warn (_(".COMMon length (%ld.) <0! Ignored."), (long) size);
1464 ignore_rest_of_line ();
1465 return;
1468 /* The third argument to .lcomm is the alignment. */
1469 if (*input_line_pointer != ',')
1470 align = 8;
1471 else
1473 ++input_line_pointer;
1474 align = get_absolute_expression ();
1475 if (align <= 0)
1477 as_warn (_("ignoring bad alignment"));
1478 align = 8;
1482 *p = 0;
1483 symbolP = symbol_find_or_make (name);
1484 *p = c;
1486 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
1488 as_bad (_("Ignoring attempt to re-define symbol `%s'."),
1489 S_GET_NAME (symbolP));
1490 ignore_rest_of_line ();
1491 return;
1494 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
1496 as_bad (_("Length of .lcomm \"%s\" is already %ld. Not changed to %ld."),
1497 S_GET_NAME (symbolP),
1498 (long) S_GET_VALUE (symbolP),
1499 (long) size);
1501 ignore_rest_of_line ();
1502 return;
1505 /* allocate_bss: */
1506 old_sec = now_seg;
1507 old_subsec = now_subseg;
1508 if (align)
1510 /* convert to a power of 2 alignment */
1511 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2);
1512 if (align != 1)
1514 as_bad (_("Common alignment not a power of 2"));
1515 ignore_rest_of_line ();
1516 return;
1519 else
1520 align2 = 0;
1522 record_alignment (bss_section, align2);
1523 subseg_set (bss_section, 0);
1524 if (align2)
1525 frag_align (align2, 0, 0);
1526 if (S_GET_SEGMENT (symbolP) == bss_section)
1527 symbol_get_frag (symbolP)->fr_symbol = 0;
1528 symbol_set_frag (symbolP, frag_now);
1529 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
1530 (char *) 0);
1531 *pfrag = 0;
1532 S_SET_SIZE (symbolP, size);
1533 S_SET_SEGMENT (symbolP, bss_section);
1534 subseg_set (old_sec, old_subsec);
1535 demand_empty_rest_of_line ();
1538 /* Validate any relocations emitted for -mrelocatable, possibly adding
1539 fixups for word relocations in writable segments, so we can adjust
1540 them at runtime. */
1541 static void
1542 ppc_elf_validate_fix (fixp, seg)
1543 fixS *fixp;
1544 segT seg;
1546 if (fixp->fx_done || fixp->fx_pcrel)
1547 return;
1549 switch (shlib)
1551 case SHLIB_NONE:
1552 case SHLIB_PIC:
1553 return;
1555 case SHILB_MRELOCATABLE:
1556 if (fixp->fx_r_type <= BFD_RELOC_UNUSED
1557 && fixp->fx_r_type != BFD_RELOC_16_GOTOFF
1558 && fixp->fx_r_type != BFD_RELOC_HI16_GOTOFF
1559 && fixp->fx_r_type != BFD_RELOC_LO16_GOTOFF
1560 && fixp->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
1561 && fixp->fx_r_type != BFD_RELOC_32_BASEREL
1562 && fixp->fx_r_type != BFD_RELOC_LO16_BASEREL
1563 && fixp->fx_r_type != BFD_RELOC_HI16_BASEREL
1564 && fixp->fx_r_type != BFD_RELOC_HI16_S_BASEREL
1565 && strcmp (segment_name (seg), ".got2") != 0
1566 && strcmp (segment_name (seg), ".dtors") != 0
1567 && strcmp (segment_name (seg), ".ctors") != 0
1568 && strcmp (segment_name (seg), ".fixup") != 0
1569 && strcmp (segment_name (seg), ".stab") != 0
1570 && strcmp (segment_name (seg), ".gcc_except_table") != 0
1571 && strcmp (segment_name (seg), ".eh_frame") != 0
1572 && strcmp (segment_name (seg), ".ex_shared") != 0)
1574 if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
1575 || fixp->fx_r_type != BFD_RELOC_CTOR)
1577 as_bad_where (fixp->fx_file, fixp->fx_line,
1578 _("Relocation cannot be done when using -mrelocatable"));
1581 return;
1584 #endif /* OBJ_ELF */
1586 #ifdef TE_PE
1589 * Summary of parse_toc_entry().
1591 * in: Input_line_pointer points to the '[' in one of:
1593 * [toc] [tocv] [toc32] [toc64]
1595 * Anything else is an error of one kind or another.
1597 * out:
1598 * return value: success or failure
1599 * toc_kind: kind of toc reference
1600 * input_line_pointer:
1601 * success: first char after the ']'
1602 * failure: unchanged
1604 * settings:
1606 * [toc] - rv == success, toc_kind = default_toc
1607 * [tocv] - rv == success, toc_kind = data_in_toc
1608 * [toc32] - rv == success, toc_kind = must_be_32
1609 * [toc64] - rv == success, toc_kind = must_be_64
1613 enum toc_size_qualifier
1615 default_toc, /* The toc cell constructed should be the system default size */
1616 data_in_toc, /* This is a direct reference to a toc cell */
1617 must_be_32, /* The toc cell constructed must be 32 bits wide */
1618 must_be_64 /* The toc cell constructed must be 64 bits wide */
1621 static int
1622 parse_toc_entry(toc_kind)
1623 enum toc_size_qualifier *toc_kind;
1625 char *start;
1626 char *toc_spec;
1627 char c;
1628 enum toc_size_qualifier t;
1630 /* save the input_line_pointer */
1631 start = input_line_pointer;
1633 /* skip over the '[' , and whitespace */
1634 ++input_line_pointer;
1635 SKIP_WHITESPACE ();
1637 /* find the spelling of the operand */
1638 toc_spec = input_line_pointer;
1639 c = get_symbol_end ();
1641 if (strcmp(toc_spec, "toc") == 0)
1643 t = default_toc;
1645 else if (strcmp(toc_spec, "tocv") == 0)
1647 t = data_in_toc;
1649 else if (strcmp(toc_spec, "toc32") == 0)
1651 t = must_be_32;
1653 else if (strcmp(toc_spec, "toc64") == 0)
1655 t = must_be_64;
1657 else
1659 as_bad (_("syntax error: invalid toc specifier `%s'"), toc_spec);
1660 *input_line_pointer = c; /* put back the delimiting char */
1661 input_line_pointer = start; /* reset input_line pointer */
1662 return 0;
1665 /* now find the ']' */
1666 *input_line_pointer = c; /* put back the delimiting char */
1668 SKIP_WHITESPACE (); /* leading whitespace could be there. */
1669 c = *input_line_pointer++; /* input_line_pointer->past char in c. */
1671 if (c != ']')
1673 as_bad (_("syntax error: expected `]', found `%c'"), c);
1674 input_line_pointer = start; /* reset input_line pointer */
1675 return 0;
1678 *toc_kind = t; /* set return value */
1679 return 1;
1681 #endif
1684 /* We need to keep a list of fixups. We can't simply generate them as
1685 we go, because that would require us to first create the frag, and
1686 that would screw up references to ``.''. */
1688 struct ppc_fixup
1690 expressionS exp;
1691 int opindex;
1692 bfd_reloc_code_real_type reloc;
1695 #define MAX_INSN_FIXUPS (5)
1697 /* This routine is called for each instruction to be assembled. */
1699 void
1700 md_assemble (str)
1701 char *str;
1703 char *s;
1704 const struct powerpc_opcode *opcode;
1705 unsigned long insn;
1706 const unsigned char *opindex_ptr;
1707 int skip_optional;
1708 int need_paren;
1709 int next_opindex;
1710 struct ppc_fixup fixups[MAX_INSN_FIXUPS];
1711 int fc;
1712 char *f;
1713 int i;
1714 #ifdef OBJ_ELF
1715 bfd_reloc_code_real_type reloc;
1716 #endif
1718 /* Get the opcode. */
1719 for (s = str; *s != '\0' && ! isspace (*s); s++)
1721 if (*s != '\0')
1722 *s++ = '\0';
1724 /* Look up the opcode in the hash table. */
1725 opcode = (const struct powerpc_opcode *) hash_find (ppc_hash, str);
1726 if (opcode == (const struct powerpc_opcode *) NULL)
1728 const struct powerpc_macro *macro;
1730 macro = (const struct powerpc_macro *) hash_find (ppc_macro_hash, str);
1731 if (macro == (const struct powerpc_macro *) NULL)
1732 as_bad (_("Unrecognized opcode: `%s'"), str);
1733 else
1734 ppc_macro (s, macro);
1736 return;
1739 insn = opcode->opcode;
1741 str = s;
1742 while (isspace (*str))
1743 ++str;
1745 /* PowerPC operands are just expressions. The only real issue is
1746 that a few operand types are optional. All cases which might use
1747 an optional operand separate the operands only with commas (in
1748 some cases parentheses are used, as in ``lwz 1,0(1)'' but such
1749 cases never have optional operands). There is never more than
1750 one optional operand for an instruction. So, before we start
1751 seriously parsing the operands, we check to see if we have an
1752 optional operand, and, if we do, we count the number of commas to
1753 see whether the operand should be omitted. */
1754 skip_optional = 0;
1755 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1757 const struct powerpc_operand *operand;
1759 operand = &powerpc_operands[*opindex_ptr];
1760 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
1762 unsigned int opcount;
1764 /* There is an optional operand. Count the number of
1765 commas in the input line. */
1766 if (*str == '\0')
1767 opcount = 0;
1768 else
1770 opcount = 1;
1771 s = str;
1772 while ((s = strchr (s, ',')) != (char *) NULL)
1774 ++opcount;
1775 ++s;
1779 /* If there are fewer operands in the line then are called
1780 for by the instruction, we want to skip the optional
1781 operand. */
1782 if (opcount < strlen (opcode->operands))
1783 skip_optional = 1;
1785 break;
1789 /* Gather the operands. */
1790 need_paren = 0;
1791 next_opindex = 0;
1792 fc = 0;
1793 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1795 const struct powerpc_operand *operand;
1796 const char *errmsg;
1797 char *hold;
1798 expressionS ex;
1799 char endc;
1801 if (next_opindex == 0)
1802 operand = &powerpc_operands[*opindex_ptr];
1803 else
1805 operand = &powerpc_operands[next_opindex];
1806 next_opindex = 0;
1809 errmsg = NULL;
1811 /* If this is a fake operand, then we do not expect anything
1812 from the input. */
1813 if ((operand->flags & PPC_OPERAND_FAKE) != 0)
1815 insn = (*operand->insert) (insn, 0L, &errmsg);
1816 if (errmsg != (const char *) NULL)
1817 as_bad (errmsg);
1818 continue;
1821 /* If this is an optional operand, and we are skipping it, just
1822 insert a zero. */
1823 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
1824 && skip_optional)
1826 if (operand->insert)
1828 insn = (*operand->insert) (insn, 0L, &errmsg);
1829 if (errmsg != (const char *) NULL)
1830 as_bad (errmsg);
1832 if ((operand->flags & PPC_OPERAND_NEXT) != 0)
1833 next_opindex = *opindex_ptr + 1;
1834 continue;
1837 /* Gather the operand. */
1838 hold = input_line_pointer;
1839 input_line_pointer = str;
1841 #ifdef TE_PE
1842 if (*input_line_pointer == '[')
1844 /* We are expecting something like the second argument here:
1846 lwz r4,[toc].GS.0.static_int(rtoc)
1847 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1848 The argument following the `]' must be a symbol name, and the
1849 register must be the toc register: 'rtoc' or '2'
1851 The effect is to 0 as the displacement field
1852 in the instruction, and issue an IMAGE_REL_PPC_TOCREL16 (or
1853 the appropriate variation) reloc against it based on the symbol.
1854 The linker will build the toc, and insert the resolved toc offset.
1856 Note:
1857 o The size of the toc entry is currently assumed to be
1858 32 bits. This should not be assumed to be a hard coded
1859 number.
1860 o In an effort to cope with a change from 32 to 64 bits,
1861 there are also toc entries that are specified to be
1862 either 32 or 64 bits:
1863 lwz r4,[toc32].GS.0.static_int(rtoc)
1864 lwz r4,[toc64].GS.0.static_int(rtoc)
1865 These demand toc entries of the specified size, and the
1866 instruction probably requires it.
1869 int valid_toc;
1870 enum toc_size_qualifier toc_kind;
1871 bfd_reloc_code_real_type toc_reloc;
1873 /* go parse off the [tocXX] part */
1874 valid_toc = parse_toc_entry(&toc_kind);
1876 if (!valid_toc)
1878 /* Note: message has already been issued. */
1879 /* FIXME: what sort of recovery should we do? */
1880 /* demand_rest_of_line(); return; ? */
1883 /* Now get the symbol following the ']' */
1884 expression(&ex);
1886 switch (toc_kind)
1888 case default_toc:
1889 /* In this case, we may not have seen the symbol yet, since */
1890 /* it is allowed to appear on a .extern or .globl or just be */
1891 /* a label in the .data section. */
1892 toc_reloc = BFD_RELOC_PPC_TOC16;
1893 break;
1894 case data_in_toc:
1895 /* 1. The symbol must be defined and either in the toc */
1896 /* section, or a global. */
1897 /* 2. The reloc generated must have the TOCDEFN flag set in */
1898 /* upper bit mess of the reloc type. */
1899 /* FIXME: It's a little confusing what the tocv qualifier can */
1900 /* be used for. At the very least, I've seen three */
1901 /* uses, only one of which I'm sure I can explain. */
1902 if (ex.X_op == O_symbol)
1904 assert (ex.X_add_symbol != NULL);
1905 if (symbol_get_bfdsym (ex.X_add_symbol)->section
1906 != tocdata_section)
1908 as_bad(_("[tocv] symbol is not a toc symbol"));
1912 toc_reloc = BFD_RELOC_PPC_TOC16;
1913 break;
1914 case must_be_32:
1915 /* FIXME: these next two specifically specify 32/64 bit toc */
1916 /* entries. We don't support them today. Is this the */
1917 /* right way to say that? */
1918 toc_reloc = BFD_RELOC_UNUSED;
1919 as_bad (_("Unimplemented toc32 expression modifier"));
1920 break;
1921 case must_be_64:
1922 /* FIXME: see above */
1923 toc_reloc = BFD_RELOC_UNUSED;
1924 as_bad (_("Unimplemented toc64 expression modifier"));
1925 break;
1926 default:
1927 fprintf(stderr,
1928 _("Unexpected return value [%d] from parse_toc_entry!\n"),
1929 toc_kind);
1930 abort();
1931 break;
1934 /* We need to generate a fixup for this expression. */
1935 if (fc >= MAX_INSN_FIXUPS)
1936 as_fatal (_("too many fixups"));
1938 fixups[fc].reloc = toc_reloc;
1939 fixups[fc].exp = ex;
1940 fixups[fc].opindex = *opindex_ptr;
1941 ++fc;
1943 /* Ok. We've set up the fixup for the instruction. Now make it
1944 look like the constant 0 was found here */
1945 ex.X_unsigned = 1;
1946 ex.X_op = O_constant;
1947 ex.X_add_number = 0;
1948 ex.X_add_symbol = NULL;
1949 ex.X_op_symbol = NULL;
1952 else
1953 #endif /* TE_PE */
1955 if (! register_name (&ex))
1957 if ((operand->flags & PPC_OPERAND_CR) != 0)
1958 cr_operand = true;
1959 expression (&ex);
1960 cr_operand = false;
1964 str = input_line_pointer;
1965 input_line_pointer = hold;
1967 if (ex.X_op == O_illegal)
1968 as_bad (_("illegal operand"));
1969 else if (ex.X_op == O_absent)
1970 as_bad (_("missing operand"));
1971 else if (ex.X_op == O_register)
1973 insn = ppc_insert_operand (insn, operand, ex.X_add_number,
1974 (char *) NULL, 0);
1976 else if (ex.X_op == O_constant)
1978 #ifdef OBJ_ELF
1979 /* Allow @HA, @L, @H on constants. */
1980 char *orig_str = str;
1982 if ((reloc = ppc_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
1983 switch (reloc)
1985 default:
1986 str = orig_str;
1987 break;
1989 case BFD_RELOC_LO16:
1990 /* X_unsigned is the default, so if the user has done
1991 something which cleared it, we always produce a
1992 signed value. */
1993 if (ex.X_unsigned
1994 && (operand->flags & PPC_OPERAND_SIGNED) == 0)
1995 ex.X_add_number &= 0xffff;
1996 else
1997 ex.X_add_number = (((ex.X_add_number & 0xffff)
1998 ^ 0x8000)
1999 - 0x8000);
2000 break;
2002 case BFD_RELOC_HI16:
2003 ex.X_add_number = (ex.X_add_number >> 16) & 0xffff;
2004 break;
2006 case BFD_RELOC_HI16_S:
2007 ex.X_add_number = ((((ex.X_add_number >> 16) & 0xffff)
2008 + ((ex.X_add_number >> 15) & 1))
2009 & 0xffff);
2010 break;
2012 #endif
2013 insn = ppc_insert_operand (insn, operand, ex.X_add_number,
2014 (char *) NULL, 0);
2016 #ifdef OBJ_ELF
2017 else if ((reloc = ppc_elf_suffix (&str, &ex)) != BFD_RELOC_UNUSED)
2019 /* For the absoulte forms of branchs, convert the PC relative form back into
2020 the absolute. */
2021 if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
2023 switch (reloc)
2025 case BFD_RELOC_PPC_B26:
2026 reloc = BFD_RELOC_PPC_BA26;
2027 break;
2028 case BFD_RELOC_PPC_B16:
2029 reloc = BFD_RELOC_PPC_BA16;
2030 break;
2031 case BFD_RELOC_PPC_B16_BRTAKEN:
2032 reloc = BFD_RELOC_PPC_BA16_BRTAKEN;
2033 break;
2034 case BFD_RELOC_PPC_B16_BRNTAKEN:
2035 reloc = BFD_RELOC_PPC_BA16_BRNTAKEN;
2036 break;
2037 default:
2038 break;
2042 /* We need to generate a fixup for this expression. */
2043 if (fc >= MAX_INSN_FIXUPS)
2044 as_fatal (_("too many fixups"));
2045 fixups[fc].exp = ex;
2046 fixups[fc].opindex = 0;
2047 fixups[fc].reloc = reloc;
2048 ++fc;
2050 #endif /* OBJ_ELF */
2052 else
2054 /* We need to generate a fixup for this expression. */
2055 if (fc >= MAX_INSN_FIXUPS)
2056 as_fatal (_("too many fixups"));
2057 fixups[fc].exp = ex;
2058 fixups[fc].opindex = *opindex_ptr;
2059 fixups[fc].reloc = BFD_RELOC_UNUSED;
2060 ++fc;
2063 if (need_paren)
2065 endc = ')';
2066 need_paren = 0;
2068 else if ((operand->flags & PPC_OPERAND_PARENS) != 0)
2070 endc = '(';
2071 need_paren = 1;
2073 else
2074 endc = ',';
2076 /* The call to expression should have advanced str past any
2077 whitespace. */
2078 if (*str != endc
2079 && (endc != ',' || *str != '\0'))
2081 as_bad (_("syntax error; found `%c' but expected `%c'"), *str, endc);
2082 break;
2085 if (*str != '\0')
2086 ++str;
2089 while (isspace (*str))
2090 ++str;
2092 if (*str != '\0')
2093 as_bad (_("junk at end of line: `%s'"), str);
2095 /* Write out the instruction. */
2096 f = frag_more (4);
2097 md_number_to_chars (f, insn, 4);
2099 /* Create any fixups. At this point we do not use a
2100 bfd_reloc_code_real_type, but instead just use the
2101 BFD_RELOC_UNUSED plus the operand index. This lets us easily
2102 handle fixups for any operand type, although that is admittedly
2103 not a very exciting feature. We pick a BFD reloc type in
2104 md_apply_fix. */
2105 for (i = 0; i < fc; i++)
2107 const struct powerpc_operand *operand;
2109 operand = &powerpc_operands[fixups[i].opindex];
2110 if (fixups[i].reloc != BFD_RELOC_UNUSED)
2112 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
2113 int size;
2114 int offset;
2115 fixS *fixP;
2117 if (!reloc_howto)
2118 abort ();
2120 size = bfd_get_reloc_size (reloc_howto);
2121 offset = target_big_endian ? (4 - size) : 0;
2123 if (size < 1 || size > 4)
2124 abort();
2126 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
2127 &fixups[i].exp, reloc_howto->pc_relative,
2128 fixups[i].reloc);
2130 /* Turn off complaints that the addend is too large for things like
2131 foo+100000@ha. */
2132 switch (fixups[i].reloc)
2134 case BFD_RELOC_16_GOTOFF:
2135 case BFD_RELOC_PPC_TOC16:
2136 case BFD_RELOC_LO16:
2137 case BFD_RELOC_HI16:
2138 case BFD_RELOC_HI16_S:
2139 fixP->fx_no_overflow = 1;
2140 break;
2141 default:
2142 break;
2145 else
2146 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2147 &fixups[i].exp,
2148 (operand->flags & PPC_OPERAND_RELATIVE) != 0,
2149 ((bfd_reloc_code_real_type)
2150 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
2154 /* Handle a macro. Gather all the operands, transform them as
2155 described by the macro, and call md_assemble recursively. All the
2156 operands are separated by commas; we don't accept parentheses
2157 around operands here. */
2159 static void
2160 ppc_macro (str, macro)
2161 char *str;
2162 const struct powerpc_macro *macro;
2164 char *operands[10];
2165 unsigned int count;
2166 char *s;
2167 unsigned int len;
2168 const char *format;
2169 int arg;
2170 char *send;
2171 char *complete;
2173 /* Gather the users operands into the operands array. */
2174 count = 0;
2175 s = str;
2176 while (1)
2178 if (count >= sizeof operands / sizeof operands[0])
2179 break;
2180 operands[count++] = s;
2181 s = strchr (s, ',');
2182 if (s == (char *) NULL)
2183 break;
2184 *s++ = '\0';
2187 if (count != macro->operands)
2189 as_bad (_("wrong number of operands"));
2190 return;
2193 /* Work out how large the string must be (the size is unbounded
2194 because it includes user input). */
2195 len = 0;
2196 format = macro->format;
2197 while (*format != '\0')
2199 if (*format != '%')
2201 ++len;
2202 ++format;
2204 else
2206 arg = strtol (format + 1, &send, 10);
2207 know (send != format && arg >= 0 && arg < count);
2208 len += strlen (operands[arg]);
2209 format = send;
2213 /* Put the string together. */
2214 complete = s = (char *) alloca (len + 1);
2215 format = macro->format;
2216 while (*format != '\0')
2218 if (*format != '%')
2219 *s++ = *format++;
2220 else
2222 arg = strtol (format + 1, &send, 10);
2223 strcpy (s, operands[arg]);
2224 s += strlen (s);
2225 format = send;
2228 *s = '\0';
2230 /* Assemble the constructed instruction. */
2231 md_assemble (complete);
2234 #ifdef OBJ_ELF
2235 /* For ELF, add support for SHF_EXCLUDE and SHT_ORDERED */
2238 ppc_section_letter (letter, ptr_msg)
2239 int letter;
2240 char **ptr_msg;
2242 if (letter == 'e')
2243 return SHF_EXCLUDE;
2245 *ptr_msg = _("Bad .section directive: want a,w,x,e in string");
2246 return 0;
2250 ppc_section_word (str, len)
2251 char *str;
2252 size_t len;
2254 if (len == 7 && strncmp (str, "exclude", 7) == 0)
2255 return SHF_EXCLUDE;
2257 return -1;
2261 ppc_section_type (str, len)
2262 char *str;
2263 size_t len;
2265 if (len == 7 && strncmp (str, "ordered", 7) == 0)
2266 return SHT_ORDERED;
2268 return -1;
2272 ppc_section_flags (flags, attr, type)
2273 int flags;
2274 int attr;
2275 int type;
2277 if (type == SHT_ORDERED)
2278 flags |= SEC_ALLOC | SEC_LOAD | SEC_SORT_ENTRIES;
2280 if (attr & SHF_EXCLUDE)
2281 flags |= SEC_EXCLUDE;
2283 return flags;
2285 #endif /* OBJ_ELF */
2288 /* Pseudo-op handling. */
2290 /* The .byte pseudo-op. This is similar to the normal .byte
2291 pseudo-op, but it can also take a single ASCII string. */
2293 static void
2294 ppc_byte (ignore)
2295 int ignore ATTRIBUTE_UNUSED;
2297 if (*input_line_pointer != '\"')
2299 cons (1);
2300 return;
2303 /* Gather characters. A real double quote is doubled. Unusual
2304 characters are not permitted. */
2305 ++input_line_pointer;
2306 while (1)
2308 char c;
2310 c = *input_line_pointer++;
2312 if (c == '\"')
2314 if (*input_line_pointer != '\"')
2315 break;
2316 ++input_line_pointer;
2319 FRAG_APPEND_1_CHAR (c);
2322 demand_empty_rest_of_line ();
2325 #ifdef OBJ_XCOFF
2327 /* XCOFF specific pseudo-op handling. */
2329 /* This is set if we are creating a .stabx symbol, since we don't want
2330 to handle symbol suffixes for such symbols. */
2331 static boolean ppc_stab_symbol;
2333 /* The .comm and .lcomm pseudo-ops for XCOFF. XCOFF puts common
2334 symbols in the .bss segment as though they were local common
2335 symbols, and uses a different smclas. */
2337 static void
2338 ppc_comm (lcomm)
2339 int lcomm;
2341 asection *current_seg = now_seg;
2342 subsegT current_subseg = now_subseg;
2343 char *name;
2344 char endc;
2345 char *end_name;
2346 offsetT size;
2347 offsetT align;
2348 symbolS *lcomm_sym = NULL;
2349 symbolS *sym;
2350 char *pfrag;
2352 name = input_line_pointer;
2353 endc = get_symbol_end ();
2354 end_name = input_line_pointer;
2355 *end_name = endc;
2357 if (*input_line_pointer != ',')
2359 as_bad (_("missing size"));
2360 ignore_rest_of_line ();
2361 return;
2363 ++input_line_pointer;
2365 size = get_absolute_expression ();
2366 if (size < 0)
2368 as_bad (_("negative size"));
2369 ignore_rest_of_line ();
2370 return;
2373 if (! lcomm)
2375 /* The third argument to .comm is the alignment. */
2376 if (*input_line_pointer != ',')
2377 align = 3;
2378 else
2380 ++input_line_pointer;
2381 align = get_absolute_expression ();
2382 if (align <= 0)
2384 as_warn (_("ignoring bad alignment"));
2385 align = 3;
2389 else
2391 char *lcomm_name;
2392 char lcomm_endc;
2394 if (size <= 1)
2395 align = 0;
2396 else if (size <= 2)
2397 align = 1;
2398 else if (size <= 4)
2399 align = 2;
2400 else
2401 align = 3;
2403 /* The third argument to .lcomm appears to be the real local
2404 common symbol to create. References to the symbol named in
2405 the first argument are turned into references to the third
2406 argument. */
2407 if (*input_line_pointer != ',')
2409 as_bad (_("missing real symbol name"));
2410 ignore_rest_of_line ();
2411 return;
2413 ++input_line_pointer;
2415 lcomm_name = input_line_pointer;
2416 lcomm_endc = get_symbol_end ();
2418 lcomm_sym = symbol_find_or_make (lcomm_name);
2420 *input_line_pointer = lcomm_endc;
2423 *end_name = '\0';
2424 sym = symbol_find_or_make (name);
2425 *end_name = endc;
2427 if (S_IS_DEFINED (sym)
2428 || S_GET_VALUE (sym) != 0)
2430 as_bad (_("attempt to redefine symbol"));
2431 ignore_rest_of_line ();
2432 return;
2435 record_alignment (bss_section, align);
2437 if (! lcomm
2438 || ! S_IS_DEFINED (lcomm_sym))
2440 symbolS *def_sym;
2441 offsetT def_size;
2443 if (! lcomm)
2445 def_sym = sym;
2446 def_size = size;
2447 S_SET_EXTERNAL (sym);
2449 else
2451 symbol_get_tc (lcomm_sym)->output = 1;
2452 def_sym = lcomm_sym;
2453 def_size = 0;
2456 subseg_set (bss_section, 1);
2457 frag_align (align, 0, 0);
2459 symbol_set_frag (def_sym, frag_now);
2460 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, def_sym,
2461 def_size, (char *) NULL);
2462 *pfrag = 0;
2463 S_SET_SEGMENT (def_sym, bss_section);
2464 symbol_get_tc (def_sym)->align = align;
2466 else if (lcomm)
2468 /* Align the size of lcomm_sym. */
2469 symbol_get_frag (lcomm_sym)->fr_offset =
2470 ((symbol_get_frag (lcomm_sym)->fr_offset + (1 << align) - 1)
2471 &~ ((1 << align) - 1));
2472 if (align > symbol_get_tc (lcomm_sym)->align)
2473 symbol_get_tc (lcomm_sym)->align = align;
2476 if (lcomm)
2478 /* Make sym an offset from lcomm_sym. */
2479 S_SET_SEGMENT (sym, bss_section);
2480 symbol_set_frag (sym, symbol_get_frag (lcomm_sym));
2481 S_SET_VALUE (sym, symbol_get_frag (lcomm_sym)->fr_offset);
2482 symbol_get_frag (lcomm_sym)->fr_offset += size;
2485 subseg_set (current_seg, current_subseg);
2487 demand_empty_rest_of_line ();
2490 /* The .csect pseudo-op. This switches us into a different
2491 subsegment. The first argument is a symbol whose value is the
2492 start of the .csect. In COFF, csect symbols get special aux
2493 entries defined by the x_csect field of union internal_auxent. The
2494 optional second argument is the alignment (the default is 2). */
2496 static void
2497 ppc_csect (ignore)
2498 int ignore;
2500 char *name;
2501 char endc;
2502 symbolS *sym;
2504 name = input_line_pointer;
2505 endc = get_symbol_end ();
2507 sym = symbol_find_or_make (name);
2509 *input_line_pointer = endc;
2511 if (S_GET_NAME (sym)[0] == '\0')
2513 /* An unnamed csect is assumed to be [PR]. */
2514 symbol_get_tc (sym)->class = XMC_PR;
2517 ppc_change_csect (sym);
2519 if (*input_line_pointer == ',')
2521 ++input_line_pointer;
2522 symbol_get_tc (sym)->align = get_absolute_expression ();
2525 demand_empty_rest_of_line ();
2528 /* Change to a different csect. */
2530 static void
2531 ppc_change_csect (sym)
2532 symbolS *sym;
2534 if (S_IS_DEFINED (sym))
2535 subseg_set (S_GET_SEGMENT (sym), symbol_get_tc (sym)->subseg);
2536 else
2538 symbolS **list_ptr;
2539 int after_toc;
2540 int hold_chunksize;
2541 symbolS *list;
2543 /* This is a new csect. We need to look at the symbol class to
2544 figure out whether it should go in the text section or the
2545 data section. */
2546 after_toc = 0;
2547 switch (symbol_get_tc (sym)->class)
2549 case XMC_PR:
2550 case XMC_RO:
2551 case XMC_DB:
2552 case XMC_GL:
2553 case XMC_XO:
2554 case XMC_SV:
2555 case XMC_TI:
2556 case XMC_TB:
2557 S_SET_SEGMENT (sym, text_section);
2558 symbol_get_tc (sym)->subseg = ppc_text_subsegment;
2559 ++ppc_text_subsegment;
2560 list_ptr = &ppc_text_csects;
2561 break;
2562 case XMC_RW:
2563 case XMC_TC0:
2564 case XMC_TC:
2565 case XMC_DS:
2566 case XMC_UA:
2567 case XMC_BS:
2568 case XMC_UC:
2569 if (ppc_toc_csect != NULL
2570 && (symbol_get_tc (ppc_toc_csect)->subseg + 1
2571 == ppc_data_subsegment))
2572 after_toc = 1;
2573 S_SET_SEGMENT (sym, data_section);
2574 symbol_get_tc (sym)->subseg = ppc_data_subsegment;
2575 ++ppc_data_subsegment;
2576 list_ptr = &ppc_data_csects;
2577 break;
2578 default:
2579 abort ();
2582 /* We set the obstack chunk size to a small value before
2583 changing subsegments, so that we don't use a lot of memory
2584 space for what may be a small section. */
2585 hold_chunksize = chunksize;
2586 chunksize = 64;
2588 subseg_new (segment_name (S_GET_SEGMENT (sym)),
2589 symbol_get_tc (sym)->subseg);
2591 chunksize = hold_chunksize;
2593 if (after_toc)
2594 ppc_after_toc_frag = frag_now;
2596 symbol_set_frag (sym, frag_now);
2597 S_SET_VALUE (sym, (valueT) frag_now_fix ());
2599 symbol_get_tc (sym)->align = (ppc_xcoff64) ? 3 : 2;
2600 symbol_get_tc (sym)->output = 1;
2601 symbol_get_tc (sym)->within = sym;
2603 for (list = *list_ptr;
2604 symbol_get_tc (list)->next != (symbolS *) NULL;
2605 list = symbol_get_tc (list)->next)
2607 symbol_get_tc (list)->next = sym;
2609 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2610 symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
2611 &symbol_lastP);
2614 ppc_current_csect = sym;
2617 /* This function handles the .text and .data pseudo-ops. These
2618 pseudo-ops aren't really used by XCOFF; we implement them for the
2619 convenience of people who aren't used to XCOFF. */
2621 static void
2622 ppc_section (type)
2623 int type;
2625 const char *name;
2626 symbolS *sym;
2628 if (type == 't')
2629 name = ".text[PR]";
2630 else if (type == 'd')
2631 name = ".data[RW]";
2632 else
2633 abort ();
2635 sym = symbol_find_or_make (name);
2637 ppc_change_csect (sym);
2639 demand_empty_rest_of_line ();
2642 /* This function handles the .section pseudo-op. This is mostly to
2643 give an error, since XCOFF only supports .text, .data and .bss, but
2644 we do permit the user to name the text or data section. */
2646 static void
2647 ppc_named_section (ignore)
2648 int ignore;
2650 char *user_name;
2651 const char *real_name;
2652 char c;
2653 symbolS *sym;
2655 user_name = input_line_pointer;
2656 c = get_symbol_end ();
2658 if (strcmp (user_name, ".text") == 0)
2659 real_name = ".text[PR]";
2660 else if (strcmp (user_name, ".data") == 0)
2661 real_name = ".data[RW]";
2662 else
2664 as_bad (_("The XCOFF file format does not support arbitrary sections"));
2665 *input_line_pointer = c;
2666 ignore_rest_of_line ();
2667 return;
2670 *input_line_pointer = c;
2672 sym = symbol_find_or_make (real_name);
2674 ppc_change_csect (sym);
2676 demand_empty_rest_of_line ();
2679 /* The .extern pseudo-op. We create an undefined symbol. */
2681 static void
2682 ppc_extern (ignore)
2683 int ignore;
2685 char *name;
2686 char endc;
2688 name = input_line_pointer;
2689 endc = get_symbol_end ();
2691 (void) symbol_find_or_make (name);
2693 *input_line_pointer = endc;
2695 demand_empty_rest_of_line ();
2698 /* The .lglobl pseudo-op. Keep the symbol in the symbol table. */
2700 static void
2701 ppc_lglobl (ignore)
2702 int ignore;
2704 char *name;
2705 char endc;
2706 symbolS *sym;
2708 name = input_line_pointer;
2709 endc = get_symbol_end ();
2711 sym = symbol_find_or_make (name);
2713 *input_line_pointer = endc;
2715 symbol_get_tc (sym)->output = 1;
2717 demand_empty_rest_of_line ();
2720 /* The .rename pseudo-op. The RS/6000 assembler can rename symbols,
2721 although I don't know why it bothers. */
2723 static void
2724 ppc_rename (ignore)
2725 int ignore;
2727 char *name;
2728 char endc;
2729 symbolS *sym;
2730 int len;
2732 name = input_line_pointer;
2733 endc = get_symbol_end ();
2735 sym = symbol_find_or_make (name);
2737 *input_line_pointer = endc;
2739 if (*input_line_pointer != ',')
2741 as_bad (_("missing rename string"));
2742 ignore_rest_of_line ();
2743 return;
2745 ++input_line_pointer;
2747 symbol_get_tc (sym)->real_name = demand_copy_C_string (&len);
2749 demand_empty_rest_of_line ();
2752 /* The .stabx pseudo-op. This is similar to a normal .stabs
2753 pseudo-op, but slightly different. A sample is
2754 .stabx "main:F-1",.main,142,0
2755 The first argument is the symbol name to create. The second is the
2756 value, and the third is the storage class. The fourth seems to be
2757 always zero, and I am assuming it is the type. */
2759 static void
2760 ppc_stabx (ignore)
2761 int ignore;
2763 char *name;
2764 int len;
2765 symbolS *sym;
2766 expressionS exp;
2768 name = demand_copy_C_string (&len);
2770 if (*input_line_pointer != ',')
2772 as_bad (_("missing value"));
2773 return;
2775 ++input_line_pointer;
2777 ppc_stab_symbol = true;
2778 sym = symbol_make (name);
2779 ppc_stab_symbol = false;
2781 symbol_get_tc (sym)->real_name = name;
2783 (void) expression (&exp);
2785 switch (exp.X_op)
2787 case O_illegal:
2788 case O_absent:
2789 case O_big:
2790 as_bad (_("illegal .stabx expression; zero assumed"));
2791 exp.X_add_number = 0;
2792 /* Fall through. */
2793 case O_constant:
2794 S_SET_VALUE (sym, (valueT) exp.X_add_number);
2795 symbol_set_frag (sym, &zero_address_frag);
2796 break;
2798 case O_symbol:
2799 if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section)
2800 symbol_set_value_expression (sym, &exp);
2801 else
2803 S_SET_VALUE (sym,
2804 exp.X_add_number + S_GET_VALUE (exp.X_add_symbol));
2805 symbol_set_frag (sym, symbol_get_frag (exp.X_add_symbol));
2807 break;
2809 default:
2810 /* The value is some complex expression. This will probably
2811 fail at some later point, but this is probably the right
2812 thing to do here. */
2813 symbol_set_value_expression (sym, &exp);
2814 break;
2817 S_SET_SEGMENT (sym, ppc_coff_debug_section);
2818 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
2820 if (*input_line_pointer != ',')
2822 as_bad (_("missing class"));
2823 return;
2825 ++input_line_pointer;
2827 S_SET_STORAGE_CLASS (sym, get_absolute_expression ());
2829 if (*input_line_pointer != ',')
2831 as_bad (_("missing type"));
2832 return;
2834 ++input_line_pointer;
2836 S_SET_DATA_TYPE (sym, get_absolute_expression ());
2838 symbol_get_tc (sym)->output = 1;
2840 if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
2841 symbol_get_tc (sym)->within = ppc_current_block;
2843 if (exp.X_op != O_symbol
2844 || ! S_IS_EXTERNAL (exp.X_add_symbol)
2845 || S_GET_SEGMENT (exp.X_add_symbol) != bss_section)
2846 ppc_frob_label (sym);
2847 else
2849 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2850 symbol_append (sym, exp.X_add_symbol, &symbol_rootP, &symbol_lastP);
2851 if (symbol_get_tc (ppc_current_csect)->within == exp.X_add_symbol)
2852 symbol_get_tc (ppc_current_csect)->within = sym;
2855 demand_empty_rest_of_line ();
2858 /* The .function pseudo-op. This takes several arguments. The first
2859 argument seems to be the external name of the symbol. The second
2860 argment seems to be the label for the start of the function. gcc
2861 uses the same name for both. I have no idea what the third and
2862 fourth arguments are meant to be. The optional fifth argument is
2863 an expression for the size of the function. In COFF this symbol
2864 gets an aux entry like that used for a csect. */
2866 static void
2867 ppc_function (ignore)
2868 int ignore;
2870 char *name;
2871 char endc;
2872 char *s;
2873 symbolS *ext_sym;
2874 symbolS *lab_sym;
2876 name = input_line_pointer;
2877 endc = get_symbol_end ();
2879 /* Ignore any [PR] suffix. */
2880 name = ppc_canonicalize_symbol_name (name);
2881 s = strchr (name, '[');
2882 if (s != (char *) NULL
2883 && strcmp (s + 1, "PR]") == 0)
2884 *s = '\0';
2886 ext_sym = symbol_find_or_make (name);
2888 *input_line_pointer = endc;
2890 if (*input_line_pointer != ',')
2892 as_bad (_("missing symbol name"));
2893 ignore_rest_of_line ();
2894 return;
2896 ++input_line_pointer;
2898 name = input_line_pointer;
2899 endc = get_symbol_end ();
2901 lab_sym = symbol_find_or_make (name);
2903 *input_line_pointer = endc;
2905 if (ext_sym != lab_sym)
2907 expressionS exp;
2909 exp.X_op = O_symbol;
2910 exp.X_add_symbol = lab_sym;
2911 exp.X_op_symbol = NULL;
2912 exp.X_add_number = 0;
2913 exp.X_unsigned = 0;
2914 symbol_set_value_expression (ext_sym, &exp);
2917 if (symbol_get_tc (ext_sym)->class == -1)
2918 symbol_get_tc (ext_sym)->class = XMC_PR;
2919 symbol_get_tc (ext_sym)->output = 1;
2921 if (*input_line_pointer == ',')
2923 expressionS ignore;
2925 /* Ignore the third argument. */
2926 ++input_line_pointer;
2927 expression (&ignore);
2928 if (*input_line_pointer == ',')
2930 /* Ignore the fourth argument. */
2931 ++input_line_pointer;
2932 expression (&ignore);
2933 if (*input_line_pointer == ',')
2935 /* The fifth argument is the function size. */
2936 ++input_line_pointer;
2937 symbol_get_tc (ext_sym)->size = symbol_new ("L0\001",
2938 absolute_section,
2939 (valueT) 0,
2940 &zero_address_frag);
2941 pseudo_set (symbol_get_tc (ext_sym)->size);
2946 S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
2947 SF_SET_FUNCTION (ext_sym);
2948 SF_SET_PROCESS (ext_sym);
2949 coff_add_linesym (ext_sym);
2951 demand_empty_rest_of_line ();
2954 /* The .bf pseudo-op. This is just like a COFF C_FCN symbol named
2955 ".bf". */
2957 static void
2958 ppc_bf (ignore)
2959 int ignore;
2961 symbolS *sym;
2963 sym = symbol_make (".bf");
2964 S_SET_SEGMENT (sym, text_section);
2965 symbol_set_frag (sym, frag_now);
2966 S_SET_VALUE (sym, frag_now_fix ());
2967 S_SET_STORAGE_CLASS (sym, C_FCN);
2969 coff_line_base = get_absolute_expression ();
2971 S_SET_NUMBER_AUXILIARY (sym, 1);
2972 SA_SET_SYM_LNNO (sym, coff_line_base);
2974 symbol_get_tc (sym)->output = 1;
2976 ppc_frob_label (sym);
2978 demand_empty_rest_of_line ();
2981 /* The .ef pseudo-op. This is just like a COFF C_FCN symbol named
2982 ".ef", except that the line number is absolute, not relative to the
2983 most recent ".bf" symbol. */
2985 static void
2986 ppc_ef (ignore)
2987 int ignore;
2989 symbolS *sym;
2991 sym = symbol_make (".ef");
2992 S_SET_SEGMENT (sym, text_section);
2993 symbol_set_frag (sym, frag_now);
2994 S_SET_VALUE (sym, frag_now_fix ());
2995 S_SET_STORAGE_CLASS (sym, C_FCN);
2996 S_SET_NUMBER_AUXILIARY (sym, 1);
2997 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
2998 symbol_get_tc (sym)->output = 1;
3000 ppc_frob_label (sym);
3002 demand_empty_rest_of_line ();
3005 /* The .bi and .ei pseudo-ops. These take a string argument and
3006 generates a C_BINCL or C_EINCL symbol, which goes at the start of
3007 the symbol list. */
3009 static void
3010 ppc_biei (ei)
3011 int ei;
3013 static symbolS *last_biei;
3015 char *name;
3016 int len;
3017 symbolS *sym;
3018 symbolS *look;
3020 name = demand_copy_C_string (&len);
3022 /* The value of these symbols is actually file offset. Here we set
3023 the value to the index into the line number entries. In
3024 ppc_frob_symbols we set the fix_line field, which will cause BFD
3025 to do the right thing. */
3027 sym = symbol_make (name);
3028 /* obj-coff.c currently only handles line numbers correctly in the
3029 .text section. */
3030 S_SET_SEGMENT (sym, text_section);
3031 S_SET_VALUE (sym, coff_n_line_nos);
3032 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
3034 S_SET_STORAGE_CLASS (sym, ei ? C_EINCL : C_BINCL);
3035 symbol_get_tc (sym)->output = 1;
3037 for (look = last_biei ? last_biei : symbol_rootP;
3038 (look != (symbolS *) NULL
3039 && (S_GET_STORAGE_CLASS (look) == C_FILE
3040 || S_GET_STORAGE_CLASS (look) == C_BINCL
3041 || S_GET_STORAGE_CLASS (look) == C_EINCL));
3042 look = symbol_next (look))
3044 if (look != (symbolS *) NULL)
3046 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
3047 symbol_insert (sym, look, &symbol_rootP, &symbol_lastP);
3048 last_biei = sym;
3051 demand_empty_rest_of_line ();
3054 /* The .bs pseudo-op. This generates a C_BSTAT symbol named ".bs".
3055 There is one argument, which is a csect symbol. The value of the
3056 .bs symbol is the index of this csect symbol. */
3058 static void
3059 ppc_bs (ignore)
3060 int ignore;
3062 char *name;
3063 char endc;
3064 symbolS *csect;
3065 symbolS *sym;
3067 if (ppc_current_block != NULL)
3068 as_bad (_("nested .bs blocks"));
3070 name = input_line_pointer;
3071 endc = get_symbol_end ();
3073 csect = symbol_find_or_make (name);
3075 *input_line_pointer = endc;
3077 sym = symbol_make (".bs");
3078 S_SET_SEGMENT (sym, now_seg);
3079 S_SET_STORAGE_CLASS (sym, C_BSTAT);
3080 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
3081 symbol_get_tc (sym)->output = 1;
3083 symbol_get_tc (sym)->within = csect;
3085 ppc_frob_label (sym);
3087 ppc_current_block = sym;
3089 demand_empty_rest_of_line ();
3092 /* The .es pseudo-op. Generate a C_ESTART symbol named .es. */
3094 static void
3095 ppc_es (ignore)
3096 int ignore;
3098 symbolS *sym;
3100 if (ppc_current_block == NULL)
3101 as_bad (_(".es without preceding .bs"));
3103 sym = symbol_make (".es");
3104 S_SET_SEGMENT (sym, now_seg);
3105 S_SET_STORAGE_CLASS (sym, C_ESTAT);
3106 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
3107 symbol_get_tc (sym)->output = 1;
3109 ppc_frob_label (sym);
3111 ppc_current_block = NULL;
3113 demand_empty_rest_of_line ();
3116 /* The .bb pseudo-op. Generate a C_BLOCK symbol named .bb, with a
3117 line number. */
3119 static void
3120 ppc_bb (ignore)
3121 int ignore;
3123 symbolS *sym;
3125 sym = symbol_make (".bb");
3126 S_SET_SEGMENT (sym, text_section);
3127 symbol_set_frag (sym, frag_now);
3128 S_SET_VALUE (sym, frag_now_fix ());
3129 S_SET_STORAGE_CLASS (sym, C_BLOCK);
3131 S_SET_NUMBER_AUXILIARY (sym, 1);
3132 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
3134 symbol_get_tc (sym)->output = 1;
3136 SF_SET_PROCESS (sym);
3138 ppc_frob_label (sym);
3140 demand_empty_rest_of_line ();
3143 /* The .eb pseudo-op. Generate a C_BLOCK symbol named .eb, with a
3144 line number. */
3146 static void
3147 ppc_eb (ignore)
3148 int ignore;
3150 symbolS *sym;
3152 sym = symbol_make (".eb");
3153 S_SET_SEGMENT (sym, text_section);
3154 symbol_set_frag (sym, frag_now);
3155 S_SET_VALUE (sym, frag_now_fix ());
3156 S_SET_STORAGE_CLASS (sym, C_BLOCK);
3157 S_SET_NUMBER_AUXILIARY (sym, 1);
3158 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
3159 symbol_get_tc (sym)->output = 1;
3161 SF_SET_PROCESS (sym);
3163 ppc_frob_label (sym);
3165 demand_empty_rest_of_line ();
3168 /* The .bc pseudo-op. This just creates a C_BCOMM symbol with a
3169 specified name. */
3171 static void
3172 ppc_bc (ignore)
3173 int ignore;
3175 char *name;
3176 int len;
3177 symbolS *sym;
3179 name = demand_copy_C_string (&len);
3180 sym = symbol_make (name);
3181 S_SET_SEGMENT (sym, ppc_coff_debug_section);
3182 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
3183 S_SET_STORAGE_CLASS (sym, C_BCOMM);
3184 S_SET_VALUE (sym, 0);
3185 symbol_get_tc (sym)->output = 1;
3187 ppc_frob_label (sym);
3189 demand_empty_rest_of_line ();
3192 /* The .ec pseudo-op. This just creates a C_ECOMM symbol. */
3194 static void
3195 ppc_ec (ignore)
3196 int ignore;
3198 symbolS *sym;
3200 sym = symbol_make (".ec");
3201 S_SET_SEGMENT (sym, ppc_coff_debug_section);
3202 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
3203 S_SET_STORAGE_CLASS (sym, C_ECOMM);
3204 S_SET_VALUE (sym, 0);
3205 symbol_get_tc (sym)->output = 1;
3207 ppc_frob_label (sym);
3209 demand_empty_rest_of_line ();
3212 /* The .toc pseudo-op. Switch to the .toc subsegment. */
3214 static void
3215 ppc_toc (ignore)
3216 int ignore;
3218 if (ppc_toc_csect != (symbolS *) NULL)
3219 subseg_set (data_section, symbol_get_tc (ppc_toc_csect)->subseg);
3220 else
3222 subsegT subseg;
3223 symbolS *sym;
3224 symbolS *list;
3226 subseg = ppc_data_subsegment;
3227 ++ppc_data_subsegment;
3229 subseg_new (segment_name (data_section), subseg);
3230 ppc_toc_frag = frag_now;
3232 sym = symbol_find_or_make ("TOC[TC0]");
3233 symbol_set_frag (sym, frag_now);
3234 S_SET_SEGMENT (sym, data_section);
3235 S_SET_VALUE (sym, (valueT) frag_now_fix ());
3236 symbol_get_tc (sym)->subseg = subseg;
3237 symbol_get_tc (sym)->output = 1;
3238 symbol_get_tc (sym)->within = sym;
3240 ppc_toc_csect = sym;
3242 for (list = ppc_data_csects;
3243 symbol_get_tc (list)->next != (symbolS *) NULL;
3244 list = symbol_get_tc (list)->next)
3246 symbol_get_tc (list)->next = sym;
3248 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
3249 symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
3250 &symbol_lastP);
3253 ppc_current_csect = ppc_toc_csect;
3255 demand_empty_rest_of_line ();
3258 /* The AIX assembler automatically aligns the operands of a .long or
3259 .short pseudo-op, and we want to be compatible. */
3261 static void
3262 ppc_xcoff_cons (log_size)
3263 int log_size;
3265 frag_align (log_size, 0, 0);
3266 record_alignment (now_seg, log_size);
3267 cons (1 << log_size);
3270 static void
3271 ppc_machine(dummy)
3272 int dummy;
3274 discard_rest_of_line();
3275 /* What does aix use this for? */
3278 static void
3279 ppc_vbyte (dummy)
3280 int dummy;
3282 expressionS exp;
3283 int byte_count;
3285 (void) expression (&exp);
3287 if (exp.X_op != O_constant)
3289 as_bad (_("non-constant byte count"));
3290 return;
3293 byte_count = exp.X_add_number;
3295 if (*input_line_pointer != ',')
3297 as_bad (_("missing value"));
3298 return;
3301 ++input_line_pointer;
3302 cons (byte_count);
3305 #endif /* OBJ_XCOFF */
3307 /* The .tc pseudo-op. This is used when generating either XCOFF or
3308 ELF. This takes two or more arguments.
3310 When generating XCOFF output, the first argument is the name to
3311 give to this location in the toc; this will be a symbol with class
3312 TC. The rest of the arguments are 4 byte values to actually put at
3313 this location in the TOC; often there is just one more argument, a
3314 relocateable symbol reference.
3316 When not generating XCOFF output, the arguments are the same, but
3317 the first argument is simply ignored. */
3319 static void
3320 ppc_tc (ignore)
3321 int ignore ATTRIBUTE_UNUSED;
3323 #ifdef OBJ_XCOFF
3325 /* Define the TOC symbol name. */
3327 char *name;
3328 char endc;
3329 symbolS *sym;
3331 if (ppc_toc_csect == (symbolS *) NULL
3332 || ppc_toc_csect != ppc_current_csect)
3334 as_bad (_(".tc not in .toc section"));
3335 ignore_rest_of_line ();
3336 return;
3339 name = input_line_pointer;
3340 endc = get_symbol_end ();
3342 sym = symbol_find_or_make (name);
3344 *input_line_pointer = endc;
3346 if (S_IS_DEFINED (sym))
3348 symbolS *label;
3350 label = symbol_get_tc (ppc_current_csect)->within;
3351 if (symbol_get_tc (label)->class != XMC_TC0)
3353 as_bad (_(".tc with no label"));
3354 ignore_rest_of_line ();
3355 return;
3358 S_SET_SEGMENT (label, S_GET_SEGMENT (sym));
3359 symbol_set_frag (label, symbol_get_frag (sym));
3360 S_SET_VALUE (label, S_GET_VALUE (sym));
3362 while (! is_end_of_line[(unsigned char) *input_line_pointer])
3363 ++input_line_pointer;
3365 return;
3368 S_SET_SEGMENT (sym, now_seg);
3369 symbol_set_frag (sym, frag_now);
3370 S_SET_VALUE (sym, (valueT) frag_now_fix ());
3371 symbol_get_tc (sym)->class = XMC_TC;
3372 symbol_get_tc (sym)->output = 1;
3374 ppc_frob_label (sym);
3377 #else /* ! defined (OBJ_XCOFF) */
3379 /* Skip the TOC symbol name. */
3380 while (is_part_of_name (*input_line_pointer)
3381 || *input_line_pointer == '['
3382 || *input_line_pointer == ']'
3383 || *input_line_pointer == '{'
3384 || *input_line_pointer == '}')
3385 ++input_line_pointer;
3387 /* Align to a four byte boundary. */
3388 frag_align (2, 0, 0);
3389 record_alignment (now_seg, 2);
3391 #endif /* ! defined (OBJ_XCOFF) */
3393 if (*input_line_pointer != ',')
3394 demand_empty_rest_of_line ();
3395 else
3397 ++input_line_pointer;
3398 cons ((ppc_size == PPC_OPCODE_64) ? 8 : 4);
3402 #ifdef TE_PE
3404 /* Pseudo-ops specific to the Windows NT PowerPC PE (coff) format */
3406 /* Set the current section. */
3407 static void
3408 ppc_set_current_section (new)
3409 segT new;
3411 ppc_previous_section = ppc_current_section;
3412 ppc_current_section = new;
3415 /* pseudo-op: .previous
3416 behaviour: toggles the current section with the previous section.
3417 errors: None
3418 warnings: "No previous section"
3420 static void
3421 ppc_previous(ignore)
3422 int ignore;
3424 symbolS *tmp;
3426 if (ppc_previous_section == NULL)
3428 as_warn(_("No previous section to return to. Directive ignored."));
3429 return;
3432 subseg_set(ppc_previous_section, 0);
3434 ppc_set_current_section(ppc_previous_section);
3437 /* pseudo-op: .pdata
3438 behaviour: predefined read only data section
3439 double word aligned
3440 errors: None
3441 warnings: None
3442 initial: .section .pdata "adr3"
3443 a - don't know -- maybe a misprint
3444 d - initialized data
3445 r - readable
3446 3 - double word aligned (that would be 4 byte boundary)
3448 commentary:
3449 Tag index tables (also known as the function table) for exception
3450 handling, debugging, etc.
3453 static void
3454 ppc_pdata(ignore)
3455 int ignore;
3457 if (pdata_section == 0)
3459 pdata_section = subseg_new (".pdata", 0);
3461 bfd_set_section_flags (stdoutput, pdata_section,
3462 (SEC_ALLOC | SEC_LOAD | SEC_RELOC
3463 | SEC_READONLY | SEC_DATA ));
3465 bfd_set_section_alignment (stdoutput, pdata_section, 2);
3467 else
3469 pdata_section = subseg_new(".pdata", 0);
3471 ppc_set_current_section(pdata_section);
3474 /* pseudo-op: .ydata
3475 behaviour: predefined read only data section
3476 double word aligned
3477 errors: None
3478 warnings: None
3479 initial: .section .ydata "drw3"
3480 a - don't know -- maybe a misprint
3481 d - initialized data
3482 r - readable
3483 3 - double word aligned (that would be 4 byte boundary)
3484 commentary:
3485 Tag tables (also known as the scope table) for exception handling,
3486 debugging, etc.
3488 static void
3489 ppc_ydata(ignore)
3490 int ignore;
3492 if (ydata_section == 0)
3494 ydata_section = subseg_new (".ydata", 0);
3495 bfd_set_section_flags (stdoutput, ydata_section,
3496 (SEC_ALLOC | SEC_LOAD | SEC_RELOC
3497 | SEC_READONLY | SEC_DATA ));
3499 bfd_set_section_alignment (stdoutput, ydata_section, 3);
3501 else
3503 ydata_section = subseg_new (".ydata", 0);
3505 ppc_set_current_section(ydata_section);
3508 /* pseudo-op: .reldata
3509 behaviour: predefined read write data section
3510 double word aligned (4-byte)
3511 FIXME: relocation is applied to it
3512 FIXME: what's the difference between this and .data?
3513 errors: None
3514 warnings: None
3515 initial: .section .reldata "drw3"
3516 d - initialized data
3517 r - readable
3518 w - writeable
3519 3 - double word aligned (that would be 8 byte boundary)
3521 commentary:
3522 Like .data, but intended to hold data subject to relocation, such as
3523 function descriptors, etc.
3525 static void
3526 ppc_reldata(ignore)
3527 int ignore;
3529 if (reldata_section == 0)
3531 reldata_section = subseg_new (".reldata", 0);
3533 bfd_set_section_flags (stdoutput, reldata_section,
3534 ( SEC_ALLOC | SEC_LOAD | SEC_RELOC
3535 | SEC_DATA ));
3537 bfd_set_section_alignment (stdoutput, reldata_section, 2);
3539 else
3541 reldata_section = subseg_new (".reldata", 0);
3543 ppc_set_current_section(reldata_section);
3546 /* pseudo-op: .rdata
3547 behaviour: predefined read only data section
3548 double word aligned
3549 errors: None
3550 warnings: None
3551 initial: .section .rdata "dr3"
3552 d - initialized data
3553 r - readable
3554 3 - double word aligned (that would be 4 byte boundary)
3556 static void
3557 ppc_rdata(ignore)
3558 int ignore;
3560 if (rdata_section == 0)
3562 rdata_section = subseg_new (".rdata", 0);
3563 bfd_set_section_flags (stdoutput, rdata_section,
3564 (SEC_ALLOC | SEC_LOAD | SEC_RELOC
3565 | SEC_READONLY | SEC_DATA ));
3567 bfd_set_section_alignment (stdoutput, rdata_section, 2);
3569 else
3571 rdata_section = subseg_new (".rdata", 0);
3573 ppc_set_current_section(rdata_section);
3576 /* pseudo-op: .ualong
3577 behaviour: much like .int, with the exception that no alignment is
3578 performed.
3579 FIXME: test the alignment statement
3580 errors: None
3581 warnings: None
3583 static void
3584 ppc_ualong(ignore)
3585 int ignore;
3587 /* try for long */
3588 cons ( 4 );
3591 /* pseudo-op: .znop <symbol name>
3592 behaviour: Issue a nop instruction
3593 Issue a IMAGE_REL_PPC_IFGLUE relocation against it, using
3594 the supplied symbol name.
3595 errors: None
3596 warnings: Missing symbol name
3598 static void
3599 ppc_znop(ignore)
3600 int ignore;
3602 unsigned long insn;
3603 const struct powerpc_opcode *opcode;
3604 expressionS ex;
3605 char *f;
3607 symbolS *sym;
3609 /* Strip out the symbol name */
3610 char *symbol_name;
3611 char c;
3612 char *name;
3613 unsigned int exp;
3614 flagword flags;
3615 asection *sec;
3617 symbol_name = input_line_pointer;
3618 c = get_symbol_end ();
3620 name = xmalloc (input_line_pointer - symbol_name + 1);
3621 strcpy (name, symbol_name);
3623 sym = symbol_find_or_make (name);
3625 *input_line_pointer = c;
3627 SKIP_WHITESPACE ();
3629 /* Look up the opcode in the hash table. */
3630 opcode = (const struct powerpc_opcode *) hash_find (ppc_hash, "nop");
3632 /* stick in the nop */
3633 insn = opcode->opcode;
3635 /* Write out the instruction. */
3636 f = frag_more (4);
3637 md_number_to_chars (f, insn, 4);
3638 fix_new (frag_now,
3639 f - frag_now->fr_literal,
3641 sym,
3644 BFD_RELOC_16_GOT_PCREL);
3648 /* pseudo-op:
3649 behaviour:
3650 errors:
3651 warnings:
3653 static void
3654 ppc_pe_comm(lcomm)
3655 int lcomm;
3657 register char *name;
3658 register char c;
3659 register char *p;
3660 offsetT temp;
3661 register symbolS *symbolP;
3662 offsetT align;
3664 name = input_line_pointer;
3665 c = get_symbol_end ();
3667 /* just after name is now '\0' */
3668 p = input_line_pointer;
3669 *p = c;
3670 SKIP_WHITESPACE ();
3671 if (*input_line_pointer != ',')
3673 as_bad (_("Expected comma after symbol-name: rest of line ignored."));
3674 ignore_rest_of_line ();
3675 return;
3678 input_line_pointer++; /* skip ',' */
3679 if ((temp = get_absolute_expression ()) < 0)
3681 as_warn (_(".COMMon length (%ld.) <0! Ignored."), (long) temp);
3682 ignore_rest_of_line ();
3683 return;
3686 if (! lcomm)
3688 /* The third argument to .comm is the alignment. */
3689 if (*input_line_pointer != ',')
3690 align = 3;
3691 else
3693 ++input_line_pointer;
3694 align = get_absolute_expression ();
3695 if (align <= 0)
3697 as_warn (_("ignoring bad alignment"));
3698 align = 3;
3703 *p = 0;
3704 symbolP = symbol_find_or_make (name);
3706 *p = c;
3707 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
3709 as_bad (_("Ignoring attempt to re-define symbol `%s'."),
3710 S_GET_NAME (symbolP));
3711 ignore_rest_of_line ();
3712 return;
3715 if (S_GET_VALUE (symbolP))
3717 if (S_GET_VALUE (symbolP) != (valueT) temp)
3718 as_bad (_("Length of .comm \"%s\" is already %ld. Not changed to %ld."),
3719 S_GET_NAME (symbolP),
3720 (long) S_GET_VALUE (symbolP),
3721 (long) temp);
3723 else
3725 S_SET_VALUE (symbolP, (valueT) temp);
3726 S_SET_EXTERNAL (symbolP);
3729 demand_empty_rest_of_line ();
3733 * implement the .section pseudo op:
3734 * .section name {, "flags"}
3735 * ^ ^
3736 * | +--- optional flags: 'b' for bss
3737 * | 'i' for info
3738 * +-- section name 'l' for lib
3739 * 'n' for noload
3740 * 'o' for over
3741 * 'w' for data
3742 * 'd' (apparently m88k for data)
3743 * 'x' for text
3744 * But if the argument is not a quoted string, treat it as a
3745 * subsegment number.
3747 * FIXME: this is a copy of the section processing from obj-coff.c, with
3748 * additions/changes for the moto-pas assembler support. There are three
3749 * categories:
3751 * FIXME: I just noticed this. This doesn't work at all really. It it
3752 * setting bits that bfd probably neither understands or uses. The
3753 * correct approach (?) will have to incorporate extra fields attached
3754 * to the section to hold the system specific stuff. (krk)
3756 * Section Contents:
3757 * 'a' - unknown - referred to in documentation, but no definition supplied
3758 * 'c' - section has code
3759 * 'd' - section has initialized data
3760 * 'u' - section has uninitialized data
3761 * 'i' - section contains directives (info)
3762 * 'n' - section can be discarded
3763 * 'R' - remove section at link time
3765 * Section Protection:
3766 * 'r' - section is readable
3767 * 'w' - section is writeable
3768 * 'x' - section is executable
3769 * 's' - section is sharable
3771 * Section Alignment:
3772 * '0' - align to byte boundary
3773 * '1' - align to halfword undary
3774 * '2' - align to word boundary
3775 * '3' - align to doubleword boundary
3776 * '4' - align to quadword boundary
3777 * '5' - align to 32 byte boundary
3778 * '6' - align to 64 byte boundary
3782 void
3783 ppc_pe_section (ignore)
3784 int ignore;
3786 /* Strip out the section name */
3787 char *section_name;
3788 char c;
3789 char *name;
3790 unsigned int exp;
3791 flagword flags;
3792 segT sec;
3793 int align;
3795 section_name = input_line_pointer;
3796 c = get_symbol_end ();
3798 name = xmalloc (input_line_pointer - section_name + 1);
3799 strcpy (name, section_name);
3801 *input_line_pointer = c;
3803 SKIP_WHITESPACE ();
3805 exp = 0;
3806 flags = SEC_NO_FLAGS;
3808 if (strcmp (name, ".idata$2") == 0)
3810 align = 0;
3812 else if (strcmp (name, ".idata$3") == 0)
3814 align = 0;
3816 else if (strcmp (name, ".idata$4") == 0)
3818 align = 2;
3820 else if (strcmp (name, ".idata$5") == 0)
3822 align = 2;
3824 else if (strcmp (name, ".idata$6") == 0)
3826 align = 1;
3828 else
3829 align = 4; /* default alignment to 16 byte boundary */
3831 if (*input_line_pointer == ',')
3833 ++input_line_pointer;
3834 SKIP_WHITESPACE ();
3835 if (*input_line_pointer != '"')
3836 exp = get_absolute_expression ();
3837 else
3839 ++input_line_pointer;
3840 while (*input_line_pointer != '"'
3841 && ! is_end_of_line[(unsigned char) *input_line_pointer])
3843 switch (*input_line_pointer)
3845 /* Section Contents */
3846 case 'a': /* unknown */
3847 as_bad (_("Unsupported section attribute -- 'a'"));
3848 break;
3849 case 'c': /* code section */
3850 flags |= SEC_CODE;
3851 break;
3852 case 'd': /* section has initialized data */
3853 flags |= SEC_DATA;
3854 break;
3855 case 'u': /* section has uninitialized data */
3856 /* FIXME: This is IMAGE_SCN_CNT_UNINITIALIZED_DATA
3857 in winnt.h */
3858 flags |= SEC_ROM;
3859 break;
3860 case 'i': /* section contains directives (info) */
3861 /* FIXME: This is IMAGE_SCN_LNK_INFO
3862 in winnt.h */
3863 flags |= SEC_HAS_CONTENTS;
3864 break;
3865 case 'n': /* section can be discarded */
3866 flags &=~ SEC_LOAD;
3867 break;
3868 case 'R': /* Remove section at link time */
3869 flags |= SEC_NEVER_LOAD;
3870 break;
3872 /* Section Protection */
3873 case 'r': /* section is readable */
3874 flags |= IMAGE_SCN_MEM_READ;
3875 break;
3876 case 'w': /* section is writeable */
3877 flags |= IMAGE_SCN_MEM_WRITE;
3878 break;
3879 case 'x': /* section is executable */
3880 flags |= IMAGE_SCN_MEM_EXECUTE;
3881 break;
3882 case 's': /* section is sharable */
3883 flags |= IMAGE_SCN_MEM_SHARED;
3884 break;
3886 /* Section Alignment */
3887 case '0': /* align to byte boundary */
3888 flags |= IMAGE_SCN_ALIGN_1BYTES;
3889 align = 0;
3890 break;
3891 case '1': /* align to halfword boundary */
3892 flags |= IMAGE_SCN_ALIGN_2BYTES;
3893 align = 1;
3894 break;
3895 case '2': /* align to word boundary */
3896 flags |= IMAGE_SCN_ALIGN_4BYTES;
3897 align = 2;
3898 break;
3899 case '3': /* align to doubleword boundary */
3900 flags |= IMAGE_SCN_ALIGN_8BYTES;
3901 align = 3;
3902 break;
3903 case '4': /* align to quadword boundary */
3904 flags |= IMAGE_SCN_ALIGN_16BYTES;
3905 align = 4;
3906 break;
3907 case '5': /* align to 32 byte boundary */
3908 flags |= IMAGE_SCN_ALIGN_32BYTES;
3909 align = 5;
3910 break;
3911 case '6': /* align to 64 byte boundary */
3912 flags |= IMAGE_SCN_ALIGN_64BYTES;
3913 align = 6;
3914 break;
3916 default:
3917 as_bad(_("unknown section attribute '%c'"),
3918 *input_line_pointer);
3919 break;
3921 ++input_line_pointer;
3923 if (*input_line_pointer == '"')
3924 ++input_line_pointer;
3928 sec = subseg_new (name, (subsegT) exp);
3930 ppc_set_current_section(sec);
3932 if (flags != SEC_NO_FLAGS)
3934 if (! bfd_set_section_flags (stdoutput, sec, flags))
3935 as_bad (_("error setting flags for \"%s\": %s"),
3936 bfd_section_name (stdoutput, sec),
3937 bfd_errmsg (bfd_get_error ()));
3940 bfd_set_section_alignment(stdoutput, sec, align);
3944 static void
3945 ppc_pe_function (ignore)
3946 int ignore;
3948 char *name;
3949 char endc;
3950 symbolS *ext_sym;
3952 name = input_line_pointer;
3953 endc = get_symbol_end ();
3955 ext_sym = symbol_find_or_make (name);
3957 *input_line_pointer = endc;
3959 S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
3960 SF_SET_FUNCTION (ext_sym);
3961 SF_SET_PROCESS (ext_sym);
3962 coff_add_linesym (ext_sym);
3964 demand_empty_rest_of_line ();
3967 static void
3968 ppc_pe_tocd (ignore)
3969 int ignore;
3971 if (tocdata_section == 0)
3973 tocdata_section = subseg_new (".tocd", 0);
3974 /* FIXME: section flags won't work */
3975 bfd_set_section_flags (stdoutput, tocdata_section,
3976 (SEC_ALLOC | SEC_LOAD | SEC_RELOC
3977 | SEC_READONLY | SEC_DATA ));
3979 bfd_set_section_alignment (stdoutput, tocdata_section, 2);
3981 else
3983 rdata_section = subseg_new (".tocd", 0);
3986 ppc_set_current_section(tocdata_section);
3988 demand_empty_rest_of_line ();
3991 /* Don't adjust TOC relocs to use the section symbol. */
3994 ppc_pe_fix_adjustable (fix)
3995 fixS *fix;
3997 return fix->fx_r_type != BFD_RELOC_PPC_TOC16;
4000 #endif
4002 #ifdef OBJ_XCOFF
4004 /* XCOFF specific symbol and file handling. */
4006 /* Canonicalize the symbol name. We use the to force the suffix, if
4007 any, to use square brackets, and to be in upper case. */
4009 char *
4010 ppc_canonicalize_symbol_name (name)
4011 char *name;
4013 char *s;
4015 if (ppc_stab_symbol)
4016 return name;
4018 for (s = name; *s != '\0' && *s != '{' && *s != '['; s++)
4020 if (*s != '\0')
4022 char brac;
4024 if (*s == '[')
4025 brac = ']';
4026 else
4028 *s = '[';
4029 brac = '}';
4032 for (s++; *s != '\0' && *s != brac; s++)
4033 if (islower (*s))
4034 *s = toupper (*s);
4036 if (*s == '\0' || s[1] != '\0')
4037 as_bad (_("bad symbol suffix"));
4039 *s = ']';
4042 return name;
4045 /* Set the class of a symbol based on the suffix, if any. This is
4046 called whenever a new symbol is created. */
4048 void
4049 ppc_symbol_new_hook (sym)
4050 symbolS *sym;
4052 struct ppc_tc_sy *tc;
4053 const char *s;
4055 tc = symbol_get_tc (sym);
4056 tc->next = NULL;
4057 tc->output = 0;
4058 tc->class = -1;
4059 tc->real_name = NULL;
4060 tc->subseg = 0;
4061 tc->align = 0;
4062 tc->size = NULL;
4063 tc->within = NULL;
4065 if (ppc_stab_symbol)
4066 return;
4068 s = strchr (S_GET_NAME (sym), '[');
4069 if (s == (const char *) NULL)
4071 /* There is no suffix. */
4072 return;
4075 ++s;
4077 switch (s[0])
4079 case 'B':
4080 if (strcmp (s, "BS]") == 0)
4081 tc->class = XMC_BS;
4082 break;
4083 case 'D':
4084 if (strcmp (s, "DB]") == 0)
4085 tc->class = XMC_DB;
4086 else if (strcmp (s, "DS]") == 0)
4087 tc->class = XMC_DS;
4088 break;
4089 case 'G':
4090 if (strcmp (s, "GL]") == 0)
4091 tc->class = XMC_GL;
4092 break;
4093 case 'P':
4094 if (strcmp (s, "PR]") == 0)
4095 tc->class = XMC_PR;
4096 break;
4097 case 'R':
4098 if (strcmp (s, "RO]") == 0)
4099 tc->class = XMC_RO;
4100 else if (strcmp (s, "RW]") == 0)
4101 tc->class = XMC_RW;
4102 break;
4103 case 'S':
4104 if (strcmp (s, "SV]") == 0)
4105 tc->class = XMC_SV;
4106 break;
4107 case 'T':
4108 if (strcmp (s, "TC]") == 0)
4109 tc->class = XMC_TC;
4110 else if (strcmp (s, "TI]") == 0)
4111 tc->class = XMC_TI;
4112 else if (strcmp (s, "TB]") == 0)
4113 tc->class = XMC_TB;
4114 else if (strcmp (s, "TC0]") == 0 || strcmp (s, "T0]") == 0)
4115 tc->class = XMC_TC0;
4116 break;
4117 case 'U':
4118 if (strcmp (s, "UA]") == 0)
4119 tc->class = XMC_UA;
4120 else if (strcmp (s, "UC]") == 0)
4121 tc->class = XMC_UC;
4122 break;
4123 case 'X':
4124 if (strcmp (s, "XO]") == 0)
4125 tc->class = XMC_XO;
4126 break;
4129 if (tc->class == -1)
4130 as_bad (_("Unrecognized symbol suffix"));
4133 /* Set the class of a label based on where it is defined. This
4134 handles symbols without suffixes. Also, move the symbol so that it
4135 follows the csect symbol. */
4137 void
4138 ppc_frob_label (sym)
4139 symbolS *sym;
4141 if (ppc_current_csect != (symbolS *) NULL)
4143 if (symbol_get_tc (sym)->class == -1)
4144 symbol_get_tc (sym)->class = symbol_get_tc (ppc_current_csect)->class;
4146 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
4147 symbol_append (sym, symbol_get_tc (ppc_current_csect)->within,
4148 &symbol_rootP, &symbol_lastP);
4149 symbol_get_tc (ppc_current_csect)->within = sym;
4153 /* This variable is set by ppc_frob_symbol if any absolute symbols are
4154 seen. It tells ppc_adjust_symtab whether it needs to look through
4155 the symbols. */
4157 static boolean ppc_saw_abs;
4159 /* Change the name of a symbol just before writing it out. Set the
4160 real name if the .rename pseudo-op was used. Otherwise, remove any
4161 class suffix. Return 1 if the symbol should not be included in the
4162 symbol table. */
4165 ppc_frob_symbol (sym)
4166 symbolS *sym;
4168 static symbolS *ppc_last_function;
4169 static symbolS *set_end;
4171 /* Discard symbols that should not be included in the output symbol
4172 table. */
4173 if (! symbol_used_in_reloc_p (sym)
4174 && ((symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) != 0
4175 || (! S_IS_EXTERNAL (sym)
4176 && ! symbol_get_tc (sym)->output
4177 && S_GET_STORAGE_CLASS (sym) != C_FILE)))
4178 return 1;
4180 if (symbol_get_tc (sym)->real_name != (char *) NULL)
4181 S_SET_NAME (sym, symbol_get_tc (sym)->real_name);
4182 else
4184 const char *name;
4185 const char *s;
4187 name = S_GET_NAME (sym);
4188 s = strchr (name, '[');
4189 if (s != (char *) NULL)
4191 unsigned int len;
4192 char *snew;
4194 len = s - name;
4195 snew = xmalloc (len + 1);
4196 memcpy (snew, name, len);
4197 snew[len] = '\0';
4199 S_SET_NAME (sym, snew);
4203 if (set_end != (symbolS *) NULL)
4205 SA_SET_SYM_ENDNDX (set_end, sym);
4206 set_end = NULL;
4209 if (SF_GET_FUNCTION (sym))
4211 if (ppc_last_function != (symbolS *) NULL)
4212 as_bad (_("two .function pseudo-ops with no intervening .ef"));
4213 ppc_last_function = sym;
4214 if (symbol_get_tc (sym)->size != (symbolS *) NULL)
4216 resolve_symbol_value (symbol_get_tc (sym)->size, 1);
4217 SA_SET_SYM_FSIZE (sym,
4218 (long) S_GET_VALUE (symbol_get_tc (sym)->size));
4221 else if (S_GET_STORAGE_CLASS (sym) == C_FCN
4222 && strcmp (S_GET_NAME (sym), ".ef") == 0)
4224 if (ppc_last_function == (symbolS *) NULL)
4225 as_bad (_(".ef with no preceding .function"));
4226 else
4228 set_end = ppc_last_function;
4229 ppc_last_function = NULL;
4231 /* We don't have a C_EFCN symbol, but we need to force the
4232 COFF backend to believe that it has seen one. */
4233 coff_last_function = NULL;
4237 if (! S_IS_EXTERNAL (sym)
4238 && (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) == 0
4239 && S_GET_STORAGE_CLASS (sym) != C_FILE
4240 && S_GET_STORAGE_CLASS (sym) != C_FCN
4241 && S_GET_STORAGE_CLASS (sym) != C_BLOCK
4242 && S_GET_STORAGE_CLASS (sym) != C_BSTAT
4243 && S_GET_STORAGE_CLASS (sym) != C_ESTAT
4244 && S_GET_STORAGE_CLASS (sym) != C_BINCL
4245 && S_GET_STORAGE_CLASS (sym) != C_EINCL
4246 && S_GET_SEGMENT (sym) != ppc_coff_debug_section)
4247 S_SET_STORAGE_CLASS (sym, C_HIDEXT);
4249 if (S_GET_STORAGE_CLASS (sym) == C_EXT
4250 || S_GET_STORAGE_CLASS (sym) == C_HIDEXT)
4252 int i;
4253 union internal_auxent *a;
4255 /* Create a csect aux. */
4256 i = S_GET_NUMBER_AUXILIARY (sym);
4257 S_SET_NUMBER_AUXILIARY (sym, i + 1);
4258 a = &coffsymbol (symbol_get_bfdsym (sym))->native[i + 1].u.auxent;
4259 if (symbol_get_tc (sym)->class == XMC_TC0)
4261 /* This is the TOC table. */
4262 know (strcmp (S_GET_NAME (sym), "TOC") == 0);
4263 a->x_csect.x_scnlen.l = 0;
4264 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
4266 else if (symbol_get_tc (sym)->subseg != 0)
4268 /* This is a csect symbol. x_scnlen is the size of the
4269 csect. */
4270 if (symbol_get_tc (sym)->next == (symbolS *) NULL)
4271 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
4272 S_GET_SEGMENT (sym))
4273 - S_GET_VALUE (sym));
4274 else
4276 resolve_symbol_value (symbol_get_tc (sym)->next, 1);
4277 a->x_csect.x_scnlen.l = (S_GET_VALUE (symbol_get_tc (sym)->next)
4278 - S_GET_VALUE (sym));
4280 a->x_csect.x_smtyp = (symbol_get_tc (sym)->align << 3) | XTY_SD;
4282 else if (S_GET_SEGMENT (sym) == bss_section)
4284 /* This is a common symbol. */
4285 a->x_csect.x_scnlen.l = symbol_get_frag (sym)->fr_offset;
4286 a->x_csect.x_smtyp = (symbol_get_tc (sym)->align << 3) | XTY_CM;
4287 if (S_IS_EXTERNAL (sym))
4288 symbol_get_tc (sym)->class = XMC_RW;
4289 else
4290 symbol_get_tc (sym)->class = XMC_BS;
4292 else if (S_GET_SEGMENT (sym) == absolute_section)
4294 /* This is an absolute symbol. The csect will be created by
4295 ppc_adjust_symtab. */
4296 ppc_saw_abs = true;
4297 a->x_csect.x_smtyp = XTY_LD;
4298 if (symbol_get_tc (sym)->class == -1)
4299 symbol_get_tc (sym)->class = XMC_XO;
4301 else if (! S_IS_DEFINED (sym))
4303 /* This is an external symbol. */
4304 a->x_csect.x_scnlen.l = 0;
4305 a->x_csect.x_smtyp = XTY_ER;
4307 else if (symbol_get_tc (sym)->class == XMC_TC)
4309 symbolS *next;
4311 /* This is a TOC definition. x_scnlen is the size of the
4312 TOC entry. */
4313 next = symbol_next (sym);
4314 while (symbol_get_tc (next)->class == XMC_TC0)
4315 next = symbol_next (next);
4316 if (next == (symbolS *) NULL
4317 || symbol_get_tc (next)->class != XMC_TC)
4319 if (ppc_after_toc_frag == (fragS *) NULL)
4320 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
4321 data_section)
4322 - S_GET_VALUE (sym));
4323 else
4324 a->x_csect.x_scnlen.l = (ppc_after_toc_frag->fr_address
4325 - S_GET_VALUE (sym));
4327 else
4329 resolve_symbol_value (next, 1);
4330 a->x_csect.x_scnlen.l = (S_GET_VALUE (next)
4331 - S_GET_VALUE (sym));
4333 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
4335 else
4337 symbolS *csect;
4339 /* This is a normal symbol definition. x_scnlen is the
4340 symbol index of the containing csect. */
4341 if (S_GET_SEGMENT (sym) == text_section)
4342 csect = ppc_text_csects;
4343 else if (S_GET_SEGMENT (sym) == data_section)
4344 csect = ppc_data_csects;
4345 else
4346 abort ();
4348 /* Skip the initial dummy symbol. */
4349 csect = symbol_get_tc (csect)->next;
4351 if (csect == (symbolS *) NULL)
4353 as_warn (_("warning: symbol %s has no csect"), S_GET_NAME (sym));
4354 a->x_csect.x_scnlen.l = 0;
4356 else
4358 while (symbol_get_tc (csect)->next != (symbolS *) NULL)
4360 resolve_symbol_value (symbol_get_tc (csect)->next, 1);
4361 if (S_GET_VALUE (symbol_get_tc (csect)->next)
4362 > S_GET_VALUE (sym))
4363 break;
4364 csect = symbol_get_tc (csect)->next;
4367 a->x_csect.x_scnlen.p =
4368 coffsymbol (symbol_get_bfdsym (csect))->native;
4369 coffsymbol (symbol_get_bfdsym (sym))->native[i + 1].fix_scnlen =
4372 a->x_csect.x_smtyp = XTY_LD;
4375 a->x_csect.x_parmhash = 0;
4376 a->x_csect.x_snhash = 0;
4377 if (symbol_get_tc (sym)->class == -1)
4378 a->x_csect.x_smclas = XMC_PR;
4379 else
4380 a->x_csect.x_smclas = symbol_get_tc (sym)->class;
4381 a->x_csect.x_stab = 0;
4382 a->x_csect.x_snstab = 0;
4384 /* Don't let the COFF backend resort these symbols. */
4385 symbol_get_bfdsym (sym)->flags |= BSF_NOT_AT_END;
4387 else if (S_GET_STORAGE_CLASS (sym) == C_BSTAT)
4389 /* We want the value to be the symbol index of the referenced
4390 csect symbol. BFD will do that for us if we set the right
4391 flags. */
4392 S_SET_VALUE (sym,
4393 ((valueT)
4394 coffsymbol (symbol_get_bfdsym
4395 (symbol_get_tc (sym)->within))->native));
4396 coffsymbol (symbol_get_bfdsym (sym))->native->fix_value = 1;
4398 else if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
4400 symbolS *block;
4401 symbolS *csect;
4403 /* The value is the offset from the enclosing csect. */
4404 block = symbol_get_tc (sym)->within;
4405 csect = symbol_get_tc (block)->within;
4406 resolve_symbol_value (csect, 1);
4407 S_SET_VALUE (sym, S_GET_VALUE (sym) - S_GET_VALUE (csect));
4409 else if (S_GET_STORAGE_CLASS (sym) == C_BINCL
4410 || S_GET_STORAGE_CLASS (sym) == C_EINCL)
4412 /* We want the value to be a file offset into the line numbers.
4413 BFD will do that for us if we set the right flags. We have
4414 already set the value correctly. */
4415 coffsymbol (symbol_get_bfdsym (sym))->native->fix_line = 1;
4418 return 0;
4421 /* Adjust the symbol table. This creates csect symbols for all
4422 absolute symbols. */
4424 void
4425 ppc_adjust_symtab ()
4427 symbolS *sym;
4429 if (! ppc_saw_abs)
4430 return;
4432 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
4434 symbolS *csect;
4435 int i;
4436 union internal_auxent *a;
4438 if (S_GET_SEGMENT (sym) != absolute_section)
4439 continue;
4441 csect = symbol_create (".abs[XO]", absolute_section,
4442 S_GET_VALUE (sym), &zero_address_frag);
4443 symbol_get_bfdsym (csect)->value = S_GET_VALUE (sym);
4444 S_SET_STORAGE_CLASS (csect, C_HIDEXT);
4445 i = S_GET_NUMBER_AUXILIARY (csect);
4446 S_SET_NUMBER_AUXILIARY (csect, i + 1);
4447 a = &coffsymbol (symbol_get_bfdsym (csect))->native[i + 1].u.auxent;
4448 a->x_csect.x_scnlen.l = 0;
4449 a->x_csect.x_smtyp = XTY_SD;
4450 a->x_csect.x_parmhash = 0;
4451 a->x_csect.x_snhash = 0;
4452 a->x_csect.x_smclas = XMC_XO;
4453 a->x_csect.x_stab = 0;
4454 a->x_csect.x_snstab = 0;
4456 symbol_insert (csect, sym, &symbol_rootP, &symbol_lastP);
4458 i = S_GET_NUMBER_AUXILIARY (sym);
4459 a = &coffsymbol (symbol_get_bfdsym (sym))->native[i].u.auxent;
4460 a->x_csect.x_scnlen.p = coffsymbol (symbol_get_bfdsym (csect))->native;
4461 coffsymbol (symbol_get_bfdsym (sym))->native[i].fix_scnlen = 1;
4464 ppc_saw_abs = false;
4467 /* Set the VMA for a section. This is called on all the sections in
4468 turn. */
4470 void
4471 ppc_frob_section (sec)
4472 asection *sec;
4474 static bfd_size_type vma = 0;
4476 bfd_set_section_vma (stdoutput, sec, vma);
4477 vma += bfd_section_size (stdoutput, sec);
4480 #endif /* OBJ_XCOFF */
4482 /* Turn a string in input_line_pointer into a floating point constant
4483 of type type, and store the appropriate bytes in *litp. The number
4484 of LITTLENUMS emitted is stored in *sizep . An error message is
4485 returned, or NULL on OK. */
4487 char *
4488 md_atof (type, litp, sizep)
4489 int type;
4490 char *litp;
4491 int *sizep;
4493 int prec;
4494 LITTLENUM_TYPE words[4];
4495 char *t;
4496 int i;
4498 switch (type)
4500 case 'f':
4501 prec = 2;
4502 break;
4504 case 'd':
4505 prec = 4;
4506 break;
4508 default:
4509 *sizep = 0;
4510 return _("bad call to md_atof");
4513 t = atof_ieee (input_line_pointer, type, words);
4514 if (t)
4515 input_line_pointer = t;
4517 *sizep = prec * 2;
4519 if (target_big_endian)
4521 for (i = 0; i < prec; i++)
4523 md_number_to_chars (litp, (valueT) words[i], 2);
4524 litp += 2;
4527 else
4529 for (i = prec - 1; i >= 0; i--)
4531 md_number_to_chars (litp, (valueT) words[i], 2);
4532 litp += 2;
4536 return NULL;
4539 /* Write a value out to the object file, using the appropriate
4540 endianness. */
4542 void
4543 md_number_to_chars (buf, val, n)
4544 char *buf;
4545 valueT val;
4546 int n;
4548 if (target_big_endian)
4549 number_to_chars_bigendian (buf, val, n);
4550 else
4551 number_to_chars_littleendian (buf, val, n);
4554 /* Align a section (I don't know why this is machine dependent). */
4556 valueT
4557 md_section_align (seg, addr)
4558 asection *seg;
4559 valueT addr;
4561 int align = bfd_get_section_alignment (stdoutput, seg);
4563 return ((addr + (1 << align) - 1) & (-1 << align));
4566 /* We don't have any form of relaxing. */
4569 md_estimate_size_before_relax (fragp, seg)
4570 fragS *fragp ATTRIBUTE_UNUSED;
4571 asection *seg ATTRIBUTE_UNUSED;
4573 abort ();
4574 return 0;
4577 /* Convert a machine dependent frag. We never generate these. */
4579 void
4580 md_convert_frag (abfd, sec, fragp)
4581 bfd *abfd ATTRIBUTE_UNUSED;
4582 asection *sec ATTRIBUTE_UNUSED;
4583 fragS *fragp ATTRIBUTE_UNUSED;
4585 abort ();
4588 /* We have no need to default values of symbols. */
4590 /*ARGSUSED*/
4591 symbolS *
4592 md_undefined_symbol (name)
4593 char *name ATTRIBUTE_UNUSED;
4595 return 0;
4598 /* Functions concerning relocs. */
4600 /* The location from which a PC relative jump should be calculated,
4601 given a PC relative reloc. */
4603 long
4604 md_pcrel_from_section (fixp, sec)
4605 fixS *fixp;
4606 segT sec ATTRIBUTE_UNUSED;
4608 return fixp->fx_frag->fr_address + fixp->fx_where;
4611 #ifdef OBJ_XCOFF
4613 /* This is called to see whether a fixup should be adjusted to use a
4614 section symbol. We take the opportunity to change a fixup against
4615 a symbol in the TOC subsegment into a reloc against the
4616 corresponding .tc symbol. */
4619 ppc_fix_adjustable (fix)
4620 fixS *fix;
4622 valueT val;
4624 resolve_symbol_value (fix->fx_addsy, 1);
4625 val = S_GET_VALUE (fix->fx_addsy);
4626 if (ppc_toc_csect != (symbolS *) NULL
4627 && fix->fx_addsy != (symbolS *) NULL
4628 && fix->fx_addsy != ppc_toc_csect
4629 && S_GET_SEGMENT (fix->fx_addsy) == data_section
4630 && val >= ppc_toc_frag->fr_address
4631 && (ppc_after_toc_frag == (fragS *) NULL
4632 || val < ppc_after_toc_frag->fr_address))
4634 symbolS *sy;
4636 for (sy = symbol_next (ppc_toc_csect);
4637 sy != (symbolS *) NULL;
4638 sy = symbol_next (sy))
4640 if (symbol_get_tc (sy)->class == XMC_TC0)
4641 continue;
4642 if (symbol_get_tc (sy)->class != XMC_TC)
4643 break;
4644 resolve_symbol_value (sy, 1);
4645 if (val == S_GET_VALUE (sy))
4647 fix->fx_addsy = sy;
4648 fix->fx_addnumber = val - ppc_toc_frag->fr_address;
4649 return 0;
4653 as_bad_where (fix->fx_file, fix->fx_line,
4654 _("symbol in .toc does not match any .tc"));
4657 /* Possibly adjust the reloc to be against the csect. */
4658 if (fix->fx_addsy != (symbolS *) NULL
4659 && symbol_get_tc (fix->fx_addsy)->subseg == 0
4660 && symbol_get_tc (fix->fx_addsy)->class != XMC_TC0
4661 && symbol_get_tc (fix->fx_addsy)->class != XMC_TC
4662 && S_GET_SEGMENT (fix->fx_addsy) != bss_section
4663 /* Don't adjust if this is a reloc in the toc section. */
4664 && (S_GET_SEGMENT (fix->fx_addsy) != data_section
4665 || ppc_toc_csect == NULL
4666 || val < ppc_toc_frag->fr_address
4667 || (ppc_after_toc_frag != NULL
4668 && val >= ppc_after_toc_frag->fr_address)))
4670 symbolS *csect;
4672 if (S_GET_SEGMENT (fix->fx_addsy) == text_section)
4673 csect = ppc_text_csects;
4674 else if (S_GET_SEGMENT (fix->fx_addsy) == data_section)
4675 csect = ppc_data_csects;
4676 else
4677 abort ();
4679 /* Skip the initial dummy symbol. */
4680 csect = symbol_get_tc (csect)->next;
4682 if (csect != (symbolS *) NULL)
4684 while (symbol_get_tc (csect)->next != (symbolS *) NULL
4685 && (symbol_get_frag (symbol_get_tc (csect)->next)->fr_address
4686 <= val))
4688 /* If the csect address equals the symbol value, then we
4689 have to look through the full symbol table to see
4690 whether this is the csect we want. Note that we will
4691 only get here if the csect has zero length. */
4692 if ((symbol_get_frag (csect)->fr_address == val)
4693 && S_GET_VALUE (csect) == S_GET_VALUE (fix->fx_addsy))
4695 symbolS *scan;
4697 for (scan = symbol_next (csect);
4698 scan != NULL;
4699 scan = symbol_next (scan))
4701 if (symbol_get_tc (scan)->subseg != 0)
4702 break;
4703 if (scan == fix->fx_addsy)
4704 break;
4707 /* If we found the symbol before the next csect
4708 symbol, then this is the csect we want. */
4709 if (scan == fix->fx_addsy)
4710 break;
4713 csect = symbol_get_tc (csect)->next;
4716 fix->fx_offset += (S_GET_VALUE (fix->fx_addsy)
4717 - symbol_get_frag (csect)->fr_address);
4718 fix->fx_addsy = csect;
4722 /* Adjust a reloc against a .lcomm symbol to be against the base
4723 .lcomm. */
4724 if (fix->fx_addsy != (symbolS *) NULL
4725 && S_GET_SEGMENT (fix->fx_addsy) == bss_section
4726 && ! S_IS_EXTERNAL (fix->fx_addsy))
4728 resolve_symbol_value (symbol_get_frag (fix->fx_addsy)->fr_symbol, 1);
4729 fix->fx_offset +=
4730 (S_GET_VALUE (fix->fx_addsy)
4731 - S_GET_VALUE (symbol_get_frag (fix->fx_addsy)->fr_symbol));
4732 fix->fx_addsy = symbol_get_frag (fix->fx_addsy)->fr_symbol;
4735 return 0;
4738 /* A reloc from one csect to another must be kept. The assembler
4739 will, of course, keep relocs between sections, and it will keep
4740 absolute relocs, but we need to force it to keep PC relative relocs
4741 between two csects in the same section. */
4744 ppc_force_relocation (fix)
4745 fixS *fix;
4747 /* At this point fix->fx_addsy should already have been converted to
4748 a csect symbol. If the csect does not include the fragment, then
4749 we need to force the relocation. */
4750 if (fix->fx_pcrel
4751 && fix->fx_addsy != NULL
4752 && symbol_get_tc (fix->fx_addsy)->subseg != 0
4753 && ((symbol_get_frag (fix->fx_addsy)->fr_address
4754 > fix->fx_frag->fr_address)
4755 || (symbol_get_tc (fix->fx_addsy)->next != NULL
4756 && (symbol_get_frag (symbol_get_tc (fix->fx_addsy)->next)->fr_address
4757 <= fix->fx_frag->fr_address))))
4758 return 1;
4760 return 0;
4763 #endif /* OBJ_XCOFF */
4765 /* See whether a symbol is in the TOC section. */
4767 static int
4768 ppc_is_toc_sym (sym)
4769 symbolS *sym;
4771 #ifdef OBJ_XCOFF
4772 return symbol_get_tc (sym)->class == XMC_TC;
4773 #else
4774 return strcmp (segment_name (S_GET_SEGMENT (sym)), ".got") == 0;
4775 #endif
4778 /* Apply a fixup to the object code. This is called for all the
4779 fixups we generated by the call to fix_new_exp, above. In the call
4780 above we used a reloc code which was the largest legal reloc code
4781 plus the operand index. Here we undo that to recover the operand
4782 index. At this point all symbol values should be fully resolved,
4783 and we attempt to completely resolve the reloc. If we can not do
4784 that, we determine the correct reloc code and put it back in the
4785 fixup. */
4788 md_apply_fix3 (fixp, valuep, seg)
4789 fixS *fixp;
4790 valueT *valuep;
4791 segT seg;
4793 valueT value;
4795 #ifdef OBJ_ELF
4796 value = *valuep;
4797 if (fixp->fx_addsy != NULL)
4799 /* `*valuep' may contain the value of the symbol on which the reloc
4800 will be based; we have to remove it. */
4801 if (symbol_used_in_reloc_p (fixp->fx_addsy)
4802 && S_GET_SEGMENT (fixp->fx_addsy) != absolute_section
4803 && S_GET_SEGMENT (fixp->fx_addsy) != undefined_section
4804 && ! bfd_is_com_section (S_GET_SEGMENT (fixp->fx_addsy)))
4805 value -= S_GET_VALUE (fixp->fx_addsy);
4807 /* FIXME: Why '+'? Better yet, what exactly is '*valuep'
4808 supposed to be? I think this is related to various similar
4809 FIXMEs in tc-i386.c and tc-sparc.c. */
4810 if (fixp->fx_pcrel)
4811 value += fixp->fx_frag->fr_address + fixp->fx_where;
4813 else
4815 fixp->fx_done = 1;
4817 #else
4818 /* FIXME FIXME FIXME: The value we are passed in *valuep includes
4819 the symbol values. Since we are using BFD_ASSEMBLER, if we are
4820 doing this relocation the code in write.c is going to call
4821 bfd_install_relocation, which is also going to use the symbol
4822 value. That means that if the reloc is fully resolved we want to
4823 use *valuep since bfd_install_relocation is not being used.
4824 However, if the reloc is not fully resolved we do not want to use
4825 *valuep, and must use fx_offset instead. However, if the reloc
4826 is PC relative, we do want to use *valuep since it includes the
4827 result of md_pcrel_from. This is confusing. */
4828 if (fixp->fx_addsy == (symbolS *) NULL)
4830 value = *valuep;
4831 fixp->fx_done = 1;
4833 else if (fixp->fx_pcrel)
4834 value = *valuep;
4835 else
4837 value = fixp->fx_offset;
4838 if (fixp->fx_subsy != (symbolS *) NULL)
4840 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
4841 value -= S_GET_VALUE (fixp->fx_subsy);
4842 else
4844 /* We can't actually support subtracting a symbol. */
4845 as_bad_where (fixp->fx_file, fixp->fx_line,
4846 _("expression too complex"));
4850 #endif
4852 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
4854 int opindex;
4855 const struct powerpc_operand *operand;
4856 char *where;
4857 unsigned long insn;
4859 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
4861 operand = &powerpc_operands[opindex];
4863 #ifdef OBJ_XCOFF
4864 /* It appears that an instruction like
4865 l 9,LC..1(30)
4866 when LC..1 is not a TOC symbol does not generate a reloc. It
4867 uses the offset of LC..1 within its csect. However, .long
4868 LC..1 will generate a reloc. I can't find any documentation
4869 on how these cases are to be distinguished, so this is a wild
4870 guess. These cases are generated by gcc -mminimal-toc. */
4871 if ((operand->flags & PPC_OPERAND_PARENS) != 0
4872 && operand->bits == 16
4873 && operand->shift == 0
4874 && operand->insert == NULL
4875 && fixp->fx_addsy != NULL
4876 && symbol_get_tc (fixp->fx_addsy)->subseg != 0
4877 && symbol_get_tc (fixp->fx_addsy)->class != XMC_TC
4878 && symbol_get_tc (fixp->fx_addsy)->class != XMC_TC0
4879 && S_GET_SEGMENT (fixp->fx_addsy) != bss_section)
4881 value = fixp->fx_offset;
4882 fixp->fx_done = 1;
4884 #endif
4886 /* Fetch the instruction, insert the fully resolved operand
4887 value, and stuff the instruction back again. */
4888 where = fixp->fx_frag->fr_literal + fixp->fx_where;
4889 if (target_big_endian)
4890 insn = bfd_getb32 ((unsigned char *) where);
4891 else
4892 insn = bfd_getl32 ((unsigned char *) where);
4893 insn = ppc_insert_operand (insn, operand, (offsetT) value,
4894 fixp->fx_file, fixp->fx_line);
4895 if (target_big_endian)
4896 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
4897 else
4898 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
4900 if (fixp->fx_done)
4902 /* Nothing else to do here. */
4903 return 1;
4906 /* Determine a BFD reloc value based on the operand information.
4907 We are only prepared to turn a few of the operands into
4908 relocs.
4909 FIXME: We need to handle the DS field at the very least.
4910 FIXME: Selecting the reloc type is a bit haphazard; perhaps
4911 there should be a new field in the operand table. */
4912 if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
4913 && operand->bits == 26
4914 && operand->shift == 0)
4915 fixp->fx_r_type = BFD_RELOC_PPC_B26;
4916 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
4917 && operand->bits == 16
4918 && operand->shift == 0)
4919 fixp->fx_r_type = BFD_RELOC_PPC_B16;
4920 else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0
4921 && operand->bits == 26
4922 && operand->shift == 0)
4923 fixp->fx_r_type = BFD_RELOC_PPC_BA26;
4924 else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0
4925 && operand->bits == 16
4926 && operand->shift == 0)
4927 fixp->fx_r_type = BFD_RELOC_PPC_BA16;
4928 else if ((operand->flags & PPC_OPERAND_PARENS) != 0
4929 && operand->bits == 16
4930 && operand->shift == 0
4931 && fixp->fx_addsy != NULL
4932 && ppc_is_toc_sym (fixp->fx_addsy))
4934 fixp->fx_size = 2;
4935 if (target_big_endian)
4936 fixp->fx_where += 2;
4937 fixp->fx_r_type = BFD_RELOC_PPC_TOC16;
4939 else
4941 char *sfile;
4942 unsigned int sline;
4944 /* Use expr_symbol_where to see if this is an expression
4945 symbol. */
4946 if (expr_symbol_where (fixp->fx_addsy, &sfile, &sline))
4947 as_bad_where (fixp->fx_file, fixp->fx_line,
4948 _("unresolved expression that must be resolved"));
4949 else
4950 as_bad_where (fixp->fx_file, fixp->fx_line,
4951 _("unsupported relocation type"));
4952 fixp->fx_done = 1;
4953 return 1;
4956 else
4958 #ifdef OBJ_ELF
4959 ppc_elf_validate_fix (fixp, seg);
4960 #endif
4961 switch (fixp->fx_r_type)
4963 case BFD_RELOC_32:
4964 case BFD_RELOC_CTOR:
4965 if (fixp->fx_pcrel)
4966 fixp->fx_r_type = BFD_RELOC_32_PCREL;
4967 /* fall through */
4969 case BFD_RELOC_RVA:
4970 case BFD_RELOC_32_PCREL:
4971 case BFD_RELOC_32_BASEREL:
4972 case BFD_RELOC_PPC_EMB_NADDR32:
4973 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
4974 value, 4);
4975 break;
4977 case BFD_RELOC_64:
4978 if (fixp->fx_pcrel)
4979 fixp->fx_r_type = BFD_RELOC_64_PCREL;
4980 /* fall through */
4981 case BFD_RELOC_64_PCREL:
4982 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
4983 value, 8);
4984 break;
4985 case BFD_RELOC_LO16:
4986 case BFD_RELOC_16:
4987 case BFD_RELOC_GPREL16:
4988 case BFD_RELOC_16_GOT_PCREL:
4989 case BFD_RELOC_16_GOTOFF:
4990 case BFD_RELOC_LO16_GOTOFF:
4991 case BFD_RELOC_HI16_GOTOFF:
4992 case BFD_RELOC_HI16_S_GOTOFF:
4993 case BFD_RELOC_LO16_BASEREL:
4994 case BFD_RELOC_HI16_BASEREL:
4995 case BFD_RELOC_HI16_S_BASEREL:
4996 case BFD_RELOC_PPC_EMB_NADDR16:
4997 case BFD_RELOC_PPC_EMB_NADDR16_LO:
4998 case BFD_RELOC_PPC_EMB_NADDR16_HI:
4999 case BFD_RELOC_PPC_EMB_NADDR16_HA:
5000 case BFD_RELOC_PPC_EMB_SDAI16:
5001 case BFD_RELOC_PPC_EMB_SDA2REL:
5002 case BFD_RELOC_PPC_EMB_SDA2I16:
5003 case BFD_RELOC_PPC_EMB_RELSEC16:
5004 case BFD_RELOC_PPC_EMB_RELST_LO:
5005 case BFD_RELOC_PPC_EMB_RELST_HI:
5006 case BFD_RELOC_PPC_EMB_RELST_HA:
5007 case BFD_RELOC_PPC_EMB_RELSDA:
5008 case BFD_RELOC_PPC_TOC16:
5009 if (fixp->fx_pcrel)
5011 if (fixp->fx_addsy != NULL)
5012 as_bad_where (fixp->fx_file, fixp->fx_line,
5013 _("cannot emit PC relative %s relocation against %s"),
5014 bfd_get_reloc_code_name (fixp->fx_r_type),
5015 S_GET_NAME (fixp->fx_addsy));
5016 else
5017 as_bad_where (fixp->fx_file, fixp->fx_line,
5018 _("cannot emit PC relative %s relocation"),
5019 bfd_get_reloc_code_name (fixp->fx_r_type));
5022 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
5023 value, 2);
5024 break;
5026 /* This case happens when you write, for example,
5027 lis %r3,(L1-L2)@ha
5028 where L1 and L2 are defined later. */
5029 case BFD_RELOC_HI16:
5030 if (fixp->fx_pcrel)
5031 abort ();
5032 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
5033 value >> 16, 2);
5034 break;
5035 case BFD_RELOC_HI16_S:
5036 if (fixp->fx_pcrel)
5037 abort ();
5038 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
5039 (value + 0x8000) >> 16, 2);
5040 break;
5042 /* Because SDA21 modifies the register field, the size is set to 4
5043 bytes, rather than 2, so offset it here appropriately */
5044 case BFD_RELOC_PPC_EMB_SDA21:
5045 if (fixp->fx_pcrel)
5046 abort ();
5048 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where
5049 + ((target_big_endian) ? 2 : 0),
5050 value, 2);
5051 break;
5053 case BFD_RELOC_8:
5054 if (fixp->fx_pcrel)
5055 abort ();
5057 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
5058 value, 1);
5059 break;
5061 case BFD_RELOC_24_PLT_PCREL:
5062 case BFD_RELOC_PPC_LOCAL24PC:
5063 if (!fixp->fx_pcrel && !fixp->fx_done)
5064 abort ();
5066 if (fixp->fx_done)
5068 char *where;
5069 unsigned long insn;
5071 /* Fetch the instruction, insert the fully resolved operand
5072 value, and stuff the instruction back again. */
5073 where = fixp->fx_frag->fr_literal + fixp->fx_where;
5074 if (target_big_endian)
5075 insn = bfd_getb32 ((unsigned char *) where);
5076 else
5077 insn = bfd_getl32 ((unsigned char *) where);
5078 if ((value & 3) != 0)
5079 as_bad_where (fixp->fx_file, fixp->fx_line,
5080 _("must branch to an address a multiple of 4"));
5081 if ((offsetT) value < -0x40000000
5082 || (offsetT) value >= 0x40000000)
5083 as_bad_where (fixp->fx_file, fixp->fx_line,
5084 _("@local or @plt branch destination is too far away, %ld bytes"),
5085 value);
5086 insn = insn | (value & 0x03fffffc);
5087 if (target_big_endian)
5088 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
5089 else
5090 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
5092 break;
5094 case BFD_RELOC_VTABLE_INHERIT:
5095 fixp->fx_done = 0;
5096 if (fixp->fx_addsy
5097 && !S_IS_DEFINED (fixp->fx_addsy)
5098 && !S_IS_WEAK (fixp->fx_addsy))
5099 S_SET_WEAK (fixp->fx_addsy);
5100 break;
5102 case BFD_RELOC_VTABLE_ENTRY:
5103 fixp->fx_done = 0;
5104 break;
5106 default:
5107 fprintf(stderr,
5108 _("Gas failure, reloc value %d\n"), fixp->fx_r_type);
5109 fflush(stderr);
5110 abort ();
5114 #ifdef OBJ_ELF
5115 fixp->fx_addnumber = value;
5116 #else
5117 if (fixp->fx_r_type != BFD_RELOC_PPC_TOC16)
5118 fixp->fx_addnumber = 0;
5119 else
5121 #ifdef TE_PE
5122 fixp->fx_addnumber = 0;
5123 #else
5124 /* We want to use the offset within the data segment of the
5125 symbol, not the actual VMA of the symbol. */
5126 fixp->fx_addnumber =
5127 - bfd_get_section_vma (stdoutput, S_GET_SEGMENT (fixp->fx_addsy));
5128 #endif
5130 #endif
5132 return 1;
5135 /* Generate a reloc for a fixup. */
5137 arelent *
5138 tc_gen_reloc (seg, fixp)
5139 asection *seg ATTRIBUTE_UNUSED;
5140 fixS *fixp;
5142 arelent *reloc;
5144 reloc = (arelent *) xmalloc (sizeof (arelent));
5146 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
5147 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
5148 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
5149 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
5150 if (reloc->howto == (reloc_howto_type *) NULL)
5152 as_bad_where (fixp->fx_file, fixp->fx_line,
5153 _("reloc %d not supported by object file format"), (int)fixp->fx_r_type);
5154 return NULL;
5156 reloc->addend = fixp->fx_addnumber;
5158 return reloc;