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