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