* elf32-ppc.c (ppc_elf_create_linker_section): Create symbols in
[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 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., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, 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
36 /* RELA relocations are used here.  */
37
38 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc_elf_unhandled_reloc
41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42
43 /* Branch prediction bit for branch taken relocs.  */
44 #define BRANCH_PREDICT_BIT 0x200000
45 /* Mask to set RA in memory instructions.  */
46 #define RA_REGISTER_MASK 0x001f0000
47 /* Value to shift register by to insert RA.  */
48 #define RA_REGISTER_SHIFT 16
49
50 /* The name of the dynamic interpreter.  This is put in the .interp
51    section.  */
52 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
53
54 /* The size in bytes of an entry in the procedure linkage table.  */
55 #define PLT_ENTRY_SIZE 12
56 /* The initial size of the plt reserved for the dynamic linker.  */
57 #define PLT_INITIAL_ENTRY_SIZE 72
58 /* The size of the gap between entries in the PLT.  */
59 #define PLT_SLOT_SIZE 8
60 /* The number of single-slot PLT entries (the rest use two slots).  */
61 #define PLT_NUM_SINGLE_ENTRIES 8192
62
63 /* Some nop instructions.  */
64 #define NOP             0x60000000
65 #define CROR_151515     0x4def7b82
66 #define CROR_313131     0x4ffffb82
67
68 /* Offset of tp and dtp pointers from start of TLS block.  */
69 #define TP_OFFSET       0x7000
70 #define DTP_OFFSET      0x8000
71
72 \f
73 /* Enumeration to specify the special section.  */
74 enum elf_linker_section_enum
75 {
76   LINKER_SECTION_SDATA,
77   LINKER_SECTION_SDATA2
78 };
79
80 /* Sections created by the linker.  */
81
82 typedef struct elf_linker_section
83 {
84   /* pointer to the section */
85   asection *section;
86   /* pointer to the relocations needed for this section */
87   asection *rel_section;
88   /* pointer to the created symbol hash value */
89   struct elf_link_hash_entry *sym_hash;
90   /* offset of symbol from beginning of section */
91   bfd_vma sym_offset;
92 } elf_linker_section_t;
93
94 /* Linked list of allocated pointer entries.  This hangs off of the
95    symbol lists, and provides allows us to return different pointers,
96    based on different addend's.  */
97
98 typedef struct elf_linker_section_pointers
99 {
100   /* next allocated pointer for this symbol */
101   struct elf_linker_section_pointers *next;
102   /* offset of pointer from beginning of section */
103   bfd_vma offset;
104   /* addend used */
105   bfd_vma addend;
106   /* which linker section this is */
107   elf_linker_section_t *lsect;
108   /* whether address was written yet */
109   bfd_boolean written_address_p;
110 } elf_linker_section_pointers_t;
111
112 struct ppc_elf_obj_tdata
113 {
114   struct elf_obj_tdata elf;
115
116   /* A mapping from local symbols to offsets into the various linker
117      sections added.  This is index by the symbol index.  */
118   elf_linker_section_pointers_t **linker_section_pointers;
119 };
120
121 #define ppc_elf_tdata(bfd) \
122   ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
123
124 #define elf_local_ptr_offsets(bfd) \
125   (ppc_elf_tdata (bfd)->linker_section_pointers)
126
127 /* Override the generic function because we store some extras.  */
128
129 static bfd_boolean
130 ppc_elf_mkobject (bfd *abfd)
131 {
132   bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
133   abfd->tdata.any = bfd_zalloc (abfd, amt);
134   if (abfd->tdata.any == NULL)
135     return FALSE;
136   return TRUE;
137 }
138
139 /* The PPC linker needs to keep track of the number of relocs that it
140    decides to copy as dynamic relocs in check_relocs for each symbol.
141    This is so that it can later discard them if they are found to be
142    unnecessary.  We store the information in a field extending the
143    regular ELF linker hash table.  */
144
145 struct ppc_elf_dyn_relocs
146 {
147   struct ppc_elf_dyn_relocs *next;
148
149   /* The input section of the reloc.  */
150   asection *sec;
151
152   /* Total number of relocs copied for the input section.  */
153   bfd_size_type count;
154
155   /* Number of pc-relative relocs copied for the input section.  */
156   bfd_size_type pc_count;
157 };
158
159 /* PPC ELF linker hash entry.  */
160
161 struct ppc_elf_link_hash_entry
162 {
163   struct elf_link_hash_entry elf;
164
165   /* If this symbol is used in the linker created sections, the processor
166      specific backend uses this field to map the field into the offset
167      from the beginning of the section.  */
168   elf_linker_section_pointers_t *linker_section_pointer;
169
170   /* Track dynamic relocs copied for this symbol.  */
171   struct ppc_elf_dyn_relocs *dyn_relocs;
172
173   /* Contexts in which symbol is used in the GOT (or TOC).
174      TLS_GD .. TLS_TLS bits are or'd into the mask as the
175      corresponding relocs are encountered during check_relocs.
176      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
177      indicate the corresponding GOT entry type is not needed.  */
178 #define TLS_GD           1      /* GD reloc. */
179 #define TLS_LD           2      /* LD reloc. */
180 #define TLS_TPREL        4      /* TPREL reloc, => IE. */
181 #define TLS_DTPREL       8      /* DTPREL reloc, => LD. */
182 #define TLS_TLS         16      /* Any TLS reloc.  */
183 #define TLS_TPRELGD     32      /* TPREL reloc resulting from GD->IE. */
184   char tls_mask;
185 };
186
187 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
188
189 /* PPC ELF linker hash table.  */
190
191 struct ppc_elf_link_hash_table
192 {
193   struct elf_link_hash_table elf;
194
195   /* Short-cuts to get to dynamic linker sections.  */
196   asection *got;
197   asection *relgot;
198   asection *plt;
199   asection *relplt;
200   asection *dynbss;
201   asection *relbss;
202   asection *dynsbss;
203   asection *relsbss;
204   elf_linker_section_t *sdata;
205   elf_linker_section_t *sdata2;
206   asection *sbss;
207
208   /* Shortcut to .__tls_get_addr.  */
209   struct elf_link_hash_entry *tls_get_addr;
210
211   /* TLS local dynamic got entry handling.  */
212   union {
213     bfd_signed_vma refcount;
214     bfd_vma offset;
215   } tlsld_got;
216
217   /* Small local sym to section mapping cache.  */
218   struct sym_sec_cache sym_sec;
219 };
220
221 /* Get the PPC ELF linker hash table from a link_info structure.  */
222
223 #define ppc_elf_hash_table(p) \
224   ((struct ppc_elf_link_hash_table *) (p)->hash)
225
226 /* Create an entry in a PPC ELF linker hash table.  */
227
228 static struct bfd_hash_entry *
229 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
230                            struct bfd_hash_table *table,
231                            const char *string)
232 {
233   /* Allocate the structure if it has not already been allocated by a
234      subclass.  */
235   if (entry == NULL)
236     {
237       entry = bfd_hash_allocate (table,
238                                  sizeof (struct ppc_elf_link_hash_entry));
239       if (entry == NULL)
240         return entry;
241     }
242
243   /* Call the allocation method of the superclass.  */
244   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
245   if (entry != NULL)
246     {
247       ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
248       ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
249       ppc_elf_hash_entry (entry)->tls_mask = 0;
250     }
251
252   return entry;
253 }
254
255 /* Create a PPC ELF linker hash table.  */
256
257 static struct bfd_link_hash_table *
258 ppc_elf_link_hash_table_create (bfd *abfd)
259 {
260   struct ppc_elf_link_hash_table *ret;
261
262   ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
263   if (ret == NULL)
264     return NULL;
265
266   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
267                                        ppc_elf_link_hash_newfunc))
268     {
269       free (ret);
270       return NULL;
271     }
272
273   return &ret->elf.root;
274 }
275
276 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
277    copying dynamic variables from a shared lib into an app's dynbss
278    section, and instead use a dynamic relocation to point into the
279    shared lib.  */
280 #define ELIMINATE_COPY_RELOCS 1
281
282 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
283
284 static void
285 ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
286                               struct elf_link_hash_entry *dir,
287                               struct elf_link_hash_entry *ind)
288 {
289   struct ppc_elf_link_hash_entry *edir, *eind;
290
291   edir = (struct ppc_elf_link_hash_entry *) dir;
292   eind = (struct ppc_elf_link_hash_entry *) ind;
293
294   if (eind->dyn_relocs != NULL)
295     {
296       if (edir->dyn_relocs != NULL)
297         {
298           struct ppc_elf_dyn_relocs **pp;
299           struct ppc_elf_dyn_relocs *p;
300
301           if (ind->root.type == bfd_link_hash_indirect)
302             abort ();
303
304           /* Add reloc counts against the weak sym to the strong sym
305              list.  Merge any entries against the same section.  */
306           for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
307             {
308               struct ppc_elf_dyn_relocs *q;
309
310               for (q = edir->dyn_relocs; q != NULL; q = q->next)
311                 if (q->sec == p->sec)
312                   {
313                     q->pc_count += p->pc_count;
314                     q->count += p->count;
315                     *pp = p->next;
316                     break;
317                   }
318               if (q == NULL)
319                 pp = &p->next;
320             }
321           *pp = edir->dyn_relocs;
322         }
323
324       edir->dyn_relocs = eind->dyn_relocs;
325       eind->dyn_relocs = NULL;
326     }
327
328   edir->tls_mask |= eind->tls_mask;
329
330   if (ELIMINATE_COPY_RELOCS
331       && ind->root.type != bfd_link_hash_indirect
332       && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
333     /* If called to transfer flags for a weakdef during processing
334        of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
335        We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
336     dir->elf_link_hash_flags |=
337       (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
338                                    | ELF_LINK_HASH_REF_REGULAR
339                                    | ELF_LINK_HASH_REF_REGULAR_NONWEAK
340                                    | ELF_LINK_HASH_NEEDS_PLT));
341   else
342     _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
343 }
344 \f
345 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
346
347 static reloc_howto_type ppc_elf_howto_raw[] = {
348   /* This reloc does nothing.  */
349   HOWTO (R_PPC_NONE,            /* type */
350          0,                     /* rightshift */
351          2,                     /* size (0 = byte, 1 = short, 2 = long) */
352          32,                    /* bitsize */
353          FALSE,                 /* pc_relative */
354          0,                     /* bitpos */
355          complain_overflow_bitfield, /* complain_on_overflow */
356          bfd_elf_generic_reloc, /* special_function */
357          "R_PPC_NONE",          /* name */
358          FALSE,                 /* partial_inplace */
359          0,                     /* src_mask */
360          0,                     /* dst_mask */
361          FALSE),                /* pcrel_offset */
362
363   /* A standard 32 bit relocation.  */
364   HOWTO (R_PPC_ADDR32,          /* type */
365          0,                     /* rightshift */
366          2,                     /* size (0 = byte, 1 = short, 2 = long) */
367          32,                    /* bitsize */
368          FALSE,                 /* pc_relative */
369          0,                     /* bitpos */
370          complain_overflow_bitfield, /* complain_on_overflow */
371          bfd_elf_generic_reloc, /* special_function */
372          "R_PPC_ADDR32",        /* name */
373          FALSE,                 /* partial_inplace */
374          0,                     /* src_mask */
375          0xffffffff,            /* dst_mask */
376          FALSE),                /* pcrel_offset */
377
378   /* An absolute 26 bit branch; the lower two bits must be zero.
379      FIXME: we don't check that, we just clear them.  */
380   HOWTO (R_PPC_ADDR24,          /* type */
381          0,                     /* rightshift */
382          2,                     /* size (0 = byte, 1 = short, 2 = long) */
383          26,                    /* bitsize */
384          FALSE,                 /* pc_relative */
385          0,                     /* bitpos */
386          complain_overflow_bitfield, /* complain_on_overflow */
387          bfd_elf_generic_reloc, /* special_function */
388          "R_PPC_ADDR24",        /* name */
389          FALSE,                 /* partial_inplace */
390          0,                     /* src_mask */
391          0x3fffffc,             /* dst_mask */
392          FALSE),                /* pcrel_offset */
393
394   /* A standard 16 bit relocation.  */
395   HOWTO (R_PPC_ADDR16,          /* type */
396          0,                     /* rightshift */
397          1,                     /* size (0 = byte, 1 = short, 2 = long) */
398          16,                    /* bitsize */
399          FALSE,                 /* pc_relative */
400          0,                     /* bitpos */
401          complain_overflow_bitfield, /* complain_on_overflow */
402          bfd_elf_generic_reloc, /* special_function */
403          "R_PPC_ADDR16",        /* name */
404          FALSE,                 /* partial_inplace */
405          0,                     /* src_mask */
406          0xffff,                /* dst_mask */
407          FALSE),                /* pcrel_offset */
408
409   /* A 16 bit relocation without overflow.  */
410   HOWTO (R_PPC_ADDR16_LO,       /* type */
411          0,                     /* rightshift */
412          1,                     /* size (0 = byte, 1 = short, 2 = long) */
413          16,                    /* bitsize */
414          FALSE,                 /* pc_relative */
415          0,                     /* bitpos */
416          complain_overflow_dont,/* complain_on_overflow */
417          bfd_elf_generic_reloc, /* special_function */
418          "R_PPC_ADDR16_LO",     /* name */
419          FALSE,                 /* partial_inplace */
420          0,                     /* src_mask */
421          0xffff,                /* dst_mask */
422          FALSE),                /* pcrel_offset */
423
424   /* The high order 16 bits of an address.  */
425   HOWTO (R_PPC_ADDR16_HI,       /* type */
426          16,                    /* rightshift */
427          1,                     /* size (0 = byte, 1 = short, 2 = long) */
428          16,                    /* bitsize */
429          FALSE,                 /* pc_relative */
430          0,                     /* bitpos */
431          complain_overflow_dont, /* complain_on_overflow */
432          bfd_elf_generic_reloc, /* special_function */
433          "R_PPC_ADDR16_HI",     /* name */
434          FALSE,                 /* partial_inplace */
435          0,                     /* src_mask */
436          0xffff,                /* dst_mask */
437          FALSE),                /* pcrel_offset */
438
439   /* The high order 16 bits of an address, plus 1 if the contents of
440      the low 16 bits, treated as a signed number, is negative.  */
441   HOWTO (R_PPC_ADDR16_HA,       /* type */
442          16,                    /* rightshift */
443          1,                     /* size (0 = byte, 1 = short, 2 = long) */
444          16,                    /* bitsize */
445          FALSE,                 /* pc_relative */
446          0,                     /* bitpos */
447          complain_overflow_dont, /* complain_on_overflow */
448          ppc_elf_addr16_ha_reloc, /* special_function */
449          "R_PPC_ADDR16_HA",     /* name */
450          FALSE,                 /* partial_inplace */
451          0,                     /* src_mask */
452          0xffff,                /* dst_mask */
453          FALSE),                /* pcrel_offset */
454
455   /* An absolute 16 bit branch; the lower two bits must be zero.
456      FIXME: we don't check that, we just clear them.  */
457   HOWTO (R_PPC_ADDR14,          /* type */
458          0,                     /* rightshift */
459          2,                     /* size (0 = byte, 1 = short, 2 = long) */
460          16,                    /* bitsize */
461          FALSE,                 /* pc_relative */
462          0,                     /* bitpos */
463          complain_overflow_bitfield, /* complain_on_overflow */
464          bfd_elf_generic_reloc, /* special_function */
465          "R_PPC_ADDR14",        /* name */
466          FALSE,                 /* partial_inplace */
467          0,                     /* src_mask */
468          0xfffc,                /* dst_mask */
469          FALSE),                /* pcrel_offset */
470
471   /* An absolute 16 bit branch, for which bit 10 should be set to
472      indicate that the branch is expected to be taken.  The lower two
473      bits must be zero.  */
474   HOWTO (R_PPC_ADDR14_BRTAKEN,  /* type */
475          0,                     /* rightshift */
476          2,                     /* size (0 = byte, 1 = short, 2 = long) */
477          16,                    /* bitsize */
478          FALSE,                 /* pc_relative */
479          0,                     /* bitpos */
480          complain_overflow_bitfield, /* complain_on_overflow */
481          bfd_elf_generic_reloc, /* special_function */
482          "R_PPC_ADDR14_BRTAKEN",/* name */
483          FALSE,                 /* partial_inplace */
484          0,                     /* src_mask */
485          0xfffc,                /* dst_mask */
486          FALSE),                /* pcrel_offset */
487
488   /* An absolute 16 bit branch, for which bit 10 should be set to
489      indicate that the branch is not expected to be taken.  The lower
490      two bits must be zero.  */
491   HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
492          0,                     /* rightshift */
493          2,                     /* size (0 = byte, 1 = short, 2 = long) */
494          16,                    /* bitsize */
495          FALSE,                 /* pc_relative */
496          0,                     /* bitpos */
497          complain_overflow_bitfield, /* complain_on_overflow */
498          bfd_elf_generic_reloc, /* special_function */
499          "R_PPC_ADDR14_BRNTAKEN",/* name */
500          FALSE,                 /* partial_inplace */
501          0,                     /* src_mask */
502          0xfffc,                /* dst_mask */
503          FALSE),                /* pcrel_offset */
504
505   /* A relative 26 bit branch; the lower two bits must be zero.  */
506   HOWTO (R_PPC_REL24,           /* type */
507          0,                     /* rightshift */
508          2,                     /* size (0 = byte, 1 = short, 2 = long) */
509          26,                    /* bitsize */
510          TRUE,                  /* pc_relative */
511          0,                     /* bitpos */
512          complain_overflow_signed, /* complain_on_overflow */
513          bfd_elf_generic_reloc, /* special_function */
514          "R_PPC_REL24",         /* name */
515          FALSE,                 /* partial_inplace */
516          0,                     /* src_mask */
517          0x3fffffc,             /* dst_mask */
518          TRUE),                 /* pcrel_offset */
519
520   /* A relative 16 bit branch; the lower two bits must be zero.  */
521   HOWTO (R_PPC_REL14,           /* type */
522          0,                     /* rightshift */
523          2,                     /* size (0 = byte, 1 = short, 2 = long) */
524          16,                    /* bitsize */
525          TRUE,                  /* pc_relative */
526          0,                     /* bitpos */
527          complain_overflow_signed, /* complain_on_overflow */
528          bfd_elf_generic_reloc, /* special_function */
529          "R_PPC_REL14",         /* name */
530          FALSE,                 /* partial_inplace */
531          0,                     /* src_mask */
532          0xfffc,                /* dst_mask */
533          TRUE),                 /* pcrel_offset */
534
535   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
536      the branch is expected to be taken.  The lower two bits must be
537      zero.  */
538   HOWTO (R_PPC_REL14_BRTAKEN,   /* type */
539          0,                     /* rightshift */
540          2,                     /* size (0 = byte, 1 = short, 2 = long) */
541          16,                    /* bitsize */
542          TRUE,                  /* pc_relative */
543          0,                     /* bitpos */
544          complain_overflow_signed, /* complain_on_overflow */
545          bfd_elf_generic_reloc, /* special_function */
546          "R_PPC_REL14_BRTAKEN", /* name */
547          FALSE,                 /* partial_inplace */
548          0,                     /* src_mask */
549          0xfffc,                /* dst_mask */
550          TRUE),                 /* pcrel_offset */
551
552   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
553      the branch is not expected to be taken.  The lower two bits must
554      be zero.  */
555   HOWTO (R_PPC_REL14_BRNTAKEN,  /* type */
556          0,                     /* rightshift */
557          2,                     /* size (0 = byte, 1 = short, 2 = long) */
558          16,                    /* bitsize */
559          TRUE,                  /* pc_relative */
560          0,                     /* bitpos */
561          complain_overflow_signed, /* complain_on_overflow */
562          bfd_elf_generic_reloc, /* special_function */
563          "R_PPC_REL14_BRNTAKEN",/* name */
564          FALSE,                 /* partial_inplace */
565          0,                     /* src_mask */
566          0xfffc,                /* dst_mask */
567          TRUE),                 /* pcrel_offset */
568
569   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
570      symbol.  */
571   HOWTO (R_PPC_GOT16,           /* type */
572          0,                     /* rightshift */
573          1,                     /* size (0 = byte, 1 = short, 2 = long) */
574          16,                    /* bitsize */
575          FALSE,                 /* pc_relative */
576          0,                     /* bitpos */
577          complain_overflow_signed, /* complain_on_overflow */
578          bfd_elf_generic_reloc, /* special_function */
579          "R_PPC_GOT16",         /* name */
580          FALSE,                 /* partial_inplace */
581          0,                     /* src_mask */
582          0xffff,                /* dst_mask */
583          FALSE),                /* pcrel_offset */
584
585   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
586      the symbol.  */
587   HOWTO (R_PPC_GOT16_LO,        /* type */
588          0,                     /* rightshift */
589          1,                     /* size (0 = byte, 1 = short, 2 = long) */
590          16,                    /* bitsize */
591          FALSE,                 /* pc_relative */
592          0,                     /* bitpos */
593          complain_overflow_dont, /* complain_on_overflow */
594          bfd_elf_generic_reloc, /* special_function */
595          "R_PPC_GOT16_LO",      /* name */
596          FALSE,                 /* partial_inplace */
597          0,                     /* src_mask */
598          0xffff,                /* dst_mask */
599          FALSE),                /* pcrel_offset */
600
601   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
602      the symbol.  */
603   HOWTO (R_PPC_GOT16_HI,        /* type */
604          16,                    /* rightshift */
605          1,                     /* size (0 = byte, 1 = short, 2 = long) */
606          16,                    /* bitsize */
607          FALSE,                 /* pc_relative */
608          0,                     /* bitpos */
609          complain_overflow_bitfield, /* complain_on_overflow */
610          bfd_elf_generic_reloc, /* special_function */
611          "R_PPC_GOT16_HI",      /* name */
612          FALSE,                 /* partial_inplace */
613          0,                     /* src_mask */
614          0xffff,                /* dst_mask */
615          FALSE),                 /* pcrel_offset */
616
617   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
618      the symbol.  */
619   HOWTO (R_PPC_GOT16_HA,        /* type */
620          16,                    /* rightshift */
621          1,                     /* size (0 = byte, 1 = short, 2 = long) */
622          16,                    /* bitsize */
623          FALSE,                 /* pc_relative */
624          0,                     /* bitpos */
625          complain_overflow_bitfield, /* complain_on_overflow */
626          ppc_elf_addr16_ha_reloc, /* special_function */
627          "R_PPC_GOT16_HA",      /* name */
628          FALSE,                 /* partial_inplace */
629          0,                     /* src_mask */
630          0xffff,                /* dst_mask */
631          FALSE),                /* pcrel_offset */
632
633   /* Like R_PPC_REL24, but referring to the procedure linkage table
634      entry for the symbol.  */
635   HOWTO (R_PPC_PLTREL24,        /* type */
636          0,                     /* rightshift */
637          2,                     /* size (0 = byte, 1 = short, 2 = long) */
638          26,                    /* bitsize */
639          TRUE,                  /* pc_relative */
640          0,                     /* bitpos */
641          complain_overflow_signed,  /* complain_on_overflow */
642          bfd_elf_generic_reloc, /* special_function */
643          "R_PPC_PLTREL24",      /* name */
644          FALSE,                 /* partial_inplace */
645          0,                     /* src_mask */
646          0x3fffffc,             /* dst_mask */
647          TRUE),                 /* pcrel_offset */
648
649   /* This is used only by the dynamic linker.  The symbol should exist
650      both in the object being run and in some shared library.  The
651      dynamic linker copies the data addressed by the symbol from the
652      shared library into the object, because the object being
653      run has to have the data at some particular address.  */
654   HOWTO (R_PPC_COPY,            /* type */
655          0,                     /* rightshift */
656          2,                     /* size (0 = byte, 1 = short, 2 = long) */
657          32,                    /* bitsize */
658          FALSE,                 /* pc_relative */
659          0,                     /* bitpos */
660          complain_overflow_bitfield, /* complain_on_overflow */
661          bfd_elf_generic_reloc,  /* special_function */
662          "R_PPC_COPY",          /* name */
663          FALSE,                 /* partial_inplace */
664          0,                     /* src_mask */
665          0,                     /* dst_mask */
666          FALSE),                /* pcrel_offset */
667
668   /* Like R_PPC_ADDR32, but used when setting global offset table
669      entries.  */
670   HOWTO (R_PPC_GLOB_DAT,        /* type */
671          0,                     /* rightshift */
672          2,                     /* size (0 = byte, 1 = short, 2 = long) */
673          32,                    /* bitsize */
674          FALSE,                 /* pc_relative */
675          0,                     /* bitpos */
676          complain_overflow_bitfield, /* complain_on_overflow */
677          bfd_elf_generic_reloc,  /* special_function */
678          "R_PPC_GLOB_DAT",      /* name */
679          FALSE,                 /* partial_inplace */
680          0,                     /* src_mask */
681          0xffffffff,            /* dst_mask */
682          FALSE),                /* pcrel_offset */
683
684   /* Marks a procedure linkage table entry for a symbol.  */
685   HOWTO (R_PPC_JMP_SLOT,        /* type */
686          0,                     /* rightshift */
687          2,                     /* size (0 = byte, 1 = short, 2 = long) */
688          32,                    /* bitsize */
689          FALSE,                 /* pc_relative */
690          0,                     /* bitpos */
691          complain_overflow_bitfield, /* complain_on_overflow */
692          bfd_elf_generic_reloc,  /* special_function */
693          "R_PPC_JMP_SLOT",      /* name */
694          FALSE,                 /* partial_inplace */
695          0,                     /* src_mask */
696          0,                     /* dst_mask */
697          FALSE),                /* pcrel_offset */
698
699   /* Used only by the dynamic linker.  When the object is run, this
700      longword is set to the load address of the object, plus the
701      addend.  */
702   HOWTO (R_PPC_RELATIVE,        /* type */
703          0,                     /* rightshift */
704          2,                     /* size (0 = byte, 1 = short, 2 = long) */
705          32,                    /* bitsize */
706          FALSE,                 /* pc_relative */
707          0,                     /* bitpos */
708          complain_overflow_bitfield, /* complain_on_overflow */
709          bfd_elf_generic_reloc,  /* special_function */
710          "R_PPC_RELATIVE",      /* name */
711          FALSE,                 /* partial_inplace */
712          0,                     /* src_mask */
713          0xffffffff,            /* dst_mask */
714          FALSE),                /* pcrel_offset */
715
716   /* Like R_PPC_REL24, but uses the value of the symbol within the
717      object rather than the final value.  Normally used for
718      _GLOBAL_OFFSET_TABLE_.  */
719   HOWTO (R_PPC_LOCAL24PC,       /* type */
720          0,                     /* rightshift */
721          2,                     /* size (0 = byte, 1 = short, 2 = long) */
722          26,                    /* bitsize */
723          TRUE,                  /* pc_relative */
724          0,                     /* bitpos */
725          complain_overflow_signed, /* complain_on_overflow */
726          bfd_elf_generic_reloc, /* special_function */
727          "R_PPC_LOCAL24PC",     /* name */
728          FALSE,                 /* partial_inplace */
729          0,                     /* src_mask */
730          0x3fffffc,             /* dst_mask */
731          TRUE),                 /* pcrel_offset */
732
733   /* Like R_PPC_ADDR32, but may be unaligned.  */
734   HOWTO (R_PPC_UADDR32,         /* type */
735          0,                     /* rightshift */
736          2,                     /* size (0 = byte, 1 = short, 2 = long) */
737          32,                    /* bitsize */
738          FALSE,                 /* pc_relative */
739          0,                     /* bitpos */
740          complain_overflow_bitfield, /* complain_on_overflow */
741          bfd_elf_generic_reloc, /* special_function */
742          "R_PPC_UADDR32",       /* name */
743          FALSE,                 /* partial_inplace */
744          0,                     /* src_mask */
745          0xffffffff,            /* dst_mask */
746          FALSE),                /* pcrel_offset */
747
748   /* Like R_PPC_ADDR16, but may be unaligned.  */
749   HOWTO (R_PPC_UADDR16,         /* type */
750          0,                     /* rightshift */
751          1,                     /* size (0 = byte, 1 = short, 2 = long) */
752          16,                    /* bitsize */
753          FALSE,                 /* pc_relative */
754          0,                     /* bitpos */
755          complain_overflow_bitfield, /* complain_on_overflow */
756          bfd_elf_generic_reloc, /* special_function */
757          "R_PPC_UADDR16",       /* name */
758          FALSE,                 /* partial_inplace */
759          0,                     /* src_mask */
760          0xffff,                /* dst_mask */
761          FALSE),                /* pcrel_offset */
762
763   /* 32-bit PC relative */
764   HOWTO (R_PPC_REL32,           /* type */
765          0,                     /* rightshift */
766          2,                     /* size (0 = byte, 1 = short, 2 = long) */
767          32,                    /* bitsize */
768          TRUE,                  /* pc_relative */
769          0,                     /* bitpos */
770          complain_overflow_bitfield, /* complain_on_overflow */
771          bfd_elf_generic_reloc, /* special_function */
772          "R_PPC_REL32",         /* name */
773          FALSE,                 /* partial_inplace */
774          0,                     /* src_mask */
775          0xffffffff,            /* dst_mask */
776          TRUE),                 /* pcrel_offset */
777
778   /* 32-bit relocation to the symbol's procedure linkage table.
779      FIXME: not supported.  */
780   HOWTO (R_PPC_PLT32,           /* type */
781          0,                     /* rightshift */
782          2,                     /* size (0 = byte, 1 = short, 2 = long) */
783          32,                    /* bitsize */
784          FALSE,                 /* pc_relative */
785          0,                     /* bitpos */
786          complain_overflow_bitfield, /* complain_on_overflow */
787          bfd_elf_generic_reloc, /* special_function */
788          "R_PPC_PLT32",         /* name */
789          FALSE,                 /* partial_inplace */
790          0,                     /* src_mask */
791          0,                     /* dst_mask */
792          FALSE),                /* pcrel_offset */
793
794   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
795      FIXME: not supported.  */
796   HOWTO (R_PPC_PLTREL32,        /* type */
797          0,                     /* rightshift */
798          2,                     /* size (0 = byte, 1 = short, 2 = long) */
799          32,                    /* bitsize */
800          TRUE,                  /* pc_relative */
801          0,                     /* bitpos */
802          complain_overflow_bitfield, /* complain_on_overflow */
803          bfd_elf_generic_reloc, /* special_function */
804          "R_PPC_PLTREL32",      /* name */
805          FALSE,                 /* partial_inplace */
806          0,                     /* src_mask */
807          0,                     /* dst_mask */
808          TRUE),                 /* pcrel_offset */
809
810   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
811      the symbol.  */
812   HOWTO (R_PPC_PLT16_LO,        /* type */
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          bfd_elf_generic_reloc, /* special_function */
820          "R_PPC_PLT16_LO",      /* name */
821          FALSE,                 /* partial_inplace */
822          0,                     /* src_mask */
823          0xffff,                /* dst_mask */
824          FALSE),                /* pcrel_offset */
825
826   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
827      the symbol.  */
828   HOWTO (R_PPC_PLT16_HI,        /* type */
829          16,                    /* rightshift */
830          1,                     /* size (0 = byte, 1 = short, 2 = long) */
831          16,                    /* bitsize */
832          FALSE,                 /* pc_relative */
833          0,                     /* bitpos */
834          complain_overflow_bitfield, /* complain_on_overflow */
835          bfd_elf_generic_reloc, /* special_function */
836          "R_PPC_PLT16_HI",      /* name */
837          FALSE,                 /* partial_inplace */
838          0,                     /* src_mask */
839          0xffff,                /* dst_mask */
840          FALSE),                 /* pcrel_offset */
841
842   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
843      the symbol.  */
844   HOWTO (R_PPC_PLT16_HA,        /* type */
845          16,                    /* rightshift */
846          1,                     /* size (0 = byte, 1 = short, 2 = long) */
847          16,                    /* bitsize */
848          FALSE,                 /* pc_relative */
849          0,                     /* bitpos */
850          complain_overflow_bitfield, /* complain_on_overflow */
851          ppc_elf_addr16_ha_reloc, /* special_function */
852          "R_PPC_PLT16_HA",      /* name */
853          FALSE,                 /* partial_inplace */
854          0,                     /* src_mask */
855          0xffff,                /* dst_mask */
856          FALSE),                /* pcrel_offset */
857
858   /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
859      small data items.  */
860   HOWTO (R_PPC_SDAREL16,        /* type */
861          0,                     /* rightshift */
862          1,                     /* size (0 = byte, 1 = short, 2 = long) */
863          16,                    /* bitsize */
864          FALSE,                 /* pc_relative */
865          0,                     /* bitpos */
866          complain_overflow_signed, /* complain_on_overflow */
867          bfd_elf_generic_reloc, /* special_function */
868          "R_PPC_SDAREL16",      /* name */
869          FALSE,                 /* partial_inplace */
870          0,                     /* src_mask */
871          0xffff,                /* dst_mask */
872          FALSE),                /* pcrel_offset */
873
874   /* 16-bit section relative relocation.  */
875   HOWTO (R_PPC_SECTOFF,         /* type */
876          0,                     /* rightshift */
877          1,                     /* size (0 = byte, 1 = short, 2 = long) */
878          16,                    /* bitsize */
879          FALSE,                 /* pc_relative */
880          0,                     /* bitpos */
881          complain_overflow_bitfield, /* complain_on_overflow */
882          bfd_elf_generic_reloc, /* special_function */
883          "R_PPC_SECTOFF",       /* name */
884          FALSE,                 /* partial_inplace */
885          0,                     /* src_mask */
886          0xffff,                /* dst_mask */
887          FALSE),                /* pcrel_offset */
888
889   /* 16-bit lower half section relative relocation.  */
890   HOWTO (R_PPC_SECTOFF_LO,        /* type */
891          0,                     /* rightshift */
892          1,                     /* size (0 = byte, 1 = short, 2 = long) */
893          16,                    /* bitsize */
894          FALSE,                 /* pc_relative */
895          0,                     /* bitpos */
896          complain_overflow_dont, /* complain_on_overflow */
897          bfd_elf_generic_reloc, /* special_function */
898          "R_PPC_SECTOFF_LO",    /* name */
899          FALSE,                 /* partial_inplace */
900          0,                     /* src_mask */
901          0xffff,                /* dst_mask */
902          FALSE),                /* pcrel_offset */
903
904   /* 16-bit upper half section relative relocation.  */
905   HOWTO (R_PPC_SECTOFF_HI,      /* type */
906          16,                    /* rightshift */
907          1,                     /* size (0 = byte, 1 = short, 2 = long) */
908          16,                    /* bitsize */
909          FALSE,                 /* pc_relative */
910          0,                     /* bitpos */
911          complain_overflow_bitfield, /* complain_on_overflow */
912          bfd_elf_generic_reloc, /* special_function */
913          "R_PPC_SECTOFF_HI",    /* name */
914          FALSE,                 /* partial_inplace */
915          0,                     /* src_mask */
916          0xffff,                /* dst_mask */
917          FALSE),                 /* pcrel_offset */
918
919   /* 16-bit upper half adjusted section relative relocation.  */
920   HOWTO (R_PPC_SECTOFF_HA,      /* type */
921          16,                    /* rightshift */
922          1,                     /* size (0 = byte, 1 = short, 2 = long) */
923          16,                    /* bitsize */
924          FALSE,                 /* pc_relative */
925          0,                     /* bitpos */
926          complain_overflow_bitfield, /* complain_on_overflow */
927          ppc_elf_addr16_ha_reloc, /* special_function */
928          "R_PPC_SECTOFF_HA",    /* name */
929          FALSE,                 /* partial_inplace */
930          0,                     /* src_mask */
931          0xffff,                /* dst_mask */
932          FALSE),                /* pcrel_offset */
933
934   /* Marker reloc for TLS.  */
935   HOWTO (R_PPC_TLS,
936          0,                     /* rightshift */
937          2,                     /* size (0 = byte, 1 = short, 2 = long) */
938          32,                    /* bitsize */
939          FALSE,                 /* pc_relative */
940          0,                     /* bitpos */
941          complain_overflow_dont, /* complain_on_overflow */
942          bfd_elf_generic_reloc, /* special_function */
943          "R_PPC_TLS",           /* name */
944          FALSE,                 /* partial_inplace */
945          0,                     /* src_mask */
946          0,                     /* dst_mask */
947          FALSE),                /* pcrel_offset */
948
949   /* Computes the load module index of the load module that contains the
950      definition of its TLS sym.  */
951   HOWTO (R_PPC_DTPMOD32,
952          0,                     /* rightshift */
953          2,                     /* size (0 = byte, 1 = short, 2 = long) */
954          32,                    /* bitsize */
955          FALSE,                 /* pc_relative */
956          0,                     /* bitpos */
957          complain_overflow_dont, /* complain_on_overflow */
958          ppc_elf_unhandled_reloc, /* special_function */
959          "R_PPC_DTPMOD32",      /* name */
960          FALSE,                 /* partial_inplace */
961          0,                     /* src_mask */
962          0xffffffff,            /* dst_mask */
963          FALSE),                /* pcrel_offset */
964
965   /* Computes a dtv-relative displacement, the difference between the value
966      of sym+add and the base address of the thread-local storage block that
967      contains the definition of sym, minus 0x8000.  */
968   HOWTO (R_PPC_DTPREL32,
969          0,                     /* rightshift */
970          2,                     /* size (0 = byte, 1 = short, 2 = long) */
971          32,                    /* bitsize */
972          FALSE,                 /* pc_relative */
973          0,                     /* bitpos */
974          complain_overflow_dont, /* complain_on_overflow */
975          ppc_elf_unhandled_reloc, /* special_function */
976          "R_PPC_DTPREL32",      /* name */
977          FALSE,                 /* partial_inplace */
978          0,                     /* src_mask */
979          0xffffffff,            /* dst_mask */
980          FALSE),                /* pcrel_offset */
981
982   /* A 16 bit dtprel reloc.  */
983   HOWTO (R_PPC_DTPREL16,
984          0,                     /* rightshift */
985          1,                     /* size (0 = byte, 1 = short, 2 = long) */
986          16,                    /* bitsize */
987          FALSE,                 /* pc_relative */
988          0,                     /* bitpos */
989          complain_overflow_signed, /* complain_on_overflow */
990          ppc_elf_unhandled_reloc, /* special_function */
991          "R_PPC_DTPREL16",      /* name */
992          FALSE,                 /* partial_inplace */
993          0,                     /* src_mask */
994          0xffff,                /* dst_mask */
995          FALSE),                /* pcrel_offset */
996
997   /* Like DTPREL16, but no overflow.  */
998   HOWTO (R_PPC_DTPREL16_LO,
999          0,                     /* rightshift */
1000          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1001          16,                    /* bitsize */
1002          FALSE,                 /* pc_relative */
1003          0,                     /* bitpos */
1004          complain_overflow_dont, /* complain_on_overflow */
1005          ppc_elf_unhandled_reloc, /* special_function */
1006          "R_PPC_DTPREL16_LO",   /* name */
1007          FALSE,                 /* partial_inplace */
1008          0,                     /* src_mask */
1009          0xffff,                /* dst_mask */
1010          FALSE),                /* pcrel_offset */
1011
1012   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
1013   HOWTO (R_PPC_DTPREL16_HI,
1014          16,                    /* rightshift */
1015          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1016          16,                    /* bitsize */
1017          FALSE,                 /* pc_relative */
1018          0,                     /* bitpos */
1019          complain_overflow_dont, /* complain_on_overflow */
1020          ppc_elf_unhandled_reloc, /* special_function */
1021          "R_PPC_DTPREL16_HI",   /* name */
1022          FALSE,                 /* partial_inplace */
1023          0,                     /* src_mask */
1024          0xffff,                /* dst_mask */
1025          FALSE),                /* pcrel_offset */
1026
1027   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
1028   HOWTO (R_PPC_DTPREL16_HA,
1029          16,                    /* rightshift */
1030          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1031          16,                    /* bitsize */
1032          FALSE,                 /* pc_relative */
1033          0,                     /* bitpos */
1034          complain_overflow_dont, /* complain_on_overflow */
1035          ppc_elf_unhandled_reloc, /* special_function */
1036          "R_PPC_DTPREL16_HA",   /* name */
1037          FALSE,                 /* partial_inplace */
1038          0,                     /* src_mask */
1039          0xffff,                /* dst_mask */
1040          FALSE),                /* pcrel_offset */
1041
1042   /* Computes a tp-relative displacement, the difference between the value of
1043      sym+add and the value of the thread pointer (r13).  */
1044   HOWTO (R_PPC_TPREL32,
1045          0,                     /* rightshift */
1046          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1047          32,                    /* bitsize */
1048          FALSE,                 /* pc_relative */
1049          0,                     /* bitpos */
1050          complain_overflow_dont, /* complain_on_overflow */
1051          ppc_elf_unhandled_reloc, /* special_function */
1052          "R_PPC_TPREL32",       /* name */
1053          FALSE,                 /* partial_inplace */
1054          0,                     /* src_mask */
1055          0xffffffff,            /* dst_mask */
1056          FALSE),                /* pcrel_offset */
1057
1058   /* A 16 bit tprel reloc.  */
1059   HOWTO (R_PPC_TPREL16,
1060          0,                     /* rightshift */
1061          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1062          16,                    /* bitsize */
1063          FALSE,                 /* pc_relative */
1064          0,                     /* bitpos */
1065          complain_overflow_signed, /* complain_on_overflow */
1066          ppc_elf_unhandled_reloc, /* special_function */
1067          "R_PPC_TPREL16",       /* name */
1068          FALSE,                 /* partial_inplace */
1069          0,                     /* src_mask */
1070          0xffff,                /* dst_mask */
1071          FALSE),                /* pcrel_offset */
1072
1073   /* Like TPREL16, but no overflow.  */
1074   HOWTO (R_PPC_TPREL16_LO,
1075          0,                     /* rightshift */
1076          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1077          16,                    /* bitsize */
1078          FALSE,                 /* pc_relative */
1079          0,                     /* bitpos */
1080          complain_overflow_dont, /* complain_on_overflow */
1081          ppc_elf_unhandled_reloc, /* special_function */
1082          "R_PPC_TPREL16_LO",    /* name */
1083          FALSE,                 /* partial_inplace */
1084          0,                     /* src_mask */
1085          0xffff,                /* dst_mask */
1086          FALSE),                /* pcrel_offset */
1087
1088   /* Like TPREL16_LO, but next higher group of 16 bits.  */
1089   HOWTO (R_PPC_TPREL16_HI,
1090          16,                    /* rightshift */
1091          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1092          16,                    /* bitsize */
1093          FALSE,                 /* pc_relative */
1094          0,                     /* bitpos */
1095          complain_overflow_dont, /* complain_on_overflow */
1096          ppc_elf_unhandled_reloc, /* special_function */
1097          "R_PPC_TPREL16_HI",    /* name */
1098          FALSE,                 /* partial_inplace */
1099          0,                     /* src_mask */
1100          0xffff,                /* dst_mask */
1101          FALSE),                /* pcrel_offset */
1102
1103   /* Like TPREL16_HI, but adjust for low 16 bits.  */
1104   HOWTO (R_PPC_TPREL16_HA,
1105          16,                    /* rightshift */
1106          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1107          16,                    /* bitsize */
1108          FALSE,                 /* pc_relative */
1109          0,                     /* bitpos */
1110          complain_overflow_dont, /* complain_on_overflow */
1111          ppc_elf_unhandled_reloc, /* special_function */
1112          "R_PPC_TPREL16_HA",    /* name */
1113          FALSE,                 /* partial_inplace */
1114          0,                     /* src_mask */
1115          0xffff,                /* dst_mask */
1116          FALSE),                /* pcrel_offset */
1117
1118   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1119      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1120      to the first entry.  */
1121   HOWTO (R_PPC_GOT_TLSGD16,
1122          0,                     /* rightshift */
1123          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1124          16,                    /* bitsize */
1125          FALSE,                 /* pc_relative */
1126          0,                     /* bitpos */
1127          complain_overflow_signed, /* complain_on_overflow */
1128          ppc_elf_unhandled_reloc, /* special_function */
1129          "R_PPC_GOT_TLSGD16",   /* name */
1130          FALSE,                 /* partial_inplace */
1131          0,                     /* src_mask */
1132          0xffff,                /* dst_mask */
1133          FALSE),                /* pcrel_offset */
1134
1135   /* Like GOT_TLSGD16, but no overflow.  */
1136   HOWTO (R_PPC_GOT_TLSGD16_LO,
1137          0,                     /* rightshift */
1138          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1139          16,                    /* bitsize */
1140          FALSE,                 /* pc_relative */
1141          0,                     /* bitpos */
1142          complain_overflow_dont, /* complain_on_overflow */
1143          ppc_elf_unhandled_reloc, /* special_function */
1144          "R_PPC_GOT_TLSGD16_LO", /* name */
1145          FALSE,                 /* partial_inplace */
1146          0,                     /* src_mask */
1147          0xffff,                /* dst_mask */
1148          FALSE),                /* pcrel_offset */
1149
1150   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
1151   HOWTO (R_PPC_GOT_TLSGD16_HI,
1152          16,                    /* rightshift */
1153          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1154          16,                    /* bitsize */
1155          FALSE,                 /* pc_relative */
1156          0,                     /* bitpos */
1157          complain_overflow_dont, /* complain_on_overflow */
1158          ppc_elf_unhandled_reloc, /* special_function */
1159          "R_PPC_GOT_TLSGD16_HI", /* name */
1160          FALSE,                 /* partial_inplace */
1161          0,                     /* src_mask */
1162          0xffff,                /* dst_mask */
1163          FALSE),                /* pcrel_offset */
1164
1165   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
1166   HOWTO (R_PPC_GOT_TLSGD16_HA,
1167          16,                    /* rightshift */
1168          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1169          16,                    /* bitsize */
1170          FALSE,                 /* pc_relative */
1171          0,                     /* bitpos */
1172          complain_overflow_dont, /* complain_on_overflow */
1173          ppc_elf_unhandled_reloc, /* special_function */
1174          "R_PPC_GOT_TLSGD16_HA", /* name */
1175          FALSE,                 /* partial_inplace */
1176          0,                     /* src_mask */
1177          0xffff,                /* dst_mask */
1178          FALSE),                /* pcrel_offset */
1179
1180   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1181      with values (sym+add)@dtpmod and zero, and computes the offset to the
1182      first entry.  */
1183   HOWTO (R_PPC_GOT_TLSLD16,
1184          0,                     /* rightshift */
1185          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1186          16,                    /* bitsize */
1187          FALSE,                 /* pc_relative */
1188          0,                     /* bitpos */
1189          complain_overflow_signed, /* complain_on_overflow */
1190          ppc_elf_unhandled_reloc, /* special_function */
1191          "R_PPC_GOT_TLSLD16",   /* name */
1192          FALSE,                 /* partial_inplace */
1193          0,                     /* src_mask */
1194          0xffff,                /* dst_mask */
1195          FALSE),                /* pcrel_offset */
1196
1197   /* Like GOT_TLSLD16, but no overflow.  */
1198   HOWTO (R_PPC_GOT_TLSLD16_LO,
1199          0,                     /* rightshift */
1200          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1201          16,                    /* bitsize */
1202          FALSE,                 /* pc_relative */
1203          0,                     /* bitpos */
1204          complain_overflow_dont, /* complain_on_overflow */
1205          ppc_elf_unhandled_reloc, /* special_function */
1206          "R_PPC_GOT_TLSLD16_LO", /* name */
1207          FALSE,                 /* partial_inplace */
1208          0,                     /* src_mask */
1209          0xffff,                /* dst_mask */
1210          FALSE),                /* pcrel_offset */
1211
1212   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
1213   HOWTO (R_PPC_GOT_TLSLD16_HI,
1214          16,                    /* rightshift */
1215          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1216          16,                    /* bitsize */
1217          FALSE,                 /* pc_relative */
1218          0,                     /* bitpos */
1219          complain_overflow_dont, /* complain_on_overflow */
1220          ppc_elf_unhandled_reloc, /* special_function */
1221          "R_PPC_GOT_TLSLD16_HI", /* name */
1222          FALSE,                 /* partial_inplace */
1223          0,                     /* src_mask */
1224          0xffff,                /* dst_mask */
1225          FALSE),                /* pcrel_offset */
1226
1227   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
1228   HOWTO (R_PPC_GOT_TLSLD16_HA,
1229          16,                    /* rightshift */
1230          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1231          16,                    /* bitsize */
1232          FALSE,                 /* pc_relative */
1233          0,                     /* bitpos */
1234          complain_overflow_dont, /* complain_on_overflow */
1235          ppc_elf_unhandled_reloc, /* special_function */
1236          "R_PPC_GOT_TLSLD16_HA", /* name */
1237          FALSE,                 /* partial_inplace */
1238          0,                     /* src_mask */
1239          0xffff,                /* dst_mask */
1240          FALSE),                /* pcrel_offset */
1241
1242   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1243      the offset to the entry.  */
1244   HOWTO (R_PPC_GOT_DTPREL16,
1245          0,                     /* rightshift */
1246          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1247          16,                    /* bitsize */
1248          FALSE,                 /* pc_relative */
1249          0,                     /* bitpos */
1250          complain_overflow_signed, /* complain_on_overflow */
1251          ppc_elf_unhandled_reloc, /* special_function */
1252          "R_PPC_GOT_DTPREL16",  /* name */
1253          FALSE,                 /* partial_inplace */
1254          0,                     /* src_mask */
1255          0xffff,                /* dst_mask */
1256          FALSE),                /* pcrel_offset */
1257
1258   /* Like GOT_DTPREL16, but no overflow.  */
1259   HOWTO (R_PPC_GOT_DTPREL16_LO,
1260          0,                     /* rightshift */
1261          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1262          16,                    /* bitsize */
1263          FALSE,                 /* pc_relative */
1264          0,                     /* bitpos */
1265          complain_overflow_dont, /* complain_on_overflow */
1266          ppc_elf_unhandled_reloc, /* special_function */
1267          "R_PPC_GOT_DTPREL16_LO", /* name */
1268          FALSE,                 /* partial_inplace */
1269          0,                     /* src_mask */
1270          0xffff,                /* dst_mask */
1271          FALSE),                /* pcrel_offset */
1272
1273   /* Like GOT_DTPREL16_LO, but next higher group of 16 bits.  */
1274   HOWTO (R_PPC_GOT_DTPREL16_HI,
1275          16,                    /* rightshift */
1276          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1277          16,                    /* bitsize */
1278          FALSE,                 /* pc_relative */
1279          0,                     /* bitpos */
1280          complain_overflow_dont, /* complain_on_overflow */
1281          ppc_elf_unhandled_reloc, /* special_function */
1282          "R_PPC_GOT_DTPREL16_HI", /* name */
1283          FALSE,                 /* partial_inplace */
1284          0,                     /* src_mask */
1285          0xffff,                /* dst_mask */
1286          FALSE),                /* pcrel_offset */
1287
1288   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
1289   HOWTO (R_PPC_GOT_DTPREL16_HA,
1290          16,                    /* rightshift */
1291          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1292          16,                    /* bitsize */
1293          FALSE,                 /* pc_relative */
1294          0,                     /* bitpos */
1295          complain_overflow_dont, /* complain_on_overflow */
1296          ppc_elf_unhandled_reloc, /* special_function */
1297          "R_PPC_GOT_DTPREL16_HA", /* name */
1298          FALSE,                 /* partial_inplace */
1299          0,                     /* src_mask */
1300          0xffff,                /* dst_mask */
1301          FALSE),                /* pcrel_offset */
1302
1303   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1304      offset to the entry.  */
1305   HOWTO (R_PPC_GOT_TPREL16,
1306          0,                     /* rightshift */
1307          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1308          16,                    /* bitsize */
1309          FALSE,                 /* pc_relative */
1310          0,                     /* bitpos */
1311          complain_overflow_signed, /* complain_on_overflow */
1312          ppc_elf_unhandled_reloc, /* special_function */
1313          "R_PPC_GOT_TPREL16",   /* name */
1314          FALSE,                 /* partial_inplace */
1315          0,                     /* src_mask */
1316          0xffff,                /* dst_mask */
1317          FALSE),                /* pcrel_offset */
1318
1319   /* Like GOT_TPREL16, but no overflow.  */
1320   HOWTO (R_PPC_GOT_TPREL16_LO,
1321          0,                     /* rightshift */
1322          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1323          16,                    /* bitsize */
1324          FALSE,                 /* pc_relative */
1325          0,                     /* bitpos */
1326          complain_overflow_dont, /* complain_on_overflow */
1327          ppc_elf_unhandled_reloc, /* special_function */
1328          "R_PPC_GOT_TPREL16_LO", /* name */
1329          FALSE,                 /* partial_inplace */
1330          0,                     /* src_mask */
1331          0xffff,                /* dst_mask */
1332          FALSE),                /* pcrel_offset */
1333
1334   /* Like GOT_TPREL16_LO, but next higher group of 16 bits.  */
1335   HOWTO (R_PPC_GOT_TPREL16_HI,
1336          16,                    /* rightshift */
1337          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1338          16,                    /* bitsize */
1339          FALSE,                 /* pc_relative */
1340          0,                     /* bitpos */
1341          complain_overflow_dont, /* complain_on_overflow */
1342          ppc_elf_unhandled_reloc, /* special_function */
1343          "R_PPC_GOT_TPREL16_HI", /* name */
1344          FALSE,                 /* partial_inplace */
1345          0,                     /* src_mask */
1346          0xffff,                /* dst_mask */
1347          FALSE),                /* pcrel_offset */
1348
1349   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
1350   HOWTO (R_PPC_GOT_TPREL16_HA,
1351          16,                    /* rightshift */
1352          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1353          16,                    /* bitsize */
1354          FALSE,                 /* pc_relative */
1355          0,                     /* bitpos */
1356          complain_overflow_dont, /* complain_on_overflow */
1357          ppc_elf_unhandled_reloc, /* special_function */
1358          "R_PPC_GOT_TPREL16_HA", /* name */
1359          FALSE,                 /* partial_inplace */
1360          0,                     /* src_mask */
1361          0xffff,                /* dst_mask */
1362          FALSE),                /* pcrel_offset */
1363
1364   /* The remaining relocs are from the Embedded ELF ABI, and are not
1365      in the SVR4 ELF ABI.  */
1366
1367   /* 32 bit value resulting from the addend minus the symbol.  */
1368   HOWTO (R_PPC_EMB_NADDR32,     /* type */
1369          0,                     /* rightshift */
1370          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1371          32,                    /* bitsize */
1372          FALSE,                 /* pc_relative */
1373          0,                     /* bitpos */
1374          complain_overflow_bitfield, /* complain_on_overflow */
1375          bfd_elf_generic_reloc, /* special_function */
1376          "R_PPC_EMB_NADDR32",   /* name */
1377          FALSE,                 /* partial_inplace */
1378          0,                     /* src_mask */
1379          0xffffffff,            /* dst_mask */
1380          FALSE),                /* pcrel_offset */
1381
1382   /* 16 bit value resulting from the addend minus the symbol.  */
1383   HOWTO (R_PPC_EMB_NADDR16,     /* type */
1384          0,                     /* rightshift */
1385          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1386          16,                    /* bitsize */
1387          FALSE,                 /* pc_relative */
1388          0,                     /* bitpos */
1389          complain_overflow_bitfield, /* complain_on_overflow */
1390          bfd_elf_generic_reloc, /* special_function */
1391          "R_PPC_EMB_NADDR16",   /* name */
1392          FALSE,                 /* partial_inplace */
1393          0,                     /* src_mask */
1394          0xffff,                /* dst_mask */
1395          FALSE),                /* pcrel_offset */
1396
1397   /* 16 bit value resulting from the addend minus the symbol.  */
1398   HOWTO (R_PPC_EMB_NADDR16_LO,  /* type */
1399          0,                     /* rightshift */
1400          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1401          16,                    /* bitsize */
1402          FALSE,                 /* pc_relative */
1403          0,                     /* bitpos */
1404          complain_overflow_dont,/* complain_on_overflow */
1405          bfd_elf_generic_reloc, /* special_function */
1406          "R_PPC_EMB_ADDR16_LO", /* name */
1407          FALSE,                 /* partial_inplace */
1408          0,                     /* src_mask */
1409          0xffff,                /* dst_mask */
1410          FALSE),                /* pcrel_offset */
1411
1412   /* The high order 16 bits of the addend minus the symbol.  */
1413   HOWTO (R_PPC_EMB_NADDR16_HI,  /* type */
1414          16,                    /* rightshift */
1415          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1416          16,                    /* bitsize */
1417          FALSE,                 /* pc_relative */
1418          0,                     /* bitpos */
1419          complain_overflow_dont, /* complain_on_overflow */
1420          bfd_elf_generic_reloc, /* special_function */
1421          "R_PPC_EMB_NADDR16_HI", /* name */
1422          FALSE,                 /* partial_inplace */
1423          0,                     /* src_mask */
1424          0xffff,                /* dst_mask */
1425          FALSE),                /* pcrel_offset */
1426
1427   /* The high order 16 bits of the result of the addend minus the address,
1428      plus 1 if the contents of the low 16 bits, treated as a signed number,
1429      is negative.  */
1430   HOWTO (R_PPC_EMB_NADDR16_HA,  /* type */
1431          16,                    /* rightshift */
1432          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1433          16,                    /* bitsize */
1434          FALSE,                 /* pc_relative */
1435          0,                     /* bitpos */
1436          complain_overflow_dont, /* complain_on_overflow */
1437          ppc_elf_addr16_ha_reloc, /* special_function */
1438          "R_PPC_EMB_NADDR16_HA", /* name */
1439          FALSE,                 /* partial_inplace */
1440          0,                     /* src_mask */
1441          0xffff,                /* dst_mask */
1442          FALSE),                /* pcrel_offset */
1443
1444   /* 16 bit value resulting from allocating a 4 byte word to hold an
1445      address in the .sdata section, and returning the offset from
1446      _SDA_BASE_ for that relocation.  */
1447   HOWTO (R_PPC_EMB_SDAI16,      /* type */
1448          0,                     /* rightshift */
1449          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1450          16,                    /* bitsize */
1451          FALSE,                 /* pc_relative */
1452          0,                     /* bitpos */
1453          complain_overflow_bitfield, /* complain_on_overflow */
1454          bfd_elf_generic_reloc, /* special_function */
1455          "R_PPC_EMB_SDAI16",    /* name */
1456          FALSE,                 /* partial_inplace */
1457          0,                     /* src_mask */
1458          0xffff,                /* dst_mask */
1459          FALSE),                /* pcrel_offset */
1460
1461   /* 16 bit value resulting from allocating a 4 byte word to hold an
1462      address in the .sdata2 section, and returning the offset from
1463      _SDA2_BASE_ for that relocation.  */
1464   HOWTO (R_PPC_EMB_SDA2I16,     /* type */
1465          0,                     /* rightshift */
1466          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1467          16,                    /* bitsize */
1468          FALSE,                 /* pc_relative */
1469          0,                     /* bitpos */
1470          complain_overflow_bitfield, /* complain_on_overflow */
1471          bfd_elf_generic_reloc, /* special_function */
1472          "R_PPC_EMB_SDA2I16",   /* name */
1473          FALSE,                 /* partial_inplace */
1474          0,                     /* src_mask */
1475          0xffff,                /* dst_mask */
1476          FALSE),                /* pcrel_offset */
1477
1478   /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1479      small data items.   */
1480   HOWTO (R_PPC_EMB_SDA2REL,     /* type */
1481          0,                     /* rightshift */
1482          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1483          16,                    /* bitsize */
1484          FALSE,                 /* pc_relative */
1485          0,                     /* bitpos */
1486          complain_overflow_signed, /* complain_on_overflow */
1487          bfd_elf_generic_reloc, /* special_function */
1488          "R_PPC_EMB_SDA2REL",   /* name */
1489          FALSE,                 /* partial_inplace */
1490          0,                     /* src_mask */
1491          0xffff,                /* dst_mask */
1492          FALSE),                /* pcrel_offset */
1493
1494   /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1495      signed offset from the appropriate base, and filling in the register
1496      field with the appropriate register (0, 2, or 13).  */
1497   HOWTO (R_PPC_EMB_SDA21,       /* type */
1498          0,                     /* rightshift */
1499          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1500          16,                    /* bitsize */
1501          FALSE,                 /* pc_relative */
1502          0,                     /* bitpos */
1503          complain_overflow_signed, /* complain_on_overflow */
1504          bfd_elf_generic_reloc, /* special_function */
1505          "R_PPC_EMB_SDA21",     /* name */
1506          FALSE,                 /* partial_inplace */
1507          0,                     /* src_mask */
1508          0xffff,                /* dst_mask */
1509          FALSE),                /* pcrel_offset */
1510
1511   /* Relocation not handled: R_PPC_EMB_MRKREF */
1512   /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1513   /* Relocation not handled: R_PPC_EMB_RELST_LO */
1514   /* Relocation not handled: R_PPC_EMB_RELST_HI */
1515   /* Relocation not handled: R_PPC_EMB_RELST_HA */
1516   /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1517
1518   /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1519      in the 16 bit signed offset from the appropriate base, and filling in the
1520      register field with the appropriate register (0, 2, or 13).  */
1521   HOWTO (R_PPC_EMB_RELSDA,      /* type */
1522          0,                     /* rightshift */
1523          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1524          16,                    /* bitsize */
1525          TRUE,                  /* pc_relative */
1526          0,                     /* bitpos */
1527          complain_overflow_signed, /* complain_on_overflow */
1528          bfd_elf_generic_reloc, /* special_function */
1529          "R_PPC_EMB_RELSDA",    /* name */
1530          FALSE,                 /* partial_inplace */
1531          0,                     /* src_mask */
1532          0xffff,                /* dst_mask */
1533          FALSE),                /* pcrel_offset */
1534
1535   /* Phony relocs to handle branch stubs.  */
1536   HOWTO (R_PPC_RELAX32,         /* type */
1537          0,                     /* rightshift */
1538          0,                     /* size */
1539          0,                     /* bitsize */
1540          FALSE,                 /* pc_relative */
1541          0,                     /* bitpos */
1542          complain_overflow_dont, /* complain_on_overflow */
1543          bfd_elf_generic_reloc, /* special_function */
1544          "R_PPC_RELAX32",       /* name */
1545          FALSE,                 /* partial_inplace */
1546          0,                     /* src_mask */
1547          0,                     /* dst_mask */
1548          FALSE),                /* pcrel_offset */
1549
1550   HOWTO (R_PPC_RELAX32PC,       /* type */
1551          0,                     /* rightshift */
1552          0,                     /* size */
1553          0,                     /* bitsize */
1554          FALSE,                 /* pc_relative */
1555          0,                     /* bitpos */
1556          complain_overflow_dont, /* complain_on_overflow */
1557          bfd_elf_generic_reloc, /* special_function */
1558          "R_PPC_RELAX32PC",     /* name */
1559          FALSE,                 /* partial_inplace */
1560          0,                     /* src_mask */
1561          0,                     /* dst_mask */
1562          FALSE),                /* pcrel_offset */
1563
1564   /* GNU extension to record C++ vtable hierarchy.  */
1565   HOWTO (R_PPC_GNU_VTINHERIT,   /* type */
1566          0,                     /* rightshift */
1567          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1568          0,                     /* bitsize */
1569          FALSE,                 /* pc_relative */
1570          0,                     /* bitpos */
1571          complain_overflow_dont, /* complain_on_overflow */
1572          NULL,                  /* special_function */
1573          "R_PPC_GNU_VTINHERIT", /* name */
1574          FALSE,                 /* partial_inplace */
1575          0,                     /* src_mask */
1576          0,                     /* dst_mask */
1577          FALSE),                /* pcrel_offset */
1578
1579   /* GNU extension to record C++ vtable member usage.  */
1580   HOWTO (R_PPC_GNU_VTENTRY,     /* type */
1581          0,                     /* rightshift */
1582          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1583          0,                     /* bitsize */
1584          FALSE,                 /* pc_relative */
1585          0,                     /* bitpos */
1586          complain_overflow_dont, /* complain_on_overflow */
1587          NULL,                  /* special_function */
1588          "R_PPC_GNU_VTENTRY",   /* name */
1589          FALSE,                 /* partial_inplace */
1590          0,                     /* src_mask */
1591          0,                     /* dst_mask */
1592          FALSE),                /* pcrel_offset */
1593
1594   /* Phony reloc to handle AIX style TOC entries.  */
1595   HOWTO (R_PPC_TOC16,           /* type */
1596          0,                     /* rightshift */
1597          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1598          16,                    /* bitsize */
1599          FALSE,                 /* pc_relative */
1600          0,                     /* bitpos */
1601          complain_overflow_signed, /* complain_on_overflow */
1602          bfd_elf_generic_reloc, /* special_function */
1603          "R_PPC_TOC16",         /* name */
1604          FALSE,                 /* partial_inplace */
1605          0,                     /* src_mask */
1606          0xffff,                /* dst_mask */
1607          FALSE),                /* pcrel_offset */
1608 };
1609 \f
1610 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
1611
1612 static void
1613 ppc_elf_howto_init (void)
1614 {
1615   unsigned int i, type;
1616
1617   for (i = 0;
1618        i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1619        i++)
1620     {
1621       type = ppc_elf_howto_raw[i].type;
1622       if (type >= (sizeof (ppc_elf_howto_table)
1623                    / sizeof (ppc_elf_howto_table[0])))
1624         abort ();
1625       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1626     }
1627 }
1628 \f
1629 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
1630
1631 static const int shared_stub_entry[] =
1632   {
1633     0x7c0802a6, /* mflr 0 */
1634     0x429f0005, /* bcl 20, 31, .Lxxx */
1635     0x7d6802a6, /* mflr 11 */
1636     0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
1637     0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
1638     0x7c0803a6, /* mtlr 0 */
1639     0x7d6903a6, /* mtctr 11 */
1640     0x4e800420, /* bctr */
1641   };
1642
1643 static const int stub_entry[] =
1644   {
1645     0x3d600000, /* lis 11,xxx@ha */
1646     0x396b0000, /* addi 11,11,xxx@l */
1647     0x7d6903a6, /* mtctr 11 */
1648     0x4e800420, /* bctr */
1649   };
1650
1651
1652 static bfd_boolean
1653 ppc_elf_relax_section (bfd *abfd,
1654                        asection *isec,
1655                        struct bfd_link_info *link_info,
1656                        bfd_boolean *again)
1657 {
1658   struct one_fixup
1659   {
1660     struct one_fixup *next;
1661     asection *tsec;
1662     bfd_vma toff;
1663     bfd_vma trampoff;
1664   };
1665
1666   Elf_Internal_Shdr *symtab_hdr;
1667   bfd_byte *contents = NULL;
1668   Elf_Internal_Sym *isymbuf = NULL;
1669   Elf_Internal_Rela *internal_relocs = NULL;
1670   Elf_Internal_Rela *irel, *irelend;
1671   struct one_fixup *fixups = NULL;
1672   bfd_boolean changed;
1673   struct ppc_elf_link_hash_table *ppc_info;
1674   bfd_size_type trampoff;
1675
1676   *again = FALSE;
1677
1678   /* Nothing to do if there are no relocations.  */
1679   if ((isec->flags & SEC_RELOC) == 0 || isec->reloc_count == 0)
1680     return TRUE;
1681
1682   /* If needed, initialize this section's cooked size.  */
1683   if (isec->_cooked_size == 0)
1684     isec->_cooked_size = isec->_raw_size;
1685
1686   trampoff = (isec->_cooked_size + 3) & (bfd_vma) -4;
1687   /* Space for a branch around any trampolines.  */
1688   trampoff += 4;
1689
1690   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1691
1692   /* Get a copy of the native relocations.  */
1693   internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
1694                                                link_info->keep_memory);
1695   if (internal_relocs == NULL)
1696     goto error_return;
1697
1698   ppc_info = ppc_elf_hash_table (link_info);
1699   irelend = internal_relocs + isec->reloc_count;
1700
1701   /* Get the section contents.  */
1702   /* Get cached copy if it exists.  */
1703   if (elf_section_data (isec)->this_hdr.contents != NULL)
1704     contents = elf_section_data (isec)->this_hdr.contents;
1705   else
1706     {
1707       /* Go get them off disk.  */
1708       contents = bfd_malloc (isec->_raw_size);
1709       if (contents == NULL)
1710         goto error_return;
1711
1712       if (!bfd_get_section_contents (abfd, isec, contents, 0, isec->_raw_size))
1713         goto error_return;
1714     }
1715
1716   for (irel = internal_relocs; irel < irelend; irel++)
1717     {
1718       unsigned long r_type = ELF32_R_TYPE (irel->r_info);
1719       bfd_vma symaddr, reladdr, toff, roff;
1720       asection *tsec;
1721       struct one_fixup *f;
1722       size_t insn_offset = 0;
1723       bfd_vma max_branch_offset, val;
1724       bfd_byte *hit_addr;
1725       unsigned long t0;
1726
1727       switch (r_type)
1728         {
1729         case R_PPC_REL24:
1730         case R_PPC_LOCAL24PC:
1731         case R_PPC_PLTREL24:
1732           max_branch_offset = 1 << 25;
1733           break;
1734
1735         case R_PPC_REL14:
1736         case R_PPC_REL14_BRTAKEN:
1737         case R_PPC_REL14_BRNTAKEN:
1738           max_branch_offset = 1 << 15;
1739           break;
1740
1741         default:
1742           continue;
1743         }
1744
1745       /* Get the value of the symbol referred to by the reloc.  */
1746       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1747         {
1748           /* A local symbol.  */
1749           Elf_Internal_Sym *isym;
1750
1751           /* Read this BFD's local symbols.  */
1752           if (isymbuf == NULL)
1753             {
1754               isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1755               if (isymbuf == NULL)
1756                 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1757                                                 symtab_hdr->sh_info, 0,
1758                                                 NULL, NULL, NULL);
1759               if (isymbuf == 0)
1760                 goto error_return;
1761             }
1762           isym = isymbuf + ELF32_R_SYM (irel->r_info);
1763           if (isym->st_shndx == SHN_UNDEF)
1764             continue;   /* We can't do anything with undefined symbols.  */
1765           else if (isym->st_shndx == SHN_ABS)
1766             tsec = bfd_abs_section_ptr;
1767           else if (isym->st_shndx == SHN_COMMON)
1768             tsec = bfd_com_section_ptr;
1769           else
1770             tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1771
1772           toff = isym->st_value;
1773         }
1774       else
1775         {
1776           /* Global symbol handling.  */
1777           unsigned long indx;
1778           struct elf_link_hash_entry *h;
1779
1780           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1781           h = elf_sym_hashes (abfd)[indx];
1782
1783           while (h->root.type == bfd_link_hash_indirect
1784                  || h->root.type == bfd_link_hash_warning)
1785             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1786
1787           if (r_type == R_PPC_PLTREL24
1788               && ppc_info->plt != NULL
1789               && h->plt.offset != (bfd_vma) -1)
1790             {
1791               tsec = ppc_info->plt;
1792               toff = h->plt.offset;
1793             }
1794           else if (h->root.type == bfd_link_hash_defined
1795                    || h->root.type == bfd_link_hash_defweak)
1796             {
1797               tsec = h->root.u.def.section;
1798               toff = h->root.u.def.value;
1799             }
1800           else
1801             continue;
1802         }
1803
1804       /* If the branch and target are in the same section, you have
1805          no hope of adding stubs.  We'll error out later should the
1806          branch overflow.  */
1807       if (tsec == isec)
1808         continue;
1809
1810       toff += irel->r_addend;
1811       if (tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
1812         toff = _bfd_merged_section_offset (abfd, &tsec,
1813                                            elf_section_data (tsec)->sec_info,
1814                                            toff);
1815
1816       symaddr = tsec->output_section->vma + tsec->output_offset + toff;
1817
1818       roff = irel->r_offset;
1819       reladdr = isec->output_section->vma + isec->output_offset + roff;
1820
1821       /* If the branch is in range, no need to do anything.  */
1822       if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
1823         continue;
1824
1825       /* Look for an existing fixup to this address.  */
1826       for (f = fixups; f ; f = f->next)
1827         if (f->tsec == tsec && f->toff == toff)
1828           break;
1829
1830       if (f == NULL)
1831         {
1832           size_t size;
1833           unsigned long stub_rtype;
1834
1835           val = trampoff - roff;
1836           if (val >= max_branch_offset)
1837             /* Oh dear, we can't reach a trampoline.  Don't try to add
1838                one.  We'll report an error later.  */
1839             continue;
1840
1841           if (link_info->shared)
1842             {
1843               size = 4 * ARRAY_SIZE (shared_stub_entry);
1844               insn_offset = 12;
1845               stub_rtype = R_PPC_RELAX32PC;
1846             }
1847           else
1848             {
1849               size = 4 * ARRAY_SIZE (stub_entry);
1850               insn_offset = 0;
1851               stub_rtype = R_PPC_RELAX32;
1852             }
1853
1854           /* Hijack the old relocation.  Since we need two
1855              relocations for this use a "composite" reloc.  */
1856           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1857                                        stub_rtype);
1858           irel->r_offset = trampoff + insn_offset;
1859
1860           /* Record the fixup so we don't do it again this section.  */
1861           f = bfd_malloc (sizeof (*f));
1862           f->next = fixups;
1863           f->tsec = tsec;
1864           f->toff = toff;
1865           f->trampoff = trampoff;
1866           fixups = f;
1867
1868           trampoff += size;
1869         }
1870       else
1871         {
1872           val = f->trampoff - roff;
1873           if (val >= max_branch_offset)
1874             continue;
1875
1876           /* Nop out the reloc, since we're finalizing things here.  */
1877           irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
1878         }
1879
1880       /* Fix up the existing branch to hit the trampoline.  */
1881       hit_addr = contents + roff;
1882       switch (r_type)
1883         {
1884         case R_PPC_REL24:
1885         case R_PPC_LOCAL24PC:
1886         case R_PPC_PLTREL24:
1887           t0 = bfd_get_32 (abfd, hit_addr);
1888           t0 &= ~0x3fffffc;
1889           t0 |= val & 0x3fffffc;
1890           bfd_put_32 (abfd, t0, hit_addr);
1891           break;
1892
1893         case R_PPC_REL14:
1894         case R_PPC_REL14_BRTAKEN:
1895         case R_PPC_REL14_BRNTAKEN:
1896           t0 = bfd_get_32 (abfd, hit_addr);
1897           t0 &= ~0xfffc;
1898           t0 |= val & 0xfffc;
1899           bfd_put_32 (abfd, t0, hit_addr);
1900           break;
1901         }
1902     }
1903
1904   /* Write out the trampolines.  */
1905   changed = fixups != NULL;
1906   if (fixups != NULL)
1907     {
1908       const int *stub;
1909       bfd_byte *dest;
1910       bfd_vma val;
1911       int i, size;
1912
1913       do
1914         {
1915           struct one_fixup *f = fixups;
1916           fixups = fixups->next;
1917           free (f);
1918         }
1919       while (fixups);
1920
1921       contents = bfd_realloc (contents, trampoff);
1922       if (contents == NULL)
1923         goto error_return;
1924
1925       isec->_cooked_size = (isec->_cooked_size + 3) & (bfd_vma) -4;
1926       /* Branch around the trampolines.  */
1927       val = trampoff - isec->_cooked_size + 0x48000000;
1928       dest = contents + isec->_cooked_size;
1929       isec->_cooked_size = trampoff;
1930       bfd_put_32 (abfd, val, dest);
1931       dest += 4;
1932
1933       if (link_info->shared)
1934         {
1935           stub = shared_stub_entry;
1936           size = ARRAY_SIZE (shared_stub_entry);
1937         }
1938       else
1939         {
1940           stub = stub_entry;
1941           size = ARRAY_SIZE (stub_entry);
1942         }
1943
1944       i = 0;
1945       while (dest < contents + trampoff)
1946         {
1947           bfd_put_32 (abfd, stub[i], dest);
1948           i++;
1949           if (i == size)
1950             i = 0;
1951           dest += 4;
1952         }
1953       BFD_ASSERT (i == 0);
1954     }
1955
1956   if (isymbuf != NULL
1957       && symtab_hdr->contents != (unsigned char *) isymbuf)
1958     {
1959       if (! link_info->keep_memory)
1960         free (isymbuf);
1961       else
1962         {
1963           /* Cache the symbols for elf_link_input_bfd.  */
1964           symtab_hdr->contents = (unsigned char *) isymbuf;
1965         }
1966     }
1967
1968   if (contents != NULL
1969       && elf_section_data (isec)->this_hdr.contents != contents)
1970     {
1971       if (!changed && !link_info->keep_memory)
1972         free (contents);
1973       else
1974         {
1975           /* Cache the section contents for elf_link_input_bfd.  */
1976           elf_section_data (isec)->this_hdr.contents = contents;
1977         }
1978     }
1979
1980   if (elf_section_data (isec)->relocs != internal_relocs)
1981     {
1982       if (!changed)
1983         free (internal_relocs);
1984       else
1985         elf_section_data (isec)->relocs = internal_relocs;
1986     }
1987
1988   *again = changed;
1989   return TRUE;
1990
1991  error_return:
1992   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
1993     free (isymbuf);
1994   if (contents != NULL
1995       && elf_section_data (isec)->this_hdr.contents != contents)
1996     free (contents);
1997   if (internal_relocs != NULL
1998       && elf_section_data (isec)->relocs != internal_relocs)
1999     free (internal_relocs);
2000   return FALSE;
2001 }
2002 \f
2003 static reloc_howto_type *
2004 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2005                            bfd_reloc_code_real_type code)
2006 {
2007   enum elf_ppc_reloc_type r;
2008
2009   /* Initialize howto table if not already done.  */
2010   if (!ppc_elf_howto_table[R_PPC_ADDR32])
2011     ppc_elf_howto_init ();
2012
2013   switch (code)
2014     {
2015     default:
2016       return NULL;
2017
2018     case BFD_RELOC_NONE:                r = R_PPC_NONE;                 break;
2019     case BFD_RELOC_32:                  r = R_PPC_ADDR32;               break;
2020     case BFD_RELOC_PPC_BA26:            r = R_PPC_ADDR24;               break;
2021     case BFD_RELOC_16:                  r = R_PPC_ADDR16;               break;
2022     case BFD_RELOC_LO16:                r = R_PPC_ADDR16_LO;            break;
2023     case BFD_RELOC_HI16:                r = R_PPC_ADDR16_HI;            break;
2024     case BFD_RELOC_HI16_S:              r = R_PPC_ADDR16_HA;            break;
2025     case BFD_RELOC_PPC_BA16:            r = R_PPC_ADDR14;               break;
2026     case BFD_RELOC_PPC_BA16_BRTAKEN:    r = R_PPC_ADDR14_BRTAKEN;       break;
2027     case BFD_RELOC_PPC_BA16_BRNTAKEN:   r = R_PPC_ADDR14_BRNTAKEN;      break;
2028     case BFD_RELOC_PPC_B26:             r = R_PPC_REL24;                break;
2029     case BFD_RELOC_PPC_B16:             r = R_PPC_REL14;                break;
2030     case BFD_RELOC_PPC_B16_BRTAKEN:     r = R_PPC_REL14_BRTAKEN;        break;
2031     case BFD_RELOC_PPC_B16_BRNTAKEN:    r = R_PPC_REL14_BRNTAKEN;       break;
2032     case BFD_RELOC_16_GOTOFF:           r = R_PPC_GOT16;                break;
2033     case BFD_RELOC_LO16_GOTOFF:         r = R_PPC_GOT16_LO;             break;
2034     case BFD_RELOC_HI16_GOTOFF:         r = R_PPC_GOT16_HI;             break;
2035     case BFD_RELOC_HI16_S_GOTOFF:       r = R_PPC_GOT16_HA;             break;
2036     case BFD_RELOC_24_PLT_PCREL:        r = R_PPC_PLTREL24;             break;
2037     case BFD_RELOC_PPC_COPY:            r = R_PPC_COPY;                 break;
2038     case BFD_RELOC_PPC_GLOB_DAT:        r = R_PPC_GLOB_DAT;             break;
2039     case BFD_RELOC_PPC_LOCAL24PC:       r = R_PPC_LOCAL24PC;            break;
2040     case BFD_RELOC_32_PCREL:            r = R_PPC_REL32;                break;
2041     case BFD_RELOC_32_PLTOFF:           r = R_PPC_PLT32;                break;
2042     case BFD_RELOC_32_PLT_PCREL:        r = R_PPC_PLTREL32;             break;
2043     case BFD_RELOC_LO16_PLTOFF:         r = R_PPC_PLT16_LO;             break;
2044     case BFD_RELOC_HI16_PLTOFF:         r = R_PPC_PLT16_HI;             break;
2045     case BFD_RELOC_HI16_S_PLTOFF:       r = R_PPC_PLT16_HA;             break;
2046     case BFD_RELOC_GPREL16:             r = R_PPC_SDAREL16;             break;
2047     case BFD_RELOC_16_BASEREL:          r = R_PPC_SECTOFF;              break;
2048     case BFD_RELOC_LO16_BASEREL:        r = R_PPC_SECTOFF_LO;           break;
2049     case BFD_RELOC_HI16_BASEREL:        r = R_PPC_SECTOFF_HI;           break;
2050     case BFD_RELOC_HI16_S_BASEREL:      r = R_PPC_SECTOFF_HA;           break;
2051     case BFD_RELOC_CTOR:                r = R_PPC_ADDR32;               break;
2052     case BFD_RELOC_PPC_TOC16:           r = R_PPC_TOC16;                break;
2053     case BFD_RELOC_PPC_TLS:             r = R_PPC_TLS;                  break;
2054     case BFD_RELOC_PPC_DTPMOD:          r = R_PPC_DTPMOD32;             break;
2055     case BFD_RELOC_PPC_TPREL16:         r = R_PPC_TPREL16;              break;
2056     case BFD_RELOC_PPC_TPREL16_LO:      r = R_PPC_TPREL16_LO;           break;
2057     case BFD_RELOC_PPC_TPREL16_HI:      r = R_PPC_TPREL16_HI;           break;
2058     case BFD_RELOC_PPC_TPREL16_HA:      r = R_PPC_TPREL16_HA;           break;
2059     case BFD_RELOC_PPC_TPREL:           r = R_PPC_TPREL32;              break;
2060     case BFD_RELOC_PPC_DTPREL16:        r = R_PPC_DTPREL16;             break;
2061     case BFD_RELOC_PPC_DTPREL16_LO:     r = R_PPC_DTPREL16_LO;          break;
2062     case BFD_RELOC_PPC_DTPREL16_HI:     r = R_PPC_DTPREL16_HI;          break;
2063     case BFD_RELOC_PPC_DTPREL16_HA:     r = R_PPC_DTPREL16_HA;          break;
2064     case BFD_RELOC_PPC_DTPREL:          r = R_PPC_DTPREL32;             break;
2065     case BFD_RELOC_PPC_GOT_TLSGD16:     r = R_PPC_GOT_TLSGD16;          break;
2066     case BFD_RELOC_PPC_GOT_TLSGD16_LO:  r = R_PPC_GOT_TLSGD16_LO;       break;
2067     case BFD_RELOC_PPC_GOT_TLSGD16_HI:  r = R_PPC_GOT_TLSGD16_HI;       break;
2068     case BFD_RELOC_PPC_GOT_TLSGD16_HA:  r = R_PPC_GOT_TLSGD16_HA;       break;
2069     case BFD_RELOC_PPC_GOT_TLSLD16:     r = R_PPC_GOT_TLSLD16;          break;
2070     case BFD_RELOC_PPC_GOT_TLSLD16_LO:  r = R_PPC_GOT_TLSLD16_LO;       break;
2071     case BFD_RELOC_PPC_GOT_TLSLD16_HI:  r = R_PPC_GOT_TLSLD16_HI;       break;
2072     case BFD_RELOC_PPC_GOT_TLSLD16_HA:  r = R_PPC_GOT_TLSLD16_HA;       break;
2073     case BFD_RELOC_PPC_GOT_TPREL16:     r = R_PPC_GOT_TPREL16;          break;
2074     case BFD_RELOC_PPC_GOT_TPREL16_LO:  r = R_PPC_GOT_TPREL16_LO;       break;
2075     case BFD_RELOC_PPC_GOT_TPREL16_HI:  r = R_PPC_GOT_TPREL16_HI;       break;
2076     case BFD_RELOC_PPC_GOT_TPREL16_HA:  r = R_PPC_GOT_TPREL16_HA;       break;
2077     case BFD_RELOC_PPC_GOT_DTPREL16:    r = R_PPC_GOT_DTPREL16;         break;
2078     case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO;      break;
2079     case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI;      break;
2080     case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA;      break;
2081     case BFD_RELOC_PPC_EMB_NADDR32:     r = R_PPC_EMB_NADDR32;          break;
2082     case BFD_RELOC_PPC_EMB_NADDR16:     r = R_PPC_EMB_NADDR16;          break;
2083     case BFD_RELOC_PPC_EMB_NADDR16_LO:  r = R_PPC_EMB_NADDR16_LO;       break;
2084     case BFD_RELOC_PPC_EMB_NADDR16_HI:  r = R_PPC_EMB_NADDR16_HI;       break;
2085     case BFD_RELOC_PPC_EMB_NADDR16_HA:  r = R_PPC_EMB_NADDR16_HA;       break;
2086     case BFD_RELOC_PPC_EMB_SDAI16:      r = R_PPC_EMB_SDAI16;           break;
2087     case BFD_RELOC_PPC_EMB_SDA2I16:     r = R_PPC_EMB_SDA2I16;          break;
2088     case BFD_RELOC_PPC_EMB_SDA2REL:     r = R_PPC_EMB_SDA2REL;          break;
2089     case BFD_RELOC_PPC_EMB_SDA21:       r = R_PPC_EMB_SDA21;            break;
2090     case BFD_RELOC_PPC_EMB_MRKREF:      r = R_PPC_EMB_MRKREF;           break;
2091     case BFD_RELOC_PPC_EMB_RELSEC16:    r = R_PPC_EMB_RELSEC16;         break;
2092     case BFD_RELOC_PPC_EMB_RELST_LO:    r = R_PPC_EMB_RELST_LO;         break;
2093     case BFD_RELOC_PPC_EMB_RELST_HI:    r = R_PPC_EMB_RELST_HI;         break;
2094     case BFD_RELOC_PPC_EMB_RELST_HA:    r = R_PPC_EMB_RELST_HA;         break;
2095     case BFD_RELOC_PPC_EMB_BIT_FLD:     r = R_PPC_EMB_BIT_FLD;          break;
2096     case BFD_RELOC_PPC_EMB_RELSDA:      r = R_PPC_EMB_RELSDA;           break;
2097     case BFD_RELOC_VTABLE_INHERIT:      r = R_PPC_GNU_VTINHERIT;        break;
2098     case BFD_RELOC_VTABLE_ENTRY:        r = R_PPC_GNU_VTENTRY;          break;
2099     }
2100
2101   return ppc_elf_howto_table[r];
2102 };
2103
2104 /* Set the howto pointer for a PowerPC ELF reloc.  */
2105
2106 static void
2107 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
2108                        arelent *cache_ptr,
2109                        Elf_Internal_Rela *dst)
2110 {
2111   /* Initialize howto table if not already done.  */
2112   if (!ppc_elf_howto_table[R_PPC_ADDR32])
2113     ppc_elf_howto_init ();
2114
2115   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
2116   cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
2117 }
2118
2119 /* Handle the R_PPC_ADDR16_HA reloc.  */
2120
2121 static bfd_reloc_status_type
2122 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2123                          arelent *reloc_entry,
2124                          asymbol *symbol,
2125                          void *data ATTRIBUTE_UNUSED,
2126                          asection *input_section,
2127                          bfd *output_bfd,
2128                          char **error_message ATTRIBUTE_UNUSED)
2129 {
2130   bfd_vma relocation;
2131
2132   if (output_bfd != NULL)
2133     {
2134       reloc_entry->address += input_section->output_offset;
2135       return bfd_reloc_ok;
2136     }
2137
2138   if (reloc_entry->address > input_section->_cooked_size)
2139     return bfd_reloc_outofrange;
2140
2141   if (bfd_is_com_section (symbol->section))
2142     relocation = 0;
2143   else
2144     relocation = symbol->value;
2145
2146   relocation += symbol->section->output_section->vma;
2147   relocation += symbol->section->output_offset;
2148   relocation += reloc_entry->addend;
2149
2150   reloc_entry->addend += (relocation & 0x8000) << 1;
2151
2152   return bfd_reloc_continue;
2153 }
2154
2155 static bfd_reloc_status_type
2156 ppc_elf_unhandled_reloc (bfd *abfd,
2157                          arelent *reloc_entry,
2158                          asymbol *symbol,
2159                          void *data,
2160                          asection *input_section,
2161                          bfd *output_bfd,
2162                          char **error_message)
2163 {
2164   /* If this is a relocatable link (output_bfd test tells us), just
2165      call the generic function.  Any adjustment will be done at final
2166      link time.  */
2167   if (output_bfd != NULL)
2168     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2169                                   input_section, output_bfd, error_message);
2170
2171   if (error_message != NULL)
2172     {
2173       static char buf[60];
2174       sprintf (buf, _("generic linker can't handle %s"),
2175                reloc_entry->howto->name);
2176       *error_message = buf;
2177     }
2178   return bfd_reloc_dangerous;
2179 }
2180
2181 /* Fix bad default arch selected for a 32 bit input bfd when the
2182    default is 64 bit.  */
2183
2184 static bfd_boolean
2185 ppc_elf_object_p (bfd *abfd)
2186 {
2187   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
2188     {
2189       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2190
2191       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
2192         {
2193           /* Relies on arch after 64 bit default being 32 bit default.  */
2194           abfd->arch_info = abfd->arch_info->next;
2195           BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
2196         }
2197     }
2198   return TRUE;
2199 }
2200
2201 /* Function to set whether a module needs the -mrelocatable bit set.  */
2202
2203 static bfd_boolean
2204 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
2205 {
2206   BFD_ASSERT (!elf_flags_init (abfd)
2207               || elf_elfheader (abfd)->e_flags == flags);
2208
2209   elf_elfheader (abfd)->e_flags = flags;
2210   elf_flags_init (abfd) = TRUE;
2211   return TRUE;
2212 }
2213
2214 /* Merge backend specific data from an object file to the output
2215    object file when linking.  */
2216
2217 static bfd_boolean
2218 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2219 {
2220   flagword old_flags;
2221   flagword new_flags;
2222   bfd_boolean error;
2223
2224   /* Check if we have the same endianess.  */
2225   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2226     return FALSE;
2227
2228   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2229       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2230     return TRUE;
2231
2232   new_flags = elf_elfheader (ibfd)->e_flags;
2233   old_flags = elf_elfheader (obfd)->e_flags;
2234   if (!elf_flags_init (obfd))
2235     {
2236       /* First call, no flags set.  */
2237       elf_flags_init (obfd) = TRUE;
2238       elf_elfheader (obfd)->e_flags = new_flags;
2239     }
2240
2241   /* Compatible flags are ok.  */
2242   else if (new_flags == old_flags)
2243     ;
2244
2245   /* Incompatible flags.  */
2246   else
2247     {
2248       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
2249          to be linked with either.  */
2250       error = FALSE;
2251       if ((new_flags & EF_PPC_RELOCATABLE) != 0
2252           && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
2253         {
2254           error = TRUE;
2255           (*_bfd_error_handler)
2256             (_("%s: compiled with -mrelocatable and linked with "
2257                "modules compiled normally"),
2258              bfd_archive_filename (ibfd));
2259         }
2260       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
2261                && (old_flags & EF_PPC_RELOCATABLE) != 0)
2262         {
2263           error = TRUE;
2264           (*_bfd_error_handler)
2265             (_("%s: compiled normally and linked with "
2266                "modules compiled with -mrelocatable"),
2267              bfd_archive_filename (ibfd));
2268         }
2269
2270       /* The output is -mrelocatable-lib iff both the input files are.  */
2271       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
2272         elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
2273
2274       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
2275          but each input file is either -mrelocatable or -mrelocatable-lib.  */
2276       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
2277           && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
2278           && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
2279         elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
2280
2281       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
2282          any module uses it.  */
2283       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
2284
2285       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2286       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2287
2288       /* Warn about any other mismatches.  */
2289       if (new_flags != old_flags)
2290         {
2291           error = TRUE;
2292           (*_bfd_error_handler)
2293             (_("%s: uses different e_flags (0x%lx) fields "
2294                "than previous modules (0x%lx)"),
2295              bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
2296         }
2297
2298       if (error)
2299         {
2300           bfd_set_error (bfd_error_bad_value);
2301           return FALSE;
2302         }
2303     }
2304
2305   return TRUE;
2306 }
2307 \f
2308 /* Handle a PowerPC specific section when reading an object file.  This
2309    is called when elfcode.h finds a section with an unknown type.  */
2310
2311 static bfd_boolean
2312 ppc_elf_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr, const char *name)
2313 {
2314   asection *newsect;
2315   flagword flags;
2316
2317   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2318     return FALSE;
2319
2320   newsect = hdr->bfd_section;
2321   flags = bfd_get_section_flags (abfd, newsect);
2322   if (hdr->sh_flags & SHF_EXCLUDE)
2323     flags |= SEC_EXCLUDE;
2324
2325   if (hdr->sh_type == SHT_ORDERED)
2326     flags |= SEC_SORT_ENTRIES;
2327
2328   bfd_set_section_flags (abfd, newsect, flags);
2329   return TRUE;
2330 }
2331 \f
2332 /* Set up any other section flags and such that may be necessary.  */
2333
2334 static bfd_boolean
2335 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2336                        Elf_Internal_Shdr *shdr,
2337                        asection *asect)
2338 {
2339   if ((asect->flags & SEC_EXCLUDE) != 0)
2340     shdr->sh_flags |= SHF_EXCLUDE;
2341
2342   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2343     shdr->sh_type = SHT_ORDERED;
2344
2345   return TRUE;
2346 }
2347 \f
2348 /* Find a linker generated pointer with a given addend and type.  */
2349
2350 static elf_linker_section_pointers_t *
2351 elf_find_pointer_linker_section
2352   (elf_linker_section_pointers_t *linker_pointers,
2353    bfd_vma addend,
2354    elf_linker_section_t *lsect)
2355 {
2356   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2357     if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2358       return linker_pointers;
2359
2360   return NULL;
2361 }
2362 \f
2363 /* Allocate a pointer to live in a linker created section.  */
2364
2365 static bfd_boolean
2366 elf_create_pointer_linker_section (bfd *abfd,
2367                                    struct bfd_link_info *info,
2368                                    elf_linker_section_t *lsect,
2369                                    struct elf_link_hash_entry *h,
2370                                    const Elf_Internal_Rela *rel)
2371 {
2372   elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2373   elf_linker_section_pointers_t *linker_section_ptr;
2374   unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2375   bfd_size_type amt;
2376
2377   BFD_ASSERT (lsect != NULL);
2378
2379   /* Is this a global symbol?  */
2380   if (h != NULL)
2381     {
2382       struct ppc_elf_link_hash_entry *eh;
2383
2384       /* Has this symbol already been allocated?  If so, our work is done.  */
2385       eh = (struct ppc_elf_link_hash_entry *) h;
2386       if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2387                                            rel->r_addend,
2388                                            lsect))
2389         return TRUE;
2390
2391       ptr_linker_section_ptr = &eh->linker_section_pointer;
2392       /* Make sure this symbol is output as a dynamic symbol.  */
2393       if (h->dynindx == -1)
2394         {
2395           if (! bfd_elf_link_record_dynamic_symbol (info, h))
2396             return FALSE;
2397         }
2398
2399       if (lsect->rel_section)
2400         lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
2401     }
2402   else
2403     {
2404       /* Allocation of a pointer to a local symbol.  */
2405       elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2406
2407       /* Allocate a table to hold the local symbols if first time.  */
2408       if (!ptr)
2409         {
2410           unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2411
2412           amt = num_symbols;
2413           amt *= sizeof (elf_linker_section_pointers_t *);
2414           ptr = bfd_zalloc (abfd, amt);
2415
2416           if (!ptr)
2417             return FALSE;
2418
2419           elf_local_ptr_offsets (abfd) = ptr;
2420         }
2421
2422       /* Has this symbol already been allocated?  If so, our work is done.  */
2423       if (elf_find_pointer_linker_section (ptr[r_symndx],
2424                                            rel->r_addend,
2425                                            lsect))
2426         return TRUE;
2427
2428       ptr_linker_section_ptr = &ptr[r_symndx];
2429
2430       if (info->shared)
2431         {
2432           /* If we are generating a shared object, we need to
2433              output a R_<xxx>_RELATIVE reloc so that the
2434              dynamic linker can adjust this GOT entry.  */
2435           BFD_ASSERT (lsect->rel_section != NULL);
2436           lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
2437         }
2438     }
2439
2440   /* Allocate space for a pointer in the linker section, and allocate
2441      a new pointer record from internal memory.  */
2442   BFD_ASSERT (ptr_linker_section_ptr != NULL);
2443   amt = sizeof (elf_linker_section_pointers_t);
2444   linker_section_ptr = bfd_alloc (abfd, amt);
2445
2446   if (!linker_section_ptr)
2447     return FALSE;
2448
2449   linker_section_ptr->next = *ptr_linker_section_ptr;
2450   linker_section_ptr->addend = rel->r_addend;
2451   linker_section_ptr->lsect = lsect;
2452   linker_section_ptr->written_address_p = FALSE;
2453   *ptr_linker_section_ptr = linker_section_ptr;
2454
2455   linker_section_ptr->offset = lsect->section->_raw_size;
2456   lsect->section->_raw_size += 4;
2457
2458 #ifdef DEBUG
2459   fprintf (stderr,
2460            "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2461            lsect->name, (long) linker_section_ptr->offset,
2462            (long) lsect->section->_raw_size);
2463 #endif
2464
2465   return TRUE;
2466 }
2467 \f
2468 #define bfd_put_ptr(BFD, VAL, ADDR) bfd_put_32 (BFD, VAL, ADDR)
2469
2470 /* Fill in the address for a pointer generated in a linker section.  */
2471
2472 static bfd_vma
2473 elf_finish_pointer_linker_section (bfd *output_bfd,
2474                                    bfd *input_bfd,
2475                                    struct bfd_link_info *info,
2476                                    elf_linker_section_t *lsect,
2477                                    struct elf_link_hash_entry *h,
2478                                    bfd_vma relocation,
2479                                    const Elf_Internal_Rela *rel,
2480                                    int relative_reloc)
2481 {
2482   elf_linker_section_pointers_t *linker_section_ptr;
2483
2484   BFD_ASSERT (lsect != NULL);
2485
2486   if (h != NULL)
2487     {
2488       /* Handle global symbol.  */
2489       struct ppc_elf_link_hash_entry *eh;
2490
2491       eh = (struct ppc_elf_link_hash_entry *) h;
2492       linker_section_ptr
2493         = elf_find_pointer_linker_section (eh->linker_section_pointer,
2494                                            rel->r_addend,
2495                                            lsect);
2496
2497       BFD_ASSERT (linker_section_ptr != NULL);
2498
2499       if (! elf_hash_table (info)->dynamic_sections_created
2500           || (info->shared
2501               && info->symbolic
2502               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2503         {
2504           /* This is actually a static link, or it is a
2505              -Bsymbolic link and the symbol is defined
2506              locally.  We must initialize this entry in the
2507              global section.
2508
2509              When doing a dynamic link, we create a .rela.<xxx>
2510              relocation entry to initialize the value.  This
2511              is done in the finish_dynamic_symbol routine.  */
2512           if (!linker_section_ptr->written_address_p)
2513             {
2514               linker_section_ptr->written_address_p = TRUE;
2515               bfd_put_ptr (output_bfd,
2516                            relocation + linker_section_ptr->addend,
2517                            (lsect->section->contents
2518                             + linker_section_ptr->offset));
2519             }
2520         }
2521     }
2522   else
2523     {
2524       /* Handle local symbol.  */
2525       unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2526       BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
2527       BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
2528       linker_section_ptr = (elf_find_pointer_linker_section
2529                             (elf_local_ptr_offsets (input_bfd)[r_symndx],
2530                              rel->r_addend,
2531                              lsect));
2532
2533       BFD_ASSERT (linker_section_ptr != NULL);
2534
2535       /* Write out pointer if it hasn't been rewritten out before.  */
2536       if (!linker_section_ptr->written_address_p)
2537         {
2538           linker_section_ptr->written_address_p = TRUE;
2539           bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
2540                        lsect->section->contents + linker_section_ptr->offset);
2541
2542           if (info->shared)
2543             {
2544               /* We need to generate a relative reloc for the dynamic
2545                  linker.  */
2546
2547               asection *srel = lsect->rel_section;
2548               Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL];
2549               bfd_byte *erel;
2550               const struct elf_backend_data *bed;
2551               unsigned int i;
2552
2553               BFD_ASSERT (srel != NULL);
2554
2555               bed = get_elf_backend_data (output_bfd);
2556               for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
2557                 {
2558                   outrel[i].r_offset = (lsect->section->output_section->vma
2559                                         + lsect->section->output_offset
2560                                         + linker_section_ptr->offset);
2561                   outrel[i].r_info = 0;
2562                   outrel[i].r_addend = 0;
2563                 }
2564               outrel[0].r_info = ELF32_R_INFO (0, relative_reloc);
2565               erel = lsect->section->contents;
2566               erel += (elf_section_data (lsect->section)->rel_count++
2567                        * sizeof (Elf32_External_Rela));
2568               bfd_elf32_swap_reloca_out (output_bfd, outrel, erel);
2569             }
2570         }
2571     }
2572
2573   relocation = (lsect->section->output_offset
2574                 + linker_section_ptr->offset
2575                 - lsect->sym_offset);
2576
2577 #ifdef DEBUG
2578   fprintf (stderr,
2579            "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
2580            lsect->name, (long) relocation, (long) relocation);
2581 #endif
2582
2583   /* Subtract out the addend, because it will get added back in by the normal
2584      processing.  */
2585   return relocation - linker_section_ptr->addend;
2586 }
2587 \f
2588 /* Create a special linker section */
2589 static elf_linker_section_t *
2590 ppc_elf_create_linker_section (bfd *abfd,
2591                                struct bfd_link_info *info,
2592                                enum elf_linker_section_enum which)
2593 {
2594   elf_linker_section_t *lsect;
2595   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2596   asection *s, *sym_sec;
2597   bfd_size_type amt;
2598   flagword flags;
2599   const char *name;
2600   const char *rel_name;
2601   const char *sym_name;
2602   bfd_vma sym_offset;
2603
2604   /* Both of these sections are (technically) created by the user
2605      putting data in them, so they shouldn't be marked
2606      SEC_LINKER_CREATED.
2607
2608      The linker creates them so it has somewhere to attach their
2609      respective symbols. In fact, if they were empty it would
2610      be OK to leave the symbol set to 0 (or any random number), because
2611      the appropriate register should never be used.  */
2612   flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
2613   sym_offset = 32768;
2614
2615   switch (which)
2616     {
2617     default:
2618       abort ();
2619       return NULL;
2620
2621     case LINKER_SECTION_SDATA:  /* .sdata/.sbss section */
2622       name      = ".sdata";
2623       rel_name  = ".rela.sdata";
2624       sym_name  = "_SDA_BASE_";
2625       break;
2626
2627     case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2628       name      = ".sdata2";
2629       rel_name  = ".rela.sdata2";
2630       sym_name  = "_SDA2_BASE_";
2631       flags    |= SEC_READONLY;
2632       break;
2633     }
2634
2635   /* Record the first bfd that needs the special sections.  */
2636   if (!htab->elf.dynobj)
2637     htab->elf.dynobj = abfd;
2638
2639   amt = sizeof (elf_linker_section_t);
2640   lsect = bfd_zalloc (htab->elf.dynobj, amt);
2641
2642   lsect->sym_offset = sym_offset;
2643
2644   /* See if the sections already exist.  */
2645   sym_sec = s = bfd_get_section_by_name (htab->elf.dynobj, name);
2646   if (s == NULL || (s->flags & flags) != flags)
2647     {
2648       s = bfd_make_section_anyway (htab->elf.dynobj, name);
2649       if (s == NULL
2650           || !bfd_set_section_flags (htab->elf.dynobj, s, flags))
2651         return NULL;
2652       if (sym_sec == NULL)
2653         sym_sec = s;
2654     }
2655   lsect->section = s;
2656
2657   if (bfd_get_section_alignment (htab->elf.dynobj, s) < 2
2658       && !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2659     return NULL;
2660
2661   s->_raw_size = align_power (s->_raw_size, 2);
2662
2663 #ifdef DEBUG
2664   fprintf (stderr, "Creating section %s, current size = %ld\n",
2665            name, (long) s->_raw_size);
2666 #endif
2667
2668   if (sym_name)
2669     {
2670       struct elf_link_hash_entry *h;
2671       struct bfd_link_hash_entry *bh;
2672
2673 #ifdef DEBUG
2674       fprintf (stderr, "Adding %s to section %s\n", sym_name, name);
2675 #endif
2676       bh = bfd_link_hash_lookup (info->hash, sym_name,
2677                                  FALSE, FALSE, FALSE);
2678
2679       if ((bh == NULL || bh->type == bfd_link_hash_undefined)
2680           && !(_bfd_generic_link_add_one_symbol
2681                (info, abfd, sym_name, BSF_GLOBAL, sym_sec, sym_offset, NULL,
2682                 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2683         return NULL;
2684       h = (struct elf_link_hash_entry *) bh;
2685
2686       h->type = STT_OBJECT;
2687       lsect->sym_hash = h;
2688
2689       if (info->shared
2690           && ! bfd_elf_link_record_dynamic_symbol (info, h))
2691         return NULL;
2692     }
2693
2694   if (info->shared)
2695     {
2696       s = bfd_make_section_anyway (htab->elf.dynobj, rel_name);
2697       lsect->rel_section = s;
2698       flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2699                | SEC_LINKER_CREATED | SEC_READONLY);
2700       if (s == NULL
2701           || ! bfd_set_section_flags (htab->elf.dynobj, s, flags)
2702           || ! bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2703         return NULL;
2704     }
2705
2706   return lsect;
2707 }
2708 \f
2709 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
2710    need to bump up the number of section headers.  */
2711
2712 static int
2713 ppc_elf_additional_program_headers (bfd *abfd)
2714 {
2715   asection *s;
2716   int ret;
2717
2718   ret = 0;
2719
2720   s = bfd_get_section_by_name (abfd, ".interp");
2721   if (s != NULL)
2722     ++ret;
2723
2724   s = bfd_get_section_by_name (abfd, ".sbss2");
2725   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2726     ++ret;
2727
2728   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2729   if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2730     ++ret;
2731
2732   return ret;
2733 }
2734
2735 /* Modify the segment map if needed.  */
2736
2737 static bfd_boolean
2738 ppc_elf_modify_segment_map (bfd *abfd ATTRIBUTE_UNUSED,
2739                             struct bfd_link_info *info ATTRIBUTE_UNUSED)
2740 {
2741   return TRUE;
2742 }
2743 \f
2744 /* The powerpc .got has a blrl instruction in it.  Mark it executable.  */
2745
2746 static bfd_boolean
2747 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2748 {
2749   struct ppc_elf_link_hash_table *htab;
2750   asection *s;
2751   flagword flags;
2752
2753   if (!_bfd_elf_create_got_section (abfd, info))
2754     return FALSE;
2755
2756   htab = ppc_elf_hash_table (info);
2757   htab->got = s = bfd_get_section_by_name (abfd, ".got");
2758   if (s == NULL)
2759     abort ();
2760
2761   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2762            | SEC_LINKER_CREATED);
2763   if (!bfd_set_section_flags (abfd, s, flags))
2764     return FALSE;
2765
2766   htab->relgot = bfd_make_section (abfd, ".rela.got");
2767   if (!htab->relgot
2768       || ! bfd_set_section_flags (abfd, htab->relgot,
2769                                   (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2770                                    | SEC_IN_MEMORY | SEC_LINKER_CREATED
2771                                    | SEC_READONLY))
2772       || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2773     return FALSE;
2774
2775   return TRUE;
2776 }
2777
2778 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2779    to output sections (just like _bfd_elf_create_dynamic_sections has
2780    to create .dynbss and .rela.bss).  */
2781
2782 static bfd_boolean
2783 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2784 {
2785   struct ppc_elf_link_hash_table *htab;
2786   asection *s;
2787   flagword flags;
2788
2789   htab = ppc_elf_hash_table (info);
2790
2791   if (htab->got == NULL
2792       && !ppc_elf_create_got (abfd, info))
2793     return FALSE;
2794
2795   if (!_bfd_elf_create_dynamic_sections (abfd, info))
2796     return FALSE;
2797
2798   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2799            | SEC_LINKER_CREATED);
2800
2801   htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2802   htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2803   if (s == NULL
2804       || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2805     return FALSE;
2806
2807   if (! info->shared)
2808     {
2809       htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2810       htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2811       if (s == NULL
2812           || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2813           || ! bfd_set_section_alignment (abfd, s, 2))
2814         return FALSE;
2815     }
2816
2817   htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2818   htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2819   if (s == NULL)
2820     abort ();
2821
2822   flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2823   return bfd_set_section_flags (abfd, s, flags);
2824 }
2825
2826 /* Adjust a symbol defined by a dynamic object and referenced by a
2827    regular object.  The current definition is in some section of the
2828    dynamic object, but we're not including those sections.  We have to
2829    change the definition to something the rest of the link can
2830    understand.  */
2831
2832 static bfd_boolean
2833 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2834                                struct elf_link_hash_entry *h)
2835 {
2836   struct ppc_elf_link_hash_table *htab;
2837   asection *s;
2838   unsigned int power_of_two;
2839
2840 #ifdef DEBUG
2841   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
2842            h->root.root.string);
2843 #endif
2844
2845   /* Make sure we know what is going on here.  */
2846   htab = ppc_elf_hash_table (info);
2847   BFD_ASSERT (htab->elf.dynobj != NULL
2848               && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2849                   || h->weakdef != NULL
2850                   || ((h->elf_link_hash_flags
2851                        & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2852                       && (h->elf_link_hash_flags
2853                           & ELF_LINK_HASH_REF_REGULAR) != 0
2854                       && (h->elf_link_hash_flags
2855                           & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2856
2857   /* Deal with function syms.  */
2858   if (h->type == STT_FUNC
2859       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2860     {
2861       /* Clear procedure linkage table information for any symbol that
2862          won't need a .plt entry.  */
2863       if (h->plt.refcount <= 0
2864           || SYMBOL_CALLS_LOCAL (info, h)
2865           || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2866               && h->root.type == bfd_link_hash_undefweak))
2867         {
2868           /* A PLT entry is not required/allowed when:
2869
2870              1. We are not using ld.so; because then the PLT entry
2871              can't be set up, so we can't use one.  In this case,
2872              ppc_elf_adjust_dynamic_symbol won't even be called.
2873
2874              2. GC has rendered the entry unused.
2875
2876              3. We know for certain that a call to this symbol
2877              will go to this object, or will remain undefined.  */
2878           h->plt.offset = (bfd_vma) -1;
2879           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2880         }
2881       return TRUE;
2882     }
2883   else
2884     h->plt.offset = (bfd_vma) -1;
2885
2886   /* If this is a weak symbol, and there is a real definition, the
2887      processor independent code will have arranged for us to see the
2888      real definition first, and we can just use the same value.  */
2889   if (h->weakdef != NULL)
2890     {
2891       BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2892                   || h->weakdef->root.type == bfd_link_hash_defweak);
2893       h->root.u.def.section = h->weakdef->root.u.def.section;
2894       h->root.u.def.value = h->weakdef->root.u.def.value;
2895       if (ELIMINATE_COPY_RELOCS)
2896         h->elf_link_hash_flags
2897           = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
2898              | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
2899       return TRUE;
2900     }
2901
2902   /* This is a reference to a symbol defined by a dynamic object which
2903      is not a function.  */
2904
2905   /* If we are creating a shared library, we must presume that the
2906      only references to the symbol are via the global offset table.
2907      For such cases we need not do anything here; the relocations will
2908      be handled correctly by relocate_section.  */
2909   if (info->shared)
2910     return TRUE;
2911
2912   /* If there are no references to this symbol that do not use the
2913      GOT, we don't need to generate a copy reloc.  */
2914   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2915     return TRUE;
2916
2917   if (ELIMINATE_COPY_RELOCS)
2918     {
2919       struct ppc_elf_dyn_relocs *p;
2920       for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2921         {
2922           s = p->sec->output_section;
2923           if (s != NULL && (s->flags & SEC_READONLY) != 0)
2924             break;
2925         }
2926
2927       /* If we didn't find any dynamic relocs in read-only sections, then
2928          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
2929       if (p == NULL)
2930         {
2931           h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2932           return TRUE;
2933         }
2934     }
2935
2936   /* We must allocate the symbol in our .dynbss section, which will
2937      become part of the .bss section of the executable.  There will be
2938      an entry for this symbol in the .dynsym section.  The dynamic
2939      object will contain position independent code, so all references
2940      from the dynamic object to this symbol will go through the global
2941      offset table.  The dynamic linker will use the .dynsym entry to
2942      determine the address it must put in the global offset table, so
2943      both the dynamic object and the regular object will refer to the
2944      same memory location for the variable.
2945
2946      Of course, if the symbol is sufficiently small, we must instead
2947      allocate it in .sbss.  FIXME: It would be better to do this if and
2948      only if there were actually SDAREL relocs for that symbol.  */
2949
2950   if (h->size <= elf_gp_size (htab->elf.dynobj))
2951     s = htab->dynsbss;
2952   else
2953     s = htab->dynbss;
2954   BFD_ASSERT (s != NULL);
2955
2956   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2957      copy the initial value out of the dynamic object and into the
2958      runtime process image.  We need to remember the offset into the
2959      .rela.bss section we are going to use.  */
2960   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2961     {
2962       asection *srel;
2963
2964       if (h->size <= elf_gp_size (htab->elf.dynobj))
2965         srel = htab->relsbss;
2966       else
2967         srel = htab->relbss;
2968       BFD_ASSERT (srel != NULL);
2969       srel->_raw_size += sizeof (Elf32_External_Rela);
2970       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2971     }
2972
2973   /* We need to figure out the alignment required for this symbol.  I
2974      have no idea how ELF linkers handle this.  */
2975   power_of_two = bfd_log2 (h->size);
2976   if (power_of_two > 4)
2977     power_of_two = 4;
2978
2979   /* Apply the required alignment.  */
2980   s->_raw_size = BFD_ALIGN (s->_raw_size,
2981                             (bfd_size_type) (1 << power_of_two));
2982   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
2983     {
2984       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
2985         return FALSE;
2986     }
2987
2988   /* Define the symbol as being at this point in the section.  */
2989   h->root.u.def.section = s;
2990   h->root.u.def.value = s->_raw_size;
2991
2992   /* Increment the section size to make room for the symbol.  */
2993   s->_raw_size += h->size;
2994
2995   return TRUE;
2996 }
2997 \f
2998 /* Of those relocs that might be copied as dynamic relocs, this macro
2999    selects those that must be copied when linking a shared library,
3000    even when the symbol is local.  */
3001
3002 #define MUST_BE_DYN_RELOC(RTYPE)                \
3003   ((RTYPE) != R_PPC_REL24                       \
3004    && (RTYPE) != R_PPC_REL14                    \
3005    && (RTYPE) != R_PPC_REL14_BRTAKEN            \
3006    && (RTYPE) != R_PPC_REL14_BRNTAKEN           \
3007    && (RTYPE) != R_PPC_REL32)
3008
3009 /* Allocate space in associated reloc sections for dynamic relocs.  */
3010
3011 static bfd_boolean
3012 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3013 {
3014   struct bfd_link_info *info = inf;
3015   struct ppc_elf_link_hash_entry *eh;
3016   struct ppc_elf_link_hash_table *htab;
3017   struct ppc_elf_dyn_relocs *p;
3018
3019   if (h->root.type == bfd_link_hash_indirect)
3020     return TRUE;
3021
3022   if (h->root.type == bfd_link_hash_warning)
3023     /* When warning symbols are created, they **replace** the "real"
3024        entry in the hash table, thus we never get to see the real
3025        symbol in a hash traversal.  So look at it now.  */
3026     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3027
3028   htab = ppc_elf_hash_table (info);
3029   if (htab->elf.dynamic_sections_created
3030       && h->plt.refcount > 0)
3031     {
3032       /* Make sure this symbol is output as a dynamic symbol.  */
3033       if (h->dynindx == -1
3034           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3035         {
3036           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3037             return FALSE;
3038         }
3039
3040       if (info->shared
3041           || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3042         {
3043           asection *s = htab->plt;
3044
3045           /* If this is the first .plt entry, make room for the special
3046              first entry.  */
3047           if (s->_raw_size == 0)
3048             s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
3049
3050           /* The PowerPC PLT is actually composed of two parts, the
3051              first part is 2 words (for a load and a jump), and then
3052              there is a remaining word available at the end.  */
3053           h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3054                            + (PLT_SLOT_SIZE
3055                               * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
3056                                  / PLT_ENTRY_SIZE)));
3057
3058           /* If this symbol is not defined in a regular file, and we
3059              are not generating a shared library, then set the symbol
3060              to this location in the .plt.  This is required to make
3061              function pointers compare as equal between the normal
3062              executable and the shared library.  */
3063           if (! info->shared
3064               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3065             {
3066               h->root.u.def.section = s;
3067               h->root.u.def.value = h->plt.offset;
3068             }
3069
3070           /* Make room for this entry.  After the 8192nd entry, room
3071              for two entries is allocated.  */
3072           s->_raw_size += PLT_ENTRY_SIZE;
3073           if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3074               > PLT_NUM_SINGLE_ENTRIES)
3075             s->_raw_size += PLT_ENTRY_SIZE;
3076
3077           /* We also need to make an entry in the .rela.plt section.  */
3078           htab->relplt->_raw_size += sizeof (Elf32_External_Rela);
3079         }
3080       else
3081         {
3082           h->plt.offset = (bfd_vma) -1;
3083           h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3084         }
3085     }
3086   else
3087     {
3088       h->plt.offset = (bfd_vma) -1;
3089       h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3090     }
3091
3092   eh = (struct ppc_elf_link_hash_entry *) h;
3093   if (eh->elf.got.refcount > 0)
3094     {
3095       /* Make sure this symbol is output as a dynamic symbol.  */
3096       if (eh->elf.dynindx == -1
3097           && (eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3098         {
3099           if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
3100             return FALSE;
3101         }
3102
3103       if (eh->tls_mask == (TLS_TLS | TLS_LD)
3104           && !(eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
3105         /* If just an LD reloc, we'll just use htab->tlsld_got.offset.  */
3106         eh->elf.got.offset = (bfd_vma) -1;
3107       else
3108         {
3109           bfd_boolean dyn;
3110           eh->elf.got.offset = htab->got->_raw_size;
3111           if ((eh->tls_mask & TLS_TLS) != 0)
3112             {
3113               if ((eh->tls_mask & TLS_LD) != 0)
3114                 htab->got->_raw_size += 8;
3115               if ((eh->tls_mask & TLS_GD) != 0)
3116                 htab->got->_raw_size += 8;
3117               if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
3118                 htab->got->_raw_size += 4;
3119               if ((eh->tls_mask & TLS_DTPREL) != 0)
3120                 htab->got->_raw_size += 4;
3121             }
3122           else
3123             htab->got->_raw_size += 4;
3124           dyn = htab->elf.dynamic_sections_created;
3125           if ((info->shared
3126                || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
3127               && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
3128                   || eh->elf.root.type != bfd_link_hash_undefweak))
3129             {
3130               /* All the entries we allocated need relocs.  */
3131               htab->relgot->_raw_size
3132                 += ((htab->got->_raw_size - eh->elf.got.offset) / 4
3133                     * sizeof (Elf32_External_Rela));
3134               /* Except LD only needs one.  */
3135               if ((eh->tls_mask & TLS_LD) != 0)
3136                 htab->relgot->_raw_size -= sizeof (Elf32_External_Rela);
3137             }
3138         }
3139     }
3140   else
3141     eh->elf.got.offset = (bfd_vma) -1;
3142
3143   if (eh->dyn_relocs == NULL)
3144     return TRUE;
3145
3146   /* In the shared -Bsymbolic case, discard space allocated for
3147      dynamic pc-relative relocs against symbols which turn out to be
3148      defined in regular objects.  For the normal shared case, discard
3149      space for relocs that have become local due to symbol visibility
3150      changes.  */
3151
3152   if (info->shared)
3153     {
3154       /* Relocs that use pc_count are those that appear on a call insn,
3155          or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
3156          generated via assembly.  We want calls to protected symbols to
3157          resolve directly to the function rather than going via the plt.
3158          If people want function pointer comparisons to work as expected
3159          then they should avoid writing weird assembly.  */
3160       if (SYMBOL_CALLS_LOCAL (info, h))
3161         {
3162           struct ppc_elf_dyn_relocs **pp;
3163
3164           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3165             {
3166               p->count -= p->pc_count;
3167               p->pc_count = 0;
3168               if (p->count == 0)
3169                 *pp = p->next;
3170               else
3171                 pp = &p->next;
3172             }
3173         }
3174
3175       /* Also discard relocs on undefined weak syms with non-default
3176          visibility.  */
3177       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3178           && h->root.type == bfd_link_hash_undefweak)
3179         eh->dyn_relocs = NULL;
3180
3181       /* Make sure undefined weak symbols are output as a dynamic symbol
3182          in PIEs.  */
3183       if (info->pie
3184           && eh->dyn_relocs != NULL
3185           && h->dynindx == -1
3186           && h->root.type == bfd_link_hash_undefweak
3187           && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3188         {
3189           if (! bfd_elf_link_record_dynamic_symbol (info, h))
3190             return FALSE;
3191         }
3192     }
3193   else if (ELIMINATE_COPY_RELOCS)
3194     {
3195       /* For the non-shared case, discard space for relocs against
3196          symbols which turn out to need copy relocs or are not
3197          dynamic.  */
3198
3199       if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3200           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3201           && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3202         {
3203           /* Make sure this symbol is output as a dynamic symbol.
3204              Undefined weak syms won't yet be marked as dynamic.  */
3205           if (h->dynindx == -1
3206               && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3207             {
3208               if (! bfd_elf_link_record_dynamic_symbol (info, h))
3209                 return FALSE;
3210             }
3211
3212           /* If that succeeded, we know we'll be keeping all the
3213              relocs.  */
3214           if (h->dynindx != -1)
3215             goto keep;
3216         }
3217
3218       eh->dyn_relocs = NULL;
3219
3220     keep: ;
3221     }
3222
3223   /* Finally, allocate space.  */
3224   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3225     {
3226       asection *sreloc = elf_section_data (p->sec)->sreloc;
3227       sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
3228     }
3229
3230   return TRUE;
3231 }
3232
3233 /* Find any dynamic relocs that apply to read-only sections.  */
3234
3235 static bfd_boolean
3236 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
3237 {
3238   struct ppc_elf_dyn_relocs *p;
3239
3240   if (h->root.type == bfd_link_hash_indirect)
3241     return TRUE;
3242
3243   if (h->root.type == bfd_link_hash_warning)
3244     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3245
3246   for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3247     {
3248       asection *s = p->sec->output_section;
3249
3250       if (s != NULL
3251           && ((s->flags & (SEC_READONLY | SEC_ALLOC))
3252               == (SEC_READONLY | SEC_ALLOC)))
3253         {
3254           ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
3255
3256           /* Not an error, just cut short the traversal.  */
3257           return FALSE;
3258         }
3259     }
3260   return TRUE;
3261 }
3262
3263 /* Set the sizes of the dynamic sections.  */
3264
3265 static bfd_boolean
3266 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3267                                struct bfd_link_info *info)
3268 {
3269   struct ppc_elf_link_hash_table *htab;
3270   asection *s;
3271   bfd_boolean relocs;
3272   bfd *ibfd;
3273
3274 #ifdef DEBUG
3275   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
3276 #endif
3277
3278   htab = ppc_elf_hash_table (info);
3279   BFD_ASSERT (htab->elf.dynobj != NULL);
3280
3281   if (elf_hash_table (info)->dynamic_sections_created)
3282     {
3283       /* Set the contents of the .interp section to the interpreter.  */
3284       if (info->executable)
3285         {
3286           s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
3287           BFD_ASSERT (s != NULL);
3288           s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3289           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3290         }
3291     }
3292
3293   if (htab->tlsld_got.refcount > 0)
3294     {
3295       htab->tlsld_got.offset = htab->got->_raw_size;
3296       htab->got->_raw_size += 8;
3297       if (info->shared)
3298         htab->relgot->_raw_size += sizeof (Elf32_External_Rela);
3299     }
3300   else
3301     htab->tlsld_got.offset = (bfd_vma) -1;
3302
3303   /* Set up .got offsets for local syms, and space for local dynamic
3304      relocs.  */
3305   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3306     {
3307       bfd_signed_vma *local_got;
3308       bfd_signed_vma *end_local_got;
3309       char *lgot_masks;
3310       bfd_size_type locsymcount;
3311       Elf_Internal_Shdr *symtab_hdr;
3312       asection *srel;
3313
3314       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3315         continue;
3316
3317       for (s = ibfd->sections; s != NULL; s = s->next)
3318         {
3319           struct ppc_elf_dyn_relocs *p;
3320
3321           for (p = ((struct ppc_elf_dyn_relocs *)
3322                     elf_section_data (s)->local_dynrel);
3323                p != NULL;
3324                p = p->next)
3325             {
3326               if (!bfd_is_abs_section (p->sec)
3327                   && bfd_is_abs_section (p->sec->output_section))
3328                 {
3329                   /* Input section has been discarded, either because
3330                      it is a copy of a linkonce section or due to
3331                      linker script /DISCARD/, so we'll be discarding
3332                      the relocs too.  */
3333                 }
3334               else if (p->count != 0)
3335                 {
3336                   elf_section_data (p->sec)->sreloc->_raw_size
3337                     += p->count * sizeof (Elf32_External_Rela);
3338                   if ((p->sec->output_section->flags
3339                        & (SEC_READONLY | SEC_ALLOC))
3340                       == (SEC_READONLY | SEC_ALLOC))
3341                     info->flags |= DF_TEXTREL;
3342                 }
3343             }
3344         }
3345
3346       local_got = elf_local_got_refcounts (ibfd);
3347       if (!local_got)
3348         continue;
3349
3350       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3351       locsymcount = symtab_hdr->sh_info;
3352       end_local_got = local_got + locsymcount;
3353       lgot_masks = (char *) end_local_got;
3354       s = htab->got;
3355       srel = htab->relgot;
3356       for (; local_got < end_local_got; ++local_got, ++lgot_masks)
3357         if (*local_got > 0)
3358           {
3359             if (*lgot_masks == (TLS_TLS | TLS_LD))
3360               {
3361                 /* If just an LD reloc, we'll just use
3362                    htab->tlsld_got.offset.  */
3363                 if (htab->tlsld_got.offset == (bfd_vma) -1)
3364                   {
3365                     htab->tlsld_got.offset = s->_raw_size;
3366                     s->_raw_size += 8;
3367                     if (info->shared)
3368                       srel->_raw_size += sizeof (Elf32_External_Rela);
3369                   }
3370                 *local_got = (bfd_vma) -1;
3371               }
3372             else
3373               {
3374                 *local_got = s->_raw_size;
3375                 if ((*lgot_masks & TLS_TLS) != 0)
3376                   {
3377                     if ((*lgot_masks & TLS_GD) != 0)
3378                       s->_raw_size += 8;
3379                     if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
3380                       s->_raw_size += 4;
3381                     if ((*lgot_masks & TLS_DTPREL) != 0)
3382                       s->_raw_size += 4;
3383                   }
3384                 else
3385                   s->_raw_size += 4;
3386                 if (info->shared)
3387                   srel->_raw_size += ((s->_raw_size - *local_got) / 4
3388                                       * sizeof (Elf32_External_Rela));
3389               }
3390           }
3391         else
3392           *local_got = (bfd_vma) -1;
3393     }
3394
3395   /* Allocate space for global sym dynamic relocs.  */
3396   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
3397
3398   /* We've now determined the sizes of the various dynamic sections.
3399      Allocate memory for them.  */
3400   relocs = FALSE;
3401   for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
3402     {
3403       if ((s->flags & SEC_LINKER_CREATED) == 0)
3404         continue;
3405
3406       if (s == htab->plt
3407           || s == htab->got
3408           || (htab->sdata != NULL && s == htab->sdata->section)
3409           || (htab->sdata2 != NULL && s == htab->sdata2->section))
3410         {
3411           /* Strip this section if we don't need it; see the
3412              comment below.  */
3413         }
3414       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3415         {
3416           if (s->_raw_size == 0)
3417             {
3418               /* If we don't need this section, strip it from the
3419                  output file.  This is mostly to handle .rela.bss and
3420                  .rela.plt.  We must create both sections in
3421                  create_dynamic_sections, because they must be created
3422                  before the linker maps input sections to output
3423                  sections.  The linker does that before
3424                  adjust_dynamic_symbol is called, and it is that
3425                  function which decides whether anything needs to go
3426                  into these sections.  */
3427             }
3428           else
3429             {
3430               /* Remember whether there are any relocation sections.  */
3431               relocs = TRUE;
3432
3433               /* We use the reloc_count field as a counter if we need
3434                  to copy relocs into the output file.  */
3435               s->reloc_count = 0;
3436             }
3437         }
3438       else
3439         {
3440           /* It's not one of our sections, so don't allocate space.  */
3441           continue;
3442         }
3443
3444       if (s->_raw_size == 0)
3445         {
3446           _bfd_strip_section_from_output (info, s);
3447           continue;
3448         }
3449
3450       /* Allocate memory for the section contents.  */
3451       s->contents = bfd_zalloc (htab->elf.dynobj, s->_raw_size);
3452       if (s->contents == NULL)
3453         return FALSE;
3454     }
3455
3456   if (htab->elf.dynamic_sections_created)
3457     {
3458       /* Add some entries to the .dynamic section.  We fill in the
3459          values later, in ppc_elf_finish_dynamic_sections, but we
3460          must add the entries now so that we get the correct size for
3461          the .dynamic section.  The DT_DEBUG entry is filled in by the
3462          dynamic linker and used by the debugger.  */
3463 #define add_dynamic_entry(TAG, VAL) \
3464   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3465
3466       if (info->executable)
3467         {
3468           if (!add_dynamic_entry (DT_DEBUG, 0))
3469             return FALSE;
3470         }
3471
3472       if (htab->plt != NULL && htab->plt->_raw_size != 0)
3473         {
3474           if (!add_dynamic_entry (DT_PLTGOT, 0)
3475               || !add_dynamic_entry (DT_PLTRELSZ, 0)
3476               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3477               || !add_dynamic_entry (DT_JMPREL, 0))
3478             return FALSE;
3479         }
3480
3481       if (relocs)
3482         {
3483           if (!add_dynamic_entry (DT_RELA, 0)
3484               || !add_dynamic_entry (DT_RELASZ, 0)
3485               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3486             return FALSE;
3487         }
3488
3489       /* If any dynamic relocs apply to a read-only section, then we
3490          need a DT_TEXTREL entry.  */
3491       if ((info->flags & DF_TEXTREL) == 0)
3492         elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
3493                                 info);
3494
3495       if ((info->flags & DF_TEXTREL) != 0)
3496         {
3497           if (!add_dynamic_entry (DT_TEXTREL, 0))
3498             return FALSE;
3499         }
3500     }
3501 #undef add_dynamic_entry
3502
3503   return TRUE;
3504 }
3505 \f
3506 static bfd_boolean
3507 update_local_sym_info (bfd *abfd,
3508                        Elf_Internal_Shdr *symtab_hdr,
3509                        unsigned long r_symndx,
3510                        int tls_type)
3511 {
3512   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3513   char *local_got_tls_masks;
3514
3515   if (local_got_refcounts == NULL)
3516     {
3517       bfd_size_type size = symtab_hdr->sh_info;
3518
3519       size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
3520       local_got_refcounts = bfd_zalloc (abfd, size);
3521       if (local_got_refcounts == NULL)
3522         return FALSE;
3523       elf_local_got_refcounts (abfd) = local_got_refcounts;
3524     }
3525
3526   local_got_refcounts[r_symndx] += 1;
3527   local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
3528   local_got_tls_masks[r_symndx] |= tls_type;
3529   return TRUE;
3530 }
3531
3532 static void
3533 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3534 {
3535   (*_bfd_error_handler)
3536     (_("%s: relocation %s cannot be used when making a shared object"),
3537      bfd_archive_filename (abfd),
3538      ppc_elf_howto_table[r_type]->name);
3539   bfd_set_error (bfd_error_bad_value);
3540 }
3541
3542 /* Look through the relocs for a section during the first phase, and
3543    allocate space in the global offset table or procedure linkage
3544    table.  */
3545
3546 static bfd_boolean
3547 ppc_elf_check_relocs (bfd *abfd,
3548                       struct bfd_link_info *info,
3549                       asection *sec,
3550                       const Elf_Internal_Rela *relocs)
3551 {
3552   struct ppc_elf_link_hash_table *htab;
3553   Elf_Internal_Shdr *symtab_hdr;
3554   struct elf_link_hash_entry **sym_hashes;
3555   const Elf_Internal_Rela *rel;
3556   const Elf_Internal_Rela *rel_end;
3557   asection *sreloc;
3558
3559   if (info->relocatable)
3560     return TRUE;
3561
3562 #ifdef DEBUG
3563   fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
3564            bfd_get_section_name (abfd, sec),
3565            bfd_archive_filename (abfd));
3566 #endif
3567
3568   /* Initialize howto table if not already done.  */
3569   if (!ppc_elf_howto_table[R_PPC_ADDR32])
3570     ppc_elf_howto_init ();
3571
3572   /* Create the linker generated sections all the time so that the
3573      special symbols are created.  */
3574   htab = ppc_elf_hash_table (info);
3575   if (htab->sdata == NULL)
3576     {
3577       htab->sdata = ppc_elf_create_linker_section (abfd, info,
3578                                                    LINKER_SECTION_SDATA);
3579       if (htab->sdata == NULL)
3580         return FALSE;
3581     }
3582
3583   if (htab->sdata2 == NULL)
3584     {
3585       htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
3586                                                     LINKER_SECTION_SDATA2);
3587       if (htab->sdata2 == NULL)
3588         return FALSE;
3589     }
3590
3591   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3592   sym_hashes = elf_sym_hashes (abfd);
3593   sreloc = NULL;
3594
3595   rel_end = relocs + sec->reloc_count;
3596   for (rel = relocs; rel < rel_end; rel++)
3597     {
3598       unsigned long r_symndx;
3599       enum elf_ppc_reloc_type r_type;
3600       struct elf_link_hash_entry *h;
3601       int tls_type = 0;
3602
3603       r_symndx = ELF32_R_SYM (rel->r_info);
3604       if (r_symndx < symtab_hdr->sh_info)
3605         h = NULL;
3606       else
3607         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3608
3609       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3610          This shows up in particular in an R_PPC_ADDR32 in the eabi
3611          startup code.  */
3612       if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3613         {
3614           if (htab->got == NULL)
3615             {
3616               if (htab->elf.dynobj == NULL)
3617                 htab->elf.dynobj = abfd;
3618               if (!ppc_elf_create_got (htab->elf.dynobj, info))
3619                 return FALSE;
3620             }
3621         }
3622
3623       r_type = ELF32_R_TYPE (rel->r_info);
3624       switch (r_type)
3625         {
3626         case R_PPC_GOT_TLSLD16:
3627         case R_PPC_GOT_TLSLD16_LO:
3628         case R_PPC_GOT_TLSLD16_HI:
3629         case R_PPC_GOT_TLSLD16_HA:
3630           htab->tlsld_got.refcount += 1;
3631           tls_type = TLS_TLS | TLS_LD;
3632           goto dogottls;
3633
3634         case R_PPC_GOT_TLSGD16:
3635         case R_PPC_GOT_TLSGD16_LO:
3636         case R_PPC_GOT_TLSGD16_HI:
3637         case R_PPC_GOT_TLSGD16_HA:
3638           tls_type = TLS_TLS | TLS_GD;
3639           goto dogottls;
3640
3641         case R_PPC_GOT_TPREL16:
3642         case R_PPC_GOT_TPREL16_LO:
3643         case R_PPC_GOT_TPREL16_HI:
3644         case R_PPC_GOT_TPREL16_HA:
3645           if (info->shared)
3646             info->flags |= DF_STATIC_TLS;
3647           tls_type = TLS_TLS | TLS_TPREL;
3648           goto dogottls;
3649
3650         case R_PPC_GOT_DTPREL16:
3651         case R_PPC_GOT_DTPREL16_LO:
3652         case R_PPC_GOT_DTPREL16_HI:
3653         case R_PPC_GOT_DTPREL16_HA:
3654           tls_type = TLS_TLS | TLS_DTPREL;
3655         dogottls:
3656           sec->has_tls_reloc = 1;
3657           /* Fall thru */
3658
3659           /* GOT16 relocations */
3660         case R_PPC_GOT16:
3661         case R_PPC_GOT16_LO:
3662         case R_PPC_GOT16_HI:
3663         case R_PPC_GOT16_HA:
3664           /* This symbol requires a global offset table entry.  */
3665           if (htab->got == NULL)
3666             {
3667               if (htab->elf.dynobj == NULL)
3668                 htab->elf.dynobj = abfd;
3669               if (!ppc_elf_create_got (htab->elf.dynobj, info))
3670                 return FALSE;
3671             }
3672           if (h != NULL)
3673             {
3674               h->got.refcount += 1;
3675               ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3676             }
3677           else
3678             /* This is a global offset table entry for a local symbol.  */
3679             if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3680               return FALSE;
3681           break;
3682
3683           /* Indirect .sdata relocation.  */
3684         case R_PPC_EMB_SDAI16:
3685           if (info->shared)
3686             {
3687               bad_shared_reloc (abfd, r_type);
3688               return FALSE;
3689             }
3690           if (!elf_create_pointer_linker_section (abfd, info,
3691                                                   htab->sdata, h, rel))
3692             return FALSE;
3693           break;
3694
3695           /* Indirect .sdata2 relocation.  */
3696         case R_PPC_EMB_SDA2I16:
3697           if (info->shared)
3698             {
3699               bad_shared_reloc (abfd, r_type);
3700               return FALSE;
3701             }
3702           if (!elf_create_pointer_linker_section (abfd, info,
3703                                                   htab->sdata2, h, rel))
3704             return FALSE;
3705           break;
3706
3707         case R_PPC_SDAREL16:
3708         case R_PPC_EMB_SDA2REL:
3709         case R_PPC_EMB_SDA21:
3710         case R_PPC_EMB_RELSDA:
3711         case R_PPC_EMB_NADDR32:
3712         case R_PPC_EMB_NADDR16:
3713         case R_PPC_EMB_NADDR16_LO:
3714         case R_PPC_EMB_NADDR16_HI:
3715         case R_PPC_EMB_NADDR16_HA:
3716           if (info->shared)
3717             {
3718               bad_shared_reloc (abfd, r_type);
3719               return FALSE;
3720             }
3721           break;
3722
3723         case R_PPC_PLT32:
3724         case R_PPC_PLTREL24:
3725         case R_PPC_PLTREL32:
3726         case R_PPC_PLT16_LO:
3727         case R_PPC_PLT16_HI:
3728         case R_PPC_PLT16_HA:
3729 #ifdef DEBUG
3730           fprintf (stderr, "Reloc requires a PLT entry\n");
3731 #endif
3732           /* This symbol requires a procedure linkage table entry.  We
3733              actually build the entry in finish_dynamic_symbol,
3734              because this might be a case of linking PIC code without
3735              linking in any dynamic objects, in which case we don't
3736              need to generate a procedure linkage table after all.  */
3737
3738           if (h == NULL)
3739             {
3740               /* It does not make sense to have a procedure linkage
3741                  table entry for a local symbol.  */
3742               (*_bfd_error_handler) (_("%s(%s+0x%lx): %s reloc against "
3743                                        "local symbol"),
3744                                      bfd_archive_filename (abfd),
3745                                      sec->name,
3746                                      (long) rel->r_offset,
3747                                      ppc_elf_howto_table[r_type]->name);
3748               bfd_set_error (bfd_error_bad_value);
3749               return FALSE;
3750             }
3751
3752           h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3753           h->plt.refcount++;
3754           break;
3755
3756           /* The following relocations don't need to propagate the
3757              relocation if linking a shared object since they are
3758              section relative.  */
3759         case R_PPC_SECTOFF:
3760         case R_PPC_SECTOFF_LO:
3761         case R_PPC_SECTOFF_HI:
3762         case R_PPC_SECTOFF_HA:
3763         case R_PPC_DTPREL16:
3764         case R_PPC_DTPREL16_LO:
3765         case R_PPC_DTPREL16_HI:
3766         case R_PPC_DTPREL16_HA:
3767         case R_PPC_TOC16:
3768           break;
3769
3770           /* This are just markers.  */
3771         case R_PPC_TLS:
3772         case R_PPC_EMB_MRKREF:
3773         case R_PPC_NONE:
3774         case R_PPC_max:
3775           break;
3776
3777           /* These should only appear in dynamic objects.  */
3778         case R_PPC_COPY:
3779         case R_PPC_GLOB_DAT:
3780         case R_PPC_JMP_SLOT:
3781         case R_PPC_RELATIVE:
3782           break;
3783
3784           /* These aren't handled yet.  We'll report an error later.  */
3785         case R_PPC_ADDR30:
3786         case R_PPC_EMB_RELSEC16:
3787         case R_PPC_EMB_RELST_LO:
3788         case R_PPC_EMB_RELST_HI:
3789         case R_PPC_EMB_RELST_HA:
3790         case R_PPC_EMB_BIT_FLD:
3791           break;
3792
3793           /* This refers only to functions defined in the shared library.  */
3794         case R_PPC_LOCAL24PC:
3795           break;
3796
3797           /* This relocation describes the C++ object vtable hierarchy.
3798              Reconstruct it for later use during GC.  */
3799         case R_PPC_GNU_VTINHERIT:
3800           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3801             return FALSE;
3802           break;
3803
3804           /* This relocation describes which C++ vtable entries are actually
3805              used.  Record for later use during GC.  */
3806         case R_PPC_GNU_VTENTRY:
3807           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3808             return FALSE;
3809           break;
3810
3811           /* We shouldn't really be seeing these.  */
3812         case R_PPC_TPREL32:
3813           if (info->shared)
3814             info->flags |= DF_STATIC_TLS;
3815           goto dodyn;
3816
3817           /* Nor these.  */
3818         case R_PPC_DTPMOD32:
3819         case R_PPC_DTPREL32:
3820           goto dodyn;
3821
3822         case R_PPC_TPREL16:
3823         case R_PPC_TPREL16_LO:
3824         case R_PPC_TPREL16_HI:
3825         case R_PPC_TPREL16_HA:
3826           if (info->shared)
3827             info->flags |= DF_STATIC_TLS;
3828           goto dodyn;
3829
3830           /* When creating a shared object, we must copy these
3831              relocs into the output file.  We create a reloc
3832              section in dynobj and make room for the reloc.  */
3833         case R_PPC_REL24:
3834         case R_PPC_REL14:
3835         case R_PPC_REL14_BRTAKEN:
3836         case R_PPC_REL14_BRNTAKEN:
3837         case R_PPC_REL32:
3838           if (h == NULL
3839               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3840             break;
3841           /* fall through */
3842
3843         case R_PPC_ADDR32:
3844         case R_PPC_ADDR24:
3845         case R_PPC_ADDR16:
3846         case R_PPC_ADDR16_LO:
3847         case R_PPC_ADDR16_HI:
3848         case R_PPC_ADDR16_HA:
3849         case R_PPC_ADDR14:
3850         case R_PPC_ADDR14_BRTAKEN:
3851         case R_PPC_ADDR14_BRNTAKEN:
3852         case R_PPC_UADDR32:
3853         case R_PPC_UADDR16:
3854           if (h != NULL && !info->shared)
3855             {
3856               /* We may need a plt entry if the symbol turns out to be
3857                  a function defined in a dynamic object.  */
3858               h->plt.refcount++;
3859
3860               /* We may need a copy reloc too.  */
3861               h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3862             }
3863
3864         dodyn:
3865           /* If we are creating a shared library, and this is a reloc
3866              against a global symbol, or a non PC relative reloc
3867              against a local symbol, then we need to copy the reloc
3868              into the shared library.  However, if we are linking with
3869              -Bsymbolic, we do not need to copy a reloc against a
3870              global symbol which is defined in an object we are
3871              including in the link (i.e., DEF_REGULAR is set).  At
3872              this point we have not seen all the input files, so it is
3873              possible that DEF_REGULAR is not set now but will be set
3874              later (it is never cleared).  In case of a weak definition,
3875              DEF_REGULAR may be cleared later by a strong definition in
3876              a shared library.  We account for that possibility below by
3877              storing information in the dyn_relocs field of the hash
3878              table entry.  A similar situation occurs when creating
3879              shared libraries and symbol visibility changes render the
3880              symbol local.
3881
3882              If on the other hand, we are creating an executable, we
3883              may need to keep relocations for symbols satisfied by a
3884              dynamic library if we manage to avoid copy relocs for the
3885              symbol.  */
3886           if ((info->shared
3887                && (MUST_BE_DYN_RELOC (r_type)
3888                    || (h != NULL
3889                        && (! info->symbolic
3890                            || h->root.type == bfd_link_hash_defweak
3891                            || (h->elf_link_hash_flags
3892                                & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3893               || (ELIMINATE_COPY_RELOCS
3894                   && !info->shared
3895                   && (sec->flags & SEC_ALLOC) != 0
3896                   && h != NULL
3897                   && (h->root.type == bfd_link_hash_defweak
3898                       || (h->elf_link_hash_flags
3899                           & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3900             {
3901               struct ppc_elf_dyn_relocs *p;
3902               struct ppc_elf_dyn_relocs **head;
3903
3904 #ifdef DEBUG
3905               fprintf (stderr,
3906                        "ppc_elf_check_relocs needs to "
3907                        "create relocation for %s\n",
3908                        (h && h->root.root.string
3909                         ? h->root.root.string : "<unknown>"));
3910 #endif
3911               if (sreloc == NULL)
3912                 {
3913                   const char *name;
3914
3915                   name = (bfd_elf_string_from_elf_section
3916                           (abfd,
3917                            elf_elfheader (abfd)->e_shstrndx,
3918                            elf_section_data (sec)->rel_hdr.sh_name));
3919                   if (name == NULL)
3920                     return FALSE;
3921
3922                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3923                               && strcmp (bfd_get_section_name (abfd, sec),
3924                                          name + 5) == 0);
3925
3926                   sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3927                   if (sreloc == NULL)
3928                     {
3929                       flagword flags;
3930
3931                       sreloc = bfd_make_section (htab->elf.dynobj, name);
3932                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
3933                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3934                       if ((sec->flags & SEC_ALLOC) != 0)
3935                         flags |= SEC_ALLOC | SEC_LOAD;
3936                       if (sreloc == NULL
3937                           || ! bfd_set_section_flags (htab->elf.dynobj,
3938                                                       sreloc, flags)
3939                           || ! bfd_set_section_alignment (htab->elf.dynobj,
3940                                                           sreloc, 2))
3941                         return FALSE;
3942                     }
3943                   elf_section_data (sec)->sreloc = sreloc;
3944                 }
3945
3946               /* If this is a global symbol, we count the number of
3947                  relocations we need for this symbol.  */
3948               if (h != NULL)
3949                 {
3950                   head = &ppc_elf_hash_entry (h)->dyn_relocs;
3951                 }
3952               else
3953                 {
3954                   /* Track dynamic relocs needed for local syms too.
3955                      We really need local syms available to do this
3956                      easily.  Oh well.  */
3957
3958                   asection *s;
3959                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3960                                                  sec, r_symndx);
3961                   if (s == NULL)
3962                     return FALSE;
3963
3964                   head = ((struct ppc_elf_dyn_relocs **)
3965                           &elf_section_data (s)->local_dynrel);
3966                 }
3967
3968               p = *head;
3969               if (p == NULL || p->sec != sec)
3970                 {
3971                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3972                   if (p == NULL)
3973                     return FALSE;
3974                   p->next = *head;
3975                   *head = p;
3976                   p->sec = sec;
3977                   p->count = 0;
3978                   p->pc_count = 0;
3979                 }
3980
3981               p->count += 1;
3982               if (!MUST_BE_DYN_RELOC (r_type))
3983                 p->pc_count += 1;
3984             }
3985
3986           break;
3987         }
3988     }
3989
3990   return TRUE;
3991 }
3992
3993 /* Return the section that should be marked against GC for a given
3994    relocation.  */
3995
3996 static asection *
3997 ppc_elf_gc_mark_hook (asection *sec,
3998                       struct bfd_link_info *info ATTRIBUTE_UNUSED,
3999                       Elf_Internal_Rela *rel,
4000                       struct elf_link_hash_entry *h,
4001                       Elf_Internal_Sym *sym)
4002 {
4003   if (h != NULL)
4004     {
4005       switch (ELF32_R_TYPE (rel->r_info))
4006         {
4007         case R_PPC_GNU_VTINHERIT:
4008         case R_PPC_GNU_VTENTRY:
4009           break;
4010
4011         default:
4012           switch (h->root.type)
4013             {
4014             case bfd_link_hash_defined:
4015             case bfd_link_hash_defweak:
4016               return h->root.u.def.section;
4017
4018             case bfd_link_hash_common:
4019               return h->root.u.c.p->section;
4020
4021             default:
4022               break;
4023             }
4024         }
4025     }
4026   else
4027     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4028
4029   return NULL;
4030 }
4031
4032 /* Update the got, plt and dynamic reloc reference counts for the
4033    section being removed.  */
4034
4035 static bfd_boolean
4036 ppc_elf_gc_sweep_hook (bfd *abfd,
4037                        struct bfd_link_info *info,
4038                        asection *sec,
4039                        const Elf_Internal_Rela *relocs)
4040 {
4041   struct ppc_elf_link_hash_table *htab;
4042   Elf_Internal_Shdr *symtab_hdr;
4043   struct elf_link_hash_entry **sym_hashes;
4044   bfd_signed_vma *local_got_refcounts;
4045   const Elf_Internal_Rela *rel, *relend;
4046
4047   elf_section_data (sec)->local_dynrel = NULL;
4048
4049   htab = ppc_elf_hash_table (info);
4050   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4051   sym_hashes = elf_sym_hashes (abfd);
4052   local_got_refcounts = elf_local_got_refcounts (abfd);
4053
4054   relend = relocs + sec->reloc_count;
4055   for (rel = relocs; rel < relend; rel++)
4056     {
4057       unsigned long r_symndx;
4058       enum elf_ppc_reloc_type r_type;
4059       struct elf_link_hash_entry *h = NULL;
4060
4061       r_symndx = ELF32_R_SYM (rel->r_info);
4062       if (r_symndx >= symtab_hdr->sh_info)
4063         {
4064           struct ppc_elf_dyn_relocs **pp, *p;
4065           struct ppc_elf_link_hash_entry *eh;
4066
4067           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4068           eh = (struct ppc_elf_link_hash_entry *) h;
4069
4070           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4071             if (p->sec == sec)
4072               {
4073                 /* Everything must go for SEC.  */
4074                 *pp = p->next;
4075                 break;
4076               }
4077         }
4078
4079       r_type = ELF32_R_TYPE (rel->r_info);
4080       switch (r_type)
4081         {
4082         case R_PPC_GOT_TLSLD16:
4083         case R_PPC_GOT_TLSLD16_LO:
4084         case R_PPC_GOT_TLSLD16_HI:
4085         case R_PPC_GOT_TLSLD16_HA:
4086           htab->tlsld_got.refcount -= 1;
4087           /* Fall thru */
4088
4089         case R_PPC_GOT_TLSGD16:
4090         case R_PPC_GOT_TLSGD16_LO:
4091         case R_PPC_GOT_TLSGD16_HI:
4092         case R_PPC_GOT_TLSGD16_HA:
4093         case R_PPC_GOT_TPREL16:
4094         case R_PPC_GOT_TPREL16_LO:
4095         case R_PPC_GOT_TPREL16_HI:
4096         case R_PPC_GOT_TPREL16_HA:
4097         case R_PPC_GOT_DTPREL16:
4098         case R_PPC_GOT_DTPREL16_LO:
4099         case R_PPC_GOT_DTPREL16_HI:
4100         case R_PPC_GOT_DTPREL16_HA:
4101         case R_PPC_GOT16:
4102         case R_PPC_GOT16_LO:
4103         case R_PPC_GOT16_HI:
4104         case R_PPC_GOT16_HA:
4105           if (h != NULL)
4106             {
4107               if (h->got.refcount > 0)
4108                 h->got.refcount--;
4109             }
4110           else if (local_got_refcounts != NULL)
4111             {
4112               if (local_got_refcounts[r_symndx] > 0)
4113                 local_got_refcounts[r_symndx]--;
4114             }
4115           break;
4116
4117         case R_PPC_REL24:
4118         case R_PPC_REL14:
4119         case R_PPC_REL14_BRTAKEN:
4120         case R_PPC_REL14_BRNTAKEN:
4121         case R_PPC_REL32:
4122           if (h == NULL
4123               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4124             break;
4125           /* Fall thru */
4126
4127         case R_PPC_ADDR32:
4128         case R_PPC_ADDR24:
4129         case R_PPC_ADDR16:
4130         case R_PPC_ADDR16_LO:
4131         case R_PPC_ADDR16_HI:
4132         case R_PPC_ADDR16_HA:
4133         case R_PPC_ADDR14:
4134         case R_PPC_ADDR14_BRTAKEN:
4135         case R_PPC_ADDR14_BRNTAKEN:
4136         case R_PPC_UADDR32:
4137         case R_PPC_UADDR16:
4138         case R_PPC_PLT32:
4139         case R_PPC_PLTREL24:
4140         case R_PPC_PLT16_LO:
4141         case R_PPC_PLT16_HI:
4142         case R_PPC_PLT16_HA:
4143           if (h != NULL)
4144             {
4145               if (h->plt.refcount > 0)
4146                 h->plt.refcount--;
4147             }
4148           break;
4149
4150         default:
4151           break;
4152         }
4153     }
4154   return TRUE;
4155 }
4156
4157 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
4158
4159 asection *
4160 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
4161 {
4162   struct ppc_elf_link_hash_table *htab;
4163
4164   htab = ppc_elf_hash_table (info);
4165   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4166                                              FALSE, FALSE, TRUE);
4167
4168   return _bfd_elf_tls_setup (obfd, info);
4169 }
4170
4171 /* Run through all the TLS relocs looking for optimization
4172    opportunities.  */
4173
4174 bfd_boolean
4175 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4176                       struct bfd_link_info *info)
4177 {
4178   bfd *ibfd;
4179   asection *sec;
4180   struct ppc_elf_link_hash_table *htab;
4181
4182   if (info->relocatable || info->shared)
4183     return TRUE;
4184
4185   htab = ppc_elf_hash_table (info);
4186   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4187     {
4188       Elf_Internal_Sym *locsyms = NULL;
4189       Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4190
4191       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4192         if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4193           {
4194             Elf_Internal_Rela *relstart, *rel, *relend;
4195             int expecting_tls_get_addr;
4196
4197             /* Read the relocations.  */
4198             relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4199                                                   info->keep_memory);
4200             if (relstart == NULL)
4201               return FALSE;
4202
4203             expecting_tls_get_addr = 0;
4204             relend = relstart + sec->reloc_count;
4205             for (rel = relstart; rel < relend; rel++)
4206               {
4207                 enum elf_ppc_reloc_type r_type;
4208                 unsigned long r_symndx;
4209                 struct elf_link_hash_entry *h = NULL;
4210                 char *tls_mask;
4211                 char tls_set, tls_clear;
4212                 bfd_boolean is_local;
4213
4214                 r_symndx = ELF32_R_SYM (rel->r_info);
4215                 if (r_symndx >= symtab_hdr->sh_info)
4216                   {
4217                     struct elf_link_hash_entry **sym_hashes;
4218
4219                     sym_hashes = elf_sym_hashes (ibfd);
4220                     h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4221                     while (h->root.type == bfd_link_hash_indirect
4222                            || h->root.type == bfd_link_hash_warning)
4223                       h = (struct elf_link_hash_entry *) h->root.u.i.link;
4224                   }
4225
4226                 is_local = FALSE;
4227                 if (h == NULL
4228                     || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
4229                   is_local = TRUE;
4230
4231                 r_type = ELF32_R_TYPE (rel->r_info);
4232                 switch (r_type)
4233                   {
4234                   case R_PPC_GOT_TLSLD16:
4235                   case R_PPC_GOT_TLSLD16_LO:
4236                   case R_PPC_GOT_TLSLD16_HI:
4237                   case R_PPC_GOT_TLSLD16_HA:
4238                     /* These relocs should never be against a symbol
4239                        defined in a shared lib.  Leave them alone if
4240                        that turns out to be the case.  */
4241                     expecting_tls_get_addr = 0;
4242                     htab->tlsld_got.refcount -= 1;
4243                     if (!is_local)
4244                       continue;
4245
4246                     /* LD -> LE */
4247                     tls_set = 0;
4248                     tls_clear = TLS_LD;
4249                     expecting_tls_get_addr = 1;
4250                     break;
4251
4252                   case R_PPC_GOT_TLSGD16:
4253                   case R_PPC_GOT_TLSGD16_LO:
4254                   case R_PPC_GOT_TLSGD16_HI:
4255                   case R_PPC_GOT_TLSGD16_HA:
4256                     if (is_local)
4257                       /* GD -> LE */
4258                       tls_set = 0;
4259                     else
4260                       /* GD -> IE */
4261                       tls_set = TLS_TLS | TLS_TPRELGD;
4262                     tls_clear = TLS_GD;
4263                     expecting_tls_get_addr = 1;
4264                     break;
4265
4266                   case R_PPC_GOT_TPREL16:
4267                   case R_PPC_GOT_TPREL16_LO:
4268                   case R_PPC_GOT_TPREL16_HI:
4269                   case R_PPC_GOT_TPREL16_HA:
4270                     expecting_tls_get_addr = 0;
4271                     if (is_local)
4272                       {
4273                         /* IE -> LE */
4274                         tls_set = 0;
4275                         tls_clear = TLS_TPREL;
4276                         break;
4277                       }
4278                     else
4279                       continue;
4280
4281                   case R_PPC_REL14:
4282                   case R_PPC_REL14_BRTAKEN:
4283                   case R_PPC_REL14_BRNTAKEN:
4284                   case R_PPC_REL24:
4285                     if (expecting_tls_get_addr
4286                         && h != NULL
4287                         && h == htab->tls_get_addr)
4288                       {
4289                         if (h->plt.refcount > 0)
4290                           h->plt.refcount -= 1;
4291                       }
4292                     expecting_tls_get_addr = 0;
4293                     continue;
4294
4295                   default:
4296                     expecting_tls_get_addr = 0;
4297                     continue;
4298                   }
4299
4300                 if (h != NULL)
4301                   {
4302                     if (tls_set == 0)
4303                       {
4304                         /* We managed to get rid of a got entry.  */
4305                         if (h->got.refcount > 0)
4306                           h->got.refcount -= 1;
4307                       }
4308                     tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4309                   }
4310                 else
4311                   {
4312                     Elf_Internal_Sym *sym;
4313                     bfd_signed_vma *lgot_refs;
4314                     char *lgot_masks;
4315
4316                     if (locsyms == NULL)
4317                       {
4318                         locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4319                         if (locsyms == NULL)
4320                           locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4321                                                           symtab_hdr->sh_info,
4322                                                           0, NULL, NULL, NULL);
4323                         if (locsyms == NULL)
4324                           {
4325                             if (elf_section_data (sec)->relocs != relstart)
4326                               free (relstart);
4327                             return FALSE;
4328                           }
4329                       }
4330                     sym = locsyms + r_symndx;
4331                     lgot_refs = elf_local_got_refcounts (ibfd);
4332                     if (lgot_refs == NULL)
4333                       abort ();
4334                     if (tls_set == 0)
4335                       {
4336                         /* We managed to get rid of a got entry.  */
4337                         if (lgot_refs[r_symndx] > 0)
4338                           lgot_refs[r_symndx] -= 1;
4339                       }
4340                     lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
4341                     tls_mask = &lgot_masks[r_symndx];
4342                   }
4343
4344                 *tls_mask |= tls_set;
4345                 *tls_mask &= ~tls_clear;
4346               }
4347
4348             if (elf_section_data (sec)->relocs != relstart)
4349               free (relstart);
4350           }
4351
4352       if (locsyms != NULL
4353           && (symtab_hdr->contents != (unsigned char *) locsyms))
4354         {
4355           if (!info->keep_memory)
4356             free (locsyms);
4357           else
4358             symtab_hdr->contents = (unsigned char *) locsyms;
4359         }
4360     }
4361   return TRUE;
4362 }
4363 \f
4364 /* Hook called by the linker routine which adds symbols from an object
4365    file.  We use it to put .comm items in .sbss, and not .bss.  */
4366
4367 static bfd_boolean
4368 ppc_elf_add_symbol_hook (bfd *abfd,
4369                          struct bfd_link_info *info,
4370                          Elf_Internal_Sym *sym,
4371                          const char **namep ATTRIBUTE_UNUSED,
4372                          flagword *flagsp ATTRIBUTE_UNUSED,
4373                          asection **secp,
4374                          bfd_vma *valp)
4375 {
4376   if (sym->st_shndx == SHN_COMMON
4377       && !info->relocatable
4378       && sym->st_size <= elf_gp_size (abfd)
4379       && (info->hash->creator == abfd->xvec
4380           || info->hash->creator == abfd->xvec->alternative_target))
4381     {
4382       /* Common symbols less than or equal to -G nn bytes are automatically
4383          put into .sbss.  */
4384       struct ppc_elf_link_hash_table *htab;
4385
4386       htab = ppc_elf_hash_table (info);
4387       if (htab->sbss == NULL)
4388         {
4389           flagword flags = SEC_IS_COMMON;
4390
4391           htab->sbss = bfd_make_section_anyway (abfd, ".sbss");
4392           if (htab->sbss == NULL
4393               || ! bfd_set_section_flags (abfd, htab->sbss, flags))
4394             return FALSE;
4395         }
4396
4397       *secp = htab->sbss;
4398       *valp = sym->st_size;
4399     }
4400
4401   return TRUE;
4402 }
4403 \f
4404 /* Finish up dynamic symbol handling.  We set the contents of various
4405    dynamic sections here.  */
4406
4407 static bfd_boolean
4408 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
4409                                struct bfd_link_info *info,
4410                                struct elf_link_hash_entry *h,
4411                                Elf_Internal_Sym *sym)
4412 {
4413   struct ppc_elf_link_hash_table *htab;
4414
4415 #ifdef DEBUG
4416   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
4417            h->root.root.string);
4418 #endif
4419
4420   htab = ppc_elf_hash_table (info);
4421   BFD_ASSERT (htab->elf.dynobj != NULL);
4422
4423   if (h->plt.offset != (bfd_vma) -1)
4424     {
4425       Elf_Internal_Rela rela;
4426       bfd_byte *loc;
4427       bfd_vma reloc_index;
4428
4429 #ifdef DEBUG
4430       fprintf (stderr, ", plt_offset = %d", h->plt.offset);
4431 #endif
4432
4433       /* This symbol has an entry in the procedure linkage table.  Set
4434          it up.  */
4435
4436       BFD_ASSERT (h->dynindx != -1);
4437       BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
4438
4439       /* We don't need to fill in the .plt.  The ppc dynamic linker
4440          will fill it in.  */
4441
4442       /* Fill in the entry in the .rela.plt section.  */
4443       rela.r_offset = (htab->plt->output_section->vma
4444                        + htab->plt->output_offset
4445                        + h->plt.offset);
4446       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
4447       rela.r_addend = 0;
4448
4449       reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
4450       if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
4451         reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
4452       loc = (htab->relplt->contents
4453              + reloc_index * sizeof (Elf32_External_Rela));
4454       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4455
4456       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4457         {
4458           /* Mark the symbol as undefined, rather than as defined in
4459              the .plt section.  Leave the value alone.  */
4460           sym->st_shndx = SHN_UNDEF;
4461           /* If the symbol is weak, we do need to clear the value.
4462              Otherwise, the PLT entry would provide a definition for
4463              the symbol even if the symbol wasn't defined anywhere,
4464              and so the symbol would never be NULL.  */
4465           if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
4466               == 0)
4467             sym->st_value = 0;
4468         }
4469     }
4470
4471   if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4472     {
4473       asection *s;
4474       Elf_Internal_Rela rela;
4475       bfd_byte *loc;
4476
4477       /* This symbols needs a copy reloc.  Set it up.  */
4478
4479 #ifdef DEBUG
4480       fprintf (stderr, ", copy");
4481 #endif
4482
4483       BFD_ASSERT (h->dynindx != -1);
4484
4485       if (h->size <= elf_gp_size (htab->elf.dynobj))
4486         s = htab->relsbss;
4487       else
4488         s = htab->relbss;
4489       BFD_ASSERT (s != NULL);
4490
4491       rela.r_offset = (h->root.u.def.value
4492                        + h->root.u.def.section->output_section->vma
4493                        + h->root.u.def.section->output_offset);
4494       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
4495       rela.r_addend = 0;
4496       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4497       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4498     }
4499
4500 #ifdef DEBUG
4501   fprintf (stderr, "\n");
4502 #endif
4503
4504   /* Mark some specially defined symbols as absolute.  */
4505   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4506       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4507       || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4508     sym->st_shndx = SHN_ABS;
4509
4510   return TRUE;
4511 }
4512 \f
4513 /* Finish up the dynamic sections.  */
4514
4515 static bfd_boolean
4516 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
4517                                  struct bfd_link_info *info)
4518 {
4519   asection *sdyn;
4520   struct ppc_elf_link_hash_table *htab;
4521
4522 #ifdef DEBUG
4523   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
4524 #endif
4525
4526   htab = ppc_elf_hash_table (info);
4527   sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
4528
4529   if (htab->elf.dynamic_sections_created)
4530     {
4531       Elf32_External_Dyn *dyncon, *dynconend;
4532
4533       BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
4534
4535       dyncon = (Elf32_External_Dyn *) sdyn->contents;
4536       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4537       for (; dyncon < dynconend; dyncon++)
4538         {
4539           Elf_Internal_Dyn dyn;
4540           asection *s;
4541
4542           bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
4543
4544           switch (dyn.d_tag)
4545             {
4546             case DT_PLTGOT:
4547               s = htab->plt;
4548               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4549               break;
4550
4551             case DT_PLTRELSZ:
4552               dyn.d_un.d_val = htab->relplt->_raw_size;
4553               break;
4554
4555             case DT_JMPREL:
4556               s = htab->relplt;
4557               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4558               break;
4559
4560             default:
4561               continue;
4562             }
4563
4564           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4565         }
4566     }
4567
4568   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
4569      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
4570   if (htab->got)
4571     {
4572       unsigned char *contents = htab->got->contents;
4573       bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
4574
4575       if (sdyn == NULL)
4576         bfd_put_32 (output_bfd, 0, contents + 4);
4577       else
4578         bfd_put_32 (output_bfd,
4579                     sdyn->output_section->vma + sdyn->output_offset,
4580                     contents + 4);
4581
4582       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
4583     }
4584
4585   return TRUE;
4586 }
4587 \f
4588 /* The RELOCATE_SECTION function is called by the ELF backend linker
4589    to handle the relocations for a section.
4590
4591    The relocs are always passed as Rela structures; if the section
4592    actually uses Rel structures, the r_addend field will always be
4593    zero.
4594
4595    This function is responsible for adjust the section contents as
4596    necessary, and (if using Rela relocs and generating a
4597    relocatable output file) adjusting the reloc addend as
4598    necessary.
4599
4600    This function does not have to worry about setting the reloc
4601    address or the reloc symbol index.
4602
4603    LOCAL_SYMS is a pointer to the swapped in local symbols.
4604
4605    LOCAL_SECTIONS is an array giving the section in the input file
4606    corresponding to the st_shndx field of each local symbol.
4607
4608    The global hash table entry for the global symbols can be found
4609    via elf_sym_hashes (input_bfd).
4610
4611    When generating relocatable output, this function must handle
4612    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4613    going to be the section symbol corresponding to the output
4614    section, which means that the addend must be adjusted
4615    accordingly.  */
4616
4617 static bfd_boolean
4618 ppc_elf_relocate_section (bfd *output_bfd,
4619                           struct bfd_link_info *info,
4620                           bfd *input_bfd,
4621                           asection *input_section,
4622                           bfd_byte *contents,
4623                           Elf_Internal_Rela *relocs,
4624                           Elf_Internal_Sym *local_syms,
4625                           asection **local_sections)
4626 {
4627   Elf_Internal_Shdr *symtab_hdr;
4628   struct elf_link_hash_entry **sym_hashes;
4629   struct ppc_elf_link_hash_table *htab;
4630   Elf_Internal_Rela *rel;
4631   Elf_Internal_Rela *relend;
4632   Elf_Internal_Rela outrel;
4633   bfd_byte *loc;
4634   asection *sreloc = NULL;
4635   bfd_vma *local_got_offsets;
4636   bfd_boolean ret = TRUE;
4637
4638 #ifdef DEBUG
4639   fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, "
4640            "%ld relocations%s\n",
4641            bfd_archive_filename (input_bfd),
4642            bfd_section_name(input_bfd, input_section),
4643            (long) input_section->reloc_count,
4644            (info->relocatable) ? " (relocatable)" : "");
4645 #endif
4646
4647   if (info->relocatable)
4648     return TRUE;
4649
4650   /* Initialize howto table if not already done.  */
4651   if (!ppc_elf_howto_table[R_PPC_ADDR32])
4652     ppc_elf_howto_init ();
4653
4654   htab = ppc_elf_hash_table (info);
4655   local_got_offsets = elf_local_got_offsets (input_bfd);
4656   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4657   sym_hashes = elf_sym_hashes (input_bfd);
4658   rel = relocs;
4659   relend = relocs + input_section->reloc_count;
4660   for (; rel < relend; rel++)
4661     {
4662       enum elf_ppc_reloc_type r_type;
4663       bfd_vma addend;
4664       bfd_reloc_status_type r;
4665       Elf_Internal_Sym *sym;
4666       asection *sec;
4667       struct elf_link_hash_entry *h;
4668       const char *sym_name;
4669       reloc_howto_type *howto;
4670       unsigned long r_symndx;
4671       bfd_vma relocation;
4672       bfd_vma branch_bit, insn, from;
4673       bfd_boolean unresolved_reloc;
4674       bfd_boolean warned;
4675       unsigned int tls_type, tls_mask, tls_gd;
4676
4677       r_type = ELF32_R_TYPE (rel->r_info);
4678       sym = NULL;
4679       sec = NULL;
4680       h = NULL;
4681       unresolved_reloc = FALSE;
4682       warned = FALSE;
4683       r_symndx = ELF32_R_SYM (rel->r_info);
4684
4685       if (r_symndx < symtab_hdr->sh_info)
4686         {
4687           sym = local_syms + r_symndx;
4688           sec = local_sections[r_symndx];
4689           sym_name = bfd_elf_local_sym_name (input_bfd, sym);
4690
4691           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4692         }
4693       else
4694         {
4695           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4696                                    r_symndx, symtab_hdr, sym_hashes,
4697                                    h, sec, relocation,
4698                                    unresolved_reloc, warned);
4699
4700           sym_name = h->root.root.string;
4701         }
4702
4703       /* TLS optimizations.  Replace instruction sequences and relocs
4704          based on information we collected in tls_optimize.  We edit
4705          RELOCS so that --emit-relocs will output something sensible
4706          for the final instruction stream.  */
4707       tls_mask = 0;
4708       tls_gd = 0;
4709       if (IS_PPC_TLS_RELOC (r_type))
4710         {
4711           if (h != NULL)
4712             tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4713           else if (local_got_offsets != NULL)
4714             {
4715               char *lgot_masks;
4716               lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4717               tls_mask = lgot_masks[r_symndx];
4718             }
4719         }
4720
4721       /* Ensure reloc mapping code below stays sane.  */
4722       if ((R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TLSGD16 & 3)
4723           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4724           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4725           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4726           || (R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TPREL16 & 3)
4727           || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4728           || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4729           || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4730         abort ();
4731       switch (r_type)
4732         {
4733         default:
4734           break;
4735
4736         case R_PPC_GOT_TPREL16:
4737         case R_PPC_GOT_TPREL16_LO:
4738           if (tls_mask != 0
4739               && (tls_mask & TLS_TPREL) == 0)
4740             {
4741               bfd_vma insn;
4742               insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4743               insn &= 31 << 21;
4744               insn |= 0x3c020000;       /* addis 0,2,0 */
4745               bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4746               r_type = R_PPC_TPREL16_HA;
4747               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4748             }
4749           break;
4750
4751         case R_PPC_TLS:
4752           if (tls_mask != 0
4753               && (tls_mask & TLS_TPREL) == 0)
4754             {
4755               bfd_vma insn, rtra;
4756               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4757               if ((insn & ((31 << 26) | (31 << 11)))
4758                   == ((31 << 26) | (2 << 11)))
4759                 rtra = insn & ((1 << 26) - (1 << 16));
4760               else if ((insn & ((31 << 26) | (31 << 16)))
4761                        == ((31 << 26) | (2 << 16)))
4762                 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4763               else
4764                 abort ();
4765               if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4766                 /* add -> addi.  */
4767                 insn = 14 << 26;
4768               else if ((insn & (31 << 1)) == 23 << 1
4769                        && ((insn & (31 << 6)) < 14 << 6
4770                            || ((insn & (31 << 6)) >= 16 << 6
4771                                && (insn & (31 << 6)) < 24 << 6)))
4772                 /* load and store indexed -> dform.  */
4773                 insn = (32 | ((insn >> 6) & 31)) << 26;
4774               else if ((insn & (31 << 1)) == 21 << 1
4775                        && (insn & (0x1a << 6)) == 0)
4776                 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
4777                 insn = (((58 | ((insn >> 6) & 4)) << 26)
4778                         | ((insn >> 6) & 1));
4779               else if ((insn & (31 << 1)) == 21 << 1
4780                        && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4781                 /* lwax -> lwa.  */
4782                 insn = (58 << 26) | 2;
4783               else
4784                 abort ();
4785               insn |= rtra;
4786               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4787               r_type = R_PPC_TPREL16_LO;
4788               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4789               /* Was PPC_TLS which sits on insn boundary, now
4790                  PPC_TPREL16_LO which is at insn+2.  */
4791               rel->r_offset += 2;
4792             }
4793           break;
4794
4795         case R_PPC_GOT_TLSGD16_HI:
4796         case R_PPC_GOT_TLSGD16_HA:
4797           tls_gd = TLS_TPRELGD;
4798           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4799             goto tls_gdld_hi;
4800           break;
4801
4802         case R_PPC_GOT_TLSLD16_HI:
4803         case R_PPC_GOT_TLSLD16_HA:
4804           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4805             {
4806             tls_gdld_hi:
4807               if ((tls_mask & tls_gd) != 0)
4808                 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4809                           + R_PPC_GOT_TPREL16);
4810               else
4811                 {
4812                   bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4813                   rel->r_offset -= 2;
4814                   r_type = R_PPC_NONE;
4815                 }
4816               rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4817             }
4818           break;
4819
4820         case R_PPC_GOT_TLSGD16:
4821         case R_PPC_GOT_TLSGD16_LO:
4822           tls_gd = TLS_TPRELGD;
4823           if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4824             goto tls_get_addr_check;
4825           break;
4826
4827         case R_PPC_GOT_TLSLD16:
4828         case R_PPC_GOT_TLSLD16_LO:
4829           if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4830             {
4831             tls_get_addr_check:
4832               if (rel + 1 < relend)
4833                 {
4834                   enum elf_ppc_reloc_type r_type2;
4835                   unsigned long r_symndx2;
4836                   struct elf_link_hash_entry *h2;
4837                   bfd_vma insn1, insn2;
4838                   bfd_vma offset;
4839
4840                   /* The next instruction should be a call to
4841                      __tls_get_addr.  Peek at the reloc to be sure.  */
4842                   r_type2 = ELF32_R_TYPE (rel[1].r_info);
4843                   r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4844                   if (r_symndx2 < symtab_hdr->sh_info
4845                       || (r_type2 != R_PPC_REL14
4846                           && r_type2 != R_PPC_REL14_BRTAKEN
4847                           && r_type2 != R_PPC_REL14_BRNTAKEN
4848                           && r_type2 != R_PPC_REL24
4849                           && r_type2 != R_PPC_PLTREL24))
4850                     break;
4851
4852                   h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4853                   while (h2->root.type == bfd_link_hash_indirect
4854                          || h2->root.type == bfd_link_hash_warning)
4855                     h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4856                   if (h2 == NULL || h2 != htab->tls_get_addr)
4857                     break;
4858
4859                   /* OK, it checks out.  Replace the call.  */
4860                   offset = rel[1].r_offset;
4861                   insn1 = bfd_get_32 (output_bfd,
4862                                       contents + rel->r_offset - 2);
4863                   if ((tls_mask & tls_gd) != 0)
4864                     {
4865                       /* IE */
4866                       insn1 &= (1 << 26) - 1;
4867                       insn1 |= 32 << 26;        /* lwz */
4868                       insn2 = 0x7c631214;       /* add 3,3,2 */
4869                       rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4870                       r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4871                                 + R_PPC_GOT_TPREL16);
4872                       rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4873                     }
4874                   else
4875                     {
4876                       /* LE */
4877                       insn1 = 0x3c620000;       /* addis 3,2,0 */
4878                       insn2 = 0x38630000;       /* addi 3,3,0 */
4879                       if (tls_gd == 0)
4880                         {
4881                           /* Was an LD reloc.  */
4882                           r_symndx = 0;
4883                           rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4884                           rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4885                         }
4886                       r_type = R_PPC_TPREL16_HA;
4887                       rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4888                       rel[1].r_info = ELF32_R_INFO (r_symndx,
4889                                                     R_PPC_TPREL16_LO);
4890                       rel[1].r_offset += 2;
4891                     }
4892                   bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4893                   bfd_put_32 (output_bfd, insn2, contents + offset);
4894                   if (tls_gd == 0)
4895                     {
4896                       /* We changed the symbol on an LD reloc.  Start over
4897                          in order to get h, sym, sec etc. right.  */
4898                       rel--;
4899                       continue;
4900                     }
4901                 }
4902             }
4903           break;
4904         }
4905
4906       /* Handle other relocations that tweak non-addend part of insn.  */
4907       branch_bit = 0;
4908       switch (r_type)
4909         {
4910         default:
4911           break;
4912
4913           /* Branch taken prediction relocations.  */
4914         case R_PPC_ADDR14_BRTAKEN:
4915         case R_PPC_REL14_BRTAKEN:
4916           branch_bit = BRANCH_PREDICT_BIT;
4917           /* Fall thru */
4918
4919           /* Branch not taken prediction relocations.  */
4920         case R_PPC_ADDR14_BRNTAKEN:
4921         case R_PPC_REL14_BRNTAKEN:
4922           insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4923           insn &= ~BRANCH_PREDICT_BIT;
4924           insn |= branch_bit;
4925
4926           from = (rel->r_offset
4927                   + input_section->output_offset
4928                   + input_section->output_section->vma);
4929
4930           /* Invert 'y' bit if not the default.  */
4931           if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
4932             insn ^= BRANCH_PREDICT_BIT;
4933
4934           bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4935           break;
4936         }
4937
4938       addend = rel->r_addend;
4939       tls_type = 0;
4940       howto = NULL;
4941       if (r_type < R_PPC_max)
4942         howto = ppc_elf_howto_table[r_type];
4943       switch (r_type)
4944         {
4945         default:
4946           (*_bfd_error_handler)
4947             (_("%s: unknown relocation type %d for symbol %s"),
4948              bfd_archive_filename (input_bfd), (int) r_type, sym_name);
4949
4950           bfd_set_error (bfd_error_bad_value);
4951           ret = FALSE;
4952           continue;
4953
4954         case R_PPC_NONE:
4955         case R_PPC_TLS:
4956         case R_PPC_EMB_MRKREF:
4957         case R_PPC_GNU_VTINHERIT:
4958         case R_PPC_GNU_VTENTRY:
4959           continue;
4960
4961           /* GOT16 relocations.  Like an ADDR16 using the symbol's
4962              address in the GOT as relocation value instead of the
4963              symbol's value itself.  Also, create a GOT entry for the
4964              symbol and put the symbol value there.  */
4965         case R_PPC_GOT_TLSGD16:
4966         case R_PPC_GOT_TLSGD16_LO:
4967         case R_PPC_GOT_TLSGD16_HI:
4968         case R_PPC_GOT_TLSGD16_HA:
4969           tls_type = TLS_TLS | TLS_GD;
4970           goto dogot;
4971
4972         case R_PPC_GOT_TLSLD16:
4973         case R_PPC_GOT_TLSLD16_LO:
4974         case R_PPC_GOT_TLSLD16_HI:
4975         case R_PPC_GOT_TLSLD16_HA:
4976           tls_type = TLS_TLS | TLS_LD;
4977           goto dogot;
4978
4979         case R_PPC_GOT_TPREL16:
4980         case R_PPC_GOT_TPREL16_LO:
4981         case R_PPC_GOT_TPREL16_HI:
4982         case R_PPC_GOT_TPREL16_HA:
4983           tls_type = TLS_TLS | TLS_TPREL;
4984           goto dogot;
4985
4986         case R_PPC_GOT_DTPREL16:
4987         case R_PPC_GOT_DTPREL16_LO:
4988         case R_PPC_GOT_DTPREL16_HI:
4989         case R_PPC_GOT_DTPREL16_HA:
4990           tls_type = TLS_TLS | TLS_DTPREL;
4991           goto dogot;
4992
4993         case R_PPC_GOT16:
4994         case R_PPC_GOT16_LO:
4995         case R_PPC_GOT16_HI:
4996         case R_PPC_GOT16_HA:
4997         dogot:
4998           {
4999             /* Relocation is to the entry for this symbol in the global
5000                offset table.  */
5001             bfd_vma off;
5002             bfd_vma *offp;
5003             unsigned long indx;
5004
5005             if (htab->got == NULL)
5006               abort ();
5007
5008             indx = 0;
5009             if (tls_type == (TLS_TLS | TLS_LD)
5010                 && (h == NULL
5011                     || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
5012               offp = &htab->tlsld_got.offset;
5013             else if (h != NULL)
5014               {
5015                 bfd_boolean dyn;
5016                 dyn = htab->elf.dynamic_sections_created;
5017                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5018                     || (info->shared
5019                         && SYMBOL_REFERENCES_LOCAL (info, h)))
5020                   /* This is actually a static link, or it is a
5021                      -Bsymbolic link and the symbol is defined
5022                      locally, or the symbol was forced to be local
5023                      because of a version file.  */
5024                   ;
5025                 else
5026                   {
5027                     indx = h->dynindx;
5028                     unresolved_reloc = FALSE;
5029                   }
5030                 offp = &h->got.offset;
5031               }
5032             else
5033               {
5034                 if (local_got_offsets == NULL)
5035                   abort ();
5036                 offp = &local_got_offsets[r_symndx];
5037               }
5038
5039             /* The offset must always be a multiple of 4.  We use the
5040                least significant bit to record whether we have already
5041                processed this entry.  */
5042             off = *offp;
5043             if ((off & 1) != 0)
5044               off &= ~1;
5045             else
5046               {
5047                 unsigned int tls_m = (tls_mask
5048                                       & (TLS_LD | TLS_GD | TLS_DTPREL
5049                                          | TLS_TPREL | TLS_TPRELGD));
5050
5051                 if (offp == &htab->tlsld_got.offset)
5052                   tls_m = TLS_LD;
5053                 else if (h == NULL
5054                          || !(h->elf_link_hash_flags
5055                               & ELF_LINK_HASH_DEF_DYNAMIC))
5056                   tls_m &= ~TLS_LD;
5057
5058                 /* We might have multiple got entries for this sym.
5059                    Initialize them all.  */
5060                 do
5061                   {
5062                     int tls_ty = 0;
5063
5064                     if ((tls_m & TLS_LD) != 0)
5065                       {
5066                         tls_ty = TLS_TLS | TLS_LD;
5067                         tls_m &= ~TLS_LD;
5068                       }
5069                     else if ((tls_m & TLS_GD) != 0)
5070                       {
5071                         tls_ty = TLS_TLS | TLS_GD;
5072                         tls_m &= ~TLS_GD;
5073                       }
5074                     else if ((tls_m & TLS_DTPREL) != 0)
5075                       {
5076                         tls_ty = TLS_TLS | TLS_DTPREL;
5077                         tls_m &= ~TLS_DTPREL;
5078                       }
5079                     else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5080                       {
5081                         tls_ty = TLS_TLS | TLS_TPREL;
5082                         tls_m = 0;
5083                       }
5084
5085                     /* Generate relocs for the dynamic linker.  */
5086                     if ((info->shared || indx != 0)
5087                         && (h == NULL
5088                             || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5089                             || h->root.type != bfd_link_hash_undefweak))
5090                       {
5091                         outrel.r_offset = (htab->got->output_section->vma
5092                                            + htab->got->output_offset
5093                                            + off);
5094                         outrel.r_addend = 0;
5095                         if (tls_ty & (TLS_LD | TLS_GD))
5096                           {
5097                             outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
5098                             if (tls_ty == (TLS_TLS | TLS_GD))
5099                               {
5100                                 loc = htab->relgot->contents;
5101                                 loc += (htab->relgot->reloc_count++
5102                                         * sizeof (Elf32_External_Rela));
5103                                 bfd_elf32_swap_reloca_out (output_bfd,
5104                                                            &outrel, loc);
5105                                 outrel.r_offset += 4;
5106                                 outrel.r_info
5107                                   = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5108                               }
5109                           }
5110                         else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5111                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5112                         else if (tls_ty == (TLS_TLS | TLS_TPREL))
5113                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5114                         else if (indx == 0)
5115                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5116                         else
5117                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
5118                         if (indx == 0)
5119                           {
5120                             outrel.r_addend += relocation;
5121                             if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
5122                               outrel.r_addend -= htab->elf.tls_sec->vma;
5123                           }
5124                         loc = htab->relgot->contents;
5125                         loc += (htab->relgot->reloc_count++
5126                                 * sizeof (Elf32_External_Rela));
5127                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5128                       }
5129
5130                     /* Init the .got section contents if we're not
5131                        emitting a reloc.  */
5132                     else
5133                       {
5134                         bfd_vma value = relocation;
5135
5136                         if (tls_ty == (TLS_TLS | TLS_LD))
5137                           value = 1;
5138                         else if (tls_ty != 0)
5139                           {
5140                             value -= htab->elf.tls_sec->vma + DTP_OFFSET;
5141                             if (tls_ty == (TLS_TLS | TLS_TPREL))
5142                               value += DTP_OFFSET - TP_OFFSET;
5143
5144                             if (tls_ty == (TLS_TLS | TLS_GD))
5145                               {
5146                                 bfd_put_32 (output_bfd, value,
5147                                             htab->got->contents + off + 4);
5148                                 value = 1;
5149                               }
5150                           }
5151                         bfd_put_32 (output_bfd, value,
5152                                     htab->got->contents + off);
5153                       }
5154
5155                     off += 4;
5156                     if (tls_ty & (TLS_LD | TLS_GD))
5157                       off += 4;
5158                   }
5159                 while (tls_m != 0);
5160
5161                 off = *offp;
5162                 *offp = off | 1;
5163               }
5164
5165             if (off >= (bfd_vma) -2)
5166               abort ();
5167
5168             if ((tls_type & TLS_TLS) != 0)
5169               {
5170                 if (tls_type != (TLS_TLS | TLS_LD))
5171                   {
5172                     if ((tls_mask & TLS_LD) != 0
5173                         && !(h == NULL
5174                              || !(h->elf_link_hash_flags
5175                                   & ELF_LINK_HASH_DEF_DYNAMIC)))
5176                       off += 8;
5177                     if (tls_type != (TLS_TLS | TLS_GD))
5178                       {
5179                         if ((tls_mask & TLS_GD) != 0)
5180                           off += 8;
5181                         if (tls_type != (TLS_TLS | TLS_DTPREL))
5182                           {
5183                             if ((tls_mask & TLS_DTPREL) != 0)
5184                               off += 4;
5185                           }
5186                       }
5187                   }
5188               }
5189
5190             relocation = htab->got->output_offset + off - 4;
5191
5192             /* Addends on got relocations don't make much sense.
5193                x+off@got is actually x@got+off, and since the got is
5194                generated by a hash table traversal, the value in the
5195                got at entry m+n bears little relation to the entry m.  */
5196             if (addend != 0)
5197               (*_bfd_error_handler)
5198                 (_("%s(%s+0x%lx): non-zero addend on %s reloc against `%s'"),
5199                  bfd_archive_filename (input_bfd),
5200                  bfd_get_section_name (input_bfd, input_section),
5201                  (long) rel->r_offset,
5202                  howto->name,
5203                  sym_name);
5204           }
5205         break;
5206
5207         /* Relocations that need no special processing.  */
5208         case R_PPC_LOCAL24PC:
5209           /* It makes no sense to point a local relocation
5210              at a symbol not in this object.  */
5211           if (unresolved_reloc)
5212             {
5213               if (! (*info->callbacks->undefined_symbol) (info,
5214                                                           h->root.root.string,
5215                                                           input_bfd,
5216                                                           input_section,
5217                                                           rel->r_offset,
5218                                                           TRUE))
5219                 return FALSE;
5220               continue;
5221             }
5222           break;
5223
5224         case R_PPC_DTPREL16:
5225         case R_PPC_DTPREL16_LO:
5226         case R_PPC_DTPREL16_HI:
5227         case R_PPC_DTPREL16_HA:
5228           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5229           break;
5230
5231           /* Relocations that may need to be propagated if this is a shared
5232              object.  */
5233         case R_PPC_TPREL16:
5234         case R_PPC_TPREL16_LO:
5235         case R_PPC_TPREL16_HI:
5236         case R_PPC_TPREL16_HA:
5237           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5238           /* The TPREL16 relocs shouldn't really be used in shared
5239              libs as they will result in DT_TEXTREL being set, but
5240              support them anyway.  */
5241           goto dodyn;
5242
5243         case R_PPC_TPREL32:
5244           addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5245           goto dodyn;
5246
5247         case R_PPC_DTPREL32:
5248           addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5249           goto dodyn;
5250
5251         case R_PPC_DTPMOD32:
5252           relocation = 1;
5253           addend = 0;
5254           goto dodyn;
5255
5256         case R_PPC_REL24:
5257         case R_PPC_REL32:
5258         case R_PPC_REL14:
5259         case R_PPC_REL14_BRTAKEN:
5260         case R_PPC_REL14_BRNTAKEN:
5261           /* If these relocations are not to a named symbol, they can be
5262              handled right here, no need to bother the dynamic linker.  */
5263           if (SYMBOL_REFERENCES_LOCAL (info, h)
5264               || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5265             break;
5266           /* fall through */
5267
5268           /* Relocations that always need to be propagated if this is a shared
5269              object.  */
5270         case R_PPC_ADDR32:
5271         case R_PPC_ADDR24:
5272         case R_PPC_ADDR16:
5273         case R_PPC_ADDR16_LO:
5274         case R_PPC_ADDR16_HI:
5275         case R_PPC_ADDR16_HA:
5276         case R_PPC_ADDR14:
5277         case R_PPC_ADDR14_BRTAKEN:
5278         case R_PPC_ADDR14_BRNTAKEN:
5279         case R_PPC_UADDR32:
5280         case R_PPC_UADDR16:
5281           /* r_symndx will be zero only for relocs against symbols
5282              from removed linkonce sections, or sections discarded by
5283              a linker script.  */
5284         dodyn:
5285           if (r_symndx == 0)
5286             break;
5287           /* Fall thru.  */
5288
5289           if ((info->shared
5290                && (h == NULL
5291                    || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5292                    || h->root.type != bfd_link_hash_undefweak)
5293                && (MUST_BE_DYN_RELOC (r_type)
5294                    || !SYMBOL_CALLS_LOCAL (info, h)))
5295               || (ELIMINATE_COPY_RELOCS
5296                   && !info->shared
5297                   && (input_section->flags & SEC_ALLOC) != 0
5298                   && h != NULL
5299                   && h->dynindx != -1
5300                   && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5301                   && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5302                   && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
5303             {
5304               int skip;
5305
5306 #ifdef DEBUG
5307               fprintf (stderr, "ppc_elf_relocate_section needs to "
5308                        "create relocation for %s\n",
5309                        (h && h->root.root.string
5310                         ? h->root.root.string : "<unknown>"));
5311 #endif
5312
5313               /* When generating a shared object, these relocations
5314                  are copied into the output file to be resolved at run
5315                  time.  */
5316               if (sreloc == NULL)
5317                 {
5318                   const char *name;
5319
5320                   name = (bfd_elf_string_from_elf_section
5321                           (input_bfd,
5322                            elf_elfheader (input_bfd)->e_shstrndx,
5323                            elf_section_data (input_section)->rel_hdr.sh_name));
5324                   if (name == NULL)
5325                     return FALSE;
5326
5327                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5328                               && strcmp (bfd_get_section_name (input_bfd,
5329                                                                input_section),
5330                                          name + 5) == 0);
5331
5332                   sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5333                   BFD_ASSERT (sreloc != NULL);
5334                 }
5335
5336               skip = 0;
5337
5338               outrel.r_offset =
5339                 _bfd_elf_section_offset (output_bfd, info, input_section,
5340                                          rel->r_offset);
5341               if (outrel.r_offset == (bfd_vma) -1
5342                   || outrel.r_offset == (bfd_vma) -2)
5343                 skip = (int) outrel.r_offset;
5344               outrel.r_offset += (input_section->output_section->vma
5345                                   + input_section->output_offset);
5346
5347               if (skip)
5348                 memset (&outrel, 0, sizeof outrel);
5349               else if (!SYMBOL_REFERENCES_LOCAL (info, h))
5350                 {
5351                   unresolved_reloc = FALSE;
5352                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5353                   outrel.r_addend = rel->r_addend;
5354                 }
5355               else
5356                 {
5357                   outrel.r_addend = relocation + rel->r_addend;
5358
5359                   if (r_type == R_PPC_ADDR32)
5360                     outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5361                   else
5362                     {
5363                       long indx;
5364
5365                       if (bfd_is_abs_section (sec))
5366                         indx = 0;
5367                       else if (sec == NULL || sec->owner == NULL)
5368                         {
5369                           bfd_set_error (bfd_error_bad_value);
5370                           return FALSE;
5371                         }
5372                       else
5373                         {
5374                           asection *osec;
5375
5376                           /* We are turning this relocation into one
5377                              against a section symbol.  It would be
5378                              proper to subtract the symbol's value,
5379                              osec->vma, from the emitted reloc addend,
5380                              but ld.so expects buggy relocs.  */
5381                           osec = sec->output_section;
5382                           indx = elf_section_data (osec)->dynindx;
5383                           BFD_ASSERT (indx > 0);
5384 #ifdef DEBUG
5385                           if (indx <= 0)
5386                             printf ("indx=%d section=%s flags=%08x name=%s\n",
5387                                     indx, osec->name, osec->flags,
5388                                     h->root.root.string);
5389 #endif
5390                         }
5391
5392                       outrel.r_info = ELF32_R_INFO (indx, r_type);
5393                     }
5394                 }
5395
5396               loc = sreloc->contents;
5397               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5398               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5399
5400               if (skip == -1)
5401                 continue;
5402
5403               /* This reloc will be computed at runtime.  We clear the memory
5404                  so that it contains predictable value.  */
5405               if (! skip
5406                   && ((input_section->flags & SEC_ALLOC) != 0
5407                       || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5408                 {
5409                   relocation = howto->pc_relative ? outrel.r_offset : 0;
5410                   addend = 0;
5411                   break;
5412                 }
5413             }
5414           break;
5415
5416         case R_PPC_RELAX32PC:
5417           relocation -= (input_section->output_section->vma
5418                          + input_section->output_offset
5419                          + rel->r_offset - 4);
5420           /* Fall thru */
5421         case R_PPC_RELAX32:
5422           {
5423             unsigned long t0;
5424             unsigned long t1;
5425
5426             t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
5427             t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
5428
5429             /* We're clearing the bits for R_PPC_ADDR16_HA
5430                and R_PPC_ADDR16_LO here.  */
5431             t0 &= ~0xffff;
5432             t1 &= ~0xffff;
5433
5434             /* t0 is HA, t1 is LO */
5435             relocation += addend;
5436             t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
5437             t1 |= relocation & 0xffff;
5438
5439             bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
5440             bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
5441           }
5442           continue;
5443
5444           /* Indirect .sdata relocation.  */
5445         case R_PPC_EMB_SDAI16:
5446           BFD_ASSERT (htab->sdata != NULL);
5447           relocation
5448             = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5449                                                  htab->sdata, h, relocation,
5450                                                  rel, R_PPC_RELATIVE);
5451           break;
5452
5453           /* Indirect .sdata2 relocation.  */
5454         case R_PPC_EMB_SDA2I16:
5455           BFD_ASSERT (htab->sdata2 != NULL);
5456           relocation
5457             = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5458                                                  htab->sdata2, h, relocation,
5459                                                  rel, R_PPC_RELATIVE);
5460           break;
5461
5462           /* Handle the TOC16 reloc.  We want to use the offset within the .got
5463              section, not the actual VMA.  This is appropriate when generating
5464              an embedded ELF object, for which the .got section acts like the
5465              AIX .toc section.  */
5466         case R_PPC_TOC16:                       /* phony GOT16 relocations */
5467           BFD_ASSERT (sec != NULL);
5468           BFD_ASSERT (bfd_is_und_section (sec)
5469                       || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5470                       || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
5471
5472             addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5473           break;
5474
5475         case R_PPC_PLTREL24:
5476           /* Relocation is to the entry for this symbol in the
5477              procedure linkage table.  */
5478           BFD_ASSERT (h != NULL);
5479
5480           if (h->plt.offset == (bfd_vma) -1
5481               || htab->plt == NULL)
5482             {
5483               /* We didn't make a PLT entry for this symbol.  This
5484                  happens when statically linking PIC code, or when
5485                  using -Bsymbolic.  */
5486               break;
5487             }
5488
5489           unresolved_reloc = FALSE;
5490           relocation = (htab->plt->output_section->vma
5491                         + htab->plt->output_offset
5492                         + h->plt.offset);
5493           break;
5494
5495           /* Relocate against _SDA_BASE_.  */
5496         case R_PPC_SDAREL16:
5497           {
5498             const char *name;
5499             const struct elf_link_hash_entry *sh;
5500
5501             BFD_ASSERT (sec != NULL);
5502             name = bfd_get_section_name (abfd, sec->output_section);
5503             if (! ((strncmp (name, ".sdata", 6) == 0
5504                     && (name[6] == 0 || name[6] == '.'))
5505                    || (strncmp (name, ".sbss", 5) == 0
5506                        && (name[5] == 0 || name[5] == '.'))))
5507               {
5508                 (*_bfd_error_handler)
5509                   (_("%s: the target (%s) of a %s relocation is "
5510                      "in the wrong output section (%s)"),
5511                    bfd_archive_filename (input_bfd),
5512                    sym_name,
5513                    howto->name,
5514                    name);
5515               }
5516             sh = htab->sdata->sym_hash;
5517             addend -= (sh->root.u.def.value
5518                        + sh->root.u.def.section->output_section->vma
5519                        + sh->root.u.def.section->output_offset);
5520           }
5521           break;
5522
5523           /* Relocate against _SDA2_BASE_.  */
5524         case R_PPC_EMB_SDA2REL:
5525           {
5526             const char *name;
5527             const struct elf_link_hash_entry *sh;
5528
5529             BFD_ASSERT (sec != NULL);
5530             name = bfd_get_section_name (abfd, sec->output_section);
5531             if (! (strncmp (name, ".sdata2", 7) == 0
5532                    || strncmp (name, ".sbss2", 6) == 0))
5533               {
5534                 (*_bfd_error_handler)
5535                   (_("%s: the target (%s) of a %s relocation is "
5536                      "in the wrong output section (%s)"),
5537                    bfd_archive_filename (input_bfd),
5538                    sym_name,
5539                    howto->name,
5540                    name);
5541
5542                 bfd_set_error (bfd_error_bad_value);
5543                 ret = FALSE;
5544                 continue;
5545               }
5546             sh = htab->sdata2->sym_hash;
5547             addend -= (sh->root.u.def.value
5548                        + sh->root.u.def.section->output_section->vma
5549                        + sh->root.u.def.section->output_offset);
5550           }
5551           break;
5552
5553           /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
5554         case R_PPC_EMB_SDA21:
5555         case R_PPC_EMB_RELSDA:
5556           {
5557             const char *name;
5558             const struct elf_link_hash_entry *sh;
5559             int reg;
5560
5561             BFD_ASSERT (sec != NULL);
5562             name = bfd_get_section_name (abfd, sec->output_section);
5563             if (((strncmp (name, ".sdata", 6) == 0
5564                   && (name[6] == 0 || name[6] == '.'))
5565                  || (strncmp (name, ".sbss", 5) == 0
5566                      && (name[5] == 0 || name[5] == '.'))))
5567               {
5568                 reg = 13;
5569                 sh = htab->sdata->sym_hash;
5570                 addend -= (sh->root.u.def.value
5571                            + sh->root.u.def.section->output_section->vma
5572                            + sh->root.u.def.section->output_offset);
5573               }
5574
5575             else if (strncmp (name, ".sdata2", 7) == 0
5576                      || strncmp (name, ".sbss2", 6) == 0)
5577               {
5578                 reg = 2;
5579                 sh = htab->sdata2->sym_hash;
5580                 addend -= (sh->root.u.def.value
5581                            + sh->root.u.def.section->output_section->vma
5582                            + sh->root.u.def.section->output_offset);
5583               }
5584
5585             else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5586                      || strcmp (name, ".PPC.EMB.sbss0") == 0)
5587               {
5588                 reg = 0;
5589               }
5590
5591             else
5592               {
5593                 (*_bfd_error_handler)
5594                   (_("%s: the target (%s) of a %s relocation is "
5595                      "in the wrong output section (%s)"),
5596                    bfd_archive_filename (input_bfd),
5597                    sym_name,
5598                    howto->name,
5599                    name);
5600
5601                 bfd_set_error (bfd_error_bad_value);
5602                 ret = FALSE;
5603                 continue;
5604               }
5605
5606             if (r_type == R_PPC_EMB_SDA21)
5607               {                 /* fill in register field */
5608                 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5609                 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5610                 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5611               }
5612           }
5613           break;
5614
5615           /* Relocate against the beginning of the section.  */
5616         case R_PPC_SECTOFF:
5617         case R_PPC_SECTOFF_LO:
5618         case R_PPC_SECTOFF_HI:
5619         case R_PPC_SECTOFF_HA:
5620           BFD_ASSERT (sec != NULL);
5621           addend -= sec->output_section->vma;
5622           break;
5623
5624           /* Negative relocations.  */
5625         case R_PPC_EMB_NADDR32:
5626         case R_PPC_EMB_NADDR16:
5627         case R_PPC_EMB_NADDR16_LO:
5628         case R_PPC_EMB_NADDR16_HI:
5629         case R_PPC_EMB_NADDR16_HA:
5630           addend -= 2 * relocation;
5631           break;
5632
5633         case R_PPC_COPY:
5634         case R_PPC_GLOB_DAT:
5635         case R_PPC_JMP_SLOT:
5636         case R_PPC_RELATIVE:
5637         case R_PPC_PLT32:
5638         case R_PPC_PLTREL32:
5639         case R_PPC_PLT16_LO:
5640         case R_PPC_PLT16_HI:
5641         case R_PPC_PLT16_HA:
5642         case R_PPC_ADDR30:
5643         case R_PPC_EMB_RELSEC16:
5644         case R_PPC_EMB_RELST_LO:
5645         case R_PPC_EMB_RELST_HI:
5646         case R_PPC_EMB_RELST_HA:
5647         case R_PPC_EMB_BIT_FLD:
5648           (*_bfd_error_handler)
5649             (_("%s: relocation %s is not yet supported for symbol %s."),
5650              bfd_archive_filename (input_bfd),
5651              howto->name,
5652              sym_name);
5653
5654           bfd_set_error (bfd_error_invalid_operation);
5655           ret = FALSE;
5656           continue;
5657         }
5658
5659       /* Do any further special processing.  */
5660       switch (r_type)
5661         {
5662         default:
5663           break;
5664
5665         case R_PPC_ADDR16_HA:
5666         case R_PPC_GOT16_HA:
5667         case R_PPC_PLT16_HA:
5668         case R_PPC_SECTOFF_HA:
5669         case R_PPC_TPREL16_HA:
5670         case R_PPC_DTPREL16_HA:
5671         case R_PPC_GOT_TLSGD16_HA:
5672         case R_PPC_GOT_TLSLD16_HA:
5673         case R_PPC_GOT_TPREL16_HA:
5674         case R_PPC_GOT_DTPREL16_HA:
5675         case R_PPC_EMB_NADDR16_HA:
5676         case R_PPC_EMB_RELST_HA:
5677           /* It's just possible that this symbol is a weak symbol
5678              that's not actually defined anywhere.  In that case,
5679              'sec' would be NULL, and we should leave the symbol
5680              alone (it will be set to zero elsewhere in the link).  */
5681           if (sec != NULL)
5682             /* Add 0x10000 if sign bit in 0:15 is set.
5683                Bits 0:15 are not used.  */
5684             addend += 0x8000;
5685           break;
5686         }
5687
5688 #ifdef DEBUG
5689       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5690                "offset = %ld, addend = %ld\n",
5691                howto->name,
5692                (int) r_type,
5693                sym_name,
5694                r_symndx,
5695                (long) rel->r_offset,
5696                (long) addend);
5697 #endif
5698
5699       if (unresolved_reloc
5700           && !((input_section->flags & SEC_DEBUGGING) != 0
5701                && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5702         {
5703           (*_bfd_error_handler)
5704             (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
5705              bfd_archive_filename (input_bfd),
5706              bfd_get_section_name (input_bfd, input_section),
5707              (long) rel->r_offset,
5708              howto->name,
5709              sym_name);
5710           ret = FALSE;
5711         }
5712
5713       r = _bfd_final_link_relocate (howto,
5714                                     input_bfd,
5715                                     input_section,
5716                                     contents,
5717                                     rel->r_offset,
5718                                     relocation,
5719                                     addend);
5720
5721       if (r != bfd_reloc_ok)
5722         {
5723           if (sym_name == NULL)
5724             sym_name = "(null)";
5725           if (r == bfd_reloc_overflow)
5726             {
5727               if (warned)
5728                 continue;
5729               if (h != NULL
5730                   && h->root.type == bfd_link_hash_undefweak
5731                   && howto->pc_relative)
5732                 {
5733                   /* Assume this is a call protected by other code that
5734                      detect the symbol is undefined.  If this is the case,
5735                      we can safely ignore the overflow.  If not, the
5736                      program is hosed anyway, and a little warning isn't
5737                      going to help.  */
5738
5739                   continue;
5740                 }
5741
5742               if (! (*info->callbacks->reloc_overflow) (info,
5743                                                         sym_name,
5744                                                         howto->name,
5745                                                         rel->r_addend,
5746                                                         input_bfd,
5747                                                         input_section,
5748                                                         rel->r_offset))
5749                 return FALSE;
5750             }
5751           else
5752             {
5753               (*_bfd_error_handler)
5754                 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
5755                  bfd_archive_filename (input_bfd),
5756                  bfd_get_section_name (input_bfd, input_section),
5757                  (long) rel->r_offset, howto->name, sym_name, (int) r);
5758               ret = FALSE;
5759             }
5760         }
5761     }
5762
5763 #ifdef DEBUG
5764   fprintf (stderr, "\n");
5765 #endif
5766
5767   return ret;
5768 }
5769
5770 static enum elf_reloc_type_class
5771 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
5772 {
5773   switch (ELF32_R_TYPE (rela->r_info))
5774     {
5775     case R_PPC_RELATIVE:
5776       return reloc_class_relative;
5777     case R_PPC_REL24:
5778     case R_PPC_ADDR24:
5779     case R_PPC_JMP_SLOT:
5780       return reloc_class_plt;
5781     case R_PPC_COPY:
5782       return reloc_class_copy;
5783     default:
5784       return reloc_class_normal;
5785     }
5786 }
5787 \f
5788 /* Support for core dump NOTE sections.  */
5789
5790 static bfd_boolean
5791 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5792 {
5793   int offset;
5794   unsigned int raw_size;
5795
5796   switch (note->descsz)
5797     {
5798     default:
5799       return FALSE;
5800
5801     case 268:           /* Linux/PPC.  */
5802       /* pr_cursig */
5803       elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
5804
5805       /* pr_pid */
5806       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
5807
5808       /* pr_reg */
5809       offset = 72;
5810       raw_size = 192;
5811
5812       break;
5813     }
5814
5815   /* Make a ".reg/999" section.  */
5816   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5817                                           raw_size, note->descpos + offset);
5818 }
5819
5820 static bfd_boolean
5821 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5822 {
5823   switch (note->descsz)
5824     {
5825     default:
5826       return FALSE;
5827
5828     case 128:           /* Linux/PPC elf_prpsinfo.  */
5829       elf_tdata (abfd)->core_program
5830         = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
5831       elf_tdata (abfd)->core_command
5832         = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
5833     }
5834
5835   /* Note that for some reason, a spurious space is tacked
5836      onto the end of the args in some (at least one anyway)
5837      implementations, so strip it off if it exists.  */
5838
5839   {
5840     char *command = elf_tdata (abfd)->core_command;
5841     int n = strlen (command);
5842
5843     if (0 < n && command[n - 1] == ' ')
5844       command[n - 1] = '\0';
5845   }
5846
5847   return TRUE;
5848 }
5849 \f
5850 /* Very simple linked list structure for recording apuinfo values.  */
5851 typedef struct apuinfo_list
5852 {
5853   struct apuinfo_list *next;
5854   unsigned long value;
5855 }
5856 apuinfo_list;
5857
5858 static apuinfo_list *head;
5859
5860
5861 static void
5862 apuinfo_list_init (void)
5863 {
5864   head = NULL;
5865 }
5866
5867 static void
5868 apuinfo_list_add (unsigned long value)
5869 {
5870   apuinfo_list *entry = head;
5871
5872   while (entry != NULL)
5873     {
5874       if (entry->value == value)
5875         return;
5876       entry = entry->next;
5877     }
5878
5879   entry = bfd_malloc (sizeof (* entry));
5880   if (entry == NULL)
5881     return;
5882
5883   entry->value = value;
5884   entry->next  = head;
5885   head = entry;
5886 }
5887
5888 static unsigned
5889 apuinfo_list_length (void)
5890 {
5891   apuinfo_list *entry;
5892   unsigned long count;
5893
5894   for (entry = head, count = 0;
5895        entry;
5896        entry = entry->next)
5897     ++ count;
5898
5899   return count;
5900 }
5901
5902 static inline unsigned long
5903 apuinfo_list_element (unsigned long number)
5904 {
5905   apuinfo_list * entry;
5906
5907   for (entry = head;
5908        entry && number --;
5909        entry = entry->next)
5910     ;
5911
5912   return entry ? entry->value : 0;
5913 }
5914
5915 static void
5916 apuinfo_list_finish (void)
5917 {
5918   apuinfo_list *entry;
5919
5920   for (entry = head; entry;)
5921     {
5922       apuinfo_list *next = entry->next;
5923       free (entry);
5924       entry = next;
5925     }
5926
5927   head = NULL;
5928 }
5929
5930 #define APUINFO_SECTION_NAME    ".PPC.EMB.apuinfo"
5931 #define APUINFO_LABEL           "APUinfo"
5932
5933 /* Scan the input BFDs and create a linked list of
5934    the APUinfo values that will need to be emitted.  */
5935
5936 static void
5937 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
5938 {
5939   bfd *ibfd;
5940   asection *asec;
5941   char *buffer;
5942   unsigned num_input_sections;
5943   bfd_size_type output_section_size;
5944   unsigned i;
5945   unsigned num_entries;
5946   unsigned long offset;
5947   unsigned long length;
5948   const char *error_message = NULL;
5949
5950   if (link_info == NULL)
5951     return;
5952
5953   /* Scan the input bfds, looking for apuinfo sections.  */
5954   num_input_sections = 0;
5955   output_section_size = 0;
5956
5957   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5958     {
5959       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5960       if (asec)
5961         {
5962           ++ num_input_sections;
5963           output_section_size += asec->_raw_size;
5964         }
5965     }
5966
5967   /* We need at least one input sections
5968      in order to make merging worthwhile.  */
5969   if (num_input_sections < 1)
5970     return;
5971
5972   /* Just make sure that the output section exists as well.  */
5973   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5974   if (asec == NULL)
5975     return;
5976
5977   /* Allocate a buffer for the contents of the input sections.  */
5978   buffer = bfd_malloc (output_section_size);
5979   if (buffer == NULL)
5980     return;
5981
5982   offset = 0;
5983   apuinfo_list_init ();
5984
5985   /* Read in the input sections contents.  */
5986   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5987     {
5988       unsigned long datum;
5989       char *ptr;
5990
5991       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5992       if (asec == NULL)
5993         continue;
5994
5995       length = asec->_raw_size;
5996       if (length < 24)
5997         {
5998           error_message = _("corrupt or empty %s section in %s");
5999           goto fail;
6000         }
6001
6002       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
6003           || (bfd_bread (buffer + offset, length, ibfd) != length))
6004         {
6005           error_message = _("unable to read in %s section from %s");
6006           goto fail;
6007         }
6008
6009       /* Process the contents of the section.  */
6010       ptr = buffer + offset;
6011       error_message = _("corrupt %s section in %s");
6012
6013       /* Verify the contents of the header.  Note - we have to
6014          extract the values this way in order to allow for a
6015          host whose endian-ness is different from the target.  */
6016       datum = bfd_get_32 (ibfd, ptr);
6017       if (datum != sizeof APUINFO_LABEL)
6018         goto fail;
6019
6020       datum = bfd_get_32 (ibfd, ptr + 8);
6021       if (datum != 0x2)
6022         goto fail;
6023
6024       if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
6025         goto fail;
6026
6027       /* Get the number of bytes used for apuinfo entries.  */
6028       datum = bfd_get_32 (ibfd, ptr + 4);
6029       if (datum + 20 != length)
6030         goto fail;
6031
6032       /* Make sure that we do not run off the end of the section.  */
6033       if (offset + length > output_section_size)
6034         goto fail;
6035
6036       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
6037       for (i = 0; i < datum; i += 4)
6038         apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
6039
6040       /* Update the offset.  */
6041       offset += length;
6042     }
6043
6044   error_message = NULL;
6045
6046   /* Compute the size of the output section.  */
6047   num_entries = apuinfo_list_length ();
6048   output_section_size = 20 + num_entries * 4;
6049
6050   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
6051
6052   if (! bfd_set_section_size (abfd, asec, output_section_size))
6053     ibfd = abfd,
6054       error_message = _("warning: unable to set size of %s section in %s");
6055
6056  fail:
6057   free (buffer);
6058
6059   if (error_message)
6060     (*_bfd_error_handler) (error_message, APUINFO_SECTION_NAME,
6061                            bfd_archive_filename (ibfd));
6062 }
6063
6064
6065 /* Prevent the output section from accumulating the input sections'
6066    contents.  We have already stored this in our linked list structure.  */
6067
6068 static bfd_boolean
6069 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
6070                        asection *asec,
6071                        bfd_byte *contents ATTRIBUTE_UNUSED)
6072 {
6073   return (apuinfo_list_length ()
6074           && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
6075 }
6076
6077
6078 /* Finally we can generate the output section.  */
6079
6080 static void
6081 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
6082 {
6083   bfd_byte *buffer;
6084   asection *asec;
6085   unsigned i;
6086   unsigned num_entries;
6087   bfd_size_type length;
6088
6089   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
6090   if (asec == NULL)
6091     return;
6092
6093   if (apuinfo_list_length () == 0)
6094     return;
6095
6096   length = asec->_raw_size;
6097   if (length < 20)
6098     return;
6099
6100   buffer = bfd_malloc (length);
6101   if (buffer == NULL)
6102     {
6103       (*_bfd_error_handler)
6104         (_("failed to allocate space for new APUinfo section."));
6105       return;
6106     }
6107
6108   /* Create the apuinfo header.  */
6109   num_entries = apuinfo_list_length ();
6110   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
6111   bfd_put_32 (abfd, num_entries * 4, buffer + 4);
6112   bfd_put_32 (abfd, 0x2, buffer + 8);
6113   strcpy (buffer + 12, APUINFO_LABEL);
6114
6115   length = 20;
6116   for (i = 0; i < num_entries; i++)
6117     {
6118       bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
6119       length += 4;
6120     }
6121
6122   if (length != asec->_raw_size)
6123     (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
6124
6125   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
6126     (*_bfd_error_handler) (_("failed to install new APUinfo section."));
6127
6128   free (buffer);
6129
6130   apuinfo_list_finish ();
6131 }
6132
6133 /* Return address for Ith PLT stub in section PLT, for relocation REL
6134    or (bfd_vma) -1 if it should not be included.  */
6135
6136 static bfd_vma
6137 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
6138                      const asection *plt ATTRIBUTE_UNUSED,
6139                      const arelent *rel)
6140 {
6141   return rel->address;
6142 }
6143
6144 /* Add extra PPC sections -- Note, for now, make .sbss2 and
6145    .PPC.EMB.sbss0 a normal section, and not a bss section so
6146    that the linker doesn't crater when trying to make more than
6147    2 sections.  */
6148
6149 static struct bfd_elf_special_section const ppc_elf_special_sections[]=
6150 {
6151   { ".tags",             5,  0, SHT_ORDERED,  SHF_ALLOC },
6152   { ".sdata",            6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6153   { ".sbss",             5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
6154   { ".sdata2",           7, -2, SHT_PROGBITS, SHF_ALLOC },
6155   { ".sbss2",            6, -2, SHT_PROGBITS, SHF_ALLOC },
6156   { ".PPC.EMB.apuinfo", 16,  0, SHT_NOTE,     0 },
6157   { ".PPC.EMB.sdata0",  15,  0, SHT_PROGBITS, SHF_ALLOC },
6158   { ".PPC.EMB.sbss0",   14,  0, SHT_PROGBITS, SHF_ALLOC },
6159   { ".plt",              4,  0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
6160   { NULL,                0,  0, 0,            0 }
6161 };
6162 \f
6163 #define TARGET_LITTLE_SYM       bfd_elf32_powerpcle_vec
6164 #define TARGET_LITTLE_NAME      "elf32-powerpcle"
6165 #define TARGET_BIG_SYM          bfd_elf32_powerpc_vec
6166 #define TARGET_BIG_NAME         "elf32-powerpc"
6167 #define ELF_ARCH                bfd_arch_powerpc
6168 #define ELF_MACHINE_CODE        EM_PPC
6169 #ifdef __QNXTARGET__
6170 #define ELF_MAXPAGESIZE         0x1000
6171 #else
6172 #define ELF_MAXPAGESIZE         0x10000
6173 #endif
6174 #define elf_info_to_howto       ppc_elf_info_to_howto
6175
6176 #ifdef  EM_CYGNUS_POWERPC
6177 #define ELF_MACHINE_ALT1        EM_CYGNUS_POWERPC
6178 #endif
6179
6180 #ifdef EM_PPC_OLD
6181 #define ELF_MACHINE_ALT2        EM_PPC_OLD
6182 #endif
6183
6184 #define elf_backend_plt_not_loaded      1
6185 #define elf_backend_got_symbol_offset   4
6186 #define elf_backend_can_gc_sections     1
6187 #define elf_backend_can_refcount        1
6188 #define elf_backend_got_header_size     12
6189 #define elf_backend_rela_normal         1
6190
6191 #define bfd_elf32_mkobject                      ppc_elf_mkobject
6192 #define bfd_elf32_bfd_merge_private_bfd_data    ppc_elf_merge_private_bfd_data
6193 #define bfd_elf32_bfd_relax_section             ppc_elf_relax_section
6194 #define bfd_elf32_bfd_reloc_type_lookup         ppc_elf_reloc_type_lookup
6195 #define bfd_elf32_bfd_set_private_flags         ppc_elf_set_private_flags
6196 #define bfd_elf32_bfd_link_hash_table_create    ppc_elf_link_hash_table_create
6197
6198 #define elf_backend_object_p                    ppc_elf_object_p
6199 #define elf_backend_gc_mark_hook                ppc_elf_gc_mark_hook
6200 #define elf_backend_gc_sweep_hook               ppc_elf_gc_sweep_hook
6201 #define elf_backend_section_from_shdr           ppc_elf_section_from_shdr
6202 #define elf_backend_relocate_section            ppc_elf_relocate_section
6203 #define elf_backend_create_dynamic_sections     ppc_elf_create_dynamic_sections
6204 #define elf_backend_check_relocs                ppc_elf_check_relocs
6205 #define elf_backend_copy_indirect_symbol        ppc_elf_copy_indirect_symbol
6206 #define elf_backend_adjust_dynamic_symbol       ppc_elf_adjust_dynamic_symbol
6207 #define elf_backend_add_symbol_hook             ppc_elf_add_symbol_hook
6208 #define elf_backend_size_dynamic_sections       ppc_elf_size_dynamic_sections
6209 #define elf_backend_finish_dynamic_symbol       ppc_elf_finish_dynamic_symbol
6210 #define elf_backend_finish_dynamic_sections     ppc_elf_finish_dynamic_sections
6211 #define elf_backend_fake_sections               ppc_elf_fake_sections
6212 #define elf_backend_additional_program_headers  ppc_elf_additional_program_headers
6213 #define elf_backend_modify_segment_map          ppc_elf_modify_segment_map
6214 #define elf_backend_grok_prstatus               ppc_elf_grok_prstatus
6215 #define elf_backend_grok_psinfo                 ppc_elf_grok_psinfo
6216 #define elf_backend_reloc_type_class            ppc_elf_reloc_type_class
6217 #define elf_backend_begin_write_processing      ppc_elf_begin_write_processing
6218 #define elf_backend_final_write_processing      ppc_elf_final_write_processing
6219 #define elf_backend_write_section               ppc_elf_write_section
6220 #define elf_backend_special_sections            ppc_elf_special_sections
6221 #define elf_backend_plt_sym_val                 ppc_elf_plt_sym_val
6222
6223 #include "elf32-target.h"