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