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