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