gdb/testsuite: fix gdb.trace/signal.exp on x86
[binutils-gdb/blckswan.git] / gas / config / tc-ppc.c
blob4d789fd16a9a4af8ce4b4894c235273da09d67ff
1 /* tc-ppc.c -- Assemble for the PowerPC or POWER (RS/6000)
2 Copyright (C) 1994-2022 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "as.h"
23 #include "safe-ctype.h"
24 #include "subsegs.h"
25 #include "dw2gencfi.h"
26 #include "opcode/ppc.h"
28 #ifdef OBJ_ELF
29 #include "elf/ppc.h"
30 #include "elf/ppc64.h"
31 #include "dwarf2dbg.h"
32 #endif
34 #ifdef OBJ_XCOFF
35 #include "coff/xcoff.h"
36 #include "libxcoff.h"
37 #endif
39 /* This is the assembler for the PowerPC or POWER (RS/6000) chips. */
41 /* Tell the main code what the endianness is. */
42 extern int target_big_endian;
44 /* Whether or not, we've set target_big_endian. */
45 static int set_target_endian = 0;
47 /* Whether to use user friendly register names. */
48 #ifndef TARGET_REG_NAMES_P
49 #define TARGET_REG_NAMES_P false
50 #endif
52 /* Macros for calculating LO, HI, HA, HIGHER, HIGHERA, HIGHEST,
53 HIGHESTA. */
55 /* #lo(value) denotes the least significant 16 bits of the indicated. */
56 #define PPC_LO(v) ((v) & 0xffff)
58 /* #hi(value) denotes bits 16 through 31 of the indicated value. */
59 #define PPC_HI(v) (((v) >> 16) & 0xffff)
61 /* #ha(value) denotes the high adjusted value: bits 16 through 31 of
62 the indicated value, compensating for #lo() being treated as a
63 signed number. */
64 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
66 /* #higher(value) denotes bits 32 through 47 of the indicated value. */
67 #define PPC_HIGHER(v) (((v) >> 16 >> 16) & 0xffff)
69 /* #highera(value) denotes bits 32 through 47 of the indicated value,
70 compensating for #lo() being treated as a signed number. */
71 #define PPC_HIGHERA(v) PPC_HIGHER ((v) + 0x8000)
73 /* #highest(value) denotes bits 48 through 63 of the indicated value. */
74 #define PPC_HIGHEST(v) (((v) >> 24 >> 24) & 0xffff)
76 /* #highesta(value) denotes bits 48 through 63 of the indicated value,
77 compensating for #lo being treated as a signed number. */
78 #define PPC_HIGHESTA(v) PPC_HIGHEST ((v) + 0x8000)
80 #define SEX16(val) (((val) ^ 0x8000) - 0x8000)
82 /* For the time being on ppc64, don't report overflow on @h and @ha
83 applied to constants. */
84 #define REPORT_OVERFLOW_HI 0
86 static bool reg_names_p = TARGET_REG_NAMES_P;
88 static void ppc_byte (int);
90 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
91 static void ppc_tc (int);
92 static void ppc_machine (int);
93 #endif
95 #ifdef OBJ_XCOFF
96 static void ppc_comm (int);
97 static void ppc_bb (int);
98 static void ppc_bc (int);
99 static void ppc_bf (int);
100 static void ppc_biei (int);
101 static void ppc_bs (int);
102 static void ppc_eb (int);
103 static void ppc_ec (int);
104 static void ppc_ef (int);
105 static void ppc_es (int);
106 static void ppc_csect (int);
107 static void ppc_dwsect (int);
108 static void ppc_change_csect (symbolS *, offsetT);
109 static void ppc_file (int);
110 static void ppc_function (int);
111 static void ppc_extern (int);
112 static void ppc_globl (int);
113 static void ppc_lglobl (int);
114 static void ppc_ref (int);
115 static void ppc_section (int);
116 static void ppc_named_section (int);
117 static void ppc_stabx (int);
118 static void ppc_rename (int);
119 static void ppc_toc (int);
120 static void ppc_xcoff_cons (int);
121 static void ppc_vbyte (int);
122 static void ppc_weak (int);
123 static void ppc_GNU_visibility (int);
124 #endif
126 #ifdef OBJ_ELF
127 static void ppc_elf_rdata (int);
128 static void ppc_elf_lcomm (int);
129 static void ppc_elf_localentry (int);
130 static void ppc_elf_abiversion (int);
131 static void ppc_elf_gnu_attribute (int);
132 #endif
134 /* Generic assembler global variables which must be defined by all
135 targets. */
137 #ifdef OBJ_ELF
138 /* This string holds the chars that always start a comment. If the
139 pre-processor is disabled, these aren't very useful. The macro
140 tc_comment_chars points to this. We use this, rather than the
141 usual comment_chars, so that we can switch for Solaris conventions. */
142 static const char ppc_solaris_comment_chars[] = "#!";
143 static const char ppc_eabi_comment_chars[] = "#";
145 #ifdef TARGET_SOLARIS_COMMENT
146 const char *ppc_comment_chars = ppc_solaris_comment_chars;
147 #else
148 const char *ppc_comment_chars = ppc_eabi_comment_chars;
149 #endif
150 #else
151 const char comment_chars[] = "#";
152 #endif
154 /* Characters which start a comment at the beginning of a line. */
155 const char line_comment_chars[] = "#";
157 /* Characters which may be used to separate multiple commands on a
158 single line. */
159 const char line_separator_chars[] = ";";
161 /* Characters which are used to indicate an exponent in a floating
162 point number. */
163 const char EXP_CHARS[] = "eE";
165 /* Characters which mean that a number is a floating point constant,
166 as in 0d1.0. */
167 const char FLT_CHARS[] = "dD";
169 /* Anything that can start an operand needs to be mentioned here,
170 to stop the input scrubber eating whitespace. */
171 const char ppc_symbol_chars[] = "%[";
173 /* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
174 int ppc_cie_data_alignment;
176 /* The dwarf2 minimum instruction length. */
177 int ppc_dwarf2_line_min_insn_length;
179 /* More than this number of nops in an alignment op gets a branch
180 instead. */
181 unsigned long nop_limit = 4;
183 /* The type of processor we are assembling for. This is one or more
184 of the PPC_OPCODE flags defined in opcode/ppc.h. */
185 ppc_cpu_t ppc_cpu = 0;
186 ppc_cpu_t sticky = 0;
188 /* Value for ELF e_flags EF_PPC64_ABI. */
189 unsigned int ppc_abiversion = 0;
191 #ifdef OBJ_ELF
192 /* Flags set on encountering toc relocs. */
193 static enum {
194 has_large_toc_reloc = 1,
195 has_small_toc_reloc = 2
196 } toc_reloc_types;
197 #endif
199 /* Warn on emitting data to code sections. */
200 int warn_476;
201 uint64_t last_insn;
202 segT last_seg;
203 subsegT last_subseg;
205 /* The target specific pseudo-ops which we support. */
207 const pseudo_typeS md_pseudo_table[] =
209 /* Pseudo-ops which must be overridden. */
210 { "byte", ppc_byte, 0 },
212 #ifdef OBJ_XCOFF
213 /* Pseudo-ops specific to the RS/6000 XCOFF format. Some of these
214 legitimately belong in the obj-*.c file. However, XCOFF is based
215 on COFF, and is only implemented for the RS/6000. We just use
216 obj-coff.c, and add what we need here. */
217 { "comm", ppc_comm, 0 },
218 { "lcomm", ppc_comm, 1 },
219 { "bb", ppc_bb, 0 },
220 { "bc", ppc_bc, 0 },
221 { "bf", ppc_bf, 0 },
222 { "bi", ppc_biei, 0 },
223 { "bs", ppc_bs, 0 },
224 { "csect", ppc_csect, 0 },
225 { "dwsect", ppc_dwsect, 0 },
226 { "data", ppc_section, 'd' },
227 { "eb", ppc_eb, 0 },
228 { "ec", ppc_ec, 0 },
229 { "ef", ppc_ef, 0 },
230 { "ei", ppc_biei, 1 },
231 { "es", ppc_es, 0 },
232 { "extern", ppc_extern, 0 },
233 { "file", ppc_file, 0 },
234 { "function", ppc_function, 0 },
235 { "globl", ppc_globl, 0 },
236 { "lglobl", ppc_lglobl, 0 },
237 { "ref", ppc_ref, 0 },
238 { "rename", ppc_rename, 0 },
239 { "section", ppc_named_section, 0 },
240 { "stabx", ppc_stabx, 0 },
241 { "text", ppc_section, 't' },
242 { "toc", ppc_toc, 0 },
243 { "long", ppc_xcoff_cons, 2 },
244 { "llong", ppc_xcoff_cons, 3 },
245 { "word", ppc_xcoff_cons, 1 },
246 { "short", ppc_xcoff_cons, 1 },
247 { "vbyte", ppc_vbyte, 0 },
248 { "weak", ppc_weak, 0 },
250 /* Enable GNU syntax for symbol visibility. */
251 {"internal", ppc_GNU_visibility, SYM_V_INTERNAL},
252 {"hidden", ppc_GNU_visibility, SYM_V_HIDDEN},
253 {"protected", ppc_GNU_visibility, SYM_V_PROTECTED},
254 #endif
256 #ifdef OBJ_ELF
257 { "llong", cons, 8 },
258 { "rdata", ppc_elf_rdata, 0 },
259 { "rodata", ppc_elf_rdata, 0 },
260 { "lcomm", ppc_elf_lcomm, 0 },
261 { "localentry", ppc_elf_localentry, 0 },
262 { "abiversion", ppc_elf_abiversion, 0 },
263 { "gnu_attribute", ppc_elf_gnu_attribute, 0},
264 #endif
266 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
267 { "tc", ppc_tc, 0 },
268 { "machine", ppc_machine, 0 },
269 #endif
271 { NULL, NULL, 0 }
275 /* Predefined register names if -mregnames (or default for Windows NT).
276 In general, there are lots of them, in an attempt to be compatible
277 with a number of other Windows NT assemblers. */
279 /* Structure to hold information about predefined registers. */
280 struct pd_reg
282 const char *name;
283 unsigned short value;
284 unsigned short flags;
287 /* List of registers that are pre-defined:
289 Each general register has predefined names of the form:
290 1. r<reg_num> which has the value <reg_num>.
291 2. r.<reg_num> which has the value <reg_num>.
293 Each floating point register has predefined names of the form:
294 1. f<reg_num> which has the value <reg_num>.
295 2. f.<reg_num> which has the value <reg_num>.
297 Each vector unit register has predefined names of the form:
298 1. v<reg_num> which has the value <reg_num>.
299 2. v.<reg_num> which has the value <reg_num>.
301 Each condition register has predefined names of the form:
302 1. cr<reg_num> which has the value <reg_num>.
303 2. cr.<reg_num> which has the value <reg_num>.
305 There are individual registers as well:
306 sp or r.sp has the value 1
307 rtoc or r.toc has the value 2
308 xer has the value 1
309 lr has the value 8
310 ctr has the value 9
311 dar has the value 19
312 dsisr has the value 18
313 dec has the value 22
314 sdr1 has the value 25
315 srr0 has the value 26
316 srr1 has the value 27
318 The table is sorted. Suitable for searching by a binary search. */
320 static const struct pd_reg pre_defined_registers[] =
322 /* VSX accumulators. */
323 { "a0", 0, PPC_OPERAND_ACC },
324 { "a1", 1, PPC_OPERAND_ACC },
325 { "a2", 2, PPC_OPERAND_ACC },
326 { "a3", 3, PPC_OPERAND_ACC },
327 { "a4", 4, PPC_OPERAND_ACC },
328 { "a5", 5, PPC_OPERAND_ACC },
329 { "a6", 6, PPC_OPERAND_ACC },
330 { "a7", 7, PPC_OPERAND_ACC },
332 /* Condition Registers */
333 { "cr.0", 0, PPC_OPERAND_CR_REG },
334 { "cr.1", 1, PPC_OPERAND_CR_REG },
335 { "cr.2", 2, PPC_OPERAND_CR_REG },
336 { "cr.3", 3, PPC_OPERAND_CR_REG },
337 { "cr.4", 4, PPC_OPERAND_CR_REG },
338 { "cr.5", 5, PPC_OPERAND_CR_REG },
339 { "cr.6", 6, PPC_OPERAND_CR_REG },
340 { "cr.7", 7, PPC_OPERAND_CR_REG },
342 { "cr0", 0, PPC_OPERAND_CR_REG },
343 { "cr1", 1, PPC_OPERAND_CR_REG },
344 { "cr2", 2, PPC_OPERAND_CR_REG },
345 { "cr3", 3, PPC_OPERAND_CR_REG },
346 { "cr4", 4, PPC_OPERAND_CR_REG },
347 { "cr5", 5, PPC_OPERAND_CR_REG },
348 { "cr6", 6, PPC_OPERAND_CR_REG },
349 { "cr7", 7, PPC_OPERAND_CR_REG },
351 { "ctr", 9, PPC_OPERAND_SPR },
352 { "dar", 19, PPC_OPERAND_SPR },
353 { "dec", 22, PPC_OPERAND_SPR },
354 { "dsisr", 18, PPC_OPERAND_SPR },
356 /* Floating point registers */
357 { "f.0", 0, PPC_OPERAND_FPR },
358 { "f.1", 1, PPC_OPERAND_FPR },
359 { "f.10", 10, PPC_OPERAND_FPR },
360 { "f.11", 11, PPC_OPERAND_FPR },
361 { "f.12", 12, PPC_OPERAND_FPR },
362 { "f.13", 13, PPC_OPERAND_FPR },
363 { "f.14", 14, PPC_OPERAND_FPR },
364 { "f.15", 15, PPC_OPERAND_FPR },
365 { "f.16", 16, PPC_OPERAND_FPR },
366 { "f.17", 17, PPC_OPERAND_FPR },
367 { "f.18", 18, PPC_OPERAND_FPR },
368 { "f.19", 19, PPC_OPERAND_FPR },
369 { "f.2", 2, PPC_OPERAND_FPR },
370 { "f.20", 20, PPC_OPERAND_FPR },
371 { "f.21", 21, PPC_OPERAND_FPR },
372 { "f.22", 22, PPC_OPERAND_FPR },
373 { "f.23", 23, PPC_OPERAND_FPR },
374 { "f.24", 24, PPC_OPERAND_FPR },
375 { "f.25", 25, PPC_OPERAND_FPR },
376 { "f.26", 26, PPC_OPERAND_FPR },
377 { "f.27", 27, PPC_OPERAND_FPR },
378 { "f.28", 28, PPC_OPERAND_FPR },
379 { "f.29", 29, PPC_OPERAND_FPR },
380 { "f.3", 3, PPC_OPERAND_FPR },
381 { "f.30", 30, PPC_OPERAND_FPR },
382 { "f.31", 31, PPC_OPERAND_FPR },
383 { "f.32", 32, PPC_OPERAND_VSR },
384 { "f.33", 33, PPC_OPERAND_VSR },
385 { "f.34", 34, PPC_OPERAND_VSR },
386 { "f.35", 35, PPC_OPERAND_VSR },
387 { "f.36", 36, PPC_OPERAND_VSR },
388 { "f.37", 37, PPC_OPERAND_VSR },
389 { "f.38", 38, PPC_OPERAND_VSR },
390 { "f.39", 39, PPC_OPERAND_VSR },
391 { "f.4", 4, PPC_OPERAND_FPR },
392 { "f.40", 40, PPC_OPERAND_VSR },
393 { "f.41", 41, PPC_OPERAND_VSR },
394 { "f.42", 42, PPC_OPERAND_VSR },
395 { "f.43", 43, PPC_OPERAND_VSR },
396 { "f.44", 44, PPC_OPERAND_VSR },
397 { "f.45", 45, PPC_OPERAND_VSR },
398 { "f.46", 46, PPC_OPERAND_VSR },
399 { "f.47", 47, PPC_OPERAND_VSR },
400 { "f.48", 48, PPC_OPERAND_VSR },
401 { "f.49", 49, PPC_OPERAND_VSR },
402 { "f.5", 5, PPC_OPERAND_FPR },
403 { "f.50", 50, PPC_OPERAND_VSR },
404 { "f.51", 51, PPC_OPERAND_VSR },
405 { "f.52", 52, PPC_OPERAND_VSR },
406 { "f.53", 53, PPC_OPERAND_VSR },
407 { "f.54", 54, PPC_OPERAND_VSR },
408 { "f.55", 55, PPC_OPERAND_VSR },
409 { "f.56", 56, PPC_OPERAND_VSR },
410 { "f.57", 57, PPC_OPERAND_VSR },
411 { "f.58", 58, PPC_OPERAND_VSR },
412 { "f.59", 59, PPC_OPERAND_VSR },
413 { "f.6", 6, PPC_OPERAND_FPR },
414 { "f.60", 60, PPC_OPERAND_VSR },
415 { "f.61", 61, PPC_OPERAND_VSR },
416 { "f.62", 62, PPC_OPERAND_VSR },
417 { "f.63", 63, PPC_OPERAND_VSR },
418 { "f.7", 7, PPC_OPERAND_FPR },
419 { "f.8", 8, PPC_OPERAND_FPR },
420 { "f.9", 9, PPC_OPERAND_FPR },
422 { "f0", 0, PPC_OPERAND_FPR },
423 { "f1", 1, PPC_OPERAND_FPR },
424 { "f10", 10, PPC_OPERAND_FPR },
425 { "f11", 11, PPC_OPERAND_FPR },
426 { "f12", 12, PPC_OPERAND_FPR },
427 { "f13", 13, PPC_OPERAND_FPR },
428 { "f14", 14, PPC_OPERAND_FPR },
429 { "f15", 15, PPC_OPERAND_FPR },
430 { "f16", 16, PPC_OPERAND_FPR },
431 { "f17", 17, PPC_OPERAND_FPR },
432 { "f18", 18, PPC_OPERAND_FPR },
433 { "f19", 19, PPC_OPERAND_FPR },
434 { "f2", 2, PPC_OPERAND_FPR },
435 { "f20", 20, PPC_OPERAND_FPR },
436 { "f21", 21, PPC_OPERAND_FPR },
437 { "f22", 22, PPC_OPERAND_FPR },
438 { "f23", 23, PPC_OPERAND_FPR },
439 { "f24", 24, PPC_OPERAND_FPR },
440 { "f25", 25, PPC_OPERAND_FPR },
441 { "f26", 26, PPC_OPERAND_FPR },
442 { "f27", 27, PPC_OPERAND_FPR },
443 { "f28", 28, PPC_OPERAND_FPR },
444 { "f29", 29, PPC_OPERAND_FPR },
445 { "f3", 3, PPC_OPERAND_FPR },
446 { "f30", 30, PPC_OPERAND_FPR },
447 { "f31", 31, PPC_OPERAND_FPR },
448 { "f32", 32, PPC_OPERAND_VSR },
449 { "f33", 33, PPC_OPERAND_VSR },
450 { "f34", 34, PPC_OPERAND_VSR },
451 { "f35", 35, PPC_OPERAND_VSR },
452 { "f36", 36, PPC_OPERAND_VSR },
453 { "f37", 37, PPC_OPERAND_VSR },
454 { "f38", 38, PPC_OPERAND_VSR },
455 { "f39", 39, PPC_OPERAND_VSR },
456 { "f4", 4, PPC_OPERAND_FPR },
457 { "f40", 40, PPC_OPERAND_VSR },
458 { "f41", 41, PPC_OPERAND_VSR },
459 { "f42", 42, PPC_OPERAND_VSR },
460 { "f43", 43, PPC_OPERAND_VSR },
461 { "f44", 44, PPC_OPERAND_VSR },
462 { "f45", 45, PPC_OPERAND_VSR },
463 { "f46", 46, PPC_OPERAND_VSR },
464 { "f47", 47, PPC_OPERAND_VSR },
465 { "f48", 48, PPC_OPERAND_VSR },
466 { "f49", 49, PPC_OPERAND_VSR },
467 { "f5", 5, PPC_OPERAND_FPR },
468 { "f50", 50, PPC_OPERAND_VSR },
469 { "f51", 51, PPC_OPERAND_VSR },
470 { "f52", 52, PPC_OPERAND_VSR },
471 { "f53", 53, PPC_OPERAND_VSR },
472 { "f54", 54, PPC_OPERAND_VSR },
473 { "f55", 55, PPC_OPERAND_VSR },
474 { "f56", 56, PPC_OPERAND_VSR },
475 { "f57", 57, PPC_OPERAND_VSR },
476 { "f58", 58, PPC_OPERAND_VSR },
477 { "f59", 59, PPC_OPERAND_VSR },
478 { "f6", 6, PPC_OPERAND_FPR },
479 { "f60", 60, PPC_OPERAND_VSR },
480 { "f61", 61, PPC_OPERAND_VSR },
481 { "f62", 62, PPC_OPERAND_VSR },
482 { "f63", 63, PPC_OPERAND_VSR },
483 { "f7", 7, PPC_OPERAND_FPR },
484 { "f8", 8, PPC_OPERAND_FPR },
485 { "f9", 9, PPC_OPERAND_FPR },
487 /* Quantization registers used with pair single instructions. */
488 { "gqr.0", 0, PPC_OPERAND_GQR },
489 { "gqr.1", 1, PPC_OPERAND_GQR },
490 { "gqr.2", 2, PPC_OPERAND_GQR },
491 { "gqr.3", 3, PPC_OPERAND_GQR },
492 { "gqr.4", 4, PPC_OPERAND_GQR },
493 { "gqr.5", 5, PPC_OPERAND_GQR },
494 { "gqr.6", 6, PPC_OPERAND_GQR },
495 { "gqr.7", 7, PPC_OPERAND_GQR },
496 { "gqr0", 0, PPC_OPERAND_GQR },
497 { "gqr1", 1, PPC_OPERAND_GQR },
498 { "gqr2", 2, PPC_OPERAND_GQR },
499 { "gqr3", 3, PPC_OPERAND_GQR },
500 { "gqr4", 4, PPC_OPERAND_GQR },
501 { "gqr5", 5, PPC_OPERAND_GQR },
502 { "gqr6", 6, PPC_OPERAND_GQR },
503 { "gqr7", 7, PPC_OPERAND_GQR },
505 { "lr", 8, PPC_OPERAND_SPR },
507 /* General Purpose Registers */
508 { "r.0", 0, PPC_OPERAND_GPR },
509 { "r.1", 1, PPC_OPERAND_GPR },
510 { "r.10", 10, PPC_OPERAND_GPR },
511 { "r.11", 11, PPC_OPERAND_GPR },
512 { "r.12", 12, PPC_OPERAND_GPR },
513 { "r.13", 13, PPC_OPERAND_GPR },
514 { "r.14", 14, PPC_OPERAND_GPR },
515 { "r.15", 15, PPC_OPERAND_GPR },
516 { "r.16", 16, PPC_OPERAND_GPR },
517 { "r.17", 17, PPC_OPERAND_GPR },
518 { "r.18", 18, PPC_OPERAND_GPR },
519 { "r.19", 19, PPC_OPERAND_GPR },
520 { "r.2", 2, PPC_OPERAND_GPR },
521 { "r.20", 20, PPC_OPERAND_GPR },
522 { "r.21", 21, PPC_OPERAND_GPR },
523 { "r.22", 22, PPC_OPERAND_GPR },
524 { "r.23", 23, PPC_OPERAND_GPR },
525 { "r.24", 24, PPC_OPERAND_GPR },
526 { "r.25", 25, PPC_OPERAND_GPR },
527 { "r.26", 26, PPC_OPERAND_GPR },
528 { "r.27", 27, PPC_OPERAND_GPR },
529 { "r.28", 28, PPC_OPERAND_GPR },
530 { "r.29", 29, PPC_OPERAND_GPR },
531 { "r.3", 3, PPC_OPERAND_GPR },
532 { "r.30", 30, PPC_OPERAND_GPR },
533 { "r.31", 31, PPC_OPERAND_GPR },
534 { "r.4", 4, PPC_OPERAND_GPR },
535 { "r.5", 5, PPC_OPERAND_GPR },
536 { "r.6", 6, PPC_OPERAND_GPR },
537 { "r.7", 7, PPC_OPERAND_GPR },
538 { "r.8", 8, PPC_OPERAND_GPR },
539 { "r.9", 9, PPC_OPERAND_GPR },
541 { "r.sp", 1, PPC_OPERAND_GPR },
543 { "r.toc", 2, PPC_OPERAND_GPR },
545 { "r0", 0, PPC_OPERAND_GPR },
546 { "r1", 1, PPC_OPERAND_GPR },
547 { "r10", 10, PPC_OPERAND_GPR },
548 { "r11", 11, PPC_OPERAND_GPR },
549 { "r12", 12, PPC_OPERAND_GPR },
550 { "r13", 13, PPC_OPERAND_GPR },
551 { "r14", 14, PPC_OPERAND_GPR },
552 { "r15", 15, PPC_OPERAND_GPR },
553 { "r16", 16, PPC_OPERAND_GPR },
554 { "r17", 17, PPC_OPERAND_GPR },
555 { "r18", 18, PPC_OPERAND_GPR },
556 { "r19", 19, PPC_OPERAND_GPR },
557 { "r2", 2, PPC_OPERAND_GPR },
558 { "r20", 20, PPC_OPERAND_GPR },
559 { "r21", 21, PPC_OPERAND_GPR },
560 { "r22", 22, PPC_OPERAND_GPR },
561 { "r23", 23, PPC_OPERAND_GPR },
562 { "r24", 24, PPC_OPERAND_GPR },
563 { "r25", 25, PPC_OPERAND_GPR },
564 { "r26", 26, PPC_OPERAND_GPR },
565 { "r27", 27, PPC_OPERAND_GPR },
566 { "r28", 28, PPC_OPERAND_GPR },
567 { "r29", 29, PPC_OPERAND_GPR },
568 { "r3", 3, PPC_OPERAND_GPR },
569 { "r30", 30, PPC_OPERAND_GPR },
570 { "r31", 31, PPC_OPERAND_GPR },
571 { "r4", 4, PPC_OPERAND_GPR },
572 { "r5", 5, PPC_OPERAND_GPR },
573 { "r6", 6, PPC_OPERAND_GPR },
574 { "r7", 7, PPC_OPERAND_GPR },
575 { "r8", 8, PPC_OPERAND_GPR },
576 { "r9", 9, PPC_OPERAND_GPR },
578 { "rtoc", 2, PPC_OPERAND_GPR },
580 { "sdr1", 25, PPC_OPERAND_SPR },
582 { "sp", 1, PPC_OPERAND_GPR },
584 { "srr0", 26, PPC_OPERAND_SPR },
585 { "srr1", 27, PPC_OPERAND_SPR },
587 /* Vector (Altivec/VMX) registers */
588 { "v.0", 0, PPC_OPERAND_VR },
589 { "v.1", 1, PPC_OPERAND_VR },
590 { "v.10", 10, PPC_OPERAND_VR },
591 { "v.11", 11, PPC_OPERAND_VR },
592 { "v.12", 12, PPC_OPERAND_VR },
593 { "v.13", 13, PPC_OPERAND_VR },
594 { "v.14", 14, PPC_OPERAND_VR },
595 { "v.15", 15, PPC_OPERAND_VR },
596 { "v.16", 16, PPC_OPERAND_VR },
597 { "v.17", 17, PPC_OPERAND_VR },
598 { "v.18", 18, PPC_OPERAND_VR },
599 { "v.19", 19, PPC_OPERAND_VR },
600 { "v.2", 2, PPC_OPERAND_VR },
601 { "v.20", 20, PPC_OPERAND_VR },
602 { "v.21", 21, PPC_OPERAND_VR },
603 { "v.22", 22, PPC_OPERAND_VR },
604 { "v.23", 23, PPC_OPERAND_VR },
605 { "v.24", 24, PPC_OPERAND_VR },
606 { "v.25", 25, PPC_OPERAND_VR },
607 { "v.26", 26, PPC_OPERAND_VR },
608 { "v.27", 27, PPC_OPERAND_VR },
609 { "v.28", 28, PPC_OPERAND_VR },
610 { "v.29", 29, PPC_OPERAND_VR },
611 { "v.3", 3, PPC_OPERAND_VR },
612 { "v.30", 30, PPC_OPERAND_VR },
613 { "v.31", 31, PPC_OPERAND_VR },
614 { "v.4", 4, PPC_OPERAND_VR },
615 { "v.5", 5, PPC_OPERAND_VR },
616 { "v.6", 6, PPC_OPERAND_VR },
617 { "v.7", 7, PPC_OPERAND_VR },
618 { "v.8", 8, PPC_OPERAND_VR },
619 { "v.9", 9, PPC_OPERAND_VR },
621 { "v0", 0, PPC_OPERAND_VR },
622 { "v1", 1, PPC_OPERAND_VR },
623 { "v10", 10, PPC_OPERAND_VR },
624 { "v11", 11, PPC_OPERAND_VR },
625 { "v12", 12, PPC_OPERAND_VR },
626 { "v13", 13, PPC_OPERAND_VR },
627 { "v14", 14, PPC_OPERAND_VR },
628 { "v15", 15, PPC_OPERAND_VR },
629 { "v16", 16, PPC_OPERAND_VR },
630 { "v17", 17, PPC_OPERAND_VR },
631 { "v18", 18, PPC_OPERAND_VR },
632 { "v19", 19, PPC_OPERAND_VR },
633 { "v2", 2, PPC_OPERAND_VR },
634 { "v20", 20, PPC_OPERAND_VR },
635 { "v21", 21, PPC_OPERAND_VR },
636 { "v22", 22, PPC_OPERAND_VR },
637 { "v23", 23, PPC_OPERAND_VR },
638 { "v24", 24, PPC_OPERAND_VR },
639 { "v25", 25, PPC_OPERAND_VR },
640 { "v26", 26, PPC_OPERAND_VR },
641 { "v27", 27, PPC_OPERAND_VR },
642 { "v28", 28, PPC_OPERAND_VR },
643 { "v29", 29, PPC_OPERAND_VR },
644 { "v3", 3, PPC_OPERAND_VR },
645 { "v30", 30, PPC_OPERAND_VR },
646 { "v31", 31, PPC_OPERAND_VR },
647 { "v4", 4, PPC_OPERAND_VR },
648 { "v5", 5, PPC_OPERAND_VR },
649 { "v6", 6, PPC_OPERAND_VR },
650 { "v7", 7, PPC_OPERAND_VR },
651 { "v8", 8, PPC_OPERAND_VR },
652 { "v9", 9, PPC_OPERAND_VR },
654 /* Vector Scalar (VSX) registers (ISA 2.06). */
655 { "vs.0", 0, PPC_OPERAND_VSR },
656 { "vs.1", 1, PPC_OPERAND_VSR },
657 { "vs.10", 10, PPC_OPERAND_VSR },
658 { "vs.11", 11, PPC_OPERAND_VSR },
659 { "vs.12", 12, PPC_OPERAND_VSR },
660 { "vs.13", 13, PPC_OPERAND_VSR },
661 { "vs.14", 14, PPC_OPERAND_VSR },
662 { "vs.15", 15, PPC_OPERAND_VSR },
663 { "vs.16", 16, PPC_OPERAND_VSR },
664 { "vs.17", 17, PPC_OPERAND_VSR },
665 { "vs.18", 18, PPC_OPERAND_VSR },
666 { "vs.19", 19, PPC_OPERAND_VSR },
667 { "vs.2", 2, PPC_OPERAND_VSR },
668 { "vs.20", 20, PPC_OPERAND_VSR },
669 { "vs.21", 21, PPC_OPERAND_VSR },
670 { "vs.22", 22, PPC_OPERAND_VSR },
671 { "vs.23", 23, PPC_OPERAND_VSR },
672 { "vs.24", 24, PPC_OPERAND_VSR },
673 { "vs.25", 25, PPC_OPERAND_VSR },
674 { "vs.26", 26, PPC_OPERAND_VSR },
675 { "vs.27", 27, PPC_OPERAND_VSR },
676 { "vs.28", 28, PPC_OPERAND_VSR },
677 { "vs.29", 29, PPC_OPERAND_VSR },
678 { "vs.3", 3, PPC_OPERAND_VSR },
679 { "vs.30", 30, PPC_OPERAND_VSR },
680 { "vs.31", 31, PPC_OPERAND_VSR },
681 { "vs.32", 32, PPC_OPERAND_VSR },
682 { "vs.33", 33, PPC_OPERAND_VSR },
683 { "vs.34", 34, PPC_OPERAND_VSR },
684 { "vs.35", 35, PPC_OPERAND_VSR },
685 { "vs.36", 36, PPC_OPERAND_VSR },
686 { "vs.37", 37, PPC_OPERAND_VSR },
687 { "vs.38", 38, PPC_OPERAND_VSR },
688 { "vs.39", 39, PPC_OPERAND_VSR },
689 { "vs.4", 4, PPC_OPERAND_VSR },
690 { "vs.40", 40, PPC_OPERAND_VSR },
691 { "vs.41", 41, PPC_OPERAND_VSR },
692 { "vs.42", 42, PPC_OPERAND_VSR },
693 { "vs.43", 43, PPC_OPERAND_VSR },
694 { "vs.44", 44, PPC_OPERAND_VSR },
695 { "vs.45", 45, PPC_OPERAND_VSR },
696 { "vs.46", 46, PPC_OPERAND_VSR },
697 { "vs.47", 47, PPC_OPERAND_VSR },
698 { "vs.48", 48, PPC_OPERAND_VSR },
699 { "vs.49", 49, PPC_OPERAND_VSR },
700 { "vs.5", 5, PPC_OPERAND_VSR },
701 { "vs.50", 50, PPC_OPERAND_VSR },
702 { "vs.51", 51, PPC_OPERAND_VSR },
703 { "vs.52", 52, PPC_OPERAND_VSR },
704 { "vs.53", 53, PPC_OPERAND_VSR },
705 { "vs.54", 54, PPC_OPERAND_VSR },
706 { "vs.55", 55, PPC_OPERAND_VSR },
707 { "vs.56", 56, PPC_OPERAND_VSR },
708 { "vs.57", 57, PPC_OPERAND_VSR },
709 { "vs.58", 58, PPC_OPERAND_VSR },
710 { "vs.59", 59, PPC_OPERAND_VSR },
711 { "vs.6", 6, PPC_OPERAND_VSR },
712 { "vs.60", 60, PPC_OPERAND_VSR },
713 { "vs.61", 61, PPC_OPERAND_VSR },
714 { "vs.62", 62, PPC_OPERAND_VSR },
715 { "vs.63", 63, PPC_OPERAND_VSR },
716 { "vs.7", 7, PPC_OPERAND_VSR },
717 { "vs.8", 8, PPC_OPERAND_VSR },
718 { "vs.9", 9, PPC_OPERAND_VSR },
720 { "vs0", 0, PPC_OPERAND_VSR },
721 { "vs1", 1, PPC_OPERAND_VSR },
722 { "vs10", 10, PPC_OPERAND_VSR },
723 { "vs11", 11, PPC_OPERAND_VSR },
724 { "vs12", 12, PPC_OPERAND_VSR },
725 { "vs13", 13, PPC_OPERAND_VSR },
726 { "vs14", 14, PPC_OPERAND_VSR },
727 { "vs15", 15, PPC_OPERAND_VSR },
728 { "vs16", 16, PPC_OPERAND_VSR },
729 { "vs17", 17, PPC_OPERAND_VSR },
730 { "vs18", 18, PPC_OPERAND_VSR },
731 { "vs19", 19, PPC_OPERAND_VSR },
732 { "vs2", 2, PPC_OPERAND_VSR },
733 { "vs20", 20, PPC_OPERAND_VSR },
734 { "vs21", 21, PPC_OPERAND_VSR },
735 { "vs22", 22, PPC_OPERAND_VSR },
736 { "vs23", 23, PPC_OPERAND_VSR },
737 { "vs24", 24, PPC_OPERAND_VSR },
738 { "vs25", 25, PPC_OPERAND_VSR },
739 { "vs26", 26, PPC_OPERAND_VSR },
740 { "vs27", 27, PPC_OPERAND_VSR },
741 { "vs28", 28, PPC_OPERAND_VSR },
742 { "vs29", 29, PPC_OPERAND_VSR },
743 { "vs3", 3, PPC_OPERAND_VSR },
744 { "vs30", 30, PPC_OPERAND_VSR },
745 { "vs31", 31, PPC_OPERAND_VSR },
746 { "vs32", 32, PPC_OPERAND_VSR },
747 { "vs33", 33, PPC_OPERAND_VSR },
748 { "vs34", 34, PPC_OPERAND_VSR },
749 { "vs35", 35, PPC_OPERAND_VSR },
750 { "vs36", 36, PPC_OPERAND_VSR },
751 { "vs37", 37, PPC_OPERAND_VSR },
752 { "vs38", 38, PPC_OPERAND_VSR },
753 { "vs39", 39, PPC_OPERAND_VSR },
754 { "vs4", 4, PPC_OPERAND_VSR },
755 { "vs40", 40, PPC_OPERAND_VSR },
756 { "vs41", 41, PPC_OPERAND_VSR },
757 { "vs42", 42, PPC_OPERAND_VSR },
758 { "vs43", 43, PPC_OPERAND_VSR },
759 { "vs44", 44, PPC_OPERAND_VSR },
760 { "vs45", 45, PPC_OPERAND_VSR },
761 { "vs46", 46, PPC_OPERAND_VSR },
762 { "vs47", 47, PPC_OPERAND_VSR },
763 { "vs48", 48, PPC_OPERAND_VSR },
764 { "vs49", 49, PPC_OPERAND_VSR },
765 { "vs5", 5, PPC_OPERAND_VSR },
766 { "vs50", 50, PPC_OPERAND_VSR },
767 { "vs51", 51, PPC_OPERAND_VSR },
768 { "vs52", 52, PPC_OPERAND_VSR },
769 { "vs53", 53, PPC_OPERAND_VSR },
770 { "vs54", 54, PPC_OPERAND_VSR },
771 { "vs55", 55, PPC_OPERAND_VSR },
772 { "vs56", 56, PPC_OPERAND_VSR },
773 { "vs57", 57, PPC_OPERAND_VSR },
774 { "vs58", 58, PPC_OPERAND_VSR },
775 { "vs59", 59, PPC_OPERAND_VSR },
776 { "vs6", 6, PPC_OPERAND_VSR },
777 { "vs60", 60, PPC_OPERAND_VSR },
778 { "vs61", 61, PPC_OPERAND_VSR },
779 { "vs62", 62, PPC_OPERAND_VSR },
780 { "vs63", 63, PPC_OPERAND_VSR },
781 { "vs7", 7, PPC_OPERAND_VSR },
782 { "vs8", 8, PPC_OPERAND_VSR },
783 { "vs9", 9, PPC_OPERAND_VSR },
785 { "xer", 1, PPC_OPERAND_SPR }
788 #define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct pd_reg))
790 /* Given NAME, find the register number associated with that name, return
791 the integer value associated with the given name or -1 on failure. */
793 static const struct pd_reg *
794 reg_name_search (const struct pd_reg *regs, int regcount, const char *name)
796 int middle, low, high;
797 int cmp;
799 low = 0;
800 high = regcount - 1;
804 middle = (low + high) / 2;
805 cmp = strcasecmp (name, regs[middle].name);
806 if (cmp < 0)
807 high = middle - 1;
808 else if (cmp > 0)
809 low = middle + 1;
810 else
811 return &regs[middle];
813 while (low <= high);
815 return NULL;
819 * Summary of register_name.
821 * in: Input_line_pointer points to 1st char of operand.
823 * out: A expressionS.
824 * The operand may have been a register: in this case, X_op == O_register,
825 * X_add_number is set to the register number, and truth is returned.
826 * Input_line_pointer->(next non-blank) char after operand, or is in its
827 * original state.
830 static bool
831 register_name (expressionS *expressionP)
833 const struct pd_reg *reg;
834 char *name;
835 char *start;
836 char c;
838 /* Find the spelling of the operand. */
839 start = name = input_line_pointer;
840 if (name[0] == '%' && ISALPHA (name[1]))
841 name = ++input_line_pointer;
843 else if (!reg_names_p || !ISALPHA (name[0]))
844 return false;
846 c = get_symbol_name (&name);
847 reg = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
849 /* Put back the delimiting char. */
850 *input_line_pointer = c;
852 /* Look to see if it's in the register table. */
853 if (reg != NULL)
855 expressionP->X_op = O_register;
856 expressionP->X_add_number = reg->value;
857 expressionP->X_md = reg->flags;
859 /* Make the rest nice. */
860 expressionP->X_add_symbol = NULL;
861 expressionP->X_op_symbol = NULL;
862 return true;
865 /* Reset the line as if we had not done anything. */
866 input_line_pointer = start;
867 return false;
870 /* This function is called for each symbol seen in an expression. It
871 handles the special parsing which PowerPC assemblers are supposed
872 to use for condition codes. */
874 /* Whether to do the special parsing. */
875 static bool cr_operand;
877 /* Names to recognize in a condition code. This table is sorted. */
878 static const struct pd_reg cr_names[] =
880 { "cr0", 0, PPC_OPERAND_CR_REG },
881 { "cr1", 1, PPC_OPERAND_CR_REG },
882 { "cr2", 2, PPC_OPERAND_CR_REG },
883 { "cr3", 3, PPC_OPERAND_CR_REG },
884 { "cr4", 4, PPC_OPERAND_CR_REG },
885 { "cr5", 5, PPC_OPERAND_CR_REG },
886 { "cr6", 6, PPC_OPERAND_CR_REG },
887 { "cr7", 7, PPC_OPERAND_CR_REG },
888 { "eq", 2, PPC_OPERAND_CR_BIT },
889 { "gt", 1, PPC_OPERAND_CR_BIT },
890 { "lt", 0, PPC_OPERAND_CR_BIT },
891 { "so", 3, PPC_OPERAND_CR_BIT },
892 { "un", 3, PPC_OPERAND_CR_BIT }
895 /* Parsing function. This returns non-zero if it recognized an
896 expression. */
899 ppc_parse_name (const char *name, expressionS *exp)
901 const struct pd_reg *reg;
903 if (! cr_operand)
904 return 0;
906 if (*name == '%')
907 ++name;
908 reg = reg_name_search (cr_names, sizeof cr_names / sizeof cr_names[0],
909 name);
910 if (reg == NULL)
911 return 0;
913 exp->X_op = O_register;
914 exp->X_add_number = reg->value;
915 exp->X_md = reg->flags;
917 return 1;
920 /* Propagate X_md and check register expressions. This is to support
921 condition codes like 4*cr5+eq. */
924 ppc_optimize_expr (expressionS *left, operatorT op, expressionS *right)
926 /* Accept 4*cr<n> and cr<n>*4. */
927 if (op == O_multiply
928 && ((right->X_op == O_register
929 && right->X_md == PPC_OPERAND_CR_REG
930 && left->X_op == O_constant
931 && left->X_add_number == 4)
932 || (left->X_op == O_register
933 && left->X_md == PPC_OPERAND_CR_REG
934 && right->X_op == O_constant
935 && right->X_add_number == 4)))
937 left->X_op = O_register;
938 left->X_md = PPC_OPERAND_CR_REG | PPC_OPERAND_CR_BIT;
939 left->X_add_number *= right->X_add_number;
940 return 1;
943 /* Accept the above plus <cr bit>, and <cr bit> plus the above. */
944 if (right->X_op == O_register
945 && left->X_op == O_register
946 && op == O_add
947 && ((right->X_md == PPC_OPERAND_CR_BIT
948 && left->X_md == (PPC_OPERAND_CR_REG | PPC_OPERAND_CR_BIT))
949 || (right->X_md == (PPC_OPERAND_CR_REG | PPC_OPERAND_CR_BIT)
950 && left->X_md == PPC_OPERAND_CR_BIT)))
952 left->X_md = PPC_OPERAND_CR_BIT;
953 right->X_op = O_constant;
954 return 0;
957 /* Accept reg +/- constant. */
958 if (left->X_op == O_register
959 && !((op == O_add || op == O_subtract) && right->X_op == O_constant))
960 as_warn (_("invalid register expression"));
962 /* Accept constant + reg. */
963 if (right->X_op == O_register)
965 if (op == O_add && left->X_op == O_constant)
966 left->X_md = right->X_md;
967 else
968 as_warn (_("invalid register expression"));
971 return 0;
974 /* Local variables. */
976 /* Whether to target xcoff64/elf64. */
977 static unsigned int ppc_obj64 = BFD_DEFAULT_TARGET_SIZE == 64;
979 /* Opcode hash table. */
980 static htab_t ppc_hash;
982 #ifdef OBJ_ELF
983 /* What type of shared library support to use. */
984 static enum { SHLIB_NONE, SHLIB_PIC, SHLIB_MRELOCATABLE } shlib = SHLIB_NONE;
986 /* Flags to set in the elf header. */
987 static flagword ppc_flags = 0;
989 /* Whether this is Solaris or not. */
990 #ifdef TARGET_SOLARIS_COMMENT
991 #define SOLARIS_P true
992 #else
993 #define SOLARIS_P false
994 #endif
996 static bool msolaris = SOLARIS_P;
997 #endif
999 #ifdef OBJ_XCOFF
1001 /* The RS/6000 assembler uses the .csect pseudo-op to generate code
1002 using a bunch of different sections. These assembler sections,
1003 however, are all encompassed within the .text, .data or .bss sections
1004 of the final output file. We handle this by using different
1005 subsegments within these main segments.
1006 .tdata and .tbss sections only have one type of csects for now,
1007 but it's better to follow the same construction like the others. */
1009 struct ppc_xcoff_section ppc_xcoff_text_section;
1010 struct ppc_xcoff_section ppc_xcoff_data_section;
1011 struct ppc_xcoff_section ppc_xcoff_bss_section;
1012 struct ppc_xcoff_section ppc_xcoff_tdata_section;
1013 struct ppc_xcoff_section ppc_xcoff_tbss_section;
1015 /* Return true if the ppc_xcoff_section structure is already
1016 initialized. */
1017 static bool
1018 ppc_xcoff_section_is_initialized (struct ppc_xcoff_section *section)
1020 return section->segment != NULL;
1023 /* Initialize a ppc_xcoff_section.
1024 Dummy symbols are used to ensure the position of .text over .data
1025 and .tdata. Moreover, they allow all algorithms here to be sure that
1026 csects isn't NULL. These symbols won't be output. */
1027 static void
1028 ppc_init_xcoff_section (struct ppc_xcoff_section *s, segT seg)
1030 s->segment = seg;
1031 s->next_subsegment = 2;
1032 s->csects = symbol_make ("dummy\001");
1033 symbol_get_tc (s->csects)->within = s->csects;
1036 /* The current csect. */
1037 static symbolS *ppc_current_csect;
1039 /* The RS/6000 assembler uses a TOC which holds addresses of functions
1040 and variables. Symbols are put in the TOC with the .tc pseudo-op.
1041 A special relocation is used when accessing TOC entries. We handle
1042 the TOC as a subsegment within the .data segment. We set it up if
1043 we see a .toc pseudo-op, and save the csect symbol here. */
1044 static symbolS *ppc_toc_csect;
1046 /* The first frag in the TOC subsegment. */
1047 static fragS *ppc_toc_frag;
1049 /* The first frag in the first subsegment after the TOC in the .data
1050 segment. NULL if there are no subsegments after the TOC. */
1051 static fragS *ppc_after_toc_frag;
1053 /* The current static block. */
1054 static symbolS *ppc_current_block;
1056 /* The COFF debugging section; set by md_begin. This is not the
1057 .debug section, but is instead the secret BFD section which will
1058 cause BFD to set the section number of a symbol to N_DEBUG. */
1059 static asection *ppc_coff_debug_section;
1061 /* Structure to set the length field of the dwarf sections. */
1062 struct dw_subsection {
1063 /* Subsections are simply linked. */
1064 struct dw_subsection *link;
1066 /* The subsection number. */
1067 subsegT subseg;
1069 /* Expression to compute the length of the section. */
1070 expressionS end_exp;
1073 static struct dw_section {
1074 /* Corresponding section. */
1075 segT sect;
1077 /* Simply linked list of subsections with a label. */
1078 struct dw_subsection *list_subseg;
1080 /* The anonymous subsection. */
1081 struct dw_subsection *anon_subseg;
1082 } dw_sections[XCOFF_DWSECT_NBR_NAMES];
1083 #endif /* OBJ_XCOFF */
1085 #ifdef OBJ_ELF
1086 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE" */
1087 unsigned long *ppc_apuinfo_list;
1088 unsigned int ppc_apuinfo_num;
1089 unsigned int ppc_apuinfo_num_alloc;
1090 #endif /* OBJ_ELF */
1092 #ifdef OBJ_ELF
1093 const char *const md_shortopts = "b:l:usm:K:VQ:";
1094 #else
1095 const char *const md_shortopts = "um:";
1096 #endif
1097 #define OPTION_NOPS (OPTION_MD_BASE + 0)
1098 const struct option md_longopts[] = {
1099 {"nops", required_argument, NULL, OPTION_NOPS},
1100 {"ppc476-workaround", no_argument, &warn_476, 1},
1101 {"no-ppc476-workaround", no_argument, &warn_476, 0},
1102 {NULL, no_argument, NULL, 0}
1104 const size_t md_longopts_size = sizeof (md_longopts);
1107 md_parse_option (int c, const char *arg)
1109 ppc_cpu_t new_cpu;
1111 switch (c)
1113 case 'u':
1114 /* -u means that any undefined symbols should be treated as
1115 external, which is the default for gas anyhow. */
1116 break;
1118 #ifdef OBJ_ELF
1119 case 'l':
1120 /* Solaris as takes -le (presumably for little endian). For completeness
1121 sake, recognize -be also. */
1122 if (strcmp (arg, "e") == 0)
1124 target_big_endian = 0;
1125 set_target_endian = 1;
1126 if (ppc_cpu & PPC_OPCODE_VLE)
1127 as_bad (_("the use of -mvle requires big endian."));
1129 else
1130 return 0;
1132 break;
1134 case 'b':
1135 if (strcmp (arg, "e") == 0)
1137 target_big_endian = 1;
1138 set_target_endian = 1;
1140 else
1141 return 0;
1143 break;
1145 case 'K':
1146 /* Recognize -K PIC. */
1147 if (strcmp (arg, "PIC") == 0 || strcmp (arg, "pic") == 0)
1149 shlib = SHLIB_PIC;
1150 ppc_flags |= EF_PPC_RELOCATABLE_LIB;
1152 else
1153 return 0;
1155 break;
1156 #endif
1158 /* a64 and a32 determine whether to use XCOFF64 or XCOFF32. */
1159 case 'a':
1160 if (strcmp (arg, "64") == 0)
1162 #ifdef BFD64
1163 ppc_obj64 = 1;
1164 if (ppc_cpu & PPC_OPCODE_VLE)
1165 as_bad (_("the use of -mvle requires -a32."));
1166 #else
1167 as_fatal (_("%s unsupported"), "-a64");
1168 #endif
1170 else if (strcmp (arg, "32") == 0)
1171 ppc_obj64 = 0;
1172 else
1173 return 0;
1174 break;
1176 case 'm':
1177 new_cpu = ppc_parse_cpu (ppc_cpu, &sticky, arg);
1178 /* "raw" is only valid for the disassembler. */
1179 if (new_cpu != 0 && (new_cpu & PPC_OPCODE_RAW) == 0)
1181 ppc_cpu = new_cpu;
1182 if (strcmp (arg, "vle") == 0)
1184 if (set_target_endian && target_big_endian == 0)
1185 as_bad (_("the use of -mvle requires big endian."));
1186 if (ppc_obj64)
1187 as_bad (_("the use of -mvle requires -a32."));
1191 else if (strcmp (arg, "no-vle") == 0)
1193 sticky &= ~PPC_OPCODE_VLE;
1195 new_cpu = ppc_parse_cpu (ppc_cpu, &sticky, "booke");
1196 new_cpu &= ~PPC_OPCODE_VLE;
1198 ppc_cpu = new_cpu;
1201 else if (strcmp (arg, "regnames") == 0)
1202 reg_names_p = true;
1204 else if (strcmp (arg, "no-regnames") == 0)
1205 reg_names_p = false;
1207 #ifdef OBJ_ELF
1208 /* -mrelocatable/-mrelocatable-lib -- warn about initializations
1209 that require relocation. */
1210 else if (strcmp (arg, "relocatable") == 0)
1212 shlib = SHLIB_MRELOCATABLE;
1213 ppc_flags |= EF_PPC_RELOCATABLE;
1216 else if (strcmp (arg, "relocatable-lib") == 0)
1218 shlib = SHLIB_MRELOCATABLE;
1219 ppc_flags |= EF_PPC_RELOCATABLE_LIB;
1222 /* -memb, set embedded bit. */
1223 else if (strcmp (arg, "emb") == 0)
1224 ppc_flags |= EF_PPC_EMB;
1226 /* -mlittle/-mbig set the endianness. */
1227 else if (strcmp (arg, "little") == 0
1228 || strcmp (arg, "little-endian") == 0)
1230 target_big_endian = 0;
1231 set_target_endian = 1;
1232 if (ppc_cpu & PPC_OPCODE_VLE)
1233 as_bad (_("the use of -mvle requires big endian."));
1236 else if (strcmp (arg, "big") == 0 || strcmp (arg, "big-endian") == 0)
1238 target_big_endian = 1;
1239 set_target_endian = 1;
1242 else if (strcmp (arg, "solaris") == 0)
1244 msolaris = true;
1245 ppc_comment_chars = ppc_solaris_comment_chars;
1248 else if (strcmp (arg, "no-solaris") == 0)
1250 msolaris = false;
1251 ppc_comment_chars = ppc_eabi_comment_chars;
1253 else if (strcmp (arg, "spe2") == 0)
1255 ppc_cpu |= PPC_OPCODE_SPE2;
1257 #endif
1258 else
1260 as_bad (_("invalid switch -m%s"), arg);
1261 return 0;
1263 break;
1265 #ifdef OBJ_ELF
1266 /* -V: SVR4 argument to print version ID. */
1267 case 'V':
1268 print_version_id ();
1269 break;
1271 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
1272 should be emitted or not. FIXME: Not implemented. */
1273 case 'Q':
1274 break;
1276 /* Solaris takes -s to specify that .stabs go in a .stabs section,
1277 rather than .stabs.excl, which is ignored by the linker.
1278 FIXME: Not implemented. */
1279 case 's':
1280 if (arg)
1281 return 0;
1283 break;
1284 #endif
1286 case OPTION_NOPS:
1288 char *end;
1289 nop_limit = strtoul (optarg, &end, 0);
1290 if (*end)
1291 as_bad (_("--nops needs a numeric argument"));
1293 break;
1295 case 0:
1296 break;
1298 default:
1299 return 0;
1302 return 1;
1305 static int
1306 is_ppc64_target (const bfd_target *targ, void *data ATTRIBUTE_UNUSED)
1308 switch (targ->flavour)
1310 #ifdef OBJ_ELF
1311 case bfd_target_elf_flavour:
1312 return startswith (targ->name, "elf64-powerpc");
1313 #endif
1314 #ifdef OBJ_XCOFF
1315 case bfd_target_xcoff_flavour:
1316 return (strcmp (targ->name, "aixcoff64-rs6000") == 0
1317 || strcmp (targ->name, "aix5coff64-rs6000") == 0);
1318 #endif
1319 default:
1320 return 0;
1324 void
1325 md_show_usage (FILE *stream)
1327 fprintf (stream, _("\
1328 PowerPC options:\n"));
1329 fprintf (stream, _("\
1330 -a32 generate ELF32/XCOFF32\n"));
1331 if (bfd_iterate_over_targets (is_ppc64_target, NULL))
1332 fprintf (stream, _("\
1333 -a64 generate ELF64/XCOFF64\n"));
1334 fprintf (stream, _("\
1335 -u ignored\n"));
1336 fprintf (stream, _("\
1337 -mpwrx, -mpwr2 generate code for POWER/2 (RIOS2)\n"));
1338 fprintf (stream, _("\
1339 -mpwr generate code for POWER (RIOS1)\n"));
1340 fprintf (stream, _("\
1341 -m601 generate code for PowerPC 601\n"));
1342 fprintf (stream, _("\
1343 -mppc, -mppc32, -m603, -m604\n\
1344 generate code for PowerPC 603/604\n"));
1345 fprintf (stream, _("\
1346 -m403 generate code for PowerPC 403\n"));
1347 fprintf (stream, _("\
1348 -m405 generate code for PowerPC 405\n"));
1349 fprintf (stream, _("\
1350 -m440 generate code for PowerPC 440\n"));
1351 fprintf (stream, _("\
1352 -m464 generate code for PowerPC 464\n"));
1353 fprintf (stream, _("\
1354 -m476 generate code for PowerPC 476\n"));
1355 fprintf (stream, _("\
1356 -m7400, -m7410, -m7450, -m7455\n\
1357 generate code for PowerPC 7400/7410/7450/7455\n"));
1358 fprintf (stream, _("\
1359 -m750cl, -mgekko, -mbroadway\n\
1360 generate code for PowerPC 750cl/Gekko/Broadway\n"));
1361 fprintf (stream, _("\
1362 -m821, -m850, -m860 generate code for PowerPC 821/850/860\n"));
1363 fprintf (stream, _("\
1364 -mppc64, -m620 generate code for PowerPC 620/625/630\n"));
1365 fprintf (stream, _("\
1366 -mppc64bridge generate code for PowerPC 64, including bridge insns\n"));
1367 fprintf (stream, _("\
1368 -mbooke generate code for 32-bit PowerPC BookE\n"));
1369 fprintf (stream, _("\
1370 -ma2 generate code for A2 architecture\n"));
1371 fprintf (stream, _("\
1372 -mpower4, -mpwr4 generate code for Power4 architecture\n"));
1373 fprintf (stream, _("\
1374 -mpower5, -mpwr5, -mpwr5x\n\
1375 generate code for Power5 architecture\n"));
1376 fprintf (stream, _("\
1377 -mpower6, -mpwr6 generate code for Power6 architecture\n"));
1378 fprintf (stream, _("\
1379 -mpower7, -mpwr7 generate code for Power7 architecture\n"));
1380 fprintf (stream, _("\
1381 -mpower8, -mpwr8 generate code for Power8 architecture\n"));
1382 fprintf (stream, _("\
1383 -mpower9, -mpwr9 generate code for Power9 architecture\n"));
1384 fprintf (stream, _("\
1385 -mpower10, -mpwr10 generate code for Power10 architecture\n"));
1386 fprintf (stream, _("\
1387 -mcell generate code for Cell Broadband Engine architecture\n"));
1388 fprintf (stream, _("\
1389 -mcom generate code for Power/PowerPC common instructions\n"));
1390 fprintf (stream, _("\
1391 -many generate code for any architecture (PWR/PWRX/PPC)\n"));
1392 fprintf (stream, _("\
1393 -maltivec generate code for AltiVec\n"));
1394 fprintf (stream, _("\
1395 -mvsx generate code for Vector-Scalar (VSX) instructions\n"));
1396 fprintf (stream, _("\
1397 -me300 generate code for PowerPC e300 family\n"));
1398 fprintf (stream, _("\
1399 -me500, -me500x2 generate code for Motorola e500 core complex\n"));
1400 fprintf (stream, _("\
1401 -me500mc, generate code for Freescale e500mc core complex\n"));
1402 fprintf (stream, _("\
1403 -me500mc64, generate code for Freescale e500mc64 core complex\n"));
1404 fprintf (stream, _("\
1405 -me5500, generate code for Freescale e5500 core complex\n"));
1406 fprintf (stream, _("\
1407 -me6500, generate code for Freescale e6500 core complex\n"));
1408 fprintf (stream, _("\
1409 -mspe generate code for Motorola SPE instructions\n"));
1410 fprintf (stream, _("\
1411 -mspe2 generate code for Freescale SPE2 instructions\n"));
1412 fprintf (stream, _("\
1413 -mvle generate code for Freescale VLE instructions\n"));
1414 fprintf (stream, _("\
1415 -mtitan generate code for AppliedMicro Titan core complex\n"));
1416 fprintf (stream, _("\
1417 -mregnames Allow symbolic names for registers\n"));
1418 fprintf (stream, _("\
1419 -mno-regnames Do not allow symbolic names for registers\n"));
1420 #ifdef OBJ_ELF
1421 fprintf (stream, _("\
1422 -mrelocatable support for GCC's -mrelocatble option\n"));
1423 fprintf (stream, _("\
1424 -mrelocatable-lib support for GCC's -mrelocatble-lib option\n"));
1425 fprintf (stream, _("\
1426 -memb set PPC_EMB bit in ELF flags\n"));
1427 fprintf (stream, _("\
1428 -mlittle, -mlittle-endian, -le\n\
1429 generate code for a little endian machine\n"));
1430 fprintf (stream, _("\
1431 -mbig, -mbig-endian, -be\n\
1432 generate code for a big endian machine\n"));
1433 fprintf (stream, _("\
1434 -msolaris generate code for Solaris\n"));
1435 fprintf (stream, _("\
1436 -mno-solaris do not generate code for Solaris\n"));
1437 fprintf (stream, _("\
1438 -K PIC set EF_PPC_RELOCATABLE_LIB in ELF flags\n"));
1439 fprintf (stream, _("\
1440 -V print assembler version number\n"));
1441 fprintf (stream, _("\
1442 -Qy, -Qn ignored\n"));
1443 #endif
1444 fprintf (stream, _("\
1445 -nops=count when aligning, more than COUNT nops uses a branch\n"));
1446 fprintf (stream, _("\
1447 -ppc476-workaround warn if emitting data to code sections\n"));
1450 /* Set ppc_cpu if it is not already set. */
1452 static void
1453 ppc_set_cpu (void)
1455 const char *default_os = TARGET_OS;
1456 const char *default_cpu = TARGET_CPU;
1458 if ((ppc_cpu & ~(ppc_cpu_t) PPC_OPCODE_ANY) == 0)
1460 if (ppc_obj64)
1461 if (target_big_endian)
1462 ppc_cpu |= PPC_OPCODE_PPC | PPC_OPCODE_64;
1463 else
1464 /* The minimum supported cpu for 64-bit little-endian is power8. */
1465 ppc_cpu |= ppc_parse_cpu (ppc_cpu, &sticky, "power8");
1466 else if (startswith (default_os, "aix")
1467 && default_os[3] >= '4' && default_os[3] <= '9')
1468 ppc_cpu |= PPC_OPCODE_COMMON;
1469 else if (startswith (default_os, "aix3"))
1470 ppc_cpu |= PPC_OPCODE_POWER;
1471 else if (strcmp (default_cpu, "rs6000") == 0)
1472 ppc_cpu |= PPC_OPCODE_POWER;
1473 else if (startswith (default_cpu, "powerpc"))
1474 ppc_cpu |= PPC_OPCODE_PPC;
1475 else
1476 as_fatal (_("unknown default cpu = %s, os = %s"),
1477 default_cpu, default_os);
1481 /* Figure out the BFD architecture to use. This function and ppc_mach
1482 are called well before md_begin, when the output file is opened. */
1484 enum bfd_architecture
1485 ppc_arch (void)
1487 const char *default_cpu = TARGET_CPU;
1488 ppc_set_cpu ();
1490 if ((ppc_cpu & PPC_OPCODE_PPC) != 0)
1491 return bfd_arch_powerpc;
1492 if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
1493 return bfd_arch_powerpc;
1494 if ((ppc_cpu & PPC_OPCODE_POWER) != 0)
1495 return bfd_arch_rs6000;
1496 if ((ppc_cpu & (PPC_OPCODE_COMMON | PPC_OPCODE_ANY)) != 0)
1498 if (strcmp (default_cpu, "rs6000") == 0)
1499 return bfd_arch_rs6000;
1500 else if (startswith (default_cpu, "powerpc"))
1501 return bfd_arch_powerpc;
1504 as_fatal (_("neither Power nor PowerPC opcodes were selected."));
1505 return bfd_arch_unknown;
1508 unsigned long
1509 ppc_mach (void)
1511 if (ppc_obj64)
1512 return bfd_mach_ppc64;
1513 else if (ppc_arch () == bfd_arch_rs6000)
1514 return bfd_mach_rs6k;
1515 else if (ppc_cpu & PPC_OPCODE_TITAN)
1516 return bfd_mach_ppc_titan;
1517 else if (ppc_cpu & PPC_OPCODE_VLE)
1518 return bfd_mach_ppc_vle;
1519 else
1520 return bfd_mach_ppc;
1523 extern const char*
1524 ppc_target_format (void)
1526 #ifdef OBJ_COFF
1527 #if TE_POWERMAC
1528 return "xcoff-powermac";
1529 #else
1530 # ifdef TE_AIX5
1531 return (ppc_obj64 ? "aix5coff64-rs6000" : "aixcoff-rs6000");
1532 # else
1533 return (ppc_obj64 ? "aixcoff64-rs6000" : "aixcoff-rs6000");
1534 # endif
1535 #endif
1536 #endif
1537 #ifdef OBJ_ELF
1538 # ifdef TE_FreeBSD
1539 return (ppc_obj64 ? "elf64-powerpc-freebsd" : "elf32-powerpc-freebsd");
1540 # elif defined (TE_VXWORKS)
1541 return "elf32-powerpc-vxworks";
1542 # else
1543 return (target_big_endian
1544 ? (ppc_obj64 ? "elf64-powerpc" : "elf32-powerpc")
1545 : (ppc_obj64 ? "elf64-powerpcle" : "elf32-powerpcle"));
1546 # endif
1547 #endif
1550 /* Validate one entry in powerpc_opcodes[] or vle_opcodes[].
1551 Return TRUE if there's a problem, otherwise FALSE. */
1553 static bool
1554 insn_validate (const struct powerpc_opcode *op)
1556 const ppc_opindex_t *o;
1557 uint64_t omask = op->mask;
1559 /* The mask had better not trim off opcode bits. */
1560 if ((op->opcode & omask) != op->opcode)
1562 as_bad (_("mask trims opcode bits for %s"), op->name);
1563 return true;
1566 /* The operands must not overlap the opcode or each other. */
1567 for (o = op->operands; *o; ++o)
1569 bool optional = false;
1570 if (*o >= num_powerpc_operands)
1572 as_bad (_("operand index error for %s"), op->name);
1573 return true;
1575 else
1577 uint64_t mask;
1578 const struct powerpc_operand *operand = &powerpc_operands[*o];
1579 if (operand->shift == (int) PPC_OPSHIFT_INV)
1581 const char *errmsg;
1582 uint64_t val;
1584 errmsg = NULL;
1585 val = -1;
1586 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
1587 val = -val;
1588 mask = (*operand->insert) (0, val, ppc_cpu, &errmsg);
1590 else if (operand->shift == (int) PPC_OPSHIFT_SH6)
1591 mask = (0x1f << 11) | 0x2;
1592 else if (operand->shift >= 0)
1593 mask = operand->bitm << operand->shift;
1594 else
1595 mask = operand->bitm >> -operand->shift;
1596 if (omask & mask)
1598 as_bad (_("operand %d overlap in %s"),
1599 (int) (o - op->operands), op->name);
1600 return true;
1602 omask |= mask;
1603 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
1604 optional = true;
1605 else if (optional)
1607 as_bad (_("non-optional operand %d follows optional operand in %s"),
1608 (int) (o - op->operands), op->name);
1609 return true;
1613 return false;
1616 /* Insert opcodes into hash tables. Called at startup and for
1617 .machine pseudo. */
1619 static void
1620 ppc_setup_opcodes (void)
1622 const struct powerpc_opcode *op;
1623 const struct powerpc_opcode *op_end;
1624 bool bad_insn = false;
1626 if (ppc_hash != NULL)
1627 htab_delete (ppc_hash);
1629 /* Insert the opcodes into a hash table. */
1630 ppc_hash = str_htab_create ();
1632 if (ENABLE_CHECKING)
1634 unsigned int i;
1636 /* An index into powerpc_operands is stored in struct fix
1637 fx_pcrel_adjust which is a 16 bit field. */
1638 gas_assert (num_powerpc_operands <= PPC_OPINDEX_MAX + 1);
1640 /* Check operand masks. Code here and in the disassembler assumes
1641 all the 1's in the mask are contiguous. */
1642 for (i = 0; i < num_powerpc_operands; ++i)
1644 uint64_t mask = powerpc_operands[i].bitm;
1645 uint64_t right_bit;
1646 unsigned int j;
1648 right_bit = mask & -mask;
1649 mask += right_bit;
1650 right_bit = mask & -mask;
1651 if (mask != right_bit)
1653 as_bad (_("powerpc_operands[%d].bitm invalid"), i);
1654 bad_insn = true;
1656 for (j = i + 1; j < num_powerpc_operands; ++j)
1657 if (memcmp (&powerpc_operands[i], &powerpc_operands[j],
1658 sizeof (powerpc_operands[0])) == 0)
1660 as_bad (_("powerpc_operands[%d] duplicates powerpc_operands[%d]"),
1661 j, i);
1662 bad_insn = true;
1667 op_end = powerpc_opcodes + powerpc_num_opcodes;
1668 for (op = powerpc_opcodes; op < op_end; op++)
1670 if (ENABLE_CHECKING)
1672 unsigned int new_opcode = PPC_OP (op[0].opcode);
1674 #ifdef PRINT_OPCODE_TABLE
1675 printf ("%-14s\t#%04u\tmajor op: 0x%x\top: 0x%llx\tmask: 0x%llx\tflags: 0x%llx\n",
1676 op->name, (unsigned int) (op - powerpc_opcodes),
1677 new_opcode, (unsigned long long) op->opcode,
1678 (unsigned long long) op->mask, (unsigned long long) op->flags);
1679 #endif
1681 /* The major opcodes had better be sorted. Code in the disassembler
1682 assumes the insns are sorted according to major opcode. */
1683 if (op != powerpc_opcodes
1684 && new_opcode < PPC_OP (op[-1].opcode))
1686 as_bad (_("major opcode is not sorted for %s"), op->name);
1687 bad_insn = true;
1690 if ((op->flags & PPC_OPCODE_VLE) != 0)
1692 as_bad (_("%s is enabled by vle flag"), op->name);
1693 bad_insn = true;
1695 if (PPC_OP (op->opcode) != 4
1696 && PPC_OP (op->opcode) != 31
1697 && (op->deprecated & PPC_OPCODE_VLE) == 0)
1699 as_bad (_("%s not disabled by vle flag"), op->name);
1700 bad_insn = true;
1702 bad_insn |= insn_validate (op);
1705 if ((ppc_cpu & op->flags) != 0
1706 && !(ppc_cpu & op->deprecated)
1707 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1709 as_bad (_("duplicate %s"), op->name);
1710 bad_insn = true;
1714 if ((ppc_cpu & PPC_OPCODE_ANY) != 0)
1715 for (op = powerpc_opcodes; op < op_end; op++)
1716 str_hash_insert (ppc_hash, op->name, op, 0);
1718 op_end = prefix_opcodes + prefix_num_opcodes;
1719 for (op = prefix_opcodes; op < op_end; op++)
1721 if (ENABLE_CHECKING)
1723 unsigned int new_opcode = PPC_PREFIX_SEG (op[0].opcode);
1725 #ifdef PRINT_OPCODE_TABLE
1726 printf ("%-14s\t#%04u\tmajor op/2: 0x%x\top: 0x%llx\tmask: 0x%llx\tflags: 0x%llx\n",
1727 op->name, (unsigned int) (op - prefix_opcodes),
1728 new_opcode, (unsigned long long) op->opcode,
1729 (unsigned long long) op->mask, (unsigned long long) op->flags);
1730 #endif
1732 /* The major opcodes had better be sorted. Code in the disassembler
1733 assumes the insns are sorted according to major opcode. */
1734 if (op != prefix_opcodes
1735 && new_opcode < PPC_PREFIX_SEG (op[-1].opcode))
1737 as_bad (_("major opcode is not sorted for %s"), op->name);
1738 bad_insn = true;
1740 bad_insn |= insn_validate (op);
1743 if ((ppc_cpu & op->flags) != 0
1744 && !(ppc_cpu & op->deprecated)
1745 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1747 as_bad (_("duplicate %s"), op->name);
1748 bad_insn = true;
1752 if ((ppc_cpu & PPC_OPCODE_ANY) != 0)
1753 for (op = prefix_opcodes; op < op_end; op++)
1754 str_hash_insert (ppc_hash, op->name, op, 0);
1756 op_end = vle_opcodes + vle_num_opcodes;
1757 for (op = vle_opcodes; op < op_end; op++)
1759 if (ENABLE_CHECKING)
1761 unsigned new_seg = VLE_OP_TO_SEG (VLE_OP (op[0].opcode, op[0].mask));
1763 #ifdef PRINT_OPCODE_TABLE
1764 printf ("%-14s\t#%04u\tmajor op: 0x%x\top: 0x%llx\tmask: 0x%llx\tflags: 0x%llx\n",
1765 op->name, (unsigned int) (op - vle_opcodes),
1766 (unsigned int) new_seg, (unsigned long long) op->opcode,
1767 (unsigned long long) op->mask, (unsigned long long) op->flags);
1768 #endif
1770 /* The major opcodes had better be sorted. Code in the disassembler
1771 assumes the insns are sorted according to major opcode. */
1772 if (op != vle_opcodes
1773 && new_seg < VLE_OP_TO_SEG (VLE_OP (op[-1].opcode, op[-1].mask)))
1775 as_bad (_("major opcode is not sorted for %s"), op->name);
1776 bad_insn = true;
1779 bad_insn |= insn_validate (op);
1782 if ((ppc_cpu & op->flags) != 0
1783 && !(ppc_cpu & op->deprecated)
1784 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1786 as_bad (_("duplicate %s"), op->name);
1787 bad_insn = true;
1791 /* SPE2 instructions */
1792 if ((ppc_cpu & PPC_OPCODE_SPE2) == PPC_OPCODE_SPE2)
1794 op_end = spe2_opcodes + spe2_num_opcodes;
1795 for (op = spe2_opcodes; op < op_end; op++)
1797 if (ENABLE_CHECKING)
1799 if (op != spe2_opcodes)
1801 unsigned old_seg, new_seg;
1803 old_seg = VLE_OP (op[-1].opcode, op[-1].mask);
1804 old_seg = VLE_OP_TO_SEG (old_seg);
1805 new_seg = VLE_OP (op[0].opcode, op[0].mask);
1806 new_seg = VLE_OP_TO_SEG (new_seg);
1808 /* The major opcodes had better be sorted. Code in the
1809 disassembler assumes the insns are sorted according to
1810 major opcode. */
1811 if (new_seg < old_seg)
1813 as_bad (_("major opcode is not sorted for %s"), op->name);
1814 bad_insn = true;
1818 bad_insn |= insn_validate (op);
1821 if ((ppc_cpu & op->flags) != 0
1822 && !(ppc_cpu & op->deprecated)
1823 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1825 as_bad (_("duplicate %s"), op->name);
1826 bad_insn = true;
1830 for (op = spe2_opcodes; op < op_end; op++)
1831 str_hash_insert (ppc_hash, op->name, op, 0);
1834 if (bad_insn)
1835 abort ();
1838 /* This function is called when the assembler starts up. It is called
1839 after the options have been parsed and the output file has been
1840 opened. */
1842 void
1843 md_begin (void)
1845 ppc_set_cpu ();
1847 ppc_cie_data_alignment = ppc_obj64 ? -8 : -4;
1848 ppc_dwarf2_line_min_insn_length = (ppc_cpu & PPC_OPCODE_VLE) ? 2 : 4;
1850 #ifdef OBJ_ELF
1851 /* Set the ELF flags if desired. */
1852 if (ppc_flags && !msolaris)
1853 bfd_set_private_flags (stdoutput, ppc_flags);
1854 #endif
1856 ppc_setup_opcodes ();
1858 /* Tell the main code what the endianness is if it is not overridden
1859 by the user. */
1860 if (!set_target_endian)
1862 set_target_endian = 1;
1863 target_big_endian = PPC_BIG_ENDIAN;
1866 #ifdef OBJ_XCOFF
1867 ppc_coff_debug_section = coff_section_from_bfd_index (stdoutput, N_DEBUG);
1869 /* Create XCOFF sections with .text in first, as it's creating dummy symbols
1870 to serve as initial csects. This forces the text csects to precede the
1871 data csects. These symbols will not be output. */
1872 ppc_init_xcoff_section (&ppc_xcoff_text_section, text_section);
1873 ppc_init_xcoff_section (&ppc_xcoff_data_section, data_section);
1874 ppc_init_xcoff_section (&ppc_xcoff_bss_section, bss_section);
1875 #endif
1878 void
1879 ppc_cleanup (void)
1881 #ifdef OBJ_ELF
1882 if (ppc_apuinfo_list == NULL)
1883 return;
1885 /* Ok, so write the section info out. We have this layout:
1887 byte data what
1888 ---- ---- ----
1889 0 8 length of "APUinfo\0"
1890 4 (n*4) number of APU's (4 bytes each)
1891 8 2 note type 2
1892 12 "APUinfo\0" name
1893 20 APU#1 first APU's info
1894 24 APU#2 second APU's info
1895 ... ...
1898 char *p;
1899 asection *seg = now_seg;
1900 subsegT subseg = now_subseg;
1901 asection *apuinfo_secp = (asection *) NULL;
1902 unsigned int i;
1904 /* Create the .PPC.EMB.apuinfo section. */
1905 apuinfo_secp = subseg_new (APUINFO_SECTION_NAME, 0);
1906 bfd_set_section_flags (apuinfo_secp, SEC_HAS_CONTENTS | SEC_READONLY);
1908 p = frag_more (4);
1909 md_number_to_chars (p, (valueT) 8, 4);
1911 p = frag_more (4);
1912 md_number_to_chars (p, (valueT) ppc_apuinfo_num * 4, 4);
1914 p = frag_more (4);
1915 md_number_to_chars (p, (valueT) 2, 4);
1917 p = frag_more (8);
1918 strcpy (p, APUINFO_LABEL);
1920 for (i = 0; i < ppc_apuinfo_num; i++)
1922 p = frag_more (4);
1923 md_number_to_chars (p, (valueT) ppc_apuinfo_list[i], 4);
1926 frag_align (2, 0, 0);
1928 /* We probably can't restore the current segment, for there likely
1929 isn't one yet... */
1930 if (seg && subseg)
1931 subseg_set (seg, subseg);
1933 #endif
1936 /* Insert an operand value into an instruction. */
1938 static uint64_t
1939 ppc_insert_operand (uint64_t insn,
1940 const struct powerpc_operand *operand,
1941 int64_t val,
1942 ppc_cpu_t cpu,
1943 const char *file,
1944 unsigned int line)
1946 int64_t min, max, right;
1948 max = operand->bitm;
1949 right = max & -max;
1950 min = 0;
1952 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0)
1954 /* Extend the allowed range for addis to [-32768, 65535].
1955 Similarly for cmpli and some VLE high part insns. For 64-bit
1956 it would be good to disable this for signed fields since the
1957 value is sign extended into the high 32 bits of the register.
1958 If the value is, say, an address, then we might care about
1959 the high bits. However, gcc as of 2014-06 uses unsigned
1960 values when loading the high part of 64-bit constants using
1961 lis. */
1962 min = ~(max >> 1) & -right;
1964 else if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
1966 max = (max >> 1) & -right;
1967 min = ~max & -right;
1970 if ((operand->flags & PPC_OPERAND_PLUS1) != 0)
1971 max++;
1973 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
1975 int64_t tmp = min;
1976 min = -max;
1977 max = -tmp;
1980 if (min <= max)
1982 /* Some people write constants with the sign extension done by
1983 hand but only up to 32 bits. This shouldn't really be valid,
1984 but, to permit this code to assemble on a 64-bit host, we
1985 sign extend the 32-bit value to 64 bits if so doing makes the
1986 value valid. We only do this for operands that are 32-bits or
1987 smaller. */
1988 if (val > max
1989 && (operand->bitm & ~0xffffffffULL) == 0
1990 && (val - (1LL << 32)) >= min
1991 && (val - (1LL << 32)) <= max
1992 && ((val - (1LL << 32)) & (right - 1)) == 0)
1993 val = val - (1LL << 32);
1995 /* Similarly, people write expressions like ~(1<<15), and expect
1996 this to be OK for a 32-bit unsigned value. */
1997 else if (val < min
1998 && (operand->bitm & ~0xffffffffULL) == 0
1999 && (val + (1LL << 32)) >= min
2000 && (val + (1LL << 32)) <= max
2001 && ((val + (1LL << 32)) & (right - 1)) == 0)
2002 val = val + (1LL << 32);
2004 else if (val < min
2005 || val > max
2006 || (val & (right - 1)) != 0)
2007 as_bad_value_out_of_range (_("operand"), val, min, max, file, line);
2010 if (operand->insert)
2012 const char *errmsg;
2014 errmsg = NULL;
2015 insn = (*operand->insert) (insn, val, cpu, &errmsg);
2016 if (errmsg != (const char *) NULL)
2017 as_bad_where (file, line, "%s", errmsg);
2019 else if (operand->shift >= 0)
2020 insn |= (val & operand->bitm) << operand->shift;
2021 else
2022 insn |= (val & operand->bitm) >> -operand->shift;
2024 return insn;
2028 #ifdef OBJ_ELF
2029 /* Parse @got, etc. and return the desired relocation. */
2030 static bfd_reloc_code_real_type
2031 ppc_elf_suffix (char **str_p, expressionS *exp_p)
2033 struct map_bfd {
2034 const char *string;
2035 unsigned int length : 8;
2036 unsigned int valid32 : 1;
2037 unsigned int valid64 : 1;
2038 unsigned int reloc;
2041 char ident[20];
2042 char *str = *str_p;
2043 char *str2;
2044 int ch;
2045 int len;
2046 const struct map_bfd *ptr;
2048 #define MAP(str, reloc) { str, sizeof (str) - 1, 1, 1, reloc }
2049 #define MAP32(str, reloc) { str, sizeof (str) - 1, 1, 0, reloc }
2050 #define MAP64(str, reloc) { str, sizeof (str) - 1, 0, 1, reloc }
2052 static const struct map_bfd mapping[] = {
2053 MAP ("l", BFD_RELOC_LO16),
2054 MAP ("h", BFD_RELOC_HI16),
2055 MAP ("ha", BFD_RELOC_HI16_S),
2056 MAP ("brtaken", BFD_RELOC_PPC_B16_BRTAKEN),
2057 MAP ("brntaken", BFD_RELOC_PPC_B16_BRNTAKEN),
2058 MAP ("got", BFD_RELOC_16_GOTOFF),
2059 MAP ("got@l", BFD_RELOC_LO16_GOTOFF),
2060 MAP ("got@h", BFD_RELOC_HI16_GOTOFF),
2061 MAP ("got@ha", BFD_RELOC_HI16_S_GOTOFF),
2062 MAP ("plt@l", BFD_RELOC_LO16_PLTOFF),
2063 MAP ("plt@h", BFD_RELOC_HI16_PLTOFF),
2064 MAP ("plt@ha", BFD_RELOC_HI16_S_PLTOFF),
2065 MAP ("copy", BFD_RELOC_PPC_COPY),
2066 MAP ("globdat", BFD_RELOC_PPC_GLOB_DAT),
2067 MAP ("sectoff", BFD_RELOC_16_BASEREL),
2068 MAP ("sectoff@l", BFD_RELOC_LO16_BASEREL),
2069 MAP ("sectoff@h", BFD_RELOC_HI16_BASEREL),
2070 MAP ("sectoff@ha", BFD_RELOC_HI16_S_BASEREL),
2071 MAP ("tls", BFD_RELOC_PPC_TLS),
2072 MAP ("dtpmod", BFD_RELOC_PPC_DTPMOD),
2073 MAP ("dtprel", BFD_RELOC_PPC_DTPREL),
2074 MAP ("dtprel@l", BFD_RELOC_PPC_DTPREL16_LO),
2075 MAP ("dtprel@h", BFD_RELOC_PPC_DTPREL16_HI),
2076 MAP ("dtprel@ha", BFD_RELOC_PPC_DTPREL16_HA),
2077 MAP ("tprel", BFD_RELOC_PPC_TPREL),
2078 MAP ("tprel@l", BFD_RELOC_PPC_TPREL16_LO),
2079 MAP ("tprel@h", BFD_RELOC_PPC_TPREL16_HI),
2080 MAP ("tprel@ha", BFD_RELOC_PPC_TPREL16_HA),
2081 MAP ("got@tlsgd", BFD_RELOC_PPC_GOT_TLSGD16),
2082 MAP ("got@tlsgd@l", BFD_RELOC_PPC_GOT_TLSGD16_LO),
2083 MAP ("got@tlsgd@h", BFD_RELOC_PPC_GOT_TLSGD16_HI),
2084 MAP ("got@tlsgd@ha", BFD_RELOC_PPC_GOT_TLSGD16_HA),
2085 MAP ("got@tlsld", BFD_RELOC_PPC_GOT_TLSLD16),
2086 MAP ("got@tlsld@l", BFD_RELOC_PPC_GOT_TLSLD16_LO),
2087 MAP ("got@tlsld@h", BFD_RELOC_PPC_GOT_TLSLD16_HI),
2088 MAP ("got@tlsld@ha", BFD_RELOC_PPC_GOT_TLSLD16_HA),
2089 MAP ("got@dtprel", BFD_RELOC_PPC_GOT_DTPREL16),
2090 MAP ("got@dtprel@l", BFD_RELOC_PPC_GOT_DTPREL16_LO),
2091 MAP ("got@dtprel@h", BFD_RELOC_PPC_GOT_DTPREL16_HI),
2092 MAP ("got@dtprel@ha", BFD_RELOC_PPC_GOT_DTPREL16_HA),
2093 MAP ("got@tprel", BFD_RELOC_PPC_GOT_TPREL16),
2094 MAP ("got@tprel@l", BFD_RELOC_PPC_GOT_TPREL16_LO),
2095 MAP ("got@tprel@h", BFD_RELOC_PPC_GOT_TPREL16_HI),
2096 MAP ("got@tprel@ha", BFD_RELOC_PPC_GOT_TPREL16_HA),
2097 MAP32 ("fixup", BFD_RELOC_CTOR),
2098 MAP32 ("plt", BFD_RELOC_24_PLT_PCREL),
2099 MAP32 ("pltrel24", BFD_RELOC_24_PLT_PCREL),
2100 MAP32 ("local24pc", BFD_RELOC_PPC_LOCAL24PC),
2101 MAP32 ("local", BFD_RELOC_PPC_LOCAL24PC),
2102 MAP32 ("pltrel", BFD_RELOC_32_PLT_PCREL),
2103 MAP32 ("sdarel", BFD_RELOC_GPREL16),
2104 MAP32 ("sdarel@l", BFD_RELOC_PPC_VLE_SDAREL_LO16A),
2105 MAP32 ("sdarel@h", BFD_RELOC_PPC_VLE_SDAREL_HI16A),
2106 MAP32 ("sdarel@ha", BFD_RELOC_PPC_VLE_SDAREL_HA16A),
2107 MAP32 ("naddr", BFD_RELOC_PPC_EMB_NADDR32),
2108 MAP32 ("naddr16", BFD_RELOC_PPC_EMB_NADDR16),
2109 MAP32 ("naddr@l", BFD_RELOC_PPC_EMB_NADDR16_LO),
2110 MAP32 ("naddr@h", BFD_RELOC_PPC_EMB_NADDR16_HI),
2111 MAP32 ("naddr@ha", BFD_RELOC_PPC_EMB_NADDR16_HA),
2112 MAP32 ("sdai16", BFD_RELOC_PPC_EMB_SDAI16),
2113 MAP32 ("sda2rel", BFD_RELOC_PPC_EMB_SDA2REL),
2114 MAP32 ("sda2i16", BFD_RELOC_PPC_EMB_SDA2I16),
2115 MAP32 ("sda21", BFD_RELOC_PPC_EMB_SDA21),
2116 MAP32 ("sda21@l", BFD_RELOC_PPC_VLE_SDA21_LO),
2117 MAP32 ("mrkref", BFD_RELOC_PPC_EMB_MRKREF),
2118 MAP32 ("relsect", BFD_RELOC_PPC_EMB_RELSEC16),
2119 MAP32 ("relsect@l", BFD_RELOC_PPC_EMB_RELST_LO),
2120 MAP32 ("relsect@h", BFD_RELOC_PPC_EMB_RELST_HI),
2121 MAP32 ("relsect@ha", BFD_RELOC_PPC_EMB_RELST_HA),
2122 MAP32 ("bitfld", BFD_RELOC_PPC_EMB_BIT_FLD),
2123 MAP32 ("relsda", BFD_RELOC_PPC_EMB_RELSDA),
2124 MAP32 ("xgot", BFD_RELOC_PPC_TOC16),
2125 MAP64 ("high", BFD_RELOC_PPC64_ADDR16_HIGH),
2126 MAP64 ("higha", BFD_RELOC_PPC64_ADDR16_HIGHA),
2127 MAP64 ("higher", BFD_RELOC_PPC64_HIGHER),
2128 MAP64 ("highera", BFD_RELOC_PPC64_HIGHER_S),
2129 MAP64 ("highest", BFD_RELOC_PPC64_HIGHEST),
2130 MAP64 ("highesta", BFD_RELOC_PPC64_HIGHEST_S),
2131 MAP64 ("tocbase", BFD_RELOC_PPC64_TOC),
2132 MAP64 ("toc", BFD_RELOC_PPC_TOC16),
2133 MAP64 ("toc@l", BFD_RELOC_PPC64_TOC16_LO),
2134 MAP64 ("toc@h", BFD_RELOC_PPC64_TOC16_HI),
2135 MAP64 ("toc@ha", BFD_RELOC_PPC64_TOC16_HA),
2136 MAP64 ("dtprel@high", BFD_RELOC_PPC64_DTPREL16_HIGH),
2137 MAP64 ("dtprel@higha", BFD_RELOC_PPC64_DTPREL16_HIGHA),
2138 MAP64 ("dtprel@higher", BFD_RELOC_PPC64_DTPREL16_HIGHER),
2139 MAP64 ("dtprel@highera", BFD_RELOC_PPC64_DTPREL16_HIGHERA),
2140 MAP64 ("dtprel@highest", BFD_RELOC_PPC64_DTPREL16_HIGHEST),
2141 MAP64 ("dtprel@highesta", BFD_RELOC_PPC64_DTPREL16_HIGHESTA),
2142 MAP64 ("localentry", BFD_RELOC_PPC64_ADDR64_LOCAL),
2143 MAP64 ("tprel@high", BFD_RELOC_PPC64_TPREL16_HIGH),
2144 MAP64 ("tprel@higha", BFD_RELOC_PPC64_TPREL16_HIGHA),
2145 MAP64 ("tprel@higher", BFD_RELOC_PPC64_TPREL16_HIGHER),
2146 MAP64 ("tprel@highera", BFD_RELOC_PPC64_TPREL16_HIGHERA),
2147 MAP64 ("tprel@highest", BFD_RELOC_PPC64_TPREL16_HIGHEST),
2148 MAP64 ("tprel@highesta", BFD_RELOC_PPC64_TPREL16_HIGHESTA),
2149 MAP64 ("notoc", BFD_RELOC_PPC64_REL24_NOTOC),
2150 MAP64 ("pcrel", BFD_RELOC_PPC64_PCREL34),
2151 MAP64 ("got@pcrel", BFD_RELOC_PPC64_GOT_PCREL34),
2152 MAP64 ("plt@pcrel", BFD_RELOC_PPC64_PLT_PCREL34),
2153 MAP64 ("tls@pcrel", BFD_RELOC_PPC64_TLS_PCREL),
2154 MAP64 ("got@tlsgd@pcrel", BFD_RELOC_PPC64_GOT_TLSGD_PCREL34),
2155 MAP64 ("got@tlsld@pcrel", BFD_RELOC_PPC64_GOT_TLSLD_PCREL34),
2156 MAP64 ("got@tprel@pcrel", BFD_RELOC_PPC64_GOT_TPREL_PCREL34),
2157 MAP64 ("got@dtprel@pcrel", BFD_RELOC_PPC64_GOT_DTPREL_PCREL34),
2158 MAP64 ("higher34", BFD_RELOC_PPC64_ADDR16_HIGHER34),
2159 MAP64 ("highera34", BFD_RELOC_PPC64_ADDR16_HIGHERA34),
2160 MAP64 ("highest34", BFD_RELOC_PPC64_ADDR16_HIGHEST34),
2161 MAP64 ("highesta34", BFD_RELOC_PPC64_ADDR16_HIGHESTA34),
2162 { (char *) 0, 0, 0, 0, BFD_RELOC_NONE }
2165 if (*str++ != '@')
2166 return BFD_RELOC_NONE;
2168 for (ch = *str, str2 = ident;
2169 (str2 < ident + sizeof (ident) - 1
2170 && (ISALNUM (ch) || ch == '@'));
2171 ch = *++str)
2173 *str2++ = TOLOWER (ch);
2176 *str2 = '\0';
2177 len = str2 - ident;
2179 ch = ident[0];
2180 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
2181 if (ch == ptr->string[0]
2182 && len == ptr->length
2183 && memcmp (ident, ptr->string, ptr->length) == 0
2184 && (ppc_obj64 ? ptr->valid64 : ptr->valid32))
2186 int reloc = ptr->reloc;
2188 if (!ppc_obj64 && (exp_p->X_op == O_big || exp_p->X_add_number != 0))
2190 switch (reloc)
2192 case BFD_RELOC_16_GOTOFF:
2193 case BFD_RELOC_LO16_GOTOFF:
2194 case BFD_RELOC_HI16_GOTOFF:
2195 case BFD_RELOC_HI16_S_GOTOFF:
2196 as_warn (_("symbol+offset@%s means symbol@%s+offset"),
2197 ptr->string, ptr->string);
2198 break;
2200 case BFD_RELOC_PPC_GOT_TLSGD16:
2201 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
2202 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
2203 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
2204 case BFD_RELOC_PPC_GOT_TLSLD16:
2205 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
2206 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
2207 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
2208 case BFD_RELOC_PPC_GOT_DTPREL16:
2209 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
2210 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
2211 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
2212 case BFD_RELOC_PPC_GOT_TPREL16:
2213 case BFD_RELOC_PPC_GOT_TPREL16_LO:
2214 case BFD_RELOC_PPC_GOT_TPREL16_HI:
2215 case BFD_RELOC_PPC_GOT_TPREL16_HA:
2216 as_bad (_("symbol+offset@%s not supported"), ptr->string);
2217 break;
2221 /* Now check for identifier@suffix+constant. */
2222 if (*str == '-' || *str == '+')
2224 char *orig_line = input_line_pointer;
2225 expressionS new_exp;
2227 input_line_pointer = str;
2228 expression (&new_exp);
2229 if (new_exp.X_op == O_constant && exp_p->X_op != O_big)
2231 exp_p->X_add_number += new_exp.X_add_number;
2232 str = input_line_pointer;
2234 input_line_pointer = orig_line;
2236 *str_p = str;
2238 if (reloc == (int) BFD_RELOC_PPC64_TOC
2239 && exp_p->X_op == O_symbol
2240 && strcmp (S_GET_NAME (exp_p->X_add_symbol), ".TOC.") == 0)
2242 /* Change the symbol so that the dummy .TOC. symbol can be
2243 omitted from the object file. */
2244 exp_p->X_add_symbol = &abs_symbol;
2247 if (reloc == BFD_RELOC_PPC64_REL24_NOTOC
2248 && (ppc_cpu & PPC_OPCODE_POWER10) == 0)
2249 reloc = BFD_RELOC_PPC64_REL24_P9NOTOC;
2251 return (bfd_reloc_code_real_type) reloc;
2254 return BFD_RELOC_NONE;
2257 /* Support @got, etc. on constants emitted via .short, .int etc. */
2259 bfd_reloc_code_real_type
2260 ppc_elf_parse_cons (expressionS *exp, unsigned int nbytes)
2262 expression (exp);
2263 if (nbytes >= 2 && *input_line_pointer == '@')
2264 return ppc_elf_suffix (&input_line_pointer, exp);
2265 return BFD_RELOC_NONE;
2268 /* Warn when emitting data to code sections, unless we are emitting
2269 a relocation that ld --ppc476-workaround uses to recognise data
2270 *and* there was an unconditional branch prior to the data. */
2272 void
2273 ppc_elf_cons_fix_check (expressionS *exp ATTRIBUTE_UNUSED,
2274 unsigned int nbytes, fixS *fix)
2276 if (warn_476
2277 && (now_seg->flags & SEC_CODE) != 0
2278 && (nbytes != 4
2279 || fix == NULL
2280 || !(fix->fx_r_type == BFD_RELOC_32
2281 || fix->fx_r_type == BFD_RELOC_CTOR
2282 || fix->fx_r_type == BFD_RELOC_32_PCREL)
2283 || !(last_seg == now_seg && last_subseg == now_subseg)
2284 || !((last_insn & (0x3f << 26)) == (18u << 26)
2285 || ((last_insn & (0x3f << 26)) == (16u << 26)
2286 && (last_insn & (0x14 << 21)) == (0x14 << 21))
2287 || ((last_insn & (0x3f << 26)) == (19u << 26)
2288 && (last_insn & (0x3ff << 1)) == (16u << 1)
2289 && (last_insn & (0x14 << 21)) == (0x14 << 21)))))
2291 /* Flag that we've warned. */
2292 if (fix != NULL)
2293 fix->fx_tcbit = 1;
2295 as_warn (_("data in executable section"));
2299 /* Solaris pseduo op to change to the .rodata section. */
2300 static void
2301 ppc_elf_rdata (int xxx)
2303 char *save_line = input_line_pointer;
2304 static char section[] = ".rodata\n";
2306 /* Just pretend this is .section .rodata */
2307 input_line_pointer = section;
2308 obj_elf_section (xxx);
2310 input_line_pointer = save_line;
2313 /* Pseudo op to make file scope bss items. */
2314 static void
2315 ppc_elf_lcomm (int xxx ATTRIBUTE_UNUSED)
2317 char *name;
2318 char c;
2319 char *p;
2320 offsetT size;
2321 symbolS *symbolP;
2322 offsetT align;
2323 segT old_sec;
2324 int old_subsec;
2325 char *pfrag;
2326 int align2;
2328 c = get_symbol_name (&name);
2330 /* Just after name is now '\0'. */
2331 p = input_line_pointer;
2332 *p = c;
2333 SKIP_WHITESPACE_AFTER_NAME ();
2334 if (*input_line_pointer != ',')
2336 as_bad (_("expected comma after symbol-name: rest of line ignored."));
2337 ignore_rest_of_line ();
2338 return;
2341 input_line_pointer++; /* skip ',' */
2342 if ((size = get_absolute_expression ()) < 0)
2344 as_warn (_(".COMMon length (%ld.) <0! Ignored."), (long) size);
2345 ignore_rest_of_line ();
2346 return;
2349 /* The third argument to .lcomm is the alignment. */
2350 if (*input_line_pointer != ',')
2351 align = 8;
2352 else
2354 ++input_line_pointer;
2355 align = get_absolute_expression ();
2356 if (align <= 0)
2358 as_warn (_("ignoring bad alignment"));
2359 align = 8;
2363 *p = 0;
2364 symbolP = symbol_find_or_make (name);
2365 *p = c;
2367 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
2369 as_bad (_("ignoring attempt to re-define symbol `%s'."),
2370 S_GET_NAME (symbolP));
2371 ignore_rest_of_line ();
2372 return;
2375 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
2377 as_bad (_("length of .lcomm \"%s\" is already %ld. Not changed to %ld."),
2378 S_GET_NAME (symbolP),
2379 (long) S_GET_VALUE (symbolP),
2380 (long) size);
2382 ignore_rest_of_line ();
2383 return;
2386 /* Allocate_bss. */
2387 old_sec = now_seg;
2388 old_subsec = now_subseg;
2389 if (align)
2391 /* Convert to a power of 2 alignment. */
2392 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2);
2393 if (align != 1)
2395 as_bad (_("common alignment not a power of 2"));
2396 ignore_rest_of_line ();
2397 return;
2400 else
2401 align2 = 0;
2403 record_alignment (bss_section, align2);
2404 subseg_set (bss_section, 1);
2405 if (align2)
2406 frag_align (align2, 0, 0);
2407 if (S_GET_SEGMENT (symbolP) == bss_section)
2408 symbol_get_frag (symbolP)->fr_symbol = 0;
2409 symbol_set_frag (symbolP, frag_now);
2410 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
2411 (char *) 0);
2412 *pfrag = 0;
2413 S_SET_SIZE (symbolP, size);
2414 S_SET_SEGMENT (symbolP, bss_section);
2415 subseg_set (old_sec, old_subsec);
2416 demand_empty_rest_of_line ();
2419 /* Pseudo op to set symbol local entry point. */
2420 static void
2421 ppc_elf_localentry (int ignore ATTRIBUTE_UNUSED)
2423 char *name;
2424 char c = get_symbol_name (&name);
2425 char *p;
2426 expressionS exp;
2427 symbolS *sym;
2428 asymbol *bfdsym;
2429 elf_symbol_type *elfsym;
2431 p = input_line_pointer;
2432 *p = c;
2433 SKIP_WHITESPACE_AFTER_NAME ();
2434 if (*input_line_pointer != ',')
2436 *p = 0;
2437 as_bad (_("expected comma after name `%s' in .localentry directive"),
2438 name);
2439 *p = c;
2440 ignore_rest_of_line ();
2441 return;
2443 input_line_pointer++;
2444 expression (&exp);
2445 if (exp.X_op == O_absent)
2447 as_bad (_("missing expression in .localentry directive"));
2448 exp.X_op = O_constant;
2449 exp.X_add_number = 0;
2451 *p = 0;
2452 sym = symbol_find_or_make (name);
2453 *p = c;
2455 if (resolve_expression (&exp)
2456 && exp.X_op == O_constant)
2458 unsigned int encoded, ok;
2460 ok = 1;
2461 if (exp.X_add_number == 1 || exp.X_add_number == 7)
2462 encoded = exp.X_add_number << STO_PPC64_LOCAL_BIT;
2463 else
2465 encoded = PPC64_SET_LOCAL_ENTRY_OFFSET (exp.X_add_number);
2466 if (exp.X_add_number != (offsetT) PPC64_LOCAL_ENTRY_OFFSET (encoded))
2468 as_bad (_(".localentry expression for `%s' "
2469 "is not a valid power of 2"), S_GET_NAME (sym));
2470 ok = 0;
2473 if (ok)
2475 bfdsym = symbol_get_bfdsym (sym);
2476 elfsym = elf_symbol_from (bfdsym);
2477 gas_assert (elfsym);
2478 elfsym->internal_elf_sym.st_other &= ~STO_PPC64_LOCAL_MASK;
2479 elfsym->internal_elf_sym.st_other |= encoded;
2480 if (ppc_abiversion == 0)
2481 ppc_abiversion = 2;
2484 else
2485 as_bad (_(".localentry expression for `%s' "
2486 "does not evaluate to a constant"), S_GET_NAME (sym));
2488 demand_empty_rest_of_line ();
2491 /* Pseudo op to set ABI version. */
2492 static void
2493 ppc_elf_abiversion (int ignore ATTRIBUTE_UNUSED)
2495 expressionS exp;
2497 expression (&exp);
2498 if (exp.X_op == O_absent)
2500 as_bad (_("missing expression in .abiversion directive"));
2501 exp.X_op = O_constant;
2502 exp.X_add_number = 0;
2505 if (resolve_expression (&exp)
2506 && exp.X_op == O_constant)
2507 ppc_abiversion = exp.X_add_number;
2508 else
2509 as_bad (_(".abiversion expression does not evaluate to a constant"));
2510 demand_empty_rest_of_line ();
2513 /* Parse a .gnu_attribute directive. */
2514 static void
2515 ppc_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
2517 int tag = obj_elf_vendor_attribute (OBJ_ATTR_GNU);
2519 /* Check validity of defined powerpc tags. */
2520 if (tag == Tag_GNU_Power_ABI_FP
2521 || tag == Tag_GNU_Power_ABI_Vector
2522 || tag == Tag_GNU_Power_ABI_Struct_Return)
2524 unsigned int val;
2526 val = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU, tag);
2528 if ((tag == Tag_GNU_Power_ABI_FP && val > 15)
2529 || (tag == Tag_GNU_Power_ABI_Vector && val > 3)
2530 || (tag == Tag_GNU_Power_ABI_Struct_Return && val > 2))
2531 as_warn (_("unknown .gnu_attribute value"));
2535 /* Set ABI version in output file. */
2536 void
2537 ppc_elf_end (void)
2539 if (ppc_obj64 && ppc_abiversion != 0)
2541 elf_elfheader (stdoutput)->e_flags &= ~EF_PPC64_ABI;
2542 elf_elfheader (stdoutput)->e_flags |= ppc_abiversion & EF_PPC64_ABI;
2544 /* Any selection of opcodes based on ppc_cpu after gas has finished
2545 parsing the file is invalid. md_apply_fix and ppc_handle_align
2546 must select opcodes based on the machine in force at the point
2547 where the fixup or alignment frag was created, not the machine in
2548 force at the end of file. */
2549 ppc_cpu = 0;
2552 /* Validate any relocations emitted for -mrelocatable, possibly adding
2553 fixups for word relocations in writable segments, so we can adjust
2554 them at runtime. */
2555 static void
2556 ppc_elf_validate_fix (fixS *fixp, segT seg)
2558 if (fixp->fx_done || fixp->fx_pcrel)
2559 return;
2561 switch (shlib)
2563 case SHLIB_NONE:
2564 case SHLIB_PIC:
2565 return;
2567 case SHLIB_MRELOCATABLE:
2568 if (fixp->fx_r_type != BFD_RELOC_16_GOTOFF
2569 && fixp->fx_r_type != BFD_RELOC_HI16_GOTOFF
2570 && fixp->fx_r_type != BFD_RELOC_LO16_GOTOFF
2571 && fixp->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
2572 && fixp->fx_r_type != BFD_RELOC_16_BASEREL
2573 && fixp->fx_r_type != BFD_RELOC_LO16_BASEREL
2574 && fixp->fx_r_type != BFD_RELOC_HI16_BASEREL
2575 && fixp->fx_r_type != BFD_RELOC_HI16_S_BASEREL
2576 && (seg->flags & SEC_LOAD) != 0
2577 && strcmp (segment_name (seg), ".got2") != 0
2578 && strcmp (segment_name (seg), ".dtors") != 0
2579 && strcmp (segment_name (seg), ".ctors") != 0
2580 && strcmp (segment_name (seg), ".fixup") != 0
2581 && strcmp (segment_name (seg), ".gcc_except_table") != 0
2582 && strcmp (segment_name (seg), ".eh_frame") != 0
2583 && strcmp (segment_name (seg), ".ex_shared") != 0)
2585 if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
2586 || fixp->fx_r_type != BFD_RELOC_CTOR)
2588 as_bad_where (fixp->fx_file, fixp->fx_line,
2589 _("relocation cannot be done when using -mrelocatable"));
2592 return;
2596 /* Prevent elf_frob_file_before_adjust removing a weak undefined
2597 function descriptor sym if the corresponding code sym is used. */
2599 void
2600 ppc_frob_file_before_adjust (void)
2602 symbolS *symp;
2603 asection *toc;
2605 if (!ppc_obj64)
2606 return;
2608 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2610 const char *name;
2611 char *dotname;
2612 symbolS *dotsym;
2614 name = S_GET_NAME (symp);
2615 if (name[0] == '.')
2616 continue;
2618 if (! S_IS_WEAK (symp)
2619 || S_IS_DEFINED (symp))
2620 continue;
2622 dotname = concat (".", name, (char *) NULL);
2623 dotsym = symbol_find_noref (dotname, 1);
2624 free (dotname);
2625 if (dotsym != NULL && (symbol_used_p (dotsym)
2626 || symbol_used_in_reloc_p (dotsym)))
2627 symbol_mark_used (symp);
2631 toc = bfd_get_section_by_name (stdoutput, ".toc");
2632 if (toc != NULL
2633 && toc_reloc_types != has_large_toc_reloc
2634 && bfd_section_size (toc) > 0x10000)
2635 as_warn (_("TOC section size exceeds 64k"));
2638 /* .TOC. used in an opd entry as .TOC.@tocbase doesn't need to be
2639 emitted. Other uses of .TOC. will cause the symbol to be marked
2640 with BSF_KEEP in md_apply_fix. */
2642 void
2643 ppc_elf_adjust_symtab (void)
2645 if (ppc_obj64)
2647 symbolS *symp;
2648 symp = symbol_find (".TOC.");
2649 if (symp != NULL)
2651 asymbol *bsym = symbol_get_bfdsym (symp);
2652 if ((bsym->flags & BSF_KEEP) == 0)
2653 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2657 #endif /* OBJ_ELF */
2659 #ifdef OBJ_XCOFF
2660 /* Parse XCOFF relocations. */
2661 static bfd_reloc_code_real_type
2662 ppc_xcoff_suffix (char **str_p)
2664 struct map_bfd {
2665 const char *string;
2666 unsigned int length : 8;
2667 unsigned int valid32 : 1;
2668 unsigned int valid64 : 1;
2669 unsigned int reloc;
2672 char ident[20];
2673 char *str = *str_p;
2674 char *str2;
2675 int ch;
2676 int len;
2677 const struct map_bfd *ptr;
2679 #define MAP(str, reloc) { str, sizeof (str) - 1, 1, 1, reloc }
2680 #define MAP32(str, reloc) { str, sizeof (str) - 1, 1, 0, reloc }
2681 #define MAP64(str, reloc) { str, sizeof (str) - 1, 0, 1, reloc }
2683 static const struct map_bfd mapping[] = {
2684 MAP ("l", BFD_RELOC_PPC_TOC16_LO),
2685 MAP ("u", BFD_RELOC_PPC_TOC16_HI),
2686 MAP32 ("ie", BFD_RELOC_PPC_TLSIE),
2687 MAP32 ("ld", BFD_RELOC_PPC_TLSLD),
2688 MAP32 ("le", BFD_RELOC_PPC_TLSLE),
2689 MAP32 ("m", BFD_RELOC_PPC_TLSM),
2690 MAP32 ("ml", BFD_RELOC_PPC_TLSML),
2691 MAP64 ("ie", BFD_RELOC_PPC64_TLSIE),
2692 MAP64 ("ld", BFD_RELOC_PPC64_TLSLD),
2693 MAP64 ("le", BFD_RELOC_PPC64_TLSLE),
2694 MAP64 ("m", BFD_RELOC_PPC64_TLSM),
2695 MAP64 ("ml", BFD_RELOC_PPC64_TLSML),
2698 if (*str++ != '@')
2699 return BFD_RELOC_NONE;
2701 for (ch = *str, str2 = ident;
2702 (str2 < ident + sizeof (ident) - 1
2703 && (ISALNUM (ch) || ch == '@'));
2704 ch = *++str)
2706 *str2++ = TOLOWER (ch);
2709 *str2 = '\0';
2710 len = str2 - ident;
2712 ch = ident[0];
2713 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
2714 if (ch == ptr->string[0]
2715 && len == ptr->length
2716 && memcmp (ident, ptr->string, ptr->length) == 0
2717 && (ppc_obj64 ? ptr->valid64 : ptr->valid32))
2719 *str_p = str;
2720 return (bfd_reloc_code_real_type) ptr->reloc;
2723 return BFD_RELOC_NONE;
2726 /* Restore XCOFF addis instruction to ELF format.
2727 AIX often generates addis instructions using "addis RT,D(RA)"
2728 format instead of the ELF "addis RT,RA,SI" one.
2729 On entry RT_E is at the comma after RT, D_E is at the open
2730 parenthesis after D, and RA_E is at the close parenthesis after RA. */
2731 static void
2732 ppc_xcoff_fixup_addis (char *rt_e, char *d_e, char *ra_e)
2734 size_t ra_size = ra_e - d_e - 1;
2735 char *save_ra = xmalloc (ra_size);
2737 /* Copy RA. */
2738 memcpy (save_ra, d_e + 1, ra_size);
2739 /* Shuffle D to make room for RA, copying the comma too. */
2740 memmove (rt_e + ra_size + 1, rt_e, d_e - rt_e);
2741 /* Erase the trailing ')', keeping any rubbish for potential errors. */
2742 memmove (ra_e, ra_e + 1, strlen (ra_e));
2743 /* Write RA back. */
2744 memcpy (rt_e + 1, save_ra, ra_size);
2745 free (save_ra);
2748 /* Support @ie, etc. on constants emitted via .short, .int etc. */
2750 bfd_reloc_code_real_type
2751 ppc_xcoff_parse_cons (expressionS *exp, unsigned int nbytes)
2753 expression (exp);
2754 if (nbytes >= 2 && *input_line_pointer == '@')
2755 return ppc_xcoff_suffix (&input_line_pointer);
2757 /* There isn't any @ symbol for default TLS relocations (R_TLS). */
2758 if (exp->X_add_symbol != NULL
2759 && (symbol_get_tc (exp->X_add_symbol)->symbol_class == XMC_TL
2760 || symbol_get_tc (exp->X_add_symbol)->symbol_class == XMC_UL))
2761 return (ppc_obj64 ? BFD_RELOC_PPC64_TLSGD: BFD_RELOC_PPC_TLSGD);
2763 return BFD_RELOC_NONE;
2766 #endif /* OBJ_XCOFF */
2768 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
2769 /* See whether a symbol is in the TOC section. */
2771 static int
2772 ppc_is_toc_sym (symbolS *sym)
2774 #ifdef OBJ_XCOFF
2775 return (symbol_get_tc (sym)->symbol_class == XMC_TC
2776 || symbol_get_tc (sym)->symbol_class == XMC_TE
2777 || symbol_get_tc (sym)->symbol_class == XMC_TC0);
2778 #endif
2779 #ifdef OBJ_ELF
2780 const char *sname = segment_name (S_GET_SEGMENT (sym));
2781 if (ppc_obj64)
2782 return strcmp (sname, ".toc") == 0;
2783 else
2784 return strcmp (sname, ".got") == 0;
2785 #endif
2787 #endif /* defined (OBJ_XCOFF) || defined (OBJ_ELF) */
2790 #ifdef OBJ_ELF
2791 #define APUID(a,v) ((((a) & 0xffff) << 16) | ((v) & 0xffff))
2792 static void
2793 ppc_apuinfo_section_add (unsigned int apu, unsigned int version)
2795 unsigned int i;
2797 /* Check we don't already exist. */
2798 for (i = 0; i < ppc_apuinfo_num; i++)
2799 if (ppc_apuinfo_list[i] == APUID (apu, version))
2800 return;
2802 if (ppc_apuinfo_num == ppc_apuinfo_num_alloc)
2804 if (ppc_apuinfo_num_alloc == 0)
2806 ppc_apuinfo_num_alloc = 4;
2807 ppc_apuinfo_list = XNEWVEC (unsigned long, ppc_apuinfo_num_alloc);
2809 else
2811 ppc_apuinfo_num_alloc += 4;
2812 ppc_apuinfo_list = XRESIZEVEC (unsigned long, ppc_apuinfo_list,
2813 ppc_apuinfo_num_alloc);
2816 ppc_apuinfo_list[ppc_apuinfo_num++] = APUID (apu, version);
2818 #undef APUID
2819 #endif
2821 /* Various frobbings of labels and their addresses. */
2823 /* Symbols labelling the current insn. */
2824 struct insn_label_list
2826 struct insn_label_list *next;
2827 symbolS *label;
2830 static struct insn_label_list *insn_labels;
2831 static struct insn_label_list *free_insn_labels;
2833 static void
2834 ppc_record_label (symbolS *sym)
2836 struct insn_label_list *l;
2838 if (free_insn_labels == NULL)
2839 l = XNEW (struct insn_label_list);
2840 else
2842 l = free_insn_labels;
2843 free_insn_labels = l->next;
2846 l->label = sym;
2847 l->next = insn_labels;
2848 insn_labels = l;
2851 static void
2852 ppc_clear_labels (void)
2854 while (insn_labels != NULL)
2856 struct insn_label_list *l = insn_labels;
2857 insn_labels = l->next;
2858 l->next = free_insn_labels;
2859 free_insn_labels = l;
2863 void
2864 ppc_start_line_hook (void)
2866 ppc_clear_labels ();
2869 void
2870 ppc_new_dot_label (symbolS *sym)
2872 ppc_record_label (sym);
2873 #ifdef OBJ_XCOFF
2874 /* Anchor this label to the current csect for relocations. */
2875 symbol_get_tc (sym)->within = ppc_current_csect;
2876 #endif
2879 void
2880 ppc_frob_label (symbolS *sym)
2882 ppc_record_label (sym);
2884 #ifdef OBJ_XCOFF
2885 /* Set the class of a label based on where it is defined. This handles
2886 symbols without suffixes. Also, move the symbol so that it follows
2887 the csect symbol. */
2888 if (ppc_current_csect != (symbolS *) NULL)
2890 if (symbol_get_tc (sym)->symbol_class == -1)
2891 symbol_get_tc (sym)->symbol_class = symbol_get_tc (ppc_current_csect)->symbol_class;
2893 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2894 symbol_append (sym, symbol_get_tc (ppc_current_csect)->within,
2895 &symbol_rootP, &symbol_lastP);
2896 /* Update last csect symbol. */
2897 symbol_get_tc (ppc_current_csect)->within = sym;
2899 /* Some labels like .bs are using within differently.
2900 So avoid changing it, if it's already set. */
2901 if (symbol_get_tc (sym)->within == NULL)
2902 symbol_get_tc (sym)->within = ppc_current_csect;
2904 #endif
2906 #ifdef OBJ_ELF
2907 dwarf2_emit_label (sym);
2908 #endif
2911 /* We need to keep a list of fixups. We can't simply generate them as
2912 we go, because that would require us to first create the frag, and
2913 that would screw up references to ``.''. */
2915 struct ppc_fixup
2917 expressionS exp;
2918 int opindex;
2919 bfd_reloc_code_real_type reloc;
2922 #define MAX_INSN_FIXUPS (5)
2924 /* Return the field size operated on by RELOC, and whether it is
2925 pc-relative in PC_RELATIVE. */
2927 static unsigned int
2928 fixup_size (bfd_reloc_code_real_type reloc, bool *pc_relative)
2930 unsigned int size = 0;
2931 bool pcrel = false;
2933 switch (reloc)
2935 /* This switch statement must handle all BFD_RELOC values
2936 possible in instruction fixups. As is, it handles all
2937 BFD_RELOC values used in bfd/elf64-ppc.c, bfd/elf32-ppc.c,
2938 bfd/coff-rs6000.c and bfd/coff64-rs6000.c.
2939 Overkill since data and marker relocs need not be handled
2940 here, but this way we can be sure a needed fixup reloc isn't
2941 accidentally omitted. */
2942 case BFD_RELOC_PPC_EMB_MRKREF:
2943 case BFD_RELOC_VTABLE_ENTRY:
2944 case BFD_RELOC_VTABLE_INHERIT:
2945 break;
2947 case BFD_RELOC_8:
2948 size = 1;
2949 break;
2951 case BFD_RELOC_16:
2952 case BFD_RELOC_16_BASEREL:
2953 case BFD_RELOC_16_GOTOFF:
2954 case BFD_RELOC_GPREL16:
2955 case BFD_RELOC_HI16:
2956 case BFD_RELOC_HI16_BASEREL:
2957 case BFD_RELOC_HI16_GOTOFF:
2958 case BFD_RELOC_HI16_PLTOFF:
2959 case BFD_RELOC_HI16_S:
2960 case BFD_RELOC_HI16_S_BASEREL:
2961 case BFD_RELOC_HI16_S_GOTOFF:
2962 case BFD_RELOC_HI16_S_PLTOFF:
2963 case BFD_RELOC_LO16:
2964 case BFD_RELOC_LO16_BASEREL:
2965 case BFD_RELOC_LO16_GOTOFF:
2966 case BFD_RELOC_LO16_PLTOFF:
2967 case BFD_RELOC_PPC64_ADDR16_DS:
2968 case BFD_RELOC_PPC64_ADDR16_HIGH:
2969 case BFD_RELOC_PPC64_ADDR16_HIGHA:
2970 case BFD_RELOC_PPC64_ADDR16_HIGHER34:
2971 case BFD_RELOC_PPC64_ADDR16_HIGHERA34:
2972 case BFD_RELOC_PPC64_ADDR16_HIGHEST34:
2973 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:
2974 case BFD_RELOC_PPC64_ADDR16_LO_DS:
2975 case BFD_RELOC_PPC64_DTPREL16_DS:
2976 case BFD_RELOC_PPC64_DTPREL16_HIGH:
2977 case BFD_RELOC_PPC64_DTPREL16_HIGHA:
2978 case BFD_RELOC_PPC64_DTPREL16_HIGHER:
2979 case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
2980 case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
2981 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
2982 case BFD_RELOC_PPC64_DTPREL16_LO_DS:
2983 case BFD_RELOC_PPC64_GOT16_DS:
2984 case BFD_RELOC_PPC64_GOT16_LO_DS:
2985 case BFD_RELOC_PPC64_HIGHER:
2986 case BFD_RELOC_PPC64_HIGHER_S:
2987 case BFD_RELOC_PPC64_HIGHEST:
2988 case BFD_RELOC_PPC64_HIGHEST_S:
2989 case BFD_RELOC_PPC64_PLT16_LO_DS:
2990 case BFD_RELOC_PPC64_PLTGOT16:
2991 case BFD_RELOC_PPC64_PLTGOT16_DS:
2992 case BFD_RELOC_PPC64_PLTGOT16_HA:
2993 case BFD_RELOC_PPC64_PLTGOT16_HI:
2994 case BFD_RELOC_PPC64_PLTGOT16_LO:
2995 case BFD_RELOC_PPC64_PLTGOT16_LO_DS:
2996 case BFD_RELOC_PPC64_SECTOFF_DS:
2997 case BFD_RELOC_PPC64_SECTOFF_LO_DS:
2998 case BFD_RELOC_PPC64_TOC16_DS:
2999 case BFD_RELOC_PPC64_TOC16_HA:
3000 case BFD_RELOC_PPC64_TOC16_HI:
3001 case BFD_RELOC_PPC64_TOC16_LO:
3002 case BFD_RELOC_PPC64_TOC16_LO_DS:
3003 case BFD_RELOC_PPC64_TPREL16_DS:
3004 case BFD_RELOC_PPC64_TPREL16_HIGH:
3005 case BFD_RELOC_PPC64_TPREL16_HIGHA:
3006 case BFD_RELOC_PPC64_TPREL16_HIGHER:
3007 case BFD_RELOC_PPC64_TPREL16_HIGHERA:
3008 case BFD_RELOC_PPC64_TPREL16_HIGHEST:
3009 case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
3010 case BFD_RELOC_PPC64_TPREL16_LO_DS:
3011 #ifdef OBJ_XCOFF
3012 case BFD_RELOC_PPC_BA16:
3013 #endif
3014 case BFD_RELOC_PPC_DTPREL16:
3015 case BFD_RELOC_PPC_DTPREL16_HA:
3016 case BFD_RELOC_PPC_DTPREL16_HI:
3017 case BFD_RELOC_PPC_DTPREL16_LO:
3018 case BFD_RELOC_PPC_EMB_NADDR16:
3019 case BFD_RELOC_PPC_EMB_NADDR16_HA:
3020 case BFD_RELOC_PPC_EMB_NADDR16_HI:
3021 case BFD_RELOC_PPC_EMB_NADDR16_LO:
3022 case BFD_RELOC_PPC_EMB_RELSDA:
3023 case BFD_RELOC_PPC_EMB_RELSEC16:
3024 case BFD_RELOC_PPC_EMB_RELST_LO:
3025 case BFD_RELOC_PPC_EMB_RELST_HI:
3026 case BFD_RELOC_PPC_EMB_RELST_HA:
3027 case BFD_RELOC_PPC_EMB_SDA2I16:
3028 case BFD_RELOC_PPC_EMB_SDA2REL:
3029 case BFD_RELOC_PPC_EMB_SDAI16:
3030 case BFD_RELOC_PPC_GOT_DTPREL16:
3031 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
3032 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
3033 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
3034 case BFD_RELOC_PPC_GOT_TLSGD16:
3035 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
3036 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
3037 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
3038 case BFD_RELOC_PPC_GOT_TLSLD16:
3039 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
3040 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
3041 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
3042 case BFD_RELOC_PPC_GOT_TPREL16:
3043 case BFD_RELOC_PPC_GOT_TPREL16_HA:
3044 case BFD_RELOC_PPC_GOT_TPREL16_HI:
3045 case BFD_RELOC_PPC_GOT_TPREL16_LO:
3046 case BFD_RELOC_PPC_TOC16:
3047 case BFD_RELOC_PPC_TOC16_HI:
3048 case BFD_RELOC_PPC_TOC16_LO:
3049 case BFD_RELOC_PPC_TPREL16:
3050 case BFD_RELOC_PPC_TPREL16_HA:
3051 case BFD_RELOC_PPC_TPREL16_HI:
3052 case BFD_RELOC_PPC_TPREL16_LO:
3053 size = 2;
3054 break;
3056 case BFD_RELOC_16_PCREL:
3057 case BFD_RELOC_HI16_PCREL:
3058 case BFD_RELOC_HI16_S_PCREL:
3059 case BFD_RELOC_LO16_PCREL:
3060 case BFD_RELOC_PPC64_REL16_HIGH:
3061 case BFD_RELOC_PPC64_REL16_HIGHA:
3062 case BFD_RELOC_PPC64_REL16_HIGHER:
3063 case BFD_RELOC_PPC64_REL16_HIGHER34:
3064 case BFD_RELOC_PPC64_REL16_HIGHERA:
3065 case BFD_RELOC_PPC64_REL16_HIGHERA34:
3066 case BFD_RELOC_PPC64_REL16_HIGHEST:
3067 case BFD_RELOC_PPC64_REL16_HIGHEST34:
3068 case BFD_RELOC_PPC64_REL16_HIGHESTA:
3069 case BFD_RELOC_PPC64_REL16_HIGHESTA34:
3070 #ifdef OBJ_XCOFF
3071 case BFD_RELOC_PPC_B16:
3072 #endif
3073 case BFD_RELOC_PPC_VLE_REL8:
3074 size = 2;
3075 pcrel = true;
3076 break;
3078 case BFD_RELOC_32:
3079 case BFD_RELOC_32_PLTOFF:
3080 #ifdef OBJ_XCOFF
3081 case BFD_RELOC_CTOR:
3082 #endif
3083 case BFD_RELOC_PPC64_ENTRY:
3084 case BFD_RELOC_PPC_16DX_HA:
3085 #ifndef OBJ_XCOFF
3086 case BFD_RELOC_PPC_BA16:
3087 #endif
3088 case BFD_RELOC_PPC_BA16_BRNTAKEN:
3089 case BFD_RELOC_PPC_BA16_BRTAKEN:
3090 case BFD_RELOC_PPC_BA26:
3091 case BFD_RELOC_PPC_EMB_BIT_FLD:
3092 case BFD_RELOC_PPC_EMB_NADDR32:
3093 case BFD_RELOC_PPC_EMB_SDA21:
3094 case BFD_RELOC_PPC_TLS:
3095 case BFD_RELOC_PPC_TLSGD:
3096 case BFD_RELOC_PPC_TLSLD:
3097 case BFD_RELOC_PPC_TLSLE:
3098 case BFD_RELOC_PPC_TLSIE:
3099 case BFD_RELOC_PPC_TLSM:
3100 case BFD_RELOC_PPC_TLSML:
3101 case BFD_RELOC_PPC_VLE_HA16A:
3102 case BFD_RELOC_PPC_VLE_HA16D:
3103 case BFD_RELOC_PPC_VLE_HI16A:
3104 case BFD_RELOC_PPC_VLE_HI16D:
3105 case BFD_RELOC_PPC_VLE_LO16A:
3106 case BFD_RELOC_PPC_VLE_LO16D:
3107 case BFD_RELOC_PPC_VLE_SDA21:
3108 case BFD_RELOC_PPC_VLE_SDA21_LO:
3109 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
3110 case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
3111 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
3112 case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
3113 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
3114 case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
3115 case BFD_RELOC_PPC64_TLS_PCREL:
3116 case BFD_RELOC_RVA:
3117 size = 4;
3118 break;
3120 case BFD_RELOC_24_PLT_PCREL:
3121 case BFD_RELOC_32_PCREL:
3122 case BFD_RELOC_32_PLT_PCREL:
3123 case BFD_RELOC_PPC64_REL24_NOTOC:
3124 case BFD_RELOC_PPC64_REL24_P9NOTOC:
3125 #ifndef OBJ_XCOFF
3126 case BFD_RELOC_PPC_B16:
3127 #endif
3128 case BFD_RELOC_PPC_B16_BRNTAKEN:
3129 case BFD_RELOC_PPC_B16_BRTAKEN:
3130 case BFD_RELOC_PPC_B26:
3131 case BFD_RELOC_PPC_LOCAL24PC:
3132 case BFD_RELOC_PPC_REL16DX_HA:
3133 case BFD_RELOC_PPC_VLE_REL15:
3134 case BFD_RELOC_PPC_VLE_REL24:
3135 size = 4;
3136 pcrel = true;
3137 break;
3139 #ifndef OBJ_XCOFF
3140 case BFD_RELOC_CTOR:
3141 #endif
3142 case BFD_RELOC_PPC_COPY:
3143 case BFD_RELOC_PPC_DTPMOD:
3144 case BFD_RELOC_PPC_DTPREL:
3145 case BFD_RELOC_PPC_GLOB_DAT:
3146 case BFD_RELOC_PPC_TPREL:
3147 size = ppc_obj64 ? 8 : 4;
3148 break;
3150 case BFD_RELOC_64:
3151 case BFD_RELOC_64_PLTOFF:
3152 case BFD_RELOC_PPC64_ADDR64_LOCAL:
3153 case BFD_RELOC_PPC64_D28:
3154 case BFD_RELOC_PPC64_D34:
3155 case BFD_RELOC_PPC64_D34_LO:
3156 case BFD_RELOC_PPC64_D34_HI30:
3157 case BFD_RELOC_PPC64_D34_HA30:
3158 case BFD_RELOC_PPC64_TPREL34:
3159 case BFD_RELOC_PPC64_DTPREL34:
3160 case BFD_RELOC_PPC64_TOC:
3161 case BFD_RELOC_PPC64_TLSGD:
3162 case BFD_RELOC_PPC64_TLSLD:
3163 case BFD_RELOC_PPC64_TLSLE:
3164 case BFD_RELOC_PPC64_TLSIE:
3165 case BFD_RELOC_PPC64_TLSM:
3166 case BFD_RELOC_PPC64_TLSML:
3167 size = 8;
3168 break;
3170 case BFD_RELOC_64_PCREL:
3171 case BFD_RELOC_64_PLT_PCREL:
3172 case BFD_RELOC_PPC64_GOT_PCREL34:
3173 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:
3174 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:
3175 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:
3176 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:
3177 case BFD_RELOC_PPC64_PCREL28:
3178 case BFD_RELOC_PPC64_PCREL34:
3179 case BFD_RELOC_PPC64_PLT_PCREL34:
3180 size = 8;
3181 pcrel = true;
3182 break;
3184 default:
3185 abort ();
3188 if (ENABLE_CHECKING)
3190 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
3191 if (reloc_howto != NULL
3192 && (size != bfd_get_reloc_size (reloc_howto)
3193 || pcrel != reloc_howto->pc_relative))
3195 as_bad (_("%s howto doesn't match size/pcrel in gas"),
3196 reloc_howto->name);
3197 abort ();
3200 *pc_relative = pcrel;
3201 return size;
3204 #ifdef OBJ_ELF
3205 /* If we have parsed a call to __tls_get_addr, parse an argument like
3206 (gd0@tlsgd). *STR is the leading parenthesis on entry. If an arg
3207 is successfully parsed, *STR is updated past the trailing
3208 parenthesis and trailing white space, and *TLS_FIX contains the
3209 reloc and arg expression. */
3211 static int
3212 parse_tls_arg (char **str, const expressionS *exp, struct ppc_fixup *tls_fix)
3214 const char *sym_name = S_GET_NAME (exp->X_add_symbol);
3215 if (sym_name[0] == '.')
3216 ++sym_name;
3218 tls_fix->reloc = BFD_RELOC_NONE;
3219 if (strncasecmp (sym_name, "__tls_get_addr", 14) == 0
3220 && (sym_name[14] == 0
3221 || strcasecmp (sym_name + 14, "_desc") == 0
3222 || strcasecmp (sym_name + 14, "_opt") == 0))
3224 char *hold = input_line_pointer;
3225 input_line_pointer = *str + 1;
3226 expression (&tls_fix->exp);
3227 if (tls_fix->exp.X_op == O_symbol)
3229 if (strncasecmp (input_line_pointer, "@tlsgd)", 7) == 0)
3230 tls_fix->reloc = BFD_RELOC_PPC_TLSGD;
3231 else if (strncasecmp (input_line_pointer, "@tlsld)", 7) == 0)
3232 tls_fix->reloc = BFD_RELOC_PPC_TLSLD;
3233 if (tls_fix->reloc != BFD_RELOC_NONE)
3235 input_line_pointer += 7;
3236 SKIP_WHITESPACE ();
3237 *str = input_line_pointer;
3240 input_line_pointer = hold;
3242 return tls_fix->reloc != BFD_RELOC_NONE;
3244 #endif
3246 /* This routine is called for each instruction to be assembled. */
3248 void
3249 md_assemble (char *str)
3251 char *s;
3252 const struct powerpc_opcode *opcode;
3253 uint64_t insn;
3254 const ppc_opindex_t *opindex_ptr;
3255 int need_paren;
3256 int next_opindex;
3257 struct ppc_fixup fixups[MAX_INSN_FIXUPS];
3258 int fc;
3259 char *f;
3260 int addr_mask;
3261 int i;
3262 unsigned int insn_length;
3264 /* Get the opcode. */
3265 for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
3267 if (*s != '\0')
3268 *s++ = '\0';
3270 /* Look up the opcode in the hash table. */
3271 opcode = (const struct powerpc_opcode *) str_hash_find (ppc_hash, str);
3272 if (opcode == (const struct powerpc_opcode *) NULL)
3274 as_bad (_("unrecognized opcode: `%s'"), str);
3275 ppc_clear_labels ();
3276 return;
3279 insn = opcode->opcode;
3280 if (!target_big_endian
3281 && ((insn & ~(1 << 26)) == 46u << 26
3282 || (insn & ~(0xc0 << 1)) == (31u << 26 | 533 << 1)))
3284 /* lmw, stmw, lswi, lswx, stswi, stswx */
3285 as_bad (_("`%s' invalid when little-endian"), str);
3286 ppc_clear_labels ();
3287 return;
3290 str = s;
3291 while (ISSPACE (*str))
3292 ++str;
3294 #ifdef OBJ_XCOFF
3295 /* AIX often generates addis instructions using "addis RT, D(RA)"
3296 format instead of the classic "addis RT, RA, SI" one.
3297 Restore it to the default format as it's the one encoded
3298 in ppc opcodes. */
3299 if (!strcmp (opcode->name, "addis"))
3301 char *rt_e = strchr (str, ',');
3302 if (rt_e != NULL
3303 && strchr (rt_e + 1, ',') == NULL)
3305 char *d_e = strchr (rt_e + 1, '(');
3306 if (d_e != NULL && d_e != rt_e + 1)
3308 char *ra_e = strrchr (d_e + 1, ')');
3309 if (ra_e != NULL && ra_e != d_e + 1)
3310 ppc_xcoff_fixup_addis (rt_e, d_e, ra_e);
3314 #endif
3316 /* PowerPC operands are just expressions. The only real issue is
3317 that a few operand types are optional. If an instruction has
3318 multiple optional operands and one is omitted, then all optional
3319 operands past the first omitted one must also be omitted. */
3320 int num_optional_operands = 0;
3321 int num_optional_provided = 0;
3323 /* Gather the operands. */
3324 need_paren = 0;
3325 next_opindex = 0;
3326 fc = 0;
3327 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
3329 const struct powerpc_operand *operand;
3330 const char *errmsg;
3331 char *hold;
3332 expressionS ex;
3333 char endc;
3335 if (next_opindex == 0)
3336 operand = &powerpc_operands[*opindex_ptr];
3337 else
3339 operand = &powerpc_operands[next_opindex];
3340 next_opindex = 0;
3342 errmsg = NULL;
3344 /* If this is an optional operand, and we are skipping it, just
3345 insert the default value, usually a zero. */
3346 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
3347 && !((operand->flags & PPC_OPERAND_OPTIONAL32) != 0 && ppc_obj64))
3349 if (num_optional_operands == 0)
3351 const ppc_opindex_t *optr;
3352 int total = 0;
3353 int provided = 0;
3354 int omitted;
3356 s = str;
3357 for (optr = opindex_ptr; *optr != 0; optr++)
3359 const struct powerpc_operand *op;
3360 op = &powerpc_operands[*optr];
3362 ++total;
3364 if ((op->flags & PPC_OPERAND_OPTIONAL) != 0
3365 && !((op->flags & PPC_OPERAND_OPTIONAL32) != 0
3366 && ppc_obj64))
3367 ++num_optional_operands;
3369 if (s != NULL && *s != '\0')
3371 ++provided;
3373 /* Look for the start of the next operand. */
3374 if ((op->flags & PPC_OPERAND_PARENS) != 0)
3375 s = strpbrk (s, "(,");
3376 else
3377 s = strchr (s, ',');
3379 if (s != NULL)
3380 ++s;
3383 omitted = total - provided;
3384 num_optional_provided = num_optional_operands - omitted;
3386 if (--num_optional_provided < 0)
3388 uint64_t val = ppc_optional_operand_value (operand, insn, ppc_cpu,
3389 num_optional_provided);
3390 if (operand->insert)
3392 insn = (*operand->insert) (insn, val, ppc_cpu, &errmsg);
3393 if (errmsg != (const char *) NULL)
3394 as_bad ("%s", errmsg);
3396 else if (operand->shift >= 0)
3397 insn |= (val & operand->bitm) << operand->shift;
3398 else
3399 insn |= (val & operand->bitm) >> -operand->shift;
3401 if ((operand->flags & PPC_OPERAND_NEXT) != 0)
3402 next_opindex = *opindex_ptr + 1;
3403 continue;
3407 /* Gather the operand. */
3408 hold = input_line_pointer;
3409 input_line_pointer = str;
3411 if ((reg_names_p
3412 && (((operand->flags & PPC_OPERAND_CR_BIT) != 0)
3413 || ((operand->flags & PPC_OPERAND_CR_REG) != 0)))
3414 || !register_name (&ex))
3416 char save_lex = lex_type['%'];
3418 if (((operand->flags & PPC_OPERAND_CR_REG) != 0)
3419 || (operand->flags & PPC_OPERAND_CR_BIT) != 0)
3421 cr_operand = true;
3422 lex_type['%'] |= LEX_BEGIN_NAME;
3424 expression (&ex);
3425 cr_operand = false;
3426 lex_type['%'] = save_lex;
3429 str = input_line_pointer;
3430 input_line_pointer = hold;
3432 if (ex.X_op == O_illegal)
3433 as_bad (_("illegal operand"));
3434 else if (ex.X_op == O_absent)
3435 as_bad (_("missing operand"));
3436 else if (ex.X_op == O_register)
3438 if ((ex.X_md
3439 & ~operand->flags
3440 & (PPC_OPERAND_GPR | PPC_OPERAND_FPR | PPC_OPERAND_VR
3441 | PPC_OPERAND_VSR | PPC_OPERAND_CR_BIT | PPC_OPERAND_CR_REG
3442 | PPC_OPERAND_SPR | PPC_OPERAND_GQR | PPC_OPERAND_ACC)) != 0
3443 && !((ex.X_md & PPC_OPERAND_GPR) != 0
3444 && ex.X_add_number != 0
3445 && (operand->flags & PPC_OPERAND_GPR_0) != 0))
3446 as_warn (_("invalid register expression"));
3447 insn = ppc_insert_operand (insn, operand, ex.X_add_number,
3448 ppc_cpu, (char *) NULL, 0);
3450 else if (ex.X_op == O_constant
3451 || (ex.X_op == O_big && ex.X_add_number > 0))
3453 uint64_t val;
3454 if (ex.X_op == O_constant)
3456 val = ex.X_add_number;
3457 if (sizeof (ex.X_add_number) < sizeof (val)
3458 && (ex.X_add_number < 0) != ex.X_extrabit)
3459 val = val ^ ((addressT) -1 ^ (uint64_t) -1);
3461 else
3462 val = generic_bignum_to_int64 ();
3463 #ifdef OBJ_ELF
3464 /* Allow @HA, @L, @H on constants. */
3465 char *orig_str = str;
3466 bfd_reloc_code_real_type reloc = ppc_elf_suffix (&str, &ex);
3468 if (ex.X_op == O_constant)
3470 val = ex.X_add_number;
3471 if (sizeof (ex.X_add_number) < sizeof (val)
3472 && (ex.X_add_number < 0) != ex.X_extrabit)
3473 val = val ^ ((addressT) -1 ^ (uint64_t) -1);
3475 if (reloc != BFD_RELOC_NONE)
3476 switch (reloc)
3478 default:
3479 str = orig_str;
3480 break;
3482 case BFD_RELOC_LO16:
3483 val &= 0xffff;
3484 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3485 val = SEX16 (val);
3486 break;
3488 case BFD_RELOC_HI16:
3489 if (REPORT_OVERFLOW_HI && ppc_obj64)
3491 /* PowerPC64 @h is tested for overflow. */
3492 val = val >> 16;
3493 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3495 uint64_t sign = (((uint64_t) -1 >> 16) + 1) >> 1;
3496 val = (val ^ sign) - sign;
3498 break;
3500 /* Fallthru */
3502 case BFD_RELOC_PPC64_ADDR16_HIGH:
3503 val = PPC_HI (val);
3504 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3505 val = SEX16 (val);
3506 break;
3508 case BFD_RELOC_HI16_S:
3509 if (REPORT_OVERFLOW_HI && ppc_obj64)
3511 /* PowerPC64 @ha is tested for overflow. */
3512 val = (val + 0x8000) >> 16;
3513 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3515 uint64_t sign = (((uint64_t) -1 >> 16) + 1) >> 1;
3516 val = (val ^ sign) - sign;
3518 break;
3520 /* Fallthru */
3522 case BFD_RELOC_PPC64_ADDR16_HIGHA:
3523 val = PPC_HA (val);
3524 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3525 val = SEX16 (val);
3526 break;
3528 case BFD_RELOC_PPC64_HIGHER:
3529 val = PPC_HIGHER (val);
3530 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3531 val = SEX16 (val);
3532 break;
3534 case BFD_RELOC_PPC64_HIGHER_S:
3535 val = PPC_HIGHERA (val);
3536 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3537 val = SEX16 (val);
3538 break;
3540 case BFD_RELOC_PPC64_HIGHEST:
3541 val = PPC_HIGHEST (val);
3542 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3543 val = SEX16 (val);
3544 break;
3546 case BFD_RELOC_PPC64_HIGHEST_S:
3547 val = PPC_HIGHESTA (val);
3548 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3549 val = SEX16 (val);
3550 break;
3552 #endif /* OBJ_ELF */
3553 insn = ppc_insert_operand (insn, operand, val, ppc_cpu, NULL, 0);
3555 else
3557 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
3558 #ifdef OBJ_ELF
3559 /* Look for a __tls_get_addr arg using the insane old syntax. */
3560 if (ex.X_op == O_symbol && *str == '(' && fc < MAX_INSN_FIXUPS
3561 && parse_tls_arg (&str, &ex, &fixups[fc]))
3563 fixups[fc].opindex = *opindex_ptr;
3564 ++fc;
3567 if ((reloc = ppc_elf_suffix (&str, &ex)) != BFD_RELOC_NONE)
3569 /* If VLE-mode convert LO/HI/HA relocations. */
3570 if (opcode->flags & PPC_OPCODE_VLE)
3572 uint64_t tmp_insn = insn & opcode->mask;
3574 int use_a_reloc = (tmp_insn == E_OR2I_INSN
3575 || tmp_insn == E_AND2I_DOT_INSN
3576 || tmp_insn == E_OR2IS_INSN
3577 || tmp_insn == E_LI_INSN
3578 || tmp_insn == E_LIS_INSN
3579 || tmp_insn == E_AND2IS_DOT_INSN);
3582 int use_d_reloc = (tmp_insn == E_ADD2I_DOT_INSN
3583 || tmp_insn == E_ADD2IS_INSN
3584 || tmp_insn == E_CMP16I_INSN
3585 || tmp_insn == E_MULL2I_INSN
3586 || tmp_insn == E_CMPL16I_INSN
3587 || tmp_insn == E_CMPH16I_INSN
3588 || tmp_insn == E_CMPHL16I_INSN);
3590 switch (reloc)
3592 default:
3593 break;
3595 case BFD_RELOC_PPC_EMB_SDA21:
3596 reloc = BFD_RELOC_PPC_VLE_SDA21;
3597 break;
3599 case BFD_RELOC_LO16:
3600 if (use_d_reloc)
3601 reloc = BFD_RELOC_PPC_VLE_LO16D;
3602 else if (use_a_reloc)
3603 reloc = BFD_RELOC_PPC_VLE_LO16A;
3604 break;
3606 case BFD_RELOC_HI16:
3607 if (use_d_reloc)
3608 reloc = BFD_RELOC_PPC_VLE_HI16D;
3609 else if (use_a_reloc)
3610 reloc = BFD_RELOC_PPC_VLE_HI16A;
3611 break;
3613 case BFD_RELOC_HI16_S:
3614 if (use_d_reloc)
3615 reloc = BFD_RELOC_PPC_VLE_HA16D;
3616 else if (use_a_reloc)
3617 reloc = BFD_RELOC_PPC_VLE_HA16A;
3618 break;
3620 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
3621 if (use_d_reloc)
3622 reloc = BFD_RELOC_PPC_VLE_SDAREL_LO16D;
3623 break;
3625 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
3626 if (use_d_reloc)
3627 reloc = BFD_RELOC_PPC_VLE_SDAREL_HI16D;
3628 break;
3630 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
3631 if (use_d_reloc)
3632 reloc = BFD_RELOC_PPC_VLE_SDAREL_HA16D;
3633 break;
3637 /* TLS and other tweaks. */
3638 switch (reloc)
3640 default:
3641 break;
3643 case BFD_RELOC_PPC_TLS:
3644 case BFD_RELOC_PPC64_TLS_PCREL:
3645 if (!_bfd_elf_ppc_at_tls_transform (opcode->opcode, 0))
3646 as_bad (_("@tls may not be used with \"%s\" operands"),
3647 opcode->name);
3648 else if (operand->shift != 11)
3649 as_bad (_("@tls may only be used in last operand"));
3650 else
3651 insn = ppc_insert_operand (insn, operand,
3652 ppc_obj64 ? 13 : 2,
3653 ppc_cpu, (char *) NULL, 0);
3654 break;
3656 /* We'll only use the 32 (or 64) bit form of these relocations
3657 in constants. Instructions get the 16 or 34 bit form. */
3658 case BFD_RELOC_PPC_DTPREL:
3659 if (operand->bitm == 0x3ffffffffULL)
3660 reloc = BFD_RELOC_PPC64_DTPREL34;
3661 else
3662 reloc = BFD_RELOC_PPC_DTPREL16;
3663 break;
3665 case BFD_RELOC_PPC_TPREL:
3666 if (operand->bitm == 0x3ffffffffULL)
3667 reloc = BFD_RELOC_PPC64_TPREL34;
3668 else
3669 reloc = BFD_RELOC_PPC_TPREL16;
3670 break;
3672 case BFD_RELOC_PPC64_PCREL34:
3673 if (operand->bitm == 0xfffffffULL)
3675 reloc = BFD_RELOC_PPC64_PCREL28;
3676 break;
3678 /* Fall through. */
3679 case BFD_RELOC_PPC64_GOT_PCREL34:
3680 case BFD_RELOC_PPC64_PLT_PCREL34:
3681 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:
3682 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:
3683 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:
3684 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:
3685 if (operand->bitm != 0x3ffffffffULL
3686 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
3687 as_warn (_("%s unsupported on this instruction"), "@pcrel");
3688 break;
3690 case BFD_RELOC_LO16:
3691 if (operand->bitm == 0x3ffffffffULL
3692 && (operand->flags & PPC_OPERAND_NEGATIVE) == 0)
3693 reloc = BFD_RELOC_PPC64_D34_LO;
3694 else if ((operand->bitm | 0xf) != 0xffff
3695 || operand->shift != 0
3696 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
3697 as_warn (_("%s unsupported on this instruction"), "@l");
3698 break;
3700 case BFD_RELOC_HI16:
3701 if (operand->bitm == 0x3ffffffffULL
3702 && (operand->flags & PPC_OPERAND_NEGATIVE) == 0)
3703 reloc = BFD_RELOC_PPC64_D34_HI30;
3704 else if (operand->bitm != 0xffff
3705 || operand->shift != 0
3706 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
3707 as_warn (_("%s unsupported on this instruction"), "@h");
3708 break;
3710 case BFD_RELOC_HI16_S:
3711 if (operand->bitm == 0x3ffffffffULL
3712 && (operand->flags & PPC_OPERAND_NEGATIVE) == 0)
3713 reloc = BFD_RELOC_PPC64_D34_HA30;
3714 else if (operand->bitm == 0xffff
3715 && operand->shift == (int) PPC_OPSHIFT_INV
3716 && opcode->opcode == (19 << 26) + (2 << 1))
3717 /* addpcis. */
3718 reloc = BFD_RELOC_PPC_16DX_HA;
3719 else if (operand->bitm != 0xffff
3720 || operand->shift != 0
3721 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
3722 as_warn (_("%s unsupported on this instruction"), "@ha");
3725 #endif /* OBJ_ELF */
3726 #ifdef OBJ_XCOFF
3727 reloc = ppc_xcoff_suffix (&str);
3728 #endif /* OBJ_XCOFF */
3730 if (reloc != BFD_RELOC_NONE)
3732 /* Determine a BFD reloc value based on the operand information.
3733 We are only prepared to turn a few of the operands into
3734 relocs. */
3735 else if ((operand->flags & (PPC_OPERAND_RELATIVE
3736 | PPC_OPERAND_ABSOLUTE)) != 0
3737 && operand->bitm == 0x3fffffc
3738 && operand->shift == 0)
3739 reloc = BFD_RELOC_PPC_B26;
3740 else if ((operand->flags & (PPC_OPERAND_RELATIVE
3741 | PPC_OPERAND_ABSOLUTE)) != 0
3742 && operand->bitm == 0xfffc
3743 && operand->shift == 0)
3744 reloc = BFD_RELOC_PPC_B16;
3745 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3746 && operand->bitm == 0x1fe
3747 && operand->shift == -1)
3748 reloc = BFD_RELOC_PPC_VLE_REL8;
3749 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3750 && operand->bitm == 0xfffe
3751 && operand->shift == 0)
3752 reloc = BFD_RELOC_PPC_VLE_REL15;
3753 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3754 && operand->bitm == 0x1fffffe
3755 && operand->shift == 0)
3756 reloc = BFD_RELOC_PPC_VLE_REL24;
3757 else if ((operand->flags & PPC_OPERAND_NEGATIVE) == 0
3758 && (operand->bitm & 0xfff0) == 0xfff0
3759 && operand->shift == 0)
3761 reloc = BFD_RELOC_16;
3762 #if defined OBJ_XCOFF || defined OBJ_ELF
3763 /* Note: the symbol may be not yet defined. */
3764 if ((operand->flags & PPC_OPERAND_PARENS) != 0
3765 && ppc_is_toc_sym (ex.X_add_symbol))
3767 reloc = BFD_RELOC_PPC_TOC16;
3768 #ifdef OBJ_ELF
3769 as_warn (_("assuming %s on symbol"),
3770 ppc_obj64 ? "@toc" : "@xgot");
3771 #endif
3773 #endif
3775 else if (operand->bitm == 0x3ffffffffULL)
3776 reloc = BFD_RELOC_PPC64_D34;
3777 else if (operand->bitm == 0xfffffffULL)
3778 reloc = BFD_RELOC_PPC64_D28;
3780 /* For the absolute forms of branches, convert the PC
3781 relative form back into the absolute. */
3782 if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
3784 switch (reloc)
3786 case BFD_RELOC_PPC_B26:
3787 reloc = BFD_RELOC_PPC_BA26;
3788 break;
3789 case BFD_RELOC_PPC_B16:
3790 reloc = BFD_RELOC_PPC_BA16;
3791 break;
3792 #ifdef OBJ_ELF
3793 case BFD_RELOC_PPC_B16_BRTAKEN:
3794 reloc = BFD_RELOC_PPC_BA16_BRTAKEN;
3795 break;
3796 case BFD_RELOC_PPC_B16_BRNTAKEN:
3797 reloc = BFD_RELOC_PPC_BA16_BRNTAKEN;
3798 break;
3799 #endif
3800 default:
3801 break;
3805 #ifdef OBJ_ELF
3806 switch (reloc)
3808 case BFD_RELOC_PPC_TOC16:
3809 toc_reloc_types |= has_small_toc_reloc;
3810 break;
3811 case BFD_RELOC_PPC64_TOC16_LO:
3812 case BFD_RELOC_PPC64_TOC16_HI:
3813 case BFD_RELOC_PPC64_TOC16_HA:
3814 toc_reloc_types |= has_large_toc_reloc;
3815 break;
3816 default:
3817 break;
3820 if (ppc_obj64
3821 && (operand->flags & (PPC_OPERAND_DS | PPC_OPERAND_DQ)) != 0)
3823 switch (reloc)
3825 case BFD_RELOC_16:
3826 reloc = BFD_RELOC_PPC64_ADDR16_DS;
3827 break;
3829 case BFD_RELOC_LO16:
3830 reloc = BFD_RELOC_PPC64_ADDR16_LO_DS;
3831 break;
3833 case BFD_RELOC_16_GOTOFF:
3834 reloc = BFD_RELOC_PPC64_GOT16_DS;
3835 break;
3837 case BFD_RELOC_LO16_GOTOFF:
3838 reloc = BFD_RELOC_PPC64_GOT16_LO_DS;
3839 break;
3841 case BFD_RELOC_LO16_PLTOFF:
3842 reloc = BFD_RELOC_PPC64_PLT16_LO_DS;
3843 break;
3845 case BFD_RELOC_16_BASEREL:
3846 reloc = BFD_RELOC_PPC64_SECTOFF_DS;
3847 break;
3849 case BFD_RELOC_LO16_BASEREL:
3850 reloc = BFD_RELOC_PPC64_SECTOFF_LO_DS;
3851 break;
3853 case BFD_RELOC_PPC_TOC16:
3854 reloc = BFD_RELOC_PPC64_TOC16_DS;
3855 break;
3857 case BFD_RELOC_PPC64_TOC16_LO:
3858 reloc = BFD_RELOC_PPC64_TOC16_LO_DS;
3859 break;
3861 case BFD_RELOC_PPC64_PLTGOT16:
3862 reloc = BFD_RELOC_PPC64_PLTGOT16_DS;
3863 break;
3865 case BFD_RELOC_PPC64_PLTGOT16_LO:
3866 reloc = BFD_RELOC_PPC64_PLTGOT16_LO_DS;
3867 break;
3869 case BFD_RELOC_PPC_DTPREL16:
3870 reloc = BFD_RELOC_PPC64_DTPREL16_DS;
3871 break;
3873 case BFD_RELOC_PPC_DTPREL16_LO:
3874 reloc = BFD_RELOC_PPC64_DTPREL16_LO_DS;
3875 break;
3877 case BFD_RELOC_PPC_TPREL16:
3878 reloc = BFD_RELOC_PPC64_TPREL16_DS;
3879 break;
3881 case BFD_RELOC_PPC_TPREL16_LO:
3882 reloc = BFD_RELOC_PPC64_TPREL16_LO_DS;
3883 break;
3885 case BFD_RELOC_PPC_GOT_DTPREL16:
3886 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
3887 case BFD_RELOC_PPC_GOT_TPREL16:
3888 case BFD_RELOC_PPC_GOT_TPREL16_LO:
3889 break;
3891 default:
3892 as_bad (_("unsupported relocation for DS offset field"));
3893 break;
3897 /* Look for a __tls_get_addr arg after any __tls_get_addr
3898 modifiers like @plt. This fixup must be emitted before
3899 the usual call fixup. */
3900 if (ex.X_op == O_symbol && *str == '(' && fc < MAX_INSN_FIXUPS
3901 && parse_tls_arg (&str, &ex, &fixups[fc]))
3903 fixups[fc].opindex = *opindex_ptr;
3904 ++fc;
3906 #endif
3908 /* We need to generate a fixup for this expression. */
3909 if (fc >= MAX_INSN_FIXUPS)
3910 as_fatal (_("too many fixups"));
3911 fixups[fc].exp = ex;
3912 fixups[fc].opindex = *opindex_ptr;
3913 fixups[fc].reloc = reloc;
3914 ++fc;
3917 if (need_paren)
3919 endc = ')';
3920 need_paren = 0;
3921 /* If expecting more operands, then we want to see "),". */
3922 if (*str == endc && opindex_ptr[1] != 0)
3925 ++str;
3926 while (ISSPACE (*str));
3927 endc = ',';
3930 else if ((operand->flags & PPC_OPERAND_PARENS) != 0)
3931 endc = '(';
3932 else
3933 endc = ',';
3935 /* The call to expression should have advanced str past any
3936 whitespace. */
3937 if (*str == endc)
3939 ++str;
3940 if (endc == '(')
3941 need_paren = 1;
3943 else if (*str != '\0')
3945 as_bad (_("syntax error; found `%c', expected `%c'"), *str, endc);
3946 break;
3948 else if (endc == ')')
3950 as_bad (_("syntax error; end of line, expected `%c'"), endc);
3951 break;
3955 while (ISSPACE (*str))
3956 ++str;
3958 if (*str != '\0')
3959 as_bad (_("junk at end of line: `%s'"), str);
3961 #ifdef OBJ_ELF
3962 /* Do we need/want an APUinfo section? */
3963 if ((ppc_cpu & (PPC_OPCODE_E500 | PPC_OPCODE_E500MC | PPC_OPCODE_VLE)) != 0
3964 && !ppc_obj64)
3966 /* These are all version "1". */
3967 if (opcode->flags & PPC_OPCODE_SPE)
3968 ppc_apuinfo_section_add (PPC_APUINFO_SPE, 1);
3969 if (opcode->flags & PPC_OPCODE_ISEL)
3970 ppc_apuinfo_section_add (PPC_APUINFO_ISEL, 1);
3971 if (opcode->flags & PPC_OPCODE_EFS)
3972 ppc_apuinfo_section_add (PPC_APUINFO_EFS, 1);
3973 if (opcode->flags & PPC_OPCODE_BRLOCK)
3974 ppc_apuinfo_section_add (PPC_APUINFO_BRLOCK, 1);
3975 if (opcode->flags & PPC_OPCODE_PMR)
3976 ppc_apuinfo_section_add (PPC_APUINFO_PMR, 1);
3977 if (opcode->flags & PPC_OPCODE_CACHELCK)
3978 ppc_apuinfo_section_add (PPC_APUINFO_CACHELCK, 1);
3979 if (opcode->flags & PPC_OPCODE_RFMCI)
3980 ppc_apuinfo_section_add (PPC_APUINFO_RFMCI, 1);
3981 /* Only set the VLE flag if the instruction has been pulled via
3982 the VLE instruction set. This way the flag is guaranteed to
3983 be set for VLE-only instructions or for VLE-only processors,
3984 however it'll remain clear for dual-mode instructions on
3985 dual-mode and, more importantly, standard-mode processors. */
3986 if ((ppc_cpu & opcode->flags) == PPC_OPCODE_VLE)
3988 ppc_apuinfo_section_add (PPC_APUINFO_VLE, 1);
3989 if (elf_section_data (now_seg) != NULL)
3990 elf_section_data (now_seg)->this_hdr.sh_flags |= SHF_PPC_VLE;
3993 #endif
3995 /* Write out the instruction. */
3997 addr_mask = 3;
3998 if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
3999 /* All instructions can start on a 2 byte boundary for VLE. */
4000 addr_mask = 1;
4002 if (frag_now->insn_addr != addr_mask)
4004 /* Don't emit instructions to a frag started for data, or for a
4005 CPU differing in VLE mode. Data is allowed to be misaligned,
4006 and it's possible to start a new frag in the middle of
4007 misaligned data. */
4008 frag_wane (frag_now);
4009 frag_new (0);
4012 /* Check that insns within the frag are aligned. ppc_frag_check
4013 will ensure that the frag start address is aligned. */
4014 if ((frag_now_fix () & addr_mask) != 0)
4015 as_bad (_("instruction address is not a multiple of %d"), addr_mask + 1);
4017 /* Differentiate between two, four, and eight byte insns. */
4018 insn_length = 4;
4019 if ((ppc_cpu & PPC_OPCODE_VLE) != 0 && PPC_OP_SE_VLE (insn))
4020 insn_length = 2;
4021 else if ((opcode->flags & PPC_OPCODE_POWER10) != 0
4022 && PPC_PREFIX_P (insn))
4024 struct insn_label_list *l;
4026 insn_length = 8;
4028 /* 8-byte prefix instructions are not allowed to cross 64-byte
4029 boundaries. */
4030 frag_align_code (6, 4);
4031 record_alignment (now_seg, 6);
4032 #ifdef OBJ_XCOFF
4033 /* Update alignment of the containing csect. */
4034 if (symbol_get_tc (ppc_current_csect)->align < 6)
4035 symbol_get_tc (ppc_current_csect)->align = 6;
4036 #endif
4038 /* Update "dot" in any expressions used by this instruction, and
4039 a label attached to the instruction. By "attached" we mean
4040 on the same source line as the instruction and without any
4041 intervening semicolons. */
4042 dot_value = frag_now_fix ();
4043 dot_frag = frag_now;
4044 for (l = insn_labels; l != NULL; l = l->next)
4046 symbol_set_frag (l->label, dot_frag);
4047 S_SET_VALUE (l->label, dot_value);
4051 ppc_clear_labels ();
4053 f = frag_more (insn_length);
4054 frag_now->insn_addr = addr_mask;
4056 /* The prefix part of an 8-byte instruction always occupies the lower
4057 addressed word in a doubleword, regardless of endianness. */
4058 if (insn_length == 8
4059 && (sizeof (insn) > sizeof (valueT) || !target_big_endian))
4061 md_number_to_chars (f, PPC_GET_PREFIX (insn), 4);
4062 md_number_to_chars (f + 4, PPC_GET_SUFFIX (insn), 4);
4064 else
4065 md_number_to_chars (f, insn, insn_length);
4067 last_insn = insn;
4068 last_seg = now_seg;
4069 last_subseg = now_subseg;
4071 #ifdef OBJ_ELF
4072 dwarf2_emit_insn (insn_length);
4073 #endif
4075 /* Create any fixups. */
4076 for (i = 0; i < fc; i++)
4078 fixS *fixP;
4079 if (fixups[i].reloc != BFD_RELOC_NONE)
4081 bool pcrel;
4082 unsigned int size = fixup_size (fixups[i].reloc, &pcrel);
4083 int offset = target_big_endian ? (insn_length - size) : 0;
4085 fixP = fix_new_exp (frag_now,
4086 f - frag_now->fr_literal + offset,
4087 size,
4088 &fixups[i].exp,
4089 pcrel,
4090 fixups[i].reloc);
4092 else
4094 const struct powerpc_operand *operand;
4096 operand = &powerpc_operands[fixups[i].opindex];
4097 fixP = fix_new_exp (frag_now,
4098 f - frag_now->fr_literal,
4099 insn_length,
4100 &fixups[i].exp,
4101 (operand->flags & PPC_OPERAND_RELATIVE) != 0,
4102 BFD_RELOC_NONE);
4104 fixP->fx_pcrel_adjust = fixups[i].opindex;
4108 #ifdef OBJ_ELF
4109 /* For ELF, add support for SHT_ORDERED. */
4112 ppc_section_type (char *str, size_t len)
4114 if (len == 7 && startswith (str, "ordered"))
4115 return SHT_ORDERED;
4117 return -1;
4121 ppc_section_flags (flagword flags, bfd_vma attr ATTRIBUTE_UNUSED, int type)
4123 if (type == SHT_ORDERED)
4124 flags |= SEC_ALLOC | SEC_LOAD | SEC_SORT_ENTRIES;
4126 return flags;
4129 bfd_vma
4130 ppc_elf_section_letter (int letter, const char **ptrmsg)
4132 if (letter == 'v')
4133 return SHF_PPC_VLE;
4135 *ptrmsg = _("bad .section directive: want a,e,v,w,x,M,S,G,T in string");
4136 return -1;
4138 #endif /* OBJ_ELF */
4141 /* Pseudo-op handling. */
4143 /* The .byte pseudo-op. This is similar to the normal .byte
4144 pseudo-op, but it can also take a single ASCII string. */
4146 static void
4147 ppc_byte (int ignore ATTRIBUTE_UNUSED)
4149 int count = 0;
4151 if (*input_line_pointer != '\"')
4153 cons (1);
4154 return;
4157 /* Gather characters. A real double quote is doubled. Unusual
4158 characters are not permitted. */
4159 ++input_line_pointer;
4160 while (1)
4162 char c;
4164 c = *input_line_pointer++;
4166 if (c == '\"')
4168 if (*input_line_pointer != '\"')
4169 break;
4170 ++input_line_pointer;
4173 FRAG_APPEND_1_CHAR (c);
4174 ++count;
4177 if (warn_476 && count != 0 && (now_seg->flags & SEC_CODE) != 0)
4178 as_warn (_("data in executable section"));
4179 demand_empty_rest_of_line ();
4182 #ifdef OBJ_XCOFF
4184 /* XCOFF specific pseudo-op handling. */
4186 /* This is set if we are creating a .stabx symbol, since we don't want
4187 to handle symbol suffixes for such symbols. */
4188 static bool ppc_stab_symbol;
4190 /* Retrieve the visiblity input for pseudo-ops having ones. */
4191 static unsigned short
4192 ppc_xcoff_get_visibility (void) {
4193 SKIP_WHITESPACE();
4195 if (startswith (input_line_pointer, "exported"))
4197 input_line_pointer += 8;
4198 return SYM_V_EXPORTED;
4201 if (startswith (input_line_pointer, "hidden"))
4203 input_line_pointer += 6;
4204 return SYM_V_HIDDEN;
4207 if (startswith (input_line_pointer, "internal"))
4209 input_line_pointer += 8;
4210 return SYM_V_INTERNAL;
4213 if (startswith (input_line_pointer, "protected"))
4215 input_line_pointer += 9;
4216 return SYM_V_PROTECTED;
4219 return 0;
4222 /* Retrieve visiblity using GNU syntax. */
4223 static void ppc_GNU_visibility (int visibility) {
4224 int c;
4225 char *name;
4226 symbolS *symbolP;
4227 coff_symbol_type *coffsym;
4231 if ((name = read_symbol_name ()) == NULL)
4232 break;
4233 symbolP = symbol_find_or_make (name);
4234 coffsym = coffsymbol (symbol_get_bfdsym (symbolP));
4236 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4237 coffsym->native->u.syment.n_type |= visibility;
4239 c = *input_line_pointer;
4240 if (c == ',')
4242 input_line_pointer ++;
4244 SKIP_WHITESPACE ();
4246 if (*input_line_pointer == '\n')
4247 c = '\n';
4250 while (c == ',');
4252 demand_empty_rest_of_line ();
4255 /* The .comm and .lcomm pseudo-ops for XCOFF. XCOFF puts common
4256 symbols in the .bss segment as though they were local common
4257 symbols, and uses a different smclas. The native Aix 4.3.3 assembler
4258 aligns .comm and .lcomm to 4 bytes.
4259 Symbols having a XMC_UL storage class are uninialized thread-local
4260 data. */
4262 static void
4263 ppc_comm (int lcomm)
4265 asection *current_seg = now_seg;
4266 subsegT current_subseg = now_subseg;
4267 char *name;
4268 char endc;
4269 char *end_name;
4270 offsetT size;
4271 offsetT align;
4272 symbolS *lcomm_sym = NULL;
4273 symbolS *sym;
4274 char *pfrag;
4275 unsigned short visibility = 0;
4276 struct ppc_xcoff_section *section;
4278 endc = get_symbol_name (&name);
4279 end_name = input_line_pointer;
4280 (void) restore_line_pointer (endc);
4282 if (*input_line_pointer != ',')
4284 as_bad (_("missing size"));
4285 ignore_rest_of_line ();
4286 return;
4288 ++input_line_pointer;
4290 size = get_absolute_expression ();
4291 if (size < 0)
4293 as_bad (_("negative size"));
4294 ignore_rest_of_line ();
4295 return;
4298 if (! lcomm)
4300 /* The third argument to .comm is the alignment. */
4301 if (*input_line_pointer != ',')
4302 align = 2;
4303 else
4305 ++input_line_pointer;
4306 align = get_absolute_expression ();
4307 if (align <= 0)
4309 as_warn (_("ignoring bad alignment"));
4310 align = 2;
4313 /* The fourth argument to .comm is the visibility. */
4314 if (*input_line_pointer == ',')
4316 input_line_pointer++;
4317 visibility = ppc_xcoff_get_visibility ();
4318 if (!visibility)
4320 as_bad (_("Unknown visibility field in .comm"));
4321 ignore_rest_of_line ();
4322 return;
4327 else
4329 char *lcomm_name;
4330 char lcomm_endc;
4332 /* The third argument to .lcomm appears to be the real local
4333 common symbol to create. References to the symbol named in
4334 the first argument are turned into references to the third
4335 argument. */
4336 if (*input_line_pointer != ',')
4338 as_bad (_("missing real symbol name"));
4339 ignore_rest_of_line ();
4340 return;
4342 ++input_line_pointer;
4344 lcomm_endc = get_symbol_name (&lcomm_name);
4346 lcomm_sym = symbol_find_or_make (lcomm_name);
4348 (void) restore_line_pointer (lcomm_endc);
4350 /* The fourth argument to .lcomm is the alignment. */
4351 if (*input_line_pointer != ',')
4353 if (size <= 4)
4354 align = 2;
4355 else
4356 align = 3;
4358 else
4360 ++input_line_pointer;
4361 align = get_absolute_expression ();
4362 if (align <= 0)
4364 as_warn (_("ignoring bad alignment"));
4365 align = 2;
4370 *end_name = '\0';
4371 sym = symbol_find_or_make (name);
4372 *end_name = endc;
4374 if (S_IS_DEFINED (sym)
4375 || S_GET_VALUE (sym) != 0)
4377 as_bad (_("attempt to redefine symbol"));
4378 ignore_rest_of_line ();
4379 return;
4382 if (symbol_get_tc (sym)->symbol_class == XMC_UL
4383 || (lcomm && symbol_get_tc (lcomm_sym)->symbol_class == XMC_UL))
4385 section = &ppc_xcoff_tbss_section;
4386 if (!ppc_xcoff_section_is_initialized (section))
4388 ppc_init_xcoff_section (section, subseg_new (".tbss", 0));
4389 bfd_set_section_flags (section->segment,
4390 SEC_ALLOC | SEC_THREAD_LOCAL);
4391 seg_info (section->segment)->bss = 1;
4394 else
4395 section = &ppc_xcoff_bss_section;
4397 record_alignment (section->segment, align);
4399 if (! lcomm
4400 || ! S_IS_DEFINED (lcomm_sym))
4402 symbolS *def_sym;
4403 offsetT def_size;
4405 if (! lcomm)
4407 def_sym = sym;
4408 def_size = size;
4409 S_SET_EXTERNAL (sym);
4411 else
4413 symbol_get_tc (lcomm_sym)->output = 1;
4414 def_sym = lcomm_sym;
4415 def_size = 0;
4418 subseg_set (section->segment, 1);
4419 frag_align (align, 0, 0);
4421 symbol_set_frag (def_sym, frag_now);
4422 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, def_sym,
4423 def_size, (char *) NULL);
4424 *pfrag = 0;
4425 S_SET_SEGMENT (def_sym, section->segment);
4426 symbol_get_tc (def_sym)->align = align;
4428 else if (lcomm)
4430 /* Align the size of lcomm_sym. */
4431 symbol_get_frag (lcomm_sym)->fr_offset =
4432 ((symbol_get_frag (lcomm_sym)->fr_offset + (1 << align) - 1)
4433 &~ ((1 << align) - 1));
4434 if (align > symbol_get_tc (lcomm_sym)->align)
4435 symbol_get_tc (lcomm_sym)->align = align;
4438 if (lcomm)
4440 /* Make sym an offset from lcomm_sym. */
4441 S_SET_SEGMENT (sym, section->segment);
4442 symbol_set_frag (sym, symbol_get_frag (lcomm_sym));
4443 S_SET_VALUE (sym, symbol_get_frag (lcomm_sym)->fr_offset);
4444 symbol_get_frag (lcomm_sym)->fr_offset += size;
4447 if (!lcomm && visibility)
4449 /* Add visibility to .comm symbol. */
4450 coff_symbol_type *coffsym = coffsymbol (symbol_get_bfdsym (sym));
4451 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4452 coffsym->native->u.syment.n_type |= visibility;
4455 subseg_set (current_seg, current_subseg);
4457 demand_empty_rest_of_line ();
4460 /* The .csect pseudo-op. This switches us into a different
4461 subsegment. The first argument is a symbol whose value is the
4462 start of the .csect. In COFF, csect symbols get special aux
4463 entries defined by the x_csect field of union internal_auxent. The
4464 optional second argument is the alignment (the default is 2). */
4466 static void
4467 ppc_csect (int ignore ATTRIBUTE_UNUSED)
4469 char *name;
4470 char endc;
4471 symbolS *sym;
4472 offsetT align;
4474 endc = get_symbol_name (&name);
4476 sym = symbol_find_or_make (name);
4478 (void) restore_line_pointer (endc);
4480 if (S_GET_NAME (sym)[0] == '\0')
4482 /* An unnamed csect is assumed to be [PR]. */
4483 symbol_get_tc (sym)->symbol_class = XMC_PR;
4486 align = 2;
4487 if (*input_line_pointer == ',')
4489 ++input_line_pointer;
4490 align = get_absolute_expression ();
4493 ppc_change_csect (sym, align);
4495 demand_empty_rest_of_line ();
4498 /* Change to a different csect. */
4500 static void
4501 ppc_change_csect (symbolS *sym, offsetT align)
4503 if (S_IS_DEFINED (sym))
4504 subseg_set (S_GET_SEGMENT (sym), symbol_get_tc (sym)->subseg);
4505 else
4507 struct ppc_xcoff_section *section;
4508 int after_toc;
4509 int hold_chunksize;
4510 symbolS *list;
4511 int is_code;
4512 segT sec;
4514 /* This is a new csect. We need to look at the symbol class to
4515 figure out whether it should go in the text section or the
4516 data section. */
4517 after_toc = 0;
4518 is_code = 0;
4519 switch (symbol_get_tc (sym)->symbol_class)
4521 case XMC_PR:
4522 case XMC_RO:
4523 case XMC_DB:
4524 case XMC_GL:
4525 case XMC_XO:
4526 case XMC_SV:
4527 case XMC_TI:
4528 case XMC_TB:
4529 section = &ppc_xcoff_text_section;
4530 is_code = 1;
4531 break;
4532 case XMC_RW:
4533 case XMC_TC0:
4534 case XMC_TC:
4535 case XMC_TE:
4536 case XMC_DS:
4537 case XMC_UA:
4538 case XMC_UC:
4539 section = &ppc_xcoff_data_section;
4540 if (ppc_toc_csect != NULL
4541 && (symbol_get_tc (ppc_toc_csect)->subseg + 1
4542 == section->next_subsegment))
4543 after_toc = 1;
4544 break;
4545 case XMC_BS:
4546 section = &ppc_xcoff_bss_section;
4547 break;
4548 case XMC_TL:
4549 section = &ppc_xcoff_tdata_section;
4550 /* Create .tdata section if not yet done. */
4551 if (!ppc_xcoff_section_is_initialized (section))
4553 ppc_init_xcoff_section (section, subseg_new (".tdata", 0));
4554 bfd_set_section_flags (section->segment, SEC_ALLOC
4555 | SEC_LOAD | SEC_RELOC | SEC_DATA
4556 | SEC_THREAD_LOCAL);
4558 break;
4559 case XMC_UL:
4560 section = &ppc_xcoff_tbss_section;
4561 /* Create .tbss section if not yet done. */
4562 if (!ppc_xcoff_section_is_initialized (section))
4564 ppc_init_xcoff_section (section, subseg_new (".tbss", 0));
4565 bfd_set_section_flags (section->segment, SEC_ALLOC |
4566 SEC_THREAD_LOCAL);
4567 seg_info (section->segment)->bss = 1;
4569 break;
4570 default:
4571 abort ();
4574 S_SET_SEGMENT (sym, section->segment);
4575 symbol_get_tc (sym)->subseg = section->next_subsegment;
4576 ++section->next_subsegment;
4578 /* We set the obstack chunk size to a small value before
4579 changing subsegments, so that we don't use a lot of memory
4580 space for what may be a small section. */
4581 hold_chunksize = chunksize;
4582 chunksize = 64;
4584 sec = subseg_new (segment_name (S_GET_SEGMENT (sym)),
4585 symbol_get_tc (sym)->subseg);
4587 chunksize = hold_chunksize;
4589 if (after_toc)
4590 ppc_after_toc_frag = frag_now;
4592 record_alignment (sec, align);
4593 if (is_code)
4594 frag_align_code (align, 0);
4595 else
4596 frag_align (align, 0, 0);
4598 symbol_set_frag (sym, frag_now);
4599 S_SET_VALUE (sym, (valueT) frag_now_fix ());
4601 symbol_get_tc (sym)->align = align;
4602 symbol_get_tc (sym)->output = 1;
4603 symbol_get_tc (sym)->within = sym;
4605 for (list = section->csects;
4606 symbol_get_tc (list)->next != (symbolS *) NULL;
4607 list = symbol_get_tc (list)->next)
4609 symbol_get_tc (list)->next = sym;
4611 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
4612 symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
4613 &symbol_lastP);
4616 ppc_current_csect = sym;
4619 static void
4620 ppc_change_debug_section (unsigned int idx, subsegT subseg)
4622 segT sec;
4623 flagword oldflags;
4624 const struct xcoff_dwsect_name *dw = &xcoff_dwsect_names[idx];
4626 sec = subseg_new (dw->xcoff_name, subseg);
4627 oldflags = bfd_section_flags (sec);
4628 if (oldflags == SEC_NO_FLAGS)
4630 /* Just created section. */
4631 gas_assert (dw_sections[idx].sect == NULL);
4633 bfd_set_section_flags (sec, SEC_DEBUGGING);
4634 bfd_set_section_alignment (sec, 0);
4635 dw_sections[idx].sect = sec;
4638 /* Not anymore in a csect. */
4639 ppc_current_csect = NULL;
4642 /* The .dwsect pseudo-op. Defines a DWARF section. Syntax is:
4643 .dwsect flag [, opt-label ]
4646 static void
4647 ppc_dwsect (int ignore ATTRIBUTE_UNUSED)
4649 valueT flag;
4650 symbolS *opt_label;
4651 const struct xcoff_dwsect_name *dw;
4652 struct dw_subsection *subseg;
4653 struct dw_section *dws;
4654 int i;
4656 /* Find section. */
4657 flag = get_absolute_expression ();
4658 dw = NULL;
4659 for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
4660 if (xcoff_dwsect_names[i].flag == flag)
4662 dw = &xcoff_dwsect_names[i];
4663 break;
4666 /* Parse opt-label. */
4667 if (*input_line_pointer == ',')
4669 char *label;
4670 char c;
4672 ++input_line_pointer;
4674 c = get_symbol_name (&label);
4675 opt_label = symbol_find_or_make (label);
4676 (void) restore_line_pointer (c);
4678 else
4679 opt_label = NULL;
4681 demand_empty_rest_of_line ();
4683 /* Return now in case of unknown subsection. */
4684 if (dw == NULL)
4686 as_bad (_("no known dwarf XCOFF section for flag 0x%08x\n"),
4687 (unsigned)flag);
4688 return;
4691 /* Find the subsection. */
4692 dws = &dw_sections[i];
4693 subseg = NULL;
4694 if (opt_label != NULL && S_IS_DEFINED (opt_label))
4696 /* Sanity check (note that in theory S_GET_SEGMENT mustn't be null). */
4697 if (dws->sect == NULL || S_GET_SEGMENT (opt_label) != dws->sect)
4699 as_bad (_("label %s was not defined in this dwarf section"),
4700 S_GET_NAME (opt_label));
4701 subseg = dws->anon_subseg;
4702 opt_label = NULL;
4704 else
4705 subseg = symbol_get_tc (opt_label)->u.dw;
4708 if (subseg != NULL)
4710 /* Switch to the subsection. */
4711 ppc_change_debug_section (i, subseg->subseg);
4713 else
4715 /* Create a new dw subsection. */
4716 subseg = XCNEW (struct dw_subsection);
4718 if (opt_label == NULL)
4720 /* The anonymous one. */
4721 subseg->subseg = 0;
4722 subseg->link = NULL;
4723 dws->anon_subseg = subseg;
4725 else
4727 /* A named one. */
4728 if (dws->list_subseg != NULL)
4729 subseg->subseg = dws->list_subseg->subseg + 1;
4730 else
4731 subseg->subseg = 1;
4733 subseg->link = dws->list_subseg;
4734 dws->list_subseg = subseg;
4735 symbol_get_tc (opt_label)->u.dw = subseg;
4738 ppc_change_debug_section (i, subseg->subseg);
4740 if (dw->def_size)
4742 /* Add the length field. */
4743 expressionS *exp = &subseg->end_exp;
4744 int sz;
4746 if (opt_label != NULL)
4747 symbol_set_value_now (opt_label);
4749 /* Add the length field. Note that according to the AIX assembler
4750 manual, the size of the length field is 4 for powerpc32 but
4751 12 for powerpc64. */
4752 if (ppc_obj64)
4754 /* Write the 64bit marker. */
4755 md_number_to_chars (frag_more (4), -1, 4);
4758 exp->X_op = O_subtract;
4759 exp->X_op_symbol = symbol_temp_new_now ();
4760 exp->X_add_symbol = symbol_temp_make ();
4762 sz = ppc_obj64 ? 8 : 4;
4763 exp->X_add_number = -sz;
4764 emit_expr (exp, sz);
4769 /* This function handles the .text and .data pseudo-ops. These
4770 pseudo-ops aren't really used by XCOFF; we implement them for the
4771 convenience of people who aren't used to XCOFF. */
4773 static void
4774 ppc_section (int type)
4776 const char *name;
4777 symbolS *sym;
4779 if (type == 't')
4780 name = ".text[PR]";
4781 else if (type == 'd')
4782 name = ".data[RW]";
4783 else
4784 abort ();
4786 sym = symbol_find_or_make (name);
4788 ppc_change_csect (sym, 2);
4790 demand_empty_rest_of_line ();
4793 /* This function handles the .section pseudo-op. This is mostly to
4794 give an error, since XCOFF only supports .text, .data and .bss, but
4795 we do permit the user to name the text or data section. */
4797 static void
4798 ppc_named_section (int ignore ATTRIBUTE_UNUSED)
4800 char *user_name;
4801 const char *real_name;
4802 char c;
4803 symbolS *sym;
4805 c = get_symbol_name (&user_name);
4807 if (strcmp (user_name, ".text") == 0)
4808 real_name = ".text[PR]";
4809 else if (strcmp (user_name, ".data") == 0)
4810 real_name = ".data[RW]";
4811 else
4813 as_bad (_("the XCOFF file format does not support arbitrary sections"));
4814 (void) restore_line_pointer (c);
4815 ignore_rest_of_line ();
4816 return;
4819 (void) restore_line_pointer (c);
4821 sym = symbol_find_or_make (real_name);
4823 ppc_change_csect (sym, 2);
4825 demand_empty_rest_of_line ();
4828 /* The .extern pseudo-op. We create an undefined symbol. */
4830 static void
4831 ppc_extern (int ignore ATTRIBUTE_UNUSED)
4833 char *name;
4834 symbolS *sym;
4836 if ((name = read_symbol_name ()) == NULL)
4837 return;
4839 sym = symbol_find_or_make (name);
4841 if (*input_line_pointer == ',')
4843 unsigned short visibility;
4844 coff_symbol_type *coffsym = coffsymbol (symbol_get_bfdsym (sym));
4846 input_line_pointer++;
4847 visibility = ppc_xcoff_get_visibility ();
4848 if (!visibility)
4850 as_bad (_("Unknown visibility field in .extern"));
4851 ignore_rest_of_line ();
4852 return;
4855 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4856 coffsym->native->u.syment.n_type |= visibility;
4859 demand_empty_rest_of_line ();
4862 /* XCOFF semantic for .globl says that the second parameter is
4863 the symbol visibility. */
4865 static void
4866 ppc_globl (int ignore ATTRIBUTE_UNUSED)
4868 char *name;
4869 symbolS *sym;
4871 if ((name = read_symbol_name ()) == NULL)
4872 return;
4874 sym = symbol_find_or_make (name);
4875 S_SET_EXTERNAL (sym);
4877 if (*input_line_pointer == ',')
4879 unsigned short visibility;
4880 coff_symbol_type *coffsym = coffsymbol (symbol_get_bfdsym (sym));
4882 input_line_pointer++;
4883 visibility = ppc_xcoff_get_visibility ();
4884 if (!visibility)
4886 as_bad (_("Unknown visibility field in .globl"));
4887 ignore_rest_of_line ();
4888 return;
4891 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4892 coffsym->native->u.syment.n_type |= visibility;
4895 demand_empty_rest_of_line ();
4898 /* XCOFF semantic for .weak says that the second parameter is
4899 the symbol visibility. */
4901 static void
4902 ppc_weak (int ignore ATTRIBUTE_UNUSED)
4904 char *name;
4905 symbolS *sym;
4907 if ((name = read_symbol_name ()) == NULL)
4908 return;
4910 sym = symbol_find_or_make (name);
4911 S_SET_WEAK (sym);
4913 if (*input_line_pointer == ',')
4915 unsigned short visibility;
4916 coff_symbol_type *coffsym = coffsymbol (symbol_get_bfdsym (sym));
4918 input_line_pointer++;
4919 visibility = ppc_xcoff_get_visibility ();
4920 if (!visibility)
4922 as_bad (_("Unknown visibility field in .weak"));
4923 ignore_rest_of_line ();
4924 return;
4927 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4928 coffsym->native->u.syment.n_type |= visibility;
4931 demand_empty_rest_of_line ();
4934 /* The .lglobl pseudo-op. Keep the symbol in the symbol table. */
4936 static void
4937 ppc_lglobl (int ignore ATTRIBUTE_UNUSED)
4939 char *name;
4940 char endc;
4941 symbolS *sym;
4943 endc = get_symbol_name (&name);
4945 sym = symbol_find_or_make (name);
4947 (void) restore_line_pointer (endc);
4949 symbol_get_tc (sym)->output = 1;
4951 demand_empty_rest_of_line ();
4954 /* The .ref pseudo-op. It takes a list of symbol names and inserts R_REF
4955 relocations at the beginning of the current csect.
4957 (In principle, there's no reason why the relocations _have_ to be at
4958 the beginning. Anywhere in the csect would do. However, inserting
4959 at the beginning is what the native assembler does, and it helps to
4960 deal with cases where the .ref statements follow the section contents.)
4962 ??? .refs don't work for empty .csects. However, the native assembler
4963 doesn't report an error in this case, and neither yet do we. */
4965 static void
4966 ppc_ref (int ignore ATTRIBUTE_UNUSED)
4968 char *name;
4969 char c;
4971 if (ppc_current_csect == NULL)
4973 as_bad (_(".ref outside .csect"));
4974 ignore_rest_of_line ();
4975 return;
4980 c = get_symbol_name (&name);
4982 fix_at_start (symbol_get_frag (ppc_current_csect), 0,
4983 symbol_find_or_make (name), 0, false, BFD_RELOC_NONE);
4985 *input_line_pointer = c;
4986 SKIP_WHITESPACE_AFTER_NAME ();
4987 c = *input_line_pointer;
4988 if (c == ',')
4990 input_line_pointer++;
4991 SKIP_WHITESPACE ();
4992 if (is_end_of_line[(unsigned char) *input_line_pointer])
4994 as_bad (_("missing symbol name"));
4995 ignore_rest_of_line ();
4996 return;
5000 while (c == ',');
5002 demand_empty_rest_of_line ();
5005 /* The .rename pseudo-op. The RS/6000 assembler can rename symbols,
5006 although I don't know why it bothers. */
5008 static void
5009 ppc_rename (int ignore ATTRIBUTE_UNUSED)
5011 char *name;
5012 char endc;
5013 symbolS *sym;
5014 int len;
5016 endc = get_symbol_name (&name);
5018 sym = symbol_find_or_make (name);
5020 (void) restore_line_pointer (endc);
5022 if (*input_line_pointer != ',')
5024 as_bad (_("missing rename string"));
5025 ignore_rest_of_line ();
5026 return;
5028 ++input_line_pointer;
5030 symbol_get_tc (sym)->real_name = demand_copy_C_string (&len);
5032 demand_empty_rest_of_line ();
5035 /* The .stabx pseudo-op. This is similar to a normal .stabs
5036 pseudo-op, but slightly different. A sample is
5037 .stabx "main:F-1",.main,142,0
5038 The first argument is the symbol name to create. The second is the
5039 value, and the third is the storage class. The fourth seems to be
5040 always zero, and I am assuming it is the type. */
5042 static void
5043 ppc_stabx (int ignore ATTRIBUTE_UNUSED)
5045 char *name;
5046 int len;
5047 symbolS *sym;
5048 expressionS exp;
5050 name = demand_copy_C_string (&len);
5052 if (*input_line_pointer != ',')
5054 as_bad (_("missing value"));
5055 return;
5057 ++input_line_pointer;
5059 ppc_stab_symbol = true;
5060 sym = symbol_make (name);
5061 ppc_stab_symbol = false;
5063 symbol_get_tc (sym)->real_name = name;
5065 (void) expression (&exp);
5067 switch (exp.X_op)
5069 case O_illegal:
5070 case O_absent:
5071 case O_big:
5072 as_bad (_("illegal .stabx expression; zero assumed"));
5073 exp.X_add_number = 0;
5074 /* Fall through. */
5075 case O_constant:
5076 S_SET_VALUE (sym, (valueT) exp.X_add_number);
5077 symbol_set_frag (sym, &zero_address_frag);
5078 break;
5080 case O_symbol:
5081 if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section)
5082 symbol_set_value_expression (sym, &exp);
5083 else
5085 S_SET_VALUE (sym,
5086 exp.X_add_number + S_GET_VALUE (exp.X_add_symbol));
5087 symbol_set_frag (sym, symbol_get_frag (exp.X_add_symbol));
5089 break;
5091 default:
5092 /* The value is some complex expression. This will probably
5093 fail at some later point, but this is probably the right
5094 thing to do here. */
5095 symbol_set_value_expression (sym, &exp);
5096 break;
5099 S_SET_SEGMENT (sym, ppc_coff_debug_section);
5100 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5102 if (*input_line_pointer != ',')
5104 as_bad (_("missing class"));
5105 return;
5107 ++input_line_pointer;
5109 S_SET_STORAGE_CLASS (sym, get_absolute_expression ());
5111 if (*input_line_pointer != ',')
5113 as_bad (_("missing type"));
5114 return;
5116 ++input_line_pointer;
5118 S_SET_DATA_TYPE (sym, get_absolute_expression ());
5120 symbol_get_tc (sym)->output = 1;
5122 if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
5124 /* In this case :
5126 .bs name
5127 .stabx "z",arrays_,133,0
5130 .comm arrays_,13768,3
5132 resolve_symbol_value will copy the exp's "within" into sym's when the
5133 offset is 0. Since this seems to be corner case problem,
5134 only do the correction for storage class C_STSYM. A better solution
5135 would be to have the tc field updated in ppc_symbol_new_hook. */
5137 if (exp.X_op == O_symbol)
5139 if (ppc_current_block == NULL)
5140 as_bad (_(".stabx of storage class stsym must be within .bs/.es"));
5142 symbol_get_tc (sym)->within = ppc_current_block;
5146 if (exp.X_op != O_symbol
5147 || ! S_IS_EXTERNAL (exp.X_add_symbol)
5148 || S_GET_SEGMENT (exp.X_add_symbol) != bss_section)
5149 ppc_frob_label (sym);
5150 else
5152 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
5153 symbol_append (sym, exp.X_add_symbol, &symbol_rootP, &symbol_lastP);
5154 if (symbol_get_tc (ppc_current_csect)->within == exp.X_add_symbol)
5155 symbol_get_tc (ppc_current_csect)->within = sym;
5158 demand_empty_rest_of_line ();
5161 /* The .file pseudo-op. On XCOFF, .file can have several parameters
5162 which are being added to the symbol table to provide additional
5163 information. */
5165 static void
5166 ppc_file (int ignore ATTRIBUTE_UNUSED)
5168 char *sfname, *s1 = NULL, *s2 = NULL, *s3 = NULL;
5169 int length, auxnb = 1;
5171 /* Some assemblers tolerate immediately following '"'. */
5172 if ((sfname = demand_copy_string (&length)) != 0)
5174 coff_symbol_type *coffsym;
5175 if (*input_line_pointer == ',')
5177 ++input_line_pointer;
5178 s1 = demand_copy_string (&length);
5179 auxnb++;
5181 if (*input_line_pointer == ',')
5183 ++input_line_pointer;
5184 s2 = demand_copy_string (&length);
5185 auxnb++;
5187 if (*input_line_pointer == ',')
5189 ++input_line_pointer;
5190 s3 = demand_copy_string (&length);
5191 auxnb++;
5196 /* Use coff dot_file creation and adjust auxiliary entries. */
5197 c_dot_file_symbol (sfname);
5198 S_SET_NUMBER_AUXILIARY (symbol_rootP, auxnb);
5199 coffsym = coffsymbol (symbol_get_bfdsym (symbol_rootP));
5200 coffsym->native[1].u.auxent.x_file.x_ftype = XFT_FN;
5202 if (s1)
5204 coffsym->native[2].u.auxent.x_file.x_ftype = XFT_CT;
5205 coffsym->native[2].extrap = s1;
5207 if (s2)
5209 coffsym->native[3].u.auxent.x_file.x_ftype = XFT_CV;
5210 coffsym->native[3].extrap = s2;
5212 if (s3)
5214 coffsym->native[4].u.auxent.x_file.x_ftype = XFT_CD;
5215 coffsym->native[4].extrap = s3;
5218 demand_empty_rest_of_line ();
5222 /* The .function pseudo-op. This takes several arguments. The first
5223 argument seems to be the external name of the symbol. The second
5224 argument seems to be the label for the start of the function. gcc
5225 uses the same name for both. I have no idea what the third and
5226 fourth arguments are meant to be. The optional fifth argument is
5227 an expression for the size of the function. In COFF this symbol
5228 gets an aux entry like that used for a csect. */
5230 static void
5231 ppc_function (int ignore ATTRIBUTE_UNUSED)
5233 char *name;
5234 char endc;
5235 char *s;
5236 symbolS *ext_sym;
5237 symbolS *lab_sym;
5239 endc = get_symbol_name (&name);
5241 /* Ignore any [PR] suffix. */
5242 name = ppc_canonicalize_symbol_name (name);
5243 s = strchr (name, '[');
5244 if (s != (char *) NULL
5245 && strcmp (s + 1, "PR]") == 0)
5246 *s = '\0';
5248 ext_sym = symbol_find_or_make (name);
5250 (void) restore_line_pointer (endc);
5252 if (*input_line_pointer != ',')
5254 as_bad (_("missing symbol name"));
5255 ignore_rest_of_line ();
5256 return;
5258 ++input_line_pointer;
5260 endc = get_symbol_name (&name);
5262 lab_sym = symbol_find_or_make (name);
5264 (void) restore_line_pointer (endc);
5266 if (ext_sym != lab_sym)
5268 expressionS exp;
5270 exp.X_op = O_symbol;
5271 exp.X_add_symbol = lab_sym;
5272 exp.X_op_symbol = NULL;
5273 exp.X_add_number = 0;
5274 exp.X_unsigned = 0;
5275 symbol_set_value_expression (ext_sym, &exp);
5278 if (symbol_get_tc (ext_sym)->symbol_class == -1)
5279 symbol_get_tc (ext_sym)->symbol_class = XMC_PR;
5280 symbol_get_tc (ext_sym)->output = 1;
5282 if (*input_line_pointer == ',')
5284 expressionS exp;
5286 /* Ignore the third argument. */
5287 ++input_line_pointer;
5288 expression (& exp);
5289 if (*input_line_pointer == ',')
5291 /* Ignore the fourth argument. */
5292 ++input_line_pointer;
5293 expression (& exp);
5294 if (*input_line_pointer == ',')
5296 /* The fifth argument is the function size.
5297 If it's omitted, the size will be the containing csect.
5298 This will be donce during ppc_frob_symtab. */
5299 ++input_line_pointer;
5300 symbol_get_tc (ext_sym)->u.size
5301 = symbol_new ("L0\001", absolute_section,
5302 &zero_address_frag, 0);
5303 pseudo_set (symbol_get_tc (ext_sym)->u.size);
5308 S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
5309 SF_SET_FUNCTION (ext_sym);
5310 SF_SET_PROCESS (ext_sym);
5311 coff_add_linesym (ext_sym);
5313 demand_empty_rest_of_line ();
5316 /* The .bf pseudo-op. This is just like a COFF C_FCN symbol named
5317 ".bf". If the pseudo op .bi was seen before .bf, patch the .bi sym
5318 with the correct line number */
5320 static symbolS *saved_bi_sym = 0;
5322 static void
5323 ppc_bf (int ignore ATTRIBUTE_UNUSED)
5325 symbolS *sym;
5327 sym = symbol_make (".bf");
5328 S_SET_SEGMENT (sym, text_section);
5329 symbol_set_frag (sym, frag_now);
5330 S_SET_VALUE (sym, frag_now_fix ());
5331 S_SET_STORAGE_CLASS (sym, C_FCN);
5333 coff_line_base = get_absolute_expression ();
5335 S_SET_NUMBER_AUXILIARY (sym, 1);
5336 SA_SET_SYM_LNNO (sym, coff_line_base);
5338 /* Line number for bi. */
5339 if (saved_bi_sym)
5341 S_SET_VALUE (saved_bi_sym, coff_n_line_nos);
5342 saved_bi_sym = 0;
5346 symbol_get_tc (sym)->output = 1;
5348 ppc_frob_label (sym);
5350 demand_empty_rest_of_line ();
5353 /* The .ef pseudo-op. This is just like a COFF C_FCN symbol named
5354 ".ef", except that the line number is absolute, not relative to the
5355 most recent ".bf" symbol. */
5357 static void
5358 ppc_ef (int ignore ATTRIBUTE_UNUSED)
5360 symbolS *sym;
5362 sym = symbol_make (".ef");
5363 S_SET_SEGMENT (sym, text_section);
5364 symbol_set_frag (sym, frag_now);
5365 S_SET_VALUE (sym, frag_now_fix ());
5366 S_SET_STORAGE_CLASS (sym, C_FCN);
5367 S_SET_NUMBER_AUXILIARY (sym, 1);
5368 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
5369 symbol_get_tc (sym)->output = 1;
5371 ppc_frob_label (sym);
5373 demand_empty_rest_of_line ();
5376 /* The .bi and .ei pseudo-ops. These take a string argument and
5377 generates a C_BINCL or C_EINCL symbol, which goes at the start of
5378 the symbol list. The value of .bi will be know when the next .bf
5379 is encountered. */
5381 static void
5382 ppc_biei (int ei)
5384 static symbolS *last_biei;
5386 char *name;
5387 int len;
5388 symbolS *sym;
5389 symbolS *look;
5391 name = demand_copy_C_string (&len);
5393 /* The value of these symbols is actually file offset. Here we set
5394 the value to the index into the line number entries. In
5395 ppc_frob_symbols we set the fix_line field, which will cause BFD
5396 to do the right thing. */
5398 sym = symbol_make (name);
5399 /* obj-coff.c currently only handles line numbers correctly in the
5400 .text section. */
5401 S_SET_SEGMENT (sym, text_section);
5402 S_SET_VALUE (sym, coff_n_line_nos);
5403 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5405 S_SET_STORAGE_CLASS (sym, ei ? C_EINCL : C_BINCL);
5406 symbol_get_tc (sym)->output = 1;
5408 /* Save bi. */
5409 if (ei)
5410 saved_bi_sym = 0;
5411 else
5412 saved_bi_sym = sym;
5414 for (look = last_biei ? last_biei : symbol_rootP;
5415 (look != (symbolS *) NULL
5416 && (S_GET_STORAGE_CLASS (look) == C_FILE
5417 || S_GET_STORAGE_CLASS (look) == C_BINCL
5418 || S_GET_STORAGE_CLASS (look) == C_EINCL));
5419 look = symbol_next (look))
5421 if (look != (symbolS *) NULL)
5423 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
5424 symbol_insert (sym, look, &symbol_rootP, &symbol_lastP);
5425 last_biei = sym;
5428 demand_empty_rest_of_line ();
5431 /* The .bs pseudo-op. This generates a C_BSTAT symbol named ".bs".
5432 There is one argument, which is a csect symbol. The value of the
5433 .bs symbol is the index of this csect symbol. */
5435 static void
5436 ppc_bs (int ignore ATTRIBUTE_UNUSED)
5438 char *name;
5439 char endc;
5440 symbolS *csect;
5441 symbolS *sym;
5443 if (ppc_current_block != NULL)
5444 as_bad (_("nested .bs blocks"));
5446 endc = get_symbol_name (&name);
5448 csect = symbol_find_or_make (name);
5450 (void) restore_line_pointer (endc);
5452 sym = symbol_make (".bs");
5453 S_SET_SEGMENT (sym, now_seg);
5454 S_SET_STORAGE_CLASS (sym, C_BSTAT);
5455 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5456 symbol_get_tc (sym)->output = 1;
5458 symbol_get_tc (sym)->within = csect;
5460 ppc_frob_label (sym);
5462 ppc_current_block = sym;
5464 demand_empty_rest_of_line ();
5467 /* The .es pseudo-op. Generate a C_ESTART symbol named .es. */
5469 static void
5470 ppc_es (int ignore ATTRIBUTE_UNUSED)
5472 symbolS *sym;
5474 if (ppc_current_block == NULL)
5475 as_bad (_(".es without preceding .bs"));
5477 sym = symbol_make (".es");
5478 S_SET_SEGMENT (sym, now_seg);
5479 S_SET_STORAGE_CLASS (sym, C_ESTAT);
5480 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5481 symbol_get_tc (sym)->output = 1;
5483 ppc_frob_label (sym);
5485 ppc_current_block = NULL;
5487 demand_empty_rest_of_line ();
5490 /* The .bb pseudo-op. Generate a C_BLOCK symbol named .bb, with a
5491 line number. */
5493 static void
5494 ppc_bb (int ignore ATTRIBUTE_UNUSED)
5496 symbolS *sym;
5498 sym = symbol_make (".bb");
5499 S_SET_SEGMENT (sym, text_section);
5500 symbol_set_frag (sym, frag_now);
5501 S_SET_VALUE (sym, frag_now_fix ());
5502 S_SET_STORAGE_CLASS (sym, C_BLOCK);
5504 S_SET_NUMBER_AUXILIARY (sym, 1);
5505 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
5507 symbol_get_tc (sym)->output = 1;
5509 SF_SET_PROCESS (sym);
5511 ppc_frob_label (sym);
5513 demand_empty_rest_of_line ();
5516 /* The .eb pseudo-op. Generate a C_BLOCK symbol named .eb, with a
5517 line number. */
5519 static void
5520 ppc_eb (int ignore ATTRIBUTE_UNUSED)
5522 symbolS *sym;
5524 sym = symbol_make (".eb");
5525 S_SET_SEGMENT (sym, text_section);
5526 symbol_set_frag (sym, frag_now);
5527 S_SET_VALUE (sym, frag_now_fix ());
5528 S_SET_STORAGE_CLASS (sym, C_BLOCK);
5529 S_SET_NUMBER_AUXILIARY (sym, 1);
5530 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
5531 symbol_get_tc (sym)->output = 1;
5533 SF_SET_PROCESS (sym);
5535 ppc_frob_label (sym);
5537 demand_empty_rest_of_line ();
5540 /* The .bc pseudo-op. This just creates a C_BCOMM symbol with a
5541 specified name. */
5543 static void
5544 ppc_bc (int ignore ATTRIBUTE_UNUSED)
5546 char *name;
5547 int len;
5548 symbolS *sym;
5550 name = demand_copy_C_string (&len);
5551 sym = symbol_make (name);
5552 S_SET_SEGMENT (sym, ppc_coff_debug_section);
5553 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5554 S_SET_STORAGE_CLASS (sym, C_BCOMM);
5555 S_SET_VALUE (sym, 0);
5556 symbol_get_tc (sym)->output = 1;
5558 ppc_frob_label (sym);
5560 demand_empty_rest_of_line ();
5563 /* The .ec pseudo-op. This just creates a C_ECOMM symbol. */
5565 static void
5566 ppc_ec (int ignore ATTRIBUTE_UNUSED)
5568 symbolS *sym;
5570 sym = symbol_make (".ec");
5571 S_SET_SEGMENT (sym, ppc_coff_debug_section);
5572 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5573 S_SET_STORAGE_CLASS (sym, C_ECOMM);
5574 S_SET_VALUE (sym, 0);
5575 symbol_get_tc (sym)->output = 1;
5577 ppc_frob_label (sym);
5579 demand_empty_rest_of_line ();
5582 /* The .toc pseudo-op. Switch to the .toc subsegment. */
5584 static void
5585 ppc_toc (int ignore ATTRIBUTE_UNUSED)
5587 if (ppc_toc_csect != (symbolS *) NULL)
5588 subseg_set (data_section, symbol_get_tc (ppc_toc_csect)->subseg);
5589 else
5591 subsegT subseg;
5592 symbolS *sym;
5593 symbolS *list;
5595 subseg = ppc_xcoff_data_section.next_subsegment;
5596 ++ppc_xcoff_data_section.next_subsegment;
5598 subseg_new (segment_name (data_section), subseg);
5599 ppc_toc_frag = frag_now;
5601 sym = symbol_find_or_make ("TOC[TC0]");
5602 symbol_set_frag (sym, frag_now);
5603 S_SET_SEGMENT (sym, data_section);
5604 S_SET_VALUE (sym, (valueT) frag_now_fix ());
5605 symbol_get_tc (sym)->subseg = subseg;
5606 symbol_get_tc (sym)->output = 1;
5607 symbol_get_tc (sym)->within = sym;
5609 ppc_toc_csect = sym;
5611 for (list = ppc_xcoff_data_section.csects;
5612 symbol_get_tc (list)->next != (symbolS *) NULL;
5613 list = symbol_get_tc (list)->next)
5615 symbol_get_tc (list)->next = sym;
5617 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
5618 symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
5619 &symbol_lastP);
5622 ppc_current_csect = ppc_toc_csect;
5624 demand_empty_rest_of_line ();
5627 /* The AIX assembler automatically aligns the operands of a .long or
5628 .short pseudo-op, and we want to be compatible. */
5630 static void
5631 ppc_xcoff_cons (int log_size)
5633 frag_align (log_size, 0, 0);
5634 record_alignment (now_seg, log_size);
5635 cons (1 << log_size);
5638 static void
5639 ppc_vbyte (int dummy ATTRIBUTE_UNUSED)
5641 expressionS exp;
5642 int byte_count;
5644 (void) expression (&exp);
5646 if (exp.X_op != O_constant)
5648 as_bad (_("non-constant byte count"));
5649 return;
5652 byte_count = exp.X_add_number;
5654 if (*input_line_pointer != ',')
5656 as_bad (_("missing value"));
5657 return;
5660 ++input_line_pointer;
5661 cons (byte_count);
5664 void
5665 ppc_xcoff_end (void)
5667 int i;
5669 for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
5671 struct dw_section *dws = &dw_sections[i];
5672 struct dw_subsection *dwss;
5674 if (dws->anon_subseg)
5676 dwss = dws->anon_subseg;
5677 dwss->link = dws->list_subseg;
5679 else
5680 dwss = dws->list_subseg;
5682 for (; dwss != NULL; dwss = dwss->link)
5683 if (dwss->end_exp.X_add_symbol != NULL)
5685 subseg_set (dws->sect, dwss->subseg);
5686 symbol_set_value_now (dwss->end_exp.X_add_symbol);
5689 ppc_cpu = 0;
5692 #endif /* OBJ_XCOFF */
5693 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
5695 /* The .tc pseudo-op. This is used when generating either XCOFF or
5696 ELF. This takes two or more arguments.
5698 When generating XCOFF output, the first argument is the name to
5699 give to this location in the toc; this will be a symbol with class
5700 TC. The rest of the arguments are N-byte values to actually put at
5701 this location in the TOC; often there is just one more argument, a
5702 relocatable symbol reference. The size of the value to store
5703 depends on target word size. A 32-bit target uses 4-byte values, a
5704 64-bit target uses 8-byte values.
5706 When not generating XCOFF output, the arguments are the same, but
5707 the first argument is simply ignored. */
5709 static void
5710 ppc_tc (int ignore ATTRIBUTE_UNUSED)
5712 #ifdef OBJ_XCOFF
5714 /* Define the TOC symbol name. */
5716 char *name;
5717 char endc;
5718 symbolS *sym;
5720 if (ppc_toc_csect == (symbolS *) NULL
5721 || ppc_toc_csect != ppc_current_csect)
5723 as_bad (_(".tc not in .toc section"));
5724 ignore_rest_of_line ();
5725 return;
5728 endc = get_symbol_name (&name);
5730 sym = symbol_find_or_make (name);
5732 (void) restore_line_pointer (endc);
5734 if (S_IS_DEFINED (sym))
5736 symbolS *label;
5738 label = symbol_get_tc (ppc_current_csect)->within;
5739 if (symbol_get_tc (label)->symbol_class != XMC_TC0)
5741 as_bad (_(".tc with no label"));
5742 ignore_rest_of_line ();
5743 return;
5746 S_SET_SEGMENT (label, S_GET_SEGMENT (sym));
5747 symbol_set_frag (label, symbol_get_frag (sym));
5748 S_SET_VALUE (label, S_GET_VALUE (sym));
5750 while (! is_end_of_line[(unsigned char) *input_line_pointer])
5751 ++input_line_pointer;
5753 return;
5756 S_SET_SEGMENT (sym, now_seg);
5757 symbol_set_frag (sym, frag_now);
5758 S_SET_VALUE (sym, (valueT) frag_now_fix ());
5760 /* AIX assembler seems to allow any storage class to be set in .tc.
5761 But for now, only XMC_TC and XMC_TE are supported by us. */
5762 switch (symbol_get_tc (sym)->symbol_class)
5764 case XMC_TC:
5765 case XMC_TE:
5766 break;
5768 default:
5769 as_bad (_(".tc with storage class %d not yet supported"),
5770 symbol_get_tc (sym)->symbol_class);
5771 ignore_rest_of_line ();
5772 return;
5774 symbol_get_tc (sym)->output = 1;
5776 ppc_frob_label (sym);
5779 #endif /* OBJ_XCOFF */
5780 #ifdef OBJ_ELF
5781 int align;
5783 /* Skip the TOC symbol name. */
5784 while (is_part_of_name (*input_line_pointer)
5785 || *input_line_pointer == ' '
5786 || *input_line_pointer == '['
5787 || *input_line_pointer == ']'
5788 || *input_line_pointer == '{'
5789 || *input_line_pointer == '}')
5790 ++input_line_pointer;
5792 /* Align to a four/eight byte boundary. */
5793 align = ppc_obj64 ? 3 : 2;
5794 frag_align (align, 0, 0);
5795 record_alignment (now_seg, align);
5796 #endif /* OBJ_ELF */
5798 if (*input_line_pointer != ',')
5799 demand_empty_rest_of_line ();
5800 else
5802 ++input_line_pointer;
5803 cons (ppc_obj64 ? 8 : 4);
5807 /* Pseudo-op .machine. */
5809 static void
5810 ppc_machine (int ignore ATTRIBUTE_UNUSED)
5812 char c;
5813 char *cpu_string;
5814 #define MAX_HISTORY 100
5815 static ppc_cpu_t *cpu_history;
5816 static int curr_hist;
5818 SKIP_WHITESPACE ();
5820 c = get_symbol_name (&cpu_string);
5821 cpu_string = xstrdup (cpu_string);
5822 (void) restore_line_pointer (c);
5824 if (cpu_string != NULL)
5826 ppc_cpu_t old_cpu = ppc_cpu;
5827 char *p;
5829 for (p = cpu_string; *p != 0; p++)
5830 *p = TOLOWER (*p);
5832 if (strcmp (cpu_string, "push") == 0)
5834 if (cpu_history == NULL)
5835 cpu_history = XNEWVEC (ppc_cpu_t, MAX_HISTORY);
5837 if (curr_hist >= MAX_HISTORY)
5838 as_bad (_(".machine stack overflow"));
5839 else
5840 cpu_history[curr_hist++] = ppc_cpu;
5842 else if (strcmp (cpu_string, "pop") == 0)
5844 if (curr_hist <= 0)
5845 as_bad (_(".machine stack underflow"));
5846 else
5847 ppc_cpu = cpu_history[--curr_hist];
5849 else
5851 ppc_cpu_t new_cpu;
5852 /* Not using the global "sticky" variable here results in
5853 none of the extra functional unit command line options,
5854 -many, -maltivec, -mspe, -mspe2, -mvle, -mvsx, being in
5855 force after selecting a new cpu with .machine.
5856 ".machine altivec" and other extra functional unit
5857 options do not count as a new machine, instead they add
5858 to currently selected opcodes. */
5859 ppc_cpu_t machine_sticky = 0;
5860 /* Unfortunately, some versions of gcc emit a .machine
5861 directive very near the start of the compiler's assembly
5862 output file. This is bad because it overrides user -Wa
5863 cpu selection. Worse, there are versions of gcc that
5864 emit the *wrong* cpu, not even respecting the -mcpu given
5865 to gcc. See gcc pr101393. And to compound the problem,
5866 as of 20220222 gcc doesn't pass the correct cpu option to
5867 gas on the command line. See gcc pr59828. Hack around
5868 this by keeping sticky options for an early .machine. */
5869 asection *sec;
5870 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
5872 segment_info_type *info = seg_info (sec);
5873 /* Are the frags for this section perturbed from their
5874 initial state? Even .align will count here. */
5875 if (info != NULL
5876 && (info->frchainP->frch_root != info->frchainP->frch_last
5877 || info->frchainP->frch_root->fr_type != rs_fill
5878 || info->frchainP->frch_root->fr_fix != 0))
5879 break;
5881 new_cpu = ppc_parse_cpu (ppc_cpu,
5882 sec == NULL ? &sticky : &machine_sticky,
5883 cpu_string);
5884 if (new_cpu != 0)
5885 ppc_cpu = new_cpu;
5886 else
5887 as_bad (_("invalid machine `%s'"), cpu_string);
5890 if (ppc_cpu != old_cpu)
5891 ppc_setup_opcodes ();
5894 demand_empty_rest_of_line ();
5896 #endif /* defined (OBJ_XCOFF) || defined (OBJ_ELF) */
5898 #ifdef OBJ_XCOFF
5900 /* XCOFF specific symbol and file handling. */
5902 /* Canonicalize the symbol name. We use the to force the suffix, if
5903 any, to use square brackets, and to be in upper case. */
5905 char *
5906 ppc_canonicalize_symbol_name (char *name)
5908 char *s;
5910 if (ppc_stab_symbol)
5911 return name;
5913 for (s = name; *s != '\0' && *s != '{' && *s != '['; s++)
5915 if (*s != '\0')
5917 char brac;
5919 if (*s == '[')
5920 brac = ']';
5921 else
5923 *s = '[';
5924 brac = '}';
5927 for (s++; *s != '\0' && *s != brac; s++)
5928 *s = TOUPPER (*s);
5930 if (*s == '\0' || s[1] != '\0')
5931 as_bad (_("bad symbol suffix"));
5933 *s = ']';
5936 return name;
5939 /* Set the class of a symbol based on the suffix, if any. This is
5940 called whenever a new symbol is created. */
5942 void
5943 ppc_symbol_new_hook (symbolS *sym)
5945 struct ppc_tc_sy *tc;
5946 const char *s;
5948 tc = symbol_get_tc (sym);
5949 tc->next = NULL;
5950 tc->output = 0;
5951 tc->symbol_class = -1;
5952 tc->real_name = NULL;
5953 tc->subseg = 0;
5954 tc->align = 0;
5955 tc->u.size = NULL;
5956 tc->u.dw = NULL;
5957 tc->within = NULL;
5959 if (ppc_stab_symbol)
5960 return;
5962 s = strchr (S_GET_NAME (sym), '[');
5963 if (s == (const char *) NULL)
5965 /* There is no suffix. */
5966 return;
5969 ++s;
5971 switch (s[0])
5973 case 'B':
5974 if (strcmp (s, "BS]") == 0)
5975 tc->symbol_class = XMC_BS;
5976 break;
5977 case 'D':
5978 if (strcmp (s, "DB]") == 0)
5979 tc->symbol_class = XMC_DB;
5980 else if (strcmp (s, "DS]") == 0)
5981 tc->symbol_class = XMC_DS;
5982 break;
5983 case 'G':
5984 if (strcmp (s, "GL]") == 0)
5985 tc->symbol_class = XMC_GL;
5986 break;
5987 case 'P':
5988 if (strcmp (s, "PR]") == 0)
5989 tc->symbol_class = XMC_PR;
5990 break;
5991 case 'R':
5992 if (strcmp (s, "RO]") == 0)
5993 tc->symbol_class = XMC_RO;
5994 else if (strcmp (s, "RW]") == 0)
5995 tc->symbol_class = XMC_RW;
5996 break;
5997 case 'S':
5998 if (strcmp (s, "SV]") == 0)
5999 tc->symbol_class = XMC_SV;
6000 break;
6001 case 'T':
6002 if (strcmp (s, "TC]") == 0)
6003 tc->symbol_class = XMC_TC;
6004 else if (strcmp (s, "TI]") == 0)
6005 tc->symbol_class = XMC_TI;
6006 else if (strcmp (s, "TB]") == 0)
6007 tc->symbol_class = XMC_TB;
6008 else if (strcmp (s, "TC0]") == 0 || strcmp (s, "T0]") == 0)
6009 tc->symbol_class = XMC_TC0;
6010 else if (strcmp (s, "TE]") == 0)
6011 tc->symbol_class = XMC_TE;
6012 else if (strcmp (s, "TL]") == 0)
6013 tc->symbol_class = XMC_TL;
6014 break;
6015 case 'U':
6016 if (strcmp (s, "UA]") == 0)
6017 tc->symbol_class = XMC_UA;
6018 else if (strcmp (s, "UC]") == 0)
6019 tc->symbol_class = XMC_UC;
6020 else if (strcmp (s, "UL]") == 0)
6021 tc->symbol_class = XMC_UL;
6022 break;
6023 case 'X':
6024 if (strcmp (s, "XO]") == 0)
6025 tc->symbol_class = XMC_XO;
6026 break;
6029 if (tc->symbol_class == -1)
6030 as_bad (_("unrecognized symbol suffix"));
6033 /* This variable is set by ppc_frob_symbol if any absolute symbols are
6034 seen. It tells ppc_adjust_symtab whether it needs to look through
6035 the symbols. */
6037 static bool ppc_saw_abs;
6039 /* Change the name of a symbol just before writing it out. Set the
6040 real name if the .rename pseudo-op was used. Otherwise, remove any
6041 class suffix. Return 1 if the symbol should not be included in the
6042 symbol table. */
6045 ppc_frob_symbol (symbolS *sym)
6047 static symbolS *ppc_last_function;
6048 static symbolS *set_end;
6050 /* Discard symbols that should not be included in the output symbol
6051 table. */
6052 if (! symbol_used_in_reloc_p (sym)
6053 && S_GET_STORAGE_CLASS (sym) != C_DWARF
6054 && ((symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) != 0
6055 || (! (S_IS_EXTERNAL (sym) || S_IS_WEAK (sym))
6056 && ! symbol_get_tc (sym)->output
6057 && S_GET_STORAGE_CLASS (sym) != C_FILE)))
6058 return 1;
6060 /* This one will disappear anyway. Don't make a csect sym for it. */
6061 if (sym == abs_section_sym)
6062 return 1;
6064 if (symbol_get_tc (sym)->real_name != (char *) NULL)
6065 S_SET_NAME (sym, symbol_get_tc (sym)->real_name);
6066 else
6068 const char *name;
6069 const char *s;
6071 name = S_GET_NAME (sym);
6072 s = strchr (name, '[');
6073 if (s != (char *) NULL)
6075 unsigned int len;
6076 char *snew;
6078 len = s - name;
6079 snew = xstrndup (name, len);
6081 S_SET_NAME (sym, snew);
6085 if (set_end != (symbolS *) NULL)
6087 SA_SET_SYM_ENDNDX (set_end, sym);
6088 set_end = NULL;
6091 if (SF_GET_FUNCTION (sym))
6093 /* Make sure coff_last_function is reset. Otherwise, we won't create
6094 the auxent for the next function. */
6095 coff_last_function = 0;
6096 ppc_last_function = sym;
6097 if (symbol_get_tc (sym)->u.size != (symbolS *) NULL)
6099 resolve_symbol_value (symbol_get_tc (sym)->u.size);
6100 SA_SET_SYM_FSIZE (sym,
6101 (long) S_GET_VALUE (symbol_get_tc (sym)->u.size));
6103 else
6105 /* Size of containing csect. */
6106 symbolS* within = symbol_get_tc (sym)->within;
6107 union internal_auxent *csectaux;
6108 csectaux = &coffsymbol (symbol_get_bfdsym (within))
6109 ->native[S_GET_NUMBER_AUXILIARY(within)].u.auxent;
6111 SA_SET_SYM_FSIZE (sym, csectaux->x_csect.x_scnlen.l);
6114 else if (S_GET_STORAGE_CLASS (sym) == C_FCN
6115 && strcmp (S_GET_NAME (sym), ".ef") == 0)
6117 if (ppc_last_function == (symbolS *) NULL)
6118 as_bad (_(".ef with no preceding .function"));
6119 else
6121 set_end = ppc_last_function;
6122 ppc_last_function = NULL;
6124 /* We don't have a C_EFCN symbol, but we need to force the
6125 COFF backend to believe that it has seen one. */
6126 coff_last_function = NULL;
6130 if (! (S_IS_EXTERNAL (sym) || S_IS_WEAK (sym))
6131 && (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) == 0
6132 && S_GET_STORAGE_CLASS (sym) != C_FILE
6133 && S_GET_STORAGE_CLASS (sym) != C_FCN
6134 && S_GET_STORAGE_CLASS (sym) != C_BLOCK
6135 && S_GET_STORAGE_CLASS (sym) != C_BSTAT
6136 && S_GET_STORAGE_CLASS (sym) != C_ESTAT
6137 && S_GET_STORAGE_CLASS (sym) != C_BINCL
6138 && S_GET_STORAGE_CLASS (sym) != C_EINCL
6139 && S_GET_SEGMENT (sym) != ppc_coff_debug_section)
6140 S_SET_STORAGE_CLASS (sym, C_HIDEXT);
6142 if (S_GET_STORAGE_CLASS (sym) == C_EXT
6143 || S_GET_STORAGE_CLASS (sym) == C_AIX_WEAKEXT
6144 || S_GET_STORAGE_CLASS (sym) == C_HIDEXT)
6146 int i;
6147 union internal_auxent *a;
6149 /* Create a csect aux. */
6150 i = S_GET_NUMBER_AUXILIARY (sym);
6151 S_SET_NUMBER_AUXILIARY (sym, i + 1);
6152 a = &coffsymbol (symbol_get_bfdsym (sym))->native[i + 1].u.auxent;
6153 if (symbol_get_tc (sym)->symbol_class == XMC_TC0)
6155 /* This is the TOC table. */
6156 know (strcmp (S_GET_NAME (sym), "TOC") == 0);
6157 a->x_csect.x_scnlen.l = 0;
6158 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
6160 else if (symbol_get_tc (sym)->subseg != 0)
6162 /* This is a csect symbol. x_scnlen is the size of the
6163 csect. */
6164 if (symbol_get_tc (sym)->next == (symbolS *) NULL)
6165 a->x_csect.x_scnlen.l = (bfd_section_size (S_GET_SEGMENT (sym))
6166 - S_GET_VALUE (sym));
6167 else
6169 resolve_symbol_value (symbol_get_tc (sym)->next);
6170 a->x_csect.x_scnlen.l = (S_GET_VALUE (symbol_get_tc (sym)->next)
6171 - S_GET_VALUE (sym));
6173 if (symbol_get_tc (sym)->symbol_class == XMC_BS
6174 || symbol_get_tc (sym)->symbol_class == XMC_UL)
6175 a->x_csect.x_smtyp = (symbol_get_tc (sym)->align << 3) | XTY_CM;
6176 else
6177 a->x_csect.x_smtyp = (symbol_get_tc (sym)->align << 3) | XTY_SD;
6179 else if (S_GET_SEGMENT (sym) == bss_section
6180 || S_GET_SEGMENT (sym) == ppc_xcoff_tbss_section.segment)
6182 /* This is a common symbol. */
6183 a->x_csect.x_scnlen.l = symbol_get_frag (sym)->fr_offset;
6184 a->x_csect.x_smtyp = (symbol_get_tc (sym)->align << 3) | XTY_CM;
6185 if (S_GET_SEGMENT (sym) == ppc_xcoff_tbss_section.segment)
6186 symbol_get_tc (sym)->symbol_class = XMC_UL;
6187 else if (S_IS_EXTERNAL (sym))
6188 symbol_get_tc (sym)->symbol_class = XMC_RW;
6189 else
6190 symbol_get_tc (sym)->symbol_class = XMC_BS;
6192 else if (S_GET_SEGMENT (sym) == absolute_section)
6194 /* This is an absolute symbol. The csect will be created by
6195 ppc_adjust_symtab. */
6196 ppc_saw_abs = true;
6197 a->x_csect.x_smtyp = XTY_LD;
6198 if (symbol_get_tc (sym)->symbol_class == -1)
6199 symbol_get_tc (sym)->symbol_class = XMC_XO;
6201 else if (! S_IS_DEFINED (sym))
6203 /* This is an external symbol. */
6204 a->x_csect.x_scnlen.l = 0;
6205 a->x_csect.x_smtyp = XTY_ER;
6207 else if (ppc_is_toc_sym (sym))
6209 symbolS *next;
6211 /* This is a TOC definition. x_scnlen is the size of the
6212 TOC entry. */
6213 next = symbol_next (sym);
6214 while (symbol_get_tc (next)->symbol_class == XMC_TC0)
6215 next = symbol_next (next);
6216 if (next == (symbolS *) NULL
6217 || (!ppc_is_toc_sym (next)))
6219 if (ppc_after_toc_frag == (fragS *) NULL)
6220 a->x_csect.x_scnlen.l = (bfd_section_size (data_section)
6221 - S_GET_VALUE (sym));
6222 else
6223 a->x_csect.x_scnlen.l = (ppc_after_toc_frag->fr_address
6224 - S_GET_VALUE (sym));
6226 else
6228 resolve_symbol_value (next);
6229 a->x_csect.x_scnlen.l = (S_GET_VALUE (next)
6230 - S_GET_VALUE (sym));
6232 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
6234 else
6236 symbolS *csect;
6238 /* This is a normal symbol definition. x_scnlen is the
6239 symbol index of the containing csect. */
6240 if (S_GET_SEGMENT (sym) == text_section)
6241 csect = ppc_xcoff_text_section.csects;
6242 else if (S_GET_SEGMENT (sym) == data_section)
6243 csect = ppc_xcoff_data_section.csects;
6244 else if (S_GET_SEGMENT (sym) == ppc_xcoff_tdata_section.segment)
6245 csect = ppc_xcoff_tdata_section.csects;
6246 else
6247 abort ();
6249 /* Skip the initial dummy symbol. */
6250 csect = symbol_get_tc (csect)->next;
6252 if (csect == (symbolS *) NULL)
6254 as_warn (_("warning: symbol %s has no csect"), S_GET_NAME (sym));
6255 a->x_csect.x_scnlen.l = 0;
6257 else
6259 while (symbol_get_tc (csect)->next != (symbolS *) NULL)
6261 resolve_symbol_value (symbol_get_tc (csect)->next);
6262 if (S_GET_VALUE (symbol_get_tc (csect)->next)
6263 > S_GET_VALUE (sym))
6264 break;
6265 csect = symbol_get_tc (csect)->next;
6268 a->x_csect.x_scnlen.p =
6269 coffsymbol (symbol_get_bfdsym (csect))->native;
6270 coffsymbol (symbol_get_bfdsym (sym))->native[i + 1].fix_scnlen =
6273 a->x_csect.x_smtyp = XTY_LD;
6276 a->x_csect.x_parmhash = 0;
6277 a->x_csect.x_snhash = 0;
6278 if (symbol_get_tc (sym)->symbol_class == -1)
6279 a->x_csect.x_smclas = XMC_PR;
6280 else
6281 a->x_csect.x_smclas = symbol_get_tc (sym)->symbol_class;
6282 a->x_csect.x_stab = 0;
6283 a->x_csect.x_snstab = 0;
6285 /* Don't let the COFF backend resort these symbols. */
6286 symbol_get_bfdsym (sym)->flags |= BSF_NOT_AT_END;
6288 else if (S_GET_STORAGE_CLASS (sym) == C_BSTAT)
6290 /* We want the value to be the symbol index of the referenced
6291 csect symbol. BFD will do that for us if we set the right
6292 flags. */
6293 asymbol *bsym = symbol_get_bfdsym (symbol_get_tc (sym)->within);
6294 combined_entry_type *c = coffsymbol (bsym)->native;
6296 S_SET_VALUE (sym, (valueT) (size_t) c);
6297 coffsymbol (symbol_get_bfdsym (sym))->native->fix_value = 1;
6299 else if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
6301 symbolS *block;
6302 valueT base;
6304 block = symbol_get_tc (sym)->within;
6305 if (block)
6307 /* The value is the offset from the enclosing csect. */
6308 symbolS *csect;
6310 csect = symbol_get_tc (block)->within;
6311 resolve_symbol_value (csect);
6312 base = S_GET_VALUE (csect);
6314 else
6315 base = 0;
6317 S_SET_VALUE (sym, S_GET_VALUE (sym) - base);
6319 else if (S_GET_STORAGE_CLASS (sym) == C_BINCL
6320 || S_GET_STORAGE_CLASS (sym) == C_EINCL)
6322 /* We want the value to be a file offset into the line numbers.
6323 BFD will do that for us if we set the right flags. We have
6324 already set the value correctly. */
6325 coffsymbol (symbol_get_bfdsym (sym))->native->fix_line = 1;
6328 return 0;
6331 /* Adjust the symbol table. */
6333 void
6334 ppc_adjust_symtab (void)
6336 symbolS *sym;
6337 symbolS *anchorSym;
6339 /* Make sure C_DWARF symbols come right after C_FILE.
6340 As the C_FILE might not be defined yet and as C_DWARF
6341 might already be ordered, we insert them before the
6342 first symbol which isn't a C_FILE or a C_DWARF. */
6343 for (anchorSym = symbol_rootP; anchorSym != NULL;
6344 anchorSym = symbol_next (anchorSym))
6346 if (S_GET_STORAGE_CLASS (anchorSym) != C_FILE
6347 && S_GET_STORAGE_CLASS (anchorSym) != C_DWARF)
6348 break;
6351 sym = anchorSym;
6352 while (sym != NULL)
6354 if (S_GET_STORAGE_CLASS (sym) != C_DWARF)
6356 sym = symbol_next (sym);
6357 continue;
6360 symbolS* tsym = sym;
6361 sym = symbol_next (sym);
6363 symbol_remove (tsym, &symbol_rootP, &symbol_lastP);
6364 symbol_insert (tsym, anchorSym, &symbol_rootP, &symbol_lastP);
6367 /* Create csect symbols for all absolute symbols. */
6369 if (! ppc_saw_abs)
6370 return;
6372 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
6374 symbolS *csect;
6375 int i;
6376 union internal_auxent *a;
6378 if (S_GET_SEGMENT (sym) != absolute_section)
6379 continue;
6381 csect = symbol_create (".abs[XO]", absolute_section,
6382 &zero_address_frag, S_GET_VALUE (sym));
6383 symbol_get_bfdsym (csect)->value = S_GET_VALUE (sym);
6384 S_SET_STORAGE_CLASS (csect, C_HIDEXT);
6385 i = S_GET_NUMBER_AUXILIARY (csect);
6386 S_SET_NUMBER_AUXILIARY (csect, i + 1);
6387 a = &coffsymbol (symbol_get_bfdsym (csect))->native[i + 1].u.auxent;
6388 a->x_csect.x_scnlen.l = 0;
6389 a->x_csect.x_smtyp = XTY_SD;
6390 a->x_csect.x_parmhash = 0;
6391 a->x_csect.x_snhash = 0;
6392 a->x_csect.x_smclas = XMC_XO;
6393 a->x_csect.x_stab = 0;
6394 a->x_csect.x_snstab = 0;
6396 symbol_insert (csect, sym, &symbol_rootP, &symbol_lastP);
6398 i = S_GET_NUMBER_AUXILIARY (sym);
6399 a = &coffsymbol (symbol_get_bfdsym (sym))->native[i].u.auxent;
6400 a->x_csect.x_scnlen.p = coffsymbol (symbol_get_bfdsym (csect))->native;
6401 coffsymbol (symbol_get_bfdsym (sym))->native[i].fix_scnlen = 1;
6404 ppc_saw_abs = false;
6407 /* Set the VMA for a section. This is called on all the sections in
6408 turn. */
6410 void
6411 ppc_frob_section (asection *sec)
6413 static bfd_vma vma = 0;
6415 /* Dwarf sections start at 0. */
6416 if (bfd_section_flags (sec) & SEC_DEBUGGING)
6417 return;
6419 vma = md_section_align (sec, vma);
6420 bfd_set_section_vma (sec, vma);
6421 vma += bfd_section_size (sec);
6424 #endif /* OBJ_XCOFF */
6426 const char *
6427 md_atof (int type, char *litp, int *sizep)
6429 return ieee_md_atof (type, litp, sizep, target_big_endian);
6432 /* Write a value out to the object file, using the appropriate
6433 endianness. */
6435 void
6436 md_number_to_chars (char *buf, valueT val, int n)
6438 if (target_big_endian)
6439 number_to_chars_bigendian (buf, val, n);
6440 else
6441 number_to_chars_littleendian (buf, val, n);
6444 /* Align a section (I don't know why this is machine dependent). */
6446 valueT
6447 md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT addr)
6449 #ifdef OBJ_ELF
6450 return addr;
6451 #else
6452 int align = bfd_section_alignment (seg);
6454 return ((addr + (1 << align) - 1) & -(1 << align));
6455 #endif
6458 /* We don't have any form of relaxing. */
6461 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
6462 asection *seg ATTRIBUTE_UNUSED)
6464 abort ();
6465 return 0;
6468 /* Convert a machine dependent frag. We never generate these. */
6470 void
6471 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
6472 asection *sec ATTRIBUTE_UNUSED,
6473 fragS *fragp ATTRIBUTE_UNUSED)
6475 abort ();
6478 /* We have no need to default values of symbols. */
6480 symbolS *
6481 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
6483 return 0;
6486 /* Functions concerning relocs. */
6488 /* The location from which a PC relative jump should be calculated,
6489 given a PC relative reloc. */
6491 long
6492 md_pcrel_from_section (fixS *fixp, segT sec ATTRIBUTE_UNUSED)
6494 return fixp->fx_frag->fr_address + fixp->fx_where;
6497 #ifdef OBJ_XCOFF
6499 /* Return the surrending csect for sym when possible. */
6501 static symbolS*
6502 ppc_get_csect_to_adjust (symbolS *sym)
6504 if (sym == NULL)
6505 return NULL;
6507 valueT val = resolve_symbol_value (sym);
6508 TC_SYMFIELD_TYPE *tc = symbol_get_tc (sym);
6509 segT symseg = S_GET_SEGMENT (sym);
6511 if (tc->subseg == 0
6512 && tc->symbol_class != XMC_TC0
6513 && tc->symbol_class != XMC_TC
6514 && tc->symbol_class != XMC_TE
6515 && symseg != bss_section
6516 && symseg != ppc_xcoff_tbss_section.segment
6517 /* Don't adjust if this is a reloc in the toc section. */
6518 && (symseg != data_section
6519 || ppc_toc_csect == NULL
6520 || val < ppc_toc_frag->fr_address
6521 || (ppc_after_toc_frag != NULL
6522 && val >= ppc_after_toc_frag->fr_address)))
6524 symbolS* csect = tc->within;
6526 /* If the symbol was not declared by a label (eg: a section symbol),
6527 use the section instead of the csect. This doesn't happen in
6528 normal AIX assembly code. */
6529 if (csect == NULL)
6530 csect = seg_info (symseg)->sym;
6532 return csect;
6535 return NULL;
6538 /* This is called to see whether a fixup should be adjusted to use a
6539 section symbol. We take the opportunity to change a fixup against
6540 a symbol in the TOC subsegment into a reloc against the
6541 corresponding .tc symbol. */
6544 ppc_fix_adjustable (fixS *fix)
6546 valueT val = resolve_symbol_value (fix->fx_addsy);
6547 segT symseg = S_GET_SEGMENT (fix->fx_addsy);
6548 symbolS* csect;
6550 if (symseg == absolute_section)
6551 return 0;
6553 /* Always adjust symbols in debugging sections. */
6554 if (bfd_section_flags (symseg) & SEC_DEBUGGING)
6555 return 1;
6557 if (ppc_toc_csect != (symbolS *) NULL
6558 && fix->fx_addsy != ppc_toc_csect
6559 && symseg == data_section
6560 && val >= ppc_toc_frag->fr_address
6561 && (ppc_after_toc_frag == (fragS *) NULL
6562 || val < ppc_after_toc_frag->fr_address))
6564 symbolS *sy;
6566 for (sy = symbol_next (ppc_toc_csect);
6567 sy != (symbolS *) NULL;
6568 sy = symbol_next (sy))
6570 TC_SYMFIELD_TYPE *sy_tc = symbol_get_tc (sy);
6572 if (sy_tc->symbol_class == XMC_TC0)
6573 continue;
6574 if (sy_tc->symbol_class != XMC_TC
6575 && sy_tc->symbol_class != XMC_TE)
6576 break;
6577 if (val == resolve_symbol_value (sy))
6579 fix->fx_addsy = sy;
6580 fix->fx_addnumber = val - ppc_toc_frag->fr_address;
6581 return 0;
6585 as_bad_where (fix->fx_file, fix->fx_line,
6586 _("symbol in .toc does not match any .tc"));
6589 /* Possibly adjust the reloc to be against the csect. */
6590 if ((csect = ppc_get_csect_to_adjust (fix->fx_addsy)) != NULL)
6592 fix->fx_offset += val - symbol_get_frag (csect)->fr_address;
6593 fix->fx_addsy = csect;
6596 if ((csect = ppc_get_csect_to_adjust (fix->fx_subsy)) != NULL)
6598 fix->fx_offset -= resolve_symbol_value (fix->fx_subsy)
6599 - symbol_get_frag (csect)->fr_address;
6600 fix->fx_subsy = csect;
6603 /* Adjust a reloc against a .lcomm symbol to be against the base
6604 .lcomm. */
6605 if (symseg == bss_section
6606 && ! S_IS_EXTERNAL (fix->fx_addsy)
6607 && symbol_get_tc (fix->fx_addsy)->subseg == 0)
6609 symbolS *sy = symbol_get_frag (fix->fx_addsy)->fr_symbol;
6611 fix->fx_offset += val - resolve_symbol_value (sy);
6612 fix->fx_addsy = sy;
6615 return 0;
6618 /* A reloc from one csect to another must be kept. The assembler
6619 will, of course, keep relocs between sections, and it will keep
6620 absolute relocs, but we need to force it to keep PC relative relocs
6621 between two csects in the same section. */
6624 ppc_force_relocation (fixS *fix)
6626 /* At this point fix->fx_addsy should already have been converted to
6627 a csect symbol. If the csect does not include the fragment, then
6628 we need to force the relocation. */
6629 if (fix->fx_pcrel
6630 && fix->fx_addsy != NULL
6631 && symbol_get_tc (fix->fx_addsy)->subseg != 0
6632 && ((symbol_get_frag (fix->fx_addsy)->fr_address
6633 > fix->fx_frag->fr_address)
6634 || (symbol_get_tc (fix->fx_addsy)->next != NULL
6635 && (symbol_get_frag (symbol_get_tc (fix->fx_addsy)->next)->fr_address
6636 <= fix->fx_frag->fr_address))))
6637 return 1;
6639 return generic_force_reloc (fix);
6641 #endif /* OBJ_XCOFF */
6643 #ifdef OBJ_ELF
6644 /* If this function returns non-zero, it guarantees that a relocation
6645 will be emitted for a fixup. */
6648 ppc_force_relocation (fixS *fix)
6650 /* Branch prediction relocations must force a relocation, as must
6651 the vtable description relocs. */
6652 switch (fix->fx_r_type)
6654 case BFD_RELOC_PPC_B16_BRTAKEN:
6655 case BFD_RELOC_PPC_B16_BRNTAKEN:
6656 case BFD_RELOC_PPC_BA16_BRTAKEN:
6657 case BFD_RELOC_PPC_BA16_BRNTAKEN:
6658 case BFD_RELOC_24_PLT_PCREL:
6659 case BFD_RELOC_PPC64_TOC:
6660 return 1;
6661 case BFD_RELOC_PPC_B26:
6662 case BFD_RELOC_PPC_BA26:
6663 case BFD_RELOC_PPC_B16:
6664 case BFD_RELOC_PPC_BA16:
6665 case BFD_RELOC_PPC64_REL24_NOTOC:
6666 case BFD_RELOC_PPC64_REL24_P9NOTOC:
6667 /* All branch fixups targeting a localentry symbol must
6668 force a relocation. */
6669 if (fix->fx_addsy)
6671 asymbol *bfdsym = symbol_get_bfdsym (fix->fx_addsy);
6672 elf_symbol_type *elfsym = elf_symbol_from (bfdsym);
6673 gas_assert (elfsym);
6674 if ((STO_PPC64_LOCAL_MASK & elfsym->internal_elf_sym.st_other) != 0)
6675 return 1;
6677 break;
6678 default:
6679 break;
6682 if (fix->fx_r_type >= BFD_RELOC_PPC_TLS
6683 && fix->fx_r_type <= BFD_RELOC_PPC64_TLS_PCREL)
6684 return 1;
6686 return generic_force_reloc (fix);
6690 ppc_fix_adjustable (fixS *fix)
6692 switch (fix->fx_r_type)
6694 /* All branch fixups targeting a localentry symbol must
6695 continue using the symbol. */
6696 case BFD_RELOC_PPC_B26:
6697 case BFD_RELOC_PPC_BA26:
6698 case BFD_RELOC_PPC_B16:
6699 case BFD_RELOC_PPC_BA16:
6700 case BFD_RELOC_PPC_B16_BRTAKEN:
6701 case BFD_RELOC_PPC_B16_BRNTAKEN:
6702 case BFD_RELOC_PPC_BA16_BRTAKEN:
6703 case BFD_RELOC_PPC_BA16_BRNTAKEN:
6704 case BFD_RELOC_PPC64_REL24_NOTOC:
6705 case BFD_RELOC_PPC64_REL24_P9NOTOC:
6706 if (fix->fx_addsy)
6708 asymbol *bfdsym = symbol_get_bfdsym (fix->fx_addsy);
6709 elf_symbol_type *elfsym = elf_symbol_from (bfdsym);
6710 gas_assert (elfsym);
6711 if ((STO_PPC64_LOCAL_MASK & elfsym->internal_elf_sym.st_other) != 0)
6712 return 0;
6714 break;
6715 default:
6716 break;
6719 return (fix->fx_r_type != BFD_RELOC_16_GOTOFF
6720 && fix->fx_r_type != BFD_RELOC_LO16_GOTOFF
6721 && fix->fx_r_type != BFD_RELOC_HI16_GOTOFF
6722 && fix->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
6723 && fix->fx_r_type != BFD_RELOC_PPC64_GOT16_DS
6724 && fix->fx_r_type != BFD_RELOC_PPC64_GOT16_LO_DS
6725 && fix->fx_r_type != BFD_RELOC_PPC64_GOT_PCREL34
6726 && fix->fx_r_type != BFD_RELOC_24_PLT_PCREL
6727 && fix->fx_r_type != BFD_RELOC_32_PLTOFF
6728 && fix->fx_r_type != BFD_RELOC_32_PLT_PCREL
6729 && fix->fx_r_type != BFD_RELOC_LO16_PLTOFF
6730 && fix->fx_r_type != BFD_RELOC_HI16_PLTOFF
6731 && fix->fx_r_type != BFD_RELOC_HI16_S_PLTOFF
6732 && fix->fx_r_type != BFD_RELOC_64_PLTOFF
6733 && fix->fx_r_type != BFD_RELOC_64_PLT_PCREL
6734 && fix->fx_r_type != BFD_RELOC_PPC64_PLT16_LO_DS
6735 && fix->fx_r_type != BFD_RELOC_PPC64_PLT_PCREL34
6736 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16
6737 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_LO
6738 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_HI
6739 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_HA
6740 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_DS
6741 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_LO_DS
6742 && fix->fx_r_type != BFD_RELOC_GPREL16
6743 && fix->fx_r_type != BFD_RELOC_PPC_VLE_SDAREL_LO16A
6744 && fix->fx_r_type != BFD_RELOC_PPC_VLE_SDAREL_HI16A
6745 && fix->fx_r_type != BFD_RELOC_PPC_VLE_SDAREL_HA16A
6746 && fix->fx_r_type != BFD_RELOC_VTABLE_INHERIT
6747 && fix->fx_r_type != BFD_RELOC_VTABLE_ENTRY
6748 && !(fix->fx_r_type >= BFD_RELOC_PPC_TLS
6749 && fix->fx_r_type <= BFD_RELOC_PPC64_TLS_PCREL));
6751 #endif
6753 void
6754 ppc_frag_check (struct frag *fragP)
6756 if ((fragP->fr_address & fragP->insn_addr) != 0)
6757 as_bad_where (fragP->fr_file, fragP->fr_line,
6758 _("instruction address is not a multiple of %d"),
6759 fragP->insn_addr + 1);
6762 /* rs_align_code frag handling. */
6764 enum ppc_nop_encoding_for_rs_align_code
6766 PPC_NOP_VANILLA,
6767 PPC_NOP_VLE,
6768 PPC_NOP_GROUP_P6,
6769 PPC_NOP_GROUP_P7
6772 unsigned int
6773 ppc_nop_select (void)
6775 if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
6776 return PPC_NOP_VLE;
6777 if ((ppc_cpu & (PPC_OPCODE_POWER9 | PPC_OPCODE_E500MC)) == 0)
6779 if ((ppc_cpu & PPC_OPCODE_POWER7) != 0)
6780 return PPC_NOP_GROUP_P7;
6781 if ((ppc_cpu & PPC_OPCODE_POWER6) != 0)
6782 return PPC_NOP_GROUP_P6;
6784 return PPC_NOP_VANILLA;
6787 void
6788 ppc_handle_align (struct frag *fragP)
6790 valueT count = (fragP->fr_next->fr_address
6791 - (fragP->fr_address + fragP->fr_fix));
6792 char *dest = fragP->fr_literal + fragP->fr_fix;
6793 enum ppc_nop_encoding_for_rs_align_code nop_select = *dest & 0xff;
6795 /* Pad with zeros if not inserting a whole number of instructions.
6796 We could pad with zeros up to an instruction boundary then follow
6797 with nops but odd counts indicate data in an executable section
6798 so padding with zeros is most appropriate. */
6799 if (count == 0
6800 || (nop_select == PPC_NOP_VLE ? (count & 1) != 0 : (count & 3) != 0))
6802 *dest = 0;
6803 return;
6806 if (nop_select == PPC_NOP_VLE)
6809 fragP->fr_var = 2;
6810 md_number_to_chars (dest, 0x4400, 2);
6812 else
6814 fragP->fr_var = 4;
6816 if (count > 4 * nop_limit && count < 0x2000000)
6818 struct frag *rest;
6820 /* Make a branch, then follow with nops. Insert another
6821 frag to handle the nops. */
6822 md_number_to_chars (dest, 0x48000000 + count, 4);
6823 count -= 4;
6824 if (count == 0)
6825 return;
6827 rest = xmalloc (SIZEOF_STRUCT_FRAG + 4);
6828 memcpy (rest, fragP, SIZEOF_STRUCT_FRAG);
6829 fragP->fr_next = rest;
6830 fragP = rest;
6831 rest->fr_address += rest->fr_fix + 4;
6832 rest->fr_fix = 0;
6833 /* If we leave the next frag as rs_align_code we'll come here
6834 again, resulting in a bunch of branches rather than a
6835 branch followed by nops. */
6836 rest->fr_type = rs_align;
6837 dest = rest->fr_literal;
6840 md_number_to_chars (dest, 0x60000000, 4);
6842 if (nop_select >= PPC_NOP_GROUP_P6)
6844 /* For power6, power7, and power8, we want the last nop to
6845 be a group terminating one. Do this by inserting an
6846 rs_fill frag immediately after this one, with its address
6847 set to the last nop location. This will automatically
6848 reduce the number of nops in the current frag by one. */
6849 if (count > 4)
6851 struct frag *group_nop = xmalloc (SIZEOF_STRUCT_FRAG + 4);
6853 memcpy (group_nop, fragP, SIZEOF_STRUCT_FRAG);
6854 group_nop->fr_address = group_nop->fr_next->fr_address - 4;
6855 group_nop->fr_fix = 0;
6856 group_nop->fr_offset = 1;
6857 group_nop->fr_type = rs_fill;
6858 fragP->fr_next = group_nop;
6859 dest = group_nop->fr_literal;
6862 if (nop_select == PPC_NOP_GROUP_P6)
6863 /* power6 group terminating nop: "ori 1,1,0". */
6864 md_number_to_chars (dest, 0x60210000, 4);
6865 else
6866 /* power7/power8 group terminating nop: "ori 2,2,0". */
6867 md_number_to_chars (dest, 0x60420000, 4);
6872 /* Apply a fixup to the object code. This is called for all the
6873 fixups we generated by the calls to fix_new_exp, above. */
6875 void
6876 md_apply_fix (fixS *fixP, valueT *valP, segT seg)
6878 valueT value = * valP;
6879 offsetT fieldval;
6880 const struct powerpc_operand *operand;
6882 #ifdef OBJ_ELF
6883 if (fixP->fx_addsy != NULL)
6885 /* Hack around bfd_install_relocation brain damage. */
6886 if (fixP->fx_pcrel)
6887 value += fixP->fx_frag->fr_address + fixP->fx_where;
6889 if (fixP->fx_addsy == abs_section_sym)
6890 fixP->fx_done = 1;
6892 else
6893 fixP->fx_done = 1;
6894 #else
6895 /* FIXME FIXME FIXME: The value we are passed in *valP includes
6896 the symbol values. If we are doing this relocation the code in
6897 write.c is going to call bfd_install_relocation, which is also
6898 going to use the symbol value. That means that if the reloc is
6899 fully resolved we want to use *valP since bfd_install_relocation is
6900 not being used.
6901 However, if the reloc is not fully resolved we do not want to
6902 use *valP, and must use fx_offset instead. If the relocation
6903 is PC-relative, we then need to re-apply md_pcrel_from_section
6904 to this new relocation value. */
6905 if (fixP->fx_addsy == (symbolS *) NULL)
6906 fixP->fx_done = 1;
6908 else
6910 value = fixP->fx_offset;
6911 if (fixP->fx_pcrel)
6912 value -= md_pcrel_from_section (fixP, seg);
6914 #endif
6916 /* We are only able to convert some relocs to pc-relative. */
6917 if (fixP->fx_pcrel)
6919 switch (fixP->fx_r_type)
6921 case BFD_RELOC_64:
6922 fixP->fx_r_type = BFD_RELOC_64_PCREL;
6923 break;
6925 case BFD_RELOC_32:
6926 fixP->fx_r_type = BFD_RELOC_32_PCREL;
6927 break;
6929 case BFD_RELOC_16:
6930 fixP->fx_r_type = BFD_RELOC_16_PCREL;
6931 break;
6933 case BFD_RELOC_LO16:
6934 fixP->fx_r_type = BFD_RELOC_LO16_PCREL;
6935 break;
6937 case BFD_RELOC_HI16:
6938 fixP->fx_r_type = BFD_RELOC_HI16_PCREL;
6939 break;
6941 case BFD_RELOC_HI16_S:
6942 fixP->fx_r_type = BFD_RELOC_HI16_S_PCREL;
6943 break;
6945 case BFD_RELOC_PPC64_ADDR16_HIGH:
6946 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGH;
6947 break;
6949 case BFD_RELOC_PPC64_ADDR16_HIGHA:
6950 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHA;
6951 break;
6953 case BFD_RELOC_PPC64_HIGHER:
6954 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHER;
6955 break;
6957 case BFD_RELOC_PPC64_HIGHER_S:
6958 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHERA;
6959 break;
6961 case BFD_RELOC_PPC64_HIGHEST:
6962 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHEST;
6963 break;
6965 case BFD_RELOC_PPC64_HIGHEST_S:
6966 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHESTA;
6967 break;
6969 case BFD_RELOC_PPC64_ADDR16_HIGHER34:
6970 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHER34;
6971 break;
6973 case BFD_RELOC_PPC64_ADDR16_HIGHERA34:
6974 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHERA34;
6975 break;
6977 case BFD_RELOC_PPC64_ADDR16_HIGHEST34:
6978 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHEST34;
6979 break;
6981 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:
6982 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHESTA34;
6983 break;
6985 case BFD_RELOC_PPC_16DX_HA:
6986 fixP->fx_r_type = BFD_RELOC_PPC_REL16DX_HA;
6987 break;
6989 case BFD_RELOC_PPC64_D34:
6990 fixP->fx_r_type = BFD_RELOC_PPC64_PCREL34;
6991 break;
6993 case BFD_RELOC_PPC64_D28:
6994 fixP->fx_r_type = BFD_RELOC_PPC64_PCREL28;
6995 break;
6997 default:
6998 break;
7001 else if (!fixP->fx_done
7002 && fixP->fx_r_type == BFD_RELOC_PPC_16DX_HA)
7004 /* addpcis is relative to next insn address. */
7005 value -= 4;
7006 fixP->fx_r_type = BFD_RELOC_PPC_REL16DX_HA;
7007 fixP->fx_pcrel = 1;
7010 operand = NULL;
7011 if (fixP->fx_pcrel_adjust != 0)
7013 /* This is a fixup on an instruction. */
7014 ppc_opindex_t opindex = fixP->fx_pcrel_adjust & PPC_OPINDEX_MAX;
7016 operand = &powerpc_operands[opindex];
7017 #ifdef OBJ_XCOFF
7018 /* An instruction like `lwz 9,sym(30)' when `sym' is not a TOC symbol
7019 does not generate a reloc. It uses the offset of `sym' within its
7020 csect. Other usages, such as `.long sym', generate relocs. This
7021 is the documented behaviour of non-TOC symbols. */
7022 if ((operand->flags & PPC_OPERAND_PARENS) != 0
7023 && (operand->bitm & 0xfff0) == 0xfff0
7024 && operand->shift == 0
7025 && (operand->insert == NULL || ppc_obj64)
7026 && fixP->fx_addsy != NULL
7027 && symbol_get_tc (fixP->fx_addsy)->subseg != 0
7028 && !ppc_is_toc_sym (fixP->fx_addsy)
7029 && S_GET_SEGMENT (fixP->fx_addsy) != bss_section)
7031 value = fixP->fx_offset;
7032 fixP->fx_done = 1;
7035 /* During parsing of instructions, a TOC16 reloc is generated for
7036 instructions such as 'lwz RT,SYM(RB)' if SYM is a symbol defined
7037 in the toc. But at parse time, SYM may be not yet defined, so
7038 check again here. */
7039 if (fixP->fx_r_type == BFD_RELOC_16
7040 && fixP->fx_addsy != NULL
7041 && ppc_is_toc_sym (fixP->fx_addsy))
7042 fixP->fx_r_type = BFD_RELOC_PPC_TOC16;
7043 #endif
7046 /* Calculate value to be stored in field. */
7047 fieldval = value;
7048 switch (fixP->fx_r_type)
7050 #ifdef OBJ_ELF
7051 case BFD_RELOC_PPC64_ADDR16_LO_DS:
7052 case BFD_RELOC_PPC_VLE_LO16A:
7053 case BFD_RELOC_PPC_VLE_LO16D:
7054 #endif
7055 case BFD_RELOC_LO16:
7056 case BFD_RELOC_LO16_PCREL:
7057 fieldval = value & 0xffff;
7058 sign_extend_16:
7059 if (operand != NULL && (operand->flags & PPC_OPERAND_SIGNED) != 0)
7060 fieldval = SEX16 (fieldval);
7061 fixP->fx_no_overflow = 1;
7062 break;
7064 case BFD_RELOC_HI16:
7065 case BFD_RELOC_HI16_PCREL:
7066 #ifdef OBJ_ELF
7067 if (REPORT_OVERFLOW_HI && ppc_obj64)
7069 fieldval = value >> 16;
7070 if (operand != NULL && (operand->flags & PPC_OPERAND_SIGNED) != 0)
7072 valueT sign = (((valueT) -1 >> 16) + 1) >> 1;
7073 fieldval = ((valueT) fieldval ^ sign) - sign;
7075 break;
7077 /* Fallthru */
7079 case BFD_RELOC_PPC_VLE_HI16A:
7080 case BFD_RELOC_PPC_VLE_HI16D:
7081 case BFD_RELOC_PPC64_ADDR16_HIGH:
7082 #endif
7083 fieldval = PPC_HI (value);
7084 goto sign_extend_16;
7086 case BFD_RELOC_HI16_S:
7087 case BFD_RELOC_HI16_S_PCREL:
7088 case BFD_RELOC_PPC_16DX_HA:
7089 case BFD_RELOC_PPC_REL16DX_HA:
7090 #ifdef OBJ_ELF
7091 if (REPORT_OVERFLOW_HI && ppc_obj64)
7093 fieldval = (value + 0x8000) >> 16;
7094 if (operand != NULL && (operand->flags & PPC_OPERAND_SIGNED) != 0)
7096 valueT sign = (((valueT) -1 >> 16) + 1) >> 1;
7097 fieldval = ((valueT) fieldval ^ sign) - sign;
7099 break;
7101 /* Fallthru */
7103 case BFD_RELOC_PPC_VLE_HA16A:
7104 case BFD_RELOC_PPC_VLE_HA16D:
7105 case BFD_RELOC_PPC64_ADDR16_HIGHA:
7106 #endif
7107 fieldval = PPC_HA (value);
7108 goto sign_extend_16;
7110 #ifdef OBJ_ELF
7111 case BFD_RELOC_PPC64_HIGHER:
7112 fieldval = PPC_HIGHER (value);
7113 goto sign_extend_16;
7115 case BFD_RELOC_PPC64_HIGHER_S:
7116 fieldval = PPC_HIGHERA (value);
7117 goto sign_extend_16;
7119 case BFD_RELOC_PPC64_HIGHEST:
7120 fieldval = PPC_HIGHEST (value);
7121 goto sign_extend_16;
7123 case BFD_RELOC_PPC64_HIGHEST_S:
7124 fieldval = PPC_HIGHESTA (value);
7125 goto sign_extend_16;
7126 #endif
7128 default:
7129 break;
7132 if (operand != NULL)
7134 /* Handle relocs in an insn. */
7135 switch (fixP->fx_r_type)
7137 #ifdef OBJ_ELF
7138 /* The following relocs can't be calculated by the assembler.
7139 Leave the field zero. */
7140 case BFD_RELOC_PPC_TPREL16:
7141 case BFD_RELOC_PPC_TPREL16_LO:
7142 case BFD_RELOC_PPC_TPREL16_HI:
7143 case BFD_RELOC_PPC_TPREL16_HA:
7144 case BFD_RELOC_PPC_DTPREL16:
7145 case BFD_RELOC_PPC_DTPREL16_LO:
7146 case BFD_RELOC_PPC_DTPREL16_HI:
7147 case BFD_RELOC_PPC_DTPREL16_HA:
7148 case BFD_RELOC_PPC_GOT_TLSGD16:
7149 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
7150 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
7151 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
7152 case BFD_RELOC_PPC_GOT_TLSLD16:
7153 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
7154 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
7155 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
7156 case BFD_RELOC_PPC_GOT_TPREL16:
7157 case BFD_RELOC_PPC_GOT_TPREL16_LO:
7158 case BFD_RELOC_PPC_GOT_TPREL16_HI:
7159 case BFD_RELOC_PPC_GOT_TPREL16_HA:
7160 case BFD_RELOC_PPC_GOT_DTPREL16:
7161 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
7162 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
7163 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
7164 case BFD_RELOC_PPC64_TPREL16_DS:
7165 case BFD_RELOC_PPC64_TPREL16_LO_DS:
7166 case BFD_RELOC_PPC64_TPREL16_HIGH:
7167 case BFD_RELOC_PPC64_TPREL16_HIGHA:
7168 case BFD_RELOC_PPC64_TPREL16_HIGHER:
7169 case BFD_RELOC_PPC64_TPREL16_HIGHERA:
7170 case BFD_RELOC_PPC64_TPREL16_HIGHEST:
7171 case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
7172 case BFD_RELOC_PPC64_DTPREL16_HIGH:
7173 case BFD_RELOC_PPC64_DTPREL16_HIGHA:
7174 case BFD_RELOC_PPC64_DTPREL16_DS:
7175 case BFD_RELOC_PPC64_DTPREL16_LO_DS:
7176 case BFD_RELOC_PPC64_DTPREL16_HIGHER:
7177 case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
7178 case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
7179 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
7180 case BFD_RELOC_PPC64_TPREL34:
7181 case BFD_RELOC_PPC64_DTPREL34:
7182 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:
7183 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:
7184 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:
7185 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:
7186 gas_assert (fixP->fx_addsy != NULL);
7187 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7188 fieldval = 0;
7189 break;
7191 /* These also should leave the field zero for the same
7192 reason. Note that older versions of gas wrote values
7193 here. If we want to go back to the old behaviour, then
7194 all _LO and _LO_DS cases will need to be treated like
7195 BFD_RELOC_LO16_PCREL above. Similarly for _HI etc. */
7196 case BFD_RELOC_16_GOTOFF:
7197 case BFD_RELOC_LO16_GOTOFF:
7198 case BFD_RELOC_HI16_GOTOFF:
7199 case BFD_RELOC_HI16_S_GOTOFF:
7200 case BFD_RELOC_LO16_PLTOFF:
7201 case BFD_RELOC_HI16_PLTOFF:
7202 case BFD_RELOC_HI16_S_PLTOFF:
7203 case BFD_RELOC_GPREL16:
7204 case BFD_RELOC_16_BASEREL:
7205 case BFD_RELOC_LO16_BASEREL:
7206 case BFD_RELOC_HI16_BASEREL:
7207 case BFD_RELOC_HI16_S_BASEREL:
7208 case BFD_RELOC_PPC_TOC16:
7209 case BFD_RELOC_PPC64_TOC16_LO:
7210 case BFD_RELOC_PPC64_TOC16_HI:
7211 case BFD_RELOC_PPC64_TOC16_HA:
7212 case BFD_RELOC_PPC64_PLTGOT16:
7213 case BFD_RELOC_PPC64_PLTGOT16_LO:
7214 case BFD_RELOC_PPC64_PLTGOT16_HI:
7215 case BFD_RELOC_PPC64_PLTGOT16_HA:
7216 case BFD_RELOC_PPC64_GOT16_DS:
7217 case BFD_RELOC_PPC64_GOT16_LO_DS:
7218 case BFD_RELOC_PPC64_PLT16_LO_DS:
7219 case BFD_RELOC_PPC64_SECTOFF_DS:
7220 case BFD_RELOC_PPC64_SECTOFF_LO_DS:
7221 case BFD_RELOC_PPC64_TOC16_DS:
7222 case BFD_RELOC_PPC64_TOC16_LO_DS:
7223 case BFD_RELOC_PPC64_PLTGOT16_DS:
7224 case BFD_RELOC_PPC64_PLTGOT16_LO_DS:
7225 case BFD_RELOC_PPC_EMB_NADDR16:
7226 case BFD_RELOC_PPC_EMB_NADDR16_LO:
7227 case BFD_RELOC_PPC_EMB_NADDR16_HI:
7228 case BFD_RELOC_PPC_EMB_NADDR16_HA:
7229 case BFD_RELOC_PPC_EMB_SDAI16:
7230 case BFD_RELOC_PPC_EMB_SDA2I16:
7231 case BFD_RELOC_PPC_EMB_SDA2REL:
7232 case BFD_RELOC_PPC_EMB_SDA21:
7233 case BFD_RELOC_PPC_EMB_MRKREF:
7234 case BFD_RELOC_PPC_EMB_RELSEC16:
7235 case BFD_RELOC_PPC_EMB_RELST_LO:
7236 case BFD_RELOC_PPC_EMB_RELST_HI:
7237 case BFD_RELOC_PPC_EMB_RELST_HA:
7238 case BFD_RELOC_PPC_EMB_BIT_FLD:
7239 case BFD_RELOC_PPC_EMB_RELSDA:
7240 case BFD_RELOC_PPC_VLE_SDA21:
7241 case BFD_RELOC_PPC_VLE_SDA21_LO:
7242 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
7243 case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
7244 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
7245 case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
7246 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
7247 case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
7248 case BFD_RELOC_PPC64_GOT_PCREL34:
7249 case BFD_RELOC_PPC64_PLT_PCREL34:
7250 gas_assert (fixP->fx_addsy != NULL);
7251 /* Fallthru */
7253 case BFD_RELOC_PPC_TLS:
7254 case BFD_RELOC_PPC_TLSGD:
7255 case BFD_RELOC_PPC_TLSLD:
7256 case BFD_RELOC_PPC64_TLS_PCREL:
7257 fieldval = 0;
7258 break;
7259 #endif
7261 #ifdef OBJ_XCOFF
7262 case BFD_RELOC_PPC_B16:
7263 /* Adjust the offset to the instruction boundary. */
7264 fieldval += 2;
7265 break;
7266 #endif
7268 case BFD_RELOC_VTABLE_INHERIT:
7269 case BFD_RELOC_VTABLE_ENTRY:
7270 case BFD_RELOC_PPC_DTPMOD:
7271 case BFD_RELOC_PPC_TPREL:
7272 case BFD_RELOC_PPC_DTPREL:
7273 case BFD_RELOC_PPC_COPY:
7274 case BFD_RELOC_PPC_GLOB_DAT:
7275 case BFD_RELOC_32_PLT_PCREL:
7276 case BFD_RELOC_PPC_EMB_NADDR32:
7277 case BFD_RELOC_PPC64_TOC:
7278 case BFD_RELOC_CTOR:
7279 case BFD_RELOC_32:
7280 case BFD_RELOC_32_PCREL:
7281 case BFD_RELOC_RVA:
7282 case BFD_RELOC_64:
7283 case BFD_RELOC_64_PCREL:
7284 case BFD_RELOC_PPC64_ADDR64_LOCAL:
7285 as_bad_where (fixP->fx_file, fixP->fx_line,
7286 _("%s unsupported as instruction fixup"),
7287 bfd_get_reloc_code_name (fixP->fx_r_type));
7288 fixP->fx_done = 1;
7289 return;
7291 default:
7292 break;
7295 #ifdef OBJ_ELF
7296 /* powerpc uses RELA style relocs, so if emitting a reloc the field
7297 contents can stay at zero. */
7298 #define APPLY_RELOC fixP->fx_done
7299 #else
7300 #define APPLY_RELOC 1
7301 #endif
7302 /* We need to call the insert function even when fieldval is
7303 zero if the insert function would translate that zero to a
7304 bit pattern other than all zeros. */
7305 if ((fieldval != 0 && APPLY_RELOC) || operand->insert != NULL)
7307 uint64_t insn;
7308 unsigned char *where;
7310 /* Fetch the instruction, insert the fully resolved operand
7311 value, and stuff the instruction back again. */
7312 where = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
7313 if (target_big_endian)
7315 if (fixP->fx_size < 4)
7316 insn = bfd_getb16 (where);
7317 else
7319 insn = bfd_getb32 (where);
7320 if (fixP->fx_size > 4)
7321 insn = insn << 32 | bfd_getb32 (where + 4);
7324 else
7326 if (fixP->fx_size < 4)
7327 insn = bfd_getl16 (where);
7328 else
7330 insn = bfd_getl32 (where);
7331 if (fixP->fx_size > 4)
7332 insn = insn << 32 | bfd_getl32 (where + 4);
7335 insn = ppc_insert_operand (insn, operand, fieldval,
7336 fixP->tc_fix_data.ppc_cpu,
7337 fixP->fx_file, fixP->fx_line);
7338 if (target_big_endian)
7340 if (fixP->fx_size < 4)
7341 bfd_putb16 (insn, where);
7342 else
7344 if (fixP->fx_size > 4)
7346 bfd_putb32 (insn, where + 4);
7347 insn >>= 32;
7349 bfd_putb32 (insn, where);
7352 else
7354 if (fixP->fx_size < 4)
7355 bfd_putl16 (insn, where);
7356 else
7358 if (fixP->fx_size > 4)
7360 bfd_putl32 (insn, where + 4);
7361 insn >>= 32;
7363 bfd_putl32 (insn, where);
7368 if (fixP->fx_done)
7369 /* Nothing else to do here. */
7370 return;
7372 gas_assert (fixP->fx_addsy != NULL);
7373 if (fixP->fx_r_type == BFD_RELOC_NONE)
7375 const char *sfile;
7376 unsigned int sline;
7378 /* Use expr_symbol_where to see if this is an expression
7379 symbol. */
7380 if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
7381 as_bad_where (fixP->fx_file, fixP->fx_line,
7382 _("unresolved expression that must be resolved"));
7383 else
7384 as_bad_where (fixP->fx_file, fixP->fx_line,
7385 _("unsupported relocation against %s"),
7386 S_GET_NAME (fixP->fx_addsy));
7387 fixP->fx_done = 1;
7388 return;
7391 else
7393 /* Handle relocs in data. */
7394 switch (fixP->fx_r_type)
7396 case BFD_RELOC_VTABLE_INHERIT:
7397 if (fixP->fx_addsy
7398 && !S_IS_DEFINED (fixP->fx_addsy)
7399 && !S_IS_WEAK (fixP->fx_addsy))
7400 S_SET_WEAK (fixP->fx_addsy);
7401 /* Fallthru */
7403 case BFD_RELOC_VTABLE_ENTRY:
7404 fixP->fx_done = 0;
7405 break;
7407 #ifdef OBJ_ELF
7408 /* These can appear with @l etc. in data. */
7409 case BFD_RELOC_LO16:
7410 case BFD_RELOC_LO16_PCREL:
7411 case BFD_RELOC_HI16:
7412 case BFD_RELOC_HI16_PCREL:
7413 case BFD_RELOC_HI16_S:
7414 case BFD_RELOC_HI16_S_PCREL:
7415 case BFD_RELOC_PPC64_HIGHER:
7416 case BFD_RELOC_PPC64_HIGHER_S:
7417 case BFD_RELOC_PPC64_HIGHEST:
7418 case BFD_RELOC_PPC64_HIGHEST_S:
7419 case BFD_RELOC_PPC64_ADDR16_HIGH:
7420 case BFD_RELOC_PPC64_ADDR16_HIGHA:
7421 case BFD_RELOC_PPC64_ADDR64_LOCAL:
7422 break;
7424 case BFD_RELOC_PPC_DTPMOD:
7425 case BFD_RELOC_PPC_TPREL:
7426 case BFD_RELOC_PPC_DTPREL:
7427 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7428 break;
7430 /* Just punt all of these to the linker. */
7431 case BFD_RELOC_PPC_B16_BRTAKEN:
7432 case BFD_RELOC_PPC_B16_BRNTAKEN:
7433 case BFD_RELOC_16_GOTOFF:
7434 case BFD_RELOC_LO16_GOTOFF:
7435 case BFD_RELOC_HI16_GOTOFF:
7436 case BFD_RELOC_HI16_S_GOTOFF:
7437 case BFD_RELOC_LO16_PLTOFF:
7438 case BFD_RELOC_HI16_PLTOFF:
7439 case BFD_RELOC_HI16_S_PLTOFF:
7440 case BFD_RELOC_PPC_COPY:
7441 case BFD_RELOC_PPC_GLOB_DAT:
7442 case BFD_RELOC_16_BASEREL:
7443 case BFD_RELOC_LO16_BASEREL:
7444 case BFD_RELOC_HI16_BASEREL:
7445 case BFD_RELOC_HI16_S_BASEREL:
7446 case BFD_RELOC_PPC_TLS:
7447 case BFD_RELOC_PPC_DTPREL16_LO:
7448 case BFD_RELOC_PPC_DTPREL16_HI:
7449 case BFD_RELOC_PPC_DTPREL16_HA:
7450 case BFD_RELOC_PPC_TPREL16_LO:
7451 case BFD_RELOC_PPC_TPREL16_HI:
7452 case BFD_RELOC_PPC_TPREL16_HA:
7453 case BFD_RELOC_PPC_GOT_TLSGD16:
7454 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
7455 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
7456 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
7457 case BFD_RELOC_PPC_GOT_TLSLD16:
7458 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
7459 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
7460 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
7461 case BFD_RELOC_PPC_GOT_DTPREL16:
7462 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
7463 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
7464 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
7465 case BFD_RELOC_PPC_GOT_TPREL16:
7466 case BFD_RELOC_PPC_GOT_TPREL16_LO:
7467 case BFD_RELOC_PPC_GOT_TPREL16_HI:
7468 case BFD_RELOC_PPC_GOT_TPREL16_HA:
7469 case BFD_RELOC_24_PLT_PCREL:
7470 case BFD_RELOC_PPC_LOCAL24PC:
7471 case BFD_RELOC_32_PLT_PCREL:
7472 case BFD_RELOC_GPREL16:
7473 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
7474 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
7475 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
7476 case BFD_RELOC_PPC_EMB_NADDR32:
7477 case BFD_RELOC_PPC_EMB_NADDR16:
7478 case BFD_RELOC_PPC_EMB_NADDR16_LO:
7479 case BFD_RELOC_PPC_EMB_NADDR16_HI:
7480 case BFD_RELOC_PPC_EMB_NADDR16_HA:
7481 case BFD_RELOC_PPC_EMB_SDAI16:
7482 case BFD_RELOC_PPC_EMB_SDA2REL:
7483 case BFD_RELOC_PPC_EMB_SDA2I16:
7484 case BFD_RELOC_PPC_EMB_SDA21:
7485 case BFD_RELOC_PPC_VLE_SDA21_LO:
7486 case BFD_RELOC_PPC_EMB_MRKREF:
7487 case BFD_RELOC_PPC_EMB_RELSEC16:
7488 case BFD_RELOC_PPC_EMB_RELST_LO:
7489 case BFD_RELOC_PPC_EMB_RELST_HI:
7490 case BFD_RELOC_PPC_EMB_RELST_HA:
7491 case BFD_RELOC_PPC_EMB_BIT_FLD:
7492 case BFD_RELOC_PPC_EMB_RELSDA:
7493 case BFD_RELOC_PPC64_TOC:
7494 case BFD_RELOC_PPC_TOC16:
7495 case BFD_RELOC_PPC_TOC16_LO:
7496 case BFD_RELOC_PPC_TOC16_HI:
7497 case BFD_RELOC_PPC64_TOC16_LO:
7498 case BFD_RELOC_PPC64_TOC16_HI:
7499 case BFD_RELOC_PPC64_TOC16_HA:
7500 case BFD_RELOC_PPC64_DTPREL16_HIGH:
7501 case BFD_RELOC_PPC64_DTPREL16_HIGHA:
7502 case BFD_RELOC_PPC64_DTPREL16_HIGHER:
7503 case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
7504 case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
7505 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
7506 case BFD_RELOC_PPC64_TPREL16_HIGH:
7507 case BFD_RELOC_PPC64_TPREL16_HIGHA:
7508 case BFD_RELOC_PPC64_TPREL16_HIGHER:
7509 case BFD_RELOC_PPC64_TPREL16_HIGHERA:
7510 case BFD_RELOC_PPC64_TPREL16_HIGHEST:
7511 case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
7512 case BFD_RELOC_PPC64_TLS_PCREL:
7513 fixP->fx_done = 0;
7514 break;
7515 #endif
7517 #ifdef OBJ_XCOFF
7518 case BFD_RELOC_PPC_TLSGD:
7519 case BFD_RELOC_PPC_TLSLD:
7520 case BFD_RELOC_PPC_TLSLE:
7521 case BFD_RELOC_PPC_TLSIE:
7522 case BFD_RELOC_PPC_TLSM:
7523 case BFD_RELOC_PPC64_TLSGD:
7524 case BFD_RELOC_PPC64_TLSLD:
7525 case BFD_RELOC_PPC64_TLSLE:
7526 case BFD_RELOC_PPC64_TLSIE:
7527 case BFD_RELOC_PPC64_TLSM:
7528 gas_assert (fixP->fx_addsy != NULL);
7529 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7530 break;
7532 /* Officially, R_TLSML relocations must be from a TOC entry
7533 targeting itself. In practice, this TOC entry is always
7534 named (or .rename) "_$TLSML".
7535 Thus, as it doesn't seem possible to retrieve the symbol
7536 being relocated here, we simply check that the symbol
7537 targeted by R_TLSML is indeed a TOC entry named "_$TLSML".
7538 FIXME: Find a way to correctly check R_TLSML relocations
7539 as described above. */
7540 case BFD_RELOC_PPC_TLSML:
7541 case BFD_RELOC_PPC64_TLSML:
7542 gas_assert (fixP->fx_addsy != NULL);
7543 if ((symbol_get_tc (fixP->fx_addsy)->symbol_class != XMC_TC
7544 || symbol_get_tc (fixP->fx_addsy)->symbol_class != XMC_TE)
7545 && strcmp (symbol_get_tc (fixP->fx_addsy)->real_name, "_$TLSML") != 0)
7546 as_bad_where (fixP->fx_file, fixP->fx_line,
7547 _("R_TLSML relocation doesn't target a "
7548 "TOC entry named \"_$TLSML\": %s"), S_GET_NAME(fixP->fx_addsy));
7549 fieldval = 0;
7550 break;
7552 case BFD_RELOC_NONE:
7553 #endif
7554 case BFD_RELOC_CTOR:
7555 case BFD_RELOC_32:
7556 case BFD_RELOC_32_PCREL:
7557 case BFD_RELOC_RVA:
7558 case BFD_RELOC_64:
7559 case BFD_RELOC_64_PCREL:
7560 case BFD_RELOC_16:
7561 case BFD_RELOC_16_PCREL:
7562 case BFD_RELOC_8:
7563 break;
7565 default:
7566 fprintf (stderr,
7567 _("Gas failure, reloc value %d\n"), fixP->fx_r_type);
7568 fflush (stderr);
7569 abort ();
7572 if (fixP->fx_size && APPLY_RELOC)
7573 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
7574 fieldval, fixP->fx_size);
7575 if (warn_476
7576 && (seg->flags & SEC_CODE) != 0
7577 && fixP->fx_size == 4
7578 && fixP->fx_done
7579 && !fixP->fx_tcbit
7580 && (fixP->fx_r_type == BFD_RELOC_32
7581 || fixP->fx_r_type == BFD_RELOC_CTOR
7582 || fixP->fx_r_type == BFD_RELOC_32_PCREL))
7583 as_warn_where (fixP->fx_file, fixP->fx_line,
7584 _("data in executable section"));
7587 #ifdef OBJ_ELF
7588 ppc_elf_validate_fix (fixP, seg);
7589 fixP->fx_addnumber = value;
7591 /* PowerPC uses RELA relocs, ie. the reloc addend is stored separately
7592 from the section contents. If we are going to be emitting a reloc
7593 then the section contents are immaterial, so don't warn if they
7594 happen to overflow. Leave such warnings to ld. */
7595 if (!fixP->fx_done)
7597 fixP->fx_no_overflow = 1;
7599 /* Arrange to emit .TOC. as a normal symbol if used in anything
7600 but .TOC.@tocbase. */
7601 if (ppc_obj64
7602 && fixP->fx_r_type != BFD_RELOC_PPC64_TOC
7603 && fixP->fx_addsy != NULL
7604 && strcmp (S_GET_NAME (fixP->fx_addsy), ".TOC.") == 0)
7605 symbol_get_bfdsym (fixP->fx_addsy)->flags |= BSF_KEEP;
7607 #else
7608 if (fixP->fx_r_type == BFD_RELOC_PPC_TOC16
7609 || fixP->fx_r_type == BFD_RELOC_PPC_TOC16_HI
7610 || fixP->fx_r_type == BFD_RELOC_PPC_TOC16_LO)
7612 /* We want to use the offset within the toc, not the actual VMA
7613 of the symbol. */
7614 fixP->fx_addnumber = (- bfd_section_vma (S_GET_SEGMENT (fixP->fx_addsy))
7615 - S_GET_VALUE (ppc_toc_csect));
7617 /* The high bits must be adjusted for the low bits being signed. */
7618 if (fixP->fx_r_type == BFD_RELOC_PPC_TOC16_HI) {
7619 fixP->fx_addnumber += 0x8000;
7622 /* Set *valP to avoid errors. */
7623 *valP = value;
7625 else if (fixP->fx_r_type == BFD_RELOC_PPC_TLSM
7626 || fixP->fx_r_type == BFD_RELOC_PPC64_TLSM
7627 || fixP->fx_r_type == BFD_RELOC_PPC_TLSML
7628 || fixP->fx_r_type == BFD_RELOC_PPC64_TLSML)
7629 /* AIX ld expects the section contents for these relocations
7630 to be zero. Arrange for that to occur when
7631 bfd_install_relocation is called. */
7632 fixP->fx_addnumber = (- bfd_section_vma (S_GET_SEGMENT (fixP->fx_addsy))
7633 - S_GET_VALUE (fixP->fx_addsy)
7634 - fieldval);
7635 else
7636 fixP->fx_addnumber = 0;
7637 #endif
7640 /* Generate a reloc for a fixup. */
7642 arelent **
7643 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
7645 static arelent *relocs[3];
7646 arelent *reloc;
7648 relocs[0] = reloc = XNEW (arelent);
7649 relocs[1] = NULL;
7651 reloc->sym_ptr_ptr = XNEW (asymbol *);
7652 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
7653 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
7654 /* BFD_RELOC_PPC64_TLS_PCREL generates R_PPC64_TLS with an odd r_offset. */
7655 if (fixp->fx_r_type == BFD_RELOC_PPC64_TLS_PCREL)
7656 reloc->address++;
7657 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
7658 if (reloc->howto == (reloc_howto_type *) NULL)
7660 as_bad_where (fixp->fx_file, fixp->fx_line,
7661 _("reloc %d not supported by object file format"),
7662 (int) fixp->fx_r_type);
7663 relocs[0] = NULL;
7665 reloc->addend = fixp->fx_addnumber;
7667 if (fixp->fx_subsy != NULL)
7669 relocs[1] = reloc = XNEW (arelent);
7670 relocs[2] = NULL;
7672 reloc->sym_ptr_ptr = XNEW (asymbol *);
7673 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
7674 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
7676 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_PPC_NEG);
7677 reloc->addend = fixp->fx_addnumber;
7679 if (reloc->howto == (reloc_howto_type *) NULL)
7681 as_bad_subtract (fixp);
7682 free (relocs[1]->sym_ptr_ptr);
7683 free (relocs[1]);
7684 free (relocs[0]->sym_ptr_ptr);
7685 free (relocs[0]);
7686 relocs[0] = NULL;
7691 return relocs;
7694 void
7695 ppc_cfi_frame_initial_instructions (void)
7697 cfi_add_CFA_def_cfa (1, 0);
7701 tc_ppc_regname_to_dw2regnum (char *regname)
7703 unsigned int regnum = -1;
7704 unsigned int i;
7705 const char *p;
7706 char *q;
7707 static struct { const char *name; int dw2regnum; } regnames[] =
7709 { "sp", 1 }, { "r.sp", 1 }, { "rtoc", 2 }, { "r.toc", 2 },
7710 { "mq", 64 }, { "lr", 65 }, { "ctr", 66 }, { "ap", 67 },
7711 { "cr", 70 }, { "xer", 76 }, { "vrsave", 109 }, { "vscr", 110 },
7712 { "spe_acc", 111 }, { "spefscr", 112 }
7715 for (i = 0; i < ARRAY_SIZE (regnames); ++i)
7716 if (strcmp (regnames[i].name, regname) == 0)
7717 return regnames[i].dw2regnum;
7719 if (regname[0] == 'r' || regname[0] == 'f' || regname[0] == 'v')
7721 p = regname + 1 + (regname[1] == '.');
7722 regnum = strtoul (p, &q, 10);
7723 if (p == q || *q || regnum >= 32)
7724 return -1;
7725 if (regname[0] == 'f')
7726 regnum += 32;
7727 else if (regname[0] == 'v')
7728 regnum += 77;
7730 else if (regname[0] == 'c' && regname[1] == 'r')
7732 p = regname + 2 + (regname[2] == '.');
7733 if (p[0] < '0' || p[0] > '7' || p[1])
7734 return -1;
7735 regnum = p[0] - '0' + 68;
7737 return regnum;