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