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