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