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