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