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