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