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