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