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