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