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