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