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