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