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