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