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