Automatic date update in version.in
[binutils-gdb.git] / gas / config / tc-ppc.c
blob8c00d01f6f289226b459ad1a48ecabc12efa291c
1 /* tc-ppc.c -- Assemble for the PowerPC or POWER (RS/6000)
2 Copyright (C) 1994-2024 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 char name[6];
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 /* Dense Math Registers. */
357 { "dm0", 0, PPC_OPERAND_DMR },
358 { "dm1", 1, PPC_OPERAND_DMR },
359 { "dm2", 2, PPC_OPERAND_DMR },
360 { "dm3", 3, PPC_OPERAND_DMR },
361 { "dm4", 4, PPC_OPERAND_DMR },
362 { "dm5", 5, PPC_OPERAND_DMR },
363 { "dm6", 6, PPC_OPERAND_DMR },
364 { "dm7", 7, PPC_OPERAND_DMR },
366 /* Floating point registers */
367 { "f.0", 0, PPC_OPERAND_FPR },
368 { "f.1", 1, PPC_OPERAND_FPR },
369 { "f.10", 10, PPC_OPERAND_FPR },
370 { "f.11", 11, PPC_OPERAND_FPR },
371 { "f.12", 12, PPC_OPERAND_FPR },
372 { "f.13", 13, PPC_OPERAND_FPR },
373 { "f.14", 14, PPC_OPERAND_FPR },
374 { "f.15", 15, PPC_OPERAND_FPR },
375 { "f.16", 16, PPC_OPERAND_FPR },
376 { "f.17", 17, PPC_OPERAND_FPR },
377 { "f.18", 18, PPC_OPERAND_FPR },
378 { "f.19", 19, PPC_OPERAND_FPR },
379 { "f.2", 2, PPC_OPERAND_FPR },
380 { "f.20", 20, PPC_OPERAND_FPR },
381 { "f.21", 21, PPC_OPERAND_FPR },
382 { "f.22", 22, PPC_OPERAND_FPR },
383 { "f.23", 23, PPC_OPERAND_FPR },
384 { "f.24", 24, PPC_OPERAND_FPR },
385 { "f.25", 25, PPC_OPERAND_FPR },
386 { "f.26", 26, PPC_OPERAND_FPR },
387 { "f.27", 27, PPC_OPERAND_FPR },
388 { "f.28", 28, PPC_OPERAND_FPR },
389 { "f.29", 29, PPC_OPERAND_FPR },
390 { "f.3", 3, PPC_OPERAND_FPR },
391 { "f.30", 30, PPC_OPERAND_FPR },
392 { "f.31", 31, PPC_OPERAND_FPR },
393 { "f.32", 32, PPC_OPERAND_VSR },
394 { "f.33", 33, PPC_OPERAND_VSR },
395 { "f.34", 34, PPC_OPERAND_VSR },
396 { "f.35", 35, PPC_OPERAND_VSR },
397 { "f.36", 36, PPC_OPERAND_VSR },
398 { "f.37", 37, PPC_OPERAND_VSR },
399 { "f.38", 38, PPC_OPERAND_VSR },
400 { "f.39", 39, PPC_OPERAND_VSR },
401 { "f.4", 4, PPC_OPERAND_FPR },
402 { "f.40", 40, PPC_OPERAND_VSR },
403 { "f.41", 41, PPC_OPERAND_VSR },
404 { "f.42", 42, PPC_OPERAND_VSR },
405 { "f.43", 43, PPC_OPERAND_VSR },
406 { "f.44", 44, PPC_OPERAND_VSR },
407 { "f.45", 45, PPC_OPERAND_VSR },
408 { "f.46", 46, PPC_OPERAND_VSR },
409 { "f.47", 47, PPC_OPERAND_VSR },
410 { "f.48", 48, PPC_OPERAND_VSR },
411 { "f.49", 49, PPC_OPERAND_VSR },
412 { "f.5", 5, PPC_OPERAND_FPR },
413 { "f.50", 50, PPC_OPERAND_VSR },
414 { "f.51", 51, PPC_OPERAND_VSR },
415 { "f.52", 52, PPC_OPERAND_VSR },
416 { "f.53", 53, PPC_OPERAND_VSR },
417 { "f.54", 54, PPC_OPERAND_VSR },
418 { "f.55", 55, PPC_OPERAND_VSR },
419 { "f.56", 56, PPC_OPERAND_VSR },
420 { "f.57", 57, PPC_OPERAND_VSR },
421 { "f.58", 58, PPC_OPERAND_VSR },
422 { "f.59", 59, PPC_OPERAND_VSR },
423 { "f.6", 6, PPC_OPERAND_FPR },
424 { "f.60", 60, PPC_OPERAND_VSR },
425 { "f.61", 61, PPC_OPERAND_VSR },
426 { "f.62", 62, PPC_OPERAND_VSR },
427 { "f.63", 63, PPC_OPERAND_VSR },
428 { "f.7", 7, PPC_OPERAND_FPR },
429 { "f.8", 8, PPC_OPERAND_FPR },
430 { "f.9", 9, PPC_OPERAND_FPR },
432 { "f0", 0, PPC_OPERAND_FPR },
433 { "f1", 1, PPC_OPERAND_FPR },
434 { "f10", 10, PPC_OPERAND_FPR },
435 { "f11", 11, PPC_OPERAND_FPR },
436 { "f12", 12, PPC_OPERAND_FPR },
437 { "f13", 13, PPC_OPERAND_FPR },
438 { "f14", 14, PPC_OPERAND_FPR },
439 { "f15", 15, PPC_OPERAND_FPR },
440 { "f16", 16, PPC_OPERAND_FPR },
441 { "f17", 17, PPC_OPERAND_FPR },
442 { "f18", 18, PPC_OPERAND_FPR },
443 { "f19", 19, PPC_OPERAND_FPR },
444 { "f2", 2, PPC_OPERAND_FPR },
445 { "f20", 20, PPC_OPERAND_FPR },
446 { "f21", 21, PPC_OPERAND_FPR },
447 { "f22", 22, PPC_OPERAND_FPR },
448 { "f23", 23, PPC_OPERAND_FPR },
449 { "f24", 24, PPC_OPERAND_FPR },
450 { "f25", 25, PPC_OPERAND_FPR },
451 { "f26", 26, PPC_OPERAND_FPR },
452 { "f27", 27, PPC_OPERAND_FPR },
453 { "f28", 28, PPC_OPERAND_FPR },
454 { "f29", 29, PPC_OPERAND_FPR },
455 { "f3", 3, PPC_OPERAND_FPR },
456 { "f30", 30, PPC_OPERAND_FPR },
457 { "f31", 31, PPC_OPERAND_FPR },
458 { "f32", 32, PPC_OPERAND_VSR },
459 { "f33", 33, PPC_OPERAND_VSR },
460 { "f34", 34, PPC_OPERAND_VSR },
461 { "f35", 35, PPC_OPERAND_VSR },
462 { "f36", 36, PPC_OPERAND_VSR },
463 { "f37", 37, PPC_OPERAND_VSR },
464 { "f38", 38, PPC_OPERAND_VSR },
465 { "f39", 39, PPC_OPERAND_VSR },
466 { "f4", 4, PPC_OPERAND_FPR },
467 { "f40", 40, PPC_OPERAND_VSR },
468 { "f41", 41, PPC_OPERAND_VSR },
469 { "f42", 42, PPC_OPERAND_VSR },
470 { "f43", 43, PPC_OPERAND_VSR },
471 { "f44", 44, PPC_OPERAND_VSR },
472 { "f45", 45, PPC_OPERAND_VSR },
473 { "f46", 46, PPC_OPERAND_VSR },
474 { "f47", 47, PPC_OPERAND_VSR },
475 { "f48", 48, PPC_OPERAND_VSR },
476 { "f49", 49, PPC_OPERAND_VSR },
477 { "f5", 5, PPC_OPERAND_FPR },
478 { "f50", 50, PPC_OPERAND_VSR },
479 { "f51", 51, PPC_OPERAND_VSR },
480 { "f52", 52, PPC_OPERAND_VSR },
481 { "f53", 53, PPC_OPERAND_VSR },
482 { "f54", 54, PPC_OPERAND_VSR },
483 { "f55", 55, PPC_OPERAND_VSR },
484 { "f56", 56, PPC_OPERAND_VSR },
485 { "f57", 57, PPC_OPERAND_VSR },
486 { "f58", 58, PPC_OPERAND_VSR },
487 { "f59", 59, PPC_OPERAND_VSR },
488 { "f6", 6, PPC_OPERAND_FPR },
489 { "f60", 60, PPC_OPERAND_VSR },
490 { "f61", 61, PPC_OPERAND_VSR },
491 { "f62", 62, PPC_OPERAND_VSR },
492 { "f63", 63, PPC_OPERAND_VSR },
493 { "f7", 7, PPC_OPERAND_FPR },
494 { "f8", 8, PPC_OPERAND_FPR },
495 { "f9", 9, PPC_OPERAND_FPR },
497 /* Quantization registers used with pair single instructions. */
498 { "gqr.0", 0, PPC_OPERAND_GQR },
499 { "gqr.1", 1, PPC_OPERAND_GQR },
500 { "gqr.2", 2, PPC_OPERAND_GQR },
501 { "gqr.3", 3, PPC_OPERAND_GQR },
502 { "gqr.4", 4, PPC_OPERAND_GQR },
503 { "gqr.5", 5, PPC_OPERAND_GQR },
504 { "gqr.6", 6, PPC_OPERAND_GQR },
505 { "gqr.7", 7, PPC_OPERAND_GQR },
506 { "gqr0", 0, PPC_OPERAND_GQR },
507 { "gqr1", 1, PPC_OPERAND_GQR },
508 { "gqr2", 2, PPC_OPERAND_GQR },
509 { "gqr3", 3, PPC_OPERAND_GQR },
510 { "gqr4", 4, PPC_OPERAND_GQR },
511 { "gqr5", 5, PPC_OPERAND_GQR },
512 { "gqr6", 6, PPC_OPERAND_GQR },
513 { "gqr7", 7, PPC_OPERAND_GQR },
515 { "lr", 8, PPC_OPERAND_SPR },
517 /* General Purpose Registers */
518 { "r.0", 0, PPC_OPERAND_GPR },
519 { "r.1", 1, PPC_OPERAND_GPR },
520 { "r.10", 10, PPC_OPERAND_GPR },
521 { "r.11", 11, PPC_OPERAND_GPR },
522 { "r.12", 12, PPC_OPERAND_GPR },
523 { "r.13", 13, PPC_OPERAND_GPR },
524 { "r.14", 14, PPC_OPERAND_GPR },
525 { "r.15", 15, PPC_OPERAND_GPR },
526 { "r.16", 16, PPC_OPERAND_GPR },
527 { "r.17", 17, PPC_OPERAND_GPR },
528 { "r.18", 18, PPC_OPERAND_GPR },
529 { "r.19", 19, PPC_OPERAND_GPR },
530 { "r.2", 2, PPC_OPERAND_GPR },
531 { "r.20", 20, PPC_OPERAND_GPR },
532 { "r.21", 21, PPC_OPERAND_GPR },
533 { "r.22", 22, PPC_OPERAND_GPR },
534 { "r.23", 23, PPC_OPERAND_GPR },
535 { "r.24", 24, PPC_OPERAND_GPR },
536 { "r.25", 25, PPC_OPERAND_GPR },
537 { "r.26", 26, PPC_OPERAND_GPR },
538 { "r.27", 27, PPC_OPERAND_GPR },
539 { "r.28", 28, PPC_OPERAND_GPR },
540 { "r.29", 29, PPC_OPERAND_GPR },
541 { "r.3", 3, PPC_OPERAND_GPR },
542 { "r.30", 30, PPC_OPERAND_GPR },
543 { "r.31", 31, PPC_OPERAND_GPR },
544 { "r.4", 4, PPC_OPERAND_GPR },
545 { "r.5", 5, PPC_OPERAND_GPR },
546 { "r.6", 6, PPC_OPERAND_GPR },
547 { "r.7", 7, PPC_OPERAND_GPR },
548 { "r.8", 8, PPC_OPERAND_GPR },
549 { "r.9", 9, PPC_OPERAND_GPR },
551 { "r.sp", 1, PPC_OPERAND_GPR },
553 { "r.toc", 2, PPC_OPERAND_GPR },
555 { "r0", 0, PPC_OPERAND_GPR },
556 { "r1", 1, PPC_OPERAND_GPR },
557 { "r10", 10, PPC_OPERAND_GPR },
558 { "r11", 11, PPC_OPERAND_GPR },
559 { "r12", 12, PPC_OPERAND_GPR },
560 { "r13", 13, PPC_OPERAND_GPR },
561 { "r14", 14, PPC_OPERAND_GPR },
562 { "r15", 15, PPC_OPERAND_GPR },
563 { "r16", 16, PPC_OPERAND_GPR },
564 { "r17", 17, PPC_OPERAND_GPR },
565 { "r18", 18, PPC_OPERAND_GPR },
566 { "r19", 19, PPC_OPERAND_GPR },
567 { "r2", 2, PPC_OPERAND_GPR },
568 { "r20", 20, PPC_OPERAND_GPR },
569 { "r21", 21, PPC_OPERAND_GPR },
570 { "r22", 22, PPC_OPERAND_GPR },
571 { "r23", 23, PPC_OPERAND_GPR },
572 { "r24", 24, PPC_OPERAND_GPR },
573 { "r25", 25, PPC_OPERAND_GPR },
574 { "r26", 26, PPC_OPERAND_GPR },
575 { "r27", 27, PPC_OPERAND_GPR },
576 { "r28", 28, PPC_OPERAND_GPR },
577 { "r29", 29, PPC_OPERAND_GPR },
578 { "r3", 3, PPC_OPERAND_GPR },
579 { "r30", 30, PPC_OPERAND_GPR },
580 { "r31", 31, PPC_OPERAND_GPR },
581 { "r4", 4, PPC_OPERAND_GPR },
582 { "r5", 5, PPC_OPERAND_GPR },
583 { "r6", 6, PPC_OPERAND_GPR },
584 { "r7", 7, PPC_OPERAND_GPR },
585 { "r8", 8, PPC_OPERAND_GPR },
586 { "r9", 9, PPC_OPERAND_GPR },
588 { "rtoc", 2, PPC_OPERAND_GPR },
590 { "sdr1", 25, PPC_OPERAND_SPR },
592 { "sp", 1, PPC_OPERAND_GPR },
594 { "srr0", 26, PPC_OPERAND_SPR },
595 { "srr1", 27, PPC_OPERAND_SPR },
597 /* Vector (Altivec/VMX) registers */
598 { "v.0", 0, PPC_OPERAND_VR },
599 { "v.1", 1, PPC_OPERAND_VR },
600 { "v.10", 10, PPC_OPERAND_VR },
601 { "v.11", 11, PPC_OPERAND_VR },
602 { "v.12", 12, PPC_OPERAND_VR },
603 { "v.13", 13, PPC_OPERAND_VR },
604 { "v.14", 14, PPC_OPERAND_VR },
605 { "v.15", 15, PPC_OPERAND_VR },
606 { "v.16", 16, PPC_OPERAND_VR },
607 { "v.17", 17, PPC_OPERAND_VR },
608 { "v.18", 18, PPC_OPERAND_VR },
609 { "v.19", 19, PPC_OPERAND_VR },
610 { "v.2", 2, PPC_OPERAND_VR },
611 { "v.20", 20, PPC_OPERAND_VR },
612 { "v.21", 21, PPC_OPERAND_VR },
613 { "v.22", 22, PPC_OPERAND_VR },
614 { "v.23", 23, PPC_OPERAND_VR },
615 { "v.24", 24, PPC_OPERAND_VR },
616 { "v.25", 25, PPC_OPERAND_VR },
617 { "v.26", 26, PPC_OPERAND_VR },
618 { "v.27", 27, PPC_OPERAND_VR },
619 { "v.28", 28, PPC_OPERAND_VR },
620 { "v.29", 29, PPC_OPERAND_VR },
621 { "v.3", 3, PPC_OPERAND_VR },
622 { "v.30", 30, PPC_OPERAND_VR },
623 { "v.31", 31, PPC_OPERAND_VR },
624 { "v.4", 4, PPC_OPERAND_VR },
625 { "v.5", 5, PPC_OPERAND_VR },
626 { "v.6", 6, PPC_OPERAND_VR },
627 { "v.7", 7, PPC_OPERAND_VR },
628 { "v.8", 8, PPC_OPERAND_VR },
629 { "v.9", 9, PPC_OPERAND_VR },
631 { "v0", 0, PPC_OPERAND_VR },
632 { "v1", 1, PPC_OPERAND_VR },
633 { "v10", 10, PPC_OPERAND_VR },
634 { "v11", 11, PPC_OPERAND_VR },
635 { "v12", 12, PPC_OPERAND_VR },
636 { "v13", 13, PPC_OPERAND_VR },
637 { "v14", 14, PPC_OPERAND_VR },
638 { "v15", 15, PPC_OPERAND_VR },
639 { "v16", 16, PPC_OPERAND_VR },
640 { "v17", 17, PPC_OPERAND_VR },
641 { "v18", 18, PPC_OPERAND_VR },
642 { "v19", 19, PPC_OPERAND_VR },
643 { "v2", 2, PPC_OPERAND_VR },
644 { "v20", 20, PPC_OPERAND_VR },
645 { "v21", 21, PPC_OPERAND_VR },
646 { "v22", 22, PPC_OPERAND_VR },
647 { "v23", 23, PPC_OPERAND_VR },
648 { "v24", 24, PPC_OPERAND_VR },
649 { "v25", 25, PPC_OPERAND_VR },
650 { "v26", 26, PPC_OPERAND_VR },
651 { "v27", 27, PPC_OPERAND_VR },
652 { "v28", 28, PPC_OPERAND_VR },
653 { "v29", 29, PPC_OPERAND_VR },
654 { "v3", 3, PPC_OPERAND_VR },
655 { "v30", 30, PPC_OPERAND_VR },
656 { "v31", 31, PPC_OPERAND_VR },
657 { "v4", 4, PPC_OPERAND_VR },
658 { "v5", 5, PPC_OPERAND_VR },
659 { "v6", 6, PPC_OPERAND_VR },
660 { "v7", 7, PPC_OPERAND_VR },
661 { "v8", 8, PPC_OPERAND_VR },
662 { "v9", 9, PPC_OPERAND_VR },
664 /* Vector Scalar (VSX) registers (ISA 2.06). */
665 { "vs.0", 0, PPC_OPERAND_VSR },
666 { "vs.1", 1, PPC_OPERAND_VSR },
667 { "vs.10", 10, PPC_OPERAND_VSR },
668 { "vs.11", 11, PPC_OPERAND_VSR },
669 { "vs.12", 12, PPC_OPERAND_VSR },
670 { "vs.13", 13, PPC_OPERAND_VSR },
671 { "vs.14", 14, PPC_OPERAND_VSR },
672 { "vs.15", 15, PPC_OPERAND_VSR },
673 { "vs.16", 16, PPC_OPERAND_VSR },
674 { "vs.17", 17, PPC_OPERAND_VSR },
675 { "vs.18", 18, PPC_OPERAND_VSR },
676 { "vs.19", 19, PPC_OPERAND_VSR },
677 { "vs.2", 2, PPC_OPERAND_VSR },
678 { "vs.20", 20, PPC_OPERAND_VSR },
679 { "vs.21", 21, PPC_OPERAND_VSR },
680 { "vs.22", 22, PPC_OPERAND_VSR },
681 { "vs.23", 23, PPC_OPERAND_VSR },
682 { "vs.24", 24, PPC_OPERAND_VSR },
683 { "vs.25", 25, PPC_OPERAND_VSR },
684 { "vs.26", 26, PPC_OPERAND_VSR },
685 { "vs.27", 27, PPC_OPERAND_VSR },
686 { "vs.28", 28, PPC_OPERAND_VSR },
687 { "vs.29", 29, PPC_OPERAND_VSR },
688 { "vs.3", 3, PPC_OPERAND_VSR },
689 { "vs.30", 30, PPC_OPERAND_VSR },
690 { "vs.31", 31, PPC_OPERAND_VSR },
691 { "vs.32", 32, PPC_OPERAND_VSR },
692 { "vs.33", 33, PPC_OPERAND_VSR },
693 { "vs.34", 34, PPC_OPERAND_VSR },
694 { "vs.35", 35, PPC_OPERAND_VSR },
695 { "vs.36", 36, PPC_OPERAND_VSR },
696 { "vs.37", 37, PPC_OPERAND_VSR },
697 { "vs.38", 38, PPC_OPERAND_VSR },
698 { "vs.39", 39, PPC_OPERAND_VSR },
699 { "vs.4", 4, PPC_OPERAND_VSR },
700 { "vs.40", 40, PPC_OPERAND_VSR },
701 { "vs.41", 41, PPC_OPERAND_VSR },
702 { "vs.42", 42, PPC_OPERAND_VSR },
703 { "vs.43", 43, PPC_OPERAND_VSR },
704 { "vs.44", 44, PPC_OPERAND_VSR },
705 { "vs.45", 45, PPC_OPERAND_VSR },
706 { "vs.46", 46, PPC_OPERAND_VSR },
707 { "vs.47", 47, PPC_OPERAND_VSR },
708 { "vs.48", 48, PPC_OPERAND_VSR },
709 { "vs.49", 49, PPC_OPERAND_VSR },
710 { "vs.5", 5, PPC_OPERAND_VSR },
711 { "vs.50", 50, PPC_OPERAND_VSR },
712 { "vs.51", 51, PPC_OPERAND_VSR },
713 { "vs.52", 52, PPC_OPERAND_VSR },
714 { "vs.53", 53, PPC_OPERAND_VSR },
715 { "vs.54", 54, PPC_OPERAND_VSR },
716 { "vs.55", 55, PPC_OPERAND_VSR },
717 { "vs.56", 56, PPC_OPERAND_VSR },
718 { "vs.57", 57, PPC_OPERAND_VSR },
719 { "vs.58", 58, PPC_OPERAND_VSR },
720 { "vs.59", 59, PPC_OPERAND_VSR },
721 { "vs.6", 6, PPC_OPERAND_VSR },
722 { "vs.60", 60, PPC_OPERAND_VSR },
723 { "vs.61", 61, PPC_OPERAND_VSR },
724 { "vs.62", 62, PPC_OPERAND_VSR },
725 { "vs.63", 63, PPC_OPERAND_VSR },
726 { "vs.7", 7, PPC_OPERAND_VSR },
727 { "vs.8", 8, PPC_OPERAND_VSR },
728 { "vs.9", 9, PPC_OPERAND_VSR },
730 { "vs0", 0, PPC_OPERAND_VSR },
731 { "vs1", 1, PPC_OPERAND_VSR },
732 { "vs10", 10, PPC_OPERAND_VSR },
733 { "vs11", 11, PPC_OPERAND_VSR },
734 { "vs12", 12, PPC_OPERAND_VSR },
735 { "vs13", 13, PPC_OPERAND_VSR },
736 { "vs14", 14, PPC_OPERAND_VSR },
737 { "vs15", 15, PPC_OPERAND_VSR },
738 { "vs16", 16, PPC_OPERAND_VSR },
739 { "vs17", 17, PPC_OPERAND_VSR },
740 { "vs18", 18, PPC_OPERAND_VSR },
741 { "vs19", 19, PPC_OPERAND_VSR },
742 { "vs2", 2, PPC_OPERAND_VSR },
743 { "vs20", 20, PPC_OPERAND_VSR },
744 { "vs21", 21, PPC_OPERAND_VSR },
745 { "vs22", 22, PPC_OPERAND_VSR },
746 { "vs23", 23, PPC_OPERAND_VSR },
747 { "vs24", 24, PPC_OPERAND_VSR },
748 { "vs25", 25, PPC_OPERAND_VSR },
749 { "vs26", 26, PPC_OPERAND_VSR },
750 { "vs27", 27, PPC_OPERAND_VSR },
751 { "vs28", 28, PPC_OPERAND_VSR },
752 { "vs29", 29, PPC_OPERAND_VSR },
753 { "vs3", 3, PPC_OPERAND_VSR },
754 { "vs30", 30, PPC_OPERAND_VSR },
755 { "vs31", 31, PPC_OPERAND_VSR },
756 { "vs32", 32, PPC_OPERAND_VSR },
757 { "vs33", 33, PPC_OPERAND_VSR },
758 { "vs34", 34, PPC_OPERAND_VSR },
759 { "vs35", 35, PPC_OPERAND_VSR },
760 { "vs36", 36, PPC_OPERAND_VSR },
761 { "vs37", 37, PPC_OPERAND_VSR },
762 { "vs38", 38, PPC_OPERAND_VSR },
763 { "vs39", 39, PPC_OPERAND_VSR },
764 { "vs4", 4, PPC_OPERAND_VSR },
765 { "vs40", 40, PPC_OPERAND_VSR },
766 { "vs41", 41, PPC_OPERAND_VSR },
767 { "vs42", 42, PPC_OPERAND_VSR },
768 { "vs43", 43, PPC_OPERAND_VSR },
769 { "vs44", 44, PPC_OPERAND_VSR },
770 { "vs45", 45, PPC_OPERAND_VSR },
771 { "vs46", 46, PPC_OPERAND_VSR },
772 { "vs47", 47, PPC_OPERAND_VSR },
773 { "vs48", 48, PPC_OPERAND_VSR },
774 { "vs49", 49, PPC_OPERAND_VSR },
775 { "vs5", 5, PPC_OPERAND_VSR },
776 { "vs50", 50, PPC_OPERAND_VSR },
777 { "vs51", 51, PPC_OPERAND_VSR },
778 { "vs52", 52, PPC_OPERAND_VSR },
779 { "vs53", 53, PPC_OPERAND_VSR },
780 { "vs54", 54, PPC_OPERAND_VSR },
781 { "vs55", 55, PPC_OPERAND_VSR },
782 { "vs56", 56, PPC_OPERAND_VSR },
783 { "vs57", 57, PPC_OPERAND_VSR },
784 { "vs58", 58, PPC_OPERAND_VSR },
785 { "vs59", 59, PPC_OPERAND_VSR },
786 { "vs6", 6, PPC_OPERAND_VSR },
787 { "vs60", 60, PPC_OPERAND_VSR },
788 { "vs61", 61, PPC_OPERAND_VSR },
789 { "vs62", 62, PPC_OPERAND_VSR },
790 { "vs63", 63, PPC_OPERAND_VSR },
791 { "vs7", 7, PPC_OPERAND_VSR },
792 { "vs8", 8, PPC_OPERAND_VSR },
793 { "vs9", 9, PPC_OPERAND_VSR },
795 { "xer", 1, PPC_OPERAND_SPR }
798 /* Given NAME, find the register number associated with that name, return
799 the integer value associated with the given name or -1 on failure. */
801 static const struct pd_reg *
802 reg_name_search (const struct pd_reg *regs, int regcount, const char *name)
804 int middle, low, high;
805 int cmp;
807 low = 0;
808 high = regcount - 1;
812 middle = (low + high) / 2;
813 cmp = strcasecmp (name, regs[middle].name);
814 if (cmp < 0)
815 high = middle - 1;
816 else if (cmp > 0)
817 low = middle + 1;
818 else
819 return &regs[middle];
821 while (low <= high);
823 return NULL;
826 /* Called for a non-symbol, non-number operand. Handles %reg. */
828 void
829 md_operand (expressionS *expressionP)
831 const struct pd_reg *reg;
832 char *name;
833 char *start;
834 char c;
836 if (input_line_pointer[0] != '%' || !ISALPHA (input_line_pointer[1]))
837 return;
839 start = input_line_pointer;
840 ++input_line_pointer;
842 c = get_symbol_name (&name);
843 reg = reg_name_search (pre_defined_registers,
844 ARRAY_SIZE (pre_defined_registers), name);
845 *input_line_pointer = c;
847 if (reg != NULL)
849 expressionP->X_op = O_register;
850 expressionP->X_add_number = reg->value;
851 expressionP->X_md = reg->flags;
853 else
854 input_line_pointer = start;
857 /* Whether to do the special parsing. */
858 static bool cr_operand;
860 /* Extra names to recognise in a condition code. This table is sorted. */
861 static const struct pd_reg cr_cond[] =
863 { "eq", 2, PPC_OPERAND_CR_BIT },
864 { "gt", 1, PPC_OPERAND_CR_BIT },
865 { "lt", 0, PPC_OPERAND_CR_BIT },
866 { "so", 3, PPC_OPERAND_CR_BIT },
867 { "un", 3, PPC_OPERAND_CR_BIT }
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, and recognises other registers when
873 -mregnames. */
875 void
876 ppc_parse_name (const char *name, expressionS *exp, enum expr_mode mode)
878 const struct pd_reg *reg = NULL;
880 if (cr_operand)
881 reg = reg_name_search (cr_cond, ARRAY_SIZE (cr_cond), name);
882 if (reg == NULL && (cr_operand || reg_names_p))
883 reg = reg_name_search (pre_defined_registers,
884 ARRAY_SIZE (pre_defined_registers), name);
885 if (reg != NULL)
887 exp->X_op = O_register;
888 exp->X_add_number = reg->value;
889 exp->X_md = reg->flags;
890 return;
893 /* The following replaces code in expr.c operand() after the
894 md_parse_name call. There is too much difference between targets
895 in the way X_md is used to move this code into expr.c. If you
896 do, you'll get failures on x86 due to uninitialised X_md fields,
897 failures on alpha and other targets due to creating register
898 symbols as O_constant rather than O_register, and failures on arc
899 and others due to expecting expr() to leave X_md alone. */
900 symbolS *sym = symbol_find_or_make (name);
902 /* If we have an absolute symbol or a reg, then we know its value
903 now. Copy the symbol value expression to propagate X_md. */
904 bool done = false;
905 if (mode != expr_defer
906 && !S_FORCE_RELOC (sym, 0))
908 segT segment = S_GET_SEGMENT (sym);
909 if (segment == absolute_section || segment == reg_section)
911 resolve_symbol_value (sym);
912 *exp = *symbol_get_value_expression (sym);
913 done = true;
916 if (!done)
918 exp->X_op = O_symbol;
919 exp->X_add_symbol = sym;
920 exp->X_add_number = 0;
924 /* Propagate X_md and check register expressions. This is to support
925 condition codes like 4*cr5+eq. */
928 ppc_optimize_expr (expressionS *left, operatorT op, expressionS *right)
930 /* Accept 4*cr<n> and cr<n>*4. */
931 if (op == O_multiply
932 && ((right->X_op == O_register
933 && right->X_md == PPC_OPERAND_CR_REG
934 && left->X_op == O_constant
935 && left->X_add_number == 4)
936 || (left->X_op == O_register
937 && left->X_md == PPC_OPERAND_CR_REG
938 && right->X_op == O_constant
939 && right->X_add_number == 4)))
941 left->X_op = O_register;
942 left->X_md = PPC_OPERAND_CR_REG | PPC_OPERAND_CR_BIT;
943 left->X_add_number *= right->X_add_number;
944 return 1;
947 /* Accept the above plus <cr bit>, and <cr bit> plus the above. */
948 if (op == O_add
949 && left->X_op == O_register
950 && right->X_op == O_register
951 && ((right->X_md == PPC_OPERAND_CR_BIT
952 && left->X_md == (PPC_OPERAND_CR_REG | PPC_OPERAND_CR_BIT))
953 || (right->X_md == (PPC_OPERAND_CR_REG | PPC_OPERAND_CR_BIT)
954 && left->X_md == PPC_OPERAND_CR_BIT)))
956 left->X_md = PPC_OPERAND_CR_BIT;
957 right->X_op = O_constant;
958 return 0;
961 /* Accept reg +/- constant. */
962 if (left && left->X_op == O_register
963 && !((op == O_add || op == O_subtract) && right->X_op == O_constant))
964 as_warn (_("invalid register expression"));
966 /* Accept constant + reg. */
967 if (right->X_op == O_register)
969 if (op == O_add && left->X_op == O_constant)
970 left->X_md = right->X_md;
971 else
972 as_warn (_("invalid register expression"));
975 return 0;
978 /* Local variables. */
980 /* Whether to target xcoff64/elf64. */
981 static unsigned int ppc_obj64 = BFD_DEFAULT_TARGET_SIZE == 64;
983 /* A separate obstack for use by ppc_hash, so that we can quickly
984 throw away hash table memory . */
985 struct obstack insn_obstack;
987 /* Opcode hash table. */
988 static htab_t ppc_hash;
990 #ifdef OBJ_ELF
991 /* What type of shared library support to use. */
992 static enum { SHLIB_NONE, SHLIB_PIC, SHLIB_MRELOCATABLE } shlib = SHLIB_NONE;
994 /* Flags to set in the elf header. */
995 static flagword ppc_flags = 0;
997 /* Whether this is Solaris or not. */
998 #ifdef TARGET_SOLARIS_COMMENT
999 #define SOLARIS_P true
1000 #else
1001 #define SOLARIS_P false
1002 #endif
1004 static bool msolaris = SOLARIS_P;
1005 #endif
1007 #ifdef OBJ_XCOFF
1009 /* The RS/6000 assembler uses the .csect pseudo-op to generate code
1010 using a bunch of different sections. These assembler sections,
1011 however, are all encompassed within the .text, .data or .bss sections
1012 of the final output file. We handle this by using different
1013 subsegments within these main segments.
1014 .tdata and .tbss sections only have one type of csects for now,
1015 but it's better to follow the same construction like the others. */
1017 struct ppc_xcoff_section ppc_xcoff_text_section;
1018 struct ppc_xcoff_section ppc_xcoff_data_section;
1019 struct ppc_xcoff_section ppc_xcoff_bss_section;
1020 struct ppc_xcoff_section ppc_xcoff_tdata_section;
1021 struct ppc_xcoff_section ppc_xcoff_tbss_section;
1023 /* Return true if the ppc_xcoff_section structure is already
1024 initialized. */
1025 static bool
1026 ppc_xcoff_section_is_initialized (struct ppc_xcoff_section *section)
1028 return section->segment != NULL;
1031 /* Initialize a ppc_xcoff_section.
1032 Dummy symbols are used to ensure the position of .text over .data
1033 and .tdata. Moreover, they allow all algorithms here to be sure that
1034 csects isn't NULL. These symbols won't be output. */
1035 static void
1036 ppc_init_xcoff_section (struct ppc_xcoff_section *s, segT seg)
1038 s->segment = seg;
1039 s->next_subsegment = 2;
1040 s->csects = symbol_make ("dummy\001");
1041 symbol_get_tc (s->csects)->within = s->csects;
1044 /* The current csect. */
1045 static symbolS *ppc_current_csect;
1047 /* The RS/6000 assembler uses a TOC which holds addresses of functions
1048 and variables. Symbols are put in the TOC with the .tc pseudo-op.
1049 A special relocation is used when accessing TOC entries. We handle
1050 the TOC as a subsegment within the .data segment. We set it up if
1051 we see a .toc pseudo-op, and save the csect symbol here. */
1052 static symbolS *ppc_toc_csect;
1054 /* The first frag in the TOC subsegment. */
1055 static fragS *ppc_toc_frag;
1057 /* The first frag in the first subsegment after the TOC in the .data
1058 segment. NULL if there are no subsegments after the TOC. */
1059 static fragS *ppc_after_toc_frag;
1061 /* The current static block. */
1062 static symbolS *ppc_current_block;
1064 /* The COFF debugging section; set by md_begin. This is not the
1065 .debug section, but is instead the secret BFD section which will
1066 cause BFD to set the section number of a symbol to N_DEBUG. */
1067 static asection *ppc_coff_debug_section;
1069 /* Structure to set the length field of the dwarf sections. */
1070 struct dw_subsection {
1071 /* Subsections are simply linked. */
1072 struct dw_subsection *link;
1074 /* The subsection number. */
1075 subsegT subseg;
1077 /* Expression to compute the length of the section. */
1078 expressionS end_exp;
1081 static struct dw_section {
1082 /* Corresponding section. */
1083 segT sect;
1085 /* Simply linked list of subsections with a label. */
1086 struct dw_subsection *list_subseg;
1088 /* The anonymous subsection. */
1089 struct dw_subsection *anon_subseg;
1090 } dw_sections[XCOFF_DWSECT_NBR_NAMES];
1091 #endif /* OBJ_XCOFF */
1093 #ifdef OBJ_ELF
1094 symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE" */
1095 unsigned long *ppc_apuinfo_list;
1096 unsigned int ppc_apuinfo_num;
1097 unsigned int ppc_apuinfo_num_alloc;
1098 #endif /* OBJ_ELF */
1100 #ifdef OBJ_ELF
1101 const char *const md_shortopts = "b:l:usm:K:VQ:";
1102 #else
1103 const char *const md_shortopts = "um:";
1104 #endif
1105 #define OPTION_NOPS (OPTION_MD_BASE + 0)
1106 const struct option md_longopts[] = {
1107 {"nops", required_argument, NULL, OPTION_NOPS},
1108 {"ppc476-workaround", no_argument, &warn_476, 1},
1109 {"no-ppc476-workaround", no_argument, &warn_476, 0},
1110 {NULL, no_argument, NULL, 0}
1112 const size_t md_longopts_size = sizeof (md_longopts);
1115 md_parse_option (int c, const char *arg)
1117 ppc_cpu_t new_cpu;
1119 switch (c)
1121 case 'u':
1122 /* -u means that any undefined symbols should be treated as
1123 external, which is the default for gas anyhow. */
1124 break;
1126 #ifdef OBJ_ELF
1127 case 'l':
1128 /* Solaris as takes -le (presumably for little endian). For completeness
1129 sake, recognize -be also. */
1130 if (strcmp (arg, "e") == 0)
1132 target_big_endian = 0;
1133 set_target_endian = 1;
1134 if (ppc_cpu & PPC_OPCODE_VLE)
1135 as_bad (_("the use of -mvle requires big endian."));
1137 else
1138 return 0;
1140 break;
1142 case 'b':
1143 if (strcmp (arg, "e") == 0)
1145 target_big_endian = 1;
1146 set_target_endian = 1;
1148 else
1149 return 0;
1151 break;
1153 case 'K':
1154 /* Recognize -K PIC. */
1155 if (strcmp (arg, "PIC") == 0 || strcmp (arg, "pic") == 0)
1157 shlib = SHLIB_PIC;
1158 ppc_flags |= EF_PPC_RELOCATABLE_LIB;
1160 else
1161 return 0;
1163 break;
1164 #endif
1166 /* a64 and a32 determine whether to use XCOFF64 or XCOFF32. */
1167 case 'a':
1168 if (strcmp (arg, "64") == 0)
1170 #ifdef BFD64
1171 ppc_obj64 = 1;
1172 if (ppc_cpu & PPC_OPCODE_VLE)
1173 as_bad (_("the use of -mvle requires -a32."));
1174 #else
1175 as_fatal (_("%s unsupported"), "-a64");
1176 #endif
1178 else if (strcmp (arg, "32") == 0)
1179 ppc_obj64 = 0;
1180 else
1181 return 0;
1182 break;
1184 case 'm':
1185 new_cpu = ppc_parse_cpu (ppc_cpu, &sticky, arg);
1186 /* "raw" is only valid for the disassembler. */
1187 if (new_cpu != 0 && (new_cpu & PPC_OPCODE_RAW) == 0)
1189 ppc_cpu = new_cpu;
1190 if (strcmp (arg, "vle") == 0)
1192 if (set_target_endian && target_big_endian == 0)
1193 as_bad (_("the use of -mvle requires big endian."));
1194 if (ppc_obj64)
1195 as_bad (_("the use of -mvle requires -a32."));
1199 else if (strcmp (arg, "no-vle") == 0)
1201 sticky &= ~PPC_OPCODE_VLE;
1203 new_cpu = ppc_parse_cpu (ppc_cpu, &sticky, "booke");
1204 new_cpu &= ~PPC_OPCODE_VLE;
1206 ppc_cpu = new_cpu;
1209 else if (strcmp (arg, "regnames") == 0)
1210 reg_names_p = true;
1212 else if (strcmp (arg, "no-regnames") == 0)
1213 reg_names_p = false;
1215 #ifdef OBJ_ELF
1216 /* -mrelocatable/-mrelocatable-lib -- warn about initializations
1217 that require relocation. */
1218 else if (strcmp (arg, "relocatable") == 0)
1220 shlib = SHLIB_MRELOCATABLE;
1221 ppc_flags |= EF_PPC_RELOCATABLE;
1224 else if (strcmp (arg, "relocatable-lib") == 0)
1226 shlib = SHLIB_MRELOCATABLE;
1227 ppc_flags |= EF_PPC_RELOCATABLE_LIB;
1230 /* -memb, set embedded bit. */
1231 else if (strcmp (arg, "emb") == 0)
1232 ppc_flags |= EF_PPC_EMB;
1234 /* -mlittle/-mbig set the endianness. */
1235 else if (strcmp (arg, "little") == 0
1236 || strcmp (arg, "little-endian") == 0)
1238 target_big_endian = 0;
1239 set_target_endian = 1;
1240 if (ppc_cpu & PPC_OPCODE_VLE)
1241 as_bad (_("the use of -mvle requires big endian."));
1244 else if (strcmp (arg, "big") == 0 || strcmp (arg, "big-endian") == 0)
1246 target_big_endian = 1;
1247 set_target_endian = 1;
1250 else if (strcmp (arg, "solaris") == 0)
1252 msolaris = true;
1253 ppc_comment_chars = ppc_solaris_comment_chars;
1256 else if (strcmp (arg, "no-solaris") == 0)
1258 msolaris = false;
1259 ppc_comment_chars = ppc_eabi_comment_chars;
1261 else if (strcmp (arg, "spe2") == 0)
1263 ppc_cpu |= PPC_OPCODE_SPE2;
1265 #endif
1266 else
1268 as_bad (_("invalid switch -m%s"), arg);
1269 return 0;
1271 break;
1273 #ifdef OBJ_ELF
1274 /* -V: SVR4 argument to print version ID. */
1275 case 'V':
1276 print_version_id ();
1277 break;
1279 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
1280 should be emitted or not. FIXME: Not implemented. */
1281 case 'Q':
1282 break;
1284 /* Solaris takes -s to specify that .stabs go in a .stabs section,
1285 rather than .stabs.excl, which is ignored by the linker.
1286 FIXME: Not implemented. */
1287 case 's':
1288 if (arg)
1289 return 0;
1291 break;
1292 #endif
1294 case OPTION_NOPS:
1296 char *end;
1297 nop_limit = strtoul (optarg, &end, 0);
1298 if (*end)
1299 as_bad (_("--nops needs a numeric argument"));
1301 break;
1303 case 0:
1304 break;
1306 default:
1307 return 0;
1310 return 1;
1313 static int
1314 is_ppc64_target (const bfd_target *targ, void *data ATTRIBUTE_UNUSED)
1316 switch (targ->flavour)
1318 #ifdef OBJ_ELF
1319 case bfd_target_elf_flavour:
1320 return startswith (targ->name, "elf64-powerpc");
1321 #endif
1322 #ifdef OBJ_XCOFF
1323 case bfd_target_xcoff_flavour:
1324 return (strcmp (targ->name, "aixcoff64-rs6000") == 0
1325 || strcmp (targ->name, "aix5coff64-rs6000") == 0);
1326 #endif
1327 default:
1328 return 0;
1332 void
1333 md_show_usage (FILE *stream)
1335 fprintf (stream, _("\
1336 PowerPC options:\n"));
1337 fprintf (stream, _("\
1338 -a32 generate ELF32/XCOFF32\n"));
1339 if (bfd_iterate_over_targets (is_ppc64_target, NULL))
1340 fprintf (stream, _("\
1341 -a64 generate ELF64/XCOFF64\n"));
1342 fprintf (stream, _("\
1343 -u ignored\n"));
1344 fprintf (stream, _("\
1345 -mpwrx, -mpwr2 generate code for POWER/2 (RIOS2)\n"));
1346 fprintf (stream, _("\
1347 -mpwr generate code for POWER (RIOS1)\n"));
1348 fprintf (stream, _("\
1349 -m601 generate code for PowerPC 601\n"));
1350 fprintf (stream, _("\
1351 -mppc, -mppc32, -m603, -m604\n\
1352 generate code for PowerPC 603/604\n"));
1353 fprintf (stream, _("\
1354 -m403 generate code for PowerPC 403\n"));
1355 fprintf (stream, _("\
1356 -m405 generate code for PowerPC 405\n"));
1357 fprintf (stream, _("\
1358 -m440 generate code for PowerPC 440\n"));
1359 fprintf (stream, _("\
1360 -m464 generate code for PowerPC 464\n"));
1361 fprintf (stream, _("\
1362 -m476 generate code for PowerPC 476\n"));
1363 fprintf (stream, _("\
1364 -m7400, -m7410, -m7450, -m7455\n\
1365 generate code for PowerPC 7400/7410/7450/7455\n"));
1366 fprintf (stream, _("\
1367 -m750cl, -mgekko, -mbroadway\n\
1368 generate code for PowerPC 750cl/Gekko/Broadway\n"));
1369 fprintf (stream, _("\
1370 -m821, -m850, -m860 generate code for PowerPC 821/850/860\n"));
1371 fprintf (stream, _("\
1372 -mppc64, -m620 generate code for PowerPC 620/625/630\n"));
1373 fprintf (stream, _("\
1374 -mppc64bridge generate code for PowerPC 64, including bridge insns\n"));
1375 fprintf (stream, _("\
1376 -mbooke generate code for 32-bit PowerPC BookE\n"));
1377 fprintf (stream, _("\
1378 -ma2 generate code for A2 architecture\n"));
1379 fprintf (stream, _("\
1380 -mpower4, -mpwr4 generate code for Power4 architecture\n"));
1381 fprintf (stream, _("\
1382 -mpower5, -mpwr5, -mpwr5x\n\
1383 generate code for Power5 architecture\n"));
1384 fprintf (stream, _("\
1385 -mpower6, -mpwr6 generate code for Power6 architecture\n"));
1386 fprintf (stream, _("\
1387 -mpower7, -mpwr7 generate code for Power7 architecture\n"));
1388 fprintf (stream, _("\
1389 -mpower8, -mpwr8 generate code for Power8 architecture\n"));
1390 fprintf (stream, _("\
1391 -mpower9, -mpwr9 generate code for Power9 architecture\n"));
1392 fprintf (stream, _("\
1393 -mpower10, -mpwr10 generate code for Power10 architecture\n"));
1394 fprintf (stream, _("\
1395 -mpower11, -mpwr11 generate code for Power11 architecture\n"));
1396 fprintf (stream, _("\
1397 -mlibresoc generate code for Libre-SOC architecture\n"));
1398 fprintf (stream, _("\
1399 -mfuture generate code for 'future' architecture\n"));
1400 fprintf (stream, _("\
1401 -mcell generate code for Cell Broadband Engine architecture\n"));
1402 fprintf (stream, _("\
1403 -mcom generate code for Power/PowerPC common instructions\n"));
1404 fprintf (stream, _("\
1405 -many generate code for any architecture (PWR/PWRX/PPC)\n"));
1406 fprintf (stream, _("\
1407 -maltivec generate code for AltiVec\n"));
1408 fprintf (stream, _("\
1409 -mvsx generate code for Vector-Scalar (VSX) instructions\n"));
1410 fprintf (stream, _("\
1411 -me300 generate code for PowerPC e300 family\n"));
1412 fprintf (stream, _("\
1413 -me500, -me500x2 generate code for Motorola e500 core complex\n"));
1414 fprintf (stream, _("\
1415 -me500mc, generate code for Freescale e500mc core complex\n"));
1416 fprintf (stream, _("\
1417 -me500mc64, generate code for Freescale e500mc64 core complex\n"));
1418 fprintf (stream, _("\
1419 -me5500, generate code for Freescale e5500 core complex\n"));
1420 fprintf (stream, _("\
1421 -me6500, generate code for Freescale e6500 core complex\n"));
1422 fprintf (stream, _("\
1423 -mspe generate code for Motorola SPE instructions\n"));
1424 fprintf (stream, _("\
1425 -mspe2 generate code for Freescale SPE2 instructions\n"));
1426 fprintf (stream, _("\
1427 -mvle generate code for Freescale VLE instructions\n"));
1428 fprintf (stream, _("\
1429 -mtitan generate code for AppliedMicro Titan core complex\n"));
1430 fprintf (stream, _("\
1431 -mregnames Allow symbolic names for registers\n"));
1432 fprintf (stream, _("\
1433 -mno-regnames Do not allow symbolic names for registers\n"));
1434 #ifdef OBJ_ELF
1435 fprintf (stream, _("\
1436 -mrelocatable support for GCC's -mrelocatble option\n"));
1437 fprintf (stream, _("\
1438 -mrelocatable-lib support for GCC's -mrelocatble-lib option\n"));
1439 fprintf (stream, _("\
1440 -memb set PPC_EMB bit in ELF flags\n"));
1441 fprintf (stream, _("\
1442 -mlittle, -mlittle-endian, -le\n\
1443 generate code for a little endian machine\n"));
1444 fprintf (stream, _("\
1445 -mbig, -mbig-endian, -be\n\
1446 generate code for a big endian machine\n"));
1447 fprintf (stream, _("\
1448 -msolaris generate code for Solaris\n"));
1449 fprintf (stream, _("\
1450 -mno-solaris do not generate code for Solaris\n"));
1451 fprintf (stream, _("\
1452 -K PIC set EF_PPC_RELOCATABLE_LIB in ELF flags\n"));
1453 fprintf (stream, _("\
1454 -V print assembler version number\n"));
1455 fprintf (stream, _("\
1456 -Qy, -Qn ignored\n"));
1457 #endif
1458 fprintf (stream, _("\
1459 -nops=count when aligning, more than COUNT nops uses a branch\n"));
1460 fprintf (stream, _("\
1461 -ppc476-workaround warn if emitting data to code sections\n"));
1464 /* Set ppc_cpu if it is not already set. */
1466 static void
1467 ppc_set_cpu (void)
1469 const char *default_os = TARGET_OS;
1470 const char *default_cpu = TARGET_CPU;
1472 if ((ppc_cpu & ~(ppc_cpu_t) PPC_OPCODE_ANY) == 0)
1474 if (ppc_obj64)
1475 if (target_big_endian)
1476 ppc_cpu |= PPC_OPCODE_PPC | PPC_OPCODE_64;
1477 else
1478 /* The minimum supported cpu for 64-bit little-endian is power8. */
1479 ppc_cpu |= ppc_parse_cpu (ppc_cpu, &sticky, "power8");
1480 else if (startswith (default_os, "aix")
1481 && default_os[3] >= '4' && default_os[3] <= '9')
1482 ppc_cpu |= PPC_OPCODE_COMMON;
1483 else if (startswith (default_os, "aix3"))
1484 ppc_cpu |= PPC_OPCODE_POWER;
1485 else if (strcmp (default_cpu, "rs6000") == 0)
1486 ppc_cpu |= PPC_OPCODE_POWER;
1487 else if (startswith (default_cpu, "powerpc"))
1488 ppc_cpu |= PPC_OPCODE_PPC;
1489 else
1490 as_fatal (_("unknown default cpu = %s, os = %s"),
1491 default_cpu, default_os);
1495 /* Figure out the BFD architecture to use. This function and ppc_mach
1496 are called well before md_begin, when the output file is opened. */
1498 enum bfd_architecture
1499 ppc_arch (void)
1501 ppc_set_cpu ();
1503 #ifdef OBJ_ELF
1504 return bfd_arch_powerpc;
1505 #else
1506 if ((ppc_cpu & PPC_OPCODE_PPC) != 0)
1507 return bfd_arch_powerpc;
1508 if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
1509 return bfd_arch_powerpc;
1510 if ((ppc_cpu & PPC_OPCODE_POWER) != 0)
1511 return bfd_arch_rs6000;
1512 if ((ppc_cpu & (PPC_OPCODE_COMMON | PPC_OPCODE_ANY)) != 0)
1514 const char *default_cpu = TARGET_CPU;
1515 if (startswith (default_cpu, "powerpc"))
1516 return bfd_arch_powerpc;
1518 return bfd_arch_rs6000;
1519 #endif
1522 unsigned long
1523 ppc_mach (void)
1525 if (ppc_obj64)
1526 return bfd_mach_ppc64;
1527 else if (ppc_arch () == bfd_arch_rs6000)
1528 return bfd_mach_rs6k;
1529 else if (ppc_cpu & PPC_OPCODE_TITAN)
1530 return bfd_mach_ppc_titan;
1531 else if (ppc_cpu & PPC_OPCODE_VLE)
1532 return bfd_mach_ppc_vle;
1533 else
1534 return bfd_mach_ppc;
1537 extern const char*
1538 ppc_target_format (void)
1540 #ifdef OBJ_COFF
1541 #if TE_POWERMAC
1542 return "xcoff-powermac";
1543 #else
1544 # ifdef TE_AIX5
1545 return (ppc_obj64 ? "aix5coff64-rs6000" : "aixcoff-rs6000");
1546 # else
1547 return (ppc_obj64 ? "aixcoff64-rs6000" : "aixcoff-rs6000");
1548 # endif
1549 #endif
1550 #endif
1551 #ifdef OBJ_ELF
1552 # ifdef TE_FreeBSD
1553 return (ppc_obj64 ? "elf64-powerpc-freebsd" : "elf32-powerpc-freebsd");
1554 # elif defined (TE_VXWORKS)
1555 return "elf32-powerpc-vxworks";
1556 # else
1557 return (target_big_endian
1558 ? (ppc_obj64 ? "elf64-powerpc" : "elf32-powerpc")
1559 : (ppc_obj64 ? "elf64-powerpcle" : "elf32-powerpcle"));
1560 # endif
1561 #endif
1564 /* Validate one entry in powerpc_opcodes[] or vle_opcodes[].
1565 Return TRUE if there's a problem, otherwise FALSE. */
1567 static bool
1568 insn_validate (const struct powerpc_opcode *op)
1570 const ppc_opindex_t *o;
1571 uint64_t omask = op->mask;
1573 /* The mask had better not trim off opcode bits. */
1574 if ((op->opcode & omask) != op->opcode)
1576 as_bad (_("mask trims opcode bits for %s"), op->name);
1577 return true;
1580 /* The operands must not overlap the opcode or each other. */
1581 for (o = op->operands; *o; ++o)
1583 bool optional = false;
1584 if (*o >= num_powerpc_operands)
1586 as_bad (_("operand index error for %s"), op->name);
1587 return true;
1589 else
1591 uint64_t mask;
1592 const struct powerpc_operand *operand = &powerpc_operands[*o];
1593 if (operand->shift == (int) PPC_OPSHIFT_INV)
1595 const char *errmsg;
1596 uint64_t val;
1598 errmsg = NULL;
1599 val = -1;
1600 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
1601 val = -val;
1602 mask = (*operand->insert) (0, val, ppc_cpu, &errmsg);
1604 else if (operand->shift == (int) PPC_OPSHIFT_SH6)
1605 mask = (0x1f << 11) | 0x2;
1606 else if (operand->shift >= 0)
1607 mask = operand->bitm << operand->shift;
1608 else
1609 mask = operand->bitm >> -operand->shift;
1610 if (omask & mask)
1612 as_bad (_("operand %d overlap in %s"),
1613 (int) (o - op->operands), op->name);
1614 return true;
1616 omask |= mask;
1617 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
1618 optional = true;
1619 else if (optional)
1621 as_bad (_("non-optional operand %d follows optional operand in %s"),
1622 (int) (o - op->operands), op->name);
1623 return true;
1627 return false;
1630 static void *
1631 insn_calloc (size_t n, size_t size)
1633 size_t amt = n * size;
1634 void *ret = obstack_alloc (&insn_obstack, amt);
1635 memset (ret, 0, amt);
1636 return ret;
1639 /* Insert opcodes into hash tables. Called at startup and for
1640 .machine pseudo. */
1642 static void
1643 ppc_setup_opcodes (void)
1645 const struct powerpc_opcode *op;
1646 const struct powerpc_opcode *op_end;
1647 bool bad_insn = false;
1649 if (ppc_hash != NULL)
1651 htab_delete (ppc_hash);
1652 _obstack_free (&insn_obstack, NULL);
1655 obstack_begin (&insn_obstack, chunksize);
1657 /* Insert the opcodes into a hash table. */
1658 ppc_hash = htab_create_alloc (5000, hash_string_tuple, eq_string_tuple,
1659 NULL, insn_calloc, NULL);
1661 if (ENABLE_CHECKING)
1663 unsigned int i;
1665 /* An index into powerpc_operands is stored in struct fix
1666 fx_pcrel_adjust which is a 16 bit field. */
1667 gas_assert (num_powerpc_operands <= PPC_OPINDEX_MAX + 1);
1669 /* Check operand masks. Code here and in the disassembler assumes
1670 all the 1's in the mask are contiguous. */
1671 for (i = 0; i < num_powerpc_operands; ++i)
1673 uint64_t mask = powerpc_operands[i].bitm;
1674 unsigned long flags = powerpc_operands[i].flags;
1675 uint64_t right_bit;
1676 unsigned int j;
1678 if ((flags & PPC_OPERAND_PLUS1) != 0
1679 && (flags & PPC_OPERAND_NONZERO) != 0)
1680 as_bad ("mutually exclusive operand flags");
1682 right_bit = mask & -mask;
1683 mask += right_bit;
1684 right_bit = mask & -mask;
1685 if (mask != right_bit)
1687 as_bad (_("powerpc_operands[%d].bitm invalid"), i);
1688 bad_insn = true;
1690 for (j = i + 1; j < num_powerpc_operands; ++j)
1691 if (memcmp (&powerpc_operands[i], &powerpc_operands[j],
1692 sizeof (powerpc_operands[0])) == 0)
1694 as_bad (_("powerpc_operands[%d] duplicates powerpc_operands[%d]"),
1695 j, i);
1696 bad_insn = true;
1701 op_end = powerpc_opcodes + powerpc_num_opcodes;
1702 for (op = powerpc_opcodes; op < op_end; op++)
1704 if (ENABLE_CHECKING)
1706 unsigned int new_opcode = PPC_OP (op[0].opcode);
1708 #ifdef PRINT_OPCODE_TABLE
1709 printf ("%-14s\t#%04u\tmajor op: 0x%x\top: 0x%llx"
1710 "\tmask: 0x%llx\tflags: 0x%llx\n",
1711 op->name, (unsigned int) (op - powerpc_opcodes),
1712 new_opcode, (unsigned long long) op->opcode,
1713 (unsigned long long) op->mask,
1714 (unsigned long long) op->flags);
1715 #endif
1717 /* The major opcodes had better be sorted. Code in the disassembler
1718 assumes the insns are sorted according to major opcode. */
1719 if (op != powerpc_opcodes
1720 && new_opcode < PPC_OP (op[-1].opcode))
1722 as_bad (_("major opcode is not sorted for %s"), op->name);
1723 bad_insn = true;
1726 if ((op->flags & PPC_OPCODE_VLE) != 0)
1728 as_bad (_("%s is enabled by vle flag"), op->name);
1729 bad_insn = true;
1731 if (PPC_OP (op->opcode) != 4
1732 && PPC_OP (op->opcode) != 31
1733 && (op->deprecated & PPC_OPCODE_VLE) == 0)
1735 as_bad (_("%s not disabled by vle flag"), op->name);
1736 bad_insn = true;
1738 bad_insn |= insn_validate (op);
1741 if ((ppc_cpu & op->flags) != 0
1742 && !(ppc_cpu & op->deprecated)
1743 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1745 as_bad (_("duplicate %s"), op->name);
1746 bad_insn = true;
1750 if ((ppc_cpu & PPC_OPCODE_ANY) != 0)
1751 for (op = powerpc_opcodes; op < op_end; op++)
1752 str_hash_insert (ppc_hash, op->name, op, 0);
1754 op_end = prefix_opcodes + prefix_num_opcodes;
1755 for (op = prefix_opcodes; op < op_end; op++)
1757 if (ENABLE_CHECKING)
1759 unsigned int new_opcode = PPC_PREFIX_SEG (op[0].opcode);
1761 #ifdef PRINT_OPCODE_TABLE
1762 printf ("%-14s\t#%04u\tmajor op/2: 0x%x\top: 0x%llx"
1763 "\tmask: 0x%llx\tflags: 0x%llx\n",
1764 op->name, (unsigned int) (op - prefix_opcodes),
1765 new_opcode, (unsigned long long) op->opcode,
1766 (unsigned long long) op->mask,
1767 (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 != prefix_opcodes
1773 && new_opcode < PPC_PREFIX_SEG (op[-1].opcode))
1775 as_bad (_("major opcode is not sorted for %s"), op->name);
1776 bad_insn = true;
1778 bad_insn |= insn_validate (op);
1781 if ((ppc_cpu & op->flags) != 0
1782 && !(ppc_cpu & op->deprecated)
1783 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1785 as_bad (_("duplicate %s"), op->name);
1786 bad_insn = true;
1790 if ((ppc_cpu & PPC_OPCODE_ANY) != 0)
1791 for (op = prefix_opcodes; op < op_end; op++)
1792 str_hash_insert (ppc_hash, op->name, op, 0);
1794 if ((ppc_cpu & (PPC_OPCODE_VLE | PPC_OPCODE_ANY)) != 0)
1796 unsigned int prev_seg = 0;
1797 unsigned int seg;
1799 op_end = vle_opcodes + vle_num_opcodes;
1800 for (op = vle_opcodes; op < op_end; op++)
1802 if (ENABLE_CHECKING)
1804 seg = VLE_OP_TO_SEG (VLE_OP (op[0].opcode, op[0].mask));
1806 #ifdef PRINT_OPCODE_TABLE
1807 printf ("%-14s\t#%04u\tmajor op: 0x%x\top: 0x%llx"
1808 "\tmask: 0x%llx\tflags: 0x%llx\n",
1809 op->name, (unsigned int) (op - vle_opcodes),
1810 (unsigned int) seg, (unsigned long long) op->opcode,
1811 (unsigned long long) op->mask,
1812 (unsigned long long) op->flags);
1813 #endif
1815 if (seg < prev_seg)
1817 as_bad (_("major opcode is not sorted for %s"), op->name);
1818 bad_insn = true;
1820 prev_seg = seg;
1821 bad_insn |= insn_validate (op);
1824 str_hash_insert (ppc_hash, op->name, op, 0);
1828 /* LSP instructions */
1829 if ((ppc_cpu & (PPC_OPCODE_LSP | PPC_OPCODE_ANY)) != 0)
1831 unsigned int prev_seg = 0;
1832 unsigned int seg;
1833 op_end = lsp_opcodes + lsp_num_opcodes;
1834 for (op = lsp_opcodes; op < op_end; op++)
1836 if (ENABLE_CHECKING)
1838 seg = LSP_OP_TO_SEG (op->opcode);
1839 if (seg < prev_seg)
1841 as_bad (_("opcode is not sorted for %s"), op->name);
1842 bad_insn = true;
1844 prev_seg = seg;
1845 bad_insn |= insn_validate (op);
1848 str_hash_insert (ppc_hash, op->name, op, 0);
1852 /* SPE2 instructions */
1853 if ((ppc_cpu & (PPC_OPCODE_SPE2 | PPC_OPCODE_ANY)) != 0)
1855 unsigned int prev_seg = 0;
1856 unsigned int seg;
1857 op_end = spe2_opcodes + spe2_num_opcodes;
1858 for (op = spe2_opcodes; op < op_end; op++)
1860 if (ENABLE_CHECKING)
1862 seg = VLE_OP_TO_SEG (VLE_OP (op[0].opcode, op[0].mask));
1863 if (seg < prev_seg)
1865 as_bad (_("major opcode is not sorted for %s"), op->name);
1866 bad_insn = true;
1868 prev_seg = seg;
1869 bad_insn |= insn_validate (op);
1872 str_hash_insert (ppc_hash, op->name, op, 0);
1876 if (bad_insn)
1877 abort ();
1880 /* This function is called when the assembler starts up. It is called
1881 after the options have been parsed and the output file has been
1882 opened. */
1884 void
1885 md_begin (void)
1887 ppc_set_cpu ();
1889 ppc_cie_data_alignment = ppc_obj64 ? -8 : -4;
1890 ppc_dwarf2_line_min_insn_length = (ppc_cpu & PPC_OPCODE_VLE) ? 2 : 4;
1892 #ifdef OBJ_ELF
1893 /* Set the ELF flags if desired. */
1894 if (ppc_flags && !msolaris)
1895 bfd_set_private_flags (stdoutput, ppc_flags);
1896 #endif
1898 ppc_setup_opcodes ();
1900 /* Tell the main code what the endianness is if it is not overridden
1901 by the user. */
1902 if (!set_target_endian)
1904 set_target_endian = 1;
1905 target_big_endian = PPC_BIG_ENDIAN;
1908 #ifdef OBJ_XCOFF
1909 ppc_coff_debug_section = coff_section_from_bfd_index (stdoutput, N_DEBUG);
1911 /* Create XCOFF sections with .text in first, as it's creating dummy symbols
1912 to serve as initial csects. This forces the text csects to precede the
1913 data csects. These symbols will not be output. */
1914 ppc_init_xcoff_section (&ppc_xcoff_text_section, text_section);
1915 ppc_init_xcoff_section (&ppc_xcoff_data_section, data_section);
1916 ppc_init_xcoff_section (&ppc_xcoff_bss_section, bss_section);
1917 #endif
1920 void
1921 ppc_md_end (void)
1923 if (ppc_hash)
1925 htab_delete (ppc_hash);
1926 _obstack_free (&insn_obstack, NULL);
1928 ppc_hash = NULL;
1931 void
1932 ppc_cleanup (void)
1934 #ifdef OBJ_ELF
1935 if (ppc_apuinfo_list == NULL)
1936 return;
1938 /* Ok, so write the section info out. We have this layout:
1940 byte data what
1941 ---- ---- ----
1942 0 8 length of "APUinfo\0"
1943 4 (n*4) number of APU's (4 bytes each)
1944 8 2 note type 2
1945 12 "APUinfo\0" name
1946 20 APU#1 first APU's info
1947 24 APU#2 second APU's info
1948 ... ...
1951 char *p;
1952 asection *seg = now_seg;
1953 subsegT subseg = now_subseg;
1954 asection *apuinfo_secp = (asection *) NULL;
1955 unsigned int i;
1957 /* Create the .PPC.EMB.apuinfo section. */
1958 apuinfo_secp = subseg_new (APUINFO_SECTION_NAME, 0);
1959 bfd_set_section_flags (apuinfo_secp, SEC_HAS_CONTENTS | SEC_READONLY);
1961 p = frag_more (4);
1962 md_number_to_chars (p, (valueT) 8, 4);
1964 p = frag_more (4);
1965 md_number_to_chars (p, (valueT) ppc_apuinfo_num * 4, 4);
1967 p = frag_more (4);
1968 md_number_to_chars (p, (valueT) 2, 4);
1970 p = frag_more (8);
1971 strcpy (p, APUINFO_LABEL);
1973 for (i = 0; i < ppc_apuinfo_num; i++)
1975 p = frag_more (4);
1976 md_number_to_chars (p, (valueT) ppc_apuinfo_list[i], 4);
1979 frag_align (2, 0, 0);
1981 /* We probably can't restore the current segment, for there likely
1982 isn't one yet... */
1983 if (seg && subseg)
1984 subseg_set (seg, subseg);
1986 #endif
1989 /* Insert an operand value into an instruction. */
1991 static uint64_t
1992 ppc_insert_operand (uint64_t insn,
1993 const struct powerpc_operand *operand,
1994 int64_t val,
1995 ppc_cpu_t cpu,
1996 const char *file,
1997 unsigned int line)
1999 int64_t min, max, right;
2001 max = operand->bitm;
2002 right = max & -max;
2003 min = 0;
2005 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0)
2007 /* Extend the allowed range for addis to [-32768, 65535].
2008 Similarly for cmpli and some VLE high part insns. For 64-bit
2009 it would be good to disable this for signed fields since the
2010 value is sign extended into the high 32 bits of the register.
2011 If the value is, say, an address, then we might care about
2012 the high bits. However, gcc as of 2014-06 uses unsigned
2013 values when loading the high part of 64-bit constants using
2014 lis. */
2015 min = ~(max >> 1) & -right;
2017 else if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
2019 max = (max >> 1) & -right;
2020 min = ~max & -right;
2022 else if ((operand->flags & PPC_OPERAND_NONZERO) != 0)
2024 ++min;
2025 ++max;
2028 if ((operand->flags & PPC_OPERAND_PLUS1) != 0)
2029 max++;
2031 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
2033 int64_t tmp = min;
2034 min = -max;
2035 max = -tmp;
2038 if (min <= max)
2040 /* Some people write constants with the sign extension done by
2041 hand but only up to 32 bits. This shouldn't really be valid,
2042 but, to permit this code to assemble on a 64-bit host, we
2043 sign extend the 32-bit value to 64 bits if so doing makes the
2044 value valid. We only do this for operands that are 32-bits or
2045 smaller. */
2046 if (val > max
2047 && (operand->bitm & ~0xffffffffULL) == 0
2048 && (val - (1LL << 32)) >= min
2049 && (val - (1LL << 32)) <= max
2050 && ((val - (1LL << 32)) & (right - 1)) == 0)
2051 val = val - (1LL << 32);
2053 /* Similarly, people write expressions like ~(1<<15), and expect
2054 this to be OK for a 32-bit unsigned value. */
2055 else if (val < min
2056 && (operand->bitm & ~0xffffffffULL) == 0
2057 && (val + (1LL << 32)) >= min
2058 && (val + (1LL << 32)) <= max
2059 && ((val + (1LL << 32)) & (right - 1)) == 0)
2060 val = val + (1LL << 32);
2062 else if (val < min
2063 || val > max
2064 || (val & (right - 1)) != 0)
2065 as_bad_value_out_of_range (_("operand"), val, min, max, file, line);
2068 if (operand->insert)
2070 const char *errmsg;
2072 errmsg = NULL;
2073 insn = (*operand->insert) (insn, val, cpu, &errmsg);
2074 if (errmsg != (const char *) NULL)
2075 as_bad_where (file, line, "%s", errmsg);
2077 else
2079 if ((operand->flags & PPC_OPERAND_NONZERO) != 0)
2080 --val;
2081 if (operand->shift >= 0)
2082 insn |= (val & operand->bitm) << operand->shift;
2083 else
2084 insn |= (val & operand->bitm) >> -operand->shift;
2087 return insn;
2091 #ifdef OBJ_ELF
2092 /* Parse @got, etc. and return the desired relocation. */
2093 static bfd_reloc_code_real_type
2094 ppc_elf_suffix (char **str_p, expressionS *exp_p)
2096 struct map_bfd {
2097 const char *string;
2098 unsigned int length : 8;
2099 unsigned int valid32 : 1;
2100 unsigned int valid64 : 1;
2101 unsigned int reloc;
2104 char ident[20];
2105 char *str = *str_p;
2106 char *str2;
2107 int ch;
2108 int len;
2109 const struct map_bfd *ptr;
2111 #define MAP(str, reloc) { str, sizeof (str) - 1, 1, 1, reloc }
2112 #define MAP32(str, reloc) { str, sizeof (str) - 1, 1, 0, reloc }
2113 #define MAP64(str, reloc) { str, sizeof (str) - 1, 0, 1, reloc }
2115 static const struct map_bfd mapping[] = {
2116 MAP ("l", BFD_RELOC_LO16),
2117 MAP ("h", BFD_RELOC_HI16),
2118 MAP ("ha", BFD_RELOC_HI16_S),
2119 MAP ("brtaken", BFD_RELOC_PPC_B16_BRTAKEN),
2120 MAP ("brntaken", BFD_RELOC_PPC_B16_BRNTAKEN),
2121 MAP ("got", BFD_RELOC_16_GOTOFF),
2122 MAP ("got@l", BFD_RELOC_LO16_GOTOFF),
2123 MAP ("got@h", BFD_RELOC_HI16_GOTOFF),
2124 MAP ("got@ha", BFD_RELOC_HI16_S_GOTOFF),
2125 MAP ("plt@l", BFD_RELOC_LO16_PLTOFF),
2126 MAP ("plt@h", BFD_RELOC_HI16_PLTOFF),
2127 MAP ("plt@ha", BFD_RELOC_HI16_S_PLTOFF),
2128 MAP ("copy", BFD_RELOC_PPC_COPY),
2129 MAP ("globdat", BFD_RELOC_PPC_GLOB_DAT),
2130 MAP ("sectoff", BFD_RELOC_16_BASEREL),
2131 MAP ("sectoff@l", BFD_RELOC_LO16_BASEREL),
2132 MAP ("sectoff@h", BFD_RELOC_HI16_BASEREL),
2133 MAP ("sectoff@ha", BFD_RELOC_HI16_S_BASEREL),
2134 MAP ("tls", BFD_RELOC_PPC_TLS),
2135 MAP ("dtpmod", BFD_RELOC_PPC_DTPMOD),
2136 MAP ("dtprel", BFD_RELOC_PPC_DTPREL),
2137 MAP ("dtprel@l", BFD_RELOC_PPC_DTPREL16_LO),
2138 MAP ("dtprel@h", BFD_RELOC_PPC_DTPREL16_HI),
2139 MAP ("dtprel@ha", BFD_RELOC_PPC_DTPREL16_HA),
2140 MAP ("tprel", BFD_RELOC_PPC_TPREL),
2141 MAP ("tprel@l", BFD_RELOC_PPC_TPREL16_LO),
2142 MAP ("tprel@h", BFD_RELOC_PPC_TPREL16_HI),
2143 MAP ("tprel@ha", BFD_RELOC_PPC_TPREL16_HA),
2144 MAP ("got@tlsgd", BFD_RELOC_PPC_GOT_TLSGD16),
2145 MAP ("got@tlsgd@l", BFD_RELOC_PPC_GOT_TLSGD16_LO),
2146 MAP ("got@tlsgd@h", BFD_RELOC_PPC_GOT_TLSGD16_HI),
2147 MAP ("got@tlsgd@ha", BFD_RELOC_PPC_GOT_TLSGD16_HA),
2148 MAP ("got@tlsld", BFD_RELOC_PPC_GOT_TLSLD16),
2149 MAP ("got@tlsld@l", BFD_RELOC_PPC_GOT_TLSLD16_LO),
2150 MAP ("got@tlsld@h", BFD_RELOC_PPC_GOT_TLSLD16_HI),
2151 MAP ("got@tlsld@ha", BFD_RELOC_PPC_GOT_TLSLD16_HA),
2152 MAP ("got@dtprel", BFD_RELOC_PPC_GOT_DTPREL16),
2153 MAP ("got@dtprel@l", BFD_RELOC_PPC_GOT_DTPREL16_LO),
2154 MAP ("got@dtprel@h", BFD_RELOC_PPC_GOT_DTPREL16_HI),
2155 MAP ("got@dtprel@ha", BFD_RELOC_PPC_GOT_DTPREL16_HA),
2156 MAP ("got@tprel", BFD_RELOC_PPC_GOT_TPREL16),
2157 MAP ("got@tprel@l", BFD_RELOC_PPC_GOT_TPREL16_LO),
2158 MAP ("got@tprel@h", BFD_RELOC_PPC_GOT_TPREL16_HI),
2159 MAP ("got@tprel@ha", BFD_RELOC_PPC_GOT_TPREL16_HA),
2160 MAP32 ("fixup", BFD_RELOC_CTOR),
2161 MAP32 ("plt", BFD_RELOC_24_PLT_PCREL),
2162 MAP32 ("pltrel24", BFD_RELOC_24_PLT_PCREL),
2163 MAP32 ("local24pc", BFD_RELOC_PPC_LOCAL24PC),
2164 MAP32 ("local", BFD_RELOC_PPC_LOCAL24PC),
2165 MAP32 ("pltrel", BFD_RELOC_32_PLT_PCREL),
2166 MAP32 ("sdarel", BFD_RELOC_GPREL16),
2167 MAP32 ("sdarel@l", BFD_RELOC_PPC_VLE_SDAREL_LO16A),
2168 MAP32 ("sdarel@h", BFD_RELOC_PPC_VLE_SDAREL_HI16A),
2169 MAP32 ("sdarel@ha", BFD_RELOC_PPC_VLE_SDAREL_HA16A),
2170 MAP32 ("naddr", BFD_RELOC_PPC_EMB_NADDR32),
2171 MAP32 ("naddr16", BFD_RELOC_PPC_EMB_NADDR16),
2172 MAP32 ("naddr@l", BFD_RELOC_PPC_EMB_NADDR16_LO),
2173 MAP32 ("naddr@h", BFD_RELOC_PPC_EMB_NADDR16_HI),
2174 MAP32 ("naddr@ha", BFD_RELOC_PPC_EMB_NADDR16_HA),
2175 MAP32 ("sdai16", BFD_RELOC_PPC_EMB_SDAI16),
2176 MAP32 ("sda2rel", BFD_RELOC_PPC_EMB_SDA2REL),
2177 MAP32 ("sda2i16", BFD_RELOC_PPC_EMB_SDA2I16),
2178 MAP32 ("sda21", BFD_RELOC_PPC_EMB_SDA21),
2179 MAP32 ("sda21@l", BFD_RELOC_PPC_VLE_SDA21_LO),
2180 MAP32 ("mrkref", BFD_RELOC_PPC_EMB_MRKREF),
2181 MAP32 ("relsect", BFD_RELOC_PPC_EMB_RELSEC16),
2182 MAP32 ("relsect@l", BFD_RELOC_PPC_EMB_RELST_LO),
2183 MAP32 ("relsect@h", BFD_RELOC_PPC_EMB_RELST_HI),
2184 MAP32 ("relsect@ha", BFD_RELOC_PPC_EMB_RELST_HA),
2185 MAP32 ("bitfld", BFD_RELOC_PPC_EMB_BIT_FLD),
2186 MAP32 ("relsda", BFD_RELOC_PPC_EMB_RELSDA),
2187 MAP32 ("xgot", BFD_RELOC_PPC_TOC16),
2188 MAP64 ("high", BFD_RELOC_PPC64_ADDR16_HIGH),
2189 MAP64 ("higha", BFD_RELOC_PPC64_ADDR16_HIGHA),
2190 MAP64 ("higher", BFD_RELOC_PPC64_HIGHER),
2191 MAP64 ("highera", BFD_RELOC_PPC64_HIGHER_S),
2192 MAP64 ("highest", BFD_RELOC_PPC64_HIGHEST),
2193 MAP64 ("highesta", BFD_RELOC_PPC64_HIGHEST_S),
2194 MAP64 ("tocbase", BFD_RELOC_PPC64_TOC),
2195 MAP64 ("toc", BFD_RELOC_PPC_TOC16),
2196 MAP64 ("toc@l", BFD_RELOC_PPC64_TOC16_LO),
2197 MAP64 ("toc@h", BFD_RELOC_PPC64_TOC16_HI),
2198 MAP64 ("toc@ha", BFD_RELOC_PPC64_TOC16_HA),
2199 MAP64 ("dtprel@high", BFD_RELOC_PPC64_DTPREL16_HIGH),
2200 MAP64 ("dtprel@higha", BFD_RELOC_PPC64_DTPREL16_HIGHA),
2201 MAP64 ("dtprel@higher", BFD_RELOC_PPC64_DTPREL16_HIGHER),
2202 MAP64 ("dtprel@highera", BFD_RELOC_PPC64_DTPREL16_HIGHERA),
2203 MAP64 ("dtprel@highest", BFD_RELOC_PPC64_DTPREL16_HIGHEST),
2204 MAP64 ("dtprel@highesta", BFD_RELOC_PPC64_DTPREL16_HIGHESTA),
2205 MAP64 ("localentry", BFD_RELOC_PPC64_ADDR64_LOCAL),
2206 MAP64 ("tprel@high", BFD_RELOC_PPC64_TPREL16_HIGH),
2207 MAP64 ("tprel@higha", BFD_RELOC_PPC64_TPREL16_HIGHA),
2208 MAP64 ("tprel@higher", BFD_RELOC_PPC64_TPREL16_HIGHER),
2209 MAP64 ("tprel@highera", BFD_RELOC_PPC64_TPREL16_HIGHERA),
2210 MAP64 ("tprel@highest", BFD_RELOC_PPC64_TPREL16_HIGHEST),
2211 MAP64 ("tprel@highesta", BFD_RELOC_PPC64_TPREL16_HIGHESTA),
2212 MAP64 ("notoc", BFD_RELOC_PPC64_REL24_NOTOC),
2213 MAP64 ("pcrel", BFD_RELOC_PPC64_PCREL34),
2214 MAP64 ("got@pcrel", BFD_RELOC_PPC64_GOT_PCREL34),
2215 MAP64 ("plt@pcrel", BFD_RELOC_PPC64_PLT_PCREL34),
2216 MAP64 ("tls@pcrel", BFD_RELOC_PPC64_TLS_PCREL),
2217 MAP64 ("got@tlsgd@pcrel", BFD_RELOC_PPC64_GOT_TLSGD_PCREL34),
2218 MAP64 ("got@tlsld@pcrel", BFD_RELOC_PPC64_GOT_TLSLD_PCREL34),
2219 MAP64 ("got@tprel@pcrel", BFD_RELOC_PPC64_GOT_TPREL_PCREL34),
2220 MAP64 ("got@dtprel@pcrel", BFD_RELOC_PPC64_GOT_DTPREL_PCREL34),
2221 MAP64 ("higher34", BFD_RELOC_PPC64_ADDR16_HIGHER34),
2222 MAP64 ("highera34", BFD_RELOC_PPC64_ADDR16_HIGHERA34),
2223 MAP64 ("highest34", BFD_RELOC_PPC64_ADDR16_HIGHEST34),
2224 MAP64 ("highesta34", BFD_RELOC_PPC64_ADDR16_HIGHESTA34),
2225 { (char *) 0, 0, 0, 0, BFD_RELOC_NONE }
2228 if (*str++ != '@')
2229 return BFD_RELOC_NONE;
2231 for (ch = *str, str2 = ident;
2232 (str2 < ident + sizeof (ident) - 1
2233 && (ISALNUM (ch) || ch == '@'));
2234 ch = *++str)
2236 *str2++ = TOLOWER (ch);
2239 *str2 = '\0';
2240 len = str2 - ident;
2242 ch = ident[0];
2243 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
2244 if (ch == ptr->string[0]
2245 && len == ptr->length
2246 && memcmp (ident, ptr->string, ptr->length) == 0
2247 && (ppc_obj64 ? ptr->valid64 : ptr->valid32))
2249 int reloc = ptr->reloc;
2251 if (!ppc_obj64 && (exp_p->X_op == O_big || exp_p->X_add_number != 0))
2253 switch (reloc)
2255 case BFD_RELOC_16_GOTOFF:
2256 case BFD_RELOC_LO16_GOTOFF:
2257 case BFD_RELOC_HI16_GOTOFF:
2258 case BFD_RELOC_HI16_S_GOTOFF:
2259 as_warn (_("symbol+offset@%s means symbol@%s+offset"),
2260 ptr->string, ptr->string);
2261 break;
2263 case BFD_RELOC_PPC_GOT_TLSGD16:
2264 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
2265 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
2266 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
2267 case BFD_RELOC_PPC_GOT_TLSLD16:
2268 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
2269 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
2270 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
2271 case BFD_RELOC_PPC_GOT_DTPREL16:
2272 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
2273 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
2274 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
2275 case BFD_RELOC_PPC_GOT_TPREL16:
2276 case BFD_RELOC_PPC_GOT_TPREL16_LO:
2277 case BFD_RELOC_PPC_GOT_TPREL16_HI:
2278 case BFD_RELOC_PPC_GOT_TPREL16_HA:
2279 as_bad (_("symbol+offset@%s not supported"), ptr->string);
2280 break;
2284 /* Now check for identifier@suffix+constant. */
2285 if (*str == '-' || *str == '+')
2287 char *orig_line = input_line_pointer;
2288 expressionS new_exp;
2290 input_line_pointer = str;
2291 expression (&new_exp);
2292 if (new_exp.X_op == O_constant && exp_p->X_op != O_big)
2294 exp_p->X_add_number += new_exp.X_add_number;
2295 str = input_line_pointer;
2297 input_line_pointer = orig_line;
2299 *str_p = str;
2301 if (reloc == (int) BFD_RELOC_PPC64_TOC
2302 && exp_p->X_op == O_symbol
2303 && strcmp (S_GET_NAME (exp_p->X_add_symbol), ".TOC.") == 0)
2305 /* Change the symbol so that the dummy .TOC. symbol can be
2306 omitted from the object file. */
2307 exp_p->X_add_symbol = &abs_symbol;
2310 if (reloc == BFD_RELOC_PPC64_REL24_NOTOC
2311 && (ppc_cpu & PPC_OPCODE_POWER10) == 0)
2312 reloc = BFD_RELOC_PPC64_REL24_P9NOTOC;
2314 return (bfd_reloc_code_real_type) reloc;
2317 return BFD_RELOC_NONE;
2320 /* Support @got, etc. on constants emitted via .short, .int etc. */
2322 bfd_reloc_code_real_type
2323 ppc_elf_parse_cons (expressionS *exp, unsigned int nbytes)
2325 expression (exp);
2326 if (nbytes >= 2 && *input_line_pointer == '@')
2327 return ppc_elf_suffix (&input_line_pointer, exp);
2328 return BFD_RELOC_NONE;
2331 /* Warn when emitting data to code sections, unless we are emitting
2332 a relocation that ld --ppc476-workaround uses to recognise data
2333 *and* there was an unconditional branch prior to the data. */
2335 void
2336 ppc_elf_cons_fix_check (expressionS *exp ATTRIBUTE_UNUSED,
2337 unsigned int nbytes, fixS *fix)
2339 if (warn_476
2340 && (now_seg->flags & SEC_CODE) != 0
2341 && (nbytes != 4
2342 || fix == NULL
2343 || !(fix->fx_r_type == BFD_RELOC_32
2344 || fix->fx_r_type == BFD_RELOC_CTOR
2345 || fix->fx_r_type == BFD_RELOC_32_PCREL)
2346 || !(last_seg == now_seg && last_subseg == now_subseg)
2347 || !((last_insn & (0x3f << 26)) == (18u << 26)
2348 || ((last_insn & (0x3f << 26)) == (16u << 26)
2349 && (last_insn & (0x14 << 21)) == (0x14 << 21))
2350 || ((last_insn & (0x3f << 26)) == (19u << 26)
2351 && (last_insn & (0x3ff << 1)) == (16u << 1)
2352 && (last_insn & (0x14 << 21)) == (0x14 << 21)))))
2354 /* Flag that we've warned. */
2355 if (fix != NULL)
2356 fix->fx_tcbit = 1;
2358 as_warn (_("data in executable section"));
2362 /* Solaris pseduo op to change to the .rodata section. */
2363 static void
2364 ppc_elf_rdata (int xxx)
2366 char *save_line = input_line_pointer;
2367 static char section[] = ".rodata\n";
2369 /* Just pretend this is .section .rodata */
2370 input_line_pointer = section;
2371 obj_elf_section (xxx);
2373 input_line_pointer = save_line;
2376 /* Pseudo op to make file scope bss items. */
2377 static void
2378 ppc_elf_lcomm (int xxx ATTRIBUTE_UNUSED)
2380 char *name;
2381 char c;
2382 char *p;
2383 offsetT size;
2384 symbolS *symbolP;
2385 offsetT align;
2386 segT old_sec;
2387 int old_subsec;
2388 char *pfrag;
2389 int align2;
2391 c = get_symbol_name (&name);
2393 /* Just after name is now '\0'. */
2394 p = input_line_pointer;
2395 *p = c;
2396 SKIP_WHITESPACE_AFTER_NAME ();
2397 if (*input_line_pointer != ',')
2399 as_bad (_("expected comma after symbol-name: rest of line ignored."));
2400 ignore_rest_of_line ();
2401 return;
2404 input_line_pointer++; /* skip ',' */
2405 if ((size = get_absolute_expression ()) < 0)
2407 as_warn (_(".COMMon length (%ld.) <0! Ignored."), (long) size);
2408 ignore_rest_of_line ();
2409 return;
2412 /* The third argument to .lcomm is the alignment. */
2413 if (*input_line_pointer != ',')
2414 align = 8;
2415 else
2417 ++input_line_pointer;
2418 align = get_absolute_expression ();
2419 if (align <= 0)
2421 as_warn (_("ignoring bad alignment"));
2422 align = 8;
2426 *p = 0;
2427 symbolP = symbol_find_or_make (name);
2428 *p = c;
2430 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
2432 as_bad (_("ignoring attempt to re-define symbol `%s'."),
2433 S_GET_NAME (symbolP));
2434 ignore_rest_of_line ();
2435 return;
2438 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
2440 as_bad (_("length of .lcomm \"%s\" is already %ld. Not changed to %ld."),
2441 S_GET_NAME (symbolP),
2442 (long) S_GET_VALUE (symbolP),
2443 (long) size);
2445 ignore_rest_of_line ();
2446 return;
2449 /* Allocate_bss. */
2450 old_sec = now_seg;
2451 old_subsec = now_subseg;
2452 if (align)
2454 /* Convert to a power of 2 alignment. */
2455 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2);
2456 if (align != 1)
2458 as_bad (_("common alignment not a power of 2"));
2459 ignore_rest_of_line ();
2460 return;
2463 else
2464 align2 = 0;
2466 record_alignment (bss_section, align2);
2467 subseg_set (bss_section, 1);
2468 if (align2)
2469 frag_align (align2, 0, 0);
2470 if (S_GET_SEGMENT (symbolP) == bss_section)
2471 symbol_get_frag (symbolP)->fr_symbol = 0;
2472 symbol_set_frag (symbolP, frag_now);
2473 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
2474 (char *) 0);
2475 *pfrag = 0;
2476 S_SET_SIZE (symbolP, size);
2477 S_SET_SEGMENT (symbolP, bss_section);
2478 subseg_set (old_sec, old_subsec);
2479 demand_empty_rest_of_line ();
2482 /* Pseudo op to set symbol local entry point. */
2483 static void
2484 ppc_elf_localentry (int ignore ATTRIBUTE_UNUSED)
2486 char *name;
2487 char c = get_symbol_name (&name);
2488 char *p;
2489 expressionS exp;
2490 symbolS *sym;
2491 asymbol *bfdsym;
2492 elf_symbol_type *elfsym;
2494 p = input_line_pointer;
2495 *p = c;
2496 SKIP_WHITESPACE_AFTER_NAME ();
2497 if (*input_line_pointer != ',')
2499 *p = 0;
2500 as_bad (_("expected comma after name `%s' in .localentry directive"),
2501 name);
2502 *p = c;
2503 ignore_rest_of_line ();
2504 return;
2506 input_line_pointer++;
2507 expression (&exp);
2508 if (exp.X_op == O_absent)
2510 as_bad (_("missing expression in .localentry directive"));
2511 exp.X_op = O_constant;
2512 exp.X_add_number = 0;
2514 *p = 0;
2515 sym = symbol_find_or_make (name);
2516 *p = c;
2518 if (resolve_expression (&exp)
2519 && exp.X_op == O_constant)
2521 unsigned int encoded, ok;
2523 ok = 1;
2524 if (exp.X_add_number == 1 || exp.X_add_number == 7)
2525 encoded = exp.X_add_number << STO_PPC64_LOCAL_BIT;
2526 else
2528 encoded = PPC64_SET_LOCAL_ENTRY_OFFSET (exp.X_add_number);
2529 if (exp.X_add_number != (offsetT) PPC64_LOCAL_ENTRY_OFFSET (encoded))
2531 as_bad (_(".localentry expression for `%s' "
2532 "is not a valid power of 2"), S_GET_NAME (sym));
2533 ok = 0;
2536 if (ok)
2538 bfdsym = symbol_get_bfdsym (sym);
2539 elfsym = elf_symbol_from (bfdsym);
2540 gas_assert (elfsym);
2541 elfsym->internal_elf_sym.st_other &= ~STO_PPC64_LOCAL_MASK;
2542 elfsym->internal_elf_sym.st_other |= encoded;
2543 if (ppc_abiversion == 0)
2544 ppc_abiversion = 2;
2547 else
2548 as_bad (_(".localentry expression for `%s' "
2549 "does not evaluate to a constant"), S_GET_NAME (sym));
2551 demand_empty_rest_of_line ();
2554 /* Pseudo op to set ABI version. */
2555 static void
2556 ppc_elf_abiversion (int ignore ATTRIBUTE_UNUSED)
2558 expressionS exp;
2560 expression (&exp);
2561 if (exp.X_op == O_absent)
2563 as_bad (_("missing expression in .abiversion directive"));
2564 exp.X_op = O_constant;
2565 exp.X_add_number = 0;
2568 if (resolve_expression (&exp)
2569 && exp.X_op == O_constant)
2570 ppc_abiversion = exp.X_add_number;
2571 else
2572 as_bad (_(".abiversion expression does not evaluate to a constant"));
2573 demand_empty_rest_of_line ();
2576 /* Parse a .gnu_attribute directive. */
2577 static void
2578 ppc_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
2580 int tag = obj_elf_vendor_attribute (OBJ_ATTR_GNU);
2582 /* Check validity of defined powerpc tags. */
2583 if (tag == Tag_GNU_Power_ABI_FP
2584 || tag == Tag_GNU_Power_ABI_Vector
2585 || tag == Tag_GNU_Power_ABI_Struct_Return)
2587 unsigned int val;
2589 val = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU, tag);
2591 if ((tag == Tag_GNU_Power_ABI_FP && val > 15)
2592 || (tag == Tag_GNU_Power_ABI_Vector && val > 3)
2593 || (tag == Tag_GNU_Power_ABI_Struct_Return && val > 2))
2594 as_warn (_("unknown .gnu_attribute value"));
2598 /* Set ABI version in output file. */
2599 void
2600 ppc_elf_md_finish (void)
2602 if (ppc_obj64 && ppc_abiversion != 0)
2604 elf_elfheader (stdoutput)->e_flags &= ~EF_PPC64_ABI;
2605 elf_elfheader (stdoutput)->e_flags |= ppc_abiversion & EF_PPC64_ABI;
2607 /* Any selection of opcodes based on ppc_cpu after gas has finished
2608 parsing the file is invalid. md_apply_fix and ppc_handle_align
2609 must select opcodes based on the machine in force at the point
2610 where the fixup or alignment frag was created, not the machine in
2611 force at the end of file. */
2612 ppc_cpu = 0;
2615 /* Validate any relocations emitted for -mrelocatable, possibly adding
2616 fixups for word relocations in writable segments, so we can adjust
2617 them at runtime. */
2618 static void
2619 ppc_elf_validate_fix (fixS *fixp, segT seg)
2621 if (fixp->fx_done || fixp->fx_pcrel)
2622 return;
2624 switch (shlib)
2626 case SHLIB_NONE:
2627 case SHLIB_PIC:
2628 return;
2630 case SHLIB_MRELOCATABLE:
2631 if (fixp->fx_r_type != BFD_RELOC_16_GOTOFF
2632 && fixp->fx_r_type != BFD_RELOC_HI16_GOTOFF
2633 && fixp->fx_r_type != BFD_RELOC_LO16_GOTOFF
2634 && fixp->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
2635 && fixp->fx_r_type != BFD_RELOC_16_BASEREL
2636 && fixp->fx_r_type != BFD_RELOC_LO16_BASEREL
2637 && fixp->fx_r_type != BFD_RELOC_HI16_BASEREL
2638 && fixp->fx_r_type != BFD_RELOC_HI16_S_BASEREL
2639 && (seg->flags & SEC_LOAD) != 0
2640 && strcmp (segment_name (seg), ".got2") != 0
2641 && strcmp (segment_name (seg), ".dtors") != 0
2642 && strcmp (segment_name (seg), ".ctors") != 0
2643 && strcmp (segment_name (seg), ".fixup") != 0
2644 && strcmp (segment_name (seg), ".gcc_except_table") != 0
2645 && strcmp (segment_name (seg), ".eh_frame") != 0
2646 && strcmp (segment_name (seg), ".ex_shared") != 0)
2648 if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
2649 || fixp->fx_r_type != BFD_RELOC_CTOR)
2651 as_bad_where (fixp->fx_file, fixp->fx_line,
2652 _("relocation cannot be done when using -mrelocatable"));
2655 return;
2659 /* Prevent elf_frob_file_before_adjust removing a weak undefined
2660 function descriptor sym if the corresponding code sym is used. */
2662 void
2663 ppc_frob_file_before_adjust (void)
2665 symbolS *symp;
2666 asection *toc;
2668 if (!ppc_obj64)
2669 return;
2671 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2673 const char *name;
2674 char *dotname;
2675 symbolS *dotsym;
2677 name = S_GET_NAME (symp);
2678 if (name[0] == '.')
2679 continue;
2681 if (! S_IS_WEAK (symp)
2682 || S_IS_DEFINED (symp))
2683 continue;
2685 dotname = concat (".", name, (char *) NULL);
2686 dotsym = symbol_find_noref (dotname, 1);
2687 free (dotname);
2688 if (dotsym != NULL && (symbol_used_p (dotsym)
2689 || symbol_used_in_reloc_p (dotsym)))
2690 symbol_mark_used (symp);
2694 toc = bfd_get_section_by_name (stdoutput, ".toc");
2695 if (toc != NULL
2696 && toc_reloc_types != has_large_toc_reloc
2697 && bfd_section_size (toc) > 0x10000)
2698 as_warn (_("TOC section size exceeds 64k"));
2701 /* .TOC. used in an opd entry as .TOC.@tocbase doesn't need to be
2702 emitted. Other uses of .TOC. will cause the symbol to be marked
2703 with BSF_KEEP in md_apply_fix. */
2705 void
2706 ppc_elf_adjust_symtab (void)
2708 if (ppc_obj64)
2710 symbolS *symp;
2711 symp = symbol_find (".TOC.");
2712 if (symp != NULL)
2714 asymbol *bsym = symbol_get_bfdsym (symp);
2715 if ((bsym->flags & BSF_KEEP) == 0)
2716 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2720 #endif /* OBJ_ELF */
2722 #ifdef OBJ_XCOFF
2723 /* Parse XCOFF relocations. */
2724 static bfd_reloc_code_real_type
2725 ppc_xcoff_suffix (char **str_p)
2727 struct map_bfd {
2728 const char *string;
2729 unsigned int length : 8;
2730 unsigned int valid32 : 1;
2731 unsigned int valid64 : 1;
2732 unsigned int reloc;
2735 char ident[20];
2736 char *str = *str_p;
2737 char *str2;
2738 int ch;
2739 int len;
2740 const struct map_bfd *ptr;
2742 #define MAP(str, reloc) { str, sizeof (str) - 1, 1, 1, reloc }
2743 #define MAP32(str, reloc) { str, sizeof (str) - 1, 1, 0, reloc }
2744 #define MAP64(str, reloc) { str, sizeof (str) - 1, 0, 1, reloc }
2746 static const struct map_bfd mapping[] = {
2747 MAP ("l", BFD_RELOC_PPC_TOC16_LO),
2748 MAP ("u", BFD_RELOC_PPC_TOC16_HI),
2749 MAP32 ("ie", BFD_RELOC_PPC_TLSIE),
2750 MAP32 ("ld", BFD_RELOC_PPC_TLSLD),
2751 MAP32 ("le", BFD_RELOC_PPC_TLSLE),
2752 MAP32 ("m", BFD_RELOC_PPC_TLSM),
2753 MAP32 ("ml", BFD_RELOC_PPC_TLSML),
2754 MAP64 ("ie", BFD_RELOC_PPC64_TLSIE),
2755 MAP64 ("ld", BFD_RELOC_PPC64_TLSLD),
2756 MAP64 ("le", BFD_RELOC_PPC64_TLSLE),
2757 MAP64 ("m", BFD_RELOC_PPC64_TLSM),
2758 MAP64 ("ml", BFD_RELOC_PPC64_TLSML),
2761 if (*str++ != '@')
2762 return BFD_RELOC_NONE;
2764 for (ch = *str, str2 = ident;
2765 (str2 < ident + sizeof (ident) - 1
2766 && (ISALNUM (ch) || ch == '@'));
2767 ch = *++str)
2769 *str2++ = TOLOWER (ch);
2772 *str2 = '\0';
2773 len = str2 - ident;
2775 ch = ident[0];
2776 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
2777 if (ch == ptr->string[0]
2778 && len == ptr->length
2779 && memcmp (ident, ptr->string, ptr->length) == 0
2780 && (ppc_obj64 ? ptr->valid64 : ptr->valid32))
2782 *str_p = str;
2783 return (bfd_reloc_code_real_type) ptr->reloc;
2786 return BFD_RELOC_NONE;
2789 /* Restore XCOFF addis instruction to ELF format.
2790 AIX often generates addis instructions using "addis RT,D(RA)"
2791 format instead of the ELF "addis RT,RA,SI" one.
2792 On entry RT_E is at the comma after RT, D_E is at the open
2793 parenthesis after D, and RA_E is at the close parenthesis after RA. */
2794 static void
2795 ppc_xcoff_fixup_addis (char *rt_e, char *d_e, char *ra_e)
2797 size_t ra_size = ra_e - d_e - 1;
2798 char *save_ra = xmalloc (ra_size);
2800 /* Copy RA. */
2801 memcpy (save_ra, d_e + 1, ra_size);
2802 /* Shuffle D to make room for RA, copying the comma too. */
2803 memmove (rt_e + ra_size + 1, rt_e, d_e - rt_e);
2804 /* Erase the trailing ')', keeping any rubbish for potential errors. */
2805 memmove (ra_e, ra_e + 1, strlen (ra_e));
2806 /* Write RA back. */
2807 memcpy (rt_e + 1, save_ra, ra_size);
2808 free (save_ra);
2811 /* Support @ie, etc. on constants emitted via .short, .int etc. */
2813 bfd_reloc_code_real_type
2814 ppc_xcoff_parse_cons (expressionS *exp, unsigned int nbytes)
2816 expression (exp);
2817 if (nbytes >= 2 && *input_line_pointer == '@')
2818 return ppc_xcoff_suffix (&input_line_pointer);
2820 /* There isn't any @ symbol for default TLS relocations (R_TLS). */
2821 if (exp->X_add_symbol != NULL
2822 && (symbol_get_tc (exp->X_add_symbol)->symbol_class == XMC_TL
2823 || symbol_get_tc (exp->X_add_symbol)->symbol_class == XMC_UL))
2824 return (ppc_obj64 ? BFD_RELOC_PPC64_TLSGD: BFD_RELOC_PPC_TLSGD);
2826 return BFD_RELOC_NONE;
2829 #endif /* OBJ_XCOFF */
2831 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
2832 /* See whether a symbol is in the TOC section. */
2834 static int
2835 ppc_is_toc_sym (symbolS *sym)
2837 #ifdef OBJ_XCOFF
2838 return (symbol_get_tc (sym)->symbol_class == XMC_TC
2839 || symbol_get_tc (sym)->symbol_class == XMC_TE
2840 || symbol_get_tc (sym)->symbol_class == XMC_TC0);
2841 #endif
2842 #ifdef OBJ_ELF
2843 const char *sname = segment_name (S_GET_SEGMENT (sym));
2844 if (ppc_obj64)
2845 return strcmp (sname, ".toc") == 0;
2846 else
2847 return strcmp (sname, ".got") == 0;
2848 #endif
2850 #endif /* defined (OBJ_XCOFF) || defined (OBJ_ELF) */
2853 #ifdef OBJ_ELF
2854 #define APUID(a,v) ((((a) & 0xffff) << 16) | ((v) & 0xffff))
2855 static void
2856 ppc_apuinfo_section_add (unsigned int apu, unsigned int version)
2858 unsigned int i;
2860 /* Check we don't already exist. */
2861 for (i = 0; i < ppc_apuinfo_num; i++)
2862 if (ppc_apuinfo_list[i] == APUID (apu, version))
2863 return;
2865 if (ppc_apuinfo_num == ppc_apuinfo_num_alloc)
2867 if (ppc_apuinfo_num_alloc == 0)
2869 ppc_apuinfo_num_alloc = 4;
2870 ppc_apuinfo_list = XNEWVEC (unsigned long, ppc_apuinfo_num_alloc);
2872 else
2874 ppc_apuinfo_num_alloc += 4;
2875 ppc_apuinfo_list = XRESIZEVEC (unsigned long, ppc_apuinfo_list,
2876 ppc_apuinfo_num_alloc);
2879 ppc_apuinfo_list[ppc_apuinfo_num++] = APUID (apu, version);
2881 #undef APUID
2882 #endif
2884 /* Various frobbings of labels and their addresses. */
2886 /* Symbols labelling the current insn. */
2887 struct insn_label_list
2889 struct insn_label_list *next;
2890 symbolS *label;
2893 static struct insn_label_list *insn_labels;
2894 static struct insn_label_list *free_insn_labels;
2896 static void
2897 ppc_record_label (symbolS *sym)
2899 struct insn_label_list *l;
2901 if (free_insn_labels == NULL)
2902 l = XNEW (struct insn_label_list);
2903 else
2905 l = free_insn_labels;
2906 free_insn_labels = l->next;
2909 l->label = sym;
2910 l->next = insn_labels;
2911 insn_labels = l;
2914 static void
2915 ppc_clear_labels (void)
2917 while (insn_labels != NULL)
2919 struct insn_label_list *l = insn_labels;
2920 insn_labels = l->next;
2921 l->next = free_insn_labels;
2922 free_insn_labels = l;
2926 void
2927 ppc_start_line_hook (void)
2929 ppc_clear_labels ();
2932 void
2933 ppc_new_dot_label (symbolS *sym)
2935 ppc_record_label (sym);
2936 #ifdef OBJ_XCOFF
2937 /* Anchor this label to the current csect for relocations. */
2938 symbol_get_tc (sym)->within = ppc_current_csect;
2939 #endif
2942 void
2943 ppc_frob_label (symbolS *sym)
2945 ppc_record_label (sym);
2947 #ifdef OBJ_XCOFF
2948 /* Set the class of a label based on where it is defined. This handles
2949 symbols without suffixes. Also, move the symbol so that it follows
2950 the csect symbol. */
2951 if (ppc_current_csect != (symbolS *) NULL)
2953 if (symbol_get_tc (sym)->symbol_class == -1)
2954 symbol_get_tc (sym)->symbol_class = symbol_get_tc (ppc_current_csect)->symbol_class;
2956 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2957 symbol_append (sym, symbol_get_tc (ppc_current_csect)->within,
2958 &symbol_rootP, &symbol_lastP);
2959 /* Update last csect symbol. */
2960 symbol_get_tc (ppc_current_csect)->within = sym;
2962 /* Some labels like .bs are using within differently.
2963 So avoid changing it, if it's already set. */
2964 if (symbol_get_tc (sym)->within == NULL)
2965 symbol_get_tc (sym)->within = ppc_current_csect;
2967 #endif
2969 #ifdef OBJ_ELF
2970 dwarf2_emit_label (sym);
2971 #endif
2974 /* We need to keep a list of fixups. We can't simply generate them as
2975 we go, because that would require us to first create the frag, and
2976 that would screw up references to ``.''. */
2978 struct ppc_fixup
2980 expressionS exp;
2981 int opindex;
2982 bfd_reloc_code_real_type reloc;
2985 #define MAX_INSN_FIXUPS (5)
2987 /* Return the field size operated on by RELOC, and whether it is
2988 pc-relative in PC_RELATIVE. */
2990 static unsigned int
2991 fixup_size (bfd_reloc_code_real_type reloc, bool *pc_relative)
2993 unsigned int size = 0;
2994 bool pcrel = false;
2996 switch (reloc)
2998 /* This switch statement must handle all BFD_RELOC values
2999 possible in instruction fixups. As is, it handles all
3000 BFD_RELOC values used in bfd/elf64-ppc.c, bfd/elf32-ppc.c,
3001 bfd/coff-rs6000.c and bfd/coff64-rs6000.c.
3002 Overkill since data and marker relocs need not be handled
3003 here, but this way we can be sure a needed fixup reloc isn't
3004 accidentally omitted. */
3005 case BFD_RELOC_PPC_EMB_MRKREF:
3006 case BFD_RELOC_VTABLE_ENTRY:
3007 case BFD_RELOC_VTABLE_INHERIT:
3008 break;
3010 case BFD_RELOC_8:
3011 size = 1;
3012 break;
3014 case BFD_RELOC_16:
3015 case BFD_RELOC_16_BASEREL:
3016 case BFD_RELOC_16_GOTOFF:
3017 case BFD_RELOC_GPREL16:
3018 case BFD_RELOC_HI16:
3019 case BFD_RELOC_HI16_BASEREL:
3020 case BFD_RELOC_HI16_GOTOFF:
3021 case BFD_RELOC_HI16_PLTOFF:
3022 case BFD_RELOC_HI16_S:
3023 case BFD_RELOC_HI16_S_BASEREL:
3024 case BFD_RELOC_HI16_S_GOTOFF:
3025 case BFD_RELOC_HI16_S_PLTOFF:
3026 case BFD_RELOC_LO16:
3027 case BFD_RELOC_LO16_BASEREL:
3028 case BFD_RELOC_LO16_GOTOFF:
3029 case BFD_RELOC_LO16_PLTOFF:
3030 case BFD_RELOC_PPC64_ADDR16_DS:
3031 case BFD_RELOC_PPC64_ADDR16_HIGH:
3032 case BFD_RELOC_PPC64_ADDR16_HIGHA:
3033 case BFD_RELOC_PPC64_ADDR16_HIGHER34:
3034 case BFD_RELOC_PPC64_ADDR16_HIGHERA34:
3035 case BFD_RELOC_PPC64_ADDR16_HIGHEST34:
3036 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:
3037 case BFD_RELOC_PPC64_ADDR16_LO_DS:
3038 case BFD_RELOC_PPC64_DTPREL16_DS:
3039 case BFD_RELOC_PPC64_DTPREL16_HIGH:
3040 case BFD_RELOC_PPC64_DTPREL16_HIGHA:
3041 case BFD_RELOC_PPC64_DTPREL16_HIGHER:
3042 case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
3043 case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
3044 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
3045 case BFD_RELOC_PPC64_DTPREL16_LO_DS:
3046 case BFD_RELOC_PPC64_GOT16_DS:
3047 case BFD_RELOC_PPC64_GOT16_LO_DS:
3048 case BFD_RELOC_PPC64_HIGHER:
3049 case BFD_RELOC_PPC64_HIGHER_S:
3050 case BFD_RELOC_PPC64_HIGHEST:
3051 case BFD_RELOC_PPC64_HIGHEST_S:
3052 case BFD_RELOC_PPC64_PLT16_LO_DS:
3053 case BFD_RELOC_PPC64_PLTGOT16:
3054 case BFD_RELOC_PPC64_PLTGOT16_DS:
3055 case BFD_RELOC_PPC64_PLTGOT16_HA:
3056 case BFD_RELOC_PPC64_PLTGOT16_HI:
3057 case BFD_RELOC_PPC64_PLTGOT16_LO:
3058 case BFD_RELOC_PPC64_PLTGOT16_LO_DS:
3059 case BFD_RELOC_PPC64_SECTOFF_DS:
3060 case BFD_RELOC_PPC64_SECTOFF_LO_DS:
3061 case BFD_RELOC_PPC64_TOC16_DS:
3062 case BFD_RELOC_PPC64_TOC16_HA:
3063 case BFD_RELOC_PPC64_TOC16_HI:
3064 case BFD_RELOC_PPC64_TOC16_LO:
3065 case BFD_RELOC_PPC64_TOC16_LO_DS:
3066 case BFD_RELOC_PPC64_TPREL16_DS:
3067 case BFD_RELOC_PPC64_TPREL16_HIGH:
3068 case BFD_RELOC_PPC64_TPREL16_HIGHA:
3069 case BFD_RELOC_PPC64_TPREL16_HIGHER:
3070 case BFD_RELOC_PPC64_TPREL16_HIGHERA:
3071 case BFD_RELOC_PPC64_TPREL16_HIGHEST:
3072 case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
3073 case BFD_RELOC_PPC64_TPREL16_LO_DS:
3074 #ifdef OBJ_XCOFF
3075 case BFD_RELOC_PPC_BA16:
3076 #endif
3077 case BFD_RELOC_PPC_DTPREL16:
3078 case BFD_RELOC_PPC_DTPREL16_HA:
3079 case BFD_RELOC_PPC_DTPREL16_HI:
3080 case BFD_RELOC_PPC_DTPREL16_LO:
3081 case BFD_RELOC_PPC_EMB_NADDR16:
3082 case BFD_RELOC_PPC_EMB_NADDR16_HA:
3083 case BFD_RELOC_PPC_EMB_NADDR16_HI:
3084 case BFD_RELOC_PPC_EMB_NADDR16_LO:
3085 case BFD_RELOC_PPC_EMB_RELSDA:
3086 case BFD_RELOC_PPC_EMB_RELSEC16:
3087 case BFD_RELOC_PPC_EMB_RELST_LO:
3088 case BFD_RELOC_PPC_EMB_RELST_HI:
3089 case BFD_RELOC_PPC_EMB_RELST_HA:
3090 case BFD_RELOC_PPC_EMB_SDA2I16:
3091 case BFD_RELOC_PPC_EMB_SDA2REL:
3092 case BFD_RELOC_PPC_EMB_SDAI16:
3093 case BFD_RELOC_PPC_GOT_DTPREL16:
3094 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
3095 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
3096 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
3097 case BFD_RELOC_PPC_GOT_TLSGD16:
3098 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
3099 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
3100 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
3101 case BFD_RELOC_PPC_GOT_TLSLD16:
3102 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
3103 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
3104 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
3105 case BFD_RELOC_PPC_GOT_TPREL16:
3106 case BFD_RELOC_PPC_GOT_TPREL16_HA:
3107 case BFD_RELOC_PPC_GOT_TPREL16_HI:
3108 case BFD_RELOC_PPC_GOT_TPREL16_LO:
3109 case BFD_RELOC_PPC_TOC16:
3110 case BFD_RELOC_PPC_TOC16_HI:
3111 case BFD_RELOC_PPC_TOC16_LO:
3112 case BFD_RELOC_PPC_TPREL16:
3113 case BFD_RELOC_PPC_TPREL16_HA:
3114 case BFD_RELOC_PPC_TPREL16_HI:
3115 case BFD_RELOC_PPC_TPREL16_LO:
3116 size = 2;
3117 break;
3119 case BFD_RELOC_16_PCREL:
3120 case BFD_RELOC_HI16_PCREL:
3121 case BFD_RELOC_HI16_S_PCREL:
3122 case BFD_RELOC_LO16_PCREL:
3123 case BFD_RELOC_PPC64_REL16_HIGH:
3124 case BFD_RELOC_PPC64_REL16_HIGHA:
3125 case BFD_RELOC_PPC64_REL16_HIGHER:
3126 case BFD_RELOC_PPC64_REL16_HIGHER34:
3127 case BFD_RELOC_PPC64_REL16_HIGHERA:
3128 case BFD_RELOC_PPC64_REL16_HIGHERA34:
3129 case BFD_RELOC_PPC64_REL16_HIGHEST:
3130 case BFD_RELOC_PPC64_REL16_HIGHEST34:
3131 case BFD_RELOC_PPC64_REL16_HIGHESTA:
3132 case BFD_RELOC_PPC64_REL16_HIGHESTA34:
3133 #ifdef OBJ_XCOFF
3134 case BFD_RELOC_PPC_B16:
3135 #endif
3136 case BFD_RELOC_PPC_VLE_REL8:
3137 size = 2;
3138 pcrel = true;
3139 break;
3141 case BFD_RELOC_32:
3142 case BFD_RELOC_32_PLTOFF:
3143 #ifdef OBJ_XCOFF
3144 case BFD_RELOC_CTOR:
3145 #endif
3146 case BFD_RELOC_PPC64_ENTRY:
3147 case BFD_RELOC_PPC_16DX_HA:
3148 #ifndef OBJ_XCOFF
3149 case BFD_RELOC_PPC_BA16:
3150 #endif
3151 case BFD_RELOC_PPC_BA16_BRNTAKEN:
3152 case BFD_RELOC_PPC_BA16_BRTAKEN:
3153 case BFD_RELOC_PPC_BA26:
3154 case BFD_RELOC_PPC_EMB_BIT_FLD:
3155 case BFD_RELOC_PPC_EMB_NADDR32:
3156 case BFD_RELOC_PPC_EMB_SDA21:
3157 case BFD_RELOC_PPC_TLS:
3158 case BFD_RELOC_PPC_TLSGD:
3159 case BFD_RELOC_PPC_TLSLD:
3160 case BFD_RELOC_PPC_TLSLE:
3161 case BFD_RELOC_PPC_TLSIE:
3162 case BFD_RELOC_PPC_TLSM:
3163 case BFD_RELOC_PPC_TLSML:
3164 case BFD_RELOC_PPC_VLE_HA16A:
3165 case BFD_RELOC_PPC_VLE_HA16D:
3166 case BFD_RELOC_PPC_VLE_HI16A:
3167 case BFD_RELOC_PPC_VLE_HI16D:
3168 case BFD_RELOC_PPC_VLE_LO16A:
3169 case BFD_RELOC_PPC_VLE_LO16D:
3170 case BFD_RELOC_PPC_VLE_SDA21:
3171 case BFD_RELOC_PPC_VLE_SDA21_LO:
3172 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
3173 case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
3174 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
3175 case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
3176 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
3177 case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
3178 case BFD_RELOC_PPC64_TLS_PCREL:
3179 case BFD_RELOC_RVA:
3180 size = 4;
3181 break;
3183 case BFD_RELOC_24_PLT_PCREL:
3184 case BFD_RELOC_32_PCREL:
3185 case BFD_RELOC_32_PLT_PCREL:
3186 case BFD_RELOC_PPC64_REL24_NOTOC:
3187 case BFD_RELOC_PPC64_REL24_P9NOTOC:
3188 #ifndef OBJ_XCOFF
3189 case BFD_RELOC_PPC_B16:
3190 #endif
3191 case BFD_RELOC_PPC_B16_BRNTAKEN:
3192 case BFD_RELOC_PPC_B16_BRTAKEN:
3193 case BFD_RELOC_PPC_B26:
3194 case BFD_RELOC_PPC_LOCAL24PC:
3195 case BFD_RELOC_PPC_REL16DX_HA:
3196 case BFD_RELOC_PPC_VLE_REL15:
3197 case BFD_RELOC_PPC_VLE_REL24:
3198 size = 4;
3199 pcrel = true;
3200 break;
3202 #ifndef OBJ_XCOFF
3203 case BFD_RELOC_CTOR:
3204 #endif
3205 case BFD_RELOC_PPC_COPY:
3206 case BFD_RELOC_PPC_DTPMOD:
3207 case BFD_RELOC_PPC_DTPREL:
3208 case BFD_RELOC_PPC_GLOB_DAT:
3209 case BFD_RELOC_PPC_TPREL:
3210 size = ppc_obj64 ? 8 : 4;
3211 break;
3213 case BFD_RELOC_64:
3214 case BFD_RELOC_64_PLTOFF:
3215 case BFD_RELOC_PPC64_ADDR64_LOCAL:
3216 case BFD_RELOC_PPC64_D28:
3217 case BFD_RELOC_PPC64_D34:
3218 case BFD_RELOC_PPC64_D34_LO:
3219 case BFD_RELOC_PPC64_D34_HI30:
3220 case BFD_RELOC_PPC64_D34_HA30:
3221 case BFD_RELOC_PPC64_TPREL34:
3222 case BFD_RELOC_PPC64_DTPREL34:
3223 case BFD_RELOC_PPC64_TOC:
3224 case BFD_RELOC_PPC64_TLSGD:
3225 case BFD_RELOC_PPC64_TLSLD:
3226 case BFD_RELOC_PPC64_TLSLE:
3227 case BFD_RELOC_PPC64_TLSIE:
3228 case BFD_RELOC_PPC64_TLSM:
3229 case BFD_RELOC_PPC64_TLSML:
3230 size = 8;
3231 break;
3233 case BFD_RELOC_64_PCREL:
3234 case BFD_RELOC_64_PLT_PCREL:
3235 case BFD_RELOC_PPC64_GOT_PCREL34:
3236 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:
3237 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:
3238 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:
3239 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:
3240 case BFD_RELOC_PPC64_PCREL28:
3241 case BFD_RELOC_PPC64_PCREL34:
3242 case BFD_RELOC_PPC64_PLT_PCREL34:
3243 size = 8;
3244 pcrel = true;
3245 break;
3247 default:
3248 abort ();
3251 if (ENABLE_CHECKING)
3253 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
3254 if (reloc_howto != NULL
3255 && (size != bfd_get_reloc_size (reloc_howto)
3256 || pcrel != reloc_howto->pc_relative))
3258 as_bad (_("%s howto doesn't match size/pcrel in gas"),
3259 reloc_howto->name);
3260 abort ();
3263 *pc_relative = pcrel;
3264 return size;
3267 #ifdef OBJ_ELF
3268 /* If we have parsed a call to __tls_get_addr, parse an argument like
3269 (gd0@tlsgd). *STR is the leading parenthesis on entry. If an arg
3270 is successfully parsed, *STR is updated past the trailing
3271 parenthesis and trailing white space, and *TLS_FIX contains the
3272 reloc and arg expression. */
3274 static int
3275 parse_tls_arg (char **str, const expressionS *exp, struct ppc_fixup *tls_fix)
3277 const char *sym_name = S_GET_NAME (exp->X_add_symbol);
3278 if (sym_name[0] == '.')
3279 ++sym_name;
3281 tls_fix->reloc = BFD_RELOC_NONE;
3282 if (strncasecmp (sym_name, "__tls_get_addr", 14) == 0
3283 && (sym_name[14] == 0
3284 || strcasecmp (sym_name + 14, "_desc") == 0
3285 || strcasecmp (sym_name + 14, "_opt") == 0))
3287 char *hold = input_line_pointer;
3288 input_line_pointer = *str + 1;
3289 expression (&tls_fix->exp);
3290 if (tls_fix->exp.X_op == O_symbol)
3292 if (strncasecmp (input_line_pointer, "@tlsgd)", 7) == 0)
3293 tls_fix->reloc = BFD_RELOC_PPC_TLSGD;
3294 else if (strncasecmp (input_line_pointer, "@tlsld)", 7) == 0)
3295 tls_fix->reloc = BFD_RELOC_PPC_TLSLD;
3296 if (tls_fix->reloc != BFD_RELOC_NONE)
3298 input_line_pointer += 7;
3299 SKIP_WHITESPACE ();
3300 *str = input_line_pointer;
3303 input_line_pointer = hold;
3305 return tls_fix->reloc != BFD_RELOC_NONE;
3307 #endif
3309 /* This routine is called for each instruction to be assembled. */
3311 void
3312 md_assemble (char *str)
3314 char *s;
3315 const struct powerpc_opcode *opcode;
3316 uint64_t insn;
3317 const ppc_opindex_t *opindex_ptr;
3318 int need_paren;
3319 int next_opindex;
3320 struct ppc_fixup fixups[MAX_INSN_FIXUPS];
3321 int fc;
3322 char *f;
3323 int addr_mask;
3324 int i;
3325 unsigned int insn_length;
3327 /* Get the opcode. */
3328 for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
3330 if (*s != '\0')
3331 *s++ = '\0';
3333 /* Look up the opcode in the hash table. */
3334 opcode = (const struct powerpc_opcode *) str_hash_find (ppc_hash, str);
3335 if (opcode == (const struct powerpc_opcode *) NULL)
3337 as_bad (_("unrecognized opcode: `%s'"), str);
3338 ppc_clear_labels ();
3339 return;
3342 insn = opcode->opcode;
3343 if (!target_big_endian
3344 && ((insn & ~(1 << 26)) == 46u << 26
3345 || (insn & ~(0xc0 << 1)) == (31u << 26 | 533 << 1)))
3347 /* lmw, stmw, lswi, lswx, stswi, stswx */
3348 as_bad (_("`%s' invalid when little-endian"), str);
3349 ppc_clear_labels ();
3350 return;
3353 str = s;
3354 while (ISSPACE (*str))
3355 ++str;
3357 #ifdef OBJ_XCOFF
3358 /* AIX often generates addis instructions using "addis RT, D(RA)"
3359 format instead of the classic "addis RT, RA, SI" one.
3360 Restore it to the default format as it's the one encoded
3361 in ppc opcodes. */
3362 if (!strcmp (opcode->name, "addis"))
3364 char *rt_e = strchr (str, ',');
3365 if (rt_e != NULL
3366 && strchr (rt_e + 1, ',') == NULL)
3368 char *d_e = strchr (rt_e + 1, '(');
3369 if (d_e != NULL && d_e != rt_e + 1)
3371 char *ra_e = strrchr (d_e + 1, ')');
3372 if (ra_e != NULL && ra_e != d_e + 1)
3373 ppc_xcoff_fixup_addis (rt_e, d_e, ra_e);
3377 #endif
3379 /* PowerPC operands are just expressions. The only real issue is
3380 that a few operand types are optional. If an instruction has
3381 multiple optional operands and one is omitted, then all optional
3382 operands past the first omitted one must also be omitted. */
3383 int num_optional_operands = 0;
3384 int num_optional_provided = 0;
3386 /* Gather the operands. */
3387 need_paren = 0;
3388 next_opindex = 0;
3389 fc = 0;
3390 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
3392 const struct powerpc_operand *operand;
3393 const char *errmsg;
3394 char *hold;
3395 expressionS ex;
3396 char endc;
3398 if (next_opindex == 0)
3399 operand = &powerpc_operands[*opindex_ptr];
3400 else
3402 operand = &powerpc_operands[next_opindex];
3403 next_opindex = 0;
3405 errmsg = NULL;
3407 /* If this is an optional operand, and we are skipping it, just
3408 insert the default value, usually a zero. */
3409 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
3410 && !((operand->flags & PPC_OPERAND_OPTIONAL32) != 0 && ppc_obj64))
3412 if (num_optional_operands == 0)
3414 const ppc_opindex_t *optr;
3415 int total = 0;
3416 int provided = 0;
3417 int omitted;
3419 s = str;
3420 for (optr = opindex_ptr; *optr != 0; optr++)
3422 const struct powerpc_operand *op;
3423 op = &powerpc_operands[*optr];
3425 ++total;
3427 if ((op->flags & PPC_OPERAND_OPTIONAL) != 0
3428 && !((op->flags & PPC_OPERAND_OPTIONAL32) != 0
3429 && ppc_obj64))
3430 ++num_optional_operands;
3432 if (s != NULL && *s != '\0')
3434 ++provided;
3436 /* Look for the start of the next operand. */
3437 if ((op->flags & PPC_OPERAND_PARENS) != 0)
3438 s = strpbrk (s, "(,");
3439 else
3440 s = strchr (s, ',');
3442 if (s != NULL)
3443 ++s;
3446 omitted = total - provided;
3447 num_optional_provided = num_optional_operands - omitted;
3449 if (--num_optional_provided < 0)
3451 uint64_t val = ppc_optional_operand_value (operand, insn, ppc_cpu,
3452 num_optional_provided);
3453 if (operand->insert)
3455 insn = (*operand->insert) (insn, val, ppc_cpu, &errmsg);
3456 if (errmsg != (const char *) NULL)
3457 as_bad ("%s", errmsg);
3459 else if (operand->shift >= 0)
3460 insn |= (val & operand->bitm) << operand->shift;
3461 else
3462 insn |= (val & operand->bitm) >> -operand->shift;
3464 if ((operand->flags & PPC_OPERAND_NEXT) != 0)
3465 next_opindex = *opindex_ptr + 1;
3466 continue;
3470 /* Gather the operand. */
3471 hold = input_line_pointer;
3472 input_line_pointer = str;
3473 cr_operand = ((operand->flags & PPC_OPERAND_CR_BIT) != 0
3474 || (operand->flags & PPC_OPERAND_CR_REG) != 0);
3475 expression (&ex);
3476 cr_operand = false;
3477 str = input_line_pointer;
3478 input_line_pointer = hold;
3480 resolve_register (&ex);
3482 if (ex.X_op == O_illegal)
3483 as_bad (_("illegal operand"));
3484 else if (ex.X_op == O_absent)
3485 as_bad (_("missing operand"));
3486 else if (ex.X_op == O_register)
3488 if ((ex.X_md
3489 & ~operand->flags
3490 & (PPC_OPERAND_GPR | PPC_OPERAND_FPR | PPC_OPERAND_VR
3491 | PPC_OPERAND_VSR | PPC_OPERAND_CR_BIT | PPC_OPERAND_CR_REG
3492 | PPC_OPERAND_SPR | PPC_OPERAND_GQR | PPC_OPERAND_ACC
3493 | PPC_OPERAND_DMR)) != 0
3494 && !((ex.X_md & PPC_OPERAND_GPR) != 0
3495 && ex.X_add_number != 0
3496 && (operand->flags & PPC_OPERAND_GPR_0) != 0))
3497 as_warn (_("invalid register expression"));
3498 insn = ppc_insert_operand (insn, operand, ex.X_add_number,
3499 ppc_cpu, (char *) NULL, 0);
3501 else if (ex.X_op == O_constant
3502 || (ex.X_op == O_big && ex.X_add_number > 0))
3504 uint64_t val;
3505 if (ex.X_op == O_constant)
3507 val = ex.X_add_number;
3508 if (sizeof (ex.X_add_number) < sizeof (val)
3509 && (ex.X_add_number < 0) != ex.X_extrabit)
3510 val = val ^ ((addressT) -1 ^ (uint64_t) -1);
3512 else
3513 val = generic_bignum_to_int64 ();
3514 #ifdef OBJ_ELF
3515 /* Allow @HA, @L, @H on constants. */
3516 char *orig_str = str;
3517 bfd_reloc_code_real_type reloc = ppc_elf_suffix (&str, &ex);
3519 if (ex.X_op == O_constant)
3521 val = ex.X_add_number;
3522 if (sizeof (ex.X_add_number) < sizeof (val)
3523 && (ex.X_add_number < 0) != ex.X_extrabit)
3524 val = val ^ ((addressT) -1 ^ (uint64_t) -1);
3526 if (reloc != BFD_RELOC_NONE)
3527 switch (reloc)
3529 default:
3530 str = orig_str;
3531 break;
3533 case BFD_RELOC_LO16:
3534 val &= 0xffff;
3535 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3536 val = SEX16 (val);
3537 break;
3539 case BFD_RELOC_HI16:
3540 if (REPORT_OVERFLOW_HI && ppc_obj64)
3542 /* PowerPC64 @h is tested for overflow. */
3543 val = val >> 16;
3544 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3546 uint64_t sign = (((uint64_t) -1 >> 16) + 1) >> 1;
3547 val = (val ^ sign) - sign;
3549 break;
3551 /* Fallthru */
3553 case BFD_RELOC_PPC64_ADDR16_HIGH:
3554 val = PPC_HI (val);
3555 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3556 val = SEX16 (val);
3557 break;
3559 case BFD_RELOC_HI16_S:
3560 if (REPORT_OVERFLOW_HI && ppc_obj64)
3562 /* PowerPC64 @ha is tested for overflow. */
3563 val = (val + 0x8000) >> 16;
3564 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3566 uint64_t sign = (((uint64_t) -1 >> 16) + 1) >> 1;
3567 val = (val ^ sign) - sign;
3569 break;
3571 /* Fallthru */
3573 case BFD_RELOC_PPC64_ADDR16_HIGHA:
3574 val = PPC_HA (val);
3575 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3576 val = SEX16 (val);
3577 break;
3579 case BFD_RELOC_PPC64_HIGHER:
3580 val = PPC_HIGHER (val);
3581 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3582 val = SEX16 (val);
3583 break;
3585 case BFD_RELOC_PPC64_HIGHER_S:
3586 val = PPC_HIGHERA (val);
3587 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3588 val = SEX16 (val);
3589 break;
3591 case BFD_RELOC_PPC64_HIGHEST:
3592 val = PPC_HIGHEST (val);
3593 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3594 val = SEX16 (val);
3595 break;
3597 case BFD_RELOC_PPC64_HIGHEST_S:
3598 val = PPC_HIGHESTA (val);
3599 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3600 val = SEX16 (val);
3601 break;
3603 #endif /* OBJ_ELF */
3604 insn = ppc_insert_operand (insn, operand, val, ppc_cpu, NULL, 0);
3606 else
3608 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
3609 #ifdef OBJ_ELF
3610 /* Look for a __tls_get_addr arg using the insane old syntax. */
3611 if (ex.X_op == O_symbol && *str == '(' && fc < MAX_INSN_FIXUPS
3612 && parse_tls_arg (&str, &ex, &fixups[fc]))
3614 fixups[fc].opindex = *opindex_ptr;
3615 ++fc;
3618 if ((reloc = ppc_elf_suffix (&str, &ex)) != BFD_RELOC_NONE)
3620 /* If VLE-mode convert LO/HI/HA relocations. */
3621 if (opcode->flags & PPC_OPCODE_VLE)
3623 uint64_t tmp_insn = insn & opcode->mask;
3625 int use_a_reloc = (tmp_insn == E_OR2I_INSN
3626 || tmp_insn == E_AND2I_DOT_INSN
3627 || tmp_insn == E_OR2IS_INSN
3628 || tmp_insn == E_LI_INSN
3629 || tmp_insn == E_LIS_INSN
3630 || tmp_insn == E_AND2IS_DOT_INSN);
3633 int use_d_reloc = (tmp_insn == E_ADD2I_DOT_INSN
3634 || tmp_insn == E_ADD2IS_INSN
3635 || tmp_insn == E_CMP16I_INSN
3636 || tmp_insn == E_MULL2I_INSN
3637 || tmp_insn == E_CMPL16I_INSN
3638 || tmp_insn == E_CMPH16I_INSN
3639 || tmp_insn == E_CMPHL16I_INSN);
3641 switch (reloc)
3643 default:
3644 break;
3646 case BFD_RELOC_PPC_EMB_SDA21:
3647 reloc = BFD_RELOC_PPC_VLE_SDA21;
3648 break;
3650 case BFD_RELOC_LO16:
3651 if (use_d_reloc)
3652 reloc = BFD_RELOC_PPC_VLE_LO16D;
3653 else if (use_a_reloc)
3654 reloc = BFD_RELOC_PPC_VLE_LO16A;
3655 break;
3657 case BFD_RELOC_HI16:
3658 if (use_d_reloc)
3659 reloc = BFD_RELOC_PPC_VLE_HI16D;
3660 else if (use_a_reloc)
3661 reloc = BFD_RELOC_PPC_VLE_HI16A;
3662 break;
3664 case BFD_RELOC_HI16_S:
3665 if (use_d_reloc)
3666 reloc = BFD_RELOC_PPC_VLE_HA16D;
3667 else if (use_a_reloc)
3668 reloc = BFD_RELOC_PPC_VLE_HA16A;
3669 break;
3671 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
3672 if (use_d_reloc)
3673 reloc = BFD_RELOC_PPC_VLE_SDAREL_LO16D;
3674 break;
3676 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
3677 if (use_d_reloc)
3678 reloc = BFD_RELOC_PPC_VLE_SDAREL_HI16D;
3679 break;
3681 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
3682 if (use_d_reloc)
3683 reloc = BFD_RELOC_PPC_VLE_SDAREL_HA16D;
3684 break;
3688 /* TLS and other tweaks. */
3689 switch (reloc)
3691 default:
3692 break;
3694 case BFD_RELOC_PPC_TLS:
3695 case BFD_RELOC_PPC64_TLS_PCREL:
3696 if (!_bfd_elf_ppc_at_tls_transform (opcode->opcode, 0))
3697 as_bad (_("@tls may not be used with \"%s\" operands"),
3698 opcode->name);
3699 else if (operand->shift != 11)
3700 as_bad (_("@tls may only be used in last operand"));
3701 else
3702 insn = ppc_insert_operand (insn, operand,
3703 ppc_obj64 ? 13 : 2,
3704 ppc_cpu, (char *) NULL, 0);
3705 break;
3707 /* We'll only use the 32 (or 64) bit form of these relocations
3708 in constants. Instructions get the 16 or 34 bit form. */
3709 case BFD_RELOC_PPC_DTPREL:
3710 if (operand->bitm == 0x3ffffffffULL)
3711 reloc = BFD_RELOC_PPC64_DTPREL34;
3712 else
3713 reloc = BFD_RELOC_PPC_DTPREL16;
3714 break;
3716 case BFD_RELOC_PPC_TPREL:
3717 if (operand->bitm == 0x3ffffffffULL)
3718 reloc = BFD_RELOC_PPC64_TPREL34;
3719 else
3720 reloc = BFD_RELOC_PPC_TPREL16;
3721 break;
3723 case BFD_RELOC_PPC64_PCREL34:
3724 if (operand->bitm == 0xfffffffULL)
3726 reloc = BFD_RELOC_PPC64_PCREL28;
3727 break;
3729 /* Fall through. */
3730 case BFD_RELOC_PPC64_GOT_PCREL34:
3731 case BFD_RELOC_PPC64_PLT_PCREL34:
3732 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:
3733 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:
3734 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:
3735 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:
3736 if (operand->bitm != 0x3ffffffffULL
3737 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
3738 as_warn (_("%s unsupported on this instruction"), "@pcrel");
3739 break;
3741 case BFD_RELOC_LO16:
3742 if (operand->bitm == 0x3ffffffffULL
3743 && (operand->flags & PPC_OPERAND_NEGATIVE) == 0)
3744 reloc = BFD_RELOC_PPC64_D34_LO;
3745 else if ((operand->bitm | 0xf) != 0xffff
3746 || operand->shift != 0
3747 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
3748 as_warn (_("%s unsupported on this instruction"), "@l");
3749 break;
3751 case BFD_RELOC_HI16:
3752 if (operand->bitm == 0x3ffffffffULL
3753 && (operand->flags & PPC_OPERAND_NEGATIVE) == 0)
3754 reloc = BFD_RELOC_PPC64_D34_HI30;
3755 else if (operand->bitm != 0xffff
3756 || operand->shift != 0
3757 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
3758 as_warn (_("%s unsupported on this instruction"), "@h");
3759 break;
3761 case BFD_RELOC_HI16_S:
3762 if (operand->bitm == 0x3ffffffffULL
3763 && (operand->flags & PPC_OPERAND_NEGATIVE) == 0)
3764 reloc = BFD_RELOC_PPC64_D34_HA30;
3765 else if (operand->bitm == 0xffff
3766 && operand->shift == (int) PPC_OPSHIFT_INV
3767 && opcode->opcode == (19 << 26) + (2 << 1))
3768 /* addpcis. */
3769 reloc = BFD_RELOC_PPC_16DX_HA;
3770 else if (operand->bitm != 0xffff
3771 || operand->shift != 0
3772 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
3773 as_warn (_("%s unsupported on this instruction"), "@ha");
3776 #endif /* OBJ_ELF */
3777 #ifdef OBJ_XCOFF
3778 reloc = ppc_xcoff_suffix (&str);
3779 #endif /* OBJ_XCOFF */
3781 if (reloc != BFD_RELOC_NONE)
3783 /* Determine a BFD reloc value based on the operand information.
3784 We are only prepared to turn a few of the operands into
3785 relocs. */
3786 else if ((operand->flags & (PPC_OPERAND_RELATIVE
3787 | PPC_OPERAND_ABSOLUTE)) != 0
3788 && operand->bitm == 0x3fffffc
3789 && operand->shift == 0)
3790 reloc = BFD_RELOC_PPC_B26;
3791 else if ((operand->flags & (PPC_OPERAND_RELATIVE
3792 | PPC_OPERAND_ABSOLUTE)) != 0
3793 && operand->bitm == 0xfffc
3794 && operand->shift == 0)
3795 reloc = BFD_RELOC_PPC_B16;
3796 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3797 && operand->bitm == 0x1fe
3798 && operand->shift == -1)
3799 reloc = BFD_RELOC_PPC_VLE_REL8;
3800 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3801 && operand->bitm == 0xfffe
3802 && operand->shift == 0)
3803 reloc = BFD_RELOC_PPC_VLE_REL15;
3804 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3805 && operand->bitm == 0x1fffffe
3806 && operand->shift == 0)
3807 reloc = BFD_RELOC_PPC_VLE_REL24;
3808 else if ((operand->flags & PPC_OPERAND_NEGATIVE) == 0
3809 && (operand->bitm & 0xfff0) == 0xfff0
3810 && operand->shift == 0)
3812 reloc = BFD_RELOC_16;
3813 #if defined OBJ_XCOFF || defined OBJ_ELF
3814 /* Note: the symbol may be not yet defined. */
3815 if ((operand->flags & PPC_OPERAND_PARENS) != 0
3816 && ppc_is_toc_sym (ex.X_add_symbol))
3818 reloc = BFD_RELOC_PPC_TOC16;
3819 #ifdef OBJ_ELF
3820 as_warn (_("assuming %s on symbol"),
3821 ppc_obj64 ? "@toc" : "@xgot");
3822 #endif
3824 #endif
3826 else if (operand->bitm == 0x3ffffffffULL)
3827 reloc = BFD_RELOC_PPC64_D34;
3828 else if (operand->bitm == 0xfffffffULL)
3829 reloc = BFD_RELOC_PPC64_D28;
3831 /* For the absolute forms of branches, convert the PC
3832 relative form back into the absolute. */
3833 if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
3835 switch (reloc)
3837 case BFD_RELOC_PPC_B26:
3838 reloc = BFD_RELOC_PPC_BA26;
3839 break;
3840 case BFD_RELOC_PPC_B16:
3841 reloc = BFD_RELOC_PPC_BA16;
3842 break;
3843 #ifdef OBJ_ELF
3844 case BFD_RELOC_PPC_B16_BRTAKEN:
3845 reloc = BFD_RELOC_PPC_BA16_BRTAKEN;
3846 break;
3847 case BFD_RELOC_PPC_B16_BRNTAKEN:
3848 reloc = BFD_RELOC_PPC_BA16_BRNTAKEN;
3849 break;
3850 #endif
3851 default:
3852 break;
3856 #ifdef OBJ_ELF
3857 switch (reloc)
3859 case BFD_RELOC_PPC_TOC16:
3860 toc_reloc_types |= has_small_toc_reloc;
3861 break;
3862 case BFD_RELOC_PPC64_TOC16_LO:
3863 case BFD_RELOC_PPC64_TOC16_HI:
3864 case BFD_RELOC_PPC64_TOC16_HA:
3865 toc_reloc_types |= has_large_toc_reloc;
3866 break;
3867 default:
3868 break;
3871 if (ppc_obj64
3872 && (operand->flags & (PPC_OPERAND_DS | PPC_OPERAND_DQ)) != 0)
3874 switch (reloc)
3876 case BFD_RELOC_16:
3877 reloc = BFD_RELOC_PPC64_ADDR16_DS;
3878 break;
3880 case BFD_RELOC_LO16:
3881 reloc = BFD_RELOC_PPC64_ADDR16_LO_DS;
3882 break;
3884 case BFD_RELOC_16_GOTOFF:
3885 reloc = BFD_RELOC_PPC64_GOT16_DS;
3886 break;
3888 case BFD_RELOC_LO16_GOTOFF:
3889 reloc = BFD_RELOC_PPC64_GOT16_LO_DS;
3890 break;
3892 case BFD_RELOC_LO16_PLTOFF:
3893 reloc = BFD_RELOC_PPC64_PLT16_LO_DS;
3894 break;
3896 case BFD_RELOC_16_BASEREL:
3897 reloc = BFD_RELOC_PPC64_SECTOFF_DS;
3898 break;
3900 case BFD_RELOC_LO16_BASEREL:
3901 reloc = BFD_RELOC_PPC64_SECTOFF_LO_DS;
3902 break;
3904 case BFD_RELOC_PPC_TOC16:
3905 reloc = BFD_RELOC_PPC64_TOC16_DS;
3906 break;
3908 case BFD_RELOC_PPC64_TOC16_LO:
3909 reloc = BFD_RELOC_PPC64_TOC16_LO_DS;
3910 break;
3912 case BFD_RELOC_PPC64_PLTGOT16:
3913 reloc = BFD_RELOC_PPC64_PLTGOT16_DS;
3914 break;
3916 case BFD_RELOC_PPC64_PLTGOT16_LO:
3917 reloc = BFD_RELOC_PPC64_PLTGOT16_LO_DS;
3918 break;
3920 case BFD_RELOC_PPC_DTPREL16:
3921 reloc = BFD_RELOC_PPC64_DTPREL16_DS;
3922 break;
3924 case BFD_RELOC_PPC_DTPREL16_LO:
3925 reloc = BFD_RELOC_PPC64_DTPREL16_LO_DS;
3926 break;
3928 case BFD_RELOC_PPC_TPREL16:
3929 reloc = BFD_RELOC_PPC64_TPREL16_DS;
3930 break;
3932 case BFD_RELOC_PPC_TPREL16_LO:
3933 reloc = BFD_RELOC_PPC64_TPREL16_LO_DS;
3934 break;
3936 case BFD_RELOC_PPC_GOT_DTPREL16:
3937 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
3938 case BFD_RELOC_PPC_GOT_TPREL16:
3939 case BFD_RELOC_PPC_GOT_TPREL16_LO:
3940 break;
3942 default:
3943 as_bad (_("unsupported relocation for DS offset field"));
3944 break;
3948 /* Look for a __tls_get_addr arg after any __tls_get_addr
3949 modifiers like @plt. This fixup must be emitted before
3950 the usual call fixup. */
3951 if (ex.X_op == O_symbol && *str == '(' && fc < MAX_INSN_FIXUPS
3952 && parse_tls_arg (&str, &ex, &fixups[fc]))
3954 fixups[fc].opindex = *opindex_ptr;
3955 ++fc;
3957 #endif
3959 /* We need to generate a fixup for this expression. */
3960 if (fc >= MAX_INSN_FIXUPS)
3961 as_fatal (_("too many fixups"));
3962 fixups[fc].exp = ex;
3963 fixups[fc].opindex = *opindex_ptr;
3964 fixups[fc].reloc = reloc;
3965 ++fc;
3968 if (need_paren)
3970 endc = ')';
3971 need_paren = 0;
3972 /* If expecting more operands, then we want to see "),". */
3973 if (*str == endc && opindex_ptr[1] != 0)
3976 ++str;
3977 while (ISSPACE (*str));
3978 endc = ',';
3981 else if ((operand->flags & PPC_OPERAND_PARENS) != 0)
3982 endc = '(';
3983 else
3984 endc = ',';
3986 /* The call to expression should have advanced str past any
3987 whitespace. */
3988 if (*str == endc)
3990 ++str;
3991 if (endc == '(')
3992 need_paren = 1;
3994 else if (*str != '\0')
3996 as_bad (_("syntax error; found `%c', expected `%c'"), *str, endc);
3997 break;
3999 else if (endc == ')')
4001 as_bad (_("syntax error; end of line, expected `%c'"), endc);
4002 break;
4006 while (ISSPACE (*str))
4007 ++str;
4009 if (*str != '\0')
4010 as_bad (_("junk at end of line: `%s'"), str);
4012 #ifdef OBJ_ELF
4013 /* Do we need/want an APUinfo section? */
4014 if ((ppc_cpu & (PPC_OPCODE_E500 | PPC_OPCODE_E500MC | PPC_OPCODE_VLE)) != 0
4015 && !ppc_obj64)
4017 /* These are all version "1". */
4018 if (opcode->flags & PPC_OPCODE_SPE)
4019 ppc_apuinfo_section_add (PPC_APUINFO_SPE, 1);
4020 if (opcode->flags & PPC_OPCODE_ISEL)
4021 ppc_apuinfo_section_add (PPC_APUINFO_ISEL, 1);
4022 if (opcode->flags & PPC_OPCODE_EFS)
4023 ppc_apuinfo_section_add (PPC_APUINFO_EFS, 1);
4024 if (opcode->flags & PPC_OPCODE_BRLOCK)
4025 ppc_apuinfo_section_add (PPC_APUINFO_BRLOCK, 1);
4026 if (opcode->flags & PPC_OPCODE_PMR)
4027 ppc_apuinfo_section_add (PPC_APUINFO_PMR, 1);
4028 if (opcode->flags & PPC_OPCODE_CACHELCK)
4029 ppc_apuinfo_section_add (PPC_APUINFO_CACHELCK, 1);
4030 if (opcode->flags & PPC_OPCODE_RFMCI)
4031 ppc_apuinfo_section_add (PPC_APUINFO_RFMCI, 1);
4032 /* Only set the VLE flag if the instruction has been pulled via
4033 the VLE instruction set. This way the flag is guaranteed to
4034 be set for VLE-only instructions or for VLE-only processors,
4035 however it'll remain clear for dual-mode instructions on
4036 dual-mode and, more importantly, standard-mode processors. */
4037 if (ppc_cpu & opcode->flags & PPC_OPCODE_VLE)
4039 ppc_apuinfo_section_add (PPC_APUINFO_VLE, 1);
4040 if (elf_section_data (now_seg) != NULL)
4041 elf_section_data (now_seg)->this_hdr.sh_flags |= SHF_PPC_VLE;
4044 #endif
4046 /* Write out the instruction. */
4048 addr_mask = 3;
4049 if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
4050 /* All instructions can start on a 2 byte boundary for VLE. */
4051 addr_mask = 1;
4053 if (frag_now->insn_addr != addr_mask)
4055 /* Don't emit instructions to a frag started for data, or for a
4056 CPU differing in VLE mode. Data is allowed to be misaligned,
4057 and it's possible to start a new frag in the middle of
4058 misaligned data. */
4059 frag_wane (frag_now);
4060 frag_new (0);
4063 /* Check that insns within the frag are aligned. ppc_frag_check
4064 will ensure that the frag start address is aligned. */
4065 if ((frag_now_fix () & addr_mask) != 0)
4066 as_bad (_("instruction address is not a multiple of %d"), addr_mask + 1);
4068 /* Differentiate between two, four, and eight byte insns. */
4069 insn_length = 4;
4070 if ((ppc_cpu & PPC_OPCODE_VLE) != 0 && PPC_OP_SE_VLE (insn))
4071 insn_length = 2;
4072 else if (PPC_PREFIX_P (insn))
4074 struct insn_label_list *l;
4076 insn_length = 8;
4078 /* 8-byte prefix instructions are not allowed to cross 64-byte
4079 boundaries. */
4080 frag_align_code (6, 4);
4081 record_alignment (now_seg, 6);
4082 #ifdef OBJ_XCOFF
4083 /* Update alignment of the containing csect. */
4084 if (symbol_get_tc (ppc_current_csect)->align < 6)
4085 symbol_get_tc (ppc_current_csect)->align = 6;
4086 #endif
4088 /* Update "dot" in any expressions used by this instruction, and
4089 a label attached to the instruction. By "attached" we mean
4090 on the same source line as the instruction and without any
4091 intervening semicolons. */
4092 dot_value = frag_now_fix ();
4093 dot_frag = frag_now;
4094 for (l = insn_labels; l != NULL; l = l->next)
4096 symbol_set_frag (l->label, dot_frag);
4097 S_SET_VALUE (l->label, dot_value);
4101 ppc_clear_labels ();
4103 f = frag_more (insn_length);
4104 frag_now->insn_addr = addr_mask;
4106 /* The prefix part of an 8-byte instruction always occupies the lower
4107 addressed word in a doubleword, regardless of endianness. */
4108 if (insn_length == 8
4109 && (sizeof (insn) > sizeof (valueT) || !target_big_endian))
4111 md_number_to_chars (f, PPC_GET_PREFIX (insn), 4);
4112 md_number_to_chars (f + 4, PPC_GET_SUFFIX (insn), 4);
4114 else
4115 md_number_to_chars (f, insn, insn_length);
4117 last_insn = insn;
4118 last_seg = now_seg;
4119 last_subseg = now_subseg;
4121 #ifdef OBJ_ELF
4122 dwarf2_emit_insn (insn_length);
4123 #endif
4125 /* Create any fixups. */
4126 for (i = 0; i < fc; i++)
4128 fixS *fixP;
4129 if (fixups[i].reloc != BFD_RELOC_NONE)
4131 bool pcrel;
4132 unsigned int size = fixup_size (fixups[i].reloc, &pcrel);
4133 int offset = target_big_endian ? (insn_length - size) : 0;
4135 fixP = fix_new_exp (frag_now,
4136 f - frag_now->fr_literal + offset,
4137 size,
4138 &fixups[i].exp,
4139 pcrel,
4140 fixups[i].reloc);
4142 else
4144 const struct powerpc_operand *operand;
4146 operand = &powerpc_operands[fixups[i].opindex];
4147 fixP = fix_new_exp (frag_now,
4148 f - frag_now->fr_literal,
4149 insn_length,
4150 &fixups[i].exp,
4151 (operand->flags & PPC_OPERAND_RELATIVE) != 0,
4152 BFD_RELOC_NONE);
4154 fixP->fx_pcrel_adjust = fixups[i].opindex;
4158 #ifdef OBJ_ELF
4159 /* For ELF, add support for SHT_ORDERED. */
4162 ppc_section_type (char *str, size_t len)
4164 if (len == 7 && startswith (str, "ordered"))
4165 return SHT_ORDERED;
4167 return -1;
4171 ppc_section_flags (flagword flags, bfd_vma attr ATTRIBUTE_UNUSED, int type)
4173 if (type == SHT_ORDERED)
4174 flags |= SEC_ALLOC | SEC_LOAD | SEC_SORT_ENTRIES;
4176 return flags;
4179 bfd_vma
4180 ppc_elf_section_letter (int letter, const char **ptrmsg)
4182 if (letter == 'v')
4183 return SHF_PPC_VLE;
4185 *ptrmsg = _("bad .section directive: want a,e,v,w,x,M,S,G,T in string");
4186 return -1;
4188 #endif /* OBJ_ELF */
4191 /* Pseudo-op handling. */
4193 /* The .byte pseudo-op. This is similar to the normal .byte
4194 pseudo-op, but it can also take a single ASCII string. */
4196 static void
4197 ppc_byte (int ignore ATTRIBUTE_UNUSED)
4199 int count = 0;
4201 if (*input_line_pointer != '\"')
4203 cons (1);
4204 return;
4207 /* Gather characters. A real double quote is doubled. Unusual
4208 characters are not permitted. */
4209 ++input_line_pointer;
4210 while (1)
4212 char c;
4214 c = *input_line_pointer++;
4216 if (c == '\"')
4218 if (*input_line_pointer != '\"')
4219 break;
4220 ++input_line_pointer;
4223 FRAG_APPEND_1_CHAR (c);
4224 ++count;
4227 if (warn_476 && count != 0 && (now_seg->flags & SEC_CODE) != 0)
4228 as_warn (_("data in executable section"));
4229 demand_empty_rest_of_line ();
4232 #ifdef OBJ_XCOFF
4234 /* XCOFF specific pseudo-op handling. */
4236 /* This is set if we are creating a .stabx symbol, since we don't want
4237 to handle symbol suffixes for such symbols. */
4238 static bool ppc_stab_symbol;
4240 /* Retrieve the visiblity input for pseudo-ops having ones. */
4241 static unsigned short
4242 ppc_xcoff_get_visibility (void) {
4243 SKIP_WHITESPACE();
4245 if (startswith (input_line_pointer, "exported"))
4247 input_line_pointer += 8;
4248 return SYM_V_EXPORTED;
4251 if (startswith (input_line_pointer, "hidden"))
4253 input_line_pointer += 6;
4254 return SYM_V_HIDDEN;
4257 if (startswith (input_line_pointer, "internal"))
4259 input_line_pointer += 8;
4260 return SYM_V_INTERNAL;
4263 if (startswith (input_line_pointer, "protected"))
4265 input_line_pointer += 9;
4266 return SYM_V_PROTECTED;
4269 return 0;
4272 /* Retrieve visiblity using GNU syntax. */
4273 static void ppc_GNU_visibility (int visibility) {
4274 int c;
4275 char *name;
4276 symbolS *symbolP;
4277 coff_symbol_type *coffsym;
4281 if ((name = read_symbol_name ()) == NULL)
4282 break;
4283 symbolP = symbol_find_or_make (name);
4284 free (name);
4285 coffsym = coffsymbol (symbol_get_bfdsym (symbolP));
4287 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4288 coffsym->native->u.syment.n_type |= visibility;
4290 c = *input_line_pointer;
4291 if (c == ',')
4293 input_line_pointer ++;
4295 SKIP_WHITESPACE ();
4297 if (*input_line_pointer == '\n')
4298 c = '\n';
4301 while (c == ',');
4303 demand_empty_rest_of_line ();
4306 /* The .comm and .lcomm pseudo-ops for XCOFF. XCOFF puts common
4307 symbols in the .bss segment as though they were local common
4308 symbols, and uses a different smclas. The native Aix 4.3.3 assembler
4309 aligns .comm and .lcomm to 4 bytes.
4310 Symbols having a XMC_UL storage class are uninialized thread-local
4311 data. */
4313 static void
4314 ppc_comm (int lcomm)
4316 asection *current_seg = now_seg;
4317 subsegT current_subseg = now_subseg;
4318 char *name;
4319 char endc;
4320 char *end_name;
4321 offsetT size;
4322 offsetT align;
4323 symbolS *lcomm_sym = NULL;
4324 symbolS *sym;
4325 char *pfrag;
4326 unsigned short visibility = 0;
4327 struct ppc_xcoff_section *section;
4329 endc = get_symbol_name (&name);
4330 end_name = input_line_pointer;
4331 (void) restore_line_pointer (endc);
4333 if (*input_line_pointer != ',')
4335 as_bad (_("missing size"));
4336 ignore_rest_of_line ();
4337 return;
4339 ++input_line_pointer;
4341 size = get_absolute_expression ();
4342 if (size < 0)
4344 as_bad (_("negative size"));
4345 ignore_rest_of_line ();
4346 return;
4349 if (! lcomm)
4351 /* The third argument to .comm is the alignment. */
4352 if (*input_line_pointer != ',')
4353 align = 2;
4354 else
4356 ++input_line_pointer;
4357 align = get_absolute_expression ();
4358 if (align <= 0)
4360 as_warn (_("ignoring bad alignment"));
4361 align = 2;
4364 /* The fourth argument to .comm is the visibility. */
4365 if (*input_line_pointer == ',')
4367 input_line_pointer++;
4368 visibility = ppc_xcoff_get_visibility ();
4369 if (!visibility)
4371 as_bad (_("Unknown visibility field in .comm"));
4372 ignore_rest_of_line ();
4373 return;
4378 else
4380 char *lcomm_name;
4381 char lcomm_endc;
4383 /* The third argument to .lcomm appears to be the real local
4384 common symbol to create. References to the symbol named in
4385 the first argument are turned into references to the third
4386 argument. */
4387 if (*input_line_pointer != ',')
4389 as_bad (_("missing real symbol name"));
4390 ignore_rest_of_line ();
4391 return;
4393 ++input_line_pointer;
4395 lcomm_endc = get_symbol_name (&lcomm_name);
4397 lcomm_sym = symbol_find_or_make (lcomm_name);
4399 (void) restore_line_pointer (lcomm_endc);
4401 /* The fourth argument to .lcomm is the alignment. */
4402 if (*input_line_pointer != ',')
4404 if (size <= 4)
4405 align = 2;
4406 else
4407 align = 3;
4409 else
4411 ++input_line_pointer;
4412 align = get_absolute_expression ();
4413 if (align <= 0)
4415 as_warn (_("ignoring bad alignment"));
4416 align = 2;
4421 *end_name = '\0';
4422 sym = symbol_find_or_make (name);
4423 *end_name = endc;
4425 if (S_IS_DEFINED (sym)
4426 || S_GET_VALUE (sym) != 0)
4428 as_bad (_("attempt to redefine symbol"));
4429 ignore_rest_of_line ();
4430 return;
4433 if (symbol_get_tc (sym)->symbol_class == XMC_UL
4434 || (lcomm && symbol_get_tc (lcomm_sym)->symbol_class == XMC_UL))
4436 section = &ppc_xcoff_tbss_section;
4437 if (!ppc_xcoff_section_is_initialized (section))
4439 ppc_init_xcoff_section (section, subseg_new (".tbss", 0));
4440 bfd_set_section_flags (section->segment,
4441 SEC_ALLOC | SEC_THREAD_LOCAL);
4442 seg_info (section->segment)->bss = 1;
4445 else
4446 section = &ppc_xcoff_bss_section;
4448 record_alignment (section->segment, align);
4450 if (! lcomm
4451 || ! S_IS_DEFINED (lcomm_sym))
4453 symbolS *def_sym;
4454 offsetT def_size;
4456 if (! lcomm)
4458 def_sym = sym;
4459 def_size = size;
4460 S_SET_EXTERNAL (sym);
4462 else
4464 symbol_get_tc (lcomm_sym)->output = 1;
4465 def_sym = lcomm_sym;
4466 def_size = 0;
4469 subseg_set (section->segment, 1);
4470 frag_align (align, 0, 0);
4472 symbol_set_frag (def_sym, frag_now);
4473 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, def_sym,
4474 def_size, (char *) NULL);
4475 *pfrag = 0;
4476 S_SET_SEGMENT (def_sym, section->segment);
4477 symbol_get_tc (def_sym)->align = align;
4479 else if (lcomm)
4481 /* Align the size of lcomm_sym. */
4482 symbol_get_frag (lcomm_sym)->fr_offset =
4483 ((symbol_get_frag (lcomm_sym)->fr_offset + (1 << align) - 1)
4484 &~ ((1 << align) - 1));
4485 if (align > symbol_get_tc (lcomm_sym)->align)
4486 symbol_get_tc (lcomm_sym)->align = align;
4489 if (lcomm)
4491 /* Make sym an offset from lcomm_sym. */
4492 S_SET_SEGMENT (sym, section->segment);
4493 symbol_set_frag (sym, symbol_get_frag (lcomm_sym));
4494 S_SET_VALUE (sym, symbol_get_frag (lcomm_sym)->fr_offset);
4495 symbol_get_frag (lcomm_sym)->fr_offset += size;
4498 if (!lcomm && visibility)
4500 /* Add visibility to .comm symbol. */
4501 coff_symbol_type *coffsym = coffsymbol (symbol_get_bfdsym (sym));
4502 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4503 coffsym->native->u.syment.n_type |= visibility;
4506 subseg_set (current_seg, current_subseg);
4508 demand_empty_rest_of_line ();
4511 /* The .csect pseudo-op. This switches us into a different
4512 subsegment. The first argument is a symbol whose value is the
4513 start of the .csect. In COFF, csect symbols get special aux
4514 entries defined by the x_csect field of union internal_auxent. The
4515 optional second argument is the alignment (the default is 2). */
4517 static void
4518 ppc_csect (int ignore ATTRIBUTE_UNUSED)
4520 char *name;
4521 char endc;
4522 symbolS *sym;
4523 offsetT align;
4525 endc = get_symbol_name (&name);
4527 sym = symbol_find_or_make (name);
4529 (void) restore_line_pointer (endc);
4531 if (S_GET_NAME (sym)[0] == '\0')
4533 /* An unnamed csect is assumed to be [PR]. */
4534 symbol_get_tc (sym)->symbol_class = XMC_PR;
4537 align = 2;
4538 if (*input_line_pointer == ',')
4540 ++input_line_pointer;
4541 align = get_absolute_expression ();
4544 ppc_change_csect (sym, align);
4546 demand_empty_rest_of_line ();
4549 /* Change to a different csect. */
4551 static void
4552 ppc_change_csect (symbolS *sym, offsetT align)
4554 if (S_IS_DEFINED (sym))
4555 subseg_set (S_GET_SEGMENT (sym), symbol_get_tc (sym)->subseg);
4556 else
4558 struct ppc_xcoff_section *section;
4559 int after_toc;
4560 int hold_chunksize;
4561 symbolS *list;
4562 int is_code;
4563 segT sec;
4565 /* This is a new csect. We need to look at the symbol class to
4566 figure out whether it should go in the text section or the
4567 data section. */
4568 after_toc = 0;
4569 is_code = 0;
4570 switch (symbol_get_tc (sym)->symbol_class)
4572 case XMC_PR:
4573 case XMC_RO:
4574 case XMC_DB:
4575 case XMC_GL:
4576 case XMC_XO:
4577 case XMC_SV:
4578 case XMC_TI:
4579 case XMC_TB:
4580 section = &ppc_xcoff_text_section;
4581 is_code = 1;
4582 break;
4583 case XMC_RW:
4584 case XMC_TC0:
4585 case XMC_TC:
4586 case XMC_TE:
4587 case XMC_DS:
4588 case XMC_UA:
4589 case XMC_UC:
4590 section = &ppc_xcoff_data_section;
4591 if (ppc_toc_csect != NULL
4592 && (symbol_get_tc (ppc_toc_csect)->subseg + 1
4593 == section->next_subsegment))
4594 after_toc = 1;
4595 break;
4596 case XMC_BS:
4597 section = &ppc_xcoff_bss_section;
4598 break;
4599 case XMC_TL:
4600 section = &ppc_xcoff_tdata_section;
4601 /* Create .tdata section if not yet done. */
4602 if (!ppc_xcoff_section_is_initialized (section))
4604 ppc_init_xcoff_section (section, subseg_new (".tdata", 0));
4605 bfd_set_section_flags (section->segment, SEC_ALLOC
4606 | SEC_LOAD | SEC_RELOC | SEC_DATA
4607 | SEC_THREAD_LOCAL);
4609 break;
4610 case XMC_UL:
4611 section = &ppc_xcoff_tbss_section;
4612 /* Create .tbss section if not yet done. */
4613 if (!ppc_xcoff_section_is_initialized (section))
4615 ppc_init_xcoff_section (section, subseg_new (".tbss", 0));
4616 bfd_set_section_flags (section->segment, SEC_ALLOC |
4617 SEC_THREAD_LOCAL);
4618 seg_info (section->segment)->bss = 1;
4620 break;
4621 default:
4622 abort ();
4625 S_SET_SEGMENT (sym, section->segment);
4626 symbol_get_tc (sym)->subseg = section->next_subsegment;
4627 ++section->next_subsegment;
4629 /* We set the obstack chunk size to a small value before
4630 changing subsegments, so that we don't use a lot of memory
4631 space for what may be a small section. */
4632 hold_chunksize = chunksize;
4633 chunksize = 64;
4635 sec = subseg_new (segment_name (S_GET_SEGMENT (sym)),
4636 symbol_get_tc (sym)->subseg);
4638 chunksize = hold_chunksize;
4640 if (after_toc)
4641 ppc_after_toc_frag = frag_now;
4643 record_alignment (sec, align);
4644 if (is_code)
4645 frag_align_code (align, 0);
4646 else
4647 frag_align (align, 0, 0);
4649 symbol_set_frag (sym, frag_now);
4650 S_SET_VALUE (sym, (valueT) frag_now_fix ());
4652 symbol_get_tc (sym)->align = align;
4653 symbol_get_tc (sym)->output = 1;
4654 symbol_get_tc (sym)->within = sym;
4656 for (list = section->csects;
4657 symbol_get_tc (list)->next != (symbolS *) NULL;
4658 list = symbol_get_tc (list)->next)
4660 symbol_get_tc (list)->next = sym;
4662 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
4663 symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
4664 &symbol_lastP);
4667 ppc_current_csect = sym;
4670 static void
4671 ppc_change_debug_section (unsigned int idx, subsegT subseg)
4673 segT sec;
4674 flagword oldflags;
4675 const struct xcoff_dwsect_name *dw = &xcoff_dwsect_names[idx];
4677 sec = subseg_new (dw->xcoff_name, subseg);
4678 oldflags = bfd_section_flags (sec);
4679 if (oldflags == SEC_NO_FLAGS)
4681 /* Just created section. */
4682 gas_assert (dw_sections[idx].sect == NULL);
4684 bfd_set_section_flags (sec, SEC_DEBUGGING);
4685 bfd_set_section_alignment (sec, 0);
4686 dw_sections[idx].sect = sec;
4689 /* Not anymore in a csect. */
4690 ppc_current_csect = NULL;
4693 /* The .dwsect pseudo-op. Defines a DWARF section. Syntax is:
4694 .dwsect flag [, opt-label ]
4697 static void
4698 ppc_dwsect (int ignore ATTRIBUTE_UNUSED)
4700 valueT flag;
4701 symbolS *opt_label;
4702 const struct xcoff_dwsect_name *dw;
4703 struct dw_subsection *subseg;
4704 struct dw_section *dws;
4705 int i;
4707 /* Find section. */
4708 flag = get_absolute_expression ();
4709 dw = NULL;
4710 for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
4711 if (xcoff_dwsect_names[i].flag == flag)
4713 dw = &xcoff_dwsect_names[i];
4714 break;
4717 /* Parse opt-label. */
4718 if (*input_line_pointer == ',')
4720 char *label;
4721 char c;
4723 ++input_line_pointer;
4725 c = get_symbol_name (&label);
4726 opt_label = symbol_find_or_make (label);
4727 (void) restore_line_pointer (c);
4729 else
4730 opt_label = NULL;
4732 demand_empty_rest_of_line ();
4734 /* Return now in case of unknown subsection. */
4735 if (dw == NULL)
4737 as_bad (_("no known dwarf XCOFF section for flag 0x%08x\n"),
4738 (unsigned)flag);
4739 return;
4742 /* Find the subsection. */
4743 dws = &dw_sections[i];
4744 subseg = NULL;
4745 if (opt_label != NULL && S_IS_DEFINED (opt_label))
4747 /* Sanity check (note that in theory S_GET_SEGMENT mustn't be null). */
4748 if (dws->sect == NULL || S_GET_SEGMENT (opt_label) != dws->sect)
4750 as_bad (_("label %s was not defined in this dwarf section"),
4751 S_GET_NAME (opt_label));
4752 subseg = dws->anon_subseg;
4753 opt_label = NULL;
4755 else
4756 subseg = symbol_get_tc (opt_label)->u.dw;
4759 if (subseg != NULL)
4761 /* Switch to the subsection. */
4762 ppc_change_debug_section (i, subseg->subseg);
4764 else
4766 /* Create a new dw subsection. */
4767 subseg = XCNEW (struct dw_subsection);
4769 if (opt_label == NULL)
4771 /* The anonymous one. */
4772 subseg->subseg = 0;
4773 subseg->link = NULL;
4774 dws->anon_subseg = subseg;
4776 else
4778 /* A named one. */
4779 if (dws->list_subseg != NULL)
4780 subseg->subseg = dws->list_subseg->subseg + 1;
4781 else
4782 subseg->subseg = 1;
4784 subseg->link = dws->list_subseg;
4785 dws->list_subseg = subseg;
4786 symbol_get_tc (opt_label)->u.dw = subseg;
4789 ppc_change_debug_section (i, subseg->subseg);
4791 if (dw->def_size)
4793 /* Add the length field. */
4794 expressionS *exp = &subseg->end_exp;
4795 int sz;
4797 if (opt_label != NULL)
4798 symbol_set_value_now (opt_label);
4800 /* Add the length field. Note that according to the AIX assembler
4801 manual, the size of the length field is 4 for powerpc32 but
4802 12 for powerpc64. */
4803 if (ppc_obj64)
4805 /* Write the 64bit marker. */
4806 md_number_to_chars (frag_more (4), -1, 4);
4809 exp->X_op = O_subtract;
4810 exp->X_op_symbol = symbol_temp_new_now ();
4811 exp->X_add_symbol = symbol_temp_make ();
4813 sz = ppc_obj64 ? 8 : 4;
4814 exp->X_add_number = -sz;
4815 emit_expr (exp, sz);
4820 /* This function handles the .text and .data pseudo-ops. These
4821 pseudo-ops aren't really used by XCOFF; we implement them for the
4822 convenience of people who aren't used to XCOFF. */
4824 static void
4825 ppc_section (int type)
4827 const char *name;
4828 symbolS *sym;
4830 if (type == 't')
4831 name = ".text[PR]";
4832 else if (type == 'd')
4833 name = ".data[RW]";
4834 else
4835 abort ();
4837 sym = symbol_find_or_make (name);
4839 ppc_change_csect (sym, 2);
4841 demand_empty_rest_of_line ();
4844 /* This function handles the .section pseudo-op. This is mostly to
4845 give an error, since XCOFF only supports .text, .data and .bss, but
4846 we do permit the user to name the text or data section. */
4848 static void
4849 ppc_named_section (int ignore ATTRIBUTE_UNUSED)
4851 char *user_name;
4852 const char *real_name;
4853 char c;
4854 symbolS *sym;
4856 c = get_symbol_name (&user_name);
4858 if (strcmp (user_name, ".text") == 0)
4859 real_name = ".text[PR]";
4860 else if (strcmp (user_name, ".data") == 0)
4861 real_name = ".data[RW]";
4862 else
4864 as_bad (_("the XCOFF file format does not support arbitrary sections"));
4865 (void) restore_line_pointer (c);
4866 ignore_rest_of_line ();
4867 return;
4870 (void) restore_line_pointer (c);
4872 sym = symbol_find_or_make (real_name);
4874 ppc_change_csect (sym, 2);
4876 demand_empty_rest_of_line ();
4879 /* The .extern pseudo-op. We create an undefined symbol. */
4881 static void
4882 ppc_extern (int ignore ATTRIBUTE_UNUSED)
4884 char *name;
4885 symbolS *sym;
4887 if ((name = read_symbol_name ()) == NULL)
4888 return;
4890 sym = symbol_find_or_make (name);
4891 free (name);
4893 if (*input_line_pointer == ',')
4895 unsigned short visibility;
4896 coff_symbol_type *coffsym = coffsymbol (symbol_get_bfdsym (sym));
4898 input_line_pointer++;
4899 visibility = ppc_xcoff_get_visibility ();
4900 if (!visibility)
4902 as_bad (_("Unknown visibility field in .extern"));
4903 ignore_rest_of_line ();
4904 return;
4907 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4908 coffsym->native->u.syment.n_type |= visibility;
4911 demand_empty_rest_of_line ();
4914 /* XCOFF semantic for .globl says that the second parameter is
4915 the symbol visibility. */
4917 static void
4918 ppc_globl (int ignore ATTRIBUTE_UNUSED)
4920 char *name;
4921 symbolS *sym;
4923 if ((name = read_symbol_name ()) == NULL)
4924 return;
4926 sym = symbol_find_or_make (name);
4927 free (name);
4928 S_SET_EXTERNAL (sym);
4930 if (*input_line_pointer == ',')
4932 unsigned short visibility;
4933 coff_symbol_type *coffsym = coffsymbol (symbol_get_bfdsym (sym));
4935 input_line_pointer++;
4936 visibility = ppc_xcoff_get_visibility ();
4937 if (!visibility)
4939 as_bad (_("Unknown visibility field in .globl"));
4940 ignore_rest_of_line ();
4941 return;
4944 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4945 coffsym->native->u.syment.n_type |= visibility;
4948 demand_empty_rest_of_line ();
4951 /* XCOFF semantic for .weak says that the second parameter is
4952 the symbol visibility. */
4954 static void
4955 ppc_weak (int ignore ATTRIBUTE_UNUSED)
4957 char *name;
4958 symbolS *sym;
4960 if ((name = read_symbol_name ()) == NULL)
4961 return;
4963 sym = symbol_find_or_make (name);
4964 free (name);
4965 S_SET_WEAK (sym);
4967 if (*input_line_pointer == ',')
4969 unsigned short visibility;
4970 coff_symbol_type *coffsym = coffsymbol (symbol_get_bfdsym (sym));
4972 input_line_pointer++;
4973 visibility = ppc_xcoff_get_visibility ();
4974 if (!visibility)
4976 as_bad (_("Unknown visibility field in .weak"));
4977 ignore_rest_of_line ();
4978 return;
4981 coffsym->native->u.syment.n_type &= ~SYM_V_MASK;
4982 coffsym->native->u.syment.n_type |= visibility;
4985 demand_empty_rest_of_line ();
4988 /* The .lglobl pseudo-op. Keep the symbol in the symbol table. */
4990 static void
4991 ppc_lglobl (int ignore ATTRIBUTE_UNUSED)
4993 char *name;
4994 char endc;
4995 symbolS *sym;
4997 endc = get_symbol_name (&name);
4999 sym = symbol_find_or_make (name);
5001 (void) restore_line_pointer (endc);
5003 symbol_get_tc (sym)->output = 1;
5005 demand_empty_rest_of_line ();
5008 /* The .ref pseudo-op. It takes a list of symbol names and inserts R_REF
5009 relocations at the beginning of the current csect.
5011 (In principle, there's no reason why the relocations _have_ to be at
5012 the beginning. Anywhere in the csect would do. However, inserting
5013 at the beginning is what the native assembler does, and it helps to
5014 deal with cases where the .ref statements follow the section contents.)
5016 ??? .refs don't work for empty .csects. However, the native assembler
5017 doesn't report an error in this case, and neither yet do we. */
5019 static void
5020 ppc_ref (int ignore ATTRIBUTE_UNUSED)
5022 char *name;
5023 char c;
5025 if (ppc_current_csect == NULL)
5027 as_bad (_(".ref outside .csect"));
5028 ignore_rest_of_line ();
5029 return;
5034 c = get_symbol_name (&name);
5036 fix_at_start (symbol_get_frag (ppc_current_csect), 0,
5037 symbol_find_or_make (name), 0, false, BFD_RELOC_NONE);
5039 *input_line_pointer = c;
5040 SKIP_WHITESPACE_AFTER_NAME ();
5041 c = *input_line_pointer;
5042 if (c == ',')
5044 input_line_pointer++;
5045 SKIP_WHITESPACE ();
5046 if (is_end_of_line[(unsigned char) *input_line_pointer])
5048 as_bad (_("missing symbol name"));
5049 ignore_rest_of_line ();
5050 return;
5054 while (c == ',');
5056 demand_empty_rest_of_line ();
5059 /* The .rename pseudo-op. The RS/6000 assembler can rename symbols,
5060 although I don't know why it bothers. */
5062 static void
5063 ppc_rename (int ignore ATTRIBUTE_UNUSED)
5065 char *name;
5066 char endc;
5067 symbolS *sym;
5068 int len;
5070 endc = get_symbol_name (&name);
5072 sym = symbol_find_or_make (name);
5074 (void) restore_line_pointer (endc);
5076 if (*input_line_pointer != ',')
5078 as_bad (_("missing rename string"));
5079 ignore_rest_of_line ();
5080 return;
5082 ++input_line_pointer;
5084 symbol_get_tc (sym)->real_name = demand_copy_C_string (&len);
5086 demand_empty_rest_of_line ();
5089 /* The .stabx pseudo-op. This is similar to a normal .stabs
5090 pseudo-op, but slightly different. A sample is
5091 .stabx "main:F-1",.main,142,0
5092 The first argument is the symbol name to create. The second is the
5093 value, and the third is the storage class. The fourth seems to be
5094 always zero, and I am assuming it is the type. */
5096 static void
5097 ppc_stabx (int ignore ATTRIBUTE_UNUSED)
5099 char *name;
5100 int len;
5101 symbolS *sym;
5102 expressionS exp;
5104 name = demand_copy_C_string (&len);
5106 if (*input_line_pointer != ',')
5108 as_bad (_("missing value"));
5109 return;
5111 ++input_line_pointer;
5113 ppc_stab_symbol = true;
5114 sym = symbol_make (name);
5115 ppc_stab_symbol = false;
5117 symbol_get_tc (sym)->real_name = name;
5119 (void) expression (&exp);
5121 switch (exp.X_op)
5123 case O_illegal:
5124 case O_absent:
5125 case O_big:
5126 as_bad (_("illegal .stabx expression; zero assumed"));
5127 exp.X_add_number = 0;
5128 /* Fall through. */
5129 case O_constant:
5130 S_SET_VALUE (sym, (valueT) exp.X_add_number);
5131 symbol_set_frag (sym, &zero_address_frag);
5132 break;
5134 case O_symbol:
5135 if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section)
5136 symbol_set_value_expression (sym, &exp);
5137 else
5139 S_SET_VALUE (sym,
5140 exp.X_add_number + S_GET_VALUE (exp.X_add_symbol));
5141 symbol_set_frag (sym, symbol_get_frag (exp.X_add_symbol));
5143 break;
5145 default:
5146 /* The value is some complex expression. This will probably
5147 fail at some later point, but this is probably the right
5148 thing to do here. */
5149 symbol_set_value_expression (sym, &exp);
5150 break;
5153 S_SET_SEGMENT (sym, ppc_coff_debug_section);
5154 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5156 if (*input_line_pointer != ',')
5158 as_bad (_("missing class"));
5159 return;
5161 ++input_line_pointer;
5163 S_SET_STORAGE_CLASS (sym, get_absolute_expression ());
5165 if (*input_line_pointer != ',')
5167 as_bad (_("missing type"));
5168 return;
5170 ++input_line_pointer;
5172 S_SET_DATA_TYPE (sym, get_absolute_expression ());
5174 symbol_get_tc (sym)->output = 1;
5176 if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
5178 /* In this case :
5180 .bs name
5181 .stabx "z",arrays_,133,0
5184 .comm arrays_,13768,3
5186 resolve_symbol_value will copy the exp's "within" into sym's when the
5187 offset is 0. Since this seems to be corner case problem,
5188 only do the correction for storage class C_STSYM. A better solution
5189 would be to have the tc field updated in ppc_symbol_new_hook. */
5191 if (exp.X_op == O_symbol)
5193 if (ppc_current_block == NULL)
5194 as_bad (_(".stabx of storage class stsym must be within .bs/.es"));
5196 symbol_get_tc (sym)->within = ppc_current_block;
5200 if (exp.X_op != O_symbol
5201 || ! S_IS_EXTERNAL (exp.X_add_symbol)
5202 || S_GET_SEGMENT (exp.X_add_symbol) != bss_section)
5203 ppc_frob_label (sym);
5204 else
5206 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
5207 symbol_append (sym, exp.X_add_symbol, &symbol_rootP, &symbol_lastP);
5208 if (symbol_get_tc (ppc_current_csect)->within == exp.X_add_symbol)
5209 symbol_get_tc (ppc_current_csect)->within = sym;
5212 demand_empty_rest_of_line ();
5215 /* The .file pseudo-op. On XCOFF, .file can have several parameters
5216 which are being added to the symbol table to provide additional
5217 information. */
5219 static void
5220 ppc_file (int ignore ATTRIBUTE_UNUSED)
5222 char *sfname, *s1 = NULL, *s2 = NULL, *s3 = NULL;
5223 int length, auxnb = 1;
5225 /* Some assemblers tolerate immediately following '"'. */
5226 if ((sfname = demand_copy_string (&length)) != 0)
5228 coff_symbol_type *coffsym;
5229 if (*input_line_pointer == ',')
5231 ++input_line_pointer;
5232 s1 = demand_copy_string (&length);
5233 auxnb++;
5235 if (*input_line_pointer == ',')
5237 ++input_line_pointer;
5238 s2 = demand_copy_string (&length);
5239 auxnb++;
5241 if (*input_line_pointer == ',')
5243 ++input_line_pointer;
5244 s3 = demand_copy_string (&length);
5245 auxnb++;
5250 /* Use coff dot_file creation and adjust auxiliary entries. */
5251 c_dot_file_symbol (sfname);
5252 S_SET_NUMBER_AUXILIARY (symbol_rootP, auxnb);
5253 coffsym = coffsymbol (symbol_get_bfdsym (symbol_rootP));
5254 coffsym->native[1].u.auxent.x_file.x_ftype = XFT_FN;
5256 if (s1)
5258 coffsym->native[2].u.auxent.x_file.x_ftype = XFT_CT;
5259 coffsym->native[2].extrap = s1;
5261 if (s2)
5263 coffsym->native[3].u.auxent.x_file.x_ftype = XFT_CV;
5264 coffsym->native[3].extrap = s2;
5266 if (s3)
5268 coffsym->native[4].u.auxent.x_file.x_ftype = XFT_CD;
5269 coffsym->native[4].extrap = s3;
5272 demand_empty_rest_of_line ();
5276 /* The .function pseudo-op. This takes several arguments. The first
5277 argument seems to be the external name of the symbol. The second
5278 argument seems to be the label for the start of the function. gcc
5279 uses the same name for both. I have no idea what the third and
5280 fourth arguments are meant to be. The optional fifth argument is
5281 an expression for the size of the function. In COFF this symbol
5282 gets an aux entry like that used for a csect. */
5284 static void
5285 ppc_function (int ignore ATTRIBUTE_UNUSED)
5287 char *name;
5288 char endc;
5289 char *s;
5290 symbolS *ext_sym;
5291 symbolS *lab_sym;
5293 endc = get_symbol_name (&name);
5295 /* Ignore any [PR] suffix. */
5296 name = ppc_canonicalize_symbol_name (name);
5297 s = strchr (name, '[');
5298 if (s != (char *) NULL
5299 && strcmp (s + 1, "PR]") == 0)
5300 *s = '\0';
5302 ext_sym = symbol_find_or_make (name);
5304 (void) restore_line_pointer (endc);
5306 if (*input_line_pointer != ',')
5308 as_bad (_("missing symbol name"));
5309 ignore_rest_of_line ();
5310 return;
5312 ++input_line_pointer;
5314 endc = get_symbol_name (&name);
5316 lab_sym = symbol_find_or_make (name);
5318 (void) restore_line_pointer (endc);
5320 if (ext_sym != lab_sym)
5322 expressionS exp;
5324 exp.X_op = O_symbol;
5325 exp.X_add_symbol = lab_sym;
5326 exp.X_op_symbol = NULL;
5327 exp.X_add_number = 0;
5328 exp.X_unsigned = 0;
5329 symbol_set_value_expression (ext_sym, &exp);
5332 if (symbol_get_tc (ext_sym)->symbol_class == -1)
5333 symbol_get_tc (ext_sym)->symbol_class = XMC_PR;
5334 symbol_get_tc (ext_sym)->output = 1;
5336 if (*input_line_pointer == ',')
5338 expressionS exp;
5340 /* Ignore the third argument. */
5341 ++input_line_pointer;
5342 expression (& exp);
5343 if (*input_line_pointer == ',')
5345 /* Ignore the fourth argument. */
5346 ++input_line_pointer;
5347 expression (& exp);
5348 if (*input_line_pointer == ',')
5350 /* The fifth argument is the function size.
5351 If it's omitted, the size will be the containing csect.
5352 This will be donce during ppc_frob_symtab. */
5353 ++input_line_pointer;
5354 symbol_get_tc (ext_sym)->u.size
5355 = symbol_new ("L0\001", absolute_section,
5356 &zero_address_frag, 0);
5357 pseudo_set (symbol_get_tc (ext_sym)->u.size);
5362 S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
5363 SF_SET_FUNCTION (ext_sym);
5364 SF_SET_PROCESS (ext_sym);
5365 coff_add_linesym (ext_sym);
5367 demand_empty_rest_of_line ();
5370 /* The .bf pseudo-op. This is just like a COFF C_FCN symbol named
5371 ".bf". If the pseudo op .bi was seen before .bf, patch the .bi sym
5372 with the correct line number */
5374 static symbolS *saved_bi_sym = 0;
5376 static void
5377 ppc_bf (int ignore ATTRIBUTE_UNUSED)
5379 symbolS *sym;
5381 sym = symbol_make (".bf");
5382 S_SET_SEGMENT (sym, text_section);
5383 symbol_set_frag (sym, frag_now);
5384 S_SET_VALUE (sym, frag_now_fix ());
5385 S_SET_STORAGE_CLASS (sym, C_FCN);
5387 coff_line_base = get_absolute_expression ();
5389 S_SET_NUMBER_AUXILIARY (sym, 1);
5390 SA_SET_SYM_LNNO (sym, coff_line_base);
5392 /* Line number for bi. */
5393 if (saved_bi_sym)
5395 S_SET_VALUE (saved_bi_sym, coff_n_line_nos);
5396 saved_bi_sym = 0;
5400 symbol_get_tc (sym)->output = 1;
5402 ppc_frob_label (sym);
5404 demand_empty_rest_of_line ();
5407 /* The .ef pseudo-op. This is just like a COFF C_FCN symbol named
5408 ".ef", except that the line number is absolute, not relative to the
5409 most recent ".bf" symbol. */
5411 static void
5412 ppc_ef (int ignore ATTRIBUTE_UNUSED)
5414 symbolS *sym;
5416 sym = symbol_make (".ef");
5417 S_SET_SEGMENT (sym, text_section);
5418 symbol_set_frag (sym, frag_now);
5419 S_SET_VALUE (sym, frag_now_fix ());
5420 S_SET_STORAGE_CLASS (sym, C_FCN);
5421 S_SET_NUMBER_AUXILIARY (sym, 1);
5422 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
5423 symbol_get_tc (sym)->output = 1;
5425 ppc_frob_label (sym);
5427 demand_empty_rest_of_line ();
5430 /* The .bi and .ei pseudo-ops. These take a string argument and
5431 generates a C_BINCL or C_EINCL symbol, which goes at the start of
5432 the symbol list. The value of .bi will be know when the next .bf
5433 is encountered. */
5435 static void
5436 ppc_biei (int ei)
5438 static symbolS *last_biei;
5440 char *name;
5441 int len;
5442 symbolS *sym;
5443 symbolS *look;
5445 name = demand_copy_C_string (&len);
5447 /* The value of these symbols is actually file offset. Here we set
5448 the value to the index into the line number entries. In
5449 ppc_frob_symbols we set the fix_line field, which will cause BFD
5450 to do the right thing. */
5452 sym = symbol_make (name);
5453 /* obj-coff.c currently only handles line numbers correctly in the
5454 .text section. */
5455 S_SET_SEGMENT (sym, text_section);
5456 S_SET_VALUE (sym, coff_n_line_nos);
5457 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5459 S_SET_STORAGE_CLASS (sym, ei ? C_EINCL : C_BINCL);
5460 symbol_get_tc (sym)->output = 1;
5462 /* Save bi. */
5463 if (ei)
5464 saved_bi_sym = 0;
5465 else
5466 saved_bi_sym = sym;
5468 for (look = last_biei ? last_biei : symbol_rootP;
5469 (look != (symbolS *) NULL
5470 && (S_GET_STORAGE_CLASS (look) == C_FILE
5471 || S_GET_STORAGE_CLASS (look) == C_BINCL
5472 || S_GET_STORAGE_CLASS (look) == C_EINCL));
5473 look = symbol_next (look))
5475 if (look != (symbolS *) NULL)
5477 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
5478 symbol_insert (sym, look, &symbol_rootP, &symbol_lastP);
5479 last_biei = sym;
5482 demand_empty_rest_of_line ();
5485 /* The .bs pseudo-op. This generates a C_BSTAT symbol named ".bs".
5486 There is one argument, which is a csect symbol. The value of the
5487 .bs symbol is the index of this csect symbol. */
5489 static void
5490 ppc_bs (int ignore ATTRIBUTE_UNUSED)
5492 char *name;
5493 char endc;
5494 symbolS *csect;
5495 symbolS *sym;
5497 if (ppc_current_block != NULL)
5498 as_bad (_("nested .bs blocks"));
5500 endc = get_symbol_name (&name);
5502 csect = symbol_find_or_make (name);
5504 (void) restore_line_pointer (endc);
5506 sym = symbol_make (".bs");
5507 S_SET_SEGMENT (sym, now_seg);
5508 S_SET_STORAGE_CLASS (sym, C_BSTAT);
5509 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5510 symbol_get_tc (sym)->output = 1;
5512 symbol_get_tc (sym)->within = csect;
5514 ppc_frob_label (sym);
5516 ppc_current_block = sym;
5518 demand_empty_rest_of_line ();
5521 /* The .es pseudo-op. Generate a C_ESTART symbol named .es. */
5523 static void
5524 ppc_es (int ignore ATTRIBUTE_UNUSED)
5526 symbolS *sym;
5528 if (ppc_current_block == NULL)
5529 as_bad (_(".es without preceding .bs"));
5531 sym = symbol_make (".es");
5532 S_SET_SEGMENT (sym, now_seg);
5533 S_SET_STORAGE_CLASS (sym, C_ESTAT);
5534 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5535 symbol_get_tc (sym)->output = 1;
5537 ppc_frob_label (sym);
5539 ppc_current_block = NULL;
5541 demand_empty_rest_of_line ();
5544 /* The .bb pseudo-op. Generate a C_BLOCK symbol named .bb, with a
5545 line number. */
5547 static void
5548 ppc_bb (int ignore ATTRIBUTE_UNUSED)
5550 symbolS *sym;
5552 sym = symbol_make (".bb");
5553 S_SET_SEGMENT (sym, text_section);
5554 symbol_set_frag (sym, frag_now);
5555 S_SET_VALUE (sym, frag_now_fix ());
5556 S_SET_STORAGE_CLASS (sym, C_BLOCK);
5558 S_SET_NUMBER_AUXILIARY (sym, 1);
5559 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
5561 symbol_get_tc (sym)->output = 1;
5563 SF_SET_PROCESS (sym);
5565 ppc_frob_label (sym);
5567 demand_empty_rest_of_line ();
5570 /* The .eb pseudo-op. Generate a C_BLOCK symbol named .eb, with a
5571 line number. */
5573 static void
5574 ppc_eb (int ignore ATTRIBUTE_UNUSED)
5576 symbolS *sym;
5578 sym = symbol_make (".eb");
5579 S_SET_SEGMENT (sym, text_section);
5580 symbol_set_frag (sym, frag_now);
5581 S_SET_VALUE (sym, frag_now_fix ());
5582 S_SET_STORAGE_CLASS (sym, C_BLOCK);
5583 S_SET_NUMBER_AUXILIARY (sym, 1);
5584 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
5585 symbol_get_tc (sym)->output = 1;
5587 SF_SET_PROCESS (sym);
5589 ppc_frob_label (sym);
5591 demand_empty_rest_of_line ();
5594 /* The .bc pseudo-op. This just creates a C_BCOMM symbol with a
5595 specified name. */
5597 static void
5598 ppc_bc (int ignore ATTRIBUTE_UNUSED)
5600 char *name;
5601 int len;
5602 symbolS *sym;
5604 name = demand_copy_C_string (&len);
5605 sym = symbol_make (name);
5606 S_SET_SEGMENT (sym, ppc_coff_debug_section);
5607 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5608 S_SET_STORAGE_CLASS (sym, C_BCOMM);
5609 S_SET_VALUE (sym, 0);
5610 symbol_get_tc (sym)->output = 1;
5612 ppc_frob_label (sym);
5614 demand_empty_rest_of_line ();
5617 /* The .ec pseudo-op. This just creates a C_ECOMM symbol. */
5619 static void
5620 ppc_ec (int ignore ATTRIBUTE_UNUSED)
5622 symbolS *sym;
5624 sym = symbol_make (".ec");
5625 S_SET_SEGMENT (sym, ppc_coff_debug_section);
5626 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5627 S_SET_STORAGE_CLASS (sym, C_ECOMM);
5628 S_SET_VALUE (sym, 0);
5629 symbol_get_tc (sym)->output = 1;
5631 ppc_frob_label (sym);
5633 demand_empty_rest_of_line ();
5636 /* The .toc pseudo-op. Switch to the .toc subsegment. */
5638 static void
5639 ppc_toc (int ignore ATTRIBUTE_UNUSED)
5641 if (ppc_toc_csect != (symbolS *) NULL)
5642 subseg_set (data_section, symbol_get_tc (ppc_toc_csect)->subseg);
5643 else
5645 subsegT subseg;
5646 symbolS *sym;
5647 symbolS *list;
5649 subseg = ppc_xcoff_data_section.next_subsegment;
5650 ++ppc_xcoff_data_section.next_subsegment;
5652 subseg_new (segment_name (data_section), subseg);
5653 ppc_toc_frag = frag_now;
5655 sym = symbol_find_or_make ("TOC[TC0]");
5656 symbol_set_frag (sym, frag_now);
5657 S_SET_SEGMENT (sym, data_section);
5658 S_SET_VALUE (sym, (valueT) frag_now_fix ());
5659 symbol_get_tc (sym)->subseg = subseg;
5660 symbol_get_tc (sym)->output = 1;
5661 symbol_get_tc (sym)->within = sym;
5663 ppc_toc_csect = sym;
5665 for (list = ppc_xcoff_data_section.csects;
5666 symbol_get_tc (list)->next != (symbolS *) NULL;
5667 list = symbol_get_tc (list)->next)
5669 symbol_get_tc (list)->next = sym;
5671 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
5672 symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
5673 &symbol_lastP);
5676 ppc_current_csect = ppc_toc_csect;
5678 demand_empty_rest_of_line ();
5681 /* The AIX assembler automatically aligns the operands of a .long or
5682 .short pseudo-op, and we want to be compatible. */
5684 static void
5685 ppc_xcoff_cons (int log_size)
5687 frag_align (log_size, 0, 0);
5688 record_alignment (now_seg, log_size);
5689 cons (1 << log_size);
5692 static void
5693 ppc_vbyte (int dummy ATTRIBUTE_UNUSED)
5695 expressionS exp;
5696 int byte_count;
5698 (void) expression (&exp);
5700 if (exp.X_op != O_constant)
5702 as_bad (_("non-constant byte count"));
5703 return;
5706 byte_count = exp.X_add_number;
5708 if (*input_line_pointer != ',')
5710 as_bad (_("missing value"));
5711 return;
5714 ++input_line_pointer;
5715 cons (byte_count);
5718 void
5719 ppc_xcoff_md_finish (void)
5721 int i;
5723 for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
5725 struct dw_section *dws = &dw_sections[i];
5726 struct dw_subsection *dwss;
5728 if (dws->anon_subseg)
5730 dwss = dws->anon_subseg;
5731 dwss->link = dws->list_subseg;
5733 else
5734 dwss = dws->list_subseg;
5736 for (; dwss != NULL; dwss = dwss->link)
5737 if (dwss->end_exp.X_add_symbol != NULL)
5739 subseg_set (dws->sect, dwss->subseg);
5740 symbol_set_value_now (dwss->end_exp.X_add_symbol);
5743 ppc_cpu = 0;
5746 #endif /* OBJ_XCOFF */
5747 #if defined (OBJ_XCOFF) || defined (OBJ_ELF)
5749 /* The .tc pseudo-op. This is used when generating either XCOFF or
5750 ELF. This takes two or more arguments.
5752 When generating XCOFF output, the first argument is the name to
5753 give to this location in the toc; this will be a symbol with class
5754 TC. The rest of the arguments are N-byte values to actually put at
5755 this location in the TOC; often there is just one more argument, a
5756 relocatable symbol reference. The size of the value to store
5757 depends on target word size. A 32-bit target uses 4-byte values, a
5758 64-bit target uses 8-byte values.
5760 When not generating XCOFF output, the arguments are the same, but
5761 the first argument is simply ignored. */
5763 static void
5764 ppc_tc (int ignore ATTRIBUTE_UNUSED)
5766 #ifdef OBJ_XCOFF
5768 /* Define the TOC symbol name. */
5770 char *name;
5771 char endc;
5772 symbolS *sym;
5774 if (ppc_toc_csect == (symbolS *) NULL
5775 || ppc_toc_csect != ppc_current_csect)
5777 as_bad (_(".tc not in .toc section"));
5778 ignore_rest_of_line ();
5779 return;
5782 endc = get_symbol_name (&name);
5784 sym = symbol_find_or_make (name);
5786 (void) restore_line_pointer (endc);
5788 if (S_IS_DEFINED (sym))
5790 symbolS *label;
5792 label = symbol_get_tc (ppc_current_csect)->within;
5793 if (symbol_get_tc (label)->symbol_class != XMC_TC0)
5795 as_bad (_(".tc with no label"));
5796 ignore_rest_of_line ();
5797 return;
5800 S_SET_SEGMENT (label, S_GET_SEGMENT (sym));
5801 symbol_set_frag (label, symbol_get_frag (sym));
5802 S_SET_VALUE (label, S_GET_VALUE (sym));
5804 while (! is_end_of_line[(unsigned char) *input_line_pointer])
5805 ++input_line_pointer;
5807 return;
5810 S_SET_SEGMENT (sym, now_seg);
5811 symbol_set_frag (sym, frag_now);
5812 S_SET_VALUE (sym, (valueT) frag_now_fix ());
5814 /* AIX assembler seems to allow any storage class to be set in .tc.
5815 But for now, only XMC_TC and XMC_TE are supported by us. */
5816 switch (symbol_get_tc (sym)->symbol_class)
5818 case XMC_TC:
5819 case XMC_TE:
5820 break;
5822 default:
5823 as_bad (_(".tc with storage class %d not yet supported"),
5824 symbol_get_tc (sym)->symbol_class);
5825 ignore_rest_of_line ();
5826 return;
5828 symbol_get_tc (sym)->output = 1;
5830 ppc_frob_label (sym);
5833 #endif /* OBJ_XCOFF */
5834 #ifdef OBJ_ELF
5835 int align;
5837 /* Skip the TOC symbol name. */
5838 while (is_part_of_name (*input_line_pointer)
5839 || *input_line_pointer == ' '
5840 || *input_line_pointer == '['
5841 || *input_line_pointer == ']'
5842 || *input_line_pointer == '{'
5843 || *input_line_pointer == '}')
5844 ++input_line_pointer;
5846 /* Align to a four/eight byte boundary. */
5847 align = ppc_obj64 ? 3 : 2;
5848 frag_align (align, 0, 0);
5849 record_alignment (now_seg, align);
5850 #endif /* OBJ_ELF */
5852 if (*input_line_pointer != ',')
5853 demand_empty_rest_of_line ();
5854 else
5856 ++input_line_pointer;
5857 cons (ppc_obj64 ? 8 : 4);
5861 /* Pseudo-op .machine. */
5863 static void
5864 ppc_machine (int ignore ATTRIBUTE_UNUSED)
5866 char c;
5867 char *cpu_string;
5868 #define MAX_HISTORY 100
5869 static ppc_cpu_t *cpu_history;
5870 static int curr_hist;
5872 SKIP_WHITESPACE ();
5874 c = get_symbol_name (&cpu_string);
5875 cpu_string = xstrdup (cpu_string);
5876 (void) restore_line_pointer (c);
5878 if (cpu_string != NULL)
5880 ppc_cpu_t old_cpu = ppc_cpu;
5881 char *p;
5883 for (p = cpu_string; *p != 0; p++)
5884 *p = TOLOWER (*p);
5886 if (strcmp (cpu_string, "push") == 0)
5888 if (cpu_history == NULL)
5889 cpu_history = XNEWVEC (ppc_cpu_t, MAX_HISTORY);
5891 if (curr_hist >= MAX_HISTORY)
5892 as_bad (_(".machine stack overflow"));
5893 else
5894 cpu_history[curr_hist++] = ppc_cpu;
5896 else if (strcmp (cpu_string, "pop") == 0)
5898 if (curr_hist <= 0)
5899 as_bad (_(".machine stack underflow"));
5900 else
5901 ppc_cpu = cpu_history[--curr_hist];
5903 else
5905 ppc_cpu_t new_cpu;
5906 /* Not using the global "sticky" variable here results in
5907 none of the extra functional unit command line options,
5908 -many, -maltivec, -mspe, -mspe2, -mvle, -mvsx, being in
5909 force after selecting a new cpu with .machine.
5910 ".machine altivec" and other extra functional unit
5911 options do not count as a new machine, instead they add
5912 to currently selected opcodes. */
5913 ppc_cpu_t machine_sticky = 0;
5914 /* Unfortunately, some versions of gcc emit a .machine
5915 directive very near the start of the compiler's assembly
5916 output file. This is bad because it overrides user -Wa
5917 cpu selection. Worse, there are versions of gcc that
5918 emit the *wrong* cpu, not even respecting the -mcpu given
5919 to gcc. See gcc pr101393. And to compound the problem,
5920 as of 20220222 gcc doesn't pass the correct cpu option to
5921 gas on the command line. See gcc pr59828. Hack around
5922 this by keeping sticky options for an early .machine. */
5923 asection *sec;
5924 for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
5926 segment_info_type *info = seg_info (sec);
5927 /* Are the frags for this section perturbed from their
5928 initial state? Even .align will count here. */
5929 if (info != NULL
5930 && (info->frchainP->frch_root != info->frchainP->frch_last
5931 || info->frchainP->frch_root->fr_type != rs_fill
5932 || info->frchainP->frch_root->fr_fix != 0))
5933 break;
5935 new_cpu = ppc_parse_cpu (ppc_cpu,
5936 sec == NULL ? &sticky : &machine_sticky,
5937 cpu_string);
5938 if (new_cpu != 0)
5939 ppc_cpu = new_cpu;
5940 else
5941 as_bad (_("invalid machine `%s'"), cpu_string);
5944 if (ppc_cpu != old_cpu)
5945 ppc_setup_opcodes ();
5948 demand_empty_rest_of_line ();
5950 #endif /* defined (OBJ_XCOFF) || defined (OBJ_ELF) */
5952 #ifdef OBJ_XCOFF
5954 /* XCOFF specific symbol and file handling. */
5956 /* Canonicalize the symbol name. We use the to force the suffix, if
5957 any, to use square brackets, and to be in upper case. */
5959 char *
5960 ppc_canonicalize_symbol_name (char *name)
5962 char *s;
5964 if (ppc_stab_symbol)
5965 return name;
5967 for (s = name; *s != '\0' && *s != '{' && *s != '['; s++)
5969 if (*s != '\0')
5971 char brac;
5973 if (*s == '[')
5974 brac = ']';
5975 else
5977 *s = '[';
5978 brac = '}';
5981 for (s++; *s != '\0' && *s != brac; s++)
5982 *s = TOUPPER (*s);
5984 if (*s == '\0' || s[1] != '\0')
5985 as_bad (_("bad symbol suffix"));
5987 *s = ']';
5990 return name;
5993 /* Set the class of a symbol based on the suffix, if any. This is
5994 called whenever a new symbol is created. */
5996 void
5997 ppc_symbol_new_hook (symbolS *sym)
5999 struct ppc_tc_sy *tc;
6000 const char *s;
6002 tc = symbol_get_tc (sym);
6003 tc->next = NULL;
6004 tc->output = 0;
6005 tc->symbol_class = -1;
6006 tc->real_name = NULL;
6007 tc->subseg = 0;
6008 tc->align = 0;
6009 tc->u.size = NULL;
6010 tc->u.dw = NULL;
6011 tc->within = NULL;
6013 if (ppc_stab_symbol)
6014 return;
6016 s = strchr (S_GET_NAME (sym), '[');
6017 if (s == (const char *) NULL)
6019 /* There is no suffix. */
6020 return;
6023 ++s;
6025 switch (s[0])
6027 case 'B':
6028 if (strcmp (s, "BS]") == 0)
6029 tc->symbol_class = XMC_BS;
6030 break;
6031 case 'D':
6032 if (strcmp (s, "DB]") == 0)
6033 tc->symbol_class = XMC_DB;
6034 else if (strcmp (s, "DS]") == 0)
6035 tc->symbol_class = XMC_DS;
6036 break;
6037 case 'G':
6038 if (strcmp (s, "GL]") == 0)
6039 tc->symbol_class = XMC_GL;
6040 break;
6041 case 'P':
6042 if (strcmp (s, "PR]") == 0)
6043 tc->symbol_class = XMC_PR;
6044 break;
6045 case 'R':
6046 if (strcmp (s, "RO]") == 0)
6047 tc->symbol_class = XMC_RO;
6048 else if (strcmp (s, "RW]") == 0)
6049 tc->symbol_class = XMC_RW;
6050 break;
6051 case 'S':
6052 if (strcmp (s, "SV]") == 0)
6053 tc->symbol_class = XMC_SV;
6054 break;
6055 case 'T':
6056 if (strcmp (s, "TC]") == 0)
6057 tc->symbol_class = XMC_TC;
6058 else if (strcmp (s, "TI]") == 0)
6059 tc->symbol_class = XMC_TI;
6060 else if (strcmp (s, "TB]") == 0)
6061 tc->symbol_class = XMC_TB;
6062 else if (strcmp (s, "TC0]") == 0 || strcmp (s, "T0]") == 0)
6063 tc->symbol_class = XMC_TC0;
6064 else if (strcmp (s, "TE]") == 0)
6065 tc->symbol_class = XMC_TE;
6066 else if (strcmp (s, "TL]") == 0)
6067 tc->symbol_class = XMC_TL;
6068 break;
6069 case 'U':
6070 if (strcmp (s, "UA]") == 0)
6071 tc->symbol_class = XMC_UA;
6072 else if (strcmp (s, "UC]") == 0)
6073 tc->symbol_class = XMC_UC;
6074 else if (strcmp (s, "UL]") == 0)
6075 tc->symbol_class = XMC_UL;
6076 break;
6077 case 'X':
6078 if (strcmp (s, "XO]") == 0)
6079 tc->symbol_class = XMC_XO;
6080 break;
6083 if (tc->symbol_class == -1)
6084 as_bad (_("unrecognized symbol suffix"));
6087 /* This variable is set by ppc_frob_symbol if any absolute symbols are
6088 seen. It tells ppc_adjust_symtab whether it needs to look through
6089 the symbols. */
6091 static bool ppc_saw_abs;
6093 /* Change the name of a symbol just before writing it out. Set the
6094 real name if the .rename pseudo-op was used. Otherwise, remove any
6095 class suffix. Return 1 if the symbol should not be included in the
6096 symbol table. */
6099 ppc_frob_symbol (symbolS *sym)
6101 static symbolS *ppc_last_function;
6102 static symbolS *set_end;
6104 /* Discard symbols that should not be included in the output symbol
6105 table. */
6106 if (! symbol_used_in_reloc_p (sym)
6107 && S_GET_STORAGE_CLASS (sym) != C_DWARF
6108 && ((symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) != 0
6109 || (! (S_IS_EXTERNAL (sym) || S_IS_WEAK (sym))
6110 && ! symbol_get_tc (sym)->output
6111 && S_GET_STORAGE_CLASS (sym) != C_FILE)))
6112 return 1;
6114 /* This one will disappear anyway. Don't make a csect sym for it. */
6115 if (sym == abs_section_sym)
6116 return 1;
6118 if (symbol_get_tc (sym)->real_name != (char *) NULL)
6119 S_SET_NAME (sym, symbol_get_tc (sym)->real_name);
6120 else
6122 const char *name;
6123 const char *s;
6125 name = S_GET_NAME (sym);
6126 s = strchr (name, '[');
6127 if (s != (char *) NULL)
6129 unsigned int len;
6130 char *snew;
6132 len = s - name;
6133 snew = xstrndup (name, len);
6135 S_SET_NAME (sym, snew);
6139 if (set_end != (symbolS *) NULL)
6141 SA_SET_SYM_ENDNDX (set_end, sym);
6142 set_end = NULL;
6145 if (SF_GET_FUNCTION (sym))
6147 /* Make sure coff_last_function is reset. Otherwise, we won't create
6148 the auxent for the next function. */
6149 coff_last_function = 0;
6150 ppc_last_function = sym;
6151 if (symbol_get_tc (sym)->u.size != (symbolS *) NULL)
6153 resolve_symbol_value (symbol_get_tc (sym)->u.size);
6154 SA_SET_SYM_FSIZE (sym,
6155 (long) S_GET_VALUE (symbol_get_tc (sym)->u.size));
6157 else
6159 /* Size of containing csect. */
6160 symbolS* within = symbol_get_tc (sym)->within;
6161 coff_symbol_type *csect = coffsymbol (symbol_get_bfdsym (within));
6162 combined_entry_type *csectaux
6163 = &csect->native[S_GET_NUMBER_AUXILIARY(within)];
6165 SA_SET_SYM_FSIZE (sym, csectaux->u.auxent.x_csect.x_scnlen.u64);
6168 else if (S_GET_STORAGE_CLASS (sym) == C_FCN
6169 && strcmp (S_GET_NAME (sym), ".ef") == 0)
6171 if (ppc_last_function == (symbolS *) NULL)
6172 as_bad (_(".ef with no preceding .function"));
6173 else
6175 set_end = ppc_last_function;
6176 ppc_last_function = NULL;
6178 /* We don't have a C_EFCN symbol, but we need to force the
6179 COFF backend to believe that it has seen one. */
6180 coff_last_function = NULL;
6184 if (! (S_IS_EXTERNAL (sym) || S_IS_WEAK (sym))
6185 && (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) == 0
6186 && S_GET_STORAGE_CLASS (sym) != C_FILE
6187 && S_GET_STORAGE_CLASS (sym) != C_FCN
6188 && S_GET_STORAGE_CLASS (sym) != C_BLOCK
6189 && S_GET_STORAGE_CLASS (sym) != C_BSTAT
6190 && S_GET_STORAGE_CLASS (sym) != C_ESTAT
6191 && S_GET_STORAGE_CLASS (sym) != C_BINCL
6192 && S_GET_STORAGE_CLASS (sym) != C_EINCL
6193 && S_GET_SEGMENT (sym) != ppc_coff_debug_section)
6194 S_SET_STORAGE_CLASS (sym, C_HIDEXT);
6196 if (S_GET_STORAGE_CLASS (sym) == C_EXT
6197 || S_GET_STORAGE_CLASS (sym) == C_AIX_WEAKEXT
6198 || S_GET_STORAGE_CLASS (sym) == C_HIDEXT)
6200 int i;
6201 combined_entry_type *a;
6203 /* Create a csect aux. */
6204 i = S_GET_NUMBER_AUXILIARY (sym);
6205 S_SET_NUMBER_AUXILIARY (sym, i + 1);
6206 a = &coffsymbol (symbol_get_bfdsym (sym))->native[i + 1];
6207 if (symbol_get_tc (sym)->symbol_class == XMC_TC0)
6209 /* This is the TOC table. */
6210 know (strcmp (S_GET_NAME (sym), "TOC") == 0);
6211 a->u.auxent.x_csect.x_scnlen.u64 = 0;
6212 a->u.auxent.x_csect.x_smtyp = (2 << 3) | XTY_SD;
6214 else if (symbol_get_tc (sym)->subseg != 0)
6216 /* This is a csect symbol. x_scnlen is the size of the
6217 csect. */
6218 if (symbol_get_tc (sym)->next == (symbolS *) NULL)
6219 a->u.auxent.x_csect.x_scnlen.u64
6220 = bfd_section_size (S_GET_SEGMENT (sym)) - S_GET_VALUE (sym);
6221 else
6223 resolve_symbol_value (symbol_get_tc (sym)->next);
6224 a->u.auxent.x_csect.x_scnlen.u64
6225 = S_GET_VALUE (symbol_get_tc (sym)->next) - S_GET_VALUE (sym);
6227 if (symbol_get_tc (sym)->symbol_class == XMC_BS
6228 || symbol_get_tc (sym)->symbol_class == XMC_UL)
6229 a->u.auxent.x_csect.x_smtyp
6230 = (symbol_get_tc (sym)->align << 3) | XTY_CM;
6231 else
6232 a->u.auxent.x_csect.x_smtyp
6233 = (symbol_get_tc (sym)->align << 3) | XTY_SD;
6235 else if (S_GET_SEGMENT (sym) == bss_section
6236 || S_GET_SEGMENT (sym) == ppc_xcoff_tbss_section.segment)
6238 /* This is a common symbol. */
6239 a->u.auxent.x_csect.x_scnlen.u64 = symbol_get_frag (sym)->fr_offset;
6240 a->u.auxent.x_csect.x_smtyp
6241 = (symbol_get_tc (sym)->align << 3) | XTY_CM;
6242 if (S_GET_SEGMENT (sym) == ppc_xcoff_tbss_section.segment)
6243 symbol_get_tc (sym)->symbol_class = XMC_UL;
6244 else if (S_IS_EXTERNAL (sym))
6245 symbol_get_tc (sym)->symbol_class = XMC_RW;
6246 else
6247 symbol_get_tc (sym)->symbol_class = XMC_BS;
6249 else if (S_GET_SEGMENT (sym) == absolute_section)
6251 /* This is an absolute symbol. The csect will be created by
6252 ppc_adjust_symtab. */
6253 ppc_saw_abs = true;
6254 a->u.auxent.x_csect.x_smtyp = XTY_LD;
6255 if (symbol_get_tc (sym)->symbol_class == -1)
6256 symbol_get_tc (sym)->symbol_class = XMC_XO;
6258 else if (! S_IS_DEFINED (sym))
6260 /* This is an external symbol. */
6261 a->u.auxent.x_csect.x_scnlen.u64 = 0;
6262 a->u.auxent.x_csect.x_smtyp = XTY_ER;
6264 else if (ppc_is_toc_sym (sym))
6266 symbolS *next;
6268 /* This is a TOC definition. x_scnlen is the size of the
6269 TOC entry. */
6270 next = symbol_next (sym);
6271 while (symbol_get_tc (next)->symbol_class == XMC_TC0)
6272 next = symbol_next (next);
6273 if (next == (symbolS *) NULL
6274 || (!ppc_is_toc_sym (next)))
6276 if (ppc_after_toc_frag == (fragS *) NULL)
6277 a->u.auxent.x_csect.x_scnlen.u64
6278 = bfd_section_size (data_section) - S_GET_VALUE (sym);
6279 else
6280 a->u.auxent.x_csect.x_scnlen.u64
6281 = ppc_after_toc_frag->fr_address - S_GET_VALUE (sym);
6283 else
6285 resolve_symbol_value (next);
6286 a->u.auxent.x_csect.x_scnlen.u64
6287 = S_GET_VALUE (next) - S_GET_VALUE (sym);
6289 a->u.auxent.x_csect.x_smtyp = (2 << 3) | XTY_SD;
6291 else
6293 symbolS *csect;
6295 /* This is a normal symbol definition. x_scnlen is the
6296 symbol index of the containing csect. */
6297 if (S_GET_SEGMENT (sym) == text_section)
6298 csect = ppc_xcoff_text_section.csects;
6299 else if (S_GET_SEGMENT (sym) == data_section)
6300 csect = ppc_xcoff_data_section.csects;
6301 else if (S_GET_SEGMENT (sym) == ppc_xcoff_tdata_section.segment)
6302 csect = ppc_xcoff_tdata_section.csects;
6303 else
6304 abort ();
6306 /* Skip the initial dummy symbol. */
6307 csect = symbol_get_tc (csect)->next;
6309 if (csect == (symbolS *) NULL)
6311 as_warn (_("warning: symbol %s has no csect"), S_GET_NAME (sym));
6312 a->u.auxent.x_csect.x_scnlen.u64 = 0;
6314 else
6316 while (symbol_get_tc (csect)->next != (symbolS *) NULL)
6318 resolve_symbol_value (symbol_get_tc (csect)->next);
6319 if (S_GET_VALUE (symbol_get_tc (csect)->next)
6320 > S_GET_VALUE (sym))
6321 break;
6322 csect = symbol_get_tc (csect)->next;
6325 a->u.auxent.x_csect.x_scnlen.p
6326 = coffsymbol (symbol_get_bfdsym (csect))->native;
6327 a->fix_scnlen = 1;
6329 a->u.auxent.x_csect.x_smtyp = XTY_LD;
6332 a->u.auxent.x_csect.x_parmhash = 0;
6333 a->u.auxent.x_csect.x_snhash = 0;
6334 if (symbol_get_tc (sym)->symbol_class == -1)
6335 a->u.auxent.x_csect.x_smclas = XMC_PR;
6336 else
6337 a->u.auxent.x_csect.x_smclas = symbol_get_tc (sym)->symbol_class;
6338 a->u.auxent.x_csect.x_stab = 0;
6339 a->u.auxent.x_csect.x_snstab = 0;
6341 /* Don't let the COFF backend resort these symbols. */
6342 symbol_get_bfdsym (sym)->flags |= BSF_NOT_AT_END;
6344 else if (S_GET_STORAGE_CLASS (sym) == C_BSTAT)
6346 /* We want the value to be the symbol index of the referenced
6347 csect symbol. BFD will do that for us if we set the right
6348 flags. */
6349 asymbol *bsym = symbol_get_bfdsym (symbol_get_tc (sym)->within);
6350 combined_entry_type *c = coffsymbol (bsym)->native;
6352 S_SET_VALUE (sym, (valueT) (size_t) c);
6353 coffsymbol (symbol_get_bfdsym (sym))->native->fix_value = 1;
6355 else if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
6357 symbolS *block;
6358 valueT base;
6360 block = symbol_get_tc (sym)->within;
6361 if (block)
6363 /* The value is the offset from the enclosing csect. */
6364 symbolS *csect;
6366 csect = symbol_get_tc (block)->within;
6367 resolve_symbol_value (csect);
6368 base = S_GET_VALUE (csect);
6370 else
6371 base = 0;
6373 S_SET_VALUE (sym, S_GET_VALUE (sym) - base);
6375 else if (S_GET_STORAGE_CLASS (sym) == C_BINCL
6376 || S_GET_STORAGE_CLASS (sym) == C_EINCL)
6378 /* We want the value to be a file offset into the line numbers.
6379 BFD will do that for us if we set the right flags. We have
6380 already set the value correctly. */
6381 coffsymbol (symbol_get_bfdsym (sym))->native->fix_line = 1;
6384 return 0;
6387 /* Adjust the symbol table. */
6389 void
6390 ppc_adjust_symtab (void)
6392 symbolS *sym;
6393 symbolS *anchorSym;
6395 /* Make sure C_DWARF symbols come right after C_FILE.
6396 As the C_FILE might not be defined yet and as C_DWARF
6397 might already be ordered, we insert them before the
6398 first symbol which isn't a C_FILE or a C_DWARF. */
6399 for (anchorSym = symbol_rootP; anchorSym != NULL;
6400 anchorSym = symbol_next (anchorSym))
6402 if (S_GET_STORAGE_CLASS (anchorSym) != C_FILE
6403 && S_GET_STORAGE_CLASS (anchorSym) != C_DWARF)
6404 break;
6407 sym = anchorSym;
6408 while (sym != NULL)
6410 if (S_GET_STORAGE_CLASS (sym) != C_DWARF)
6412 sym = symbol_next (sym);
6413 continue;
6416 symbolS* tsym = sym;
6417 sym = symbol_next (sym);
6419 symbol_remove (tsym, &symbol_rootP, &symbol_lastP);
6420 symbol_insert (tsym, anchorSym, &symbol_rootP, &symbol_lastP);
6423 /* Create csect symbols for all absolute symbols. */
6425 if (! ppc_saw_abs)
6426 return;
6428 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
6430 symbolS *csect;
6431 int i;
6432 combined_entry_type *a;
6434 if (S_GET_SEGMENT (sym) != absolute_section)
6435 continue;
6437 csect = symbol_create (".abs[XO]", absolute_section,
6438 &zero_address_frag, S_GET_VALUE (sym));
6439 symbol_get_bfdsym (csect)->value = S_GET_VALUE (sym);
6440 S_SET_STORAGE_CLASS (csect, C_HIDEXT);
6441 i = S_GET_NUMBER_AUXILIARY (csect);
6442 S_SET_NUMBER_AUXILIARY (csect, i + 1);
6443 a = &coffsymbol (symbol_get_bfdsym (csect))->native[i + 1];
6444 a->u.auxent.x_csect.x_scnlen.u64 = 0;
6445 a->u.auxent.x_csect.x_smtyp = XTY_SD;
6446 a->u.auxent.x_csect.x_parmhash = 0;
6447 a->u.auxent.x_csect.x_snhash = 0;
6448 a->u.auxent.x_csect.x_smclas = XMC_XO;
6449 a->u.auxent.x_csect.x_stab = 0;
6450 a->u.auxent.x_csect.x_snstab = 0;
6452 symbol_insert (csect, sym, &symbol_rootP, &symbol_lastP);
6454 i = S_GET_NUMBER_AUXILIARY (sym);
6455 a = &coffsymbol (symbol_get_bfdsym (sym))->native[i];
6456 a->u.auxent.x_csect.x_scnlen.p
6457 = coffsymbol (symbol_get_bfdsym (csect))->native;
6458 a->fix_scnlen = 1;
6461 ppc_saw_abs = false;
6464 /* Set the VMA for a section. This is called on all the sections in
6465 turn. */
6467 void
6468 ppc_frob_section (asection *sec)
6470 static bfd_vma vma = 0;
6472 /* Dwarf sections start at 0. */
6473 if (bfd_section_flags (sec) & SEC_DEBUGGING)
6474 return;
6476 vma = md_section_align (sec, vma);
6477 bfd_set_section_vma (sec, vma);
6478 vma += bfd_section_size (sec);
6481 #endif /* OBJ_XCOFF */
6483 const char *
6484 md_atof (int type, char *litp, int *sizep)
6486 return ieee_md_atof (type, litp, sizep, target_big_endian);
6489 /* Write a value out to the object file, using the appropriate
6490 endianness. */
6492 void
6493 md_number_to_chars (char *buf, valueT val, int n)
6495 if (target_big_endian)
6496 number_to_chars_bigendian (buf, val, n);
6497 else
6498 number_to_chars_littleendian (buf, val, n);
6501 /* Align a section (I don't know why this is machine dependent). */
6503 valueT
6504 md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT addr)
6506 #ifdef OBJ_ELF
6507 return addr;
6508 #else
6509 int align = bfd_section_alignment (seg);
6511 return ((addr + (1 << align) - 1) & -(1 << align));
6512 #endif
6515 /* We don't have any form of relaxing. */
6518 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
6519 asection *seg ATTRIBUTE_UNUSED)
6521 abort ();
6522 return 0;
6525 /* Convert a machine dependent frag. We never generate these. */
6527 void
6528 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
6529 asection *sec ATTRIBUTE_UNUSED,
6530 fragS *fragp ATTRIBUTE_UNUSED)
6532 abort ();
6535 /* We have no need to default values of symbols. */
6537 symbolS *
6538 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
6540 return 0;
6543 /* Functions concerning relocs. */
6545 /* The location from which a PC relative jump should be calculated,
6546 given a PC relative reloc. */
6548 long
6549 md_pcrel_from_section (fixS *fixp, segT sec ATTRIBUTE_UNUSED)
6551 return fixp->fx_frag->fr_address + fixp->fx_where;
6554 #ifdef OBJ_XCOFF
6556 /* Return the surrending csect for sym when possible. */
6558 static symbolS*
6559 ppc_get_csect_to_adjust (symbolS *sym)
6561 if (sym == NULL)
6562 return NULL;
6564 valueT val = resolve_symbol_value (sym);
6565 TC_SYMFIELD_TYPE *tc = symbol_get_tc (sym);
6566 segT symseg = S_GET_SEGMENT (sym);
6568 if (tc->subseg == 0
6569 && tc->symbol_class != XMC_TC0
6570 && tc->symbol_class != XMC_TC
6571 && tc->symbol_class != XMC_TE
6572 && symseg != bss_section
6573 && symseg != ppc_xcoff_tbss_section.segment
6574 /* Don't adjust if this is a reloc in the toc section. */
6575 && (symseg != data_section
6576 || ppc_toc_csect == NULL
6577 || val < ppc_toc_frag->fr_address
6578 || (ppc_after_toc_frag != NULL
6579 && val >= ppc_after_toc_frag->fr_address)))
6581 symbolS* csect = tc->within;
6583 /* If the symbol was not declared by a label (eg: a section symbol),
6584 use the section instead of the csect. This doesn't happen in
6585 normal AIX assembly code. */
6586 if (csect == NULL)
6587 csect = seg_info (symseg)->sym;
6589 return csect;
6592 return NULL;
6595 /* This is called to see whether a fixup should be adjusted to use a
6596 section symbol. We take the opportunity to change a fixup against
6597 a symbol in the TOC subsegment into a reloc against the
6598 corresponding .tc symbol. */
6601 ppc_fix_adjustable (fixS *fix)
6603 valueT val = resolve_symbol_value (fix->fx_addsy);
6604 segT symseg = S_GET_SEGMENT (fix->fx_addsy);
6605 symbolS* csect;
6607 if (symseg == absolute_section)
6608 return 0;
6610 /* Always adjust symbols in debugging sections. */
6611 if (bfd_section_flags (symseg) & SEC_DEBUGGING)
6612 return 1;
6614 if (ppc_toc_csect != (symbolS *) NULL
6615 && fix->fx_addsy != ppc_toc_csect
6616 && symseg == data_section
6617 && val >= ppc_toc_frag->fr_address
6618 && (ppc_after_toc_frag == (fragS *) NULL
6619 || val < ppc_after_toc_frag->fr_address))
6621 symbolS *sy;
6623 for (sy = symbol_next (ppc_toc_csect);
6624 sy != (symbolS *) NULL;
6625 sy = symbol_next (sy))
6627 TC_SYMFIELD_TYPE *sy_tc = symbol_get_tc (sy);
6629 if (sy_tc->symbol_class == XMC_TC0)
6630 continue;
6631 if (sy_tc->symbol_class != XMC_TC
6632 && sy_tc->symbol_class != XMC_TE)
6633 break;
6634 if (val == resolve_symbol_value (sy))
6636 fix->fx_addsy = sy;
6637 fix->fx_addnumber = val - ppc_toc_frag->fr_address;
6638 return 0;
6642 as_bad_where (fix->fx_file, fix->fx_line,
6643 _("symbol in .toc does not match any .tc"));
6646 /* Possibly adjust the reloc to be against the csect. */
6647 if ((csect = ppc_get_csect_to_adjust (fix->fx_addsy)) != NULL)
6649 fix->fx_offset += val - symbol_get_frag (csect)->fr_address;
6650 fix->fx_addsy = csect;
6653 if ((csect = ppc_get_csect_to_adjust (fix->fx_subsy)) != NULL)
6655 fix->fx_offset -= resolve_symbol_value (fix->fx_subsy)
6656 - symbol_get_frag (csect)->fr_address;
6657 fix->fx_subsy = csect;
6660 /* Adjust a reloc against a .lcomm symbol to be against the base
6661 .lcomm. */
6662 if (symseg == bss_section
6663 && ! S_IS_EXTERNAL (fix->fx_addsy)
6664 && symbol_get_tc (fix->fx_addsy)->subseg == 0)
6666 symbolS *sy = symbol_get_frag (fix->fx_addsy)->fr_symbol;
6668 fix->fx_offset += val - resolve_symbol_value (sy);
6669 fix->fx_addsy = sy;
6672 return 0;
6675 /* A reloc from one csect to another must be kept. The assembler
6676 will, of course, keep relocs between sections, and it will keep
6677 absolute relocs, but we need to force it to keep PC relative relocs
6678 between two csects in the same section. */
6681 ppc_force_relocation (fixS *fix)
6683 /* At this point fix->fx_addsy should already have been converted to
6684 a csect symbol. If the csect does not include the fragment, then
6685 we need to force the relocation. */
6686 if (fix->fx_pcrel
6687 && fix->fx_addsy != NULL
6688 && symbol_get_tc (fix->fx_addsy)->subseg != 0
6689 && ((symbol_get_frag (fix->fx_addsy)->fr_address
6690 > fix->fx_frag->fr_address)
6691 || (symbol_get_tc (fix->fx_addsy)->next != NULL
6692 && (symbol_get_frag (symbol_get_tc (fix->fx_addsy)->next)->fr_address
6693 <= fix->fx_frag->fr_address))))
6694 return 1;
6696 return generic_force_reloc (fix);
6698 #endif /* OBJ_XCOFF */
6700 #ifdef OBJ_ELF
6701 /* If this function returns non-zero, it guarantees that a relocation
6702 will be emitted for a fixup. */
6705 ppc_force_relocation (fixS *fix)
6707 switch (fix->fx_r_type)
6709 case BFD_RELOC_PPC_B16_BRTAKEN:
6710 case BFD_RELOC_PPC_B16_BRNTAKEN:
6711 case BFD_RELOC_PPC_BA16_BRTAKEN:
6712 case BFD_RELOC_PPC_BA16_BRNTAKEN:
6713 case BFD_RELOC_24_PLT_PCREL:
6714 case BFD_RELOC_PPC64_TOC:
6715 case BFD_RELOC_16_GOTOFF:
6716 case BFD_RELOC_LO16_GOTOFF:
6717 case BFD_RELOC_HI16_GOTOFF:
6718 case BFD_RELOC_HI16_S_GOTOFF:
6719 case BFD_RELOC_LO16_PLTOFF:
6720 case BFD_RELOC_HI16_PLTOFF:
6721 case BFD_RELOC_HI16_S_PLTOFF:
6722 case BFD_RELOC_GPREL16:
6723 case BFD_RELOC_16_BASEREL:
6724 case BFD_RELOC_LO16_BASEREL:
6725 case BFD_RELOC_HI16_BASEREL:
6726 case BFD_RELOC_HI16_S_BASEREL:
6727 case BFD_RELOC_PPC_TOC16:
6728 case BFD_RELOC_PPC64_TOC16_LO:
6729 case BFD_RELOC_PPC64_TOC16_HI:
6730 case BFD_RELOC_PPC64_TOC16_HA:
6731 case BFD_RELOC_PPC64_PLTGOT16:
6732 case BFD_RELOC_PPC64_PLTGOT16_LO:
6733 case BFD_RELOC_PPC64_PLTGOT16_HI:
6734 case BFD_RELOC_PPC64_PLTGOT16_HA:
6735 case BFD_RELOC_PPC64_GOT16_DS:
6736 case BFD_RELOC_PPC64_GOT16_LO_DS:
6737 case BFD_RELOC_PPC64_PLT16_LO_DS:
6738 case BFD_RELOC_PPC64_SECTOFF_DS:
6739 case BFD_RELOC_PPC64_SECTOFF_LO_DS:
6740 case BFD_RELOC_PPC64_TOC16_DS:
6741 case BFD_RELOC_PPC64_TOC16_LO_DS:
6742 case BFD_RELOC_PPC64_PLTGOT16_DS:
6743 case BFD_RELOC_PPC64_PLTGOT16_LO_DS:
6744 case BFD_RELOC_PPC_EMB_NADDR16:
6745 case BFD_RELOC_PPC_EMB_NADDR16_LO:
6746 case BFD_RELOC_PPC_EMB_NADDR16_HI:
6747 case BFD_RELOC_PPC_EMB_NADDR16_HA:
6748 case BFD_RELOC_PPC_EMB_SDAI16:
6749 case BFD_RELOC_PPC_EMB_SDA2I16:
6750 case BFD_RELOC_PPC_EMB_SDA2REL:
6751 case BFD_RELOC_PPC_EMB_SDA21:
6752 case BFD_RELOC_PPC_EMB_MRKREF:
6753 case BFD_RELOC_PPC_EMB_RELSEC16:
6754 case BFD_RELOC_PPC_EMB_RELST_LO:
6755 case BFD_RELOC_PPC_EMB_RELST_HI:
6756 case BFD_RELOC_PPC_EMB_RELST_HA:
6757 case BFD_RELOC_PPC_EMB_BIT_FLD:
6758 case BFD_RELOC_PPC_EMB_RELSDA:
6759 case BFD_RELOC_PPC_VLE_SDA21:
6760 case BFD_RELOC_PPC_VLE_SDA21_LO:
6761 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
6762 case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
6763 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
6764 case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
6765 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
6766 case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
6767 case BFD_RELOC_PPC64_PLT_PCREL34:
6768 case BFD_RELOC_PPC64_GOT_PCREL34:
6769 return 1;
6770 case BFD_RELOC_PPC_B26:
6771 case BFD_RELOC_PPC_BA26:
6772 case BFD_RELOC_PPC_B16:
6773 case BFD_RELOC_PPC_BA16:
6774 case BFD_RELOC_PPC64_REL24_NOTOC:
6775 case BFD_RELOC_PPC64_REL24_P9NOTOC:
6776 /* All branch fixups targeting a localentry symbol must
6777 force a relocation. */
6778 if (fix->fx_addsy)
6780 asymbol *bfdsym = symbol_get_bfdsym (fix->fx_addsy);
6781 elf_symbol_type *elfsym = elf_symbol_from (bfdsym);
6782 gas_assert (elfsym);
6783 if ((STO_PPC64_LOCAL_MASK & elfsym->internal_elf_sym.st_other) != 0)
6784 return 1;
6786 break;
6787 default:
6788 break;
6791 if (fix->fx_r_type >= BFD_RELOC_PPC_TLS
6792 && fix->fx_r_type <= BFD_RELOC_PPC64_TLS_PCREL)
6793 return 1;
6795 return generic_force_reloc (fix);
6799 ppc_fix_adjustable (fixS *fix)
6801 switch (fix->fx_r_type)
6803 /* All branch fixups targeting a localentry symbol must
6804 continue using the symbol. */
6805 case BFD_RELOC_PPC_B26:
6806 case BFD_RELOC_PPC_BA26:
6807 case BFD_RELOC_PPC_B16:
6808 case BFD_RELOC_PPC_BA16:
6809 case BFD_RELOC_PPC_B16_BRTAKEN:
6810 case BFD_RELOC_PPC_B16_BRNTAKEN:
6811 case BFD_RELOC_PPC_BA16_BRTAKEN:
6812 case BFD_RELOC_PPC_BA16_BRNTAKEN:
6813 case BFD_RELOC_PPC64_REL24_NOTOC:
6814 case BFD_RELOC_PPC64_REL24_P9NOTOC:
6815 if (fix->fx_addsy)
6817 asymbol *bfdsym = symbol_get_bfdsym (fix->fx_addsy);
6818 elf_symbol_type *elfsym = elf_symbol_from (bfdsym);
6819 gas_assert (elfsym);
6820 if ((STO_PPC64_LOCAL_MASK & elfsym->internal_elf_sym.st_other) != 0)
6821 return 0;
6823 break;
6824 default:
6825 break;
6828 return (fix->fx_r_type != BFD_RELOC_16_GOTOFF
6829 && fix->fx_r_type != BFD_RELOC_LO16_GOTOFF
6830 && fix->fx_r_type != BFD_RELOC_HI16_GOTOFF
6831 && fix->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
6832 && fix->fx_r_type != BFD_RELOC_PPC64_GOT16_DS
6833 && fix->fx_r_type != BFD_RELOC_PPC64_GOT16_LO_DS
6834 && fix->fx_r_type != BFD_RELOC_PPC64_GOT_PCREL34
6835 && fix->fx_r_type != BFD_RELOC_24_PLT_PCREL
6836 && fix->fx_r_type != BFD_RELOC_32_PLTOFF
6837 && fix->fx_r_type != BFD_RELOC_32_PLT_PCREL
6838 && fix->fx_r_type != BFD_RELOC_LO16_PLTOFF
6839 && fix->fx_r_type != BFD_RELOC_HI16_PLTOFF
6840 && fix->fx_r_type != BFD_RELOC_HI16_S_PLTOFF
6841 && fix->fx_r_type != BFD_RELOC_64_PLTOFF
6842 && fix->fx_r_type != BFD_RELOC_64_PLT_PCREL
6843 && fix->fx_r_type != BFD_RELOC_PPC64_PLT16_LO_DS
6844 && fix->fx_r_type != BFD_RELOC_PPC64_PLT_PCREL34
6845 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16
6846 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_LO
6847 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_HI
6848 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_HA
6849 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_DS
6850 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_LO_DS
6851 && fix->fx_r_type != BFD_RELOC_GPREL16
6852 && fix->fx_r_type != BFD_RELOC_PPC_VLE_SDAREL_LO16A
6853 && fix->fx_r_type != BFD_RELOC_PPC_VLE_SDAREL_HI16A
6854 && fix->fx_r_type != BFD_RELOC_PPC_VLE_SDAREL_HA16A
6855 && fix->fx_r_type != BFD_RELOC_VTABLE_INHERIT
6856 && fix->fx_r_type != BFD_RELOC_VTABLE_ENTRY
6857 && !(fix->fx_r_type >= BFD_RELOC_PPC_TLS
6858 && fix->fx_r_type <= BFD_RELOC_PPC64_TLS_PCREL));
6860 #endif
6862 void
6863 ppc_frag_check (struct frag *fragP)
6865 if ((fragP->fr_address & fragP->insn_addr) != 0)
6866 as_bad_where (fragP->fr_file, fragP->fr_line,
6867 _("instruction address is not a multiple of %d"),
6868 fragP->insn_addr + 1);
6871 /* rs_align_code frag handling. */
6873 enum ppc_nop_encoding_for_rs_align_code
6875 PPC_NOP_VANILLA,
6876 PPC_NOP_VLE,
6877 PPC_NOP_GROUP_P6,
6878 PPC_NOP_GROUP_P7
6881 unsigned int
6882 ppc_nop_select (void)
6884 if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
6885 return PPC_NOP_VLE;
6886 if ((ppc_cpu & (PPC_OPCODE_POWER9 | PPC_OPCODE_E500MC)) == 0)
6888 if ((ppc_cpu & PPC_OPCODE_POWER7) != 0)
6889 return PPC_NOP_GROUP_P7;
6890 if ((ppc_cpu & PPC_OPCODE_POWER6) != 0)
6891 return PPC_NOP_GROUP_P6;
6893 return PPC_NOP_VANILLA;
6896 void
6897 ppc_handle_align (struct frag *fragP)
6899 valueT count = (fragP->fr_next->fr_address
6900 - (fragP->fr_address + fragP->fr_fix));
6901 char *dest = fragP->fr_literal + fragP->fr_fix;
6902 enum ppc_nop_encoding_for_rs_align_code nop_select = *dest & 0xff;
6904 /* Pad with zeros if not inserting a whole number of instructions.
6905 We could pad with zeros up to an instruction boundary then follow
6906 with nops but odd counts indicate data in an executable section
6907 so padding with zeros is most appropriate. */
6908 if (count == 0
6909 || (nop_select == PPC_NOP_VLE ? (count & 1) != 0 : (count & 3) != 0))
6911 *dest = 0;
6912 return;
6915 if (nop_select == PPC_NOP_VLE)
6918 fragP->fr_var = 2;
6919 md_number_to_chars (dest, 0x4400, 2);
6921 else
6923 fragP->fr_var = 4;
6925 if (count > 4 * nop_limit && count < 0x2000000)
6927 struct frag *rest;
6929 /* Make a branch, then follow with nops. Insert another
6930 frag to handle the nops. */
6931 md_number_to_chars (dest, 0x48000000 + count, 4);
6932 count -= 4;
6933 if (count == 0)
6934 return;
6936 rest = xmalloc (SIZEOF_STRUCT_FRAG + 4);
6937 memcpy (rest, fragP, SIZEOF_STRUCT_FRAG);
6938 fragP->fr_next = rest;
6939 fragP = rest;
6940 rest->fr_address += rest->fr_fix + 4;
6941 rest->fr_fix = 0;
6942 /* If we leave the next frag as rs_align_code we'll come here
6943 again, resulting in a bunch of branches rather than a
6944 branch followed by nops. */
6945 rest->fr_type = rs_align;
6946 dest = rest->fr_literal;
6949 md_number_to_chars (dest, 0x60000000, 4);
6951 if (nop_select >= PPC_NOP_GROUP_P6)
6953 /* For power6, power7, and power8, we want the last nop to
6954 be a group terminating one. Do this by inserting an
6955 rs_fill frag immediately after this one, with its address
6956 set to the last nop location. This will automatically
6957 reduce the number of nops in the current frag by one. */
6958 if (count > 4)
6960 struct frag *group_nop = xmalloc (SIZEOF_STRUCT_FRAG + 4);
6962 memcpy (group_nop, fragP, SIZEOF_STRUCT_FRAG);
6963 group_nop->fr_address = group_nop->fr_next->fr_address - 4;
6964 group_nop->fr_fix = 0;
6965 group_nop->fr_offset = 1;
6966 group_nop->fr_type = rs_fill;
6967 fragP->fr_next = group_nop;
6968 dest = group_nop->fr_literal;
6971 if (nop_select == PPC_NOP_GROUP_P6)
6972 /* power6 group terminating nop: "ori 1,1,0". */
6973 md_number_to_chars (dest, 0x60210000, 4);
6974 else
6975 /* power7/power8 group terminating nop: "ori 2,2,0". */
6976 md_number_to_chars (dest, 0x60420000, 4);
6981 /* Apply a fixup to the object code. This is called for all the
6982 fixups we generated by the calls to fix_new_exp, above. */
6984 void
6985 md_apply_fix (fixS *fixP, valueT *valP, segT seg)
6987 valueT value = * valP;
6988 offsetT fieldval;
6989 const struct powerpc_operand *operand;
6991 #ifdef OBJ_ELF
6992 if (fixP->fx_addsy != NULL)
6994 /* Hack around bfd_install_relocation brain damage. */
6995 if (fixP->fx_pcrel)
6996 value += fixP->fx_frag->fr_address + fixP->fx_where;
6998 if (fixP->fx_addsy == abs_section_sym)
6999 fixP->fx_done = 1;
7001 else
7002 fixP->fx_done = 1;
7003 #else
7004 /* FIXME FIXME FIXME: The value we are passed in *valP includes
7005 the symbol values. If we are doing this relocation the code in
7006 write.c is going to call bfd_install_relocation, which is also
7007 going to use the symbol value. That means that if the reloc is
7008 fully resolved we want to use *valP since bfd_install_relocation is
7009 not being used.
7010 However, if the reloc is not fully resolved we do not want to
7011 use *valP, and must use fx_offset instead. If the relocation
7012 is PC-relative, we then need to re-apply md_pcrel_from_section
7013 to this new relocation value. */
7014 if (fixP->fx_addsy == (symbolS *) NULL)
7015 fixP->fx_done = 1;
7017 else
7019 value = fixP->fx_offset;
7020 if (fixP->fx_pcrel)
7021 value -= md_pcrel_from_section (fixP, seg);
7023 #endif
7025 /* We are only able to convert some relocs to pc-relative. */
7026 if (fixP->fx_pcrel)
7028 switch (fixP->fx_r_type)
7030 case BFD_RELOC_64:
7031 fixP->fx_r_type = BFD_RELOC_64_PCREL;
7032 break;
7034 case BFD_RELOC_32:
7035 fixP->fx_r_type = BFD_RELOC_32_PCREL;
7036 break;
7038 case BFD_RELOC_16:
7039 fixP->fx_r_type = BFD_RELOC_16_PCREL;
7040 break;
7042 case BFD_RELOC_LO16:
7043 fixP->fx_r_type = BFD_RELOC_LO16_PCREL;
7044 break;
7046 case BFD_RELOC_HI16:
7047 fixP->fx_r_type = BFD_RELOC_HI16_PCREL;
7048 break;
7050 case BFD_RELOC_HI16_S:
7051 fixP->fx_r_type = BFD_RELOC_HI16_S_PCREL;
7052 break;
7054 case BFD_RELOC_PPC64_ADDR16_HIGH:
7055 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGH;
7056 break;
7058 case BFD_RELOC_PPC64_ADDR16_HIGHA:
7059 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHA;
7060 break;
7062 case BFD_RELOC_PPC64_HIGHER:
7063 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHER;
7064 break;
7066 case BFD_RELOC_PPC64_HIGHER_S:
7067 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHERA;
7068 break;
7070 case BFD_RELOC_PPC64_HIGHEST:
7071 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHEST;
7072 break;
7074 case BFD_RELOC_PPC64_HIGHEST_S:
7075 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHESTA;
7076 break;
7078 case BFD_RELOC_PPC64_ADDR16_HIGHER34:
7079 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHER34;
7080 break;
7082 case BFD_RELOC_PPC64_ADDR16_HIGHERA34:
7083 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHERA34;
7084 break;
7086 case BFD_RELOC_PPC64_ADDR16_HIGHEST34:
7087 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHEST34;
7088 break;
7090 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:
7091 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHESTA34;
7092 break;
7094 case BFD_RELOC_PPC_16DX_HA:
7095 fixP->fx_r_type = BFD_RELOC_PPC_REL16DX_HA;
7096 break;
7098 case BFD_RELOC_PPC64_D34:
7099 fixP->fx_r_type = BFD_RELOC_PPC64_PCREL34;
7100 break;
7102 case BFD_RELOC_PPC64_D28:
7103 fixP->fx_r_type = BFD_RELOC_PPC64_PCREL28;
7104 break;
7106 default:
7107 break;
7110 else if (!fixP->fx_done
7111 && fixP->fx_r_type == BFD_RELOC_PPC_16DX_HA)
7113 /* addpcis is relative to next insn address. */
7114 value -= 4;
7115 fixP->fx_r_type = BFD_RELOC_PPC_REL16DX_HA;
7116 fixP->fx_pcrel = 1;
7119 operand = NULL;
7120 if (fixP->fx_pcrel_adjust != 0)
7122 /* This is a fixup on an instruction. */
7123 ppc_opindex_t opindex = fixP->fx_pcrel_adjust & PPC_OPINDEX_MAX;
7125 operand = &powerpc_operands[opindex];
7126 #ifdef OBJ_XCOFF
7127 /* An instruction like `lwz 9,sym(30)' when `sym' is not a TOC symbol
7128 does not generate a reloc. It uses the offset of `sym' within its
7129 csect. Other usages, such as `.long sym', generate relocs. This
7130 is the documented behaviour of non-TOC symbols. */
7131 if ((operand->flags & PPC_OPERAND_PARENS) != 0
7132 && (operand->bitm & 0xfff0) == 0xfff0
7133 && operand->shift == 0
7134 && (operand->insert == NULL || ppc_obj64)
7135 && fixP->fx_addsy != NULL
7136 && symbol_get_tc (fixP->fx_addsy)->subseg != 0
7137 && !ppc_is_toc_sym (fixP->fx_addsy)
7138 && S_GET_SEGMENT (fixP->fx_addsy) != bss_section)
7140 value = fixP->fx_offset;
7141 fixP->fx_done = 1;
7144 /* During parsing of instructions, a TOC16 reloc is generated for
7145 instructions such as 'lwz RT,SYM(RB)' if SYM is a symbol defined
7146 in the toc. But at parse time, SYM may be not yet defined, so
7147 check again here. */
7148 if (fixP->fx_r_type == BFD_RELOC_16
7149 && fixP->fx_addsy != NULL
7150 && ppc_is_toc_sym (fixP->fx_addsy))
7151 fixP->fx_r_type = BFD_RELOC_PPC_TOC16;
7152 #endif
7155 /* Calculate value to be stored in field. */
7156 fieldval = value;
7157 switch (fixP->fx_r_type)
7159 #ifdef OBJ_ELF
7160 case BFD_RELOC_PPC64_ADDR16_LO_DS:
7161 case BFD_RELOC_PPC_VLE_LO16A:
7162 case BFD_RELOC_PPC_VLE_LO16D:
7163 #endif
7164 case BFD_RELOC_LO16:
7165 case BFD_RELOC_LO16_PCREL:
7166 fieldval = value & 0xffff;
7167 sign_extend_16:
7168 if (operand != NULL && (operand->flags & PPC_OPERAND_SIGNED) != 0)
7169 fieldval = SEX16 (fieldval);
7170 fixP->fx_no_overflow = 1;
7171 break;
7173 case BFD_RELOC_HI16:
7174 case BFD_RELOC_HI16_PCREL:
7175 #ifdef OBJ_ELF
7176 if (REPORT_OVERFLOW_HI && ppc_obj64)
7178 fieldval = value >> 16;
7179 if (operand != NULL && (operand->flags & PPC_OPERAND_SIGNED) != 0)
7181 valueT sign = (((valueT) -1 >> 16) + 1) >> 1;
7182 fieldval = ((valueT) fieldval ^ sign) - sign;
7184 break;
7186 /* Fallthru */
7188 case BFD_RELOC_PPC_VLE_HI16A:
7189 case BFD_RELOC_PPC_VLE_HI16D:
7190 case BFD_RELOC_PPC64_ADDR16_HIGH:
7191 #endif
7192 fieldval = PPC_HI (value);
7193 goto sign_extend_16;
7195 case BFD_RELOC_HI16_S:
7196 case BFD_RELOC_HI16_S_PCREL:
7197 case BFD_RELOC_PPC_16DX_HA:
7198 case BFD_RELOC_PPC_REL16DX_HA:
7199 #ifdef OBJ_ELF
7200 if (REPORT_OVERFLOW_HI && ppc_obj64)
7202 fieldval = (value + 0x8000) >> 16;
7203 if (operand != NULL && (operand->flags & PPC_OPERAND_SIGNED) != 0)
7205 valueT sign = (((valueT) -1 >> 16) + 1) >> 1;
7206 fieldval = ((valueT) fieldval ^ sign) - sign;
7208 break;
7210 /* Fallthru */
7212 case BFD_RELOC_PPC_VLE_HA16A:
7213 case BFD_RELOC_PPC_VLE_HA16D:
7214 case BFD_RELOC_PPC64_ADDR16_HIGHA:
7215 #endif
7216 fieldval = PPC_HA (value);
7217 goto sign_extend_16;
7219 #ifdef OBJ_ELF
7220 case BFD_RELOC_PPC64_HIGHER:
7221 fieldval = PPC_HIGHER (value);
7222 goto sign_extend_16;
7224 case BFD_RELOC_PPC64_HIGHER_S:
7225 fieldval = PPC_HIGHERA (value);
7226 goto sign_extend_16;
7228 case BFD_RELOC_PPC64_HIGHEST:
7229 fieldval = PPC_HIGHEST (value);
7230 goto sign_extend_16;
7232 case BFD_RELOC_PPC64_HIGHEST_S:
7233 fieldval = PPC_HIGHESTA (value);
7234 goto sign_extend_16;
7235 #endif
7237 default:
7238 break;
7241 if (operand != NULL)
7243 /* Handle relocs in an insn. */
7244 switch (fixP->fx_r_type)
7246 #ifdef OBJ_ELF
7247 /* The following relocs can't be calculated by the assembler.
7248 Leave the field zero. */
7249 case BFD_RELOC_PPC_TPREL16:
7250 case BFD_RELOC_PPC_TPREL16_LO:
7251 case BFD_RELOC_PPC_TPREL16_HI:
7252 case BFD_RELOC_PPC_TPREL16_HA:
7253 case BFD_RELOC_PPC_DTPREL16:
7254 case BFD_RELOC_PPC_DTPREL16_LO:
7255 case BFD_RELOC_PPC_DTPREL16_HI:
7256 case BFD_RELOC_PPC_DTPREL16_HA:
7257 case BFD_RELOC_PPC_GOT_TLSGD16:
7258 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
7259 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
7260 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
7261 case BFD_RELOC_PPC_GOT_TLSLD16:
7262 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
7263 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
7264 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
7265 case BFD_RELOC_PPC_GOT_TPREL16:
7266 case BFD_RELOC_PPC_GOT_TPREL16_LO:
7267 case BFD_RELOC_PPC_GOT_TPREL16_HI:
7268 case BFD_RELOC_PPC_GOT_TPREL16_HA:
7269 case BFD_RELOC_PPC_GOT_DTPREL16:
7270 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
7271 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
7272 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
7273 case BFD_RELOC_PPC64_TPREL16_DS:
7274 case BFD_RELOC_PPC64_TPREL16_LO_DS:
7275 case BFD_RELOC_PPC64_TPREL16_HIGH:
7276 case BFD_RELOC_PPC64_TPREL16_HIGHA:
7277 case BFD_RELOC_PPC64_TPREL16_HIGHER:
7278 case BFD_RELOC_PPC64_TPREL16_HIGHERA:
7279 case BFD_RELOC_PPC64_TPREL16_HIGHEST:
7280 case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
7281 case BFD_RELOC_PPC64_DTPREL16_HIGH:
7282 case BFD_RELOC_PPC64_DTPREL16_HIGHA:
7283 case BFD_RELOC_PPC64_DTPREL16_DS:
7284 case BFD_RELOC_PPC64_DTPREL16_LO_DS:
7285 case BFD_RELOC_PPC64_DTPREL16_HIGHER:
7286 case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
7287 case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
7288 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
7289 case BFD_RELOC_PPC64_TPREL34:
7290 case BFD_RELOC_PPC64_DTPREL34:
7291 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:
7292 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:
7293 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:
7294 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:
7295 gas_assert (fixP->fx_addsy != NULL);
7296 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7297 fieldval = 0;
7298 break;
7300 /* These also should leave the field zero for the same
7301 reason. Note that older versions of gas wrote values
7302 here. If we want to go back to the old behaviour, then
7303 all _LO and _LO_DS cases will need to be treated like
7304 BFD_RELOC_LO16_PCREL above. Similarly for _HI etc. */
7305 case BFD_RELOC_16_GOTOFF:
7306 case BFD_RELOC_LO16_GOTOFF:
7307 case BFD_RELOC_HI16_GOTOFF:
7308 case BFD_RELOC_HI16_S_GOTOFF:
7309 case BFD_RELOC_LO16_PLTOFF:
7310 case BFD_RELOC_HI16_PLTOFF:
7311 case BFD_RELOC_HI16_S_PLTOFF:
7312 case BFD_RELOC_GPREL16:
7313 case BFD_RELOC_16_BASEREL:
7314 case BFD_RELOC_LO16_BASEREL:
7315 case BFD_RELOC_HI16_BASEREL:
7316 case BFD_RELOC_HI16_S_BASEREL:
7317 case BFD_RELOC_PPC_TOC16:
7318 case BFD_RELOC_PPC64_TOC16_LO:
7319 case BFD_RELOC_PPC64_TOC16_HI:
7320 case BFD_RELOC_PPC64_TOC16_HA:
7321 case BFD_RELOC_PPC64_PLTGOT16:
7322 case BFD_RELOC_PPC64_PLTGOT16_LO:
7323 case BFD_RELOC_PPC64_PLTGOT16_HI:
7324 case BFD_RELOC_PPC64_PLTGOT16_HA:
7325 case BFD_RELOC_PPC64_GOT16_DS:
7326 case BFD_RELOC_PPC64_GOT16_LO_DS:
7327 case BFD_RELOC_PPC64_PLT16_LO_DS:
7328 case BFD_RELOC_PPC64_SECTOFF_DS:
7329 case BFD_RELOC_PPC64_SECTOFF_LO_DS:
7330 case BFD_RELOC_PPC64_TOC16_DS:
7331 case BFD_RELOC_PPC64_TOC16_LO_DS:
7332 case BFD_RELOC_PPC64_PLTGOT16_DS:
7333 case BFD_RELOC_PPC64_PLTGOT16_LO_DS:
7334 case BFD_RELOC_PPC_EMB_NADDR16:
7335 case BFD_RELOC_PPC_EMB_NADDR16_LO:
7336 case BFD_RELOC_PPC_EMB_NADDR16_HI:
7337 case BFD_RELOC_PPC_EMB_NADDR16_HA:
7338 case BFD_RELOC_PPC_EMB_SDAI16:
7339 case BFD_RELOC_PPC_EMB_SDA2I16:
7340 case BFD_RELOC_PPC_EMB_SDA2REL:
7341 case BFD_RELOC_PPC_EMB_SDA21:
7342 case BFD_RELOC_PPC_EMB_MRKREF:
7343 case BFD_RELOC_PPC_EMB_RELSEC16:
7344 case BFD_RELOC_PPC_EMB_RELST_LO:
7345 case BFD_RELOC_PPC_EMB_RELST_HI:
7346 case BFD_RELOC_PPC_EMB_RELST_HA:
7347 case BFD_RELOC_PPC_EMB_BIT_FLD:
7348 case BFD_RELOC_PPC_EMB_RELSDA:
7349 case BFD_RELOC_PPC_VLE_SDA21:
7350 case BFD_RELOC_PPC_VLE_SDA21_LO:
7351 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
7352 case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
7353 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
7354 case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
7355 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
7356 case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
7357 case BFD_RELOC_PPC64_GOT_PCREL34:
7358 case BFD_RELOC_PPC64_PLT_PCREL34:
7359 gas_assert (fixP->fx_addsy != NULL);
7360 /* Fallthru */
7362 case BFD_RELOC_PPC_TLS:
7363 case BFD_RELOC_PPC_TLSGD:
7364 case BFD_RELOC_PPC_TLSLD:
7365 case BFD_RELOC_PPC64_TLS_PCREL:
7366 fieldval = 0;
7367 break;
7368 #endif
7370 #ifdef OBJ_XCOFF
7371 case BFD_RELOC_PPC_B16:
7372 /* Adjust the offset to the instruction boundary. */
7373 fieldval += 2;
7374 break;
7375 #endif
7377 case BFD_RELOC_VTABLE_INHERIT:
7378 case BFD_RELOC_VTABLE_ENTRY:
7379 case BFD_RELOC_PPC_DTPMOD:
7380 case BFD_RELOC_PPC_TPREL:
7381 case BFD_RELOC_PPC_DTPREL:
7382 case BFD_RELOC_PPC_COPY:
7383 case BFD_RELOC_PPC_GLOB_DAT:
7384 case BFD_RELOC_32_PLT_PCREL:
7385 case BFD_RELOC_PPC_EMB_NADDR32:
7386 case BFD_RELOC_PPC64_TOC:
7387 case BFD_RELOC_CTOR:
7388 case BFD_RELOC_32:
7389 case BFD_RELOC_32_PCREL:
7390 case BFD_RELOC_RVA:
7391 case BFD_RELOC_64:
7392 case BFD_RELOC_64_PCREL:
7393 case BFD_RELOC_PPC64_ADDR64_LOCAL:
7394 as_bad_where (fixP->fx_file, fixP->fx_line,
7395 _("%s unsupported as instruction fixup"),
7396 bfd_get_reloc_code_name (fixP->fx_r_type));
7397 fixP->fx_done = 1;
7398 return;
7400 default:
7401 break;
7404 #ifdef OBJ_ELF
7405 /* powerpc uses RELA style relocs, so if emitting a reloc the field
7406 contents can stay at zero. */
7407 #define APPLY_RELOC fixP->fx_done
7408 #else
7409 #define APPLY_RELOC 1
7410 #endif
7411 /* We need to call the insert function even when fieldval is
7412 zero if the insert function would translate that zero to a
7413 bit pattern other than all zeros. */
7414 if ((fieldval != 0 && APPLY_RELOC) || operand->insert != NULL)
7416 uint64_t insn;
7417 unsigned char *where;
7419 /* Fetch the instruction, insert the fully resolved operand
7420 value, and stuff the instruction back again. */
7421 where = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
7422 if (target_big_endian)
7424 if (fixP->fx_size < 4)
7425 insn = bfd_getb16 (where);
7426 else
7428 insn = bfd_getb32 (where);
7429 if (fixP->fx_size > 4)
7430 insn = insn << 32 | bfd_getb32 (where + 4);
7433 else
7435 if (fixP->fx_size < 4)
7436 insn = bfd_getl16 (where);
7437 else
7439 insn = bfd_getl32 (where);
7440 if (fixP->fx_size > 4)
7441 insn = insn << 32 | bfd_getl32 (where + 4);
7444 insn = ppc_insert_operand (insn, operand, fieldval,
7445 fixP->tc_fix_data.ppc_cpu,
7446 fixP->fx_file, fixP->fx_line);
7447 if (target_big_endian)
7449 if (fixP->fx_size < 4)
7450 bfd_putb16 (insn, where);
7451 else
7453 if (fixP->fx_size > 4)
7455 bfd_putb32 (insn, where + 4);
7456 insn >>= 32;
7458 bfd_putb32 (insn, where);
7461 else
7463 if (fixP->fx_size < 4)
7464 bfd_putl16 (insn, where);
7465 else
7467 if (fixP->fx_size > 4)
7469 bfd_putl32 (insn, where + 4);
7470 insn >>= 32;
7472 bfd_putl32 (insn, where);
7477 if (fixP->fx_done)
7478 /* Nothing else to do here. */
7479 return;
7481 gas_assert (fixP->fx_addsy != NULL);
7482 if (fixP->fx_r_type == BFD_RELOC_NONE)
7484 const char *sfile;
7485 unsigned int sline;
7487 /* Use expr_symbol_where to see if this is an expression
7488 symbol. */
7489 if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
7490 as_bad_where (fixP->fx_file, fixP->fx_line,
7491 _("unresolved expression that must be resolved"));
7492 else
7493 as_bad_where (fixP->fx_file, fixP->fx_line,
7494 _("unsupported relocation against %s"),
7495 S_GET_NAME (fixP->fx_addsy));
7496 fixP->fx_done = 1;
7497 return;
7500 else
7502 /* Handle relocs in data. */
7503 switch (fixP->fx_r_type)
7505 case BFD_RELOC_VTABLE_INHERIT:
7506 if (fixP->fx_addsy
7507 && !S_IS_DEFINED (fixP->fx_addsy)
7508 && !S_IS_WEAK (fixP->fx_addsy))
7509 S_SET_WEAK (fixP->fx_addsy);
7510 /* Fallthru */
7512 case BFD_RELOC_VTABLE_ENTRY:
7513 fixP->fx_done = 0;
7514 break;
7516 #ifdef OBJ_ELF
7517 /* These can appear with @l etc. in data. */
7518 case BFD_RELOC_LO16:
7519 case BFD_RELOC_LO16_PCREL:
7520 case BFD_RELOC_HI16:
7521 case BFD_RELOC_HI16_PCREL:
7522 case BFD_RELOC_HI16_S:
7523 case BFD_RELOC_HI16_S_PCREL:
7524 case BFD_RELOC_PPC64_HIGHER:
7525 case BFD_RELOC_PPC64_HIGHER_S:
7526 case BFD_RELOC_PPC64_HIGHEST:
7527 case BFD_RELOC_PPC64_HIGHEST_S:
7528 case BFD_RELOC_PPC64_ADDR16_HIGH:
7529 case BFD_RELOC_PPC64_ADDR16_HIGHA:
7530 case BFD_RELOC_PPC64_ADDR64_LOCAL:
7531 break;
7533 case BFD_RELOC_PPC_DTPMOD:
7534 case BFD_RELOC_PPC_TPREL:
7535 case BFD_RELOC_PPC_DTPREL:
7536 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7537 break;
7539 /* Just punt all of these to the linker. */
7540 case BFD_RELOC_PPC_B16_BRTAKEN:
7541 case BFD_RELOC_PPC_B16_BRNTAKEN:
7542 case BFD_RELOC_16_GOTOFF:
7543 case BFD_RELOC_LO16_GOTOFF:
7544 case BFD_RELOC_HI16_GOTOFF:
7545 case BFD_RELOC_HI16_S_GOTOFF:
7546 case BFD_RELOC_LO16_PLTOFF:
7547 case BFD_RELOC_HI16_PLTOFF:
7548 case BFD_RELOC_HI16_S_PLTOFF:
7549 case BFD_RELOC_PPC_COPY:
7550 case BFD_RELOC_PPC_GLOB_DAT:
7551 case BFD_RELOC_16_BASEREL:
7552 case BFD_RELOC_LO16_BASEREL:
7553 case BFD_RELOC_HI16_BASEREL:
7554 case BFD_RELOC_HI16_S_BASEREL:
7555 case BFD_RELOC_PPC_TLS:
7556 case BFD_RELOC_PPC_DTPREL16_LO:
7557 case BFD_RELOC_PPC_DTPREL16_HI:
7558 case BFD_RELOC_PPC_DTPREL16_HA:
7559 case BFD_RELOC_PPC_TPREL16_LO:
7560 case BFD_RELOC_PPC_TPREL16_HI:
7561 case BFD_RELOC_PPC_TPREL16_HA:
7562 case BFD_RELOC_PPC_GOT_TLSGD16:
7563 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
7564 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
7565 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
7566 case BFD_RELOC_PPC_GOT_TLSLD16:
7567 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
7568 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
7569 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
7570 case BFD_RELOC_PPC_GOT_DTPREL16:
7571 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
7572 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
7573 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
7574 case BFD_RELOC_PPC_GOT_TPREL16:
7575 case BFD_RELOC_PPC_GOT_TPREL16_LO:
7576 case BFD_RELOC_PPC_GOT_TPREL16_HI:
7577 case BFD_RELOC_PPC_GOT_TPREL16_HA:
7578 case BFD_RELOC_24_PLT_PCREL:
7579 case BFD_RELOC_PPC_LOCAL24PC:
7580 case BFD_RELOC_32_PLT_PCREL:
7581 case BFD_RELOC_GPREL16:
7582 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
7583 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
7584 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
7585 case BFD_RELOC_PPC_EMB_NADDR32:
7586 case BFD_RELOC_PPC_EMB_NADDR16:
7587 case BFD_RELOC_PPC_EMB_NADDR16_LO:
7588 case BFD_RELOC_PPC_EMB_NADDR16_HI:
7589 case BFD_RELOC_PPC_EMB_NADDR16_HA:
7590 case BFD_RELOC_PPC_EMB_SDAI16:
7591 case BFD_RELOC_PPC_EMB_SDA2REL:
7592 case BFD_RELOC_PPC_EMB_SDA2I16:
7593 case BFD_RELOC_PPC_EMB_SDA21:
7594 case BFD_RELOC_PPC_VLE_SDA21_LO:
7595 case BFD_RELOC_PPC_EMB_MRKREF:
7596 case BFD_RELOC_PPC_EMB_RELSEC16:
7597 case BFD_RELOC_PPC_EMB_RELST_LO:
7598 case BFD_RELOC_PPC_EMB_RELST_HI:
7599 case BFD_RELOC_PPC_EMB_RELST_HA:
7600 case BFD_RELOC_PPC_EMB_BIT_FLD:
7601 case BFD_RELOC_PPC_EMB_RELSDA:
7602 case BFD_RELOC_PPC64_TOC:
7603 case BFD_RELOC_PPC_TOC16:
7604 case BFD_RELOC_PPC_TOC16_LO:
7605 case BFD_RELOC_PPC_TOC16_HI:
7606 case BFD_RELOC_PPC64_TOC16_LO:
7607 case BFD_RELOC_PPC64_TOC16_HI:
7608 case BFD_RELOC_PPC64_TOC16_HA:
7609 case BFD_RELOC_PPC64_DTPREL16_HIGH:
7610 case BFD_RELOC_PPC64_DTPREL16_HIGHA:
7611 case BFD_RELOC_PPC64_DTPREL16_HIGHER:
7612 case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
7613 case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
7614 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
7615 case BFD_RELOC_PPC64_TPREL16_HIGH:
7616 case BFD_RELOC_PPC64_TPREL16_HIGHA:
7617 case BFD_RELOC_PPC64_TPREL16_HIGHER:
7618 case BFD_RELOC_PPC64_TPREL16_HIGHERA:
7619 case BFD_RELOC_PPC64_TPREL16_HIGHEST:
7620 case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
7621 case BFD_RELOC_PPC64_TLS_PCREL:
7622 fixP->fx_done = 0;
7623 break;
7624 #endif
7626 #ifdef OBJ_XCOFF
7627 case BFD_RELOC_PPC_TLSGD:
7628 case BFD_RELOC_PPC_TLSLD:
7629 case BFD_RELOC_PPC_TLSLE:
7630 case BFD_RELOC_PPC_TLSIE:
7631 case BFD_RELOC_PPC_TLSM:
7632 case BFD_RELOC_PPC64_TLSGD:
7633 case BFD_RELOC_PPC64_TLSLD:
7634 case BFD_RELOC_PPC64_TLSLE:
7635 case BFD_RELOC_PPC64_TLSIE:
7636 case BFD_RELOC_PPC64_TLSM:
7637 gas_assert (fixP->fx_addsy != NULL);
7638 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7639 break;
7641 /* Officially, R_TLSML relocations must be from a TOC entry
7642 targeting itself. In practice, this TOC entry is always
7643 named (or .rename) "_$TLSML".
7644 Thus, as it doesn't seem possible to retrieve the symbol
7645 being relocated here, we simply check that the symbol
7646 targeted by R_TLSML is indeed a TOC entry named "_$TLSML".
7647 FIXME: Find a way to correctly check R_TLSML relocations
7648 as described above. */
7649 case BFD_RELOC_PPC_TLSML:
7650 case BFD_RELOC_PPC64_TLSML:
7651 gas_assert (fixP->fx_addsy != NULL);
7652 if ((symbol_get_tc (fixP->fx_addsy)->symbol_class != XMC_TC
7653 || symbol_get_tc (fixP->fx_addsy)->symbol_class != XMC_TE)
7654 && strcmp (symbol_get_tc (fixP->fx_addsy)->real_name, "_$TLSML") != 0)
7655 as_bad_where (fixP->fx_file, fixP->fx_line,
7656 _("R_TLSML relocation doesn't target a "
7657 "TOC entry named \"_$TLSML\": %s"), S_GET_NAME(fixP->fx_addsy));
7658 fieldval = 0;
7659 break;
7661 case BFD_RELOC_NONE:
7662 #endif
7663 case BFD_RELOC_CTOR:
7664 case BFD_RELOC_32:
7665 case BFD_RELOC_32_PCREL:
7666 case BFD_RELOC_RVA:
7667 case BFD_RELOC_64:
7668 case BFD_RELOC_64_PCREL:
7669 case BFD_RELOC_16:
7670 case BFD_RELOC_16_PCREL:
7671 case BFD_RELOC_8:
7672 break;
7674 default:
7675 fprintf (stderr,
7676 _("Gas failure, reloc value %d\n"), fixP->fx_r_type);
7677 fflush (stderr);
7678 abort ();
7681 if (fixP->fx_size && APPLY_RELOC)
7682 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
7683 fieldval, fixP->fx_size);
7684 if (warn_476
7685 && (seg->flags & SEC_CODE) != 0
7686 && fixP->fx_size == 4
7687 && fixP->fx_done
7688 && !fixP->fx_tcbit
7689 && (fixP->fx_r_type == BFD_RELOC_32
7690 || fixP->fx_r_type == BFD_RELOC_CTOR
7691 || fixP->fx_r_type == BFD_RELOC_32_PCREL))
7692 as_warn_where (fixP->fx_file, fixP->fx_line,
7693 _("data in executable section"));
7696 #ifdef OBJ_ELF
7697 ppc_elf_validate_fix (fixP, seg);
7698 fixP->fx_addnumber = value;
7700 /* PowerPC uses RELA relocs, ie. the reloc addend is stored separately
7701 from the section contents. If we are going to be emitting a reloc
7702 then the section contents are immaterial, so don't warn if they
7703 happen to overflow. Leave such warnings to ld. */
7704 if (!fixP->fx_done)
7706 fixP->fx_no_overflow = 1;
7708 /* Arrange to emit .TOC. as a normal symbol if used in anything
7709 but .TOC.@tocbase. */
7710 if (ppc_obj64
7711 && fixP->fx_r_type != BFD_RELOC_PPC64_TOC
7712 && fixP->fx_addsy != NULL
7713 && strcmp (S_GET_NAME (fixP->fx_addsy), ".TOC.") == 0)
7714 symbol_get_bfdsym (fixP->fx_addsy)->flags |= BSF_KEEP;
7716 #else
7717 if (fixP->fx_r_type == BFD_RELOC_PPC_TOC16
7718 || fixP->fx_r_type == BFD_RELOC_PPC_TOC16_HI
7719 || fixP->fx_r_type == BFD_RELOC_PPC_TOC16_LO)
7721 /* We want to use the offset within the toc, not the actual VMA
7722 of the symbol. */
7723 fixP->fx_addnumber = (- bfd_section_vma (S_GET_SEGMENT (fixP->fx_addsy))
7724 - S_GET_VALUE (ppc_toc_csect));
7726 /* The high bits must be adjusted for the low bits being signed. */
7727 if (fixP->fx_r_type == BFD_RELOC_PPC_TOC16_HI) {
7728 fixP->fx_addnumber += 0x8000;
7731 /* Set *valP to avoid errors. */
7732 *valP = value;
7734 else if (fixP->fx_r_type == BFD_RELOC_PPC_TLSM
7735 || fixP->fx_r_type == BFD_RELOC_PPC64_TLSM
7736 || fixP->fx_r_type == BFD_RELOC_PPC_TLSML
7737 || fixP->fx_r_type == BFD_RELOC_PPC64_TLSML)
7738 /* AIX ld expects the section contents for these relocations
7739 to be zero. Arrange for that to occur when
7740 bfd_install_relocation is called. */
7741 fixP->fx_addnumber = (- bfd_section_vma (S_GET_SEGMENT (fixP->fx_addsy))
7742 - S_GET_VALUE (fixP->fx_addsy)
7743 - fieldval);
7744 else
7745 fixP->fx_addnumber = 0;
7746 #endif
7749 /* Generate a reloc for a fixup. */
7751 arelent **
7752 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
7754 static arelent *relocs[3];
7755 arelent *reloc;
7757 relocs[0] = reloc = XNEW (arelent);
7758 relocs[1] = NULL;
7760 reloc->sym_ptr_ptr = XNEW (asymbol *);
7761 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
7762 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
7763 /* BFD_RELOC_PPC64_TLS_PCREL generates R_PPC64_TLS with an odd r_offset. */
7764 if (fixp->fx_r_type == BFD_RELOC_PPC64_TLS_PCREL)
7765 reloc->address++;
7766 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
7767 if (reloc->howto == (reloc_howto_type *) NULL)
7769 as_bad_where (fixp->fx_file, fixp->fx_line,
7770 _("reloc %d not supported by object file format"),
7771 (int) fixp->fx_r_type);
7772 relocs[0] = NULL;
7774 reloc->addend = fixp->fx_addnumber;
7776 if (fixp->fx_subsy != NULL)
7778 relocs[1] = reloc = XNEW (arelent);
7779 relocs[2] = NULL;
7781 reloc->sym_ptr_ptr = XNEW (asymbol *);
7782 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_subsy);
7783 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
7785 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_PPC_NEG);
7786 reloc->addend = fixp->fx_addnumber;
7788 if (reloc->howto == (reloc_howto_type *) NULL)
7790 as_bad_subtract (fixp);
7791 free (relocs[1]->sym_ptr_ptr);
7792 free (relocs[1]);
7793 free (relocs[0]->sym_ptr_ptr);
7794 free (relocs[0]);
7795 relocs[0] = NULL;
7800 return relocs;
7803 void
7804 ppc_cfi_frame_initial_instructions (void)
7806 cfi_add_CFA_def_cfa (1, 0);
7810 tc_ppc_regname_to_dw2regnum (char *regname)
7812 unsigned int regnum = -1;
7813 unsigned int i;
7814 const char *p;
7815 char *q;
7816 static struct { const char *name; int dw2regnum; } regnames[] =
7818 { "sp", 1 }, { "r.sp", 1 }, { "rtoc", 2 }, { "r.toc", 2 },
7819 { "mq", 64 }, { "lr", 65 }, { "ctr", 66 }, { "ap", 67 },
7820 { "cr", 70 }, { "xer", 76 }, { "vrsave", 109 }, { "vscr", 110 },
7821 { "spe_acc", 111 }, { "spefscr", 112 }
7824 for (i = 0; i < ARRAY_SIZE (regnames); ++i)
7825 if (strcmp (regnames[i].name, regname) == 0)
7826 return regnames[i].dw2regnum;
7828 if (regname[0] == 'r' || regname[0] == 'f' || regname[0] == 'v')
7830 p = regname + 1 + (regname[1] == '.');
7831 regnum = strtoul (p, &q, 10);
7832 if (p == q || *q || regnum >= 32)
7833 return -1;
7834 if (regname[0] == 'f')
7835 regnum += 32;
7836 else if (regname[0] == 'v')
7837 regnum += 77;
7839 else if (regname[0] == 'c' && regname[1] == 'r')
7841 p = regname + 2 + (regname[2] == '.');
7842 if (p[0] < '0' || p[0] > '7' || p[1])
7843 return -1;
7844 regnum = p[0] - '0' + 68;
7846 return regnum;