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