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