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