* bfd-in.h (STRING_AND_COMMA): New macro. Takes one constant string as its
[external/binutils.git] / bfd / elf32-ppc.c
1 /* PowerPC-specific support for 32-bit ELF
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005, 2006 Free Software Foundation, Inc.
4    Written by Ian Lance Taylor, Cygnus Support.
5
6    This file is part of BFD, the Binary File Descriptor library.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the
20    Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 /* This file is based on a preliminary PowerPC ELF ABI.  The
24    information may not match the final PowerPC ELF ABI.  It includes
25    suggestions from the in-progress Embedded PowerPC ABI, and that
26    information may also not match.  */
27
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/ppc.h"
34 #include "elf32-ppc.h"
35 #include "elf-vxworks.h"
36
37 /* RELA relocations are used here.  */
38
39 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
40   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41 static bfd_reloc_status_type ppc_elf_unhandled_reloc
42   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43
44 /* Branch prediction bit for branch taken relocs.  */
45 #define BRANCH_PREDICT_BIT 0x200000
46 /* Mask to set RA in memory instructions.  */
47 #define RA_REGISTER_MASK 0x001f0000
48 /* Value to shift register by to insert RA.  */
49 #define RA_REGISTER_SHIFT 16
50
51 /* The name of the dynamic interpreter.  This is put in the .interp
52    section.  */
53 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
54
55 /* For old-style PLT.  */
56 /* The number of single-slot PLT entries (the rest use two slots).  */
57 #define PLT_NUM_SINGLE_ENTRIES 8192
58
59 /* For new-style .glink and .plt.  */
60 #define GLINK_PLTRESOLVE 16*4
61 #define GLINK_ENTRY_SIZE 4*4
62
63 /* VxWorks uses its own plt layout, filled in by the static linker.  */
64
65 /* The standard VxWorks PLT entry.  */
66 #define VXWORKS_PLT_ENTRY_SIZE 32
67 static const bfd_vma ppc_elf_vxworks_plt_entry
68     [VXWORKS_PLT_ENTRY_SIZE / 4] =
69   {
70     0x3d800000, /* lis     r12,0                 */
71     0x818c0000, /* lwz     r12,0(r12)            */
72     0x7d8903a6, /* mtctr   r12                   */
73     0x4e800420, /* bctr                          */
74     0x39600000, /* li      r11,0                 */
75     0x48000000, /* b       14 <.PLT0resolve+0x4> */
76     0x60000000, /* nop                           */
77     0x60000000, /* nop                           */
78   };
79 static const bfd_vma ppc_elf_vxworks_pic_plt_entry
80     [VXWORKS_PLT_ENTRY_SIZE / 4] =
81   {
82     0x3d9e0000, /* addis r12,r30,0 */
83     0x818c0000, /* lwz   r12,0(r12) */
84     0x7d8903a6, /* mtctr r12 */
85     0x4e800420, /* bctr */
86     0x39600000, /* li    r11,0 */
87     0x48000000, /* b     14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
88     0x60000000, /* nop */
89     0x60000000, /* nop */
90   };
91
92 /* The initial VxWorks PLT entry.  */
93 #define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
94 static const bfd_vma ppc_elf_vxworks_plt0_entry
95     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
96   {
97     0x3d800000, /* lis     r12,0        */
98     0x398c0000, /* addi    r12,r12,0    */
99     0x800c0008, /* lwz     r0,8(r12)    */
100     0x7c0903a6, /* mtctr   r0           */
101     0x818c0004, /* lwz     r12,4(r12)   */
102     0x4e800420, /* bctr                 */
103     0x60000000, /* nop                  */
104     0x60000000, /* nop                  */
105   };
106 static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
107     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
108   {
109     0x819e0008, /* lwz   r12,8(r30) */
110     0x7d8903a6, /* mtctr r12        */
111     0x819e0004, /* lwz   r12,4(r30) */
112     0x4e800420, /* bctr             */
113     0x60000000, /* nop              */
114     0x60000000, /* nop              */
115     0x60000000, /* nop              */
116     0x60000000, /* nop              */
117   };
118
119 /* For executables, we have some additional relocations in
120    .rela.plt.unloaded, for the kernel loader.  */
121
122 /* The number of non-JMP_SLOT relocations per PLT0 slot. */
123 #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
124 /* The number of relocations in the PLTResolve slot. */
125 #define VXWORKS_PLTRESOLVE_RELOCS 2
126 /* The number of relocations in the PLTResolve slot when when creating
127    a shared library. */
128 #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
129
130 /* Some instructions.  */
131 #define ADDIS_11_11     0x3d6b0000
132 #define ADDIS_11_30     0x3d7e0000
133 #define ADDIS_12_12     0x3d8c0000
134 #define ADDI_11_11      0x396b0000
135 #define ADD_0_11_11     0x7c0b5a14
136 #define ADD_11_0_11     0x7d605a14
137 #define B               0x48000000
138 #define BCL_20_31       0x429f0005
139 #define BCTR            0x4e800420
140 #define LIS_11          0x3d600000
141 #define LIS_12          0x3d800000
142 #define LWZU_0_12       0x840c0000
143 #define LWZ_0_12        0x800c0000
144 #define LWZ_11_11       0x816b0000
145 #define LWZ_11_30       0x817e0000
146 #define LWZ_12_12       0x818c0000
147 #define MFLR_0          0x7c0802a6
148 #define MFLR_12         0x7d8802a6
149 #define MTCTR_0         0x7c0903a6
150 #define MTCTR_11        0x7d6903a6
151 #define MTLR_0          0x7c0803a6
152 #define NOP             0x60000000
153 #define SUB_11_11_12    0x7d6c5850
154
155 /* Offset of tp and dtp pointers from start of TLS block.  */
156 #define TP_OFFSET       0x7000
157 #define DTP_OFFSET      0x8000
158 \f
159 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
160
161 static reloc_howto_type ppc_elf_howto_raw[] = {
162   /* This reloc does nothing.  */
163   HOWTO (R_PPC_NONE,            /* type */
164          0,                     /* rightshift */
165          2,                     /* size (0 = byte, 1 = short, 2 = long) */
166          32,                    /* bitsize */
167          FALSE,                 /* pc_relative */
168          0,                     /* bitpos */
169          complain_overflow_bitfield, /* complain_on_overflow */
170          bfd_elf_generic_reloc, /* special_function */
171          "R_PPC_NONE",          /* name */
172          FALSE,                 /* partial_inplace */
173          0,                     /* src_mask */
174          0,                     /* dst_mask */
175          FALSE),                /* pcrel_offset */
176
177   /* A standard 32 bit relocation.  */
178   HOWTO (R_PPC_ADDR32,          /* type */
179          0,                     /* rightshift */
180          2,                     /* size (0 = byte, 1 = short, 2 = long) */
181          32,                    /* bitsize */
182          FALSE,                 /* pc_relative */
183          0,                     /* bitpos */
184          complain_overflow_bitfield, /* complain_on_overflow */
185          bfd_elf_generic_reloc, /* special_function */
186          "R_PPC_ADDR32",        /* name */
187          FALSE,                 /* partial_inplace */
188          0,                     /* src_mask */
189          0xffffffff,            /* dst_mask */
190          FALSE),                /* pcrel_offset */
191
192   /* An absolute 26 bit branch; the lower two bits must be zero.
193      FIXME: we don't check that, we just clear them.  */
194   HOWTO (R_PPC_ADDR24,          /* type */
195          0,                     /* rightshift */
196          2,                     /* size (0 = byte, 1 = short, 2 = long) */
197          26,                    /* bitsize */
198          FALSE,                 /* pc_relative */
199          0,                     /* bitpos */
200          complain_overflow_bitfield, /* complain_on_overflow */
201          bfd_elf_generic_reloc, /* special_function */
202          "R_PPC_ADDR24",        /* name */
203          FALSE,                 /* partial_inplace */
204          0,                     /* src_mask */
205          0x3fffffc,             /* dst_mask */
206          FALSE),                /* pcrel_offset */
207
208   /* A standard 16 bit relocation.  */
209   HOWTO (R_PPC_ADDR16,          /* type */
210          0,                     /* rightshift */
211          1,                     /* size (0 = byte, 1 = short, 2 = long) */
212          16,                    /* bitsize */
213          FALSE,                 /* pc_relative */
214          0,                     /* bitpos */
215          complain_overflow_bitfield, /* complain_on_overflow */
216          bfd_elf_generic_reloc, /* special_function */
217          "R_PPC_ADDR16",        /* name */
218          FALSE,                 /* partial_inplace */
219          0,                     /* src_mask */
220          0xffff,                /* dst_mask */
221          FALSE),                /* pcrel_offset */
222
223   /* A 16 bit relocation without overflow.  */
224   HOWTO (R_PPC_ADDR16_LO,       /* type */
225          0,                     /* rightshift */
226          1,                     /* size (0 = byte, 1 = short, 2 = long) */
227          16,                    /* bitsize */
228          FALSE,                 /* pc_relative */
229          0,                     /* bitpos */
230          complain_overflow_dont,/* complain_on_overflow */
231          bfd_elf_generic_reloc, /* special_function */
232          "R_PPC_ADDR16_LO",     /* name */
233          FALSE,                 /* partial_inplace */
234          0,                     /* src_mask */
235          0xffff,                /* dst_mask */
236          FALSE),                /* pcrel_offset */
237
238   /* The high order 16 bits of an address.  */
239   HOWTO (R_PPC_ADDR16_HI,       /* type */
240          16,                    /* rightshift */
241          1,                     /* size (0 = byte, 1 = short, 2 = long) */
242          16,                    /* bitsize */
243          FALSE,                 /* pc_relative */
244          0,                     /* bitpos */
245          complain_overflow_dont, /* complain_on_overflow */
246          bfd_elf_generic_reloc, /* special_function */
247          "R_PPC_ADDR16_HI",     /* name */
248          FALSE,                 /* partial_inplace */
249          0,                     /* src_mask */
250          0xffff,                /* dst_mask */
251          FALSE),                /* pcrel_offset */
252
253   /* The high order 16 bits of an address, plus 1 if the contents of
254      the low 16 bits, treated as a signed number, is negative.  */
255   HOWTO (R_PPC_ADDR16_HA,       /* type */
256          16,                    /* rightshift */
257          1,                     /* size (0 = byte, 1 = short, 2 = long) */
258          16,                    /* bitsize */
259          FALSE,                 /* pc_relative */
260          0,                     /* bitpos */
261          complain_overflow_dont, /* complain_on_overflow */
262          ppc_elf_addr16_ha_reloc, /* special_function */
263          "R_PPC_ADDR16_HA",     /* name */
264          FALSE,                 /* partial_inplace */
265          0,                     /* src_mask */
266          0xffff,                /* dst_mask */
267          FALSE),                /* pcrel_offset */
268
269   /* An absolute 16 bit branch; the lower two bits must be zero.
270      FIXME: we don't check that, we just clear them.  */
271   HOWTO (R_PPC_ADDR14,          /* type */
272          0,                     /* rightshift */
273          2,                     /* size (0 = byte, 1 = short, 2 = long) */
274          16,                    /* bitsize */
275          FALSE,                 /* pc_relative */
276          0,                     /* bitpos */
277          complain_overflow_bitfield, /* complain_on_overflow */
278          bfd_elf_generic_reloc, /* special_function */
279          "R_PPC_ADDR14",        /* name */
280          FALSE,                 /* partial_inplace */
281          0,                     /* src_mask */
282          0xfffc,                /* dst_mask */
283          FALSE),                /* pcrel_offset */
284
285   /* An absolute 16 bit branch, for which bit 10 should be set to
286      indicate that the branch is expected to be taken.  The lower two
287      bits must be zero.  */
288   HOWTO (R_PPC_ADDR14_BRTAKEN,  /* 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_BRTAKEN",/* 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 not expected to be taken.  The lower
304      two bits must be zero.  */
305   HOWTO (R_PPC_ADDR14_BRNTAKEN, /* 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_BRNTAKEN",/* name */
314          FALSE,                 /* partial_inplace */
315          0,                     /* src_mask */
316          0xfffc,                /* dst_mask */
317          FALSE),                /* pcrel_offset */
318
319   /* A relative 26 bit branch; the lower two bits must be zero.  */
320   HOWTO (R_PPC_REL24,           /* type */
321          0,                     /* rightshift */
322          2,                     /* size (0 = byte, 1 = short, 2 = long) */
323          26,                    /* bitsize */
324          TRUE,                  /* pc_relative */
325          0,                     /* bitpos */
326          complain_overflow_signed, /* complain_on_overflow */
327          bfd_elf_generic_reloc, /* special_function */
328          "R_PPC_REL24",         /* name */
329          FALSE,                 /* partial_inplace */
330          0,                     /* src_mask */
331          0x3fffffc,             /* dst_mask */
332          TRUE),                 /* pcrel_offset */
333
334   /* A relative 16 bit branch; the lower two bits must be zero.  */
335   HOWTO (R_PPC_REL14,           /* type */
336          0,                     /* rightshift */
337          2,                     /* size (0 = byte, 1 = short, 2 = long) */
338          16,                    /* bitsize */
339          TRUE,                  /* pc_relative */
340          0,                     /* bitpos */
341          complain_overflow_signed, /* complain_on_overflow */
342          bfd_elf_generic_reloc, /* special_function */
343          "R_PPC_REL14",         /* name */
344          FALSE,                 /* partial_inplace */
345          0,                     /* src_mask */
346          0xfffc,                /* dst_mask */
347          TRUE),                 /* pcrel_offset */
348
349   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
350      the branch is expected to be taken.  The lower two bits must be
351      zero.  */
352   HOWTO (R_PPC_REL14_BRTAKEN,   /* 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_BRTAKEN", /* 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 not expected to be taken.  The lower two bits must
368      be zero.  */
369   HOWTO (R_PPC_REL14_BRNTAKEN,  /* 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_BRNTAKEN",/* name */
378          FALSE,                 /* partial_inplace */
379          0,                     /* src_mask */
380          0xfffc,                /* dst_mask */
381          TRUE),                 /* pcrel_offset */
382
383   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
384      symbol.  */
385   HOWTO (R_PPC_GOT16,           /* type */
386          0,                     /* rightshift */
387          1,                     /* size (0 = byte, 1 = short, 2 = long) */
388          16,                    /* bitsize */
389          FALSE,                 /* pc_relative */
390          0,                     /* bitpos */
391          complain_overflow_signed, /* complain_on_overflow */
392          bfd_elf_generic_reloc, /* special_function */
393          "R_PPC_GOT16",         /* name */
394          FALSE,                 /* partial_inplace */
395          0,                     /* src_mask */
396          0xffff,                /* dst_mask */
397          FALSE),                /* pcrel_offset */
398
399   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
400      the symbol.  */
401   HOWTO (R_PPC_GOT16_LO,        /* type */
402          0,                     /* rightshift */
403          1,                     /* size (0 = byte, 1 = short, 2 = long) */
404          16,                    /* bitsize */
405          FALSE,                 /* pc_relative */
406          0,                     /* bitpos */
407          complain_overflow_dont, /* complain_on_overflow */
408          bfd_elf_generic_reloc, /* special_function */
409          "R_PPC_GOT16_LO",      /* name */
410          FALSE,                 /* partial_inplace */
411          0,                     /* src_mask */
412          0xffff,                /* dst_mask */
413          FALSE),                /* pcrel_offset */
414
415   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
416      the symbol.  */
417   HOWTO (R_PPC_GOT16_HI,        /* type */
418          16,                    /* rightshift */
419          1,                     /* size (0 = byte, 1 = short, 2 = long) */
420          16,                    /* bitsize */
421          FALSE,                 /* pc_relative */
422          0,                     /* bitpos */
423          complain_overflow_bitfield, /* complain_on_overflow */
424          bfd_elf_generic_reloc, /* special_function */
425          "R_PPC_GOT16_HI",      /* name */
426          FALSE,                 /* partial_inplace */
427          0,                     /* src_mask */
428          0xffff,                /* dst_mask */
429          FALSE),                 /* pcrel_offset */
430
431   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
432      the symbol.  */
433   HOWTO (R_PPC_GOT16_HA,        /* type */
434          16,                    /* rightshift */
435          1,                     /* size (0 = byte, 1 = short, 2 = long) */
436          16,                    /* bitsize */
437          FALSE,                 /* pc_relative */
438          0,                     /* bitpos */
439          complain_overflow_bitfield, /* complain_on_overflow */
440          ppc_elf_addr16_ha_reloc, /* special_function */
441          "R_PPC_GOT16_HA",      /* name */
442          FALSE,                 /* partial_inplace */
443          0,                     /* src_mask */
444          0xffff,                /* dst_mask */
445          FALSE),                /* pcrel_offset */
446
447   /* Like R_PPC_REL24, but referring to the procedure linkage table
448      entry for the symbol.  */
449   HOWTO (R_PPC_PLTREL24,        /* type */
450          0,                     /* rightshift */
451          2,                     /* size (0 = byte, 1 = short, 2 = long) */
452          26,                    /* bitsize */
453          TRUE,                  /* pc_relative */
454          0,                     /* bitpos */
455          complain_overflow_signed,  /* complain_on_overflow */
456          bfd_elf_generic_reloc, /* special_function */
457          "R_PPC_PLTREL24",      /* name */
458          FALSE,                 /* partial_inplace */
459          0,                     /* src_mask */
460          0x3fffffc,             /* dst_mask */
461          TRUE),                 /* pcrel_offset */
462
463   /* This is used only by the dynamic linker.  The symbol should exist
464      both in the object being run and in some shared library.  The
465      dynamic linker copies the data addressed by the symbol from the
466      shared library into the object, because the object being
467      run has to have the data at some particular address.  */
468   HOWTO (R_PPC_COPY,            /* type */
469          0,                     /* rightshift */
470          2,                     /* size (0 = byte, 1 = short, 2 = long) */
471          32,                    /* bitsize */
472          FALSE,                 /* pc_relative */
473          0,                     /* bitpos */
474          complain_overflow_bitfield, /* complain_on_overflow */
475          bfd_elf_generic_reloc,  /* special_function */
476          "R_PPC_COPY",          /* name */
477          FALSE,                 /* partial_inplace */
478          0,                     /* src_mask */
479          0,                     /* dst_mask */
480          FALSE),                /* pcrel_offset */
481
482   /* Like R_PPC_ADDR32, but used when setting global offset table
483      entries.  */
484   HOWTO (R_PPC_GLOB_DAT,        /* type */
485          0,                     /* rightshift */
486          2,                     /* size (0 = byte, 1 = short, 2 = long) */
487          32,                    /* bitsize */
488          FALSE,                 /* pc_relative */
489          0,                     /* bitpos */
490          complain_overflow_bitfield, /* complain_on_overflow */
491          bfd_elf_generic_reloc,  /* special_function */
492          "R_PPC_GLOB_DAT",      /* name */
493          FALSE,                 /* partial_inplace */
494          0,                     /* src_mask */
495          0xffffffff,            /* dst_mask */
496          FALSE),                /* pcrel_offset */
497
498   /* Marks a procedure linkage table entry for a symbol.  */
499   HOWTO (R_PPC_JMP_SLOT,        /* type */
500          0,                     /* rightshift */
501          2,                     /* size (0 = byte, 1 = short, 2 = long) */
502          32,                    /* bitsize */
503          FALSE,                 /* pc_relative */
504          0,                     /* bitpos */
505          complain_overflow_bitfield, /* complain_on_overflow */
506          bfd_elf_generic_reloc,  /* special_function */
507          "R_PPC_JMP_SLOT",      /* name */
508          FALSE,                 /* partial_inplace */
509          0,                     /* src_mask */
510          0,                     /* dst_mask */
511          FALSE),                /* pcrel_offset */
512
513   /* Used only by the dynamic linker.  When the object is run, this
514      longword is set to the load address of the object, plus the
515      addend.  */
516   HOWTO (R_PPC_RELATIVE,        /* 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_RELATIVE",      /* name */
525          FALSE,                 /* partial_inplace */
526          0,                     /* src_mask */
527          0xffffffff,            /* dst_mask */
528          FALSE),                /* pcrel_offset */
529
530   /* Like R_PPC_REL24, but uses the value of the symbol within the
531      object rather than the final value.  Normally used for
532      _GLOBAL_OFFSET_TABLE_.  */
533   HOWTO (R_PPC_LOCAL24PC,       /* type */
534          0,                     /* rightshift */
535          2,                     /* size (0 = byte, 1 = short, 2 = long) */
536          26,                    /* bitsize */
537          TRUE,                  /* pc_relative */
538          0,                     /* bitpos */
539          complain_overflow_signed, /* complain_on_overflow */
540          bfd_elf_generic_reloc, /* special_function */
541          "R_PPC_LOCAL24PC",     /* name */
542          FALSE,                 /* partial_inplace */
543          0,                     /* src_mask */
544          0x3fffffc,             /* dst_mask */
545          TRUE),                 /* pcrel_offset */
546
547   /* Like R_PPC_ADDR32, but may be unaligned.  */
548   HOWTO (R_PPC_UADDR32,         /* type */
549          0,                     /* rightshift */
550          2,                     /* size (0 = byte, 1 = short, 2 = long) */
551          32,                    /* bitsize */
552          FALSE,                 /* pc_relative */
553          0,                     /* bitpos */
554          complain_overflow_bitfield, /* complain_on_overflow */
555          bfd_elf_generic_reloc, /* special_function */
556          "R_PPC_UADDR32",       /* name */
557          FALSE,                 /* partial_inplace */
558          0,                     /* src_mask */
559          0xffffffff,            /* dst_mask */
560          FALSE),                /* pcrel_offset */
561
562   /* Like R_PPC_ADDR16, but may be unaligned.  */
563   HOWTO (R_PPC_UADDR16,         /* type */
564          0,                     /* rightshift */
565          1,                     /* size (0 = byte, 1 = short, 2 = long) */
566          16,                    /* bitsize */
567          FALSE,                 /* pc_relative */
568          0,                     /* bitpos */
569          complain_overflow_bitfield, /* complain_on_overflow */
570          bfd_elf_generic_reloc, /* special_function */
571          "R_PPC_UADDR16",       /* name */
572          FALSE,                 /* partial_inplace */
573          0,                     /* src_mask */
574          0xffff,                /* dst_mask */
575          FALSE),                /* pcrel_offset */
576
577   /* 32-bit PC relative */
578   HOWTO (R_PPC_REL32,           /* type */
579          0,                     /* rightshift */
580          2,                     /* size (0 = byte, 1 = short, 2 = long) */
581          32,                    /* bitsize */
582          TRUE,                  /* pc_relative */
583          0,                     /* bitpos */
584          complain_overflow_bitfield, /* complain_on_overflow */
585          bfd_elf_generic_reloc, /* special_function */
586          "R_PPC_REL32",         /* name */
587          FALSE,                 /* partial_inplace */
588          0,                     /* src_mask */
589          0xffffffff,            /* dst_mask */
590          TRUE),                 /* pcrel_offset */
591
592   /* 32-bit relocation to the symbol's procedure linkage table.
593      FIXME: not supported.  */
594   HOWTO (R_PPC_PLT32,           /* type */
595          0,                     /* rightshift */
596          2,                     /* size (0 = byte, 1 = short, 2 = long) */
597          32,                    /* bitsize */
598          FALSE,                 /* pc_relative */
599          0,                     /* bitpos */
600          complain_overflow_bitfield, /* complain_on_overflow */
601          bfd_elf_generic_reloc, /* special_function */
602          "R_PPC_PLT32",         /* name */
603          FALSE,                 /* partial_inplace */
604          0,                     /* src_mask */
605          0,                     /* dst_mask */
606          FALSE),                /* pcrel_offset */
607
608   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
609      FIXME: not supported.  */
610   HOWTO (R_PPC_PLTREL32,        /* type */
611          0,                     /* rightshift */
612          2,                     /* size (0 = byte, 1 = short, 2 = long) */
613          32,                    /* bitsize */
614          TRUE,                  /* pc_relative */
615          0,                     /* bitpos */
616          complain_overflow_bitfield, /* complain_on_overflow */
617          bfd_elf_generic_reloc, /* special_function */
618          "R_PPC_PLTREL32",      /* name */
619          FALSE,                 /* partial_inplace */
620          0,                     /* src_mask */
621          0,                     /* dst_mask */
622          TRUE),                 /* pcrel_offset */
623
624   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
625      the symbol.  */
626   HOWTO (R_PPC_PLT16_LO,        /* type */
627          0,                     /* rightshift */
628          1,                     /* size (0 = byte, 1 = short, 2 = long) */
629          16,                    /* bitsize */
630          FALSE,                 /* pc_relative */
631          0,                     /* bitpos */
632          complain_overflow_dont, /* complain_on_overflow */
633          bfd_elf_generic_reloc, /* special_function */
634          "R_PPC_PLT16_LO",      /* name */
635          FALSE,                 /* partial_inplace */
636          0,                     /* src_mask */
637          0xffff,                /* dst_mask */
638          FALSE),                /* pcrel_offset */
639
640   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
641      the symbol.  */
642   HOWTO (R_PPC_PLT16_HI,        /* type */
643          16,                    /* rightshift */
644          1,                     /* size (0 = byte, 1 = short, 2 = long) */
645          16,                    /* bitsize */
646          FALSE,                 /* pc_relative */
647          0,                     /* bitpos */
648          complain_overflow_bitfield, /* complain_on_overflow */
649          bfd_elf_generic_reloc, /* special_function */
650          "R_PPC_PLT16_HI",      /* name */
651          FALSE,                 /* partial_inplace */
652          0,                     /* src_mask */
653          0xffff,                /* dst_mask */
654          FALSE),                 /* pcrel_offset */
655
656   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
657      the symbol.  */
658   HOWTO (R_PPC_PLT16_HA,        /* type */
659          16,                    /* rightshift */
660          1,                     /* size (0 = byte, 1 = short, 2 = long) */
661          16,                    /* bitsize */
662          FALSE,                 /* pc_relative */
663          0,                     /* bitpos */
664          complain_overflow_bitfield, /* complain_on_overflow */
665          ppc_elf_addr16_ha_reloc, /* special_function */
666          "R_PPC_PLT16_HA",      /* name */
667          FALSE,                 /* partial_inplace */
668          0,                     /* src_mask */
669          0xffff,                /* dst_mask */
670          FALSE),                /* pcrel_offset */
671
672   /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
673      small data items.  */
674   HOWTO (R_PPC_SDAREL16,        /* type */
675          0,                     /* rightshift */
676          1,                     /* size (0 = byte, 1 = short, 2 = long) */
677          16,                    /* bitsize */
678          FALSE,                 /* pc_relative */
679          0,                     /* bitpos */
680          complain_overflow_signed, /* complain_on_overflow */
681          bfd_elf_generic_reloc, /* special_function */
682          "R_PPC_SDAREL16",      /* name */
683          FALSE,                 /* partial_inplace */
684          0,                     /* src_mask */
685          0xffff,                /* dst_mask */
686          FALSE),                /* pcrel_offset */
687
688   /* 16-bit section relative relocation.  */
689   HOWTO (R_PPC_SECTOFF,         /* type */
690          0,                     /* rightshift */
691          1,                     /* size (0 = byte, 1 = short, 2 = long) */
692          16,                    /* bitsize */
693          FALSE,                 /* pc_relative */
694          0,                     /* bitpos */
695          complain_overflow_bitfield, /* complain_on_overflow */
696          bfd_elf_generic_reloc, /* special_function */
697          "R_PPC_SECTOFF",       /* name */
698          FALSE,                 /* partial_inplace */
699          0,                     /* src_mask */
700          0xffff,                /* dst_mask */
701          FALSE),                /* pcrel_offset */
702
703   /* 16-bit lower half section relative relocation.  */
704   HOWTO (R_PPC_SECTOFF_LO,        /* type */
705          0,                     /* rightshift */
706          1,                     /* size (0 = byte, 1 = short, 2 = long) */
707          16,                    /* bitsize */
708          FALSE,                 /* pc_relative */
709          0,                     /* bitpos */
710          complain_overflow_dont, /* complain_on_overflow */
711          bfd_elf_generic_reloc, /* special_function */
712          "R_PPC_SECTOFF_LO",    /* name */
713          FALSE,                 /* partial_inplace */
714          0,                     /* src_mask */
715          0xffff,                /* dst_mask */
716          FALSE),                /* pcrel_offset */
717
718   /* 16-bit upper half section relative relocation.  */
719   HOWTO (R_PPC_SECTOFF_HI,      /* type */
720          16,                    /* rightshift */
721          1,                     /* size (0 = byte, 1 = short, 2 = long) */
722          16,                    /* bitsize */
723          FALSE,                 /* pc_relative */
724          0,                     /* bitpos */
725          complain_overflow_bitfield, /* complain_on_overflow */
726          bfd_elf_generic_reloc, /* special_function */
727          "R_PPC_SECTOFF_HI",    /* name */
728          FALSE,                 /* partial_inplace */
729          0,                     /* src_mask */
730          0xffff,                /* dst_mask */
731          FALSE),                 /* pcrel_offset */
732
733   /* 16-bit upper half adjusted section relative relocation.  */
734   HOWTO (R_PPC_SECTOFF_HA,      /* type */
735          16,                    /* rightshift */
736          1,                     /* size (0 = byte, 1 = short, 2 = long) */
737          16,                    /* bitsize */
738          FALSE,                 /* pc_relative */
739          0,                     /* bitpos */
740          complain_overflow_bitfield, /* complain_on_overflow */
741          ppc_elf_addr16_ha_reloc, /* special_function */
742          "R_PPC_SECTOFF_HA",    /* name */
743          FALSE,                 /* partial_inplace */
744          0,                     /* src_mask */
745          0xffff,                /* dst_mask */
746          FALSE),                /* pcrel_offset */
747
748   /* Marker reloc for TLS.  */
749   HOWTO (R_PPC_TLS,
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_dont, /* complain_on_overflow */
756          bfd_elf_generic_reloc, /* special_function */
757          "R_PPC_TLS",           /* name */
758          FALSE,                 /* partial_inplace */
759          0,                     /* src_mask */
760          0,                     /* dst_mask */
761          FALSE),                /* pcrel_offset */
762
763   /* Computes the load module index of the load module that contains the
764      definition of its TLS sym.  */
765   HOWTO (R_PPC_DTPMOD32,
766          0,                     /* rightshift */
767          2,                     /* size (0 = byte, 1 = short, 2 = long) */
768          32,                    /* bitsize */
769          FALSE,                 /* pc_relative */
770          0,                     /* bitpos */
771          complain_overflow_dont, /* complain_on_overflow */
772          ppc_elf_unhandled_reloc, /* special_function */
773          "R_PPC_DTPMOD32",      /* name */
774          FALSE,                 /* partial_inplace */
775          0,                     /* src_mask */
776          0xffffffff,            /* dst_mask */
777          FALSE),                /* pcrel_offset */
778
779   /* Computes a dtv-relative displacement, the difference between the value
780      of sym+add and the base address of the thread-local storage block that
781      contains the definition of sym, minus 0x8000.  */
782   HOWTO (R_PPC_DTPREL32,
783          0,                     /* rightshift */
784          2,                     /* size (0 = byte, 1 = short, 2 = long) */
785          32,                    /* bitsize */
786          FALSE,                 /* pc_relative */
787          0,                     /* bitpos */
788          complain_overflow_dont, /* complain_on_overflow */
789          ppc_elf_unhandled_reloc, /* special_function */
790          "R_PPC_DTPREL32",      /* name */
791          FALSE,                 /* partial_inplace */
792          0,                     /* src_mask */
793          0xffffffff,            /* dst_mask */
794          FALSE),                /* pcrel_offset */
795
796   /* A 16 bit dtprel reloc.  */
797   HOWTO (R_PPC_DTPREL16,
798          0,                     /* rightshift */
799          1,                     /* size (0 = byte, 1 = short, 2 = long) */
800          16,                    /* bitsize */
801          FALSE,                 /* pc_relative */
802          0,                     /* bitpos */
803          complain_overflow_signed, /* complain_on_overflow */
804          ppc_elf_unhandled_reloc, /* special_function */
805          "R_PPC_DTPREL16",      /* name */
806          FALSE,                 /* partial_inplace */
807          0,                     /* src_mask */
808          0xffff,                /* dst_mask */
809          FALSE),                /* pcrel_offset */
810
811   /* Like DTPREL16, but no overflow.  */
812   HOWTO (R_PPC_DTPREL16_LO,
813          0,                     /* rightshift */
814          1,                     /* size (0 = byte, 1 = short, 2 = long) */
815          16,                    /* bitsize */
816          FALSE,                 /* pc_relative */
817          0,                     /* bitpos */
818          complain_overflow_dont, /* complain_on_overflow */
819          ppc_elf_unhandled_reloc, /* special_function */
820          "R_PPC_DTPREL16_LO",   /* name */
821          FALSE,                 /* partial_inplace */
822          0,                     /* src_mask */
823          0xffff,                /* dst_mask */
824          FALSE),                /* pcrel_offset */
825
826   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
827   HOWTO (R_PPC_DTPREL16_HI,
828          16,                    /* rightshift */
829          1,                     /* size (0 = byte, 1 = short, 2 = long) */
830          16,                    /* bitsize */
831          FALSE,                 /* pc_relative */
832          0,                     /* bitpos */
833          complain_overflow_dont, /* complain_on_overflow */
834          ppc_elf_unhandled_reloc, /* special_function */
835          "R_PPC_DTPREL16_HI",   /* name */
836          FALSE,                 /* partial_inplace */
837          0,                     /* src_mask */
838          0xffff,                /* dst_mask */
839          FALSE),                /* pcrel_offset */
840
841   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
842   HOWTO (R_PPC_DTPREL16_HA,
843          16,                    /* rightshift */
844          1,                     /* size (0 = byte, 1 = short, 2 = long) */
845          16,                    /* bitsize */
846          FALSE,                 /* pc_relative */
847          0,                     /* bitpos */
848          complain_overflow_dont, /* complain_on_overflow */
849          ppc_elf_unhandled_reloc, /* special_function */
850          "R_PPC_DTPREL16_HA",   /* name */
851          FALSE,                 /* partial_inplace */
852          0,                     /* src_mask */
853          0xffff,                /* dst_mask */
854          FALSE),                /* pcrel_offset */
855
856   /* Computes a tp-relative displacement, the difference between the value of
857      sym+add and the value of the thread pointer (r13).  */
858   HOWTO (R_PPC_TPREL32,
859          0,                     /* rightshift */
860          2,                     /* size (0 = byte, 1 = short, 2 = long) */
861          32,                    /* bitsize */
862          FALSE,                 /* pc_relative */
863          0,                     /* bitpos */
864          complain_overflow_dont, /* complain_on_overflow */
865          ppc_elf_unhandled_reloc, /* special_function */
866          "R_PPC_TPREL32",       /* name */
867          FALSE,                 /* partial_inplace */
868          0,                     /* src_mask */
869          0xffffffff,            /* dst_mask */
870          FALSE),                /* pcrel_offset */
871
872   /* A 16 bit tprel reloc.  */
873   HOWTO (R_PPC_TPREL16,
874          0,                     /* rightshift */
875          1,                     /* size (0 = byte, 1 = short, 2 = long) */
876          16,                    /* bitsize */
877          FALSE,                 /* pc_relative */
878          0,                     /* bitpos */
879          complain_overflow_signed, /* complain_on_overflow */
880          ppc_elf_unhandled_reloc, /* special_function */
881          "R_PPC_TPREL16",       /* name */
882          FALSE,                 /* partial_inplace */
883          0,                     /* src_mask */
884          0xffff,                /* dst_mask */
885          FALSE),                /* pcrel_offset */
886
887   /* Like TPREL16, but no overflow.  */
888   HOWTO (R_PPC_TPREL16_LO,
889          0,                     /* rightshift */
890          1,                     /* size (0 = byte, 1 = short, 2 = long) */
891          16,                    /* bitsize */
892          FALSE,                 /* pc_relative */
893          0,                     /* bitpos */
894          complain_overflow_dont, /* complain_on_overflow */
895          ppc_elf_unhandled_reloc, /* special_function */
896          "R_PPC_TPREL16_LO",    /* name */
897          FALSE,                 /* partial_inplace */
898          0,                     /* src_mask */
899          0xffff,                /* dst_mask */
900          FALSE),                /* pcrel_offset */
901
902   /* Like TPREL16_LO, but next higher group of 16 bits.  */
903   HOWTO (R_PPC_TPREL16_HI,
904          16,                    /* rightshift */
905          1,                     /* size (0 = byte, 1 = short, 2 = long) */
906          16,                    /* bitsize */
907          FALSE,                 /* pc_relative */
908          0,                     /* bitpos */
909          complain_overflow_dont, /* complain_on_overflow */
910          ppc_elf_unhandled_reloc, /* special_function */
911          "R_PPC_TPREL16_HI",    /* name */
912          FALSE,                 /* partial_inplace */
913          0,                     /* src_mask */
914          0xffff,                /* dst_mask */
915          FALSE),                /* pcrel_offset */
916
917   /* Like TPREL16_HI, but adjust for low 16 bits.  */
918   HOWTO (R_PPC_TPREL16_HA,
919          16,                    /* rightshift */
920          1,                     /* size (0 = byte, 1 = short, 2 = long) */
921          16,                    /* bitsize */
922          FALSE,                 /* pc_relative */
923          0,                     /* bitpos */
924          complain_overflow_dont, /* complain_on_overflow */
925          ppc_elf_unhandled_reloc, /* special_function */
926          "R_PPC_TPREL16_HA",    /* name */
927          FALSE,                 /* partial_inplace */
928          0,                     /* src_mask */
929          0xffff,                /* dst_mask */
930          FALSE),                /* pcrel_offset */
931
932   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
933      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
934      to the first entry.  */
935   HOWTO (R_PPC_GOT_TLSGD16,
936          0,                     /* rightshift */
937          1,                     /* size (0 = byte, 1 = short, 2 = long) */
938          16,                    /* bitsize */
939          FALSE,                 /* pc_relative */
940          0,                     /* bitpos */
941          complain_overflow_signed, /* complain_on_overflow */
942          ppc_elf_unhandled_reloc, /* special_function */
943          "R_PPC_GOT_TLSGD16",   /* name */
944          FALSE,                 /* partial_inplace */
945          0,                     /* src_mask */
946          0xffff,                /* dst_mask */
947          FALSE),                /* pcrel_offset */
948
949   /* Like GOT_TLSGD16, but no overflow.  */
950   HOWTO (R_PPC_GOT_TLSGD16_LO,
951          0,                     /* rightshift */
952          1,                     /* size (0 = byte, 1 = short, 2 = long) */
953          16,                    /* bitsize */
954          FALSE,                 /* pc_relative */
955          0,                     /* bitpos */
956          complain_overflow_dont, /* complain_on_overflow */
957          ppc_elf_unhandled_reloc, /* special_function */
958          "R_PPC_GOT_TLSGD16_LO", /* name */
959          FALSE,                 /* partial_inplace */
960          0,                     /* src_mask */
961          0xffff,                /* dst_mask */
962          FALSE),                /* pcrel_offset */
963
964   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
965   HOWTO (R_PPC_GOT_TLSGD16_HI,
966          16,                    /* rightshift */
967          1,                     /* size (0 = byte, 1 = short, 2 = long) */
968          16,                    /* bitsize */
969          FALSE,                 /* pc_relative */
970          0,                     /* bitpos */
971          complain_overflow_dont, /* complain_on_overflow */
972          ppc_elf_unhandled_reloc, /* special_function */
973          "R_PPC_GOT_TLSGD16_HI", /* name */
974          FALSE,                 /* partial_inplace */
975          0,                     /* src_mask */
976          0xffff,                /* dst_mask */
977          FALSE),                /* pcrel_offset */
978
979   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
980   HOWTO (R_PPC_GOT_TLSGD16_HA,
981          16,                    /* rightshift */
982          1,                     /* size (0 = byte, 1 = short, 2 = long) */
983          16,                    /* bitsize */
984          FALSE,                 /* pc_relative */
985          0,                     /* bitpos */
986          complain_overflow_dont, /* complain_on_overflow */
987          ppc_elf_unhandled_reloc, /* special_function */
988          "R_PPC_GOT_TLSGD16_HA", /* name */
989          FALSE,                 /* partial_inplace */
990          0,                     /* src_mask */
991          0xffff,                /* dst_mask */
992          FALSE),                /* pcrel_offset */
993
994   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
995      with values (sym+add)@dtpmod and zero, and computes the offset to the
996      first entry.  */
997   HOWTO (R_PPC_GOT_TLSLD16,
998          0,                     /* rightshift */
999          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1000          16,                    /* bitsize */
1001          FALSE,                 /* pc_relative */
1002          0,                     /* bitpos */
1003          complain_overflow_signed, /* complain_on_overflow */
1004          ppc_elf_unhandled_reloc, /* special_function */
1005          "R_PPC_GOT_TLSLD16",   /* name */
1006          FALSE,                 /* partial_inplace */
1007          0,                     /* src_mask */
1008          0xffff,                /* dst_mask */
1009          FALSE),                /* pcrel_offset */
1010
1011   /* Like GOT_TLSLD16, but no overflow.  */
1012   HOWTO (R_PPC_GOT_TLSLD16_LO,
1013          0,                     /* rightshift */
1014          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1015          16,                    /* bitsize */
1016          FALSE,                 /* pc_relative */
1017          0,                     /* bitpos */
1018          complain_overflow_dont, /* complain_on_overflow */
1019          ppc_elf_unhandled_reloc, /* special_function */
1020          "R_PPC_GOT_TLSLD16_LO", /* name */
1021          FALSE,                 /* partial_inplace */
1022          0,                     /* src_mask */
1023          0xffff,                /* dst_mask */
1024          FALSE),                /* pcrel_offset */
1025
1026   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1027   HOWTO (R_PPC_GOT_TLSLD16_HI,
1028          16,                    /* rightshift */
1029          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1030          16,                    /* bitsize */
1031          FALSE,                 /* pc_relative */
1032          0,                     /* bitpos */
1033          complain_overflow_dont, /* complain_on_overflow */
1034          ppc_elf_unhandled_reloc, /* special_function */
1035          "R_PPC_GOT_TLSLD16_HI", /* name */
1036          FALSE,                 /* partial_inplace */
1037          0,                     /* src_mask */
1038          0xffff,                /* dst_mask */
1039          FALSE),                /* pcrel_offset */
1040
1041   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1042   HOWTO (R_PPC_GOT_TLSLD16_HA,
1043          16,                    /* rightshift */
1044          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1045          16,                    /* bitsize */
1046          FALSE,                 /* pc_relative */
1047          0,                     /* bitpos */
1048          complain_overflow_dont, /* complain_on_overflow */
1049          ppc_elf_unhandled_reloc, /* special_function */
1050          "R_PPC_GOT_TLSLD16_HA", /* name */
1051          FALSE,                 /* partial_inplace */
1052          0,                     /* src_mask */
1053          0xffff,                /* dst_mask */
1054          FALSE),                /* pcrel_offset */
1055
1056   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1057      the offset to the entry.  */
1058   HOWTO (R_PPC_GOT_DTPREL16,
1059          0,                     /* rightshift */
1060          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1061          16,                    /* bitsize */
1062          FALSE,                 /* pc_relative */
1063          0,                     /* bitpos */
1064          complain_overflow_signed, /* complain_on_overflow */
1065          ppc_elf_unhandled_reloc, /* special_function */
1066          "R_PPC_GOT_DTPREL16",  /* name */
1067          FALSE,                 /* partial_inplace */
1068          0,                     /* src_mask */
1069          0xffff,                /* dst_mask */
1070          FALSE),                /* pcrel_offset */
1071
1072   /* Like GOT_DTPREL16, but no overflow.  */
1073   HOWTO (R_PPC_GOT_DTPREL16_LO,
1074          0,                     /* rightshift */
1075          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1076          16,                    /* bitsize */
1077          FALSE,                 /* pc_relative */
1078          0,                     /* bitpos */
1079          complain_overflow_dont, /* complain_on_overflow */
1080          ppc_elf_unhandled_reloc, /* special_function */
1081          "R_PPC_GOT_DTPREL16_LO", /* name */
1082          FALSE,                 /* partial_inplace */
1083          0,                     /* src_mask */
1084          0xffff,                /* dst_mask */
1085          FALSE),                /* pcrel_offset */
1086
1087   /* Like GOT_DTPREL16_LO, but next higher group of 16 bits.  */
1088   HOWTO (R_PPC_GOT_DTPREL16_HI,
1089          16,                    /* rightshift */
1090          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1091          16,                    /* bitsize */
1092          FALSE,                 /* pc_relative */
1093          0,                     /* bitpos */
1094          complain_overflow_dont, /* complain_on_overflow */
1095          ppc_elf_unhandled_reloc, /* special_function */
1096          "R_PPC_GOT_DTPREL16_HI", /* name */
1097          FALSE,                 /* partial_inplace */
1098          0,                     /* src_mask */
1099          0xffff,                /* dst_mask */
1100          FALSE),                /* pcrel_offset */
1101
1102   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1103   HOWTO (R_PPC_GOT_DTPREL16_HA,
1104          16,                    /* rightshift */
1105          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1106          16,                    /* bitsize */
1107          FALSE,                 /* pc_relative */
1108          0,                     /* bitpos */
1109          complain_overflow_dont, /* complain_on_overflow */
1110          ppc_elf_unhandled_reloc, /* special_function */
1111          "R_PPC_GOT_DTPREL16_HA", /* name */
1112          FALSE,                 /* partial_inplace */
1113          0,                     /* src_mask */
1114          0xffff,                /* dst_mask */
1115          FALSE),                /* pcrel_offset */
1116
1117   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1118      offset to the entry.  */
1119   HOWTO (R_PPC_GOT_TPREL16,
1120          0,                     /* rightshift */
1121          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1122          16,                    /* bitsize */
1123          FALSE,                 /* pc_relative */
1124          0,                     /* bitpos */
1125          complain_overflow_signed, /* complain_on_overflow */
1126          ppc_elf_unhandled_reloc, /* special_function */
1127          "R_PPC_GOT_TPREL16",   /* name */
1128          FALSE,                 /* partial_inplace */
1129          0,                     /* src_mask */
1130          0xffff,                /* dst_mask */
1131          FALSE),                /* pcrel_offset */
1132
1133   /* Like GOT_TPREL16, but no overflow.  */
1134   HOWTO (R_PPC_GOT_TPREL16_LO,
1135          0,                     /* rightshift */
1136          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1137          16,                    /* bitsize */
1138          FALSE,                 /* pc_relative */
1139          0,                     /* bitpos */
1140          complain_overflow_dont, /* complain_on_overflow */
1141          ppc_elf_unhandled_reloc, /* special_function */
1142          "R_PPC_GOT_TPREL16_LO", /* name */
1143          FALSE,                 /* partial_inplace */
1144          0,                     /* src_mask */
1145          0xffff,                /* dst_mask */
1146          FALSE),                /* pcrel_offset */
1147
1148   /* Like GOT_TPREL16_LO, but next higher group of 16 bits.  */
1149   HOWTO (R_PPC_GOT_TPREL16_HI,
1150          16,                    /* rightshift */
1151          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1152          16,                    /* bitsize */
1153          FALSE,                 /* pc_relative */
1154          0,                     /* bitpos */
1155          complain_overflow_dont, /* complain_on_overflow */
1156          ppc_elf_unhandled_reloc, /* special_function */
1157          "R_PPC_GOT_TPREL16_HI", /* name */
1158          FALSE,                 /* partial_inplace */
1159          0,                     /* src_mask */
1160          0xffff,                /* dst_mask */
1161          FALSE),                /* pcrel_offset */
1162
1163   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1164   HOWTO (R_PPC_GOT_TPREL16_HA,
1165          16,                    /* rightshift */
1166          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1167          16,                    /* bitsize */
1168          FALSE,                 /* pc_relative */
1169          0,                     /* bitpos */
1170          complain_overflow_dont, /* complain_on_overflow */
1171          ppc_elf_unhandled_reloc, /* special_function */
1172          "R_PPC_GOT_TPREL16_HA", /* name */
1173          FALSE,                 /* partial_inplace */
1174          0,                     /* src_mask */
1175          0xffff,                /* dst_mask */
1176          FALSE),                /* pcrel_offset */
1177
1178   /* The remaining relocs are from the Embedded ELF ABI, and are not
1179      in the SVR4 ELF ABI.  */
1180
1181   /* 32 bit value resulting from the addend minus the symbol.  */
1182   HOWTO (R_PPC_EMB_NADDR32,     /* type */
1183          0,                     /* rightshift */
1184          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1185          32,                    /* bitsize */
1186          FALSE,                 /* pc_relative */
1187          0,                     /* bitpos */
1188          complain_overflow_bitfield, /* complain_on_overflow */
1189          bfd_elf_generic_reloc, /* special_function */
1190          "R_PPC_EMB_NADDR32",   /* name */
1191          FALSE,                 /* partial_inplace */
1192          0,                     /* src_mask */
1193          0xffffffff,            /* dst_mask */
1194          FALSE),                /* pcrel_offset */
1195
1196   /* 16 bit value resulting from the addend minus the symbol.  */
1197   HOWTO (R_PPC_EMB_NADDR16,     /* type */
1198          0,                     /* rightshift */
1199          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1200          16,                    /* bitsize */
1201          FALSE,                 /* pc_relative */
1202          0,                     /* bitpos */
1203          complain_overflow_bitfield, /* complain_on_overflow */
1204          bfd_elf_generic_reloc, /* special_function */
1205          "R_PPC_EMB_NADDR16",   /* name */
1206          FALSE,                 /* partial_inplace */
1207          0,                     /* src_mask */
1208          0xffff,                /* dst_mask */
1209          FALSE),                /* pcrel_offset */
1210
1211   /* 16 bit value resulting from the addend minus the symbol.  */
1212   HOWTO (R_PPC_EMB_NADDR16_LO,  /* type */
1213          0,                     /* rightshift */
1214          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1215          16,                    /* bitsize */
1216          FALSE,                 /* pc_relative */
1217          0,                     /* bitpos */
1218          complain_overflow_dont,/* complain_on_overflow */
1219          bfd_elf_generic_reloc, /* special_function */
1220          "R_PPC_EMB_ADDR16_LO", /* name */
1221          FALSE,                 /* partial_inplace */
1222          0,                     /* src_mask */
1223          0xffff,                /* dst_mask */
1224          FALSE),                /* pcrel_offset */
1225
1226   /* The high order 16 bits of the addend minus the symbol.  */
1227   HOWTO (R_PPC_EMB_NADDR16_HI,  /* type */
1228          16,                    /* rightshift */
1229          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1230          16,                    /* bitsize */
1231          FALSE,                 /* pc_relative */
1232          0,                     /* bitpos */
1233          complain_overflow_dont, /* complain_on_overflow */
1234          bfd_elf_generic_reloc, /* special_function */
1235          "R_PPC_EMB_NADDR16_HI", /* name */
1236          FALSE,                 /* partial_inplace */
1237          0,                     /* src_mask */
1238          0xffff,                /* dst_mask */
1239          FALSE),                /* pcrel_offset */
1240
1241   /* The high order 16 bits of the result of the addend minus the address,
1242      plus 1 if the contents of the low 16 bits, treated as a signed number,
1243      is negative.  */
1244   HOWTO (R_PPC_EMB_NADDR16_HA,  /* type */
1245          16,                    /* rightshift */
1246          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1247          16,                    /* bitsize */
1248          FALSE,                 /* pc_relative */
1249          0,                     /* bitpos */
1250          complain_overflow_dont, /* complain_on_overflow */
1251          ppc_elf_addr16_ha_reloc, /* special_function */
1252          "R_PPC_EMB_NADDR16_HA", /* name */
1253          FALSE,                 /* partial_inplace */
1254          0,                     /* src_mask */
1255          0xffff,                /* dst_mask */
1256          FALSE),                /* pcrel_offset */
1257
1258   /* 16 bit value resulting from allocating a 4 byte word to hold an
1259      address in the .sdata section, and returning the offset from
1260      _SDA_BASE_ for that relocation.  */
1261   HOWTO (R_PPC_EMB_SDAI16,      /* type */
1262          0,                     /* rightshift */
1263          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1264          16,                    /* bitsize */
1265          FALSE,                 /* pc_relative */
1266          0,                     /* bitpos */
1267          complain_overflow_bitfield, /* complain_on_overflow */
1268          bfd_elf_generic_reloc, /* special_function */
1269          "R_PPC_EMB_SDAI16",    /* name */
1270          FALSE,                 /* partial_inplace */
1271          0,                     /* src_mask */
1272          0xffff,                /* dst_mask */
1273          FALSE),                /* pcrel_offset */
1274
1275   /* 16 bit value resulting from allocating a 4 byte word to hold an
1276      address in the .sdata2 section, and returning the offset from
1277      _SDA2_BASE_ for that relocation.  */
1278   HOWTO (R_PPC_EMB_SDA2I16,     /* type */
1279          0,                     /* rightshift */
1280          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1281          16,                    /* bitsize */
1282          FALSE,                 /* pc_relative */
1283          0,                     /* bitpos */
1284          complain_overflow_bitfield, /* complain_on_overflow */
1285          bfd_elf_generic_reloc, /* special_function */
1286          "R_PPC_EMB_SDA2I16",   /* name */
1287          FALSE,                 /* partial_inplace */
1288          0,                     /* src_mask */
1289          0xffff,                /* dst_mask */
1290          FALSE),                /* pcrel_offset */
1291
1292   /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1293      small data items.   */
1294   HOWTO (R_PPC_EMB_SDA2REL,     /* type */
1295          0,                     /* rightshift */
1296          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1297          16,                    /* bitsize */
1298          FALSE,                 /* pc_relative */
1299          0,                     /* bitpos */
1300          complain_overflow_signed, /* complain_on_overflow */
1301          bfd_elf_generic_reloc, /* special_function */
1302          "R_PPC_EMB_SDA2REL",   /* name */
1303          FALSE,                 /* partial_inplace */
1304          0,                     /* src_mask */
1305          0xffff,                /* dst_mask */
1306          FALSE),                /* pcrel_offset */
1307
1308   /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1309      signed offset from the appropriate base, and filling in the register
1310      field with the appropriate register (0, 2, or 13).  */
1311   HOWTO (R_PPC_EMB_SDA21,       /* type */
1312          0,                     /* rightshift */
1313          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1314          16,                    /* bitsize */
1315          FALSE,                 /* pc_relative */
1316          0,                     /* bitpos */
1317          complain_overflow_signed, /* complain_on_overflow */
1318          bfd_elf_generic_reloc, /* special_function */
1319          "R_PPC_EMB_SDA21",     /* name */
1320          FALSE,                 /* partial_inplace */
1321          0,                     /* src_mask */
1322          0xffff,                /* dst_mask */
1323          FALSE),                /* pcrel_offset */
1324
1325   /* Relocation not handled: R_PPC_EMB_MRKREF */
1326   /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1327   /* Relocation not handled: R_PPC_EMB_RELST_LO */
1328   /* Relocation not handled: R_PPC_EMB_RELST_HI */
1329   /* Relocation not handled: R_PPC_EMB_RELST_HA */
1330   /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1331
1332   /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1333      in the 16 bit signed offset from the appropriate base, and filling in the
1334      register field with the appropriate register (0, 2, or 13).  */
1335   HOWTO (R_PPC_EMB_RELSDA,      /* type */
1336          0,                     /* rightshift */
1337          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1338          16,                    /* bitsize */
1339          TRUE,                  /* pc_relative */
1340          0,                     /* bitpos */
1341          complain_overflow_signed, /* complain_on_overflow */
1342          bfd_elf_generic_reloc, /* special_function */
1343          "R_PPC_EMB_RELSDA",    /* name */
1344          FALSE,                 /* partial_inplace */
1345          0,                     /* src_mask */
1346          0xffff,                /* dst_mask */
1347          FALSE),                /* pcrel_offset */
1348
1349   /* A 16 bit relative relocation.  */
1350   HOWTO (R_PPC_REL16,           /* type */
1351          0,                     /* rightshift */
1352          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1353          16,                    /* bitsize */
1354          TRUE,                  /* pc_relative */
1355          0,                     /* bitpos */
1356          complain_overflow_bitfield, /* complain_on_overflow */
1357          bfd_elf_generic_reloc, /* special_function */
1358          "R_PPC_REL16",         /* name */
1359          FALSE,                 /* partial_inplace */
1360          0,                     /* src_mask */
1361          0xffff,                /* dst_mask */
1362          TRUE),                 /* pcrel_offset */
1363
1364   /* A 16 bit relative relocation without overflow.  */
1365   HOWTO (R_PPC_REL16_LO,        /* type */
1366          0,                     /* rightshift */
1367          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1368          16,                    /* bitsize */
1369          TRUE,                  /* pc_relative */
1370          0,                     /* bitpos */
1371          complain_overflow_dont,/* complain_on_overflow */
1372          bfd_elf_generic_reloc, /* special_function */
1373          "R_PPC_REL16_LO",      /* name */
1374          FALSE,                 /* partial_inplace */
1375          0,                     /* src_mask */
1376          0xffff,                /* dst_mask */
1377          TRUE),                 /* pcrel_offset */
1378
1379   /* The high order 16 bits of a relative address.  */
1380   HOWTO (R_PPC_REL16_HI,        /* type */
1381          16,                    /* rightshift */
1382          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1383          16,                    /* bitsize */
1384          TRUE,                  /* pc_relative */
1385          0,                     /* bitpos */
1386          complain_overflow_dont, /* complain_on_overflow */
1387          bfd_elf_generic_reloc, /* special_function */
1388          "R_PPC_REL16_HI",      /* name */
1389          FALSE,                 /* partial_inplace */
1390          0,                     /* src_mask */
1391          0xffff,                /* dst_mask */
1392          TRUE),                 /* pcrel_offset */
1393
1394   /* The high order 16 bits of a relative address, plus 1 if the contents of
1395      the low 16 bits, treated as a signed number, is negative.  */
1396   HOWTO (R_PPC_REL16_HA,        /* type */
1397          16,                    /* rightshift */
1398          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1399          16,                    /* bitsize */
1400          TRUE,                  /* pc_relative */
1401          0,                     /* bitpos */
1402          complain_overflow_dont, /* complain_on_overflow */
1403          ppc_elf_addr16_ha_reloc, /* special_function */
1404          "R_PPC_REL16_HA",      /* name */
1405          FALSE,                 /* partial_inplace */
1406          0,                     /* src_mask */
1407          0xffff,                /* dst_mask */
1408          TRUE),                 /* pcrel_offset */
1409
1410   /* GNU extension to record C++ vtable hierarchy.  */
1411   HOWTO (R_PPC_GNU_VTINHERIT,   /* type */
1412          0,                     /* rightshift */
1413          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1414          0,                     /* bitsize */
1415          FALSE,                 /* pc_relative */
1416          0,                     /* bitpos */
1417          complain_overflow_dont, /* complain_on_overflow */
1418          NULL,                  /* special_function */
1419          "R_PPC_GNU_VTINHERIT", /* name */
1420          FALSE,                 /* partial_inplace */
1421          0,                     /* src_mask */
1422          0,                     /* dst_mask */
1423          FALSE),                /* pcrel_offset */
1424
1425   /* GNU extension to record C++ vtable member usage.  */
1426   HOWTO (R_PPC_GNU_VTENTRY,     /* type */
1427          0,                     /* rightshift */
1428          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1429          0,                     /* bitsize */
1430          FALSE,                 /* pc_relative */
1431          0,                     /* bitpos */
1432          complain_overflow_dont, /* complain_on_overflow */
1433          NULL,                  /* special_function */
1434          "R_PPC_GNU_VTENTRY",   /* name */
1435          FALSE,                 /* partial_inplace */
1436          0,                     /* src_mask */
1437          0,                     /* dst_mask */
1438          FALSE),                /* pcrel_offset */
1439
1440   /* Phony reloc to handle AIX style TOC entries.  */
1441   HOWTO (R_PPC_TOC16,           /* type */
1442          0,                     /* rightshift */
1443          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1444          16,                    /* bitsize */
1445          FALSE,                 /* pc_relative */
1446          0,                     /* bitpos */
1447          complain_overflow_signed, /* complain_on_overflow */
1448          bfd_elf_generic_reloc, /* special_function */
1449          "R_PPC_TOC16",         /* name */
1450          FALSE,                 /* partial_inplace */
1451          0,                     /* src_mask */
1452          0xffff,                /* dst_mask */
1453          FALSE),                /* pcrel_offset */
1454 };
1455 \f
1456 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
1457
1458 static void
1459 ppc_elf_howto_init (void)
1460 {
1461   unsigned int i, type;
1462
1463   for (i = 0;
1464        i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1465        i++)
1466     {
1467       type = ppc_elf_howto_raw[i].type;
1468       if (type >= (sizeof (ppc_elf_howto_table)
1469                    / sizeof (ppc_elf_howto_table[0])))
1470         abort ();
1471       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1472     }
1473 }
1474
1475 static reloc_howto_type *
1476 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1477                            bfd_reloc_code_real_type code)
1478 {
1479   enum elf_ppc_reloc_type r;
1480
1481   /* Initialize howto table if not already done.  */
1482   if (!ppc_elf_howto_table[R_PPC_ADDR32])
1483     ppc_elf_howto_init ();
1484
1485   switch (code)
1486     {
1487     default:
1488       return NULL;
1489
1490     case BFD_RELOC_NONE:                r = R_PPC_NONE;                 break;
1491     case BFD_RELOC_32:                  r = R_PPC_ADDR32;               break;
1492     case BFD_RELOC_PPC_BA26:            r = R_PPC_ADDR24;               break;
1493     case BFD_RELOC_16:                  r = R_PPC_ADDR16;               break;
1494     case BFD_RELOC_LO16:                r = R_PPC_ADDR16_LO;            break;
1495     case BFD_RELOC_HI16:                r = R_PPC_ADDR16_HI;            break;
1496     case BFD_RELOC_HI16_S:              r = R_PPC_ADDR16_HA;            break;
1497     case BFD_RELOC_PPC_BA16:            r = R_PPC_ADDR14;               break;
1498     case BFD_RELOC_PPC_BA16_BRTAKEN:    r = R_PPC_ADDR14_BRTAKEN;       break;
1499     case BFD_RELOC_PPC_BA16_BRNTAKEN:   r = R_PPC_ADDR14_BRNTAKEN;      break;
1500     case BFD_RELOC_PPC_B26:             r = R_PPC_REL24;                break;
1501     case BFD_RELOC_PPC_B16:             r = R_PPC_REL14;                break;
1502     case BFD_RELOC_PPC_B16_BRTAKEN:     r = R_PPC_REL14_BRTAKEN;        break;
1503     case BFD_RELOC_PPC_B16_BRNTAKEN:    r = R_PPC_REL14_BRNTAKEN;       break;
1504     case BFD_RELOC_16_GOTOFF:           r = R_PPC_GOT16;                break;
1505     case BFD_RELOC_LO16_GOTOFF:         r = R_PPC_GOT16_LO;             break;
1506     case BFD_RELOC_HI16_GOTOFF:         r = R_PPC_GOT16_HI;             break;
1507     case BFD_RELOC_HI16_S_GOTOFF:       r = R_PPC_GOT16_HA;             break;
1508     case BFD_RELOC_24_PLT_PCREL:        r = R_PPC_PLTREL24;             break;
1509     case BFD_RELOC_PPC_COPY:            r = R_PPC_COPY;                 break;
1510     case BFD_RELOC_PPC_GLOB_DAT:        r = R_PPC_GLOB_DAT;             break;
1511     case BFD_RELOC_PPC_LOCAL24PC:       r = R_PPC_LOCAL24PC;            break;
1512     case BFD_RELOC_32_PCREL:            r = R_PPC_REL32;                break;
1513     case BFD_RELOC_32_PLTOFF:           r = R_PPC_PLT32;                break;
1514     case BFD_RELOC_32_PLT_PCREL:        r = R_PPC_PLTREL32;             break;
1515     case BFD_RELOC_LO16_PLTOFF:         r = R_PPC_PLT16_LO;             break;
1516     case BFD_RELOC_HI16_PLTOFF:         r = R_PPC_PLT16_HI;             break;
1517     case BFD_RELOC_HI16_S_PLTOFF:       r = R_PPC_PLT16_HA;             break;
1518     case BFD_RELOC_GPREL16:             r = R_PPC_SDAREL16;             break;
1519     case BFD_RELOC_16_BASEREL:          r = R_PPC_SECTOFF;              break;
1520     case BFD_RELOC_LO16_BASEREL:        r = R_PPC_SECTOFF_LO;           break;
1521     case BFD_RELOC_HI16_BASEREL:        r = R_PPC_SECTOFF_HI;           break;
1522     case BFD_RELOC_HI16_S_BASEREL:      r = R_PPC_SECTOFF_HA;           break;
1523     case BFD_RELOC_CTOR:                r = R_PPC_ADDR32;               break;
1524     case BFD_RELOC_PPC_TOC16:           r = R_PPC_TOC16;                break;
1525     case BFD_RELOC_PPC_TLS:             r = R_PPC_TLS;                  break;
1526     case BFD_RELOC_PPC_DTPMOD:          r = R_PPC_DTPMOD32;             break;
1527     case BFD_RELOC_PPC_TPREL16:         r = R_PPC_TPREL16;              break;
1528     case BFD_RELOC_PPC_TPREL16_LO:      r = R_PPC_TPREL16_LO;           break;
1529     case BFD_RELOC_PPC_TPREL16_HI:      r = R_PPC_TPREL16_HI;           break;
1530     case BFD_RELOC_PPC_TPREL16_HA:      r = R_PPC_TPREL16_HA;           break;
1531     case BFD_RELOC_PPC_TPREL:           r = R_PPC_TPREL32;              break;
1532     case BFD_RELOC_PPC_DTPREL16:        r = R_PPC_DTPREL16;             break;
1533     case BFD_RELOC_PPC_DTPREL16_LO:     r = R_PPC_DTPREL16_LO;          break;
1534     case BFD_RELOC_PPC_DTPREL16_HI:     r = R_PPC_DTPREL16_HI;          break;
1535     case BFD_RELOC_PPC_DTPREL16_HA:     r = R_PPC_DTPREL16_HA;          break;
1536     case BFD_RELOC_PPC_DTPREL:          r = R_PPC_DTPREL32;             break;
1537     case BFD_RELOC_PPC_GOT_TLSGD16:     r = R_PPC_GOT_TLSGD16;          break;
1538     case BFD_RELOC_PPC_GOT_TLSGD16_LO:  r = R_PPC_GOT_TLSGD16_LO;       break;
1539     case BFD_RELOC_PPC_GOT_TLSGD16_HI:  r = R_PPC_GOT_TLSGD16_HI;       break;
1540     case BFD_RELOC_PPC_GOT_TLSGD16_HA:  r = R_PPC_GOT_TLSGD16_HA;       break;
1541     case BFD_RELOC_PPC_GOT_TLSLD16:     r = R_PPC_GOT_TLSLD16;          break;
1542     case BFD_RELOC_PPC_GOT_TLSLD16_LO:  r = R_PPC_GOT_TLSLD16_LO;       break;
1543     case BFD_RELOC_PPC_GOT_TLSLD16_HI:  r = R_PPC_GOT_TLSLD16_HI;       break;
1544     case BFD_RELOC_PPC_GOT_TLSLD16_HA:  r = R_PPC_GOT_TLSLD16_HA;       break;
1545     case BFD_RELOC_PPC_GOT_TPREL16:     r = R_PPC_GOT_TPREL16;          break;
1546     case BFD_RELOC_PPC_GOT_TPREL16_LO:  r = R_PPC_GOT_TPREL16_LO;       break;
1547     case BFD_RELOC_PPC_GOT_TPREL16_HI:  r = R_PPC_GOT_TPREL16_HI;       break;
1548     case BFD_RELOC_PPC_GOT_TPREL16_HA:  r = R_PPC_GOT_TPREL16_HA;       break;
1549     case BFD_RELOC_PPC_GOT_DTPREL16:    r = R_PPC_GOT_DTPREL16;         break;
1550     case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO;      break;
1551     case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI;      break;
1552     case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA;      break;
1553     case BFD_RELOC_PPC_EMB_NADDR32:     r = R_PPC_EMB_NADDR32;          break;
1554     case BFD_RELOC_PPC_EMB_NADDR16:     r = R_PPC_EMB_NADDR16;          break;
1555     case BFD_RELOC_PPC_EMB_NADDR16_LO:  r = R_PPC_EMB_NADDR16_LO;       break;
1556     case BFD_RELOC_PPC_EMB_NADDR16_HI:  r = R_PPC_EMB_NADDR16_HI;       break;
1557     case BFD_RELOC_PPC_EMB_NADDR16_HA:  r = R_PPC_EMB_NADDR16_HA;       break;
1558     case BFD_RELOC_PPC_EMB_SDAI16:      r = R_PPC_EMB_SDAI16;           break;
1559     case BFD_RELOC_PPC_EMB_SDA2I16:     r = R_PPC_EMB_SDA2I16;          break;
1560     case BFD_RELOC_PPC_EMB_SDA2REL:     r = R_PPC_EMB_SDA2REL;          break;
1561     case BFD_RELOC_PPC_EMB_SDA21:       r = R_PPC_EMB_SDA21;            break;
1562     case BFD_RELOC_PPC_EMB_MRKREF:      r = R_PPC_EMB_MRKREF;           break;
1563     case BFD_RELOC_PPC_EMB_RELSEC16:    r = R_PPC_EMB_RELSEC16;         break;
1564     case BFD_RELOC_PPC_EMB_RELST_LO:    r = R_PPC_EMB_RELST_LO;         break;
1565     case BFD_RELOC_PPC_EMB_RELST_HI:    r = R_PPC_EMB_RELST_HI;         break;
1566     case BFD_RELOC_PPC_EMB_RELST_HA:    r = R_PPC_EMB_RELST_HA;         break;
1567     case BFD_RELOC_PPC_EMB_BIT_FLD:     r = R_PPC_EMB_BIT_FLD;          break;
1568     case BFD_RELOC_PPC_EMB_RELSDA:      r = R_PPC_EMB_RELSDA;           break;
1569     case BFD_RELOC_16_PCREL:            r = R_PPC_REL16;                break;
1570     case BFD_RELOC_LO16_PCREL:          r = R_PPC_REL16_LO;             break;
1571     case BFD_RELOC_HI16_PCREL:          r = R_PPC_REL16_HI;             break;
1572     case BFD_RELOC_HI16_S_PCREL:        r = R_PPC_REL16_HA;             break;
1573     case BFD_RELOC_VTABLE_INHERIT:      r = R_PPC_GNU_VTINHERIT;        break;
1574     case BFD_RELOC_VTABLE_ENTRY:        r = R_PPC_GNU_VTENTRY;          break;
1575     }
1576
1577   return ppc_elf_howto_table[r];
1578 };
1579
1580 /* Set the howto pointer for a PowerPC ELF reloc.  */
1581
1582 static void
1583 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1584                        arelent *cache_ptr,
1585                        Elf_Internal_Rela *dst)
1586 {
1587   /* Initialize howto table if not already done.  */
1588   if (!ppc_elf_howto_table[R_PPC_ADDR32])
1589     ppc_elf_howto_init ();
1590
1591   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1592   cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1593 }
1594
1595 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs.  */
1596
1597 static bfd_reloc_status_type
1598 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1599                          arelent *reloc_entry,
1600                          asymbol *symbol,
1601                          void *data ATTRIBUTE_UNUSED,
1602                          asection *input_section,
1603                          bfd *output_bfd,
1604                          char **error_message ATTRIBUTE_UNUSED)
1605 {
1606   bfd_vma relocation;
1607
1608   if (output_bfd != NULL)
1609     {
1610       reloc_entry->address += input_section->output_offset;
1611       return bfd_reloc_ok;
1612     }
1613
1614   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1615     return bfd_reloc_outofrange;
1616
1617   if (bfd_is_com_section (symbol->section))
1618     relocation = 0;
1619   else
1620     relocation = symbol->value;
1621
1622   relocation += symbol->section->output_section->vma;
1623   relocation += symbol->section->output_offset;
1624   relocation += reloc_entry->addend;
1625   if (reloc_entry->howto->pc_relative)
1626     relocation -= reloc_entry->address;
1627
1628   reloc_entry->addend += (relocation & 0x8000) << 1;
1629
1630   return bfd_reloc_continue;
1631 }
1632
1633 static bfd_reloc_status_type
1634 ppc_elf_unhandled_reloc (bfd *abfd,
1635                          arelent *reloc_entry,
1636                          asymbol *symbol,
1637                          void *data,
1638                          asection *input_section,
1639                          bfd *output_bfd,
1640                          char **error_message)
1641 {
1642   /* If this is a relocatable link (output_bfd test tells us), just
1643      call the generic function.  Any adjustment will be done at final
1644      link time.  */
1645   if (output_bfd != NULL)
1646     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1647                                   input_section, output_bfd, error_message);
1648
1649   if (error_message != NULL)
1650     {
1651       static char buf[60];
1652       sprintf (buf, _("generic linker can't handle %s"),
1653                reloc_entry->howto->name);
1654       *error_message = buf;
1655     }
1656   return bfd_reloc_dangerous;
1657 }
1658 \f
1659 /* Sections created by the linker.  */
1660
1661 typedef struct elf_linker_section
1662 {
1663   /* Pointer to the bfd section.  */
1664   asection *section;
1665   /* Section name.  */
1666   const char *name;
1667   /* Associated bss section name.  */
1668   const char *bss_name;
1669   /* Associated symbol name.  */
1670   const char *sym_name;
1671   /* Associated symbol.  */
1672   struct elf_link_hash_entry *sym;
1673 } elf_linker_section_t;
1674
1675 /* Linked list of allocated pointer entries.  This hangs off of the
1676    symbol lists, and provides allows us to return different pointers,
1677    based on different addend's.  */
1678
1679 typedef struct elf_linker_section_pointers
1680 {
1681   /* next allocated pointer for this symbol */
1682   struct elf_linker_section_pointers *next;
1683   /* offset of pointer from beginning of section */
1684   bfd_vma offset;
1685   /* addend used */
1686   bfd_vma addend;
1687   /* which linker section this is */
1688   elf_linker_section_t *lsect;
1689 } elf_linker_section_pointers_t;
1690
1691 struct ppc_elf_obj_tdata
1692 {
1693   struct elf_obj_tdata elf;
1694
1695   /* A mapping from local symbols to offsets into the various linker
1696      sections added.  This is index by the symbol index.  */
1697   elf_linker_section_pointers_t **linker_section_pointers;
1698 };
1699
1700 #define ppc_elf_tdata(bfd) \
1701   ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1702
1703 #define elf_local_ptr_offsets(bfd) \
1704   (ppc_elf_tdata (bfd)->linker_section_pointers)
1705
1706 /* Override the generic function because we store some extras.  */
1707
1708 static bfd_boolean
1709 ppc_elf_mkobject (bfd *abfd)
1710 {
1711   if (abfd->tdata.any == NULL)
1712     {
1713       bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
1714       abfd->tdata.any = bfd_zalloc (abfd, amt);
1715       if (abfd->tdata.any == NULL)
1716         return FALSE;
1717     }
1718   return bfd_elf_mkobject (abfd);
1719 }
1720
1721 /* Fix bad default arch selected for a 32 bit input bfd when the
1722    default is 64 bit.  */
1723
1724 static bfd_boolean
1725 ppc_elf_object_p (bfd *abfd)
1726 {
1727   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1728     {
1729       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1730
1731       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1732         {
1733           /* Relies on arch after 64 bit default being 32 bit default.  */
1734           abfd->arch_info = abfd->arch_info->next;
1735           BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1736         }
1737     }
1738   return TRUE;
1739 }
1740
1741 /* Function to set whether a module needs the -mrelocatable bit set.  */
1742
1743 static bfd_boolean
1744 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1745 {
1746   BFD_ASSERT (!elf_flags_init (abfd)
1747               || elf_elfheader (abfd)->e_flags == flags);
1748
1749   elf_elfheader (abfd)->e_flags = flags;
1750   elf_flags_init (abfd) = TRUE;
1751   return TRUE;
1752 }
1753
1754 /* Support for core dump NOTE sections.  */
1755
1756 static bfd_boolean
1757 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1758 {
1759   int offset;
1760   unsigned int size;
1761
1762   switch (note->descsz)
1763     {
1764     default:
1765       return FALSE;
1766
1767     case 268:           /* Linux/PPC.  */
1768       /* pr_cursig */
1769       elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1770
1771       /* pr_pid */
1772       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1773
1774       /* pr_reg */
1775       offset = 72;
1776       size = 192;
1777
1778       break;
1779     }
1780
1781   /* Make a ".reg/999" section.  */
1782   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1783                                           size, note->descpos + offset);
1784 }
1785
1786 static bfd_boolean
1787 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1788 {
1789   switch (note->descsz)
1790     {
1791     default:
1792       return FALSE;
1793
1794     case 128:           /* Linux/PPC elf_prpsinfo.  */
1795       elf_tdata (abfd)->core_program
1796         = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1797       elf_tdata (abfd)->core_command
1798         = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1799     }
1800
1801   /* Note that for some reason, a spurious space is tacked
1802      onto the end of the args in some (at least one anyway)
1803      implementations, so strip it off if it exists.  */
1804
1805   {
1806     char *command = elf_tdata (abfd)->core_command;
1807     int n = strlen (command);
1808
1809     if (0 < n && command[n - 1] == ' ')
1810       command[n - 1] = '\0';
1811   }
1812
1813   return TRUE;
1814 }
1815
1816 /* Return address for Ith PLT stub in section PLT, for relocation REL
1817    or (bfd_vma) -1 if it should not be included.  */
1818
1819 static bfd_vma
1820 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1821                      const asection *plt ATTRIBUTE_UNUSED,
1822                      const arelent *rel)
1823 {
1824   return rel->address;
1825 }
1826
1827 /* Handle a PowerPC specific section when reading an object file.  This
1828    is called when bfd_section_from_shdr finds a section with an unknown
1829    type.  */
1830
1831 static bfd_boolean
1832 ppc_elf_section_from_shdr (bfd *abfd,
1833                            Elf_Internal_Shdr *hdr,
1834                            const char *name,
1835                            int shindex)
1836 {
1837   asection *newsect;
1838   flagword flags;
1839
1840   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1841     return FALSE;
1842
1843   newsect = hdr->bfd_section;
1844   flags = bfd_get_section_flags (abfd, newsect);
1845   if (hdr->sh_flags & SHF_EXCLUDE)
1846     flags |= SEC_EXCLUDE;
1847
1848   if (hdr->sh_type == SHT_ORDERED)
1849     flags |= SEC_SORT_ENTRIES;
1850
1851   bfd_set_section_flags (abfd, newsect, flags);
1852   return TRUE;
1853 }
1854
1855 /* Set up any other section flags and such that may be necessary.  */
1856
1857 static bfd_boolean
1858 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1859                        Elf_Internal_Shdr *shdr,
1860                        asection *asect)
1861 {
1862   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
1863     shdr->sh_flags |= SHF_EXCLUDE;
1864
1865   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1866     shdr->sh_type = SHT_ORDERED;
1867
1868   return TRUE;
1869 }
1870
1871 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
1872    need to bump up the number of section headers.  */
1873
1874 static int
1875 ppc_elf_additional_program_headers (bfd *abfd,
1876                                     struct bfd_link_info *info ATTRIBUTE_UNUSED)
1877 {
1878   asection *s;
1879   int ret = 0;
1880
1881   s = bfd_get_section_by_name (abfd, ".sbss2");
1882   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1883     ++ret;
1884
1885   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1886   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1887     ++ret;
1888
1889   return ret;
1890 }
1891
1892 /* Add extra PPC sections -- Note, for now, make .sbss2 and
1893    .PPC.EMB.sbss0 a normal section, and not a bss section so
1894    that the linker doesn't crater when trying to make more than
1895    2 sections.  */
1896
1897 static const struct bfd_elf_special_section ppc_elf_special_sections[] =
1898 {
1899   { STRING_COMMA_LEN (".plt"),             0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
1900   { STRING_COMMA_LEN (".sbss"),           -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
1901   { STRING_COMMA_LEN (".sbss2"),          -2, SHT_PROGBITS, SHF_ALLOC },
1902   { STRING_COMMA_LEN (".sdata"),          -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1903   { STRING_COMMA_LEN (".sdata2"),         -2, SHT_PROGBITS, SHF_ALLOC },
1904   { STRING_COMMA_LEN (".tags"),            0, SHT_ORDERED,  SHF_ALLOC },
1905   { STRING_COMMA_LEN (".PPC.EMB.apuinfo"), 0, SHT_NOTE,     0 },
1906   { STRING_COMMA_LEN (".PPC.EMB.sbss0"),   0, SHT_PROGBITS, SHF_ALLOC },
1907   { STRING_COMMA_LEN (".PPC.EMB.sdata0"),  0, SHT_PROGBITS, SHF_ALLOC },
1908   { NULL,                              0,  0, 0,            0 }
1909 };
1910
1911 /* This is what we want for new plt/got.  */
1912 static struct bfd_elf_special_section ppc_alt_plt =
1913   { STRING_COMMA_LEN (".plt"),             0, SHT_PROGBITS, SHF_ALLOC };
1914
1915 static const struct bfd_elf_special_section *
1916 ppc_elf_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
1917 {
1918   const struct bfd_elf_special_section *ssect;
1919
1920   /* See if this is one of the special sections.  */
1921   if (sec->name == NULL)
1922     return NULL;
1923
1924   ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
1925                                         sec->use_rela_p);
1926   if (ssect != NULL)
1927     {
1928       if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
1929         ssect = &ppc_alt_plt;
1930       return ssect;
1931     }
1932
1933   return _bfd_elf_get_sec_type_attr (abfd, sec);
1934 }
1935 \f
1936 /* Very simple linked list structure for recording apuinfo values.  */
1937 typedef struct apuinfo_list
1938 {
1939   struct apuinfo_list *next;
1940   unsigned long value;
1941 }
1942 apuinfo_list;
1943
1944 static apuinfo_list *head;
1945
1946
1947 static void
1948 apuinfo_list_init (void)
1949 {
1950   head = NULL;
1951 }
1952
1953 static void
1954 apuinfo_list_add (unsigned long value)
1955 {
1956   apuinfo_list *entry = head;
1957
1958   while (entry != NULL)
1959     {
1960       if (entry->value == value)
1961         return;
1962       entry = entry->next;
1963     }
1964
1965   entry = bfd_malloc (sizeof (* entry));
1966   if (entry == NULL)
1967     return;
1968
1969   entry->value = value;
1970   entry->next  = head;
1971   head = entry;
1972 }
1973
1974 static unsigned
1975 apuinfo_list_length (void)
1976 {
1977   apuinfo_list *entry;
1978   unsigned long count;
1979
1980   for (entry = head, count = 0;
1981        entry;
1982        entry = entry->next)
1983     ++ count;
1984
1985   return count;
1986 }
1987
1988 static inline unsigned long
1989 apuinfo_list_element (unsigned long number)
1990 {
1991   apuinfo_list * entry;
1992
1993   for (entry = head;
1994        entry && number --;
1995        entry = entry->next)
1996     ;
1997
1998   return entry ? entry->value : 0;
1999 }
2000
2001 static void
2002 apuinfo_list_finish (void)
2003 {
2004   apuinfo_list *entry;
2005
2006   for (entry = head; entry;)
2007     {
2008       apuinfo_list *next = entry->next;
2009       free (entry);
2010       entry = next;
2011     }
2012
2013   head = NULL;
2014 }
2015
2016 #define APUINFO_SECTION_NAME    ".PPC.EMB.apuinfo"
2017 #define APUINFO_LABEL           "APUinfo"
2018
2019 /* Scan the input BFDs and create a linked list of
2020    the APUinfo values that will need to be emitted.  */
2021
2022 static void
2023 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
2024 {
2025   bfd *ibfd;
2026   asection *asec;
2027   char *buffer;
2028   unsigned num_input_sections;
2029   bfd_size_type output_section_size;
2030   unsigned i;
2031   unsigned num_entries;
2032   unsigned long offset;
2033   unsigned long length;
2034   const char *error_message = NULL;
2035
2036   if (link_info == NULL)
2037     return;
2038
2039   /* Scan the input bfds, looking for apuinfo sections.  */
2040   num_input_sections = 0;
2041   output_section_size = 0;
2042
2043   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2044     {
2045       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2046       if (asec)
2047         {
2048           ++ num_input_sections;
2049           output_section_size += asec->size;
2050         }
2051     }
2052
2053   /* We need at least one input sections
2054      in order to make merging worthwhile.  */
2055   if (num_input_sections < 1)
2056     return;
2057
2058   /* Just make sure that the output section exists as well.  */
2059   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2060   if (asec == NULL)
2061     return;
2062
2063   /* Allocate a buffer for the contents of the input sections.  */
2064   buffer = bfd_malloc (output_section_size);
2065   if (buffer == NULL)
2066     return;
2067
2068   offset = 0;
2069   apuinfo_list_init ();
2070
2071   /* Read in the input sections contents.  */
2072   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2073     {
2074       unsigned long datum;
2075       char *ptr;
2076
2077       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2078       if (asec == NULL)
2079         continue;
2080
2081       length = asec->size;
2082       if (length < 24)
2083         {
2084           error_message = _("corrupt or empty %s section in %B");
2085           goto fail;
2086         }
2087
2088       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
2089           || (bfd_bread (buffer + offset, length, ibfd) != length))
2090         {
2091           error_message = _("unable to read in %s section from %B");
2092           goto fail;
2093         }
2094
2095       /* Process the contents of the section.  */
2096       ptr = buffer + offset;
2097       error_message = _("corrupt %s section in %B");
2098
2099       /* Verify the contents of the header.  Note - we have to
2100          extract the values this way in order to allow for a
2101          host whose endian-ness is different from the target.  */
2102       datum = bfd_get_32 (ibfd, ptr);
2103       if (datum != sizeof APUINFO_LABEL)
2104         goto fail;
2105
2106       datum = bfd_get_32 (ibfd, ptr + 8);
2107       if (datum != 0x2)
2108         goto fail;
2109
2110       if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
2111         goto fail;
2112
2113       /* Get the number of bytes used for apuinfo entries.  */
2114       datum = bfd_get_32 (ibfd, ptr + 4);
2115       if (datum + 20 != length)
2116         goto fail;
2117
2118       /* Make sure that we do not run off the end of the section.  */
2119       if (offset + length > output_section_size)
2120         goto fail;
2121
2122       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
2123       for (i = 0; i < datum; i += 4)
2124         apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
2125
2126       /* Update the offset.  */
2127       offset += length;
2128     }
2129
2130   error_message = NULL;
2131
2132   /* Compute the size of the output section.  */
2133   num_entries = apuinfo_list_length ();
2134   output_section_size = 20 + num_entries * 4;
2135
2136   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2137
2138   if (! bfd_set_section_size (abfd, asec, output_section_size))
2139     ibfd = abfd,
2140       error_message = _("warning: unable to set size of %s section in %B");
2141
2142  fail:
2143   free (buffer);
2144
2145   if (error_message)
2146     (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
2147 }
2148
2149 /* Prevent the output section from accumulating the input sections'
2150    contents.  We have already stored this in our linked list structure.  */
2151
2152 static bfd_boolean
2153 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
2154                        asection *asec,
2155                        bfd_byte *contents ATTRIBUTE_UNUSED)
2156 {
2157   return (apuinfo_list_length ()
2158           && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
2159 }
2160
2161 /* Finally we can generate the output section.  */
2162
2163 static void
2164 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
2165 {
2166   bfd_byte *buffer;
2167   asection *asec;
2168   unsigned i;
2169   unsigned num_entries;
2170   bfd_size_type length;
2171
2172   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2173   if (asec == NULL)
2174     return;
2175
2176   if (apuinfo_list_length () == 0)
2177     return;
2178
2179   length = asec->size;
2180   if (length < 20)
2181     return;
2182
2183   buffer = bfd_malloc (length);
2184   if (buffer == NULL)
2185     {
2186       (*_bfd_error_handler)
2187         (_("failed to allocate space for new APUinfo section."));
2188       return;
2189     }
2190
2191   /* Create the apuinfo header.  */
2192   num_entries = apuinfo_list_length ();
2193   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2194   bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2195   bfd_put_32 (abfd, 0x2, buffer + 8);
2196   strcpy ((char *) buffer + 12, APUINFO_LABEL);
2197
2198   length = 20;
2199   for (i = 0; i < num_entries; i++)
2200     {
2201       bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2202       length += 4;
2203     }
2204
2205   if (length != asec->size)
2206     (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2207
2208   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2209     (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2210
2211   free (buffer);
2212
2213   apuinfo_list_finish ();
2214 }
2215 \f
2216 /* The following functions are specific to the ELF linker, while
2217    functions above are used generally.  They appear in this file more
2218    or less in the order in which they are called.  eg.
2219    ppc_elf_check_relocs is called early in the link process,
2220    ppc_elf_finish_dynamic_sections is one of the last functions
2221    called.  */
2222
2223 /* The PPC linker needs to keep track of the number of relocs that it
2224    decides to copy as dynamic relocs in check_relocs for each symbol.
2225    This is so that it can later discard them if they are found to be
2226    unnecessary.  We store the information in a field extending the
2227    regular ELF linker hash table.  */
2228
2229 struct ppc_elf_dyn_relocs
2230 {
2231   struct ppc_elf_dyn_relocs *next;
2232
2233   /* The input section of the reloc.  */
2234   asection *sec;
2235
2236   /* Total number of relocs copied for the input section.  */
2237   bfd_size_type count;
2238
2239   /* Number of pc-relative relocs copied for the input section.  */
2240   bfd_size_type pc_count;
2241 };
2242
2243 /* Track PLT entries needed for a given symbol.  We might need more
2244    than one glink entry per symbol.  */
2245 struct plt_entry
2246 {
2247   struct plt_entry *next;
2248
2249   /* -fPIC uses multiple GOT sections, one per file, called ".got2".
2250      This field stores the offset into .got2 used to initialise the
2251      GOT pointer reg.  It will always be at least 32768 (and for
2252      current gcc this is the only offset used).  */
2253   bfd_vma addend;
2254
2255   /* The .got2 section.  */
2256   asection *sec;
2257
2258   /* PLT refcount or offset.  */
2259   union
2260     {
2261       bfd_signed_vma refcount;
2262       bfd_vma offset;
2263     } plt;
2264
2265   /* .glink stub offset.  */
2266   bfd_vma glink_offset;
2267 };
2268
2269 /* Of those relocs that might be copied as dynamic relocs, this macro
2270    selects those that must be copied when linking a shared library,
2271    even when the symbol is local.  */
2272
2273 #define MUST_BE_DYN_RELOC(RTYPE)                \
2274   ((RTYPE) != R_PPC_REL24                       \
2275    && (RTYPE) != R_PPC_REL14                    \
2276    && (RTYPE) != R_PPC_REL14_BRTAKEN            \
2277    && (RTYPE) != R_PPC_REL14_BRNTAKEN           \
2278    && (RTYPE) != R_PPC_REL32)
2279
2280 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2281    copying dynamic variables from a shared lib into an app's dynbss
2282    section, and instead use a dynamic relocation to point into the
2283    shared lib.  */
2284 #define ELIMINATE_COPY_RELOCS 1
2285
2286 /* PPC ELF linker hash entry.  */
2287
2288 struct ppc_elf_link_hash_entry
2289 {
2290   struct elf_link_hash_entry elf;
2291
2292   /* If this symbol is used in the linker created sections, the processor
2293      specific backend uses this field to map the field into the offset
2294      from the beginning of the section.  */
2295   elf_linker_section_pointers_t *linker_section_pointer;
2296
2297   /* Track dynamic relocs copied for this symbol.  */
2298   struct ppc_elf_dyn_relocs *dyn_relocs;
2299
2300   /* Contexts in which symbol is used in the GOT (or TOC).
2301      TLS_GD .. TLS_TLS bits are or'd into the mask as the
2302      corresponding relocs are encountered during check_relocs.
2303      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2304      indicate the corresponding GOT entry type is not needed.  */
2305 #define TLS_GD           1      /* GD reloc. */
2306 #define TLS_LD           2      /* LD reloc. */
2307 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
2308 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
2309 #define TLS_TLS         16      /* Any TLS reloc.  */
2310 #define TLS_TPRELGD     32      /* TPREL reloc resulting from GD->IE. */
2311   char tls_mask;
2312
2313   /* Nonzero if we have seen a small data relocation referring to this
2314      symbol.  */
2315   unsigned char has_sda_refs;
2316 };
2317
2318 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2319
2320 enum ppc_elf_plt_type {
2321   PLT_UNSET,
2322   PLT_OLD,
2323   PLT_NEW,
2324   PLT_VXWORKS
2325 };
2326
2327 /* PPC ELF linker hash table.  */
2328
2329 struct ppc_elf_link_hash_table
2330 {
2331   struct elf_link_hash_table elf;
2332
2333   /* Short-cuts to get to dynamic linker sections.  */
2334   asection *got;
2335   asection *relgot;
2336   asection *glink;
2337   asection *plt;
2338   asection *relplt;
2339   asection *dynbss;
2340   asection *relbss;
2341   asection *dynsbss;
2342   asection *relsbss;
2343   elf_linker_section_t sdata[2];
2344   asection *sbss;
2345
2346   /* Shortcut to .__tls_get_addr.  */
2347   struct elf_link_hash_entry *tls_get_addr;
2348
2349   /* TLS local dynamic got entry handling.  */
2350   union {
2351     bfd_signed_vma refcount;
2352     bfd_vma offset;
2353   } tlsld_got;
2354
2355   /* Offset of PltResolve function in glink.  */
2356   bfd_vma glink_pltresolve;
2357
2358   /* Size of reserved GOT entries.  */
2359   unsigned int got_header_size;
2360   /* Non-zero if allocating the header left a gap.  */
2361   unsigned int got_gap;
2362
2363   /* The type of PLT we have chosen to use.  */
2364   enum ppc_elf_plt_type plt_type;
2365
2366   /* Whether we can use the new PLT layout.  */
2367   unsigned int can_use_new_plt:1;
2368
2369   /* Set if we should emit symbols for stubs.  */
2370   unsigned int emit_stub_syms:1;
2371
2372   /* Small local sym to section mapping cache.  */
2373   struct sym_sec_cache sym_sec;
2374
2375   /* The (unloaded but important) .rela.plt.unloaded on VxWorks.  */
2376   asection *srelplt2;
2377
2378   /* The .got.plt section (VxWorks only)*/
2379   asection *sgotplt;
2380
2381   /* True if the target system is VxWorks.  */
2382   int is_vxworks;
2383
2384   /* The size of PLT entries.  */
2385   int plt_entry_size;
2386   /* The distance between adjacent PLT slots.  */
2387   int plt_slot_size;
2388   /* The size of the first PLT entry.  */
2389   int plt_initial_entry_size;
2390 };
2391
2392 /* Get the PPC ELF linker hash table from a link_info structure.  */
2393
2394 #define ppc_elf_hash_table(p) \
2395   ((struct ppc_elf_link_hash_table *) (p)->hash)
2396
2397 /* Create an entry in a PPC ELF linker hash table.  */
2398
2399 static struct bfd_hash_entry *
2400 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2401                            struct bfd_hash_table *table,
2402                            const char *string)
2403 {
2404   /* Allocate the structure if it has not already been allocated by a
2405      subclass.  */
2406   if (entry == NULL)
2407     {
2408       entry = bfd_hash_allocate (table,
2409                                  sizeof (struct ppc_elf_link_hash_entry));
2410       if (entry == NULL)
2411         return entry;
2412     }
2413
2414   /* Call the allocation method of the superclass.  */
2415   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2416   if (entry != NULL)
2417     {
2418       ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2419       ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2420       ppc_elf_hash_entry (entry)->tls_mask = 0;
2421     }
2422
2423   return entry;
2424 }
2425
2426 /* Create a PPC ELF linker hash table.  */
2427
2428 static struct bfd_link_hash_table *
2429 ppc_elf_link_hash_table_create (bfd *abfd)
2430 {
2431   struct ppc_elf_link_hash_table *ret;
2432
2433   ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2434   if (ret == NULL)
2435     return NULL;
2436
2437   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
2438                                       ppc_elf_link_hash_newfunc,
2439                                       sizeof (struct ppc_elf_link_hash_entry)))
2440     {
2441       free (ret);
2442       return NULL;
2443     }
2444
2445   ret->elf.init_plt_refcount.refcount = 0;
2446   ret->elf.init_plt_refcount.glist = NULL;
2447   ret->elf.init_plt_offset.offset = 0;
2448   ret->elf.init_plt_offset.glist = NULL;
2449
2450   ret->sdata[0].name = ".sdata";
2451   ret->sdata[0].sym_name = "_SDA_BASE_";
2452   ret->sdata[0].bss_name = ".sbss";
2453
2454   ret->sdata[1].name = ".sdata2";
2455   ret->sdata[1].sym_name = "_SDA2_BASE_";
2456   ret->sdata[1].bss_name = ".sbss2";
2457
2458   ret->plt_entry_size = 12;
2459   ret->plt_slot_size = 8;
2460   ret->plt_initial_entry_size = 72;
2461   
2462   ret->is_vxworks = 0;
2463
2464   return &ret->elf.root;
2465 }
2466
2467 /* Create .got and the related sections.  */
2468
2469 static bfd_boolean
2470 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2471 {
2472   struct ppc_elf_link_hash_table *htab;
2473   asection *s;
2474   flagword flags;
2475
2476   if (!_bfd_elf_create_got_section (abfd, info))
2477     return FALSE;
2478
2479   htab = ppc_elf_hash_table (info);
2480   htab->got = s = bfd_get_section_by_name (abfd, ".got");
2481   if (s == NULL)
2482     abort ();
2483
2484   if (htab->is_vxworks)
2485     {
2486       htab->sgotplt = bfd_get_section_by_name (abfd, ".got.plt");
2487       if (!htab->sgotplt)
2488         abort ();
2489     }
2490   else
2491     {
2492       /* The powerpc .got has a blrl instruction in it.  Mark it
2493          executable.  */
2494       flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
2495                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2496       if (!bfd_set_section_flags (abfd, s, flags))
2497         return FALSE;
2498     }
2499
2500   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2501            | SEC_LINKER_CREATED | SEC_READONLY);
2502   htab->relgot = bfd_make_section_with_flags (abfd, ".rela.got", flags);
2503   if (!htab->relgot
2504       || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2505     return FALSE;
2506
2507   return TRUE;
2508 }
2509
2510 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2511    to output sections (just like _bfd_elf_create_dynamic_sections has
2512    to create .dynbss and .rela.bss).  */
2513
2514 static bfd_boolean
2515 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2516 {
2517   struct ppc_elf_link_hash_table *htab;
2518   asection *s;
2519   flagword flags;
2520
2521   htab = ppc_elf_hash_table (info);
2522
2523   if (htab->got == NULL
2524       && !ppc_elf_create_got (abfd, info))
2525     return FALSE;
2526
2527   if (!_bfd_elf_create_dynamic_sections (abfd, info))
2528     return FALSE;
2529
2530   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2531            | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2532
2533   s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags | SEC_CODE);
2534   htab->glink = s;
2535   if (s == NULL
2536       || !bfd_set_section_alignment (abfd, s, 4))
2537     return FALSE;
2538
2539   htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2540   s = bfd_make_section_with_flags (abfd, ".dynsbss",
2541                                    SEC_ALLOC | SEC_LINKER_CREATED);
2542   htab->dynsbss = s;
2543   if (s == NULL)
2544     return FALSE;
2545
2546   if (! info->shared)
2547     {
2548       htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2549       s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
2550       htab->relsbss = s;
2551       if (s == NULL
2552           || ! bfd_set_section_alignment (abfd, s, 2))
2553         return FALSE;
2554     }
2555
2556   if (htab->is_vxworks
2557       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2558     return FALSE;
2559
2560   htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2561   htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2562   if (s == NULL)
2563     abort ();
2564
2565   flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
2566   if (htab->plt_type == PLT_VXWORKS)
2567     /* The VxWorks PLT is a loaded section with contents.  */
2568     flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
2569   return bfd_set_section_flags (abfd, s, flags);
2570 }
2571
2572 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
2573
2574 static void
2575 ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
2576                               struct elf_link_hash_entry *dir,
2577                               struct elf_link_hash_entry *ind)
2578 {
2579   struct ppc_elf_link_hash_entry *edir, *eind;
2580
2581   edir = (struct ppc_elf_link_hash_entry *) dir;
2582   eind = (struct ppc_elf_link_hash_entry *) ind;
2583
2584   if (eind->dyn_relocs != NULL)
2585     {
2586       if (edir->dyn_relocs != NULL)
2587         {
2588           struct ppc_elf_dyn_relocs **pp;
2589           struct ppc_elf_dyn_relocs *p;
2590
2591           /* Add reloc counts against the indirect sym to the direct sym
2592              list.  Merge any entries against the same section.  */
2593           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2594             {
2595               struct ppc_elf_dyn_relocs *q;
2596
2597               for (q = edir->dyn_relocs; q != NULL; q = q->next)
2598                 if (q->sec == p->sec)
2599                   {
2600                     q->pc_count += p->pc_count;
2601                     q->count += p->count;
2602                     *pp = p->next;
2603                     break;
2604                   }
2605               if (q == NULL)
2606                 pp = &p->next;
2607             }
2608           *pp = edir->dyn_relocs;
2609         }
2610
2611       edir->dyn_relocs = eind->dyn_relocs;
2612       eind->dyn_relocs = NULL;
2613     }
2614
2615   edir->tls_mask |= eind->tls_mask;
2616   edir->has_sda_refs |= eind->has_sda_refs;
2617
2618   /* If called to transfer flags for a weakdef during processing
2619      of elf_adjust_dynamic_symbol, don't copy non_got_ref.
2620      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
2621   if (!(ELIMINATE_COPY_RELOCS
2622         && eind->elf.root.type != bfd_link_hash_indirect
2623         && edir->elf.dynamic_adjusted))
2624     edir->elf.non_got_ref |= eind->elf.non_got_ref;
2625
2626   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
2627   edir->elf.ref_regular |= eind->elf.ref_regular;
2628   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
2629   edir->elf.needs_plt |= eind->elf.needs_plt;
2630
2631   /* If we were called to copy over info for a weak sym, that's all.  */
2632   if (eind->elf.root.type != bfd_link_hash_indirect)
2633     return;
2634
2635   /* Copy over the GOT refcount entries that we may have already seen to
2636      the symbol which just became indirect.  */
2637   edir->elf.got.refcount += eind->elf.got.refcount;
2638   eind->elf.got.refcount = 0;
2639
2640   /* And plt entries.  */
2641   if (eind->elf.plt.plist != NULL)
2642     {
2643       if (edir->elf.plt.plist != NULL)
2644         {
2645           struct plt_entry **entp;
2646           struct plt_entry *ent;
2647
2648           for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
2649             {
2650               struct plt_entry *dent;
2651
2652               for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
2653                 if (dent->sec == ent->sec && dent->addend == ent->addend)
2654                   {
2655                     dent->plt.refcount += ent->plt.refcount;
2656                     *entp = ent->next;
2657                     break;
2658                   }
2659               if (dent == NULL)
2660                 entp = &ent->next;
2661             }
2662           *entp = edir->elf.plt.plist;
2663         }
2664
2665       edir->elf.plt.plist = eind->elf.plt.plist;
2666       eind->elf.plt.plist = NULL;
2667     }
2668
2669   if (eind->elf.dynindx != -1)
2670     {
2671       if (edir->elf.dynindx != -1)
2672         _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
2673                                 edir->elf.dynstr_index);
2674       edir->elf.dynindx = eind->elf.dynindx;
2675       edir->elf.dynstr_index = eind->elf.dynstr_index;
2676       eind->elf.dynindx = -1;
2677       eind->elf.dynstr_index = 0;
2678     }
2679 }
2680
2681 /* Return 1 if target is one of ours.  */
2682
2683 static bfd_boolean
2684 is_ppc_elf_target (const struct bfd_target *targ)
2685 {
2686   extern const bfd_target bfd_elf32_powerpc_vec;
2687   extern const bfd_target bfd_elf32_powerpc_vxworks_vec;
2688   extern const bfd_target bfd_elf32_powerpcle_vec;
2689
2690   return (targ == &bfd_elf32_powerpc_vec
2691           || targ == &bfd_elf32_powerpc_vxworks_vec
2692           || targ == &bfd_elf32_powerpcle_vec);
2693 }
2694
2695 /* Hook called by the linker routine which adds symbols from an object
2696    file.  We use it to put .comm items in .sbss, and not .bss.  */
2697
2698 static bfd_boolean
2699 ppc_elf_add_symbol_hook (bfd *abfd,
2700                          struct bfd_link_info *info,
2701                          Elf_Internal_Sym *sym,
2702                          const char **namep ATTRIBUTE_UNUSED,
2703                          flagword *flagsp ATTRIBUTE_UNUSED,
2704                          asection **secp,
2705                          bfd_vma *valp)
2706 {
2707   if (sym->st_shndx == SHN_COMMON
2708       && !info->relocatable
2709       && sym->st_size <= elf_gp_size (abfd)
2710       && is_ppc_elf_target (info->hash->creator))
2711     {
2712       /* Common symbols less than or equal to -G nn bytes are automatically
2713          put into .sbss.  */
2714       struct ppc_elf_link_hash_table *htab;
2715
2716       htab = ppc_elf_hash_table (info);
2717       if (htab->sbss == NULL)
2718         {
2719           flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
2720
2721           if (!htab->elf.dynobj)
2722             htab->elf.dynobj = abfd;
2723
2724           htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2725                                                            ".sbss",
2726                                                            flags);
2727           if (htab->sbss == NULL)
2728             return FALSE;
2729         }
2730
2731       *secp = htab->sbss;
2732       *valp = sym->st_size;
2733     }
2734
2735   return TRUE;
2736 }
2737 \f
2738 static bfd_boolean
2739 create_sdata_sym (struct ppc_elf_link_hash_table *htab,
2740                   elf_linker_section_t *lsect)
2741 {
2742   lsect->sym = elf_link_hash_lookup (&htab->elf, lsect->sym_name,
2743                                      TRUE, FALSE, TRUE);
2744   if (lsect->sym == NULL)
2745     return FALSE;
2746   if (lsect->sym->root.type == bfd_link_hash_new)
2747     lsect->sym->non_elf = 0;
2748   lsect->sym->ref_regular = 1;
2749   return TRUE;
2750 }
2751
2752 /* Create a special linker section.  */
2753
2754 static bfd_boolean
2755 ppc_elf_create_linker_section (bfd *abfd,
2756                                struct bfd_link_info *info,
2757                                flagword flags,
2758                                elf_linker_section_t *lsect)
2759 {
2760   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2761   asection *s;
2762
2763   flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2764             | SEC_LINKER_CREATED);
2765
2766   /* Record the first bfd that needs the special sections.  */
2767   if (!htab->elf.dynobj)
2768     htab->elf.dynobj = abfd;
2769
2770   s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
2771                                           lsect->name,
2772                                           flags);
2773   if (s == NULL
2774       || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2775     return FALSE;
2776   lsect->section = s;
2777
2778   return create_sdata_sym (htab, lsect);
2779 }
2780
2781 /* Find a linker generated pointer with a given addend and type.  */
2782
2783 static elf_linker_section_pointers_t *
2784 elf_find_pointer_linker_section
2785   (elf_linker_section_pointers_t *linker_pointers,
2786    bfd_vma addend,
2787    elf_linker_section_t *lsect)
2788 {
2789   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2790     if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2791       return linker_pointers;
2792
2793   return NULL;
2794 }
2795
2796 /* Allocate a pointer to live in a linker created section.  */
2797
2798 static bfd_boolean
2799 elf_create_pointer_linker_section (bfd *abfd,
2800                                    elf_linker_section_t *lsect,
2801                                    struct elf_link_hash_entry *h,
2802                                    const Elf_Internal_Rela *rel)
2803 {
2804   elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2805   elf_linker_section_pointers_t *linker_section_ptr;
2806   unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2807   bfd_size_type amt;
2808
2809   BFD_ASSERT (lsect != NULL);
2810
2811   /* Is this a global symbol?  */
2812   if (h != NULL)
2813     {
2814       struct ppc_elf_link_hash_entry *eh;
2815
2816       /* Has this symbol already been allocated?  If so, our work is done.  */
2817       eh = (struct ppc_elf_link_hash_entry *) h;
2818       if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2819                                            rel->r_addend,
2820                                            lsect))
2821         return TRUE;
2822
2823       ptr_linker_section_ptr = &eh->linker_section_pointer;
2824     }
2825   else
2826     {
2827       /* Allocation of a pointer to a local symbol.  */
2828       elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2829
2830       /* Allocate a table to hold the local symbols if first time.  */
2831       if (!ptr)
2832         {
2833           unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2834
2835           amt = num_symbols;
2836           amt *= sizeof (elf_linker_section_pointers_t *);
2837           ptr = bfd_zalloc (abfd, amt);
2838
2839           if (!ptr)
2840             return FALSE;
2841
2842           elf_local_ptr_offsets (abfd) = ptr;
2843         }
2844
2845       /* Has this symbol already been allocated?  If so, our work is done.  */
2846       if (elf_find_pointer_linker_section (ptr[r_symndx],
2847                                            rel->r_addend,
2848                                            lsect))
2849         return TRUE;
2850
2851       ptr_linker_section_ptr = &ptr[r_symndx];
2852     }
2853
2854   /* Allocate space for a pointer in the linker section, and allocate
2855      a new pointer record from internal memory.  */
2856   BFD_ASSERT (ptr_linker_section_ptr != NULL);
2857   amt = sizeof (elf_linker_section_pointers_t);
2858   linker_section_ptr = bfd_alloc (abfd, amt);
2859
2860   if (!linker_section_ptr)
2861     return FALSE;
2862
2863   linker_section_ptr->next = *ptr_linker_section_ptr;
2864   linker_section_ptr->addend = rel->r_addend;
2865   linker_section_ptr->lsect = lsect;
2866   *ptr_linker_section_ptr = linker_section_ptr;
2867
2868   linker_section_ptr->offset = lsect->section->size;
2869   lsect->section->size += 4;
2870
2871 #ifdef DEBUG
2872   fprintf (stderr,
2873            "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2874            lsect->name, (long) linker_section_ptr->offset,
2875            (long) lsect->section->size);
2876 #endif
2877
2878   return TRUE;
2879 }
2880
2881 static bfd_boolean
2882 update_local_sym_info (bfd *abfd,
2883                        Elf_Internal_Shdr *symtab_hdr,
2884                        unsigned long r_symndx,
2885                        int tls_type)
2886 {
2887   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
2888   char *local_got_tls_masks;
2889
2890   if (local_got_refcounts == NULL)
2891     {
2892       bfd_size_type size = symtab_hdr->sh_info;
2893
2894       size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
2895       local_got_refcounts = bfd_zalloc (abfd, size);
2896       if (local_got_refcounts == NULL)
2897         return FALSE;
2898       elf_local_got_refcounts (abfd) = local_got_refcounts;
2899     }
2900
2901   local_got_refcounts[r_symndx] += 1;
2902   local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2903   local_got_tls_masks[r_symndx] |= tls_type;
2904   return TRUE;
2905 }
2906
2907 static bfd_boolean
2908 update_plt_info (bfd *abfd, struct elf_link_hash_entry *h,
2909                  asection *sec, bfd_vma addend)
2910 {
2911   struct plt_entry *ent;
2912
2913   if (addend < 32768)
2914     sec = NULL;
2915   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
2916     if (ent->sec == sec && ent->addend == addend)
2917       break;
2918   if (ent == NULL)
2919     {
2920       bfd_size_type amt = sizeof (*ent);
2921       ent = bfd_alloc (abfd, amt);
2922       if (ent == NULL)
2923         return FALSE;
2924       ent->next = h->plt.plist;
2925       ent->sec = sec;
2926       ent->addend = addend;
2927       ent->plt.refcount = 0;
2928       h->plt.plist = ent;
2929     }
2930   ent->plt.refcount += 1;
2931   return TRUE;
2932 }
2933
2934 static struct plt_entry *
2935 find_plt_ent (struct elf_link_hash_entry *h, asection *sec, bfd_vma addend)
2936 {
2937   struct plt_entry *ent;
2938
2939   if (addend < 32768)
2940     sec = NULL;
2941   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
2942     if (ent->sec == sec && ent->addend == addend)
2943       break;
2944   return ent;
2945 }
2946
2947 static void
2948 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
2949 {
2950   (*_bfd_error_handler)
2951     (_("%B: relocation %s cannot be used when making a shared object"),
2952      abfd,
2953      ppc_elf_howto_table[r_type]->name);
2954   bfd_set_error (bfd_error_bad_value);
2955 }
2956
2957 /* Look through the relocs for a section during the first phase, and
2958    allocate space in the global offset table or procedure linkage
2959    table.  */
2960
2961 static bfd_boolean
2962 ppc_elf_check_relocs (bfd *abfd,
2963                       struct bfd_link_info *info,
2964                       asection *sec,
2965                       const Elf_Internal_Rela *relocs)
2966 {
2967   struct ppc_elf_link_hash_table *htab;
2968   Elf_Internal_Shdr *symtab_hdr;
2969   struct elf_link_hash_entry **sym_hashes;
2970   const Elf_Internal_Rela *rel;
2971   const Elf_Internal_Rela *rel_end;
2972   asection *got2, *sreloc;
2973
2974   if (info->relocatable)
2975     return TRUE;
2976
2977   /* Don't do anything special with non-loaded, non-alloced sections.
2978      In particular, any relocs in such sections should not affect GOT
2979      and PLT reference counting (ie. we don't allow them to create GOT
2980      or PLT entries), there's no possibility or desire to optimize TLS
2981      relocs, and there's not much point in propagating relocs to shared
2982      libs that the dynamic linker won't relocate.  */
2983   if ((sec->flags & SEC_ALLOC) == 0)
2984     return TRUE;
2985
2986 #ifdef DEBUG
2987   _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
2988                       sec, abfd);
2989 #endif
2990
2991   /* Initialize howto table if not already done.  */
2992   if (!ppc_elf_howto_table[R_PPC_ADDR32])
2993     ppc_elf_howto_init ();
2994
2995   htab = ppc_elf_hash_table (info);
2996   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2997   sym_hashes = elf_sym_hashes (abfd);
2998   got2 = bfd_get_section_by_name (abfd, ".got2");
2999   sreloc = NULL;
3000
3001   rel_end = relocs + sec->reloc_count;
3002   for (rel = relocs; rel < rel_end; rel++)
3003     {
3004       unsigned long r_symndx;
3005       enum elf_ppc_reloc_type r_type;
3006       struct elf_link_hash_entry *h;
3007       int tls_type = 0;
3008
3009       r_symndx = ELF32_R_SYM (rel->r_info);
3010       if (r_symndx < symtab_hdr->sh_info)
3011         h = NULL;
3012       else
3013         {
3014           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3015           while (h->root.type == bfd_link_hash_indirect
3016                  || h->root.type == bfd_link_hash_warning)
3017             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3018         }
3019
3020       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3021          This shows up in particular in an R_PPC_ADDR32 in the eabi
3022          startup code.  */
3023       if (h != NULL
3024           && htab->got == NULL
3025           && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3026         {
3027           if (htab->elf.dynobj == NULL)
3028             htab->elf.dynobj = abfd;
3029           if (!ppc_elf_create_got (htab->elf.dynobj, info))
3030             return FALSE;
3031           BFD_ASSERT (h == htab->elf.hgot);
3032         }
3033
3034       r_type = ELF32_R_TYPE (rel->r_info);
3035       switch (r_type)
3036         {
3037         case R_PPC_GOT_TLSLD16:
3038         case R_PPC_GOT_TLSLD16_LO:
3039         case R_PPC_GOT_TLSLD16_HI:
3040         case R_PPC_GOT_TLSLD16_HA:
3041           htab->tlsld_got.refcount += 1;
3042           tls_type = TLS_TLS | TLS_LD;
3043           goto dogottls;
3044
3045         case R_PPC_GOT_TLSGD16:
3046         case R_PPC_GOT_TLSGD16_LO:
3047         case R_PPC_GOT_TLSGD16_HI:
3048         case R_PPC_GOT_TLSGD16_HA:
3049           tls_type = TLS_TLS | TLS_GD;
3050           goto dogottls;
3051
3052         case R_PPC_GOT_TPREL16:
3053         case R_PPC_GOT_TPREL16_LO:
3054         case R_PPC_GOT_TPREL16_HI:
3055         case R_PPC_GOT_TPREL16_HA:
3056           if (info->shared)
3057             info->flags |= DF_STATIC_TLS;
3058           tls_type = TLS_TLS | TLS_TPREL;
3059           goto dogottls;
3060
3061         case R_PPC_GOT_DTPREL16:
3062         case R_PPC_GOT_DTPREL16_LO:
3063         case R_PPC_GOT_DTPREL16_HI:
3064         case R_PPC_GOT_DTPREL16_HA:
3065           tls_type = TLS_TLS | TLS_DTPREL;
3066         dogottls:
3067           sec->has_tls_reloc = 1;
3068           /* Fall thru */
3069
3070           /* GOT16 relocations */
3071         case R_PPC_GOT16:
3072         case R_PPC_GOT16_LO:
3073         case R_PPC_GOT16_HI:
3074         case R_PPC_GOT16_HA:
3075           /* This symbol requires a global offset table entry.  */
3076           if (htab->got == NULL)
3077             {
3078               if (htab->elf.dynobj == NULL)
3079                 htab->elf.dynobj = abfd;
3080               if (!ppc_elf_create_got (htab->elf.dynobj, info))
3081                 return FALSE;
3082             }
3083           if (h != NULL)
3084             {
3085               h->got.refcount += 1;
3086               ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3087             }
3088           else
3089             /* This is a global offset table entry for a local symbol.  */
3090             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3091               return FALSE;
3092           break;
3093
3094           /* Indirect .sdata relocation.  */
3095         case R_PPC_EMB_SDAI16:
3096           if (info->shared)
3097             {
3098               bad_shared_reloc (abfd, r_type);
3099               return FALSE;
3100             }
3101           if (htab->sdata[0].section == NULL
3102               && !ppc_elf_create_linker_section (abfd, info, 0,
3103                                                  &htab->sdata[0]))
3104             return FALSE;
3105           if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
3106                                                   h, rel))
3107             return FALSE;
3108           if (h != NULL)
3109             {
3110               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3111               h->non_got_ref = TRUE;
3112             }
3113           break;
3114
3115           /* Indirect .sdata2 relocation.  */
3116         case R_PPC_EMB_SDA2I16:
3117           if (info->shared)
3118             {
3119               bad_shared_reloc (abfd, r_type);
3120               return FALSE;
3121             }
3122           if (htab->sdata[1].section == NULL
3123               && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
3124                                                  &htab->sdata[1]))
3125             return FALSE;
3126           if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
3127                                                   h, rel))
3128             return FALSE;
3129           if (h != NULL)
3130             {
3131               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3132               h->non_got_ref = TRUE;
3133             }
3134           break;
3135
3136         case R_PPC_SDAREL16:
3137           if (info->shared)
3138             {
3139               bad_shared_reloc (abfd, r_type);
3140               return FALSE;
3141             }
3142           if (htab->sdata[0].sym == NULL
3143               && !create_sdata_sym (htab, &htab->sdata[0]))
3144             return FALSE;
3145           if (h != NULL)
3146             {
3147               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3148               h->non_got_ref = TRUE;
3149             }
3150           break;
3151
3152         case R_PPC_EMB_SDA2REL:
3153           if (info->shared)
3154             {
3155               bad_shared_reloc (abfd, r_type);
3156               return FALSE;
3157             }
3158           if (htab->sdata[1].sym == NULL
3159               && !create_sdata_sym (htab, &htab->sdata[1]))
3160             return FALSE;
3161           if (h != NULL)
3162             {
3163               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3164               h->non_got_ref = TRUE;
3165             }
3166           break;
3167
3168         case R_PPC_EMB_SDA21:
3169         case R_PPC_EMB_RELSDA:
3170           if (info->shared)
3171             {
3172               bad_shared_reloc (abfd, r_type);
3173               return FALSE;
3174             }
3175           if (htab->sdata[0].sym == NULL
3176               && !create_sdata_sym (htab, &htab->sdata[0]))
3177             return FALSE;
3178           if (htab->sdata[1].sym == NULL
3179               && !create_sdata_sym (htab, &htab->sdata[1]))
3180             return FALSE;
3181           if (h != NULL)
3182             {
3183               ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3184               h->non_got_ref = TRUE;
3185             }
3186           break;
3187
3188         case R_PPC_EMB_NADDR32:
3189         case R_PPC_EMB_NADDR16:
3190         case R_PPC_EMB_NADDR16_LO:
3191         case R_PPC_EMB_NADDR16_HI:
3192         case R_PPC_EMB_NADDR16_HA:
3193           if (info->shared)
3194             {
3195               bad_shared_reloc (abfd, r_type);
3196               return FALSE;
3197             }
3198           if (h != NULL)
3199             h->non_got_ref = TRUE;
3200           break;
3201
3202         case R_PPC_PLT32:
3203         case R_PPC_PLTREL24:
3204         case R_PPC_PLTREL32:
3205         case R_PPC_PLT16_LO:
3206         case R_PPC_PLT16_HI:
3207         case R_PPC_PLT16_HA:
3208 #ifdef DEBUG
3209           fprintf (stderr, "Reloc requires a PLT entry\n");
3210 #endif
3211           /* This symbol requires a procedure linkage table entry.  We
3212              actually build the entry in finish_dynamic_symbol,
3213              because this might be a case of linking PIC code without
3214              linking in any dynamic objects, in which case we don't
3215              need to generate a procedure linkage table after all.  */
3216
3217           if (h == NULL)
3218             {
3219               /* It does not make sense to have a procedure linkage
3220                  table entry for a local symbol.  */
3221               (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
3222                                        "local symbol"),
3223                                      abfd,
3224                                      sec,
3225                                      (long) rel->r_offset,
3226                                      ppc_elf_howto_table[r_type]->name);
3227               bfd_set_error (bfd_error_bad_value);
3228               return FALSE;
3229             }
3230           else
3231             {
3232               bfd_vma addend = r_type == R_PPC_PLTREL24 ? rel->r_addend : 0;
3233
3234               h->needs_plt = 1;
3235               if (!update_plt_info (abfd, h, got2, addend))
3236                 return FALSE;
3237             }
3238           break;
3239
3240           /* The following relocations don't need to propagate the
3241              relocation if linking a shared object since they are
3242              section relative.  */
3243         case R_PPC_SECTOFF:
3244         case R_PPC_SECTOFF_LO:
3245         case R_PPC_SECTOFF_HI:
3246         case R_PPC_SECTOFF_HA:
3247         case R_PPC_DTPREL16:
3248         case R_PPC_DTPREL16_LO:
3249         case R_PPC_DTPREL16_HI:
3250         case R_PPC_DTPREL16_HA:
3251         case R_PPC_TOC16:
3252           break;
3253
3254         case R_PPC_REL16:
3255         case R_PPC_REL16_LO:
3256         case R_PPC_REL16_HI:
3257         case R_PPC_REL16_HA:
3258           htab->can_use_new_plt = 1;
3259           break;
3260
3261           /* These are just markers.  */
3262         case R_PPC_TLS:
3263         case R_PPC_EMB_MRKREF:
3264         case R_PPC_NONE:
3265         case R_PPC_max:
3266           break;
3267
3268           /* These should only appear in dynamic objects.  */
3269         case R_PPC_COPY:
3270         case R_PPC_GLOB_DAT:
3271         case R_PPC_JMP_SLOT:
3272         case R_PPC_RELATIVE:
3273           break;
3274
3275           /* These aren't handled yet.  We'll report an error later.  */
3276         case R_PPC_ADDR30:
3277         case R_PPC_EMB_RELSEC16:
3278         case R_PPC_EMB_RELST_LO:
3279         case R_PPC_EMB_RELST_HI:
3280         case R_PPC_EMB_RELST_HA:
3281         case R_PPC_EMB_BIT_FLD:
3282           break;
3283
3284           /* This refers only to functions defined in the shared library.  */
3285         case R_PPC_LOCAL24PC:
3286           if (h && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
3287             htab->plt_type = PLT_OLD;
3288           break;
3289
3290           /* This relocation describes the C++ object vtable hierarchy.
3291              Reconstruct it for later use during GC.  */
3292         case R_PPC_GNU_VTINHERIT:
3293           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3294             return FALSE;
3295           break;
3296
3297           /* This relocation describes which C++ vtable entries are actually
3298              used.  Record for later use during GC.  */
3299         case R_PPC_GNU_VTENTRY:
3300           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3301             return FALSE;
3302           break;
3303
3304           /* We shouldn't really be seeing these.  */
3305         case R_PPC_TPREL32:
3306           if (info->shared)
3307             info->flags |= DF_STATIC_TLS;
3308           goto dodyn;
3309
3310           /* Nor these.  */
3311         case R_PPC_DTPMOD32:
3312         case R_PPC_DTPREL32:
3313           goto dodyn;
3314
3315         case R_PPC_TPREL16:
3316         case R_PPC_TPREL16_LO:
3317         case R_PPC_TPREL16_HI:
3318         case R_PPC_TPREL16_HA:
3319           if (info->shared)
3320             info->flags |= DF_STATIC_TLS;
3321           goto dodyn;
3322
3323         case R_PPC_REL32:
3324           if (h == NULL
3325               && got2 != NULL
3326               && (sec->flags & SEC_CODE) != 0
3327               && (info->shared || info->pie)
3328               && htab->plt_type == PLT_UNSET)
3329             {
3330               /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
3331                  the start of a function, which assembles to a REL32
3332                  reference to .got2.  If we detect one of these, then
3333                  force the old PLT layout because the linker cannot
3334                  reliably deduce the GOT pointer value needed for
3335                  PLT call stubs.  */
3336               asection *s;
3337
3338               s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
3339                                              r_symndx);
3340               if (s == got2)
3341                 htab->plt_type = PLT_OLD;
3342             }
3343           if (h == NULL || h == htab->elf.hgot)
3344             break;
3345           goto dodyn1;
3346
3347         case R_PPC_REL24:
3348         case R_PPC_REL14:
3349         case R_PPC_REL14_BRTAKEN:
3350         case R_PPC_REL14_BRNTAKEN:
3351           if (h == NULL)
3352             break;
3353           if (h == htab->elf.hgot)
3354             {
3355               if (htab->plt_type == PLT_UNSET)
3356                 htab->plt_type = PLT_OLD;
3357               break;
3358             }
3359           /* fall through */
3360
3361         case R_PPC_ADDR32:
3362         case R_PPC_ADDR24:
3363         case R_PPC_ADDR16:
3364         case R_PPC_ADDR16_LO:
3365         case R_PPC_ADDR16_HI:
3366         case R_PPC_ADDR16_HA:
3367         case R_PPC_ADDR14:
3368         case R_PPC_ADDR14_BRTAKEN:
3369         case R_PPC_ADDR14_BRNTAKEN:
3370         case R_PPC_UADDR32:
3371         case R_PPC_UADDR16:
3372         dodyn1:
3373           if (h != NULL && !info->shared)
3374             {
3375               /* We may need a plt entry if the symbol turns out to be
3376                  a function defined in a dynamic object.  */
3377               if (!update_plt_info (abfd, h, NULL, 0))
3378                 return FALSE;
3379
3380               /* We may need a copy reloc too.  */
3381               h->non_got_ref = 1;
3382             }
3383
3384         dodyn:
3385           /* If we are creating a shared library, and this is a reloc
3386              against a global symbol, or a non PC relative reloc
3387              against a local symbol, then we need to copy the reloc
3388              into the shared library.  However, if we are linking with
3389              -Bsymbolic, we do not need to copy a reloc against a
3390              global symbol which is defined in an object we are
3391              including in the link (i.e., DEF_REGULAR is set).  At
3392              this point we have not seen all the input files, so it is
3393              possible that DEF_REGULAR is not set now but will be set
3394              later (it is never cleared).  In case of a weak definition,
3395              DEF_REGULAR may be cleared later by a strong definition in
3396              a shared library.  We account for that possibility below by
3397              storing information in the dyn_relocs field of the hash
3398              table entry.  A similar situation occurs when creating
3399              shared libraries and symbol visibility changes render the
3400              symbol local.
3401
3402              If on the other hand, we are creating an executable, we
3403              may need to keep relocations for symbols satisfied by a
3404              dynamic library if we manage to avoid copy relocs for the
3405              symbol.  */
3406           if ((info->shared
3407                && (MUST_BE_DYN_RELOC (r_type)
3408                    || (h != NULL
3409                        && (! info->symbolic
3410                            || h->root.type == bfd_link_hash_defweak
3411                            || !h->def_regular))))
3412               || (ELIMINATE_COPY_RELOCS
3413                   && !info->shared
3414                   && h != NULL
3415                   && (h->root.type == bfd_link_hash_defweak
3416                       || !h->def_regular)))
3417             {
3418               struct ppc_elf_dyn_relocs *p;
3419               struct ppc_elf_dyn_relocs **head;
3420
3421 #ifdef DEBUG
3422               fprintf (stderr,
3423                        "ppc_elf_check_relocs needs to "
3424                        "create relocation for %s\n",
3425                        (h && h->root.root.string
3426                         ? h->root.root.string : "<unknown>"));
3427 #endif
3428               if (sreloc == NULL)
3429                 {
3430                   const char *name;
3431
3432                   name = (bfd_elf_string_from_elf_section
3433                           (abfd,
3434                            elf_elfheader (abfd)->e_shstrndx,
3435                            elf_section_data (sec)->rel_hdr.sh_name));
3436                   if (name == NULL)
3437                     return FALSE;
3438
3439                   BFD_ASSERT (CONST_STRNEQ (name, ".rela")
3440                               && strcmp (bfd_get_section_name (abfd, sec),
3441                                          name + 5) == 0);
3442
3443                   if (htab->elf.dynobj == NULL)
3444                     htab->elf.dynobj = abfd;
3445                   sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3446                   if (sreloc == NULL)
3447                     {
3448                       flagword flags;
3449
3450                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
3451                                | SEC_IN_MEMORY | SEC_LINKER_CREATED
3452                                | SEC_ALLOC | SEC_LOAD);
3453                       sreloc = bfd_make_section_with_flags (htab->elf.dynobj,
3454                                                             name,
3455                                                             flags);
3456                       if (sreloc == NULL
3457                           || ! bfd_set_section_alignment (htab->elf.dynobj,
3458                                                           sreloc, 2))
3459                         return FALSE;
3460                     }
3461                   elf_section_data (sec)->sreloc = sreloc;
3462                 }
3463
3464               /* If this is a global symbol, we count the number of
3465                  relocations we need for this symbol.  */
3466               if (h != NULL)
3467                 {
3468                   head = &ppc_elf_hash_entry (h)->dyn_relocs;
3469                 }
3470               else
3471                 {
3472                   /* Track dynamic relocs needed for local syms too.
3473                      We really need local syms available to do this
3474                      easily.  Oh well.  */
3475
3476                   asection *s;
3477                   void *vpp;
3478
3479                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3480                                                  sec, r_symndx);
3481                   if (s == NULL)
3482                     return FALSE;
3483
3484                   vpp = &elf_section_data (s)->local_dynrel;
3485                   head = (struct ppc_elf_dyn_relocs **) vpp;
3486                 }
3487
3488               p = *head;
3489               if (p == NULL || p->sec != sec)
3490                 {
3491                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3492                   if (p == NULL)
3493                     return FALSE;
3494                   p->next = *head;
3495                   *head = p;
3496                   p->sec = sec;
3497                   p->count = 0;
3498                   p->pc_count = 0;
3499                 }
3500
3501               p->count += 1;
3502               if (!MUST_BE_DYN_RELOC (r_type))
3503                 p->pc_count += 1;
3504             }
3505
3506           break;
3507         }
3508     }
3509
3510   return TRUE;
3511 }
3512 \f
3513 /* Merge backend specific data from an object file to the output
3514    object file when linking.  */
3515
3516 static bfd_boolean
3517 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3518 {
3519   flagword old_flags;
3520   flagword new_flags;
3521   bfd_boolean error;
3522
3523   if (!is_ppc_elf_target (ibfd->xvec)
3524       || !is_ppc_elf_target (obfd->xvec))
3525     return TRUE;
3526
3527   /* Check if we have the same endianess.  */
3528   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
3529     return FALSE;
3530
3531   new_flags = elf_elfheader (ibfd)->e_flags;
3532   old_flags = elf_elfheader (obfd)->e_flags;
3533   if (!elf_flags_init (obfd))
3534     {
3535       /* First call, no flags set.  */
3536       elf_flags_init (obfd) = TRUE;
3537       elf_elfheader (obfd)->e_flags = new_flags;
3538     }
3539
3540   /* Compatible flags are ok.  */
3541   else if (new_flags == old_flags)
3542     ;
3543
3544   /* Incompatible flags.  */
3545   else
3546     {
3547       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
3548          to be linked with either.  */
3549       error = FALSE;
3550       if ((new_flags & EF_PPC_RELOCATABLE) != 0
3551           && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
3552         {
3553           error = TRUE;
3554           (*_bfd_error_handler)
3555             (_("%B: compiled with -mrelocatable and linked with "
3556                "modules compiled normally"), ibfd);
3557         }
3558       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
3559                && (old_flags & EF_PPC_RELOCATABLE) != 0)
3560         {
3561           error = TRUE;
3562           (*_bfd_error_handler)
3563             (_("%B: compiled normally and linked with "
3564                "modules compiled with -mrelocatable"), ibfd);
3565         }
3566
3567       /* The output is -mrelocatable-lib iff both the input files are.  */
3568       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
3569         elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
3570
3571       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
3572          but each input file is either -mrelocatable or -mrelocatable-lib.  */
3573       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
3574           && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
3575           && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
3576         elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
3577
3578       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
3579          any module uses it.  */
3580       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
3581
3582       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3583       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
3584
3585       /* Warn about any other mismatches.  */
3586       if (new_flags != old_flags)
3587         {
3588           error = TRUE;
3589           (*_bfd_error_handler)
3590             (_("%B: uses different e_flags (0x%lx) fields "
3591                "than previous modules (0x%lx)"),
3592              ibfd, (long) new_flags, (long) old_flags);
3593         }
3594
3595       if (error)
3596         {
3597           bfd_set_error (bfd_error_bad_value);
3598           return FALSE;
3599         }
3600     }
3601
3602   return TRUE;
3603 }
3604 \f
3605 /* Choose which PLT scheme to use, and set .plt flags appropriately.
3606    Returns -1 on error, 0 for old PLT, 1 for new PLT.  */
3607 int
3608 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
3609                            struct bfd_link_info *info,
3610                            int force_old_plt,
3611                            int emit_stub_syms)
3612 {
3613   struct ppc_elf_link_hash_table *htab;
3614   flagword flags;
3615
3616   htab = ppc_elf_hash_table (info);
3617
3618   if (htab->plt_type == PLT_UNSET)
3619     htab->plt_type = (force_old_plt || !htab->can_use_new_plt
3620                       ? PLT_OLD : PLT_NEW);
3621
3622   htab->emit_stub_syms = emit_stub_syms;
3623
3624   BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
3625
3626   if (htab->plt_type == PLT_NEW)
3627     {
3628       flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3629                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3630
3631       /* The new PLT is a loaded section.  */
3632       if (htab->plt != NULL
3633           && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
3634         return -1;
3635
3636       /* The new GOT is not executable.  */
3637       if (htab->got != NULL
3638           && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
3639         return -1;
3640     }
3641   else
3642     {
3643       /* Stop an unused .glink section from affecting .text alignment.  */
3644       if (htab->glink != NULL
3645           && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
3646         return -1;
3647     }
3648   return htab->plt_type == PLT_NEW;
3649 }
3650 \f
3651 /* Return the section that should be marked against GC for a given
3652    relocation.  */
3653
3654 static asection *
3655 ppc_elf_gc_mark_hook (asection *sec,
3656                       struct bfd_link_info *info ATTRIBUTE_UNUSED,
3657                       Elf_Internal_Rela *rel,
3658                       struct elf_link_hash_entry *h,
3659                       Elf_Internal_Sym *sym)
3660 {
3661   if (h != NULL)
3662     {
3663       switch (ELF32_R_TYPE (rel->r_info))
3664         {
3665         case R_PPC_GNU_VTINHERIT:
3666         case R_PPC_GNU_VTENTRY:
3667           break;
3668
3669         default:
3670           switch (h->root.type)
3671             {
3672             case bfd_link_hash_defined:
3673             case bfd_link_hash_defweak:
3674               return h->root.u.def.section;
3675
3676             case bfd_link_hash_common:
3677               return h->root.u.c.p->section;
3678
3679             default:
3680               break;
3681             }
3682         }
3683     }
3684   else
3685     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3686
3687   return NULL;
3688 }
3689
3690 /* Update the got, plt and dynamic reloc reference counts for the
3691    section being removed.  */
3692
3693 static bfd_boolean
3694 ppc_elf_gc_sweep_hook (bfd *abfd,
3695                        struct bfd_link_info *info,
3696                        asection *sec,
3697                        const Elf_Internal_Rela *relocs)
3698 {
3699   struct ppc_elf_link_hash_table *htab;
3700   Elf_Internal_Shdr *symtab_hdr;
3701   struct elf_link_hash_entry **sym_hashes;
3702   bfd_signed_vma *local_got_refcounts;
3703   const Elf_Internal_Rela *rel, *relend;
3704   asection *got2;
3705
3706   if ((sec->flags & SEC_ALLOC) == 0)
3707     return TRUE;
3708
3709   elf_section_data (sec)->local_dynrel = NULL;
3710
3711   htab = ppc_elf_hash_table (info);
3712   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3713   sym_hashes = elf_sym_hashes (abfd);
3714   local_got_refcounts = elf_local_got_refcounts (abfd);
3715   got2 = bfd_get_section_by_name (abfd, ".got2");
3716
3717   relend = relocs + sec->reloc_count;
3718   for (rel = relocs; rel < relend; rel++)
3719     {
3720       unsigned long r_symndx;
3721       enum elf_ppc_reloc_type r_type;
3722       struct elf_link_hash_entry *h = NULL;
3723
3724       r_symndx = ELF32_R_SYM (rel->r_info);
3725       if (r_symndx >= symtab_hdr->sh_info)
3726         {
3727           struct ppc_elf_dyn_relocs **pp, *p;
3728           struct ppc_elf_link_hash_entry *eh;
3729
3730           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3731           while (h->root.type == bfd_link_hash_indirect
3732                  || h->root.type == bfd_link_hash_warning)
3733             h = (struct elf_link_hash_entry *) h->root.u.i.link;
3734           eh = (struct ppc_elf_link_hash_entry *) h;
3735
3736           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3737             if (p->sec == sec)
3738               {
3739                 /* Everything must go for SEC.  */
3740                 *pp = p->next;
3741                 break;
3742               }
3743         }
3744
3745       r_type = ELF32_R_TYPE (rel->r_info);
3746       switch (r_type)
3747         {
3748         case R_PPC_GOT_TLSLD16:
3749         case R_PPC_GOT_TLSLD16_LO:
3750         case R_PPC_GOT_TLSLD16_HI:
3751         case R_PPC_GOT_TLSLD16_HA:
3752           htab->tlsld_got.refcount -= 1;
3753           /* Fall thru */
3754
3755         case R_PPC_GOT_TLSGD16:
3756         case R_PPC_GOT_TLSGD16_LO:
3757         case R_PPC_GOT_TLSGD16_HI:
3758         case R_PPC_GOT_TLSGD16_HA:
3759         case R_PPC_GOT_TPREL16:
3760         case R_PPC_GOT_TPREL16_LO:
3761         case R_PPC_GOT_TPREL16_HI:
3762         case R_PPC_GOT_TPREL16_HA:
3763         case R_PPC_GOT_DTPREL16:
3764         case R_PPC_GOT_DTPREL16_LO:
3765         case R_PPC_GOT_DTPREL16_HI:
3766         case R_PPC_GOT_DTPREL16_HA:
3767         case R_PPC_GOT16:
3768         case R_PPC_GOT16_LO:
3769         case R_PPC_GOT16_HI:
3770         case R_PPC_GOT16_HA:
3771           if (h != NULL)
3772             {
3773               if (h->got.refcount > 0)
3774                 h->got.refcount--;
3775             }
3776           else if (local_got_refcounts != NULL)
3777             {
3778               if (local_got_refcounts[r_symndx] > 0)
3779                 local_got_refcounts[r_symndx]--;
3780             }
3781           break;
3782
3783         case R_PPC_REL24:
3784         case R_PPC_REL14:
3785         case R_PPC_REL14_BRTAKEN:
3786         case R_PPC_REL14_BRNTAKEN:
3787         case R_PPC_REL32:
3788           if (h == NULL || h == htab->elf.hgot)
3789             break;
3790           /* Fall thru */
3791
3792         case R_PPC_ADDR32:
3793         case R_PPC_ADDR24:
3794         case R_PPC_ADDR16:
3795         case R_PPC_ADDR16_LO:
3796         case R_PPC_ADDR16_HI:
3797         case R_PPC_ADDR16_HA:
3798         case R_PPC_ADDR14:
3799         case R_PPC_ADDR14_BRTAKEN:
3800         case R_PPC_ADDR14_BRNTAKEN:
3801         case R_PPC_UADDR32:
3802         case R_PPC_UADDR16:
3803           if (info->shared)
3804             break;
3805
3806         case R_PPC_PLT32:
3807         case R_PPC_PLTREL24:
3808         case R_PPC_PLTREL32:
3809         case R_PPC_PLT16_LO:
3810         case R_PPC_PLT16_HI:
3811         case R_PPC_PLT16_HA:
3812           if (h != NULL)
3813             {
3814               bfd_vma addend = r_type == R_PPC_PLTREL24 ? rel->r_addend : 0;
3815               struct plt_entry *ent = find_plt_ent (h, got2, addend);
3816               if (ent->plt.refcount > 0)
3817                 ent->plt.refcount -= 1;
3818             }
3819           break;
3820
3821         default:
3822           break;
3823         }
3824     }
3825   return TRUE;
3826 }
3827 \f
3828 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
3829
3830 asection *
3831 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3832 {
3833   struct ppc_elf_link_hash_table *htab;
3834
3835   htab = ppc_elf_hash_table (info);
3836   if (htab->plt_type == PLT_NEW
3837       && htab->plt != NULL
3838       && htab->plt->output_section != NULL)
3839     {
3840       elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
3841       elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
3842     }
3843
3844   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3845                                              FALSE, FALSE, TRUE);
3846   return _bfd_elf_tls_setup (obfd, info);
3847 }
3848
3849 /* Run through all the TLS relocs looking for optimization
3850    opportunities.  */
3851
3852 bfd_boolean
3853 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
3854                       struct bfd_link_info *info)
3855 {
3856   bfd *ibfd;
3857   asection *sec;
3858   struct ppc_elf_link_hash_table *htab;
3859
3860   if (info->relocatable || info->shared)
3861     return TRUE;
3862
3863   htab = ppc_elf_hash_table (info);
3864   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3865     {
3866       Elf_Internal_Sym *locsyms = NULL;
3867       Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3868
3869       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3870         if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3871           {
3872             Elf_Internal_Rela *relstart, *rel, *relend;
3873             int expecting_tls_get_addr;
3874
3875             /* Read the relocations.  */
3876             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
3877                                                   info->keep_memory);
3878             if (relstart == NULL)
3879               return FALSE;
3880
3881             expecting_tls_get_addr = 0;
3882             relend = relstart + sec->reloc_count;
3883             for (rel = relstart; rel < relend; rel++)
3884               {
3885                 enum elf_ppc_reloc_type r_type;
3886                 unsigned long r_symndx;
3887                 struct elf_link_hash_entry *h = NULL;
3888                 char *tls_mask;
3889                 char tls_set, tls_clear;
3890                 bfd_boolean is_local;
3891
3892                 r_symndx = ELF32_R_SYM (rel->r_info);
3893                 if (r_symndx >= symtab_hdr->sh_info)
3894                   {
3895                     struct elf_link_hash_entry **sym_hashes;
3896
3897                     sym_hashes = elf_sym_hashes (ibfd);
3898                     h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3899                     while (h->root.type == bfd_link_hash_indirect
3900                            || h->root.type == bfd_link_hash_warning)
3901                       h = (struct elf_link_hash_entry *) h->root.u.i.link;
3902                   }
3903
3904                 is_local = FALSE;
3905                 if (h == NULL
3906                     || !h->def_dynamic)
3907                   is_local = TRUE;
3908
3909                 r_type = ELF32_R_TYPE (rel->r_info);
3910                 switch (r_type)
3911                   {
3912                   case R_PPC_GOT_TLSLD16:
3913                   case R_PPC_GOT_TLSLD16_LO:
3914                   case R_PPC_GOT_TLSLD16_HI:
3915                   case R_PPC_GOT_TLSLD16_HA:
3916                     /* These relocs should never be against a symbol
3917                        defined in a shared lib.  Leave them alone if
3918                        that turns out to be the case.  */
3919                     expecting_tls_get_addr = 0;
3920                     htab->tlsld_got.refcount -= 1;
3921                     if (!is_local)
3922                       continue;
3923
3924                     /* LD -> LE */
3925                     tls_set = 0;
3926                     tls_clear = TLS_LD;
3927                     expecting_tls_get_addr = 1;
3928                     break;
3929
3930                   case R_PPC_GOT_TLSGD16:
3931                   case R_PPC_GOT_TLSGD16_LO:
3932                   case R_PPC_GOT_TLSGD16_HI:
3933                   case R_PPC_GOT_TLSGD16_HA:
3934                     if (is_local)
3935                       /* GD -> LE */
3936                       tls_set = 0;
3937                     else
3938                       /* GD -> IE */
3939                       tls_set = TLS_TLS | TLS_TPRELGD;
3940                     tls_clear = TLS_GD;
3941                     expecting_tls_get_addr = 1;
3942                     break;
3943
3944                   case R_PPC_GOT_TPREL16:
3945                   case R_PPC_GOT_TPREL16_LO:
3946                   case R_PPC_GOT_TPREL16_HI:
3947                   case R_PPC_GOT_TPREL16_HA:
3948                     expecting_tls_get_addr = 0;
3949                     if (is_local)
3950                       {
3951                         /* IE -> LE */
3952                         tls_set = 0;
3953                         tls_clear = TLS_TPREL;
3954                         break;
3955                       }
3956                     else
3957                       continue;
3958
3959                   case R_PPC_REL14:
3960                   case R_PPC_REL14_BRTAKEN:
3961                   case R_PPC_REL14_BRNTAKEN:
3962                   case R_PPC_REL24:
3963                     if (expecting_tls_get_addr
3964                         && h != NULL
3965                         && h == htab->tls_get_addr)
3966                       {
3967                         struct plt_entry *ent = find_plt_ent (h, NULL, 0);
3968                         if (ent != NULL && ent->plt.refcount > 0)
3969                           ent->plt.refcount -= 1;
3970                       }
3971                     expecting_tls_get_addr = 0;
3972                     continue;
3973
3974                   default:
3975                     expecting_tls_get_addr = 0;
3976                     continue;
3977                   }
3978
3979                 if (h != NULL)
3980                   {
3981                     if (tls_set == 0)
3982                       {
3983                         /* We managed to get rid of a got entry.  */
3984                         if (h->got.refcount > 0)
3985                           h->got.refcount -= 1;
3986                       }
3987                     tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3988                   }
3989                 else
3990                   {
3991                     Elf_Internal_Sym *sym;
3992                     bfd_signed_vma *lgot_refs;
3993                     char *lgot_masks;
3994
3995                     if (locsyms == NULL)
3996                       {
3997                         locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3998                         if (locsyms == NULL)
3999                           locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4000                                                           symtab_hdr->sh_info,
4001                                                           0, NULL, NULL, NULL);
4002                         if (locsyms == NULL)
4003                           {
4004                             if (elf_section_data (sec)->relocs != relstart)
4005                               free (relstart);
4006                             return FALSE;
4007                           }
4008                       }
4009                     sym = locsyms + r_symndx;
4010                     lgot_refs = elf_local_got_refcounts (ibfd);
4011                     if (lgot_refs == NULL)
4012                       abort ();
4013                     if (tls_set == 0)
4014                       {
4015                         /* We managed to get rid of a got entry.  */
4016                         if (lgot_refs[r_symndx] > 0)
4017                           lgot_refs[r_symndx] -= 1;
4018                       }
4019                     lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
4020                     tls_mask = &lgot_masks[r_symndx];
4021                   }
4022
4023                 *tls_mask |= tls_set;
4024                 *tls_mask &= ~tls_clear;
4025               }
4026
4027             if (elf_section_data (sec)->relocs != relstart)
4028               free (relstart);
4029           }
4030
4031       if (locsyms != NULL
4032           && (symtab_hdr->contents != (unsigned char *) locsyms))
4033         {
4034           if (!info->keep_memory)
4035             free (locsyms);
4036           else
4037             symtab_hdr->contents = (unsigned char *) locsyms;
4038         }
4039     }
4040   return TRUE;
4041 }
4042 \f
4043 /* Adjust a symbol defined by a dynamic object and referenced by a
4044    regular object.  The current definition is in some section of the
4045    dynamic object, but we're not including those sections.  We have to
4046    change the definition to something the rest of the link can
4047    understand.  */
4048
4049 static bfd_boolean
4050 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4051                                struct elf_link_hash_entry *h)
4052 {
4053   struct ppc_elf_link_hash_table *htab;
4054   asection *s;
4055   unsigned int power_of_two;
4056
4057 #ifdef DEBUG
4058   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
4059            h->root.root.string);
4060 #endif
4061
4062   /* Make sure we know what is going on here.  */
4063   htab = ppc_elf_hash_table (info);
4064   BFD_ASSERT (htab->elf.dynobj != NULL
4065               && (h->needs_plt
4066                   || h->u.weakdef != NULL
4067                   || (h->def_dynamic
4068                       && h->ref_regular
4069                       && !h->def_regular)));
4070
4071   /* Deal with function syms.  */
4072   if (h->type == STT_FUNC
4073       || h->needs_plt)
4074     {
4075       /* Clear procedure linkage table information for any symbol that
4076          won't need a .plt entry.  */
4077       struct plt_entry *ent;
4078       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4079         if (ent->plt.refcount > 0)
4080           break;
4081       if (ent == NULL
4082           || SYMBOL_CALLS_LOCAL (info, h)
4083           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4084               && h->root.type == bfd_link_hash_undefweak))
4085         {
4086           /* A PLT entry is not required/allowed when:
4087
4088              1. We are not using ld.so; because then the PLT entry
4089              can't be set up, so we can't use one.  In this case,
4090              ppc_elf_adjust_dynamic_symbol won't even be called.
4091
4092              2. GC has rendered the entry unused.
4093
4094              3. We know for certain that a call to this symbol
4095              will go to this object, or will remain undefined.  */
4096           h->plt.plist = NULL;
4097           h->needs_plt = 0;
4098         }
4099       return TRUE;
4100     }
4101   else
4102     h->plt.plist = NULL;
4103
4104   /* If this is a weak symbol, and there is a real definition, the
4105      processor independent code will have arranged for us to see the
4106      real definition first, and we can just use the same value.  */
4107   if (h->u.weakdef != NULL)
4108     {
4109       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4110                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
4111       h->root.u.def.section = h->u.weakdef->root.u.def.section;
4112       h->root.u.def.value = h->u.weakdef->root.u.def.value;
4113       if (ELIMINATE_COPY_RELOCS)
4114         h->non_got_ref = h->u.weakdef->non_got_ref;
4115       return TRUE;
4116     }
4117
4118   /* This is a reference to a symbol defined by a dynamic object which
4119      is not a function.  */
4120
4121   /* If we are creating a shared library, we must presume that the
4122      only references to the symbol are via the global offset table.
4123      For such cases we need not do anything here; the relocations will
4124      be handled correctly by relocate_section.  */
4125   if (info->shared)
4126     return TRUE;
4127
4128   /* If there are no references to this symbol that do not use the
4129      GOT, we don't need to generate a copy reloc.  */
4130   if (!h->non_got_ref)
4131     return TRUE;
4132
4133    /* If we didn't find any dynamic relocs in read-only sections, then we'll
4134       be keeping the dynamic relocs and avoiding the copy reloc.  We can't
4135       do this if there are any small data relocations.  */
4136   if (ELIMINATE_COPY_RELOCS
4137       && !ppc_elf_hash_entry (h)->has_sda_refs)
4138     {
4139       struct ppc_elf_dyn_relocs *p;
4140       for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4141         {
4142           s = p->sec->output_section;
4143           if (s != NULL && (s->flags & SEC_READONLY) != 0)
4144             break;
4145         }
4146
4147       if (p == NULL)
4148         {
4149           h->non_got_ref = 0;
4150           return TRUE;
4151         }
4152     }
4153
4154   if (h->size == 0)
4155     {
4156       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4157                              h->root.root.string);
4158       return TRUE;
4159     }
4160
4161   /* We must allocate the symbol in our .dynbss section, which will
4162      become part of the .bss section of the executable.  There will be
4163      an entry for this symbol in the .dynsym section.  The dynamic
4164      object will contain position independent code, so all references
4165      from the dynamic object to this symbol will go through the global
4166      offset table.  The dynamic linker will use the .dynsym entry to
4167      determine the address it must put in the global offset table, so
4168      both the dynamic object and the regular object will refer to the
4169      same memory location for the variable.
4170
4171      Of course, if the symbol is referenced using SDAREL relocs, we
4172      must instead allocate it in .sbss.  */
4173
4174   if (ppc_elf_hash_entry (h)->has_sda_refs)
4175     s = htab->dynsbss;
4176   else
4177     s = htab->dynbss;
4178   BFD_ASSERT (s != NULL);
4179
4180   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
4181      copy the initial value out of the dynamic object and into the
4182      runtime process image.  We need to remember the offset into the
4183      .rela.bss section we are going to use.  */
4184   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4185     {
4186       asection *srel;
4187
4188       if (ppc_elf_hash_entry (h)->has_sda_refs)
4189         srel = htab->relsbss;
4190       else
4191         srel = htab->relbss;
4192       BFD_ASSERT (srel != NULL);
4193       srel->size += sizeof (Elf32_External_Rela);
4194       h->needs_copy = 1;
4195     }
4196
4197   /* We need to figure out the alignment required for this symbol.  I
4198      have no idea how ELF linkers handle this.  */
4199   power_of_two = bfd_log2 (h->size);
4200   if (power_of_two > 4)
4201     power_of_two = 4;
4202
4203   /* Apply the required alignment.  */
4204   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4205   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
4206     {
4207       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
4208         return FALSE;
4209     }
4210
4211   /* Define the symbol as being at this point in the section.  */
4212   h->root.u.def.section = s;
4213   h->root.u.def.value = s->size;
4214
4215   /* Increment the section size to make room for the symbol.  */
4216   s->size += h->size;
4217
4218   return TRUE;
4219 }
4220 \f
4221 /* Generate a symbol to mark plt call stubs.  For non-PIC code the sym is
4222    xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
4223    specifying the addend on the plt relocation.  For -fpic code, the sym
4224    is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
4225    xxxxxxxx.got2.plt_pic32.<callee>.  */
4226
4227 static bfd_boolean
4228 add_stub_sym (struct plt_entry *ent,
4229               struct elf_link_hash_entry *h,
4230               struct bfd_link_info *info)
4231 {
4232   struct elf_link_hash_entry *sh;
4233   size_t len1, len2, len3;
4234   char *name;
4235   const char *stub;
4236   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
4237
4238   if (info->shared || info->pie)
4239     stub = ".plt_pic32.";
4240   else
4241     stub = ".plt_call32.";
4242
4243   len1 = strlen (h->root.root.string);
4244   len2 = strlen (stub);
4245   len3 = 0;
4246   if (ent->sec)
4247     len3 = strlen (ent->sec->name);
4248   name = bfd_malloc (len1 + len2 + len3 + 9);
4249   if (name == NULL)
4250     return FALSE;
4251   sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
4252   if (ent->sec)
4253     memcpy (name + 8, ent->sec->name, len3);
4254   memcpy (name + 8 + len3, stub, len2);
4255   memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
4256   sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
4257   if (sh == NULL)
4258     return FALSE;
4259   if (sh->root.type == bfd_link_hash_new)
4260     {
4261       sh->root.type = bfd_link_hash_defined;
4262       sh->root.u.def.section = htab->glink;
4263       sh->root.u.def.value = ent->glink_offset;
4264       sh->ref_regular = 1;
4265       sh->def_regular = 1;
4266       sh->ref_regular_nonweak = 1;
4267       sh->forced_local = 1;
4268       sh->non_elf = 0;
4269     }
4270   return TRUE;
4271 }
4272
4273 /* Allocate NEED contiguous space in .got, and return the offset.
4274    Handles allocation of the got header when crossing 32k.  */
4275
4276 static bfd_vma
4277 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
4278 {
4279   bfd_vma where;
4280   unsigned int max_before_header;
4281
4282   if (htab->plt_type == PLT_VXWORKS)
4283     {
4284       where = htab->got->size;
4285       htab->got->size += need;
4286     }
4287   else
4288     {
4289       max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
4290       if (need <= htab->got_gap)
4291         {
4292           where = max_before_header - htab->got_gap;
4293           htab->got_gap -= need;
4294         }
4295       else
4296         {
4297           if (htab->got->size + need > max_before_header
4298               && htab->got->size <= max_before_header)
4299             {
4300               htab->got_gap = max_before_header - htab->got->size;
4301               htab->got->size = max_before_header + htab->got_header_size;
4302             }
4303           where = htab->got->size;
4304           htab->got->size += need;
4305         }
4306     }
4307   return where;
4308 }
4309
4310 /* Allocate space in associated reloc sections for dynamic relocs.  */
4311
4312 static bfd_boolean
4313 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4314 {
4315   struct bfd_link_info *info = inf;
4316   struct ppc_elf_link_hash_entry *eh;
4317   struct ppc_elf_link_hash_table *htab;
4318   struct ppc_elf_dyn_relocs *p;
4319
4320   if (h->root.type == bfd_link_hash_indirect)
4321     return TRUE;
4322
4323   if (h->root.type == bfd_link_hash_warning)
4324     /* When warning symbols are created, they **replace** the "real"
4325        entry in the hash table, thus we never get to see the real
4326        symbol in a hash traversal.  So look at it now.  */
4327     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4328
4329   htab = ppc_elf_hash_table (info);
4330   if (htab->elf.dynamic_sections_created)
4331     {
4332       struct plt_entry *ent;
4333       bfd_boolean doneone = FALSE;
4334       bfd_vma plt_offset = 0, glink_offset = 0;
4335
4336       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4337         if (ent->plt.refcount > 0)
4338           {
4339             /* Make sure this symbol is output as a dynamic symbol.  */
4340             if (h->dynindx == -1
4341                 && !h->forced_local)
4342               {
4343                 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4344                   return FALSE;
4345               }
4346
4347             if (info->shared
4348                 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
4349               {
4350                 asection *s = htab->plt;
4351
4352                 if (htab->plt_type == PLT_NEW)
4353                   {
4354                     if (!doneone)
4355                       {
4356                         plt_offset = s->size;
4357                         s->size += 4;
4358                       }
4359                     ent->plt.offset = plt_offset;
4360
4361                     s = htab->glink;
4362                     if (!doneone || info->shared || info->pie)
4363                       {
4364                         glink_offset = s->size;
4365                         s->size += GLINK_ENTRY_SIZE;
4366                       }
4367                     if (!doneone
4368                         && !info->shared
4369                         && !h->def_regular)
4370                       {
4371                         h->root.u.def.section = s;
4372                         h->root.u.def.value = glink_offset;
4373                       }
4374                     ent->glink_offset = glink_offset;
4375
4376                     if (htab->emit_stub_syms
4377                         && !add_stub_sym (ent, h, info))
4378                       return FALSE;
4379                   }
4380                 else
4381                   {
4382                     if (!doneone)
4383                       {
4384                         /* If this is the first .plt entry, make room
4385                            for the special first entry.  */
4386                         if (s->size == 0)
4387                           s->size += htab->plt_initial_entry_size;
4388
4389                         /* The PowerPC PLT is actually composed of two
4390                            parts, the first part is 2 words (for a load
4391                            and a jump), and then there is a remaining
4392                            word available at the end.  */
4393                         plt_offset = (htab->plt_initial_entry_size
4394                                       + (htab->plt_slot_size
4395                                          * ((s->size
4396                                              - htab->plt_initial_entry_size)
4397                                             / htab->plt_entry_size)));
4398
4399                         /* If this symbol is not defined in a regular
4400                            file, and we are not generating a shared
4401                            library, then set the symbol to this location
4402                            in the .plt.  This is required to make
4403                            function pointers compare as equal between
4404                            the normal executable and the shared library.  */
4405                         if (! info->shared
4406                             && !h->def_regular)
4407                           {
4408                             h->root.u.def.section = s;
4409                             h->root.u.def.value = plt_offset;
4410                           }
4411
4412                         /* Make room for this entry.  */
4413                         s->size += htab->plt_entry_size;
4414                         /* After the 8192nd entry, room for two entries
4415                            is allocated.  */
4416                         if (htab->plt_type == PLT_OLD
4417                             && (s->size - htab->plt_initial_entry_size)
4418                                 / htab->plt_entry_size
4419                                > PLT_NUM_SINGLE_ENTRIES)
4420                           s->size += htab->plt_entry_size;
4421                       }
4422                     ent->plt.offset = plt_offset;
4423                   }
4424
4425                 /* We also need to make an entry in the .rela.plt section.  */
4426                 if (!doneone)
4427                   {
4428                     htab->relplt->size += sizeof (Elf32_External_Rela);
4429
4430                     if (htab->plt_type == PLT_VXWORKS)
4431                       {
4432                         /* Allocate space for the unloaded relocations.  */
4433                         if (!info->shared)
4434                           {
4435                             if (ent->plt.offset
4436                                 == (bfd_vma) htab->plt_initial_entry_size)
4437                               {
4438                                 htab->srelplt2->size
4439                                   += sizeof (Elf32_External_Rela)
4440                                       * VXWORKS_PLTRESOLVE_RELOCS;
4441                               }
4442
4443                             htab->srelplt2->size
4444                               += sizeof (Elf32_External_Rela)
4445                                   * VXWORKS_PLT_NON_JMP_SLOT_RELOCS;
4446                           }
4447
4448                         /* Every PLT entry has an associated GOT entry in
4449                            .got.plt.  */
4450                         htab->sgotplt->size += 4;
4451                       }
4452                     doneone = TRUE;
4453                   }
4454               }
4455             else
4456               ent->plt.offset = (bfd_vma) -1;
4457
4458             if (!doneone)
4459               {
4460                 h->plt.plist = NULL;
4461                 h->needs_plt = 0;
4462               }
4463           }
4464     }
4465   else
4466     {
4467       h->plt.plist = NULL;
4468       h->needs_plt = 0;
4469     }
4470
4471   eh = (struct ppc_elf_link_hash_entry *) h;
4472   if (eh->elf.got.refcount > 0)
4473     {
4474       /* Make sure this symbol is output as a dynamic symbol.  */
4475       if (eh->elf.dynindx == -1
4476           && !eh->elf.forced_local)
4477         {
4478           if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
4479             return FALSE;
4480         }
4481
4482       if (eh->tls_mask == (TLS_TLS | TLS_LD)
4483           && !eh->elf.def_dynamic)
4484         /* If just an LD reloc, we'll just use htab->tlsld_got.offset.  */
4485         eh->elf.got.offset = (bfd_vma) -1;
4486       else
4487         {
4488           bfd_boolean dyn;
4489           unsigned int need = 0;
4490           if ((eh->tls_mask & TLS_TLS) != 0)
4491             {
4492               if ((eh->tls_mask & TLS_LD) != 0)
4493                 need += 8;
4494               if ((eh->tls_mask & TLS_GD) != 0)
4495                 need += 8;
4496               if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
4497                 need += 4;
4498               if ((eh->tls_mask & TLS_DTPREL) != 0)
4499                 need += 4;
4500             }
4501           else
4502             need += 4;
4503           eh->elf.got.offset = allocate_got (htab, need);
4504           dyn = htab->elf.dynamic_sections_created;
4505           if ((info->shared
4506                || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
4507               && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
4508                   || eh->elf.root.type != bfd_link_hash_undefweak))
4509             {
4510               /* All the entries we allocated need relocs.
4511                  Except LD only needs one.  */
4512               if ((eh->tls_mask & TLS_LD) != 0)
4513                 need -= 4;
4514               htab->relgot->size += need * (sizeof (Elf32_External_Rela) / 4);
4515             }
4516         }
4517     }
4518   else
4519     eh->elf.got.offset = (bfd_vma) -1;
4520
4521   if (eh->dyn_relocs == NULL)
4522     return TRUE;
4523
4524   /* In the shared -Bsymbolic case, discard space allocated for
4525      dynamic pc-relative relocs against symbols which turn out to be
4526      defined in regular objects.  For the normal shared case, discard
4527      space for relocs that have become local due to symbol visibility
4528      changes.  */
4529
4530   if (info->shared)
4531     {
4532       /* Relocs that use pc_count are those that appear on a call insn,
4533          or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
4534          generated via assembly.  We want calls to protected symbols to
4535          resolve directly to the function rather than going via the plt.
4536          If people want function pointer comparisons to work as expected
4537          then they should avoid writing weird assembly.  */
4538       if (SYMBOL_CALLS_LOCAL (info, h))
4539         {
4540           struct ppc_elf_dyn_relocs **pp;
4541
4542           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4543             {
4544               p->count -= p->pc_count;
4545               p->pc_count = 0;
4546               if (p->count == 0)
4547                 *pp = p->next;
4548               else
4549                 pp = &p->next;
4550             }
4551         }
4552
4553       /* Also discard relocs on undefined weak syms with non-default
4554          visibility.  */
4555       if (eh->dyn_relocs != NULL
4556           && h->root.type == bfd_link_hash_undefweak)
4557         {
4558           if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4559             eh->dyn_relocs = NULL;
4560
4561           /* Make sure undefined weak symbols are output as a dynamic
4562              symbol in PIEs.  */
4563           else if (h->dynindx == -1
4564                    && !h->forced_local)
4565             {
4566               if (! bfd_elf_link_record_dynamic_symbol (info, h))
4567                 return FALSE;
4568             }
4569         }
4570     }
4571   else if (ELIMINATE_COPY_RELOCS)
4572     {
4573       /* For the non-shared case, discard space for relocs against
4574          symbols which turn out to need copy relocs or are not
4575          dynamic.  */
4576
4577       if (!h->non_got_ref
4578           && h->def_dynamic
4579           && !h->def_regular)
4580         {
4581           /* Make sure this symbol is output as a dynamic symbol.
4582              Undefined weak syms won't yet be marked as dynamic.  */
4583           if (h->dynindx == -1
4584               && !h->forced_local)
4585             {
4586               if (! bfd_elf_link_record_dynamic_symbol (info, h))
4587                 return FALSE;
4588             }
4589
4590           /* If that succeeded, we know we'll be keeping all the
4591              relocs.  */
4592           if (h->dynindx != -1)
4593             goto keep;
4594         }
4595
4596       eh->dyn_relocs = NULL;
4597
4598     keep: ;
4599     }
4600
4601   /* Finally, allocate space.  */
4602   for (p = eh->dyn_relocs; p != NULL; p = p->next)
4603     {
4604       asection *sreloc = elf_section_data (p->sec)->sreloc;
4605       sreloc->size += p->count * sizeof (Elf32_External_Rela);
4606     }
4607
4608   return TRUE;
4609 }
4610
4611 /* Find any dynamic relocs that apply to read-only sections.  */
4612
4613 static bfd_boolean
4614 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
4615 {
4616   struct ppc_elf_dyn_relocs *p;
4617
4618   if (h->root.type == bfd_link_hash_indirect)
4619     return TRUE;
4620
4621   if (h->root.type == bfd_link_hash_warning)
4622     h = (struct elf_link_hash_entry *) h->root.u.i.link;
4623
4624   for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4625     {
4626       asection *s = p->sec->output_section;
4627
4628       if (s != NULL
4629           && ((s->flags & (SEC_READONLY | SEC_ALLOC))
4630               == (SEC_READONLY | SEC_ALLOC)))
4631         {
4632           ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
4633
4634           /* Not an error, just cut short the traversal.  */
4635           return FALSE;
4636         }
4637     }
4638   return TRUE;
4639 }
4640
4641 /* Set the sizes of the dynamic sections.  */
4642
4643 static bfd_boolean
4644 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4645                                struct bfd_link_info *info)
4646 {
4647   struct ppc_elf_link_hash_table *htab;
4648   asection *s;
4649   bfd_boolean relocs;
4650   bfd *ibfd;
4651
4652 #ifdef DEBUG
4653   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
4654 #endif
4655
4656   htab = ppc_elf_hash_table (info);
4657   BFD_ASSERT (htab->elf.dynobj != NULL);
4658
4659   if (elf_hash_table (info)->dynamic_sections_created)
4660     {
4661       /* Set the contents of the .interp section to the interpreter.  */
4662       if (info->executable)
4663         {
4664           s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
4665           BFD_ASSERT (s != NULL);
4666           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4667           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4668         }
4669     }
4670
4671   if (htab->plt_type == PLT_OLD)
4672     htab->got_header_size = 16;
4673   else if (htab->plt_type == PLT_NEW)
4674     htab->got_header_size = 12;
4675
4676   /* Set up .got offsets for local syms, and space for local dynamic
4677      relocs.  */
4678   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4679     {
4680       bfd_signed_vma *local_got;
4681       bfd_signed_vma *end_local_got;
4682       char *lgot_masks;
4683       bfd_size_type locsymcount;
4684       Elf_Internal_Shdr *symtab_hdr;
4685
4686       if (!is_ppc_elf_target (ibfd->xvec))
4687         continue;
4688
4689       for (s = ibfd->sections; s != NULL; s = s->next)
4690         {
4691           struct ppc_elf_dyn_relocs *p;
4692
4693           for (p = ((struct ppc_elf_dyn_relocs *)
4694                     elf_section_data (s)->local_dynrel);
4695                p != NULL;
4696                p = p->next)
4697             {
4698               if (!bfd_is_abs_section (p->sec)
4699                   && bfd_is_abs_section (p->sec->output_section))
4700                 {
4701                   /* Input section has been discarded, either because
4702                      it is a copy of a linkonce section or due to
4703                      linker script /DISCARD/, so we'll be discarding
4704                      the relocs too.  */
4705                 }
4706               else if (p->count != 0)
4707                 {
4708                   elf_section_data (p->sec)->sreloc->size
4709                     += p->count * sizeof (Elf32_External_Rela);
4710                   if ((p->sec->output_section->flags
4711                        & (SEC_READONLY | SEC_ALLOC))
4712                       == (SEC_READONLY | SEC_ALLOC))
4713                     info->flags |= DF_TEXTREL;
4714                 }
4715             }
4716         }
4717
4718       local_got = elf_local_got_refcounts (ibfd);
4719       if (!local_got)
4720         continue;
4721
4722       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4723       locsymcount = symtab_hdr->sh_info;
4724       end_local_got = local_got + locsymcount;
4725       lgot_masks = (char *) end_local_got;
4726       for (; local_got < end_local_got; ++local_got, ++lgot_masks)
4727         if (*local_got > 0)
4728           {
4729             if (*lgot_masks == (TLS_TLS | TLS_LD))
4730               {
4731                 /* If just an LD reloc, we'll just use
4732                    htab->tlsld_got.offset.  */
4733                 htab->tlsld_got.refcount += 1;
4734                 *local_got = (bfd_vma) -1;
4735               }
4736             else
4737               {
4738                 unsigned int need = 0;
4739                 if ((*lgot_masks & TLS_TLS) != 0)
4740                   {
4741                     if ((*lgot_masks & TLS_GD) != 0)
4742                       need += 8;
4743                     if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
4744                       need += 4;
4745                     if ((*lgot_masks & TLS_DTPREL) != 0)
4746                       need += 4;
4747                   }
4748                 else
4749                   need += 4;
4750                 *local_got = allocate_got (htab, need);
4751                 if (info->shared)
4752                   htab->relgot->size += (need
4753                                          * (sizeof (Elf32_External_Rela) / 4));
4754               }
4755           }
4756         else
4757           *local_got = (bfd_vma) -1;
4758     }
4759
4760   if (htab->tlsld_got.refcount > 0)
4761     {
4762       htab->tlsld_got.offset = allocate_got (htab, 8);
4763       if (info->shared)
4764         htab->relgot->size += sizeof (Elf32_External_Rela);
4765     }
4766   else
4767     htab->tlsld_got.offset = (bfd_vma) -1;
4768
4769   /* Allocate space for global sym dynamic relocs.  */
4770   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
4771
4772   if (htab->got != NULL && htab->plt_type != PLT_VXWORKS)
4773     {
4774       unsigned int g_o_t = 32768;
4775
4776       /* If we haven't allocated the header, do so now.  When we get here,
4777          for old plt/got the got size will be 0 to 32764 (not allocated),
4778          or 32780 to 65536 (header allocated).  For new plt/got, the
4779          corresponding ranges are 0 to 32768 and 32780 to 65536.  */
4780       if (htab->got->size <= 32768)
4781         {
4782           g_o_t = htab->got->size;
4783           if (htab->plt_type == PLT_OLD)
4784             g_o_t += 4;
4785           htab->got->size += htab->got_header_size;
4786         }
4787
4788       htab->elf.hgot->root.u.def.value = g_o_t;
4789     }
4790
4791   if (htab->glink != NULL && htab->glink->size != 0)
4792     {
4793       htab->glink_pltresolve = htab->glink->size;
4794       /* Space for the branch table.  */
4795       htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
4796       /* Pad out to align the start of PLTresolve.  */
4797       htab->glink->size += -htab->glink->size & 15;
4798       htab->glink->size += GLINK_PLTRESOLVE;
4799
4800       if (htab->emit_stub_syms)
4801         {
4802           struct elf_link_hash_entry *sh;
4803           sh = elf_link_hash_lookup (&htab->elf, "__glink",
4804                                      TRUE, FALSE, FALSE);
4805           if (sh == NULL)
4806             return FALSE;
4807           if (sh->root.type == bfd_link_hash_new)
4808             {
4809               sh->root.type = bfd_link_hash_defined;
4810               sh->root.u.def.section = htab->glink;
4811               sh->root.u.def.value = htab->glink_pltresolve;
4812               sh->ref_regular = 1;
4813               sh->def_regular = 1;
4814               sh->ref_regular_nonweak = 1;
4815               sh->forced_local = 1;
4816               sh->non_elf = 0;
4817             }
4818           sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
4819                                      TRUE, FALSE, FALSE);
4820           if (sh == NULL)
4821             return FALSE;
4822           if (sh->root.type == bfd_link_hash_new)
4823             {
4824               sh->root.type = bfd_link_hash_defined;
4825               sh->root.u.def.section = htab->glink;
4826               sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
4827               sh->ref_regular = 1;
4828               sh->def_regular = 1;
4829               sh->ref_regular_nonweak = 1;
4830               sh->forced_local = 1;
4831               sh->non_elf = 0;
4832             }
4833         }
4834     }
4835
4836   /* We've now determined the sizes of the various dynamic sections.
4837      Allocate memory for them.  */
4838   relocs = FALSE;
4839   for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
4840     {
4841       bfd_boolean strip_section = TRUE;
4842
4843       if ((s->flags & SEC_LINKER_CREATED) == 0)
4844         continue;
4845
4846       if (s == htab->plt
4847           || s == htab->glink
4848           || s == htab->got
4849           || s == htab->sgotplt
4850           || s == htab->sbss
4851           || s == htab->dynbss
4852           || s == htab->dynsbss)
4853         {
4854           /* We'd like to strip these sections if they aren't needed, but if
4855              we've exported dynamic symbols from them we must leave them.
4856              It's too late to tell BFD to get rid of the symbols.  */
4857           if ((s == htab->plt || s == htab->got) && htab->elf.hplt != NULL)
4858             strip_section = FALSE;
4859           /* Strip this section if we don't need it; see the
4860              comment below.  */
4861         }
4862       else if (s == htab->sdata[0].section
4863                || s == htab->sdata[1].section)
4864         {
4865           /* Strip these too.  */
4866         }
4867       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
4868         {
4869           if (s->size != 0)
4870             {
4871               /* Remember whether there are any relocation sections.  */
4872               relocs = TRUE;
4873
4874               /* We use the reloc_count field as a counter if we need
4875                  to copy relocs into the output file.  */
4876               s->reloc_count = 0;
4877             }
4878         }
4879       else
4880         {
4881           /* It's not one of our sections, so don't allocate space.  */
4882           continue;
4883         }
4884
4885       if (s->size == 0 && strip_section)
4886         {
4887           /* If we don't need this section, strip it from the
4888              output file.  This is mostly to handle .rela.bss and
4889              .rela.plt.  We must create both sections in
4890              create_dynamic_sections, because they must be created
4891              before the linker maps input sections to output
4892              sections.  The linker does that before
4893              adjust_dynamic_symbol is called, and it is that
4894              function which decides whether anything needs to go
4895              into these sections.  */
4896           s->flags |= SEC_EXCLUDE;
4897           continue;
4898         }
4899
4900       if ((s->flags & SEC_HAS_CONTENTS) == 0)
4901         continue;
4902
4903       /* Allocate memory for the section contents.  */
4904       s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
4905       if (s->contents == NULL)
4906         return FALSE;
4907     }
4908
4909   if (htab->elf.dynamic_sections_created)
4910     {
4911       /* Add some entries to the .dynamic section.  We fill in the
4912          values later, in ppc_elf_finish_dynamic_sections, but we
4913          must add the entries now so that we get the correct size for
4914          the .dynamic section.  The DT_DEBUG entry is filled in by the
4915          dynamic linker and used by the debugger.  */
4916 #define add_dynamic_entry(TAG, VAL) \
4917   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4918
4919       if (info->executable)
4920         {
4921           if (!add_dynamic_entry (DT_DEBUG, 0))
4922             return FALSE;
4923         }
4924
4925       if (htab->plt != NULL && htab->plt->size != 0)
4926         {
4927           if (!add_dynamic_entry (DT_PLTGOT, 0)
4928               || !add_dynamic_entry (DT_PLTRELSZ, 0)
4929               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4930               || !add_dynamic_entry (DT_JMPREL, 0))
4931             return FALSE;
4932         }
4933
4934       if (htab->glink != NULL && htab->glink->size != 0)
4935         {
4936           if (!add_dynamic_entry (DT_PPC_GOT, 0))
4937             return FALSE;
4938         }
4939
4940       if (relocs)
4941         {
4942           if (!add_dynamic_entry (DT_RELA, 0)
4943               || !add_dynamic_entry (DT_RELASZ, 0)
4944               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4945             return FALSE;
4946         }
4947
4948       /* If any dynamic relocs apply to a read-only section, then we
4949          need a DT_TEXTREL entry.  */
4950       if ((info->flags & DF_TEXTREL) == 0)
4951         elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
4952                                 info);
4953
4954       if ((info->flags & DF_TEXTREL) != 0)
4955         {
4956           if (!add_dynamic_entry (DT_TEXTREL, 0))
4957             return FALSE;
4958         }
4959     }
4960 #undef add_dynamic_entry
4961
4962   return TRUE;
4963 }
4964 \f
4965 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
4966
4967 static const int shared_stub_entry[] =
4968   {
4969     0x7c0802a6, /* mflr 0 */
4970     0x429f0005, /* bcl 20, 31, .Lxxx */
4971     0x7d6802a6, /* mflr 11 */
4972     0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
4973     0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
4974     0x7c0803a6, /* mtlr 0 */
4975     0x7d6903a6, /* mtctr 11 */
4976     0x4e800420, /* bctr */
4977   };
4978
4979 static const int stub_entry[] =
4980   {
4981     0x3d600000, /* lis 11,xxx@ha */
4982     0x396b0000, /* addi 11,11,xxx@l */
4983     0x7d6903a6, /* mtctr 11 */
4984     0x4e800420, /* bctr */
4985   };
4986
4987 static bfd_boolean
4988 ppc_elf_relax_section (bfd *abfd,
4989                        asection *isec,
4990                        struct bfd_link_info *link_info,
4991                        bfd_boolean *again)
4992 {
4993   struct one_fixup
4994   {
4995     struct one_fixup *next;
4996     asection *tsec;
4997     bfd_vma toff;
4998     bfd_vma trampoff;
4999   };
5000
5001   Elf_Internal_Shdr *symtab_hdr;
5002   bfd_byte *contents = NULL;
5003   Elf_Internal_Sym *isymbuf = NULL;
5004   Elf_Internal_Rela *internal_relocs = NULL;
5005   Elf_Internal_Rela *irel, *irelend;
5006   struct one_fixup *fixups = NULL;
5007   bfd_boolean changed;
5008   struct ppc_elf_link_hash_table *htab;
5009   bfd_size_type trampoff;
5010   asection *got2;
5011
5012   *again = FALSE;
5013
5014   /* Nothing to do if there are no relocations, and no need to do
5015      anything with non-alloc sections.  */
5016   if ((isec->flags & SEC_ALLOC) == 0
5017       || (isec->flags & SEC_RELOC) == 0
5018       || isec->reloc_count == 0)
5019     return TRUE;
5020
5021   trampoff = (isec->size + 3) & (bfd_vma) -4;
5022   /* Space for a branch around any trampolines.  */
5023   trampoff += 4;
5024
5025   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5026
5027   /* Get a copy of the native relocations.  */
5028   internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
5029                                                link_info->keep_memory);
5030   if (internal_relocs == NULL)
5031     goto error_return;
5032
5033   htab = ppc_elf_hash_table (link_info);
5034   got2 = bfd_get_section_by_name (abfd, ".got2");
5035
5036   irelend = internal_relocs + isec->reloc_count;
5037   for (irel = internal_relocs; irel < irelend; irel++)
5038     {
5039       unsigned long r_type = ELF32_R_TYPE (irel->r_info);
5040       bfd_vma symaddr, reladdr, toff, roff;
5041       asection *tsec;
5042       struct one_fixup *f;
5043       size_t insn_offset = 0;
5044       bfd_vma max_branch_offset, val;
5045       bfd_byte *hit_addr;
5046       unsigned long t0;
5047       unsigned char sym_type;
5048
5049       switch (r_type)
5050         {
5051         case R_PPC_REL24:
5052         case R_PPC_LOCAL24PC:
5053         case R_PPC_PLTREL24:
5054           max_branch_offset = 1 << 25;
5055           break;
5056
5057         case R_PPC_REL14:
5058         case R_PPC_REL14_BRTAKEN:
5059         case R_PPC_REL14_BRNTAKEN:
5060           max_branch_offset = 1 << 15;
5061           break;
5062
5063         default:
5064           continue;
5065         }
5066
5067       /* Get the value of the symbol referred to by the reloc.  */
5068       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
5069         {
5070           /* A local symbol.  */
5071           Elf_Internal_Sym *isym;
5072
5073           /* Read this BFD's local symbols.  */
5074           if (isymbuf == NULL)
5075             {
5076               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5077               if (isymbuf == NULL)
5078                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
5079                                                 symtab_hdr->sh_info, 0,
5080                                                 NULL, NULL, NULL);
5081               if (isymbuf == 0)
5082                 goto error_return;
5083             }
5084           isym = isymbuf + ELF32_R_SYM (irel->r_info);
5085           if (isym->st_shndx == SHN_UNDEF)
5086             continue;   /* We can't do anything with undefined symbols.  */
5087           else if (isym->st_shndx == SHN_ABS)
5088             tsec = bfd_abs_section_ptr;
5089           else if (isym->st_shndx == SHN_COMMON)
5090             tsec = bfd_com_section_ptr;
5091           else
5092             tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5093
5094           toff = isym->st_value;
5095           sym_type = ELF_ST_TYPE (isym->st_info);
5096         }
5097       else
5098         {
5099           /* Global symbol handling.  */
5100           unsigned long indx;
5101           struct elf_link_hash_entry *h;
5102
5103           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
5104           h = elf_sym_hashes (abfd)[indx];
5105
5106           while (h->root.type == bfd_link_hash_indirect
5107                  || h->root.type == bfd_link_hash_warning)
5108             h = (struct elf_link_hash_entry *) h->root.u.i.link;
5109
5110           tsec = NULL;
5111           toff = 0;
5112           if (r_type == R_PPC_PLTREL24
5113               && htab->plt != NULL)
5114             {
5115               struct plt_entry *ent = find_plt_ent (h, got2, irel->r_addend);
5116
5117               if (ent != NULL)
5118                 {
5119                   if (htab->plt_type == PLT_NEW)
5120                     {
5121                       tsec = htab->glink;
5122                       toff = ent->glink_offset;
5123                     }
5124                   else
5125                     {
5126                       tsec = htab->plt;
5127                       toff = ent->plt.offset;
5128                     }
5129                 }
5130             }
5131           if (tsec != NULL)
5132             ;
5133           else if (h->root.type == bfd_link_hash_defined
5134                    || h->root.type == bfd_link_hash_defweak)
5135             {
5136               tsec = h->root.u.def.section;
5137               toff = h->root.u.def.value;
5138             }
5139           else
5140             continue;
5141
5142           sym_type = h->type;
5143         }
5144
5145       /* If the branch and target are in the same section, you have
5146          no hope of adding stubs.  We'll error out later should the
5147          branch overflow.  */
5148       if (tsec == isec)
5149         continue;
5150
5151       /* There probably isn't any reason to handle symbols in
5152          SEC_MERGE sections;  SEC_MERGE doesn't seem a likely
5153          attribute for a code section, and we are only looking at
5154          branches.  However, implement it correctly here as a
5155          reference for other target relax_section functions.  */
5156       if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
5157         {
5158           /* At this stage in linking, no SEC_MERGE symbol has been
5159              adjusted, so all references to such symbols need to be
5160              passed through _bfd_merged_section_offset.  (Later, in
5161              relocate_section, all SEC_MERGE symbols *except* for
5162              section symbols have been adjusted.)
5163
5164              gas may reduce relocations against symbols in SEC_MERGE
5165              sections to a relocation against the section symbol when
5166              the original addend was zero.  When the reloc is against
5167              a section symbol we should include the addend in the
5168              offset passed to _bfd_merged_section_offset, since the
5169              location of interest is the original symbol.  On the
5170              other hand, an access to "sym+addend" where "sym" is not
5171              a section symbol should not include the addend;  Such an
5172              access is presumed to be an offset from "sym";  The
5173              location of interest is just "sym".  */
5174           if (sym_type == STT_SECTION)
5175             toff += irel->r_addend;
5176
5177           toff = _bfd_merged_section_offset (abfd, &tsec,
5178                                              elf_section_data (tsec)->sec_info,
5179                                              toff);
5180
5181           if (sym_type != STT_SECTION)
5182             toff += irel->r_addend;
5183         }
5184       /* PLTREL24 addends are special.  */
5185       else if (r_type != R_PPC_PLTREL24)
5186         toff += irel->r_addend;
5187
5188       /* Attempted -shared link of non-pic code loses.  */
5189       if (tsec->output_section == NULL)
5190         continue;
5191
5192       symaddr = tsec->output_section->vma + tsec->output_offset + toff;
5193
5194       roff = irel->r_offset;
5195       reladdr = isec->output_section->vma + isec->output_offset + roff;
5196
5197       /* If the branch is in range, no need to do anything.  */
5198       if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
5199         continue;
5200
5201       /* Look for an existing fixup to this address.  */
5202       for (f = fixups; f ; f = f->next)
5203         if (f->tsec == tsec && f->toff == toff)
5204           break;
5205
5206       if (f == NULL)
5207         {
5208           size_t size;
5209           unsigned long stub_rtype;
5210
5211           val = trampoff - roff;
5212           if (val >= max_branch_offset)
5213             /* Oh dear, we can't reach a trampoline.  Don't try to add
5214                one.  We'll report an error later.  */
5215             continue;
5216
5217           if (link_info->shared)
5218             {
5219               size = 4 * ARRAY_SIZE (shared_stub_entry);
5220               insn_offset = 12;
5221               stub_rtype = R_PPC_RELAX32PC;
5222             }
5223           else
5224             {
5225               size = 4 * ARRAY_SIZE (stub_entry);
5226               insn_offset = 0;
5227               stub_rtype = R_PPC_RELAX32;
5228             }
5229
5230           if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
5231               != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
5232             abort ();
5233           if (tsec == htab->plt
5234               || tsec == htab->glink)
5235             stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
5236
5237           /* Hijack the old relocation.  Since we need two
5238              relocations for this use a "composite" reloc.  */
5239           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
5240                                        stub_rtype);
5241           irel->r_offset = trampoff + insn_offset;
5242
5243           /* Record the fixup so we don't do it again this section.  */
5244           f = bfd_malloc (sizeof (*f));
5245           f->next = fixups;
5246           f->tsec = tsec;
5247           f->toff = toff;
5248           f->trampoff = trampoff;
5249           fixups = f;
5250
5251           trampoff += size;
5252         }
5253       else
5254         {
5255           val = f->trampoff - roff;
5256           if (val >= max_branch_offset)
5257             continue;
5258
5259           /* Nop out the reloc, since we're finalizing things here.  */
5260           irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
5261         }
5262
5263       /* Get the section contents.  */
5264       if (contents == NULL)
5265         {
5266           /* Get cached copy if it exists.  */
5267           if (elf_section_data (isec)->this_hdr.contents != NULL)
5268             contents = elf_section_data (isec)->this_hdr.contents;
5269           else
5270             {
5271               /* Go get them off disk.  */
5272               if (!bfd_malloc_and_get_section (abfd, isec, &contents))
5273                 goto error_return;
5274             }
5275         }
5276
5277       /* Fix up the existing branch to hit the trampoline.  */
5278       hit_addr = contents + roff;
5279       switch (r_type)
5280         {
5281         case R_PPC_REL24:
5282         case R_PPC_LOCAL24PC:
5283         case R_PPC_PLTREL24:
5284           t0 = bfd_get_32 (abfd, hit_addr);
5285           t0 &= ~0x3fffffc;
5286           t0 |= val & 0x3fffffc;
5287           bfd_put_32 (abfd, t0, hit_addr);
5288           break;
5289
5290         case R_PPC_REL14:
5291         case R_PPC_REL14_BRTAKEN:
5292         case R_PPC_REL14_BRNTAKEN:
5293           t0 = bfd_get_32 (abfd, hit_addr);
5294           t0 &= ~0xfffc;
5295           t0 |= val & 0xfffc;
5296           bfd_put_32 (abfd, t0, hit_addr);
5297           break;
5298         }
5299     }
5300
5301   /* Write out the trampolines.  */
5302   changed = fixups != NULL;
5303   if (fixups != NULL)
5304     {
5305       const int *stub;
5306       bfd_byte *dest;
5307       bfd_vma val;
5308       int i, size;
5309
5310       do
5311         {
5312           struct one_fixup *f = fixups;
5313           fixups = fixups->next;
5314           free (f);
5315         }
5316       while (fixups);
5317
5318       contents = bfd_realloc (contents, trampoff);
5319       if (contents == NULL)
5320         goto error_return;
5321
5322       isec->size = (isec->size + 3) & (bfd_vma) -4;
5323       /* Branch around the trampolines.  */
5324       val = trampoff - isec->size + 0x48000000;
5325       dest = contents + isec->size;
5326       isec->size = trampoff;
5327       bfd_put_32 (abfd, val, dest);
5328       dest += 4;
5329
5330       if (link_info->shared)
5331         {
5332           stub = shared_stub_entry;
5333           size = ARRAY_SIZE (shared_stub_entry);
5334         }
5335       else
5336         {
5337           stub = stub_entry;
5338           size = ARRAY_SIZE (stub_entry);
5339         }
5340
5341       i = 0;
5342       while (dest < contents + trampoff)
5343         {
5344           bfd_put_32 (abfd, stub[i], dest);
5345           i++;
5346           if (i == size)
5347             i = 0;
5348           dest += 4;
5349         }
5350       BFD_ASSERT (i == 0);
5351     }
5352
5353   if (isymbuf != NULL
5354       && symtab_hdr->contents != (unsigned char *) isymbuf)
5355     {
5356       if (! link_info->keep_memory)
5357         free (isymbuf);
5358       else
5359         {
5360           /* Cache the symbols for elf_link_input_bfd.  */
5361           symtab_hdr->contents = (unsigned char *) isymbuf;
5362         }
5363     }
5364
5365   if (contents != NULL
5366       && elf_section_data (isec)->this_hdr.contents != contents)
5367     {
5368       if (!changed && !link_info->keep_memory)
5369         free (contents);
5370       else
5371         {
5372           /* Cache the section contents for elf_link_input_bfd.  */
5373           elf_section_data (isec)->this_hdr.contents = contents;
5374         }
5375     }
5376
5377   if (elf_section_data (isec)->relocs != internal_relocs)
5378     {
5379       if (!changed)
5380         free (internal_relocs);
5381       else
5382         elf_section_data (isec)->relocs = internal_relocs;
5383     }
5384
5385   *again = changed;
5386   return TRUE;
5387
5388  error_return:
5389   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
5390     free (isymbuf);
5391   if (contents != NULL
5392       && elf_section_data (isec)->this_hdr.contents != contents)
5393     free (contents);
5394   if (internal_relocs != NULL
5395       && elf_section_data (isec)->relocs != internal_relocs)
5396     free (internal_relocs);
5397   return FALSE;
5398 }
5399 \f
5400 /* What to do when ld finds relocations against symbols defined in
5401    discarded sections.  */
5402
5403 static unsigned int
5404 ppc_elf_action_discarded (asection *sec)
5405 {
5406   if (strcmp (".fixup", sec->name) == 0)
5407     return 0;
5408
5409   if (strcmp (".got2", sec->name) == 0)
5410     return 0;
5411
5412   return _bfd_elf_default_action_discarded (sec);
5413 }
5414 \f
5415 /* Fill in the address for a pointer generated in a linker section.  */
5416
5417 static bfd_vma
5418 elf_finish_pointer_linker_section (bfd *input_bfd,
5419                                    elf_linker_section_t *lsect,
5420                                    struct elf_link_hash_entry *h,
5421                                    bfd_vma relocation,
5422                                    const Elf_Internal_Rela *rel)
5423 {
5424   elf_linker_section_pointers_t *linker_section_ptr;
5425
5426   BFD_ASSERT (lsect != NULL);
5427
5428   if (h != NULL)
5429     {
5430       /* Handle global symbol.  */
5431       struct ppc_elf_link_hash_entry *eh;
5432
5433       eh = (struct ppc_elf_link_hash_entry *) h;
5434       BFD_ASSERT (eh->elf.def_regular);
5435       linker_section_ptr = eh->linker_section_pointer;
5436     }
5437   else
5438     {
5439       /* Handle local symbol.  */
5440       unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
5441
5442       BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
5443       linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
5444     }
5445
5446   linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
5447                                                         rel->r_addend,
5448                                                         lsect);
5449   BFD_ASSERT (linker_section_ptr != NULL);
5450
5451   /* Offset will always be a multiple of four, so use the bottom bit
5452      as a "written" flag.  */
5453   if ((linker_section_ptr->offset & 1) == 0)
5454     {
5455       bfd_put_32 (lsect->section->owner,
5456                   relocation + linker_section_ptr->addend,
5457                   lsect->section->contents + linker_section_ptr->offset);
5458       linker_section_ptr->offset += 1;
5459     }
5460
5461   relocation = (lsect->section->output_offset
5462                 + linker_section_ptr->offset - 1
5463                 - 0x8000);
5464
5465 #ifdef DEBUG
5466   fprintf (stderr,
5467            "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
5468            lsect->name, (long) relocation, (long) relocation);
5469 #endif
5470
5471   /* Subtract out the addend, because it will get added back in by the normal
5472      processing.  */
5473   return relocation - linker_section_ptr->addend;
5474 }
5475
5476 /* The RELOCATE_SECTION function is called by the ELF backend linker
5477    to handle the relocations for a section.
5478
5479    The relocs are always passed as Rela structures; if the section
5480    actually uses Rel structures, the r_addend field will always be
5481    zero.
5482
5483    This function is responsible for adjust the section contents as
5484    necessary, and (if using Rela relocs and generating a
5485    relocatable output file) adjusting the reloc addend as
5486    necessary.
5487
5488    This function does not have to worry about setting the reloc
5489    address or the reloc symbol index.
5490
5491    LOCAL_SYMS is a pointer to the swapped in local symbols.
5492
5493    LOCAL_SECTIONS is an array giving the section in the input file
5494    corresponding to the st_shndx field of each local symbol.
5495
5496    The global hash table entry for the global symbols can be found
5497    via elf_sym_hashes (input_bfd).
5498
5499    When generating relocatable output, this function must handle
5500    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
5501    going to be the section symbol corresponding to the output
5502    section, which means that the addend must be adjusted
5503    accordingly.  */
5504
5505 static bfd_boolean
5506 ppc_elf_relocate_section (bfd *output_bfd,
5507                           struct bfd_link_info *info,
5508                           bfd *input_bfd,
5509                           asection *input_section,
5510                           bfd_byte *contents,
5511                           Elf_Internal_Rela *relocs,
5512                           Elf_Internal_Sym *local_syms,
5513                           asection **local_sections)
5514 {
5515   Elf_Internal_Shdr *symtab_hdr;
5516   struct elf_link_hash_entry **sym_hashes;
5517   struct ppc_elf_link_hash_table *htab;
5518   Elf_Internal_Rela *rel;
5519   Elf_Internal_Rela *relend;
5520   Elf_Internal_Rela outrel;
5521   bfd_byte *loc;
5522   asection *got2, *sreloc = NULL;
5523   bfd_vma *local_got_offsets;
5524   bfd_boolean ret = TRUE;
5525
5526 #ifdef DEBUG
5527   _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
5528                       "%ld relocations%s",
5529                       input_bfd, input_section,
5530                       (long) input_section->reloc_count,
5531                       (info->relocatable) ? " (relocatable)" : "");
5532 #endif
5533
5534   got2 = bfd_get_section_by_name (input_bfd, ".got2");
5535
5536   if (info->relocatable)
5537     {
5538       if (got2 == NULL)
5539         return TRUE;
5540
5541       rel = relocs;
5542       relend = relocs + input_section->reloc_count;
5543       for (; rel < relend; rel++)
5544         {
5545           enum elf_ppc_reloc_type r_type;
5546
5547           r_type = ELF32_R_TYPE (rel->r_info);
5548           if (r_type == R_PPC_PLTREL24
5549               && rel->r_addend >= 32768)
5550             {
5551               /* R_PPC_PLTREL24 is rather special.  If non-zero, the
5552                  addend specifies the GOT pointer offset within .got2.  */
5553               rel->r_addend += got2->output_offset;
5554             }
5555         }
5556       return TRUE;
5557     }
5558
5559   /* Initialize howto table if not already done.  */
5560   if (!ppc_elf_howto_table[R_PPC_ADDR32])
5561     ppc_elf_howto_init ();
5562
5563   htab = ppc_elf_hash_table (info);
5564   local_got_offsets = elf_local_got_offsets (input_bfd);
5565   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5566   sym_hashes = elf_sym_hashes (input_bfd);
5567   rel = relocs;
5568   relend = relocs + input_section->reloc_count;
5569   for (; rel < relend; rel++)
5570     {
5571       enum elf_ppc_reloc_type r_type;
5572       bfd_vma addend;
5573       bfd_reloc_status_type r;
5574       Elf_Internal_Sym *sym;
5575       asection *sec;
5576       struct elf_link_hash_entry *h;
5577       const char *sym_name;
5578       reloc_howto_type *howto;
5579       unsigned long r_symndx;
5580       bfd_vma relocation;
5581       bfd_vma branch_bit, insn, from;
5582       bfd_boolean unresolved_reloc;
5583       bfd_boolean warned;
5584       unsigned int tls_type, tls_mask, tls_gd;
5585
5586       r_type = ELF32_R_TYPE (rel->r_info);
5587       sym = NULL;
5588       sec = NULL;
5589       h = NULL;
5590       unresolved_reloc = FALSE;
5591       warned = FALSE;
5592       r_symndx = ELF32_R_SYM (rel->r_info);
5593
5594       if (r_symndx < symtab_hdr->sh_info)
5595         {
5596           sym = local_syms + r_symndx;
5597           sec = local_sections[r_symndx];
5598           sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
5599
5600           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5601         }
5602       else
5603         {
5604           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5605                                    r_symndx, symtab_hdr, sym_hashes,
5606                                    h, sec, relocation,
5607                                    unresolved_reloc, warned);
5608
5609           sym_name = h->root.root.string;
5610         }
5611
5612       /* TLS optimizations.  Replace instruction sequences and relocs
5613          based on information we collected in tls_optimize.  We edit
5614          RELOCS so that --emit-relocs will output something sensible
5615          for the final instruction stream.  */
5616       tls_mask = 0;
5617       tls_gd = 0;
5618       if (IS_PPC_TLS_RELOC (r_type))
5619         {
5620           if (h != NULL)
5621             tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
5622           else if (local_got_offsets != NULL)
5623             {
5624               char *lgot_masks;
5625               lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
5626               tls_mask = lgot_masks[r_symndx];
5627             }
5628         }
5629
5630       /* Ensure reloc mapping code below stays sane.  */
5631       if ((R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TLSGD16 & 3)
5632           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
5633           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
5634           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
5635           || (R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TPREL16 & 3)
5636           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
5637           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
5638           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
5639         abort ();
5640       switch (r_type)
5641         {
5642         default:
5643           break;
5644
5645         case R_PPC_GOT_TPREL16:
5646         case R_PPC_GOT_TPREL16_LO:
5647           if (tls_mask != 0
5648               && (tls_mask & TLS_TPREL) == 0)
5649             {
5650               bfd_vma insn;
5651               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
5652               insn &= 31 << 21;
5653               insn |= 0x3c020000;       /* addis 0,2,0 */
5654               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
5655               r_type = R_PPC_TPREL16_HA;
5656               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5657             }
5658           break;
5659
5660         case R_PPC_TLS:
5661           if (tls_mask != 0
5662               && (tls_mask & TLS_TPREL) == 0)
5663             {
5664               bfd_vma insn, rtra;
5665               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5666               if ((insn & ((31 << 26) | (31 << 11)))
5667                   == ((31 << 26) | (2 << 11)))
5668                 rtra = insn & ((1 << 26) - (1 << 16));
5669               else if ((insn & ((31 << 26) | (31 << 16)))
5670                        == ((31 << 26) | (2 << 16)))
5671                 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
5672               else
5673                 abort ();
5674               if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
5675                 /* add -> addi.  */
5676                 insn = 14 << 26;
5677               else if ((insn & (31 << 1)) == 23 << 1
5678                        && ((insn & (31 << 6)) < 14 << 6
5679                            || ((insn & (31 << 6)) >= 16 << 6
5680                                && (insn & (31 << 6)) < 24 << 6)))
5681                 /* load and store indexed -> dform.  */
5682                 insn = (32 | ((insn >> 6) & 31)) << 26;
5683               else if ((insn & (31 << 1)) == 21 << 1
5684                        && (insn & (0x1a << 6)) == 0)
5685                 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
5686                 insn = (((58 | ((insn >> 6) & 4)) << 26)
5687                         | ((insn >> 6) & 1));
5688               else if ((insn & (31 << 1)) == 21 << 1
5689                        && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
5690                 /* lwax -> lwa.  */
5691                 insn = (58 << 26) | 2;
5692               else
5693                 abort ();
5694               insn |= rtra;
5695               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5696               r_type = R_PPC_TPREL16_LO;
5697               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5698               /* Was PPC_TLS which sits on insn boundary, now
5699                  PPC_TPREL16_LO which is at insn+2.  */
5700               rel->r_offset += 2;
5701             }
5702           break;
5703
5704         case R_PPC_GOT_TLSGD16_HI:
5705         case R_PPC_GOT_TLSGD16_HA:
5706           tls_gd = TLS_TPRELGD;
5707           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
5708             goto tls_gdld_hi;
5709           break;
5710
5711         case R_PPC_GOT_TLSLD16_HI:
5712         case R_PPC_GOT_TLSLD16_HA:
5713           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
5714             {
5715             tls_gdld_hi:
5716               if ((tls_mask & tls_gd) != 0)
5717                 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
5718                           + R_PPC_GOT_TPREL16);
5719               else
5720                 {
5721                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
5722                   rel->r_offset -= 2;
5723                   r_type = R_PPC_NONE;
5724                 }
5725               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5726             }
5727           break;
5728
5729         case R_PPC_GOT_TLSGD16:
5730         case R_PPC_GOT_TLSGD16_LO:
5731           tls_gd = TLS_TPRELGD;
5732           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
5733             goto tls_get_addr_check;
5734           break;
5735
5736         case R_PPC_GOT_TLSLD16:
5737         case R_PPC_GOT_TLSLD16_LO:
5738           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
5739             {
5740             tls_get_addr_check:
5741               if (rel + 1 < relend)
5742                 {
5743                   enum elf_ppc_reloc_type r_type2;
5744                   unsigned long r_symndx2;
5745                   struct elf_link_hash_entry *h2;
5746                   bfd_vma insn1, insn2;
5747                   bfd_vma offset;
5748
5749                   /* The next instruction should be a call to
5750                      __tls_get_addr.  Peek at the reloc to be sure.  */
5751                   r_type2 = ELF32_R_TYPE (rel[1].r_info);
5752                   r_symndx2 = ELF32_R_SYM (rel[1].r_info);
5753                   if (r_symndx2 < symtab_hdr->sh_info
5754                       || (r_type2 != R_PPC_REL14
5755                           && r_type2 != R_PPC_REL14_BRTAKEN
5756                           && r_type2 != R_PPC_REL14_BRNTAKEN
5757                           && r_type2 != R_PPC_REL24
5758                           && r_type2 != R_PPC_PLTREL24))
5759                     break;
5760
5761                   h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
5762                   while (h2->root.type == bfd_link_hash_indirect
5763                          || h2->root.type == bfd_link_hash_warning)
5764                     h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
5765                   if (h2 == NULL || h2 != htab->tls_get_addr)
5766                     break;
5767
5768                   /* OK, it checks out.  Replace the call.  */
5769                   offset = rel[1].r_offset;
5770                   insn1 = bfd_get_32 (output_bfd,
5771                                       contents + rel->r_offset - 2);
5772                   if ((tls_mask & tls_gd) != 0)
5773                     {
5774                       /* IE */
5775                       insn1 &= (1 << 26) - 1;
5776                       insn1 |= 32 << 26;        /* lwz */
5777                       insn2 = 0x7c631214;       /* add 3,3,2 */
5778                       rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
5779                       rel[1].r_addend = 0;
5780                       r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
5781                                 + R_PPC_GOT_TPREL16);
5782                       rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5783                     }
5784                   else
5785                     {
5786                       /* LE */
5787                       insn1 = 0x3c620000;       /* addis 3,2,0 */
5788                       insn2 = 0x38630000;       /* addi 3,3,0 */
5789                       if (tls_gd == 0)
5790                         {
5791                           /* Was an LD reloc.  */
5792                           r_symndx = 0;
5793                           rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
5794                         }
5795                       r_type = R_PPC_TPREL16_HA;
5796                       rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5797                       rel[1].r_info = ELF32_R_INFO (r_symndx,
5798                                                     R_PPC_TPREL16_LO);
5799                       rel[1].r_offset += 2;
5800                       rel[1].r_addend = rel->r_addend;
5801                     }
5802                   bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
5803                   bfd_put_32 (output_bfd, insn2, contents + offset);
5804                   if (tls_gd == 0)
5805                     {
5806                       /* We changed the symbol on an LD reloc.  Start over
5807                          in order to get h, sym, sec etc. right.  */
5808                       rel--;
5809                       continue;
5810                     }
5811                 }
5812             }
5813           break;
5814         }
5815
5816       /* Handle other relocations that tweak non-addend part of insn.  */
5817       branch_bit = 0;
5818       switch (r_type)
5819         {
5820         default:
5821           break;
5822
5823           /* Branch taken prediction relocations.  */
5824         case R_PPC_ADDR14_BRTAKEN:
5825         case R_PPC_REL14_BRTAKEN:
5826           branch_bit = BRANCH_PREDICT_BIT;
5827           /* Fall thru */
5828
5829           /* Branch not taken prediction relocations.  */
5830         case R_PPC_ADDR14_BRNTAKEN:
5831         case R_PPC_REL14_BRNTAKEN:
5832           insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5833           insn &= ~BRANCH_PREDICT_BIT;
5834           insn |= branch_bit;
5835
5836           from = (rel->r_offset
5837                   + input_section->output_offset
5838                   + input_section->output_section->vma);
5839
5840           /* Invert 'y' bit if not the default.  */
5841           if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
5842             insn ^= BRANCH_PREDICT_BIT;
5843
5844           bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5845           break;
5846         }
5847
5848       addend = rel->r_addend;
5849       tls_type = 0;
5850       howto = NULL;
5851       if (r_type < R_PPC_max)
5852         howto = ppc_elf_howto_table[r_type];
5853       switch (r_type)
5854         {
5855         default:
5856           (*_bfd_error_handler)
5857             (_("%B: unknown relocation type %d for symbol %s"),
5858              input_bfd, (int) r_type, sym_name);
5859
5860           bfd_set_error (bfd_error_bad_value);
5861           ret = FALSE;
5862           continue;
5863
5864         case R_PPC_NONE:
5865         case R_PPC_TLS:
5866         case R_PPC_EMB_MRKREF:
5867         case R_PPC_GNU_VTINHERIT:
5868         case R_PPC_GNU_VTENTRY:
5869           continue;
5870
5871           /* GOT16 relocations.  Like an ADDR16 using the symbol's
5872              address in the GOT as relocation value instead of the
5873              symbol's value itself.  Also, create a GOT entry for the
5874              symbol and put the symbol value there.  */
5875         case R_PPC_GOT_TLSGD16:
5876         case R_PPC_GOT_TLSGD16_LO:
5877         case R_PPC_GOT_TLSGD16_HI:
5878         case R_PPC_GOT_TLSGD16_HA:
5879           tls_type = TLS_TLS | TLS_GD;
5880           goto dogot;
5881
5882         case R_PPC_GOT_TLSLD16:
5883         case R_PPC_GOT_TLSLD16_LO:
5884         case R_PPC_GOT_TLSLD16_HI:
5885         case R_PPC_GOT_TLSLD16_HA:
5886           tls_type = TLS_TLS | TLS_LD;
5887           goto dogot;
5888
5889         case R_PPC_GOT_TPREL16:
5890         case R_PPC_GOT_TPREL16_LO:
5891         case R_PPC_GOT_TPREL16_HI:
5892         case R_PPC_GOT_TPREL16_HA:
5893           tls_type = TLS_TLS | TLS_TPREL;
5894           goto dogot;
5895
5896         case R_PPC_GOT_DTPREL16:
5897         case R_PPC_GOT_DTPREL16_LO:
5898         case R_PPC_GOT_DTPREL16_HI:
5899         case R_PPC_GOT_DTPREL16_HA:
5900           tls_type = TLS_TLS | TLS_DTPREL;
5901           goto dogot;
5902
5903         case R_PPC_GOT16:
5904         case R_PPC_GOT16_LO:
5905         case R_PPC_GOT16_HI:
5906         case R_PPC_GOT16_HA:
5907         dogot:
5908           {
5909             /* Relocation is to the entry for this symbol in the global
5910                offset table.  */
5911             bfd_vma off;
5912             bfd_vma *offp;
5913             unsigned long indx;
5914
5915             if (htab->got == NULL)
5916               abort ();
5917
5918             indx = 0;
5919             if (tls_type == (TLS_TLS | TLS_LD)
5920                 && (h == NULL
5921                     || !h->def_dynamic))
5922               offp = &htab->tlsld_got.offset;
5923             else if (h != NULL)
5924               {
5925                 bfd_boolean dyn;
5926                 dyn = htab->elf.dynamic_sections_created;
5927                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5928                     || (info->shared
5929                         && SYMBOL_REFERENCES_LOCAL (info, h)))
5930                   /* This is actually a static link, or it is a
5931                      -Bsymbolic link and the symbol is defined
5932                      locally, or the symbol was forced to be local
5933                      because of a version file.  */
5934                   ;
5935                 else
5936                   {
5937                     indx = h->dynindx;
5938                     unresolved_reloc = FALSE;
5939                   }
5940                 offp = &h->got.offset;
5941               }
5942             else
5943               {
5944                 if (local_got_offsets == NULL)
5945                   abort ();
5946                 offp = &local_got_offsets[r_symndx];
5947               }
5948
5949             /* The offset must always be a multiple of 4.  We use the
5950                least significant bit to record whether we have already
5951                processed this entry.  */
5952             off = *offp;
5953             if ((off & 1) != 0)
5954               off &= ~1;
5955             else
5956               {
5957                 unsigned int tls_m = (tls_mask
5958                                       & (TLS_LD | TLS_GD | TLS_DTPREL
5959                                          | TLS_TPREL | TLS_TPRELGD));
5960
5961                 if (offp == &htab->tlsld_got.offset)
5962                   tls_m = TLS_LD;
5963                 else if (h == NULL
5964                          || !h->def_dynamic)
5965                   tls_m &= ~TLS_LD;
5966
5967                 /* We might have multiple got entries for this sym.
5968                    Initialize them all.  */
5969                 do
5970                   {
5971                     int tls_ty = 0;
5972
5973                     if ((tls_m & TLS_LD) != 0)
5974                       {
5975                         tls_ty = TLS_TLS | TLS_LD;
5976                         tls_m &= ~TLS_LD;
5977                       }
5978                     else if ((tls_m & TLS_GD) != 0)
5979                       {
5980                         tls_ty = TLS_TLS | TLS_GD;
5981                         tls_m &= ~TLS_GD;
5982                       }
5983                     else if ((tls_m & TLS_DTPREL) != 0)
5984                       {
5985                         tls_ty = TLS_TLS | TLS_DTPREL;
5986                         tls_m &= ~TLS_DTPREL;
5987                       }
5988                     else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5989                       {
5990                         tls_ty = TLS_TLS | TLS_TPREL;
5991                         tls_m = 0;
5992                       }
5993
5994                     /* Generate relocs for the dynamic linker.  */
5995                     if ((info->shared || indx != 0)
5996                         && (h == NULL
5997                             || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5998                             || h->root.type != bfd_link_hash_undefweak))
5999                       {
6000                         outrel.r_offset = (htab->got->output_section->vma
6001                                            + htab->got->output_offset
6002                                            + off);
6003                         outrel.r_addend = 0;
6004                         if (tls_ty & (TLS_LD | TLS_GD))
6005                           {
6006                             outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
6007                             if (tls_ty == (TLS_TLS | TLS_GD))
6008                               {
6009                                 loc = htab->relgot->contents;
6010                                 loc += (htab->relgot->reloc_count++
6011                                         * sizeof (Elf32_External_Rela));
6012                                 bfd_elf32_swap_reloca_out (output_bfd,
6013                                                            &outrel, loc);
6014                                 outrel.r_offset += 4;
6015                                 outrel.r_info
6016                                   = ELF32_R_INFO (indx, R_PPC_DTPREL32);
6017                               }
6018                           }
6019                         else if (tls_ty == (TLS_TLS | TLS_DTPREL))
6020                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
6021                         else if (tls_ty == (TLS_TLS | TLS_TPREL))
6022                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
6023                         else if (indx == 0)
6024                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
6025                         else
6026                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
6027                         if (indx == 0)
6028                           {
6029                             outrel.r_addend += relocation;
6030                             if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
6031                               outrel.r_addend -= htab->elf.tls_sec->vma;
6032                           }
6033                         loc = htab->relgot->contents;
6034                         loc += (htab->relgot->reloc_count++
6035                                 * sizeof (Elf32_External_Rela));
6036                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6037                       }
6038
6039                     /* Init the .got section contents if we're not
6040                        emitting a reloc.  */
6041                     else
6042                       {
6043                         bfd_vma value = relocation;
6044
6045                         if (tls_ty == (TLS_TLS | TLS_LD))
6046                           value = 1;
6047                         else if (tls_ty != 0)
6048                           {
6049                             value -= htab->elf.tls_sec->vma + DTP_OFFSET;
6050                             if (tls_ty == (TLS_TLS | TLS_TPREL))
6051                               value += DTP_OFFSET - TP_OFFSET;
6052
6053                             if (tls_ty == (TLS_TLS | TLS_GD))
6054                               {
6055                                 bfd_put_32 (output_bfd, value,
6056                                             htab->got->contents + off + 4);
6057                                 value = 1;
6058                               }
6059                           }
6060                         bfd_put_32 (output_bfd, value,
6061                                     htab->got->contents + off);
6062                       }
6063
6064                     off += 4;
6065                     if (tls_ty & (TLS_LD | TLS_GD))
6066                       off += 4;
6067                   }
6068                 while (tls_m != 0);
6069
6070                 off = *offp;
6071                 *offp = off | 1;
6072               }
6073
6074             if (off >= (bfd_vma) -2)
6075               abort ();
6076
6077             if ((tls_type & TLS_TLS) != 0)
6078               {
6079                 if (tls_type != (TLS_TLS | TLS_LD))
6080                   {
6081                     if ((tls_mask & TLS_LD) != 0
6082                         && !(h == NULL
6083                              || !h->def_dynamic))
6084                       off += 8;
6085                     if (tls_type != (TLS_TLS | TLS_GD))
6086                       {
6087                         if ((tls_mask & TLS_GD) != 0)
6088                           off += 8;
6089                         if (tls_type != (TLS_TLS | TLS_DTPREL))
6090                           {
6091                             if ((tls_mask & TLS_DTPREL) != 0)
6092                               off += 4;
6093                           }
6094                       }
6095                   }
6096               }
6097
6098             relocation = htab->got->output_offset + off;
6099             relocation -= htab->elf.hgot->root.u.def.value;
6100
6101             /* Addends on got relocations don't make much sense.
6102                x+off@got is actually x@got+off, and since the got is
6103                generated by a hash table traversal, the value in the
6104                got at entry m+n bears little relation to the entry m.  */
6105             if (addend != 0)
6106               (*_bfd_error_handler)
6107                 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
6108                  input_bfd,
6109                  input_section,
6110                  (long) rel->r_offset,
6111                  howto->name,
6112                  sym_name);
6113           }
6114         break;
6115
6116         /* Relocations that need no special processing.  */
6117         case R_PPC_LOCAL24PC:
6118           /* It makes no sense to point a local relocation
6119              at a symbol not in this object.  */
6120           if (unresolved_reloc)
6121             {
6122               if (! (*info->callbacks->undefined_symbol) (info,
6123                                                           h->root.root.string,
6124                                                           input_bfd,
6125                                                           input_section,
6126                                                           rel->r_offset,
6127                                                           TRUE))
6128                 return FALSE;
6129               continue;
6130             }
6131           break;
6132
6133         case R_PPC_DTPREL16:
6134         case R_PPC_DTPREL16_LO:
6135         case R_PPC_DTPREL16_HI:
6136         case R_PPC_DTPREL16_HA:
6137           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
6138           break;
6139
6140           /* Relocations that may need to be propagated if this is a shared
6141              object.  */
6142         case R_PPC_TPREL16:
6143         case R_PPC_TPREL16_LO:
6144         case R_PPC_TPREL16_HI:
6145         case R_PPC_TPREL16_HA:
6146           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
6147           /* The TPREL16 relocs shouldn't really be used in shared
6148              libs as they will result in DT_TEXTREL being set, but
6149              support them anyway.  */
6150           goto dodyn;
6151
6152         case R_PPC_TPREL32:
6153           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
6154           goto dodyn;
6155
6156         case R_PPC_DTPREL32:
6157           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
6158           goto dodyn;
6159
6160         case R_PPC_DTPMOD32:
6161           relocation = 1;
6162           addend = 0;
6163           goto dodyn;
6164
6165         case R_PPC_REL16:
6166         case R_PPC_REL16_LO:
6167         case R_PPC_REL16_HI:
6168         case R_PPC_REL16_HA:
6169           break;
6170
6171         case R_PPC_REL24:
6172         case R_PPC_REL32:
6173         case R_PPC_REL14:
6174         case R_PPC_REL14_BRTAKEN:
6175         case R_PPC_REL14_BRNTAKEN:
6176           /* If these relocations are not to a named symbol, they can be
6177              handled right here, no need to bother the dynamic linker.  */
6178           if (SYMBOL_REFERENCES_LOCAL (info, h)
6179               || h == htab->elf.hgot)
6180             break;
6181           /* fall through */
6182
6183           /* Relocations that always need to be propagated if this is a shared
6184              object.  */
6185         case R_PPC_ADDR32:
6186         case R_PPC_ADDR24:
6187         case R_PPC_ADDR16:
6188         case R_PPC_ADDR16_LO:
6189         case R_PPC_ADDR16_HI:
6190         case R_PPC_ADDR16_HA:
6191         case R_PPC_ADDR14:
6192         case R_PPC_ADDR14_BRTAKEN:
6193         case R_PPC_ADDR14_BRNTAKEN:
6194         case R_PPC_UADDR32:
6195         case R_PPC_UADDR16:
6196           /* r_symndx will be zero only for relocs against symbols
6197              from removed linkonce sections, or sections discarded by
6198              a linker script.  */
6199         dodyn:
6200           if (r_symndx == 0)
6201             break;
6202           /* Fall thru.  */
6203
6204           if ((input_section->flags & SEC_ALLOC) == 0)
6205             break;
6206           /* Fall thru.  */
6207
6208           if ((info->shared
6209                && (h == NULL
6210                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
6211                    || h->root.type != bfd_link_hash_undefweak)
6212                && (MUST_BE_DYN_RELOC (r_type)
6213                    || !SYMBOL_CALLS_LOCAL (info, h)))
6214               || (ELIMINATE_COPY_RELOCS
6215                   && !info->shared
6216                   && h != NULL
6217                   && h->dynindx != -1
6218                   && !h->non_got_ref
6219                   && h->def_dynamic
6220                   && !h->def_regular))
6221             {
6222               int skip;
6223
6224 #ifdef DEBUG
6225               fprintf (stderr, "ppc_elf_relocate_section needs to "
6226                        "create relocation for %s\n",
6227                        (h && h->root.root.string
6228                         ? h->root.root.string : "<unknown>"));
6229 #endif
6230
6231               /* When generating a shared object, these relocations
6232                  are copied into the output file to be resolved at run
6233                  time.  */
6234               if (sreloc == NULL)
6235                 {
6236                   const char *name;
6237
6238                   name = (bfd_elf_string_from_elf_section
6239                           (input_bfd,
6240                            elf_elfheader (input_bfd)->e_shstrndx,
6241                            elf_section_data (input_section)->rel_hdr.sh_name));
6242                   if (name == NULL)
6243                     return FALSE;
6244
6245                   BFD_ASSERT (CONST_STRNEQ (name, ".rela")
6246                               && strcmp (bfd_get_section_name (input_bfd,
6247                                                                input_section),
6248                                          name + 5) == 0);
6249
6250                   sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
6251                   BFD_ASSERT (sreloc != NULL);
6252                 }
6253
6254               skip = 0;
6255
6256               outrel.r_offset =
6257                 _bfd_elf_section_offset (output_bfd, info, input_section,
6258                                          rel->r_offset);
6259               if (outrel.r_offset == (bfd_vma) -1
6260                   || outrel.r_offset == (bfd_vma) -2)
6261                 skip = (int) outrel.r_offset;
6262               outrel.r_offset += (input_section->output_section->vma
6263                                   + input_section->output_offset);
6264
6265               if (skip)
6266                 memset (&outrel, 0, sizeof outrel);
6267               else if (!SYMBOL_REFERENCES_LOCAL (info, h))
6268                 {
6269                   unresolved_reloc = FALSE;
6270                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
6271                   outrel.r_addend = rel->r_addend;
6272                 }
6273               else
6274                 {
6275                   outrel.r_addend = relocation + rel->r_addend;
6276
6277                   if (r_type == R_PPC_ADDR32)
6278                     outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
6279                   else
6280                     {
6281                       long indx;
6282
6283                       if (bfd_is_abs_section (sec))
6284                         indx = 0;
6285                       else if (sec == NULL || sec->owner == NULL)
6286                         {
6287                           bfd_set_error (bfd_error_bad_value);
6288                           return FALSE;
6289                         }
6290                       else
6291                         {
6292                           asection *osec;
6293
6294                           /* We are turning this relocation into one
6295                              against a section symbol.  It would be
6296                              proper to subtract the symbol's value,
6297                              osec->vma, from the emitted reloc addend,
6298                              but ld.so expects buggy relocs.  */
6299                           osec = sec->output_section;
6300                           indx = elf_section_data (osec)->dynindx;
6301                           BFD_ASSERT (indx > 0);
6302 #ifdef DEBUG
6303                           if (indx <= 0)
6304                             printf ("indx=%ld section=%s flags=%08x name=%s\n",
6305                                     indx, osec->name, osec->flags,
6306                                     h->root.root.string);
6307 #endif
6308                         }
6309
6310                       outrel.r_info = ELF32_R_INFO (indx, r_type);
6311                     }
6312                 }
6313
6314               loc = sreloc->contents;
6315               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
6316               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6317
6318               if (skip == -1)
6319                 continue;
6320
6321               /* This reloc will be computed at runtime.  We clear the memory
6322                  so that it contains predictable value.  */
6323               if (! skip
6324                   && ((input_section->flags & SEC_ALLOC) != 0
6325                       || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
6326                 {
6327                   relocation = howto->pc_relative ? outrel.r_offset : 0;
6328                   addend = 0;
6329                   break;
6330                 }
6331             }
6332           break;
6333
6334         case R_PPC_RELAX32PC_PLT:
6335         case R_PPC_RELAX32_PLT:
6336           {
6337             struct plt_entry *ent = find_plt_ent (h, got2, addend);
6338
6339             if (htab->plt_type == PLT_NEW)
6340               relocation = (htab->glink->output_section->vma
6341                             + htab->glink->output_offset
6342                             + ent->glink_offset);
6343             else
6344               relocation = (htab->plt->output_section->vma
6345                             + htab->plt->output_offset
6346                             + ent->plt.offset);
6347             addend = 0;
6348           }
6349           if (r_type == R_PPC_RELAX32_PLT)
6350             goto relax32;
6351           /* Fall thru */
6352
6353         case R_PPC_RELAX32PC:
6354           relocation -= (input_section->output_section->vma
6355                          + input_section->output_offset
6356                          + rel->r_offset - 4);
6357           /* Fall thru */
6358
6359         case R_PPC_RELAX32:
6360         relax32:
6361           {
6362             unsigned long t0;
6363             unsigned long t1;
6364
6365             t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
6366             t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
6367
6368             /* We're clearing the bits for R_PPC_ADDR16_HA
6369                and R_PPC_ADDR16_LO here.  */
6370             t0 &= ~0xffff;
6371             t1 &= ~0xffff;
6372
6373             /* t0 is HA, t1 is LO */
6374             relocation += addend;
6375             t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
6376             t1 |= relocation & 0xffff;
6377
6378             bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
6379             bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
6380           }
6381           continue;
6382
6383           /* Indirect .sdata relocation.  */
6384         case R_PPC_EMB_SDAI16:
6385           BFD_ASSERT (htab->sdata[0].section != NULL);
6386           relocation
6387             = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
6388                                                  h, relocation, rel);
6389           break;
6390
6391           /* Indirect .sdata2 relocation.  */
6392         case R_PPC_EMB_SDA2I16:
6393           BFD_ASSERT (htab->sdata[1].section != NULL);
6394           relocation
6395             = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
6396                                                  h, relocation, rel);
6397           break;
6398
6399           /* Handle the TOC16 reloc.  We want to use the offset within the .got
6400              section, not the actual VMA.  This is appropriate when generating
6401              an embedded ELF object, for which the .got section acts like the
6402              AIX .toc section.  */
6403         case R_PPC_TOC16:                       /* phony GOT16 relocations */
6404           BFD_ASSERT (sec != NULL);
6405           BFD_ASSERT (bfd_is_und_section (sec)
6406                       || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
6407                       || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
6408
6409             addend -= sec->output_section->vma + sec->output_offset + 0x8000;
6410           break;
6411
6412         case R_PPC_PLTREL24:
6413           /* Relocation is to the entry for this symbol in the
6414              procedure linkage table.  */
6415           {
6416             struct plt_entry *ent = find_plt_ent (h, got2, addend);
6417
6418             addend = 0;
6419             if (ent == NULL
6420                 || htab->plt == NULL)
6421               {
6422                 /* We didn't make a PLT entry for this symbol.  This
6423                    happens when statically linking PIC code, or when
6424                    using -Bsymbolic.  */
6425                 break;
6426               }
6427
6428             unresolved_reloc = FALSE;
6429             if (htab->plt_type == PLT_NEW)
6430               relocation = (htab->glink->output_section->vma
6431                             + htab->glink->output_offset
6432                             + ent->glink_offset);
6433             else
6434               relocation = (htab->plt->output_section->vma
6435                             + htab->plt->output_offset
6436                             + ent->plt.offset);
6437           }
6438           break;
6439
6440           /* Relocate against _SDA_BASE_.  */
6441         case R_PPC_SDAREL16:
6442           {
6443             const char *name;
6444             struct elf_link_hash_entry *sh;
6445
6446             BFD_ASSERT (sec != NULL);
6447             name = bfd_get_section_name (abfd, sec->output_section);
6448             if (! ((CONST_STRNEQ (name, ".sdata")
6449                     && (name[6] == 0 || name[6] == '.'))
6450                    || (CONST_STRNEQ (name, ".sbss")
6451                        && (name[5] == 0 || name[5] == '.'))))
6452               {
6453                 (*_bfd_error_handler)
6454                   (_("%B: the target (%s) of a %s relocation is "
6455                      "in the wrong output section (%s)"),
6456                    input_bfd,
6457                    sym_name,
6458                    howto->name,
6459                    name);
6460               }
6461             sh = htab->sdata[0].sym;
6462             addend -= (sh->root.u.def.value
6463                        + sh->root.u.def.section->output_offset
6464                        + sh->root.u.def.section->output_section->vma);
6465           }
6466           break;
6467
6468           /* Relocate against _SDA2_BASE_.  */
6469         case R_PPC_EMB_SDA2REL:
6470           {
6471             const char *name;
6472             struct elf_link_hash_entry *sh;
6473
6474             BFD_ASSERT (sec != NULL);
6475             name = bfd_get_section_name (abfd, sec->output_section);
6476             if (! (CONST_STRNEQ (name, ".sdata2")
6477                    || CONST_STRNEQ (name, ".sbss2")))
6478               {
6479                 (*_bfd_error_handler)
6480                   (_("%B: the target (%s) of a %s relocation is "
6481                      "in the wrong output section (%s)"),
6482                    input_bfd,
6483                    sym_name,
6484                    howto->name,
6485                    name);
6486
6487                 bfd_set_error (bfd_error_bad_value);
6488                 ret = FALSE;
6489                 continue;
6490               }
6491             sh = htab->sdata[1].sym;
6492             addend -= (sh->root.u.def.value
6493                        + sh->root.u.def.section->output_offset
6494                        + sh->root.u.def.section->output_section->vma);
6495           }
6496           break;
6497
6498           /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
6499         case R_PPC_EMB_SDA21:
6500         case R_PPC_EMB_RELSDA:
6501           {
6502             const char *name;
6503             int reg;
6504             struct elf_link_hash_entry *sh;
6505
6506             BFD_ASSERT (sec != NULL);
6507             name = bfd_get_section_name (abfd, sec->output_section);
6508             if (((CONST_STRNEQ (name, ".sdata")
6509                   && (name[6] == 0 || name[6] == '.'))
6510                  || (CONST_STRNEQ (name, ".sbss")
6511                      && (name[5] == 0 || name[5] == '.'))))
6512               {
6513                 reg = 13;
6514                 sh = htab->sdata[0].sym;
6515                 addend -= (sh->root.u.def.value
6516                            + sh->root.u.def.section->output_offset
6517                            + sh->root.u.def.section->output_section->vma);
6518               }
6519
6520             else if (CONST_STRNEQ (name, ".sdata2")
6521                      || CONST_STRNEQ (name, ".sbss2"))
6522               {
6523                 reg = 2;
6524                 sh = htab->sdata[1].sym;
6525                 addend -= (sh->root.u.def.value
6526                            + sh->root.u.def.section->output_offset
6527                            + sh->root.u.def.section->output_section->vma);
6528               }
6529
6530             else if (strcmp (name, ".PPC.EMB.sdata0") == 0
6531                      || strcmp (name, ".PPC.EMB.sbss0") == 0)
6532               {
6533                 reg = 0;
6534               }
6535
6536             else
6537               {
6538                 (*_bfd_error_handler)
6539                   (_("%B: the target (%s) of a %s relocation is "
6540                      "in the wrong output section (%s)"),
6541                    input_bfd,
6542                    sym_name,
6543                    howto->name,
6544                    name);
6545
6546                 bfd_set_error (bfd_error_bad_value);
6547                 ret = FALSE;
6548                 continue;
6549               }
6550
6551             if (r_type == R_PPC_EMB_SDA21)
6552               {                 /* fill in register field */
6553                 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
6554                 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
6555                 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
6556               }
6557           }
6558           break;
6559
6560           /* Relocate against the beginning of the section.  */
6561         case R_PPC_SECTOFF:
6562         case R_PPC_SECTOFF_LO:
6563         case R_PPC_SECTOFF_HI:
6564         case R_PPC_SECTOFF_HA:
6565           BFD_ASSERT (sec != NULL);
6566           addend -= sec->output_section->vma;
6567           break;
6568
6569           /* Negative relocations.  */
6570         case R_PPC_EMB_NADDR32:
6571         case R_PPC_EMB_NADDR16:
6572         case R_PPC_EMB_NADDR16_LO:
6573         case R_PPC_EMB_NADDR16_HI:
6574         case R_PPC_EMB_NADDR16_HA:
6575           addend -= 2 * relocation;
6576           break;
6577
6578         case R_PPC_COPY:
6579         case R_PPC_GLOB_DAT:
6580         case R_PPC_JMP_SLOT:
6581         case R_PPC_RELATIVE:
6582         case R_PPC_PLT32:
6583         case R_PPC_PLTREL32:
6584         case R_PPC_PLT16_LO:
6585         case R_PPC_PLT16_HI:
6586         case R_PPC_PLT16_HA:
6587         case R_PPC_ADDR30:
6588         case R_PPC_EMB_RELSEC16:
6589         case R_PPC_EMB_RELST_LO:
6590         case R_PPC_EMB_RELST_HI:
6591         case R_PPC_EMB_RELST_HA:
6592         case R_PPC_EMB_BIT_FLD:
6593           (*_bfd_error_handler)
6594             (_("%B: relocation %s is not yet supported for symbol %s."),
6595              input_bfd,
6596              howto->name,
6597              sym_name);
6598
6599           bfd_set_error (bfd_error_invalid_operation);
6600           ret = FALSE;
6601           continue;
6602         }
6603
6604       /* Do any further special processing.  */
6605       switch (r_type)
6606         {
6607         default:
6608           break;
6609
6610         case R_PPC_ADDR16_HA:
6611         case R_PPC_REL16_HA:
6612         case R_PPC_SECTOFF_HA:
6613         case R_PPC_TPREL16_HA:
6614         case R_PPC_DTPREL16_HA:
6615         case R_PPC_EMB_NADDR16_HA:
6616         case R_PPC_EMB_RELST_HA:
6617           /* It's just possible that this symbol is a weak symbol
6618              that's not actually defined anywhere.  In that case,
6619              'sec' would be NULL, and we should leave the symbol
6620              alone (it will be set to zero elsewhere in the link).  */
6621           if (sec == NULL)
6622             break;
6623           /* Fall thru */
6624
6625         case R_PPC_PLT16_HA:
6626         case R_PPC_GOT16_HA:
6627         case R_PPC_GOT_TLSGD16_HA:
6628         case R_PPC_GOT_TLSLD16_HA:
6629         case R_PPC_GOT_TPREL16_HA:
6630         case R_PPC_GOT_DTPREL16_HA:
6631           /* Add 0x10000 if sign bit in 0:15 is set.
6632              Bits 0:15 are not used.  */
6633           addend += 0x8000;
6634           break;
6635         }
6636
6637 #ifdef DEBUG
6638       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
6639                "offset = %ld, addend = %ld\n",
6640                howto->name,
6641                (int) r_type,
6642                sym_name,
6643                r_symndx,
6644                (long) rel->r_offset,
6645                (long) addend);
6646 #endif
6647
6648       if (unresolved_reloc
6649           && !((input_section->flags & SEC_DEBUGGING) != 0
6650                && h->def_dynamic))
6651         {
6652           (*_bfd_error_handler)
6653             (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
6654              input_bfd,
6655              input_section,
6656              (long) rel->r_offset,
6657              howto->name,
6658              sym_name);
6659           ret = FALSE;
6660         }
6661
6662       r = _bfd_final_link_relocate (howto,
6663                                     input_bfd,
6664                                     input_section,
6665                                     contents,
6666                                     rel->r_offset,
6667                                     relocation,
6668                                     addend);
6669
6670       if (r != bfd_reloc_ok)
6671         {
6672           if (r == bfd_reloc_overflow)
6673             {
6674               if (warned)
6675                 continue;
6676               if (h != NULL
6677                   && h->root.type == bfd_link_hash_undefweak
6678                   && howto->pc_relative)
6679                 {
6680                   /* Assume this is a call protected by other code that
6681                      detect the symbol is undefined.  If this is the case,
6682                      we can safely ignore the overflow.  If not, the
6683                      program is hosed anyway, and a little warning isn't
6684                      going to help.  */
6685
6686                   continue;
6687                 }
6688
6689               if (! (*info->callbacks->reloc_overflow) (info,
6690                                                         (h ? &h->root : NULL),
6691                                                         sym_name,
6692                                                         howto->name,
6693                                                         rel->r_addend,
6694                                                         input_bfd,
6695                                                         input_section,
6696                                                         rel->r_offset))
6697                 return FALSE;
6698             }
6699           else
6700             {
6701               (*_bfd_error_handler)
6702                 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
6703                  input_bfd, input_section,
6704                  (long) rel->r_offset, howto->name, sym_name, (int) r);
6705               ret = FALSE;
6706             }
6707         }
6708     }
6709
6710 #ifdef DEBUG
6711   fprintf (stderr, "\n");
6712 #endif
6713
6714   return ret;
6715 }
6716 \f
6717 #define PPC_LO(v) ((v) & 0xffff)
6718 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6719 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6720
6721 /* Finish up dynamic symbol handling.  We set the contents of various
6722    dynamic sections here.  */
6723
6724 static bfd_boolean
6725 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
6726                                struct bfd_link_info *info,
6727                                struct elf_link_hash_entry *h,
6728                                Elf_Internal_Sym *sym)
6729 {
6730   struct ppc_elf_link_hash_table *htab;
6731   struct plt_entry *ent;
6732   bfd_boolean doneone;
6733
6734 #ifdef DEBUG
6735   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
6736            h->root.root.string);
6737 #endif
6738
6739   htab = ppc_elf_hash_table (info);
6740   BFD_ASSERT (htab->elf.dynobj != NULL);
6741
6742   doneone = FALSE;
6743   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6744     if (ent->plt.offset != (bfd_vma) -1)
6745       {
6746         if (!doneone)
6747           {
6748             Elf_Internal_Rela rela;
6749             bfd_byte *loc;
6750             bfd_vma reloc_index;
6751
6752             if (htab->plt_type == PLT_NEW)
6753               reloc_index = ent->plt.offset / 4;
6754             else
6755               {
6756                 reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
6757                                / htab->plt_slot_size);
6758                 if (reloc_index > PLT_NUM_SINGLE_ENTRIES
6759                     && htab->plt_type == PLT_OLD)
6760                   reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
6761               }
6762
6763             /* This symbol has an entry in the procedure linkage table.
6764                Set it up.  */
6765             if (htab->plt_type == PLT_VXWORKS)
6766               {
6767                 bfd_vma got_offset;
6768                 const bfd_vma *plt_entry;
6769                 
6770                 /* The first three entries in .got.plt are reserved.  */
6771                 got_offset = (reloc_index + 3) * 4;
6772
6773                 /* Use the right PLT. */
6774                 plt_entry = info->shared ? ppc_elf_vxworks_pic_plt_entry
6775                             : ppc_elf_vxworks_plt_entry;
6776
6777                 /* Fill in the .plt on VxWorks.  */
6778                 if (info->shared)
6779                   {
6780                     bfd_vma got_offset_hi = (got_offset >> 16)
6781                                             + ((got_offset & 0x8000) >> 15);
6782
6783                     bfd_put_32 (output_bfd,
6784                                 plt_entry[0] | (got_offset_hi & 0xffff),
6785                                 htab->plt->contents + ent->plt.offset + 0);
6786                     bfd_put_32 (output_bfd,
6787                                 plt_entry[1] | (got_offset & 0xffff),
6788                                 htab->plt->contents + ent->plt.offset + 4);
6789                   }
6790                 else
6791                   {
6792                     bfd_vma got_loc
6793                       = (got_offset
6794                          + htab->elf.hgot->root.u.def.value
6795                          + htab->elf.hgot->root.u.def.section->output_offset
6796                          + htab->elf.hgot->root.u.def.section->output_section->vma);
6797                     bfd_vma got_loc_hi = (got_loc >> 16)
6798                                          + ((got_loc & 0x8000) >> 15);
6799
6800                     bfd_put_32 (output_bfd,
6801                                 plt_entry[0] | (got_loc_hi & 0xffff),
6802                                 htab->plt->contents + ent->plt.offset + 0);
6803                     bfd_put_32 (output_bfd,
6804                                 plt_entry[1] | (got_loc & 0xffff),
6805                                 htab->plt->contents + ent->plt.offset + 4);
6806                   }
6807
6808                 bfd_put_32 (output_bfd, plt_entry[2],
6809                             htab->plt->contents + ent->plt.offset + 8);
6810                 bfd_put_32 (output_bfd, plt_entry[3],
6811                             htab->plt->contents + ent->plt.offset + 12);
6812
6813                 /* This instruction is an immediate load.  The value loaded is
6814                    the byte offset of the R_PPC_JMP_SLOT relocation from the
6815                    start of the .rela.plt section.  The value is stored in the
6816                    low-order 16 bits of the load instruction.  */
6817                 /* NOTE: It appears that this is now an index rather than a
6818                    prescaled offset.  */
6819                 bfd_put_32 (output_bfd, 
6820                             plt_entry[4] | reloc_index,
6821                             htab->plt->contents + ent->plt.offset + 16);
6822                 /* This instruction is a PC-relative branch whose target is
6823                    the start of the PLT section.  The address of this branch
6824                    instruction is 20 bytes beyond the start of this PLT entry.
6825                    The address is encoded in bits 6-29, inclusive.  The value
6826                    stored is right-shifted by two bits, permitting a 26-bit
6827                    offset.  */
6828                 bfd_put_32 (output_bfd, 
6829                             (plt_entry[5] 
6830                              | (-(ent->plt.offset + 20) & 0x03fffffc)),
6831                             htab->plt->contents + ent->plt.offset + 20);
6832                 bfd_put_32 (output_bfd, plt_entry[6],
6833                             htab->plt->contents + ent->plt.offset + 24);
6834                 bfd_put_32 (output_bfd, plt_entry[7],
6835                             htab->plt->contents + ent->plt.offset + 28);
6836
6837                 /* Fill in the GOT entry corresponding to this PLT slot with
6838                    the address immediately after the the "bctr" instruction
6839                    in this PLT entry.  */
6840                 bfd_put_32 (output_bfd, (htab->plt->output_section->vma
6841                                          + htab->plt->output_offset
6842                                          + ent->plt.offset + 16),
6843                             htab->sgotplt->contents + got_offset);
6844
6845                 if (!info->shared)
6846                   {
6847                     /* Fill in a couple of entries in .rela.plt.unloaded.  */
6848                     loc = htab->srelplt2->contents
6849                       + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
6850                           * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
6851                          * sizeof (Elf32_External_Rela));
6852
6853                     /* Provide the @ha relocation for the first instruction.  */
6854                     rela.r_offset = (htab->plt->output_section->vma
6855                                      + htab->plt->output_offset
6856                                      + ent->plt.offset + 2);
6857                     rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
6858                                                 R_PPC_ADDR16_HA);
6859                     rela.r_addend = got_offset;
6860                     bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6861                     loc += sizeof (Elf32_External_Rela);
6862
6863                     /* Provide the @l relocation for the second instruction.  */
6864                     rela.r_offset = (htab->plt->output_section->vma
6865                                      + htab->plt->output_offset
6866                                      + ent->plt.offset + 6);
6867                     rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
6868                                                 R_PPC_ADDR16_LO);
6869                     rela.r_addend = got_offset;
6870                     bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6871                     loc += sizeof (Elf32_External_Rela);
6872
6873                     /* Provide a relocation for the GOT entry corresponding to this
6874                        PLT slot.  Point it at the middle of the .plt entry.  */
6875                     rela.r_offset = (htab->sgotplt->output_section->vma
6876                                      + htab->sgotplt->output_offset
6877                                      + got_offset);
6878                     rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
6879                                                 R_PPC_ADDR32);
6880                     rela.r_addend = ent->plt.offset + 16;
6881                     bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6882                   }
6883
6884                 /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
6885                    In particular, the offset for the relocation is not the
6886                    address of the PLT entry for this function, as specified
6887                    by the ABI.  Instead, the offset is set to the address of
6888                    the GOT slot for this function.  See EABI 4.4.4.1.  */
6889                 rela.r_offset = (htab->sgotplt->output_section->vma
6890                                  + htab->sgotplt->output_offset
6891                                  + got_offset);
6892
6893               }
6894             else
6895               {
6896                 rela.r_offset = (htab->plt->output_section->vma
6897                                  + htab->plt->output_offset
6898                                  + ent->plt.offset);
6899                 if (htab->plt_type == PLT_OLD)
6900                   {
6901                     /* We don't need to fill in the .plt.  The ppc dynamic
6902                        linker will fill it in.  */
6903                   }
6904                 else
6905                   {
6906                     bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
6907                                    + htab->glink->output_section->vma
6908                                    + htab->glink->output_offset);
6909                     bfd_put_32 (output_bfd, val,
6910                                 htab->plt->contents + ent->plt.offset);
6911                   }
6912               }
6913
6914             /* Fill in the entry in the .rela.plt section.  */
6915             rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
6916             rela.r_addend = 0;
6917
6918             loc = (htab->relplt->contents
6919                    + reloc_index * sizeof (Elf32_External_Rela));
6920             bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6921
6922             if (!h->def_regular)
6923               {
6924                 /* Mark the symbol as undefined, rather than as defined in
6925                    the .plt section.  Leave the value alone.  */
6926                 sym->st_shndx = SHN_UNDEF;
6927                 /* If the symbol is weak, we do need to clear the value.
6928                    Otherwise, the PLT entry would provide a definition for
6929                    the symbol even if the symbol wasn't defined anywhere,
6930                    and so the symbol would never be NULL.  */
6931                 if (!h->ref_regular_nonweak)
6932                   sym->st_value = 0;
6933               }
6934             doneone = TRUE;
6935           }
6936
6937         if (htab->plt_type == PLT_NEW)
6938           {
6939             bfd_vma plt;
6940             unsigned char *p;
6941
6942             plt = (ent->plt.offset
6943                    + htab->plt->output_section->vma
6944                    + htab->plt->output_offset);
6945             p = (unsigned char *) htab->glink->contents + ent->glink_offset;
6946
6947             if (info->shared || info->pie)
6948               {
6949                 bfd_vma got = 0;
6950
6951                 if (ent->addend >= 32768)
6952                   got = (ent->addend
6953                          + ent->sec->output_section->vma
6954                          + ent->sec->output_offset);
6955                 else if (htab->elf.hgot != NULL)
6956                   got = (htab->elf.hgot->root.u.def.value
6957                          + htab->elf.hgot->root.u.def.section->output_section->vma
6958                          + htab->elf.hgot->root.u.def.section->output_offset);
6959
6960                 plt -= got;
6961
6962                 if (plt + 0x8000 < 0x10000)
6963                   {
6964                     bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
6965                     p += 4;
6966                     bfd_put_32 (output_bfd, MTCTR_11, p);
6967                     p += 4;
6968                     bfd_put_32 (output_bfd, BCTR, p);
6969                     p += 4;
6970                     bfd_put_32 (output_bfd, NOP, p);
6971                     p += 4;
6972                   }
6973                 else
6974                   {
6975                     bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
6976                     p += 4;
6977                     bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6978                     p += 4;
6979                     bfd_put_32 (output_bfd, MTCTR_11, p);
6980                     p += 4;
6981                     bfd_put_32 (output_bfd, BCTR, p);
6982                     p += 4;
6983                   }
6984               }
6985             else
6986               {
6987                 bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
6988                 p += 4;
6989                 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6990                 p += 4;
6991                 bfd_put_32 (output_bfd, MTCTR_11, p);
6992                 p += 4;
6993                 bfd_put_32 (output_bfd, BCTR, p);
6994                 p += 4;
6995
6996                 /* We only need one non-PIC glink stub.  */
6997                 break;
6998               }
6999           }
7000         else
7001           break;
7002       }
7003
7004   if (h->needs_copy)
7005     {
7006       asection *s;
7007       Elf_Internal_Rela rela;
7008       bfd_byte *loc;
7009
7010       /* This symbols needs a copy reloc.  Set it up.  */
7011
7012 #ifdef DEBUG
7013       fprintf (stderr, ", copy");
7014 #endif
7015
7016       BFD_ASSERT (h->dynindx != -1);
7017
7018       if (ppc_elf_hash_entry (h)->has_sda_refs)
7019         s = htab->relsbss;
7020       else
7021         s = htab->relbss;
7022       BFD_ASSERT (s != NULL);
7023
7024       rela.r_offset = (h->root.u.def.value
7025                        + h->root.u.def.section->output_section->vma
7026                        + h->root.u.def.section->output_offset);
7027       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
7028       rela.r_addend = 0;
7029       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
7030       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
7031     }
7032
7033 #ifdef DEBUG
7034   fprintf (stderr, "\n");
7035 #endif
7036
7037   /* Mark some specially defined symbols as absolute.  */
7038   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
7039       || (!htab->is_vxworks
7040           && (h == htab->elf.hgot
7041               || strcmp (h->root.root.string,
7042                          "_PROCEDURE_LINKAGE_TABLE_") == 0)))
7043     sym->st_shndx = SHN_ABS;
7044
7045   return TRUE;
7046 }
7047 \f
7048 static enum elf_reloc_type_class
7049 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
7050 {
7051   switch (ELF32_R_TYPE (rela->r_info))
7052     {
7053     case R_PPC_RELATIVE:
7054       return reloc_class_relative;
7055     case R_PPC_REL24:
7056     case R_PPC_ADDR24:
7057     case R_PPC_JMP_SLOT:
7058       return reloc_class_plt;
7059     case R_PPC_COPY:
7060       return reloc_class_copy;
7061     default:
7062       return reloc_class_normal;
7063     }
7064 }
7065 \f
7066 /* Finish up the dynamic sections.  */
7067
7068 static bfd_boolean
7069 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
7070                                  struct bfd_link_info *info)
7071 {
7072   asection *sdyn;
7073   asection *splt;
7074   struct ppc_elf_link_hash_table *htab;
7075   bfd_vma got;
7076   bfd * dynobj;
7077
7078 #ifdef DEBUG
7079   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
7080 #endif
7081
7082   htab = ppc_elf_hash_table (info);
7083   dynobj = elf_hash_table (info)->dynobj;
7084   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
7085   if (htab->is_vxworks)
7086     splt = bfd_get_section_by_name (dynobj, ".plt");  
7087   else
7088     splt = NULL;
7089
7090   got = 0;
7091   if (htab->elf.hgot != NULL)
7092     got = (htab->elf.hgot->root.u.def.value
7093            + htab->elf.hgot->root.u.def.section->output_section->vma
7094            + htab->elf.hgot->root.u.def.section->output_offset);
7095
7096   if (htab->elf.dynamic_sections_created)
7097     {
7098       Elf32_External_Dyn *dyncon, *dynconend;
7099
7100       BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
7101
7102       dyncon = (Elf32_External_Dyn *) sdyn->contents;
7103       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
7104       for (; dyncon < dynconend; dyncon++)
7105         {
7106           Elf_Internal_Dyn dyn;
7107           asection *s;
7108
7109           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
7110
7111           switch (dyn.d_tag)
7112             {
7113             case DT_PLTGOT:
7114               if (htab->is_vxworks)
7115                 s = htab->sgotplt;
7116               else
7117                 s = htab->plt;
7118               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
7119               break;
7120
7121             case DT_PLTRELSZ:
7122               dyn.d_un.d_val = htab->relplt->size;
7123               break;
7124
7125             case DT_JMPREL:
7126               s = htab->relplt;
7127               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
7128               break;
7129
7130             case DT_PPC_GOT:
7131               dyn.d_un.d_ptr = got;
7132               break;
7133
7134             case DT_RELASZ:
7135               if (htab->is_vxworks)
7136                 {
7137                   if (htab->relplt)
7138                     dyn.d_un.d_ptr -= htab->relplt->size;
7139                   break;
7140                 }
7141               continue;
7142
7143             default:
7144               continue;
7145             }
7146
7147           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
7148         }
7149     }
7150
7151   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
7152      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
7153   if (htab->got != NULL)
7154     {
7155       unsigned char *p = htab->got->contents;
7156       bfd_vma val;
7157
7158       p += htab->elf.hgot->root.u.def.value;
7159       if (htab->plt_type == PLT_OLD)
7160         bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, p - 4);
7161
7162       val = 0;
7163       if (sdyn != NULL)
7164         val = sdyn->output_section->vma + sdyn->output_offset;
7165       bfd_put_32 (output_bfd, val, p);
7166
7167       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
7168     }
7169
7170   /* Fill in the first entry in the VxWorks procedure linkage table.  */
7171   if (splt && splt->size > 0)
7172     {
7173       /* Use the right PLT. */
7174       static const bfd_vma *plt_entry = NULL;
7175       plt_entry = info->shared ? 
7176         ppc_elf_vxworks_pic_plt0_entry : ppc_elf_vxworks_plt0_entry;
7177
7178       if (!info->shared)
7179         {
7180           bfd_vma got_value =
7181             (htab->elf.hgot->root.u.def.section->output_section->vma
7182              + htab->elf.hgot->root.u.def.section->output_offset
7183              + htab->elf.hgot->root.u.def.value);
7184           bfd_vma got_hi = (got_value >> 16) + ((got_value & 0x8000) >> 15);
7185
7186           bfd_put_32 (output_bfd, plt_entry[0] | (got_hi & 0xffff),
7187                       splt->contents +  0);
7188           bfd_put_32 (output_bfd, plt_entry[1] | (got_value & 0xffff),
7189                       splt->contents +  4);
7190         }
7191       else
7192         {
7193           bfd_put_32 (output_bfd, plt_entry[0], splt->contents +  0);
7194           bfd_put_32 (output_bfd, plt_entry[1], splt->contents +  4);
7195         }
7196       bfd_put_32 (output_bfd, plt_entry[2], splt->contents +  8);
7197       bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
7198       bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
7199       bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
7200       bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
7201       bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
7202
7203       if (! info->shared)
7204         {
7205           Elf_Internal_Rela rela;
7206           bfd_byte *loc;
7207
7208           loc = htab->srelplt2->contents;
7209
7210           /* Output the @ha relocation for the first instruction.  */
7211           rela.r_offset = (htab->plt->output_section->vma
7212                            + htab->plt->output_offset
7213                            + 2);
7214           rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
7215           rela.r_addend = 0;
7216           bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
7217           loc += sizeof (Elf32_External_Rela);
7218           
7219           /* Output the @l relocation for the second instruction.  */
7220           rela.r_offset = (htab->plt->output_section->vma
7221                            + htab->plt->output_offset
7222                            + 6);
7223           rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
7224           rela.r_addend = 0;
7225           bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
7226           loc += sizeof (Elf32_External_Rela);
7227
7228           /* Fix up the remaining relocations.  They may have the wrong
7229              symbol index for _G_O_T_ or _P_L_T_ depending on the order
7230              in which symbols were output.  */
7231           while (loc < htab->srelplt2->contents + htab->srelplt2->size)
7232             {
7233               Elf_Internal_Rela rel;
7234
7235               bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7236               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
7237               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7238               loc += sizeof (Elf32_External_Rela);
7239
7240               bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7241               rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
7242               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7243               loc += sizeof (Elf32_External_Rela);
7244
7245               bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
7246               rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
7247               bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
7248               loc += sizeof (Elf32_External_Rela);
7249             }
7250         }
7251     }
7252
7253   if (htab->glink != NULL && htab->glink->contents != NULL)
7254     {
7255       unsigned char *p;
7256       unsigned char *endp;
7257       bfd_vma res0;
7258       unsigned int i;
7259
7260       /*
7261        * PIC glink code is the following:
7262        *
7263        * # ith PLT code stub.
7264        *   addis 11,30,(plt+(i-1)*4-got)@ha
7265        *   lwz 11,(plt+(i-1)*4-got)@l(11)
7266        *   mtctr 11
7267        *   bctr
7268        *
7269        * # A table of branches, one for each plt entry.
7270        * # The idea is that the plt call stub loads ctr (and r11) with these
7271        * # addresses, so (r11 - res_0) gives the plt index * 4.
7272        * res_0: b PLTresolve
7273        * res_1: b PLTresolve
7274        * .
7275        * # Some number of entries towards the end can be nops
7276        * res_n_m3: nop
7277        * res_n_m2: nop
7278        * res_n_m1:
7279        *
7280        * PLTresolve:
7281        *    addis 11,11,(1f-res_0)@ha
7282        *    mflr 0
7283        *    bcl 20,31,1f
7284        * 1: addi 11,11,(1b-res_0)@l
7285        *    mflr 12
7286        *    mtlr 0
7287        *    sub 11,11,12                # r11 = index * 4
7288        *    addis 12,12,(got+4-1b)@ha
7289        *    lwz 0,(got+4-1b)@l(12)      # got[1] address of dl_runtime_resolve
7290        *    lwz 12,(got+8-1b)@l(12)     # got[2] contains the map address
7291        *    mtctr 0
7292        *    add 0,11,11
7293        *    add 11,0,11                 # r11 = index * 12 = reloc offset.
7294        *    bctr
7295        */
7296       static const unsigned int pic_plt_resolve[] =
7297         {
7298           ADDIS_11_11,
7299           MFLR_0,
7300           BCL_20_31,
7301           ADDI_11_11,
7302           MFLR_12,
7303           MTLR_0,
7304           SUB_11_11_12,
7305           ADDIS_12_12,
7306           LWZ_0_12,
7307           LWZ_12_12,
7308           MTCTR_0,
7309           ADD_0_11_11,
7310           ADD_11_0_11,
7311           BCTR,
7312           NOP,
7313           NOP
7314         };
7315
7316       static const unsigned int plt_resolve[] =
7317         {
7318           LIS_12,
7319           ADDIS_11_11,
7320           LWZ_0_12,
7321           ADDI_11_11,
7322           MTCTR_0,
7323           ADD_0_11_11,
7324           LWZ_12_12,
7325           ADD_11_0_11,
7326           BCTR,
7327           NOP,
7328           NOP,
7329           NOP,
7330           NOP,
7331           NOP,
7332           NOP,
7333           NOP
7334         };
7335
7336       if (ARRAY_SIZE (pic_plt_resolve) != GLINK_PLTRESOLVE / 4)
7337         abort ();
7338       if (ARRAY_SIZE (plt_resolve) != GLINK_PLTRESOLVE / 4)
7339         abort ();
7340
7341       /* Build the branch table, one for each plt entry (less one),
7342          and perhaps some padding.  */
7343       p = htab->glink->contents;
7344       p += htab->glink_pltresolve;
7345       endp = htab->glink->contents;
7346       endp += htab->glink->size - GLINK_PLTRESOLVE;
7347       while (p < endp - 8 * 4)
7348         {
7349           bfd_put_32 (output_bfd, B + endp - p, p);
7350           p += 4;
7351         }
7352       while (p < endp)
7353         {
7354           bfd_put_32 (output_bfd, NOP, p);
7355           p += 4;
7356         }
7357
7358       res0 = (htab->glink_pltresolve
7359               + htab->glink->output_section->vma
7360               + htab->glink->output_offset);
7361
7362       /* Last comes the PLTresolve stub.  */
7363       if (info->shared || info->pie)
7364         {
7365           bfd_vma bcl;
7366
7367           for (i = 0; i < ARRAY_SIZE (pic_plt_resolve); i++)
7368             {
7369               bfd_put_32 (output_bfd, pic_plt_resolve[i], p);
7370               p += 4;
7371             }
7372           p -= 4 * ARRAY_SIZE (pic_plt_resolve);
7373
7374           bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
7375                  + htab->glink->output_section->vma
7376                  + htab->glink->output_offset);
7377
7378           bfd_put_32 (output_bfd,
7379                       ADDIS_11_11 + PPC_HA (bcl - res0), p + 0*4);
7380           bfd_put_32 (output_bfd,
7381                       ADDI_11_11 + PPC_LO (bcl - res0), p + 3*4);
7382           bfd_put_32 (output_bfd,
7383                       ADDIS_12_12 + PPC_HA (got + 4 - bcl), p + 7*4);
7384           if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
7385             {
7386               bfd_put_32 (output_bfd,
7387                           LWZ_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
7388               bfd_put_32 (output_bfd,
7389                           LWZ_12_12 + PPC_LO (got + 8 - bcl), p + 9*4);
7390             }
7391           else
7392             {
7393               bfd_put_32 (output_bfd,
7394                           LWZU_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
7395               bfd_put_32 (output_bfd,
7396                           LWZ_12_12 + 4, p + 9*4);
7397             }
7398         }
7399       else
7400         {
7401           for (i = 0; i < ARRAY_SIZE (plt_resolve); i++)
7402             {
7403               bfd_put_32 (output_bfd, plt_resolve[i], p);
7404               p += 4;
7405             }
7406           p -= 4 * ARRAY_SIZE (plt_resolve);
7407
7408           bfd_put_32 (output_bfd,
7409                       LIS_12 + PPC_HA (got + 4), p + 0*4);
7410           bfd_put_32 (output_bfd,
7411                       ADDIS_11_11 + PPC_HA (-res0), p + 1*4);
7412           bfd_put_32 (output_bfd,
7413                       ADDI_11_11 + PPC_LO (-res0), p + 3*4);
7414           if (PPC_HA (got + 4) == PPC_HA (got + 8))
7415             {
7416               bfd_put_32 (output_bfd,
7417                           LWZ_0_12 + PPC_LO (got + 4), p + 2*4);
7418               bfd_put_32 (output_bfd,
7419                           LWZ_12_12 + PPC_LO (got + 8), p + 6*4);
7420             }
7421           else
7422             {
7423               bfd_put_32 (output_bfd,
7424                           LWZU_0_12 + PPC_LO (got + 4), p + 2*4);
7425               bfd_put_32 (output_bfd,
7426                           LWZ_12_12 + 4, p + 6*4);
7427             }
7428         }
7429     }
7430
7431   return TRUE;
7432 }
7433 \f
7434 #define TARGET_LITTLE_SYM       bfd_elf32_powerpcle_vec
7435 #define TARGET_LITTLE_NAME      "elf32-powerpcle"
7436 #define TARGET_BIG_SYM          bfd_elf32_powerpc_vec
7437 #define TARGET_BIG_NAME         "elf32-powerpc"
7438 #define ELF_ARCH                bfd_arch_powerpc
7439 #define ELF_MACHINE_CODE        EM_PPC
7440 #ifdef __QNXTARGET__
7441 #define ELF_MAXPAGESIZE         0x1000
7442 #else
7443 #define ELF_MAXPAGESIZE         0x10000
7444 #endif
7445 #define ELF_MINPAGESIZE         0x1000
7446 #define ELF_COMMONPAGESIZE      0x1000
7447 #define elf_info_to_howto       ppc_elf_info_to_howto
7448
7449 #ifdef  EM_CYGNUS_POWERPC
7450 #define ELF_MACHINE_ALT1        EM_CYGNUS_POWERPC
7451 #endif
7452
7453 #ifdef EM_PPC_OLD
7454 #define ELF_MACHINE_ALT2        EM_PPC_OLD
7455 #endif
7456
7457 #define elf_backend_plt_not_loaded      1
7458 #define elf_backend_can_gc_sections     1
7459 #define elf_backend_can_refcount        1
7460 #define elf_backend_rela_normal         1
7461
7462 #define bfd_elf32_mkobject                      ppc_elf_mkobject
7463 #define bfd_elf32_bfd_merge_private_bfd_data    ppc_elf_merge_private_bfd_data
7464 #define bfd_elf32_bfd_relax_section             ppc_elf_relax_section
7465 #define bfd_elf32_bfd_reloc_type_lookup         ppc_elf_reloc_type_lookup
7466 #define bfd_elf32_bfd_set_private_flags         ppc_elf_set_private_flags
7467 #define bfd_elf32_bfd_link_hash_table_create    ppc_elf_link_hash_table_create
7468
7469 #define elf_backend_object_p                    ppc_elf_object_p
7470 #define elf_backend_gc_mark_hook                ppc_elf_gc_mark_hook
7471 #define elf_backend_gc_sweep_hook               ppc_elf_gc_sweep_hook
7472 #define elf_backend_section_from_shdr           ppc_elf_section_from_shdr
7473 #define elf_backend_relocate_section            ppc_elf_relocate_section
7474 #define elf_backend_create_dynamic_sections     ppc_elf_create_dynamic_sections
7475 #define elf_backend_check_relocs                ppc_elf_check_relocs
7476 #define elf_backend_copy_indirect_symbol        ppc_elf_copy_indirect_symbol
7477 #define elf_backend_adjust_dynamic_symbol       ppc_elf_adjust_dynamic_symbol
7478 #define elf_backend_add_symbol_hook             ppc_elf_add_symbol_hook
7479 #define elf_backend_size_dynamic_sections       ppc_elf_size_dynamic_sections
7480 #define elf_backend_finish_dynamic_symbol       ppc_elf_finish_dynamic_symbol
7481 #define elf_backend_finish_dynamic_sections     ppc_elf_finish_dynamic_sections
7482 #define elf_backend_fake_sections               ppc_elf_fake_sections
7483 #define elf_backend_additional_program_headers  ppc_elf_additional_program_headers
7484 #define elf_backend_grok_prstatus               ppc_elf_grok_prstatus
7485 #define elf_backend_grok_psinfo                 ppc_elf_grok_psinfo
7486 #define elf_backend_reloc_type_class            ppc_elf_reloc_type_class
7487 #define elf_backend_begin_write_processing      ppc_elf_begin_write_processing
7488 #define elf_backend_final_write_processing      ppc_elf_final_write_processing
7489 #define elf_backend_write_section               ppc_elf_write_section
7490 #define elf_backend_get_sec_type_attr           ppc_elf_get_sec_type_attr
7491 #define elf_backend_plt_sym_val                 ppc_elf_plt_sym_val
7492 #define elf_backend_action_discarded            ppc_elf_action_discarded
7493
7494 #include "elf32-target.h"
7495
7496 /* VxWorks Target */
7497
7498 #undef TARGET_LITTLE_SYM
7499 #undef TARGET_LITTLE_NAME
7500
7501 #undef TARGET_BIG_SYM
7502 #define TARGET_BIG_SYM          bfd_elf32_powerpc_vxworks_vec
7503 #undef TARGET_BIG_NAME
7504 #define TARGET_BIG_NAME         "elf32-powerpc-vxworks"
7505
7506 /* VxWorks uses the elf default section flags for .plt.  */
7507 static const struct bfd_elf_special_section *
7508 ppc_elf_vxworks_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
7509 {
7510   if (sec->name == NULL)
7511     return NULL;
7512
7513   if (strcmp (sec->name, ".plt") == 0)
7514     return _bfd_elf_get_sec_type_attr (abfd, sec);
7515
7516   return ppc_elf_get_sec_type_attr (abfd, sec);
7517 }
7518
7519 /* Like ppc_elf_link_hash_table_create, but overrides
7520    appropriately for VxWorks.  */
7521 static struct bfd_link_hash_table *
7522 ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
7523 {
7524   struct bfd_link_hash_table *ret;
7525
7526   ret = ppc_elf_link_hash_table_create (abfd);
7527   if (ret)
7528     {
7529       struct ppc_elf_link_hash_table *htab
7530         = (struct ppc_elf_link_hash_table *)ret;
7531       htab->is_vxworks = 1;
7532       htab->plt_type = PLT_VXWORKS;
7533       htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
7534       htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
7535       htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
7536     }
7537   return ret;
7538 }
7539
7540 /* Tweak magic VxWorks symbols as they are loaded.  */
7541 static bfd_boolean
7542 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
7543                                  struct bfd_link_info *info,
7544                                  Elf_Internal_Sym *sym,
7545                                  const char **namep ATTRIBUTE_UNUSED,
7546                                  flagword *flagsp ATTRIBUTE_UNUSED,
7547                                  asection **secp,
7548                                  bfd_vma *valp)
7549 {
7550   if (!elf_vxworks_add_symbol_hook(abfd, info, sym,namep, flagsp, secp,
7551                                    valp))
7552     return FALSE;
7553
7554   return ppc_elf_add_symbol_hook(abfd, info, sym,namep, flagsp, secp, valp);
7555 }
7556
7557 static void
7558 ppc_elf_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
7559 {
7560   ppc_elf_final_write_processing(abfd, linker);
7561   elf_vxworks_final_write_processing(abfd, linker);
7562 }
7563
7564 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
7565    define it.  */
7566 #undef elf_backend_want_plt_sym
7567 #define elf_backend_want_plt_sym                1
7568 #undef elf_backend_want_got_plt
7569 #define elf_backend_want_got_plt                1
7570 #undef elf_backend_got_symbol_offset
7571 #define elf_backend_got_symbol_offset           0
7572 #undef elf_backend_plt_not_loaded
7573 #define elf_backend_plt_not_loaded              0
7574 #undef elf_backend_plt_readonly
7575 #define elf_backend_plt_readonly                1
7576 #undef elf_backend_got_header_size
7577 #define elf_backend_got_header_size             12
7578
7579 #undef bfd_elf32_bfd_link_hash_table_create
7580 #define bfd_elf32_bfd_link_hash_table_create \
7581   ppc_elf_vxworks_link_hash_table_create
7582 #undef elf_backend_add_symbol_hook
7583 #define elf_backend_add_symbol_hook \
7584   ppc_elf_vxworks_add_symbol_hook
7585 #undef elf_backend_link_output_symbol_hook
7586 #define elf_backend_link_output_symbol_hook \
7587   elf_vxworks_link_output_symbol_hook
7588 #undef elf_backend_final_write_processing
7589 #define elf_backend_final_write_processing \
7590   ppc_elf_vxworks_final_write_processing
7591 #undef elf_backend_get_sec_type_attr
7592 #define elf_backend_get_sec_type_attr \
7593   ppc_elf_vxworks_get_sec_type_attr
7594 #undef elf_backend_emit_relocs
7595 #define elf_backend_emit_relocs \
7596   elf_vxworks_emit_relocs
7597
7598 #undef elf32_bed
7599 #define elf32_bed                               ppc_elf_vxworks_bed
7600
7601 #include "elf32-target.h"