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