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