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