095dd8a6a63e93aa09c4359201967bf2c0d0006c
[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 demand_empty_rest_of_line ();
1757 }
1758
1759 /* The .extern pseudo-op. We create an undefined symbol. */
1760
1761 static void
1762 ppc_extern (ignore)
1763 int ignore;
1764 {
1765 char *name;
1766 char endc;
1767
1768 name = input_line_pointer;
1769 endc = get_symbol_end ();
1770
1771 (void) symbol_find_or_make (name);
1772
1773 *input_line_pointer = endc;
1774
1775 demand_empty_rest_of_line ();
1776 }
1777
1778 /* The .lglobl pseudo-op. I think the RS/6000 assembler only needs
1779 this because it can't handle undefined symbols. I think we can
1780 just ignore it. */
1781
1782 static void
1783 ppc_lglobl (ignore)
1784 int ignore;
1785 {
1786 s_ignore (0);
1787 }
1788
1789 /* The .rename pseudo-op. The RS/6000 assembler can rename symbols,
1790 although I don't know why it bothers. */
1791
1792 static void
1793 ppc_rename (ignore)
1794 int ignore;
1795 {
1796 char *name;
1797 char endc;
1798 symbolS *sym;
1799 int len;
1800
1801 name = input_line_pointer;
1802 endc = get_symbol_end ();
1803
1804 sym = symbol_find_or_make (name);
1805
1806 *input_line_pointer = endc;
1807
1808 if (*input_line_pointer != ',')
1809 {
1810 as_bad ("missing rename string");
1811 ignore_rest_of_line ();
1812 return;
1813 }
1814 ++input_line_pointer;
1815
1816 sym->sy_tc.real_name = demand_copy_C_string (&len);
1817
1818 demand_empty_rest_of_line ();
1819 }
1820
1821 /* The .stabx pseudo-op. This is similar to a normal .stabs
1822 pseudo-op, but slightly different. A sample is
1823 .stabx "main:F-1",.main,142,0
1824 The first argument is the symbol name to create. The second is the
1825 value, and the third is the storage class. The fourth seems to be
1826 always zero, and I am assuming it is the type. */
1827
1828 static void
1829 ppc_stabx (ignore)
1830 int ignore;
1831 {
1832 char *name;
1833 int len;
1834 symbolS *sym;
1835 expressionS exp;
1836
1837 name = demand_copy_C_string (&len);
1838
1839 if (*input_line_pointer != ',')
1840 {
1841 as_bad ("missing value");
1842 return;
1843 }
1844 ++input_line_pointer;
1845
1846 sym = symbol_make (name);
1847
1848 (void) expression (&exp);
1849
1850 switch (exp.X_op)
1851 {
1852 case O_illegal:
1853 case O_absent:
1854 case O_big:
1855 as_bad ("illegal .stabx expression; zero assumed");
1856 exp.X_add_number = 0;
1857 /* Fall through. */
1858 case O_constant:
1859 S_SET_VALUE (sym, (valueT) exp.X_add_number);
1860 sym->sy_frag = &zero_address_frag;
1861 break;
1862
1863 case O_symbol:
1864 if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section)
1865 sym->sy_value = exp;
1866 else
1867 {
1868 S_SET_VALUE (sym,
1869 exp.X_add_number + S_GET_VALUE (exp.X_add_symbol));
1870 sym->sy_frag = exp.X_add_symbol->sy_frag;
1871 }
1872 break;
1873
1874 default:
1875 /* The value is some complex expression. This will probably
1876 fail at some later point, but this is probably the right
1877 thing to do here. */
1878 sym->sy_value = exp;
1879 break;
1880 }
1881
1882 S_SET_SEGMENT (sym, ppc_coff_debug_section);
1883 sym->bsym->flags |= BSF_DEBUGGING;
1884
1885 if (*input_line_pointer != ',')
1886 {
1887 as_bad ("missing class");
1888 return;
1889 }
1890 ++input_line_pointer;
1891
1892 S_SET_STORAGE_CLASS (sym, get_absolute_expression ());
1893
1894 if (*input_line_pointer != ',')
1895 {
1896 as_bad ("missing type");
1897 return;
1898 }
1899 ++input_line_pointer;
1900
1901 S_SET_DATA_TYPE (sym, get_absolute_expression ());
1902
1903 sym->sy_tc.output = 1;
1904
1905 if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
1906 sym->sy_tc.within = ppc_current_block;
1907
1908 if (exp.X_op != O_symbol
1909 || ! S_IS_EXTERNAL (exp.X_add_symbol)
1910 || S_GET_SEGMENT (exp.X_add_symbol) != bss_section)
1911 ppc_frob_label (sym);
1912 else
1913 {
1914 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
1915 symbol_append (sym, exp.X_add_symbol, &symbol_rootP, &symbol_lastP);
1916 if (ppc_current_csect->sy_tc.within == exp.X_add_symbol)
1917 ppc_current_csect->sy_tc.within = sym;
1918 }
1919
1920 if (strlen (name) > SYMNMLEN)
1921 {
1922 /* For some reason, each name is preceded by a two byte length
1923 and followed by a null byte. */
1924 ppc_debug_name_section_size += strlen (name) + 3;
1925 }
1926
1927 demand_empty_rest_of_line ();
1928 }
1929
1930 /* The .function pseudo-op. This takes several arguments. The first
1931 argument seems to be the external name of the symbol. The second
1932 argment seems to be the label for the start of the function. gcc
1933 uses the same name for both. I have no idea what the third and
1934 fourth arguments are meant to be. The optional fifth argument is
1935 an expression for the size of the function. In COFF this symbol
1936 gets an aux entry like that used for a csect. */
1937
1938 static void
1939 ppc_function (ignore)
1940 int ignore;
1941 {
1942 char *name;
1943 char endc;
1944 char *s;
1945 symbolS *ext_sym;
1946 symbolS *lab_sym;
1947
1948 name = input_line_pointer;
1949 endc = get_symbol_end ();
1950
1951 /* Ignore any [PR] suffix. */
1952 name = ppc_canonicalize_symbol_name (name);
1953 s = strchr (name, '[');
1954 if (s != (char *) NULL
1955 && strcmp (s + 1, "PR]") == 0)
1956 *s = '\0';
1957
1958 ext_sym = symbol_find_or_make (name);
1959
1960 *input_line_pointer = endc;
1961
1962 if (*input_line_pointer != ',')
1963 {
1964 as_bad ("missing symbol name");
1965 ignore_rest_of_line ();
1966 return;
1967 }
1968 ++input_line_pointer;
1969
1970 name = input_line_pointer;
1971 endc = get_symbol_end ();
1972
1973 lab_sym = symbol_find_or_make (name);
1974
1975 *input_line_pointer = endc;
1976
1977 if (ext_sym != lab_sym)
1978 {
1979 ext_sym->sy_value.X_op = O_symbol;
1980 ext_sym->sy_value.X_add_symbol = lab_sym;
1981 ext_sym->sy_value.X_op_symbol = NULL;
1982 ext_sym->sy_value.X_add_number = 0;
1983 }
1984
1985 if (ext_sym->sy_tc.class == -1)
1986 ext_sym->sy_tc.class = XMC_PR;
1987 ext_sym->sy_tc.output = 1;
1988
1989 if (*input_line_pointer == ',')
1990 {
1991 expressionS ignore;
1992
1993 /* Ignore the third argument. */
1994 ++input_line_pointer;
1995 expression (&ignore);
1996 if (*input_line_pointer == ',')
1997 {
1998 /* Ignore the fourth argument. */
1999 ++input_line_pointer;
2000 expression (&ignore);
2001 if (*input_line_pointer == ',')
2002 {
2003 /* The fifth argument is the function size. */
2004 ++input_line_pointer;
2005 ext_sym->sy_tc.size = symbol_new ("L0\001",
2006 absolute_section,
2007 (valueT) 0,
2008 &zero_address_frag);
2009 pseudo_set (ext_sym->sy_tc.size);
2010 }
2011 }
2012 }
2013
2014 S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
2015 SF_SET_FUNCTION (ext_sym);
2016 SF_SET_PROCESS (ext_sym);
2017 coff_add_linesym (ext_sym);
2018
2019 demand_empty_rest_of_line ();
2020 }
2021
2022 /* The .bf pseudo-op. This is just like a COFF C_FCN symbol named
2023 ".bf". */
2024
2025 static void
2026 ppc_bf (ignore)
2027 int ignore;
2028 {
2029 symbolS *sym;
2030
2031 sym = symbol_make (".bf");
2032 S_SET_SEGMENT (sym, text_section);
2033 sym->sy_frag = frag_now;
2034 S_SET_VALUE (sym, frag_now_fix ());
2035 S_SET_STORAGE_CLASS (sym, C_FCN);
2036
2037 coff_line_base = get_absolute_expression ();
2038
2039 S_SET_NUMBER_AUXILIARY (sym, 1);
2040 SA_SET_SYM_LNNO (sym, coff_line_base);
2041
2042 sym->sy_tc.output = 1;
2043
2044 ppc_frob_label (sym);
2045
2046 demand_empty_rest_of_line ();
2047 }
2048
2049 /* The .ef pseudo-op. This is just like a COFF C_FCN symbol named
2050 ".ef", except that the line number is absolute, not relative to the
2051 most recent ".bf" symbol. */
2052
2053 static void
2054 ppc_ef (ignore)
2055 int ignore;
2056 {
2057 symbolS *sym;
2058
2059 sym = symbol_make (".ef");
2060 S_SET_SEGMENT (sym, text_section);
2061 sym->sy_frag = frag_now;
2062 S_SET_VALUE (sym, frag_now_fix ());
2063 S_SET_STORAGE_CLASS (sym, C_FCN);
2064 S_SET_NUMBER_AUXILIARY (sym, 1);
2065 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
2066 sym->sy_tc.output = 1;
2067
2068 ppc_frob_label (sym);
2069
2070 demand_empty_rest_of_line ();
2071 }
2072
2073 /* The .bi and .ei pseudo-ops. These take a string argument and
2074 generates a C_BINCL or C_EINCL symbol, which goes at the start of
2075 the symbol list. */
2076
2077 static void
2078 ppc_biei (ei)
2079 int ei;
2080 {
2081 char *name;
2082 int len;
2083 symbolS *sym;
2084 symbolS *look;
2085
2086 name = demand_copy_C_string (&len);
2087
2088 sym = symbol_make (name);
2089 S_SET_SEGMENT (sym, ppc_coff_debug_section);
2090 sym->bsym->flags |= BSF_DEBUGGING;
2091
2092 /* FIXME: The value of the .bi or .ei symbol is supposed to be the
2093 offset in the file to the line number entry to use. That is
2094 quite difficult to implement using BFD, so I'm just not doing it.
2095 Sorry. Please add it if you can figure out how. Note that this
2096 approach is the only way to support multiple files in COFF, since
2097 line numbers are associated with function symbols. Note further
2098 that it still doesn't work, since the line numbers are stored as
2099 offsets from a base line number. */
2100
2101 S_SET_STORAGE_CLASS (sym, ei ? C_EINCL : C_BINCL);
2102 sym->sy_tc.output = 1;
2103
2104 for (look = symbol_rootP;
2105 (look != (symbolS *) NULL
2106 && (S_GET_STORAGE_CLASS (look) == C_FILE
2107 || S_GET_STORAGE_CLASS (look) == C_BINCL
2108 || S_GET_STORAGE_CLASS (look) == C_EINCL));
2109 look = symbol_next (look))
2110 ;
2111 if (look != (symbolS *) NULL)
2112 {
2113 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2114 symbol_insert (sym, look, &symbol_rootP, &symbol_lastP);
2115 }
2116
2117 demand_empty_rest_of_line ();
2118 }
2119
2120 /* The .bs pseudo-op. This generates a C_BSTAT symbol named ".bs".
2121 There is one argument, which is a csect symbol. The value of the
2122 .bs symbol is the index of this csect symbol. */
2123
2124 static void
2125 ppc_bs (ignore)
2126 int ignore;
2127 {
2128 char *name;
2129 char endc;
2130 symbolS *csect;
2131 symbolS *sym;
2132
2133 if (ppc_current_block != NULL)
2134 as_bad ("nested .bs blocks");
2135
2136 name = input_line_pointer;
2137 endc = get_symbol_end ();
2138
2139 csect = symbol_find_or_make (name);
2140
2141 *input_line_pointer = endc;
2142
2143 sym = symbol_make (".bs");
2144 S_SET_SEGMENT (sym, now_seg);
2145 S_SET_STORAGE_CLASS (sym, C_BSTAT);
2146 sym->bsym->flags |= BSF_DEBUGGING;
2147 sym->sy_tc.output = 1;
2148
2149 sym->sy_tc.within = csect;
2150
2151 ppc_frob_label (sym);
2152
2153 ppc_current_block = sym;
2154
2155 demand_empty_rest_of_line ();
2156 }
2157
2158 /* The .es pseudo-op. Generate a C_ESTART symbol named .es. */
2159
2160 static void
2161 ppc_es (ignore)
2162 int ignore;
2163 {
2164 symbolS *sym;
2165
2166 if (ppc_current_block == NULL)
2167 as_bad (".es without preceding .bs");
2168
2169 sym = symbol_make (".es");
2170 S_SET_SEGMENT (sym, now_seg);
2171 S_SET_STORAGE_CLASS (sym, C_ESTAT);
2172 sym->bsym->flags |= BSF_DEBUGGING;
2173 sym->sy_tc.output = 1;
2174
2175 ppc_frob_label (sym);
2176
2177 ppc_current_block = NULL;
2178
2179 demand_empty_rest_of_line ();
2180 }
2181
2182 /* The .bb pseudo-op. Generate a C_BLOCK symbol named .bb, with a
2183 line number. */
2184
2185 static void
2186 ppc_bb (ignore)
2187 int ignore;
2188 {
2189 symbolS *sym;
2190
2191 sym = symbol_make (".bb");
2192 S_SET_SEGMENT (sym, text_section);
2193 sym->sy_frag = frag_now;
2194 S_SET_VALUE (sym, frag_now_fix ());
2195 S_SET_STORAGE_CLASS (sym, C_BLOCK);
2196
2197 S_SET_NUMBER_AUXILIARY (sym, 1);
2198 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
2199
2200 sym->sy_tc.output = 1;
2201
2202 ppc_frob_label (sym);
2203
2204 demand_empty_rest_of_line ();
2205 }
2206
2207 /* The .eb pseudo-op. Generate a C_BLOCK symbol named .eb, with a
2208 line number. */
2209
2210 static void
2211 ppc_eb (ignore)
2212 int ignore;
2213 {
2214 symbolS *sym;
2215
2216 sym = symbol_make (".eb");
2217 S_SET_SEGMENT (sym, text_section);
2218 sym->sy_frag = frag_now;
2219 S_SET_VALUE (sym, frag_now_fix ());
2220 S_SET_STORAGE_CLASS (sym, C_FCN);
2221 S_SET_NUMBER_AUXILIARY (sym, 1);
2222 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
2223 sym->sy_tc.output = 1;
2224
2225 ppc_frob_label (sym);
2226
2227 demand_empty_rest_of_line ();
2228 }
2229
2230 /* The .toc pseudo-op. Switch to the .toc subsegment. */
2231
2232 static void
2233 ppc_toc (ignore)
2234 int ignore;
2235 {
2236 if (ppc_toc_csect != (symbolS *) NULL)
2237 subseg_set (data_section, ppc_toc_csect->sy_tc.subseg);
2238 else
2239 {
2240 subsegT subseg;
2241 symbolS *sym;
2242 symbolS *list;
2243
2244 subseg = ppc_data_subsegment;
2245 ++ppc_data_subsegment;
2246
2247 subseg_new (segment_name (data_section), subseg);
2248 ppc_toc_frag = frag_now;
2249
2250 sym = symbol_find_or_make ("TOC[TC0]");
2251 sym->sy_frag = frag_now;
2252 S_SET_SEGMENT (sym, data_section);
2253 S_SET_VALUE (sym, (valueT) frag_now_fix ());
2254 sym->sy_tc.subseg = subseg;
2255 sym->sy_tc.output = 1;
2256 sym->sy_tc.within = sym;
2257
2258 ppc_toc_csect = sym;
2259
2260 for (list = ppc_data_csects;
2261 list->sy_tc.next != (symbolS *) NULL;
2262 list = list->sy_tc.next)
2263 ;
2264 list->sy_tc.next = sym;
2265
2266 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2267 symbol_append (sym, list->sy_tc.within, &symbol_rootP, &symbol_lastP);
2268 }
2269
2270 demand_empty_rest_of_line ();
2271 }
2272
2273 #endif /* OBJ_COFF */
2274 \f
2275 /* The .tc pseudo-op. This is used when generating either XCOFF or
2276 ELF. This takes two or more arguments.
2277
2278 When generating XCOFF output, the first argument is the name to
2279 give to this location in the toc; this will be a symbol with class
2280 TC. The rest of the arguments are 4 byte values to actually put at
2281 this location in the TOC; often there is just one more argument, a
2282 relocateable symbol reference.
2283
2284 When not generating XCOFF output, the arguments are the same, but
2285 the first argument is simply ignored. */
2286
2287 static void
2288 ppc_tc (ignore)
2289 int ignore;
2290 {
2291 #ifdef OBJ_COFF
2292
2293 /* Define the TOC symbol name. */
2294 {
2295 char *name;
2296 char endc;
2297 symbolS *sym;
2298
2299 if (ppc_toc_csect == (symbolS *) NULL
2300 || ppc_toc_csect != ppc_current_csect)
2301 {
2302 as_bad (".tc not in .toc section");
2303 ignore_rest_of_line ();
2304 return;
2305 }
2306
2307 name = input_line_pointer;
2308 endc = get_symbol_end ();
2309
2310 sym = symbol_find_or_make (name);
2311
2312 *input_line_pointer = endc;
2313
2314 if (S_IS_DEFINED (sym))
2315 {
2316 symbolS *label;
2317
2318 label = ppc_current_csect->sy_tc.within;
2319 if (label->sy_tc.class != XMC_TC0)
2320 {
2321 as_warn (".tc with no label");
2322 ignore_rest_of_line ();
2323 return;
2324 }
2325
2326 S_SET_SEGMENT (label, S_GET_SEGMENT (sym));
2327 label->sy_frag = sym->sy_frag;
2328 S_SET_VALUE (label, S_GET_VALUE (sym));
2329
2330 while (! is_end_of_line[(unsigned char) *input_line_pointer])
2331 ++input_line_pointer;
2332
2333 return;
2334 }
2335
2336 S_SET_SEGMENT (sym, now_seg);
2337 sym->sy_frag = frag_now;
2338 S_SET_VALUE (sym, (valueT) frag_now_fix ());
2339 sym->sy_tc.class = XMC_TC;
2340 sym->sy_tc.output = 1;
2341
2342 ppc_frob_label (sym);
2343 }
2344
2345 #else /* ! defined (OBJ_COFF) */
2346
2347 /* Skip the TOC symbol name. */
2348 while (is_part_of_name (*input_line_pointer)
2349 || *input_line_pointer == '['
2350 || *input_line_pointer == ']'
2351 || *input_line_pointer == '{'
2352 || *input_line_pointer == '}')
2353 ++input_line_pointer;
2354
2355 /* Align to a four byte boundary. */
2356 frag_align (2, 0);
2357 record_alignment (now_seg, 2);
2358
2359 #endif /* ! defined (OBJ_COFF) */
2360
2361 if (*input_line_pointer != ',')
2362 demand_empty_rest_of_line ();
2363 else
2364 {
2365 ++input_line_pointer;
2366 cons (4);
2367 }
2368 }
2369
2370 \f
2371 #ifdef TE_PE
2372 /* Pseudo-ops specific to the Windows NT PowerPC PE (coff) format */
2373
2374 /*
2375 * Section characteristics
2376 */
2377
2378 #define IMAGE_SCN_TYPE_NO_PAD 0x00000008 /* Reserved. */
2379
2380 #define IMAGE_SCN_CNT_CODE 0x00000020 /* Section contains code. */
2381 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 /* Section contains initialized data. */
2382 #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 /* Section contains uninitialized data. */
2383
2384 #define IMAGE_SCN_LNK_OTHER 0x00000100 /* Reserved.
2385 #define IMAGE_SCN_LNK_INFO 0x00000200 /* Section contains comments or some other type of information. */
2386 #define IMAGE_SCN_LNK_REMOVE 0x00000800 /* Section contents will not become part of image. */
2387 #define IMAGE_SCN_LNK_COMDAT 0x00001000 /* Section contents comdat. */
2388
2389 #define IMAGE_SCN_MEM_FARDATA 0x00008000
2390
2391 #define IMAGE_SCN_MEM_PURGEABLE 0x00020000
2392 #define IMAGE_SCN_MEM_16BIT 0x00020000
2393 #define IMAGE_SCN_MEM_LOCKED 0x00040000
2394 #define IMAGE_SCN_MEM_PRELOAD 0x00080000
2395
2396 #define IMAGE_SCN_ALIGN_1BYTES 0x00100000
2397 #define IMAGE_SCN_ALIGN_2BYTES 0x00200000
2398 #define IMAGE_SCN_ALIGN_4BYTES 0x00300000
2399 #define IMAGE_SCN_ALIGN_8BYTES 0x00400000
2400 #define IMAGE_SCN_ALIGN_16BYTES 0x00500000 /* Default alignment if no others are specified. */
2401 #define IMAGE_SCN_ALIGN_32BYTES 0x00600000
2402 #define IMAGE_SCN_ALIGN_64BYTES 0x00700000
2403
2404
2405 #define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 /* Section contains extended relocations. */
2406 #define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 /* Section can be discarded. */
2407 #define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 /* Section is not cachable. */
2408 #define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 /* Section is not pageable. */
2409 #define IMAGE_SCN_MEM_SHARED 0x10000000 /* Section is shareable. */
2410 #define IMAGE_SCN_MEM_EXECUTE 0x20000000 /* Section is executable. */
2411 #define IMAGE_SCN_MEM_READ 0x40000000 /* Section is readable. */
2412 #define IMAGE_SCN_MEM_WRITE 0x80000000 /* Section is writeable. */
2413
2414
2415 /* pseudo-op: .previous
2416 behaviour: toggles the current section with the previous section.
2417 errors: None
2418 warnings: "No previous section"
2419 */
2420 static void
2421 ppc_previous(ignore)
2422 int ignore;
2423 {
2424 symbolS *tmp;
2425
2426 if (ppc_previous_section == NULL)
2427 {
2428 as_warn("No previous section to return to. Directive ignored.");
2429 return;
2430 }
2431
2432 subseg_set(ppc_previous_section, 0);
2433
2434 setCurrentSection(ppc_previous_section);
2435 }
2436
2437 /* pseudo-op: .pdata
2438 behaviour: predefined read only data section
2439 double word aligned
2440 errors: None
2441 warnings: None
2442 initial: .section .pdata "adr3"
2443 a - don't know -- maybe a misprint
2444 d - initialized data
2445 r - readable
2446 3 - double word aligned (that would be 4 byte boundary)
2447
2448 commentary:
2449 Tag index tables (also known as the function table) for exception
2450 handling, debugging, etc.
2451
2452 */
2453 static void
2454 ppc_pdata(ignore)
2455 int ignore;
2456 {
2457 if (pdata_section == 0)
2458 {
2459 pdata_section = subseg_new (".pdata", 0);
2460
2461 bfd_set_section_flags (stdoutput, pdata_section,
2462 (SEC_ALLOC | SEC_LOAD | SEC_RELOC
2463 | SEC_READONLY | SEC_DATA ));
2464
2465 bfd_set_section_alignment (stdoutput, pdata_section, 3);
2466 }
2467 else
2468 {
2469 pdata_section = subseg_new(".pdata", 0);
2470 }
2471 setCurrentSection(pdata_section);
2472 }
2473
2474 /* pseudo-op: .ydata
2475 behaviour: predefined read only data section
2476 double word aligned
2477 errors: None
2478 warnings: None
2479 initial: .section .ydata "drw3"
2480 a - don't know -- maybe a misprint
2481 d - initialized data
2482 r - readable
2483 3 - double word aligned (that would be 4 byte boundary)
2484 commentary:
2485 Tag tables (also known as the scope table) for exception handling,
2486 debugging, etc.
2487 */
2488 static void
2489 ppc_ydata(ignore)
2490 int ignore;
2491 {
2492 if (ydata_section == 0)
2493 {
2494 ydata_section = subseg_new (".ydata", 0);
2495 bfd_set_section_flags (stdoutput, ydata_section,
2496 (SEC_ALLOC | SEC_LOAD | SEC_RELOC
2497 | SEC_READONLY | SEC_DATA ));
2498
2499 bfd_set_section_alignment (stdoutput, ydata_section, 3);
2500 }
2501 else
2502 {
2503 ydata_section = subseg_new (".ydata", 0);
2504 }
2505 setCurrentSection(ydata_section);
2506 }
2507
2508 /* pseudo-op: .reldata
2509 behaviour: predefined read write data section
2510 double word aligned (4-byte)
2511 FIXME: relocation is applied to it
2512 FIXME: what's the difference between this and .data?
2513 errors: None
2514 warnings: None
2515 initial: .section .reldata "drw3"
2516 d - initialized data
2517 r - readable
2518 w - writeable
2519 3 - double word aligned (that would be 8 byte boundary)
2520
2521 commentary:
2522 Like .data, but intended to hold data subject to relocation, such as
2523 function descriptors, etc.
2524 */
2525 static void
2526 ppc_reldata(ignore)
2527 int ignore;
2528 {
2529 if (reldata_section == 0)
2530 {
2531 reldata_section = subseg_new (".reldata", 0);
2532
2533 bfd_set_section_flags (stdoutput, reldata_section,
2534 ( SEC_ALLOC | SEC_LOAD | SEC_RELOC
2535 | SEC_DATA ));
2536
2537 bfd_set_section_alignment (stdoutput, reldata_section, 3);
2538 }
2539 else
2540 {
2541 reldata_section = subseg_new (".reldata", 0);
2542 }
2543 setCurrentSection(reldata_section);
2544 }
2545
2546 /* pseudo-op: .rdata
2547 behaviour: predefined read only data section
2548 double word aligned
2549 errors: None
2550 warnings: None
2551 initial: .section .rdata "dr3"
2552 d - initialized data
2553 r - readable
2554 3 - double word aligned (that would be 4 byte boundary)
2555 */
2556 static void
2557 ppc_rdata(ignore)
2558 int ignore;
2559 {
2560 if (rdata_section == 0)
2561 {
2562 rdata_section = subseg_new (".rdata", 0);
2563 bfd_set_section_flags (stdoutput, rdata_section,
2564 (SEC_ALLOC | SEC_LOAD | SEC_RELOC
2565 | SEC_READONLY | SEC_DATA ));
2566
2567 bfd_set_section_alignment (stdoutput, rdata_section, 2);
2568 }
2569 else
2570 {
2571 rdata_section = subseg_new (".rdata", 0);
2572 }
2573 setCurrentSection(rdata_section);
2574 }
2575
2576 /* pseudo-op: .ualong
2577 behaviour: much like .int, with the exception that no alignment is
2578 performed.
2579 FIXME: test the alignment statement
2580 errors: None
2581 warnings: None
2582 */
2583 static void
2584 ppc_ualong(ignore)
2585 int ignore;
2586 {
2587 /* try for long */
2588 cons ( 4 );
2589 }
2590
2591 /* pseudo-op: .znop <symbol name>
2592 behaviour: Issue a nop instruction
2593 Issue a IMAGE_REL_PPC_IFGLUE relocation against it, using
2594 the supplied symbol name.
2595 errors: None
2596 warnings: Missing symbol name
2597 */
2598 static void
2599 ppc_znop(ignore)
2600 int ignore;
2601 {
2602 unsigned long insn;
2603 const struct powerpc_opcode *opcode;
2604 expressionS ex;
2605 char *f;
2606
2607 symbolS *sym;
2608
2609 /* Strip out the symbol name */
2610 char *symbol_name;
2611 char c;
2612 char *name;
2613 unsigned int exp;
2614 flagword flags;
2615 asection *sec;
2616
2617 symbol_name = input_line_pointer;
2618 c = get_symbol_end ();
2619
2620 name = xmalloc (input_line_pointer - symbol_name + 1);
2621 strcpy (name, symbol_name);
2622
2623 sym = symbol_find_or_make (name);
2624
2625 *input_line_pointer = c;
2626
2627 SKIP_WHITESPACE ();
2628
2629 /* Look up the opcode in the hash table. */
2630 opcode = (const struct powerpc_opcode *) hash_find (ppc_hash, "nop");
2631
2632 /* stick in the nop */
2633 insn = opcode->opcode;
2634
2635 /* Write out the instruction. */
2636 f = frag_more (4);
2637 md_number_to_chars (f, insn, 4);
2638 fix_new (frag_now,
2639 f - frag_now->fr_literal,
2640 4,
2641 sym,
2642 0,
2643 0,
2644 BFD_RELOC_16_GOT_PCREL);
2645
2646 }
2647 /* pseudo-op:
2648 behaviour:
2649 errors:
2650 warnings:
2651 */
2652 static void
2653 ppc_pe_comm(lcomm)
2654 int lcomm;
2655 {
2656 register char *name;
2657 register char c;
2658 register char *p;
2659 offsetT temp;
2660 register symbolS *symbolP;
2661 offsetT align;
2662
2663 name = input_line_pointer;
2664 c = get_symbol_end ();
2665
2666 /* just after name is now '\0' */
2667 p = input_line_pointer;
2668 *p = c;
2669 SKIP_WHITESPACE ();
2670 if (*input_line_pointer != ',')
2671 {
2672 as_bad ("Expected comma after symbol-name: rest of line ignored.");
2673 ignore_rest_of_line ();
2674 return;
2675 }
2676
2677 input_line_pointer++; /* skip ',' */
2678 if ((temp = get_absolute_expression ()) < 0)
2679 {
2680 as_warn (".COMMon length (%ld.) <0! Ignored.", (long) temp);
2681 ignore_rest_of_line ();
2682 return;
2683 }
2684
2685 if (! lcomm)
2686 {
2687 /* The third argument to .comm is the alignment. */
2688 if (*input_line_pointer != ',')
2689 align = 3;
2690 else
2691 {
2692 ++input_line_pointer;
2693 align = get_absolute_expression ();
2694 if (align <= 0)
2695 {
2696 as_warn ("ignoring bad alignment");
2697 align = 3;
2698 }
2699 }
2700 }
2701
2702 *p = 0;
2703 symbolP = symbol_find_or_make (name);
2704
2705 *p = c;
2706 if (S_IS_DEFINED (symbolP))
2707 {
2708 as_bad ("Ignoring attempt to re-define symbol `%s'.",
2709 S_GET_NAME (symbolP));
2710 ignore_rest_of_line ();
2711 return;
2712 }
2713
2714 if (S_GET_VALUE (symbolP))
2715 {
2716 if (S_GET_VALUE (symbolP) != (valueT) temp)
2717 as_bad ("Length of .comm \"%s\" is already %ld. Not changed to %ld.",
2718 S_GET_NAME (symbolP),
2719 (long) S_GET_VALUE (symbolP),
2720 (long) temp);
2721 }
2722 else
2723 {
2724 S_SET_VALUE (symbolP, (valueT) temp);
2725 S_SET_EXTERNAL (symbolP);
2726 }
2727
2728 demand_empty_rest_of_line ();
2729 }
2730
2731 /*
2732 * implement the .section pseudo op:
2733 * .section name {, "flags"}
2734 * ^ ^
2735 * | +--- optional flags: 'b' for bss
2736 * | 'i' for info
2737 * +-- section name 'l' for lib
2738 * 'n' for noload
2739 * 'o' for over
2740 * 'w' for data
2741 * 'd' (apparently m88k for data)
2742 * 'x' for text
2743 * But if the argument is not a quoted string, treat it as a
2744 * subsegment number.
2745 *
2746 * FIXME: this is a copy of the section processing from obj-coff.c, with
2747 * additions/changes for the moto-pas assembler support. There are three
2748 * categories:
2749 *
2750 * Section Contents:
2751 * 'a' - unknown - referred to in documentation, but no definition supplied
2752 * 'c' - section has code
2753 * 'd' - section has initialized data
2754 * 'u' - section has uninitialized data
2755 * 'i' - section contains directives (info)
2756 * 'n' - section can be discarded
2757 * 'R' - remove section at link time
2758 *
2759 * Section Protection:
2760 * 'r' - section is readable
2761 * 'w' - section is writeable
2762 * 'x' - section is executable
2763 * 's' - section is sharable
2764 *
2765 * Section Alignment:
2766 * '0' - align to byte boundary
2767 * '1' - align to halfword undary
2768 * '2' - align to word boundary
2769 * '3' - align to doubleword boundary
2770 * '4' - align to quadword boundary
2771 * '5' - align to 32 byte boundary
2772 * '6' - align to 64 byte boundary
2773 *
2774 */
2775
2776 void
2777 ppc_pe_section (ignore)
2778 int ignore;
2779 {
2780 /* Strip out the section name */
2781 char *section_name;
2782 char c;
2783 char *name;
2784 unsigned int exp;
2785 flagword flags;
2786 segT sec;
2787 int align;
2788
2789 align = 4; /* default alignment to 16 byte boundary */
2790
2791 section_name = input_line_pointer;
2792 c = get_symbol_end ();
2793
2794 name = xmalloc (input_line_pointer - section_name + 1);
2795 strcpy (name, section_name);
2796
2797 *input_line_pointer = c;
2798
2799 SKIP_WHITESPACE ();
2800
2801 exp = 0;
2802 flags = SEC_NO_FLAGS;
2803
2804 if (*input_line_pointer == ',')
2805 {
2806 ++input_line_pointer;
2807 SKIP_WHITESPACE ();
2808 if (*input_line_pointer != '"')
2809 exp = get_absolute_expression ();
2810 else
2811 {
2812 ++input_line_pointer;
2813 while (*input_line_pointer != '"'
2814 && ! is_end_of_line[(unsigned char) *input_line_pointer])
2815 {
2816 switch (*input_line_pointer)
2817 {
2818 /* Section Contents */
2819 case 'a': /* unknown */
2820 as_warn ("Unsupported section attribute -- 'a'");
2821 break;
2822 case 'c': /* code section */
2823 flags |= SEC_CODE;
2824 break;
2825 case 'd': /* section has initialized data */
2826 flags |= SEC_DATA;
2827 break;
2828 case 'u': /* section has uninitialized data */
2829 /* FIXME: This is IMAGE_SCN_CNT_UNINITIALIZED_DATA
2830 in winnt.h */
2831 flags |= SEC_ROM;
2832 break;
2833 case 'i': /* section contains directives (info) */
2834 /* FIXME: This is IMAGE_SCN_LNK_INFO
2835 in winnt.h */
2836 flags |= SEC_HAS_CONTENTS;
2837 break;
2838 case 'n': /* section can be discarded */
2839 flags &=~ SEC_LOAD;
2840 break;
2841 case 'R': /* Remove section at link time */
2842 flags |= SEC_NEVER_LOAD;
2843 break;
2844 /* Section Protection */
2845
2846 #if 0
2847 #define IMAGE_SCN_MEM_SHARED 0x10000000 /* Section is shareable. */
2848 #define IMAGE_SCN_MEM_EXECUTE 0x20000000 /* Section is executable. */
2849 #define IMAGE_SCN_MEM_READ 0x40000000 /* Section is readable. */
2850 #define IMAGE_SCN_MEM_WRITE 0x80000000 /* Section is writeable. */
2851 #endif
2852
2853 case 'r': /* section is readable */
2854 flags |= IMAGE_SCN_MEM_READ;
2855 break;
2856 case 'w': /* section is writeable */
2857 flags |= IMAGE_SCN_MEM_WRITE;
2858 break;
2859 case 'x': /* section is executable */
2860 flags |= IMAGE_SCN_MEM_EXECUTE;
2861 break;
2862 case 's': /* section is sharable */
2863 flags |= IMAGE_SCN_MEM_SHARED;
2864 break;
2865
2866 /* Section Alignment */
2867 #if 0
2868 #define IMAGE_SCN_ALIGN_1BYTES 0x00100000
2869 #define IMAGE_SCN_ALIGN_2BYTES 0x00200000
2870 #define IMAGE_SCN_ALIGN_4BYTES 0x00300000
2871 #define IMAGE_SCN_ALIGN_8BYTES 0x00400000
2872 #define IMAGE_SCN_ALIGN_16BYTES 0x00500000 /* Default alignment if no others are specified. */
2873 #define IMAGE_SCN_ALIGN_32BYTES 0x00600000
2874 #define IMAGE_SCN_ALIGN_64BYTES 0x00700000
2875 #endif
2876
2877 case '0': /* align to byte boundary */
2878 flags |= IMAGE_SCN_ALIGN_1BYTES;
2879 align = 0;
2880 break;
2881 case '1': /* align to halfword boundary */
2882 flags |= IMAGE_SCN_ALIGN_2BYTES;
2883 align = 1;
2884 break;
2885 case '2': /* align to word boundary */
2886 flags |= IMAGE_SCN_ALIGN_4BYTES;
2887 align = 2;
2888 break;
2889 case '3': /* align to doubleword boundary */
2890 flags |= IMAGE_SCN_ALIGN_8BYTES;
2891 align = 3;
2892 break;
2893 case '4': /* align to quadword boundary */
2894 flags |= IMAGE_SCN_ALIGN_16BYTES;
2895 align = 4;
2896 break;
2897 case '5': /* align to 32 byte boundary */
2898 flags |= IMAGE_SCN_ALIGN_32BYTES;
2899 align = 5;
2900 break;
2901 case '6': /* align to 64 byte boundary */
2902 flags |= IMAGE_SCN_ALIGN_64BYTES;
2903 align = 6;
2904 break;
2905
2906 default:
2907 as_warn("unknown section attribute '%c'",
2908 *input_line_pointer);
2909 break;
2910 }
2911 ++input_line_pointer;
2912 }
2913 if (*input_line_pointer == '"')
2914 ++input_line_pointer;
2915 }
2916 }
2917
2918 sec = subseg_new (name, (subsegT) exp);
2919
2920 setCurrentSection(sec);
2921
2922 if (flags != SEC_NO_FLAGS)
2923 {
2924 if (! bfd_set_section_flags (stdoutput, sec, flags))
2925 as_warn ("error setting flags for \"%s\": %s",
2926 bfd_section_name (stdoutput, sec),
2927 bfd_errmsg (bfd_get_error ()));
2928 }
2929
2930
2931 /* FIXME: Make sure the winnt alignment bits get set */
2932 #if 0
2933 #define IMAGE_SCN_ALIGN_1BYTES 0x00100000
2934 #define IMAGE_SCN_ALIGN_2BYTES 0x00200000
2935 #define IMAGE_SCN_ALIGN_4BYTES 0x00300000
2936 #define IMAGE_SCN_ALIGN_8BYTES 0x00400000
2937 #define IMAGE_SCN_ALIGN_16BYTES 0x00500000 /* Default alignment if no others are specified. */
2938 #define IMAGE_SCN_ALIGN_32BYTES 0x00600000
2939 #define IMAGE_SCN_ALIGN_64BYTES 0x00700000
2940 #endif
2941
2942 bfd_set_section_alignment(stdoutput, sec, align);
2943
2944 }
2945
2946 static void
2947 ppc_pe_function (ignore)
2948 int ignore;
2949 {
2950 char *name;
2951 char endc;
2952 char *s;
2953 symbolS *ext_sym;
2954 symbolS *lab_sym;
2955
2956 name = input_line_pointer;
2957 endc = get_symbol_end ();
2958
2959 /* Ignore any [PR] suffix. */
2960 name = ppc_canonicalize_symbol_name (name);
2961 s = strchr (name, '[');
2962 if (s != (char *) NULL
2963 && strcmp (s + 1, "PR]") == 0)
2964 *s = '\0';
2965
2966 ext_sym = symbol_find_or_make (name);
2967
2968 *input_line_pointer = endc;
2969
2970 S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
2971 SF_SET_FUNCTION (ext_sym);
2972 SF_SET_PROCESS (ext_sym);
2973 coff_add_linesym (ext_sym);
2974
2975 demand_empty_rest_of_line ();
2976 }
2977
2978
2979 #endif
2980
2981
2982
2983 \f
2984 #ifdef OBJ_COFF
2985
2986 /* XCOFF specific symbol and file handling. */
2987
2988 /* Canonicalize the symbol name. We use the to force the suffix, if
2989 any, to use square brackets, and to be in upper case. */
2990
2991 char *
2992 ppc_canonicalize_symbol_name (name)
2993 char *name;
2994 {
2995 char *s;
2996
2997 for (s = name; *s != '\0' && *s != '{' && *s != '['; s++)
2998 ;
2999 if (*s != '\0')
3000 {
3001 char brac;
3002
3003 if (*s == '[')
3004 brac = ']';
3005 else
3006 {
3007 *s = '[';
3008 brac = '}';
3009 }
3010
3011 for (s++; *s != '\0' && *s != brac; s++)
3012 if (islower (*s))
3013 *s = toupper (*s);
3014
3015 if (*s == '\0' || s[1] != '\0')
3016 as_bad ("bad symbol suffix");
3017
3018 *s = ']';
3019 }
3020
3021 return name;
3022 }
3023
3024 /* Set the class of a symbol based on the suffix, if any. This is
3025 called whenever a new symbol is created. */
3026
3027 void
3028 ppc_symbol_new_hook (sym)
3029 symbolS *sym;
3030 {
3031 const char *s;
3032
3033 sym->sy_tc.next = NULL;
3034 sym->sy_tc.output = 0;
3035 sym->sy_tc.class = -1;
3036 sym->sy_tc.real_name = NULL;
3037 sym->sy_tc.subseg = 0;
3038 sym->sy_tc.align = 0;
3039 sym->sy_tc.size = NULL;
3040 sym->sy_tc.within = NULL;
3041
3042 s = strchr (S_GET_NAME (sym), '[');
3043 if (s == (const char *) NULL)
3044 {
3045 /* There is no suffix. */
3046 return;
3047 }
3048
3049 ++s;
3050
3051 switch (s[0])
3052 {
3053 case 'B':
3054 if (strcmp (s, "BS]") == 0)
3055 sym->sy_tc.class = XMC_BS;
3056 break;
3057 case 'D':
3058 if (strcmp (s, "DB]") == 0)
3059 sym->sy_tc.class = XMC_DB;
3060 else if (strcmp (s, "DS]") == 0)
3061 sym->sy_tc.class = XMC_DS;
3062 break;
3063 case 'G':
3064 if (strcmp (s, "GL]") == 0)
3065 sym->sy_tc.class = XMC_GL;
3066 break;
3067 case 'P':
3068 if (strcmp (s, "PR]") == 0)
3069 sym->sy_tc.class = XMC_PR;
3070 break;
3071 case 'R':
3072 if (strcmp (s, "RO]") == 0)
3073 sym->sy_tc.class = XMC_RO;
3074 else if (strcmp (s, "RW]") == 0)
3075 sym->sy_tc.class = XMC_RW;
3076 break;
3077 case 'S':
3078 if (strcmp (s, "SV]") == 0)
3079 sym->sy_tc.class = XMC_SV;
3080 break;
3081 case 'T':
3082 if (strcmp (s, "TC]") == 0)
3083 sym->sy_tc.class = XMC_TC;
3084 else if (strcmp (s, "TI]") == 0)
3085 sym->sy_tc.class = XMC_TI;
3086 else if (strcmp (s, "TB]") == 0)
3087 sym->sy_tc.class = XMC_TB;
3088 else if (strcmp (s, "TC0]") == 0 || strcmp (s, "T0]") == 0)
3089 sym->sy_tc.class = XMC_TC0;
3090 break;
3091 case 'U':
3092 if (strcmp (s, "UA]") == 0)
3093 sym->sy_tc.class = XMC_UA;
3094 else if (strcmp (s, "UC]") == 0)
3095 sym->sy_tc.class = XMC_UC;
3096 break;
3097 case 'X':
3098 if (strcmp (s, "XO]") == 0)
3099 sym->sy_tc.class = XMC_XO;
3100 break;
3101 }
3102
3103 if (sym->sy_tc.class == -1)
3104 as_bad ("Unrecognized symbol suffix");
3105 }
3106
3107 /* Set the class of a label based on where it is defined. This
3108 handles symbols without suffixes. Also, move the symbol so that it
3109 follows the csect symbol. */
3110
3111 void
3112 ppc_frob_label (sym)
3113 symbolS *sym;
3114 {
3115 #ifndef TE_PE
3116 if (ppc_current_csect != (symbolS *) NULL)
3117 {
3118 if (sym->sy_tc.class == -1)
3119 sym->sy_tc.class = ppc_current_csect->sy_tc.class;
3120
3121 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
3122 symbol_append (sym, ppc_current_csect->sy_tc.within, &symbol_rootP,
3123 &symbol_lastP);
3124 ppc_current_csect->sy_tc.within = sym;
3125 }
3126 #endif
3127 }
3128
3129 /* Change the name of a symbol just before writing it out. Set the
3130 real name if the .rename pseudo-op was used. Otherwise, remove any
3131 class suffix. Return 1 if the symbol should not be included in the
3132 symbol table. */
3133
3134 int
3135 ppc_frob_symbol (sym)
3136 symbolS *sym;
3137 {
3138 static symbolS *ppc_last_function;
3139 static symbolS *set_end;
3140
3141 /* Discard symbols that should not be included in the output symbol
3142 table. */
3143 if (! sym->sy_used_in_reloc
3144 && ((sym->bsym->flags & BSF_SECTION_SYM) != 0
3145 || (! S_IS_EXTERNAL (sym)
3146 && ! sym->sy_tc.output
3147 && S_GET_STORAGE_CLASS (sym) != C_FILE)))
3148 return 1;
3149
3150 if (sym->sy_tc.real_name != (char *) NULL)
3151 S_SET_NAME (sym, sym->sy_tc.real_name);
3152 else
3153 {
3154 const char *name;
3155 const char *s;
3156
3157 name = S_GET_NAME (sym);
3158 s = strchr (name, '[');
3159 if (s != (char *) NULL)
3160 {
3161 unsigned int len;
3162 char *snew;
3163
3164 len = s - name;
3165 snew = xmalloc (len + 1);
3166 memcpy (snew, name, len);
3167 snew[len] = '\0';
3168
3169 S_SET_NAME (sym, snew);
3170 }
3171 }
3172
3173 if (set_end != (symbolS *) NULL)
3174 {
3175 SA_SET_SYM_ENDNDX (set_end, sym);
3176 set_end = NULL;
3177 }
3178
3179 if (SF_GET_FUNCTION (sym))
3180 {
3181 if (ppc_last_function != (symbolS *) NULL)
3182 as_warn ("two .function pseudo-ops with no intervening .ef");
3183 ppc_last_function = sym;
3184 if (sym->sy_tc.size != (symbolS *) NULL)
3185 {
3186 resolve_symbol_value (sym->sy_tc.size);
3187 SA_SET_SYM_FSIZE (sym, (long) S_GET_VALUE (sym->sy_tc.size));
3188 }
3189 }
3190 else if (S_GET_STORAGE_CLASS (sym) == C_FCN
3191 && strcmp (S_GET_NAME (sym), ".ef") == 0)
3192 {
3193 if (ppc_last_function == (symbolS *) NULL)
3194 as_warn (".ef with no preceding .function");
3195 else
3196 {
3197 set_end = ppc_last_function;
3198 ppc_last_function = NULL;
3199
3200 /* We don't have a C_EFCN symbol, but we need to force the
3201 COFF backend to believe that it has seen one. */
3202 coff_last_function = NULL;
3203 }
3204 }
3205
3206 #ifndef TE_PE
3207 if (! S_IS_EXTERNAL (sym)
3208 && (sym->bsym->flags & BSF_SECTION_SYM) == 0
3209 && S_GET_STORAGE_CLASS (sym) != C_FILE
3210 && S_GET_STORAGE_CLASS (sym) != C_FCN
3211 && S_GET_STORAGE_CLASS (sym) != C_BSTAT
3212 && S_GET_STORAGE_CLASS (sym) != C_ESTAT
3213 && S_GET_SEGMENT (sym) != ppc_coff_debug_section)
3214 S_SET_STORAGE_CLASS (sym, C_HIDEXT);
3215 #endif
3216
3217 if (S_GET_STORAGE_CLASS (sym) == C_EXT
3218 || S_GET_STORAGE_CLASS (sym) == C_HIDEXT)
3219 {
3220 int i;
3221 union internal_auxent *a;
3222
3223 /* Create a csect aux. */
3224 i = S_GET_NUMBER_AUXILIARY (sym);
3225 S_SET_NUMBER_AUXILIARY (sym, i + 1);
3226 a = &coffsymbol (sym->bsym)->native[i + 1].u.auxent;
3227 if (sym->sy_tc.class == XMC_TC0)
3228 {
3229 /* This is the TOC table. */
3230 know (strcmp (S_GET_NAME (sym), "TOC") == 0);
3231 a->x_csect.x_scnlen.l = 0;
3232 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
3233 }
3234 else if (sym->sy_tc.subseg != 0)
3235 {
3236 /* This is a csect symbol. x_scnlen is the size of the
3237 csect. */
3238 if (sym->sy_tc.next == (symbolS *) NULL)
3239 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
3240 S_GET_SEGMENT (sym))
3241 - S_GET_VALUE (sym));
3242 else
3243 {
3244 resolve_symbol_value (sym->sy_tc.next);
3245 a->x_csect.x_scnlen.l = (S_GET_VALUE (sym->sy_tc.next)
3246 - S_GET_VALUE (sym));
3247 }
3248 a->x_csect.x_smtyp = (sym->sy_tc.align << 3) | XTY_SD;
3249 }
3250 else if (S_GET_SEGMENT (sym) == bss_section)
3251 {
3252 /* This is a common symbol. */
3253 a->x_csect.x_scnlen.l = sym->sy_frag->fr_offset;
3254 a->x_csect.x_smtyp = (sym->sy_tc.align << 3) | XTY_CM;
3255 if (S_IS_EXTERNAL (sym))
3256 sym->sy_tc.class = XMC_RW;
3257 else
3258 sym->sy_tc.class = XMC_BS;
3259 }
3260 else if (! S_IS_DEFINED (sym))
3261 {
3262 /* This is an external symbol. */
3263 a->x_csect.x_scnlen.l = 0;
3264 a->x_csect.x_smtyp = XTY_ER;
3265 }
3266 else if (sym->sy_tc.class == XMC_TC)
3267 {
3268 symbolS *next;
3269
3270 /* This is a TOC definition. x_scnlen is the size of the
3271 TOC entry. */
3272 next = symbol_next (sym);
3273 while (next->sy_tc.class == XMC_TC0)
3274 next = symbol_next (next);
3275 if (next == (symbolS *) NULL
3276 || next->sy_tc.class != XMC_TC)
3277 {
3278 if (ppc_after_toc_frag == (fragS *) NULL)
3279 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
3280 data_section)
3281 - S_GET_VALUE (sym));
3282 else
3283 a->x_csect.x_scnlen.l = (ppc_after_toc_frag->fr_address
3284 - S_GET_VALUE (sym));
3285 }
3286 else
3287 {
3288 resolve_symbol_value (next);
3289 a->x_csect.x_scnlen.l = (S_GET_VALUE (next)
3290 - S_GET_VALUE (sym));
3291 }
3292 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
3293 }
3294
3295 #ifdef TE_PE
3296 else if (S_GET_SEGMENT (sym) == pdata_section)
3297 {
3298 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
3299 S_GET_SEGMENT (sym))
3300 - S_GET_VALUE (sym));
3301 }
3302 else if (S_GET_SEGMENT (sym) == rdata_section)
3303 {
3304 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
3305 S_GET_SEGMENT (sym))
3306 - S_GET_VALUE (sym));
3307 }
3308 else if (S_GET_SEGMENT (sym) == reldata_section)
3309 {
3310 a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput,
3311 S_GET_SEGMENT (sym))
3312 - S_GET_VALUE (sym));
3313 }
3314 #endif
3315 else
3316 {
3317 #ifndef TE_PE
3318 symbolS *csect;
3319
3320 /* This is a normal symbol definition. x_scnlen is the
3321 symbol index of the containing csect. */
3322 if (S_GET_SEGMENT (sym) == text_section)
3323 csect = ppc_text_csects;
3324 else if (S_GET_SEGMENT (sym) == data_section)
3325 csect = ppc_data_csects;
3326 else
3327 abort ();
3328
3329 /* Skip the initial dummy symbol. */
3330 csect = csect->sy_tc.next;
3331
3332 if (csect == (symbolS *) NULL)
3333 a->x_csect.x_scnlen.l = 0;
3334 else
3335 {
3336 while (csect->sy_tc.next != (symbolS *) NULL)
3337 {
3338 resolve_symbol_value (csect->sy_tc.next);
3339 if (S_GET_VALUE (csect->sy_tc.next) > S_GET_VALUE (sym))
3340 break;
3341 csect = csect->sy_tc.next;
3342 }
3343
3344 a->x_csect.x_scnlen.p = coffsymbol (csect->bsym)->native;
3345 coffsymbol (sym->bsym)->native[i + 1].fix_scnlen = 1;
3346 }
3347 a->x_csect.x_smtyp = XTY_LD;
3348 #endif
3349 }
3350
3351 a->x_csect.x_parmhash = 0;
3352 a->x_csect.x_snhash = 0;
3353 if (sym->sy_tc.class == -1)
3354 a->x_csect.x_smclas = XMC_PR;
3355 else
3356 a->x_csect.x_smclas = sym->sy_tc.class;
3357 a->x_csect.x_stab = 0;
3358 a->x_csect.x_snstab = 0;
3359 }
3360 else if (S_GET_STORAGE_CLASS (sym) == C_BSTAT)
3361 {
3362 /* We want the value to be the symbol index of the referenced
3363 csect symbol. BFD will do that for us if we set the right
3364 flags. */
3365 S_SET_VALUE (sym,
3366 (valueT) coffsymbol (sym->sy_tc.within->bsym)->native);
3367 coffsymbol (sym->bsym)->native->fix_value = 1;
3368 }
3369 else if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
3370 {
3371 symbolS *block;
3372 symbolS *csect;
3373
3374 /* The value is the offset from the enclosing csect. */
3375 block = sym->sy_tc.within;
3376 csect = block->sy_tc.within;
3377 resolve_symbol_value (csect);
3378 S_SET_VALUE (sym, S_GET_VALUE (sym) - S_GET_VALUE (csect));
3379 }
3380
3381 return 0;
3382 }
3383
3384 /* Set the VMA for a section. This is called on all the sections in
3385 turn. */
3386
3387 void
3388 ppc_frob_section (sec)
3389 asection *sec;
3390 {
3391 #ifndef TE_PE
3392 static bfd_size_type vma = 0;
3393
3394 bfd_set_section_vma (stdoutput, sec, vma);
3395 vma += bfd_section_size (stdoutput, sec);
3396 #endif
3397 }
3398
3399 /* Adjust the file by adding a .debug section if needed. */
3400
3401 void
3402 ppc_frob_file ()
3403 {
3404 if (ppc_debug_name_section_size > 0)
3405 {
3406 asection *sec;
3407
3408 sec = bfd_make_section (stdoutput, ".debug");
3409 if (sec == (asection *) NULL
3410 || ! bfd_set_section_size (stdoutput, sec,
3411 ppc_debug_name_section_size)
3412 || ! bfd_set_section_flags (stdoutput, sec,
3413 SEC_HAS_CONTENTS | SEC_LOAD))
3414 as_fatal ("can't make .debug section");
3415 }
3416 }
3417
3418 #endif /* OBJ_COFF */
3419 \f
3420 /* Turn a string in input_line_pointer into a floating point constant
3421 of type type, and store the appropriate bytes in *litp. The number
3422 of LITTLENUMS emitted is stored in *sizep . An error message is
3423 returned, or NULL on OK. */
3424
3425 char *
3426 md_atof (type, litp, sizep)
3427 int type;
3428 char *litp;
3429 int *sizep;
3430 {
3431 int prec;
3432 LITTLENUM_TYPE words[4];
3433 char *t;
3434 int i;
3435
3436 switch (type)
3437 {
3438 case 'f':
3439 prec = 2;
3440 break;
3441
3442 case 'd':
3443 prec = 4;
3444 break;
3445
3446 default:
3447 *sizep = 0;
3448 return "bad call to md_atof";
3449 }
3450
3451 t = atof_ieee (input_line_pointer, type, words);
3452 if (t)
3453 input_line_pointer = t;
3454
3455 *sizep = prec * 2;
3456
3457 if (target_big_endian)
3458 {
3459 for (i = 0; i < prec; i++)
3460 {
3461 md_number_to_chars (litp, (valueT) words[i], 2);
3462 litp += 2;
3463 }
3464 }
3465 else
3466 {
3467 for (i = prec - 1; i >= 0; i--)
3468 {
3469 md_number_to_chars (litp, (valueT) words[i], 2);
3470 litp += 2;
3471 }
3472 }
3473
3474 return NULL;
3475 }
3476
3477 /* Write a value out to the object file, using the appropriate
3478 endianness. */
3479
3480 void
3481 md_number_to_chars (buf, val, n)
3482 char *buf;
3483 valueT val;
3484 int n;
3485 {
3486 if (target_big_endian)
3487 number_to_chars_bigendian (buf, val, n);
3488 else
3489 number_to_chars_littleendian (buf, val, n);
3490 }
3491
3492 /* Align a section (I don't know why this is machine dependent). */
3493
3494 valueT
3495 md_section_align (seg, addr)
3496 asection *seg;
3497 valueT addr;
3498 {
3499 int align = bfd_get_section_alignment (stdoutput, seg);
3500
3501 return ((addr + (1 << align) - 1) & (-1 << align));
3502 }
3503
3504 /* We don't have any form of relaxing. */
3505
3506 int
3507 md_estimate_size_before_relax (fragp, seg)
3508 fragS *fragp;
3509 asection *seg;
3510 {
3511 abort ();
3512 }
3513
3514 /* Convert a machine dependent frag. We never generate these. */
3515
3516 void
3517 md_convert_frag (abfd, sec, fragp)
3518 bfd *abfd;
3519 asection *sec;
3520 fragS *fragp;
3521 {
3522 abort ();
3523 }
3524
3525 /* We have no need to default values of symbols. */
3526
3527 /*ARGSUSED*/
3528 symbolS *
3529 md_undefined_symbol (name)
3530 char *name;
3531 {
3532 return 0;
3533 }
3534 \f
3535 /* Functions concerning relocs. */
3536
3537 /* The location from which a PC relative jump should be calculated,
3538 given a PC relative reloc. */
3539
3540 long
3541 md_pcrel_from (fixp)
3542 fixS *fixp;
3543 {
3544 #ifdef OBJ_ELF
3545 if (fixp->fx_addsy != (symbolS *) NULL
3546 && (! S_IS_DEFINED (fixp->fx_addsy)
3547 || TC_FORCE_RELOCATION (fixp)))
3548 return 0;
3549 #endif
3550
3551 return fixp->fx_frag->fr_address + fixp->fx_where;
3552 }
3553
3554 #ifdef OBJ_COFF
3555
3556 /* This is called to see whether a fixup should be adjusted to use a
3557 section symbol. We take the opportunity to change a fixup against
3558 a symbol in the TOC subsegment into a reloc against the
3559 corresponding .tc symbol. */
3560
3561 int
3562 ppc_fix_adjustable (fix)
3563 fixS *fix;
3564 {
3565 valueT val;
3566
3567 #ifdef TE_PE
3568 /* FIXME: Certainly the toc stuff gets us into trouble, and should
3569 not be executed. Not sure about the reloc adjustments. */
3570 return 0;
3571 #endif
3572
3573 resolve_symbol_value (fix->fx_addsy);
3574 val = S_GET_VALUE (fix->fx_addsy);
3575 if (ppc_toc_csect != (symbolS *) NULL
3576 && fix->fx_addsy != (symbolS *) NULL
3577 && fix->fx_addsy != ppc_toc_csect
3578 && S_GET_SEGMENT (fix->fx_addsy) == data_section
3579 && val >= ppc_toc_frag->fr_address
3580 && (ppc_after_toc_frag == (fragS *) NULL
3581 || val < ppc_after_toc_frag->fr_address))
3582 {
3583 symbolS *sy;
3584
3585 for (sy = symbol_next (ppc_toc_csect);
3586 sy != (symbolS *) NULL;
3587 sy = symbol_next (sy))
3588 {
3589 if (sy->sy_tc.class == XMC_TC0)
3590 continue;
3591 if (sy->sy_tc.class != XMC_TC)
3592 break;
3593 resolve_symbol_value (sy);
3594 if (val == S_GET_VALUE (sy))
3595 {
3596 fix->fx_addsy = sy;
3597 fix->fx_addnumber = val - ppc_toc_frag->fr_address;
3598 return 0;
3599 }
3600 }
3601
3602 as_bad_where (fix->fx_file, fix->fx_line,
3603 "symbol in .toc does not match any .tc");
3604 }
3605
3606 /* Possibly adjust the reloc to be against the csect. */
3607 if (fix->fx_addsy != (symbolS *) NULL
3608 && fix->fx_addsy->sy_tc.subseg == 0
3609 && fix->fx_addsy->sy_tc.class != XMC_TC0
3610 && fix->fx_addsy->sy_tc.class != XMC_TC
3611 && S_GET_SEGMENT (fix->fx_addsy) != bss_section)
3612 {
3613 symbolS *csect;
3614
3615 if (S_GET_SEGMENT (fix->fx_addsy) == text_section)
3616 csect = ppc_text_csects;
3617 else if (S_GET_SEGMENT (fix->fx_addsy) == data_section)
3618 csect = ppc_data_csects;
3619 else
3620 abort ();
3621
3622 /* Skip the initial dummy symbol. */
3623 csect = csect->sy_tc.next;
3624
3625 if (csect != (symbolS *) NULL)
3626 {
3627 while (csect->sy_tc.next != (symbolS *) NULL
3628 && (csect->sy_tc.next->sy_frag->fr_address
3629 <= fix->fx_addsy->sy_frag->fr_address))
3630 csect = csect->sy_tc.next;
3631
3632 fix->fx_offset += (S_GET_VALUE (fix->fx_addsy)
3633 - csect->sy_frag->fr_address);
3634 fix->fx_addsy = csect;
3635 }
3636 }
3637
3638 /* Adjust a reloc against a .lcomm symbol to be against the base
3639 .lcomm. */
3640 if (fix->fx_addsy != (symbolS *) NULL
3641 && S_GET_SEGMENT (fix->fx_addsy) == bss_section
3642 && ! S_IS_EXTERNAL (fix->fx_addsy))
3643 {
3644 resolve_symbol_value (fix->fx_addsy->sy_frag->fr_symbol);
3645 fix->fx_offset += (S_GET_VALUE (fix->fx_addsy)
3646 - S_GET_VALUE (fix->fx_addsy->sy_frag->fr_symbol));
3647 fix->fx_addsy = fix->fx_addsy->sy_frag->fr_symbol;
3648 }
3649
3650 return 0;
3651 }
3652
3653 #endif
3654
3655 /* See whether a symbol is in the TOC section. */
3656
3657 static int
3658 ppc_is_toc_sym (sym)
3659 symbolS *sym;
3660 {
3661 #ifdef OBJ_COFF
3662 return sym->sy_tc.class == XMC_TC;
3663 #else
3664 return strcmp (segment_name (S_GET_SEGMENT (sym)), ".got") == 0;
3665 #endif
3666 }
3667
3668 /* Apply a fixup to the object code. This is called for all the
3669 fixups we generated by the call to fix_new_exp, above. In the call
3670 above we used a reloc code which was the largest legal reloc code
3671 plus the operand index. Here we undo that to recover the operand
3672 index. At this point all symbol values should be fully resolved,
3673 and we attempt to completely resolve the reloc. If we can not do
3674 that, we determine the correct reloc code and put it back in the
3675 fixup. */
3676
3677 int
3678 md_apply_fix3 (fixp, valuep, seg)
3679 fixS *fixp;
3680 valueT *valuep;
3681 segT seg;
3682 {
3683 valueT value;
3684
3685 /* FIXME FIXME FIXME: The value we are passed in *valuep includes
3686 the symbol values. Since we are using BFD_ASSEMBLER, if we are
3687 doing this relocation the code in write.c is going to call
3688 bfd_perform_relocation, which is also going to use the symbol
3689 value. That means that if the reloc is fully resolved we want to
3690 use *valuep since bfd_perform_relocation is not being used.
3691 However, if the reloc is not fully resolved we do not want to use
3692 *valuep, and must use fx_offset instead. However, if the reloc
3693 is PC relative, we do want to use *valuep since it includes the
3694 result of md_pcrel_from. This is confusing. */
3695
3696 if (fixp->fx_addsy == (symbolS *) NULL)
3697 {
3698 value = *valuep;
3699 fixp->fx_done = 1;
3700 }
3701 else if (fixp->fx_pcrel)
3702 value = *valuep;
3703 else
3704 {
3705 value = fixp->fx_offset;
3706 if (fixp->fx_subsy != (symbolS *) NULL)
3707 {
3708 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
3709 value -= S_GET_VALUE (fixp->fx_subsy);
3710 else
3711 {
3712 /* We can't actually support subtracting a symbol. */
3713 as_bad_where (fixp->fx_file, fixp->fx_line,
3714 "expression too complex");
3715 }
3716 }
3717 }
3718
3719 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
3720 {
3721 int opindex;
3722 const struct powerpc_operand *operand;
3723 char *where;
3724 unsigned long insn;
3725
3726 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
3727
3728 operand = &powerpc_operands[opindex];
3729
3730 #ifdef OBJ_COFF
3731 /* It appears that an instruction like
3732 l 9,LC..1(30)
3733 when LC..1 is not a TOC symbol does not generate a reloc. It
3734 uses the offset of LC..1 within its csect. However, .long
3735 LC..1 will generate a reloc. I can't find any documentation
3736 on how these cases are to be distinguished, so this is a wild
3737 guess. These cases are generated by gcc -mminimal-toc. */
3738 if ((operand->flags & PPC_OPERAND_PARENS) != 0
3739 && operand->bits == 16
3740 && operand->shift == 0
3741 && operand->insert == NULL
3742 && fixp->fx_addsy != NULL
3743 && fixp->fx_addsy->sy_tc.subseg != 0
3744 && fixp->fx_addsy->sy_tc.class != XMC_TC
3745 && fixp->fx_addsy->sy_tc.class != XMC_TC0
3746 && S_GET_SEGMENT (fixp->fx_addsy) != bss_section)
3747 {
3748 value = fixp->fx_offset;
3749 fixp->fx_done = 1;
3750 }
3751 #endif
3752
3753 /* Fetch the instruction, insert the fully resolved operand
3754 value, and stuff the instruction back again. */
3755 where = fixp->fx_frag->fr_literal + fixp->fx_where;
3756 if (target_big_endian)
3757 insn = bfd_getb32 ((unsigned char *) where);
3758 else
3759 insn = bfd_getl32 ((unsigned char *) where);
3760 insn = ppc_insert_operand (insn, operand, (offsetT) value,
3761 fixp->fx_file, fixp->fx_line);
3762 if (target_big_endian)
3763 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
3764 else
3765 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
3766
3767 if (fixp->fx_done)
3768 {
3769 /* Nothing else to do here. */
3770 return 1;
3771 }
3772
3773 /* Determine a BFD reloc value based on the operand information.
3774 We are only prepared to turn a few of the operands into
3775 relocs.
3776 FIXME: We need to handle the DS field at the very least.
3777 FIXME: Selecting the reloc type is a bit haphazard; perhaps
3778 there should be a new field in the operand table. */
3779 if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3780 && operand->bits == 26
3781 && operand->shift == 0)
3782 fixp->fx_r_type = BFD_RELOC_PPC_B26;
3783 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3784 && operand->bits == 16
3785 && operand->shift == 0)
3786 fixp->fx_r_type = BFD_RELOC_PPC_B16;
3787 else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0
3788 && operand->bits == 26
3789 && operand->shift == 0)
3790 fixp->fx_r_type = BFD_RELOC_PPC_BA26;
3791 else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0
3792 && operand->bits == 16
3793 && operand->shift == 0)
3794 fixp->fx_r_type = BFD_RELOC_PPC_BA16;
3795 else if ((operand->flags & PPC_OPERAND_PARENS) != 0
3796 && operand->bits == 16
3797 && operand->shift == 0
3798 && operand->insert == NULL
3799 && fixp->fx_addsy != NULL
3800 && ppc_is_toc_sym (fixp->fx_addsy))
3801 {
3802 fixp->fx_size = 2;
3803 if (target_big_endian)
3804 fixp->fx_where += 2;
3805 fixp->fx_r_type = BFD_RELOC_PPC_TOC16;
3806 }
3807 else
3808 {
3809 as_bad_where (fixp->fx_file, fixp->fx_line,
3810 "unresolved expression that must be resolved");
3811 fixp->fx_done = 1;
3812 return 1;
3813 }
3814 }
3815 else
3816 {
3817 #ifdef OBJ_ELF
3818 ppc_elf_validate_fix (fixp, seg);
3819 #endif
3820 switch (fixp->fx_r_type)
3821 {
3822 case BFD_RELOC_32:
3823 case BFD_RELOC_CTOR:
3824 if (fixp->fx_pcrel)
3825 {
3826 fixp->fx_r_type = BFD_RELOC_32_PCREL;
3827 value += fixp->fx_frag->fr_address + fixp->fx_where;
3828 } /* fall through */
3829
3830 case BFD_RELOC_32_PCREL:
3831 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
3832 value, 4);
3833 break;
3834
3835 case BFD_RELOC_LO16:
3836 case BFD_RELOC_HI16:
3837 case BFD_RELOC_HI16_S:
3838 case BFD_RELOC_PPC_TOC16:
3839 case BFD_RELOC_16:
3840 case BFD_RELOC_GPREL16:
3841 if (fixp->fx_pcrel)
3842 abort ();
3843
3844 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
3845 value, 2);
3846 break;
3847
3848 case BFD_RELOC_8:
3849 if (fixp->fx_pcrel)
3850 abort ();
3851
3852 md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where,
3853 value, 1);
3854 break;
3855
3856 default:
3857 abort ();
3858 }
3859 }
3860
3861 #ifdef OBJ_ELF
3862 fixp->fx_addnumber = value;
3863 #else
3864 if (fixp->fx_r_type != BFD_RELOC_PPC_TOC16)
3865 fixp->fx_addnumber = 0;
3866 else
3867 {
3868 #ifdef TE_PE
3869 fixp->fx_addnumber = 0;
3870 #else
3871 /* We want to use the offset within the data segment of the
3872 symbol, not the actual VMA of the symbol. */
3873 fixp->fx_addnumber =
3874 - bfd_get_section_vma (stdoutput, S_GET_SEGMENT (fixp->fx_addsy));
3875 #endif
3876 }
3877 #endif
3878
3879 return 1;
3880 }
3881
3882 /* Generate a reloc for a fixup. */
3883
3884 arelent *
3885 tc_gen_reloc (seg, fixp)
3886 asection *seg;
3887 fixS *fixp;
3888 {
3889 arelent *reloc;
3890
3891 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
3892
3893 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
3894 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
3895 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
3896 if (reloc->howto == (reloc_howto_type *) NULL)
3897 {
3898 as_bad_where (fixp->fx_file, fixp->fx_line,
3899 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
3900 return NULL;
3901 }
3902 reloc->addend = fixp->fx_addnumber;
3903
3904 return reloc;
3905 }