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