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