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