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