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