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