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