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