Back to index

cell-binutils  2.17cvs20070401
elf32-ppc.c
Go to the documentation of this file.
00001 /* PowerPC-specific support for 32-bit ELF
00002    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
00003    2004, 2005, 2006, 2007 Free Software Foundation, Inc.
00004    Written by Ian Lance Taylor, Cygnus Support.
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the
00020    Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00021    Boston, MA 02110-1301, USA.  */
00022 
00023 /* This file is based on a preliminary PowerPC ELF ABI.  The
00024    information may not match the final PowerPC ELF ABI.  It includes
00025    suggestions from the in-progress Embedded PowerPC ABI, and that
00026    information may also not match.  */
00027 
00028 #include <stdarg.h>
00029 #include "bfd.h"
00030 #include "sysdep.h"
00031 #include "bfdlink.h"
00032 #include "libbfd.h"
00033 #include "elf-bfd.h"
00034 #include "elf/ppc.h"
00035 #include "elf32-ppc.h"
00036 #include "elf-vxworks.h"
00037 
00038 /* RELA relocations are used here.  */
00039 
00040 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
00041   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00042 static bfd_reloc_status_type ppc_elf_unhandled_reloc
00043   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
00044 
00045 /* Branch prediction bit for branch taken relocs.  */
00046 #define BRANCH_PREDICT_BIT 0x200000
00047 /* Mask to set RA in memory instructions.  */
00048 #define RA_REGISTER_MASK 0x001f0000
00049 /* Value to shift register by to insert RA.  */
00050 #define RA_REGISTER_SHIFT 16
00051 
00052 /* The name of the dynamic interpreter.  This is put in the .interp
00053    section.  */
00054 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
00055 
00056 /* For old-style PLT.  */
00057 /* The number of single-slot PLT entries (the rest use two slots).  */
00058 #define PLT_NUM_SINGLE_ENTRIES 8192
00059 
00060 /* For new-style .glink and .plt.  */
00061 #define GLINK_PLTRESOLVE 16*4
00062 #define GLINK_ENTRY_SIZE 4*4
00063 
00064 /* VxWorks uses its own plt layout, filled in by the static linker.  */
00065 
00066 /* The standard VxWorks PLT entry.  */
00067 #define VXWORKS_PLT_ENTRY_SIZE 32
00068 static const bfd_vma ppc_elf_vxworks_plt_entry
00069     [VXWORKS_PLT_ENTRY_SIZE / 4] =
00070   {
00071     0x3d800000, /* lis     r12,0                 */
00072     0x818c0000, /* lwz     r12,0(r12)            */
00073     0x7d8903a6, /* mtctr   r12                   */
00074     0x4e800420, /* bctr                          */
00075     0x39600000, /* li      r11,0                 */
00076     0x48000000, /* b       14 <.PLT0resolve+0x4> */
00077     0x60000000, /* nop                           */
00078     0x60000000, /* nop                           */
00079   };
00080 static const bfd_vma ppc_elf_vxworks_pic_plt_entry
00081     [VXWORKS_PLT_ENTRY_SIZE / 4] =
00082   {
00083     0x3d9e0000, /* addis r12,r30,0 */
00084     0x818c0000, /* lwz       r12,0(r12) */
00085     0x7d8903a6, /* mtctr r12 */
00086     0x4e800420, /* bctr */
00087     0x39600000, /* li        r11,0 */
00088     0x48000000, /* b  14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
00089     0x60000000, /* nop */
00090     0x60000000, /* nop */
00091   };
00092 
00093 /* The initial VxWorks PLT entry.  */
00094 #define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
00095 static const bfd_vma ppc_elf_vxworks_plt0_entry
00096     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
00097   {
00098     0x3d800000, /* lis     r12,0        */
00099     0x398c0000, /* addi    r12,r12,0    */
00100     0x800c0008, /* lwz     r0,8(r12)    */
00101     0x7c0903a6, /* mtctr   r0           */
00102     0x818c0004, /* lwz     r12,4(r12)   */
00103     0x4e800420, /* bctr                 */
00104     0x60000000, /* nop                  */
00105     0x60000000, /* nop                  */
00106   };
00107 static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
00108     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
00109   {
00110     0x819e0008, /* lwz       r12,8(r30) */
00111     0x7d8903a6, /* mtctr r12        */
00112     0x819e0004, /* lwz       r12,4(r30) */
00113     0x4e800420, /* bctr             */
00114     0x60000000, /* nop              */
00115     0x60000000, /* nop              */
00116     0x60000000, /* nop              */
00117     0x60000000, /* nop              */
00118   };
00119 
00120 /* For executables, we have some additional relocations in
00121    .rela.plt.unloaded, for the kernel loader.  */
00122 
00123 /* The number of non-JMP_SLOT relocations per PLT0 slot. */
00124 #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
00125 /* The number of relocations in the PLTResolve slot. */
00126 #define VXWORKS_PLTRESOLVE_RELOCS 2
00127 /* The number of relocations in the PLTResolve slot when when creating
00128    a shared library. */
00129 #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
00130 
00131 /* Some instructions.  */
00132 #define ADDIS_11_11  0x3d6b0000
00133 #define ADDIS_11_30  0x3d7e0000
00134 #define ADDIS_12_12  0x3d8c0000
00135 #define ADDI_11_11   0x396b0000
00136 #define ADD_0_11_11  0x7c0b5a14
00137 #define ADD_11_0_11  0x7d605a14
00138 #define B            0x48000000
00139 #define BCL_20_31    0x429f0005
00140 #define BCTR         0x4e800420
00141 #define LIS_11              0x3d600000
00142 #define LIS_12              0x3d800000
00143 #define LWZU_0_12    0x840c0000
00144 #define LWZ_0_12     0x800c0000
00145 #define LWZ_11_11    0x816b0000
00146 #define LWZ_11_30    0x817e0000
00147 #define LWZ_12_12    0x818c0000
00148 #define MFLR_0              0x7c0802a6
00149 #define MFLR_12             0x7d8802a6
00150 #define MTCTR_0             0x7c0903a6
00151 #define MTCTR_11     0x7d6903a6
00152 #define MTLR_0              0x7c0803a6
00153 #define NOP          0x60000000
00154 #define SUB_11_11_12 0x7d6c5850
00155 
00156 /* Offset of tp and dtp pointers from start of TLS block.  */
00157 #define TP_OFFSET    0x7000
00158 #define DTP_OFFSET   0x8000
00159 
00160 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
00161 
00162 static reloc_howto_type ppc_elf_howto_raw[] = {
00163   /* This reloc does nothing.  */
00164   HOWTO (R_PPC_NONE,        /* type */
00165         0,                  /* rightshift */
00166         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00167         32,                 /* bitsize */
00168         FALSE,                     /* pc_relative */
00169         0,                  /* bitpos */
00170         complain_overflow_bitfield, /* complain_on_overflow */
00171         bfd_elf_generic_reloc,     /* special_function */
00172         "R_PPC_NONE",              /* name */
00173         FALSE,                     /* partial_inplace */
00174         0,                  /* src_mask */
00175         0,                  /* dst_mask */
00176         FALSE),             /* pcrel_offset */
00177 
00178   /* A standard 32 bit relocation.  */
00179   HOWTO (R_PPC_ADDR32,             /* type */
00180         0,                  /* rightshift */
00181         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00182         32,                 /* bitsize */
00183         FALSE,                     /* pc_relative */
00184         0,                  /* bitpos */
00185         complain_overflow_bitfield, /* complain_on_overflow */
00186         bfd_elf_generic_reloc,     /* special_function */
00187         "R_PPC_ADDR32",     /* name */
00188         FALSE,                     /* partial_inplace */
00189         0,                  /* src_mask */
00190         0xffffffff,         /* dst_mask */
00191         FALSE),             /* pcrel_offset */
00192 
00193   /* An absolute 26 bit branch; the lower two bits must be zero.
00194      FIXME: we don't check that, we just clear them.  */
00195   HOWTO (R_PPC_ADDR24,             /* type */
00196         0,                  /* rightshift */
00197         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00198         26,                 /* bitsize */
00199         FALSE,                     /* pc_relative */
00200         0,                  /* bitpos */
00201         complain_overflow_bitfield, /* complain_on_overflow */
00202         bfd_elf_generic_reloc,     /* special_function */
00203         "R_PPC_ADDR24",     /* name */
00204         FALSE,                     /* partial_inplace */
00205         0,                  /* src_mask */
00206         0x3fffffc,          /* dst_mask */
00207         FALSE),             /* pcrel_offset */
00208 
00209   /* A standard 16 bit relocation.  */
00210   HOWTO (R_PPC_ADDR16,             /* type */
00211         0,                  /* rightshift */
00212         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00213         16,                 /* bitsize */
00214         FALSE,                     /* pc_relative */
00215         0,                  /* bitpos */
00216         complain_overflow_bitfield, /* complain_on_overflow */
00217         bfd_elf_generic_reloc,     /* special_function */
00218         "R_PPC_ADDR16",     /* name */
00219         FALSE,                     /* partial_inplace */
00220         0,                  /* src_mask */
00221         0xffff,             /* dst_mask */
00222         FALSE),             /* pcrel_offset */
00223 
00224   /* A 16 bit relocation without overflow.  */
00225   HOWTO (R_PPC_ADDR16_LO,   /* type */
00226         0,                  /* rightshift */
00227         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00228         16,                 /* bitsize */
00229         FALSE,                     /* pc_relative */
00230         0,                  /* bitpos */
00231         complain_overflow_dont,/* complain_on_overflow */
00232         bfd_elf_generic_reloc,     /* special_function */
00233         "R_PPC_ADDR16_LO",  /* name */
00234         FALSE,                     /* partial_inplace */
00235         0,                  /* src_mask */
00236         0xffff,             /* dst_mask */
00237         FALSE),             /* pcrel_offset */
00238 
00239   /* The high order 16 bits of an address.  */
00240   HOWTO (R_PPC_ADDR16_HI,   /* type */
00241         16,                 /* rightshift */
00242         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00243         16,                 /* bitsize */
00244         FALSE,                     /* pc_relative */
00245         0,                  /* bitpos */
00246         complain_overflow_dont, /* complain_on_overflow */
00247         bfd_elf_generic_reloc,     /* special_function */
00248         "R_PPC_ADDR16_HI",  /* name */
00249         FALSE,                     /* partial_inplace */
00250         0,                  /* src_mask */
00251         0xffff,             /* dst_mask */
00252         FALSE),             /* pcrel_offset */
00253 
00254   /* The high order 16 bits of an address, plus 1 if the contents of
00255      the low 16 bits, treated as a signed number, is negative.  */
00256   HOWTO (R_PPC_ADDR16_HA,   /* type */
00257         16,                 /* rightshift */
00258         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00259         16,                 /* bitsize */
00260         FALSE,                     /* pc_relative */
00261         0,                  /* bitpos */
00262         complain_overflow_dont, /* complain_on_overflow */
00263         ppc_elf_addr16_ha_reloc, /* special_function */
00264         "R_PPC_ADDR16_HA",  /* name */
00265         FALSE,                     /* partial_inplace */
00266         0,                  /* src_mask */
00267         0xffff,             /* dst_mask */
00268         FALSE),             /* pcrel_offset */
00269 
00270   /* An absolute 16 bit branch; the lower two bits must be zero.
00271      FIXME: we don't check that, we just clear them.  */
00272   HOWTO (R_PPC_ADDR14,             /* type */
00273         0,                  /* rightshift */
00274         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00275         16,                 /* bitsize */
00276         FALSE,                     /* pc_relative */
00277         0,                  /* bitpos */
00278         complain_overflow_bitfield, /* complain_on_overflow */
00279         bfd_elf_generic_reloc,     /* special_function */
00280         "R_PPC_ADDR14",     /* name */
00281         FALSE,                     /* partial_inplace */
00282         0,                  /* src_mask */
00283         0xfffc,             /* dst_mask */
00284         FALSE),             /* pcrel_offset */
00285 
00286   /* An absolute 16 bit branch, for which bit 10 should be set to
00287      indicate that the branch is expected to be taken.  The lower two
00288      bits must be zero.  */
00289   HOWTO (R_PPC_ADDR14_BRTAKEN,     /* type */
00290         0,                  /* rightshift */
00291         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00292         16,                 /* bitsize */
00293         FALSE,                     /* pc_relative */
00294         0,                  /* bitpos */
00295         complain_overflow_bitfield, /* complain_on_overflow */
00296         bfd_elf_generic_reloc,     /* special_function */
00297         "R_PPC_ADDR14_BRTAKEN",/* name */
00298         FALSE,                     /* partial_inplace */
00299         0,                  /* src_mask */
00300         0xfffc,             /* dst_mask */
00301         FALSE),             /* pcrel_offset */
00302 
00303   /* An absolute 16 bit branch, for which bit 10 should be set to
00304      indicate that the branch is not expected to be taken.  The lower
00305      two bits must be zero.  */
00306   HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
00307         0,                  /* rightshift */
00308         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00309         16,                 /* bitsize */
00310         FALSE,                     /* pc_relative */
00311         0,                  /* bitpos */
00312         complain_overflow_bitfield, /* complain_on_overflow */
00313         bfd_elf_generic_reloc,     /* special_function */
00314         "R_PPC_ADDR14_BRNTAKEN",/* name */
00315         FALSE,                     /* partial_inplace */
00316         0,                  /* src_mask */
00317         0xfffc,             /* dst_mask */
00318         FALSE),             /* pcrel_offset */
00319 
00320   /* A relative 26 bit branch; the lower two bits must be zero.  */
00321   HOWTO (R_PPC_REL24,              /* type */
00322         0,                  /* rightshift */
00323         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00324         26,                 /* bitsize */
00325         TRUE,               /* pc_relative */
00326         0,                  /* bitpos */
00327         complain_overflow_signed, /* complain_on_overflow */
00328         bfd_elf_generic_reloc,     /* special_function */
00329         "R_PPC_REL24",             /* name */
00330         FALSE,                     /* partial_inplace */
00331         0,                  /* src_mask */
00332         0x3fffffc,          /* dst_mask */
00333         TRUE),                     /* pcrel_offset */
00334 
00335   /* A relative 16 bit branch; the lower two bits must be zero.  */
00336   HOWTO (R_PPC_REL14,              /* type */
00337         0,                  /* rightshift */
00338         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00339         16,                 /* bitsize */
00340         TRUE,               /* pc_relative */
00341         0,                  /* bitpos */
00342         complain_overflow_signed, /* complain_on_overflow */
00343         bfd_elf_generic_reloc,     /* special_function */
00344         "R_PPC_REL14",             /* name */
00345         FALSE,                     /* partial_inplace */
00346         0,                  /* src_mask */
00347         0xfffc,             /* dst_mask */
00348         TRUE),                     /* pcrel_offset */
00349 
00350   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
00351      the branch is expected to be taken.  The lower two bits must be
00352      zero.  */
00353   HOWTO (R_PPC_REL14_BRTAKEN,      /* type */
00354         0,                  /* rightshift */
00355         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00356         16,                 /* bitsize */
00357         TRUE,               /* pc_relative */
00358         0,                  /* bitpos */
00359         complain_overflow_signed, /* complain_on_overflow */
00360         bfd_elf_generic_reloc,     /* special_function */
00361         "R_PPC_REL14_BRTAKEN",     /* name */
00362         FALSE,                     /* partial_inplace */
00363         0,                  /* src_mask */
00364         0xfffc,             /* dst_mask */
00365         TRUE),                     /* pcrel_offset */
00366 
00367   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
00368      the branch is not expected to be taken.  The lower two bits must
00369      be zero.  */
00370   HOWTO (R_PPC_REL14_BRNTAKEN,     /* type */
00371         0,                  /* rightshift */
00372         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00373         16,                 /* bitsize */
00374         TRUE,               /* pc_relative */
00375         0,                  /* bitpos */
00376         complain_overflow_signed, /* complain_on_overflow */
00377         bfd_elf_generic_reloc,     /* special_function */
00378         "R_PPC_REL14_BRNTAKEN",/* name */
00379         FALSE,                     /* partial_inplace */
00380         0,                  /* src_mask */
00381         0xfffc,             /* dst_mask */
00382         TRUE),                     /* pcrel_offset */
00383 
00384   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
00385      symbol.  */
00386   HOWTO (R_PPC_GOT16,              /* type */
00387         0,                  /* rightshift */
00388         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00389         16,                 /* bitsize */
00390         FALSE,                     /* pc_relative */
00391         0,                  /* bitpos */
00392         complain_overflow_signed, /* complain_on_overflow */
00393         bfd_elf_generic_reloc,     /* special_function */
00394         "R_PPC_GOT16",             /* name */
00395         FALSE,                     /* partial_inplace */
00396         0,                  /* src_mask */
00397         0xffff,             /* dst_mask */
00398         FALSE),             /* pcrel_offset */
00399 
00400   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
00401      the symbol.  */
00402   HOWTO (R_PPC_GOT16_LO,    /* type */
00403         0,                  /* rightshift */
00404         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00405         16,                 /* bitsize */
00406         FALSE,                     /* pc_relative */
00407         0,                  /* bitpos */
00408         complain_overflow_dont, /* complain_on_overflow */
00409         bfd_elf_generic_reloc,     /* special_function */
00410         "R_PPC_GOT16_LO",   /* name */
00411         FALSE,                     /* partial_inplace */
00412         0,                  /* src_mask */
00413         0xffff,             /* dst_mask */
00414         FALSE),             /* pcrel_offset */
00415 
00416   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
00417      the symbol.  */
00418   HOWTO (R_PPC_GOT16_HI,    /* type */
00419         16,                 /* rightshift */
00420         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00421         16,                 /* bitsize */
00422         FALSE,                     /* pc_relative */
00423         0,                  /* bitpos */
00424         complain_overflow_bitfield, /* complain_on_overflow */
00425         bfd_elf_generic_reloc,     /* special_function */
00426         "R_PPC_GOT16_HI",   /* name */
00427         FALSE,                     /* partial_inplace */
00428         0,                  /* src_mask */
00429         0xffff,             /* dst_mask */
00430         FALSE),              /* pcrel_offset */
00431 
00432   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
00433      the symbol.  */
00434   HOWTO (R_PPC_GOT16_HA,    /* type */
00435         16,                 /* rightshift */
00436         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00437         16,                 /* bitsize */
00438         FALSE,                     /* pc_relative */
00439         0,                  /* bitpos */
00440         complain_overflow_bitfield, /* complain_on_overflow */
00441         ppc_elf_addr16_ha_reloc, /* special_function */
00442         "R_PPC_GOT16_HA",   /* name */
00443         FALSE,                     /* partial_inplace */
00444         0,                  /* src_mask */
00445         0xffff,             /* dst_mask */
00446         FALSE),             /* pcrel_offset */
00447 
00448   /* Like R_PPC_REL24, but referring to the procedure linkage table
00449      entry for the symbol.  */
00450   HOWTO (R_PPC_PLTREL24,    /* type */
00451         0,                  /* rightshift */
00452         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00453         26,                 /* bitsize */
00454         TRUE,               /* pc_relative */
00455         0,                  /* bitpos */
00456         complain_overflow_signed,  /* complain_on_overflow */
00457         bfd_elf_generic_reloc,     /* special_function */
00458         "R_PPC_PLTREL24",   /* name */
00459         FALSE,                     /* partial_inplace */
00460         0,                  /* src_mask */
00461         0x3fffffc,          /* dst_mask */
00462         TRUE),                     /* pcrel_offset */
00463 
00464   /* This is used only by the dynamic linker.  The symbol should exist
00465      both in the object being run and in some shared library.  The
00466      dynamic linker copies the data addressed by the symbol from the
00467      shared library into the object, because the object being
00468      run has to have the data at some particular address.  */
00469   HOWTO (R_PPC_COPY,        /* type */
00470         0,                  /* rightshift */
00471         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00472         32,                 /* bitsize */
00473         FALSE,                     /* pc_relative */
00474         0,                  /* bitpos */
00475         complain_overflow_bitfield, /* complain_on_overflow */
00476         bfd_elf_generic_reloc,      /* special_function */
00477         "R_PPC_COPY",              /* name */
00478         FALSE,                     /* partial_inplace */
00479         0,                  /* src_mask */
00480         0,                  /* dst_mask */
00481         FALSE),             /* pcrel_offset */
00482 
00483   /* Like R_PPC_ADDR32, but used when setting global offset table
00484      entries.  */
00485   HOWTO (R_PPC_GLOB_DAT,    /* type */
00486         0,                  /* rightshift */
00487         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00488         32,                 /* bitsize */
00489         FALSE,                     /* pc_relative */
00490         0,                  /* bitpos */
00491         complain_overflow_bitfield, /* complain_on_overflow */
00492         bfd_elf_generic_reloc,      /* special_function */
00493         "R_PPC_GLOB_DAT",   /* name */
00494         FALSE,                     /* partial_inplace */
00495         0,                  /* src_mask */
00496         0xffffffff,         /* dst_mask */
00497         FALSE),             /* pcrel_offset */
00498 
00499   /* Marks a procedure linkage table entry for a symbol.  */
00500   HOWTO (R_PPC_JMP_SLOT,    /* type */
00501         0,                  /* rightshift */
00502         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00503         32,                 /* bitsize */
00504         FALSE,                     /* pc_relative */
00505         0,                  /* bitpos */
00506         complain_overflow_bitfield, /* complain_on_overflow */
00507         bfd_elf_generic_reloc,      /* special_function */
00508         "R_PPC_JMP_SLOT",   /* name */
00509         FALSE,                     /* partial_inplace */
00510         0,                  /* src_mask */
00511         0,                  /* dst_mask */
00512         FALSE),             /* pcrel_offset */
00513 
00514   /* Used only by the dynamic linker.  When the object is run, this
00515      longword is set to the load address of the object, plus the
00516      addend.  */
00517   HOWTO (R_PPC_RELATIVE,    /* type */
00518         0,                  /* rightshift */
00519         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00520         32,                 /* bitsize */
00521         FALSE,                     /* pc_relative */
00522         0,                  /* bitpos */
00523         complain_overflow_bitfield, /* complain_on_overflow */
00524         bfd_elf_generic_reloc,      /* special_function */
00525         "R_PPC_RELATIVE",   /* name */
00526         FALSE,                     /* partial_inplace */
00527         0,                  /* src_mask */
00528         0xffffffff,         /* dst_mask */
00529         FALSE),             /* pcrel_offset */
00530 
00531   /* Like R_PPC_REL24, but uses the value of the symbol within the
00532      object rather than the final value.  Normally used for
00533      _GLOBAL_OFFSET_TABLE_.  */
00534   HOWTO (R_PPC_LOCAL24PC,   /* type */
00535         0,                  /* rightshift */
00536         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00537         26,                 /* bitsize */
00538         TRUE,               /* pc_relative */
00539         0,                  /* bitpos */
00540         complain_overflow_signed, /* complain_on_overflow */
00541         bfd_elf_generic_reloc,     /* special_function */
00542         "R_PPC_LOCAL24PC",  /* name */
00543         FALSE,                     /* partial_inplace */
00544         0,                  /* src_mask */
00545         0x3fffffc,          /* dst_mask */
00546         TRUE),                     /* pcrel_offset */
00547 
00548   /* Like R_PPC_ADDR32, but may be unaligned.  */
00549   HOWTO (R_PPC_UADDR32,            /* type */
00550         0,                  /* rightshift */
00551         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00552         32,                 /* bitsize */
00553         FALSE,                     /* pc_relative */
00554         0,                  /* bitpos */
00555         complain_overflow_bitfield, /* complain_on_overflow */
00556         bfd_elf_generic_reloc,     /* special_function */
00557         "R_PPC_UADDR32",    /* name */
00558         FALSE,                     /* partial_inplace */
00559         0,                  /* src_mask */
00560         0xffffffff,         /* dst_mask */
00561         FALSE),             /* pcrel_offset */
00562 
00563   /* Like R_PPC_ADDR16, but may be unaligned.  */
00564   HOWTO (R_PPC_UADDR16,            /* type */
00565         0,                  /* rightshift */
00566         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00567         16,                 /* bitsize */
00568         FALSE,                     /* pc_relative */
00569         0,                  /* bitpos */
00570         complain_overflow_bitfield, /* complain_on_overflow */
00571         bfd_elf_generic_reloc,     /* special_function */
00572         "R_PPC_UADDR16",    /* name */
00573         FALSE,                     /* partial_inplace */
00574         0,                  /* src_mask */
00575         0xffff,             /* dst_mask */
00576         FALSE),             /* pcrel_offset */
00577 
00578   /* 32-bit PC relative */
00579   HOWTO (R_PPC_REL32,              /* type */
00580         0,                  /* rightshift */
00581         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00582         32,                 /* bitsize */
00583         TRUE,               /* pc_relative */
00584         0,                  /* bitpos */
00585         complain_overflow_bitfield, /* complain_on_overflow */
00586         bfd_elf_generic_reloc,     /* special_function */
00587         "R_PPC_REL32",             /* name */
00588         FALSE,                     /* partial_inplace */
00589         0,                  /* src_mask */
00590         0xffffffff,         /* dst_mask */
00591         TRUE),                     /* pcrel_offset */
00592 
00593   /* 32-bit relocation to the symbol's procedure linkage table.
00594      FIXME: not supported.  */
00595   HOWTO (R_PPC_PLT32,              /* type */
00596         0,                  /* rightshift */
00597         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00598         32,                 /* bitsize */
00599         FALSE,                     /* pc_relative */
00600         0,                  /* bitpos */
00601         complain_overflow_bitfield, /* complain_on_overflow */
00602         bfd_elf_generic_reloc,     /* special_function */
00603         "R_PPC_PLT32",             /* name */
00604         FALSE,                     /* partial_inplace */
00605         0,                  /* src_mask */
00606         0,                  /* dst_mask */
00607         FALSE),             /* pcrel_offset */
00608 
00609   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
00610      FIXME: not supported.  */
00611   HOWTO (R_PPC_PLTREL32,    /* type */
00612         0,                  /* rightshift */
00613         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00614         32,                 /* bitsize */
00615         TRUE,               /* pc_relative */
00616         0,                  /* bitpos */
00617         complain_overflow_bitfield, /* complain_on_overflow */
00618         bfd_elf_generic_reloc,     /* special_function */
00619         "R_PPC_PLTREL32",   /* name */
00620         FALSE,                     /* partial_inplace */
00621         0,                  /* src_mask */
00622         0,                  /* dst_mask */
00623         TRUE),                     /* pcrel_offset */
00624 
00625   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
00626      the symbol.  */
00627   HOWTO (R_PPC_PLT16_LO,    /* type */
00628         0,                  /* rightshift */
00629         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00630         16,                 /* bitsize */
00631         FALSE,                     /* pc_relative */
00632         0,                  /* bitpos */
00633         complain_overflow_dont, /* complain_on_overflow */
00634         bfd_elf_generic_reloc,     /* special_function */
00635         "R_PPC_PLT16_LO",   /* name */
00636         FALSE,                     /* partial_inplace */
00637         0,                  /* src_mask */
00638         0xffff,             /* dst_mask */
00639         FALSE),             /* pcrel_offset */
00640 
00641   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
00642      the symbol.  */
00643   HOWTO (R_PPC_PLT16_HI,    /* type */
00644         16,                 /* rightshift */
00645         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00646         16,                 /* bitsize */
00647         FALSE,                     /* pc_relative */
00648         0,                  /* bitpos */
00649         complain_overflow_bitfield, /* complain_on_overflow */
00650         bfd_elf_generic_reloc,     /* special_function */
00651         "R_PPC_PLT16_HI",   /* name */
00652         FALSE,                     /* partial_inplace */
00653         0,                  /* src_mask */
00654         0xffff,             /* dst_mask */
00655         FALSE),              /* pcrel_offset */
00656 
00657   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
00658      the symbol.  */
00659   HOWTO (R_PPC_PLT16_HA,    /* type */
00660         16,                 /* rightshift */
00661         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00662         16,                 /* bitsize */
00663         FALSE,                     /* pc_relative */
00664         0,                  /* bitpos */
00665         complain_overflow_bitfield, /* complain_on_overflow */
00666         ppc_elf_addr16_ha_reloc, /* special_function */
00667         "R_PPC_PLT16_HA",   /* name */
00668         FALSE,                     /* partial_inplace */
00669         0,                  /* src_mask */
00670         0xffff,             /* dst_mask */
00671         FALSE),             /* pcrel_offset */
00672 
00673   /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
00674      small data items.  */
00675   HOWTO (R_PPC_SDAREL16,    /* type */
00676         0,                  /* rightshift */
00677         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00678         16,                 /* bitsize */
00679         FALSE,                     /* pc_relative */
00680         0,                  /* bitpos */
00681         complain_overflow_signed, /* complain_on_overflow */
00682         bfd_elf_generic_reloc,     /* special_function */
00683         "R_PPC_SDAREL16",   /* name */
00684         FALSE,                     /* partial_inplace */
00685         0,                  /* src_mask */
00686         0xffff,             /* dst_mask */
00687         FALSE),             /* pcrel_offset */
00688 
00689   /* 16-bit section relative relocation.  */
00690   HOWTO (R_PPC_SECTOFF,            /* type */
00691         0,                  /* rightshift */
00692         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00693         16,                 /* bitsize */
00694         FALSE,                     /* pc_relative */
00695         0,                  /* bitpos */
00696         complain_overflow_bitfield, /* complain_on_overflow */
00697         bfd_elf_generic_reloc,     /* special_function */
00698         "R_PPC_SECTOFF",    /* name */
00699         FALSE,                     /* partial_inplace */
00700         0,                  /* src_mask */
00701         0xffff,             /* dst_mask */
00702         FALSE),             /* pcrel_offset */
00703 
00704   /* 16-bit lower half section relative relocation.  */
00705   HOWTO (R_PPC_SECTOFF_LO,    /* type */
00706         0,                  /* rightshift */
00707         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00708         16,                 /* bitsize */
00709         FALSE,                     /* pc_relative */
00710         0,                  /* bitpos */
00711         complain_overflow_dont, /* complain_on_overflow */
00712         bfd_elf_generic_reloc,     /* special_function */
00713         "R_PPC_SECTOFF_LO", /* name */
00714         FALSE,                     /* partial_inplace */
00715         0,                  /* src_mask */
00716         0xffff,             /* dst_mask */
00717         FALSE),             /* pcrel_offset */
00718 
00719   /* 16-bit upper half section relative relocation.  */
00720   HOWTO (R_PPC_SECTOFF_HI,  /* type */
00721         16,                 /* rightshift */
00722         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00723         16,                 /* bitsize */
00724         FALSE,                     /* pc_relative */
00725         0,                  /* bitpos */
00726         complain_overflow_bitfield, /* complain_on_overflow */
00727         bfd_elf_generic_reloc,     /* special_function */
00728         "R_PPC_SECTOFF_HI", /* name */
00729         FALSE,                     /* partial_inplace */
00730         0,                  /* src_mask */
00731         0xffff,             /* dst_mask */
00732         FALSE),              /* pcrel_offset */
00733 
00734   /* 16-bit upper half adjusted section relative relocation.  */
00735   HOWTO (R_PPC_SECTOFF_HA,  /* type */
00736         16,                 /* rightshift */
00737         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00738         16,                 /* bitsize */
00739         FALSE,                     /* pc_relative */
00740         0,                  /* bitpos */
00741         complain_overflow_bitfield, /* complain_on_overflow */
00742         ppc_elf_addr16_ha_reloc, /* special_function */
00743         "R_PPC_SECTOFF_HA", /* name */
00744         FALSE,                     /* partial_inplace */
00745         0,                  /* src_mask */
00746         0xffff,             /* dst_mask */
00747         FALSE),             /* pcrel_offset */
00748 
00749   /* Marker reloc for TLS.  */
00750   HOWTO (R_PPC_TLS,
00751         0,                  /* rightshift */
00752         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00753         32,                 /* bitsize */
00754         FALSE,                     /* pc_relative */
00755         0,                  /* bitpos */
00756         complain_overflow_dont, /* complain_on_overflow */
00757         bfd_elf_generic_reloc,     /* special_function */
00758         "R_PPC_TLS",        /* name */
00759         FALSE,                     /* partial_inplace */
00760         0,                  /* src_mask */
00761         0,                  /* dst_mask */
00762         FALSE),             /* pcrel_offset */
00763 
00764   /* Computes the load module index of the load module that contains the
00765      definition of its TLS sym.  */
00766   HOWTO (R_PPC_DTPMOD32,
00767         0,                  /* rightshift */
00768         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00769         32,                 /* bitsize */
00770         FALSE,                     /* pc_relative */
00771         0,                  /* bitpos */
00772         complain_overflow_dont, /* complain_on_overflow */
00773         ppc_elf_unhandled_reloc, /* special_function */
00774         "R_PPC_DTPMOD32",   /* name */
00775         FALSE,                     /* partial_inplace */
00776         0,                  /* src_mask */
00777         0xffffffff,         /* dst_mask */
00778         FALSE),             /* pcrel_offset */
00779 
00780   /* Computes a dtv-relative displacement, the difference between the value
00781      of sym+add and the base address of the thread-local storage block that
00782      contains the definition of sym, minus 0x8000.  */
00783   HOWTO (R_PPC_DTPREL32,
00784         0,                  /* rightshift */
00785         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00786         32,                 /* bitsize */
00787         FALSE,                     /* pc_relative */
00788         0,                  /* bitpos */
00789         complain_overflow_dont, /* complain_on_overflow */
00790         ppc_elf_unhandled_reloc, /* special_function */
00791         "R_PPC_DTPREL32",   /* name */
00792         FALSE,                     /* partial_inplace */
00793         0,                  /* src_mask */
00794         0xffffffff,         /* dst_mask */
00795         FALSE),             /* pcrel_offset */
00796 
00797   /* A 16 bit dtprel reloc.  */
00798   HOWTO (R_PPC_DTPREL16,
00799         0,                  /* rightshift */
00800         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00801         16,                 /* bitsize */
00802         FALSE,                     /* pc_relative */
00803         0,                  /* bitpos */
00804         complain_overflow_signed, /* complain_on_overflow */
00805         ppc_elf_unhandled_reloc, /* special_function */
00806         "R_PPC_DTPREL16",   /* name */
00807         FALSE,                     /* partial_inplace */
00808         0,                  /* src_mask */
00809         0xffff,             /* dst_mask */
00810         FALSE),             /* pcrel_offset */
00811 
00812   /* Like DTPREL16, but no overflow.  */
00813   HOWTO (R_PPC_DTPREL16_LO,
00814         0,                  /* rightshift */
00815         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00816         16,                 /* bitsize */
00817         FALSE,                     /* pc_relative */
00818         0,                  /* bitpos */
00819         complain_overflow_dont, /* complain_on_overflow */
00820         ppc_elf_unhandled_reloc, /* special_function */
00821         "R_PPC_DTPREL16_LO",       /* name */
00822         FALSE,                     /* partial_inplace */
00823         0,                  /* src_mask */
00824         0xffff,             /* dst_mask */
00825         FALSE),             /* pcrel_offset */
00826 
00827   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
00828   HOWTO (R_PPC_DTPREL16_HI,
00829         16,                 /* rightshift */
00830         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00831         16,                 /* bitsize */
00832         FALSE,                     /* pc_relative */
00833         0,                  /* bitpos */
00834         complain_overflow_dont, /* complain_on_overflow */
00835         ppc_elf_unhandled_reloc, /* special_function */
00836         "R_PPC_DTPREL16_HI",       /* name */
00837         FALSE,                     /* partial_inplace */
00838         0,                  /* src_mask */
00839         0xffff,             /* dst_mask */
00840         FALSE),             /* pcrel_offset */
00841 
00842   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
00843   HOWTO (R_PPC_DTPREL16_HA,
00844         16,                 /* rightshift */
00845         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00846         16,                 /* bitsize */
00847         FALSE,                     /* pc_relative */
00848         0,                  /* bitpos */
00849         complain_overflow_dont, /* complain_on_overflow */
00850         ppc_elf_unhandled_reloc, /* special_function */
00851         "R_PPC_DTPREL16_HA",       /* name */
00852         FALSE,                     /* partial_inplace */
00853         0,                  /* src_mask */
00854         0xffff,             /* dst_mask */
00855         FALSE),             /* pcrel_offset */
00856 
00857   /* Computes a tp-relative displacement, the difference between the value of
00858      sym+add and the value of the thread pointer (r13).  */
00859   HOWTO (R_PPC_TPREL32,
00860         0,                  /* rightshift */
00861         2,                  /* size (0 = byte, 1 = short, 2 = long) */
00862         32,                 /* bitsize */
00863         FALSE,                     /* pc_relative */
00864         0,                  /* bitpos */
00865         complain_overflow_dont, /* complain_on_overflow */
00866         ppc_elf_unhandled_reloc, /* special_function */
00867         "R_PPC_TPREL32",    /* name */
00868         FALSE,                     /* partial_inplace */
00869         0,                  /* src_mask */
00870         0xffffffff,         /* dst_mask */
00871         FALSE),             /* pcrel_offset */
00872 
00873   /* A 16 bit tprel reloc.  */
00874   HOWTO (R_PPC_TPREL16,
00875         0,                  /* rightshift */
00876         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00877         16,                 /* bitsize */
00878         FALSE,                     /* pc_relative */
00879         0,                  /* bitpos */
00880         complain_overflow_signed, /* complain_on_overflow */
00881         ppc_elf_unhandled_reloc, /* special_function */
00882         "R_PPC_TPREL16",    /* name */
00883         FALSE,                     /* partial_inplace */
00884         0,                  /* src_mask */
00885         0xffff,             /* dst_mask */
00886         FALSE),             /* pcrel_offset */
00887 
00888   /* Like TPREL16, but no overflow.  */
00889   HOWTO (R_PPC_TPREL16_LO,
00890         0,                  /* rightshift */
00891         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00892         16,                 /* bitsize */
00893         FALSE,                     /* pc_relative */
00894         0,                  /* bitpos */
00895         complain_overflow_dont, /* complain_on_overflow */
00896         ppc_elf_unhandled_reloc, /* special_function */
00897         "R_PPC_TPREL16_LO", /* name */
00898         FALSE,                     /* partial_inplace */
00899         0,                  /* src_mask */
00900         0xffff,             /* dst_mask */
00901         FALSE),             /* pcrel_offset */
00902 
00903   /* Like TPREL16_LO, but next higher group of 16 bits.  */
00904   HOWTO (R_PPC_TPREL16_HI,
00905         16,                 /* rightshift */
00906         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00907         16,                 /* bitsize */
00908         FALSE,                     /* pc_relative */
00909         0,                  /* bitpos */
00910         complain_overflow_dont, /* complain_on_overflow */
00911         ppc_elf_unhandled_reloc, /* special_function */
00912         "R_PPC_TPREL16_HI", /* name */
00913         FALSE,                     /* partial_inplace */
00914         0,                  /* src_mask */
00915         0xffff,             /* dst_mask */
00916         FALSE),             /* pcrel_offset */
00917 
00918   /* Like TPREL16_HI, but adjust for low 16 bits.  */
00919   HOWTO (R_PPC_TPREL16_HA,
00920         16,                 /* rightshift */
00921         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00922         16,                 /* bitsize */
00923         FALSE,                     /* pc_relative */
00924         0,                  /* bitpos */
00925         complain_overflow_dont, /* complain_on_overflow */
00926         ppc_elf_unhandled_reloc, /* special_function */
00927         "R_PPC_TPREL16_HA", /* name */
00928         FALSE,                     /* partial_inplace */
00929         0,                  /* src_mask */
00930         0xffff,             /* dst_mask */
00931         FALSE),             /* pcrel_offset */
00932 
00933   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
00934      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
00935      to the first entry.  */
00936   HOWTO (R_PPC_GOT_TLSGD16,
00937         0,                  /* rightshift */
00938         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00939         16,                 /* bitsize */
00940         FALSE,                     /* pc_relative */
00941         0,                  /* bitpos */
00942         complain_overflow_signed, /* complain_on_overflow */
00943         ppc_elf_unhandled_reloc, /* special_function */
00944         "R_PPC_GOT_TLSGD16",       /* name */
00945         FALSE,                     /* partial_inplace */
00946         0,                  /* src_mask */
00947         0xffff,             /* dst_mask */
00948         FALSE),             /* pcrel_offset */
00949 
00950   /* Like GOT_TLSGD16, but no overflow.  */
00951   HOWTO (R_PPC_GOT_TLSGD16_LO,
00952         0,                  /* rightshift */
00953         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00954         16,                 /* bitsize */
00955         FALSE,                     /* pc_relative */
00956         0,                  /* bitpos */
00957         complain_overflow_dont, /* complain_on_overflow */
00958         ppc_elf_unhandled_reloc, /* special_function */
00959         "R_PPC_GOT_TLSGD16_LO", /* name */
00960         FALSE,                     /* partial_inplace */
00961         0,                  /* src_mask */
00962         0xffff,             /* dst_mask */
00963         FALSE),             /* pcrel_offset */
00964 
00965   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
00966   HOWTO (R_PPC_GOT_TLSGD16_HI,
00967         16,                 /* rightshift */
00968         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00969         16,                 /* bitsize */
00970         FALSE,                     /* pc_relative */
00971         0,                  /* bitpos */
00972         complain_overflow_dont, /* complain_on_overflow */
00973         ppc_elf_unhandled_reloc, /* special_function */
00974         "R_PPC_GOT_TLSGD16_HI", /* name */
00975         FALSE,                     /* partial_inplace */
00976         0,                  /* src_mask */
00977         0xffff,             /* dst_mask */
00978         FALSE),             /* pcrel_offset */
00979 
00980   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
00981   HOWTO (R_PPC_GOT_TLSGD16_HA,
00982         16,                 /* rightshift */
00983         1,                  /* size (0 = byte, 1 = short, 2 = long) */
00984         16,                 /* bitsize */
00985         FALSE,                     /* pc_relative */
00986         0,                  /* bitpos */
00987         complain_overflow_dont, /* complain_on_overflow */
00988         ppc_elf_unhandled_reloc, /* special_function */
00989         "R_PPC_GOT_TLSGD16_HA", /* name */
00990         FALSE,                     /* partial_inplace */
00991         0,                  /* src_mask */
00992         0xffff,             /* dst_mask */
00993         FALSE),             /* pcrel_offset */
00994 
00995   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
00996      with values (sym+add)@dtpmod and zero, and computes the offset to the
00997      first entry.  */
00998   HOWTO (R_PPC_GOT_TLSLD16,
00999         0,                  /* rightshift */
01000         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01001         16,                 /* bitsize */
01002         FALSE,                     /* pc_relative */
01003         0,                  /* bitpos */
01004         complain_overflow_signed, /* complain_on_overflow */
01005         ppc_elf_unhandled_reloc, /* special_function */
01006         "R_PPC_GOT_TLSLD16",       /* name */
01007         FALSE,                     /* partial_inplace */
01008         0,                  /* src_mask */
01009         0xffff,             /* dst_mask */
01010         FALSE),             /* pcrel_offset */
01011 
01012   /* Like GOT_TLSLD16, but no overflow.  */
01013   HOWTO (R_PPC_GOT_TLSLD16_LO,
01014         0,                  /* rightshift */
01015         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01016         16,                 /* bitsize */
01017         FALSE,                     /* pc_relative */
01018         0,                  /* bitpos */
01019         complain_overflow_dont, /* complain_on_overflow */
01020         ppc_elf_unhandled_reloc, /* special_function */
01021         "R_PPC_GOT_TLSLD16_LO", /* name */
01022         FALSE,                     /* partial_inplace */
01023         0,                  /* src_mask */
01024         0xffff,             /* dst_mask */
01025         FALSE),             /* pcrel_offset */
01026 
01027   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
01028   HOWTO (R_PPC_GOT_TLSLD16_HI,
01029         16,                 /* rightshift */
01030         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01031         16,                 /* bitsize */
01032         FALSE,                     /* pc_relative */
01033         0,                  /* bitpos */
01034         complain_overflow_dont, /* complain_on_overflow */
01035         ppc_elf_unhandled_reloc, /* special_function */
01036         "R_PPC_GOT_TLSLD16_HI", /* name */
01037         FALSE,                     /* partial_inplace */
01038         0,                  /* src_mask */
01039         0xffff,             /* dst_mask */
01040         FALSE),             /* pcrel_offset */
01041 
01042   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
01043   HOWTO (R_PPC_GOT_TLSLD16_HA,
01044         16,                 /* rightshift */
01045         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01046         16,                 /* bitsize */
01047         FALSE,                     /* pc_relative */
01048         0,                  /* bitpos */
01049         complain_overflow_dont, /* complain_on_overflow */
01050         ppc_elf_unhandled_reloc, /* special_function */
01051         "R_PPC_GOT_TLSLD16_HA", /* name */
01052         FALSE,                     /* partial_inplace */
01053         0,                  /* src_mask */
01054         0xffff,             /* dst_mask */
01055         FALSE),             /* pcrel_offset */
01056 
01057   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
01058      the offset to the entry.  */
01059   HOWTO (R_PPC_GOT_DTPREL16,
01060         0,                  /* rightshift */
01061         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01062         16,                 /* bitsize */
01063         FALSE,                     /* pc_relative */
01064         0,                  /* bitpos */
01065         complain_overflow_signed, /* complain_on_overflow */
01066         ppc_elf_unhandled_reloc, /* special_function */
01067         "R_PPC_GOT_DTPREL16",      /* name */
01068         FALSE,                     /* partial_inplace */
01069         0,                  /* src_mask */
01070         0xffff,             /* dst_mask */
01071         FALSE),             /* pcrel_offset */
01072 
01073   /* Like GOT_DTPREL16, but no overflow.  */
01074   HOWTO (R_PPC_GOT_DTPREL16_LO,
01075         0,                  /* rightshift */
01076         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01077         16,                 /* bitsize */
01078         FALSE,                     /* pc_relative */
01079         0,                  /* bitpos */
01080         complain_overflow_dont, /* complain_on_overflow */
01081         ppc_elf_unhandled_reloc, /* special_function */
01082         "R_PPC_GOT_DTPREL16_LO", /* name */
01083         FALSE,                     /* partial_inplace */
01084         0,                  /* src_mask */
01085         0xffff,             /* dst_mask */
01086         FALSE),             /* pcrel_offset */
01087 
01088   /* Like GOT_DTPREL16_LO, but next higher group of 16 bits.  */
01089   HOWTO (R_PPC_GOT_DTPREL16_HI,
01090         16,                 /* rightshift */
01091         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01092         16,                 /* bitsize */
01093         FALSE,                     /* pc_relative */
01094         0,                  /* bitpos */
01095         complain_overflow_dont, /* complain_on_overflow */
01096         ppc_elf_unhandled_reloc, /* special_function */
01097         "R_PPC_GOT_DTPREL16_HI", /* name */
01098         FALSE,                     /* partial_inplace */
01099         0,                  /* src_mask */
01100         0xffff,             /* dst_mask */
01101         FALSE),             /* pcrel_offset */
01102 
01103   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
01104   HOWTO (R_PPC_GOT_DTPREL16_HA,
01105         16,                 /* rightshift */
01106         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01107         16,                 /* bitsize */
01108         FALSE,                     /* pc_relative */
01109         0,                  /* bitpos */
01110         complain_overflow_dont, /* complain_on_overflow */
01111         ppc_elf_unhandled_reloc, /* special_function */
01112         "R_PPC_GOT_DTPREL16_HA", /* name */
01113         FALSE,                     /* partial_inplace */
01114         0,                  /* src_mask */
01115         0xffff,             /* dst_mask */
01116         FALSE),             /* pcrel_offset */
01117 
01118   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
01119      offset to the entry.  */
01120   HOWTO (R_PPC_GOT_TPREL16,
01121         0,                  /* rightshift */
01122         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01123         16,                 /* bitsize */
01124         FALSE,                     /* pc_relative */
01125         0,                  /* bitpos */
01126         complain_overflow_signed, /* complain_on_overflow */
01127         ppc_elf_unhandled_reloc, /* special_function */
01128         "R_PPC_GOT_TPREL16",       /* name */
01129         FALSE,                     /* partial_inplace */
01130         0,                  /* src_mask */
01131         0xffff,             /* dst_mask */
01132         FALSE),             /* pcrel_offset */
01133 
01134   /* Like GOT_TPREL16, but no overflow.  */
01135   HOWTO (R_PPC_GOT_TPREL16_LO,
01136         0,                  /* rightshift */
01137         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01138         16,                 /* bitsize */
01139         FALSE,                     /* pc_relative */
01140         0,                  /* bitpos */
01141         complain_overflow_dont, /* complain_on_overflow */
01142         ppc_elf_unhandled_reloc, /* special_function */
01143         "R_PPC_GOT_TPREL16_LO", /* name */
01144         FALSE,                     /* partial_inplace */
01145         0,                  /* src_mask */
01146         0xffff,             /* dst_mask */
01147         FALSE),             /* pcrel_offset */
01148 
01149   /* Like GOT_TPREL16_LO, but next higher group of 16 bits.  */
01150   HOWTO (R_PPC_GOT_TPREL16_HI,
01151         16,                 /* rightshift */
01152         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01153         16,                 /* bitsize */
01154         FALSE,                     /* pc_relative */
01155         0,                  /* bitpos */
01156         complain_overflow_dont, /* complain_on_overflow */
01157         ppc_elf_unhandled_reloc, /* special_function */
01158         "R_PPC_GOT_TPREL16_HI", /* name */
01159         FALSE,                     /* partial_inplace */
01160         0,                  /* src_mask */
01161         0xffff,             /* dst_mask */
01162         FALSE),             /* pcrel_offset */
01163 
01164   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
01165   HOWTO (R_PPC_GOT_TPREL16_HA,
01166         16,                 /* rightshift */
01167         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01168         16,                 /* bitsize */
01169         FALSE,                     /* pc_relative */
01170         0,                  /* bitpos */
01171         complain_overflow_dont, /* complain_on_overflow */
01172         ppc_elf_unhandled_reloc, /* special_function */
01173         "R_PPC_GOT_TPREL16_HA", /* name */
01174         FALSE,                     /* partial_inplace */
01175         0,                  /* src_mask */
01176         0xffff,             /* dst_mask */
01177         FALSE),             /* pcrel_offset */
01178 
01179   /* The remaining relocs are from the Embedded ELF ABI, and are not
01180      in the SVR4 ELF ABI.  */
01181 
01182   /* 32 bit value resulting from the addend minus the symbol.  */
01183   HOWTO (R_PPC_EMB_NADDR32, /* type */
01184         0,                  /* rightshift */
01185         2,                  /* size (0 = byte, 1 = short, 2 = long) */
01186         32,                 /* bitsize */
01187         FALSE,                     /* pc_relative */
01188         0,                  /* bitpos */
01189         complain_overflow_bitfield, /* complain_on_overflow */
01190         bfd_elf_generic_reloc,     /* special_function */
01191         "R_PPC_EMB_NADDR32",       /* name */
01192         FALSE,                     /* partial_inplace */
01193         0,                  /* src_mask */
01194         0xffffffff,         /* dst_mask */
01195         FALSE),             /* pcrel_offset */
01196 
01197   /* 16 bit value resulting from the addend minus the symbol.  */
01198   HOWTO (R_PPC_EMB_NADDR16, /* type */
01199         0,                  /* rightshift */
01200         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01201         16,                 /* bitsize */
01202         FALSE,                     /* pc_relative */
01203         0,                  /* bitpos */
01204         complain_overflow_bitfield, /* complain_on_overflow */
01205         bfd_elf_generic_reloc,     /* special_function */
01206         "R_PPC_EMB_NADDR16",       /* name */
01207         FALSE,                     /* partial_inplace */
01208         0,                  /* src_mask */
01209         0xffff,             /* dst_mask */
01210         FALSE),             /* pcrel_offset */
01211 
01212   /* 16 bit value resulting from the addend minus the symbol.  */
01213   HOWTO (R_PPC_EMB_NADDR16_LO,     /* type */
01214         0,                  /* rightshift */
01215         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01216         16,                 /* bitsize */
01217         FALSE,                     /* pc_relative */
01218         0,                  /* bitpos */
01219         complain_overflow_dont,/* complain_on_overflow */
01220         bfd_elf_generic_reloc,     /* special_function */
01221         "R_PPC_EMB_ADDR16_LO",     /* name */
01222         FALSE,                     /* partial_inplace */
01223         0,                  /* src_mask */
01224         0xffff,             /* dst_mask */
01225         FALSE),             /* pcrel_offset */
01226 
01227   /* The high order 16 bits of the addend minus the symbol.  */
01228   HOWTO (R_PPC_EMB_NADDR16_HI,     /* type */
01229         16,                 /* rightshift */
01230         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01231         16,                 /* bitsize */
01232         FALSE,                     /* pc_relative */
01233         0,                  /* bitpos */
01234         complain_overflow_dont, /* complain_on_overflow */
01235         bfd_elf_generic_reloc,     /* special_function */
01236         "R_PPC_EMB_NADDR16_HI", /* name */
01237         FALSE,                     /* partial_inplace */
01238         0,                  /* src_mask */
01239         0xffff,             /* dst_mask */
01240         FALSE),             /* pcrel_offset */
01241 
01242   /* The high order 16 bits of the result of the addend minus the address,
01243      plus 1 if the contents of the low 16 bits, treated as a signed number,
01244      is negative.  */
01245   HOWTO (R_PPC_EMB_NADDR16_HA,     /* type */
01246         16,                 /* rightshift */
01247         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01248         16,                 /* bitsize */
01249         FALSE,                     /* pc_relative */
01250         0,                  /* bitpos */
01251         complain_overflow_dont, /* complain_on_overflow */
01252         ppc_elf_addr16_ha_reloc, /* special_function */
01253         "R_PPC_EMB_NADDR16_HA", /* name */
01254         FALSE,                     /* partial_inplace */
01255         0,                  /* src_mask */
01256         0xffff,             /* dst_mask */
01257         FALSE),             /* pcrel_offset */
01258 
01259   /* 16 bit value resulting from allocating a 4 byte word to hold an
01260      address in the .sdata section, and returning the offset from
01261      _SDA_BASE_ for that relocation.  */
01262   HOWTO (R_PPC_EMB_SDAI16,  /* type */
01263         0,                  /* rightshift */
01264         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01265         16,                 /* bitsize */
01266         FALSE,                     /* pc_relative */
01267         0,                  /* bitpos */
01268         complain_overflow_bitfield, /* complain_on_overflow */
01269         bfd_elf_generic_reloc,     /* special_function */
01270         "R_PPC_EMB_SDAI16", /* name */
01271         FALSE,                     /* partial_inplace */
01272         0,                  /* src_mask */
01273         0xffff,             /* dst_mask */
01274         FALSE),             /* pcrel_offset */
01275 
01276   /* 16 bit value resulting from allocating a 4 byte word to hold an
01277      address in the .sdata2 section, and returning the offset from
01278      _SDA2_BASE_ for that relocation.  */
01279   HOWTO (R_PPC_EMB_SDA2I16, /* type */
01280         0,                  /* rightshift */
01281         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01282         16,                 /* bitsize */
01283         FALSE,                     /* pc_relative */
01284         0,                  /* bitpos */
01285         complain_overflow_bitfield, /* complain_on_overflow */
01286         bfd_elf_generic_reloc,     /* special_function */
01287         "R_PPC_EMB_SDA2I16",       /* name */
01288         FALSE,                     /* partial_inplace */
01289         0,                  /* src_mask */
01290         0xffff,             /* dst_mask */
01291         FALSE),             /* pcrel_offset */
01292 
01293   /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
01294      small data items.       */
01295   HOWTO (R_PPC_EMB_SDA2REL, /* type */
01296         0,                  /* rightshift */
01297         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01298         16,                 /* bitsize */
01299         FALSE,                     /* pc_relative */
01300         0,                  /* bitpos */
01301         complain_overflow_signed, /* complain_on_overflow */
01302         bfd_elf_generic_reloc,     /* special_function */
01303         "R_PPC_EMB_SDA2REL",       /* name */
01304         FALSE,                     /* partial_inplace */
01305         0,                  /* src_mask */
01306         0xffff,             /* dst_mask */
01307         FALSE),             /* pcrel_offset */
01308 
01309   /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
01310      signed offset from the appropriate base, and filling in the register
01311      field with the appropriate register (0, 2, or 13).  */
01312   HOWTO (R_PPC_EMB_SDA21,   /* type */
01313         0,                  /* rightshift */
01314         2,                  /* size (0 = byte, 1 = short, 2 = long) */
01315         16,                 /* bitsize */
01316         FALSE,                     /* pc_relative */
01317         0,                  /* bitpos */
01318         complain_overflow_signed, /* complain_on_overflow */
01319         bfd_elf_generic_reloc,     /* special_function */
01320         "R_PPC_EMB_SDA21",  /* name */
01321         FALSE,                     /* partial_inplace */
01322         0,                  /* src_mask */
01323         0xffff,             /* dst_mask */
01324         FALSE),             /* pcrel_offset */
01325 
01326   /* Relocation not handled: R_PPC_EMB_MRKREF */
01327   /* Relocation not handled: R_PPC_EMB_RELSEC16 */
01328   /* Relocation not handled: R_PPC_EMB_RELST_LO */
01329   /* Relocation not handled: R_PPC_EMB_RELST_HI */
01330   /* Relocation not handled: R_PPC_EMB_RELST_HA */
01331   /* Relocation not handled: R_PPC_EMB_BIT_FLD */
01332 
01333   /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
01334      in the 16 bit signed offset from the appropriate base, and filling in the
01335      register field with the appropriate register (0, 2, or 13).  */
01336   HOWTO (R_PPC_EMB_RELSDA,  /* type */
01337         0,                  /* rightshift */
01338         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01339         16,                 /* bitsize */
01340         TRUE,               /* pc_relative */
01341         0,                  /* bitpos */
01342         complain_overflow_signed, /* complain_on_overflow */
01343         bfd_elf_generic_reloc,     /* special_function */
01344         "R_PPC_EMB_RELSDA", /* name */
01345         FALSE,                     /* partial_inplace */
01346         0,                  /* src_mask */
01347         0xffff,             /* dst_mask */
01348         FALSE),             /* pcrel_offset */
01349 
01350   /* A 16 bit relative relocation.  */
01351   HOWTO (R_PPC_REL16,              /* type */
01352         0,                  /* rightshift */
01353         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01354         16,                 /* bitsize */
01355         TRUE,               /* pc_relative */
01356         0,                  /* bitpos */
01357         complain_overflow_bitfield, /* complain_on_overflow */
01358         bfd_elf_generic_reloc,     /* special_function */
01359         "R_PPC_REL16",             /* name */
01360         FALSE,                     /* partial_inplace */
01361         0,                  /* src_mask */
01362         0xffff,             /* dst_mask */
01363         TRUE),                     /* pcrel_offset */
01364 
01365   /* A 16 bit relative relocation without overflow.  */
01366   HOWTO (R_PPC_REL16_LO,    /* type */
01367         0,                  /* rightshift */
01368         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01369         16,                 /* bitsize */
01370         TRUE,               /* pc_relative */
01371         0,                  /* bitpos */
01372         complain_overflow_dont,/* complain_on_overflow */
01373         bfd_elf_generic_reloc,     /* special_function */
01374         "R_PPC_REL16_LO",   /* name */
01375         FALSE,                     /* partial_inplace */
01376         0,                  /* src_mask */
01377         0xffff,             /* dst_mask */
01378         TRUE),                     /* pcrel_offset */
01379 
01380   /* The high order 16 bits of a relative address.  */
01381   HOWTO (R_PPC_REL16_HI,    /* type */
01382         16,                 /* rightshift */
01383         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01384         16,                 /* bitsize */
01385         TRUE,               /* pc_relative */
01386         0,                  /* bitpos */
01387         complain_overflow_dont, /* complain_on_overflow */
01388         bfd_elf_generic_reloc,     /* special_function */
01389         "R_PPC_REL16_HI",   /* name */
01390         FALSE,                     /* partial_inplace */
01391         0,                  /* src_mask */
01392         0xffff,             /* dst_mask */
01393         TRUE),                     /* pcrel_offset */
01394 
01395   /* The high order 16 bits of a relative address, plus 1 if the contents of
01396      the low 16 bits, treated as a signed number, is negative.  */
01397   HOWTO (R_PPC_REL16_HA,    /* type */
01398         16,                 /* rightshift */
01399         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01400         16,                 /* bitsize */
01401         TRUE,               /* pc_relative */
01402         0,                  /* bitpos */
01403         complain_overflow_dont, /* complain_on_overflow */
01404         ppc_elf_addr16_ha_reloc, /* special_function */
01405         "R_PPC_REL16_HA",   /* name */
01406         FALSE,                     /* partial_inplace */
01407         0,                  /* src_mask */
01408         0xffff,             /* dst_mask */
01409         TRUE),                     /* pcrel_offset */
01410 
01411   /* GNU extension to record C++ vtable hierarchy.  */
01412   HOWTO (R_PPC_GNU_VTINHERIT,      /* type */
01413         0,                  /* rightshift */
01414         0,                  /* size (0 = byte, 1 = short, 2 = long) */
01415         0,                  /* bitsize */
01416         FALSE,                     /* pc_relative */
01417         0,                  /* bitpos */
01418         complain_overflow_dont, /* complain_on_overflow */
01419         NULL,               /* special_function */
01420         "R_PPC_GNU_VTINHERIT",     /* name */
01421         FALSE,                     /* partial_inplace */
01422         0,                  /* src_mask */
01423         0,                  /* dst_mask */
01424         FALSE),             /* pcrel_offset */
01425 
01426   /* GNU extension to record C++ vtable member usage.  */
01427   HOWTO (R_PPC_GNU_VTENTRY, /* type */
01428         0,                  /* rightshift */
01429         0,                  /* size (0 = byte, 1 = short, 2 = long) */
01430         0,                  /* bitsize */
01431         FALSE,                     /* pc_relative */
01432         0,                  /* bitpos */
01433         complain_overflow_dont, /* complain_on_overflow */
01434         NULL,               /* special_function */
01435         "R_PPC_GNU_VTENTRY",       /* name */
01436         FALSE,                     /* partial_inplace */
01437         0,                  /* src_mask */
01438         0,                  /* dst_mask */
01439         FALSE),             /* pcrel_offset */
01440 
01441   /* Phony reloc to handle AIX style TOC entries.  */
01442   HOWTO (R_PPC_TOC16,              /* type */
01443         0,                  /* rightshift */
01444         1,                  /* size (0 = byte, 1 = short, 2 = long) */
01445         16,                 /* bitsize */
01446         FALSE,                     /* pc_relative */
01447         0,                  /* bitpos */
01448         complain_overflow_signed, /* complain_on_overflow */
01449         bfd_elf_generic_reloc,     /* special_function */
01450         "R_PPC_TOC16",             /* name */
01451         FALSE,                     /* partial_inplace */
01452         0,                  /* src_mask */
01453         0xffff,             /* dst_mask */
01454         FALSE),             /* pcrel_offset */
01455 };
01456 
01457 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
01458 
01459 static void
01460 ppc_elf_howto_init (void)
01461 {
01462   unsigned int i, type;
01463 
01464   for (i = 0;
01465        i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
01466        i++)
01467     {
01468       type = ppc_elf_howto_raw[i].type;
01469       if (type >= (sizeof (ppc_elf_howto_table)
01470                  / sizeof (ppc_elf_howto_table[0])))
01471        abort ();
01472       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
01473     }
01474 }
01475 
01476 static reloc_howto_type *
01477 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
01478                         bfd_reloc_code_real_type code)
01479 {
01480   enum elf_ppc_reloc_type r;
01481 
01482   /* Initialize howto table if not already done.  */
01483   if (!ppc_elf_howto_table[R_PPC_ADDR32])
01484     ppc_elf_howto_init ();
01485 
01486   switch (code)
01487     {
01488     default:
01489       return NULL;
01490 
01491     case BFD_RELOC_NONE:           r = R_PPC_NONE;                    break;
01492     case BFD_RELOC_32:                    r = R_PPC_ADDR32;           break;
01493     case BFD_RELOC_PPC_BA26:              r = R_PPC_ADDR24;           break;
01494     case BFD_RELOC_16:                    r = R_PPC_ADDR16;           break;
01495     case BFD_RELOC_LO16:           r = R_PPC_ADDR16_LO;        break;
01496     case BFD_RELOC_HI16:           r = R_PPC_ADDR16_HI;        break;
01497     case BFD_RELOC_HI16_S:         r = R_PPC_ADDR16_HA;        break;
01498     case BFD_RELOC_PPC_BA16:              r = R_PPC_ADDR14;           break;
01499     case BFD_RELOC_PPC_BA16_BRTAKEN:      r = R_PPC_ADDR14_BRTAKEN;   break;
01500     case BFD_RELOC_PPC_BA16_BRNTAKEN:     r = R_PPC_ADDR14_BRNTAKEN;  break;
01501     case BFD_RELOC_PPC_B26:        r = R_PPC_REL24;            break;
01502     case BFD_RELOC_PPC_B16:        r = R_PPC_REL14;            break;
01503     case BFD_RELOC_PPC_B16_BRTAKEN:       r = R_PPC_REL14_BRTAKEN;    break;
01504     case BFD_RELOC_PPC_B16_BRNTAKEN:      r = R_PPC_REL14_BRNTAKEN;   break;
01505     case BFD_RELOC_16_GOTOFF:             r = R_PPC_GOT16;            break;
01506     case BFD_RELOC_LO16_GOTOFF:           r = R_PPC_GOT16_LO;         break;
01507     case BFD_RELOC_HI16_GOTOFF:           r = R_PPC_GOT16_HI;         break;
01508     case BFD_RELOC_HI16_S_GOTOFF:  r = R_PPC_GOT16_HA;         break;
01509     case BFD_RELOC_24_PLT_PCREL:   r = R_PPC_PLTREL24;         break;
01510     case BFD_RELOC_PPC_COPY:              r = R_PPC_COPY;                    break;
01511     case BFD_RELOC_PPC_GLOB_DAT:   r = R_PPC_GLOB_DAT;         break;
01512     case BFD_RELOC_PPC_LOCAL24PC:  r = R_PPC_LOCAL24PC;        break;
01513     case BFD_RELOC_32_PCREL:              r = R_PPC_REL32;            break;
01514     case BFD_RELOC_32_PLTOFF:             r = R_PPC_PLT32;            break;
01515     case BFD_RELOC_32_PLT_PCREL:   r = R_PPC_PLTREL32;         break;
01516     case BFD_RELOC_LO16_PLTOFF:           r = R_PPC_PLT16_LO;         break;
01517     case BFD_RELOC_HI16_PLTOFF:           r = R_PPC_PLT16_HI;         break;
01518     case BFD_RELOC_HI16_S_PLTOFF:  r = R_PPC_PLT16_HA;         break;
01519     case BFD_RELOC_GPREL16:        r = R_PPC_SDAREL16;         break;
01520     case BFD_RELOC_16_BASEREL:            r = R_PPC_SECTOFF;          break;
01521     case BFD_RELOC_LO16_BASEREL:   r = R_PPC_SECTOFF_LO;              break;
01522     case BFD_RELOC_HI16_BASEREL:   r = R_PPC_SECTOFF_HI;              break;
01523     case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA;              break;
01524     case BFD_RELOC_CTOR:           r = R_PPC_ADDR32;           break;
01525     case BFD_RELOC_PPC_TOC16:             r = R_PPC_TOC16;            break;
01526     case BFD_RELOC_PPC_TLS:        r = R_PPC_TLS;                     break;
01527     case BFD_RELOC_PPC_DTPMOD:            r = R_PPC_DTPMOD32;         break;
01528     case BFD_RELOC_PPC_TPREL16:           r = R_PPC_TPREL16;          break;
01529     case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO;              break;
01530     case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI;              break;
01531     case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA;              break;
01532     case BFD_RELOC_PPC_TPREL:             r = R_PPC_TPREL32;          break;
01533     case BFD_RELOC_PPC_DTPREL16:   r = R_PPC_DTPREL16;         break;
01534     case BFD_RELOC_PPC_DTPREL16_LO:       r = R_PPC_DTPREL16_LO;             break;
01535     case BFD_RELOC_PPC_DTPREL16_HI:       r = R_PPC_DTPREL16_HI;             break;
01536     case BFD_RELOC_PPC_DTPREL16_HA:       r = R_PPC_DTPREL16_HA;             break;
01537     case BFD_RELOC_PPC_DTPREL:            r = R_PPC_DTPREL32;         break;
01538     case BFD_RELOC_PPC_GOT_TLSGD16:       r = R_PPC_GOT_TLSGD16;             break;
01539     case BFD_RELOC_PPC_GOT_TLSGD16_LO:    r = R_PPC_GOT_TLSGD16_LO;   break;
01540     case BFD_RELOC_PPC_GOT_TLSGD16_HI:    r = R_PPC_GOT_TLSGD16_HI;   break;
01541     case BFD_RELOC_PPC_GOT_TLSGD16_HA:    r = R_PPC_GOT_TLSGD16_HA;   break;
01542     case BFD_RELOC_PPC_GOT_TLSLD16:       r = R_PPC_GOT_TLSLD16;             break;
01543     case BFD_RELOC_PPC_GOT_TLSLD16_LO:    r = R_PPC_GOT_TLSLD16_LO;   break;
01544     case BFD_RELOC_PPC_GOT_TLSLD16_HI:    r = R_PPC_GOT_TLSLD16_HI;   break;
01545     case BFD_RELOC_PPC_GOT_TLSLD16_HA:    r = R_PPC_GOT_TLSLD16_HA;   break;
01546     case BFD_RELOC_PPC_GOT_TPREL16:       r = R_PPC_GOT_TPREL16;             break;
01547     case BFD_RELOC_PPC_GOT_TPREL16_LO:    r = R_PPC_GOT_TPREL16_LO;   break;
01548     case BFD_RELOC_PPC_GOT_TPREL16_HI:    r = R_PPC_GOT_TPREL16_HI;   break;
01549     case BFD_RELOC_PPC_GOT_TPREL16_HA:    r = R_PPC_GOT_TPREL16_HA;   break;
01550     case BFD_RELOC_PPC_GOT_DTPREL16:      r = R_PPC_GOT_DTPREL16;            break;
01551     case BFD_RELOC_PPC_GOT_DTPREL16_LO:   r = R_PPC_GOT_DTPREL16_LO;  break;
01552     case BFD_RELOC_PPC_GOT_DTPREL16_HI:   r = R_PPC_GOT_DTPREL16_HI;  break;
01553     case BFD_RELOC_PPC_GOT_DTPREL16_HA:   r = R_PPC_GOT_DTPREL16_HA;  break;
01554     case BFD_RELOC_PPC_EMB_NADDR32:       r = R_PPC_EMB_NADDR32;             break;
01555     case BFD_RELOC_PPC_EMB_NADDR16:       r = R_PPC_EMB_NADDR16;             break;
01556     case BFD_RELOC_PPC_EMB_NADDR16_LO:    r = R_PPC_EMB_NADDR16_LO;   break;
01557     case BFD_RELOC_PPC_EMB_NADDR16_HI:    r = R_PPC_EMB_NADDR16_HI;   break;
01558     case BFD_RELOC_PPC_EMB_NADDR16_HA:    r = R_PPC_EMB_NADDR16_HA;   break;
01559     case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16;              break;
01560     case BFD_RELOC_PPC_EMB_SDA2I16:       r = R_PPC_EMB_SDA2I16;             break;
01561     case BFD_RELOC_PPC_EMB_SDA2REL:       r = R_PPC_EMB_SDA2REL;             break;
01562     case BFD_RELOC_PPC_EMB_SDA21:  r = R_PPC_EMB_SDA21;        break;
01563     case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF;              break;
01564     case BFD_RELOC_PPC_EMB_RELSEC16:      r = R_PPC_EMB_RELSEC16;            break;
01565     case BFD_RELOC_PPC_EMB_RELST_LO:      r = R_PPC_EMB_RELST_LO;            break;
01566     case BFD_RELOC_PPC_EMB_RELST_HI:      r = R_PPC_EMB_RELST_HI;            break;
01567     case BFD_RELOC_PPC_EMB_RELST_HA:      r = R_PPC_EMB_RELST_HA;            break;
01568     case BFD_RELOC_PPC_EMB_BIT_FLD:       r = R_PPC_EMB_BIT_FLD;             break;
01569     case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA;              break;
01570     case BFD_RELOC_16_PCREL:              r = R_PPC_REL16;            break;
01571     case BFD_RELOC_LO16_PCREL:            r = R_PPC_REL16_LO;         break;
01572     case BFD_RELOC_HI16_PCREL:            r = R_PPC_REL16_HI;         break;
01573     case BFD_RELOC_HI16_S_PCREL:   r = R_PPC_REL16_HA;         break;
01574     case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT;    break;
01575     case BFD_RELOC_VTABLE_ENTRY:   r = R_PPC_GNU_VTENTRY;             break;
01576     }
01577 
01578   return ppc_elf_howto_table[r];
01579 };
01580 
01581 static reloc_howto_type *
01582 ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
01583                         const char *r_name)
01584 {
01585   unsigned int i;
01586 
01587   for (i = 0;
01588        i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
01589        i++)
01590     if (ppc_elf_howto_raw[i].name != NULL
01591        && strcasecmp (ppc_elf_howto_raw[i].name, r_name) == 0)
01592       return &ppc_elf_howto_raw[i];
01593 
01594   return NULL;
01595 }
01596 
01597 /* Set the howto pointer for a PowerPC ELF reloc.  */
01598 
01599 static void
01600 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
01601                      arelent *cache_ptr,
01602                      Elf_Internal_Rela *dst)
01603 {
01604   /* Initialize howto table if not already done.  */
01605   if (!ppc_elf_howto_table[R_PPC_ADDR32])
01606     ppc_elf_howto_init ();
01607 
01608   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
01609   cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
01610 }
01611 
01612 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs.  */
01613 
01614 static bfd_reloc_status_type
01615 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
01616                       arelent *reloc_entry,
01617                       asymbol *symbol,
01618                       void *data ATTRIBUTE_UNUSED,
01619                       asection *input_section,
01620                       bfd *output_bfd,
01621                       char **error_message ATTRIBUTE_UNUSED)
01622 {
01623   bfd_vma relocation;
01624 
01625   if (output_bfd != NULL)
01626     {
01627       reloc_entry->address += input_section->output_offset;
01628       return bfd_reloc_ok;
01629     }
01630 
01631   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
01632     return bfd_reloc_outofrange;
01633 
01634   if (bfd_is_com_section (symbol->section))
01635     relocation = 0;
01636   else
01637     relocation = symbol->value;
01638 
01639   relocation += symbol->section->output_section->vma;
01640   relocation += symbol->section->output_offset;
01641   relocation += reloc_entry->addend;
01642   if (reloc_entry->howto->pc_relative)
01643     relocation -= reloc_entry->address;
01644 
01645   reloc_entry->addend += (relocation & 0x8000) << 1;
01646 
01647   return bfd_reloc_continue;
01648 }
01649 
01650 static bfd_reloc_status_type
01651 ppc_elf_unhandled_reloc (bfd *abfd,
01652                       arelent *reloc_entry,
01653                       asymbol *symbol,
01654                       void *data,
01655                       asection *input_section,
01656                       bfd *output_bfd,
01657                       char **error_message)
01658 {
01659   /* If this is a relocatable link (output_bfd test tells us), just
01660      call the generic function.  Any adjustment will be done at final
01661      link time.  */
01662   if (output_bfd != NULL)
01663     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
01664                               input_section, output_bfd, error_message);
01665 
01666   if (error_message != NULL)
01667     {
01668       static char buf[60];
01669       sprintf (buf, _("generic linker can't handle %s"),
01670               reloc_entry->howto->name);
01671       *error_message = buf;
01672     }
01673   return bfd_reloc_dangerous;
01674 }
01675 
01676 /* Sections created by the linker.  */
01677 
01678 typedef struct elf_linker_section
01679 {
01680   /* Pointer to the bfd section.  */
01681   asection *section;
01682   /* Section name.  */
01683   const char *name;
01684   /* Associated bss section name.  */
01685   const char *bss_name;
01686   /* Associated symbol name.  */
01687   const char *sym_name;
01688   /* Associated symbol.  */
01689   struct elf_link_hash_entry *sym;
01690 } elf_linker_section_t;
01691 
01692 /* Linked list of allocated pointer entries.  This hangs off of the
01693    symbol lists, and provides allows us to return different pointers,
01694    based on different addend's.  */
01695 
01696 typedef struct elf_linker_section_pointers
01697 {
01698   /* next allocated pointer for this symbol */
01699   struct elf_linker_section_pointers *next;
01700   /* offset of pointer from beginning of section */
01701   bfd_vma offset;
01702   /* addend used */
01703   bfd_vma addend;
01704   /* which linker section this is */
01705   elf_linker_section_t *lsect;
01706 } elf_linker_section_pointers_t;
01707 
01708 struct ppc_elf_obj_tdata
01709 {
01710   struct elf_obj_tdata elf;
01711 
01712   /* A mapping from local symbols to offsets into the various linker
01713      sections added.  This is index by the symbol index.  */
01714   elf_linker_section_pointers_t **linker_section_pointers;
01715 };
01716 
01717 #define ppc_elf_tdata(bfd) \
01718   ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
01719 
01720 #define elf_local_ptr_offsets(bfd) \
01721   (ppc_elf_tdata (bfd)->linker_section_pointers)
01722 
01723 /* Override the generic function because we store some extras.  */
01724 
01725 static bfd_boolean
01726 ppc_elf_mkobject (bfd *abfd)
01727 {
01728   if (abfd->tdata.any == NULL)
01729     {
01730       bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
01731       abfd->tdata.any = bfd_zalloc (abfd, amt);
01732       if (abfd->tdata.any == NULL)
01733        return FALSE;
01734     }
01735   return bfd_elf_mkobject (abfd);
01736 }
01737 
01738 /* Fix bad default arch selected for a 32 bit input bfd when the
01739    default is 64 bit.  */
01740 
01741 static bfd_boolean
01742 ppc_elf_object_p (bfd *abfd)
01743 {
01744   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
01745     {
01746       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
01747 
01748       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
01749        {
01750          /* Relies on arch after 64 bit default being 32 bit default.  */
01751          abfd->arch_info = abfd->arch_info->next;
01752          BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
01753        }
01754     }
01755   return TRUE;
01756 }
01757 
01758 /* Function to set whether a module needs the -mrelocatable bit set.  */
01759 
01760 static bfd_boolean
01761 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
01762 {
01763   BFD_ASSERT (!elf_flags_init (abfd)
01764              || elf_elfheader (abfd)->e_flags == flags);
01765 
01766   elf_elfheader (abfd)->e_flags = flags;
01767   elf_flags_init (abfd) = TRUE;
01768   return TRUE;
01769 }
01770 
01771 /* Support for core dump NOTE sections.  */
01772 
01773 static bfd_boolean
01774 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
01775 {
01776   int offset;
01777   unsigned int size;
01778 
01779   switch (note->descsz)
01780     {
01781     default:
01782       return FALSE;
01783 
01784     case 268:        /* Linux/PPC.  */
01785       /* pr_cursig */
01786       elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
01787 
01788       /* pr_pid */
01789       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
01790 
01791       /* pr_reg */
01792       offset = 72;
01793       size = 192;
01794 
01795       break;
01796     }
01797 
01798   /* Make a ".reg/999" section.  */
01799   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
01800                                      size, note->descpos + offset);
01801 }
01802 
01803 static bfd_boolean
01804 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
01805 {
01806   switch (note->descsz)
01807     {
01808     default:
01809       return FALSE;
01810 
01811     case 128:        /* Linux/PPC elf_prpsinfo.  */
01812       elf_tdata (abfd)->core_program
01813        = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
01814       elf_tdata (abfd)->core_command
01815        = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
01816     }
01817 
01818   /* Note that for some reason, a spurious space is tacked
01819      onto the end of the args in some (at least one anyway)
01820      implementations, so strip it off if it exists.  */
01821 
01822   {
01823     char *command = elf_tdata (abfd)->core_command;
01824     int n = strlen (command);
01825 
01826     if (0 < n && command[n - 1] == ' ')
01827       command[n - 1] = '\0';
01828   }
01829 
01830   return TRUE;
01831 }
01832 
01833 static char *
01834 ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
01835 {
01836   switch (note_type)
01837     {
01838     default:
01839       return NULL;
01840 
01841     case NT_PRPSINFO:
01842       {
01843        char data[128];
01844        va_list ap;
01845 
01846        va_start (ap, note_type);
01847        memset (data, 0, 32);
01848        strncpy (data + 32, va_arg (ap, const char *), 16);
01849        strncpy (data + 48, va_arg (ap, const char *), 80);
01850        va_end (ap);
01851        return elfcore_write_note (abfd, buf, bufsiz,
01852                                "CORE", note_type, data, sizeof (data));
01853       }
01854 
01855     case NT_PRSTATUS:
01856       {
01857        char data[268];
01858        va_list ap;
01859        long pid;
01860        int cursig;
01861        const void *greg;
01862 
01863        va_start (ap, note_type);
01864        memset (data, 0, 72);
01865        pid = va_arg (ap, long);
01866        bfd_put_32 (abfd, pid, data + 24);
01867        cursig = va_arg (ap, int);
01868        bfd_put_16 (abfd, cursig, data + 12);
01869        greg = va_arg (ap, const void *);
01870        memcpy (data + 72, greg, 192);
01871        memset (data + 264, 0, 4);
01872        va_end (ap);
01873        return elfcore_write_note (abfd, buf, bufsiz,
01874                                "CORE", note_type, data, sizeof (data));
01875       }
01876     }
01877 }
01878 
01879 /* Return address for Ith PLT stub in section PLT, for relocation REL
01880    or (bfd_vma) -1 if it should not be included.  */
01881 
01882 static bfd_vma
01883 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
01884                    const asection *plt ATTRIBUTE_UNUSED,
01885                    const arelent *rel)
01886 {
01887   return rel->address;
01888 }
01889 
01890 /* Handle a PowerPC specific section when reading an object file.  This
01891    is called when bfd_section_from_shdr finds a section with an unknown
01892    type.  */
01893 
01894 static bfd_boolean
01895 ppc_elf_section_from_shdr (bfd *abfd,
01896                         Elf_Internal_Shdr *hdr,
01897                         const char *name,
01898                         int shindex)
01899 {
01900   asection *newsect;
01901   flagword flags;
01902 
01903   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
01904     return FALSE;
01905 
01906   newsect = hdr->bfd_section;
01907   flags = bfd_get_section_flags (abfd, newsect);
01908   if (hdr->sh_flags & SHF_EXCLUDE)
01909     flags |= SEC_EXCLUDE;
01910 
01911   if (hdr->sh_type == SHT_ORDERED)
01912     flags |= SEC_SORT_ENTRIES;
01913 
01914   bfd_set_section_flags (abfd, newsect, flags);
01915   return TRUE;
01916 }
01917 
01918 /* Set up any other section flags and such that may be necessary.  */
01919 
01920 static bfd_boolean
01921 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
01922                      Elf_Internal_Shdr *shdr,
01923                      asection *asect)
01924 {
01925   if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
01926     shdr->sh_flags |= SHF_EXCLUDE;
01927 
01928   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
01929     shdr->sh_type = SHT_ORDERED;
01930 
01931   return TRUE;
01932 }
01933 
01934 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
01935    need to bump up the number of section headers.  */
01936 
01937 static int
01938 ppc_elf_additional_program_headers (bfd *abfd,
01939                                 struct bfd_link_info *info ATTRIBUTE_UNUSED)
01940 {
01941   asection *s;
01942   int ret = 0;
01943 
01944   s = bfd_get_section_by_name (abfd, ".sbss2");
01945   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
01946     ++ret;
01947 
01948   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
01949   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
01950     ++ret;
01951 
01952   return ret;
01953 }
01954 
01955 /* Add extra PPC sections -- Note, for now, make .sbss2 and
01956    .PPC.EMB.sbss0 a normal section, and not a bss section so
01957    that the linker doesn't crater when trying to make more than
01958    2 sections.  */
01959 
01960 static const struct bfd_elf_special_section ppc_elf_special_sections[] =
01961 {
01962   { STRING_COMMA_LEN (".plt"),             0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
01963   { STRING_COMMA_LEN (".sbss"),           -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
01964   { STRING_COMMA_LEN (".sbss2"),          -2, SHT_PROGBITS, SHF_ALLOC },
01965   { STRING_COMMA_LEN (".sdata"),          -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
01966   { STRING_COMMA_LEN (".sdata2"),         -2, SHT_PROGBITS, SHF_ALLOC },
01967   { STRING_COMMA_LEN (".tags"),            0, SHT_ORDERED,  SHF_ALLOC },
01968   { STRING_COMMA_LEN (".PPC.EMB.apuinfo"), 0, SHT_NOTE,     0 },
01969   { STRING_COMMA_LEN (".PPC.EMB.sbss0"),   0, SHT_PROGBITS, SHF_ALLOC },
01970   { STRING_COMMA_LEN (".PPC.EMB.sdata0"),  0, SHT_PROGBITS, SHF_ALLOC },
01971   { NULL,                              0,  0, 0,            0 }
01972 };
01973 
01974 /* This is what we want for new plt/got.  */
01975 static struct bfd_elf_special_section ppc_alt_plt =
01976   { STRING_COMMA_LEN (".plt"),             0, SHT_PROGBITS, SHF_ALLOC };
01977 
01978 static const struct bfd_elf_special_section *
01979 ppc_elf_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
01980 {
01981   const struct bfd_elf_special_section *ssect;
01982 
01983   /* See if this is one of the special sections.  */
01984   if (sec->name == NULL)
01985     return NULL;
01986 
01987   ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
01988                                    sec->use_rela_p);
01989   if (ssect != NULL)
01990     {
01991       if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
01992        ssect = &ppc_alt_plt;
01993       return ssect;
01994     }
01995 
01996   return _bfd_elf_get_sec_type_attr (abfd, sec);
01997 }
01998 
01999 /* Very simple linked list structure for recording apuinfo values.  */
02000 typedef struct apuinfo_list
02001 {
02002   struct apuinfo_list *next;
02003   unsigned long value;
02004 }
02005 apuinfo_list;
02006 
02007 static apuinfo_list *head;
02008 
02009 
02010 static void
02011 apuinfo_list_init (void)
02012 {
02013   head = NULL;
02014 }
02015 
02016 static void
02017 apuinfo_list_add (unsigned long value)
02018 {
02019   apuinfo_list *entry = head;
02020 
02021   while (entry != NULL)
02022     {
02023       if (entry->value == value)
02024        return;
02025       entry = entry->next;
02026     }
02027 
02028   entry = bfd_malloc (sizeof (* entry));
02029   if (entry == NULL)
02030     return;
02031 
02032   entry->value = value;
02033   entry->next  = head;
02034   head = entry;
02035 }
02036 
02037 static unsigned
02038 apuinfo_list_length (void)
02039 {
02040   apuinfo_list *entry;
02041   unsigned long count;
02042 
02043   for (entry = head, count = 0;
02044        entry;
02045        entry = entry->next)
02046     ++ count;
02047 
02048   return count;
02049 }
02050 
02051 static inline unsigned long
02052 apuinfo_list_element (unsigned long number)
02053 {
02054   apuinfo_list * entry;
02055 
02056   for (entry = head;
02057        entry && number --;
02058        entry = entry->next)
02059     ;
02060 
02061   return entry ? entry->value : 0;
02062 }
02063 
02064 static void
02065 apuinfo_list_finish (void)
02066 {
02067   apuinfo_list *entry;
02068 
02069   for (entry = head; entry;)
02070     {
02071       apuinfo_list *next = entry->next;
02072       free (entry);
02073       entry = next;
02074     }
02075 
02076   head = NULL;
02077 }
02078 
02079 #define APUINFO_SECTION_NAME       ".PPC.EMB.apuinfo"
02080 #define APUINFO_LABEL              "APUinfo"
02081 
02082 /* Scan the input BFDs and create a linked list of
02083    the APUinfo values that will need to be emitted.  */
02084 
02085 static void
02086 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
02087 {
02088   bfd *ibfd;
02089   asection *asec;
02090   char *buffer;
02091   unsigned num_input_sections;
02092   bfd_size_type      output_section_size;
02093   unsigned i;
02094   unsigned num_entries;
02095   unsigned long      offset;
02096   unsigned long length;
02097   const char *error_message = NULL;
02098 
02099   if (link_info == NULL)
02100     return;
02101 
02102   /* Scan the input bfds, looking for apuinfo sections.  */
02103   num_input_sections = 0;
02104   output_section_size = 0;
02105 
02106   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
02107     {
02108       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
02109       if (asec)
02110        {
02111          ++ num_input_sections;
02112          output_section_size += asec->size;
02113        }
02114     }
02115 
02116   /* We need at least one input sections
02117      in order to make merging worthwhile.  */
02118   if (num_input_sections < 1)
02119     return;
02120 
02121   /* Just make sure that the output section exists as well.  */
02122   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
02123   if (asec == NULL)
02124     return;
02125 
02126   /* Allocate a buffer for the contents of the input sections.  */
02127   buffer = bfd_malloc (output_section_size);
02128   if (buffer == NULL)
02129     return;
02130 
02131   offset = 0;
02132   apuinfo_list_init ();
02133 
02134   /* Read in the input sections contents.  */
02135   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
02136     {
02137       unsigned long datum;
02138       char *ptr;
02139 
02140       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
02141       if (asec == NULL)
02142        continue;
02143 
02144       length = asec->size;
02145       if (length < 24)
02146        {
02147          error_message = _("corrupt or empty %s section in %B");
02148          goto fail;
02149        }
02150 
02151       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
02152          || (bfd_bread (buffer + offset, length, ibfd) != length))
02153        {
02154          error_message = _("unable to read in %s section from %B");
02155          goto fail;
02156        }
02157 
02158       /* Process the contents of the section.  */
02159       ptr = buffer + offset;
02160       error_message = _("corrupt %s section in %B");
02161 
02162       /* Verify the contents of the header.  Note - we have to
02163         extract the values this way in order to allow for a
02164         host whose endian-ness is different from the target.  */
02165       datum = bfd_get_32 (ibfd, ptr);
02166       if (datum != sizeof APUINFO_LABEL)
02167        goto fail;
02168 
02169       datum = bfd_get_32 (ibfd, ptr + 8);
02170       if (datum != 0x2)
02171        goto fail;
02172 
02173       if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
02174        goto fail;
02175 
02176       /* Get the number of bytes used for apuinfo entries.  */
02177       datum = bfd_get_32 (ibfd, ptr + 4);
02178       if (datum + 20 != length)
02179        goto fail;
02180 
02181       /* Make sure that we do not run off the end of the section.  */
02182       if (offset + length > output_section_size)
02183        goto fail;
02184 
02185       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
02186       for (i = 0; i < datum; i += 4)
02187        apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
02188 
02189       /* Update the offset.  */
02190       offset += length;
02191     }
02192 
02193   error_message = NULL;
02194 
02195   /* Compute the size of the output section.  */
02196   num_entries = apuinfo_list_length ();
02197   output_section_size = 20 + num_entries * 4;
02198 
02199   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
02200 
02201   if (! bfd_set_section_size (abfd, asec, output_section_size))
02202     ibfd = abfd,
02203       error_message = _("warning: unable to set size of %s section in %B");
02204 
02205  fail:
02206   free (buffer);
02207 
02208   if (error_message)
02209     (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
02210 }
02211 
02212 /* Prevent the output section from accumulating the input sections'
02213    contents.  We have already stored this in our linked list structure.  */
02214 
02215 static bfd_boolean
02216 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
02217                      struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
02218                      asection *asec,
02219                      bfd_byte *contents ATTRIBUTE_UNUSED)
02220 {
02221   return (apuinfo_list_length ()
02222          && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
02223 }
02224 
02225 /* Finally we can generate the output section.  */
02226 
02227 static void
02228 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
02229 {
02230   bfd_byte *buffer;
02231   asection *asec;
02232   unsigned i;
02233   unsigned num_entries;
02234   bfd_size_type length;
02235 
02236   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
02237   if (asec == NULL)
02238     return;
02239 
02240   if (apuinfo_list_length () == 0)
02241     return;
02242 
02243   length = asec->size;
02244   if (length < 20)
02245     return;
02246 
02247   buffer = bfd_malloc (length);
02248   if (buffer == NULL)
02249     {
02250       (*_bfd_error_handler)
02251        (_("failed to allocate space for new APUinfo section."));
02252       return;
02253     }
02254 
02255   /* Create the apuinfo header.  */
02256   num_entries = apuinfo_list_length ();
02257   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
02258   bfd_put_32 (abfd, num_entries * 4, buffer + 4);
02259   bfd_put_32 (abfd, 0x2, buffer + 8);
02260   strcpy ((char *) buffer + 12, APUINFO_LABEL);
02261 
02262   length = 20;
02263   for (i = 0; i < num_entries; i++)
02264     {
02265       bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
02266       length += 4;
02267     }
02268 
02269   if (length != asec->size)
02270     (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
02271 
02272   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
02273     (*_bfd_error_handler) (_("failed to install new APUinfo section."));
02274 
02275   free (buffer);
02276 
02277   apuinfo_list_finish ();
02278 }
02279 
02280 /* The following functions are specific to the ELF linker, while
02281    functions above are used generally.  They appear in this file more
02282    or less in the order in which they are called.  eg.
02283    ppc_elf_check_relocs is called early in the link process,
02284    ppc_elf_finish_dynamic_sections is one of the last functions
02285    called.  */
02286 
02287 /* The PPC linker needs to keep track of the number of relocs that it
02288    decides to copy as dynamic relocs in check_relocs for each symbol.
02289    This is so that it can later discard them if they are found to be
02290    unnecessary.  We store the information in a field extending the
02291    regular ELF linker hash table.  */
02292 
02293 struct ppc_elf_dyn_relocs
02294 {
02295   struct ppc_elf_dyn_relocs *next;
02296 
02297   /* The input section of the reloc.  */
02298   asection *sec;
02299 
02300   /* Total number of relocs copied for the input section.  */
02301   bfd_size_type count;
02302 
02303   /* Number of pc-relative relocs copied for the input section.  */
02304   bfd_size_type pc_count;
02305 };
02306 
02307 /* Track PLT entries needed for a given symbol.  We might need more
02308    than one glink entry per symbol.  */
02309 struct plt_entry
02310 {
02311   struct plt_entry *next;
02312 
02313   /* -fPIC uses multiple GOT sections, one per file, called ".got2".
02314      This field stores the offset into .got2 used to initialise the
02315      GOT pointer reg.  It will always be at least 32768 (and for
02316      current gcc this is the only offset used).  */
02317   bfd_vma addend;
02318 
02319   /* The .got2 section.  */
02320   asection *sec;
02321 
02322   /* PLT refcount or offset.  */
02323   union
02324     {
02325       bfd_signed_vma refcount;
02326       bfd_vma offset;
02327     } plt;
02328 
02329   /* .glink stub offset.  */
02330   bfd_vma glink_offset;
02331 };
02332 
02333 /* Of those relocs that might be copied as dynamic relocs, this macro
02334    selects those that must be copied when linking a shared library,
02335    even when the symbol is local.  */
02336 
02337 #define MUST_BE_DYN_RELOC(RTYPE)          \
02338   ((RTYPE) != R_PPC_REL24                 \
02339    && (RTYPE) != R_PPC_REL14                     \
02340    && (RTYPE) != R_PPC_REL14_BRTAKEN             \
02341    && (RTYPE) != R_PPC_REL14_BRNTAKEN            \
02342    && (RTYPE) != R_PPC_REL32)
02343 
02344 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
02345    copying dynamic variables from a shared lib into an app's dynbss
02346    section, and instead use a dynamic relocation to point into the
02347    shared lib.  */
02348 #define ELIMINATE_COPY_RELOCS 1
02349 
02350 /* PPC ELF linker hash entry.  */
02351 
02352 struct ppc_elf_link_hash_entry
02353 {
02354   struct elf_link_hash_entry elf;
02355 
02356   /* If this symbol is used in the linker created sections, the processor
02357      specific backend uses this field to map the field into the offset
02358      from the beginning of the section.  */
02359   elf_linker_section_pointers_t *linker_section_pointer;
02360 
02361   /* Track dynamic relocs copied for this symbol.  */
02362   struct ppc_elf_dyn_relocs *dyn_relocs;
02363 
02364   /* Contexts in which symbol is used in the GOT (or TOC).
02365      TLS_GD .. TLS_TLS bits are or'd into the mask as the
02366      corresponding relocs are encountered during check_relocs.
02367      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
02368      indicate the corresponding GOT entry type is not needed.  */
02369 #define TLS_GD               1     /* GD reloc. */
02370 #define TLS_LD               2     /* LD reloc. */
02371 #define TLS_TPREL     4     /* TPREL reloc, => IE. */
02372 #define TLS_DTPREL    8     /* DTPREL reloc, => LD. */
02373 #define TLS_TLS             16     /* Any TLS reloc.  */
02374 #define TLS_TPRELGD  32     /* TPREL reloc resulting from GD->IE. */
02375   char tls_mask;
02376 
02377   /* Nonzero if we have seen a small data relocation referring to this
02378      symbol.  */
02379   unsigned char has_sda_refs;
02380 };
02381 
02382 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
02383 
02384 enum ppc_elf_plt_type {
02385   PLT_UNSET,
02386   PLT_OLD,
02387   PLT_NEW,
02388   PLT_VXWORKS
02389 };
02390 
02391 /* PPC ELF linker hash table.  */
02392 
02393 struct ppc_elf_link_hash_table
02394 {
02395   struct elf_link_hash_table elf;
02396 
02397   /* Short-cuts to get to dynamic linker sections.  */
02398   asection *got;
02399   asection *relgot;
02400   asection *glink;
02401   asection *plt;
02402   asection *relplt;
02403   asection *dynbss;
02404   asection *relbss;
02405   asection *dynsbss;
02406   asection *relsbss;
02407   elf_linker_section_t sdata[2];
02408   asection *sbss;
02409 
02410   /* Shortcut to .__tls_get_addr.  */
02411   struct elf_link_hash_entry *tls_get_addr;
02412 
02413   /* TLS local dynamic got entry handling.  */
02414   union {
02415     bfd_signed_vma refcount;
02416     bfd_vma offset;
02417   } tlsld_got;
02418 
02419   /* Offset of PltResolve function in glink.  */
02420   bfd_vma glink_pltresolve;
02421 
02422   /* Size of reserved GOT entries.  */
02423   unsigned int got_header_size;
02424   /* Non-zero if allocating the header left a gap.  */
02425   unsigned int got_gap;
02426 
02427   /* The type of PLT we have chosen to use.  */
02428   enum ppc_elf_plt_type plt_type;
02429 
02430   /* Whether we can use the new PLT layout.  */
02431   unsigned int can_use_new_plt:1;
02432 
02433   /* Set if we should emit symbols for stubs.  */
02434   unsigned int emit_stub_syms:1;
02435 
02436   /* Small local sym to section mapping cache.  */
02437   struct sym_sec_cache sym_sec;
02438 
02439   /* The (unloaded but important) .rela.plt.unloaded on VxWorks.  */
02440   asection *srelplt2;
02441 
02442   /* The .got.plt section (VxWorks only)*/
02443   asection *sgotplt;
02444 
02445   /* True if the target system is VxWorks.  */
02446   int is_vxworks;
02447 
02448   /* The size of PLT entries.  */
02449   int plt_entry_size;
02450   /* The distance between adjacent PLT slots.  */
02451   int plt_slot_size;
02452   /* The size of the first PLT entry.  */
02453   int plt_initial_entry_size;
02454 };
02455 
02456 /* Get the PPC ELF linker hash table from a link_info structure.  */
02457 
02458 #define ppc_elf_hash_table(p) \
02459   ((struct ppc_elf_link_hash_table *) (p)->hash)
02460 
02461 /* Create an entry in a PPC ELF linker hash table.  */
02462 
02463 static struct bfd_hash_entry *
02464 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
02465                         struct bfd_hash_table *table,
02466                         const char *string)
02467 {
02468   /* Allocate the structure if it has not already been allocated by a
02469      subclass.  */
02470   if (entry == NULL)
02471     {
02472       entry = bfd_hash_allocate (table,
02473                              sizeof (struct ppc_elf_link_hash_entry));
02474       if (entry == NULL)
02475        return entry;
02476     }
02477 
02478   /* Call the allocation method of the superclass.  */
02479   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
02480   if (entry != NULL)
02481     {
02482       ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
02483       ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
02484       ppc_elf_hash_entry (entry)->tls_mask = 0;
02485     }
02486 
02487   return entry;
02488 }
02489 
02490 /* Create a PPC ELF linker hash table.  */
02491 
02492 static struct bfd_link_hash_table *
02493 ppc_elf_link_hash_table_create (bfd *abfd)
02494 {
02495   struct ppc_elf_link_hash_table *ret;
02496 
02497   ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
02498   if (ret == NULL)
02499     return NULL;
02500 
02501   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
02502                                   ppc_elf_link_hash_newfunc,
02503                                   sizeof (struct ppc_elf_link_hash_entry)))
02504     {
02505       free (ret);
02506       return NULL;
02507     }
02508 
02509   ret->elf.init_plt_refcount.refcount = 0;
02510   ret->elf.init_plt_refcount.glist = NULL;
02511   ret->elf.init_plt_offset.offset = 0;
02512   ret->elf.init_plt_offset.glist = NULL;
02513 
02514   ret->sdata[0].name = ".sdata";
02515   ret->sdata[0].sym_name = "_SDA_BASE_";
02516   ret->sdata[0].bss_name = ".sbss";
02517 
02518   ret->sdata[1].name = ".sdata2";
02519   ret->sdata[1].sym_name = "_SDA2_BASE_";
02520   ret->sdata[1].bss_name = ".sbss2";
02521 
02522   ret->plt_entry_size = 12;
02523   ret->plt_slot_size = 8;
02524   ret->plt_initial_entry_size = 72;
02525   
02526   ret->is_vxworks = 0;
02527 
02528   return &ret->elf.root;
02529 }
02530 
02531 /* Create .got and the related sections.  */
02532 
02533 static bfd_boolean
02534 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
02535 {
02536   struct ppc_elf_link_hash_table *htab;
02537   asection *s;
02538   flagword flags;
02539 
02540   if (!_bfd_elf_create_got_section (abfd, info))
02541     return FALSE;
02542 
02543   htab = ppc_elf_hash_table (info);
02544   htab->got = s = bfd_get_section_by_name (abfd, ".got");
02545   if (s == NULL)
02546     abort ();
02547 
02548   if (htab->is_vxworks)
02549     {
02550       htab->sgotplt = bfd_get_section_by_name (abfd, ".got.plt");
02551       if (!htab->sgotplt)
02552        abort ();
02553     }
02554   else
02555     {
02556       /* The powerpc .got has a blrl instruction in it.  Mark it
02557         executable.  */
02558       flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
02559               | SEC_IN_MEMORY | SEC_LINKER_CREATED);
02560       if (!bfd_set_section_flags (abfd, s, flags))
02561        return FALSE;
02562     }
02563 
02564   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
02565           | SEC_LINKER_CREATED | SEC_READONLY);
02566   htab->relgot = bfd_make_section_with_flags (abfd, ".rela.got", flags);
02567   if (!htab->relgot
02568       || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
02569     return FALSE;
02570 
02571   return TRUE;
02572 }
02573 
02574 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
02575    to output sections (just like _bfd_elf_create_dynamic_sections has
02576    to create .dynbss and .rela.bss).  */
02577 
02578 static bfd_boolean
02579 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
02580 {
02581   struct ppc_elf_link_hash_table *htab;
02582   asection *s;
02583   flagword flags;
02584 
02585   htab = ppc_elf_hash_table (info);
02586 
02587   if (htab->got == NULL
02588       && !ppc_elf_create_got (abfd, info))
02589     return FALSE;
02590 
02591   if (!_bfd_elf_create_dynamic_sections (abfd, info))
02592     return FALSE;
02593 
02594   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
02595           | SEC_IN_MEMORY | SEC_LINKER_CREATED);
02596 
02597   s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags | SEC_CODE);
02598   htab->glink = s;
02599   if (s == NULL
02600       || !bfd_set_section_alignment (abfd, s, 4))
02601     return FALSE;
02602 
02603   htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
02604   s = bfd_make_section_with_flags (abfd, ".dynsbss",
02605                                SEC_ALLOC | SEC_LINKER_CREATED);
02606   htab->dynsbss = s;
02607   if (s == NULL)
02608     return FALSE;
02609 
02610   if (! info->shared)
02611     {
02612       htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
02613       s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
02614       htab->relsbss = s;
02615       if (s == NULL
02616          || ! bfd_set_section_alignment (abfd, s, 2))
02617        return FALSE;
02618     }
02619 
02620   if (htab->is_vxworks
02621       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
02622     return FALSE;
02623 
02624   htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
02625   htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
02626   if (s == NULL)
02627     abort ();
02628 
02629   flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
02630   if (htab->plt_type == PLT_VXWORKS)
02631     /* The VxWorks PLT is a loaded section with contents.  */
02632     flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
02633   return bfd_set_section_flags (abfd, s, flags);
02634 }
02635 
02636 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
02637 
02638 static void
02639 ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
02640                            struct elf_link_hash_entry *dir,
02641                            struct elf_link_hash_entry *ind)
02642 {
02643   struct ppc_elf_link_hash_entry *edir, *eind;
02644 
02645   edir = (struct ppc_elf_link_hash_entry *) dir;
02646   eind = (struct ppc_elf_link_hash_entry *) ind;
02647 
02648   if (eind->dyn_relocs != NULL)
02649     {
02650       if (edir->dyn_relocs != NULL)
02651        {
02652          struct ppc_elf_dyn_relocs **pp;
02653          struct ppc_elf_dyn_relocs *p;
02654 
02655          /* Add reloc counts against the indirect sym to the direct sym
02656             list.  Merge any entries against the same section.  */
02657          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
02658            {
02659              struct ppc_elf_dyn_relocs *q;
02660 
02661              for (q = edir->dyn_relocs; q != NULL; q = q->next)
02662               if (q->sec == p->sec)
02663                 {
02664                   q->pc_count += p->pc_count;
02665                   q->count += p->count;
02666                   *pp = p->next;
02667                   break;
02668                 }
02669              if (q == NULL)
02670               pp = &p->next;
02671            }
02672          *pp = edir->dyn_relocs;
02673        }
02674 
02675       edir->dyn_relocs = eind->dyn_relocs;
02676       eind->dyn_relocs = NULL;
02677     }
02678 
02679   edir->tls_mask |= eind->tls_mask;
02680   edir->has_sda_refs |= eind->has_sda_refs;
02681 
02682   /* If called to transfer flags for a weakdef during processing
02683      of elf_adjust_dynamic_symbol, don't copy non_got_ref.
02684      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
02685   if (!(ELIMINATE_COPY_RELOCS
02686        && eind->elf.root.type != bfd_link_hash_indirect
02687        && edir->elf.dynamic_adjusted))
02688     edir->elf.non_got_ref |= eind->elf.non_got_ref;
02689 
02690   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
02691   edir->elf.ref_regular |= eind->elf.ref_regular;
02692   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
02693   edir->elf.needs_plt |= eind->elf.needs_plt;
02694 
02695   /* If we were called to copy over info for a weak sym, that's all.  */
02696   if (eind->elf.root.type != bfd_link_hash_indirect)
02697     return;
02698 
02699   /* Copy over the GOT refcount entries that we may have already seen to
02700      the symbol which just became indirect.  */
02701   edir->elf.got.refcount += eind->elf.got.refcount;
02702   eind->elf.got.refcount = 0;
02703 
02704   /* And plt entries.  */
02705   if (eind->elf.plt.plist != NULL)
02706     {
02707       if (edir->elf.plt.plist != NULL)
02708        {
02709          struct plt_entry **entp;
02710          struct plt_entry *ent;
02711 
02712          for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
02713            {
02714              struct plt_entry *dent;
02715 
02716              for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
02717               if (dent->sec == ent->sec && dent->addend == ent->addend)
02718                 {
02719                   dent->plt.refcount += ent->plt.refcount;
02720                   *entp = ent->next;
02721                   break;
02722                 }
02723              if (dent == NULL)
02724               entp = &ent->next;
02725            }
02726          *entp = edir->elf.plt.plist;
02727        }
02728 
02729       edir->elf.plt.plist = eind->elf.plt.plist;
02730       eind->elf.plt.plist = NULL;
02731     }
02732 
02733   if (eind->elf.dynindx != -1)
02734     {
02735       if (edir->elf.dynindx != -1)
02736        _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
02737                             edir->elf.dynstr_index);
02738       edir->elf.dynindx = eind->elf.dynindx;
02739       edir->elf.dynstr_index = eind->elf.dynstr_index;
02740       eind->elf.dynindx = -1;
02741       eind->elf.dynstr_index = 0;
02742     }
02743 }
02744 
02745 /* Return 1 if target is one of ours.  */
02746 
02747 static bfd_boolean
02748 is_ppc_elf_target (const struct bfd_target *targ)
02749 {
02750   extern const bfd_target bfd_elf32_powerpc_vec;
02751   extern const bfd_target bfd_elf32_powerpc_vxworks_vec;
02752   extern const bfd_target bfd_elf32_powerpcle_vec;
02753 
02754   return (targ == &bfd_elf32_powerpc_vec
02755          || targ == &bfd_elf32_powerpc_vxworks_vec
02756          || targ == &bfd_elf32_powerpcle_vec);
02757 }
02758 
02759 /* Hook called by the linker routine which adds symbols from an object
02760    file.  We use it to put .comm items in .sbss, and not .bss.  */
02761 
02762 static bfd_boolean
02763 ppc_elf_add_symbol_hook (bfd *abfd,
02764                       struct bfd_link_info *info,
02765                       Elf_Internal_Sym *sym,
02766                       const char **namep ATTRIBUTE_UNUSED,
02767                       flagword *flagsp ATTRIBUTE_UNUSED,
02768                       asection **secp,
02769                       bfd_vma *valp)
02770 {
02771   if (sym->st_shndx == SHN_COMMON
02772       && !info->relocatable
02773       && sym->st_size <= elf_gp_size (abfd)
02774       && is_ppc_elf_target (info->hash->creator))
02775     {
02776       /* Common symbols less than or equal to -G nn bytes are automatically
02777         put into .sbss.  */
02778       struct ppc_elf_link_hash_table *htab;
02779 
02780       htab = ppc_elf_hash_table (info);
02781       if (htab->sbss == NULL)
02782        {
02783          flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
02784 
02785          if (!htab->elf.dynobj)
02786            htab->elf.dynobj = abfd;
02787 
02788          htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
02789                                                     ".sbss",
02790                                                     flags);
02791          if (htab->sbss == NULL)
02792            return FALSE;
02793        }
02794 
02795       *secp = htab->sbss;
02796       *valp = sym->st_size;
02797     }
02798 
02799   return TRUE;
02800 }
02801 
02802 static bfd_boolean
02803 create_sdata_sym (struct ppc_elf_link_hash_table *htab,
02804                 elf_linker_section_t *lsect)
02805 {
02806   lsect->sym = elf_link_hash_lookup (&htab->elf, lsect->sym_name,
02807                                  TRUE, FALSE, TRUE);
02808   if (lsect->sym == NULL)
02809     return FALSE;
02810   if (lsect->sym->root.type == bfd_link_hash_new)
02811     lsect->sym->non_elf = 0;
02812   lsect->sym->ref_regular = 1;
02813   return TRUE;
02814 }
02815 
02816 /* Create a special linker section.  */
02817 
02818 static bfd_boolean
02819 ppc_elf_create_linker_section (bfd *abfd,
02820                             struct bfd_link_info *info,
02821                             flagword flags,
02822                             elf_linker_section_t *lsect)
02823 {
02824   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
02825   asection *s;
02826 
02827   flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
02828            | SEC_LINKER_CREATED);
02829 
02830   /* Record the first bfd that needs the special sections.  */
02831   if (!htab->elf.dynobj)
02832     htab->elf.dynobj = abfd;
02833 
02834   s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
02835                                      lsect->name,
02836                                      flags);
02837   if (s == NULL
02838       || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
02839     return FALSE;
02840   lsect->section = s;
02841 
02842   return create_sdata_sym (htab, lsect);
02843 }
02844 
02845 /* Find a linker generated pointer with a given addend and type.  */
02846 
02847 static elf_linker_section_pointers_t *
02848 elf_find_pointer_linker_section
02849   (elf_linker_section_pointers_t *linker_pointers,
02850    bfd_vma addend,
02851    elf_linker_section_t *lsect)
02852 {
02853   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
02854     if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
02855       return linker_pointers;
02856 
02857   return NULL;
02858 }
02859 
02860 /* Allocate a pointer to live in a linker created section.  */
02861 
02862 static bfd_boolean
02863 elf_create_pointer_linker_section (bfd *abfd,
02864                                elf_linker_section_t *lsect,
02865                                struct elf_link_hash_entry *h,
02866                                const Elf_Internal_Rela *rel)
02867 {
02868   elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
02869   elf_linker_section_pointers_t *linker_section_ptr;
02870   unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
02871   bfd_size_type amt;
02872 
02873   BFD_ASSERT (lsect != NULL);
02874 
02875   /* Is this a global symbol?  */
02876   if (h != NULL)
02877     {
02878       struct ppc_elf_link_hash_entry *eh;
02879 
02880       /* Has this symbol already been allocated?  If so, our work is done.  */
02881       eh = (struct ppc_elf_link_hash_entry *) h;
02882       if (elf_find_pointer_linker_section (eh->linker_section_pointer,
02883                                       rel->r_addend,
02884                                       lsect))
02885        return TRUE;
02886 
02887       ptr_linker_section_ptr = &eh->linker_section_pointer;
02888     }
02889   else
02890     {
02891       /* Allocation of a pointer to a local symbol.  */
02892       elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
02893 
02894       /* Allocate a table to hold the local symbols if first time.  */
02895       if (!ptr)
02896        {
02897          unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
02898 
02899          amt = num_symbols;
02900          amt *= sizeof (elf_linker_section_pointers_t *);
02901          ptr = bfd_zalloc (abfd, amt);
02902 
02903          if (!ptr)
02904            return FALSE;
02905 
02906          elf_local_ptr_offsets (abfd) = ptr;
02907        }
02908 
02909       /* Has this symbol already been allocated?  If so, our work is done.  */
02910       if (elf_find_pointer_linker_section (ptr[r_symndx],
02911                                       rel->r_addend,
02912                                       lsect))
02913        return TRUE;
02914 
02915       ptr_linker_section_ptr = &ptr[r_symndx];
02916     }
02917 
02918   /* Allocate space for a pointer in the linker section, and allocate
02919      a new pointer record from internal memory.  */
02920   BFD_ASSERT (ptr_linker_section_ptr != NULL);
02921   amt = sizeof (elf_linker_section_pointers_t);
02922   linker_section_ptr = bfd_alloc (abfd, amt);
02923 
02924   if (!linker_section_ptr)
02925     return FALSE;
02926 
02927   linker_section_ptr->next = *ptr_linker_section_ptr;
02928   linker_section_ptr->addend = rel->r_addend;
02929   linker_section_ptr->lsect = lsect;
02930   *ptr_linker_section_ptr = linker_section_ptr;
02931 
02932   linker_section_ptr->offset = lsect->section->size;
02933   lsect->section->size += 4;
02934 
02935 #ifdef DEBUG
02936   fprintf (stderr,
02937           "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
02938           lsect->name, (long) linker_section_ptr->offset,
02939           (long) lsect->section->size);
02940 #endif
02941 
02942   return TRUE;
02943 }
02944 
02945 static bfd_boolean
02946 update_local_sym_info (bfd *abfd,
02947                      Elf_Internal_Shdr *symtab_hdr,
02948                      unsigned long r_symndx,
02949                      int tls_type)
02950 {
02951   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
02952   char *local_got_tls_masks;
02953 
02954   if (local_got_refcounts == NULL)
02955     {
02956       bfd_size_type size = symtab_hdr->sh_info;
02957 
02958       size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
02959       local_got_refcounts = bfd_zalloc (abfd, size);
02960       if (local_got_refcounts == NULL)
02961        return FALSE;
02962       elf_local_got_refcounts (abfd) = local_got_refcounts;
02963     }
02964 
02965   local_got_refcounts[r_symndx] += 1;
02966   local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
02967   local_got_tls_masks[r_symndx] |= tls_type;
02968   return TRUE;
02969 }
02970 
02971 static bfd_boolean
02972 update_plt_info (bfd *abfd, struct elf_link_hash_entry *h,
02973                asection *sec, bfd_vma addend)
02974 {
02975   struct plt_entry *ent;
02976 
02977   if (addend < 32768)
02978     sec = NULL;
02979   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
02980     if (ent->sec == sec && ent->addend == addend)
02981       break;
02982   if (ent == NULL)
02983     {
02984       bfd_size_type amt = sizeof (*ent);
02985       ent = bfd_alloc (abfd, amt);
02986       if (ent == NULL)
02987        return FALSE;
02988       ent->next = h->plt.plist;
02989       ent->sec = sec;
02990       ent->addend = addend;
02991       ent->plt.refcount = 0;
02992       h->plt.plist = ent;
02993     }
02994   ent->plt.refcount += 1;
02995   return TRUE;
02996 }
02997 
02998 static struct plt_entry *
02999 find_plt_ent (struct elf_link_hash_entry *h, asection *sec, bfd_vma addend)
03000 {
03001   struct plt_entry *ent;
03002 
03003   if (addend < 32768)
03004     sec = NULL;
03005   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
03006     if (ent->sec == sec && ent->addend == addend)
03007       break;
03008   return ent;
03009 }
03010 
03011 static void
03012 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
03013 {
03014   (*_bfd_error_handler)
03015     (_("%B: relocation %s cannot be used when making a shared object"),
03016      abfd,
03017      ppc_elf_howto_table[r_type]->name);
03018   bfd_set_error (bfd_error_bad_value);
03019 }
03020 
03021 /* Look through the relocs for a section during the first phase, and
03022    allocate space in the global offset table or procedure linkage
03023    table.  */
03024 
03025 static bfd_boolean
03026 ppc_elf_check_relocs (bfd *abfd,
03027                     struct bfd_link_info *info,
03028                     asection *sec,
03029                     const Elf_Internal_Rela *relocs)
03030 {
03031   struct ppc_elf_link_hash_table *htab;
03032   Elf_Internal_Shdr *symtab_hdr;
03033   struct elf_link_hash_entry **sym_hashes;
03034   const Elf_Internal_Rela *rel;
03035   const Elf_Internal_Rela *rel_end;
03036   asection *got2, *sreloc;
03037 
03038   if (info->relocatable)
03039     return TRUE;
03040 
03041   /* Don't do anything special with non-loaded, non-alloced sections.
03042      In particular, any relocs in such sections should not affect GOT
03043      and PLT reference counting (ie. we don't allow them to create GOT
03044      or PLT entries), there's no possibility or desire to optimize TLS
03045      relocs, and there's not much point in propagating relocs to shared
03046      libs that the dynamic linker won't relocate.  */
03047   if ((sec->flags & SEC_ALLOC) == 0)
03048     return TRUE;
03049 
03050 #ifdef DEBUG
03051   _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
03052                     sec, abfd);
03053 #endif
03054 
03055   /* Initialize howto table if not already done.  */
03056   if (!ppc_elf_howto_table[R_PPC_ADDR32])
03057     ppc_elf_howto_init ();
03058 
03059   htab = ppc_elf_hash_table (info);
03060   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
03061   sym_hashes = elf_sym_hashes (abfd);
03062   got2 = bfd_get_section_by_name (abfd, ".got2");
03063   sreloc = NULL;
03064 
03065   rel_end = relocs + sec->reloc_count;
03066   for (rel = relocs; rel < rel_end; rel++)
03067     {
03068       unsigned long r_symndx;
03069       enum elf_ppc_reloc_type r_type;
03070       struct elf_link_hash_entry *h;
03071       int tls_type = 0;
03072 
03073       r_symndx = ELF32_R_SYM (rel->r_info);
03074       if (r_symndx < symtab_hdr->sh_info)
03075        h = NULL;
03076       else
03077        {
03078          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
03079          while (h->root.type == bfd_link_hash_indirect
03080                || h->root.type == bfd_link_hash_warning)
03081            h = (struct elf_link_hash_entry *) h->root.u.i.link;
03082        }
03083 
03084       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
03085         This shows up in particular in an R_PPC_ADDR32 in the eabi
03086         startup code.  */
03087       if (h != NULL
03088          && htab->got == NULL
03089          && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
03090        {
03091          if (htab->elf.dynobj == NULL)
03092            htab->elf.dynobj = abfd;
03093          if (!ppc_elf_create_got (htab->elf.dynobj, info))
03094            return FALSE;
03095          BFD_ASSERT (h == htab->elf.hgot);
03096        }
03097 
03098       r_type = ELF32_R_TYPE (rel->r_info);
03099       switch (r_type)
03100        {
03101        case R_PPC_GOT_TLSLD16:
03102        case R_PPC_GOT_TLSLD16_LO:
03103        case R_PPC_GOT_TLSLD16_HI:
03104        case R_PPC_GOT_TLSLD16_HA:
03105          htab->tlsld_got.refcount += 1;
03106          tls_type = TLS_TLS | TLS_LD;
03107          goto dogottls;
03108 
03109        case R_PPC_GOT_TLSGD16:
03110        case R_PPC_GOT_TLSGD16_LO:
03111        case R_PPC_GOT_TLSGD16_HI:
03112        case R_PPC_GOT_TLSGD16_HA:
03113          tls_type = TLS_TLS | TLS_GD;
03114          goto dogottls;
03115 
03116        case R_PPC_GOT_TPREL16:
03117        case R_PPC_GOT_TPREL16_LO:
03118        case R_PPC_GOT_TPREL16_HI:
03119        case R_PPC_GOT_TPREL16_HA:
03120          if (info->shared)
03121            info->flags |= DF_STATIC_TLS;
03122          tls_type = TLS_TLS | TLS_TPREL;
03123          goto dogottls;
03124 
03125        case R_PPC_GOT_DTPREL16:
03126        case R_PPC_GOT_DTPREL16_LO:
03127        case R_PPC_GOT_DTPREL16_HI:
03128        case R_PPC_GOT_DTPREL16_HA:
03129          tls_type = TLS_TLS | TLS_DTPREL;
03130        dogottls:
03131          sec->has_tls_reloc = 1;
03132          /* Fall thru */
03133 
03134          /* GOT16 relocations */
03135        case R_PPC_GOT16:
03136        case R_PPC_GOT16_LO:
03137        case R_PPC_GOT16_HI:
03138        case R_PPC_GOT16_HA:
03139          /* This symbol requires a global offset table entry.  */
03140          if (htab->got == NULL)
03141            {
03142              if (htab->elf.dynobj == NULL)
03143               htab->elf.dynobj = abfd;
03144              if (!ppc_elf_create_got (htab->elf.dynobj, info))
03145               return FALSE;
03146            }
03147          if (h != NULL)
03148            {
03149              h->got.refcount += 1;
03150              ppc_elf_hash_entry (h)->tls_mask |= tls_type;
03151            }
03152          else
03153            /* This is a global offset table entry for a local symbol.  */
03154            if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
03155              return FALSE;
03156          break;
03157 
03158          /* Indirect .sdata relocation.  */
03159        case R_PPC_EMB_SDAI16:
03160          if (info->shared)
03161            {
03162              bad_shared_reloc (abfd, r_type);
03163              return FALSE;
03164            }
03165          if (htab->sdata[0].section == NULL
03166              && !ppc_elf_create_linker_section (abfd, info, 0,
03167                                            &htab->sdata[0]))
03168            return FALSE;
03169          if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
03170                                             h, rel))
03171            return FALSE;
03172          if (h != NULL)
03173            {
03174              ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
03175              h->non_got_ref = TRUE;
03176            }
03177          break;
03178 
03179          /* Indirect .sdata2 relocation.  */
03180        case R_PPC_EMB_SDA2I16:
03181          if (info->shared)
03182            {
03183              bad_shared_reloc (abfd, r_type);
03184              return FALSE;
03185            }
03186          if (htab->sdata[1].section == NULL
03187              && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
03188                                            &htab->sdata[1]))
03189            return FALSE;
03190          if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
03191                                             h, rel))
03192            return FALSE;
03193          if (h != NULL)
03194            {
03195              ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
03196              h->non_got_ref = TRUE;
03197            }
03198          break;
03199 
03200        case R_PPC_SDAREL16:
03201          if (info->shared)
03202            {
03203              bad_shared_reloc (abfd, r_type);
03204              return FALSE;
03205            }
03206          if (htab->sdata[0].sym == NULL
03207              && !create_sdata_sym (htab, &htab->sdata[0]))
03208            return FALSE;
03209          if (h != NULL)
03210            {
03211              ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
03212              h->non_got_ref = TRUE;
03213            }
03214          break;
03215 
03216        case R_PPC_EMB_SDA2REL:
03217          if (info->shared)
03218            {
03219              bad_shared_reloc (abfd, r_type);
03220              return FALSE;
03221            }
03222          if (htab->sdata[1].sym == NULL
03223              && !create_sdata_sym (htab, &htab->sdata[1]))
03224            return FALSE;
03225          if (h != NULL)
03226            {
03227              ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
03228              h->non_got_ref = TRUE;
03229            }
03230          break;
03231 
03232        case R_PPC_EMB_SDA21:
03233        case R_PPC_EMB_RELSDA:
03234          if (info->shared)
03235            {
03236              bad_shared_reloc (abfd, r_type);
03237              return FALSE;
03238            }
03239          if (htab->sdata[0].sym == NULL
03240              && !create_sdata_sym (htab, &htab->sdata[0]))
03241            return FALSE;
03242          if (htab->sdata[1].sym == NULL
03243              && !create_sdata_sym (htab, &htab->sdata[1]))
03244            return FALSE;
03245          if (h != NULL)
03246            {
03247              ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
03248              h->non_got_ref = TRUE;
03249            }
03250          break;
03251 
03252        case R_PPC_EMB_NADDR32:
03253        case R_PPC_EMB_NADDR16:
03254        case R_PPC_EMB_NADDR16_LO:
03255        case R_PPC_EMB_NADDR16_HI:
03256        case R_PPC_EMB_NADDR16_HA:
03257          if (info->shared)
03258            {
03259              bad_shared_reloc (abfd, r_type);
03260              return FALSE;
03261            }
03262          if (h != NULL)
03263            h->non_got_ref = TRUE;
03264          break;
03265 
03266        case R_PPC_PLT32:
03267        case R_PPC_PLTREL24:
03268        case R_PPC_PLTREL32:
03269        case R_PPC_PLT16_LO:
03270        case R_PPC_PLT16_HI:
03271        case R_PPC_PLT16_HA:
03272 #ifdef DEBUG
03273          fprintf (stderr, "Reloc requires a PLT entry\n");
03274 #endif
03275          /* This symbol requires a procedure linkage table entry.  We
03276             actually build the entry in finish_dynamic_symbol,
03277             because this might be a case of linking PIC code without
03278             linking in any dynamic objects, in which case we don't
03279             need to generate a procedure linkage table after all.  */
03280 
03281          if (h == NULL)
03282            {
03283              /* It does not make sense to have a procedure linkage
03284                table entry for a local symbol.  */
03285              (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
03286                                    "local symbol"),
03287                                  abfd,
03288                                  sec,
03289                                  (long) rel->r_offset,
03290                                  ppc_elf_howto_table[r_type]->name);
03291              bfd_set_error (bfd_error_bad_value);
03292              return FALSE;
03293            }
03294          else
03295            {
03296              bfd_vma addend = r_type == R_PPC_PLTREL24 ? rel->r_addend : 0;
03297 
03298              h->needs_plt = 1;
03299              if (!update_plt_info (abfd, h, got2, addend))
03300               return FALSE;
03301            }
03302          break;
03303 
03304          /* The following relocations don't need to propagate the
03305             relocation if linking a shared object since they are
03306             section relative.  */
03307        case R_PPC_SECTOFF:
03308        case R_PPC_SECTOFF_LO:
03309        case R_PPC_SECTOFF_HI:
03310        case R_PPC_SECTOFF_HA:
03311        case R_PPC_DTPREL16:
03312        case R_PPC_DTPREL16_LO:
03313        case R_PPC_DTPREL16_HI:
03314        case R_PPC_DTPREL16_HA:
03315        case R_PPC_TOC16:
03316          break;
03317 
03318        case R_PPC_REL16:
03319        case R_PPC_REL16_LO:
03320        case R_PPC_REL16_HI:
03321        case R_PPC_REL16_HA:
03322          htab->can_use_new_plt = 1;
03323          break;
03324 
03325          /* These are just markers.  */
03326        case R_PPC_TLS:
03327        case R_PPC_EMB_MRKREF:
03328        case R_PPC_NONE:
03329        case R_PPC_max:
03330          break;
03331 
03332          /* These should only appear in dynamic objects.  */
03333        case R_PPC_COPY:
03334        case R_PPC_GLOB_DAT:
03335        case R_PPC_JMP_SLOT:
03336        case R_PPC_RELATIVE:
03337          break;
03338 
03339          /* These aren't handled yet.  We'll report an error later.  */
03340        case R_PPC_ADDR30:
03341        case R_PPC_EMB_RELSEC16:
03342        case R_PPC_EMB_RELST_LO:
03343        case R_PPC_EMB_RELST_HI:
03344        case R_PPC_EMB_RELST_HA:
03345        case R_PPC_EMB_BIT_FLD:
03346          break;
03347 
03348          /* This refers only to functions defined in the shared library.  */
03349        case R_PPC_LOCAL24PC:
03350          if (h && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
03351            htab->plt_type = PLT_OLD;
03352          break;
03353 
03354          /* This relocation describes the C++ object vtable hierarchy.
03355             Reconstruct it for later use during GC.  */
03356        case R_PPC_GNU_VTINHERIT:
03357          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
03358            return FALSE;
03359          break;
03360 
03361          /* This relocation describes which C++ vtable entries are actually
03362             used.  Record for later use during GC.  */
03363        case R_PPC_GNU_VTENTRY:
03364          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
03365            return FALSE;
03366          break;
03367 
03368          /* We shouldn't really be seeing these.  */
03369        case R_PPC_TPREL32:
03370          if (info->shared)
03371            info->flags |= DF_STATIC_TLS;
03372          goto dodyn;
03373 
03374          /* Nor these.  */
03375        case R_PPC_DTPMOD32:
03376        case R_PPC_DTPREL32:
03377          goto dodyn;
03378 
03379        case R_PPC_TPREL16:
03380        case R_PPC_TPREL16_LO:
03381        case R_PPC_TPREL16_HI:
03382        case R_PPC_TPREL16_HA:
03383          if (info->shared)
03384            info->flags |= DF_STATIC_TLS;
03385          goto dodyn;
03386 
03387        case R_PPC_REL32:
03388          if (h == NULL
03389              && got2 != NULL
03390              && (sec->flags & SEC_CODE) != 0
03391              && (info->shared || info->pie)
03392              && htab->plt_type == PLT_UNSET)
03393            {
03394              /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
03395                the start of a function, which assembles to a REL32
03396                reference to .got2.  If we detect one of these, then
03397                force the old PLT layout because the linker cannot
03398                reliably deduce the GOT pointer value needed for
03399                PLT call stubs.  */
03400              asection *s;
03401 
03402              s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
03403                                         r_symndx);
03404              if (s == got2)
03405               htab->plt_type = PLT_OLD;
03406            }
03407          if (h == NULL || h == htab->elf.hgot)
03408            break;
03409          goto dodyn1;
03410 
03411        case R_PPC_REL24:
03412        case R_PPC_REL14:
03413        case R_PPC_REL14_BRTAKEN:
03414        case R_PPC_REL14_BRNTAKEN:
03415          if (h == NULL)
03416            break;
03417          if (h == htab->elf.hgot)
03418            {
03419              if (htab->plt_type == PLT_UNSET)
03420               htab->plt_type = PLT_OLD;
03421              break;
03422            }
03423          /* fall through */
03424 
03425        case R_PPC_ADDR32:
03426        case R_PPC_ADDR24:
03427        case R_PPC_ADDR16:
03428        case R_PPC_ADDR16_LO:
03429        case R_PPC_ADDR16_HI:
03430        case R_PPC_ADDR16_HA:
03431        case R_PPC_ADDR14:
03432        case R_PPC_ADDR14_BRTAKEN:
03433        case R_PPC_ADDR14_BRNTAKEN:
03434        case R_PPC_UADDR32:
03435        case R_PPC_UADDR16:
03436        dodyn1:
03437          if (h != NULL && !info->shared)
03438            {
03439              /* We may need a plt entry if the symbol turns out to be
03440                a function defined in a dynamic object.  */
03441              if (!update_plt_info (abfd, h, NULL, 0))
03442               return FALSE;
03443 
03444              /* We may need a copy reloc too.  */
03445              h->non_got_ref = 1;
03446            }
03447 
03448        dodyn:
03449          /* If we are creating a shared library, and this is a reloc
03450             against a global symbol, or a non PC relative reloc
03451             against a local symbol, then we need to copy the reloc
03452             into the shared library.  However, if we are linking with
03453             -Bsymbolic, we do not need to copy a reloc against a
03454             global symbol which is defined in an object we are
03455             including in the link (i.e., DEF_REGULAR is set).  At
03456             this point we have not seen all the input files, so it is
03457             possible that DEF_REGULAR is not set now but will be set
03458             later (it is never cleared).  In case of a weak definition,
03459             DEF_REGULAR may be cleared later by a strong definition in
03460             a shared library.  We account for that possibility below by
03461             storing information in the dyn_relocs field of the hash
03462             table entry.  A similar situation occurs when creating
03463             shared libraries and symbol visibility changes render the
03464             symbol local.
03465 
03466             If on the other hand, we are creating an executable, we
03467             may need to keep relocations for symbols satisfied by a
03468             dynamic library if we manage to avoid copy relocs for the
03469             symbol.  */
03470          if ((info->shared
03471               && (MUST_BE_DYN_RELOC (r_type)
03472                  || (h != NULL
03473                      && (! info->symbolic
03474                         || h->root.type == bfd_link_hash_defweak
03475                         || !h->def_regular))))
03476              || (ELIMINATE_COPY_RELOCS
03477                 && !info->shared
03478                 && h != NULL
03479                 && (h->root.type == bfd_link_hash_defweak
03480                     || !h->def_regular)))
03481            {
03482              struct ppc_elf_dyn_relocs *p;
03483              struct ppc_elf_dyn_relocs **head;
03484 
03485 #ifdef DEBUG
03486              fprintf (stderr,
03487                      "ppc_elf_check_relocs needs to "
03488                      "create relocation for %s\n",
03489                      (h && h->root.root.string
03490                      ? h->root.root.string : "<unknown>"));
03491 #endif
03492              if (sreloc == NULL)
03493               {
03494                 const char *name;
03495 
03496                 name = (bfd_elf_string_from_elf_section
03497                        (abfd,
03498                         elf_elfheader (abfd)->e_shstrndx,
03499                         elf_section_data (sec)->rel_hdr.sh_name));
03500                 if (name == NULL)
03501                   return FALSE;
03502 
03503                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
03504                            && strcmp (bfd_get_section_name (abfd, sec),
03505                                     name + 5) == 0);
03506 
03507                 if (htab->elf.dynobj == NULL)
03508                   htab->elf.dynobj = abfd;
03509                 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
03510                 if (sreloc == NULL)
03511                   {
03512                     flagword flags;
03513 
03514                     flags = (SEC_HAS_CONTENTS | SEC_READONLY
03515                             | SEC_IN_MEMORY | SEC_LINKER_CREATED
03516                             | SEC_ALLOC | SEC_LOAD);
03517                     sreloc = bfd_make_section_with_flags (htab->elf.dynobj,
03518                                                      name,
03519                                                      flags);
03520                     if (sreloc == NULL
03521                        || ! bfd_set_section_alignment (htab->elf.dynobj,
03522                                                    sreloc, 2))
03523                      return FALSE;
03524                   }
03525                 elf_section_data (sec)->sreloc = sreloc;
03526               }
03527 
03528              /* If this is a global symbol, we count the number of
03529                relocations we need for this symbol.  */
03530              if (h != NULL)
03531               {
03532                 head = &ppc_elf_hash_entry (h)->dyn_relocs;
03533               }
03534              else
03535               {
03536                 /* Track dynamic relocs needed for local syms too.
03537                    We really need local syms available to do this
03538                    easily.  Oh well.  */
03539 
03540                 asection *s;
03541                 void *vpp;
03542 
03543                 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
03544                                            sec, r_symndx);
03545                 if (s == NULL)
03546                   return FALSE;
03547 
03548                 vpp = &elf_section_data (s)->local_dynrel;
03549                 head = (struct ppc_elf_dyn_relocs **) vpp;
03550               }
03551 
03552              p = *head;
03553              if (p == NULL || p->sec != sec)
03554               {
03555                 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
03556                 if (p == NULL)
03557                   return FALSE;
03558                 p->next = *head;
03559                 *head = p;
03560                 p->sec = sec;
03561                 p->count = 0;
03562                 p->pc_count = 0;
03563               }
03564 
03565              p->count += 1;
03566              if (!MUST_BE_DYN_RELOC (r_type))
03567               p->pc_count += 1;
03568            }
03569 
03570          break;
03571        }
03572     }
03573 
03574   return TRUE;
03575 }
03576 
03577 /* Merge backend specific data from an object file to the output
03578    object file when linking.  */
03579 
03580 static bfd_boolean
03581 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
03582 {
03583   flagword old_flags;
03584   flagword new_flags;
03585   bfd_boolean error;
03586 
03587   if (!is_ppc_elf_target (ibfd->xvec)
03588       || !is_ppc_elf_target (obfd->xvec))
03589     return TRUE;
03590 
03591   /* Check if we have the same endianess.  */
03592   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
03593     return FALSE;
03594 
03595   new_flags = elf_elfheader (ibfd)->e_flags;
03596   old_flags = elf_elfheader (obfd)->e_flags;
03597   if (!elf_flags_init (obfd))
03598     {
03599       /* First call, no flags set.  */
03600       elf_flags_init (obfd) = TRUE;
03601       elf_elfheader (obfd)->e_flags = new_flags;
03602     }
03603 
03604   /* Compatible flags are ok.  */
03605   else if (new_flags == old_flags)
03606     ;
03607 
03608   /* Incompatible flags.  */
03609   else
03610     {
03611       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
03612         to be linked with either.  */
03613       error = FALSE;
03614       if ((new_flags & EF_PPC_RELOCATABLE) != 0
03615          && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
03616        {
03617          error = TRUE;
03618          (*_bfd_error_handler)
03619            (_("%B: compiled with -mrelocatable and linked with "
03620               "modules compiled normally"), ibfd);
03621        }
03622       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
03623               && (old_flags & EF_PPC_RELOCATABLE) != 0)
03624        {
03625          error = TRUE;
03626          (*_bfd_error_handler)
03627            (_("%B: compiled normally and linked with "
03628               "modules compiled with -mrelocatable"), ibfd);
03629        }
03630 
03631       /* The output is -mrelocatable-lib iff both the input files are.  */
03632       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
03633        elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
03634 
03635       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
03636         but each input file is either -mrelocatable or -mrelocatable-lib.  */
03637       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
03638          && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
03639          && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
03640        elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
03641 
03642       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
03643         any module uses it.  */
03644       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
03645 
03646       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
03647       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
03648 
03649       /* Warn about any other mismatches.  */
03650       if (new_flags != old_flags)
03651        {
03652          error = TRUE;
03653          (*_bfd_error_handler)
03654            (_("%B: uses different e_flags (0x%lx) fields "
03655               "than previous modules (0x%lx)"),
03656             ibfd, (long) new_flags, (long) old_flags);
03657        }
03658 
03659       if (error)
03660        {
03661          bfd_set_error (bfd_error_bad_value);
03662          return FALSE;
03663        }
03664     }
03665 
03666   return TRUE;
03667 }
03668 
03669 /* Choose which PLT scheme to use, and set .plt flags appropriately.
03670    Returns -1 on error, 0 for old PLT, 1 for new PLT.  */
03671 int
03672 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
03673                         struct bfd_link_info *info,
03674                         int force_old_plt,
03675                         int emit_stub_syms)
03676 {
03677   struct ppc_elf_link_hash_table *htab;
03678   flagword flags;
03679 
03680   htab = ppc_elf_hash_table (info);
03681 
03682   if (htab->plt_type == PLT_UNSET)
03683     htab->plt_type = (force_old_plt || !htab->can_use_new_plt
03684                     ? PLT_OLD : PLT_NEW);
03685 
03686   htab->emit_stub_syms = emit_stub_syms;
03687 
03688   BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
03689 
03690   if (htab->plt_type == PLT_NEW)
03691     {
03692       flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
03693               | SEC_IN_MEMORY | SEC_LINKER_CREATED);
03694 
03695       /* The new PLT is a loaded section.  */
03696       if (htab->plt != NULL
03697          && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
03698        return -1;
03699 
03700       /* The new GOT is not executable.  */
03701       if (htab->got != NULL
03702          && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
03703        return -1;
03704     }
03705   else
03706     {
03707       /* Stop an unused .glink section from affecting .text alignment.  */
03708       if (htab->glink != NULL
03709          && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
03710        return -1;
03711     }
03712   return htab->plt_type == PLT_NEW;
03713 }
03714 
03715 /* Return the section that should be marked against GC for a given
03716    relocation.  */
03717 
03718 static asection *
03719 ppc_elf_gc_mark_hook (asection *sec,
03720                     struct bfd_link_info *info,
03721                     Elf_Internal_Rela *rel,
03722                     struct elf_link_hash_entry *h,
03723                     Elf_Internal_Sym *sym)
03724 {
03725   if (h != NULL)
03726     switch (ELF32_R_TYPE (rel->r_info))
03727       {
03728       case R_PPC_GNU_VTINHERIT:
03729       case R_PPC_GNU_VTENTRY:
03730        return NULL;
03731       }
03732 
03733   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
03734 }
03735 
03736 /* Update the got, plt and dynamic reloc reference counts for the
03737    section being removed.  */
03738 
03739 static bfd_boolean
03740 ppc_elf_gc_sweep_hook (bfd *abfd,
03741                      struct bfd_link_info *info,
03742                      asection *sec,
03743                      const Elf_Internal_Rela *relocs)
03744 {
03745   struct ppc_elf_link_hash_table *htab;
03746   Elf_Internal_Shdr *symtab_hdr;
03747   struct elf_link_hash_entry **sym_hashes;
03748   bfd_signed_vma *local_got_refcounts;
03749   const Elf_Internal_Rela *rel, *relend;
03750   asection *got2;
03751 
03752   if ((sec->flags & SEC_ALLOC) == 0)
03753     return TRUE;
03754 
03755   elf_section_data (sec)->local_dynrel = NULL;
03756 
03757   htab = ppc_elf_hash_table (info);
03758   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
03759   sym_hashes = elf_sym_hashes (abfd);
03760   local_got_refcounts = elf_local_got_refcounts (abfd);
03761   got2 = bfd_get_section_by_name (abfd, ".got2");
03762 
03763   relend = relocs + sec->reloc_count;
03764   for (rel = relocs; rel < relend; rel++)
03765     {
03766       unsigned long r_symndx;
03767       enum elf_ppc_reloc_type r_type;
03768       struct elf_link_hash_entry *h = NULL;
03769 
03770       r_symndx = ELF32_R_SYM (rel->r_info);
03771       if (r_symndx >= symtab_hdr->sh_info)
03772        {
03773          struct ppc_elf_dyn_relocs **pp, *p;
03774          struct ppc_elf_link_hash_entry *eh;
03775 
03776          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
03777          while (h->root.type == bfd_link_hash_indirect
03778                || h->root.type == bfd_link_hash_warning)
03779            h = (struct elf_link_hash_entry *) h->root.u.i.link;
03780          eh = (struct ppc_elf_link_hash_entry *) h;
03781 
03782          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
03783            if (p->sec == sec)
03784              {
03785               /* Everything must go for SEC.  */
03786               *pp = p->next;
03787               break;
03788              }
03789        }
03790 
03791       r_type = ELF32_R_TYPE (rel->r_info);
03792       switch (r_type)
03793        {
03794        case R_PPC_GOT_TLSLD16:
03795        case R_PPC_GOT_TLSLD16_LO:
03796        case R_PPC_GOT_TLSLD16_HI:
03797        case R_PPC_GOT_TLSLD16_HA:
03798          htab->tlsld_got.refcount -= 1;
03799          /* Fall thru */
03800 
03801        case R_PPC_GOT_TLSGD16:
03802        case R_PPC_GOT_TLSGD16_LO:
03803        case R_PPC_GOT_TLSGD16_HI:
03804        case R_PPC_GOT_TLSGD16_HA:
03805        case R_PPC_GOT_TPREL16:
03806        case R_PPC_GOT_TPREL16_LO:
03807        case R_PPC_GOT_TPREL16_HI:
03808        case R_PPC_GOT_TPREL16_HA:
03809        case R_PPC_GOT_DTPREL16:
03810        case R_PPC_GOT_DTPREL16_LO:
03811        case R_PPC_GOT_DTPREL16_HI:
03812        case R_PPC_GOT_DTPREL16_HA:
03813        case R_PPC_GOT16:
03814        case R_PPC_GOT16_LO:
03815        case R_PPC_GOT16_HI:
03816        case R_PPC_GOT16_HA:
03817          if (h != NULL)
03818            {
03819              if (h->got.refcount > 0)
03820               h->got.refcount--;
03821            }
03822          else if (local_got_refcounts != NULL)
03823            {
03824              if (local_got_refcounts[r_symndx] > 0)
03825               local_got_refcounts[r_symndx]--;
03826            }
03827          break;
03828 
03829        case R_PPC_REL24:
03830        case R_PPC_REL14:
03831        case R_PPC_REL14_BRTAKEN:
03832        case R_PPC_REL14_BRNTAKEN:
03833        case R_PPC_REL32:
03834          if (h == NULL || h == htab->elf.hgot)
03835            break;
03836          /* Fall thru */
03837 
03838        case R_PPC_ADDR32:
03839        case R_PPC_ADDR24:
03840        case R_PPC_ADDR16:
03841        case R_PPC_ADDR16_LO:
03842        case R_PPC_ADDR16_HI:
03843        case R_PPC_ADDR16_HA:
03844        case R_PPC_ADDR14:
03845        case R_PPC_ADDR14_BRTAKEN:
03846        case R_PPC_ADDR14_BRNTAKEN:
03847        case R_PPC_UADDR32:
03848        case R_PPC_UADDR16:
03849          if (info->shared)
03850            break;
03851 
03852        case R_PPC_PLT32:
03853        case R_PPC_PLTREL24:
03854        case R_PPC_PLTREL32:
03855        case R_PPC_PLT16_LO:
03856        case R_PPC_PLT16_HI:
03857        case R_PPC_PLT16_HA:
03858          if (h != NULL)
03859            {
03860              bfd_vma addend = r_type == R_PPC_PLTREL24 ? rel->r_addend : 0;
03861              struct plt_entry *ent = find_plt_ent (h, got2, addend);
03862              if (ent->plt.refcount > 0)
03863               ent->plt.refcount -= 1;
03864            }
03865          break;
03866 
03867        default:
03868          break;
03869        }
03870     }
03871   return TRUE;
03872 }
03873 
03874 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
03875 
03876 asection *
03877 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
03878 {
03879   struct ppc_elf_link_hash_table *htab;
03880 
03881   htab = ppc_elf_hash_table (info);
03882   if (htab->plt_type == PLT_NEW
03883       && htab->plt != NULL
03884       && htab->plt->output_section != NULL)
03885     {
03886       elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
03887       elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
03888     }
03889 
03890   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
03891                                         FALSE, FALSE, TRUE);
03892   return _bfd_elf_tls_setup (obfd, info);
03893 }
03894 
03895 /* Run through all the TLS relocs looking for optimization
03896    opportunities.  */
03897 
03898 bfd_boolean
03899 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
03900                     struct bfd_link_info *info)
03901 {
03902   bfd *ibfd;
03903   asection *sec;
03904   struct ppc_elf_link_hash_table *htab;
03905 
03906   if (info->relocatable || info->shared)
03907     return TRUE;
03908 
03909   htab = ppc_elf_hash_table (info);
03910   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
03911     {
03912       Elf_Internal_Sym *locsyms = NULL;
03913       Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
03914 
03915       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
03916        if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
03917          {
03918            Elf_Internal_Rela *relstart, *rel, *relend;
03919            int expecting_tls_get_addr;
03920 
03921            /* Read the relocations.  */
03922            relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
03923                                             info->keep_memory);
03924            if (relstart == NULL)
03925              return FALSE;
03926 
03927            expecting_tls_get_addr = 0;
03928            relend = relstart + sec->reloc_count;
03929            for (rel = relstart; rel < relend; rel++)
03930              {
03931               enum elf_ppc_reloc_type r_type;
03932               unsigned long r_symndx;
03933               struct elf_link_hash_entry *h = NULL;
03934               char *tls_mask;
03935               char tls_set, tls_clear;
03936               bfd_boolean is_local;
03937 
03938               r_symndx = ELF32_R_SYM (rel->r_info);
03939               if (r_symndx >= symtab_hdr->sh_info)
03940                 {
03941                   struct elf_link_hash_entry **sym_hashes;
03942 
03943                   sym_hashes = elf_sym_hashes (ibfd);
03944                   h = sym_hashes[r_symndx - symtab_hdr->sh_info];
03945                   while (h->root.type == bfd_link_hash_indirect
03946                         || h->root.type == bfd_link_hash_warning)
03947                     h = (struct elf_link_hash_entry *) h->root.u.i.link;
03948                 }
03949 
03950               is_local = FALSE;
03951               if (h == NULL
03952                   || !h->def_dynamic)
03953                 is_local = TRUE;
03954 
03955               r_type = ELF32_R_TYPE (rel->r_info);
03956               switch (r_type)
03957                 {
03958                 case R_PPC_GOT_TLSLD16:
03959                 case R_PPC_GOT_TLSLD16_LO:
03960                 case R_PPC_GOT_TLSLD16_HI:
03961                 case R_PPC_GOT_TLSLD16_HA:
03962                   /* These relocs should never be against a symbol
03963                      defined in a shared lib.  Leave them alone if
03964                      that turns out to be the case.  */
03965                   expecting_tls_get_addr = 0;
03966                   htab->tlsld_got.refcount -= 1;
03967                   if (!is_local)
03968                     continue;
03969 
03970                   /* LD -> LE */
03971                   tls_set = 0;
03972                   tls_clear = TLS_LD;
03973                   expecting_tls_get_addr = 1;
03974                   break;
03975 
03976                 case R_PPC_GOT_TLSGD16:
03977                 case R_PPC_GOT_TLSGD16_LO:
03978                 case R_PPC_GOT_TLSGD16_HI:
03979                 case R_PPC_GOT_TLSGD16_HA:
03980                   if (is_local)
03981                     /* GD -> LE */
03982                     tls_set = 0;
03983                   else
03984                     /* GD -> IE */
03985                     tls_set = TLS_TLS | TLS_TPRELGD;
03986                   tls_clear = TLS_GD;
03987                   expecting_tls_get_addr = 1;
03988                   break;
03989 
03990                 case R_PPC_GOT_TPREL16:
03991                 case R_PPC_GOT_TPREL16_LO:
03992                 case R_PPC_GOT_TPREL16_HI:
03993                 case R_PPC_GOT_TPREL16_HA:
03994                   expecting_tls_get_addr = 0;
03995                   if (is_local)
03996                     {
03997                      /* IE -> LE */
03998                      tls_set = 0;
03999                      tls_clear = TLS_TPREL;
04000                      break;
04001                     }
04002                   else
04003                     continue;
04004 
04005                 case R_PPC_REL14:
04006                 case R_PPC_REL14_BRTAKEN:
04007                 case R_PPC_REL14_BRNTAKEN:
04008                 case R_PPC_REL24:
04009                   if (expecting_tls_get_addr
04010                      && h != NULL
04011                      && h == htab->tls_get_addr)
04012                     {
04013                      struct plt_entry *ent = find_plt_ent (h, NULL, 0);
04014                      if (ent != NULL && ent->plt.refcount > 0)
04015                        ent->plt.refcount -= 1;
04016                     }
04017                   expecting_tls_get_addr = 0;
04018                   continue;
04019 
04020                 default:
04021                   expecting_tls_get_addr = 0;
04022                   continue;
04023                 }
04024 
04025               if (h != NULL)
04026                 {
04027                   if (tls_set == 0)
04028                     {
04029                      /* We managed to get rid of a got entry.  */
04030                      if (h->got.refcount > 0)
04031                        h->got.refcount -= 1;
04032                     }
04033                   tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
04034                 }
04035               else
04036                 {
04037                   Elf_Internal_Sym *sym;
04038                   bfd_signed_vma *lgot_refs;
04039                   char *lgot_masks;
04040 
04041                   if (locsyms == NULL)
04042                     {
04043                      locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
04044                      if (locsyms == NULL)
04045                        locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
04046                                                    symtab_hdr->sh_info,
04047                                                    0, NULL, NULL, NULL);
04048                      if (locsyms == NULL)
04049                        {
04050                          if (elf_section_data (sec)->relocs != relstart)
04051                            free (relstart);
04052                          return FALSE;
04053                        }
04054                     }
04055                   sym = locsyms + r_symndx;
04056                   lgot_refs = elf_local_got_refcounts (ibfd);
04057                   if (lgot_refs == NULL)
04058                     abort ();
04059                   if (tls_set == 0)
04060                     {
04061                      /* We managed to get rid of a got entry.  */
04062                      if (lgot_refs[r_symndx] > 0)
04063                        lgot_refs[r_symndx] -= 1;
04064                     }
04065                   lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
04066                   tls_mask = &lgot_masks[r_symndx];
04067                 }
04068 
04069               *tls_mask |= tls_set;
04070               *tls_mask &= ~tls_clear;
04071              }
04072 
04073            if (elf_section_data (sec)->relocs != relstart)
04074              free (relstart);
04075          }
04076 
04077       if (locsyms != NULL
04078          && (symtab_hdr->contents != (unsigned char *) locsyms))
04079        {
04080          if (!info->keep_memory)
04081            free (locsyms);
04082          else
04083            symtab_hdr->contents = (unsigned char *) locsyms;
04084        }
04085     }
04086   return TRUE;
04087 }
04088 
04089 /* Adjust a symbol defined by a dynamic object and referenced by a
04090    regular object.  The current definition is in some section of the
04091    dynamic object, but we're not including those sections.  We have to
04092    change the definition to something the rest of the link can
04093    understand.  */
04094 
04095 static bfd_boolean
04096 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
04097                             struct elf_link_hash_entry *h)
04098 {
04099   struct ppc_elf_link_hash_table *htab;
04100   asection *s;
04101   unsigned int power_of_two;
04102 
04103 #ifdef DEBUG
04104   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
04105           h->root.root.string);
04106 #endif
04107 
04108   /* Make sure we know what is going on here.  */
04109   htab = ppc_elf_hash_table (info);
04110   BFD_ASSERT (htab->elf.dynobj != NULL
04111              && (h->needs_plt
04112                 || h->u.weakdef != NULL
04113                 || (h->def_dynamic
04114                     && h->ref_regular
04115                     && !h->def_regular)));
04116 
04117   /* Deal with function syms.  */
04118   if (h->type == STT_FUNC
04119       || h->needs_plt)
04120     {
04121       /* Clear procedure linkage table information for any symbol that
04122         won't need a .plt entry.  */
04123       struct plt_entry *ent;
04124       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
04125        if (ent->plt.refcount > 0)
04126          break;
04127       if (ent == NULL
04128          || SYMBOL_CALLS_LOCAL (info, h)
04129          || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
04130              && h->root.type == bfd_link_hash_undefweak))
04131        {
04132          /* A PLT entry is not required/allowed when:
04133 
04134             1. We are not using ld.so; because then the PLT entry
04135             can't be set up, so we can't use one.  In this case,
04136             ppc_elf_adjust_dynamic_symbol won't even be called.
04137 
04138             2. GC has rendered the entry unused.
04139 
04140             3. We know for certain that a call to this symbol
04141             will go to this object, or will remain undefined.  */
04142          h->plt.plist = NULL;
04143          h->needs_plt = 0;
04144        }
04145       return TRUE;
04146     }
04147   else
04148     h->plt.plist = NULL;
04149 
04150   /* If this is a weak symbol, and there is a real definition, the
04151      processor independent code will have arranged for us to see the
04152      real definition first, and we can just use the same value.  */
04153   if (h->u.weakdef != NULL)
04154     {
04155       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
04156                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
04157       h->root.u.def.section = h->u.weakdef->root.u.def.section;
04158       h->root.u.def.value = h->u.weakdef->root.u.def.value;
04159       if (ELIMINATE_COPY_RELOCS)
04160        h->non_got_ref = h->u.weakdef->non_got_ref;
04161       return TRUE;
04162     }
04163 
04164   /* This is a reference to a symbol defined by a dynamic object which
04165      is not a function.  */
04166 
04167   /* If we are creating a shared library, we must presume that the
04168      only references to the symbol are via the global offset table.
04169      For such cases we need not do anything here; the relocations will
04170      be handled correctly by relocate_section.  */
04171   if (info->shared)
04172     return TRUE;
04173 
04174   /* If there are no references to this symbol that do not use the
04175      GOT, we don't need to generate a copy reloc.  */
04176   if (!h->non_got_ref)
04177     return TRUE;
04178 
04179    /* If we didn't find any dynamic relocs in read-only sections, then we'll
04180       be keeping the dynamic relocs and avoiding the copy reloc.  We can't
04181       do this if there are any small data relocations.  */
04182   if (ELIMINATE_COPY_RELOCS
04183       && !ppc_elf_hash_entry (h)->has_sda_refs)
04184     {
04185       struct ppc_elf_dyn_relocs *p;
04186       for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
04187        {
04188          s = p->sec->output_section;
04189          if (s != NULL && (s->flags & SEC_READONLY) != 0)
04190            break;
04191        }
04192 
04193       if (p == NULL)
04194        {
04195          h->non_got_ref = 0;
04196          return TRUE;
04197        }
04198     }
04199 
04200   if (h->size == 0)
04201     {
04202       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
04203                           h->root.root.string);
04204       return TRUE;
04205     }
04206 
04207   /* We must allocate the symbol in our .dynbss section, which will
04208      become part of the .bss section of the executable.  There will be
04209      an entry for this symbol in the .dynsym section.  The dynamic
04210      object will contain position independent code, so all references
04211      from the dynamic object to this symbol will go through the global
04212      offset table.  The dynamic linker will use the .dynsym entry to
04213      determine the address it must put in the global offset table, so
04214      both the dynamic object and the regular object will refer to the
04215      same memory location for the variable.
04216 
04217      Of course, if the symbol is referenced using SDAREL relocs, we
04218      must instead allocate it in .sbss.  */
04219 
04220   if (ppc_elf_hash_entry (h)->has_sda_refs)
04221     s = htab->dynsbss;
04222   else
04223     s = htab->dynbss;
04224   BFD_ASSERT (s != NULL);
04225 
04226   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
04227      copy the initial value out of the dynamic object and into the
04228      runtime process image.  We need to remember the offset into the
04229      .rela.bss section we are going to use.  */
04230   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
04231     {
04232       asection *srel;
04233 
04234       if (ppc_elf_hash_entry (h)->has_sda_refs)
04235        srel = htab->relsbss;
04236       else
04237        srel = htab->relbss;
04238       BFD_ASSERT (srel != NULL);
04239       srel->size += sizeof (Elf32_External_Rela);
04240       h->needs_copy = 1;
04241     }
04242 
04243   /* We need to figure out the alignment required for this symbol.  I
04244      have no idea how ELF linkers handle this.  */
04245   power_of_two = bfd_log2 (h->size);
04246   if (power_of_two > 4)
04247     power_of_two = 4;
04248 
04249   /* Apply the required alignment.  */
04250   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
04251   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
04252     {
04253       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
04254        return FALSE;
04255     }
04256 
04257   /* Define the symbol as being at this point in the section.  */
04258   h->root.u.def.section = s;
04259   h->root.u.def.value = s->size;
04260 
04261   /* Increment the section size to make room for the symbol.  */
04262   s->size += h->size;
04263 
04264   return TRUE;
04265 }
04266 
04267 /* Generate a symbol to mark plt call stubs.  For non-PIC code the sym is
04268    xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
04269    specifying the addend on the plt relocation.  For -fpic code, the sym
04270    is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
04271    xxxxxxxx.got2.plt_pic32.<callee>.  */
04272 
04273 static bfd_boolean
04274 add_stub_sym (struct plt_entry *ent,
04275              struct elf_link_hash_entry *h,
04276              struct bfd_link_info *info)
04277 {
04278   struct elf_link_hash_entry *sh;
04279   size_t len1, len2, len3;
04280   char *name;
04281   const char *stub;
04282   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
04283 
04284   if (info->shared || info->pie)
04285     stub = ".plt_pic32.";
04286   else
04287     stub = ".plt_call32.";
04288 
04289   len1 = strlen (h->root.root.string);
04290   len2 = strlen (stub);
04291   len3 = 0;
04292   if (ent->sec)
04293     len3 = strlen (ent->sec->name);
04294   name = bfd_malloc (len1 + len2 + len3 + 9);
04295   if (name == NULL)
04296     return FALSE;
04297   sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
04298   if (ent->sec)
04299     memcpy (name + 8, ent->sec->name, len3);
04300   memcpy (name + 8 + len3, stub, len2);
04301   memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
04302   sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
04303   if (sh == NULL)
04304     return FALSE;
04305   if (sh->root.type == bfd_link_hash_new)
04306     {
04307       sh->root.type = bfd_link_hash_defined;
04308       sh->root.u.def.section = htab->glink;
04309       sh->root.u.def.value = ent->glink_offset;
04310       sh->ref_regular = 1;
04311       sh->def_regular = 1;
04312       sh->ref_regular_nonweak = 1;
04313       sh->forced_local = 1;
04314       sh->non_elf = 0;
04315     }
04316   return TRUE;
04317 }
04318 
04319 /* Allocate NEED contiguous space in .got, and return the offset.
04320    Handles allocation of the got header when crossing 32k.  */
04321 
04322 static bfd_vma
04323 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
04324 {
04325   bfd_vma where;
04326   unsigned int max_before_header;
04327 
04328   if (htab->plt_type == PLT_VXWORKS)
04329     {
04330       where = htab->got->size;
04331       htab->got->size += need;
04332     }
04333   else
04334     {
04335       max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
04336       if (need <= htab->got_gap)
04337        {
04338          where = max_before_header - htab->got_gap;
04339          htab->got_gap -= need;
04340        }
04341       else
04342        {
04343          if (htab->got->size + need > max_before_header
04344              && htab->got->size <= max_before_header)
04345            {
04346              htab->got_gap = max_before_header - htab->got->size;
04347              htab->got->size = max_before_header + htab->got_header_size;
04348            }
04349          where = htab->got->size;
04350          htab->got->size += need;
04351        }
04352     }
04353   return where;
04354 }
04355 
04356 /* Allocate space in associated reloc sections for dynamic relocs.  */
04357 
04358 static bfd_boolean
04359 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
04360 {
04361   struct bfd_link_info *info = inf;
04362   struct ppc_elf_link_hash_entry *eh;
04363   struct ppc_elf_link_hash_table *htab;
04364   struct ppc_elf_dyn_relocs *p;
04365 
04366   if (h->root.type == bfd_link_hash_indirect)
04367     return TRUE;
04368 
04369   if (h->root.type == bfd_link_hash_warning)
04370     /* When warning symbols are created, they **replace** the "real"
04371        entry in the hash table, thus we never get to see the real
04372        symbol in a hash traversal.  So look at it now.  */
04373     h = (struct elf_link_hash_entry *) h->root.u.i.link;
04374 
04375   htab = ppc_elf_hash_table (info);
04376   if (htab->elf.dynamic_sections_created)
04377     {
04378       struct plt_entry *ent;
04379       bfd_boolean doneone = FALSE;
04380       bfd_vma plt_offset = 0, glink_offset = 0;
04381 
04382       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
04383        if (ent->plt.refcount > 0)
04384          {
04385            /* Make sure this symbol is output as a dynamic symbol.  */
04386            if (h->dynindx == -1
04387               && !h->forced_local)
04388              {
04389               if (! bfd_elf_link_record_dynamic_symbol (info, h))
04390                 return FALSE;
04391              }
04392 
04393            if (info->shared
04394               || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
04395              {
04396               asection *s = htab->plt;
04397 
04398               if (htab->plt_type == PLT_NEW)
04399                 {
04400                   if (!doneone)
04401                     {
04402                      plt_offset = s->size;
04403                      s->size += 4;
04404                     }
04405                   ent->plt.offset = plt_offset;
04406 
04407                   s = htab->glink;
04408                   if (!doneone || info->shared || info->pie)
04409                     {
04410                      glink_offset = s->size;
04411                      s->size += GLINK_ENTRY_SIZE;
04412                     }
04413                   if (!doneone
04414                      && !info->shared
04415                      && !h->def_regular)
04416                     {
04417                      h->root.u.def.section = s;
04418                      h->root.u.def.value = glink_offset;
04419                     }
04420                   ent->glink_offset = glink_offset;
04421 
04422                   if (htab->emit_stub_syms
04423                      && !add_stub_sym (ent, h, info))
04424                     return FALSE;
04425                 }
04426               else
04427                 {
04428                   if (!doneone)
04429                     {
04430                      /* If this is the first .plt entry, make room
04431                         for the special first entry.  */
04432                      if (s->size == 0)
04433                        s->size += htab->plt_initial_entry_size;
04434 
04435                      /* The PowerPC PLT is actually composed of two
04436                         parts, the first part is 2 words (for a load
04437                         and a jump), and then there is a remaining
04438                         word available at the end.  */
04439                      plt_offset = (htab->plt_initial_entry_size
04440                                   + (htab->plt_slot_size
04441                                     * ((s->size
04442                                         - htab->plt_initial_entry_size)
04443                                        / htab->plt_entry_size)));
04444 
04445                      /* If this symbol is not defined in a regular
04446                         file, and we are not generating a shared
04447                         library, then set the symbol to this location
04448                         in the .plt.  This is required to make
04449                         function pointers compare as equal between
04450                         the normal executable and the shared library.  */
04451                      if (! info->shared
04452                          && !h->def_regular)
04453                        {
04454                          h->root.u.def.section = s;
04455                          h->root.u.def.value = plt_offset;
04456                        }
04457 
04458                      /* Make room for this entry.  */
04459                      s->size += htab->plt_entry_size;
04460                      /* After the 8192nd entry, room for two entries
04461                         is allocated.  */
04462                      if (htab->plt_type == PLT_OLD
04463                          && (s->size - htab->plt_initial_entry_size)
04464                             / htab->plt_entry_size
04465                             > PLT_NUM_SINGLE_ENTRIES)
04466                        s->size += htab->plt_entry_size;
04467                     }
04468                   ent->plt.offset = plt_offset;
04469                 }
04470 
04471               /* We also need to make an entry in the .rela.plt section.  */
04472               if (!doneone)
04473                 {
04474                   htab->relplt->size += sizeof (Elf32_External_Rela);
04475 
04476                   if (htab->plt_type == PLT_VXWORKS)
04477                     {
04478                      /* Allocate space for the unloaded relocations.  */
04479                      if (!info->shared)
04480                        {
04481                          if (ent->plt.offset
04482                             == (bfd_vma) htab->plt_initial_entry_size)
04483                            {
04484                             htab->srelplt2->size
04485                               += sizeof (Elf32_External_Rela)
04486                                   * VXWORKS_PLTRESOLVE_RELOCS;
04487                            }
04488 
04489                          htab->srelplt2->size
04490                            += sizeof (Elf32_External_Rela)
04491                               * VXWORKS_PLT_NON_JMP_SLOT_RELOCS;
04492                        }
04493 
04494                      /* Every PLT entry has an associated GOT entry in
04495                         .got.plt.  */
04496                      htab->sgotplt->size += 4;
04497                     }
04498                   doneone = TRUE;
04499                 }
04500              }
04501            else
04502              ent->plt.offset = (bfd_vma) -1;
04503          }
04504        else
04505          ent->plt.offset = (bfd_vma) -1;
04506 
04507       if (!doneone)
04508        {
04509          h->plt.plist = NULL;
04510          h->needs_plt = 0;
04511        }
04512     }
04513   else
04514     {
04515       h->plt.plist = NULL;
04516       h->needs_plt = 0;
04517     }
04518 
04519   eh = (struct ppc_elf_link_hash_entry *) h;
04520   if (eh->elf.got.refcount > 0)
04521     {
04522       /* Make sure this symbol is output as a dynamic symbol.  */
04523       if (eh->elf.dynindx == -1
04524          && !eh->elf.forced_local)
04525        {
04526          if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
04527            return FALSE;
04528        }
04529 
04530       if (eh->tls_mask == (TLS_TLS | TLS_LD)
04531          && !eh->elf.def_dynamic)
04532        /* If just an LD reloc, we'll just use htab->tlsld_got.offset.  */
04533        eh->elf.got.offset = (bfd_vma) -1;
04534       else
04535        {
04536          bfd_boolean dyn;
04537          unsigned int need = 0;
04538          if ((eh->tls_mask & TLS_TLS) != 0)
04539            {
04540              if ((eh->tls_mask & TLS_LD) != 0)
04541               need += 8;
04542              if ((eh->tls_mask & TLS_GD) != 0)
04543               need += 8;
04544              if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
04545               need += 4;
04546              if ((eh->tls_mask & TLS_DTPREL) != 0)
04547               need += 4;
04548            }
04549          else
04550            need += 4;
04551          eh->elf.got.offset = allocate_got (htab, need);
04552          dyn = htab->elf.dynamic_sections_created;
04553          if ((info->shared
04554               || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
04555              && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
04556                 || eh->elf.root.type != bfd_link_hash_undefweak))
04557            {
04558              /* All the entries we allocated need relocs.
04559                Except LD only needs one.  */
04560              if ((eh->tls_mask & TLS_LD) != 0)
04561               need -= 4;
04562              htab->relgot->size += need * (sizeof (Elf32_External_Rela) / 4);
04563            }
04564        }
04565     }
04566   else
04567     eh->elf.got.offset = (bfd_vma) -1;
04568 
04569   if (eh->dyn_relocs == NULL)
04570     return TRUE;
04571 
04572   /* In the shared -Bsymbolic case, discard space allocated for
04573      dynamic pc-relative relocs against symbols which turn out to be
04574      defined in regular objects.  For the normal shared case, discard
04575      space for relocs that have become local due to symbol visibility
04576      changes.  */
04577 
04578   if (info->shared)
04579     {
04580       /* Relocs that use pc_count are those that appear on a call insn,
04581         or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
04582         generated via assembly.  We want calls to protected symbols to
04583         resolve directly to the function rather than going via the plt.
04584         If people want function pointer comparisons to work as expected
04585         then they should avoid writing weird assembly.  */
04586       if (SYMBOL_CALLS_LOCAL (info, h))
04587        {
04588          struct ppc_elf_dyn_relocs **pp;
04589 
04590          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
04591            {
04592              p->count -= p->pc_count;
04593              p->pc_count = 0;
04594              if (p->count == 0)
04595               *pp = p->next;
04596              else
04597               pp = &p->next;
04598            }
04599        }
04600 
04601       /* Also discard relocs on undefined weak syms with non-default
04602         visibility.  */
04603       if (eh->dyn_relocs != NULL
04604          && h->root.type == bfd_link_hash_undefweak)
04605        {
04606          if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
04607            eh->dyn_relocs = NULL;
04608 
04609          /* Make sure undefined weak symbols are output as a dynamic
04610             symbol in PIEs.  */
04611          else if (h->dynindx == -1
04612                  && !h->forced_local)
04613            {
04614              if (! bfd_elf_link_record_dynamic_symbol (info, h))
04615               return FALSE;
04616            }
04617        }
04618     }
04619   else if (ELIMINATE_COPY_RELOCS)
04620     {
04621       /* For the non-shared case, discard space for relocs against
04622         symbols which turn out to need copy relocs or are not
04623         dynamic.  */
04624 
04625       if (!h->non_got_ref
04626          && h->def_dynamic
04627          && !h->def_regular)
04628        {
04629          /* Make sure this symbol is output as a dynamic symbol.
04630             Undefined weak syms won't yet be marked as dynamic.  */
04631          if (h->dynindx == -1
04632              && !h->forced_local)
04633            {
04634              if (! bfd_elf_link_record_dynamic_symbol (info, h))
04635               return FALSE;
04636            }
04637 
04638          /* If that succeeded, we know we'll be keeping all the
04639             relocs.  */
04640          if (h->dynindx != -1)
04641            goto keep;
04642        }
04643 
04644       eh->dyn_relocs = NULL;
04645 
04646     keep: ;
04647     }
04648 
04649   /* Finally, allocate space.  */
04650   for (p = eh->dyn_relocs; p != NULL; p = p->next)
04651     {
04652       asection *sreloc = elf_section_data (p->sec)->sreloc;
04653       sreloc->size += p->count * sizeof (Elf32_External_Rela);
04654     }
04655 
04656   return TRUE;
04657 }
04658 
04659 /* Find any dynamic relocs that apply to read-only sections.  */
04660 
04661 static bfd_boolean
04662 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
04663 {
04664   struct ppc_elf_dyn_relocs *p;
04665 
04666   if (h->root.type == bfd_link_hash_indirect)
04667     return TRUE;
04668 
04669   if (h->root.type == bfd_link_hash_warning)
04670     h = (struct elf_link_hash_entry *) h->root.u.i.link;
04671 
04672   for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
04673     {
04674       asection *s = p->sec->output_section;
04675 
04676       if (s != NULL
04677          && ((s->flags & (SEC_READONLY | SEC_ALLOC))
04678              == (SEC_READONLY | SEC_ALLOC)))
04679        {
04680          ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
04681 
04682          /* Not an error, just cut short the traversal.  */
04683          return FALSE;
04684        }
04685     }
04686   return TRUE;
04687 }
04688 
04689 /* Set the sizes of the dynamic sections.  */
04690 
04691 static bfd_boolean
04692 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
04693                             struct bfd_link_info *info)
04694 {
04695   struct ppc_elf_link_hash_table *htab;
04696   asection *s;
04697   bfd_boolean relocs;
04698   bfd *ibfd;
04699 
04700 #ifdef DEBUG
04701   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
04702 #endif
04703 
04704   htab = ppc_elf_hash_table (info);
04705   BFD_ASSERT (htab->elf.dynobj != NULL);
04706 
04707   if (elf_hash_table (info)->dynamic_sections_created)
04708     {
04709       /* Set the contents of the .interp section to the interpreter.  */
04710       if (info->executable)
04711        {
04712          s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
04713          BFD_ASSERT (s != NULL);
04714          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
04715          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
04716        }
04717     }
04718 
04719   if (htab->plt_type == PLT_OLD)
04720     htab->got_header_size = 16;
04721   else if (htab->plt_type == PLT_NEW)
04722     htab->got_header_size = 12;
04723 
04724   /* Set up .got offsets for local syms, and space for local dynamic
04725      relocs.  */
04726   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
04727     {
04728       bfd_signed_vma *local_got;
04729       bfd_signed_vma *end_local_got;
04730       char *lgot_masks;
04731       bfd_size_type locsymcount;
04732       Elf_Internal_Shdr *symtab_hdr;
04733 
04734       if (!is_ppc_elf_target (ibfd->xvec))
04735        continue;
04736 
04737       for (s = ibfd->sections; s != NULL; s = s->next)
04738        {
04739          struct ppc_elf_dyn_relocs *p;
04740 
04741          for (p = ((struct ppc_elf_dyn_relocs *)
04742                   elf_section_data (s)->local_dynrel);
04743               p != NULL;
04744               p = p->next)
04745            {
04746              if (!bfd_is_abs_section (p->sec)
04747                 && bfd_is_abs_section (p->sec->output_section))
04748               {
04749                 /* Input section has been discarded, either because
04750                    it is a copy of a linkonce section or due to
04751                    linker script /DISCARD/, so we'll be discarding
04752                    the relocs too.  */
04753               }
04754              else if (p->count != 0)
04755               {
04756                 elf_section_data (p->sec)->sreloc->size
04757                   += p->count * sizeof (Elf32_External_Rela);
04758                 if ((p->sec->output_section->flags
04759                      & (SEC_READONLY | SEC_ALLOC))
04760                     == (SEC_READONLY | SEC_ALLOC))
04761                   info->flags |= DF_TEXTREL;
04762               }
04763            }
04764        }
04765 
04766       local_got = elf_local_got_refcounts (ibfd);
04767       if (!local_got)
04768        continue;
04769 
04770       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
04771       locsymcount = symtab_hdr->sh_info;
04772       end_local_got = local_got + locsymcount;
04773       lgot_masks = (char *) end_local_got;
04774       for (; local_got < end_local_got; ++local_got, ++lgot_masks)
04775        if (*local_got > 0)
04776          {
04777            if (*lgot_masks == (TLS_TLS | TLS_LD))
04778              {
04779               /* If just an LD reloc, we'll just use
04780                  htab->tlsld_got.offset.  */
04781               htab->tlsld_got.refcount += 1;
04782               *local_got = (bfd_vma) -1;
04783              }
04784            else
04785              {
04786               unsigned int need = 0;
04787               if ((*lgot_masks & TLS_TLS) != 0)
04788                 {
04789                   if ((*lgot_masks & TLS_GD) != 0)
04790                     need += 8;
04791                   if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
04792                     need += 4;
04793                   if ((*lgot_masks & TLS_DTPREL) != 0)
04794                     need += 4;
04795                 }
04796               else
04797                 need += 4;
04798               *local_got = allocate_got (htab, need);
04799               if (info->shared)
04800                 htab->relgot->size += (need
04801                                     * (sizeof (Elf32_External_Rela) / 4));
04802              }
04803          }
04804        else
04805          *local_got = (bfd_vma) -1;
04806     }
04807 
04808   if (htab->tlsld_got.refcount > 0)
04809     {
04810       htab->tlsld_got.offset = allocate_got (htab, 8);
04811       if (info->shared)
04812        htab->relgot->size += sizeof (Elf32_External_Rela);
04813     }
04814   else
04815     htab->tlsld_got.offset = (bfd_vma) -1;
04816 
04817   /* Allocate space for global sym dynamic relocs.  */
04818   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
04819 
04820   if (htab->got != NULL && htab->plt_type != PLT_VXWORKS)
04821     {
04822       unsigned int g_o_t = 32768;
04823 
04824       /* If we haven't allocated the header, do so now.  When we get here,
04825         for old plt/got the got size will be 0 to 32764 (not allocated),
04826         or 32780 to 65536 (header allocated).  For new plt/got, the
04827         corresponding ranges are 0 to 32768 and 32780 to 65536.  */
04828       if (htab->got->size <= 32768)
04829        {
04830          g_o_t = htab->got->size;
04831          if (htab->plt_type == PLT_OLD)
04832            g_o_t += 4;
04833          htab->got->size += htab->got_header_size;
04834        }
04835 
04836       htab->elf.hgot->root.u.def.value = g_o_t;
04837     }
04838 
04839   if (htab->glink != NULL && htab->glink->size != 0)
04840     {
04841       htab->glink_pltresolve = htab->glink->size;
04842       /* Space for the branch table.  */
04843       htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
04844       /* Pad out to align the start of PLTresolve.  */
04845       htab->glink->size += -htab->glink->size & 15;
04846       htab->glink->size += GLINK_PLTRESOLVE;
04847 
04848       if (htab->emit_stub_syms)
04849        {
04850          struct elf_link_hash_entry *sh;
04851          sh = elf_link_hash_lookup (&htab->elf, "__glink",
04852                                  TRUE, FALSE, FALSE);
04853          if (sh == NULL)
04854            return FALSE;
04855          if (sh->root.type == bfd_link_hash_new)
04856            {
04857              sh->root.type = bfd_link_hash_defined;
04858              sh->root.u.def.section = htab->glink;
04859              sh->root.u.def.value = htab->glink_pltresolve;
04860              sh->ref_regular = 1;
04861              sh->def_regular = 1;
04862              sh->ref_regular_nonweak = 1;
04863              sh->forced_local = 1;
04864              sh->non_elf = 0;
04865            }
04866          sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
04867                                  TRUE, FALSE, FALSE);
04868          if (sh == NULL)
04869            return FALSE;
04870          if (sh->root.type == bfd_link_hash_new)
04871            {
04872              sh->root.type = bfd_link_hash_defined;
04873              sh->root.u.def.section = htab->glink;
04874              sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
04875              sh->ref_regular = 1;
04876              sh->def_regular = 1;
04877              sh->ref_regular_nonweak = 1;
04878              sh->forced_local = 1;
04879              sh->non_elf = 0;
04880            }
04881        }
04882     }
04883 
04884   /* We've now determined the sizes of the various dynamic sections.
04885      Allocate memory for them.  */
04886   relocs = FALSE;
04887   for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
04888     {
04889       bfd_boolean strip_section = TRUE;
04890 
04891       if ((s->flags & SEC_LINKER_CREATED) == 0)
04892        continue;
04893 
04894       if (s == htab->plt
04895          || s == htab->glink
04896          || s == htab->got
04897          || s == htab->sgotplt
04898          || s == htab->sbss
04899          || s == htab->dynbss
04900          || s == htab->dynsbss)
04901        {
04902          /* We'd like to strip these sections if they aren't needed, but if
04903             we've exported dynamic symbols from them we must leave them.
04904             It's too late to tell BFD to get rid of the symbols.  */
04905          if ((s == htab->plt || s == htab->got) && htab->elf.hplt != NULL)
04906            strip_section = FALSE;
04907          /* Strip this section if we don't need it; see the
04908             comment below.  */
04909        }
04910       else if (s == htab->sdata[0].section
04911               || s == htab->sdata[1].section)
04912        {
04913          /* Strip these too.  */
04914        }
04915       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
04916        {
04917          if (s->size != 0)
04918            {
04919              /* Remember whether there are any relocation sections.  */
04920              relocs = TRUE;
04921 
04922              /* We use the reloc_count field as a counter if we need
04923                to copy relocs into the output file.  */
04924              s->reloc_count = 0;
04925            }
04926        }
04927       else
04928        {
04929          /* It's not one of our sections, so don't allocate space.  */
04930          continue;
04931        }
04932 
04933       if (s->size == 0 && strip_section)
04934        {
04935          /* If we don't need this section, strip it from the
04936             output file.  This is mostly to handle .rela.bss and
04937             .rela.plt.  We must create both sections in
04938             create_dynamic_sections, because they must be created
04939             before the linker maps input sections to output
04940             sections.  The linker does that before
04941             adjust_dynamic_symbol is called, and it is that
04942             function which decides whether anything needs to go
04943             into these sections.  */
04944          s->flags |= SEC_EXCLUDE;
04945          continue;
04946        }
04947 
04948       if ((s->flags & SEC_HAS_CONTENTS) == 0)
04949        continue;
04950 
04951       /* Allocate memory for the section contents.  */
04952       s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
04953       if (s->contents == NULL)
04954        return FALSE;
04955     }
04956 
04957   if (htab->elf.dynamic_sections_created)
04958     {
04959       /* Add some entries to the .dynamic section.  We fill in the
04960         values later, in ppc_elf_finish_dynamic_sections, but we
04961         must add the entries now so that we get the correct size for
04962         the .dynamic section.  The DT_DEBUG entry is filled in by the
04963         dynamic linker and used by the debugger.  */
04964 #define add_dynamic_entry(TAG, VAL) \
04965   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
04966 
04967       if (info->executable)
04968        {
04969          if (!add_dynamic_entry (DT_DEBUG, 0))
04970            return FALSE;
04971        }
04972 
04973       if (htab->plt != NULL && htab->plt->size != 0)
04974        {
04975          if (!add_dynamic_entry (DT_PLTGOT, 0)
04976              || !add_dynamic_entry (DT_PLTRELSZ, 0)
04977              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
04978              || !add_dynamic_entry (DT_JMPREL, 0))
04979            return FALSE;
04980        }
04981 
04982       if (htab->glink != NULL && htab->glink->size != 0)
04983        {
04984          if (!add_dynamic_entry (DT_PPC_GOT, 0))
04985            return FALSE;
04986        }
04987 
04988       if (relocs)
04989        {
04990          if (!add_dynamic_entry (DT_RELA, 0)
04991              || !add_dynamic_entry (DT_RELASZ, 0)
04992              || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
04993            return FALSE;
04994        }
04995 
04996       /* If any dynamic relocs apply to a read-only section, then we
04997         need a DT_TEXTREL entry.  */
04998       if ((info->flags & DF_TEXTREL) == 0)
04999        elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
05000                             info);
05001 
05002       if ((info->flags & DF_TEXTREL) != 0)
05003        {
05004          if (!add_dynamic_entry (DT_TEXTREL, 0))
05005            return FALSE;
05006        }
05007     }
05008 #undef add_dynamic_entry
05009 
05010   return TRUE;
05011 }
05012 
05013 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
05014 
05015 static const int shared_stub_entry[] =
05016   {
05017     0x7c0802a6, /* mflr 0 */
05018     0x429f0005, /* bcl 20, 31, .Lxxx */
05019     0x7d6802a6, /* mflr 11 */
05020     0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
05021     0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
05022     0x7c0803a6, /* mtlr 0 */
05023     0x7d6903a6, /* mtctr 11 */
05024     0x4e800420, /* bctr */
05025   };
05026 
05027 static const int stub_entry[] =
05028   {
05029     0x3d600000, /* lis 11,xxx@ha */
05030     0x396b0000, /* addi 11,11,xxx@l */
05031     0x7d6903a6, /* mtctr 11 */
05032     0x4e800420, /* bctr */
05033   };
05034 
05035 static bfd_boolean
05036 ppc_elf_relax_section (bfd *abfd,
05037                      asection *isec,
05038                      struct bfd_link_info *link_info,
05039                      bfd_boolean *again)
05040 {
05041   struct one_fixup
05042   {
05043     struct one_fixup *next;
05044     asection *tsec;
05045     bfd_vma toff;
05046     bfd_vma trampoff;
05047   };
05048 
05049   Elf_Internal_Shdr *symtab_hdr;
05050   bfd_byte *contents = NULL;
05051   Elf_Internal_Sym *isymbuf = NULL;
05052   Elf_Internal_Rela *internal_relocs = NULL;
05053   Elf_Internal_Rela *irel, *irelend;
05054   struct one_fixup *fixups = NULL;
05055   bfd_boolean changed;
05056   struct ppc_elf_link_hash_table *htab;
05057   bfd_size_type trampoff;
05058   asection *got2;
05059 
05060   *again = FALSE;
05061 
05062   /* Nothing to do if there are no relocations, and no need to do
05063      anything with non-alloc sections.  */
05064   if ((isec->flags & SEC_ALLOC) == 0
05065       || (isec->flags & SEC_RELOC) == 0
05066       || isec->reloc_count == 0)
05067     return TRUE;
05068 
05069   trampoff = (isec->size + 3) & (bfd_vma) -4;
05070   /* Space for a branch around any trampolines.  */
05071   trampoff += 4;
05072 
05073   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
05074 
05075   /* Get a copy of the native relocations.  */
05076   internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
05077                                           link_info->keep_memory);
05078   if (internal_relocs == NULL)
05079     goto error_return;
05080 
05081   htab = ppc_elf_hash_table (link_info);
05082   got2 = bfd_get_section_by_name (abfd, ".got2");
05083 
05084   irelend = internal_relocs + isec->reloc_count;
05085   for (irel = internal_relocs; irel < irelend; irel++)
05086     {
05087       unsigned long r_type = ELF32_R_TYPE (irel->r_info);
05088       bfd_vma symaddr, reladdr, toff, roff;
05089       asection *tsec;
05090       struct one_fixup *f;
05091       size_t insn_offset = 0;
05092       bfd_vma max_branch_offset, val;
05093       bfd_byte *hit_addr;
05094       unsigned long t0;
05095       unsigned char sym_type;
05096 
05097       switch (r_type)
05098        {
05099        case R_PPC_REL24:
05100        case R_PPC_LOCAL24PC:
05101        case R_PPC_PLTREL24:
05102          max_branch_offset = 1 << 25;
05103          break;
05104 
05105        case R_PPC_REL14:
05106        case R_PPC_REL14_BRTAKEN:
05107        case R_PPC_REL14_BRNTAKEN:
05108          max_branch_offset = 1 << 15;
05109          break;
05110 
05111        default:
05112          continue;
05113        }
05114 
05115       /* Get the value of the symbol referred to by the reloc.  */
05116       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
05117        {
05118          /* A local symbol.  */
05119          Elf_Internal_Sym *isym;
05120 
05121          /* Read this BFD's local symbols.  */
05122          if (isymbuf == NULL)
05123            {
05124              isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
05125              if (isymbuf == NULL)
05126               isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
05127                                           symtab_hdr->sh_info, 0,
05128                                           NULL, NULL, NULL);
05129              if (isymbuf == 0)
05130               goto error_return;
05131            }
05132          isym = isymbuf + ELF32_R_SYM (irel->r_info);
05133          if (isym->st_shndx == SHN_UNDEF)
05134            continue; /* We can't do anything with undefined symbols.  */
05135          else if (isym->st_shndx == SHN_ABS)
05136            tsec = bfd_abs_section_ptr;
05137          else if (isym->st_shndx == SHN_COMMON)
05138            tsec = bfd_com_section_ptr;
05139          else
05140            tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
05141 
05142          toff = isym->st_value;
05143          sym_type = ELF_ST_TYPE (isym->st_info);
05144        }
05145       else
05146        {
05147          /* Global symbol handling.  */
05148          unsigned long indx;
05149          struct elf_link_hash_entry *h;
05150 
05151          indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
05152          h = elf_sym_hashes (abfd)[indx];
05153 
05154          while (h->root.type == bfd_link_hash_indirect
05155                || h->root.type == bfd_link_hash_warning)
05156            h = (struct elf_link_hash_entry *) h->root.u.i.link;
05157 
05158          tsec = NULL;
05159          toff = 0;
05160          if (r_type == R_PPC_PLTREL24
05161              && htab->plt != NULL)
05162            {
05163              struct plt_entry *ent = find_plt_ent (h, got2, irel->r_addend);
05164 
05165              if (ent != NULL)
05166               {
05167                 if (htab->plt_type == PLT_NEW)
05168                   {
05169                     tsec = htab->glink;
05170                     toff = ent->glink_offset;
05171                   }
05172                 else
05173                   {
05174                     tsec = htab->plt;
05175                     toff = ent->plt.offset;
05176                   }
05177               }
05178            }
05179          if (tsec != NULL)
05180            ;
05181          else if (h->root.type == bfd_link_hash_defined
05182                  || h->root.type == bfd_link_hash_defweak)
05183            {
05184              tsec = h->root.u.def.section;
05185              toff = h->root.u.def.value;
05186            }
05187          else
05188            continue;
05189 
05190          sym_type = h->type;
05191        }
05192 
05193       /* If the branch and target are in the same section, you have
05194         no hope of adding stubs.  We'll error out later should the
05195         branch overflow.  */
05196       if (tsec == isec)
05197        continue;
05198 
05199       /* There probably isn't any reason to handle symbols in
05200         SEC_MERGE sections;  SEC_MERGE doesn't seem a likely
05201         attribute for a code section, and we are only looking at
05202         branches.  However, implement it correctly here as a
05203         reference for other target relax_section functions.  */
05204       if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
05205        {
05206          /* At this stage in linking, no SEC_MERGE symbol has been
05207             adjusted, so all references to such symbols need to be
05208             passed through _bfd_merged_section_offset.  (Later, in
05209             relocate_section, all SEC_MERGE symbols *except* for
05210             section symbols have been adjusted.)
05211 
05212             gas may reduce relocations against symbols in SEC_MERGE
05213             sections to a relocation against the section symbol when
05214             the original addend was zero.  When the reloc is against
05215             a section symbol we should include the addend in the
05216             offset passed to _bfd_merged_section_offset, since the
05217             location of interest is the original symbol.  On the
05218             other hand, an access to "sym+addend" where "sym" is not
05219             a section symbol should not include the addend;  Such an
05220             access is presumed to be an offset from "sym";  The
05221             location of interest is just "sym".  */
05222          if (sym_type == STT_SECTION)
05223            toff += irel->r_addend;
05224 
05225          toff = _bfd_merged_section_offset (abfd, &tsec,
05226                                         elf_section_data (tsec)->sec_info,
05227                                         toff);
05228 
05229          if (sym_type != STT_SECTION)
05230            toff += irel->r_addend;
05231        }
05232       /* PLTREL24 addends are special.  */
05233       else if (r_type != R_PPC_PLTREL24)
05234        toff += irel->r_addend;
05235 
05236       /* Attempted -shared link of non-pic code loses.  */
05237       if (tsec->output_section == NULL)
05238        continue;
05239 
05240       symaddr = tsec->output_section->vma + tsec->output_offset + toff;
05241 
05242       roff = irel->r_offset;
05243       reladdr = isec->output_section->vma + isec->output_offset + roff;
05244 
05245       /* If the branch is in range, no need to do anything.  */
05246       if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
05247        continue;
05248 
05249       /* Look for an existing fixup to this address.  */
05250       for (f = fixups; f ; f = f->next)
05251        if (f->tsec == tsec && f->toff == toff)
05252          break;
05253 
05254       if (f == NULL)
05255        {
05256          size_t size;
05257          unsigned long stub_rtype;
05258 
05259          val = trampoff - roff;
05260          if (val >= max_branch_offset)
05261            /* Oh dear, we can't reach a trampoline.  Don't try to add
05262               one.  We'll report an error later.  */
05263            continue;
05264 
05265          if (link_info->shared)
05266            {
05267              size = 4 * ARRAY_SIZE (shared_stub_entry);
05268              insn_offset = 12;
05269              stub_rtype = R_PPC_RELAX32PC;
05270            }
05271          else
05272            {
05273              size = 4 * ARRAY_SIZE (stub_entry);
05274              insn_offset = 0;
05275              stub_rtype = R_PPC_RELAX32;
05276            }
05277 
05278          if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
05279              != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
05280            abort ();
05281          if (tsec == htab->plt
05282              || tsec == htab->glink)
05283            stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
05284 
05285          /* Hijack the old relocation.  Since we need two
05286             relocations for this use a "composite" reloc.  */
05287          irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
05288                                    stub_rtype);
05289          irel->r_offset = trampoff + insn_offset;
05290 
05291          /* Record the fixup so we don't do it again this section.  */
05292          f = bfd_malloc (sizeof (*f));
05293          f->next = fixups;
05294          f->tsec = tsec;
05295          f->toff = toff;
05296          f->trampoff = trampoff;
05297          fixups = f;
05298 
05299          trampoff += size;
05300        }
05301       else
05302        {
05303          val = f->trampoff - roff;
05304          if (val >= max_branch_offset)
05305            continue;
05306 
05307          /* Nop out the reloc, since we're finalizing things here.  */
05308          irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
05309        }
05310 
05311       /* Get the section contents.  */
05312       if (contents == NULL)
05313        {
05314          /* Get cached copy if it exists.  */
05315          if (elf_section_data (isec)->this_hdr.contents != NULL)
05316            contents = elf_section_data (isec)->this_hdr.contents;
05317          else
05318            {
05319              /* Go get them off disk.  */
05320              if (!bfd_malloc_and_get_section (abfd, isec, &contents))
05321               goto error_return;
05322            }
05323        }
05324 
05325       /* Fix up the existing branch to hit the trampoline.  */
05326       hit_addr = contents + roff;
05327       switch (r_type)
05328        {
05329        case R_PPC_REL24:
05330        case R_PPC_LOCAL24PC:
05331        case R_PPC_PLTREL24:
05332          t0 = bfd_get_32 (abfd, hit_addr);
05333          t0 &= ~0x3fffffc;
05334          t0 |= val & 0x3fffffc;
05335          bfd_put_32 (abfd, t0, hit_addr);
05336          break;
05337 
05338        case R_PPC_REL14:
05339        case R_PPC_REL14_BRTAKEN:
05340        case R_PPC_REL14_BRNTAKEN:
05341          t0 = bfd_get_32 (abfd, hit_addr);
05342          t0 &= ~0xfffc;
05343          t0 |= val & 0xfffc;
05344          bfd_put_32 (abfd, t0, hit_addr);
05345          break;
05346        }
05347     }
05348 
05349   /* Write out the trampolines.  */
05350   changed = fixups != NULL;
05351   if (fixups != NULL)
05352     {
05353       const int *stub;
05354       bfd_byte *dest;
05355       bfd_vma val;
05356       int i, size;
05357 
05358       do
05359        {
05360          struct one_fixup *f = fixups;
05361          fixups = fixups->next;
05362          free (f);
05363        }
05364       while (fixups);
05365 
05366       contents = bfd_realloc (contents, trampoff);
05367       if (contents == NULL)
05368        goto error_return;
05369 
05370       isec->size = (isec->size + 3) & (bfd_vma) -4;
05371       /* Branch around the trampolines.  */
05372       val = trampoff - isec->size + 0x48000000;
05373       dest = contents + isec->size;
05374       isec->size = trampoff;
05375       bfd_put_32 (abfd, val, dest);
05376       dest += 4;
05377 
05378       if (link_info->shared)
05379        {
05380          stub = shared_stub_entry;
05381          size = ARRAY_SIZE (shared_stub_entry);
05382        }
05383       else
05384        {
05385          stub = stub_entry;
05386          size = ARRAY_SIZE (stub_entry);
05387        }
05388 
05389       i = 0;
05390       while (dest < contents + trampoff)
05391        {
05392          bfd_put_32 (abfd, stub[i], dest);
05393          i++;
05394          if (i == size)
05395            i = 0;
05396          dest += 4;
05397        }
05398       BFD_ASSERT (i == 0);
05399     }
05400 
05401   if (isymbuf != NULL
05402       && symtab_hdr->contents != (unsigned char *) isymbuf)
05403     {
05404       if (! link_info->keep_memory)
05405        free (isymbuf);
05406       else
05407        {
05408          /* Cache the symbols for elf_link_input_bfd.  */
05409          symtab_hdr->contents = (unsigned char *) isymbuf;
05410        }
05411     }
05412 
05413   if (contents != NULL
05414       && elf_section_data (isec)->this_hdr.contents != contents)
05415     {
05416       if (!changed && !link_info->keep_memory)
05417        free (contents);
05418       else
05419        {
05420          /* Cache the section contents for elf_link_input_bfd.  */
05421          elf_section_data (isec)->this_hdr.contents = contents;
05422        }
05423     }
05424 
05425   if (elf_section_data (isec)->relocs != internal_relocs)
05426     {
05427       if (!changed)
05428        free (internal_relocs);
05429       else
05430        elf_section_data (isec)->relocs = internal_relocs;
05431     }
05432 
05433   *again = changed;
05434   return TRUE;
05435 
05436  error_return:
05437   if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
05438     free (isymbuf);
05439   if (contents != NULL
05440       && elf_section_data (isec)->this_hdr.contents != contents)
05441     free (contents);
05442   if (internal_relocs != NULL
05443       && elf_section_data (isec)->relocs != internal_relocs)
05444     free (internal_relocs);
05445   return FALSE;
05446 }
05447 
05448 /* What to do when ld finds relocations against symbols defined in
05449    discarded sections.  */
05450 
05451 static unsigned int
05452 ppc_elf_action_discarded (asection *sec)
05453 {
05454   if (strcmp (".fixup", sec->name) == 0)
05455     return 0;
05456 
05457   if (strcmp (".got2", sec->name) == 0)
05458     return 0;
05459 
05460   return _bfd_elf_default_action_discarded (sec);
05461 }
05462 
05463 /* Fill in the address for a pointer generated in a linker section.  */
05464 
05465 static bfd_vma
05466 elf_finish_pointer_linker_section (bfd *input_bfd,
05467                                elf_linker_section_t *lsect,
05468                                struct elf_link_hash_entry *h,
05469                                bfd_vma relocation,
05470                                const Elf_Internal_Rela *rel)
05471 {
05472   elf_linker_section_pointers_t *linker_section_ptr;
05473 
05474   BFD_ASSERT (lsect != NULL);
05475 
05476   if (h != NULL)
05477     {
05478       /* Handle global symbol.  */
05479       struct ppc_elf_link_hash_entry *eh;
05480 
05481       eh = (struct ppc_elf_link_hash_entry *) h;
05482       BFD_ASSERT (eh->elf.def_regular);
05483       linker_section_ptr = eh->linker_section_pointer;
05484     }
05485   else
05486     {
05487       /* Handle local symbol.  */
05488       unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
05489 
05490       BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
05491       linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
05492     }
05493 
05494   linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
05495                                                  rel->r_addend,
05496                                                  lsect);
05497   BFD_ASSERT (linker_section_ptr != NULL);
05498 
05499   /* Offset will always be a multiple of four, so use the bottom bit
05500      as a "written" flag.  */
05501   if ((linker_section_ptr->offset & 1) == 0)
05502     {
05503       bfd_put_32 (lsect->section->owner,
05504                 relocation + linker_section_ptr->addend,
05505                 lsect->section->contents + linker_section_ptr->offset);
05506       linker_section_ptr->offset += 1;
05507     }
05508 
05509   relocation = (lsect->section->output_offset
05510               + linker_section_ptr->offset - 1
05511               - 0x8000);
05512 
05513 #ifdef DEBUG
05514   fprintf (stderr,
05515           "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
05516           lsect->name, (long) relocation, (long) relocation);
05517 #endif
05518 
05519   /* Subtract out the addend, because it will get added back in by the normal
05520      processing.  */
05521   return relocation - linker_section_ptr->addend;
05522 }
05523 
05524 /* The RELOCATE_SECTION function is called by the ELF backend linker
05525    to handle the relocations for a section.
05526 
05527    The relocs are always passed as Rela structures; if the section
05528    actually uses Rel structures, the r_addend field will always be
05529    zero.
05530 
05531    This function is responsible for adjust the section contents as
05532    necessary, and (if using Rela relocs and generating a
05533    relocatable output file) adjusting the reloc addend as
05534    necessary.
05535 
05536    This function does not have to worry about setting the reloc
05537    address or the reloc symbol index.
05538 
05539    LOCAL_SYMS is a pointer to the swapped in local symbols.
05540 
05541    LOCAL_SECTIONS is an array giving the section in the input file
05542    corresponding to the st_shndx field of each local symbol.
05543 
05544    The global hash table entry for the global symbols can be found
05545    via elf_sym_hashes (input_bfd).
05546 
05547    When generating relocatable output, this function must handle
05548    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
05549    going to be the section symbol corresponding to the output
05550    section, which means that the addend must be adjusted
05551    accordingly.  */
05552 
05553 static bfd_boolean
05554 ppc_elf_relocate_section (bfd *output_bfd,
05555                        struct bfd_link_info *info,
05556                        bfd *input_bfd,
05557                        asection *input_section,
05558                        bfd_byte *contents,
05559                        Elf_Internal_Rela *relocs,
05560                        Elf_Internal_Sym *local_syms,
05561                        asection **local_sections)
05562 {
05563   Elf_Internal_Shdr *symtab_hdr;
05564   struct elf_link_hash_entry **sym_hashes;
05565   struct ppc_elf_link_hash_table *htab;
05566   Elf_Internal_Rela *rel;
05567   Elf_Internal_Rela *relend;
05568   Elf_Internal_Rela outrel;
05569   bfd_byte *loc;
05570   asection *got2, *sreloc = NULL;
05571   bfd_vma *local_got_offsets;
05572   bfd_boolean ret = TRUE;
05573   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
05574 
05575 #ifdef DEBUG
05576   _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
05577                     "%ld relocations%s",
05578                     input_bfd, input_section,
05579                     (long) input_section->reloc_count,
05580                     (info->relocatable) ? " (relocatable)" : "");
05581 #endif
05582 
05583   got2 = bfd_get_section_by_name (input_bfd, ".got2");
05584 
05585   /* Initialize howto table if not already done.  */
05586   if (!ppc_elf_howto_table[R_PPC_ADDR32])
05587     ppc_elf_howto_init ();
05588 
05589   htab = ppc_elf_hash_table (info);
05590   local_got_offsets = elf_local_got_offsets (input_bfd);
05591   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
05592   sym_hashes = elf_sym_hashes (input_bfd);
05593   rel = relocs;
05594   relend = relocs + input_section->reloc_count;
05595   for (; rel < relend; rel++)
05596     {
05597       enum elf_ppc_reloc_type r_type;
05598       bfd_vma addend;
05599       bfd_reloc_status_type r;
05600       Elf_Internal_Sym *sym;
05601       asection *sec;
05602       struct elf_link_hash_entry *h;
05603       const char *sym_name;
05604       reloc_howto_type *howto;
05605       unsigned long r_symndx;
05606       bfd_vma relocation;
05607       bfd_vma branch_bit, insn, from;
05608       bfd_boolean unresolved_reloc;
05609       bfd_boolean warned;
05610       unsigned int tls_type, tls_mask, tls_gd;
05611 
05612       r_type = ELF32_R_TYPE (rel->r_info);
05613       sym = NULL;
05614       sec = NULL;
05615       h = NULL;
05616       unresolved_reloc = FALSE;
05617       warned = FALSE;
05618       r_symndx = ELF32_R_SYM (rel->r_info);
05619 
05620       if (r_symndx < symtab_hdr->sh_info)
05621        {
05622          sym = local_syms + r_symndx;
05623          sec = local_sections[r_symndx];
05624          sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
05625 
05626          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
05627        }
05628       else
05629        {
05630          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
05631                                r_symndx, symtab_hdr, sym_hashes,
05632                                h, sec, relocation,
05633                                unresolved_reloc, warned);
05634 
05635          sym_name = h->root.root.string;
05636        }
05637 
05638       if (sec != NULL && elf_discarded_section (sec))
05639        {
05640          /* For relocs against symbols from removed linkonce sections,
05641             or sections discarded by a linker script, we just want the
05642             section contents zeroed.  Avoid any special processing.  */
05643          howto = NULL;
05644          if (r_type < R_PPC_max)
05645            howto = ppc_elf_howto_table[r_type];
05646          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
05647          rel->r_info = 0;
05648          rel->r_addend = 0;
05649          continue;
05650        }
05651 
05652       if (info->relocatable)
05653        {
05654          if (got2 != NULL
05655              && r_type == R_PPC_PLTREL24
05656              && rel->r_addend >= 32768)
05657            {
05658              /* R_PPC_PLTREL24 is rather special.  If non-zero, the
05659                addend specifies the GOT pointer offset within .got2.  */
05660              rel->r_addend += got2->output_offset;
05661            }
05662          continue;
05663        }
05664 
05665       /* TLS optimizations.  Replace instruction sequences and relocs
05666         based on information we collected in tls_optimize.  We edit
05667         RELOCS so that --emit-relocs will output something sensible
05668         for the final instruction stream.  */
05669       tls_mask = 0;
05670       tls_gd = 0;
05671       if (IS_PPC_TLS_RELOC (r_type))
05672        {
05673          if (h != NULL)
05674            tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
05675          else if (local_got_offsets != NULL)
05676            {
05677              char *lgot_masks;
05678              lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
05679              tls_mask = lgot_masks[r_symndx];
05680            }
05681        }
05682 
05683       /* Ensure reloc mapping code below stays sane.  */
05684       if ((R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TLSGD16 & 3)
05685          || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
05686          || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
05687          || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
05688          || (R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TPREL16 & 3)
05689          || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
05690          || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
05691          || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
05692        abort ();
05693       switch (r_type)
05694        {
05695        default:
05696          break;
05697 
05698        case R_PPC_GOT_TPREL16:
05699        case R_PPC_GOT_TPREL16_LO:
05700          if (tls_mask != 0
05701              && (tls_mask & TLS_TPREL) == 0)
05702            {
05703              bfd_vma insn;
05704              insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
05705              insn &= 31 << 21;
05706              insn |= 0x3c020000;   /* addis 0,2,0 */
05707              bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
05708              r_type = R_PPC_TPREL16_HA;
05709              rel->r_info = ELF32_R_INFO (r_symndx, r_type);
05710            }
05711          break;
05712 
05713        case R_PPC_TLS:
05714          if (tls_mask != 0
05715              && (tls_mask & TLS_TPREL) == 0)
05716            {
05717              bfd_vma insn, rtra;
05718              insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
05719              if ((insn & ((31 << 26) | (31 << 11)))
05720                 == ((31 << 26) | (2 << 11)))
05721               rtra = insn & ((1 << 26) - (1 << 16));
05722              else if ((insn & ((31 << 26) | (31 << 16)))
05723                      == ((31 << 26) | (2 << 16)))
05724               rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
05725              else
05726               abort ();
05727              if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
05728               /* add -> addi.  */
05729               insn = 14 << 26;
05730              else if ((insn & (31 << 1)) == 23 << 1
05731                      && ((insn & (31 << 6)) < 14 << 6
05732                         || ((insn & (31 << 6)) >= 16 << 6
05733                             && (insn & (31 << 6)) < 24 << 6)))
05734               /* load and store indexed -> dform.  */
05735               insn = (32 | ((insn >> 6) & 31)) << 26;
05736              else if ((insn & (31 << 1)) == 21 << 1
05737                      && (insn & (0x1a << 6)) == 0)
05738               /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
05739               insn = (((58 | ((insn >> 6) & 4)) << 26)
05740                      | ((insn >> 6) & 1));
05741              else if ((insn & (31 << 1)) == 21 << 1
05742                      && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
05743               /* lwax -> lwa.  */
05744               insn = (58 << 26) | 2;
05745              else
05746               abort ();
05747              insn |= rtra;
05748              bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
05749              r_type = R_PPC_TPREL16_LO;
05750              rel->r_info = ELF32_R_INFO (r_symndx, r_type);
05751 
05752              /* Was PPC_TLS which sits on insn boundary, now
05753                PPC_TPREL16_LO which is at low-order half-word.  */
05754              rel->r_offset += d_offset;
05755            }
05756          break;
05757 
05758        case R_PPC_GOT_TLSGD16_HI:
05759        case R_PPC_GOT_TLSGD16_HA:
05760          tls_gd = TLS_TPRELGD;
05761          if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
05762            goto tls_gdld_hi;
05763          break;
05764 
05765        case R_PPC_GOT_TLSLD16_HI:
05766        case R_PPC_GOT_TLSLD16_HA:
05767          if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
05768            {
05769            tls_gdld_hi:
05770              if ((tls_mask & tls_gd) != 0)
05771               r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
05772                        + R_PPC_GOT_TPREL16);
05773              else
05774               {
05775                 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
05776                 rel->r_offset -= d_offset;
05777                 r_type = R_PPC_NONE;
05778               }
05779              rel->r_info = ELF32_R_INFO (r_symndx, r_type);
05780            }
05781          break;
05782 
05783        case R_PPC_GOT_TLSGD16:
05784        case R_PPC_GOT_TLSGD16_LO:
05785          tls_gd = TLS_TPRELGD;
05786          if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
05787            goto tls_get_addr_check;
05788          break;
05789 
05790        case R_PPC_GOT_TLSLD16:
05791        case R_PPC_GOT_TLSLD16_LO:
05792          if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
05793            {
05794            tls_get_addr_check:
05795              if (rel + 1 < relend)
05796               {
05797                 enum elf_ppc_reloc_type r_type2;
05798                 unsigned long r_symndx2;
05799                 struct elf_link_hash_entry *h2;
05800                 bfd_vma insn1, insn2;
05801                 bfd_vma offset;
05802 
05803                 /* The next instruction should be a call to
05804                    __tls_get_addr.  Peek at the reloc to be sure.  */
05805                 r_type2 = ELF32_R_TYPE (rel[1].r_info);
05806                 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
05807                 if (r_symndx2 < symtab_hdr->sh_info
05808                     || (r_type2 != R_PPC_REL14
05809                        && r_type2 != R_PPC_REL14_BRTAKEN
05810                        && r_type2 != R_PPC_REL14_BRNTAKEN
05811                        && r_type2 != R_PPC_REL24
05812                        && r_type2 != R_PPC_PLTREL24))
05813                   break;
05814 
05815                 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
05816                 while (h2->root.type == bfd_link_hash_indirect
05817                       || h2->root.type == bfd_link_hash_warning)
05818                   h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
05819                 if (h2 == NULL || h2 != htab->tls_get_addr)
05820                   break;
05821 
05822                 /* OK, it checks out.  Replace the call.  */
05823                 offset = rel[1].r_offset;
05824                 insn1 = bfd_get_32 (output_bfd,
05825                                   contents + rel->r_offset - d_offset);
05826                 if ((tls_mask & tls_gd) != 0)
05827                   {
05828                     /* IE */
05829                     insn1 &= (1 << 26) - 1;
05830                     insn1 |= 32 << 26;    /* lwz */
05831                     insn2 = 0x7c631214;   /* add 3,3,2 */
05832                     rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
05833                     rel[1].r_addend = 0;
05834                     r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
05835                             + R_PPC_GOT_TPREL16);
05836                     rel->r_info = ELF32_R_INFO (r_symndx, r_type);
05837                   }
05838                 else
05839                   {
05840                     /* LE */
05841                     insn1 = 0x3c620000;   /* addis 3,2,0 */
05842                     insn2 = 0x38630000;   /* addi 3,3,0 */
05843                     if (tls_gd == 0)
05844                      {
05845                        /* Was an LD reloc.  */
05846                        r_symndx = 0;
05847                        rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
05848                      }
05849                     r_type = R_PPC_TPREL16_HA;
05850                     rel->r_info = ELF32_R_INFO (r_symndx, r_type);
05851                     rel[1].r_info = ELF32_R_INFO (r_symndx,
05852                                               R_PPC_TPREL16_LO);
05853                     rel[1].r_offset += d_offset;
05854                     rel[1].r_addend = rel->r_addend;
05855                   }
05856                 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - d_offset);
05857                 bfd_put_32 (output_bfd, insn2, contents + offset);
05858                 if (tls_gd == 0)
05859                   {
05860                     /* We changed the symbol on an LD reloc.  Start over
05861                       in order to get h, sym, sec etc. right.  */
05862                     rel--;
05863                     continue;
05864                   }
05865               }
05866            }
05867          break;
05868        }
05869 
05870       /* Handle other relocations that tweak non-addend part of insn.  */
05871       branch_bit = 0;
05872       switch (r_type)
05873        {
05874        default:
05875          break;
05876 
05877          /* Branch taken prediction relocations.  */
05878        case R_PPC_ADDR14_BRTAKEN:
05879        case R_PPC_REL14_BRTAKEN:
05880          branch_bit = BRANCH_PREDICT_BIT;
05881          /* Fall thru */
05882 
05883          /* Branch not taken prediction relocations.  */
05884        case R_PPC_ADDR14_BRNTAKEN:
05885        case R_PPC_REL14_BRNTAKEN:
05886          insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
05887          insn &= ~BRANCH_PREDICT_BIT;
05888          insn |= branch_bit;
05889 
05890          from = (rel->r_offset
05891                 + input_section->output_offset
05892                 + input_section->output_section->vma);
05893 
05894          /* Invert 'y' bit if not the default.  */
05895          if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
05896            insn ^= BRANCH_PREDICT_BIT;
05897 
05898          bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
05899          break;
05900        }
05901 
05902       addend = rel->r_addend;
05903       tls_type = 0;
05904       howto = NULL;
05905       if (r_type < R_PPC_max)
05906        howto = ppc_elf_howto_table[r_type];
05907       switch (r_type)
05908        {
05909        default:
05910          (*_bfd_error_handler)
05911            (_("%B: unknown relocation type %d for symbol %s"),
05912             input_bfd, (int) r_type, sym_name);
05913 
05914          bfd_set_error (bfd_error_bad_value);
05915          ret = FALSE;
05916          continue;
05917 
05918        case R_PPC_NONE:
05919        case R_PPC_TLS:
05920        case R_PPC_EMB_MRKREF:
05921        case R_PPC_GNU_VTINHERIT:
05922        case R_PPC_GNU_VTENTRY:
05923          continue;
05924 
05925          /* GOT16 relocations.  Like an ADDR16 using the symbol's
05926             address in the GOT as relocation value instead of the
05927             symbol's value itself.  Also, create a GOT entry for the
05928             symbol and put the symbol value there.  */
05929        case R_PPC_GOT_TLSGD16:
05930        case R_PPC_GOT_TLSGD16_LO:
05931        case R_PPC_GOT_TLSGD16_HI:
05932        case R_PPC_GOT_TLSGD16_HA:
05933          tls_type = TLS_TLS | TLS_GD;
05934          goto dogot;
05935 
05936        case R_PPC_GOT_TLSLD16:
05937        case R_PPC_GOT_TLSLD16_LO:
05938        case R_PPC_GOT_TLSLD16_HI:
05939        case R_PPC_GOT_TLSLD16_HA:
05940          tls_type = TLS_TLS | TLS_LD;
05941          goto dogot;
05942 
05943        case R_PPC_GOT_TPREL16:
05944        case R_PPC_GOT_TPREL16_LO:
05945        case R_PPC_GOT_TPREL16_HI:
05946        case R_PPC_GOT_TPREL16_HA:
05947          tls_type = TLS_TLS | TLS_TPREL;
05948          goto dogot;
05949 
05950        case R_PPC_GOT_DTPREL16:
05951        case R_PPC_GOT_DTPREL16_LO:
05952        case R_PPC_GOT_DTPREL16_HI:
05953        case R_PPC_GOT_DTPREL16_HA:
05954          tls_type = TLS_TLS | TLS_DTPREL;
05955          goto dogot;
05956 
05957        case R_PPC_GOT16:
05958        case R_PPC_GOT16_LO:
05959        case R_PPC_GOT16_HI:
05960        case R_PPC_GOT16_HA:
05961        dogot:
05962          {
05963            /* Relocation is to the entry for this symbol in the global
05964               offset table.  */
05965            bfd_vma off;
05966            bfd_vma *offp;
05967            unsigned long indx;
05968 
05969            if (htab->got == NULL)
05970              abort ();
05971 
05972            indx = 0;
05973            if (tls_type == (TLS_TLS | TLS_LD)
05974               && (h == NULL
05975                   || !h->def_dynamic))
05976              offp = &htab->tlsld_got.offset;
05977            else if (h != NULL)
05978              {
05979               bfd_boolean dyn;
05980               dyn = htab->elf.dynamic_sections_created;
05981               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
05982                   || (info->shared
05983                      && SYMBOL_REFERENCES_LOCAL (info, h)))
05984                 /* This is actually a static link, or it is a
05985                    -Bsymbolic link and the symbol is defined
05986                    locally, or the symbol was forced to be local
05987                    because of a version file.  */
05988                 ;
05989               else
05990                 {
05991                   indx = h->dynindx;
05992                   unresolved_reloc = FALSE;
05993                 }
05994               offp = &h->got.offset;
05995              }
05996            else
05997              {
05998               if (local_got_offsets == NULL)
05999                 abort ();
06000               offp = &local_got_offsets[r_symndx];
06001              }
06002 
06003            /* The offset must always be a multiple of 4.  We use the
06004               least significant bit to record whether we have already
06005               processed this entry.  */
06006            off = *offp;
06007            if ((off & 1) != 0)
06008              off &= ~1;
06009            else
06010              {
06011               unsigned int tls_m = (tls_mask
06012                                   & (TLS_LD | TLS_GD | TLS_DTPREL
06013                                     | TLS_TPREL | TLS_TPRELGD));
06014 
06015               if (offp == &htab->tlsld_got.offset)
06016                 tls_m = TLS_LD;
06017               else if (h == NULL
06018                       || !h->def_dynamic)
06019                 tls_m &= ~TLS_LD;
06020 
06021               /* We might have multiple got entries for this sym.
06022                  Initialize them all.  */
06023               do
06024                 {
06025                   int tls_ty = 0;
06026 
06027                   if ((tls_m & TLS_LD) != 0)
06028                     {
06029                      tls_ty = TLS_TLS | TLS_LD;
06030                      tls_m &= ~TLS_LD;
06031                     }
06032                   else if ((tls_m & TLS_GD) != 0)
06033                     {
06034                      tls_ty = TLS_TLS | TLS_GD;
06035                      tls_m &= ~TLS_GD;
06036                     }
06037                   else if ((tls_m & TLS_DTPREL) != 0)
06038                     {
06039                      tls_ty = TLS_TLS | TLS_DTPREL;
06040                      tls_m &= ~TLS_DTPREL;
06041                     }
06042                   else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
06043                     {
06044                      tls_ty = TLS_TLS | TLS_TPREL;
06045                      tls_m = 0;
06046                     }
06047 
06048                   /* Generate relocs for the dynamic linker.  */
06049                   if ((info->shared || indx != 0)
06050                      && (h == NULL
06051                          || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
06052                          || h->root.type != bfd_link_hash_undefweak))
06053                     {
06054                      outrel.r_offset = (htab->got->output_section->vma
06055                                       + htab->got->output_offset
06056                                       + off);
06057                      outrel.r_addend = 0;
06058                      if (tls_ty & (TLS_LD | TLS_GD))
06059                        {
06060                          outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
06061                          if (tls_ty == (TLS_TLS | TLS_GD))
06062                            {
06063                             loc = htab->relgot->contents;
06064                             loc += (htab->relgot->reloc_count++
06065                                    * sizeof (Elf32_External_Rela));
06066                             bfd_elf32_swap_reloca_out (output_bfd,
06067                                                     &outrel, loc);
06068                             outrel.r_offset += 4;
06069                             outrel.r_info
06070                               = ELF32_R_INFO (indx, R_PPC_DTPREL32);
06071                            }
06072                        }
06073                      else if (tls_ty == (TLS_TLS | TLS_DTPREL))
06074                        outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
06075                      else if (tls_ty == (TLS_TLS | TLS_TPREL))
06076                        outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
06077                      else if (indx == 0)
06078                        outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
06079                      else
06080                        outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
06081                      if (indx == 0)
06082                        {
06083                          outrel.r_addend += relocation;
06084                          if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
06085                            outrel.r_addend -= htab->elf.tls_sec->vma;
06086                        }
06087                      loc = htab->relgot->contents;
06088                      loc += (htab->relgot->reloc_count++
06089                             * sizeof (Elf32_External_Rela));
06090                      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
06091                     }
06092 
06093                   /* Init the .got section contents if we're not
06094                      emitting a reloc.  */
06095                   else
06096                     {
06097                      bfd_vma value = relocation;
06098 
06099                      if (tls_ty == (TLS_TLS | TLS_LD))
06100                        value = 1;
06101                      else if (tls_ty != 0)
06102                        {
06103                          value -= htab->elf.tls_sec->vma + DTP_OFFSET;
06104                          if (tls_ty == (TLS_TLS | TLS_TPREL))
06105                            value += DTP_OFFSET - TP_OFFSET;
06106 
06107                          if (tls_ty == (TLS_TLS | TLS_GD))
06108                            {
06109                             bfd_put_32 (output_bfd, value,
06110                                        htab->got->contents + off + 4);
06111                             value = 1;
06112                            }
06113                        }
06114                      bfd_put_32 (output_bfd, value,
06115                                 htab->got->contents + off);
06116                     }
06117 
06118                   off += 4;
06119                   if (tls_ty & (TLS_LD | TLS_GD))
06120                     off += 4;
06121                 }
06122               while (tls_m != 0);
06123 
06124               off = *offp;
06125               *offp = off | 1;
06126              }
06127 
06128            if (off >= (bfd_vma) -2)
06129              abort ();
06130 
06131            if ((tls_type & TLS_TLS) != 0)
06132              {
06133               if (tls_type != (TLS_TLS | TLS_LD))
06134                 {
06135                   if ((tls_mask & TLS_LD) != 0
06136                      && !(h == NULL
06137                           || !h->def_dynamic))
06138                     off += 8;
06139                   if (tls_type != (TLS_TLS | TLS_GD))
06140                     {
06141                      if ((tls_mask & TLS_GD) != 0)
06142                        off += 8;
06143                      if (tls_type != (TLS_TLS | TLS_DTPREL))
06144                        {
06145                          if ((tls_mask & TLS_DTPREL) != 0)
06146                            off += 4;
06147                        }
06148                     }
06149                 }
06150              }
06151 
06152            relocation = htab->got->output_offset + off;
06153            relocation -= htab->elf.hgot->root.u.def.value;
06154 
06155            /* Addends on got relocations don't make much sense.
06156               x+off@got is actually x@got+off, and since the got is
06157               generated by a hash table traversal, the value in the
06158               got at entry m+n bears little relation to the entry m.  */
06159            if (addend != 0)
06160              (*_bfd_error_handler)
06161               (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
06162                input_bfd,
06163                input_section,
06164                (long) rel->r_offset,
06165                howto->name,
06166                sym_name);
06167          }
06168        break;
06169 
06170        /* Relocations that need no special processing.  */
06171        case R_PPC_LOCAL24PC:
06172          /* It makes no sense to point a local relocation
06173             at a symbol not in this object.  */
06174          if (unresolved_reloc)
06175            {
06176              if (! (*info->callbacks->undefined_symbol) (info,
06177                                                    h->root.root.string,
06178                                                    input_bfd,
06179                                                    input_section,
06180                                                    rel->r_offset,
06181                                                    TRUE))
06182               return FALSE;
06183              continue;
06184            }
06185          break;
06186 
06187        case R_PPC_DTPREL16:
06188        case R_PPC_DTPREL16_LO:
06189        case R_PPC_DTPREL16_HI:
06190        case R_PPC_DTPREL16_HA:
06191          addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
06192          break;
06193 
06194          /* Relocations that may need to be propagated if this is a shared
06195             object.  */
06196        case R_PPC_TPREL16:
06197        case R_PPC_TPREL16_LO:
06198        case R_PPC_TPREL16_HI:
06199        case R_PPC_TPREL16_HA:
06200          addend -= htab->elf.tls_sec->vma + TP_OFFSET;
06201          /* The TPREL16 relocs shouldn't really be used in shared
06202             libs as they will result in DT_TEXTREL being set, but
06203             support them anyway.  */
06204          goto dodyn;
06205 
06206        case R_PPC_TPREL32:
06207          addend -= htab->elf.tls_sec->vma + TP_OFFSET;
06208          goto dodyn;
06209 
06210        case R_PPC_DTPREL32:
06211          addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
06212          goto dodyn;
06213 
06214        case R_PPC_DTPMOD32:
06215          relocation = 1;
06216          addend = 0;
06217          goto dodyn;
06218 
06219        case R_PPC_REL16:
06220        case R_PPC_REL16_LO:
06221        case R_PPC_REL16_HI:
06222        case R_PPC_REL16_HA:
06223          break;
06224 
06225        case R_PPC_REL24:
06226        case R_PPC_REL32:
06227        case R_PPC_REL14:
06228        case R_PPC_REL14_BRTAKEN:
06229        case R_PPC_REL14_BRNTAKEN:
06230          /* If these relocations are not to a named symbol, they can be
06231             handled right here, no need to bother the dynamic linker.  */
06232          if (SYMBOL_REFERENCES_LOCAL (info, h)
06233              || h == htab->elf.hgot)
06234            break;
06235          /* fall through */
06236 
06237          /* Relocations that always need to be propagated if this is a shared
06238             object.  */
06239        case R_PPC_ADDR32:
06240        case R_PPC_ADDR24:
06241        case R_PPC_ADDR16:
06242        case R_PPC_ADDR16_LO:
06243        case R_PPC_ADDR16_HI:
06244        case R_PPC_ADDR16_HA:
06245        case R_PPC_ADDR14:
06246        case R_PPC_ADDR14_BRTAKEN:
06247        case R_PPC_ADDR14_BRNTAKEN:
06248        case R_PPC_UADDR32:
06249        case R_PPC_UADDR16:
06250        dodyn:
06251          if ((input_section->flags & SEC_ALLOC) == 0)
06252            break;
06253          /* Fall thru.  */
06254 
06255          if ((info->shared
06256               && (h == NULL
06257                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
06258                  || h->root.type != bfd_link_hash_undefweak)
06259               && (MUST_BE_DYN_RELOC (r_type)
06260                  || !SYMBOL_CALLS_LOCAL (info, h)))
06261              || (ELIMINATE_COPY_RELOCS
06262                 && !info->shared
06263                 && h != NULL
06264                 && h->dynindx != -1
06265                 && !h->non_got_ref
06266                 && h->def_dynamic
06267                 && !h->def_regular))
06268            {
06269              int skip;
06270 
06271 #ifdef DEBUG
06272              fprintf (stderr, "ppc_elf_relocate_section needs to "
06273                      "create relocation for %s\n",
06274                      (h && h->root.root.string
06275                      ? h->root.root.string : "<unknown>"));
06276 #endif
06277 
06278              /* When generating a shared object, these relocations
06279                are copied into the output file to be resolved at run
06280                time.  */
06281              if (sreloc == NULL)
06282               {
06283                 const char *name;
06284 
06285                 name = (bfd_elf_string_from_elf_section
06286                        (input_bfd,
06287                         elf_elfheader (input_bfd)->e_shstrndx,
06288                         elf_section_data (input_section)->rel_hdr.sh_name));
06289                 if (name == NULL)
06290                   return FALSE;
06291 
06292                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
06293                            && strcmp (bfd_get_section_name (input_bfd,
06294                                                         input_section),
06295                                     name + 5) == 0);
06296 
06297                 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
06298                 BFD_ASSERT (sreloc != NULL);
06299               }
06300 
06301              skip = 0;
06302 
06303              outrel.r_offset =
06304               _bfd_elf_section_offset (output_bfd, info, input_section,
06305                                     rel->r_offset);
06306              if (outrel.r_offset == (bfd_vma) -1
06307                 || outrel.r_offset == (bfd_vma) -2)
06308               skip = (int) outrel.r_offset;
06309              outrel.r_offset += (input_section->output_section->vma
06310                               + input_section->output_offset);
06311 
06312              if (skip)
06313               memset (&outrel, 0, sizeof outrel);
06314              else if (!SYMBOL_REFERENCES_LOCAL (info, h))
06315               {
06316                 unresolved_reloc = FALSE;
06317                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
06318                 outrel.r_addend = rel->r_addend;
06319               }
06320              else
06321               {
06322                 outrel.r_addend = relocation + rel->r_addend;
06323 
06324                 if (r_type == R_PPC_ADDR32)
06325                   outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
06326                 else
06327                   {
06328                     long indx;
06329 
06330                     if (bfd_is_abs_section (sec))
06331                      indx = 0;
06332                     else if (sec == NULL || sec->owner == NULL)
06333                      {
06334                        bfd_set_error (bfd_error_bad_value);
06335                        return FALSE;
06336                      }
06337                     else
06338                      {
06339                        asection *osec;
06340 
06341                        /* We are turning this relocation into one
06342                           against a section symbol.  It would be
06343                           proper to subtract the symbol's value,
06344                           osec->vma, from the emitted reloc addend,
06345                           but ld.so expects buggy relocs.  */
06346                        osec = sec->output_section;
06347                        indx = elf_section_data (osec)->dynindx;
06348                        if (indx == 0)
06349                          {
06350                            osec = htab->elf.text_index_section;
06351                            indx = elf_section_data (osec)->dynindx;
06352                          }
06353                        BFD_ASSERT (indx != 0);
06354 #ifdef DEBUG
06355                        if (indx == 0)
06356                          printf ("indx=%ld section=%s flags=%08x name=%s\n",
06357                                 indx, osec->name, osec->flags,
06358                                 h->root.root.string);
06359 #endif
06360                      }
06361 
06362                     outrel.r_info = ELF32_R_INFO (indx, r_type);
06363                   }
06364               }
06365 
06366              loc = sreloc->contents;
06367              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
06368              bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
06369 
06370              if (skip == -1)
06371               continue;
06372 
06373              /* This reloc will be computed at runtime.  We clear the memory
06374                so that it contains predictable value.  */
06375              if (! skip
06376                 && ((input_section->flags & SEC_ALLOC) != 0
06377                     || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
06378               {
06379                 relocation = howto->pc_relative ? outrel.r_offset : 0;
06380                 addend = 0;
06381                 break;
06382               }
06383            }
06384          break;
06385 
06386        case R_PPC_RELAX32PC_PLT:
06387        case R_PPC_RELAX32_PLT:
06388          {
06389            struct plt_entry *ent = find_plt_ent (h, got2, addend);
06390 
06391            if (htab->plt_type == PLT_NEW)
06392              relocation = (htab->glink->output_section->vma
06393                          + htab->glink->output_offset
06394                          + ent->glink_offset);
06395            else
06396              relocation = (htab->plt->output_section->vma
06397                          + htab->plt->output_offset
06398                          + ent->plt.offset);
06399            addend = 0;
06400          }
06401          if (r_type == R_PPC_RELAX32_PLT)
06402            goto relax32;
06403          /* Fall thru */
06404 
06405        case R_PPC_RELAX32PC:
06406          relocation -= (input_section->output_section->vma
06407                       + input_section->output_offset
06408                       + rel->r_offset - 4);
06409          /* Fall thru */
06410 
06411        case R_PPC_RELAX32:
06412        relax32:
06413          {
06414            unsigned long t0;
06415            unsigned long t1;
06416 
06417            t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
06418            t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
06419 
06420            /* We're clearing the bits for R_PPC_ADDR16_HA
06421               and R_PPC_ADDR16_LO here.  */
06422            t0 &= ~0xffff;
06423            t1 &= ~0xffff;
06424 
06425            /* t0 is HA, t1 is LO */
06426            relocation += addend;
06427            t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
06428            t1 |= relocation & 0xffff;
06429 
06430            bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
06431            bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
06432          }
06433          continue;
06434 
06435          /* Indirect .sdata relocation.  */
06436        case R_PPC_EMB_SDAI16:
06437          BFD_ASSERT (htab->sdata[0].section != NULL);
06438          relocation
06439            = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
06440                                            h, relocation, rel);
06441          break;
06442 
06443          /* Indirect .sdata2 relocation.  */
06444        case R_PPC_EMB_SDA2I16:
06445          BFD_ASSERT (htab->sdata[1].section != NULL);
06446          relocation
06447            = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
06448                                            h, relocation, rel);
06449          break;
06450 
06451          /* Handle the TOC16 reloc.  We want to use the offset within the .got
06452             section, not the actual VMA.  This is appropriate when generating
06453             an embedded ELF object, for which the .got section acts like the
06454             AIX .toc section.  */
06455        case R_PPC_TOC16:                  /* phony GOT16 relocations */
06456          BFD_ASSERT (sec != NULL);
06457          BFD_ASSERT (bfd_is_und_section (sec)
06458                     || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
06459                     || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
06460 
06461            addend -= sec->output_section->vma + sec->output_offset + 0x8000;
06462          break;
06463 
06464        case R_PPC_PLTREL24:
06465          /* Relocation is to the entry for this symbol in the
06466             procedure linkage table.  */
06467          {
06468            struct plt_entry *ent = find_plt_ent (h, got2, addend);
06469 
06470            addend = 0;
06471            if (ent == NULL
06472               || htab->plt == NULL)
06473              {
06474               /* We didn't make a PLT entry for this symbol.  This
06475                  happens when statically linking PIC code, or when
06476                  using -Bsymbolic.  */
06477               break;
06478              }
06479 
06480            unresolved_reloc = FALSE;
06481            if (htab->plt_type == PLT_NEW)
06482              relocation = (htab->glink->output_section->vma
06483                          + htab->glink->output_offset
06484                          + ent->glink_offset);
06485            else
06486              relocation = (htab->plt->output_section->vma
06487                          + htab->plt->output_offset
06488                          + ent->plt.offset);
06489          }
06490          break;
06491 
06492          /* Relocate against _SDA_BASE_.  */
06493        case R_PPC_SDAREL16:
06494          {
06495            const char *name;
06496            struct elf_link_hash_entry *sh;
06497 
06498            BFD_ASSERT (sec != NULL);
06499            name = bfd_get_section_name (abfd, sec->output_section);
06500            if (! ((CONST_STRNEQ (name, ".sdata")
06501                   && (name[6] == 0 || name[6] == '.'))
06502                  || (CONST_STRNEQ (name, ".sbss")
06503                      && (name[5] == 0 || name[5] == '.'))))
06504              {
06505               (*_bfd_error_handler)
06506                 (_("%B: the target (%s) of a %s relocation is "
06507                    "in the wrong output section (%s)"),
06508                  input_bfd,
06509                  sym_name,
06510                  howto->name,
06511                  name);
06512              }
06513            sh = htab->sdata[0].sym;
06514            addend -= (sh->root.u.def.value
06515                      + sh->root.u.def.section->output_offset
06516                      + sh->root.u.def.section->output_section->vma);
06517          }
06518          break;
06519 
06520          /* Relocate against _SDA2_BASE_.  */
06521        case R_PPC_EMB_SDA2REL:
06522          {
06523            const char *name;
06524            struct elf_link_hash_entry *sh;
06525 
06526            BFD_ASSERT (sec != NULL);
06527            name = bfd_get_section_name (abfd, sec->output_section);
06528            if (! (CONST_STRNEQ (name, ".sdata2")
06529                  || CONST_STRNEQ (name, ".sbss2")))
06530              {
06531               (*_bfd_error_handler)
06532                 (_("%B: the target (%s) of a %s relocation is "
06533                    "in the wrong output section (%s)"),
06534                  input_bfd,
06535                  sym_name,
06536                  howto->name,
06537                  name);
06538 
06539               bfd_set_error (bfd_error_bad_value);
06540               ret = FALSE;
06541               continue;
06542              }
06543            sh = htab->sdata[1].sym;
06544            addend -= (sh->root.u.def.value
06545                      + sh->root.u.def.section->output_offset
06546                      + sh->root.u.def.section->output_section->vma);
06547          }
06548          break;
06549 
06550          /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
06551        case R_PPC_EMB_SDA21:
06552        case R_PPC_EMB_RELSDA:
06553          {
06554            const char *name;
06555            int reg;
06556            struct elf_link_hash_entry *sh;
06557 
06558            BFD_ASSERT (sec != NULL);
06559            name = bfd_get_section_name (abfd, sec->output_section);
06560            if (((CONST_STRNEQ (name, ".sdata")
06561                 && (name[6] == 0 || name[6] == '.'))
06562                || (CONST_STRNEQ (name, ".sbss")
06563                    && (name[5] == 0 || name[5] == '.'))))
06564              {
06565               reg = 13;
06566               sh = htab->sdata[0].sym;
06567               addend -= (sh->root.u.def.value
06568                         + sh->root.u.def.section->output_offset
06569                         + sh->root.u.def.section->output_section->vma);
06570              }
06571 
06572            else if (CONST_STRNEQ (name, ".sdata2")
06573                    || CONST_STRNEQ (name, ".sbss2"))
06574              {
06575               reg = 2;
06576               sh = htab->sdata[1].sym;
06577               addend -= (sh->root.u.def.value
06578                         + sh->root.u.def.section->output_offset
06579                         + sh->root.u.def.section->output_section->vma);
06580              }
06581 
06582            else if (strcmp (name, ".PPC.EMB.sdata0") == 0
06583                    || strcmp (name, ".PPC.EMB.sbss0") == 0)
06584              {
06585               reg = 0;
06586              }
06587 
06588            else
06589              {
06590               (*_bfd_error_handler)
06591                 (_("%B: the target (%s) of a %s relocation is "
06592                    "in the wrong output section (%s)"),
06593                  input_bfd,
06594                  sym_name,
06595                  howto->name,
06596                  name);
06597 
06598               bfd_set_error (bfd_error_bad_value);
06599               ret = FALSE;
06600               continue;
06601              }
06602 
06603            if (r_type == R_PPC_EMB_SDA21)
06604              {                     /* fill in register field */
06605               insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
06606               insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
06607               bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
06608              }
06609          }
06610          break;
06611 
06612          /* Relocate against the beginning of the section.  */
06613        case R_PPC_SECTOFF:
06614        case R_PPC_SECTOFF_LO:
06615        case R_PPC_SECTOFF_HI:
06616        case R_PPC_SECTOFF_HA:
06617          BFD_ASSERT (sec != NULL);
06618          addend -= sec->output_section->vma;
06619          break;
06620 
06621          /* Negative relocations.  */
06622        case R_PPC_EMB_NADDR32:
06623        case R_PPC_EMB_NADDR16:
06624        case R_PPC_EMB_NADDR16_LO:
06625        case R_PPC_EMB_NADDR16_HI:
06626        case R_PPC_EMB_NADDR16_HA:
06627          addend -= 2 * relocation;
06628          break;
06629 
06630        case R_PPC_COPY:
06631        case R_PPC_GLOB_DAT:
06632        case R_PPC_JMP_SLOT:
06633        case R_PPC_RELATIVE:
06634        case R_PPC_PLT32:
06635        case R_PPC_PLTREL32:
06636        case R_PPC_PLT16_LO:
06637        case R_PPC_PLT16_HI:
06638        case R_PPC_PLT16_HA:
06639        case R_PPC_ADDR30:
06640        case R_PPC_EMB_RELSEC16:
06641        case R_PPC_EMB_RELST_LO:
06642        case R_PPC_EMB_RELST_HI:
06643        case R_PPC_EMB_RELST_HA:
06644        case R_PPC_EMB_BIT_FLD:
06645          (*_bfd_error_handler)
06646            (_("%B: relocation %s is not yet supported for symbol %s."),
06647             input_bfd,
06648             howto->name,
06649             sym_name);
06650 
06651          bfd_set_error (bfd_error_invalid_operation);
06652          ret = FALSE;
06653          continue;
06654        }
06655 
06656       /* Do any further special processing.  */
06657       switch (r_type)
06658        {
06659        default:
06660          break;
06661 
06662        case R_PPC_ADDR16_HA:
06663        case R_PPC_REL16_HA:
06664        case R_PPC_SECTOFF_HA:
06665        case R_PPC_TPREL16_HA:
06666        case R_PPC_DTPREL16_HA:
06667        case R_PPC_EMB_NADDR16_HA:
06668        case R_PPC_EMB_RELST_HA:
06669          /* It's just possible that this symbol is a weak symbol
06670             that's not actually defined anywhere.  In that case,
06671             'sec' would be NULL, and we should leave the symbol
06672             alone (it will be set to zero elsewhere in the link).  */
06673          if (sec == NULL)
06674            break;
06675          /* Fall thru */
06676 
06677        case R_PPC_PLT16_HA:
06678        case R_PPC_GOT16_HA:
06679        case R_PPC_GOT_TLSGD16_HA:
06680        case R_PPC_GOT_TLSLD16_HA:
06681        case R_PPC_GOT_TPREL16_HA:
06682        case R_PPC_GOT_DTPREL16_HA:
06683          /* Add 0x10000 if sign bit in 0:15 is set.
06684             Bits 0:15 are not used.  */
06685          addend += 0x8000;
06686          break;
06687        }
06688 
06689 #ifdef DEBUG
06690       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
06691               "offset = %ld, addend = %ld\n",
06692               howto->name,
06693               (int) r_type,
06694               sym_name,
06695               r_symndx,
06696               (long) rel->r_offset,
06697               (long) addend);
06698 #endif
06699 
06700       if (unresolved_reloc
06701          && !((input_section->flags & SEC_DEBUGGING) != 0
06702               && h->def_dynamic))
06703        {
06704          (*_bfd_error_handler)
06705            (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
06706             input_bfd,
06707             input_section,
06708             (long) rel->r_offset,
06709             howto->name,
06710             sym_name);
06711          ret = FALSE;
06712        }
06713 
06714       r = _bfd_final_link_relocate (howto,
06715                                 input_bfd,
06716                                 input_section,
06717                                 contents,
06718                                 rel->r_offset,
06719                                 relocation,
06720                                 addend);
06721 
06722       if (r != bfd_reloc_ok)
06723        {
06724          if (r == bfd_reloc_overflow)
06725            {
06726              if (warned)
06727               continue;
06728              if (h != NULL
06729                 && h->root.type == bfd_link_hash_undefweak
06730                 && howto->pc_relative)
06731               {
06732                 /* Assume this is a call protected by other code that
06733                    detect the symbol is undefined.  If this is the case,
06734                    we can safely ignore the overflow.  If not, the
06735                    program is hosed anyway, and a little warning isn't
06736                    going to help.  */
06737 
06738                 continue;
06739               }
06740 
06741              if (! (*info->callbacks->reloc_overflow) (info,
06742                                                  (h ? &h->root : NULL),
06743                                                  sym_name,
06744                                                  howto->name,
06745                                                  rel->r_addend,
06746                                                  input_bfd,
06747                                                  input_section,
06748                                                  rel->r_offset))
06749               return FALSE;
06750            }
06751          else
06752            {
06753              (*_bfd_error_handler)
06754               (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
06755                input_bfd, input_section,
06756                (long) rel->r_offset, howto->name, sym_name, (int) r);
06757              ret = FALSE;
06758            }
06759        }
06760     }
06761 
06762 #ifdef DEBUG
06763   fprintf (stderr, "\n");
06764 #endif
06765 
06766   return ret;
06767 }
06768 
06769 #define PPC_LO(v) ((v) & 0xffff)
06770 #define PPC_HI(v) (((v) >> 16) & 0xffff)
06771 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
06772 
06773 /* Finish up dynamic symbol handling.  We set the contents of various
06774    dynamic sections here.  */
06775 
06776 static bfd_boolean
06777 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
06778                             struct bfd_link_info *info,
06779                             struct elf_link_hash_entry *h,
06780                             Elf_Internal_Sym *sym)
06781 {
06782   struct ppc_elf_link_hash_table *htab;
06783   struct plt_entry *ent;
06784   bfd_boolean doneone;
06785 
06786 #ifdef DEBUG
06787   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
06788           h->root.root.string);
06789 #endif
06790 
06791   htab = ppc_elf_hash_table (info);
06792   BFD_ASSERT (htab->elf.dynobj != NULL);
06793 
06794   doneone = FALSE;
06795   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
06796     if (ent->plt.offset != (bfd_vma) -1)
06797       {
06798        if (!doneone)
06799          {
06800            Elf_Internal_Rela rela;
06801            bfd_byte *loc;
06802            bfd_vma reloc_index;
06803 
06804            if (htab->plt_type == PLT_NEW)
06805              reloc_index = ent->plt.offset / 4;
06806            else
06807              {
06808               reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
06809                             / htab->plt_slot_size);
06810               if (reloc_index > PLT_NUM_SINGLE_ENTRIES
06811                   && htab->plt_type == PLT_OLD)
06812                 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
06813              }
06814 
06815            /* This symbol has an entry in the procedure linkage table.
06816               Set it up.  */
06817            if (htab->plt_type == PLT_VXWORKS)
06818              {
06819               bfd_vma got_offset;
06820               const bfd_vma *plt_entry;
06821               
06822               /* The first three entries in .got.plt are reserved.  */
06823               got_offset = (reloc_index + 3) * 4;
06824 
06825               /* Use the right PLT. */
06826               plt_entry = info->shared ? ppc_elf_vxworks_pic_plt_entry
06827                          : ppc_elf_vxworks_plt_entry;
06828 
06829               /* Fill in the .plt on VxWorks.  */
06830               if (info->shared)
06831                 {
06832                   bfd_vma got_offset_hi = (got_offset >> 16)
06833                                        + ((got_offset & 0x8000) >> 15);
06834 
06835                   bfd_put_32 (output_bfd,
06836                             plt_entry[0] | (got_offset_hi & 0xffff),
06837                             htab->plt->contents + ent->plt.offset + 0);
06838                   bfd_put_32 (output_bfd,
06839                             plt_entry[1] | (got_offset & 0xffff),
06840                             htab->plt->contents + ent->plt.offset + 4);
06841                 }
06842               else
06843                 {
06844                   bfd_vma got_loc
06845                     = (got_offset
06846                       + htab->elf.hgot->root.u.def.value
06847                       + htab->elf.hgot->root.u.def.section->output_offset
06848                       + htab->elf.hgot->root.u.def.section->output_section->vma);
06849                   bfd_vma got_loc_hi = (got_loc >> 16)
06850                                     + ((got_loc & 0x8000) >> 15);
06851 
06852                   bfd_put_32 (output_bfd,
06853                             plt_entry[0] | (got_loc_hi & 0xffff),
06854                             htab->plt->contents + ent->plt.offset + 0);
06855                   bfd_put_32 (output_bfd,
06856                             plt_entry[1] | (got_loc & 0xffff),
06857                             htab->plt->contents + ent->plt.offset + 4);
06858                 }
06859 
06860               bfd_put_32 (output_bfd, plt_entry[2],
06861                          htab->plt->contents + ent->plt.offset + 8);
06862               bfd_put_32 (output_bfd, plt_entry[3],
06863                          htab->plt->contents + ent->plt.offset + 12);
06864 
06865               /* This instruction is an immediate load.  The value loaded is
06866                  the byte offset of the R_PPC_JMP_SLOT relocation from the
06867                  start of the .rela.plt section.  The value is stored in the
06868                  low-order 16 bits of the load instruction.  */
06869               /* NOTE: It appears that this is now an index rather than a
06870                  prescaled offset.  */
06871               bfd_put_32 (output_bfd, 
06872                          plt_entry[4] | reloc_index,
06873                          htab->plt->contents + ent->plt.offset + 16);
06874               /* This instruction is a PC-relative branch whose target is
06875                  the start of the PLT section.  The address of this branch
06876                  instruction is 20 bytes beyond the start of this PLT entry.
06877                  The address is encoded in bits 6-29, inclusive.  The value
06878                  stored is right-shifted by two bits, permitting a 26-bit
06879                  offset.  */
06880               bfd_put_32 (output_bfd, 
06881                          (plt_entry[5] 
06882                           | (-(ent->plt.offset + 20) & 0x03fffffc)),
06883                          htab->plt->contents + ent->plt.offset + 20);
06884               bfd_put_32 (output_bfd, plt_entry[6],
06885                          htab->plt->contents + ent->plt.offset + 24);
06886               bfd_put_32 (output_bfd, plt_entry[7],
06887                          htab->plt->contents + ent->plt.offset + 28);
06888 
06889               /* Fill in the GOT entry corresponding to this PLT slot with
06890                  the address immediately after the the "bctr" instruction
06891                  in this PLT entry.  */
06892               bfd_put_32 (output_bfd, (htab->plt->output_section->vma
06893                                     + htab->plt->output_offset
06894                                     + ent->plt.offset + 16),
06895                          htab->sgotplt->contents + got_offset);
06896 
06897               if (!info->shared)
06898                 {
06899                   /* Fill in a couple of entries in .rela.plt.unloaded.  */
06900                   loc = htab->srelplt2->contents
06901                     + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
06902                        * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
06903                       * sizeof (Elf32_External_Rela));
06904 
06905                   /* Provide the @ha relocation for the first instruction.  */
06906                   rela.r_offset = (htab->plt->output_section->vma
06907                                  + htab->plt->output_offset
06908                                  + ent->plt.offset + 2);
06909                   rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
06910                                           R_PPC_ADDR16_HA);
06911                   rela.r_addend = got_offset;
06912                   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
06913                   loc += sizeof (Elf32_External_Rela);
06914 
06915                   /* Provide the @l relocation for the second instruction.  */
06916                   rela.r_offset = (htab->plt->output_section->vma
06917                                  + htab->plt->output_offset
06918                                  + ent->plt.offset + 6);
06919                   rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
06920                                           R_PPC_ADDR16_LO);
06921                   rela.r_addend = got_offset;
06922                   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
06923                   loc += sizeof (Elf32_External_Rela);
06924 
06925                   /* Provide a relocation for the GOT entry corresponding to this
06926                      PLT slot.  Point it at the middle of the .plt entry.  */
06927                   rela.r_offset = (htab->sgotplt->output_section->vma
06928                                  + htab->sgotplt->output_offset
06929                                  + got_offset);
06930                   rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
06931                                           R_PPC_ADDR32);
06932                   rela.r_addend = ent->plt.offset + 16;
06933                   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
06934                 }
06935 
06936               /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
06937                  In particular, the offset for the relocation is not the
06938                  address of the PLT entry for this function, as specified
06939                  by the ABI.  Instead, the offset is set to the address of
06940                  the GOT slot for this function.  See EABI 4.4.4.1.  */
06941               rela.r_offset = (htab->sgotplt->output_section->vma
06942                              + htab->sgotplt->output_offset
06943                              + got_offset);
06944 
06945              }
06946            else
06947              {
06948               rela.r_offset = (htab->plt->output_section->vma
06949                              + htab->plt->output_offset
06950                              + ent->plt.offset);
06951               if (htab->plt_type == PLT_OLD)
06952                 {
06953                   /* We don't need to fill in the .plt.  The ppc dynamic
06954                      linker will fill it in.  */
06955                 }
06956               else
06957                 {
06958                   bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
06959                                + htab->glink->output_section->vma
06960                                + htab->glink->output_offset);
06961                   bfd_put_32 (output_bfd, val,
06962                             htab->plt->contents + ent->plt.offset);
06963                 }
06964              }
06965 
06966            /* Fill in the entry in the .rela.plt section.  */
06967            rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
06968            rela.r_addend = 0;
06969 
06970            loc = (htab->relplt->contents
06971                  + reloc_index * sizeof (Elf32_External_Rela));
06972            bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
06973 
06974            if (!h->def_regular)
06975              {
06976               /* Mark the symbol as undefined, rather than as defined in
06977                  the .plt section.  Leave the value alone.  */
06978               sym->st_shndx = SHN_UNDEF;
06979               /* If the symbol is weak, we do need to clear the value.
06980                  Otherwise, the PLT entry would provide a definition for
06981                  the symbol even if the symbol wasn't defined anywhere,
06982                  and so the symbol would never be NULL.  */
06983               if (!h->ref_regular_nonweak)
06984                 sym->st_value = 0;
06985              }
06986            doneone = TRUE;
06987          }
06988 
06989        if (htab->plt_type == PLT_NEW)
06990          {
06991            bfd_vma plt;
06992            unsigned char *p;
06993 
06994            plt = (ent->plt.offset
06995                  + htab->plt->output_section->vma
06996                  + htab->plt->output_offset);
06997            p = (unsigned char *) htab->glink->contents + ent->glink_offset;
06998 
06999            if (info->shared || info->pie)
07000              {
07001               bfd_vma got = 0;
07002 
07003               if (ent->addend >= 32768)
07004                 got = (ent->addend
07005                       + ent->sec->output_section->vma
07006                       + ent->sec->output_offset);
07007               else if (htab->elf.hgot != NULL)
07008                 got = (htab->elf.hgot->root.u.def.value
07009                       + htab->elf.hgot->root.u.def.section->output_section->vma
07010                       + htab->elf.hgot->root.u.def.section->output_offset);
07011 
07012               plt -= got;
07013 
07014               if (plt + 0x8000 < 0x10000)
07015                 {
07016                   bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
07017                   p += 4;
07018                   bfd_put_32 (output_bfd, MTCTR_11, p);
07019                   p += 4;
07020                   bfd_put_32 (output_bfd, BCTR, p);
07021                   p += 4;
07022                   bfd_put_32 (output_bfd, NOP, p);
07023                   p += 4;
07024                 }
07025               else
07026                 {
07027                   bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
07028                   p += 4;
07029                   bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
07030                   p += 4;
07031                   bfd_put_32 (output_bfd, MTCTR_11, p);
07032                   p += 4;
07033                   bfd_put_32 (output_bfd, BCTR, p);
07034                   p += 4;
07035                 }
07036              }
07037            else
07038              {
07039               bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
07040               p += 4;
07041               bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
07042               p += 4;
07043               bfd_put_32 (output_bfd, MTCTR_11, p);
07044               p += 4;
07045               bfd_put_32 (output_bfd, BCTR, p);
07046               p += 4;
07047 
07048               /* We only need one non-PIC glink stub.  */
07049               break;
07050              }
07051          }
07052        else
07053          break;
07054       }
07055 
07056   if (h->needs_copy)
07057     {
07058       asection *s;
07059       Elf_Internal_Rela rela;
07060       bfd_byte *loc;
07061 
07062       /* This symbols needs a copy reloc.  Set it up.  */
07063 
07064 #ifdef DEBUG
07065       fprintf (stderr, ", copy");
07066 #endif
07067 
07068       BFD_ASSERT (h->dynindx != -1);
07069 
07070       if (ppc_elf_hash_entry (h)->has_sda_refs)
07071        s = htab->relsbss;
07072       else
07073        s = htab->relbss;
07074       BFD_ASSERT (s != NULL);
07075 
07076       rela.r_offset = (h->root.u.def.value
07077                      + h->root.u.def.section->output_section->vma
07078                      + h->root.u.def.section->output_offset);
07079       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
07080       rela.r_addend = 0;
07081       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
07082       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
07083     }
07084 
07085 #ifdef DEBUG
07086   fprintf (stderr, "\n");
07087 #endif
07088 
07089   /* Mark some specially defined symbols as absolute.  */
07090   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
07091       || (!htab->is_vxworks
07092          && (h == htab->elf.hgot
07093              || strcmp (h->root.root.string,
07094                       "_PROCEDURE_LINKAGE_TABLE_") == 0)))
07095     sym->st_shndx = SHN_ABS;
07096 
07097   return TRUE;
07098 }
07099 
07100 static enum elf_reloc_type_class
07101 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
07102 {
07103   switch (ELF32_R_TYPE (rela->r_info))
07104     {
07105     case R_PPC_RELATIVE:
07106       return reloc_class_relative;
07107     case R_PPC_REL24:
07108     case R_PPC_ADDR24:
07109     case R_PPC_JMP_SLOT:
07110       return reloc_class_plt;
07111     case R_PPC_COPY:
07112       return reloc_class_copy;
07113     default:
07114       return reloc_class_normal;
07115     }
07116 }
07117 
07118 /* Finish up the dynamic sections.  */
07119 
07120 static bfd_boolean
07121 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
07122                              struct bfd_link_info *info)
07123 {
07124   asection *sdyn;
07125   asection *splt;
07126   struct ppc_elf_link_hash_table *htab;
07127   bfd_vma got;
07128   bfd * dynobj;
07129 
07130 #ifdef DEBUG
07131   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
07132 #endif
07133 
07134   htab = ppc_elf_hash_table (info);
07135   dynobj = elf_hash_table (info)->dynobj;
07136   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
07137   if (htab->is_vxworks)
07138     splt = bfd_get_section_by_name (dynobj, ".plt");  
07139   else
07140     splt = NULL;
07141 
07142   got = 0;
07143   if (htab->elf.hgot != NULL)
07144     got = (htab->elf.hgot->root.u.def.value
07145           + htab->elf.hgot->root.u.def.section->output_section->vma
07146           + htab->elf.hgot->root.u.def.section->output_offset);
07147 
07148   if (htab->elf.dynamic_sections_created)
07149     {
07150       Elf32_External_Dyn *dyncon, *dynconend;
07151 
07152       BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
07153 
07154       dyncon = (Elf32_External_Dyn *) sdyn->contents;
07155       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
07156       for (; dyncon < dynconend; dyncon++)
07157        {
07158          Elf_Internal_Dyn dyn;
07159          asection *s;
07160 
07161          bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
07162 
07163          switch (dyn.d_tag)
07164            {
07165            case DT_PLTGOT:
07166              if (htab->is_vxworks)
07167               s = htab->sgotplt;
07168              else
07169               s = htab->plt;
07170              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
07171              break;
07172 
07173            case DT_PLTRELSZ:
07174              dyn.d_un.d_val = htab->relplt->size;
07175              break;
07176 
07177            case DT_JMPREL:
07178              s = htab->relplt;
07179              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
07180              break;
07181 
07182            case DT_PPC_GOT:
07183              dyn.d_un.d_ptr = got;
07184              break;
07185 
07186            case DT_RELASZ:
07187              if (htab->is_vxworks)
07188               {
07189                 if (htab->relplt)
07190                   dyn.d_un.d_ptr -= htab->relplt->size;
07191                 break;
07192               }
07193              continue;
07194 
07195            default:
07196              continue;
07197            }
07198 
07199          bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
07200        }
07201     }
07202 
07203   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
07204      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
07205   if (htab->got != NULL)
07206     {
07207       unsigned char *p = htab->got->contents;
07208       bfd_vma val;
07209 
07210       p += htab->elf.hgot->root.u.def.value;
07211       if (htab->plt_type == PLT_OLD)
07212        bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, p - 4);
07213 
07214       val = 0;
07215       if (sdyn != NULL)
07216        val = sdyn->output_section->vma + sdyn->output_offset;
07217       bfd_put_32 (output_bfd, val, p);
07218 
07219       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
07220     }
07221 
07222   /* Fill in the first entry in the VxWorks procedure linkage table.  */
07223   if (splt && splt->size > 0)
07224     {
07225       /* Use the right PLT. */
07226       static const bfd_vma *plt_entry = NULL;
07227       plt_entry = info->shared ? 
07228        ppc_elf_vxworks_pic_plt0_entry : ppc_elf_vxworks_plt0_entry;
07229 
07230       if (!info->shared)
07231        {
07232          bfd_vma got_value =
07233            (htab->elf.hgot->root.u.def.section->output_section->vma
07234             + htab->elf.hgot->root.u.def.section->output_offset
07235             + htab->elf.hgot->root.u.def.value);
07236          bfd_vma got_hi = (got_value >> 16) + ((got_value & 0x8000) >> 15);
07237 
07238          bfd_put_32 (output_bfd, plt_entry[0] | (got_hi & 0xffff),
07239                     splt->contents +  0);
07240          bfd_put_32 (output_bfd, plt_entry[1] | (got_value & 0xffff),
07241                     splt->contents +  4);
07242        }
07243       else
07244        {
07245          bfd_put_32 (output_bfd, plt_entry[0], splt->contents +  0);
07246          bfd_put_32 (output_bfd, plt_entry[1], splt->contents +  4);
07247        }
07248       bfd_put_32 (output_bfd, plt_entry[2], splt->contents +  8);
07249       bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
07250       bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
07251       bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
07252       bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
07253       bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
07254 
07255       if (! info->shared)
07256        {
07257          Elf_Internal_Rela rela;
07258          bfd_byte *loc;
07259 
07260          loc = htab->srelplt2->contents;
07261 
07262          /* Output the @ha relocation for the first instruction.  */
07263          rela.r_offset = (htab->plt->output_section->vma
07264                         + htab->plt->output_offset
07265                         + 2);
07266          rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
07267          rela.r_addend = 0;
07268          bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
07269          loc += sizeof (Elf32_External_Rela);
07270          
07271          /* Output the @l relocation for the second instruction.  */
07272          rela.r_offset = (htab->plt->output_section->vma
07273                         + htab->plt->output_offset
07274                         + 6);
07275          rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
07276          rela.r_addend = 0;
07277          bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
07278          loc += sizeof (Elf32_External_Rela);
07279 
07280          /* Fix up the remaining relocations.  They may have the wrong
07281             symbol index for _G_O_T_ or _P_L_T_ depending on the order
07282             in which symbols were output.  */
07283          while (loc < htab->srelplt2->contents + htab->srelplt2->size)
07284            {
07285              Elf_Internal_Rela rel;
07286 
07287              bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
07288              rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
07289              bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
07290              loc += sizeof (Elf32_External_Rela);
07291 
07292              bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
07293              rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
07294              bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
07295              loc += sizeof (Elf32_External_Rela);
07296 
07297              bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
07298              rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
07299              bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
07300              loc += sizeof (Elf32_External_Rela);
07301            }
07302        }
07303     }
07304 
07305   if (htab->glink != NULL && htab->glink->contents != NULL)
07306     {
07307       unsigned char *p;
07308       unsigned char *endp;
07309       bfd_vma res0;
07310       unsigned int i;
07311 
07312       /*
07313        * PIC glink code is the following:
07314        *
07315        * # ith PLT code stub.
07316        *   addis 11,30,(plt+(i-1)*4-got)@ha
07317        *   lwz 11,(plt+(i-1)*4-got)@l(11)
07318        *   mtctr 11
07319        *   bctr
07320        *
07321        * # A table of branches, one for each plt entry.
07322        * # The idea is that the plt call stub loads ctr (and r11) with these
07323        * # addresses, so (r11 - res_0) gives the plt index * 4.
07324        * res_0:      b PLTresolve
07325        * res_1:      b PLTresolve
07326        * .
07327        * # Some number of entries towards the end can be nops
07328        * res_n_m3: nop
07329        * res_n_m2: nop
07330        * res_n_m1:
07331        *
07332        * PLTresolve:
07333        *    addis 11,11,(1f-res_0)@ha
07334        *    mflr 0
07335        *    bcl 20,31,1f
07336        * 1: addi 11,11,(1b-res_0)@l
07337        *    mflr 12
07338        *    mtlr 0
07339        *    sub 11,11,12                # r11 = index * 4
07340        *    addis 12,12,(got+4-1b)@ha
07341        *    lwz 0,(got+4-1b)@l(12)      # got[1] address of dl_runtime_resolve
07342        *    lwz 12,(got+8-1b)@l(12)     # got[2] contains the map address
07343        *    mtctr 0
07344        *    add 0,11,11
07345        *    add 11,0,11                 # r11 = index * 12 = reloc offset.
07346        *    bctr
07347        */
07348       static const unsigned int pic_plt_resolve[] =
07349        {
07350          ADDIS_11_11,
07351          MFLR_0,
07352          BCL_20_31,
07353          ADDI_11_11,
07354          MFLR_12,
07355          MTLR_0,
07356          SUB_11_11_12,
07357          ADDIS_12_12,
07358          LWZ_0_12,
07359          LWZ_12_12,
07360          MTCTR_0,
07361          ADD_0_11_11,
07362          ADD_11_0_11,
07363          BCTR,
07364          NOP,
07365          NOP
07366        };
07367 
07368       static const unsigned int plt_resolve[] =
07369        {
07370          LIS_12,
07371          ADDIS_11_11,
07372          LWZ_0_12,
07373          ADDI_11_11,
07374          MTCTR_0,
07375          ADD_0_11_11,
07376          LWZ_12_12,
07377          ADD_11_0_11,
07378          BCTR,
07379          NOP,
07380          NOP,
07381          NOP,
07382          NOP,
07383          NOP,
07384          NOP,
07385          NOP
07386        };
07387 
07388       if (ARRAY_SIZE (pic_plt_resolve) != GLINK_PLTRESOLVE / 4)
07389        abort ();
07390       if (ARRAY_SIZE (plt_resolve) != GLINK_PLTRESOLVE / 4)
07391        abort ();
07392 
07393       /* Build the branch table, one for each plt entry (less one),
07394         and perhaps some padding.  */
07395       p = htab->glink->contents;
07396       p += htab->glink_pltresolve;
07397       endp = htab->glink->contents;
07398       endp += htab->glink->size - GLINK_PLTRESOLVE;
07399       while (p < endp - 8 * 4)
07400        {
07401          bfd_put_32 (output_bfd, B + endp - p, p);
07402          p += 4;
07403        }
07404       while (p < endp)
07405        {
07406          bfd_put_32 (output_bfd, NOP, p);
07407          p += 4;
07408        }
07409 
07410       res0 = (htab->glink_pltresolve
07411              + htab->glink->output_section->vma
07412              + htab->glink->output_offset);
07413 
07414       /* Last comes the PLTresolve stub.  */
07415       if (info->shared || info->pie)
07416        {
07417          bfd_vma bcl;
07418 
07419          for (i = 0; i < ARRAY_SIZE (pic_plt_resolve); i++)
07420            {
07421              bfd_put_32 (output_bfd, pic_plt_resolve[i], p);
07422              p += 4;
07423            }
07424          p -= 4 * ARRAY_SIZE (pic_plt_resolve);
07425 
07426          bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
07427                + htab->glink->output_section->vma
07428                + htab->glink->output_offset);
07429 
07430          bfd_put_32 (output_bfd,
07431                     ADDIS_11_11 + PPC_HA (bcl - res0), p + 0*4);
07432          bfd_put_32 (output_bfd,
07433                     ADDI_11_11 + PPC_LO (bcl - res0), p + 3*4);
07434          bfd_put_32 (output_bfd,
07435                     ADDIS_12_12 + PPC_HA (got + 4 - bcl), p + 7*4);
07436          if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
07437            {
07438              bfd_put_32 (output_bfd,
07439                        LWZ_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
07440              bfd_put_32 (output_bfd,
07441                        LWZ_12_12 + PPC_LO (got + 8 - bcl), p + 9*4);
07442            }
07443          else
07444            {
07445              bfd_put_32 (output_bfd,
07446                        LWZU_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
07447              bfd_put_32 (output_bfd,
07448                        LWZ_12_12 + 4, p + 9*4);
07449            }
07450        }
07451       else
07452        {
07453          for (i = 0; i < ARRAY_SIZE (plt_resolve); i++)
07454            {
07455              bfd_put_32 (output_bfd, plt_resolve[i], p);
07456              p += 4;
07457            }
07458          p -= 4 * ARRAY_SIZE (plt_resolve);
07459 
07460          bfd_put_32 (output_bfd,
07461                     LIS_12 + PPC_HA (got + 4), p + 0*4);
07462          bfd_put_32 (output_bfd,
07463                     ADDIS_11_11 + PPC_HA (-res0), p + 1*4);
07464          bfd_put_32 (output_bfd,
07465                     ADDI_11_11 + PPC_LO (-res0), p + 3*4);
07466          if (PPC_HA (got + 4) == PPC_HA (got + 8))
07467            {
07468              bfd_put_32 (output_bfd,
07469                        LWZ_0_12 + PPC_LO (got + 4), p + 2*4);
07470              bfd_put_32 (output_bfd,
07471                        LWZ_12_12 + PPC_LO (got + 8), p + 6*4);
07472            }
07473          else
07474            {
07475              bfd_put_32 (output_bfd,
07476                        LWZU_0_12 + PPC_LO (got + 4), p + 2*4);
07477              bfd_put_32 (output_bfd,
07478                        LWZ_12_12 + 4, p + 6*4);
07479            }
07480        }
07481     }
07482 
07483   return TRUE;
07484 }
07485 
07486 #define TARGET_LITTLE_SYM   bfd_elf32_powerpcle_vec
07487 #define TARGET_LITTLE_NAME  "elf32-powerpcle"
07488 #define TARGET_BIG_SYM             bfd_elf32_powerpc_vec
07489 #define TARGET_BIG_NAME            "elf32-powerpc"
07490 #define ELF_ARCH            bfd_arch_powerpc
07491 #define ELF_MACHINE_CODE    EM_PPC
07492 #ifdef __QNXTARGET__
07493 #define ELF_MAXPAGESIZE            0x1000
07494 #else
07495 #define ELF_MAXPAGESIZE            0x10000
07496 #endif
07497 #define ELF_MINPAGESIZE            0x1000
07498 #define ELF_COMMONPAGESIZE  0x1000
07499 #define elf_info_to_howto   ppc_elf_info_to_howto
07500 
07501 #ifdef  EM_CYGNUS_POWERPC
07502 #define ELF_MACHINE_ALT1    EM_CYGNUS_POWERPC
07503 #endif
07504 
07505 #ifdef EM_PPC_OLD
07506 #define ELF_MACHINE_ALT2    EM_PPC_OLD
07507 #endif
07508 
07509 #define elf_backend_plt_not_loaded 1
07510 #define elf_backend_can_gc_sections       1
07511 #define elf_backend_can_refcount   1
07512 #define elf_backend_rela_normal           1
07513 
07514 #define bfd_elf32_mkobject                ppc_elf_mkobject
07515 #define bfd_elf32_bfd_merge_private_bfd_data     ppc_elf_merge_private_bfd_data
07516 #define bfd_elf32_bfd_relax_section              ppc_elf_relax_section
07517 #define bfd_elf32_bfd_reloc_type_lookup          ppc_elf_reloc_type_lookup
07518 #define bfd_elf32_bfd_reloc_name_lookup   ppc_elf_reloc_name_lookup
07519 #define bfd_elf32_bfd_set_private_flags          ppc_elf_set_private_flags
07520 #define bfd_elf32_bfd_link_hash_table_create     ppc_elf_link_hash_table_create
07521 
07522 #define elf_backend_object_p                     ppc_elf_object_p
07523 #define elf_backend_gc_mark_hook          ppc_elf_gc_mark_hook
07524 #define elf_backend_gc_sweep_hook         ppc_elf_gc_sweep_hook
07525 #define elf_backend_section_from_shdr            ppc_elf_section_from_shdr
07526 #define elf_backend_relocate_section             ppc_elf_relocate_section
07527 #define elf_backend_create_dynamic_sections      ppc_elf_create_dynamic_sections
07528 #define elf_backend_check_relocs          ppc_elf_check_relocs
07529 #define elf_backend_copy_indirect_symbol  ppc_elf_copy_indirect_symbol
07530 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
07531 #define elf_backend_add_symbol_hook              ppc_elf_add_symbol_hook
07532 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
07533 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
07534 #define elf_backend_finish_dynamic_sections      ppc_elf_finish_dynamic_sections
07535 #define elf_backend_fake_sections         ppc_elf_fake_sections
07536 #define elf_backend_additional_program_headers   ppc_elf_additional_program_headers
07537 #define elf_backend_grok_prstatus         ppc_elf_grok_prstatus
07538 #define elf_backend_grok_psinfo                  ppc_elf_grok_psinfo
07539 #define elf_backend_write_core_note              ppc_elf_write_core_note
07540 #define elf_backend_reloc_type_class             ppc_elf_reloc_type_class
07541 #define elf_backend_begin_write_processing       ppc_elf_begin_write_processing
07542 #define elf_backend_final_write_processing       ppc_elf_final_write_processing
07543 #define elf_backend_write_section         ppc_elf_write_section
07544 #define elf_backend_get_sec_type_attr            ppc_elf_get_sec_type_attr
07545 #define elf_backend_plt_sym_val                  ppc_elf_plt_sym_val
07546 #define elf_backend_action_discarded             ppc_elf_action_discarded
07547 #define elf_backend_init_index_section           _bfd_elf_init_1_index_section
07548 
07549 #include "elf32-target.h"
07550 
07551 /* VxWorks Target */
07552 
07553 #undef TARGET_LITTLE_SYM
07554 #undef TARGET_LITTLE_NAME
07555 
07556 #undef TARGET_BIG_SYM
07557 #define TARGET_BIG_SYM             bfd_elf32_powerpc_vxworks_vec
07558 #undef TARGET_BIG_NAME
07559 #define TARGET_BIG_NAME            "elf32-powerpc-vxworks"
07560 
07561 /* VxWorks uses the elf default section flags for .plt.  */
07562 static const struct bfd_elf_special_section *
07563 ppc_elf_vxworks_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
07564 {
07565   if (sec->name == NULL)
07566     return NULL;
07567 
07568   if (strcmp (sec->name, ".plt") == 0)
07569     return _bfd_elf_get_sec_type_attr (abfd, sec);
07570 
07571   return ppc_elf_get_sec_type_attr (abfd, sec);
07572 }
07573 
07574 /* Like ppc_elf_link_hash_table_create, but overrides
07575    appropriately for VxWorks.  */
07576 static struct bfd_link_hash_table *
07577 ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
07578 {
07579   struct bfd_link_hash_table *ret;
07580 
07581   ret = ppc_elf_link_hash_table_create (abfd);
07582   if (ret)
07583     {
07584       struct ppc_elf_link_hash_table *htab
07585         = (struct ppc_elf_link_hash_table *)ret;
07586       htab->is_vxworks = 1;
07587       htab->plt_type = PLT_VXWORKS;
07588       htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
07589       htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
07590       htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
07591     }
07592   return ret;
07593 }
07594 
07595 /* Tweak magic VxWorks symbols as they are loaded.  */
07596 static bfd_boolean
07597 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
07598                              struct bfd_link_info *info,
07599                              Elf_Internal_Sym *sym,
07600                              const char **namep ATTRIBUTE_UNUSED,
07601                              flagword *flagsp ATTRIBUTE_UNUSED,
07602                              asection **secp,
07603                              bfd_vma *valp)
07604 {
07605   if (!elf_vxworks_add_symbol_hook(abfd, info, sym,namep, flagsp, secp,
07606                                valp))
07607     return FALSE;
07608 
07609   return ppc_elf_add_symbol_hook(abfd, info, sym,namep, flagsp, secp, valp);
07610 }
07611 
07612 static void
07613 ppc_elf_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
07614 {
07615   ppc_elf_final_write_processing(abfd, linker);
07616   elf_vxworks_final_write_processing(abfd, linker);
07617 }
07618 
07619 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
07620    define it.  */
07621 #undef elf_backend_want_plt_sym
07622 #define elf_backend_want_plt_sym          1
07623 #undef elf_backend_want_got_plt
07624 #define elf_backend_want_got_plt          1
07625 #undef elf_backend_got_symbol_offset
07626 #define elf_backend_got_symbol_offset            0
07627 #undef elf_backend_plt_not_loaded
07628 #define elf_backend_plt_not_loaded        0
07629 #undef elf_backend_plt_readonly
07630 #define elf_backend_plt_readonly          1
07631 #undef elf_backend_got_header_size
07632 #define elf_backend_got_header_size              12
07633 
07634 #undef bfd_elf32_bfd_link_hash_table_create
07635 #define bfd_elf32_bfd_link_hash_table_create \
07636   ppc_elf_vxworks_link_hash_table_create
07637 #undef elf_backend_add_symbol_hook
07638 #define elf_backend_add_symbol_hook \
07639   ppc_elf_vxworks_add_symbol_hook
07640 #undef elf_backend_link_output_symbol_hook
07641 #define elf_backend_link_output_symbol_hook \
07642   elf_vxworks_link_output_symbol_hook
07643 #undef elf_backend_final_write_processing
07644 #define elf_backend_final_write_processing \
07645   ppc_elf_vxworks_final_write_processing
07646 #undef elf_backend_get_sec_type_attr
07647 #define elf_backend_get_sec_type_attr \
07648   ppc_elf_vxworks_get_sec_type_attr
07649 #undef elf_backend_emit_relocs
07650 #define elf_backend_emit_relocs \
07651   elf_vxworks_emit_relocs
07652 
07653 #undef elf32_bed
07654 #define elf32_bed                         ppc_elf_vxworks_bed
07655 
07656 #include "elf32-target.h"