Initial revision
[binutils.git] / bfd / elf32-ppc.c
blob25bb24ae4f946f15f0ed95272dbdeca244c1dbce
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This file is based on a preliminary PowerPC ELF ABI. The
22 information may not match the final PowerPC ELF ABI. It includes
23 suggestions from the in-progress Embedded PowerPC ABI, and that
24 information may also not match. */
26 #include "bfd.h"
27 #include "sysdep.h"
28 #include "bfdlink.h"
29 #include "libbfd.h"
30 #include "elf-bfd.h"
31 #include "elf/ppc.h"
33 #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_adjust_dynindx PARAMS ((struct elf_link_hash_entry *, PTR));
85 static boolean ppc_elf_size_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
87 static boolean ppc_elf_relocate_section PARAMS ((bfd *,
88 struct bfd_link_info *info,
89 bfd *,
90 asection *,
91 bfd_byte *,
92 Elf_Internal_Rela *relocs,
93 Elf_Internal_Sym *local_syms,
94 asection **));
96 static boolean ppc_elf_add_symbol_hook PARAMS ((bfd *,
97 struct bfd_link_info *,
98 const Elf_Internal_Sym *,
99 const char **,
100 flagword *,
101 asection **,
102 bfd_vma *));
104 static boolean ppc_elf_finish_dynamic_symbol PARAMS ((bfd *,
105 struct bfd_link_info *,
106 struct elf_link_hash_entry *,
107 Elf_Internal_Sym *));
109 static boolean ppc_elf_finish_dynamic_sections PARAMS ((bfd *, struct bfd_link_info *));
111 #define BRANCH_PREDICT_BIT 0x200000 /* branch prediction bit for branch taken relocs */
112 #define RA_REGISTER_MASK 0x001f0000 /* mask to set RA in memory instructions */
113 #define RA_REGISTER_SHIFT 16 /* value to shift register by to insert RA */
115 /* The name of the dynamic interpreter. This is put in the .interp
116 section. */
118 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
120 /* The size in bytes of an entry in the procedure linkage table. */
121 #define PLT_ENTRY_SIZE 12
122 /* The initial size of the plt reserved for the dynamic linker. */
123 #define PLT_INITIAL_ENTRY_SIZE 72
124 /* The size of the gap between entries in the PLT. */
125 #define PLT_SLOT_SIZE 8
126 /* The number of single-slot PLT entries (the rest use two slots). */
127 #define PLT_NUM_SINGLE_ENTRIES 8192
130 static reloc_howto_type *ppc_elf_howto_table[ (int)R_PPC_max ];
132 static reloc_howto_type ppc_elf_howto_raw[] =
134 /* This reloc does nothing. */
135 HOWTO (R_PPC_NONE, /* type */
136 0, /* rightshift */
137 2, /* size (0 = byte, 1 = short, 2 = long) */
138 32, /* bitsize */
139 false, /* pc_relative */
140 0, /* bitpos */
141 complain_overflow_bitfield, /* complain_on_overflow */
142 bfd_elf_generic_reloc, /* special_function */
143 "R_PPC_NONE", /* name */
144 false, /* partial_inplace */
145 0, /* src_mask */
146 0, /* dst_mask */
147 false), /* pcrel_offset */
149 /* A standard 32 bit relocation. */
150 HOWTO (R_PPC_ADDR32, /* type */
151 0, /* rightshift */
152 2, /* size (0 = byte, 1 = short, 2 = long) */
153 32, /* bitsize */
154 false, /* pc_relative */
155 0, /* bitpos */
156 complain_overflow_bitfield, /* complain_on_overflow */
157 bfd_elf_generic_reloc, /* special_function */
158 "R_PPC_ADDR32", /* name */
159 false, /* partial_inplace */
160 0, /* src_mask */
161 0xffffffff, /* dst_mask */
162 false), /* pcrel_offset */
164 /* An absolute 26 bit branch; the lower two bits must be zero.
165 FIXME: we don't check that, we just clear them. */
166 HOWTO (R_PPC_ADDR24, /* type */
167 0, /* rightshift */
168 2, /* size (0 = byte, 1 = short, 2 = long) */
169 26, /* bitsize */
170 false, /* pc_relative */
171 0, /* bitpos */
172 complain_overflow_bitfield, /* complain_on_overflow */
173 bfd_elf_generic_reloc, /* special_function */
174 "R_PPC_ADDR24", /* name */
175 false, /* partial_inplace */
176 0, /* src_mask */
177 0x3fffffc, /* dst_mask */
178 false), /* pcrel_offset */
180 /* A standard 16 bit relocation. */
181 HOWTO (R_PPC_ADDR16, /* type */
182 0, /* rightshift */
183 1, /* size (0 = byte, 1 = short, 2 = long) */
184 16, /* bitsize */
185 false, /* pc_relative */
186 0, /* bitpos */
187 complain_overflow_bitfield, /* complain_on_overflow */
188 bfd_elf_generic_reloc, /* special_function */
189 "R_PPC_ADDR16", /* name */
190 false, /* partial_inplace */
191 0, /* src_mask */
192 0xffff, /* dst_mask */
193 false), /* pcrel_offset */
195 /* A 16 bit relocation without overflow. */
196 HOWTO (R_PPC_ADDR16_LO, /* type */
197 0, /* rightshift */
198 1, /* size (0 = byte, 1 = short, 2 = long) */
199 16, /* bitsize */
200 false, /* pc_relative */
201 0, /* bitpos */
202 complain_overflow_dont,/* complain_on_overflow */
203 bfd_elf_generic_reloc, /* special_function */
204 "R_PPC_ADDR16_LO", /* name */
205 false, /* partial_inplace */
206 0, /* src_mask */
207 0xffff, /* dst_mask */
208 false), /* pcrel_offset */
210 /* The high order 16 bits of an address. */
211 HOWTO (R_PPC_ADDR16_HI, /* type */
212 16, /* rightshift */
213 1, /* size (0 = byte, 1 = short, 2 = long) */
214 16, /* bitsize */
215 false, /* pc_relative */
216 0, /* bitpos */
217 complain_overflow_dont, /* complain_on_overflow */
218 bfd_elf_generic_reloc, /* special_function */
219 "R_PPC_ADDR16_HI", /* name */
220 false, /* partial_inplace */
221 0, /* src_mask */
222 0xffff, /* dst_mask */
223 false), /* pcrel_offset */
225 /* The high order 16 bits of an address, plus 1 if the contents of
226 the low 16 bits, treated as a signed number, is negative. */
227 HOWTO (R_PPC_ADDR16_HA, /* type */
228 16, /* rightshift */
229 1, /* size (0 = byte, 1 = short, 2 = long) */
230 16, /* bitsize */
231 false, /* pc_relative */
232 0, /* bitpos */
233 complain_overflow_dont, /* complain_on_overflow */
234 ppc_elf_addr16_ha_reloc, /* special_function */
235 "R_PPC_ADDR16_HA", /* name */
236 false, /* partial_inplace */
237 0, /* src_mask */
238 0xffff, /* dst_mask */
239 false), /* pcrel_offset */
241 /* An absolute 16 bit branch; the lower two bits must be zero.
242 FIXME: we don't check that, we just clear them. */
243 HOWTO (R_PPC_ADDR14, /* type */
244 0, /* rightshift */
245 2, /* size (0 = byte, 1 = short, 2 = long) */
246 16, /* bitsize */
247 false, /* pc_relative */
248 0, /* bitpos */
249 complain_overflow_bitfield, /* complain_on_overflow */
250 bfd_elf_generic_reloc, /* special_function */
251 "R_PPC_ADDR14", /* name */
252 false, /* partial_inplace */
253 0, /* src_mask */
254 0xfffc, /* dst_mask */
255 false), /* pcrel_offset */
257 /* An absolute 16 bit branch, for which bit 10 should be set to
258 indicate that the branch is expected to be taken. The lower two
259 bits must be zero. */
260 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
261 0, /* rightshift */
262 2, /* size (0 = byte, 1 = short, 2 = long) */
263 16, /* bitsize */
264 false, /* pc_relative */
265 0, /* bitpos */
266 complain_overflow_bitfield, /* complain_on_overflow */
267 bfd_elf_generic_reloc, /* special_function */
268 "R_PPC_ADDR14_BRTAKEN",/* name */
269 false, /* partial_inplace */
270 0, /* src_mask */
271 0xfffc, /* dst_mask */
272 false), /* pcrel_offset */
274 /* An absolute 16 bit branch, for which bit 10 should be set to
275 indicate that the branch is not expected to be taken. The lower
276 two bits must be zero. */
277 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
278 0, /* rightshift */
279 2, /* size (0 = byte, 1 = short, 2 = long) */
280 16, /* bitsize */
281 false, /* pc_relative */
282 0, /* bitpos */
283 complain_overflow_bitfield, /* complain_on_overflow */
284 bfd_elf_generic_reloc, /* special_function */
285 "R_PPC_ADDR14_BRNTAKEN",/* name */
286 false, /* partial_inplace */
287 0, /* src_mask */
288 0xfffc, /* dst_mask */
289 false), /* pcrel_offset */
291 /* A relative 26 bit branch; the lower two bits must be zero. */
292 HOWTO (R_PPC_REL24, /* type */
293 0, /* rightshift */
294 2, /* size (0 = byte, 1 = short, 2 = long) */
295 26, /* bitsize */
296 true, /* pc_relative */
297 0, /* bitpos */
298 complain_overflow_signed, /* complain_on_overflow */
299 bfd_elf_generic_reloc, /* special_function */
300 "R_PPC_REL24", /* name */
301 false, /* partial_inplace */
302 0, /* src_mask */
303 0x3fffffc, /* dst_mask */
304 true), /* pcrel_offset */
306 /* A relative 16 bit branch; the lower two bits must be zero. */
307 HOWTO (R_PPC_REL14, /* type */
308 0, /* rightshift */
309 2, /* size (0 = byte, 1 = short, 2 = long) */
310 16, /* bitsize */
311 true, /* pc_relative */
312 0, /* bitpos */
313 complain_overflow_signed, /* complain_on_overflow */
314 bfd_elf_generic_reloc, /* special_function */
315 "R_PPC_REL14", /* name */
316 false, /* partial_inplace */
317 0, /* src_mask */
318 0xfffc, /* dst_mask */
319 true), /* pcrel_offset */
321 /* A relative 16 bit branch. Bit 10 should be set to indicate that
322 the branch is expected to be taken. The lower two bits must be
323 zero. */
324 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
325 0, /* rightshift */
326 2, /* size (0 = byte, 1 = short, 2 = long) */
327 16, /* bitsize */
328 true, /* pc_relative */
329 0, /* bitpos */
330 complain_overflow_signed, /* complain_on_overflow */
331 bfd_elf_generic_reloc, /* special_function */
332 "R_PPC_REL14_BRTAKEN", /* name */
333 false, /* partial_inplace */
334 0, /* src_mask */
335 0xfffc, /* dst_mask */
336 true), /* pcrel_offset */
338 /* A relative 16 bit branch. Bit 10 should be set to indicate that
339 the branch is not expected to be taken. The lower two bits must
340 be zero. */
341 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
342 0, /* rightshift */
343 2, /* size (0 = byte, 1 = short, 2 = long) */
344 16, /* bitsize */
345 true, /* pc_relative */
346 0, /* bitpos */
347 complain_overflow_signed, /* complain_on_overflow */
348 bfd_elf_generic_reloc, /* special_function */
349 "R_PPC_REL14_BRNTAKEN",/* name */
350 false, /* partial_inplace */
351 0, /* src_mask */
352 0xfffc, /* dst_mask */
353 true), /* pcrel_offset */
355 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
356 symbol. */
357 HOWTO (R_PPC_GOT16, /* type */
358 0, /* rightshift */
359 1, /* size (0 = byte, 1 = short, 2 = long) */
360 16, /* bitsize */
361 false, /* pc_relative */
362 0, /* bitpos */
363 complain_overflow_signed, /* complain_on_overflow */
364 bfd_elf_generic_reloc, /* special_function */
365 "R_PPC_GOT16", /* name */
366 false, /* partial_inplace */
367 0, /* src_mask */
368 0xffff, /* dst_mask */
369 false), /* pcrel_offset */
371 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
372 the symbol. */
373 HOWTO (R_PPC_GOT16_LO, /* type */
374 0, /* rightshift */
375 1, /* size (0 = byte, 1 = short, 2 = long) */
376 16, /* bitsize */
377 false, /* pc_relative */
378 0, /* bitpos */
379 complain_overflow_dont, /* complain_on_overflow */
380 bfd_elf_generic_reloc, /* special_function */
381 "R_PPC_GOT16_LO", /* name */
382 false, /* partial_inplace */
383 0, /* src_mask */
384 0xffff, /* dst_mask */
385 false), /* pcrel_offset */
387 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
388 the symbol. */
389 HOWTO (R_PPC_GOT16_HI, /* type */
390 16, /* rightshift */
391 1, /* size (0 = byte, 1 = short, 2 = long) */
392 16, /* bitsize */
393 false, /* pc_relative */
394 0, /* bitpos */
395 complain_overflow_bitfield, /* complain_on_overflow */
396 bfd_elf_generic_reloc, /* special_function */
397 "R_PPC_GOT16_HI", /* name */
398 false, /* partial_inplace */
399 0, /* src_mask */
400 0xffff, /* dst_mask */
401 false), /* pcrel_offset */
403 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
404 the symbol. */
405 HOWTO (R_PPC_GOT16_HA, /* type */
406 16, /* rightshift */
407 1, /* size (0 = byte, 1 = short, 2 = long) */
408 16, /* bitsize */
409 false, /* pc_relative */
410 0, /* bitpos */
411 complain_overflow_bitfield, /* complain_on_overflow */
412 ppc_elf_addr16_ha_reloc, /* special_function */
413 "R_PPC_GOT16_HA", /* name */
414 false, /* partial_inplace */
415 0, /* src_mask */
416 0xffff, /* dst_mask */
417 false), /* pcrel_offset */
419 /* Like R_PPC_REL24, but referring to the procedure linkage table
420 entry for the symbol. */
421 HOWTO (R_PPC_PLTREL24, /* type */
422 0, /* rightshift */
423 2, /* size (0 = byte, 1 = short, 2 = long) */
424 26, /* bitsize */
425 true, /* pc_relative */
426 0, /* bitpos */
427 complain_overflow_signed, /* complain_on_overflow */
428 bfd_elf_generic_reloc, /* special_function */
429 "R_PPC_PLTREL24", /* name */
430 false, /* partial_inplace */
431 0, /* src_mask */
432 0x3fffffc, /* dst_mask */
433 true), /* pcrel_offset */
435 /* This is used only by the dynamic linker. The symbol should exist
436 both in the object being run and in some shared library. The
437 dynamic linker copies the data addressed by the symbol from the
438 shared library into the object, because the object being
439 run has to have the data at some particular address. */
440 HOWTO (R_PPC_COPY, /* type */
441 0, /* rightshift */
442 2, /* size (0 = byte, 1 = short, 2 = long) */
443 32, /* bitsize */
444 false, /* pc_relative */
445 0, /* bitpos */
446 complain_overflow_bitfield, /* complain_on_overflow */
447 bfd_elf_generic_reloc, /* special_function */
448 "R_PPC_COPY", /* name */
449 false, /* partial_inplace */
450 0, /* src_mask */
451 0, /* dst_mask */
452 false), /* pcrel_offset */
454 /* Like R_PPC_ADDR32, but used when setting global offset table
455 entries. */
456 HOWTO (R_PPC_GLOB_DAT, /* type */
457 0, /* rightshift */
458 2, /* size (0 = byte, 1 = short, 2 = long) */
459 32, /* bitsize */
460 false, /* pc_relative */
461 0, /* bitpos */
462 complain_overflow_bitfield, /* complain_on_overflow */
463 bfd_elf_generic_reloc, /* special_function */
464 "R_PPC_GLOB_DAT", /* name */
465 false, /* partial_inplace */
466 0, /* src_mask */
467 0xffffffff, /* dst_mask */
468 false), /* pcrel_offset */
470 /* Marks a procedure linkage table entry for a symbol. */
471 HOWTO (R_PPC_JMP_SLOT, /* type */
472 0, /* rightshift */
473 2, /* size (0 = byte, 1 = short, 2 = long) */
474 32, /* bitsize */
475 false, /* pc_relative */
476 0, /* bitpos */
477 complain_overflow_bitfield, /* complain_on_overflow */
478 bfd_elf_generic_reloc, /* special_function */
479 "R_PPC_JMP_SLOT", /* name */
480 false, /* partial_inplace */
481 0, /* src_mask */
482 0, /* dst_mask */
483 false), /* pcrel_offset */
485 /* Used only by the dynamic linker. When the object is run, this
486 longword is set to the load address of the object, plus the
487 addend. */
488 HOWTO (R_PPC_RELATIVE, /* type */
489 0, /* rightshift */
490 2, /* size (0 = byte, 1 = short, 2 = long) */
491 32, /* bitsize */
492 false, /* pc_relative */
493 0, /* bitpos */
494 complain_overflow_bitfield, /* complain_on_overflow */
495 bfd_elf_generic_reloc, /* special_function */
496 "R_PPC_RELATIVE", /* name */
497 false, /* partial_inplace */
498 0, /* src_mask */
499 0xffffffff, /* dst_mask */
500 false), /* pcrel_offset */
502 /* Like R_PPC_REL24, but uses the value of the symbol within the
503 object rather than the final value. Normally used for
504 _GLOBAL_OFFSET_TABLE_. */
505 HOWTO (R_PPC_LOCAL24PC, /* type */
506 0, /* rightshift */
507 2, /* size (0 = byte, 1 = short, 2 = long) */
508 26, /* bitsize */
509 true, /* pc_relative */
510 0, /* bitpos */
511 complain_overflow_signed, /* complain_on_overflow */
512 bfd_elf_generic_reloc, /* special_function */
513 "R_PPC_LOCAL24PC", /* name */
514 false, /* partial_inplace */
515 0, /* src_mask */
516 0x3fffffc, /* dst_mask */
517 true), /* pcrel_offset */
519 /* Like R_PPC_ADDR32, but may be unaligned. */
520 HOWTO (R_PPC_UADDR32, /* type */
521 0, /* rightshift */
522 2, /* size (0 = byte, 1 = short, 2 = long) */
523 32, /* bitsize */
524 false, /* pc_relative */
525 0, /* bitpos */
526 complain_overflow_bitfield, /* complain_on_overflow */
527 bfd_elf_generic_reloc, /* special_function */
528 "R_PPC_UADDR32", /* name */
529 false, /* partial_inplace */
530 0, /* src_mask */
531 0xffffffff, /* dst_mask */
532 false), /* pcrel_offset */
534 /* Like R_PPC_ADDR16, but may be unaligned. */
535 HOWTO (R_PPC_UADDR16, /* type */
536 0, /* rightshift */
537 1, /* size (0 = byte, 1 = short, 2 = long) */
538 16, /* bitsize */
539 false, /* pc_relative */
540 0, /* bitpos */
541 complain_overflow_bitfield, /* complain_on_overflow */
542 bfd_elf_generic_reloc, /* special_function */
543 "R_PPC_UADDR16", /* name */
544 false, /* partial_inplace */
545 0, /* src_mask */
546 0xffff, /* dst_mask */
547 false), /* pcrel_offset */
549 /* 32-bit PC relative */
550 HOWTO (R_PPC_REL32, /* type */
551 0, /* rightshift */
552 2, /* size (0 = byte, 1 = short, 2 = long) */
553 32, /* bitsize */
554 true, /* pc_relative */
555 0, /* bitpos */
556 complain_overflow_bitfield, /* complain_on_overflow */
557 bfd_elf_generic_reloc, /* special_function */
558 "R_PPC_REL32", /* name */
559 false, /* partial_inplace */
560 0, /* src_mask */
561 0xffffffff, /* dst_mask */
562 true), /* pcrel_offset */
564 /* 32-bit relocation to the symbol's procedure linkage table.
565 FIXME: not supported. */
566 HOWTO (R_PPC_PLT32, /* type */
567 0, /* rightshift */
568 2, /* size (0 = byte, 1 = short, 2 = long) */
569 32, /* bitsize */
570 false, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_bitfield, /* complain_on_overflow */
573 bfd_elf_generic_reloc, /* special_function */
574 "R_PPC_PLT32", /* name */
575 false, /* partial_inplace */
576 0, /* src_mask */
577 0, /* dst_mask */
578 false), /* pcrel_offset */
580 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
581 FIXME: not supported. */
582 HOWTO (R_PPC_PLTREL32, /* type */
583 0, /* rightshift */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
585 32, /* bitsize */
586 true, /* pc_relative */
587 0, /* bitpos */
588 complain_overflow_bitfield, /* complain_on_overflow */
589 bfd_elf_generic_reloc, /* special_function */
590 "R_PPC_PLTREL32", /* name */
591 false, /* partial_inplace */
592 0, /* src_mask */
593 0, /* dst_mask */
594 true), /* pcrel_offset */
596 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
597 the symbol. */
598 HOWTO (R_PPC_PLT16_LO, /* type */
599 0, /* rightshift */
600 1, /* size (0 = byte, 1 = short, 2 = long) */
601 16, /* bitsize */
602 false, /* pc_relative */
603 0, /* bitpos */
604 complain_overflow_dont, /* complain_on_overflow */
605 bfd_elf_generic_reloc, /* special_function */
606 "R_PPC_PLT16_LO", /* name */
607 false, /* partial_inplace */
608 0, /* src_mask */
609 0xffff, /* dst_mask */
610 false), /* pcrel_offset */
612 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
613 the symbol. */
614 HOWTO (R_PPC_PLT16_HI, /* type */
615 16, /* rightshift */
616 1, /* size (0 = byte, 1 = short, 2 = long) */
617 16, /* bitsize */
618 false, /* pc_relative */
619 0, /* bitpos */
620 complain_overflow_bitfield, /* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_PPC_PLT16_HI", /* name */
623 false, /* partial_inplace */
624 0, /* src_mask */
625 0xffff, /* dst_mask */
626 false), /* pcrel_offset */
628 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
629 the symbol. */
630 HOWTO (R_PPC_PLT16_HA, /* type */
631 16, /* rightshift */
632 1, /* size (0 = byte, 1 = short, 2 = long) */
633 16, /* bitsize */
634 false, /* pc_relative */
635 0, /* bitpos */
636 complain_overflow_bitfield, /* complain_on_overflow */
637 ppc_elf_addr16_ha_reloc, /* special_function */
638 "R_PPC_PLT16_HA", /* name */
639 false, /* partial_inplace */
640 0, /* src_mask */
641 0xffff, /* dst_mask */
642 false), /* pcrel_offset */
644 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
645 small data items. */
646 HOWTO (R_PPC_SDAREL16, /* type */
647 0, /* rightshift */
648 1, /* size (0 = byte, 1 = short, 2 = long) */
649 16, /* bitsize */
650 false, /* pc_relative */
651 0, /* bitpos */
652 complain_overflow_signed, /* complain_on_overflow */
653 bfd_elf_generic_reloc, /* special_function */
654 "R_PPC_SDAREL16", /* name */
655 false, /* partial_inplace */
656 0, /* src_mask */
657 0xffff, /* dst_mask */
658 false), /* pcrel_offset */
660 /* 32-bit section relative relocation. */
661 HOWTO (R_PPC_SECTOFF, /* type */
662 0, /* rightshift */
663 2, /* size (0 = byte, 1 = short, 2 = long) */
664 32, /* bitsize */
665 true, /* pc_relative */
666 0, /* bitpos */
667 complain_overflow_bitfield, /* complain_on_overflow */
668 bfd_elf_generic_reloc, /* special_function */
669 "R_PPC_SECTOFF", /* name */
670 false, /* partial_inplace */
671 0, /* src_mask */
672 0, /* dst_mask */
673 true), /* pcrel_offset */
675 /* 16-bit lower half section relative relocation. */
676 HOWTO (R_PPC_SECTOFF_LO, /* type */
677 0, /* rightshift */
678 1, /* size (0 = byte, 1 = short, 2 = long) */
679 16, /* bitsize */
680 false, /* pc_relative */
681 0, /* bitpos */
682 complain_overflow_dont, /* complain_on_overflow */
683 bfd_elf_generic_reloc, /* special_function */
684 "R_PPC_SECTOFF_LO", /* name */
685 false, /* partial_inplace */
686 0, /* src_mask */
687 0xffff, /* dst_mask */
688 false), /* pcrel_offset */
690 /* 16-bit upper half section relative relocation. */
691 HOWTO (R_PPC_SECTOFF_HI, /* type */
692 16, /* rightshift */
693 1, /* size (0 = byte, 1 = short, 2 = long) */
694 16, /* bitsize */
695 false, /* pc_relative */
696 0, /* bitpos */
697 complain_overflow_bitfield, /* complain_on_overflow */
698 bfd_elf_generic_reloc, /* special_function */
699 "R_PPC_SECTOFF_HI", /* name */
700 false, /* partial_inplace */
701 0, /* src_mask */
702 0xffff, /* dst_mask */
703 false), /* pcrel_offset */
705 /* 16-bit upper half adjusted section relative relocation. */
706 HOWTO (R_PPC_SECTOFF_HA, /* type */
707 16, /* rightshift */
708 1, /* size (0 = byte, 1 = short, 2 = long) */
709 16, /* bitsize */
710 false, /* pc_relative */
711 0, /* bitpos */
712 complain_overflow_bitfield, /* complain_on_overflow */
713 ppc_elf_addr16_ha_reloc, /* special_function */
714 "R_PPC_SECTOFF_HA", /* name */
715 false, /* partial_inplace */
716 0, /* src_mask */
717 0xffff, /* dst_mask */
718 false), /* pcrel_offset */
720 /* The remaining relocs are from the Embedded ELF ABI, and are not
721 in the SVR4 ELF ABI. */
723 /* 32 bit value resulting from the addend minus the symbol */
724 HOWTO (R_PPC_EMB_NADDR32, /* type */
725 0, /* rightshift */
726 2, /* size (0 = byte, 1 = short, 2 = long) */
727 32, /* bitsize */
728 false, /* pc_relative */
729 0, /* bitpos */
730 complain_overflow_bitfield, /* complain_on_overflow */
731 bfd_elf_generic_reloc, /* special_function */
732 "R_PPC_EMB_NADDR32", /* name */
733 false, /* partial_inplace */
734 0, /* src_mask */
735 0xffffffff, /* dst_mask */
736 false), /* pcrel_offset */
738 /* 16 bit value resulting from the addend minus the symbol */
739 HOWTO (R_PPC_EMB_NADDR16, /* type */
740 0, /* rightshift */
741 1, /* size (0 = byte, 1 = short, 2 = long) */
742 16, /* bitsize */
743 false, /* pc_relative */
744 0, /* bitpos */
745 complain_overflow_bitfield, /* complain_on_overflow */
746 bfd_elf_generic_reloc, /* special_function */
747 "R_PPC_EMB_NADDR16", /* name */
748 false, /* partial_inplace */
749 0, /* src_mask */
750 0xffff, /* dst_mask */
751 false), /* pcrel_offset */
753 /* 16 bit value resulting from the addend minus the symbol */
754 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
755 0, /* rightshift */
756 1, /* size (0 = byte, 1 = short, 2 = long) */
757 16, /* bitsize */
758 false, /* pc_relative */
759 0, /* bitpos */
760 complain_overflow_dont,/* complain_on_overflow */
761 bfd_elf_generic_reloc, /* special_function */
762 "R_PPC_EMB_ADDR16_LO", /* name */
763 false, /* partial_inplace */
764 0, /* src_mask */
765 0xffff, /* dst_mask */
766 false), /* pcrel_offset */
768 /* The high order 16 bits of the addend minus the symbol */
769 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
770 16, /* rightshift */
771 1, /* size (0 = byte, 1 = short, 2 = long) */
772 16, /* bitsize */
773 false, /* pc_relative */
774 0, /* bitpos */
775 complain_overflow_dont, /* complain_on_overflow */
776 bfd_elf_generic_reloc, /* special_function */
777 "R_PPC_EMB_NADDR16_HI", /* name */
778 false, /* partial_inplace */
779 0, /* src_mask */
780 0xffff, /* dst_mask */
781 false), /* pcrel_offset */
783 /* The high order 16 bits of the result of the addend minus the address,
784 plus 1 if the contents of the low 16 bits, treated as a signed number,
785 is negative. */
786 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
787 16, /* rightshift */
788 1, /* size (0 = byte, 1 = short, 2 = long) */
789 16, /* bitsize */
790 false, /* pc_relative */
791 0, /* bitpos */
792 complain_overflow_dont, /* complain_on_overflow */
793 ppc_elf_addr16_ha_reloc, /* special_function */
794 "R_PPC_EMB_NADDR16_HA", /* name */
795 false, /* partial_inplace */
796 0, /* src_mask */
797 0xffff, /* dst_mask */
798 false), /* pcrel_offset */
800 /* 16 bit value resulting from allocating a 4 byte word to hold an
801 address in the .sdata section, and returning the offset from
802 _SDA_BASE_ for that relocation */
803 HOWTO (R_PPC_EMB_SDAI16, /* type */
804 0, /* rightshift */
805 1, /* size (0 = byte, 1 = short, 2 = long) */
806 16, /* bitsize */
807 false, /* pc_relative */
808 0, /* bitpos */
809 complain_overflow_bitfield, /* complain_on_overflow */
810 bfd_elf_generic_reloc, /* special_function */
811 "R_PPC_EMB_SDAI16", /* name */
812 false, /* partial_inplace */
813 0, /* src_mask */
814 0xffff, /* dst_mask */
815 false), /* pcrel_offset */
817 /* 16 bit value resulting from allocating a 4 byte word to hold an
818 address in the .sdata2 section, and returning the offset from
819 _SDA2_BASE_ for that relocation */
820 HOWTO (R_PPC_EMB_SDA2I16, /* type */
821 0, /* rightshift */
822 1, /* size (0 = byte, 1 = short, 2 = long) */
823 16, /* bitsize */
824 false, /* pc_relative */
825 0, /* bitpos */
826 complain_overflow_bitfield, /* complain_on_overflow */
827 bfd_elf_generic_reloc, /* special_function */
828 "R_PPC_EMB_SDA2I16", /* name */
829 false, /* partial_inplace */
830 0, /* src_mask */
831 0xffff, /* dst_mask */
832 false), /* pcrel_offset */
834 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
835 small data items. */
836 HOWTO (R_PPC_EMB_SDA2REL, /* type */
837 0, /* rightshift */
838 1, /* size (0 = byte, 1 = short, 2 = long) */
839 16, /* bitsize */
840 false, /* pc_relative */
841 0, /* bitpos */
842 complain_overflow_signed, /* complain_on_overflow */
843 bfd_elf_generic_reloc, /* special_function */
844 "R_PPC_EMB_SDA2REL", /* name */
845 false, /* partial_inplace */
846 0, /* src_mask */
847 0xffff, /* dst_mask */
848 false), /* pcrel_offset */
850 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
851 signed offset from the appropriate base, and filling in the register
852 field with the appropriate register (0, 2, or 13). */
853 HOWTO (R_PPC_EMB_SDA21, /* type */
854 0, /* rightshift */
855 2, /* size (0 = byte, 1 = short, 2 = long) */
856 16, /* bitsize */
857 false, /* pc_relative */
858 0, /* bitpos */
859 complain_overflow_signed, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 "R_PPC_EMB_SDA21", /* name */
862 false, /* partial_inplace */
863 0, /* src_mask */
864 0xffff, /* dst_mask */
865 false), /* pcrel_offset */
867 /* Relocation not handled: R_PPC_EMB_MRKREF */
868 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
869 /* Relocation not handled: R_PPC_EMB_RELST_LO */
870 /* Relocation not handled: R_PPC_EMB_RELST_HI */
871 /* Relocation not handled: R_PPC_EMB_RELST_HA */
872 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
874 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
875 in the 16 bit signed offset from the appropriate base, and filling in the
876 register field with the appropriate register (0, 2, or 13). */
877 HOWTO (R_PPC_EMB_RELSDA, /* type */
878 0, /* rightshift */
879 1, /* size (0 = byte, 1 = short, 2 = long) */
880 16, /* bitsize */
881 true, /* pc_relative */
882 0, /* bitpos */
883 complain_overflow_signed, /* complain_on_overflow */
884 bfd_elf_generic_reloc, /* special_function */
885 "R_PPC_EMB_RELSDA", /* name */
886 false, /* partial_inplace */
887 0, /* src_mask */
888 0xffff, /* dst_mask */
889 false), /* pcrel_offset */
891 /* GNU extension to record C++ vtable hierarchy */
892 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
893 0, /* rightshift */
894 0, /* size (0 = byte, 1 = short, 2 = long) */
895 0, /* bitsize */
896 false, /* pc_relative */
897 0, /* bitpos */
898 complain_overflow_dont, /* complain_on_overflow */
899 NULL, /* special_function */
900 "R_PPC_GNU_VTINHERIT", /* name */
901 false, /* partial_inplace */
902 0, /* src_mask */
903 0, /* dst_mask */
904 false), /* pcrel_offset */
906 /* GNU extension to record C++ vtable member usage */
907 HOWTO (R_PPC_GNU_VTENTRY, /* type */
908 0, /* rightshift */
909 0, /* size (0 = byte, 1 = short, 2 = long) */
910 0, /* bitsize */
911 false, /* pc_relative */
912 0, /* bitpos */
913 complain_overflow_dont, /* complain_on_overflow */
914 NULL, /* special_function */
915 "R_PPC_GNU_VTENTRY", /* name */
916 false, /* partial_inplace */
917 0, /* src_mask */
918 0, /* dst_mask */
919 false), /* pcrel_offset */
921 /* Phony reloc to handle AIX style TOC entries */
922 HOWTO (R_PPC_TOC16, /* type */
923 0, /* rightshift */
924 1, /* size (0 = byte, 1 = short, 2 = long) */
925 16, /* bitsize */
926 false, /* pc_relative */
927 0, /* bitpos */
928 complain_overflow_signed, /* complain_on_overflow */
929 bfd_elf_generic_reloc, /* special_function */
930 "R_PPC_TOC16", /* name */
931 false, /* partial_inplace */
932 0, /* src_mask */
933 0xffff, /* dst_mask */
934 false), /* pcrel_offset */
938 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
940 static void
941 ppc_elf_howto_init ()
943 unsigned int i, type;
945 for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
947 type = ppc_elf_howto_raw[i].type;
948 BFD_ASSERT (type < sizeof(ppc_elf_howto_table) / sizeof(ppc_elf_howto_table[0]));
949 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
954 /* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
956 The MPC860, revision C0 or earlier contains a bug in the die.
957 If all of the following conditions are true, the next instruction
958 to be executed *may* be treated as a no-op.
959 1/ A forward branch is executed.
960 2/ The branch is predicted as not taken.
961 3/ The branch is taken.
962 4/ The branch is located in the last 5 words of a page.
963 (The EOP limit is 5 by default but may be specified as any value from 1-10.)
965 Our software solution is to detect these problematic branches in a
966 linker pass and modify them as follows:
967 1/ Unconditional branches - Since these are always predicted taken,
968 there is no problem and no action is required.
969 2/ Conditional backward branches - No problem, no action required.
970 3/ Conditional forward branches - Ensure that the "inverse prediction
971 bit" is set (ensure it is predicted taken).
972 4/ Conditional register branches - Ensure that the "y bit" is set
973 (ensure it is predicted taken).
976 /* Sort sections by address. */
978 static int
979 ppc_elf_sort_rela (arg1, arg2)
980 const void *arg1;
981 const void *arg2;
983 const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
984 const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
986 /* Sort by offset. */
987 return ((*rela1)->r_offset - (*rela2)->r_offset);
990 static boolean
991 ppc_elf_relax_section (abfd, isec, link_info, again)
992 bfd *abfd;
993 asection *isec;
994 struct bfd_link_info *link_info;
995 boolean *again;
997 #define PAGESIZE 0x1000
999 bfd_byte *contents = NULL;
1000 bfd_byte *free_contents = NULL;
1001 Elf_Internal_Rela *internal_relocs = NULL;
1002 Elf_Internal_Rela *free_relocs = NULL;
1003 Elf_Internal_Rela **rela_comb = NULL;
1004 int comb_curr, comb_count;
1006 /* We never have to do this more than once per input section. */
1007 *again = false;
1009 /* If needed, initialize this section's cooked size. */
1010 if (isec->_cooked_size == 0)
1011 isec->_cooked_size = isec->_raw_size;
1013 /* We're only interested in text sections which overlap the
1014 troublesome area at the end of a page. */
1015 if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1017 bfd_vma dot, end_page, end_section;
1018 boolean section_modified;
1020 /* Get the section contents. */
1021 /* Get cached copy if it exists. */
1022 if (elf_section_data (isec)->this_hdr.contents != NULL)
1023 contents = elf_section_data (isec)->this_hdr.contents;
1024 else
1026 /* Go get them off disk. */
1027 contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
1028 if (contents == NULL)
1029 goto error_return;
1030 free_contents = contents;
1032 if (! bfd_get_section_contents (abfd, isec, contents,
1033 (file_ptr) 0, isec->_raw_size))
1034 goto error_return;
1037 comb_curr = 0;
1038 comb_count = 0;
1039 if (isec->reloc_count)
1041 unsigned n;
1043 /* Get a copy of the native relocations. */
1044 internal_relocs = _bfd_elf32_link_read_relocs (
1045 abfd, isec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1046 link_info->keep_memory);
1047 if (internal_relocs == NULL)
1048 goto error_return;
1049 if (! link_info->keep_memory)
1050 free_relocs = internal_relocs;
1052 /* Setup a faster access method for the reloc info we need. */
1053 rela_comb = (Elf_Internal_Rela**)
1054 bfd_malloc (isec->reloc_count*sizeof(Elf_Internal_Rela*));
1055 if (rela_comb == NULL)
1056 goto error_return;
1057 for (n=0; n<isec->reloc_count; ++n)
1059 long r_type;
1061 r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
1062 if (r_type < 0 || r_type >= (int)R_PPC_max)
1063 goto error_return;
1065 /* Prologue constants are sometimes present in the ".text"
1066 sections and they can be identified by their associated relocation.
1067 We don't want to process those words and some others which
1068 can also be identified by their relocations. However, not all
1069 conditional branches will have a relocation so we will
1070 only ignore words that 1) have a reloc, and 2) the reloc
1071 is not applicable to a conditional branch.
1072 The array rela_comb is built here for use in the EOP scan loop. */
1073 switch (r_type)
1075 case R_PPC_ADDR14_BRNTAKEN: /* absolute, predicted not taken */
1076 case R_PPC_REL14: /* relative cond. br. */
1077 case R_PPC_REL14_BRNTAKEN: /* rel. cond. br., predicted not taken */
1078 /* We should check the instruction. */
1079 break;
1080 default:
1081 /* The word is not a conditional branch - ignore it. */
1082 rela_comb[comb_count++] = &internal_relocs[n];
1083 break;
1086 if (comb_count > 1)
1087 qsort (rela_comb, (size_t) comb_count, sizeof (int), ppc_elf_sort_rela);
1090 /* Enumerate each EOP region that overlaps this section. */
1091 end_section = isec->vma + isec->_cooked_size;
1092 dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1093 dot -= link_info->mpc860c0;
1094 section_modified = false;
1095 if (dot < isec->vma) /* Increment the start position if this section */
1096 dot = isec->vma; /* begins in the middle of its first EOP region. */
1097 for (;
1098 dot < end_section;
1099 dot += PAGESIZE, end_page += PAGESIZE)
1102 /* Check each word in this EOP region. */
1103 for (; dot < end_page; dot += 4)
1105 bfd_vma isec_offset;
1106 unsigned long insn;
1107 boolean skip, modified;
1109 /* Don't process this word if there is a relocation for it and
1110 the relocation indicates the word is not a conditional branch. */
1111 skip = false;
1112 isec_offset = dot - isec->vma;
1113 for (; comb_curr<comb_count; ++comb_curr)
1115 bfd_vma r_offset;
1117 r_offset = rela_comb[comb_curr]->r_offset;
1118 if (r_offset >= isec_offset)
1120 if (r_offset == isec_offset) skip = true;
1121 break;
1124 if (skip) continue;
1126 /* Check the current word for a problematic conditional branch. */
1127 #define BO0(insn) ((insn) & 0x02000000)
1128 #define BO2(insn) ((insn) & 0x00800000)
1129 #define BO4(insn) ((insn) & 0x00200000)
1130 insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1131 modified = false;
1132 if ((insn & 0xFc000000) == 0x40000000)
1134 /* Instruction is BCx */
1135 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1137 bfd_vma target;
1138 /* This branch is predicted as "normal".
1139 If this is a forward branch, it is problematic. */
1141 target = insn & 0x0000Fffc; /*extract*/
1142 target = (target ^ 0x8000) - 0x8000; /*sign extend*/
1143 if ((insn & 0x00000002) == 0)
1144 target += dot; /*convert to abs*/
1145 if (target > dot)
1147 insn |= 0x00200000; /* set the prediction bit */
1148 modified = true;
1152 else if ((insn & 0xFc00Fffe) == 0x4c000420)
1154 /* Instruction is BCCTRx */
1155 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1157 /* This branch is predicted as not-taken.
1158 If this is a forward branch, it is problematic.
1159 Since we can't tell statically if it will branch forward,
1160 always set the prediction bit. */
1161 insn |= 0x00200000; /* set the prediction bit */
1162 modified = true;
1165 else if ((insn & 0xFc00Fffe) == 0x4c000020)
1167 /* Instruction is BCLRx */
1168 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1170 /* This branch is predicted as not-taken.
1171 If this is a forward branch, it is problematic.
1172 Since we can't tell statically if it will branch forward,
1173 always set the prediction bit. */
1174 insn |= 0x00200000; /* set the prediction bit */
1175 modified = true;
1178 #undef BO0
1179 #undef BO2
1180 #undef BO4
1181 if (modified)
1183 bfd_put_32 (abfd, insn, contents + isec_offset);
1184 section_modified = true;
1188 if (section_modified)
1190 elf_section_data (isec)->this_hdr.contents = contents;
1191 free_contents = NULL;
1195 if (rela_comb != NULL)
1197 free (rela_comb);
1198 rela_comb = NULL;
1201 if (free_relocs != NULL)
1203 free (free_relocs);
1204 free_relocs = NULL;
1207 if (free_contents != NULL)
1209 if (! link_info->keep_memory)
1210 free (free_contents);
1211 else
1213 /* Cache the section contents for elf_link_input_bfd. */
1214 elf_section_data (isec)->this_hdr.contents = contents;
1216 free_contents = NULL;
1219 return true;
1221 error_return:
1222 if (rela_comb != NULL)
1223 free (rela_comb);
1224 if (free_relocs != NULL)
1225 free (free_relocs);
1226 if (free_contents != NULL)
1227 free (free_contents);
1228 return false;
1232 static reloc_howto_type *
1233 ppc_elf_reloc_type_lookup (abfd, code)
1234 bfd *abfd;
1235 bfd_reloc_code_real_type code;
1237 enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1239 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1240 /* Initialize howto table if needed */
1241 ppc_elf_howto_init ();
1243 switch ((int)code)
1245 default:
1246 return (reloc_howto_type *)NULL;
1248 case BFD_RELOC_NONE: ppc_reloc = R_PPC_NONE; break;
1249 case BFD_RELOC_32: ppc_reloc = R_PPC_ADDR32; break;
1250 case BFD_RELOC_PPC_BA26: ppc_reloc = R_PPC_ADDR24; break;
1251 case BFD_RELOC_16: ppc_reloc = R_PPC_ADDR16; break;
1252 case BFD_RELOC_LO16: ppc_reloc = R_PPC_ADDR16_LO; break;
1253 case BFD_RELOC_HI16: ppc_reloc = R_PPC_ADDR16_HI; break;
1254 case BFD_RELOC_HI16_S: ppc_reloc = R_PPC_ADDR16_HA; break;
1255 case BFD_RELOC_PPC_BA16: ppc_reloc = R_PPC_ADDR14; break;
1256 case BFD_RELOC_PPC_BA16_BRTAKEN: ppc_reloc = R_PPC_ADDR14_BRTAKEN; break;
1257 case BFD_RELOC_PPC_BA16_BRNTAKEN: ppc_reloc = R_PPC_ADDR14_BRNTAKEN; break;
1258 case BFD_RELOC_PPC_B26: ppc_reloc = R_PPC_REL24; break;
1259 case BFD_RELOC_PPC_B16: ppc_reloc = R_PPC_REL14; break;
1260 case BFD_RELOC_PPC_B16_BRTAKEN: ppc_reloc = R_PPC_REL14_BRTAKEN; break;
1261 case BFD_RELOC_PPC_B16_BRNTAKEN: ppc_reloc = R_PPC_REL14_BRNTAKEN; break;
1262 case BFD_RELOC_16_GOTOFF: ppc_reloc = R_PPC_GOT16; break;
1263 case BFD_RELOC_LO16_GOTOFF: ppc_reloc = R_PPC_GOT16_LO; break;
1264 case BFD_RELOC_HI16_GOTOFF: ppc_reloc = R_PPC_GOT16_HI; break;
1265 case BFD_RELOC_HI16_S_GOTOFF: ppc_reloc = R_PPC_GOT16_HA; break;
1266 case BFD_RELOC_24_PLT_PCREL: ppc_reloc = R_PPC_PLTREL24; break;
1267 case BFD_RELOC_PPC_COPY: ppc_reloc = R_PPC_COPY; break;
1268 case BFD_RELOC_PPC_GLOB_DAT: ppc_reloc = R_PPC_GLOB_DAT; break;
1269 case BFD_RELOC_PPC_LOCAL24PC: ppc_reloc = R_PPC_LOCAL24PC; break;
1270 case BFD_RELOC_32_PCREL: ppc_reloc = R_PPC_REL32; break;
1271 case BFD_RELOC_32_PLTOFF: ppc_reloc = R_PPC_PLT32; break;
1272 case BFD_RELOC_32_PLT_PCREL: ppc_reloc = R_PPC_PLTREL32; break;
1273 case BFD_RELOC_LO16_PLTOFF: ppc_reloc = R_PPC_PLT16_LO; break;
1274 case BFD_RELOC_HI16_PLTOFF: ppc_reloc = R_PPC_PLT16_HI; break;
1275 case BFD_RELOC_HI16_S_PLTOFF: ppc_reloc = R_PPC_PLT16_HA; break;
1276 case BFD_RELOC_GPREL16: ppc_reloc = R_PPC_SDAREL16; break;
1277 case BFD_RELOC_32_BASEREL: ppc_reloc = R_PPC_SECTOFF; break;
1278 case BFD_RELOC_LO16_BASEREL: ppc_reloc = R_PPC_SECTOFF_LO; break;
1279 case BFD_RELOC_HI16_BASEREL: ppc_reloc = R_PPC_SECTOFF_HI; break;
1280 case BFD_RELOC_HI16_S_BASEREL: ppc_reloc = R_PPC_SECTOFF_HA; break;
1281 case BFD_RELOC_CTOR: ppc_reloc = R_PPC_ADDR32; break;
1282 case BFD_RELOC_PPC_TOC16: ppc_reloc = R_PPC_TOC16; break;
1283 case BFD_RELOC_PPC_EMB_NADDR32: ppc_reloc = R_PPC_EMB_NADDR32; break;
1284 case BFD_RELOC_PPC_EMB_NADDR16: ppc_reloc = R_PPC_EMB_NADDR16; break;
1285 case BFD_RELOC_PPC_EMB_NADDR16_LO: ppc_reloc = R_PPC_EMB_NADDR16_LO; break;
1286 case BFD_RELOC_PPC_EMB_NADDR16_HI: ppc_reloc = R_PPC_EMB_NADDR16_HI; break;
1287 case BFD_RELOC_PPC_EMB_NADDR16_HA: ppc_reloc = R_PPC_EMB_NADDR16_HA; break;
1288 case BFD_RELOC_PPC_EMB_SDAI16: ppc_reloc = R_PPC_EMB_SDAI16; break;
1289 case BFD_RELOC_PPC_EMB_SDA2I16: ppc_reloc = R_PPC_EMB_SDA2I16; break;
1290 case BFD_RELOC_PPC_EMB_SDA2REL: ppc_reloc = R_PPC_EMB_SDA2REL; break;
1291 case BFD_RELOC_PPC_EMB_SDA21: ppc_reloc = R_PPC_EMB_SDA21; break;
1292 case BFD_RELOC_PPC_EMB_MRKREF: ppc_reloc = R_PPC_EMB_MRKREF; break;
1293 case BFD_RELOC_PPC_EMB_RELSEC16: ppc_reloc = R_PPC_EMB_RELSEC16; break;
1294 case BFD_RELOC_PPC_EMB_RELST_LO: ppc_reloc = R_PPC_EMB_RELST_LO; break;
1295 case BFD_RELOC_PPC_EMB_RELST_HI: ppc_reloc = R_PPC_EMB_RELST_HI; break;
1296 case BFD_RELOC_PPC_EMB_RELST_HA: ppc_reloc = R_PPC_EMB_RELST_HA; break;
1297 case BFD_RELOC_PPC_EMB_BIT_FLD: ppc_reloc = R_PPC_EMB_BIT_FLD; break;
1298 case BFD_RELOC_PPC_EMB_RELSDA: ppc_reloc = R_PPC_EMB_RELSDA; break;
1299 case BFD_RELOC_VTABLE_INHERIT: ppc_reloc = R_PPC_GNU_VTINHERIT; break;
1300 case BFD_RELOC_VTABLE_ENTRY: ppc_reloc = R_PPC_GNU_VTENTRY; break;
1303 return ppc_elf_howto_table[ (int)ppc_reloc ];
1306 /* Set the howto pointer for a PowerPC ELF reloc. */
1308 static void
1309 ppc_elf_info_to_howto (abfd, cache_ptr, dst)
1310 bfd *abfd;
1311 arelent *cache_ptr;
1312 Elf32_Internal_Rela *dst;
1314 if (!ppc_elf_howto_table[ R_PPC_ADDR32 ]) /* Initialize howto table if needed */
1315 ppc_elf_howto_init ();
1317 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1318 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1321 /* Handle the R_PPC_ADDR16_HA reloc. */
1323 static bfd_reloc_status_type
1324 ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
1325 output_bfd, error_message)
1326 bfd *abfd;
1327 arelent *reloc_entry;
1328 asymbol *symbol;
1329 PTR data;
1330 asection *input_section;
1331 bfd *output_bfd;
1332 char **error_message;
1334 bfd_vma relocation;
1336 if (output_bfd != NULL)
1338 reloc_entry->address += input_section->output_offset;
1339 return bfd_reloc_ok;
1342 if (reloc_entry->address > input_section->_cooked_size)
1343 return bfd_reloc_outofrange;
1345 if (bfd_is_com_section (symbol->section))
1346 relocation = 0;
1347 else
1348 relocation = symbol->value;
1350 relocation += symbol->section->output_section->vma;
1351 relocation += symbol->section->output_offset;
1352 relocation += reloc_entry->addend;
1354 reloc_entry->addend += (relocation & 0x8000) << 1;
1356 return bfd_reloc_continue;
1359 /* Function to set whether a module needs the -mrelocatable bit set. */
1361 static boolean
1362 ppc_elf_set_private_flags (abfd, flags)
1363 bfd *abfd;
1364 flagword flags;
1366 BFD_ASSERT (!elf_flags_init (abfd)
1367 || elf_elfheader (abfd)->e_flags == flags);
1369 elf_elfheader (abfd)->e_flags = flags;
1370 elf_flags_init (abfd) = true;
1371 return true;
1374 /* Copy backend specific data from one object module to another */
1375 static boolean
1376 ppc_elf_copy_private_bfd_data (ibfd, obfd)
1377 bfd *ibfd;
1378 bfd *obfd;
1380 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1381 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1382 return true;
1384 BFD_ASSERT (!elf_flags_init (obfd)
1385 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
1387 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1388 elf_flags_init (obfd) = true;
1389 return true;
1392 /* Merge backend specific data from an object file to the output
1393 object file when linking */
1394 static boolean
1395 ppc_elf_merge_private_bfd_data (ibfd, obfd)
1396 bfd *ibfd;
1397 bfd *obfd;
1399 flagword old_flags;
1400 flagword new_flags;
1401 boolean error;
1403 /* Check if we have the same endianess */
1404 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1405 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1407 const char *msg;
1409 if (bfd_big_endian (ibfd))
1410 msg = _("%s: compiled for a big endian system and target is little endian");
1411 else
1412 msg = _("%s: compiled for a little endian system and target is big endian");
1414 (*_bfd_error_handler) (msg, bfd_get_filename (ibfd));
1416 bfd_set_error (bfd_error_wrong_format);
1417 return false;
1420 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1421 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1422 return true;
1424 new_flags = elf_elfheader (ibfd)->e_flags;
1425 old_flags = elf_elfheader (obfd)->e_flags;
1426 if (!elf_flags_init (obfd)) /* First call, no flags set */
1428 elf_flags_init (obfd) = true;
1429 elf_elfheader (obfd)->e_flags = new_flags;
1432 else if (new_flags == old_flags) /* Compatible flags are ok */
1435 else /* Incompatible flags */
1437 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib to be linked
1438 with either. */
1439 error = false;
1440 if ((new_flags & EF_PPC_RELOCATABLE) != 0
1441 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1443 error = true;
1444 (*_bfd_error_handler)
1445 (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
1446 bfd_get_filename (ibfd));
1448 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1449 && (old_flags & EF_PPC_RELOCATABLE) != 0)
1451 error = true;
1452 (*_bfd_error_handler)
1453 (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
1454 bfd_get_filename (ibfd));
1457 /* The output is -mrelocatable-lib iff both the input files are. */
1458 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1459 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1461 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1462 but each input file is either -mrelocatable or -mrelocatable-lib. */
1463 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1464 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1465 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1466 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1468 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it */
1469 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1471 new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1472 old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1474 /* Warn about any other mismatches */
1475 if (new_flags != old_flags)
1477 error = true;
1478 (*_bfd_error_handler)
1479 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1480 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
1483 if (error)
1485 bfd_set_error (bfd_error_bad_value);
1486 return false;
1490 return true;
1494 /* Handle a PowerPC specific section when reading an object file. This
1495 is called when elfcode.h finds a section with an unknown type. */
1497 static boolean
1498 ppc_elf_section_from_shdr (abfd, hdr, name)
1499 bfd *abfd;
1500 Elf32_Internal_Shdr *hdr;
1501 char *name;
1503 asection *newsect;
1504 flagword flags;
1506 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1507 return false;
1509 newsect = hdr->bfd_section;
1510 flags = bfd_get_section_flags (abfd, newsect);
1511 if (hdr->sh_flags & SHF_EXCLUDE)
1512 flags |= SEC_EXCLUDE;
1514 if (hdr->sh_type == SHT_ORDERED)
1515 flags |= SEC_SORT_ENTRIES;
1517 bfd_set_section_flags (abfd, newsect, flags);
1518 return true;
1522 /* Set up any other section flags and such that may be necessary. */
1524 static boolean
1525 ppc_elf_fake_sections (abfd, shdr, asect)
1526 bfd *abfd;
1527 Elf32_Internal_Shdr *shdr;
1528 asection *asect;
1530 if ((asect->flags & SEC_EXCLUDE) != 0)
1531 shdr->sh_flags |= SHF_EXCLUDE;
1533 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1534 shdr->sh_type = SHT_ORDERED;
1536 return true;
1540 /* Create a special linker section */
1541 static elf_linker_section_t *
1542 ppc_elf_create_linker_section (abfd, info, which)
1543 bfd *abfd;
1544 struct bfd_link_info *info;
1545 enum elf_linker_section_enum which;
1547 bfd *dynobj = elf_hash_table (info)->dynobj;
1548 elf_linker_section_t *lsect;
1550 /* Record the first bfd section that needs the special section */
1551 if (!dynobj)
1552 dynobj = elf_hash_table (info)->dynobj = abfd;
1554 /* If this is the first time, create the section */
1555 lsect = elf_linker_section (dynobj, which);
1556 if (!lsect)
1558 elf_linker_section_t defaults;
1559 static elf_linker_section_t zero_section;
1561 defaults = zero_section;
1562 defaults.which = which;
1563 defaults.hole_written_p = false;
1564 defaults.alignment = 2;
1566 /* Both of these sections are (technically) created by the user
1567 putting data in them, so they shouldn't be marked
1568 SEC_LINKER_CREATED.
1570 The linker creates them so it has somewhere to attach their
1571 respective symbols. In fact, if they were empty it would
1572 be OK to leave the symbol set to 0 (or any random number), because
1573 the appropriate register should never be used. */
1574 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1575 | SEC_IN_MEMORY);
1577 switch (which)
1579 default:
1580 (*_bfd_error_handler) (_("%s: Unknown special linker type %d"),
1581 bfd_get_filename (abfd),
1582 (int)which);
1584 bfd_set_error (bfd_error_bad_value);
1585 return (elf_linker_section_t *)0;
1587 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
1588 defaults.name = ".sdata";
1589 defaults.rel_name = ".rela.sdata";
1590 defaults.bss_name = ".sbss";
1591 defaults.sym_name = "_SDA_BASE_";
1592 defaults.sym_offset = 32768;
1593 break;
1595 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
1596 defaults.name = ".sdata2";
1597 defaults.rel_name = ".rela.sdata2";
1598 defaults.bss_name = ".sbss2";
1599 defaults.sym_name = "_SDA2_BASE_";
1600 defaults.sym_offset = 32768;
1601 defaults.flags |= SEC_READONLY;
1602 break;
1605 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
1608 return lsect;
1612 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we need to bump up
1613 the number of section headers. */
1615 static int
1616 ppc_elf_additional_program_headers (abfd)
1617 bfd *abfd;
1619 asection *s;
1620 int ret;
1622 ret = 0;
1624 s = bfd_get_section_by_name (abfd, ".interp");
1625 if (s != NULL)
1626 ++ret;
1628 s = bfd_get_section_by_name (abfd, ".sbss2");
1629 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1630 ++ret;
1632 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1633 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1634 ++ret;
1636 return ret;
1639 /* Modify the segment map if needed */
1641 static boolean
1642 ppc_elf_modify_segment_map (abfd)
1643 bfd *abfd;
1645 return true;
1648 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
1649 to output sections (just like _bfd_elf_create_dynamic_sections has
1650 to create .dynbss and .rela.bss). */
1652 static boolean
1653 ppc_elf_create_dynamic_sections (abfd, info)
1654 bfd *abfd;
1655 struct bfd_link_info *info;
1657 register asection *s;
1658 flagword flags;
1660 if (!_bfd_elf_create_dynamic_sections(abfd, info))
1661 return false;
1663 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1664 | SEC_LINKER_CREATED);
1666 s = bfd_make_section (abfd, ".dynsbss");
1667 if (s == NULL
1668 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1669 return false;
1671 if (! info->shared)
1673 s = bfd_make_section (abfd, ".rela.sbss");
1674 if (s == NULL
1675 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1676 || ! bfd_set_section_alignment (abfd, s, 2))
1677 return false;
1679 return true;
1682 /* Adjust a symbol defined by a dynamic object and referenced by a
1683 regular object. The current definition is in some section of the
1684 dynamic object, but we're not including those sections. We have to
1685 change the definition to something the rest of the link can
1686 understand. */
1688 static boolean
1689 ppc_elf_adjust_dynamic_symbol (info, h)
1690 struct bfd_link_info *info;
1691 struct elf_link_hash_entry *h;
1693 bfd *dynobj = elf_hash_table (info)->dynobj;
1694 asection *s;
1695 unsigned int power_of_two;
1696 bfd_vma plt_offset;
1698 #ifdef DEBUG
1699 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n", h->root.root.string);
1700 #endif
1702 /* Make sure we know what is going on here. */
1703 BFD_ASSERT (dynobj != NULL
1704 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1705 || h->weakdef != NULL
1706 || ((h->elf_link_hash_flags
1707 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1708 && (h->elf_link_hash_flags
1709 & ELF_LINK_HASH_REF_REGULAR) != 0
1710 && (h->elf_link_hash_flags
1711 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1714 /* If this is a function, put it in the procedure linkage table. We
1715 will fill in the contents of the procedure linkage table later,
1716 when we know the address of the .got section. */
1717 if (h->type == STT_FUNC
1718 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1720 if (! elf_hash_table (info)->dynamic_sections_created
1721 || ((!info->shared || info->symbolic || h->dynindx == -1)
1722 && (h->elf_link_hash_flags
1723 & ELF_LINK_HASH_DEF_REGULAR) != 0)
1724 || (info->shared && h->plt.refcount <= 0))
1726 /* A PLT entry is not required/allowed when:
1728 1. We are not using ld.so; because then the PLT entry
1729 can't be set up, so we can't use one.
1731 2. We know for certain that a symbol is defined in
1732 this object, because this object is the application,
1733 is linked with -Bsymbolic, or because the symbol is local.
1735 3. GC has rendered the entry unused.
1736 Note, however, that in an executable all references to the
1737 symbol go to the PLT, so we can't turn it off in that case.
1738 ??? The correct thing to do here is to reference count
1739 all uses of the symbol, not just those to the GOT or PLT. */
1740 h->plt.offset = (bfd_vma) -1;
1741 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1742 return true;
1745 /* Make sure this symbol is output as a dynamic symbol. */
1746 if (h->dynindx == -1)
1748 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1749 return false;
1751 BFD_ASSERT (h->dynindx != -1);
1753 s = bfd_get_section_by_name (dynobj, ".plt");
1754 BFD_ASSERT (s != NULL);
1756 /* If this is the first .plt entry, make room for the special
1757 first entry. */
1758 if (s->_raw_size == 0)
1759 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
1761 /* The PowerPC PLT is actually composed of two parts, the first part
1762 is 2 words (for a load and a jump), and then there is a remaining
1763 word available at the end. */
1764 plt_offset = (PLT_INITIAL_ENTRY_SIZE
1765 + (PLT_SLOT_SIZE
1766 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
1767 / PLT_ENTRY_SIZE)));
1769 /* If this symbol is not defined in a regular file, and we are
1770 not generating a shared library, then set the symbol to this
1771 location in the .plt. This is required to make function
1772 pointers compare as equal between the normal executable and
1773 the shared library. */
1774 if (! info->shared
1775 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1777 h->root.u.def.section = s;
1778 h->root.u.def.value = plt_offset;
1781 h->plt.offset = plt_offset;
1783 /* Make room for this entry. After the 8192nd entry, room
1784 for two entries is allocated. */
1785 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
1786 >= PLT_NUM_SINGLE_ENTRIES)
1787 s->_raw_size += 2 * PLT_ENTRY_SIZE;
1788 else
1789 s->_raw_size += PLT_ENTRY_SIZE;
1791 /* We also need to make an entry in the .rela.plt section. */
1792 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1793 BFD_ASSERT (s != NULL);
1794 s->_raw_size += sizeof (Elf32_External_Rela);
1796 return true;
1799 /* If this is a weak symbol, and there is a real definition, the
1800 processor independent code will have arranged for us to see the
1801 real definition first, and we can just use the same value. */
1802 if (h->weakdef != NULL)
1804 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1805 || h->weakdef->root.type == bfd_link_hash_defweak);
1806 h->root.u.def.section = h->weakdef->root.u.def.section;
1807 h->root.u.def.value = h->weakdef->root.u.def.value;
1808 return true;
1811 /* This is a reference to a symbol defined by a dynamic object which
1812 is not a function. */
1814 /* If we are creating a shared library, we must presume that the
1815 only references to the symbol are via the global offset table.
1816 For such cases we need not do anything here; the relocations will
1817 be handled correctly by relocate_section. */
1818 if (info->shared)
1819 return true;
1821 /* We must allocate the symbol in our .dynbss section, which will
1822 become part of the .bss section of the executable. There will be
1823 an entry for this symbol in the .dynsym section. The dynamic
1824 object will contain position independent code, so all references
1825 from the dynamic object to this symbol will go through the global
1826 offset table. The dynamic linker will use the .dynsym entry to
1827 determine the address it must put in the global offset table, so
1828 both the dynamic object and the regular object will refer to the
1829 same memory location for the variable.
1831 Of course, if the symbol is sufficiently small, we must instead
1832 allocate it in .sbss. FIXME: It would be better to do this if and
1833 only if there were actually SDAREL relocs for that symbol. */
1835 if (h->size <= elf_gp_size (dynobj))
1836 s = bfd_get_section_by_name (dynobj, ".dynsbss");
1837 else
1838 s = bfd_get_section_by_name (dynobj, ".dynbss");
1839 BFD_ASSERT (s != NULL);
1841 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
1842 copy the initial value out of the dynamic object and into the
1843 runtime process image. We need to remember the offset into the
1844 .rela.bss section we are going to use. */
1845 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1847 asection *srel;
1849 if (h->size <= elf_gp_size (dynobj))
1850 srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
1851 else
1852 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1853 BFD_ASSERT (srel != NULL);
1854 srel->_raw_size += sizeof (Elf32_External_Rela);
1855 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1858 /* We need to figure out the alignment required for this symbol. I
1859 have no idea how ELF linkers handle this. */
1860 power_of_two = bfd_log2 (h->size);
1861 if (power_of_two > 4)
1862 power_of_two = 4;
1864 /* Apply the required alignment. */
1865 s->_raw_size = BFD_ALIGN (s->_raw_size,
1866 (bfd_size_type) (1 << power_of_two));
1867 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1869 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1870 return false;
1873 /* Define the symbol as being at this point in the section. */
1874 h->root.u.def.section = s;
1875 h->root.u.def.value = s->_raw_size;
1877 /* Increment the section size to make room for the symbol. */
1878 s->_raw_size += h->size;
1880 return true;
1884 /* Increment the index of a dynamic symbol by a given amount. Called
1885 via elf_link_hash_traverse. */
1887 static boolean
1888 ppc_elf_adjust_dynindx (h, cparg)
1889 struct elf_link_hash_entry *h;
1890 PTR cparg;
1892 int *cp = (int *) cparg;
1894 #ifdef DEBUG
1895 fprintf (stderr, "ppc_elf_adjust_dynindx called, h->dynindx = %d, *cp = %d\n", h->dynindx, *cp);
1896 #endif
1898 if (h->dynindx != -1)
1899 h->dynindx += *cp;
1901 return true;
1905 /* Set the sizes of the dynamic sections. */
1907 static boolean
1908 ppc_elf_size_dynamic_sections (output_bfd, info)
1909 bfd *output_bfd;
1910 struct bfd_link_info *info;
1912 bfd *dynobj;
1913 asection *s;
1914 boolean plt;
1915 boolean relocs;
1916 boolean reltext;
1918 #ifdef DEBUG
1919 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
1920 #endif
1922 dynobj = elf_hash_table (info)->dynobj;
1923 BFD_ASSERT (dynobj != NULL);
1925 if (elf_hash_table (info)->dynamic_sections_created)
1927 /* Set the contents of the .interp section to the interpreter. */
1928 if (! info->shared)
1930 s = bfd_get_section_by_name (dynobj, ".interp");
1931 BFD_ASSERT (s != NULL);
1932 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1933 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1936 else
1938 /* We may have created entries in the .rela.got, .rela.sdata, and
1939 .rela.sdata2 sections. However, if we are not creating the
1940 dynamic sections, we will not actually use these entries. Reset
1941 the size of .rela.got, et al, which will cause it to get
1942 stripped from the output file below. */
1943 static char *rela_sections[] = { ".rela.got", ".rela.sdata",
1944 ".rela.sdata2", ".rela.sbss",
1945 (char *)0 };
1946 char **p;
1948 for (p = rela_sections; *p != (char *)0; p++)
1950 s = bfd_get_section_by_name (dynobj, *p);
1951 if (s != NULL)
1952 s->_raw_size = 0;
1956 /* The check_relocs and adjust_dynamic_symbol entry points have
1957 determined the sizes of the various dynamic sections. Allocate
1958 memory for them. */
1959 plt = false;
1960 relocs = false;
1961 reltext = false;
1962 for (s = dynobj->sections; s != NULL; s = s->next)
1964 const char *name;
1965 boolean strip;
1967 if ((s->flags & SEC_LINKER_CREATED) == 0)
1968 continue;
1970 /* It's OK to base decisions on the section name, because none
1971 of the dynobj section names depend upon the input files. */
1972 name = bfd_get_section_name (dynobj, s);
1974 strip = false;
1976 if (strcmp (name, ".plt") == 0)
1978 if (s->_raw_size == 0)
1980 /* Strip this section if we don't need it; see the
1981 comment below. */
1982 strip = true;
1984 else
1986 /* Remember whether there is a PLT. */
1987 plt = true;
1990 else if (strncmp (name, ".rela", 5) == 0)
1992 if (s->_raw_size == 0)
1994 /* If we don't need this section, strip it from the
1995 output file. This is mostly to handle .rela.bss and
1996 .rela.plt. We must create both sections in
1997 create_dynamic_sections, because they must be created
1998 before the linker maps input sections to output
1999 sections. The linker does that before
2000 adjust_dynamic_symbol is called, and it is that
2001 function which decides whether anything needs to go
2002 into these sections. */
2003 strip = true;
2005 else
2007 asection *target;
2008 const char *outname;
2010 /* Remember whether there are any relocation sections. */
2011 relocs = true;
2013 /* If this relocation section applies to a read only
2014 section, then we probably need a DT_TEXTREL entry. */
2015 outname = bfd_get_section_name (output_bfd,
2016 s->output_section);
2017 target = bfd_get_section_by_name (output_bfd, outname + 5);
2018 if (target != NULL
2019 && (target->flags & SEC_READONLY) != 0
2020 && (target->flags & SEC_ALLOC) != 0)
2021 reltext = true;
2023 /* We use the reloc_count field as a counter if we need
2024 to copy relocs into the output file. */
2025 s->reloc_count = 0;
2028 else if (strcmp (name, ".got") != 0
2029 && strcmp (name, ".sdata") != 0
2030 && strcmp (name, ".sdata2") != 0)
2032 /* It's not one of our sections, so don't allocate space. */
2033 continue;
2036 if (strip)
2038 _bfd_strip_section_from_output (s);
2039 continue;
2042 /* Allocate memory for the section contents. */
2043 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2044 if (s->contents == NULL && s->_raw_size != 0)
2045 return false;
2048 if (elf_hash_table (info)->dynamic_sections_created)
2050 /* Add some entries to the .dynamic section. We fill in the
2051 values later, in ppc_elf_finish_dynamic_sections, but we
2052 must add the entries now so that we get the correct size for
2053 the .dynamic section. The DT_DEBUG entry is filled in by the
2054 dynamic linker and used by the debugger. */
2055 if (! info->shared)
2057 if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
2058 return false;
2061 if (plt)
2063 if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
2064 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
2065 || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
2066 || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
2067 return false;
2070 if (relocs)
2072 if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
2073 || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
2074 || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
2075 sizeof (Elf32_External_Rela)))
2076 return false;
2079 if (reltext)
2081 if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
2082 return false;
2086 /* If we are generating a shared library, we generate a section
2087 symbol for each output section. These are local symbols, which
2088 means that they must come first in the dynamic symbol table.
2089 That means we must increment the dynamic symbol index of every
2090 other dynamic symbol.
2092 FIXME: We assume that there will never be relocations to
2093 locations in linker-created sections that do not have
2094 externally-visible names. Instead, we should work out precisely
2095 which sections relocations are targetted at. */
2096 if (info->shared)
2098 int c;
2100 for (c = 0, s = output_bfd->sections; s != NULL; s = s->next)
2102 if ((s->flags & SEC_LINKER_CREATED) != 0
2103 || (s->flags & SEC_ALLOC) == 0)
2105 elf_section_data (s)->dynindx = -1;
2106 continue;
2109 /* These symbols will have no names, so we don't need to
2110 fiddle with dynstr_index. */
2112 elf_section_data (s)->dynindx = c + 1;
2114 c++;
2117 elf_link_hash_traverse (elf_hash_table (info),
2118 ppc_elf_adjust_dynindx,
2119 (PTR) &c);
2120 elf_hash_table (info)->dynsymcount += c;
2123 return true;
2127 /* Look through the relocs for a section during the first phase, and
2128 allocate space in the global offset table or procedure linkage
2129 table. */
2131 static boolean
2132 ppc_elf_check_relocs (abfd, info, sec, relocs)
2133 bfd *abfd;
2134 struct bfd_link_info *info;
2135 asection *sec;
2136 const Elf_Internal_Rela *relocs;
2138 bfd *dynobj;
2139 Elf_Internal_Shdr *symtab_hdr;
2140 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2141 const Elf_Internal_Rela *rel;
2142 const Elf_Internal_Rela *rel_end;
2143 bfd_signed_vma *local_got_refcounts;
2144 elf_linker_section_t *sdata;
2145 elf_linker_section_t *sdata2;
2146 asection *sreloc;
2147 asection *sgot = NULL;
2148 asection *srelgot = NULL;
2150 if (info->relocateable)
2151 return true;
2153 #ifdef DEBUG
2154 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
2155 bfd_get_section_name (abfd, sec),
2156 bfd_get_filename (abfd));
2157 #endif
2159 /* Create the linker generated sections all the time so that the
2160 special symbols are created. */
2162 if ((sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA)) == NULL)
2164 sdata = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2165 if (!sdata)
2166 return false;
2170 if ((sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2)) == NULL)
2172 sdata2 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA2);
2173 if (!sdata2)
2174 return false;
2177 dynobj = elf_hash_table (info)->dynobj;
2178 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2179 local_got_refcounts = elf_local_got_refcounts (abfd);
2181 sym_hashes = elf_sym_hashes (abfd);
2182 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
2183 if (!elf_bad_symtab (abfd))
2184 sym_hashes_end -= symtab_hdr->sh_info;
2186 sreloc = NULL;
2188 rel_end = relocs + sec->reloc_count;
2189 for (rel = relocs; rel < rel_end; rel++)
2191 unsigned long r_symndx;
2192 struct elf_link_hash_entry *h;
2194 r_symndx = ELF32_R_SYM (rel->r_info);
2195 if (r_symndx < symtab_hdr->sh_info)
2196 h = NULL;
2197 else
2198 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2200 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2201 This shows up in particular in an R_PPC_ADDR32 in the eabi
2202 startup code. */
2203 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2205 if (sgot == NULL)
2207 if (dynobj == NULL)
2208 elf_hash_table (info)->dynobj = dynobj = abfd;
2209 if (! _bfd_elf_create_got_section (dynobj, info))
2210 return false;
2211 sgot = bfd_get_section_by_name (dynobj, ".got");
2212 BFD_ASSERT (sgot != NULL);
2216 switch (ELF32_R_TYPE (rel->r_info))
2218 /* GOT16 relocations */
2219 case R_PPC_GOT16:
2220 case R_PPC_GOT16_LO:
2221 case R_PPC_GOT16_HI:
2222 case R_PPC_GOT16_HA:
2223 /* This symbol requires a global offset table entry. */
2225 if (sgot == NULL)
2227 if (dynobj == NULL)
2228 elf_hash_table (info)->dynobj = dynobj = abfd;
2229 if (! _bfd_elf_create_got_section (dynobj, info))
2230 return false;
2231 sgot = bfd_get_section_by_name (dynobj, ".got");
2232 BFD_ASSERT (sgot != NULL);
2235 if (srelgot == NULL
2236 && (h != NULL || info->shared))
2238 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2239 if (srelgot == NULL)
2241 srelgot = bfd_make_section (dynobj, ".rela.got");
2242 if (srelgot == NULL
2243 || ! bfd_set_section_flags (dynobj, srelgot,
2244 (SEC_ALLOC
2245 | SEC_LOAD
2246 | SEC_HAS_CONTENTS
2247 | SEC_IN_MEMORY
2248 | SEC_LINKER_CREATED
2249 | SEC_READONLY))
2250 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2251 return false;
2255 if (h != NULL)
2257 if (h->got.refcount == -1)
2259 /* Make sure this symbol is output as a dynamic symbol. */
2260 if (h->dynindx == -1)
2261 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2262 return false;
2264 /* Allocate space in the .got. */
2265 sgot->_raw_size += 4;
2266 /* Allocate relocation space. */
2267 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2269 h->got.refcount = 1;
2271 else
2272 h->got.refcount++;
2274 else
2276 /* This is a global offset table entry for a local symbol. */
2277 if (local_got_refcounts == NULL)
2279 size_t size;
2281 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
2282 local_got_refcounts = (bfd_signed_vma *)
2283 bfd_alloc (abfd, size);
2284 if (local_got_refcounts == NULL)
2285 return false;
2286 elf_local_got_refcounts (abfd) = local_got_refcounts;
2287 memset (local_got_refcounts, -1, size);
2289 if (local_got_refcounts[r_symndx] == -1)
2291 sgot->_raw_size += 4;
2293 /* If we are generating a shared object, we need to
2294 output a R_PPC_RELATIVE reloc so that the
2295 dynamic linker can adjust this GOT entry. */
2296 if (info->shared)
2297 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2299 local_got_refcounts[r_symndx] = 1;
2301 else
2302 local_got_refcounts[r_symndx]++;
2304 break;
2306 /* Indirect .sdata relocation */
2307 case R_PPC_EMB_SDAI16:
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), "R_PPC_EMB_SDAI16"));
2313 return false;
2316 if (srelgot == NULL && (h != NULL || info->shared))
2318 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2319 if (srelgot == NULL)
2321 srelgot = bfd_make_section (dynobj, ".rela.got");
2322 if (srelgot == NULL
2323 || ! bfd_set_section_flags (dynobj, srelgot,
2324 (SEC_ALLOC
2325 | SEC_LOAD
2326 | SEC_HAS_CONTENTS
2327 | SEC_IN_MEMORY
2328 | SEC_LINKER_CREATED
2329 | SEC_READONLY))
2330 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2331 return false;
2335 if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata, h, rel))
2336 return false;
2338 break;
2340 /* Indirect .sdata2 relocation */
2341 case R_PPC_EMB_SDA2I16:
2342 if (info->shared)
2344 ((*_bfd_error_handler)
2345 (_("%s: relocation %s cannot be used when making a shared object"),
2346 bfd_get_filename (abfd), "R_PPC_EMB_SDA2I16"));
2347 return false;
2350 if (srelgot == NULL && (h != NULL || info->shared))
2352 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2353 if (srelgot == NULL)
2355 srelgot = bfd_make_section (dynobj, ".rela.got");
2356 if (srelgot == NULL
2357 || ! bfd_set_section_flags (dynobj, srelgot,
2358 (SEC_ALLOC
2359 | SEC_LOAD
2360 | SEC_HAS_CONTENTS
2361 | SEC_IN_MEMORY
2362 | SEC_LINKER_CREATED
2363 | SEC_READONLY))
2364 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2365 return false;
2369 if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata2, h, rel))
2370 return false;
2372 break;
2374 case R_PPC_SDAREL16:
2375 case R_PPC_EMB_SDA2REL:
2376 case R_PPC_EMB_SDA21:
2377 if (info->shared)
2379 ((*_bfd_error_handler)
2380 (_("%s: relocation %s cannot be used when making a shared object"),
2381 bfd_get_filename (abfd),
2382 ppc_elf_howto_table[(int)ELF32_R_TYPE (rel->r_info)]->name));
2383 return false;
2385 break;
2387 case R_PPC_PLT32:
2388 case R_PPC_PLTREL24:
2389 case R_PPC_PLT16_LO:
2390 case R_PPC_PLT16_HI:
2391 case R_PPC_PLT16_HA:
2392 #ifdef DEBUG
2393 fprintf (stderr, "Reloc requires a PLT entry\n");
2394 #endif
2395 /* This symbol requires a procedure linkage table entry. We
2396 actually build the entry in adjust_dynamic_symbol,
2397 because this might be a case of linking PIC code without
2398 linking in any dynamic objects, in which case we don't
2399 need to generate a procedure linkage table after all. */
2401 if (h == NULL)
2403 /* It does not make sense to have a procedure linkage
2404 table entry for a local symbol. */
2405 bfd_set_error (bfd_error_bad_value);
2406 return false;
2409 /* Make sure this symbol is output as a dynamic symbol. */
2410 if (h->dynindx == -1)
2412 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2413 return false;
2415 if (h->plt.refcount == -1)
2417 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2418 h->plt.refcount = 1;
2420 else
2421 h->plt.refcount++;
2422 break;
2424 /* The following relocations don't need to propagate the
2425 relocation if linking a shared object since they are
2426 section relative. */
2427 case R_PPC_SECTOFF:
2428 case R_PPC_SECTOFF_LO:
2429 case R_PPC_SECTOFF_HI:
2430 case R_PPC_SECTOFF_HA:
2431 break;
2433 /* This refers only to functions defined in the shared library */
2434 case R_PPC_LOCAL24PC:
2435 break;
2437 /* This relocation describes the C++ object vtable hierarchy.
2438 Reconstruct it for later use during GC. */
2439 case R_PPC_GNU_VTINHERIT:
2440 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2441 return false;
2442 break;
2444 /* This relocation describes which C++ vtable entries are actually
2445 used. Record for later use during GC. */
2446 case R_PPC_GNU_VTENTRY:
2447 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2448 return false;
2449 break;
2451 /* When creating a shared object, we must copy these
2452 relocs into the output file. We create a reloc
2453 section in dynobj and make room for the reloc. */
2454 case R_PPC_REL24:
2455 case R_PPC_REL14:
2456 case R_PPC_REL14_BRTAKEN:
2457 case R_PPC_REL14_BRNTAKEN:
2458 case R_PPC_REL32:
2459 if (h == NULL
2460 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2461 break;
2462 /* fall through */
2464 default:
2465 if (info->shared)
2467 #ifdef DEBUG
2468 fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
2469 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
2470 #endif
2471 if (sreloc == NULL)
2473 const char *name;
2475 name = (bfd_elf_string_from_elf_section
2476 (abfd,
2477 elf_elfheader (abfd)->e_shstrndx,
2478 elf_section_data (sec)->rel_hdr.sh_name));
2479 if (name == NULL)
2480 return false;
2482 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2483 && strcmp (bfd_get_section_name (abfd, sec),
2484 name + 5) == 0);
2486 sreloc = bfd_get_section_by_name (dynobj, name);
2487 if (sreloc == NULL)
2489 flagword flags;
2491 sreloc = bfd_make_section (dynobj, name);
2492 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2493 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2494 if ((sec->flags & SEC_ALLOC) != 0)
2495 flags |= SEC_ALLOC | SEC_LOAD;
2496 if (sreloc == NULL
2497 || ! bfd_set_section_flags (dynobj, sreloc, flags)
2498 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
2499 return false;
2503 sreloc->_raw_size += sizeof (Elf32_External_Rela);
2505 /* FIXME: We should here do what the m68k and i386
2506 backends do: if the reloc is pc-relative, record it
2507 in case it turns out that the reloc is unnecessary
2508 because the symbol is forced local by versioning or
2509 we are linking with -Bdynamic. Fortunately this
2510 case is not frequent. */
2513 break;
2517 return true;
2520 /* Return the section that should be marked against GC for a given
2521 relocation. */
2523 static asection *
2524 ppc_elf_gc_mark_hook (abfd, info, rel, h, sym)
2525 bfd *abfd;
2526 struct bfd_link_info *info;
2527 Elf_Internal_Rela *rel;
2528 struct elf_link_hash_entry *h;
2529 Elf_Internal_Sym *sym;
2531 if (h != NULL)
2533 switch (ELF32_R_TYPE (rel->r_info))
2535 case R_PPC_GNU_VTINHERIT:
2536 case R_PPC_GNU_VTENTRY:
2537 break;
2539 default:
2540 switch (h->root.type)
2542 case bfd_link_hash_defined:
2543 case bfd_link_hash_defweak:
2544 return h->root.u.def.section;
2546 case bfd_link_hash_common:
2547 return h->root.u.c.p->section;
2549 default:
2550 break;
2554 else
2556 if (!(elf_bad_symtab (abfd)
2557 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
2558 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
2559 && sym->st_shndx != SHN_COMMON))
2561 return bfd_section_from_elf_index (abfd, sym->st_shndx);
2565 return NULL;
2568 /* Update the got entry reference counts for the section being removed. */
2570 static boolean
2571 ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
2572 bfd *abfd;
2573 struct bfd_link_info *info;
2574 asection *sec;
2575 const Elf_Internal_Rela *relocs;
2577 Elf_Internal_Shdr *symtab_hdr;
2578 struct elf_link_hash_entry **sym_hashes;
2579 bfd_signed_vma *local_got_refcounts;
2580 const Elf_Internal_Rela *rel, *relend;
2581 unsigned long r_symndx;
2582 struct elf_link_hash_entry *h;
2584 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2585 sym_hashes = elf_sym_hashes (abfd);
2586 local_got_refcounts = elf_local_got_refcounts (abfd);
2588 relend = relocs + sec->reloc_count;
2589 for (rel = relocs; rel < relend; rel++)
2590 switch (ELF32_R_TYPE (rel->r_info))
2592 case R_PPC_GOT16:
2593 case R_PPC_GOT16_LO:
2594 case R_PPC_GOT16_HI:
2595 case R_PPC_GOT16_HA:
2596 r_symndx = ELF32_R_SYM (rel->r_info);
2597 if (r_symndx >= symtab_hdr->sh_info)
2599 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2600 if (h->got.refcount > 0)
2601 h->got.refcount--;
2603 else
2605 if (local_got_refcounts[r_symndx] > 0)
2606 local_got_refcounts[r_symndx]--;
2608 break;
2610 case R_PPC_PLT32:
2611 case R_PPC_PLTREL24:
2612 case R_PPC_PLT16_LO:
2613 case R_PPC_PLT16_HI:
2614 case R_PPC_PLT16_HA:
2615 r_symndx = ELF32_R_SYM (rel->r_info);
2616 if (r_symndx >= symtab_hdr->sh_info)
2618 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2619 if (h->plt.refcount > 0)
2620 h->plt.refcount--;
2622 break;
2624 default:
2625 break;
2628 return true;
2631 /* Hook called by the linker routine which adds symbols from an object
2632 file. We use it to put .comm items in .sbss, and not .bss. */
2634 /*ARGSUSED*/
2635 static boolean
2636 ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2637 bfd *abfd;
2638 struct bfd_link_info *info;
2639 const Elf_Internal_Sym *sym;
2640 const char **namep;
2641 flagword *flagsp;
2642 asection **secp;
2643 bfd_vma *valp;
2645 if (sym->st_shndx == SHN_COMMON
2646 && !info->relocateable
2647 && sym->st_size <= (bfd_vma) bfd_get_gp_size (abfd))
2649 /* Common symbols less than or equal to -G nn bytes are automatically
2650 put into .sdata. */
2651 elf_linker_section_t *sdata
2652 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2654 if (!sdata->bss_section)
2656 /* We don't go through bfd_make_section, because we don't
2657 want to attach this common section to DYNOBJ. The linker
2658 will move the symbols to the appropriate output section
2659 when it defines common symbols. */
2660 sdata->bss_section = ((asection *)
2661 bfd_zalloc (abfd, sizeof (asection)));
2662 if (sdata->bss_section == NULL)
2663 return false;
2664 sdata->bss_section->name = sdata->bss_name;
2665 sdata->bss_section->flags = SEC_IS_COMMON;
2666 sdata->bss_section->output_section = sdata->bss_section;
2667 sdata->bss_section->symbol =
2668 (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
2669 sdata->bss_section->symbol_ptr_ptr =
2670 (asymbol **) bfd_zalloc (abfd, sizeof (asymbol *));
2671 if (sdata->bss_section->symbol == NULL
2672 || sdata->bss_section->symbol_ptr_ptr == NULL)
2673 return false;
2674 sdata->bss_section->symbol->name = sdata->bss_name;
2675 sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
2676 sdata->bss_section->symbol->section = sdata->bss_section;
2677 *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
2680 *secp = sdata->bss_section;
2681 *valp = sym->st_size;
2684 return true;
2688 /* Finish up dynamic symbol handling. We set the contents of various
2689 dynamic sections here. */
2691 static boolean
2692 ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
2693 bfd *output_bfd;
2694 struct bfd_link_info *info;
2695 struct elf_link_hash_entry *h;
2696 Elf_Internal_Sym *sym;
2698 bfd *dynobj;
2700 #ifdef DEBUG
2701 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
2702 h->root.root.string);
2703 #endif
2705 dynobj = elf_hash_table (info)->dynobj;
2706 BFD_ASSERT (dynobj != NULL);
2708 if (h->plt.offset != (bfd_vma) -1)
2710 asection *splt;
2711 asection *srela;
2712 Elf_Internal_Rela rela;
2713 bfd_vma reloc_index;
2715 #ifdef DEBUG
2716 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
2717 #endif
2719 /* This symbol has an entry in the procedure linkage table. Set
2720 it up. */
2722 BFD_ASSERT (h->dynindx != -1);
2724 splt = bfd_get_section_by_name (dynobj, ".plt");
2725 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2726 BFD_ASSERT (splt != NULL && srela != NULL);
2728 /* We don't need to fill in the .plt. The ppc dynamic linker
2729 will fill it in. */
2731 /* Fill in the entry in the .rela.plt section. */
2732 rela.r_offset = (splt->output_section->vma
2733 + splt->output_offset
2734 + h->plt.offset);
2735 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
2736 rela.r_addend = 0;
2738 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
2739 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
2740 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
2741 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2742 ((Elf32_External_Rela *) srela->contents
2743 + reloc_index));
2745 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2747 /* Mark the symbol as undefined, rather than as defined in
2748 the .plt section. Leave the value alone. */
2749 sym->st_shndx = SHN_UNDEF;
2753 if (h->got.offset != (bfd_vma) -1)
2755 asection *sgot;
2756 asection *srela;
2757 Elf_Internal_Rela rela;
2759 /* This symbol has an entry in the global offset table. Set it
2760 up. */
2762 sgot = bfd_get_section_by_name (dynobj, ".got");
2763 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2764 BFD_ASSERT (sgot != NULL && srela != NULL);
2766 rela.r_offset = (sgot->output_section->vma
2767 + sgot->output_offset
2768 + (h->got.offset &~ 1));
2770 /* If this is a -Bsymbolic link, and the symbol is defined
2771 locally, we just want to emit a RELATIVE reloc. The entry in
2772 the global offset table will already have been initialized in
2773 the relocate_section function. */
2774 if (info->shared
2775 && (info->symbolic || h->dynindx == -1)
2776 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2778 rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
2779 rela.r_addend = (h->root.u.def.value
2780 + h->root.u.def.section->output_section->vma
2781 + h->root.u.def.section->output_offset);
2783 else
2785 BFD_ASSERT((h->got.offset & 1) == 0);
2786 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
2787 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_GLOB_DAT);
2788 rela.r_addend = 0;
2791 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2792 ((Elf32_External_Rela *) srela->contents
2793 + srela->reloc_count));
2794 ++srela->reloc_count;
2797 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2799 asection *s;
2800 Elf_Internal_Rela rela;
2802 /* This symbols needs a copy reloc. Set it up. */
2804 #ifdef DEBUG
2805 fprintf (stderr, ", copy");
2806 #endif
2808 BFD_ASSERT (h->dynindx != -1);
2810 if (h->size <= elf_gp_size (dynobj))
2811 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2812 ".rela.sbss");
2813 else
2814 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2815 ".rela.bss");
2816 BFD_ASSERT (s != NULL);
2818 rela.r_offset = (h->root.u.def.value
2819 + h->root.u.def.section->output_section->vma
2820 + h->root.u.def.section->output_offset);
2821 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
2822 rela.r_addend = 0;
2823 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2824 ((Elf32_External_Rela *) s->contents
2825 + s->reloc_count));
2826 ++s->reloc_count;
2829 #ifdef DEBUG
2830 fprintf (stderr, "\n");
2831 #endif
2833 /* Mark some specially defined symbols as absolute. */
2834 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2835 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2836 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2837 sym->st_shndx = SHN_ABS;
2839 return true;
2843 /* Finish up the dynamic sections. */
2845 static boolean
2846 ppc_elf_finish_dynamic_sections (output_bfd, info)
2847 bfd *output_bfd;
2848 struct bfd_link_info *info;
2850 asection *sdyn;
2851 bfd *dynobj = elf_hash_table (info)->dynobj;
2852 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
2854 #ifdef DEBUG
2855 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
2856 #endif
2858 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2860 if (elf_hash_table (info)->dynamic_sections_created)
2862 asection *splt;
2863 Elf32_External_Dyn *dyncon, *dynconend;
2865 splt = bfd_get_section_by_name (dynobj, ".plt");
2866 BFD_ASSERT (splt != NULL && sdyn != NULL);
2868 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2869 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2870 for (; dyncon < dynconend; dyncon++)
2872 Elf_Internal_Dyn dyn;
2873 const char *name;
2874 boolean size;
2876 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2878 switch (dyn.d_tag)
2880 case DT_PLTGOT: name = ".plt"; size = false; break;
2881 case DT_PLTRELSZ: name = ".rela.plt"; size = true; break;
2882 case DT_JMPREL: name = ".rela.plt"; size = false; break;
2883 default: name = NULL; size = false; break;
2886 if (name != NULL)
2888 asection *s;
2890 s = bfd_get_section_by_name (output_bfd, name);
2891 if (s == NULL)
2892 dyn.d_un.d_val = 0;
2893 else
2895 if (! size)
2896 dyn.d_un.d_ptr = s->vma;
2897 else
2899 if (s->_cooked_size != 0)
2900 dyn.d_un.d_val = s->_cooked_size;
2901 else
2902 dyn.d_un.d_val = s->_raw_size;
2905 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2910 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
2911 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
2912 if (sgot)
2914 unsigned char *contents = sgot->contents;
2915 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
2917 if (sdyn == NULL)
2918 bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
2919 else
2920 bfd_put_32 (output_bfd,
2921 sdyn->output_section->vma + sdyn->output_offset,
2922 contents+4);
2924 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2927 if (info->shared)
2929 asection *sdynsym;
2930 asection *s;
2931 Elf_Internal_Sym sym;
2932 int maxdindx = 0;
2934 /* Set up the section symbols for the output sections. */
2936 sdynsym = bfd_get_section_by_name (dynobj, ".dynsym");
2937 BFD_ASSERT (sdynsym != NULL);
2939 sym.st_size = 0;
2940 sym.st_name = 0;
2941 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2942 sym.st_other = 0;
2944 for (s = output_bfd->sections; s != NULL; s = s->next)
2946 int indx, dindx;
2948 sym.st_value = s->vma;
2950 indx = elf_section_data (s)->this_idx;
2951 dindx = elf_section_data (s)->dynindx;
2952 if (dindx != -1)
2954 BFD_ASSERT(indx > 0);
2955 BFD_ASSERT(dindx > 0);
2957 if (dindx > maxdindx)
2958 maxdindx = dindx;
2960 sym.st_shndx = indx;
2962 bfd_elf32_swap_symbol_out (output_bfd, &sym,
2963 (PTR) (((Elf32_External_Sym *)
2964 sdynsym->contents)
2965 + dindx));
2969 /* Set the sh_info field of the output .dynsym section to the
2970 index of the first global symbol. */
2971 elf_section_data (sdynsym->output_section)->this_hdr.sh_info =
2972 maxdindx + 1;
2975 return true;
2979 /* The RELOCATE_SECTION function is called by the ELF backend linker
2980 to handle the relocations for a section.
2982 The relocs are always passed as Rela structures; if the section
2983 actually uses Rel structures, the r_addend field will always be
2984 zero.
2986 This function is responsible for adjust the section contents as
2987 necessary, and (if using Rela relocs and generating a
2988 relocateable output file) adjusting the reloc addend as
2989 necessary.
2991 This function does not have to worry about setting the reloc
2992 address or the reloc symbol index.
2994 LOCAL_SYMS is a pointer to the swapped in local symbols.
2996 LOCAL_SECTIONS is an array giving the section in the input file
2997 corresponding to the st_shndx field of each local symbol.
2999 The global hash table entry for the global symbols can be found
3000 via elf_sym_hashes (input_bfd).
3002 When generating relocateable output, this function must handle
3003 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
3004 going to be the section symbol corresponding to the output
3005 section, which means that the addend must be adjusted
3006 accordingly. */
3008 static boolean
3009 ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
3010 contents, relocs, local_syms, local_sections)
3011 bfd *output_bfd;
3012 struct bfd_link_info *info;
3013 bfd *input_bfd;
3014 asection *input_section;
3015 bfd_byte *contents;
3016 Elf_Internal_Rela *relocs;
3017 Elf_Internal_Sym *local_syms;
3018 asection **local_sections;
3020 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3021 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3022 bfd *dynobj = elf_hash_table (info)->dynobj;
3023 elf_linker_section_t *sdata = (dynobj) ? elf_linker_section (dynobj, LINKER_SECTION_SDATA) : NULL;
3024 elf_linker_section_t *sdata2 = (dynobj) ? elf_linker_section (dynobj, LINKER_SECTION_SDATA2) : NULL;
3025 Elf_Internal_Rela *rel = relocs;
3026 Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
3027 asection *sreloc = NULL;
3028 asection *splt;
3029 asection *sgot;
3030 bfd_vma *local_got_offsets;
3031 boolean ret = true;
3032 long insn;
3034 #ifdef DEBUG
3035 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
3036 bfd_get_filename (input_bfd),
3037 bfd_section_name(input_bfd, input_section),
3038 (long)input_section->reloc_count,
3039 (info->relocateable) ? " (relocatable)" : "");
3040 #endif
3042 if (!ppc_elf_howto_table[ R_PPC_ADDR32 ]) /* Initialize howto table if needed */
3043 ppc_elf_howto_init ();
3045 local_got_offsets = elf_local_got_offsets (input_bfd);
3047 splt = sgot = NULL;
3048 if (dynobj != NULL)
3050 splt = bfd_get_section_by_name (dynobj, ".plt");
3051 sgot = bfd_get_section_by_name (dynobj, ".got");
3054 for (; rel < relend; rel++)
3056 enum elf_ppc_reloc_type r_type = (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
3057 bfd_vma offset = rel->r_offset;
3058 bfd_vma addend = rel->r_addend;
3059 bfd_reloc_status_type r = bfd_reloc_other;
3060 Elf_Internal_Sym *sym = (Elf_Internal_Sym *)0;
3061 asection *sec = (asection *)0;
3062 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *)0;
3063 const char *sym_name = (const char *)0;
3064 reloc_howto_type *howto;
3065 unsigned long r_symndx;
3066 bfd_vma relocation;
3068 /* Unknown relocation handling */
3069 if ((unsigned)r_type >= (unsigned)R_PPC_max || !ppc_elf_howto_table[(int)r_type])
3071 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
3072 bfd_get_filename (input_bfd),
3073 (int)r_type);
3075 bfd_set_error (bfd_error_bad_value);
3076 ret = false;
3077 continue;
3080 howto = ppc_elf_howto_table[(int)r_type];
3081 r_symndx = ELF32_R_SYM (rel->r_info);
3083 if (info->relocateable)
3085 /* This is a relocateable link. We don't have to change
3086 anything, unless the reloc is against a section symbol,
3087 in which case we have to adjust according to where the
3088 section symbol winds up in the output section. */
3089 if (r_symndx < symtab_hdr->sh_info)
3091 sym = local_syms + r_symndx;
3092 if ((unsigned)ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3094 sec = local_sections[r_symndx];
3095 addend = rel->r_addend += sec->output_offset + sym->st_value;
3099 #ifdef DEBUG
3100 fprintf (stderr, "\ttype = %s (%d), symbol index = %ld, offset = %ld, addend = %ld\n",
3101 howto->name,
3102 (int)r_type,
3103 r_symndx,
3104 (long)offset,
3105 (long)addend);
3106 #endif
3107 continue;
3110 /* This is a final link. */
3111 if (r_symndx < symtab_hdr->sh_info)
3113 sym = local_syms + r_symndx;
3114 sec = local_sections[r_symndx];
3115 sym_name = "<local symbol>";
3117 relocation = (sec->output_section->vma
3118 + sec->output_offset
3119 + sym->st_value);
3121 else
3123 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3124 while (h->root.type == bfd_link_hash_indirect
3125 || h->root.type == bfd_link_hash_warning)
3126 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3127 sym_name = h->root.root.string;
3128 if (h->root.type == bfd_link_hash_defined
3129 || h->root.type == bfd_link_hash_defweak)
3131 sec = h->root.u.def.section;
3132 if ((r_type == R_PPC_PLT32
3133 && h->plt.offset != (bfd_vma) -1)
3134 || (r_type == R_PPC_LOCAL24PC
3135 && sec->output_section == NULL)
3136 || ((r_type == R_PPC_GOT16
3137 || r_type == R_PPC_GOT16_LO
3138 || r_type == R_PPC_GOT16_HI
3139 || r_type == R_PPC_GOT16_HA)
3140 && elf_hash_table (info)->dynamic_sections_created
3141 && (! info->shared
3142 || (! info->symbolic && h->dynindx != -1)
3143 || (h->elf_link_hash_flags
3144 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3145 || (info->shared
3146 && ((! info->symbolic && h->dynindx != -1)
3147 || (h->elf_link_hash_flags
3148 & ELF_LINK_HASH_DEF_REGULAR) == 0)
3149 && (input_section->flags & SEC_ALLOC) != 0
3150 && (r_type == R_PPC_ADDR32
3151 || r_type == R_PPC_ADDR24
3152 || r_type == R_PPC_ADDR16
3153 || r_type == R_PPC_ADDR16_LO
3154 || r_type == R_PPC_ADDR16_HI
3155 || r_type == R_PPC_ADDR16_HA
3156 || r_type == R_PPC_ADDR14
3157 || r_type == R_PPC_ADDR14_BRTAKEN
3158 || r_type == R_PPC_ADDR14_BRNTAKEN
3159 || r_type == R_PPC_PLTREL24
3160 || r_type == R_PPC_COPY
3161 || r_type == R_PPC_GLOB_DAT
3162 || r_type == R_PPC_JMP_SLOT
3163 || r_type == R_PPC_UADDR32
3164 || r_type == R_PPC_UADDR16
3165 || r_type == R_PPC_SDAREL16
3166 || r_type == R_PPC_EMB_NADDR32
3167 || r_type == R_PPC_EMB_NADDR16
3168 || r_type == R_PPC_EMB_NADDR16_LO
3169 || r_type == R_PPC_EMB_NADDR16_HI
3170 || r_type == R_PPC_EMB_NADDR16_HA
3171 || r_type == R_PPC_EMB_SDAI16
3172 || r_type == R_PPC_EMB_SDA2I16
3173 || r_type == R_PPC_EMB_SDA2REL
3174 || r_type == R_PPC_EMB_SDA21
3175 || r_type == R_PPC_EMB_MRKREF
3176 || r_type == R_PPC_EMB_BIT_FLD
3177 || r_type == R_PPC_EMB_RELSDA
3178 || ((r_type == R_PPC_REL24
3179 || r_type == R_PPC_REL32
3180 || r_type == R_PPC_REL14
3181 || r_type == R_PPC_REL14_BRTAKEN
3182 || r_type == R_PPC_REL14_BRNTAKEN
3183 || r_type == R_PPC_RELATIVE)
3184 && strcmp (h->root.root.string,
3185 "_GLOBAL_OFFSET_TABLE_") != 0))))
3187 /* In these cases, we don't need the relocation
3188 value. We check specially because in some
3189 obscure cases sec->output_section will be NULL. */
3190 relocation = 0;
3192 else
3193 relocation = (h->root.u.def.value
3194 + sec->output_section->vma
3195 + sec->output_offset);
3197 else if (h->root.type == bfd_link_hash_undefweak)
3198 relocation = 0;
3199 else if (info->shared && !info->symbolic && !info->no_undefined)
3200 relocation = 0;
3201 else
3203 (*info->callbacks->undefined_symbol)(info,
3204 h->root.root.string,
3205 input_bfd,
3206 input_section,
3207 rel->r_offset);
3208 ret = false;
3209 continue;
3213 switch ((int)r_type)
3215 default:
3216 (*_bfd_error_handler) (_("%s: unknown relocation type %d for symbol %s"),
3217 bfd_get_filename (input_bfd),
3218 (int)r_type, sym_name);
3220 bfd_set_error (bfd_error_bad_value);
3221 ret = false;
3222 continue;
3224 /* Relocations that need no special processing. */
3225 case (int)R_PPC_LOCAL24PC:
3226 /* It makes no sense to point a local relocation
3227 at a symbol not in this object. */
3228 if (h != NULL
3229 && (h->root.type == bfd_link_hash_defined
3230 || h->root.type == bfd_link_hash_defweak)
3231 && sec->output_section == NULL)
3233 (*info->callbacks->undefined_symbol) (info,
3234 h->root.root.string,
3235 input_bfd,
3236 input_section,
3237 rel->r_offset);
3238 ret = false;
3239 continue;
3241 break;
3243 /* Relocations that may need to be propagated if this is a shared
3244 object. */
3245 case (int)R_PPC_REL24:
3246 case (int)R_PPC_REL32:
3247 case (int)R_PPC_REL14:
3248 /* If these relocations are not to a named symbol, they can be
3249 handled right here, no need to bother the dynamic linker. */
3250 if (h == NULL
3251 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3252 break;
3253 /* fall through */
3255 /* Relocations that always need to be propagated if this is a shared
3256 object. */
3257 case (int)R_PPC_NONE:
3258 case (int)R_PPC_ADDR32:
3259 case (int)R_PPC_ADDR24:
3260 case (int)R_PPC_ADDR16:
3261 case (int)R_PPC_ADDR16_LO:
3262 case (int)R_PPC_ADDR16_HI:
3263 case (int)R_PPC_ADDR16_HA:
3264 case (int)R_PPC_ADDR14:
3265 case (int)R_PPC_UADDR32:
3266 case (int)R_PPC_UADDR16:
3267 if (info->shared)
3269 Elf_Internal_Rela outrel;
3270 boolean skip;
3272 #ifdef DEBUG
3273 fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
3274 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
3275 #endif
3277 /* When generating a shared object, these relocations
3278 are copied into the output file to be resolved at run
3279 time. */
3281 if (sreloc == NULL)
3283 const char *name;
3285 name = (bfd_elf_string_from_elf_section
3286 (input_bfd,
3287 elf_elfheader (input_bfd)->e_shstrndx,
3288 elf_section_data (input_section)->rel_hdr.sh_name));
3289 if (name == NULL)
3290 return false;
3292 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3293 && strcmp (bfd_get_section_name (input_bfd,
3294 input_section),
3295 name + 5) == 0);
3297 sreloc = bfd_get_section_by_name (dynobj, name);
3298 BFD_ASSERT (sreloc != NULL);
3301 skip = false;
3303 if (elf_section_data (input_section)->stab_info == NULL)
3304 outrel.r_offset = rel->r_offset;
3305 else
3307 bfd_vma off;
3309 off = (_bfd_stab_section_offset
3310 (output_bfd, &elf_hash_table (info)->stab_info,
3311 input_section,
3312 &elf_section_data (input_section)->stab_info,
3313 rel->r_offset));
3314 if (off == (bfd_vma) -1)
3315 skip = true;
3316 outrel.r_offset = off;
3319 outrel.r_offset += (input_section->output_section->vma
3320 + input_section->output_offset);
3322 if (skip)
3323 memset (&outrel, 0, sizeof outrel);
3324 /* h->dynindx may be -1 if this symbol was marked to
3325 become local. */
3326 else if (h != NULL
3327 && ((! info->symbolic && h->dynindx != -1)
3328 || (h->elf_link_hash_flags
3329 & ELF_LINK_HASH_DEF_REGULAR) == 0))
3331 BFD_ASSERT (h->dynindx != -1);
3332 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3333 outrel.r_addend = rel->r_addend;
3335 else
3337 if (r_type == R_PPC_ADDR32)
3339 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3340 outrel.r_addend = relocation + rel->r_addend;
3342 else
3344 long indx;
3346 if (h == NULL)
3347 sec = local_sections[r_symndx];
3348 else
3350 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3351 || (h->root.type
3352 == bfd_link_hash_defweak));
3353 sec = h->root.u.def.section;
3355 if (sec != NULL && bfd_is_abs_section (sec))
3356 indx = 0;
3357 else if (sec == NULL || sec->owner == NULL)
3359 bfd_set_error (bfd_error_bad_value);
3360 return false;
3362 else
3364 asection *osec;
3366 osec = sec->output_section;
3367 indx = elf_section_data (osec)->dynindx;
3368 BFD_ASSERT(indx > 0);
3369 #ifdef DEBUG
3370 if (indx <= 0)
3372 printf("indx=%d section=%s flags=%08x name=%s\n",
3373 indx, osec->name, osec->flags,
3374 h->root.root.string);
3376 #endif
3379 outrel.r_info = ELF32_R_INFO (indx, r_type);
3380 outrel.r_addend = relocation + rel->r_addend;
3384 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3385 (((Elf32_External_Rela *)
3386 sreloc->contents)
3387 + sreloc->reloc_count));
3388 ++sreloc->reloc_count;
3390 /* This reloc will be computed at runtime, so there's no
3391 need to do anything now, unless this is a RELATIVE
3392 reloc in an unallocated section. */
3393 if (skip
3394 || (input_section->flags & SEC_ALLOC) != 0
3395 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE)
3396 continue;
3399 /* Arithmetic adjust relocations that aren't going into a
3400 shared object. */
3401 if (r_type == R_PPC_ADDR16_HA
3402 /* It's just possible that this symbol is a weak symbol
3403 that's not actually defined anywhere. In that case,
3404 'sec' would be NULL, and we should leave the symbol
3405 alone (it will be set to zero elsewhere in the link). */
3406 && sec != NULL)
3408 addend += ((relocation + addend) & 0x8000) << 1;
3410 break;
3412 /* branch taken prediction relocations */
3413 case (int)R_PPC_ADDR14_BRTAKEN:
3414 case (int)R_PPC_REL14_BRTAKEN:
3415 insn = bfd_get_32 (output_bfd, contents + offset);
3416 if ((relocation - offset) & 0x8000)
3417 insn &= ~BRANCH_PREDICT_BIT;
3418 else
3419 insn |= BRANCH_PREDICT_BIT;
3420 bfd_put_32 (output_bfd, insn, contents + offset);
3421 break;
3423 /* branch not taken predicition relocations */
3424 case (int)R_PPC_ADDR14_BRNTAKEN:
3425 case (int)R_PPC_REL14_BRNTAKEN:
3426 insn = bfd_get_32 (output_bfd, contents + offset);
3427 if ((relocation - offset) & 0x8000)
3428 insn |= BRANCH_PREDICT_BIT;
3429 else
3430 insn &= ~BRANCH_PREDICT_BIT;
3431 bfd_put_32 (output_bfd, insn, contents + offset);
3432 break;
3434 /* GOT16 relocations */
3435 case (int)R_PPC_GOT16:
3436 case (int)R_PPC_GOT16_LO:
3437 case (int)R_PPC_GOT16_HI:
3438 case (int)R_PPC_GOT16_HA:
3439 /* Relocation is to the entry for this symbol in the global
3440 offset table. */
3441 BFD_ASSERT (sgot != NULL);
3443 if (h != NULL)
3445 bfd_vma off;
3447 off = h->got.offset;
3448 BFD_ASSERT (off != (bfd_vma) -1);
3450 if (! elf_hash_table (info)->dynamic_sections_created
3451 || (info->shared
3452 && (info->symbolic || h->dynindx == -1)
3453 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
3455 /* This is actually a static link, or it is a
3456 -Bsymbolic link and the symbol is defined
3457 locally. We must initialize this entry in the
3458 global offset table. Since the offset must
3459 always be a multiple of 4, we use the least
3460 significant bit to record whether we have
3461 initialized it already.
3463 When doing a dynamic link, we create a .rela.got
3464 relocation entry to initialize the value. This
3465 is done in the finish_dynamic_symbol routine. */
3466 if ((off & 1) != 0)
3467 off &= ~1;
3468 else
3470 bfd_put_32 (output_bfd, relocation,
3471 sgot->contents + off);
3472 h->got.offset |= 1;
3476 relocation = sgot->output_offset + off - 4;
3478 else
3480 bfd_vma off;
3482 BFD_ASSERT (local_got_offsets != NULL
3483 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3485 off = local_got_offsets[r_symndx];
3487 /* The offset must always be a multiple of 4. We use
3488 the least significant bit to record whether we have
3489 already processed this entry. */
3490 if ((off & 1) != 0)
3491 off &= ~1;
3492 else
3494 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3496 if (info->shared)
3498 asection *srelgot;
3499 Elf_Internal_Rela outrel;
3501 /* We need to generate a R_PPC_RELATIVE reloc
3502 for the dynamic linker. */
3503 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3504 BFD_ASSERT (srelgot != NULL);
3506 outrel.r_offset = (sgot->output_section->vma
3507 + sgot->output_offset
3508 + off);
3509 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3510 outrel.r_addend = relocation;
3511 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3512 (((Elf32_External_Rela *)
3513 srelgot->contents)
3514 + srelgot->reloc_count));
3515 ++srelgot->reloc_count;
3518 local_got_offsets[r_symndx] |= 1;
3521 relocation = sgot->output_offset + off - 4;
3523 break;
3525 /* Indirect .sdata relocation */
3526 case (int)R_PPC_EMB_SDAI16:
3527 BFD_ASSERT (sdata != NULL);
3528 relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3529 sdata, h, relocation, rel,
3530 R_PPC_RELATIVE);
3531 break;
3533 /* Indirect .sdata2 relocation */
3534 case (int)R_PPC_EMB_SDA2I16:
3535 BFD_ASSERT (sdata2 != NULL);
3536 relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3537 sdata2, h, relocation, rel,
3538 R_PPC_RELATIVE);
3539 break;
3541 /* Handle the TOC16 reloc. We want to use the offset within the .got
3542 section, not the actual VMA. This is appropriate when generating
3543 an embedded ELF object, for which the .got section acts like the
3544 AIX .toc section. */
3545 case (int)R_PPC_TOC16: /* phony GOT16 relocations */
3546 BFD_ASSERT (sec != (asection *)0);
3547 BFD_ASSERT (bfd_is_und_section (sec)
3548 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
3549 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
3551 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
3552 break;
3554 case (int)R_PPC_PLTREL24:
3555 /* Relocation is to the entry for this symbol in the
3556 procedure linkage table. */
3557 BFD_ASSERT (h != NULL);
3559 if (h->plt.offset == (bfd_vma) -1
3560 || splt == NULL)
3562 /* We didn't make a PLT entry for this symbol. This
3563 happens when statically linking PIC code, or when
3564 using -Bsymbolic. */
3565 break;
3568 relocation = (splt->output_section->vma
3569 + splt->output_offset
3570 + h->plt.offset);
3571 break;
3573 /* relocate against _SDA_BASE_ */
3574 case (int)R_PPC_SDAREL16:
3576 const char *name;
3578 BFD_ASSERT (sec != (asection *)0);
3579 name = bfd_get_section_name (abfd, sec->output_section);
3580 if (strcmp (name, ".sdata") != 0
3581 && strcmp (name, ".sbss") != 0)
3583 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3584 bfd_get_filename (input_bfd),
3585 sym_name,
3586 ppc_elf_howto_table[ (int)r_type ]->name,
3587 name);
3589 addend -= (sdata->sym_hash->root.u.def.value
3590 + sdata->sym_hash->root.u.def.section->output_section->vma
3591 + sdata->sym_hash->root.u.def.section->output_offset);
3593 break;
3596 /* relocate against _SDA2_BASE_ */
3597 case (int)R_PPC_EMB_SDA2REL:
3599 const char *name;
3601 BFD_ASSERT (sec != (asection *)0);
3602 name = bfd_get_section_name (abfd, sec->output_section);
3603 if (strcmp (name, ".sdata2") != 0 && strcmp (name, ".sbss2") != 0)
3605 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3606 bfd_get_filename (input_bfd),
3607 sym_name,
3608 ppc_elf_howto_table[ (int)r_type ]->name,
3609 name);
3611 bfd_set_error (bfd_error_bad_value);
3612 ret = false;
3613 continue;
3615 addend -= (sdata2->sym_hash->root.u.def.value
3616 + sdata2->sym_hash->root.u.def.section->output_section->vma
3617 + sdata2->sym_hash->root.u.def.section->output_offset);
3619 break;
3622 /* relocate against either _SDA_BASE_, _SDA2_BASE_, or 0 */
3623 case (int)R_PPC_EMB_SDA21:
3624 case (int)R_PPC_EMB_RELSDA:
3626 const char *name;
3627 int reg;
3629 BFD_ASSERT (sec != (asection *)0);
3630 name = bfd_get_section_name (abfd, sec->output_section);
3631 if (strcmp (name, ".sdata") == 0 || strcmp (name, ".sbss") == 0)
3633 reg = 13;
3634 addend -= (sdata->sym_hash->root.u.def.value
3635 + sdata->sym_hash->root.u.def.section->output_section->vma
3636 + sdata->sym_hash->root.u.def.section->output_offset);
3639 else if (strcmp (name, ".sdata2") == 0 || strcmp (name, ".sbss2") == 0)
3641 reg = 2;
3642 addend -= (sdata2->sym_hash->root.u.def.value
3643 + sdata2->sym_hash->root.u.def.section->output_section->vma
3644 + sdata2->sym_hash->root.u.def.section->output_offset);
3647 else if (strcmp (name, ".PPC.EMB.sdata0") == 0 || strcmp (name, ".PPC.EMB.sbss0") == 0)
3649 reg = 0;
3652 else
3654 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3655 bfd_get_filename (input_bfd),
3656 sym_name,
3657 ppc_elf_howto_table[ (int)r_type ]->name,
3658 name);
3660 bfd_set_error (bfd_error_bad_value);
3661 ret = false;
3662 continue;
3665 if (r_type == R_PPC_EMB_SDA21)
3666 { /* fill in register field */
3667 insn = bfd_get_32 (output_bfd, contents + offset);
3668 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
3669 bfd_put_32 (output_bfd, insn, contents + offset);
3672 break;
3674 /* Relocate against the beginning of the section */
3675 case (int)R_PPC_SECTOFF:
3676 case (int)R_PPC_SECTOFF_LO:
3677 case (int)R_PPC_SECTOFF_HI:
3678 BFD_ASSERT (sec != (asection *)0);
3679 addend -= sec->output_section->vma;
3680 break;
3682 case (int)R_PPC_SECTOFF_HA:
3683 BFD_ASSERT (sec != (asection *)0);
3684 addend -= sec->output_section->vma;
3685 addend += ((relocation + addend) & 0x8000) << 1;
3686 break;
3688 /* Negative relocations */
3689 case (int)R_PPC_EMB_NADDR32:
3690 case (int)R_PPC_EMB_NADDR16:
3691 case (int)R_PPC_EMB_NADDR16_LO:
3692 case (int)R_PPC_EMB_NADDR16_HI:
3693 addend -= 2*relocation;
3694 break;
3696 case (int)R_PPC_EMB_NADDR16_HA:
3697 addend -= 2*relocation;
3698 addend += ((relocation + addend) & 0x8000) << 1;
3699 break;
3701 /* NOP relocation that prevents garbage collecting linkers from omitting a
3702 reference. */
3703 case (int)R_PPC_EMB_MRKREF:
3704 continue;
3706 case (int)R_PPC_COPY:
3707 case (int)R_PPC_GLOB_DAT:
3708 case (int)R_PPC_JMP_SLOT:
3709 case (int)R_PPC_RELATIVE:
3710 case (int)R_PPC_PLT32:
3711 case (int)R_PPC_PLTREL32:
3712 case (int)R_PPC_PLT16_LO:
3713 case (int)R_PPC_PLT16_HI:
3714 case (int)R_PPC_PLT16_HA:
3715 case (int)R_PPC_EMB_RELSEC16:
3716 case (int)R_PPC_EMB_RELST_LO:
3717 case (int)R_PPC_EMB_RELST_HI:
3718 case (int)R_PPC_EMB_RELST_HA:
3719 case (int)R_PPC_EMB_BIT_FLD:
3720 (*_bfd_error_handler) (_("%s: Relocation %s is not yet supported for symbol %s."),
3721 bfd_get_filename (input_bfd),
3722 ppc_elf_howto_table[ (int)r_type ]->name,
3723 sym_name);
3725 bfd_set_error (bfd_error_invalid_operation);
3726 ret = false;
3727 continue;
3729 case (int)R_PPC_GNU_VTINHERIT:
3730 case (int)R_PPC_GNU_VTENTRY:
3731 /* These are no-ops in the end. */
3732 continue;
3736 #ifdef DEBUG
3737 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
3738 howto->name,
3739 (int)r_type,
3740 sym_name,
3741 r_symndx,
3742 (long)offset,
3743 (long)addend);
3744 #endif
3746 r = _bfd_final_link_relocate (howto,
3747 input_bfd,
3748 input_section,
3749 contents,
3750 offset,
3751 relocation,
3752 addend);
3754 if (r != bfd_reloc_ok)
3756 ret = false;
3757 switch (r)
3759 default:
3760 break;
3762 case bfd_reloc_overflow:
3764 const char *name;
3766 if (h != NULL)
3767 name = h->root.root.string;
3768 else
3770 name = bfd_elf_string_from_elf_section (input_bfd,
3771 symtab_hdr->sh_link,
3772 sym->st_name);
3773 if (name == NULL)
3774 break;
3776 if (*name == '\0')
3777 name = bfd_section_name (input_bfd, sec);
3780 (*info->callbacks->reloc_overflow)(info,
3781 name,
3782 howto->name,
3783 (bfd_vma) 0,
3784 input_bfd,
3785 input_section,
3786 offset);
3788 break;
3795 #ifdef DEBUG
3796 fprintf (stderr, "\n");
3797 #endif
3799 return ret;
3803 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
3804 #define TARGET_LITTLE_NAME "elf32-powerpcle"
3805 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
3806 #define TARGET_BIG_NAME "elf32-powerpc"
3807 #define ELF_ARCH bfd_arch_powerpc
3808 #define ELF_MACHINE_CODE EM_PPC
3809 #define ELF_MAXPAGESIZE 0x10000
3810 #define elf_info_to_howto ppc_elf_info_to_howto
3812 #ifdef EM_CYGNUS_POWERPC
3813 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
3814 #endif
3816 #ifdef EM_PPC_OLD
3817 #define ELF_MACHINE_ALT2 EM_PPC_OLD
3818 #endif
3820 #define elf_backend_plt_not_loaded 1
3821 #define elf_backend_got_symbol_offset 4
3822 #define elf_backend_can_gc_sections 1
3823 #define elf_backend_got_header_size 12
3824 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
3826 #define bfd_elf32_bfd_copy_private_bfd_data ppc_elf_copy_private_bfd_data
3827 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
3828 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
3829 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
3830 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
3831 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
3833 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
3834 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
3835 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
3836 #define elf_backend_relocate_section ppc_elf_relocate_section
3837 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
3838 #define elf_backend_check_relocs ppc_elf_check_relocs
3839 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
3840 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
3841 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
3842 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
3843 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
3844 #define elf_backend_fake_sections ppc_elf_fake_sections
3845 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
3846 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
3848 #include "elf32-target.h"