]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-ppc.c
Use startswith more for strncmp function calls.
[thirdparty/binutils-gdb.git] / gas / config / tc-ppc.c
CommitLineData
252b5132 1/* tc-ppc.c -- Assemble for the PowerPC or POWER (RS/6000)
250d07de 2 Copyright (C) 1994-2021 Free Software Foundation, Inc.
252b5132
RH
3 Written by Ian Lance Taylor, Cygnus Support.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
ec2655a6 9 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
252b5132 21
252b5132 22#include "as.h"
3882b010 23#include "safe-ctype.h"
252b5132 24#include "subsegs.h"
75e21f08 25#include "dw2gencfi.h"
252b5132
RH
26#include "opcode/ppc.h"
27
28#ifdef OBJ_ELF
29#include "elf/ppc.h"
ee67d69a 30#include "elf/ppc64.h"
5d6f4f16 31#include "dwarf2dbg.h"
252b5132
RH
32#endif
33
85645aed
TG
34#ifdef OBJ_XCOFF
35#include "coff/xcoff.h"
36#include "libxcoff.h"
37#endif
38
252b5132
RH
39/* This is the assembler for the PowerPC or POWER (RS/6000) chips. */
40
41/* Tell the main code what the endianness is. */
42extern int target_big_endian;
43
44/* Whether or not, we've set target_big_endian. */
45static int set_target_endian = 0;
46
47/* Whether to use user friendly register names. */
48#ifndef TARGET_REG_NAMES_P
5b7c81bd 49#define TARGET_REG_NAMES_P false
252b5132 50#endif
252b5132 51
0baf16f2
AM
52/* Macros for calculating LO, HI, HA, HIGHER, HIGHERA, HIGHEST,
53 HIGHESTA. */
54
55/* #lo(value) denotes the least significant 16 bits of the indicated. */
56#define PPC_LO(v) ((v) & 0xffff)
57
58/* #hi(value) denotes bits 16 through 31 of the indicated value. */
59#define PPC_HI(v) (((v) >> 16) & 0xffff)
60
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. */
15c1449b 64#define PPC_HA(v) PPC_HI ((v) + 0x8000)
0baf16f2
AM
65
66/* #higher(value) denotes bits 32 through 47 of the indicated value. */
2a98c3a6 67#define PPC_HIGHER(v) (((v) >> 16 >> 16) & 0xffff)
0baf16f2
AM
68
69/* #highera(value) denotes bits 32 through 47 of the indicated value,
70 compensating for #lo() being treated as a signed number. */
15c1449b 71#define PPC_HIGHERA(v) PPC_HIGHER ((v) + 0x8000)
0baf16f2
AM
72
73/* #highest(value) denotes bits 48 through 63 of the indicated value. */
2a98c3a6 74#define PPC_HIGHEST(v) (((v) >> 24 >> 24) & 0xffff)
0baf16f2
AM
75
76/* #highesta(value) denotes bits 48 through 63 of the indicated value,
15c1449b
AM
77 compensating for #lo being treated as a signed number. */
78#define PPC_HIGHESTA(v) PPC_HIGHEST ((v) + 0x8000)
0baf16f2 79
f9c6b907
AM
80#define SEX16(val) (((val) ^ 0x8000) - 0x8000)
81
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
0baf16f2 85
5b7c81bd 86static bool reg_names_p = TARGET_REG_NAMES_P;
252b5132 87
98027b10
AM
88static void ppc_macro (char *, const struct powerpc_macro *);
89static void ppc_byte (int);
0baf16f2
AM
90
91#if defined (OBJ_XCOFF) || defined (OBJ_ELF)
98027b10
AM
92static void ppc_tc (int);
93static void ppc_machine (int);
0baf16f2 94#endif
252b5132
RH
95
96#ifdef OBJ_XCOFF
98027b10
AM
97static void ppc_comm (int);
98static void ppc_bb (int);
99static void ppc_bc (int);
100static void ppc_bf (int);
101static void ppc_biei (int);
102static void ppc_bs (int);
103static void ppc_eb (int);
104static void ppc_ec (int);
105static void ppc_ef (int);
106static void ppc_es (int);
107static void ppc_csect (int);
85645aed 108static void ppc_dwsect (int);
98027b10
AM
109static void ppc_change_csect (symbolS *, offsetT);
110static void ppc_function (int);
111static void ppc_extern (int);
112static void ppc_lglobl (int);
c865e45b 113static void ppc_ref (int);
98027b10
AM
114static void ppc_section (int);
115static void ppc_named_section (int);
116static void ppc_stabx (int);
117static void ppc_rename (int);
118static void ppc_toc (int);
119static void ppc_xcoff_cons (int);
120static void ppc_vbyte (int);
252b5132
RH
121#endif
122
123#ifdef OBJ_ELF
98027b10
AM
124static void ppc_elf_rdata (int);
125static void ppc_elf_lcomm (int);
6911b7dc 126static void ppc_elf_localentry (int);
ee67d69a 127static void ppc_elf_abiversion (int);
005d79fd 128static void ppc_elf_gnu_attribute (int);
252b5132 129#endif
252b5132
RH
130\f
131/* Generic assembler global variables which must be defined by all
132 targets. */
133
134#ifdef OBJ_ELF
135/* This string holds the chars that always start a comment. If the
136 pre-processor is disabled, these aren't very useful. The macro
137 tc_comment_chars points to this. We use this, rather than the
138 usual comment_chars, so that we can switch for Solaris conventions. */
139static const char ppc_solaris_comment_chars[] = "#!";
140static const char ppc_eabi_comment_chars[] = "#";
141
142#ifdef TARGET_SOLARIS_COMMENT
143const char *ppc_comment_chars = ppc_solaris_comment_chars;
144#else
145const char *ppc_comment_chars = ppc_eabi_comment_chars;
146#endif
147#else
148const char comment_chars[] = "#";
149#endif
150
151/* Characters which start a comment at the beginning of a line. */
152const char line_comment_chars[] = "#";
153
154/* Characters which may be used to separate multiple commands on a
155 single line. */
156const char line_separator_chars[] = ";";
157
158/* Characters which are used to indicate an exponent in a floating
159 point number. */
160const char EXP_CHARS[] = "eE";
161
162/* Characters which mean that a number is a floating point constant,
163 as in 0d1.0. */
164const char FLT_CHARS[] = "dD";
5ce8663f 165
5e02f92e 166/* Anything that can start an operand needs to be mentioned here,
ac805826 167 to stop the input scrubber eating whitespace. */
5e02f92e 168const char ppc_symbol_chars[] = "%[";
75e21f08
JJ
169
170/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
171int ppc_cie_data_alignment;
783de163 172
8fbf7334
JL
173/* The dwarf2 minimum instruction length. */
174int ppc_dwarf2_line_min_insn_length;
175
cef4f754
AM
176/* More than this number of nops in an alignment op gets a branch
177 instead. */
178unsigned long nop_limit = 4;
179
783de163
AM
180/* The type of processor we are assembling for. This is one or more
181 of the PPC_OPCODE flags defined in opcode/ppc.h. */
fa452fa6 182ppc_cpu_t ppc_cpu = 0;
776fc418 183ppc_cpu_t sticky = 0;
01efc3af 184
ee67d69a
AM
185/* Value for ELF e_flags EF_PPC64_ABI. */
186unsigned int ppc_abiversion = 0;
187
05f7541e 188#ifdef OBJ_ELF
01efc3af 189/* Flags set on encountering toc relocs. */
4f2a7b51 190static enum {
01efc3af
AM
191 has_large_toc_reloc = 1,
192 has_small_toc_reloc = 2
193} toc_reloc_types;
05f7541e 194#endif
bf7279d5
AM
195
196/* Warn on emitting data to code sections. */
197int warn_476;
0f873fd5 198uint64_t last_insn;
bf7279d5
AM
199segT last_seg;
200subsegT last_subseg;
252b5132
RH
201\f
202/* The target specific pseudo-ops which we support. */
203
204const pseudo_typeS md_pseudo_table[] =
205{
206 /* Pseudo-ops which must be overridden. */
207 { "byte", ppc_byte, 0 },
208
209#ifdef OBJ_XCOFF
210 /* Pseudo-ops specific to the RS/6000 XCOFF format. Some of these
211 legitimately belong in the obj-*.c file. However, XCOFF is based
212 on COFF, and is only implemented for the RS/6000. We just use
213 obj-coff.c, and add what we need here. */
214 { "comm", ppc_comm, 0 },
215 { "lcomm", ppc_comm, 1 },
216 { "bb", ppc_bb, 0 },
217 { "bc", ppc_bc, 0 },
218 { "bf", ppc_bf, 0 },
219 { "bi", ppc_biei, 0 },
220 { "bs", ppc_bs, 0 },
221 { "csect", ppc_csect, 0 },
85645aed 222 { "dwsect", ppc_dwsect, 0 },
252b5132
RH
223 { "data", ppc_section, 'd' },
224 { "eb", ppc_eb, 0 },
225 { "ec", ppc_ec, 0 },
226 { "ef", ppc_ef, 0 },
227 { "ei", ppc_biei, 1 },
228 { "es", ppc_es, 0 },
229 { "extern", ppc_extern, 0 },
230 { "function", ppc_function, 0 },
231 { "lglobl", ppc_lglobl, 0 },
c865e45b 232 { "ref", ppc_ref, 0 },
252b5132
RH
233 { "rename", ppc_rename, 0 },
234 { "section", ppc_named_section, 0 },
235 { "stabx", ppc_stabx, 0 },
236 { "text", ppc_section, 't' },
237 { "toc", ppc_toc, 0 },
238 { "long", ppc_xcoff_cons, 2 },
7f6d05e8 239 { "llong", ppc_xcoff_cons, 3 },
252b5132
RH
240 { "word", ppc_xcoff_cons, 1 },
241 { "short", ppc_xcoff_cons, 1 },
242 { "vbyte", ppc_vbyte, 0 },
243#endif
244
245#ifdef OBJ_ELF
62ebcb5c 246 { "llong", cons, 8 },
252b5132
RH
247 { "rdata", ppc_elf_rdata, 0 },
248 { "rodata", ppc_elf_rdata, 0 },
249 { "lcomm", ppc_elf_lcomm, 0 },
6911b7dc 250 { "localentry", ppc_elf_localentry, 0 },
ee67d69a 251 { "abiversion", ppc_elf_abiversion, 0 },
005d79fd 252 { "gnu_attribute", ppc_elf_gnu_attribute, 0},
252b5132
RH
253#endif
254
0baf16f2 255#if defined (OBJ_XCOFF) || defined (OBJ_ELF)
252b5132 256 { "tc", ppc_tc, 0 },
0baf16f2
AM
257 { "machine", ppc_machine, 0 },
258#endif
252b5132
RH
259
260 { NULL, NULL, 0 }
261};
262
263\f
99a814a1
AM
264/* Predefined register names if -mregnames (or default for Windows NT).
265 In general, there are lots of them, in an attempt to be compatible
266 with a number of other Windows NT assemblers. */
252b5132
RH
267
268/* Structure to hold information about predefined registers. */
269struct pd_reg
270 {
e0471c16 271 const char *name;
7e0de605
AM
272 unsigned short value;
273 unsigned short flags;
252b5132
RH
274 };
275
276/* List of registers that are pre-defined:
277
278 Each general register has predefined names of the form:
279 1. r<reg_num> which has the value <reg_num>.
280 2. r.<reg_num> which has the value <reg_num>.
281
252b5132
RH
282 Each floating point register has predefined names of the form:
283 1. f<reg_num> which has the value <reg_num>.
284 2. f.<reg_num> which has the value <reg_num>.
285
7a899fff
C
286 Each vector unit register has predefined names of the form:
287 1. v<reg_num> which has the value <reg_num>.
288 2. v.<reg_num> which has the value <reg_num>.
289
252b5132
RH
290 Each condition register has predefined names of the form:
291 1. cr<reg_num> which has the value <reg_num>.
292 2. cr.<reg_num> which has the value <reg_num>.
293
294 There are individual registers as well:
295 sp or r.sp has the value 1
296 rtoc or r.toc has the value 2
252b5132
RH
297 xer has the value 1
298 lr has the value 8
299 ctr has the value 9
252b5132
RH
300 dar has the value 19
301 dsisr has the value 18
302 dec has the value 22
303 sdr1 has the value 25
304 srr0 has the value 26
305 srr1 has the value 27
306
81d4177b 307 The table is sorted. Suitable for searching by a binary search. */
252b5132
RH
308
309static const struct pd_reg pre_defined_registers[] =
310{
aa3c112f
AM
311 /* VSX accumulators. */
312 { "a0", 0, PPC_OPERAND_ACC },
313 { "a1", 1, PPC_OPERAND_ACC },
314 { "a2", 2, PPC_OPERAND_ACC },
315 { "a3", 3, PPC_OPERAND_ACC },
316 { "a4", 4, PPC_OPERAND_ACC },
317 { "a5", 5, PPC_OPERAND_ACC },
318 { "a6", 6, PPC_OPERAND_ACC },
319 { "a7", 7, PPC_OPERAND_ACC },
320
7e0de605
AM
321 /* Condition Registers */
322 { "cr.0", 0, PPC_OPERAND_CR_REG },
323 { "cr.1", 1, PPC_OPERAND_CR_REG },
324 { "cr.2", 2, PPC_OPERAND_CR_REG },
325 { "cr.3", 3, PPC_OPERAND_CR_REG },
326 { "cr.4", 4, PPC_OPERAND_CR_REG },
327 { "cr.5", 5, PPC_OPERAND_CR_REG },
328 { "cr.6", 6, PPC_OPERAND_CR_REG },
329 { "cr.7", 7, PPC_OPERAND_CR_REG },
330
331 { "cr0", 0, PPC_OPERAND_CR_REG },
332 { "cr1", 1, PPC_OPERAND_CR_REG },
333 { "cr2", 2, PPC_OPERAND_CR_REG },
334 { "cr3", 3, PPC_OPERAND_CR_REG },
335 { "cr4", 4, PPC_OPERAND_CR_REG },
336 { "cr5", 5, PPC_OPERAND_CR_REG },
337 { "cr6", 6, PPC_OPERAND_CR_REG },
338 { "cr7", 7, PPC_OPERAND_CR_REG },
339
340 { "ctr", 9, PPC_OPERAND_SPR },
341 { "dar", 19, PPC_OPERAND_SPR },
342 { "dec", 22, PPC_OPERAND_SPR },
343 { "dsisr", 18, PPC_OPERAND_SPR },
344
345 /* Floating point registers */
346 { "f.0", 0, PPC_OPERAND_FPR },
347 { "f.1", 1, PPC_OPERAND_FPR },
348 { "f.10", 10, PPC_OPERAND_FPR },
349 { "f.11", 11, PPC_OPERAND_FPR },
350 { "f.12", 12, PPC_OPERAND_FPR },
351 { "f.13", 13, PPC_OPERAND_FPR },
352 { "f.14", 14, PPC_OPERAND_FPR },
353 { "f.15", 15, PPC_OPERAND_FPR },
354 { "f.16", 16, PPC_OPERAND_FPR },
355 { "f.17", 17, PPC_OPERAND_FPR },
356 { "f.18", 18, PPC_OPERAND_FPR },
357 { "f.19", 19, PPC_OPERAND_FPR },
358 { "f.2", 2, PPC_OPERAND_FPR },
359 { "f.20", 20, PPC_OPERAND_FPR },
360 { "f.21", 21, PPC_OPERAND_FPR },
361 { "f.22", 22, PPC_OPERAND_FPR },
362 { "f.23", 23, PPC_OPERAND_FPR },
363 { "f.24", 24, PPC_OPERAND_FPR },
364 { "f.25", 25, PPC_OPERAND_FPR },
365 { "f.26", 26, PPC_OPERAND_FPR },
366 { "f.27", 27, PPC_OPERAND_FPR },
367 { "f.28", 28, PPC_OPERAND_FPR },
368 { "f.29", 29, PPC_OPERAND_FPR },
369 { "f.3", 3, PPC_OPERAND_FPR },
370 { "f.30", 30, PPC_OPERAND_FPR },
371 { "f.31", 31, PPC_OPERAND_FPR },
372 { "f.32", 32, PPC_OPERAND_VSR },
373 { "f.33", 33, PPC_OPERAND_VSR },
374 { "f.34", 34, PPC_OPERAND_VSR },
375 { "f.35", 35, PPC_OPERAND_VSR },
376 { "f.36", 36, PPC_OPERAND_VSR },
377 { "f.37", 37, PPC_OPERAND_VSR },
378 { "f.38", 38, PPC_OPERAND_VSR },
379 { "f.39", 39, PPC_OPERAND_VSR },
380 { "f.4", 4, PPC_OPERAND_FPR },
381 { "f.40", 40, PPC_OPERAND_VSR },
382 { "f.41", 41, PPC_OPERAND_VSR },
383 { "f.42", 42, PPC_OPERAND_VSR },
384 { "f.43", 43, PPC_OPERAND_VSR },
385 { "f.44", 44, PPC_OPERAND_VSR },
386 { "f.45", 45, PPC_OPERAND_VSR },
387 { "f.46", 46, PPC_OPERAND_VSR },
388 { "f.47", 47, PPC_OPERAND_VSR },
389 { "f.48", 48, PPC_OPERAND_VSR },
390 { "f.49", 49, PPC_OPERAND_VSR },
391 { "f.5", 5, PPC_OPERAND_FPR },
392 { "f.50", 50, PPC_OPERAND_VSR },
393 { "f.51", 51, PPC_OPERAND_VSR },
394 { "f.52", 52, PPC_OPERAND_VSR },
395 { "f.53", 53, PPC_OPERAND_VSR },
396 { "f.54", 54, PPC_OPERAND_VSR },
397 { "f.55", 55, PPC_OPERAND_VSR },
398 { "f.56", 56, PPC_OPERAND_VSR },
399 { "f.57", 57, PPC_OPERAND_VSR },
400 { "f.58", 58, PPC_OPERAND_VSR },
401 { "f.59", 59, PPC_OPERAND_VSR },
402 { "f.6", 6, PPC_OPERAND_FPR },
403 { "f.60", 60, PPC_OPERAND_VSR },
404 { "f.61", 61, PPC_OPERAND_VSR },
405 { "f.62", 62, PPC_OPERAND_VSR },
406 { "f.63", 63, PPC_OPERAND_VSR },
407 { "f.7", 7, PPC_OPERAND_FPR },
408 { "f.8", 8, PPC_OPERAND_FPR },
409 { "f.9", 9, PPC_OPERAND_FPR },
410
411 { "f0", 0, PPC_OPERAND_FPR },
412 { "f1", 1, PPC_OPERAND_FPR },
413 { "f10", 10, PPC_OPERAND_FPR },
414 { "f11", 11, PPC_OPERAND_FPR },
415 { "f12", 12, PPC_OPERAND_FPR },
416 { "f13", 13, PPC_OPERAND_FPR },
417 { "f14", 14, PPC_OPERAND_FPR },
418 { "f15", 15, PPC_OPERAND_FPR },
419 { "f16", 16, PPC_OPERAND_FPR },
420 { "f17", 17, PPC_OPERAND_FPR },
421 { "f18", 18, PPC_OPERAND_FPR },
422 { "f19", 19, PPC_OPERAND_FPR },
423 { "f2", 2, PPC_OPERAND_FPR },
424 { "f20", 20, PPC_OPERAND_FPR },
425 { "f21", 21, PPC_OPERAND_FPR },
426 { "f22", 22, PPC_OPERAND_FPR },
427 { "f23", 23, PPC_OPERAND_FPR },
428 { "f24", 24, PPC_OPERAND_FPR },
429 { "f25", 25, PPC_OPERAND_FPR },
430 { "f26", 26, PPC_OPERAND_FPR },
431 { "f27", 27, PPC_OPERAND_FPR },
432 { "f28", 28, PPC_OPERAND_FPR },
433 { "f29", 29, PPC_OPERAND_FPR },
434 { "f3", 3, PPC_OPERAND_FPR },
435 { "f30", 30, PPC_OPERAND_FPR },
436 { "f31", 31, PPC_OPERAND_FPR },
437 { "f32", 32, PPC_OPERAND_VSR },
438 { "f33", 33, PPC_OPERAND_VSR },
439 { "f34", 34, PPC_OPERAND_VSR },
440 { "f35", 35, PPC_OPERAND_VSR },
441 { "f36", 36, PPC_OPERAND_VSR },
442 { "f37", 37, PPC_OPERAND_VSR },
443 { "f38", 38, PPC_OPERAND_VSR },
444 { "f39", 39, PPC_OPERAND_VSR },
445 { "f4", 4, PPC_OPERAND_FPR },
446 { "f40", 40, PPC_OPERAND_VSR },
447 { "f41", 41, PPC_OPERAND_VSR },
448 { "f42", 42, PPC_OPERAND_VSR },
449 { "f43", 43, PPC_OPERAND_VSR },
450 { "f44", 44, PPC_OPERAND_VSR },
451 { "f45", 45, PPC_OPERAND_VSR },
452 { "f46", 46, PPC_OPERAND_VSR },
453 { "f47", 47, PPC_OPERAND_VSR },
454 { "f48", 48, PPC_OPERAND_VSR },
455 { "f49", 49, PPC_OPERAND_VSR },
456 { "f5", 5, PPC_OPERAND_FPR },
457 { "f50", 50, PPC_OPERAND_VSR },
458 { "f51", 51, PPC_OPERAND_VSR },
459 { "f52", 52, PPC_OPERAND_VSR },
460 { "f53", 53, PPC_OPERAND_VSR },
461 { "f54", 54, PPC_OPERAND_VSR },
462 { "f55", 55, PPC_OPERAND_VSR },
463 { "f56", 56, PPC_OPERAND_VSR },
464 { "f57", 57, PPC_OPERAND_VSR },
465 { "f58", 58, PPC_OPERAND_VSR },
466 { "f59", 59, PPC_OPERAND_VSR },
467 { "f6", 6, PPC_OPERAND_FPR },
468 { "f60", 60, PPC_OPERAND_VSR },
469 { "f61", 61, PPC_OPERAND_VSR },
470 { "f62", 62, PPC_OPERAND_VSR },
471 { "f63", 63, PPC_OPERAND_VSR },
472 { "f7", 7, PPC_OPERAND_FPR },
473 { "f8", 8, PPC_OPERAND_FPR },
474 { "f9", 9, PPC_OPERAND_FPR },
252b5132 475
c3d65c1c 476 /* Quantization registers used with pair single instructions. */
7e0de605
AM
477 { "gqr.0", 0, PPC_OPERAND_GQR },
478 { "gqr.1", 1, PPC_OPERAND_GQR },
479 { "gqr.2", 2, PPC_OPERAND_GQR },
480 { "gqr.3", 3, PPC_OPERAND_GQR },
481 { "gqr.4", 4, PPC_OPERAND_GQR },
482 { "gqr.5", 5, PPC_OPERAND_GQR },
483 { "gqr.6", 6, PPC_OPERAND_GQR },
484 { "gqr.7", 7, PPC_OPERAND_GQR },
485 { "gqr0", 0, PPC_OPERAND_GQR },
486 { "gqr1", 1, PPC_OPERAND_GQR },
487 { "gqr2", 2, PPC_OPERAND_GQR },
488 { "gqr3", 3, PPC_OPERAND_GQR },
489 { "gqr4", 4, PPC_OPERAND_GQR },
490 { "gqr5", 5, PPC_OPERAND_GQR },
491 { "gqr6", 6, PPC_OPERAND_GQR },
492 { "gqr7", 7, PPC_OPERAND_GQR },
493
494 { "lr", 8, PPC_OPERAND_SPR },
495
496 /* General Purpose Registers */
497 { "r.0", 0, PPC_OPERAND_GPR },
498 { "r.1", 1, PPC_OPERAND_GPR },
499 { "r.10", 10, PPC_OPERAND_GPR },
500 { "r.11", 11, PPC_OPERAND_GPR },
501 { "r.12", 12, PPC_OPERAND_GPR },
502 { "r.13", 13, PPC_OPERAND_GPR },
503 { "r.14", 14, PPC_OPERAND_GPR },
504 { "r.15", 15, PPC_OPERAND_GPR },
505 { "r.16", 16, PPC_OPERAND_GPR },
506 { "r.17", 17, PPC_OPERAND_GPR },
507 { "r.18", 18, PPC_OPERAND_GPR },
508 { "r.19", 19, PPC_OPERAND_GPR },
509 { "r.2", 2, PPC_OPERAND_GPR },
510 { "r.20", 20, PPC_OPERAND_GPR },
511 { "r.21", 21, PPC_OPERAND_GPR },
512 { "r.22", 22, PPC_OPERAND_GPR },
513 { "r.23", 23, PPC_OPERAND_GPR },
514 { "r.24", 24, PPC_OPERAND_GPR },
515 { "r.25", 25, PPC_OPERAND_GPR },
516 { "r.26", 26, PPC_OPERAND_GPR },
517 { "r.27", 27, PPC_OPERAND_GPR },
518 { "r.28", 28, PPC_OPERAND_GPR },
519 { "r.29", 29, PPC_OPERAND_GPR },
520 { "r.3", 3, PPC_OPERAND_GPR },
521 { "r.30", 30, PPC_OPERAND_GPR },
522 { "r.31", 31, PPC_OPERAND_GPR },
523 { "r.4", 4, PPC_OPERAND_GPR },
524 { "r.5", 5, PPC_OPERAND_GPR },
525 { "r.6", 6, PPC_OPERAND_GPR },
526 { "r.7", 7, PPC_OPERAND_GPR },
527 { "r.8", 8, PPC_OPERAND_GPR },
528 { "r.9", 9, PPC_OPERAND_GPR },
529
530 { "r.sp", 1, PPC_OPERAND_GPR },
531
532 { "r.toc", 2, PPC_OPERAND_GPR },
533
534 { "r0", 0, PPC_OPERAND_GPR },
535 { "r1", 1, PPC_OPERAND_GPR },
536 { "r10", 10, PPC_OPERAND_GPR },
537 { "r11", 11, PPC_OPERAND_GPR },
538 { "r12", 12, PPC_OPERAND_GPR },
539 { "r13", 13, PPC_OPERAND_GPR },
540 { "r14", 14, PPC_OPERAND_GPR },
541 { "r15", 15, PPC_OPERAND_GPR },
542 { "r16", 16, PPC_OPERAND_GPR },
543 { "r17", 17, PPC_OPERAND_GPR },
544 { "r18", 18, PPC_OPERAND_GPR },
545 { "r19", 19, PPC_OPERAND_GPR },
546 { "r2", 2, PPC_OPERAND_GPR },
547 { "r20", 20, PPC_OPERAND_GPR },
548 { "r21", 21, PPC_OPERAND_GPR },
549 { "r22", 22, PPC_OPERAND_GPR },
550 { "r23", 23, PPC_OPERAND_GPR },
551 { "r24", 24, PPC_OPERAND_GPR },
552 { "r25", 25, PPC_OPERAND_GPR },
553 { "r26", 26, PPC_OPERAND_GPR },
554 { "r27", 27, PPC_OPERAND_GPR },
555 { "r28", 28, PPC_OPERAND_GPR },
556 { "r29", 29, PPC_OPERAND_GPR },
557 { "r3", 3, PPC_OPERAND_GPR },
558 { "r30", 30, PPC_OPERAND_GPR },
559 { "r31", 31, PPC_OPERAND_GPR },
560 { "r4", 4, PPC_OPERAND_GPR },
561 { "r5", 5, PPC_OPERAND_GPR },
562 { "r6", 6, PPC_OPERAND_GPR },
563 { "r7", 7, PPC_OPERAND_GPR },
564 { "r8", 8, PPC_OPERAND_GPR },
565 { "r9", 9, PPC_OPERAND_GPR },
566
567 { "rtoc", 2, PPC_OPERAND_GPR },
568
569 { "sdr1", 25, PPC_OPERAND_SPR },
570
571 { "sp", 1, PPC_OPERAND_GPR },
572
573 { "srr0", 26, PPC_OPERAND_SPR },
574 { "srr1", 27, PPC_OPERAND_SPR },
575
576 /* Vector (Altivec/VMX) registers */
577 { "v.0", 0, PPC_OPERAND_VR },
578 { "v.1", 1, PPC_OPERAND_VR },
579 { "v.10", 10, PPC_OPERAND_VR },
580 { "v.11", 11, PPC_OPERAND_VR },
581 { "v.12", 12, PPC_OPERAND_VR },
582 { "v.13", 13, PPC_OPERAND_VR },
583 { "v.14", 14, PPC_OPERAND_VR },
584 { "v.15", 15, PPC_OPERAND_VR },
585 { "v.16", 16, PPC_OPERAND_VR },
586 { "v.17", 17, PPC_OPERAND_VR },
587 { "v.18", 18, PPC_OPERAND_VR },
588 { "v.19", 19, PPC_OPERAND_VR },
589 { "v.2", 2, PPC_OPERAND_VR },
590 { "v.20", 20, PPC_OPERAND_VR },
591 { "v.21", 21, PPC_OPERAND_VR },
592 { "v.22", 22, PPC_OPERAND_VR },
593 { "v.23", 23, PPC_OPERAND_VR },
594 { "v.24", 24, PPC_OPERAND_VR },
595 { "v.25", 25, PPC_OPERAND_VR },
596 { "v.26", 26, PPC_OPERAND_VR },
597 { "v.27", 27, PPC_OPERAND_VR },
598 { "v.28", 28, PPC_OPERAND_VR },
599 { "v.29", 29, PPC_OPERAND_VR },
600 { "v.3", 3, PPC_OPERAND_VR },
601 { "v.30", 30, PPC_OPERAND_VR },
602 { "v.31", 31, PPC_OPERAND_VR },
603 { "v.4", 4, PPC_OPERAND_VR },
604 { "v.5", 5, PPC_OPERAND_VR },
605 { "v.6", 6, PPC_OPERAND_VR },
606 { "v.7", 7, PPC_OPERAND_VR },
607 { "v.8", 8, PPC_OPERAND_VR },
608 { "v.9", 9, PPC_OPERAND_VR },
609
610 { "v0", 0, PPC_OPERAND_VR },
611 { "v1", 1, PPC_OPERAND_VR },
612 { "v10", 10, PPC_OPERAND_VR },
613 { "v11", 11, PPC_OPERAND_VR },
614 { "v12", 12, PPC_OPERAND_VR },
615 { "v13", 13, PPC_OPERAND_VR },
616 { "v14", 14, PPC_OPERAND_VR },
617 { "v15", 15, PPC_OPERAND_VR },
618 { "v16", 16, PPC_OPERAND_VR },
619 { "v17", 17, PPC_OPERAND_VR },
620 { "v18", 18, PPC_OPERAND_VR },
621 { "v19", 19, PPC_OPERAND_VR },
622 { "v2", 2, PPC_OPERAND_VR },
623 { "v20", 20, PPC_OPERAND_VR },
624 { "v21", 21, PPC_OPERAND_VR },
625 { "v22", 22, PPC_OPERAND_VR },
626 { "v23", 23, PPC_OPERAND_VR },
627 { "v24", 24, PPC_OPERAND_VR },
628 { "v25", 25, PPC_OPERAND_VR },
629 { "v26", 26, PPC_OPERAND_VR },
630 { "v27", 27, PPC_OPERAND_VR },
631 { "v28", 28, PPC_OPERAND_VR },
632 { "v29", 29, PPC_OPERAND_VR },
633 { "v3", 3, PPC_OPERAND_VR },
634 { "v30", 30, PPC_OPERAND_VR },
635 { "v31", 31, PPC_OPERAND_VR },
636 { "v4", 4, PPC_OPERAND_VR },
637 { "v5", 5, PPC_OPERAND_VR },
638 { "v6", 6, PPC_OPERAND_VR },
639 { "v7", 7, PPC_OPERAND_VR },
640 { "v8", 8, PPC_OPERAND_VR },
641 { "v9", 9, PPC_OPERAND_VR },
642
643 /* Vector Scalar (VSX) registers (ISA 2.06). */
644 { "vs.0", 0, PPC_OPERAND_VSR },
645 { "vs.1", 1, PPC_OPERAND_VSR },
646 { "vs.10", 10, PPC_OPERAND_VSR },
647 { "vs.11", 11, PPC_OPERAND_VSR },
648 { "vs.12", 12, PPC_OPERAND_VSR },
649 { "vs.13", 13, PPC_OPERAND_VSR },
650 { "vs.14", 14, PPC_OPERAND_VSR },
651 { "vs.15", 15, PPC_OPERAND_VSR },
652 { "vs.16", 16, PPC_OPERAND_VSR },
653 { "vs.17", 17, PPC_OPERAND_VSR },
654 { "vs.18", 18, PPC_OPERAND_VSR },
655 { "vs.19", 19, PPC_OPERAND_VSR },
656 { "vs.2", 2, PPC_OPERAND_VSR },
657 { "vs.20", 20, PPC_OPERAND_VSR },
658 { "vs.21", 21, PPC_OPERAND_VSR },
659 { "vs.22", 22, PPC_OPERAND_VSR },
660 { "vs.23", 23, PPC_OPERAND_VSR },
661 { "vs.24", 24, PPC_OPERAND_VSR },
662 { "vs.25", 25, PPC_OPERAND_VSR },
663 { "vs.26", 26, PPC_OPERAND_VSR },
664 { "vs.27", 27, PPC_OPERAND_VSR },
665 { "vs.28", 28, PPC_OPERAND_VSR },
666 { "vs.29", 29, PPC_OPERAND_VSR },
667 { "vs.3", 3, PPC_OPERAND_VSR },
668 { "vs.30", 30, PPC_OPERAND_VSR },
669 { "vs.31", 31, PPC_OPERAND_VSR },
670 { "vs.32", 32, PPC_OPERAND_VSR },
671 { "vs.33", 33, PPC_OPERAND_VSR },
672 { "vs.34", 34, PPC_OPERAND_VSR },
673 { "vs.35", 35, PPC_OPERAND_VSR },
674 { "vs.36", 36, PPC_OPERAND_VSR },
675 { "vs.37", 37, PPC_OPERAND_VSR },
676 { "vs.38", 38, PPC_OPERAND_VSR },
677 { "vs.39", 39, PPC_OPERAND_VSR },
678 { "vs.4", 4, PPC_OPERAND_VSR },
679 { "vs.40", 40, PPC_OPERAND_VSR },
680 { "vs.41", 41, PPC_OPERAND_VSR },
681 { "vs.42", 42, PPC_OPERAND_VSR },
682 { "vs.43", 43, PPC_OPERAND_VSR },
683 { "vs.44", 44, PPC_OPERAND_VSR },
684 { "vs.45", 45, PPC_OPERAND_VSR },
685 { "vs.46", 46, PPC_OPERAND_VSR },
686 { "vs.47", 47, PPC_OPERAND_VSR },
687 { "vs.48", 48, PPC_OPERAND_VSR },
688 { "vs.49", 49, PPC_OPERAND_VSR },
689 { "vs.5", 5, PPC_OPERAND_VSR },
690 { "vs.50", 50, PPC_OPERAND_VSR },
691 { "vs.51", 51, PPC_OPERAND_VSR },
692 { "vs.52", 52, PPC_OPERAND_VSR },
693 { "vs.53", 53, PPC_OPERAND_VSR },
694 { "vs.54", 54, PPC_OPERAND_VSR },
695 { "vs.55", 55, PPC_OPERAND_VSR },
696 { "vs.56", 56, PPC_OPERAND_VSR },
697 { "vs.57", 57, PPC_OPERAND_VSR },
698 { "vs.58", 58, PPC_OPERAND_VSR },
699 { "vs.59", 59, PPC_OPERAND_VSR },
700 { "vs.6", 6, PPC_OPERAND_VSR },
701 { "vs.60", 60, PPC_OPERAND_VSR },
702 { "vs.61", 61, PPC_OPERAND_VSR },
703 { "vs.62", 62, PPC_OPERAND_VSR },
704 { "vs.63", 63, PPC_OPERAND_VSR },
705 { "vs.7", 7, PPC_OPERAND_VSR },
706 { "vs.8", 8, PPC_OPERAND_VSR },
707 { "vs.9", 9, PPC_OPERAND_VSR },
708
709 { "vs0", 0, PPC_OPERAND_VSR },
710 { "vs1", 1, PPC_OPERAND_VSR },
711 { "vs10", 10, PPC_OPERAND_VSR },
712 { "vs11", 11, PPC_OPERAND_VSR },
713 { "vs12", 12, PPC_OPERAND_VSR },
714 { "vs13", 13, PPC_OPERAND_VSR },
715 { "vs14", 14, PPC_OPERAND_VSR },
716 { "vs15", 15, PPC_OPERAND_VSR },
717 { "vs16", 16, PPC_OPERAND_VSR },
718 { "vs17", 17, PPC_OPERAND_VSR },
719 { "vs18", 18, PPC_OPERAND_VSR },
720 { "vs19", 19, PPC_OPERAND_VSR },
721 { "vs2", 2, PPC_OPERAND_VSR },
722 { "vs20", 20, PPC_OPERAND_VSR },
723 { "vs21", 21, PPC_OPERAND_VSR },
724 { "vs22", 22, PPC_OPERAND_VSR },
725 { "vs23", 23, PPC_OPERAND_VSR },
726 { "vs24", 24, PPC_OPERAND_VSR },
727 { "vs25", 25, PPC_OPERAND_VSR },
728 { "vs26", 26, PPC_OPERAND_VSR },
729 { "vs27", 27, PPC_OPERAND_VSR },
730 { "vs28", 28, PPC_OPERAND_VSR },
731 { "vs29", 29, PPC_OPERAND_VSR },
732 { "vs3", 3, PPC_OPERAND_VSR },
733 { "vs30", 30, PPC_OPERAND_VSR },
734 { "vs31", 31, PPC_OPERAND_VSR },
735 { "vs32", 32, PPC_OPERAND_VSR },
736 { "vs33", 33, PPC_OPERAND_VSR },
737 { "vs34", 34, PPC_OPERAND_VSR },
738 { "vs35", 35, PPC_OPERAND_VSR },
739 { "vs36", 36, PPC_OPERAND_VSR },
740 { "vs37", 37, PPC_OPERAND_VSR },
741 { "vs38", 38, PPC_OPERAND_VSR },
742 { "vs39", 39, PPC_OPERAND_VSR },
743 { "vs4", 4, PPC_OPERAND_VSR },
744 { "vs40", 40, PPC_OPERAND_VSR },
745 { "vs41", 41, PPC_OPERAND_VSR },
746 { "vs42", 42, PPC_OPERAND_VSR },
747 { "vs43", 43, PPC_OPERAND_VSR },
748 { "vs44", 44, PPC_OPERAND_VSR },
749 { "vs45", 45, PPC_OPERAND_VSR },
750 { "vs46", 46, PPC_OPERAND_VSR },
751 { "vs47", 47, PPC_OPERAND_VSR },
752 { "vs48", 48, PPC_OPERAND_VSR },
753 { "vs49", 49, PPC_OPERAND_VSR },
754 { "vs5", 5, PPC_OPERAND_VSR },
755 { "vs50", 50, PPC_OPERAND_VSR },
756 { "vs51", 51, PPC_OPERAND_VSR },
757 { "vs52", 52, PPC_OPERAND_VSR },
758 { "vs53", 53, PPC_OPERAND_VSR },
759 { "vs54", 54, PPC_OPERAND_VSR },
760 { "vs55", 55, PPC_OPERAND_VSR },
761 { "vs56", 56, PPC_OPERAND_VSR },
762 { "vs57", 57, PPC_OPERAND_VSR },
763 { "vs58", 58, PPC_OPERAND_VSR },
764 { "vs59", 59, PPC_OPERAND_VSR },
765 { "vs6", 6, PPC_OPERAND_VSR },
766 { "vs60", 60, PPC_OPERAND_VSR },
767 { "vs61", 61, PPC_OPERAND_VSR },
768 { "vs62", 62, PPC_OPERAND_VSR },
769 { "vs63", 63, PPC_OPERAND_VSR },
770 { "vs7", 7, PPC_OPERAND_VSR },
771 { "vs8", 8, PPC_OPERAND_VSR },
772 { "vs9", 9, PPC_OPERAND_VSR },
773
774 { "xer", 1, PPC_OPERAND_SPR }
252b5132
RH
775};
776
bc805888 777#define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct pd_reg))
252b5132
RH
778
779/* Given NAME, find the register number associated with that name, return
780 the integer value associated with the given name or -1 on failure. */
781
7e0de605 782static const struct pd_reg *
98027b10 783reg_name_search (const struct pd_reg *regs, int regcount, const char *name)
252b5132
RH
784{
785 int middle, low, high;
786 int cmp;
787
788 low = 0;
789 high = regcount - 1;
790
791 do
792 {
793 middle = (low + high) / 2;
794 cmp = strcasecmp (name, regs[middle].name);
795 if (cmp < 0)
796 high = middle - 1;
797 else if (cmp > 0)
798 low = middle + 1;
799 else
7e0de605 800 return &regs[middle];
252b5132
RH
801 }
802 while (low <= high);
803
7e0de605 804 return NULL;
252b5132
RH
805}
806
807/*
99a814a1 808 * Summary of register_name.
252b5132
RH
809 *
810 * in: Input_line_pointer points to 1st char of operand.
811 *
812 * out: A expressionS.
813 * The operand may have been a register: in this case, X_op == O_register,
814 * X_add_number is set to the register number, and truth is returned.
815 * Input_line_pointer->(next non-blank) char after operand, or is in its
816 * original state.
817 */
818
5b7c81bd 819static bool
98027b10 820register_name (expressionS *expressionP)
252b5132 821{
7e0de605 822 const struct pd_reg *reg;
252b5132
RH
823 char *name;
824 char *start;
825 char c;
826
99a814a1 827 /* Find the spelling of the operand. */
252b5132 828 start = name = input_line_pointer;
3882b010 829 if (name[0] == '%' && ISALPHA (name[1]))
252b5132
RH
830 name = ++input_line_pointer;
831
3882b010 832 else if (!reg_names_p || !ISALPHA (name[0]))
5b7c81bd 833 return false;
252b5132 834
d02603dc 835 c = get_symbol_name (&name);
7e0de605 836 reg = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
252b5132 837
468cced8
AM
838 /* Put back the delimiting char. */
839 *input_line_pointer = c;
840
99a814a1 841 /* Look to see if it's in the register table. */
7e0de605 842 if (reg != NULL)
252b5132
RH
843 {
844 expressionP->X_op = O_register;
7e0de605
AM
845 expressionP->X_add_number = reg->value;
846 expressionP->X_md = reg->flags;
81d4177b 847
99a814a1 848 /* Make the rest nice. */
252b5132
RH
849 expressionP->X_add_symbol = NULL;
850 expressionP->X_op_symbol = NULL;
5b7c81bd 851 return true;
252b5132 852 }
468cced8
AM
853
854 /* Reset the line as if we had not done anything. */
855 input_line_pointer = start;
5b7c81bd 856 return false;
252b5132
RH
857}
858\f
859/* This function is called for each symbol seen in an expression. It
860 handles the special parsing which PowerPC assemblers are supposed
861 to use for condition codes. */
862
863/* Whether to do the special parsing. */
5b7c81bd 864static bool cr_operand;
252b5132
RH
865
866/* Names to recognize in a condition code. This table is sorted. */
867static const struct pd_reg cr_names[] =
868{
7e0de605
AM
869 { "cr0", 0, PPC_OPERAND_CR_REG },
870 { "cr1", 1, PPC_OPERAND_CR_REG },
871 { "cr2", 2, PPC_OPERAND_CR_REG },
872 { "cr3", 3, PPC_OPERAND_CR_REG },
873 { "cr4", 4, PPC_OPERAND_CR_REG },
874 { "cr5", 5, PPC_OPERAND_CR_REG },
875 { "cr6", 6, PPC_OPERAND_CR_REG },
876 { "cr7", 7, PPC_OPERAND_CR_REG },
877 { "eq", 2, PPC_OPERAND_CR_BIT },
878 { "gt", 1, PPC_OPERAND_CR_BIT },
879 { "lt", 0, PPC_OPERAND_CR_BIT },
880 { "so", 3, PPC_OPERAND_CR_BIT },
881 { "un", 3, PPC_OPERAND_CR_BIT }
252b5132
RH
882};
883
884/* Parsing function. This returns non-zero if it recognized an
885 expression. */
886
887int
91d6fa6a 888ppc_parse_name (const char *name, expressionS *exp)
252b5132 889{
7e0de605 890 const struct pd_reg *reg;
252b5132
RH
891
892 if (! cr_operand)
893 return 0;
894
13abbae3
AM
895 if (*name == '%')
896 ++name;
7e0de605 897 reg = reg_name_search (cr_names, sizeof cr_names / sizeof cr_names[0],
252b5132 898 name);
7e0de605 899 if (reg == NULL)
252b5132
RH
900 return 0;
901
7e0de605
AM
902 exp->X_op = O_register;
903 exp->X_add_number = reg->value;
904 exp->X_md = reg->flags;
252b5132
RH
905
906 return 1;
907}
7e0de605
AM
908
909/* Propagate X_md and check register expressions. This is to support
910 condition codes like 4*cr5+eq. */
911
912int
913ppc_optimize_expr (expressionS *left, operatorT op, expressionS *right)
914{
915 /* Accept 4*cr<n> and cr<n>*4. */
916 if (op == O_multiply
917 && ((right->X_op == O_register
918 && right->X_md == PPC_OPERAND_CR_REG
919 && left->X_op == O_constant
920 && left->X_add_number == 4)
921 || (left->X_op == O_register
922 && left->X_md == PPC_OPERAND_CR_REG
923 && right->X_op == O_constant
924 && right->X_add_number == 4)))
925 {
926 left->X_op = O_register;
927 left->X_md = PPC_OPERAND_CR_REG | PPC_OPERAND_CR_BIT;
928 left->X_add_number *= right->X_add_number;
929 return 1;
930 }
931
932 /* Accept the above plus <cr bit>, and <cr bit> plus the above. */
933 if (right->X_op == O_register
934 && left->X_op == O_register
935 && op == O_add
936 && ((right->X_md == PPC_OPERAND_CR_BIT
937 && left->X_md == (PPC_OPERAND_CR_REG | PPC_OPERAND_CR_BIT))
938 || (right->X_md == (PPC_OPERAND_CR_REG | PPC_OPERAND_CR_BIT)
939 && left->X_md == PPC_OPERAND_CR_BIT)))
940 {
941 left->X_md = PPC_OPERAND_CR_BIT;
942 right->X_op = O_constant;
943 return 0;
944 }
945
946 /* Accept reg +/- constant. */
947 if (left->X_op == O_register
948 && !((op == O_add || op == O_subtract) && right->X_op == O_constant))
ece5dcc1 949 as_warn (_("invalid register expression"));
7e0de605
AM
950
951 /* Accept constant + reg. */
952 if (right->X_op == O_register)
953 {
954 if (op == O_add && left->X_op == O_constant)
955 left->X_md = right->X_md;
956 else
ece5dcc1 957 as_warn (_("invalid register expression"));
7e0de605
AM
958 }
959
960 return 0;
961}
252b5132
RH
962\f
963/* Local variables. */
964
2b3c4602
AM
965/* Whether to target xcoff64/elf64. */
966static unsigned int ppc_obj64 = BFD_DEFAULT_TARGET_SIZE == 64;
7f6d05e8 967
252b5132 968/* Opcode hash table. */
629310ab 969static htab_t ppc_hash;
252b5132
RH
970
971/* Macro hash table. */
629310ab 972static htab_t ppc_macro_hash;
252b5132
RH
973
974#ifdef OBJ_ELF
99a814a1 975/* What type of shared library support to use. */
5d6f4f16 976static enum { SHLIB_NONE, SHLIB_PIC, SHLIB_MRELOCATABLE } shlib = SHLIB_NONE;
252b5132 977
99a814a1 978/* Flags to set in the elf header. */
252b5132
RH
979static flagword ppc_flags = 0;
980
981/* Whether this is Solaris or not. */
982#ifdef TARGET_SOLARIS_COMMENT
5b7c81bd 983#define SOLARIS_P true
252b5132 984#else
5b7c81bd 985#define SOLARIS_P false
252b5132
RH
986#endif
987
5b7c81bd 988static bool msolaris = SOLARIS_P;
252b5132
RH
989#endif
990
991#ifdef OBJ_XCOFF
992
993/* The RS/6000 assembler uses the .csect pseudo-op to generate code
994 using a bunch of different sections. These assembler sections,
1b2cb8e2
CC
995 however, are all encompassed within the .text, .data or .bss sections
996 of the final output file. We handle this by using different
997 subsegments within these main segments.
998 .tdata and .tbss sections only have one type of csects for now,
999 but it's better to follow the same construction like the others. */
1000
1001struct ppc_xcoff_section ppc_xcoff_text_section;
1002struct ppc_xcoff_section ppc_xcoff_data_section;
1003struct ppc_xcoff_section ppc_xcoff_bss_section;
1004struct ppc_xcoff_section ppc_xcoff_tdata_section;
1005struct ppc_xcoff_section ppc_xcoff_tbss_section;
1006
1007/* Return true if the ppc_xcoff_section structure is already
1008 initialized. */
5b7c81bd 1009static bool
1b2cb8e2
CC
1010ppc_xcoff_section_is_initialized (struct ppc_xcoff_section *section)
1011{
1012 return section->segment != NULL;
1013}
252b5132 1014
1b2cb8e2
CC
1015/* Initialize a ppc_xcoff_section.
1016 Dummy symbols are used to ensure the position of .text over .data
1017 and .tdata. These symbols won't be output. */
1018static void
1019ppc_init_xcoff_section (struct ppc_xcoff_section *s, segT seg,
5b7c81bd 1020 bool need_dummy)
1b2cb8e2
CC
1021{
1022 s->segment = seg;
1023 s->next_subsegment = 2;
1024 if (need_dummy)
1025 {
1026 s->csects = symbol_make ("dummy\001");
1027 symbol_get_tc (s->csects)->within = s->csects;
1028 }
1029}
252b5132
RH
1030
1031/* The current csect. */
1032static symbolS *ppc_current_csect;
1033
1034/* The RS/6000 assembler uses a TOC which holds addresses of functions
1035 and variables. Symbols are put in the TOC with the .tc pseudo-op.
1036 A special relocation is used when accessing TOC entries. We handle
1037 the TOC as a subsegment within the .data segment. We set it up if
1038 we see a .toc pseudo-op, and save the csect symbol here. */
1039static symbolS *ppc_toc_csect;
1040
1041/* The first frag in the TOC subsegment. */
1042static fragS *ppc_toc_frag;
1043
1044/* The first frag in the first subsegment after the TOC in the .data
1045 segment. NULL if there are no subsegments after the TOC. */
1046static fragS *ppc_after_toc_frag;
1047
1048/* The current static block. */
1049static symbolS *ppc_current_block;
1050
1051/* The COFF debugging section; set by md_begin. This is not the
1052 .debug section, but is instead the secret BFD section which will
1053 cause BFD to set the section number of a symbol to N_DEBUG. */
1054static asection *ppc_coff_debug_section;
1055
85645aed
TG
1056/* Structure to set the length field of the dwarf sections. */
1057struct dw_subsection {
1058 /* Subsections are simply linked. */
1059 struct dw_subsection *link;
1060
1061 /* The subsection number. */
1062 subsegT subseg;
1063
1064 /* Expression to compute the length of the section. */
1065 expressionS end_exp;
1066};
1067
1068static struct dw_section {
1069 /* Corresponding section. */
1070 segT sect;
1071
1072 /* Simply linked list of subsections with a label. */
1073 struct dw_subsection *list_subseg;
1074
1075 /* The anonymous subsection. */
1076 struct dw_subsection *anon_subseg;
1077} dw_sections[XCOFF_DWSECT_NBR_NAMES];
252b5132
RH
1078#endif /* OBJ_XCOFF */
1079
252b5132
RH
1080#ifdef OBJ_ELF
1081symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE" */
6a0c61b7
EZ
1082unsigned long *ppc_apuinfo_list;
1083unsigned int ppc_apuinfo_num;
1084unsigned int ppc_apuinfo_num_alloc;
252b5132
RH
1085#endif /* OBJ_ELF */
1086\f
1087#ifdef OBJ_ELF
15c1449b 1088const char *const md_shortopts = "b:l:usm:K:VQ:";
252b5132 1089#else
15c1449b 1090const char *const md_shortopts = "um:";
252b5132 1091#endif
cef4f754 1092#define OPTION_NOPS (OPTION_MD_BASE + 0)
15c1449b 1093const struct option md_longopts[] = {
cef4f754 1094 {"nops", required_argument, NULL, OPTION_NOPS},
bf7279d5
AM
1095 {"ppc476-workaround", no_argument, &warn_476, 1},
1096 {"no-ppc476-workaround", no_argument, &warn_476, 0},
252b5132
RH
1097 {NULL, no_argument, NULL, 0}
1098};
15c1449b 1099const size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
1100
1101int
17b9d67d 1102md_parse_option (int c, const char *arg)
252b5132 1103{
69fe9ce5
AM
1104 ppc_cpu_t new_cpu;
1105
252b5132
RH
1106 switch (c)
1107 {
1108 case 'u':
1109 /* -u means that any undefined symbols should be treated as
1110 external, which is the default for gas anyhow. */
1111 break;
1112
1113#ifdef OBJ_ELF
1114 case 'l':
1115 /* Solaris as takes -le (presumably for little endian). For completeness
99a814a1 1116 sake, recognize -be also. */
252b5132
RH
1117 if (strcmp (arg, "e") == 0)
1118 {
1119 target_big_endian = 0;
1120 set_target_endian = 1;
b9c361e0 1121 if (ppc_cpu & PPC_OPCODE_VLE)
d6ed37ed 1122 as_bad (_("the use of -mvle requires big endian."));
252b5132
RH
1123 }
1124 else
1125 return 0;
1126
1127 break;
1128
1129 case 'b':
1130 if (strcmp (arg, "e") == 0)
1131 {
1132 target_big_endian = 1;
1133 set_target_endian = 1;
1134 }
1135 else
1136 return 0;
1137
1138 break;
1139
1140 case 'K':
99a814a1 1141 /* Recognize -K PIC. */
252b5132
RH
1142 if (strcmp (arg, "PIC") == 0 || strcmp (arg, "pic") == 0)
1143 {
1144 shlib = SHLIB_PIC;
1145 ppc_flags |= EF_PPC_RELOCATABLE_LIB;
1146 }
1147 else
1148 return 0;
1149
1150 break;
1151#endif
1152
7f6d05e8
CP
1153 /* a64 and a32 determine whether to use XCOFF64 or XCOFF32. */
1154 case 'a':
1155 if (strcmp (arg, "64") == 0)
2a98c3a6
AM
1156 {
1157#ifdef BFD64
1158 ppc_obj64 = 1;
d6ed37ed
AM
1159 if (ppc_cpu & PPC_OPCODE_VLE)
1160 as_bad (_("the use of -mvle requires -a32."));
2a98c3a6
AM
1161#else
1162 as_fatal (_("%s unsupported"), "-a64");
1163#endif
1164 }
7f6d05e8 1165 else if (strcmp (arg, "32") == 0)
2b3c4602 1166 ppc_obj64 = 0;
7f6d05e8
CP
1167 else
1168 return 0;
1169 break;
81d4177b 1170
252b5132 1171 case 'm':
776fc418 1172 new_cpu = ppc_parse_cpu (ppc_cpu, &sticky, arg);
52be03fd
AM
1173 /* "raw" is only valid for the disassembler. */
1174 if (new_cpu != 0 && (new_cpu & PPC_OPCODE_RAW) == 0)
b9c361e0
JL
1175 {
1176 ppc_cpu = new_cpu;
d6ed37ed
AM
1177 if (strcmp (arg, "vle") == 0)
1178 {
1179 if (set_target_endian && target_big_endian == 0)
1180 as_bad (_("the use of -mvle requires big endian."));
1181 if (ppc_obj64)
1182 as_bad (_("the use of -mvle requires -a32."));
1183 }
b9c361e0 1184 }
252b5132 1185
83eef883
AFB
1186 else if (strcmp (arg, "no-vle") == 0)
1187 {
1188 sticky &= ~PPC_OPCODE_VLE;
1189
1190 new_cpu = ppc_parse_cpu (ppc_cpu, &sticky, "booke");
1191 new_cpu &= ~PPC_OPCODE_VLE;
1192
1193 ppc_cpu = new_cpu;
1194 }
1195
252b5132 1196 else if (strcmp (arg, "regnames") == 0)
5b7c81bd 1197 reg_names_p = true;
252b5132
RH
1198
1199 else if (strcmp (arg, "no-regnames") == 0)
5b7c81bd 1200 reg_names_p = false;
252b5132
RH
1201
1202#ifdef OBJ_ELF
99a814a1
AM
1203 /* -mrelocatable/-mrelocatable-lib -- warn about initializations
1204 that require relocation. */
252b5132
RH
1205 else if (strcmp (arg, "relocatable") == 0)
1206 {
5d6f4f16 1207 shlib = SHLIB_MRELOCATABLE;
252b5132
RH
1208 ppc_flags |= EF_PPC_RELOCATABLE;
1209 }
1210
1211 else if (strcmp (arg, "relocatable-lib") == 0)
1212 {
5d6f4f16 1213 shlib = SHLIB_MRELOCATABLE;
252b5132
RH
1214 ppc_flags |= EF_PPC_RELOCATABLE_LIB;
1215 }
1216
99a814a1 1217 /* -memb, set embedded bit. */
252b5132
RH
1218 else if (strcmp (arg, "emb") == 0)
1219 ppc_flags |= EF_PPC_EMB;
1220
cc643b88 1221 /* -mlittle/-mbig set the endianness. */
99a814a1
AM
1222 else if (strcmp (arg, "little") == 0
1223 || strcmp (arg, "little-endian") == 0)
252b5132
RH
1224 {
1225 target_big_endian = 0;
1226 set_target_endian = 1;
b9c361e0 1227 if (ppc_cpu & PPC_OPCODE_VLE)
d6ed37ed 1228 as_bad (_("the use of -mvle requires big endian."));
252b5132
RH
1229 }
1230
1231 else if (strcmp (arg, "big") == 0 || strcmp (arg, "big-endian") == 0)
1232 {
1233 target_big_endian = 1;
1234 set_target_endian = 1;
1235 }
1236
1237 else if (strcmp (arg, "solaris") == 0)
1238 {
5b7c81bd 1239 msolaris = true;
252b5132
RH
1240 ppc_comment_chars = ppc_solaris_comment_chars;
1241 }
1242
1243 else if (strcmp (arg, "no-solaris") == 0)
1244 {
5b7c81bd 1245 msolaris = false;
252b5132
RH
1246 ppc_comment_chars = ppc_eabi_comment_chars;
1247 }
74081948
AF
1248 else if (strcmp (arg, "spe2") == 0)
1249 {
1250 ppc_cpu |= PPC_OPCODE_SPE2;
1251 }
252b5132
RH
1252#endif
1253 else
1254 {
1255 as_bad (_("invalid switch -m%s"), arg);
1256 return 0;
1257 }
1258 break;
1259
1260#ifdef OBJ_ELF
1261 /* -V: SVR4 argument to print version ID. */
1262 case 'V':
1263 print_version_id ();
1264 break;
1265
1266 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
1267 should be emitted or not. FIXME: Not implemented. */
1268 case 'Q':
1269 break;
1270
1271 /* Solaris takes -s to specify that .stabs go in a .stabs section,
1272 rather than .stabs.excl, which is ignored by the linker.
1273 FIXME: Not implemented. */
1274 case 's':
1275 if (arg)
1276 return 0;
1277
1278 break;
1279#endif
1280
cef4f754
AM
1281 case OPTION_NOPS:
1282 {
1283 char *end;
1284 nop_limit = strtoul (optarg, &end, 0);
1285 if (*end)
1286 as_bad (_("--nops needs a numeric argument"));
1287 }
1288 break;
85645aed 1289
bf7279d5
AM
1290 case 0:
1291 break;
1292
252b5132
RH
1293 default:
1294 return 0;
1295 }
1296
1297 return 1;
1298}
1299
5f63875b
AM
1300static int
1301is_ppc64_target (const bfd_target *targ, void *data ATTRIBUTE_UNUSED)
1302{
1303 switch (targ->flavour)
1304 {
1305#ifdef OBJ_ELF
1306 case bfd_target_elf_flavour:
1307 return strncmp (targ->name, "elf64-powerpc", 13) == 0;
1308#endif
1309#ifdef OBJ_XCOFF
1310 case bfd_target_xcoff_flavour:
1311 return (strcmp (targ->name, "aixcoff64-rs6000") == 0
1312 || strcmp (targ->name, "aix5coff64-rs6000") == 0);
1313#endif
1314 default:
1315 return 0;
1316 }
1317}
1318
252b5132 1319void
98027b10 1320md_show_usage (FILE *stream)
252b5132 1321{
bc805888 1322 fprintf (stream, _("\
5f63875b
AM
1323PowerPC options:\n"));
1324 fprintf (stream, _("\
1325-a32 generate ELF32/XCOFF32\n"));
1326 if (bfd_iterate_over_targets (is_ppc64_target, NULL))
1327 fprintf (stream, _("\
1328-a64 generate ELF64/XCOFF64\n"));
1329 fprintf (stream, _("\
1330-u ignored\n"));
1331 fprintf (stream, _("\
1332-mpwrx, -mpwr2 generate code for POWER/2 (RIOS2)\n"));
1333 fprintf (stream, _("\
1334-mpwr generate code for POWER (RIOS1)\n"));
1335 fprintf (stream, _("\
1336-m601 generate code for PowerPC 601\n"));
1337 fprintf (stream, _("\
418c1742 1338-mppc, -mppc32, -m603, -m604\n\
5f63875b
AM
1339 generate code for PowerPC 603/604\n"));
1340 fprintf (stream, _("\
1341-m403 generate code for PowerPC 403\n"));
1342 fprintf (stream, _("\
1343-m405 generate code for PowerPC 405\n"));
1344 fprintf (stream, _("\
1345-m440 generate code for PowerPC 440\n"));
1346 fprintf (stream, _("\
1347-m464 generate code for PowerPC 464\n"));
1348 fprintf (stream, _("\
1349-m476 generate code for PowerPC 476\n"));
1350 fprintf (stream, _("\
f5c120c5 1351-m7400, -m7410, -m7450, -m7455\n\
5f63875b
AM
1352 generate code for PowerPC 7400/7410/7450/7455\n"));
1353 fprintf (stream, _("\
fa758a70 1354-m750cl, -mgekko, -mbroadway\n\
5f63875b
AM
1355 generate code for PowerPC 750cl/Gekko/Broadway\n"));
1356 fprintf (stream, _("\
ef5a96d5 1357-m821, -m850, -m860 generate code for PowerPC 821/850/860\n"));
df12615d 1358 fprintf (stream, _("\
5f63875b
AM
1359-mppc64, -m620 generate code for PowerPC 620/625/630\n"));
1360 fprintf (stream, _("\
1361-mppc64bridge generate code for PowerPC 64, including bridge insns\n"));
1362 fprintf (stream, _("\
1363-mbooke generate code for 32-bit PowerPC BookE\n"));
1364 fprintf (stream, _("\
1365-ma2 generate code for A2 architecture\n"));
1366 fprintf (stream, _("\
1367-mpower4, -mpwr4 generate code for Power4 architecture\n"));
1368 fprintf (stream, _("\
cdc51b07 1369-mpower5, -mpwr5, -mpwr5x\n\
5f63875b
AM
1370 generate code for Power5 architecture\n"));
1371 fprintf (stream, _("\
1372-mpower6, -mpwr6 generate code for Power6 architecture\n"));
1373 fprintf (stream, _("\
1374-mpower7, -mpwr7 generate code for Power7 architecture\n"));
1375 fprintf (stream, _("\
1376-mpower8, -mpwr8 generate code for Power8 architecture\n"));
1377 fprintf (stream, _("\
1378-mpower9, -mpwr9 generate code for Power9 architecture\n"));
1379 fprintf (stream, _("\
1424c35d
AM
1380-mpower10, -mpwr10 generate code for Power10 architecture\n"));
1381 fprintf (stream, _("\
5f63875b
AM
1382-mcell generate code for Cell Broadband Engine architecture\n"));
1383 fprintf (stream, _("\
1384-mcom generate code for Power/PowerPC common instructions\n"));
1385 fprintf (stream, _("\
ce3d2015 1386-many generate code for any architecture (PWR/PWRX/PPC)\n"));
6a0c61b7 1387 fprintf (stream, _("\
5f63875b
AM
1388-maltivec generate code for AltiVec\n"));
1389 fprintf (stream, _("\
1390-mvsx generate code for Vector-Scalar (VSX) instructions\n"));
1391 fprintf (stream, _("\
1392-me300 generate code for PowerPC e300 family\n"));
1393 fprintf (stream, _("\
1394-me500, -me500x2 generate code for Motorola e500 core complex\n"));
1395 fprintf (stream, _("\
1396-me500mc, generate code for Freescale e500mc core complex\n"));
1397 fprintf (stream, _("\
1398-me500mc64, generate code for Freescale e500mc64 core complex\n"));
1399 fprintf (stream, _("\
1400-me5500, generate code for Freescale e5500 core complex\n"));
1401 fprintf (stream, _("\
1402-me6500, generate code for Freescale e6500 core complex\n"));
1403 fprintf (stream, _("\
1404-mspe generate code for Motorola SPE instructions\n"));
1405 fprintf (stream, _("\
1406-mspe2 generate code for Freescale SPE2 instructions\n"));
1407 fprintf (stream, _("\
1408-mvle generate code for Freescale VLE instructions\n"));
1409 fprintf (stream, _("\
1410-mtitan generate code for AppliedMicro Titan core complex\n"));
1411 fprintf (stream, _("\
1412-mregnames Allow symbolic names for registers\n"));
1413 fprintf (stream, _("\
ce3d2015 1414-mno-regnames Do not allow symbolic names for registers\n"));
252b5132 1415#ifdef OBJ_ELF
bc805888 1416 fprintf (stream, _("\
5f63875b
AM
1417-mrelocatable support for GCC's -mrelocatble option\n"));
1418 fprintf (stream, _("\
1419-mrelocatable-lib support for GCC's -mrelocatble-lib option\n"));
1420 fprintf (stream, _("\
1421-memb set PPC_EMB bit in ELF flags\n"));
1422 fprintf (stream, _("\
b8b738ac 1423-mlittle, -mlittle-endian, -le\n\
5f63875b
AM
1424 generate code for a little endian machine\n"));
1425 fprintf (stream, _("\
b8b738ac 1426-mbig, -mbig-endian, -be\n\
5f63875b
AM
1427 generate code for a big endian machine\n"));
1428 fprintf (stream, _("\
1429-msolaris generate code for Solaris\n"));
1430 fprintf (stream, _("\
1431-mno-solaris do not generate code for Solaris\n"));
1432 fprintf (stream, _("\
1433-K PIC set EF_PPC_RELOCATABLE_LIB in ELF flags\n"));
1434 fprintf (stream, _("\
1435-V print assembler version number\n"));
1436 fprintf (stream, _("\
ce3d2015 1437-Qy, -Qn ignored\n"));
252b5132 1438#endif
cef4f754 1439 fprintf (stream, _("\
5f63875b
AM
1440-nops=count when aligning, more than COUNT nops uses a branch\n"));
1441 fprintf (stream, _("\
bf7279d5 1442-ppc476-workaround warn if emitting data to code sections\n"));
252b5132
RH
1443}
1444\f
1445/* Set ppc_cpu if it is not already set. */
1446
1447static void
98027b10 1448ppc_set_cpu (void)
252b5132
RH
1449{
1450 const char *default_os = TARGET_OS;
1451 const char *default_cpu = TARGET_CPU;
1452
7102e95e 1453 if ((ppc_cpu & ~(ppc_cpu_t) PPC_OPCODE_ANY) == 0)
252b5132 1454 {
2a98c3a6 1455 if (ppc_obj64)
f2ab4b42
PB
1456 if (target_big_endian)
1457 ppc_cpu |= PPC_OPCODE_PPC | PPC_OPCODE_64;
1458 else
1459 /* The minimum supported cpu for 64-bit little-endian is power8. */
1460 ppc_cpu |= ppc_parse_cpu (ppc_cpu, &sticky, "power8");
2a98c3a6
AM
1461 else if (strncmp (default_os, "aix", 3) == 0
1462 && default_os[3] >= '4' && default_os[3] <= '9')
bdc70b4a 1463 ppc_cpu |= PPC_OPCODE_COMMON;
252b5132 1464 else if (strncmp (default_os, "aix3", 4) == 0)
bdc70b4a 1465 ppc_cpu |= PPC_OPCODE_POWER;
252b5132 1466 else if (strcmp (default_cpu, "rs6000") == 0)
bdc70b4a 1467 ppc_cpu |= PPC_OPCODE_POWER;
0baf16f2 1468 else if (strncmp (default_cpu, "powerpc", 7) == 0)
bdc70b4a 1469 ppc_cpu |= PPC_OPCODE_PPC;
252b5132 1470 else
d6ed37ed 1471 as_fatal (_("unknown default cpu = %s, os = %s"),
99a814a1 1472 default_cpu, default_os);
252b5132
RH
1473 }
1474}
1475
9232bbb0
AM
1476/* Figure out the BFD architecture to use. This function and ppc_mach
1477 are called well before md_begin, when the output file is opened. */
252b5132
RH
1478
1479enum bfd_architecture
98027b10 1480ppc_arch (void)
252b5132
RH
1481{
1482 const char *default_cpu = TARGET_CPU;
1483 ppc_set_cpu ();
1484
1485 if ((ppc_cpu & PPC_OPCODE_PPC) != 0)
1486 return bfd_arch_powerpc;
b9c361e0
JL
1487 if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
1488 return bfd_arch_powerpc;
1489 if ((ppc_cpu & PPC_OPCODE_POWER) != 0)
252b5132 1490 return bfd_arch_rs6000;
b9c361e0 1491 if ((ppc_cpu & (PPC_OPCODE_COMMON | PPC_OPCODE_ANY)) != 0)
252b5132
RH
1492 {
1493 if (strcmp (default_cpu, "rs6000") == 0)
1494 return bfd_arch_rs6000;
0baf16f2 1495 else if (strncmp (default_cpu, "powerpc", 7) == 0)
252b5132
RH
1496 return bfd_arch_powerpc;
1497 }
1498
d6ed37ed 1499 as_fatal (_("neither Power nor PowerPC opcodes were selected."));
252b5132
RH
1500 return bfd_arch_unknown;
1501}
1502
7f6d05e8 1503unsigned long
98027b10 1504ppc_mach (void)
7f6d05e8 1505{
2a98c3a6
AM
1506 if (ppc_obj64)
1507 return bfd_mach_ppc64;
1508 else if (ppc_arch () == bfd_arch_rs6000)
1509 return bfd_mach_rs6k;
ce3d2015
AM
1510 else if (ppc_cpu & PPC_OPCODE_TITAN)
1511 return bfd_mach_ppc_titan;
b9c361e0
JL
1512 else if (ppc_cpu & PPC_OPCODE_VLE)
1513 return bfd_mach_ppc_vle;
2a98c3a6
AM
1514 else
1515 return bfd_mach_ppc;
7f6d05e8
CP
1516}
1517
e0471c16 1518extern const char*
98027b10 1519ppc_target_format (void)
7f6d05e8
CP
1520{
1521#ifdef OBJ_COFF
fe49679d 1522#if TE_POWERMAC
0baf16f2 1523 return "xcoff-powermac";
7f6d05e8 1524#else
eb1e0e80 1525# ifdef TE_AIX5
edc1d652 1526 return (ppc_obj64 ? "aix5coff64-rs6000" : "aixcoff-rs6000");
eb1e0e80 1527# else
edc1d652 1528 return (ppc_obj64 ? "aixcoff64-rs6000" : "aixcoff-rs6000");
eb1e0e80 1529# endif
7f6d05e8 1530#endif
7f6d05e8
CP
1531#endif
1532#ifdef OBJ_ELF
edc1d652
AM
1533# ifdef TE_FreeBSD
1534 return (ppc_obj64 ? "elf64-powerpc-freebsd" : "elf32-powerpc-freebsd");
1535# elif defined (TE_VXWORKS)
9d8504b1
PB
1536 return "elf32-powerpc-vxworks";
1537# else
0baf16f2 1538 return (target_big_endian
2b3c4602
AM
1539 ? (ppc_obj64 ? "elf64-powerpc" : "elf32-powerpc")
1540 : (ppc_obj64 ? "elf64-powerpcle" : "elf32-powerpcle"));
9d8504b1 1541# endif
7f6d05e8
CP
1542#endif
1543}
1544
b9c361e0
JL
1545/* Validate one entry in powerpc_opcodes[] or vle_opcodes[].
1546 Return TRUE if there's a problem, otherwise FALSE. */
1547
5b7c81bd 1548static bool
b9c361e0
JL
1549insn_validate (const struct powerpc_opcode *op)
1550{
1551 const unsigned char *o;
0f873fd5 1552 uint64_t omask = op->mask;
b9c361e0
JL
1553
1554 /* The mask had better not trim off opcode bits. */
1555 if ((op->opcode & omask) != op->opcode)
1556 {
1557 as_bad (_("mask trims opcode bits for %s"), op->name);
5b7c81bd 1558 return true;
b9c361e0
JL
1559 }
1560
1561 /* The operands must not overlap the opcode or each other. */
1562 for (o = op->operands; *o; ++o)
1563 {
5b7c81bd 1564 bool optional = false;
b9c361e0
JL
1565 if (*o >= num_powerpc_operands)
1566 {
1567 as_bad (_("operand index error for %s"), op->name);
5b7c81bd 1568 return true;
b9c361e0
JL
1569 }
1570 else
1571 {
71553718 1572 uint64_t mask;
b9c361e0 1573 const struct powerpc_operand *operand = &powerpc_operands[*o];
71553718 1574 if (operand->shift == (int) PPC_OPSHIFT_INV)
b9c361e0 1575 {
71553718
AM
1576 const char *errmsg;
1577 int64_t val;
1578
1579 errmsg = NULL;
1580 val = -1;
1581 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
1582 val = -val;
1583 else if ((operand->flags & PPC_OPERAND_PLUS1) != 0)
1584 val += 1;
1585 mask = (*operand->insert) (0, val, ppc_cpu, &errmsg);
b9c361e0 1586 }
71553718
AM
1587 else if (operand->shift >= 0)
1588 mask = operand->bitm << operand->shift;
1589 else
1590 mask = operand->bitm >> -operand->shift;
1591 if (omask & mask)
1592 {
1593 as_bad (_("operand %d overlap in %s"),
1594 (int) (o - op->operands), op->name);
5b7c81bd 1595 return true;
71553718
AM
1596 }
1597 omask |= mask;
2eac3da1 1598 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0)
5b7c81bd 1599 optional = true;
2eac3da1
AM
1600 else if (optional)
1601 {
1602 as_bad (_("non-optional operand %d follows optional operand in %s"),
1603 (int) (o - op->operands), op->name);
5b7c81bd 1604 return true;
2eac3da1 1605 }
b9c361e0
JL
1606 }
1607 }
5b7c81bd 1608 return false;
b9c361e0
JL
1609}
1610
69c040df 1611/* Insert opcodes and macros into hash tables. Called at startup and
1fe532cf 1612 for .machine pseudo. */
252b5132 1613
69c040df
AM
1614static void
1615ppc_setup_opcodes (void)
252b5132 1616{
98027b10 1617 const struct powerpc_opcode *op;
252b5132
RH
1618 const struct powerpc_opcode *op_end;
1619 const struct powerpc_macro *macro;
1620 const struct powerpc_macro *macro_end;
5b7c81bd 1621 bool bad_insn = false;
252b5132 1622
69c040df 1623 if (ppc_hash != NULL)
629310ab 1624 htab_delete (ppc_hash);
69c040df 1625 if (ppc_macro_hash != NULL)
629310ab 1626 htab_delete (ppc_macro_hash);
252b5132
RH
1627
1628 /* Insert the opcodes into a hash table. */
629310ab 1629 ppc_hash = str_htab_create ();
252b5132 1630
c43a438d 1631 if (ENABLE_CHECKING)
b84bf58a 1632 {
c43a438d 1633 unsigned int i;
b84bf58a 1634
3b8b57a9
AM
1635 /* An index into powerpc_operands is stored in struct fix
1636 fx_pcrel_adjust which is 8 bits wide. */
1637 gas_assert (num_powerpc_operands < 256);
1638
c43a438d
AM
1639 /* Check operand masks. Code here and in the disassembler assumes
1640 all the 1's in the mask are contiguous. */
1641 for (i = 0; i < num_powerpc_operands; ++i)
b84bf58a 1642 {
0f873fd5
PB
1643 uint64_t mask = powerpc_operands[i].bitm;
1644 uint64_t right_bit;
c43a438d
AM
1645 unsigned int j;
1646
1647 right_bit = mask & -mask;
1648 mask += right_bit;
1649 right_bit = mask & -mask;
1650 if (mask != right_bit)
1651 {
1652 as_bad (_("powerpc_operands[%d].bitm invalid"), i);
5b7c81bd 1653 bad_insn = true;
c43a438d
AM
1654 }
1655 for (j = i + 1; j < num_powerpc_operands; ++j)
1656 if (memcmp (&powerpc_operands[i], &powerpc_operands[j],
1657 sizeof (powerpc_operands[0])) == 0)
1658 {
1659 as_bad (_("powerpc_operands[%d] duplicates powerpc_operands[%d]"),
1660 j, i);
5b7c81bd 1661 bad_insn = true;
c43a438d 1662 }
b84bf58a
AM
1663 }
1664 }
1665
252b5132
RH
1666 op_end = powerpc_opcodes + powerpc_num_opcodes;
1667 for (op = powerpc_opcodes; op < op_end; op++)
1668 {
c43a438d 1669 if (ENABLE_CHECKING)
b84bf58a 1670 {
2ceb7719 1671 unsigned int new_opcode = PPC_OP (op[0].opcode);
b9c361e0
JL
1672
1673#ifdef PRINT_OPCODE_TABLE
2ceb7719
PB
1674 printf ("%-14s\t#%04u\tmajor op: 0x%x\top: 0x%llx\tmask: 0x%llx\tflags: 0x%llx\n",
1675 op->name, (unsigned int) (op - powerpc_opcodes),
1676 new_opcode, (unsigned long long) op->opcode,
1677 (unsigned long long) op->mask, (unsigned long long) op->flags);
b9c361e0
JL
1678#endif
1679
2ceb7719
PB
1680 /* The major opcodes had better be sorted. Code in the disassembler
1681 assumes the insns are sorted according to major opcode. */
1682 if (op != powerpc_opcodes
1683 && new_opcode < PPC_OP (op[-1].opcode))
1684 {
1685 as_bad (_("major opcode is not sorted for %s"), op->name);
5b7c81bd 1686 bad_insn = true;
8dbcd839 1687 }
2ceb7719 1688
14b57c7c
AM
1689 if ((op->flags & PPC_OPCODE_VLE) != 0)
1690 {
1691 as_bad (_("%s is enabled by vle flag"), op->name);
5b7c81bd 1692 bad_insn = true;
14b57c7c
AM
1693 }
1694 if (PPC_OP (op->opcode) != 4
1695 && PPC_OP (op->opcode) != 31
1696 && (op->deprecated & PPC_OPCODE_VLE) == 0)
1697 {
1698 as_bad (_("%s not disabled by vle flag"), op->name);
5b7c81bd 1699 bad_insn = true;
14b57c7c 1700 }
b9c361e0
JL
1701 bad_insn |= insn_validate (op);
1702 }
c43a438d 1703
b9c361e0 1704 if ((ppc_cpu & op->flags) != 0
fe0e921f
AM
1705 && !(ppc_cpu & op->deprecated)
1706 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1707 {
1708 as_bad (_("duplicate %s"), op->name);
5b7c81bd 1709 bad_insn = true;
fe0e921f 1710 }
b9c361e0 1711 }
c43a438d 1712
b9c361e0
JL
1713 if ((ppc_cpu & PPC_OPCODE_ANY) != 0)
1714 for (op = powerpc_opcodes; op < op_end; op++)
fe0e921f 1715 str_hash_insert (ppc_hash, op->name, op, 0);
b9c361e0 1716
dd7efa79
PB
1717 op_end = prefix_opcodes + prefix_num_opcodes;
1718 for (op = prefix_opcodes; op < op_end; op++)
1719 {
1720 if (ENABLE_CHECKING)
1721 {
1722 unsigned int new_opcode = PPC_PREFIX_SEG (op[0].opcode);
1723
1724#ifdef PRINT_OPCODE_TABLE
1725 printf ("%-14s\t#%04u\tmajor op/2: 0x%x\top: 0x%llx\tmask: 0x%llx\tflags: 0x%llx\n",
1726 op->name, (unsigned int) (op - prefix_opcodes),
1727 new_opcode, (unsigned long long) op->opcode,
1728 (unsigned long long) op->mask, (unsigned long long) op->flags);
1729#endif
1730
1731 /* The major opcodes had better be sorted. Code in the disassembler
1732 assumes the insns are sorted according to major opcode. */
1733 if (op != prefix_opcodes
1734 && new_opcode < PPC_PREFIX_SEG (op[-1].opcode))
1735 {
1736 as_bad (_("major opcode is not sorted for %s"), op->name);
5b7c81bd 1737 bad_insn = true;
dd7efa79
PB
1738 }
1739 bad_insn |= insn_validate (op);
1740 }
1741
1742 if ((ppc_cpu & op->flags) != 0
fe0e921f
AM
1743 && !(ppc_cpu & op->deprecated)
1744 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1745 {
1746 as_bad (_("duplicate %s"), op->name);
5b7c81bd 1747 bad_insn = true;
fe0e921f 1748 }
dd7efa79
PB
1749 }
1750
1751 if ((ppc_cpu & PPC_OPCODE_ANY) != 0)
1752 for (op = prefix_opcodes; op < op_end; op++)
fe0e921f 1753 str_hash_insert (ppc_hash, op->name, op, 0);
dd7efa79 1754
b9c361e0
JL
1755 op_end = vle_opcodes + vle_num_opcodes;
1756 for (op = vle_opcodes; op < op_end; op++)
1757 {
1758 if (ENABLE_CHECKING)
1759 {
2ceb7719 1760 unsigned new_seg = VLE_OP_TO_SEG (VLE_OP (op[0].opcode, op[0].mask));
b9c361e0
JL
1761
1762#ifdef PRINT_OPCODE_TABLE
2ceb7719
PB
1763 printf ("%-14s\t#%04u\tmajor op: 0x%x\top: 0x%llx\tmask: 0x%llx\tflags: 0x%llx\n",
1764 op->name, (unsigned int) (op - vle_opcodes),
1765 (unsigned int) new_seg, (unsigned long long) op->opcode,
1766 (unsigned long long) op->mask, (unsigned long long) op->flags);
b9c361e0 1767#endif
2ceb7719
PB
1768
1769 /* The major opcodes had better be sorted. Code in the disassembler
1770 assumes the insns are sorted according to major opcode. */
1771 if (op != vle_opcodes
1772 && new_seg < VLE_OP_TO_SEG (VLE_OP (op[-1].opcode, op[-1].mask)))
1773 {
1774 as_bad (_("major opcode is not sorted for %s"), op->name);
5b7c81bd 1775 bad_insn = true;
b9c361e0
JL
1776 }
1777
1778 bad_insn |= insn_validate (op);
c43a438d 1779 }
252b5132 1780
bdc70b4a 1781 if ((ppc_cpu & op->flags) != 0
fe0e921f
AM
1782 && !(ppc_cpu & op->deprecated)
1783 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1784 {
1785 as_bad (_("duplicate %s"), op->name);
5b7c81bd 1786 bad_insn = true;
fe0e921f 1787 }
252b5132
RH
1788 }
1789
74081948
AF
1790 /* SPE2 instructions */
1791 if ((ppc_cpu & PPC_OPCODE_SPE2) == PPC_OPCODE_SPE2)
1792 {
1793 op_end = spe2_opcodes + spe2_num_opcodes;
1794 for (op = spe2_opcodes; op < op_end; op++)
1795 {
1796 if (ENABLE_CHECKING)
1797 {
1798 if (op != spe2_opcodes)
1799 {
1800 unsigned old_seg, new_seg;
1801
1802 old_seg = VLE_OP (op[-1].opcode, op[-1].mask);
1803 old_seg = VLE_OP_TO_SEG (old_seg);
1804 new_seg = VLE_OP (op[0].opcode, op[0].mask);
1805 new_seg = VLE_OP_TO_SEG (new_seg);
1806
1807 /* The major opcodes had better be sorted. Code in the
1808 disassembler assumes the insns are sorted according to
1809 major opcode. */
1810 if (new_seg < old_seg)
1811 {
1812 as_bad (_("major opcode is not sorted for %s"), op->name);
5b7c81bd 1813 bad_insn = true;
74081948
AF
1814 }
1815 }
1816
1817 bad_insn |= insn_validate (op);
1818 }
1819
fe0e921f
AM
1820 if ((ppc_cpu & op->flags) != 0
1821 && !(ppc_cpu & op->deprecated)
1822 && str_hash_insert (ppc_hash, op->name, op, 0) != NULL)
1823 {
1824 as_bad (_("duplicate %s"), op->name);
5b7c81bd 1825 bad_insn = true;
fe0e921f 1826 }
74081948
AF
1827 }
1828
1829 for (op = spe2_opcodes; op < op_end; op++)
fe0e921f 1830 str_hash_insert (ppc_hash, op->name, op, 0);
74081948
AF
1831 }
1832
252b5132 1833 /* Insert the macros into a hash table. */
629310ab 1834 ppc_macro_hash = str_htab_create ();
252b5132
RH
1835
1836 macro_end = powerpc_macros + powerpc_num_macros;
1837 for (macro = powerpc_macros; macro < macro_end; macro++)
fe0e921f
AM
1838 if (((macro->flags & ppc_cpu) != 0
1839 || (ppc_cpu & PPC_OPCODE_ANY) != 0)
1840 && str_hash_insert (ppc_macro_hash, macro->name, macro, 0) != NULL)
1841 {
1842 as_bad (_("duplicate %s"), macro->name);
5b7c81bd 1843 bad_insn = true;
fe0e921f 1844 }
252b5132 1845
b84bf58a 1846 if (bad_insn)
252b5132 1847 abort ();
69c040df
AM
1848}
1849
1850/* This function is called when the assembler starts up. It is called
1851 after the options have been parsed and the output file has been
1852 opened. */
1853
1854void
98027b10 1855md_begin (void)
69c040df
AM
1856{
1857 ppc_set_cpu ();
1858
1859 ppc_cie_data_alignment = ppc_obj64 ? -8 : -4;
8fbf7334 1860 ppc_dwarf2_line_min_insn_length = (ppc_cpu & PPC_OPCODE_VLE) ? 2 : 4;
69c040df
AM
1861
1862#ifdef OBJ_ELF
1863 /* Set the ELF flags if desired. */
1864 if (ppc_flags && !msolaris)
1865 bfd_set_private_flags (stdoutput, ppc_flags);
1866#endif
1867
1868 ppc_setup_opcodes ();
252b5132 1869
67c1ffbe 1870 /* Tell the main code what the endianness is if it is not overridden
99a814a1 1871 by the user. */
252b5132
RH
1872 if (!set_target_endian)
1873 {
1874 set_target_endian = 1;
1875 target_big_endian = PPC_BIG_ENDIAN;
1876 }
1877
1878#ifdef OBJ_XCOFF
1879 ppc_coff_debug_section = coff_section_from_bfd_index (stdoutput, N_DEBUG);
1880
1b2cb8e2
CC
1881 /* Create XCOFF sections with .text in first, as it's creating dummy symbols
1882 to serve as initial csects. This forces the text csects to precede the
1883 data csects. These symbols will not be output. */
5b7c81bd
AM
1884 ppc_init_xcoff_section (&ppc_xcoff_text_section, text_section, true);
1885 ppc_init_xcoff_section (&ppc_xcoff_data_section, data_section, true);
1886 ppc_init_xcoff_section (&ppc_xcoff_bss_section, bss_section, false);
252b5132 1887#endif
252b5132
RH
1888}
1889
6a0c61b7 1890void
98027b10 1891ppc_cleanup (void)
6a0c61b7 1892{
dc1d03fc 1893#ifdef OBJ_ELF
6a0c61b7
EZ
1894 if (ppc_apuinfo_list == NULL)
1895 return;
1896
1897 /* Ok, so write the section info out. We have this layout:
1898
1899 byte data what
1900 ---- ---- ----
1901 0 8 length of "APUinfo\0"
1902 4 (n*4) number of APU's (4 bytes each)
1903 8 2 note type 2
1904 12 "APUinfo\0" name
1905 20 APU#1 first APU's info
1906 24 APU#2 second APU's info
1907 ... ...
1908 */
1909 {
1910 char *p;
1911 asection *seg = now_seg;
1912 subsegT subseg = now_subseg;
1913 asection *apuinfo_secp = (asection *) NULL;
49181a6a 1914 unsigned int i;
6a0c61b7
EZ
1915
1916 /* Create the .PPC.EMB.apuinfo section. */
14b57c7c 1917 apuinfo_secp = subseg_new (APUINFO_SECTION_NAME, 0);
fd361982 1918 bfd_set_section_flags (apuinfo_secp, SEC_HAS_CONTENTS | SEC_READONLY);
6a0c61b7
EZ
1919
1920 p = frag_more (4);
1921 md_number_to_chars (p, (valueT) 8, 4);
1922
1923 p = frag_more (4);
e98d298c 1924 md_number_to_chars (p, (valueT) ppc_apuinfo_num * 4, 4);
6a0c61b7
EZ
1925
1926 p = frag_more (4);
1927 md_number_to_chars (p, (valueT) 2, 4);
1928
1929 p = frag_more (8);
14b57c7c 1930 strcpy (p, APUINFO_LABEL);
6a0c61b7
EZ
1931
1932 for (i = 0; i < ppc_apuinfo_num; i++)
1933 {
b34976b6
AM
1934 p = frag_more (4);
1935 md_number_to_chars (p, (valueT) ppc_apuinfo_list[i], 4);
6a0c61b7
EZ
1936 }
1937
1938 frag_align (2, 0, 0);
1939
1940 /* We probably can't restore the current segment, for there likely
1941 isn't one yet... */
1942 if (seg && subseg)
1943 subseg_set (seg, subseg);
1944 }
dc1d03fc 1945#endif
6a0c61b7
EZ
1946}
1947
252b5132
RH
1948/* Insert an operand value into an instruction. */
1949
0f873fd5
PB
1950static uint64_t
1951ppc_insert_operand (uint64_t insn,
a1867a27 1952 const struct powerpc_operand *operand,
0f873fd5 1953 int64_t val,
91d6fa6a 1954 ppc_cpu_t cpu,
3b4dbbbf 1955 const char *file,
a1867a27 1956 unsigned int line)
252b5132 1957{
0f873fd5 1958 int64_t min, max, right;
eb42fac1 1959
b84bf58a
AM
1960 max = operand->bitm;
1961 right = max & -max;
1962 min = 0;
1963
a47622ac 1964 if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0)
252b5132 1965 {
a255f00a
AM
1966 /* Extend the allowed range for addis to [-32768, 65535].
1967 Similarly for cmpli and some VLE high part insns. For 64-bit
1968 it would be good to disable this for signed fields since the
a47622ac
AM
1969 value is sign extended into the high 32 bits of the register.
1970 If the value is, say, an address, then we might care about
1971 the high bits. However, gcc as of 2014-06 uses unsigned
1972 values when loading the high part of 64-bit constants using
a255f00a
AM
1973 lis. */
1974 min = ~(max >> 1) & -right;
a47622ac
AM
1975 }
1976 else if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
1977 {
1978 max = (max >> 1) & -right;
931774a9 1979 min = ~max & -right;
b84bf58a 1980 }
252b5132 1981
b84bf58a 1982 if ((operand->flags & PPC_OPERAND_PLUS1) != 0)
3896c469 1983 max++;
252b5132 1984
b84bf58a 1985 if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0)
a1867a27 1986 {
0f873fd5 1987 int64_t tmp = min;
a1867a27
AM
1988 min = -max;
1989 max = -tmp;
1990 }
b84bf58a 1991
a1867a27
AM
1992 if (min <= max)
1993 {
1994 /* Some people write constants with the sign extension done by
1995 hand but only up to 32 bits. This shouldn't really be valid,
1996 but, to permit this code to assemble on a 64-bit host, we
1997 sign extend the 32-bit value to 64 bits if so doing makes the
8acf1435
PB
1998 value valid. We only do this for operands that are 32-bits or
1999 smaller. */
a1867a27 2000 if (val > max
8acf1435 2001 && (operand->bitm & ~0xffffffffULL) == 0
0f873fd5
PB
2002 && (val - (1LL << 32)) >= min
2003 && (val - (1LL << 32)) <= max
2004 && ((val - (1LL << 32)) & (right - 1)) == 0)
2005 val = val - (1LL << 32);
a1867a27
AM
2006
2007 /* Similarly, people write expressions like ~(1<<15), and expect
2008 this to be OK for a 32-bit unsigned value. */
2009 else if (val < min
8acf1435 2010 && (operand->bitm & ~0xffffffffULL) == 0
0f873fd5
PB
2011 && (val + (1LL << 32)) >= min
2012 && (val + (1LL << 32)) <= max
2013 && ((val + (1LL << 32)) & (right - 1)) == 0)
2014 val = val + (1LL << 32);
a1867a27
AM
2015
2016 else if (val < min
2017 || val > max
2018 || (val & (right - 1)) != 0)
2019 as_bad_value_out_of_range (_("operand"), val, min, max, file, line);
2020 }
b84bf58a 2021
252b5132
RH
2022 if (operand->insert)
2023 {
2024 const char *errmsg;
2025
2026 errmsg = NULL;
0f873fd5 2027 insn = (*operand->insert) (insn, val, cpu, &errmsg);
252b5132 2028 if (errmsg != (const char *) NULL)
ee2c9aa9 2029 as_bad_where (file, line, "%s", errmsg);
252b5132 2030 }
b9c361e0 2031 else if (operand->shift >= 0)
0f873fd5 2032 insn |= (val & operand->bitm) << operand->shift;
b9c361e0 2033 else
0f873fd5 2034 insn |= (val & operand->bitm) >> -operand->shift;
252b5132
RH
2035
2036 return insn;
2037}
2038
2039\f
2040#ifdef OBJ_ELF
2041/* Parse @got, etc. and return the desired relocation. */
2042static bfd_reloc_code_real_type
98027b10 2043ppc_elf_suffix (char **str_p, expressionS *exp_p)
252b5132
RH
2044{
2045 struct map_bfd {
e0471c16 2046 const char *string;
b7d7dc63
AM
2047 unsigned int length : 8;
2048 unsigned int valid32 : 1;
2049 unsigned int valid64 : 1;
2050 unsigned int reloc;
252b5132
RH
2051 };
2052
2053 char ident[20];
2054 char *str = *str_p;
2055 char *str2;
2056 int ch;
2057 int len;
15c1449b 2058 const struct map_bfd *ptr;
252b5132 2059
b7d7dc63
AM
2060#define MAP(str, reloc) { str, sizeof (str) - 1, 1, 1, reloc }
2061#define MAP32(str, reloc) { str, sizeof (str) - 1, 1, 0, reloc }
2062#define MAP64(str, reloc) { str, sizeof (str) - 1, 0, 1, reloc }
252b5132 2063
15c1449b 2064 static const struct map_bfd mapping[] = {
b7d7dc63
AM
2065 MAP ("l", BFD_RELOC_LO16),
2066 MAP ("h", BFD_RELOC_HI16),
2067 MAP ("ha", BFD_RELOC_HI16_S),
2068 MAP ("brtaken", BFD_RELOC_PPC_B16_BRTAKEN),
2069 MAP ("brntaken", BFD_RELOC_PPC_B16_BRNTAKEN),
2070 MAP ("got", BFD_RELOC_16_GOTOFF),
2071 MAP ("got@l", BFD_RELOC_LO16_GOTOFF),
2072 MAP ("got@h", BFD_RELOC_HI16_GOTOFF),
2073 MAP ("got@ha", BFD_RELOC_HI16_S_GOTOFF),
2074 MAP ("plt@l", BFD_RELOC_LO16_PLTOFF),
2075 MAP ("plt@h", BFD_RELOC_HI16_PLTOFF),
2076 MAP ("plt@ha", BFD_RELOC_HI16_S_PLTOFF),
2077 MAP ("copy", BFD_RELOC_PPC_COPY),
2078 MAP ("globdat", BFD_RELOC_PPC_GLOB_DAT),
2079 MAP ("sectoff", BFD_RELOC_16_BASEREL),
2080 MAP ("sectoff@l", BFD_RELOC_LO16_BASEREL),
2081 MAP ("sectoff@h", BFD_RELOC_HI16_BASEREL),
2082 MAP ("sectoff@ha", BFD_RELOC_HI16_S_BASEREL),
2083 MAP ("tls", BFD_RELOC_PPC_TLS),
2084 MAP ("dtpmod", BFD_RELOC_PPC_DTPMOD),
2085 MAP ("dtprel", BFD_RELOC_PPC_DTPREL),
2086 MAP ("dtprel@l", BFD_RELOC_PPC_DTPREL16_LO),
2087 MAP ("dtprel@h", BFD_RELOC_PPC_DTPREL16_HI),
2088 MAP ("dtprel@ha", BFD_RELOC_PPC_DTPREL16_HA),
2089 MAP ("tprel", BFD_RELOC_PPC_TPREL),
2090 MAP ("tprel@l", BFD_RELOC_PPC_TPREL16_LO),
2091 MAP ("tprel@h", BFD_RELOC_PPC_TPREL16_HI),
2092 MAP ("tprel@ha", BFD_RELOC_PPC_TPREL16_HA),
2093 MAP ("got@tlsgd", BFD_RELOC_PPC_GOT_TLSGD16),
2094 MAP ("got@tlsgd@l", BFD_RELOC_PPC_GOT_TLSGD16_LO),
2095 MAP ("got@tlsgd@h", BFD_RELOC_PPC_GOT_TLSGD16_HI),
2096 MAP ("got@tlsgd@ha", BFD_RELOC_PPC_GOT_TLSGD16_HA),
2097 MAP ("got@tlsld", BFD_RELOC_PPC_GOT_TLSLD16),
2098 MAP ("got@tlsld@l", BFD_RELOC_PPC_GOT_TLSLD16_LO),
2099 MAP ("got@tlsld@h", BFD_RELOC_PPC_GOT_TLSLD16_HI),
2100 MAP ("got@tlsld@ha", BFD_RELOC_PPC_GOT_TLSLD16_HA),
2101 MAP ("got@dtprel", BFD_RELOC_PPC_GOT_DTPREL16),
2102 MAP ("got@dtprel@l", BFD_RELOC_PPC_GOT_DTPREL16_LO),
2103 MAP ("got@dtprel@h", BFD_RELOC_PPC_GOT_DTPREL16_HI),
2104 MAP ("got@dtprel@ha", BFD_RELOC_PPC_GOT_DTPREL16_HA),
2105 MAP ("got@tprel", BFD_RELOC_PPC_GOT_TPREL16),
2106 MAP ("got@tprel@l", BFD_RELOC_PPC_GOT_TPREL16_LO),
2107 MAP ("got@tprel@h", BFD_RELOC_PPC_GOT_TPREL16_HI),
2108 MAP ("got@tprel@ha", BFD_RELOC_PPC_GOT_TPREL16_HA),
2109 MAP32 ("fixup", BFD_RELOC_CTOR),
2110 MAP32 ("plt", BFD_RELOC_24_PLT_PCREL),
2111 MAP32 ("pltrel24", BFD_RELOC_24_PLT_PCREL),
2112 MAP32 ("local24pc", BFD_RELOC_PPC_LOCAL24PC),
2113 MAP32 ("local", BFD_RELOC_PPC_LOCAL24PC),
2114 MAP32 ("pltrel", BFD_RELOC_32_PLT_PCREL),
2115 MAP32 ("sdarel", BFD_RELOC_GPREL16),
b9c361e0
JL
2116 MAP32 ("sdarel@l", BFD_RELOC_PPC_VLE_SDAREL_LO16A),
2117 MAP32 ("sdarel@h", BFD_RELOC_PPC_VLE_SDAREL_HI16A),
2118 MAP32 ("sdarel@ha", BFD_RELOC_PPC_VLE_SDAREL_HA16A),
b7d7dc63
AM
2119 MAP32 ("naddr", BFD_RELOC_PPC_EMB_NADDR32),
2120 MAP32 ("naddr16", BFD_RELOC_PPC_EMB_NADDR16),
2121 MAP32 ("naddr@l", BFD_RELOC_PPC_EMB_NADDR16_LO),
2122 MAP32 ("naddr@h", BFD_RELOC_PPC_EMB_NADDR16_HI),
2123 MAP32 ("naddr@ha", BFD_RELOC_PPC_EMB_NADDR16_HA),
2124 MAP32 ("sdai16", BFD_RELOC_PPC_EMB_SDAI16),
2125 MAP32 ("sda2rel", BFD_RELOC_PPC_EMB_SDA2REL),
2126 MAP32 ("sda2i16", BFD_RELOC_PPC_EMB_SDA2I16),
2127 MAP32 ("sda21", BFD_RELOC_PPC_EMB_SDA21),
b9c361e0 2128 MAP32 ("sda21@l", BFD_RELOC_PPC_VLE_SDA21_LO),
b7d7dc63
AM
2129 MAP32 ("mrkref", BFD_RELOC_PPC_EMB_MRKREF),
2130 MAP32 ("relsect", BFD_RELOC_PPC_EMB_RELSEC16),
2131 MAP32 ("relsect@l", BFD_RELOC_PPC_EMB_RELST_LO),
2132 MAP32 ("relsect@h", BFD_RELOC_PPC_EMB_RELST_HI),
2133 MAP32 ("relsect@ha", BFD_RELOC_PPC_EMB_RELST_HA),
2134 MAP32 ("bitfld", BFD_RELOC_PPC_EMB_BIT_FLD),
2135 MAP32 ("relsda", BFD_RELOC_PPC_EMB_RELSDA),
2136 MAP32 ("xgot", BFD_RELOC_PPC_TOC16),
f9c6b907
AM
2137 MAP64 ("high", BFD_RELOC_PPC64_ADDR16_HIGH),
2138 MAP64 ("higha", BFD_RELOC_PPC64_ADDR16_HIGHA),
b7d7dc63
AM
2139 MAP64 ("higher", BFD_RELOC_PPC64_HIGHER),
2140 MAP64 ("highera", BFD_RELOC_PPC64_HIGHER_S),
2141 MAP64 ("highest", BFD_RELOC_PPC64_HIGHEST),
2142 MAP64 ("highesta", BFD_RELOC_PPC64_HIGHEST_S),
2143 MAP64 ("tocbase", BFD_RELOC_PPC64_TOC),
2144 MAP64 ("toc", BFD_RELOC_PPC_TOC16),
2145 MAP64 ("toc@l", BFD_RELOC_PPC64_TOC16_LO),
2146 MAP64 ("toc@h", BFD_RELOC_PPC64_TOC16_HI),
2147 MAP64 ("toc@ha", BFD_RELOC_PPC64_TOC16_HA),
f9c6b907
AM
2148 MAP64 ("dtprel@high", BFD_RELOC_PPC64_DTPREL16_HIGH),
2149 MAP64 ("dtprel@higha", BFD_RELOC_PPC64_DTPREL16_HIGHA),
b7d7dc63
AM
2150 MAP64 ("dtprel@higher", BFD_RELOC_PPC64_DTPREL16_HIGHER),
2151 MAP64 ("dtprel@highera", BFD_RELOC_PPC64_DTPREL16_HIGHERA),
2152 MAP64 ("dtprel@highest", BFD_RELOC_PPC64_DTPREL16_HIGHEST),
2153 MAP64 ("dtprel@highesta", BFD_RELOC_PPC64_DTPREL16_HIGHESTA),
45965137 2154 MAP64 ("localentry", BFD_RELOC_PPC64_ADDR64_LOCAL),
f9c6b907
AM
2155 MAP64 ("tprel@high", BFD_RELOC_PPC64_TPREL16_HIGH),
2156 MAP64 ("tprel@higha", BFD_RELOC_PPC64_TPREL16_HIGHA),
b7d7dc63
AM
2157 MAP64 ("tprel@higher", BFD_RELOC_PPC64_TPREL16_HIGHER),
2158 MAP64 ("tprel@highera", BFD_RELOC_PPC64_TPREL16_HIGHERA),
2159 MAP64 ("tprel@highest", BFD_RELOC_PPC64_TPREL16_HIGHEST),
2160 MAP64 ("tprel@highesta", BFD_RELOC_PPC64_TPREL16_HIGHESTA),
05d0e962 2161 MAP64 ("notoc", BFD_RELOC_PPC64_REL24_NOTOC),
5663e321
AM
2162 MAP64 ("pcrel", BFD_RELOC_PPC64_PCREL34),
2163 MAP64 ("got@pcrel", BFD_RELOC_PPC64_GOT_PCREL34),
2164 MAP64 ("plt@pcrel", BFD_RELOC_PPC64_PLT_PCREL34),
c213164a 2165 MAP64 ("tls@pcrel", BFD_RELOC_PPC64_TLS_PCREL),
87c69f97
AM
2166 MAP64 ("got@tlsgd@pcrel", BFD_RELOC_PPC64_GOT_TLSGD_PCREL34),
2167 MAP64 ("got@tlsld@pcrel", BFD_RELOC_PPC64_GOT_TLSLD_PCREL34),
2168 MAP64 ("got@tprel@pcrel", BFD_RELOC_PPC64_GOT_TPREL_PCREL34),
2169 MAP64 ("got@dtprel@pcrel", BFD_RELOC_PPC64_GOT_DTPREL_PCREL34),
5663e321
AM
2170 MAP64 ("higher34", BFD_RELOC_PPC64_ADDR16_HIGHER34),
2171 MAP64 ("highera34", BFD_RELOC_PPC64_ADDR16_HIGHERA34),
2172 MAP64 ("highest34", BFD_RELOC_PPC64_ADDR16_HIGHEST34),
2173 MAP64 ("highesta34", BFD_RELOC_PPC64_ADDR16_HIGHESTA34),
62ebcb5c 2174 { (char *) 0, 0, 0, 0, BFD_RELOC_NONE }
252b5132
RH
2175 };
2176
2177 if (*str++ != '@')
62ebcb5c 2178 return BFD_RELOC_NONE;
252b5132
RH
2179
2180 for (ch = *str, str2 = ident;
2181 (str2 < ident + sizeof (ident) - 1
3882b010 2182 && (ISALNUM (ch) || ch == '@'));
252b5132
RH
2183 ch = *++str)
2184 {
3882b010 2185 *str2++ = TOLOWER (ch);
252b5132
RH
2186 }
2187
2188 *str2 = '\0';
2189 len = str2 - ident;
2190
2191 ch = ident[0];
2192 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
2193 if (ch == ptr->string[0]
2194 && len == ptr->length
b7d7dc63
AM
2195 && memcmp (ident, ptr->string, ptr->length) == 0
2196 && (ppc_obj64 ? ptr->valid64 : ptr->valid32))
252b5132 2197 {
15c1449b
AM
2198 int reloc = ptr->reloc;
2199
727fc41e
AM
2200 if (!ppc_obj64 && exp_p->X_add_number != 0)
2201 {
2202 switch (reloc)
2203 {
2204 case BFD_RELOC_16_GOTOFF:
2205 case BFD_RELOC_LO16_GOTOFF:
2206 case BFD_RELOC_HI16_GOTOFF:
2207 case BFD_RELOC_HI16_S_GOTOFF:
e47d413a
AM
2208 as_warn (_("symbol+offset@%s means symbol@%s+offset"),
2209 ptr->string, ptr->string);
727fc41e
AM
2210 break;
2211
2212 case BFD_RELOC_PPC_GOT_TLSGD16:
2213 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
2214 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
2215 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
2216 case BFD_RELOC_PPC_GOT_TLSLD16:
2217 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
2218 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
2219 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
2220 case BFD_RELOC_PPC_GOT_DTPREL16:
2221 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
2222 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
2223 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
2224 case BFD_RELOC_PPC_GOT_TPREL16:
2225 case BFD_RELOC_PPC_GOT_TPREL16_LO:
2226 case BFD_RELOC_PPC_GOT_TPREL16_HI:
2227 case BFD_RELOC_PPC_GOT_TPREL16_HA:
e47d413a 2228 as_bad (_("symbol+offset@%s not supported"), ptr->string);
727fc41e
AM
2229 break;
2230 }
2231 }
5f6db75a
AM
2232
2233 /* Now check for identifier@suffix+constant. */
2234 if (*str == '-' || *str == '+')
252b5132 2235 {
5f6db75a
AM
2236 char *orig_line = input_line_pointer;
2237 expressionS new_exp;
2238
2239 input_line_pointer = str;
2240 expression (&new_exp);
2241 if (new_exp.X_op == O_constant)
252b5132 2242 {
5f6db75a
AM
2243 exp_p->X_add_number += new_exp.X_add_number;
2244 str = input_line_pointer;
252b5132 2245 }
5f6db75a
AM
2246
2247 if (&input_line_pointer != str_p)
2248 input_line_pointer = orig_line;
252b5132 2249 }
252b5132 2250 *str_p = str;
0baf16f2 2251
2b3c4602 2252 if (reloc == (int) BFD_RELOC_PPC64_TOC
9f2b53d7
AM
2253 && exp_p->X_op == O_symbol
2254 && strcmp (S_GET_NAME (exp_p->X_add_symbol), ".TOC.") == 0)
0baf16f2 2255 {
9f2b53d7
AM
2256 /* Change the symbol so that the dummy .TOC. symbol can be
2257 omitted from the object file. */
0baf16f2
AM
2258 exp_p->X_add_symbol = &abs_symbol;
2259 }
2260
15c1449b 2261 return (bfd_reloc_code_real_type) reloc;
252b5132
RH
2262 }
2263
62ebcb5c 2264 return BFD_RELOC_NONE;
252b5132
RH
2265}
2266
62ebcb5c 2267/* Support @got, etc. on constants emitted via .short, .int etc. */
99a814a1 2268
62ebcb5c
AM
2269bfd_reloc_code_real_type
2270ppc_elf_parse_cons (expressionS *exp, unsigned int nbytes)
2271{
2272 expression (exp);
2273 if (nbytes >= 2 && *input_line_pointer == '@')
2274 return ppc_elf_suffix (&input_line_pointer, exp);
2275 return BFD_RELOC_NONE;
252b5132
RH
2276}
2277
bf7279d5
AM
2278/* Warn when emitting data to code sections, unless we are emitting
2279 a relocation that ld --ppc476-workaround uses to recognise data
2280 *and* there was an unconditional branch prior to the data. */
2281
2282void
2283ppc_elf_cons_fix_check (expressionS *exp ATTRIBUTE_UNUSED,
2284 unsigned int nbytes, fixS *fix)
2285{
2286 if (warn_476
2287 && (now_seg->flags & SEC_CODE) != 0
2288 && (nbytes != 4
2289 || fix == NULL
2290 || !(fix->fx_r_type == BFD_RELOC_32
2291 || fix->fx_r_type == BFD_RELOC_CTOR
2292 || fix->fx_r_type == BFD_RELOC_32_PCREL)
2293 || !(last_seg == now_seg && last_subseg == now_subseg)
2294 || !((last_insn & (0x3f << 26)) == (18u << 26)
2295 || ((last_insn & (0x3f << 26)) == (16u << 26)
2296 && (last_insn & (0x14 << 21)) == (0x14 << 21))
2297 || ((last_insn & (0x3f << 26)) == (19u << 26)
2298 && (last_insn & (0x3ff << 1)) == (16u << 1)
2299 && (last_insn & (0x14 << 21)) == (0x14 << 21)))))
2300 {
2301 /* Flag that we've warned. */
2302 if (fix != NULL)
2303 fix->fx_tcbit = 1;
2304
2305 as_warn (_("data in executable section"));
2306 }
2307}
2308
252b5132
RH
2309/* Solaris pseduo op to change to the .rodata section. */
2310static void
98027b10 2311ppc_elf_rdata (int xxx)
252b5132
RH
2312{
2313 char *save_line = input_line_pointer;
2314 static char section[] = ".rodata\n";
2315
99a814a1 2316 /* Just pretend this is .section .rodata */
252b5132
RH
2317 input_line_pointer = section;
2318 obj_elf_section (xxx);
2319
2320 input_line_pointer = save_line;
2321}
2322
99a814a1 2323/* Pseudo op to make file scope bss items. */
252b5132 2324static void
98027b10 2325ppc_elf_lcomm (int xxx ATTRIBUTE_UNUSED)
252b5132 2326{
98027b10
AM
2327 char *name;
2328 char c;
2329 char *p;
252b5132 2330 offsetT size;
98027b10 2331 symbolS *symbolP;
252b5132
RH
2332 offsetT align;
2333 segT old_sec;
2334 int old_subsec;
2335 char *pfrag;
2336 int align2;
2337
d02603dc 2338 c = get_symbol_name (&name);
252b5132 2339
d02603dc 2340 /* Just after name is now '\0'. */
252b5132
RH
2341 p = input_line_pointer;
2342 *p = c;
d02603dc 2343 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
2344 if (*input_line_pointer != ',')
2345 {
d6ed37ed 2346 as_bad (_("expected comma after symbol-name: rest of line ignored."));
252b5132
RH
2347 ignore_rest_of_line ();
2348 return;
2349 }
2350
2351 input_line_pointer++; /* skip ',' */
2352 if ((size = get_absolute_expression ()) < 0)
2353 {
2354 as_warn (_(".COMMon length (%ld.) <0! Ignored."), (long) size);
2355 ignore_rest_of_line ();
2356 return;
2357 }
2358
2359 /* The third argument to .lcomm is the alignment. */
2360 if (*input_line_pointer != ',')
2361 align = 8;
2362 else
2363 {
2364 ++input_line_pointer;
2365 align = get_absolute_expression ();
2366 if (align <= 0)
2367 {
2368 as_warn (_("ignoring bad alignment"));
2369 align = 8;
2370 }
2371 }
2372
2373 *p = 0;
2374 symbolP = symbol_find_or_make (name);
2375 *p = c;
2376
2377 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
2378 {
d6ed37ed 2379 as_bad (_("ignoring attempt to re-define symbol `%s'."),
252b5132
RH
2380 S_GET_NAME (symbolP));
2381 ignore_rest_of_line ();
2382 return;
2383 }
2384
2385 if (S_GET_VALUE (symbolP) && S_GET_VALUE (symbolP) != (valueT) size)
2386 {
d6ed37ed 2387 as_bad (_("length of .lcomm \"%s\" is already %ld. Not changed to %ld."),
252b5132
RH
2388 S_GET_NAME (symbolP),
2389 (long) S_GET_VALUE (symbolP),
2390 (long) size);
2391
2392 ignore_rest_of_line ();
2393 return;
2394 }
2395
99a814a1 2396 /* Allocate_bss. */
252b5132
RH
2397 old_sec = now_seg;
2398 old_subsec = now_subseg;
2399 if (align)
2400 {
99a814a1 2401 /* Convert to a power of 2 alignment. */
252b5132
RH
2402 for (align2 = 0; (align & 1) == 0; align >>= 1, ++align2);
2403 if (align != 1)
2404 {
d6ed37ed 2405 as_bad (_("common alignment not a power of 2"));
252b5132
RH
2406 ignore_rest_of_line ();
2407 return;
2408 }
2409 }
2410 else
2411 align2 = 0;
2412
2413 record_alignment (bss_section, align2);
cbe02d4f 2414 subseg_set (bss_section, 1);
252b5132
RH
2415 if (align2)
2416 frag_align (align2, 0, 0);
2417 if (S_GET_SEGMENT (symbolP) == bss_section)
49309057
ILT
2418 symbol_get_frag (symbolP)->fr_symbol = 0;
2419 symbol_set_frag (symbolP, frag_now);
252b5132
RH
2420 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
2421 (char *) 0);
2422 *pfrag = 0;
2423 S_SET_SIZE (symbolP, size);
2424 S_SET_SEGMENT (symbolP, bss_section);
2425 subseg_set (old_sec, old_subsec);
2426 demand_empty_rest_of_line ();
2427}
2428
6911b7dc
AM
2429/* Pseudo op to set symbol local entry point. */
2430static void
2431ppc_elf_localentry (int ignore ATTRIBUTE_UNUSED)
2432{
d02603dc
NC
2433 char *name;
2434 char c = get_symbol_name (&name);
6911b7dc
AM
2435 char *p;
2436 expressionS exp;
2437 symbolS *sym;
2438 asymbol *bfdsym;
2439 elf_symbol_type *elfsym;
2440
2441 p = input_line_pointer;
2442 *p = c;
d02603dc 2443 SKIP_WHITESPACE_AFTER_NAME ();
6911b7dc
AM
2444 if (*input_line_pointer != ',')
2445 {
2446 *p = 0;
2447 as_bad (_("expected comma after name `%s' in .localentry directive"),
2448 name);
2449 *p = c;
2450 ignore_rest_of_line ();
2451 return;
2452 }
2453 input_line_pointer++;
2454 expression (&exp);
2455 if (exp.X_op == O_absent)
2456 {
2457 as_bad (_("missing expression in .localentry directive"));
2458 exp.X_op = O_constant;
2459 exp.X_add_number = 0;
2460 }
2461 *p = 0;
2462 sym = symbol_find_or_make (name);
2463 *p = c;
2464
2465 if (resolve_expression (&exp)
2466 && exp.X_op == O_constant)
2467 {
33cb30a1 2468 unsigned int encoded, ok;
6911b7dc 2469
33cb30a1
AM
2470 ok = 1;
2471 if (exp.X_add_number == 1 || exp.X_add_number == 7)
2472 encoded = exp.X_add_number << STO_PPC64_LOCAL_BIT;
6911b7dc 2473 else
33cb30a1
AM
2474 {
2475 encoded = PPC64_SET_LOCAL_ENTRY_OFFSET (exp.X_add_number);
2476 if (exp.X_add_number != (offsetT) PPC64_LOCAL_ENTRY_OFFSET (encoded))
2477 {
2478 as_bad (_(".localentry expression for `%s' "
2479 "is not a valid power of 2"), S_GET_NAME (sym));
2480 ok = 0;
2481 }
2482 }
2483 if (ok)
6911b7dc
AM
2484 {
2485 bfdsym = symbol_get_bfdsym (sym);
c1229f84 2486 elfsym = elf_symbol_from (bfdsym);
6911b7dc
AM
2487 gas_assert (elfsym);
2488 elfsym->internal_elf_sym.st_other &= ~STO_PPC64_LOCAL_MASK;
2489 elfsym->internal_elf_sym.st_other |= encoded;
2490 if (ppc_abiversion == 0)
2491 ppc_abiversion = 2;
2492 }
2493 }
2494 else
2495 as_bad (_(".localentry expression for `%s' "
2496 "does not evaluate to a constant"), S_GET_NAME (sym));
2497
2498 demand_empty_rest_of_line ();
2499}
2500
ee67d69a
AM
2501/* Pseudo op to set ABI version. */
2502static void
2503ppc_elf_abiversion (int ignore ATTRIBUTE_UNUSED)
2504{
2505 expressionS exp;
2506
2507 expression (&exp);
2508 if (exp.X_op == O_absent)
2509 {
2510 as_bad (_("missing expression in .abiversion directive"));
2511 exp.X_op = O_constant;
2512 exp.X_add_number = 0;
2513 }
2514
2515 if (resolve_expression (&exp)
2516 && exp.X_op == O_constant)
2517 ppc_abiversion = exp.X_add_number;
2518 else
2519 as_bad (_(".abiversion expression does not evaluate to a constant"));
2520 demand_empty_rest_of_line ();
2521}
2522
005d79fd
AM
2523/* Parse a .gnu_attribute directive. */
2524static void
2525ppc_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
2526{
2527 int tag = obj_elf_vendor_attribute (OBJ_ATTR_GNU);
2528
2529 /* Check validity of defined powerpc tags. */
2530 if (tag == Tag_GNU_Power_ABI_FP
2531 || tag == Tag_GNU_Power_ABI_Vector
2532 || tag == Tag_GNU_Power_ABI_Struct_Return)
2533 {
2534 unsigned int val;
2535
2536 val = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU, tag);
2537
2538 if ((tag == Tag_GNU_Power_ABI_FP && val > 15)
2539 || (tag == Tag_GNU_Power_ABI_Vector && val > 3)
2540 || (tag == Tag_GNU_Power_ABI_Struct_Return && val > 2))
2541 as_warn (_("unknown .gnu_attribute value"));
2542 }
2543}
2544
ee67d69a
AM
2545/* Set ABI version in output file. */
2546void
2547ppc_elf_end (void)
2548{
2549 if (ppc_obj64 && ppc_abiversion != 0)
2550 {
2551 elf_elfheader (stdoutput)->e_flags &= ~EF_PPC64_ABI;
2552 elf_elfheader (stdoutput)->e_flags |= ppc_abiversion & EF_PPC64_ABI;
2553 }
22f72c48
AM
2554 /* Any selection of opcodes based on ppc_cpu after gas has finished
2555 parsing the file is invalid. md_apply_fix and ppc_handle_align
2556 must select opcodes based on the machine in force at the point
2557 where the fixup or alignment frag was created, not the machine in
2558 force at the end of file. */
2559 ppc_cpu = 0;
ee67d69a
AM
2560}
2561
252b5132
RH
2562/* Validate any relocations emitted for -mrelocatable, possibly adding
2563 fixups for word relocations in writable segments, so we can adjust
2564 them at runtime. */
2565static void
98027b10 2566ppc_elf_validate_fix (fixS *fixp, segT seg)
252b5132
RH
2567{
2568 if (fixp->fx_done || fixp->fx_pcrel)
2569 return;
2570
2571 switch (shlib)
2572 {
2573 case SHLIB_NONE:
2574 case SHLIB_PIC:
2575 return;
2576
5d6f4f16 2577 case SHLIB_MRELOCATABLE:
62ebcb5c 2578 if (fixp->fx_r_type != BFD_RELOC_16_GOTOFF
252b5132
RH
2579 && fixp->fx_r_type != BFD_RELOC_HI16_GOTOFF
2580 && fixp->fx_r_type != BFD_RELOC_LO16_GOTOFF
2581 && fixp->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
1cfc59d5 2582 && fixp->fx_r_type != BFD_RELOC_16_BASEREL
252b5132
RH
2583 && fixp->fx_r_type != BFD_RELOC_LO16_BASEREL
2584 && fixp->fx_r_type != BFD_RELOC_HI16_BASEREL
2585 && fixp->fx_r_type != BFD_RELOC_HI16_S_BASEREL
e138127a 2586 && (seg->flags & SEC_LOAD) != 0
252b5132
RH
2587 && strcmp (segment_name (seg), ".got2") != 0
2588 && strcmp (segment_name (seg), ".dtors") != 0
2589 && strcmp (segment_name (seg), ".ctors") != 0
2590 && strcmp (segment_name (seg), ".fixup") != 0
252b5132
RH
2591 && strcmp (segment_name (seg), ".gcc_except_table") != 0
2592 && strcmp (segment_name (seg), ".eh_frame") != 0
2593 && strcmp (segment_name (seg), ".ex_shared") != 0)
2594 {
2595 if ((seg->flags & (SEC_READONLY | SEC_CODE)) != 0
2596 || fixp->fx_r_type != BFD_RELOC_CTOR)
2597 {
2598 as_bad_where (fixp->fx_file, fixp->fx_line,
d6ed37ed 2599 _("relocation cannot be done when using -mrelocatable"));
252b5132
RH
2600 }
2601 }
2602 return;
2603 }
2604}
0baf16f2 2605
7e8d4ab4
AM
2606/* Prevent elf_frob_file_before_adjust removing a weak undefined
2607 function descriptor sym if the corresponding code sym is used. */
2608
2609void
98027b10 2610ppc_frob_file_before_adjust (void)
0baf16f2 2611{
7e8d4ab4 2612 symbolS *symp;
9232bbb0 2613 asection *toc;
0baf16f2 2614
7e8d4ab4
AM
2615 if (!ppc_obj64)
2616 return;
2617
2618 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
0baf16f2 2619 {
7e8d4ab4
AM
2620 const char *name;
2621 char *dotname;
2622 symbolS *dotsym;
7e8d4ab4
AM
2623
2624 name = S_GET_NAME (symp);
2625 if (name[0] == '.')
2626 continue;
2627
2628 if (! S_IS_WEAK (symp)
2629 || S_IS_DEFINED (symp))
2630 continue;
2631
a44e2901 2632 dotname = concat (".", name, (char *) NULL);
461b725f 2633 dotsym = symbol_find_noref (dotname, 1);
7e8d4ab4
AM
2634 free (dotname);
2635 if (dotsym != NULL && (symbol_used_p (dotsym)
2636 || symbol_used_in_reloc_p (dotsym)))
670ec21d
NC
2637 symbol_mark_used (symp);
2638
0baf16f2
AM
2639 }
2640
9232bbb0
AM
2641 toc = bfd_get_section_by_name (stdoutput, ".toc");
2642 if (toc != NULL
01efc3af 2643 && toc_reloc_types != has_large_toc_reloc
fd361982 2644 && bfd_section_size (toc) > 0x10000)
9232bbb0 2645 as_warn (_("TOC section size exceeds 64k"));
a38a07e0
AM
2646}
2647
2648/* .TOC. used in an opd entry as .TOC.@tocbase doesn't need to be
2649 emitted. Other uses of .TOC. will cause the symbol to be marked
2650 with BSF_KEEP in md_apply_fix. */
9232bbb0 2651
a38a07e0
AM
2652void
2653ppc_elf_adjust_symtab (void)
2654{
2655 if (ppc_obj64)
2656 {
2657 symbolS *symp;
2658 symp = symbol_find (".TOC.");
2659 if (symp != NULL)
2660 {
2661 asymbol *bsym = symbol_get_bfdsym (symp);
2662 if ((bsym->flags & BSF_KEEP) == 0)
2663 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
a38a07e0
AM
2664 }
2665 }
0baf16f2 2666}
252b5132 2667#endif /* OBJ_ELF */
4a403be0
CC
2668
2669#ifdef OBJ_XCOFF
2670/* Parse XCOFF relocations. */
2671static bfd_reloc_code_real_type
2672ppc_xcoff_suffix (char **str_p)
2673{
2674 struct map_bfd {
2675 const char *string;
2676 unsigned int length : 8;
2677 unsigned int valid32 : 1;
2678 unsigned int valid64 : 1;
2679 unsigned int reloc;
2680 };
2681
2682 char ident[20];
2683 char *str = *str_p;
2684 char *str2;
2685 int ch;
2686 int len;
2687 const struct map_bfd *ptr;
2688
2689#define MAP(str, reloc) { str, sizeof (str) - 1, 1, 1, reloc }
2690#define MAP32(str, reloc) { str, sizeof (str) - 1, 1, 0, reloc }
2691#define MAP64(str, reloc) { str, sizeof (str) - 1, 0, 1, reloc }
2692
2693 static const struct map_bfd mapping[] = {
2694 MAP ("l", BFD_RELOC_PPC_TOC16_LO),
2695 MAP ("u", BFD_RELOC_PPC_TOC16_HI),
1b2cb8e2
CC
2696 MAP32 ("ie", BFD_RELOC_PPC_TLSIE),
2697 MAP32 ("ld", BFD_RELOC_PPC_TLSLD),
2698 MAP32 ("le", BFD_RELOC_PPC_TLSLE),
2699 MAP32 ("m", BFD_RELOC_PPC_TLSM),
2700 MAP32 ("ml", BFD_RELOC_PPC_TLSML),
2701 MAP64 ("ie", BFD_RELOC_PPC64_TLSIE),
2702 MAP64 ("ld", BFD_RELOC_PPC64_TLSLD),
2703 MAP64 ("le", BFD_RELOC_PPC64_TLSLE),
2704 MAP64 ("m", BFD_RELOC_PPC64_TLSM),
2705 MAP64 ("ml", BFD_RELOC_PPC64_TLSML),
4a403be0
CC
2706 };
2707
2708 if (*str++ != '@')
2709 return BFD_RELOC_NONE;
2710
2711 for (ch = *str, str2 = ident;
2712 (str2 < ident + sizeof (ident) - 1
2713 && (ISALNUM (ch) || ch == '@'));
2714 ch = *++str)
2715 {
2716 *str2++ = TOLOWER (ch);
2717 }
2718
2719 *str2 = '\0';
2720 len = str2 - ident;
2721
2722 ch = ident[0];
2723 for (ptr = &mapping[0]; ptr->length > 0; ptr++)
2724 if (ch == ptr->string[0]
2725 && len == ptr->length
2726 && memcmp (ident, ptr->string, ptr->length) == 0
2727 && (ppc_obj64 ? ptr->valid64 : ptr->valid32))
2728 {
2729 *str_p = str;
2730 return (bfd_reloc_code_real_type) ptr->reloc;
2731 }
2732
2733 return BFD_RELOC_NONE;
2734}
2735
2736/* Restore XCOFF addis instruction to ELF format.
2737 AIX often generates addis instructions using "addis RT,D(RA)"
2738 format instead of the ELF "addis RT,RA,SI" one.
2739 On entry RT_E is at the comma after RT, D_E is at the open
2740 parenthesis after D, and RA_E is at the close parenthesis after RA. */
2741static void
2742ppc_xcoff_fixup_addis (char *rt_e, char *d_e, char *ra_e)
2743{
2744 size_t ra_size = ra_e - d_e - 1;
2745 char *save_ra = xmalloc (ra_size);
2746
2747 /* Copy RA. */
2748 memcpy (save_ra, d_e + 1, ra_size);
2749 /* Shuffle D to make room for RA, copying the comma too. */
2750 memmove (rt_e + ra_size + 1, rt_e, d_e - rt_e);
2751 /* Erase the trailing ')', keeping any rubbish for potential errors. */
2752 memmove (ra_e, ra_e + 1, strlen (ra_e));
2753 /* Write RA back. */
2754 memcpy (rt_e + 1, save_ra, ra_size);
2755 free (save_ra);
2756}
2757
1b2cb8e2
CC
2758/* Support @ie, etc. on constants emitted via .short, .int etc. */
2759
2760bfd_reloc_code_real_type
2761ppc_xcoff_parse_cons (expressionS *exp, unsigned int nbytes)
2762{
2763 expression (exp);
2764 if (nbytes >= 2 && *input_line_pointer == '@')
2765 return ppc_xcoff_suffix (&input_line_pointer);
2766
2767 /* There isn't any @ symbol for default TLS relocations (R_TLS). */
2768 if (exp->X_add_symbol != NULL
2769 && (symbol_get_tc (exp->X_add_symbol)->symbol_class == XMC_TL
2770 || symbol_get_tc (exp->X_add_symbol)->symbol_class == XMC_UL))
2771 return (ppc_obj64 ? BFD_RELOC_PPC64_TLSGD: BFD_RELOC_PPC_TLSGD);
2772
2773 return BFD_RELOC_NONE;
2774}
2775
4a403be0 2776#endif /* OBJ_XCOFF */
252b5132 2777\f
3e60bf4d 2778#if defined (OBJ_XCOFF) || defined (OBJ_ELF)
3b8b57a9
AM
2779/* See whether a symbol is in the TOC section. */
2780
2781static int
2782ppc_is_toc_sym (symbolS *sym)
2783{
3e60bf4d 2784#ifdef OBJ_XCOFF
9f6e76f4 2785 return (symbol_get_tc (sym)->symbol_class == XMC_TC
4a403be0 2786 || symbol_get_tc (sym)->symbol_class == XMC_TE
9f6e76f4 2787 || symbol_get_tc (sym)->symbol_class == XMC_TC0);
f50c47f1 2788#endif
3e60bf4d
AM
2789#ifdef OBJ_ELF
2790 const char *sname = segment_name (S_GET_SEGMENT (sym));
2791 if (ppc_obj64)
2792 return strcmp (sname, ".toc") == 0;
2793 else
2794 return strcmp (sname, ".got") == 0;
2795#endif
2796}
2797#endif /* defined (OBJ_XCOFF) || defined (OBJ_ELF) */
252b5132
RH
2798\f
2799
dc1d03fc 2800#ifdef OBJ_ELF
6a0c61b7
EZ
2801#define APUID(a,v) ((((a) & 0xffff) << 16) | ((v) & 0xffff))
2802static void
98027b10 2803ppc_apuinfo_section_add (unsigned int apu, unsigned int version)
6a0c61b7
EZ
2804{
2805 unsigned int i;
2806
2807 /* Check we don't already exist. */
2808 for (i = 0; i < ppc_apuinfo_num; i++)
dc1d03fc 2809 if (ppc_apuinfo_list[i] == APUID (apu, version))
6a0c61b7 2810 return;
b34976b6 2811
6a0c61b7
EZ
2812 if (ppc_apuinfo_num == ppc_apuinfo_num_alloc)
2813 {
2814 if (ppc_apuinfo_num_alloc == 0)
2815 {
2816 ppc_apuinfo_num_alloc = 4;
325801bd 2817 ppc_apuinfo_list = XNEWVEC (unsigned long, ppc_apuinfo_num_alloc);
6a0c61b7
EZ
2818 }
2819 else
2820 {
2821 ppc_apuinfo_num_alloc += 4;
325801bd
TS
2822 ppc_apuinfo_list = XRESIZEVEC (unsigned long, ppc_apuinfo_list,
2823 ppc_apuinfo_num_alloc);
6a0c61b7
EZ
2824 }
2825 }
dc1d03fc 2826 ppc_apuinfo_list[ppc_apuinfo_num++] = APUID (apu, version);
6a0c61b7
EZ
2827}
2828#undef APUID
dc1d03fc 2829#endif
6a0c61b7 2830\f
dd7efa79
PB
2831/* Various frobbings of labels and their addresses. */
2832
2833/* Symbols labelling the current insn. */
2834struct insn_label_list
2835{
2836 struct insn_label_list *next;
2837 symbolS *label;
2838};
2839
2840static struct insn_label_list *insn_labels;
2841static struct insn_label_list *free_insn_labels;
2842
2843static void
2844ppc_record_label (symbolS *sym)
2845{
2846 struct insn_label_list *l;
2847
2848 if (free_insn_labels == NULL)
2849 l = XNEW (struct insn_label_list);
2850 else
2851 {
2852 l = free_insn_labels;
2853 free_insn_labels = l->next;
2854 }
2855
2856 l->label = sym;
2857 l->next = insn_labels;
2858 insn_labels = l;
2859}
2860
2861static void
2862ppc_clear_labels (void)
2863{
2864 while (insn_labels != NULL)
2865 {
2866 struct insn_label_list *l = insn_labels;
2867 insn_labels = l->next;
2868 l->next = free_insn_labels;
2869 free_insn_labels = l;
2870 }
2871}
2872
2873void
2874ppc_start_line_hook (void)
2875{
2876 ppc_clear_labels ();
2877}
2878
2879void
2880ppc_new_dot_label (symbolS *sym)
2881{
2882 ppc_record_label (sym);
2883#ifdef OBJ_XCOFF
2884 /* Anchor this label to the current csect for relocations. */
2885 symbol_get_tc (sym)->within = ppc_current_csect;
2886#endif
2887}
2888
2889void
2890ppc_frob_label (symbolS *sym)
2891{
2892 ppc_record_label (sym);
2893
2894#ifdef OBJ_XCOFF
2895 /* Set the class of a label based on where it is defined. This handles
2896 symbols without suffixes. Also, move the symbol so that it follows
2897 the csect symbol. */
2898 if (ppc_current_csect != (symbolS *) NULL)
2899 {
2900 if (symbol_get_tc (sym)->symbol_class == -1)
2901 symbol_get_tc (sym)->symbol_class = symbol_get_tc (ppc_current_csect)->symbol_class;
2902
2903 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
2904 symbol_append (sym, symbol_get_tc (ppc_current_csect)->within,
2905 &symbol_rootP, &symbol_lastP);
2906 symbol_get_tc (ppc_current_csect)->within = sym;
2907 symbol_get_tc (sym)->within = ppc_current_csect;
2908 }
2909#endif
2910
2911#ifdef OBJ_ELF
2912 dwarf2_emit_label (sym);
2913#endif
2914}
6a0c61b7 2915
252b5132
RH
2916/* We need to keep a list of fixups. We can't simply generate them as
2917 we go, because that would require us to first create the frag, and
2918 that would screw up references to ``.''. */
2919
2920struct ppc_fixup
2921{
2922 expressionS exp;
2923 int opindex;
2924 bfd_reloc_code_real_type reloc;
2925};
2926
2927#define MAX_INSN_FIXUPS (5)
2928
0e2779e9
AM
2929/* Return the field size operated on by RELOC, and whether it is
2930 pc-relative in PC_RELATIVE. */
2931
2932static unsigned int
5b7c81bd 2933fixup_size (bfd_reloc_code_real_type reloc, bool *pc_relative)
0e2779e9
AM
2934{
2935 unsigned int size = 0;
5b7c81bd 2936 bool pcrel = false;
0e2779e9
AM
2937
2938 switch (reloc)
2939 {
2940 /* This switch statement must handle all BFD_RELOC values
2941 possible in instruction fixups. As is, it handles all
2942 BFD_RELOC values used in bfd/elf64-ppc.c, bfd/elf32-ppc.c,
fe49679d 2943 bfd/coff-rs6000.c and bfd/coff64-rs6000.c.
0e2779e9
AM
2944 Overkill since data and marker relocs need not be handled
2945 here, but this way we can be sure a needed fixup reloc isn't
2946 accidentally omitted. */
2947 case BFD_RELOC_PPC_EMB_MRKREF:
2948 case BFD_RELOC_VTABLE_ENTRY:
2949 case BFD_RELOC_VTABLE_INHERIT:
2950 break;
2951
2952 case BFD_RELOC_8:
2953 size = 1;
2954 break;
2955
2956 case BFD_RELOC_16:
2957 case BFD_RELOC_16_BASEREL:
2958 case BFD_RELOC_16_GOTOFF:
2959 case BFD_RELOC_GPREL16:
2960 case BFD_RELOC_HI16:
2961 case BFD_RELOC_HI16_BASEREL:
2962 case BFD_RELOC_HI16_GOTOFF:
2963 case BFD_RELOC_HI16_PLTOFF:
2964 case BFD_RELOC_HI16_S:
2965 case BFD_RELOC_HI16_S_BASEREL:
2966 case BFD_RELOC_HI16_S_GOTOFF:
2967 case BFD_RELOC_HI16_S_PLTOFF:
2968 case BFD_RELOC_LO16:
2969 case BFD_RELOC_LO16_BASEREL:
2970 case BFD_RELOC_LO16_GOTOFF:
2971 case BFD_RELOC_LO16_PLTOFF:
2972 case BFD_RELOC_PPC64_ADDR16_DS:
2973 case BFD_RELOC_PPC64_ADDR16_HIGH:
2974 case BFD_RELOC_PPC64_ADDR16_HIGHA:
5663e321
AM
2975 case BFD_RELOC_PPC64_ADDR16_HIGHER34:
2976 case BFD_RELOC_PPC64_ADDR16_HIGHERA34:
2977 case BFD_RELOC_PPC64_ADDR16_HIGHEST34:
2978 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:
0e2779e9
AM
2979 case BFD_RELOC_PPC64_ADDR16_LO_DS:
2980 case BFD_RELOC_PPC64_DTPREL16_DS:
2981 case BFD_RELOC_PPC64_DTPREL16_HIGH:
2982 case BFD_RELOC_PPC64_DTPREL16_HIGHA:
2983 case BFD_RELOC_PPC64_DTPREL16_HIGHER:
2984 case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
2985 case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
2986 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
2987 case BFD_RELOC_PPC64_DTPREL16_LO_DS:
2988 case BFD_RELOC_PPC64_GOT16_DS:
2989 case BFD_RELOC_PPC64_GOT16_LO_DS:
2990 case BFD_RELOC_PPC64_HIGHER:
2991 case BFD_RELOC_PPC64_HIGHER_S:
2992 case BFD_RELOC_PPC64_HIGHEST:
2993 case BFD_RELOC_PPC64_HIGHEST_S:
2994 case BFD_RELOC_PPC64_PLT16_LO_DS:
2995 case BFD_RELOC_PPC64_PLTGOT16:
2996 case BFD_RELOC_PPC64_PLTGOT16_DS:
2997 case BFD_RELOC_PPC64_PLTGOT16_HA:
2998 case BFD_RELOC_PPC64_PLTGOT16_HI:
2999 case BFD_RELOC_PPC64_PLTGOT16_LO:
3000 case BFD_RELOC_PPC64_PLTGOT16_LO_DS:
3001 case BFD_RELOC_PPC64_SECTOFF_DS:
3002 case BFD_RELOC_PPC64_SECTOFF_LO_DS:
3003 case BFD_RELOC_PPC64_TOC16_DS:
3004 case BFD_RELOC_PPC64_TOC16_HA:
3005 case BFD_RELOC_PPC64_TOC16_HI:
3006 case BFD_RELOC_PPC64_TOC16_LO:
3007 case BFD_RELOC_PPC64_TOC16_LO_DS:
3008 case BFD_RELOC_PPC64_TPREL16_DS:
3009 case BFD_RELOC_PPC64_TPREL16_HIGH:
3010 case BFD_RELOC_PPC64_TPREL16_HIGHA:
3011 case BFD_RELOC_PPC64_TPREL16_HIGHER:
3012 case BFD_RELOC_PPC64_TPREL16_HIGHERA:
3013 case BFD_RELOC_PPC64_TPREL16_HIGHEST:
3014 case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
3015 case BFD_RELOC_PPC64_TPREL16_LO_DS:
3016#ifdef OBJ_XCOFF
3017 case BFD_RELOC_PPC_BA16:
3018#endif
3019 case BFD_RELOC_PPC_DTPREL16:
3020 case BFD_RELOC_PPC_DTPREL16_HA:
3021 case BFD_RELOC_PPC_DTPREL16_HI:
3022 case BFD_RELOC_PPC_DTPREL16_LO:
3023 case BFD_RELOC_PPC_EMB_NADDR16:
3024 case BFD_RELOC_PPC_EMB_NADDR16_HA:
3025 case BFD_RELOC_PPC_EMB_NADDR16_HI:
3026 case BFD_RELOC_PPC_EMB_NADDR16_LO:
3027 case BFD_RELOC_PPC_EMB_RELSDA:
3028 case BFD_RELOC_PPC_EMB_RELSEC16:
3029 case BFD_RELOC_PPC_EMB_RELST_LO:
3030 case BFD_RELOC_PPC_EMB_RELST_HI:
3031 case BFD_RELOC_PPC_EMB_RELST_HA:
3032 case BFD_RELOC_PPC_EMB_SDA2I16:
3033 case BFD_RELOC_PPC_EMB_SDA2REL:
3034 case BFD_RELOC_PPC_EMB_SDAI16:
3035 case BFD_RELOC_PPC_GOT_DTPREL16:
3036 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
3037 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
3038 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
3039 case BFD_RELOC_PPC_GOT_TLSGD16:
3040 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
3041 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
3042 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
3043 case BFD_RELOC_PPC_GOT_TLSLD16:
3044 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
3045 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
3046 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
3047 case BFD_RELOC_PPC_GOT_TPREL16:
3048 case BFD_RELOC_PPC_GOT_TPREL16_HA:
3049 case BFD_RELOC_PPC_GOT_TPREL16_HI:
3050 case BFD_RELOC_PPC_GOT_TPREL16_LO:
3051 case BFD_RELOC_PPC_TOC16:
4a403be0
CC
3052 case BFD_RELOC_PPC_TOC16_HI:
3053 case BFD_RELOC_PPC_TOC16_LO:
0e2779e9
AM
3054 case BFD_RELOC_PPC_TPREL16:
3055 case BFD_RELOC_PPC_TPREL16_HA:
3056 case BFD_RELOC_PPC_TPREL16_HI:
3057 case BFD_RELOC_PPC_TPREL16_LO:
3058 size = 2;
3059 break;
3060
3061 case BFD_RELOC_16_PCREL:
3062 case BFD_RELOC_HI16_PCREL:
3063 case BFD_RELOC_HI16_S_PCREL:
3064 case BFD_RELOC_LO16_PCREL:
3065 case BFD_RELOC_PPC64_REL16_HIGH:
3066 case BFD_RELOC_PPC64_REL16_HIGHA:
3067 case BFD_RELOC_PPC64_REL16_HIGHER:
5663e321 3068 case BFD_RELOC_PPC64_REL16_HIGHER34:
0e2779e9 3069 case BFD_RELOC_PPC64_REL16_HIGHERA:
5663e321 3070 case BFD_RELOC_PPC64_REL16_HIGHERA34:
0e2779e9 3071 case BFD_RELOC_PPC64_REL16_HIGHEST:
5663e321 3072 case BFD_RELOC_PPC64_REL16_HIGHEST34:
0e2779e9 3073 case BFD_RELOC_PPC64_REL16_HIGHESTA:
5663e321 3074 case BFD_RELOC_PPC64_REL16_HIGHESTA34:
0e2779e9
AM
3075#ifdef OBJ_XCOFF
3076 case BFD_RELOC_PPC_B16:
3077#endif
3078 case BFD_RELOC_PPC_VLE_REL8:
3079 size = 2;
5b7c81bd 3080 pcrel = true;
0e2779e9
AM
3081 break;
3082
0e2779e9 3083 case BFD_RELOC_32:
0e2779e9
AM
3084 case BFD_RELOC_32_PLTOFF:
3085#ifdef OBJ_XCOFF
3086 case BFD_RELOC_CTOR:
3087#endif
3088 case BFD_RELOC_PPC64_ENTRY:
3089 case BFD_RELOC_PPC_16DX_HA:
3090#ifndef OBJ_XCOFF
3091 case BFD_RELOC_PPC_BA16:
3092#endif
3093 case BFD_RELOC_PPC_BA16_BRNTAKEN:
3094 case BFD_RELOC_PPC_BA16_BRTAKEN:
3095 case BFD_RELOC_PPC_BA26:
3096 case BFD_RELOC_PPC_EMB_BIT_FLD:
3097 case BFD_RELOC_PPC_EMB_NADDR32:
3098 case BFD_RELOC_PPC_EMB_SDA21:
3099 case BFD_RELOC_PPC_TLS:
3100 case BFD_RELOC_PPC_TLSGD:
3101 case BFD_RELOC_PPC_TLSLD:
1b2cb8e2
CC
3102 case BFD_RELOC_PPC_TLSLE:
3103 case BFD_RELOC_PPC_TLSIE:
3104 case BFD_RELOC_PPC_TLSM:
3105 case BFD_RELOC_PPC_TLSML:
0e2779e9
AM
3106 case BFD_RELOC_PPC_VLE_HA16A:
3107 case BFD_RELOC_PPC_VLE_HA16D:
3108 case BFD_RELOC_PPC_VLE_HI16A:
3109 case BFD_RELOC_PPC_VLE_HI16D:
3110 case BFD_RELOC_PPC_VLE_LO16A:
3111 case BFD_RELOC_PPC_VLE_LO16D:
3112 case BFD_RELOC_PPC_VLE_SDA21:
3113 case BFD_RELOC_PPC_VLE_SDA21_LO:
3114 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
3115 case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
3116 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
3117 case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
3118 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
3119 case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
c213164a 3120 case BFD_RELOC_PPC64_TLS_PCREL:
0e2779e9
AM
3121 case BFD_RELOC_RVA:
3122 size = 4;
3123 break;
3124
3125 case BFD_RELOC_24_PLT_PCREL:
3126 case BFD_RELOC_32_PCREL:
3127 case BFD_RELOC_32_PLT_PCREL:
3128 case BFD_RELOC_PPC64_REL24_NOTOC:
3129#ifndef OBJ_XCOFF
3130 case BFD_RELOC_PPC_B16:
3131#endif
3132 case BFD_RELOC_PPC_B16_BRNTAKEN:
3133 case BFD_RELOC_PPC_B16_BRTAKEN:
3134 case BFD_RELOC_PPC_B26:
3135 case BFD_RELOC_PPC_LOCAL24PC:
3136 case BFD_RELOC_PPC_REL16DX_HA:
3137 case BFD_RELOC_PPC_VLE_REL15:
3138 case BFD_RELOC_PPC_VLE_REL24:
3139 size = 4;
5b7c81bd 3140 pcrel = true;
0e2779e9
AM
3141 break;
3142
3143#ifndef OBJ_XCOFF
3144 case BFD_RELOC_CTOR:
3145#endif
3146 case BFD_RELOC_PPC_COPY:
3147 case BFD_RELOC_PPC_DTPMOD:
3148 case BFD_RELOC_PPC_DTPREL:
3149 case BFD_RELOC_PPC_GLOB_DAT:
3150 case BFD_RELOC_PPC_TPREL:
3151 size = ppc_obj64 ? 8 : 4;
3152 break;
3153
3154 case BFD_RELOC_64:
3155 case BFD_RELOC_64_PLTOFF:
3156 case BFD_RELOC_PPC64_ADDR64_LOCAL:
5663e321
AM
3157 case BFD_RELOC_PPC64_D28:
3158 case BFD_RELOC_PPC64_D34:
3159 case BFD_RELOC_PPC64_D34_LO:
3160 case BFD_RELOC_PPC64_D34_HI30:
3161 case BFD_RELOC_PPC64_D34_HA30:
c213164a
AM
3162 case BFD_RELOC_PPC64_TPREL34:
3163 case BFD_RELOC_PPC64_DTPREL34:
0e2779e9 3164 case BFD_RELOC_PPC64_TOC:
1b2cb8e2
CC
3165 case BFD_RELOC_PPC64_TLSGD:
3166 case BFD_RELOC_PPC64_TLSLD:
3167 case BFD_RELOC_PPC64_TLSLE:
3168 case BFD_RELOC_PPC64_TLSIE:
3169 case BFD_RELOC_PPC64_TLSM:
3170 case BFD_RELOC_PPC64_TLSML:
0e2779e9
AM
3171 size = 8;
3172 break;
3173
3174 case BFD_RELOC_64_PCREL:
3175 case BFD_RELOC_64_PLT_PCREL:
5663e321 3176 case BFD_RELOC_PPC64_GOT_PCREL34:
87c69f97
AM
3177 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:
3178 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:
3179 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:
3180 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:
5663e321
AM
3181 case BFD_RELOC_PPC64_PCREL28:
3182 case BFD_RELOC_PPC64_PCREL34:
3183 case BFD_RELOC_PPC64_PLT_PCREL34:
0e2779e9 3184 size = 8;
5b7c81bd 3185 pcrel = true;
0e2779e9
AM
3186 break;
3187
3188 default:
3189 abort ();
3190 }
3191
3192 if (ENABLE_CHECKING)
3193 {
3194 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
3195 if (reloc_howto != NULL
3196 && (size != bfd_get_reloc_size (reloc_howto)
3197 || pcrel != reloc_howto->pc_relative))
3198 {
3199 as_bad (_("%s howto doesn't match size/pcrel in gas"),
3200 reloc_howto->name);
3201 abort ();
3202 }
3203 }
3204 *pc_relative = pcrel;
3205 return size;
3206}
3207
a894d76a 3208#ifdef OBJ_ELF
160eba93
AM
3209/* If we have parsed a call to __tls_get_addr, parse an argument like
3210 (gd0@tlsgd). *STR is the leading parenthesis on entry. If an arg
3211 is successfully parsed, *STR is updated past the trailing
3212 parenthesis and trailing white space, and *TLS_FIX contains the
3213 reloc and arg expression. */
3214
3215static int
3216parse_tls_arg (char **str, const expressionS *exp, struct ppc_fixup *tls_fix)
3217{
3218 const char *sym_name = S_GET_NAME (exp->X_add_symbol);
3219 if (sym_name[0] == '.')
3220 ++sym_name;
3221
3222 tls_fix->reloc = BFD_RELOC_NONE;
9e7028aa
AM
3223 if (strncasecmp (sym_name, "__tls_get_addr", 14) == 0
3224 && (sym_name[14] == 0
3225 || strcasecmp (sym_name + 14, "_desc") == 0
3226 || strcasecmp (sym_name + 14, "_opt") == 0))
160eba93
AM
3227 {
3228 char *hold = input_line_pointer;
3229 input_line_pointer = *str + 1;
3230 expression (&tls_fix->exp);
3231 if (tls_fix->exp.X_op == O_symbol)
3232 {
3233 if (strncasecmp (input_line_pointer, "@tlsgd)", 7) == 0)
3234 tls_fix->reloc = BFD_RELOC_PPC_TLSGD;
3235 else if (strncasecmp (input_line_pointer, "@tlsld)", 7) == 0)
3236 tls_fix->reloc = BFD_RELOC_PPC_TLSLD;
3237 if (tls_fix->reloc != BFD_RELOC_NONE)
3238 {
3239 input_line_pointer += 7;
3240 SKIP_WHITESPACE ();
3241 *str = input_line_pointer;
3242 }
3243 }
3244 input_line_pointer = hold;
3245 }
3246 return tls_fix->reloc != BFD_RELOC_NONE;
3247}
a894d76a 3248#endif
160eba93 3249
252b5132
RH
3250/* This routine is called for each instruction to be assembled. */
3251
3252void
98027b10 3253md_assemble (char *str)
252b5132
RH
3254{
3255 char *s;
3256 const struct powerpc_opcode *opcode;
0f873fd5 3257 uint64_t insn;
252b5132 3258 const unsigned char *opindex_ptr;
252b5132
RH
3259 int need_paren;
3260 int next_opindex;
3261 struct ppc_fixup fixups[MAX_INSN_FIXUPS];
3262 int fc;
3263 char *f;
a9479dc0 3264 int addr_mask;
252b5132 3265 int i;
b9c361e0 3266 unsigned int insn_length;
252b5132
RH
3267
3268 /* Get the opcode. */
3882b010 3269 for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
252b5132
RH
3270 ;
3271 if (*s != '\0')
3272 *s++ = '\0';
3273
3274 /* Look up the opcode in the hash table. */
629310ab 3275 opcode = (const struct powerpc_opcode *) str_hash_find (ppc_hash, str);
252b5132
RH
3276 if (opcode == (const struct powerpc_opcode *) NULL)
3277 {
3278 const struct powerpc_macro *macro;
3279
fe0e921f
AM
3280 macro = (const struct powerpc_macro *) str_hash_find (ppc_macro_hash,
3281 str);
252b5132 3282 if (macro == (const struct powerpc_macro *) NULL)
d6ed37ed 3283 as_bad (_("unrecognized opcode: `%s'"), str);
252b5132
RH
3284 else
3285 ppc_macro (s, macro);
3286
dd7efa79 3287 ppc_clear_labels ();
252b5132
RH
3288 return;
3289 }
3290
3291 insn = opcode->opcode;
86c0f617
AM
3292 if (!target_big_endian
3293 && ((insn & ~(1 << 26)) == 46u << 26
3294 || (insn & ~(0xc0 << 1)) == (31u << 26 | 533 << 1)))
3295 {
3296 /* lmw, stmw, lswi, lswx, stswi, stswx */
3297 as_bad (_("`%s' invalid when little-endian"), str);
3298 ppc_clear_labels ();
3299 return;
3300 }
252b5132
RH
3301
3302 str = s;
3882b010 3303 while (ISSPACE (*str))
252b5132
RH
3304 ++str;
3305
4a403be0
CC
3306#ifdef OBJ_XCOFF
3307 /* AIX often generates addis instructions using "addis RT, D(RA)"
3308 format instead of the classic "addis RT, RA, SI" one.
3309 Restore it to the default format as it's the one encoded
3310 in ppc opcodes. */
3311 if (!strcmp (opcode->name, "addis"))
3312 {
3313 char *rt_e = strchr (str, ',');
3314 if (rt_e != NULL
3315 && strchr (rt_e + 1, ',') == NULL)
3316 {
3317 char *d_e = strchr (rt_e + 1, '(');
3318 if (d_e != NULL && d_e != rt_e + 1)
3319 {
3320 char *ra_e = strrchr (d_e + 1, ')');
3321 if (ra_e != NULL && ra_e != d_e + 1)
3322 ppc_xcoff_fixup_addis (rt_e, d_e, ra_e);
3323 }
3324 }
3325 }
3326#endif
3327
252b5132 3328 /* PowerPC operands are just expressions. The only real issue is
9cf7e568
AM
3329 that a few operand types are optional. If an instruction has
3330 multiple optional operands and one is omitted, then all optional
3331 operands past the first omitted one must also be omitted. */
3332 int num_optional_operands = 0;
3333 int num_optional_provided = 0;
252b5132
RH
3334
3335 /* Gather the operands. */
3336 need_paren = 0;
3337 next_opindex = 0;
3338 fc = 0;
3339 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
3340 {
3341 const struct powerpc_operand *operand;
3342 const char *errmsg;
3343 char *hold;
3344 expressionS ex;
3345 char endc;
3346
3347 if (next_opindex == 0)
3348 operand = &powerpc_operands[*opindex_ptr];
3349 else
3350 {
3351 operand = &powerpc_operands[next_opindex];
3352 next_opindex = 0;
3353 }
252b5132
RH
3354 errmsg = NULL;
3355
252b5132 3356 /* If this is an optional operand, and we are skipping it, just
9cf7e568 3357 insert the default value, usually a zero. */
252b5132 3358 if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0
9cf7e568 3359 && !((operand->flags & PPC_OPERAND_OPTIONAL32) != 0 && ppc_obj64))
252b5132 3360 {
9cf7e568
AM
3361 if (num_optional_operands == 0)
3362 {
3363 const unsigned char *optr;
3364 int total = 0;
3365 int provided = 0;
3366 int omitted;
3367
3368 s = str;
3369 for (optr = opindex_ptr; *optr != 0; optr++)
3370 {
3371 const struct powerpc_operand *op;
3372 op = &powerpc_operands[*optr];
3373
3374 ++total;
3375
3376 if ((op->flags & PPC_OPERAND_OPTIONAL) != 0
3377 && !((op->flags & PPC_OPERAND_OPTIONAL32) != 0
3378 && ppc_obj64))
3379 ++num_optional_operands;
3380
3381 if (s != NULL && *s != '\0')
3382 {
3383 ++provided;
3384
3385 /* Look for the start of the next operand. */
3386 if ((op->flags & PPC_OPERAND_PARENS) != 0)
3387 s = strpbrk (s, "(,");
3388 else
3389 s = strchr (s, ',');
3390
3391 if (s != NULL)
3392 ++s;
3393 }
3394 }
3395 omitted = total - provided;
3396 num_optional_provided = num_optional_operands - omitted;
3397 }
3398 if (--num_optional_provided < 0)
252b5132 3399 {
9cf7e568
AM
3400 int64_t val = ppc_optional_operand_value (operand, insn, ppc_cpu,
3401 num_optional_provided);
3402 if (operand->insert)
3403 {
3404 insn = (*operand->insert) (insn, val, ppc_cpu, &errmsg);
3405 if (errmsg != (const char *) NULL)
3406 as_bad ("%s", errmsg);
3407 }
3408 else if (operand->shift >= 0)
3409 insn |= (val & operand->bitm) << operand->shift;
3410 else
3411 insn |= (val & operand->bitm) >> -operand->shift;
3412
3413 if ((operand->flags & PPC_OPERAND_NEXT) != 0)
3414 next_opindex = *opindex_ptr + 1;
3415 continue;
252b5132 3416 }
252b5132
RH
3417 }
3418
3419 /* Gather the operand. */
3420 hold = input_line_pointer;
3421 input_line_pointer = str;
3422
fe49679d
AM
3423 if ((reg_names_p
3424 && (((operand->flags & PPC_OPERAND_CR_BIT) != 0)
3425 || ((operand->flags & PPC_OPERAND_CR_REG) != 0)))
3426 || !register_name (&ex))
252b5132 3427 {
fe49679d 3428 char save_lex = lex_type['%'];
252b5132 3429
fe49679d
AM
3430 if (((operand->flags & PPC_OPERAND_CR_REG) != 0)
3431 || (operand->flags & PPC_OPERAND_CR_BIT) != 0)
252b5132 3432 {
5b7c81bd 3433 cr_operand = true;
fe49679d 3434 lex_type['%'] |= LEX_BEGIN_NAME;
252b5132 3435 }
fe49679d 3436 expression (&ex);
5b7c81bd 3437 cr_operand = false;
fe49679d 3438 lex_type['%'] = save_lex;
252b5132
RH
3439 }
3440
3441 str = input_line_pointer;
3442 input_line_pointer = hold;
3443
3444 if (ex.X_op == O_illegal)
3445 as_bad (_("illegal operand"));
3446 else if (ex.X_op == O_absent)
3447 as_bad (_("missing operand"));
3448 else if (ex.X_op == O_register)
3449 {
7e0de605
AM
3450 if ((ex.X_md
3451 & ~operand->flags
3452 & (PPC_OPERAND_GPR | PPC_OPERAND_FPR | PPC_OPERAND_VR
3453 | PPC_OPERAND_VSR | PPC_OPERAND_CR_BIT | PPC_OPERAND_CR_REG
aa3c112f 3454 | PPC_OPERAND_SPR | PPC_OPERAND_GQR | PPC_OPERAND_ACC)) != 0
7e0de605
AM
3455 && !((ex.X_md & PPC_OPERAND_GPR) != 0
3456 && ex.X_add_number != 0
3457 && (operand->flags & PPC_OPERAND_GPR_0) != 0))
ece5dcc1 3458 as_warn (_("invalid register expression"));
4b1c0f7e 3459 insn = ppc_insert_operand (insn, operand, ex.X_add_number,
783de163 3460 ppc_cpu, (char *) NULL, 0);
252b5132
RH
3461 }
3462 else if (ex.X_op == O_constant)
3463 {
3464#ifdef OBJ_ELF
81d4177b 3465 /* Allow @HA, @L, @H on constants. */
3b8b57a9 3466 bfd_reloc_code_real_type reloc;
252b5132
RH
3467 char *orig_str = str;
3468
62ebcb5c 3469 if ((reloc = ppc_elf_suffix (&str, &ex)) != BFD_RELOC_NONE)
252b5132
RH
3470 switch (reloc)
3471 {
3472 default:
3473 str = orig_str;
3474 break;
3475
3476 case BFD_RELOC_LO16:
f9c6b907
AM
3477 ex.X_add_number &= 0xffff;
3478 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
0baf16f2 3479 ex.X_add_number = SEX16 (ex.X_add_number);
252b5132
RH
3480 break;
3481
3482 case BFD_RELOC_HI16:
f9c6b907
AM
3483 if (REPORT_OVERFLOW_HI && ppc_obj64)
3484 {
3485 /* PowerPC64 @h is tested for overflow. */
3486 ex.X_add_number = (addressT) ex.X_add_number >> 16;
3487 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3488 {
3489 addressT sign = (((addressT) -1 >> 16) + 1) >> 1;
3490 ex.X_add_number
3491 = ((addressT) ex.X_add_number ^ sign) - sign;
3492 }
3493 break;
3494 }
2b0f3761 3495 /* Fallthru */
f9c6b907
AM
3496
3497 case BFD_RELOC_PPC64_ADDR16_HIGH:
3498 ex.X_add_number = PPC_HI (ex.X_add_number);
3499 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3500 ex.X_add_number = SEX16 (ex.X_add_number);
252b5132
RH
3501 break;
3502
3503 case BFD_RELOC_HI16_S:
f9c6b907
AM
3504 if (REPORT_OVERFLOW_HI && ppc_obj64)
3505 {
3506 /* PowerPC64 @ha is tested for overflow. */
3507 ex.X_add_number
3508 = ((addressT) ex.X_add_number + 0x8000) >> 16;
3509 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3510 {
3511 addressT sign = (((addressT) -1 >> 16) + 1) >> 1;
3512 ex.X_add_number
3513 = ((addressT) ex.X_add_number ^ sign) - sign;
3514 }
3515 break;
3516 }
2b0f3761 3517 /* Fallthru */
f9c6b907
AM
3518
3519 case BFD_RELOC_PPC64_ADDR16_HIGHA:
3520 ex.X_add_number = PPC_HA (ex.X_add_number);
3521 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3522 ex.X_add_number = SEX16 (ex.X_add_number);
0baf16f2
AM
3523 break;
3524
0baf16f2 3525 case BFD_RELOC_PPC64_HIGHER:
f9c6b907
AM
3526 ex.X_add_number = PPC_HIGHER (ex.X_add_number);
3527 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3528 ex.X_add_number = SEX16 (ex.X_add_number);
0baf16f2
AM
3529 break;
3530
3531 case BFD_RELOC_PPC64_HIGHER_S:
f9c6b907
AM
3532 ex.X_add_number = PPC_HIGHERA (ex.X_add_number);
3533 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3534 ex.X_add_number = SEX16 (ex.X_add_number);
252b5132 3535 break;
0baf16f2
AM
3536
3537 case BFD_RELOC_PPC64_HIGHEST:
f9c6b907
AM
3538 ex.X_add_number = PPC_HIGHEST (ex.X_add_number);
3539 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3540 ex.X_add_number = SEX16 (ex.X_add_number);
0baf16f2
AM
3541 break;
3542
3543 case BFD_RELOC_PPC64_HIGHEST_S:
f9c6b907
AM
3544 ex.X_add_number = PPC_HIGHESTA (ex.X_add_number);
3545 if ((operand->flags & PPC_OPERAND_SIGNED) != 0)
3546 ex.X_add_number = SEX16 (ex.X_add_number);
0baf16f2 3547 break;
252b5132 3548 }
0baf16f2 3549#endif /* OBJ_ELF */
252b5132 3550 insn = ppc_insert_operand (insn, operand, ex.X_add_number,
783de163 3551 ppc_cpu, (char *) NULL, 0);
252b5132 3552 }
727fc41e 3553 else
252b5132 3554 {
62ebcb5c 3555 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
3b8b57a9 3556#ifdef OBJ_ELF
160eba93
AM
3557 /* Look for a __tls_get_addr arg using the insane old syntax. */
3558 if (ex.X_op == O_symbol && *str == '(' && fc < MAX_INSN_FIXUPS
3559 && parse_tls_arg (&str, &ex, &fixups[fc]))
cdba85ec 3560 {
160eba93
AM
3561 fixups[fc].opindex = *opindex_ptr;
3562 ++fc;
252b5132
RH
3563 }
3564
62ebcb5c 3565 if ((reloc = ppc_elf_suffix (&str, &ex)) != BFD_RELOC_NONE)
0baf16f2 3566 {
b9c361e0
JL
3567 /* If VLE-mode convert LO/HI/HA relocations. */
3568 if (opcode->flags & PPC_OPCODE_VLE)
3569 {
0f873fd5 3570 uint64_t tmp_insn = insn & opcode->mask;
3739860c 3571
08dc996f 3572 int use_a_reloc = (tmp_insn == E_OR2I_INSN
b9c361e0
JL
3573 || tmp_insn == E_AND2I_DOT_INSN
3574 || tmp_insn == E_OR2IS_INSN
bb6bf75e 3575 || tmp_insn == E_LI_INSN
b9c361e0
JL
3576 || tmp_insn == E_LIS_INSN
3577 || tmp_insn == E_AND2IS_DOT_INSN);
3578
3579
08dc996f 3580 int use_d_reloc = (tmp_insn == E_ADD2I_DOT_INSN
b9c361e0
JL
3581 || tmp_insn == E_ADD2IS_INSN
3582 || tmp_insn == E_CMP16I_INSN
3583 || tmp_insn == E_MULL2I_INSN
3584 || tmp_insn == E_CMPL16I_INSN
3585 || tmp_insn == E_CMPH16I_INSN
3586 || tmp_insn == E_CMPHL16I_INSN);
3587
3588 switch (reloc)
3589 {
3590 default:
3591 break;
3592
3593 case BFD_RELOC_PPC_EMB_SDA21:
3594 reloc = BFD_RELOC_PPC_VLE_SDA21;
3595 break;
3596
3597 case BFD_RELOC_LO16:
3598 if (use_d_reloc)
3599 reloc = BFD_RELOC_PPC_VLE_LO16D;
3600 else if (use_a_reloc)
3601 reloc = BFD_RELOC_PPC_VLE_LO16A;
3602 break;
3603
3604 case BFD_RELOC_HI16:
3605 if (use_d_reloc)
3606 reloc = BFD_RELOC_PPC_VLE_HI16D;
3607 else if (use_a_reloc)
3608 reloc = BFD_RELOC_PPC_VLE_HI16A;
3609 break;
3739860c 3610
b9c361e0
JL
3611 case BFD_RELOC_HI16_S:
3612 if (use_d_reloc)
3613 reloc = BFD_RELOC_PPC_VLE_HA16D;
3614 else if (use_a_reloc)
3615 reloc = BFD_RELOC_PPC_VLE_HA16A;
3616 break;
3617
3618 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
3619 if (use_d_reloc)
3620 reloc = BFD_RELOC_PPC_VLE_SDAREL_LO16D;
3621 break;
3622
3623 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
3624 if (use_d_reloc)
3625 reloc = BFD_RELOC_PPC_VLE_SDAREL_HI16D;
3626 break;
3627
3628 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
3629 if (use_d_reloc)
3630 reloc = BFD_RELOC_PPC_VLE_SDAREL_HA16D;
3631 break;
3632 }
3633 }
bb6bf75e
AM
3634
3635 /* TLS and other tweaks. */
3636 switch (reloc)
3637 {
3638 default:
3639 break;
3640
3641 case BFD_RELOC_PPC_TLS:
c213164a 3642 case BFD_RELOC_PPC64_TLS_PCREL:
bb6bf75e
AM
3643 if (!_bfd_elf_ppc_at_tls_transform (opcode->opcode, 0))
3644 as_bad (_("@tls may not be used with \"%s\" operands"),
3645 opcode->name);
3646 else if (operand->shift != 11)
3647 as_bad (_("@tls may only be used in last operand"));
3648 else
3649 insn = ppc_insert_operand (insn, operand,
3650 ppc_obj64 ? 13 : 2,
3651 ppc_cpu, (char *) NULL, 0);
3652 break;
3653
3654 /* We'll only use the 32 (or 64) bit form of these relocations
c213164a 3655 in constants. Instructions get the 16 or 34 bit form. */
bb6bf75e 3656 case BFD_RELOC_PPC_DTPREL:
c213164a
AM
3657 if (operand->bitm == 0x3ffffffffULL)
3658 reloc = BFD_RELOC_PPC64_DTPREL34;
3659 else
3660 reloc = BFD_RELOC_PPC_DTPREL16;
bb6bf75e
AM
3661 break;
3662
3663 case BFD_RELOC_PPC_TPREL:
c213164a
AM
3664 if (operand->bitm == 0x3ffffffffULL)
3665 reloc = BFD_RELOC_PPC64_TPREL34;
3666 else
3667 reloc = BFD_RELOC_PPC_TPREL16;
bb6bf75e
AM
3668 break;
3669
5663e321
AM
3670 case BFD_RELOC_PPC64_PCREL34:
3671 if (operand->bitm == 0xfffffffULL)
3672 {
3673 reloc = BFD_RELOC_PPC64_PCREL28;
3674 break;
3675 }
3676 /* Fall through. */
3677 case BFD_RELOC_PPC64_GOT_PCREL34:
3678 case BFD_RELOC_PPC64_PLT_PCREL34:
87c69f97
AM
3679 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:
3680 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:
3681 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:
3682 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:
5663e321 3683 if (operand->bitm != 0x3ffffffffULL
bb6bf75e 3684 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
5663e321
AM
3685 as_warn (_("%s unsupported on this instruction"), "@pcrel");
3686 break;
3687
3688 case BFD_RELOC_LO16:
3689 if (operand->bitm == 0x3ffffffffULL
3690 && (operand->flags & PPC_OPERAND_NEGATIVE) == 0)
3691 reloc = BFD_RELOC_PPC64_D34_LO;
3692 else if ((operand->bitm | 0xf) != 0xffff
3693 || operand->shift != 0
3694 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
bb6bf75e
AM
3695 as_warn (_("%s unsupported on this instruction"), "@l");
3696 break;
3697
3698 case BFD_RELOC_HI16:
5663e321
AM
3699 if (operand->bitm == 0x3ffffffffULL
3700 && (operand->flags & PPC_OPERAND_NEGATIVE) == 0)
3701 reloc = BFD_RELOC_PPC64_D34_HI30;
3702 else if (operand->bitm != 0xffff
3703 || operand->shift != 0
3704 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
bb6bf75e
AM
3705 as_warn (_("%s unsupported on this instruction"), "@h");
3706 break;
3707
3708 case BFD_RELOC_HI16_S:
5663e321
AM
3709 if (operand->bitm == 0x3ffffffffULL
3710 && (operand->flags & PPC_OPERAND_NEGATIVE) == 0)
3711 reloc = BFD_RELOC_PPC64_D34_HA30;
3712 else if (operand->bitm == 0xffff
3713 && operand->shift == (int) PPC_OPSHIFT_INV
3714 && opcode->opcode == (19 << 26) + (2 << 1))
bb6bf75e
AM
3715 /* addpcis. */
3716 reloc = BFD_RELOC_PPC_16DX_HA;
3717 else if (operand->bitm != 0xffff
3718 || operand->shift != 0
3719 || (operand->flags & PPC_OPERAND_NEGATIVE) != 0)
3720 as_warn (_("%s unsupported on this instruction"), "@ha");
3721 }
0baf16f2 3722 }
3b8b57a9 3723#endif /* OBJ_ELF */
4a403be0
CC
3724#ifdef OBJ_XCOFF
3725 reloc = ppc_xcoff_suffix (&str);
3726#endif /* OBJ_XCOFF */
3b8b57a9 3727
62ebcb5c 3728 if (reloc != BFD_RELOC_NONE)
3b8b57a9
AM
3729 ;
3730 /* Determine a BFD reloc value based on the operand information.
3731 We are only prepared to turn a few of the operands into
3732 relocs. */
a0593ad9
AM
3733 else if ((operand->flags & (PPC_OPERAND_RELATIVE
3734 | PPC_OPERAND_ABSOLUTE)) != 0
3b8b57a9
AM
3735 && operand->bitm == 0x3fffffc
3736 && operand->shift == 0)
3737 reloc = BFD_RELOC_PPC_B26;
a0593ad9
AM
3738 else if ((operand->flags & (PPC_OPERAND_RELATIVE
3739 | PPC_OPERAND_ABSOLUTE)) != 0
3b8b57a9
AM
3740 && operand->bitm == 0xfffc
3741 && operand->shift == 0)
3742 reloc = BFD_RELOC_PPC_B16;
3743 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3744 && operand->bitm == 0x1fe
3745 && operand->shift == -1)
3746 reloc = BFD_RELOC_PPC_VLE_REL8;
3747 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3748 && operand->bitm == 0xfffe
3749 && operand->shift == 0)
3750 reloc = BFD_RELOC_PPC_VLE_REL15;
3751 else if ((operand->flags & PPC_OPERAND_RELATIVE) != 0
3752 && operand->bitm == 0x1fffffe
3753 && operand->shift == 0)
3754 reloc = BFD_RELOC_PPC_VLE_REL24;
a0593ad9 3755 else if ((operand->flags & PPC_OPERAND_NEGATIVE) == 0
3b8b57a9
AM
3756 && (operand->bitm & 0xfff0) == 0xfff0
3757 && operand->shift == 0)
3758 {
f50c47f1 3759 reloc = BFD_RELOC_16;
3e60bf4d 3760#if defined OBJ_XCOFF || defined OBJ_ELF
f50c47f1 3761 /* Note: the symbol may be not yet defined. */
a0593ad9
AM
3762 if ((operand->flags & PPC_OPERAND_PARENS) != 0
3763 && ppc_is_toc_sym (ex.X_add_symbol))
3e60bf4d
AM
3764 {
3765 reloc = BFD_RELOC_PPC_TOC16;
3766#ifdef OBJ_ELF
3767 as_warn (_("assuming %s on symbol"),
3768 ppc_obj64 ? "@toc" : "@xgot");
3769#endif
3770 }
3b8b57a9 3771#endif
3b8b57a9 3772 }
5663e321
AM
3773 else if (operand->bitm == 0x3ffffffffULL)
3774 reloc = BFD_RELOC_PPC64_D34;
3775 else if (operand->bitm == 0xfffffffULL)
3776 reloc = BFD_RELOC_PPC64_D28;
a0593ad9
AM
3777
3778 /* For the absolute forms of branches, convert the PC
3779 relative form back into the absolute. */
3780 if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0)
3781 {
3782 switch (reloc)
3783 {
3784 case BFD_RELOC_PPC_B26:
3785 reloc = BFD_RELOC_PPC_BA26;
3786 break;
3787 case BFD_RELOC_PPC_B16:
3788 reloc = BFD_RELOC_PPC_BA16;
3789 break;
3790#ifdef OBJ_ELF
3791 case BFD_RELOC_PPC_B16_BRTAKEN:
3792 reloc = BFD_RELOC_PPC_BA16_BRTAKEN;
3793 break;
3794 case BFD_RELOC_PPC_B16_BRNTAKEN:
3795 reloc = BFD_RELOC_PPC_BA16_BRNTAKEN;
3796 break;
3797#endif
3798 default:
3799 break;
3800 }
3801 }
3802
3803#ifdef OBJ_ELF
3804 switch (reloc)
3805 {
3806 case BFD_RELOC_PPC_TOC16:
3807 toc_reloc_types |= has_small_toc_reloc;
3808 break;
3809 case BFD_RELOC_PPC64_TOC16_LO:
3810 case BFD_RELOC_PPC64_TOC16_HI:
3811 case BFD_RELOC_PPC64_TOC16_HA:
3812 toc_reloc_types |= has_large_toc_reloc;
3813 break;
3814 default:
3815 break;
3816 }
3817
3818 if (ppc_obj64
3819 && (operand->flags & (PPC_OPERAND_DS | PPC_OPERAND_DQ)) != 0)
3820 {
3821 switch (reloc)
3822 {
3823 case BFD_RELOC_16:
3824 reloc = BFD_RELOC_PPC64_ADDR16_DS;
3825 break;
5663e321 3826
a0593ad9
AM
3827 case BFD_RELOC_LO16:
3828 reloc = BFD_RELOC_PPC64_ADDR16_LO_DS;
3829 break;
5663e321 3830
a0593ad9
AM
3831 case BFD_RELOC_16_GOTOFF:
3832 reloc = BFD_RELOC_PPC64_GOT16_DS;
3833 break;
5663e321 3834
a0593ad9
AM
3835 case BFD_RELOC_LO16_GOTOFF:
3836 reloc = BFD_RELOC_PPC64_GOT16_LO_DS;
3837 break;
5663e321 3838
a0593ad9
AM
3839 case BFD_RELOC_LO16_PLTOFF:
3840 reloc = BFD_RELOC_PPC64_PLT16_LO_DS;
3841 break;
5663e321 3842
a0593ad9
AM
3843 case BFD_RELOC_16_BASEREL:
3844 reloc = BFD_RELOC_PPC64_SECTOFF_DS;
3845 break;
5663e321 3846
a0593ad9
AM
3847 case BFD_RELOC_LO16_BASEREL:
3848 reloc = BFD_RELOC_PPC64_SECTOFF_LO_DS;
3849 break;
5663e321 3850
a0593ad9
AM
3851 case BFD_RELOC_PPC_TOC16:
3852 reloc = BFD_RELOC_PPC64_TOC16_DS;
3853 break;
5663e321 3854
a0593ad9
AM
3855 case BFD_RELOC_PPC64_TOC16_LO:
3856 reloc = BFD_RELOC_PPC64_TOC16_LO_DS;
3857 break;
5663e321 3858
a0593ad9
AM
3859 case BFD_RELOC_PPC64_PLTGOT16:
3860 reloc = BFD_RELOC_PPC64_PLTGOT16_DS;
3861 break;
5663e321 3862
a0593ad9
AM
3863 case BFD_RELOC_PPC64_PLTGOT16_LO:
3864 reloc = BFD_RELOC_PPC64_PLTGOT16_LO_DS;
3865 break;
5663e321 3866
a0593ad9
AM
3867 case BFD_RELOC_PPC_DTPREL16:
3868 reloc = BFD_RELOC_PPC64_DTPREL16_DS;
3869 break;
5663e321 3870
a0593ad9
AM
3871 case BFD_RELOC_PPC_DTPREL16_LO:
3872 reloc = BFD_RELOC_PPC64_DTPREL16_LO_DS;
3873 break;
5663e321 3874
a0593ad9
AM
3875 case BFD_RELOC_PPC_TPREL16:
3876 reloc = BFD_RELOC_PPC64_TPREL16_DS;
3877 break;
5663e321 3878
a0593ad9
AM
3879 case BFD_RELOC_PPC_TPREL16_LO:
3880 reloc = BFD_RELOC_PPC64_TPREL16_LO_DS;
3881 break;
5663e321 3882
a0593ad9
AM
3883 case BFD_RELOC_PPC_GOT_DTPREL16:
3884 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
3885 case BFD_RELOC_PPC_GOT_TPREL16:
3886 case BFD_RELOC_PPC_GOT_TPREL16_LO:
3887 break;
5663e321 3888
a0593ad9
AM
3889 default:
3890 as_bad (_("unsupported relocation for DS offset field"));
3891 break;
3892 }
3893 }
160eba93
AM
3894
3895 /* Look for a __tls_get_addr arg after any __tls_get_addr
3896 modifiers like @plt. This fixup must be emitted before
3897 the usual call fixup. */
3898 if (ex.X_op == O_symbol && *str == '(' && fc < MAX_INSN_FIXUPS
3899 && parse_tls_arg (&str, &ex, &fixups[fc]))
3900 {
3901 fixups[fc].opindex = *opindex_ptr;
3902 ++fc;
3903 }
a0593ad9 3904#endif
0baf16f2 3905
252b5132
RH
3906 /* We need to generate a fixup for this expression. */
3907 if (fc >= MAX_INSN_FIXUPS)
3908 as_fatal (_("too many fixups"));
3909 fixups[fc].exp = ex;
727fc41e 3910 fixups[fc].opindex = *opindex_ptr;
252b5132
RH
3911 fixups[fc].reloc = reloc;
3912 ++fc;
3913 }
252b5132
RH
3914
3915 if (need_paren)
3916 {
3917 endc = ')';
3918 need_paren = 0;
c3d65c1c
BE
3919 /* If expecting more operands, then we want to see "),". */
3920 if (*str == endc && opindex_ptr[1] != 0)
3921 {
3922 do
3923 ++str;
3924 while (ISSPACE (*str));
3925 endc = ',';
3926 }
252b5132
RH
3927 }
3928 else if ((operand->flags & PPC_OPERAND_PARENS) != 0)
9cf7e568 3929 endc = '(';
252b5132
RH
3930 else
3931 endc = ',';
3932
3933 /* The call to expression should have advanced str past any
3934 whitespace. */
9cf7e568 3935 if (*str == endc)
252b5132 3936 {
9cf7e568
AM
3937 ++str;
3938 if (endc == '(')
3939 need_paren = 1;
3940 }
3941 else if (*str != '\0')
3942 {
3943 as_bad (_("syntax error; found `%c', expected `%c'"), *str, endc);
3944 break;
3945 }
3946 else if (endc == ')')
3947 {
3948 as_bad (_("syntax error; end of line, expected `%c'"), endc);
252b5132
RH
3949 break;
3950 }
252b5132
RH
3951 }
3952
3882b010 3953 while (ISSPACE (*str))
252b5132
RH
3954 ++str;
3955
3956 if (*str != '\0')
3957 as_bad (_("junk at end of line: `%s'"), str);
3958
dc1d03fc 3959#ifdef OBJ_ELF
b9c361e0 3960 /* Do we need/want an APUinfo section? */
4faf939a
JM
3961 if ((ppc_cpu & (PPC_OPCODE_E500 | PPC_OPCODE_E500MC | PPC_OPCODE_VLE)) != 0
3962 && !ppc_obj64)
6a0c61b7
EZ
3963 {
3964 /* These are all version "1". */
3965 if (opcode->flags & PPC_OPCODE_SPE)
b34976b6 3966 ppc_apuinfo_section_add (PPC_APUINFO_SPE, 1);
6a0c61b7 3967 if (opcode->flags & PPC_OPCODE_ISEL)
b34976b6 3968 ppc_apuinfo_section_add (PPC_APUINFO_ISEL, 1);
6a0c61b7 3969 if (opcode->flags & PPC_OPCODE_EFS)
b34976b6 3970 ppc_apuinfo_section_add (PPC_APUINFO_EFS, 1);
6a0c61b7 3971 if (opcode->flags & PPC_OPCODE_BRLOCK)
b34976b6 3972 ppc_apuinfo_section_add (PPC_APUINFO_BRLOCK, 1);
6a0c61b7 3973 if (opcode->flags & PPC_OPCODE_PMR)
b34976b6 3974 ppc_apuinfo_section_add (PPC_APUINFO_PMR, 1);
6a0c61b7 3975 if (opcode->flags & PPC_OPCODE_CACHELCK)
b34976b6 3976 ppc_apuinfo_section_add (PPC_APUINFO_CACHELCK, 1);
6a0c61b7 3977 if (opcode->flags & PPC_OPCODE_RFMCI)
b34976b6 3978 ppc_apuinfo_section_add (PPC_APUINFO_RFMCI, 1);
fbd94057
MR
3979 /* Only set the VLE flag if the instruction has been pulled via
3980 the VLE instruction set. This way the flag is guaranteed to
3981 be set for VLE-only instructions or for VLE-only processors,
3982 however it'll remain clear for dual-mode instructions on
3983 dual-mode and, more importantly, standard-mode processors. */
3984 if ((ppc_cpu & opcode->flags) == PPC_OPCODE_VLE)
f7d69005
AM
3985 {
3986 ppc_apuinfo_section_add (PPC_APUINFO_VLE, 1);
3987 if (elf_section_data (now_seg) != NULL)
3988 elf_section_data (now_seg)->this_hdr.sh_flags |= SHF_PPC_VLE;
3989 }
6a0c61b7 3990 }
dc1d03fc 3991#endif
6a0c61b7 3992
252b5132 3993 /* Write out the instruction. */
a9479dc0
AM
3994
3995 addr_mask = 3;
f7d69005 3996 if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
a9479dc0
AM
3997 /* All instructions can start on a 2 byte boundary for VLE. */
3998 addr_mask = 1;
3999
4000 if (frag_now->insn_addr != addr_mask)
b9c361e0 4001 {
a9479dc0
AM
4002 /* Don't emit instructions to a frag started for data, or for a
4003 CPU differing in VLE mode. Data is allowed to be misaligned,
4004 and it's possible to start a new frag in the middle of
4005 misaligned data. */
4006 frag_wane (frag_now);
4007 frag_new (0);
b9c361e0 4008 }
a9479dc0
AM
4009
4010 /* Check that insns within the frag are aligned. ppc_frag_check
4011 will ensure that the frag start address is aligned. */
4012 if ((frag_now_fix () & addr_mask) != 0)
4013 as_bad (_("instruction address is not a multiple of %d"), addr_mask + 1);
4014
dd7efa79 4015 /* Differentiate between two, four, and eight byte insns. */
a9479dc0
AM
4016 insn_length = 4;
4017 if ((ppc_cpu & PPC_OPCODE_VLE) != 0 && PPC_OP_SE_VLE (insn))
4018 insn_length = 2;
7c1f4227 4019 else if ((opcode->flags & PPC_OPCODE_POWER10) != 0
dd7efa79
PB
4020 && PPC_PREFIX_P (insn))
4021 {
4022 struct insn_label_list *l;
4023
4024 insn_length = 8;
4025
4026 /* 8-byte prefix instructions are not allowed to cross 64-byte
4027 boundaries. */
4028 frag_align_code (6, 4);
4029 record_alignment (now_seg, 6);
4030
4031 /* Update "dot" in any expressions used by this instruction, and
4032 a label attached to the instruction. By "attached" we mean
4033 on the same source line as the instruction and without any
4034 intervening semicolons. */
4035 dot_value = frag_now_fix ();
4036 dot_frag = frag_now;
4037 for (l = insn_labels; l != NULL; l = l->next)
4038 {
4039 symbol_set_frag (l->label, dot_frag);
4040 S_SET_VALUE (l->label, dot_value);
4041 }
4042 }
4043
4044 ppc_clear_labels ();
a9479dc0 4045
b9c361e0 4046 f = frag_more (insn_length);
a9479dc0 4047 frag_now->insn_addr = addr_mask;
dd7efa79
PB
4048
4049 /* The prefix part of an 8-byte instruction always occupies the lower
4050 addressed word in a doubleword, regardless of endianness. */
15a32af5
AM
4051 if (insn_length == 8
4052 && (sizeof (insn) > sizeof (valueT) || !target_big_endian))
dd7efa79
PB
4053 {
4054 md_number_to_chars (f, PPC_GET_PREFIX (insn), 4);
4055 md_number_to_chars (f + 4, PPC_GET_SUFFIX (insn), 4);
4056 }
4057 else
4058 md_number_to_chars (f, insn, insn_length);
4059
bf7279d5
AM
4060 last_insn = insn;
4061 last_seg = now_seg;
4062 last_subseg = now_subseg;
252b5132 4063
5d6f4f16 4064#ifdef OBJ_ELF
b9c361e0 4065 dwarf2_emit_insn (insn_length);
5d6f4f16
GK
4066#endif
4067
3b8b57a9 4068 /* Create any fixups. */
252b5132
RH
4069 for (i = 0; i < fc; i++)
4070 {
3b8b57a9 4071 fixS *fixP;
62ebcb5c 4072 if (fixups[i].reloc != BFD_RELOC_NONE)
252b5132 4073 {
5b7c81bd 4074 bool pcrel;
0e2779e9
AM
4075 unsigned int size = fixup_size (fixups[i].reloc, &pcrel);
4076 int offset = target_big_endian ? (insn_length - size) : 0;
252b5132 4077
99a814a1
AM
4078 fixP = fix_new_exp (frag_now,
4079 f - frag_now->fr_literal + offset,
4080 size,
4081 &fixups[i].exp,
0e2779e9 4082 pcrel,
252b5132 4083 fixups[i].reloc);
252b5132
RH
4084 }
4085 else
727fc41e
AM
4086 {
4087 const struct powerpc_operand *operand;
4088
4089 operand = &powerpc_operands[fixups[i].opindex];
3b8b57a9
AM
4090 fixP = fix_new_exp (frag_now,
4091 f - frag_now->fr_literal,
4092 insn_length,
4093 &fixups[i].exp,
4094 (operand->flags & PPC_OPERAND_RELATIVE) != 0,
62ebcb5c 4095 BFD_RELOC_NONE);
727fc41e 4096 }
3b8b57a9 4097 fixP->fx_pcrel_adjust = fixups[i].opindex;
252b5132
RH
4098 }
4099}
4100
4101/* Handle a macro. Gather all the operands, transform them as
4102 described by the macro, and call md_assemble recursively. All the
4103 operands are separated by commas; we don't accept parentheses
4104 around operands here. */
4105
4106static void
98027b10 4107ppc_macro (char *str, const struct powerpc_macro *macro)
252b5132
RH
4108{
4109 char *operands[10];
4110 unsigned int count;
4111 char *s;
4112 unsigned int len;
4113 const char *format;
db557034 4114 unsigned int arg;
252b5132
RH
4115 char *send;
4116 char *complete;
4117
4118 /* Gather the users operands into the operands array. */
4119 count = 0;
4120 s = str;
4121 while (1)
4122 {
4123 if (count >= sizeof operands / sizeof operands[0])
4124 break;
4125 operands[count++] = s;
4126 s = strchr (s, ',');
4127 if (s == (char *) NULL)
4128 break;
4129 *s++ = '\0';
81d4177b 4130 }
252b5132
RH
4131
4132 if (count != macro->operands)
4133 {
4134 as_bad (_("wrong number of operands"));
4135 return;
4136 }
4137
4138 /* Work out how large the string must be (the size is unbounded
4139 because it includes user input). */
4140 len = 0;
4141 format = macro->format;
4142 while (*format != '\0')
4143 {
4144 if (*format != '%')
4145 {
4146 ++len;
4147 ++format;
4148 }
4149 else
4150 {
4151 arg = strtol (format + 1, &send, 10);
db557034 4152 know (send != format && arg < count);
252b5132
RH
4153 len += strlen (operands[arg]);
4154 format = send;
4155 }
4156 }
4157
4158 /* Put the string together. */
325801bd 4159 complete = s = XNEWVEC (char, len + 1);
252b5132
RH
4160 format = macro->format;
4161 while (*format != '\0')
4162 {
4163 if (*format != '%')
4164 *s++ = *format++;
4165 else
4166 {
4167 arg = strtol (format + 1, &send, 10);
4168 strcpy (s, operands[arg]);
4169 s += strlen (s);
4170 format = send;
4171 }
4172 }
4173 *s = '\0';
4174
4175 /* Assemble the constructed instruction. */
4176 md_assemble (complete);
e1fa0163 4177 free (complete);
81d4177b 4178}
252b5132
RH
4179\f
4180#ifdef OBJ_ELF
18ae9cc1 4181/* For ELF, add support for SHT_ORDERED. */
252b5132
RH
4182
4183int
98027b10 4184ppc_section_type (char *str, size_t len)
252b5132 4185{
9de8d8f1
RH
4186 if (len == 7 && strncmp (str, "ordered", 7) == 0)
4187 return SHT_ORDERED;
252b5132 4188
9de8d8f1 4189 return -1;
252b5132
RH
4190}
4191
4192int
1239de13 4193ppc_section_flags (flagword flags, bfd_vma attr ATTRIBUTE_UNUSED, int type)
252b5132
RH
4194{
4195 if (type == SHT_ORDERED)
4196 flags |= SEC_ALLOC | SEC_LOAD | SEC_SORT_ENTRIES;
4197
252b5132
RH
4198 return flags;
4199}
83eef883
AFB
4200
4201bfd_vma
4202ppc_elf_section_letter (int letter, const char **ptrmsg)
4203{
4204 if (letter == 'v')
4205 return SHF_PPC_VLE;
4206
4207 *ptrmsg = _("bad .section directive: want a,e,v,w,x,M,S,G,T in string");
4208 return -1;
4209}
252b5132
RH
4210#endif /* OBJ_ELF */
4211
4212\f
4213/* Pseudo-op handling. */
4214
4215/* The .byte pseudo-op. This is similar to the normal .byte
4216 pseudo-op, but it can also take a single ASCII string. */
4217
4218static void
98027b10 4219ppc_byte (int ignore ATTRIBUTE_UNUSED)
252b5132 4220{
bf7279d5
AM
4221 int count = 0;
4222
252b5132
RH
4223 if (*input_line_pointer != '\"')
4224 {
4225 cons (1);
4226 return;
4227 }
4228
4229 /* Gather characters. A real double quote is doubled. Unusual
4230 characters are not permitted. */
4231 ++input_line_pointer;
4232 while (1)
4233 {
4234 char c;
4235
4236 c = *input_line_pointer++;
4237
4238 if (c == '\"')
4239 {
4240 if (*input_line_pointer != '\"')
4241 break;
4242 ++input_line_pointer;
4243 }
4244
4245 FRAG_APPEND_1_CHAR (c);
bf7279d5 4246 ++count;
252b5132
RH
4247 }
4248
bf7279d5
AM
4249 if (warn_476 && count != 0 && (now_seg->flags & SEC_CODE) != 0)
4250 as_warn (_("data in executable section"));
252b5132
RH
4251 demand_empty_rest_of_line ();
4252}
4253\f
4254#ifdef OBJ_XCOFF
4255
4256/* XCOFF specific pseudo-op handling. */
4257
4258/* This is set if we are creating a .stabx symbol, since we don't want
4259 to handle symbol suffixes for such symbols. */
5b7c81bd 4260static bool ppc_stab_symbol;
252b5132
RH
4261
4262/* The .comm and .lcomm pseudo-ops for XCOFF. XCOFF puts common
4263 symbols in the .bss segment as though they were local common
67c1ffbe 4264 symbols, and uses a different smclas. The native Aix 4.3.3 assembler
1b2cb8e2
CC
4265 aligns .comm and .lcomm to 4 bytes.
4266 Symbols having a XMC_UL storage class are uninialized thread-local
4267 data. */
252b5132
RH
4268
4269static void
98027b10 4270ppc_comm (int lcomm)
252b5132
RH
4271{
4272 asection *current_seg = now_seg;
4273 subsegT current_subseg = now_subseg;
4274 char *name;
4275 char endc;
4276 char *end_name;
4277 offsetT size;
4278 offsetT align;
4279 symbolS *lcomm_sym = NULL;
4280 symbolS *sym;
4281 char *pfrag;
1b2cb8e2 4282 struct ppc_xcoff_section *section;
252b5132 4283
d02603dc 4284 endc = get_symbol_name (&name);
252b5132 4285 end_name = input_line_pointer;
d02603dc 4286 (void) restore_line_pointer (endc);
252b5132
RH
4287
4288 if (*input_line_pointer != ',')
4289 {
4290 as_bad (_("missing size"));
4291 ignore_rest_of_line ();
4292 return;
4293 }
4294 ++input_line_pointer;
4295
4296 size = get_absolute_expression ();
4297 if (size < 0)
4298 {
4299 as_bad (_("negative size"));
4300 ignore_rest_of_line ();
4301 return;
4302 }
4303
4304 if (! lcomm)
4305 {
4306 /* The third argument to .comm is the alignment. */
4307 if (*input_line_pointer != ',')
1ad63b2f 4308 align = 2;
252b5132
RH
4309 else
4310 {
4311 ++input_line_pointer;
4312 align = get_absolute_expression ();
4313 if (align <= 0)
4314 {
4315 as_warn (_("ignoring bad alignment"));
1ad63b2f 4316 align = 2;
252b5132
RH
4317 }
4318 }
4319 }
4320 else
4321 {
4322 char *lcomm_name;
4323 char lcomm_endc;
4324
252b5132
RH
4325 /* The third argument to .lcomm appears to be the real local
4326 common symbol to create. References to the symbol named in
4327 the first argument are turned into references to the third
4328 argument. */
4329 if (*input_line_pointer != ',')
4330 {
4331 as_bad (_("missing real symbol name"));
4332 ignore_rest_of_line ();
4333 return;
4334 }
4335 ++input_line_pointer;
4336
d02603dc 4337 lcomm_endc = get_symbol_name (&lcomm_name);
81d4177b 4338
252b5132
RH
4339 lcomm_sym = symbol_find_or_make (lcomm_name);
4340
d02603dc 4341 (void) restore_line_pointer (lcomm_endc);
3c02c47f
DE
4342
4343 /* The fourth argument to .lcomm is the alignment. */
4344 if (*input_line_pointer != ',')
4345 {
4346 if (size <= 4)
4347 align = 2;
4348 else
4349 align = 3;
4350 }
4351 else
4352 {
4353 ++input_line_pointer;
4354 align = get_absolute_expression ();
4355 if (align <= 0)
4356 {
4357 as_warn (_("ignoring bad alignment"));
4358 align = 2;
4359 }
4360 }
252b5132
RH
4361 }
4362
4363 *end_name = '\0';
4364 sym = symbol_find_or_make (name);
4365 *end_name = endc;
4366
4367 if (S_IS_DEFINED (sym)
4368 || S_GET_VALUE (sym) != 0)
4369 {
4370 as_bad (_("attempt to redefine symbol"));
4371 ignore_rest_of_line ();
4372 return;
4373 }
81d4177b 4374
1b2cb8e2
CC
4375 if (symbol_get_tc (sym)->symbol_class == XMC_UL
4376 || (lcomm && symbol_get_tc (lcomm_sym)->symbol_class == XMC_UL))
4377 {
4378 section = &ppc_xcoff_tbss_section;
4379 if (!ppc_xcoff_section_is_initialized (section))
4380 {
4381 ppc_init_xcoff_section (section,
5b7c81bd 4382 subseg_new (".tbss", 0), false);
1b2cb8e2
CC
4383 bfd_set_section_flags (section->segment,
4384 SEC_ALLOC | SEC_THREAD_LOCAL);
4385 seg_info (section->segment)->bss = 1;
4386 }
4387 }
4388 else
4389 section = &ppc_xcoff_bss_section;
4390
4391 record_alignment (section->segment, align);
81d4177b 4392
252b5132
RH
4393 if (! lcomm
4394 || ! S_IS_DEFINED (lcomm_sym))
4395 {
4396 symbolS *def_sym;
4397 offsetT def_size;
4398
4399 if (! lcomm)
4400 {
4401 def_sym = sym;
4402 def_size = size;
4403 S_SET_EXTERNAL (sym);
4404 }
4405 else
4406 {
809ffe0d 4407 symbol_get_tc (lcomm_sym)->output = 1;
252b5132
RH
4408 def_sym = lcomm_sym;
4409 def_size = 0;
4410 }
4411
1b2cb8e2 4412 subseg_set (section->segment, 1);
252b5132 4413 frag_align (align, 0, 0);
81d4177b 4414
809ffe0d 4415 symbol_set_frag (def_sym, frag_now);
252b5132
RH
4416 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, def_sym,
4417 def_size, (char *) NULL);
4418 *pfrag = 0;
1b2cb8e2 4419 S_SET_SEGMENT (def_sym, section->segment);
809ffe0d 4420 symbol_get_tc (def_sym)->align = align;
252b5132
RH
4421 }
4422 else if (lcomm)
4423 {
4424 /* Align the size of lcomm_sym. */
809ffe0d
ILT
4425 symbol_get_frag (lcomm_sym)->fr_offset =
4426 ((symbol_get_frag (lcomm_sym)->fr_offset + (1 << align) - 1)
252b5132 4427 &~ ((1 << align) - 1));
809ffe0d
ILT
4428 if (align > symbol_get_tc (lcomm_sym)->align)
4429 symbol_get_tc (lcomm_sym)->align = align;
252b5132
RH
4430 }
4431
4432 if (lcomm)
4433 {
4434 /* Make sym an offset from lcomm_sym. */
1b2cb8e2 4435 S_SET_SEGMENT (sym, section->segment);
809ffe0d
ILT
4436 symbol_set_frag (sym, symbol_get_frag (lcomm_sym));
4437 S_SET_VALUE (sym, symbol_get_frag (lcomm_sym)->fr_offset);
4438 symbol_get_frag (lcomm_sym)->fr_offset += size;
252b5132
RH
4439 }
4440
4441 subseg_set (current_seg, current_subseg);
4442
4443 demand_empty_rest_of_line ();
4444}
4445
4446/* The .csect pseudo-op. This switches us into a different
4447 subsegment. The first argument is a symbol whose value is the
4448 start of the .csect. In COFF, csect symbols get special aux
4449 entries defined by the x_csect field of union internal_auxent. The
4450 optional second argument is the alignment (the default is 2). */
4451
4452static void
98027b10 4453ppc_csect (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4454{
4455 char *name;
4456 char endc;
4457 symbolS *sym;
931e13a6 4458 offsetT align;
252b5132 4459
d02603dc 4460 endc = get_symbol_name (&name);
81d4177b 4461
252b5132
RH
4462 sym = symbol_find_or_make (name);
4463
d02603dc 4464 (void) restore_line_pointer (endc);
252b5132
RH
4465
4466 if (S_GET_NAME (sym)[0] == '\0')
4467 {
4468 /* An unnamed csect is assumed to be [PR]. */
96d56e9f 4469 symbol_get_tc (sym)->symbol_class = XMC_PR;
252b5132
RH
4470 }
4471
931e13a6 4472 align = 2;
252b5132
RH
4473 if (*input_line_pointer == ',')
4474 {
4475 ++input_line_pointer;
931e13a6 4476 align = get_absolute_expression ();
252b5132
RH
4477 }
4478
931e13a6
AM
4479 ppc_change_csect (sym, align);
4480
252b5132
RH
4481 demand_empty_rest_of_line ();
4482}
4483
4484/* Change to a different csect. */
4485
4486static void
98027b10 4487ppc_change_csect (symbolS *sym, offsetT align)
252b5132
RH
4488{
4489 if (S_IS_DEFINED (sym))
809ffe0d 4490 subseg_set (S_GET_SEGMENT (sym), symbol_get_tc (sym)->subseg);
252b5132
RH
4491 else
4492 {
1b2cb8e2 4493 struct ppc_xcoff_section *section;
252b5132
RH
4494 int after_toc;
4495 int hold_chunksize;
4496 symbolS *list;
931e13a6
AM
4497 int is_code;
4498 segT sec;
252b5132
RH
4499
4500 /* This is a new csect. We need to look at the symbol class to
4501 figure out whether it should go in the text section or the
4502 data section. */
4503 after_toc = 0;
931e13a6 4504 is_code = 0;
96d56e9f 4505 switch (symbol_get_tc (sym)->symbol_class)
252b5132
RH
4506 {
4507 case XMC_PR:
4508 case XMC_RO:
4509 case XMC_DB:
4510 case XMC_GL:
4511 case XMC_XO:
4512 case XMC_SV:
4513 case XMC_TI:
4514 case XMC_TB:
1b2cb8e2 4515 section = &ppc_xcoff_text_section;
931e13a6 4516 is_code = 1;
252b5132
RH
4517 break;
4518 case XMC_RW:
4519 case XMC_TC0:
4520 case XMC_TC:
4a403be0 4521 case XMC_TE:
252b5132
RH
4522 case XMC_DS:
4523 case XMC_UA:
252b5132 4524 case XMC_UC:
1b2cb8e2 4525 section = &ppc_xcoff_data_section;
252b5132 4526 if (ppc_toc_csect != NULL
809ffe0d 4527 && (symbol_get_tc (ppc_toc_csect)->subseg + 1
1b2cb8e2 4528 == section->next_subsegment))
252b5132 4529 after_toc = 1;
1b2cb8e2
CC
4530 break;
4531 case XMC_BS:
4532 section = &ppc_xcoff_bss_section;
4533 break;
4534 case XMC_TL:
4535 section = &ppc_xcoff_tdata_section;
4536 /* Create .tdata section if not yet done. */
4537 if (!ppc_xcoff_section_is_initialized (section))
4538 {
4539 ppc_init_xcoff_section (section, subseg_new (".tdata", 0),
5b7c81bd 4540 true);
1b2cb8e2
CC
4541 bfd_set_section_flags (section->segment, SEC_ALLOC
4542 | SEC_LOAD | SEC_RELOC | SEC_DATA
4543 | SEC_THREAD_LOCAL);
4544 }
4545 break;
4546 case XMC_UL:
4547 section = &ppc_xcoff_tbss_section;
4548 /* Create .tbss section if not yet done. */
4549 if (!ppc_xcoff_section_is_initialized (section))
4550 {
4551 ppc_init_xcoff_section (section, subseg_new (".tbss", 0),
5b7c81bd 4552 false);
1b2cb8e2
CC
4553 bfd_set_section_flags (section->segment, SEC_ALLOC |
4554 SEC_THREAD_LOCAL);
4555 seg_info (section->segment)->bss = 1;
4556 }
252b5132
RH
4557 break;
4558 default:
4559 abort ();
4560 }
4561
1b2cb8e2
CC
4562 S_SET_SEGMENT (sym, section->segment);
4563 symbol_get_tc (sym)->subseg = section->next_subsegment;
4564 ++section->next_subsegment;
4565
252b5132 4566 /* We set the obstack chunk size to a small value before
99a814a1
AM
4567 changing subsegments, so that we don't use a lot of memory
4568 space for what may be a small section. */
252b5132
RH
4569 hold_chunksize = chunksize;
4570 chunksize = 64;
4571
931e13a6
AM
4572 sec = subseg_new (segment_name (S_GET_SEGMENT (sym)),
4573 symbol_get_tc (sym)->subseg);
252b5132
RH
4574
4575 chunksize = hold_chunksize;
4576
4577 if (after_toc)
4578 ppc_after_toc_frag = frag_now;
4579
931e13a6
AM
4580 record_alignment (sec, align);
4581 if (is_code)
4582 frag_align_code (align, 0);
4583 else
4584 frag_align (align, 0, 0);
4585
809ffe0d 4586 symbol_set_frag (sym, frag_now);
252b5132
RH
4587 S_SET_VALUE (sym, (valueT) frag_now_fix ());
4588
931e13a6 4589 symbol_get_tc (sym)->align = align;
809ffe0d
ILT
4590 symbol_get_tc (sym)->output = 1;
4591 symbol_get_tc (sym)->within = sym;
81d4177b 4592
1b2cb8e2 4593 for (list = section->csects;
809ffe0d
ILT
4594 symbol_get_tc (list)->next != (symbolS *) NULL;
4595 list = symbol_get_tc (list)->next)
252b5132 4596 ;
809ffe0d 4597 symbol_get_tc (list)->next = sym;
81d4177b 4598
252b5132 4599 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
809ffe0d
ILT
4600 symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
4601 &symbol_lastP);
252b5132
RH
4602 }
4603
4604 ppc_current_csect = sym;
4605}
4606
85645aed
TG
4607static void
4608ppc_change_debug_section (unsigned int idx, subsegT subseg)
4609{
4610 segT sec;
4611 flagword oldflags;
4612 const struct xcoff_dwsect_name *dw = &xcoff_dwsect_names[idx];
4613
4614 sec = subseg_new (dw->name, subseg);
fd361982 4615 oldflags = bfd_section_flags (sec);
85645aed
TG
4616 if (oldflags == SEC_NO_FLAGS)
4617 {
4618 /* Just created section. */
4619 gas_assert (dw_sections[idx].sect == NULL);
4620
fd361982
AM
4621 bfd_set_section_flags (sec, SEC_DEBUGGING);
4622 bfd_set_section_alignment (sec, 0);
85645aed
TG
4623 dw_sections[idx].sect = sec;
4624 }
4625
4626 /* Not anymore in a csect. */
4627 ppc_current_csect = NULL;
4628}
4629
4630/* The .dwsect pseudo-op. Defines a DWARF section. Syntax is:
4631 .dwsect flag [, opt-label ]
4632*/
4633
4634static void
4635ppc_dwsect (int ignore ATTRIBUTE_UNUSED)
4636{
16de26a6 4637 valueT flag;
85645aed
TG
4638 symbolS *opt_label;
4639 const struct xcoff_dwsect_name *dw;
4640 struct dw_subsection *subseg;
4641 struct dw_section *dws;
4642 int i;
4643
4644 /* Find section. */
4645 flag = get_absolute_expression ();
4646 dw = NULL;
4647 for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
4648 if (xcoff_dwsect_names[i].flag == flag)
4649 {
4650 dw = &xcoff_dwsect_names[i];
4651 break;
4652 }
4653
4654 /* Parse opt-label. */
4655 if (*input_line_pointer == ',')
4656 {
d02603dc 4657 char *label;
85645aed
TG
4658 char c;
4659
4660 ++input_line_pointer;
4661
d02603dc 4662 c = get_symbol_name (&label);
85645aed 4663 opt_label = symbol_find_or_make (label);
d02603dc 4664 (void) restore_line_pointer (c);
85645aed
TG
4665 }
4666 else
4667 opt_label = NULL;
4668
4669 demand_empty_rest_of_line ();
4670
4671 /* Return now in case of unknown subsection. */
4672 if (dw == NULL)
4673 {
d6ed37ed 4674 as_bad (_("no known dwarf XCOFF section for flag 0x%08x\n"),
85645aed
TG
4675 (unsigned)flag);
4676 return;
4677 }
4678
4679 /* Find the subsection. */
4680 dws = &dw_sections[i];
4681 subseg = NULL;
4682 if (opt_label != NULL && S_IS_DEFINED (opt_label))
4683 {
4684 /* Sanity check (note that in theory S_GET_SEGMENT mustn't be null). */
4685 if (dws->sect == NULL || S_GET_SEGMENT (opt_label) != dws->sect)
4686 {
4687 as_bad (_("label %s was not defined in this dwarf section"),
4688 S_GET_NAME (opt_label));
4689 subseg = dws->anon_subseg;
4690 opt_label = NULL;
4691 }
4692 else
4693 subseg = symbol_get_tc (opt_label)->u.dw;
4694 }
4695
4696 if (subseg != NULL)
4697 {
4698 /* Switch to the subsection. */
4699 ppc_change_debug_section (i, subseg->subseg);
4700 }
4701 else
4702 {
4703 /* Create a new dw subsection. */
325801bd 4704 subseg = XNEW (struct dw_subsection);
85645aed
TG
4705
4706 if (opt_label == NULL)
4707 {
4708 /* The anonymous one. */
4709 subseg->subseg = 0;
4710 subseg->link = NULL;
4711 dws->anon_subseg = subseg;
4712 }
4713 else
4714 {
4715 /* A named one. */
4716 if (dws->list_subseg != NULL)
4717 subseg->subseg = dws->list_subseg->subseg + 1;
4718 else
4719 subseg->subseg = 1;
4720
4721 subseg->link = dws->list_subseg;
4722 dws->list_subseg = subseg;
4723 symbol_get_tc (opt_label)->u.dw = subseg;
4724 }
4725
4726 ppc_change_debug_section (i, subseg->subseg);
4727
4728 if (dw->def_size)
4729 {
4730 /* Add the length field. */
4731 expressionS *exp = &subseg->end_exp;
4732 int sz;
4733
4734 if (opt_label != NULL)
4735 symbol_set_value_now (opt_label);
4736
4737 /* Add the length field. Note that according to the AIX assembler
4738 manual, the size of the length field is 4 for powerpc32 but
4739 12 for powerpc64. */
4740 if (ppc_obj64)
4741 {
4742 /* Write the 64bit marker. */
4743 md_number_to_chars (frag_more (4), -1, 4);
4744 }
4745
4746 exp->X_op = O_subtract;
4747 exp->X_op_symbol = symbol_temp_new_now ();
4748 exp->X_add_symbol = symbol_temp_make ();
4749
4750 sz = ppc_obj64 ? 8 : 4;
4751 exp->X_add_number = -sz;
4752 emit_expr (exp, sz);
4753 }
4754 }
4755}
4756
252b5132
RH
4757/* This function handles the .text and .data pseudo-ops. These
4758 pseudo-ops aren't really used by XCOFF; we implement them for the
4759 convenience of people who aren't used to XCOFF. */
4760
4761static void
98027b10 4762ppc_section (int type)
252b5132
RH
4763{
4764 const char *name;
4765 symbolS *sym;
4766
4767 if (type == 't')
4768 name = ".text[PR]";
4769 else if (type == 'd')
4770 name = ".data[RW]";
4771 else
4772 abort ();
4773
4774 sym = symbol_find_or_make (name);
4775
931e13a6 4776 ppc_change_csect (sym, 2);
252b5132
RH
4777
4778 demand_empty_rest_of_line ();
4779}
4780
4781/* This function handles the .section pseudo-op. This is mostly to
4782 give an error, since XCOFF only supports .text, .data and .bss, but
4783 we do permit the user to name the text or data section. */
4784
4785static void
98027b10 4786ppc_named_section (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4787{
4788 char *user_name;
4789 const char *real_name;
4790 char c;
4791 symbolS *sym;
4792
d02603dc 4793 c = get_symbol_name (&user_name);
252b5132
RH
4794
4795 if (strcmp (user_name, ".text") == 0)
4796 real_name = ".text[PR]";
4797 else if (strcmp (user_name, ".data") == 0)
4798 real_name = ".data[RW]";
4799 else
4800 {
d6ed37ed 4801 as_bad (_("the XCOFF file format does not support arbitrary sections"));
d02603dc 4802 (void) restore_line_pointer (c);
252b5132
RH
4803 ignore_rest_of_line ();
4804 return;
4805 }
4806
d02603dc 4807 (void) restore_line_pointer (c);
252b5132
RH
4808
4809 sym = symbol_find_or_make (real_name);
4810
931e13a6 4811 ppc_change_csect (sym, 2);
252b5132
RH
4812
4813 demand_empty_rest_of_line ();
4814}
4815
4816/* The .extern pseudo-op. We create an undefined symbol. */
4817
4818static void
98027b10 4819ppc_extern (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4820{
4821 char *name;
4822 char endc;
4823
d02603dc 4824 endc = get_symbol_name (&name);
252b5132
RH
4825
4826 (void) symbol_find_or_make (name);
4827
d02603dc 4828 (void) restore_line_pointer (endc);
252b5132
RH
4829
4830 demand_empty_rest_of_line ();
4831}
4832
4833/* The .lglobl pseudo-op. Keep the symbol in the symbol table. */
4834
4835static void
98027b10 4836ppc_lglobl (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4837{
4838 char *name;
4839 char endc;
4840 symbolS *sym;
4841
d02603dc 4842 endc = get_symbol_name (&name);
252b5132
RH
4843
4844 sym = symbol_find_or_make (name);
4845
d02603dc 4846 (void) restore_line_pointer (endc);
252b5132 4847
809ffe0d 4848 symbol_get_tc (sym)->output = 1;
252b5132
RH
4849
4850 demand_empty_rest_of_line ();
4851}
4852
c865e45b
RS
4853/* The .ref pseudo-op. It takes a list of symbol names and inserts R_REF
4854 relocations at the beginning of the current csect.
4855
4856 (In principle, there's no reason why the relocations _have_ to be at
4857 the beginning. Anywhere in the csect would do. However, inserting
33eaf5de 4858 at the beginning is what the native assembler does, and it helps to
c865e45b
RS
4859 deal with cases where the .ref statements follow the section contents.)
4860
4861 ??? .refs don't work for empty .csects. However, the native assembler
4862 doesn't report an error in this case, and neither yet do we. */
4863
4864static void
4865ppc_ref (int ignore ATTRIBUTE_UNUSED)
4866{
4867 char *name;
4868 char c;
4869
4870 if (ppc_current_csect == NULL)
4871 {
4872 as_bad (_(".ref outside .csect"));
4873 ignore_rest_of_line ();
4874 return;
4875 }
4876
4877 do
4878 {
d02603dc 4879 c = get_symbol_name (&name);
c865e45b
RS
4880
4881 fix_at_start (symbol_get_frag (ppc_current_csect), 0,
5b7c81bd 4882 symbol_find_or_make (name), 0, false, BFD_RELOC_NONE);
c865e45b
RS
4883
4884 *input_line_pointer = c;
d02603dc 4885 SKIP_WHITESPACE_AFTER_NAME ();
c865e45b
RS
4886 c = *input_line_pointer;
4887 if (c == ',')
4888 {
4889 input_line_pointer++;
4890 SKIP_WHITESPACE ();
4891 if (is_end_of_line[(unsigned char) *input_line_pointer])
4892 {
4893 as_bad (_("missing symbol name"));
4894 ignore_rest_of_line ();
4895 return;
4896 }
4897 }
4898 }
4899 while (c == ',');
4900
4901 demand_empty_rest_of_line ();
4902}
4903
252b5132
RH
4904/* The .rename pseudo-op. The RS/6000 assembler can rename symbols,
4905 although I don't know why it bothers. */
4906
4907static void
98027b10 4908ppc_rename (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4909{
4910 char *name;
4911 char endc;
4912 symbolS *sym;
4913 int len;
4914
d02603dc 4915 endc = get_symbol_name (&name);
252b5132
RH
4916
4917 sym = symbol_find_or_make (name);
4918
d02603dc 4919 (void) restore_line_pointer (endc);
252b5132
RH
4920
4921 if (*input_line_pointer != ',')
4922 {
4923 as_bad (_("missing rename string"));
4924 ignore_rest_of_line ();
4925 return;
4926 }
4927 ++input_line_pointer;
4928
809ffe0d 4929 symbol_get_tc (sym)->real_name = demand_copy_C_string (&len);
252b5132
RH
4930
4931 demand_empty_rest_of_line ();
4932}
4933
4934/* The .stabx pseudo-op. This is similar to a normal .stabs
4935 pseudo-op, but slightly different. A sample is
4936 .stabx "main:F-1",.main,142,0
4937 The first argument is the symbol name to create. The second is the
4938 value, and the third is the storage class. The fourth seems to be
4939 always zero, and I am assuming it is the type. */
4940
4941static void
98027b10 4942ppc_stabx (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
4943{
4944 char *name;
4945 int len;
4946 symbolS *sym;
4947 expressionS exp;
4948
4949 name = demand_copy_C_string (&len);
4950
4951 if (*input_line_pointer != ',')
4952 {
4953 as_bad (_("missing value"));
4954 return;
4955 }
4956 ++input_line_pointer;
4957
5b7c81bd 4958 ppc_stab_symbol = true;
252b5132 4959 sym = symbol_make (name);
5b7c81bd 4960 ppc_stab_symbol = false;
252b5132 4961
809ffe0d 4962 symbol_get_tc (sym)->real_name = name;
252b5132
RH
4963
4964 (void) expression (&exp);
4965
4966 switch (exp.X_op)
4967 {
4968 case O_illegal:
4969 case O_absent:
4970 case O_big:
4971 as_bad (_("illegal .stabx expression; zero assumed"));
4972 exp.X_add_number = 0;
4973 /* Fall through. */
4974 case O_constant:
4975 S_SET_VALUE (sym, (valueT) exp.X_add_number);
809ffe0d 4976 symbol_set_frag (sym, &zero_address_frag);
252b5132
RH
4977 break;
4978
4979 case O_symbol:
4980 if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section)
809ffe0d 4981 symbol_set_value_expression (sym, &exp);
252b5132
RH
4982 else
4983 {
4984 S_SET_VALUE (sym,
4985 exp.X_add_number + S_GET_VALUE (exp.X_add_symbol));
809ffe0d 4986 symbol_set_frag (sym, symbol_get_frag (exp.X_add_symbol));
252b5132
RH
4987 }
4988 break;
4989
4990 default:
4991 /* The value is some complex expression. This will probably
99a814a1
AM
4992 fail at some later point, but this is probably the right
4993 thing to do here. */
809ffe0d 4994 symbol_set_value_expression (sym, &exp);
252b5132
RH
4995 break;
4996 }
4997
4998 S_SET_SEGMENT (sym, ppc_coff_debug_section);
809ffe0d 4999 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
252b5132
RH
5000
5001 if (*input_line_pointer != ',')
5002 {
5003 as_bad (_("missing class"));
5004 return;
5005 }
5006 ++input_line_pointer;
5007
5008 S_SET_STORAGE_CLASS (sym, get_absolute_expression ());
5009
5010 if (*input_line_pointer != ',')
5011 {
5012 as_bad (_("missing type"));
5013 return;
5014 }
5015 ++input_line_pointer;
5016
5017 S_SET_DATA_TYPE (sym, get_absolute_expression ());
5018
809ffe0d 5019 symbol_get_tc (sym)->output = 1;
252b5132 5020
c734e7e3
TG
5021 if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
5022 {
5023 /* In this case :
252b5132 5024
c734e7e3
TG
5025 .bs name
5026 .stabx "z",arrays_,133,0
5027 .es
99a814a1 5028
c734e7e3 5029 .comm arrays_,13768,3
99a814a1 5030
c734e7e3
TG
5031 resolve_symbol_value will copy the exp's "within" into sym's when the
5032 offset is 0. Since this seems to be corner case problem,
5033 only do the correction for storage class C_STSYM. A better solution
5034 would be to have the tc field updated in ppc_symbol_new_hook. */
99a814a1 5035
c734e7e3
TG
5036 if (exp.X_op == O_symbol)
5037 {
5038 if (ppc_current_block == NULL)
5039 as_bad (_(".stabx of storage class stsym must be within .bs/.es"));
99a814a1 5040
c734e7e3
TG
5041 symbol_get_tc (sym)->within = ppc_current_block;
5042 symbol_get_tc (exp.X_add_symbol)->within = ppc_current_block;
5043 }
5044 }
99a814a1 5045
252b5132
RH
5046 if (exp.X_op != O_symbol
5047 || ! S_IS_EXTERNAL (exp.X_add_symbol)
5048 || S_GET_SEGMENT (exp.X_add_symbol) != bss_section)
5049 ppc_frob_label (sym);
5050 else
5051 {
5052 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
5053 symbol_append (sym, exp.X_add_symbol, &symbol_rootP, &symbol_lastP);
809ffe0d
ILT
5054 if (symbol_get_tc (ppc_current_csect)->within == exp.X_add_symbol)
5055 symbol_get_tc (ppc_current_csect)->within = sym;
252b5132
RH
5056 }
5057
5058 demand_empty_rest_of_line ();
5059}
5060
5061/* The .function pseudo-op. This takes several arguments. The first
5062 argument seems to be the external name of the symbol. The second
67c1ffbe 5063 argument seems to be the label for the start of the function. gcc
252b5132
RH
5064 uses the same name for both. I have no idea what the third and
5065 fourth arguments are meant to be. The optional fifth argument is
5066 an expression for the size of the function. In COFF this symbol
5067 gets an aux entry like that used for a csect. */
5068
5069static void
98027b10 5070ppc_function (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5071{
5072 char *name;
5073 char endc;
5074 char *s;
5075 symbolS *ext_sym;
5076 symbolS *lab_sym;
5077
d02603dc 5078 endc = get_symbol_name (&name);
252b5132
RH
5079
5080 /* Ignore any [PR] suffix. */
5081 name = ppc_canonicalize_symbol_name (name);
5082 s = strchr (name, '[');
5083 if (s != (char *) NULL
5084 && strcmp (s + 1, "PR]") == 0)
5085 *s = '\0';
5086
5087 ext_sym = symbol_find_or_make (name);
5088
d02603dc 5089 (void) restore_line_pointer (endc);
252b5132
RH
5090
5091 if (*input_line_pointer != ',')
5092 {
5093 as_bad (_("missing symbol name"));
5094 ignore_rest_of_line ();
5095 return;
5096 }
5097 ++input_line_pointer;
5098
d02603dc 5099 endc = get_symbol_name (&name);
252b5132
RH
5100
5101 lab_sym = symbol_find_or_make (name);
5102
d02603dc 5103 (void) restore_line_pointer (endc);
252b5132
RH
5104
5105 if (ext_sym != lab_sym)
5106 {
809ffe0d
ILT
5107 expressionS exp;
5108
5109 exp.X_op = O_symbol;
5110 exp.X_add_symbol = lab_sym;
5111 exp.X_op_symbol = NULL;
5112 exp.X_add_number = 0;
5113 exp.X_unsigned = 0;
5114 symbol_set_value_expression (ext_sym, &exp);
252b5132
RH
5115 }
5116
96d56e9f
NC
5117 if (symbol_get_tc (ext_sym)->symbol_class == -1)
5118 symbol_get_tc (ext_sym)->symbol_class = XMC_PR;
809ffe0d 5119 symbol_get_tc (ext_sym)->output = 1;
252b5132
RH
5120
5121 if (*input_line_pointer == ',')
5122 {
91d6fa6a 5123 expressionS exp;
252b5132
RH
5124
5125 /* Ignore the third argument. */
5126 ++input_line_pointer;
91d6fa6a 5127 expression (& exp);
252b5132
RH
5128 if (*input_line_pointer == ',')
5129 {
5130 /* Ignore the fourth argument. */
5131 ++input_line_pointer;
91d6fa6a 5132 expression (& exp);
252b5132
RH
5133 if (*input_line_pointer == ',')
5134 {
5135 /* The fifth argument is the function size. */
5136 ++input_line_pointer;
e01e1cee
AM
5137 symbol_get_tc (ext_sym)->u.size
5138 = symbol_new ("L0\001", absolute_section,
5139 &zero_address_frag, 0);
85645aed 5140 pseudo_set (symbol_get_tc (ext_sym)->u.size);
252b5132
RH
5141 }
5142 }
5143 }
5144
5145 S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT);
5146 SF_SET_FUNCTION (ext_sym);
5147 SF_SET_PROCESS (ext_sym);
5148 coff_add_linesym (ext_sym);
5149
5150 demand_empty_rest_of_line ();
5151}
5152
5153/* The .bf pseudo-op. This is just like a COFF C_FCN symbol named
8642cce8
TR
5154 ".bf". If the pseudo op .bi was seen before .bf, patch the .bi sym
5155 with the correct line number */
5d6255fe 5156
8642cce8 5157static symbolS *saved_bi_sym = 0;
252b5132
RH
5158
5159static void
98027b10 5160ppc_bf (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5161{
5162 symbolS *sym;
5163
5164 sym = symbol_make (".bf");
5165 S_SET_SEGMENT (sym, text_section);
809ffe0d 5166 symbol_set_frag (sym, frag_now);
252b5132
RH
5167 S_SET_VALUE (sym, frag_now_fix ());
5168 S_SET_STORAGE_CLASS (sym, C_FCN);
5169
5170 coff_line_base = get_absolute_expression ();
5171
5172 S_SET_NUMBER_AUXILIARY (sym, 1);
5173 SA_SET_SYM_LNNO (sym, coff_line_base);
5174
8642cce8 5175 /* Line number for bi. */
5d6255fe 5176 if (saved_bi_sym)
8642cce8
TR
5177 {
5178 S_SET_VALUE (saved_bi_sym, coff_n_line_nos);
5179 saved_bi_sym = 0;
5180 }
5d6255fe 5181
8642cce8 5182
809ffe0d 5183 symbol_get_tc (sym)->output = 1;
252b5132
RH
5184
5185 ppc_frob_label (sym);
5186
5187 demand_empty_rest_of_line ();
5188}
5189
5190/* The .ef pseudo-op. This is just like a COFF C_FCN symbol named
5191 ".ef", except that the line number is absolute, not relative to the
5192 most recent ".bf" symbol. */
5193
5194static void
98027b10 5195ppc_ef (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5196{
5197 symbolS *sym;
5198
5199 sym = symbol_make (".ef");
5200 S_SET_SEGMENT (sym, text_section);
809ffe0d 5201 symbol_set_frag (sym, frag_now);
252b5132
RH
5202 S_SET_VALUE (sym, frag_now_fix ());
5203 S_SET_STORAGE_CLASS (sym, C_FCN);
5204 S_SET_NUMBER_AUXILIARY (sym, 1);
5205 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
809ffe0d 5206 symbol_get_tc (sym)->output = 1;
252b5132
RH
5207
5208 ppc_frob_label (sym);
5209
5210 demand_empty_rest_of_line ();
5211}
5212
5213/* The .bi and .ei pseudo-ops. These take a string argument and
5214 generates a C_BINCL or C_EINCL symbol, which goes at the start of
8642cce8
TR
5215 the symbol list. The value of .bi will be know when the next .bf
5216 is encountered. */
252b5132
RH
5217
5218static void
98027b10 5219ppc_biei (int ei)
252b5132
RH
5220{
5221 static symbolS *last_biei;
5222
5223 char *name;
5224 int len;
5225 symbolS *sym;
5226 symbolS *look;
5227
5228 name = demand_copy_C_string (&len);
5229
5230 /* The value of these symbols is actually file offset. Here we set
5231 the value to the index into the line number entries. In
5232 ppc_frob_symbols we set the fix_line field, which will cause BFD
5233 to do the right thing. */
5234
5235 sym = symbol_make (name);
5236 /* obj-coff.c currently only handles line numbers correctly in the
5237 .text section. */
5238 S_SET_SEGMENT (sym, text_section);
5239 S_SET_VALUE (sym, coff_n_line_nos);
809ffe0d 5240 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
252b5132
RH
5241
5242 S_SET_STORAGE_CLASS (sym, ei ? C_EINCL : C_BINCL);
809ffe0d 5243 symbol_get_tc (sym)->output = 1;
81d4177b 5244
8642cce8 5245 /* Save bi. */
5d6255fe 5246 if (ei)
8642cce8
TR
5247 saved_bi_sym = 0;
5248 else
5249 saved_bi_sym = sym;
5250
252b5132
RH
5251 for (look = last_biei ? last_biei : symbol_rootP;
5252 (look != (symbolS *) NULL
5253 && (S_GET_STORAGE_CLASS (look) == C_FILE
5254 || S_GET_STORAGE_CLASS (look) == C_BINCL
5255 || S_GET_STORAGE_CLASS (look) == C_EINCL));
5256 look = symbol_next (look))
5257 ;
5258 if (look != (symbolS *) NULL)
5259 {
5260 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
5261 symbol_insert (sym, look, &symbol_rootP, &symbol_lastP);
5262 last_biei = sym;
5263 }
5264
5265 demand_empty_rest_of_line ();
5266}
5267
5268/* The .bs pseudo-op. This generates a C_BSTAT symbol named ".bs".
5269 There is one argument, which is a csect symbol. The value of the
5270 .bs symbol is the index of this csect symbol. */
5271
5272static void
98027b10 5273ppc_bs (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5274{
5275 char *name;
5276 char endc;
5277 symbolS *csect;
5278 symbolS *sym;
5279
5280 if (ppc_current_block != NULL)
5281 as_bad (_("nested .bs blocks"));
5282
d02603dc 5283 endc = get_symbol_name (&name);
252b5132
RH
5284
5285 csect = symbol_find_or_make (name);
5286
d02603dc 5287 (void) restore_line_pointer (endc);
252b5132
RH
5288
5289 sym = symbol_make (".bs");
5290 S_SET_SEGMENT (sym, now_seg);
5291 S_SET_STORAGE_CLASS (sym, C_BSTAT);
809ffe0d
ILT
5292 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5293 symbol_get_tc (sym)->output = 1;
252b5132 5294
809ffe0d 5295 symbol_get_tc (sym)->within = csect;
252b5132
RH
5296
5297 ppc_frob_label (sym);
5298
5299 ppc_current_block = sym;
5300
5301 demand_empty_rest_of_line ();
5302}
5303
5304/* The .es pseudo-op. Generate a C_ESTART symbol named .es. */
5305
5306static void
98027b10 5307ppc_es (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5308{
5309 symbolS *sym;
5310
5311 if (ppc_current_block == NULL)
5312 as_bad (_(".es without preceding .bs"));
5313
5314 sym = symbol_make (".es");
5315 S_SET_SEGMENT (sym, now_seg);
5316 S_SET_STORAGE_CLASS (sym, C_ESTAT);
809ffe0d
ILT
5317 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
5318 symbol_get_tc (sym)->output = 1;
252b5132
RH
5319
5320 ppc_frob_label (sym);
5321
5322 ppc_current_block = NULL;
5323
5324 demand_empty_rest_of_line ();
5325}
5326
5327/* The .bb pseudo-op. Generate a C_BLOCK symbol named .bb, with a
5328 line number. */
5329
5330static void
98027b10 5331ppc_bb (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5332{
5333 symbolS *sym;
5334
5335 sym = symbol_make (".bb");
5336 S_SET_SEGMENT (sym, text_section);
809ffe0d 5337 symbol_set_frag (sym, frag_now);
252b5132
RH
5338 S_SET_VALUE (sym, frag_now_fix ());
5339 S_SET_STORAGE_CLASS (sym, C_BLOCK);
5340
5341 S_SET_NUMBER_AUXILIARY (sym, 1);
5342 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
5343
809ffe0d 5344 symbol_get_tc (sym)->output = 1;
252b5132
RH
5345
5346 SF_SET_PROCESS (sym);
5347
5348 ppc_frob_label (sym);
5349
5350 demand_empty_rest_of_line ();
5351}
5352
5353/* The .eb pseudo-op. Generate a C_BLOCK symbol named .eb, with a
5354 line number. */
5355
5356static void
98027b10 5357ppc_eb (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5358{
5359 symbolS *sym;
5360
5361 sym = symbol_make (".eb");
5362 S_SET_SEGMENT (sym, text_section);
809ffe0d 5363 symbol_set_frag (sym, frag_now);
252b5132
RH
5364 S_SET_VALUE (sym, frag_now_fix ());
5365 S_SET_STORAGE_CLASS (sym, C_BLOCK);
5366 S_SET_NUMBER_AUXILIARY (sym, 1);
5367 SA_SET_SYM_LNNO (sym, get_absolute_expression ());
809ffe0d 5368 symbol_get_tc (sym)->output = 1;
252b5132
RH
5369
5370 SF_SET_PROCESS (sym);
5371
5372 ppc_frob_label (sym);
5373
5374 demand_empty_rest_of_line ();
5375}
5376
5377/* The .bc pseudo-op. This just creates a C_BCOMM symbol with a
5378 specified name. */
5379
5380static void
98027b10 5381ppc_bc (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5382{
5383 char *name;
5384 int len;
5385 symbolS *sym;
5386
5387 name = demand_copy_C_string (&len);
5388 sym = symbol_make (name);
5389 S_SET_SEGMENT (sym, ppc_coff_debug_section);
809ffe0d 5390 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
252b5132
RH
5391 S_SET_STORAGE_CLASS (sym, C_BCOMM);
5392 S_SET_VALUE (sym, 0);
809ffe0d 5393 symbol_get_tc (sym)->output = 1;
252b5132
RH
5394
5395 ppc_frob_label (sym);
5396
5397 demand_empty_rest_of_line ();
5398}
5399
5400/* The .ec pseudo-op. This just creates a C_ECOMM symbol. */
5401
5402static void
98027b10 5403ppc_ec (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5404{
5405 symbolS *sym;
5406
5407 sym = symbol_make (".ec");
5408 S_SET_SEGMENT (sym, ppc_coff_debug_section);
809ffe0d 5409 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
252b5132
RH
5410 S_SET_STORAGE_CLASS (sym, C_ECOMM);
5411 S_SET_VALUE (sym, 0);
809ffe0d 5412 symbol_get_tc (sym)->output = 1;
252b5132
RH
5413
5414 ppc_frob_label (sym);
5415
5416 demand_empty_rest_of_line ();
5417}
5418
5419/* The .toc pseudo-op. Switch to the .toc subsegment. */
5420
5421static void
98027b10 5422ppc_toc (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5423{
5424 if (ppc_toc_csect != (symbolS *) NULL)
809ffe0d 5425 subseg_set (data_section, symbol_get_tc (ppc_toc_csect)->subseg);
252b5132
RH
5426 else
5427 {
5428 subsegT subseg;
5429 symbolS *sym;
5430 symbolS *list;
81d4177b 5431
1b2cb8e2
CC
5432 subseg = ppc_xcoff_data_section.next_subsegment;
5433 ++ppc_xcoff_data_section.next_subsegment;
252b5132
RH
5434
5435 subseg_new (segment_name (data_section), subseg);
5436 ppc_toc_frag = frag_now;
5437
5438 sym = symbol_find_or_make ("TOC[TC0]");
809ffe0d 5439 symbol_set_frag (sym, frag_now);
252b5132
RH
5440 S_SET_SEGMENT (sym, data_section);
5441 S_SET_VALUE (sym, (valueT) frag_now_fix ());
809ffe0d
ILT
5442 symbol_get_tc (sym)->subseg = subseg;
5443 symbol_get_tc (sym)->output = 1;
5444 symbol_get_tc (sym)->within = sym;
252b5132
RH
5445
5446 ppc_toc_csect = sym;
81d4177b 5447
1b2cb8e2 5448 for (list = ppc_xcoff_data_section.csects;
809ffe0d
ILT
5449 symbol_get_tc (list)->next != (symbolS *) NULL;
5450 list = symbol_get_tc (list)->next)
252b5132 5451 ;
809ffe0d 5452 symbol_get_tc (list)->next = sym;
252b5132
RH
5453
5454 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
809ffe0d
ILT
5455 symbol_append (sym, symbol_get_tc (list)->within, &symbol_rootP,
5456 &symbol_lastP);
252b5132
RH
5457 }
5458
5459 ppc_current_csect = ppc_toc_csect;
5460
5461 demand_empty_rest_of_line ();
5462}
5463
5464/* The AIX assembler automatically aligns the operands of a .long or
5465 .short pseudo-op, and we want to be compatible. */
5466
5467static void
98027b10 5468ppc_xcoff_cons (int log_size)
252b5132
RH
5469{
5470 frag_align (log_size, 0, 0);
5471 record_alignment (now_seg, log_size);
5472 cons (1 << log_size);
5473}
5474
5475static void
98027b10 5476ppc_vbyte (int dummy ATTRIBUTE_UNUSED)
252b5132
RH
5477{
5478 expressionS exp;
5479 int byte_count;
5480
5481 (void) expression (&exp);
5482
5483 if (exp.X_op != O_constant)
5484 {
5485 as_bad (_("non-constant byte count"));
5486 return;
5487 }
5488
5489 byte_count = exp.X_add_number;
5490
5491 if (*input_line_pointer != ',')
5492 {
5493 as_bad (_("missing value"));
5494 return;
5495 }
5496
5497 ++input_line_pointer;
5498 cons (byte_count);
5499}
5500
85645aed
TG
5501void
5502ppc_xcoff_end (void)
5503{
5504 int i;
5505
5506 for (i = 0; i < XCOFF_DWSECT_NBR_NAMES; i++)
5507 {
5508 struct dw_section *dws = &dw_sections[i];
5509 struct dw_subsection *dwss;
5510
5511 if (dws->anon_subseg)
5512 {
5513 dwss = dws->anon_subseg;
5514 dwss->link = dws->list_subseg;
5515 }
5516 else
5517 dwss = dws->list_subseg;
5518
5519 for (; dwss != NULL; dwss = dwss->link)
5520 if (dwss->end_exp.X_add_symbol != NULL)
5521 {
5522 subseg_set (dws->sect, dwss->subseg);
5523 symbol_set_value_now (dwss->end_exp.X_add_symbol);
5524 }
5525 }
22f72c48 5526 ppc_cpu = 0;
85645aed
TG
5527}
5528
252b5132 5529#endif /* OBJ_XCOFF */
0baf16f2 5530#if defined (OBJ_XCOFF) || defined (OBJ_ELF)
252b5132
RH
5531\f
5532/* The .tc pseudo-op. This is used when generating either XCOFF or
5533 ELF. This takes two or more arguments.
5534
5535 When generating XCOFF output, the first argument is the name to
5536 give to this location in the toc; this will be a symbol with class
0baf16f2 5537 TC. The rest of the arguments are N-byte values to actually put at
252b5132 5538 this location in the TOC; often there is just one more argument, a
1049f94e 5539 relocatable symbol reference. The size of the value to store
0baf16f2
AM
5540 depends on target word size. A 32-bit target uses 4-byte values, a
5541 64-bit target uses 8-byte values.
252b5132
RH
5542
5543 When not generating XCOFF output, the arguments are the same, but
5544 the first argument is simply ignored. */
5545
5546static void
98027b10 5547ppc_tc (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
5548{
5549#ifdef OBJ_XCOFF
5550
5551 /* Define the TOC symbol name. */
5552 {
5553 char *name;
5554 char endc;
5555 symbolS *sym;
5556
5557 if (ppc_toc_csect == (symbolS *) NULL
5558 || ppc_toc_csect != ppc_current_csect)
5559 {
5560 as_bad (_(".tc not in .toc section"));
5561 ignore_rest_of_line ();
5562 return;
5563 }
5564
d02603dc 5565 endc = get_symbol_name (&name);
252b5132
RH
5566
5567 sym = symbol_find_or_make (name);
5568
d02603dc 5569 (void) restore_line_pointer (endc);
252b5132
RH
5570
5571 if (S_IS_DEFINED (sym))
5572 {
5573 symbolS *label;
5574
809ffe0d 5575 label = symbol_get_tc (ppc_current_csect)->within;
96d56e9f 5576 if (symbol_get_tc (label)->symbol_class != XMC_TC0)
252b5132
RH
5577 {
5578 as_bad (_(".tc with no label"));
5579 ignore_rest_of_line ();
5580 return;
5581 }
5582
5583 S_SET_SEGMENT (label, S_GET_SEGMENT (sym));
809ffe0d 5584 symbol_set_frag (label, symbol_get_frag (sym));
252b5132
RH
5585 S_SET_VALUE (label, S_GET_VALUE (sym));
5586
5587 while (! is_end_of_line[(unsigned char) *input_line_pointer])
5588 ++input_line_pointer;
5589
5590 return;
5591 }
5592
5593 S_SET_SEGMENT (sym, now_seg);
809ffe0d 5594 symbol_set_frag (sym, frag_now);
252b5132 5595 S_SET_VALUE (sym, (valueT) frag_now_fix ());
4a403be0
CC
5596
5597 /* AIX assembler seems to allow any storage class to be set in .tc.
5598 But for now, only XMC_TC and XMC_TE are supported by us. */
5599 switch (symbol_get_tc (sym)->symbol_class)
5600 {
5601 case XMC_TC:
5602 case XMC_TE:
5603 break;
5604
5605 default:
5606 as_bad (_(".tc with storage class %d not yet supported"),
5607 symbol_get_tc (sym)->symbol_class);
5608 ignore_rest_of_line ();
5609 return;
5610 }
809ffe0d 5611 symbol_get_tc (sym)->output = 1;
252b5132
RH
5612
5613 ppc_frob_label (sym);
5614 }
5615
0baf16f2
AM
5616#endif /* OBJ_XCOFF */
5617#ifdef OBJ_ELF
9c7977b3 5618 int align;
252b5132
RH
5619
5620 /* Skip the TOC symbol name. */
5621 while (is_part_of_name (*input_line_pointer)
d13d4015 5622 || *input_line_pointer == ' '
252b5132
RH
5623 || *input_line_pointer == '['
5624 || *input_line_pointer == ']'
5625 || *input_line_pointer == '{'
5626 || *input_line_pointer == '}')
5627 ++input_line_pointer;
5628
0baf16f2 5629 /* Align to a four/eight byte boundary. */
2b3c4602 5630 align = ppc_obj64 ? 3 : 2;
9c7977b3
AM
5631 frag_align (align, 0, 0);
5632 record_alignment (now_seg, align);
0baf16f2 5633#endif /* OBJ_ELF */
252b5132
RH
5634
5635 if (*input_line_pointer != ',')
5636 demand_empty_rest_of_line ();
5637 else
5638 {
5639 ++input_line_pointer;
2b3c4602 5640 cons (ppc_obj64 ? 8 : 4);
252b5132
RH
5641 }
5642}
0baf16f2
AM
5643
5644/* Pseudo-op .machine. */
0baf16f2
AM
5645
5646static void
98027b10 5647ppc_machine (int ignore ATTRIBUTE_UNUSED)
0baf16f2 5648{
d02603dc 5649 char c;
69c040df
AM
5650 char *cpu_string;
5651#define MAX_HISTORY 100
fa452fa6 5652 static ppc_cpu_t *cpu_history;
69c040df
AM
5653 static int curr_hist;
5654
5655 SKIP_WHITESPACE ();
5656
d02603dc
NC
5657 c = get_symbol_name (&cpu_string);
5658 cpu_string = xstrdup (cpu_string);
5659 (void) restore_line_pointer (c);
69c040df
AM
5660
5661 if (cpu_string != NULL)
5662 {
fa452fa6 5663 ppc_cpu_t old_cpu = ppc_cpu;
69fe9ce5 5664 ppc_cpu_t new_cpu;
69c040df
AM
5665 char *p;
5666
5667 for (p = cpu_string; *p != 0; p++)
5668 *p = TOLOWER (*p);
5669
5670 if (strcmp (cpu_string, "push") == 0)
5671 {
5672 if (cpu_history == NULL)
325801bd 5673 cpu_history = XNEWVEC (ppc_cpu_t, MAX_HISTORY);
69c040df
AM
5674
5675 if (curr_hist >= MAX_HISTORY)
5676 as_bad (_(".machine stack overflow"));
5677 else
5678 cpu_history[curr_hist++] = ppc_cpu;
5679 }
5680 else if (strcmp (cpu_string, "pop") == 0)
5681 {
5682 if (curr_hist <= 0)
5683 as_bad (_(".machine stack underflow"));
5684 else
5685 ppc_cpu = cpu_history[--curr_hist];
5686 }
776fc418 5687 else if ((new_cpu = ppc_parse_cpu (ppc_cpu, &sticky, cpu_string)) != 0)
69fe9ce5 5688 ppc_cpu = new_cpu;
69c040df
AM
5689 else
5690 as_bad (_("invalid machine `%s'"), cpu_string);
5691
5692 if (ppc_cpu != old_cpu)
5693 ppc_setup_opcodes ();
5694 }
5695
5696 demand_empty_rest_of_line ();
0baf16f2 5697}
0baf16f2 5698#endif /* defined (OBJ_XCOFF) || defined (OBJ_ELF) */
252b5132 5699\f
252b5132
RH
5700#ifdef OBJ_XCOFF
5701
5702/* XCOFF specific symbol and file handling. */
5703
5704/* Canonicalize the symbol name. We use the to force the suffix, if
5705 any, to use square brackets, and to be in upper case. */
5706
5707char *
98027b10 5708ppc_canonicalize_symbol_name (char *name)
252b5132
RH
5709{
5710 char *s;
5711
5712 if (ppc_stab_symbol)
5713 return name;
5714
5715 for (s = name; *s != '\0' && *s != '{' && *s != '['; s++)
5716 ;
5717 if (*s != '\0')
5718 {
5719 char brac;
5720
5721 if (*s == '[')
5722 brac = ']';
5723 else
5724 {
5725 *s = '[';
5726 brac = '}';
5727 }
5728
5729 for (s++; *s != '\0' && *s != brac; s++)
3882b010 5730 *s = TOUPPER (*s);
252b5132
RH
5731
5732 if (*s == '\0' || s[1] != '\0')
5733 as_bad (_("bad symbol suffix"));
5734
5735 *s = ']';
5736 }
5737
5738 return name;
5739}
5740
5741/* Set the class of a symbol based on the suffix, if any. This is
5742 called whenever a new symbol is created. */
5743
5744void
98027b10 5745ppc_symbol_new_hook (symbolS *sym)
252b5132 5746{
809ffe0d 5747 struct ppc_tc_sy *tc;
252b5132
RH
5748 const char *s;
5749
809ffe0d
ILT
5750 tc = symbol_get_tc (sym);
5751 tc->next = NULL;
5752 tc->output = 0;
96d56e9f 5753 tc->symbol_class = -1;
809ffe0d
ILT
5754 tc->real_name = NULL;
5755 tc->subseg = 0;
5756 tc->align = 0;
85645aed
TG
5757 tc->u.size = NULL;
5758 tc->u.dw = NULL;
809ffe0d 5759 tc->within = NULL;
252b5132
RH
5760
5761 if (ppc_stab_symbol)
5762 return;
5763
5764 s = strchr (S_GET_NAME (sym), '[');
5765 if (s == (const char *) NULL)
5766 {
5767 /* There is no suffix. */
5768 return;
5769 }
5770
5771 ++s;
5772
5773 switch (s[0])
5774 {
5775 case 'B':
5776 if (strcmp (s, "BS]") == 0)
96d56e9f 5777 tc->symbol_class = XMC_BS;
252b5132
RH
5778 break;
5779 case 'D':
5780 if (strcmp (s, "DB]") == 0)
96d56e9f 5781 tc->symbol_class = XMC_DB;
252b5132 5782 else if (strcmp (s, "DS]") == 0)
96d56e9f 5783 tc->symbol_class = XMC_DS;
252b5132
RH
5784 break;
5785 case 'G':
5786 if (strcmp (s, "GL]") == 0)
96d56e9f 5787 tc->symbol_class = XMC_GL;
252b5132
RH
5788 break;
5789 case 'P':
5790 if (strcmp (s, "PR]") == 0)
96d56e9f 5791 tc->symbol_class = XMC_PR;
252b5132
RH
5792 break;
5793 case 'R':
5794 if (strcmp (s, "RO]") == 0)
96d56e9f 5795 tc->symbol_class = XMC_RO;
252b5132 5796 else if (strcmp (s, "RW]") == 0)
96d56e9f 5797 tc->symbol_class = XMC_RW;
252b5132
RH
5798 break;
5799 case 'S':
5800 if (strcmp (s, "SV]") == 0)
96d56e9f 5801 tc->symbol_class = XMC_SV;
252b5132
RH
5802 break;
5803 case 'T':
5804 if (strcmp (s, "TC]") == 0)
96d56e9f 5805 tc->symbol_class = XMC_TC;
252b5132 5806 else if (strcmp (s, "TI]") == 0)
96d56e9f 5807 tc->symbol_class = XMC_TI;
252b5132 5808 else if (strcmp (s, "TB]") == 0)
96d56e9f 5809 tc->symbol_class = XMC_TB;
252b5132 5810 else if (strcmp (s, "TC0]") == 0 || strcmp (s, "T0]") == 0)
96d56e9f 5811 tc->symbol_class = XMC_TC0;
4a403be0
CC
5812 else if (strcmp (s, "TE]") == 0)
5813 tc->symbol_class = XMC_TE;
1b2cb8e2
CC
5814 else if (strcmp (s, "TL]") == 0)
5815 tc->symbol_class = XMC_TL;
252b5132
RH
5816 break;
5817 case 'U':
5818 if (strcmp (s, "UA]") == 0)
96d56e9f 5819 tc->symbol_class = XMC_UA;
252b5132 5820 else if (strcmp (s, "UC]") == 0)
96d56e9f 5821 tc->symbol_class = XMC_UC;
1b2cb8e2
CC
5822 else if (strcmp (s, "UL]") == 0)
5823 tc->symbol_class = XMC_UL;
252b5132
RH
5824 break;
5825 case 'X':
5826 if (strcmp (s, "XO]") == 0)
96d56e9f 5827 tc->symbol_class = XMC_XO;
252b5132
RH
5828 break;
5829 }
5830
96d56e9f 5831 if (tc->symbol_class == -1)
d6ed37ed 5832 as_bad (_("unrecognized symbol suffix"));
252b5132
RH
5833}
5834
252b5132
RH
5835/* This variable is set by ppc_frob_symbol if any absolute symbols are
5836 seen. It tells ppc_adjust_symtab whether it needs to look through
5837 the symbols. */
5838
5b7c81bd 5839static bool ppc_saw_abs;
252b5132
RH
5840
5841/* Change the name of a symbol just before writing it out. Set the
5842 real name if the .rename pseudo-op was used. Otherwise, remove any
5843 class suffix. Return 1 if the symbol should not be included in the
5844 symbol table. */
5845
5846int
98027b10 5847ppc_frob_symbol (symbolS *sym)
252b5132
RH
5848{
5849 static symbolS *ppc_last_function;
5850 static symbolS *set_end;
5851
5852 /* Discard symbols that should not be included in the output symbol
5853 table. */
809ffe0d
ILT
5854 if (! symbol_used_in_reloc_p (sym)
5855 && ((symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) != 0
670ec21d 5856 || (! (S_IS_EXTERNAL (sym) || S_IS_WEAK (sym))
809ffe0d 5857 && ! symbol_get_tc (sym)->output
252b5132
RH
5858 && S_GET_STORAGE_CLASS (sym) != C_FILE)))
5859 return 1;
5860
a161fe53
AM
5861 /* This one will disappear anyway. Don't make a csect sym for it. */
5862 if (sym == abs_section_sym)
5863 return 1;
5864
809ffe0d
ILT
5865 if (symbol_get_tc (sym)->real_name != (char *) NULL)
5866 S_SET_NAME (sym, symbol_get_tc (sym)->real_name);
252b5132
RH
5867 else
5868 {
5869 const char *name;
5870 const char *s;
5871
5872 name = S_GET_NAME (sym);
5873 s = strchr (name, '[');
5874 if (s != (char *) NULL)
5875 {
5876 unsigned int len;
5877 char *snew;
5878
5879 len = s - name;
a44e2901 5880 snew = xstrndup (name, len);
252b5132
RH
5881
5882 S_SET_NAME (sym, snew);
5883 }
5884 }
5885
5886 if (set_end != (symbolS *) NULL)
5887 {
5888 SA_SET_SYM_ENDNDX (set_end, sym);
5889 set_end = NULL;
5890 }
5891
5892 if (SF_GET_FUNCTION (sym))
5893 {
5894 if (ppc_last_function != (symbolS *) NULL)
5895 as_bad (_("two .function pseudo-ops with no intervening .ef"));
5896 ppc_last_function = sym;
85645aed 5897 if (symbol_get_tc (sym)->u.size != (symbolS *) NULL)
252b5132 5898 {
85645aed 5899 resolve_symbol_value (symbol_get_tc (sym)->u.size);
809ffe0d 5900 SA_SET_SYM_FSIZE (sym,
85645aed 5901 (long) S_GET_VALUE (symbol_get_tc (sym)->u.size));
252b5132
RH
5902 }
5903 }
5904 else if (S_GET_STORAGE_CLASS (sym) == C_FCN
5905 && strcmp (S_GET_NAME (sym), ".ef") == 0)
5906 {
5907 if (ppc_last_function == (symbolS *) NULL)
5908 as_bad (_(".ef with no preceding .function"));
5909 else
5910 {
5911 set_end = ppc_last_function;
5912 ppc_last_function = NULL;
5913
5914 /* We don't have a C_EFCN symbol, but we need to force the
5915 COFF backend to believe that it has seen one. */
5916 coff_last_function = NULL;
5917 }
5918 }
5919
670ec21d 5920 if (! (S_IS_EXTERNAL (sym) || S_IS_WEAK (sym))
809ffe0d 5921 && (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) == 0
252b5132
RH
5922 && S_GET_STORAGE_CLASS (sym) != C_FILE
5923 && S_GET_STORAGE_CLASS (sym) != C_FCN
5924 && S_GET_STORAGE_CLASS (sym) != C_BLOCK
5925 && S_GET_STORAGE_CLASS (sym) != C_BSTAT
5926 && S_GET_STORAGE_CLASS (sym) != C_ESTAT
5927 && S_GET_STORAGE_CLASS (sym) != C_BINCL
5928 && S_GET_STORAGE_CLASS (sym) != C_EINCL
5929 && S_GET_SEGMENT (sym) != ppc_coff_debug_section)
5930 S_SET_STORAGE_CLASS (sym, C_HIDEXT);
5931
5932 if (S_GET_STORAGE_CLASS (sym) == C_EXT
8602d4fe 5933 || S_GET_STORAGE_CLASS (sym) == C_AIX_WEAKEXT
252b5132
RH
5934 || S_GET_STORAGE_CLASS (sym) == C_HIDEXT)
5935 {
5936 int i;
5937 union internal_auxent *a;
5938
5939 /* Create a csect aux. */
5940 i = S_GET_NUMBER_AUXILIARY (sym);
5941 S_SET_NUMBER_AUXILIARY (sym, i + 1);
809ffe0d 5942 a = &coffsymbol (symbol_get_bfdsym (sym))->native[i + 1].u.auxent;
96d56e9f 5943 if (symbol_get_tc (sym)->symbol_class == XMC_TC0)
252b5132
RH
5944 {
5945 /* This is the TOC table. */
5946 know (strcmp (S_GET_NAME (sym), "TOC") == 0);
5947 a->x_csect.x_scnlen.l = 0;
5948 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
5949 }
809ffe0d 5950 else if (symbol_get_tc (sym)->subseg != 0)
252b5132
RH
5951 {
5952 /* This is a csect symbol. x_scnlen is the size of the
5953 csect. */
809ffe0d 5954 if (symbol_get_tc (sym)->next == (symbolS *) NULL)
fd361982 5955 a->x_csect.x_scnlen.l = (bfd_section_size (S_GET_SEGMENT (sym))
252b5132
RH
5956 - S_GET_VALUE (sym));
5957 else
5958 {
6386f3a7 5959 resolve_symbol_value (symbol_get_tc (sym)->next);
809ffe0d 5960 a->x_csect.x_scnlen.l = (S_GET_VALUE (symbol_get_tc (sym)->next)
252b5132
RH
5961 - S_GET_VALUE (sym));
5962 }
809ffe0d 5963 a->x_csect.x_smtyp = (symbol_get_tc (sym)->align << 3) | XTY_SD;
252b5132 5964 }
1b2cb8e2
CC
5965 else if (S_GET_SEGMENT (sym) == bss_section
5966 || S_GET_SEGMENT (sym) == ppc_xcoff_tbss_section.segment)
252b5132
RH
5967 {
5968 /* This is a common symbol. */
809ffe0d
ILT
5969 a->x_csect.x_scnlen.l = symbol_get_frag (sym)->fr_offset;
5970 a->x_csect.x_smtyp = (symbol_get_tc (sym)->align << 3) | XTY_CM;
1b2cb8e2
CC
5971 if (S_GET_SEGMENT (sym) == ppc_xcoff_tbss_section.segment)
5972 symbol_get_tc (sym)->symbol_class = XMC_UL;
5973 else if (S_IS_EXTERNAL (sym))
96d56e9f 5974 symbol_get_tc (sym)->symbol_class = XMC_RW;
252b5132 5975 else
96d56e9f 5976 symbol_get_tc (sym)->symbol_class = XMC_BS;
252b5132
RH
5977 }
5978 else if (S_GET_SEGMENT (sym) == absolute_section)
5979 {
5980 /* This is an absolute symbol. The csect will be created by
99a814a1 5981 ppc_adjust_symtab. */
5b7c81bd 5982 ppc_saw_abs = true;
252b5132 5983 a->x_csect.x_smtyp = XTY_LD;
96d56e9f
NC
5984 if (symbol_get_tc (sym)->symbol_class == -1)
5985 symbol_get_tc (sym)->symbol_class = XMC_XO;
252b5132
RH
5986 }
5987 else if (! S_IS_DEFINED (sym))
5988 {
5989 /* This is an external symbol. */
5990 a->x_csect.x_scnlen.l = 0;
5991 a->x_csect.x_smtyp = XTY_ER;
5992 }
4a403be0 5993 else if (ppc_is_toc_sym (sym))
252b5132
RH
5994 {
5995 symbolS *next;
5996
5997 /* This is a TOC definition. x_scnlen is the size of the
5998 TOC entry. */
5999 next = symbol_next (sym);
96d56e9f 6000 while (symbol_get_tc (next)->symbol_class == XMC_TC0)
252b5132
RH
6001 next = symbol_next (next);
6002 if (next == (symbolS *) NULL
4a403be0 6003 || (!ppc_is_toc_sym (next)))
252b5132
RH
6004 {
6005 if (ppc_after_toc_frag == (fragS *) NULL)
fd361982 6006 a->x_csect.x_scnlen.l = (bfd_section_size (data_section)
252b5132
RH
6007 - S_GET_VALUE (sym));
6008 else
6009 a->x_csect.x_scnlen.l = (ppc_after_toc_frag->fr_address
6010 - S_GET_VALUE (sym));
6011 }
6012 else
6013 {
6386f3a7 6014 resolve_symbol_value (next);
252b5132
RH
6015 a->x_csect.x_scnlen.l = (S_GET_VALUE (next)
6016 - S_GET_VALUE (sym));
6017 }
6018 a->x_csect.x_smtyp = (2 << 3) | XTY_SD;
6019 }
6020 else
6021 {
6022 symbolS *csect;
6023
6024 /* This is a normal symbol definition. x_scnlen is the
6025 symbol index of the containing csect. */
6026 if (S_GET_SEGMENT (sym) == text_section)
1b2cb8e2 6027 csect = ppc_xcoff_text_section.csects;
252b5132 6028 else if (S_GET_SEGMENT (sym) == data_section)
1b2cb8e2
CC
6029 csect = ppc_xcoff_data_section.csects;
6030 else if (S_GET_SEGMENT (sym) == ppc_xcoff_tdata_section.segment)
6031 csect = ppc_xcoff_tdata_section.csects;
252b5132
RH
6032 else
6033 abort ();
6034
6035 /* Skip the initial dummy symbol. */
809ffe0d 6036 csect = symbol_get_tc (csect)->next;
252b5132
RH
6037
6038 if (csect == (symbolS *) NULL)
6039 {
6040 as_warn (_("warning: symbol %s has no csect"), S_GET_NAME (sym));
6041 a->x_csect.x_scnlen.l = 0;
6042 }
6043 else
6044 {
809ffe0d 6045 while (symbol_get_tc (csect)->next != (symbolS *) NULL)
252b5132 6046 {
6386f3a7 6047 resolve_symbol_value (symbol_get_tc (csect)->next);
809ffe0d
ILT
6048 if (S_GET_VALUE (symbol_get_tc (csect)->next)
6049 > S_GET_VALUE (sym))
252b5132 6050 break;
809ffe0d 6051 csect = symbol_get_tc (csect)->next;
252b5132
RH
6052 }
6053
809ffe0d
ILT
6054 a->x_csect.x_scnlen.p =
6055 coffsymbol (symbol_get_bfdsym (csect))->native;
6056 coffsymbol (symbol_get_bfdsym (sym))->native[i + 1].fix_scnlen =
6057 1;
252b5132
RH
6058 }
6059 a->x_csect.x_smtyp = XTY_LD;
6060 }
81d4177b 6061
252b5132
RH
6062 a->x_csect.x_parmhash = 0;
6063 a->x_csect.x_snhash = 0;
96d56e9f 6064 if (symbol_get_tc (sym)->symbol_class == -1)
252b5132
RH
6065 a->x_csect.x_smclas = XMC_PR;
6066 else
96d56e9f 6067 a->x_csect.x_smclas = symbol_get_tc (sym)->symbol_class;
252b5132
RH
6068 a->x_csect.x_stab = 0;
6069 a->x_csect.x_snstab = 0;
6070
6071 /* Don't let the COFF backend resort these symbols. */
809ffe0d 6072 symbol_get_bfdsym (sym)->flags |= BSF_NOT_AT_END;
252b5132
RH
6073 }
6074 else if (S_GET_STORAGE_CLASS (sym) == C_BSTAT)
6075 {
6076 /* We want the value to be the symbol index of the referenced
6077 csect symbol. BFD will do that for us if we set the right
6078 flags. */
b782de16
AM
6079 asymbol *bsym = symbol_get_bfdsym (symbol_get_tc (sym)->within);
6080 combined_entry_type *c = coffsymbol (bsym)->native;
6081
6082 S_SET_VALUE (sym, (valueT) (size_t) c);
809ffe0d 6083 coffsymbol (symbol_get_bfdsym (sym))->native->fix_value = 1;
252b5132
RH
6084 }
6085 else if (S_GET_STORAGE_CLASS (sym) == C_STSYM)
6086 {
6087 symbolS *block;
c734e7e3 6088 valueT base;
252b5132 6089
809ffe0d 6090 block = symbol_get_tc (sym)->within;
c734e7e3
TG
6091 if (block)
6092 {
6093 /* The value is the offset from the enclosing csect. */
6094 symbolS *csect;
6095
6096 csect = symbol_get_tc (block)->within;
6097 resolve_symbol_value (csect);
6098 base = S_GET_VALUE (csect);
6099 }
6100 else
6101 base = 0;
6102
6103 S_SET_VALUE (sym, S_GET_VALUE (sym) - base);
252b5132
RH
6104 }
6105 else if (S_GET_STORAGE_CLASS (sym) == C_BINCL
6106 || S_GET_STORAGE_CLASS (sym) == C_EINCL)
6107 {
6108 /* We want the value to be a file offset into the line numbers.
99a814a1
AM
6109 BFD will do that for us if we set the right flags. We have
6110 already set the value correctly. */
809ffe0d 6111 coffsymbol (symbol_get_bfdsym (sym))->native->fix_line = 1;
252b5132
RH
6112 }
6113
6114 return 0;
6115}
6116
6117/* Adjust the symbol table. This creates csect symbols for all
6118 absolute symbols. */
6119
6120void
98027b10 6121ppc_adjust_symtab (void)
252b5132
RH
6122{
6123 symbolS *sym;
6124
6125 if (! ppc_saw_abs)
6126 return;
6127
6128 for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
6129 {
6130 symbolS *csect;
6131 int i;
6132 union internal_auxent *a;
6133
6134 if (S_GET_SEGMENT (sym) != absolute_section)
6135 continue;
6136
6137 csect = symbol_create (".abs[XO]", absolute_section,
e01e1cee 6138 &zero_address_frag, S_GET_VALUE (sym));
809ffe0d 6139 symbol_get_bfdsym (csect)->value = S_GET_VALUE (sym);
252b5132
RH
6140 S_SET_STORAGE_CLASS (csect, C_HIDEXT);
6141 i = S_GET_NUMBER_AUXILIARY (csect);
6142 S_SET_NUMBER_AUXILIARY (csect, i + 1);
809ffe0d 6143 a = &coffsymbol (symbol_get_bfdsym (csect))->native[i + 1].u.auxent;
252b5132
RH
6144 a->x_csect.x_scnlen.l = 0;
6145 a->x_csect.x_smtyp = XTY_SD;
6146 a->x_csect.x_parmhash = 0;
6147 a->x_csect.x_snhash = 0;
6148 a->x_csect.x_smclas = XMC_XO;
6149 a->x_csect.x_stab = 0;
6150 a->x_csect.x_snstab = 0;
6151
6152 symbol_insert (csect, sym, &symbol_rootP, &symbol_lastP);
6153
6154 i = S_GET_NUMBER_AUXILIARY (sym);
809ffe0d
ILT
6155 a = &coffsymbol (symbol_get_bfdsym (sym))->native[i].u.auxent;
6156 a->x_csect.x_scnlen.p = coffsymbol (symbol_get_bfdsym (csect))->native;
6157 coffsymbol (symbol_get_bfdsym (sym))->native[i].fix_scnlen = 1;
252b5132
RH
6158 }
6159
5b7c81bd 6160 ppc_saw_abs = false;
252b5132
RH
6161}
6162
6163/* Set the VMA for a section. This is called on all the sections in
6164 turn. */
6165
6166void
98027b10 6167ppc_frob_section (asection *sec)
252b5132 6168{
931e13a6 6169 static bfd_vma vma = 0;
252b5132 6170
85645aed 6171 /* Dwarf sections start at 0. */
fd361982 6172 if (bfd_section_flags (sec) & SEC_DEBUGGING)
85645aed
TG
6173 return;
6174
931e13a6 6175 vma = md_section_align (sec, vma);
fd361982
AM
6176 bfd_set_section_vma (sec, vma);
6177 vma += bfd_section_size (sec);
252b5132
RH
6178}
6179
6180#endif /* OBJ_XCOFF */
6181\f
6d4af3c2 6182const char *
98027b10 6183md_atof (int type, char *litp, int *sizep)
252b5132 6184{
499ac353 6185 return ieee_md_atof (type, litp, sizep, target_big_endian);
252b5132
RH
6186}
6187
6188/* Write a value out to the object file, using the appropriate
6189 endianness. */
6190
6191void
98027b10 6192md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
6193{
6194 if (target_big_endian)
6195 number_to_chars_bigendian (buf, val, n);
6196 else
6197 number_to_chars_littleendian (buf, val, n);
6198}
6199
6200/* Align a section (I don't know why this is machine dependent). */
6201
6202valueT
3aeeedbb 6203md_section_align (asection *seg ATTRIBUTE_UNUSED, valueT addr)
252b5132 6204{
3aeeedbb
AM
6205#ifdef OBJ_ELF
6206 return addr;
6207#else
fd361982 6208 int align = bfd_section_alignment (seg);
252b5132 6209
8d3842cd 6210 return ((addr + (1 << align) - 1) & -(1 << align));
3aeeedbb 6211#endif
252b5132
RH
6212}
6213
6214/* We don't have any form of relaxing. */
6215
6216int
98027b10
AM
6217md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
6218 asection *seg ATTRIBUTE_UNUSED)
252b5132
RH
6219{
6220 abort ();
6221 return 0;
6222}
6223
6224/* Convert a machine dependent frag. We never generate these. */
6225
6226void
98027b10
AM
6227md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
6228 asection *sec ATTRIBUTE_UNUSED,
6229 fragS *fragp ATTRIBUTE_UNUSED)
252b5132
RH
6230{
6231 abort ();
6232}
6233
6234/* We have no need to default values of symbols. */
6235
252b5132 6236symbolS *
98027b10 6237md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
252b5132
RH
6238{
6239 return 0;
6240}
6241\f
6242/* Functions concerning relocs. */
6243
6244/* The location from which a PC relative jump should be calculated,
6245 given a PC relative reloc. */
6246
6247long
98027b10 6248md_pcrel_from_section (fixS *fixp, segT sec ATTRIBUTE_UNUSED)
252b5132
RH
6249{
6250 return fixp->fx_frag->fr_address + fixp->fx_where;
6251}
6252
6253#ifdef OBJ_XCOFF
6254
6255/* This is called to see whether a fixup should be adjusted to use a
6256 section symbol. We take the opportunity to change a fixup against
6257 a symbol in the TOC subsegment into a reloc against the
6258 corresponding .tc symbol. */
6259
6260int
98027b10 6261ppc_fix_adjustable (fixS *fix)
252b5132 6262{
b782de16
AM
6263 valueT val = resolve_symbol_value (fix->fx_addsy);
6264 segT symseg = S_GET_SEGMENT (fix->fx_addsy);
6265 TC_SYMFIELD_TYPE *tc;
6266
6267 if (symseg == absolute_section)
6268 return 0;
252b5132 6269
85645aed 6270 /* Always adjust symbols in debugging sections. */
fd361982 6271 if (bfd_section_flags (symseg) & SEC_DEBUGGING)
85645aed
TG
6272 return 1;
6273
252b5132 6274 if (ppc_toc_csect != (symbolS *) NULL
252b5132 6275 && fix->fx_addsy != ppc_toc_csect
b782de16 6276 && symseg == data_section
252b5132
RH
6277 && val >= ppc_toc_frag->fr_address
6278 && (ppc_after_toc_frag == (fragS *) NULL
6279 || val < ppc_after_toc_frag->fr_address))
6280 {
6281 symbolS *sy;
6282
6283 for (sy = symbol_next (ppc_toc_csect);
6284 sy != (symbolS *) NULL;
6285 sy = symbol_next (sy))
6286 {
b782de16
AM
6287 TC_SYMFIELD_TYPE *sy_tc = symbol_get_tc (sy);
6288
96d56e9f 6289 if (sy_tc->symbol_class == XMC_TC0)
252b5132 6290 continue;
4a403be0
CC
6291 if (sy_tc->symbol_class != XMC_TC
6292 && sy_tc->symbol_class != XMC_TE)
252b5132 6293 break;
b782de16 6294 if (val == resolve_symbol_value (sy))
252b5132
RH
6295 {
6296 fix->fx_addsy = sy;
6297 fix->fx_addnumber = val - ppc_toc_frag->fr_address;
6298 return 0;
6299 }
6300 }
6301
6302 as_bad_where (fix->fx_file, fix->fx_line,
6303 _("symbol in .toc does not match any .tc"));
6304 }
6305
6306 /* Possibly adjust the reloc to be against the csect. */
b782de16
AM
6307 tc = symbol_get_tc (fix->fx_addsy);
6308 if (tc->subseg == 0
96d56e9f
NC
6309 && tc->symbol_class != XMC_TC0
6310 && tc->symbol_class != XMC_TC
4a403be0 6311 && tc->symbol_class != XMC_TE
b782de16 6312 && symseg != bss_section
1b2cb8e2 6313 && symseg != ppc_xcoff_tbss_section.segment
252b5132 6314 /* Don't adjust if this is a reloc in the toc section. */
b782de16 6315 && (symseg != data_section
252b5132
RH
6316 || ppc_toc_csect == NULL
6317 || val < ppc_toc_frag->fr_address
6318 || (ppc_after_toc_frag != NULL
6319 && val >= ppc_after_toc_frag->fr_address)))
6320 {
2fb4b302 6321 symbolS *csect = tc->within;
252b5132 6322
2fb4b302
TG
6323 /* If the symbol was not declared by a label (eg: a section symbol),
6324 use the section instead of the csect. This doesn't happen in
6325 normal AIX assembly code. */
6326 if (csect == NULL)
6327 csect = seg_info (symseg)->sym;
252b5132 6328
2fb4b302
TG
6329 fix->fx_offset += val - symbol_get_frag (csect)->fr_address;
6330 fix->fx_addsy = csect;
252b5132 6331
b782de16 6332 return 0;
252b5132
RH
6333 }
6334
6335 /* Adjust a reloc against a .lcomm symbol to be against the base
6336 .lcomm. */
b782de16 6337 if (symseg == bss_section
252b5132
RH
6338 && ! S_IS_EXTERNAL (fix->fx_addsy))
6339 {
b782de16
AM
6340 symbolS *sy = symbol_get_frag (fix->fx_addsy)->fr_symbol;
6341
6342 fix->fx_offset += val - resolve_symbol_value (sy);
6343 fix->fx_addsy = sy;
252b5132
RH
6344 }
6345
6346 return 0;
6347}
6348
6349/* A reloc from one csect to another must be kept. The assembler
6350 will, of course, keep relocs between sections, and it will keep
6351 absolute relocs, but we need to force it to keep PC relative relocs
6352 between two csects in the same section. */
6353
6354int
98027b10 6355ppc_force_relocation (fixS *fix)
252b5132
RH
6356{
6357 /* At this point fix->fx_addsy should already have been converted to
6358 a csect symbol. If the csect does not include the fragment, then
6359 we need to force the relocation. */
6360 if (fix->fx_pcrel
6361 && fix->fx_addsy != NULL
809ffe0d
ILT
6362 && symbol_get_tc (fix->fx_addsy)->subseg != 0
6363 && ((symbol_get_frag (fix->fx_addsy)->fr_address
6364 > fix->fx_frag->fr_address)
6365 || (symbol_get_tc (fix->fx_addsy)->next != NULL
6366 && (symbol_get_frag (symbol_get_tc (fix->fx_addsy)->next)->fr_address
252b5132
RH
6367 <= fix->fx_frag->fr_address))))
6368 return 1;
6369
ae6063d4 6370 return generic_force_reloc (fix);
252b5132 6371}
252b5132
RH
6372#endif /* OBJ_XCOFF */
6373
0baf16f2 6374#ifdef OBJ_ELF
a161fe53
AM
6375/* If this function returns non-zero, it guarantees that a relocation
6376 will be emitted for a fixup. */
6377
6378int
98027b10 6379ppc_force_relocation (fixS *fix)
a161fe53
AM
6380{
6381 /* Branch prediction relocations must force a relocation, as must
6382 the vtable description relocs. */
6383 switch (fix->fx_r_type)
6384 {
6385 case BFD_RELOC_PPC_B16_BRTAKEN:
6386 case BFD_RELOC_PPC_B16_BRNTAKEN:
6387 case BFD_RELOC_PPC_BA16_BRTAKEN:
6388 case BFD_RELOC_PPC_BA16_BRNTAKEN:
c744ecf2 6389 case BFD_RELOC_24_PLT_PCREL:
a161fe53 6390 case BFD_RELOC_PPC64_TOC:
a161fe53 6391 return 1;
6911b7dc
AM
6392 case BFD_RELOC_PPC_B26:
6393 case BFD_RELOC_PPC_BA26:
6394 case BFD_RELOC_PPC_B16:
6395 case BFD_RELOC_PPC_BA16:
05d0e962 6396 case BFD_RELOC_PPC64_REL24_NOTOC:
6911b7dc
AM
6397 /* All branch fixups targeting a localentry symbol must
6398 force a relocation. */
6399 if (fix->fx_addsy)
6400 {
6401 asymbol *bfdsym = symbol_get_bfdsym (fix->fx_addsy);
c1229f84 6402 elf_symbol_type *elfsym = elf_symbol_from (bfdsym);
6911b7dc
AM
6403 gas_assert (elfsym);
6404 if ((STO_PPC64_LOCAL_MASK & elfsym->internal_elf_sym.st_other) != 0)
6405 return 1;
6406 }
6407 break;
a161fe53
AM
6408 default:
6409 break;
6410 }
6411
cdba85ec 6412 if (fix->fx_r_type >= BFD_RELOC_PPC_TLS
c213164a 6413 && fix->fx_r_type <= BFD_RELOC_PPC64_TLS_PCREL)
cdba85ec
AM
6414 return 1;
6415
ae6063d4 6416 return generic_force_reloc (fix);
a161fe53
AM
6417}
6418
0baf16f2 6419int
98027b10 6420ppc_fix_adjustable (fixS *fix)
252b5132 6421{
6911b7dc
AM
6422 switch (fix->fx_r_type)
6423 {
6424 /* All branch fixups targeting a localentry symbol must
6425 continue using the symbol. */
6426 case BFD_RELOC_PPC_B26:
6427 case BFD_RELOC_PPC_BA26:
6428 case BFD_RELOC_PPC_B16:
6429 case BFD_RELOC_PPC_BA16:
6430 case BFD_RELOC_PPC_B16_BRTAKEN:
6431 case BFD_RELOC_PPC_B16_BRNTAKEN:
6432 case BFD_RELOC_PPC_BA16_BRTAKEN:
6433 case BFD_RELOC_PPC_BA16_BRNTAKEN:
05d0e962 6434 case BFD_RELOC_PPC64_REL24_NOTOC:
6911b7dc
AM
6435 if (fix->fx_addsy)
6436 {
6437 asymbol *bfdsym = symbol_get_bfdsym (fix->fx_addsy);
c1229f84 6438 elf_symbol_type *elfsym = elf_symbol_from (bfdsym);
6911b7dc
AM
6439 gas_assert (elfsym);
6440 if ((STO_PPC64_LOCAL_MASK & elfsym->internal_elf_sym.st_other) != 0)
6441 return 0;
6442 }
6443 break;
6444 default:
6445 break;
6446 }
6447
0baf16f2
AM
6448 return (fix->fx_r_type != BFD_RELOC_16_GOTOFF
6449 && fix->fx_r_type != BFD_RELOC_LO16_GOTOFF
6450 && fix->fx_r_type != BFD_RELOC_HI16_GOTOFF
6451 && fix->fx_r_type != BFD_RELOC_HI16_S_GOTOFF
cc9edbf3
AM
6452 && fix->fx_r_type != BFD_RELOC_PPC64_GOT16_DS
6453 && fix->fx_r_type != BFD_RELOC_PPC64_GOT16_LO_DS
5663e321 6454 && fix->fx_r_type != BFD_RELOC_PPC64_GOT_PCREL34
334d91b9
AM
6455 && fix->fx_r_type != BFD_RELOC_24_PLT_PCREL
6456 && fix->fx_r_type != BFD_RELOC_32_PLTOFF
6457 && fix->fx_r_type != BFD_RELOC_32_PLT_PCREL
6458 && fix->fx_r_type != BFD_RELOC_LO16_PLTOFF
6459 && fix->fx_r_type != BFD_RELOC_HI16_PLTOFF
6460 && fix->fx_r_type != BFD_RELOC_HI16_S_PLTOFF
6461 && fix->fx_r_type != BFD_RELOC_64_PLTOFF
6462 && fix->fx_r_type != BFD_RELOC_64_PLT_PCREL
6463 && fix->fx_r_type != BFD_RELOC_PPC64_PLT16_LO_DS
5663e321 6464 && fix->fx_r_type != BFD_RELOC_PPC64_PLT_PCREL34
334d91b9
AM
6465 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16
6466 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_LO
6467 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_HI
6468 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_HA
6469 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_DS
6470 && fix->fx_r_type != BFD_RELOC_PPC64_PLTGOT16_LO_DS
0baf16f2 6471 && fix->fx_r_type != BFD_RELOC_GPREL16
334d91b9
AM
6472 && fix->fx_r_type != BFD_RELOC_PPC_VLE_SDAREL_LO16A
6473 && fix->fx_r_type != BFD_RELOC_PPC_VLE_SDAREL_HI16A
6474 && fix->fx_r_type != BFD_RELOC_PPC_VLE_SDAREL_HA16A
0baf16f2
AM
6475 && fix->fx_r_type != BFD_RELOC_VTABLE_INHERIT
6476 && fix->fx_r_type != BFD_RELOC_VTABLE_ENTRY
cdba85ec 6477 && !(fix->fx_r_type >= BFD_RELOC_PPC_TLS
c213164a 6478 && fix->fx_r_type <= BFD_RELOC_PPC64_TLS_PCREL));
252b5132 6479}
0baf16f2 6480#endif
252b5132 6481
b9c361e0
JL
6482void
6483ppc_frag_check (struct frag *fragP)
6484{
a9479dc0
AM
6485 if ((fragP->fr_address & fragP->insn_addr) != 0)
6486 as_bad_where (fragP->fr_file, fragP->fr_line,
6487 _("instruction address is not a multiple of %d"),
6488 fragP->insn_addr + 1);
b9c361e0
JL
6489}
6490
22f72c48
AM
6491/* rs_align_code frag handling. */
6492
6493enum ppc_nop_encoding_for_rs_align_code
6494{
6495 PPC_NOP_VANILLA,
6496 PPC_NOP_VLE,
6497 PPC_NOP_GROUP_P6,
6498 PPC_NOP_GROUP_P7
6499};
6500
6501unsigned int
6502ppc_nop_select (void)
6503{
6504 if ((ppc_cpu & PPC_OPCODE_VLE) != 0)
6505 return PPC_NOP_VLE;
6506 if ((ppc_cpu & (PPC_OPCODE_POWER9 | PPC_OPCODE_E500MC)) == 0)
6507 {
6508 if ((ppc_cpu & PPC_OPCODE_POWER7) != 0)
6509 return PPC_NOP_GROUP_P7;
6510 if ((ppc_cpu & PPC_OPCODE_POWER6) != 0)
6511 return PPC_NOP_GROUP_P6;
6512 }
6513 return PPC_NOP_VANILLA;
6514}
3aeeedbb
AM
6515
6516void
6517ppc_handle_align (struct frag *fragP)
6518{
6519 valueT count = (fragP->fr_next->fr_address
6520 - (fragP->fr_address + fragP->fr_fix));
22f72c48
AM
6521 char *dest = fragP->fr_literal + fragP->fr_fix;
6522 enum ppc_nop_encoding_for_rs_align_code nop_select = *dest & 0xff;
6523
6524 /* Pad with zeros if not inserting a whole number of instructions.
6525 We could pad with zeros up to an instruction boundary then follow
6526 with nops but odd counts indicate data in an executable section
6527 so padding with zeros is most appropriate. */
6528 if (count == 0
de626965 6529 || (nop_select == PPC_NOP_VLE ? (count & 1) != 0 : (count & 3) != 0))
22f72c48
AM
6530 {
6531 *dest = 0;
6532 return;
6533 }
3aeeedbb 6534
22f72c48 6535 if (nop_select == PPC_NOP_VLE)
b9c361e0 6536 {
b9c361e0
JL
6537
6538 fragP->fr_var = 2;
6539 md_number_to_chars (dest, 0x4400, 2);
6540 }
22f72c48 6541 else
3aeeedbb 6542 {
3aeeedbb 6543 fragP->fr_var = 4;
cef4f754
AM
6544
6545 if (count > 4 * nop_limit && count < 0x2000000)
6546 {
6547 struct frag *rest;
6548
6549 /* Make a branch, then follow with nops. Insert another
6550 frag to handle the nops. */
6551 md_number_to_chars (dest, 0x48000000 + count, 4);
6552 count -= 4;
6553 if (count == 0)
6554 return;
6555
6556 rest = xmalloc (SIZEOF_STRUCT_FRAG + 4);
6557 memcpy (rest, fragP, SIZEOF_STRUCT_FRAG);
6558 fragP->fr_next = rest;
6559 fragP = rest;
6560 rest->fr_address += rest->fr_fix + 4;
6561 rest->fr_fix = 0;
6562 /* If we leave the next frag as rs_align_code we'll come here
6563 again, resulting in a bunch of branches rather than a
6564 branch followed by nops. */
6565 rest->fr_type = rs_align;
6566 dest = rest->fr_literal;
6567 }
6568
3aeeedbb
AM
6569 md_number_to_chars (dest, 0x60000000, 4);
6570
22f72c48 6571 if (nop_select >= PPC_NOP_GROUP_P6)
3aeeedbb 6572 {
3fea0c3b
AM
6573 /* For power6, power7, and power8, we want the last nop to
6574 be a group terminating one. Do this by inserting an
6575 rs_fill frag immediately after this one, with its address
6576 set to the last nop location. This will automatically
6577 reduce the number of nops in the current frag by one. */
3aeeedbb
AM
6578 if (count > 4)
6579 {
6580 struct frag *group_nop = xmalloc (SIZEOF_STRUCT_FRAG + 4);
6581
6582 memcpy (group_nop, fragP, SIZEOF_STRUCT_FRAG);
6583 group_nop->fr_address = group_nop->fr_next->fr_address - 4;
6584 group_nop->fr_fix = 0;
6585 group_nop->fr_offset = 1;
6586 group_nop->fr_type = rs_fill;
6587 fragP->fr_next = group_nop;
6588 dest = group_nop->fr_literal;
6589 }
6590
22f72c48 6591 if (nop_select == PPC_NOP_GROUP_P6)
42240548
PB
6592 /* power6 group terminating nop: "ori 1,1,0". */
6593 md_number_to_chars (dest, 0x60210000, 4);
22f72c48
AM
6594 else
6595 /* power7/power8 group terminating nop: "ori 2,2,0". */
6596 md_number_to_chars (dest, 0x60420000, 4);
3aeeedbb
AM
6597 }
6598 }
6599}
6600
252b5132 6601/* Apply a fixup to the object code. This is called for all the
3b8b57a9 6602 fixups we generated by the calls to fix_new_exp, above. */
252b5132 6603
94f592af 6604void
62ebcb5c 6605md_apply_fix (fixS *fixP, valueT *valP, segT seg)
252b5132 6606{
94f592af 6607 valueT value = * valP;
5656a981
AM
6608 offsetT fieldval;
6609 const struct powerpc_operand *operand;
252b5132
RH
6610
6611#ifdef OBJ_ELF
94f592af 6612 if (fixP->fx_addsy != NULL)
252b5132 6613 {
a161fe53 6614 /* Hack around bfd_install_relocation brain damage. */
94f592af
NC
6615 if (fixP->fx_pcrel)
6616 value += fixP->fx_frag->fr_address + fixP->fx_where;
a680de9a
PB
6617
6618 if (fixP->fx_addsy == abs_section_sym)
6619 fixP->fx_done = 1;
252b5132
RH
6620 }
6621 else
94f592af 6622 fixP->fx_done = 1;
252b5132 6623#else
a161fe53 6624 /* FIXME FIXME FIXME: The value we are passed in *valP includes
7be1c489
AM
6625 the symbol values. If we are doing this relocation the code in
6626 write.c is going to call bfd_install_relocation, which is also
6627 going to use the symbol value. That means that if the reloc is
6628 fully resolved we want to use *valP since bfd_install_relocation is
6629 not being used.
9f0eb232
RS
6630 However, if the reloc is not fully resolved we do not want to
6631 use *valP, and must use fx_offset instead. If the relocation
6632 is PC-relative, we then need to re-apply md_pcrel_from_section
6633 to this new relocation value. */
94f592af
NC
6634 if (fixP->fx_addsy == (symbolS *) NULL)
6635 fixP->fx_done = 1;
6636
252b5132 6637 else
9f0eb232
RS
6638 {
6639 value = fixP->fx_offset;
6640 if (fixP->fx_pcrel)
6641 value -= md_pcrel_from_section (fixP, seg);
6642 }
a161fe53
AM
6643#endif
6644
7ba71655
AM
6645 /* We are only able to convert some relocs to pc-relative. */
6646 if (fixP->fx_pcrel)
6647 {
6648 switch (fixP->fx_r_type)
6649 {
4a969973
AM
6650 case BFD_RELOC_64:
6651 fixP->fx_r_type = BFD_RELOC_64_PCREL;
6652 break;
6653
6654 case BFD_RELOC_32:
6655 fixP->fx_r_type = BFD_RELOC_32_PCREL;
6656 break;
6657
6658 case BFD_RELOC_16:
6659 fixP->fx_r_type = BFD_RELOC_16_PCREL;
6660 break;
6661
7ba71655
AM
6662 case BFD_RELOC_LO16:
6663 fixP->fx_r_type = BFD_RELOC_LO16_PCREL;
6664 break;
6665
6666 case BFD_RELOC_HI16:
6667 fixP->fx_r_type = BFD_RELOC_HI16_PCREL;
6668 break;
6669
6670 case BFD_RELOC_HI16_S:
6671 fixP->fx_r_type = BFD_RELOC_HI16_S_PCREL;
6672 break;
6673
4a969973
AM
6674 case BFD_RELOC_PPC64_ADDR16_HIGH:
6675 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGH;
7ba71655
AM
6676 break;
6677
4a969973
AM
6678 case BFD_RELOC_PPC64_ADDR16_HIGHA:
6679 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHA;
7ba71655
AM
6680 break;
6681
4a969973
AM
6682 case BFD_RELOC_PPC64_HIGHER:
6683 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHER;
6684 break;
6685
6686 case BFD_RELOC_PPC64_HIGHER_S:
6687 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHERA;
6688 break;
6689
6690 case BFD_RELOC_PPC64_HIGHEST:
6691 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHEST;
6692 break;
6693
6694 case BFD_RELOC_PPC64_HIGHEST_S:
6695 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHESTA;
7ba71655
AM
6696 break;
6697
5663e321
AM
6698 case BFD_RELOC_PPC64_ADDR16_HIGHER34:
6699 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHER34;
6700 break;
6701
6702 case BFD_RELOC_PPC64_ADDR16_HIGHERA34:
6703 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHERA34;
6704 break;
6705
6706 case BFD_RELOC_PPC64_ADDR16_HIGHEST34:
6707 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHEST34;
6708 break;
6709
6710 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34:
6711 fixP->fx_r_type = BFD_RELOC_PPC64_REL16_HIGHESTA34;
6712 break;
6713
7ba71655
AM
6714 case BFD_RELOC_PPC_16DX_HA:
6715 fixP->fx_r_type = BFD_RELOC_PPC_REL16DX_HA;
6716 break;
6717
5663e321
AM
6718 case BFD_RELOC_PPC64_D34:
6719 fixP->fx_r_type = BFD_RELOC_PPC64_PCREL34;
6720 break;
6721
6722 case BFD_RELOC_PPC64_D28:
6723 fixP->fx_r_type = BFD_RELOC_PPC64_PCREL28;
6724 break;
6725
7ba71655
AM
6726 default:
6727 break;
6728 }
6729 }
6730 else if (!fixP->fx_done
6731 && fixP->fx_r_type == BFD_RELOC_PPC_16DX_HA)
252b5132 6732 {
7ba71655
AM
6733 /* addpcis is relative to next insn address. */
6734 value -= 4;
6735 fixP->fx_r_type = BFD_RELOC_PPC_REL16DX_HA;
6736 fixP->fx_pcrel = 1;
252b5132 6737 }
252b5132 6738
5656a981 6739 operand = NULL;
3b8b57a9 6740 if (fixP->fx_pcrel_adjust != 0)
252b5132 6741 {
5656a981 6742 /* This is a fixup on an instruction. */
3b8b57a9 6743 int opindex = fixP->fx_pcrel_adjust & 0xff;
252b5132 6744
5656a981 6745 operand = &powerpc_operands[opindex];
252b5132 6746#ifdef OBJ_XCOFF
0baf16f2
AM
6747 /* An instruction like `lwz 9,sym(30)' when `sym' is not a TOC symbol
6748 does not generate a reloc. It uses the offset of `sym' within its
6749 csect. Other usages, such as `.long sym', generate relocs. This
6750 is the documented behaviour of non-TOC symbols. */
252b5132 6751 if ((operand->flags & PPC_OPERAND_PARENS) != 0
b84bf58a 6752 && (operand->bitm & 0xfff0) == 0xfff0
252b5132 6753 && operand->shift == 0
2b3c4602 6754 && (operand->insert == NULL || ppc_obj64)
94f592af
NC
6755 && fixP->fx_addsy != NULL
6756 && symbol_get_tc (fixP->fx_addsy)->subseg != 0
4a403be0 6757 && !ppc_is_toc_sym (fixP->fx_addsy)
94f592af 6758 && S_GET_SEGMENT (fixP->fx_addsy) != bss_section)
252b5132 6759 {
94f592af
NC
6760 value = fixP->fx_offset;
6761 fixP->fx_done = 1;
252b5132 6762 }
ac21e7da
TG
6763
6764 /* During parsing of instructions, a TOC16 reloc is generated for
6765 instructions such as 'lwz RT,SYM(RB)' if SYM is a symbol defined
6766 in the toc. But at parse time, SYM may be not yet defined, so
6767 check again here. */
6768 if (fixP->fx_r_type == BFD_RELOC_16
6769 && fixP->fx_addsy != NULL
6770 && ppc_is_toc_sym (fixP->fx_addsy))
4a403be0 6771 fixP->fx_r_type = BFD_RELOC_PPC_TOC16;
252b5132 6772#endif
5656a981
AM
6773 }
6774
6775 /* Calculate value to be stored in field. */
6776 fieldval = value;
6777 switch (fixP->fx_r_type)
6778 {
1ec2d25e 6779#ifdef OBJ_ELF
5656a981
AM
6780 case BFD_RELOC_PPC64_ADDR16_LO_DS:
6781 case BFD_RELOC_PPC_VLE_LO16A:
6782 case BFD_RELOC_PPC_VLE_LO16D:
1ec2d25e 6783#endif
5656a981
AM
6784 case BFD_RELOC_LO16:
6785 case BFD_RELOC_LO16_PCREL:
6786 fieldval = value & 0xffff;
6787 sign_extend_16:
6788 if (operand != NULL && (operand->flags & PPC_OPERAND_SIGNED) != 0)
f9c6b907 6789 fieldval = SEX16 (fieldval);
5656a981
AM
6790 fixP->fx_no_overflow = 1;
6791 break;
3c9d25f4 6792
f9c6b907
AM
6793 case BFD_RELOC_HI16:
6794 case BFD_RELOC_HI16_PCREL:
5656a981 6795#ifdef OBJ_ELF
f9c6b907
AM
6796 if (REPORT_OVERFLOW_HI && ppc_obj64)
6797 {
6798 fieldval = value >> 16;
6799 if (operand != NULL && (operand->flags & PPC_OPERAND_SIGNED) != 0)
6800 {
6801 valueT sign = (((valueT) -1 >> 16) + 1) >> 1;
6802 fieldval = ((valueT) fieldval ^ sign) - sign;
6803 }
6804 break;
6805 }
2b0f3761 6806 /* Fallthru */
f9c6b907 6807
5656a981
AM
6808 case BFD_RELOC_PPC_VLE_HI16A:
6809 case BFD_RELOC_PPC_VLE_HI16D:
f9c6b907 6810 case BFD_RELOC_PPC64_ADDR16_HIGH:
5656a981 6811#endif
5656a981
AM
6812 fieldval = PPC_HI (value);
6813 goto sign_extend_16;
0baf16f2 6814
f9c6b907
AM
6815 case BFD_RELOC_HI16_S:
6816 case BFD_RELOC_HI16_S_PCREL:
7ba71655 6817 case BFD_RELOC_PPC_16DX_HA:
a680de9a 6818 case BFD_RELOC_PPC_REL16DX_HA:
5656a981 6819#ifdef OBJ_ELF
f9c6b907
AM
6820 if (REPORT_OVERFLOW_HI && ppc_obj64)
6821 {
6822 fieldval = (value + 0x8000) >> 16;
6823 if (operand != NULL && (operand->flags & PPC_OPERAND_SIGNED) != 0)
6824 {
6825 valueT sign = (((valueT) -1 >> 16) + 1) >> 1;
6826 fieldval = ((valueT) fieldval ^ sign) - sign;
6827 }
6828 break;
6829 }
2b0f3761 6830 /* Fallthru */
f9c6b907 6831
5656a981
AM
6832 case BFD_RELOC_PPC_VLE_HA16A:
6833 case BFD_RELOC_PPC_VLE_HA16D:
f9c6b907 6834 case BFD_RELOC_PPC64_ADDR16_HIGHA:
5656a981 6835#endif
5656a981
AM
6836 fieldval = PPC_HA (value);
6837 goto sign_extend_16;
0baf16f2 6838
3b8b57a9 6839#ifdef OBJ_ELF
5656a981
AM
6840 case BFD_RELOC_PPC64_HIGHER:
6841 fieldval = PPC_HIGHER (value);
6842 goto sign_extend_16;
252b5132 6843
5656a981
AM
6844 case BFD_RELOC_PPC64_HIGHER_S:
6845 fieldval = PPC_HIGHERA (value);
6846 goto sign_extend_16;
0baf16f2 6847
5656a981
AM
6848 case BFD_RELOC_PPC64_HIGHEST:
6849 fieldval = PPC_HIGHEST (value);
6850 goto sign_extend_16;
0baf16f2 6851
5656a981
AM
6852 case BFD_RELOC_PPC64_HIGHEST_S:
6853 fieldval = PPC_HIGHESTA (value);
6854 goto sign_extend_16;
6855#endif
6856
6857 default:
6858 break;
6859 }
6860
6861 if (operand != NULL)
6862 {
6863 /* Handle relocs in an insn. */
5656a981
AM
6864 switch (fixP->fx_r_type)
6865 {
7fa9fcb6 6866#ifdef OBJ_ELF
3b8b57a9
AM
6867 /* The following relocs can't be calculated by the assembler.
6868 Leave the field zero. */
cdba85ec
AM
6869 case BFD_RELOC_PPC_TPREL16:
6870 case BFD_RELOC_PPC_TPREL16_LO:
6871 case BFD_RELOC_PPC_TPREL16_HI:
6872 case BFD_RELOC_PPC_TPREL16_HA:
cdba85ec
AM
6873 case BFD_RELOC_PPC_DTPREL16:
6874 case BFD_RELOC_PPC_DTPREL16_LO:
6875 case BFD_RELOC_PPC_DTPREL16_HI:
6876 case BFD_RELOC_PPC_DTPREL16_HA:
cdba85ec
AM
6877 case BFD_RELOC_PPC_GOT_TLSGD16:
6878 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
6879 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
6880 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
6881 case BFD_RELOC_PPC_GOT_TLSLD16:
6882 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
6883 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
6884 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
6885 case BFD_RELOC_PPC_GOT_TPREL16:
6886 case BFD_RELOC_PPC_GOT_TPREL16_LO:
6887 case BFD_RELOC_PPC_GOT_TPREL16_HI:
6888 case BFD_RELOC_PPC_GOT_TPREL16_HA:
6889 case BFD_RELOC_PPC_GOT_DTPREL16:
6890 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
6891 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
6892 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
6893 case BFD_RELOC_PPC64_TPREL16_DS:
6894 case BFD_RELOC_PPC64_TPREL16_LO_DS:
f9c6b907
AM
6895 case BFD_RELOC_PPC64_TPREL16_HIGH:
6896 case BFD_RELOC_PPC64_TPREL16_HIGHA:
cdba85ec
AM
6897 case BFD_RELOC_PPC64_TPREL16_HIGHER:
6898 case BFD_RELOC_PPC64_TPREL16_HIGHERA:
6899 case BFD_RELOC_PPC64_TPREL16_HIGHEST:
6900 case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
6901 case BFD_RELOC_PPC64_DTPREL16_HIGH:
6902 case BFD_RELOC_PPC64_DTPREL16_HIGHA:
cdba85ec
AM
6903 case BFD_RELOC_PPC64_DTPREL16_DS:
6904 case BFD_RELOC_PPC64_DTPREL16_LO_DS:
6905 case BFD_RELOC_PPC64_DTPREL16_HIGHER:
6906 case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
6907 case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
6908 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
c213164a
AM
6909 case BFD_RELOC_PPC64_TPREL34:
6910 case BFD_RELOC_PPC64_DTPREL34:
87c69f97
AM
6911 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34:
6912 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34:
6913 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34:
6914 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34:
3b8b57a9 6915 gas_assert (fixP->fx_addsy != NULL);
7c1d0959 6916 S_SET_THREAD_LOCAL (fixP->fx_addsy);
3b8b57a9 6917 fieldval = 0;
cdba85ec 6918 break;
3b8b57a9
AM
6919
6920 /* These also should leave the field zero for the same
6921 reason. Note that older versions of gas wrote values
6922 here. If we want to go back to the old behaviour, then
6923 all _LO and _LO_DS cases will need to be treated like
6924 BFD_RELOC_LO16_PCREL above. Similarly for _HI etc. */
6925 case BFD_RELOC_16_GOTOFF:
6926 case BFD_RELOC_LO16_GOTOFF:
6927 case BFD_RELOC_HI16_GOTOFF:
6928 case BFD_RELOC_HI16_S_GOTOFF:
6929 case BFD_RELOC_LO16_PLTOFF:
6930 case BFD_RELOC_HI16_PLTOFF:
6931 case BFD_RELOC_HI16_S_PLTOFF:
6932 case BFD_RELOC_GPREL16:
6933 case BFD_RELOC_16_BASEREL:
6934 case BFD_RELOC_LO16_BASEREL:
6935 case BFD_RELOC_HI16_BASEREL:
6936 case BFD_RELOC_HI16_S_BASEREL:
6937 case BFD_RELOC_PPC_TOC16:
6938 case BFD_RELOC_PPC64_TOC16_LO:
6939 case BFD_RELOC_PPC64_TOC16_HI:
6940 case BFD_RELOC_PPC64_TOC16_HA:
6941 case BFD_RELOC_PPC64_PLTGOT16:
6942 case BFD_RELOC_PPC64_PLTGOT16_LO:
6943 case BFD_RELOC_PPC64_PLTGOT16_HI:
6944 case BFD_RELOC_PPC64_PLTGOT16_HA:
6945 case BFD_RELOC_PPC64_GOT16_DS:
6946 case BFD_RELOC_PPC64_GOT16_LO_DS:
6947 case BFD_RELOC_PPC64_PLT16_LO_DS:
6948 case BFD_RELOC_PPC64_SECTOFF_DS:
6949 case BFD_RELOC_PPC64_SECTOFF_LO_DS:
6950 case BFD_RELOC_PPC64_TOC16_DS:
6951 case BFD_RELOC_PPC64_TOC16_LO_DS:
6952 case BFD_RELOC_PPC64_PLTGOT16_DS:
6953 case BFD_RELOC_PPC64_PLTGOT16_LO_DS:
6954 case BFD_RELOC_PPC_EMB_NADDR16:
6955 case BFD_RELOC_PPC_EMB_NADDR16_LO:
6956 case BFD_RELOC_PPC_EMB_NADDR16_HI:
6957 case BFD_RELOC_PPC_EMB_NADDR16_HA:
6958 case BFD_RELOC_PPC_EMB_SDAI16:
6959 case BFD_RELOC_PPC_EMB_SDA2I16:
6960 case BFD_RELOC_PPC_EMB_SDA2REL:
252b5132 6961 case BFD_RELOC_PPC_EMB_SDA21:
3b8b57a9
AM
6962 case BFD_RELOC_PPC_EMB_MRKREF:
6963 case BFD_RELOC_PPC_EMB_RELSEC16:
6964 case BFD_RELOC_PPC_EMB_RELST_LO:
6965 case BFD_RELOC_PPC_EMB_RELST_HI:
6966 case BFD_RELOC_PPC_EMB_RELST_HA:
6967 case BFD_RELOC_PPC_EMB_BIT_FLD:
6968 case BFD_RELOC_PPC_EMB_RELSDA:
6969 case BFD_RELOC_PPC_VLE_SDA21:
6970 case BFD_RELOC_PPC_VLE_SDA21_LO:
6971 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
6972 case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
6973 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
6974 case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
6975 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
6976 case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
5663e321
AM
6977 case BFD_RELOC_PPC64_GOT_PCREL34:
6978 case BFD_RELOC_PPC64_PLT_PCREL34:
3b8b57a9 6979 gas_assert (fixP->fx_addsy != NULL);
2b0f3761 6980 /* Fallthru */
3b8b57a9
AM
6981
6982 case BFD_RELOC_PPC_TLS:
6983 case BFD_RELOC_PPC_TLSGD:
6984 case BFD_RELOC_PPC_TLSLD:
c213164a 6985 case BFD_RELOC_PPC64_TLS_PCREL:
3b8b57a9 6986 fieldval = 0;
3b8b57a9 6987 break;
7fa9fcb6
TG
6988#endif
6989
6990#ifdef OBJ_XCOFF
6991 case BFD_RELOC_PPC_B16:
6992 /* Adjust the offset to the instruction boundary. */
6993 fieldval += 2;
6994 break;
6995#endif
252b5132 6996
f728387b
AM
6997 case BFD_RELOC_VTABLE_INHERIT:
6998 case BFD_RELOC_VTABLE_ENTRY:
6999 case BFD_RELOC_PPC_DTPMOD:
7000 case BFD_RELOC_PPC_TPREL:
7001 case BFD_RELOC_PPC_DTPREL:
7002 case BFD_RELOC_PPC_COPY:
7003 case BFD_RELOC_PPC_GLOB_DAT:
7004 case BFD_RELOC_32_PLT_PCREL:
7005 case BFD_RELOC_PPC_EMB_NADDR32:
7006 case BFD_RELOC_PPC64_TOC:
7007 case BFD_RELOC_CTOR:
7008 case BFD_RELOC_32:
7009 case BFD_RELOC_32_PCREL:
7010 case BFD_RELOC_RVA:
7011 case BFD_RELOC_64:
7012 case BFD_RELOC_64_PCREL:
7013 case BFD_RELOC_PPC64_ADDR64_LOCAL:
7014 as_bad_where (fixP->fx_file, fixP->fx_line,
7015 _("%s unsupported as instruction fixup"),
7016 bfd_get_reloc_code_name (fixP->fx_r_type));
7017 fixP->fx_done = 1;
7018 return;
7019
3b8b57a9 7020 default:
252b5132 7021 break;
3b8b57a9 7022 }
252b5132 7023
3b8b57a9
AM
7024#ifdef OBJ_ELF
7025/* powerpc uses RELA style relocs, so if emitting a reloc the field
7026 contents can stay at zero. */
7027#define APPLY_RELOC fixP->fx_done
7028#else
7029#define APPLY_RELOC 1
7030#endif
5663e321
AM
7031 /* We need to call the insert function even when fieldval is
7032 zero if the insert function would translate that zero to a
7033 bit pattern other than all zeros. */
3b8b57a9
AM
7034 if ((fieldval != 0 && APPLY_RELOC) || operand->insert != NULL)
7035 {
5663e321 7036 uint64_t insn;
487b24d8
AM
7037 unsigned char *where;
7038
3b8b57a9
AM
7039 /* Fetch the instruction, insert the fully resolved operand
7040 value, and stuff the instruction back again. */
487b24d8 7041 where = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
3b8b57a9 7042 if (target_big_endian)
31a91399 7043 {
5663e321 7044 if (fixP->fx_size < 4)
487b24d8 7045 insn = bfd_getb16 (where);
5663e321
AM
7046 else
7047 {
7048 insn = bfd_getb32 (where);
7049 if (fixP->fx_size > 4)
7050 insn = insn << 32 | bfd_getb32 (where + 4);
7051 }
31a91399
NC
7052 }
7053 else
3b8b57a9 7054 {
5663e321 7055 if (fixP->fx_size < 4)
487b24d8 7056 insn = bfd_getl16 (where);
5663e321
AM
7057 else
7058 {
7059 insn = bfd_getl32 (where);
7060 if (fixP->fx_size > 4)
7061 insn = insn << 32 | bfd_getl32 (where + 4);
7062 }
3b8b57a9
AM
7063 }
7064 insn = ppc_insert_operand (insn, operand, fieldval,
7065 fixP->tc_fix_data.ppc_cpu,
7066 fixP->fx_file, fixP->fx_line);
7067 if (target_big_endian)
7068 {
5663e321 7069 if (fixP->fx_size < 4)
487b24d8 7070 bfd_putb16 (insn, where);
5663e321
AM
7071 else
7072 {
7073 if (fixP->fx_size > 4)
7074 {
7075 bfd_putb32 (insn, where + 4);
7076 insn >>= 32;
7077 }
7078 bfd_putb32 (insn, where);
7079 }
3b8b57a9
AM
7080 }
7081 else
7082 {
5663e321 7083 if (fixP->fx_size < 4)
487b24d8 7084 bfd_putl16 (insn, where);
5663e321
AM
7085 else
7086 {
7087 if (fixP->fx_size > 4)
7088 {
7089 bfd_putl32 (insn, where + 4);
7090 insn >>= 32;
7091 }
7092 bfd_putl32 (insn, where);
7093 }
3b8b57a9
AM
7094 }
7095 }
7096
7097 if (fixP->fx_done)
7098 /* Nothing else to do here. */
7099 return;
7100
7101 gas_assert (fixP->fx_addsy != NULL);
62ebcb5c 7102 if (fixP->fx_r_type == BFD_RELOC_NONE)
3b8b57a9 7103 {
3b4dbbbf 7104 const char *sfile;
3b8b57a9
AM
7105 unsigned int sline;
7106
7107 /* Use expr_symbol_where to see if this is an expression
7108 symbol. */
7109 if (expr_symbol_where (fixP->fx_addsy, &sfile, &sline))
7110 as_bad_where (fixP->fx_file, fixP->fx_line,
7111 _("unresolved expression that must be resolved"));
7112 else
7113 as_bad_where (fixP->fx_file, fixP->fx_line,
7114 _("unsupported relocation against %s"),
7115 S_GET_NAME (fixP->fx_addsy));
7116 fixP->fx_done = 1;
7117 return;
7118 }
7119 }
7120 else
7121 {
7122 /* Handle relocs in data. */
7123 switch (fixP->fx_r_type)
7124 {
252b5132 7125 case BFD_RELOC_VTABLE_INHERIT:
94f592af
NC
7126 if (fixP->fx_addsy
7127 && !S_IS_DEFINED (fixP->fx_addsy)
7128 && !S_IS_WEAK (fixP->fx_addsy))
7129 S_SET_WEAK (fixP->fx_addsy);
2b0f3761 7130 /* Fallthru */
252b5132
RH
7131
7132 case BFD_RELOC_VTABLE_ENTRY:
94f592af 7133 fixP->fx_done = 0;
252b5132
RH
7134 break;
7135
0baf16f2 7136#ifdef OBJ_ELF
3b8b57a9
AM
7137 /* These can appear with @l etc. in data. */
7138 case BFD_RELOC_LO16:
3b8b57a9 7139 case BFD_RELOC_LO16_PCREL:
3b8b57a9 7140 case BFD_RELOC_HI16:
3b8b57a9 7141 case BFD_RELOC_HI16_PCREL:
3b8b57a9 7142 case BFD_RELOC_HI16_S:
3b8b57a9 7143 case BFD_RELOC_HI16_S_PCREL:
3b8b57a9 7144 case BFD_RELOC_PPC64_HIGHER:
3b8b57a9 7145 case BFD_RELOC_PPC64_HIGHER_S:
3b8b57a9 7146 case BFD_RELOC_PPC64_HIGHEST:
3b8b57a9 7147 case BFD_RELOC_PPC64_HIGHEST_S:
f9c6b907
AM
7148 case BFD_RELOC_PPC64_ADDR16_HIGH:
7149 case BFD_RELOC_PPC64_ADDR16_HIGHA:
45965137 7150 case BFD_RELOC_PPC64_ADDR64_LOCAL:
3b8b57a9
AM
7151 break;
7152
7153 case BFD_RELOC_PPC_DTPMOD:
7154 case BFD_RELOC_PPC_TPREL:
7155 case BFD_RELOC_PPC_DTPREL:
7156 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7157 break;
7158
7159 /* Just punt all of these to the linker. */
7160 case BFD_RELOC_PPC_B16_BRTAKEN:
7161 case BFD_RELOC_PPC_B16_BRNTAKEN:
7162 case BFD_RELOC_16_GOTOFF:
7163 case BFD_RELOC_LO16_GOTOFF:
7164 case BFD_RELOC_HI16_GOTOFF:
7165 case BFD_RELOC_HI16_S_GOTOFF:
7166 case BFD_RELOC_LO16_PLTOFF:
7167 case BFD_RELOC_HI16_PLTOFF:
7168 case BFD_RELOC_HI16_S_PLTOFF:
7169 case BFD_RELOC_PPC_COPY:
7170 case BFD_RELOC_PPC_GLOB_DAT:
7171 case BFD_RELOC_16_BASEREL:
7172 case BFD_RELOC_LO16_BASEREL:
7173 case BFD_RELOC_HI16_BASEREL:
7174 case BFD_RELOC_HI16_S_BASEREL:
7175 case BFD_RELOC_PPC_TLS:
7176 case BFD_RELOC_PPC_DTPREL16_LO:
7177 case BFD_RELOC_PPC_DTPREL16_HI:
7178 case BFD_RELOC_PPC_DTPREL16_HA:
7179 case BFD_RELOC_PPC_TPREL16_LO:
7180 case BFD_RELOC_PPC_TPREL16_HI:
7181 case BFD_RELOC_PPC_TPREL16_HA:
7182 case BFD_RELOC_PPC_GOT_TLSGD16:
7183 case BFD_RELOC_PPC_GOT_TLSGD16_LO:
7184 case BFD_RELOC_PPC_GOT_TLSGD16_HI:
7185 case BFD_RELOC_PPC_GOT_TLSGD16_HA:
7186 case BFD_RELOC_PPC_GOT_TLSLD16:
7187 case BFD_RELOC_PPC_GOT_TLSLD16_LO:
7188 case BFD_RELOC_PPC_GOT_TLSLD16_HI:
7189 case BFD_RELOC_PPC_GOT_TLSLD16_HA:
7190 case BFD_RELOC_PPC_GOT_DTPREL16:
7191 case BFD_RELOC_PPC_GOT_DTPREL16_LO:
7192 case BFD_RELOC_PPC_GOT_DTPREL16_HI:
7193 case BFD_RELOC_PPC_GOT_DTPREL16_HA:
7194 case BFD_RELOC_PPC_GOT_TPREL16:
7195 case BFD_RELOC_PPC_GOT_TPREL16_LO:
7196 case BFD_RELOC_PPC_GOT_TPREL16_HI:
7197 case BFD_RELOC_PPC_GOT_TPREL16_HA:
7198 case BFD_RELOC_24_PLT_PCREL:
7199 case BFD_RELOC_PPC_LOCAL24PC:
7200 case BFD_RELOC_32_PLT_PCREL:
7201 case BFD_RELOC_GPREL16:
7202 case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
7203 case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
7204 case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
7205 case BFD_RELOC_PPC_EMB_NADDR32:
7206 case BFD_RELOC_PPC_EMB_NADDR16:
7207 case BFD_RELOC_PPC_EMB_NADDR16_LO:
7208 case BFD_RELOC_PPC_EMB_NADDR16_HI:
7209 case BFD_RELOC_PPC_EMB_NADDR16_HA:
7210 case BFD_RELOC_PPC_EMB_SDAI16:
7211 case BFD_RELOC_PPC_EMB_SDA2REL:
7212 case BFD_RELOC_PPC_EMB_SDA2I16:
7213 case BFD_RELOC_PPC_EMB_SDA21:
7214 case BFD_RELOC_PPC_VLE_SDA21_LO:
7215 case BFD_RELOC_PPC_EMB_MRKREF:
7216 case BFD_RELOC_PPC_EMB_RELSEC16:
7217 case BFD_RELOC_PPC_EMB_RELST_LO:
7218 case BFD_RELOC_PPC_EMB_RELST_HI:
7219 case BFD_RELOC_PPC_EMB_RELST_HA:
7220 case BFD_RELOC_PPC_EMB_BIT_FLD:
7221 case BFD_RELOC_PPC_EMB_RELSDA:
0baf16f2 7222 case BFD_RELOC_PPC64_TOC:
3b8b57a9 7223 case BFD_RELOC_PPC_TOC16:
4a403be0
CC
7224 case BFD_RELOC_PPC_TOC16_LO:
7225 case BFD_RELOC_PPC_TOC16_HI:
3b8b57a9
AM
7226 case BFD_RELOC_PPC64_TOC16_LO:
7227 case BFD_RELOC_PPC64_TOC16_HI:
7228 case BFD_RELOC_PPC64_TOC16_HA:
f9c6b907
AM
7229 case BFD_RELOC_PPC64_DTPREL16_HIGH:
7230 case BFD_RELOC_PPC64_DTPREL16_HIGHA:
3b8b57a9
AM
7231 case BFD_RELOC_PPC64_DTPREL16_HIGHER:
7232 case BFD_RELOC_PPC64_DTPREL16_HIGHERA:
7233 case BFD_RELOC_PPC64_DTPREL16_HIGHEST:
7234 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:
f9c6b907
AM
7235 case BFD_RELOC_PPC64_TPREL16_HIGH:
7236 case BFD_RELOC_PPC64_TPREL16_HIGHA:
3b8b57a9
AM
7237 case BFD_RELOC_PPC64_TPREL16_HIGHER:
7238 case BFD_RELOC_PPC64_TPREL16_HIGHERA:
7239 case BFD_RELOC_PPC64_TPREL16_HIGHEST:
7240 case BFD_RELOC_PPC64_TPREL16_HIGHESTA:
c213164a 7241 case BFD_RELOC_PPC64_TLS_PCREL:
94f592af 7242 fixP->fx_done = 0;
0baf16f2 7243 break;
0baf16f2 7244#endif
3b8b57a9
AM
7245
7246#ifdef OBJ_XCOFF
1b2cb8e2
CC
7247 case BFD_RELOC_PPC_TLSGD:
7248 case BFD_RELOC_PPC_TLSLD:
7249 case BFD_RELOC_PPC_TLSLE:
7250 case BFD_RELOC_PPC_TLSIE:
7251 case BFD_RELOC_PPC_TLSM:
7252 case BFD_RELOC_PPC64_TLSGD:
7253 case BFD_RELOC_PPC64_TLSLD:
7254 case BFD_RELOC_PPC64_TLSLE:
7255 case BFD_RELOC_PPC64_TLSIE:
7256 case BFD_RELOC_PPC64_TLSM:
7257 gas_assert (fixP->fx_addsy != NULL);
7258 S_SET_THREAD_LOCAL (fixP->fx_addsy);
7259 fieldval = 0;
7260 break;
7261
7262 /* TLSML relocations are targeting a XMC_TC symbol named
7263 "_$TLSML". We can't check earlier because the relocation
7264 can target any symbol name which will be latter .rename
7265 to "_$TLSML". */
7266 case BFD_RELOC_PPC_TLSML:
7267 case BFD_RELOC_PPC64_TLSML:
7268 gas_assert (fixP->fx_addsy != NULL);
7269 if (strcmp (symbol_get_tc (fixP->fx_addsy)->real_name, "_$TLSML") != 0)
7270 {
7271 as_bad_where (fixP->fx_file, fixP->fx_line,
7272 _("R_TLSML relocation doesn't target a "
7273 "symbol named \"_$TLSML\". %s"), S_GET_NAME(fixP->fx_addsy));
7274 }
7275 fieldval = 0;
7276 break;
7277
3b8b57a9 7278 case BFD_RELOC_NONE:
3b8b57a9 7279#endif
5656a981
AM
7280 case BFD_RELOC_CTOR:
7281 case BFD_RELOC_32:
7282 case BFD_RELOC_32_PCREL:
7283 case BFD_RELOC_RVA:
7284 case BFD_RELOC_64:
7285 case BFD_RELOC_64_PCREL:
7286 case BFD_RELOC_16:
7287 case BFD_RELOC_16_PCREL:
7288 case BFD_RELOC_8:
7289 break;
3b8b57a9 7290
252b5132 7291 default:
bc805888 7292 fprintf (stderr,
94f592af 7293 _("Gas failure, reloc value %d\n"), fixP->fx_r_type);
99a814a1 7294 fflush (stderr);
252b5132
RH
7295 abort ();
7296 }
46b596ff 7297
5656a981 7298 if (fixP->fx_size && APPLY_RELOC)
46b596ff 7299 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
5656a981 7300 fieldval, fixP->fx_size);
bf7279d5
AM
7301 if (warn_476
7302 && (seg->flags & SEC_CODE) != 0
7303 && fixP->fx_size == 4
7304 && fixP->fx_done
7305 && !fixP->fx_tcbit
7306 && (fixP->fx_r_type == BFD_RELOC_32
7307 || fixP->fx_r_type == BFD_RELOC_CTOR
7308 || fixP->fx_r_type == BFD_RELOC_32_PCREL))
7309 as_warn_where (fixP->fx_file, fixP->fx_line,
7310 _("data in executable section"));
5656a981
AM
7311 }
7312
252b5132 7313#ifdef OBJ_ELF
3b8b57a9 7314 ppc_elf_validate_fix (fixP, seg);
94f592af 7315 fixP->fx_addnumber = value;
4e6935a6
AM
7316
7317 /* PowerPC uses RELA relocs, ie. the reloc addend is stored separately
7318 from the section contents. If we are going to be emitting a reloc
7319 then the section contents are immaterial, so don't warn if they
7320 happen to overflow. Leave such warnings to ld. */
7321 if (!fixP->fx_done)
a38a07e0
AM
7322 {
7323 fixP->fx_no_overflow = 1;
7324
7325 /* Arrange to emit .TOC. as a normal symbol if used in anything
7326 but .TOC.@tocbase. */
7327 if (ppc_obj64
7328 && fixP->fx_r_type != BFD_RELOC_PPC64_TOC
7329 && fixP->fx_addsy != NULL
7330 && strcmp (S_GET_NAME (fixP->fx_addsy), ".TOC.") == 0)
7331 symbol_get_bfdsym (fixP->fx_addsy)->flags |= BSF_KEEP;
7332 }
252b5132 7333#else
4a403be0
CC
7334 if (fixP->fx_r_type != BFD_RELOC_PPC_TOC16
7335 && fixP->fx_r_type != BFD_RELOC_PPC_TOC16_HI
7336 && fixP->fx_r_type != BFD_RELOC_PPC_TOC16_LO)
94f592af 7337 fixP->fx_addnumber = 0;
252b5132
RH
7338 else
7339 {
8edcbfcd
TG
7340 /* We want to use the offset within the toc, not the actual VMA
7341 of the symbol. */
fd361982
AM
7342 fixP->fx_addnumber = (- bfd_section_vma (S_GET_SEGMENT (fixP->fx_addsy))
7343 - S_GET_VALUE (ppc_toc_csect));
4a403be0
CC
7344
7345 /* The high bits must be adjusted for the low bits being signed. */
7346 if (fixP->fx_r_type == BFD_RELOC_PPC_TOC16_HI) {
7347 fixP->fx_addnumber += 0x8000;
7348 }
7349
ac21e7da
TG
7350 /* Set *valP to avoid errors. */
7351 *valP = value;
252b5132
RH
7352 }
7353#endif
252b5132
RH
7354}
7355
7356/* Generate a reloc for a fixup. */
7357
7358arelent *
98027b10 7359tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
7360{
7361 arelent *reloc;
7362
325801bd 7363 reloc = XNEW (arelent);
252b5132 7364
325801bd 7365 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 7366 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132 7367 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
c213164a
AM
7368 /* BFD_RELOC_PPC64_TLS_PCREL generates R_PPC64_TLS with an odd r_offset. */
7369 if (fixp->fx_r_type == BFD_RELOC_PPC64_TLS_PCREL)
7370 reloc->address++;
252b5132
RH
7371 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
7372 if (reloc->howto == (reloc_howto_type *) NULL)
7373 {
7374 as_bad_where (fixp->fx_file, fixp->fx_line,
99a814a1
AM
7375 _("reloc %d not supported by object file format"),
7376 (int) fixp->fx_r_type);
252b5132
RH
7377 return NULL;
7378 }
7379 reloc->addend = fixp->fx_addnumber;
7380
7381 return reloc;
7382}
75e21f08
JJ
7383
7384void
98027b10 7385ppc_cfi_frame_initial_instructions (void)
75e21f08
JJ
7386{
7387 cfi_add_CFA_def_cfa (1, 0);
7388}
7389
7390int
1df69f4f 7391tc_ppc_regname_to_dw2regnum (char *regname)
75e21f08
JJ
7392{
7393 unsigned int regnum = -1;
7394 unsigned int i;
7395 const char *p;
7396 char *q;
e0471c16 7397 static struct { const char *name; int dw2regnum; } regnames[] =
75e21f08
JJ
7398 {
7399 { "sp", 1 }, { "r.sp", 1 }, { "rtoc", 2 }, { "r.toc", 2 },
7400 { "mq", 64 }, { "lr", 65 }, { "ctr", 66 }, { "ap", 67 },
80f846b6 7401 { "cr", 70 }, { "xer", 76 }, { "vrsave", 109 }, { "vscr", 110 },
75e21f08
JJ
7402 { "spe_acc", 111 }, { "spefscr", 112 }
7403 };
7404
7405 for (i = 0; i < ARRAY_SIZE (regnames); ++i)
7406 if (strcmp (regnames[i].name, regname) == 0)
7407 return regnames[i].dw2regnum;
7408
7409 if (regname[0] == 'r' || regname[0] == 'f' || regname[0] == 'v')
7410 {
7411 p = regname + 1 + (regname[1] == '.');
7412 regnum = strtoul (p, &q, 10);
7413 if (p == q || *q || regnum >= 32)
7414 return -1;
7415 if (regname[0] == 'f')
b7d7dc63 7416 regnum += 32;
75e21f08 7417 else if (regname[0] == 'v')
b7d7dc63 7418 regnum += 77;
75e21f08
JJ
7419 }
7420 else if (regname[0] == 'c' && regname[1] == 'r')
7421 {
7422 p = regname + 2 + (regname[2] == '.');
7423 if (p[0] < '0' || p[0] > '7' || p[1])
b7d7dc63 7424 return -1;
75e21f08
JJ
7425 regnum = p[0] - '0' + 68;
7426 }
7427 return regnum;
7428}