* elf32-i386.c: Add missing prototypes.
[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
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 Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 /* This file is based on a preliminary PowerPC ELF ABI. The
23 information may not match the final PowerPC ELF ABI. It includes
24 suggestions from the in-progress Embedded PowerPC ABI, and that
25 information may also not match. */
26
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "bfdlink.h"
30 #include "libbfd.h"
31 #include "elf-bfd.h"
32 #include "elf/ppc.h"
33
34 #define USE_RELA /* we want RELA relocations, not REL */
35
36 static reloc_howto_type *ppc_elf_reloc_type_lookup
37 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
38 static void ppc_elf_info_to_howto
39 PARAMS ((bfd *abfd, arelent *cache_ptr, Elf32_Internal_Rela *dst));
40 static void ppc_elf_howto_init PARAMS ((void));
41 static int ppc_elf_sort_rela PARAMS ((const PTR, const PTR));
42 static boolean ppc_elf_relax_section
43 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
44 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
45 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
46 static boolean ppc_elf_set_private_flags PARAMS ((bfd *, flagword));
47 static boolean ppc_elf_copy_private_bfd_data PARAMS ((bfd *, bfd *));
48 static boolean ppc_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
49
50 static int ppc_elf_additional_program_headers PARAMS ((bfd *));
51 static boolean ppc_elf_modify_segment_map PARAMS ((bfd *));
52
53 static boolean ppc_elf_create_dynamic_sections
54 PARAMS ((bfd *, struct bfd_link_info *));
55
56 static boolean ppc_elf_section_from_shdr PARAMS ((bfd *,
57 Elf32_Internal_Shdr *,
58 char *));
59 static boolean ppc_elf_fake_sections
60 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
61
62 static elf_linker_section_t *ppc_elf_create_linker_section
63 PARAMS ((bfd *abfd,
64 struct bfd_link_info *info,
65 enum elf_linker_section_enum));
66
67 static boolean ppc_elf_check_relocs PARAMS ((bfd *,
68 struct bfd_link_info *,
69 asection *,
70 const Elf_Internal_Rela *));
71
72 static asection * ppc_elf_gc_mark_hook PARAMS ((bfd *abfd,
73 struct bfd_link_info *info,
74 Elf_Internal_Rela *rel,
75 struct elf_link_hash_entry *h,
76 Elf_Internal_Sym *sym));
77
78 static boolean ppc_elf_gc_sweep_hook PARAMS ((bfd *abfd,
79 struct bfd_link_info *info,
80 asection *sec,
81 const Elf_Internal_Rela *relocs));
82
83 static boolean ppc_elf_adjust_dynamic_symbol PARAMS ((struct bfd_link_info *,
84 struct elf_link_hash_entry *));
85
86 static boolean ppc_elf_size_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
87
88 static boolean ppc_elf_relocate_section PARAMS ((bfd *,
89 struct bfd_link_info *info,
90 bfd *,
91 asection *,
92 bfd_byte *,
93 Elf_Internal_Rela *relocs,
94 Elf_Internal_Sym *local_syms,
95 asection **));
96
97 static boolean ppc_elf_add_symbol_hook PARAMS ((bfd *,
98 struct bfd_link_info *,
99 const Elf_Internal_Sym *,
100 const char **,
101 flagword *,
102 asection **,
103 bfd_vma *));
104
105 static boolean ppc_elf_finish_dynamic_symbol PARAMS ((bfd *,
106 struct bfd_link_info *,
107 struct elf_link_hash_entry *,
108 Elf_Internal_Sym *));
109
110 static boolean ppc_elf_finish_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
111 static enum elf_reloc_type_class ppc_elf_reloc_type_class PARAMS ((int));
112 static boolean ppc_elf_grok_prstatus
113 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
114 static boolean ppc_elf_grok_psinfo
115 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
116
117 #define BRANCH_PREDICT_BIT 0x200000 /* branch prediction bit for branch taken relocs */
118 #define RA_REGISTER_MASK 0x001f0000 /* mask to set RA in memory instructions */
119 #define RA_REGISTER_SHIFT 16 /* value to shift register by to insert RA */
120
121 /* The name of the dynamic interpreter. This is put in the .interp
122 section. */
123
124 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
125
126 /* The size in bytes of an entry in the procedure linkage table. */
127 #define PLT_ENTRY_SIZE 12
128 /* The initial size of the plt reserved for the dynamic linker. */
129 #define PLT_INITIAL_ENTRY_SIZE 72
130 /* The size of the gap between entries in the PLT. */
131 #define PLT_SLOT_SIZE 8
132 /* The number of single-slot PLT entries (the rest use two slots). */
133 #define PLT_NUM_SINGLE_ENTRIES 8192
134
135 /* Will references to this symbol always reference the symbol
136 in this object? */
137 #define SYMBOL_REFERENCES_LOCAL(INFO, H) \
138 ((! INFO->shared \
139 || INFO->symbolic \
140 || H->dynindx == -1 \
141 || ELF_ST_VISIBILITY (H->other) == STV_INTERNAL \
142 || ELF_ST_VISIBILITY (H->other) == STV_HIDDEN) \
143 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
144
145 /* Will _calls_ to this symbol always call the version in this object? */
146 #define SYMBOL_CALLS_LOCAL(INFO, H) \
147 ((! INFO->shared \
148 || INFO->symbolic \
149 || H->dynindx == -1 \
150 || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT) \
151 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
152 \f
153 static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
154
155 static reloc_howto_type ppc_elf_howto_raw[] = {
156 /* This reloc does nothing. */
157 HOWTO (R_PPC_NONE, /* type */
158 0, /* rightshift */
159 2, /* size (0 = byte, 1 = short, 2 = long) */
160 32, /* bitsize */
161 false, /* pc_relative */
162 0, /* bitpos */
163 complain_overflow_bitfield, /* complain_on_overflow */
164 bfd_elf_generic_reloc, /* special_function */
165 "R_PPC_NONE", /* name */
166 false, /* partial_inplace */
167 0, /* src_mask */
168 0, /* dst_mask */
169 false), /* pcrel_offset */
170
171 /* A standard 32 bit relocation. */
172 HOWTO (R_PPC_ADDR32, /* type */
173 0, /* rightshift */
174 2, /* size (0 = byte, 1 = short, 2 = long) */
175 32, /* bitsize */
176 false, /* pc_relative */
177 0, /* bitpos */
178 complain_overflow_bitfield, /* complain_on_overflow */
179 bfd_elf_generic_reloc, /* special_function */
180 "R_PPC_ADDR32", /* name */
181 false, /* partial_inplace */
182 0, /* src_mask */
183 0xffffffff, /* dst_mask */
184 false), /* pcrel_offset */
185
186 /* An absolute 26 bit branch; the lower two bits must be zero.
187 FIXME: we don't check that, we just clear them. */
188 HOWTO (R_PPC_ADDR24, /* type */
189 0, /* rightshift */
190 2, /* size (0 = byte, 1 = short, 2 = long) */
191 26, /* bitsize */
192 false, /* pc_relative */
193 0, /* bitpos */
194 complain_overflow_bitfield, /* complain_on_overflow */
195 bfd_elf_generic_reloc, /* special_function */
196 "R_PPC_ADDR24", /* name */
197 false, /* partial_inplace */
198 0, /* src_mask */
199 0x3fffffc, /* dst_mask */
200 false), /* pcrel_offset */
201
202 /* A standard 16 bit relocation. */
203 HOWTO (R_PPC_ADDR16, /* type */
204 0, /* rightshift */
205 1, /* size (0 = byte, 1 = short, 2 = long) */
206 16, /* bitsize */
207 false, /* pc_relative */
208 0, /* bitpos */
209 complain_overflow_bitfield, /* complain_on_overflow */
210 bfd_elf_generic_reloc, /* special_function */
211 "R_PPC_ADDR16", /* name */
212 false, /* partial_inplace */
213 0, /* src_mask */
214 0xffff, /* dst_mask */
215 false), /* pcrel_offset */
216
217 /* A 16 bit relocation without overflow. */
218 HOWTO (R_PPC_ADDR16_LO, /* type */
219 0, /* rightshift */
220 1, /* size (0 = byte, 1 = short, 2 = long) */
221 16, /* bitsize */
222 false, /* pc_relative */
223 0, /* bitpos */
224 complain_overflow_dont,/* complain_on_overflow */
225 bfd_elf_generic_reloc, /* special_function */
226 "R_PPC_ADDR16_LO", /* name */
227 false, /* partial_inplace */
228 0, /* src_mask */
229 0xffff, /* dst_mask */
230 false), /* pcrel_offset */
231
232 /* The high order 16 bits of an address. */
233 HOWTO (R_PPC_ADDR16_HI, /* type */
234 16, /* rightshift */
235 1, /* size (0 = byte, 1 = short, 2 = long) */
236 16, /* bitsize */
237 false, /* pc_relative */
238 0, /* bitpos */
239 complain_overflow_dont, /* complain_on_overflow */
240 bfd_elf_generic_reloc, /* special_function */
241 "R_PPC_ADDR16_HI", /* name */
242 false, /* partial_inplace */
243 0, /* src_mask */
244 0xffff, /* dst_mask */
245 false), /* pcrel_offset */
246
247 /* The high order 16 bits of an address, plus 1 if the contents of
248 the low 16 bits, treated as a signed number, is negative. */
249 HOWTO (R_PPC_ADDR16_HA, /* type */
250 16, /* rightshift */
251 1, /* size (0 = byte, 1 = short, 2 = long) */
252 16, /* bitsize */
253 false, /* pc_relative */
254 0, /* bitpos */
255 complain_overflow_dont, /* complain_on_overflow */
256 ppc_elf_addr16_ha_reloc, /* special_function */
257 "R_PPC_ADDR16_HA", /* name */
258 false, /* partial_inplace */
259 0, /* src_mask */
260 0xffff, /* dst_mask */
261 false), /* pcrel_offset */
262
263 /* An absolute 16 bit branch; the lower two bits must be zero.
264 FIXME: we don't check that, we just clear them. */
265 HOWTO (R_PPC_ADDR14, /* type */
266 0, /* rightshift */
267 2, /* size (0 = byte, 1 = short, 2 = long) */
268 16, /* bitsize */
269 false, /* pc_relative */
270 0, /* bitpos */
271 complain_overflow_bitfield, /* complain_on_overflow */
272 bfd_elf_generic_reloc, /* special_function */
273 "R_PPC_ADDR14", /* name */
274 false, /* partial_inplace */
275 0, /* src_mask */
276 0xfffc, /* dst_mask */
277 false), /* pcrel_offset */
278
279 /* An absolute 16 bit branch, for which bit 10 should be set to
280 indicate that the branch is expected to be taken. The lower two
281 bits must be zero. */
282 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
283 0, /* rightshift */
284 2, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
286 false, /* pc_relative */
287 0, /* bitpos */
288 complain_overflow_bitfield, /* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC_ADDR14_BRTAKEN",/* name */
291 false, /* partial_inplace */
292 0, /* src_mask */
293 0xfffc, /* dst_mask */
294 false), /* pcrel_offset */
295
296 /* An absolute 16 bit branch, for which bit 10 should be set to
297 indicate that the branch is not expected to be taken. The lower
298 two bits must be zero. */
299 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
300 0, /* rightshift */
301 2, /* size (0 = byte, 1 = short, 2 = long) */
302 16, /* bitsize */
303 false, /* pc_relative */
304 0, /* bitpos */
305 complain_overflow_bitfield, /* complain_on_overflow */
306 bfd_elf_generic_reloc, /* special_function */
307 "R_PPC_ADDR14_BRNTAKEN",/* name */
308 false, /* partial_inplace */
309 0, /* src_mask */
310 0xfffc, /* dst_mask */
311 false), /* pcrel_offset */
312
313 /* A relative 26 bit branch; the lower two bits must be zero. */
314 HOWTO (R_PPC_REL24, /* type */
315 0, /* rightshift */
316 2, /* size (0 = byte, 1 = short, 2 = long) */
317 26, /* bitsize */
318 true, /* pc_relative */
319 0, /* bitpos */
320 complain_overflow_signed, /* complain_on_overflow */
321 bfd_elf_generic_reloc, /* special_function */
322 "R_PPC_REL24", /* name */
323 false, /* partial_inplace */
324 0, /* src_mask */
325 0x3fffffc, /* dst_mask */
326 true), /* pcrel_offset */
327
328 /* A relative 16 bit branch; the lower two bits must be zero. */
329 HOWTO (R_PPC_REL14, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 16, /* bitsize */
333 true, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_signed, /* complain_on_overflow */
336 bfd_elf_generic_reloc, /* special_function */
337 "R_PPC_REL14", /* name */
338 false, /* partial_inplace */
339 0, /* src_mask */
340 0xfffc, /* dst_mask */
341 true), /* pcrel_offset */
342
343 /* A relative 16 bit branch. Bit 10 should be set to indicate that
344 the branch is expected to be taken. The lower two bits must be
345 zero. */
346 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
350 true, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_signed, /* complain_on_overflow */
353 bfd_elf_generic_reloc, /* special_function */
354 "R_PPC_REL14_BRTAKEN", /* name */
355 false, /* partial_inplace */
356 0, /* src_mask */
357 0xfffc, /* dst_mask */
358 true), /* pcrel_offset */
359
360 /* A relative 16 bit branch. Bit 10 should be set to indicate that
361 the branch is not expected to be taken. The lower two bits must
362 be zero. */
363 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
367 true, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_signed, /* complain_on_overflow */
370 bfd_elf_generic_reloc, /* special_function */
371 "R_PPC_REL14_BRNTAKEN",/* name */
372 false, /* partial_inplace */
373 0, /* src_mask */
374 0xfffc, /* dst_mask */
375 true), /* pcrel_offset */
376
377 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
378 symbol. */
379 HOWTO (R_PPC_GOT16, /* type */
380 0, /* rightshift */
381 1, /* size (0 = byte, 1 = short, 2 = long) */
382 16, /* bitsize */
383 false, /* pc_relative */
384 0, /* bitpos */
385 complain_overflow_signed, /* complain_on_overflow */
386 bfd_elf_generic_reloc, /* special_function */
387 "R_PPC_GOT16", /* name */
388 false, /* partial_inplace */
389 0, /* src_mask */
390 0xffff, /* dst_mask */
391 false), /* pcrel_offset */
392
393 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
394 the symbol. */
395 HOWTO (R_PPC_GOT16_LO, /* type */
396 0, /* rightshift */
397 1, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
399 false, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_dont, /* complain_on_overflow */
402 bfd_elf_generic_reloc, /* special_function */
403 "R_PPC_GOT16_LO", /* name */
404 false, /* partial_inplace */
405 0, /* src_mask */
406 0xffff, /* dst_mask */
407 false), /* pcrel_offset */
408
409 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
410 the symbol. */
411 HOWTO (R_PPC_GOT16_HI, /* type */
412 16, /* rightshift */
413 1, /* size (0 = byte, 1 = short, 2 = long) */
414 16, /* bitsize */
415 false, /* pc_relative */
416 0, /* bitpos */
417 complain_overflow_bitfield, /* complain_on_overflow */
418 bfd_elf_generic_reloc, /* special_function */
419 "R_PPC_GOT16_HI", /* name */
420 false, /* partial_inplace */
421 0, /* src_mask */
422 0xffff, /* dst_mask */
423 false), /* pcrel_offset */
424
425 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
426 the symbol. */
427 HOWTO (R_PPC_GOT16_HA, /* type */
428 16, /* rightshift */
429 1, /* size (0 = byte, 1 = short, 2 = long) */
430 16, /* bitsize */
431 false, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_bitfield, /* complain_on_overflow */
434 ppc_elf_addr16_ha_reloc, /* special_function */
435 "R_PPC_GOT16_HA", /* name */
436 false, /* partial_inplace */
437 0, /* src_mask */
438 0xffff, /* dst_mask */
439 false), /* pcrel_offset */
440
441 /* Like R_PPC_REL24, but referring to the procedure linkage table
442 entry for the symbol. */
443 HOWTO (R_PPC_PLTREL24, /* type */
444 0, /* rightshift */
445 2, /* size (0 = byte, 1 = short, 2 = long) */
446 26, /* bitsize */
447 true, /* pc_relative */
448 0, /* bitpos */
449 complain_overflow_signed, /* complain_on_overflow */
450 bfd_elf_generic_reloc, /* special_function */
451 "R_PPC_PLTREL24", /* name */
452 false, /* partial_inplace */
453 0, /* src_mask */
454 0x3fffffc, /* dst_mask */
455 true), /* pcrel_offset */
456
457 /* This is used only by the dynamic linker. The symbol should exist
458 both in the object being run and in some shared library. The
459 dynamic linker copies the data addressed by the symbol from the
460 shared library into the object, because the object being
461 run has to have the data at some particular address. */
462 HOWTO (R_PPC_COPY, /* type */
463 0, /* rightshift */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
465 32, /* bitsize */
466 false, /* pc_relative */
467 0, /* bitpos */
468 complain_overflow_bitfield, /* complain_on_overflow */
469 bfd_elf_generic_reloc, /* special_function */
470 "R_PPC_COPY", /* name */
471 false, /* partial_inplace */
472 0, /* src_mask */
473 0, /* dst_mask */
474 false), /* pcrel_offset */
475
476 /* Like R_PPC_ADDR32, but used when setting global offset table
477 entries. */
478 HOWTO (R_PPC_GLOB_DAT, /* type */
479 0, /* rightshift */
480 2, /* size (0 = byte, 1 = short, 2 = long) */
481 32, /* bitsize */
482 false, /* pc_relative */
483 0, /* bitpos */
484 complain_overflow_bitfield, /* complain_on_overflow */
485 bfd_elf_generic_reloc, /* special_function */
486 "R_PPC_GLOB_DAT", /* name */
487 false, /* partial_inplace */
488 0, /* src_mask */
489 0xffffffff, /* dst_mask */
490 false), /* pcrel_offset */
491
492 /* Marks a procedure linkage table entry for a symbol. */
493 HOWTO (R_PPC_JMP_SLOT, /* type */
494 0, /* rightshift */
495 2, /* size (0 = byte, 1 = short, 2 = long) */
496 32, /* bitsize */
497 false, /* pc_relative */
498 0, /* bitpos */
499 complain_overflow_bitfield, /* complain_on_overflow */
500 bfd_elf_generic_reloc, /* special_function */
501 "R_PPC_JMP_SLOT", /* name */
502 false, /* partial_inplace */
503 0, /* src_mask */
504 0, /* dst_mask */
505 false), /* pcrel_offset */
506
507 /* Used only by the dynamic linker. When the object is run, this
508 longword is set to the load address of the object, plus the
509 addend. */
510 HOWTO (R_PPC_RELATIVE, /* type */
511 0, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 32, /* bitsize */
514 false, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_bitfield, /* complain_on_overflow */
517 bfd_elf_generic_reloc, /* special_function */
518 "R_PPC_RELATIVE", /* name */
519 false, /* partial_inplace */
520 0, /* src_mask */
521 0xffffffff, /* dst_mask */
522 false), /* pcrel_offset */
523
524 /* Like R_PPC_REL24, but uses the value of the symbol within the
525 object rather than the final value. Normally used for
526 _GLOBAL_OFFSET_TABLE_. */
527 HOWTO (R_PPC_LOCAL24PC, /* type */
528 0, /* rightshift */
529 2, /* size (0 = byte, 1 = short, 2 = long) */
530 26, /* bitsize */
531 true, /* pc_relative */
532 0, /* bitpos */
533 complain_overflow_signed, /* complain_on_overflow */
534 bfd_elf_generic_reloc, /* special_function */
535 "R_PPC_LOCAL24PC", /* name */
536 false, /* partial_inplace */
537 0, /* src_mask */
538 0x3fffffc, /* dst_mask */
539 true), /* pcrel_offset */
540
541 /* Like R_PPC_ADDR32, but may be unaligned. */
542 HOWTO (R_PPC_UADDR32, /* type */
543 0, /* rightshift */
544 2, /* size (0 = byte, 1 = short, 2 = long) */
545 32, /* bitsize */
546 false, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_bitfield, /* complain_on_overflow */
549 bfd_elf_generic_reloc, /* special_function */
550 "R_PPC_UADDR32", /* name */
551 false, /* partial_inplace */
552 0, /* src_mask */
553 0xffffffff, /* dst_mask */
554 false), /* pcrel_offset */
555
556 /* Like R_PPC_ADDR16, but may be unaligned. */
557 HOWTO (R_PPC_UADDR16, /* type */
558 0, /* rightshift */
559 1, /* size (0 = byte, 1 = short, 2 = long) */
560 16, /* bitsize */
561 false, /* pc_relative */
562 0, /* bitpos */
563 complain_overflow_bitfield, /* complain_on_overflow */
564 bfd_elf_generic_reloc, /* special_function */
565 "R_PPC_UADDR16", /* name */
566 false, /* partial_inplace */
567 0, /* src_mask */
568 0xffff, /* dst_mask */
569 false), /* pcrel_offset */
570
571 /* 32-bit PC relative */
572 HOWTO (R_PPC_REL32, /* type */
573 0, /* rightshift */
574 2, /* size (0 = byte, 1 = short, 2 = long) */
575 32, /* bitsize */
576 true, /* pc_relative */
577 0, /* bitpos */
578 complain_overflow_bitfield, /* complain_on_overflow */
579 bfd_elf_generic_reloc, /* special_function */
580 "R_PPC_REL32", /* name */
581 false, /* partial_inplace */
582 0, /* src_mask */
583 0xffffffff, /* dst_mask */
584 true), /* pcrel_offset */
585
586 /* 32-bit relocation to the symbol's procedure linkage table.
587 FIXME: not supported. */
588 HOWTO (R_PPC_PLT32, /* type */
589 0, /* rightshift */
590 2, /* size (0 = byte, 1 = short, 2 = long) */
591 32, /* bitsize */
592 false, /* pc_relative */
593 0, /* bitpos */
594 complain_overflow_bitfield, /* complain_on_overflow */
595 bfd_elf_generic_reloc, /* special_function */
596 "R_PPC_PLT32", /* name */
597 false, /* partial_inplace */
598 0, /* src_mask */
599 0, /* dst_mask */
600 false), /* pcrel_offset */
601
602 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
603 FIXME: not supported. */
604 HOWTO (R_PPC_PLTREL32, /* type */
605 0, /* rightshift */
606 2, /* size (0 = byte, 1 = short, 2 = long) */
607 32, /* bitsize */
608 true, /* pc_relative */
609 0, /* bitpos */
610 complain_overflow_bitfield, /* complain_on_overflow */
611 bfd_elf_generic_reloc, /* special_function */
612 "R_PPC_PLTREL32", /* name */
613 false, /* partial_inplace */
614 0, /* src_mask */
615 0, /* dst_mask */
616 true), /* pcrel_offset */
617
618 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
619 the symbol. */
620 HOWTO (R_PPC_PLT16_LO, /* type */
621 0, /* rightshift */
622 1, /* size (0 = byte, 1 = short, 2 = long) */
623 16, /* bitsize */
624 false, /* pc_relative */
625 0, /* bitpos */
626 complain_overflow_dont, /* complain_on_overflow */
627 bfd_elf_generic_reloc, /* special_function */
628 "R_PPC_PLT16_LO", /* name */
629 false, /* partial_inplace */
630 0, /* src_mask */
631 0xffff, /* dst_mask */
632 false), /* pcrel_offset */
633
634 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
635 the symbol. */
636 HOWTO (R_PPC_PLT16_HI, /* type */
637 16, /* rightshift */
638 1, /* size (0 = byte, 1 = short, 2 = long) */
639 16, /* bitsize */
640 false, /* pc_relative */
641 0, /* bitpos */
642 complain_overflow_bitfield, /* complain_on_overflow */
643 bfd_elf_generic_reloc, /* special_function */
644 "R_PPC_PLT16_HI", /* name */
645 false, /* partial_inplace */
646 0, /* src_mask */
647 0xffff, /* dst_mask */
648 false), /* pcrel_offset */
649
650 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
651 the symbol. */
652 HOWTO (R_PPC_PLT16_HA, /* type */
653 16, /* rightshift */
654 1, /* size (0 = byte, 1 = short, 2 = long) */
655 16, /* bitsize */
656 false, /* pc_relative */
657 0, /* bitpos */
658 complain_overflow_bitfield, /* complain_on_overflow */
659 ppc_elf_addr16_ha_reloc, /* special_function */
660 "R_PPC_PLT16_HA", /* name */
661 false, /* partial_inplace */
662 0, /* src_mask */
663 0xffff, /* dst_mask */
664 false), /* pcrel_offset */
665
666 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
667 small data items. */
668 HOWTO (R_PPC_SDAREL16, /* type */
669 0, /* rightshift */
670 1, /* size (0 = byte, 1 = short, 2 = long) */
671 16, /* bitsize */
672 false, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_signed, /* complain_on_overflow */
675 bfd_elf_generic_reloc, /* special_function */
676 "R_PPC_SDAREL16", /* name */
677 false, /* partial_inplace */
678 0, /* src_mask */
679 0xffff, /* dst_mask */
680 false), /* pcrel_offset */
681
682 /* 32-bit section relative relocation. */
683 HOWTO (R_PPC_SECTOFF, /* type */
684 0, /* rightshift */
685 2, /* size (0 = byte, 1 = short, 2 = long) */
686 32, /* bitsize */
687 true, /* pc_relative */
688 0, /* bitpos */
689 complain_overflow_bitfield, /* complain_on_overflow */
690 bfd_elf_generic_reloc, /* special_function */
691 "R_PPC_SECTOFF", /* name */
692 false, /* partial_inplace */
693 0, /* src_mask */
694 0, /* dst_mask */
695 true), /* pcrel_offset */
696
697 /* 16-bit lower half section relative relocation. */
698 HOWTO (R_PPC_SECTOFF_LO, /* type */
699 0, /* rightshift */
700 1, /* size (0 = byte, 1 = short, 2 = long) */
701 16, /* bitsize */
702 false, /* pc_relative */
703 0, /* bitpos */
704 complain_overflow_dont, /* complain_on_overflow */
705 bfd_elf_generic_reloc, /* special_function */
706 "R_PPC_SECTOFF_LO", /* name */
707 false, /* partial_inplace */
708 0, /* src_mask */
709 0xffff, /* dst_mask */
710 false), /* pcrel_offset */
711
712 /* 16-bit upper half section relative relocation. */
713 HOWTO (R_PPC_SECTOFF_HI, /* type */
714 16, /* rightshift */
715 1, /* size (0 = byte, 1 = short, 2 = long) */
716 16, /* bitsize */
717 false, /* pc_relative */
718 0, /* bitpos */
719 complain_overflow_bitfield, /* complain_on_overflow */
720 bfd_elf_generic_reloc, /* special_function */
721 "R_PPC_SECTOFF_HI", /* name */
722 false, /* partial_inplace */
723 0, /* src_mask */
724 0xffff, /* dst_mask */
725 false), /* pcrel_offset */
726
727 /* 16-bit upper half adjusted section relative relocation. */
728 HOWTO (R_PPC_SECTOFF_HA, /* type */
729 16, /* rightshift */
730 1, /* size (0 = byte, 1 = short, 2 = long) */
731 16, /* bitsize */
732 false, /* pc_relative */
733 0, /* bitpos */
734 complain_overflow_bitfield, /* complain_on_overflow */
735 ppc_elf_addr16_ha_reloc, /* special_function */
736 "R_PPC_SECTOFF_HA", /* name */
737 false, /* partial_inplace */
738 0, /* src_mask */
739 0xffff, /* dst_mask */
740 false), /* pcrel_offset */
741
742 /* The remaining relocs are from the Embedded ELF ABI, and are not
743 in the SVR4 ELF ABI. */
744
745 /* 32 bit value resulting from the addend minus the symbol */
746 HOWTO (R_PPC_EMB_NADDR32, /* type */
747 0, /* rightshift */
748 2, /* size (0 = byte, 1 = short, 2 = long) */
749 32, /* bitsize */
750 false, /* pc_relative */
751 0, /* bitpos */
752 complain_overflow_bitfield, /* complain_on_overflow */
753 bfd_elf_generic_reloc, /* special_function */
754 "R_PPC_EMB_NADDR32", /* name */
755 false, /* partial_inplace */
756 0, /* src_mask */
757 0xffffffff, /* dst_mask */
758 false), /* pcrel_offset */
759
760 /* 16 bit value resulting from the addend minus the symbol */
761 HOWTO (R_PPC_EMB_NADDR16, /* type */
762 0, /* rightshift */
763 1, /* size (0 = byte, 1 = short, 2 = long) */
764 16, /* bitsize */
765 false, /* pc_relative */
766 0, /* bitpos */
767 complain_overflow_bitfield, /* complain_on_overflow */
768 bfd_elf_generic_reloc, /* special_function */
769 "R_PPC_EMB_NADDR16", /* name */
770 false, /* partial_inplace */
771 0, /* src_mask */
772 0xffff, /* dst_mask */
773 false), /* pcrel_offset */
774
775 /* 16 bit value resulting from the addend minus the symbol */
776 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
777 0, /* rightshift */
778 1, /* size (0 = byte, 1 = short, 2 = long) */
779 16, /* bitsize */
780 false, /* pc_relative */
781 0, /* bitpos */
782 complain_overflow_dont,/* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_PPC_EMB_ADDR16_LO", /* name */
785 false, /* partial_inplace */
786 0, /* src_mask */
787 0xffff, /* dst_mask */
788 false), /* pcrel_offset */
789
790 /* The high order 16 bits of the addend minus the symbol */
791 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
792 16, /* rightshift */
793 1, /* size (0 = byte, 1 = short, 2 = long) */
794 16, /* bitsize */
795 false, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_dont, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_PPC_EMB_NADDR16_HI", /* name */
800 false, /* partial_inplace */
801 0, /* src_mask */
802 0xffff, /* dst_mask */
803 false), /* pcrel_offset */
804
805 /* The high order 16 bits of the result of the addend minus the address,
806 plus 1 if the contents of the low 16 bits, treated as a signed number,
807 is negative. */
808 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
809 16, /* rightshift */
810 1, /* size (0 = byte, 1 = short, 2 = long) */
811 16, /* bitsize */
812 false, /* pc_relative */
813 0, /* bitpos */
814 complain_overflow_dont, /* complain_on_overflow */
815 ppc_elf_addr16_ha_reloc, /* special_function */
816 "R_PPC_EMB_NADDR16_HA", /* name */
817 false, /* partial_inplace */
818 0, /* src_mask */
819 0xffff, /* dst_mask */
820 false), /* pcrel_offset */
821
822 /* 16 bit value resulting from allocating a 4 byte word to hold an
823 address in the .sdata section, and returning the offset from
824 _SDA_BASE_ for that relocation */
825 HOWTO (R_PPC_EMB_SDAI16, /* type */
826 0, /* rightshift */
827 1, /* size (0 = byte, 1 = short, 2 = long) */
828 16, /* bitsize */
829 false, /* pc_relative */
830 0, /* bitpos */
831 complain_overflow_bitfield, /* complain_on_overflow */
832 bfd_elf_generic_reloc, /* special_function */
833 "R_PPC_EMB_SDAI16", /* name */
834 false, /* partial_inplace */
835 0, /* src_mask */
836 0xffff, /* dst_mask */
837 false), /* pcrel_offset */
838
839 /* 16 bit value resulting from allocating a 4 byte word to hold an
840 address in the .sdata2 section, and returning the offset from
841 _SDA2_BASE_ for that relocation */
842 HOWTO (R_PPC_EMB_SDA2I16, /* type */
843 0, /* rightshift */
844 1, /* size (0 = byte, 1 = short, 2 = long) */
845 16, /* bitsize */
846 false, /* pc_relative */
847 0, /* bitpos */
848 complain_overflow_bitfield, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_PPC_EMB_SDA2I16", /* name */
851 false, /* partial_inplace */
852 0, /* src_mask */
853 0xffff, /* dst_mask */
854 false), /* pcrel_offset */
855
856 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
857 small data items. */
858 HOWTO (R_PPC_EMB_SDA2REL, /* 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_signed, /* complain_on_overflow */
865 bfd_elf_generic_reloc, /* special_function */
866 "R_PPC_EMB_SDA2REL", /* name */
867 false, /* partial_inplace */
868 0, /* src_mask */
869 0xffff, /* dst_mask */
870 false), /* pcrel_offset */
871
872 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
873 signed offset from the appropriate base, and filling in the register
874 field with the appropriate register (0, 2, or 13). */
875 HOWTO (R_PPC_EMB_SDA21, /* type */
876 0, /* rightshift */
877 2, /* size (0 = byte, 1 = short, 2 = long) */
878 16, /* bitsize */
879 false, /* pc_relative */
880 0, /* bitpos */
881 complain_overflow_signed, /* complain_on_overflow */
882 bfd_elf_generic_reloc, /* special_function */
883 "R_PPC_EMB_SDA21", /* name */
884 false, /* partial_inplace */
885 0, /* src_mask */
886 0xffff, /* dst_mask */
887 false), /* pcrel_offset */
888
889 /* Relocation not handled: R_PPC_EMB_MRKREF */
890 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
891 /* Relocation not handled: R_PPC_EMB_RELST_LO */
892 /* Relocation not handled: R_PPC_EMB_RELST_HI */
893 /* Relocation not handled: R_PPC_EMB_RELST_HA */
894 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
895
896 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
897 in the 16 bit signed offset from the appropriate base, and filling in the
898 register field with the appropriate register (0, 2, or 13). */
899 HOWTO (R_PPC_EMB_RELSDA, /* type */
900 0, /* rightshift */
901 1, /* size (0 = byte, 1 = short, 2 = long) */
902 16, /* bitsize */
903 true, /* pc_relative */
904 0, /* bitpos */
905 complain_overflow_signed, /* complain_on_overflow */
906 bfd_elf_generic_reloc, /* special_function */
907 "R_PPC_EMB_RELSDA", /* name */
908 false, /* partial_inplace */
909 0, /* src_mask */
910 0xffff, /* dst_mask */
911 false), /* pcrel_offset */
912
913 /* GNU extension to record C++ vtable hierarchy */
914 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
915 0, /* rightshift */
916 0, /* size (0 = byte, 1 = short, 2 = long) */
917 0, /* bitsize */
918 false, /* pc_relative */
919 0, /* bitpos */
920 complain_overflow_dont, /* complain_on_overflow */
921 NULL, /* special_function */
922 "R_PPC_GNU_VTINHERIT", /* name */
923 false, /* partial_inplace */
924 0, /* src_mask */
925 0, /* dst_mask */
926 false), /* pcrel_offset */
927
928 /* GNU extension to record C++ vtable member usage */
929 HOWTO (R_PPC_GNU_VTENTRY, /* type */
930 0, /* rightshift */
931 0, /* size (0 = byte, 1 = short, 2 = long) */
932 0, /* bitsize */
933 false, /* pc_relative */
934 0, /* bitpos */
935 complain_overflow_dont, /* complain_on_overflow */
936 NULL, /* special_function */
937 "R_PPC_GNU_VTENTRY", /* name */
938 false, /* partial_inplace */
939 0, /* src_mask */
940 0, /* dst_mask */
941 false), /* pcrel_offset */
942
943 /* Phony reloc to handle AIX style TOC entries */
944 HOWTO (R_PPC_TOC16, /* type */
945 0, /* rightshift */
946 1, /* size (0 = byte, 1 = short, 2 = long) */
947 16, /* bitsize */
948 false, /* pc_relative */
949 0, /* bitpos */
950 complain_overflow_signed, /* complain_on_overflow */
951 bfd_elf_generic_reloc, /* special_function */
952 "R_PPC_TOC16", /* name */
953 false, /* partial_inplace */
954 0, /* src_mask */
955 0xffff, /* dst_mask */
956 false), /* pcrel_offset */
957 };
958 \f
959 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
960
961 static void
962 ppc_elf_howto_init ()
963 {
964 unsigned int i, type;
965
966 for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
967 {
968 type = ppc_elf_howto_raw[i].type;
969 BFD_ASSERT (type < sizeof (ppc_elf_howto_table) / sizeof (ppc_elf_howto_table[0]));
970 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
971 }
972 }
973 \f
974 /* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
975
976 The MPC860, revision C0 or earlier contains a bug in the die.
977 If all of the following conditions are true, the next instruction
978 to be executed *may* be treated as a no-op.
979 1/ A forward branch is executed.
980 2/ The branch is predicted as not taken.
981 3/ The branch is taken.
982 4/ The branch is located in the last 5 words of a page.
983 (The EOP limit is 5 by default but may be specified as any value from 1-10.)
984
985 Our software solution is to detect these problematic branches in a
986 linker pass and modify them as follows:
987 1/ Unconditional branches - Since these are always predicted taken,
988 there is no problem and no action is required.
989 2/ Conditional backward branches - No problem, no action required.
990 3/ Conditional forward branches - Ensure that the "inverse prediction
991 bit" is set (ensure it is predicted taken).
992 4/ Conditional register branches - Ensure that the "y bit" is set
993 (ensure it is predicted taken).
994 */
995
996 /* Sort sections by address. */
997
998 static int
999 ppc_elf_sort_rela (arg1, arg2)
1000 const PTR arg1;
1001 const PTR arg2;
1002 {
1003 const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
1004 const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
1005
1006 /* Sort by offset. */
1007 return ((*rela1)->r_offset - (*rela2)->r_offset);
1008 }
1009
1010 static boolean
1011 ppc_elf_relax_section (abfd, isec, link_info, again)
1012 bfd *abfd;
1013 asection *isec;
1014 struct bfd_link_info *link_info;
1015 boolean *again;
1016 {
1017 #define PAGESIZE 0x1000
1018
1019 bfd_byte *contents = NULL;
1020 bfd_byte *free_contents = NULL;
1021 Elf_Internal_Rela *internal_relocs = NULL;
1022 Elf_Internal_Rela *free_relocs = NULL;
1023 Elf_Internal_Rela **rela_comb = NULL;
1024 int comb_curr, comb_count;
1025
1026 /* We never have to do this more than once per input section. */
1027 *again = false;
1028
1029 /* If needed, initialize this section's cooked size. */
1030 if (isec->_cooked_size == 0)
1031 isec->_cooked_size = isec->_raw_size;
1032
1033 /* We're only interested in text sections which overlap the
1034 troublesome area at the end of a page. */
1035 if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1036 {
1037 bfd_vma dot, end_page, end_section;
1038 boolean section_modified;
1039
1040 /* Get the section contents. */
1041 /* Get cached copy if it exists. */
1042 if (elf_section_data (isec)->this_hdr.contents != NULL)
1043 contents = elf_section_data (isec)->this_hdr.contents;
1044 else
1045 {
1046 /* Go get them off disk. */
1047 contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
1048 if (contents == NULL)
1049 goto error_return;
1050 free_contents = contents;
1051
1052 if (! bfd_get_section_contents (abfd, isec, contents,
1053 (file_ptr) 0, isec->_raw_size))
1054 goto error_return;
1055 }
1056
1057 comb_curr = 0;
1058 comb_count = 0;
1059 if (isec->reloc_count)
1060 {
1061 unsigned n;
1062
1063 /* Get a copy of the native relocations. */
1064 internal_relocs = _bfd_elf32_link_read_relocs (
1065 abfd, isec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1066 link_info->keep_memory);
1067 if (internal_relocs == NULL)
1068 goto error_return;
1069 if (! link_info->keep_memory)
1070 free_relocs = internal_relocs;
1071
1072 /* Setup a faster access method for the reloc info we need. */
1073 rela_comb = (Elf_Internal_Rela**)
1074 bfd_malloc (isec->reloc_count*sizeof (Elf_Internal_Rela*));
1075 if (rela_comb == NULL)
1076 goto error_return;
1077 for (n = 0; n < isec->reloc_count; ++n)
1078 {
1079 long r_type;
1080
1081 r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
1082 if (r_type < 0 || r_type >= (int) R_PPC_max)
1083 goto error_return;
1084
1085 /* Prologue constants are sometimes present in the ".text"
1086 sections and they can be identified by their associated relocation.
1087 We don't want to process those words and some others which
1088 can also be identified by their relocations. However, not all
1089 conditional branches will have a relocation so we will
1090 only ignore words that 1) have a reloc, and 2) the reloc
1091 is not applicable to a conditional branch.
1092 The array rela_comb is built here for use in the EOP scan loop. */
1093 switch (r_type)
1094 {
1095 case R_PPC_ADDR14_BRNTAKEN: /* absolute, predicted not taken */
1096 case R_PPC_REL14: /* relative cond. br. */
1097 case R_PPC_REL14_BRNTAKEN: /* rel. cond. br., predicted not taken */
1098 /* We should check the instruction. */
1099 break;
1100 default:
1101 /* The word is not a conditional branch - ignore it. */
1102 rela_comb[comb_count++] = &internal_relocs[n];
1103 break;
1104 }
1105 }
1106 if (comb_count > 1)
1107 qsort (rela_comb, (size_t) comb_count, sizeof (int), ppc_elf_sort_rela);
1108 }
1109
1110 /* Enumerate each EOP region that overlaps this section. */
1111 end_section = isec->vma + isec->_cooked_size;
1112 dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1113 dot -= link_info->mpc860c0;
1114 section_modified = false;
1115 if (dot < isec->vma) /* Increment the start position if this section */
1116 dot = isec->vma; /* begins in the middle of its first EOP region. */
1117 for (;
1118 dot < end_section;
1119 dot += PAGESIZE, end_page += PAGESIZE)
1120 {
1121
1122 /* Check each word in this EOP region. */
1123 for (; dot < end_page; dot += 4)
1124 {
1125 bfd_vma isec_offset;
1126 unsigned long insn;
1127 boolean skip, modified;
1128
1129 /* Don't process this word if there is a relocation for it and
1130 the relocation indicates the word is not a conditional branch. */
1131 skip = false;
1132 isec_offset = dot - isec->vma;
1133 for (; comb_curr<comb_count; ++comb_curr)
1134 {
1135 bfd_vma r_offset;
1136
1137 r_offset = rela_comb[comb_curr]->r_offset;
1138 if (r_offset >= isec_offset)
1139 {
1140 if (r_offset == isec_offset) skip = true;
1141 break;
1142 }
1143 }
1144 if (skip) continue;
1145
1146 /* Check the current word for a problematic conditional branch. */
1147 #define BO0(insn) ((insn) & 0x02000000)
1148 #define BO2(insn) ((insn) & 0x00800000)
1149 #define BO4(insn) ((insn) & 0x00200000)
1150 insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1151 modified = false;
1152 if ((insn & 0xFc000000) == 0x40000000)
1153 {
1154 /* Instruction is BCx */
1155 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1156 {
1157 bfd_vma target;
1158 /* This branch is predicted as "normal".
1159 If this is a forward branch, it is problematic. */
1160
1161 target = insn & 0x0000Fffc; /*extract*/
1162 target = (target ^ 0x8000) - 0x8000; /*sign extend*/
1163 if ((insn & 0x00000002) == 0)
1164 target += dot; /*convert to abs*/
1165 if (target > dot)
1166 {
1167 insn |= 0x00200000; /* set the prediction bit */
1168 modified = true;
1169 }
1170 }
1171 }
1172 else if ((insn & 0xFc00Fffe) == 0x4c000420)
1173 {
1174 /* Instruction is BCCTRx */
1175 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1176 {
1177 /* This branch is predicted as not-taken.
1178 If this is a forward branch, it is problematic.
1179 Since we can't tell statically if it will branch forward,
1180 always set the prediction bit. */
1181 insn |= 0x00200000; /* set the prediction bit */
1182 modified = true;
1183 }
1184 }
1185 else if ((insn & 0xFc00Fffe) == 0x4c000020)
1186 {
1187 /* Instruction is BCLRx */
1188 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1189 {
1190 /* This branch is predicted as not-taken.
1191 If this is a forward branch, it is problematic.
1192 Since we can't tell statically if it will branch forward,
1193 always set the prediction bit. */
1194 insn |= 0x00200000; /* set the prediction bit */
1195 modified = true;
1196 }
1197 }
1198 #undef BO0
1199 #undef BO2
1200 #undef BO4
1201 if (modified)
1202 {
1203 bfd_put_32 (abfd, insn, contents + isec_offset);
1204 section_modified = true;
1205 }
1206 }
1207 }
1208 if (section_modified)
1209 {
1210 elf_section_data (isec)->this_hdr.contents = contents;
1211 free_contents = NULL;
1212 }
1213 }
1214
1215 if (rela_comb != NULL)
1216 {
1217 free (rela_comb);
1218 rela_comb = NULL;
1219 }
1220
1221 if (free_relocs != NULL)
1222 {
1223 free (free_relocs);
1224 free_relocs = NULL;
1225 }
1226
1227 if (free_contents != NULL)
1228 {
1229 if (! link_info->keep_memory)
1230 free (free_contents);
1231 else
1232 {
1233 /* Cache the section contents for elf_link_input_bfd. */
1234 elf_section_data (isec)->this_hdr.contents = contents;
1235 }
1236 free_contents = NULL;
1237 }
1238
1239 return true;
1240
1241 error_return:
1242 if (rela_comb != NULL)
1243 free (rela_comb);
1244 if (free_relocs != NULL)
1245 free (free_relocs);
1246 if (free_contents != NULL)
1247 free (free_contents);
1248 return false;
1249 }
1250 \f
1251 static reloc_howto_type *
1252 ppc_elf_reloc_type_lookup (abfd, code)
1253 bfd *abfd ATTRIBUTE_UNUSED;
1254 bfd_reloc_code_real_type code;
1255 {
1256 enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1257
1258 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1259 /* Initialize howto table if needed. */
1260 ppc_elf_howto_init ();
1261
1262 switch ((int) code)
1263 {
1264 default:
1265 return (reloc_howto_type *) NULL;
1266
1267 case BFD_RELOC_NONE: ppc_reloc = R_PPC_NONE; break;
1268 case BFD_RELOC_32: ppc_reloc = R_PPC_ADDR32; break;
1269 case BFD_RELOC_PPC_BA26: ppc_reloc = R_PPC_ADDR24; break;
1270 case BFD_RELOC_16: ppc_reloc = R_PPC_ADDR16; break;
1271 case BFD_RELOC_LO16: ppc_reloc = R_PPC_ADDR16_LO; break;
1272 case BFD_RELOC_HI16: ppc_reloc = R_PPC_ADDR16_HI; break;
1273 case BFD_RELOC_HI16_S: ppc_reloc = R_PPC_ADDR16_HA; break;
1274 case BFD_RELOC_PPC_BA16: ppc_reloc = R_PPC_ADDR14; break;
1275 case BFD_RELOC_PPC_BA16_BRTAKEN: ppc_reloc = R_PPC_ADDR14_BRTAKEN; break;
1276 case BFD_RELOC_PPC_BA16_BRNTAKEN: ppc_reloc = R_PPC_ADDR14_BRNTAKEN; break;
1277 case BFD_RELOC_PPC_B26: ppc_reloc = R_PPC_REL24; break;
1278 case BFD_RELOC_PPC_B16: ppc_reloc = R_PPC_REL14; break;
1279 case BFD_RELOC_PPC_B16_BRTAKEN: ppc_reloc = R_PPC_REL14_BRTAKEN; break;
1280 case BFD_RELOC_PPC_B16_BRNTAKEN: ppc_reloc = R_PPC_REL14_BRNTAKEN; break;
1281 case BFD_RELOC_16_GOTOFF: ppc_reloc = R_PPC_GOT16; break;
1282 case BFD_RELOC_LO16_GOTOFF: ppc_reloc = R_PPC_GOT16_LO; break;
1283 case BFD_RELOC_HI16_GOTOFF: ppc_reloc = R_PPC_GOT16_HI; break;
1284 case BFD_RELOC_HI16_S_GOTOFF: ppc_reloc = R_PPC_GOT16_HA; break;
1285 case BFD_RELOC_24_PLT_PCREL: ppc_reloc = R_PPC_PLTREL24; break;
1286 case BFD_RELOC_PPC_COPY: ppc_reloc = R_PPC_COPY; break;
1287 case BFD_RELOC_PPC_GLOB_DAT: ppc_reloc = R_PPC_GLOB_DAT; break;
1288 case BFD_RELOC_PPC_LOCAL24PC: ppc_reloc = R_PPC_LOCAL24PC; break;
1289 case BFD_RELOC_32_PCREL: ppc_reloc = R_PPC_REL32; break;
1290 case BFD_RELOC_32_PLTOFF: ppc_reloc = R_PPC_PLT32; break;
1291 case BFD_RELOC_32_PLT_PCREL: ppc_reloc = R_PPC_PLTREL32; break;
1292 case BFD_RELOC_LO16_PLTOFF: ppc_reloc = R_PPC_PLT16_LO; break;
1293 case BFD_RELOC_HI16_PLTOFF: ppc_reloc = R_PPC_PLT16_HI; break;
1294 case BFD_RELOC_HI16_S_PLTOFF: ppc_reloc = R_PPC_PLT16_HA; break;
1295 case BFD_RELOC_GPREL16: ppc_reloc = R_PPC_SDAREL16; break;
1296 case BFD_RELOC_32_BASEREL: ppc_reloc = R_PPC_SECTOFF; break;
1297 case BFD_RELOC_LO16_BASEREL: ppc_reloc = R_PPC_SECTOFF_LO; break;
1298 case BFD_RELOC_HI16_BASEREL: ppc_reloc = R_PPC_SECTOFF_HI; break;
1299 case BFD_RELOC_HI16_S_BASEREL: ppc_reloc = R_PPC_SECTOFF_HA; break;
1300 case BFD_RELOC_CTOR: ppc_reloc = R_PPC_ADDR32; break;
1301 case BFD_RELOC_PPC_TOC16: ppc_reloc = R_PPC_TOC16; break;
1302 case BFD_RELOC_PPC_EMB_NADDR32: ppc_reloc = R_PPC_EMB_NADDR32; break;
1303 case BFD_RELOC_PPC_EMB_NADDR16: ppc_reloc = R_PPC_EMB_NADDR16; break;
1304 case BFD_RELOC_PPC_EMB_NADDR16_LO: ppc_reloc = R_PPC_EMB_NADDR16_LO; break;
1305 case BFD_RELOC_PPC_EMB_NADDR16_HI: ppc_reloc = R_PPC_EMB_NADDR16_HI; break;
1306 case BFD_RELOC_PPC_EMB_NADDR16_HA: ppc_reloc = R_PPC_EMB_NADDR16_HA; break;
1307 case BFD_RELOC_PPC_EMB_SDAI16: ppc_reloc = R_PPC_EMB_SDAI16; break;
1308 case BFD_RELOC_PPC_EMB_SDA2I16: ppc_reloc = R_PPC_EMB_SDA2I16; break;
1309 case BFD_RELOC_PPC_EMB_SDA2REL: ppc_reloc = R_PPC_EMB_SDA2REL; break;
1310 case BFD_RELOC_PPC_EMB_SDA21: ppc_reloc = R_PPC_EMB_SDA21; break;
1311 case BFD_RELOC_PPC_EMB_MRKREF: ppc_reloc = R_PPC_EMB_MRKREF; break;
1312 case BFD_RELOC_PPC_EMB_RELSEC16: ppc_reloc = R_PPC_EMB_RELSEC16; break;
1313 case BFD_RELOC_PPC_EMB_RELST_LO: ppc_reloc = R_PPC_EMB_RELST_LO; break;
1314 case BFD_RELOC_PPC_EMB_RELST_HI: ppc_reloc = R_PPC_EMB_RELST_HI; break;
1315 case BFD_RELOC_PPC_EMB_RELST_HA: ppc_reloc = R_PPC_EMB_RELST_HA; break;
1316 case BFD_RELOC_PPC_EMB_BIT_FLD: ppc_reloc = R_PPC_EMB_BIT_FLD; break;
1317 case BFD_RELOC_PPC_EMB_RELSDA: ppc_reloc = R_PPC_EMB_RELSDA; break;
1318 case BFD_RELOC_VTABLE_INHERIT: ppc_reloc = R_PPC_GNU_VTINHERIT; break;
1319 case BFD_RELOC_VTABLE_ENTRY: ppc_reloc = R_PPC_GNU_VTENTRY; break;
1320 }
1321
1322 return ppc_elf_howto_table[(int) ppc_reloc];
1323 };
1324
1325 /* Set the howto pointer for a PowerPC ELF reloc. */
1326
1327 static void
1328 ppc_elf_info_to_howto (abfd, cache_ptr, dst)
1329 bfd *abfd ATTRIBUTE_UNUSED;
1330 arelent *cache_ptr;
1331 Elf32_Internal_Rela *dst;
1332 {
1333 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1334 /* Initialize howto table if needed. */
1335 ppc_elf_howto_init ();
1336
1337 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1338 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1339 }
1340
1341 /* Handle the R_PPC_ADDR16_HA reloc. */
1342
1343 static bfd_reloc_status_type
1344 ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
1345 output_bfd, error_message)
1346 bfd *abfd ATTRIBUTE_UNUSED;
1347 arelent *reloc_entry;
1348 asymbol *symbol;
1349 PTR data ATTRIBUTE_UNUSED;
1350 asection *input_section;
1351 bfd *output_bfd;
1352 char **error_message ATTRIBUTE_UNUSED;
1353 {
1354 bfd_vma relocation;
1355
1356 if (output_bfd != NULL)
1357 {
1358 reloc_entry->address += input_section->output_offset;
1359 return bfd_reloc_ok;
1360 }
1361
1362 if (reloc_entry->address > input_section->_cooked_size)
1363 return bfd_reloc_outofrange;
1364
1365 if (bfd_is_com_section (symbol->section))
1366 relocation = 0;
1367 else
1368 relocation = symbol->value;
1369
1370 relocation += symbol->section->output_section->vma;
1371 relocation += symbol->section->output_offset;
1372 relocation += reloc_entry->addend;
1373
1374 reloc_entry->addend += (relocation & 0x8000) << 1;
1375
1376 return bfd_reloc_continue;
1377 }
1378
1379 /* Function to set whether a module needs the -mrelocatable bit set. */
1380
1381 static boolean
1382 ppc_elf_set_private_flags (abfd, flags)
1383 bfd *abfd;
1384 flagword flags;
1385 {
1386 BFD_ASSERT (!elf_flags_init (abfd)
1387 || elf_elfheader (abfd)->e_flags == flags);
1388
1389 elf_elfheader (abfd)->e_flags = flags;
1390 elf_flags_init (abfd) = true;
1391 return true;
1392 }
1393
1394 /* Copy backend specific data from one object module to another */
1395 static boolean
1396 ppc_elf_copy_private_bfd_data (ibfd, obfd)
1397 bfd *ibfd;
1398 bfd *obfd;
1399 {
1400 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1401 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1402 return true;
1403
1404 BFD_ASSERT (!elf_flags_init (obfd)
1405 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
1406
1407 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1408 elf_flags_init (obfd) = true;
1409 return true;
1410 }
1411
1412 /* Merge backend specific data from an object file to the output
1413 object file when linking */
1414 static boolean
1415 ppc_elf_merge_private_bfd_data (ibfd, obfd)
1416 bfd *ibfd;
1417 bfd *obfd;
1418 {
1419 flagword old_flags;
1420 flagword new_flags;
1421 boolean error;
1422
1423 /* Check if we have the same endianess */
1424 if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
1425 return false;
1426
1427 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1428 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1429 return true;
1430
1431 new_flags = elf_elfheader (ibfd)->e_flags;
1432 old_flags = elf_elfheader (obfd)->e_flags;
1433 if (!elf_flags_init (obfd)) /* First call, no flags set */
1434 {
1435 elf_flags_init (obfd) = true;
1436 elf_elfheader (obfd)->e_flags = new_flags;
1437 }
1438
1439 else if (new_flags == old_flags) /* Compatible flags are ok */
1440 ;
1441
1442 else /* Incompatible flags */
1443 {
1444 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib to be linked
1445 with either. */
1446 error = false;
1447 if ((new_flags & EF_PPC_RELOCATABLE) != 0
1448 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1449 {
1450 error = true;
1451 (*_bfd_error_handler)
1452 (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
1453 bfd_get_filename (ibfd));
1454 }
1455 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1456 && (old_flags & EF_PPC_RELOCATABLE) != 0)
1457 {
1458 error = true;
1459 (*_bfd_error_handler)
1460 (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
1461 bfd_get_filename (ibfd));
1462 }
1463
1464 /* The output is -mrelocatable-lib iff both the input files are. */
1465 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1466 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1467
1468 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1469 but each input file is either -mrelocatable or -mrelocatable-lib. */
1470 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1471 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1472 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1473 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1474
1475 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it */
1476 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1477
1478 new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1479 old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1480
1481 /* Warn about any other mismatches */
1482 if (new_flags != old_flags)
1483 {
1484 error = true;
1485 (*_bfd_error_handler)
1486 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1487 bfd_get_filename (ibfd), (long) new_flags, (long) old_flags);
1488 }
1489
1490 if (error)
1491 {
1492 bfd_set_error (bfd_error_bad_value);
1493 return false;
1494 }
1495 }
1496
1497 return true;
1498 }
1499 \f
1500 /* Handle a PowerPC specific section when reading an object file. This
1501 is called when elfcode.h finds a section with an unknown type. */
1502
1503 static boolean
1504 ppc_elf_section_from_shdr (abfd, hdr, name)
1505 bfd *abfd;
1506 Elf32_Internal_Shdr *hdr;
1507 char *name;
1508 {
1509 asection *newsect;
1510 flagword flags;
1511
1512 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1513 return false;
1514
1515 newsect = hdr->bfd_section;
1516 flags = bfd_get_section_flags (abfd, newsect);
1517 if (hdr->sh_flags & SHF_EXCLUDE)
1518 flags |= SEC_EXCLUDE;
1519
1520 if (hdr->sh_type == SHT_ORDERED)
1521 flags |= SEC_SORT_ENTRIES;
1522
1523 bfd_set_section_flags (abfd, newsect, flags);
1524 return true;
1525 }
1526 \f
1527 /* Set up any other section flags and such that may be necessary. */
1528
1529 static boolean
1530 ppc_elf_fake_sections (abfd, shdr, asect)
1531 bfd *abfd ATTRIBUTE_UNUSED;
1532 Elf32_Internal_Shdr *shdr;
1533 asection *asect;
1534 {
1535 if ((asect->flags & SEC_EXCLUDE) != 0)
1536 shdr->sh_flags |= SHF_EXCLUDE;
1537
1538 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1539 shdr->sh_type = SHT_ORDERED;
1540
1541 return true;
1542 }
1543 \f
1544 /* Create a special linker section */
1545 static elf_linker_section_t *
1546 ppc_elf_create_linker_section (abfd, info, which)
1547 bfd *abfd;
1548 struct bfd_link_info *info;
1549 enum elf_linker_section_enum which;
1550 {
1551 bfd *dynobj = elf_hash_table (info)->dynobj;
1552 elf_linker_section_t *lsect;
1553
1554 /* Record the first bfd section that needs the special section */
1555 if (!dynobj)
1556 dynobj = elf_hash_table (info)->dynobj = abfd;
1557
1558 /* If this is the first time, create the section */
1559 lsect = elf_linker_section (dynobj, which);
1560 if (!lsect)
1561 {
1562 elf_linker_section_t defaults;
1563 static elf_linker_section_t zero_section;
1564
1565 defaults = zero_section;
1566 defaults.which = which;
1567 defaults.hole_written_p = false;
1568 defaults.alignment = 2;
1569
1570 /* Both of these sections are (technically) created by the user
1571 putting data in them, so they shouldn't be marked
1572 SEC_LINKER_CREATED.
1573
1574 The linker creates them so it has somewhere to attach their
1575 respective symbols. In fact, if they were empty it would
1576 be OK to leave the symbol set to 0 (or any random number), because
1577 the appropriate register should never be used. */
1578 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1579 | SEC_IN_MEMORY);
1580
1581 switch (which)
1582 {
1583 default:
1584 (*_bfd_error_handler) (_("%s: Unknown special linker type %d"),
1585 bfd_get_filename (abfd),
1586 (int) which);
1587
1588 bfd_set_error (bfd_error_bad_value);
1589 return (elf_linker_section_t *) 0;
1590
1591 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
1592 defaults.name = ".sdata";
1593 defaults.rel_name = ".rela.sdata";
1594 defaults.bss_name = ".sbss";
1595 defaults.sym_name = "_SDA_BASE_";
1596 defaults.sym_offset = 32768;
1597 break;
1598
1599 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
1600 defaults.name = ".sdata2";
1601 defaults.rel_name = ".rela.sdata2";
1602 defaults.bss_name = ".sbss2";
1603 defaults.sym_name = "_SDA2_BASE_";
1604 defaults.sym_offset = 32768;
1605 defaults.flags |= SEC_READONLY;
1606 break;
1607 }
1608
1609 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
1610 }
1611
1612 return lsect;
1613 }
1614 \f
1615 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
1616 need to bump up the number of section headers. */
1617
1618 static int
1619 ppc_elf_additional_program_headers (abfd)
1620 bfd *abfd;
1621 {
1622 asection *s;
1623 int ret;
1624
1625 ret = 0;
1626
1627 s = bfd_get_section_by_name (abfd, ".interp");
1628 if (s != NULL)
1629 ++ret;
1630
1631 s = bfd_get_section_by_name (abfd, ".sbss2");
1632 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1633 ++ret;
1634
1635 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1636 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1637 ++ret;
1638
1639 return ret;
1640 }
1641
1642 /* Modify the segment map if needed. */
1643
1644 static boolean
1645 ppc_elf_modify_segment_map (abfd)
1646 bfd *abfd ATTRIBUTE_UNUSED;
1647 {
1648 return true;
1649 }
1650 \f
1651 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
1652 to output sections (just like _bfd_elf_create_dynamic_sections has
1653 to create .dynbss and .rela.bss). */
1654
1655 static boolean
1656 ppc_elf_create_dynamic_sections (abfd, info)
1657 bfd *abfd;
1658 struct bfd_link_info *info;
1659 {
1660 register asection *s;
1661 flagword flags;
1662
1663 if (!_bfd_elf_create_dynamic_sections (abfd, info))
1664 return false;
1665
1666 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1667 | SEC_LINKER_CREATED);
1668
1669 s = bfd_make_section (abfd, ".dynsbss");
1670 if (s == NULL
1671 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1672 return false;
1673
1674 if (! info->shared)
1675 {
1676 s = bfd_make_section (abfd, ".rela.sbss");
1677 if (s == NULL
1678 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1679 || ! bfd_set_section_alignment (abfd, s, 2))
1680 return false;
1681 }
1682 return true;
1683 }
1684
1685 /* Adjust a symbol defined by a dynamic object and referenced by a
1686 regular object. The current definition is in some section of the
1687 dynamic object, but we're not including those sections. We have to
1688 change the definition to something the rest of the link can
1689 understand. */
1690
1691 static boolean
1692 ppc_elf_adjust_dynamic_symbol (info, h)
1693 struct bfd_link_info *info;
1694 struct elf_link_hash_entry *h;
1695 {
1696 bfd *dynobj = elf_hash_table (info)->dynobj;
1697 asection *s;
1698 unsigned int power_of_two;
1699 bfd_vma plt_offset;
1700
1701 #ifdef DEBUG
1702 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n", h->root.root.string);
1703 #endif
1704
1705 /* Make sure we know what is going on here. */
1706 BFD_ASSERT (dynobj != NULL
1707 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1708 || h->weakdef != NULL
1709 || ((h->elf_link_hash_flags
1710 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1711 && (h->elf_link_hash_flags
1712 & ELF_LINK_HASH_REF_REGULAR) != 0
1713 && (h->elf_link_hash_flags
1714 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1715
1716 /* If this is a function, put it in the procedure linkage table. We
1717 will fill in the contents of the procedure linkage table later,
1718 when we know the address of the .got section. */
1719 if (h->type == STT_FUNC
1720 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1721 {
1722 if (! elf_hash_table (info)->dynamic_sections_created
1723 || SYMBOL_CALLS_LOCAL (info, h)
1724 || (info->shared && h->plt.refcount <= 0))
1725 {
1726 /* A PLT entry is not required/allowed when:
1727
1728 1. We are not using ld.so; because then the PLT entry
1729 can't be set up, so we can't use one.
1730
1731 2. We know for certain that a call to this symbol
1732 will go to this object.
1733
1734 3. GC has rendered the entry unused.
1735 Note, however, that in an executable all references to the
1736 symbol go to the PLT, so we can't turn it off in that case.
1737 ??? The correct thing to do here is to reference count
1738 all uses of the symbol, not just those to the GOT or PLT. */
1739 h->plt.offset = (bfd_vma) -1;
1740 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1741 return true;
1742 }
1743
1744 /* Make sure this symbol is output as a dynamic symbol. */
1745 if (h->dynindx == -1)
1746 {
1747 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1748 return false;
1749 }
1750 BFD_ASSERT (h->dynindx != -1);
1751
1752 s = bfd_get_section_by_name (dynobj, ".plt");
1753 BFD_ASSERT (s != NULL);
1754
1755 /* If this is the first .plt entry, make room for the special
1756 first entry. */
1757 if (s->_raw_size == 0)
1758 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
1759
1760 /* The PowerPC PLT is actually composed of two parts, the first part
1761 is 2 words (for a load and a jump), and then there is a remaining
1762 word available at the end. */
1763 plt_offset = (PLT_INITIAL_ENTRY_SIZE
1764 + (PLT_SLOT_SIZE
1765 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
1766 / PLT_ENTRY_SIZE)));
1767
1768 /* If this symbol is not defined in a regular file, and we are
1769 not generating a shared library, then set the symbol to this
1770 location in the .plt. This is required to make function
1771 pointers compare as equal between the normal executable and
1772 the shared library. */
1773 if (! info->shared
1774 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1775 {
1776 h->root.u.def.section = s;
1777 h->root.u.def.value = plt_offset;
1778 }
1779
1780 h->plt.offset = plt_offset;
1781
1782 /* Make room for this entry. After the 8192nd entry, room
1783 for two entries is allocated. */
1784 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
1785 >= PLT_NUM_SINGLE_ENTRIES)
1786 s->_raw_size += 2 * PLT_ENTRY_SIZE;
1787 else
1788 s->_raw_size += PLT_ENTRY_SIZE;
1789
1790 /* We also need to make an entry in the .rela.plt section. */
1791 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1792 BFD_ASSERT (s != NULL);
1793 s->_raw_size += sizeof (Elf32_External_Rela);
1794
1795 return true;
1796 }
1797
1798 /* If this is a weak symbol, and there is a real definition, the
1799 processor independent code will have arranged for us to see the
1800 real definition first, and we can just use the same value. */
1801 if (h->weakdef != NULL)
1802 {
1803 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1804 || h->weakdef->root.type == bfd_link_hash_defweak);
1805 h->root.u.def.section = h->weakdef->root.u.def.section;
1806 h->root.u.def.value = h->weakdef->root.u.def.value;
1807 return true;
1808 }
1809
1810 /* This is a reference to a symbol defined by a dynamic object which
1811 is not a function. */
1812
1813 /* If we are creating a shared library, we must presume that the
1814 only references to the symbol are via the global offset table.
1815 For such cases we need not do anything here; the relocations will
1816 be handled correctly by relocate_section. */
1817 if (info->shared)
1818 return true;
1819
1820 /* We must allocate the symbol in our .dynbss section, which will
1821 become part of the .bss section of the executable. There will be
1822 an entry for this symbol in the .dynsym section. The dynamic
1823 object will contain position independent code, so all references
1824 from the dynamic object to this symbol will go through the global
1825 offset table. The dynamic linker will use the .dynsym entry to
1826 determine the address it must put in the global offset table, so
1827 both the dynamic object and the regular object will refer to the
1828 same memory location for the variable.
1829
1830 Of course, if the symbol is sufficiently small, we must instead
1831 allocate it in .sbss. FIXME: It would be better to do this if and
1832 only if there were actually SDAREL relocs for that symbol. */
1833
1834 if (h->size <= elf_gp_size (dynobj))
1835 s = bfd_get_section_by_name (dynobj, ".dynsbss");
1836 else
1837 s = bfd_get_section_by_name (dynobj, ".dynbss");
1838 BFD_ASSERT (s != NULL);
1839
1840 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
1841 copy the initial value out of the dynamic object and into the
1842 runtime process image. We need to remember the offset into the
1843 .rela.bss section we are going to use. */
1844 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1845 {
1846 asection *srel;
1847
1848 if (h->size <= elf_gp_size (dynobj))
1849 srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
1850 else
1851 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1852 BFD_ASSERT (srel != NULL);
1853 srel->_raw_size += sizeof (Elf32_External_Rela);
1854 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1855 }
1856
1857 /* We need to figure out the alignment required for this symbol. I
1858 have no idea how ELF linkers handle this. */
1859 power_of_two = bfd_log2 (h->size);
1860 if (power_of_two > 4)
1861 power_of_two = 4;
1862
1863 /* Apply the required alignment. */
1864 s->_raw_size = BFD_ALIGN (s->_raw_size,
1865 (bfd_size_type) (1 << power_of_two));
1866 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1867 {
1868 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1869 return false;
1870 }
1871
1872 /* Define the symbol as being at this point in the section. */
1873 h->root.u.def.section = s;
1874 h->root.u.def.value = s->_raw_size;
1875
1876 /* Increment the section size to make room for the symbol. */
1877 s->_raw_size += h->size;
1878
1879 return true;
1880 }
1881 \f
1882 /* Set the sizes of the dynamic sections. */
1883
1884 static boolean
1885 ppc_elf_size_dynamic_sections (output_bfd, info)
1886 bfd *output_bfd ATTRIBUTE_UNUSED;
1887 struct bfd_link_info *info;
1888 {
1889 bfd *dynobj;
1890 asection *s;
1891 boolean plt;
1892 boolean relocs;
1893
1894 #ifdef DEBUG
1895 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
1896 #endif
1897
1898 dynobj = elf_hash_table (info)->dynobj;
1899 BFD_ASSERT (dynobj != NULL);
1900
1901 if (elf_hash_table (info)->dynamic_sections_created)
1902 {
1903 /* Set the contents of the .interp section to the interpreter. */
1904 if (! info->shared)
1905 {
1906 s = bfd_get_section_by_name (dynobj, ".interp");
1907 BFD_ASSERT (s != NULL);
1908 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1909 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1910 }
1911 }
1912 else
1913 {
1914 /* We may have created entries in the .rela.got, .rela.sdata, and
1915 .rela.sdata2 sections. However, if we are not creating the
1916 dynamic sections, we will not actually use these entries. Reset
1917 the size of .rela.got, et al, which will cause it to get
1918 stripped from the output file below. */
1919 static char *rela_sections[] = { ".rela.got", ".rela.sdata",
1920 ".rela.sdata2", ".rela.sbss",
1921 (char *) 0 };
1922 char **p;
1923
1924 for (p = rela_sections; *p != (char *) 0; p++)
1925 {
1926 s = bfd_get_section_by_name (dynobj, *p);
1927 if (s != NULL)
1928 s->_raw_size = 0;
1929 }
1930 }
1931
1932 /* The check_relocs and adjust_dynamic_symbol entry points have
1933 determined the sizes of the various dynamic sections. Allocate
1934 memory for them. */
1935 plt = false;
1936 relocs = false;
1937 for (s = dynobj->sections; s != NULL; s = s->next)
1938 {
1939 const char *name;
1940 boolean strip;
1941
1942 if ((s->flags & SEC_LINKER_CREATED) == 0)
1943 continue;
1944
1945 /* It's OK to base decisions on the section name, because none
1946 of the dynobj section names depend upon the input files. */
1947 name = bfd_get_section_name (dynobj, s);
1948
1949 strip = false;
1950
1951 if (strcmp (name, ".plt") == 0)
1952 {
1953 if (s->_raw_size == 0)
1954 {
1955 /* Strip this section if we don't need it; see the
1956 comment below. */
1957 strip = true;
1958 }
1959 else
1960 {
1961 /* Remember whether there is a PLT. */
1962 plt = true;
1963 }
1964 }
1965 else if (strncmp (name, ".rela", 5) == 0)
1966 {
1967 if (s->_raw_size == 0)
1968 {
1969 /* If we don't need this section, strip it from the
1970 output file. This is mostly to handle .rela.bss and
1971 .rela.plt. We must create both sections in
1972 create_dynamic_sections, because they must be created
1973 before the linker maps input sections to output
1974 sections. The linker does that before
1975 adjust_dynamic_symbol is called, and it is that
1976 function which decides whether anything needs to go
1977 into these sections. */
1978 strip = true;
1979 }
1980 else
1981 {
1982 /* Remember whether there are any relocation sections. */
1983 relocs = true;
1984
1985 /* We use the reloc_count field as a counter if we need
1986 to copy relocs into the output file. */
1987 s->reloc_count = 0;
1988 }
1989 }
1990 else if (strcmp (name, ".got") != 0
1991 && strcmp (name, ".sdata") != 0
1992 && strcmp (name, ".sdata2") != 0)
1993 {
1994 /* It's not one of our sections, so don't allocate space. */
1995 continue;
1996 }
1997
1998 if (strip)
1999 {
2000 _bfd_strip_section_from_output (info, s);
2001 continue;
2002 }
2003
2004 /* Allocate memory for the section contents. */
2005 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2006 if (s->contents == NULL && s->_raw_size != 0)
2007 return false;
2008 }
2009
2010 if (elf_hash_table (info)->dynamic_sections_created)
2011 {
2012 /* Add some entries to the .dynamic section. We fill in the
2013 values later, in ppc_elf_finish_dynamic_sections, but we
2014 must add the entries now so that we get the correct size for
2015 the .dynamic section. The DT_DEBUG entry is filled in by the
2016 dynamic linker and used by the debugger. */
2017 if (! info->shared)
2018 {
2019 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2020 return false;
2021 }
2022
2023 if (plt)
2024 {
2025 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
2026 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2027 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2028 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2029 return false;
2030 }
2031
2032 if (relocs)
2033 {
2034 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2035 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2036 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2037 sizeof (Elf32_External_Rela)))
2038 return false;
2039 }
2040
2041 if ((info->flags & DF_TEXTREL) != 0)
2042 {
2043 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2044 return false;
2045 info->flags |= DF_TEXTREL;
2046 }
2047 }
2048
2049 return true;
2050 }
2051 \f
2052 /* Look through the relocs for a section during the first phase, and
2053 allocate space in the global offset table or procedure linkage
2054 table. */
2055
2056 static boolean
2057 ppc_elf_check_relocs (abfd, info, sec, relocs)
2058 bfd *abfd;
2059 struct bfd_link_info *info;
2060 asection *sec;
2061 const Elf_Internal_Rela *relocs;
2062 {
2063 bfd *dynobj;
2064 Elf_Internal_Shdr *symtab_hdr;
2065 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2066 const Elf_Internal_Rela *rel;
2067 const Elf_Internal_Rela *rel_end;
2068 bfd_signed_vma *local_got_refcounts;
2069 elf_linker_section_t *sdata;
2070 elf_linker_section_t *sdata2;
2071 asection *sreloc;
2072 asection *sgot = NULL;
2073 asection *srelgot = NULL;
2074
2075 if (info->relocateable)
2076 return true;
2077
2078 #ifdef DEBUG
2079 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
2080 bfd_get_section_name (abfd, sec),
2081 bfd_get_filename (abfd));
2082 #endif
2083
2084 /* Create the linker generated sections all the time so that the
2085 special symbols are created. */
2086
2087 if ((sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA)) == NULL)
2088 {
2089 sdata = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2090 if (!sdata)
2091 return false;
2092 }
2093
2094 if ((sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2)) == NULL)
2095 {
2096 sdata2 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA2);
2097 if (!sdata2)
2098 return false;
2099 }
2100
2101 dynobj = elf_hash_table (info)->dynobj;
2102 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2103 local_got_refcounts = elf_local_got_refcounts (abfd);
2104
2105 sym_hashes = elf_sym_hashes (abfd);
2106 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2107 if (!elf_bad_symtab (abfd))
2108 sym_hashes_end -= symtab_hdr->sh_info;
2109
2110 sreloc = NULL;
2111
2112 rel_end = relocs + sec->reloc_count;
2113 for (rel = relocs; rel < rel_end; rel++)
2114 {
2115 unsigned long r_symndx;
2116 struct elf_link_hash_entry *h;
2117
2118 r_symndx = ELF32_R_SYM (rel->r_info);
2119 if (r_symndx < symtab_hdr->sh_info)
2120 h = NULL;
2121 else
2122 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2123
2124 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2125 This shows up in particular in an R_PPC_ADDR32 in the eabi
2126 startup code. */
2127 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2128 {
2129 if (sgot == NULL)
2130 {
2131 if (dynobj == NULL)
2132 elf_hash_table (info)->dynobj = dynobj = abfd;
2133 if (! _bfd_elf_create_got_section (dynobj, info))
2134 return false;
2135 sgot = bfd_get_section_by_name (dynobj, ".got");
2136 BFD_ASSERT (sgot != NULL);
2137 }
2138 }
2139
2140 switch (ELF32_R_TYPE (rel->r_info))
2141 {
2142 /* GOT16 relocations */
2143 case R_PPC_GOT16:
2144 case R_PPC_GOT16_LO:
2145 case R_PPC_GOT16_HI:
2146 case R_PPC_GOT16_HA:
2147 /* This symbol requires a global offset table entry. */
2148
2149 if (sgot == NULL)
2150 {
2151 if (dynobj == NULL)
2152 elf_hash_table (info)->dynobj = dynobj = abfd;
2153 if (! _bfd_elf_create_got_section (dynobj, info))
2154 return false;
2155 sgot = bfd_get_section_by_name (dynobj, ".got");
2156 BFD_ASSERT (sgot != NULL);
2157 }
2158
2159 if (srelgot == NULL
2160 && (h != NULL || info->shared))
2161 {
2162 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2163 if (srelgot == NULL)
2164 {
2165 srelgot = bfd_make_section (dynobj, ".rela.got");
2166 if (srelgot == NULL
2167 || ! bfd_set_section_flags (dynobj, srelgot,
2168 (SEC_ALLOC
2169 | SEC_LOAD
2170 | SEC_HAS_CONTENTS
2171 | SEC_IN_MEMORY
2172 | SEC_LINKER_CREATED
2173 | SEC_READONLY))
2174 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2175 return false;
2176 }
2177 }
2178
2179 if (h != NULL)
2180 {
2181 if (h->got.refcount == -1)
2182 {
2183 /* Make sure this symbol is output as a dynamic symbol. */
2184 if (h->dynindx == -1)
2185 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2186 return false;
2187
2188 /* Allocate space in the .got. */
2189 sgot->_raw_size += 4;
2190 /* Allocate relocation space. */
2191 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2192
2193 h->got.refcount = 1;
2194 }
2195 else
2196 h->got.refcount++;
2197 }
2198 else
2199 {
2200 /* This is a global offset table entry for a local symbol. */
2201 if (local_got_refcounts == NULL)
2202 {
2203 size_t size;
2204
2205 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
2206 local_got_refcounts = (bfd_signed_vma *)
2207 bfd_alloc (abfd, size);
2208 if (local_got_refcounts == NULL)
2209 return false;
2210 elf_local_got_refcounts (abfd) = local_got_refcounts;
2211 memset (local_got_refcounts, -1, size);
2212 }
2213 if (local_got_refcounts[r_symndx] == -1)
2214 {
2215 sgot->_raw_size += 4;
2216
2217 /* If we are generating a shared object, we need to
2218 output a R_PPC_RELATIVE reloc so that the
2219 dynamic linker can adjust this GOT entry. */
2220 if (info->shared)
2221 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2222
2223 local_got_refcounts[r_symndx] = 1;
2224 }
2225 else
2226 local_got_refcounts[r_symndx]++;
2227 }
2228 break;
2229
2230 /* Indirect .sdata relocation */
2231 case R_PPC_EMB_SDAI16:
2232 if (info->shared)
2233 {
2234 ((*_bfd_error_handler)
2235 (_("%s: relocation %s cannot be used when making a shared object"),
2236 bfd_get_filename (abfd), "R_PPC_EMB_SDAI16"));
2237 return false;
2238 }
2239
2240 if (srelgot == NULL && (h != NULL || info->shared))
2241 {
2242 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2243 if (srelgot == NULL)
2244 {
2245 srelgot = bfd_make_section (dynobj, ".rela.got");
2246 if (srelgot == NULL
2247 || ! bfd_set_section_flags (dynobj, srelgot,
2248 (SEC_ALLOC
2249 | SEC_LOAD
2250 | SEC_HAS_CONTENTS
2251 | SEC_IN_MEMORY
2252 | SEC_LINKER_CREATED
2253 | SEC_READONLY))
2254 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2255 return false;
2256 }
2257 }
2258
2259 if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata, h, rel))
2260 return false;
2261
2262 break;
2263
2264 /* Indirect .sdata2 relocation */
2265 case R_PPC_EMB_SDA2I16:
2266 if (info->shared)
2267 {
2268 ((*_bfd_error_handler)
2269 (_("%s: relocation %s cannot be used when making a shared object"),
2270 bfd_get_filename (abfd), "R_PPC_EMB_SDA2I16"));
2271 return false;
2272 }
2273
2274 if (srelgot == NULL && (h != NULL || info->shared))
2275 {
2276 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2277 if (srelgot == NULL)
2278 {
2279 srelgot = bfd_make_section (dynobj, ".rela.got");
2280 if (srelgot == NULL
2281 || ! bfd_set_section_flags (dynobj, srelgot,
2282 (SEC_ALLOC
2283 | SEC_LOAD
2284 | SEC_HAS_CONTENTS
2285 | SEC_IN_MEMORY
2286 | SEC_LINKER_CREATED
2287 | SEC_READONLY))
2288 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2289 return false;
2290 }
2291 }
2292
2293 if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata2, h, rel))
2294 return false;
2295
2296 break;
2297
2298 case R_PPC_SDAREL16:
2299 case R_PPC_EMB_SDA2REL:
2300 case R_PPC_EMB_SDA21:
2301 if (info->shared)
2302 {
2303 ((*_bfd_error_handler)
2304 (_("%s: relocation %s cannot be used when making a shared object"),
2305 bfd_get_filename (abfd),
2306 ppc_elf_howto_table[(int) ELF32_R_TYPE (rel->r_info)]->name));
2307 return false;
2308 }
2309 break;
2310
2311 case R_PPC_PLT32:
2312 case R_PPC_PLTREL24:
2313 case R_PPC_PLT16_LO:
2314 case R_PPC_PLT16_HI:
2315 case R_PPC_PLT16_HA:
2316 #ifdef DEBUG
2317 fprintf (stderr, "Reloc requires a PLT entry\n");
2318 #endif
2319 /* This symbol requires a procedure linkage table entry. We
2320 actually build the entry in adjust_dynamic_symbol,
2321 because this might be a case of linking PIC code without
2322 linking in any dynamic objects, in which case we don't
2323 need to generate a procedure linkage table after all. */
2324
2325 if (h == NULL)
2326 {
2327 /* It does not make sense to have a procedure linkage
2328 table entry for a local symbol. */
2329 bfd_set_error (bfd_error_bad_value);
2330 return false;
2331 }
2332
2333 /* Make sure this symbol is output as a dynamic symbol. */
2334 if (h->dynindx == -1)
2335 {
2336 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2337 return false;
2338 }
2339 if (h->plt.refcount == -1)
2340 {
2341 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2342 h->plt.refcount = 1;
2343 }
2344 else
2345 h->plt.refcount++;
2346 break;
2347
2348 /* The following relocations don't need to propagate the
2349 relocation if linking a shared object since they are
2350 section relative. */
2351 case R_PPC_SECTOFF:
2352 case R_PPC_SECTOFF_LO:
2353 case R_PPC_SECTOFF_HI:
2354 case R_PPC_SECTOFF_HA:
2355 break;
2356
2357 /* This refers only to functions defined in the shared library */
2358 case R_PPC_LOCAL24PC:
2359 break;
2360
2361 /* This relocation describes the C++ object vtable hierarchy.
2362 Reconstruct it for later use during GC. */
2363 case R_PPC_GNU_VTINHERIT:
2364 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2365 return false;
2366 break;
2367
2368 /* This relocation describes which C++ vtable entries are actually
2369 used. Record for later use during GC. */
2370 case R_PPC_GNU_VTENTRY:
2371 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2372 return false;
2373 break;
2374
2375 /* When creating a shared object, we must copy these
2376 relocs into the output file. We create a reloc
2377 section in dynobj and make room for the reloc. */
2378 case R_PPC_REL24:
2379 case R_PPC_REL14:
2380 case R_PPC_REL14_BRTAKEN:
2381 case R_PPC_REL14_BRNTAKEN:
2382 case R_PPC_REL32:
2383 if (h == NULL
2384 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2385 || SYMBOL_REFERENCES_LOCAL (info, h))
2386 break;
2387 /* fall through */
2388
2389 default:
2390 if (info->shared)
2391 {
2392 #ifdef DEBUG
2393 fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
2394 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
2395 #endif
2396 if (sreloc == NULL)
2397 {
2398 const char *name;
2399
2400 name = (bfd_elf_string_from_elf_section
2401 (abfd,
2402 elf_elfheader (abfd)->e_shstrndx,
2403 elf_section_data (sec)->rel_hdr.sh_name));
2404 if (name == NULL)
2405 return false;
2406
2407 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2408 && strcmp (bfd_get_section_name (abfd, sec),
2409 name + 5) == 0);
2410
2411 sreloc = bfd_get_section_by_name (dynobj, name);
2412 if (sreloc == NULL)
2413 {
2414 flagword flags;
2415
2416 sreloc = bfd_make_section (dynobj, name);
2417 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2418 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2419 if ((sec->flags & SEC_ALLOC) != 0)
2420 flags |= SEC_ALLOC | SEC_LOAD;
2421 if (sreloc == NULL
2422 || ! bfd_set_section_flags (dynobj, sreloc, flags)
2423 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
2424 return false;
2425 }
2426 if (sec->flags & SEC_READONLY)
2427 info->flags |= DF_TEXTREL;
2428 }
2429
2430 sreloc->_raw_size += sizeof (Elf32_External_Rela);
2431
2432 /* FIXME: We should here do what the m68k and i386
2433 backends do: if the reloc is pc-relative, record it
2434 in case it turns out that the reloc is unnecessary
2435 because the symbol is forced local by versioning or
2436 we are linking with -Bdynamic. Fortunately this
2437 case is not frequent. */
2438 }
2439
2440 break;
2441 }
2442 }
2443
2444 return true;
2445 }
2446
2447 /* Return the section that should be marked against GC for a given
2448 relocation. */
2449
2450 static asection *
2451 ppc_elf_gc_mark_hook (abfd, info, rel, h, sym)
2452 bfd *abfd;
2453 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2454 Elf_Internal_Rela *rel;
2455 struct elf_link_hash_entry *h;
2456 Elf_Internal_Sym *sym;
2457 {
2458 if (h != NULL)
2459 {
2460 switch (ELF32_R_TYPE (rel->r_info))
2461 {
2462 case R_PPC_GNU_VTINHERIT:
2463 case R_PPC_GNU_VTENTRY:
2464 break;
2465
2466 default:
2467 switch (h->root.type)
2468 {
2469 case bfd_link_hash_defined:
2470 case bfd_link_hash_defweak:
2471 return h->root.u.def.section;
2472
2473 case bfd_link_hash_common:
2474 return h->root.u.c.p->section;
2475
2476 default:
2477 break;
2478 }
2479 }
2480 }
2481 else
2482 {
2483 if (!(elf_bad_symtab (abfd)
2484 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
2485 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
2486 && sym->st_shndx != SHN_COMMON))
2487 {
2488 return bfd_section_from_elf_index (abfd, sym->st_shndx);
2489 }
2490 }
2491
2492 return NULL;
2493 }
2494
2495 /* Update the got entry reference counts for the section being removed. */
2496
2497 static boolean
2498 ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
2499 bfd *abfd;
2500 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2501 asection *sec;
2502 const Elf_Internal_Rela *relocs;
2503 {
2504 Elf_Internal_Shdr *symtab_hdr;
2505 struct elf_link_hash_entry **sym_hashes;
2506 bfd_signed_vma *local_got_refcounts;
2507 const Elf_Internal_Rela *rel, *relend;
2508 unsigned long r_symndx;
2509 struct elf_link_hash_entry *h;
2510
2511 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2512 sym_hashes = elf_sym_hashes (abfd);
2513 local_got_refcounts = elf_local_got_refcounts (abfd);
2514
2515 relend = relocs + sec->reloc_count;
2516 for (rel = relocs; rel < relend; rel++)
2517 switch (ELF32_R_TYPE (rel->r_info))
2518 {
2519 case R_PPC_GOT16:
2520 case R_PPC_GOT16_LO:
2521 case R_PPC_GOT16_HI:
2522 case R_PPC_GOT16_HA:
2523 r_symndx = ELF32_R_SYM (rel->r_info);
2524 if (r_symndx >= symtab_hdr->sh_info)
2525 {
2526 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2527 if (h->got.refcount > 0)
2528 h->got.refcount--;
2529 }
2530 else if (local_got_refcounts != NULL)
2531 {
2532 if (local_got_refcounts[r_symndx] > 0)
2533 local_got_refcounts[r_symndx]--;
2534 }
2535 break;
2536
2537 case R_PPC_PLT32:
2538 case R_PPC_PLTREL24:
2539 case R_PPC_PLT16_LO:
2540 case R_PPC_PLT16_HI:
2541 case R_PPC_PLT16_HA:
2542 r_symndx = ELF32_R_SYM (rel->r_info);
2543 if (r_symndx >= symtab_hdr->sh_info)
2544 {
2545 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2546 if (h->plt.refcount > 0)
2547 h->plt.refcount--;
2548 }
2549 break;
2550
2551 default:
2552 break;
2553 }
2554
2555 return true;
2556 }
2557 \f
2558 /* Hook called by the linker routine which adds symbols from an object
2559 file. We use it to put .comm items in .sbss, and not .bss. */
2560
2561 static boolean
2562 ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2563 bfd *abfd;
2564 struct bfd_link_info *info;
2565 const Elf_Internal_Sym *sym;
2566 const char **namep ATTRIBUTE_UNUSED;
2567 flagword *flagsp ATTRIBUTE_UNUSED;
2568 asection **secp;
2569 bfd_vma *valp;
2570 {
2571 if (sym->st_shndx == SHN_COMMON
2572 && !info->relocateable
2573 && sym->st_size <= elf_gp_size (abfd))
2574 {
2575 /* Common symbols less than or equal to -G nn bytes are automatically
2576 put into .sdata. */
2577 elf_linker_section_t *sdata
2578 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2579
2580 if (!sdata->bss_section)
2581 {
2582 /* We don't go through bfd_make_section, because we don't
2583 want to attach this common section to DYNOBJ. The linker
2584 will move the symbols to the appropriate output section
2585 when it defines common symbols. */
2586 sdata->bss_section = ((asection *)
2587 bfd_zalloc (abfd, sizeof (asection)));
2588 if (sdata->bss_section == NULL)
2589 return false;
2590 sdata->bss_section->name = sdata->bss_name;
2591 sdata->bss_section->flags = SEC_IS_COMMON;
2592 sdata->bss_section->output_section = sdata->bss_section;
2593 sdata->bss_section->symbol =
2594 (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
2595 sdata->bss_section->symbol_ptr_ptr =
2596 (asymbol **) bfd_zalloc (abfd, sizeof (asymbol *));
2597 if (sdata->bss_section->symbol == NULL
2598 || sdata->bss_section->symbol_ptr_ptr == NULL)
2599 return false;
2600 sdata->bss_section->symbol->name = sdata->bss_name;
2601 sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
2602 sdata->bss_section->symbol->section = sdata->bss_section;
2603 *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
2604 }
2605
2606 *secp = sdata->bss_section;
2607 *valp = sym->st_size;
2608 }
2609
2610 return true;
2611 }
2612 \f
2613 /* Finish up dynamic symbol handling. We set the contents of various
2614 dynamic sections here. */
2615
2616 static boolean
2617 ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
2618 bfd *output_bfd;
2619 struct bfd_link_info *info;
2620 struct elf_link_hash_entry *h;
2621 Elf_Internal_Sym *sym;
2622 {
2623 bfd *dynobj;
2624
2625 #ifdef DEBUG
2626 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
2627 h->root.root.string);
2628 #endif
2629
2630 dynobj = elf_hash_table (info)->dynobj;
2631 BFD_ASSERT (dynobj != NULL);
2632
2633 if (h->plt.offset != (bfd_vma) -1)
2634 {
2635 asection *splt;
2636 asection *srela;
2637 Elf_Internal_Rela rela;
2638 bfd_vma reloc_index;
2639
2640 #ifdef DEBUG
2641 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
2642 #endif
2643
2644 /* This symbol has an entry in the procedure linkage table. Set
2645 it up. */
2646
2647 BFD_ASSERT (h->dynindx != -1);
2648
2649 splt = bfd_get_section_by_name (dynobj, ".plt");
2650 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2651 BFD_ASSERT (splt != NULL && srela != NULL);
2652
2653 /* We don't need to fill in the .plt. The ppc dynamic linker
2654 will fill it in. */
2655
2656 /* Fill in the entry in the .rela.plt section. */
2657 rela.r_offset = (splt->output_section->vma
2658 + splt->output_offset
2659 + h->plt.offset);
2660 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
2661 rela.r_addend = 0;
2662
2663 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
2664 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
2665 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
2666 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2667 ((Elf32_External_Rela *) srela->contents
2668 + reloc_index));
2669
2670 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2671 {
2672 /* Mark the symbol as undefined, rather than as defined in
2673 the .plt section. Leave the value alone. */
2674 sym->st_shndx = SHN_UNDEF;
2675 /* If the symbol is weak, we do need to clear the value.
2676 Otherwise, the PLT entry would provide a definition for
2677 the symbol even if the symbol wasn't defined anywhere,
2678 and so the symbol would never be NULL. */
2679 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
2680 == 0)
2681 sym->st_value = 0;
2682 }
2683 }
2684
2685 if (h->got.offset != (bfd_vma) -1)
2686 {
2687 asection *sgot;
2688 asection *srela;
2689 Elf_Internal_Rela rela;
2690
2691 /* This symbol has an entry in the global offset table. Set it
2692 up. */
2693
2694 sgot = bfd_get_section_by_name (dynobj, ".got");
2695 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2696 BFD_ASSERT (sgot != NULL && srela != NULL);
2697
2698 rela.r_offset = (sgot->output_section->vma
2699 + sgot->output_offset
2700 + (h->got.offset &~ 1));
2701
2702 /* If this is a -Bsymbolic link, and the symbol is defined
2703 locally, we just want to emit a RELATIVE reloc. The entry in
2704 the global offset table will already have been initialized in
2705 the relocate_section function. */
2706 if (info->shared
2707 && SYMBOL_REFERENCES_LOCAL (info, h))
2708 {
2709 rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
2710 rela.r_addend = (h->root.u.def.value
2711 + h->root.u.def.section->output_section->vma
2712 + h->root.u.def.section->output_offset);
2713 }
2714 else
2715 {
2716 BFD_ASSERT ((h->got.offset & 1) == 0);
2717 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
2718 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_GLOB_DAT);
2719 rela.r_addend = 0;
2720 }
2721
2722 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2723 ((Elf32_External_Rela *) srela->contents
2724 + srela->reloc_count));
2725 ++srela->reloc_count;
2726 }
2727
2728 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2729 {
2730 asection *s;
2731 Elf_Internal_Rela rela;
2732
2733 /* This symbols needs a copy reloc. Set it up. */
2734
2735 #ifdef DEBUG
2736 fprintf (stderr, ", copy");
2737 #endif
2738
2739 BFD_ASSERT (h->dynindx != -1);
2740
2741 if (h->size <= elf_gp_size (dynobj))
2742 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2743 ".rela.sbss");
2744 else
2745 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2746 ".rela.bss");
2747 BFD_ASSERT (s != NULL);
2748
2749 rela.r_offset = (h->root.u.def.value
2750 + h->root.u.def.section->output_section->vma
2751 + h->root.u.def.section->output_offset);
2752 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
2753 rela.r_addend = 0;
2754 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2755 ((Elf32_External_Rela *) s->contents
2756 + s->reloc_count));
2757 ++s->reloc_count;
2758 }
2759
2760 #ifdef DEBUG
2761 fprintf (stderr, "\n");
2762 #endif
2763
2764 /* Mark some specially defined symbols as absolute. */
2765 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2766 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2767 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2768 sym->st_shndx = SHN_ABS;
2769
2770 return true;
2771 }
2772 \f
2773 /* Finish up the dynamic sections. */
2774
2775 static boolean
2776 ppc_elf_finish_dynamic_sections (output_bfd, info)
2777 bfd *output_bfd;
2778 struct bfd_link_info *info;
2779 {
2780 asection *sdyn;
2781 bfd *dynobj = elf_hash_table (info)->dynobj;
2782 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
2783
2784 #ifdef DEBUG
2785 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
2786 #endif
2787
2788 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2789
2790 if (elf_hash_table (info)->dynamic_sections_created)
2791 {
2792 asection *splt;
2793 Elf32_External_Dyn *dyncon, *dynconend;
2794
2795 splt = bfd_get_section_by_name (dynobj, ".plt");
2796 BFD_ASSERT (splt != NULL && sdyn != NULL);
2797
2798 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2799 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2800 for (; dyncon < dynconend; dyncon++)
2801 {
2802 Elf_Internal_Dyn dyn;
2803 const char *name;
2804 boolean size;
2805
2806 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2807
2808 switch (dyn.d_tag)
2809 {
2810 case DT_PLTGOT: name = ".plt"; size = false; break;
2811 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
2812 case DT_JMPREL: name = ".rela.plt"; size = false; break;
2813 default: name = NULL; size = false; break;
2814 }
2815
2816 if (name != NULL)
2817 {
2818 asection *s;
2819
2820 s = bfd_get_section_by_name (output_bfd, name);
2821 if (s == NULL)
2822 dyn.d_un.d_val = 0;
2823 else
2824 {
2825 if (! size)
2826 dyn.d_un.d_ptr = s->vma;
2827 else
2828 {
2829 if (s->_cooked_size != 0)
2830 dyn.d_un.d_val = s->_cooked_size;
2831 else
2832 dyn.d_un.d_val = s->_raw_size;
2833 }
2834 }
2835 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2836 }
2837 }
2838 }
2839
2840 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
2841 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
2842 if (sgot)
2843 {
2844 unsigned char *contents = sgot->contents;
2845 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
2846
2847 if (sdyn == NULL)
2848 bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
2849 else
2850 bfd_put_32 (output_bfd,
2851 sdyn->output_section->vma + sdyn->output_offset,
2852 contents+4);
2853
2854 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2855 }
2856
2857 return true;
2858 }
2859 \f
2860 /* The RELOCATE_SECTION function is called by the ELF backend linker
2861 to handle the relocations for a section.
2862
2863 The relocs are always passed as Rela structures; if the section
2864 actually uses Rel structures, the r_addend field will always be
2865 zero.
2866
2867 This function is responsible for adjust the section contents as
2868 necessary, and (if using Rela relocs and generating a
2869 relocateable output file) adjusting the reloc addend as
2870 necessary.
2871
2872 This function does not have to worry about setting the reloc
2873 address or the reloc symbol index.
2874
2875 LOCAL_SYMS is a pointer to the swapped in local symbols.
2876
2877 LOCAL_SECTIONS is an array giving the section in the input file
2878 corresponding to the st_shndx field of each local symbol.
2879
2880 The global hash table entry for the global symbols can be found
2881 via elf_sym_hashes (input_bfd).
2882
2883 When generating relocateable output, this function must handle
2884 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2885 going to be the section symbol corresponding to the output
2886 section, which means that the addend must be adjusted
2887 accordingly. */
2888
2889 static boolean
2890 ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2891 contents, relocs, local_syms, local_sections)
2892 bfd *output_bfd;
2893 struct bfd_link_info *info;
2894 bfd *input_bfd;
2895 asection *input_section;
2896 bfd_byte *contents;
2897 Elf_Internal_Rela *relocs;
2898 Elf_Internal_Sym *local_syms;
2899 asection **local_sections;
2900 {
2901 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2902 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2903 bfd *dynobj = elf_hash_table (info)->dynobj;
2904 elf_linker_section_t *sdata = (dynobj) ? elf_linker_section (dynobj, LINKER_SECTION_SDATA) : NULL;
2905 elf_linker_section_t *sdata2 = (dynobj) ? elf_linker_section (dynobj, LINKER_SECTION_SDATA2) : NULL;
2906 Elf_Internal_Rela *rel = relocs;
2907 Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
2908 asection *sreloc = NULL;
2909 asection *splt;
2910 asection *sgot;
2911 bfd_vma *local_got_offsets;
2912 boolean ret = true;
2913 long insn;
2914
2915 #ifdef DEBUG
2916 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
2917 bfd_get_filename (input_bfd),
2918 bfd_section_name(input_bfd, input_section),
2919 (long) input_section->reloc_count,
2920 (info->relocateable) ? " (relocatable)" : "");
2921 #endif
2922
2923 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2924 /* Initialize howto table if needed. */
2925 ppc_elf_howto_init ();
2926
2927 local_got_offsets = elf_local_got_offsets (input_bfd);
2928
2929 splt = sgot = NULL;
2930 if (dynobj != NULL)
2931 {
2932 splt = bfd_get_section_by_name (dynobj, ".plt");
2933 sgot = bfd_get_section_by_name (dynobj, ".got");
2934 }
2935
2936 for (; rel < relend; rel++)
2937 {
2938 enum elf_ppc_reloc_type r_type = (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
2939 bfd_vma offset = rel->r_offset;
2940 bfd_vma addend = rel->r_addend;
2941 bfd_reloc_status_type r = bfd_reloc_other;
2942 Elf_Internal_Sym *sym = (Elf_Internal_Sym *) 0;
2943 asection *sec = (asection *) 0;
2944 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) 0;
2945 const char *sym_name = (const char *) 0;
2946 reloc_howto_type *howto;
2947 unsigned long r_symndx;
2948 bfd_vma relocation;
2949 int will_become_local;
2950
2951 /* Unknown relocation handling */
2952 if ((unsigned) r_type >= (unsigned) R_PPC_max
2953 || !ppc_elf_howto_table[(int) r_type])
2954 {
2955 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
2956 bfd_get_filename (input_bfd),
2957 (int) r_type);
2958
2959 bfd_set_error (bfd_error_bad_value);
2960 ret = false;
2961 continue;
2962 }
2963
2964 howto = ppc_elf_howto_table[(int) r_type];
2965 r_symndx = ELF32_R_SYM (rel->r_info);
2966
2967 if (info->relocateable)
2968 {
2969 /* This is a relocateable link. We don't have to change
2970 anything, unless the reloc is against a section symbol,
2971 in which case we have to adjust according to where the
2972 section symbol winds up in the output section. */
2973 if (r_symndx < symtab_hdr->sh_info)
2974 {
2975 sym = local_syms + r_symndx;
2976 if ((unsigned) ELF_ST_TYPE (sym->st_info) == STT_SECTION)
2977 {
2978 sec = local_sections[r_symndx];
2979 addend = rel->r_addend += sec->output_offset + sym->st_value;
2980 }
2981 }
2982
2983 #ifdef DEBUG
2984 fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
2985 howto->name,
2986 (int) r_type,
2987 r_symndx,
2988 (long) offset,
2989 (long) addend);
2990 #endif
2991 continue;
2992 }
2993
2994 /* This is a final link. */
2995 if (r_symndx < symtab_hdr->sh_info)
2996 {
2997 sym = local_syms + r_symndx;
2998 sec = local_sections[r_symndx];
2999 sym_name = "<local symbol>";
3000
3001 relocation = (sec->output_section->vma
3002 + sec->output_offset
3003 + sym->st_value);
3004 /* Relocs to local symbols are always resolved. */
3005 will_become_local = 1;
3006 }
3007 else
3008 {
3009 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3010 while (h->root.type == bfd_link_hash_indirect
3011 || h->root.type == bfd_link_hash_warning)
3012 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3013 sym_name = h->root.root.string;
3014
3015 /* Can this relocation be resolved immediately? */
3016 will_become_local = SYMBOL_REFERENCES_LOCAL (info, h);
3017
3018 if (h->root.type == bfd_link_hash_defined
3019 || h->root.type == bfd_link_hash_defweak)
3020 {
3021 sec = h->root.u.def.section;
3022 if (((r_type == R_PPC_PLT32
3023 || r_type == R_PPC_PLTREL24)
3024 && splt != NULL
3025 && h->plt.offset != (bfd_vma) -1)
3026 || (r_type == R_PPC_LOCAL24PC
3027 && sec->output_section == NULL)
3028 || ((r_type == R_PPC_GOT16
3029 || r_type == R_PPC_GOT16_LO
3030 || r_type == R_PPC_GOT16_HI
3031 || r_type == R_PPC_GOT16_HA)
3032 && elf_hash_table (info)->dynamic_sections_created
3033 && (! info->shared || ! will_become_local))
3034 || (info->shared
3035 && ! will_become_local
3036 && ((input_section->flags & SEC_ALLOC) != 0
3037 /* Testing SEC_DEBUGGING here may be wrong.
3038 It's here to avoid a crash when
3039 generating a shared library with DWARF
3040 debugging information. */
3041 || ((input_section->flags & SEC_DEBUGGING) != 0
3042 && (h->elf_link_hash_flags
3043 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
3044 && (r_type == R_PPC_ADDR32
3045 || r_type == R_PPC_ADDR24
3046 || r_type == R_PPC_ADDR16
3047 || r_type == R_PPC_ADDR16_LO
3048 || r_type == R_PPC_ADDR16_HI
3049 || r_type == R_PPC_ADDR16_HA
3050 || r_type == R_PPC_ADDR14
3051 || r_type == R_PPC_ADDR14_BRTAKEN
3052 || r_type == R_PPC_ADDR14_BRNTAKEN
3053 || r_type == R_PPC_COPY
3054 || r_type == R_PPC_GLOB_DAT
3055 || r_type == R_PPC_JMP_SLOT
3056 || r_type == R_PPC_UADDR32
3057 || r_type == R_PPC_UADDR16
3058 || r_type == R_PPC_SDAREL16
3059 || r_type == R_PPC_EMB_NADDR32
3060 || r_type == R_PPC_EMB_NADDR16
3061 || r_type == R_PPC_EMB_NADDR16_LO
3062 || r_type == R_PPC_EMB_NADDR16_HI
3063 || r_type == R_PPC_EMB_NADDR16_HA
3064 || r_type == R_PPC_EMB_SDAI16
3065 || r_type == R_PPC_EMB_SDA2I16
3066 || r_type == R_PPC_EMB_SDA2REL
3067 || r_type == R_PPC_EMB_SDA21
3068 || r_type == R_PPC_EMB_MRKREF
3069 || r_type == R_PPC_EMB_BIT_FLD
3070 || r_type == R_PPC_EMB_RELSDA
3071 || ((r_type == R_PPC_REL24
3072 || r_type == R_PPC_REL32
3073 || r_type == R_PPC_REL14
3074 || r_type == R_PPC_REL14_BRTAKEN
3075 || r_type == R_PPC_REL14_BRNTAKEN
3076 || r_type == R_PPC_RELATIVE)
3077 && strcmp (h->root.root.string,
3078 "_GLOBAL_OFFSET_TABLE_") != 0))))
3079 {
3080 /* In these cases, we don't need the relocation
3081 value. We check specially because in some
3082 obscure cases sec->output_section will be NULL. */
3083 relocation = 0;
3084 }
3085 else if (sec->output_section == NULL)
3086 {
3087 (*_bfd_error_handler)
3088 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
3089 bfd_get_filename (input_bfd), h->root.root.string,
3090 bfd_get_section_name (input_bfd, input_section));
3091 relocation = 0;
3092 }
3093 else
3094 relocation = (h->root.u.def.value
3095 + sec->output_section->vma
3096 + sec->output_offset);
3097 }
3098 else if (h->root.type == bfd_link_hash_undefweak)
3099 relocation = 0;
3100 else if (info->shared && !info->symbolic && !info->no_undefined
3101 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3102 relocation = 0;
3103 else
3104 {
3105 if (! (*info->callbacks->undefined_symbol) (info,
3106 h->root.root.string,
3107 input_bfd,
3108 input_section,
3109 rel->r_offset,
3110 (!info->shared
3111 || info->no_undefined
3112 || ELF_ST_VISIBILITY (h->other))))
3113 return false;
3114 relocation = 0;
3115 }
3116 }
3117
3118 switch ((int) r_type)
3119 {
3120 default:
3121 (*_bfd_error_handler) (_("%s: unknown relocation type %d for symbol %s"),
3122 bfd_get_filename (input_bfd),
3123 (int) r_type, sym_name);
3124
3125 bfd_set_error (bfd_error_bad_value);
3126 ret = false;
3127 continue;
3128
3129 /* Relocations that need no special processing. */
3130 case (int) R_PPC_LOCAL24PC:
3131 /* It makes no sense to point a local relocation
3132 at a symbol not in this object. */
3133 if (h != NULL
3134 && (h->root.type == bfd_link_hash_defined
3135 || h->root.type == bfd_link_hash_defweak)
3136 && sec->output_section == NULL)
3137 {
3138 if (! (*info->callbacks->undefined_symbol) (info,
3139 h->root.root.string,
3140 input_bfd,
3141 input_section,
3142 rel->r_offset,
3143 true))
3144 return false;
3145 continue;
3146 }
3147 break;
3148
3149 /* Relocations that may need to be propagated if this is a shared
3150 object. */
3151 case (int) R_PPC_REL24:
3152 case (int) R_PPC_REL32:
3153 case (int) R_PPC_REL14:
3154 /* If these relocations are not to a named symbol, they can be
3155 handled right here, no need to bother the dynamic linker. */
3156 if (h == NULL
3157 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3158 || SYMBOL_REFERENCES_LOCAL (info, h))
3159 break;
3160 /* fall through */
3161
3162 /* Relocations that always need to be propagated if this is a shared
3163 object. */
3164 case (int) R_PPC_NONE:
3165 case (int) R_PPC_ADDR32:
3166 case (int) R_PPC_ADDR24:
3167 case (int) R_PPC_ADDR16:
3168 case (int) R_PPC_ADDR16_LO:
3169 case (int) R_PPC_ADDR16_HI:
3170 case (int) R_PPC_ADDR16_HA:
3171 case (int) R_PPC_ADDR14:
3172 case (int) R_PPC_UADDR32:
3173 case (int) R_PPC_UADDR16:
3174 if (info->shared)
3175 {
3176 Elf_Internal_Rela outrel;
3177 boolean skip;
3178
3179 #ifdef DEBUG
3180 fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
3181 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
3182 #endif
3183
3184 /* When generating a shared object, these relocations
3185 are copied into the output file to be resolved at run
3186 time. */
3187
3188 if (sreloc == NULL)
3189 {
3190 const char *name;
3191
3192 name = (bfd_elf_string_from_elf_section
3193 (input_bfd,
3194 elf_elfheader (input_bfd)->e_shstrndx,
3195 elf_section_data (input_section)->rel_hdr.sh_name));
3196 if (name == NULL)
3197 return false;
3198
3199 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3200 && strcmp (bfd_get_section_name (input_bfd,
3201 input_section),
3202 name + 5) == 0);
3203
3204 sreloc = bfd_get_section_by_name (dynobj, name);
3205 BFD_ASSERT (sreloc != NULL);
3206 }
3207
3208 skip = false;
3209
3210 if (elf_section_data (input_section)->stab_info == NULL)
3211 outrel.r_offset = rel->r_offset;
3212 else
3213 {
3214 bfd_vma off;
3215
3216 off = (_bfd_stab_section_offset
3217 (output_bfd, &elf_hash_table (info)->stab_info,
3218 input_section,
3219 &elf_section_data (input_section)->stab_info,
3220 rel->r_offset));
3221 if (off == (bfd_vma) -1)
3222 skip = true;
3223 outrel.r_offset = off;
3224 }
3225
3226 outrel.r_offset += (input_section->output_section->vma
3227 + input_section->output_offset);
3228
3229 if (skip)
3230 memset (&outrel, 0, sizeof outrel);
3231 /* h->dynindx may be -1 if this symbol was marked to
3232 become local. */
3233 else if (! will_become_local)
3234 {
3235 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3236 outrel.r_addend = rel->r_addend;
3237 }
3238 else
3239 {
3240 if (r_type == R_PPC_ADDR32)
3241 {
3242 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3243 outrel.r_addend = relocation + rel->r_addend;
3244 }
3245 else
3246 {
3247 long indx;
3248
3249 if (h == NULL)
3250 sec = local_sections[r_symndx];
3251 else
3252 {
3253 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3254 || (h->root.type
3255 == bfd_link_hash_defweak));
3256 sec = h->root.u.def.section;
3257 }
3258 if (sec != NULL && bfd_is_abs_section (sec))
3259 indx = 0;
3260 else if (sec == NULL || sec->owner == NULL)
3261 {
3262 bfd_set_error (bfd_error_bad_value);
3263 return false;
3264 }
3265 else
3266 {
3267 asection *osec;
3268
3269 osec = sec->output_section;
3270 indx = elf_section_data (osec)->dynindx;
3271 BFD_ASSERT (indx > 0);
3272 #ifdef DEBUG
3273 if (indx <= 0)
3274 {
3275 printf ("indx=%d section=%s flags=%08x name=%s\n",
3276 indx, osec->name, osec->flags,
3277 h->root.root.string);
3278 }
3279 #endif
3280 }
3281
3282 outrel.r_info = ELF32_R_INFO (indx, r_type);
3283 outrel.r_addend = relocation + rel->r_addend;
3284 }
3285 }
3286
3287 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3288 (((Elf32_External_Rela *)
3289 sreloc->contents)
3290 + sreloc->reloc_count));
3291 ++sreloc->reloc_count;
3292
3293 /* This reloc will be computed at runtime, so there's no
3294 need to do anything now, unless this is a RELATIVE
3295 reloc in an unallocated section. */
3296 if (skip
3297 || (input_section->flags & SEC_ALLOC) != 0
3298 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE)
3299 continue;
3300 }
3301
3302 /* Arithmetic adjust relocations that aren't going into a
3303 shared object. */
3304 if (r_type == R_PPC_ADDR16_HA
3305 /* It's just possible that this symbol is a weak symbol
3306 that's not actually defined anywhere. In that case,
3307 'sec' would be NULL, and we should leave the symbol
3308 alone (it will be set to zero elsewhere in the link). */
3309 && sec != NULL)
3310 {
3311 addend += ((relocation + addend) & 0x8000) << 1;
3312 }
3313 break;
3314
3315 /* branch taken prediction relocations */
3316 case (int) R_PPC_ADDR14_BRTAKEN:
3317 case (int) R_PPC_REL14_BRTAKEN:
3318 insn = bfd_get_32 (output_bfd, contents + offset);
3319 if ((relocation - offset) & 0x8000)
3320 insn &= ~BRANCH_PREDICT_BIT;
3321 else
3322 insn |= BRANCH_PREDICT_BIT;
3323 bfd_put_32 (output_bfd, insn, contents + offset);
3324 break;
3325
3326 /* branch not taken predicition relocations */
3327 case (int) R_PPC_ADDR14_BRNTAKEN:
3328 case (int) R_PPC_REL14_BRNTAKEN:
3329 insn = bfd_get_32 (output_bfd, contents + offset);
3330 if ((relocation - offset) & 0x8000)
3331 insn |= BRANCH_PREDICT_BIT;
3332 else
3333 insn &= ~BRANCH_PREDICT_BIT;
3334 bfd_put_32 (output_bfd, insn, contents + offset);
3335 break;
3336
3337 /* GOT16 relocations */
3338 case (int) R_PPC_GOT16:
3339 case (int) R_PPC_GOT16_LO:
3340 case (int) R_PPC_GOT16_HI:
3341 case (int) R_PPC_GOT16_HA:
3342 /* Relocation is to the entry for this symbol in the global
3343 offset table. */
3344 BFD_ASSERT (sgot != NULL);
3345
3346 if (h != NULL)
3347 {
3348 bfd_vma off;
3349
3350 off = h->got.offset;
3351 BFD_ASSERT (off != (bfd_vma) -1);
3352
3353 if (! elf_hash_table (info)->dynamic_sections_created
3354 || (info->shared
3355 && SYMBOL_REFERENCES_LOCAL (info, h)))
3356 {
3357 /* This is actually a static link, or it is a
3358 -Bsymbolic link and the symbol is defined
3359 locally. We must initialize this entry in the
3360 global offset table. Since the offset must
3361 always be a multiple of 4, we use the least
3362 significant bit to record whether we have
3363 initialized it already.
3364
3365 When doing a dynamic link, we create a .rela.got
3366 relocation entry to initialize the value. This
3367 is done in the finish_dynamic_symbol routine. */
3368 if ((off & 1) != 0)
3369 off &= ~1;
3370 else
3371 {
3372 bfd_put_32 (output_bfd, relocation,
3373 sgot->contents + off);
3374 h->got.offset |= 1;
3375 }
3376 }
3377
3378 relocation = sgot->output_offset + off - 4;
3379 }
3380 else
3381 {
3382 bfd_vma off;
3383
3384 BFD_ASSERT (local_got_offsets != NULL
3385 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3386
3387 off = local_got_offsets[r_symndx];
3388
3389 /* The offset must always be a multiple of 4. We use
3390 the least significant bit to record whether we have
3391 already processed this entry. */
3392 if ((off & 1) != 0)
3393 off &= ~1;
3394 else
3395 {
3396 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3397
3398 if (info->shared)
3399 {
3400 asection *srelgot;
3401 Elf_Internal_Rela outrel;
3402
3403 /* We need to generate a R_PPC_RELATIVE reloc
3404 for the dynamic linker. */
3405 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3406 BFD_ASSERT (srelgot != NULL);
3407
3408 outrel.r_offset = (sgot->output_section->vma
3409 + sgot->output_offset
3410 + off);
3411 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3412 outrel.r_addend = relocation;
3413 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3414 (((Elf32_External_Rela *)
3415 srelgot->contents)
3416 + srelgot->reloc_count));
3417 ++srelgot->reloc_count;
3418 }
3419
3420 local_got_offsets[r_symndx] |= 1;
3421 }
3422
3423 relocation = sgot->output_offset + off - 4;
3424 }
3425 break;
3426
3427 /* Indirect .sdata relocation */
3428 case (int) R_PPC_EMB_SDAI16:
3429 BFD_ASSERT (sdata != NULL);
3430 relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3431 sdata, h, relocation, rel,
3432 R_PPC_RELATIVE);
3433 break;
3434
3435 /* Indirect .sdata2 relocation */
3436 case (int) R_PPC_EMB_SDA2I16:
3437 BFD_ASSERT (sdata2 != NULL);
3438 relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3439 sdata2, h, relocation, rel,
3440 R_PPC_RELATIVE);
3441 break;
3442
3443 /* Handle the TOC16 reloc. We want to use the offset within the .got
3444 section, not the actual VMA. This is appropriate when generating
3445 an embedded ELF object, for which the .got section acts like the
3446 AIX .toc section. */
3447 case (int) R_PPC_TOC16: /* phony GOT16 relocations */
3448 BFD_ASSERT (sec != (asection *) 0);
3449 BFD_ASSERT (bfd_is_und_section (sec)
3450 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
3451 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
3452
3453 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
3454 break;
3455
3456 case (int) R_PPC_PLTREL24:
3457 /* Relocation is to the entry for this symbol in the
3458 procedure linkage table. */
3459 BFD_ASSERT (h != NULL);
3460
3461 if (h->plt.offset == (bfd_vma) -1
3462 || splt == NULL)
3463 {
3464 /* We didn't make a PLT entry for this symbol. This
3465 happens when statically linking PIC code, or when
3466 using -Bsymbolic. */
3467 break;
3468 }
3469
3470 relocation = (splt->output_section->vma
3471 + splt->output_offset
3472 + h->plt.offset);
3473 break;
3474
3475 /* relocate against _SDA_BASE_ */
3476 case (int) R_PPC_SDAREL16:
3477 {
3478 const char *name;
3479
3480 BFD_ASSERT (sec != (asection *) 0);
3481 name = bfd_get_section_name (abfd, sec->output_section);
3482 if (strcmp (name, ".sdata") != 0
3483 && strcmp (name, ".sbss") != 0)
3484 {
3485 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3486 bfd_get_filename (input_bfd),
3487 sym_name,
3488 ppc_elf_howto_table[(int) r_type]->name,
3489 name);
3490 }
3491 addend -= (sdata->sym_hash->root.u.def.value
3492 + sdata->sym_hash->root.u.def.section->output_section->vma
3493 + sdata->sym_hash->root.u.def.section->output_offset);
3494 }
3495 break;
3496
3497 /* relocate against _SDA2_BASE_ */
3498 case (int) R_PPC_EMB_SDA2REL:
3499 {
3500 const char *name;
3501
3502 BFD_ASSERT (sec != (asection *) 0);
3503 name = bfd_get_section_name (abfd, sec->output_section);
3504 if (strcmp (name, ".sdata2") != 0 && strcmp (name, ".sbss2") != 0)
3505 {
3506 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3507 bfd_get_filename (input_bfd),
3508 sym_name,
3509 ppc_elf_howto_table[(int) r_type]->name,
3510 name);
3511
3512 bfd_set_error (bfd_error_bad_value);
3513 ret = false;
3514 continue;
3515 }
3516 addend -= (sdata2->sym_hash->root.u.def.value
3517 + sdata2->sym_hash->root.u.def.section->output_section->vma
3518 + sdata2->sym_hash->root.u.def.section->output_offset);
3519 }
3520 break;
3521
3522 /* relocate against either _SDA_BASE_, _SDA2_BASE_, or 0 */
3523 case (int) R_PPC_EMB_SDA21:
3524 case (int) R_PPC_EMB_RELSDA:
3525 {
3526 const char *name;
3527 int reg;
3528
3529 BFD_ASSERT (sec != (asection *) 0);
3530 name = bfd_get_section_name (abfd, sec->output_section);
3531 if (strcmp (name, ".sdata") == 0 || strcmp (name, ".sbss") == 0)
3532 {
3533 reg = 13;
3534 addend -= (sdata->sym_hash->root.u.def.value
3535 + sdata->sym_hash->root.u.def.section->output_section->vma
3536 + sdata->sym_hash->root.u.def.section->output_offset);
3537 }
3538
3539 else if (strcmp (name, ".sdata2") == 0
3540 || strcmp (name, ".sbss2") == 0)
3541 {
3542 reg = 2;
3543 addend -= (sdata2->sym_hash->root.u.def.value
3544 + sdata2->sym_hash->root.u.def.section->output_section->vma
3545 + sdata2->sym_hash->root.u.def.section->output_offset);
3546 }
3547
3548 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
3549 || strcmp (name, ".PPC.EMB.sbss0") == 0)
3550 {
3551 reg = 0;
3552 }
3553
3554 else
3555 {
3556 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3557 bfd_get_filename (input_bfd),
3558 sym_name,
3559 ppc_elf_howto_table[(int) r_type]->name,
3560 name);
3561
3562 bfd_set_error (bfd_error_bad_value);
3563 ret = false;
3564 continue;
3565 }
3566
3567 if (r_type == R_PPC_EMB_SDA21)
3568 { /* fill in register field */
3569 insn = bfd_get_32 (output_bfd, contents + offset);
3570 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
3571 bfd_put_32 (output_bfd, insn, contents + offset);
3572 }
3573 }
3574 break;
3575
3576 /* Relocate against the beginning of the section */
3577 case (int) R_PPC_SECTOFF:
3578 case (int) R_PPC_SECTOFF_LO:
3579 case (int) R_PPC_SECTOFF_HI:
3580 BFD_ASSERT (sec != (asection *) 0);
3581 addend -= sec->output_section->vma;
3582 break;
3583
3584 case (int) R_PPC_SECTOFF_HA:
3585 BFD_ASSERT (sec != (asection *) 0);
3586 addend -= sec->output_section->vma;
3587 addend += ((relocation + addend) & 0x8000) << 1;
3588 break;
3589
3590 /* Negative relocations */
3591 case (int) R_PPC_EMB_NADDR32:
3592 case (int) R_PPC_EMB_NADDR16:
3593 case (int) R_PPC_EMB_NADDR16_LO:
3594 case (int) R_PPC_EMB_NADDR16_HI:
3595 addend -= 2 * relocation;
3596 break;
3597
3598 case (int) R_PPC_EMB_NADDR16_HA:
3599 addend -= 2 * relocation;
3600 addend += ((relocation + addend) & 0x8000) << 1;
3601 break;
3602
3603 /* NOP relocation that prevents garbage collecting linkers from omitting a
3604 reference. */
3605 case (int) R_PPC_EMB_MRKREF:
3606 continue;
3607
3608 case (int) R_PPC_COPY:
3609 case (int) R_PPC_GLOB_DAT:
3610 case (int) R_PPC_JMP_SLOT:
3611 case (int) R_PPC_RELATIVE:
3612 case (int) R_PPC_PLT32:
3613 case (int) R_PPC_PLTREL32:
3614 case (int) R_PPC_PLT16_LO:
3615 case (int) R_PPC_PLT16_HI:
3616 case (int) R_PPC_PLT16_HA:
3617 case (int) R_PPC_EMB_RELSEC16:
3618 case (int) R_PPC_EMB_RELST_LO:
3619 case (int) R_PPC_EMB_RELST_HI:
3620 case (int) R_PPC_EMB_RELST_HA:
3621 case (int) R_PPC_EMB_BIT_FLD:
3622 (*_bfd_error_handler) (_("%s: Relocation %s is not yet supported for symbol %s."),
3623 bfd_get_filename (input_bfd),
3624 ppc_elf_howto_table[(int) r_type]->name,
3625 sym_name);
3626
3627 bfd_set_error (bfd_error_invalid_operation);
3628 ret = false;
3629 continue;
3630
3631 case (int) R_PPC_GNU_VTINHERIT:
3632 case (int) R_PPC_GNU_VTENTRY:
3633 /* These are no-ops in the end. */
3634 continue;
3635 }
3636
3637 #ifdef DEBUG
3638 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
3639 howto->name,
3640 (int) r_type,
3641 sym_name,
3642 r_symndx,
3643 (long) offset,
3644 (long) addend);
3645 #endif
3646
3647 r = _bfd_final_link_relocate (howto,
3648 input_bfd,
3649 input_section,
3650 contents,
3651 offset,
3652 relocation,
3653 addend);
3654
3655 if (r == bfd_reloc_ok)
3656 ;
3657 else if (r == bfd_reloc_overflow)
3658 {
3659 const char *name;
3660
3661 if (h != NULL)
3662 {
3663 if (h->root.type == bfd_link_hash_undefweak
3664 && howto->pc_relative)
3665 {
3666 /* Assume this is a call protected by other code that
3667 detect the symbol is undefined. If this is the case,
3668 we can safely ignore the overflow. If not, the
3669 program is hosed anyway, and a little warning isn't
3670 going to help. */
3671
3672 continue;
3673 }
3674
3675 name = h->root.root.string;
3676 }
3677 else
3678 {
3679 name = bfd_elf_string_from_elf_section (input_bfd,
3680 symtab_hdr->sh_link,
3681 sym->st_name);
3682 if (name == NULL)
3683 continue;
3684 if (*name == '\0')
3685 name = bfd_section_name (input_bfd, sec);
3686 }
3687
3688 if (! (*info->callbacks->reloc_overflow) (info,
3689 name,
3690 howto->name,
3691 (bfd_vma) 0,
3692 input_bfd,
3693 input_section,
3694 offset))
3695 return false;
3696 }
3697 else
3698 ret = false;
3699 }
3700
3701 #ifdef DEBUG
3702 fprintf (stderr, "\n");
3703 #endif
3704
3705 return ret;
3706 }
3707
3708 static enum elf_reloc_type_class
3709 ppc_elf_reloc_type_class (type)
3710 int type;
3711 {
3712 switch (type)
3713 {
3714 case R_PPC_RELATIVE:
3715 return reloc_class_relative;
3716 case R_PPC_REL24:
3717 case R_PPC_ADDR24:
3718 case R_PPC_JMP_SLOT:
3719 return reloc_class_plt;
3720 case R_PPC_COPY:
3721 return reloc_class_copy;
3722 default:
3723 return reloc_class_normal;
3724 }
3725 }
3726 \f
3727 /* Support for core dump NOTE sections */
3728 static boolean
3729 ppc_elf_grok_prstatus (abfd, note)
3730 bfd *abfd;
3731 Elf_Internal_Note *note;
3732 {
3733 int offset;
3734 int raw_size;
3735
3736 switch (note->descsz)
3737 {
3738 default:
3739 return false;
3740
3741 case 268: /* Linux/PPC */
3742 /* pr_cursig */
3743 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
3744
3745 /* pr_pid */
3746 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
3747
3748 /* pr_reg */
3749 offset = 72;
3750 raw_size = 192;
3751
3752 break;
3753 }
3754
3755 /* Make a ".reg/999" section. */
3756 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3757 raw_size, note->descpos + offset);
3758 }
3759
3760 static boolean
3761 ppc_elf_grok_psinfo (abfd, note)
3762 bfd *abfd;
3763 Elf_Internal_Note *note;
3764 {
3765 switch (note->descsz)
3766 {
3767 default:
3768 return false;
3769
3770 case 128: /* Linux/PPC elf_prpsinfo */
3771 elf_tdata (abfd)->core_program
3772 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
3773 elf_tdata (abfd)->core_command
3774 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
3775 }
3776
3777 /* Note that for some reason, a spurious space is tacked
3778 onto the end of the args in some (at least one anyway)
3779 implementations, so strip it off if it exists. */
3780
3781 {
3782 char *command = elf_tdata (abfd)->core_command;
3783 int n = strlen (command);
3784
3785 if (0 < n && command[n - 1] == ' ')
3786 command[n - 1] = '\0';
3787 }
3788
3789 return true;
3790 }
3791 \f
3792 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
3793 #define TARGET_LITTLE_NAME "elf32-powerpcle"
3794 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
3795 #define TARGET_BIG_NAME "elf32-powerpc"
3796 #define ELF_ARCH bfd_arch_powerpc
3797 #define ELF_MACHINE_CODE EM_PPC
3798 #define ELF_MAXPAGESIZE 0x10000
3799 #define elf_info_to_howto ppc_elf_info_to_howto
3800
3801 #ifdef EM_CYGNUS_POWERPC
3802 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
3803 #endif
3804
3805 #ifdef EM_PPC_OLD
3806 #define ELF_MACHINE_ALT2 EM_PPC_OLD
3807 #endif
3808
3809 #define elf_backend_plt_not_loaded 1
3810 #define elf_backend_got_symbol_offset 4
3811 #define elf_backend_can_gc_sections 1
3812 #define elf_backend_got_header_size 12
3813 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
3814
3815 #define bfd_elf32_bfd_copy_private_bfd_data ppc_elf_copy_private_bfd_data
3816 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
3817 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
3818 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
3819 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
3820 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
3821
3822 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
3823 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
3824 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
3825 #define elf_backend_relocate_section ppc_elf_relocate_section
3826 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
3827 #define elf_backend_check_relocs ppc_elf_check_relocs
3828 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
3829 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
3830 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
3831 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
3832 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
3833 #define elf_backend_fake_sections ppc_elf_fake_sections
3834 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
3835 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
3836 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
3837 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
3838 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
3839
3840 #include "elf32-target.h"