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