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