* reloc.c: Add BFD_RELOC_XSTORMY16_12.
[binutils.git] / bfd / elf32-ppc.c
blob2406c6751b5dcf8c6f33a3f3b3fcc199345ced0a
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file is based on a preliminary PowerPC ELF ABI. The
23 information may not match the final PowerPC ELF ABI. It includes
24 suggestions from the in-progress Embedded PowerPC ABI, and that
25 information may also not match. */
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "bfdlink.h"
30 #include "libbfd.h"
31 #include "elf-bfd.h"
32 #include "elf/ppc.h"
34 /* RELA relocations are used here. */
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, Elf_Internal_Rela *dst));
40 static void ppc_elf_howto_init
41 PARAMS ((void));
42 static int ppc_elf_sort_rela
43 PARAMS ((const PTR, const PTR));
44 static bfd_boolean ppc_elf_relax_section
45 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
46 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
47 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
48 static bfd_boolean ppc_elf_object_p
49 PARAMS ((bfd *));
50 static bfd_boolean ppc_elf_set_private_flags
51 PARAMS ((bfd *, flagword));
52 static bfd_boolean ppc_elf_merge_private_bfd_data
53 PARAMS ((bfd *, bfd *));
54 static int ppc_elf_additional_program_headers
55 PARAMS ((bfd *));
56 static bfd_boolean ppc_elf_modify_segment_map
57 PARAMS ((bfd *));
58 static asection *ppc_elf_create_got
59 PARAMS ((bfd *, struct bfd_link_info *));
60 static bfd_boolean ppc_elf_create_dynamic_sections
61 PARAMS ((bfd *, struct bfd_link_info *));
62 static bfd_boolean ppc_elf_section_from_shdr
63 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
64 static bfd_boolean ppc_elf_fake_sections
65 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
66 static elf_linker_section_t *ppc_elf_create_linker_section
67 PARAMS ((bfd *abfd, struct bfd_link_info *info,
68 enum elf_linker_section_enum));
69 static bfd_boolean ppc_elf_check_relocs
70 PARAMS ((bfd *, struct bfd_link_info *, asection *,
71 const Elf_Internal_Rela *));
72 static asection * ppc_elf_gc_mark_hook
73 PARAMS ((asection *sec, struct bfd_link_info *info, Elf_Internal_Rela *rel,
74 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
75 static bfd_boolean ppc_elf_gc_sweep_hook
76 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
77 const Elf_Internal_Rela *relocs));
78 static bfd_boolean ppc_elf_adjust_dynamic_symbol
79 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
80 static bfd_boolean ppc_elf_size_dynamic_sections
81 PARAMS ((bfd *, struct bfd_link_info *));
82 static bfd_boolean ppc_elf_relocate_section
83 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
84 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
85 asection **));
86 static bfd_boolean ppc_elf_add_symbol_hook
87 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
88 const char **, flagword *, asection **, bfd_vma *));
89 static bfd_boolean ppc_elf_finish_dynamic_symbol
90 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
91 Elf_Internal_Sym *));
92 static bfd_boolean ppc_elf_finish_dynamic_sections
93 PARAMS ((bfd *, struct bfd_link_info *));
94 static enum elf_reloc_type_class ppc_elf_reloc_type_class
95 PARAMS ((const Elf_Internal_Rela *));
96 static bfd_boolean ppc_elf_grok_prstatus
97 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
98 static bfd_boolean ppc_elf_grok_psinfo
99 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
101 #define BRANCH_PREDICT_BIT 0x200000 /* branch prediction bit for branch taken relocs */
102 #define RA_REGISTER_MASK 0x001f0000 /* mask to set RA in memory instructions */
103 #define RA_REGISTER_SHIFT 16 /* value to shift register by to insert RA */
105 /* The name of the dynamic interpreter. This is put in the .interp
106 section. */
108 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
110 /* The size in bytes of an entry in the procedure linkage table. */
111 #define PLT_ENTRY_SIZE 12
112 /* The initial size of the plt reserved for the dynamic linker. */
113 #define PLT_INITIAL_ENTRY_SIZE 72
114 /* The size of the gap between entries in the PLT. */
115 #define PLT_SLOT_SIZE 8
116 /* The number of single-slot PLT entries (the rest use two slots). */
117 #define PLT_NUM_SINGLE_ENTRIES 8192
119 /* Will references to this symbol always reference the symbol
120 in this object? */
121 #define SYMBOL_REFERENCES_LOCAL(INFO, H) \
122 ((! INFO->shared \
123 || INFO->symbolic \
124 || H->dynindx == -1 \
125 || ELF_ST_VISIBILITY (H->other) == STV_INTERNAL \
126 || ELF_ST_VISIBILITY (H->other) == STV_HIDDEN) \
127 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
129 /* Will _calls_ to this symbol always call the version in this object? */
130 #define SYMBOL_CALLS_LOCAL(INFO, H) \
131 ((! INFO->shared \
132 || INFO->symbolic \
133 || H->dynindx == -1 \
134 || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT) \
135 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
137 static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
139 static reloc_howto_type ppc_elf_howto_raw[] = {
140 /* This reloc does nothing. */
141 HOWTO (R_PPC_NONE, /* type */
142 0, /* rightshift */
143 2, /* size (0 = byte, 1 = short, 2 = long) */
144 32, /* bitsize */
145 FALSE, /* pc_relative */
146 0, /* bitpos */
147 complain_overflow_bitfield, /* complain_on_overflow */
148 bfd_elf_generic_reloc, /* special_function */
149 "R_PPC_NONE", /* name */
150 FALSE, /* partial_inplace */
151 0, /* src_mask */
152 0, /* dst_mask */
153 FALSE), /* pcrel_offset */
155 /* A standard 32 bit relocation. */
156 HOWTO (R_PPC_ADDR32, /* type */
157 0, /* rightshift */
158 2, /* size (0 = byte, 1 = short, 2 = long) */
159 32, /* bitsize */
160 FALSE, /* pc_relative */
161 0, /* bitpos */
162 complain_overflow_bitfield, /* complain_on_overflow */
163 bfd_elf_generic_reloc, /* special_function */
164 "R_PPC_ADDR32", /* name */
165 FALSE, /* partial_inplace */
166 0, /* src_mask */
167 0xffffffff, /* dst_mask */
168 FALSE), /* pcrel_offset */
170 /* An absolute 26 bit branch; the lower two bits must be zero.
171 FIXME: we don't check that, we just clear them. */
172 HOWTO (R_PPC_ADDR24, /* type */
173 0, /* rightshift */
174 2, /* size (0 = byte, 1 = short, 2 = long) */
175 26, /* bitsize */
176 FALSE, /* pc_relative */
177 0, /* bitpos */
178 complain_overflow_bitfield, /* complain_on_overflow */
179 bfd_elf_generic_reloc, /* special_function */
180 "R_PPC_ADDR24", /* name */
181 FALSE, /* partial_inplace */
182 0, /* src_mask */
183 0x3fffffc, /* dst_mask */
184 FALSE), /* pcrel_offset */
186 /* A standard 16 bit relocation. */
187 HOWTO (R_PPC_ADDR16, /* type */
188 0, /* rightshift */
189 1, /* size (0 = byte, 1 = short, 2 = long) */
190 16, /* bitsize */
191 FALSE, /* pc_relative */
192 0, /* bitpos */
193 complain_overflow_bitfield, /* complain_on_overflow */
194 bfd_elf_generic_reloc, /* special_function */
195 "R_PPC_ADDR16", /* name */
196 FALSE, /* partial_inplace */
197 0, /* src_mask */
198 0xffff, /* dst_mask */
199 FALSE), /* pcrel_offset */
201 /* A 16 bit relocation without overflow. */
202 HOWTO (R_PPC_ADDR16_LO, /* type */
203 0, /* rightshift */
204 1, /* size (0 = byte, 1 = short, 2 = long) */
205 16, /* bitsize */
206 FALSE, /* pc_relative */
207 0, /* bitpos */
208 complain_overflow_dont,/* complain_on_overflow */
209 bfd_elf_generic_reloc, /* special_function */
210 "R_PPC_ADDR16_LO", /* name */
211 FALSE, /* partial_inplace */
212 0, /* src_mask */
213 0xffff, /* dst_mask */
214 FALSE), /* pcrel_offset */
216 /* The high order 16 bits of an address. */
217 HOWTO (R_PPC_ADDR16_HI, /* type */
218 16, /* rightshift */
219 1, /* size (0 = byte, 1 = short, 2 = long) */
220 16, /* bitsize */
221 FALSE, /* pc_relative */
222 0, /* bitpos */
223 complain_overflow_dont, /* complain_on_overflow */
224 bfd_elf_generic_reloc, /* special_function */
225 "R_PPC_ADDR16_HI", /* name */
226 FALSE, /* partial_inplace */
227 0, /* src_mask */
228 0xffff, /* dst_mask */
229 FALSE), /* pcrel_offset */
231 /* The high order 16 bits of an address, plus 1 if the contents of
232 the low 16 bits, treated as a signed number, is negative. */
233 HOWTO (R_PPC_ADDR16_HA, /* type */
234 16, /* rightshift */
235 1, /* size (0 = byte, 1 = short, 2 = long) */
236 16, /* bitsize */
237 FALSE, /* pc_relative */
238 0, /* bitpos */
239 complain_overflow_dont, /* complain_on_overflow */
240 ppc_elf_addr16_ha_reloc, /* special_function */
241 "R_PPC_ADDR16_HA", /* name */
242 FALSE, /* partial_inplace */
243 0, /* src_mask */
244 0xffff, /* dst_mask */
245 FALSE), /* pcrel_offset */
247 /* An absolute 16 bit branch; the lower two bits must be zero.
248 FIXME: we don't check that, we just clear them. */
249 HOWTO (R_PPC_ADDR14, /* type */
250 0, /* rightshift */
251 2, /* size (0 = byte, 1 = short, 2 = long) */
252 16, /* bitsize */
253 FALSE, /* pc_relative */
254 0, /* bitpos */
255 complain_overflow_bitfield, /* complain_on_overflow */
256 bfd_elf_generic_reloc, /* special_function */
257 "R_PPC_ADDR14", /* name */
258 FALSE, /* partial_inplace */
259 0, /* src_mask */
260 0xfffc, /* dst_mask */
261 FALSE), /* pcrel_offset */
263 /* An absolute 16 bit branch, for which bit 10 should be set to
264 indicate that the branch is expected to be taken. The lower two
265 bits must be zero. */
266 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
267 0, /* rightshift */
268 2, /* size (0 = byte, 1 = short, 2 = long) */
269 16, /* bitsize */
270 FALSE, /* pc_relative */
271 0, /* bitpos */
272 complain_overflow_bitfield, /* complain_on_overflow */
273 bfd_elf_generic_reloc, /* special_function */
274 "R_PPC_ADDR14_BRTAKEN",/* name */
275 FALSE, /* partial_inplace */
276 0, /* src_mask */
277 0xfffc, /* dst_mask */
278 FALSE), /* pcrel_offset */
280 /* An absolute 16 bit branch, for which bit 10 should be set to
281 indicate that the branch is not expected to be taken. The lower
282 two bits must be zero. */
283 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
284 0, /* rightshift */
285 2, /* size (0 = byte, 1 = short, 2 = long) */
286 16, /* bitsize */
287 FALSE, /* pc_relative */
288 0, /* bitpos */
289 complain_overflow_bitfield, /* complain_on_overflow */
290 bfd_elf_generic_reloc, /* special_function */
291 "R_PPC_ADDR14_BRNTAKEN",/* name */
292 FALSE, /* partial_inplace */
293 0, /* src_mask */
294 0xfffc, /* dst_mask */
295 FALSE), /* pcrel_offset */
297 /* A relative 26 bit branch; the lower two bits must be zero. */
298 HOWTO (R_PPC_REL24, /* type */
299 0, /* rightshift */
300 2, /* size (0 = byte, 1 = short, 2 = long) */
301 26, /* bitsize */
302 TRUE, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_signed, /* complain_on_overflow */
305 bfd_elf_generic_reloc, /* special_function */
306 "R_PPC_REL24", /* name */
307 FALSE, /* partial_inplace */
308 0, /* src_mask */
309 0x3fffffc, /* dst_mask */
310 TRUE), /* pcrel_offset */
312 /* A relative 16 bit branch; the lower two bits must be zero. */
313 HOWTO (R_PPC_REL14, /* type */
314 0, /* rightshift */
315 2, /* size (0 = byte, 1 = short, 2 = long) */
316 16, /* bitsize */
317 TRUE, /* pc_relative */
318 0, /* bitpos */
319 complain_overflow_signed, /* complain_on_overflow */
320 bfd_elf_generic_reloc, /* special_function */
321 "R_PPC_REL14", /* name */
322 FALSE, /* partial_inplace */
323 0, /* src_mask */
324 0xfffc, /* dst_mask */
325 TRUE), /* pcrel_offset */
327 /* A relative 16 bit branch. Bit 10 should be set to indicate that
328 the branch is expected to be taken. The lower two bits must be
329 zero. */
330 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
331 0, /* rightshift */
332 2, /* size (0 = byte, 1 = short, 2 = long) */
333 16, /* bitsize */
334 TRUE, /* pc_relative */
335 0, /* bitpos */
336 complain_overflow_signed, /* complain_on_overflow */
337 bfd_elf_generic_reloc, /* special_function */
338 "R_PPC_REL14_BRTAKEN", /* name */
339 FALSE, /* partial_inplace */
340 0, /* src_mask */
341 0xfffc, /* dst_mask */
342 TRUE), /* pcrel_offset */
344 /* A relative 16 bit branch. Bit 10 should be set to indicate that
345 the branch is not expected to be taken. The lower two bits must
346 be zero. */
347 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
348 0, /* rightshift */
349 2, /* size (0 = byte, 1 = short, 2 = long) */
350 16, /* bitsize */
351 TRUE, /* pc_relative */
352 0, /* bitpos */
353 complain_overflow_signed, /* complain_on_overflow */
354 bfd_elf_generic_reloc, /* special_function */
355 "R_PPC_REL14_BRNTAKEN",/* name */
356 FALSE, /* partial_inplace */
357 0, /* src_mask */
358 0xfffc, /* dst_mask */
359 TRUE), /* pcrel_offset */
361 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
362 symbol. */
363 HOWTO (R_PPC_GOT16, /* type */
364 0, /* rightshift */
365 1, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
367 FALSE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_signed, /* complain_on_overflow */
370 bfd_elf_generic_reloc, /* special_function */
371 "R_PPC_GOT16", /* name */
372 FALSE, /* partial_inplace */
373 0, /* src_mask */
374 0xffff, /* dst_mask */
375 FALSE), /* pcrel_offset */
377 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
378 the symbol. */
379 HOWTO (R_PPC_GOT16_LO, /* type */
380 0, /* rightshift */
381 1, /* size (0 = byte, 1 = short, 2 = long) */
382 16, /* bitsize */
383 FALSE, /* pc_relative */
384 0, /* bitpos */
385 complain_overflow_dont, /* complain_on_overflow */
386 bfd_elf_generic_reloc, /* special_function */
387 "R_PPC_GOT16_LO", /* name */
388 FALSE, /* partial_inplace */
389 0, /* src_mask */
390 0xffff, /* dst_mask */
391 FALSE), /* pcrel_offset */
393 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
394 the symbol. */
395 HOWTO (R_PPC_GOT16_HI, /* type */
396 16, /* rightshift */
397 1, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
399 FALSE, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_bitfield, /* complain_on_overflow */
402 bfd_elf_generic_reloc, /* special_function */
403 "R_PPC_GOT16_HI", /* name */
404 FALSE, /* partial_inplace */
405 0, /* src_mask */
406 0xffff, /* dst_mask */
407 FALSE), /* pcrel_offset */
409 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
410 the symbol. */
411 HOWTO (R_PPC_GOT16_HA, /* type */
412 16, /* rightshift */
413 1, /* size (0 = byte, 1 = short, 2 = long) */
414 16, /* bitsize */
415 FALSE, /* pc_relative */
416 0, /* bitpos */
417 complain_overflow_bitfield, /* complain_on_overflow */
418 ppc_elf_addr16_ha_reloc, /* special_function */
419 "R_PPC_GOT16_HA", /* name */
420 FALSE, /* partial_inplace */
421 0, /* src_mask */
422 0xffff, /* dst_mask */
423 FALSE), /* pcrel_offset */
425 /* Like R_PPC_REL24, but referring to the procedure linkage table
426 entry for the symbol. */
427 HOWTO (R_PPC_PLTREL24, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 26, /* bitsize */
431 TRUE, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
434 bfd_elf_generic_reloc, /* special_function */
435 "R_PPC_PLTREL24", /* name */
436 FALSE, /* partial_inplace */
437 0, /* src_mask */
438 0x3fffffc, /* dst_mask */
439 TRUE), /* pcrel_offset */
441 /* This is used only by the dynamic linker. The symbol should exist
442 both in the object being run and in some shared library. The
443 dynamic linker copies the data addressed by the symbol from the
444 shared library into the object, because the object being
445 run has to have the data at some particular address. */
446 HOWTO (R_PPC_COPY, /* type */
447 0, /* rightshift */
448 2, /* size (0 = byte, 1 = short, 2 = long) */
449 32, /* bitsize */
450 FALSE, /* pc_relative */
451 0, /* bitpos */
452 complain_overflow_bitfield, /* complain_on_overflow */
453 bfd_elf_generic_reloc, /* special_function */
454 "R_PPC_COPY", /* name */
455 FALSE, /* partial_inplace */
456 0, /* src_mask */
457 0, /* dst_mask */
458 FALSE), /* pcrel_offset */
460 /* Like R_PPC_ADDR32, but used when setting global offset table
461 entries. */
462 HOWTO (R_PPC_GLOB_DAT, /* type */
463 0, /* rightshift */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
465 32, /* bitsize */
466 FALSE, /* pc_relative */
467 0, /* bitpos */
468 complain_overflow_bitfield, /* complain_on_overflow */
469 bfd_elf_generic_reloc, /* special_function */
470 "R_PPC_GLOB_DAT", /* name */
471 FALSE, /* partial_inplace */
472 0, /* src_mask */
473 0xffffffff, /* dst_mask */
474 FALSE), /* pcrel_offset */
476 /* Marks a procedure linkage table entry for a symbol. */
477 HOWTO (R_PPC_JMP_SLOT, /* type */
478 0, /* rightshift */
479 2, /* size (0 = byte, 1 = short, 2 = long) */
480 32, /* bitsize */
481 FALSE, /* pc_relative */
482 0, /* bitpos */
483 complain_overflow_bitfield, /* complain_on_overflow */
484 bfd_elf_generic_reloc, /* special_function */
485 "R_PPC_JMP_SLOT", /* name */
486 FALSE, /* partial_inplace */
487 0, /* src_mask */
488 0, /* dst_mask */
489 FALSE), /* pcrel_offset */
491 /* Used only by the dynamic linker. When the object is run, this
492 longword is set to the load address of the object, plus the
493 addend. */
494 HOWTO (R_PPC_RELATIVE, /* type */
495 0, /* rightshift */
496 2, /* size (0 = byte, 1 = short, 2 = long) */
497 32, /* bitsize */
498 FALSE, /* pc_relative */
499 0, /* bitpos */
500 complain_overflow_bitfield, /* complain_on_overflow */
501 bfd_elf_generic_reloc, /* special_function */
502 "R_PPC_RELATIVE", /* name */
503 FALSE, /* partial_inplace */
504 0, /* src_mask */
505 0xffffffff, /* dst_mask */
506 FALSE), /* pcrel_offset */
508 /* Like R_PPC_REL24, but uses the value of the symbol within the
509 object rather than the final value. Normally used for
510 _GLOBAL_OFFSET_TABLE_. */
511 HOWTO (R_PPC_LOCAL24PC, /* type */
512 0, /* rightshift */
513 2, /* size (0 = byte, 1 = short, 2 = long) */
514 26, /* bitsize */
515 TRUE, /* pc_relative */
516 0, /* bitpos */
517 complain_overflow_signed, /* complain_on_overflow */
518 bfd_elf_generic_reloc, /* special_function */
519 "R_PPC_LOCAL24PC", /* name */
520 FALSE, /* partial_inplace */
521 0, /* src_mask */
522 0x3fffffc, /* dst_mask */
523 TRUE), /* pcrel_offset */
525 /* Like R_PPC_ADDR32, but may be unaligned. */
526 HOWTO (R_PPC_UADDR32, /* type */
527 0, /* rightshift */
528 2, /* size (0 = byte, 1 = short, 2 = long) */
529 32, /* bitsize */
530 FALSE, /* pc_relative */
531 0, /* bitpos */
532 complain_overflow_bitfield, /* complain_on_overflow */
533 bfd_elf_generic_reloc, /* special_function */
534 "R_PPC_UADDR32", /* name */
535 FALSE, /* partial_inplace */
536 0, /* src_mask */
537 0xffffffff, /* dst_mask */
538 FALSE), /* pcrel_offset */
540 /* Like R_PPC_ADDR16, but may be unaligned. */
541 HOWTO (R_PPC_UADDR16, /* type */
542 0, /* rightshift */
543 1, /* size (0 = byte, 1 = short, 2 = long) */
544 16, /* bitsize */
545 FALSE, /* pc_relative */
546 0, /* bitpos */
547 complain_overflow_bitfield, /* complain_on_overflow */
548 bfd_elf_generic_reloc, /* special_function */
549 "R_PPC_UADDR16", /* name */
550 FALSE, /* partial_inplace */
551 0, /* src_mask */
552 0xffff, /* dst_mask */
553 FALSE), /* pcrel_offset */
555 /* 32-bit PC relative */
556 HOWTO (R_PPC_REL32, /* type */
557 0, /* rightshift */
558 2, /* size (0 = byte, 1 = short, 2 = long) */
559 32, /* bitsize */
560 TRUE, /* pc_relative */
561 0, /* bitpos */
562 complain_overflow_bitfield, /* complain_on_overflow */
563 bfd_elf_generic_reloc, /* special_function */
564 "R_PPC_REL32", /* name */
565 FALSE, /* partial_inplace */
566 0, /* src_mask */
567 0xffffffff, /* dst_mask */
568 TRUE), /* pcrel_offset */
570 /* 32-bit relocation to the symbol's procedure linkage table.
571 FIXME: not supported. */
572 HOWTO (R_PPC_PLT32, /* type */
573 0, /* rightshift */
574 2, /* size (0 = byte, 1 = short, 2 = long) */
575 32, /* bitsize */
576 FALSE, /* pc_relative */
577 0, /* bitpos */
578 complain_overflow_bitfield, /* complain_on_overflow */
579 bfd_elf_generic_reloc, /* special_function */
580 "R_PPC_PLT32", /* name */
581 FALSE, /* partial_inplace */
582 0, /* src_mask */
583 0, /* dst_mask */
584 FALSE), /* pcrel_offset */
586 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
587 FIXME: not supported. */
588 HOWTO (R_PPC_PLTREL32, /* type */
589 0, /* rightshift */
590 2, /* size (0 = byte, 1 = short, 2 = long) */
591 32, /* bitsize */
592 TRUE, /* pc_relative */
593 0, /* bitpos */
594 complain_overflow_bitfield, /* complain_on_overflow */
595 bfd_elf_generic_reloc, /* special_function */
596 "R_PPC_PLTREL32", /* name */
597 FALSE, /* partial_inplace */
598 0, /* src_mask */
599 0, /* dst_mask */
600 TRUE), /* pcrel_offset */
602 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
603 the symbol. */
604 HOWTO (R_PPC_PLT16_LO, /* type */
605 0, /* rightshift */
606 1, /* size (0 = byte, 1 = short, 2 = long) */
607 16, /* bitsize */
608 FALSE, /* pc_relative */
609 0, /* bitpos */
610 complain_overflow_dont, /* complain_on_overflow */
611 bfd_elf_generic_reloc, /* special_function */
612 "R_PPC_PLT16_LO", /* name */
613 FALSE, /* partial_inplace */
614 0, /* src_mask */
615 0xffff, /* dst_mask */
616 FALSE), /* pcrel_offset */
618 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
619 the symbol. */
620 HOWTO (R_PPC_PLT16_HI, /* type */
621 16, /* rightshift */
622 1, /* size (0 = byte, 1 = short, 2 = long) */
623 16, /* bitsize */
624 FALSE, /* pc_relative */
625 0, /* bitpos */
626 complain_overflow_bitfield, /* complain_on_overflow */
627 bfd_elf_generic_reloc, /* special_function */
628 "R_PPC_PLT16_HI", /* name */
629 FALSE, /* partial_inplace */
630 0, /* src_mask */
631 0xffff, /* dst_mask */
632 FALSE), /* pcrel_offset */
634 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
635 the symbol. */
636 HOWTO (R_PPC_PLT16_HA, /* type */
637 16, /* rightshift */
638 1, /* size (0 = byte, 1 = short, 2 = long) */
639 16, /* bitsize */
640 FALSE, /* pc_relative */
641 0, /* bitpos */
642 complain_overflow_bitfield, /* complain_on_overflow */
643 ppc_elf_addr16_ha_reloc, /* special_function */
644 "R_PPC_PLT16_HA", /* name */
645 FALSE, /* partial_inplace */
646 0, /* src_mask */
647 0xffff, /* dst_mask */
648 FALSE), /* pcrel_offset */
650 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
651 small data items. */
652 HOWTO (R_PPC_SDAREL16, /* type */
653 0, /* rightshift */
654 1, /* size (0 = byte, 1 = short, 2 = long) */
655 16, /* bitsize */
656 FALSE, /* pc_relative */
657 0, /* bitpos */
658 complain_overflow_signed, /* complain_on_overflow */
659 bfd_elf_generic_reloc, /* special_function */
660 "R_PPC_SDAREL16", /* name */
661 FALSE, /* partial_inplace */
662 0, /* src_mask */
663 0xffff, /* dst_mask */
664 FALSE), /* pcrel_offset */
666 /* 16-bit section relative relocation. */
667 HOWTO (R_PPC_SECTOFF, /* type */
668 0, /* rightshift */
669 1, /* size (0 = byte, 1 = short, 2 = long) */
670 16, /* bitsize */
671 FALSE, /* pc_relative */
672 0, /* bitpos */
673 complain_overflow_bitfield, /* complain_on_overflow */
674 bfd_elf_generic_reloc, /* special_function */
675 "R_PPC_SECTOFF", /* name */
676 FALSE, /* partial_inplace */
677 0, /* src_mask */
678 0xffff, /* dst_mask */
679 FALSE), /* pcrel_offset */
681 /* 16-bit lower half section relative relocation. */
682 HOWTO (R_PPC_SECTOFF_LO, /* type */
683 0, /* rightshift */
684 1, /* size (0 = byte, 1 = short, 2 = long) */
685 16, /* bitsize */
686 FALSE, /* pc_relative */
687 0, /* bitpos */
688 complain_overflow_dont, /* complain_on_overflow */
689 bfd_elf_generic_reloc, /* special_function */
690 "R_PPC_SECTOFF_LO", /* name */
691 FALSE, /* partial_inplace */
692 0, /* src_mask */
693 0xffff, /* dst_mask */
694 FALSE), /* pcrel_offset */
696 /* 16-bit upper half section relative relocation. */
697 HOWTO (R_PPC_SECTOFF_HI, /* type */
698 16, /* rightshift */
699 1, /* size (0 = byte, 1 = short, 2 = long) */
700 16, /* bitsize */
701 FALSE, /* pc_relative */
702 0, /* bitpos */
703 complain_overflow_bitfield, /* complain_on_overflow */
704 bfd_elf_generic_reloc, /* special_function */
705 "R_PPC_SECTOFF_HI", /* name */
706 FALSE, /* partial_inplace */
707 0, /* src_mask */
708 0xffff, /* dst_mask */
709 FALSE), /* pcrel_offset */
711 /* 16-bit upper half adjusted section relative relocation. */
712 HOWTO (R_PPC_SECTOFF_HA, /* type */
713 16, /* rightshift */
714 1, /* size (0 = byte, 1 = short, 2 = long) */
715 16, /* bitsize */
716 FALSE, /* pc_relative */
717 0, /* bitpos */
718 complain_overflow_bitfield, /* complain_on_overflow */
719 ppc_elf_addr16_ha_reloc, /* special_function */
720 "R_PPC_SECTOFF_HA", /* name */
721 FALSE, /* partial_inplace */
722 0, /* src_mask */
723 0xffff, /* dst_mask */
724 FALSE), /* pcrel_offset */
726 /* The remaining relocs are from the Embedded ELF ABI, and are not
727 in the SVR4 ELF ABI. */
729 /* 32 bit value resulting from the addend minus the symbol */
730 HOWTO (R_PPC_EMB_NADDR32, /* type */
731 0, /* rightshift */
732 2, /* size (0 = byte, 1 = short, 2 = long) */
733 32, /* bitsize */
734 FALSE, /* pc_relative */
735 0, /* bitpos */
736 complain_overflow_bitfield, /* complain_on_overflow */
737 bfd_elf_generic_reloc, /* special_function */
738 "R_PPC_EMB_NADDR32", /* name */
739 FALSE, /* partial_inplace */
740 0, /* src_mask */
741 0xffffffff, /* dst_mask */
742 FALSE), /* pcrel_offset */
744 /* 16 bit value resulting from the addend minus the symbol */
745 HOWTO (R_PPC_EMB_NADDR16, /* type */
746 0, /* rightshift */
747 1, /* size (0 = byte, 1 = short, 2 = long) */
748 16, /* bitsize */
749 FALSE, /* pc_relative */
750 0, /* bitpos */
751 complain_overflow_bitfield, /* complain_on_overflow */
752 bfd_elf_generic_reloc, /* special_function */
753 "R_PPC_EMB_NADDR16", /* name */
754 FALSE, /* partial_inplace */
755 0, /* src_mask */
756 0xffff, /* dst_mask */
757 FALSE), /* pcrel_offset */
759 /* 16 bit value resulting from the addend minus the symbol */
760 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
761 0, /* rightshift */
762 1, /* size (0 = byte, 1 = short, 2 = long) */
763 16, /* bitsize */
764 FALSE, /* pc_relative */
765 0, /* bitpos */
766 complain_overflow_dont,/* complain_on_overflow */
767 bfd_elf_generic_reloc, /* special_function */
768 "R_PPC_EMB_ADDR16_LO", /* name */
769 FALSE, /* partial_inplace */
770 0, /* src_mask */
771 0xffff, /* dst_mask */
772 FALSE), /* pcrel_offset */
774 /* The high order 16 bits of the addend minus the symbol */
775 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
776 16, /* rightshift */
777 1, /* size (0 = byte, 1 = short, 2 = long) */
778 16, /* bitsize */
779 FALSE, /* pc_relative */
780 0, /* bitpos */
781 complain_overflow_dont, /* complain_on_overflow */
782 bfd_elf_generic_reloc, /* special_function */
783 "R_PPC_EMB_NADDR16_HI", /* name */
784 FALSE, /* partial_inplace */
785 0, /* src_mask */
786 0xffff, /* dst_mask */
787 FALSE), /* pcrel_offset */
789 /* The high order 16 bits of the result of the addend minus the address,
790 plus 1 if the contents of the low 16 bits, treated as a signed number,
791 is negative. */
792 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
793 16, /* rightshift */
794 1, /* size (0 = byte, 1 = short, 2 = long) */
795 16, /* bitsize */
796 FALSE, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_dont, /* complain_on_overflow */
799 ppc_elf_addr16_ha_reloc, /* special_function */
800 "R_PPC_EMB_NADDR16_HA", /* name */
801 FALSE, /* partial_inplace */
802 0, /* src_mask */
803 0xffff, /* dst_mask */
804 FALSE), /* pcrel_offset */
806 /* 16 bit value resulting from allocating a 4 byte word to hold an
807 address in the .sdata section, and returning the offset from
808 _SDA_BASE_ for that relocation */
809 HOWTO (R_PPC_EMB_SDAI16, /* type */
810 0, /* rightshift */
811 1, /* size (0 = byte, 1 = short, 2 = long) */
812 16, /* bitsize */
813 FALSE, /* pc_relative */
814 0, /* bitpos */
815 complain_overflow_bitfield, /* complain_on_overflow */
816 bfd_elf_generic_reloc, /* special_function */
817 "R_PPC_EMB_SDAI16", /* name */
818 FALSE, /* partial_inplace */
819 0, /* src_mask */
820 0xffff, /* dst_mask */
821 FALSE), /* pcrel_offset */
823 /* 16 bit value resulting from allocating a 4 byte word to hold an
824 address in the .sdata2 section, and returning the offset from
825 _SDA2_BASE_ for that relocation */
826 HOWTO (R_PPC_EMB_SDA2I16, /* type */
827 0, /* rightshift */
828 1, /* size (0 = byte, 1 = short, 2 = long) */
829 16, /* bitsize */
830 FALSE, /* pc_relative */
831 0, /* bitpos */
832 complain_overflow_bitfield, /* complain_on_overflow */
833 bfd_elf_generic_reloc, /* special_function */
834 "R_PPC_EMB_SDA2I16", /* name */
835 FALSE, /* partial_inplace */
836 0, /* src_mask */
837 0xffff, /* dst_mask */
838 FALSE), /* pcrel_offset */
840 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
841 small data items. */
842 HOWTO (R_PPC_EMB_SDA2REL, /* type */
843 0, /* rightshift */
844 1, /* size (0 = byte, 1 = short, 2 = long) */
845 16, /* bitsize */
846 FALSE, /* pc_relative */
847 0, /* bitpos */
848 complain_overflow_signed, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_PPC_EMB_SDA2REL", /* name */
851 FALSE, /* partial_inplace */
852 0, /* src_mask */
853 0xffff, /* dst_mask */
854 FALSE), /* pcrel_offset */
856 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
857 signed offset from the appropriate base, and filling in the register
858 field with the appropriate register (0, 2, or 13). */
859 HOWTO (R_PPC_EMB_SDA21, /* type */
860 0, /* rightshift */
861 2, /* size (0 = byte, 1 = short, 2 = long) */
862 16, /* bitsize */
863 FALSE, /* pc_relative */
864 0, /* bitpos */
865 complain_overflow_signed, /* complain_on_overflow */
866 bfd_elf_generic_reloc, /* special_function */
867 "R_PPC_EMB_SDA21", /* name */
868 FALSE, /* partial_inplace */
869 0, /* src_mask */
870 0xffff, /* dst_mask */
871 FALSE), /* pcrel_offset */
873 /* Relocation not handled: R_PPC_EMB_MRKREF */
874 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
875 /* Relocation not handled: R_PPC_EMB_RELST_LO */
876 /* Relocation not handled: R_PPC_EMB_RELST_HI */
877 /* Relocation not handled: R_PPC_EMB_RELST_HA */
878 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
880 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
881 in the 16 bit signed offset from the appropriate base, and filling in the
882 register field with the appropriate register (0, 2, or 13). */
883 HOWTO (R_PPC_EMB_RELSDA, /* type */
884 0, /* rightshift */
885 1, /* size (0 = byte, 1 = short, 2 = long) */
886 16, /* bitsize */
887 TRUE, /* pc_relative */
888 0, /* bitpos */
889 complain_overflow_signed, /* complain_on_overflow */
890 bfd_elf_generic_reloc, /* special_function */
891 "R_PPC_EMB_RELSDA", /* name */
892 FALSE, /* partial_inplace */
893 0, /* src_mask */
894 0xffff, /* dst_mask */
895 FALSE), /* pcrel_offset */
897 /* GNU extension to record C++ vtable hierarchy */
898 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
899 0, /* rightshift */
900 0, /* size (0 = byte, 1 = short, 2 = long) */
901 0, /* bitsize */
902 FALSE, /* pc_relative */
903 0, /* bitpos */
904 complain_overflow_dont, /* complain_on_overflow */
905 NULL, /* special_function */
906 "R_PPC_GNU_VTINHERIT", /* name */
907 FALSE, /* partial_inplace */
908 0, /* src_mask */
909 0, /* dst_mask */
910 FALSE), /* pcrel_offset */
912 /* GNU extension to record C++ vtable member usage */
913 HOWTO (R_PPC_GNU_VTENTRY, /* type */
914 0, /* rightshift */
915 0, /* size (0 = byte, 1 = short, 2 = long) */
916 0, /* bitsize */
917 FALSE, /* pc_relative */
918 0, /* bitpos */
919 complain_overflow_dont, /* complain_on_overflow */
920 NULL, /* special_function */
921 "R_PPC_GNU_VTENTRY", /* name */
922 FALSE, /* partial_inplace */
923 0, /* src_mask */
924 0, /* dst_mask */
925 FALSE), /* pcrel_offset */
927 /* Phony reloc to handle AIX style TOC entries */
928 HOWTO (R_PPC_TOC16, /* type */
929 0, /* rightshift */
930 1, /* size (0 = byte, 1 = short, 2 = long) */
931 16, /* bitsize */
932 FALSE, /* pc_relative */
933 0, /* bitpos */
934 complain_overflow_signed, /* complain_on_overflow */
935 bfd_elf_generic_reloc, /* special_function */
936 "R_PPC_TOC16", /* name */
937 FALSE, /* partial_inplace */
938 0, /* src_mask */
939 0xffff, /* dst_mask */
940 FALSE), /* pcrel_offset */
943 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
945 static void
946 ppc_elf_howto_init ()
948 unsigned int i, type;
950 for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
952 type = ppc_elf_howto_raw[i].type;
953 BFD_ASSERT (type < sizeof (ppc_elf_howto_table) / sizeof (ppc_elf_howto_table[0]));
954 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
958 /* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
960 The MPC860, revision C0 or earlier contains a bug in the die.
961 If all of the following conditions are true, the next instruction
962 to be executed *may* be treated as a no-op.
963 1/ A forward branch is executed.
964 2/ The branch is predicted as not taken.
965 3/ The branch is taken.
966 4/ The branch is located in the last 5 words of a page.
967 (The EOP limit is 5 by default but may be specified as any value from 1-10.)
969 Our software solution is to detect these problematic branches in a
970 linker pass and modify them as follows:
971 1/ Unconditional branches - Since these are always predicted taken,
972 there is no problem and no action is required.
973 2/ Conditional backward branches - No problem, no action required.
974 3/ Conditional forward branches - Ensure that the "inverse prediction
975 bit" is set (ensure it is predicted taken).
976 4/ Conditional register branches - Ensure that the "y bit" is set
977 (ensure it is predicted taken).
980 /* Sort sections by address. */
982 static int
983 ppc_elf_sort_rela (arg1, arg2)
984 const PTR arg1;
985 const PTR arg2;
987 const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
988 const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
990 /* Sort by offset. */
991 return ((*rela1)->r_offset - (*rela2)->r_offset);
994 static bfd_boolean
995 ppc_elf_relax_section (abfd, isec, link_info, again)
996 bfd *abfd;
997 asection *isec;
998 struct bfd_link_info *link_info;
999 bfd_boolean *again;
1001 #define PAGESIZE 0x1000
1003 bfd_byte *contents = NULL;
1004 bfd_byte *free_contents = NULL;
1005 Elf_Internal_Rela *internal_relocs = NULL;
1006 Elf_Internal_Rela *free_relocs = NULL;
1007 Elf_Internal_Rela **rela_comb = NULL;
1008 int comb_curr, comb_count;
1010 /* We never have to do this more than once per input section. */
1011 *again = FALSE;
1013 /* If needed, initialize this section's cooked size. */
1014 if (isec->_cooked_size == 0)
1015 isec->_cooked_size = isec->_raw_size;
1017 /* We're only interested in text sections which overlap the
1018 troublesome area at the end of a page. */
1019 if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1021 bfd_vma dot, end_page, end_section;
1022 bfd_boolean section_modified;
1024 /* Get the section contents. */
1025 /* Get cached copy if it exists. */
1026 if (elf_section_data (isec)->this_hdr.contents != NULL)
1027 contents = elf_section_data (isec)->this_hdr.contents;
1028 else
1030 /* Go get them off disk. */
1031 contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
1032 if (contents == NULL)
1033 goto error_return;
1034 free_contents = contents;
1036 if (! bfd_get_section_contents (abfd, isec, contents,
1037 (file_ptr) 0, isec->_raw_size))
1038 goto error_return;
1041 comb_curr = 0;
1042 comb_count = 0;
1043 if (isec->reloc_count)
1045 unsigned n;
1046 bfd_size_type amt;
1048 /* Get a copy of the native relocations. */
1049 internal_relocs = _bfd_elf32_link_read_relocs (
1050 abfd, isec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1051 link_info->keep_memory);
1052 if (internal_relocs == NULL)
1053 goto error_return;
1054 if (! link_info->keep_memory)
1055 free_relocs = internal_relocs;
1057 /* Setup a faster access method for the reloc info we need. */
1058 amt = isec->reloc_count;
1059 amt *= sizeof (Elf_Internal_Rela*);
1060 rela_comb = (Elf_Internal_Rela**) bfd_malloc (amt);
1061 if (rela_comb == NULL)
1062 goto error_return;
1063 for (n = 0; n < isec->reloc_count; ++n)
1065 long r_type;
1067 r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
1068 if (r_type < 0 || r_type >= (int) R_PPC_max)
1069 goto error_return;
1071 /* Prologue constants are sometimes present in the ".text"
1072 sections and they can be identified by their associated relocation.
1073 We don't want to process those words and some others which
1074 can also be identified by their relocations. However, not all
1075 conditional branches will have a relocation so we will
1076 only ignore words that 1) have a reloc, and 2) the reloc
1077 is not applicable to a conditional branch.
1078 The array rela_comb is built here for use in the EOP scan loop. */
1079 switch (r_type)
1081 case R_PPC_ADDR14_BRNTAKEN: /* absolute, predicted not taken */
1082 case R_PPC_REL14: /* relative cond. br. */
1083 case R_PPC_REL14_BRNTAKEN: /* rel. cond. br., predicted not taken */
1084 /* We should check the instruction. */
1085 break;
1086 default:
1087 /* The word is not a conditional branch - ignore it. */
1088 rela_comb[comb_count++] = &internal_relocs[n];
1089 break;
1092 if (comb_count > 1)
1093 qsort (rela_comb, (size_t) comb_count, sizeof (int), ppc_elf_sort_rela);
1096 /* Enumerate each EOP region that overlaps this section. */
1097 end_section = isec->vma + isec->_cooked_size;
1098 dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1099 dot -= link_info->mpc860c0;
1100 section_modified = FALSE;
1101 if (dot < isec->vma) /* Increment the start position if this section */
1102 dot = isec->vma; /* begins in the middle of its first EOP region. */
1103 for (;
1104 dot < end_section;
1105 dot += PAGESIZE, end_page += PAGESIZE)
1108 /* Check each word in this EOP region. */
1109 for (; dot < end_page; dot += 4)
1111 bfd_vma isec_offset;
1112 unsigned long insn;
1113 bfd_boolean skip, modified;
1115 /* Don't process this word if there is a relocation for it and
1116 the relocation indicates the word is not a conditional branch. */
1117 skip = FALSE;
1118 isec_offset = dot - isec->vma;
1119 for (; comb_curr<comb_count; ++comb_curr)
1121 bfd_vma r_offset;
1123 r_offset = rela_comb[comb_curr]->r_offset;
1124 if (r_offset >= isec_offset)
1126 if (r_offset == isec_offset) skip = TRUE;
1127 break;
1130 if (skip) continue;
1132 /* Check the current word for a problematic conditional branch. */
1133 #define BO0(insn) ((insn) & 0x02000000)
1134 #define BO2(insn) ((insn) & 0x00800000)
1135 #define BO4(insn) ((insn) & 0x00200000)
1136 insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1137 modified = FALSE;
1138 if ((insn & 0xFc000000) == 0x40000000)
1140 /* Instruction is BCx */
1141 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1143 bfd_vma target;
1144 /* This branch is predicted as "normal".
1145 If this is a forward branch, it is problematic. */
1147 target = insn & 0x0000Fffc; /*extract*/
1148 target = (target ^ 0x8000) - 0x8000; /*sign extend*/
1149 if ((insn & 0x00000002) == 0)
1150 target += dot; /*convert to abs*/
1151 if (target > dot)
1153 insn |= 0x00200000; /* set the prediction bit */
1154 modified = TRUE;
1158 else if ((insn & 0xFc00Fffe) == 0x4c000420)
1160 /* Instruction is BCCTRx */
1161 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1163 /* This branch is predicted as not-taken.
1164 If this is a forward branch, it is problematic.
1165 Since we can't tell statically if it will branch forward,
1166 always set the prediction bit. */
1167 insn |= 0x00200000; /* set the prediction bit */
1168 modified = TRUE;
1171 else if ((insn & 0xFc00Fffe) == 0x4c000020)
1173 /* Instruction is BCLRx */
1174 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1176 /* This branch is predicted as not-taken.
1177 If this is a forward branch, it is problematic.
1178 Since we can't tell statically if it will branch forward,
1179 always set the prediction bit. */
1180 insn |= 0x00200000; /* set the prediction bit */
1181 modified = TRUE;
1184 #undef BO0
1185 #undef BO2
1186 #undef BO4
1187 if (modified)
1189 bfd_put_32 (abfd, (bfd_vma) insn, contents + isec_offset);
1190 section_modified = TRUE;
1194 if (section_modified)
1196 elf_section_data (isec)->this_hdr.contents = contents;
1197 free_contents = NULL;
1201 if (rela_comb != NULL)
1203 free (rela_comb);
1204 rela_comb = NULL;
1207 if (free_relocs != NULL)
1209 free (free_relocs);
1210 free_relocs = NULL;
1213 if (free_contents != NULL)
1215 if (! link_info->keep_memory)
1216 free (free_contents);
1217 else
1219 /* Cache the section contents for elf_link_input_bfd. */
1220 elf_section_data (isec)->this_hdr.contents = contents;
1222 free_contents = NULL;
1225 return TRUE;
1227 error_return:
1228 if (rela_comb != NULL)
1229 free (rela_comb);
1230 if (free_relocs != NULL)
1231 free (free_relocs);
1232 if (free_contents != NULL)
1233 free (free_contents);
1234 return FALSE;
1237 static reloc_howto_type *
1238 ppc_elf_reloc_type_lookup (abfd, code)
1239 bfd *abfd ATTRIBUTE_UNUSED;
1240 bfd_reloc_code_real_type code;
1242 enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1244 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1245 /* Initialize howto table if needed. */
1246 ppc_elf_howto_init ();
1248 switch ((int) code)
1250 default:
1251 return (reloc_howto_type *) NULL;
1253 case BFD_RELOC_NONE: ppc_reloc = R_PPC_NONE; break;
1254 case BFD_RELOC_32: ppc_reloc = R_PPC_ADDR32; break;
1255 case BFD_RELOC_PPC_BA26: ppc_reloc = R_PPC_ADDR24; break;
1256 case BFD_RELOC_16: ppc_reloc = R_PPC_ADDR16; break;
1257 case BFD_RELOC_LO16: ppc_reloc = R_PPC_ADDR16_LO; break;
1258 case BFD_RELOC_HI16: ppc_reloc = R_PPC_ADDR16_HI; break;
1259 case BFD_RELOC_HI16_S: ppc_reloc = R_PPC_ADDR16_HA; break;
1260 case BFD_RELOC_PPC_BA16: ppc_reloc = R_PPC_ADDR14; break;
1261 case BFD_RELOC_PPC_BA16_BRTAKEN: ppc_reloc = R_PPC_ADDR14_BRTAKEN; break;
1262 case BFD_RELOC_PPC_BA16_BRNTAKEN: ppc_reloc = R_PPC_ADDR14_BRNTAKEN; break;
1263 case BFD_RELOC_PPC_B26: ppc_reloc = R_PPC_REL24; break;
1264 case BFD_RELOC_PPC_B16: ppc_reloc = R_PPC_REL14; break;
1265 case BFD_RELOC_PPC_B16_BRTAKEN: ppc_reloc = R_PPC_REL14_BRTAKEN; break;
1266 case BFD_RELOC_PPC_B16_BRNTAKEN: ppc_reloc = R_PPC_REL14_BRNTAKEN; break;
1267 case BFD_RELOC_16_GOTOFF: ppc_reloc = R_PPC_GOT16; break;
1268 case BFD_RELOC_LO16_GOTOFF: ppc_reloc = R_PPC_GOT16_LO; break;
1269 case BFD_RELOC_HI16_GOTOFF: ppc_reloc = R_PPC_GOT16_HI; break;
1270 case BFD_RELOC_HI16_S_GOTOFF: ppc_reloc = R_PPC_GOT16_HA; break;
1271 case BFD_RELOC_24_PLT_PCREL: ppc_reloc = R_PPC_PLTREL24; break;
1272 case BFD_RELOC_PPC_COPY: ppc_reloc = R_PPC_COPY; break;
1273 case BFD_RELOC_PPC_GLOB_DAT: ppc_reloc = R_PPC_GLOB_DAT; break;
1274 case BFD_RELOC_PPC_LOCAL24PC: ppc_reloc = R_PPC_LOCAL24PC; break;
1275 case BFD_RELOC_32_PCREL: ppc_reloc = R_PPC_REL32; break;
1276 case BFD_RELOC_32_PLTOFF: ppc_reloc = R_PPC_PLT32; break;
1277 case BFD_RELOC_32_PLT_PCREL: ppc_reloc = R_PPC_PLTREL32; break;
1278 case BFD_RELOC_LO16_PLTOFF: ppc_reloc = R_PPC_PLT16_LO; break;
1279 case BFD_RELOC_HI16_PLTOFF: ppc_reloc = R_PPC_PLT16_HI; break;
1280 case BFD_RELOC_HI16_S_PLTOFF: ppc_reloc = R_PPC_PLT16_HA; break;
1281 case BFD_RELOC_GPREL16: ppc_reloc = R_PPC_SDAREL16; break;
1282 case BFD_RELOC_16_BASEREL: ppc_reloc = R_PPC_SECTOFF; break;
1283 case BFD_RELOC_LO16_BASEREL: ppc_reloc = R_PPC_SECTOFF_LO; break;
1284 case BFD_RELOC_HI16_BASEREL: ppc_reloc = R_PPC_SECTOFF_HI; break;
1285 case BFD_RELOC_HI16_S_BASEREL: ppc_reloc = R_PPC_SECTOFF_HA; break;
1286 case BFD_RELOC_CTOR: ppc_reloc = R_PPC_ADDR32; break;
1287 case BFD_RELOC_PPC_TOC16: ppc_reloc = R_PPC_TOC16; break;
1288 case BFD_RELOC_PPC_EMB_NADDR32: ppc_reloc = R_PPC_EMB_NADDR32; break;
1289 case BFD_RELOC_PPC_EMB_NADDR16: ppc_reloc = R_PPC_EMB_NADDR16; break;
1290 case BFD_RELOC_PPC_EMB_NADDR16_LO: ppc_reloc = R_PPC_EMB_NADDR16_LO; break;
1291 case BFD_RELOC_PPC_EMB_NADDR16_HI: ppc_reloc = R_PPC_EMB_NADDR16_HI; break;
1292 case BFD_RELOC_PPC_EMB_NADDR16_HA: ppc_reloc = R_PPC_EMB_NADDR16_HA; break;
1293 case BFD_RELOC_PPC_EMB_SDAI16: ppc_reloc = R_PPC_EMB_SDAI16; break;
1294 case BFD_RELOC_PPC_EMB_SDA2I16: ppc_reloc = R_PPC_EMB_SDA2I16; break;
1295 case BFD_RELOC_PPC_EMB_SDA2REL: ppc_reloc = R_PPC_EMB_SDA2REL; break;
1296 case BFD_RELOC_PPC_EMB_SDA21: ppc_reloc = R_PPC_EMB_SDA21; break;
1297 case BFD_RELOC_PPC_EMB_MRKREF: ppc_reloc = R_PPC_EMB_MRKREF; break;
1298 case BFD_RELOC_PPC_EMB_RELSEC16: ppc_reloc = R_PPC_EMB_RELSEC16; break;
1299 case BFD_RELOC_PPC_EMB_RELST_LO: ppc_reloc = R_PPC_EMB_RELST_LO; break;
1300 case BFD_RELOC_PPC_EMB_RELST_HI: ppc_reloc = R_PPC_EMB_RELST_HI; break;
1301 case BFD_RELOC_PPC_EMB_RELST_HA: ppc_reloc = R_PPC_EMB_RELST_HA; break;
1302 case BFD_RELOC_PPC_EMB_BIT_FLD: ppc_reloc = R_PPC_EMB_BIT_FLD; break;
1303 case BFD_RELOC_PPC_EMB_RELSDA: ppc_reloc = R_PPC_EMB_RELSDA; break;
1304 case BFD_RELOC_VTABLE_INHERIT: ppc_reloc = R_PPC_GNU_VTINHERIT; break;
1305 case BFD_RELOC_VTABLE_ENTRY: ppc_reloc = R_PPC_GNU_VTENTRY; break;
1308 return ppc_elf_howto_table[(int) ppc_reloc];
1311 /* Set the howto pointer for a PowerPC ELF reloc. */
1313 static void
1314 ppc_elf_info_to_howto (abfd, cache_ptr, dst)
1315 bfd *abfd ATTRIBUTE_UNUSED;
1316 arelent *cache_ptr;
1317 Elf_Internal_Rela *dst;
1319 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1320 /* Initialize howto table if needed. */
1321 ppc_elf_howto_init ();
1323 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1324 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1327 /* Handle the R_PPC_ADDR16_HA reloc. */
1329 static bfd_reloc_status_type
1330 ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
1331 output_bfd, error_message)
1332 bfd *abfd ATTRIBUTE_UNUSED;
1333 arelent *reloc_entry;
1334 asymbol *symbol;
1335 PTR data ATTRIBUTE_UNUSED;
1336 asection *input_section;
1337 bfd *output_bfd;
1338 char **error_message ATTRIBUTE_UNUSED;
1340 bfd_vma relocation;
1342 if (output_bfd != NULL)
1344 reloc_entry->address += input_section->output_offset;
1345 return bfd_reloc_ok;
1348 if (reloc_entry->address > input_section->_cooked_size)
1349 return bfd_reloc_outofrange;
1351 if (bfd_is_com_section (symbol->section))
1352 relocation = 0;
1353 else
1354 relocation = symbol->value;
1356 relocation += symbol->section->output_section->vma;
1357 relocation += symbol->section->output_offset;
1358 relocation += reloc_entry->addend;
1360 reloc_entry->addend += (relocation & 0x8000) << 1;
1362 return bfd_reloc_continue;
1365 /* Fix bad default arch selected for a 32 bit input bfd when the
1366 default is 64 bit. */
1368 static bfd_boolean
1369 ppc_elf_object_p (abfd)
1370 bfd *abfd;
1372 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1374 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1376 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1378 /* Relies on arch after 64 bit default being 32 bit default. */
1379 abfd->arch_info = abfd->arch_info->next;
1380 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1383 return TRUE;
1386 /* Function to set whether a module needs the -mrelocatable bit set. */
1388 static bfd_boolean
1389 ppc_elf_set_private_flags (abfd, flags)
1390 bfd *abfd;
1391 flagword flags;
1393 BFD_ASSERT (!elf_flags_init (abfd)
1394 || elf_elfheader (abfd)->e_flags == flags);
1396 elf_elfheader (abfd)->e_flags = flags;
1397 elf_flags_init (abfd) = TRUE;
1398 return TRUE;
1401 /* Merge backend specific data from an object file to the output
1402 object file when linking */
1403 static bfd_boolean
1404 ppc_elf_merge_private_bfd_data (ibfd, obfd)
1405 bfd *ibfd;
1406 bfd *obfd;
1408 flagword old_flags;
1409 flagword new_flags;
1410 bfd_boolean error;
1412 /* Check if we have the same endianess */
1413 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
1414 return FALSE;
1416 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1417 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1418 return TRUE;
1420 new_flags = elf_elfheader (ibfd)->e_flags;
1421 old_flags = elf_elfheader (obfd)->e_flags;
1422 if (!elf_flags_init (obfd)) /* First call, no flags set */
1424 elf_flags_init (obfd) = TRUE;
1425 elf_elfheader (obfd)->e_flags = new_flags;
1428 else if (new_flags == old_flags) /* Compatible flags are ok */
1431 else /* Incompatible flags */
1433 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib to be linked
1434 with either. */
1435 error = FALSE;
1436 if ((new_flags & EF_PPC_RELOCATABLE) != 0
1437 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1439 error = TRUE;
1440 (*_bfd_error_handler)
1441 (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
1442 bfd_archive_filename (ibfd));
1444 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1445 && (old_flags & EF_PPC_RELOCATABLE) != 0)
1447 error = TRUE;
1448 (*_bfd_error_handler)
1449 (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
1450 bfd_archive_filename (ibfd));
1453 /* The output is -mrelocatable-lib iff both the input files are. */
1454 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1455 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1457 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1458 but each input file is either -mrelocatable or -mrelocatable-lib. */
1459 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1460 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1461 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1462 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1464 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it */
1465 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1467 new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1468 old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1470 /* Warn about any other mismatches */
1471 if (new_flags != old_flags)
1473 error = TRUE;
1474 (*_bfd_error_handler)
1475 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1476 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
1479 if (error)
1481 bfd_set_error (bfd_error_bad_value);
1482 return FALSE;
1486 return TRUE;
1489 /* Handle a PowerPC specific section when reading an object file. This
1490 is called when elfcode.h finds a section with an unknown type. */
1492 static bfd_boolean
1493 ppc_elf_section_from_shdr (abfd, hdr, name)
1494 bfd *abfd;
1495 Elf_Internal_Shdr *hdr;
1496 const char *name;
1498 asection *newsect;
1499 flagword flags;
1501 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1502 return FALSE;
1504 newsect = hdr->bfd_section;
1505 flags = bfd_get_section_flags (abfd, newsect);
1506 if (hdr->sh_flags & SHF_EXCLUDE)
1507 flags |= SEC_EXCLUDE;
1509 if (hdr->sh_type == SHT_ORDERED)
1510 flags |= SEC_SORT_ENTRIES;
1512 bfd_set_section_flags (abfd, newsect, flags);
1513 return TRUE;
1516 /* Set up any other section flags and such that may be necessary. */
1518 static bfd_boolean
1519 ppc_elf_fake_sections (abfd, shdr, asect)
1520 bfd *abfd ATTRIBUTE_UNUSED;
1521 Elf_Internal_Shdr *shdr;
1522 asection *asect;
1524 if ((asect->flags & SEC_EXCLUDE) != 0)
1525 shdr->sh_flags |= SHF_EXCLUDE;
1527 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1528 shdr->sh_type = SHT_ORDERED;
1530 return TRUE;
1533 /* Create a special linker section */
1534 static elf_linker_section_t *
1535 ppc_elf_create_linker_section (abfd, info, which)
1536 bfd *abfd;
1537 struct bfd_link_info *info;
1538 enum elf_linker_section_enum which;
1540 bfd *dynobj = elf_hash_table (info)->dynobj;
1541 elf_linker_section_t *lsect;
1543 /* Record the first bfd section that needs the special section */
1544 if (!dynobj)
1545 dynobj = elf_hash_table (info)->dynobj = abfd;
1547 /* If this is the first time, create the section */
1548 lsect = elf_linker_section (dynobj, which);
1549 if (!lsect)
1551 elf_linker_section_t defaults;
1552 static elf_linker_section_t zero_section;
1554 defaults = zero_section;
1555 defaults.which = which;
1556 defaults.hole_written_p = FALSE;
1557 defaults.alignment = 2;
1559 /* Both of these sections are (technically) created by the user
1560 putting data in them, so they shouldn't be marked
1561 SEC_LINKER_CREATED.
1563 The linker creates them so it has somewhere to attach their
1564 respective symbols. In fact, if they were empty it would
1565 be OK to leave the symbol set to 0 (or any random number), because
1566 the appropriate register should never be used. */
1567 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1568 | SEC_IN_MEMORY);
1570 switch (which)
1572 default:
1573 (*_bfd_error_handler) (_("%s: Unknown special linker type %d"),
1574 bfd_get_filename (abfd),
1575 (int) which);
1577 bfd_set_error (bfd_error_bad_value);
1578 return (elf_linker_section_t *) 0;
1580 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
1581 defaults.name = ".sdata";
1582 defaults.rel_name = ".rela.sdata";
1583 defaults.bss_name = ".sbss";
1584 defaults.sym_name = "_SDA_BASE_";
1585 defaults.sym_offset = 32768;
1586 break;
1588 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
1589 defaults.name = ".sdata2";
1590 defaults.rel_name = ".rela.sdata2";
1591 defaults.bss_name = ".sbss2";
1592 defaults.sym_name = "_SDA2_BASE_";
1593 defaults.sym_offset = 32768;
1594 defaults.flags |= SEC_READONLY;
1595 break;
1598 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
1601 return lsect;
1604 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
1605 need to bump up the number of section headers. */
1607 static int
1608 ppc_elf_additional_program_headers (abfd)
1609 bfd *abfd;
1611 asection *s;
1612 int ret;
1614 ret = 0;
1616 s = bfd_get_section_by_name (abfd, ".interp");
1617 if (s != NULL)
1618 ++ret;
1620 s = bfd_get_section_by_name (abfd, ".sbss2");
1621 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1622 ++ret;
1624 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1625 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1626 ++ret;
1628 return ret;
1631 /* Modify the segment map if needed. */
1633 static bfd_boolean
1634 ppc_elf_modify_segment_map (abfd)
1635 bfd *abfd ATTRIBUTE_UNUSED;
1637 return TRUE;
1640 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
1642 static asection *
1643 ppc_elf_create_got (abfd, info)
1644 bfd *abfd;
1645 struct bfd_link_info *info;
1647 register asection *s;
1648 flagword flags;
1650 if (!_bfd_elf_create_got_section (abfd, info))
1651 return NULL;
1653 s = bfd_get_section_by_name (abfd, ".got");
1654 if (s == NULL)
1655 abort ();
1657 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1658 | SEC_LINKER_CREATED);
1659 if (!bfd_set_section_flags (abfd, s, flags))
1660 return NULL;
1661 return s;
1664 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
1665 to output sections (just like _bfd_elf_create_dynamic_sections has
1666 to create .dynbss and .rela.bss). */
1668 static bfd_boolean
1669 ppc_elf_create_dynamic_sections (abfd, info)
1670 bfd *abfd;
1671 struct bfd_link_info *info;
1673 register asection *s;
1674 flagword flags;
1676 if (!ppc_elf_create_got (abfd, info))
1677 return FALSE;
1679 if (!_bfd_elf_create_dynamic_sections (abfd, info))
1680 return FALSE;
1682 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1683 | SEC_LINKER_CREATED);
1685 s = bfd_make_section (abfd, ".dynsbss");
1686 if (s == NULL
1687 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1688 return FALSE;
1690 if (! info->shared)
1692 s = bfd_make_section (abfd, ".rela.sbss");
1693 if (s == NULL
1694 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1695 || ! bfd_set_section_alignment (abfd, s, 2))
1696 return FALSE;
1699 s = bfd_get_section_by_name (abfd, ".plt");
1700 if (s == NULL)
1701 abort ();
1703 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
1704 return bfd_set_section_flags (abfd, s, flags);
1707 /* Adjust a symbol defined by a dynamic object and referenced by a
1708 regular object. The current definition is in some section of the
1709 dynamic object, but we're not including those sections. We have to
1710 change the definition to something the rest of the link can
1711 understand. */
1713 static bfd_boolean
1714 ppc_elf_adjust_dynamic_symbol (info, h)
1715 struct bfd_link_info *info;
1716 struct elf_link_hash_entry *h;
1718 bfd *dynobj = elf_hash_table (info)->dynobj;
1719 asection *s;
1720 unsigned int power_of_two;
1721 bfd_vma plt_offset;
1723 #ifdef DEBUG
1724 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n", h->root.root.string);
1725 #endif
1727 /* Make sure we know what is going on here. */
1728 BFD_ASSERT (dynobj != NULL
1729 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1730 || h->weakdef != NULL
1731 || ((h->elf_link_hash_flags
1732 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1733 && (h->elf_link_hash_flags
1734 & ELF_LINK_HASH_REF_REGULAR) != 0
1735 && (h->elf_link_hash_flags
1736 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1738 /* If this is a function, put it in the procedure linkage table. We
1739 will fill in the contents of the procedure linkage table later,
1740 when we know the address of the .got section. */
1741 if (h->type == STT_FUNC
1742 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1744 if (! elf_hash_table (info)->dynamic_sections_created
1745 || SYMBOL_CALLS_LOCAL (info, h)
1746 || (info->shared && h->plt.refcount <= 0))
1748 /* A PLT entry is not required/allowed when:
1750 1. We are not using ld.so; because then the PLT entry
1751 can't be set up, so we can't use one.
1753 2. We know for certain that a call to this symbol
1754 will go to this object.
1756 3. GC has rendered the entry unused.
1757 Note, however, that in an executable all references to the
1758 symbol go to the PLT, so we can't turn it off in that case.
1759 ??? The correct thing to do here is to reference count
1760 all uses of the symbol, not just those to the GOT or PLT. */
1761 h->plt.offset = (bfd_vma) -1;
1762 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1763 return TRUE;
1766 /* Make sure this symbol is output as a dynamic symbol. */
1767 if (h->dynindx == -1)
1769 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1770 return FALSE;
1772 BFD_ASSERT (h->dynindx != -1);
1774 s = bfd_get_section_by_name (dynobj, ".plt");
1775 BFD_ASSERT (s != NULL);
1777 /* If this is the first .plt entry, make room for the special
1778 first entry. */
1779 if (s->_raw_size == 0)
1780 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
1782 /* The PowerPC PLT is actually composed of two parts, the first part
1783 is 2 words (for a load and a jump), and then there is a remaining
1784 word available at the end. */
1785 plt_offset = (PLT_INITIAL_ENTRY_SIZE
1786 + (PLT_SLOT_SIZE
1787 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
1788 / PLT_ENTRY_SIZE)));
1790 /* If this symbol is not defined in a regular file, and we are
1791 not generating a shared library, then set the symbol to this
1792 location in the .plt. This is required to make function
1793 pointers compare as equal between the normal executable and
1794 the shared library. */
1795 if (! info->shared
1796 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1798 h->root.u.def.section = s;
1799 h->root.u.def.value = plt_offset;
1802 h->plt.offset = plt_offset;
1804 /* Make room for this entry. After the 8192nd entry, room
1805 for two entries is allocated. */
1806 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
1807 >= PLT_NUM_SINGLE_ENTRIES)
1808 s->_raw_size += 2 * PLT_ENTRY_SIZE;
1809 else
1810 s->_raw_size += PLT_ENTRY_SIZE;
1812 /* We also need to make an entry in the .rela.plt section. */
1813 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1814 BFD_ASSERT (s != NULL);
1815 s->_raw_size += sizeof (Elf32_External_Rela);
1817 return TRUE;
1819 else
1820 h->plt.offset = (bfd_vma) -1;
1822 /* If this is a weak symbol, and there is a real definition, the
1823 processor independent code will have arranged for us to see the
1824 real definition first, and we can just use the same value. */
1825 if (h->weakdef != NULL)
1827 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1828 || h->weakdef->root.type == bfd_link_hash_defweak);
1829 h->root.u.def.section = h->weakdef->root.u.def.section;
1830 h->root.u.def.value = h->weakdef->root.u.def.value;
1831 return TRUE;
1834 /* This is a reference to a symbol defined by a dynamic object which
1835 is not a function. */
1837 /* If we are creating a shared library, we must presume that the
1838 only references to the symbol are via the global offset table.
1839 For such cases we need not do anything here; the relocations will
1840 be handled correctly by relocate_section. */
1841 if (info->shared)
1842 return TRUE;
1844 /* We must allocate the symbol in our .dynbss section, which will
1845 become part of the .bss section of the executable. There will be
1846 an entry for this symbol in the .dynsym section. The dynamic
1847 object will contain position independent code, so all references
1848 from the dynamic object to this symbol will go through the global
1849 offset table. The dynamic linker will use the .dynsym entry to
1850 determine the address it must put in the global offset table, so
1851 both the dynamic object and the regular object will refer to the
1852 same memory location for the variable.
1854 Of course, if the symbol is sufficiently small, we must instead
1855 allocate it in .sbss. FIXME: It would be better to do this if and
1856 only if there were actually SDAREL relocs for that symbol. */
1858 if (h->size <= elf_gp_size (dynobj))
1859 s = bfd_get_section_by_name (dynobj, ".dynsbss");
1860 else
1861 s = bfd_get_section_by_name (dynobj, ".dynbss");
1862 BFD_ASSERT (s != NULL);
1864 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
1865 copy the initial value out of the dynamic object and into the
1866 runtime process image. We need to remember the offset into the
1867 .rela.bss section we are going to use. */
1868 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1870 asection *srel;
1872 if (h->size <= elf_gp_size (dynobj))
1873 srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
1874 else
1875 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1876 BFD_ASSERT (srel != NULL);
1877 srel->_raw_size += sizeof (Elf32_External_Rela);
1878 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1881 /* We need to figure out the alignment required for this symbol. I
1882 have no idea how ELF linkers handle this. */
1883 power_of_two = bfd_log2 (h->size);
1884 if (power_of_two > 4)
1885 power_of_two = 4;
1887 /* Apply the required alignment. */
1888 s->_raw_size = BFD_ALIGN (s->_raw_size,
1889 (bfd_size_type) (1 << power_of_two));
1890 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1892 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
1893 return FALSE;
1896 /* Define the symbol as being at this point in the section. */
1897 h->root.u.def.section = s;
1898 h->root.u.def.value = s->_raw_size;
1900 /* Increment the section size to make room for the symbol. */
1901 s->_raw_size += h->size;
1903 return TRUE;
1906 /* Set the sizes of the dynamic sections. */
1908 static bfd_boolean
1909 ppc_elf_size_dynamic_sections (output_bfd, info)
1910 bfd *output_bfd ATTRIBUTE_UNUSED;
1911 struct bfd_link_info *info;
1913 bfd *dynobj;
1914 asection *s;
1915 bfd_boolean plt;
1916 bfd_boolean relocs;
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 for (s = dynobj->sections; s != NULL; s = s->next)
1963 const char *name;
1964 bfd_boolean strip;
1966 if ((s->flags & SEC_LINKER_CREATED) == 0)
1967 continue;
1969 /* It's OK to base decisions on the section name, because none
1970 of the dynobj section names depend upon the input files. */
1971 name = bfd_get_section_name (dynobj, s);
1973 strip = FALSE;
1975 if (strcmp (name, ".plt") == 0)
1977 if (s->_raw_size == 0)
1979 /* Strip this section if we don't need it; see the
1980 comment below. */
1981 strip = TRUE;
1983 else
1985 /* Remember whether there is a PLT. */
1986 plt = TRUE;
1989 else if (strncmp (name, ".rela", 5) == 0)
1991 if (s->_raw_size == 0)
1993 /* If we don't need this section, strip it from the
1994 output file. This is mostly to handle .rela.bss and
1995 .rela.plt. We must create both sections in
1996 create_dynamic_sections, because they must be created
1997 before the linker maps input sections to output
1998 sections. The linker does that before
1999 adjust_dynamic_symbol is called, and it is that
2000 function which decides whether anything needs to go
2001 into these sections. */
2002 strip = TRUE;
2004 else
2006 /* Remember whether there are any relocation sections. */
2007 relocs = TRUE;
2009 /* We use the reloc_count field as a counter if we need
2010 to copy relocs into the output file. */
2011 s->reloc_count = 0;
2014 else if (strcmp (name, ".got") != 0
2015 && strcmp (name, ".sdata") != 0
2016 && strcmp (name, ".sdata2") != 0)
2018 /* It's not one of our sections, so don't allocate space. */
2019 continue;
2022 if (strip)
2024 _bfd_strip_section_from_output (info, s);
2025 continue;
2028 /* Allocate memory for the section contents. */
2029 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2030 if (s->contents == NULL && s->_raw_size != 0)
2031 return FALSE;
2034 if (elf_hash_table (info)->dynamic_sections_created)
2036 /* Add some entries to the .dynamic section. We fill in the
2037 values later, in ppc_elf_finish_dynamic_sections, but we
2038 must add the entries now so that we get the correct size for
2039 the .dynamic section. The DT_DEBUG entry is filled in by the
2040 dynamic linker and used by the debugger. */
2041 #define add_dynamic_entry(TAG, VAL) \
2042 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2044 if (!info->shared)
2046 if (!add_dynamic_entry (DT_DEBUG, 0))
2047 return FALSE;
2050 if (plt)
2052 if (!add_dynamic_entry (DT_PLTGOT, 0)
2053 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2054 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2055 || !add_dynamic_entry (DT_JMPREL, 0))
2056 return FALSE;
2059 if (relocs)
2061 if (!add_dynamic_entry (DT_RELA, 0)
2062 || !add_dynamic_entry (DT_RELASZ, 0)
2063 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2064 return FALSE;
2067 if ((info->flags & DF_TEXTREL) != 0)
2069 if (!add_dynamic_entry (DT_TEXTREL, 0))
2070 return FALSE;
2071 info->flags |= DF_TEXTREL;
2074 #undef add_dynamic_entry
2076 return TRUE;
2079 /* Look through the relocs for a section during the first phase, and
2080 allocate space in the global offset table or procedure linkage
2081 table. */
2083 static bfd_boolean
2084 ppc_elf_check_relocs (abfd, info, sec, relocs)
2085 bfd *abfd;
2086 struct bfd_link_info *info;
2087 asection *sec;
2088 const Elf_Internal_Rela *relocs;
2090 bfd *dynobj;
2091 Elf_Internal_Shdr *symtab_hdr;
2092 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2093 const Elf_Internal_Rela *rel;
2094 const Elf_Internal_Rela *rel_end;
2095 bfd_signed_vma *local_got_refcounts;
2096 elf_linker_section_t *sdata;
2097 elf_linker_section_t *sdata2;
2098 asection *sreloc;
2099 asection *sgot = NULL;
2100 asection *srelgot = NULL;
2102 if (info->relocateable)
2103 return TRUE;
2105 #ifdef DEBUG
2106 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
2107 bfd_get_section_name (abfd, sec),
2108 bfd_archive_filename (abfd));
2109 #endif
2111 /* Create the linker generated sections all the time so that the
2112 special symbols are created. */
2114 if ((sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA)) == NULL)
2116 sdata = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2117 if (!sdata)
2118 return FALSE;
2121 if ((sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2)) == NULL)
2123 sdata2 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA2);
2124 if (!sdata2)
2125 return FALSE;
2128 dynobj = elf_hash_table (info)->dynobj;
2129 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2130 local_got_refcounts = elf_local_got_refcounts (abfd);
2132 sym_hashes = elf_sym_hashes (abfd);
2133 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2134 if (!elf_bad_symtab (abfd))
2135 sym_hashes_end -= symtab_hdr->sh_info;
2137 sreloc = NULL;
2139 rel_end = relocs + sec->reloc_count;
2140 for (rel = relocs; rel < rel_end; rel++)
2142 unsigned long r_symndx;
2143 struct elf_link_hash_entry *h;
2145 r_symndx = ELF32_R_SYM (rel->r_info);
2146 if (r_symndx < symtab_hdr->sh_info)
2147 h = NULL;
2148 else
2149 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2151 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2152 This shows up in particular in an R_PPC_ADDR32 in the eabi
2153 startup code. */
2154 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2156 if (sgot == NULL)
2158 if (dynobj == NULL)
2159 elf_hash_table (info)->dynobj = dynobj = abfd;
2160 sgot = ppc_elf_create_got (dynobj, info);
2161 if (sgot == NULL)
2162 return FALSE;
2166 switch (ELF32_R_TYPE (rel->r_info))
2168 /* GOT16 relocations */
2169 case R_PPC_GOT16:
2170 case R_PPC_GOT16_LO:
2171 case R_PPC_GOT16_HI:
2172 case R_PPC_GOT16_HA:
2173 /* This symbol requires a global offset table entry. */
2175 if (sgot == NULL)
2177 if (dynobj == NULL)
2178 elf_hash_table (info)->dynobj = dynobj = abfd;
2179 sgot = ppc_elf_create_got (dynobj, info);
2180 if (sgot == NULL)
2181 return FALSE;
2184 if (srelgot == NULL
2185 && (h != NULL || info->shared))
2187 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2188 if (srelgot == NULL)
2190 srelgot = bfd_make_section (dynobj, ".rela.got");
2191 if (srelgot == NULL
2192 || ! bfd_set_section_flags (dynobj, srelgot,
2193 (SEC_ALLOC
2194 | SEC_LOAD
2195 | SEC_HAS_CONTENTS
2196 | SEC_IN_MEMORY
2197 | SEC_LINKER_CREATED
2198 | SEC_READONLY))
2199 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2200 return FALSE;
2204 if (h != NULL)
2206 if (h->got.refcount == 0)
2208 /* Make sure this symbol is output as a dynamic symbol. */
2209 if (h->dynindx == -1)
2210 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2211 return FALSE;
2213 /* Allocate space in the .got. */
2214 sgot->_raw_size += 4;
2215 /* Allocate relocation space. */
2216 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2218 h->got.refcount++;
2220 else
2222 /* This is a global offset table entry for a local symbol. */
2223 if (local_got_refcounts == NULL)
2225 bfd_size_type size;
2227 size = symtab_hdr->sh_info;
2228 size *= sizeof (bfd_signed_vma);
2229 local_got_refcounts
2230 = (bfd_signed_vma *) bfd_zalloc (abfd, size);
2231 if (local_got_refcounts == NULL)
2232 return FALSE;
2233 elf_local_got_refcounts (abfd) = local_got_refcounts;
2235 if (local_got_refcounts[r_symndx] == 0)
2237 sgot->_raw_size += 4;
2239 /* If we are generating a shared object, we need to
2240 output a R_PPC_RELATIVE reloc so that the
2241 dynamic linker can adjust this GOT entry. */
2242 if (info->shared)
2243 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2245 local_got_refcounts[r_symndx]++;
2247 break;
2249 /* Indirect .sdata relocation */
2250 case R_PPC_EMB_SDAI16:
2251 if (info->shared)
2253 ((*_bfd_error_handler)
2254 (_("%s: relocation %s cannot be used when making a shared object"),
2255 bfd_archive_filename (abfd), "R_PPC_EMB_SDAI16"));
2256 return FALSE;
2259 if (srelgot == NULL && (h != NULL || info->shared))
2261 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2262 if (srelgot == NULL)
2264 srelgot = bfd_make_section (dynobj, ".rela.got");
2265 if (srelgot == NULL
2266 || ! bfd_set_section_flags (dynobj, srelgot,
2267 (SEC_ALLOC
2268 | SEC_LOAD
2269 | SEC_HAS_CONTENTS
2270 | SEC_IN_MEMORY
2271 | SEC_LINKER_CREATED
2272 | SEC_READONLY))
2273 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2274 return FALSE;
2278 if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata, h, rel))
2279 return FALSE;
2281 break;
2283 /* Indirect .sdata2 relocation */
2284 case R_PPC_EMB_SDA2I16:
2285 if (info->shared)
2287 ((*_bfd_error_handler)
2288 (_("%s: relocation %s cannot be used when making a shared object"),
2289 bfd_archive_filename (abfd), "R_PPC_EMB_SDA2I16"));
2290 return FALSE;
2293 if (srelgot == NULL && (h != NULL || info->shared))
2295 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2296 if (srelgot == NULL)
2298 srelgot = bfd_make_section (dynobj, ".rela.got");
2299 if (srelgot == NULL
2300 || ! bfd_set_section_flags (dynobj, srelgot,
2301 (SEC_ALLOC
2302 | SEC_LOAD
2303 | SEC_HAS_CONTENTS
2304 | SEC_IN_MEMORY
2305 | SEC_LINKER_CREATED
2306 | SEC_READONLY))
2307 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2308 return FALSE;
2312 if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata2, h, rel))
2313 return FALSE;
2315 break;
2317 case R_PPC_SDAREL16:
2318 case R_PPC_EMB_SDA2REL:
2319 case R_PPC_EMB_SDA21:
2320 if (info->shared)
2322 ((*_bfd_error_handler)
2323 (_("%s: relocation %s cannot be used when making a shared object"),
2324 bfd_archive_filename (abfd),
2325 ppc_elf_howto_table[(int) ELF32_R_TYPE (rel->r_info)]->name));
2326 return FALSE;
2328 break;
2330 case R_PPC_PLT32:
2331 case R_PPC_PLTREL24:
2332 case R_PPC_PLT16_LO:
2333 case R_PPC_PLT16_HI:
2334 case R_PPC_PLT16_HA:
2335 #ifdef DEBUG
2336 fprintf (stderr, "Reloc requires a PLT entry\n");
2337 #endif
2338 /* This symbol requires a procedure linkage table entry. We
2339 actually build the entry in adjust_dynamic_symbol,
2340 because this might be a case of linking PIC code without
2341 linking in any dynamic objects, in which case we don't
2342 need to generate a procedure linkage table after all. */
2344 if (h == NULL)
2346 /* It does not make sense to have a procedure linkage
2347 table entry for a local symbol. */
2348 bfd_set_error (bfd_error_bad_value);
2349 return FALSE;
2352 /* Make sure this symbol is output as a dynamic symbol. */
2353 if (h->dynindx == -1)
2355 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2356 return FALSE;
2358 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2359 h->plt.refcount++;
2360 break;
2362 /* The following relocations don't need to propagate the
2363 relocation if linking a shared object since they are
2364 section relative. */
2365 case R_PPC_SECTOFF:
2366 case R_PPC_SECTOFF_LO:
2367 case R_PPC_SECTOFF_HI:
2368 case R_PPC_SECTOFF_HA:
2369 break;
2371 /* This refers only to functions defined in the shared library */
2372 case R_PPC_LOCAL24PC:
2373 break;
2375 /* This relocation describes the C++ object vtable hierarchy.
2376 Reconstruct it for later use during GC. */
2377 case R_PPC_GNU_VTINHERIT:
2378 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2379 return FALSE;
2380 break;
2382 /* This relocation describes which C++ vtable entries are actually
2383 used. Record for later use during GC. */
2384 case R_PPC_GNU_VTENTRY:
2385 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2386 return FALSE;
2387 break;
2389 /* When creating a shared object, we must copy these
2390 relocs into the output file. We create a reloc
2391 section in dynobj and make room for the reloc. */
2392 case R_PPC_REL24:
2393 case R_PPC_REL14:
2394 case R_PPC_REL14_BRTAKEN:
2395 case R_PPC_REL14_BRNTAKEN:
2396 case R_PPC_REL32:
2397 if (h == NULL
2398 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2399 || SYMBOL_REFERENCES_LOCAL (info, h))
2400 break;
2401 /* fall through */
2403 default:
2404 if (info->shared)
2406 #ifdef DEBUG
2407 fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
2408 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
2409 #endif
2410 if (sreloc == NULL)
2412 const char *name;
2414 name = (bfd_elf_string_from_elf_section
2415 (abfd,
2416 elf_elfheader (abfd)->e_shstrndx,
2417 elf_section_data (sec)->rel_hdr.sh_name));
2418 if (name == NULL)
2419 return FALSE;
2421 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2422 && strcmp (bfd_get_section_name (abfd, sec),
2423 name + 5) == 0);
2425 sreloc = bfd_get_section_by_name (dynobj, name);
2426 if (sreloc == NULL)
2428 flagword flags;
2430 sreloc = bfd_make_section (dynobj, name);
2431 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2432 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2433 if ((sec->flags & SEC_ALLOC) != 0)
2434 flags |= SEC_ALLOC | SEC_LOAD;
2435 if (sreloc == NULL
2436 || ! bfd_set_section_flags (dynobj, sreloc, flags)
2437 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
2438 return FALSE;
2440 if (sec->flags & SEC_READONLY)
2441 info->flags |= DF_TEXTREL;
2444 sreloc->_raw_size += sizeof (Elf32_External_Rela);
2446 /* FIXME: We should here do what the m68k and i386
2447 backends do: if the reloc is pc-relative, record it
2448 in case it turns out that the reloc is unnecessary
2449 because the symbol is forced local by versioning or
2450 we are linking with -Bdynamic. Fortunately this
2451 case is not frequent. */
2454 break;
2458 return TRUE;
2461 /* Return the section that should be marked against GC for a given
2462 relocation. */
2464 static asection *
2465 ppc_elf_gc_mark_hook (sec, info, rel, h, sym)
2466 asection *sec;
2467 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2468 Elf_Internal_Rela *rel;
2469 struct elf_link_hash_entry *h;
2470 Elf_Internal_Sym *sym;
2472 if (h != NULL)
2474 switch (ELF32_R_TYPE (rel->r_info))
2476 case R_PPC_GNU_VTINHERIT:
2477 case R_PPC_GNU_VTENTRY:
2478 break;
2480 default:
2481 switch (h->root.type)
2483 case bfd_link_hash_defined:
2484 case bfd_link_hash_defweak:
2485 return h->root.u.def.section;
2487 case bfd_link_hash_common:
2488 return h->root.u.c.p->section;
2490 default:
2491 break;
2495 else
2496 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2498 return NULL;
2501 /* Update the got entry reference counts for the section being removed. */
2503 static bfd_boolean
2504 ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
2505 bfd *abfd;
2506 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2507 asection *sec;
2508 const Elf_Internal_Rela *relocs;
2510 Elf_Internal_Shdr *symtab_hdr;
2511 struct elf_link_hash_entry **sym_hashes;
2512 bfd_signed_vma *local_got_refcounts;
2513 const Elf_Internal_Rela *rel, *relend;
2514 unsigned long r_symndx;
2515 struct elf_link_hash_entry *h;
2517 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2518 sym_hashes = elf_sym_hashes (abfd);
2519 local_got_refcounts = elf_local_got_refcounts (abfd);
2521 relend = relocs + sec->reloc_count;
2522 for (rel = relocs; rel < relend; rel++)
2523 switch (ELF32_R_TYPE (rel->r_info))
2525 case R_PPC_GOT16:
2526 case R_PPC_GOT16_LO:
2527 case R_PPC_GOT16_HI:
2528 case R_PPC_GOT16_HA:
2529 r_symndx = ELF32_R_SYM (rel->r_info);
2530 if (r_symndx >= symtab_hdr->sh_info)
2532 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2533 if (h->got.refcount > 0)
2534 h->got.refcount--;
2536 else if (local_got_refcounts != NULL)
2538 if (local_got_refcounts[r_symndx] > 0)
2539 local_got_refcounts[r_symndx]--;
2541 break;
2543 case R_PPC_PLT32:
2544 case R_PPC_PLTREL24:
2545 case R_PPC_PLT16_LO:
2546 case R_PPC_PLT16_HI:
2547 case R_PPC_PLT16_HA:
2548 r_symndx = ELF32_R_SYM (rel->r_info);
2549 if (r_symndx >= symtab_hdr->sh_info)
2551 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2552 if (h->plt.refcount > 0)
2553 h->plt.refcount--;
2555 break;
2557 default:
2558 break;
2561 return TRUE;
2564 /* Hook called by the linker routine which adds symbols from an object
2565 file. We use it to put .comm items in .sbss, and not .bss. */
2567 static bfd_boolean
2568 ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2569 bfd *abfd;
2570 struct bfd_link_info *info;
2571 const Elf_Internal_Sym *sym;
2572 const char **namep ATTRIBUTE_UNUSED;
2573 flagword *flagsp ATTRIBUTE_UNUSED;
2574 asection **secp;
2575 bfd_vma *valp;
2577 if (sym->st_shndx == SHN_COMMON
2578 && !info->relocateable
2579 && sym->st_size <= elf_gp_size (abfd)
2580 && info->hash->creator->flavour == bfd_target_elf_flavour)
2582 /* Common symbols less than or equal to -G nn bytes are automatically
2583 put into .sdata. */
2584 elf_linker_section_t *sdata
2585 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2587 if (!sdata->bss_section)
2589 bfd_size_type amt;
2591 /* We don't go through bfd_make_section, because we don't
2592 want to attach this common section to DYNOBJ. The linker
2593 will move the symbols to the appropriate output section
2594 when it defines common symbols. */
2595 amt = sizeof (asection);
2596 sdata->bss_section = (asection *) bfd_zalloc (abfd, amt);
2597 if (sdata->bss_section == NULL)
2598 return FALSE;
2599 sdata->bss_section->name = sdata->bss_name;
2600 sdata->bss_section->flags = SEC_IS_COMMON;
2601 sdata->bss_section->output_section = sdata->bss_section;
2602 amt = sizeof (asymbol);
2603 sdata->bss_section->symbol = (asymbol *) bfd_zalloc (abfd, amt);
2604 amt = sizeof (asymbol *);
2605 sdata->bss_section->symbol_ptr_ptr =
2606 (asymbol **) bfd_zalloc (abfd, amt);
2607 if (sdata->bss_section->symbol == NULL
2608 || sdata->bss_section->symbol_ptr_ptr == NULL)
2609 return FALSE;
2610 sdata->bss_section->symbol->name = sdata->bss_name;
2611 sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
2612 sdata->bss_section->symbol->section = sdata->bss_section;
2613 *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
2616 *secp = sdata->bss_section;
2617 *valp = sym->st_size;
2620 return TRUE;
2623 /* Finish up dynamic symbol handling. We set the contents of various
2624 dynamic sections here. */
2626 static bfd_boolean
2627 ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
2628 bfd *output_bfd;
2629 struct bfd_link_info *info;
2630 struct elf_link_hash_entry *h;
2631 Elf_Internal_Sym *sym;
2633 bfd *dynobj;
2635 #ifdef DEBUG
2636 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
2637 h->root.root.string);
2638 #endif
2640 dynobj = elf_hash_table (info)->dynobj;
2641 BFD_ASSERT (dynobj != NULL);
2643 if (h->plt.offset != (bfd_vma) -1)
2645 asection *splt;
2646 asection *srela;
2647 Elf_Internal_Rela rela;
2648 bfd_byte *loc;
2649 bfd_vma reloc_index;
2651 #ifdef DEBUG
2652 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
2653 #endif
2655 /* This symbol has an entry in the procedure linkage table. Set
2656 it up. */
2658 BFD_ASSERT (h->dynindx != -1);
2660 splt = bfd_get_section_by_name (dynobj, ".plt");
2661 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2662 BFD_ASSERT (splt != NULL && srela != NULL);
2664 /* We don't need to fill in the .plt. The ppc dynamic linker
2665 will fill it in. */
2667 /* Fill in the entry in the .rela.plt section. */
2668 rela.r_offset = (splt->output_section->vma
2669 + splt->output_offset
2670 + h->plt.offset);
2671 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
2672 rela.r_addend = 0;
2674 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
2675 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
2676 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
2677 loc = srela->contents + reloc_index * sizeof (Elf32_External_Rela);
2678 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2680 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2682 /* Mark the symbol as undefined, rather than as defined in
2683 the .plt section. Leave the value alone. */
2684 sym->st_shndx = SHN_UNDEF;
2685 /* If the symbol is weak, we do need to clear the value.
2686 Otherwise, the PLT entry would provide a definition for
2687 the symbol even if the symbol wasn't defined anywhere,
2688 and so the symbol would never be NULL. */
2689 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
2690 == 0)
2691 sym->st_value = 0;
2695 if (h->got.offset != (bfd_vma) -1)
2697 asection *sgot;
2698 asection *srela;
2699 Elf_Internal_Rela rela;
2700 bfd_byte *loc;
2702 /* This symbol has an entry in the global offset table. Set it
2703 up. */
2705 sgot = bfd_get_section_by_name (dynobj, ".got");
2706 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2707 BFD_ASSERT (sgot != NULL && srela != NULL);
2709 rela.r_offset = (sgot->output_section->vma
2710 + sgot->output_offset
2711 + (h->got.offset &~ (bfd_vma) 1));
2713 /* If this is a -Bsymbolic link, and the symbol is defined
2714 locally, we just want to emit a RELATIVE reloc. The entry in
2715 the global offset table will already have been initialized in
2716 the relocate_section function. */
2717 if (info->shared
2718 && SYMBOL_REFERENCES_LOCAL (info, h))
2720 rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
2721 rela.r_addend = (h->root.u.def.value
2722 + h->root.u.def.section->output_section->vma
2723 + h->root.u.def.section->output_offset);
2725 else
2727 BFD_ASSERT ((h->got.offset & 1) == 0);
2728 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_GLOB_DAT);
2729 rela.r_addend = 0;
2732 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
2733 loc = srela->contents;
2734 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2735 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2738 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2740 asection *s;
2741 Elf_Internal_Rela rela;
2742 bfd_byte *loc;
2744 /* This symbols needs a copy reloc. Set it up. */
2746 #ifdef DEBUG
2747 fprintf (stderr, ", copy");
2748 #endif
2750 BFD_ASSERT (h->dynindx != -1);
2752 if (h->size <= elf_gp_size (dynobj))
2753 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2754 ".rela.sbss");
2755 else
2756 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2757 ".rela.bss");
2758 BFD_ASSERT (s != NULL);
2760 rela.r_offset = (h->root.u.def.value
2761 + h->root.u.def.section->output_section->vma
2762 + h->root.u.def.section->output_offset);
2763 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
2764 rela.r_addend = 0;
2765 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2766 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2769 #ifdef DEBUG
2770 fprintf (stderr, "\n");
2771 #endif
2773 /* Mark some specially defined symbols as absolute. */
2774 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2775 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2776 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
2777 sym->st_shndx = SHN_ABS;
2779 return TRUE;
2782 /* Finish up the dynamic sections. */
2784 static bfd_boolean
2785 ppc_elf_finish_dynamic_sections (output_bfd, info)
2786 bfd *output_bfd;
2787 struct bfd_link_info *info;
2789 asection *sdyn;
2790 bfd *dynobj = elf_hash_table (info)->dynobj;
2791 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
2793 #ifdef DEBUG
2794 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
2795 #endif
2797 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2799 if (elf_hash_table (info)->dynamic_sections_created)
2801 asection *splt;
2802 Elf32_External_Dyn *dyncon, *dynconend;
2804 splt = bfd_get_section_by_name (dynobj, ".plt");
2805 BFD_ASSERT (splt != NULL && sdyn != NULL);
2807 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2808 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2809 for (; dyncon < dynconend; dyncon++)
2811 Elf_Internal_Dyn dyn;
2812 const char *name;
2813 bfd_boolean size;
2815 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2817 switch (dyn.d_tag)
2819 case DT_PLTGOT: name = ".plt"; size = FALSE; break;
2820 case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
2821 case DT_JMPREL: name = ".rela.plt"; size = FALSE; break;
2822 default: name = NULL; size = FALSE; break;
2825 if (name != NULL)
2827 asection *s;
2829 s = bfd_get_section_by_name (output_bfd, name);
2830 if (s == NULL)
2831 dyn.d_un.d_val = 0;
2832 else
2834 if (! size)
2835 dyn.d_un.d_ptr = s->vma;
2836 else
2838 if (s->_cooked_size != 0)
2839 dyn.d_un.d_val = s->_cooked_size;
2840 else
2841 dyn.d_un.d_val = s->_raw_size;
2844 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2849 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
2850 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
2851 if (sgot)
2853 unsigned char *contents = sgot->contents;
2854 bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
2856 if (sdyn == NULL)
2857 bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
2858 else
2859 bfd_put_32 (output_bfd,
2860 sdyn->output_section->vma + sdyn->output_offset,
2861 contents+4);
2863 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2866 return TRUE;
2869 /* The RELOCATE_SECTION function is called by the ELF backend linker
2870 to handle the relocations for a section.
2872 The relocs are always passed as Rela structures; if the section
2873 actually uses Rel structures, the r_addend field will always be
2874 zero.
2876 This function is responsible for adjust the section contents as
2877 necessary, and (if using Rela relocs and generating a
2878 relocateable output file) adjusting the reloc addend as
2879 necessary.
2881 This function does not have to worry about setting the reloc
2882 address or the reloc symbol index.
2884 LOCAL_SYMS is a pointer to the swapped in local symbols.
2886 LOCAL_SECTIONS is an array giving the section in the input file
2887 corresponding to the st_shndx field of each local symbol.
2889 The global hash table entry for the global symbols can be found
2890 via elf_sym_hashes (input_bfd).
2892 When generating relocateable output, this function must handle
2893 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2894 going to be the section symbol corresponding to the output
2895 section, which means that the addend must be adjusted
2896 accordingly. */
2898 static bfd_boolean
2899 ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
2900 contents, relocs, local_syms, local_sections)
2901 bfd *output_bfd;
2902 struct bfd_link_info *info;
2903 bfd *input_bfd;
2904 asection *input_section;
2905 bfd_byte *contents;
2906 Elf_Internal_Rela *relocs;
2907 Elf_Internal_Sym *local_syms;
2908 asection **local_sections;
2910 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2911 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
2912 bfd *dynobj = elf_hash_table (info)->dynobj;
2913 elf_linker_section_t *sdata = NULL;
2914 elf_linker_section_t *sdata2 = NULL;
2915 Elf_Internal_Rela *rel = relocs;
2916 Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
2917 asection *sreloc = NULL;
2918 asection *splt;
2919 asection *sgot;
2920 bfd_vma *local_got_offsets;
2921 bfd_boolean ret = TRUE;
2922 long insn;
2924 if (dynobj)
2926 sdata = elf_linker_section (dynobj, LINKER_SECTION_SDATA);
2927 sdata2 = elf_linker_section (dynobj, LINKER_SECTION_SDATA2);
2930 #ifdef DEBUG
2931 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
2932 bfd_archive_filename (input_bfd),
2933 bfd_section_name(input_bfd, input_section),
2934 (long) input_section->reloc_count,
2935 (info->relocateable) ? " (relocatable)" : "");
2936 #endif
2938 if (info->relocateable)
2939 return TRUE;
2941 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2942 /* Initialize howto table if needed. */
2943 ppc_elf_howto_init ();
2945 local_got_offsets = elf_local_got_offsets (input_bfd);
2947 splt = sgot = NULL;
2948 if (dynobj != NULL)
2950 splt = bfd_get_section_by_name (dynobj, ".plt");
2951 sgot = bfd_get_section_by_name (dynobj, ".got");
2954 for (; rel < relend; rel++)
2956 enum elf_ppc_reloc_type r_type = (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
2957 bfd_vma offset = rel->r_offset;
2958 bfd_vma addend = rel->r_addend;
2959 bfd_reloc_status_type r = bfd_reloc_other;
2960 Elf_Internal_Sym *sym = (Elf_Internal_Sym *) 0;
2961 asection *sec = (asection *) 0;
2962 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) 0;
2963 const char *sym_name = (const char *) 0;
2964 reloc_howto_type *howto;
2965 unsigned long r_symndx;
2966 bfd_vma relocation;
2967 int will_become_local;
2969 /* Unknown relocation handling */
2970 if ((unsigned) r_type >= (unsigned) R_PPC_max
2971 || !ppc_elf_howto_table[(int) r_type])
2973 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
2974 bfd_archive_filename (input_bfd),
2975 (int) r_type);
2977 bfd_set_error (bfd_error_bad_value);
2978 ret = FALSE;
2979 continue;
2982 howto = ppc_elf_howto_table[(int) r_type];
2983 r_symndx = ELF32_R_SYM (rel->r_info);
2985 if (r_symndx < symtab_hdr->sh_info)
2987 sym = local_syms + r_symndx;
2988 sec = local_sections[r_symndx];
2989 sym_name = "<local symbol>";
2991 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
2992 addend = rel->r_addend;
2993 /* Relocs to local symbols are always resolved. */
2994 will_become_local = 1;
2996 else
2998 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2999 while (h->root.type == bfd_link_hash_indirect
3000 || h->root.type == bfd_link_hash_warning)
3001 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3002 sym_name = h->root.root.string;
3004 /* Can this relocation be resolved immediately? */
3005 will_become_local = SYMBOL_REFERENCES_LOCAL (info, h);
3007 if (h->root.type == bfd_link_hash_defined
3008 || h->root.type == bfd_link_hash_defweak)
3010 sec = h->root.u.def.section;
3011 if (((r_type == R_PPC_PLT32
3012 || r_type == R_PPC_PLTREL24)
3013 && splt != NULL
3014 && h->plt.offset != (bfd_vma) -1)
3015 || (r_type == R_PPC_LOCAL24PC
3016 && sec->output_section == NULL)
3017 || ((r_type == R_PPC_GOT16
3018 || r_type == R_PPC_GOT16_LO
3019 || r_type == R_PPC_GOT16_HI
3020 || r_type == R_PPC_GOT16_HA)
3021 && elf_hash_table (info)->dynamic_sections_created
3022 && (! info->shared || ! will_become_local))
3023 || (info->shared
3024 && ! will_become_local
3025 && ((input_section->flags & SEC_ALLOC) != 0
3026 /* Testing SEC_DEBUGGING here may be wrong.
3027 It's here to avoid a crash when
3028 generating a shared library with DWARF
3029 debugging information. */
3030 || ((input_section->flags & SEC_DEBUGGING) != 0
3031 && (h->elf_link_hash_flags
3032 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
3033 && (r_type == R_PPC_ADDR32
3034 || r_type == R_PPC_ADDR24
3035 || r_type == R_PPC_ADDR16
3036 || r_type == R_PPC_ADDR16_LO
3037 || r_type == R_PPC_ADDR16_HI
3038 || r_type == R_PPC_ADDR16_HA
3039 || r_type == R_PPC_ADDR14
3040 || r_type == R_PPC_ADDR14_BRTAKEN
3041 || r_type == R_PPC_ADDR14_BRNTAKEN
3042 || r_type == R_PPC_COPY
3043 || r_type == R_PPC_GLOB_DAT
3044 || r_type == R_PPC_JMP_SLOT
3045 || r_type == R_PPC_UADDR32
3046 || r_type == R_PPC_UADDR16
3047 || r_type == R_PPC_SDAREL16
3048 || r_type == R_PPC_EMB_NADDR32
3049 || r_type == R_PPC_EMB_NADDR16
3050 || r_type == R_PPC_EMB_NADDR16_LO
3051 || r_type == R_PPC_EMB_NADDR16_HI
3052 || r_type == R_PPC_EMB_NADDR16_HA
3053 || r_type == R_PPC_EMB_SDAI16
3054 || r_type == R_PPC_EMB_SDA2I16
3055 || r_type == R_PPC_EMB_SDA2REL
3056 || r_type == R_PPC_EMB_SDA21
3057 || r_type == R_PPC_EMB_MRKREF
3058 || r_type == R_PPC_EMB_BIT_FLD
3059 || r_type == R_PPC_EMB_RELSDA
3060 || ((r_type == R_PPC_REL24
3061 || r_type == R_PPC_REL32
3062 || r_type == R_PPC_REL14
3063 || r_type == R_PPC_REL14_BRTAKEN
3064 || r_type == R_PPC_REL14_BRNTAKEN
3065 || r_type == R_PPC_RELATIVE)
3066 && strcmp (h->root.root.string,
3067 "_GLOBAL_OFFSET_TABLE_") != 0))))
3069 /* In these cases, we don't need the relocation
3070 value. We check specially because in some
3071 obscure cases sec->output_section will be NULL. */
3072 relocation = 0;
3074 else if (sec->output_section == NULL)
3076 (*_bfd_error_handler)
3077 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
3078 bfd_archive_filename (input_bfd), h->root.root.string,
3079 bfd_get_section_name (input_bfd, input_section));
3080 relocation = 0;
3082 else
3083 relocation = (h->root.u.def.value
3084 + sec->output_section->vma
3085 + sec->output_offset);
3087 else if (h->root.type == bfd_link_hash_undefweak)
3088 relocation = 0;
3089 else if (info->shared
3090 && (!info->symbolic || info->allow_shlib_undefined)
3091 && !info->no_undefined
3092 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3093 relocation = 0;
3094 else
3096 if (! (*info->callbacks->undefined_symbol) (info,
3097 h->root.root.string,
3098 input_bfd,
3099 input_section,
3100 rel->r_offset,
3101 (!info->shared
3102 || info->no_undefined
3103 || ELF_ST_VISIBILITY (h->other))))
3104 return FALSE;
3105 relocation = 0;
3109 switch ((int) r_type)
3111 default:
3112 (*_bfd_error_handler) (_("%s: unknown relocation type %d for symbol %s"),
3113 bfd_archive_filename (input_bfd),
3114 (int) r_type, sym_name);
3116 bfd_set_error (bfd_error_bad_value);
3117 ret = FALSE;
3118 continue;
3120 case (int) R_PPC_NONE:
3121 continue;
3123 /* Relocations that need no special processing. */
3124 case (int) R_PPC_LOCAL24PC:
3125 /* It makes no sense to point a local relocation
3126 at a symbol not in this object. */
3127 if (h != NULL
3128 && (h->root.type == bfd_link_hash_defined
3129 || h->root.type == bfd_link_hash_defweak)
3130 && sec->output_section == NULL)
3132 if (! (*info->callbacks->undefined_symbol) (info,
3133 h->root.root.string,
3134 input_bfd,
3135 input_section,
3136 rel->r_offset,
3137 TRUE))
3138 return FALSE;
3139 continue;
3141 break;
3143 /* Relocations that may need to be propagated if this is a shared
3144 object. */
3145 case (int) R_PPC_REL24:
3146 case (int) R_PPC_REL32:
3147 case (int) R_PPC_REL14:
3148 /* If these relocations are not to a named symbol, they can be
3149 handled right here, no need to bother the dynamic linker. */
3150 if (h == NULL
3151 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3152 || SYMBOL_REFERENCES_LOCAL (info, h))
3153 break;
3154 /* fall through */
3156 /* Relocations that always need to be propagated if this is a shared
3157 object. */
3158 case (int) R_PPC_ADDR32:
3159 case (int) R_PPC_ADDR24:
3160 case (int) R_PPC_ADDR16:
3161 case (int) R_PPC_ADDR16_LO:
3162 case (int) R_PPC_ADDR16_HI:
3163 case (int) R_PPC_ADDR16_HA:
3164 case (int) R_PPC_ADDR14:
3165 case (int) R_PPC_UADDR32:
3166 case (int) R_PPC_UADDR16:
3167 if (info->shared && r_symndx != 0)
3169 Elf_Internal_Rela outrel;
3170 bfd_byte *loc;
3171 int skip;
3173 #ifdef DEBUG
3174 fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
3175 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
3176 #endif
3178 /* When generating a shared object, these relocations
3179 are copied into the output file to be resolved at run
3180 time. */
3182 if (sreloc == NULL)
3184 const char *name;
3186 name = (bfd_elf_string_from_elf_section
3187 (input_bfd,
3188 elf_elfheader (input_bfd)->e_shstrndx,
3189 elf_section_data (input_section)->rel_hdr.sh_name));
3190 if (name == NULL)
3191 return FALSE;
3193 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3194 && strcmp (bfd_get_section_name (input_bfd,
3195 input_section),
3196 name + 5) == 0);
3198 sreloc = bfd_get_section_by_name (dynobj, name);
3199 BFD_ASSERT (sreloc != NULL);
3202 skip = 0;
3204 outrel.r_offset =
3205 _bfd_elf_section_offset (output_bfd, info, input_section,
3206 rel->r_offset);
3207 if (outrel.r_offset == (bfd_vma) -1
3208 || outrel.r_offset == (bfd_vma) -2)
3209 skip = (int) outrel.r_offset;
3210 outrel.r_offset += (input_section->output_section->vma
3211 + input_section->output_offset);
3213 if (skip)
3214 memset (&outrel, 0, sizeof outrel);
3215 /* h->dynindx may be -1 if this symbol was marked to
3216 become local. */
3217 else if (! will_become_local)
3219 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3220 outrel.r_addend = rel->r_addend;
3222 else
3224 outrel.r_addend = relocation + rel->r_addend;
3226 if (r_type == R_PPC_ADDR32)
3227 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3228 else
3230 long indx;
3232 if (h == NULL)
3233 sec = local_sections[r_symndx];
3234 else
3236 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3237 || (h->root.type
3238 == bfd_link_hash_defweak));
3239 sec = h->root.u.def.section;
3242 if (bfd_is_abs_section (sec))
3243 indx = 0;
3244 else if (sec == NULL || sec->owner == NULL)
3246 bfd_set_error (bfd_error_bad_value);
3247 return FALSE;
3249 else
3251 asection *osec;
3253 /* We are turning this relocation into one
3254 against a section symbol. It would be
3255 proper to subtract the symbol's value,
3256 osec->vma, from the emitted reloc addend,
3257 but ld.so expects buggy relocs. */
3258 osec = sec->output_section;
3259 indx = elf_section_data (osec)->dynindx;
3260 BFD_ASSERT (indx > 0);
3261 #ifdef DEBUG
3262 if (indx <= 0)
3264 printf ("indx=%d section=%s flags=%08x name=%s\n",
3265 indx, osec->name, osec->flags,
3266 h->root.root.string);
3268 #endif
3271 outrel.r_info = ELF32_R_INFO (indx, r_type);
3275 loc = sreloc->contents;
3276 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3277 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3279 if (skip == -1)
3280 continue;
3282 /* This reloc will be computed at runtime. We clear the memory
3283 so that it contains predictable value. */
3284 if (! skip
3285 && ((input_section->flags & SEC_ALLOC) != 0
3286 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
3288 relocation = howto->pc_relative ? outrel.r_offset : 0;
3289 addend = 0;
3290 break;
3294 /* Arithmetic adjust relocations that aren't going into a
3295 shared object. */
3296 if (r_type == R_PPC_ADDR16_HA
3297 /* It's just possible that this symbol is a weak symbol
3298 that's not actually defined anywhere. In that case,
3299 'sec' would be NULL, and we should leave the symbol
3300 alone (it will be set to zero elsewhere in the link). */
3301 && sec != NULL)
3303 addend += ((relocation + addend) & 0x8000) << 1;
3305 break;
3307 /* branch taken prediction relocations */
3308 case (int) R_PPC_ADDR14_BRTAKEN:
3309 case (int) R_PPC_REL14_BRTAKEN:
3310 insn = bfd_get_32 (output_bfd, contents + offset);
3311 if ((relocation - offset) & 0x8000)
3312 insn &= ~BRANCH_PREDICT_BIT;
3313 else
3314 insn |= BRANCH_PREDICT_BIT;
3315 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3316 break;
3318 /* branch not taken predicition relocations */
3319 case (int) R_PPC_ADDR14_BRNTAKEN:
3320 case (int) R_PPC_REL14_BRNTAKEN:
3321 insn = bfd_get_32 (output_bfd, contents + offset);
3322 if ((relocation - offset) & 0x8000)
3323 insn |= BRANCH_PREDICT_BIT;
3324 else
3325 insn &= ~BRANCH_PREDICT_BIT;
3326 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3327 break;
3329 /* GOT16 relocations */
3330 case (int) R_PPC_GOT16:
3331 case (int) R_PPC_GOT16_LO:
3332 case (int) R_PPC_GOT16_HI:
3333 case (int) R_PPC_GOT16_HA:
3334 /* Relocation is to the entry for this symbol in the global
3335 offset table. */
3336 BFD_ASSERT (sgot != NULL);
3338 if (h != NULL)
3340 bfd_vma off;
3342 off = h->got.offset;
3343 BFD_ASSERT (off != (bfd_vma) -1);
3345 if (! elf_hash_table (info)->dynamic_sections_created
3346 || (info->shared
3347 && SYMBOL_REFERENCES_LOCAL (info, h)))
3349 /* This is actually a static link, or it is a
3350 -Bsymbolic link and the symbol is defined
3351 locally. We must initialize this entry in the
3352 global offset table. Since the offset must
3353 always be a multiple of 4, we use the least
3354 significant bit to record whether we have
3355 initialized it already.
3357 When doing a dynamic link, we create a .rela.got
3358 relocation entry to initialize the value. This
3359 is done in the finish_dynamic_symbol routine. */
3360 if ((off & 1) != 0)
3361 off &= ~1;
3362 else
3364 bfd_put_32 (output_bfd, relocation,
3365 sgot->contents + off);
3366 h->got.offset |= 1;
3370 relocation = sgot->output_offset + off - 4;
3372 else
3374 bfd_vma off;
3376 BFD_ASSERT (local_got_offsets != NULL
3377 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3379 off = local_got_offsets[r_symndx];
3381 /* The offset must always be a multiple of 4. We use
3382 the least significant bit to record whether we have
3383 already processed this entry. */
3384 if ((off & 1) != 0)
3385 off &= ~1;
3386 else
3389 if (info->shared)
3391 asection *srelgot;
3392 Elf_Internal_Rela outrel;
3393 bfd_byte *loc;
3395 /* We need to generate a R_PPC_RELATIVE reloc
3396 for the dynamic linker. */
3397 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3398 BFD_ASSERT (srelgot != NULL);
3400 outrel.r_offset = (sgot->output_section->vma
3401 + sgot->output_offset
3402 + off);
3403 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3404 outrel.r_addend = relocation;
3405 loc = srelgot->contents;
3406 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3407 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3408 relocation = 0;
3411 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3412 local_got_offsets[r_symndx] |= 1;
3415 relocation = sgot->output_offset + off - 4;
3417 break;
3419 /* Indirect .sdata relocation */
3420 case (int) R_PPC_EMB_SDAI16:
3421 BFD_ASSERT (sdata != NULL);
3422 relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3423 sdata, h, relocation, rel,
3424 R_PPC_RELATIVE);
3425 break;
3427 /* Indirect .sdata2 relocation */
3428 case (int) R_PPC_EMB_SDA2I16:
3429 BFD_ASSERT (sdata2 != NULL);
3430 relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3431 sdata2, h, relocation, rel,
3432 R_PPC_RELATIVE);
3433 break;
3435 /* Handle the TOC16 reloc. We want to use the offset within the .got
3436 section, not the actual VMA. This is appropriate when generating
3437 an embedded ELF object, for which the .got section acts like the
3438 AIX .toc section. */
3439 case (int) R_PPC_TOC16: /* phony GOT16 relocations */
3440 BFD_ASSERT (sec != (asection *) 0);
3441 BFD_ASSERT (bfd_is_und_section (sec)
3442 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
3443 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
3445 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
3446 break;
3448 case (int) R_PPC_PLTREL24:
3449 /* Relocation is to the entry for this symbol in the
3450 procedure linkage table. */
3451 BFD_ASSERT (h != NULL);
3453 if (h->plt.offset == (bfd_vma) -1
3454 || splt == NULL)
3456 /* We didn't make a PLT entry for this symbol. This
3457 happens when statically linking PIC code, or when
3458 using -Bsymbolic. */
3459 break;
3462 relocation = (splt->output_section->vma
3463 + splt->output_offset
3464 + h->plt.offset);
3465 break;
3467 /* relocate against _SDA_BASE_ */
3468 case (int) R_PPC_SDAREL16:
3470 const char *name;
3472 BFD_ASSERT (sec != (asection *) 0);
3473 name = bfd_get_section_name (abfd, sec->output_section);
3474 if (! ((strncmp (name, ".sdata", 6) == 0
3475 && (name[6] == 0 || name[6] == '.'))
3476 || (strncmp (name, ".sbss", 5) == 0
3477 && (name[5] == 0 || name[5] == '.'))))
3479 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3480 bfd_archive_filename (input_bfd),
3481 sym_name,
3482 ppc_elf_howto_table[(int) r_type]->name,
3483 name);
3485 addend -= (sdata->sym_hash->root.u.def.value
3486 + sdata->sym_hash->root.u.def.section->output_section->vma
3487 + sdata->sym_hash->root.u.def.section->output_offset);
3489 break;
3491 /* relocate against _SDA2_BASE_ */
3492 case (int) R_PPC_EMB_SDA2REL:
3494 const char *name;
3496 BFD_ASSERT (sec != (asection *) 0);
3497 name = bfd_get_section_name (abfd, sec->output_section);
3498 if (! (strncmp (name, ".sdata2", 7) == 0
3499 || strncmp (name, ".sbss2", 6) == 0))
3501 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3502 bfd_archive_filename (input_bfd),
3503 sym_name,
3504 ppc_elf_howto_table[(int) r_type]->name,
3505 name);
3507 bfd_set_error (bfd_error_bad_value);
3508 ret = FALSE;
3509 continue;
3511 addend -= (sdata2->sym_hash->root.u.def.value
3512 + sdata2->sym_hash->root.u.def.section->output_section->vma
3513 + sdata2->sym_hash->root.u.def.section->output_offset);
3515 break;
3517 /* relocate against either _SDA_BASE_, _SDA2_BASE_, or 0 */
3518 case (int) R_PPC_EMB_SDA21:
3519 case (int) R_PPC_EMB_RELSDA:
3521 const char *name;
3522 int reg;
3524 BFD_ASSERT (sec != (asection *) 0);
3525 name = bfd_get_section_name (abfd, sec->output_section);
3526 if (((strncmp (name, ".sdata", 6) == 0
3527 && (name[6] == 0 || name[6] == '.'))
3528 || (strncmp (name, ".sbss", 5) == 0
3529 && (name[5] == 0 || name[5] == '.'))))
3531 reg = 13;
3532 addend -= (sdata->sym_hash->root.u.def.value
3533 + sdata->sym_hash->root.u.def.section->output_section->vma
3534 + sdata->sym_hash->root.u.def.section->output_offset);
3537 else if (strncmp (name, ".sdata2", 7) == 0
3538 || strncmp (name, ".sbss2", 6) == 0)
3540 reg = 2;
3541 addend -= (sdata2->sym_hash->root.u.def.value
3542 + sdata2->sym_hash->root.u.def.section->output_section->vma
3543 + sdata2->sym_hash->root.u.def.section->output_offset);
3546 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
3547 || strcmp (name, ".PPC.EMB.sbss0") == 0)
3549 reg = 0;
3552 else
3554 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3555 bfd_archive_filename (input_bfd),
3556 sym_name,
3557 ppc_elf_howto_table[(int) r_type]->name,
3558 name);
3560 bfd_set_error (bfd_error_bad_value);
3561 ret = FALSE;
3562 continue;
3565 if (r_type == R_PPC_EMB_SDA21)
3566 { /* fill in register field */
3567 insn = bfd_get_32 (output_bfd, contents + offset);
3568 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
3569 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3572 break;
3574 /* Relocate against the beginning of the section */
3575 case (int) R_PPC_SECTOFF:
3576 case (int) R_PPC_SECTOFF_LO:
3577 case (int) R_PPC_SECTOFF_HI:
3578 BFD_ASSERT (sec != (asection *) 0);
3579 addend -= sec->output_section->vma;
3580 break;
3582 case (int) R_PPC_SECTOFF_HA:
3583 BFD_ASSERT (sec != (asection *) 0);
3584 addend -= sec->output_section->vma;
3585 addend += ((relocation + addend) & 0x8000) << 1;
3586 break;
3588 /* Negative relocations */
3589 case (int) R_PPC_EMB_NADDR32:
3590 case (int) R_PPC_EMB_NADDR16:
3591 case (int) R_PPC_EMB_NADDR16_LO:
3592 case (int) R_PPC_EMB_NADDR16_HI:
3593 addend -= 2 * relocation;
3594 break;
3596 case (int) R_PPC_EMB_NADDR16_HA:
3597 addend -= 2 * relocation;
3598 addend += ((relocation + addend) & 0x8000) << 1;
3599 break;
3601 /* NOP relocation that prevents garbage collecting linkers from omitting a
3602 reference. */
3603 case (int) R_PPC_EMB_MRKREF:
3604 continue;
3606 case (int) R_PPC_COPY:
3607 case (int) R_PPC_GLOB_DAT:
3608 case (int) R_PPC_JMP_SLOT:
3609 case (int) R_PPC_RELATIVE:
3610 case (int) R_PPC_PLT32:
3611 case (int) R_PPC_PLTREL32:
3612 case (int) R_PPC_PLT16_LO:
3613 case (int) R_PPC_PLT16_HI:
3614 case (int) R_PPC_PLT16_HA:
3615 case (int) R_PPC_EMB_RELSEC16:
3616 case (int) R_PPC_EMB_RELST_LO:
3617 case (int) R_PPC_EMB_RELST_HI:
3618 case (int) R_PPC_EMB_RELST_HA:
3619 case (int) R_PPC_EMB_BIT_FLD:
3620 (*_bfd_error_handler) (_("%s: Relocation %s is not yet supported for symbol %s."),
3621 bfd_archive_filename (input_bfd),
3622 ppc_elf_howto_table[(int) r_type]->name,
3623 sym_name);
3625 bfd_set_error (bfd_error_invalid_operation);
3626 ret = FALSE;
3627 continue;
3629 case (int) R_PPC_GNU_VTINHERIT:
3630 case (int) R_PPC_GNU_VTENTRY:
3631 /* These are no-ops in the end. */
3632 continue;
3635 #ifdef DEBUG
3636 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
3637 howto->name,
3638 (int) r_type,
3639 sym_name,
3640 r_symndx,
3641 (long) offset,
3642 (long) addend);
3643 #endif
3645 r = _bfd_final_link_relocate (howto,
3646 input_bfd,
3647 input_section,
3648 contents,
3649 offset,
3650 relocation,
3651 addend);
3653 if (r == bfd_reloc_ok)
3655 else if (r == bfd_reloc_overflow)
3657 const char *name;
3659 if (h != NULL)
3661 if (h->root.type == bfd_link_hash_undefweak
3662 && howto->pc_relative)
3664 /* Assume this is a call protected by other code that
3665 detect the symbol is undefined. If this is the case,
3666 we can safely ignore the overflow. If not, the
3667 program is hosed anyway, and a little warning isn't
3668 going to help. */
3670 continue;
3673 name = h->root.root.string;
3675 else
3677 name = bfd_elf_string_from_elf_section (input_bfd,
3678 symtab_hdr->sh_link,
3679 sym->st_name);
3680 if (name == NULL)
3681 continue;
3682 if (*name == '\0')
3683 name = bfd_section_name (input_bfd, sec);
3686 if (! (*info->callbacks->reloc_overflow) (info,
3687 name,
3688 howto->name,
3689 (bfd_vma) 0,
3690 input_bfd,
3691 input_section,
3692 offset))
3693 return FALSE;
3695 else
3696 ret = FALSE;
3699 #ifdef DEBUG
3700 fprintf (stderr, "\n");
3701 #endif
3703 return ret;
3706 static enum elf_reloc_type_class
3707 ppc_elf_reloc_type_class (rela)
3708 const Elf_Internal_Rela *rela;
3710 switch ((int) ELF32_R_TYPE (rela->r_info))
3712 case R_PPC_RELATIVE:
3713 return reloc_class_relative;
3714 case R_PPC_REL24:
3715 case R_PPC_ADDR24:
3716 case R_PPC_JMP_SLOT:
3717 return reloc_class_plt;
3718 case R_PPC_COPY:
3719 return reloc_class_copy;
3720 default:
3721 return reloc_class_normal;
3725 /* Support for core dump NOTE sections */
3726 static bfd_boolean
3727 ppc_elf_grok_prstatus (abfd, note)
3728 bfd *abfd;
3729 Elf_Internal_Note *note;
3731 int offset;
3732 unsigned int raw_size;
3734 switch (note->descsz)
3736 default:
3737 return FALSE;
3739 case 268: /* Linux/PPC */
3740 /* pr_cursig */
3741 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
3743 /* pr_pid */
3744 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
3746 /* pr_reg */
3747 offset = 72;
3748 raw_size = 192;
3750 break;
3753 /* Make a ".reg/999" section. */
3754 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3755 raw_size, note->descpos + offset);
3758 static bfd_boolean
3759 ppc_elf_grok_psinfo (abfd, note)
3760 bfd *abfd;
3761 Elf_Internal_Note *note;
3763 switch (note->descsz)
3765 default:
3766 return FALSE;
3768 case 128: /* Linux/PPC elf_prpsinfo */
3769 elf_tdata (abfd)->core_program
3770 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
3771 elf_tdata (abfd)->core_command
3772 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
3775 /* Note that for some reason, a spurious space is tacked
3776 onto the end of the args in some (at least one anyway)
3777 implementations, so strip it off if it exists. */
3780 char *command = elf_tdata (abfd)->core_command;
3781 int n = strlen (command);
3783 if (0 < n && command[n - 1] == ' ')
3784 command[n - 1] = '\0';
3787 return TRUE;
3790 /* Very simple linked list structure for recording apuinfo values. */
3791 typedef struct apuinfo_list
3793 struct apuinfo_list * next;
3794 unsigned long value;
3796 apuinfo_list;
3798 static apuinfo_list * head;
3800 static void apuinfo_list_init PARAMS ((void));
3801 static void apuinfo_list_add PARAMS ((unsigned long));
3802 static unsigned apuinfo_list_length PARAMS ((void));
3803 static unsigned long apuinfo_list_element PARAMS ((unsigned long));
3804 static void apuinfo_list_finish PARAMS ((void));
3806 extern void ppc_elf_begin_write_processing
3807 PARAMS ((bfd *, struct bfd_link_info *));
3808 extern void ppc_elf_final_write_processing
3809 PARAMS ((bfd *, bfd_boolean));
3810 extern bfd_boolean ppc_elf_write_section
3811 PARAMS ((bfd *, asection *, bfd_byte *));
3815 static void
3816 apuinfo_list_init PARAMS ((void))
3818 head = NULL;
3821 static void
3822 apuinfo_list_add (value)
3823 unsigned long value;
3825 apuinfo_list * entry = head;
3827 while (entry != NULL)
3829 if (entry->value == value)
3830 return;
3831 entry = entry->next;
3834 entry = bfd_malloc (sizeof (* entry));
3835 if (entry == NULL)
3836 return;
3838 entry->value = value;
3839 entry->next = head;
3840 head = entry;
3843 static unsigned
3844 apuinfo_list_length PARAMS ((void))
3846 apuinfo_list * entry;
3847 unsigned long count;
3849 for (entry = head, count = 0;
3850 entry;
3851 entry = entry->next)
3852 ++ count;
3854 return count;
3857 static inline unsigned long
3858 apuinfo_list_element (number)
3859 unsigned long number;
3861 apuinfo_list * entry;
3863 for (entry = head;
3864 entry && number --;
3865 entry = entry->next)
3868 return entry ? entry->value : 0;
3871 static void
3872 apuinfo_list_finish PARAMS ((void))
3874 apuinfo_list * entry;
3876 for (entry = head; entry;)
3878 apuinfo_list * next = entry->next;
3879 free (entry);
3880 entry = next;
3883 head = NULL;
3886 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
3887 #define APUINFO_LABEL "APUinfo"
3889 /* Scan the input BFDs and create a linked list of
3890 the APUinfo values that will need to be emitted. */
3892 void
3893 ppc_elf_begin_write_processing (abfd, link_info)
3894 bfd *abfd;
3895 struct bfd_link_info *link_info;
3897 bfd * ibfd;
3898 asection * asec;
3899 char * buffer;
3900 unsigned num_input_sections;
3901 bfd_size_type output_section_size;
3902 unsigned i;
3903 unsigned num_entries;
3904 unsigned long offset;
3905 unsigned long length;
3906 const char * error_message = NULL;
3908 if (link_info == NULL)
3909 return;
3911 /* Scan the input bfds, looking for apuinfo sections. */
3912 num_input_sections = 0;
3913 output_section_size = 0;
3915 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
3917 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
3918 if (asec)
3920 ++ num_input_sections;
3921 output_section_size += asec->_raw_size;
3925 /* We need at least one input sections
3926 in order to make merging worthwhile. */
3927 if (num_input_sections < 1)
3928 return;
3930 /* Just make sure that the output section exists as well. */
3931 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
3932 if (asec == NULL)
3933 return;
3935 /* Allocate a buffer for the contents of the input sections. */
3936 buffer = bfd_malloc (output_section_size);
3937 if (buffer == NULL)
3938 return;
3940 offset = 0;
3941 apuinfo_list_init ();
3943 /* Read in the input sections contents. */
3944 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
3946 unsigned long datum;
3947 char * ptr;
3950 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
3951 if (asec == NULL)
3952 continue;
3954 length = asec->_raw_size;
3955 if (length < 24)
3957 error_message = _("corrupt or empty %s section in %s");
3958 goto fail;
3961 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
3962 || (bfd_bread (buffer + offset, length, ibfd) != length))
3964 error_message = _("unable to read in %s section from %s");
3965 goto fail;
3968 /* Process the contents of the section. */
3969 ptr = buffer + offset;
3970 error_message = _("corrupt %s section in %s");
3972 /* Verify the contents of the header. Note - we have to
3973 extract the values this way in order to allow for a
3974 host whose endian-ness is different from the target. */
3975 datum = bfd_get_32 (ibfd, ptr);
3976 if (datum != sizeof APUINFO_LABEL)
3977 goto fail;
3979 datum = bfd_get_32 (ibfd, ptr + 8);
3980 if (datum != 0x2)
3981 goto fail;
3983 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
3984 goto fail;
3986 /* Get the number of apuinfo entries. */
3987 datum = bfd_get_32 (ibfd, ptr + 4);
3988 if ((datum * 4 + 20) != length)
3989 goto fail;
3991 /* Make sure that we do not run off the end of the section. */
3992 if (offset + length > output_section_size)
3993 goto fail;
3995 /* Scan the apuinfo section, building a list of apuinfo numbers. */
3996 for (i = 0; i < datum; i++)
3997 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + (i * 4)));
3999 /* Update the offset. */
4000 offset += length;
4003 error_message = NULL;
4005 /* Compute the size of the output section. */
4006 num_entries = apuinfo_list_length ();
4007 output_section_size = 20 + num_entries * 4;
4009 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
4011 if (! bfd_set_section_size (abfd, asec, output_section_size))
4012 ibfd = abfd,
4013 error_message = _("warning: unable to set size of %s section in %s");
4015 fail:
4016 free (buffer);
4018 if (error_message)
4019 _bfd_error_handler (error_message, APUINFO_SECTION_NAME,
4020 bfd_archive_filename (ibfd));
4024 /* Prevent the output section from accumulating the input sections'
4025 contents. We have already stored this in our linked list structure. */
4027 bfd_boolean
4028 ppc_elf_write_section (abfd, asec, contents)
4029 bfd * abfd ATTRIBUTE_UNUSED;
4030 asection * asec;
4031 bfd_byte * contents ATTRIBUTE_UNUSED;
4033 return apuinfo_list_length () && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
4037 /* Finally we can generate the output section. */
4039 void
4040 ppc_elf_final_write_processing (abfd, linker)
4041 bfd * abfd;
4042 bfd_boolean linker ATTRIBUTE_UNUSED;
4044 bfd_byte * buffer;
4045 asection * asec;
4046 unsigned i;
4047 unsigned num_entries;
4048 bfd_size_type length;
4050 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
4051 if (asec == NULL)
4052 return;
4054 if (apuinfo_list_length () == 0)
4055 return;
4057 length = asec->_raw_size;
4058 if (length < 20)
4059 return;
4061 buffer = bfd_malloc (length);
4062 if (buffer == NULL)
4064 _bfd_error_handler (_("failed to allocate space for new APUinfo section."));
4065 return;
4068 /* Create the apuinfo header. */
4069 num_entries = apuinfo_list_length ();
4070 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
4071 bfd_put_32 (abfd, num_entries, buffer + 4);
4072 bfd_put_32 (abfd, 0x2, buffer + 8);
4073 strcpy (buffer + 12, APUINFO_LABEL);
4075 length = 20;
4076 for (i = 0; i < num_entries; i++)
4078 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
4079 length += 4;
4082 if (length != asec->_raw_size)
4083 _bfd_error_handler (_("failed to compute new APUinfo section."));
4085 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
4086 _bfd_error_handler (_("failed to install new APUinfo section."));
4088 free (buffer);
4090 apuinfo_list_finish ();
4093 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
4094 #define TARGET_LITTLE_NAME "elf32-powerpcle"
4095 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
4096 #define TARGET_BIG_NAME "elf32-powerpc"
4097 #define ELF_ARCH bfd_arch_powerpc
4098 #define ELF_MACHINE_CODE EM_PPC
4099 #define ELF_MAXPAGESIZE 0x10000
4100 #define elf_info_to_howto ppc_elf_info_to_howto
4102 #ifdef EM_CYGNUS_POWERPC
4103 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
4104 #endif
4106 #ifdef EM_PPC_OLD
4107 #define ELF_MACHINE_ALT2 EM_PPC_OLD
4108 #endif
4110 #define elf_backend_plt_not_loaded 1
4111 #define elf_backend_got_symbol_offset 4
4112 #define elf_backend_can_gc_sections 1
4113 #define elf_backend_can_refcount 1
4114 #define elf_backend_got_header_size 12
4115 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
4116 #define elf_backend_rela_normal 1
4118 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
4119 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
4120 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
4121 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
4122 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
4124 #define elf_backend_object_p ppc_elf_object_p
4125 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
4126 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
4127 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
4128 #define elf_backend_relocate_section ppc_elf_relocate_section
4129 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
4130 #define elf_backend_check_relocs ppc_elf_check_relocs
4131 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
4132 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
4133 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
4134 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
4135 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
4136 #define elf_backend_fake_sections ppc_elf_fake_sections
4137 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
4138 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
4139 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
4140 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
4141 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
4142 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
4143 #define elf_backend_final_write_processing ppc_elf_final_write_processing
4144 #define elf_backend_write_section ppc_elf_write_section
4146 #include "elf32-target.h"