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