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