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