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