Home | History | Annotate | Line # | Download | only in bfd
elf32-ppc.c revision 1.9
      1 /* PowerPC-specific support for 32-bit ELF
      2    Copyright (C) 1994-2020 Free Software Foundation, Inc.
      3    Written by Ian Lance Taylor, Cygnus Support.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the
     19    Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
     20    Boston, MA 02110-1301, USA.  */
     21 
     22 
     23 /* This file is based on a preliminary PowerPC ELF ABI.  The
     24    information may not match the final PowerPC ELF ABI.  It includes
     25    suggestions from the in-progress Embedded PowerPC ABI, and that
     26    information may also not match.  */
     27 
     28 #include "sysdep.h"
     29 #include <stdarg.h>
     30 #include "bfd.h"
     31 #include "bfdlink.h"
     32 #include "libbfd.h"
     33 #include "elf-bfd.h"
     34 #include "elf/ppc.h"
     35 #include "elf32-ppc.h"
     36 #include "elf-vxworks.h"
     37 #include "dwarf2.h"
     38 #include "opcode/ppc.h"
     39 
     40 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
     41 #define OCTETS_PER_BYTE(ABFD, SEC) 1
     42 
     43 typedef enum split16_format_type
     44 {
     45   split16a_type = 0,
     46   split16d_type
     47 }
     48 split16_format_type;
     49 
     50 /* RELA relocations are used here.  */
     51 
     52 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
     53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     54 static bfd_reloc_status_type ppc_elf_unhandled_reloc
     55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     56 
     57 /* Branch prediction bit for branch taken relocs.  */
     58 #define BRANCH_PREDICT_BIT 0x200000
     59 /* Mask to set RA in memory instructions.  */
     60 #define RA_REGISTER_MASK 0x001f0000
     61 /* Value to shift register by to insert RA.  */
     62 #define RA_REGISTER_SHIFT 16
     63 
     64 /* The name of the dynamic interpreter.  This is put in the .interp
     65    section.  */
     66 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
     67 
     68 /* For old-style PLT.  */
     69 /* The number of single-slot PLT entries (the rest use two slots).  */
     70 #define PLT_NUM_SINGLE_ENTRIES 8192
     71 
     72 /* For new-style .glink and .plt.  */
     73 #define GLINK_PLTRESOLVE 16*4
     74 #define GLINK_ENTRY_SIZE(htab, h)					\
     75   ((4*4									\
     76     + (h != NULL							\
     77        && h == htab->tls_get_addr					\
     78        && !htab->params->no_tls_get_addr_opt ? 8*4 : 0)			\
     79     + (1u << htab->params->plt_stub_align) - 1)				\
     80    & -(1u << htab->params->plt_stub_align))
     81 
     82 /* VxWorks uses its own plt layout, filled in by the static linker.  */
     83 
     84 /* The standard VxWorks PLT entry.  */
     85 #define VXWORKS_PLT_ENTRY_SIZE 32
     86 static const bfd_vma ppc_elf_vxworks_plt_entry
     87     [VXWORKS_PLT_ENTRY_SIZE / 4] =
     88   {
     89     0x3d800000, /* lis	   r12,0		 */
     90     0x818c0000, /* lwz	   r12,0(r12)		 */
     91     0x7d8903a6, /* mtctr   r12			 */
     92     0x4e800420, /* bctr				 */
     93     0x39600000, /* li	   r11,0		 */
     94     0x48000000, /* b	   14 <.PLT0resolve+0x4> */
     95     0x60000000, /* nop				 */
     96     0x60000000, /* nop				 */
     97   };
     98 static const bfd_vma ppc_elf_vxworks_pic_plt_entry
     99     [VXWORKS_PLT_ENTRY_SIZE / 4] =
    100   {
    101     0x3d9e0000, /* addis r12,r30,0 */
    102     0x818c0000, /* lwz	 r12,0(r12) */
    103     0x7d8903a6, /* mtctr r12 */
    104     0x4e800420, /* bctr */
    105     0x39600000, /* li	 r11,0 */
    106     0x48000000, /* b	 14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
    107     0x60000000, /* nop */
    108     0x60000000, /* nop */
    109   };
    110 
    111 /* The initial VxWorks PLT entry.  */
    112 #define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
    113 static const bfd_vma ppc_elf_vxworks_plt0_entry
    114     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
    115   {
    116     0x3d800000, /* lis	   r12,0	*/
    117     0x398c0000, /* addi	   r12,r12,0	*/
    118     0x800c0008, /* lwz	   r0,8(r12)	*/
    119     0x7c0903a6, /* mtctr   r0		*/
    120     0x818c0004, /* lwz	   r12,4(r12)	*/
    121     0x4e800420, /* bctr			*/
    122     0x60000000, /* nop			*/
    123     0x60000000, /* nop			*/
    124   };
    125 static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
    126     [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
    127   {
    128     0x819e0008, /* lwz	 r12,8(r30) */
    129     0x7d8903a6, /* mtctr r12	    */
    130     0x819e0004, /* lwz	 r12,4(r30) */
    131     0x4e800420, /* bctr		    */
    132     0x60000000, /* nop		    */
    133     0x60000000, /* nop		    */
    134     0x60000000, /* nop		    */
    135     0x60000000, /* nop		    */
    136   };
    137 
    138 /* For executables, we have some additional relocations in
    139    .rela.plt.unloaded, for the kernel loader.  */
    140 
    141 /* The number of non-JMP_SLOT relocations per PLT0 slot. */
    142 #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
    143 /* The number of relocations in the PLTResolve slot. */
    144 #define VXWORKS_PLTRESOLVE_RELOCS 2
    145 /* The number of relocations in the PLTResolve slot when creating
    146    a shared library. */
    147 #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
    148 
    149 /* Some instructions.  */
    150 #define ADDIS_11_11	0x3d6b0000
    151 #define ADDIS_11_30	0x3d7e0000
    152 #define ADDIS_12_12	0x3d8c0000
    153 #define ADDI_11_11	0x396b0000
    154 #define ADD_0_11_11	0x7c0b5a14
    155 #define ADD_3_12_2	0x7c6c1214
    156 #define ADD_11_0_11	0x7d605a14
    157 #define B		0x48000000
    158 #define BA		0x48000002
    159 #define BCL_20_31	0x429f0005
    160 #define BCTR		0x4e800420
    161 #define BEQLR		0x4d820020
    162 #define CMPWI_11_0	0x2c0b0000
    163 #define LIS_11		0x3d600000
    164 #define LIS_12		0x3d800000
    165 #define LWZU_0_12	0x840c0000
    166 #define LWZ_0_12	0x800c0000
    167 #define LWZ_11_3	0x81630000
    168 #define LWZ_11_11	0x816b0000
    169 #define LWZ_11_30	0x817e0000
    170 #define LWZ_12_3	0x81830000
    171 #define LWZ_12_12	0x818c0000
    172 #define MR_0_3		0x7c601b78
    173 #define MR_3_0		0x7c030378
    174 #define MFLR_0		0x7c0802a6
    175 #define MFLR_12		0x7d8802a6
    176 #define MTCTR_0		0x7c0903a6
    177 #define MTCTR_11	0x7d6903a6
    178 #define MTLR_0		0x7c0803a6
    179 #define NOP		0x60000000
    180 #define SUB_11_11_12	0x7d6c5850
    181 
    182 /* Offset of tp and dtp pointers from start of TLS block.  */
    183 #define TP_OFFSET	0x7000
    184 #define DTP_OFFSET	0x8000
    185 
    186 /* The value of a defined global symbol.  */
    187 #define SYM_VAL(SYM) \
    188   ((SYM)->root.u.def.section->output_section->vma	\
    189    + (SYM)->root.u.def.section->output_offset		\
    190    + (SYM)->root.u.def.value)
    191 
    192 /* Relocation HOWTO's.  */
    194 /* Like other ELF RELA targets that don't apply multiple
    195    field-altering relocations to the same localation, src_mask is
    196    always zero and pcrel_offset is the same as pc_relative.
    197    PowerPC can always use a zero bitpos, even when the field is not at
    198    the LSB.  For example, a REL24 could use rightshift=2, bisize=24
    199    and bitpos=2 which matches the ABI description, or as we do here,
    200    rightshift=0, bitsize=26 and bitpos=0.  */
    201 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
    202 	    complain, special_func)				\
    203   HOWTO (type, rightshift, size, bitsize, pc_relative, 0,	\
    204 	 complain_overflow_ ## complain, special_func,		\
    205 	 #type, FALSE, 0, mask, pc_relative)
    206 
    207 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
    208 
    209 static reloc_howto_type ppc_elf_howto_raw[] = {
    210   /* This reloc does nothing.  */
    211   HOW (R_PPC_NONE, 3, 0, 0, 0, FALSE, dont,
    212        bfd_elf_generic_reloc),
    213 
    214   /* A standard 32 bit relocation.  */
    215   HOW (R_PPC_ADDR32, 2, 32, 0xffffffff, 0, FALSE, dont,
    216        bfd_elf_generic_reloc),
    217 
    218   /* An absolute 26 bit branch; the lower two bits must be zero.
    219      FIXME: we don't check that, we just clear them.  */
    220   HOW (R_PPC_ADDR24, 2, 26, 0x3fffffc, 0, FALSE, signed,
    221        bfd_elf_generic_reloc),
    222 
    223   /* A standard 16 bit relocation.  */
    224   HOW (R_PPC_ADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
    225        bfd_elf_generic_reloc),
    226 
    227   /* A 16 bit relocation without overflow.  */
    228   HOW (R_PPC_ADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    229        bfd_elf_generic_reloc),
    230 
    231   /* The high order 16 bits of an address.  */
    232   HOW (R_PPC_ADDR16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    233        bfd_elf_generic_reloc),
    234 
    235   /* The high order 16 bits of an address, plus 1 if the contents of
    236      the low 16 bits, treated as a signed number, is negative.  */
    237   HOW (R_PPC_ADDR16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    238        ppc_elf_addr16_ha_reloc),
    239 
    240   /* An absolute 16 bit branch; the lower two bits must be zero.
    241      FIXME: we don't check that, we just clear them.  */
    242   HOW (R_PPC_ADDR14, 2, 16, 0xfffc, 0, FALSE, signed,
    243        bfd_elf_generic_reloc),
    244 
    245   /* An absolute 16 bit branch, for which bit 10 should be set to
    246      indicate that the branch is expected to be taken.	The lower two
    247      bits must be zero.  */
    248   HOW (R_PPC_ADDR14_BRTAKEN, 2, 16, 0xfffc, 0, FALSE, signed,
    249        bfd_elf_generic_reloc),
    250 
    251   /* An absolute 16 bit branch, for which bit 10 should be set to
    252      indicate that the branch is not expected to be taken.  The lower
    253      two bits must be zero.  */
    254   HOW (R_PPC_ADDR14_BRNTAKEN, 2, 16, 0xfffc, 0, FALSE, signed,
    255        bfd_elf_generic_reloc),
    256 
    257   /* A relative 26 bit branch; the lower two bits must be zero.  */
    258   HOW (R_PPC_REL24, 2, 26, 0x3fffffc, 0, TRUE, signed,
    259        bfd_elf_generic_reloc),
    260 
    261   /* A relative 16 bit branch; the lower two bits must be zero.  */
    262   HOW (R_PPC_REL14, 2, 16, 0xfffc, 0, TRUE, signed,
    263        bfd_elf_generic_reloc),
    264 
    265   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    266      the branch is expected to be taken.  The lower two bits must be
    267      zero.  */
    268   HOW (R_PPC_REL14_BRTAKEN, 2, 16, 0xfffc, 0, TRUE, signed,
    269        bfd_elf_generic_reloc),
    270 
    271   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    272      the branch is not expected to be taken.  The lower two bits must
    273      be zero.  */
    274   HOW (R_PPC_REL14_BRNTAKEN, 2, 16, 0xfffc, 0, TRUE, signed,
    275        bfd_elf_generic_reloc),
    276 
    277   /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
    278      symbol.  */
    279   HOW (R_PPC_GOT16, 1, 16, 0xffff, 0, FALSE, signed,
    280        ppc_elf_unhandled_reloc),
    281 
    282   /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
    283      the symbol.  */
    284   HOW (R_PPC_GOT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    285        ppc_elf_unhandled_reloc),
    286 
    287   /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
    288      the symbol.  */
    289   HOW (R_PPC_GOT16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    290        ppc_elf_unhandled_reloc),
    291 
    292   /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
    293      the symbol.  */
    294   HOW (R_PPC_GOT16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    295        ppc_elf_unhandled_reloc),
    296 
    297   /* Like R_PPC_REL24, but referring to the procedure linkage table
    298      entry for the symbol.  */
    299   HOW (R_PPC_PLTREL24, 2, 26, 0x3fffffc, 0, TRUE, signed,
    300        ppc_elf_unhandled_reloc),
    301 
    302   /* This is used only by the dynamic linker.  The symbol should exist
    303      both in the object being run and in some shared library.  The
    304      dynamic linker copies the data addressed by the symbol from the
    305      shared library into the object, because the object being
    306      run has to have the data at some particular address.  */
    307   HOW (R_PPC_COPY, 2, 32, 0, 0, FALSE, dont,
    308        ppc_elf_unhandled_reloc),
    309 
    310   /* Like R_PPC_ADDR32, but used when setting global offset table
    311      entries.  */
    312   HOW (R_PPC_GLOB_DAT, 2, 32, 0xffffffff, 0, FALSE, dont,
    313        ppc_elf_unhandled_reloc),
    314 
    315   /* Marks a procedure linkage table entry for a symbol.  */
    316   HOW (R_PPC_JMP_SLOT, 2, 32, 0, 0, FALSE, dont,
    317        ppc_elf_unhandled_reloc),
    318 
    319   /* Used only by the dynamic linker.  When the object is run, this
    320      longword is set to the load address of the object, plus the
    321      addend.  */
    322   HOW (R_PPC_RELATIVE, 2, 32, 0xffffffff, 0, FALSE, dont,
    323        bfd_elf_generic_reloc),
    324 
    325   /* Like R_PPC_REL24, but uses the value of the symbol within the
    326      object rather than the final value.  Normally used for
    327      _GLOBAL_OFFSET_TABLE_.  */
    328   HOW (R_PPC_LOCAL24PC, 2, 26, 0x3fffffc, 0, TRUE, signed,
    329        bfd_elf_generic_reloc),
    330 
    331   /* Like R_PPC_ADDR32, but may be unaligned.  */
    332   HOW (R_PPC_UADDR32, 2, 32, 0xffffffff, 0, FALSE, dont,
    333        bfd_elf_generic_reloc),
    334 
    335   /* Like R_PPC_ADDR16, but may be unaligned.  */
    336   HOW (R_PPC_UADDR16, 1, 16, 0xffff, 0, FALSE, bitfield,
    337        bfd_elf_generic_reloc),
    338 
    339   /* 32-bit PC relative */
    340   HOW (R_PPC_REL32, 2, 32, 0xffffffff, 0, TRUE, dont,
    341        bfd_elf_generic_reloc),
    342 
    343   /* 32-bit relocation to the symbol's procedure linkage table.
    344      FIXME: not supported.  */
    345   HOW (R_PPC_PLT32, 2, 32, 0, 0, FALSE, dont,
    346        ppc_elf_unhandled_reloc),
    347 
    348   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    349      FIXME: not supported.  */
    350   HOW (R_PPC_PLTREL32, 2, 32, 0, 0, TRUE, dont,
    351        ppc_elf_unhandled_reloc),
    352 
    353   /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
    354      the symbol.  */
    355   HOW (R_PPC_PLT16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    356        ppc_elf_unhandled_reloc),
    357 
    358   /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
    359      the symbol.  */
    360   HOW (R_PPC_PLT16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    361        ppc_elf_unhandled_reloc),
    362 
    363   /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
    364      the symbol.  */
    365   HOW (R_PPC_PLT16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    366        ppc_elf_unhandled_reloc),
    367 
    368   /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
    369      small data items.  */
    370   HOW (R_PPC_SDAREL16, 1, 16, 0xffff, 0, FALSE, signed,
    371        ppc_elf_unhandled_reloc),
    372 
    373   /* 16-bit section relative relocation.  */
    374   HOW (R_PPC_SECTOFF, 1, 16, 0xffff, 0, FALSE, signed,
    375        ppc_elf_unhandled_reloc),
    376 
    377   /* 16-bit lower half section relative relocation.  */
    378   HOW (R_PPC_SECTOFF_LO, 1, 16, 0xffff, 0, FALSE, dont,
    379        ppc_elf_unhandled_reloc),
    380 
    381   /* 16-bit upper half section relative relocation.  */
    382   HOW (R_PPC_SECTOFF_HI, 1, 16, 0xffff, 16, FALSE, dont,
    383        ppc_elf_unhandled_reloc),
    384 
    385   /* 16-bit upper half adjusted section relative relocation.  */
    386   HOW (R_PPC_SECTOFF_HA, 1, 16, 0xffff, 16, FALSE, dont,
    387        ppc_elf_unhandled_reloc),
    388 
    389   /* Marker relocs for TLS.  */
    390   HOW (R_PPC_TLS, 2, 32, 0, 0, FALSE, dont,
    391        bfd_elf_generic_reloc),
    392 
    393   HOW (R_PPC_TLSGD, 2, 32, 0, 0, FALSE, dont,
    394        bfd_elf_generic_reloc),
    395 
    396   HOW (R_PPC_TLSLD, 2, 32, 0, 0, FALSE, dont,
    397        bfd_elf_generic_reloc),
    398 
    399   /* Marker relocs on inline plt call instructions.  */
    400   HOW (R_PPC_PLTSEQ, 2, 32, 0, 0, FALSE, dont,
    401        bfd_elf_generic_reloc),
    402 
    403   HOW (R_PPC_PLTCALL, 2, 32, 0, 0, FALSE, dont,
    404        bfd_elf_generic_reloc),
    405 
    406   /* Computes the load module index of the load module that contains the
    407      definition of its TLS sym.  */
    408   HOW (R_PPC_DTPMOD32, 2, 32, 0xffffffff, 0, FALSE, dont,
    409        ppc_elf_unhandled_reloc),
    410 
    411   /* Computes a dtv-relative displacement, the difference between the value
    412      of sym+add and the base address of the thread-local storage block that
    413      contains the definition of sym, minus 0x8000.  */
    414   HOW (R_PPC_DTPREL32, 2, 32, 0xffffffff, 0, FALSE, dont,
    415        ppc_elf_unhandled_reloc),
    416 
    417   /* A 16 bit dtprel reloc.  */
    418   HOW (R_PPC_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
    419        ppc_elf_unhandled_reloc),
    420 
    421   /* Like DTPREL16, but no overflow.  */
    422   HOW (R_PPC_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    423        ppc_elf_unhandled_reloc),
    424 
    425   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
    426   HOW (R_PPC_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    427        ppc_elf_unhandled_reloc),
    428 
    429   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
    430   HOW (R_PPC_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    431        ppc_elf_unhandled_reloc),
    432 
    433   /* Computes a tp-relative displacement, the difference between the value of
    434      sym+add and the value of the thread pointer (r13).  */
    435   HOW (R_PPC_TPREL32, 2, 32, 0xffffffff, 0, FALSE, dont,
    436        ppc_elf_unhandled_reloc),
    437 
    438   /* A 16 bit tprel reloc.  */
    439   HOW (R_PPC_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
    440        ppc_elf_unhandled_reloc),
    441 
    442   /* Like TPREL16, but no overflow.  */
    443   HOW (R_PPC_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    444        ppc_elf_unhandled_reloc),
    445 
    446   /* Like TPREL16_LO, but next higher group of 16 bits.  */
    447   HOW (R_PPC_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    448        ppc_elf_unhandled_reloc),
    449 
    450   /* Like TPREL16_HI, but adjust for low 16 bits.  */
    451   HOW (R_PPC_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    452        ppc_elf_unhandled_reloc),
    453 
    454   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
    455      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
    456      to the first entry.  */
    457   HOW (R_PPC_GOT_TLSGD16, 1, 16, 0xffff, 0, FALSE, signed,
    458        ppc_elf_unhandled_reloc),
    459 
    460   /* Like GOT_TLSGD16, but no overflow.  */
    461   HOW (R_PPC_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    462        ppc_elf_unhandled_reloc),
    463 
    464   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
    465   HOW (R_PPC_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    466        ppc_elf_unhandled_reloc),
    467 
    468   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
    469   HOW (R_PPC_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    470        ppc_elf_unhandled_reloc),
    471 
    472   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
    473      with values (sym+add)@dtpmod and zero, and computes the offset to the
    474      first entry.  */
    475   HOW (R_PPC_GOT_TLSLD16, 1, 16, 0xffff, 0, FALSE, signed,
    476        ppc_elf_unhandled_reloc),
    477 
    478   /* Like GOT_TLSLD16, but no overflow.  */
    479   HOW (R_PPC_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    480        ppc_elf_unhandled_reloc),
    481 
    482   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
    483   HOW (R_PPC_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    484        ppc_elf_unhandled_reloc),
    485 
    486   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
    487   HOW (R_PPC_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    488        ppc_elf_unhandled_reloc),
    489 
    490   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
    491      the offset to the entry.  */
    492   HOW (R_PPC_GOT_DTPREL16, 1, 16, 0xffff, 0, FALSE, signed,
    493        ppc_elf_unhandled_reloc),
    494 
    495   /* Like GOT_DTPREL16, but no overflow.  */
    496   HOW (R_PPC_GOT_DTPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    497        ppc_elf_unhandled_reloc),
    498 
    499   /* Like GOT_DTPREL16_LO, but next higher group of 16 bits.  */
    500   HOW (R_PPC_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    501        ppc_elf_unhandled_reloc),
    502 
    503   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
    504   HOW (R_PPC_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    505        ppc_elf_unhandled_reloc),
    506 
    507   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
    508      offset to the entry.  */
    509   HOW (R_PPC_GOT_TPREL16, 1, 16, 0xffff, 0, FALSE, signed,
    510        ppc_elf_unhandled_reloc),
    511 
    512   /* Like GOT_TPREL16, but no overflow.  */
    513   HOW (R_PPC_GOT_TPREL16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    514        ppc_elf_unhandled_reloc),
    515 
    516   /* Like GOT_TPREL16_LO, but next higher group of 16 bits.  */
    517   HOW (R_PPC_GOT_TPREL16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    518        ppc_elf_unhandled_reloc),
    519 
    520   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
    521   HOW (R_PPC_GOT_TPREL16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    522        ppc_elf_unhandled_reloc),
    523 
    524   /* The remaining relocs are from the Embedded ELF ABI, and are not
    525      in the SVR4 ELF ABI.  */
    526 
    527   /* 32 bit value resulting from the addend minus the symbol.  */
    528   HOW (R_PPC_EMB_NADDR32, 2, 32, 0xffffffff, 0, FALSE, dont,
    529        ppc_elf_unhandled_reloc),
    530 
    531   /* 16 bit value resulting from the addend minus the symbol.  */
    532   HOW (R_PPC_EMB_NADDR16, 1, 16, 0xffff, 0, FALSE, signed,
    533        ppc_elf_unhandled_reloc),
    534 
    535   /* 16 bit value resulting from the addend minus the symbol.  */
    536   HOW (R_PPC_EMB_NADDR16_LO, 1, 16, 0xffff, 0, FALSE, dont,
    537        ppc_elf_unhandled_reloc),
    538 
    539   /* The high order 16 bits of the addend minus the symbol.  */
    540   HOW (R_PPC_EMB_NADDR16_HI, 1, 16, 0xffff, 16, FALSE, dont,
    541        ppc_elf_unhandled_reloc),
    542 
    543   /* The high order 16 bits of the result of the addend minus the address,
    544      plus 1 if the contents of the low 16 bits, treated as a signed number,
    545      is negative.  */
    546   HOW (R_PPC_EMB_NADDR16_HA, 1, 16, 0xffff, 16, FALSE, dont,
    547        ppc_elf_unhandled_reloc),
    548 
    549   /* 16 bit value resulting from allocating a 4 byte word to hold an
    550      address in the .sdata section, and returning the offset from
    551      _SDA_BASE_ for that relocation.  */
    552   HOW (R_PPC_EMB_SDAI16, 1, 16, 0xffff, 0, FALSE, signed,
    553        ppc_elf_unhandled_reloc),
    554 
    555   /* 16 bit value resulting from allocating a 4 byte word to hold an
    556      address in the .sdata2 section, and returning the offset from
    557      _SDA2_BASE_ for that relocation.  */
    558   HOW (R_PPC_EMB_SDA2I16, 1, 16, 0xffff, 0, FALSE, signed,
    559        ppc_elf_unhandled_reloc),
    560 
    561   /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
    562      small data items.	 */
    563   HOW (R_PPC_EMB_SDA2REL, 1, 16, 0xffff, 0, FALSE, signed,
    564        ppc_elf_unhandled_reloc),
    565 
    566   /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
    567      signed offset from the appropriate base, and filling in the register
    568      field with the appropriate register (0, 2, or 13).  */
    569   HOW (R_PPC_EMB_SDA21, 2, 16, 0xffff, 0, FALSE, signed,
    570        ppc_elf_unhandled_reloc),
    571 
    572   /* Relocation not handled: R_PPC_EMB_MRKREF */
    573   /* Relocation not handled: R_PPC_EMB_RELSEC16 */
    574   /* Relocation not handled: R_PPC_EMB_RELST_LO */
    575   /* Relocation not handled: R_PPC_EMB_RELST_HI */
    576   /* Relocation not handled: R_PPC_EMB_RELST_HA */
    577   /* Relocation not handled: R_PPC_EMB_BIT_FLD */
    578 
    579   /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
    580      in the 16 bit signed offset from the appropriate base, and filling in the
    581      register field with the appropriate register (0, 2, or 13).  */
    582   HOW (R_PPC_EMB_RELSDA, 1, 16, 0xffff, 0, FALSE, signed,
    583        ppc_elf_unhandled_reloc),
    584 
    585   /* A relative 8 bit branch.  */
    586   HOW (R_PPC_VLE_REL8, 1, 8, 0xff, 1, TRUE, signed,
    587        bfd_elf_generic_reloc),
    588 
    589   /* A relative 15 bit branch.  */
    590   HOW (R_PPC_VLE_REL15, 2, 16, 0xfffe, 0, TRUE, signed,
    591        bfd_elf_generic_reloc),
    592 
    593   /* A relative 24 bit branch.  */
    594   HOW (R_PPC_VLE_REL24, 2, 25, 0x1fffffe, 0, TRUE, signed,
    595        bfd_elf_generic_reloc),
    596 
    597   /* The 16 LSBS in split16a format.  */
    598   HOW (R_PPC_VLE_LO16A, 2, 16, 0x1f07ff, 0, FALSE, dont,
    599        ppc_elf_unhandled_reloc),
    600 
    601   /* The 16 LSBS in split16d format.  */
    602   HOW (R_PPC_VLE_LO16D, 2, 16, 0x3e007ff, 0, FALSE, dont,
    603        ppc_elf_unhandled_reloc),
    604 
    605   /* Bits 16-31 split16a format.  */
    606   HOW (R_PPC_VLE_HI16A, 2, 16, 0x1f07ff, 16, FALSE, dont,
    607        ppc_elf_unhandled_reloc),
    608 
    609   /* Bits 16-31 split16d format.  */
    610   HOW (R_PPC_VLE_HI16D, 2, 16, 0x3e007ff, 16, FALSE, dont,
    611        ppc_elf_unhandled_reloc),
    612 
    613   /* Bits 16-31 (High Adjusted) in split16a format.  */
    614   HOW (R_PPC_VLE_HA16A, 2, 16, 0x1f07ff, 16, FALSE, dont,
    615        ppc_elf_unhandled_reloc),
    616 
    617   /* Bits 16-31 (High Adjusted) in split16d format.  */
    618   HOW (R_PPC_VLE_HA16D, 2, 16, 0x3e007ff, 16, FALSE, dont,
    619        ppc_elf_unhandled_reloc),
    620 
    621   /* This reloc is like R_PPC_EMB_SDA21 but only applies to e_add16i
    622      instructions.  If the register base is 0 then the linker changes
    623      the e_add16i to an e_li instruction.  */
    624   HOW (R_PPC_VLE_SDA21, 2, 16, 0xffff, 0, FALSE, signed,
    625        ppc_elf_unhandled_reloc),
    626 
    627   /* Like R_PPC_VLE_SDA21 but ignore overflow.  */
    628   HOW (R_PPC_VLE_SDA21_LO, 2, 16, 0xffff, 0, FALSE, dont,
    629        ppc_elf_unhandled_reloc),
    630 
    631   /* The 16 LSBS relative to _SDA_BASE_ in split16a format.  */
    632   HOW (R_PPC_VLE_SDAREL_LO16A, 2, 16, 0x1f07ff, 0, FALSE, dont,
    633        ppc_elf_unhandled_reloc),
    634 
    635   /* The 16 LSBS relative to _SDA_BASE_ in split16d format.  */
    636   HOW (R_PPC_VLE_SDAREL_LO16D, 2, 16, 0x3e007ff, 0, FALSE, dont,
    637        ppc_elf_unhandled_reloc),
    638 
    639   /* Bits 16-31 relative to _SDA_BASE_ in split16a format.  */
    640   HOW (R_PPC_VLE_SDAREL_HI16A, 2, 16, 0x1f07ff, 16, FALSE, dont,
    641        ppc_elf_unhandled_reloc),
    642 
    643   /* Bits 16-31 relative to _SDA_BASE_ in split16d format.  */
    644   HOW (R_PPC_VLE_SDAREL_HI16D, 2, 16, 0x3e007ff, 16, FALSE, dont,
    645        ppc_elf_unhandled_reloc),
    646 
    647   /* Bits 16-31 (HA) relative to _SDA_BASE split16a format.  */
    648   HOW (R_PPC_VLE_SDAREL_HA16A, 2, 16, 0x1f07ff, 16, FALSE, dont,
    649        ppc_elf_unhandled_reloc),
    650 
    651   /* Bits 16-31 (HA) relative to _SDA_BASE split16d format.  */
    652   HOW (R_PPC_VLE_SDAREL_HA16D, 2, 16, 0x3e007ff, 16, FALSE, dont,
    653        ppc_elf_unhandled_reloc),
    654 
    655   /* e_li split20 format.  */
    656   HOW (R_PPC_VLE_ADDR20, 2, 20, 0x1f7fff, 0, FALSE, dont,
    657        ppc_elf_unhandled_reloc),
    658 
    659   HOW (R_PPC_IRELATIVE, 2, 32, 0xffffffff, 0, FALSE, dont,
    660        ppc_elf_unhandled_reloc),
    661 
    662   /* A 16 bit relative relocation.  */
    663   HOW (R_PPC_REL16, 1, 16, 0xffff, 0, TRUE, signed,
    664        bfd_elf_generic_reloc),
    665 
    666   /* A 16 bit relative relocation without overflow.  */
    667   HOW (R_PPC_REL16_LO, 1, 16, 0xffff, 0, TRUE, dont,
    668        bfd_elf_generic_reloc),
    669 
    670   /* The high order 16 bits of a relative address.  */
    671   HOW (R_PPC_REL16_HI, 1, 16, 0xffff, 16, TRUE, dont,
    672        bfd_elf_generic_reloc),
    673 
    674   /* The high order 16 bits of a relative address, plus 1 if the contents of
    675      the low 16 bits, treated as a signed number, is negative.  */
    676   HOW (R_PPC_REL16_HA, 1, 16, 0xffff, 16, TRUE, dont,
    677        ppc_elf_addr16_ha_reloc),
    678 
    679   /* Like R_PPC_REL16_HA but for split field in addpcis.  */
    680   HOW (R_PPC_REL16DX_HA, 2, 16, 0x1fffc1, 16, TRUE, signed,
    681        ppc_elf_addr16_ha_reloc),
    682 
    683   /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
    684   HOW (R_PPC_16DX_HA, 2, 16, 0x1fffc1, 16, FALSE, signed,
    685        ppc_elf_addr16_ha_reloc),
    686 
    687   /* GNU extension to record C++ vtable hierarchy.  */
    688   HOW (R_PPC_GNU_VTINHERIT, 0, 0, 0, 0, FALSE, dont,
    689        NULL),
    690 
    691   /* GNU extension to record C++ vtable member usage.  */
    692   HOW (R_PPC_GNU_VTENTRY, 0, 0, 0, 0, FALSE, dont,
    693        NULL),
    694 
    695   /* Phony reloc to handle AIX style TOC entries.  */
    696   HOW (R_PPC_TOC16, 1, 16, 0xffff, 0, FALSE, signed,
    697        ppc_elf_unhandled_reloc),
    698 };
    699 
    700 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
    702 
    703 static void
    704 ppc_elf_howto_init (void)
    705 {
    706   unsigned int i, type;
    707 
    708   for (i = 0;
    709        i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
    710        i++)
    711     {
    712       type = ppc_elf_howto_raw[i].type;
    713       if (type >= (sizeof (ppc_elf_howto_table)
    714 		   / sizeof (ppc_elf_howto_table[0])))
    715 	abort ();
    716       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
    717     }
    718 }
    719 
    720 static reloc_howto_type *
    721 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    722 			   bfd_reloc_code_real_type code)
    723 {
    724   enum elf_ppc_reloc_type r;
    725 
    726   /* Initialize howto table if not already done.  */
    727   if (!ppc_elf_howto_table[R_PPC_ADDR32])
    728     ppc_elf_howto_init ();
    729 
    730   switch (code)
    731     {
    732     default:
    733       return NULL;
    734 
    735     case BFD_RELOC_NONE:		r = R_PPC_NONE;			break;
    736     case BFD_RELOC_32:			r = R_PPC_ADDR32;		break;
    737     case BFD_RELOC_PPC_BA26:		r = R_PPC_ADDR24;		break;
    738     case BFD_RELOC_PPC64_ADDR16_DS:
    739     case BFD_RELOC_16:			r = R_PPC_ADDR16;		break;
    740     case BFD_RELOC_PPC64_ADDR16_LO_DS:
    741     case BFD_RELOC_LO16:		r = R_PPC_ADDR16_LO;		break;
    742     case BFD_RELOC_HI16:		r = R_PPC_ADDR16_HI;		break;
    743     case BFD_RELOC_HI16_S:		r = R_PPC_ADDR16_HA;		break;
    744     case BFD_RELOC_PPC_BA16:		r = R_PPC_ADDR14;		break;
    745     case BFD_RELOC_PPC_BA16_BRTAKEN:	r = R_PPC_ADDR14_BRTAKEN;	break;
    746     case BFD_RELOC_PPC_BA16_BRNTAKEN:	r = R_PPC_ADDR14_BRNTAKEN;	break;
    747     case BFD_RELOC_PPC_B26:		r = R_PPC_REL24;		break;
    748     case BFD_RELOC_PPC_B16:		r = R_PPC_REL14;		break;
    749     case BFD_RELOC_PPC_B16_BRTAKEN:	r = R_PPC_REL14_BRTAKEN;	break;
    750     case BFD_RELOC_PPC_B16_BRNTAKEN:	r = R_PPC_REL14_BRNTAKEN;	break;
    751     case BFD_RELOC_PPC64_GOT16_DS:
    752     case BFD_RELOC_16_GOTOFF:		r = R_PPC_GOT16;		break;
    753     case BFD_RELOC_PPC64_GOT16_LO_DS:
    754     case BFD_RELOC_LO16_GOTOFF:		r = R_PPC_GOT16_LO;		break;
    755     case BFD_RELOC_HI16_GOTOFF:		r = R_PPC_GOT16_HI;		break;
    756     case BFD_RELOC_HI16_S_GOTOFF:	r = R_PPC_GOT16_HA;		break;
    757     case BFD_RELOC_24_PLT_PCREL:	r = R_PPC_PLTREL24;		break;
    758     case BFD_RELOC_PPC_COPY:		r = R_PPC_COPY;			break;
    759     case BFD_RELOC_PPC_GLOB_DAT:	r = R_PPC_GLOB_DAT;		break;
    760     case BFD_RELOC_PPC_LOCAL24PC:	r = R_PPC_LOCAL24PC;		break;
    761     case BFD_RELOC_32_PCREL:		r = R_PPC_REL32;		break;
    762     case BFD_RELOC_32_PLTOFF:		r = R_PPC_PLT32;		break;
    763     case BFD_RELOC_32_PLT_PCREL:	r = R_PPC_PLTREL32;		break;
    764     case BFD_RELOC_PPC64_PLT16_LO_DS:
    765     case BFD_RELOC_LO16_PLTOFF:		r = R_PPC_PLT16_LO;		break;
    766     case BFD_RELOC_HI16_PLTOFF:		r = R_PPC_PLT16_HI;		break;
    767     case BFD_RELOC_HI16_S_PLTOFF:	r = R_PPC_PLT16_HA;		break;
    768     case BFD_RELOC_GPREL16:		r = R_PPC_SDAREL16;		break;
    769     case BFD_RELOC_PPC64_SECTOFF_DS:
    770     case BFD_RELOC_16_BASEREL:		r = R_PPC_SECTOFF;		break;
    771     case BFD_RELOC_PPC64_SECTOFF_LO_DS:
    772     case BFD_RELOC_LO16_BASEREL:	r = R_PPC_SECTOFF_LO;		break;
    773     case BFD_RELOC_HI16_BASEREL:	r = R_PPC_SECTOFF_HI;		break;
    774     case BFD_RELOC_HI16_S_BASEREL:	r = R_PPC_SECTOFF_HA;		break;
    775     case BFD_RELOC_CTOR:		r = R_PPC_ADDR32;		break;
    776     case BFD_RELOC_PPC64_TOC16_DS:
    777     case BFD_RELOC_PPC_TOC16:		r = R_PPC_TOC16;		break;
    778     case BFD_RELOC_PPC_TLS:		r = R_PPC_TLS;			break;
    779     case BFD_RELOC_PPC_TLSGD:		r = R_PPC_TLSGD;		break;
    780     case BFD_RELOC_PPC_TLSLD:		r = R_PPC_TLSLD;		break;
    781     case BFD_RELOC_PPC_DTPMOD:		r = R_PPC_DTPMOD32;		break;
    782     case BFD_RELOC_PPC64_TPREL16_DS:
    783     case BFD_RELOC_PPC_TPREL16:		r = R_PPC_TPREL16;		break;
    784     case BFD_RELOC_PPC64_TPREL16_LO_DS:
    785     case BFD_RELOC_PPC_TPREL16_LO:	r = R_PPC_TPREL16_LO;		break;
    786     case BFD_RELOC_PPC_TPREL16_HI:	r = R_PPC_TPREL16_HI;		break;
    787     case BFD_RELOC_PPC_TPREL16_HA:	r = R_PPC_TPREL16_HA;		break;
    788     case BFD_RELOC_PPC_TPREL:		r = R_PPC_TPREL32;		break;
    789     case BFD_RELOC_PPC64_DTPREL16_DS:
    790     case BFD_RELOC_PPC_DTPREL16:	r = R_PPC_DTPREL16;		break;
    791     case BFD_RELOC_PPC64_DTPREL16_LO_DS:
    792     case BFD_RELOC_PPC_DTPREL16_LO:	r = R_PPC_DTPREL16_LO;		break;
    793     case BFD_RELOC_PPC_DTPREL16_HI:	r = R_PPC_DTPREL16_HI;		break;
    794     case BFD_RELOC_PPC_DTPREL16_HA:	r = R_PPC_DTPREL16_HA;		break;
    795     case BFD_RELOC_PPC_DTPREL:		r = R_PPC_DTPREL32;		break;
    796     case BFD_RELOC_PPC_GOT_TLSGD16:	r = R_PPC_GOT_TLSGD16;		break;
    797     case BFD_RELOC_PPC_GOT_TLSGD16_LO:	r = R_PPC_GOT_TLSGD16_LO;	break;
    798     case BFD_RELOC_PPC_GOT_TLSGD16_HI:	r = R_PPC_GOT_TLSGD16_HI;	break;
    799     case BFD_RELOC_PPC_GOT_TLSGD16_HA:	r = R_PPC_GOT_TLSGD16_HA;	break;
    800     case BFD_RELOC_PPC_GOT_TLSLD16:	r = R_PPC_GOT_TLSLD16;		break;
    801     case BFD_RELOC_PPC_GOT_TLSLD16_LO:	r = R_PPC_GOT_TLSLD16_LO;	break;
    802     case BFD_RELOC_PPC_GOT_TLSLD16_HI:	r = R_PPC_GOT_TLSLD16_HI;	break;
    803     case BFD_RELOC_PPC_GOT_TLSLD16_HA:	r = R_PPC_GOT_TLSLD16_HA;	break;
    804     case BFD_RELOC_PPC_GOT_TPREL16:	r = R_PPC_GOT_TPREL16;		break;
    805     case BFD_RELOC_PPC_GOT_TPREL16_LO:	r = R_PPC_GOT_TPREL16_LO;	break;
    806     case BFD_RELOC_PPC_GOT_TPREL16_HI:	r = R_PPC_GOT_TPREL16_HI;	break;
    807     case BFD_RELOC_PPC_GOT_TPREL16_HA:	r = R_PPC_GOT_TPREL16_HA;	break;
    808     case BFD_RELOC_PPC_GOT_DTPREL16:	r = R_PPC_GOT_DTPREL16;		break;
    809     case BFD_RELOC_PPC_GOT_DTPREL16_LO:	r = R_PPC_GOT_DTPREL16_LO;	break;
    810     case BFD_RELOC_PPC_GOT_DTPREL16_HI:	r = R_PPC_GOT_DTPREL16_HI;	break;
    811     case BFD_RELOC_PPC_GOT_DTPREL16_HA:	r = R_PPC_GOT_DTPREL16_HA;	break;
    812     case BFD_RELOC_PPC_EMB_NADDR32:	r = R_PPC_EMB_NADDR32;		break;
    813     case BFD_RELOC_PPC_EMB_NADDR16:	r = R_PPC_EMB_NADDR16;		break;
    814     case BFD_RELOC_PPC_EMB_NADDR16_LO:	r = R_PPC_EMB_NADDR16_LO;	break;
    815     case BFD_RELOC_PPC_EMB_NADDR16_HI:	r = R_PPC_EMB_NADDR16_HI;	break;
    816     case BFD_RELOC_PPC_EMB_NADDR16_HA:	r = R_PPC_EMB_NADDR16_HA;	break;
    817     case BFD_RELOC_PPC_EMB_SDAI16:	r = R_PPC_EMB_SDAI16;		break;
    818     case BFD_RELOC_PPC_EMB_SDA2I16:	r = R_PPC_EMB_SDA2I16;		break;
    819     case BFD_RELOC_PPC_EMB_SDA2REL:	r = R_PPC_EMB_SDA2REL;		break;
    820     case BFD_RELOC_PPC_EMB_SDA21:	r = R_PPC_EMB_SDA21;		break;
    821     case BFD_RELOC_PPC_EMB_MRKREF:	r = R_PPC_EMB_MRKREF;		break;
    822     case BFD_RELOC_PPC_EMB_RELSEC16:	r = R_PPC_EMB_RELSEC16;		break;
    823     case BFD_RELOC_PPC_EMB_RELST_LO:	r = R_PPC_EMB_RELST_LO;		break;
    824     case BFD_RELOC_PPC_EMB_RELST_HI:	r = R_PPC_EMB_RELST_HI;		break;
    825     case BFD_RELOC_PPC_EMB_RELST_HA:	r = R_PPC_EMB_RELST_HA;		break;
    826     case BFD_RELOC_PPC_EMB_BIT_FLD:	r = R_PPC_EMB_BIT_FLD;		break;
    827     case BFD_RELOC_PPC_EMB_RELSDA:	r = R_PPC_EMB_RELSDA;		break;
    828     case BFD_RELOC_PPC_VLE_REL8:	r = R_PPC_VLE_REL8;		break;
    829     case BFD_RELOC_PPC_VLE_REL15:	r = R_PPC_VLE_REL15;		break;
    830     case BFD_RELOC_PPC_VLE_REL24:	r = R_PPC_VLE_REL24;		break;
    831     case BFD_RELOC_PPC_VLE_LO16A:	r = R_PPC_VLE_LO16A;		break;
    832     case BFD_RELOC_PPC_VLE_LO16D:	r = R_PPC_VLE_LO16D;		break;
    833     case BFD_RELOC_PPC_VLE_HI16A:	r = R_PPC_VLE_HI16A;		break;
    834     case BFD_RELOC_PPC_VLE_HI16D:	r = R_PPC_VLE_HI16D;		break;
    835     case BFD_RELOC_PPC_VLE_HA16A:	r = R_PPC_VLE_HA16A;		break;
    836     case BFD_RELOC_PPC_VLE_HA16D:	r = R_PPC_VLE_HA16D;		break;
    837     case BFD_RELOC_PPC_VLE_SDA21:	r = R_PPC_VLE_SDA21;		break;
    838     case BFD_RELOC_PPC_VLE_SDA21_LO:	r = R_PPC_VLE_SDA21_LO;		break;
    839     case BFD_RELOC_PPC_VLE_SDAREL_LO16A:
    840       r = R_PPC_VLE_SDAREL_LO16A;
    841       break;
    842     case BFD_RELOC_PPC_VLE_SDAREL_LO16D:
    843       r = R_PPC_VLE_SDAREL_LO16D;
    844       break;
    845     case BFD_RELOC_PPC_VLE_SDAREL_HI16A:
    846       r = R_PPC_VLE_SDAREL_HI16A;
    847       break;
    848     case BFD_RELOC_PPC_VLE_SDAREL_HI16D:
    849       r = R_PPC_VLE_SDAREL_HI16D;
    850       break;
    851     case BFD_RELOC_PPC_VLE_SDAREL_HA16A:
    852       r = R_PPC_VLE_SDAREL_HA16A;
    853       break;
    854     case BFD_RELOC_PPC_VLE_SDAREL_HA16D:
    855       r = R_PPC_VLE_SDAREL_HA16D;
    856       break;
    857     case BFD_RELOC_16_PCREL:		r = R_PPC_REL16;		break;
    858     case BFD_RELOC_LO16_PCREL:		r = R_PPC_REL16_LO;		break;
    859     case BFD_RELOC_HI16_PCREL:		r = R_PPC_REL16_HI;		break;
    860     case BFD_RELOC_HI16_S_PCREL:	r = R_PPC_REL16_HA;		break;
    861     case BFD_RELOC_PPC_16DX_HA:		r = R_PPC_16DX_HA;		break;
    862     case BFD_RELOC_PPC_REL16DX_HA:	r = R_PPC_REL16DX_HA;		break;
    863     case BFD_RELOC_VTABLE_INHERIT:	r = R_PPC_GNU_VTINHERIT;	break;
    864     case BFD_RELOC_VTABLE_ENTRY:	r = R_PPC_GNU_VTENTRY;		break;
    865     }
    866 
    867   return ppc_elf_howto_table[r];
    868 };
    869 
    870 static reloc_howto_type *
    871 ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
    872 			   const char *r_name)
    873 {
    874   unsigned int i;
    875 
    876   for (i = 0;
    877        i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
    878        i++)
    879     if (ppc_elf_howto_raw[i].name != NULL
    880 	&& strcasecmp (ppc_elf_howto_raw[i].name, r_name) == 0)
    881       return &ppc_elf_howto_raw[i];
    882 
    883   return NULL;
    884 }
    885 
    886 /* Set the howto pointer for a PowerPC ELF reloc.  */
    887 
    888 static bfd_boolean
    889 ppc_elf_info_to_howto (bfd *abfd,
    890 		       arelent *cache_ptr,
    891 		       Elf_Internal_Rela *dst)
    892 {
    893   unsigned int r_type;
    894 
    895   /* Initialize howto table if not already done.  */
    896   if (!ppc_elf_howto_table[R_PPC_ADDR32])
    897     ppc_elf_howto_init ();
    898 
    899   r_type = ELF32_R_TYPE (dst->r_info);
    900   if (r_type >= R_PPC_max)
    901     {
    902       /* xgettext:c-format */
    903       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
    904 			  abfd, r_type);
    905       bfd_set_error (bfd_error_bad_value);
    906       return FALSE;
    907     }
    908 
    909   cache_ptr->howto = ppc_elf_howto_table[r_type];
    910 
    911   /* Just because the above assert didn't trigger doesn't mean that
    912      ELF32_R_TYPE (dst->r_info) is necessarily a valid relocation.  */
    913   if (cache_ptr->howto == NULL)
    914     {
    915       /* xgettext:c-format */
    916       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
    917 			  abfd, r_type);
    918       bfd_set_error (bfd_error_bad_value);
    919 
    920       return FALSE;
    921     }
    922 
    923   return TRUE;
    924 }
    925 
    926 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs.  */
    927 
    928 static bfd_reloc_status_type
    929 ppc_elf_addr16_ha_reloc (bfd *abfd,
    930 			 arelent *reloc_entry,
    931 			 asymbol *symbol,
    932 			 void *data,
    933 			 asection *input_section,
    934 			 bfd *output_bfd,
    935 			 char **error_message ATTRIBUTE_UNUSED)
    936 {
    937   enum elf_ppc_reloc_type r_type;
    938   long insn;
    939   bfd_size_type octets;
    940   bfd_vma value;
    941 
    942   if (output_bfd != NULL)
    943     {
    944       reloc_entry->address += input_section->output_offset;
    945       return bfd_reloc_ok;
    946     }
    947 
    948   reloc_entry->addend += 0x8000;
    949   r_type = reloc_entry->howto->type;
    950   if (r_type != R_PPC_REL16DX_HA)
    951     return bfd_reloc_continue;
    952 
    953   value = 0;
    954   if (!bfd_is_com_section (symbol->section))
    955     value = symbol->value;
    956   value += (reloc_entry->addend
    957 	    + symbol->section->output_offset
    958 	    + symbol->section->output_section->vma);
    959   value -= (reloc_entry->address
    960 	    + input_section->output_offset
    961 	    + input_section->output_section->vma);
    962   value >>= 16;
    963 
    964   octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
    965   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
    966   insn &= ~0x1fffc1;
    967   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
    968   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
    969   return bfd_reloc_ok;
    970 }
    971 
    972 static bfd_reloc_status_type
    973 ppc_elf_unhandled_reloc (bfd *abfd,
    974 			 arelent *reloc_entry,
    975 			 asymbol *symbol,
    976 			 void *data,
    977 			 asection *input_section,
    978 			 bfd *output_bfd,
    979 			 char **error_message)
    980 {
    981   /* If this is a relocatable link (output_bfd test tells us), just
    982      call the generic function.  Any adjustment will be done at final
    983      link time.  */
    984   if (output_bfd != NULL)
    985     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
    986 				  input_section, output_bfd, error_message);
    987 
    988   if (error_message != NULL)
    989     {
    990       static char buf[60];
    991       sprintf (buf, _("generic linker can't handle %s"),
    992 	       reloc_entry->howto->name);
    993       *error_message = buf;
    994     }
    995   return bfd_reloc_dangerous;
    996 }
    997 
    998 /* Sections created by the linker.  */
   1000 
   1001 typedef struct elf_linker_section
   1002 {
   1003   /* Pointer to the bfd section.  */
   1004   asection *section;
   1005   /* Section name.  */
   1006   const char *name;
   1007   /* Associated bss section name.  */
   1008   const char *bss_name;
   1009   /* Associated symbol name.  */
   1010   const char *sym_name;
   1011   /* Associated symbol.  */
   1012   struct elf_link_hash_entry *sym;
   1013 } elf_linker_section_t;
   1014 
   1015 /* Linked list of allocated pointer entries.  This hangs off of the
   1016    symbol lists, and provides allows us to return different pointers,
   1017    based on different addend's.  */
   1018 
   1019 typedef struct elf_linker_section_pointers
   1020 {
   1021   /* next allocated pointer for this symbol */
   1022   struct elf_linker_section_pointers *next;
   1023   /* offset of pointer from beginning of section */
   1024   bfd_vma offset;
   1025   /* addend used */
   1026   bfd_vma addend;
   1027   /* which linker section this is */
   1028   elf_linker_section_t *lsect;
   1029 } elf_linker_section_pointers_t;
   1030 
   1031 struct ppc_elf_obj_tdata
   1032 {
   1033   struct elf_obj_tdata elf;
   1034 
   1035   /* A mapping from local symbols to offsets into the various linker
   1036      sections added.  This is index by the symbol index.  */
   1037   elf_linker_section_pointers_t **linker_section_pointers;
   1038 
   1039   /* Flags used to auto-detect plt type.  */
   1040   unsigned int makes_plt_call : 1;
   1041   unsigned int has_rel16 : 1;
   1042 };
   1043 
   1044 #define ppc_elf_tdata(bfd) \
   1045   ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
   1046 
   1047 #define elf_local_ptr_offsets(bfd) \
   1048   (ppc_elf_tdata (bfd)->linker_section_pointers)
   1049 
   1050 #define is_ppc_elf(bfd) \
   1051   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   1052    && elf_object_id (bfd) == PPC32_ELF_DATA)
   1053 
   1054 /* Override the generic function because we store some extras.  */
   1055 
   1056 static bfd_boolean
   1057 ppc_elf_mkobject (bfd *abfd)
   1058 {
   1059   return bfd_elf_allocate_object (abfd, sizeof (struct ppc_elf_obj_tdata),
   1060 				  PPC32_ELF_DATA);
   1061 }
   1062 
   1063 /* When defaulting arch/mach, decode apuinfo to find a better match.  */
   1064 
   1065 bfd_boolean
   1066 _bfd_elf_ppc_set_arch (bfd *abfd)
   1067 {
   1068   unsigned long mach = 0;
   1069   asection *s;
   1070   unsigned char *contents;
   1071 
   1072   if (abfd->arch_info->bits_per_word == 32
   1073       && bfd_big_endian (abfd))
   1074     {
   1075 
   1076       for (s = abfd->sections; s != NULL; s = s->next)
   1077 	if ((elf_section_data (s)->this_hdr.sh_flags & SHF_PPC_VLE) != 0)
   1078 	  break;
   1079       if (s != NULL)
   1080 	mach = bfd_mach_ppc_vle;
   1081     }
   1082 
   1083   if (mach == 0)
   1084     {
   1085       s = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
   1086       if (s != NULL
   1087 	  && s->size >= 24
   1088 	  && bfd_malloc_and_get_section (abfd, s, &contents))
   1089 	{
   1090 	  unsigned int apuinfo_size = bfd_get_32 (abfd, contents + 4);
   1091 	  unsigned int i;
   1092 
   1093 	  for (i = 20; i < apuinfo_size + 20 && i + 4 <= s->size; i += 4)
   1094 	    {
   1095 	      unsigned int val = bfd_get_32 (abfd, contents + i);
   1096 	      switch (val >> 16)
   1097 		{
   1098 		case PPC_APUINFO_PMR:
   1099 		case PPC_APUINFO_RFMCI:
   1100 		  if (mach == 0)
   1101 		    mach = bfd_mach_ppc_titan;
   1102 		  break;
   1103 
   1104 		case PPC_APUINFO_ISEL:
   1105 		case PPC_APUINFO_CACHELCK:
   1106 		  if (mach == bfd_mach_ppc_titan)
   1107 		    mach = bfd_mach_ppc_e500mc;
   1108 		  break;
   1109 
   1110 		case PPC_APUINFO_SPE:
   1111 		case PPC_APUINFO_EFS:
   1112 		case PPC_APUINFO_BRLOCK:
   1113 		  if (mach != bfd_mach_ppc_vle)
   1114 		    mach = bfd_mach_ppc_e500;
   1115 		  break;
   1116 
   1117 		case PPC_APUINFO_VLE:
   1118 		  mach = bfd_mach_ppc_vle;
   1119 		  break;
   1120 
   1121 		default:
   1122 		  mach = -1ul;
   1123 		}
   1124 	    }
   1125 	  free (contents);
   1126 	}
   1127     }
   1128 
   1129   if (mach != 0 && mach != -1ul)
   1130     {
   1131       const bfd_arch_info_type *arch;
   1132 
   1133       for (arch = abfd->arch_info->next; arch; arch = arch->next)
   1134 	if (arch->mach == mach)
   1135 	  {
   1136 	    abfd->arch_info = arch;
   1137 	    break;
   1138 	  }
   1139     }
   1140   return TRUE;
   1141 }
   1142 
   1143 /* Fix bad default arch selected for a 32 bit input bfd when the
   1144    default is 64 bit.  Also select arch based on apuinfo.  */
   1145 
   1146 static bfd_boolean
   1147 ppc_elf_object_p (bfd *abfd)
   1148 {
   1149   if (!abfd->arch_info->the_default)
   1150     return TRUE;
   1151 
   1152   if (abfd->arch_info->bits_per_word == 64)
   1153     {
   1154       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   1155 
   1156       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
   1157 	{
   1158 	  /* Relies on arch after 64 bit default being 32 bit default.  */
   1159 	  abfd->arch_info = abfd->arch_info->next;
   1160 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
   1161 	}
   1162     }
   1163   return _bfd_elf_ppc_set_arch (abfd);
   1164 }
   1165 
   1166 /* Function to set whether a module needs the -mrelocatable bit set.  */
   1167 
   1168 static bfd_boolean
   1169 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
   1170 {
   1171   BFD_ASSERT (!elf_flags_init (abfd)
   1172 	      || elf_elfheader (abfd)->e_flags == flags);
   1173 
   1174   elf_elfheader (abfd)->e_flags = flags;
   1175   elf_flags_init (abfd) = TRUE;
   1176   return TRUE;
   1177 }
   1178 
   1179 /* Support for core dump NOTE sections.  */
   1180 
   1181 static bfd_boolean
   1182 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   1183 {
   1184   int offset;
   1185   unsigned int size;
   1186 
   1187   switch (note->descsz)
   1188     {
   1189     default:
   1190       return FALSE;
   1191 
   1192     case 268:		/* Linux/PPC.  */
   1193       /* pr_cursig */
   1194       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   1195 
   1196       /* pr_pid */
   1197       elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
   1198 
   1199       /* pr_reg */
   1200       offset = 72;
   1201       size = 192;
   1202 
   1203       break;
   1204     }
   1205 
   1206   /* Make a ".reg/999" section.  */
   1207   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   1208 					  size, note->descpos + offset);
   1209 }
   1210 
   1211 static bfd_boolean
   1212 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   1213 {
   1214   switch (note->descsz)
   1215     {
   1216     default:
   1217       return FALSE;
   1218 
   1219     case 128:		/* Linux/PPC elf_prpsinfo.  */
   1220       elf_tdata (abfd)->core->pid
   1221 	= bfd_get_32 (abfd, note->descdata + 16);
   1222       elf_tdata (abfd)->core->program
   1223 	= _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
   1224       elf_tdata (abfd)->core->command
   1225 	= _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
   1226     }
   1227 
   1228   /* Note that for some reason, a spurious space is tacked
   1229      onto the end of the args in some (at least one anyway)
   1230      implementations, so strip it off if it exists.  */
   1231 
   1232   {
   1233     char *command = elf_tdata (abfd)->core->command;
   1234     int n = strlen (command);
   1235 
   1236     if (0 < n && command[n - 1] == ' ')
   1237       command[n - 1] = '\0';
   1238   }
   1239 
   1240   return TRUE;
   1241 }
   1242 
   1243 static char *
   1244 ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
   1245 {
   1246   switch (note_type)
   1247     {
   1248     default:
   1249       return NULL;
   1250 
   1251     case NT_PRPSINFO:
   1252       {
   1253 	char data[128] ATTRIBUTE_NONSTRING;
   1254 	va_list ap;
   1255 
   1256 	va_start (ap, note_type);
   1257 	memset (data, 0, sizeof (data));
   1258 	strncpy (data + 32, va_arg (ap, const char *), 16);
   1259 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
   1260 	DIAGNOSTIC_PUSH;
   1261 	/* GCC 8.0 and 8.1 warn about 80 equals destination size with
   1262 	   -Wstringop-truncation:
   1263 	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
   1264 	 */
   1265 	DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
   1266 #endif
   1267 	strncpy (data + 48, va_arg (ap, const char *), 80);
   1268 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
   1269 	DIAGNOSTIC_POP;
   1270 #endif
   1271 	va_end (ap);
   1272 	return elfcore_write_note (abfd, buf, bufsiz,
   1273 				   "CORE", note_type, data, sizeof (data));
   1274       }
   1275 
   1276     case NT_PRSTATUS:
   1277       {
   1278 	char data[268];
   1279 	va_list ap;
   1280 	long pid;
   1281 	int cursig;
   1282 	const void *greg;
   1283 
   1284 	va_start (ap, note_type);
   1285 	memset (data, 0, 72);
   1286 	pid = va_arg (ap, long);
   1287 	bfd_put_32 (abfd, pid, data + 24);
   1288 	cursig = va_arg (ap, int);
   1289 	bfd_put_16 (abfd, cursig, data + 12);
   1290 	greg = va_arg (ap, const void *);
   1291 	memcpy (data + 72, greg, 192);
   1292 	memset (data + 264, 0, 4);
   1293 	va_end (ap);
   1294 	return elfcore_write_note (abfd, buf, bufsiz,
   1295 				   "CORE", note_type, data, sizeof (data));
   1296       }
   1297     }
   1298 }
   1299 
   1300 static flagword
   1301 ppc_elf_lookup_section_flags (char *flag_name)
   1302 {
   1303 
   1304   if (!strcmp (flag_name, "SHF_PPC_VLE"))
   1305     return SHF_PPC_VLE;
   1306 
   1307   return 0;
   1308 }
   1309 
   1310 /* Return address for Ith PLT stub in section PLT, for relocation REL
   1311    or (bfd_vma) -1 if it should not be included.  */
   1312 
   1313 static bfd_vma
   1314 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
   1315 		     const asection *plt ATTRIBUTE_UNUSED,
   1316 		     const arelent *rel)
   1317 {
   1318   return rel->address;
   1319 }
   1320 
   1321 /* Handle a PowerPC specific section when reading an object file.  This
   1322    is called when bfd_section_from_shdr finds a section with an unknown
   1323    type.  */
   1324 
   1325 static bfd_boolean
   1326 ppc_elf_section_from_shdr (bfd *abfd,
   1327 			   Elf_Internal_Shdr *hdr,
   1328 			   const char *name,
   1329 			   int shindex)
   1330 {
   1331   asection *newsect;
   1332   flagword flags;
   1333 
   1334   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
   1335     return FALSE;
   1336 
   1337   newsect = hdr->bfd_section;
   1338   flags = 0;
   1339   if (hdr->sh_flags & SHF_EXCLUDE)
   1340     flags |= SEC_EXCLUDE;
   1341 
   1342   if (hdr->sh_type == SHT_ORDERED)
   1343     flags |= SEC_SORT_ENTRIES;
   1344 
   1345   if (strncmp (name, ".PPC.EMB", 8) == 0)
   1346     name += 8;
   1347   if (strncmp (name, ".sbss", 5) == 0
   1348       || strncmp (name, ".sdata", 6) == 0)
   1349     flags |= SEC_SMALL_DATA;
   1350 
   1351   return (flags == 0
   1352 	  || bfd_set_section_flags (newsect, newsect->flags | flags));
   1353 }
   1354 
   1355 /* Set up any other section flags and such that may be necessary.  */
   1356 
   1357 static bfd_boolean
   1358 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
   1359 		       Elf_Internal_Shdr *shdr,
   1360 		       asection *asect)
   1361 {
   1362   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
   1363     shdr->sh_type = SHT_ORDERED;
   1364 
   1365   return TRUE;
   1366 }
   1367 
   1368 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
   1369    need to bump up the number of section headers.  */
   1370 
   1371 static int
   1372 ppc_elf_additional_program_headers (bfd *abfd,
   1373 				    struct bfd_link_info *info ATTRIBUTE_UNUSED)
   1374 {
   1375   asection *s;
   1376   int ret = 0;
   1377 
   1378   s = bfd_get_section_by_name (abfd, ".sbss2");
   1379   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
   1380     ++ret;
   1381 
   1382   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
   1383   if (s != NULL && (s->flags & SEC_ALLOC) != 0)
   1384     ++ret;
   1385 
   1386   return ret;
   1387 }
   1388 
   1389 /* Modify the segment map for VLE executables.  */
   1390 
   1391 bfd_boolean
   1392 ppc_elf_modify_segment_map (bfd *abfd,
   1393 			    struct bfd_link_info *info ATTRIBUTE_UNUSED)
   1394 {
   1395   struct elf_segment_map *m;
   1396 
   1397   /* At this point in the link, output sections have already been sorted by
   1398      LMA and assigned to segments.  All that is left to do is to ensure
   1399      there is no mixing of VLE & non-VLE sections in a text segment.
   1400      If we find that case, we split the segment.
   1401      We maintain the original output section order.  */
   1402 
   1403   for (m = elf_seg_map (abfd); m != NULL; m = m->next)
   1404     {
   1405       struct elf_segment_map *n;
   1406       size_t amt;
   1407       unsigned int j, k;
   1408       unsigned int p_flags;
   1409 
   1410       if (m->p_type != PT_LOAD || m->count == 0)
   1411 	continue;
   1412 
   1413       for (p_flags = PF_R, j = 0; j != m->count; ++j)
   1414 	{
   1415 	  if ((m->sections[j]->flags & SEC_READONLY) == 0)
   1416 	    p_flags |= PF_W;
   1417 	  if ((m->sections[j]->flags & SEC_CODE) != 0)
   1418 	    {
   1419 	      p_flags |= PF_X;
   1420 	      if ((elf_section_flags (m->sections[j]) & SHF_PPC_VLE) != 0)
   1421 		p_flags |= PF_PPC_VLE;
   1422 	      break;
   1423 	    }
   1424 	}
   1425       if (j != m->count)
   1426 	while (++j != m->count)
   1427 	  {
   1428 	    unsigned int p_flags1 = PF_R;
   1429 
   1430 	    if ((m->sections[j]->flags & SEC_READONLY) == 0)
   1431 	      p_flags1 |= PF_W;
   1432 	    if ((m->sections[j]->flags & SEC_CODE) != 0)
   1433 	      {
   1434 		p_flags1 |= PF_X;
   1435 		if ((elf_section_flags (m->sections[j]) & SHF_PPC_VLE) != 0)
   1436 		  p_flags1 |= PF_PPC_VLE;
   1437 		if (((p_flags1 ^ p_flags) & PF_PPC_VLE) != 0)
   1438 		  break;
   1439 	      }
   1440 	    p_flags |= p_flags1;
   1441 	  }
   1442       /* If we're splitting a segment which originally contained rw
   1443 	 sections then those sections might now only be in one of the
   1444 	 two parts.  So always set p_flags if splitting, even if we
   1445 	 are being called for objcopy with p_flags_valid set.  */
   1446       if (j != m->count || !m->p_flags_valid)
   1447 	{
   1448 	  m->p_flags_valid = 1;
   1449 	  m->p_flags = p_flags;
   1450 	}
   1451       if (j == m->count)
   1452 	continue;
   1453 
   1454       /* Sections 0..j-1 stay in this (current) segment,
   1455 	 the remainder are put in a new segment.
   1456 	 The scan resumes with the new segment.  */
   1457 
   1458       amt = sizeof (struct elf_segment_map);
   1459       amt += (m->count - j - 1) * sizeof (asection *);
   1460       n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
   1461       if (n == NULL)
   1462 	return FALSE;
   1463 
   1464       n->p_type = PT_LOAD;
   1465       n->count = m->count - j;
   1466       for (k = 0; k < n->count; ++k)
   1467 	n->sections[k] = m->sections[j + k];
   1468       m->count = j;
   1469       m->p_size_valid = 0;
   1470       n->next = m->next;
   1471       m->next = n;
   1472     }
   1473 
   1474   return TRUE;
   1475 }
   1476 
   1477 /* Add extra PPC sections -- Note, for now, make .sbss2 and
   1478    .PPC.EMB.sbss0 a normal section, and not a bss section so
   1479    that the linker doesn't crater when trying to make more than
   1480    2 sections.  */
   1481 
   1482 static const struct bfd_elf_special_section ppc_elf_special_sections[] =
   1483 {
   1484   { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
   1485   { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
   1486   { STRING_COMMA_LEN (".sbss2"), -2, SHT_PROGBITS, SHF_ALLOC },
   1487   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   1488   { STRING_COMMA_LEN (".sdata2"), -2, SHT_PROGBITS, SHF_ALLOC },
   1489   { STRING_COMMA_LEN (".tags"), 0, SHT_ORDERED, SHF_ALLOC },
   1490   { STRING_COMMA_LEN (APUINFO_SECTION_NAME), 0, SHT_NOTE, 0 },
   1491   { STRING_COMMA_LEN (".PPC.EMB.sbss0"), 0, SHT_PROGBITS, SHF_ALLOC },
   1492   { STRING_COMMA_LEN (".PPC.EMB.sdata0"), 0, SHT_PROGBITS, SHF_ALLOC },
   1493   { NULL, 0, 0, 0, 0 }
   1494 };
   1495 
   1496 /* This is what we want for new plt/got.  */
   1497 static struct bfd_elf_special_section ppc_alt_plt =
   1498   { STRING_COMMA_LEN (".plt"),		   0, SHT_PROGBITS, SHF_ALLOC };
   1499 
   1500 static const struct bfd_elf_special_section *
   1501 ppc_elf_get_sec_type_attr (bfd *abfd, asection *sec)
   1502 {
   1503   const struct bfd_elf_special_section *ssect;
   1504 
   1505   /* See if this is one of the special sections.  */
   1506   if (sec->name == NULL)
   1507     return NULL;
   1508 
   1509   ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
   1510 					sec->use_rela_p);
   1511   if (ssect != NULL)
   1512     {
   1513       if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
   1514 	ssect = &ppc_alt_plt;
   1515       return ssect;
   1516     }
   1517 
   1518   return _bfd_elf_get_sec_type_attr (abfd, sec);
   1519 }
   1520 
   1521 /* Very simple linked list structure for recording apuinfo values.  */
   1523 typedef struct apuinfo_list
   1524 {
   1525   struct apuinfo_list *next;
   1526   unsigned long value;
   1527 }
   1528 apuinfo_list;
   1529 
   1530 static apuinfo_list *head;
   1531 static bfd_boolean apuinfo_set;
   1532 
   1533 static void
   1534 apuinfo_list_init (void)
   1535 {
   1536   head = NULL;
   1537   apuinfo_set = FALSE;
   1538 }
   1539 
   1540 static void
   1541 apuinfo_list_add (unsigned long value)
   1542 {
   1543   apuinfo_list *entry = head;
   1544 
   1545   while (entry != NULL)
   1546     {
   1547       if (entry->value == value)
   1548 	return;
   1549       entry = entry->next;
   1550     }
   1551 
   1552   entry = bfd_malloc (sizeof (* entry));
   1553   if (entry == NULL)
   1554     return;
   1555 
   1556   entry->value = value;
   1557   entry->next  = head;
   1558   head = entry;
   1559 }
   1560 
   1561 static unsigned
   1562 apuinfo_list_length (void)
   1563 {
   1564   apuinfo_list *entry;
   1565   unsigned long count;
   1566 
   1567   for (entry = head, count = 0;
   1568        entry;
   1569        entry = entry->next)
   1570     ++ count;
   1571 
   1572   return count;
   1573 }
   1574 
   1575 static inline unsigned long
   1576 apuinfo_list_element (unsigned long number)
   1577 {
   1578   apuinfo_list * entry;
   1579 
   1580   for (entry = head;
   1581        entry && number --;
   1582        entry = entry->next)
   1583     ;
   1584 
   1585   return entry ? entry->value : 0;
   1586 }
   1587 
   1588 static void
   1589 apuinfo_list_finish (void)
   1590 {
   1591   apuinfo_list *entry;
   1592 
   1593   for (entry = head; entry;)
   1594     {
   1595       apuinfo_list *next = entry->next;
   1596       free (entry);
   1597       entry = next;
   1598     }
   1599 
   1600   head = NULL;
   1601 }
   1602 
   1603 /* Scan the input BFDs and create a linked list of
   1604    the APUinfo values that will need to be emitted.  */
   1605 
   1606 static void
   1607 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
   1608 {
   1609   bfd *ibfd;
   1610   asection *asec;
   1611   char *buffer = NULL;
   1612   bfd_size_type largest_input_size = 0;
   1613   unsigned i;
   1614   unsigned long length;
   1615   const char *error_message = NULL;
   1616 
   1617   if (link_info == NULL)
   1618     return;
   1619 
   1620   apuinfo_list_init ();
   1621 
   1622   /* Read in the input sections contents.  */
   1623   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link.next)
   1624     {
   1625       unsigned long datum;
   1626 
   1627       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
   1628       if (asec == NULL)
   1629 	continue;
   1630 
   1631       /* xgettext:c-format */
   1632       error_message = _("corrupt %s section in %pB");
   1633       length = asec->size;
   1634       if (length < 20)
   1635 	goto fail;
   1636 
   1637       apuinfo_set = TRUE;
   1638       if (largest_input_size < asec->size)
   1639 	{
   1640 	  free (buffer);
   1641 	  largest_input_size = asec->size;
   1642 	  buffer = bfd_malloc (largest_input_size);
   1643 	  if (!buffer)
   1644 	    return;
   1645 	}
   1646 
   1647       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
   1648 	  || (bfd_bread (buffer, length, ibfd) != length))
   1649 	{
   1650 	  /* xgettext:c-format */
   1651 	  error_message = _("unable to read in %s section from %pB");
   1652 	  goto fail;
   1653 	}
   1654 
   1655       /* Verify the contents of the header.  Note - we have to
   1656 	 extract the values this way in order to allow for a
   1657 	 host whose endian-ness is different from the target.  */
   1658       datum = bfd_get_32 (ibfd, buffer);
   1659       if (datum != sizeof APUINFO_LABEL)
   1660 	goto fail;
   1661 
   1662       datum = bfd_get_32 (ibfd, buffer + 8);
   1663       if (datum != 0x2)
   1664 	goto fail;
   1665 
   1666       if (strcmp (buffer + 12, APUINFO_LABEL) != 0)
   1667 	goto fail;
   1668 
   1669       /* Get the number of bytes used for apuinfo entries.  */
   1670       datum = bfd_get_32 (ibfd, buffer + 4);
   1671       if (datum + 20 != length)
   1672 	goto fail;
   1673 
   1674       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
   1675       for (i = 0; i < datum; i += 4)
   1676 	apuinfo_list_add (bfd_get_32 (ibfd, buffer + 20 + i));
   1677     }
   1678 
   1679   error_message = NULL;
   1680 
   1681   if (apuinfo_set)
   1682     {
   1683       /* Compute the size of the output section.  */
   1684       unsigned num_entries = apuinfo_list_length ();
   1685 
   1686       /* Set the output section size, if it exists.  */
   1687       asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
   1688 
   1689       if (asec && !bfd_set_section_size (asec, 20 + num_entries * 4))
   1690 	{
   1691 	  ibfd = abfd;
   1692 	  /* xgettext:c-format */
   1693 	  error_message = _("warning: unable to set size of %s section in %pB");
   1694 	}
   1695     }
   1696 
   1697  fail:
   1698   free (buffer);
   1699 
   1700   if (error_message)
   1701     _bfd_error_handler (error_message, APUINFO_SECTION_NAME, ibfd);
   1702 }
   1703 
   1704 /* Prevent the output section from accumulating the input sections'
   1705    contents.  We have already stored this in our linked list structure.  */
   1706 
   1707 static bfd_boolean
   1708 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
   1709 		       struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
   1710 		       asection *asec,
   1711 		       bfd_byte *contents ATTRIBUTE_UNUSED)
   1712 {
   1713   return apuinfo_set && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
   1714 }
   1715 
   1716 /* Finally we can generate the output section.  */
   1717 
   1718 static void
   1719 ppc_final_write_processing (bfd *abfd)
   1720 {
   1721   bfd_byte *buffer;
   1722   asection *asec;
   1723   unsigned i;
   1724   unsigned num_entries;
   1725   bfd_size_type length;
   1726 
   1727   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
   1728   if (asec == NULL)
   1729     return;
   1730 
   1731   if (!apuinfo_set)
   1732     return;
   1733 
   1734   length = asec->size;
   1735   if (length < 20)
   1736     return;
   1737 
   1738   buffer = bfd_malloc (length);
   1739   if (buffer == NULL)
   1740     {
   1741       _bfd_error_handler
   1742 	(_("failed to allocate space for new APUinfo section"));
   1743       return;
   1744     }
   1745 
   1746   /* Create the apuinfo header.  */
   1747   num_entries = apuinfo_list_length ();
   1748   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
   1749   bfd_put_32 (abfd, num_entries * 4, buffer + 4);
   1750   bfd_put_32 (abfd, 0x2, buffer + 8);
   1751   strcpy ((char *) buffer + 12, APUINFO_LABEL);
   1752 
   1753   length = 20;
   1754   for (i = 0; i < num_entries; i++)
   1755     {
   1756       bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
   1757       length += 4;
   1758     }
   1759 
   1760   if (length != asec->size)
   1761     _bfd_error_handler (_("failed to compute new APUinfo section"));
   1762 
   1763   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
   1764     _bfd_error_handler (_("failed to install new APUinfo section"));
   1765 
   1766   free (buffer);
   1767 
   1768   apuinfo_list_finish ();
   1769 }
   1770 
   1771 static bfd_boolean
   1772 ppc_elf_final_write_processing (bfd *abfd)
   1773 {
   1774   ppc_final_write_processing (abfd);
   1775   return _bfd_elf_final_write_processing (abfd);
   1776 }
   1777 
   1778 static bfd_boolean
   1780 is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
   1781 {
   1782   bfd_byte buf[4 * 4];
   1783 
   1784   if (!bfd_get_section_contents (abfd, glink, buf, off, sizeof buf))
   1785     return FALSE;
   1786 
   1787   return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
   1788 	  && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
   1789 	  && bfd_get_32 (abfd, buf + 8) == MTCTR_11
   1790 	  && bfd_get_32 (abfd, buf + 12) == BCTR);
   1791 }
   1792 
   1793 static bfd_boolean
   1794 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
   1795 {
   1796   bfd_vma vma = *(bfd_vma *) ptr;
   1797   return ((section->flags & SEC_ALLOC) != 0
   1798 	  && section->vma <= vma
   1799 	  && vma < section->vma + section->size);
   1800 }
   1801 
   1802 static long
   1803 ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms,
   1804 			      long dynsymcount, asymbol **dynsyms,
   1805 			      asymbol **ret)
   1806 {
   1807   bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   1808   asection *plt, *relplt, *dynamic, *glink;
   1809   bfd_vma glink_vma = 0;
   1810   bfd_vma resolv_vma = 0;
   1811   bfd_vma stub_off;
   1812   asymbol *s;
   1813   arelent *p;
   1814   size_t count, i, stub_delta;
   1815   size_t size;
   1816   char *names;
   1817   bfd_byte buf[4];
   1818 
   1819   *ret = NULL;
   1820 
   1821   if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
   1822     return 0;
   1823 
   1824   if (dynsymcount <= 0)
   1825     return 0;
   1826 
   1827   relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   1828   if (relplt == NULL)
   1829     return 0;
   1830 
   1831   plt = bfd_get_section_by_name (abfd, ".plt");
   1832   if (plt == NULL)
   1833     return 0;
   1834 
   1835   /* Call common code to handle old-style executable PLTs.  */
   1836   if (elf_section_flags (plt) & SHF_EXECINSTR)
   1837     return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
   1838 					  dynsymcount, dynsyms, ret);
   1839 
   1840   /* If this object was prelinked, the prelinker stored the address
   1841      of .glink at got[1].  If it wasn't prelinked, got[1] will be zero.  */
   1842   dynamic = bfd_get_section_by_name (abfd, ".dynamic");
   1843   if (dynamic != NULL)
   1844     {
   1845       bfd_byte *dynbuf, *extdyn, *extdynend;
   1846       size_t extdynsize;
   1847       void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   1848 
   1849       if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
   1850 	return -1;
   1851 
   1852       extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   1853       swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   1854 
   1855       extdyn = dynbuf;
   1856       extdynend = extdyn + dynamic->size;
   1857       for (; extdyn < extdynend; extdyn += extdynsize)
   1858 	{
   1859 	  Elf_Internal_Dyn dyn;
   1860 	  (*swap_dyn_in) (abfd, extdyn, &dyn);
   1861 
   1862 	  if (dyn.d_tag == DT_NULL)
   1863 	    break;
   1864 
   1865 	  if (dyn.d_tag == DT_PPC_GOT)
   1866 	    {
   1867 	      unsigned int g_o_t = dyn.d_un.d_val;
   1868 	      asection *got = bfd_get_section_by_name (abfd, ".got");
   1869 	      if (got != NULL
   1870 		  && bfd_get_section_contents (abfd, got, buf,
   1871 					       g_o_t - got->vma + 4, 4))
   1872 		glink_vma = bfd_get_32 (abfd, buf);
   1873 	      break;
   1874 	    }
   1875 	}
   1876       free (dynbuf);
   1877     }
   1878 
   1879   /* Otherwise we read the first plt entry.  */
   1880   if (glink_vma == 0)
   1881     {
   1882       if (bfd_get_section_contents (abfd, plt, buf, 0, 4))
   1883 	glink_vma = bfd_get_32 (abfd, buf);
   1884     }
   1885 
   1886   if (glink_vma == 0)
   1887     return 0;
   1888 
   1889   /* The .glink section usually does not survive the final
   1890      link; search for the section (usually .text) where the
   1891      glink stubs now reside.  */
   1892   glink = bfd_sections_find_if (abfd, section_covers_vma, &glink_vma);
   1893   if (glink == NULL)
   1894     return 0;
   1895 
   1896   /* Determine glink PLT resolver by reading the relative branch
   1897      from the first glink stub.  */
   1898   if (bfd_get_section_contents (abfd, glink, buf,
   1899 				glink_vma - glink->vma, 4))
   1900     {
   1901       unsigned int insn = bfd_get_32 (abfd, buf);
   1902 
   1903       /* The first glink stub may either branch to the resolver ...  */
   1904       insn ^= B;
   1905       if ((insn & ~0x3fffffc) == 0)
   1906 	resolv_vma = glink_vma + (insn ^ 0x2000000) - 0x2000000;
   1907 
   1908       /* ... or fall through a bunch of NOPs.  */
   1909       else if ((insn ^ B ^ NOP) == 0)
   1910 	for (i = 4;
   1911 	     bfd_get_section_contents (abfd, glink, buf,
   1912 				       glink_vma - glink->vma + i, 4);
   1913 	     i += 4)
   1914 	  if (bfd_get_32 (abfd, buf) != NOP)
   1915 	    {
   1916 	      resolv_vma = glink_vma + i;
   1917 	      break;
   1918 	    }
   1919     }
   1920 
   1921   count = relplt->size / sizeof (Elf32_External_Rela);
   1922   /* If the stubs are those for -shared/-pie then we might have
   1923      multiple stubs for each plt entry.  If that is the case then
   1924      there is no way to associate stubs with their plt entries short
   1925      of figuring out the GOT pointer value used in the stub.
   1926      The offsets tested here need to cover all possible values of
   1927      GLINK_ENTRY_SIZE for other than __tls_get_addr_opt.  */
   1928   stub_off = glink_vma - glink->vma;
   1929   for (stub_delta = 16; stub_delta <= 32; stub_delta += 8)
   1930     if (is_nonpic_glink_stub (abfd, glink, stub_off - stub_delta))
   1931       break;
   1932   if (stub_delta > 32)
   1933     return 0;
   1934 
   1935   slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   1936   if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
   1937     return -1;
   1938 
   1939   size = count * sizeof (asymbol);
   1940   p = relplt->relocation;
   1941   for (i = 0; i < count; i++, p++)
   1942     {
   1943       size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   1944       if (p->addend != 0)
   1945 	size += sizeof ("+0x") - 1 + 8;
   1946     }
   1947 
   1948   size += sizeof (asymbol) + sizeof ("__glink");
   1949 
   1950   if (resolv_vma)
   1951     size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
   1952 
   1953   s = *ret = bfd_malloc (size);
   1954   if (s == NULL)
   1955     return -1;
   1956 
   1957   stub_off = glink_vma - glink->vma;
   1958   names = (char *) (s + count + 1 + (resolv_vma != 0));
   1959   p = relplt->relocation + count - 1;
   1960   for (i = 0; i < count; i++)
   1961     {
   1962       size_t len;
   1963 
   1964       stub_off -= stub_delta;
   1965       if (strcmp ((*p->sym_ptr_ptr)->name, "__tls_get_addr_opt") == 0)
   1966 	stub_off -= 32;
   1967       *s = **p->sym_ptr_ptr;
   1968       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   1969 	 we are defining a symbol, ensure one of them is set.  */
   1970       if ((s->flags & BSF_LOCAL) == 0)
   1971 	s->flags |= BSF_GLOBAL;
   1972       s->flags |= BSF_SYNTHETIC;
   1973       s->section = glink;
   1974       s->value = stub_off;
   1975       s->name = names;
   1976       s->udata.p = NULL;
   1977       len = strlen ((*p->sym_ptr_ptr)->name);
   1978       memcpy (names, (*p->sym_ptr_ptr)->name, len);
   1979       names += len;
   1980       if (p->addend != 0)
   1981 	{
   1982 	  memcpy (names, "+0x", sizeof ("+0x") - 1);
   1983 	  names += sizeof ("+0x") - 1;
   1984 	  bfd_sprintf_vma (abfd, names, p->addend);
   1985 	  names += strlen (names);
   1986 	}
   1987       memcpy (names, "@plt", sizeof ("@plt"));
   1988       names += sizeof ("@plt");
   1989       ++s;
   1990       --p;
   1991     }
   1992 
   1993   /* Add a symbol at the start of the glink branch table.  */
   1994   memset (s, 0, sizeof *s);
   1995   s->the_bfd = abfd;
   1996   s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   1997   s->section = glink;
   1998   s->value = glink_vma - glink->vma;
   1999   s->name = names;
   2000   memcpy (names, "__glink", sizeof ("__glink"));
   2001   names += sizeof ("__glink");
   2002   s++;
   2003   count++;
   2004 
   2005   if (resolv_vma)
   2006     {
   2007       /* Add a symbol for the glink PLT resolver.  */
   2008       memset (s, 0, sizeof *s);
   2009       s->the_bfd = abfd;
   2010       s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   2011       s->section = glink;
   2012       s->value = resolv_vma - glink->vma;
   2013       s->name = names;
   2014       memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
   2015       names += sizeof ("__glink_PLTresolve");
   2016       s++;
   2017       count++;
   2018     }
   2019 
   2020   return count;
   2021 }
   2022 
   2023 /* The following functions are specific to the ELF linker, while
   2025    functions above are used generally.  They appear in this file more
   2026    or less in the order in which they are called.  eg.
   2027    ppc_elf_check_relocs is called early in the link process,
   2028    ppc_elf_finish_dynamic_sections is one of the last functions
   2029    called.  */
   2030 
   2031 /* Track PLT entries needed for a given symbol.  We might need more
   2032    than one glink entry per symbol when generating a pic binary.  */
   2033 struct plt_entry
   2034 {
   2035   struct plt_entry *next;
   2036 
   2037   /* -fPIC uses multiple GOT sections, one per file, called ".got2".
   2038      This field stores the offset into .got2 used to initialise the
   2039      GOT pointer reg.  It will always be at least 32768.  (Current
   2040      gcc always uses an offset of 32768, but ld -r will pack .got2
   2041      sections together resulting in larger offsets).  */
   2042   bfd_vma addend;
   2043 
   2044   /* The .got2 section.  */
   2045   asection *sec;
   2046 
   2047   /* PLT refcount or offset.  */
   2048   union
   2049     {
   2050       bfd_signed_vma refcount;
   2051       bfd_vma offset;
   2052     } plt;
   2053 
   2054   /* .glink stub offset.  */
   2055   bfd_vma glink_offset;
   2056 };
   2057 
   2058 /* Of those relocs that might be copied as dynamic relocs, this
   2059    function selects those that must be copied when linking a shared
   2060    library or PIE, even when the symbol is local.  */
   2061 
   2062 static int
   2063 must_be_dyn_reloc (struct bfd_link_info *info,
   2064 		   enum elf_ppc_reloc_type r_type)
   2065 {
   2066   switch (r_type)
   2067     {
   2068     default:
   2069       /* Only relative relocs can be resolved when the object load
   2070 	 address isn't fixed.  DTPREL32 is excluded because the
   2071 	 dynamic linker needs to differentiate global dynamic from
   2072 	 local dynamic __tls_index pairs when PPC_OPT_TLS is set.  */
   2073       return 1;
   2074 
   2075     case R_PPC_REL24:
   2076     case R_PPC_REL14:
   2077     case R_PPC_REL14_BRTAKEN:
   2078     case R_PPC_REL14_BRNTAKEN:
   2079     case R_PPC_REL32:
   2080       return 0;
   2081 
   2082     case R_PPC_TPREL32:
   2083     case R_PPC_TPREL16:
   2084     case R_PPC_TPREL16_LO:
   2085     case R_PPC_TPREL16_HI:
   2086     case R_PPC_TPREL16_HA:
   2087       /* These relocations are relative but in a shared library the
   2088 	 linker doesn't know the thread pointer base.  */
   2089       return bfd_link_dll (info);
   2090     }
   2091 }
   2092 
   2093 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   2094    copying dynamic variables from a shared lib into an app's dynbss
   2095    section, and instead use a dynamic relocation to point into the
   2096    shared lib.  */
   2097 #define ELIMINATE_COPY_RELOCS 1
   2098 
   2099 /* Used to track dynamic relocations for local symbols.  */
   2100 struct ppc_dyn_relocs
   2101 {
   2102   struct ppc_dyn_relocs *next;
   2103 
   2104   /* The input section of the reloc.  */
   2105   asection *sec;
   2106 
   2107   /* Total number of relocs copied for the input section.  */
   2108   unsigned int count : 31;
   2109 
   2110   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
   2111   unsigned int ifunc : 1;
   2112 };
   2113 
   2114 /* PPC ELF linker hash entry.  */
   2115 
   2116 struct ppc_elf_link_hash_entry
   2117 {
   2118   struct elf_link_hash_entry elf;
   2119 
   2120   /* If this symbol is used in the linker created sections, the processor
   2121      specific backend uses this field to map the field into the offset
   2122      from the beginning of the section.  */
   2123   elf_linker_section_pointers_t *linker_section_pointer;
   2124 
   2125   /* Contexts in which symbol is used in the GOT.
   2126      Bits are or'd into the mask as the corresponding relocs are
   2127      encountered during check_relocs, with TLS_TLS being set when any
   2128      of the other TLS bits are set.  tls_optimize clears bits when
   2129      optimizing to indicate the corresponding GOT entry type is not
   2130      needed.  If set, TLS_TLS is never cleared.  tls_optimize may also
   2131      set TLS_GDIE when a GD reloc turns into an IE one.
   2132      These flags are also kept for local symbols.  */
   2133 #define TLS_TLS		 1	/* Any TLS reloc.  */
   2134 #define TLS_GD		 2	/* GD reloc. */
   2135 #define TLS_LD		 4	/* LD reloc. */
   2136 #define TLS_TPREL	 8	/* TPREL reloc, => IE. */
   2137 #define TLS_DTPREL	16	/* DTPREL reloc, => LD. */
   2138 #define TLS_MARK	32	/* __tls_get_addr call marked. */
   2139 #define TLS_GDIE	64	/* GOT TPREL reloc resulting from GD->IE. */
   2140   unsigned char tls_mask;
   2141 
   2142   /* The above field is also used to mark function symbols.  In which
   2143      case TLS_TLS will be 0.  */
   2144 #define PLT_IFUNC	 2	/* STT_GNU_IFUNC.  */
   2145 #define PLT_KEEP	 4	/* inline plt call requires plt entry.  */
   2146 #define NON_GOT        256	/* local symbol plt, not stored.  */
   2147 
   2148   /* Nonzero if we have seen a small data relocation referring to this
   2149      symbol.  */
   2150   unsigned char has_sda_refs : 1;
   2151 
   2152   /* Flag use of given relocations.  */
   2153   unsigned char has_addr16_ha : 1;
   2154   unsigned char has_addr16_lo : 1;
   2155 };
   2156 
   2157 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
   2158 
   2159 /* PPC ELF linker hash table.  */
   2160 
   2161 struct ppc_elf_link_hash_table
   2162 {
   2163   struct elf_link_hash_table elf;
   2164 
   2165   /* Various options passed from the linker.  */
   2166   struct ppc_elf_params *params;
   2167 
   2168   /* Short-cuts to get to dynamic linker sections.  */
   2169   asection *glink;
   2170   asection *dynsbss;
   2171   asection *relsbss;
   2172   elf_linker_section_t sdata[2];
   2173   asection *sbss;
   2174   asection *glink_eh_frame;
   2175   asection *pltlocal;
   2176   asection *relpltlocal;
   2177 
   2178   /* The (unloaded but important) .rela.plt.unloaded on VxWorks.  */
   2179   asection *srelplt2;
   2180 
   2181   /* Shortcut to __tls_get_addr.  */
   2182   struct elf_link_hash_entry *tls_get_addr;
   2183 
   2184   /* The bfd that forced an old-style PLT.  */
   2185   bfd *old_bfd;
   2186 
   2187   /* TLS local dynamic got entry handling.  */
   2188   union {
   2189     bfd_signed_vma refcount;
   2190     bfd_vma offset;
   2191   } tlsld_got;
   2192 
   2193   /* Offset of branch table to PltResolve function in glink.  */
   2194   bfd_vma glink_pltresolve;
   2195 
   2196   /* Size of reserved GOT entries.  */
   2197   unsigned int got_header_size;
   2198   /* Non-zero if allocating the header left a gap.  */
   2199   unsigned int got_gap;
   2200 
   2201   /* The type of PLT we have chosen to use.  */
   2202   enum ppc_elf_plt_type plt_type;
   2203 
   2204   /* Whether there exist local gnu indirect function resolvers,
   2205      referenced by dynamic relocations.  */
   2206   unsigned int local_ifunc_resolver:1;
   2207   unsigned int maybe_local_ifunc_resolver:1;
   2208 
   2209   /* Set if tls optimization is enabled.  */
   2210   unsigned int do_tls_opt:1;
   2211 
   2212   /* Set if inline plt calls should be converted to direct calls.  */
   2213   unsigned int can_convert_all_inline_plt:1;
   2214 
   2215   /* The size of PLT entries.  */
   2216   int plt_entry_size;
   2217   /* The distance between adjacent PLT slots.  */
   2218   int plt_slot_size;
   2219   /* The size of the first PLT entry.  */
   2220   int plt_initial_entry_size;
   2221 };
   2222 
   2223 /* Rename some of the generic section flags to better document how they
   2224    are used for ppc32.  The flags are only valid for ppc32 elf objects.  */
   2225 
   2226 /* Nonzero if this section has TLS related relocations.  */
   2227 #define has_tls_reloc sec_flg0
   2228 
   2229 /* Nonzero if this section has a call to __tls_get_addr lacking marker
   2230    relocs.  */
   2231 #define nomark_tls_get_addr sec_flg1
   2232 
   2233   /* Flag set when PLTCALL relocs are detected.  */
   2234 #define has_pltcall sec_flg2
   2235 
   2236 /* Get the PPC ELF linker hash table from a link_info structure.  */
   2237 
   2238 #define ppc_elf_hash_table(p) \
   2239   ((is_elf_hash_table ((p)->hash)					\
   2240     && elf_hash_table_id (elf_hash_table (p)) == PPC32_ELF_DATA)	\
   2241    ? (struct ppc_elf_link_hash_table *) (p)->hash : NULL)
   2242 
   2243 /* Create an entry in a PPC ELF linker hash table.  */
   2244 
   2245 static struct bfd_hash_entry *
   2246 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   2247 			   struct bfd_hash_table *table,
   2248 			   const char *string)
   2249 {
   2250   /* Allocate the structure if it has not already been allocated by a
   2251      subclass.  */
   2252   if (entry == NULL)
   2253     {
   2254       entry = bfd_hash_allocate (table,
   2255 				 sizeof (struct ppc_elf_link_hash_entry));
   2256       if (entry == NULL)
   2257 	return entry;
   2258     }
   2259 
   2260   /* Call the allocation method of the superclass.  */
   2261   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   2262   if (entry != NULL)
   2263     {
   2264       ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
   2265       ppc_elf_hash_entry (entry)->tls_mask = 0;
   2266       ppc_elf_hash_entry (entry)->has_sda_refs = 0;
   2267     }
   2268 
   2269   return entry;
   2270 }
   2271 
   2272 /* Create a PPC ELF linker hash table.  */
   2273 
   2274 static struct bfd_link_hash_table *
   2275 ppc_elf_link_hash_table_create (bfd *abfd)
   2276 {
   2277   struct ppc_elf_link_hash_table *ret;
   2278   static struct ppc_elf_params default_params
   2279     = { PLT_OLD, 0, 0, 1, 0, 0, 12, 0, 0, 0 };
   2280 
   2281   ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
   2282   if (ret == NULL)
   2283     return NULL;
   2284 
   2285   if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
   2286 				      ppc_elf_link_hash_newfunc,
   2287 				      sizeof (struct ppc_elf_link_hash_entry),
   2288 				      PPC32_ELF_DATA))
   2289     {
   2290       free (ret);
   2291       return NULL;
   2292     }
   2293 
   2294   ret->elf.init_plt_refcount.refcount = 0;
   2295   ret->elf.init_plt_refcount.glist = NULL;
   2296   ret->elf.init_plt_offset.offset = 0;
   2297   ret->elf.init_plt_offset.glist = NULL;
   2298 
   2299   ret->params = &default_params;
   2300 
   2301   ret->sdata[0].name = ".sdata";
   2302   ret->sdata[0].sym_name = "_SDA_BASE_";
   2303   ret->sdata[0].bss_name = ".sbss";
   2304 
   2305   ret->sdata[1].name = ".sdata2";
   2306   ret->sdata[1].sym_name = "_SDA2_BASE_";
   2307   ret->sdata[1].bss_name = ".sbss2";
   2308 
   2309   ret->plt_entry_size = 12;
   2310   ret->plt_slot_size = 8;
   2311   ret->plt_initial_entry_size = 72;
   2312 
   2313   return &ret->elf.root;
   2314 }
   2315 
   2316 /* Hook linker params into hash table.  */
   2317 
   2318 void
   2319 ppc_elf_link_params (struct bfd_link_info *info, struct ppc_elf_params *params)
   2320 {
   2321   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   2322 
   2323   if (htab)
   2324     htab->params = params;
   2325   params->pagesize_p2 = bfd_log2 (params->pagesize);
   2326 }
   2327 
   2328 /* Create .got and the related sections.  */
   2329 
   2330 static bfd_boolean
   2331 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
   2332 {
   2333   struct ppc_elf_link_hash_table *htab;
   2334 
   2335   if (!_bfd_elf_create_got_section (abfd, info))
   2336     return FALSE;
   2337 
   2338   htab = ppc_elf_hash_table (info);
   2339   if (htab->elf.target_os != is_vxworks)
   2340     {
   2341       /* The powerpc .got has a blrl instruction in it.  Mark it
   2342 	 executable.  */
   2343       flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
   2344 			| SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2345       if (!bfd_set_section_flags (htab->elf.sgot, flags))
   2346 	return FALSE;
   2347     }
   2348 
   2349   return TRUE;
   2350 }
   2351 
   2352 /* Create a special linker section, used for R_PPC_EMB_SDAI16 and
   2353    R_PPC_EMB_SDA2I16 pointers.  These sections become part of .sdata
   2354    and .sdata2.  Create _SDA_BASE_ and _SDA2_BASE too.  */
   2355 
   2356 static bfd_boolean
   2357 ppc_elf_create_linker_section (bfd *abfd,
   2358 			       struct bfd_link_info *info,
   2359 			       flagword flags,
   2360 			       elf_linker_section_t *lsect)
   2361 {
   2362   asection *s;
   2363 
   2364   flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   2365 	    | SEC_LINKER_CREATED);
   2366 
   2367   s = bfd_make_section_anyway_with_flags (abfd, lsect->name, flags);
   2368   if (s == NULL)
   2369     return FALSE;
   2370   lsect->section = s;
   2371 
   2372   /* Define the sym on the first section of this name.  */
   2373   s = bfd_get_section_by_name (abfd, lsect->name);
   2374 
   2375   lsect->sym = _bfd_elf_define_linkage_sym (abfd, info, s, lsect->sym_name);
   2376   if (lsect->sym == NULL)
   2377     return FALSE;
   2378   lsect->sym->root.u.def.value = 0x8000;
   2379   return TRUE;
   2380 }
   2381 
   2382 static bfd_boolean
   2383 ppc_elf_create_glink (bfd *abfd, struct bfd_link_info *info)
   2384 {
   2385   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   2386   asection *s;
   2387   flagword flags;
   2388   int p2align;
   2389 
   2390   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS
   2391 	   | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2392   s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags);
   2393   htab->glink = s;
   2394   p2align = htab->params->ppc476_workaround ? 6 : 4;
   2395   if (p2align < htab->params->plt_stub_align)
   2396     p2align = htab->params->plt_stub_align;
   2397   if (s == NULL
   2398       || !bfd_set_section_alignment (s, p2align))
   2399     return FALSE;
   2400 
   2401   if (!info->no_ld_generated_unwind_info)
   2402     {
   2403       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   2404 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2405       s = bfd_make_section_anyway_with_flags (abfd, ".eh_frame", flags);
   2406       htab->glink_eh_frame = s;
   2407       if (s == NULL
   2408 	  || !bfd_set_section_alignment (s, 2))
   2409 	return FALSE;
   2410     }
   2411 
   2412   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   2413   s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
   2414   htab->elf.iplt = s;
   2415   if (s == NULL
   2416       || !bfd_set_section_alignment (s, 4))
   2417     return FALSE;
   2418 
   2419   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   2420 	   | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2421   s = bfd_make_section_anyway_with_flags (abfd, ".rela.iplt", flags);
   2422   htab->elf.irelplt = s;
   2423   if (s == NULL
   2424       || ! bfd_set_section_alignment (s, 2))
   2425     return FALSE;
   2426 
   2427   /* Local plt entries.  */
   2428   flags = (SEC_ALLOC | SEC_LOAD
   2429 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2430   htab->pltlocal = bfd_make_section_anyway_with_flags (abfd, ".branch_lt",
   2431 						       flags);
   2432   if (htab->pltlocal == NULL
   2433       || !bfd_set_section_alignment (htab->pltlocal, 2))
   2434     return FALSE;
   2435 
   2436   if (bfd_link_pic (info))
   2437     {
   2438       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   2439 	       | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2440       htab->relpltlocal
   2441 	= bfd_make_section_anyway_with_flags (abfd, ".rela.branch_lt", flags);
   2442       if (htab->relpltlocal == NULL
   2443 	  || !bfd_set_section_alignment (htab->relpltlocal, 2))
   2444 	return FALSE;
   2445     }
   2446 
   2447   if (!ppc_elf_create_linker_section (abfd, info, 0,
   2448 				      &htab->sdata[0]))
   2449     return FALSE;
   2450 
   2451   if (!ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
   2452 				      &htab->sdata[1]))
   2453     return FALSE;
   2454 
   2455   return TRUE;
   2456 }
   2457 
   2458 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
   2459    to output sections (just like _bfd_elf_create_dynamic_sections has
   2460    to create .dynbss and .rela.bss).  */
   2461 
   2462 static bfd_boolean
   2463 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   2464 {
   2465   struct ppc_elf_link_hash_table *htab;
   2466   asection *s;
   2467   flagword flags;
   2468 
   2469   htab = ppc_elf_hash_table (info);
   2470 
   2471   if (htab->elf.sgot == NULL
   2472       && !ppc_elf_create_got (abfd, info))
   2473     return FALSE;
   2474 
   2475   if (!_bfd_elf_create_dynamic_sections (abfd, info))
   2476     return FALSE;
   2477 
   2478   if (htab->glink == NULL
   2479       && !ppc_elf_create_glink (abfd, info))
   2480     return FALSE;
   2481 
   2482   s = bfd_make_section_anyway_with_flags (abfd, ".dynsbss",
   2483 					  SEC_ALLOC | SEC_LINKER_CREATED);
   2484   htab->dynsbss = s;
   2485   if (s == NULL)
   2486     return FALSE;
   2487 
   2488   if (! bfd_link_pic (info))
   2489     {
   2490       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   2491 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   2492       s = bfd_make_section_anyway_with_flags (abfd, ".rela.sbss", flags);
   2493       htab->relsbss = s;
   2494       if (s == NULL
   2495 	  || !bfd_set_section_alignment (s, 2))
   2496 	return FALSE;
   2497     }
   2498 
   2499   if (htab->elf.target_os == is_vxworks
   2500       && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
   2501     return FALSE;
   2502 
   2503   s = htab->elf.splt;
   2504   flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
   2505   if (htab->plt_type == PLT_VXWORKS)
   2506     /* The VxWorks PLT is a loaded section with contents.  */
   2507     flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
   2508   return bfd_set_section_flags (s, flags);
   2509 }
   2510 
   2511 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   2512 
   2513 static void
   2514 ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
   2515 			      struct elf_link_hash_entry *dir,
   2516 			      struct elf_link_hash_entry *ind)
   2517 {
   2518   struct ppc_elf_link_hash_entry *edir, *eind;
   2519 
   2520   edir = (struct ppc_elf_link_hash_entry *) dir;
   2521   eind = (struct ppc_elf_link_hash_entry *) ind;
   2522 
   2523   edir->tls_mask |= eind->tls_mask;
   2524   edir->has_sda_refs |= eind->has_sda_refs;
   2525 
   2526   if (edir->elf.versioned != versioned_hidden)
   2527     edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   2528   edir->elf.ref_regular |= eind->elf.ref_regular;
   2529   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   2530   edir->elf.non_got_ref |= eind->elf.non_got_ref;
   2531   edir->elf.needs_plt |= eind->elf.needs_plt;
   2532   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
   2533 
   2534   /* If we were called to copy over info for a weak sym, that's all.  */
   2535   if (eind->elf.root.type != bfd_link_hash_indirect)
   2536     return;
   2537 
   2538   if (ind->dyn_relocs != NULL)
   2539     {
   2540       if (dir->dyn_relocs != NULL)
   2541 	{
   2542 	  struct elf_dyn_relocs **pp;
   2543 	  struct elf_dyn_relocs *p;
   2544 
   2545 	  /* Add reloc counts against the indirect sym to the direct sym
   2546 	     list.  Merge any entries against the same section.  */
   2547 	  for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
   2548 	    {
   2549 	      struct elf_dyn_relocs *q;
   2550 
   2551 	      for (q = dir->dyn_relocs; q != NULL; q = q->next)
   2552 		if (q->sec == p->sec)
   2553 		  {
   2554 		    q->pc_count += p->pc_count;
   2555 		    q->count += p->count;
   2556 		    *pp = p->next;
   2557 		    break;
   2558 		  }
   2559 	      if (q == NULL)
   2560 		pp = &p->next;
   2561 	    }
   2562 	  *pp = dir->dyn_relocs;
   2563 	}
   2564 
   2565       dir->dyn_relocs = ind->dyn_relocs;
   2566       ind->dyn_relocs = NULL;
   2567     }
   2568 
   2569   /* Copy over the GOT refcount entries that we may have already seen to
   2570      the symbol which just became indirect.  */
   2571   edir->elf.got.refcount += eind->elf.got.refcount;
   2572   eind->elf.got.refcount = 0;
   2573 
   2574   /* And plt entries.  */
   2575   if (eind->elf.plt.plist != NULL)
   2576     {
   2577       if (edir->elf.plt.plist != NULL)
   2578 	{
   2579 	  struct plt_entry **entp;
   2580 	  struct plt_entry *ent;
   2581 
   2582 	  for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
   2583 	    {
   2584 	      struct plt_entry *dent;
   2585 
   2586 	      for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
   2587 		if (dent->sec == ent->sec && dent->addend == ent->addend)
   2588 		  {
   2589 		    dent->plt.refcount += ent->plt.refcount;
   2590 		    *entp = ent->next;
   2591 		    break;
   2592 		  }
   2593 	      if (dent == NULL)
   2594 		entp = &ent->next;
   2595 	    }
   2596 	  *entp = edir->elf.plt.plist;
   2597 	}
   2598 
   2599       edir->elf.plt.plist = eind->elf.plt.plist;
   2600       eind->elf.plt.plist = NULL;
   2601     }
   2602 
   2603   if (eind->elf.dynindx != -1)
   2604     {
   2605       if (edir->elf.dynindx != -1)
   2606 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   2607 				edir->elf.dynstr_index);
   2608       edir->elf.dynindx = eind->elf.dynindx;
   2609       edir->elf.dynstr_index = eind->elf.dynstr_index;
   2610       eind->elf.dynindx = -1;
   2611       eind->elf.dynstr_index = 0;
   2612     }
   2613 }
   2614 
   2615 /* Hook called by the linker routine which adds symbols from an object
   2616    file.  We use it to put .comm items in .sbss, and not .bss.  */
   2617 
   2618 static bfd_boolean
   2619 ppc_elf_add_symbol_hook (bfd *abfd,
   2620 			 struct bfd_link_info *info,
   2621 			 Elf_Internal_Sym *sym,
   2622 			 const char **namep ATTRIBUTE_UNUSED,
   2623 			 flagword *flagsp ATTRIBUTE_UNUSED,
   2624 			 asection **secp,
   2625 			 bfd_vma *valp)
   2626 {
   2627   if (sym->st_shndx == SHN_COMMON
   2628       && !bfd_link_relocatable (info)
   2629       && is_ppc_elf (info->output_bfd)
   2630       && sym->st_size <= elf_gp_size (abfd))
   2631     {
   2632       /* Common symbols less than or equal to -G nn bytes are automatically
   2633 	 put into .sbss.  */
   2634       struct ppc_elf_link_hash_table *htab;
   2635 
   2636       htab = ppc_elf_hash_table (info);
   2637       if (htab->sbss == NULL)
   2638 	{
   2639 	  flagword flags = SEC_IS_COMMON | SEC_SMALL_DATA | SEC_LINKER_CREATED;
   2640 
   2641 	  if (!htab->elf.dynobj)
   2642 	    htab->elf.dynobj = abfd;
   2643 
   2644 	  htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
   2645 							   ".sbss",
   2646 							   flags);
   2647 	  if (htab->sbss == NULL)
   2648 	    return FALSE;
   2649 	}
   2650 
   2651       *secp = htab->sbss;
   2652       *valp = sym->st_size;
   2653     }
   2654 
   2655   return TRUE;
   2656 }
   2657 
   2658 /* Find a linker generated pointer with a given addend and type.  */
   2660 
   2661 static elf_linker_section_pointers_t *
   2662 elf_find_pointer_linker_section
   2663   (elf_linker_section_pointers_t *linker_pointers,
   2664    bfd_vma addend,
   2665    elf_linker_section_t *lsect)
   2666 {
   2667   for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
   2668     if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
   2669       return linker_pointers;
   2670 
   2671   return NULL;
   2672 }
   2673 
   2674 /* Allocate a pointer to live in a linker created section.  */
   2675 
   2676 static bfd_boolean
   2677 elf_allocate_pointer_linker_section (bfd *abfd,
   2678 				     elf_linker_section_t *lsect,
   2679 				     struct elf_link_hash_entry *h,
   2680 				     const Elf_Internal_Rela *rel)
   2681 {
   2682   elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
   2683   elf_linker_section_pointers_t *linker_section_ptr;
   2684   unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
   2685   bfd_size_type amt;
   2686 
   2687   BFD_ASSERT (lsect != NULL);
   2688 
   2689   /* Is this a global symbol?  */
   2690   if (h != NULL)
   2691     {
   2692       struct ppc_elf_link_hash_entry *eh;
   2693 
   2694       /* Has this symbol already been allocated?  If so, our work is done.  */
   2695       eh = (struct ppc_elf_link_hash_entry *) h;
   2696       if (elf_find_pointer_linker_section (eh->linker_section_pointer,
   2697 					   rel->r_addend,
   2698 					   lsect))
   2699 	return TRUE;
   2700 
   2701       ptr_linker_section_ptr = &eh->linker_section_pointer;
   2702     }
   2703   else
   2704     {
   2705       BFD_ASSERT (is_ppc_elf (abfd));
   2706 
   2707       /* Allocation of a pointer to a local symbol.  */
   2708       elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
   2709 
   2710       /* Allocate a table to hold the local symbols if first time.  */
   2711       if (!ptr)
   2712 	{
   2713 	  unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
   2714 
   2715 	  amt = num_symbols;
   2716 	  amt *= sizeof (elf_linker_section_pointers_t *);
   2717 	  ptr = bfd_zalloc (abfd, amt);
   2718 
   2719 	  if (!ptr)
   2720 	    return FALSE;
   2721 
   2722 	  elf_local_ptr_offsets (abfd) = ptr;
   2723 	}
   2724 
   2725       /* Has this symbol already been allocated?  If so, our work is done.  */
   2726       if (elf_find_pointer_linker_section (ptr[r_symndx],
   2727 					   rel->r_addend,
   2728 					   lsect))
   2729 	return TRUE;
   2730 
   2731       ptr_linker_section_ptr = &ptr[r_symndx];
   2732     }
   2733 
   2734   /* Allocate space for a pointer in the linker section, and allocate
   2735      a new pointer record from internal memory.  */
   2736   BFD_ASSERT (ptr_linker_section_ptr != NULL);
   2737   amt = sizeof (elf_linker_section_pointers_t);
   2738   linker_section_ptr = bfd_alloc (abfd, amt);
   2739 
   2740   if (!linker_section_ptr)
   2741     return FALSE;
   2742 
   2743   linker_section_ptr->next = *ptr_linker_section_ptr;
   2744   linker_section_ptr->addend = rel->r_addend;
   2745   linker_section_ptr->lsect = lsect;
   2746   *ptr_linker_section_ptr = linker_section_ptr;
   2747 
   2748   if (!bfd_set_section_alignment (lsect->section, 2))
   2749     return FALSE;
   2750   linker_section_ptr->offset = lsect->section->size;
   2751   lsect->section->size += 4;
   2752 
   2753 #ifdef DEBUG
   2754   fprintf (stderr,
   2755 	   "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
   2756 	   lsect->name, (long) linker_section_ptr->offset,
   2757 	   (long) lsect->section->size);
   2758 #endif
   2759 
   2760   return TRUE;
   2761 }
   2762 
   2763 static struct plt_entry **
   2764 update_local_sym_info (bfd *abfd,
   2765 		       Elf_Internal_Shdr *symtab_hdr,
   2766 		       unsigned long r_symndx,
   2767 		       int tls_type)
   2768 {
   2769   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
   2770   struct plt_entry **local_plt;
   2771   unsigned char *local_got_tls_masks;
   2772 
   2773   if (local_got_refcounts == NULL)
   2774     {
   2775       bfd_size_type size = symtab_hdr->sh_info;
   2776 
   2777       size *= (sizeof (*local_got_refcounts)
   2778 	       + sizeof (*local_plt)
   2779 	       + sizeof (*local_got_tls_masks));
   2780       local_got_refcounts = bfd_zalloc (abfd, size);
   2781       if (local_got_refcounts == NULL)
   2782 	return NULL;
   2783       elf_local_got_refcounts (abfd) = local_got_refcounts;
   2784     }
   2785 
   2786   local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
   2787   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   2788   local_got_tls_masks[r_symndx] |= tls_type & 0xff;
   2789   if ((tls_type & NON_GOT) == 0)
   2790     local_got_refcounts[r_symndx] += 1;
   2791   return local_plt + r_symndx;
   2792 }
   2793 
   2794 static bfd_boolean
   2795 update_plt_info (bfd *abfd, struct plt_entry **plist,
   2796 		 asection *sec, bfd_vma addend)
   2797 {
   2798   struct plt_entry *ent;
   2799 
   2800   if (addend < 32768)
   2801     sec = NULL;
   2802   for (ent = *plist; ent != NULL; ent = ent->next)
   2803     if (ent->sec == sec && ent->addend == addend)
   2804       break;
   2805   if (ent == NULL)
   2806     {
   2807       size_t amt = sizeof (*ent);
   2808       ent = bfd_alloc (abfd, amt);
   2809       if (ent == NULL)
   2810 	return FALSE;
   2811       ent->next = *plist;
   2812       ent->sec = sec;
   2813       ent->addend = addend;
   2814       ent->plt.refcount = 0;
   2815       *plist = ent;
   2816     }
   2817   ent->plt.refcount += 1;
   2818   return TRUE;
   2819 }
   2820 
   2821 static struct plt_entry *
   2822 find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
   2823 {
   2824   struct plt_entry *ent;
   2825 
   2826   if (addend < 32768)
   2827     sec = NULL;
   2828   for (ent = *plist; ent != NULL; ent = ent->next)
   2829     if (ent->sec == sec && ent->addend == addend)
   2830       break;
   2831   return ent;
   2832 }
   2833 
   2834 static bfd_boolean
   2835 is_branch_reloc (enum elf_ppc_reloc_type r_type)
   2836 {
   2837   return (r_type == R_PPC_PLTREL24
   2838 	  || r_type == R_PPC_LOCAL24PC
   2839 	  || r_type == R_PPC_REL24
   2840 	  || r_type == R_PPC_REL14
   2841 	  || r_type == R_PPC_REL14_BRTAKEN
   2842 	  || r_type == R_PPC_REL14_BRNTAKEN
   2843 	  || r_type == R_PPC_ADDR24
   2844 	  || r_type == R_PPC_ADDR14
   2845 	  || r_type == R_PPC_ADDR14_BRTAKEN
   2846 	  || r_type == R_PPC_ADDR14_BRNTAKEN
   2847 	  || r_type == R_PPC_VLE_REL24);
   2848 }
   2849 
   2850 /* Relocs on inline plt call sequence insns prior to the call.  */
   2851 
   2852 static bfd_boolean
   2853 is_plt_seq_reloc (enum elf_ppc_reloc_type r_type)
   2854 {
   2855   return (r_type == R_PPC_PLT16_HA
   2856 	  || r_type == R_PPC_PLT16_HI
   2857 	  || r_type == R_PPC_PLT16_LO
   2858 	  || r_type == R_PPC_PLTSEQ);
   2859 }
   2860 
   2861 static void
   2862 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
   2863 {
   2864   _bfd_error_handler
   2865     /* xgettext:c-format */
   2866     (_("%pB: relocation %s cannot be used when making a shared object"),
   2867      abfd,
   2868      ppc_elf_howto_table[r_type]->name);
   2869   bfd_set_error (bfd_error_bad_value);
   2870 }
   2871 
   2872 /* Look through the relocs for a section during the first phase, and
   2873    allocate space in the global offset table or procedure linkage
   2874    table.  */
   2875 
   2876 static bfd_boolean
   2877 ppc_elf_check_relocs (bfd *abfd,
   2878 		      struct bfd_link_info *info,
   2879 		      asection *sec,
   2880 		      const Elf_Internal_Rela *relocs)
   2881 {
   2882   struct ppc_elf_link_hash_table *htab;
   2883   Elf_Internal_Shdr *symtab_hdr;
   2884   struct elf_link_hash_entry **sym_hashes;
   2885   const Elf_Internal_Rela *rel;
   2886   const Elf_Internal_Rela *rel_end;
   2887   asection *got2, *sreloc;
   2888   struct elf_link_hash_entry *tga;
   2889 
   2890   if (bfd_link_relocatable (info))
   2891     return TRUE;
   2892 
   2893 #ifdef DEBUG
   2894   _bfd_error_handler ("ppc_elf_check_relocs called for section %pA in %pB",
   2895 		      sec, abfd);
   2896 #endif
   2897 
   2898   BFD_ASSERT (is_ppc_elf (abfd));
   2899 
   2900   /* Initialize howto table if not already done.  */
   2901   if (!ppc_elf_howto_table[R_PPC_ADDR32])
   2902     ppc_elf_howto_init ();
   2903 
   2904   htab = ppc_elf_hash_table (info);
   2905   if (htab->glink == NULL)
   2906     {
   2907       if (htab->elf.dynobj == NULL)
   2908 	htab->elf.dynobj = abfd;
   2909       if (!ppc_elf_create_glink (htab->elf.dynobj, info))
   2910 	return FALSE;
   2911     }
   2912   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   2913 			      FALSE, FALSE, TRUE);
   2914   symtab_hdr = &elf_symtab_hdr (abfd);
   2915   sym_hashes = elf_sym_hashes (abfd);
   2916   got2 = bfd_get_section_by_name (abfd, ".got2");
   2917   sreloc = NULL;
   2918 
   2919   rel_end = relocs + sec->reloc_count;
   2920   for (rel = relocs; rel < rel_end; rel++)
   2921     {
   2922       unsigned long r_symndx;
   2923       enum elf_ppc_reloc_type r_type;
   2924       struct elf_link_hash_entry *h;
   2925       int tls_type;
   2926       struct plt_entry **ifunc;
   2927       struct plt_entry **pltent;
   2928       bfd_vma addend;
   2929 
   2930       r_symndx = ELF32_R_SYM (rel->r_info);
   2931       if (r_symndx < symtab_hdr->sh_info)
   2932 	h = NULL;
   2933       else
   2934 	{
   2935 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   2936 	  while (h->root.type == bfd_link_hash_indirect
   2937 		 || h->root.type == bfd_link_hash_warning)
   2938 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   2939 	}
   2940 
   2941       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
   2942 	 This shows up in particular in an R_PPC_ADDR32 in the eabi
   2943 	 startup code.  */
   2944       if (h != NULL
   2945 	  && htab->elf.sgot == NULL
   2946 	  && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
   2947 	{
   2948 	  if (htab->elf.dynobj == NULL)
   2949 	    htab->elf.dynobj = abfd;
   2950 	  if (!ppc_elf_create_got (htab->elf.dynobj, info))
   2951 	    return FALSE;
   2952 	  BFD_ASSERT (h == htab->elf.hgot);
   2953 	}
   2954 
   2955       tls_type = 0;
   2956       r_type = ELF32_R_TYPE (rel->r_info);
   2957       ifunc = NULL;
   2958       if (h == NULL && htab->elf.target_os != is_vxworks)
   2959 	{
   2960 	  Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
   2961 							  abfd, r_symndx);
   2962 	  if (isym == NULL)
   2963 	    return FALSE;
   2964 
   2965 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   2966 	    {
   2967 	      /* Set PLT_IFUNC flag for this sym, no GOT entry yet.  */
   2968 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   2969 					     NON_GOT | PLT_IFUNC);
   2970 	      if (ifunc == NULL)
   2971 		return FALSE;
   2972 
   2973 	      /* STT_GNU_IFUNC symbols must have a PLT entry;
   2974 		 In a non-pie executable even when there are
   2975 		 no plt calls.  */
   2976 	      if (!bfd_link_pic (info)
   2977 		  || is_branch_reloc (r_type)
   2978 		  || r_type == R_PPC_PLT16_LO
   2979 		  || r_type == R_PPC_PLT16_HI
   2980 		  || r_type == R_PPC_PLT16_HA)
   2981 		{
   2982 		  addend = 0;
   2983 		  if (r_type == R_PPC_PLTREL24)
   2984 		    ppc_elf_tdata (abfd)->makes_plt_call = 1;
   2985 		  if (bfd_link_pic (info)
   2986 		      && (r_type == R_PPC_PLTREL24
   2987 			  || r_type == R_PPC_PLT16_LO
   2988 			  || r_type == R_PPC_PLT16_HI
   2989 			  || r_type == R_PPC_PLT16_HA))
   2990 		    addend = rel->r_addend;
   2991 		  if (!update_plt_info (abfd, ifunc, got2, addend))
   2992 		    return FALSE;
   2993 		}
   2994 	    }
   2995 	}
   2996 
   2997       if (htab->elf.target_os != is_vxworks
   2998 	  && is_branch_reloc (r_type)
   2999 	  && h != NULL
   3000 	  && h == tga)
   3001 	{
   3002 	  if (rel != relocs
   3003 	      && (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
   3004 		  || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
   3005 	    /* We have a new-style __tls_get_addr call with a marker
   3006 	       reloc.  */
   3007 	    ;
   3008 	  else
   3009 	    /* Mark this section as having an old-style call.  */
   3010 	    sec->nomark_tls_get_addr = 1;
   3011 	}
   3012 
   3013       switch (r_type)
   3014 	{
   3015 	case R_PPC_TLSGD:
   3016 	case R_PPC_TLSLD:
   3017 	  /* These special tls relocs tie a call to __tls_get_addr with
   3018 	     its parameter symbol.  */
   3019 	  if (h != NULL)
   3020 	    ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
   3021 	  else
   3022 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   3023 					NON_GOT | TLS_TLS | TLS_MARK))
   3024 	      return FALSE;
   3025 	  break;
   3026 
   3027 	case R_PPC_PLTSEQ:
   3028 	  break;
   3029 
   3030 	case R_PPC_GOT_TLSLD16:
   3031 	case R_PPC_GOT_TLSLD16_LO:
   3032 	case R_PPC_GOT_TLSLD16_HI:
   3033 	case R_PPC_GOT_TLSLD16_HA:
   3034 	  tls_type = TLS_TLS | TLS_LD;
   3035 	  goto dogottls;
   3036 
   3037 	case R_PPC_GOT_TLSGD16:
   3038 	case R_PPC_GOT_TLSGD16_LO:
   3039 	case R_PPC_GOT_TLSGD16_HI:
   3040 	case R_PPC_GOT_TLSGD16_HA:
   3041 	  tls_type = TLS_TLS | TLS_GD;
   3042 	  goto dogottls;
   3043 
   3044 	case R_PPC_GOT_TPREL16:
   3045 	case R_PPC_GOT_TPREL16_LO:
   3046 	case R_PPC_GOT_TPREL16_HI:
   3047 	case R_PPC_GOT_TPREL16_HA:
   3048 	  if (bfd_link_dll (info))
   3049 	    info->flags |= DF_STATIC_TLS;
   3050 	  tls_type = TLS_TLS | TLS_TPREL;
   3051 	  goto dogottls;
   3052 
   3053 	case R_PPC_GOT_DTPREL16:
   3054 	case R_PPC_GOT_DTPREL16_LO:
   3055 	case R_PPC_GOT_DTPREL16_HI:
   3056 	case R_PPC_GOT_DTPREL16_HA:
   3057 	  tls_type = TLS_TLS | TLS_DTPREL;
   3058 	dogottls:
   3059 	  sec->has_tls_reloc = 1;
   3060 	  /* Fall through.  */
   3061 
   3062 	  /* GOT16 relocations */
   3063 	case R_PPC_GOT16:
   3064 	case R_PPC_GOT16_LO:
   3065 	case R_PPC_GOT16_HI:
   3066 	case R_PPC_GOT16_HA:
   3067 	  /* This symbol requires a global offset table entry.  */
   3068 	  if (htab->elf.sgot == NULL)
   3069 	    {
   3070 	      if (htab->elf.dynobj == NULL)
   3071 		htab->elf.dynobj = abfd;
   3072 	      if (!ppc_elf_create_got (htab->elf.dynobj, info))
   3073 		return FALSE;
   3074 	    }
   3075 	  if (h != NULL)
   3076 	    {
   3077 	      h->got.refcount += 1;
   3078 	      ppc_elf_hash_entry (h)->tls_mask |= tls_type;
   3079 	    }
   3080 	  else
   3081 	    /* This is a global offset table entry for a local symbol.  */
   3082 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
   3083 	      return FALSE;
   3084 
   3085 	  /* We may also need a plt entry if the symbol turns out to be
   3086 	     an ifunc.  */
   3087 	  if (h != NULL && !bfd_link_pic (info))
   3088 	    {
   3089 	      if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
   3090 		return FALSE;
   3091 	    }
   3092 	  break;
   3093 
   3094 	  /* Indirect .sdata relocation.  */
   3095 	case R_PPC_EMB_SDAI16:
   3096 	  htab->sdata[0].sym->ref_regular = 1;
   3097 	  if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[0],
   3098 						    h, rel))
   3099 	    return FALSE;
   3100 	  if (h != NULL)
   3101 	    {
   3102 	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
   3103 	      h->non_got_ref = TRUE;
   3104 	    }
   3105 	  break;
   3106 
   3107 	  /* Indirect .sdata2 relocation.  */
   3108 	case R_PPC_EMB_SDA2I16:
   3109 	  if (!bfd_link_executable (info))
   3110 	    {
   3111 	      bad_shared_reloc (abfd, r_type);
   3112 	      return FALSE;
   3113 	    }
   3114 	  htab->sdata[1].sym->ref_regular = 1;
   3115 	  if (!elf_allocate_pointer_linker_section (abfd, &htab->sdata[1],
   3116 						    h, rel))
   3117 	    return FALSE;
   3118 	  if (h != NULL)
   3119 	    {
   3120 	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
   3121 	      h->non_got_ref = TRUE;
   3122 	    }
   3123 	  break;
   3124 
   3125 	case R_PPC_SDAREL16:
   3126 	  htab->sdata[0].sym->ref_regular = 1;
   3127 	  /* Fall through.  */
   3128 
   3129 	case R_PPC_VLE_SDAREL_LO16A:
   3130 	case R_PPC_VLE_SDAREL_LO16D:
   3131 	case R_PPC_VLE_SDAREL_HI16A:
   3132 	case R_PPC_VLE_SDAREL_HI16D:
   3133 	case R_PPC_VLE_SDAREL_HA16A:
   3134 	case R_PPC_VLE_SDAREL_HA16D:
   3135 	  if (h != NULL)
   3136 	    {
   3137 	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
   3138 	      h->non_got_ref = TRUE;
   3139 	    }
   3140 	  break;
   3141 
   3142 	case R_PPC_VLE_REL8:
   3143 	case R_PPC_VLE_REL15:
   3144 	case R_PPC_VLE_REL24:
   3145 	case R_PPC_VLE_LO16A:
   3146 	case R_PPC_VLE_LO16D:
   3147 	case R_PPC_VLE_HI16A:
   3148 	case R_PPC_VLE_HI16D:
   3149 	case R_PPC_VLE_HA16A:
   3150 	case R_PPC_VLE_HA16D:
   3151 	case R_PPC_VLE_ADDR20:
   3152 	  break;
   3153 
   3154 	case R_PPC_EMB_SDA2REL:
   3155 	  if (!bfd_link_executable (info))
   3156 	    {
   3157 	      bad_shared_reloc (abfd, r_type);
   3158 	      return FALSE;
   3159 	    }
   3160 	  htab->sdata[1].sym->ref_regular = 1;
   3161 	  if (h != NULL)
   3162 	    {
   3163 	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
   3164 	      h->non_got_ref = TRUE;
   3165 	    }
   3166 	  break;
   3167 
   3168 	case R_PPC_VLE_SDA21_LO:
   3169 	case R_PPC_VLE_SDA21:
   3170 	case R_PPC_EMB_SDA21:
   3171 	case R_PPC_EMB_RELSDA:
   3172 	  if (h != NULL)
   3173 	    {
   3174 	      ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
   3175 	      h->non_got_ref = TRUE;
   3176 	    }
   3177 	  break;
   3178 
   3179 	case R_PPC_EMB_NADDR32:
   3180 	case R_PPC_EMB_NADDR16:
   3181 	case R_PPC_EMB_NADDR16_LO:
   3182 	case R_PPC_EMB_NADDR16_HI:
   3183 	case R_PPC_EMB_NADDR16_HA:
   3184 	  if (h != NULL)
   3185 	    h->non_got_ref = TRUE;
   3186 	  break;
   3187 
   3188 	case R_PPC_PLTREL24:
   3189 	  if (h == NULL)
   3190 	    break;
   3191 	  ppc_elf_tdata (abfd)->makes_plt_call = 1;
   3192 	  goto pltentry;
   3193 
   3194 	case R_PPC_PLTCALL:
   3195 	  sec->has_pltcall = 1;
   3196 	  /* Fall through.  */
   3197 
   3198 	case R_PPC_PLT32:
   3199 	case R_PPC_PLTREL32:
   3200 	case R_PPC_PLT16_LO:
   3201 	case R_PPC_PLT16_HI:
   3202 	case R_PPC_PLT16_HA:
   3203 	pltentry:
   3204 #ifdef DEBUG
   3205 	  fprintf (stderr, "Reloc requires a PLT entry\n");
   3206 #endif
   3207 	  /* This symbol requires a procedure linkage table entry.  */
   3208 	  if (h == NULL)
   3209 	    {
   3210 	      pltent = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   3211 					      NON_GOT | PLT_KEEP);
   3212 	      if (pltent == NULL)
   3213 		return FALSE;
   3214 	    }
   3215 	  else
   3216 	    {
   3217 	      if (r_type != R_PPC_PLTREL24)
   3218 		ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
   3219 	      h->needs_plt = 1;
   3220 	      pltent = &h->plt.plist;
   3221 	    }
   3222 	  addend = 0;
   3223 	  if (bfd_link_pic (info)
   3224 	      && (r_type == R_PPC_PLTREL24
   3225 		  || r_type == R_PPC_PLT16_LO
   3226 		  || r_type == R_PPC_PLT16_HI
   3227 		  || r_type == R_PPC_PLT16_HA))
   3228 	    addend = rel->r_addend;
   3229 	  if (!update_plt_info (abfd, pltent, got2, addend))
   3230 	    return FALSE;
   3231 	  break;
   3232 
   3233 	  /* The following relocations don't need to propagate the
   3234 	     relocation if linking a shared object since they are
   3235 	     section relative.  */
   3236 	case R_PPC_SECTOFF:
   3237 	case R_PPC_SECTOFF_LO:
   3238 	case R_PPC_SECTOFF_HI:
   3239 	case R_PPC_SECTOFF_HA:
   3240 	case R_PPC_DTPREL16:
   3241 	case R_PPC_DTPREL16_LO:
   3242 	case R_PPC_DTPREL16_HI:
   3243 	case R_PPC_DTPREL16_HA:
   3244 	case R_PPC_TOC16:
   3245 	  break;
   3246 
   3247 	case R_PPC_REL16:
   3248 	case R_PPC_REL16_LO:
   3249 	case R_PPC_REL16_HI:
   3250 	case R_PPC_REL16_HA:
   3251 	case R_PPC_REL16DX_HA:
   3252 	  ppc_elf_tdata (abfd)->has_rel16 = 1;
   3253 	  break;
   3254 
   3255 	  /* These are just markers.  */
   3256 	case R_PPC_TLS:
   3257 	case R_PPC_EMB_MRKREF:
   3258 	case R_PPC_NONE:
   3259 	case R_PPC_max:
   3260 	case R_PPC_RELAX:
   3261 	case R_PPC_RELAX_PLT:
   3262 	case R_PPC_RELAX_PLTREL24:
   3263 	case R_PPC_16DX_HA:
   3264 	  break;
   3265 
   3266 	  /* These should only appear in dynamic objects.  */
   3267 	case R_PPC_COPY:
   3268 	case R_PPC_GLOB_DAT:
   3269 	case R_PPC_JMP_SLOT:
   3270 	case R_PPC_RELATIVE:
   3271 	case R_PPC_IRELATIVE:
   3272 	  break;
   3273 
   3274 	  /* These aren't handled yet.  We'll report an error later.  */
   3275 	case R_PPC_ADDR30:
   3276 	case R_PPC_EMB_RELSEC16:
   3277 	case R_PPC_EMB_RELST_LO:
   3278 	case R_PPC_EMB_RELST_HI:
   3279 	case R_PPC_EMB_RELST_HA:
   3280 	case R_PPC_EMB_BIT_FLD:
   3281 	  break;
   3282 
   3283 	  /* This refers only to functions defined in the shared library.  */
   3284 	case R_PPC_LOCAL24PC:
   3285 	  if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
   3286 	    {
   3287 	      htab->plt_type = PLT_OLD;
   3288 	      htab->old_bfd = abfd;
   3289 	    }
   3290 	  if (h != NULL && h->type == STT_GNU_IFUNC)
   3291 	    {
   3292 	      h->needs_plt = 1;
   3293 	      if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
   3294 		return FALSE;
   3295 	    }
   3296 	  break;
   3297 
   3298 	  /* This relocation describes the C++ object vtable hierarchy.
   3299 	     Reconstruct it for later use during GC.  */
   3300 	case R_PPC_GNU_VTINHERIT:
   3301 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   3302 	    return FALSE;
   3303 	  break;
   3304 
   3305 	  /* This relocation describes which C++ vtable entries are actually
   3306 	     used.  Record for later use during GC.  */
   3307 	case R_PPC_GNU_VTENTRY:
   3308 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   3309 	    return FALSE;
   3310 	  break;
   3311 
   3312 	case R_PPC_TPREL16_HI:
   3313 	case R_PPC_TPREL16_HA:
   3314 	  sec->has_tls_reloc = 1;
   3315 	  /* Fall through.  */
   3316 	  /* We shouldn't really be seeing TPREL32.  */
   3317 	case R_PPC_TPREL32:
   3318 	case R_PPC_TPREL16:
   3319 	case R_PPC_TPREL16_LO:
   3320 	  if (bfd_link_dll (info))
   3321 	    info->flags |= DF_STATIC_TLS;
   3322 	  goto dodyn;
   3323 
   3324 	  /* Nor these.  */
   3325 	case R_PPC_DTPMOD32:
   3326 	case R_PPC_DTPREL32:
   3327 	  goto dodyn;
   3328 
   3329 	case R_PPC_REL32:
   3330 	  if (h == NULL
   3331 	      && got2 != NULL
   3332 	      && (sec->flags & SEC_CODE) != 0
   3333 	      && bfd_link_pic (info)
   3334 	      && htab->plt_type == PLT_UNSET)
   3335 	    {
   3336 	      /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
   3337 		 the start of a function, which assembles to a REL32
   3338 		 reference to .got2.  If we detect one of these, then
   3339 		 force the old PLT layout because the linker cannot
   3340 		 reliably deduce the GOT pointer value needed for
   3341 		 PLT call stubs.  */
   3342 	      asection *s;
   3343 	      Elf_Internal_Sym *isym;
   3344 
   3345 	      isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
   3346 					    abfd, r_symndx);
   3347 	      if (isym == NULL)
   3348 		return FALSE;
   3349 
   3350 	      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   3351 	      if (s == got2)
   3352 		{
   3353 		  htab->plt_type = PLT_OLD;
   3354 		  htab->old_bfd = abfd;
   3355 		}
   3356 	    }
   3357 	  if (h == NULL || h == htab->elf.hgot)
   3358 	    break;
   3359 	  /* fall through */
   3360 
   3361 	case R_PPC_ADDR32:
   3362 	case R_PPC_ADDR16:
   3363 	case R_PPC_ADDR16_LO:
   3364 	case R_PPC_ADDR16_HI:
   3365 	case R_PPC_ADDR16_HA:
   3366 	case R_PPC_UADDR32:
   3367 	case R_PPC_UADDR16:
   3368 	  if (h != NULL && !bfd_link_pic (info))
   3369 	    {
   3370 	      /* We may need a plt entry if the symbol turns out to be
   3371 		 a function defined in a dynamic object.  */
   3372 	      if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
   3373 		return FALSE;
   3374 
   3375 	      /* We may need a copy reloc too.  */
   3376 	      h->non_got_ref = 1;
   3377 	      h->pointer_equality_needed = 1;
   3378 	      if (r_type == R_PPC_ADDR16_HA)
   3379 		ppc_elf_hash_entry (h)->has_addr16_ha = 1;
   3380 	      if (r_type == R_PPC_ADDR16_LO)
   3381 		ppc_elf_hash_entry (h)->has_addr16_lo = 1;
   3382 	    }
   3383 	  goto dodyn;
   3384 
   3385 	case R_PPC_REL24:
   3386 	case R_PPC_REL14:
   3387 	case R_PPC_REL14_BRTAKEN:
   3388 	case R_PPC_REL14_BRNTAKEN:
   3389 	  if (h == NULL)
   3390 	    break;
   3391 	  if (h == htab->elf.hgot)
   3392 	    {
   3393 	      if (htab->plt_type == PLT_UNSET)
   3394 		{
   3395 		  htab->plt_type = PLT_OLD;
   3396 		  htab->old_bfd = abfd;
   3397 		}
   3398 	      break;
   3399 	    }
   3400 	  /* fall through */
   3401 
   3402 	case R_PPC_ADDR24:
   3403 	case R_PPC_ADDR14:
   3404 	case R_PPC_ADDR14_BRTAKEN:
   3405 	case R_PPC_ADDR14_BRNTAKEN:
   3406 	  if (h != NULL && !bfd_link_pic (info))
   3407 	    {
   3408 	      /* We may need a plt entry if the symbol turns out to be
   3409 		 a function defined in a dynamic object.  */
   3410 	      h->needs_plt = 1;
   3411 	      if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
   3412 		return FALSE;
   3413 	      break;
   3414 	    }
   3415 
   3416 	dodyn:
   3417 	  /* If we are creating a shared library, and this is a reloc
   3418 	     against a global symbol, or a non PC relative reloc
   3419 	     against a local symbol, then we need to copy the reloc
   3420 	     into the shared library.  However, if we are linking with
   3421 	     -Bsymbolic, we do not need to copy a reloc against a
   3422 	     global symbol which is defined in an object we are
   3423 	     including in the link (i.e., DEF_REGULAR is set).  At
   3424 	     this point we have not seen all the input files, so it is
   3425 	     possible that DEF_REGULAR is not set now but will be set
   3426 	     later (it is never cleared).  In case of a weak definition,
   3427 	     DEF_REGULAR may be cleared later by a strong definition in
   3428 	     a shared library.  We account for that possibility below by
   3429 	     storing information in the dyn_relocs field of the hash
   3430 	     table entry.  A similar situation occurs when creating
   3431 	     shared libraries and symbol visibility changes render the
   3432 	     symbol local.
   3433 
   3434 	     If on the other hand, we are creating an executable, we
   3435 	     may need to keep relocations for symbols satisfied by a
   3436 	     dynamic library if we manage to avoid copy relocs for the
   3437 	     symbol.  */
   3438 	  if ((bfd_link_pic (info)
   3439 	       && (must_be_dyn_reloc (info, r_type)
   3440 		   || (h != NULL
   3441 		       && (!SYMBOLIC_BIND (info, h)
   3442 			   || h->root.type == bfd_link_hash_defweak
   3443 			   || !h->def_regular))))
   3444 	      || (ELIMINATE_COPY_RELOCS
   3445 		  && !bfd_link_pic (info)
   3446 		  && h != NULL
   3447 		  && (h->root.type == bfd_link_hash_defweak
   3448 		      || !h->def_regular)))
   3449 	    {
   3450 #ifdef DEBUG
   3451 	      fprintf (stderr,
   3452 		       "ppc_elf_check_relocs needs to "
   3453 		       "create relocation for %s\n",
   3454 		       (h && h->root.root.string
   3455 			? h->root.root.string : "<unknown>"));
   3456 #endif
   3457 	      if (sreloc == NULL)
   3458 		{
   3459 		  if (htab->elf.dynobj == NULL)
   3460 		    htab->elf.dynobj = abfd;
   3461 
   3462 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   3463 		    (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ TRUE);
   3464 
   3465 		  if (sreloc == NULL)
   3466 		    return FALSE;
   3467 		}
   3468 
   3469 	      /* If this is a global symbol, we count the number of
   3470 		 relocations we need for this symbol.  */
   3471 	      if (h != NULL)
   3472 		{
   3473 		  struct elf_dyn_relocs *p;
   3474 		  struct elf_dyn_relocs **rel_head;
   3475 
   3476 		  rel_head = &h->dyn_relocs;
   3477 		  p = *rel_head;
   3478 		  if (p == NULL || p->sec != sec)
   3479 		    {
   3480 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   3481 		      if (p == NULL)
   3482 			return FALSE;
   3483 		      p->next = *rel_head;
   3484 		      *rel_head = p;
   3485 		      p->sec = sec;
   3486 		      p->count = 0;
   3487 		      p->pc_count = 0;
   3488 		    }
   3489 		  p->count += 1;
   3490 		  if (!must_be_dyn_reloc (info, r_type))
   3491 		    p->pc_count += 1;
   3492 		}
   3493 	      else
   3494 		{
   3495 		  /* Track dynamic relocs needed for local syms too.
   3496 		     We really need local syms available to do this
   3497 		     easily.  Oh well.  */
   3498 		  struct ppc_dyn_relocs *p;
   3499 		  struct ppc_dyn_relocs **rel_head;
   3500 		  bfd_boolean is_ifunc;
   3501 		  asection *s;
   3502 		  void *vpp;
   3503 		  Elf_Internal_Sym *isym;
   3504 
   3505 		  isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
   3506 						abfd, r_symndx);
   3507 		  if (isym == NULL)
   3508 		    return FALSE;
   3509 
   3510 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   3511 		  if (s == NULL)
   3512 		    s = sec;
   3513 
   3514 		  vpp = &elf_section_data (s)->local_dynrel;
   3515 		  rel_head = (struct ppc_dyn_relocs **) vpp;
   3516 		  is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
   3517 		  p = *rel_head;
   3518 		  if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
   3519 		    p = p->next;
   3520 		  if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
   3521 		    {
   3522 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   3523 		      if (p == NULL)
   3524 			return FALSE;
   3525 		      p->next = *rel_head;
   3526 		      *rel_head = p;
   3527 		      p->sec = sec;
   3528 		      p->ifunc = is_ifunc;
   3529 		      p->count = 0;
   3530 		    }
   3531 		  p->count += 1;
   3532 		}
   3533 	    }
   3534 
   3535 	  break;
   3536 	}
   3537     }
   3538 
   3539   return TRUE;
   3540 }
   3541 
   3542 /* Warn for conflicting Tag_GNU_Power_ABI_FP attributes between IBFD
   3544    and OBFD, and merge non-conflicting ones.  */
   3545 bfd_boolean
   3546 _bfd_elf_ppc_merge_fp_attributes (bfd *ibfd, struct bfd_link_info *info)
   3547 {
   3548   bfd *obfd = info->output_bfd;
   3549   obj_attribute *in_attr, *in_attrs;
   3550   obj_attribute *out_attr, *out_attrs;
   3551   bfd_boolean ret = TRUE;
   3552   bfd_boolean warn_only;
   3553 
   3554   /* We only warn about shared library mismatches, because common
   3555      libraries advertise support for a particular long double variant
   3556      but actually support more than one variant.  For example, glibc
   3557      typically supports 128-bit IBM long double in the shared library
   3558      but has a compatibility static archive for 64-bit long double.
   3559      The linker doesn't have the smarts to see that an app using
   3560      object files marked as 64-bit long double call the compatibility
   3561      layer objects and only from there call into the shared library.  */
   3562   warn_only = (ibfd->flags & DYNAMIC) != 0;
   3563 
   3564   in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   3565   out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   3566 
   3567   in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
   3568   out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
   3569 
   3570   if (in_attr->i != out_attr->i)
   3571     {
   3572       int in_fp = in_attr->i & 3;
   3573       int out_fp = out_attr->i & 3;
   3574       static bfd *last_fp, *last_ld;
   3575 
   3576       if (in_fp == 0)
   3577 	;
   3578       else if (out_fp == 0)
   3579 	{
   3580 	  if (!warn_only)
   3581 	    {
   3582 	      out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3583 	      out_attr->i ^= in_fp;
   3584 	      last_fp = ibfd;
   3585 	    }
   3586 	}
   3587       else if (out_fp != 2 && in_fp == 2)
   3588 	{
   3589 	  _bfd_error_handler
   3590 	    /* xgettext:c-format */
   3591 	    (_("%pB uses hard float, %pB uses soft float"),
   3592 	     last_fp, ibfd);
   3593 	  ret = warn_only;
   3594 	}
   3595       else if (out_fp == 2 && in_fp != 2)
   3596 	{
   3597 	  _bfd_error_handler
   3598 	    /* xgettext:c-format */
   3599 	    (_("%pB uses hard float, %pB uses soft float"),
   3600 	     ibfd, last_fp);
   3601 	  ret = warn_only;
   3602 	}
   3603       else if (out_fp == 1 && in_fp == 3)
   3604 	{
   3605 	  _bfd_error_handler
   3606 	    /* xgettext:c-format */
   3607 	    (_("%pB uses double-precision hard float, "
   3608 	       "%pB uses single-precision hard float"), last_fp, ibfd);
   3609 	  ret = warn_only;
   3610 	}
   3611       else if (out_fp == 3 && in_fp == 1)
   3612 	{
   3613 	  _bfd_error_handler
   3614 	    /* xgettext:c-format */
   3615 	    (_("%pB uses double-precision hard float, "
   3616 	       "%pB uses single-precision hard float"), ibfd, last_fp);
   3617 	  ret = warn_only;
   3618 	}
   3619 
   3620       in_fp = in_attr->i & 0xc;
   3621       out_fp = out_attr->i & 0xc;
   3622       if (in_fp == 0)
   3623 	;
   3624       else if (out_fp == 0)
   3625 	{
   3626 	  if (!warn_only)
   3627 	    {
   3628 	      out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3629 	      out_attr->i ^= in_fp;
   3630 	      last_ld = ibfd;
   3631 	    }
   3632 	}
   3633       else if (out_fp != 2 * 4 && in_fp == 2 * 4)
   3634 	{
   3635 	  _bfd_error_handler
   3636 	    /* xgettext:c-format */
   3637 	    (_("%pB uses 64-bit long double, "
   3638 	       "%pB uses 128-bit long double"), ibfd, last_ld);
   3639 	  ret = warn_only;
   3640 	}
   3641       else if (in_fp != 2 * 4 && out_fp == 2 * 4)
   3642 	{
   3643 	  _bfd_error_handler
   3644 	    /* xgettext:c-format */
   3645 	    (_("%pB uses 64-bit long double, "
   3646 	       "%pB uses 128-bit long double"), last_ld, ibfd);
   3647 	  ret = warn_only;
   3648 	}
   3649       else if (out_fp == 1 * 4 && in_fp == 3 * 4)
   3650 	{
   3651 	  _bfd_error_handler
   3652 	    /* xgettext:c-format */
   3653 	    (_("%pB uses IBM long double, "
   3654 	       "%pB uses IEEE long double"), last_ld, ibfd);
   3655 	  ret = warn_only;
   3656 	}
   3657       else if (out_fp == 3 * 4 && in_fp == 1 * 4)
   3658 	{
   3659 	  _bfd_error_handler
   3660 	    /* xgettext:c-format */
   3661 	    (_("%pB uses IBM long double, "
   3662 	       "%pB uses IEEE long double"), ibfd, last_ld);
   3663 	  ret = warn_only;
   3664 	}
   3665     }
   3666 
   3667   if (!ret)
   3668     {
   3669       out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3670       bfd_set_error (bfd_error_bad_value);
   3671     }
   3672   return ret;
   3673 }
   3674 
   3675 /* Merge object attributes from IBFD into OBFD.  Warn if
   3676    there are conflicting attributes.  */
   3677 static bfd_boolean
   3678 ppc_elf_merge_obj_attributes (bfd *ibfd, struct bfd_link_info *info)
   3679 {
   3680   bfd *obfd;
   3681   obj_attribute *in_attr, *in_attrs;
   3682   obj_attribute *out_attr, *out_attrs;
   3683   bfd_boolean ret;
   3684 
   3685   if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
   3686     return FALSE;
   3687 
   3688   obfd = info->output_bfd;
   3689   in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   3690   out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   3691 
   3692   /* Check for conflicting Tag_GNU_Power_ABI_Vector attributes and
   3693      merge non-conflicting ones.  */
   3694   in_attr = &in_attrs[Tag_GNU_Power_ABI_Vector];
   3695   out_attr = &out_attrs[Tag_GNU_Power_ABI_Vector];
   3696   ret = TRUE;
   3697   if (in_attr->i != out_attr->i)
   3698     {
   3699       int in_vec = in_attr->i & 3;
   3700       int out_vec = out_attr->i & 3;
   3701       static bfd *last_vec;
   3702 
   3703       if (in_vec == 0)
   3704 	;
   3705       else if (out_vec == 0)
   3706 	{
   3707 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3708 	  out_attr->i = in_vec;
   3709 	  last_vec = ibfd;
   3710 	}
   3711       /* For now, allow generic to transition to AltiVec or SPE
   3712 	 without a warning.  If GCC marked files with their stack
   3713 	 alignment and used don't-care markings for files which are
   3714 	 not affected by the vector ABI, we could warn about this
   3715 	 case too.  */
   3716       else if (in_vec == 1)
   3717 	;
   3718       else if (out_vec == 1)
   3719 	{
   3720 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3721 	  out_attr->i = in_vec;
   3722 	  last_vec = ibfd;
   3723 	}
   3724       else if (out_vec < in_vec)
   3725 	{
   3726 	  _bfd_error_handler
   3727 	    /* xgettext:c-format */
   3728 	    (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
   3729 	     last_vec, ibfd);
   3730 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3731 	  ret = FALSE;
   3732 	}
   3733       else if (out_vec > in_vec)
   3734 	{
   3735 	  _bfd_error_handler
   3736 	    /* xgettext:c-format */
   3737 	    (_("%pB uses AltiVec vector ABI, %pB uses SPE vector ABI"),
   3738 	     ibfd, last_vec);
   3739 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3740 	  ret = FALSE;
   3741 	}
   3742     }
   3743 
   3744   /* Check for conflicting Tag_GNU_Power_ABI_Struct_Return attributes
   3745      and merge non-conflicting ones.  */
   3746   in_attr = &in_attrs[Tag_GNU_Power_ABI_Struct_Return];
   3747   out_attr = &out_attrs[Tag_GNU_Power_ABI_Struct_Return];
   3748   if (in_attr->i != out_attr->i)
   3749     {
   3750       int in_struct = in_attr->i & 3;
   3751       int out_struct = out_attr->i & 3;
   3752       static bfd *last_struct;
   3753 
   3754       if (in_struct == 0 || in_struct == 3)
   3755        ;
   3756       else if (out_struct == 0)
   3757 	{
   3758 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL;
   3759 	  out_attr->i = in_struct;
   3760 	  last_struct = ibfd;
   3761 	}
   3762       else if (out_struct < in_struct)
   3763 	{
   3764 	  _bfd_error_handler
   3765 	    /* xgettext:c-format */
   3766 	    (_("%pB uses r3/r4 for small structure returns, "
   3767 	       "%pB uses memory"), last_struct, ibfd);
   3768 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3769 	  ret = FALSE;
   3770 	}
   3771       else if (out_struct > in_struct)
   3772 	{
   3773 	  _bfd_error_handler
   3774 	    /* xgettext:c-format */
   3775 	    (_("%pB uses r3/r4 for small structure returns, "
   3776 	       "%pB uses memory"), ibfd, last_struct);
   3777 	  out_attr->type = ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_ERROR;
   3778 	  ret = FALSE;
   3779 	}
   3780     }
   3781   if (!ret)
   3782     {
   3783       bfd_set_error (bfd_error_bad_value);
   3784       return FALSE;
   3785     }
   3786 
   3787   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   3788   return _bfd_elf_merge_object_attributes (ibfd, info);
   3789 }
   3790 
   3791 /* Merge backend specific data from an object file to the output
   3792    object file when linking.  */
   3793 
   3794 static bfd_boolean
   3795 ppc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
   3796 {
   3797   bfd *obfd = info->output_bfd;
   3798   flagword old_flags;
   3799   flagword new_flags;
   3800   bfd_boolean error;
   3801 
   3802   if (!is_ppc_elf (ibfd) || !is_ppc_elf (obfd))
   3803     return TRUE;
   3804 
   3805   /* Check if we have the same endianness.  */
   3806   if (! _bfd_generic_verify_endian_match (ibfd, info))
   3807     return FALSE;
   3808 
   3809   if (!ppc_elf_merge_obj_attributes (ibfd, info))
   3810     return FALSE;
   3811 
   3812   if ((ibfd->flags & DYNAMIC) != 0)
   3813     return TRUE;
   3814 
   3815   new_flags = elf_elfheader (ibfd)->e_flags;
   3816   old_flags = elf_elfheader (obfd)->e_flags;
   3817   if (!elf_flags_init (obfd))
   3818     {
   3819       /* First call, no flags set.  */
   3820       elf_flags_init (obfd) = TRUE;
   3821       elf_elfheader (obfd)->e_flags = new_flags;
   3822     }
   3823 
   3824   /* Compatible flags are ok.  */
   3825   else if (new_flags == old_flags)
   3826     ;
   3827 
   3828   /* Incompatible flags.  */
   3829   else
   3830     {
   3831       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
   3832 	 to be linked with either.  */
   3833       error = FALSE;
   3834       if ((new_flags & EF_PPC_RELOCATABLE) != 0
   3835 	  && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
   3836 	{
   3837 	  error = TRUE;
   3838 	  _bfd_error_handler
   3839 	    (_("%pB: compiled with -mrelocatable and linked with "
   3840 	       "modules compiled normally"), ibfd);
   3841 	}
   3842       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
   3843 	       && (old_flags & EF_PPC_RELOCATABLE) != 0)
   3844 	{
   3845 	  error = TRUE;
   3846 	  _bfd_error_handler
   3847 	    (_("%pB: compiled normally and linked with "
   3848 	       "modules compiled with -mrelocatable"), ibfd);
   3849 	}
   3850 
   3851       /* The output is -mrelocatable-lib iff both the input files are.  */
   3852       if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
   3853 	elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
   3854 
   3855       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
   3856 	 but each input file is either -mrelocatable or -mrelocatable-lib.  */
   3857       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
   3858 	  && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
   3859 	  && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
   3860 	elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
   3861 
   3862       /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
   3863 	 any module uses it.  */
   3864       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
   3865 
   3866       new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
   3867       old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
   3868 
   3869       /* Warn about any other mismatches.  */
   3870       if (new_flags != old_flags)
   3871 	{
   3872 	  error = TRUE;
   3873 	  _bfd_error_handler
   3874 	    /* xgettext:c-format */
   3875 	    (_("%pB: uses different e_flags (%#x) fields "
   3876 	       "than previous modules (%#x)"),
   3877 	     ibfd, new_flags, old_flags);
   3878 	}
   3879 
   3880       if (error)
   3881 	{
   3882 	  bfd_set_error (bfd_error_bad_value);
   3883 	  return FALSE;
   3884 	}
   3885     }
   3886 
   3887   return TRUE;
   3888 }
   3889 
   3890 static void
   3891 ppc_elf_vle_split16 (bfd *input_bfd,
   3892 		     asection *input_section,
   3893 		     unsigned long offset,
   3894 		     bfd_byte *loc,
   3895 		     bfd_vma value,
   3896 		     split16_format_type split16_format,
   3897 		     bfd_boolean fixup)
   3898 {
   3899   unsigned int insn, opcode;
   3900 
   3901   insn = bfd_get_32 (input_bfd, loc);
   3902   opcode = insn & E_OPCODE_MASK;
   3903   if (opcode == E_OR2I_INSN
   3904       || opcode == E_AND2I_DOT_INSN
   3905       || opcode == E_OR2IS_INSN
   3906       || opcode == E_LIS_INSN
   3907       || opcode == E_AND2IS_DOT_INSN)
   3908     {
   3909       if (split16_format != split16a_type)
   3910 	{
   3911 	  if (fixup)
   3912 	    split16_format = split16a_type;
   3913 	  else
   3914 	    _bfd_error_handler
   3915 	      /* xgettext:c-format */
   3916 	      (_("%pB(%pA+0x%lx): expected 16A style relocation on 0x%08x insn"),
   3917 	       input_bfd, input_section, offset, opcode);
   3918 	}
   3919     }
   3920   else if (opcode == E_ADD2I_DOT_INSN
   3921 	   || opcode == E_ADD2IS_INSN
   3922 	   || opcode == E_CMP16I_INSN
   3923 	   || opcode == E_MULL2I_INSN
   3924 	   || opcode == E_CMPL16I_INSN
   3925 	   || opcode == E_CMPH16I_INSN
   3926 	   || opcode == E_CMPHL16I_INSN)
   3927     {
   3928       if (split16_format != split16d_type)
   3929 	{
   3930 	  if (fixup)
   3931 	    split16_format = split16d_type;
   3932 	  else
   3933 	    _bfd_error_handler
   3934 	      /* xgettext:c-format */
   3935 	      (_("%pB(%pA+0x%lx): expected 16D style relocation on 0x%08x insn"),
   3936 	       input_bfd, input_section, offset, opcode);
   3937 	}
   3938     }
   3939   if (split16_format == split16a_type)
   3940     {
   3941       insn &= ~((0xf800 << 5) | 0x7ff);
   3942       insn |= (value & 0xf800) << 5;
   3943       if ((insn & E_LI_MASK) == E_LI_INSN)
   3944 	{
   3945 	  /* Hack for e_li.  Extend sign.  */
   3946 	  insn &= ~(0xf0000 >> 5);
   3947 	  insn |= (-(value & 0x8000) & 0xf0000) >> 5;
   3948 	}
   3949     }
   3950   else
   3951     {
   3952       insn &= ~((0xf800 << 10) | 0x7ff);
   3953       insn |= (value & 0xf800) << 10;
   3954     }
   3955   insn |= value & 0x7ff;
   3956   bfd_put_32 (input_bfd, insn, loc);
   3957 }
   3958 
   3959 static void
   3960 ppc_elf_vle_split20 (bfd *output_bfd, bfd_byte *loc, bfd_vma value)
   3961 {
   3962   unsigned int insn;
   3963 
   3964   insn = bfd_get_32 (output_bfd, loc);
   3965   /* We have an li20 field, bits 17..20, 11..15, 21..31.  */
   3966   /* Top 4 bits of value to 17..20.  */
   3967   insn |= (value & 0xf0000) >> 5;
   3968   /* Next 5 bits of the value to 11..15.  */
   3969   insn |= (value & 0xf800) << 5;
   3970   /* And the final 11 bits of the value to bits 21 to 31.  */
   3971   insn |= value & 0x7ff;
   3972   bfd_put_32 (output_bfd, insn, loc);
   3973 }
   3974 
   3975 
   3976 /* Choose which PLT scheme to use, and set .plt flags appropriately.
   3978    Returns -1 on error, 0 for old PLT, 1 for new PLT.  */
   3979 int
   3980 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
   3981 			   struct bfd_link_info *info)
   3982 {
   3983   struct ppc_elf_link_hash_table *htab;
   3984   flagword flags;
   3985 
   3986   htab = ppc_elf_hash_table (info);
   3987 
   3988   if (htab->plt_type == PLT_UNSET)
   3989     {
   3990       struct elf_link_hash_entry *h;
   3991 
   3992       if (htab->params->plt_style == PLT_OLD)
   3993 	htab->plt_type = PLT_OLD;
   3994       else if (bfd_link_pic (info)
   3995 	       && htab->elf.dynamic_sections_created
   3996 	       && (h = elf_link_hash_lookup (&htab->elf, "_mcount",
   3997 					     FALSE, FALSE, TRUE)) != NULL
   3998 	       && (h->type == STT_FUNC
   3999 		   || h->needs_plt)
   4000 	       && h->ref_regular
   4001 	       && !(SYMBOL_CALLS_LOCAL (info, h)
   4002 		    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
   4003 	{
   4004 	  /* Profiling of shared libs (and pies) is not supported with
   4005 	     secure plt, because ppc32 does profiling before a
   4006 	     function prologue and a secure plt pic call stubs needs
   4007 	     r30 to be set up.  */
   4008 	  htab->plt_type = PLT_OLD;
   4009 	}
   4010       else
   4011 	{
   4012 	  bfd *ibfd;
   4013 	  enum ppc_elf_plt_type plt_type = htab->params->plt_style;
   4014 
   4015 	  /* Look through the reloc flags left by ppc_elf_check_relocs.
   4016 	     Use the old style bss plt if a file makes plt calls
   4017 	     without using the new relocs, and if ld isn't given
   4018 	     --secure-plt and we never see REL16 relocs.  */
   4019 	  if (plt_type == PLT_UNSET)
   4020 	    plt_type = PLT_OLD;
   4021 	  for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next)
   4022 	    if (is_ppc_elf (ibfd))
   4023 	      {
   4024 		if (ppc_elf_tdata (ibfd)->has_rel16)
   4025 		  plt_type = PLT_NEW;
   4026 		else if (ppc_elf_tdata (ibfd)->makes_plt_call)
   4027 		  {
   4028 		    plt_type = PLT_OLD;
   4029 		    htab->old_bfd = ibfd;
   4030 		    break;
   4031 		  }
   4032 	      }
   4033 	  htab->plt_type = plt_type;
   4034 	}
   4035     }
   4036   if (htab->plt_type == PLT_OLD && htab->params->plt_style == PLT_NEW)
   4037     {
   4038       if (htab->old_bfd != NULL)
   4039 	_bfd_error_handler (_("bss-plt forced due to %pB"), htab->old_bfd);
   4040       else
   4041 	_bfd_error_handler (_("bss-plt forced by profiling"));
   4042     }
   4043 
   4044   BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
   4045 
   4046   if (htab->plt_type == PLT_NEW)
   4047     {
   4048       flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
   4049 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4050 
   4051       /* The new PLT is a loaded section.  */
   4052       if (htab->elf.splt != NULL
   4053 	  && !bfd_set_section_flags (htab->elf.splt, flags))
   4054 	return -1;
   4055 
   4056       /* The new GOT is not executable.  */
   4057       if (htab->elf.sgot != NULL
   4058 	  && !bfd_set_section_flags (htab->elf.sgot, flags))
   4059 	return -1;
   4060     }
   4061   else
   4062     {
   4063       /* Stop an unused .glink section from affecting .text alignment.  */
   4064       if (htab->glink != NULL
   4065 	  && !bfd_set_section_alignment (htab->glink, 0))
   4066 	return -1;
   4067     }
   4068   return htab->plt_type == PLT_NEW;
   4069 }
   4070 
   4071 /* Return the section that should be marked against GC for a given
   4073    relocation.  */
   4074 
   4075 static asection *
   4076 ppc_elf_gc_mark_hook (asection *sec,
   4077 		      struct bfd_link_info *info,
   4078 		      Elf_Internal_Rela *rel,
   4079 		      struct elf_link_hash_entry *h,
   4080 		      Elf_Internal_Sym *sym)
   4081 {
   4082   if (h != NULL)
   4083     switch (ELF32_R_TYPE (rel->r_info))
   4084       {
   4085       case R_PPC_GNU_VTINHERIT:
   4086       case R_PPC_GNU_VTENTRY:
   4087 	return NULL;
   4088       }
   4089 
   4090   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   4091 }
   4092 
   4093 static bfd_boolean
   4094 get_sym_h (struct elf_link_hash_entry **hp,
   4095 	   Elf_Internal_Sym **symp,
   4096 	   asection **symsecp,
   4097 	   unsigned char **tls_maskp,
   4098 	   Elf_Internal_Sym **locsymsp,
   4099 	   unsigned long r_symndx,
   4100 	   bfd *ibfd)
   4101 {
   4102   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   4103 
   4104   if (r_symndx >= symtab_hdr->sh_info)
   4105     {
   4106       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   4107       struct elf_link_hash_entry *h;
   4108 
   4109       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4110       while (h->root.type == bfd_link_hash_indirect
   4111 	     || h->root.type == bfd_link_hash_warning)
   4112 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4113 
   4114       if (hp != NULL)
   4115 	*hp = h;
   4116 
   4117       if (symp != NULL)
   4118 	*symp = NULL;
   4119 
   4120       if (symsecp != NULL)
   4121 	{
   4122 	  asection *symsec = NULL;
   4123 	  if (h->root.type == bfd_link_hash_defined
   4124 	      || h->root.type == bfd_link_hash_defweak)
   4125 	    symsec = h->root.u.def.section;
   4126 	  *symsecp = symsec;
   4127 	}
   4128 
   4129       if (tls_maskp != NULL)
   4130 	*tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
   4131     }
   4132   else
   4133     {
   4134       Elf_Internal_Sym *sym;
   4135       Elf_Internal_Sym *locsyms = *locsymsp;
   4136 
   4137       if (locsyms == NULL)
   4138 	{
   4139 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   4140 	  if (locsyms == NULL)
   4141 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   4142 					    symtab_hdr->sh_info,
   4143 					    0, NULL, NULL, NULL);
   4144 	  if (locsyms == NULL)
   4145 	    return FALSE;
   4146 	  *locsymsp = locsyms;
   4147 	}
   4148       sym = locsyms + r_symndx;
   4149 
   4150       if (hp != NULL)
   4151 	*hp = NULL;
   4152 
   4153       if (symp != NULL)
   4154 	*symp = sym;
   4155 
   4156       if (symsecp != NULL)
   4157 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   4158 
   4159       if (tls_maskp != NULL)
   4160 	{
   4161 	  bfd_signed_vma *local_got;
   4162 	  unsigned char *tls_mask;
   4163 
   4164 	  tls_mask = NULL;
   4165 	  local_got = elf_local_got_refcounts (ibfd);
   4166 	  if (local_got != NULL)
   4167 	    {
   4168 	      struct plt_entry **local_plt = (struct plt_entry **)
   4169 		(local_got + symtab_hdr->sh_info);
   4170 	      unsigned char *lgot_masks = (unsigned char *)
   4171 		(local_plt + symtab_hdr->sh_info);
   4172 	      tls_mask = &lgot_masks[r_symndx];
   4173 	    }
   4174 	  *tls_maskp = tls_mask;
   4175 	}
   4176     }
   4177   return TRUE;
   4178 }
   4179 
   4180 /* Analyze inline PLT call relocations to see whether calls to locally
   4182    defined functions can be converted to direct calls.  */
   4183 
   4184 bfd_boolean
   4185 ppc_elf_inline_plt (struct bfd_link_info *info)
   4186 {
   4187   struct ppc_elf_link_hash_table *htab;
   4188   bfd *ibfd;
   4189   asection *sec;
   4190   bfd_vma low_vma, high_vma, limit;
   4191 
   4192   htab = ppc_elf_hash_table (info);
   4193   if (htab == NULL)
   4194     return FALSE;
   4195 
   4196   /* A bl insn can reach -0x2000000 to 0x1fffffc.  The limit is
   4197      reduced somewhat to cater for possible stubs that might be added
   4198      between the call and its destination.  */
   4199   limit = 0x1e00000;
   4200   low_vma = -1;
   4201   high_vma = 0;
   4202   for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
   4203     if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
   4204       {
   4205 	if (low_vma > sec->vma)
   4206 	  low_vma = sec->vma;
   4207 	if (high_vma < sec->vma + sec->size)
   4208 	  high_vma = sec->vma + sec->size;
   4209       }
   4210 
   4211   /* If a "bl" can reach anywhere in local code sections, then we can
   4212      convert all inline PLT sequences to direct calls when the symbol
   4213      is local.  */
   4214   if (high_vma - low_vma < limit)
   4215     {
   4216       htab->can_convert_all_inline_plt = 1;
   4217       return TRUE;
   4218     }
   4219 
   4220   /* Otherwise, go looking through relocs for cases where a direct
   4221      call won't reach.  Mark the symbol on any such reloc to disable
   4222      the optimization and keep the PLT entry as it seems likely that
   4223      this will be better than creating trampolines.  Note that this
   4224      will disable the optimization for all inline PLT calls to a
   4225      particular symbol, not just those that won't reach.  The
   4226      difficulty in doing a more precise optimization is that the
   4227      linker needs to make a decision depending on whether a
   4228      particular R_PPC_PLTCALL insn can be turned into a direct
   4229      call, for each of the R_PPC_PLTSEQ and R_PPC_PLT16* insns in
   4230      the sequence, and there is nothing that ties those relocs
   4231      together except their symbol.  */
   4232 
   4233   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   4234     {
   4235       Elf_Internal_Shdr *symtab_hdr;
   4236       Elf_Internal_Sym *local_syms;
   4237 
   4238       if (!is_ppc_elf (ibfd))
   4239 	continue;
   4240 
   4241       local_syms = NULL;
   4242       symtab_hdr = &elf_symtab_hdr (ibfd);
   4243 
   4244       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   4245 	if (sec->has_pltcall
   4246 	    && !bfd_is_abs_section (sec->output_section))
   4247 	  {
   4248 	    Elf_Internal_Rela *relstart, *rel, *relend;
   4249 
   4250 	    /* Read the relocations.  */
   4251 	    relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   4252 						  info->keep_memory);
   4253 	    if (relstart == NULL)
   4254 	      return FALSE;
   4255 
   4256 	    relend = relstart + sec->reloc_count;
   4257 	    for (rel = relstart; rel < relend; )
   4258 	      {
   4259 		enum elf_ppc_reloc_type r_type;
   4260 		unsigned long r_symndx;
   4261 		asection *sym_sec;
   4262 		struct elf_link_hash_entry *h;
   4263 		Elf_Internal_Sym *sym;
   4264 		unsigned char *tls_maskp;
   4265 
   4266 		r_type = ELF32_R_TYPE (rel->r_info);
   4267 		if (r_type != R_PPC_PLTCALL)
   4268 		  continue;
   4269 
   4270 		r_symndx = ELF32_R_SYM (rel->r_info);
   4271 		if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
   4272 				r_symndx, ibfd))
   4273 		  {
   4274 		    if (elf_section_data (sec)->relocs != relstart)
   4275 		      free (relstart);
   4276 		    if (symtab_hdr->contents != (unsigned char *) local_syms)
   4277 		      free (local_syms);
   4278 		    return FALSE;
   4279 		  }
   4280 
   4281 		if (sym_sec != NULL && sym_sec->output_section != NULL)
   4282 		  {
   4283 		    bfd_vma from, to;
   4284 		    if (h != NULL)
   4285 		      to = h->root.u.def.value;
   4286 		    else
   4287 		      to = sym->st_value;
   4288 		    to += (rel->r_addend
   4289 			   + sym_sec->output_offset
   4290 			   + sym_sec->output_section->vma);
   4291 		    from = (rel->r_offset
   4292 			    + sec->output_offset
   4293 			    + sec->output_section->vma);
   4294 		    if (to - from + limit < 2 * limit)
   4295 		      *tls_maskp &= ~PLT_KEEP;
   4296 		  }
   4297 	      }
   4298 	    if (elf_section_data (sec)->relocs != relstart)
   4299 	      free (relstart);
   4300 	  }
   4301 
   4302       if (local_syms != NULL
   4303 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   4304 	{
   4305 	  if (!info->keep_memory)
   4306 	    free (local_syms);
   4307 	  else
   4308 	    symtab_hdr->contents = (unsigned char *) local_syms;
   4309 	}
   4310     }
   4311 
   4312   return TRUE;
   4313 }
   4314 
   4315 /* Set plt output section type, htab->tls_get_addr, and call the
   4316    generic ELF tls_setup function.  */
   4317 
   4318 asection *
   4319 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   4320 {
   4321   struct ppc_elf_link_hash_table *htab;
   4322 
   4323   htab = ppc_elf_hash_table (info);
   4324   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   4325 					     FALSE, FALSE, TRUE);
   4326   if (htab->plt_type != PLT_NEW)
   4327     htab->params->no_tls_get_addr_opt = TRUE;
   4328 
   4329   if (!htab->params->no_tls_get_addr_opt)
   4330     {
   4331       struct elf_link_hash_entry *opt, *tga;
   4332       opt = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   4333 				  FALSE, FALSE, TRUE);
   4334       if (opt != NULL
   4335 	  && (opt->root.type == bfd_link_hash_defined
   4336 	      || opt->root.type == bfd_link_hash_defweak))
   4337 	{
   4338 	  /* If glibc supports an optimized __tls_get_addr call stub,
   4339 	     signalled by the presence of __tls_get_addr_opt, and we'll
   4340 	     be calling __tls_get_addr via a plt call stub, then
   4341 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   4342 	  tga = htab->tls_get_addr;
   4343 	  if (htab->elf.dynamic_sections_created
   4344 	      && tga != NULL
   4345 	      && (tga->type == STT_FUNC
   4346 		  || tga->needs_plt)
   4347 	      && !(SYMBOL_CALLS_LOCAL (info, tga)
   4348 		   || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga)))
   4349 	    {
   4350 	      struct plt_entry *ent;
   4351 	      for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
   4352 		if (ent->plt.refcount > 0)
   4353 		  break;
   4354 	      if (ent != NULL)
   4355 		{
   4356 		  tga->root.type = bfd_link_hash_indirect;
   4357 		  tga->root.u.i.link = &opt->root;
   4358 		  ppc_elf_copy_indirect_symbol (info, opt, tga);
   4359 		  opt->mark = 1;
   4360 		  if (opt->dynindx != -1)
   4361 		    {
   4362 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   4363 		      opt->dynindx = -1;
   4364 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4365 					      opt->dynstr_index);
   4366 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt))
   4367 			return FALSE;
   4368 		    }
   4369 		  htab->tls_get_addr = opt;
   4370 		}
   4371 	    }
   4372 	}
   4373       else
   4374 	htab->params->no_tls_get_addr_opt = TRUE;
   4375     }
   4376   if (htab->plt_type == PLT_NEW
   4377       && htab->elf.splt != NULL
   4378       && htab->elf.splt->output_section != NULL)
   4379     {
   4380       elf_section_type (htab->elf.splt->output_section) = SHT_PROGBITS;
   4381       elf_section_flags (htab->elf.splt->output_section) = SHF_ALLOC + SHF_WRITE;
   4382     }
   4383 
   4384   return _bfd_elf_tls_setup (obfd, info);
   4385 }
   4386 
   4387 /* Return TRUE iff REL is a branch reloc with a global symbol matching
   4388    HASH.  */
   4389 
   4390 static bfd_boolean
   4391 branch_reloc_hash_match (const bfd *ibfd,
   4392 			 const Elf_Internal_Rela *rel,
   4393 			 const struct elf_link_hash_entry *hash)
   4394 {
   4395   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   4396   enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
   4397   unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
   4398 
   4399   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   4400     {
   4401       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   4402       struct elf_link_hash_entry *h;
   4403 
   4404       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4405       while (h->root.type == bfd_link_hash_indirect
   4406 	     || h->root.type == bfd_link_hash_warning)
   4407 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4408       if (h == hash)
   4409 	return TRUE;
   4410     }
   4411   return FALSE;
   4412 }
   4413 
   4414 /* Run through all the TLS relocs looking for optimization
   4415    opportunities.  */
   4416 
   4417 bfd_boolean
   4418 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
   4419 		      struct bfd_link_info *info)
   4420 {
   4421   bfd *ibfd;
   4422   asection *sec;
   4423   struct ppc_elf_link_hash_table *htab;
   4424   int pass;
   4425 
   4426   if (!bfd_link_executable (info))
   4427     return TRUE;
   4428 
   4429   htab = ppc_elf_hash_table (info);
   4430   if (htab == NULL)
   4431     return FALSE;
   4432 
   4433   htab->do_tls_opt = 1;
   4434 
   4435   /* Make two passes through the relocs.  First time check that tls
   4436      relocs involved in setting up a tls_get_addr call are indeed
   4437      followed by such a call.  If they are not, don't do any tls
   4438      optimization.  On the second pass twiddle tls_mask flags to
   4439      notify relocate_section that optimization can be done, and
   4440      adjust got and plt refcounts.  */
   4441   for (pass = 0; pass < 2; ++pass)
   4442     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   4443       {
   4444 	Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   4445 	asection *got2 = bfd_get_section_by_name (ibfd, ".got2");
   4446 
   4447 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   4448 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   4449 	    {
   4450 	      Elf_Internal_Rela *relstart, *rel, *relend;
   4451 	      int expecting_tls_get_addr = 0;
   4452 
   4453 	      /* Read the relocations.  */
   4454 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   4455 						    info->keep_memory);
   4456 	      if (relstart == NULL)
   4457 		return FALSE;
   4458 
   4459 	      relend = relstart + sec->reloc_count;
   4460 	      for (rel = relstart; rel < relend; rel++)
   4461 		{
   4462 		  enum elf_ppc_reloc_type r_type;
   4463 		  unsigned long r_symndx;
   4464 		  struct elf_link_hash_entry *h = NULL;
   4465 		  unsigned char *tls_mask;
   4466 		  unsigned char tls_set, tls_clear;
   4467 		  bfd_boolean is_local;
   4468 		  bfd_signed_vma *got_count;
   4469 
   4470 		  r_symndx = ELF32_R_SYM (rel->r_info);
   4471 		  if (r_symndx >= symtab_hdr->sh_info)
   4472 		    {
   4473 		      struct elf_link_hash_entry **sym_hashes;
   4474 
   4475 		      sym_hashes = elf_sym_hashes (ibfd);
   4476 		      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4477 		      while (h->root.type == bfd_link_hash_indirect
   4478 			     || h->root.type == bfd_link_hash_warning)
   4479 			h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4480 		    }
   4481 
   4482 		  is_local = SYMBOL_REFERENCES_LOCAL (info, h);
   4483 		  r_type = ELF32_R_TYPE (rel->r_info);
   4484 		  /* If this section has old-style __tls_get_addr calls
   4485 		     without marker relocs, then check that each
   4486 		     __tls_get_addr call reloc is preceded by a reloc
   4487 		     that conceivably belongs to the __tls_get_addr arg
   4488 		     setup insn.  If we don't find matching arg setup
   4489 		     relocs, don't do any tls optimization.  */
   4490 		  if (pass == 0
   4491 		      && sec->nomark_tls_get_addr
   4492 		      && h != NULL
   4493 		      && h == htab->tls_get_addr
   4494 		      && !expecting_tls_get_addr
   4495 		      && is_branch_reloc (r_type))
   4496 		    {
   4497 		      info->callbacks->minfo ("%H __tls_get_addr lost arg, "
   4498 					      "TLS optimization disabled\n",
   4499 					      ibfd, sec, rel->r_offset);
   4500 		      if (elf_section_data (sec)->relocs != relstart)
   4501 			free (relstart);
   4502 		      return TRUE;
   4503 		    }
   4504 
   4505 		  expecting_tls_get_addr = 0;
   4506 		  switch (r_type)
   4507 		    {
   4508 		    case R_PPC_GOT_TLSLD16:
   4509 		    case R_PPC_GOT_TLSLD16_LO:
   4510 		      expecting_tls_get_addr = 1;
   4511 		      /* Fall through.  */
   4512 
   4513 		    case R_PPC_GOT_TLSLD16_HI:
   4514 		    case R_PPC_GOT_TLSLD16_HA:
   4515 		      /* These relocs should never be against a symbol
   4516 			 defined in a shared lib.  Leave them alone if
   4517 			 that turns out to be the case.  */
   4518 		      if (!is_local)
   4519 			continue;
   4520 
   4521 		      /* LD -> LE */
   4522 		      tls_set = 0;
   4523 		      tls_clear = TLS_LD;
   4524 		      break;
   4525 
   4526 		    case R_PPC_GOT_TLSGD16:
   4527 		    case R_PPC_GOT_TLSGD16_LO:
   4528 		      expecting_tls_get_addr = 1;
   4529 		      /* Fall through.  */
   4530 
   4531 		    case R_PPC_GOT_TLSGD16_HI:
   4532 		    case R_PPC_GOT_TLSGD16_HA:
   4533 		      if (is_local)
   4534 			/* GD -> LE */
   4535 			tls_set = 0;
   4536 		      else
   4537 			/* GD -> IE */
   4538 			tls_set = TLS_TLS | TLS_GDIE;
   4539 		      tls_clear = TLS_GD;
   4540 		      break;
   4541 
   4542 		    case R_PPC_GOT_TPREL16:
   4543 		    case R_PPC_GOT_TPREL16_LO:
   4544 		    case R_PPC_GOT_TPREL16_HI:
   4545 		    case R_PPC_GOT_TPREL16_HA:
   4546 		      if (is_local)
   4547 			{
   4548 			  /* IE -> LE */
   4549 			  tls_set = 0;
   4550 			  tls_clear = TLS_TPREL;
   4551 			  break;
   4552 			}
   4553 		      else
   4554 			continue;
   4555 
   4556 		    case R_PPC_TLSLD:
   4557 		      if (!is_local)
   4558 			continue;
   4559 		      /* Fall through.  */
   4560 		    case R_PPC_TLSGD:
   4561 		      if (rel + 1 < relend
   4562 			  && is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
   4563 			{
   4564 			  if (pass != 0
   4565 			      && ELF32_R_TYPE (rel[1].r_info) != R_PPC_PLTSEQ)
   4566 			    {
   4567 			      r_type = ELF32_R_TYPE (rel[1].r_info);
   4568 			      r_symndx = ELF32_R_SYM (rel[1].r_info);
   4569 			      if (r_symndx >= symtab_hdr->sh_info)
   4570 				{
   4571 				  struct elf_link_hash_entry **sym_hashes;
   4572 
   4573 				  sym_hashes = elf_sym_hashes (ibfd);
   4574 				  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   4575 				  while (h->root.type == bfd_link_hash_indirect
   4576 					 || h->root.type == bfd_link_hash_warning)
   4577 				    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   4578 				  if (h != NULL)
   4579 				    {
   4580 				      struct plt_entry *ent = NULL;
   4581 				      bfd_vma addend = 0;
   4582 
   4583 				      if (bfd_link_pic (info))
   4584 					addend = rel->r_addend;
   4585 				      ent = find_plt_ent (&h->plt.plist,
   4586 							  got2, addend);
   4587 				      if (ent != NULL
   4588 					  && ent->plt.refcount > 0)
   4589 					ent->plt.refcount -= 1;
   4590 				    }
   4591 				}
   4592 			    }
   4593 			  continue;
   4594 			}
   4595 		      expecting_tls_get_addr = 2;
   4596 		      tls_set = 0;
   4597 		      tls_clear = 0;
   4598 		      break;
   4599 
   4600 		    case R_PPC_TPREL16_HA:
   4601 		      if (pass == 0)
   4602 			{
   4603 			  unsigned char buf[4];
   4604 			  unsigned int insn;
   4605 			  bfd_vma off = rel->r_offset & ~3;
   4606 			  if (!bfd_get_section_contents (ibfd, sec, buf,
   4607 							 off, 4))
   4608 			    {
   4609 			      if (elf_section_data (sec)->relocs != relstart)
   4610 				free (relstart);
   4611 			      return FALSE;
   4612 			    }
   4613 			  insn = bfd_get_32 (ibfd, buf);
   4614 			  /* addis rt,2,imm */
   4615 			  if ((insn & ((0x3fu << 26) | 0x1f << 16))
   4616 			      != ((15u << 26) | (2 << 16)))
   4617 			    {
   4618 			      /* xgettext:c-format */
   4619 			      info->callbacks->minfo
   4620 				(_("%H: warning: %s unexpected insn %#x.\n"),
   4621 				 ibfd, sec, off, "R_PPC_TPREL16_HA", insn);
   4622 			      htab->do_tls_opt = 0;
   4623 			    }
   4624 			}
   4625 		      continue;
   4626 
   4627 		    case R_PPC_TPREL16_HI:
   4628 		      htab->do_tls_opt = 0;
   4629 		      continue;
   4630 
   4631 		    default:
   4632 		      continue;
   4633 		    }
   4634 
   4635 		  if (pass == 0)
   4636 		    {
   4637 		      if (!expecting_tls_get_addr
   4638 			  || !sec->nomark_tls_get_addr)
   4639 			continue;
   4640 
   4641 		      if (rel + 1 < relend
   4642 			  && branch_reloc_hash_match (ibfd, rel + 1,
   4643 						      htab->tls_get_addr))
   4644 			continue;
   4645 
   4646 		      /* Uh oh, we didn't find the expected call.  We
   4647 			 could just mark this symbol to exclude it
   4648 			 from tls optimization but it's safer to skip
   4649 			 the entire optimization.  */
   4650 		      info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
   4651 						"TLS optimization disabled\n"),
   4652 					      ibfd, sec, rel->r_offset);
   4653 		      if (elf_section_data (sec)->relocs != relstart)
   4654 			free (relstart);
   4655 		      return TRUE;
   4656 		    }
   4657 
   4658 		  if (h != NULL)
   4659 		    {
   4660 		      tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
   4661 		      got_count = &h->got.refcount;
   4662 		    }
   4663 		  else
   4664 		    {
   4665 		      bfd_signed_vma *lgot_refs;
   4666 		      struct plt_entry **local_plt;
   4667 		      unsigned char *lgot_masks;
   4668 
   4669 		      lgot_refs = elf_local_got_refcounts (ibfd);
   4670 		      if (lgot_refs == NULL)
   4671 			abort ();
   4672 		      local_plt = (struct plt_entry **)
   4673 			(lgot_refs + symtab_hdr->sh_info);
   4674 		      lgot_masks = (unsigned char *)
   4675 			(local_plt + symtab_hdr->sh_info);
   4676 		      tls_mask = &lgot_masks[r_symndx];
   4677 		      got_count = &lgot_refs[r_symndx];
   4678 		    }
   4679 
   4680 		  /* If we don't have old-style __tls_get_addr calls
   4681 		     without TLSGD/TLSLD marker relocs, and we haven't
   4682 		     found a new-style __tls_get_addr call with a
   4683 		     marker for this symbol, then we either have a
   4684 		     broken object file or an -mlongcall style
   4685 		     indirect call to __tls_get_addr without a marker.
   4686 		     Disable optimization in this case.  */
   4687 		  if ((tls_clear & (TLS_GD | TLS_LD)) != 0
   4688 		      && !sec->nomark_tls_get_addr
   4689 		      && ((*tls_mask & (TLS_TLS | TLS_MARK))
   4690 			  != (TLS_TLS | TLS_MARK)))
   4691 		    continue;
   4692 
   4693 		  if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
   4694 		    {
   4695 		      struct plt_entry *ent;
   4696 		      bfd_vma addend = 0;
   4697 
   4698 		      if (bfd_link_pic (info)
   4699 			  && (ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTREL24
   4700 			      || ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTCALL))
   4701 			addend = rel[1].r_addend;
   4702 		      ent = find_plt_ent (&htab->tls_get_addr->plt.plist,
   4703 					  got2, addend);
   4704 		      if (ent != NULL && ent->plt.refcount > 0)
   4705 			ent->plt.refcount -= 1;
   4706 		    }
   4707 		  if (tls_clear == 0)
   4708 		    continue;
   4709 
   4710 		  if (tls_set == 0)
   4711 		    {
   4712 		      /* We managed to get rid of a got entry.  */
   4713 		      if (*got_count > 0)
   4714 			*got_count -= 1;
   4715 		    }
   4716 
   4717 		  *tls_mask |= tls_set;
   4718 		  *tls_mask &= ~tls_clear;
   4719 		}
   4720 
   4721 	      if (elf_section_data (sec)->relocs != relstart)
   4722 		free (relstart);
   4723 	    }
   4724       }
   4725   return TRUE;
   4726 }
   4727 
   4728 /* Return true if we have dynamic relocs against H or any of its weak
   4730    aliases, that apply to read-only sections.  Cannot be used after
   4731    size_dynamic_sections.  */
   4732 
   4733 static bfd_boolean
   4734 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
   4735 {
   4736   struct ppc_elf_link_hash_entry *eh = ppc_elf_hash_entry (h);
   4737   do
   4738     {
   4739       if (_bfd_elf_readonly_dynrelocs (&eh->elf))
   4740 	return TRUE;
   4741       eh = ppc_elf_hash_entry (eh->elf.u.alias);
   4742     } while (eh != NULL && &eh->elf != h);
   4743 
   4744   return FALSE;
   4745 }
   4746 
   4747 /* Return whether H has pc-relative dynamic relocs.  */
   4748 
   4749 static bfd_boolean
   4750 pc_dynrelocs (struct elf_link_hash_entry *h)
   4751 {
   4752   struct elf_dyn_relocs *p;
   4753 
   4754   for (p = h->dyn_relocs; p != NULL; p = p->next)
   4755     if (p->pc_count != 0)
   4756       return TRUE;
   4757   return FALSE;
   4758 }
   4759 
   4760 /* Adjust a symbol defined by a dynamic object and referenced by a
   4761    regular object.  The current definition is in some section of the
   4762    dynamic object, but we're not including those sections.  We have to
   4763    change the definition to something the rest of the link can
   4764    understand.  */
   4765 
   4766 static bfd_boolean
   4767 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   4768 			       struct elf_link_hash_entry *h)
   4769 {
   4770   struct ppc_elf_link_hash_table *htab;
   4771   asection *s;
   4772 
   4773 #ifdef DEBUG
   4774   fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
   4775 	   h->root.root.string);
   4776 #endif
   4777 
   4778   /* Make sure we know what is going on here.  */
   4779   htab = ppc_elf_hash_table (info);
   4780   BFD_ASSERT (htab->elf.dynobj != NULL
   4781 	      && (h->needs_plt
   4782 		  || h->type == STT_GNU_IFUNC
   4783 		  || h->is_weakalias
   4784 		  || (h->def_dynamic
   4785 		      && h->ref_regular
   4786 		      && !h->def_regular)));
   4787 
   4788   /* Deal with function syms.  */
   4789   if (h->type == STT_FUNC
   4790       || h->type == STT_GNU_IFUNC
   4791       || h->needs_plt)
   4792     {
   4793       bfd_boolean local = (SYMBOL_CALLS_LOCAL (info, h)
   4794 			   || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
   4795       /* Discard dyn_relocs when non-pic if we've decided that a
   4796 	 function symbol is local.  */
   4797       if (!bfd_link_pic (info) && local)
   4798 	h->dyn_relocs = NULL;
   4799 
   4800       /* Clear procedure linkage table information for any symbol that
   4801 	 won't need a .plt entry.  */
   4802       struct plt_entry *ent;
   4803       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   4804 	if (ent->plt.refcount > 0)
   4805 	  break;
   4806       if (ent == NULL
   4807 	  || (h->type != STT_GNU_IFUNC
   4808 	      && local
   4809 	      && (htab->can_convert_all_inline_plt
   4810 		  || (ppc_elf_hash_entry (h)->tls_mask
   4811 		      & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
   4812 	{
   4813 	  /* A PLT entry is not required/allowed when:
   4814 
   4815 	     1. We are not using ld.so; because then the PLT entry
   4816 	     can't be set up, so we can't use one.  In this case,
   4817 	     ppc_elf_adjust_dynamic_symbol won't even be called.
   4818 
   4819 	     2. GC has rendered the entry unused.
   4820 
   4821 	     3. We know for certain that a call to this symbol
   4822 	     will go to this object, or will remain undefined.  */
   4823 	  h->plt.plist = NULL;
   4824 	  h->needs_plt = 0;
   4825 	  h->pointer_equality_needed = 0;
   4826 	}
   4827       else
   4828 	{
   4829 	  /* Taking a function's address in a read/write section
   4830 	     doesn't require us to define the function symbol in the
   4831 	     executable on a plt call stub.  A dynamic reloc can
   4832 	     be used instead, giving better runtime performance.
   4833 	     (Calls via that function pointer don't need to bounce
   4834 	     through the plt call stub.)  Similarly, use a dynamic
   4835 	     reloc for a weak reference when possible, allowing the
   4836 	     resolution of the symbol to be set at load time rather
   4837 	     than link time.  */
   4838 	  if ((h->pointer_equality_needed
   4839 	       || (h->non_got_ref
   4840 		   && !h->ref_regular_nonweak
   4841 		   && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
   4842 	      && htab->elf.target_os != is_vxworks
   4843 	      && !ppc_elf_hash_entry (h)->has_sda_refs
   4844 	      && !_bfd_elf_readonly_dynrelocs (h))
   4845 	    {
   4846 	      h->pointer_equality_needed = 0;
   4847 	      /* If we haven't seen a branch reloc and the symbol
   4848 		 isn't an ifunc then we don't need a plt entry.  */
   4849 	      if (!h->needs_plt && h->type != STT_GNU_IFUNC)
   4850 		h->plt.plist = NULL;
   4851 	    }
   4852 	  else if (!bfd_link_pic (info))
   4853 	    /* We are going to be defining the function symbol on the
   4854 	       plt stub, so no dyn_relocs needed when non-pic.  */
   4855 	    h->dyn_relocs = NULL;
   4856 	}
   4857       h->protected_def = 0;
   4858       /* Function symbols can't have copy relocs.  */
   4859       return TRUE;
   4860     }
   4861   else
   4862     h->plt.plist = NULL;
   4863 
   4864   /* If this is a weak symbol, and there is a real definition, the
   4865      processor independent code will have arranged for us to see the
   4866      real definition first, and we can just use the same value.  */
   4867   if (h->is_weakalias)
   4868     {
   4869       struct elf_link_hash_entry *def = weakdef (h);
   4870       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
   4871       h->root.u.def.section = def->root.u.def.section;
   4872       h->root.u.def.value = def->root.u.def.value;
   4873       if (def->root.u.def.section == htab->elf.sdynbss
   4874 	  || def->root.u.def.section == htab->elf.sdynrelro
   4875 	  || def->root.u.def.section == htab->dynsbss)
   4876 	h->dyn_relocs = NULL;
   4877       return TRUE;
   4878     }
   4879 
   4880   /* This is a reference to a symbol defined by a dynamic object which
   4881      is not a function.  */
   4882 
   4883   /* If we are creating a shared library, we must presume that the
   4884      only references to the symbol are via the global offset table.
   4885      For such cases we need not do anything here; the relocations will
   4886      be handled correctly by relocate_section.  */
   4887   if (bfd_link_pic (info))
   4888     {
   4889       h->protected_def = 0;
   4890       return TRUE;
   4891     }
   4892 
   4893   /* If there are no references to this symbol that do not use the
   4894      GOT, we don't need to generate a copy reloc.  */
   4895   if (!h->non_got_ref)
   4896     {
   4897       h->protected_def = 0;
   4898       return TRUE;
   4899     }
   4900 
   4901   /* Protected variables do not work with .dynbss.  The copy in
   4902      .dynbss won't be used by the shared library with the protected
   4903      definition for the variable.  Editing to PIC, or text relocations
   4904      are preferable to an incorrect program.  */
   4905   if (h->protected_def)
   4906     {
   4907       if (ELIMINATE_COPY_RELOCS
   4908 	  && ppc_elf_hash_entry (h)->has_addr16_ha
   4909 	  && ppc_elf_hash_entry (h)->has_addr16_lo
   4910 	  && htab->params->pic_fixup == 0
   4911 	  && info->disable_target_specific_optimizations <= 1)
   4912 	htab->params->pic_fixup = 1;
   4913       return TRUE;
   4914     }
   4915 
   4916   /* If -z nocopyreloc was given, we won't generate them either.  */
   4917   if (info->nocopyreloc)
   4918     return TRUE;
   4919 
   4920    /* If we don't find any dynamic relocs in read-only sections, then
   4921       we'll be keeping the dynamic relocs and avoiding the copy reloc.
   4922       We can't do this if there are any small data relocations.  This
   4923       doesn't work on VxWorks, where we can not have dynamic
   4924       relocations (other than copy and jump slot relocations) in an
   4925       executable.  */
   4926   if (ELIMINATE_COPY_RELOCS
   4927       && !ppc_elf_hash_entry (h)->has_sda_refs
   4928       && htab->elf.target_os != is_vxworks
   4929       && !h->def_regular
   4930       && !alias_readonly_dynrelocs (h))
   4931     return TRUE;
   4932 
   4933   /* We must allocate the symbol in our .dynbss section, which will
   4934      become part of the .bss section of the executable.  There will be
   4935      an entry for this symbol in the .dynsym section.  The dynamic
   4936      object will contain position independent code, so all references
   4937      from the dynamic object to this symbol will go through the global
   4938      offset table.  The dynamic linker will use the .dynsym entry to
   4939      determine the address it must put in the global offset table, so
   4940      both the dynamic object and the regular object will refer to the
   4941      same memory location for the variable.
   4942 
   4943      Of course, if the symbol is referenced using SDAREL relocs, we
   4944      must instead allocate it in .sbss.  */
   4945   if (ppc_elf_hash_entry (h)->has_sda_refs)
   4946     s = htab->dynsbss;
   4947   else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   4948     s = htab->elf.sdynrelro;
   4949   else
   4950     s = htab->elf.sdynbss;
   4951   BFD_ASSERT (s != NULL);
   4952 
   4953   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   4954     {
   4955       asection *srel;
   4956 
   4957       /* We must generate a R_PPC_COPY reloc to tell the dynamic
   4958 	 linker to copy the initial value out of the dynamic object
   4959 	 and into the runtime process image.  */
   4960       if (ppc_elf_hash_entry (h)->has_sda_refs)
   4961 	srel = htab->relsbss;
   4962       else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
   4963 	srel = htab->elf.sreldynrelro;
   4964       else
   4965 	srel = htab->elf.srelbss;
   4966       BFD_ASSERT (srel != NULL);
   4967       srel->size += sizeof (Elf32_External_Rela);
   4968       h->needs_copy = 1;
   4969     }
   4970 
   4971   /* We no longer want dyn_relocs.  */
   4972   h->dyn_relocs = NULL;
   4973   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   4974 }
   4975 
   4976 /* Generate a symbol to mark plt call stubs.  For non-PIC code the sym is
   4978    xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
   4979    specifying the addend on the plt relocation.  For -fpic code, the sym
   4980    is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
   4981    xxxxxxxx.got2.plt_pic32.<callee>.  */
   4982 
   4983 static bfd_boolean
   4984 add_stub_sym (struct plt_entry *ent,
   4985 	      struct elf_link_hash_entry *h,
   4986 	      struct bfd_link_info *info)
   4987 {
   4988   struct elf_link_hash_entry *sh;
   4989   size_t len1, len2, len3;
   4990   char *name;
   4991   const char *stub;
   4992   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   4993 
   4994   if (bfd_link_pic (info))
   4995     stub = ".plt_pic32.";
   4996   else
   4997     stub = ".plt_call32.";
   4998 
   4999   len1 = strlen (h->root.root.string);
   5000   len2 = strlen (stub);
   5001   len3 = 0;
   5002   if (ent->sec)
   5003     len3 = strlen (ent->sec->name);
   5004   name = bfd_malloc (len1 + len2 + len3 + 9);
   5005   if (name == NULL)
   5006     return FALSE;
   5007   sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
   5008   if (ent->sec)
   5009     memcpy (name + 8, ent->sec->name, len3);
   5010   memcpy (name + 8 + len3, stub, len2);
   5011   memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
   5012   sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   5013   if (sh == NULL)
   5014     return FALSE;
   5015   if (sh->root.type == bfd_link_hash_new)
   5016     {
   5017       sh->root.type = bfd_link_hash_defined;
   5018       sh->root.u.def.section = htab->glink;
   5019       sh->root.u.def.value = ent->glink_offset;
   5020       sh->ref_regular = 1;
   5021       sh->def_regular = 1;
   5022       sh->ref_regular_nonweak = 1;
   5023       sh->forced_local = 1;
   5024       sh->non_elf = 0;
   5025       sh->root.linker_def = 1;
   5026     }
   5027   return TRUE;
   5028 }
   5029 
   5030 /* Allocate NEED contiguous space in .got, and return the offset.
   5031    Handles allocation of the got header when crossing 32k.  */
   5032 
   5033 static bfd_vma
   5034 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
   5035 {
   5036   bfd_vma where;
   5037   unsigned int max_before_header;
   5038 
   5039   if (htab->plt_type == PLT_VXWORKS)
   5040     {
   5041       where = htab->elf.sgot->size;
   5042       htab->elf.sgot->size += need;
   5043     }
   5044   else
   5045     {
   5046       max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
   5047       if (need <= htab->got_gap)
   5048 	{
   5049 	  where = max_before_header - htab->got_gap;
   5050 	  htab->got_gap -= need;
   5051 	}
   5052       else
   5053 	{
   5054 	  if (htab->elf.sgot->size + need > max_before_header
   5055 	      && htab->elf.sgot->size <= max_before_header)
   5056 	    {
   5057 	      htab->got_gap = max_before_header - htab->elf.sgot->size;
   5058 	      htab->elf.sgot->size = max_before_header + htab->got_header_size;
   5059 	    }
   5060 	  where = htab->elf.sgot->size;
   5061 	  htab->elf.sgot->size += need;
   5062 	}
   5063     }
   5064   return where;
   5065 }
   5066 
   5067 /* Calculate size of GOT entries for symbol given its TLS_MASK.
   5068    TLS_LD is excluded because those go in a special GOT slot.  */
   5069 
   5070 static inline unsigned int
   5071 got_entries_needed (int tls_mask)
   5072 {
   5073   unsigned int need;
   5074   if ((tls_mask & TLS_TLS) == 0)
   5075     need = 4;
   5076   else
   5077     {
   5078       need = 0;
   5079       if ((tls_mask & TLS_GD) != 0)
   5080 	need += 8;
   5081       if ((tls_mask & (TLS_TPREL | TLS_GDIE)) != 0)
   5082 	need += 4;
   5083       if ((tls_mask & TLS_DTPREL) != 0)
   5084 	need += 4;
   5085     }
   5086   return need;
   5087 }
   5088 
   5089 /* If H is undefined, make it dynamic if that makes sense.  */
   5090 
   5091 static bfd_boolean
   5092 ensure_undef_dynamic (struct bfd_link_info *info,
   5093 		      struct elf_link_hash_entry *h)
   5094 {
   5095   struct elf_link_hash_table *htab = elf_hash_table (info);
   5096 
   5097   if (htab->dynamic_sections_created
   5098       && ((info->dynamic_undefined_weak != 0
   5099 	   && h->root.type == bfd_link_hash_undefweak)
   5100 	  || h->root.type == bfd_link_hash_undefined)
   5101       && h->dynindx == -1
   5102       && !h->forced_local
   5103       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
   5104     return bfd_elf_link_record_dynamic_symbol (info, h);
   5105   return TRUE;
   5106 }
   5107 
   5108 /* Allocate space in associated reloc sections for dynamic relocs.  */
   5109 
   5110 static bfd_boolean
   5111 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   5112 {
   5113   struct bfd_link_info *info = inf;
   5114   struct ppc_elf_link_hash_entry *eh;
   5115   struct ppc_elf_link_hash_table *htab;
   5116   struct elf_dyn_relocs *p;
   5117   bfd_boolean dyn;
   5118 
   5119   if (h->root.type == bfd_link_hash_indirect)
   5120     return TRUE;
   5121 
   5122   htab = ppc_elf_hash_table (info);
   5123   eh = (struct ppc_elf_link_hash_entry *) h;
   5124   if (eh->elf.got.refcount > 0
   5125       || (ELIMINATE_COPY_RELOCS
   5126 	  && !eh->elf.def_regular
   5127 	  && eh->elf.protected_def
   5128 	  && eh->has_addr16_ha
   5129 	  && eh->has_addr16_lo
   5130 	  && htab->params->pic_fixup > 0))
   5131     {
   5132       unsigned int need;
   5133 
   5134       /* Make sure this symbol is output as a dynamic symbol.  */
   5135       if (!ensure_undef_dynamic (info, &eh->elf))
   5136 	return FALSE;
   5137 
   5138       need = 0;
   5139       if ((eh->tls_mask & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
   5140 	{
   5141 	  if (SYMBOL_REFERENCES_LOCAL (info, &eh->elf))
   5142 	    /* We'll just use htab->tlsld_got.offset.  This should
   5143 	       always be the case.  It's a little odd if we have
   5144 	       a local dynamic reloc against a non-local symbol.  */
   5145 	    htab->tlsld_got.refcount += 1;
   5146 	  else
   5147 	    need += 8;
   5148 	}
   5149       need += got_entries_needed (eh->tls_mask);
   5150       if (need == 0)
   5151 	eh->elf.got.offset = (bfd_vma) -1;
   5152       else
   5153 	{
   5154 	  eh->elf.got.offset = allocate_got (htab, need);
   5155 	  if (((bfd_link_pic (info)
   5156 		&& !((eh->tls_mask & TLS_TLS) != 0
   5157 		     && bfd_link_executable (info)
   5158 		     && SYMBOL_REFERENCES_LOCAL (info, &eh->elf)))
   5159 	       || (htab->elf.dynamic_sections_created
   5160 		   && eh->elf.dynindx != -1
   5161 		   && !SYMBOL_REFERENCES_LOCAL (info, &eh->elf)))
   5162 	      && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &eh->elf))
   5163 	    {
   5164 	      asection *rsec;
   5165 
   5166 	      need *= sizeof (Elf32_External_Rela) / 4;
   5167 	      if ((eh->tls_mask & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
   5168 		need -= sizeof (Elf32_External_Rela);
   5169 	      rsec = htab->elf.srelgot;
   5170 	      if (eh->elf.type == STT_GNU_IFUNC)
   5171 		rsec = htab->elf.irelplt;
   5172 	      rsec->size += need;
   5173 	    }
   5174 	}
   5175     }
   5176   else
   5177     eh->elf.got.offset = (bfd_vma) -1;
   5178 
   5179   /* If no dynamic sections we can't have dynamic relocs, except for
   5180      IFUNCs which are handled even in static executables.  */
   5181   if (!htab->elf.dynamic_sections_created
   5182       && h->type != STT_GNU_IFUNC)
   5183     h->dyn_relocs = NULL;
   5184 
   5185   /* Discard relocs on undefined symbols that must be local.  */
   5186   else if (h->root.type == bfd_link_hash_undefined
   5187 	   && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   5188     h->dyn_relocs = NULL;
   5189 
   5190   /* Also discard relocs on undefined weak syms with non-default
   5191      visibility, or when dynamic_undefined_weak says so.  */
   5192   else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   5193     h->dyn_relocs = NULL;
   5194 
   5195   if (h->dyn_relocs == NULL)
   5196     ;
   5197 
   5198   /* In the shared -Bsymbolic case, discard space allocated for
   5199      dynamic pc-relative relocs against symbols which turn out to be
   5200      defined in regular objects.  For the normal shared case, discard
   5201      space for relocs that have become local due to symbol visibility
   5202      changes.  */
   5203   else if (bfd_link_pic (info))
   5204     {
   5205       /* Relocs that use pc_count are those that appear on a call insn,
   5206 	 or certain REL relocs (see must_be_dyn_reloc) that can be
   5207 	 generated via assembly.  We want calls to protected symbols to
   5208 	 resolve directly to the function rather than going via the plt.
   5209 	 If people want function pointer comparisons to work as expected
   5210 	 then they should avoid writing weird assembly.  */
   5211       if (SYMBOL_CALLS_LOCAL (info, h))
   5212 	{
   5213 	  struct elf_dyn_relocs **pp;
   5214 
   5215 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
   5216 	    {
   5217 	      p->count -= p->pc_count;
   5218 	      p->pc_count = 0;
   5219 	      if (p->count == 0)
   5220 		*pp = p->next;
   5221 	      else
   5222 		pp = &p->next;
   5223 	    }
   5224 	}
   5225 
   5226       if (htab->elf.target_os == is_vxworks)
   5227 	{
   5228 	  struct elf_dyn_relocs **pp;
   5229 
   5230 	  for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
   5231 	    {
   5232 	      if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
   5233 		*pp = p->next;
   5234 	      else
   5235 		pp = &p->next;
   5236 	    }
   5237 	}
   5238 
   5239       if (h->dyn_relocs != NULL)
   5240 	{
   5241 	  /* Make sure this symbol is output as a dynamic symbol.  */
   5242 	  if (!ensure_undef_dynamic (info, h))
   5243 	    return FALSE;
   5244 	}
   5245     }
   5246   else if (ELIMINATE_COPY_RELOCS)
   5247     {
   5248       /* For the non-pic case, discard space for relocs against
   5249 	 symbols which turn out to need copy relocs or are not
   5250 	 dynamic.  */
   5251       if (h->dynamic_adjusted
   5252 	  && !h->def_regular
   5253 	  && !ELF_COMMON_DEF_P (h)
   5254 	  && !(h->protected_def
   5255 	       && eh->has_addr16_ha
   5256 	       && eh->has_addr16_lo
   5257 	       && htab->params->pic_fixup > 0))
   5258 	{
   5259 	  /* Make sure this symbol is output as a dynamic symbol.  */
   5260 	  if (!ensure_undef_dynamic (info, h))
   5261 	    return FALSE;
   5262 
   5263 	  if (h->dynindx == -1)
   5264 	    h->dyn_relocs = NULL;
   5265 	}
   5266       else
   5267 	h->dyn_relocs = NULL;
   5268     }
   5269 
   5270   /* Allocate space.  */
   5271   for (p = h->dyn_relocs; p != NULL; p = p->next)
   5272     {
   5273       asection *sreloc = elf_section_data (p->sec)->sreloc;
   5274       if (eh->elf.type == STT_GNU_IFUNC)
   5275 	sreloc = htab->elf.irelplt;
   5276       sreloc->size += p->count * sizeof (Elf32_External_Rela);
   5277     }
   5278 
   5279   /* Handle PLT relocs.  Done last, after dynindx has settled.
   5280      We might need a PLT entry when the symbol
   5281      a) is dynamic, or
   5282      b) is an ifunc, or
   5283      c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
   5284      d) has plt16 relocs and we are linking statically.  */
   5285   dyn = htab->elf.dynamic_sections_created && h->dynindx != -1;
   5286   if (dyn
   5287       || h->type == STT_GNU_IFUNC
   5288       || (h->needs_plt && h->dynamic_adjusted)
   5289       || (h->needs_plt
   5290 	  && h->def_regular
   5291 	  && !htab->elf.dynamic_sections_created
   5292 	  && !htab->can_convert_all_inline_plt
   5293 	  && (ppc_elf_hash_entry (h)->tls_mask
   5294 	      & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
   5295     {
   5296       struct plt_entry *ent;
   5297       bfd_boolean doneone = FALSE;
   5298       bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
   5299 
   5300       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   5301 	if (ent->plt.refcount > 0)
   5302 	  {
   5303 	    asection *s = htab->elf.splt;
   5304 
   5305 	    if (!dyn)
   5306 	      {
   5307 		if (h->type == STT_GNU_IFUNC)
   5308 		  s = htab->elf.iplt;
   5309 		else
   5310 		  s = htab->pltlocal;
   5311 	      }
   5312 
   5313 	    if (htab->plt_type == PLT_NEW || !dyn)
   5314 	      {
   5315 		if (!doneone)
   5316 		  {
   5317 		    plt_offset = s->size;
   5318 		    s->size += 4;
   5319 		  }
   5320 		ent->plt.offset = plt_offset;
   5321 
   5322 		if (s == htab->pltlocal)
   5323 		  ent->glink_offset = glink_offset;
   5324 		else
   5325 		  {
   5326 		    s = htab->glink;
   5327 		    if (!doneone || bfd_link_pic (info))
   5328 		      {
   5329 			glink_offset = s->size;
   5330 			s->size += GLINK_ENTRY_SIZE (htab, h);
   5331 		      }
   5332 		    if (!doneone
   5333 			&& !bfd_link_pic (info)
   5334 			&& h->def_dynamic
   5335 			&& !h->def_regular)
   5336 		      {
   5337 			h->root.u.def.section = s;
   5338 			h->root.u.def.value = glink_offset;
   5339 		      }
   5340 		    ent->glink_offset = glink_offset;
   5341 
   5342 		    if (htab->params->emit_stub_syms
   5343 			&& !add_stub_sym (ent, h, info))
   5344 		      return FALSE;
   5345 		  }
   5346 	      }
   5347 	    else
   5348 	      {
   5349 		if (!doneone)
   5350 		  {
   5351 		    /* If this is the first .plt entry, make room
   5352 		       for the special first entry.  */
   5353 		    if (s->size == 0)
   5354 		      s->size += htab->plt_initial_entry_size;
   5355 
   5356 		    /* The PowerPC PLT is actually composed of two
   5357 		       parts, the first part is 2 words (for a load
   5358 		       and a jump), and then there is a remaining
   5359 		       word available at the end.  */
   5360 		    plt_offset = (htab->plt_initial_entry_size
   5361 				  + (htab->plt_slot_size
   5362 				     * ((s->size
   5363 					 - htab->plt_initial_entry_size)
   5364 					/ htab->plt_entry_size)));
   5365 
   5366 		    /* If this symbol is not defined in a regular
   5367 		       file, and we are not generating a shared
   5368 		       library, then set the symbol to this location
   5369 		       in the .plt.  This is to avoid text
   5370 		       relocations, and is required to make
   5371 		       function pointers compare as equal between
   5372 		       the normal executable and the shared library.  */
   5373 		    if (! bfd_link_pic (info)
   5374 			&& h->def_dynamic
   5375 			&& !h->def_regular)
   5376 		      {
   5377 			h->root.u.def.section = s;
   5378 			h->root.u.def.value = plt_offset;
   5379 		      }
   5380 
   5381 		    /* Make room for this entry.  */
   5382 		    s->size += htab->plt_entry_size;
   5383 		    /* After the 8192nd entry, room for two entries
   5384 		       is allocated.  */
   5385 		    if (htab->plt_type == PLT_OLD
   5386 			&& (s->size - htab->plt_initial_entry_size)
   5387 			/ htab->plt_entry_size
   5388 			> PLT_NUM_SINGLE_ENTRIES)
   5389 		      s->size += htab->plt_entry_size;
   5390 		  }
   5391 		ent->plt.offset = plt_offset;
   5392 	      }
   5393 
   5394 	    /* We also need to make an entry in the .rela.plt section.  */
   5395 	    if (!doneone)
   5396 	      {
   5397 		if (!dyn)
   5398 		  {
   5399 		    if (h->type == STT_GNU_IFUNC)
   5400 		      {
   5401 			s = htab->elf.irelplt;
   5402 			s->size += sizeof (Elf32_External_Rela);
   5403 		      }
   5404 		    else if (bfd_link_pic (info))
   5405 		      {
   5406 			s = htab->relpltlocal;
   5407 			s->size += sizeof (Elf32_External_Rela);
   5408 		      }
   5409 		  }
   5410 		else
   5411 		  {
   5412 		    htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
   5413 
   5414 		    if (htab->plt_type == PLT_VXWORKS)
   5415 		      {
   5416 			/* Allocate space for the unloaded relocations.  */
   5417 			if (!bfd_link_pic (info)
   5418 			    && htab->elf.dynamic_sections_created)
   5419 			  {
   5420 			    if (ent->plt.offset
   5421 				== (bfd_vma) htab->plt_initial_entry_size)
   5422 			      {
   5423 				htab->srelplt2->size
   5424 				  += (sizeof (Elf32_External_Rela)
   5425 				      * VXWORKS_PLTRESOLVE_RELOCS);
   5426 			      }
   5427 
   5428 			    htab->srelplt2->size
   5429 			      += (sizeof (Elf32_External_Rela)
   5430 				  * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
   5431 			  }
   5432 
   5433 			/* Every PLT entry has an associated GOT entry in
   5434 			   .got.plt.  */
   5435 			htab->elf.sgotplt->size += 4;
   5436 		      }
   5437 		  }
   5438 		doneone = TRUE;
   5439 	      }
   5440 	  }
   5441 	else
   5442 	  ent->plt.offset = (bfd_vma) -1;
   5443 
   5444       if (!doneone)
   5445 	{
   5446 	  h->plt.plist = NULL;
   5447 	  h->needs_plt = 0;
   5448 	}
   5449     }
   5450   else
   5451     {
   5452       h->plt.plist = NULL;
   5453       h->needs_plt = 0;
   5454     }
   5455 
   5456   return TRUE;
   5457 }
   5458 
   5459 static const unsigned char glink_eh_frame_cie[] =
   5460 {
   5461   0, 0, 0, 16,				/* length.  */
   5462   0, 0, 0, 0,				/* id.  */
   5463   1,					/* CIE version.  */
   5464   'z', 'R', 0,				/* Augmentation string.  */
   5465   4,					/* Code alignment.  */
   5466   0x7c,					/* Data alignment.  */
   5467   65,					/* RA reg.  */
   5468   1,					/* Augmentation size.  */
   5469   DW_EH_PE_pcrel | DW_EH_PE_sdata4,	/* FDE encoding.  */
   5470   DW_CFA_def_cfa, 1, 0			/* def_cfa: r1 offset 0.  */
   5471 };
   5472 
   5473 /* Set the sizes of the dynamic sections.  */
   5474 
   5475 static bfd_boolean
   5476 ppc_elf_size_dynamic_sections (bfd *output_bfd,
   5477 			       struct bfd_link_info *info)
   5478 {
   5479   struct ppc_elf_link_hash_table *htab;
   5480   asection *s;
   5481   bfd_boolean relocs;
   5482   bfd *ibfd;
   5483 
   5484 #ifdef DEBUG
   5485   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
   5486 #endif
   5487 
   5488   htab = ppc_elf_hash_table (info);
   5489   BFD_ASSERT (htab->elf.dynobj != NULL);
   5490 
   5491   if (elf_hash_table (info)->dynamic_sections_created)
   5492     {
   5493       /* Set the contents of the .interp section to the interpreter.  */
   5494       if (bfd_link_executable (info) && !info->nointerp)
   5495 	{
   5496 	  s = bfd_get_linker_section (htab->elf.dynobj, ".interp");
   5497 	  BFD_ASSERT (s != NULL);
   5498 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   5499 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   5500 	}
   5501     }
   5502 
   5503   if (htab->plt_type == PLT_OLD)
   5504     htab->got_header_size = 16;
   5505   else if (htab->plt_type == PLT_NEW)
   5506     htab->got_header_size = 12;
   5507 
   5508   /* Set up .got offsets for local syms, and space for local dynamic
   5509      relocs.  */
   5510   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   5511     {
   5512       bfd_signed_vma *local_got;
   5513       bfd_signed_vma *end_local_got;
   5514       struct plt_entry **local_plt;
   5515       struct plt_entry **end_local_plt;
   5516       char *lgot_masks;
   5517       bfd_size_type locsymcount;
   5518       Elf_Internal_Shdr *symtab_hdr;
   5519 
   5520       if (!is_ppc_elf (ibfd))
   5521 	continue;
   5522 
   5523       for (s = ibfd->sections; s != NULL; s = s->next)
   5524 	{
   5525 	  struct ppc_dyn_relocs *p;
   5526 
   5527 	  for (p = ((struct ppc_dyn_relocs *)
   5528 		    elf_section_data (s)->local_dynrel);
   5529 	       p != NULL;
   5530 	       p = p->next)
   5531 	    {
   5532 	      if (!bfd_is_abs_section (p->sec)
   5533 		  && bfd_is_abs_section (p->sec->output_section))
   5534 		{
   5535 		  /* Input section has been discarded, either because
   5536 		     it is a copy of a linkonce section or due to
   5537 		     linker script /DISCARD/, so we'll be discarding
   5538 		     the relocs too.  */
   5539 		}
   5540 	      else if (htab->elf.target_os == is_vxworks
   5541 		       && strcmp (p->sec->output_section->name,
   5542 				  ".tls_vars") == 0)
   5543 		{
   5544 		  /* Relocations in vxworks .tls_vars sections are
   5545 		     handled specially by the loader.  */
   5546 		}
   5547 	      else if (p->count != 0)
   5548 		{
   5549 		  asection *sreloc = elf_section_data (p->sec)->sreloc;
   5550 		  if (p->ifunc)
   5551 		    sreloc = htab->elf.irelplt;
   5552 		  sreloc->size += p->count * sizeof (Elf32_External_Rela);
   5553 		  if ((p->sec->output_section->flags
   5554 		       & (SEC_READONLY | SEC_ALLOC))
   5555 		      == (SEC_READONLY | SEC_ALLOC))
   5556 		    {
   5557 		      info->flags |= DF_TEXTREL;
   5558 		      info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
   5559 					      p->sec->owner, p->sec);
   5560 		    }
   5561 		}
   5562 	    }
   5563 	}
   5564 
   5565       local_got = elf_local_got_refcounts (ibfd);
   5566       if (!local_got)
   5567 	continue;
   5568 
   5569       symtab_hdr = &elf_symtab_hdr (ibfd);
   5570       locsymcount = symtab_hdr->sh_info;
   5571       end_local_got = local_got + locsymcount;
   5572       local_plt = (struct plt_entry **) end_local_got;
   5573       end_local_plt = local_plt + locsymcount;
   5574       lgot_masks = (char *) end_local_plt;
   5575 
   5576       for (; local_got < end_local_got; ++local_got, ++lgot_masks)
   5577 	if (*local_got > 0)
   5578 	  {
   5579 	    unsigned int need;
   5580 	    if ((*lgot_masks & (TLS_TLS | TLS_LD)) == (TLS_TLS | TLS_LD))
   5581 	      htab->tlsld_got.refcount += 1;
   5582 	    need = got_entries_needed (*lgot_masks);
   5583 	    if (need == 0)
   5584 	      *local_got = (bfd_vma) -1;
   5585 	    else
   5586 	      {
   5587 		*local_got = allocate_got (htab, need);
   5588 		if (bfd_link_pic (info)
   5589 		    && !((*lgot_masks & TLS_TLS) != 0
   5590 			 && bfd_link_executable (info)))
   5591 		  {
   5592 		    asection *srel;
   5593 
   5594 		    need *= sizeof (Elf32_External_Rela) / 4;
   5595 		    srel = htab->elf.srelgot;
   5596 		    if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   5597 		      srel = htab->elf.irelplt;
   5598 		    srel->size += need;
   5599 		  }
   5600 	      }
   5601 	  }
   5602 	else
   5603 	  *local_got = (bfd_vma) -1;
   5604 
   5605       if (htab->elf.target_os == is_vxworks)
   5606 	continue;
   5607 
   5608       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
   5609       lgot_masks = (char *) end_local_plt;
   5610       for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
   5611 	{
   5612 	  struct plt_entry *ent;
   5613 	  bfd_boolean doneone = FALSE;
   5614 	  bfd_vma plt_offset = 0, glink_offset = (bfd_vma) -1;
   5615 
   5616 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   5617 	    if (ent->plt.refcount > 0)
   5618 	      {
   5619 		if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   5620 		  s = htab->elf.iplt;
   5621 		else if (htab->can_convert_all_inline_plt
   5622 			 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
   5623 		  {
   5624 		    ent->plt.offset = (bfd_vma) -1;
   5625 		    continue;
   5626 		  }
   5627 		else
   5628 		  s = htab->pltlocal;
   5629 
   5630 		if (!doneone)
   5631 		  {
   5632 		    plt_offset = s->size;
   5633 		    s->size += 4;
   5634 		  }
   5635 		ent->plt.offset = plt_offset;
   5636 
   5637 		if (s != htab->pltlocal && (!doneone || bfd_link_pic (info)))
   5638 		  {
   5639 		    s = htab->glink;
   5640 		    glink_offset = s->size;
   5641 		    s->size += GLINK_ENTRY_SIZE (htab, NULL);
   5642 		  }
   5643 		ent->glink_offset = glink_offset;
   5644 
   5645 		if (!doneone)
   5646 		  {
   5647 		    if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
   5648 		      {
   5649 			s = htab->elf.irelplt;
   5650 			s->size += sizeof (Elf32_External_Rela);
   5651 		      }
   5652 		    else if (bfd_link_pic (info))
   5653 		      {
   5654 			s = htab->relpltlocal;
   5655 			s->size += sizeof (Elf32_External_Rela);
   5656 		      }
   5657 		    doneone = TRUE;
   5658 		  }
   5659 	      }
   5660 	    else
   5661 	      ent->plt.offset = (bfd_vma) -1;
   5662 	}
   5663     }
   5664 
   5665   /* Allocate space for global sym dynamic relocs.  */
   5666   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
   5667 
   5668   if (htab->tlsld_got.refcount > 0)
   5669     {
   5670       htab->tlsld_got.offset = allocate_got (htab, 8);
   5671       if (bfd_link_dll (info))
   5672 	htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
   5673     }
   5674   else
   5675     htab->tlsld_got.offset = (bfd_vma) -1;
   5676 
   5677   if (htab->elf.sgot != NULL && htab->plt_type != PLT_VXWORKS)
   5678     {
   5679       unsigned int g_o_t = 32768;
   5680 
   5681       /* If we haven't allocated the header, do so now.  When we get here,
   5682 	 for old plt/got the got size will be 0 to 32764 (not allocated),
   5683 	 or 32780 to 65536 (header allocated).  For new plt/got, the
   5684 	 corresponding ranges are 0 to 32768 and 32780 to 65536.  */
   5685       if (htab->elf.sgot->size <= 32768)
   5686 	{
   5687 	  g_o_t = htab->elf.sgot->size;
   5688 	  if (htab->plt_type == PLT_OLD)
   5689 	    g_o_t += 4;
   5690 	  htab->elf.sgot->size += htab->got_header_size;
   5691 	}
   5692 
   5693       htab->elf.hgot->root.u.def.value = g_o_t;
   5694     }
   5695   if (bfd_link_pic (info))
   5696     {
   5697       struct elf_link_hash_entry *sda = htab->sdata[0].sym;
   5698 
   5699       sda->root.u.def.section = htab->elf.hgot->root.u.def.section;
   5700       sda->root.u.def.value = htab->elf.hgot->root.u.def.value;
   5701     }
   5702   if (info->emitrelocations)
   5703     {
   5704       struct elf_link_hash_entry *sda = htab->sdata[0].sym;
   5705 
   5706       if (sda != NULL && sda->ref_regular)
   5707 	sda->root.u.def.section->flags |= SEC_KEEP;
   5708       sda = htab->sdata[1].sym;
   5709       if (sda != NULL && sda->ref_regular)
   5710 	sda->root.u.def.section->flags |= SEC_KEEP;
   5711     }
   5712 
   5713   if (htab->glink != NULL
   5714       && htab->glink->size != 0
   5715       && htab->elf.dynamic_sections_created)
   5716     {
   5717       htab->glink_pltresolve = htab->glink->size;
   5718       /* Space for the branch table.  */
   5719       htab->glink->size
   5720 	+= htab->elf.srelplt->size / (sizeof (Elf32_External_Rela) / 4) - 4;
   5721       /* Pad out to align the start of PLTresolve.  */
   5722       htab->glink->size += -htab->glink->size & (htab->params->ppc476_workaround
   5723 						 ? 63 : 15);
   5724       htab->glink->size += GLINK_PLTRESOLVE;
   5725 
   5726       if (htab->params->emit_stub_syms)
   5727 	{
   5728 	  struct elf_link_hash_entry *sh;
   5729 	  sh = elf_link_hash_lookup (&htab->elf, "__glink",
   5730 				     TRUE, FALSE, FALSE);
   5731 	  if (sh == NULL)
   5732 	    return FALSE;
   5733 	  if (sh->root.type == bfd_link_hash_new)
   5734 	    {
   5735 	      sh->root.type = bfd_link_hash_defined;
   5736 	      sh->root.u.def.section = htab->glink;
   5737 	      sh->root.u.def.value = htab->glink_pltresolve;
   5738 	      sh->ref_regular = 1;
   5739 	      sh->def_regular = 1;
   5740 	      sh->ref_regular_nonweak = 1;
   5741 	      sh->forced_local = 1;
   5742 	      sh->non_elf = 0;
   5743 	      sh->root.linker_def = 1;
   5744 	    }
   5745 	  sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   5746 				     TRUE, FALSE, FALSE);
   5747 	  if (sh == NULL)
   5748 	    return FALSE;
   5749 	  if (sh->root.type == bfd_link_hash_new)
   5750 	    {
   5751 	      sh->root.type = bfd_link_hash_defined;
   5752 	      sh->root.u.def.section = htab->glink;
   5753 	      sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
   5754 	      sh->ref_regular = 1;
   5755 	      sh->def_regular = 1;
   5756 	      sh->ref_regular_nonweak = 1;
   5757 	      sh->forced_local = 1;
   5758 	      sh->non_elf = 0;
   5759 	      sh->root.linker_def = 1;
   5760 	    }
   5761 	}
   5762     }
   5763 
   5764   if (htab->glink != NULL
   5765       && htab->glink->size != 0
   5766       && htab->glink_eh_frame != NULL
   5767       && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
   5768       && _bfd_elf_eh_frame_present (info))
   5769     {
   5770       s = htab->glink_eh_frame;
   5771       s->size = sizeof (glink_eh_frame_cie) + 20;
   5772       if (bfd_link_pic (info))
   5773 	{
   5774 	  s->size += 4;
   5775 	  if (htab->glink->size - GLINK_PLTRESOLVE + 8 >= 256)
   5776 	    s->size += 4;
   5777 	}
   5778     }
   5779 
   5780   /* We've now determined the sizes of the various dynamic sections.
   5781      Allocate memory for them.  */
   5782   relocs = FALSE;
   5783   for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
   5784     {
   5785       bfd_boolean strip_section = TRUE;
   5786 
   5787       if ((s->flags & SEC_LINKER_CREATED) == 0)
   5788 	continue;
   5789 
   5790       if (s == htab->elf.splt
   5791 	  || s == htab->elf.sgot)
   5792 	{
   5793 	  /* We'd like to strip these sections if they aren't needed, but if
   5794 	     we've exported dynamic symbols from them we must leave them.
   5795 	     It's too late to tell BFD to get rid of the symbols.  */
   5796 	  if (htab->elf.hplt != NULL)
   5797 	    strip_section = FALSE;
   5798 	  /* Strip this section if we don't need it; see the
   5799 	     comment below.  */
   5800 	}
   5801       else if (s == htab->elf.iplt
   5802 	       || s == htab->pltlocal
   5803 	       || s == htab->glink
   5804 	       || s == htab->glink_eh_frame
   5805 	       || s == htab->elf.sgotplt
   5806 	       || s == htab->sbss
   5807 	       || s == htab->elf.sdynbss
   5808 	       || s == htab->elf.sdynrelro
   5809 	       || s == htab->dynsbss)
   5810 	{
   5811 	  /* Strip these too.  */
   5812 	}
   5813       else if (s == htab->sdata[0].section
   5814 	       || s == htab->sdata[1].section)
   5815 	{
   5816 	  strip_section = (s->flags & SEC_KEEP) == 0;
   5817 	}
   5818       else if (CONST_STRNEQ (bfd_section_name (s), ".rela"))
   5819 	{
   5820 	  if (s->size != 0)
   5821 	    {
   5822 	      /* Remember whether there are any relocation sections.  */
   5823 	      relocs = TRUE;
   5824 
   5825 	      /* We use the reloc_count field as a counter if we need
   5826 		 to copy relocs into the output file.  */
   5827 	      s->reloc_count = 0;
   5828 	    }
   5829 	}
   5830       else
   5831 	{
   5832 	  /* It's not one of our sections, so don't allocate space.  */
   5833 	  continue;
   5834 	}
   5835 
   5836       if (s->size == 0 && strip_section)
   5837 	{
   5838 	  /* If we don't need this section, strip it from the
   5839 	     output file.  This is mostly to handle .rela.bss and
   5840 	     .rela.plt.  We must create both sections in
   5841 	     create_dynamic_sections, because they must be created
   5842 	     before the linker maps input sections to output
   5843 	     sections.  The linker does that before
   5844 	     adjust_dynamic_symbol is called, and it is that
   5845 	     function which decides whether anything needs to go
   5846 	     into these sections.  */
   5847 	  s->flags |= SEC_EXCLUDE;
   5848 	  continue;
   5849 	}
   5850 
   5851       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   5852 	continue;
   5853 
   5854       /* Allocate memory for the section contents.  */
   5855       s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
   5856       if (s->contents == NULL)
   5857 	return FALSE;
   5858     }
   5859 
   5860   if (htab->elf.dynamic_sections_created)
   5861     {
   5862       /* Add some entries to the .dynamic section.  We fill in the
   5863 	 values later, in ppc_elf_finish_dynamic_sections, but we
   5864 	 must add the entries now so that we get the correct size for
   5865 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   5866 	 dynamic linker and used by the debugger.  */
   5867 #define add_dynamic_entry(TAG, VAL) \
   5868   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   5869 
   5870       if (!_bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
   5871 						    relocs))
   5872 	return FALSE;
   5873 
   5874       if (htab->plt_type == PLT_NEW
   5875 	  && htab->glink != NULL
   5876 	  && htab->glink->size != 0)
   5877 	{
   5878 	  if (!add_dynamic_entry (DT_PPC_GOT, 0))
   5879 	    return FALSE;
   5880 	  if (!htab->params->no_tls_get_addr_opt
   5881 	      && htab->tls_get_addr != NULL
   5882 	      && htab->tls_get_addr->plt.plist != NULL
   5883 	      && !add_dynamic_entry (DT_PPC_OPT, PPC_OPT_TLS))
   5884 	    return FALSE;
   5885 	}
   5886    }
   5887 #undef add_dynamic_entry
   5888 
   5889   if (htab->glink_eh_frame != NULL
   5890       && htab->glink_eh_frame->contents != NULL)
   5891     {
   5892       unsigned char *p = htab->glink_eh_frame->contents;
   5893       bfd_vma val;
   5894 
   5895       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
   5896       /* CIE length (rewrite in case little-endian).  */
   5897       bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
   5898       p += sizeof (glink_eh_frame_cie);
   5899       /* FDE length.  */
   5900       val = htab->glink_eh_frame->size - 4 - sizeof (glink_eh_frame_cie);
   5901       bfd_put_32 (htab->elf.dynobj, val, p);
   5902       p += 4;
   5903       /* CIE pointer.  */
   5904       val = p - htab->glink_eh_frame->contents;
   5905       bfd_put_32 (htab->elf.dynobj, val, p);
   5906       p += 4;
   5907       /* Offset to .glink.  Set later.  */
   5908       p += 4;
   5909       /* .glink size.  */
   5910       bfd_put_32 (htab->elf.dynobj, htab->glink->size, p);
   5911       p += 4;
   5912       /* Augmentation.  */
   5913       p += 1;
   5914 
   5915       if (bfd_link_pic (info)
   5916 	  && htab->elf.dynamic_sections_created)
   5917 	{
   5918 	  bfd_vma adv = (htab->glink->size - GLINK_PLTRESOLVE + 8) >> 2;
   5919 	  if (adv < 64)
   5920 	    *p++ = DW_CFA_advance_loc + adv;
   5921 	  else if (adv < 256)
   5922 	    {
   5923 	      *p++ = DW_CFA_advance_loc1;
   5924 	      *p++ = adv;
   5925 	    }
   5926 	  else if (adv < 65536)
   5927 	    {
   5928 	      *p++ = DW_CFA_advance_loc2;
   5929 	      bfd_put_16 (htab->elf.dynobj, adv, p);
   5930 	      p += 2;
   5931 	    }
   5932 	  else
   5933 	    {
   5934 	      *p++ = DW_CFA_advance_loc4;
   5935 	      bfd_put_32 (htab->elf.dynobj, adv, p);
   5936 	      p += 4;
   5937 	    }
   5938 	  *p++ = DW_CFA_register;
   5939 	  *p++ = 65;
   5940 	  p++;
   5941 	  *p++ = DW_CFA_advance_loc + 4;
   5942 	  *p++ = DW_CFA_restore_extended;
   5943 	  *p++ = 65;
   5944 	}
   5945       BFD_ASSERT ((bfd_vma) ((p + 3 - htab->glink_eh_frame->contents) & -4)
   5946 		  == htab->glink_eh_frame->size);
   5947     }
   5948 
   5949   return TRUE;
   5950 }
   5951 
   5952 /* Arrange to have _SDA_BASE_ or _SDA2_BASE_ stripped from the output
   5953    if it looks like nothing is using them.  */
   5954 
   5955 static void
   5956 maybe_strip_sdasym (bfd *output_bfd, elf_linker_section_t *lsect)
   5957 {
   5958   struct elf_link_hash_entry *sda = lsect->sym;
   5959 
   5960   if (sda != NULL && !sda->ref_regular && sda->dynindx == -1)
   5961     {
   5962       asection *s;
   5963 
   5964       s = bfd_get_section_by_name (output_bfd, lsect->name);
   5965       if (s == NULL || bfd_section_removed_from_list (output_bfd, s))
   5966 	{
   5967 	  s = bfd_get_section_by_name (output_bfd, lsect->bss_name);
   5968 	  if (s == NULL || bfd_section_removed_from_list (output_bfd, s))
   5969 	    {
   5970 	      sda->def_regular = 0;
   5971 	      /* This is somewhat magic.  See elf_link_output_extsym.  */
   5972 	      sda->ref_dynamic = 1;
   5973 	      sda->forced_local = 0;
   5974 	    }
   5975 	}
   5976     }
   5977 }
   5978 
   5979 void
   5980 ppc_elf_maybe_strip_sdata_syms (struct bfd_link_info *info)
   5981 {
   5982   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   5983 
   5984   if (htab != NULL)
   5985     {
   5986       maybe_strip_sdasym (info->output_bfd, &htab->sdata[0]);
   5987       maybe_strip_sdasym (info->output_bfd, &htab->sdata[1]);
   5988     }
   5989 }
   5990 
   5991 
   5992 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   5993 
   5994 static bfd_boolean
   5995 ppc_elf_hash_symbol (struct elf_link_hash_entry *h)
   5996 {
   5997   if (h->plt.plist != NULL
   5998       && !h->def_regular
   5999       && (!h->pointer_equality_needed
   6000 	  || !h->ref_regular_nonweak))
   6001     return FALSE;
   6002 
   6003   return _bfd_elf_hash_symbol (h);
   6004 }
   6005 
   6006 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
   6008 
   6009 /* Relaxation trampolines.  r12 is available for clobbering (r11, is
   6010    used for some functions that are allowed to break the ABI).  */
   6011 static const int shared_stub_entry[] =
   6012   {
   6013     0x7c0802a6, /* mflr 0 */
   6014     0x429f0005, /* bcl 20, 31, .Lxxx */
   6015     0x7d8802a6, /* mflr 12 */
   6016     0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
   6017     0x398c0000, /* addi 12, 12, (xxx-.Lxxx)@l */
   6018     0x7c0803a6, /* mtlr 0 */
   6019     0x7d8903a6, /* mtctr 12 */
   6020     0x4e800420, /* bctr */
   6021   };
   6022 
   6023 static const int stub_entry[] =
   6024   {
   6025     0x3d800000, /* lis 12,xxx@ha */
   6026     0x398c0000, /* addi 12,12,xxx@l */
   6027     0x7d8903a6, /* mtctr 12 */
   6028     0x4e800420, /* bctr */
   6029   };
   6030 
   6031 struct ppc_elf_relax_info
   6032 {
   6033   unsigned int workaround_size;
   6034   unsigned int picfixup_size;
   6035 };
   6036 
   6037 /* This function implements long branch trampolines, and the ppc476
   6038    icache bug workaround.  Any section needing trampolines or patch
   6039    space for the workaround has its size extended so that we can
   6040    add trampolines at the end of the section.  */
   6041 
   6042 static bfd_boolean
   6043 ppc_elf_relax_section (bfd *abfd,
   6044 		       asection *isec,
   6045 		       struct bfd_link_info *link_info,
   6046 		       bfd_boolean *again)
   6047 {
   6048   struct one_branch_fixup
   6049   {
   6050     struct one_branch_fixup *next;
   6051     asection *tsec;
   6052     /* Final link, can use the symbol offset.  For a
   6053        relocatable link we use the symbol's index.  */
   6054     bfd_vma toff;
   6055     bfd_vma trampoff;
   6056   };
   6057 
   6058   Elf_Internal_Shdr *symtab_hdr;
   6059   bfd_byte *contents = NULL;
   6060   Elf_Internal_Sym *isymbuf = NULL;
   6061   Elf_Internal_Rela *internal_relocs = NULL;
   6062   Elf_Internal_Rela *irel, *irelend = NULL;
   6063   struct one_branch_fixup *branch_fixups = NULL;
   6064   struct ppc_elf_relax_info *relax_info = NULL;
   6065   unsigned changes = 0;
   6066   bfd_boolean workaround_change;
   6067   struct ppc_elf_link_hash_table *htab;
   6068   bfd_size_type trampbase, trampoff, newsize, picfixup_size;
   6069   asection *got2;
   6070   bfd_boolean maybe_pasted;
   6071 
   6072   *again = FALSE;
   6073 
   6074   /* No need to do anything with non-alloc or non-code sections.  */
   6075   if ((isec->flags & SEC_ALLOC) == 0
   6076       || (isec->flags & SEC_CODE) == 0
   6077       || (isec->flags & SEC_LINKER_CREATED) != 0
   6078       || isec->size < 4)
   6079     return TRUE;
   6080 
   6081   /* We cannot represent the required PIC relocs in the output, so don't
   6082      do anything.  The linker doesn't support mixing -shared and -r
   6083      anyway.  */
   6084   if (bfd_link_relocatable (link_info) && bfd_link_pic (link_info))
   6085     return TRUE;
   6086 
   6087   htab = ppc_elf_hash_table (link_info);
   6088   if (htab == NULL)
   6089     return TRUE;
   6090 
   6091   isec->size = (isec->size + 3) & -4;
   6092   if (isec->rawsize == 0)
   6093     isec->rawsize = isec->size;
   6094   trampbase = isec->size;
   6095 
   6096   BFD_ASSERT (isec->sec_info_type == SEC_INFO_TYPE_NONE
   6097 	      || isec->sec_info_type == SEC_INFO_TYPE_TARGET);
   6098   isec->sec_info_type = SEC_INFO_TYPE_TARGET;
   6099 
   6100   if (htab->params->ppc476_workaround
   6101       || htab->params->pic_fixup > 0)
   6102     {
   6103       if (elf_section_data (isec)->sec_info == NULL)
   6104 	{
   6105 	  elf_section_data (isec)->sec_info
   6106 	    = bfd_zalloc (abfd, sizeof (struct ppc_elf_relax_info));
   6107 	  if (elf_section_data (isec)->sec_info == NULL)
   6108 	    return FALSE;
   6109 	}
   6110       relax_info = elf_section_data (isec)->sec_info;
   6111       trampbase -= relax_info->workaround_size;
   6112     }
   6113 
   6114   maybe_pasted = (strcmp (isec->output_section->name, ".init") == 0
   6115 		  || strcmp (isec->output_section->name, ".fini") == 0);
   6116   /* Space for a branch around any trampolines.  */
   6117   trampoff = trampbase;
   6118   if (maybe_pasted && trampbase == isec->rawsize)
   6119     trampoff += 4;
   6120 
   6121   symtab_hdr = &elf_symtab_hdr (abfd);
   6122   picfixup_size = 0;
   6123   if (htab->params->branch_trampolines
   6124       || htab->params->pic_fixup > 0)
   6125     {
   6126       /* Get a copy of the native relocations.  */
   6127       if (isec->reloc_count != 0)
   6128 	{
   6129 	  internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
   6130 						       link_info->keep_memory);
   6131 	  if (internal_relocs == NULL)
   6132 	    goto error_return;
   6133 	}
   6134 
   6135       got2 = bfd_get_section_by_name (abfd, ".got2");
   6136 
   6137       irelend = internal_relocs + isec->reloc_count;
   6138       for (irel = internal_relocs; irel < irelend; irel++)
   6139 	{
   6140 	  unsigned long r_type = ELF32_R_TYPE (irel->r_info);
   6141 	  bfd_vma toff, roff;
   6142 	  asection *tsec;
   6143 	  struct one_branch_fixup *f;
   6144 	  size_t insn_offset = 0;
   6145 	  bfd_vma max_branch_offset = 0, val;
   6146 	  bfd_byte *hit_addr;
   6147 	  unsigned long t0;
   6148 	  struct elf_link_hash_entry *h;
   6149 	  Elf_Internal_Sym *isym;
   6150 	  struct plt_entry **plist;
   6151 	  unsigned char sym_type;
   6152 
   6153 	  switch (r_type)
   6154 	    {
   6155 	    case R_PPC_REL24:
   6156 	    case R_PPC_LOCAL24PC:
   6157 	    case R_PPC_PLTREL24:
   6158 	    case R_PPC_PLTCALL:
   6159 	      max_branch_offset = 1 << 25;
   6160 	      break;
   6161 
   6162 	    case R_PPC_REL14:
   6163 	    case R_PPC_REL14_BRTAKEN:
   6164 	    case R_PPC_REL14_BRNTAKEN:
   6165 	      max_branch_offset = 1 << 15;
   6166 	      break;
   6167 
   6168 	    case R_PPC_ADDR16_HA:
   6169 	      if (htab->params->pic_fixup > 0)
   6170 		break;
   6171 	      continue;
   6172 
   6173 	    default:
   6174 	      continue;
   6175 	    }
   6176 
   6177 	  /* Get the value of the symbol referred to by the reloc.  */
   6178 	  if (!get_sym_h (&h, &isym, &tsec, NULL, &isymbuf,
   6179 			  ELF32_R_SYM (irel->r_info), abfd))
   6180 	    goto error_return;
   6181 
   6182 	  if (isym != NULL)
   6183 	    {
   6184 	      if (tsec != NULL)
   6185 		;
   6186 	      else if (isym->st_shndx == SHN_ABS)
   6187 		tsec = bfd_abs_section_ptr;
   6188 	      else
   6189 		continue;
   6190 
   6191 	      toff = isym->st_value;
   6192 	      sym_type = ELF_ST_TYPE (isym->st_info);
   6193 	    }
   6194 	  else
   6195 	    {
   6196 	      if (tsec != NULL)
   6197 		toff = h->root.u.def.value;
   6198 	      else if (h->root.type == bfd_link_hash_undefined
   6199 		       || h->root.type == bfd_link_hash_undefweak)
   6200 		{
   6201 		  unsigned long indx;
   6202 
   6203 		  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   6204 		  tsec = bfd_und_section_ptr;
   6205 		  toff = bfd_link_relocatable (link_info) ? indx : 0;
   6206 		}
   6207 	      else
   6208 		continue;
   6209 
   6210 	      /* If this branch is to __tls_get_addr then we may later
   6211 		 optimise away the call.  We won't be needing a long-
   6212 		 branch stub in that case.  */
   6213 	      if (bfd_link_executable (link_info)
   6214 		  && h == htab->tls_get_addr
   6215 		  && irel != internal_relocs)
   6216 		{
   6217 		  unsigned long t_symndx = ELF32_R_SYM (irel[-1].r_info);
   6218 		  unsigned long t_rtype = ELF32_R_TYPE (irel[-1].r_info);
   6219 		  unsigned int tls_mask = 0;
   6220 
   6221 		  /* The previous reloc should be one of R_PPC_TLSGD or
   6222 		     R_PPC_TLSLD, or for older object files, a reloc
   6223 		     on the __tls_get_addr arg setup insn.  Get tls
   6224 		     mask bits from the symbol on that reloc.  */
   6225 		  if (t_symndx < symtab_hdr->sh_info)
   6226 		    {
   6227 		      bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
   6228 
   6229 		      if (local_got_offsets != NULL)
   6230 			{
   6231 			  struct plt_entry **local_plt = (struct plt_entry **)
   6232 			    (local_got_offsets + symtab_hdr->sh_info);
   6233 			  char *lgot_masks = (char *)
   6234 			    (local_plt + symtab_hdr->sh_info);
   6235 			  tls_mask = lgot_masks[t_symndx];
   6236 			}
   6237 		    }
   6238 		  else
   6239 		    {
   6240 		      struct elf_link_hash_entry *th
   6241 			= elf_sym_hashes (abfd)[t_symndx - symtab_hdr->sh_info];
   6242 
   6243 		      while (th->root.type == bfd_link_hash_indirect
   6244 			     || th->root.type == bfd_link_hash_warning)
   6245 			th = (struct elf_link_hash_entry *) th->root.u.i.link;
   6246 
   6247 		      tls_mask
   6248 			= ((struct ppc_elf_link_hash_entry *) th)->tls_mask;
   6249 		    }
   6250 
   6251 		  /* The mask bits tell us if the call will be
   6252 		     optimised away.  */
   6253 		  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   6254 		      && (t_rtype == R_PPC_TLSGD
   6255 			  || t_rtype == R_PPC_GOT_TLSGD16
   6256 			  || t_rtype == R_PPC_GOT_TLSGD16_LO))
   6257 		    continue;
   6258 		  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   6259 		      && (t_rtype == R_PPC_TLSLD
   6260 			  || t_rtype == R_PPC_GOT_TLSLD16
   6261 			  || t_rtype == R_PPC_GOT_TLSLD16_LO))
   6262 		    continue;
   6263 		}
   6264 
   6265 	      sym_type = h->type;
   6266 	    }
   6267 
   6268 	  if (r_type == R_PPC_ADDR16_HA)
   6269 	    {
   6270 	      if (h != NULL
   6271 		  && !h->def_regular
   6272 		  && h->protected_def
   6273 		  && ppc_elf_hash_entry (h)->has_addr16_ha
   6274 		  && ppc_elf_hash_entry (h)->has_addr16_lo)
   6275 		picfixup_size += 12;
   6276 	      continue;
   6277 	    }
   6278 
   6279 	  /* The condition here under which we call find_plt_ent must
   6280 	     match that in relocate_section.  If we call find_plt_ent here
   6281 	     but not in relocate_section, or vice versa, then the branch
   6282 	     destination used here may be incorrect.  */
   6283 	  plist = NULL;
   6284 	  if (h != NULL)
   6285 	    {
   6286 	      /* We know is_branch_reloc (r_type) is true.  */
   6287 	      if (h->type == STT_GNU_IFUNC
   6288 		  || r_type == R_PPC_PLTREL24)
   6289 		plist = &h->plt.plist;
   6290 	    }
   6291 	  else if (sym_type == STT_GNU_IFUNC
   6292 		   && elf_local_got_offsets (abfd) != NULL)
   6293 	    {
   6294 	      bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
   6295 	      struct plt_entry **local_plt = (struct plt_entry **)
   6296 		(local_got_offsets + symtab_hdr->sh_info);
   6297 	      plist = local_plt + ELF32_R_SYM (irel->r_info);
   6298 	    }
   6299 	  if (plist != NULL)
   6300 	    {
   6301 	      bfd_vma addend = 0;
   6302 	      struct plt_entry *ent;
   6303 
   6304 	      if (r_type == R_PPC_PLTREL24 && bfd_link_pic (link_info))
   6305 		addend = irel->r_addend;
   6306 	      ent = find_plt_ent (plist, got2, addend);
   6307 	      if (ent != NULL)
   6308 		{
   6309 		  if (htab->plt_type == PLT_NEW
   6310 		      || h == NULL
   6311 		      || !htab->elf.dynamic_sections_created
   6312 		      || h->dynindx == -1)
   6313 		    {
   6314 		      tsec = htab->glink;
   6315 		      toff = ent->glink_offset;
   6316 		    }
   6317 		  else
   6318 		    {
   6319 		      tsec = htab->elf.splt;
   6320 		      toff = ent->plt.offset;
   6321 		    }
   6322 		}
   6323 	    }
   6324 
   6325 	  /* If the branch and target are in the same section, you have
   6326 	     no hope of adding stubs.  We'll error out later should the
   6327 	     branch overflow.  */
   6328 	  if (tsec == isec)
   6329 	    continue;
   6330 
   6331 	  /* toff is used for the symbol index when the symbol is
   6332 	     undefined and we're doing a relocatable link, so we can't
   6333 	     support addends.  It would be possible to do so by
   6334 	     putting the addend in one_branch_fixup but addends on
   6335 	     branches are rare so it hardly seems worth supporting.  */
   6336 	  if (bfd_link_relocatable (link_info)
   6337 	      && tsec == bfd_und_section_ptr
   6338 	      && r_type != R_PPC_PLTREL24
   6339 	      && irel->r_addend != 0)
   6340 	    continue;
   6341 
   6342 	  /* There probably isn't any reason to handle symbols in
   6343 	     SEC_MERGE sections;  SEC_MERGE doesn't seem a likely
   6344 	     attribute for a code section, and we are only looking at
   6345 	     branches.  However, implement it correctly here as a
   6346 	     reference for other target relax_section functions.  */
   6347 	  if (0 && tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
   6348 	    {
   6349 	      /* At this stage in linking, no SEC_MERGE symbol has been
   6350 		 adjusted, so all references to such symbols need to be
   6351 		 passed through _bfd_merged_section_offset.  (Later, in
   6352 		 relocate_section, all SEC_MERGE symbols *except* for
   6353 		 section symbols have been adjusted.)
   6354 
   6355 		 gas may reduce relocations against symbols in SEC_MERGE
   6356 		 sections to a relocation against the section symbol when
   6357 		 the original addend was zero.  When the reloc is against
   6358 		 a section symbol we should include the addend in the
   6359 		 offset passed to _bfd_merged_section_offset, since the
   6360 		 location of interest is the original symbol.  On the
   6361 		 other hand, an access to "sym+addend" where "sym" is not
   6362 		 a section symbol should not include the addend;  Such an
   6363 		 access is presumed to be an offset from "sym";  The
   6364 		 location of interest is just "sym".  */
   6365 	      if (sym_type == STT_SECTION
   6366 		  && r_type != R_PPC_PLTREL24)
   6367 		toff += irel->r_addend;
   6368 
   6369 	      toff
   6370 		= _bfd_merged_section_offset (abfd, &tsec,
   6371 					      elf_section_data (tsec)->sec_info,
   6372 					      toff);
   6373 
   6374 	      if (sym_type != STT_SECTION
   6375 		  && r_type != R_PPC_PLTREL24)
   6376 		toff += irel->r_addend;
   6377 	    }
   6378 	  /* PLTREL24 addends are special.  */
   6379 	  else if (r_type != R_PPC_PLTREL24)
   6380 	    toff += irel->r_addend;
   6381 
   6382 	  /* Attempted -shared link of non-pic code loses.  */
   6383 	  if ((!bfd_link_relocatable (link_info)
   6384 	       && tsec == bfd_und_section_ptr)
   6385 	      || tsec->output_section == NULL
   6386 	      || (tsec->owner != NULL
   6387 		  && (tsec->owner->flags & BFD_PLUGIN) != 0))
   6388 	    continue;
   6389 
   6390 	  roff = irel->r_offset;
   6391 
   6392 	  /* Avoid creating a lot of unnecessary fixups when
   6393 	     relocatable if the output section size is such that a
   6394 	     fixup can be created at final link.
   6395 	     The max_branch_offset adjustment allows for some number
   6396 	     of other fixups being needed at final link.  */
   6397 	  if (bfd_link_relocatable (link_info)
   6398 	      && (isec->output_section->rawsize - (isec->output_offset + roff)
   6399 		  < max_branch_offset - (max_branch_offset >> 4)))
   6400 	    continue;
   6401 
   6402 	  /* If the branch is in range, no need to do anything.  */
   6403 	  if (tsec != bfd_und_section_ptr
   6404 	      && (!bfd_link_relocatable (link_info)
   6405 		  /* A relocatable link may have sections moved during
   6406 		     final link, so do not presume they remain in range.  */
   6407 		  || tsec->output_section == isec->output_section))
   6408 	    {
   6409 	      bfd_vma symaddr, reladdr;
   6410 
   6411 	      symaddr = tsec->output_section->vma + tsec->output_offset + toff;
   6412 	      reladdr = isec->output_section->vma + isec->output_offset + roff;
   6413 	      if (symaddr - reladdr + max_branch_offset
   6414 		  < 2 * max_branch_offset)
   6415 		continue;
   6416 	    }
   6417 
   6418 	  /* Look for an existing fixup to this address.  */
   6419 	  for (f = branch_fixups; f ; f = f->next)
   6420 	    if (f->tsec == tsec && f->toff == toff)
   6421 	      break;
   6422 
   6423 	  if (f == NULL)
   6424 	    {
   6425 	      size_t size;
   6426 	      unsigned long stub_rtype;
   6427 
   6428 	      val = trampoff - roff;
   6429 	      if (val >= max_branch_offset)
   6430 		/* Oh dear, we can't reach a trampoline.  Don't try to add
   6431 		   one.  We'll report an error later.  */
   6432 		continue;
   6433 
   6434 	      if (bfd_link_pic (link_info))
   6435 		{
   6436 		  size = 4 * ARRAY_SIZE (shared_stub_entry);
   6437 		  insn_offset = 12;
   6438 		}
   6439 	      else
   6440 		{
   6441 		  size = 4 * ARRAY_SIZE (stub_entry);
   6442 		  insn_offset = 0;
   6443 		}
   6444 	      stub_rtype = R_PPC_RELAX;
   6445 	      if (tsec == htab->elf.splt
   6446 		  || tsec == htab->glink)
   6447 		{
   6448 		  stub_rtype = R_PPC_RELAX_PLT;
   6449 		  if (r_type == R_PPC_PLTREL24)
   6450 		    stub_rtype = R_PPC_RELAX_PLTREL24;
   6451 		}
   6452 
   6453 	      /* Hijack the old relocation.  Since we need two
   6454 		 relocations for this use a "composite" reloc.  */
   6455 	      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   6456 					   stub_rtype);
   6457 	      irel->r_offset = trampoff + insn_offset;
   6458 	      if (r_type == R_PPC_PLTREL24
   6459 		  && stub_rtype != R_PPC_RELAX_PLTREL24)
   6460 		irel->r_addend = 0;
   6461 
   6462 	      /* Record the fixup so we don't do it again this section.  */
   6463 	      f = bfd_malloc (sizeof (*f));
   6464 	      f->next = branch_fixups;
   6465 	      f->tsec = tsec;
   6466 	      f->toff = toff;
   6467 	      f->trampoff = trampoff;
   6468 	      branch_fixups = f;
   6469 
   6470 	      trampoff += size;
   6471 	      changes++;
   6472 	    }
   6473 	  else
   6474 	    {
   6475 	      val = f->trampoff - roff;
   6476 	      if (val >= max_branch_offset)
   6477 		continue;
   6478 
   6479 	      /* Nop out the reloc, since we're finalizing things here.  */
   6480 	      irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
   6481 	    }
   6482 
   6483 	  /* Get the section contents.  */
   6484 	  if (contents == NULL)
   6485 	    {
   6486 	      /* Get cached copy if it exists.  */
   6487 	      if (elf_section_data (isec)->this_hdr.contents != NULL)
   6488 		contents = elf_section_data (isec)->this_hdr.contents;
   6489 	      /* Go get them off disk.  */
   6490 	      else if (!bfd_malloc_and_get_section (abfd, isec, &contents))
   6491 		goto error_return;
   6492 	    }
   6493 
   6494 	  /* Fix up the existing branch to hit the trampoline.  */
   6495 	  hit_addr = contents + roff;
   6496 	  switch (r_type)
   6497 	    {
   6498 	    case R_PPC_REL24:
   6499 	    case R_PPC_LOCAL24PC:
   6500 	    case R_PPC_PLTREL24:
   6501 	      t0 = bfd_get_32 (abfd, hit_addr);
   6502 	      t0 &= ~0x3fffffc;
   6503 	      t0 |= val & 0x3fffffc;
   6504 	      bfd_put_32 (abfd, t0, hit_addr);
   6505 	      break;
   6506 
   6507 	    case R_PPC_REL14:
   6508 	    case R_PPC_REL14_BRTAKEN:
   6509 	    case R_PPC_REL14_BRNTAKEN:
   6510 	      t0 = bfd_get_32 (abfd, hit_addr);
   6511 	      t0 &= ~0xfffc;
   6512 	      t0 |= val & 0xfffc;
   6513 	      bfd_put_32 (abfd, t0, hit_addr);
   6514 	      break;
   6515 	    }
   6516 	}
   6517 
   6518       while (branch_fixups != NULL)
   6519 	{
   6520 	  struct one_branch_fixup *f = branch_fixups;
   6521 	  branch_fixups = branch_fixups->next;
   6522 	  free (f);
   6523 	}
   6524     }
   6525 
   6526   workaround_change = FALSE;
   6527   newsize = trampoff;
   6528   if (htab->params->ppc476_workaround
   6529       && (!bfd_link_relocatable (link_info)
   6530 	  || isec->output_section->alignment_power >= htab->params->pagesize_p2))
   6531     {
   6532       bfd_vma addr, end_addr;
   6533       unsigned int crossings;
   6534       bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
   6535 
   6536       addr = isec->output_section->vma + isec->output_offset;
   6537       end_addr = addr + trampoff;
   6538       addr &= -pagesize;
   6539       crossings = ((end_addr & -pagesize) - addr) >> htab->params->pagesize_p2;
   6540       if (crossings != 0)
   6541 	{
   6542 	  /* Keep space aligned, to ensure the patch code itself does
   6543 	     not cross a page.  Don't decrease size calculated on a
   6544 	     previous pass as otherwise we might never settle on a layout.  */
   6545 	  newsize = 15 - ((end_addr - 1) & 15);
   6546 	  newsize += crossings * 16;
   6547 	  if (relax_info->workaround_size < newsize)
   6548 	    {
   6549 	      relax_info->workaround_size = newsize;
   6550 	      workaround_change = TRUE;
   6551 	    }
   6552 	  /* Ensure relocate_section is called.  */
   6553 	  isec->flags |= SEC_RELOC;
   6554 	}
   6555       newsize = trampoff + relax_info->workaround_size;
   6556     }
   6557 
   6558   if (htab->params->pic_fixup > 0)
   6559     {
   6560       picfixup_size -= relax_info->picfixup_size;
   6561       if (picfixup_size != 0)
   6562 	relax_info->picfixup_size += picfixup_size;
   6563       newsize += relax_info->picfixup_size;
   6564     }
   6565 
   6566   if (changes != 0 || picfixup_size != 0 || workaround_change)
   6567     isec->size = newsize;
   6568 
   6569   if (isymbuf != NULL
   6570       && symtab_hdr->contents != (unsigned char *) isymbuf)
   6571     {
   6572       if (! link_info->keep_memory)
   6573 	free (isymbuf);
   6574       else
   6575 	{
   6576 	  /* Cache the symbols for elf_link_input_bfd.  */
   6577 	  symtab_hdr->contents = (unsigned char *) isymbuf;
   6578 	}
   6579     }
   6580 
   6581   if (contents != NULL
   6582       && elf_section_data (isec)->this_hdr.contents != contents)
   6583     {
   6584       if (!changes && !link_info->keep_memory)
   6585 	free (contents);
   6586       else
   6587 	{
   6588 	  /* Cache the section contents for elf_link_input_bfd.  */
   6589 	  elf_section_data (isec)->this_hdr.contents = contents;
   6590 	}
   6591     }
   6592 
   6593   changes += picfixup_size;
   6594   if (changes != 0)
   6595     {
   6596       /* Append sufficient NOP relocs so we can write out relocation
   6597 	 information for the trampolines.  */
   6598       Elf_Internal_Shdr *rel_hdr;
   6599       Elf_Internal_Rela *new_relocs = bfd_malloc ((changes + isec->reloc_count)
   6600 						  * sizeof (*new_relocs));
   6601       unsigned ix;
   6602 
   6603       if (!new_relocs)
   6604 	goto error_return;
   6605       memcpy (new_relocs, internal_relocs,
   6606 	      isec->reloc_count * sizeof (*new_relocs));
   6607       for (ix = changes; ix--;)
   6608 	{
   6609 	  irel = new_relocs + ix + isec->reloc_count;
   6610 
   6611 	  irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
   6612 	}
   6613       if (internal_relocs != elf_section_data (isec)->relocs)
   6614 	free (internal_relocs);
   6615       elf_section_data (isec)->relocs = new_relocs;
   6616       isec->reloc_count += changes;
   6617       rel_hdr = _bfd_elf_single_rel_hdr (isec);
   6618       rel_hdr->sh_size += changes * rel_hdr->sh_entsize;
   6619     }
   6620   else if (elf_section_data (isec)->relocs != internal_relocs)
   6621     free (internal_relocs);
   6622 
   6623   *again = changes != 0 || workaround_change;
   6624   return TRUE;
   6625 
   6626  error_return:
   6627   while (branch_fixups != NULL)
   6628     {
   6629       struct one_branch_fixup *f = branch_fixups;
   6630       branch_fixups = branch_fixups->next;
   6631       free (f);
   6632     }
   6633   if ((unsigned char *) isymbuf != symtab_hdr->contents)
   6634     free (isymbuf);
   6635   if (elf_section_data (isec)->this_hdr.contents != contents)
   6636     free (contents);
   6637   if (elf_section_data (isec)->relocs != internal_relocs)
   6638     free (internal_relocs);
   6639   return FALSE;
   6640 }
   6641 
   6642 /* What to do when ld finds relocations against symbols defined in
   6644    discarded sections.  */
   6645 
   6646 static unsigned int
   6647 ppc_elf_action_discarded (asection *sec)
   6648 {
   6649   if (strcmp (".fixup", sec->name) == 0)
   6650     return 0;
   6651 
   6652   if (strcmp (".got2", sec->name) == 0)
   6653     return 0;
   6654 
   6655   return _bfd_elf_default_action_discarded (sec);
   6656 }
   6657 
   6658 /* Fill in the address for a pointer generated in a linker section.  */
   6660 
   6661 static bfd_vma
   6662 elf_finish_pointer_linker_section (bfd *input_bfd,
   6663 				   elf_linker_section_t *lsect,
   6664 				   struct elf_link_hash_entry *h,
   6665 				   bfd_vma relocation,
   6666 				   const Elf_Internal_Rela *rel)
   6667 {
   6668   elf_linker_section_pointers_t *linker_section_ptr;
   6669 
   6670   BFD_ASSERT (lsect != NULL);
   6671 
   6672   if (h != NULL)
   6673     {
   6674       /* Handle global symbol.  */
   6675       struct ppc_elf_link_hash_entry *eh;
   6676 
   6677       eh = (struct ppc_elf_link_hash_entry *) h;
   6678       BFD_ASSERT (eh->elf.def_regular);
   6679       linker_section_ptr = eh->linker_section_pointer;
   6680     }
   6681   else
   6682     {
   6683       /* Handle local symbol.  */
   6684       unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
   6685 
   6686       BFD_ASSERT (is_ppc_elf (input_bfd));
   6687       BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
   6688       linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
   6689     }
   6690 
   6691   linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
   6692 							rel->r_addend,
   6693 							lsect);
   6694   BFD_ASSERT (linker_section_ptr != NULL);
   6695 
   6696   /* Offset will always be a multiple of four, so use the bottom bit
   6697      as a "written" flag.  */
   6698   if ((linker_section_ptr->offset & 1) == 0)
   6699     {
   6700       bfd_put_32 (lsect->section->owner,
   6701 		  relocation + linker_section_ptr->addend,
   6702 		  lsect->section->contents + linker_section_ptr->offset);
   6703       linker_section_ptr->offset += 1;
   6704     }
   6705 
   6706   relocation = (lsect->section->output_section->vma
   6707 		+ lsect->section->output_offset
   6708 		+ linker_section_ptr->offset - 1
   6709 		- SYM_VAL (lsect->sym));
   6710 
   6711 #ifdef DEBUG
   6712   fprintf (stderr,
   6713 	   "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
   6714 	   lsect->name, (long) relocation, (long) relocation);
   6715 #endif
   6716 
   6717   return relocation;
   6718 }
   6719 
   6720 #define PPC_LO(v) ((v) & 0xffff)
   6721 #define PPC_HI(v) (((v) >> 16) & 0xffff)
   6722 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   6723 
   6724 static void
   6725 write_glink_stub (struct elf_link_hash_entry *h, struct plt_entry *ent,
   6726 		  asection *plt_sec, unsigned char *p,
   6727 		  struct bfd_link_info *info)
   6728 {
   6729   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   6730   bfd *output_bfd = info->output_bfd;
   6731   bfd_vma plt;
   6732   unsigned char *end = p + GLINK_ENTRY_SIZE (htab, h);
   6733 
   6734   if (h != NULL
   6735       && h == htab->tls_get_addr
   6736       && !htab->params->no_tls_get_addr_opt)
   6737     {
   6738       bfd_put_32 (output_bfd, LWZ_11_3, p);
   6739       p += 4;
   6740       bfd_put_32 (output_bfd, LWZ_12_3 + 4, p);
   6741       p += 4;
   6742       bfd_put_32 (output_bfd, MR_0_3, p);
   6743       p += 4;
   6744       bfd_put_32 (output_bfd, CMPWI_11_0, p);
   6745       p += 4;
   6746       bfd_put_32 (output_bfd, ADD_3_12_2, p);
   6747       p += 4;
   6748       bfd_put_32 (output_bfd, BEQLR, p);
   6749       p += 4;
   6750       bfd_put_32 (output_bfd, MR_3_0, p);
   6751       p += 4;
   6752       bfd_put_32 (output_bfd, NOP, p);
   6753       p += 4;
   6754     }
   6755 
   6756   plt = ((ent->plt.offset & ~1)
   6757 	 + plt_sec->output_section->vma
   6758 	 + plt_sec->output_offset);
   6759 
   6760   if (bfd_link_pic (info))
   6761     {
   6762       bfd_vma got = 0;
   6763 
   6764       if (ent->addend >= 32768)
   6765 	got = (ent->addend
   6766 	       + ent->sec->output_section->vma
   6767 	       + ent->sec->output_offset);
   6768       else if (htab->elf.hgot != NULL)
   6769 	got = SYM_VAL (htab->elf.hgot);
   6770 
   6771       plt -= got;
   6772 
   6773       if (plt + 0x8000 < 0x10000)
   6774 	bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
   6775       else
   6776 	{
   6777 	  bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
   6778 	  p += 4;
   6779 	  bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
   6780 	}
   6781     }
   6782   else
   6783     {
   6784       bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
   6785       p += 4;
   6786       bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
   6787     }
   6788   p += 4;
   6789   bfd_put_32 (output_bfd, MTCTR_11, p);
   6790   p += 4;
   6791   bfd_put_32 (output_bfd, BCTR, p);
   6792   p += 4;
   6793   while (p < end)
   6794     {
   6795       bfd_put_32 (output_bfd, htab->params->ppc476_workaround ? BA : NOP, p);
   6796       p += 4;
   6797     }
   6798 }
   6799 
   6800 /* Return true if symbol is defined statically.  */
   6801 
   6802 static bfd_boolean
   6803 is_static_defined (struct elf_link_hash_entry *h)
   6804 {
   6805   return ((h->root.type == bfd_link_hash_defined
   6806 	   || h->root.type == bfd_link_hash_defweak)
   6807 	  && h->root.u.def.section != NULL
   6808 	  && h->root.u.def.section->output_section != NULL);
   6809 }
   6810 
   6811 /* If INSN is an opcode that may be used with an @tls operand, return
   6812    the transformed insn for TLS optimisation, otherwise return 0.  If
   6813    REG is non-zero only match an insn with RB or RA equal to REG.  */
   6814 
   6815 unsigned int
   6816 _bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
   6817 {
   6818   unsigned int rtra;
   6819 
   6820   if ((insn & (0x3fu << 26)) != 31 << 26)
   6821     return 0;
   6822 
   6823   if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
   6824     rtra = insn & ((1 << 26) - (1 << 16));
   6825   else if (((insn >> 16) & 0x1f) == reg)
   6826     rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
   6827   else
   6828     return 0;
   6829 
   6830   if ((insn & (0x3ff << 1)) == 266 << 1)
   6831     /* add -> addi.  */
   6832     insn = 14 << 26;
   6833   else if ((insn & (0x1f << 1)) == 23 << 1
   6834 	   && ((insn & (0x1f << 6)) < 14 << 6
   6835 	       || ((insn & (0x1f << 6)) >= 16 << 6
   6836 		   && (insn & (0x1f << 6)) < 24 << 6)))
   6837     /* load and store indexed -> dform.  */
   6838     insn = (32u | ((insn >> 6) & 0x1f)) << 26;
   6839   else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
   6840     /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu.  */
   6841     insn = ((58u | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
   6842   else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
   6843     /* lwax -> lwa.  */
   6844     insn = (58u << 26) | 2;
   6845   else
   6846     return 0;
   6847   insn |= rtra;
   6848   return insn;
   6849 }
   6850 
   6851 /* If INSN is an opcode that may be used with an @tprel operand, return
   6852    the transformed insn for an undefined weak symbol, ie. with the
   6853    thread pointer REG operand removed.  Otherwise return 0.  */
   6854 
   6855 unsigned int
   6856 _bfd_elf_ppc_at_tprel_transform (unsigned int insn, unsigned int reg)
   6857 {
   6858   if ((insn & (0x1f << 16)) == reg << 16
   6859       && ((insn & (0x3fu << 26)) == 14u << 26 /* addi */
   6860 	  || (insn & (0x3fu << 26)) == 15u << 26 /* addis */
   6861 	  || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
   6862 	  || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
   6863 	  || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
   6864 	  || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
   6865 	  || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
   6866 	  || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
   6867 	  || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
   6868 	  || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
   6869 	  || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
   6870 	  || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
   6871 	  || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
   6872 	  || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
   6873 	  || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
   6874 	  || ((insn & (0x3fu << 26)) == 58u << 26 /* lwa,ld,lmd */
   6875 	      && (insn & 3) != 1)
   6876 	  || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
   6877 	      && ((insn & 3) == 0 || (insn & 3) == 3))))
   6878     {
   6879       insn &= ~(0x1f << 16);
   6880     }
   6881   else if ((insn & (0x1f << 21)) == reg << 21
   6882 	   && ((insn & (0x3eu << 26)) == 24u << 26 /* ori, oris */
   6883 	       || (insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */
   6884 	       || (insn & (0x3eu << 26)) == 28u << 26 /* andi,andis */))
   6885     {
   6886       insn &= ~(0x1f << 21);
   6887       insn |= (insn & (0x1f << 16)) << 5;
   6888       if ((insn & (0x3eu << 26)) == 26u << 26 /* xori,xoris */)
   6889 	insn -= 2 >> 26;  /* convert to ori,oris */
   6890     }
   6891   else
   6892     insn = 0;
   6893   return insn;
   6894 }
   6895 
   6896 static bfd_boolean
   6897 is_insn_ds_form (unsigned int insn)
   6898 {
   6899   return ((insn & (0x3fu << 26)) == 58u << 26 /* ld,ldu,lwa */
   6900 	  || (insn & (0x3fu << 26)) == 62u << 26 /* std,stdu,stq */
   6901 	  || (insn & (0x3fu << 26)) == 57u << 26 /* lfdp */
   6902 	  || (insn & (0x3fu << 26)) == 61u << 26 /* stfdp */);
   6903 }
   6904 
   6905 static bfd_boolean
   6906 is_insn_dq_form (unsigned int insn)
   6907 {
   6908   return ((insn & (0x3fu << 26)) == 56u << 26 /* lq */
   6909 	  || ((insn & (0x3fu << 26)) == (61u << 26) /* lxv, stxv */
   6910 	      && (insn & 3) == 1));
   6911 }
   6912 
   6913 /* The RELOCATE_SECTION function is called by the ELF backend linker
   6914    to handle the relocations for a section.
   6915 
   6916    The relocs are always passed as Rela structures; if the section
   6917    actually uses Rel structures, the r_addend field will always be
   6918    zero.
   6919 
   6920    This function is responsible for adjust the section contents as
   6921    necessary, and (if using Rela relocs and generating a
   6922    relocatable output file) adjusting the reloc addend as
   6923    necessary.
   6924 
   6925    This function does not have to worry about setting the reloc
   6926    address or the reloc symbol index.
   6927 
   6928    LOCAL_SYMS is a pointer to the swapped in local symbols.
   6929 
   6930    LOCAL_SECTIONS is an array giving the section in the input file
   6931    corresponding to the st_shndx field of each local symbol.
   6932 
   6933    The global hash table entry for the global symbols can be found
   6934    via elf_sym_hashes (input_bfd).
   6935 
   6936    When generating relocatable output, this function must handle
   6937    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   6938    going to be the section symbol corresponding to the output
   6939    section, which means that the addend must be adjusted
   6940    accordingly.  */
   6941 
   6942 static bfd_boolean
   6943 ppc_elf_relocate_section (bfd *output_bfd,
   6944 			  struct bfd_link_info *info,
   6945 			  bfd *input_bfd,
   6946 			  asection *input_section,
   6947 			  bfd_byte *contents,
   6948 			  Elf_Internal_Rela *relocs,
   6949 			  Elf_Internal_Sym *local_syms,
   6950 			  asection **local_sections)
   6951 {
   6952   Elf_Internal_Shdr *symtab_hdr;
   6953   struct elf_link_hash_entry **sym_hashes;
   6954   struct ppc_elf_link_hash_table *htab;
   6955   Elf_Internal_Rela *rel;
   6956   Elf_Internal_Rela *wrel;
   6957   Elf_Internal_Rela *relend;
   6958   Elf_Internal_Rela outrel;
   6959   asection *got2;
   6960   bfd_vma *local_got_offsets;
   6961   bfd_boolean ret = TRUE;
   6962   bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
   6963   bfd_boolean is_vxworks_tls;
   6964   unsigned int picfixup_size = 0;
   6965   struct ppc_elf_relax_info *relax_info = NULL;
   6966 
   6967 #ifdef DEBUG
   6968   _bfd_error_handler ("ppc_elf_relocate_section called for %pB section %pA, "
   6969 		      "%ld relocations%s",
   6970 		      input_bfd, input_section,
   6971 		      (long) input_section->reloc_count,
   6972 		      (bfd_link_relocatable (info)) ? " (relocatable)" : "");
   6973 #endif
   6974 
   6975   if (!is_ppc_elf (input_bfd))
   6976     {
   6977       bfd_set_error (bfd_error_wrong_format);
   6978       return FALSE;
   6979     }
   6980 
   6981   got2 = bfd_get_section_by_name (input_bfd, ".got2");
   6982 
   6983   /* Initialize howto table if not already done.  */
   6984   if (!ppc_elf_howto_table[R_PPC_ADDR32])
   6985     ppc_elf_howto_init ();
   6986 
   6987   htab = ppc_elf_hash_table (info);
   6988   local_got_offsets = elf_local_got_offsets (input_bfd);
   6989   symtab_hdr = &elf_symtab_hdr (input_bfd);
   6990   sym_hashes = elf_sym_hashes (input_bfd);
   6991   /* We have to handle relocations in vxworks .tls_vars sections
   6992      specially, because the dynamic loader is 'weird'.  */
   6993   is_vxworks_tls = (htab->elf.target_os == is_vxworks && bfd_link_pic (info)
   6994 		    && !strcmp (input_section->output_section->name,
   6995 				".tls_vars"));
   6996   if (input_section->sec_info_type == SEC_INFO_TYPE_TARGET)
   6997     relax_info = elf_section_data (input_section)->sec_info;
   6998   rel = wrel = relocs;
   6999   relend = relocs + input_section->reloc_count;
   7000   for (; rel < relend; wrel++, rel++)
   7001     {
   7002       enum elf_ppc_reloc_type r_type;
   7003       bfd_vma addend;
   7004       bfd_reloc_status_type r;
   7005       Elf_Internal_Sym *sym;
   7006       asection *sec;
   7007       struct elf_link_hash_entry *h;
   7008       const char *sym_name;
   7009       reloc_howto_type *howto;
   7010       unsigned long r_symndx;
   7011       bfd_vma relocation;
   7012       bfd_vma branch_bit, from;
   7013       bfd_boolean unresolved_reloc, save_unresolved_reloc;
   7014       bfd_boolean warned;
   7015       unsigned int tls_type, tls_mask, tls_gd;
   7016       struct plt_entry **ifunc, **plt_list;
   7017       struct reloc_howto_struct alt_howto;
   7018 
   7019     again:
   7020       r_type = ELF32_R_TYPE (rel->r_info);
   7021       sym = NULL;
   7022       sec = NULL;
   7023       h = NULL;
   7024       unresolved_reloc = FALSE;
   7025       warned = FALSE;
   7026       r_symndx = ELF32_R_SYM (rel->r_info);
   7027 
   7028       if (r_symndx < symtab_hdr->sh_info)
   7029 	{
   7030 	  sym = local_syms + r_symndx;
   7031 	  sec = local_sections[r_symndx];
   7032 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   7033 
   7034 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   7035 	}
   7036       else
   7037 	{
   7038 	  bfd_boolean ignored;
   7039 
   7040 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   7041 				   r_symndx, symtab_hdr, sym_hashes,
   7042 				   h, sec, relocation,
   7043 				   unresolved_reloc, warned, ignored);
   7044 
   7045 	  sym_name = h->root.root.string;
   7046 	}
   7047 
   7048       if (sec != NULL && discarded_section (sec))
   7049 	{
   7050 	  /* For relocs against symbols from removed linkonce sections,
   7051 	     or sections discarded by a linker script, we just want the
   7052 	     section contents zeroed.  Avoid any special processing.  */
   7053 	  howto = NULL;
   7054 	  if (r_type < R_PPC_max)
   7055 	    howto = ppc_elf_howto_table[r_type];
   7056 
   7057 	  _bfd_clear_contents (howto, input_bfd, input_section,
   7058 			       contents, rel->r_offset);
   7059 	  wrel->r_offset = rel->r_offset;
   7060 	  wrel->r_info = 0;
   7061 	  wrel->r_addend = 0;
   7062 
   7063 	  /* For ld -r, remove relocations in debug sections against
   7064 	     symbols defined in discarded sections.  Not done for
   7065 	     non-debug to preserve relocs in .eh_frame which the
   7066 	     eh_frame editing code expects to be present.  */
   7067 	  if (bfd_link_relocatable (info)
   7068 	      && (input_section->flags & SEC_DEBUGGING))
   7069 	    wrel--;
   7070 
   7071 	  continue;
   7072 	}
   7073 
   7074       if (bfd_link_relocatable (info))
   7075 	{
   7076 	  if (got2 != NULL
   7077 	      && r_type == R_PPC_PLTREL24
   7078 	      && rel->r_addend != 0)
   7079 	    {
   7080 	      /* R_PPC_PLTREL24 is rather special.  If non-zero, the
   7081 		 addend specifies the GOT pointer offset within .got2.  */
   7082 	      rel->r_addend += got2->output_offset;
   7083 	    }
   7084 	  if (r_type != R_PPC_RELAX_PLT
   7085 	      && r_type != R_PPC_RELAX_PLTREL24
   7086 	      && r_type != R_PPC_RELAX)
   7087 	    goto copy_reloc;
   7088 	}
   7089 
   7090       /* TLS optimizations.  Replace instruction sequences and relocs
   7091 	 based on information we collected in tls_optimize.  We edit
   7092 	 RELOCS so that --emit-relocs will output something sensible
   7093 	 for the final instruction stream.  */
   7094       tls_mask = 0;
   7095       tls_gd = 0;
   7096       if (h != NULL)
   7097 	tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
   7098       else if (local_got_offsets != NULL)
   7099 	{
   7100 	  struct plt_entry **local_plt;
   7101 	  char *lgot_masks;
   7102 	  local_plt
   7103 	    = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
   7104 	  lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
   7105 	  tls_mask = lgot_masks[r_symndx];
   7106 	}
   7107 
   7108       /* Ensure reloc mapping code below stays sane.  */
   7109       if ((R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TLSGD16 & 3)
   7110 	  || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
   7111 	  || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
   7112 	  || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
   7113 	  || (R_PPC_GOT_TLSLD16 & 3)    != (R_PPC_GOT_TPREL16 & 3)
   7114 	  || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
   7115 	  || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
   7116 	  || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
   7117 	abort ();
   7118       switch (r_type)
   7119 	{
   7120 	default:
   7121 	  break;
   7122 
   7123 	case R_PPC_GOT_TPREL16:
   7124 	case R_PPC_GOT_TPREL16_LO:
   7125 	  if ((tls_mask & TLS_TLS) != 0
   7126 	      && (tls_mask & TLS_TPREL) == 0)
   7127 	    {
   7128 	      bfd_vma insn;
   7129 
   7130 	      insn = bfd_get_32 (input_bfd,
   7131 				 contents + rel->r_offset - d_offset);
   7132 	      insn &= 31 << 21;
   7133 	      insn |= 0x3c020000;	/* addis 0,2,0 */
   7134 	      bfd_put_32 (input_bfd, insn,
   7135 			  contents + rel->r_offset - d_offset);
   7136 	      r_type = R_PPC_TPREL16_HA;
   7137 	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7138 	    }
   7139 	  break;
   7140 
   7141 	case R_PPC_TLS:
   7142 	  if ((tls_mask & TLS_TLS) != 0
   7143 	      && (tls_mask & TLS_TPREL) == 0)
   7144 	    {
   7145 	      bfd_vma insn;
   7146 
   7147 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   7148 	      insn = _bfd_elf_ppc_at_tls_transform (insn, 2);
   7149 	      if (insn == 0)
   7150 		abort ();
   7151 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   7152 	      r_type = R_PPC_TPREL16_LO;
   7153 	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7154 
   7155 	      /* Was PPC_TLS which sits on insn boundary, now
   7156 		 PPC_TPREL16_LO which is at low-order half-word.  */
   7157 	      rel->r_offset += d_offset;
   7158 	    }
   7159 	  break;
   7160 
   7161 	case R_PPC_GOT_TLSGD16_HI:
   7162 	case R_PPC_GOT_TLSGD16_HA:
   7163 	  tls_gd = TLS_GDIE;
   7164 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
   7165 	    goto tls_gdld_hi;
   7166 	  break;
   7167 
   7168 	case R_PPC_GOT_TLSLD16_HI:
   7169 	case R_PPC_GOT_TLSLD16_HA:
   7170 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
   7171 	    {
   7172 	    tls_gdld_hi:
   7173 	      if ((tls_mask & tls_gd) != 0)
   7174 		r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
   7175 			  + R_PPC_GOT_TPREL16);
   7176 	      else
   7177 		{
   7178 		  rel->r_offset -= d_offset;
   7179 		  bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   7180 		  r_type = R_PPC_NONE;
   7181 		}
   7182 	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7183 	    }
   7184 	  break;
   7185 
   7186 	case R_PPC_GOT_TLSGD16:
   7187 	case R_PPC_GOT_TLSGD16_LO:
   7188 	  tls_gd = TLS_GDIE;
   7189 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
   7190 	    goto tls_ldgd_opt;
   7191 	  break;
   7192 
   7193 	case R_PPC_GOT_TLSLD16:
   7194 	case R_PPC_GOT_TLSLD16_LO:
   7195 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
   7196 	    {
   7197 	      unsigned int insn1, insn2;
   7198 	      bfd_vma offset;
   7199 
   7200 	    tls_ldgd_opt:
   7201 	      offset = (bfd_vma) -1;
   7202 	      /* If not using the newer R_PPC_TLSGD/LD to mark
   7203 		 __tls_get_addr calls, we must trust that the call
   7204 		 stays with its arg setup insns, ie. that the next
   7205 		 reloc is the __tls_get_addr call associated with
   7206 		 the current reloc.  Edit both insns.  */
   7207 	      if (input_section->nomark_tls_get_addr
   7208 		  && rel + 1 < relend
   7209 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   7210 					      htab->tls_get_addr))
   7211 		offset = rel[1].r_offset;
   7212 	      /* We read the low GOT_TLS insn because we need to keep
   7213 		 the destination reg.  It may be something other than
   7214 		 the usual r3, and moved to r3 before the call by
   7215 		 intervening code.  */
   7216 	      insn1 = bfd_get_32 (input_bfd,
   7217 				  contents + rel->r_offset - d_offset);
   7218 	      if ((tls_mask & tls_gd) != 0)
   7219 		{
   7220 		  /* IE */
   7221 		  insn1 &= (0x1f << 21) | (0x1f << 16);
   7222 		  insn1 |= 32u << 26;	/* lwz */
   7223 		  if (offset != (bfd_vma) -1)
   7224 		    {
   7225 		      rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7226 		      insn2 = 0x7c631214;	/* add 3,3,2 */
   7227 		      bfd_put_32 (input_bfd, insn2, contents + offset);
   7228 		    }
   7229 		  r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
   7230 			    + R_PPC_GOT_TPREL16);
   7231 		  rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7232 		}
   7233 	      else
   7234 		{
   7235 		  /* LE */
   7236 		  insn1 &= 0x1f << 21;
   7237 		  insn1 |= 0x3c020000;	/* addis r,2,0 */
   7238 		  if (tls_gd == 0)
   7239 		    {
   7240 		      /* Was an LD reloc.  */
   7241 		      for (r_symndx = 0;
   7242 			   r_symndx < symtab_hdr->sh_info;
   7243 			   r_symndx++)
   7244 			if (local_sections[r_symndx] == sec)
   7245 			  break;
   7246 		      if (r_symndx >= symtab_hdr->sh_info)
   7247 			r_symndx = STN_UNDEF;
   7248 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   7249 		      if (r_symndx != STN_UNDEF)
   7250 			rel->r_addend -= (local_syms[r_symndx].st_value
   7251 					  + sec->output_offset
   7252 					  + sec->output_section->vma);
   7253 		    }
   7254 		  r_type = R_PPC_TPREL16_HA;
   7255 		  rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7256 		  if (offset != (bfd_vma) -1)
   7257 		    {
   7258 		      rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
   7259 		      rel[1].r_offset = offset + d_offset;
   7260 		      rel[1].r_addend = rel->r_addend;
   7261 		      insn2 = 0x38630000;	/* addi 3,3,0 */
   7262 		      bfd_put_32 (input_bfd, insn2, contents + offset);
   7263 		    }
   7264 		}
   7265 	      bfd_put_32 (input_bfd, insn1,
   7266 			  contents + rel->r_offset - d_offset);
   7267 	      if (tls_gd == 0)
   7268 		{
   7269 		  /* We changed the symbol on an LD reloc.  Start over
   7270 		     in order to get h, sym, sec etc. right.  */
   7271 		  goto again;
   7272 		}
   7273 	    }
   7274 	  break;
   7275 
   7276 	case R_PPC_TLSGD:
   7277 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
   7278 	      && rel + 1 < relend)
   7279 	    {
   7280 	      unsigned int insn2;
   7281 	      bfd_vma offset = rel->r_offset;
   7282 
   7283 	      if (is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
   7284 		{
   7285 		  bfd_put_32 (input_bfd, NOP, contents + offset);
   7286 		  rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7287 		  break;
   7288 		}
   7289 
   7290 	      if ((tls_mask & TLS_GDIE) != 0)
   7291 		{
   7292 		  /* IE */
   7293 		  r_type = R_PPC_NONE;
   7294 		  insn2 = 0x7c631214;	/* add 3,3,2 */
   7295 		}
   7296 	      else
   7297 		{
   7298 		  /* LE */
   7299 		  r_type = R_PPC_TPREL16_LO;
   7300 		  rel->r_offset += d_offset;
   7301 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   7302 		}
   7303 	      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
   7304 	      bfd_put_32 (input_bfd, insn2, contents + offset);
   7305 	      /* Zap the reloc on the _tls_get_addr call too.  */
   7306 	      BFD_ASSERT (offset == rel[1].r_offset);
   7307 	      rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7308 	    }
   7309 	  break;
   7310 
   7311 	case R_PPC_TLSLD:
   7312 	  if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
   7313 	      && rel + 1 < relend)
   7314 	    {
   7315 	      unsigned int insn2;
   7316 
   7317 	      if (is_plt_seq_reloc (ELF32_R_TYPE (rel[1].r_info)))
   7318 		{
   7319 		  bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
   7320 		  rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7321 		  break;
   7322 		}
   7323 
   7324 	      for (r_symndx = 0;
   7325 		   r_symndx < symtab_hdr->sh_info;
   7326 		   r_symndx++)
   7327 		if (local_sections[r_symndx] == sec)
   7328 		  break;
   7329 	      if (r_symndx >= symtab_hdr->sh_info)
   7330 		r_symndx = STN_UNDEF;
   7331 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   7332 	      if (r_symndx != STN_UNDEF)
   7333 		rel->r_addend -= (local_syms[r_symndx].st_value
   7334 				  + sec->output_offset
   7335 				  + sec->output_section->vma);
   7336 
   7337 	      rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
   7338 	      rel->r_offset += d_offset;
   7339 	      insn2 = 0x38630000;	/* addi 3,3,0 */
   7340 	      bfd_put_32 (input_bfd, insn2,
   7341 			  contents + rel->r_offset - d_offset);
   7342 	      /* Zap the reloc on the _tls_get_addr call too.  */
   7343 	      BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
   7344 	      rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
   7345 	      goto again;
   7346 	    }
   7347 	  break;
   7348 	}
   7349 
   7350       /* Handle other relocations that tweak non-addend part of insn.  */
   7351       branch_bit = 0;
   7352       switch (r_type)
   7353 	{
   7354 	default:
   7355 	  break;
   7356 
   7357 	  /* Branch taken prediction relocations.  */
   7358 	case R_PPC_ADDR14_BRTAKEN:
   7359 	case R_PPC_REL14_BRTAKEN:
   7360 	  branch_bit = BRANCH_PREDICT_BIT;
   7361 	  /* Fall through.  */
   7362 
   7363 	  /* Branch not taken prediction relocations.  */
   7364 	case R_PPC_ADDR14_BRNTAKEN:
   7365 	case R_PPC_REL14_BRNTAKEN:
   7366 	  {
   7367 	    unsigned int insn;
   7368 
   7369 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   7370 	    insn &= ~BRANCH_PREDICT_BIT;
   7371 	    insn |= branch_bit;
   7372 
   7373 	    from = (rel->r_offset
   7374 		    + input_section->output_offset
   7375 		    + input_section->output_section->vma);
   7376 
   7377 	    /* Invert 'y' bit if not the default.  */
   7378 	    if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
   7379 	      insn ^= BRANCH_PREDICT_BIT;
   7380 
   7381 	    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   7382 	  }
   7383 	  break;
   7384 
   7385 	case R_PPC_PLT16_HA:
   7386 	  {
   7387 	    unsigned int insn;
   7388 
   7389 	    insn = bfd_get_32 (input_bfd,
   7390 			       contents + rel->r_offset - d_offset);
   7391 	    if ((insn & (0x3fu << 26)) == 15u << 26
   7392 		&& (insn & (0x1f << 16)) != 0)
   7393 	      {
   7394 		if (!bfd_link_pic (info))
   7395 		  {
   7396 		    /* Convert addis to lis.  */
   7397 		    insn &= ~(0x1f << 16);
   7398 		    bfd_put_32 (input_bfd, insn,
   7399 				contents + rel->r_offset - d_offset);
   7400 		  }
   7401 	      }
   7402 	    else if (bfd_link_pic (info))
   7403 	      info->callbacks->einfo
   7404 		(_("%P: %H: error: %s with unexpected instruction %x\n"),
   7405 		 input_bfd, input_section, rel->r_offset,
   7406 		 "R_PPC_PLT16_HA", insn);
   7407 	  }
   7408 	  break;
   7409 	}
   7410 
   7411       if (ELIMINATE_COPY_RELOCS
   7412 	  && h != NULL
   7413 	  && !h->def_regular
   7414 	  && h->protected_def
   7415 	  && ppc_elf_hash_entry (h)->has_addr16_ha
   7416 	  && ppc_elf_hash_entry (h)->has_addr16_lo
   7417 	  && htab->params->pic_fixup > 0)
   7418 	{
   7419 	  /* Convert lis;addi or lis;load/store accessing a protected
   7420 	     variable defined in a shared library to PIC.  */
   7421 	  unsigned int insn;
   7422 
   7423 	  if (r_type == R_PPC_ADDR16_HA)
   7424 	    {
   7425 	      insn = bfd_get_32 (input_bfd,
   7426 				 contents + rel->r_offset - d_offset);
   7427 	      if ((insn & (0x3fu << 26)) == (15u << 26)
   7428 		  && (insn & (0x1f << 16)) == 0 /* lis */)
   7429 		{
   7430 		  bfd_byte *p;
   7431 		  bfd_vma off;
   7432 		  bfd_vma got_addr;
   7433 
   7434 		  p = (contents + input_section->size
   7435 		       - relax_info->workaround_size
   7436 		       - relax_info->picfixup_size
   7437 		       + picfixup_size);
   7438 		  off = (p - contents) - (rel->r_offset - d_offset);
   7439 		  if (off > 0x1fffffc || (off & 3) != 0)
   7440 		    info->callbacks->einfo
   7441 		      (_("%H: fixup branch overflow\n"),
   7442 		       input_bfd, input_section, rel->r_offset);
   7443 
   7444 		  bfd_put_32 (input_bfd, B | off,
   7445 			      contents + rel->r_offset - d_offset);
   7446 		  got_addr = (htab->elf.sgot->output_section->vma
   7447 			      + htab->elf.sgot->output_offset
   7448 			      + (h->got.offset & ~1));
   7449 		  wrel->r_offset = (p - contents) + d_offset;
   7450 		  wrel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_HA);
   7451 		  wrel->r_addend = got_addr;
   7452 		  insn &= ~0xffff;
   7453 		  insn |= ((unsigned int) (got_addr + 0x8000) >> 16) & 0xffff;
   7454 		  bfd_put_32 (input_bfd, insn, p);
   7455 
   7456 		  /* Convert lis to lwz, loading address from GOT.  */
   7457 		  insn &= ~0xffff;
   7458 		  insn ^= (32u ^ 15u) << 26;
   7459 		  insn |= (insn & (0x1f << 21)) >> 5;
   7460 		  insn |= got_addr & 0xffff;
   7461 		  bfd_put_32 (input_bfd, insn, p + 4);
   7462 
   7463 		  bfd_put_32 (input_bfd, B | ((-4 - off) & 0x3ffffff), p + 8);
   7464 		  picfixup_size += 12;
   7465 
   7466 		  /* Use one of the spare relocs, so --emit-relocs
   7467 		     output is reasonable.  */
   7468 		  memmove (rel + 1, rel, (relend - rel - 1) * sizeof (*rel));
   7469 		  wrel++, rel++;
   7470 		  rel->r_offset = wrel[-1].r_offset + 4;
   7471 		  rel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_LO);
   7472 		  rel->r_addend = wrel[-1].r_addend;
   7473 
   7474 		  /* Continue on as if we had a got reloc, to output
   7475 		     dynamic reloc.  */
   7476 		  r_type = R_PPC_GOT16_LO;
   7477 		}
   7478 	      else
   7479 		_bfd_error_handler
   7480 		  /* xgettext:c-format */
   7481 		  (_("%pB(%pA+%#" PRIx64 "): error: "
   7482 		     "%s with unexpected instruction %#x"),
   7483 		   input_bfd, input_section, (uint64_t) rel->r_offset,
   7484 		   "R_PPC_ADDR16_HA", insn);
   7485 	    }
   7486 	  else if (r_type == R_PPC_ADDR16_LO)
   7487 	    {
   7488 	      insn = bfd_get_32 (input_bfd,
   7489 				 contents + rel->r_offset - d_offset);
   7490 	      if ((insn & (0x3fu << 26)) == 14u << 26    /* addi */
   7491 		  || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
   7492 		  || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
   7493 		  || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
   7494 		  || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
   7495 		  || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
   7496 		  || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
   7497 		  || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
   7498 		  || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
   7499 		  || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
   7500 		  || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
   7501 		  || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
   7502 		  || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
   7503 		  || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
   7504 		  || ((insn & (0x3fu << 26)) == 58u << 26 /* lwa,ld,lmd */
   7505 		      && (insn & 3) != 1)
   7506 		  || ((insn & (0x3fu << 26)) == 62u << 26 /* std, stmd */
   7507 		      && ((insn & 3) == 0 || (insn & 3) == 3)))
   7508 		{
   7509 		  /* Arrange to apply the reloc addend, if any.  */
   7510 		  relocation = 0;
   7511 		  unresolved_reloc = FALSE;
   7512 		  rel->r_info = ELF32_R_INFO (0, r_type);
   7513 		}
   7514 	      else
   7515 		_bfd_error_handler
   7516 		  /* xgettext:c-format */
   7517 		  (_("%pB(%pA+%#" PRIx64 "): error: "
   7518 		     "%s with unexpected instruction %#x"),
   7519 		   input_bfd, input_section, (uint64_t) rel->r_offset,
   7520 		   "R_PPC_ADDR16_LO", insn);
   7521 	    }
   7522 	}
   7523 
   7524       ifunc = NULL;
   7525       if (htab->elf.target_os != is_vxworks)
   7526 	{
   7527 	  struct plt_entry *ent;
   7528 
   7529 	  if (h != NULL)
   7530 	    {
   7531 	      if (h->type == STT_GNU_IFUNC)
   7532 		ifunc = &h->plt.plist;
   7533 	    }
   7534 	  else if (local_got_offsets != NULL
   7535 		   && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   7536 	    {
   7537 	      struct plt_entry **local_plt;
   7538 
   7539 	      local_plt = (struct plt_entry **) (local_got_offsets
   7540 						 + symtab_hdr->sh_info);
   7541 	      ifunc = local_plt + r_symndx;
   7542 	    }
   7543 
   7544 	  ent = NULL;
   7545 	  if (ifunc != NULL
   7546 	      && (!bfd_link_pic (info)
   7547 		  || is_branch_reloc (r_type)
   7548 		  || r_type == R_PPC_PLT16_LO
   7549 		  || r_type == R_PPC_PLT16_HI
   7550 		  || r_type == R_PPC_PLT16_HA))
   7551 	    {
   7552 	      addend = 0;
   7553 	      if (bfd_link_pic (info)
   7554 		  && (r_type == R_PPC_PLTREL24
   7555 		      || r_type == R_PPC_PLT16_LO
   7556 		      || r_type == R_PPC_PLT16_HI
   7557 		      || r_type == R_PPC_PLT16_HA))
   7558 		addend = rel->r_addend;
   7559 	      ent = find_plt_ent (ifunc, got2, addend);
   7560 	    }
   7561 	  if (ent != NULL)
   7562 	    {
   7563 	      if (bfd_link_pic (info)
   7564 		  && ent->sec != got2
   7565 		  && htab->plt_type != PLT_NEW
   7566 		  && (!htab->elf.dynamic_sections_created
   7567 		      || h == NULL
   7568 		      || h->dynindx == -1))
   7569 		{
   7570 		  /* Uh oh, we are going to create a pic glink stub
   7571 		     for an ifunc (here for h == NULL and later in
   7572 		     finish_dynamic_symbol for h != NULL), and
   7573 		     apparently are using code compiled with
   7574 		     -mbss-plt.  The difficulty is that -mbss-plt code
   7575 		     gives no indication via a magic PLTREL24 addend
   7576 		     whether r30 is equal to _GLOBAL_OFFSET_TABLE_ or
   7577 		     is pointing into a .got2 section (and how far
   7578 		     into .got2).  */
   7579 		    info->callbacks->einfo
   7580 		      /* xgettext:c-format */
   7581 		      (_("%X%H: unsupported bss-plt -fPIC ifunc %s\n"),
   7582 		       input_bfd, input_section, rel->r_offset, sym_name);
   7583 		}
   7584 
   7585 	      unresolved_reloc = FALSE;
   7586 	      if (htab->plt_type == PLT_NEW
   7587 		  || !htab->elf.dynamic_sections_created
   7588 		  || h == NULL
   7589 		  || h->dynindx == -1)
   7590 		relocation = (htab->glink->output_section->vma
   7591 			      + htab->glink->output_offset
   7592 			      + (ent->glink_offset & ~1));
   7593 	      else
   7594 		relocation = (htab->elf.splt->output_section->vma
   7595 			      + htab->elf.splt->output_offset
   7596 			      + ent->plt.offset);
   7597 	    }
   7598 	}
   7599 
   7600       addend = rel->r_addend;
   7601       save_unresolved_reloc = unresolved_reloc;
   7602       howto = NULL;
   7603       if (r_type < R_PPC_max)
   7604 	howto = ppc_elf_howto_table[r_type];
   7605 
   7606       tls_type = 0;
   7607       switch (r_type)
   7608 	{
   7609 	default:
   7610 	  /* xgettext:c-format */
   7611 	  _bfd_error_handler (_("%pB: %s unsupported"),
   7612 			      input_bfd, howto->name);
   7613 
   7614 	  bfd_set_error (bfd_error_bad_value);
   7615 	  ret = FALSE;
   7616 	  goto copy_reloc;
   7617 
   7618 	case R_PPC_NONE:
   7619 	case R_PPC_TLS:
   7620 	case R_PPC_TLSGD:
   7621 	case R_PPC_TLSLD:
   7622 	case R_PPC_EMB_MRKREF:
   7623 	case R_PPC_GNU_VTINHERIT:
   7624 	case R_PPC_GNU_VTENTRY:
   7625 	  goto copy_reloc;
   7626 
   7627 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   7628 	     address in the GOT as relocation value instead of the
   7629 	     symbol's value itself.  Also, create a GOT entry for the
   7630 	     symbol and put the symbol value there.  */
   7631 	case R_PPC_GOT_TLSGD16:
   7632 	case R_PPC_GOT_TLSGD16_LO:
   7633 	case R_PPC_GOT_TLSGD16_HI:
   7634 	case R_PPC_GOT_TLSGD16_HA:
   7635 	  tls_type = TLS_TLS | TLS_GD;
   7636 	  goto dogot;
   7637 
   7638 	case R_PPC_GOT_TLSLD16:
   7639 	case R_PPC_GOT_TLSLD16_LO:
   7640 	case R_PPC_GOT_TLSLD16_HI:
   7641 	case R_PPC_GOT_TLSLD16_HA:
   7642 	  tls_type = TLS_TLS | TLS_LD;
   7643 	  goto dogot;
   7644 
   7645 	case R_PPC_GOT_TPREL16:
   7646 	case R_PPC_GOT_TPREL16_LO:
   7647 	case R_PPC_GOT_TPREL16_HI:
   7648 	case R_PPC_GOT_TPREL16_HA:
   7649 	  tls_type = TLS_TLS | TLS_TPREL;
   7650 	  goto dogot;
   7651 
   7652 	case R_PPC_GOT_DTPREL16:
   7653 	case R_PPC_GOT_DTPREL16_LO:
   7654 	case R_PPC_GOT_DTPREL16_HI:
   7655 	case R_PPC_GOT_DTPREL16_HA:
   7656 	  tls_type = TLS_TLS | TLS_DTPREL;
   7657 	  goto dogot;
   7658 
   7659 	case R_PPC_GOT16:
   7660 	case R_PPC_GOT16_LO:
   7661 	case R_PPC_GOT16_HI:
   7662 	case R_PPC_GOT16_HA:
   7663 	  tls_mask = 0;
   7664 	dogot:
   7665 	  {
   7666 	    /* Relocation is to the entry for this symbol in the global
   7667 	       offset table.  */
   7668 	    bfd_vma off;
   7669 	    bfd_vma *offp;
   7670 	    unsigned long indx;
   7671 
   7672 	    if (htab->elf.sgot == NULL)
   7673 	      abort ();
   7674 
   7675 	    indx = 0;
   7676 	    if (tls_type == (TLS_TLS | TLS_LD)
   7677 		&& SYMBOL_REFERENCES_LOCAL (info, h))
   7678 	      offp = &htab->tlsld_got.offset;
   7679 	    else if (h != NULL)
   7680 	      {
   7681 		if (!htab->elf.dynamic_sections_created
   7682 		    || h->dynindx == -1
   7683 		    || SYMBOL_REFERENCES_LOCAL (info, h)
   7684 		    || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   7685 		  /* This is actually a static link, or it is a
   7686 		     -Bsymbolic link and the symbol is defined
   7687 		     locally, or the symbol was forced to be local
   7688 		     because of a version file.  */
   7689 		  ;
   7690 		else
   7691 		  {
   7692 		    indx = h->dynindx;
   7693 		    unresolved_reloc = FALSE;
   7694 		  }
   7695 		offp = &h->got.offset;
   7696 	      }
   7697 	    else
   7698 	      {
   7699 		if (local_got_offsets == NULL)
   7700 		  abort ();
   7701 		offp = &local_got_offsets[r_symndx];
   7702 	      }
   7703 
   7704 	    /* The offset must always be a multiple of 4.  We use the
   7705 	       least significant bit to record whether we have already
   7706 	       processed this entry.  */
   7707 	    off = *offp;
   7708 	    if ((off & 1) != 0)
   7709 	      off &= ~1;
   7710 	    else
   7711 	      {
   7712 		unsigned int tls_m = ((tls_mask & TLS_TLS) != 0
   7713 				      ? tls_mask & (TLS_LD | TLS_GD | TLS_DTPREL
   7714 						    | TLS_TPREL | TLS_GDIE)
   7715 				      : 0);
   7716 
   7717 		if (offp == &htab->tlsld_got.offset)
   7718 		  tls_m = TLS_LD;
   7719 		else if ((tls_m & TLS_LD) != 0
   7720 			 && SYMBOL_REFERENCES_LOCAL (info, h))
   7721 		  tls_m &= ~TLS_LD;
   7722 
   7723 		/* We might have multiple got entries for this sym.
   7724 		   Initialize them all.  */
   7725 		do
   7726 		  {
   7727 		    int tls_ty = 0;
   7728 
   7729 		    if ((tls_m & TLS_LD) != 0)
   7730 		      {
   7731 			tls_ty = TLS_TLS | TLS_LD;
   7732 			tls_m &= ~TLS_LD;
   7733 		      }
   7734 		    else if ((tls_m & TLS_GD) != 0)
   7735 		      {
   7736 			tls_ty = TLS_TLS | TLS_GD;
   7737 			tls_m &= ~TLS_GD;
   7738 		      }
   7739 		    else if ((tls_m & TLS_DTPREL) != 0)
   7740 		      {
   7741 			tls_ty = TLS_TLS | TLS_DTPREL;
   7742 			tls_m &= ~TLS_DTPREL;
   7743 		      }
   7744 		    else if ((tls_m & (TLS_TPREL | TLS_GDIE)) != 0)
   7745 		      {
   7746 			tls_ty = TLS_TLS | TLS_TPREL;
   7747 			tls_m = 0;
   7748 		      }
   7749 
   7750 		    /* Generate relocs for the dynamic linker.  */
   7751 		    if (indx != 0
   7752 			|| (bfd_link_pic (info)
   7753 			    && (h == NULL
   7754 				|| !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
   7755 			    && !(tls_ty != 0
   7756 				 && bfd_link_executable (info)
   7757 				 && SYMBOL_REFERENCES_LOCAL (info, h))))
   7758 		      {
   7759 			asection *rsec = htab->elf.srelgot;
   7760 			bfd_byte * loc;
   7761 
   7762 			if (ifunc != NULL)
   7763 			  {
   7764 			    rsec = htab->elf.irelplt;
   7765 			    if (indx == 0)
   7766 			      htab->local_ifunc_resolver = 1;
   7767 			    else if (is_static_defined (h))
   7768 			      htab->maybe_local_ifunc_resolver = 1;
   7769 			  }
   7770 			outrel.r_offset = (htab->elf.sgot->output_section->vma
   7771 					   + htab->elf.sgot->output_offset
   7772 					   + off);
   7773 			outrel.r_addend = 0;
   7774 			if (tls_ty & (TLS_LD | TLS_GD))
   7775 			  {
   7776 			    outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
   7777 			    if (tls_ty == (TLS_TLS | TLS_GD))
   7778 			      {
   7779 				loc = rsec->contents;
   7780 				loc += (rsec->reloc_count++
   7781 					* sizeof (Elf32_External_Rela));
   7782 				bfd_elf32_swap_reloca_out (output_bfd,
   7783 							   &outrel, loc);
   7784 				outrel.r_offset += 4;
   7785 				outrel.r_info
   7786 				  = ELF32_R_INFO (indx, R_PPC_DTPREL32);
   7787 			      }
   7788 			  }
   7789 			else if (tls_ty == (TLS_TLS | TLS_DTPREL))
   7790 			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
   7791 			else if (tls_ty == (TLS_TLS | TLS_TPREL))
   7792 			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
   7793 			else if (indx != 0)
   7794 			  outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
   7795 			else if (ifunc != NULL)
   7796 			  outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
   7797 			else
   7798 			  outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
   7799 			if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
   7800 			  {
   7801 			    outrel.r_addend += relocation;
   7802 			    if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   7803 			      {
   7804 				if (htab->elf.tls_sec == NULL)
   7805 				  outrel.r_addend = 0;
   7806 				else
   7807 				  outrel.r_addend -= htab->elf.tls_sec->vma;
   7808 			      }
   7809 			  }
   7810 			loc = rsec->contents;
   7811 			loc += (rsec->reloc_count++
   7812 				* sizeof (Elf32_External_Rela));
   7813 			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   7814 		      }
   7815 
   7816 		    /* Init the .got section contents if we're not
   7817 		       emitting a reloc.  */
   7818 		    else
   7819 		      {
   7820 			bfd_vma value = relocation;
   7821 
   7822 			if (tls_ty != 0)
   7823 			  {
   7824 			    if (htab->elf.tls_sec == NULL)
   7825 			      value = 0;
   7826 			    else
   7827 			      {
   7828 				if (tls_ty & TLS_LD)
   7829 				  value = 0;
   7830 				else
   7831 				  value -= htab->elf.tls_sec->vma + DTP_OFFSET;
   7832 				if (tls_ty & TLS_TPREL)
   7833 				  value += DTP_OFFSET - TP_OFFSET;
   7834 			      }
   7835 
   7836 			    if (tls_ty & (TLS_LD | TLS_GD))
   7837 			      {
   7838 				bfd_put_32 (input_bfd, value,
   7839 					    htab->elf.sgot->contents + off + 4);
   7840 				value = 1;
   7841 			      }
   7842 			  }
   7843 			bfd_put_32 (input_bfd, value,
   7844 				    htab->elf.sgot->contents + off);
   7845 		      }
   7846 
   7847 		    off += 4;
   7848 		    if (tls_ty & (TLS_LD | TLS_GD))
   7849 		      off += 4;
   7850 		  }
   7851 		while (tls_m != 0);
   7852 
   7853 		off = *offp;
   7854 		*offp = off | 1;
   7855 	      }
   7856 
   7857 	    if (off >= (bfd_vma) -2)
   7858 	      abort ();
   7859 
   7860 	    if ((tls_type & TLS_TLS) != 0)
   7861 	      {
   7862 		if (tls_type != (TLS_TLS | TLS_LD))
   7863 		  {
   7864 		    if ((tls_mask & TLS_LD) != 0
   7865 			&& !SYMBOL_REFERENCES_LOCAL (info, h))
   7866 		      off += 8;
   7867 		    if (tls_type != (TLS_TLS | TLS_GD))
   7868 		      {
   7869 			if ((tls_mask & TLS_GD) != 0)
   7870 			  off += 8;
   7871 			if (tls_type != (TLS_TLS | TLS_DTPREL))
   7872 			  {
   7873 			    if ((tls_mask & TLS_DTPREL) != 0)
   7874 			      off += 4;
   7875 			  }
   7876 		      }
   7877 		  }
   7878 	      }
   7879 
   7880 	    /* If here for a picfixup, we're done.  */
   7881 	    if (r_type != ELF32_R_TYPE (rel->r_info))
   7882 	      goto copy_reloc;
   7883 
   7884 	    relocation = (htab->elf.sgot->output_section->vma
   7885 			  + htab->elf.sgot->output_offset
   7886 			  + off
   7887 			  - SYM_VAL (htab->elf.hgot));
   7888 
   7889 	    /* Addends on got relocations don't make much sense.
   7890 	       x+off@got is actually x@got+off, and since the got is
   7891 	       generated by a hash table traversal, the value in the
   7892 	       got at entry m+n bears little relation to the entry m.  */
   7893 	    if (addend != 0)
   7894 	      info->callbacks->einfo
   7895 		/* xgettext:c-format */
   7896 		(_("%H: non-zero addend on %s reloc against `%s'\n"),
   7897 		 input_bfd, input_section, rel->r_offset,
   7898 		 howto->name,
   7899 		 sym_name);
   7900 	  }
   7901 	  break;
   7902 
   7903 	  /* Relocations that need no special processing.  */
   7904 	case R_PPC_LOCAL24PC:
   7905 	  /* It makes no sense to point a local relocation
   7906 	     at a symbol not in this object.  */
   7907 	  if (unresolved_reloc)
   7908 	    {
   7909 	      (*info->callbacks->undefined_symbol) (info,
   7910 						    h->root.root.string,
   7911 						    input_bfd,
   7912 						    input_section,
   7913 						    rel->r_offset,
   7914 						    TRUE);
   7915 	      goto copy_reloc;
   7916 	    }
   7917 	  if (h != NULL && h->type == STT_GNU_IFUNC && bfd_link_pic (info))
   7918 	    {
   7919 	      /* @local on an ifunc does not really make sense since
   7920 		 the ifunc resolver can take you anywhere.  More
   7921 		 seriously, calls to ifuncs must go through a plt call
   7922 		 stub, and for pic the plt call stubs uses r30 to
   7923 		 access the PLT.  The problem is that a call that is
   7924 		 local won't have the +32k reloc addend trick marking
   7925 		 -fPIC code, so the linker won't know whether r30 is
   7926 		 _GLOBAL_OFFSET_TABLE_ or pointing into a .got2 section.  */
   7927 	      /* xgettext:c-format */
   7928 	      info->callbacks->einfo (_("%X%H: @local call to ifunc %s\n"),
   7929 				      input_bfd, input_section, rel->r_offset,
   7930 				      h->root.root.string);
   7931 	    }
   7932 	  break;
   7933 
   7934 	case R_PPC_DTPREL16:
   7935 	case R_PPC_DTPREL16_LO:
   7936 	case R_PPC_DTPREL16_HI:
   7937 	case R_PPC_DTPREL16_HA:
   7938 	  if (htab->elf.tls_sec != NULL)
   7939 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   7940 	  break;
   7941 
   7942 	  /* Relocations that may need to be propagated if this is a shared
   7943 	     object.  */
   7944 	case R_PPC_TPREL16:
   7945 	case R_PPC_TPREL16_LO:
   7946 	case R_PPC_TPREL16_HI:
   7947 	case R_PPC_TPREL16_HA:
   7948 	  if (h != NULL
   7949 	      && h->root.type == bfd_link_hash_undefweak
   7950 	      && h->dynindx == -1)
   7951 	    {
   7952 	      /* Make this relocation against an undefined weak symbol
   7953 		 resolve to zero.  This is really just a tweak, since
   7954 		 code using weak externs ought to check that they are
   7955 		 defined before using them.  */
   7956 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   7957 	      unsigned int insn = bfd_get_32 (input_bfd, p);
   7958 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 2);
   7959 	      if (insn != 0)
   7960 		bfd_put_32 (input_bfd, insn, p);
   7961 	      break;
   7962 	    }
   7963 	  if (htab->elf.tls_sec != NULL)
   7964 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   7965 	  /* The TPREL16 relocs shouldn't really be used in shared
   7966 	     libs or with non-local symbols as that will result in
   7967 	     DT_TEXTREL being set, but support them anyway.  */
   7968 	  goto dodyn;
   7969 
   7970 	case R_PPC_TPREL32:
   7971 	  if (htab->elf.tls_sec != NULL)
   7972 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   7973 	  goto dodyn;
   7974 
   7975 	case R_PPC_DTPREL32:
   7976 	  if (htab->elf.tls_sec != NULL)
   7977 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   7978 	  goto dodyn;
   7979 
   7980 	case R_PPC_DTPMOD32:
   7981 	  relocation = 1;
   7982 	  addend = 0;
   7983 	  goto dodyn;
   7984 
   7985 	case R_PPC_REL16:
   7986 	case R_PPC_REL16_LO:
   7987 	case R_PPC_REL16_HI:
   7988 	case R_PPC_REL16_HA:
   7989 	case R_PPC_REL16DX_HA:
   7990 	  break;
   7991 
   7992 	case R_PPC_REL32:
   7993 	  if (h == NULL || h == htab->elf.hgot)
   7994 	    break;
   7995 	  /* fall through */
   7996 
   7997 	case R_PPC_ADDR32:
   7998 	case R_PPC_ADDR16:
   7999 	case R_PPC_ADDR16_LO:
   8000 	case R_PPC_ADDR16_HI:
   8001 	case R_PPC_ADDR16_HA:
   8002 	case R_PPC_UADDR32:
   8003 	case R_PPC_UADDR16:
   8004 	  goto dodyn;
   8005 
   8006 	case R_PPC_VLE_REL8:
   8007 	case R_PPC_VLE_REL15:
   8008 	case R_PPC_VLE_REL24:
   8009 	case R_PPC_REL24:
   8010 	case R_PPC_REL14:
   8011 	case R_PPC_REL14_BRTAKEN:
   8012 	case R_PPC_REL14_BRNTAKEN:
   8013 	  /* If these relocations are not to a named symbol, they can be
   8014 	     handled right here, no need to bother the dynamic linker.  */
   8015 	  if (SYMBOL_CALLS_LOCAL (info, h)
   8016 	      || h == htab->elf.hgot)
   8017 	    break;
   8018 	  /* fall through */
   8019 
   8020 	case R_PPC_ADDR24:
   8021 	case R_PPC_ADDR14:
   8022 	case R_PPC_ADDR14_BRTAKEN:
   8023 	case R_PPC_ADDR14_BRNTAKEN:
   8024 	  if (h != NULL && !bfd_link_pic (info))
   8025 	    break;
   8026 	  /* fall through */
   8027 
   8028 	dodyn:
   8029 	  if ((input_section->flags & SEC_ALLOC) == 0
   8030 	      || is_vxworks_tls)
   8031 	    break;
   8032 
   8033 	  if (bfd_link_pic (info)
   8034 	      ? ((h == NULL
   8035 		  || h->dyn_relocs != NULL)
   8036 		 && ((h != NULL && pc_dynrelocs (h))
   8037 		     || must_be_dyn_reloc (info, r_type)))
   8038 	      : (h != NULL
   8039 		 && h->dyn_relocs != NULL))
   8040 	    {
   8041 	      int skip;
   8042 	      bfd_byte *loc;
   8043 	      asection *sreloc;
   8044 	      long indx = 0;
   8045 
   8046 #ifdef DEBUG
   8047 	      fprintf (stderr, "ppc_elf_relocate_section needs to "
   8048 		       "create relocation for %s\n",
   8049 		       (h && h->root.root.string
   8050 			? h->root.root.string : "<unknown>"));
   8051 #endif
   8052 
   8053 	      /* When generating a shared object, these relocations
   8054 		 are copied into the output file to be resolved at run
   8055 		 time.  */
   8056 	      skip = 0;
   8057 	      outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
   8058 							 input_section,
   8059 							 rel->r_offset);
   8060 	      if (outrel.r_offset == (bfd_vma) -1
   8061 		  || outrel.r_offset == (bfd_vma) -2)
   8062 		skip = (int) outrel.r_offset;
   8063 	      outrel.r_offset += (input_section->output_section->vma
   8064 				  + input_section->output_offset);
   8065 
   8066 	      /* Optimize unaligned reloc use.  */
   8067 	      if ((r_type == R_PPC_ADDR32 && (outrel.r_offset & 3) != 0)
   8068 		  || (r_type == R_PPC_UADDR32 && (outrel.r_offset & 3) == 0))
   8069 		r_type ^= R_PPC_ADDR32 ^ R_PPC_UADDR32;
   8070 	      if ((r_type == R_PPC_ADDR16 && (outrel.r_offset & 1) != 0)
   8071 		  || (r_type == R_PPC_UADDR16 && (outrel.r_offset & 1) == 0))
   8072 		r_type ^= R_PPC_ADDR16 ^ R_PPC_UADDR16;
   8073 
   8074 	      if (skip)
   8075 		memset (&outrel, 0, sizeof outrel);
   8076 	      else if (!SYMBOL_REFERENCES_LOCAL (info, h))
   8077 		{
   8078 		  indx = h->dynindx;
   8079 		  BFD_ASSERT (indx != -1);
   8080 		  unresolved_reloc = FALSE;
   8081 		  outrel.r_info = ELF32_R_INFO (indx, r_type);
   8082 		  outrel.r_addend = rel->r_addend;
   8083 		}
   8084 	      else
   8085 		{
   8086 		  outrel.r_addend = relocation + rel->r_addend;
   8087 
   8088 		  if (r_type != R_PPC_ADDR32)
   8089 		    {
   8090 		      if (ifunc != NULL)
   8091 			{
   8092 			  /* If we get here when building a static
   8093 			     executable, then the libc startup function
   8094 			     responsible for applying indirect function
   8095 			     relocations is going to complain about
   8096 			     the reloc type.
   8097 			     If we get here when building a dynamic
   8098 			     executable, it will be because we have
   8099 			     a text relocation.  The dynamic loader
   8100 			     will set the text segment writable and
   8101 			     non-executable to apply text relocations.
   8102 			     So we'll segfault when trying to run the
   8103 			     indirection function to resolve the reloc.  */
   8104 			  info->callbacks->einfo
   8105 			    /* xgettext:c-format */
   8106 			    (_("%H: relocation %s for indirect "
   8107 			       "function %s unsupported\n"),
   8108 			     input_bfd, input_section, rel->r_offset,
   8109 			     howto->name,
   8110 			     sym_name);
   8111 			  ret = FALSE;
   8112 			}
   8113 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   8114 			;
   8115 		      else if (sec == NULL || sec->owner == NULL)
   8116 			{
   8117 			  bfd_set_error (bfd_error_bad_value);
   8118 			  ret = FALSE;
   8119 			}
   8120 		      else
   8121 			{
   8122 			  asection *osec;
   8123 
   8124 			  /* We are turning this relocation into one
   8125 			     against a section symbol.  It would be
   8126 			     proper to subtract the symbol's value,
   8127 			     osec->vma, from the emitted reloc addend,
   8128 			     but ld.so expects buggy relocs.
   8129 			     FIXME: Why not always use a zero index?  */
   8130 			  osec = sec->output_section;
   8131 			  if ((osec->flags & SEC_THREAD_LOCAL) != 0)
   8132 			    {
   8133 			      osec = htab->elf.tls_sec;
   8134 			      indx = 0;
   8135 			    }
   8136 			  else
   8137 			    {
   8138 			      indx = elf_section_data (osec)->dynindx;
   8139 			      if (indx == 0)
   8140 				{
   8141 				  osec = htab->elf.text_index_section;
   8142 				  indx = elf_section_data (osec)->dynindx;
   8143 				}
   8144 			      BFD_ASSERT (indx != 0);
   8145 			    }
   8146 
   8147 			  /* ld.so doesn't expect buggy TLS relocs.
   8148 			     Don't leave the symbol value in the
   8149 			     addend for them.  */
   8150 			  if (IS_PPC_TLS_RELOC (r_type))
   8151 			    outrel.r_addend -= osec->vma;
   8152 			}
   8153 
   8154 		      outrel.r_info = ELF32_R_INFO (indx, r_type);
   8155 		    }
   8156 		  else if (ifunc != NULL)
   8157 		    outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
   8158 		  else
   8159 		    outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
   8160 		}
   8161 
   8162 	      sreloc = elf_section_data (input_section)->sreloc;
   8163 	      if (ifunc)
   8164 		{
   8165 		  sreloc = htab->elf.irelplt;
   8166 		  if (indx == 0)
   8167 		    htab->local_ifunc_resolver = 1;
   8168 		  else if (is_static_defined (h))
   8169 		    htab->maybe_local_ifunc_resolver = 1;
   8170 		}
   8171 	      if (sreloc == NULL)
   8172 		return FALSE;
   8173 
   8174 	      loc = sreloc->contents;
   8175 	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
   8176 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   8177 
   8178 	      if (skip == -1)
   8179 		goto copy_reloc;
   8180 
   8181 	      /* This reloc will be computed at runtime.  Clear the memory
   8182 		 so that it contains a predictable value for prelink.  */
   8183 	      if (!skip)
   8184 		{
   8185 		  relocation = howto->pc_relative ? outrel.r_offset : 0;
   8186 		  addend = 0;
   8187 		  break;
   8188 		}
   8189 	    }
   8190 	  break;
   8191 
   8192 	case R_PPC_RELAX_PLT:
   8193 	case R_PPC_RELAX_PLTREL24:
   8194 	  if (h != NULL)
   8195 	    {
   8196 	      struct plt_entry *ent;
   8197 	      bfd_vma got2_addend = 0;
   8198 
   8199 	      if (r_type == R_PPC_RELAX_PLTREL24)
   8200 		{
   8201 		  if (bfd_link_pic (info))
   8202 		    got2_addend = addend;
   8203 		  addend = 0;
   8204 		}
   8205 	      ent = find_plt_ent (&h->plt.plist, got2, got2_addend);
   8206 	      if (htab->plt_type == PLT_NEW)
   8207 		relocation = (htab->glink->output_section->vma
   8208 			      + htab->glink->output_offset
   8209 			      + ent->glink_offset);
   8210 	      else
   8211 		relocation = (htab->elf.splt->output_section->vma
   8212 			      + htab->elf.splt->output_offset
   8213 			      + ent->plt.offset);
   8214 	    }
   8215 	  /* Fall through.  */
   8216 
   8217 	case R_PPC_RELAX:
   8218 	  {
   8219 	    const int *stub;
   8220 	    size_t size;
   8221 	    size_t insn_offset = rel->r_offset;
   8222 	    unsigned int insn;
   8223 
   8224 	    if (bfd_link_pic (info))
   8225 	      {
   8226 		relocation -= (input_section->output_section->vma
   8227 			       + input_section->output_offset
   8228 			       + rel->r_offset - 4);
   8229 		stub = shared_stub_entry;
   8230 		bfd_put_32 (input_bfd, stub[0], contents + insn_offset - 12);
   8231 		bfd_put_32 (input_bfd, stub[1], contents + insn_offset - 8);
   8232 		bfd_put_32 (input_bfd, stub[2], contents + insn_offset - 4);
   8233 		stub += 3;
   8234 		size = ARRAY_SIZE (shared_stub_entry) - 3;
   8235 	      }
   8236 	    else
   8237 	      {
   8238 		stub = stub_entry;
   8239 		size = ARRAY_SIZE (stub_entry);
   8240 	      }
   8241 
   8242 	    relocation += addend;
   8243 	    if (bfd_link_relocatable (info))
   8244 	      relocation = 0;
   8245 
   8246 	    /* First insn is HA, second is LO.  */
   8247 	    insn = *stub++;
   8248 	    insn |= ((relocation + 0x8000) >> 16) & 0xffff;
   8249 	    bfd_put_32 (input_bfd, insn, contents + insn_offset);
   8250 	    insn_offset += 4;
   8251 
   8252 	    insn = *stub++;
   8253 	    insn |= relocation & 0xffff;
   8254 	    bfd_put_32 (input_bfd, insn, contents + insn_offset);
   8255 	    insn_offset += 4;
   8256 	    size -= 2;
   8257 
   8258 	    while (size != 0)
   8259 	      {
   8260 		insn = *stub++;
   8261 		--size;
   8262 		bfd_put_32 (input_bfd, insn, contents + insn_offset);
   8263 		insn_offset += 4;
   8264 	      }
   8265 
   8266 	    /* Rewrite the reloc and convert one of the trailing nop
   8267 	       relocs to describe this relocation.  */
   8268 	    BFD_ASSERT (ELF32_R_TYPE (relend[-1].r_info) == R_PPC_NONE);
   8269 	    /* The relocs are at the bottom 2 bytes */
   8270 	    wrel->r_offset = rel->r_offset + d_offset;
   8271 	    wrel->r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
   8272 	    wrel->r_addend = rel->r_addend;
   8273 	    memmove (wrel + 1, wrel, (relend - wrel - 1) * sizeof (*wrel));
   8274 	    wrel++, rel++;
   8275 	    wrel->r_offset += 4;
   8276 	    wrel->r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
   8277 	  }
   8278 	  continue;
   8279 
   8280 	  /* Indirect .sdata relocation.  */
   8281 	case R_PPC_EMB_SDAI16:
   8282 	  BFD_ASSERT (htab->sdata[0].section != NULL);
   8283 	  if (!is_static_defined (htab->sdata[0].sym))
   8284 	    {
   8285 	      unresolved_reloc = TRUE;
   8286 	      break;
   8287 	    }
   8288 	  relocation
   8289 	    = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
   8290 						 h, relocation, rel);
   8291 	  addend = 0;
   8292 	  break;
   8293 
   8294 	  /* Indirect .sdata2 relocation.  */
   8295 	case R_PPC_EMB_SDA2I16:
   8296 	  BFD_ASSERT (htab->sdata[1].section != NULL);
   8297 	  if (!is_static_defined (htab->sdata[1].sym))
   8298 	    {
   8299 	      unresolved_reloc = TRUE;
   8300 	      break;
   8301 	    }
   8302 	  relocation
   8303 	    = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
   8304 						 h, relocation, rel);
   8305 	  addend = 0;
   8306 	  break;
   8307 
   8308 	  /* Handle the TOC16 reloc.  We want to use the offset within the .got
   8309 	     section, not the actual VMA.  This is appropriate when generating
   8310 	     an embedded ELF object, for which the .got section acts like the
   8311 	     AIX .toc section.  */
   8312 	case R_PPC_TOC16:			/* phony GOT16 relocations */
   8313 	  if (sec == NULL || sec->output_section == NULL)
   8314 	    {
   8315 	      unresolved_reloc = TRUE;
   8316 	      break;
   8317 	    }
   8318 	  BFD_ASSERT (strcmp (bfd_section_name (sec), ".got") == 0
   8319 		      || strcmp (bfd_section_name (sec), ".cgot") == 0);
   8320 
   8321 	  addend -= sec->output_section->vma + sec->output_offset + 0x8000;
   8322 	  break;
   8323 
   8324 	case R_PPC_PLTREL24:
   8325 	  if (h != NULL && ifunc == NULL)
   8326 	    {
   8327 	      struct plt_entry *ent;
   8328 
   8329 	      ent = find_plt_ent (&h->plt.plist, got2,
   8330 				  bfd_link_pic (info) ? addend : 0);
   8331 	      if (ent == NULL
   8332 		  || htab->elf.splt == NULL)
   8333 		{
   8334 		  /* We didn't make a PLT entry for this symbol.  This
   8335 		     happens when statically linking PIC code, or when
   8336 		     using -Bsymbolic.  */
   8337 		}
   8338 	      else
   8339 		{
   8340 		  /* Relocation is to the entry for this symbol in the
   8341 		     procedure linkage table.  */
   8342 		  unresolved_reloc = FALSE;
   8343 		  if (htab->plt_type == PLT_NEW)
   8344 		    relocation = (htab->glink->output_section->vma
   8345 				  + htab->glink->output_offset
   8346 				  + ent->glink_offset);
   8347 		  else
   8348 		    relocation = (htab->elf.splt->output_section->vma
   8349 				  + htab->elf.splt->output_offset
   8350 				  + ent->plt.offset);
   8351 		}
   8352 	    }
   8353 
   8354 	  /* R_PPC_PLTREL24 is rather special.  If non-zero, the
   8355 	     addend specifies the GOT pointer offset within .got2.
   8356 	     Don't apply it to the relocation field.  */
   8357 	  addend = 0;
   8358 	  break;
   8359 
   8360 	case R_PPC_PLTSEQ:
   8361 	case R_PPC_PLTCALL:
   8362 	case R_PPC_PLT16_LO:
   8363 	case R_PPC_PLT16_HI:
   8364 	case R_PPC_PLT16_HA:
   8365 	  plt_list = NULL;
   8366 	  if (h != NULL)
   8367 	    plt_list = &h->plt.plist;
   8368 	  else if (ifunc != NULL)
   8369 	    plt_list = ifunc;
   8370 	  else if (local_got_offsets != NULL)
   8371 	    {
   8372 	      struct plt_entry **local_plt;
   8373 	      local_plt = (struct plt_entry **) (local_got_offsets
   8374 						 + symtab_hdr->sh_info);
   8375 	      plt_list = local_plt + r_symndx;
   8376 	    }
   8377 	  unresolved_reloc = TRUE;
   8378 	  if (plt_list != NULL)
   8379 	    {
   8380 	      struct plt_entry *ent;
   8381 
   8382 	      ent = find_plt_ent (plt_list, got2,
   8383 				  bfd_link_pic (info) ? addend : 0);
   8384 	      if (ent != NULL && ent->plt.offset != (bfd_vma) -1)
   8385 		{
   8386 		  asection *plt;
   8387 
   8388 		  unresolved_reloc = FALSE;
   8389 		  plt = htab->elf.splt;
   8390 		  if (!htab->elf.dynamic_sections_created
   8391 		      || h == NULL
   8392 		      || h->dynindx == -1)
   8393 		    {
   8394 		      if (ifunc != NULL)
   8395 			plt = htab->elf.iplt;
   8396 		      else
   8397 			plt = htab->pltlocal;
   8398 		    }
   8399 		  relocation = (plt->output_section->vma
   8400 				+ plt->output_offset
   8401 				+ ent->plt.offset);
   8402 		  if (bfd_link_pic (info))
   8403 		    {
   8404 		      bfd_vma got = 0;
   8405 
   8406 		      if (ent->addend >= 32768)
   8407 			got = (ent->addend
   8408 			       + ent->sec->output_section->vma
   8409 			       + ent->sec->output_offset);
   8410 		      else
   8411 			got = SYM_VAL (htab->elf.hgot);
   8412 		      relocation -= got;
   8413 		    }
   8414 		}
   8415 	    }
   8416 	  addend = 0;
   8417 	  break;
   8418 
   8419 	  /* Relocate against _SDA_BASE_.  */
   8420 	case R_PPC_SDAREL16:
   8421 	  {
   8422 	    const char *name;
   8423 	    struct elf_link_hash_entry *sda = htab->sdata[0].sym;
   8424 
   8425 	    if (sec == NULL
   8426 		|| sec->output_section == NULL
   8427 		|| !is_static_defined (sda))
   8428 	      {
   8429 		unresolved_reloc = TRUE;
   8430 		break;
   8431 	      }
   8432 	    addend -= SYM_VAL (sda);
   8433 
   8434 	    name = bfd_section_name (sec->output_section);
   8435 	    if (!(strcmp (name, ".sdata") == 0
   8436 		  || strcmp (name, ".sbss") == 0))
   8437 	      {
   8438 		_bfd_error_handler
   8439 		  /* xgettext:c-format */
   8440 		  (_("%pB: the target (%s) of a %s relocation is "
   8441 		     "in the wrong output section (%s)"),
   8442 		   input_bfd,
   8443 		   sym_name,
   8444 		   howto->name,
   8445 		   name);
   8446 	      }
   8447 	  }
   8448 	  break;
   8449 
   8450 	  /* Relocate against _SDA2_BASE_.  */
   8451 	case R_PPC_EMB_SDA2REL:
   8452 	  {
   8453 	    const char *name;
   8454 	    struct elf_link_hash_entry *sda = htab->sdata[1].sym;
   8455 
   8456 	    if (sec == NULL
   8457 		|| sec->output_section == NULL
   8458 		|| !is_static_defined (sda))
   8459 	      {
   8460 		unresolved_reloc = TRUE;
   8461 		break;
   8462 	      }
   8463 	    addend -= SYM_VAL (sda);
   8464 
   8465 	    name = bfd_section_name (sec->output_section);
   8466 	    if (!(strcmp (name, ".sdata2") == 0
   8467 		  || strcmp (name, ".sbss2") == 0))
   8468 	      {
   8469 		_bfd_error_handler
   8470 		  /* xgettext:c-format */
   8471 		  (_("%pB: the target (%s) of a %s relocation is "
   8472 		     "in the wrong output section (%s)"),
   8473 		   input_bfd,
   8474 		   sym_name,
   8475 		   howto->name,
   8476 		   name);
   8477 	      }
   8478 	  }
   8479 	  break;
   8480 
   8481 	case R_PPC_VLE_LO16A:
   8482 	  relocation = relocation + addend;
   8483 	  ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8484 			       contents + rel->r_offset, relocation,
   8485 			       split16a_type, htab->params->vle_reloc_fixup);
   8486 	  goto copy_reloc;
   8487 
   8488 	case R_PPC_VLE_LO16D:
   8489 	  relocation = relocation + addend;
   8490 	  ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8491 			       contents + rel->r_offset, relocation,
   8492 			       split16d_type, htab->params->vle_reloc_fixup);
   8493 	  goto copy_reloc;
   8494 
   8495 	case R_PPC_VLE_HI16A:
   8496 	  relocation = (relocation + addend) >> 16;
   8497 	  ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8498 			       contents + rel->r_offset, relocation,
   8499 			       split16a_type, htab->params->vle_reloc_fixup);
   8500 	  goto copy_reloc;
   8501 
   8502 	case R_PPC_VLE_HI16D:
   8503 	  relocation = (relocation + addend) >> 16;
   8504 	  ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8505 			       contents + rel->r_offset, relocation,
   8506 			       split16d_type, htab->params->vle_reloc_fixup);
   8507 	  goto copy_reloc;
   8508 
   8509 	case R_PPC_VLE_HA16A:
   8510 	  relocation = (relocation + addend + 0x8000) >> 16;
   8511 	  ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8512 			       contents + rel->r_offset, relocation,
   8513 			       split16a_type, htab->params->vle_reloc_fixup);
   8514 	  goto copy_reloc;
   8515 
   8516 	case R_PPC_VLE_HA16D:
   8517 	  relocation = (relocation + addend + 0x8000) >> 16;
   8518 	  ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8519 			       contents + rel->r_offset, relocation,
   8520 			       split16d_type, htab->params->vle_reloc_fixup);
   8521 	  goto copy_reloc;
   8522 
   8523 	  /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
   8524 	case R_PPC_EMB_SDA21:
   8525 	case R_PPC_VLE_SDA21:
   8526 	case R_PPC_EMB_RELSDA:
   8527 	case R_PPC_VLE_SDA21_LO:
   8528 	  {
   8529 	    const char *name;
   8530 	    int reg;
   8531 	    unsigned int insn;
   8532 	    struct elf_link_hash_entry *sda = NULL;
   8533 
   8534 	    if (sec == NULL || sec->output_section == NULL)
   8535 	      {
   8536 		unresolved_reloc = TRUE;
   8537 		break;
   8538 	      }
   8539 
   8540 	    name = bfd_section_name (sec->output_section);
   8541 	    if (strcmp (name, ".sdata") == 0
   8542 		|| strcmp (name, ".sbss") == 0)
   8543 	      {
   8544 		reg = 13;
   8545 		sda = htab->sdata[0].sym;
   8546 	      }
   8547 	    else if (strcmp (name, ".sdata2") == 0
   8548 		     || strcmp (name, ".sbss2") == 0)
   8549 	      {
   8550 		reg = 2;
   8551 		sda = htab->sdata[1].sym;
   8552 	      }
   8553 	    else if (strcmp (name, ".PPC.EMB.sdata0") == 0
   8554 		     || strcmp (name, ".PPC.EMB.sbss0") == 0)
   8555 	      {
   8556 		reg = 0;
   8557 	      }
   8558 	    else
   8559 	      {
   8560 		_bfd_error_handler
   8561 		  /* xgettext:c-format */
   8562 		  (_("%pB: the target (%s) of a %s relocation is "
   8563 		     "in the wrong output section (%s)"),
   8564 		   input_bfd,
   8565 		   sym_name,
   8566 		   howto->name,
   8567 		   name);
   8568 
   8569 		bfd_set_error (bfd_error_bad_value);
   8570 		ret = FALSE;
   8571 		goto copy_reloc;
   8572 	      }
   8573 
   8574 	    if (sda != NULL)
   8575 	      {
   8576 		if (!is_static_defined (sda))
   8577 		  {
   8578 		    unresolved_reloc = TRUE;
   8579 		    break;
   8580 		  }
   8581 		addend -= SYM_VAL (sda);
   8582 	      }
   8583 
   8584 	    if (r_type == R_PPC_EMB_RELSDA)
   8585 	      break;
   8586 
   8587 	    /* The PowerPC Embedded Application Binary Interface
   8588 	       version 1.0 insanely chose to specify R_PPC_EMB_SDA21
   8589 	       operating on a 24-bit field at r_offset.  GNU as and
   8590 	       GNU ld have always assumed R_PPC_EMB_SDA21 operates on
   8591 	       a 32-bit bit insn at r_offset.  Cope with object file
   8592 	       producers that possibly comply with the EABI in
   8593 	       generating an odd r_offset for big-endian objects.  */
   8594 	    if (r_type == R_PPC_EMB_SDA21)
   8595 	      rel->r_offset &= ~1;
   8596 
   8597 	    insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   8598 	    if (reg == 0
   8599 		&& (r_type == R_PPC_VLE_SDA21
   8600 		    || r_type == R_PPC_VLE_SDA21_LO))
   8601 	      {
   8602 		relocation = relocation + addend;
   8603 		addend = 0;
   8604 
   8605 		/* Force e_li insn, keeping RT from original insn.  */
   8606 		insn &= 0x1f << 21;
   8607 		insn |= 28u << 26;
   8608 
   8609 		/* We have an li20 field, bits 17..20, 11..15, 21..31.  */
   8610 		/* Top 4 bits of value to 17..20.  */
   8611 		insn |= (relocation & 0xf0000) >> 5;
   8612 		/* Next 5 bits of the value to 11..15.  */
   8613 		insn |= (relocation & 0xf800) << 5;
   8614 		/* And the final 11 bits of the value to bits 21 to 31.  */
   8615 		insn |= relocation & 0x7ff;
   8616 
   8617 		bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   8618 
   8619 		if (r_type == R_PPC_VLE_SDA21
   8620 		    && ((relocation + 0x80000) & 0xffffffff) > 0x100000)
   8621 		  goto overflow;
   8622 		goto copy_reloc;
   8623 	      }
   8624 	    /* Fill in register field.  */
   8625 	    insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
   8626 	    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   8627 	  }
   8628 	  break;
   8629 
   8630 	case R_PPC_VLE_SDAREL_LO16A:
   8631 	case R_PPC_VLE_SDAREL_LO16D:
   8632 	case R_PPC_VLE_SDAREL_HI16A:
   8633 	case R_PPC_VLE_SDAREL_HI16D:
   8634 	case R_PPC_VLE_SDAREL_HA16A:
   8635 	case R_PPC_VLE_SDAREL_HA16D:
   8636 	  {
   8637 	    bfd_vma value;
   8638 	    const char *name;
   8639 	    struct elf_link_hash_entry *sda = NULL;
   8640 
   8641 	    if (sec == NULL || sec->output_section == NULL)
   8642 	      {
   8643 		unresolved_reloc = TRUE;
   8644 		break;
   8645 	      }
   8646 
   8647 	    name = bfd_section_name (sec->output_section);
   8648 	    if (strcmp (name, ".sdata") == 0
   8649 		|| strcmp (name, ".sbss") == 0)
   8650 	      sda = htab->sdata[0].sym;
   8651 	    else if (strcmp (name, ".sdata2") == 0
   8652 		     || strcmp (name, ".sbss2") == 0)
   8653 	      sda = htab->sdata[1].sym;
   8654 	    else
   8655 	      {
   8656 		_bfd_error_handler
   8657 		  /* xgettext:c-format */
   8658 		  (_("%pB: the target (%s) of a %s relocation is "
   8659 		     "in the wrong output section (%s)"),
   8660 		   input_bfd,
   8661 		   sym_name,
   8662 		   howto->name,
   8663 		   name);
   8664 
   8665 		bfd_set_error (bfd_error_bad_value);
   8666 		ret = FALSE;
   8667 		goto copy_reloc;
   8668 	      }
   8669 
   8670 	    if (sda == NULL || !is_static_defined (sda))
   8671 	      {
   8672 		unresolved_reloc = TRUE;
   8673 		break;
   8674 	      }
   8675 	    value = relocation + addend - SYM_VAL (sda);
   8676 
   8677 	    if (r_type == R_PPC_VLE_SDAREL_LO16A)
   8678 	      ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8679 				   contents + rel->r_offset, value,
   8680 				   split16a_type,
   8681 				   htab->params->vle_reloc_fixup);
   8682 	    else if (r_type == R_PPC_VLE_SDAREL_LO16D)
   8683 	      ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8684 				   contents + rel->r_offset, value,
   8685 				   split16d_type,
   8686 				   htab->params->vle_reloc_fixup);
   8687 	    else if (r_type == R_PPC_VLE_SDAREL_HI16A)
   8688 	      {
   8689 		value = value >> 16;
   8690 		ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8691 				     contents + rel->r_offset, value,
   8692 				     split16a_type,
   8693 				     htab->params->vle_reloc_fixup);
   8694 	      }
   8695 	    else if (r_type == R_PPC_VLE_SDAREL_HI16D)
   8696 	      {
   8697 		value = value >> 16;
   8698 		ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8699 				     contents + rel->r_offset, value,
   8700 				     split16d_type,
   8701 				     htab->params->vle_reloc_fixup);
   8702 	      }
   8703 	    else if (r_type == R_PPC_VLE_SDAREL_HA16A)
   8704 	      {
   8705 		value = (value + 0x8000) >> 16;
   8706 		ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8707 				     contents + rel->r_offset, value,
   8708 				     split16a_type,
   8709 				     htab->params->vle_reloc_fixup);
   8710 	      }
   8711 	    else if (r_type == R_PPC_VLE_SDAREL_HA16D)
   8712 	      {
   8713 		value = (value + 0x8000) >> 16;
   8714 		ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
   8715 				     contents + rel->r_offset, value,
   8716 				     split16d_type,
   8717 				     htab->params->vle_reloc_fixup);
   8718 	      }
   8719 	  }
   8720 	  goto copy_reloc;
   8721 
   8722 	case R_PPC_VLE_ADDR20:
   8723 	  ppc_elf_vle_split20 (output_bfd, contents + rel->r_offset, relocation);
   8724 	  goto copy_reloc;
   8725 
   8726 	  /* Relocate against the beginning of the section.  */
   8727 	case R_PPC_SECTOFF:
   8728 	case R_PPC_SECTOFF_LO:
   8729 	case R_PPC_SECTOFF_HI:
   8730 	case R_PPC_SECTOFF_HA:
   8731 	  if (sec == NULL || sec->output_section == NULL)
   8732 	    {
   8733 	      unresolved_reloc = TRUE;
   8734 	      break;
   8735 	    }
   8736 	  addend -= sec->output_section->vma;
   8737 	  break;
   8738 
   8739 	  /* Negative relocations.  */
   8740 	case R_PPC_EMB_NADDR32:
   8741 	case R_PPC_EMB_NADDR16:
   8742 	case R_PPC_EMB_NADDR16_LO:
   8743 	case R_PPC_EMB_NADDR16_HI:
   8744 	case R_PPC_EMB_NADDR16_HA:
   8745 	  addend -= 2 * relocation;
   8746 	  break;
   8747 
   8748 	case R_PPC_COPY:
   8749 	case R_PPC_GLOB_DAT:
   8750 	case R_PPC_JMP_SLOT:
   8751 	case R_PPC_RELATIVE:
   8752 	case R_PPC_IRELATIVE:
   8753 	case R_PPC_PLT32:
   8754 	case R_PPC_PLTREL32:
   8755 	case R_PPC_ADDR30:
   8756 	case R_PPC_EMB_RELSEC16:
   8757 	case R_PPC_EMB_RELST_LO:
   8758 	case R_PPC_EMB_RELST_HI:
   8759 	case R_PPC_EMB_RELST_HA:
   8760 	case R_PPC_EMB_BIT_FLD:
   8761 	  /* xgettext:c-format */
   8762 	  _bfd_error_handler (_("%pB: %s unsupported"),
   8763 			      input_bfd, howto->name);
   8764 
   8765 	  bfd_set_error (bfd_error_invalid_operation);
   8766 	  ret = FALSE;
   8767 	  goto copy_reloc;
   8768 	}
   8769 
   8770       switch (r_type)
   8771 	{
   8772 	default:
   8773 	  break;
   8774 
   8775 	case R_PPC_TPREL16_HA:
   8776 	  if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
   8777 	    {
   8778 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   8779 	      bfd_put_32 (input_bfd, NOP, p);
   8780 	    }
   8781 	  break;
   8782 
   8783 	case R_PPC_TPREL16_LO:
   8784 	  if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
   8785 	    {
   8786 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   8787 	      unsigned int insn = bfd_get_32 (input_bfd, p);
   8788 	      insn &= ~(0x1f << 16);
   8789 	      insn |= 2 << 16;
   8790 	      bfd_put_32 (input_bfd, insn, p);
   8791 	    }
   8792 	  break;
   8793 	}
   8794 
   8795       switch (r_type)
   8796 	{
   8797 	default:
   8798 	  break;
   8799 
   8800 	case R_PPC_PLTCALL:
   8801 	  if (unresolved_reloc)
   8802 	    {
   8803 	      bfd_byte *p = contents + rel->r_offset;
   8804 	      unsigned int insn = bfd_get_32 (input_bfd, p);
   8805 	      insn &= 1;
   8806 	      bfd_put_32 (input_bfd, B | insn, p);
   8807 	      unresolved_reloc = save_unresolved_reloc;
   8808 	      r_type = R_PPC_REL24;
   8809 	      howto = ppc_elf_howto_table[r_type];
   8810 	    }
   8811 	  else if (htab->plt_type != PLT_NEW)
   8812 	    info->callbacks->einfo
   8813 	      (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
   8814 	       input_bfd, input_section, rel->r_offset,
   8815 	       howto->name);
   8816 	  break;
   8817 
   8818 	case R_PPC_PLTSEQ:
   8819 	case R_PPC_PLT16_HA:
   8820 	case R_PPC_PLT16_LO:
   8821 	  if (unresolved_reloc)
   8822 	    {
   8823 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   8824 	      bfd_put_32 (input_bfd, NOP, p);
   8825 	      unresolved_reloc = FALSE;
   8826 	      r_type = R_PPC_NONE;
   8827 	      howto = ppc_elf_howto_table[r_type];
   8828 	    }
   8829 	  else if (htab->plt_type != PLT_NEW)
   8830 	    info->callbacks->einfo
   8831 	      (_("%X%P: %H: %s relocation unsupported for bss-plt\n"),
   8832 	       input_bfd, input_section, rel->r_offset,
   8833 	       howto->name);
   8834 	  break;
   8835 	}
   8836 
   8837       /* Do any further special processing.  */
   8838       switch (r_type)
   8839 	{
   8840 	default:
   8841 	  break;
   8842 
   8843 	case R_PPC_ADDR16_HA:
   8844 	case R_PPC_REL16_HA:
   8845 	case R_PPC_REL16DX_HA:
   8846 	case R_PPC_SECTOFF_HA:
   8847 	case R_PPC_TPREL16_HA:
   8848 	case R_PPC_DTPREL16_HA:
   8849 	case R_PPC_EMB_NADDR16_HA:
   8850 	case R_PPC_EMB_RELST_HA:
   8851 	  /* It's just possible that this symbol is a weak symbol
   8852 	     that's not actually defined anywhere.  In that case,
   8853 	     'sec' would be NULL, and we should leave the symbol
   8854 	     alone (it will be set to zero elsewhere in the link).  */
   8855 	  if (sec == NULL)
   8856 	    break;
   8857 	  /* Fall through.  */
   8858 
   8859 	case R_PPC_PLT16_HA:
   8860 	case R_PPC_GOT16_HA:
   8861 	case R_PPC_GOT_TLSGD16_HA:
   8862 	case R_PPC_GOT_TLSLD16_HA:
   8863 	case R_PPC_GOT_TPREL16_HA:
   8864 	case R_PPC_GOT_DTPREL16_HA:
   8865 	  /* Add 0x10000 if sign bit in 0:15 is set.
   8866 	     Bits 0:15 are not used.  */
   8867 	  addend += 0x8000;
   8868 	  break;
   8869 
   8870 	case R_PPC_ADDR16:
   8871 	case R_PPC_ADDR16_LO:
   8872 	case R_PPC_GOT16:
   8873 	case R_PPC_GOT16_LO:
   8874 	case R_PPC_SDAREL16:
   8875 	case R_PPC_SECTOFF:
   8876 	case R_PPC_SECTOFF_LO:
   8877 	case R_PPC_DTPREL16:
   8878 	case R_PPC_DTPREL16_LO:
   8879 	case R_PPC_TPREL16:
   8880 	case R_PPC_TPREL16_LO:
   8881 	case R_PPC_GOT_TLSGD16:
   8882 	case R_PPC_GOT_TLSGD16_LO:
   8883 	case R_PPC_GOT_TLSLD16:
   8884 	case R_PPC_GOT_TLSLD16_LO:
   8885 	case R_PPC_GOT_DTPREL16:
   8886 	case R_PPC_GOT_DTPREL16_LO:
   8887 	case R_PPC_GOT_TPREL16:
   8888 	case R_PPC_GOT_TPREL16_LO:
   8889 	  {
   8890 	    /* The 32-bit ABI lacks proper relocations to deal with
   8891 	       certain 64-bit instructions.  Prevent damage to bits
   8892 	       that make up part of the insn opcode.  */
   8893 	    unsigned int insn, mask, lobit;
   8894 
   8895 	    insn = bfd_get_32 (input_bfd,
   8896 			       contents + rel->r_offset - d_offset);
   8897 	    mask = 0;
   8898 	    if (is_insn_ds_form (insn))
   8899 	      mask = 3;
   8900 	    else if (is_insn_dq_form (insn))
   8901 	      mask = 15;
   8902 	    else
   8903 	      break;
   8904 	    relocation += addend;
   8905 	    addend = insn & mask;
   8906 	    lobit = mask & relocation;
   8907 	    if (lobit != 0)
   8908 	      {
   8909 		relocation ^= lobit;
   8910 		info->callbacks->einfo
   8911 		  /* xgettext:c-format */
   8912 		  (_("%H: error: %s against `%s' not a multiple of %u\n"),
   8913 		   input_bfd, input_section, rel->r_offset,
   8914 		   howto->name, sym_name, mask + 1);
   8915 		bfd_set_error (bfd_error_bad_value);
   8916 		ret = FALSE;
   8917 	      }
   8918 	  }
   8919 	  break;
   8920 	}
   8921 
   8922 #ifdef DEBUG
   8923       fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
   8924 	       "offset = %ld, addend = %ld\n",
   8925 	       howto->name,
   8926 	       (int) r_type,
   8927 	       sym_name,
   8928 	       r_symndx,
   8929 	       (long) rel->r_offset,
   8930 	       (long) addend);
   8931 #endif
   8932 
   8933       if (unresolved_reloc
   8934 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   8935 	       && h->def_dynamic)
   8936 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   8937 				      rel->r_offset) != (bfd_vma) -1)
   8938 	{
   8939 	  info->callbacks->einfo
   8940 	    /* xgettext:c-format */
   8941 	    (_("%H: unresolvable %s relocation against symbol `%s'\n"),
   8942 	     input_bfd, input_section, rel->r_offset,
   8943 	     howto->name,
   8944 	     sym_name);
   8945 	  ret = FALSE;
   8946 	}
   8947 
   8948       /* 16-bit fields in insns mostly have signed values, but a
   8949 	 few insns have 16-bit unsigned values.  Really, we should
   8950 	 have different reloc types.  */
   8951       if (howto->complain_on_overflow != complain_overflow_dont
   8952 	  && howto->dst_mask == 0xffff
   8953 	  && (input_section->flags & SEC_CODE) != 0)
   8954 	{
   8955 	  enum complain_overflow complain = complain_overflow_signed;
   8956 
   8957 	  if ((elf_section_flags (input_section) & SHF_PPC_VLE) == 0)
   8958 	    {
   8959 	      unsigned int insn;
   8960 
   8961 	      insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   8962 	      if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
   8963 		complain = complain_overflow_bitfield;
   8964 	      else if ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
   8965 		       || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
   8966 		       || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
   8967 		complain = complain_overflow_unsigned;
   8968 	    }
   8969 	  if (howto->complain_on_overflow != complain)
   8970 	    {
   8971 	      alt_howto = *howto;
   8972 	      alt_howto.complain_on_overflow = complain;
   8973 	      howto = &alt_howto;
   8974 	    }
   8975 	}
   8976 
   8977       if (r_type == R_PPC_REL16DX_HA)
   8978 	{
   8979 	  /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
   8980 	  if (rel->r_offset + 4 > input_section->size)
   8981 	    r = bfd_reloc_outofrange;
   8982 	  else
   8983 	    {
   8984 	      unsigned int insn;
   8985 
   8986 	      relocation += addend;
   8987 	      relocation -= (rel->r_offset
   8988 			     + input_section->output_offset
   8989 			     + input_section->output_section->vma);
   8990 	      relocation >>= 16;
   8991 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   8992 	      insn &= ~0x1fffc1;
   8993 	      insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
   8994 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   8995 	      r = bfd_reloc_ok;
   8996 	    }
   8997 	}
   8998       else
   8999 	r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
   9000 				      rel->r_offset, relocation, addend);
   9001 
   9002       if (r != bfd_reloc_ok)
   9003 	{
   9004 	  if (r == bfd_reloc_overflow)
   9005 	    {
   9006 	    overflow:
   9007 	      /* On code like "if (foo) foo();" don't report overflow
   9008 		 on a branch to zero when foo is undefined.  */
   9009 	      if (!warned
   9010 		  && !(h != NULL
   9011 		       && (h->root.type == bfd_link_hash_undefweak
   9012 			   || h->root.type == bfd_link_hash_undefined)
   9013 		       && is_branch_reloc (r_type)))
   9014 		info->callbacks->reloc_overflow
   9015 		  (info, (h ? &h->root : NULL), sym_name, howto->name,
   9016 		   rel->r_addend, input_bfd, input_section, rel->r_offset);
   9017 	    }
   9018 	  else
   9019 	    {
   9020 	      info->callbacks->einfo
   9021 		/* xgettext:c-format */
   9022 		(_("%H: %s reloc against `%s': error %d\n"),
   9023 		 input_bfd, input_section, rel->r_offset,
   9024 		 howto->name, sym_name, (int) r);
   9025 	      ret = FALSE;
   9026 	    }
   9027 	}
   9028     copy_reloc:
   9029       if (wrel != rel)
   9030 	*wrel = *rel;
   9031     }
   9032 
   9033   if (wrel != rel)
   9034     {
   9035       Elf_Internal_Shdr *rel_hdr;
   9036       size_t deleted = rel - wrel;
   9037 
   9038       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
   9039       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   9040       if (rel_hdr->sh_size == 0)
   9041 	{
   9042 	  /* It is too late to remove an empty reloc section.  Leave
   9043 	     one NONE reloc.
   9044 	     ??? What is wrong with an empty section???  */
   9045 	  rel_hdr->sh_size = rel_hdr->sh_entsize;
   9046 	  deleted -= 1;
   9047 	  wrel++;
   9048 	}
   9049       relend = wrel;
   9050       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
   9051       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   9052       input_section->reloc_count -= deleted;
   9053     }
   9054 
   9055 #ifdef DEBUG
   9056   fprintf (stderr, "\n");
   9057 #endif
   9058 
   9059   if (input_section->sec_info_type == SEC_INFO_TYPE_TARGET
   9060       && input_section->size != input_section->rawsize
   9061       && (strcmp (input_section->output_section->name, ".init") == 0
   9062 	  || strcmp (input_section->output_section->name, ".fini") == 0))
   9063     {
   9064       /* Branch around the trampolines.  */
   9065       unsigned int insn = B + input_section->size - input_section->rawsize;
   9066       bfd_put_32 (input_bfd, insn, contents + input_section->rawsize);
   9067     }
   9068 
   9069   if (htab->params->ppc476_workaround
   9070       && input_section->sec_info_type == SEC_INFO_TYPE_TARGET
   9071       && (!bfd_link_relocatable (info)
   9072 	  || (input_section->output_section->alignment_power
   9073 	      >= htab->params->pagesize_p2)))
   9074     {
   9075       bfd_vma start_addr, end_addr, addr;
   9076       bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
   9077 
   9078       if (relax_info->workaround_size != 0)
   9079 	{
   9080 	  bfd_byte *p;
   9081 	  unsigned int n;
   9082 	  bfd_byte fill[4];
   9083 
   9084 	  bfd_put_32 (input_bfd, BA, fill);
   9085 	  p = contents + input_section->size - relax_info->workaround_size;
   9086 	  n = relax_info->workaround_size >> 2;
   9087 	  while (n--)
   9088 	    {
   9089 	      memcpy (p, fill, 4);
   9090 	      p += 4;
   9091 	    }
   9092 	}
   9093 
   9094       /* The idea is: Replace the last instruction on a page with a
   9095 	 branch to a patch area.  Put the insn there followed by a
   9096 	 branch back to the next page.  Complicated a little by
   9097 	 needing to handle moved conditional branches, and by not
   9098 	 wanting to touch data-in-text.  */
   9099 
   9100       start_addr = (input_section->output_section->vma
   9101 		    + input_section->output_offset);
   9102       end_addr = (start_addr + input_section->size
   9103 		  - relax_info->workaround_size);
   9104       for (addr = ((start_addr & -pagesize) + pagesize - 4);
   9105 	   addr < end_addr;
   9106 	   addr += pagesize)
   9107 	{
   9108 	  bfd_vma offset = addr - start_addr;
   9109 	  Elf_Internal_Rela *lo, *hi;
   9110 	  bfd_boolean is_data;
   9111 	  bfd_vma patch_off, patch_addr;
   9112 	  unsigned int insn;
   9113 
   9114 	  /* Do we have a data reloc at this offset?  If so, leave
   9115 	     the word alone.  */
   9116 	  is_data = FALSE;
   9117 	  lo = relocs;
   9118 	  hi = relend;
   9119 	  rel = NULL;
   9120 	  while (lo < hi)
   9121 	    {
   9122 	      rel = lo + (hi - lo) / 2;
   9123 	      if (rel->r_offset < offset)
   9124 		lo = rel + 1;
   9125 	      else if (rel->r_offset > offset + 3)
   9126 		hi = rel;
   9127 	      else
   9128 		{
   9129 		  switch (ELF32_R_TYPE (rel->r_info))
   9130 		    {
   9131 		    case R_PPC_ADDR32:
   9132 		    case R_PPC_UADDR32:
   9133 		    case R_PPC_REL32:
   9134 		    case R_PPC_ADDR30:
   9135 		      is_data = TRUE;
   9136 		      break;
   9137 		    default:
   9138 		      break;
   9139 		    }
   9140 		  break;
   9141 		}
   9142 	    }
   9143 	  if (is_data)
   9144 	    continue;
   9145 
   9146 	  /* Some instructions can be left alone too.  Unconditional
   9147 	     branches, except for bcctr with BO=0x14 (bctr, bctrl),
   9148 	     avoid the icache failure.
   9149 
   9150 	     The problem occurs due to prefetch across a page boundary
   9151 	     where stale instructions can be fetched from the next
   9152 	     page, and the mechanism for flushing these bad
   9153 	     instructions fails under certain circumstances.  The
   9154 	     unconditional branches:
   9155 	     1) Branch: b, bl, ba, bla,
   9156 	     2) Branch Conditional: bc, bca, bcl, bcla,
   9157 	     3) Branch Conditional to Link Register: bclr, bclrl,
   9158 	     where (2) and (3) have BO=0x14 making them unconditional,
   9159 	     prevent the bad prefetch because the prefetch itself is
   9160 	     affected by these instructions.  This happens even if the
   9161 	     instruction is not executed.
   9162 
   9163 	     A bctr example:
   9164 	     .
   9165 	     .	lis 9,new_page@ha
   9166 	     .	addi 9,9,new_page@l
   9167 	     .	mtctr 9
   9168 	     .	bctr
   9169 	     .	nop
   9170 	     .	nop
   9171 	     . new_page:
   9172 	     .
   9173 	     The bctr is not predicted taken due to ctr not being
   9174 	     ready, so prefetch continues on past the bctr into the
   9175 	     new page which might have stale instructions.  If they
   9176 	     fail to be flushed, then they will be executed after the
   9177 	     bctr executes.  Either of the following modifications
   9178 	     prevent the bad prefetch from happening in the first
   9179 	     place:
   9180 	     .
   9181 	     .	lis 9,new_page@ha	 lis 9,new_page@ha
   9182 	     .	addi 9,9,new_page@l	 addi 9,9,new_page@l
   9183 	     .	mtctr 9			 mtctr 9
   9184 	     .	bctr			 bctr
   9185 	     .	nop			 b somewhere_else
   9186 	     .	b somewhere_else	 nop
   9187 	     . new_page:		new_page:
   9188 	     .  */
   9189 	  insn = bfd_get_32 (input_bfd, contents + offset);
   9190 	  if ((insn & (0x3fu << 26)) == (18u << 26)	    /* b,bl,ba,bla */
   9191 	      || ((insn & (0x3fu << 26)) == (16u << 26)	    /* bc,bcl,bca,bcla*/
   9192 		  && (insn & (0x14 << 21)) == (0x14 << 21)) /*	 with BO=0x14 */
   9193 	      || ((insn & (0x3fu << 26)) == (19u << 26)
   9194 		  && (insn & (0x3ff << 1)) == (16u << 1)    /* bclr,bclrl */
   9195 		  && (insn & (0x14 << 21)) == (0x14 << 21)))/*	 with BO=0x14 */
   9196 	    continue;
   9197 
   9198 	  patch_addr = (start_addr + input_section->size
   9199 			- relax_info->workaround_size);
   9200 	  patch_addr = (patch_addr + 15) & -16;
   9201 	  patch_off = patch_addr - start_addr;
   9202 	  bfd_put_32 (input_bfd, B + patch_off - offset, contents + offset);
   9203 
   9204 	  if (rel != NULL
   9205 	      && rel->r_offset >= offset
   9206 	      && rel->r_offset < offset + 4)
   9207 	    {
   9208 	      asection *sreloc;
   9209 
   9210 	      /* If the insn we are patching had a reloc, adjust the
   9211 		 reloc r_offset so that the reloc applies to the moved
   9212 		 location.  This matters for -r and --emit-relocs.  */
   9213 	      if (rel + 1 != relend)
   9214 		{
   9215 		  Elf_Internal_Rela tmp = *rel;
   9216 
   9217 		  /* Keep the relocs sorted by r_offset.  */
   9218 		  memmove (rel, rel + 1, (relend - (rel + 1)) * sizeof (*rel));
   9219 		  relend[-1] = tmp;
   9220 		}
   9221 	      relend[-1].r_offset += patch_off - offset;
   9222 
   9223 	      /* Adjust REL16 addends too.  */
   9224 	      switch (ELF32_R_TYPE (relend[-1].r_info))
   9225 		{
   9226 		case R_PPC_REL16:
   9227 		case R_PPC_REL16_LO:
   9228 		case R_PPC_REL16_HI:
   9229 		case R_PPC_REL16_HA:
   9230 		  relend[-1].r_addend += patch_off - offset;
   9231 		  break;
   9232 		default:
   9233 		  break;
   9234 		}
   9235 
   9236 	      /* If we are building a PIE or shared library with
   9237 		 non-PIC objects, perhaps we had a dynamic reloc too?
   9238 		 If so, the dynamic reloc must move with the insn.  */
   9239 	      sreloc = elf_section_data (input_section)->sreloc;
   9240 	      if (sreloc != NULL)
   9241 		{
   9242 		  Elf32_External_Rela *slo, *shi, *srelend;
   9243 		  bfd_vma soffset;
   9244 
   9245 		  slo = (Elf32_External_Rela *) sreloc->contents;
   9246 		  shi = srelend = slo + sreloc->reloc_count;
   9247 		  soffset = (offset + input_section->output_section->vma
   9248 			     + input_section->output_offset);
   9249 		  while (slo < shi)
   9250 		    {
   9251 		      Elf32_External_Rela *srel = slo + (shi - slo) / 2;
   9252 		      bfd_elf32_swap_reloca_in (output_bfd, (bfd_byte *) srel,
   9253 						&outrel);
   9254 		      if (outrel.r_offset < soffset)
   9255 			slo = srel + 1;
   9256 		      else if (outrel.r_offset > soffset + 3)
   9257 			shi = srel;
   9258 		      else
   9259 			{
   9260 			  if (srel + 1 != srelend)
   9261 			    {
   9262 			      memmove (srel, srel + 1,
   9263 				       (srelend - (srel + 1)) * sizeof (*srel));
   9264 			      srel = srelend - 1;
   9265 			    }
   9266 			  outrel.r_offset += patch_off - offset;
   9267 			  bfd_elf32_swap_reloca_out (output_bfd, &outrel,
   9268 						     (bfd_byte *) srel);
   9269 			  break;
   9270 			}
   9271 		    }
   9272 		}
   9273 	    }
   9274 	  else
   9275 	    rel = NULL;
   9276 
   9277 	  if ((insn & (0x3fu << 26)) == (16u << 26) /* bc */
   9278 	      && (insn & 2) == 0 /* relative */)
   9279 	    {
   9280 	      bfd_vma delta = ((insn & 0xfffc) ^ 0x8000) - 0x8000;
   9281 
   9282 	      delta += offset - patch_off;
   9283 	      if (bfd_link_relocatable (info) && rel != NULL)
   9284 		delta = 0;
   9285 	      if (!bfd_link_relocatable (info) && rel != NULL)
   9286 		{
   9287 		  enum elf_ppc_reloc_type r_type;
   9288 
   9289 		  r_type = ELF32_R_TYPE (relend[-1].r_info);
   9290 		  if (r_type == R_PPC_REL14_BRTAKEN)
   9291 		    insn |= BRANCH_PREDICT_BIT;
   9292 		  else if (r_type == R_PPC_REL14_BRNTAKEN)
   9293 		    insn &= ~BRANCH_PREDICT_BIT;
   9294 		  else
   9295 		    BFD_ASSERT (r_type == R_PPC_REL14);
   9296 
   9297 		  if ((r_type == R_PPC_REL14_BRTAKEN
   9298 		       || r_type == R_PPC_REL14_BRNTAKEN)
   9299 		      && delta + 0x8000 < 0x10000
   9300 		      && (bfd_signed_vma) delta < 0)
   9301 		    insn ^= BRANCH_PREDICT_BIT;
   9302 		}
   9303 	      if (delta + 0x8000 < 0x10000)
   9304 		{
   9305 		  bfd_put_32 (input_bfd,
   9306 			      (insn & ~0xfffc) | (delta & 0xfffc),
   9307 			      contents + patch_off);
   9308 		  patch_off += 4;
   9309 		  bfd_put_32 (input_bfd,
   9310 			      B | ((offset + 4 - patch_off) & 0x3fffffc),
   9311 			      contents + patch_off);
   9312 		  patch_off += 4;
   9313 		}
   9314 	      else
   9315 		{
   9316 		  if (rel != NULL)
   9317 		    {
   9318 		      unsigned int r_sym = ELF32_R_SYM (relend[-1].r_info);
   9319 
   9320 		      relend[-1].r_offset += 8;
   9321 		      relend[-1].r_info = ELF32_R_INFO (r_sym, R_PPC_REL24);
   9322 		    }
   9323 		  bfd_put_32 (input_bfd,
   9324 			      (insn & ~0xfffc) | 8,
   9325 			      contents + patch_off);
   9326 		  patch_off += 4;
   9327 		  bfd_put_32 (input_bfd,
   9328 			      B | ((offset + 4 - patch_off) & 0x3fffffc),
   9329 			      contents + patch_off);
   9330 		  patch_off += 4;
   9331 		  bfd_put_32 (input_bfd,
   9332 			      B | ((delta - 8) & 0x3fffffc),
   9333 			      contents + patch_off);
   9334 		  patch_off += 4;
   9335 		}
   9336 	    }
   9337 	  else
   9338 	    {
   9339 	      bfd_put_32 (input_bfd, insn, contents + patch_off);
   9340 	      patch_off += 4;
   9341 	      bfd_put_32 (input_bfd,
   9342 			  B | ((offset + 4 - patch_off) & 0x3fffffc),
   9343 			  contents + patch_off);
   9344 	      patch_off += 4;
   9345 	    }
   9346 	  BFD_ASSERT (patch_off <= input_section->size);
   9347 	  relax_info->workaround_size = input_section->size - patch_off;
   9348 	}
   9349     }
   9350 
   9351   return ret;
   9352 }
   9353 
   9354 /* Write out the PLT relocs and entries for H.  */
   9356 
   9357 static bfd_boolean
   9358 write_global_sym_plt (struct elf_link_hash_entry *h, void *inf)
   9359 {
   9360   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   9361   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   9362   struct plt_entry *ent;
   9363   bfd_boolean doneone;
   9364 
   9365   doneone = FALSE;
   9366   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   9367     if (ent->plt.offset != (bfd_vma) -1)
   9368       {
   9369 	if (!doneone)
   9370 	  {
   9371 	    Elf_Internal_Rela rela;
   9372 	    bfd_byte *loc;
   9373 	    bfd_vma reloc_index;
   9374 	    asection *plt = htab->elf.splt;
   9375 	    asection *relplt = htab->elf.srelplt;
   9376 
   9377 	    if (htab->plt_type == PLT_NEW
   9378 		|| !htab->elf.dynamic_sections_created
   9379 		|| h->dynindx == -1)
   9380 	      reloc_index = ent->plt.offset / 4;
   9381 	    else
   9382 	      {
   9383 		reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
   9384 			       / htab->plt_slot_size);
   9385 		if (reloc_index > PLT_NUM_SINGLE_ENTRIES
   9386 		    && htab->plt_type == PLT_OLD)
   9387 		  reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
   9388 	      }
   9389 
   9390 	    /* This symbol has an entry in the procedure linkage table.
   9391 	       Set it up.  */
   9392 	    if (htab->plt_type == PLT_VXWORKS
   9393 		&& htab->elf.dynamic_sections_created
   9394 		&& h->dynindx != -1)
   9395 	      {
   9396 		bfd_vma got_offset;
   9397 		const bfd_vma *plt_entry;
   9398 
   9399 		/* The first three entries in .got.plt are reserved.  */
   9400 		got_offset = (reloc_index + 3) * 4;
   9401 
   9402 		/* Use the right PLT. */
   9403 		plt_entry = bfd_link_pic (info) ? ppc_elf_vxworks_pic_plt_entry
   9404 			    : ppc_elf_vxworks_plt_entry;
   9405 
   9406 		/* Fill in the .plt on VxWorks.  */
   9407 		if (bfd_link_pic (info))
   9408 		  {
   9409 		    bfd_put_32 (info->output_bfd,
   9410 				plt_entry[0] | PPC_HA (got_offset),
   9411 				plt->contents + ent->plt.offset + 0);
   9412 		    bfd_put_32 (info->output_bfd,
   9413 				plt_entry[1] | PPC_LO (got_offset),
   9414 				plt->contents + ent->plt.offset + 4);
   9415 		  }
   9416 		else
   9417 		  {
   9418 		    bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
   9419 
   9420 		    bfd_put_32 (info->output_bfd,
   9421 				plt_entry[0] | PPC_HA (got_loc),
   9422 				plt->contents + ent->plt.offset + 0);
   9423 		    bfd_put_32 (info->output_bfd,
   9424 				plt_entry[1] | PPC_LO (got_loc),
   9425 				plt->contents + ent->plt.offset + 4);
   9426 		  }
   9427 
   9428 		bfd_put_32 (info->output_bfd, plt_entry[2],
   9429 			    plt->contents + ent->plt.offset + 8);
   9430 		bfd_put_32 (info->output_bfd, plt_entry[3],
   9431 			    plt->contents + ent->plt.offset + 12);
   9432 
   9433 		/* This instruction is an immediate load.  The value loaded is
   9434 		   the byte offset of the R_PPC_JMP_SLOT relocation from the
   9435 		   start of the .rela.plt section.  The value is stored in the
   9436 		   low-order 16 bits of the load instruction.  */
   9437 		/* NOTE: It appears that this is now an index rather than a
   9438 		   prescaled offset.  */
   9439 		bfd_put_32 (info->output_bfd,
   9440 			    plt_entry[4] | reloc_index,
   9441 			    plt->contents + ent->plt.offset + 16);
   9442 		/* This instruction is a PC-relative branch whose target is
   9443 		   the start of the PLT section.  The address of this branch
   9444 		   instruction is 20 bytes beyond the start of this PLT entry.
   9445 		   The address is encoded in bits 6-29, inclusive.  The value
   9446 		   stored is right-shifted by two bits, permitting a 26-bit
   9447 		   offset.  */
   9448 		bfd_put_32 (info->output_bfd,
   9449 			    (plt_entry[5]
   9450 			     | (-(ent->plt.offset + 20) & 0x03fffffc)),
   9451 			    plt->contents + ent->plt.offset + 20);
   9452 		bfd_put_32 (info->output_bfd, plt_entry[6],
   9453 			    plt->contents + ent->plt.offset + 24);
   9454 		bfd_put_32 (info->output_bfd, plt_entry[7],
   9455 			    plt->contents + ent->plt.offset + 28);
   9456 
   9457 		/* Fill in the GOT entry corresponding to this PLT slot with
   9458 		   the address immediately after the "bctr" instruction
   9459 		   in this PLT entry.  */
   9460 		bfd_put_32 (info->output_bfd, (plt->output_section->vma
   9461 					       + plt->output_offset
   9462 					       + ent->plt.offset + 16),
   9463 			    htab->elf.sgotplt->contents + got_offset);
   9464 
   9465 		if (!bfd_link_pic (info))
   9466 		  {
   9467 		    /* Fill in a couple of entries in .rela.plt.unloaded.  */
   9468 		    loc = htab->srelplt2->contents
   9469 		      + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
   9470 			  * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
   9471 			 * sizeof (Elf32_External_Rela));
   9472 
   9473 		    /* Provide the @ha relocation for the first instruction.  */
   9474 		    rela.r_offset = (plt->output_section->vma
   9475 				     + plt->output_offset
   9476 				     + ent->plt.offset + 2);
   9477 		    rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
   9478 						R_PPC_ADDR16_HA);
   9479 		    rela.r_addend = got_offset;
   9480 		    bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
   9481 		    loc += sizeof (Elf32_External_Rela);
   9482 
   9483 		    /* Provide the @l relocation for the second instruction.  */
   9484 		    rela.r_offset = (plt->output_section->vma
   9485 				     + plt->output_offset
   9486 				     + ent->plt.offset + 6);
   9487 		    rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
   9488 						R_PPC_ADDR16_LO);
   9489 		    rela.r_addend = got_offset;
   9490 		    bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
   9491 		    loc += sizeof (Elf32_External_Rela);
   9492 
   9493 		    /* Provide a relocation for the GOT entry corresponding to this
   9494 		       PLT slot.  Point it at the middle of the .plt entry.  */
   9495 		    rela.r_offset = (htab->elf.sgotplt->output_section->vma
   9496 				     + htab->elf.sgotplt->output_offset
   9497 				     + got_offset);
   9498 		    rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
   9499 						R_PPC_ADDR32);
   9500 		    rela.r_addend = ent->plt.offset + 16;
   9501 		    bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
   9502 		  }
   9503 
   9504 		/* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
   9505 		   In particular, the offset for the relocation is not the
   9506 		   address of the PLT entry for this function, as specified
   9507 		   by the ABI.  Instead, the offset is set to the address of
   9508 		   the GOT slot for this function.  See EABI 4.4.4.1.  */
   9509 		rela.r_offset = (htab->elf.sgotplt->output_section->vma
   9510 				 + htab->elf.sgotplt->output_offset
   9511 				 + got_offset);
   9512 		rela.r_addend = 0;
   9513 	      }
   9514 	    else
   9515 	      {
   9516 		rela.r_addend = 0;
   9517 		if (!htab->elf.dynamic_sections_created
   9518 		    || h->dynindx == -1)
   9519 		  {
   9520 		    if (h->type == STT_GNU_IFUNC)
   9521 		      {
   9522 			plt = htab->elf.iplt;
   9523 			relplt = htab->elf.irelplt;
   9524 		      }
   9525 		    else
   9526 		      {
   9527 			plt = htab->pltlocal;
   9528 			relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
   9529 		      }
   9530 		    if (h->def_regular
   9531 			&& (h->root.type == bfd_link_hash_defined
   9532 			    || h->root.type == bfd_link_hash_defweak))
   9533 		      rela.r_addend = SYM_VAL (h);
   9534 		  }
   9535 
   9536 		if (relplt == NULL)
   9537 		  {
   9538 		    loc = plt->contents + ent->plt.offset;
   9539 		    bfd_put_32 (info->output_bfd, rela.r_addend, loc);
   9540 		  }
   9541 		else
   9542 		  {
   9543 		    rela.r_offset = (plt->output_section->vma
   9544 				     + plt->output_offset
   9545 				     + ent->plt.offset);
   9546 
   9547 		    if (htab->plt_type == PLT_OLD
   9548 			|| !htab->elf.dynamic_sections_created
   9549 			|| h->dynindx == -1)
   9550 		      {
   9551 			/* We don't need to fill in the .plt.  The ppc dynamic
   9552 			   linker will fill it in.  */
   9553 		      }
   9554 		    else
   9555 		      {
   9556 			bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
   9557 				       + htab->glink->output_section->vma
   9558 				       + htab->glink->output_offset);
   9559 			bfd_put_32 (info->output_bfd, val,
   9560 				    plt->contents + ent->plt.offset);
   9561 		      }
   9562 		  }
   9563 	      }
   9564 
   9565 	    if (relplt != NULL)
   9566 	      {
   9567 		/* Fill in the entry in the .rela.plt section.  */
   9568 		if (!htab->elf.dynamic_sections_created
   9569 		    || h->dynindx == -1)
   9570 		  {
   9571 		    if (h->type == STT_GNU_IFUNC)
   9572 		      rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
   9573 		    else
   9574 		      rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
   9575 		    loc = relplt->contents + (relplt->reloc_count++
   9576 					      * sizeof (Elf32_External_Rela));
   9577 		    htab->local_ifunc_resolver = 1;
   9578 		  }
   9579 		else
   9580 		  {
   9581 		    rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
   9582 		    loc = relplt->contents + (reloc_index
   9583 					      * sizeof (Elf32_External_Rela));
   9584 		    if (h->type == STT_GNU_IFUNC && is_static_defined (h))
   9585 		      htab->maybe_local_ifunc_resolver = 1;
   9586 		  }
   9587 		bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
   9588 	      }
   9589 	    doneone = TRUE;
   9590 	  }
   9591 
   9592 	if (htab->plt_type == PLT_NEW
   9593 	    || !htab->elf.dynamic_sections_created
   9594 	    || h->dynindx == -1)
   9595 	  {
   9596 	    unsigned char *p;
   9597 	    asection *plt = htab->elf.splt;
   9598 
   9599 	    if (!htab->elf.dynamic_sections_created
   9600 		|| h->dynindx == -1)
   9601 	      {
   9602 		if (h->type == STT_GNU_IFUNC)
   9603 		  plt = htab->elf.iplt;
   9604 		else
   9605 		  break;
   9606 	      }
   9607 
   9608 	    p = (unsigned char *) htab->glink->contents + ent->glink_offset;
   9609 	    write_glink_stub (h, ent, plt, p, info);
   9610 
   9611 	    if (!bfd_link_pic (info))
   9612 	      /* We only need one non-PIC glink stub.  */
   9613 	      break;
   9614 	  }
   9615 	else
   9616 	  break;
   9617       }
   9618   return TRUE;
   9619 }
   9620 
   9621 /* Finish up PLT handling.  */
   9622 
   9623 bfd_boolean
   9624 ppc_finish_symbols (struct bfd_link_info *info)
   9625 {
   9626   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   9627   bfd *ibfd;
   9628 
   9629   if (!htab)
   9630     return TRUE;
   9631 
   9632   elf_link_hash_traverse (&htab->elf, write_global_sym_plt, info);
   9633 
   9634   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   9635     {
   9636       bfd_vma *local_got, *end_local_got;
   9637       struct plt_entry **local_plt, **lplt, **end_local_plt;
   9638       Elf_Internal_Shdr *symtab_hdr;
   9639       bfd_size_type locsymcount;
   9640       Elf_Internal_Sym *local_syms = NULL;
   9641       struct plt_entry *ent;
   9642 
   9643       if (!is_ppc_elf (ibfd))
   9644 	continue;
   9645 
   9646       local_got = elf_local_got_offsets (ibfd);
   9647       if (!local_got)
   9648 	continue;
   9649 
   9650       symtab_hdr = &elf_symtab_hdr (ibfd);
   9651       locsymcount = symtab_hdr->sh_info;
   9652       end_local_got = local_got + locsymcount;
   9653       local_plt = (struct plt_entry **) end_local_got;
   9654       end_local_plt = local_plt + locsymcount;
   9655       for (lplt = local_plt; lplt < end_local_plt; ++lplt)
   9656 	for (ent = *lplt; ent != NULL; ent = ent->next)
   9657 	  {
   9658 	    if (ent->plt.offset != (bfd_vma) -1)
   9659 	      {
   9660 		Elf_Internal_Sym *sym;
   9661 		asection *sym_sec;
   9662 		asection *plt, *relplt;
   9663 		bfd_byte *loc;
   9664 		bfd_vma val;
   9665 		Elf_Internal_Rela rela;
   9666 		unsigned char *p;
   9667 
   9668 		if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
   9669 				lplt - local_plt, ibfd))
   9670 		  {
   9671 		    if (symtab_hdr->contents != (unsigned char *) local_syms)
   9672 		      free (local_syms);
   9673 		    return FALSE;
   9674 		  }
   9675 
   9676 		val = sym->st_value;
   9677 		if (sym_sec != NULL && sym_sec->output_section != NULL)
   9678 		  val += sym_sec->output_offset + sym_sec->output_section->vma;
   9679 
   9680 		if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   9681 		  {
   9682 		    htab->local_ifunc_resolver = 1;
   9683 		    plt = htab->elf.iplt;
   9684 		    relplt = htab->elf.irelplt;
   9685 		    rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
   9686 		  }
   9687 		else
   9688 		  {
   9689 		    plt = htab->pltlocal;
   9690 		    if (bfd_link_pic (info))
   9691 		      {
   9692 			relplt = htab->relpltlocal;
   9693 			rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
   9694 		      }
   9695 		    else
   9696 		      {
   9697 			loc = plt->contents + ent->plt.offset;
   9698 			bfd_put_32 (info->output_bfd, val, loc);
   9699 			continue;
   9700 		      }
   9701 		  }
   9702 
   9703 		rela.r_offset = (ent->plt.offset
   9704 				 + plt->output_offset
   9705 				 + plt->output_section->vma);
   9706 		rela.r_addend = val;
   9707 		loc = relplt->contents + (relplt->reloc_count++
   9708 					  * sizeof (Elf32_External_Rela));
   9709 		bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
   9710 
   9711 		p = (unsigned char *) htab->glink->contents + ent->glink_offset;
   9712 		write_glink_stub (NULL, ent, htab->elf.iplt, p, info);
   9713 	      }
   9714 	  }
   9715 
   9716       if (local_syms != NULL
   9717 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   9718 	{
   9719 	  if (!info->keep_memory)
   9720 	    free (local_syms);
   9721 	  else
   9722 	    symtab_hdr->contents = (unsigned char *) local_syms;
   9723 	}
   9724     }
   9725   return TRUE;
   9726 }
   9727 
   9728 /* Finish up dynamic symbol handling.  We set the contents of various
   9729    dynamic sections here.  */
   9730 
   9731 static bfd_boolean
   9732 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
   9733 			       struct bfd_link_info *info,
   9734 			       struct elf_link_hash_entry *h,
   9735 			       Elf_Internal_Sym *sym)
   9736 {
   9737   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   9738   struct plt_entry *ent;
   9739 
   9740 #ifdef DEBUG
   9741   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
   9742 	   h->root.root.string);
   9743 #endif
   9744 
   9745   if (!h->def_regular
   9746       || (h->type == STT_GNU_IFUNC && !bfd_link_pic (info)))
   9747     for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   9748       if (ent->plt.offset != (bfd_vma) -1)
   9749 	{
   9750 	  if (!h->def_regular)
   9751 	    {
   9752 	      /* Mark the symbol as undefined, rather than as
   9753 		 defined in the .plt section.  Leave the value if
   9754 		 there were any relocations where pointer equality
   9755 		 matters (this is a clue for the dynamic linker, to
   9756 		 make function pointer comparisons work between an
   9757 		 application and shared library), otherwise set it
   9758 		 to zero.  */
   9759 	      sym->st_shndx = SHN_UNDEF;
   9760 	      if (!h->pointer_equality_needed)
   9761 		sym->st_value = 0;
   9762 	      else if (!h->ref_regular_nonweak)
   9763 		{
   9764 		  /* This breaks function pointer comparisons, but
   9765 		     that is better than breaking tests for a NULL
   9766 		     function pointer.  */
   9767 		  sym->st_value = 0;
   9768 		}
   9769 	    }
   9770 	  else
   9771 	    {
   9772 	      /* Set the value of ifunc symbols in a non-pie
   9773 		 executable to the glink entry.  This is to avoid
   9774 		 text relocations.  We can't do this for ifunc in
   9775 		 allocate_dynrelocs, as we do for normal dynamic
   9776 		 function symbols with plt entries, because we need
   9777 		 to keep the original value around for the ifunc
   9778 		 relocation.  */
   9779 	      sym->st_shndx
   9780 		= (_bfd_elf_section_from_bfd_section
   9781 		   (info->output_bfd, htab->glink->output_section));
   9782 	      sym->st_value = (ent->glink_offset
   9783 			       + htab->glink->output_offset
   9784 			       + htab->glink->output_section->vma);
   9785 	    }
   9786 	  break;
   9787 	}
   9788 
   9789   if (h->needs_copy)
   9790     {
   9791       asection *s;
   9792       Elf_Internal_Rela rela;
   9793       bfd_byte *loc;
   9794 
   9795       /* This symbols needs a copy reloc.  Set it up.  */
   9796 
   9797 #ifdef DEBUG
   9798       fprintf (stderr, ", copy");
   9799 #endif
   9800 
   9801       BFD_ASSERT (h->dynindx != -1);
   9802 
   9803       if (ppc_elf_hash_entry (h)->has_sda_refs)
   9804 	s = htab->relsbss;
   9805       else if (h->root.u.def.section == htab->elf.sdynrelro)
   9806 	s = htab->elf.sreldynrelro;
   9807       else
   9808 	s = htab->elf.srelbss;
   9809       BFD_ASSERT (s != NULL);
   9810 
   9811       rela.r_offset = SYM_VAL (h);
   9812       rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
   9813       rela.r_addend = 0;
   9814       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
   9815       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   9816     }
   9817 
   9818 #ifdef DEBUG
   9819   fprintf (stderr, "\n");
   9820 #endif
   9821 
   9822   return TRUE;
   9823 }
   9824 
   9825 static enum elf_reloc_type_class
   9827 ppc_elf_reloc_type_class (const struct bfd_link_info *info,
   9828 			  const asection *rel_sec,
   9829 			  const Elf_Internal_Rela *rela)
   9830 {
   9831   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
   9832 
   9833   if (rel_sec == htab->elf.irelplt)
   9834     return reloc_class_ifunc;
   9835 
   9836   switch (ELF32_R_TYPE (rela->r_info))
   9837     {
   9838     case R_PPC_RELATIVE:
   9839       return reloc_class_relative;
   9840     case R_PPC_JMP_SLOT:
   9841       return reloc_class_plt;
   9842     case R_PPC_COPY:
   9843       return reloc_class_copy;
   9844     default:
   9845       return reloc_class_normal;
   9846     }
   9847 }
   9848 
   9849 /* Finish up the dynamic sections.  */
   9851 
   9852 static bfd_boolean
   9853 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
   9854 				 struct bfd_link_info *info)
   9855 {
   9856   asection *sdyn;
   9857   struct ppc_elf_link_hash_table *htab;
   9858   bfd_vma got;
   9859   bfd *dynobj;
   9860   bfd_boolean ret = TRUE;
   9861 
   9862 #ifdef DEBUG
   9863   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
   9864 #endif
   9865 
   9866   htab = ppc_elf_hash_table (info);
   9867   dynobj = htab->elf.dynobj;
   9868   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   9869 
   9870   got = 0;
   9871   if (htab->elf.hgot != NULL)
   9872     got = SYM_VAL (htab->elf.hgot);
   9873 
   9874   if (htab->elf.dynamic_sections_created)
   9875     {
   9876       Elf32_External_Dyn *dyncon, *dynconend;
   9877 
   9878       BFD_ASSERT (htab->elf.splt != NULL && sdyn != NULL);
   9879 
   9880       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   9881       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   9882       for (; dyncon < dynconend; dyncon++)
   9883 	{
   9884 	  Elf_Internal_Dyn dyn;
   9885 	  asection *s;
   9886 
   9887 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   9888 
   9889 	  switch (dyn.d_tag)
   9890 	    {
   9891 	    case DT_PLTGOT:
   9892 	      if (htab->elf.target_os == is_vxworks)
   9893 		s = htab->elf.sgotplt;
   9894 	      else
   9895 		s = htab->elf.splt;
   9896 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   9897 	      break;
   9898 
   9899 	    case DT_PLTRELSZ:
   9900 	      dyn.d_un.d_val = htab->elf.srelplt->size;
   9901 	      break;
   9902 
   9903 	    case DT_JMPREL:
   9904 	      s = htab->elf.srelplt;
   9905 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   9906 	      break;
   9907 
   9908 	    case DT_PPC_GOT:
   9909 	      dyn.d_un.d_ptr = got;
   9910 	      break;
   9911 
   9912 	    case DT_TEXTREL:
   9913 	      if (htab->local_ifunc_resolver)
   9914 		info->callbacks->einfo
   9915 		  (_("%X%P: text relocations and GNU indirect "
   9916 		     "functions will result in a segfault at runtime\n"));
   9917 	      else if (htab->maybe_local_ifunc_resolver)
   9918 		info->callbacks->einfo
   9919 		  (_("%P: warning: text relocations and GNU indirect "
   9920 		     "functions may result in a segfault at runtime\n"));
   9921 	      continue;
   9922 
   9923 	    default:
   9924 	      if (htab->elf.target_os == is_vxworks
   9925 		  && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
   9926 		break;
   9927 	      continue;
   9928 	    }
   9929 
   9930 	  bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   9931 	}
   9932     }
   9933 
   9934   if (htab->elf.sgot != NULL
   9935       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
   9936     {
   9937       if (htab->elf.hgot->root.u.def.section == htab->elf.sgot
   9938 	  || htab->elf.hgot->root.u.def.section == htab->elf.sgotplt)
   9939 	{
   9940 	  unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
   9941 
   9942 	  p += htab->elf.hgot->root.u.def.value;
   9943 	  if (htab->plt_type == PLT_OLD)
   9944 	    {
   9945 	      /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4
   9946 		 so that a function can easily find the address of
   9947 		 _GLOBAL_OFFSET_TABLE_.  */
   9948 	      BFD_ASSERT (htab->elf.hgot->root.u.def.value - 4
   9949 			  < htab->elf.hgot->root.u.def.section->size);
   9950 	      bfd_put_32 (output_bfd, 0x4e800021, p - 4);
   9951 	    }
   9952 
   9953 	  if (sdyn != NULL)
   9954 	    {
   9955 	      bfd_vma val = sdyn->output_section->vma + sdyn->output_offset;
   9956 	      BFD_ASSERT (htab->elf.hgot->root.u.def.value
   9957 			  < htab->elf.hgot->root.u.def.section->size);
   9958 	      bfd_put_32 (output_bfd, val, p);
   9959 	    }
   9960 	}
   9961       else
   9962 	{
   9963 	  /* xgettext:c-format */
   9964 	  _bfd_error_handler (_("%s not defined in linker created %pA"),
   9965 			      htab->elf.hgot->root.root.string,
   9966 			      (htab->elf.sgotplt != NULL
   9967 			       ? htab->elf.sgotplt : htab->elf.sgot));
   9968 	  bfd_set_error (bfd_error_bad_value);
   9969 	  ret = FALSE;
   9970 	}
   9971 
   9972       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4;
   9973     }
   9974 
   9975   /* Fill in the first entry in the VxWorks procedure linkage table.  */
   9976   if (htab->elf.target_os == is_vxworks
   9977       && htab->elf.splt != NULL
   9978       && htab->elf.splt->size != 0
   9979       && htab->elf.splt->output_section != bfd_abs_section_ptr)
   9980     {
   9981       asection *splt = htab->elf.splt;
   9982       /* Use the right PLT. */
   9983       const bfd_vma *plt_entry = (bfd_link_pic (info)
   9984 				  ? ppc_elf_vxworks_pic_plt0_entry
   9985 				  : ppc_elf_vxworks_plt0_entry);
   9986 
   9987       if (!bfd_link_pic (info))
   9988 	{
   9989 	  bfd_vma got_value = SYM_VAL (htab->elf.hgot);
   9990 
   9991 	  bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
   9992 		      splt->contents +  0);
   9993 	  bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
   9994 		      splt->contents +  4);
   9995 	}
   9996       else
   9997 	{
   9998 	  bfd_put_32 (output_bfd, plt_entry[0], splt->contents +  0);
   9999 	  bfd_put_32 (output_bfd, plt_entry[1], splt->contents +  4);
   10000 	}
   10001       bfd_put_32 (output_bfd, plt_entry[2], splt->contents +  8);
   10002       bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
   10003       bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
   10004       bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
   10005       bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
   10006       bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
   10007 
   10008       if (! bfd_link_pic (info))
   10009 	{
   10010 	  Elf_Internal_Rela rela;
   10011 	  bfd_byte *loc;
   10012 
   10013 	  loc = htab->srelplt2->contents;
   10014 
   10015 	  /* Output the @ha relocation for the first instruction.  */
   10016 	  rela.r_offset = (htab->elf.splt->output_section->vma
   10017 			   + htab->elf.splt->output_offset
   10018 			   + 2);
   10019 	  rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
   10020 	  rela.r_addend = 0;
   10021 	  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   10022 	  loc += sizeof (Elf32_External_Rela);
   10023 
   10024 	  /* Output the @l relocation for the second instruction.  */
   10025 	  rela.r_offset = (htab->elf.splt->output_section->vma
   10026 			   + htab->elf.splt->output_offset
   10027 			   + 6);
   10028 	  rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
   10029 	  rela.r_addend = 0;
   10030 	  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   10031 	  loc += sizeof (Elf32_External_Rela);
   10032 
   10033 	  /* Fix up the remaining relocations.  They may have the wrong
   10034 	     symbol index for _G_O_T_ or _P_L_T_ depending on the order
   10035 	     in which symbols were output.  */
   10036 	  while (loc < htab->srelplt2->contents + htab->srelplt2->size)
   10037 	    {
   10038 	      Elf_Internal_Rela rel;
   10039 
   10040 	      bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
   10041 	      rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
   10042 	      bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
   10043 	      loc += sizeof (Elf32_External_Rela);
   10044 
   10045 	      bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
   10046 	      rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
   10047 	      bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
   10048 	      loc += sizeof (Elf32_External_Rela);
   10049 
   10050 	      bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
   10051 	      rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
   10052 	      bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
   10053 	      loc += sizeof (Elf32_External_Rela);
   10054 	    }
   10055 	}
   10056     }
   10057 
   10058   if (htab->glink != NULL
   10059       && htab->glink->contents != NULL
   10060       && htab->elf.dynamic_sections_created)
   10061     {
   10062       unsigned char *p;
   10063       unsigned char *endp;
   10064       bfd_vma res0;
   10065 
   10066       /*
   10067        * PIC glink code is the following:
   10068        *
   10069        * # ith PLT code stub.
   10070        *   addis 11,30,(plt+(i-1)*4-got)@ha
   10071        *   lwz 11,(plt+(i-1)*4-got)@l(11)
   10072        *   mtctr 11
   10073        *   bctr
   10074        *
   10075        * # A table of branches, one for each plt entry.
   10076        * # The idea is that the plt call stub loads ctr and r11 with these
   10077        * # addresses, so (r11 - res_0) gives the plt index * 4.
   10078        * res_0:	b PLTresolve
   10079        * res_1:	b PLTresolve
   10080        * .
   10081        * # Some number of entries towards the end can be nops
   10082        * res_n_m3: nop
   10083        * res_n_m2: nop
   10084        * res_n_m1:
   10085        *
   10086        * PLTresolve:
   10087        *    addis 11,11,(1f-res_0)@ha
   10088        *    mflr 0
   10089        *    bcl 20,31,1f
   10090        * 1: addi 11,11,(1b-res_0)@l
   10091        *    mflr 12
   10092        *    mtlr 0
   10093        *    sub 11,11,12		# r11 = index * 4
   10094        *    addis 12,12,(got+4-1b)@ha
   10095        *    lwz 0,(got+4-1b)@l(12)	# got[1] address of dl_runtime_resolve
   10096        *    lwz 12,(got+8-1b)@l(12)	# got[2] contains the map address
   10097        *    mtctr 0
   10098        *    add 0,11,11
   10099        *    add 11,0,11			# r11 = index * 12 = reloc offset.
   10100        *    bctr
   10101        *
   10102        * Non-PIC glink code is a little simpler.
   10103        *
   10104        * # ith PLT code stub.
   10105        *   lis 11,(plt+(i-1)*4)@ha
   10106        *   lwz 11,(plt+(i-1)*4)@l(11)
   10107        *   mtctr 11
   10108        *   bctr
   10109        *
   10110        * The branch table is the same, then comes
   10111        *
   10112        * PLTresolve:
   10113        *    lis 12,(got+4)@ha
   10114        *    addis 11,11,(-res_0)@ha
   10115        *    lwz 0,(got+4)@l(12)		# got[1] address of dl_runtime_resolve
   10116        *    addi 11,11,(-res_0)@l	# r11 = index * 4
   10117        *    mtctr 0
   10118        *    add 0,11,11
   10119        *    lwz 12,(got+8)@l(12)	# got[2] contains the map address
   10120        *    add 11,0,11			# r11 = index * 12 = reloc offset.
   10121        *    bctr
   10122        */
   10123 
   10124       /* Build the branch table, one for each plt entry (less one),
   10125 	 and perhaps some padding.  */
   10126       p = htab->glink->contents;
   10127       p += htab->glink_pltresolve;
   10128       endp = htab->glink->contents;
   10129       endp += htab->glink->size - GLINK_PLTRESOLVE;
   10130       while (p < endp - (htab->params->ppc476_workaround ? 0 : 8 * 4))
   10131 	{
   10132 	  bfd_put_32 (output_bfd, B + endp - p, p);
   10133 	  p += 4;
   10134 	}
   10135       while (p < endp)
   10136 	{
   10137 	  bfd_put_32 (output_bfd, NOP, p);
   10138 	  p += 4;
   10139 	}
   10140 
   10141       res0 = (htab->glink_pltresolve
   10142 	      + htab->glink->output_section->vma
   10143 	      + htab->glink->output_offset);
   10144 
   10145       if (htab->params->ppc476_workaround)
   10146 	{
   10147 	  /* Ensure that a call stub at the end of a page doesn't
   10148 	     result in prefetch over the end of the page into the
   10149 	     glink branch table.  */
   10150 	  bfd_vma pagesize = (bfd_vma) 1 << htab->params->pagesize_p2;
   10151 	  bfd_vma page_addr;
   10152 	  bfd_vma glink_start = (htab->glink->output_section->vma
   10153 				 + htab->glink->output_offset);
   10154 
   10155 	  for (page_addr = res0 & -pagesize;
   10156 	       page_addr > glink_start;
   10157 	       page_addr -= pagesize)
   10158 	    {
   10159 	      /* We have a plt call stub that may need fixing.  */
   10160 	      bfd_byte *loc;
   10161 	      unsigned int insn;
   10162 
   10163 	      loc = htab->glink->contents + page_addr - 4 - glink_start;
   10164 	      insn = bfd_get_32 (output_bfd, loc);
   10165 	      if (insn == BCTR)
   10166 		{
   10167 		  /* By alignment, we know that there must be at least
   10168 		     one other call stub before this one.  */
   10169 		  insn = bfd_get_32 (output_bfd, loc - 16);
   10170 		  if (insn == BCTR)
   10171 		    bfd_put_32 (output_bfd, B | (-16 & 0x3fffffc), loc);
   10172 		  else
   10173 		    bfd_put_32 (output_bfd, B | (-20 & 0x3fffffc), loc);
   10174 		}
   10175 	    }
   10176 	}
   10177 
   10178       /* Last comes the PLTresolve stub.  */
   10179       endp = p + GLINK_PLTRESOLVE;
   10180       if (bfd_link_pic (info))
   10181 	{
   10182 	  bfd_vma bcl;
   10183 
   10184 	  bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
   10185 		 + htab->glink->output_section->vma
   10186 		 + htab->glink->output_offset);
   10187 
   10188 	  bfd_put_32 (output_bfd, ADDIS_11_11 + PPC_HA (bcl - res0), p);
   10189 	  p += 4;
   10190 	  bfd_put_32 (output_bfd, MFLR_0, p);
   10191 	  p += 4;
   10192 	  bfd_put_32 (output_bfd, BCL_20_31, p);
   10193 	  p += 4;
   10194 	  bfd_put_32 (output_bfd, ADDI_11_11 + PPC_LO (bcl - res0), p);
   10195 	  p += 4;
   10196 	  bfd_put_32 (output_bfd, MFLR_12, p);
   10197 	  p += 4;
   10198 	  bfd_put_32 (output_bfd, MTLR_0, p);
   10199 	  p += 4;
   10200 	  bfd_put_32 (output_bfd, SUB_11_11_12, p);
   10201 	  p += 4;
   10202 	  bfd_put_32 (output_bfd, ADDIS_12_12 + PPC_HA (got + 4 - bcl), p);
   10203 	  p += 4;
   10204 	  if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
   10205 	    {
   10206 	      bfd_put_32 (output_bfd, LWZ_0_12 + PPC_LO (got + 4 - bcl), p);
   10207 	      p += 4;
   10208 	      bfd_put_32 (output_bfd, LWZ_12_12 + PPC_LO (got + 8 - bcl), p);
   10209 	      p += 4;
   10210 	    }
   10211 	  else
   10212 	    {
   10213 	      bfd_put_32 (output_bfd, LWZU_0_12 + PPC_LO (got + 4 - bcl), p);
   10214 	      p += 4;
   10215 	      bfd_put_32 (output_bfd, LWZ_12_12 + 4, p);
   10216 	      p += 4;
   10217 	    }
   10218 	  bfd_put_32 (output_bfd, MTCTR_0, p);
   10219 	  p += 4;
   10220 	  bfd_put_32 (output_bfd, ADD_0_11_11, p);
   10221 	}
   10222       else
   10223 	{
   10224 	  bfd_put_32 (output_bfd, LIS_12 + PPC_HA (got + 4), p);
   10225 	  p += 4;
   10226 	  bfd_put_32 (output_bfd, ADDIS_11_11 + PPC_HA (-res0), p);
   10227 	  p += 4;
   10228 	  if (PPC_HA (got + 4) == PPC_HA (got + 8))
   10229 	    bfd_put_32 (output_bfd, LWZ_0_12 + PPC_LO (got + 4), p);
   10230 	  else
   10231 	    bfd_put_32 (output_bfd, LWZU_0_12 + PPC_LO (got + 4), p);
   10232 	  p += 4;
   10233 	  bfd_put_32 (output_bfd, ADDI_11_11 + PPC_LO (-res0), p);
   10234 	  p += 4;
   10235 	  bfd_put_32 (output_bfd, MTCTR_0, p);
   10236 	  p += 4;
   10237 	  bfd_put_32 (output_bfd, ADD_0_11_11, p);
   10238 	  p += 4;
   10239 	  if (PPC_HA (got + 4) == PPC_HA (got + 8))
   10240 	    bfd_put_32 (output_bfd, LWZ_12_12 + PPC_LO (got + 8), p);
   10241 	  else
   10242 	    bfd_put_32 (output_bfd, LWZ_12_12 + 4, p);
   10243 	}
   10244       p += 4;
   10245       bfd_put_32 (output_bfd, ADD_11_0_11, p);
   10246       p += 4;
   10247       bfd_put_32 (output_bfd, BCTR, p);
   10248       p += 4;
   10249       while (p < endp)
   10250 	{
   10251 	  bfd_put_32 (output_bfd,
   10252 		      htab->params->ppc476_workaround ? BA : NOP, p);
   10253 	  p += 4;
   10254 	}
   10255       BFD_ASSERT (p == endp);
   10256     }
   10257 
   10258   if (htab->glink_eh_frame != NULL
   10259       && htab->glink_eh_frame->contents != NULL)
   10260     {
   10261       unsigned char *p = htab->glink_eh_frame->contents;
   10262       bfd_vma val;
   10263 
   10264       p += sizeof (glink_eh_frame_cie);
   10265       /* FDE length.  */
   10266       p += 4;
   10267       /* CIE pointer.  */
   10268       p += 4;
   10269       /* Offset to .glink.  */
   10270       val = (htab->glink->output_section->vma
   10271 	     + htab->glink->output_offset);
   10272       val -= (htab->glink_eh_frame->output_section->vma
   10273 	      + htab->glink_eh_frame->output_offset);
   10274       val -= p - htab->glink_eh_frame->contents;
   10275       bfd_put_32 (htab->elf.dynobj, val, p);
   10276 
   10277       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
   10278 	  && !_bfd_elf_write_section_eh_frame (output_bfd, info,
   10279 					       htab->glink_eh_frame,
   10280 					       htab->glink_eh_frame->contents))
   10281 	return FALSE;
   10282     }
   10283 
   10284   return ret;
   10285 }
   10286 
   10287 #define TARGET_LITTLE_SYM	powerpc_elf32_le_vec
   10289 #define TARGET_LITTLE_NAME	"elf32-powerpcle"
   10290 #define TARGET_BIG_SYM		powerpc_elf32_vec
   10291 #define TARGET_BIG_NAME		"elf32-powerpc"
   10292 #define ELF_ARCH		bfd_arch_powerpc
   10293 #define ELF_TARGET_ID		PPC32_ELF_DATA
   10294 #define ELF_MACHINE_CODE	EM_PPC
   10295 #define ELF_MAXPAGESIZE		0x10000
   10296 #define ELF_COMMONPAGESIZE	0x1000
   10297 #define ELF_RELROPAGESIZE	ELF_MAXPAGESIZE
   10298 #define elf_info_to_howto	ppc_elf_info_to_howto
   10299 
   10300 #ifdef  EM_CYGNUS_POWERPC
   10301 #define ELF_MACHINE_ALT1	EM_CYGNUS_POWERPC
   10302 #endif
   10303 
   10304 #ifdef EM_PPC_OLD
   10305 #define ELF_MACHINE_ALT2	EM_PPC_OLD
   10306 #endif
   10307 
   10308 #define elf_backend_plt_not_loaded	1
   10309 #define elf_backend_want_dynrelro	1
   10310 #define elf_backend_can_gc_sections	1
   10311 #define elf_backend_can_refcount	1
   10312 #define elf_backend_rela_normal		1
   10313 #define elf_backend_caches_rawsize	1
   10314 
   10315 #define bfd_elf32_mkobject			ppc_elf_mkobject
   10316 #define bfd_elf32_bfd_merge_private_bfd_data	ppc_elf_merge_private_bfd_data
   10317 #define bfd_elf32_bfd_relax_section		ppc_elf_relax_section
   10318 #define bfd_elf32_bfd_reloc_type_lookup		ppc_elf_reloc_type_lookup
   10319 #define bfd_elf32_bfd_reloc_name_lookup		ppc_elf_reloc_name_lookup
   10320 #define bfd_elf32_bfd_set_private_flags		ppc_elf_set_private_flags
   10321 #define bfd_elf32_bfd_link_hash_table_create	ppc_elf_link_hash_table_create
   10322 #define bfd_elf32_get_synthetic_symtab		ppc_elf_get_synthetic_symtab
   10323 
   10324 #define elf_backend_object_p			ppc_elf_object_p
   10325 #define elf_backend_gc_mark_hook		ppc_elf_gc_mark_hook
   10326 #define elf_backend_section_from_shdr		ppc_elf_section_from_shdr
   10327 #define elf_backend_relocate_section		ppc_elf_relocate_section
   10328 #define elf_backend_create_dynamic_sections	ppc_elf_create_dynamic_sections
   10329 #define elf_backend_check_relocs		ppc_elf_check_relocs
   10330 #define elf_backend_relocs_compatible		_bfd_elf_relocs_compatible
   10331 #define elf_backend_copy_indirect_symbol	ppc_elf_copy_indirect_symbol
   10332 #define elf_backend_adjust_dynamic_symbol	ppc_elf_adjust_dynamic_symbol
   10333 #define elf_backend_add_symbol_hook		ppc_elf_add_symbol_hook
   10334 #define elf_backend_size_dynamic_sections	ppc_elf_size_dynamic_sections
   10335 #define elf_backend_hash_symbol			ppc_elf_hash_symbol
   10336 #define elf_backend_finish_dynamic_symbol	ppc_elf_finish_dynamic_symbol
   10337 #define elf_backend_finish_dynamic_sections	ppc_elf_finish_dynamic_sections
   10338 #define elf_backend_fake_sections		ppc_elf_fake_sections
   10339 #define elf_backend_additional_program_headers	ppc_elf_additional_program_headers
   10340 #define elf_backend_modify_segment_map		ppc_elf_modify_segment_map
   10341 #define elf_backend_grok_prstatus		ppc_elf_grok_prstatus
   10342 #define elf_backend_grok_psinfo			ppc_elf_grok_psinfo
   10343 #define elf_backend_write_core_note		ppc_elf_write_core_note
   10344 #define elf_backend_reloc_type_class		ppc_elf_reloc_type_class
   10345 #define elf_backend_begin_write_processing	ppc_elf_begin_write_processing
   10346 #define elf_backend_final_write_processing	ppc_elf_final_write_processing
   10347 #define elf_backend_write_section		ppc_elf_write_section
   10348 #define elf_backend_get_sec_type_attr		ppc_elf_get_sec_type_attr
   10349 #define elf_backend_plt_sym_val			ppc_elf_plt_sym_val
   10350 #define elf_backend_action_discarded		ppc_elf_action_discarded
   10351 #define elf_backend_init_index_section		_bfd_elf_init_1_index_section
   10352 #define elf_backend_lookup_section_flags_hook	ppc_elf_lookup_section_flags
   10353 
   10354 #include "elf32-target.h"
   10355 
   10356 /* FreeBSD Target */
   10357 
   10358 #undef  TARGET_LITTLE_SYM
   10359 #undef  TARGET_LITTLE_NAME
   10360 
   10361 #undef  TARGET_BIG_SYM
   10362 #define TARGET_BIG_SYM  powerpc_elf32_fbsd_vec
   10363 #undef  TARGET_BIG_NAME
   10364 #define TARGET_BIG_NAME "elf32-powerpc-freebsd"
   10365 
   10366 #undef  ELF_OSABI
   10367 #define ELF_OSABI	ELFOSABI_FREEBSD
   10368 
   10369 #undef  elf32_bed
   10370 #define elf32_bed	elf32_powerpc_fbsd_bed
   10371 
   10372 #include "elf32-target.h"
   10373 
   10374 /* VxWorks Target */
   10375 
   10376 #undef TARGET_LITTLE_SYM
   10377 #undef TARGET_LITTLE_NAME
   10378 
   10379 #undef TARGET_BIG_SYM
   10380 #define TARGET_BIG_SYM		powerpc_elf32_vxworks_vec
   10381 #undef TARGET_BIG_NAME
   10382 #define TARGET_BIG_NAME		"elf32-powerpc-vxworks"
   10383 
   10384 #undef  ELF_OSABI
   10385 
   10386 #undef ELF_TARGET_OS
   10387 #define ELF_TARGET_OS		is_vxworks
   10388 
   10389 /* VxWorks uses the elf default section flags for .plt.  */
   10390 static const struct bfd_elf_special_section *
   10391 ppc_elf_vxworks_get_sec_type_attr (bfd *abfd, asection *sec)
   10392 {
   10393   if (sec->name == NULL)
   10394     return NULL;
   10395 
   10396   if (strcmp (sec->name, ".plt") == 0)
   10397     return _bfd_elf_get_sec_type_attr (abfd, sec);
   10398 
   10399   return ppc_elf_get_sec_type_attr (abfd, sec);
   10400 }
   10401 
   10402 /* Like ppc_elf_link_hash_table_create, but overrides
   10403    appropriately for VxWorks.  */
   10404 static struct bfd_link_hash_table *
   10405 ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
   10406 {
   10407   struct bfd_link_hash_table *ret;
   10408 
   10409   ret = ppc_elf_link_hash_table_create (abfd);
   10410   if (ret)
   10411     {
   10412       struct ppc_elf_link_hash_table *htab
   10413 	= (struct ppc_elf_link_hash_table *)ret;
   10414       htab->plt_type = PLT_VXWORKS;
   10415       htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
   10416       htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
   10417       htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
   10418     }
   10419   return ret;
   10420 }
   10421 
   10422 /* Tweak magic VxWorks symbols as they are loaded.  */
   10423 static bfd_boolean
   10424 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
   10425 				 struct bfd_link_info *info,
   10426 				 Elf_Internal_Sym *sym,
   10427 				 const char **namep,
   10428 				 flagword *flagsp,
   10429 				 asection **secp,
   10430 				 bfd_vma *valp)
   10431 {
   10432   if (!elf_vxworks_add_symbol_hook (abfd, info, sym, namep, flagsp, secp,
   10433 				    valp))
   10434     return FALSE;
   10435 
   10436   return ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp);
   10437 }
   10438 
   10439 static bfd_boolean
   10440 ppc_elf_vxworks_final_write_processing (bfd *abfd)
   10441 {
   10442   ppc_final_write_processing (abfd);
   10443   return elf_vxworks_final_write_processing (abfd);
   10444 }
   10445 
   10446 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
   10447    define it.  */
   10448 #undef elf_backend_want_plt_sym
   10449 #define elf_backend_want_plt_sym		1
   10450 #undef elf_backend_want_got_plt
   10451 #define elf_backend_want_got_plt		1
   10452 #undef elf_backend_got_symbol_offset
   10453 #define elf_backend_got_symbol_offset		0
   10454 #undef elf_backend_plt_not_loaded
   10455 #define elf_backend_plt_not_loaded		0
   10456 #undef elf_backend_plt_readonly
   10457 #define elf_backend_plt_readonly		1
   10458 #undef elf_backend_got_header_size
   10459 #define elf_backend_got_header_size		12
   10460 #undef elf_backend_dtrel_excludes_plt
   10461 #define elf_backend_dtrel_excludes_plt		1
   10462 
   10463 #undef bfd_elf32_get_synthetic_symtab
   10464 
   10465 #undef bfd_elf32_bfd_link_hash_table_create
   10466 #define bfd_elf32_bfd_link_hash_table_create \
   10467   ppc_elf_vxworks_link_hash_table_create
   10468 #undef elf_backend_add_symbol_hook
   10469 #define elf_backend_add_symbol_hook \
   10470   ppc_elf_vxworks_add_symbol_hook
   10471 #undef elf_backend_link_output_symbol_hook
   10472 #define elf_backend_link_output_symbol_hook \
   10473   elf_vxworks_link_output_symbol_hook
   10474 #undef elf_backend_final_write_processing
   10475 #define elf_backend_final_write_processing \
   10476   ppc_elf_vxworks_final_write_processing
   10477 #undef elf_backend_get_sec_type_attr
   10478 #define elf_backend_get_sec_type_attr \
   10479   ppc_elf_vxworks_get_sec_type_attr
   10480 #undef elf_backend_emit_relocs
   10481 #define elf_backend_emit_relocs \
   10482   elf_vxworks_emit_relocs
   10483 
   10484 #undef elf32_bed
   10485 #define elf32_bed				ppc_elf_vxworks_bed
   10486 
   10487 #include "elf32-target.h"
   10488