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