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