daily update
[deliverable/binutils-gdb.git] / bfd / elf32-ppc.c
CommitLineData
252b5132 1/* PowerPC-specific support for 32-bit ELF
86bbe32f 2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
7898deda 3 Free Software Foundation, Inc.
252b5132
RH
4 Written by Ian Lance Taylor, Cygnus Support.
5
ae9a127f 6 This file is part of BFD, the Binary File Descriptor library.
252b5132 7
ae9a127f
NC
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
252b5132 12
ae9a127f
NC
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
252b5132 17
ae9a127f 18 You should have received a copy of the GNU General Public License
fc0bffd6
AM
19 along with this program; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
252b5132
RH
22
23/* This file is based on a preliminary PowerPC ELF ABI. The
24 information may not match the final PowerPC ELF ABI. It includes
25 suggestions from the in-progress Embedded PowerPC ABI, and that
26 information may also not match. */
27
28#include "bfd.h"
29#include "sysdep.h"
30#include "bfdlink.h"
31#include "libbfd.h"
32#include "elf-bfd.h"
33#include "elf/ppc.h"
7619e7c7 34#include "elf32-ppc.h"
252b5132 35
f0fe0e16 36/* RELA relocations are used here. */
252b5132 37
252b5132 38static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
55fd94b0 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
7619e7c7 40static bfd_reloc_status_type ppc_elf_unhandled_reloc
55fd94b0 41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
252b5132 42
70bccea4
AM
43/* Branch prediction bit for branch taken relocs. */
44#define BRANCH_PREDICT_BIT 0x200000
45/* Mask to set RA in memory instructions. */
46#define RA_REGISTER_MASK 0x001f0000
47/* Value to shift register by to insert RA. */
48#define RA_REGISTER_SHIFT 16
252b5132
RH
49
50/* The name of the dynamic interpreter. This is put in the .interp
51 section. */
252b5132
RH
52#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
53
54/* The size in bytes of an entry in the procedure linkage table. */
55#define PLT_ENTRY_SIZE 12
56/* The initial size of the plt reserved for the dynamic linker. */
57#define PLT_INITIAL_ENTRY_SIZE 72
58/* The size of the gap between entries in the PLT. */
59#define PLT_SLOT_SIZE 8
60/* The number of single-slot PLT entries (the rest use two slots). */
61#define PLT_NUM_SINGLE_ENTRIES 8192
62
7619e7c7
AM
63/* Some nop instructions. */
64#define NOP 0x60000000
65#define CROR_151515 0x4def7b82
66#define CROR_313131 0x4ffffb82
67
68/* Offset of tp and dtp pointers from start of TLS block. */
69#define TP_OFFSET 0x7000
70#define DTP_OFFSET 0x8000
71
252b5132 72\f
7fce784e
AS
73/* The PPC linker needs to keep track of the number of relocs that it
74 decides to copy as dynamic relocs in check_relocs for each symbol.
75 This is so that it can later discard them if they are found to be
76 unnecessary. We store the information in a field extending the
77 regular ELF linker hash table. */
78
79struct ppc_elf_dyn_relocs
80{
81 struct ppc_elf_dyn_relocs *next;
82
83 /* The input section of the reloc. */
84 asection *sec;
85
86 /* Total number of relocs copied for the input section. */
87 bfd_size_type count;
ee05f2fe
AM
88
89 /* Number of pc-relative relocs copied for the input section. */
90 bfd_size_type pc_count;
7fce784e
AS
91};
92
93/* PPC ELF linker hash entry. */
94
95struct ppc_elf_link_hash_entry
96{
7619e7c7 97 struct elf_link_hash_entry elf;
7fce784e
AS
98
99 /* Track dynamic relocs copied for this symbol. */
100 struct ppc_elf_dyn_relocs *dyn_relocs;
7619e7c7
AM
101
102 /* Contexts in which symbol is used in the GOT (or TOC).
fc0bffd6 103 TLS_GD .. TLS_TLS bits are or'd into the mask as the
7619e7c7
AM
104 corresponding relocs are encountered during check_relocs.
105 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
106 indicate the corresponding GOT entry type is not needed. */
107#define TLS_GD 1 /* GD reloc. */
108#define TLS_LD 2 /* LD reloc. */
109#define TLS_TPREL 4 /* TPREL reloc, => IE. */
110#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
111#define TLS_TLS 16 /* Any TLS reloc. */
112#define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
113 char tls_mask;
7fce784e
AS
114};
115
116#define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
117
118/* PPC ELF linker hash table. */
119
120struct ppc_elf_link_hash_table
121{
7619e7c7
AM
122 struct elf_link_hash_table elf;
123
124 /* Short-cuts to get to dynamic linker sections. */
125 asection *got;
126 asection *relgot;
127 asection *plt;
128 asection *relplt;
129 asection *dynbss;
130 asection *relbss;
131 asection *dynsbss;
132 asection *relsbss;
133 elf_linker_section_t *sdata;
134 elf_linker_section_t *sdata2;
135
136 /* Short-cut to first output tls section. */
137 asection *tls_sec;
138
139 /* Shortcut to .__tls_get_addr. */
140 struct elf_link_hash_entry *tls_get_addr;
141
142 /* TLS local dynamic got entry handling. */
143 union {
144 bfd_signed_vma refcount;
145 bfd_vma offset;
146 } tlsld_got;
7fce784e
AS
147
148 /* Small local sym to section mapping cache. */
149 struct sym_sec_cache sym_sec;
150};
151
152/* Get the PPC ELF linker hash table from a link_info structure. */
153
154#define ppc_elf_hash_table(p) \
155 ((struct ppc_elf_link_hash_table *) (p)->hash)
156
157/* Create an entry in a PPC ELF linker hash table. */
158
159static struct bfd_hash_entry *
55fd94b0
AM
160ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
161 struct bfd_hash_table *table,
162 const char *string)
7fce784e
AS
163{
164 /* Allocate the structure if it has not already been allocated by a
165 subclass. */
166 if (entry == NULL)
167 {
168 entry = bfd_hash_allocate (table,
169 sizeof (struct ppc_elf_link_hash_entry));
170 if (entry == NULL)
171 return entry;
172 }
173
174 /* Call the allocation method of the superclass. */
175 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
176 if (entry != NULL)
7619e7c7
AM
177 {
178 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
179 ppc_elf_hash_entry (entry)->tls_mask = 0;
180 }
7fce784e
AS
181
182 return entry;
183}
184
185/* Create a PPC ELF linker hash table. */
186
187static struct bfd_link_hash_table *
55fd94b0 188ppc_elf_link_hash_table_create (bfd *abfd)
7fce784e
AS
189{
190 struct ppc_elf_link_hash_table *ret;
191
55fd94b0 192 ret = bfd_malloc (sizeof (struct ppc_elf_link_hash_table));
7fce784e
AS
193 if (ret == NULL)
194 return NULL;
195
7619e7c7 196 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
7fce784e
AS
197 ppc_elf_link_hash_newfunc))
198 {
199 free (ret);
200 return NULL;
201 }
202
7619e7c7
AM
203 ret->got = NULL;
204 ret->relgot = NULL;
205 ret->plt = NULL;
206 ret->relplt = NULL;
207 ret->dynbss = NULL;
208 ret->relbss = NULL;
209 ret->dynsbss = NULL;
210 ret->relsbss = NULL;
211 ret->sdata = NULL;
212 ret->sdata2 = NULL;
213 ret->tls_sec = NULL;
214 ret->tls_get_addr = NULL;
215 ret->tlsld_got.refcount = 0;
7fce784e
AS
216 ret->sym_sec.abfd = NULL;
217
7619e7c7 218 return &ret->elf.root;
7fce784e
AS
219}
220
fc0bffd6
AM
221/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
222 copying dynamic variables from a shared lib into an app's dynbss
223 section, and instead use a dynamic relocation to point into the
224 shared lib. */
225#define ELIMINATE_COPY_RELOCS 1
226
7fce784e
AS
227/* Copy the extra info we tack onto an elf_link_hash_entry. */
228
229static void
55fd94b0
AM
230ppc_elf_copy_indirect_symbol (struct elf_backend_data *bed,
231 struct elf_link_hash_entry *dir,
232 struct elf_link_hash_entry *ind)
7fce784e
AS
233{
234 struct ppc_elf_link_hash_entry *edir, *eind;
235
236 edir = (struct ppc_elf_link_hash_entry *) dir;
237 eind = (struct ppc_elf_link_hash_entry *) ind;
238
239 if (eind->dyn_relocs != NULL)
240 {
241 if (edir->dyn_relocs != NULL)
242 {
243 struct ppc_elf_dyn_relocs **pp;
244 struct ppc_elf_dyn_relocs *p;
245
246 if (ind->root.type == bfd_link_hash_indirect)
247 abort ();
248
249 /* Add reloc counts against the weak sym to the strong sym
250 list. Merge any entries against the same section. */
251 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
252 {
253 struct ppc_elf_dyn_relocs *q;
254
255 for (q = edir->dyn_relocs; q != NULL; q = q->next)
256 if (q->sec == p->sec)
257 {
ee05f2fe 258 q->pc_count += p->pc_count;
7fce784e
AS
259 q->count += p->count;
260 *pp = p->next;
261 break;
262 }
263 if (q == NULL)
264 pp = &p->next;
265 }
266 *pp = edir->dyn_relocs;
267 }
268
269 edir->dyn_relocs = eind->dyn_relocs;
270 eind->dyn_relocs = NULL;
271 }
272
7619e7c7
AM
273 edir->tls_mask |= eind->tls_mask;
274
81848ca0
AM
275 if (ELIMINATE_COPY_RELOCS
276 && ind->root.type != bfd_link_hash_indirect
277 && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
fc0bffd6
AM
278 /* If called to transfer flags for a weakdef during processing
279 of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
280 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
281 dir->elf_link_hash_flags |=
282 (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
283 | ELF_LINK_HASH_REF_REGULAR
284 | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
285 else
286 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
7fce784e
AS
287}
288\f
8da6118f 289static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
252b5132 290
8da6118f 291static reloc_howto_type ppc_elf_howto_raw[] = {
252b5132
RH
292 /* This reloc does nothing. */
293 HOWTO (R_PPC_NONE, /* type */
294 0, /* rightshift */
295 2, /* size (0 = byte, 1 = short, 2 = long) */
296 32, /* bitsize */
b34976b6 297 FALSE, /* pc_relative */
252b5132
RH
298 0, /* bitpos */
299 complain_overflow_bitfield, /* complain_on_overflow */
300 bfd_elf_generic_reloc, /* special_function */
301 "R_PPC_NONE", /* name */
b34976b6 302 FALSE, /* partial_inplace */
252b5132
RH
303 0, /* src_mask */
304 0, /* dst_mask */
b34976b6 305 FALSE), /* pcrel_offset */
252b5132
RH
306
307 /* A standard 32 bit relocation. */
308 HOWTO (R_PPC_ADDR32, /* type */
309 0, /* rightshift */
310 2, /* size (0 = byte, 1 = short, 2 = long) */
311 32, /* bitsize */
b34976b6 312 FALSE, /* pc_relative */
252b5132
RH
313 0, /* bitpos */
314 complain_overflow_bitfield, /* complain_on_overflow */
315 bfd_elf_generic_reloc, /* special_function */
316 "R_PPC_ADDR32", /* name */
b34976b6 317 FALSE, /* partial_inplace */
252b5132
RH
318 0, /* src_mask */
319 0xffffffff, /* dst_mask */
b34976b6 320 FALSE), /* pcrel_offset */
252b5132
RH
321
322 /* An absolute 26 bit branch; the lower two bits must be zero.
323 FIXME: we don't check that, we just clear them. */
324 HOWTO (R_PPC_ADDR24, /* type */
325 0, /* rightshift */
326 2, /* size (0 = byte, 1 = short, 2 = long) */
327 26, /* bitsize */
b34976b6 328 FALSE, /* pc_relative */
252b5132
RH
329 0, /* bitpos */
330 complain_overflow_bitfield, /* complain_on_overflow */
331 bfd_elf_generic_reloc, /* special_function */
332 "R_PPC_ADDR24", /* name */
b34976b6 333 FALSE, /* partial_inplace */
252b5132
RH
334 0, /* src_mask */
335 0x3fffffc, /* dst_mask */
b34976b6 336 FALSE), /* pcrel_offset */
252b5132
RH
337
338 /* A standard 16 bit relocation. */
339 HOWTO (R_PPC_ADDR16, /* type */
340 0, /* rightshift */
341 1, /* size (0 = byte, 1 = short, 2 = long) */
342 16, /* bitsize */
b34976b6 343 FALSE, /* pc_relative */
252b5132
RH
344 0, /* bitpos */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 bfd_elf_generic_reloc, /* special_function */
347 "R_PPC_ADDR16", /* name */
b34976b6 348 FALSE, /* partial_inplace */
252b5132
RH
349 0, /* src_mask */
350 0xffff, /* dst_mask */
b34976b6 351 FALSE), /* pcrel_offset */
252b5132
RH
352
353 /* A 16 bit relocation without overflow. */
354 HOWTO (R_PPC_ADDR16_LO, /* type */
355 0, /* rightshift */
356 1, /* size (0 = byte, 1 = short, 2 = long) */
357 16, /* bitsize */
b34976b6 358 FALSE, /* pc_relative */
252b5132
RH
359 0, /* bitpos */
360 complain_overflow_dont,/* complain_on_overflow */
361 bfd_elf_generic_reloc, /* special_function */
362 "R_PPC_ADDR16_LO", /* name */
b34976b6 363 FALSE, /* partial_inplace */
252b5132
RH
364 0, /* src_mask */
365 0xffff, /* dst_mask */
b34976b6 366 FALSE), /* pcrel_offset */
252b5132
RH
367
368 /* The high order 16 bits of an address. */
369 HOWTO (R_PPC_ADDR16_HI, /* type */
370 16, /* rightshift */
371 1, /* size (0 = byte, 1 = short, 2 = long) */
372 16, /* bitsize */
b34976b6 373 FALSE, /* pc_relative */
252b5132
RH
374 0, /* bitpos */
375 complain_overflow_dont, /* complain_on_overflow */
376 bfd_elf_generic_reloc, /* special_function */
377 "R_PPC_ADDR16_HI", /* name */
b34976b6 378 FALSE, /* partial_inplace */
252b5132
RH
379 0, /* src_mask */
380 0xffff, /* dst_mask */
b34976b6 381 FALSE), /* pcrel_offset */
252b5132
RH
382
383 /* The high order 16 bits of an address, plus 1 if the contents of
8da6118f 384 the low 16 bits, treated as a signed number, is negative. */
252b5132
RH
385 HOWTO (R_PPC_ADDR16_HA, /* type */
386 16, /* rightshift */
387 1, /* size (0 = byte, 1 = short, 2 = long) */
388 16, /* bitsize */
b34976b6 389 FALSE, /* pc_relative */
252b5132
RH
390 0, /* bitpos */
391 complain_overflow_dont, /* complain_on_overflow */
392 ppc_elf_addr16_ha_reloc, /* special_function */
393 "R_PPC_ADDR16_HA", /* name */
b34976b6 394 FALSE, /* partial_inplace */
252b5132
RH
395 0, /* src_mask */
396 0xffff, /* dst_mask */
b34976b6 397 FALSE), /* pcrel_offset */
252b5132
RH
398
399 /* An absolute 16 bit branch; the lower two bits must be zero.
400 FIXME: we don't check that, we just clear them. */
401 HOWTO (R_PPC_ADDR14, /* type */
402 0, /* rightshift */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
404 16, /* bitsize */
b34976b6 405 FALSE, /* pc_relative */
252b5132
RH
406 0, /* bitpos */
407 complain_overflow_bitfield, /* complain_on_overflow */
408 bfd_elf_generic_reloc, /* special_function */
409 "R_PPC_ADDR14", /* name */
b34976b6 410 FALSE, /* partial_inplace */
252b5132
RH
411 0, /* src_mask */
412 0xfffc, /* dst_mask */
b34976b6 413 FALSE), /* pcrel_offset */
252b5132
RH
414
415 /* An absolute 16 bit branch, for which bit 10 should be set to
416 indicate that the branch is expected to be taken. The lower two
8da6118f 417 bits must be zero. */
252b5132
RH
418 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
419 0, /* rightshift */
420 2, /* size (0 = byte, 1 = short, 2 = long) */
421 16, /* bitsize */
b34976b6 422 FALSE, /* pc_relative */
252b5132
RH
423 0, /* bitpos */
424 complain_overflow_bitfield, /* complain_on_overflow */
425 bfd_elf_generic_reloc, /* special_function */
426 "R_PPC_ADDR14_BRTAKEN",/* name */
b34976b6 427 FALSE, /* partial_inplace */
252b5132
RH
428 0, /* src_mask */
429 0xfffc, /* dst_mask */
b34976b6 430 FALSE), /* pcrel_offset */
252b5132
RH
431
432 /* An absolute 16 bit branch, for which bit 10 should be set to
433 indicate that the branch is not expected to be taken. The lower
434 two bits must be zero. */
435 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
436 0, /* rightshift */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
438 16, /* bitsize */
b34976b6 439 FALSE, /* pc_relative */
252b5132
RH
440 0, /* bitpos */
441 complain_overflow_bitfield, /* complain_on_overflow */
442 bfd_elf_generic_reloc, /* special_function */
443 "R_PPC_ADDR14_BRNTAKEN",/* name */
b34976b6 444 FALSE, /* partial_inplace */
252b5132
RH
445 0, /* src_mask */
446 0xfffc, /* dst_mask */
b34976b6 447 FALSE), /* pcrel_offset */
252b5132 448
8da6118f 449 /* A relative 26 bit branch; the lower two bits must be zero. */
252b5132
RH
450 HOWTO (R_PPC_REL24, /* type */
451 0, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 26, /* bitsize */
b34976b6 454 TRUE, /* pc_relative */
252b5132
RH
455 0, /* bitpos */
456 complain_overflow_signed, /* complain_on_overflow */
457 bfd_elf_generic_reloc, /* special_function */
458 "R_PPC_REL24", /* name */
b34976b6 459 FALSE, /* partial_inplace */
252b5132
RH
460 0, /* src_mask */
461 0x3fffffc, /* dst_mask */
b34976b6 462 TRUE), /* pcrel_offset */
252b5132 463
8da6118f 464 /* A relative 16 bit branch; the lower two bits must be zero. */
252b5132
RH
465 HOWTO (R_PPC_REL14, /* type */
466 0, /* rightshift */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
468 16, /* bitsize */
b34976b6 469 TRUE, /* pc_relative */
252b5132
RH
470 0, /* bitpos */
471 complain_overflow_signed, /* complain_on_overflow */
472 bfd_elf_generic_reloc, /* special_function */
473 "R_PPC_REL14", /* name */
b34976b6 474 FALSE, /* partial_inplace */
252b5132
RH
475 0, /* src_mask */
476 0xfffc, /* dst_mask */
b34976b6 477 TRUE), /* pcrel_offset */
252b5132 478
8da6118f 479 /* A relative 16 bit branch. Bit 10 should be set to indicate that
252b5132
RH
480 the branch is expected to be taken. The lower two bits must be
481 zero. */
482 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
483 0, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 16, /* bitsize */
b34976b6 486 TRUE, /* pc_relative */
252b5132
RH
487 0, /* bitpos */
488 complain_overflow_signed, /* complain_on_overflow */
489 bfd_elf_generic_reloc, /* special_function */
490 "R_PPC_REL14_BRTAKEN", /* name */
b34976b6 491 FALSE, /* partial_inplace */
252b5132
RH
492 0, /* src_mask */
493 0xfffc, /* dst_mask */
b34976b6 494 TRUE), /* pcrel_offset */
252b5132 495
8da6118f 496 /* A relative 16 bit branch. Bit 10 should be set to indicate that
252b5132
RH
497 the branch is not expected to be taken. The lower two bits must
498 be zero. */
499 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
500 0, /* rightshift */
501 2, /* size (0 = byte, 1 = short, 2 = long) */
502 16, /* bitsize */
b34976b6 503 TRUE, /* pc_relative */
252b5132
RH
504 0, /* bitpos */
505 complain_overflow_signed, /* complain_on_overflow */
506 bfd_elf_generic_reloc, /* special_function */
507 "R_PPC_REL14_BRNTAKEN",/* name */
b34976b6 508 FALSE, /* partial_inplace */
252b5132
RH
509 0, /* src_mask */
510 0xfffc, /* dst_mask */
b34976b6 511 TRUE), /* pcrel_offset */
252b5132
RH
512
513 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
514 symbol. */
515 HOWTO (R_PPC_GOT16, /* type */
516 0, /* rightshift */
517 1, /* size (0 = byte, 1 = short, 2 = long) */
518 16, /* bitsize */
b34976b6 519 FALSE, /* pc_relative */
252b5132
RH
520 0, /* bitpos */
521 complain_overflow_signed, /* complain_on_overflow */
522 bfd_elf_generic_reloc, /* special_function */
523 "R_PPC_GOT16", /* name */
b34976b6 524 FALSE, /* partial_inplace */
252b5132
RH
525 0, /* src_mask */
526 0xffff, /* dst_mask */
b34976b6 527 FALSE), /* pcrel_offset */
252b5132
RH
528
529 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
530 the symbol. */
531 HOWTO (R_PPC_GOT16_LO, /* type */
532 0, /* rightshift */
533 1, /* size (0 = byte, 1 = short, 2 = long) */
534 16, /* bitsize */
b34976b6 535 FALSE, /* pc_relative */
252b5132
RH
536 0, /* bitpos */
537 complain_overflow_dont, /* complain_on_overflow */
538 bfd_elf_generic_reloc, /* special_function */
539 "R_PPC_GOT16_LO", /* name */
b34976b6 540 FALSE, /* partial_inplace */
252b5132
RH
541 0, /* src_mask */
542 0xffff, /* dst_mask */
b34976b6 543 FALSE), /* pcrel_offset */
252b5132
RH
544
545 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
546 the symbol. */
547 HOWTO (R_PPC_GOT16_HI, /* type */
548 16, /* rightshift */
549 1, /* size (0 = byte, 1 = short, 2 = long) */
550 16, /* bitsize */
b34976b6 551 FALSE, /* pc_relative */
252b5132
RH
552 0, /* bitpos */
553 complain_overflow_bitfield, /* complain_on_overflow */
554 bfd_elf_generic_reloc, /* special_function */
555 "R_PPC_GOT16_HI", /* name */
b34976b6 556 FALSE, /* partial_inplace */
252b5132
RH
557 0, /* src_mask */
558 0xffff, /* dst_mask */
b34976b6 559 FALSE), /* pcrel_offset */
252b5132
RH
560
561 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
562 the symbol. */
563 HOWTO (R_PPC_GOT16_HA, /* type */
564 16, /* rightshift */
565 1, /* size (0 = byte, 1 = short, 2 = long) */
566 16, /* bitsize */
b34976b6 567 FALSE, /* pc_relative */
252b5132
RH
568 0, /* bitpos */
569 complain_overflow_bitfield, /* complain_on_overflow */
570 ppc_elf_addr16_ha_reloc, /* special_function */
571 "R_PPC_GOT16_HA", /* name */
b34976b6 572 FALSE, /* partial_inplace */
252b5132
RH
573 0, /* src_mask */
574 0xffff, /* dst_mask */
b34976b6 575 FALSE), /* pcrel_offset */
252b5132
RH
576
577 /* Like R_PPC_REL24, but referring to the procedure linkage table
578 entry for the symbol. */
579 HOWTO (R_PPC_PLTREL24, /* type */
580 0, /* rightshift */
581 2, /* size (0 = byte, 1 = short, 2 = long) */
582 26, /* bitsize */
b34976b6 583 TRUE, /* pc_relative */
252b5132
RH
584 0, /* bitpos */
585 complain_overflow_signed, /* complain_on_overflow */
586 bfd_elf_generic_reloc, /* special_function */
587 "R_PPC_PLTREL24", /* name */
b34976b6 588 FALSE, /* partial_inplace */
252b5132
RH
589 0, /* src_mask */
590 0x3fffffc, /* dst_mask */
b34976b6 591 TRUE), /* pcrel_offset */
252b5132
RH
592
593 /* This is used only by the dynamic linker. The symbol should exist
594 both in the object being run and in some shared library. The
595 dynamic linker copies the data addressed by the symbol from the
596 shared library into the object, because the object being
597 run has to have the data at some particular address. */
598 HOWTO (R_PPC_COPY, /* type */
599 0, /* rightshift */
600 2, /* size (0 = byte, 1 = short, 2 = long) */
601 32, /* bitsize */
b34976b6 602 FALSE, /* pc_relative */
252b5132
RH
603 0, /* bitpos */
604 complain_overflow_bitfield, /* complain_on_overflow */
605 bfd_elf_generic_reloc, /* special_function */
606 "R_PPC_COPY", /* name */
b34976b6 607 FALSE, /* partial_inplace */
252b5132
RH
608 0, /* src_mask */
609 0, /* dst_mask */
b34976b6 610 FALSE), /* pcrel_offset */
252b5132
RH
611
612 /* Like R_PPC_ADDR32, but used when setting global offset table
613 entries. */
614 HOWTO (R_PPC_GLOB_DAT, /* type */
615 0, /* rightshift */
616 2, /* size (0 = byte, 1 = short, 2 = long) */
617 32, /* bitsize */
b34976b6 618 FALSE, /* pc_relative */
252b5132
RH
619 0, /* bitpos */
620 complain_overflow_bitfield, /* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_PPC_GLOB_DAT", /* name */
b34976b6 623 FALSE, /* partial_inplace */
252b5132
RH
624 0, /* src_mask */
625 0xffffffff, /* dst_mask */
b34976b6 626 FALSE), /* pcrel_offset */
252b5132
RH
627
628 /* Marks a procedure linkage table entry for a symbol. */
629 HOWTO (R_PPC_JMP_SLOT, /* type */
630 0, /* rightshift */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
632 32, /* bitsize */
b34976b6 633 FALSE, /* pc_relative */
252b5132
RH
634 0, /* bitpos */
635 complain_overflow_bitfield, /* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_PPC_JMP_SLOT", /* name */
b34976b6 638 FALSE, /* partial_inplace */
252b5132
RH
639 0, /* src_mask */
640 0, /* dst_mask */
b34976b6 641 FALSE), /* pcrel_offset */
252b5132
RH
642
643 /* Used only by the dynamic linker. When the object is run, this
644 longword is set to the load address of the object, plus the
645 addend. */
646 HOWTO (R_PPC_RELATIVE, /* type */
647 0, /* rightshift */
648 2, /* size (0 = byte, 1 = short, 2 = long) */
649 32, /* bitsize */
b34976b6 650 FALSE, /* pc_relative */
252b5132
RH
651 0, /* bitpos */
652 complain_overflow_bitfield, /* complain_on_overflow */
653 bfd_elf_generic_reloc, /* special_function */
654 "R_PPC_RELATIVE", /* name */
b34976b6 655 FALSE, /* partial_inplace */
252b5132
RH
656 0, /* src_mask */
657 0xffffffff, /* dst_mask */
b34976b6 658 FALSE), /* pcrel_offset */
252b5132
RH
659
660 /* Like R_PPC_REL24, but uses the value of the symbol within the
661 object rather than the final value. Normally used for
662 _GLOBAL_OFFSET_TABLE_. */
663 HOWTO (R_PPC_LOCAL24PC, /* type */
664 0, /* rightshift */
665 2, /* size (0 = byte, 1 = short, 2 = long) */
666 26, /* bitsize */
b34976b6 667 TRUE, /* pc_relative */
252b5132
RH
668 0, /* bitpos */
669 complain_overflow_signed, /* complain_on_overflow */
670 bfd_elf_generic_reloc, /* special_function */
671 "R_PPC_LOCAL24PC", /* name */
b34976b6 672 FALSE, /* partial_inplace */
252b5132
RH
673 0, /* src_mask */
674 0x3fffffc, /* dst_mask */
b34976b6 675 TRUE), /* pcrel_offset */
252b5132
RH
676
677 /* Like R_PPC_ADDR32, but may be unaligned. */
678 HOWTO (R_PPC_UADDR32, /* type */
679 0, /* rightshift */
680 2, /* size (0 = byte, 1 = short, 2 = long) */
681 32, /* bitsize */
b34976b6 682 FALSE, /* pc_relative */
252b5132
RH
683 0, /* bitpos */
684 complain_overflow_bitfield, /* complain_on_overflow */
685 bfd_elf_generic_reloc, /* special_function */
686 "R_PPC_UADDR32", /* name */
b34976b6 687 FALSE, /* partial_inplace */
252b5132
RH
688 0, /* src_mask */
689 0xffffffff, /* dst_mask */
b34976b6 690 FALSE), /* pcrel_offset */
252b5132
RH
691
692 /* Like R_PPC_ADDR16, but may be unaligned. */
693 HOWTO (R_PPC_UADDR16, /* type */
694 0, /* rightshift */
695 1, /* size (0 = byte, 1 = short, 2 = long) */
696 16, /* bitsize */
b34976b6 697 FALSE, /* pc_relative */
252b5132
RH
698 0, /* bitpos */
699 complain_overflow_bitfield, /* complain_on_overflow */
700 bfd_elf_generic_reloc, /* special_function */
701 "R_PPC_UADDR16", /* name */
b34976b6 702 FALSE, /* partial_inplace */
252b5132
RH
703 0, /* src_mask */
704 0xffff, /* dst_mask */
b34976b6 705 FALSE), /* pcrel_offset */
252b5132
RH
706
707 /* 32-bit PC relative */
708 HOWTO (R_PPC_REL32, /* type */
709 0, /* rightshift */
710 2, /* size (0 = byte, 1 = short, 2 = long) */
711 32, /* bitsize */
b34976b6 712 TRUE, /* pc_relative */
252b5132
RH
713 0, /* bitpos */
714 complain_overflow_bitfield, /* complain_on_overflow */
715 bfd_elf_generic_reloc, /* special_function */
716 "R_PPC_REL32", /* name */
b34976b6 717 FALSE, /* partial_inplace */
252b5132
RH
718 0, /* src_mask */
719 0xffffffff, /* dst_mask */
b34976b6 720 TRUE), /* pcrel_offset */
252b5132
RH
721
722 /* 32-bit relocation to the symbol's procedure linkage table.
c3668558 723 FIXME: not supported. */
252b5132
RH
724 HOWTO (R_PPC_PLT32, /* type */
725 0, /* rightshift */
726 2, /* size (0 = byte, 1 = short, 2 = long) */
727 32, /* bitsize */
b34976b6 728 FALSE, /* pc_relative */
252b5132
RH
729 0, /* bitpos */
730 complain_overflow_bitfield, /* complain_on_overflow */
731 bfd_elf_generic_reloc, /* special_function */
732 "R_PPC_PLT32", /* name */
b34976b6 733 FALSE, /* partial_inplace */
252b5132
RH
734 0, /* src_mask */
735 0, /* dst_mask */
b34976b6 736 FALSE), /* pcrel_offset */
252b5132
RH
737
738 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
c3668558 739 FIXME: not supported. */
252b5132
RH
740 HOWTO (R_PPC_PLTREL32, /* type */
741 0, /* rightshift */
742 2, /* size (0 = byte, 1 = short, 2 = long) */
743 32, /* bitsize */
b34976b6 744 TRUE, /* pc_relative */
252b5132
RH
745 0, /* bitpos */
746 complain_overflow_bitfield, /* complain_on_overflow */
747 bfd_elf_generic_reloc, /* special_function */
748 "R_PPC_PLTREL32", /* name */
b34976b6 749 FALSE, /* partial_inplace */
252b5132
RH
750 0, /* src_mask */
751 0, /* dst_mask */
b34976b6 752 TRUE), /* pcrel_offset */
252b5132
RH
753
754 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
755 the symbol. */
756 HOWTO (R_PPC_PLT16_LO, /* type */
757 0, /* rightshift */
758 1, /* size (0 = byte, 1 = short, 2 = long) */
759 16, /* bitsize */
b34976b6 760 FALSE, /* pc_relative */
252b5132
RH
761 0, /* bitpos */
762 complain_overflow_dont, /* complain_on_overflow */
763 bfd_elf_generic_reloc, /* special_function */
764 "R_PPC_PLT16_LO", /* name */
b34976b6 765 FALSE, /* partial_inplace */
252b5132
RH
766 0, /* src_mask */
767 0xffff, /* dst_mask */
b34976b6 768 FALSE), /* pcrel_offset */
252b5132
RH
769
770 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
771 the symbol. */
772 HOWTO (R_PPC_PLT16_HI, /* type */
773 16, /* rightshift */
774 1, /* size (0 = byte, 1 = short, 2 = long) */
775 16, /* bitsize */
b34976b6 776 FALSE, /* pc_relative */
252b5132
RH
777 0, /* bitpos */
778 complain_overflow_bitfield, /* complain_on_overflow */
779 bfd_elf_generic_reloc, /* special_function */
780 "R_PPC_PLT16_HI", /* name */
b34976b6 781 FALSE, /* partial_inplace */
252b5132
RH
782 0, /* src_mask */
783 0xffff, /* dst_mask */
b34976b6 784 FALSE), /* pcrel_offset */
252b5132
RH
785
786 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
787 the symbol. */
788 HOWTO (R_PPC_PLT16_HA, /* type */
789 16, /* rightshift */
790 1, /* size (0 = byte, 1 = short, 2 = long) */
791 16, /* bitsize */
b34976b6 792 FALSE, /* pc_relative */
252b5132
RH
793 0, /* bitpos */
794 complain_overflow_bitfield, /* complain_on_overflow */
795 ppc_elf_addr16_ha_reloc, /* special_function */
796 "R_PPC_PLT16_HA", /* name */
b34976b6 797 FALSE, /* partial_inplace */
252b5132
RH
798 0, /* src_mask */
799 0xffff, /* dst_mask */
b34976b6 800 FALSE), /* pcrel_offset */
252b5132
RH
801
802 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
803 small data items. */
804 HOWTO (R_PPC_SDAREL16, /* type */
805 0, /* rightshift */
806 1, /* size (0 = byte, 1 = short, 2 = long) */
807 16, /* bitsize */
b34976b6 808 FALSE, /* pc_relative */
252b5132
RH
809 0, /* bitpos */
810 complain_overflow_signed, /* complain_on_overflow */
811 bfd_elf_generic_reloc, /* special_function */
812 "R_PPC_SDAREL16", /* name */
b34976b6 813 FALSE, /* partial_inplace */
252b5132
RH
814 0, /* src_mask */
815 0xffff, /* dst_mask */
b34976b6 816 FALSE), /* pcrel_offset */
252b5132 817
c061c2d8 818 /* 16-bit section relative relocation. */
252b5132
RH
819 HOWTO (R_PPC_SECTOFF, /* type */
820 0, /* rightshift */
c061c2d8
AM
821 1, /* size (0 = byte, 1 = short, 2 = long) */
822 16, /* bitsize */
b34976b6 823 FALSE, /* pc_relative */
252b5132
RH
824 0, /* bitpos */
825 complain_overflow_bitfield, /* complain_on_overflow */
826 bfd_elf_generic_reloc, /* special_function */
827 "R_PPC_SECTOFF", /* name */
b34976b6 828 FALSE, /* partial_inplace */
252b5132 829 0, /* src_mask */
c061c2d8 830 0xffff, /* dst_mask */
b34976b6 831 FALSE), /* pcrel_offset */
252b5132 832
c3668558 833 /* 16-bit lower half section relative relocation. */
252b5132
RH
834 HOWTO (R_PPC_SECTOFF_LO, /* type */
835 0, /* rightshift */
836 1, /* size (0 = byte, 1 = short, 2 = long) */
837 16, /* bitsize */
b34976b6 838 FALSE, /* pc_relative */
252b5132
RH
839 0, /* bitpos */
840 complain_overflow_dont, /* complain_on_overflow */
841 bfd_elf_generic_reloc, /* special_function */
842 "R_PPC_SECTOFF_LO", /* name */
b34976b6 843 FALSE, /* partial_inplace */
252b5132
RH
844 0, /* src_mask */
845 0xffff, /* dst_mask */
b34976b6 846 FALSE), /* pcrel_offset */
252b5132 847
c3668558 848 /* 16-bit upper half section relative relocation. */
252b5132
RH
849 HOWTO (R_PPC_SECTOFF_HI, /* type */
850 16, /* rightshift */
851 1, /* size (0 = byte, 1 = short, 2 = long) */
852 16, /* bitsize */
b34976b6 853 FALSE, /* pc_relative */
252b5132
RH
854 0, /* bitpos */
855 complain_overflow_bitfield, /* complain_on_overflow */
856 bfd_elf_generic_reloc, /* special_function */
857 "R_PPC_SECTOFF_HI", /* name */
b34976b6 858 FALSE, /* partial_inplace */
252b5132
RH
859 0, /* src_mask */
860 0xffff, /* dst_mask */
b34976b6 861 FALSE), /* pcrel_offset */
252b5132 862
c3668558 863 /* 16-bit upper half adjusted section relative relocation. */
252b5132
RH
864 HOWTO (R_PPC_SECTOFF_HA, /* type */
865 16, /* rightshift */
866 1, /* size (0 = byte, 1 = short, 2 = long) */
867 16, /* bitsize */
b34976b6 868 FALSE, /* pc_relative */
252b5132
RH
869 0, /* bitpos */
870 complain_overflow_bitfield, /* complain_on_overflow */
871 ppc_elf_addr16_ha_reloc, /* special_function */
872 "R_PPC_SECTOFF_HA", /* name */
b34976b6 873 FALSE, /* partial_inplace */
252b5132
RH
874 0, /* src_mask */
875 0xffff, /* dst_mask */
b34976b6 876 FALSE), /* pcrel_offset */
252b5132 877
7619e7c7
AM
878 /* Marker reloc for TLS. */
879 HOWTO (R_PPC_TLS,
252b5132
RH
880 0, /* rightshift */
881 2, /* size (0 = byte, 1 = short, 2 = long) */
882 32, /* bitsize */
b34976b6 883 FALSE, /* pc_relative */
252b5132 884 0, /* bitpos */
7619e7c7 885 complain_overflow_dont, /* complain_on_overflow */
252b5132 886 bfd_elf_generic_reloc, /* special_function */
7619e7c7
AM
887 "R_PPC_TLS", /* name */
888 FALSE, /* partial_inplace */
889 0, /* src_mask */
890 0, /* dst_mask */
891 FALSE), /* pcrel_offset */
892
893 /* Computes the load module index of the load module that contains the
894 definition of its TLS sym. */
895 HOWTO (R_PPC_DTPMOD32,
896 0, /* rightshift */
897 2, /* size (0 = byte, 1 = short, 2 = long) */
898 32, /* bitsize */
899 FALSE, /* pc_relative */
900 0, /* bitpos */
901 complain_overflow_dont, /* complain_on_overflow */
902 ppc_elf_unhandled_reloc, /* special_function */
903 "R_PPC_DTPMOD32", /* name */
b34976b6 904 FALSE, /* partial_inplace */
252b5132
RH
905 0, /* src_mask */
906 0xffffffff, /* dst_mask */
b34976b6 907 FALSE), /* pcrel_offset */
252b5132 908
7619e7c7
AM
909 /* Computes a dtv-relative displacement, the difference between the value
910 of sym+add and the base address of the thread-local storage block that
911 contains the definition of sym, minus 0x8000. */
912 HOWTO (R_PPC_DTPREL32,
913 0, /* rightshift */
914 2, /* size (0 = byte, 1 = short, 2 = long) */
915 32, /* bitsize */
916 FALSE, /* pc_relative */
917 0, /* bitpos */
918 complain_overflow_dont, /* complain_on_overflow */
919 ppc_elf_unhandled_reloc, /* special_function */
920 "R_PPC_DTPREL32", /* name */
921 FALSE, /* partial_inplace */
922 0, /* src_mask */
923 0xffffffff, /* dst_mask */
924 FALSE), /* pcrel_offset */
925
926 /* A 16 bit dtprel reloc. */
927 HOWTO (R_PPC_DTPREL16,
252b5132
RH
928 0, /* rightshift */
929 1, /* size (0 = byte, 1 = short, 2 = long) */
930 16, /* bitsize */
b34976b6 931 FALSE, /* pc_relative */
252b5132 932 0, /* bitpos */
7619e7c7
AM
933 complain_overflow_signed, /* complain_on_overflow */
934 ppc_elf_unhandled_reloc, /* special_function */
935 "R_PPC_DTPREL16", /* name */
b34976b6 936 FALSE, /* partial_inplace */
252b5132
RH
937 0, /* src_mask */
938 0xffff, /* dst_mask */
b34976b6 939 FALSE), /* pcrel_offset */
252b5132 940
7619e7c7
AM
941 /* Like DTPREL16, but no overflow. */
942 HOWTO (R_PPC_DTPREL16_LO,
252b5132
RH
943 0, /* rightshift */
944 1, /* size (0 = byte, 1 = short, 2 = long) */
945 16, /* bitsize */
b34976b6 946 FALSE, /* pc_relative */
252b5132 947 0, /* bitpos */
7619e7c7
AM
948 complain_overflow_dont, /* complain_on_overflow */
949 ppc_elf_unhandled_reloc, /* special_function */
950 "R_PPC_DTPREL16_LO", /* name */
b34976b6 951 FALSE, /* partial_inplace */
252b5132
RH
952 0, /* src_mask */
953 0xffff, /* dst_mask */
b34976b6 954 FALSE), /* pcrel_offset */
252b5132 955
7619e7c7
AM
956 /* Like DTPREL16_LO, but next higher group of 16 bits. */
957 HOWTO (R_PPC_DTPREL16_HI,
252b5132
RH
958 16, /* rightshift */
959 1, /* size (0 = byte, 1 = short, 2 = long) */
960 16, /* bitsize */
b34976b6 961 FALSE, /* pc_relative */
252b5132
RH
962 0, /* bitpos */
963 complain_overflow_dont, /* complain_on_overflow */
7619e7c7
AM
964 ppc_elf_unhandled_reloc, /* special_function */
965 "R_PPC_DTPREL16_HI", /* name */
b34976b6 966 FALSE, /* partial_inplace */
252b5132
RH
967 0, /* src_mask */
968 0xffff, /* dst_mask */
b34976b6 969 FALSE), /* pcrel_offset */
252b5132 970
7619e7c7
AM
971 /* Like DTPREL16_HI, but adjust for low 16 bits. */
972 HOWTO (R_PPC_DTPREL16_HA,
252b5132
RH
973 16, /* rightshift */
974 1, /* size (0 = byte, 1 = short, 2 = long) */
975 16, /* bitsize */
b34976b6 976 FALSE, /* pc_relative */
252b5132
RH
977 0, /* bitpos */
978 complain_overflow_dont, /* complain_on_overflow */
7619e7c7
AM
979 ppc_elf_unhandled_reloc, /* special_function */
980 "R_PPC_DTPREL16_HA", /* name */
b34976b6 981 FALSE, /* partial_inplace */
252b5132
RH
982 0, /* src_mask */
983 0xffff, /* dst_mask */
b34976b6 984 FALSE), /* pcrel_offset */
252b5132 985
7619e7c7
AM
986 /* Computes a tp-relative displacement, the difference between the value of
987 sym+add and the value of the thread pointer (r13). */
988 HOWTO (R_PPC_TPREL32,
989 0, /* rightshift */
990 2, /* size (0 = byte, 1 = short, 2 = long) */
991 32, /* bitsize */
992 FALSE, /* pc_relative */
993 0, /* bitpos */
994 complain_overflow_dont, /* complain_on_overflow */
995 ppc_elf_unhandled_reloc, /* special_function */
996 "R_PPC_TPREL32", /* name */
997 FALSE, /* partial_inplace */
998 0, /* src_mask */
999 0xffffffff, /* dst_mask */
1000 FALSE), /* pcrel_offset */
1001
1002 /* A 16 bit tprel reloc. */
1003 HOWTO (R_PPC_TPREL16,
252b5132
RH
1004 0, /* rightshift */
1005 1, /* size (0 = byte, 1 = short, 2 = long) */
1006 16, /* bitsize */
b34976b6 1007 FALSE, /* pc_relative */
252b5132 1008 0, /* bitpos */
7619e7c7
AM
1009 complain_overflow_signed, /* complain_on_overflow */
1010 ppc_elf_unhandled_reloc, /* special_function */
1011 "R_PPC_TPREL16", /* name */
b34976b6 1012 FALSE, /* partial_inplace */
252b5132
RH
1013 0, /* src_mask */
1014 0xffff, /* dst_mask */
b34976b6 1015 FALSE), /* pcrel_offset */
252b5132 1016
7619e7c7
AM
1017 /* Like TPREL16, but no overflow. */
1018 HOWTO (R_PPC_TPREL16_LO,
252b5132
RH
1019 0, /* rightshift */
1020 1, /* size (0 = byte, 1 = short, 2 = long) */
1021 16, /* bitsize */
b34976b6 1022 FALSE, /* pc_relative */
252b5132 1023 0, /* bitpos */
7619e7c7
AM
1024 complain_overflow_dont, /* complain_on_overflow */
1025 ppc_elf_unhandled_reloc, /* special_function */
1026 "R_PPC_TPREL16_LO", /* name */
b34976b6 1027 FALSE, /* partial_inplace */
252b5132
RH
1028 0, /* src_mask */
1029 0xffff, /* dst_mask */
b34976b6 1030 FALSE), /* pcrel_offset */
252b5132 1031
7619e7c7
AM
1032 /* Like TPREL16_LO, but next higher group of 16 bits. */
1033 HOWTO (R_PPC_TPREL16_HI,
1034 16, /* rightshift */
1035 1, /* size (0 = byte, 1 = short, 2 = long) */
1036 16, /* bitsize */
1037 FALSE, /* pc_relative */
1038 0, /* bitpos */
1039 complain_overflow_dont, /* complain_on_overflow */
1040 ppc_elf_unhandled_reloc, /* special_function */
1041 "R_PPC_TPREL16_HI", /* name */
1042 FALSE, /* partial_inplace */
1043 0, /* src_mask */
1044 0xffff, /* dst_mask */
1045 FALSE), /* pcrel_offset */
1046
1047 /* Like TPREL16_HI, but adjust for low 16 bits. */
1048 HOWTO (R_PPC_TPREL16_HA,
1049 16, /* rightshift */
1050 1, /* size (0 = byte, 1 = short, 2 = long) */
1051 16, /* bitsize */
1052 FALSE, /* pc_relative */
1053 0, /* bitpos */
1054 complain_overflow_dont, /* complain_on_overflow */
1055 ppc_elf_unhandled_reloc, /* special_function */
1056 "R_PPC_TPREL16_HA", /* name */
1057 FALSE, /* partial_inplace */
1058 0, /* src_mask */
1059 0xffff, /* dst_mask */
1060 FALSE), /* pcrel_offset */
1061
1062 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1063 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1064 to the first entry. */
1065 HOWTO (R_PPC_GOT_TLSGD16,
252b5132
RH
1066 0, /* rightshift */
1067 1, /* size (0 = byte, 1 = short, 2 = long) */
1068 16, /* bitsize */
b34976b6 1069 FALSE, /* pc_relative */
252b5132
RH
1070 0, /* bitpos */
1071 complain_overflow_signed, /* complain_on_overflow */
7619e7c7
AM
1072 ppc_elf_unhandled_reloc, /* special_function */
1073 "R_PPC_GOT_TLSGD16", /* name */
b34976b6 1074 FALSE, /* partial_inplace */
252b5132
RH
1075 0, /* src_mask */
1076 0xffff, /* dst_mask */
b34976b6 1077 FALSE), /* pcrel_offset */
252b5132 1078
7619e7c7
AM
1079 /* Like GOT_TLSGD16, but no overflow. */
1080 HOWTO (R_PPC_GOT_TLSGD16_LO,
252b5132 1081 0, /* rightshift */
7619e7c7 1082 1, /* size (0 = byte, 1 = short, 2 = long) */
252b5132 1083 16, /* bitsize */
b34976b6 1084 FALSE, /* pc_relative */
252b5132 1085 0, /* bitpos */
7619e7c7
AM
1086 complain_overflow_dont, /* complain_on_overflow */
1087 ppc_elf_unhandled_reloc, /* special_function */
1088 "R_PPC_GOT_TLSGD16_LO", /* name */
b34976b6 1089 FALSE, /* partial_inplace */
252b5132
RH
1090 0, /* src_mask */
1091 0xffff, /* dst_mask */
b34976b6 1092 FALSE), /* pcrel_offset */
252b5132 1093
7619e7c7
AM
1094 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1095 HOWTO (R_PPC_GOT_TLSGD16_HI,
1096 16, /* rightshift */
1097 1, /* size (0 = byte, 1 = short, 2 = long) */
1098 16, /* bitsize */
1099 FALSE, /* pc_relative */
1100 0, /* bitpos */
1101 complain_overflow_dont, /* complain_on_overflow */
1102 ppc_elf_unhandled_reloc, /* special_function */
1103 "R_PPC_GOT_TLSGD16_HI", /* name */
1104 FALSE, /* partial_inplace */
1105 0, /* src_mask */
1106 0xffff, /* dst_mask */
1107 FALSE), /* pcrel_offset */
252b5132 1108
7619e7c7
AM
1109 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1110 HOWTO (R_PPC_GOT_TLSGD16_HA,
1111 16, /* rightshift */
1112 1, /* size (0 = byte, 1 = short, 2 = long) */
1113 16, /* bitsize */
1114 FALSE, /* pc_relative */
1115 0, /* bitpos */
1116 complain_overflow_dont, /* complain_on_overflow */
1117 ppc_elf_unhandled_reloc, /* special_function */
1118 "R_PPC_GOT_TLSGD16_HA", /* name */
1119 FALSE, /* partial_inplace */
1120 0, /* src_mask */
1121 0xffff, /* dst_mask */
1122 FALSE), /* pcrel_offset */
1123
1124 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1125 with values (sym+add)@dtpmod and zero, and computes the offset to the
1126 first entry. */
1127 HOWTO (R_PPC_GOT_TLSLD16,
252b5132
RH
1128 0, /* rightshift */
1129 1, /* size (0 = byte, 1 = short, 2 = long) */
1130 16, /* bitsize */
7619e7c7 1131 FALSE, /* pc_relative */
252b5132
RH
1132 0, /* bitpos */
1133 complain_overflow_signed, /* complain_on_overflow */
7619e7c7
AM
1134 ppc_elf_unhandled_reloc, /* special_function */
1135 "R_PPC_GOT_TLSLD16", /* name */
b34976b6 1136 FALSE, /* partial_inplace */
252b5132
RH
1137 0, /* src_mask */
1138 0xffff, /* dst_mask */
b34976b6 1139 FALSE), /* pcrel_offset */
252b5132 1140
7619e7c7
AM
1141 /* Like GOT_TLSLD16, but no overflow. */
1142 HOWTO (R_PPC_GOT_TLSLD16_LO,
252b5132 1143 0, /* rightshift */
7619e7c7
AM
1144 1, /* size (0 = byte, 1 = short, 2 = long) */
1145 16, /* bitsize */
b34976b6 1146 FALSE, /* pc_relative */
252b5132
RH
1147 0, /* bitpos */
1148 complain_overflow_dont, /* complain_on_overflow */
7619e7c7
AM
1149 ppc_elf_unhandled_reloc, /* special_function */
1150 "R_PPC_GOT_TLSLD16_LO", /* name */
b34976b6 1151 FALSE, /* partial_inplace */
252b5132 1152 0, /* src_mask */
7619e7c7 1153 0xffff, /* dst_mask */
b34976b6 1154 FALSE), /* pcrel_offset */
252b5132 1155
7619e7c7
AM
1156 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1157 HOWTO (R_PPC_GOT_TLSLD16_HI,
1158 16, /* rightshift */
1159 1, /* size (0 = byte, 1 = short, 2 = long) */
1160 16, /* bitsize */
b34976b6 1161 FALSE, /* pc_relative */
252b5132
RH
1162 0, /* bitpos */
1163 complain_overflow_dont, /* complain_on_overflow */
7619e7c7
AM
1164 ppc_elf_unhandled_reloc, /* special_function */
1165 "R_PPC_GOT_TLSLD16_HI", /* name */
b34976b6 1166 FALSE, /* partial_inplace */
252b5132 1167 0, /* src_mask */
7619e7c7 1168 0xffff, /* dst_mask */
b34976b6 1169 FALSE), /* pcrel_offset */
252b5132 1170
7619e7c7
AM
1171 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1172 HOWTO (R_PPC_GOT_TLSLD16_HA,
1173 16, /* rightshift */
1174 1, /* size (0 = byte, 1 = short, 2 = long) */
1175 16, /* bitsize */
1176 FALSE, /* pc_relative */
1177 0, /* bitpos */
1178 complain_overflow_dont, /* complain_on_overflow */
1179 ppc_elf_unhandled_reloc, /* special_function */
1180 "R_PPC_GOT_TLSLD16_HA", /* name */
1181 FALSE, /* partial_inplace */
1182 0, /* src_mask */
1183 0xffff, /* dst_mask */
1184 FALSE), /* pcrel_offset */
1185
1186 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1187 the offset to the entry. */
1188 HOWTO (R_PPC_GOT_DTPREL16,
252b5132
RH
1189 0, /* rightshift */
1190 1, /* size (0 = byte, 1 = short, 2 = long) */
1191 16, /* bitsize */
b34976b6 1192 FALSE, /* pc_relative */
252b5132
RH
1193 0, /* bitpos */
1194 complain_overflow_signed, /* complain_on_overflow */
7619e7c7
AM
1195 ppc_elf_unhandled_reloc, /* special_function */
1196 "R_PPC_GOT_DTPREL16", /* name */
b34976b6 1197 FALSE, /* partial_inplace */
252b5132
RH
1198 0, /* src_mask */
1199 0xffff, /* dst_mask */
b34976b6 1200 FALSE), /* pcrel_offset */
252b5132 1201
7619e7c7
AM
1202 /* Like GOT_DTPREL16, but no overflow. */
1203 HOWTO (R_PPC_GOT_DTPREL16_LO,
1204 0, /* rightshift */
1205 1, /* size (0 = byte, 1 = short, 2 = long) */
1206 16, /* bitsize */
1207 FALSE, /* pc_relative */
1208 0, /* bitpos */
1209 complain_overflow_dont, /* complain_on_overflow */
1210 ppc_elf_unhandled_reloc, /* special_function */
1211 "R_PPC_GOT_DTPREL16_LO", /* name */
1212 FALSE, /* partial_inplace */
1213 0, /* src_mask */
1214 0xffff, /* dst_mask */
1215 FALSE), /* pcrel_offset */
252b5132 1216
7619e7c7
AM
1217 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1218 HOWTO (R_PPC_GOT_DTPREL16_HI,
1219 16, /* rightshift */
1220 1, /* size (0 = byte, 1 = short, 2 = long) */
1221 16, /* bitsize */
1222 FALSE, /* pc_relative */
1223 0, /* bitpos */
1224 complain_overflow_dont, /* complain_on_overflow */
1225 ppc_elf_unhandled_reloc, /* special_function */
1226 "R_PPC_GOT_DTPREL16_HI", /* name */
1227 FALSE, /* partial_inplace */
1228 0, /* src_mask */
1229 0xffff, /* dst_mask */
1230 FALSE), /* pcrel_offset */
252b5132 1231
7619e7c7
AM
1232 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1233 HOWTO (R_PPC_GOT_DTPREL16_HA,
1234 16, /* rightshift */
1235 1, /* size (0 = byte, 1 = short, 2 = long) */
1236 16, /* bitsize */
1237 FALSE, /* pc_relative */
1238 0, /* bitpos */
1239 complain_overflow_dont, /* complain_on_overflow */
1240 ppc_elf_unhandled_reloc, /* special_function */
1241 "R_PPC_GOT_DTPREL16_HA", /* name */
1242 FALSE, /* partial_inplace */
1243 0, /* src_mask */
1244 0xffff, /* dst_mask */
1245 FALSE), /* pcrel_offset */
c3668558 1246
7619e7c7
AM
1247 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1248 offset to the entry. */
1249 HOWTO (R_PPC_GOT_TPREL16,
1250 0, /* rightshift */
1251 1, /* size (0 = byte, 1 = short, 2 = long) */
1252 16, /* bitsize */
1253 FALSE, /* pc_relative */
1254 0, /* bitpos */
1255 complain_overflow_signed, /* complain_on_overflow */
1256 ppc_elf_unhandled_reloc, /* special_function */
1257 "R_PPC_GOT_TPREL16", /* name */
1258 FALSE, /* partial_inplace */
1259 0, /* src_mask */
1260 0xffff, /* dst_mask */
1261 FALSE), /* pcrel_offset */
1262
1263 /* Like GOT_TPREL16, but no overflow. */
1264 HOWTO (R_PPC_GOT_TPREL16_LO,
1265 0, /* rightshift */
1266 1, /* size (0 = byte, 1 = short, 2 = long) */
1267 16, /* bitsize */
1268 FALSE, /* pc_relative */
1269 0, /* bitpos */
1270 complain_overflow_dont, /* complain_on_overflow */
1271 ppc_elf_unhandled_reloc, /* special_function */
1272 "R_PPC_GOT_TPREL16_LO", /* name */
1273 FALSE, /* partial_inplace */
1274 0, /* src_mask */
1275 0xffff, /* dst_mask */
1276 FALSE), /* pcrel_offset */
1277
1278 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1279 HOWTO (R_PPC_GOT_TPREL16_HI,
1280 16, /* rightshift */
1281 1, /* size (0 = byte, 1 = short, 2 = long) */
1282 16, /* bitsize */
1283 FALSE, /* pc_relative */
1284 0, /* bitpos */
1285 complain_overflow_dont, /* complain_on_overflow */
1286 ppc_elf_unhandled_reloc, /* special_function */
1287 "R_PPC_GOT_TPREL16_HI", /* name */
1288 FALSE, /* partial_inplace */
1289 0, /* src_mask */
1290 0xffff, /* dst_mask */
1291 FALSE), /* pcrel_offset */
1292
1293 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1294 HOWTO (R_PPC_GOT_TPREL16_HA,
1295 16, /* rightshift */
1296 1, /* size (0 = byte, 1 = short, 2 = long) */
1297 16, /* bitsize */
1298 FALSE, /* pc_relative */
1299 0, /* bitpos */
1300 complain_overflow_dont, /* complain_on_overflow */
1301 ppc_elf_unhandled_reloc, /* special_function */
1302 "R_PPC_GOT_TPREL16_HA", /* name */
1303 FALSE, /* partial_inplace */
1304 0, /* src_mask */
1305 0xffff, /* dst_mask */
1306 FALSE), /* pcrel_offset */
1307
1308 /* The remaining relocs are from the Embedded ELF ABI, and are not
1309 in the SVR4 ELF ABI. */
1310
1311 /* 32 bit value resulting from the addend minus the symbol. */
1312 HOWTO (R_PPC_EMB_NADDR32, /* type */
1313 0, /* rightshift */
1314 2, /* size (0 = byte, 1 = short, 2 = long) */
1315 32, /* bitsize */
1316 FALSE, /* pc_relative */
1317 0, /* bitpos */
1318 complain_overflow_bitfield, /* complain_on_overflow */
1319 bfd_elf_generic_reloc, /* special_function */
1320 "R_PPC_EMB_NADDR32", /* name */
1321 FALSE, /* partial_inplace */
1322 0, /* src_mask */
1323 0xffffffff, /* dst_mask */
1324 FALSE), /* pcrel_offset */
1325
1326 /* 16 bit value resulting from the addend minus the symbol. */
1327 HOWTO (R_PPC_EMB_NADDR16, /* type */
1328 0, /* rightshift */
1329 1, /* size (0 = byte, 1 = short, 2 = long) */
1330 16, /* bitsize */
1331 FALSE, /* pc_relative */
1332 0, /* bitpos */
1333 complain_overflow_bitfield, /* complain_on_overflow */
1334 bfd_elf_generic_reloc, /* special_function */
1335 "R_PPC_EMB_NADDR16", /* name */
1336 FALSE, /* partial_inplace */
1337 0, /* src_mask */
1338 0xffff, /* dst_mask */
1339 FALSE), /* pcrel_offset */
1340
1341 /* 16 bit value resulting from the addend minus the symbol. */
1342 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1343 0, /* rightshift */
1344 1, /* size (0 = byte, 1 = short, 2 = long) */
1345 16, /* bitsize */
1346 FALSE, /* pc_relative */
1347 0, /* bitpos */
1348 complain_overflow_dont,/* complain_on_overflow */
1349 bfd_elf_generic_reloc, /* special_function */
1350 "R_PPC_EMB_ADDR16_LO", /* name */
1351 FALSE, /* partial_inplace */
1352 0, /* src_mask */
1353 0xffff, /* dst_mask */
1354 FALSE), /* pcrel_offset */
1355
1356 /* The high order 16 bits of the addend minus the symbol. */
1357 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1358 16, /* rightshift */
1359 1, /* size (0 = byte, 1 = short, 2 = long) */
1360 16, /* bitsize */
1361 FALSE, /* pc_relative */
1362 0, /* bitpos */
1363 complain_overflow_dont, /* complain_on_overflow */
1364 bfd_elf_generic_reloc, /* special_function */
1365 "R_PPC_EMB_NADDR16_HI", /* name */
1366 FALSE, /* partial_inplace */
1367 0, /* src_mask */
1368 0xffff, /* dst_mask */
1369 FALSE), /* pcrel_offset */
1370
1371 /* The high order 16 bits of the result of the addend minus the address,
1372 plus 1 if the contents of the low 16 bits, treated as a signed number,
1373 is negative. */
1374 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1375 16, /* rightshift */
1376 1, /* size (0 = byte, 1 = short, 2 = long) */
1377 16, /* bitsize */
1378 FALSE, /* pc_relative */
1379 0, /* bitpos */
1380 complain_overflow_dont, /* complain_on_overflow */
1381 ppc_elf_addr16_ha_reloc, /* special_function */
1382 "R_PPC_EMB_NADDR16_HA", /* name */
1383 FALSE, /* partial_inplace */
1384 0, /* src_mask */
1385 0xffff, /* dst_mask */
1386 FALSE), /* pcrel_offset */
1387
1388 /* 16 bit value resulting from allocating a 4 byte word to hold an
1389 address in the .sdata section, and returning the offset from
1390 _SDA_BASE_ for that relocation. */
1391 HOWTO (R_PPC_EMB_SDAI16, /* type */
1392 0, /* rightshift */
1393 1, /* size (0 = byte, 1 = short, 2 = long) */
1394 16, /* bitsize */
1395 FALSE, /* pc_relative */
1396 0, /* bitpos */
1397 complain_overflow_bitfield, /* complain_on_overflow */
1398 bfd_elf_generic_reloc, /* special_function */
1399 "R_PPC_EMB_SDAI16", /* name */
1400 FALSE, /* partial_inplace */
1401 0, /* src_mask */
1402 0xffff, /* dst_mask */
1403 FALSE), /* pcrel_offset */
1404
1405 /* 16 bit value resulting from allocating a 4 byte word to hold an
1406 address in the .sdata2 section, and returning the offset from
1407 _SDA2_BASE_ for that relocation. */
1408 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1409 0, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 FALSE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_bitfield, /* complain_on_overflow */
1415 bfd_elf_generic_reloc, /* special_function */
1416 "R_PPC_EMB_SDA2I16", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1421
1422 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1423 small data items. */
1424 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1425 0, /* rightshift */
1426 1, /* size (0 = byte, 1 = short, 2 = long) */
1427 16, /* bitsize */
1428 FALSE, /* pc_relative */
1429 0, /* bitpos */
1430 complain_overflow_signed, /* complain_on_overflow */
1431 bfd_elf_generic_reloc, /* special_function */
1432 "R_PPC_EMB_SDA2REL", /* name */
1433 FALSE, /* partial_inplace */
1434 0, /* src_mask */
1435 0xffff, /* dst_mask */
1436 FALSE), /* pcrel_offset */
1437
1438 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1439 signed offset from the appropriate base, and filling in the register
1440 field with the appropriate register (0, 2, or 13). */
1441 HOWTO (R_PPC_EMB_SDA21, /* type */
1442 0, /* rightshift */
1443 2, /* size (0 = byte, 1 = short, 2 = long) */
1444 16, /* bitsize */
1445 FALSE, /* pc_relative */
1446 0, /* bitpos */
1447 complain_overflow_signed, /* complain_on_overflow */
1448 bfd_elf_generic_reloc, /* special_function */
1449 "R_PPC_EMB_SDA21", /* name */
1450 FALSE, /* partial_inplace */
1451 0, /* src_mask */
1452 0xffff, /* dst_mask */
1453 FALSE), /* pcrel_offset */
1454
1455 /* Relocation not handled: R_PPC_EMB_MRKREF */
1456 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1457 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1458 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1459 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1460 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1461
1462 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1463 in the 16 bit signed offset from the appropriate base, and filling in the
1464 register field with the appropriate register (0, 2, or 13). */
1465 HOWTO (R_PPC_EMB_RELSDA, /* type */
1466 0, /* rightshift */
1467 1, /* size (0 = byte, 1 = short, 2 = long) */
1468 16, /* bitsize */
1469 TRUE, /* pc_relative */
1470 0, /* bitpos */
1471 complain_overflow_signed, /* complain_on_overflow */
1472 bfd_elf_generic_reloc, /* special_function */
1473 "R_PPC_EMB_RELSDA", /* name */
1474 FALSE, /* partial_inplace */
1475 0, /* src_mask */
1476 0xffff, /* dst_mask */
1477 FALSE), /* pcrel_offset */
1478
1479 /* GNU extension to record C++ vtable hierarchy. */
1480 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1481 0, /* rightshift */
1482 0, /* size (0 = byte, 1 = short, 2 = long) */
1483 0, /* bitsize */
1484 FALSE, /* pc_relative */
1485 0, /* bitpos */
1486 complain_overflow_dont, /* complain_on_overflow */
1487 NULL, /* special_function */
1488 "R_PPC_GNU_VTINHERIT", /* name */
1489 FALSE, /* partial_inplace */
1490 0, /* src_mask */
1491 0, /* dst_mask */
1492 FALSE), /* pcrel_offset */
1493
1494 /* GNU extension to record C++ vtable member usage. */
1495 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1496 0, /* rightshift */
1497 0, /* size (0 = byte, 1 = short, 2 = long) */
1498 0, /* bitsize */
1499 FALSE, /* pc_relative */
1500 0, /* bitpos */
1501 complain_overflow_dont, /* complain_on_overflow */
1502 NULL, /* special_function */
1503 "R_PPC_GNU_VTENTRY", /* name */
1504 FALSE, /* partial_inplace */
1505 0, /* src_mask */
1506 0, /* dst_mask */
1507 FALSE), /* pcrel_offset */
1508
1509 /* Phony reloc to handle AIX style TOC entries. */
1510 HOWTO (R_PPC_TOC16, /* type */
1511 0, /* rightshift */
1512 1, /* size (0 = byte, 1 = short, 2 = long) */
1513 16, /* bitsize */
1514 FALSE, /* pc_relative */
1515 0, /* bitpos */
1516 complain_overflow_signed, /* complain_on_overflow */
1517 bfd_elf_generic_reloc, /* special_function */
1518 "R_PPC_TOC16", /* name */
1519 FALSE, /* partial_inplace */
1520 0, /* src_mask */
1521 0xffff, /* dst_mask */
1522 FALSE), /* pcrel_offset */
1523};
1524\f
1525/* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1526
1527static void
55fd94b0 1528ppc_elf_howto_init (void)
7619e7c7
AM
1529{
1530 unsigned int i, type;
1531
fc0bffd6
AM
1532 for (i = 0;
1533 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1534 i++)
7619e7c7
AM
1535 {
1536 type = ppc_elf_howto_raw[i].type;
fc0bffd6
AM
1537 if (type >= (sizeof (ppc_elf_howto_table)
1538 / sizeof (ppc_elf_howto_table[0])))
1539 abort ();
7619e7c7
AM
1540 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1541 }
1542}
1543\f
1544/* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
1545
1546 The MPC860, revision C0 or earlier contains a bug in the die.
1547 If all of the following conditions are true, the next instruction
1548 to be executed *may* be treated as a no-op.
1549 1/ A forward branch is executed.
1550 2/ The branch is predicted as not taken.
1551 3/ The branch is taken.
1552 4/ The branch is located in the last 5 words of a page.
fc0bffd6
AM
1553 (The EOP limit is 5 by default but may be specified as any value
1554 from 1-10.)
7619e7c7
AM
1555
1556 Our software solution is to detect these problematic branches in a
1557 linker pass and modify them as follows:
1558 1/ Unconditional branches - Since these are always predicted taken,
252b5132
RH
1559 there is no problem and no action is required.
1560 2/ Conditional backward branches - No problem, no action required.
1561 3/ Conditional forward branches - Ensure that the "inverse prediction
1562 bit" is set (ensure it is predicted taken).
1563 4/ Conditional register branches - Ensure that the "y bit" is set
ae9a127f 1564 (ensure it is predicted taken). */
252b5132
RH
1565
1566/* Sort sections by address. */
1567
1568static int
55fd94b0 1569ppc_elf_sort_rela (const void *arg1, const void *arg2)
252b5132 1570{
55fd94b0
AM
1571 const Elf_Internal_Rela * const *rela1 = arg1;
1572 const Elf_Internal_Rela * const *rela2 = arg2;
252b5132 1573
c3668558 1574 /* Sort by offset. */
252b5132
RH
1575 return ((*rela1)->r_offset - (*rela2)->r_offset);
1576}
1577
b34976b6 1578static bfd_boolean
55fd94b0
AM
1579ppc_elf_relax_section (bfd *abfd,
1580 asection *isec,
1581 struct bfd_link_info *link_info,
1582 bfd_boolean *again)
252b5132
RH
1583{
1584#define PAGESIZE 0x1000
1585
1586 bfd_byte *contents = NULL;
1587 bfd_byte *free_contents = NULL;
1588 Elf_Internal_Rela *internal_relocs = NULL;
1589 Elf_Internal_Rela *free_relocs = NULL;
1590 Elf_Internal_Rela **rela_comb = NULL;
1591 int comb_curr, comb_count;
1592
c3668558 1593 /* We never have to do this more than once per input section. */
b34976b6 1594 *again = FALSE;
252b5132
RH
1595
1596 /* If needed, initialize this section's cooked size. */
1597 if (isec->_cooked_size == 0)
70bccea4 1598 isec->_cooked_size = isec->_raw_size;
252b5132
RH
1599
1600 /* We're only interested in text sections which overlap the
8da6118f 1601 troublesome area at the end of a page. */
252b5132
RH
1602 if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1603 {
1604 bfd_vma dot, end_page, end_section;
b34976b6 1605 bfd_boolean section_modified;
252b5132 1606
c3668558 1607 /* Get the section contents. */
252b5132
RH
1608 /* Get cached copy if it exists. */
1609 if (elf_section_data (isec)->this_hdr.contents != NULL)
70bccea4 1610 contents = elf_section_data (isec)->this_hdr.contents;
252b5132
RH
1611 else
1612 {
1613 /* Go get them off disk. */
55fd94b0 1614 contents = bfd_malloc (isec->_raw_size);
252b5132 1615 if (contents == NULL)
8da6118f 1616 goto error_return;
252b5132
RH
1617 free_contents = contents;
1618
1619 if (! bfd_get_section_contents (abfd, isec, contents,
1620 (file_ptr) 0, isec->_raw_size))
8da6118f 1621 goto error_return;
252b5132
RH
1622 }
1623
1624 comb_curr = 0;
1625 comb_count = 0;
1626 if (isec->reloc_count)
8da6118f 1627 {
70bccea4 1628 unsigned n;
dc810e39 1629 bfd_size_type amt;
252b5132 1630
70bccea4
AM
1631 /* Get a copy of the native relocations. */
1632 internal_relocs
55fd94b0 1633 = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
45d6a902 1634 link_info->keep_memory);
70bccea4
AM
1635 if (internal_relocs == NULL)
1636 goto error_return;
1637 if (! link_info->keep_memory)
1638 free_relocs = internal_relocs;
c3668558 1639
70bccea4 1640 /* Setup a faster access method for the reloc info we need. */
dc810e39
AM
1641 amt = isec->reloc_count;
1642 amt *= sizeof (Elf_Internal_Rela*);
55fd94b0 1643 rela_comb = bfd_malloc (amt);
70bccea4
AM
1644 if (rela_comb == NULL)
1645 goto error_return;
1646 for (n = 0; n < isec->reloc_count; ++n)
1647 {
55fd94b0 1648 enum elf_ppc_reloc_type r_type;
70bccea4
AM
1649
1650 r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
55fd94b0 1651 if (r_type >= R_PPC_max)
70bccea4
AM
1652 goto error_return;
1653
1654 /* Prologue constants are sometimes present in the ".text"
1655 sections and they can be identified by their associated
1656 relocation. We don't want to process those words and
1657 some others which can also be identified by their
1658 relocations. However, not all conditional branches will
1659 have a relocation so we will only ignore words that
1660 1) have a reloc, and 2) the reloc is not applicable to a
1661 conditional branch. The array rela_comb is built here
1662 for use in the EOP scan loop. */
1663 switch (r_type)
1664 {
1665 case R_PPC_ADDR14_BRNTAKEN:
1666 case R_PPC_REL14:
1667 case R_PPC_REL14_BRNTAKEN:
1668 /* We should check the instruction. */
1669 break;
1670 default:
1671 /* The word is not a conditional branch - ignore it. */
1672 rela_comb[comb_count++] = &internal_relocs[n];
1673 break;
1674 }
1675 }
1676 if (comb_count > 1)
1677 qsort (rela_comb, (size_t) comb_count, sizeof (int),
1678 ppc_elf_sort_rela);
8da6118f 1679 }
252b5132 1680
c3668558 1681 /* Enumerate each EOP region that overlaps this section. */
252b5132
RH
1682 end_section = isec->vma + isec->_cooked_size;
1683 dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1684 dot -= link_info->mpc860c0;
b34976b6 1685 section_modified = FALSE;
70bccea4
AM
1686 /* Increment the start position if this section begins in the
1687 middle of its first EOP region. */
1688 if (dot < isec->vma)
1689 dot = isec->vma;
252b5132 1690 for (;
70bccea4
AM
1691 dot < end_section;
1692 dot += PAGESIZE, end_page += PAGESIZE)
1693 {
1694 /* Check each word in this EOP region. */
1695 for (; dot < end_page; dot += 4)
1696 {
1697 bfd_vma isec_offset;
1698 unsigned long insn;
1699 bfd_boolean skip, modified;
1700
1701 /* Don't process this word if there is a relocation for it
1702 and the relocation indicates the word is not a
1703 conditional branch. */
1704 skip = FALSE;
1705 isec_offset = dot - isec->vma;
1706 for (; comb_curr<comb_count; ++comb_curr)
1707 {
1708 bfd_vma r_offset;
1709
1710 r_offset = rela_comb[comb_curr]->r_offset;
1711 if (r_offset >= isec_offset)
1712 {
1713 if (r_offset == isec_offset) skip = TRUE;
1714 break;
1715 }
1716 }
1717 if (skip) continue;
1718
1719 /* Check the current word for a problematic conditional
1720 branch. */
252b5132
RH
1721#define BO0(insn) ((insn) & 0x02000000)
1722#define BO2(insn) ((insn) & 0x00800000)
1723#define BO4(insn) ((insn) & 0x00200000)
70bccea4
AM
1724 insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1725 modified = FALSE;
1726 if ((insn & 0xFc000000) == 0x40000000)
1727 {
1728 /* Instruction is BCx */
1729 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1730 {
1731 bfd_vma target;
252b5132 1732
70bccea4
AM
1733 /* This branch is predicted as "normal".
1734 If this is a forward branch, it is problematic. */
1735 target = insn & 0x0000Fffc;
1736 target = (target ^ 0x8000) - 0x8000;
1737 if ((insn & 0x00000002) == 0)
1738 /* Convert to abs. */
1739 target += dot;
1740 if (target > dot)
1741 {
1742 /* Set the prediction bit. */
1743 insn |= 0x00200000;
1744 modified = TRUE;
1745 }
1746 }
1747 }
1748 else if ((insn & 0xFc00Fffe) == 0x4c000420)
1749 {
1750 /* Instruction is BCCTRx. */
1751 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
252b5132
RH
1752 {
1753 /* This branch is predicted as not-taken.
ae9a127f 1754 If this is a forward branch, it is problematic.
70bccea4
AM
1755 Since we can't tell statically if it will branch
1756 forward, always set the prediction bit. */
1757 insn |= 0x00200000;
1758 modified = TRUE;
252b5132 1759 }
70bccea4
AM
1760 }
1761 else if ((insn & 0xFc00Fffe) == 0x4c000020)
1762 {
1763 /* Instruction is BCLRx */
1764 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
252b5132
RH
1765 {
1766 /* This branch is predicted as not-taken.
ae9a127f 1767 If this is a forward branch, it is problematic.
70bccea4
AM
1768 Since we can't tell statically if it will branch
1769 forward, always set the prediction bit. */
1770 insn |= 0x00200000;
1771 modified = TRUE;
252b5132 1772 }
70bccea4 1773 }
252b5132
RH
1774#undef BO0
1775#undef BO2
1776#undef BO4
70bccea4
AM
1777 if (modified)
1778 {
55fd94b0 1779 bfd_put_32 (abfd, insn, contents + isec_offset);
b34976b6 1780 section_modified = TRUE;
70bccea4
AM
1781 }
1782 }
1783 }
252b5132
RH
1784 if (section_modified)
1785 {
1786 elf_section_data (isec)->this_hdr.contents = contents;
1787 free_contents = NULL;
1788 }
1789 }
1790
1791 if (rela_comb != NULL)
1792 {
1793 free (rela_comb);
1794 rela_comb = NULL;
1795 }
1796
1797 if (free_relocs != NULL)
1798 {
1799 free (free_relocs);
1800 free_relocs = NULL;
1801 }
1802
1803 if (free_contents != NULL)
1804 {
1805 if (! link_info->keep_memory)
1806 free (free_contents);
1807 else
1808 {
1809 /* Cache the section contents for elf_link_input_bfd. */
1810 elf_section_data (isec)->this_hdr.contents = contents;
1811 }
1812 free_contents = NULL;
1813 }
1814
b34976b6 1815 return TRUE;
252b5132 1816
70bccea4 1817 error_return:
252b5132
RH
1818 if (rela_comb != NULL)
1819 free (rela_comb);
1820 if (free_relocs != NULL)
1821 free (free_relocs);
1822 if (free_contents != NULL)
1823 free (free_contents);
b34976b6 1824 return FALSE;
252b5132 1825}
252b5132
RH
1826\f
1827static reloc_howto_type *
55fd94b0
AM
1828ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1829 bfd_reloc_code_real_type code)
252b5132 1830{
7619e7c7 1831 enum elf_ppc_reloc_type r;
252b5132
RH
1832
1833 if (!ppc_elf_howto_table[R_PPC_ADDR32])
8da6118f 1834 /* Initialize howto table if needed. */
252b5132
RH
1835 ppc_elf_howto_init ();
1836
55fd94b0 1837 switch (code)
252b5132
RH
1838 {
1839 default:
55fd94b0 1840 return NULL;
252b5132 1841
7619e7c7
AM
1842 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
1843 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
1844 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
1845 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1846 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
1847 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
1848 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
1849 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
1850 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
1851 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
1852 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
1853 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
1854 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
1855 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
1856 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1857 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
1858 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
1859 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
1860 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
1861 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
1862 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
1863 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
1864 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
1865 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
1866 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
1867 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
1868 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
1869 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
1870 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
1871 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1872 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
1873 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
1874 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
1875 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
1876 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
1877 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
1878 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1879 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1880 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
1881 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
1882 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
1883 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
1884 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1885 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
1886 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
1887 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
1888 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
1889 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
1890 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
1891 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
1892 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
1893 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
1894 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
1895 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
1896 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
1897 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
1898 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
1899 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
1900 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
1901 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
1902 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
1903 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
1904 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
1905 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
1906 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
1907 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
1908 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
1909 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
1910 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
1911 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
1912 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
1913 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
1914 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
1915 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
1916 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
1917 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
1918 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
1919 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
1920 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
1921 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
1922 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
252b5132
RH
1923 }
1924
55fd94b0 1925 return ppc_elf_howto_table[r];
252b5132
RH
1926};
1927
1928/* Set the howto pointer for a PowerPC ELF reloc. */
1929
1930static void
55fd94b0
AM
1931ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1932 arelent *cache_ptr,
1933 Elf_Internal_Rela *dst)
252b5132 1934{
8da6118f
KH
1935 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1936 /* Initialize howto table if needed. */
252b5132
RH
1937 ppc_elf_howto_init ();
1938
1939 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1940 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1941}
1942
1943/* Handle the R_PPC_ADDR16_HA reloc. */
1944
1945static bfd_reloc_status_type
55fd94b0
AM
1946ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1947 arelent *reloc_entry,
1948 asymbol *symbol,
1949 void *data ATTRIBUTE_UNUSED,
1950 asection *input_section,
1951 bfd *output_bfd,
1952 char **error_message ATTRIBUTE_UNUSED)
252b5132
RH
1953{
1954 bfd_vma relocation;
1955
1956 if (output_bfd != NULL)
1957 {
1958 reloc_entry->address += input_section->output_offset;
1959 return bfd_reloc_ok;
1960 }
1961
1962 if (reloc_entry->address > input_section->_cooked_size)
1963 return bfd_reloc_outofrange;
1964
1965 if (bfd_is_com_section (symbol->section))
1966 relocation = 0;
1967 else
1968 relocation = symbol->value;
1969
1970 relocation += symbol->section->output_section->vma;
1971 relocation += symbol->section->output_offset;
1972 relocation += reloc_entry->addend;
1973
1974 reloc_entry->addend += (relocation & 0x8000) << 1;
1975
1976 return bfd_reloc_continue;
1977}
1978
7619e7c7 1979static bfd_reloc_status_type
55fd94b0
AM
1980ppc_elf_unhandled_reloc (bfd *abfd,
1981 arelent *reloc_entry,
1982 asymbol *symbol,
1983 void *data,
1984 asection *input_section,
1985 bfd *output_bfd,
1986 char **error_message)
7619e7c7
AM
1987{
1988 /* If this is a relocatable link (output_bfd test tells us), just
1989 call the generic function. Any adjustment will be done at final
1990 link time. */
1991 if (output_bfd != NULL)
1992 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1993 input_section, output_bfd, error_message);
1994
1995 if (error_message != NULL)
1996 {
1997 static char buf[60];
55fd94b0 1998 sprintf (buf, _("generic linker can't handle %s"),
7619e7c7
AM
1999 reloc_entry->howto->name);
2000 *error_message = buf;
2001 }
2002 return bfd_reloc_dangerous;
2003}
2004
feee612b
AM
2005/* Fix bad default arch selected for a 32 bit input bfd when the
2006 default is 64 bit. */
2007
b34976b6 2008static bfd_boolean
55fd94b0 2009ppc_elf_object_p (bfd *abfd)
feee612b
AM
2010{
2011 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
2012 {
2013 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2014
2015 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
2016 {
2017 /* Relies on arch after 64 bit default being 32 bit default. */
2018 abfd->arch_info = abfd->arch_info->next;
2019 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
2020 }
2021 }
b34976b6 2022 return TRUE;
feee612b
AM
2023}
2024
c3668558 2025/* Function to set whether a module needs the -mrelocatable bit set. */
252b5132 2026
b34976b6 2027static bfd_boolean
55fd94b0 2028ppc_elf_set_private_flags (bfd *abfd, flagword flags)
252b5132
RH
2029{
2030 BFD_ASSERT (!elf_flags_init (abfd)
2031 || elf_elfheader (abfd)->e_flags == flags);
2032
2033 elf_elfheader (abfd)->e_flags = flags;
b34976b6
AM
2034 elf_flags_init (abfd) = TRUE;
2035 return TRUE;
252b5132
RH
2036}
2037
252b5132 2038/* Merge backend specific data from an object file to the output
ae9a127f
NC
2039 object file when linking. */
2040
b34976b6 2041static bfd_boolean
55fd94b0 2042ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
252b5132
RH
2043{
2044 flagword old_flags;
2045 flagword new_flags;
b34976b6 2046 bfd_boolean error;
252b5132 2047
ae9a127f 2048 /* Check if we have the same endianess. */
82e51918 2049 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
b34976b6 2050 return FALSE;
252b5132
RH
2051
2052 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2053 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
b34976b6 2054 return TRUE;
252b5132
RH
2055
2056 new_flags = elf_elfheader (ibfd)->e_flags;
2057 old_flags = elf_elfheader (obfd)->e_flags;
70bccea4 2058 if (!elf_flags_init (obfd))
252b5132 2059 {
70bccea4 2060 /* First call, no flags set. */
b34976b6 2061 elf_flags_init (obfd) = TRUE;
252b5132
RH
2062 elf_elfheader (obfd)->e_flags = new_flags;
2063 }
2064
70bccea4
AM
2065 /* Compatible flags are ok. */
2066 else if (new_flags == old_flags)
252b5132
RH
2067 ;
2068
70bccea4
AM
2069 /* Incompatible flags. */
2070 else
252b5132 2071 {
70bccea4
AM
2072 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
2073 to be linked with either. */
b34976b6 2074 error = FALSE;
252b5132
RH
2075 if ((new_flags & EF_PPC_RELOCATABLE) != 0
2076 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
2077 {
b34976b6 2078 error = TRUE;
252b5132 2079 (*_bfd_error_handler)
55fd94b0
AM
2080 (_("%s: compiled with -mrelocatable and linked with "
2081 "modules compiled normally"),
8f615d07 2082 bfd_archive_filename (ibfd));
252b5132
RH
2083 }
2084 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
2085 && (old_flags & EF_PPC_RELOCATABLE) != 0)
2086 {
b34976b6 2087 error = TRUE;
252b5132 2088 (*_bfd_error_handler)
55fd94b0
AM
2089 (_("%s: compiled normally and linked with "
2090 "modules compiled with -mrelocatable"),
8f615d07 2091 bfd_archive_filename (ibfd));
252b5132
RH
2092 }
2093
2094 /* The output is -mrelocatable-lib iff both the input files are. */
2095 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
2096 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
2097
2098 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
70bccea4 2099 but each input file is either -mrelocatable or -mrelocatable-lib. */
252b5132
RH
2100 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
2101 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
2102 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
2103 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
2104
70bccea4
AM
2105 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
2106 any module uses it. */
252b5132
RH
2107 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
2108
70bccea4
AM
2109 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2110 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
252b5132 2111
ae9a127f 2112 /* Warn about any other mismatches. */
252b5132
RH
2113 if (new_flags != old_flags)
2114 {
b34976b6 2115 error = TRUE;
252b5132 2116 (*_bfd_error_handler)
55fd94b0
AM
2117 (_("%s: uses different e_flags (0x%lx) fields "
2118 "than previous modules (0x%lx)"),
8f615d07 2119 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
252b5132
RH
2120 }
2121
2122 if (error)
2123 {
2124 bfd_set_error (bfd_error_bad_value);
b34976b6 2125 return FALSE;
252b5132
RH
2126 }
2127 }
2128
b34976b6 2129 return TRUE;
252b5132 2130}
252b5132
RH
2131\f
2132/* Handle a PowerPC specific section when reading an object file. This
2133 is called when elfcode.h finds a section with an unknown type. */
2134
b34976b6 2135static bfd_boolean
55fd94b0 2136ppc_elf_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr, const char *name)
252b5132
RH
2137{
2138 asection *newsect;
2139 flagword flags;
2140
2141 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
b34976b6 2142 return FALSE;
252b5132
RH
2143
2144 newsect = hdr->bfd_section;
2145 flags = bfd_get_section_flags (abfd, newsect);
2146 if (hdr->sh_flags & SHF_EXCLUDE)
2147 flags |= SEC_EXCLUDE;
2148
2149 if (hdr->sh_type == SHT_ORDERED)
2150 flags |= SEC_SORT_ENTRIES;
2151
2152 bfd_set_section_flags (abfd, newsect, flags);
b34976b6 2153 return TRUE;
252b5132 2154}
252b5132
RH
2155\f
2156/* Set up any other section flags and such that may be necessary. */
2157
b34976b6 2158static bfd_boolean
55fd94b0
AM
2159ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2160 Elf_Internal_Shdr *shdr,
2161 asection *asect)
252b5132
RH
2162{
2163 if ((asect->flags & SEC_EXCLUDE) != 0)
2164 shdr->sh_flags |= SHF_EXCLUDE;
2165
2166 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2167 shdr->sh_type = SHT_ORDERED;
2168
b34976b6 2169 return TRUE;
252b5132 2170}
252b5132
RH
2171\f
2172/* Create a special linker section */
2173static elf_linker_section_t *
55fd94b0
AM
2174ppc_elf_create_linker_section (bfd *abfd,
2175 struct bfd_link_info *info,
2176 enum elf_linker_section_enum which)
252b5132
RH
2177{
2178 bfd *dynobj = elf_hash_table (info)->dynobj;
2179 elf_linker_section_t *lsect;
2180
ae9a127f 2181 /* Record the first bfd section that needs the special section. */
252b5132
RH
2182 if (!dynobj)
2183 dynobj = elf_hash_table (info)->dynobj = abfd;
2184
ae9a127f 2185 /* If this is the first time, create the section. */
252b5132
RH
2186 lsect = elf_linker_section (dynobj, which);
2187 if (!lsect)
2188 {
2189 elf_linker_section_t defaults;
2190 static elf_linker_section_t zero_section;
2191
2192 defaults = zero_section;
2193 defaults.which = which;
b34976b6 2194 defaults.hole_written_p = FALSE;
252b5132
RH
2195 defaults.alignment = 2;
2196
2197 /* Both of these sections are (technically) created by the user
2198 putting data in them, so they shouldn't be marked
2199 SEC_LINKER_CREATED.
2200
2201 The linker creates them so it has somewhere to attach their
2202 respective symbols. In fact, if they were empty it would
2203 be OK to leave the symbol set to 0 (or any random number), because
2204 the appropriate register should never be used. */
2205 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2206 | SEC_IN_MEMORY);
2207
2208 switch (which)
2209 {
2210 default:
7b609f53 2211 (*_bfd_error_handler) (_("%s: unknown special linker type %d"),
252b5132 2212 bfd_get_filename (abfd),
8da6118f 2213 (int) which);
252b5132
RH
2214
2215 bfd_set_error (bfd_error_bad_value);
55fd94b0 2216 return NULL;
252b5132
RH
2217
2218 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
2219 defaults.name = ".sdata";
2220 defaults.rel_name = ".rela.sdata";
2221 defaults.bss_name = ".sbss";
2222 defaults.sym_name = "_SDA_BASE_";
2223 defaults.sym_offset = 32768;
2224 break;
2225
2226 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2227 defaults.name = ".sdata2";
2228 defaults.rel_name = ".rela.sdata2";
2229 defaults.bss_name = ".sbss2";
2230 defaults.sym_name = "_SDA2_BASE_";
2231 defaults.sym_offset = 32768;
2232 defaults.flags |= SEC_READONLY;
2233 break;
2234 }
2235
2236 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
2237 }
2238
2239 return lsect;
2240}
252b5132 2241\f
8da6118f
KH
2242/* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
2243 need to bump up the number of section headers. */
252b5132
RH
2244
2245static int
55fd94b0 2246ppc_elf_additional_program_headers (bfd *abfd)
252b5132
RH
2247{
2248 asection *s;
2249 int ret;
2250
2251 ret = 0;
2252
2253 s = bfd_get_section_by_name (abfd, ".interp");
2254 if (s != NULL)
2255 ++ret;
2256
2257 s = bfd_get_section_by_name (abfd, ".sbss2");
2258 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2259 ++ret;
2260
2261 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2262 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2263 ++ret;
2264
2265 return ret;
2266}
2267
8da6118f 2268/* Modify the segment map if needed. */
252b5132 2269
b34976b6 2270static bfd_boolean
55fd94b0 2271ppc_elf_modify_segment_map (bfd *abfd ATTRIBUTE_UNUSED)
252b5132 2272{
b34976b6 2273 return TRUE;
252b5132
RH
2274}
2275\f
41fcb14e
AM
2276/* The powerpc .got has a blrl instruction in it. Mark it executable. */
2277
7619e7c7 2278static bfd_boolean
55fd94b0 2279ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
41fcb14e 2280{
7619e7c7
AM
2281 struct ppc_elf_link_hash_table *htab;
2282 asection *s;
41fcb14e
AM
2283 flagword flags;
2284
2285 if (!_bfd_elf_create_got_section (abfd, info))
7619e7c7 2286 return FALSE;
41fcb14e 2287
7619e7c7
AM
2288 htab = ppc_elf_hash_table (info);
2289 htab->got = s = bfd_get_section_by_name (abfd, ".got");
41fcb14e
AM
2290 if (s == NULL)
2291 abort ();
2292
2293 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2294 | SEC_LINKER_CREATED);
2295 if (!bfd_set_section_flags (abfd, s, flags))
7619e7c7
AM
2296 return FALSE;
2297
2298 htab->relgot = bfd_make_section (abfd, ".rela.got");
2299 if (!htab->relgot
2300 || ! bfd_set_section_flags (abfd, htab->relgot,
2301 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2302 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2303 | SEC_READONLY))
2304 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2305 return FALSE;
2306
2307 return TRUE;
41fcb14e
AM
2308}
2309
252b5132
RH
2310/* We have to create .dynsbss and .rela.sbss here so that they get mapped
2311 to output sections (just like _bfd_elf_create_dynamic_sections has
2312 to create .dynbss and .rela.bss). */
2313
b34976b6 2314static bfd_boolean
55fd94b0 2315ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
252b5132 2316{
7619e7c7
AM
2317 struct ppc_elf_link_hash_table *htab;
2318 asection *s;
252b5132
RH
2319 flagword flags;
2320
41fcb14e 2321 if (!ppc_elf_create_got (abfd, info))
b34976b6 2322 return FALSE;
41fcb14e 2323
8da6118f 2324 if (!_bfd_elf_create_dynamic_sections (abfd, info))
b34976b6 2325 return FALSE;
252b5132
RH
2326
2327 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2328 | SEC_LINKER_CREATED);
2329
7619e7c7
AM
2330 htab = ppc_elf_hash_table (info);
2331 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2332 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
252b5132
RH
2333 if (s == NULL
2334 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
b34976b6 2335 return FALSE;
252b5132
RH
2336
2337 if (! info->shared)
2338 {
7619e7c7
AM
2339 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2340 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
252b5132
RH
2341 if (s == NULL
2342 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2343 || ! bfd_set_section_alignment (abfd, s, 2))
b34976b6 2344 return FALSE;
252b5132 2345 }
41fcb14e 2346
7619e7c7
AM
2347 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2348 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
41fcb14e
AM
2349 if (s == NULL)
2350 abort ();
2351
2352 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2353 return bfd_set_section_flags (abfd, s, flags);
252b5132
RH
2354}
2355
2356/* Adjust a symbol defined by a dynamic object and referenced by a
2357 regular object. The current definition is in some section of the
2358 dynamic object, but we're not including those sections. We have to
2359 change the definition to something the rest of the link can
2360 understand. */
2361
b34976b6 2362static bfd_boolean
55fd94b0
AM
2363ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2364 struct elf_link_hash_entry *h)
252b5132 2365{
7619e7c7 2366 struct ppc_elf_link_hash_table *htab;
252b5132
RH
2367 asection *s;
2368 unsigned int power_of_two;
252b5132
RH
2369
2370#ifdef DEBUG
70bccea4
AM
2371 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
2372 h->root.root.string);
252b5132
RH
2373#endif
2374
2375 /* Make sure we know what is going on here. */
7619e7c7
AM
2376 htab = ppc_elf_hash_table (info);
2377 BFD_ASSERT (htab->elf.dynobj != NULL
252b5132
RH
2378 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2379 || h->weakdef != NULL
2380 || ((h->elf_link_hash_flags
2381 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2382 && (h->elf_link_hash_flags
2383 & ELF_LINK_HASH_REF_REGULAR) != 0
2384 && (h->elf_link_hash_flags
2385 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2386
7619e7c7 2387 /* Deal with function syms. */
252b5132
RH
2388 if (h->type == STT_FUNC
2389 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2390 {
7619e7c7
AM
2391 /* Clear procedure linkage table information for any symbol that
2392 won't need a .plt entry. */
9c7a29a3 2393 if (h->plt.refcount <= 0
fc0bffd6 2394 || SYMBOL_CALLS_LOCAL (info, h)
9c7a29a3
AM
2395 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2396 && h->root.type == bfd_link_hash_undefweak))
252b5132
RH
2397 {
2398 /* A PLT entry is not required/allowed when:
2399
9c7a29a3
AM
2400 1. We are not using ld.so; because then the PLT entry
2401 can't be set up, so we can't use one. In this case,
2402 ppc_elf_adjust_dynamic_symbol won't even be called.
252b5132 2403
9c7a29a3 2404 2. GC has rendered the entry unused.
252b5132 2405
9c7a29a3
AM
2406 3. We know for certain that a call to this symbol
2407 will go to this object, or will remain undefined. */
252b5132
RH
2408 h->plt.offset = (bfd_vma) -1;
2409 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
252b5132 2410 }
b34976b6 2411 return TRUE;
252b5132 2412 }
bbd7ec4a
AM
2413 else
2414 h->plt.offset = (bfd_vma) -1;
252b5132
RH
2415
2416 /* If this is a weak symbol, and there is a real definition, the
2417 processor independent code will have arranged for us to see the
2418 real definition first, and we can just use the same value. */
2419 if (h->weakdef != NULL)
2420 {
2421 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2422 || h->weakdef->root.type == bfd_link_hash_defweak);
2423 h->root.u.def.section = h->weakdef->root.u.def.section;
2424 h->root.u.def.value = h->weakdef->root.u.def.value;
a23b6845
AM
2425 if (ELIMINATE_COPY_RELOCS)
2426 h->elf_link_hash_flags
2427 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
2428 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
b34976b6 2429 return TRUE;
252b5132
RH
2430 }
2431
2432 /* This is a reference to a symbol defined by a dynamic object which
2433 is not a function. */
2434
2435 /* If we are creating a shared library, we must presume that the
2436 only references to the symbol are via the global offset table.
2437 For such cases we need not do anything here; the relocations will
2438 be handled correctly by relocate_section. */
2439 if (info->shared)
b34976b6 2440 return TRUE;
252b5132 2441
7619e7c7
AM
2442 /* If there are no references to this symbol that do not use the
2443 GOT, we don't need to generate a copy reloc. */
2444 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2445 return TRUE;
2446
ee05f2fe
AM
2447 if (ELIMINATE_COPY_RELOCS)
2448 {
2449 struct ppc_elf_dyn_relocs *p;
2450 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2451 {
2452 s = p->sec->output_section;
2453 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2454 break;
2455 }
2456
2457 /* If we didn't find any dynamic relocs in read-only sections, then
2458 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2459 if (p == NULL)
2460 {
2461 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2462 return TRUE;
2463 }
2464 }
2465
252b5132
RH
2466 /* We must allocate the symbol in our .dynbss section, which will
2467 become part of the .bss section of the executable. There will be
2468 an entry for this symbol in the .dynsym section. The dynamic
2469 object will contain position independent code, so all references
2470 from the dynamic object to this symbol will go through the global
2471 offset table. The dynamic linker will use the .dynsym entry to
2472 determine the address it must put in the global offset table, so
2473 both the dynamic object and the regular object will refer to the
2474 same memory location for the variable.
2475
2476 Of course, if the symbol is sufficiently small, we must instead
2477 allocate it in .sbss. FIXME: It would be better to do this if and
2478 only if there were actually SDAREL relocs for that symbol. */
2479
7619e7c7
AM
2480 if (h->size <= elf_gp_size (htab->elf.dynobj))
2481 s = htab->dynsbss;
252b5132 2482 else
7619e7c7 2483 s = htab->dynbss;
252b5132
RH
2484 BFD_ASSERT (s != NULL);
2485
2486 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2487 copy the initial value out of the dynamic object and into the
2488 runtime process image. We need to remember the offset into the
2489 .rela.bss section we are going to use. */
2490 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2491 {
2492 asection *srel;
2493
7619e7c7
AM
2494 if (h->size <= elf_gp_size (htab->elf.dynobj))
2495 srel = htab->relsbss;
252b5132 2496 else
7619e7c7 2497 srel = htab->relbss;
252b5132
RH
2498 BFD_ASSERT (srel != NULL);
2499 srel->_raw_size += sizeof (Elf32_External_Rela);
2500 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2501 }
2502
2503 /* We need to figure out the alignment required for this symbol. I
2504 have no idea how ELF linkers handle this. */
2505 power_of_two = bfd_log2 (h->size);
2506 if (power_of_two > 4)
2507 power_of_two = 4;
2508
2509 /* Apply the required alignment. */
2510 s->_raw_size = BFD_ALIGN (s->_raw_size,
2511 (bfd_size_type) (1 << power_of_two));
7619e7c7 2512 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
252b5132 2513 {
7619e7c7 2514 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
b34976b6 2515 return FALSE;
252b5132
RH
2516 }
2517
2518 /* Define the symbol as being at this point in the section. */
2519 h->root.u.def.section = s;
2520 h->root.u.def.value = s->_raw_size;
2521
2522 /* Increment the section size to make room for the symbol. */
2523 s->_raw_size += h->size;
2524
b34976b6 2525 return TRUE;
252b5132 2526}
252b5132 2527\f
7619e7c7
AM
2528/* This is the condition under which finish_dynamic_symbol will be
2529 called from elflink.h. If elflink.h doesn't call our
2530 finish_dynamic_symbol routine, we'll need to do something about
2531 initializing any .plt and .got entries in relocate_section. */
ee05f2fe 2532#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
7619e7c7 2533 ((DYN) \
ee05f2fe 2534 && ((SHARED) \
7619e7c7
AM
2535 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
2536 && ((H)->dynindx != -1 \
2537 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
2538
ee05f2fe
AM
2539/* Of those relocs that might be copied as dynamic relocs, this macro
2540 selects those that must be copied when linking a shared library,
2541 even when the symbol is local. */
2542
2543#define MUST_BE_DYN_RELOC(RTYPE) \
2544 ((RTYPE) != R_PPC_REL24 \
2545 && (RTYPE) != R_PPC_REL14 \
2546 && (RTYPE) != R_PPC_REL14_BRTAKEN \
2547 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
2548 && (RTYPE) != R_PPC_REL32)
2549
7fce784e
AS
2550/* Allocate space in associated reloc sections for dynamic relocs. */
2551
7619e7c7 2552static bfd_boolean
55fd94b0 2553allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7619e7c7 2554{
55fd94b0 2555 struct bfd_link_info *info = inf;
7619e7c7
AM
2556 struct ppc_elf_link_hash_entry *eh;
2557 struct ppc_elf_link_hash_table *htab;
2558 struct ppc_elf_dyn_relocs *p;
2559
2560 if (h->root.type == bfd_link_hash_indirect)
2561 return TRUE;
2562
2563 if (h->root.type == bfd_link_hash_warning)
2564 /* When warning symbols are created, they **replace** the "real"
2565 entry in the hash table, thus we never get to see the real
2566 symbol in a hash traversal. So look at it now. */
2567 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2568
2569 htab = ppc_elf_hash_table (info);
2570 if (htab->elf.dynamic_sections_created
9c7a29a3 2571 && h->plt.refcount > 0)
7619e7c7
AM
2572 {
2573 /* Make sure this symbol is output as a dynamic symbol. */
2574 if (h->dynindx == -1
2575 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2576 {
2577 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2578 return FALSE;
2579 }
2580
ee05f2fe
AM
2581 if (info->shared
2582 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
7619e7c7
AM
2583 {
2584 asection *s = htab->plt;
2585
2586 /* If this is the first .plt entry, make room for the special
2587 first entry. */
2588 if (s->_raw_size == 0)
2589 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
2590
2591 /* The PowerPC PLT is actually composed of two parts, the
2592 first part is 2 words (for a load and a jump), and then
2593 there is a remaining word available at the end. */
2594 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
2595 + (PLT_SLOT_SIZE
2596 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
2597 / PLT_ENTRY_SIZE)));
2598
2599 /* If this symbol is not defined in a regular file, and we
2600 are not generating a shared library, then set the symbol
2601 to this location in the .plt. This is required to make
2602 function pointers compare as equal between the normal
2603 executable and the shared library. */
2604 if (! info->shared
2605 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2606 {
2607 h->root.u.def.section = s;
2608 h->root.u.def.value = h->plt.offset;
2609 }
2610
2611 /* Make room for this entry. After the 8192nd entry, room
2612 for two entries is allocated. */
2613 s->_raw_size += PLT_ENTRY_SIZE;
2614 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
f2c822e3 2615 > PLT_NUM_SINGLE_ENTRIES)
7619e7c7
AM
2616 s->_raw_size += PLT_ENTRY_SIZE;
2617
2618 /* We also need to make an entry in the .rela.plt section. */
2619 htab->relplt->_raw_size += sizeof (Elf32_External_Rela);
2620 }
2621 else
2622 {
2623 h->plt.offset = (bfd_vma) -1;
2624 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2625 }
2626 }
2627 else
2628 {
2629 h->plt.offset = (bfd_vma) -1;
2630 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2631 }
7fce784e 2632
7619e7c7
AM
2633 eh = (struct ppc_elf_link_hash_entry *) h;
2634 if (eh->elf.got.refcount > 0)
2635 {
2636 /* Make sure this symbol is output as a dynamic symbol. */
2637 if (eh->elf.dynindx == -1
2638 && (eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2639 {
2640 if (!bfd_elf32_link_record_dynamic_symbol (info, &eh->elf))
2641 return FALSE;
2642 }
7fce784e 2643
7619e7c7
AM
2644 if (eh->tls_mask == (TLS_TLS | TLS_LD)
2645 && !(eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
2646 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
2647 eh->elf.got.offset = (bfd_vma) -1;
2648 else
2649 {
2650 bfd_boolean dyn;
2651 eh->elf.got.offset = htab->got->_raw_size;
2652 if ((eh->tls_mask & TLS_TLS) != 0)
2653 {
70bccea4
AM
2654 if ((eh->tls_mask & TLS_LD) != 0)
2655 htab->got->_raw_size += 8;
2656 if ((eh->tls_mask & TLS_GD) != 0)
7619e7c7
AM
2657 htab->got->_raw_size += 8;
2658 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
2659 htab->got->_raw_size += 4;
2660 if ((eh->tls_mask & TLS_DTPREL) != 0)
2661 htab->got->_raw_size += 4;
2662 }
2663 else
2664 htab->got->_raw_size += 4;
2665 dyn = htab->elf.dynamic_sections_created;
4e795f50
AM
2666 if ((info->shared
2667 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
2668 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
2669 || eh->elf.root.type != bfd_link_hash_undefweak))
7619e7c7
AM
2670 {
2671 /* All the entries we allocated need relocs. */
2672 htab->relgot->_raw_size
2673 += ((htab->got->_raw_size - eh->elf.got.offset) / 4
2674 * sizeof (Elf32_External_Rela));
2675 /* Except LD only needs one. */
2676 if ((eh->tls_mask & TLS_LD) != 0)
2677 htab->relgot->_raw_size -= sizeof (Elf32_External_Rela);
2678 }
2679 }
2680 }
2681 else
2682 eh->elf.got.offset = (bfd_vma) -1;
7fce784e 2683
ee05f2fe
AM
2684 if (eh->dyn_relocs == NULL)
2685 return TRUE;
2686
2687 /* In the shared -Bsymbolic case, discard space allocated for
2688 dynamic pc-relative relocs against symbols which turn out to be
2689 defined in regular objects. For the normal shared case, discard
2690 space for relocs that have become local due to symbol visibility
2691 changes. */
586119b3 2692
ee05f2fe
AM
2693 if (info->shared)
2694 {
9c7a29a3
AM
2695 /* Relocs that use pc_count are those that appear on a call insn,
2696 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
2697 generated via assembly. We want calls to protected symbols to
2698 resolve directly to the function rather than going via the plt.
2699 If people want function pointer comparisons to work as expected
55fd94b0 2700 then they should avoid writing weird assembly. */
09695f56 2701 if (SYMBOL_CALLS_LOCAL (info, h))
ee05f2fe
AM
2702 {
2703 struct ppc_elf_dyn_relocs **pp;
2704
2705 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2706 {
2707 p->count -= p->pc_count;
2708 p->pc_count = 0;
2709 if (p->count == 0)
2710 *pp = p->next;
2711 else
2712 pp = &p->next;
2713 }
2714 }
4e795f50
AM
2715
2716 /* Also discard relocs on undefined weak syms with non-default
2717 visibility. */
2718 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2719 && h->root.type == bfd_link_hash_undefweak)
2720 eh->dyn_relocs = NULL;
ee05f2fe
AM
2721 }
2722 else if (ELIMINATE_COPY_RELOCS)
2723 {
2724 /* For the non-shared case, discard space for relocs against
2725 symbols which turn out to need copy relocs or are not
2726 dynamic. */
2727
2728 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2729 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2730 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2731 {
2732 /* Make sure this symbol is output as a dynamic symbol.
2733 Undefined weak syms won't yet be marked as dynamic. */
2734 if (h->dynindx == -1
2735 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2736 {
2737 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
2738 return FALSE;
2739 }
2740
2741 /* If that succeeded, we know we'll be keeping all the
2742 relocs. */
2743 if (h->dynindx != -1)
2744 goto keep;
2745 }
2746
2747 eh->dyn_relocs = NULL;
2748
2749 keep: ;
2750 }
2751
2752 /* Finally, allocate space. */
7619e7c7 2753 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7fce784e
AS
2754 {
2755 asection *sreloc = elf_section_data (p->sec)->sreloc;
2756 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
2757 }
2758
2759 return TRUE;
2760}
2761
2762/* Find any dynamic relocs that apply to read-only sections. */
2763
2764static bfd_boolean
55fd94b0 2765readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
7fce784e
AS
2766{
2767 struct ppc_elf_dyn_relocs *p;
2768
2769 if (h->root.type == bfd_link_hash_indirect)
2770 return TRUE;
2771
2772 if (h->root.type == bfd_link_hash_warning)
2773 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2774
2775 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2776 {
2777 asection *s = p->sec->output_section;
2778
2779 if (s != NULL
2780 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
2781 == (SEC_READONLY | SEC_ALLOC)))
2782 {
2783 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
2784
2785 /* Not an error, just cut short the traversal. */
2786 return FALSE;
2787 }
2788 }
2789 return TRUE;
2790}
2791
252b5132
RH
2792/* Set the sizes of the dynamic sections. */
2793
b34976b6 2794static bfd_boolean
55fd94b0
AM
2795ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2796 struct bfd_link_info *info)
252b5132 2797{
7619e7c7 2798 struct ppc_elf_link_hash_table *htab;
252b5132 2799 asection *s;
b34976b6 2800 bfd_boolean relocs;
7fce784e 2801 bfd *ibfd;
252b5132
RH
2802
2803#ifdef DEBUG
2804 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
2805#endif
2806
7619e7c7
AM
2807 htab = ppc_elf_hash_table (info);
2808 BFD_ASSERT (htab->elf.dynobj != NULL);
252b5132
RH
2809
2810 if (elf_hash_table (info)->dynamic_sections_created)
2811 {
2812 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 2813 if (info->executable)
252b5132 2814 {
7619e7c7 2815 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
252b5132
RH
2816 BFD_ASSERT (s != NULL);
2817 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2818 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2819 }
2820 }
7619e7c7
AM
2821
2822 if (htab->tlsld_got.refcount > 0)
252b5132 2823 {
7619e7c7
AM
2824 htab->tlsld_got.offset = htab->got->_raw_size;
2825 htab->got->_raw_size += 8;
2826 if (info->shared)
2827 htab->relgot->_raw_size += sizeof (Elf32_External_Rela);
252b5132 2828 }
7619e7c7
AM
2829 else
2830 htab->tlsld_got.offset = (bfd_vma) -1;
252b5132 2831
7619e7c7
AM
2832 /* Set up .got offsets for local syms, and space for local dynamic
2833 relocs. */
7fce784e
AS
2834 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2835 {
7619e7c7
AM
2836 bfd_signed_vma *local_got;
2837 bfd_signed_vma *end_local_got;
2838 char *lgot_masks;
2839 bfd_size_type locsymcount;
2840 Elf_Internal_Shdr *symtab_hdr;
2841 asection *srel;
2842
7fce784e
AS
2843 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2844 continue;
2845
2846 for (s = ibfd->sections; s != NULL; s = s->next)
2847 {
2848 struct ppc_elf_dyn_relocs *p;
2849
2850 for (p = ((struct ppc_elf_dyn_relocs *)
70bccea4
AM
2851 elf_section_data (s)->local_dynrel);
2852 p != NULL;
2853 p = p->next)
7fce784e
AS
2854 {
2855 if (!bfd_is_abs_section (p->sec)
2856 && bfd_is_abs_section (p->sec->output_section))
2857 {
2858 /* Input section has been discarded, either because
2859 it is a copy of a linkonce section or due to
2860 linker script /DISCARD/, so we'll be discarding
2861 the relocs too. */
2862 }
2863 else if (p->count != 0)
2864 {
2865 elf_section_data (p->sec)->sreloc->_raw_size
2866 += p->count * sizeof (Elf32_External_Rela);
2867 if ((p->sec->output_section->flags
2868 & (SEC_READONLY | SEC_ALLOC))
2869 == (SEC_READONLY | SEC_ALLOC))
2870 info->flags |= DF_TEXTREL;
2871 }
2872 }
2873 }
7619e7c7
AM
2874
2875 local_got = elf_local_got_refcounts (ibfd);
2876 if (!local_got)
2877 continue;
2878
2879 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2880 locsymcount = symtab_hdr->sh_info;
2881 end_local_got = local_got + locsymcount;
2882 lgot_masks = (char *) end_local_got;
2883 s = htab->got;
2884 srel = htab->relgot;
2885 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
2886 if (*local_got > 0)
2887 {
2888 if (*lgot_masks == (TLS_TLS | TLS_LD))
2889 {
2890 /* If just an LD reloc, we'll just use
2891 htab->tlsld_got.offset. */
2892 if (htab->tlsld_got.offset == (bfd_vma) -1)
2893 {
2894 htab->tlsld_got.offset = s->_raw_size;
2895 s->_raw_size += 8;
2896 if (info->shared)
2897 srel->_raw_size += sizeof (Elf32_External_Rela);
2898 }
2899 *local_got = (bfd_vma) -1;
2900 }
2901 else
2902 {
2903 *local_got = s->_raw_size;
2904 if ((*lgot_masks & TLS_TLS) != 0)
2905 {
2906 if ((*lgot_masks & TLS_GD) != 0)
2907 s->_raw_size += 8;
2908 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
2909 s->_raw_size += 4;
2910 if ((*lgot_masks & TLS_DTPREL) != 0)
2911 s->_raw_size += 4;
2912 }
2913 else
2914 s->_raw_size += 4;
2915 if (info->shared)
2916 srel->_raw_size += ((s->_raw_size - *local_got) / 4
2917 * sizeof (Elf32_External_Rela));
2918 }
2919 }
2920 else
2921 *local_got = (bfd_vma) -1;
7fce784e
AS
2922 }
2923
2924 /* Allocate space for global sym dynamic relocs. */
7619e7c7 2925 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
7fce784e 2926
7619e7c7
AM
2927 /* We've now determined the sizes of the various dynamic sections.
2928 Allocate memory for them. */
b34976b6 2929 relocs = FALSE;
7619e7c7 2930 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
252b5132 2931 {
252b5132
RH
2932 if ((s->flags & SEC_LINKER_CREATED) == 0)
2933 continue;
2934
7619e7c7
AM
2935 if (s == htab->plt
2936 || s == htab->got
2937 || (htab->sdata != NULL && s == htab->sdata->section)
2938 || (htab->sdata2 != NULL && s == htab->sdata2->section))
252b5132 2939 {
7619e7c7
AM
2940 /* Strip this section if we don't need it; see the
2941 comment below. */
252b5132 2942 }
7619e7c7 2943 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
252b5132
RH
2944 {
2945 if (s->_raw_size == 0)
2946 {
2947 /* If we don't need this section, strip it from the
2948 output file. This is mostly to handle .rela.bss and
2949 .rela.plt. We must create both sections in
2950 create_dynamic_sections, because they must be created
2951 before the linker maps input sections to output
2952 sections. The linker does that before
2953 adjust_dynamic_symbol is called, and it is that
2954 function which decides whether anything needs to go
2955 into these sections. */
252b5132
RH
2956 }
2957 else
2958 {
c3668558 2959 /* Remember whether there are any relocation sections. */
b34976b6 2960 relocs = TRUE;
252b5132 2961
252b5132
RH
2962 /* We use the reloc_count field as a counter if we need
2963 to copy relocs into the output file. */
2964 s->reloc_count = 0;
2965 }
2966 }
7619e7c7 2967 else
252b5132
RH
2968 {
2969 /* It's not one of our sections, so don't allocate space. */
2970 continue;
2971 }
2972
7619e7c7 2973 if (s->_raw_size == 0)
252b5132 2974 {
7f8d5fc9 2975 _bfd_strip_section_from_output (info, s);
252b5132
RH
2976 continue;
2977 }
2978
2979 /* Allocate memory for the section contents. */
55fd94b0 2980 s->contents = bfd_zalloc (htab->elf.dynobj, s->_raw_size);
7619e7c7 2981 if (s->contents == NULL)
b34976b6 2982 return FALSE;
252b5132
RH
2983 }
2984
7619e7c7 2985 if (htab->elf.dynamic_sections_created)
252b5132
RH
2986 {
2987 /* Add some entries to the .dynamic section. We fill in the
2988 values later, in ppc_elf_finish_dynamic_sections, but we
2989 must add the entries now so that we get the correct size for
2990 the .dynamic section. The DT_DEBUG entry is filled in by the
2991 dynamic linker and used by the debugger. */
dc810e39 2992#define add_dynamic_entry(TAG, VAL) \
55fd94b0 2993 bfd_elf32_add_dynamic_entry (info, (TAG), (VAL))
dc810e39 2994
36af4a4e 2995 if (info->executable)
252b5132 2996 {
dc810e39 2997 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 2998 return FALSE;
252b5132
RH
2999 }
3000
7619e7c7 3001 if (htab->plt != NULL && htab->plt->_raw_size != 0)
252b5132 3002 {
dc810e39
AM
3003 if (!add_dynamic_entry (DT_PLTGOT, 0)
3004 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3005 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3006 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 3007 return FALSE;
252b5132
RH
3008 }
3009
3010 if (relocs)
3011 {
dc810e39
AM
3012 if (!add_dynamic_entry (DT_RELA, 0)
3013 || !add_dynamic_entry (DT_RELASZ, 0)
3014 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
b34976b6 3015 return FALSE;
252b5132
RH
3016 }
3017
7fce784e
AS
3018 /* If any dynamic relocs apply to a read-only section, then we
3019 need a DT_TEXTREL entry. */
3020 if ((info->flags & DF_TEXTREL) == 0)
3021 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
55fd94b0 3022 info);
7fce784e 3023
29c2fb7c 3024 if ((info->flags & DF_TEXTREL) != 0)
252b5132 3025 {
dc810e39 3026 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 3027 return FALSE;
252b5132
RH
3028 }
3029 }
dc810e39 3030#undef add_dynamic_entry
252b5132 3031
b34976b6 3032 return TRUE;
252b5132 3033}
252b5132 3034\f
7619e7c7 3035static bfd_boolean
55fd94b0
AM
3036update_local_sym_info (bfd *abfd,
3037 Elf_Internal_Shdr *symtab_hdr,
3038 unsigned long r_symndx,
3039 int tls_type)
7619e7c7
AM
3040{
3041 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3042 char *local_got_tls_masks;
3043
3044 if (local_got_refcounts == NULL)
3045 {
3046 bfd_size_type size = symtab_hdr->sh_info;
3047
3048 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
55fd94b0 3049 local_got_refcounts = bfd_zalloc (abfd, size);
7619e7c7
AM
3050 if (local_got_refcounts == NULL)
3051 return FALSE;
3052 elf_local_got_refcounts (abfd) = local_got_refcounts;
3053 }
3054
3055 local_got_refcounts[r_symndx] += 1;
3056 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
3057 local_got_tls_masks[r_symndx] |= tls_type;
3058 return TRUE;
3059}
3060
3061static void
55fd94b0 3062bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
7619e7c7
AM
3063{
3064 (*_bfd_error_handler)
3065 (_("%s: relocation %s cannot be used when making a shared object"),
3066 bfd_archive_filename (abfd),
55fd94b0 3067 ppc_elf_howto_table[r_type]->name);
7619e7c7
AM
3068 bfd_set_error (bfd_error_bad_value);
3069}
3070
252b5132
RH
3071/* Look through the relocs for a section during the first phase, and
3072 allocate space in the global offset table or procedure linkage
3073 table. */
3074
b34976b6 3075static bfd_boolean
55fd94b0
AM
3076ppc_elf_check_relocs (bfd *abfd,
3077 struct bfd_link_info *info,
3078 asection *sec,
3079 const Elf_Internal_Rela *relocs)
252b5132 3080{
7619e7c7 3081 struct ppc_elf_link_hash_table *htab;
252b5132 3082 Elf_Internal_Shdr *symtab_hdr;
7619e7c7 3083 struct elf_link_hash_entry **sym_hashes;
252b5132
RH
3084 const Elf_Internal_Rela *rel;
3085 const Elf_Internal_Rela *rel_end;
252b5132 3086 asection *sreloc;
252b5132
RH
3087
3088 if (info->relocateable)
b34976b6 3089 return TRUE;
252b5132
RH
3090
3091#ifdef DEBUG
3092 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
3093 bfd_get_section_name (abfd, sec),
8f615d07 3094 bfd_archive_filename (abfd));
252b5132
RH
3095#endif
3096
3097 /* Create the linker generated sections all the time so that the
3098 special symbols are created. */
3099
7619e7c7
AM
3100 htab = ppc_elf_hash_table (info);
3101 if (htab->sdata == NULL)
252b5132 3102 {
7619e7c7
AM
3103 htab->sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA);
3104 if (htab->sdata == NULL)
3105 htab->sdata = ppc_elf_create_linker_section (abfd, info,
3106 LINKER_SECTION_SDATA);
3107 if (htab->sdata == NULL)
b34976b6 3108 return FALSE;
252b5132
RH
3109 }
3110
7619e7c7 3111 if (htab->sdata2 == NULL)
252b5132 3112 {
7619e7c7
AM
3113 htab->sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2);
3114 if (htab->sdata2 == NULL)
3115 htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
3116 LINKER_SECTION_SDATA2);
3117 if (htab->sdata2 == NULL)
b34976b6 3118 return FALSE;
252b5132
RH
3119 }
3120
252b5132 3121 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
252b5132 3122 sym_hashes = elf_sym_hashes (abfd);
252b5132
RH
3123 sreloc = NULL;
3124
3125 rel_end = relocs + sec->reloc_count;
3126 for (rel = relocs; rel < rel_end; rel++)
3127 {
3128 unsigned long r_symndx;
7619e7c7 3129 enum elf_ppc_reloc_type r_type;
252b5132 3130 struct elf_link_hash_entry *h;
7619e7c7 3131 int tls_type = 0;
252b5132
RH
3132
3133 r_symndx = ELF32_R_SYM (rel->r_info);
3134 if (r_symndx < symtab_hdr->sh_info)
3135 h = NULL;
3136 else
3137 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3138
3139 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3140 This shows up in particular in an R_PPC_ADDR32 in the eabi
3141 startup code. */
3142 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3143 {
7619e7c7 3144 if (htab->got == NULL)
252b5132 3145 {
7619e7c7
AM
3146 if (htab->elf.dynobj == NULL)
3147 htab->elf.dynobj = abfd;
3148 if (!ppc_elf_create_got (htab->elf.dynobj, info))
b34976b6 3149 return FALSE;
252b5132
RH
3150 }
3151 }
3152
55fd94b0 3153 r_type = ELF32_R_TYPE (rel->r_info);
7619e7c7 3154 switch (r_type)
252b5132 3155 {
7619e7c7
AM
3156 case R_PPC_GOT_TLSLD16:
3157 case R_PPC_GOT_TLSLD16_LO:
3158 case R_PPC_GOT_TLSLD16_HI:
3159 case R_PPC_GOT_TLSLD16_HA:
3160 htab->tlsld_got.refcount += 1;
3161 tls_type = TLS_TLS | TLS_LD;
3162 goto dogottls;
3163
3164 case R_PPC_GOT_TLSGD16:
3165 case R_PPC_GOT_TLSGD16_LO:
3166 case R_PPC_GOT_TLSGD16_HI:
3167 case R_PPC_GOT_TLSGD16_HA:
3168 tls_type = TLS_TLS | TLS_GD;
3169 goto dogottls;
3170
3171 case R_PPC_GOT_TPREL16:
3172 case R_PPC_GOT_TPREL16_LO:
3173 case R_PPC_GOT_TPREL16_HI:
3174 case R_PPC_GOT_TPREL16_HA:
3175 if (info->shared)
3176 info->flags |= DF_STATIC_TLS;
3177 tls_type = TLS_TLS | TLS_TPREL;
3178 goto dogottls;
3179
3180 case R_PPC_GOT_DTPREL16:
3181 case R_PPC_GOT_DTPREL16_LO:
3182 case R_PPC_GOT_DTPREL16_HI:
3183 case R_PPC_GOT_DTPREL16_HA:
3184 tls_type = TLS_TLS | TLS_DTPREL;
3185 dogottls:
3186 sec->has_tls_reloc = 1;
3187 /* Fall thru */
3188
70bccea4 3189 /* GOT16 relocations */
252b5132
RH
3190 case R_PPC_GOT16:
3191 case R_PPC_GOT16_LO:
3192 case R_PPC_GOT16_HI:
3193 case R_PPC_GOT16_HA:
3194 /* This symbol requires a global offset table entry. */
7619e7c7 3195 if (htab->got == NULL)
252b5132 3196 {
7619e7c7
AM
3197 if (htab->elf.dynobj == NULL)
3198 htab->elf.dynobj = abfd;
3199 if (!ppc_elf_create_got (htab->elf.dynobj, info))
b34976b6 3200 return FALSE;
252b5132 3201 }
252b5132
RH
3202 if (h != NULL)
3203 {
7619e7c7
AM
3204 h->got.refcount += 1;
3205 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
252b5132
RH
3206 }
3207 else
7619e7c7
AM
3208 /* This is a global offset table entry for a local symbol. */
3209 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3210 return FALSE;
252b5132
RH
3211 break;
3212
70bccea4 3213 /* Indirect .sdata relocation. */
252b5132
RH
3214 case R_PPC_EMB_SDAI16:
3215 if (info->shared)
3216 {
7619e7c7 3217 bad_shared_reloc (abfd, r_type);
b34976b6 3218 return FALSE;
252b5132 3219 }
7619e7c7
AM
3220 if (!bfd_elf32_create_pointer_linker_section (abfd, info,
3221 htab->sdata, h, rel))
b34976b6 3222 return FALSE;
252b5132
RH
3223 break;
3224
70bccea4 3225 /* Indirect .sdata2 relocation. */
252b5132
RH
3226 case R_PPC_EMB_SDA2I16:
3227 if (info->shared)
3228 {
7619e7c7 3229 bad_shared_reloc (abfd, r_type);
b34976b6 3230 return FALSE;
252b5132 3231 }
7619e7c7
AM
3232 if (!bfd_elf32_create_pointer_linker_section (abfd, info,
3233 htab->sdata2, h, rel))
b34976b6 3234 return FALSE;
252b5132
RH
3235 break;
3236
3237 case R_PPC_SDAREL16:
3238 case R_PPC_EMB_SDA2REL:
3239 case R_PPC_EMB_SDA21:
7619e7c7
AM
3240 case R_PPC_EMB_RELSDA:
3241 case R_PPC_EMB_NADDR32:
3242 case R_PPC_EMB_NADDR16:
3243 case R_PPC_EMB_NADDR16_LO:
3244 case R_PPC_EMB_NADDR16_HI:
3245 case R_PPC_EMB_NADDR16_HA:
252b5132
RH
3246 if (info->shared)
3247 {
7619e7c7 3248 bad_shared_reloc (abfd, r_type);
b34976b6 3249 return FALSE;
252b5132
RH
3250 }
3251 break;
3252
3253 case R_PPC_PLT32:
3254 case R_PPC_PLTREL24:
7619e7c7 3255 case R_PPC_PLTREL32:
252b5132
RH
3256 case R_PPC_PLT16_LO:
3257 case R_PPC_PLT16_HI:
3258 case R_PPC_PLT16_HA:
3259#ifdef DEBUG
3260 fprintf (stderr, "Reloc requires a PLT entry\n");
3261#endif
3262 /* This symbol requires a procedure linkage table entry. We
70bccea4
AM
3263 actually build the entry in finish_dynamic_symbol,
3264 because this might be a case of linking PIC code without
3265 linking in any dynamic objects, in which case we don't
3266 need to generate a procedure linkage table after all. */
252b5132
RH
3267
3268 if (h == NULL)
3269 {
3270 /* It does not make sense to have a procedure linkage
70bccea4 3271 table entry for a local symbol. */
d85f2c7d
AM
3272 (*_bfd_error_handler) (_("%s(%s+0x%lx): %s reloc against "
3273 "local symbol"),
3274 bfd_archive_filename (abfd),
3275 sec->name,
3276 (long) rel->r_offset,
3277 ppc_elf_howto_table[r_type]->name);
252b5132 3278 bfd_set_error (bfd_error_bad_value);
b34976b6 3279 return FALSE;
252b5132
RH
3280 }
3281
51b64d56
AM
3282 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3283 h->plt.refcount++;
252b5132
RH
3284 break;
3285
3286 /* The following relocations don't need to propagate the
3287 relocation if linking a shared object since they are
3288 section relative. */
3289 case R_PPC_SECTOFF:
3290 case R_PPC_SECTOFF_LO:
3291 case R_PPC_SECTOFF_HI:
3292 case R_PPC_SECTOFF_HA:
7619e7c7
AM
3293 case R_PPC_DTPREL16:
3294 case R_PPC_DTPREL16_LO:
3295 case R_PPC_DTPREL16_HI:
3296 case R_PPC_DTPREL16_HA:
3297 case R_PPC_TOC16:
3298 break;
3299
3300 /* This are just markers. */
3301 case R_PPC_TLS:
3302 case R_PPC_EMB_MRKREF:
3303 case R_PPC_NONE:
3304 case R_PPC_max:
3305 break;
3306
3307 /* These should only appear in dynamic objects. */
3308 case R_PPC_COPY:
3309 case R_PPC_GLOB_DAT:
3310 case R_PPC_JMP_SLOT:
3311 case R_PPC_RELATIVE:
3312 break;
3313
3314 /* These aren't handled yet. We'll report an error later. */
3315 case R_PPC_ADDR30:
3316 case R_PPC_EMB_RELSEC16:
3317 case R_PPC_EMB_RELST_LO:
3318 case R_PPC_EMB_RELST_HI:
3319 case R_PPC_EMB_RELST_HA:
3320 case R_PPC_EMB_BIT_FLD:
252b5132
RH
3321 break;
3322
ae9a127f 3323 /* This refers only to functions defined in the shared library. */
252b5132
RH
3324 case R_PPC_LOCAL24PC:
3325 break;
3326
3327 /* This relocation describes the C++ object vtable hierarchy.
3328 Reconstruct it for later use during GC. */
3329 case R_PPC_GNU_VTINHERIT:
3330 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 3331 return FALSE;
252b5132
RH
3332 break;
3333
3334 /* This relocation describes which C++ vtable entries are actually
3335 used. Record for later use during GC. */
3336 case R_PPC_GNU_VTENTRY:
3337 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 3338 return FALSE;
252b5132
RH
3339 break;
3340
7619e7c7
AM
3341 /* We shouldn't really be seeing these. */
3342 case R_PPC_TPREL32:
3343 if (info->shared)
3344 info->flags |= DF_STATIC_TLS;
3345 goto dodyn;
3346
3347 /* Nor these. */
3348 case R_PPC_DTPMOD32:
3349 case R_PPC_DTPREL32:
3350 goto dodyn;
3351
3352 case R_PPC_TPREL16:
3353 case R_PPC_TPREL16_LO:
3354 case R_PPC_TPREL16_HI:
3355 case R_PPC_TPREL16_HA:
3356 if (info->shared)
3357 info->flags |= DF_STATIC_TLS;
3358 goto dodyn;
3359
252b5132
RH
3360 /* When creating a shared object, we must copy these
3361 relocs into the output file. We create a reloc
3362 section in dynobj and make room for the reloc. */
3363 case R_PPC_REL24:
3364 case R_PPC_REL14:
3365 case R_PPC_REL14_BRTAKEN:
3366 case R_PPC_REL14_BRNTAKEN:
3367 case R_PPC_REL32:
3368 if (h == NULL
2b98e6c0 3369 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
252b5132
RH
3370 break;
3371 /* fall through */
3372
7619e7c7
AM
3373 case R_PPC_ADDR32:
3374 case R_PPC_ADDR24:
3375 case R_PPC_ADDR16:
3376 case R_PPC_ADDR16_LO:
3377 case R_PPC_ADDR16_HI:
3378 case R_PPC_ADDR16_HA:
3379 case R_PPC_ADDR14:
3380 case R_PPC_ADDR14_BRTAKEN:
3381 case R_PPC_ADDR14_BRNTAKEN:
3382 case R_PPC_UADDR32:
3383 case R_PPC_UADDR16:
3384 if (h != NULL && !info->shared)
3385 {
3386 /* We may need a plt entry if the symbol turns out to be
3387 a function defined in a dynamic object. */
3388 h->plt.refcount++;
3389
3390 /* We may need a copy reloc too. */
3391 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3392 }
ee05f2fe 3393
7619e7c7 3394 dodyn:
ee05f2fe
AM
3395 /* If we are creating a shared library, and this is a reloc
3396 against a global symbol, or a non PC relative reloc
3397 against a local symbol, then we need to copy the reloc
3398 into the shared library. However, if we are linking with
3399 -Bsymbolic, we do not need to copy a reloc against a
3400 global symbol which is defined in an object we are
3401 including in the link (i.e., DEF_REGULAR is set). At
3402 this point we have not seen all the input files, so it is
3403 possible that DEF_REGULAR is not set now but will be set
3404 later (it is never cleared). In case of a weak definition,
3405 DEF_REGULAR may be cleared later by a strong definition in
3406 a shared library. We account for that possibility below by
3407 storing information in the dyn_relocs field of the hash
3408 table entry. A similar situation occurs when creating
3409 shared libraries and symbol visibility changes render the
3410 symbol local.
3411
3412 If on the other hand, we are creating an executable, we
3413 may need to keep relocations for symbols satisfied by a
3414 dynamic library if we manage to avoid copy relocs for the
3415 symbol. */
3416 if ((info->shared
3417 && (MUST_BE_DYN_RELOC (r_type)
3418 || (h != NULL
3419 && (! info->symbolic
3420 || h->root.type == bfd_link_hash_defweak
3421 || (h->elf_link_hash_flags
3422 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3423 || (ELIMINATE_COPY_RELOCS
3424 && !info->shared
3425 && (sec->flags & SEC_ALLOC) != 0
3426 && h != NULL
3427 && (h->root.type == bfd_link_hash_defweak
3428 || (h->elf_link_hash_flags
3429 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
252b5132 3430 {
7fce784e
AS
3431 struct ppc_elf_dyn_relocs *p;
3432 struct ppc_elf_dyn_relocs **head;
3433
252b5132 3434#ifdef DEBUG
55fd94b0
AM
3435 fprintf (stderr,
3436 "ppc_elf_check_relocs needs to "
3437 "create relocation for %s\n",
70bccea4
AM
3438 (h && h->root.root.string
3439 ? h->root.root.string : "<unknown>"));
252b5132
RH
3440#endif
3441 if (sreloc == NULL)
3442 {
3443 const char *name;
3444
3445 name = (bfd_elf_string_from_elf_section
3446 (abfd,
3447 elf_elfheader (abfd)->e_shstrndx,
3448 elf_section_data (sec)->rel_hdr.sh_name));
3449 if (name == NULL)
b34976b6 3450 return FALSE;
252b5132
RH
3451
3452 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3453 && strcmp (bfd_get_section_name (abfd, sec),
3454 name + 5) == 0);
3455
7619e7c7 3456 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
252b5132
RH
3457 if (sreloc == NULL)
3458 {
3459 flagword flags;
3460
7619e7c7 3461 sreloc = bfd_make_section (htab->elf.dynobj, name);
252b5132
RH
3462 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3463 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3464 if ((sec->flags & SEC_ALLOC) != 0)
3465 flags |= SEC_ALLOC | SEC_LOAD;
3466 if (sreloc == NULL
7619e7c7
AM
3467 || ! bfd_set_section_flags (htab->elf.dynobj,
3468 sreloc, flags)
3469 || ! bfd_set_section_alignment (htab->elf.dynobj,
3470 sreloc, 2))
b34976b6 3471 return FALSE;
252b5132 3472 }
7fce784e
AS
3473 elf_section_data (sec)->sreloc = sreloc;
3474 }
3475
3476 /* If this is a global symbol, we count the number of
3477 relocations we need for this symbol. */
3478 if (h != NULL)
3479 {
3480 head = &ppc_elf_hash_entry (h)->dyn_relocs;
252b5132 3481 }
7fce784e
AS
3482 else
3483 {
3484 /* Track dynamic relocs needed for local syms too.
3485 We really need local syms available to do this
3486 easily. Oh well. */
3487
3488 asection *s;
ee05f2fe
AM
3489 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3490 sec, r_symndx);
7fce784e
AS
3491 if (s == NULL)
3492 return FALSE;
252b5132 3493
7fce784e
AS
3494 head = ((struct ppc_elf_dyn_relocs **)
3495 &elf_section_data (s)->local_dynrel);
3496 }
252b5132 3497
7fce784e
AS
3498 p = *head;
3499 if (p == NULL || p->sec != sec)
3500 {
55fd94b0 3501 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
7fce784e
AS
3502 if (p == NULL)
3503 return FALSE;
3504 p->next = *head;
3505 *head = p;
3506 p->sec = sec;
3507 p->count = 0;
ee05f2fe 3508 p->pc_count = 0;
7fce784e
AS
3509 }
3510
ee05f2fe
AM
3511 p->count += 1;
3512 if (!MUST_BE_DYN_RELOC (r_type))
3513 p->pc_count += 1;
252b5132
RH
3514 }
3515
3516 break;
3517 }
3518 }
3519
b34976b6 3520 return TRUE;
252b5132
RH
3521}
3522
3523/* Return the section that should be marked against GC for a given
3524 relocation. */
3525
3526static asection *
55fd94b0
AM
3527ppc_elf_gc_mark_hook (asection *sec,
3528 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3529 Elf_Internal_Rela *rel,
3530 struct elf_link_hash_entry *h,
3531 Elf_Internal_Sym *sym)
252b5132
RH
3532{
3533 if (h != NULL)
3534 {
3535 switch (ELF32_R_TYPE (rel->r_info))
3536 {
3537 case R_PPC_GNU_VTINHERIT:
3538 case R_PPC_GNU_VTENTRY:
3539 break;
3540
3541 default:
3542 switch (h->root.type)
3543 {
3544 case bfd_link_hash_defined:
3545 case bfd_link_hash_defweak:
3546 return h->root.u.def.section;
3547
3548 case bfd_link_hash_common:
3549 return h->root.u.c.p->section;
3550
3551 default:
3552 break;
3553 }
3554 }
3555 }
3556 else
1e2f5b6e 3557 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
252b5132
RH
3558
3559 return NULL;
3560}
3561
7619e7c7
AM
3562/* Update the got, plt and dynamic reloc reference counts for the
3563 section being removed. */
252b5132 3564
b34976b6 3565static bfd_boolean
55fd94b0
AM
3566ppc_elf_gc_sweep_hook (bfd *abfd,
3567 struct bfd_link_info *info,
3568 asection *sec,
3569 const Elf_Internal_Rela *relocs)
252b5132 3570{
7619e7c7 3571 struct ppc_elf_link_hash_table *htab;
252b5132
RH
3572 Elf_Internal_Shdr *symtab_hdr;
3573 struct elf_link_hash_entry **sym_hashes;
3574 bfd_signed_vma *local_got_refcounts;
3575 const Elf_Internal_Rela *rel, *relend;
252b5132 3576
7fce784e
AS
3577 elf_section_data (sec)->local_dynrel = NULL;
3578
7619e7c7 3579 htab = ppc_elf_hash_table (info);
252b5132
RH
3580 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3581 sym_hashes = elf_sym_hashes (abfd);
3582 local_got_refcounts = elf_local_got_refcounts (abfd);
3583
3584 relend = relocs + sec->reloc_count;
3585 for (rel = relocs; rel < relend; rel++)
7619e7c7
AM
3586 {
3587 unsigned long r_symndx;
3588 enum elf_ppc_reloc_type r_type;
3589 struct elf_link_hash_entry *h = NULL;
252b5132 3590
7619e7c7
AM
3591 r_symndx = ELF32_R_SYM (rel->r_info);
3592 if (r_symndx >= symtab_hdr->sh_info)
3593 {
3594 struct ppc_elf_dyn_relocs **pp, *p;
3595 struct ppc_elf_link_hash_entry *eh;
3596
3597 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3598 eh = (struct ppc_elf_link_hash_entry *) h;
3599
3600 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3601 if (p->sec == sec)
3602 {
3603 /* Everything must go for SEC. */
3604 *pp = p->next;
3605 break;
3606 }
3607 }
3608
55fd94b0 3609 r_type = ELF32_R_TYPE (rel->r_info);
7619e7c7
AM
3610 switch (r_type)
3611 {
3612 case R_PPC_GOT_TLSLD16:
3613 case R_PPC_GOT_TLSLD16_LO:
3614 case R_PPC_GOT_TLSLD16_HI:
3615 case R_PPC_GOT_TLSLD16_HA:
3616 htab->tlsld_got.refcount -= 1;
3617 /* Fall thru */
3618
3619 case R_PPC_GOT_TLSGD16:
3620 case R_PPC_GOT_TLSGD16_LO:
3621 case R_PPC_GOT_TLSGD16_HI:
3622 case R_PPC_GOT_TLSGD16_HA:
3623 case R_PPC_GOT_TPREL16:
3624 case R_PPC_GOT_TPREL16_LO:
3625 case R_PPC_GOT_TPREL16_HI:
3626 case R_PPC_GOT_TPREL16_HA:
3627 case R_PPC_GOT_DTPREL16:
3628 case R_PPC_GOT_DTPREL16_LO:
3629 case R_PPC_GOT_DTPREL16_HI:
3630 case R_PPC_GOT_DTPREL16_HA:
3631 case R_PPC_GOT16:
3632 case R_PPC_GOT16_LO:
3633 case R_PPC_GOT16_HI:
3634 case R_PPC_GOT16_HA:
3635 if (h != NULL)
3636 {
3637 if (h->got.refcount > 0)
3638 h->got.refcount--;
3639 }
3640 else if (local_got_refcounts != NULL)
3641 {
3642 if (local_got_refcounts[r_symndx] > 0)
3643 local_got_refcounts[r_symndx]--;
3644 }
3645 break;
3646
3647 case R_PPC_REL24:
3648 case R_PPC_REL14:
3649 case R_PPC_REL14_BRTAKEN:
3650 case R_PPC_REL14_BRNTAKEN:
3651 case R_PPC_REL32:
3652 if (h == NULL
2b98e6c0 3653 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
7619e7c7
AM
3654 break;
3655 /* Fall thru */
3656
3657 case R_PPC_ADDR32:
3658 case R_PPC_ADDR24:
3659 case R_PPC_ADDR16:
3660 case R_PPC_ADDR16_LO:
3661 case R_PPC_ADDR16_HI:
3662 case R_PPC_ADDR16_HA:
3663 case R_PPC_ADDR14:
3664 case R_PPC_ADDR14_BRTAKEN:
3665 case R_PPC_ADDR14_BRNTAKEN:
3666 case R_PPC_UADDR32:
3667 case R_PPC_UADDR16:
3668 case R_PPC_PLT32:
3669 case R_PPC_PLTREL24:
3670 case R_PPC_PLT16_LO:
3671 case R_PPC_PLT16_HI:
3672 case R_PPC_PLT16_HA:
3673 if (h != NULL)
3674 {
3675 if (h->plt.refcount > 0)
3676 h->plt.refcount--;
3677 }
3678 break;
3679
3680 default:
3681 break;
3682 }
3683 }
3684 return TRUE;
3685}
3686
3687/* Set htab->tls_sec and htab->tls_get_addr. */
3688
3689bfd_boolean
55fd94b0 3690ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
7619e7c7
AM
3691{
3692 asection *tls;
3693 struct ppc_elf_link_hash_table *htab;
3694
3695 htab = ppc_elf_hash_table (info);
3696 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3697 FALSE, FALSE, TRUE);
3698
3699 for (tls = obfd->sections; tls != NULL; tls = tls->next)
3700 if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3701 == (SEC_THREAD_LOCAL | SEC_LOAD))
3702 break;
3703 htab->tls_sec = tls;
3704
3705 return tls != NULL;
3706}
3707
3708/* Run through all the TLS relocs looking for optimization
3709 opportunities. */
3710
3711bfd_boolean
55fd94b0
AM
3712ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
3713 struct bfd_link_info *info)
7619e7c7
AM
3714{
3715 bfd *ibfd;
3716 asection *sec;
3717 struct ppc_elf_link_hash_table *htab;
3718
3719 if (info->relocateable || info->shared)
3720 return TRUE;
3721
3722 htab = ppc_elf_hash_table (info);
3723 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3724 {
3725 Elf_Internal_Sym *locsyms = NULL;
3726 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3727
3728 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3729 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7fce784e 3730 {
7619e7c7
AM
3731 Elf_Internal_Rela *relstart, *rel, *relend;
3732 int expecting_tls_get_addr;
3733
3734 /* Read the relocations. */
55fd94b0 3735 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 3736 info->keep_memory);
7619e7c7
AM
3737 if (relstart == NULL)
3738 return FALSE;
3739
3740 expecting_tls_get_addr = 0;
3741 relend = relstart + sec->reloc_count;
3742 for (rel = relstart; rel < relend; rel++)
3743 {
3744 enum elf_ppc_reloc_type r_type;
3745 unsigned long r_symndx;
3746 struct elf_link_hash_entry *h = NULL;
3747 char *tls_mask;
3748 char tls_set, tls_clear;
3749 bfd_boolean is_local;
3750
3751 r_symndx = ELF32_R_SYM (rel->r_info);
3752 if (r_symndx >= symtab_hdr->sh_info)
3753 {
3754 struct elf_link_hash_entry **sym_hashes;
7fce784e 3755
7619e7c7
AM
3756 sym_hashes = elf_sym_hashes (ibfd);
3757 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3758 while (h->root.type == bfd_link_hash_indirect
3759 || h->root.type == bfd_link_hash_warning)
3760 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3761 }
7fce784e 3762
7619e7c7
AM
3763 is_local = FALSE;
3764 if (h == NULL
3765 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
3766 is_local = TRUE;
3767
55fd94b0 3768 r_type = ELF32_R_TYPE (rel->r_info);
7619e7c7
AM
3769 switch (r_type)
3770 {
3771 case R_PPC_GOT_TLSLD16:
3772 case R_PPC_GOT_TLSLD16_LO:
3773 case R_PPC_GOT_TLSLD16_HI:
3774 case R_PPC_GOT_TLSLD16_HA:
3775 /* These relocs should never be against a symbol
3776 defined in a shared lib. Leave them alone if
3777 that turns out to be the case. */
3778 expecting_tls_get_addr = 0;
3779 htab->tlsld_got.refcount -= 1;
3780 if (!is_local)
3781 continue;
3782
3783 /* LD -> LE */
3784 tls_set = 0;
3785 tls_clear = TLS_LD;
3786 expecting_tls_get_addr = 1;
3787 break;
3788
3789 case R_PPC_GOT_TLSGD16:
3790 case R_PPC_GOT_TLSGD16_LO:
3791 case R_PPC_GOT_TLSGD16_HI:
3792 case R_PPC_GOT_TLSGD16_HA:
3793 if (is_local)
3794 /* GD -> LE */
3795 tls_set = 0;
3796 else
3797 /* GD -> IE */
3798 tls_set = TLS_TLS | TLS_TPRELGD;
3799 tls_clear = TLS_GD;
3800 expecting_tls_get_addr = 1;
3801 break;
3802
3803 case R_PPC_GOT_TPREL16:
3804 case R_PPC_GOT_TPREL16_LO:
3805 case R_PPC_GOT_TPREL16_HI:
3806 case R_PPC_GOT_TPREL16_HA:
3807 expecting_tls_get_addr = 0;
3808 if (is_local)
3809 {
3810 /* IE -> LE */
3811 tls_set = 0;
3812 tls_clear = TLS_TPREL;
3813 break;
3814 }
3815 else
3816 continue;
3817
3818 case R_PPC_REL14:
3819 case R_PPC_REL14_BRTAKEN:
3820 case R_PPC_REL14_BRNTAKEN:
3821 case R_PPC_REL24:
3822 if (expecting_tls_get_addr
3823 && h != NULL
3824 && h == htab->tls_get_addr)
3825 {
3826 if (h->plt.refcount > 0)
3827 h->plt.refcount -= 1;
3828 }
3829 expecting_tls_get_addr = 0;
3830 continue;
3831
3832 default:
3833 expecting_tls_get_addr = 0;
3834 continue;
3835 }
3836
3837 if (h != NULL)
3838 {
3839 if (tls_set == 0)
3840 {
3841 /* We managed to get rid of a got entry. */
3842 if (h->got.refcount > 0)
3843 h->got.refcount -= 1;
3844 }
3845 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3846 }
3847 else
3848 {
3849 Elf_Internal_Sym *sym;
3850 bfd_signed_vma *lgot_refs;
3851 char *lgot_masks;
3852
3853 if (locsyms == NULL)
3854 {
3855 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3856 if (locsyms == NULL)
3857 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3858 symtab_hdr->sh_info,
3859 0, NULL, NULL, NULL);
3860 if (locsyms == NULL)
3861 {
3862 if (elf_section_data (sec)->relocs != relstart)
3863 free (relstart);
3864 return FALSE;
3865 }
3866 }
3867 sym = locsyms + r_symndx;
3868 lgot_refs = elf_local_got_refcounts (ibfd);
3869 if (lgot_refs == NULL)
3870 abort ();
3871 if (tls_set == 0)
3872 {
3873 /* We managed to get rid of a got entry. */
3874 if (lgot_refs[r_symndx] > 0)
3875 lgot_refs[r_symndx] -= 1;
3876 }
3877 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
3878 tls_mask = &lgot_masks[r_symndx];
3879 }
3880
3881 *tls_mask |= tls_set;
3882 *tls_mask &= ~tls_clear;
3883 }
3884
3885 if (elf_section_data (sec)->relocs != relstart)
3886 free (relstart);
7fce784e 3887 }
252b5132 3888
7619e7c7
AM
3889 if (locsyms != NULL
3890 && (symtab_hdr->contents != (unsigned char *) locsyms))
3891 {
3892 if (!info->keep_memory)
3893 free (locsyms);
3894 else
3895 symtab_hdr->contents = (unsigned char *) locsyms;
3896 }
3897 }
b34976b6 3898 return TRUE;
252b5132
RH
3899}
3900\f
3901/* Hook called by the linker routine which adds symbols from an object
3902 file. We use it to put .comm items in .sbss, and not .bss. */
3903
b34976b6 3904static bfd_boolean
55fd94b0
AM
3905ppc_elf_add_symbol_hook (bfd *abfd,
3906 struct bfd_link_info *info,
3907 const Elf_Internal_Sym *sym,
3908 const char **namep ATTRIBUTE_UNUSED,
3909 flagword *flagsp ATTRIBUTE_UNUSED,
3910 asection **secp,
3911 bfd_vma *valp)
252b5132
RH
3912{
3913 if (sym->st_shndx == SHN_COMMON
3914 && !info->relocateable
27242387
AM
3915 && sym->st_size <= elf_gp_size (abfd)
3916 && info->hash->creator->flavour == bfd_target_elf_flavour)
252b5132
RH
3917 {
3918 /* Common symbols less than or equal to -G nn bytes are automatically
3919 put into .sdata. */
3920 elf_linker_section_t *sdata
3921 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
3922
3923 if (!sdata->bss_section)
3924 {
dc810e39
AM
3925 bfd_size_type amt;
3926
252b5132 3927 /* We don't go through bfd_make_section, because we don't
70bccea4
AM
3928 want to attach this common section to DYNOBJ. The linker
3929 will move the symbols to the appropriate output section
3930 when it defines common symbols. */
dc810e39 3931 amt = sizeof (asection);
55fd94b0 3932 sdata->bss_section = bfd_zalloc (abfd, amt);
252b5132 3933 if (sdata->bss_section == NULL)
b34976b6 3934 return FALSE;
252b5132
RH
3935 sdata->bss_section->name = sdata->bss_name;
3936 sdata->bss_section->flags = SEC_IS_COMMON;
3937 sdata->bss_section->output_section = sdata->bss_section;
dc810e39 3938 amt = sizeof (asymbol);
55fd94b0 3939 sdata->bss_section->symbol = bfd_zalloc (abfd, amt);
dc810e39 3940 amt = sizeof (asymbol *);
55fd94b0 3941 sdata->bss_section->symbol_ptr_ptr = bfd_zalloc (abfd, amt);
252b5132
RH
3942 if (sdata->bss_section->symbol == NULL
3943 || sdata->bss_section->symbol_ptr_ptr == NULL)
b34976b6 3944 return FALSE;
252b5132
RH
3945 sdata->bss_section->symbol->name = sdata->bss_name;
3946 sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
3947 sdata->bss_section->symbol->section = sdata->bss_section;
3948 *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
3949 }
3950
3951 *secp = sdata->bss_section;
3952 *valp = sym->st_size;
3953 }
3954
b34976b6 3955 return TRUE;
252b5132 3956}
252b5132
RH
3957\f
3958/* Finish up dynamic symbol handling. We set the contents of various
3959 dynamic sections here. */
3960
b34976b6 3961static bfd_boolean
55fd94b0
AM
3962ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
3963 struct bfd_link_info *info,
3964 struct elf_link_hash_entry *h,
3965 Elf_Internal_Sym *sym)
252b5132 3966{
7619e7c7 3967 struct ppc_elf_link_hash_table *htab;
252b5132
RH
3968
3969#ifdef DEBUG
3970 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
3971 h->root.root.string);
3972#endif
3973
7619e7c7
AM
3974 htab = ppc_elf_hash_table (info);
3975 BFD_ASSERT (htab->elf.dynobj != NULL);
252b5132
RH
3976
3977 if (h->plt.offset != (bfd_vma) -1)
3978 {
252b5132 3979 Elf_Internal_Rela rela;
947216bf 3980 bfd_byte *loc;
252b5132
RH
3981 bfd_vma reloc_index;
3982
3983#ifdef DEBUG
3984 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
3985#endif
3986
3987 /* This symbol has an entry in the procedure linkage table. Set
70bccea4 3988 it up. */
252b5132
RH
3989
3990 BFD_ASSERT (h->dynindx != -1);
7619e7c7 3991 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
252b5132
RH
3992
3993 /* We don't need to fill in the .plt. The ppc dynamic linker
3994 will fill it in. */
3995
3996 /* Fill in the entry in the .rela.plt section. */
7619e7c7
AM
3997 rela.r_offset = (htab->plt->output_section->vma
3998 + htab->plt->output_offset
252b5132
RH
3999 + h->plt.offset);
4000 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
4001 rela.r_addend = 0;
4002
4003 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
4004 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
4005 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
7619e7c7
AM
4006 loc = (htab->relplt->contents
4007 + reloc_index * sizeof (Elf32_External_Rela));
947216bf 4008 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132
RH
4009
4010 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4011 {
4012 /* Mark the symbol as undefined, rather than as defined in
4013 the .plt section. Leave the value alone. */
4014 sym->st_shndx = SHN_UNDEF;
a4b120e1
GK
4015 /* If the symbol is weak, we do need to clear the value.
4016 Otherwise, the PLT entry would provide a definition for
4017 the symbol even if the symbol wasn't defined anywhere,
4018 and so the symbol would never be NULL. */
c3668558 4019 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
a4b120e1
GK
4020 == 0)
4021 sym->st_value = 0;
252b5132
RH
4022 }
4023 }
4024
252b5132
RH
4025 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4026 {
4027 asection *s;
4028 Elf_Internal_Rela rela;
947216bf 4029 bfd_byte *loc;
252b5132
RH
4030
4031 /* This symbols needs a copy reloc. Set it up. */
4032
4033#ifdef DEBUG
4034 fprintf (stderr, ", copy");
4035#endif
4036
4037 BFD_ASSERT (h->dynindx != -1);
4038
7619e7c7
AM
4039 if (h->size <= elf_gp_size (htab->elf.dynobj))
4040 s = htab->relsbss;
252b5132 4041 else
7619e7c7 4042 s = htab->relbss;
252b5132
RH
4043 BFD_ASSERT (s != NULL);
4044
4045 rela.r_offset = (h->root.u.def.value
4046 + h->root.u.def.section->output_section->vma
4047 + h->root.u.def.section->output_offset);
4048 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
4049 rela.r_addend = 0;
947216bf
AM
4050 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4051 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
252b5132
RH
4052 }
4053
4054#ifdef DEBUG
4055 fprintf (stderr, "\n");
4056#endif
4057
4058 /* Mark some specially defined symbols as absolute. */
4059 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4060 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4061 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4062 sym->st_shndx = SHN_ABS;
4063
b34976b6 4064 return TRUE;
252b5132 4065}
252b5132
RH
4066\f
4067/* Finish up the dynamic sections. */
4068
b34976b6 4069static bfd_boolean
55fd94b0
AM
4070ppc_elf_finish_dynamic_sections (bfd *output_bfd,
4071 struct bfd_link_info *info)
252b5132
RH
4072{
4073 asection *sdyn;
7619e7c7 4074 struct ppc_elf_link_hash_table *htab;
252b5132
RH
4075
4076#ifdef DEBUG
4077 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
4078#endif
4079
7619e7c7
AM
4080 htab = ppc_elf_hash_table (info);
4081 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
252b5132 4082
7619e7c7 4083 if (htab->elf.dynamic_sections_created)
252b5132 4084 {
252b5132
RH
4085 Elf32_External_Dyn *dyncon, *dynconend;
4086
7619e7c7 4087 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
252b5132
RH
4088
4089 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4090 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4091 for (; dyncon < dynconend; dyncon++)
4092 {
4093 Elf_Internal_Dyn dyn;
7619e7c7 4094 asection *s;
252b5132 4095
7619e7c7 4096 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
252b5132
RH
4097
4098 switch (dyn.d_tag)
4099 {
7619e7c7
AM
4100 case DT_PLTGOT:
4101 s = htab->plt;
4102 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4103 break;
252b5132 4104
7619e7c7
AM
4105 case DT_PLTRELSZ:
4106 dyn.d_un.d_val = htab->relplt->_raw_size;
4107 break;
252b5132 4108
7619e7c7
AM
4109 case DT_JMPREL:
4110 s = htab->relplt;
4111 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4112 break;
4113
4114 default:
4115 continue;
252b5132 4116 }
7619e7c7
AM
4117
4118 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
252b5132
RH
4119 }
4120 }
4121
4122 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
4123 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
7619e7c7 4124 if (htab->got)
252b5132 4125 {
7619e7c7 4126 unsigned char *contents = htab->got->contents;
55fd94b0 4127 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
252b5132
RH
4128
4129 if (sdyn == NULL)
55fd94b0 4130 bfd_put_32 (output_bfd, 0, contents + 4);
252b5132
RH
4131 else
4132 bfd_put_32 (output_bfd,
4133 sdyn->output_section->vma + sdyn->output_offset,
7619e7c7 4134 contents + 4);
252b5132 4135
7619e7c7 4136 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
252b5132
RH
4137 }
4138
b34976b6 4139 return TRUE;
252b5132 4140}
252b5132
RH
4141\f
4142/* The RELOCATE_SECTION function is called by the ELF backend linker
4143 to handle the relocations for a section.
4144
4145 The relocs are always passed as Rela structures; if the section
4146 actually uses Rel structures, the r_addend field will always be
4147 zero.
4148
4149 This function is responsible for adjust the section contents as
4150 necessary, and (if using Rela relocs and generating a
4151 relocateable output file) adjusting the reloc addend as
4152 necessary.
4153
4154 This function does not have to worry about setting the reloc
4155 address or the reloc symbol index.
4156
4157 LOCAL_SYMS is a pointer to the swapped in local symbols.
4158
4159 LOCAL_SECTIONS is an array giving the section in the input file
4160 corresponding to the st_shndx field of each local symbol.
4161
4162 The global hash table entry for the global symbols can be found
4163 via elf_sym_hashes (input_bfd).
4164
4165 When generating relocateable output, this function must handle
4166 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4167 going to be the section symbol corresponding to the output
4168 section, which means that the addend must be adjusted
4169 accordingly. */
4170
b34976b6 4171static bfd_boolean
55fd94b0
AM
4172ppc_elf_relocate_section (bfd *output_bfd,
4173 struct bfd_link_info *info,
4174 bfd *input_bfd,
4175 asection *input_section,
4176 bfd_byte *contents,
4177 Elf_Internal_Rela *relocs,
4178 Elf_Internal_Sym *local_syms,
4179 asection **local_sections)
252b5132 4180{
7619e7c7
AM
4181 Elf_Internal_Shdr *symtab_hdr;
4182 struct elf_link_hash_entry **sym_hashes;
4183 struct ppc_elf_link_hash_table *htab;
4184 Elf_Internal_Rela *rel;
4185 Elf_Internal_Rela *relend;
4186 Elf_Internal_Rela outrel;
4187 bfd_byte *loc;
b34976b6 4188 asection *sreloc = NULL;
252b5132 4189 bfd_vma *local_got_offsets;
b34976b6 4190 bfd_boolean ret = TRUE;
b34976b6 4191
252b5132 4192#ifdef DEBUG
55fd94b0
AM
4193 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, "
4194 "%ld relocations%s\n",
8f615d07 4195 bfd_archive_filename (input_bfd),
252b5132 4196 bfd_section_name(input_bfd, input_section),
8da6118f 4197 (long) input_section->reloc_count,
252b5132
RH
4198 (info->relocateable) ? " (relocatable)" : "");
4199#endif
4200
b491616a 4201 if (info->relocateable)
b34976b6 4202 return TRUE;
b491616a 4203
8da6118f
KH
4204 if (!ppc_elf_howto_table[R_PPC_ADDR32])
4205 /* Initialize howto table if needed. */
252b5132
RH
4206 ppc_elf_howto_init ();
4207
7619e7c7 4208 htab = ppc_elf_hash_table (info);
252b5132 4209 local_got_offsets = elf_local_got_offsets (input_bfd);
7619e7c7
AM
4210 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4211 sym_hashes = elf_sym_hashes (input_bfd);
4212 rel = relocs;
4213 relend = relocs + input_section->reloc_count;
252b5132
RH
4214 for (; rel < relend; rel++)
4215 {
7619e7c7
AM
4216 enum elf_ppc_reloc_type r_type;
4217 bfd_vma addend;
4218 bfd_reloc_status_type r;
4219 Elf_Internal_Sym *sym;
4220 asection *sec;
4221 struct elf_link_hash_entry *h;
4222 const char *sym_name;
252b5132
RH
4223 reloc_howto_type *howto;
4224 unsigned long r_symndx;
4225 bfd_vma relocation;
7619e7c7 4226 bfd_vma branch_bit, insn, from;
7619e7c7
AM
4227 bfd_boolean unresolved_reloc;
4228 bfd_boolean warned;
4229 unsigned int tls_type, tls_mask, tls_gd;
4230
55fd94b0
AM
4231 r_type = ELF32_R_TYPE (rel->r_info);
4232 sym = NULL;
4233 sec = NULL;
4234 h = NULL;
7619e7c7
AM
4235 unresolved_reloc = FALSE;
4236 warned = FALSE;
252b5132 4237 r_symndx = ELF32_R_SYM (rel->r_info);
252b5132
RH
4238 if (r_symndx < symtab_hdr->sh_info)
4239 {
4240 sym = local_syms + r_symndx;
4241 sec = local_sections[r_symndx];
7619e7c7 4242 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
252b5132 4243
f8df10f4 4244 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
252b5132
RH
4245 }
4246 else
4247 {
4248 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4249 while (h->root.type == bfd_link_hash_indirect
4250 || h->root.type == bfd_link_hash_warning)
4251 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4252 sym_name = h->root.root.string;
e1e0340b 4253
7619e7c7 4254 relocation = 0;
252b5132
RH
4255 if (h->root.type == bfd_link_hash_defined
4256 || h->root.type == bfd_link_hash_defweak)
4257 {
4258 sec = h->root.u.def.section;
70bccea4
AM
4259 /* Set a flag that will be cleared later if we find a
4260 relocation value for this symbol. output_section
4261 is typically NULL for symbols satisfied by a shared
4262 library. */
7619e7c7
AM
4263 if (sec->output_section == NULL)
4264 unresolved_reloc = TRUE;
252b5132
RH
4265 else
4266 relocation = (h->root.u.def.value
4267 + sec->output_section->vma
4268 + sec->output_offset);
4269 }
4270 else if (h->root.type == bfd_link_hash_undefweak)
7619e7c7 4271 ;
671bae9c 4272 else if (info->shared
671bae9c 4273 && !info->no_undefined
3a27a730 4274 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
7619e7c7 4275 ;
252b5132
RH
4276 else
4277 {
7619e7c7
AM
4278 if (! ((*info->callbacks->undefined_symbol)
4279 (info, h->root.root.string, input_bfd, input_section,
4280 rel->r_offset, (!info->shared
4281 || info->no_undefined
4282 || ELF_ST_VISIBILITY (h->other)))))
b34976b6 4283 return FALSE;
7619e7c7
AM
4284 warned = TRUE;
4285 }
4286 }
4287
4288 /* TLS optimizations. Replace instruction sequences and relocs
4289 based on information we collected in tls_optimize. We edit
4290 RELOCS so that --emit-relocs will output something sensible
4291 for the final instruction stream. */
4292 tls_mask = 0;
4293 tls_gd = 0;
4294 if (IS_PPC_TLS_RELOC (r_type))
4295 {
4296 if (h != NULL)
4297 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4298 else if (local_got_offsets != NULL)
4299 {
4300 char *lgot_masks;
4301 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4302 tls_mask = lgot_masks[r_symndx];
4303 }
4304 }
4305
4306 /* Ensure reloc mapping code below stays sane. */
4307 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4308 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4309 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4310 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4311 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4312 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4313 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4314 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4315 abort ();
4316 switch (r_type)
4317 {
4318 default:
4319 break;
4320
4321 case R_PPC_GOT_TPREL16:
4322 case R_PPC_GOT_TPREL16_LO:
4323 if (tls_mask != 0
4324 && (tls_mask & TLS_TPREL) == 0)
4325 {
4326 bfd_vma insn;
4327 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4328 insn &= 31 << 21;
4329 insn |= 0x3c020000; /* addis 0,2,0 */
4330 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4331 r_type = R_PPC_TPREL16_HA;
4332 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4333 }
4334 break;
4335
4336 case R_PPC_TLS:
4337 if (tls_mask != 0
4338 && (tls_mask & TLS_TPREL) == 0)
4339 {
4340 bfd_vma insn, rtra;
4341 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4342 if ((insn & ((31 << 26) | (31 << 11)))
4343 == ((31 << 26) | (2 << 11)))
4344 rtra = insn & ((1 << 26) - (1 << 16));
4345 else if ((insn & ((31 << 26) | (31 << 16)))
4346 == ((31 << 26) | (2 << 16)))
4347 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4348 else
4349 abort ();
4350 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4351 /* add -> addi. */
4352 insn = 14 << 26;
4353 else if ((insn & (31 << 1)) == 23 << 1
4354 && ((insn & (31 << 6)) < 14 << 6
4355 || ((insn & (31 << 6)) >= 16 << 6
4356 && (insn & (31 << 6)) < 24 << 6)))
4357 /* load and store indexed -> dform. */
4358 insn = (32 | ((insn >> 6) & 31)) << 26;
4359 else if ((insn & (31 << 1)) == 21 << 1
4360 && (insn & (0x1a << 6)) == 0)
4361 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4362 insn = (((58 | ((insn >> 6) & 4)) << 26)
4363 | ((insn >> 6) & 1));
4364 else if ((insn & (31 << 1)) == 21 << 1
4365 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4366 /* lwax -> lwa. */
4367 insn = (58 << 26) | 2;
4368 else
4369 abort ();
4370 insn |= rtra;
4371 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4372 r_type = R_PPC_TPREL16_LO;
4373 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4374 /* Was PPC_TLS which sits on insn boundary, now
4375 PPC_TPREL16_LO which is at insn+2. */
4376 rel->r_offset += 2;
4377 }
4378 break;
4379
4380 case R_PPC_GOT_TLSGD16_HI:
4381 case R_PPC_GOT_TLSGD16_HA:
4382 tls_gd = TLS_TPRELGD;
4383 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4384 goto tls_gdld_hi;
4385 break;
4386
4387 case R_PPC_GOT_TLSLD16_HI:
4388 case R_PPC_GOT_TLSLD16_HA:
4389 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4390 {
4391 tls_gdld_hi:
4392 if ((tls_mask & tls_gd) != 0)
4393 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4394 + R_PPC_GOT_TPREL16);
4395 else
4396 {
4397 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4398 rel->r_offset -= 2;
4399 r_type = R_PPC_NONE;
4400 }
4401 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4402 }
4403 break;
4404
4405 case R_PPC_GOT_TLSGD16:
4406 case R_PPC_GOT_TLSGD16_LO:
4407 tls_gd = TLS_TPRELGD;
4408 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4409 goto tls_get_addr_check;
4410 break;
4411
4412 case R_PPC_GOT_TLSLD16:
4413 case R_PPC_GOT_TLSLD16_LO:
4414 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4415 {
4416 tls_get_addr_check:
4417 if (rel + 1 < relend)
4418 {
4419 enum elf_ppc_reloc_type r_type2;
4420 unsigned long r_symndx2;
4421 struct elf_link_hash_entry *h2;
87d243f1 4422 bfd_vma insn1, insn2;
7619e7c7
AM
4423 bfd_vma offset;
4424
4425 /* The next instruction should be a call to
4426 __tls_get_addr. Peek at the reloc to be sure. */
55fd94b0 4427 r_type2 = ELF32_R_TYPE (rel[1].r_info);
7619e7c7
AM
4428 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4429 if (r_symndx2 < symtab_hdr->sh_info
4430 || (r_type2 != R_PPC_REL14
4431 && r_type2 != R_PPC_REL14_BRTAKEN
4432 && r_type2 != R_PPC_REL14_BRNTAKEN
4433 && r_type2 != R_PPC_REL24
4434 && r_type2 != R_PPC_PLTREL24))
4435 break;
4436
4437 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4438 while (h2->root.type == bfd_link_hash_indirect
4439 || h2->root.type == bfd_link_hash_warning)
4440 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4441 if (h2 == NULL || h2 != htab->tls_get_addr)
4442 break;
4443
4444 /* OK, it checks out. Replace the call. */
4445 offset = rel[1].r_offset;
4446 insn1 = bfd_get_32 (output_bfd,
4447 contents + rel->r_offset - 2);
7619e7c7
AM
4448 if ((tls_mask & tls_gd) != 0)
4449 {
4450 /* IE */
4451 insn1 &= (1 << 26) - 1;
4452 insn1 |= 32 << 26; /* lwz */
4453 insn2 = 0x7c631214; /* add 3,3,2 */
4454 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4455 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4456 + R_PPC_GOT_TPREL16);
4457 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4458 }
4459 else
4460 {
4461 /* LE */
4462 insn1 = 0x3c620000; /* addis 3,2,0 */
4463 insn2 = 0x38630000; /* addi 3,3,0 */
4464 if (tls_gd == 0)
4465 {
4466 /* Was an LD reloc. */
4467 r_symndx = 0;
4468 rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
4469 rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
4470 }
4471 r_type = R_PPC_TPREL16_HA;
4472 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4473 rel[1].r_info = ELF32_R_INFO (r_symndx,
4474 R_PPC_TPREL16_LO);
4475 rel[1].r_offset += 2;
4476 }
7619e7c7
AM
4477 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4478 bfd_put_32 (output_bfd, insn2, contents + offset);
7619e7c7
AM
4479 if (tls_gd == 0)
4480 {
4481 /* We changed the symbol on an LD reloc. Start over
70bccea4 4482 in order to get h, sym, sec etc. right. */
7619e7c7
AM
4483 rel--;
4484 continue;
4485 }
4486 }
252b5132 4487 }
7619e7c7
AM
4488 break;
4489 }
4490
4491 /* Handle other relocations that tweak non-addend part of insn. */
4492 branch_bit = 0;
4493 switch (r_type)
4494 {
4495 default:
4496 break;
4497
4498 /* Branch taken prediction relocations. */
4499 case R_PPC_ADDR14_BRTAKEN:
4500 case R_PPC_REL14_BRTAKEN:
4501 branch_bit = BRANCH_PREDICT_BIT;
4502 /* Fall thru */
4503
4504 /* Branch not taken predicition relocations. */
4505 case R_PPC_ADDR14_BRNTAKEN:
4506 case R_PPC_REL14_BRNTAKEN:
4507 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4508 insn &= ~BRANCH_PREDICT_BIT;
4509 insn |= branch_bit;
4510
4511 from = (rel->r_offset
4512 + input_section->output_offset
4513 + input_section->output_section->vma);
4514
4515 /* Invert 'y' bit if not the default. */
4516 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
4517 insn ^= BRANCH_PREDICT_BIT;
4518
4519 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4520 break;
252b5132
RH
4521 }
4522
7619e7c7
AM
4523 addend = rel->r_addend;
4524 tls_type = 0;
4525 howto = NULL;
55fd94b0
AM
4526 if (r_type < R_PPC_max)
4527 howto = ppc_elf_howto_table[r_type];
7619e7c7 4528 switch (r_type)
252b5132
RH
4529 {
4530 default:
7619e7c7
AM
4531 (*_bfd_error_handler)
4532 (_("%s: unknown relocation type %d for symbol %s"),
4533 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
252b5132
RH
4534
4535 bfd_set_error (bfd_error_bad_value);
b34976b6 4536 ret = FALSE;
252b5132
RH
4537 continue;
4538
7619e7c7
AM
4539 case R_PPC_NONE:
4540 case R_PPC_TLS:
4541 case R_PPC_EMB_MRKREF:
4542 case R_PPC_GNU_VTINHERIT:
4543 case R_PPC_GNU_VTENTRY:
7595d193
L
4544 continue;
4545
7619e7c7
AM
4546 /* GOT16 relocations. Like an ADDR16 using the symbol's
4547 address in the GOT as relocation value instead of the
4548 symbol's value itself. Also, create a GOT entry for the
4549 symbol and put the symbol value there. */
4550 case R_PPC_GOT_TLSGD16:
4551 case R_PPC_GOT_TLSGD16_LO:
4552 case R_PPC_GOT_TLSGD16_HI:
4553 case R_PPC_GOT_TLSGD16_HA:
4554 tls_type = TLS_TLS | TLS_GD;
4555 goto dogot;
4556
4557 case R_PPC_GOT_TLSLD16:
4558 case R_PPC_GOT_TLSLD16_LO:
4559 case R_PPC_GOT_TLSLD16_HI:
4560 case R_PPC_GOT_TLSLD16_HA:
4561 tls_type = TLS_TLS | TLS_LD;
4562 goto dogot;
4563
4564 case R_PPC_GOT_TPREL16:
4565 case R_PPC_GOT_TPREL16_LO:
4566 case R_PPC_GOT_TPREL16_HI:
4567 case R_PPC_GOT_TPREL16_HA:
4568 tls_type = TLS_TLS | TLS_TPREL;
4569 goto dogot;
4570
4571 case R_PPC_GOT_DTPREL16:
4572 case R_PPC_GOT_DTPREL16_LO:
4573 case R_PPC_GOT_DTPREL16_HI:
4574 case R_PPC_GOT_DTPREL16_HA:
4575 tls_type = TLS_TLS | TLS_DTPREL;
4576 goto dogot;
4577
4578 case R_PPC_GOT16:
4579 case R_PPC_GOT16_LO:
4580 case R_PPC_GOT16_HI:
4581 case R_PPC_GOT16_HA:
4582 dogot:
4583 {
4584 /* Relocation is to the entry for this symbol in the global
4585 offset table. */
4586 bfd_vma off;
4587 bfd_vma *offp;
4588 unsigned long indx;
4589
4590 if (htab->got == NULL)
4591 abort ();
4592
4593 indx = 0;
4594 if (tls_type == (TLS_TLS | TLS_LD)
d881513a 4595 && (h == NULL
7619e7c7
AM
4596 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4597 offp = &htab->tlsld_got.offset;
4598 else if (h != NULL)
4599 {
4600 bfd_boolean dyn;
4601 dyn = htab->elf.dynamic_sections_created;
ee05f2fe 4602 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7619e7c7
AM
4603 || (info->shared
4604 && SYMBOL_REFERENCES_LOCAL (info, h)))
4605 /* This is actually a static link, or it is a
4606 -Bsymbolic link and the symbol is defined
4607 locally, or the symbol was forced to be local
4608 because of a version file. */
4609 ;
4610 else
4611 {
4612 indx = h->dynindx;
4613 unresolved_reloc = FALSE;
4614 }
4615 offp = &h->got.offset;
4616 }
4617 else
4618 {
4619 if (local_got_offsets == NULL)
4620 abort ();
4621 offp = &local_got_offsets[r_symndx];
4622 }
4623
4624 /* The offset must always be a multiple of 4. We use the
4625 least significant bit to record whether we have already
4626 processed this entry. */
4627 off = *offp;
4628 if ((off & 1) != 0)
4629 off &= ~1;
4630 else
4631 {
70bccea4
AM
4632 unsigned int tls_m = (tls_mask
4633 & (TLS_LD | TLS_GD | TLS_DTPREL
4634 | TLS_TPREL | TLS_TPRELGD));
4635
4636 if (offp == &htab->tlsld_got.offset)
4637 tls_m = TLS_LD;
4638 else if (h == NULL
4639 || !(h->elf_link_hash_flags
4640 & ELF_LINK_HASH_DEF_DYNAMIC))
4641 tls_m &= ~TLS_LD;
4642
4643 /* We might have multiple got entries for this sym.
4644 Initialize them all. */
4645 do
7619e7c7 4646 {
70bccea4
AM
4647 int tls_ty = 0;
4648
4649 if ((tls_m & TLS_LD) != 0)
7619e7c7 4650 {
70bccea4
AM
4651 tls_ty = TLS_TLS | TLS_LD;
4652 tls_m &= ~TLS_LD;
4653 }
4654 else if ((tls_m & TLS_GD) != 0)
4655 {
4656 tls_ty = TLS_TLS | TLS_GD;
4657 tls_m &= ~TLS_GD;
4658 }
4659 else if ((tls_m & TLS_DTPREL) != 0)
4660 {
4661 tls_ty = TLS_TLS | TLS_DTPREL;
4662 tls_m &= ~TLS_DTPREL;
4663 }
4664 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
4665 {
4666 tls_ty = TLS_TLS | TLS_TPREL;
4667 tls_m = 0;
7619e7c7 4668 }
7619e7c7 4669
70bccea4 4670 /* Generate relocs for the dynamic linker. */
4e795f50
AM
4671 if ((info->shared || indx != 0)
4672 && (h == NULL
4673 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4674 || h->root.type != bfd_link_hash_undefweak))
7619e7c7 4675 {
70bccea4
AM
4676 outrel.r_offset = (htab->got->output_section->vma
4677 + htab->got->output_offset
4678 + off);
e515b051 4679 outrel.r_addend = 0;
70bccea4
AM
4680 if (tls_ty & (TLS_LD | TLS_GD))
4681 {
4682 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
70bccea4
AM
4683 if (tls_ty == (TLS_TLS | TLS_GD))
4684 {
4685 loc = htab->relgot->contents;
4686 loc += (htab->relgot->reloc_count++
4687 * sizeof (Elf32_External_Rela));
4688 bfd_elf32_swap_reloca_out (output_bfd,
4689 &outrel, loc);
e515b051 4690 outrel.r_offset += 4;
70bccea4
AM
4691 outrel.r_info
4692 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
70bccea4
AM
4693 }
4694 }
4695 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
4696 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
4697 else if (tls_ty == (TLS_TLS | TLS_TPREL))
4698 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
4699 else if (indx == 0)
4700 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
4701 else
4702 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
70bccea4 4703 if (indx == 0)
e515b051
AM
4704 {
4705 outrel.r_addend += relocation;
4706 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
4707 outrel.r_addend -= htab->tls_sec->vma;
4708 }
70bccea4
AM
4709 loc = htab->relgot->contents;
4710 loc += (htab->relgot->reloc_count++
4711 * sizeof (Elf32_External_Rela));
4712 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7619e7c7
AM
4713 }
4714
70bccea4
AM
4715 /* Init the .got section contents if we're not
4716 emitting a reloc. */
4717 else
7619e7c7 4718 {
70bccea4
AM
4719 bfd_vma value = relocation;
4720
7b609f53
AM
4721 if (tls_ty == (TLS_TLS | TLS_LD))
4722 value = 1;
4723 else if (tls_ty != 0)
70bccea4
AM
4724 {
4725 value -= htab->tls_sec->vma + DTP_OFFSET;
7b609f53 4726 if (tls_ty == (TLS_TLS | TLS_TPREL))
70bccea4 4727 value += DTP_OFFSET - TP_OFFSET;
70bccea4 4728
7b609f53
AM
4729 if (tls_ty == (TLS_TLS | TLS_GD))
4730 {
4731 bfd_put_32 (output_bfd, value,
4732 htab->got->contents + off + 4);
4733 value = 1;
4734 }
70bccea4 4735 }
70bccea4
AM
4736 bfd_put_32 (output_bfd, value,
4737 htab->got->contents + off);
7619e7c7 4738 }
70bccea4
AM
4739
4740 off += 4;
4741 if (tls_ty & (TLS_LD | TLS_GD))
4742 off += 4;
7619e7c7 4743 }
70bccea4
AM
4744 while (tls_m != 0);
4745
4746 off = *offp;
4747 *offp = off | 1;
7619e7c7
AM
4748 }
4749
4750 if (off >= (bfd_vma) -2)
4751 abort ();
4752
70bccea4
AM
4753 if ((tls_type & TLS_TLS) != 0)
4754 {
4755 if (tls_type != (TLS_TLS | TLS_LD))
4756 {
4757 if ((tls_mask & TLS_LD) != 0
4758 && !(h == NULL
4759 || !(h->elf_link_hash_flags
4760 & ELF_LINK_HASH_DEF_DYNAMIC)))
4761 off += 8;
4762 if (tls_type != (TLS_TLS | TLS_GD))
4763 {
4764 if ((tls_mask & TLS_GD) != 0)
4765 off += 8;
4766 if (tls_type != (TLS_TLS | TLS_DTPREL))
4767 {
4768 if ((tls_mask & TLS_DTPREL) != 0)
4769 off += 4;
4770 }
4771 }
4772 }
4773 }
4774
7619e7c7
AM
4775 relocation = htab->got->output_offset + off - 4;
4776
4777 /* Addends on got relocations don't make much sense.
4778 x+off@got is actually x@got+off, and since the got is
4779 generated by a hash table traversal, the value in the
4780 got at entry m+n bears little relation to the entry m. */
4781 if (addend != 0)
4782 (*_bfd_error_handler)
7b609f53 4783 (_("%s(%s+0x%lx): non-zero addend on %s reloc against `%s'"),
7619e7c7
AM
4784 bfd_archive_filename (input_bfd),
4785 bfd_get_section_name (input_bfd, input_section),
4786 (long) rel->r_offset,
7b609f53 4787 howto->name,
7619e7c7
AM
4788 sym_name);
4789 }
70bccea4 4790 break;
7619e7c7 4791
252b5132 4792 /* Relocations that need no special processing. */
7619e7c7 4793 case R_PPC_LOCAL24PC:
252b5132
RH
4794 /* It makes no sense to point a local relocation
4795 at a symbol not in this object. */
7b609f53 4796 if (unresolved_reloc)
252b5132 4797 {
464e1740
ILT
4798 if (! (*info->callbacks->undefined_symbol) (info,
4799 h->root.root.string,
4800 input_bfd,
4801 input_section,
5cc7c785 4802 rel->r_offset,
b34976b6
AM
4803 TRUE))
4804 return FALSE;
252b5132
RH
4805 continue;
4806 }
4807 break;
4808
7619e7c7
AM
4809 case R_PPC_DTPREL16:
4810 case R_PPC_DTPREL16_LO:
4811 case R_PPC_DTPREL16_HI:
4812 case R_PPC_DTPREL16_HA:
4813 addend -= htab->tls_sec->vma + DTP_OFFSET;
4814 break;
4815
70bccea4
AM
4816 /* Relocations that may need to be propagated if this is a shared
4817 object. */
7619e7c7
AM
4818 case R_PPC_TPREL16:
4819 case R_PPC_TPREL16_LO:
4820 case R_PPC_TPREL16_HI:
4821 case R_PPC_TPREL16_HA:
4822 addend -= htab->tls_sec->vma + TP_OFFSET;
4823 /* The TPREL16 relocs shouldn't really be used in shared
4824 libs as they will result in DT_TEXTREL being set, but
4825 support them anyway. */
4826 goto dodyn;
4827
4828 case R_PPC_TPREL32:
4829 addend -= htab->tls_sec->vma + TP_OFFSET;
4830 goto dodyn;
4831
4832 case R_PPC_DTPREL32:
4833 addend -= htab->tls_sec->vma + DTP_OFFSET;
4834 goto dodyn;
4835
e515b051
AM
4836 case R_PPC_DTPMOD32:
4837 relocation = 1;
4838 addend = 0;
4839 goto dodyn;
4840
7619e7c7
AM
4841 case R_PPC_REL24:
4842 case R_PPC_REL32:
4843 case R_PPC_REL14:
4844 case R_PPC_REL14_BRTAKEN:
4845 case R_PPC_REL14_BRNTAKEN:
252b5132
RH
4846 /* If these relocations are not to a named symbol, they can be
4847 handled right here, no need to bother the dynamic linker. */
4848 if (h == NULL
e1e0340b
GK
4849 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4850 || SYMBOL_REFERENCES_LOCAL (info, h))
252b5132 4851 break;
70bccea4 4852 /* fall through */
252b5132 4853
70bccea4
AM
4854 /* Relocations that always need to be propagated if this is a shared
4855 object. */
7619e7c7
AM
4856 case R_PPC_ADDR32:
4857 case R_PPC_ADDR24:
4858 case R_PPC_ADDR16:
4859 case R_PPC_ADDR16_LO:
4860 case R_PPC_ADDR16_HI:
4861 case R_PPC_ADDR16_HA:
4862 case R_PPC_ADDR14:
4863 case R_PPC_ADDR14_BRTAKEN:
4864 case R_PPC_ADDR14_BRNTAKEN:
4865 case R_PPC_UADDR32:
4866 case R_PPC_UADDR16:
ee05f2fe
AM
4867 /* r_symndx will be zero only for relocs against symbols
4868 from removed linkonce sections, or sections discarded by
4869 a linker script. */
7619e7c7 4870 dodyn:
ee05f2fe
AM
4871 if (r_symndx == 0)
4872 break;
4873 /* Fall thru. */
4874
4875 if ((info->shared
4e795f50
AM
4876 && (h == NULL
4877 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4878 || h->root.type != bfd_link_hash_undefweak)
ee05f2fe
AM
4879 && (MUST_BE_DYN_RELOC (r_type)
4880 || (h != NULL
09695f56 4881 && !SYMBOL_CALLS_LOCAL (info, h))))
ee05f2fe
AM
4882 || (ELIMINATE_COPY_RELOCS
4883 && !info->shared
4884 && (input_section->flags & SEC_ALLOC) != 0
4885 && h != NULL
4886 && h->dynindx != -1
4887 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4888 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4889 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
252b5132 4890 {
0bb2d96a 4891 int skip;
252b5132
RH
4892
4893#ifdef DEBUG
55fd94b0
AM
4894 fprintf (stderr, "ppc_elf_relocate_section needs to "
4895 "create relocation for %s\n",
70bccea4
AM
4896 (h && h->root.root.string
4897 ? h->root.root.string : "<unknown>"));
252b5132
RH
4898#endif
4899
4900 /* When generating a shared object, these relocations
70bccea4
AM
4901 are copied into the output file to be resolved at run
4902 time. */
252b5132
RH
4903 if (sreloc == NULL)
4904 {
4905 const char *name;
4906
4907 name = (bfd_elf_string_from_elf_section
4908 (input_bfd,
4909 elf_elfheader (input_bfd)->e_shstrndx,
4910 elf_section_data (input_section)->rel_hdr.sh_name));
4911 if (name == NULL)
b34976b6 4912 return FALSE;
252b5132
RH
4913
4914 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4915 && strcmp (bfd_get_section_name (input_bfd,
4916 input_section),
4917 name + 5) == 0);
4918
7619e7c7 4919 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
252b5132
RH
4920 BFD_ASSERT (sreloc != NULL);
4921 }
4922
0bb2d96a 4923 skip = 0;
252b5132 4924
c629eae0
JJ
4925 outrel.r_offset =
4926 _bfd_elf_section_offset (output_bfd, info, input_section,
4927 rel->r_offset);
0bb2d96a
JJ
4928 if (outrel.r_offset == (bfd_vma) -1
4929 || outrel.r_offset == (bfd_vma) -2)
4930 skip = (int) outrel.r_offset;
252b5132
RH
4931 outrel.r_offset += (input_section->output_section->vma
4932 + input_section->output_offset);
4933
4934 if (skip)
4935 memset (&outrel, 0, sizeof outrel);
ee05f2fe
AM
4936 else if (h != NULL
4937 && !SYMBOL_REFERENCES_LOCAL (info, h))
252b5132 4938 {
7619e7c7 4939 unresolved_reloc = FALSE;
252b5132
RH
4940 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4941 outrel.r_addend = rel->r_addend;
4942 }
4943 else
4944 {
47388f4c
AM
4945 outrel.r_addend = relocation + rel->r_addend;
4946
252b5132 4947 if (r_type == R_PPC_ADDR32)
47388f4c 4948 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
252b5132
RH
4949 else
4950 {
4951 long indx;
4952
47388f4c 4953 if (bfd_is_abs_section (sec))
252b5132
RH
4954 indx = 0;
4955 else if (sec == NULL || sec->owner == NULL)
4956 {
4957 bfd_set_error (bfd_error_bad_value);
b34976b6 4958 return FALSE;
252b5132
RH
4959 }
4960 else
4961 {
4962 asection *osec;
4963
47388f4c
AM
4964 /* We are turning this relocation into one
4965 against a section symbol. It would be
4966 proper to subtract the symbol's value,
4967 osec->vma, from the emitted reloc addend,
4968 but ld.so expects buggy relocs. */
252b5132
RH
4969 osec = sec->output_section;
4970 indx = elf_section_data (osec)->dynindx;
8da6118f 4971 BFD_ASSERT (indx > 0);
252b5132
RH
4972#ifdef DEBUG
4973 if (indx <= 0)
55fd94b0
AM
4974 printf ("indx=%d section=%s flags=%08x name=%s\n",
4975 indx, osec->name, osec->flags,
4976 h->root.root.string);
252b5132
RH
4977#endif
4978 }
4979
4980 outrel.r_info = ELF32_R_INFO (indx, r_type);
252b5132
RH
4981 }
4982 }
4983
947216bf
AM
4984 loc = sreloc->contents;
4985 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4986 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
252b5132 4987
2243deae 4988 if (skip == -1)
252b5132 4989 continue;
2243deae
L
4990
4991 /* This reloc will be computed at runtime. We clear the memory
4992 so that it contains predictable value. */
4993 if (! skip
4994 && ((input_section->flags & SEC_ALLOC) != 0
4995 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
4996 {
4997 relocation = howto->pc_relative ? outrel.r_offset : 0;
4998 addend = 0;
4999 break;
5000 }
252b5132 5001 }
252b5132
RH
5002 break;
5003
70bccea4 5004 /* Indirect .sdata relocation. */
7619e7c7
AM
5005 case R_PPC_EMB_SDAI16:
5006 BFD_ASSERT (htab->sdata != NULL);
5007 relocation
5008 = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
5009 info, htab->sdata, h,
5010 relocation, rel,
5011 R_PPC_RELATIVE);
252b5132
RH
5012 break;
5013
70bccea4 5014 /* Indirect .sdata2 relocation. */
7619e7c7
AM
5015 case R_PPC_EMB_SDA2I16:
5016 BFD_ASSERT (htab->sdata2 != NULL);
5017 relocation
5018 = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
5019 info, htab->sdata2, h,
5020 relocation, rel,
5021 R_PPC_RELATIVE);
252b5132
RH
5022 break;
5023
70bccea4
AM
5024 /* Handle the TOC16 reloc. We want to use the offset within the .got
5025 section, not the actual VMA. This is appropriate when generating
5026 an embedded ELF object, for which the .got section acts like the
5027 AIX .toc section. */
7619e7c7 5028 case R_PPC_TOC16: /* phony GOT16 relocations */
55fd94b0 5029 BFD_ASSERT (sec != NULL);
252b5132
RH
5030 BFD_ASSERT (bfd_is_und_section (sec)
5031 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5032 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
5033
70bccea4 5034 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
252b5132
RH
5035 break;
5036
7619e7c7 5037 case R_PPC_PLTREL24:
252b5132 5038 /* Relocation is to the entry for this symbol in the
70bccea4 5039 procedure linkage table. */
252b5132
RH
5040 BFD_ASSERT (h != NULL);
5041
5042 if (h->plt.offset == (bfd_vma) -1
7619e7c7 5043 || htab->plt == NULL)
252b5132
RH
5044 {
5045 /* We didn't make a PLT entry for this symbol. This
70bccea4
AM
5046 happens when statically linking PIC code, or when
5047 using -Bsymbolic. */
252b5132
RH
5048 break;
5049 }
5050
7619e7c7
AM
5051 unresolved_reloc = FALSE;
5052 relocation = (htab->plt->output_section->vma
5053 + htab->plt->output_offset
252b5132 5054 + h->plt.offset);
8da6118f 5055 break;
252b5132 5056
70bccea4 5057 /* Relocate against _SDA_BASE_. */
7619e7c7 5058 case R_PPC_SDAREL16:
252b5132
RH
5059 {
5060 const char *name;
7619e7c7 5061 const struct elf_link_hash_entry *sh;
252b5132 5062
55fd94b0 5063 BFD_ASSERT (sec != NULL);
252b5132 5064 name = bfd_get_section_name (abfd, sec->output_section);
5f819128
AM
5065 if (! ((strncmp (name, ".sdata", 6) == 0
5066 && (name[6] == 0 || name[6] == '.'))
5067 || (strncmp (name, ".sbss", 5) == 0
5068 && (name[5] == 0 || name[5] == '.'))))
252b5132 5069 {
55fd94b0
AM
5070 (*_bfd_error_handler)
5071 (_("%s: the target (%s) of a %s relocation is "
5072 "in the wrong output section (%s)"),
5073 bfd_archive_filename (input_bfd),
5074 sym_name,
5075 howto->name,
5076 name);
252b5132 5077 }
7619e7c7
AM
5078 sh = htab->sdata->sym_hash;
5079 addend -= (sh->root.u.def.value
5080 + sh->root.u.def.section->output_section->vma
5081 + sh->root.u.def.section->output_offset);
252b5132
RH
5082 }
5083 break;
5084
70bccea4 5085 /* Relocate against _SDA2_BASE_. */
7619e7c7 5086 case R_PPC_EMB_SDA2REL:
252b5132
RH
5087 {
5088 const char *name;
7619e7c7 5089 const struct elf_link_hash_entry *sh;
252b5132 5090
55fd94b0 5091 BFD_ASSERT (sec != NULL);
252b5132 5092 name = bfd_get_section_name (abfd, sec->output_section);
5f819128
AM
5093 if (! (strncmp (name, ".sdata2", 7) == 0
5094 || strncmp (name, ".sbss2", 6) == 0))
252b5132 5095 {
55fd94b0
AM
5096 (*_bfd_error_handler)
5097 (_("%s: the target (%s) of a %s relocation is "
5098 "in the wrong output section (%s)"),
5099 bfd_archive_filename (input_bfd),
5100 sym_name,
5101 howto->name,
5102 name);
c3668558 5103
252b5132 5104 bfd_set_error (bfd_error_bad_value);
b34976b6 5105 ret = FALSE;
252b5132
RH
5106 continue;
5107 }
7619e7c7
AM
5108 sh = htab->sdata2->sym_hash;
5109 addend -= (sh->root.u.def.value
5110 + sh->root.u.def.section->output_section->vma
5111 + sh->root.u.def.section->output_offset);
252b5132
RH
5112 }
5113 break;
5114
70bccea4 5115 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
7619e7c7
AM
5116 case R_PPC_EMB_SDA21:
5117 case R_PPC_EMB_RELSDA:
252b5132
RH
5118 {
5119 const char *name;
7619e7c7 5120 const struct elf_link_hash_entry *sh;
252b5132
RH
5121 int reg;
5122
55fd94b0 5123 BFD_ASSERT (sec != NULL);
252b5132 5124 name = bfd_get_section_name (abfd, sec->output_section);
b34976b6 5125 if (((strncmp (name, ".sdata", 6) == 0
5f819128
AM
5126 && (name[6] == 0 || name[6] == '.'))
5127 || (strncmp (name, ".sbss", 5) == 0
5128 && (name[5] == 0 || name[5] == '.'))))
252b5132
RH
5129 {
5130 reg = 13;
7619e7c7
AM
5131 sh = htab->sdata->sym_hash;
5132 addend -= (sh->root.u.def.value
5133 + sh->root.u.def.section->output_section->vma
5134 + sh->root.u.def.section->output_offset);
252b5132
RH
5135 }
5136
5f819128
AM
5137 else if (strncmp (name, ".sdata2", 7) == 0
5138 || strncmp (name, ".sbss2", 6) == 0)
252b5132
RH
5139 {
5140 reg = 2;
7619e7c7
AM
5141 sh = htab->sdata2->sym_hash;
5142 addend -= (sh->root.u.def.value
5143 + sh->root.u.def.section->output_section->vma
5144 + sh->root.u.def.section->output_offset);
252b5132
RH
5145 }
5146
8da6118f
KH
5147 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5148 || strcmp (name, ".PPC.EMB.sbss0") == 0)
252b5132
RH
5149 {
5150 reg = 0;
5151 }
5152
5153 else
5154 {
55fd94b0
AM
5155 (*_bfd_error_handler)
5156 (_("%s: the target (%s) of a %s relocation is "
5157 "in the wrong output section (%s)"),
5158 bfd_archive_filename (input_bfd),
5159 sym_name,
5160 howto->name,
5161 name);
252b5132
RH
5162
5163 bfd_set_error (bfd_error_bad_value);
b34976b6 5164 ret = FALSE;
252b5132
RH
5165 continue;
5166 }
5167
5168 if (r_type == R_PPC_EMB_SDA21)
5169 { /* fill in register field */
7619e7c7 5170 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
252b5132 5171 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
7619e7c7 5172 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
252b5132
RH
5173 }
5174 }
5175 break;
5176
70bccea4 5177 /* Relocate against the beginning of the section. */
7619e7c7
AM
5178 case R_PPC_SECTOFF:
5179 case R_PPC_SECTOFF_LO:
5180 case R_PPC_SECTOFF_HI:
5181 case R_PPC_SECTOFF_HA:
55fd94b0 5182 BFD_ASSERT (sec != NULL);
252b5132 5183 addend -= sec->output_section->vma;
252b5132
RH
5184 break;
5185
70bccea4 5186 /* Negative relocations. */
7619e7c7
AM
5187 case R_PPC_EMB_NADDR32:
5188 case R_PPC_EMB_NADDR16:
5189 case R_PPC_EMB_NADDR16_LO:
5190 case R_PPC_EMB_NADDR16_HI:
5191 case R_PPC_EMB_NADDR16_HA:
8da6118f 5192 addend -= 2 * relocation;
252b5132
RH
5193 break;
5194
7619e7c7
AM
5195 case R_PPC_COPY:
5196 case R_PPC_GLOB_DAT:
5197 case R_PPC_JMP_SLOT:
5198 case R_PPC_RELATIVE:
5199 case R_PPC_PLT32:
5200 case R_PPC_PLTREL32:
5201 case R_PPC_PLT16_LO:
5202 case R_PPC_PLT16_HI:
5203 case R_PPC_PLT16_HA:
5204 case R_PPC_ADDR30:
5205 case R_PPC_EMB_RELSEC16:
5206 case R_PPC_EMB_RELST_LO:
5207 case R_PPC_EMB_RELST_HI:
5208 case R_PPC_EMB_RELST_HA:
5209 case R_PPC_EMB_BIT_FLD:
5210 (*_bfd_error_handler)
7b609f53 5211 (_("%s: relocation %s is not yet supported for symbol %s."),
7619e7c7
AM
5212 bfd_archive_filename (input_bfd),
5213 howto->name,
5214 sym_name);
252b5132
RH
5215
5216 bfd_set_error (bfd_error_invalid_operation);
b34976b6 5217 ret = FALSE;
252b5132 5218 continue;
7619e7c7 5219 }
252b5132 5220
7619e7c7
AM
5221 /* Do any further special processing. */
5222 switch (r_type)
5223 {
5224 default:
5225 break;
5226
5227 case R_PPC_ADDR16_HA:
5228 case R_PPC_GOT16_HA:
5229 case R_PPC_PLT16_HA:
5230 case R_PPC_SECTOFF_HA:
5231 case R_PPC_TPREL16_HA:
5232 case R_PPC_DTPREL16_HA:
5233 case R_PPC_GOT_TLSGD16_HA:
5234 case R_PPC_GOT_TLSLD16_HA:
5235 case R_PPC_GOT_TPREL16_HA:
5236 case R_PPC_GOT_DTPREL16_HA:
5237 case R_PPC_EMB_NADDR16_HA:
5238 case R_PPC_EMB_RELST_HA:
5239 /* It's just possible that this symbol is a weak symbol
7b609f53 5240 that's not actually defined anywhere. In that case,
7619e7c7
AM
5241 'sec' would be NULL, and we should leave the symbol
5242 alone (it will be set to zero elsewhere in the link). */
5243 if (sec != NULL)
e515b051
AM
5244 /* Add 0x10000 if sign bit in 0:15 is set.
5245 Bits 0:15 are not used. */
5246 addend += 0x8000;
7619e7c7 5247 break;
252b5132
RH
5248 }
5249
252b5132 5250#ifdef DEBUG
55fd94b0
AM
5251 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5252 "offset = %ld, addend = %ld\n",
252b5132 5253 howto->name,
8da6118f 5254 (int) r_type,
252b5132
RH
5255 sym_name,
5256 r_symndx,
7619e7c7 5257 (long) rel->r_offset,
8da6118f 5258 (long) addend);
252b5132
RH
5259#endif
5260
7619e7c7
AM
5261 if (unresolved_reloc
5262 && !((input_section->flags & SEC_DEBUGGING) != 0
5263 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5264 {
5265 (*_bfd_error_handler)
7b609f53 5266 (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
7619e7c7
AM
5267 bfd_archive_filename (input_bfd),
5268 bfd_get_section_name (input_bfd, input_section),
5269 (long) rel->r_offset,
7b609f53 5270 howto->name,
7619e7c7
AM
5271 sym_name);
5272 ret = FALSE;
5273 }
5274
252b5132
RH
5275 r = _bfd_final_link_relocate (howto,
5276 input_bfd,
5277 input_section,
5278 contents,
7619e7c7 5279 rel->r_offset,
252b5132
RH
5280 relocation,
5281 addend);
5282
7619e7c7 5283 if (r != bfd_reloc_ok)
252b5132 5284 {
7619e7c7
AM
5285 if (sym_name == NULL)
5286 sym_name = "(null)";
5287 if (r == bfd_reloc_overflow)
252b5132 5288 {
7619e7c7
AM
5289 if (warned)
5290 continue;
5291 if (h != NULL
5292 && h->root.type == bfd_link_hash_undefweak
dc1bc0c9
RH
5293 && howto->pc_relative)
5294 {
5295 /* Assume this is a call protected by other code that
5296 detect the symbol is undefined. If this is the case,
5297 we can safely ignore the overflow. If not, the
5298 program is hosed anyway, and a little warning isn't
5299 going to help. */
252b5132 5300
dc1bc0c9
RH
5301 continue;
5302 }
252b5132 5303
7619e7c7
AM
5304 if (! (*info->callbacks->reloc_overflow) (info,
5305 sym_name,
5306 howto->name,
5307 rel->r_addend,
5308 input_bfd,
5309 input_section,
5310 rel->r_offset))
5311 return FALSE;
dc1bc0c9
RH
5312 }
5313 else
5314 {
7619e7c7 5315 (*_bfd_error_handler)
7b609f53 5316 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
7619e7c7
AM
5317 bfd_archive_filename (input_bfd),
5318 bfd_get_section_name (input_bfd, input_section),
7b609f53 5319 (long) rel->r_offset, howto->name, sym_name, (int) r);
7619e7c7 5320 ret = FALSE;
252b5132
RH
5321 }
5322 }
5323 }
5324
252b5132
RH
5325#ifdef DEBUG
5326 fprintf (stderr, "\n");
5327#endif
5328
5329 return ret;
5330}
29c2fb7c
AJ
5331
5332static enum elf_reloc_type_class
55fd94b0 5333ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
29c2fb7c 5334{
55fd94b0 5335 switch (ELF32_R_TYPE (rela->r_info))
29c2fb7c
AJ
5336 {
5337 case R_PPC_RELATIVE:
5338 return reloc_class_relative;
5339 case R_PPC_REL24:
5340 case R_PPC_ADDR24:
5341 case R_PPC_JMP_SLOT:
5342 return reloc_class_plt;
5343 case R_PPC_COPY:
5344 return reloc_class_copy;
5345 default:
5346 return reloc_class_normal;
5347 }
5348}
252b5132 5349\f
ae9a127f
NC
5350/* Support for core dump NOTE sections. */
5351
b34976b6 5352static bfd_boolean
55fd94b0 5353ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
c5fccbec
DJ
5354{
5355 int offset;
dc810e39 5356 unsigned int raw_size;
c5fccbec
DJ
5357
5358 switch (note->descsz)
5359 {
70bccea4
AM
5360 default:
5361 return FALSE;
c5fccbec 5362
70bccea4
AM
5363 case 268: /* Linux/PPC. */
5364 /* pr_cursig */
5365 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
c5fccbec 5366
70bccea4
AM
5367 /* pr_pid */
5368 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
c5fccbec 5369
70bccea4
AM
5370 /* pr_reg */
5371 offset = 72;
5372 raw_size = 192;
c5fccbec 5373
70bccea4 5374 break;
c5fccbec
DJ
5375 }
5376
5377 /* Make a ".reg/999" section. */
5378 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5379 raw_size, note->descpos + offset);
5380}
5381
b34976b6 5382static bfd_boolean
55fd94b0 5383ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
c5fccbec
DJ
5384{
5385 switch (note->descsz)
5386 {
70bccea4
AM
5387 default:
5388 return FALSE;
c5fccbec 5389
70bccea4
AM
5390 case 128: /* Linux/PPC elf_prpsinfo. */
5391 elf_tdata (abfd)->core_program
5392 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
5393 elf_tdata (abfd)->core_command
5394 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
c5fccbec
DJ
5395 }
5396
5397 /* Note that for some reason, a spurious space is tacked
5398 onto the end of the args in some (at least one anyway)
5399 implementations, so strip it off if it exists. */
5400
5401 {
5402 char *command = elf_tdata (abfd)->core_command;
5403 int n = strlen (command);
5404
5405 if (0 < n && command[n - 1] == ' ')
5406 command[n - 1] = '\0';
5407 }
5408
b34976b6 5409 return TRUE;
c5fccbec
DJ
5410}
5411\f
e1a9cb8e
NC
5412/* Very simple linked list structure for recording apuinfo values. */
5413typedef struct apuinfo_list
5414{
70bccea4
AM
5415 struct apuinfo_list *next;
5416 unsigned long value;
e1a9cb8e
NC
5417}
5418apuinfo_list;
5419
55fd94b0 5420static apuinfo_list *head;
e1a9cb8e
NC
5421
5422
e1a9cb8e 5423static void
55fd94b0 5424apuinfo_list_init (void)
e1a9cb8e
NC
5425{
5426 head = NULL;
5427}
5428
5429static void
55fd94b0 5430apuinfo_list_add (unsigned long value)
e1a9cb8e 5431{
70bccea4 5432 apuinfo_list *entry = head;
e1a9cb8e
NC
5433
5434 while (entry != NULL)
5435 {
5436 if (entry->value == value)
5437 return;
5438 entry = entry->next;
5439 }
5440
5441 entry = bfd_malloc (sizeof (* entry));
5442 if (entry == NULL)
5443 return;
5444
5445 entry->value = value;
5446 entry->next = head;
5447 head = entry;
5448}
5449
5450static unsigned
55fd94b0 5451apuinfo_list_length (void)
e1a9cb8e 5452{
70bccea4 5453 apuinfo_list *entry;
e1a9cb8e
NC
5454 unsigned long count;
5455
5456 for (entry = head, count = 0;
5457 entry;
5458 entry = entry->next)
5459 ++ count;
5460
5461 return count;
5462}
5463
5464static inline unsigned long
55fd94b0 5465apuinfo_list_element (unsigned long number)
e1a9cb8e
NC
5466{
5467 apuinfo_list * entry;
5468
5469 for (entry = head;
5470 entry && number --;
5471 entry = entry->next)
5472 ;
5473
5474 return entry ? entry->value : 0;
5475}
5476
5477static void
55fd94b0 5478apuinfo_list_finish (void)
e1a9cb8e 5479{
70bccea4 5480 apuinfo_list *entry;
e1a9cb8e
NC
5481
5482 for (entry = head; entry;)
5483 {
70bccea4 5484 apuinfo_list *next = entry->next;
e1a9cb8e
NC
5485 free (entry);
5486 entry = next;
5487 }
5488
5489 head = NULL;
5490}
5491
70bccea4
AM
5492#define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
5493#define APUINFO_LABEL "APUinfo"
e1a9cb8e
NC
5494
5495/* Scan the input BFDs and create a linked list of
5496 the APUinfo values that will need to be emitted. */
5497
55fd94b0
AM
5498static void
5499ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
e1a9cb8e 5500{
70bccea4
AM
5501 bfd *ibfd;
5502 asection *asec;
5503 char *buffer;
5504 unsigned num_input_sections;
e1a9cb8e 5505 bfd_size_type output_section_size;
70bccea4
AM
5506 unsigned i;
5507 unsigned num_entries;
e1a9cb8e
NC
5508 unsigned long offset;
5509 unsigned long length;
70bccea4 5510 const char *error_message = NULL;
e1a9cb8e 5511
6b0817e5
NC
5512 if (link_info == NULL)
5513 return;
5514
e1a9cb8e
NC
5515 /* Scan the input bfds, looking for apuinfo sections. */
5516 num_input_sections = 0;
5517 output_section_size = 0;
5518
5519 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5520 {
5521 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5522 if (asec)
5523 {
5524 ++ num_input_sections;
5525 output_section_size += asec->_raw_size;
5526 }
5527 }
5528
5529 /* We need at least one input sections
5530 in order to make merging worthwhile. */
5531 if (num_input_sections < 1)
5532 return;
5533
5534 /* Just make sure that the output section exists as well. */
5535 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5536 if (asec == NULL)
5537 return;
5538
5539 /* Allocate a buffer for the contents of the input sections. */
5540 buffer = bfd_malloc (output_section_size);
5541 if (buffer == NULL)
5542 return;
5543
5544 offset = 0;
5545 apuinfo_list_init ();
5546
5547 /* Read in the input sections contents. */
5548 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5549 {
70bccea4
AM
5550 unsigned long datum;
5551 char *ptr;
86bbe32f 5552
e1a9cb8e
NC
5553 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5554 if (asec == NULL)
5555 continue;
5556
5557 length = asec->_raw_size;
5558 if (length < 24)
5559 {
5560 error_message = _("corrupt or empty %s section in %s");
5561 goto fail;
5562 }
86bbe32f 5563
e1a9cb8e
NC
5564 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
5565 || (bfd_bread (buffer + offset, length, ibfd) != length))
5566 {
5567 error_message = _("unable to read in %s section from %s");
5568 goto fail;
5569 }
5570
5571 /* Process the contents of the section. */
5572 ptr = buffer + offset;
5573 error_message = _("corrupt %s section in %s");
5574
5575 /* Verify the contents of the header. Note - we have to
5576 extract the values this way in order to allow for a
5577 host whose endian-ness is different from the target. */
5578 datum = bfd_get_32 (ibfd, ptr);
5579 if (datum != sizeof APUINFO_LABEL)
5580 goto fail;
5581
5582 datum = bfd_get_32 (ibfd, ptr + 8);
5583 if (datum != 0x2)
5584 goto fail;
5585
5586 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
5587 goto fail;
5588
88810f8c 5589 /* Get the number of bytes used for apuinfo entries. */
e1a9cb8e 5590 datum = bfd_get_32 (ibfd, ptr + 4);
88810f8c 5591 if (datum + 20 != length)
e1a9cb8e
NC
5592 goto fail;
5593
5594 /* Make sure that we do not run off the end of the section. */
5595 if (offset + length > output_section_size)
5596 goto fail;
5597
5598 /* Scan the apuinfo section, building a list of apuinfo numbers. */
88810f8c
MM
5599 for (i = 0; i < datum; i += 4)
5600 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
e1a9cb8e
NC
5601
5602 /* Update the offset. */
5603 offset += length;
5604 }
5605
5606 error_message = NULL;
5607
5608 /* Compute the size of the output section. */
5609 num_entries = apuinfo_list_length ();
5610 output_section_size = 20 + num_entries * 4;
5611
5612 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5613
70bccea4 5614 if (! bfd_set_section_size (abfd, asec, output_section_size))
e1a9cb8e
NC
5615 ibfd = abfd,
5616 error_message = _("warning: unable to set size of %s section in %s");
86bbe32f 5617
e1a9cb8e
NC
5618 fail:
5619 free (buffer);
5620
5621 if (error_message)
70bccea4
AM
5622 (*_bfd_error_handler) (error_message, APUINFO_SECTION_NAME,
5623 bfd_archive_filename (ibfd));
e1a9cb8e
NC
5624}
5625
5626
5627/* Prevent the output section from accumulating the input sections'
5628 contents. We have already stored this in our linked list structure. */
5629
55fd94b0
AM
5630static bfd_boolean
5631ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
5632 asection *asec,
5633 bfd_byte *contents ATTRIBUTE_UNUSED)
e1a9cb8e 5634{
70bccea4
AM
5635 return (apuinfo_list_length ()
5636 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
e1a9cb8e
NC
5637}
5638
5639
5640/* Finally we can generate the output section. */
5641
55fd94b0
AM
5642static void
5643ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
e1a9cb8e 5644{
70bccea4
AM
5645 bfd_byte *buffer;
5646 asection *asec;
5647 unsigned i;
5648 unsigned num_entries;
e1a9cb8e
NC
5649 bfd_size_type length;
5650
5651 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5652 if (asec == NULL)
5653 return;
5654
6b0817e5
NC
5655 if (apuinfo_list_length () == 0)
5656 return;
5657
e1a9cb8e
NC
5658 length = asec->_raw_size;
5659 if (length < 20)
5660 return;
5661
5662 buffer = bfd_malloc (length);
5663 if (buffer == NULL)
5664 {
70bccea4
AM
5665 (*_bfd_error_handler)
5666 (_("failed to allocate space for new APUinfo section."));
e1a9cb8e
NC
5667 return;
5668 }
5669
5670 /* Create the apuinfo header. */
5671 num_entries = apuinfo_list_length ();
5672 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
88810f8c 5673 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
e1a9cb8e
NC
5674 bfd_put_32 (abfd, 0x2, buffer + 8);
5675 strcpy (buffer + 12, APUINFO_LABEL);
86bbe32f 5676
e1a9cb8e
NC
5677 length = 20;
5678 for (i = 0; i < num_entries; i++)
5679 {
5680 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
5681 length += 4;
5682 }
5683
5684 if (length != asec->_raw_size)
70bccea4 5685 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
86bbe32f 5686
e1a9cb8e 5687 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
70bccea4 5688 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
86bbe32f 5689
e1a9cb8e
NC
5690 free (buffer);
5691
5692 apuinfo_list_finish ();
5693}
5694\f
252b5132
RH
5695#define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
5696#define TARGET_LITTLE_NAME "elf32-powerpcle"
5697#define TARGET_BIG_SYM bfd_elf32_powerpc_vec
5698#define TARGET_BIG_NAME "elf32-powerpc"
5699#define ELF_ARCH bfd_arch_powerpc
5700#define ELF_MACHINE_CODE EM_PPC
5701#define ELF_MAXPAGESIZE 0x10000
5702#define elf_info_to_howto ppc_elf_info_to_howto
5703
5704#ifdef EM_CYGNUS_POWERPC
5705#define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
5706#endif
5707
5708#ifdef EM_PPC_OLD
5709#define ELF_MACHINE_ALT2 EM_PPC_OLD
5710#endif
5711
5712#define elf_backend_plt_not_loaded 1
5713#define elf_backend_got_symbol_offset 4
5714#define elf_backend_can_gc_sections 1
51b64d56 5715#define elf_backend_can_refcount 1
252b5132
RH
5716#define elf_backend_got_header_size 12
5717#define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
b491616a 5718#define elf_backend_rela_normal 1
252b5132 5719
252b5132 5720#define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
70bccea4 5721#define bfd_elf32_bfd_relax_section ppc_elf_relax_section
252b5132
RH
5722#define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
5723#define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
70bccea4 5724#define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
252b5132 5725
feee612b 5726#define elf_backend_object_p ppc_elf_object_p
252b5132
RH
5727#define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
5728#define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
5729#define elf_backend_section_from_shdr ppc_elf_section_from_shdr
5730#define elf_backend_relocate_section ppc_elf_relocate_section
5731#define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
5732#define elf_backend_check_relocs ppc_elf_check_relocs
7fce784e 5733#define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
252b5132
RH
5734#define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
5735#define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
5736#define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
5737#define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
5738#define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
5739#define elf_backend_fake_sections ppc_elf_fake_sections
5740#define elf_backend_additional_program_headers ppc_elf_additional_program_headers
5741#define elf_backend_modify_segment_map ppc_elf_modify_segment_map
c5fccbec
DJ
5742#define elf_backend_grok_prstatus ppc_elf_grok_prstatus
5743#define elf_backend_grok_psinfo ppc_elf_grok_psinfo
29c2fb7c 5744#define elf_backend_reloc_type_class ppc_elf_reloc_type_class
70bccea4
AM
5745#define elf_backend_begin_write_processing ppc_elf_begin_write_processing
5746#define elf_backend_final_write_processing ppc_elf_final_write_processing
5747#define elf_backend_write_section ppc_elf_write_section
252b5132
RH
5748
5749#include "elf32-target.h"
This page took 0.851429 seconds and 4 git commands to generate.