Home | History | Annotate | Download | only in bfd
      1 /* PowerPC64-specific support for 64-bit ELF.
      2    Copyright (C) 1999-2016 Free Software Foundation, Inc.
      3    Written by Linus Nordberg, Swox AB <info (at) swox.com>,
      4    based on elf32-ppc.c by Ian Lance Taylor.
      5    Largely rewritten by Alan Modra.
      6 
      7    This file is part of BFD, the Binary File Descriptor library.
      8 
      9    This program is free software; you can redistribute it and/or modify
     10    it under the terms of the GNU General Public License as published by
     11    the Free Software Foundation; either version 3 of the License, or
     12    (at your option) any later version.
     13 
     14    This program is distributed in the hope that it will be useful,
     15    but WITHOUT ANY WARRANTY; without even the implied warranty of
     16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17    GNU General Public License for more details.
     18 
     19    You should have received a copy of the GNU General Public License along
     20    with this program; if not, write to the Free Software Foundation, Inc.,
     21    51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
     22 
     23 
     24 /* The 64-bit PowerPC ELF ABI may be found at
     25    http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
     26    http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html  */
     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/ppc64.h"
     35 #include "elf64-ppc.h"
     36 #include "dwarf2.h"
     37 
     38 static bfd_reloc_status_type ppc64_elf_ha_reloc
     39   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     40 static bfd_reloc_status_type ppc64_elf_branch_reloc
     41   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
     43   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
     45   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
     47   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     48 static bfd_reloc_status_type ppc64_elf_toc_reloc
     49   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
     51   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
     53   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
     55   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
     56 static bfd_vma opd_entry_value
     57   (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
     58 
     59 #define TARGET_LITTLE_SYM	powerpc_elf64_le_vec
     60 #define TARGET_LITTLE_NAME	"elf64-powerpcle"
     61 #define TARGET_BIG_SYM		powerpc_elf64_vec
     62 #define TARGET_BIG_NAME		"elf64-powerpc"
     63 #define ELF_ARCH		bfd_arch_powerpc
     64 #define ELF_TARGET_ID		PPC64_ELF_DATA
     65 #define ELF_MACHINE_CODE	EM_PPC64
     66 #define ELF_MAXPAGESIZE		0x10000
     67 #define ELF_COMMONPAGESIZE	0x10000
     68 #define elf_info_to_howto	ppc64_elf_info_to_howto
     69 
     70 #define elf_backend_want_got_sym 0
     71 #define elf_backend_want_plt_sym 0
     72 #define elf_backend_plt_alignment 3
     73 #define elf_backend_plt_not_loaded 1
     74 #define elf_backend_got_header_size 8
     75 #define elf_backend_can_gc_sections 1
     76 #define elf_backend_can_refcount 1
     77 #define elf_backend_rela_normal 1
     78 #define elf_backend_default_execstack 0
     79 
     80 #define bfd_elf64_mkobject		      ppc64_elf_mkobject
     81 #define bfd_elf64_bfd_reloc_type_lookup	      ppc64_elf_reloc_type_lookup
     82 #define bfd_elf64_bfd_reloc_name_lookup	      ppc64_elf_reloc_name_lookup
     83 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
     84 #define bfd_elf64_bfd_print_private_bfd_data  ppc64_elf_print_private_bfd_data
     85 #define bfd_elf64_new_section_hook	      ppc64_elf_new_section_hook
     86 #define bfd_elf64_bfd_link_hash_table_create  ppc64_elf_link_hash_table_create
     87 #define bfd_elf64_get_synthetic_symtab	      ppc64_elf_get_synthetic_symtab
     88 #define bfd_elf64_bfd_link_just_syms	      ppc64_elf_link_just_syms
     89 
     90 #define elf_backend_object_p		      ppc64_elf_object_p
     91 #define elf_backend_grok_prstatus	      ppc64_elf_grok_prstatus
     92 #define elf_backend_grok_psinfo		      ppc64_elf_grok_psinfo
     93 #define elf_backend_write_core_note	      ppc64_elf_write_core_note
     94 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
     95 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
     96 #define elf_backend_add_symbol_hook	      ppc64_elf_add_symbol_hook
     97 #define elf_backend_check_directives	      ppc64_elf_before_check_relocs
     98 #define elf_backend_notice_as_needed	      ppc64_elf_notice_as_needed
     99 #define elf_backend_archive_symbol_lookup     ppc64_elf_archive_symbol_lookup
    100 #define elf_backend_check_relocs	      ppc64_elf_check_relocs
    101 #define elf_backend_gc_keep		      ppc64_elf_gc_keep
    102 #define elf_backend_gc_mark_dynamic_ref       ppc64_elf_gc_mark_dynamic_ref
    103 #define elf_backend_gc_mark_hook	      ppc64_elf_gc_mark_hook
    104 #define elf_backend_gc_sweep_hook	      ppc64_elf_gc_sweep_hook
    105 #define elf_backend_adjust_dynamic_symbol     ppc64_elf_adjust_dynamic_symbol
    106 #define elf_backend_hide_symbol		      ppc64_elf_hide_symbol
    107 #define elf_backend_maybe_function_sym	      ppc64_elf_maybe_function_sym
    108 #define elf_backend_always_size_sections      ppc64_elf_func_desc_adjust
    109 #define elf_backend_size_dynamic_sections     ppc64_elf_size_dynamic_sections
    110 #define elf_backend_hash_symbol		      ppc64_elf_hash_symbol
    111 #define elf_backend_init_index_section	      _bfd_elf_init_2_index_sections
    112 #define elf_backend_action_discarded	      ppc64_elf_action_discarded
    113 #define elf_backend_relocate_section	      ppc64_elf_relocate_section
    114 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
    115 #define elf_backend_reloc_type_class	      ppc64_elf_reloc_type_class
    116 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
    117 #define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
    118 #define elf_backend_special_sections	      ppc64_elf_special_sections
    119 #define elf_backend_merge_symbol_attribute    ppc64_elf_merge_symbol_attribute
    120 
    121 /* The name of the dynamic interpreter.  This is put in the .interp
    122    section.  */
    123 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
    124 
    125 /* The size in bytes of an entry in the procedure linkage table.  */
    126 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
    127 
    128 /* The initial size of the plt reserved for the dynamic linker.  */
    129 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
    130 
    131 /* Offsets to some stack save slots.  */
    132 #define STK_LR 16
    133 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
    134 /* This one is dodgy.  ELFv2 does not have a linker word, so use the
    135    CR save slot.  Used only by optimised __tls_get_addr call stub,
    136    relying on __tls_get_addr_opt not saving CR..  */
    137 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
    138 
    139 /* TOC base pointers offset from start of TOC.  */
    140 #define TOC_BASE_OFF	0x8000
    141 /* TOC base alignment.  */
    142 #define TOC_BASE_ALIGN	256
    143 
    144 /* Offset of tp and dtp pointers from start of TLS block.  */
    145 #define TP_OFFSET	0x7000
    146 #define DTP_OFFSET	0x8000
    147 
    148 /* .plt call stub instructions.  The normal stub is like this, but
    149    sometimes the .plt entry crosses a 64k boundary and we need to
    150    insert an addi to adjust r11.  */
    151 #define STD_R2_0R1	0xf8410000	/* std	 %r2,0+40(%r1)	     */
    152 #define ADDIS_R11_R2	0x3d620000	/* addis %r11,%r2,xxx@ha     */
    153 #define LD_R12_0R11	0xe98b0000	/* ld	 %r12,xxx+0@l(%r11)  */
    154 #define MTCTR_R12	0x7d8903a6	/* mtctr %r12		     */
    155 #define LD_R2_0R11	0xe84b0000	/* ld	 %r2,xxx+8@l(%r11)   */
    156 #define LD_R11_0R11	0xe96b0000	/* ld	 %r11,xxx+16@l(%r11) */
    157 #define BCTR		0x4e800420	/* bctr			     */
    158 
    159 #define ADDI_R11_R11	0x396b0000	/* addi %r11,%r11,off@l  */
    160 #define ADDIS_R2_R2	0x3c420000	/* addis %r2,%r2,off@ha  */
    161 #define ADDI_R2_R2	0x38420000	/* addi  %r2,%r2,off@l   */
    162 
    163 #define XOR_R2_R12_R12	0x7d826278	/* xor   %r2,%r12,%r12   */
    164 #define ADD_R11_R11_R2	0x7d6b1214	/* add   %r11,%r11,%r2   */
    165 #define XOR_R11_R12_R12	0x7d8b6278	/* xor   %r11,%r12,%r12  */
    166 #define ADD_R2_R2_R11	0x7c425a14	/* add   %r2,%r2,%r11    */
    167 #define CMPLDI_R2_0	0x28220000	/* cmpldi %r2,0          */
    168 #define BNECTR		0x4ca20420	/* bnectr+               */
    169 #define BNECTR_P4	0x4ce20420	/* bnectr+               */
    170 
    171 #define LD_R12_0R2	0xe9820000	/* ld	 %r12,xxx+0(%r2) */
    172 #define LD_R11_0R2	0xe9620000	/* ld	 %r11,xxx+0(%r2) */
    173 #define LD_R2_0R2	0xe8420000	/* ld	 %r2,xxx+0(%r2)  */
    174 
    175 #define LD_R2_0R1	0xe8410000	/* ld    %r2,0(%r1)      */
    176 #define LD_R2_0R12	0xe84c0000	/* ld    %r2,0(%r12)     */
    177 #define ADD_R2_R2_R12	0x7c426214	/* add   %r2,%r2,%r12    */
    178 
    179 #define LIS_R2		0x3c400000	/* lis %r2,xxx@ha         */
    180 #define ADDIS_R2_R12	0x3c4c0000	/* addis %r2,%r12,xxx@ha  */
    181 #define ADDIS_R12_R2	0x3d820000	/* addis %r12,%r2,xxx@ha  */
    182 #define ADDIS_R12_R12	0x3d8c0000	/* addis %r12,%r12,xxx@ha */
    183 #define LD_R12_0R12	0xe98c0000	/* ld    %r12,xxx@l(%r12) */
    184 
    185 /* glink call stub instructions.  We enter with the index in R0.  */
    186 #define GLINK_CALL_STUB_SIZE (16*4)
    187 					/* 0:				*/
    188 					/*  .quad plt0-1f		*/
    189 					/* __glink:			*/
    190 #define MFLR_R12	0x7d8802a6	/*  mflr %12			*/
    191 #define BCL_20_31	0x429f0005	/*  bcl 20,31,1f		*/
    192 					/* 1:				*/
    193 #define MFLR_R11	0x7d6802a6	/*  mflr %11			*/
    194 					/*  ld %2,(0b-1b)(%11)		*/
    195 #define MTLR_R12	0x7d8803a6	/*  mtlr %12			*/
    196 #define ADD_R11_R2_R11	0x7d625a14	/*  add %11,%2,%11		*/
    197 					/*  ld %12,0(%11)		*/
    198 					/*  ld %2,8(%11)		*/
    199 					/*  mtctr %12			*/
    200 					/*  ld %11,16(%11)		*/
    201 					/*  bctr			*/
    202 #define MFLR_R0		0x7c0802a6	/*  mflr %r0			*/
    203 #define MTLR_R0		0x7c0803a6	/*  mtlr %r0			*/
    204 #define SUB_R12_R12_R11	0x7d8b6050	/*  subf %r12,%r11,%r12		*/
    205 #define ADDI_R0_R12	0x380c0000	/*  addi %r0,%r12,0		*/
    206 #define SRDI_R0_R0_2	0x7800f082	/*  rldicl %r0,%r0,62,2		*/
    207 
    208 /* Pad with this.  */
    209 #define NOP		0x60000000
    210 
    211 /* Some other nops.  */
    212 #define CROR_151515	0x4def7b82
    213 #define CROR_313131	0x4ffffb82
    214 
    215 /* .glink entries for the first 32k functions are two instructions.  */
    216 #define LI_R0_0		0x38000000	/* li    %r0,0		*/
    217 #define B_DOT		0x48000000	/* b     .		*/
    218 
    219 /* After that, we need two instructions to load the index, followed by
    220    a branch.  */
    221 #define LIS_R0_0	0x3c000000	/* lis   %r0,0		*/
    222 #define ORI_R0_R0_0	0x60000000	/* ori	 %r0,%r0,0	*/
    223 
    224 /* Instructions used by the save and restore reg functions.  */
    225 #define STD_R0_0R1	0xf8010000	/* std   %r0,0(%r1)	*/
    226 #define STD_R0_0R12	0xf80c0000	/* std   %r0,0(%r12)	*/
    227 #define LD_R0_0R1	0xe8010000	/* ld    %r0,0(%r1)	*/
    228 #define LD_R0_0R12	0xe80c0000	/* ld    %r0,0(%r12)	*/
    229 #define STFD_FR0_0R1	0xd8010000	/* stfd  %fr0,0(%r1)	*/
    230 #define LFD_FR0_0R1	0xc8010000	/* lfd   %fr0,0(%r1)	*/
    231 #define LI_R12_0	0x39800000	/* li    %r12,0		*/
    232 #define STVX_VR0_R12_R0	0x7c0c01ce	/* stvx  %v0,%r12,%r0	*/
    233 #define LVX_VR0_R12_R0	0x7c0c00ce	/* lvx   %v0,%r12,%r0	*/
    234 #define MTLR_R0		0x7c0803a6	/* mtlr  %r0		*/
    235 #define BLR		0x4e800020	/* blr			*/
    236 
    237 /* Since .opd is an array of descriptors and each entry will end up
    238    with identical R_PPC64_RELATIVE relocs, there is really no need to
    239    propagate .opd relocs;  The dynamic linker should be taught to
    240    relocate .opd without reloc entries.  */
    241 #ifndef NO_OPD_RELOCS
    242 #define NO_OPD_RELOCS 0
    243 #endif
    244 
    245 #ifndef ARRAY_SIZE
    246 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
    247 #endif
    248 
    249 static inline int
    250 abiversion (bfd *abfd)
    251 {
    252   return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
    253 }
    254 
    255 static inline void
    256 set_abiversion (bfd *abfd, int ver)
    257 {
    258   elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
    259   elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
    260 }
    261 
    262 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
    264 
    265 /* Relocation HOWTO's.  */
    266 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
    267 
    268 static reloc_howto_type ppc64_elf_howto_raw[] = {
    269   /* This reloc does nothing.  */
    270   HOWTO (R_PPC64_NONE,		/* type */
    271 	 0,			/* rightshift */
    272 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
    273 	 0,			/* bitsize */
    274 	 FALSE,			/* pc_relative */
    275 	 0,			/* bitpos */
    276 	 complain_overflow_dont, /* complain_on_overflow */
    277 	 bfd_elf_generic_reloc,	/* special_function */
    278 	 "R_PPC64_NONE",	/* name */
    279 	 FALSE,			/* partial_inplace */
    280 	 0,			/* src_mask */
    281 	 0,			/* dst_mask */
    282 	 FALSE),		/* pcrel_offset */
    283 
    284   /* A standard 32 bit relocation.  */
    285   HOWTO (R_PPC64_ADDR32,	/* type */
    286 	 0,			/* rightshift */
    287 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    288 	 32,			/* bitsize */
    289 	 FALSE,			/* pc_relative */
    290 	 0,			/* bitpos */
    291 	 complain_overflow_bitfield, /* complain_on_overflow */
    292 	 bfd_elf_generic_reloc,	/* special_function */
    293 	 "R_PPC64_ADDR32",	/* name */
    294 	 FALSE,			/* partial_inplace */
    295 	 0,			/* src_mask */
    296 	 0xffffffff,		/* dst_mask */
    297 	 FALSE),		/* pcrel_offset */
    298 
    299   /* An absolute 26 bit branch; the lower two bits must be zero.
    300      FIXME: we don't check that, we just clear them.  */
    301   HOWTO (R_PPC64_ADDR24,	/* type */
    302 	 0,			/* rightshift */
    303 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    304 	 26,			/* bitsize */
    305 	 FALSE,			/* pc_relative */
    306 	 0,			/* bitpos */
    307 	 complain_overflow_bitfield, /* complain_on_overflow */
    308 	 bfd_elf_generic_reloc,	/* special_function */
    309 	 "R_PPC64_ADDR24",	/* name */
    310 	 FALSE,			/* partial_inplace */
    311 	 0,			/* src_mask */
    312 	 0x03fffffc,		/* dst_mask */
    313 	 FALSE),		/* pcrel_offset */
    314 
    315   /* A standard 16 bit relocation.  */
    316   HOWTO (R_PPC64_ADDR16,	/* type */
    317 	 0,			/* rightshift */
    318 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    319 	 16,			/* bitsize */
    320 	 FALSE,			/* pc_relative */
    321 	 0,			/* bitpos */
    322 	 complain_overflow_bitfield, /* complain_on_overflow */
    323 	 bfd_elf_generic_reloc,	/* special_function */
    324 	 "R_PPC64_ADDR16",	/* name */
    325 	 FALSE,			/* partial_inplace */
    326 	 0,			/* src_mask */
    327 	 0xffff,		/* dst_mask */
    328 	 FALSE),		/* pcrel_offset */
    329 
    330   /* A 16 bit relocation without overflow.  */
    331   HOWTO (R_PPC64_ADDR16_LO,	/* type */
    332 	 0,			/* rightshift */
    333 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    334 	 16,			/* bitsize */
    335 	 FALSE,			/* pc_relative */
    336 	 0,			/* bitpos */
    337 	 complain_overflow_dont,/* complain_on_overflow */
    338 	 bfd_elf_generic_reloc,	/* special_function */
    339 	 "R_PPC64_ADDR16_LO",	/* name */
    340 	 FALSE,			/* partial_inplace */
    341 	 0,			/* src_mask */
    342 	 0xffff,		/* dst_mask */
    343 	 FALSE),		/* pcrel_offset */
    344 
    345   /* Bits 16-31 of an address.  */
    346   HOWTO (R_PPC64_ADDR16_HI,	/* type */
    347 	 16,			/* rightshift */
    348 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    349 	 16,			/* bitsize */
    350 	 FALSE,			/* pc_relative */
    351 	 0,			/* bitpos */
    352 	 complain_overflow_signed, /* complain_on_overflow */
    353 	 bfd_elf_generic_reloc,	/* special_function */
    354 	 "R_PPC64_ADDR16_HI",	/* name */
    355 	 FALSE,			/* partial_inplace */
    356 	 0,			/* src_mask */
    357 	 0xffff,		/* dst_mask */
    358 	 FALSE),		/* pcrel_offset */
    359 
    360   /* Bits 16-31 of an address, plus 1 if the contents of the low 16
    361      bits, treated as a signed number, is negative.  */
    362   HOWTO (R_PPC64_ADDR16_HA,	/* type */
    363 	 16,			/* rightshift */
    364 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    365 	 16,			/* bitsize */
    366 	 FALSE,			/* pc_relative */
    367 	 0,			/* bitpos */
    368 	 complain_overflow_signed, /* complain_on_overflow */
    369 	 ppc64_elf_ha_reloc,	/* special_function */
    370 	 "R_PPC64_ADDR16_HA",	/* name */
    371 	 FALSE,			/* partial_inplace */
    372 	 0,			/* src_mask */
    373 	 0xffff,		/* dst_mask */
    374 	 FALSE),		/* pcrel_offset */
    375 
    376   /* An absolute 16 bit branch; the lower two bits must be zero.
    377      FIXME: we don't check that, we just clear them.  */
    378   HOWTO (R_PPC64_ADDR14,	/* type */
    379 	 0,			/* rightshift */
    380 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    381 	 16,			/* bitsize */
    382 	 FALSE,			/* pc_relative */
    383 	 0,			/* bitpos */
    384 	 complain_overflow_signed, /* complain_on_overflow */
    385 	 ppc64_elf_branch_reloc, /* special_function */
    386 	 "R_PPC64_ADDR14",	/* name */
    387 	 FALSE,			/* partial_inplace */
    388 	 0,			/* src_mask */
    389 	 0x0000fffc,		/* dst_mask */
    390 	 FALSE),		/* pcrel_offset */
    391 
    392   /* An absolute 16 bit branch, for which bit 10 should be set to
    393      indicate that the branch is expected to be taken.  The lower two
    394      bits must be zero.  */
    395   HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
    396 	 0,			/* rightshift */
    397 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    398 	 16,			/* bitsize */
    399 	 FALSE,			/* pc_relative */
    400 	 0,			/* bitpos */
    401 	 complain_overflow_signed, /* complain_on_overflow */
    402 	 ppc64_elf_brtaken_reloc, /* special_function */
    403 	 "R_PPC64_ADDR14_BRTAKEN",/* name */
    404 	 FALSE,			/* partial_inplace */
    405 	 0,			/* src_mask */
    406 	 0x0000fffc,		/* dst_mask */
    407 	 FALSE),		/* pcrel_offset */
    408 
    409   /* An absolute 16 bit branch, for which bit 10 should be set to
    410      indicate that the branch is not expected to be taken.  The lower
    411      two bits must be zero.  */
    412   HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
    413 	 0,			/* rightshift */
    414 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    415 	 16,			/* bitsize */
    416 	 FALSE,			/* pc_relative */
    417 	 0,			/* bitpos */
    418 	 complain_overflow_signed, /* complain_on_overflow */
    419 	 ppc64_elf_brtaken_reloc, /* special_function */
    420 	 "R_PPC64_ADDR14_BRNTAKEN",/* name */
    421 	 FALSE,			/* partial_inplace */
    422 	 0,			/* src_mask */
    423 	 0x0000fffc,		/* dst_mask */
    424 	 FALSE),		/* pcrel_offset */
    425 
    426   /* A relative 26 bit branch; the lower two bits must be zero.  */
    427   HOWTO (R_PPC64_REL24,		/* type */
    428 	 0,			/* rightshift */
    429 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    430 	 26,			/* bitsize */
    431 	 TRUE,			/* pc_relative */
    432 	 0,			/* bitpos */
    433 	 complain_overflow_signed, /* complain_on_overflow */
    434 	 ppc64_elf_branch_reloc, /* special_function */
    435 	 "R_PPC64_REL24",	/* name */
    436 	 FALSE,			/* partial_inplace */
    437 	 0,			/* src_mask */
    438 	 0x03fffffc,		/* dst_mask */
    439 	 TRUE),			/* pcrel_offset */
    440 
    441   /* A relative 16 bit branch; the lower two bits must be zero.  */
    442   HOWTO (R_PPC64_REL14,		/* type */
    443 	 0,			/* rightshift */
    444 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    445 	 16,			/* bitsize */
    446 	 TRUE,			/* pc_relative */
    447 	 0,			/* bitpos */
    448 	 complain_overflow_signed, /* complain_on_overflow */
    449 	 ppc64_elf_branch_reloc, /* special_function */
    450 	 "R_PPC64_REL14",	/* name */
    451 	 FALSE,			/* partial_inplace */
    452 	 0,			/* src_mask */
    453 	 0x0000fffc,		/* dst_mask */
    454 	 TRUE),			/* pcrel_offset */
    455 
    456   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    457      the branch is expected to be taken.  The lower two bits must be
    458      zero.  */
    459   HOWTO (R_PPC64_REL14_BRTAKEN,	/* type */
    460 	 0,			/* rightshift */
    461 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    462 	 16,			/* bitsize */
    463 	 TRUE,			/* pc_relative */
    464 	 0,			/* bitpos */
    465 	 complain_overflow_signed, /* complain_on_overflow */
    466 	 ppc64_elf_brtaken_reloc, /* special_function */
    467 	 "R_PPC64_REL14_BRTAKEN", /* name */
    468 	 FALSE,			/* partial_inplace */
    469 	 0,			/* src_mask */
    470 	 0x0000fffc,		/* dst_mask */
    471 	 TRUE),			/* pcrel_offset */
    472 
    473   /* A relative 16 bit branch.  Bit 10 should be set to indicate that
    474      the branch is not expected to be taken.  The lower two bits must
    475      be zero.  */
    476   HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
    477 	 0,			/* rightshift */
    478 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    479 	 16,			/* bitsize */
    480 	 TRUE,			/* pc_relative */
    481 	 0,			/* bitpos */
    482 	 complain_overflow_signed, /* complain_on_overflow */
    483 	 ppc64_elf_brtaken_reloc, /* special_function */
    484 	 "R_PPC64_REL14_BRNTAKEN",/* name */
    485 	 FALSE,			/* partial_inplace */
    486 	 0,			/* src_mask */
    487 	 0x0000fffc,		/* dst_mask */
    488 	 TRUE),			/* pcrel_offset */
    489 
    490   /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
    491      symbol.  */
    492   HOWTO (R_PPC64_GOT16,		/* type */
    493 	 0,			/* rightshift */
    494 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    495 	 16,			/* bitsize */
    496 	 FALSE,			/* pc_relative */
    497 	 0,			/* bitpos */
    498 	 complain_overflow_signed, /* complain_on_overflow */
    499 	 ppc64_elf_unhandled_reloc, /* special_function */
    500 	 "R_PPC64_GOT16",	/* name */
    501 	 FALSE,			/* partial_inplace */
    502 	 0,			/* src_mask */
    503 	 0xffff,		/* dst_mask */
    504 	 FALSE),		/* pcrel_offset */
    505 
    506   /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
    507      the symbol.  */
    508   HOWTO (R_PPC64_GOT16_LO,	/* type */
    509 	 0,			/* rightshift */
    510 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    511 	 16,			/* bitsize */
    512 	 FALSE,			/* pc_relative */
    513 	 0,			/* bitpos */
    514 	 complain_overflow_dont, /* complain_on_overflow */
    515 	 ppc64_elf_unhandled_reloc, /* special_function */
    516 	 "R_PPC64_GOT16_LO",	/* name */
    517 	 FALSE,			/* partial_inplace */
    518 	 0,			/* src_mask */
    519 	 0xffff,		/* dst_mask */
    520 	 FALSE),		/* pcrel_offset */
    521 
    522   /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
    523      the symbol.  */
    524   HOWTO (R_PPC64_GOT16_HI,	/* type */
    525 	 16,			/* rightshift */
    526 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    527 	 16,			/* bitsize */
    528 	 FALSE,			/* pc_relative */
    529 	 0,			/* bitpos */
    530 	 complain_overflow_signed,/* complain_on_overflow */
    531 	 ppc64_elf_unhandled_reloc, /* special_function */
    532 	 "R_PPC64_GOT16_HI",	/* name */
    533 	 FALSE,			/* partial_inplace */
    534 	 0,			/* src_mask */
    535 	 0xffff,		/* dst_mask */
    536 	 FALSE),		/* pcrel_offset */
    537 
    538   /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
    539      the symbol.  */
    540   HOWTO (R_PPC64_GOT16_HA,	/* type */
    541 	 16,			/* rightshift */
    542 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    543 	 16,			/* bitsize */
    544 	 FALSE,			/* pc_relative */
    545 	 0,			/* bitpos */
    546 	 complain_overflow_signed,/* complain_on_overflow */
    547 	 ppc64_elf_unhandled_reloc, /* special_function */
    548 	 "R_PPC64_GOT16_HA",	/* name */
    549 	 FALSE,			/* partial_inplace */
    550 	 0,			/* src_mask */
    551 	 0xffff,		/* dst_mask */
    552 	 FALSE),		/* pcrel_offset */
    553 
    554   /* This is used only by the dynamic linker.  The symbol should exist
    555      both in the object being run and in some shared library.  The
    556      dynamic linker copies the data addressed by the symbol from the
    557      shared library into the object, because the object being
    558      run has to have the data at some particular address.  */
    559   HOWTO (R_PPC64_COPY,		/* type */
    560 	 0,			/* rightshift */
    561 	 0,			/* this one is variable size */
    562 	 0,			/* bitsize */
    563 	 FALSE,			/* pc_relative */
    564 	 0,			/* bitpos */
    565 	 complain_overflow_dont, /* complain_on_overflow */
    566 	 ppc64_elf_unhandled_reloc, /* special_function */
    567 	 "R_PPC64_COPY",	/* name */
    568 	 FALSE,			/* partial_inplace */
    569 	 0,			/* src_mask */
    570 	 0,			/* dst_mask */
    571 	 FALSE),		/* pcrel_offset */
    572 
    573   /* Like R_PPC64_ADDR64, but used when setting global offset table
    574      entries.  */
    575   HOWTO (R_PPC64_GLOB_DAT,	/* type */
    576 	 0,			/* rightshift */
    577 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    578 	 64,			/* bitsize */
    579 	 FALSE,			/* pc_relative */
    580 	 0,			/* bitpos */
    581 	 complain_overflow_dont, /* complain_on_overflow */
    582 	 ppc64_elf_unhandled_reloc,  /* special_function */
    583 	 "R_PPC64_GLOB_DAT",	/* name */
    584 	 FALSE,			/* partial_inplace */
    585 	 0,			/* src_mask */
    586 	 ONES (64),		/* dst_mask */
    587 	 FALSE),		/* pcrel_offset */
    588 
    589   /* Created by the link editor.  Marks a procedure linkage table
    590      entry for a symbol.  */
    591   HOWTO (R_PPC64_JMP_SLOT,	/* type */
    592 	 0,			/* rightshift */
    593 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
    594 	 0,			/* bitsize */
    595 	 FALSE,			/* pc_relative */
    596 	 0,			/* bitpos */
    597 	 complain_overflow_dont, /* complain_on_overflow */
    598 	 ppc64_elf_unhandled_reloc, /* special_function */
    599 	 "R_PPC64_JMP_SLOT",	/* name */
    600 	 FALSE,			/* partial_inplace */
    601 	 0,			/* src_mask */
    602 	 0,			/* dst_mask */
    603 	 FALSE),		/* pcrel_offset */
    604 
    605   /* Used only by the dynamic linker.  When the object is run, this
    606      doubleword64 is set to the load address of the object, plus the
    607      addend.  */
    608   HOWTO (R_PPC64_RELATIVE,	/* type */
    609 	 0,			/* rightshift */
    610 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    611 	 64,			/* bitsize */
    612 	 FALSE,			/* pc_relative */
    613 	 0,			/* bitpos */
    614 	 complain_overflow_dont, /* complain_on_overflow */
    615 	 bfd_elf_generic_reloc,	/* special_function */
    616 	 "R_PPC64_RELATIVE",	/* name */
    617 	 FALSE,			/* partial_inplace */
    618 	 0,			/* src_mask */
    619 	 ONES (64),		/* dst_mask */
    620 	 FALSE),		/* pcrel_offset */
    621 
    622   /* Like R_PPC64_ADDR32, but may be unaligned.  */
    623   HOWTO (R_PPC64_UADDR32,	/* type */
    624 	 0,			/* rightshift */
    625 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    626 	 32,			/* bitsize */
    627 	 FALSE,			/* pc_relative */
    628 	 0,			/* bitpos */
    629 	 complain_overflow_bitfield, /* complain_on_overflow */
    630 	 bfd_elf_generic_reloc,	/* special_function */
    631 	 "R_PPC64_UADDR32",	/* name */
    632 	 FALSE,			/* partial_inplace */
    633 	 0,			/* src_mask */
    634 	 0xffffffff,		/* dst_mask */
    635 	 FALSE),		/* pcrel_offset */
    636 
    637   /* Like R_PPC64_ADDR16, but may be unaligned.  */
    638   HOWTO (R_PPC64_UADDR16,	/* type */
    639 	 0,			/* rightshift */
    640 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    641 	 16,			/* bitsize */
    642 	 FALSE,			/* pc_relative */
    643 	 0,			/* bitpos */
    644 	 complain_overflow_bitfield, /* complain_on_overflow */
    645 	 bfd_elf_generic_reloc,	/* special_function */
    646 	 "R_PPC64_UADDR16",	/* name */
    647 	 FALSE,			/* partial_inplace */
    648 	 0,			/* src_mask */
    649 	 0xffff,		/* dst_mask */
    650 	 FALSE),		/* pcrel_offset */
    651 
    652   /* 32-bit PC relative.  */
    653   HOWTO (R_PPC64_REL32,		/* type */
    654 	 0,			/* rightshift */
    655 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    656 	 32,			/* bitsize */
    657 	 TRUE,			/* pc_relative */
    658 	 0,			/* bitpos */
    659 	 complain_overflow_signed, /* complain_on_overflow */
    660 	 bfd_elf_generic_reloc,	/* special_function */
    661 	 "R_PPC64_REL32",	/* name */
    662 	 FALSE,			/* partial_inplace */
    663 	 0,			/* src_mask */
    664 	 0xffffffff,		/* dst_mask */
    665 	 TRUE),			/* pcrel_offset */
    666 
    667   /* 32-bit relocation to the symbol's procedure linkage table.  */
    668   HOWTO (R_PPC64_PLT32,		/* type */
    669 	 0,			/* rightshift */
    670 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    671 	 32,			/* bitsize */
    672 	 FALSE,			/* pc_relative */
    673 	 0,			/* bitpos */
    674 	 complain_overflow_bitfield, /* complain_on_overflow */
    675 	 ppc64_elf_unhandled_reloc, /* special_function */
    676 	 "R_PPC64_PLT32",	/* name */
    677 	 FALSE,			/* partial_inplace */
    678 	 0,			/* src_mask */
    679 	 0xffffffff,		/* dst_mask */
    680 	 FALSE),		/* pcrel_offset */
    681 
    682   /* 32-bit PC relative relocation to the symbol's procedure linkage table.
    683      FIXME: R_PPC64_PLTREL32 not supported.  */
    684   HOWTO (R_PPC64_PLTREL32,	/* type */
    685 	 0,			/* rightshift */
    686 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    687 	 32,			/* bitsize */
    688 	 TRUE,			/* pc_relative */
    689 	 0,			/* bitpos */
    690 	 complain_overflow_signed, /* complain_on_overflow */
    691 	 bfd_elf_generic_reloc,	/* special_function */
    692 	 "R_PPC64_PLTREL32",	/* name */
    693 	 FALSE,			/* partial_inplace */
    694 	 0,			/* src_mask */
    695 	 0xffffffff,		/* dst_mask */
    696 	 TRUE),			/* pcrel_offset */
    697 
    698   /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
    699      the symbol.  */
    700   HOWTO (R_PPC64_PLT16_LO,	/* type */
    701 	 0,			/* rightshift */
    702 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    703 	 16,			/* bitsize */
    704 	 FALSE,			/* pc_relative */
    705 	 0,			/* bitpos */
    706 	 complain_overflow_dont, /* complain_on_overflow */
    707 	 ppc64_elf_unhandled_reloc, /* special_function */
    708 	 "R_PPC64_PLT16_LO",	/* name */
    709 	 FALSE,			/* partial_inplace */
    710 	 0,			/* src_mask */
    711 	 0xffff,		/* dst_mask */
    712 	 FALSE),		/* pcrel_offset */
    713 
    714   /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
    715      the symbol.  */
    716   HOWTO (R_PPC64_PLT16_HI,	/* type */
    717 	 16,			/* rightshift */
    718 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    719 	 16,			/* bitsize */
    720 	 FALSE,			/* pc_relative */
    721 	 0,			/* bitpos */
    722 	 complain_overflow_signed, /* complain_on_overflow */
    723 	 ppc64_elf_unhandled_reloc, /* special_function */
    724 	 "R_PPC64_PLT16_HI",	/* name */
    725 	 FALSE,			/* partial_inplace */
    726 	 0,			/* src_mask */
    727 	 0xffff,		/* dst_mask */
    728 	 FALSE),		/* pcrel_offset */
    729 
    730   /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
    731      the symbol.  */
    732   HOWTO (R_PPC64_PLT16_HA,	/* type */
    733 	 16,			/* rightshift */
    734 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    735 	 16,			/* bitsize */
    736 	 FALSE,			/* pc_relative */
    737 	 0,			/* bitpos */
    738 	 complain_overflow_signed, /* complain_on_overflow */
    739 	 ppc64_elf_unhandled_reloc, /* special_function */
    740 	 "R_PPC64_PLT16_HA",	/* name */
    741 	 FALSE,			/* partial_inplace */
    742 	 0,			/* src_mask */
    743 	 0xffff,		/* dst_mask */
    744 	 FALSE),		/* pcrel_offset */
    745 
    746   /* 16-bit section relative relocation.  */
    747   HOWTO (R_PPC64_SECTOFF,	/* type */
    748 	 0,			/* rightshift */
    749 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    750 	 16,			/* bitsize */
    751 	 FALSE,			/* pc_relative */
    752 	 0,			/* bitpos */
    753 	 complain_overflow_signed, /* complain_on_overflow */
    754 	 ppc64_elf_sectoff_reloc, /* special_function */
    755 	 "R_PPC64_SECTOFF",	/* name */
    756 	 FALSE,			/* partial_inplace */
    757 	 0,			/* src_mask */
    758 	 0xffff,		/* dst_mask */
    759 	 FALSE),		/* pcrel_offset */
    760 
    761   /* Like R_PPC64_SECTOFF, but no overflow warning.  */
    762   HOWTO (R_PPC64_SECTOFF_LO,	/* type */
    763 	 0,			/* rightshift */
    764 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    765 	 16,			/* bitsize */
    766 	 FALSE,			/* pc_relative */
    767 	 0,			/* bitpos */
    768 	 complain_overflow_dont, /* complain_on_overflow */
    769 	 ppc64_elf_sectoff_reloc, /* special_function */
    770 	 "R_PPC64_SECTOFF_LO",	/* name */
    771 	 FALSE,			/* partial_inplace */
    772 	 0,			/* src_mask */
    773 	 0xffff,		/* dst_mask */
    774 	 FALSE),		/* pcrel_offset */
    775 
    776   /* 16-bit upper half section relative relocation.  */
    777   HOWTO (R_PPC64_SECTOFF_HI,	/* type */
    778 	 16,			/* rightshift */
    779 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    780 	 16,			/* bitsize */
    781 	 FALSE,			/* pc_relative */
    782 	 0,			/* bitpos */
    783 	 complain_overflow_signed, /* complain_on_overflow */
    784 	 ppc64_elf_sectoff_reloc, /* special_function */
    785 	 "R_PPC64_SECTOFF_HI",	/* name */
    786 	 FALSE,			/* partial_inplace */
    787 	 0,			/* src_mask */
    788 	 0xffff,		/* dst_mask */
    789 	 FALSE),		/* pcrel_offset */
    790 
    791   /* 16-bit upper half adjusted section relative relocation.  */
    792   HOWTO (R_PPC64_SECTOFF_HA,	/* type */
    793 	 16,			/* rightshift */
    794 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    795 	 16,			/* bitsize */
    796 	 FALSE,			/* pc_relative */
    797 	 0,			/* bitpos */
    798 	 complain_overflow_signed, /* complain_on_overflow */
    799 	 ppc64_elf_sectoff_ha_reloc, /* special_function */
    800 	 "R_PPC64_SECTOFF_HA",	/* name */
    801 	 FALSE,			/* partial_inplace */
    802 	 0,			/* src_mask */
    803 	 0xffff,		/* dst_mask */
    804 	 FALSE),		/* pcrel_offset */
    805 
    806   /* Like R_PPC64_REL24 without touching the two least significant bits.  */
    807   HOWTO (R_PPC64_REL30,		/* type */
    808 	 2,			/* rightshift */
    809 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    810 	 30,			/* bitsize */
    811 	 TRUE,			/* pc_relative */
    812 	 0,			/* bitpos */
    813 	 complain_overflow_dont, /* complain_on_overflow */
    814 	 bfd_elf_generic_reloc, /* special_function */
    815 	 "R_PPC64_REL30",	/* name */
    816 	 FALSE,			/* partial_inplace */
    817 	 0,			/* src_mask */
    818 	 0xfffffffc,		/* dst_mask */
    819 	 TRUE),			/* pcrel_offset */
    820 
    821   /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI.  */
    822 
    823   /* A standard 64-bit relocation.  */
    824   HOWTO (R_PPC64_ADDR64,	/* type */
    825 	 0,			/* rightshift */
    826 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    827 	 64,			/* bitsize */
    828 	 FALSE,			/* pc_relative */
    829 	 0,			/* bitpos */
    830 	 complain_overflow_dont, /* complain_on_overflow */
    831 	 bfd_elf_generic_reloc,	/* special_function */
    832 	 "R_PPC64_ADDR64",	/* name */
    833 	 FALSE,			/* partial_inplace */
    834 	 0,			/* src_mask */
    835 	 ONES (64),		/* dst_mask */
    836 	 FALSE),		/* pcrel_offset */
    837 
    838   /* The bits 32-47 of an address.  */
    839   HOWTO (R_PPC64_ADDR16_HIGHER,	/* type */
    840 	 32,			/* rightshift */
    841 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    842 	 16,			/* bitsize */
    843 	 FALSE,			/* pc_relative */
    844 	 0,			/* bitpos */
    845 	 complain_overflow_dont, /* complain_on_overflow */
    846 	 bfd_elf_generic_reloc,	/* special_function */
    847 	 "R_PPC64_ADDR16_HIGHER", /* name */
    848 	 FALSE,			/* partial_inplace */
    849 	 0,			/* src_mask */
    850 	 0xffff,		/* dst_mask */
    851 	 FALSE),		/* pcrel_offset */
    852 
    853   /* The bits 32-47 of an address, plus 1 if the contents of the low
    854      16 bits, treated as a signed number, is negative.  */
    855   HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
    856 	 32,			/* rightshift */
    857 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    858 	 16,			/* bitsize */
    859 	 FALSE,			/* pc_relative */
    860 	 0,			/* bitpos */
    861 	 complain_overflow_dont, /* complain_on_overflow */
    862 	 ppc64_elf_ha_reloc,	/* special_function */
    863 	 "R_PPC64_ADDR16_HIGHERA", /* name */
    864 	 FALSE,			/* partial_inplace */
    865 	 0,			/* src_mask */
    866 	 0xffff,		/* dst_mask */
    867 	 FALSE),		/* pcrel_offset */
    868 
    869   /* The bits 48-63 of an address.  */
    870   HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
    871 	 48,			/* rightshift */
    872 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    873 	 16,			/* bitsize */
    874 	 FALSE,			/* pc_relative */
    875 	 0,			/* bitpos */
    876 	 complain_overflow_dont, /* complain_on_overflow */
    877 	 bfd_elf_generic_reloc,	/* special_function */
    878 	 "R_PPC64_ADDR16_HIGHEST", /* name */
    879 	 FALSE,			/* partial_inplace */
    880 	 0,			/* src_mask */
    881 	 0xffff,		/* dst_mask */
    882 	 FALSE),		/* pcrel_offset */
    883 
    884   /* The bits 48-63 of an address, plus 1 if the contents of the low
    885      16 bits, treated as a signed number, is negative.  */
    886   HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
    887 	 48,			/* rightshift */
    888 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    889 	 16,			/* bitsize */
    890 	 FALSE,			/* pc_relative */
    891 	 0,			/* bitpos */
    892 	 complain_overflow_dont, /* complain_on_overflow */
    893 	 ppc64_elf_ha_reloc,	/* special_function */
    894 	 "R_PPC64_ADDR16_HIGHESTA", /* name */
    895 	 FALSE,			/* partial_inplace */
    896 	 0,			/* src_mask */
    897 	 0xffff,		/* dst_mask */
    898 	 FALSE),		/* pcrel_offset */
    899 
    900   /* Like ADDR64, but may be unaligned.  */
    901   HOWTO (R_PPC64_UADDR64,	/* type */
    902 	 0,			/* rightshift */
    903 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    904 	 64,			/* bitsize */
    905 	 FALSE,			/* pc_relative */
    906 	 0,			/* bitpos */
    907 	 complain_overflow_dont, /* complain_on_overflow */
    908 	 bfd_elf_generic_reloc,	/* special_function */
    909 	 "R_PPC64_UADDR64",	/* name */
    910 	 FALSE,			/* partial_inplace */
    911 	 0,			/* src_mask */
    912 	 ONES (64),		/* dst_mask */
    913 	 FALSE),		/* pcrel_offset */
    914 
    915   /* 64-bit relative relocation.  */
    916   HOWTO (R_PPC64_REL64,		/* type */
    917 	 0,			/* rightshift */
    918 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    919 	 64,			/* bitsize */
    920 	 TRUE,			/* pc_relative */
    921 	 0,			/* bitpos */
    922 	 complain_overflow_dont, /* complain_on_overflow */
    923 	 bfd_elf_generic_reloc,	/* special_function */
    924 	 "R_PPC64_REL64",	/* name */
    925 	 FALSE,			/* partial_inplace */
    926 	 0,			/* src_mask */
    927 	 ONES (64),		/* dst_mask */
    928 	 TRUE),			/* pcrel_offset */
    929 
    930   /* 64-bit relocation to the symbol's procedure linkage table.  */
    931   HOWTO (R_PPC64_PLT64,		/* type */
    932 	 0,			/* rightshift */
    933 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    934 	 64,			/* bitsize */
    935 	 FALSE,			/* pc_relative */
    936 	 0,			/* bitpos */
    937 	 complain_overflow_dont, /* complain_on_overflow */
    938 	 ppc64_elf_unhandled_reloc, /* special_function */
    939 	 "R_PPC64_PLT64",	/* name */
    940 	 FALSE,			/* partial_inplace */
    941 	 0,			/* src_mask */
    942 	 ONES (64),		/* dst_mask */
    943 	 FALSE),		/* pcrel_offset */
    944 
    945   /* 64-bit PC relative relocation to the symbol's procedure linkage
    946      table.  */
    947   /* FIXME: R_PPC64_PLTREL64 not supported.  */
    948   HOWTO (R_PPC64_PLTREL64,	/* type */
    949 	 0,			/* rightshift */
    950 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
    951 	 64,			/* bitsize */
    952 	 TRUE,			/* pc_relative */
    953 	 0,			/* bitpos */
    954 	 complain_overflow_dont, /* complain_on_overflow */
    955 	 ppc64_elf_unhandled_reloc, /* special_function */
    956 	 "R_PPC64_PLTREL64",	/* name */
    957 	 FALSE,			/* partial_inplace */
    958 	 0,			/* src_mask */
    959 	 ONES (64),		/* dst_mask */
    960 	 TRUE),			/* pcrel_offset */
    961 
    962   /* 16 bit TOC-relative relocation.  */
    963 
    964   /* R_PPC64_TOC16	  47	   half16*	S + A - .TOC.  */
    965   HOWTO (R_PPC64_TOC16,		/* type */
    966 	 0,			/* rightshift */
    967 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    968 	 16,			/* bitsize */
    969 	 FALSE,			/* pc_relative */
    970 	 0,			/* bitpos */
    971 	 complain_overflow_signed, /* complain_on_overflow */
    972 	 ppc64_elf_toc_reloc,	/* special_function */
    973 	 "R_PPC64_TOC16",	/* name */
    974 	 FALSE,			/* partial_inplace */
    975 	 0,			/* src_mask */
    976 	 0xffff,		/* dst_mask */
    977 	 FALSE),		/* pcrel_offset */
    978 
    979   /* 16 bit TOC-relative relocation without overflow.  */
    980 
    981   /* R_PPC64_TOC16_LO	  48	   half16	 #lo (S + A - .TOC.)  */
    982   HOWTO (R_PPC64_TOC16_LO,	/* type */
    983 	 0,			/* rightshift */
    984 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    985 	 16,			/* bitsize */
    986 	 FALSE,			/* pc_relative */
    987 	 0,			/* bitpos */
    988 	 complain_overflow_dont, /* complain_on_overflow */
    989 	 ppc64_elf_toc_reloc,	/* special_function */
    990 	 "R_PPC64_TOC16_LO",	/* name */
    991 	 FALSE,			/* partial_inplace */
    992 	 0,			/* src_mask */
    993 	 0xffff,		/* dst_mask */
    994 	 FALSE),		/* pcrel_offset */
    995 
    996   /* 16 bit TOC-relative relocation, high 16 bits.  */
    997 
    998   /* R_PPC64_TOC16_HI	  49	   half16	 #hi (S + A - .TOC.)  */
    999   HOWTO (R_PPC64_TOC16_HI,	/* type */
   1000 	 16,			/* rightshift */
   1001 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1002 	 16,			/* bitsize */
   1003 	 FALSE,			/* pc_relative */
   1004 	 0,			/* bitpos */
   1005 	 complain_overflow_signed, /* complain_on_overflow */
   1006 	 ppc64_elf_toc_reloc,	/* special_function */
   1007 	 "R_PPC64_TOC16_HI",	/* name */
   1008 	 FALSE,			/* partial_inplace */
   1009 	 0,			/* src_mask */
   1010 	 0xffff,		/* dst_mask */
   1011 	 FALSE),		/* pcrel_offset */
   1012 
   1013   /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
   1014      contents of the low 16 bits, treated as a signed number, is
   1015      negative.  */
   1016 
   1017   /* R_PPC64_TOC16_HA	  50	   half16	 #ha (S + A - .TOC.)  */
   1018   HOWTO (R_PPC64_TOC16_HA,	/* type */
   1019 	 16,			/* rightshift */
   1020 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1021 	 16,			/* bitsize */
   1022 	 FALSE,			/* pc_relative */
   1023 	 0,			/* bitpos */
   1024 	 complain_overflow_signed, /* complain_on_overflow */
   1025 	 ppc64_elf_toc_ha_reloc, /* special_function */
   1026 	 "R_PPC64_TOC16_HA",	/* name */
   1027 	 FALSE,			/* partial_inplace */
   1028 	 0,			/* src_mask */
   1029 	 0xffff,		/* dst_mask */
   1030 	 FALSE),		/* pcrel_offset */
   1031 
   1032   /* 64-bit relocation; insert value of TOC base (.TOC.).  */
   1033 
   1034   /* R_PPC64_TOC		  51	   doubleword64	 .TOC.  */
   1035   HOWTO (R_PPC64_TOC,		/* type */
   1036 	 0,			/* rightshift */
   1037 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1038 	 64,			/* bitsize */
   1039 	 FALSE,			/* pc_relative */
   1040 	 0,			/* bitpos */
   1041 	 complain_overflow_dont, /* complain_on_overflow */
   1042 	 ppc64_elf_toc64_reloc,	/* special_function */
   1043 	 "R_PPC64_TOC",		/* name */
   1044 	 FALSE,			/* partial_inplace */
   1045 	 0,			/* src_mask */
   1046 	 ONES (64),		/* dst_mask */
   1047 	 FALSE),		/* pcrel_offset */
   1048 
   1049   /* Like R_PPC64_GOT16, but also informs the link editor that the
   1050      value to relocate may (!) refer to a PLT entry which the link
   1051      editor (a) may replace with the symbol value.  If the link editor
   1052      is unable to fully resolve the symbol, it may (b) create a PLT
   1053      entry and store the address to the new PLT entry in the GOT.
   1054      This permits lazy resolution of function symbols at run time.
   1055      The link editor may also skip all of this and just (c) emit a
   1056      R_PPC64_GLOB_DAT to tie the symbol to the GOT entry.  */
   1057   /* FIXME: R_PPC64_PLTGOT16 not implemented.  */
   1058     HOWTO (R_PPC64_PLTGOT16,	/* type */
   1059 	 0,			/* rightshift */
   1060 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1061 	 16,			/* bitsize */
   1062 	 FALSE,			/* pc_relative */
   1063 	 0,			/* bitpos */
   1064 	 complain_overflow_signed, /* complain_on_overflow */
   1065 	 ppc64_elf_unhandled_reloc, /* special_function */
   1066 	 "R_PPC64_PLTGOT16",	/* name */
   1067 	 FALSE,			/* partial_inplace */
   1068 	 0,			/* src_mask */
   1069 	 0xffff,		/* dst_mask */
   1070 	 FALSE),		/* pcrel_offset */
   1071 
   1072   /* Like R_PPC64_PLTGOT16, but without overflow.  */
   1073   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1074   HOWTO (R_PPC64_PLTGOT16_LO,	/* type */
   1075 	 0,			/* rightshift */
   1076 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1077 	 16,			/* bitsize */
   1078 	 FALSE,			/* pc_relative */
   1079 	 0,			/* bitpos */
   1080 	 complain_overflow_dont, /* complain_on_overflow */
   1081 	 ppc64_elf_unhandled_reloc, /* special_function */
   1082 	 "R_PPC64_PLTGOT16_LO",	/* name */
   1083 	 FALSE,			/* partial_inplace */
   1084 	 0,			/* src_mask */
   1085 	 0xffff,		/* dst_mask */
   1086 	 FALSE),		/* pcrel_offset */
   1087 
   1088   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address.  */
   1089   /* FIXME: R_PPC64_PLTGOT16_HI not implemented.  */
   1090   HOWTO (R_PPC64_PLTGOT16_HI,	/* type */
   1091 	 16,			/* rightshift */
   1092 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1093 	 16,			/* bitsize */
   1094 	 FALSE,			/* pc_relative */
   1095 	 0,			/* bitpos */
   1096 	 complain_overflow_signed, /* complain_on_overflow */
   1097 	 ppc64_elf_unhandled_reloc, /* special_function */
   1098 	 "R_PPC64_PLTGOT16_HI",	/* name */
   1099 	 FALSE,			/* partial_inplace */
   1100 	 0,			/* src_mask */
   1101 	 0xffff,		/* dst_mask */
   1102 	 FALSE),		/* pcrel_offset */
   1103 
   1104   /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
   1105      1 if the contents of the low 16 bits, treated as a signed number,
   1106      is negative.  */
   1107   /* FIXME: R_PPC64_PLTGOT16_HA not implemented.  */
   1108   HOWTO (R_PPC64_PLTGOT16_HA,	/* type */
   1109 	 16,			/* rightshift */
   1110 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1111 	 16,			/* bitsize */
   1112 	 FALSE,			/* pc_relative */
   1113 	 0,			/* bitpos */
   1114 	 complain_overflow_signed, /* complain_on_overflow */
   1115 	 ppc64_elf_unhandled_reloc, /* special_function */
   1116 	 "R_PPC64_PLTGOT16_HA",	/* name */
   1117 	 FALSE,			/* partial_inplace */
   1118 	 0,			/* src_mask */
   1119 	 0xffff,		/* dst_mask */
   1120 	 FALSE),		/* pcrel_offset */
   1121 
   1122   /* Like R_PPC64_ADDR16, but for instructions with a DS field.  */
   1123   HOWTO (R_PPC64_ADDR16_DS,	/* type */
   1124 	 0,			/* rightshift */
   1125 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1126 	 16,			/* bitsize */
   1127 	 FALSE,			/* pc_relative */
   1128 	 0,			/* bitpos */
   1129 	 complain_overflow_signed, /* complain_on_overflow */
   1130 	 bfd_elf_generic_reloc,	/* special_function */
   1131 	 "R_PPC64_ADDR16_DS",	/* name */
   1132 	 FALSE,			/* partial_inplace */
   1133 	 0,			/* src_mask */
   1134 	 0xfffc,		/* dst_mask */
   1135 	 FALSE),		/* pcrel_offset */
   1136 
   1137   /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field.  */
   1138   HOWTO (R_PPC64_ADDR16_LO_DS,	/* type */
   1139 	 0,			/* rightshift */
   1140 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1141 	 16,			/* bitsize */
   1142 	 FALSE,			/* pc_relative */
   1143 	 0,			/* bitpos */
   1144 	 complain_overflow_dont,/* complain_on_overflow */
   1145 	 bfd_elf_generic_reloc,	/* special_function */
   1146 	 "R_PPC64_ADDR16_LO_DS",/* name */
   1147 	 FALSE,			/* partial_inplace */
   1148 	 0,			/* src_mask */
   1149 	 0xfffc,		/* dst_mask */
   1150 	 FALSE),		/* pcrel_offset */
   1151 
   1152   /* Like R_PPC64_GOT16, but for instructions with a DS field.  */
   1153   HOWTO (R_PPC64_GOT16_DS,	/* type */
   1154 	 0,			/* rightshift */
   1155 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1156 	 16,			/* bitsize */
   1157 	 FALSE,			/* pc_relative */
   1158 	 0,			/* bitpos */
   1159 	 complain_overflow_signed, /* complain_on_overflow */
   1160 	 ppc64_elf_unhandled_reloc, /* special_function */
   1161 	 "R_PPC64_GOT16_DS",	/* name */
   1162 	 FALSE,			/* partial_inplace */
   1163 	 0,			/* src_mask */
   1164 	 0xfffc,		/* dst_mask */
   1165 	 FALSE),		/* pcrel_offset */
   1166 
   1167   /* Like R_PPC64_GOT16_LO, but for instructions with a DS field.  */
   1168   HOWTO (R_PPC64_GOT16_LO_DS,	/* type */
   1169 	 0,			/* rightshift */
   1170 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1171 	 16,			/* bitsize */
   1172 	 FALSE,			/* pc_relative */
   1173 	 0,			/* bitpos */
   1174 	 complain_overflow_dont, /* complain_on_overflow */
   1175 	 ppc64_elf_unhandled_reloc, /* special_function */
   1176 	 "R_PPC64_GOT16_LO_DS",	/* name */
   1177 	 FALSE,			/* partial_inplace */
   1178 	 0,			/* src_mask */
   1179 	 0xfffc,		/* dst_mask */
   1180 	 FALSE),		/* pcrel_offset */
   1181 
   1182   /* Like R_PPC64_PLT16_LO, but for instructions with a DS field.  */
   1183   HOWTO (R_PPC64_PLT16_LO_DS,	/* type */
   1184 	 0,			/* rightshift */
   1185 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1186 	 16,			/* bitsize */
   1187 	 FALSE,			/* pc_relative */
   1188 	 0,			/* bitpos */
   1189 	 complain_overflow_dont, /* complain_on_overflow */
   1190 	 ppc64_elf_unhandled_reloc, /* special_function */
   1191 	 "R_PPC64_PLT16_LO_DS",	/* name */
   1192 	 FALSE,			/* partial_inplace */
   1193 	 0,			/* src_mask */
   1194 	 0xfffc,		/* dst_mask */
   1195 	 FALSE),		/* pcrel_offset */
   1196 
   1197   /* Like R_PPC64_SECTOFF, but for instructions with a DS field.  */
   1198   HOWTO (R_PPC64_SECTOFF_DS,	/* type */
   1199 	 0,			/* rightshift */
   1200 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1201 	 16,			/* bitsize */
   1202 	 FALSE,			/* pc_relative */
   1203 	 0,			/* bitpos */
   1204 	 complain_overflow_signed, /* complain_on_overflow */
   1205 	 ppc64_elf_sectoff_reloc, /* special_function */
   1206 	 "R_PPC64_SECTOFF_DS",	/* name */
   1207 	 FALSE,			/* partial_inplace */
   1208 	 0,			/* src_mask */
   1209 	 0xfffc,		/* dst_mask */
   1210 	 FALSE),		/* pcrel_offset */
   1211 
   1212   /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field.  */
   1213   HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
   1214 	 0,			/* rightshift */
   1215 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1216 	 16,			/* bitsize */
   1217 	 FALSE,			/* pc_relative */
   1218 	 0,			/* bitpos */
   1219 	 complain_overflow_dont, /* complain_on_overflow */
   1220 	 ppc64_elf_sectoff_reloc, /* special_function */
   1221 	 "R_PPC64_SECTOFF_LO_DS",/* name */
   1222 	 FALSE,			/* partial_inplace */
   1223 	 0,			/* src_mask */
   1224 	 0xfffc,		/* dst_mask */
   1225 	 FALSE),		/* pcrel_offset */
   1226 
   1227   /* Like R_PPC64_TOC16, but for instructions with a DS field.  */
   1228   HOWTO (R_PPC64_TOC16_DS,	/* type */
   1229 	 0,			/* rightshift */
   1230 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1231 	 16,			/* bitsize */
   1232 	 FALSE,			/* pc_relative */
   1233 	 0,			/* bitpos */
   1234 	 complain_overflow_signed, /* complain_on_overflow */
   1235 	 ppc64_elf_toc_reloc,	/* special_function */
   1236 	 "R_PPC64_TOC16_DS",	/* name */
   1237 	 FALSE,			/* partial_inplace */
   1238 	 0,			/* src_mask */
   1239 	 0xfffc,		/* dst_mask */
   1240 	 FALSE),		/* pcrel_offset */
   1241 
   1242   /* Like R_PPC64_TOC16_LO, but for instructions with a DS field.  */
   1243   HOWTO (R_PPC64_TOC16_LO_DS,	/* type */
   1244 	 0,			/* rightshift */
   1245 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1246 	 16,			/* bitsize */
   1247 	 FALSE,			/* pc_relative */
   1248 	 0,			/* bitpos */
   1249 	 complain_overflow_dont, /* complain_on_overflow */
   1250 	 ppc64_elf_toc_reloc,	/* special_function */
   1251 	 "R_PPC64_TOC16_LO_DS",	/* name */
   1252 	 FALSE,			/* partial_inplace */
   1253 	 0,			/* src_mask */
   1254 	 0xfffc,		/* dst_mask */
   1255 	 FALSE),		/* pcrel_offset */
   1256 
   1257   /* Like R_PPC64_PLTGOT16, but for instructions with a DS field.  */
   1258   /* FIXME: R_PPC64_PLTGOT16_DS not implemented.  */
   1259   HOWTO (R_PPC64_PLTGOT16_DS,	/* type */
   1260 	 0,			/* rightshift */
   1261 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1262 	 16,			/* bitsize */
   1263 	 FALSE,			/* pc_relative */
   1264 	 0,			/* bitpos */
   1265 	 complain_overflow_signed, /* complain_on_overflow */
   1266 	 ppc64_elf_unhandled_reloc, /* special_function */
   1267 	 "R_PPC64_PLTGOT16_DS",	/* name */
   1268 	 FALSE,			/* partial_inplace */
   1269 	 0,			/* src_mask */
   1270 	 0xfffc,		/* dst_mask */
   1271 	 FALSE),		/* pcrel_offset */
   1272 
   1273   /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field.  */
   1274   /* FIXME: R_PPC64_PLTGOT16_LO not implemented.  */
   1275   HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
   1276 	 0,			/* rightshift */
   1277 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1278 	 16,			/* bitsize */
   1279 	 FALSE,			/* pc_relative */
   1280 	 0,			/* bitpos */
   1281 	 complain_overflow_dont, /* complain_on_overflow */
   1282 	 ppc64_elf_unhandled_reloc, /* special_function */
   1283 	 "R_PPC64_PLTGOT16_LO_DS",/* name */
   1284 	 FALSE,			/* partial_inplace */
   1285 	 0,			/* src_mask */
   1286 	 0xfffc,		/* dst_mask */
   1287 	 FALSE),		/* pcrel_offset */
   1288 
   1289   /* Marker relocs for TLS.  */
   1290   HOWTO (R_PPC64_TLS,
   1291 	 0,			/* rightshift */
   1292 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1293 	 32,			/* bitsize */
   1294 	 FALSE,			/* pc_relative */
   1295 	 0,			/* bitpos */
   1296 	 complain_overflow_dont, /* complain_on_overflow */
   1297 	 bfd_elf_generic_reloc,	/* special_function */
   1298 	 "R_PPC64_TLS",		/* name */
   1299 	 FALSE,			/* partial_inplace */
   1300 	 0,			/* src_mask */
   1301 	 0,			/* dst_mask */
   1302 	 FALSE),		/* pcrel_offset */
   1303 
   1304   HOWTO (R_PPC64_TLSGD,
   1305 	 0,			/* rightshift */
   1306 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1307 	 32,			/* bitsize */
   1308 	 FALSE,			/* pc_relative */
   1309 	 0,			/* bitpos */
   1310 	 complain_overflow_dont, /* complain_on_overflow */
   1311 	 bfd_elf_generic_reloc,	/* special_function */
   1312 	 "R_PPC64_TLSGD",	/* name */
   1313 	 FALSE,			/* partial_inplace */
   1314 	 0,			/* src_mask */
   1315 	 0,			/* dst_mask */
   1316 	 FALSE),		/* pcrel_offset */
   1317 
   1318   HOWTO (R_PPC64_TLSLD,
   1319 	 0,			/* rightshift */
   1320 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1321 	 32,			/* bitsize */
   1322 	 FALSE,			/* pc_relative */
   1323 	 0,			/* bitpos */
   1324 	 complain_overflow_dont, /* complain_on_overflow */
   1325 	 bfd_elf_generic_reloc,	/* special_function */
   1326 	 "R_PPC64_TLSLD",	/* name */
   1327 	 FALSE,			/* partial_inplace */
   1328 	 0,			/* src_mask */
   1329 	 0,			/* dst_mask */
   1330 	 FALSE),		/* pcrel_offset */
   1331 
   1332   HOWTO (R_PPC64_TOCSAVE,
   1333 	 0,			/* rightshift */
   1334 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1335 	 32,			/* bitsize */
   1336 	 FALSE,			/* pc_relative */
   1337 	 0,			/* bitpos */
   1338 	 complain_overflow_dont, /* complain_on_overflow */
   1339 	 bfd_elf_generic_reloc,	/* special_function */
   1340 	 "R_PPC64_TOCSAVE",	/* name */
   1341 	 FALSE,			/* partial_inplace */
   1342 	 0,			/* src_mask */
   1343 	 0,			/* dst_mask */
   1344 	 FALSE),		/* pcrel_offset */
   1345 
   1346   /* Computes the load module index of the load module that contains the
   1347      definition of its TLS sym.  */
   1348   HOWTO (R_PPC64_DTPMOD64,
   1349 	 0,			/* rightshift */
   1350 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1351 	 64,			/* bitsize */
   1352 	 FALSE,			/* pc_relative */
   1353 	 0,			/* bitpos */
   1354 	 complain_overflow_dont, /* complain_on_overflow */
   1355 	 ppc64_elf_unhandled_reloc, /* special_function */
   1356 	 "R_PPC64_DTPMOD64",	/* name */
   1357 	 FALSE,			/* partial_inplace */
   1358 	 0,			/* src_mask */
   1359 	 ONES (64),		/* dst_mask */
   1360 	 FALSE),		/* pcrel_offset */
   1361 
   1362   /* Computes a dtv-relative displacement, the difference between the value
   1363      of sym+add and the base address of the thread-local storage block that
   1364      contains the definition of sym, minus 0x8000.  */
   1365   HOWTO (R_PPC64_DTPREL64,
   1366 	 0,			/* rightshift */
   1367 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1368 	 64,			/* bitsize */
   1369 	 FALSE,			/* pc_relative */
   1370 	 0,			/* bitpos */
   1371 	 complain_overflow_dont, /* complain_on_overflow */
   1372 	 ppc64_elf_unhandled_reloc, /* special_function */
   1373 	 "R_PPC64_DTPREL64",	/* name */
   1374 	 FALSE,			/* partial_inplace */
   1375 	 0,			/* src_mask */
   1376 	 ONES (64),		/* dst_mask */
   1377 	 FALSE),		/* pcrel_offset */
   1378 
   1379   /* A 16 bit dtprel reloc.  */
   1380   HOWTO (R_PPC64_DTPREL16,
   1381 	 0,			/* rightshift */
   1382 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1383 	 16,			/* bitsize */
   1384 	 FALSE,			/* pc_relative */
   1385 	 0,			/* bitpos */
   1386 	 complain_overflow_signed, /* complain_on_overflow */
   1387 	 ppc64_elf_unhandled_reloc, /* special_function */
   1388 	 "R_PPC64_DTPREL16",	/* name */
   1389 	 FALSE,			/* partial_inplace */
   1390 	 0,			/* src_mask */
   1391 	 0xffff,		/* dst_mask */
   1392 	 FALSE),		/* pcrel_offset */
   1393 
   1394   /* Like DTPREL16, but no overflow.  */
   1395   HOWTO (R_PPC64_DTPREL16_LO,
   1396 	 0,			/* rightshift */
   1397 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1398 	 16,			/* bitsize */
   1399 	 FALSE,			/* pc_relative */
   1400 	 0,			/* bitpos */
   1401 	 complain_overflow_dont, /* complain_on_overflow */
   1402 	 ppc64_elf_unhandled_reloc, /* special_function */
   1403 	 "R_PPC64_DTPREL16_LO",	/* name */
   1404 	 FALSE,			/* partial_inplace */
   1405 	 0,			/* src_mask */
   1406 	 0xffff,		/* dst_mask */
   1407 	 FALSE),		/* pcrel_offset */
   1408 
   1409   /* Like DTPREL16_LO, but next higher group of 16 bits.  */
   1410   HOWTO (R_PPC64_DTPREL16_HI,
   1411 	 16,			/* rightshift */
   1412 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1413 	 16,			/* bitsize */
   1414 	 FALSE,			/* pc_relative */
   1415 	 0,			/* bitpos */
   1416 	 complain_overflow_signed, /* complain_on_overflow */
   1417 	 ppc64_elf_unhandled_reloc, /* special_function */
   1418 	 "R_PPC64_DTPREL16_HI",	/* name */
   1419 	 FALSE,			/* partial_inplace */
   1420 	 0,			/* src_mask */
   1421 	 0xffff,		/* dst_mask */
   1422 	 FALSE),		/* pcrel_offset */
   1423 
   1424   /* Like DTPREL16_HI, but adjust for low 16 bits.  */
   1425   HOWTO (R_PPC64_DTPREL16_HA,
   1426 	 16,			/* rightshift */
   1427 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1428 	 16,			/* bitsize */
   1429 	 FALSE,			/* pc_relative */
   1430 	 0,			/* bitpos */
   1431 	 complain_overflow_signed, /* complain_on_overflow */
   1432 	 ppc64_elf_unhandled_reloc, /* special_function */
   1433 	 "R_PPC64_DTPREL16_HA",	/* name */
   1434 	 FALSE,			/* partial_inplace */
   1435 	 0,			/* src_mask */
   1436 	 0xffff,		/* dst_mask */
   1437 	 FALSE),		/* pcrel_offset */
   1438 
   1439   /* Like DTPREL16_HI, but next higher group of 16 bits.  */
   1440   HOWTO (R_PPC64_DTPREL16_HIGHER,
   1441 	 32,			/* rightshift */
   1442 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1443 	 16,			/* bitsize */
   1444 	 FALSE,			/* pc_relative */
   1445 	 0,			/* bitpos */
   1446 	 complain_overflow_dont, /* complain_on_overflow */
   1447 	 ppc64_elf_unhandled_reloc, /* special_function */
   1448 	 "R_PPC64_DTPREL16_HIGHER", /* name */
   1449 	 FALSE,			/* partial_inplace */
   1450 	 0,			/* src_mask */
   1451 	 0xffff,		/* dst_mask */
   1452 	 FALSE),		/* pcrel_offset */
   1453 
   1454   /* Like DTPREL16_HIGHER, but adjust for low 16 bits.  */
   1455   HOWTO (R_PPC64_DTPREL16_HIGHERA,
   1456 	 32,			/* rightshift */
   1457 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1458 	 16,			/* bitsize */
   1459 	 FALSE,			/* pc_relative */
   1460 	 0,			/* bitpos */
   1461 	 complain_overflow_dont, /* complain_on_overflow */
   1462 	 ppc64_elf_unhandled_reloc, /* special_function */
   1463 	 "R_PPC64_DTPREL16_HIGHERA", /* name */
   1464 	 FALSE,			/* partial_inplace */
   1465 	 0,			/* src_mask */
   1466 	 0xffff,		/* dst_mask */
   1467 	 FALSE),		/* pcrel_offset */
   1468 
   1469   /* Like DTPREL16_HIGHER, but next higher group of 16 bits.  */
   1470   HOWTO (R_PPC64_DTPREL16_HIGHEST,
   1471 	 48,			/* rightshift */
   1472 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1473 	 16,			/* bitsize */
   1474 	 FALSE,			/* pc_relative */
   1475 	 0,			/* bitpos */
   1476 	 complain_overflow_dont, /* complain_on_overflow */
   1477 	 ppc64_elf_unhandled_reloc, /* special_function */
   1478 	 "R_PPC64_DTPREL16_HIGHEST", /* name */
   1479 	 FALSE,			/* partial_inplace */
   1480 	 0,			/* src_mask */
   1481 	 0xffff,		/* dst_mask */
   1482 	 FALSE),		/* pcrel_offset */
   1483 
   1484   /* Like DTPREL16_HIGHEST, but adjust for low 16 bits.  */
   1485   HOWTO (R_PPC64_DTPREL16_HIGHESTA,
   1486 	 48,			/* rightshift */
   1487 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1488 	 16,			/* bitsize */
   1489 	 FALSE,			/* pc_relative */
   1490 	 0,			/* bitpos */
   1491 	 complain_overflow_dont, /* complain_on_overflow */
   1492 	 ppc64_elf_unhandled_reloc, /* special_function */
   1493 	 "R_PPC64_DTPREL16_HIGHESTA", /* name */
   1494 	 FALSE,			/* partial_inplace */
   1495 	 0,			/* src_mask */
   1496 	 0xffff,		/* dst_mask */
   1497 	 FALSE),		/* pcrel_offset */
   1498 
   1499   /* Like DTPREL16, but for insns with a DS field.  */
   1500   HOWTO (R_PPC64_DTPREL16_DS,
   1501 	 0,			/* rightshift */
   1502 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1503 	 16,			/* bitsize */
   1504 	 FALSE,			/* pc_relative */
   1505 	 0,			/* bitpos */
   1506 	 complain_overflow_signed, /* complain_on_overflow */
   1507 	 ppc64_elf_unhandled_reloc, /* special_function */
   1508 	 "R_PPC64_DTPREL16_DS",	/* name */
   1509 	 FALSE,			/* partial_inplace */
   1510 	 0,			/* src_mask */
   1511 	 0xfffc,		/* dst_mask */
   1512 	 FALSE),		/* pcrel_offset */
   1513 
   1514   /* Like DTPREL16_DS, but no overflow.  */
   1515   HOWTO (R_PPC64_DTPREL16_LO_DS,
   1516 	 0,			/* rightshift */
   1517 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1518 	 16,			/* bitsize */
   1519 	 FALSE,			/* pc_relative */
   1520 	 0,			/* bitpos */
   1521 	 complain_overflow_dont, /* complain_on_overflow */
   1522 	 ppc64_elf_unhandled_reloc, /* special_function */
   1523 	 "R_PPC64_DTPREL16_LO_DS", /* name */
   1524 	 FALSE,			/* partial_inplace */
   1525 	 0,			/* src_mask */
   1526 	 0xfffc,		/* dst_mask */
   1527 	 FALSE),		/* pcrel_offset */
   1528 
   1529   /* Computes a tp-relative displacement, the difference between the value of
   1530      sym+add and the value of the thread pointer (r13).  */
   1531   HOWTO (R_PPC64_TPREL64,
   1532 	 0,			/* rightshift */
   1533 	 4,			/* size (0 = byte, 1 = short, 2 = long) */
   1534 	 64,			/* bitsize */
   1535 	 FALSE,			/* pc_relative */
   1536 	 0,			/* bitpos */
   1537 	 complain_overflow_dont, /* complain_on_overflow */
   1538 	 ppc64_elf_unhandled_reloc, /* special_function */
   1539 	 "R_PPC64_TPREL64",	/* name */
   1540 	 FALSE,			/* partial_inplace */
   1541 	 0,			/* src_mask */
   1542 	 ONES (64),		/* dst_mask */
   1543 	 FALSE),		/* pcrel_offset */
   1544 
   1545   /* A 16 bit tprel reloc.  */
   1546   HOWTO (R_PPC64_TPREL16,
   1547 	 0,			/* rightshift */
   1548 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1549 	 16,			/* bitsize */
   1550 	 FALSE,			/* pc_relative */
   1551 	 0,			/* bitpos */
   1552 	 complain_overflow_signed, /* complain_on_overflow */
   1553 	 ppc64_elf_unhandled_reloc, /* special_function */
   1554 	 "R_PPC64_TPREL16",	/* name */
   1555 	 FALSE,			/* partial_inplace */
   1556 	 0,			/* src_mask */
   1557 	 0xffff,		/* dst_mask */
   1558 	 FALSE),		/* pcrel_offset */
   1559 
   1560   /* Like TPREL16, but no overflow.  */
   1561   HOWTO (R_PPC64_TPREL16_LO,
   1562 	 0,			/* rightshift */
   1563 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1564 	 16,			/* bitsize */
   1565 	 FALSE,			/* pc_relative */
   1566 	 0,			/* bitpos */
   1567 	 complain_overflow_dont, /* complain_on_overflow */
   1568 	 ppc64_elf_unhandled_reloc, /* special_function */
   1569 	 "R_PPC64_TPREL16_LO",	/* name */
   1570 	 FALSE,			/* partial_inplace */
   1571 	 0,			/* src_mask */
   1572 	 0xffff,		/* dst_mask */
   1573 	 FALSE),		/* pcrel_offset */
   1574 
   1575   /* Like TPREL16_LO, but next higher group of 16 bits.  */
   1576   HOWTO (R_PPC64_TPREL16_HI,
   1577 	 16,			/* rightshift */
   1578 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1579 	 16,			/* bitsize */
   1580 	 FALSE,			/* pc_relative */
   1581 	 0,			/* bitpos */
   1582 	 complain_overflow_signed, /* complain_on_overflow */
   1583 	 ppc64_elf_unhandled_reloc, /* special_function */
   1584 	 "R_PPC64_TPREL16_HI",	/* name */
   1585 	 FALSE,			/* partial_inplace */
   1586 	 0,			/* src_mask */
   1587 	 0xffff,		/* dst_mask */
   1588 	 FALSE),		/* pcrel_offset */
   1589 
   1590   /* Like TPREL16_HI, but adjust for low 16 bits.  */
   1591   HOWTO (R_PPC64_TPREL16_HA,
   1592 	 16,			/* rightshift */
   1593 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1594 	 16,			/* bitsize */
   1595 	 FALSE,			/* pc_relative */
   1596 	 0,			/* bitpos */
   1597 	 complain_overflow_signed, /* complain_on_overflow */
   1598 	 ppc64_elf_unhandled_reloc, /* special_function */
   1599 	 "R_PPC64_TPREL16_HA",	/* name */
   1600 	 FALSE,			/* partial_inplace */
   1601 	 0,			/* src_mask */
   1602 	 0xffff,		/* dst_mask */
   1603 	 FALSE),		/* pcrel_offset */
   1604 
   1605   /* Like TPREL16_HI, but next higher group of 16 bits.  */
   1606   HOWTO (R_PPC64_TPREL16_HIGHER,
   1607 	 32,			/* rightshift */
   1608 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1609 	 16,			/* bitsize */
   1610 	 FALSE,			/* pc_relative */
   1611 	 0,			/* bitpos */
   1612 	 complain_overflow_dont, /* complain_on_overflow */
   1613 	 ppc64_elf_unhandled_reloc, /* special_function */
   1614 	 "R_PPC64_TPREL16_HIGHER",	/* name */
   1615 	 FALSE,			/* partial_inplace */
   1616 	 0,			/* src_mask */
   1617 	 0xffff,		/* dst_mask */
   1618 	 FALSE),		/* pcrel_offset */
   1619 
   1620   /* Like TPREL16_HIGHER, but adjust for low 16 bits.  */
   1621   HOWTO (R_PPC64_TPREL16_HIGHERA,
   1622 	 32,			/* rightshift */
   1623 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1624 	 16,			/* bitsize */
   1625 	 FALSE,			/* pc_relative */
   1626 	 0,			/* bitpos */
   1627 	 complain_overflow_dont, /* complain_on_overflow */
   1628 	 ppc64_elf_unhandled_reloc, /* special_function */
   1629 	 "R_PPC64_TPREL16_HIGHERA", /* name */
   1630 	 FALSE,			/* partial_inplace */
   1631 	 0,			/* src_mask */
   1632 	 0xffff,		/* dst_mask */
   1633 	 FALSE),		/* pcrel_offset */
   1634 
   1635   /* Like TPREL16_HIGHER, but next higher group of 16 bits.  */
   1636   HOWTO (R_PPC64_TPREL16_HIGHEST,
   1637 	 48,			/* rightshift */
   1638 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1639 	 16,			/* bitsize */
   1640 	 FALSE,			/* pc_relative */
   1641 	 0,			/* bitpos */
   1642 	 complain_overflow_dont, /* complain_on_overflow */
   1643 	 ppc64_elf_unhandled_reloc, /* special_function */
   1644 	 "R_PPC64_TPREL16_HIGHEST", /* name */
   1645 	 FALSE,			/* partial_inplace */
   1646 	 0,			/* src_mask */
   1647 	 0xffff,		/* dst_mask */
   1648 	 FALSE),		/* pcrel_offset */
   1649 
   1650   /* Like TPREL16_HIGHEST, but adjust for low 16 bits.  */
   1651   HOWTO (R_PPC64_TPREL16_HIGHESTA,
   1652 	 48,			/* rightshift */
   1653 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1654 	 16,			/* bitsize */
   1655 	 FALSE,			/* pc_relative */
   1656 	 0,			/* bitpos */
   1657 	 complain_overflow_dont, /* complain_on_overflow */
   1658 	 ppc64_elf_unhandled_reloc, /* special_function */
   1659 	 "R_PPC64_TPREL16_HIGHESTA", /* name */
   1660 	 FALSE,			/* partial_inplace */
   1661 	 0,			/* src_mask */
   1662 	 0xffff,		/* dst_mask */
   1663 	 FALSE),		/* pcrel_offset */
   1664 
   1665   /* Like TPREL16, but for insns with a DS field.  */
   1666   HOWTO (R_PPC64_TPREL16_DS,
   1667 	 0,			/* rightshift */
   1668 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1669 	 16,			/* bitsize */
   1670 	 FALSE,			/* pc_relative */
   1671 	 0,			/* bitpos */
   1672 	 complain_overflow_signed, /* complain_on_overflow */
   1673 	 ppc64_elf_unhandled_reloc, /* special_function */
   1674 	 "R_PPC64_TPREL16_DS",	/* name */
   1675 	 FALSE,			/* partial_inplace */
   1676 	 0,			/* src_mask */
   1677 	 0xfffc,		/* dst_mask */
   1678 	 FALSE),		/* pcrel_offset */
   1679 
   1680   /* Like TPREL16_DS, but no overflow.  */
   1681   HOWTO (R_PPC64_TPREL16_LO_DS,
   1682 	 0,			/* rightshift */
   1683 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1684 	 16,			/* bitsize */
   1685 	 FALSE,			/* pc_relative */
   1686 	 0,			/* bitpos */
   1687 	 complain_overflow_dont, /* complain_on_overflow */
   1688 	 ppc64_elf_unhandled_reloc, /* special_function */
   1689 	 "R_PPC64_TPREL16_LO_DS", /* name */
   1690 	 FALSE,			/* partial_inplace */
   1691 	 0,			/* src_mask */
   1692 	 0xfffc,		/* dst_mask */
   1693 	 FALSE),		/* pcrel_offset */
   1694 
   1695   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1696      with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
   1697      to the first entry relative to the TOC base (r2).  */
   1698   HOWTO (R_PPC64_GOT_TLSGD16,
   1699 	 0,			/* rightshift */
   1700 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1701 	 16,			/* bitsize */
   1702 	 FALSE,			/* pc_relative */
   1703 	 0,			/* bitpos */
   1704 	 complain_overflow_signed, /* complain_on_overflow */
   1705 	 ppc64_elf_unhandled_reloc, /* special_function */
   1706 	 "R_PPC64_GOT_TLSGD16",	/* name */
   1707 	 FALSE,			/* partial_inplace */
   1708 	 0,			/* src_mask */
   1709 	 0xffff,		/* dst_mask */
   1710 	 FALSE),		/* pcrel_offset */
   1711 
   1712   /* Like GOT_TLSGD16, but no overflow.  */
   1713   HOWTO (R_PPC64_GOT_TLSGD16_LO,
   1714 	 0,			/* rightshift */
   1715 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1716 	 16,			/* bitsize */
   1717 	 FALSE,			/* pc_relative */
   1718 	 0,			/* bitpos */
   1719 	 complain_overflow_dont, /* complain_on_overflow */
   1720 	 ppc64_elf_unhandled_reloc, /* special_function */
   1721 	 "R_PPC64_GOT_TLSGD16_LO", /* name */
   1722 	 FALSE,			/* partial_inplace */
   1723 	 0,			/* src_mask */
   1724 	 0xffff,		/* dst_mask */
   1725 	 FALSE),		/* pcrel_offset */
   1726 
   1727   /* Like GOT_TLSGD16_LO, but next higher group of 16 bits.  */
   1728   HOWTO (R_PPC64_GOT_TLSGD16_HI,
   1729 	 16,			/* rightshift */
   1730 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1731 	 16,			/* bitsize */
   1732 	 FALSE,			/* pc_relative */
   1733 	 0,			/* bitpos */
   1734 	 complain_overflow_signed, /* complain_on_overflow */
   1735 	 ppc64_elf_unhandled_reloc, /* special_function */
   1736 	 "R_PPC64_GOT_TLSGD16_HI", /* name */
   1737 	 FALSE,			/* partial_inplace */
   1738 	 0,			/* src_mask */
   1739 	 0xffff,		/* dst_mask */
   1740 	 FALSE),		/* pcrel_offset */
   1741 
   1742   /* Like GOT_TLSGD16_HI, but adjust for low 16 bits.  */
   1743   HOWTO (R_PPC64_GOT_TLSGD16_HA,
   1744 	 16,			/* rightshift */
   1745 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1746 	 16,			/* bitsize */
   1747 	 FALSE,			/* pc_relative */
   1748 	 0,			/* bitpos */
   1749 	 complain_overflow_signed, /* complain_on_overflow */
   1750 	 ppc64_elf_unhandled_reloc, /* special_function */
   1751 	 "R_PPC64_GOT_TLSGD16_HA", /* name */
   1752 	 FALSE,			/* partial_inplace */
   1753 	 0,			/* src_mask */
   1754 	 0xffff,		/* dst_mask */
   1755 	 FALSE),		/* pcrel_offset */
   1756 
   1757   /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
   1758      with values (sym+add)@dtpmod and zero, and computes the offset to the
   1759      first entry relative to the TOC base (r2).  */
   1760   HOWTO (R_PPC64_GOT_TLSLD16,
   1761 	 0,			/* rightshift */
   1762 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1763 	 16,			/* bitsize */
   1764 	 FALSE,			/* pc_relative */
   1765 	 0,			/* bitpos */
   1766 	 complain_overflow_signed, /* complain_on_overflow */
   1767 	 ppc64_elf_unhandled_reloc, /* special_function */
   1768 	 "R_PPC64_GOT_TLSLD16",	/* name */
   1769 	 FALSE,			/* partial_inplace */
   1770 	 0,			/* src_mask */
   1771 	 0xffff,		/* dst_mask */
   1772 	 FALSE),		/* pcrel_offset */
   1773 
   1774   /* Like GOT_TLSLD16, but no overflow.  */
   1775   HOWTO (R_PPC64_GOT_TLSLD16_LO,
   1776 	 0,			/* rightshift */
   1777 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1778 	 16,			/* bitsize */
   1779 	 FALSE,			/* pc_relative */
   1780 	 0,			/* bitpos */
   1781 	 complain_overflow_dont, /* complain_on_overflow */
   1782 	 ppc64_elf_unhandled_reloc, /* special_function */
   1783 	 "R_PPC64_GOT_TLSLD16_LO", /* name */
   1784 	 FALSE,			/* partial_inplace */
   1785 	 0,			/* src_mask */
   1786 	 0xffff,		/* dst_mask */
   1787 	 FALSE),		/* pcrel_offset */
   1788 
   1789   /* Like GOT_TLSLD16_LO, but next higher group of 16 bits.  */
   1790   HOWTO (R_PPC64_GOT_TLSLD16_HI,
   1791 	 16,			/* rightshift */
   1792 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1793 	 16,			/* bitsize */
   1794 	 FALSE,			/* pc_relative */
   1795 	 0,			/* bitpos */
   1796 	 complain_overflow_signed, /* complain_on_overflow */
   1797 	 ppc64_elf_unhandled_reloc, /* special_function */
   1798 	 "R_PPC64_GOT_TLSLD16_HI", /* name */
   1799 	 FALSE,			/* partial_inplace */
   1800 	 0,			/* src_mask */
   1801 	 0xffff,		/* dst_mask */
   1802 	 FALSE),		/* pcrel_offset */
   1803 
   1804   /* Like GOT_TLSLD16_HI, but adjust for low 16 bits.  */
   1805   HOWTO (R_PPC64_GOT_TLSLD16_HA,
   1806 	 16,			/* rightshift */
   1807 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1808 	 16,			/* bitsize */
   1809 	 FALSE,			/* pc_relative */
   1810 	 0,			/* bitpos */
   1811 	 complain_overflow_signed, /* complain_on_overflow */
   1812 	 ppc64_elf_unhandled_reloc, /* special_function */
   1813 	 "R_PPC64_GOT_TLSLD16_HA", /* name */
   1814 	 FALSE,			/* partial_inplace */
   1815 	 0,			/* src_mask */
   1816 	 0xffff,		/* dst_mask */
   1817 	 FALSE),		/* pcrel_offset */
   1818 
   1819   /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
   1820      the offset to the entry relative to the TOC base (r2).  */
   1821   HOWTO (R_PPC64_GOT_DTPREL16_DS,
   1822 	 0,			/* rightshift */
   1823 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1824 	 16,			/* bitsize */
   1825 	 FALSE,			/* pc_relative */
   1826 	 0,			/* bitpos */
   1827 	 complain_overflow_signed, /* complain_on_overflow */
   1828 	 ppc64_elf_unhandled_reloc, /* special_function */
   1829 	 "R_PPC64_GOT_DTPREL16_DS", /* name */
   1830 	 FALSE,			/* partial_inplace */
   1831 	 0,			/* src_mask */
   1832 	 0xfffc,		/* dst_mask */
   1833 	 FALSE),		/* pcrel_offset */
   1834 
   1835   /* Like GOT_DTPREL16_DS, but no overflow.  */
   1836   HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
   1837 	 0,			/* rightshift */
   1838 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1839 	 16,			/* bitsize */
   1840 	 FALSE,			/* pc_relative */
   1841 	 0,			/* bitpos */
   1842 	 complain_overflow_dont, /* complain_on_overflow */
   1843 	 ppc64_elf_unhandled_reloc, /* special_function */
   1844 	 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
   1845 	 FALSE,			/* partial_inplace */
   1846 	 0,			/* src_mask */
   1847 	 0xfffc,		/* dst_mask */
   1848 	 FALSE),		/* pcrel_offset */
   1849 
   1850   /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits.  */
   1851   HOWTO (R_PPC64_GOT_DTPREL16_HI,
   1852 	 16,			/* rightshift */
   1853 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1854 	 16,			/* bitsize */
   1855 	 FALSE,			/* pc_relative */
   1856 	 0,			/* bitpos */
   1857 	 complain_overflow_signed, /* complain_on_overflow */
   1858 	 ppc64_elf_unhandled_reloc, /* special_function */
   1859 	 "R_PPC64_GOT_DTPREL16_HI", /* name */
   1860 	 FALSE,			/* partial_inplace */
   1861 	 0,			/* src_mask */
   1862 	 0xffff,		/* dst_mask */
   1863 	 FALSE),		/* pcrel_offset */
   1864 
   1865   /* Like GOT_DTPREL16_HI, but adjust for low 16 bits.  */
   1866   HOWTO (R_PPC64_GOT_DTPREL16_HA,
   1867 	 16,			/* rightshift */
   1868 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1869 	 16,			/* bitsize */
   1870 	 FALSE,			/* pc_relative */
   1871 	 0,			/* bitpos */
   1872 	 complain_overflow_signed, /* complain_on_overflow */
   1873 	 ppc64_elf_unhandled_reloc, /* special_function */
   1874 	 "R_PPC64_GOT_DTPREL16_HA", /* name */
   1875 	 FALSE,			/* partial_inplace */
   1876 	 0,			/* src_mask */
   1877 	 0xffff,		/* dst_mask */
   1878 	 FALSE),		/* pcrel_offset */
   1879 
   1880   /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
   1881      offset to the entry relative to the TOC base (r2).  */
   1882   HOWTO (R_PPC64_GOT_TPREL16_DS,
   1883 	 0,			/* rightshift */
   1884 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1885 	 16,			/* bitsize */
   1886 	 FALSE,			/* pc_relative */
   1887 	 0,			/* bitpos */
   1888 	 complain_overflow_signed, /* complain_on_overflow */
   1889 	 ppc64_elf_unhandled_reloc, /* special_function */
   1890 	 "R_PPC64_GOT_TPREL16_DS", /* name */
   1891 	 FALSE,			/* partial_inplace */
   1892 	 0,			/* src_mask */
   1893 	 0xfffc,		/* dst_mask */
   1894 	 FALSE),		/* pcrel_offset */
   1895 
   1896   /* Like GOT_TPREL16_DS, but no overflow.  */
   1897   HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
   1898 	 0,			/* rightshift */
   1899 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1900 	 16,			/* bitsize */
   1901 	 FALSE,			/* pc_relative */
   1902 	 0,			/* bitpos */
   1903 	 complain_overflow_dont, /* complain_on_overflow */
   1904 	 ppc64_elf_unhandled_reloc, /* special_function */
   1905 	 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
   1906 	 FALSE,			/* partial_inplace */
   1907 	 0,			/* src_mask */
   1908 	 0xfffc,		/* dst_mask */
   1909 	 FALSE),		/* pcrel_offset */
   1910 
   1911   /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits.  */
   1912   HOWTO (R_PPC64_GOT_TPREL16_HI,
   1913 	 16,			/* rightshift */
   1914 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1915 	 16,			/* bitsize */
   1916 	 FALSE,			/* pc_relative */
   1917 	 0,			/* bitpos */
   1918 	 complain_overflow_signed, /* complain_on_overflow */
   1919 	 ppc64_elf_unhandled_reloc, /* special_function */
   1920 	 "R_PPC64_GOT_TPREL16_HI", /* name */
   1921 	 FALSE,			/* partial_inplace */
   1922 	 0,			/* src_mask */
   1923 	 0xffff,		/* dst_mask */
   1924 	 FALSE),		/* pcrel_offset */
   1925 
   1926   /* Like GOT_TPREL16_HI, but adjust for low 16 bits.  */
   1927   HOWTO (R_PPC64_GOT_TPREL16_HA,
   1928 	 16,			/* rightshift */
   1929 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1930 	 16,			/* bitsize */
   1931 	 FALSE,			/* pc_relative */
   1932 	 0,			/* bitpos */
   1933 	 complain_overflow_signed, /* complain_on_overflow */
   1934 	 ppc64_elf_unhandled_reloc, /* special_function */
   1935 	 "R_PPC64_GOT_TPREL16_HA", /* name */
   1936 	 FALSE,			/* partial_inplace */
   1937 	 0,			/* src_mask */
   1938 	 0xffff,		/* dst_mask */
   1939 	 FALSE),		/* pcrel_offset */
   1940 
   1941   HOWTO (R_PPC64_JMP_IREL,	/* type */
   1942 	 0,			/* rightshift */
   1943 	 0,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1944 	 0,			/* bitsize */
   1945 	 FALSE,			/* pc_relative */
   1946 	 0,			/* bitpos */
   1947 	 complain_overflow_dont, /* complain_on_overflow */
   1948 	 ppc64_elf_unhandled_reloc, /* special_function */
   1949 	 "R_PPC64_JMP_IREL",	/* name */
   1950 	 FALSE,			/* partial_inplace */
   1951 	 0,			/* src_mask */
   1952 	 0,			/* dst_mask */
   1953 	 FALSE),		/* pcrel_offset */
   1954 
   1955   HOWTO (R_PPC64_IRELATIVE,	/* type */
   1956 	 0,			/* rightshift */
   1957 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   1958 	 64,			/* bitsize */
   1959 	 FALSE,			/* pc_relative */
   1960 	 0,			/* bitpos */
   1961 	 complain_overflow_dont, /* complain_on_overflow */
   1962 	 bfd_elf_generic_reloc,	/* special_function */
   1963 	 "R_PPC64_IRELATIVE",	/* name */
   1964 	 FALSE,			/* partial_inplace */
   1965 	 0,			/* src_mask */
   1966 	 ONES (64),		/* dst_mask */
   1967 	 FALSE),		/* pcrel_offset */
   1968 
   1969   /* A 16 bit relative relocation.  */
   1970   HOWTO (R_PPC64_REL16,		/* type */
   1971 	 0,			/* rightshift */
   1972 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1973 	 16,			/* bitsize */
   1974 	 TRUE,			/* pc_relative */
   1975 	 0,			/* bitpos */
   1976 	 complain_overflow_signed, /* complain_on_overflow */
   1977 	 bfd_elf_generic_reloc,	/* special_function */
   1978 	 "R_PPC64_REL16",	/* name */
   1979 	 FALSE,			/* partial_inplace */
   1980 	 0,			/* src_mask */
   1981 	 0xffff,		/* dst_mask */
   1982 	 TRUE),			/* pcrel_offset */
   1983 
   1984   /* A 16 bit relative relocation without overflow.  */
   1985   HOWTO (R_PPC64_REL16_LO,	/* type */
   1986 	 0,			/* rightshift */
   1987 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1988 	 16,			/* bitsize */
   1989 	 TRUE,			/* pc_relative */
   1990 	 0,			/* bitpos */
   1991 	 complain_overflow_dont,/* complain_on_overflow */
   1992 	 bfd_elf_generic_reloc,	/* special_function */
   1993 	 "R_PPC64_REL16_LO",	/* name */
   1994 	 FALSE,			/* partial_inplace */
   1995 	 0,			/* src_mask */
   1996 	 0xffff,		/* dst_mask */
   1997 	 TRUE),			/* pcrel_offset */
   1998 
   1999   /* The high order 16 bits of a relative address.  */
   2000   HOWTO (R_PPC64_REL16_HI,	/* type */
   2001 	 16,			/* rightshift */
   2002 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2003 	 16,			/* bitsize */
   2004 	 TRUE,			/* pc_relative */
   2005 	 0,			/* bitpos */
   2006 	 complain_overflow_signed, /* complain_on_overflow */
   2007 	 bfd_elf_generic_reloc,	/* special_function */
   2008 	 "R_PPC64_REL16_HI",	/* name */
   2009 	 FALSE,			/* partial_inplace */
   2010 	 0,			/* src_mask */
   2011 	 0xffff,		/* dst_mask */
   2012 	 TRUE),			/* pcrel_offset */
   2013 
   2014   /* The high order 16 bits of a relative address, plus 1 if the contents of
   2015      the low 16 bits, treated as a signed number, is negative.  */
   2016   HOWTO (R_PPC64_REL16_HA,	/* type */
   2017 	 16,			/* rightshift */
   2018 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2019 	 16,			/* bitsize */
   2020 	 TRUE,			/* pc_relative */
   2021 	 0,			/* bitpos */
   2022 	 complain_overflow_signed, /* complain_on_overflow */
   2023 	 ppc64_elf_ha_reloc,	/* special_function */
   2024 	 "R_PPC64_REL16_HA",	/* name */
   2025 	 FALSE,			/* partial_inplace */
   2026 	 0,			/* src_mask */
   2027 	 0xffff,		/* dst_mask */
   2028 	 TRUE),			/* pcrel_offset */
   2029 
   2030   /* Like R_PPC64_REL16_HA but for split field in addpcis.  */
   2031   HOWTO (R_PPC64_REL16DX_HA,	/* type */
   2032 	 16,			/* rightshift */
   2033 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2034 	 16,			/* bitsize */
   2035 	 TRUE,			/* pc_relative */
   2036 	 0,			/* bitpos */
   2037 	 complain_overflow_signed, /* complain_on_overflow */
   2038 	 ppc64_elf_ha_reloc,	/* special_function */
   2039 	 "R_PPC64_REL16DX_HA",	/* name */
   2040 	 FALSE,			/* partial_inplace */
   2041 	 0,			/* src_mask */
   2042 	 0x1fffc1,		/* dst_mask */
   2043 	 TRUE),			/* pcrel_offset */
   2044 
   2045   /* Like R_PPC64_ADDR16_HI, but no overflow.  */
   2046   HOWTO (R_PPC64_ADDR16_HIGH,	/* type */
   2047 	 16,			/* rightshift */
   2048 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2049 	 16,			/* bitsize */
   2050 	 FALSE,			/* pc_relative */
   2051 	 0,			/* bitpos */
   2052 	 complain_overflow_dont, /* complain_on_overflow */
   2053 	 bfd_elf_generic_reloc,	/* special_function */
   2054 	 "R_PPC64_ADDR16_HIGH",	/* name */
   2055 	 FALSE,			/* partial_inplace */
   2056 	 0,			/* src_mask */
   2057 	 0xffff,		/* dst_mask */
   2058 	 FALSE),		/* pcrel_offset */
   2059 
   2060   /* Like R_PPC64_ADDR16_HA, but no overflow.  */
   2061   HOWTO (R_PPC64_ADDR16_HIGHA,	/* type */
   2062 	 16,			/* rightshift */
   2063 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2064 	 16,			/* bitsize */
   2065 	 FALSE,			/* pc_relative */
   2066 	 0,			/* bitpos */
   2067 	 complain_overflow_dont, /* complain_on_overflow */
   2068 	 ppc64_elf_ha_reloc,	/* special_function */
   2069 	 "R_PPC64_ADDR16_HIGHA",	/* name */
   2070 	 FALSE,			/* partial_inplace */
   2071 	 0,			/* src_mask */
   2072 	 0xffff,		/* dst_mask */
   2073 	 FALSE),		/* pcrel_offset */
   2074 
   2075   /* Like R_PPC64_DTPREL16_HI, but no overflow.  */
   2076   HOWTO (R_PPC64_DTPREL16_HIGH,
   2077 	 16,			/* rightshift */
   2078 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2079 	 16,			/* bitsize */
   2080 	 FALSE,			/* pc_relative */
   2081 	 0,			/* bitpos */
   2082 	 complain_overflow_dont, /* complain_on_overflow */
   2083 	 ppc64_elf_unhandled_reloc, /* special_function */
   2084 	 "R_PPC64_DTPREL16_HIGH", /* name */
   2085 	 FALSE,			/* partial_inplace */
   2086 	 0,			/* src_mask */
   2087 	 0xffff,		/* dst_mask */
   2088 	 FALSE),		/* pcrel_offset */
   2089 
   2090   /* Like R_PPC64_DTPREL16_HA, but no overflow.  */
   2091   HOWTO (R_PPC64_DTPREL16_HIGHA,
   2092 	 16,			/* rightshift */
   2093 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2094 	 16,			/* bitsize */
   2095 	 FALSE,			/* pc_relative */
   2096 	 0,			/* bitpos */
   2097 	 complain_overflow_dont, /* complain_on_overflow */
   2098 	 ppc64_elf_unhandled_reloc, /* special_function */
   2099 	 "R_PPC64_DTPREL16_HIGHA", /* name */
   2100 	 FALSE,			/* partial_inplace */
   2101 	 0,			/* src_mask */
   2102 	 0xffff,		/* dst_mask */
   2103 	 FALSE),		/* pcrel_offset */
   2104 
   2105   /* Like R_PPC64_TPREL16_HI, but no overflow.  */
   2106   HOWTO (R_PPC64_TPREL16_HIGH,
   2107 	 16,			/* rightshift */
   2108 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2109 	 16,			/* bitsize */
   2110 	 FALSE,			/* pc_relative */
   2111 	 0,			/* bitpos */
   2112 	 complain_overflow_dont, /* complain_on_overflow */
   2113 	 ppc64_elf_unhandled_reloc, /* special_function */
   2114 	 "R_PPC64_TPREL16_HIGH",	/* name */
   2115 	 FALSE,			/* partial_inplace */
   2116 	 0,			/* src_mask */
   2117 	 0xffff,		/* dst_mask */
   2118 	 FALSE),		/* pcrel_offset */
   2119 
   2120   /* Like R_PPC64_TPREL16_HA, but no overflow.  */
   2121   HOWTO (R_PPC64_TPREL16_HIGHA,
   2122 	 16,			/* rightshift */
   2123 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2124 	 16,			/* bitsize */
   2125 	 FALSE,			/* pc_relative */
   2126 	 0,			/* bitpos */
   2127 	 complain_overflow_dont, /* complain_on_overflow */
   2128 	 ppc64_elf_unhandled_reloc, /* special_function */
   2129 	 "R_PPC64_TPREL16_HIGHA",	/* name */
   2130 	 FALSE,			/* partial_inplace */
   2131 	 0,			/* src_mask */
   2132 	 0xffff,		/* dst_mask */
   2133 	 FALSE),		/* pcrel_offset */
   2134 
   2135   /* Marker reloc on ELFv2 large-model function entry.  */
   2136   HOWTO (R_PPC64_ENTRY,
   2137 	 0,			/* rightshift */
   2138 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2139 	 32,			/* bitsize */
   2140 	 FALSE,			/* pc_relative */
   2141 	 0,			/* bitpos */
   2142 	 complain_overflow_dont, /* complain_on_overflow */
   2143 	 bfd_elf_generic_reloc,	/* special_function */
   2144 	 "R_PPC64_ENTRY",	/* name */
   2145 	 FALSE,			/* partial_inplace */
   2146 	 0,			/* src_mask */
   2147 	 0,			/* dst_mask */
   2148 	 FALSE),		/* pcrel_offset */
   2149 
   2150   /* Like ADDR64, but use local entry point of function.  */
   2151   HOWTO (R_PPC64_ADDR64_LOCAL,	/* type */
   2152 	 0,			/* rightshift */
   2153 	 4,			/* size (0=byte, 1=short, 2=long, 4=64 bits) */
   2154 	 64,			/* bitsize */
   2155 	 FALSE,			/* pc_relative */
   2156 	 0,			/* bitpos */
   2157 	 complain_overflow_dont, /* complain_on_overflow */
   2158 	 bfd_elf_generic_reloc,	/* special_function */
   2159 	 "R_PPC64_ADDR64_LOCAL", /* name */
   2160 	 FALSE,			/* partial_inplace */
   2161 	 0,			/* src_mask */
   2162 	 ONES (64),		/* dst_mask */
   2163 	 FALSE),		/* pcrel_offset */
   2164 
   2165   /* GNU extension to record C++ vtable hierarchy.  */
   2166   HOWTO (R_PPC64_GNU_VTINHERIT,	/* type */
   2167 	 0,			/* rightshift */
   2168 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   2169 	 0,			/* bitsize */
   2170 	 FALSE,			/* pc_relative */
   2171 	 0,			/* bitpos */
   2172 	 complain_overflow_dont, /* complain_on_overflow */
   2173 	 NULL,			/* special_function */
   2174 	 "R_PPC64_GNU_VTINHERIT", /* name */
   2175 	 FALSE,			/* partial_inplace */
   2176 	 0,			/* src_mask */
   2177 	 0,			/* dst_mask */
   2178 	 FALSE),		/* pcrel_offset */
   2179 
   2180   /* GNU extension to record C++ vtable member usage.  */
   2181   HOWTO (R_PPC64_GNU_VTENTRY,	/* type */
   2182 	 0,			/* rightshift */
   2183 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   2184 	 0,			/* bitsize */
   2185 	 FALSE,			/* pc_relative */
   2186 	 0,			/* bitpos */
   2187 	 complain_overflow_dont, /* complain_on_overflow */
   2188 	 NULL,			/* special_function */
   2189 	 "R_PPC64_GNU_VTENTRY",	/* name */
   2190 	 FALSE,			/* partial_inplace */
   2191 	 0,			/* src_mask */
   2192 	 0,			/* dst_mask */
   2193 	 FALSE),		/* pcrel_offset */
   2194 };
   2195 
   2196 
   2197 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
   2199    be done.  */
   2200 
   2201 static void
   2202 ppc_howto_init (void)
   2203 {
   2204   unsigned int i, type;
   2205 
   2206   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
   2207     {
   2208       type = ppc64_elf_howto_raw[i].type;
   2209       BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
   2210       ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
   2211     }
   2212 }
   2213 
   2214 static reloc_howto_type *
   2215 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2216 			     bfd_reloc_code_real_type code)
   2217 {
   2218   enum elf_ppc64_reloc_type r = R_PPC64_NONE;
   2219 
   2220   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2221     /* Initialize howto table if needed.  */
   2222     ppc_howto_init ();
   2223 
   2224   switch (code)
   2225     {
   2226     default:
   2227       return NULL;
   2228 
   2229     case BFD_RELOC_NONE:			r = R_PPC64_NONE;
   2230       break;
   2231     case BFD_RELOC_32:				r = R_PPC64_ADDR32;
   2232       break;
   2233     case BFD_RELOC_PPC_BA26:			r = R_PPC64_ADDR24;
   2234       break;
   2235     case BFD_RELOC_16:				r = R_PPC64_ADDR16;
   2236       break;
   2237     case BFD_RELOC_LO16:			r = R_PPC64_ADDR16_LO;
   2238       break;
   2239     case BFD_RELOC_HI16:			r = R_PPC64_ADDR16_HI;
   2240       break;
   2241     case BFD_RELOC_PPC64_ADDR16_HIGH:		r = R_PPC64_ADDR16_HIGH;
   2242       break;
   2243     case BFD_RELOC_HI16_S:			r = R_PPC64_ADDR16_HA;
   2244       break;
   2245     case BFD_RELOC_PPC64_ADDR16_HIGHA:		r = R_PPC64_ADDR16_HIGHA;
   2246       break;
   2247     case BFD_RELOC_PPC_BA16:			r = R_PPC64_ADDR14;
   2248       break;
   2249     case BFD_RELOC_PPC_BA16_BRTAKEN:		r = R_PPC64_ADDR14_BRTAKEN;
   2250       break;
   2251     case BFD_RELOC_PPC_BA16_BRNTAKEN:		r = R_PPC64_ADDR14_BRNTAKEN;
   2252       break;
   2253     case BFD_RELOC_PPC_B26:			r = R_PPC64_REL24;
   2254       break;
   2255     case BFD_RELOC_PPC_B16:			r = R_PPC64_REL14;
   2256       break;
   2257     case BFD_RELOC_PPC_B16_BRTAKEN:		r = R_PPC64_REL14_BRTAKEN;
   2258       break;
   2259     case BFD_RELOC_PPC_B16_BRNTAKEN:		r = R_PPC64_REL14_BRNTAKEN;
   2260       break;
   2261     case BFD_RELOC_16_GOTOFF:			r = R_PPC64_GOT16;
   2262       break;
   2263     case BFD_RELOC_LO16_GOTOFF:			r = R_PPC64_GOT16_LO;
   2264       break;
   2265     case BFD_RELOC_HI16_GOTOFF:			r = R_PPC64_GOT16_HI;
   2266       break;
   2267     case BFD_RELOC_HI16_S_GOTOFF:		r = R_PPC64_GOT16_HA;
   2268       break;
   2269     case BFD_RELOC_PPC_COPY:			r = R_PPC64_COPY;
   2270       break;
   2271     case BFD_RELOC_PPC_GLOB_DAT:		r = R_PPC64_GLOB_DAT;
   2272       break;
   2273     case BFD_RELOC_32_PCREL:			r = R_PPC64_REL32;
   2274       break;
   2275     case BFD_RELOC_32_PLTOFF:			r = R_PPC64_PLT32;
   2276       break;
   2277     case BFD_RELOC_32_PLT_PCREL:		r = R_PPC64_PLTREL32;
   2278       break;
   2279     case BFD_RELOC_LO16_PLTOFF:			r = R_PPC64_PLT16_LO;
   2280       break;
   2281     case BFD_RELOC_HI16_PLTOFF:			r = R_PPC64_PLT16_HI;
   2282       break;
   2283     case BFD_RELOC_HI16_S_PLTOFF:		r = R_PPC64_PLT16_HA;
   2284       break;
   2285     case BFD_RELOC_16_BASEREL:			r = R_PPC64_SECTOFF;
   2286       break;
   2287     case BFD_RELOC_LO16_BASEREL:		r = R_PPC64_SECTOFF_LO;
   2288       break;
   2289     case BFD_RELOC_HI16_BASEREL:		r = R_PPC64_SECTOFF_HI;
   2290       break;
   2291     case BFD_RELOC_HI16_S_BASEREL:		r = R_PPC64_SECTOFF_HA;
   2292       break;
   2293     case BFD_RELOC_CTOR:			r = R_PPC64_ADDR64;
   2294       break;
   2295     case BFD_RELOC_64:				r = R_PPC64_ADDR64;
   2296       break;
   2297     case BFD_RELOC_PPC64_HIGHER:		r = R_PPC64_ADDR16_HIGHER;
   2298       break;
   2299     case BFD_RELOC_PPC64_HIGHER_S:		r = R_PPC64_ADDR16_HIGHERA;
   2300       break;
   2301     case BFD_RELOC_PPC64_HIGHEST:		r = R_PPC64_ADDR16_HIGHEST;
   2302       break;
   2303     case BFD_RELOC_PPC64_HIGHEST_S:		r = R_PPC64_ADDR16_HIGHESTA;
   2304       break;
   2305     case BFD_RELOC_64_PCREL:			r = R_PPC64_REL64;
   2306       break;
   2307     case BFD_RELOC_64_PLTOFF:			r = R_PPC64_PLT64;
   2308       break;
   2309     case BFD_RELOC_64_PLT_PCREL:		r = R_PPC64_PLTREL64;
   2310       break;
   2311     case BFD_RELOC_PPC_TOC16:			r = R_PPC64_TOC16;
   2312       break;
   2313     case BFD_RELOC_PPC64_TOC16_LO:		r = R_PPC64_TOC16_LO;
   2314       break;
   2315     case BFD_RELOC_PPC64_TOC16_HI:		r = R_PPC64_TOC16_HI;
   2316       break;
   2317     case BFD_RELOC_PPC64_TOC16_HA:		r = R_PPC64_TOC16_HA;
   2318       break;
   2319     case BFD_RELOC_PPC64_TOC:			r = R_PPC64_TOC;
   2320       break;
   2321     case BFD_RELOC_PPC64_PLTGOT16:		r = R_PPC64_PLTGOT16;
   2322       break;
   2323     case BFD_RELOC_PPC64_PLTGOT16_LO:		r = R_PPC64_PLTGOT16_LO;
   2324       break;
   2325     case BFD_RELOC_PPC64_PLTGOT16_HI:		r = R_PPC64_PLTGOT16_HI;
   2326       break;
   2327     case BFD_RELOC_PPC64_PLTGOT16_HA:		r = R_PPC64_PLTGOT16_HA;
   2328       break;
   2329     case BFD_RELOC_PPC64_ADDR16_DS:		r = R_PPC64_ADDR16_DS;
   2330       break;
   2331     case BFD_RELOC_PPC64_ADDR16_LO_DS:		r = R_PPC64_ADDR16_LO_DS;
   2332       break;
   2333     case BFD_RELOC_PPC64_GOT16_DS:		r = R_PPC64_GOT16_DS;
   2334       break;
   2335     case BFD_RELOC_PPC64_GOT16_LO_DS:		r = R_PPC64_GOT16_LO_DS;
   2336       break;
   2337     case BFD_RELOC_PPC64_PLT16_LO_DS:		r = R_PPC64_PLT16_LO_DS;
   2338       break;
   2339     case BFD_RELOC_PPC64_SECTOFF_DS:		r = R_PPC64_SECTOFF_DS;
   2340       break;
   2341     case BFD_RELOC_PPC64_SECTOFF_LO_DS:		r = R_PPC64_SECTOFF_LO_DS;
   2342       break;
   2343     case BFD_RELOC_PPC64_TOC16_DS:		r = R_PPC64_TOC16_DS;
   2344       break;
   2345     case BFD_RELOC_PPC64_TOC16_LO_DS:		r = R_PPC64_TOC16_LO_DS;
   2346       break;
   2347     case BFD_RELOC_PPC64_PLTGOT16_DS:		r = R_PPC64_PLTGOT16_DS;
   2348       break;
   2349     case BFD_RELOC_PPC64_PLTGOT16_LO_DS:	r = R_PPC64_PLTGOT16_LO_DS;
   2350       break;
   2351     case BFD_RELOC_PPC_TLS:			r = R_PPC64_TLS;
   2352       break;
   2353     case BFD_RELOC_PPC_TLSGD:			r = R_PPC64_TLSGD;
   2354       break;
   2355     case BFD_RELOC_PPC_TLSLD:			r = R_PPC64_TLSLD;
   2356       break;
   2357     case BFD_RELOC_PPC_DTPMOD:			r = R_PPC64_DTPMOD64;
   2358       break;
   2359     case BFD_RELOC_PPC_TPREL16:			r = R_PPC64_TPREL16;
   2360       break;
   2361     case BFD_RELOC_PPC_TPREL16_LO:		r = R_PPC64_TPREL16_LO;
   2362       break;
   2363     case BFD_RELOC_PPC_TPREL16_HI:		r = R_PPC64_TPREL16_HI;
   2364       break;
   2365     case BFD_RELOC_PPC64_TPREL16_HIGH:		r = R_PPC64_TPREL16_HIGH;
   2366       break;
   2367     case BFD_RELOC_PPC_TPREL16_HA:		r = R_PPC64_TPREL16_HA;
   2368       break;
   2369     case BFD_RELOC_PPC64_TPREL16_HIGHA:		r = R_PPC64_TPREL16_HIGHA;
   2370       break;
   2371     case BFD_RELOC_PPC_TPREL:			r = R_PPC64_TPREL64;
   2372       break;
   2373     case BFD_RELOC_PPC_DTPREL16:		r = R_PPC64_DTPREL16;
   2374       break;
   2375     case BFD_RELOC_PPC_DTPREL16_LO:		r = R_PPC64_DTPREL16_LO;
   2376       break;
   2377     case BFD_RELOC_PPC_DTPREL16_HI:		r = R_PPC64_DTPREL16_HI;
   2378       break;
   2379     case BFD_RELOC_PPC64_DTPREL16_HIGH:		r = R_PPC64_DTPREL16_HIGH;
   2380       break;
   2381     case BFD_RELOC_PPC_DTPREL16_HA:		r = R_PPC64_DTPREL16_HA;
   2382       break;
   2383     case BFD_RELOC_PPC64_DTPREL16_HIGHA:	r = R_PPC64_DTPREL16_HIGHA;
   2384       break;
   2385     case BFD_RELOC_PPC_DTPREL:			r = R_PPC64_DTPREL64;
   2386       break;
   2387     case BFD_RELOC_PPC_GOT_TLSGD16:		r = R_PPC64_GOT_TLSGD16;
   2388       break;
   2389     case BFD_RELOC_PPC_GOT_TLSGD16_LO:		r = R_PPC64_GOT_TLSGD16_LO;
   2390       break;
   2391     case BFD_RELOC_PPC_GOT_TLSGD16_HI:		r = R_PPC64_GOT_TLSGD16_HI;
   2392       break;
   2393     case BFD_RELOC_PPC_GOT_TLSGD16_HA:		r = R_PPC64_GOT_TLSGD16_HA;
   2394       break;
   2395     case BFD_RELOC_PPC_GOT_TLSLD16:		r = R_PPC64_GOT_TLSLD16;
   2396       break;
   2397     case BFD_RELOC_PPC_GOT_TLSLD16_LO:		r = R_PPC64_GOT_TLSLD16_LO;
   2398       break;
   2399     case BFD_RELOC_PPC_GOT_TLSLD16_HI:		r = R_PPC64_GOT_TLSLD16_HI;
   2400       break;
   2401     case BFD_RELOC_PPC_GOT_TLSLD16_HA:		r = R_PPC64_GOT_TLSLD16_HA;
   2402       break;
   2403     case BFD_RELOC_PPC_GOT_TPREL16:		r = R_PPC64_GOT_TPREL16_DS;
   2404       break;
   2405     case BFD_RELOC_PPC_GOT_TPREL16_LO:		r = R_PPC64_GOT_TPREL16_LO_DS;
   2406       break;
   2407     case BFD_RELOC_PPC_GOT_TPREL16_HI:		r = R_PPC64_GOT_TPREL16_HI;
   2408       break;
   2409     case BFD_RELOC_PPC_GOT_TPREL16_HA:		r = R_PPC64_GOT_TPREL16_HA;
   2410       break;
   2411     case BFD_RELOC_PPC_GOT_DTPREL16:		r = R_PPC64_GOT_DTPREL16_DS;
   2412       break;
   2413     case BFD_RELOC_PPC_GOT_DTPREL16_LO:		r = R_PPC64_GOT_DTPREL16_LO_DS;
   2414       break;
   2415     case BFD_RELOC_PPC_GOT_DTPREL16_HI:		r = R_PPC64_GOT_DTPREL16_HI;
   2416       break;
   2417     case BFD_RELOC_PPC_GOT_DTPREL16_HA:		r = R_PPC64_GOT_DTPREL16_HA;
   2418       break;
   2419     case BFD_RELOC_PPC64_TPREL16_DS:		r = R_PPC64_TPREL16_DS;
   2420       break;
   2421     case BFD_RELOC_PPC64_TPREL16_LO_DS:		r = R_PPC64_TPREL16_LO_DS;
   2422       break;
   2423     case BFD_RELOC_PPC64_TPREL16_HIGHER:	r = R_PPC64_TPREL16_HIGHER;
   2424       break;
   2425     case BFD_RELOC_PPC64_TPREL16_HIGHERA:	r = R_PPC64_TPREL16_HIGHERA;
   2426       break;
   2427     case BFD_RELOC_PPC64_TPREL16_HIGHEST:	r = R_PPC64_TPREL16_HIGHEST;
   2428       break;
   2429     case BFD_RELOC_PPC64_TPREL16_HIGHESTA:	r = R_PPC64_TPREL16_HIGHESTA;
   2430       break;
   2431     case BFD_RELOC_PPC64_DTPREL16_DS:		r = R_PPC64_DTPREL16_DS;
   2432       break;
   2433     case BFD_RELOC_PPC64_DTPREL16_LO_DS:	r = R_PPC64_DTPREL16_LO_DS;
   2434       break;
   2435     case BFD_RELOC_PPC64_DTPREL16_HIGHER:	r = R_PPC64_DTPREL16_HIGHER;
   2436       break;
   2437     case BFD_RELOC_PPC64_DTPREL16_HIGHERA:	r = R_PPC64_DTPREL16_HIGHERA;
   2438       break;
   2439     case BFD_RELOC_PPC64_DTPREL16_HIGHEST:	r = R_PPC64_DTPREL16_HIGHEST;
   2440       break;
   2441     case BFD_RELOC_PPC64_DTPREL16_HIGHESTA:	r = R_PPC64_DTPREL16_HIGHESTA;
   2442       break;
   2443     case BFD_RELOC_16_PCREL:			r = R_PPC64_REL16;
   2444       break;
   2445     case BFD_RELOC_LO16_PCREL:			r = R_PPC64_REL16_LO;
   2446       break;
   2447     case BFD_RELOC_HI16_PCREL:			r = R_PPC64_REL16_HI;
   2448       break;
   2449     case BFD_RELOC_HI16_S_PCREL:		r = R_PPC64_REL16_HA;
   2450       break;
   2451     case BFD_RELOC_PPC_REL16DX_HA:		r = R_PPC64_REL16DX_HA;
   2452       break;
   2453     case BFD_RELOC_PPC64_ENTRY:			r = R_PPC64_ENTRY;
   2454       break;
   2455     case BFD_RELOC_PPC64_ADDR64_LOCAL:		r = R_PPC64_ADDR64_LOCAL;
   2456       break;
   2457     case BFD_RELOC_VTABLE_INHERIT:		r = R_PPC64_GNU_VTINHERIT;
   2458       break;
   2459     case BFD_RELOC_VTABLE_ENTRY:		r = R_PPC64_GNU_VTENTRY;
   2460       break;
   2461     }
   2462 
   2463   return ppc64_elf_howto_table[r];
   2464 };
   2465 
   2466 static reloc_howto_type *
   2467 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2468 			     const char *r_name)
   2469 {
   2470   unsigned int i;
   2471 
   2472   for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
   2473     if (ppc64_elf_howto_raw[i].name != NULL
   2474 	&& strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
   2475       return &ppc64_elf_howto_raw[i];
   2476 
   2477   return NULL;
   2478 }
   2479 
   2480 /* Set the howto pointer for a PowerPC ELF reloc.  */
   2481 
   2482 static void
   2483 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
   2484 			 Elf_Internal_Rela *dst)
   2485 {
   2486   unsigned int type;
   2487 
   2488   /* Initialize howto table if needed.  */
   2489   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   2490     ppc_howto_init ();
   2491 
   2492   type = ELF64_R_TYPE (dst->r_info);
   2493   if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
   2494     {
   2495       (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
   2496 			     abfd, (int) type);
   2497       type = R_PPC64_NONE;
   2498     }
   2499   cache_ptr->howto = ppc64_elf_howto_table[type];
   2500 }
   2501 
   2502 /* Handle the R_PPC64_ADDR16_HA and similar relocs.  */
   2503 
   2504 static bfd_reloc_status_type
   2505 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2506 		    void *data, asection *input_section,
   2507 		    bfd *output_bfd, char **error_message)
   2508 {
   2509   enum elf_ppc64_reloc_type r_type;
   2510   long insn;
   2511   bfd_size_type octets;
   2512   bfd_vma value;
   2513 
   2514   /* If this is a relocatable link (output_bfd test tells us), just
   2515      call the generic function.  Any adjustment will be done at final
   2516      link time.  */
   2517   if (output_bfd != NULL)
   2518     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2519 				  input_section, output_bfd, error_message);
   2520 
   2521   /* Adjust the addend for sign extension of the low 16 bits.
   2522      We won't actually be using the low 16 bits, so trashing them
   2523      doesn't matter.  */
   2524   reloc_entry->addend += 0x8000;
   2525   r_type = reloc_entry->howto->type;
   2526   if (r_type != R_PPC64_REL16DX_HA)
   2527     return bfd_reloc_continue;
   2528 
   2529   value = 0;
   2530   if (!bfd_is_com_section (symbol->section))
   2531     value = symbol->value;
   2532   value += (reloc_entry->addend
   2533 	    + symbol->section->output_offset
   2534 	    + symbol->section->output_section->vma);
   2535   value -= (reloc_entry->address
   2536 	    + input_section->output_offset
   2537 	    + input_section->output_section->vma);
   2538   value = (bfd_signed_vma) value >> 16;
   2539 
   2540   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2541   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   2542   insn &= ~0x1fffc1;
   2543   insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
   2544   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   2545   if (value + 0x8000 > 0xffff)
   2546     return bfd_reloc_overflow;
   2547   return bfd_reloc_ok;
   2548 }
   2549 
   2550 static bfd_reloc_status_type
   2551 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2552 			void *data, asection *input_section,
   2553 			bfd *output_bfd, char **error_message)
   2554 {
   2555   if (output_bfd != NULL)
   2556     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2557 				  input_section, output_bfd, error_message);
   2558 
   2559   if (strcmp (symbol->section->name, ".opd") == 0
   2560       && (symbol->section->owner->flags & DYNAMIC) == 0)
   2561     {
   2562       bfd_vma dest = opd_entry_value (symbol->section,
   2563 				      symbol->value + reloc_entry->addend,
   2564 				      NULL, NULL, FALSE);
   2565       if (dest != (bfd_vma) -1)
   2566 	reloc_entry->addend = dest - (symbol->value
   2567 				      + symbol->section->output_section->vma
   2568 				      + symbol->section->output_offset);
   2569     }
   2570   else
   2571     {
   2572       elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
   2573 
   2574       if (symbol->section->owner != abfd
   2575 	  && symbol->section->owner != NULL
   2576 	  && abiversion (symbol->section->owner) >= 2)
   2577 	{
   2578 	  unsigned int i;
   2579 
   2580 	  for (i = 0; i < symbol->section->owner->symcount; ++i)
   2581 	    {
   2582 	      asymbol *symdef = symbol->section->owner->outsymbols[i];
   2583 
   2584 	      if (strcmp (symdef->name, symbol->name) == 0)
   2585 		{
   2586 		  elfsym = (elf_symbol_type *) symdef;
   2587 		  break;
   2588 		}
   2589 	    }
   2590 	}
   2591       reloc_entry->addend
   2592 	+= PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
   2593     }
   2594   return bfd_reloc_continue;
   2595 }
   2596 
   2597 static bfd_reloc_status_type
   2598 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2599 			 void *data, asection *input_section,
   2600 			 bfd *output_bfd, char **error_message)
   2601 {
   2602   long insn;
   2603   enum elf_ppc64_reloc_type r_type;
   2604   bfd_size_type octets;
   2605   /* Assume 'at' branch hints.  */
   2606   bfd_boolean is_isa_v2 = TRUE;
   2607 
   2608   /* If this is a relocatable link (output_bfd test tells us), just
   2609      call the generic function.  Any adjustment will be done at final
   2610      link time.  */
   2611   if (output_bfd != NULL)
   2612     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2613 				  input_section, output_bfd, error_message);
   2614 
   2615   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2616   insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
   2617   insn &= ~(0x01 << 21);
   2618   r_type = reloc_entry->howto->type;
   2619   if (r_type == R_PPC64_ADDR14_BRTAKEN
   2620       || r_type == R_PPC64_REL14_BRTAKEN)
   2621     insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   2622 
   2623   if (is_isa_v2)
   2624     {
   2625       /* Set 'a' bit.  This is 0b00010 in BO field for branch
   2626 	 on CR(BI) insns (BO == 001at or 011at), and 0b01000
   2627 	 for branch on CTR insns (BO == 1a00t or 1a01t).  */
   2628       if ((insn & (0x14 << 21)) == (0x04 << 21))
   2629 	insn |= 0x02 << 21;
   2630       else if ((insn & (0x14 << 21)) == (0x10 << 21))
   2631 	insn |= 0x08 << 21;
   2632       else
   2633 	goto out;
   2634     }
   2635   else
   2636     {
   2637       bfd_vma target = 0;
   2638       bfd_vma from;
   2639 
   2640       if (!bfd_is_com_section (symbol->section))
   2641 	target = symbol->value;
   2642       target += symbol->section->output_section->vma;
   2643       target += symbol->section->output_offset;
   2644       target += reloc_entry->addend;
   2645 
   2646       from = (reloc_entry->address
   2647 	      + input_section->output_offset
   2648 	      + input_section->output_section->vma);
   2649 
   2650       /* Invert 'y' bit if not the default.  */
   2651       if ((bfd_signed_vma) (target - from) < 0)
   2652 	insn ^= 0x01 << 21;
   2653     }
   2654   bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
   2655  out:
   2656   return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
   2657 				 input_section, output_bfd, error_message);
   2658 }
   2659 
   2660 static bfd_reloc_status_type
   2661 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2662 			 void *data, asection *input_section,
   2663 			 bfd *output_bfd, char **error_message)
   2664 {
   2665   /* If this is a relocatable link (output_bfd test tells us), just
   2666      call the generic function.  Any adjustment will be done at final
   2667      link time.  */
   2668   if (output_bfd != NULL)
   2669     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2670 				  input_section, output_bfd, error_message);
   2671 
   2672   /* Subtract the symbol section base address.  */
   2673   reloc_entry->addend -= symbol->section->output_section->vma;
   2674   return bfd_reloc_continue;
   2675 }
   2676 
   2677 static bfd_reloc_status_type
   2678 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2679 			    void *data, asection *input_section,
   2680 			    bfd *output_bfd, char **error_message)
   2681 {
   2682   /* If this is a relocatable link (output_bfd test tells us), just
   2683      call the generic function.  Any adjustment will be done at final
   2684      link time.  */
   2685   if (output_bfd != NULL)
   2686     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2687 				  input_section, output_bfd, error_message);
   2688 
   2689   /* Subtract the symbol section base address.  */
   2690   reloc_entry->addend -= symbol->section->output_section->vma;
   2691 
   2692   /* Adjust the addend for sign extension of the low 16 bits.  */
   2693   reloc_entry->addend += 0x8000;
   2694   return bfd_reloc_continue;
   2695 }
   2696 
   2697 static bfd_reloc_status_type
   2698 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2699 		     void *data, asection *input_section,
   2700 		     bfd *output_bfd, char **error_message)
   2701 {
   2702   bfd_vma TOCstart;
   2703 
   2704   /* If this is a relocatable link (output_bfd test tells us), just
   2705      call the generic function.  Any adjustment will be done at final
   2706      link time.  */
   2707   if (output_bfd != NULL)
   2708     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2709 				  input_section, output_bfd, error_message);
   2710 
   2711   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2712   if (TOCstart == 0)
   2713     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   2714 
   2715   /* Subtract the TOC base address.  */
   2716   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2717   return bfd_reloc_continue;
   2718 }
   2719 
   2720 static bfd_reloc_status_type
   2721 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2722 			void *data, asection *input_section,
   2723 			bfd *output_bfd, char **error_message)
   2724 {
   2725   bfd_vma TOCstart;
   2726 
   2727   /* If this is a relocatable link (output_bfd test tells us), just
   2728      call the generic function.  Any adjustment will be done at final
   2729      link time.  */
   2730   if (output_bfd != NULL)
   2731     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2732 				  input_section, output_bfd, error_message);
   2733 
   2734   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2735   if (TOCstart == 0)
   2736     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   2737 
   2738   /* Subtract the TOC base address.  */
   2739   reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
   2740 
   2741   /* Adjust the addend for sign extension of the low 16 bits.  */
   2742   reloc_entry->addend += 0x8000;
   2743   return bfd_reloc_continue;
   2744 }
   2745 
   2746 static bfd_reloc_status_type
   2747 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2748 		       void *data, asection *input_section,
   2749 		       bfd *output_bfd, char **error_message)
   2750 {
   2751   bfd_vma TOCstart;
   2752   bfd_size_type octets;
   2753 
   2754   /* If this is a relocatable link (output_bfd test tells us), just
   2755      call the generic function.  Any adjustment will be done at final
   2756      link time.  */
   2757   if (output_bfd != NULL)
   2758     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2759 				  input_section, output_bfd, error_message);
   2760 
   2761   TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
   2762   if (TOCstart == 0)
   2763     TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
   2764 
   2765   octets = reloc_entry->address * bfd_octets_per_byte (abfd);
   2766   bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
   2767   return bfd_reloc_ok;
   2768 }
   2769 
   2770 static bfd_reloc_status_type
   2771 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2772 			   void *data, asection *input_section,
   2773 			   bfd *output_bfd, char **error_message)
   2774 {
   2775   /* If this is a relocatable link (output_bfd test tells us), just
   2776      call the generic function.  Any adjustment will be done at final
   2777      link time.  */
   2778   if (output_bfd != NULL)
   2779     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
   2780 				  input_section, output_bfd, error_message);
   2781 
   2782   if (error_message != NULL)
   2783     {
   2784       static char buf[60];
   2785       sprintf (buf, "generic linker can't handle %s",
   2786 	       reloc_entry->howto->name);
   2787       *error_message = buf;
   2788     }
   2789   return bfd_reloc_dangerous;
   2790 }
   2791 
   2792 /* Track GOT entries needed for a given symbol.  We might need more
   2793    than one got entry per symbol.  */
   2794 struct got_entry
   2795 {
   2796   struct got_entry *next;
   2797 
   2798   /* The symbol addend that we'll be placing in the GOT.  */
   2799   bfd_vma addend;
   2800 
   2801   /* Unlike other ELF targets, we use separate GOT entries for the same
   2802      symbol referenced from different input files.  This is to support
   2803      automatic multiple TOC/GOT sections, where the TOC base can vary
   2804      from one input file to another.  After partitioning into TOC groups
   2805      we merge entries within the group.
   2806 
   2807      Point to the BFD owning this GOT entry.  */
   2808   bfd *owner;
   2809 
   2810   /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
   2811      TLS_TPREL or TLS_DTPREL for tls entries.  */
   2812   unsigned char tls_type;
   2813 
   2814   /* Non-zero if got.ent points to real entry.  */
   2815   unsigned char is_indirect;
   2816 
   2817   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
   2818   union
   2819     {
   2820       bfd_signed_vma refcount;
   2821       bfd_vma offset;
   2822       struct got_entry *ent;
   2823     } got;
   2824 };
   2825 
   2826 /* The same for PLT.  */
   2827 struct plt_entry
   2828 {
   2829   struct plt_entry *next;
   2830 
   2831   bfd_vma addend;
   2832 
   2833   union
   2834     {
   2835       bfd_signed_vma refcount;
   2836       bfd_vma offset;
   2837     } plt;
   2838 };
   2839 
   2840 struct ppc64_elf_obj_tdata
   2841 {
   2842   struct elf_obj_tdata elf;
   2843 
   2844   /* Shortcuts to dynamic linker sections.  */
   2845   asection *got;
   2846   asection *relgot;
   2847 
   2848   /* Used during garbage collection.  We attach global symbols defined
   2849      on removed .opd entries to this section so that the sym is removed.  */
   2850   asection *deleted_section;
   2851 
   2852   /* TLS local dynamic got entry handling.  Support for multiple GOT
   2853      sections means we potentially need one of these for each input bfd.  */
   2854   struct got_entry tlsld_got;
   2855 
   2856   union {
   2857     /* A copy of relocs before they are modified for --emit-relocs.  */
   2858     Elf_Internal_Rela *relocs;
   2859 
   2860     /* Section contents.  */
   2861     bfd_byte *contents;
   2862   } opd;
   2863 
   2864   /* Nonzero if this bfd has small toc/got relocs, ie. that expect
   2865      the reloc to be in the range -32768 to 32767.  */
   2866   unsigned int has_small_toc_reloc : 1;
   2867 
   2868   /* Set if toc/got ha relocs detected not using r2, or lo reloc
   2869      instruction not one we handle.  */
   2870   unsigned int unexpected_toc_insn : 1;
   2871 };
   2872 
   2873 #define ppc64_elf_tdata(bfd) \
   2874   ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
   2875 
   2876 #define ppc64_tlsld_got(bfd) \
   2877   (&ppc64_elf_tdata (bfd)->tlsld_got)
   2878 
   2879 #define is_ppc64_elf(bfd) \
   2880   (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
   2881    && elf_object_id (bfd) == PPC64_ELF_DATA)
   2882 
   2883 /* Override the generic function because we store some extras.  */
   2884 
   2885 static bfd_boolean
   2886 ppc64_elf_mkobject (bfd *abfd)
   2887 {
   2888   return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
   2889 				  PPC64_ELF_DATA);
   2890 }
   2891 
   2892 /* Fix bad default arch selected for a 64 bit input bfd when the
   2893    default is 32 bit.  Also select arch based on apuinfo.  */
   2894 
   2895 static bfd_boolean
   2896 ppc64_elf_object_p (bfd *abfd)
   2897 {
   2898   if (!abfd->arch_info->the_default)
   2899     return TRUE;
   2900 
   2901   if (abfd->arch_info->bits_per_word == 32)
   2902     {
   2903       Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
   2904 
   2905       if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
   2906 	{
   2907 	  /* Relies on arch after 32 bit default being 64 bit default.  */
   2908 	  abfd->arch_info = abfd->arch_info->next;
   2909 	  BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
   2910 	}
   2911     }
   2912   return _bfd_elf_ppc_set_arch (abfd);
   2913 }
   2914 
   2915 /* Support for core dump NOTE sections.  */
   2916 
   2917 static bfd_boolean
   2918 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   2919 {
   2920   size_t offset, size;
   2921 
   2922   if (note->descsz != 504)
   2923     return FALSE;
   2924 
   2925   /* pr_cursig */
   2926   elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   2927 
   2928   /* pr_pid */
   2929   elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
   2930 
   2931   /* pr_reg */
   2932   offset = 112;
   2933   size = 384;
   2934 
   2935   /* Make a ".reg/999" section.  */
   2936   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   2937 					  size, note->descpos + offset);
   2938 }
   2939 
   2940 static bfd_boolean
   2941 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   2942 {
   2943   if (note->descsz != 136)
   2944     return FALSE;
   2945 
   2946   elf_tdata (abfd)->core->pid
   2947     = bfd_get_32 (abfd, note->descdata + 24);
   2948   elf_tdata (abfd)->core->program
   2949     = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
   2950   elf_tdata (abfd)->core->command
   2951     = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
   2952 
   2953   return TRUE;
   2954 }
   2955 
   2956 static char *
   2957 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
   2958 			   ...)
   2959 {
   2960   switch (note_type)
   2961     {
   2962     default:
   2963       return NULL;
   2964 
   2965     case NT_PRPSINFO:
   2966       {
   2967 	char data[136];
   2968 	va_list ap;
   2969 
   2970 	va_start (ap, note_type);
   2971 	memset (data, 0, sizeof (data));
   2972 	strncpy (data + 40, va_arg (ap, const char *), 16);
   2973 	strncpy (data + 56, va_arg (ap, const char *), 80);
   2974 	va_end (ap);
   2975 	return elfcore_write_note (abfd, buf, bufsiz,
   2976 				   "CORE", note_type, data, sizeof (data));
   2977       }
   2978 
   2979     case NT_PRSTATUS:
   2980       {
   2981 	char data[504];
   2982 	va_list ap;
   2983 	long pid;
   2984 	int cursig;
   2985 	const void *greg;
   2986 
   2987 	va_start (ap, note_type);
   2988 	memset (data, 0, 112);
   2989 	pid = va_arg (ap, long);
   2990 	bfd_put_32 (abfd, pid, data + 32);
   2991 	cursig = va_arg (ap, int);
   2992 	bfd_put_16 (abfd, cursig, data + 12);
   2993 	greg = va_arg (ap, const void *);
   2994 	memcpy (data + 112, greg, 384);
   2995 	memset (data + 496, 0, 8);
   2996 	va_end (ap);
   2997 	return elfcore_write_note (abfd, buf, bufsiz,
   2998 				   "CORE", note_type, data, sizeof (data));
   2999       }
   3000     }
   3001 }
   3002 
   3003 /* Add extra PPC sections.  */
   3004 
   3005 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
   3006 {
   3007   { STRING_COMMA_LEN (".plt"),    0, SHT_NOBITS,   0 },
   3008   { STRING_COMMA_LEN (".sbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3009   { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3010   { STRING_COMMA_LEN (".toc"),    0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3011   { STRING_COMMA_LEN (".toc1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
   3012   { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
   3013   { NULL,                     0,  0, 0,            0 }
   3014 };
   3015 
   3016 enum _ppc64_sec_type {
   3017   sec_normal = 0,
   3018   sec_opd = 1,
   3019   sec_toc = 2
   3020 };
   3021 
   3022 struct _ppc64_elf_section_data
   3023 {
   3024   struct bfd_elf_section_data elf;
   3025 
   3026   union
   3027   {
   3028     /* An array with one entry for each opd function descriptor,
   3029        and some spares since opd entries may be either 16 or 24 bytes.  */
   3030 #define OPD_NDX(OFF) ((OFF) >> 4)
   3031     struct _opd_sec_data
   3032     {
   3033       /* Points to the function code section for local opd entries.  */
   3034       asection **func_sec;
   3035 
   3036       /* After editing .opd, adjust references to opd local syms.  */
   3037       long *adjust;
   3038     } opd;
   3039 
   3040     /* An array for toc sections, indexed by offset/8.  */
   3041     struct _toc_sec_data
   3042     {
   3043       /* Specifies the relocation symbol index used at a given toc offset.  */
   3044       unsigned *symndx;
   3045 
   3046       /* And the relocation addend.  */
   3047       bfd_vma *add;
   3048     } toc;
   3049   } u;
   3050 
   3051   enum _ppc64_sec_type sec_type:2;
   3052 
   3053   /* Flag set when small branches are detected.  Used to
   3054      select suitable defaults for the stub group size.  */
   3055   unsigned int has_14bit_branch:1;
   3056 };
   3057 
   3058 #define ppc64_elf_section_data(sec) \
   3059   ((struct _ppc64_elf_section_data *) elf_section_data (sec))
   3060 
   3061 static bfd_boolean
   3062 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
   3063 {
   3064   if (!sec->used_by_bfd)
   3065     {
   3066       struct _ppc64_elf_section_data *sdata;
   3067       bfd_size_type amt = sizeof (*sdata);
   3068 
   3069       sdata = bfd_zalloc (abfd, amt);
   3070       if (sdata == NULL)
   3071 	return FALSE;
   3072       sec->used_by_bfd = sdata;
   3073     }
   3074 
   3075   return _bfd_elf_new_section_hook (abfd, sec);
   3076 }
   3077 
   3078 static struct _opd_sec_data *
   3079 get_opd_info (asection * sec)
   3080 {
   3081   if (sec != NULL
   3082       && ppc64_elf_section_data (sec) != NULL
   3083       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
   3084     return &ppc64_elf_section_data (sec)->u.opd;
   3085   return NULL;
   3086 }
   3087 
   3088 /* Parameters for the qsort hook.  */
   3090 static bfd_boolean synthetic_relocatable;
   3091 static asection *synthetic_opd;
   3092 
   3093 /* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
   3094 
   3095 static int
   3096 compare_symbols (const void *ap, const void *bp)
   3097 {
   3098   const asymbol *a = * (const asymbol **) ap;
   3099   const asymbol *b = * (const asymbol **) bp;
   3100 
   3101   /* Section symbols first.  */
   3102   if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
   3103     return -1;
   3104   if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
   3105     return 1;
   3106 
   3107   /* then .opd symbols.  */
   3108   if (synthetic_opd != NULL)
   3109     {
   3110       if (strcmp (a->section->name, ".opd") == 0
   3111 	  && strcmp (b->section->name, ".opd") != 0)
   3112 	return -1;
   3113       if (strcmp (a->section->name, ".opd") != 0
   3114 	  && strcmp (b->section->name, ".opd") == 0)
   3115 	return 1;
   3116     }
   3117 
   3118   /* then other code symbols.  */
   3119   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3120       == (SEC_CODE | SEC_ALLOC)
   3121       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3122 	 != (SEC_CODE | SEC_ALLOC))
   3123     return -1;
   3124 
   3125   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3126       != (SEC_CODE | SEC_ALLOC)
   3127       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3128 	 == (SEC_CODE | SEC_ALLOC))
   3129     return 1;
   3130 
   3131   if (synthetic_relocatable)
   3132     {
   3133       if (a->section->id < b->section->id)
   3134 	return -1;
   3135 
   3136       if (a->section->id > b->section->id)
   3137 	return 1;
   3138     }
   3139 
   3140   if (a->value + a->section->vma < b->value + b->section->vma)
   3141     return -1;
   3142 
   3143   if (a->value + a->section->vma > b->value + b->section->vma)
   3144     return 1;
   3145 
   3146   /* For syms with the same value, prefer strong dynamic global function
   3147      syms over other syms.  */
   3148   if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
   3149     return -1;
   3150 
   3151   if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
   3152     return 1;
   3153 
   3154   if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
   3155     return -1;
   3156 
   3157   if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
   3158     return 1;
   3159 
   3160   if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
   3161     return -1;
   3162 
   3163   if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
   3164     return 1;
   3165 
   3166   if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
   3167     return -1;
   3168 
   3169   if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
   3170     return 1;
   3171 
   3172   return 0;
   3173 }
   3174 
   3175 /* Search SYMS for a symbol of the given VALUE.  */
   3176 
   3177 static asymbol *
   3178 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
   3179 {
   3180   long mid;
   3181 
   3182   if (id == (unsigned) -1)
   3183     {
   3184       while (lo < hi)
   3185 	{
   3186 	  mid = (lo + hi) >> 1;
   3187 	  if (syms[mid]->value + syms[mid]->section->vma < value)
   3188 	    lo = mid + 1;
   3189 	  else if (syms[mid]->value + syms[mid]->section->vma > value)
   3190 	    hi = mid;
   3191 	  else
   3192 	    return syms[mid];
   3193 	}
   3194     }
   3195   else
   3196     {
   3197       while (lo < hi)
   3198 	{
   3199 	  mid = (lo + hi) >> 1;
   3200 	  if (syms[mid]->section->id < id)
   3201 	    lo = mid + 1;
   3202 	  else if (syms[mid]->section->id > id)
   3203 	    hi = mid;
   3204 	  else if (syms[mid]->value < value)
   3205 	    lo = mid + 1;
   3206 	  else if (syms[mid]->value > value)
   3207 	    hi = mid;
   3208 	  else
   3209 	    return syms[mid];
   3210 	}
   3211     }
   3212   return NULL;
   3213 }
   3214 
   3215 static bfd_boolean
   3216 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
   3217 {
   3218   bfd_vma vma = *(bfd_vma *) ptr;
   3219   return ((section->flags & SEC_ALLOC) != 0
   3220 	  && section->vma <= vma
   3221 	  && vma < section->vma + section->size);
   3222 }
   3223 
   3224 /* Create synthetic symbols, effectively restoring "dot-symbol" function
   3225    entry syms.  Also generate @plt symbols for the glink branch table.
   3226    Returns count of synthetic symbols in RET or -1 on error.  */
   3227 
   3228 static long
   3229 ppc64_elf_get_synthetic_symtab (bfd *abfd,
   3230 				long static_count, asymbol **static_syms,
   3231 				long dyn_count, asymbol **dyn_syms,
   3232 				asymbol **ret)
   3233 {
   3234   asymbol *s;
   3235   long i;
   3236   long count;
   3237   char *names;
   3238   long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
   3239   asection *opd = NULL;
   3240   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
   3241   asymbol **syms;
   3242   int abi = abiversion (abfd);
   3243 
   3244   *ret = NULL;
   3245 
   3246   if (abi < 2)
   3247     {
   3248       opd = bfd_get_section_by_name (abfd, ".opd");
   3249       if (opd == NULL && abi == 1)
   3250 	return 0;
   3251     }
   3252 
   3253   symcount = static_count;
   3254   if (!relocatable)
   3255     symcount += dyn_count;
   3256   if (symcount == 0)
   3257     return 0;
   3258 
   3259   syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
   3260   if (syms == NULL)
   3261     return -1;
   3262 
   3263   if (!relocatable && static_count != 0 && dyn_count != 0)
   3264     {
   3265       /* Use both symbol tables.  */
   3266       memcpy (syms, static_syms, static_count * sizeof (*syms));
   3267       memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
   3268     }
   3269   else if (!relocatable && static_count == 0)
   3270     memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
   3271   else
   3272     memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
   3273 
   3274   synthetic_relocatable = relocatable;
   3275   synthetic_opd = opd;
   3276   qsort (syms, symcount, sizeof (*syms), compare_symbols);
   3277 
   3278   if (!relocatable && symcount > 1)
   3279     {
   3280       long j;
   3281       /* Trim duplicate syms, since we may have merged the normal and
   3282 	 dynamic symbols.  Actually, we only care about syms that have
   3283 	 different values, so trim any with the same value.  */
   3284       for (i = 1, j = 1; i < symcount; ++i)
   3285 	if (syms[i - 1]->value + syms[i - 1]->section->vma
   3286 	    != syms[i]->value + syms[i]->section->vma)
   3287 	  syms[j++] = syms[i];
   3288       symcount = j;
   3289     }
   3290 
   3291   i = 0;
   3292   /* Note that here and in compare_symbols we can't compare opd and
   3293      sym->section directly.  With separate debug info files, the
   3294      symbols will be extracted from the debug file while abfd passed
   3295      to this function is the real binary.  */
   3296   if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
   3297     ++i;
   3298   codesecsym = i;
   3299 
   3300   for (; i < symcount; ++i)
   3301     if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3302 	 != (SEC_CODE | SEC_ALLOC))
   3303 	|| (syms[i]->flags & BSF_SECTION_SYM) == 0)
   3304       break;
   3305   codesecsymend = i;
   3306 
   3307   for (; i < symcount; ++i)
   3308     if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
   3309       break;
   3310   secsymend = i;
   3311 
   3312   if (opd != NULL)
   3313     for (; i < symcount; ++i)
   3314       if (strcmp (syms[i]->section->name, ".opd") != 0)
   3315 	break;
   3316   opdsymend = i;
   3317 
   3318   for (; i < symcount; ++i)
   3319     if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
   3320 	!= (SEC_CODE | SEC_ALLOC))
   3321       break;
   3322   symcount = i;
   3323 
   3324   count = 0;
   3325 
   3326   if (relocatable)
   3327     {
   3328       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3329       arelent *r;
   3330       size_t size;
   3331       long relcount;
   3332 
   3333       if (opdsymend == secsymend)
   3334 	goto done;
   3335 
   3336       slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3337       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
   3338       if (relcount == 0)
   3339 	goto done;
   3340 
   3341       if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
   3342 	{
   3343 	  count = -1;
   3344 	  goto done;
   3345 	}
   3346 
   3347       size = 0;
   3348       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3349 	{
   3350 	  asymbol *sym;
   3351 
   3352 	  while (r < opd->relocation + relcount
   3353 		 && r->address < syms[i]->value + opd->vma)
   3354 	    ++r;
   3355 
   3356 	  if (r == opd->relocation + relcount)
   3357 	    break;
   3358 
   3359 	  if (r->address != syms[i]->value + opd->vma)
   3360 	    continue;
   3361 
   3362 	  if (r->howto->type != R_PPC64_ADDR64)
   3363 	    continue;
   3364 
   3365 	  sym = *r->sym_ptr_ptr;
   3366 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3367 			      sym->section->id, sym->value + r->addend))
   3368 	    {
   3369 	      ++count;
   3370 	      size += sizeof (asymbol);
   3371 	      size += strlen (syms[i]->name) + 2;
   3372 	    }
   3373 	}
   3374 
   3375       if (size == 0)
   3376 	goto done;
   3377       s = *ret = bfd_malloc (size);
   3378       if (s == NULL)
   3379 	{
   3380 	  count = -1;
   3381 	  goto done;
   3382 	}
   3383 
   3384       names = (char *) (s + count);
   3385 
   3386       for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
   3387 	{
   3388 	  asymbol *sym;
   3389 
   3390 	  while (r < opd->relocation + relcount
   3391 		 && r->address < syms[i]->value + opd->vma)
   3392 	    ++r;
   3393 
   3394 	  if (r == opd->relocation + relcount)
   3395 	    break;
   3396 
   3397 	  if (r->address != syms[i]->value + opd->vma)
   3398 	    continue;
   3399 
   3400 	  if (r->howto->type != R_PPC64_ADDR64)
   3401 	    continue;
   3402 
   3403 	  sym = *r->sym_ptr_ptr;
   3404 	  if (!sym_exists_at (syms, opdsymend, symcount,
   3405 			      sym->section->id, sym->value + r->addend))
   3406 	    {
   3407 	      size_t len;
   3408 
   3409 	      *s = *syms[i];
   3410 	      s->flags |= BSF_SYNTHETIC;
   3411 	      s->section = sym->section;
   3412 	      s->value = sym->value + r->addend;
   3413 	      s->name = names;
   3414 	      *names++ = '.';
   3415 	      len = strlen (syms[i]->name);
   3416 	      memcpy (names, syms[i]->name, len + 1);
   3417 	      names += len + 1;
   3418 	      /* Have udata.p point back to the original symbol this
   3419 		 synthetic symbol was derived from.  */
   3420 	      s->udata.p = syms[i];
   3421 	      s++;
   3422 	    }
   3423 	}
   3424     }
   3425   else
   3426     {
   3427       bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
   3428       bfd_byte *contents = NULL;
   3429       size_t size;
   3430       long plt_count = 0;
   3431       bfd_vma glink_vma = 0, resolv_vma = 0;
   3432       asection *dynamic, *glink = NULL, *relplt = NULL;
   3433       arelent *p;
   3434 
   3435       if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
   3436 	{
   3437 	free_contents_and_exit_err:
   3438 	  count = -1;
   3439 	free_contents_and_exit:
   3440 	  if (contents)
   3441 	    free (contents);
   3442 	  goto done;
   3443 	}
   3444 
   3445       size = 0;
   3446       for (i = secsymend; i < opdsymend; ++i)
   3447 	{
   3448 	  bfd_vma ent;
   3449 
   3450 	  /* Ignore bogus symbols.  */
   3451 	  if (syms[i]->value > opd->size - 8)
   3452 	    continue;
   3453 
   3454 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3455 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3456 	    {
   3457 	      ++count;
   3458 	      size += sizeof (asymbol);
   3459 	      size += strlen (syms[i]->name) + 2;
   3460 	    }
   3461 	}
   3462 
   3463       /* Get start of .glink stubs from DT_PPC64_GLINK.  */
   3464       if (dyn_count != 0
   3465 	  && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
   3466 	{
   3467 	  bfd_byte *dynbuf, *extdyn, *extdynend;
   3468 	  size_t extdynsize;
   3469 	  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
   3470 
   3471 	  if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
   3472 	    goto free_contents_and_exit_err;
   3473 
   3474 	  extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
   3475 	  swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
   3476 
   3477 	  extdyn = dynbuf;
   3478 	  extdynend = extdyn + dynamic->size;
   3479 	  for (; extdyn < extdynend; extdyn += extdynsize)
   3480 	    {
   3481 	      Elf_Internal_Dyn dyn;
   3482 	      (*swap_dyn_in) (abfd, extdyn, &dyn);
   3483 
   3484 	      if (dyn.d_tag == DT_NULL)
   3485 		break;
   3486 
   3487 	      if (dyn.d_tag == DT_PPC64_GLINK)
   3488 		{
   3489 		  /* The first glink stub starts at offset 32; see
   3490 		     comment in ppc64_elf_finish_dynamic_sections. */
   3491 		  glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
   3492 		  /* The .glink section usually does not survive the final
   3493 		     link; search for the section (usually .text) where the
   3494 		     glink stubs now reside.  */
   3495 		  glink = bfd_sections_find_if (abfd, section_covers_vma,
   3496 						&glink_vma);
   3497 		  break;
   3498 		}
   3499 	    }
   3500 
   3501 	  free (dynbuf);
   3502 	}
   3503 
   3504       if (glink != NULL)
   3505 	{
   3506 	  /* Determine __glink trampoline by reading the relative branch
   3507 	     from the first glink stub.  */
   3508 	  bfd_byte buf[4];
   3509 	  unsigned int off = 0;
   3510 
   3511 	  while (bfd_get_section_contents (abfd, glink, buf,
   3512 					   glink_vma + off - glink->vma, 4))
   3513 	    {
   3514 	      unsigned int insn = bfd_get_32 (abfd, buf);
   3515 	      insn ^= B_DOT;
   3516 	      if ((insn & ~0x3fffffc) == 0)
   3517 		{
   3518 		  resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
   3519 		  break;
   3520 		}
   3521 	      off += 4;
   3522 	      if (off > 4)
   3523 		break;
   3524 	    }
   3525 
   3526 	  if (resolv_vma)
   3527 	    size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
   3528 
   3529 	  relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   3530 	  if (relplt != NULL)
   3531 	    {
   3532 	      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
   3533 	      if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
   3534 		goto free_contents_and_exit_err;
   3535 
   3536 	      plt_count = relplt->size / sizeof (Elf64_External_Rela);
   3537 	      size += plt_count * sizeof (asymbol);
   3538 
   3539 	      p = relplt->relocation;
   3540 	      for (i = 0; i < plt_count; i++, p++)
   3541 		{
   3542 		  size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
   3543 		  if (p->addend != 0)
   3544 		    size += sizeof ("+0x") - 1 + 16;
   3545 		}
   3546 	    }
   3547 	}
   3548 
   3549       if (size == 0)
   3550 	goto free_contents_and_exit;
   3551       s = *ret = bfd_malloc (size);
   3552       if (s == NULL)
   3553 	goto free_contents_and_exit_err;
   3554 
   3555       names = (char *) (s + count + plt_count + (resolv_vma != 0));
   3556 
   3557       for (i = secsymend; i < opdsymend; ++i)
   3558 	{
   3559 	  bfd_vma ent;
   3560 
   3561 	  if (syms[i]->value > opd->size - 8)
   3562 	    continue;
   3563 
   3564 	  ent = bfd_get_64 (abfd, contents + syms[i]->value);
   3565 	  if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
   3566 	    {
   3567 	      long lo, hi;
   3568 	      size_t len;
   3569 	      asection *sec = abfd->sections;
   3570 
   3571 	      *s = *syms[i];
   3572 	      lo = codesecsym;
   3573 	      hi = codesecsymend;
   3574 	      while (lo < hi)
   3575 		{
   3576 		  long mid = (lo + hi) >> 1;
   3577 		  if (syms[mid]->section->vma < ent)
   3578 		    lo = mid + 1;
   3579 		  else if (syms[mid]->section->vma > ent)
   3580 		    hi = mid;
   3581 		  else
   3582 		    {
   3583 		      sec = syms[mid]->section;
   3584 		      break;
   3585 		    }
   3586 		}
   3587 
   3588 	      if (lo >= hi && lo > codesecsym)
   3589 		sec = syms[lo - 1]->section;
   3590 
   3591 	      for (; sec != NULL; sec = sec->next)
   3592 		{
   3593 		  if (sec->vma > ent)
   3594 		    break;
   3595 		  /* SEC_LOAD may not be set if SEC is from a separate debug
   3596 		     info file.  */
   3597 		  if ((sec->flags & SEC_ALLOC) == 0)
   3598 		    break;
   3599 		  if ((sec->flags & SEC_CODE) != 0)
   3600 		    s->section = sec;
   3601 		}
   3602 	      s->flags |= BSF_SYNTHETIC;
   3603 	      s->value = ent - s->section->vma;
   3604 	      s->name = names;
   3605 	      *names++ = '.';
   3606 	      len = strlen (syms[i]->name);
   3607 	      memcpy (names, syms[i]->name, len + 1);
   3608 	      names += len + 1;
   3609 	      /* Have udata.p point back to the original symbol this
   3610 		 synthetic symbol was derived from.  */
   3611 	      s->udata.p = syms[i];
   3612 	      s++;
   3613 	    }
   3614 	}
   3615       free (contents);
   3616 
   3617       if (glink != NULL && relplt != NULL)
   3618 	{
   3619 	  if (resolv_vma)
   3620 	    {
   3621 	      /* Add a symbol for the main glink trampoline.  */
   3622 	      memset (s, 0, sizeof *s);
   3623 	      s->the_bfd = abfd;
   3624 	      s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
   3625 	      s->section = glink;
   3626 	      s->value = resolv_vma - glink->vma;
   3627 	      s->name = names;
   3628 	      memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
   3629 	      names += sizeof ("__glink_PLTresolve");
   3630 	      s++;
   3631 	      count++;
   3632 	    }
   3633 
   3634 	  /* FIXME: It would be very much nicer to put sym@plt on the
   3635 	     stub rather than on the glink branch table entry.  The
   3636 	     objdump disassembler would then use a sensible symbol
   3637 	     name on plt calls.  The difficulty in doing so is
   3638 	     a) finding the stubs, and,
   3639 	     b) matching stubs against plt entries, and,
   3640 	     c) there can be multiple stubs for a given plt entry.
   3641 
   3642 	     Solving (a) could be done by code scanning, but older
   3643 	     ppc64 binaries used different stubs to current code.
   3644 	     (b) is the tricky one since you need to known the toc
   3645 	     pointer for at least one function that uses a pic stub to
   3646 	     be able to calculate the plt address referenced.
   3647 	     (c) means gdb would need to set multiple breakpoints (or
   3648 	     find the glink branch itself) when setting breakpoints
   3649 	     for pending shared library loads.  */
   3650 	  p = relplt->relocation;
   3651 	  for (i = 0; i < plt_count; i++, p++)
   3652 	    {
   3653 	      size_t len;
   3654 
   3655 	      *s = **p->sym_ptr_ptr;
   3656 	      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
   3657 		 we are defining a symbol, ensure one of them is set.  */
   3658 	      if ((s->flags & BSF_LOCAL) == 0)
   3659 		s->flags |= BSF_GLOBAL;
   3660 	      s->flags |= BSF_SYNTHETIC;
   3661 	      s->section = glink;
   3662 	      s->value = glink_vma - glink->vma;
   3663 	      s->name = names;
   3664 	      s->udata.p = NULL;
   3665 	      len = strlen ((*p->sym_ptr_ptr)->name);
   3666 	      memcpy (names, (*p->sym_ptr_ptr)->name, len);
   3667 	      names += len;
   3668 	      if (p->addend != 0)
   3669 		{
   3670 		  memcpy (names, "+0x", sizeof ("+0x") - 1);
   3671 		  names += sizeof ("+0x") - 1;
   3672 		  bfd_sprintf_vma (abfd, names, p->addend);
   3673 		  names += strlen (names);
   3674 		}
   3675 	      memcpy (names, "@plt", sizeof ("@plt"));
   3676 	      names += sizeof ("@plt");
   3677 	      s++;
   3678 	      if (abi < 2)
   3679 		{
   3680 		  glink_vma += 8;
   3681 		  if (i >= 0x8000)
   3682 		    glink_vma += 4;
   3683 		}
   3684 	      else
   3685 		glink_vma += 4;
   3686 	    }
   3687 	  count += plt_count;
   3688 	}
   3689     }
   3690 
   3691  done:
   3692   free (syms);
   3693   return count;
   3694 }
   3695 
   3696 /* The following functions are specific to the ELF linker, while
   3698    functions above are used generally.  Those named ppc64_elf_* are
   3699    called by the main ELF linker code.  They appear in this file more
   3700    or less in the order in which they are called.  eg.
   3701    ppc64_elf_check_relocs is called early in the link process,
   3702    ppc64_elf_finish_dynamic_sections is one of the last functions
   3703    called.
   3704 
   3705    PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
   3706    functions have both a function code symbol and a function descriptor
   3707    symbol.  A call to foo in a relocatable object file looks like:
   3708 
   3709    .		.text
   3710    .	x:
   3711    .		bl	.foo
   3712    .		nop
   3713 
   3714    The function definition in another object file might be:
   3715 
   3716    .		.section .opd
   3717    .	foo:	.quad	.foo
   3718    .		.quad	.TOC.@tocbase
   3719    .		.quad	0
   3720    .
   3721    .		.text
   3722    .	.foo:	blr
   3723 
   3724    When the linker resolves the call during a static link, the branch
   3725    unsurprisingly just goes to .foo and the .opd information is unused.
   3726    If the function definition is in a shared library, things are a little
   3727    different:  The call goes via a plt call stub, the opd information gets
   3728    copied to the plt, and the linker patches the nop.
   3729 
   3730    .	x:
   3731    .		bl	.foo_stub
   3732    .		ld	2,40(1)
   3733    .
   3734    .
   3735    .	.foo_stub:
   3736    .		std	2,40(1)			# in practice, the call stub
   3737    .		addis	11,2,Lfoo@toc@ha	# is slightly optimized, but
   3738    .		addi	11,11,Lfoo@toc@l	# this is the general idea
   3739    .		ld	12,0(11)
   3740    .		ld	2,8(11)
   3741    .		mtctr	12
   3742    .		ld	11,16(11)
   3743    .		bctr
   3744    .
   3745    .		.section .plt
   3746    .	Lfoo:	reloc (R_PPC64_JMP_SLOT, foo)
   3747 
   3748    The "reloc ()" notation is supposed to indicate that the linker emits
   3749    an R_PPC64_JMP_SLOT reloc against foo.  The dynamic linker does the opd
   3750    copying.
   3751 
   3752    What are the difficulties here?  Well, firstly, the relocations
   3753    examined by the linker in check_relocs are against the function code
   3754    sym .foo, while the dynamic relocation in the plt is emitted against
   3755    the function descriptor symbol, foo.  Somewhere along the line, we need
   3756    to carefully copy dynamic link information from one symbol to the other.
   3757    Secondly, the generic part of the elf linker will make .foo a dynamic
   3758    symbol as is normal for most other backends.  We need foo dynamic
   3759    instead, at least for an application final link.  However, when
   3760    creating a shared library containing foo, we need to have both symbols
   3761    dynamic so that references to .foo are satisfied during the early
   3762    stages of linking.  Otherwise the linker might decide to pull in a
   3763    definition from some other object, eg. a static library.
   3764 
   3765    Update: As of August 2004, we support a new convention.  Function
   3766    calls may use the function descriptor symbol, ie. "bl foo".  This
   3767    behaves exactly as "bl .foo".  */
   3768 
   3769 /* Of those relocs that might be copied as dynamic relocs, this function
   3770    selects those that must be copied when linking a shared library,
   3771    even when the symbol is local.  */
   3772 
   3773 static int
   3774 must_be_dyn_reloc (struct bfd_link_info *info,
   3775 		   enum elf_ppc64_reloc_type r_type)
   3776 {
   3777   switch (r_type)
   3778     {
   3779     default:
   3780       return 1;
   3781 
   3782     case R_PPC64_REL32:
   3783     case R_PPC64_REL64:
   3784     case R_PPC64_REL30:
   3785       return 0;
   3786 
   3787     case R_PPC64_TPREL16:
   3788     case R_PPC64_TPREL16_LO:
   3789     case R_PPC64_TPREL16_HI:
   3790     case R_PPC64_TPREL16_HA:
   3791     case R_PPC64_TPREL16_DS:
   3792     case R_PPC64_TPREL16_LO_DS:
   3793     case R_PPC64_TPREL16_HIGH:
   3794     case R_PPC64_TPREL16_HIGHA:
   3795     case R_PPC64_TPREL16_HIGHER:
   3796     case R_PPC64_TPREL16_HIGHERA:
   3797     case R_PPC64_TPREL16_HIGHEST:
   3798     case R_PPC64_TPREL16_HIGHESTA:
   3799     case R_PPC64_TPREL64:
   3800       return !bfd_link_executable (info);
   3801     }
   3802 }
   3803 
   3804 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
   3805    copying dynamic variables from a shared lib into an app's dynbss
   3806    section, and instead use a dynamic relocation to point into the
   3807    shared lib.  With code that gcc generates, it's vital that this be
   3808    enabled;  In the PowerPC64 ABI, the address of a function is actually
   3809    the address of a function descriptor, which resides in the .opd
   3810    section.  gcc uses the descriptor directly rather than going via the
   3811    GOT as some other ABI's do, which means that initialized function
   3812    pointers must reference the descriptor.  Thus, a function pointer
   3813    initialized to the address of a function in a shared library will
   3814    either require a copy reloc, or a dynamic reloc.  Using a copy reloc
   3815    redefines the function descriptor symbol to point to the copy.  This
   3816    presents a problem as a plt entry for that function is also
   3817    initialized from the function descriptor symbol and the copy reloc
   3818    may not be initialized first.  */
   3819 #define ELIMINATE_COPY_RELOCS 1
   3820 
   3821 /* Section name for stubs is the associated section name plus this
   3822    string.  */
   3823 #define STUB_SUFFIX ".stub"
   3824 
   3825 /* Linker stubs.
   3826    ppc_stub_long_branch:
   3827    Used when a 14 bit branch (or even a 24 bit branch) can't reach its
   3828    destination, but a 24 bit branch in a stub section will reach.
   3829    .	b	dest
   3830 
   3831    ppc_stub_plt_branch:
   3832    Similar to the above, but a 24 bit branch in the stub section won't
   3833    reach its destination.
   3834    .	addis	%r11,%r2,xxx@toc@ha
   3835    .	ld	%r12,xxx@toc@l(%r11)
   3836    .	mtctr	%r12
   3837    .	bctr
   3838 
   3839    ppc_stub_plt_call:
   3840    Used to call a function in a shared library.  If it so happens that
   3841    the plt entry referenced crosses a 64k boundary, then an extra
   3842    "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
   3843    .	std	%r2,40(%r1)
   3844    .	addis	%r11,%r2,xxx@toc@ha
   3845    .	ld	%r12,xxx+0@toc@l(%r11)
   3846    .	mtctr	%r12
   3847    .	ld	%r2,xxx+8@toc@l(%r11)
   3848    .	ld	%r11,xxx+16@toc@l(%r11)
   3849    .	bctr
   3850 
   3851    ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
   3852    code to adjust the value and save r2 to support multiple toc sections.
   3853    A ppc_stub_long_branch with an r2 offset looks like:
   3854    .	std	%r2,40(%r1)
   3855    .	addis	%r2,%r2,off@ha
   3856    .	addi	%r2,%r2,off@l
   3857    .	b	dest
   3858 
   3859    A ppc_stub_plt_branch with an r2 offset looks like:
   3860    .	std	%r2,40(%r1)
   3861    .	addis	%r11,%r2,xxx@toc@ha
   3862    .	ld	%r12,xxx@toc@l(%r11)
   3863    .	addis	%r2,%r2,off@ha
   3864    .	addi	%r2,%r2,off@l
   3865    .	mtctr	%r12
   3866    .	bctr
   3867 
   3868    In cases where the "addis" instruction would add zero, the "addis" is
   3869    omitted and following instructions modified slightly in some cases.
   3870 */
   3871 
   3872 enum ppc_stub_type {
   3873   ppc_stub_none,
   3874   ppc_stub_long_branch,
   3875   ppc_stub_long_branch_r2off,
   3876   ppc_stub_plt_branch,
   3877   ppc_stub_plt_branch_r2off,
   3878   ppc_stub_plt_call,
   3879   ppc_stub_plt_call_r2save,
   3880   ppc_stub_global_entry,
   3881   ppc_stub_save_res
   3882 };
   3883 
   3884 /* Information on stub grouping.  */
   3885 struct map_stub
   3886 {
   3887   /* The stub section.  */
   3888   asection *stub_sec;
   3889   /* This is the section to which stubs in the group will be attached.  */
   3890   asection *link_sec;
   3891   /* Next group.  */
   3892   struct map_stub *next;
   3893   /* Whether to emit a copy of register save/restore functions in this
   3894      group.  */
   3895   int needs_save_res;
   3896 };
   3897 
   3898 struct ppc_stub_hash_entry {
   3899 
   3900   /* Base hash table entry structure.  */
   3901   struct bfd_hash_entry root;
   3902 
   3903   enum ppc_stub_type stub_type;
   3904 
   3905   /* Group information.  */
   3906   struct map_stub *group;
   3907 
   3908   /* Offset within stub_sec of the beginning of this stub.  */
   3909   bfd_vma stub_offset;
   3910 
   3911   /* Given the symbol's value and its section we can determine its final
   3912      value when building the stubs (so the stub knows where to jump.  */
   3913   bfd_vma target_value;
   3914   asection *target_section;
   3915 
   3916   /* The symbol table entry, if any, that this was derived from.  */
   3917   struct ppc_link_hash_entry *h;
   3918   struct plt_entry *plt_ent;
   3919 
   3920   /* Symbol st_other.  */
   3921   unsigned char other;
   3922 };
   3923 
   3924 struct ppc_branch_hash_entry {
   3925 
   3926   /* Base hash table entry structure.  */
   3927   struct bfd_hash_entry root;
   3928 
   3929   /* Offset within branch lookup table.  */
   3930   unsigned int offset;
   3931 
   3932   /* Generation marker.  */
   3933   unsigned int iter;
   3934 };
   3935 
   3936 /* Used to track dynamic relocations for local symbols.  */
   3937 struct ppc_dyn_relocs
   3938 {
   3939   struct ppc_dyn_relocs *next;
   3940 
   3941   /* The input section of the reloc.  */
   3942   asection *sec;
   3943 
   3944   /* Total number of relocs copied for the input section.  */
   3945   unsigned int count : 31;
   3946 
   3947   /* Whether this entry is for STT_GNU_IFUNC symbols.  */
   3948   unsigned int ifunc : 1;
   3949 };
   3950 
   3951 struct ppc_link_hash_entry
   3952 {
   3953   struct elf_link_hash_entry elf;
   3954 
   3955   union {
   3956     /* A pointer to the most recently used stub hash entry against this
   3957        symbol.  */
   3958     struct ppc_stub_hash_entry *stub_cache;
   3959 
   3960     /* A pointer to the next symbol starting with a '.'  */
   3961     struct ppc_link_hash_entry *next_dot_sym;
   3962   } u;
   3963 
   3964   /* Track dynamic relocs copied for this symbol.  */
   3965   struct elf_dyn_relocs *dyn_relocs;
   3966 
   3967   /* Link between function code and descriptor symbols.  */
   3968   struct ppc_link_hash_entry *oh;
   3969 
   3970   /* Flag function code and descriptor symbols.  */
   3971   unsigned int is_func:1;
   3972   unsigned int is_func_descriptor:1;
   3973   unsigned int fake:1;
   3974 
   3975   /* Whether global opd/toc sym has been adjusted or not.
   3976      After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
   3977      should be set for all globals defined in any opd/toc section.  */
   3978   unsigned int adjust_done:1;
   3979 
   3980   /* Set if we twiddled this symbol to weak at some stage.  */
   3981   unsigned int was_undefined:1;
   3982 
   3983   /* Set if this is an out-of-line register save/restore function,
   3984      with non-standard calling convention.  */
   3985   unsigned int save_res:1;
   3986 
   3987   /* Contexts in which symbol is used in the GOT (or TOC).
   3988      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
   3989      corresponding relocs are encountered during check_relocs.
   3990      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
   3991      indicate the corresponding GOT entry type is not needed.
   3992      tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
   3993      a TPREL one.  We use a separate flag rather than setting TPREL
   3994      just for convenience in distinguishing the two cases.  */
   3995 #define TLS_GD		 1	/* GD reloc. */
   3996 #define TLS_LD		 2	/* LD reloc. */
   3997 #define TLS_TPREL	 4	/* TPREL reloc, => IE. */
   3998 #define TLS_DTPREL	 8	/* DTPREL reloc, => LD. */
   3999 #define TLS_TLS		16	/* Any TLS reloc.  */
   4000 #define TLS_EXPLICIT	32	/* Marks TOC section TLS relocs. */
   4001 #define TLS_TPRELGD	64	/* TPREL reloc resulting from GD->IE. */
   4002 #define PLT_IFUNC      128	/* STT_GNU_IFUNC.  */
   4003   unsigned char tls_mask;
   4004 };
   4005 
   4006 /* ppc64 ELF linker hash table.  */
   4007 
   4008 struct ppc_link_hash_table
   4009 {
   4010   struct elf_link_hash_table elf;
   4011 
   4012   /* The stub hash table.  */
   4013   struct bfd_hash_table stub_hash_table;
   4014 
   4015   /* Another hash table for plt_branch stubs.  */
   4016   struct bfd_hash_table branch_hash_table;
   4017 
   4018   /* Hash table for function prologue tocsave.  */
   4019   htab_t tocsave_htab;
   4020 
   4021   /* Various options and other info passed from the linker.  */
   4022   struct ppc64_elf_params *params;
   4023 
   4024   /* The size of sec_info below.  */
   4025   unsigned int sec_info_arr_size;
   4026 
   4027   /* Per-section array of extra section info.  Done this way rather
   4028      than as part of ppc64_elf_section_data so we have the info for
   4029      non-ppc64 sections.  */
   4030   struct
   4031   {
   4032     /* Along with elf_gp, specifies the TOC pointer used by this section.  */
   4033     bfd_vma toc_off;
   4034 
   4035     union
   4036     {
   4037       /* The section group that this section belongs to.  */
   4038       struct map_stub *group;
   4039       /* A temp section list pointer.  */
   4040       asection *list;
   4041     } u;
   4042   } *sec_info;
   4043 
   4044   /* Linked list of groups.  */
   4045   struct map_stub *group;
   4046 
   4047   /* Temp used when calculating TOC pointers.  */
   4048   bfd_vma toc_curr;
   4049   bfd *toc_bfd;
   4050   asection *toc_first_sec;
   4051 
   4052   /* Used when adding symbols.  */
   4053   struct ppc_link_hash_entry *dot_syms;
   4054 
   4055   /* Shortcuts to get to dynamic linker sections.  */
   4056   asection *dynbss;
   4057   asection *relbss;
   4058   asection *glink;
   4059   asection *sfpr;
   4060   asection *brlt;
   4061   asection *relbrlt;
   4062   asection *glink_eh_frame;
   4063 
   4064   /* Shortcut to .__tls_get_addr and __tls_get_addr.  */
   4065   struct ppc_link_hash_entry *tls_get_addr;
   4066   struct ppc_link_hash_entry *tls_get_addr_fd;
   4067 
   4068   /* The size of reliplt used by got entry relocs.  */
   4069   bfd_size_type got_reli_size;
   4070 
   4071   /* Statistics.  */
   4072   unsigned long stub_count[ppc_stub_global_entry];
   4073 
   4074   /* Number of stubs against global syms.  */
   4075   unsigned long stub_globals;
   4076 
   4077   /* Set if we're linking code with function descriptors.  */
   4078   unsigned int opd_abi:1;
   4079 
   4080   /* Support for multiple toc sections.  */
   4081   unsigned int do_multi_toc:1;
   4082   unsigned int multi_toc_needed:1;
   4083   unsigned int second_toc_pass:1;
   4084   unsigned int do_toc_opt:1;
   4085 
   4086   /* Set on error.  */
   4087   unsigned int stub_error:1;
   4088 
   4089   /* Temp used by ppc64_elf_before_check_relocs.  */
   4090   unsigned int twiddled_syms:1;
   4091 
   4092   /* Incremented every time we size stubs.  */
   4093   unsigned int stub_iteration;
   4094 
   4095   /* Small local sym cache.  */
   4096   struct sym_cache sym_cache;
   4097 };
   4098 
   4099 /* Rename some of the generic section flags to better document how they
   4100    are used here.  */
   4101 
   4102 /* Nonzero if this section has TLS related relocations.  */
   4103 #define has_tls_reloc sec_flg0
   4104 
   4105 /* Nonzero if this section has a call to __tls_get_addr.  */
   4106 #define has_tls_get_addr_call sec_flg1
   4107 
   4108 /* Nonzero if this section has any toc or got relocs.  */
   4109 #define has_toc_reloc sec_flg2
   4110 
   4111 /* Nonzero if this section has a call to another section that uses
   4112    the toc or got.  */
   4113 #define makes_toc_func_call sec_flg3
   4114 
   4115 /* Recursion protection when determining above flag.  */
   4116 #define call_check_in_progress sec_flg4
   4117 #define call_check_done sec_flg5
   4118 
   4119 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
   4120 
   4121 #define ppc_hash_table(p) \
   4122   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
   4123   == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
   4124 
   4125 #define ppc_stub_hash_lookup(table, string, create, copy) \
   4126   ((struct ppc_stub_hash_entry *) \
   4127    bfd_hash_lookup ((table), (string), (create), (copy)))
   4128 
   4129 #define ppc_branch_hash_lookup(table, string, create, copy) \
   4130   ((struct ppc_branch_hash_entry *) \
   4131    bfd_hash_lookup ((table), (string), (create), (copy)))
   4132 
   4133 /* Create an entry in the stub hash table.  */
   4134 
   4135 static struct bfd_hash_entry *
   4136 stub_hash_newfunc (struct bfd_hash_entry *entry,
   4137 		   struct bfd_hash_table *table,
   4138 		   const char *string)
   4139 {
   4140   /* Allocate the structure if it has not already been allocated by a
   4141      subclass.  */
   4142   if (entry == NULL)
   4143     {
   4144       entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
   4145       if (entry == NULL)
   4146 	return entry;
   4147     }
   4148 
   4149   /* Call the allocation method of the superclass.  */
   4150   entry = bfd_hash_newfunc (entry, table, string);
   4151   if (entry != NULL)
   4152     {
   4153       struct ppc_stub_hash_entry *eh;
   4154 
   4155       /* Initialize the local fields.  */
   4156       eh = (struct ppc_stub_hash_entry *) entry;
   4157       eh->stub_type = ppc_stub_none;
   4158       eh->group = NULL;
   4159       eh->stub_offset = 0;
   4160       eh->target_value = 0;
   4161       eh->target_section = NULL;
   4162       eh->h = NULL;
   4163       eh->plt_ent = NULL;
   4164       eh->other = 0;
   4165     }
   4166 
   4167   return entry;
   4168 }
   4169 
   4170 /* Create an entry in the branch hash table.  */
   4171 
   4172 static struct bfd_hash_entry *
   4173 branch_hash_newfunc (struct bfd_hash_entry *entry,
   4174 		     struct bfd_hash_table *table,
   4175 		     const char *string)
   4176 {
   4177   /* Allocate the structure if it has not already been allocated by a
   4178      subclass.  */
   4179   if (entry == NULL)
   4180     {
   4181       entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
   4182       if (entry == NULL)
   4183 	return entry;
   4184     }
   4185 
   4186   /* Call the allocation method of the superclass.  */
   4187   entry = bfd_hash_newfunc (entry, table, string);
   4188   if (entry != NULL)
   4189     {
   4190       struct ppc_branch_hash_entry *eh;
   4191 
   4192       /* Initialize the local fields.  */
   4193       eh = (struct ppc_branch_hash_entry *) entry;
   4194       eh->offset = 0;
   4195       eh->iter = 0;
   4196     }
   4197 
   4198   return entry;
   4199 }
   4200 
   4201 /* Create an entry in a ppc64 ELF linker hash table.  */
   4202 
   4203 static struct bfd_hash_entry *
   4204 link_hash_newfunc (struct bfd_hash_entry *entry,
   4205 		   struct bfd_hash_table *table,
   4206 		   const char *string)
   4207 {
   4208   /* Allocate the structure if it has not already been allocated by a
   4209      subclass.  */
   4210   if (entry == NULL)
   4211     {
   4212       entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
   4213       if (entry == NULL)
   4214 	return entry;
   4215     }
   4216 
   4217   /* Call the allocation method of the superclass.  */
   4218   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   4219   if (entry != NULL)
   4220     {
   4221       struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
   4222 
   4223       memset (&eh->u.stub_cache, 0,
   4224 	      (sizeof (struct ppc_link_hash_entry)
   4225 	       - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
   4226 
   4227       /* When making function calls, old ABI code references function entry
   4228 	 points (dot symbols), while new ABI code references the function
   4229 	 descriptor symbol.  We need to make any combination of reference and
   4230 	 definition work together, without breaking archive linking.
   4231 
   4232 	 For a defined function "foo" and an undefined call to "bar":
   4233 	 An old object defines "foo" and ".foo", references ".bar" (possibly
   4234 	 "bar" too).
   4235 	 A new object defines "foo" and references "bar".
   4236 
   4237 	 A new object thus has no problem with its undefined symbols being
   4238 	 satisfied by definitions in an old object.  On the other hand, the
   4239 	 old object won't have ".bar" satisfied by a new object.
   4240 
   4241 	 Keep a list of newly added dot-symbols.  */
   4242 
   4243       if (string[0] == '.')
   4244 	{
   4245 	  struct ppc_link_hash_table *htab;
   4246 
   4247 	  htab = (struct ppc_link_hash_table *) table;
   4248 	  eh->u.next_dot_sym = htab->dot_syms;
   4249 	  htab->dot_syms = eh;
   4250 	}
   4251     }
   4252 
   4253   return entry;
   4254 }
   4255 
   4256 struct tocsave_entry {
   4257   asection *sec;
   4258   bfd_vma offset;
   4259 };
   4260 
   4261 static hashval_t
   4262 tocsave_htab_hash (const void *p)
   4263 {
   4264   const struct tocsave_entry *e = (const struct tocsave_entry *) p;
   4265   return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
   4266 }
   4267 
   4268 static int
   4269 tocsave_htab_eq (const void *p1, const void *p2)
   4270 {
   4271   const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
   4272   const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
   4273   return e1->sec == e2->sec && e1->offset == e2->offset;
   4274 }
   4275 
   4276 /* Destroy a ppc64 ELF linker hash table.  */
   4277 
   4278 static void
   4279 ppc64_elf_link_hash_table_free (bfd *obfd)
   4280 {
   4281   struct ppc_link_hash_table *htab;
   4282 
   4283   htab = (struct ppc_link_hash_table *) obfd->link.hash;
   4284   if (htab->tocsave_htab)
   4285     htab_delete (htab->tocsave_htab);
   4286   bfd_hash_table_free (&htab->branch_hash_table);
   4287   bfd_hash_table_free (&htab->stub_hash_table);
   4288   _bfd_elf_link_hash_table_free (obfd);
   4289 }
   4290 
   4291 /* Create a ppc64 ELF linker hash table.  */
   4292 
   4293 static struct bfd_link_hash_table *
   4294 ppc64_elf_link_hash_table_create (bfd *abfd)
   4295 {
   4296   struct ppc_link_hash_table *htab;
   4297   bfd_size_type amt = sizeof (struct ppc_link_hash_table);
   4298 
   4299   htab = bfd_zmalloc (amt);
   4300   if (htab == NULL)
   4301     return NULL;
   4302 
   4303   if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
   4304 				      sizeof (struct ppc_link_hash_entry),
   4305 				      PPC64_ELF_DATA))
   4306     {
   4307       free (htab);
   4308       return NULL;
   4309     }
   4310 
   4311   /* Init the stub hash table too.  */
   4312   if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
   4313 			    sizeof (struct ppc_stub_hash_entry)))
   4314     {
   4315       _bfd_elf_link_hash_table_free (abfd);
   4316       return NULL;
   4317     }
   4318 
   4319   /* And the branch hash table.  */
   4320   if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
   4321 			    sizeof (struct ppc_branch_hash_entry)))
   4322     {
   4323       bfd_hash_table_free (&htab->stub_hash_table);
   4324       _bfd_elf_link_hash_table_free (abfd);
   4325       return NULL;
   4326     }
   4327 
   4328   htab->tocsave_htab = htab_try_create (1024,
   4329 					tocsave_htab_hash,
   4330 					tocsave_htab_eq,
   4331 					NULL);
   4332   if (htab->tocsave_htab == NULL)
   4333     {
   4334       ppc64_elf_link_hash_table_free (abfd);
   4335       return NULL;
   4336     }
   4337   htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
   4338 
   4339   /* Initializing two fields of the union is just cosmetic.  We really
   4340      only care about glist, but when compiled on a 32-bit host the
   4341      bfd_vma fields are larger.  Setting the bfd_vma to zero makes
   4342      debugger inspection of these fields look nicer.  */
   4343   htab->elf.init_got_refcount.refcount = 0;
   4344   htab->elf.init_got_refcount.glist = NULL;
   4345   htab->elf.init_plt_refcount.refcount = 0;
   4346   htab->elf.init_plt_refcount.glist = NULL;
   4347   htab->elf.init_got_offset.offset = 0;
   4348   htab->elf.init_got_offset.glist = NULL;
   4349   htab->elf.init_plt_offset.offset = 0;
   4350   htab->elf.init_plt_offset.glist = NULL;
   4351 
   4352   return &htab->elf.root;
   4353 }
   4354 
   4355 /* Create sections for linker generated code.  */
   4356 
   4357 static bfd_boolean
   4358 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
   4359 {
   4360   struct ppc_link_hash_table *htab;
   4361   flagword flags;
   4362 
   4363   htab = ppc_hash_table (info);
   4364 
   4365   flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   4366 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4367   if (htab->params->save_restore_funcs)
   4368     {
   4369       /* Create .sfpr for code to save and restore fp regs.  */
   4370       htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
   4371 						       flags);
   4372       if (htab->sfpr == NULL
   4373 	  || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
   4374 	return FALSE;
   4375     }
   4376 
   4377   if (bfd_link_relocatable (info))
   4378     return TRUE;
   4379 
   4380   /* Create .glink for lazy dynamic linking support.  */
   4381   htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
   4382 						    flags);
   4383   if (htab->glink == NULL
   4384       || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
   4385     return FALSE;
   4386 
   4387   if (!info->no_ld_generated_unwind_info)
   4388     {
   4389       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
   4390 	       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4391       htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
   4392 								 ".eh_frame",
   4393 								 flags);
   4394       if (htab->glink_eh_frame == NULL
   4395 	  || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
   4396 	return FALSE;
   4397     }
   4398 
   4399   flags = SEC_ALLOC | SEC_LINKER_CREATED;
   4400   htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
   4401   if (htab->elf.iplt == NULL
   4402       || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
   4403     return FALSE;
   4404 
   4405   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4406 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4407   htab->elf.irelplt
   4408     = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
   4409   if (htab->elf.irelplt == NULL
   4410       || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
   4411     return FALSE;
   4412 
   4413   /* Create branch lookup table for plt_branch stubs.  */
   4414   flags = (SEC_ALLOC | SEC_LOAD
   4415 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4416   htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
   4417 						   flags);
   4418   if (htab->brlt == NULL
   4419       || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
   4420     return FALSE;
   4421 
   4422   if (!bfd_link_pic (info))
   4423     return TRUE;
   4424 
   4425   flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
   4426 	   | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   4427   htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
   4428 						      ".rela.branch_lt",
   4429 						      flags);
   4430   if (htab->relbrlt == NULL
   4431       || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
   4432     return FALSE;
   4433 
   4434   return TRUE;
   4435 }
   4436 
   4437 /* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
   4438 
   4439 bfd_boolean
   4440 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
   4441 			 struct ppc64_elf_params *params)
   4442 {
   4443   struct ppc_link_hash_table *htab;
   4444 
   4445   elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
   4446 
   4447 /* Always hook our dynamic sections into the first bfd, which is the
   4448    linker created stub bfd.  This ensures that the GOT header is at
   4449    the start of the output TOC section.  */
   4450   htab = ppc_hash_table (info);
   4451   htab->elf.dynobj = params->stub_bfd;
   4452   htab->params = params;
   4453 
   4454   return create_linkage_sections (htab->elf.dynobj, info);
   4455 }
   4456 
   4457 /* Build a name for an entry in the stub hash table.  */
   4458 
   4459 static char *
   4460 ppc_stub_name (const asection *input_section,
   4461 	       const asection *sym_sec,
   4462 	       const struct ppc_link_hash_entry *h,
   4463 	       const Elf_Internal_Rela *rel)
   4464 {
   4465   char *stub_name;
   4466   ssize_t len;
   4467 
   4468   /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
   4469      offsets from a sym as a branch target?  In fact, we could
   4470      probably assume the addend is always zero.  */
   4471   BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
   4472 
   4473   if (h)
   4474     {
   4475       len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
   4476       stub_name = bfd_malloc (len);
   4477       if (stub_name == NULL)
   4478 	return stub_name;
   4479 
   4480       len = sprintf (stub_name, "%08x.%s+%x",
   4481 		     input_section->id & 0xffffffff,
   4482 		     h->elf.root.root.string,
   4483 		     (int) rel->r_addend & 0xffffffff);
   4484     }
   4485   else
   4486     {
   4487       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
   4488       stub_name = bfd_malloc (len);
   4489       if (stub_name == NULL)
   4490 	return stub_name;
   4491 
   4492       len = sprintf (stub_name, "%08x.%x:%x+%x",
   4493 		     input_section->id & 0xffffffff,
   4494 		     sym_sec->id & 0xffffffff,
   4495 		     (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
   4496 		     (int) rel->r_addend & 0xffffffff);
   4497     }
   4498   if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
   4499     stub_name[len - 2] = 0;
   4500   return stub_name;
   4501 }
   4502 
   4503 /* Look up an entry in the stub hash.  Stub entries are cached because
   4504    creating the stub name takes a bit of time.  */
   4505 
   4506 static struct ppc_stub_hash_entry *
   4507 ppc_get_stub_entry (const asection *input_section,
   4508 		    const asection *sym_sec,
   4509 		    struct ppc_link_hash_entry *h,
   4510 		    const Elf_Internal_Rela *rel,
   4511 		    struct ppc_link_hash_table *htab)
   4512 {
   4513   struct ppc_stub_hash_entry *stub_entry;
   4514   struct map_stub *group;
   4515 
   4516   /* If this input section is part of a group of sections sharing one
   4517      stub section, then use the id of the first section in the group.
   4518      Stub names need to include a section id, as there may well be
   4519      more than one stub used to reach say, printf, and we need to
   4520      distinguish between them.  */
   4521   group = htab->sec_info[input_section->id].u.group;
   4522   if (group == NULL)
   4523     return NULL;
   4524 
   4525   if (h != NULL && h->u.stub_cache != NULL
   4526       && h->u.stub_cache->h == h
   4527       && h->u.stub_cache->group == group)
   4528     {
   4529       stub_entry = h->u.stub_cache;
   4530     }
   4531   else
   4532     {
   4533       char *stub_name;
   4534 
   4535       stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
   4536       if (stub_name == NULL)
   4537 	return NULL;
   4538 
   4539       stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   4540 					 stub_name, FALSE, FALSE);
   4541       if (h != NULL)
   4542 	h->u.stub_cache = stub_entry;
   4543 
   4544       free (stub_name);
   4545     }
   4546 
   4547   return stub_entry;
   4548 }
   4549 
   4550 /* Add a new stub entry to the stub hash.  Not all fields of the new
   4551    stub entry are initialised.  */
   4552 
   4553 static struct ppc_stub_hash_entry *
   4554 ppc_add_stub (const char *stub_name,
   4555 	      asection *section,
   4556 	      struct bfd_link_info *info)
   4557 {
   4558   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4559   struct map_stub *group;
   4560   asection *link_sec;
   4561   asection *stub_sec;
   4562   struct ppc_stub_hash_entry *stub_entry;
   4563 
   4564   group = htab->sec_info[section->id].u.group;
   4565   link_sec = group->link_sec;
   4566   stub_sec = group->stub_sec;
   4567   if (stub_sec == NULL)
   4568     {
   4569       size_t namelen;
   4570       bfd_size_type len;
   4571       char *s_name;
   4572 
   4573       namelen = strlen (link_sec->name);
   4574       len = namelen + sizeof (STUB_SUFFIX);
   4575       s_name = bfd_alloc (htab->params->stub_bfd, len);
   4576       if (s_name == NULL)
   4577 	return NULL;
   4578 
   4579       memcpy (s_name, link_sec->name, namelen);
   4580       memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
   4581       stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
   4582       if (stub_sec == NULL)
   4583 	return NULL;
   4584       group->stub_sec = stub_sec;
   4585     }
   4586 
   4587   /* Enter this entry into the linker stub hash table.  */
   4588   stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
   4589 				     TRUE, FALSE);
   4590   if (stub_entry == NULL)
   4591     {
   4592       info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
   4593 			      section->owner, stub_name);
   4594       return NULL;
   4595     }
   4596 
   4597   stub_entry->group = group;
   4598   stub_entry->stub_offset = 0;
   4599   return stub_entry;
   4600 }
   4601 
   4602 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
   4603    not already done.  */
   4604 
   4605 static bfd_boolean
   4606 create_got_section (bfd *abfd, struct bfd_link_info *info)
   4607 {
   4608   asection *got, *relgot;
   4609   flagword flags;
   4610   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4611 
   4612   if (!is_ppc64_elf (abfd))
   4613     return FALSE;
   4614   if (htab == NULL)
   4615     return FALSE;
   4616 
   4617   if (!htab->elf.sgot
   4618       && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
   4619     return FALSE;
   4620 
   4621   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   4622 	   | SEC_LINKER_CREATED);
   4623 
   4624   got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
   4625   if (!got
   4626       || !bfd_set_section_alignment (abfd, got, 3))
   4627     return FALSE;
   4628 
   4629   relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
   4630 					       flags | SEC_READONLY);
   4631   if (!relgot
   4632       || ! bfd_set_section_alignment (abfd, relgot, 3))
   4633     return FALSE;
   4634 
   4635   ppc64_elf_tdata (abfd)->got = got;
   4636   ppc64_elf_tdata (abfd)->relgot = relgot;
   4637   return TRUE;
   4638 }
   4639 
   4640 /* Create the dynamic sections, and set up shortcuts.  */
   4641 
   4642 static bfd_boolean
   4643 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
   4644 {
   4645   struct ppc_link_hash_table *htab;
   4646 
   4647   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
   4648     return FALSE;
   4649 
   4650   htab = ppc_hash_table (info);
   4651   if (htab == NULL)
   4652     return FALSE;
   4653 
   4654   htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
   4655   if (!bfd_link_pic (info))
   4656     htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
   4657 
   4658   if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
   4659       || (!bfd_link_pic (info) && !htab->relbss))
   4660     abort ();
   4661 
   4662   return TRUE;
   4663 }
   4664 
   4665 /* Follow indirect and warning symbol links.  */
   4666 
   4667 static inline struct bfd_link_hash_entry *
   4668 follow_link (struct bfd_link_hash_entry *h)
   4669 {
   4670   while (h->type == bfd_link_hash_indirect
   4671 	 || h->type == bfd_link_hash_warning)
   4672     h = h->u.i.link;
   4673   return h;
   4674 }
   4675 
   4676 static inline struct elf_link_hash_entry *
   4677 elf_follow_link (struct elf_link_hash_entry *h)
   4678 {
   4679   return (struct elf_link_hash_entry *) follow_link (&h->root);
   4680 }
   4681 
   4682 static inline struct ppc_link_hash_entry *
   4683 ppc_follow_link (struct ppc_link_hash_entry *h)
   4684 {
   4685   return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
   4686 }
   4687 
   4688 /* Merge PLT info on FROM with that on TO.  */
   4689 
   4690 static void
   4691 move_plt_plist (struct ppc_link_hash_entry *from,
   4692 		struct ppc_link_hash_entry *to)
   4693 {
   4694   if (from->elf.plt.plist != NULL)
   4695     {
   4696       if (to->elf.plt.plist != NULL)
   4697 	{
   4698 	  struct plt_entry **entp;
   4699 	  struct plt_entry *ent;
   4700 
   4701 	  for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
   4702 	    {
   4703 	      struct plt_entry *dent;
   4704 
   4705 	      for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
   4706 		if (dent->addend == ent->addend)
   4707 		  {
   4708 		    dent->plt.refcount += ent->plt.refcount;
   4709 		    *entp = ent->next;
   4710 		    break;
   4711 		  }
   4712 	      if (dent == NULL)
   4713 		entp = &ent->next;
   4714 	    }
   4715 	  *entp = to->elf.plt.plist;
   4716 	}
   4717 
   4718       to->elf.plt.plist = from->elf.plt.plist;
   4719       from->elf.plt.plist = NULL;
   4720     }
   4721 }
   4722 
   4723 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   4724 
   4725 static void
   4726 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
   4727 				struct elf_link_hash_entry *dir,
   4728 				struct elf_link_hash_entry *ind)
   4729 {
   4730   struct ppc_link_hash_entry *edir, *eind;
   4731 
   4732   edir = (struct ppc_link_hash_entry *) dir;
   4733   eind = (struct ppc_link_hash_entry *) ind;
   4734 
   4735   edir->is_func |= eind->is_func;
   4736   edir->is_func_descriptor |= eind->is_func_descriptor;
   4737   edir->tls_mask |= eind->tls_mask;
   4738   if (eind->oh != NULL)
   4739     edir->oh = ppc_follow_link (eind->oh);
   4740 
   4741   /* If called to transfer flags for a weakdef during processing
   4742      of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
   4743      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
   4744   if (!(ELIMINATE_COPY_RELOCS
   4745 	&& eind->elf.root.type != bfd_link_hash_indirect
   4746 	&& edir->elf.dynamic_adjusted))
   4747     edir->elf.non_got_ref |= eind->elf.non_got_ref;
   4748 
   4749   edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   4750   edir->elf.ref_regular |= eind->elf.ref_regular;
   4751   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   4752   edir->elf.needs_plt |= eind->elf.needs_plt;
   4753   edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
   4754 
   4755   /* Copy over any dynamic relocs we may have on the indirect sym.  */
   4756   if (eind->dyn_relocs != NULL)
   4757     {
   4758       if (edir->dyn_relocs != NULL)
   4759 	{
   4760 	  struct elf_dyn_relocs **pp;
   4761 	  struct elf_dyn_relocs *p;
   4762 
   4763 	  /* Add reloc counts against the indirect sym to the direct sym
   4764 	     list.  Merge any entries against the same section.  */
   4765 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
   4766 	    {
   4767 	      struct elf_dyn_relocs *q;
   4768 
   4769 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   4770 		if (q->sec == p->sec)
   4771 		  {
   4772 		    q->pc_count += p->pc_count;
   4773 		    q->count += p->count;
   4774 		    *pp = p->next;
   4775 		    break;
   4776 		  }
   4777 	      if (q == NULL)
   4778 		pp = &p->next;
   4779 	    }
   4780 	  *pp = edir->dyn_relocs;
   4781 	}
   4782 
   4783       edir->dyn_relocs = eind->dyn_relocs;
   4784       eind->dyn_relocs = NULL;
   4785     }
   4786 
   4787   /* If we were called to copy over info for a weak sym, that's all.
   4788      You might think dyn_relocs need not be copied over;  After all,
   4789      both syms will be dynamic or both non-dynamic so we're just
   4790      moving reloc accounting around.  However, ELIMINATE_COPY_RELOCS
   4791      code in ppc64_elf_adjust_dynamic_symbol needs to check for
   4792      dyn_relocs in read-only sections, and it does so on what is the
   4793      DIR sym here.  */
   4794   if (eind->elf.root.type != bfd_link_hash_indirect)
   4795     return;
   4796 
   4797   /* Copy over got entries that we may have already seen to the
   4798      symbol which just became indirect.  */
   4799   if (eind->elf.got.glist != NULL)
   4800     {
   4801       if (edir->elf.got.glist != NULL)
   4802 	{
   4803 	  struct got_entry **entp;
   4804 	  struct got_entry *ent;
   4805 
   4806 	  for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
   4807 	    {
   4808 	      struct got_entry *dent;
   4809 
   4810 	      for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
   4811 		if (dent->addend == ent->addend
   4812 		    && dent->owner == ent->owner
   4813 		    && dent->tls_type == ent->tls_type)
   4814 		  {
   4815 		    dent->got.refcount += ent->got.refcount;
   4816 		    *entp = ent->next;
   4817 		    break;
   4818 		  }
   4819 	      if (dent == NULL)
   4820 		entp = &ent->next;
   4821 	    }
   4822 	  *entp = edir->elf.got.glist;
   4823 	}
   4824 
   4825       edir->elf.got.glist = eind->elf.got.glist;
   4826       eind->elf.got.glist = NULL;
   4827     }
   4828 
   4829   /* And plt entries.  */
   4830   move_plt_plist (eind, edir);
   4831 
   4832   if (eind->elf.dynindx != -1)
   4833     {
   4834       if (edir->elf.dynindx != -1)
   4835 	_bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   4836 				edir->elf.dynstr_index);
   4837       edir->elf.dynindx = eind->elf.dynindx;
   4838       edir->elf.dynstr_index = eind->elf.dynstr_index;
   4839       eind->elf.dynindx = -1;
   4840       eind->elf.dynstr_index = 0;
   4841     }
   4842 }
   4843 
   4844 /* Find the function descriptor hash entry from the given function code
   4845    hash entry FH.  Link the entries via their OH fields.  */
   4846 
   4847 static struct ppc_link_hash_entry *
   4848 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
   4849 {
   4850   struct ppc_link_hash_entry *fdh = fh->oh;
   4851 
   4852   if (fdh == NULL)
   4853     {
   4854       const char *fd_name = fh->elf.root.root.string + 1;
   4855 
   4856       fdh = (struct ppc_link_hash_entry *)
   4857 	elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
   4858       if (fdh == NULL)
   4859 	return fdh;
   4860 
   4861       fdh->is_func_descriptor = 1;
   4862       fdh->oh = fh;
   4863       fh->is_func = 1;
   4864       fh->oh = fdh;
   4865     }
   4866 
   4867   return ppc_follow_link (fdh);
   4868 }
   4869 
   4870 /* Make a fake function descriptor sym for the code sym FH.  */
   4871 
   4872 static struct ppc_link_hash_entry *
   4873 make_fdh (struct bfd_link_info *info,
   4874 	  struct ppc_link_hash_entry *fh)
   4875 {
   4876   bfd *abfd;
   4877   asymbol *newsym;
   4878   struct bfd_link_hash_entry *bh;
   4879   struct ppc_link_hash_entry *fdh;
   4880 
   4881   abfd = fh->elf.root.u.undef.abfd;
   4882   newsym = bfd_make_empty_symbol (abfd);
   4883   newsym->name = fh->elf.root.root.string + 1;
   4884   newsym->section = bfd_und_section_ptr;
   4885   newsym->value = 0;
   4886   newsym->flags = BSF_WEAK;
   4887 
   4888   bh = NULL;
   4889   if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
   4890 					 newsym->flags, newsym->section,
   4891 					 newsym->value, NULL, FALSE, FALSE,
   4892 					 &bh))
   4893     return NULL;
   4894 
   4895   fdh = (struct ppc_link_hash_entry *) bh;
   4896   fdh->elf.non_elf = 0;
   4897   fdh->fake = 1;
   4898   fdh->is_func_descriptor = 1;
   4899   fdh->oh = fh;
   4900   fh->is_func = 1;
   4901   fh->oh = fdh;
   4902   return fdh;
   4903 }
   4904 
   4905 /* Fix function descriptor symbols defined in .opd sections to be
   4906    function type.  */
   4907 
   4908 static bfd_boolean
   4909 ppc64_elf_add_symbol_hook (bfd *ibfd,
   4910 			   struct bfd_link_info *info,
   4911 			   Elf_Internal_Sym *isym,
   4912 			   const char **name,
   4913 			   flagword *flags ATTRIBUTE_UNUSED,
   4914 			   asection **sec,
   4915 			   bfd_vma *value)
   4916 {
   4917   if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
   4918       && (ibfd->flags & DYNAMIC) == 0
   4919       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
   4920     elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
   4921 
   4922   if (*sec != NULL
   4923       && strcmp ((*sec)->name, ".opd") == 0)
   4924     {
   4925       asection *code_sec;
   4926 
   4927       if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
   4928 	    || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
   4929 	isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
   4930 
   4931       /* If the symbol is a function defined in .opd, and the function
   4932 	 code is in a discarded group, let it appear to be undefined.  */
   4933       if (!bfd_link_relocatable (info)
   4934 	  && (*sec)->reloc_count != 0
   4935 	  && opd_entry_value (*sec, *value, &code_sec, NULL,
   4936 			      FALSE) != (bfd_vma) -1
   4937 	  && discarded_section (code_sec))
   4938 	{
   4939 	  *sec = bfd_und_section_ptr;
   4940 	  isym->st_shndx = SHN_UNDEF;
   4941 	}
   4942     }
   4943   else if (*sec != NULL
   4944 	   && strcmp ((*sec)->name, ".toc") == 0
   4945 	   && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
   4946     {
   4947       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   4948       if (htab != NULL)
   4949 	htab->params->object_in_toc = 1;
   4950     }
   4951 
   4952   if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
   4953     {
   4954       if (abiversion (ibfd) == 0)
   4955 	set_abiversion (ibfd, 2);
   4956       else if (abiversion (ibfd) == 1)
   4957 	{
   4958 	  info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
   4959 				    " for ABI version 1\n"), name);
   4960 	  bfd_set_error (bfd_error_bad_value);
   4961 	  return FALSE;
   4962 	}
   4963     }
   4964 
   4965   return TRUE;
   4966 }
   4967 
   4968 /* Merge non-visibility st_other attributes: local entry point.  */
   4969 
   4970 static void
   4971 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
   4972 				  const Elf_Internal_Sym *isym,
   4973 				  bfd_boolean definition,
   4974 				  bfd_boolean dynamic)
   4975 {
   4976   if (definition && !dynamic)
   4977     h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
   4978 		| ELF_ST_VISIBILITY (h->other));
   4979 }
   4980 
   4981 /* This function makes an old ABI object reference to ".bar" cause the
   4982    inclusion of a new ABI object archive that defines "bar".
   4983    NAME is a symbol defined in an archive.  Return a symbol in the hash
   4984    table that might be satisfied by the archive symbols.  */
   4985 
   4986 static struct elf_link_hash_entry *
   4987 ppc64_elf_archive_symbol_lookup (bfd *abfd,
   4988 				 struct bfd_link_info *info,
   4989 				 const char *name)
   4990 {
   4991   struct elf_link_hash_entry *h;
   4992   char *dot_name;
   4993   size_t len;
   4994 
   4995   h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
   4996   if (h != NULL
   4997       /* Don't return this sym if it is a fake function descriptor
   4998 	 created by add_symbol_adjust.  */
   4999       && !(h->root.type == bfd_link_hash_undefweak
   5000 	   && ((struct ppc_link_hash_entry *) h)->fake))
   5001     return h;
   5002 
   5003   if (name[0] == '.')
   5004     return h;
   5005 
   5006   len = strlen (name);
   5007   dot_name = bfd_alloc (abfd, len + 2);
   5008   if (dot_name == NULL)
   5009     return (struct elf_link_hash_entry *) 0 - 1;
   5010   dot_name[0] = '.';
   5011   memcpy (dot_name + 1, name, len + 1);
   5012   h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
   5013   bfd_release (abfd, dot_name);
   5014   return h;
   5015 }
   5016 
   5017 /* This function satisfies all old ABI object references to ".bar" if a
   5018    new ABI object defines "bar".  Well, at least, undefined dot symbols
   5019    are made weak.  This stops later archive searches from including an
   5020    object if we already have a function descriptor definition.  It also
   5021    prevents the linker complaining about undefined symbols.
   5022    We also check and correct mismatched symbol visibility here.  The
   5023    most restrictive visibility of the function descriptor and the
   5024    function entry symbol is used.  */
   5025 
   5026 static bfd_boolean
   5027 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
   5028 {
   5029   struct ppc_link_hash_table *htab;
   5030   struct ppc_link_hash_entry *fdh;
   5031 
   5032   if (eh->elf.root.type == bfd_link_hash_indirect)
   5033     return TRUE;
   5034 
   5035   if (eh->elf.root.type == bfd_link_hash_warning)
   5036     eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
   5037 
   5038   if (eh->elf.root.root.string[0] != '.')
   5039     abort ();
   5040 
   5041   htab = ppc_hash_table (info);
   5042   if (htab == NULL)
   5043     return FALSE;
   5044 
   5045   fdh = lookup_fdh (eh, htab);
   5046   if (fdh == NULL)
   5047     {
   5048       if (!bfd_link_relocatable (info)
   5049 	  && (eh->elf.root.type == bfd_link_hash_undefined
   5050 	      || eh->elf.root.type == bfd_link_hash_undefweak)
   5051 	  && eh->elf.ref_regular)
   5052 	{
   5053 	  /* Make an undefweak function descriptor sym, which is enough to
   5054 	     pull in an --as-needed shared lib, but won't cause link
   5055 	     errors.  Archives are handled elsewhere.  */
   5056 	  fdh = make_fdh (info, eh);
   5057 	  if (fdh == NULL)
   5058 	    return FALSE;
   5059 	  fdh->elf.ref_regular = 1;
   5060 	}
   5061     }
   5062   else
   5063     {
   5064       unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
   5065       unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
   5066       if (entry_vis < descr_vis)
   5067 	fdh->elf.other += entry_vis - descr_vis;
   5068       else if (entry_vis > descr_vis)
   5069 	eh->elf.other += descr_vis - entry_vis;
   5070 
   5071       if ((fdh->elf.root.type == bfd_link_hash_defined
   5072 	   || fdh->elf.root.type == bfd_link_hash_defweak)
   5073 	  && eh->elf.root.type == bfd_link_hash_undefined)
   5074 	{
   5075 	  eh->elf.root.type = bfd_link_hash_undefweak;
   5076 	  eh->was_undefined = 1;
   5077 	  htab->twiddled_syms = 1;
   5078 	}
   5079     }
   5080 
   5081   return TRUE;
   5082 }
   5083 
   5084 /* Set up opd section info and abiversion for IBFD, and process list
   5085    of dot-symbols we made in link_hash_newfunc.  */
   5086 
   5087 static bfd_boolean
   5088 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
   5089 {
   5090   struct ppc_link_hash_table *htab;
   5091   struct ppc_link_hash_entry **p, *eh;
   5092   asection *opd = bfd_get_section_by_name (ibfd, ".opd");
   5093 
   5094   if (opd != NULL && opd->size != 0)
   5095     {
   5096       if (abiversion (ibfd) == 0)
   5097 	set_abiversion (ibfd, 1);
   5098       else if (abiversion (ibfd) >= 2)
   5099 	{
   5100 	  info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
   5101 				    " version %d\n"),
   5102 				  ibfd, abiversion (ibfd));
   5103 	  bfd_set_error (bfd_error_bad_value);
   5104 	  return FALSE;
   5105 	}
   5106 
   5107       if ((ibfd->flags & DYNAMIC) == 0
   5108 	  && (opd->flags & SEC_RELOC) != 0
   5109 	  && opd->reloc_count != 0
   5110 	  && !bfd_is_abs_section (opd->output_section))
   5111 	{
   5112 	  /* Garbage collection needs some extra help with .opd sections.
   5113 	     We don't want to necessarily keep everything referenced by
   5114 	     relocs in .opd, as that would keep all functions.  Instead,
   5115 	     if we reference an .opd symbol (a function descriptor), we
   5116 	     want to keep the function code symbol's section.  This is
   5117 	     easy for global symbols, but for local syms we need to keep
   5118 	     information about the associated function section.  */
   5119 	  bfd_size_type amt;
   5120 	  asection **opd_sym_map;
   5121 
   5122 	  amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
   5123 	  opd_sym_map = bfd_zalloc (ibfd, amt);
   5124 	  if (opd_sym_map == NULL)
   5125 	    return FALSE;
   5126 	  ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
   5127 	  BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
   5128 	  ppc64_elf_section_data (opd)->sec_type = sec_opd;
   5129 	}
   5130     }
   5131 
   5132   if (!is_ppc64_elf (info->output_bfd))
   5133     return TRUE;
   5134   htab = ppc_hash_table (info);
   5135   if (htab == NULL)
   5136     return FALSE;
   5137 
   5138   /* For input files without an explicit abiversion in e_flags
   5139      we should have flagged any with symbol st_other bits set
   5140      as ELFv1 and above flagged those with .opd as ELFv2.
   5141      Set the output abiversion if not yet set, and for any input
   5142      still ambiguous, take its abiversion from the output.
   5143      Differences in ABI are reported later.  */
   5144   if (abiversion (info->output_bfd) == 0)
   5145     set_abiversion (info->output_bfd, abiversion (ibfd));
   5146   else if (abiversion (ibfd) == 0)
   5147     set_abiversion (ibfd, abiversion (info->output_bfd));
   5148 
   5149   p = &htab->dot_syms;
   5150   while ((eh = *p) != NULL)
   5151     {
   5152       *p = NULL;
   5153       if (&eh->elf == htab->elf.hgot)
   5154 	;
   5155       else if (htab->elf.hgot == NULL
   5156 	       && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
   5157 	htab->elf.hgot = &eh->elf;
   5158       else if (!add_symbol_adjust (eh, info))
   5159 	return FALSE;
   5160       p = &eh->u.next_dot_sym;
   5161     }
   5162 
   5163   /* Clear the list for non-ppc64 input files.  */
   5164   p = &htab->dot_syms;
   5165   while ((eh = *p) != NULL)
   5166     {
   5167       *p = NULL;
   5168       p = &eh->u.next_dot_sym;
   5169     }
   5170 
   5171   /* We need to fix the undefs list for any syms we have twiddled to
   5172      undefweak.  */
   5173   if (htab->twiddled_syms)
   5174     {
   5175       bfd_link_repair_undef_list (&htab->elf.root);
   5176       htab->twiddled_syms = 0;
   5177     }
   5178   return TRUE;
   5179 }
   5180 
   5181 /* Undo hash table changes when an --as-needed input file is determined
   5182    not to be needed.  */
   5183 
   5184 static bfd_boolean
   5185 ppc64_elf_notice_as_needed (bfd *ibfd,
   5186 			    struct bfd_link_info *info,
   5187 			    enum notice_asneeded_action act)
   5188 {
   5189   if (act == notice_not_needed)
   5190     {
   5191       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   5192 
   5193       if (htab == NULL)
   5194 	return FALSE;
   5195 
   5196       htab->dot_syms = NULL;
   5197     }
   5198   return _bfd_elf_notice_as_needed (ibfd, info, act);
   5199 }
   5200 
   5201 /* If --just-symbols against a final linked binary, then assume we need
   5202    toc adjusting stubs when calling functions defined there.  */
   5203 
   5204 static void
   5205 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
   5206 {
   5207   if ((sec->flags & SEC_CODE) != 0
   5208       && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
   5209       && is_ppc64_elf (sec->owner))
   5210     {
   5211       if (abiversion (sec->owner) >= 2
   5212 	  || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
   5213 	sec->has_toc_reloc = 1;
   5214     }
   5215   _bfd_elf_link_just_syms (sec, info);
   5216 }
   5217 
   5218 static struct plt_entry **
   5219 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
   5220 		       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
   5221 {
   5222   struct got_entry **local_got_ents = elf_local_got_ents (abfd);
   5223   struct plt_entry **local_plt;
   5224   unsigned char *local_got_tls_masks;
   5225 
   5226   if (local_got_ents == NULL)
   5227     {
   5228       bfd_size_type size = symtab_hdr->sh_info;
   5229 
   5230       size *= (sizeof (*local_got_ents)
   5231 	       + sizeof (*local_plt)
   5232 	       + sizeof (*local_got_tls_masks));
   5233       local_got_ents = bfd_zalloc (abfd, size);
   5234       if (local_got_ents == NULL)
   5235 	return NULL;
   5236       elf_local_got_ents (abfd) = local_got_ents;
   5237     }
   5238 
   5239   if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
   5240     {
   5241       struct got_entry *ent;
   5242 
   5243       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
   5244 	if (ent->addend == r_addend
   5245 	    && ent->owner == abfd
   5246 	    && ent->tls_type == tls_type)
   5247 	  break;
   5248       if (ent == NULL)
   5249 	{
   5250 	  bfd_size_type amt = sizeof (*ent);
   5251 	  ent = bfd_alloc (abfd, amt);
   5252 	  if (ent == NULL)
   5253 	    return FALSE;
   5254 	  ent->next = local_got_ents[r_symndx];
   5255 	  ent->addend = r_addend;
   5256 	  ent->owner = abfd;
   5257 	  ent->tls_type = tls_type;
   5258 	  ent->is_indirect = FALSE;
   5259 	  ent->got.refcount = 0;
   5260 	  local_got_ents[r_symndx] = ent;
   5261 	}
   5262       ent->got.refcount += 1;
   5263     }
   5264 
   5265   local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
   5266   local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
   5267   local_got_tls_masks[r_symndx] |= tls_type;
   5268 
   5269   return local_plt + r_symndx;
   5270 }
   5271 
   5272 static bfd_boolean
   5273 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
   5274 {
   5275   struct plt_entry *ent;
   5276 
   5277   for (ent = *plist; ent != NULL; ent = ent->next)
   5278     if (ent->addend == addend)
   5279       break;
   5280   if (ent == NULL)
   5281     {
   5282       bfd_size_type amt = sizeof (*ent);
   5283       ent = bfd_alloc (abfd, amt);
   5284       if (ent == NULL)
   5285 	return FALSE;
   5286       ent->next = *plist;
   5287       ent->addend = addend;
   5288       ent->plt.refcount = 0;
   5289       *plist = ent;
   5290     }
   5291   ent->plt.refcount += 1;
   5292   return TRUE;
   5293 }
   5294 
   5295 static bfd_boolean
   5296 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
   5297 {
   5298   return (r_type == R_PPC64_REL24
   5299 	  || r_type == R_PPC64_REL14
   5300 	  || r_type == R_PPC64_REL14_BRTAKEN
   5301 	  || r_type == R_PPC64_REL14_BRNTAKEN
   5302 	  || r_type == R_PPC64_ADDR24
   5303 	  || r_type == R_PPC64_ADDR14
   5304 	  || r_type == R_PPC64_ADDR14_BRTAKEN
   5305 	  || r_type == R_PPC64_ADDR14_BRNTAKEN);
   5306 }
   5307 
   5308 /* Look through the relocs for a section during the first phase, and
   5309    calculate needed space in the global offset table, procedure
   5310    linkage table, and dynamic reloc sections.  */
   5311 
   5312 static bfd_boolean
   5313 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   5314 			asection *sec, const Elf_Internal_Rela *relocs)
   5315 {
   5316   struct ppc_link_hash_table *htab;
   5317   Elf_Internal_Shdr *symtab_hdr;
   5318   struct elf_link_hash_entry **sym_hashes;
   5319   const Elf_Internal_Rela *rel;
   5320   const Elf_Internal_Rela *rel_end;
   5321   asection *sreloc;
   5322   asection **opd_sym_map;
   5323   struct elf_link_hash_entry *tga, *dottga;
   5324 
   5325   if (bfd_link_relocatable (info))
   5326     return TRUE;
   5327 
   5328   /* Don't do anything special with non-loaded, non-alloced sections.
   5329      In particular, any relocs in such sections should not affect GOT
   5330      and PLT reference counting (ie. we don't allow them to create GOT
   5331      or PLT entries), there's no possibility or desire to optimize TLS
   5332      relocs, and there's not much point in propagating relocs to shared
   5333      libs that the dynamic linker won't relocate.  */
   5334   if ((sec->flags & SEC_ALLOC) == 0)
   5335     return TRUE;
   5336 
   5337   BFD_ASSERT (is_ppc64_elf (abfd));
   5338 
   5339   htab = ppc_hash_table (info);
   5340   if (htab == NULL)
   5341     return FALSE;
   5342 
   5343   tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   5344 			      FALSE, FALSE, TRUE);
   5345   dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   5346 				 FALSE, FALSE, TRUE);
   5347   symtab_hdr = &elf_symtab_hdr (abfd);
   5348   sym_hashes = elf_sym_hashes (abfd);
   5349   sreloc = NULL;
   5350   opd_sym_map = NULL;
   5351   if (ppc64_elf_section_data (sec) != NULL
   5352       && ppc64_elf_section_data (sec)->sec_type == sec_opd)
   5353     opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
   5354 
   5355   rel_end = relocs + sec->reloc_count;
   5356   for (rel = relocs; rel < rel_end; rel++)
   5357     {
   5358       unsigned long r_symndx;
   5359       struct elf_link_hash_entry *h;
   5360       enum elf_ppc64_reloc_type r_type;
   5361       int tls_type;
   5362       struct _ppc64_elf_section_data *ppc64_sec;
   5363       struct plt_entry **ifunc, **plt_list;
   5364 
   5365       r_symndx = ELF64_R_SYM (rel->r_info);
   5366       if (r_symndx < symtab_hdr->sh_info)
   5367 	h = NULL;
   5368       else
   5369 	{
   5370 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   5371 	  h = elf_follow_link (h);
   5372 
   5373 	  /* PR15323, ref flags aren't set for references in the same
   5374 	     object.  */
   5375 	  h->root.non_ir_ref = 1;
   5376 
   5377 	  if (h == htab->elf.hgot)
   5378 	    sec->has_toc_reloc = 1;
   5379 	}
   5380 
   5381       tls_type = 0;
   5382       ifunc = NULL;
   5383       if (h != NULL)
   5384 	{
   5385 	  if (h->type == STT_GNU_IFUNC)
   5386 	    {
   5387 	      h->needs_plt = 1;
   5388 	      ifunc = &h->plt.plist;
   5389 	    }
   5390 	}
   5391       else
   5392 	{
   5393 	  Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5394 							  abfd, r_symndx);
   5395 	  if (isym == NULL)
   5396 	    return FALSE;
   5397 
   5398 	  if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
   5399 	    {
   5400 	      ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5401 					     rel->r_addend, PLT_IFUNC);
   5402 	      if (ifunc == NULL)
   5403 		return FALSE;
   5404 	    }
   5405 	}
   5406 
   5407       r_type = ELF64_R_TYPE (rel->r_info);
   5408       switch (r_type)
   5409 	{
   5410 	case R_PPC64_TLSGD:
   5411 	case R_PPC64_TLSLD:
   5412 	  /* These special tls relocs tie a call to __tls_get_addr with
   5413 	     its parameter symbol.  */
   5414 	  break;
   5415 
   5416 	case R_PPC64_GOT_TLSLD16:
   5417 	case R_PPC64_GOT_TLSLD16_LO:
   5418 	case R_PPC64_GOT_TLSLD16_HI:
   5419 	case R_PPC64_GOT_TLSLD16_HA:
   5420 	  tls_type = TLS_TLS | TLS_LD;
   5421 	  goto dogottls;
   5422 
   5423 	case R_PPC64_GOT_TLSGD16:
   5424 	case R_PPC64_GOT_TLSGD16_LO:
   5425 	case R_PPC64_GOT_TLSGD16_HI:
   5426 	case R_PPC64_GOT_TLSGD16_HA:
   5427 	  tls_type = TLS_TLS | TLS_GD;
   5428 	  goto dogottls;
   5429 
   5430 	case R_PPC64_GOT_TPREL16_DS:
   5431 	case R_PPC64_GOT_TPREL16_LO_DS:
   5432 	case R_PPC64_GOT_TPREL16_HI:
   5433 	case R_PPC64_GOT_TPREL16_HA:
   5434 	  if (bfd_link_pic (info))
   5435 	    info->flags |= DF_STATIC_TLS;
   5436 	  tls_type = TLS_TLS | TLS_TPREL;
   5437 	  goto dogottls;
   5438 
   5439 	case R_PPC64_GOT_DTPREL16_DS:
   5440 	case R_PPC64_GOT_DTPREL16_LO_DS:
   5441 	case R_PPC64_GOT_DTPREL16_HI:
   5442 	case R_PPC64_GOT_DTPREL16_HA:
   5443 	  tls_type = TLS_TLS | TLS_DTPREL;
   5444 	dogottls:
   5445 	  sec->has_tls_reloc = 1;
   5446 	  /* Fall thru */
   5447 
   5448 	case R_PPC64_GOT16:
   5449 	case R_PPC64_GOT16_DS:
   5450 	case R_PPC64_GOT16_HA:
   5451 	case R_PPC64_GOT16_HI:
   5452 	case R_PPC64_GOT16_LO:
   5453 	case R_PPC64_GOT16_LO_DS:
   5454 	  /* This symbol requires a global offset table entry.  */
   5455 	  sec->has_toc_reloc = 1;
   5456 	  if (r_type == R_PPC64_GOT_TLSLD16
   5457 	      || r_type == R_PPC64_GOT_TLSGD16
   5458 	      || r_type == R_PPC64_GOT_TPREL16_DS
   5459 	      || r_type == R_PPC64_GOT_DTPREL16_DS
   5460 	      || r_type == R_PPC64_GOT16
   5461 	      || r_type == R_PPC64_GOT16_DS)
   5462 	    {
   5463 	      htab->do_multi_toc = 1;
   5464 	      ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5465 	    }
   5466 
   5467 	  if (ppc64_elf_tdata (abfd)->got == NULL
   5468 	      && !create_got_section (abfd, info))
   5469 	    return FALSE;
   5470 
   5471 	  if (h != NULL)
   5472 	    {
   5473 	      struct ppc_link_hash_entry *eh;
   5474 	      struct got_entry *ent;
   5475 
   5476 	      eh = (struct ppc_link_hash_entry *) h;
   5477 	      for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
   5478 		if (ent->addend == rel->r_addend
   5479 		    && ent->owner == abfd
   5480 		    && ent->tls_type == tls_type)
   5481 		  break;
   5482 	      if (ent == NULL)
   5483 		{
   5484 		  bfd_size_type amt = sizeof (*ent);
   5485 		  ent = bfd_alloc (abfd, amt);
   5486 		  if (ent == NULL)
   5487 		    return FALSE;
   5488 		  ent->next = eh->elf.got.glist;
   5489 		  ent->addend = rel->r_addend;
   5490 		  ent->owner = abfd;
   5491 		  ent->tls_type = tls_type;
   5492 		  ent->is_indirect = FALSE;
   5493 		  ent->got.refcount = 0;
   5494 		  eh->elf.got.glist = ent;
   5495 		}
   5496 	      ent->got.refcount += 1;
   5497 	      eh->tls_mask |= tls_type;
   5498 	    }
   5499 	  else
   5500 	    /* This is a global offset table entry for a local symbol.  */
   5501 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5502 					rel->r_addend, tls_type))
   5503 	      return FALSE;
   5504 
   5505 	  /* We may also need a plt entry if the symbol turns out to be
   5506 	     an ifunc.  */
   5507 	  if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
   5508 	    {
   5509 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5510 		return FALSE;
   5511 	    }
   5512 	  break;
   5513 
   5514 	case R_PPC64_PLT16_HA:
   5515 	case R_PPC64_PLT16_HI:
   5516 	case R_PPC64_PLT16_LO:
   5517 	case R_PPC64_PLT32:
   5518 	case R_PPC64_PLT64:
   5519 	  /* This symbol requires a procedure linkage table entry.  */
   5520 	  plt_list = ifunc;
   5521 	  if (h != NULL)
   5522 	    {
   5523 	      h->needs_plt = 1;
   5524 	      if (h->root.root.string[0] == '.'
   5525 		  && h->root.root.string[1] != '\0')
   5526 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5527 	      plt_list = &h->plt.plist;
   5528 	    }
   5529 	  if (plt_list == NULL)
   5530 	    {
   5531 	      /* It does not make sense to have a procedure linkage
   5532 		 table entry for a non-ifunc local symbol.  */
   5533 	      info->callbacks->einfo
   5534 		(_("%P: %H: %s reloc against local symbol\n"),
   5535 		 abfd, sec, rel->r_offset,
   5536 		 ppc64_elf_howto_table[r_type]->name);
   5537 	      bfd_set_error (bfd_error_bad_value);
   5538 	      return FALSE;
   5539 	    }
   5540 	  if (!update_plt_info (abfd, plt_list, rel->r_addend))
   5541 	    return FALSE;
   5542 	  break;
   5543 
   5544 	  /* The following relocations don't need to propagate the
   5545 	     relocation if linking a shared object since they are
   5546 	     section relative.  */
   5547 	case R_PPC64_SECTOFF:
   5548 	case R_PPC64_SECTOFF_LO:
   5549 	case R_PPC64_SECTOFF_HI:
   5550 	case R_PPC64_SECTOFF_HA:
   5551 	case R_PPC64_SECTOFF_DS:
   5552 	case R_PPC64_SECTOFF_LO_DS:
   5553 	case R_PPC64_DTPREL16:
   5554 	case R_PPC64_DTPREL16_LO:
   5555 	case R_PPC64_DTPREL16_HI:
   5556 	case R_PPC64_DTPREL16_HA:
   5557 	case R_PPC64_DTPREL16_DS:
   5558 	case R_PPC64_DTPREL16_LO_DS:
   5559 	case R_PPC64_DTPREL16_HIGH:
   5560 	case R_PPC64_DTPREL16_HIGHA:
   5561 	case R_PPC64_DTPREL16_HIGHER:
   5562 	case R_PPC64_DTPREL16_HIGHERA:
   5563 	case R_PPC64_DTPREL16_HIGHEST:
   5564 	case R_PPC64_DTPREL16_HIGHESTA:
   5565 	  break;
   5566 
   5567 	  /* Nor do these.  */
   5568 	case R_PPC64_REL16:
   5569 	case R_PPC64_REL16_LO:
   5570 	case R_PPC64_REL16_HI:
   5571 	case R_PPC64_REL16_HA:
   5572 	case R_PPC64_REL16DX_HA:
   5573 	  break;
   5574 
   5575 	  /* Not supported as a dynamic relocation.  */
   5576 	case R_PPC64_ADDR64_LOCAL:
   5577 	  if (bfd_link_pic (info))
   5578 	    {
   5579 	      if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   5580 		ppc_howto_init ();
   5581 	      info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
   5582 					"in shared libraries and PIEs.\n"),
   5583 				      abfd, sec, rel->r_offset,
   5584 				      ppc64_elf_howto_table[r_type]->name);
   5585 	      bfd_set_error (bfd_error_bad_value);
   5586 	      return FALSE;
   5587 	    }
   5588 	  break;
   5589 
   5590 	case R_PPC64_TOC16:
   5591 	case R_PPC64_TOC16_DS:
   5592 	  htab->do_multi_toc = 1;
   5593 	  ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
   5594 	case R_PPC64_TOC16_LO:
   5595 	case R_PPC64_TOC16_HI:
   5596 	case R_PPC64_TOC16_HA:
   5597 	case R_PPC64_TOC16_LO_DS:
   5598 	  sec->has_toc_reloc = 1;
   5599 	  break;
   5600 
   5601 	  /* Marker reloc.  */
   5602 	case R_PPC64_ENTRY:
   5603 	  break;
   5604 
   5605 	  /* This relocation describes the C++ object vtable hierarchy.
   5606 	     Reconstruct it for later use during GC.  */
   5607 	case R_PPC64_GNU_VTINHERIT:
   5608 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   5609 	    return FALSE;
   5610 	  break;
   5611 
   5612 	  /* This relocation describes which C++ vtable entries are actually
   5613 	     used.  Record for later use during GC.  */
   5614 	case R_PPC64_GNU_VTENTRY:
   5615 	  BFD_ASSERT (h != NULL);
   5616 	  if (h != NULL
   5617 	      && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   5618 	    return FALSE;
   5619 	  break;
   5620 
   5621 	case R_PPC64_REL14:
   5622 	case R_PPC64_REL14_BRTAKEN:
   5623 	case R_PPC64_REL14_BRNTAKEN:
   5624 	  {
   5625 	    asection *dest = NULL;
   5626 
   5627 	    /* Heuristic: If jumping outside our section, chances are
   5628 	       we are going to need a stub.  */
   5629 	    if (h != NULL)
   5630 	      {
   5631 		/* If the sym is weak it may be overridden later, so
   5632 		   don't assume we know where a weak sym lives.  */
   5633 		if (h->root.type == bfd_link_hash_defined)
   5634 		  dest = h->root.u.def.section;
   5635 	      }
   5636 	    else
   5637 	      {
   5638 		Elf_Internal_Sym *isym;
   5639 
   5640 		isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5641 					      abfd, r_symndx);
   5642 		if (isym == NULL)
   5643 		  return FALSE;
   5644 
   5645 		dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5646 	      }
   5647 
   5648 	    if (dest != sec)
   5649 	      ppc64_elf_section_data (sec)->has_14bit_branch = 1;
   5650 	  }
   5651 	  /* Fall through.  */
   5652 
   5653 	case R_PPC64_REL24:
   5654 	  plt_list = ifunc;
   5655 	  if (h != NULL)
   5656 	    {
   5657 	      h->needs_plt = 1;
   5658 	      if (h->root.root.string[0] == '.'
   5659 		  && h->root.root.string[1] != '\0')
   5660 		((struct ppc_link_hash_entry *) h)->is_func = 1;
   5661 
   5662 	      if (h == tga || h == dottga)
   5663 		{
   5664 		  sec->has_tls_reloc = 1;
   5665 		  if (rel != relocs
   5666 		      && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
   5667 			  || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
   5668 		    /* We have a new-style __tls_get_addr call with
   5669 		       a marker reloc.  */
   5670 		    ;
   5671 		  else
   5672 		    /* Mark this section as having an old-style call.  */
   5673 		    sec->has_tls_get_addr_call = 1;
   5674 		}
   5675 	      plt_list = &h->plt.plist;
   5676 	    }
   5677 
   5678 	  /* We may need a .plt entry if the function this reloc
   5679 	     refers to is in a shared lib.  */
   5680 	  if (plt_list
   5681 	      && !update_plt_info (abfd, plt_list, rel->r_addend))
   5682 	    return FALSE;
   5683 	  break;
   5684 
   5685 	case R_PPC64_ADDR14:
   5686 	case R_PPC64_ADDR14_BRNTAKEN:
   5687 	case R_PPC64_ADDR14_BRTAKEN:
   5688 	case R_PPC64_ADDR24:
   5689 	  goto dodyn;
   5690 
   5691 	case R_PPC64_TPREL64:
   5692 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
   5693 	  if (bfd_link_pic (info))
   5694 	    info->flags |= DF_STATIC_TLS;
   5695 	  goto dotlstoc;
   5696 
   5697 	case R_PPC64_DTPMOD64:
   5698 	  if (rel + 1 < rel_end
   5699 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   5700 	      && rel[1].r_offset == rel->r_offset + 8)
   5701 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
   5702 	  else
   5703 	    tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
   5704 	  goto dotlstoc;
   5705 
   5706 	case R_PPC64_DTPREL64:
   5707 	  tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
   5708 	  if (rel != relocs
   5709 	      && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
   5710 	      && rel[-1].r_offset == rel->r_offset - 8)
   5711 	    /* This is the second reloc of a dtpmod, dtprel pair.
   5712 	       Don't mark with TLS_DTPREL.  */
   5713 	    goto dodyn;
   5714 
   5715 	dotlstoc:
   5716 	  sec->has_tls_reloc = 1;
   5717 	  if (h != NULL)
   5718 	    {
   5719 	      struct ppc_link_hash_entry *eh;
   5720 	      eh = (struct ppc_link_hash_entry *) h;
   5721 	      eh->tls_mask |= tls_type;
   5722 	    }
   5723 	  else
   5724 	    if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
   5725 					rel->r_addend, tls_type))
   5726 	      return FALSE;
   5727 
   5728 	  ppc64_sec = ppc64_elf_section_data (sec);
   5729 	  if (ppc64_sec->sec_type != sec_toc)
   5730 	    {
   5731 	      bfd_size_type amt;
   5732 
   5733 	      /* One extra to simplify get_tls_mask.  */
   5734 	      amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
   5735 	      ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
   5736 	      if (ppc64_sec->u.toc.symndx == NULL)
   5737 		return FALSE;
   5738 	      amt = sec->size * sizeof (bfd_vma) / 8;
   5739 	      ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
   5740 	      if (ppc64_sec->u.toc.add == NULL)
   5741 		return FALSE;
   5742 	      BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
   5743 	      ppc64_sec->sec_type = sec_toc;
   5744 	    }
   5745 	  BFD_ASSERT (rel->r_offset % 8 == 0);
   5746 	  ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
   5747 	  ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
   5748 
   5749 	  /* Mark the second slot of a GD or LD entry.
   5750 	     -1 to indicate GD and -2 to indicate LD.  */
   5751 	  if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
   5752 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
   5753 	  else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
   5754 	    ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
   5755 	  goto dodyn;
   5756 
   5757 	case R_PPC64_TPREL16:
   5758 	case R_PPC64_TPREL16_LO:
   5759 	case R_PPC64_TPREL16_HI:
   5760 	case R_PPC64_TPREL16_HA:
   5761 	case R_PPC64_TPREL16_DS:
   5762 	case R_PPC64_TPREL16_LO_DS:
   5763 	case R_PPC64_TPREL16_HIGH:
   5764 	case R_PPC64_TPREL16_HIGHA:
   5765 	case R_PPC64_TPREL16_HIGHER:
   5766 	case R_PPC64_TPREL16_HIGHERA:
   5767 	case R_PPC64_TPREL16_HIGHEST:
   5768 	case R_PPC64_TPREL16_HIGHESTA:
   5769 	  if (bfd_link_pic (info))
   5770 	    {
   5771 	      info->flags |= DF_STATIC_TLS;
   5772 	      goto dodyn;
   5773 	    }
   5774 	  break;
   5775 
   5776 	case R_PPC64_ADDR64:
   5777 	  if (opd_sym_map != NULL
   5778 	      && rel + 1 < rel_end
   5779 	      && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
   5780 	    {
   5781 	      if (h != NULL)
   5782 		{
   5783 		  if (h->root.root.string[0] == '.'
   5784 		      && h->root.root.string[1] != 0
   5785 		      && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
   5786 		    ;
   5787 		  else
   5788 		    ((struct ppc_link_hash_entry *) h)->is_func = 1;
   5789 		}
   5790 	      else
   5791 		{
   5792 		  asection *s;
   5793 		  Elf_Internal_Sym *isym;
   5794 
   5795 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5796 						abfd, r_symndx);
   5797 		  if (isym == NULL)
   5798 		    return FALSE;
   5799 
   5800 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5801 		  if (s != NULL && s != sec)
   5802 		    opd_sym_map[OPD_NDX (rel->r_offset)] = s;
   5803 		}
   5804 	    }
   5805 	  /* Fall through.  */
   5806 
   5807 	case R_PPC64_ADDR16:
   5808 	case R_PPC64_ADDR16_DS:
   5809 	case R_PPC64_ADDR16_HA:
   5810 	case R_PPC64_ADDR16_HI:
   5811 	case R_PPC64_ADDR16_HIGH:
   5812 	case R_PPC64_ADDR16_HIGHA:
   5813 	case R_PPC64_ADDR16_HIGHER:
   5814 	case R_PPC64_ADDR16_HIGHERA:
   5815 	case R_PPC64_ADDR16_HIGHEST:
   5816 	case R_PPC64_ADDR16_HIGHESTA:
   5817 	case R_PPC64_ADDR16_LO:
   5818 	case R_PPC64_ADDR16_LO_DS:
   5819 	  if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
   5820 	      && rel->r_addend == 0)
   5821 	    {
   5822 	      /* We may need a .plt entry if this reloc refers to a
   5823 		 function in a shared lib.  */
   5824 	      if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
   5825 		return FALSE;
   5826 	      h->pointer_equality_needed = 1;
   5827 	    }
   5828 	  /* Fall through.  */
   5829 
   5830 	case R_PPC64_REL30:
   5831 	case R_PPC64_REL32:
   5832 	case R_PPC64_REL64:
   5833 	case R_PPC64_ADDR32:
   5834 	case R_PPC64_UADDR16:
   5835 	case R_PPC64_UADDR32:
   5836 	case R_PPC64_UADDR64:
   5837 	case R_PPC64_TOC:
   5838 	  if (h != NULL && !bfd_link_pic (info))
   5839 	    /* We may need a copy reloc.  */
   5840 	    h->non_got_ref = 1;
   5841 
   5842 	  /* Don't propagate .opd relocs.  */
   5843 	  if (NO_OPD_RELOCS && opd_sym_map != NULL)
   5844 	    break;
   5845 
   5846 	  /* If we are creating a shared library, and this is a reloc
   5847 	     against a global symbol, or a non PC relative reloc
   5848 	     against a local symbol, then we need to copy the reloc
   5849 	     into the shared library.  However, if we are linking with
   5850 	     -Bsymbolic, we do not need to copy a reloc against a
   5851 	     global symbol which is defined in an object we are
   5852 	     including in the link (i.e., DEF_REGULAR is set).  At
   5853 	     this point we have not seen all the input files, so it is
   5854 	     possible that DEF_REGULAR is not set now but will be set
   5855 	     later (it is never cleared).  In case of a weak definition,
   5856 	     DEF_REGULAR may be cleared later by a strong definition in
   5857 	     a shared library.  We account for that possibility below by
   5858 	     storing information in the dyn_relocs field of the hash
   5859 	     table entry.  A similar situation occurs when creating
   5860 	     shared libraries and symbol visibility changes render the
   5861 	     symbol local.
   5862 
   5863 	     If on the other hand, we are creating an executable, we
   5864 	     may need to keep relocations for symbols satisfied by a
   5865 	     dynamic library if we manage to avoid copy relocs for the
   5866 	     symbol.  */
   5867 	dodyn:
   5868 	  if ((bfd_link_pic (info)
   5869 	       && (must_be_dyn_reloc (info, r_type)
   5870 		   || (h != NULL
   5871 		       && (!SYMBOLIC_BIND (info, h)
   5872 			   || h->root.type == bfd_link_hash_defweak
   5873 			   || !h->def_regular))))
   5874 	      || (ELIMINATE_COPY_RELOCS
   5875 		  && !bfd_link_pic (info)
   5876 		  && h != NULL
   5877 		  && (h->root.type == bfd_link_hash_defweak
   5878 		      || !h->def_regular))
   5879 	      || (!bfd_link_pic (info)
   5880 		  && ifunc != NULL))
   5881 	    {
   5882 	      /* We must copy these reloc types into the output file.
   5883 		 Create a reloc section in dynobj and make room for
   5884 		 this reloc.  */
   5885 	      if (sreloc == NULL)
   5886 		{
   5887 		  sreloc = _bfd_elf_make_dynamic_reloc_section
   5888 		    (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
   5889 
   5890 		  if (sreloc == NULL)
   5891 		    return FALSE;
   5892 		}
   5893 
   5894 	      /* If this is a global symbol, we count the number of
   5895 		 relocations we need for this symbol.  */
   5896 	      if (h != NULL)
   5897 		{
   5898 		  struct elf_dyn_relocs *p;
   5899 		  struct elf_dyn_relocs **head;
   5900 
   5901 		  head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   5902 		  p = *head;
   5903 		  if (p == NULL || p->sec != sec)
   5904 		    {
   5905 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5906 		      if (p == NULL)
   5907 			return FALSE;
   5908 		      p->next = *head;
   5909 		      *head = p;
   5910 		      p->sec = sec;
   5911 		      p->count = 0;
   5912 		      p->pc_count = 0;
   5913 		    }
   5914 		  p->count += 1;
   5915 		  if (!must_be_dyn_reloc (info, r_type))
   5916 		    p->pc_count += 1;
   5917 		}
   5918 	      else
   5919 		{
   5920 		  /* Track dynamic relocs needed for local syms too.
   5921 		     We really need local syms available to do this
   5922 		     easily.  Oh well.  */
   5923 		  struct ppc_dyn_relocs *p;
   5924 		  struct ppc_dyn_relocs **head;
   5925 		  bfd_boolean is_ifunc;
   5926 		  asection *s;
   5927 		  void *vpp;
   5928 		  Elf_Internal_Sym *isym;
   5929 
   5930 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache,
   5931 						abfd, r_symndx);
   5932 		  if (isym == NULL)
   5933 		    return FALSE;
   5934 
   5935 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   5936 		  if (s == NULL)
   5937 		    s = sec;
   5938 
   5939 		  vpp = &elf_section_data (s)->local_dynrel;
   5940 		  head = (struct ppc_dyn_relocs **) vpp;
   5941 		  is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
   5942 		  p = *head;
   5943 		  if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
   5944 		    p = p->next;
   5945 		  if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
   5946 		    {
   5947 		      p = bfd_alloc (htab->elf.dynobj, sizeof *p);
   5948 		      if (p == NULL)
   5949 			return FALSE;
   5950 		      p->next = *head;
   5951 		      *head = p;
   5952 		      p->sec = sec;
   5953 		      p->ifunc = is_ifunc;
   5954 		      p->count = 0;
   5955 		    }
   5956 		  p->count += 1;
   5957 		}
   5958 	    }
   5959 	  break;
   5960 
   5961 	default:
   5962 	  break;
   5963 	}
   5964     }
   5965 
   5966   return TRUE;
   5967 }
   5968 
   5969 /* Merge backend specific data from an object file to the output
   5970    object file when linking.  */
   5971 
   5972 static bfd_boolean
   5973 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   5974 {
   5975   unsigned long iflags, oflags;
   5976 
   5977   if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
   5978     return TRUE;
   5979 
   5980   if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
   5981     return TRUE;
   5982 
   5983   if (!_bfd_generic_verify_endian_match (ibfd, obfd))
   5984     return FALSE;
   5985 
   5986   iflags = elf_elfheader (ibfd)->e_flags;
   5987   oflags = elf_elfheader (obfd)->e_flags;
   5988 
   5989   if (iflags & ~EF_PPC64_ABI)
   5990     {
   5991       (*_bfd_error_handler)
   5992 	(_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
   5993       bfd_set_error (bfd_error_bad_value);
   5994       return FALSE;
   5995     }
   5996   else if (iflags != oflags && iflags != 0)
   5997     {
   5998       (*_bfd_error_handler)
   5999 	(_("%B: ABI version %ld is not compatible with ABI version %ld output"),
   6000 	 ibfd, iflags, oflags);
   6001       bfd_set_error (bfd_error_bad_value);
   6002       return FALSE;
   6003     }
   6004 
   6005   _bfd_elf_ppc_merge_fp_attributes (ibfd, obfd);
   6006 
   6007   /* Merge Tag_compatibility attributes and any common GNU ones.  */
   6008   _bfd_elf_merge_object_attributes (ibfd, obfd);
   6009 
   6010   return TRUE;
   6011 }
   6012 
   6013 static bfd_boolean
   6014 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   6015 {
   6016   /* Print normal ELF private data.  */
   6017   _bfd_elf_print_private_bfd_data (abfd, ptr);
   6018 
   6019   if (elf_elfheader (abfd)->e_flags != 0)
   6020     {
   6021       FILE *file = ptr;
   6022 
   6023       /* xgettext:c-format */
   6024       fprintf (file, _("private flags = 0x%lx:"),
   6025 	       elf_elfheader (abfd)->e_flags);
   6026 
   6027       if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
   6028 	fprintf (file, _(" [abiv%ld]"),
   6029 		 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
   6030       fputc ('\n', file);
   6031     }
   6032 
   6033   return TRUE;
   6034 }
   6035 
   6036 /* OFFSET in OPD_SEC specifies a function descriptor.  Return the address
   6037    of the code entry point, and its section, which must be in the same
   6038    object as OPD_SEC.  Returns (bfd_vma) -1 on error.  */
   6039 
   6040 static bfd_vma
   6041 opd_entry_value (asection *opd_sec,
   6042 		 bfd_vma offset,
   6043 		 asection **code_sec,
   6044 		 bfd_vma *code_off,
   6045 		 bfd_boolean in_code_sec)
   6046 {
   6047   bfd *opd_bfd = opd_sec->owner;
   6048   Elf_Internal_Rela *relocs;
   6049   Elf_Internal_Rela *lo, *hi, *look;
   6050   bfd_vma val;
   6051 
   6052   /* No relocs implies we are linking a --just-symbols object, or looking
   6053      at a final linked executable with addr2line or somesuch.  */
   6054   if (opd_sec->reloc_count == 0)
   6055     {
   6056       bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
   6057 
   6058       if (contents == NULL)
   6059 	{
   6060 	  if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
   6061 	    return (bfd_vma) -1;
   6062 	  ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
   6063 	}
   6064 
   6065       /* PR 17512: file: 64b9dfbb.  */
   6066       if (offset + 7 >= opd_sec->size || offset + 7 < offset)
   6067 	return (bfd_vma) -1;
   6068 
   6069       val = bfd_get_64 (opd_bfd, contents + offset);
   6070       if (code_sec != NULL)
   6071 	{
   6072 	  asection *sec, *likely = NULL;
   6073 
   6074 	  if (in_code_sec)
   6075 	    {
   6076 	      sec = *code_sec;
   6077 	      if (sec->vma <= val
   6078 		  && val < sec->vma + sec->size)
   6079 		likely = sec;
   6080 	      else
   6081 		val = -1;
   6082 	    }
   6083 	  else
   6084 	    for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
   6085 	      if (sec->vma <= val
   6086 		  && (sec->flags & SEC_LOAD) != 0
   6087 		  && (sec->flags & SEC_ALLOC) != 0)
   6088 		likely = sec;
   6089 	  if (likely != NULL)
   6090 	    {
   6091 	      *code_sec = likely;
   6092 	      if (code_off != NULL)
   6093 		*code_off = val - likely->vma;
   6094 	    }
   6095 	}
   6096       return val;
   6097     }
   6098 
   6099   BFD_ASSERT (is_ppc64_elf (opd_bfd));
   6100 
   6101   relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
   6102   if (relocs == NULL)
   6103     relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
   6104   /* PR 17512: file: df8e1fd6.  */
   6105   if (relocs == NULL)
   6106     return (bfd_vma) -1;
   6107 
   6108   /* Go find the opd reloc at the sym address.  */
   6109   lo = relocs;
   6110   hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
   6111   val = (bfd_vma) -1;
   6112   while (lo < hi)
   6113     {
   6114       look = lo + (hi - lo) / 2;
   6115       if (look->r_offset < offset)
   6116 	lo = look + 1;
   6117       else if (look->r_offset > offset)
   6118 	hi = look;
   6119       else
   6120 	{
   6121 	  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
   6122 
   6123 	  if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
   6124 	      && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
   6125 	    {
   6126 	      unsigned long symndx = ELF64_R_SYM (look->r_info);
   6127 	      asection *sec = NULL;
   6128 
   6129 	      if (symndx >= symtab_hdr->sh_info
   6130 		  && elf_sym_hashes (opd_bfd) != NULL)
   6131 		{
   6132 		  struct elf_link_hash_entry **sym_hashes;
   6133 		  struct elf_link_hash_entry *rh;
   6134 
   6135 		  sym_hashes = elf_sym_hashes (opd_bfd);
   6136 		  rh = sym_hashes[symndx - symtab_hdr->sh_info];
   6137 		  if (rh != NULL)
   6138 		    {
   6139 		      rh = elf_follow_link (rh);
   6140 		      if (rh->root.type != bfd_link_hash_defined
   6141 			  && rh->root.type != bfd_link_hash_defweak)
   6142 			break;
   6143 		      if (rh->root.u.def.section->owner == opd_bfd)
   6144 			{
   6145 			  val = rh->root.u.def.value;
   6146 			  sec = rh->root.u.def.section;
   6147 			}
   6148 		    }
   6149 		}
   6150 
   6151 	      if (sec == NULL)
   6152 		{
   6153 		  Elf_Internal_Sym *sym;
   6154 
   6155 		  if (symndx < symtab_hdr->sh_info)
   6156 		    {
   6157 		      sym = (Elf_Internal_Sym *) symtab_hdr->contents;
   6158 		      if (sym == NULL)
   6159 			{
   6160 			  size_t symcnt = symtab_hdr->sh_info;
   6161 			  sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   6162 						      symcnt, 0,
   6163 						      NULL, NULL, NULL);
   6164 			  if (sym == NULL)
   6165 			    break;
   6166 			  symtab_hdr->contents = (bfd_byte *) sym;
   6167 			}
   6168 		      sym += symndx;
   6169 		    }
   6170 		  else
   6171 		    {
   6172 		      sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
   6173 						  1, symndx,
   6174 						  NULL, NULL, NULL);
   6175 		      if (sym == NULL)
   6176 			break;
   6177 		    }
   6178 		  sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
   6179 		  if (sec == NULL)
   6180 		    break;
   6181 		  BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
   6182 		  val = sym->st_value;
   6183 		}
   6184 
   6185 	      val += look->r_addend;
   6186 	      if (code_off != NULL)
   6187 		*code_off = val;
   6188 	      if (code_sec != NULL)
   6189 		{
   6190 		  if (in_code_sec && *code_sec != sec)
   6191 		    return -1;
   6192 		  else
   6193 		    *code_sec = sec;
   6194 		}
   6195 	      if (sec->output_section != NULL)
   6196 		val += sec->output_section->vma + sec->output_offset;
   6197 	    }
   6198 	  break;
   6199 	}
   6200     }
   6201 
   6202   return val;
   6203 }
   6204 
   6205 /* If the ELF symbol SYM might be a function in SEC, return the
   6206    function size and set *CODE_OFF to the function's entry point,
   6207    otherwise return zero.  */
   6208 
   6209 static bfd_size_type
   6210 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
   6211 			      bfd_vma *code_off)
   6212 {
   6213   bfd_size_type size;
   6214 
   6215   if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
   6216 		     | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
   6217     return 0;
   6218 
   6219   size = 0;
   6220   if (!(sym->flags & BSF_SYNTHETIC))
   6221     size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
   6222 
   6223   if (strcmp (sym->section->name, ".opd") == 0)
   6224     {
   6225       struct _opd_sec_data *opd = get_opd_info (sym->section);
   6226       bfd_vma symval = sym->value;
   6227 
   6228       if (opd != NULL
   6229 	  && opd->adjust != NULL
   6230 	  && elf_section_data (sym->section)->relocs != NULL)
   6231 	{
   6232 	  /* opd_entry_value will use cached relocs that have been
   6233 	     adjusted, but with raw symbols.  That means both local
   6234 	     and global symbols need adjusting.  */
   6235 	  long adjust = opd->adjust[OPD_NDX (symval)];
   6236 	  if (adjust == -1)
   6237 	    return 0;
   6238 	  symval += adjust;
   6239 	}
   6240 
   6241       if (opd_entry_value (sym->section, symval,
   6242 			   &sec, code_off, TRUE) == (bfd_vma) -1)
   6243 	return 0;
   6244       /* An old ABI binary with dot-syms has a size of 24 on the .opd
   6245 	 symbol.  This size has nothing to do with the code size of the
   6246 	 function, which is what we're supposed to return, but the
   6247 	 code size isn't available without looking up the dot-sym.
   6248 	 However, doing that would be a waste of time particularly
   6249 	 since elf_find_function will look at the dot-sym anyway.
   6250 	 Now, elf_find_function will keep the largest size of any
   6251 	 function sym found at the code address of interest, so return
   6252 	 1 here to avoid it incorrectly caching a larger function size
   6253 	 for a small function.  This does mean we return the wrong
   6254 	 size for a new-ABI function of size 24, but all that does is
   6255 	 disable caching for such functions.  */
   6256       if (size == 24)
   6257 	size = 1;
   6258     }
   6259   else
   6260     {
   6261       if (sym->section != sec)
   6262 	return 0;
   6263       *code_off = sym->value;
   6264     }
   6265   if (size == 0)
   6266     size = 1;
   6267   return size;
   6268 }
   6269 
   6270 /* Return true if symbol is defined in a regular object file.  */
   6271 
   6272 static bfd_boolean
   6273 is_static_defined (struct elf_link_hash_entry *h)
   6274 {
   6275   return ((h->root.type == bfd_link_hash_defined
   6276 	   || h->root.type == bfd_link_hash_defweak)
   6277 	  && h->root.u.def.section != NULL
   6278 	  && h->root.u.def.section->output_section != NULL);
   6279 }
   6280 
   6281 /* If FDH is a function descriptor symbol, return the associated code
   6282    entry symbol if it is defined.  Return NULL otherwise.  */
   6283 
   6284 static struct ppc_link_hash_entry *
   6285 defined_code_entry (struct ppc_link_hash_entry *fdh)
   6286 {
   6287   if (fdh->is_func_descriptor)
   6288     {
   6289       struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
   6290       if (fh->elf.root.type == bfd_link_hash_defined
   6291 	  || fh->elf.root.type == bfd_link_hash_defweak)
   6292 	return fh;
   6293     }
   6294   return NULL;
   6295 }
   6296 
   6297 /* If FH is a function code entry symbol, return the associated
   6298    function descriptor symbol if it is defined.  Return NULL otherwise.  */
   6299 
   6300 static struct ppc_link_hash_entry *
   6301 defined_func_desc (struct ppc_link_hash_entry *fh)
   6302 {
   6303   if (fh->oh != NULL
   6304       && fh->oh->is_func_descriptor)
   6305     {
   6306       struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
   6307       if (fdh->elf.root.type == bfd_link_hash_defined
   6308 	  || fdh->elf.root.type == bfd_link_hash_defweak)
   6309 	return fdh;
   6310     }
   6311   return NULL;
   6312 }
   6313 
   6314 /* Mark all our entry sym sections, both opd and code section.  */
   6315 
   6316 static void
   6317 ppc64_elf_gc_keep (struct bfd_link_info *info)
   6318 {
   6319   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   6320   struct bfd_sym_chain *sym;
   6321 
   6322   if (htab == NULL)
   6323     return;
   6324 
   6325   for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
   6326     {
   6327       struct ppc_link_hash_entry *eh, *fh;
   6328       asection *sec;
   6329 
   6330       eh = (struct ppc_link_hash_entry *)
   6331 	elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
   6332       if (eh == NULL)
   6333 	continue;
   6334       if (eh->elf.root.type != bfd_link_hash_defined
   6335 	  && eh->elf.root.type != bfd_link_hash_defweak)
   6336 	continue;
   6337 
   6338       fh = defined_code_entry (eh);
   6339       if (fh != NULL)
   6340 	{
   6341 	  sec = fh->elf.root.u.def.section;
   6342 	  sec->flags |= SEC_KEEP;
   6343 	}
   6344       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   6345 	       && opd_entry_value (eh->elf.root.u.def.section,
   6346 				   eh->elf.root.u.def.value,
   6347 				   &sec, NULL, FALSE) != (bfd_vma) -1)
   6348 	sec->flags |= SEC_KEEP;
   6349 
   6350       sec = eh->elf.root.u.def.section;
   6351       sec->flags |= SEC_KEEP;
   6352     }
   6353 }
   6354 
   6355 /* Mark sections containing dynamically referenced symbols.  When
   6356    building shared libraries, we must assume that any visible symbol is
   6357    referenced.  */
   6358 
   6359 static bfd_boolean
   6360 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
   6361 {
   6362   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   6363   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   6364   struct ppc_link_hash_entry *fdh;
   6365   struct bfd_elf_dynamic_list *d = info->dynamic_list;
   6366 
   6367   /* Dynamic linking info is on the func descriptor sym.  */
   6368   fdh = defined_func_desc (eh);
   6369   if (fdh != NULL)
   6370     eh = fdh;
   6371 
   6372   if ((eh->elf.root.type == bfd_link_hash_defined
   6373        || eh->elf.root.type == bfd_link_hash_defweak)
   6374       && (eh->elf.ref_dynamic
   6375 	  || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
   6376 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
   6377 	      && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
   6378 	      && (!bfd_link_executable (info)
   6379 		  || info->export_dynamic
   6380 		  || (eh->elf.dynamic
   6381 		      && d != NULL
   6382 		      && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
   6383 	      && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
   6384 		  || !bfd_hide_sym_by_version (info->version_info,
   6385 					       eh->elf.root.root.string)))))
   6386     {
   6387       asection *code_sec;
   6388       struct ppc_link_hash_entry *fh;
   6389 
   6390       eh->elf.root.u.def.section->flags |= SEC_KEEP;
   6391 
   6392       /* Function descriptor syms cause the associated
   6393 	 function code sym section to be marked.  */
   6394       fh = defined_code_entry (eh);
   6395       if (fh != NULL)
   6396 	{
   6397 	  code_sec = fh->elf.root.u.def.section;
   6398 	  code_sec->flags |= SEC_KEEP;
   6399 	}
   6400       else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   6401 	       && opd_entry_value (eh->elf.root.u.def.section,
   6402 				   eh->elf.root.u.def.value,
   6403 				   &code_sec, NULL, FALSE) != (bfd_vma) -1)
   6404 	code_sec->flags |= SEC_KEEP;
   6405     }
   6406 
   6407   return TRUE;
   6408 }
   6409 
   6410 /* Return the section that should be marked against GC for a given
   6411    relocation.  */
   6412 
   6413 static asection *
   6414 ppc64_elf_gc_mark_hook (asection *sec,
   6415 			struct bfd_link_info *info,
   6416 			Elf_Internal_Rela *rel,
   6417 			struct elf_link_hash_entry *h,
   6418 			Elf_Internal_Sym *sym)
   6419 {
   6420   asection *rsec;
   6421 
   6422   /* Syms return NULL if we're marking .opd, so we avoid marking all
   6423      function sections, as all functions are referenced in .opd.  */
   6424   rsec = NULL;
   6425   if (get_opd_info (sec) != NULL)
   6426     return rsec;
   6427 
   6428   if (h != NULL)
   6429     {
   6430       enum elf_ppc64_reloc_type r_type;
   6431       struct ppc_link_hash_entry *eh, *fh, *fdh;
   6432 
   6433       r_type = ELF64_R_TYPE (rel->r_info);
   6434       switch (r_type)
   6435 	{
   6436 	case R_PPC64_GNU_VTINHERIT:
   6437 	case R_PPC64_GNU_VTENTRY:
   6438 	  break;
   6439 
   6440 	default:
   6441 	  switch (h->root.type)
   6442 	    {
   6443 	    case bfd_link_hash_defined:
   6444 	    case bfd_link_hash_defweak:
   6445 	      eh = (struct ppc_link_hash_entry *) h;
   6446 	      fdh = defined_func_desc (eh);
   6447 	      if (fdh != NULL)
   6448 		eh = fdh;
   6449 
   6450 	      /* Function descriptor syms cause the associated
   6451 		 function code sym section to be marked.  */
   6452 	      fh = defined_code_entry (eh);
   6453 	      if (fh != NULL)
   6454 		{
   6455 		  /* They also mark their opd section.  */
   6456 		  eh->elf.root.u.def.section->gc_mark = 1;
   6457 
   6458 		  rsec = fh->elf.root.u.def.section;
   6459 		}
   6460 	      else if (get_opd_info (eh->elf.root.u.def.section) != NULL
   6461 		       && opd_entry_value (eh->elf.root.u.def.section,
   6462 					   eh->elf.root.u.def.value,
   6463 					   &rsec, NULL, FALSE) != (bfd_vma) -1)
   6464 		eh->elf.root.u.def.section->gc_mark = 1;
   6465 	      else
   6466 		rsec = h->root.u.def.section;
   6467 	      break;
   6468 
   6469 	    case bfd_link_hash_common:
   6470 	      rsec = h->root.u.c.p->section;
   6471 	      break;
   6472 
   6473 	    default:
   6474 	      return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   6475 	    }
   6476 	}
   6477     }
   6478   else
   6479     {
   6480       struct _opd_sec_data *opd;
   6481 
   6482       rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   6483       opd = get_opd_info (rsec);
   6484       if (opd != NULL && opd->func_sec != NULL)
   6485 	{
   6486 	  rsec->gc_mark = 1;
   6487 
   6488 	  rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
   6489 	}
   6490     }
   6491 
   6492   return rsec;
   6493 }
   6494 
   6495 /* Update the .got, .plt. and dynamic reloc reference counts for the
   6496    section being removed.  */
   6497 
   6498 static bfd_boolean
   6499 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
   6500 			 asection *sec, const Elf_Internal_Rela *relocs)
   6501 {
   6502   struct ppc_link_hash_table *htab;
   6503   Elf_Internal_Shdr *symtab_hdr;
   6504   struct elf_link_hash_entry **sym_hashes;
   6505   struct got_entry **local_got_ents;
   6506   const Elf_Internal_Rela *rel, *relend;
   6507 
   6508   if (bfd_link_relocatable (info))
   6509     return TRUE;
   6510 
   6511   if ((sec->flags & SEC_ALLOC) == 0)
   6512     return TRUE;
   6513 
   6514   elf_section_data (sec)->local_dynrel = NULL;
   6515 
   6516   htab = ppc_hash_table (info);
   6517   if (htab == NULL)
   6518     return FALSE;
   6519 
   6520   symtab_hdr = &elf_symtab_hdr (abfd);
   6521   sym_hashes = elf_sym_hashes (abfd);
   6522   local_got_ents = elf_local_got_ents (abfd);
   6523 
   6524   relend = relocs + sec->reloc_count;
   6525   for (rel = relocs; rel < relend; rel++)
   6526     {
   6527       unsigned long r_symndx;
   6528       enum elf_ppc64_reloc_type r_type;
   6529       struct elf_link_hash_entry *h = NULL;
   6530       struct plt_entry **plt_list;
   6531       unsigned char tls_type = 0;
   6532 
   6533       r_symndx = ELF64_R_SYM (rel->r_info);
   6534       r_type = ELF64_R_TYPE (rel->r_info);
   6535       if (r_symndx >= symtab_hdr->sh_info)
   6536 	{
   6537 	  struct ppc_link_hash_entry *eh;
   6538 	  struct elf_dyn_relocs **pp;
   6539 	  struct elf_dyn_relocs *p;
   6540 
   6541 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6542 	  h = elf_follow_link (h);
   6543 	  eh = (struct ppc_link_hash_entry *) h;
   6544 
   6545 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
   6546 	    if (p->sec == sec)
   6547 	      {
   6548 		/* Everything must go for SEC.  */
   6549 		*pp = p->next;
   6550 		break;
   6551 	      }
   6552 	}
   6553 
   6554       switch (r_type)
   6555 	{
   6556 	case R_PPC64_GOT_TLSLD16:
   6557 	case R_PPC64_GOT_TLSLD16_LO:
   6558 	case R_PPC64_GOT_TLSLD16_HI:
   6559 	case R_PPC64_GOT_TLSLD16_HA:
   6560 	  tls_type = TLS_TLS | TLS_LD;
   6561 	  goto dogot;
   6562 
   6563 	case R_PPC64_GOT_TLSGD16:
   6564 	case R_PPC64_GOT_TLSGD16_LO:
   6565 	case R_PPC64_GOT_TLSGD16_HI:
   6566 	case R_PPC64_GOT_TLSGD16_HA:
   6567 	  tls_type = TLS_TLS | TLS_GD;
   6568 	  goto dogot;
   6569 
   6570 	case R_PPC64_GOT_TPREL16_DS:
   6571 	case R_PPC64_GOT_TPREL16_LO_DS:
   6572 	case R_PPC64_GOT_TPREL16_HI:
   6573 	case R_PPC64_GOT_TPREL16_HA:
   6574 	  tls_type = TLS_TLS | TLS_TPREL;
   6575 	  goto dogot;
   6576 
   6577 	case R_PPC64_GOT_DTPREL16_DS:
   6578 	case R_PPC64_GOT_DTPREL16_LO_DS:
   6579 	case R_PPC64_GOT_DTPREL16_HI:
   6580 	case R_PPC64_GOT_DTPREL16_HA:
   6581 	  tls_type = TLS_TLS | TLS_DTPREL;
   6582 	  goto dogot;
   6583 
   6584 	case R_PPC64_GOT16:
   6585 	case R_PPC64_GOT16_DS:
   6586 	case R_PPC64_GOT16_HA:
   6587 	case R_PPC64_GOT16_HI:
   6588 	case R_PPC64_GOT16_LO:
   6589 	case R_PPC64_GOT16_LO_DS:
   6590 	dogot:
   6591 	  {
   6592 	    struct got_entry *ent;
   6593 
   6594 	    if (h != NULL)
   6595 	      ent = h->got.glist;
   6596 	    else
   6597 	      ent = local_got_ents[r_symndx];
   6598 
   6599 	    for (; ent != NULL; ent = ent->next)
   6600 	      if (ent->addend == rel->r_addend
   6601 		  && ent->owner == abfd
   6602 		  && ent->tls_type == tls_type)
   6603 		break;
   6604 	    if (ent == NULL)
   6605 	      abort ();
   6606 	    if (ent->got.refcount > 0)
   6607 	      ent->got.refcount -= 1;
   6608 	  }
   6609 	  break;
   6610 
   6611 	case R_PPC64_PLT16_HA:
   6612 	case R_PPC64_PLT16_HI:
   6613 	case R_PPC64_PLT16_LO:
   6614 	case R_PPC64_PLT32:
   6615 	case R_PPC64_PLT64:
   6616 	case R_PPC64_REL14:
   6617 	case R_PPC64_REL14_BRNTAKEN:
   6618 	case R_PPC64_REL14_BRTAKEN:
   6619 	case R_PPC64_REL24:
   6620 	  plt_list = NULL;
   6621 	  if (h != NULL)
   6622 	    plt_list = &h->plt.plist;
   6623 	  else if (local_got_ents != NULL)
   6624 	    {
   6625 	      struct plt_entry **local_plt = (struct plt_entry **)
   6626 		(local_got_ents + symtab_hdr->sh_info);
   6627 	      unsigned char *local_got_tls_masks = (unsigned char *)
   6628 		(local_plt + symtab_hdr->sh_info);
   6629 	      if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
   6630 		plt_list = local_plt + r_symndx;
   6631 	    }
   6632 	  if (plt_list)
   6633 	    {
   6634 	      struct plt_entry *ent;
   6635 
   6636 	      for (ent = *plt_list; ent != NULL; ent = ent->next)
   6637 		if (ent->addend == rel->r_addend)
   6638 		  break;
   6639 	      if (ent != NULL && ent->plt.refcount > 0)
   6640 		ent->plt.refcount -= 1;
   6641 	    }
   6642 	  break;
   6643 
   6644 	default:
   6645 	  break;
   6646 	}
   6647     }
   6648   return TRUE;
   6649 }
   6650 
   6651 /* The maximum size of .sfpr.  */
   6652 #define SFPR_MAX (218*4)
   6653 
   6654 struct sfpr_def_parms
   6655 {
   6656   const char name[12];
   6657   unsigned char lo, hi;
   6658   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
   6659   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
   6660 };
   6661 
   6662 /* Auto-generate _save*, _rest* functions in .sfpr.
   6663    If STUB_SEC is non-null, define alias symbols in STUB_SEC
   6664    instead.  */
   6665 
   6666 static bfd_boolean
   6667 sfpr_define (struct bfd_link_info *info,
   6668 	     const struct sfpr_def_parms *parm,
   6669 	     asection *stub_sec)
   6670 {
   6671   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   6672   unsigned int i;
   6673   size_t len = strlen (parm->name);
   6674   bfd_boolean writing = FALSE;
   6675   char sym[16];
   6676 
   6677   if (htab == NULL)
   6678     return FALSE;
   6679 
   6680   memcpy (sym, parm->name, len);
   6681   sym[len + 2] = 0;
   6682 
   6683   for (i = parm->lo; i <= parm->hi; i++)
   6684     {
   6685       struct ppc_link_hash_entry *h;
   6686 
   6687       sym[len + 0] = i / 10 + '0';
   6688       sym[len + 1] = i % 10 + '0';
   6689       h = (struct ppc_link_hash_entry *)
   6690 	elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
   6691       if (stub_sec != NULL)
   6692 	{
   6693 	  if (h != NULL
   6694 	      && h->elf.root.type == bfd_link_hash_defined
   6695 	      && h->elf.root.u.def.section == htab->sfpr)
   6696 	    {
   6697 	      struct elf_link_hash_entry *s;
   6698 	      char buf[32];
   6699 	      sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
   6700 	      s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
   6701 	      if (s == NULL)
   6702 		return FALSE;
   6703 	      if (s->root.type == bfd_link_hash_new
   6704 		  || (s->root.type = bfd_link_hash_defined
   6705 		      && s->root.u.def.section == stub_sec))
   6706 		{
   6707 		  s->root.type = bfd_link_hash_defined;
   6708 		  s->root.u.def.section = stub_sec;
   6709 		  s->root.u.def.value = (stub_sec->size
   6710 					 + h->elf.root.u.def.value);
   6711 		  s->ref_regular = 1;
   6712 		  s->def_regular = 1;
   6713 		  s->ref_regular_nonweak = 1;
   6714 		  s->forced_local = 1;
   6715 		  s->non_elf = 0;
   6716 		  s->root.linker_def = 1;
   6717 		}
   6718 	    }
   6719 	  continue;
   6720 	}
   6721       if (h != NULL)
   6722 	{
   6723 	  h->save_res = 1;
   6724 	  if (!h->elf.def_regular)
   6725 	    {
   6726 	      h->elf.root.type = bfd_link_hash_defined;
   6727 	      h->elf.root.u.def.section = htab->sfpr;
   6728 	      h->elf.root.u.def.value = htab->sfpr->size;
   6729 	      h->elf.type = STT_FUNC;
   6730 	      h->elf.def_regular = 1;
   6731 	      h->elf.non_elf = 0;
   6732 	      _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
   6733 	      writing = TRUE;
   6734 	      if (htab->sfpr->contents == NULL)
   6735 		{
   6736 		  htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
   6737 		  if (htab->sfpr->contents == NULL)
   6738 		    return FALSE;
   6739 		}
   6740 	    }
   6741 	}
   6742       if (writing)
   6743 	{
   6744 	  bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
   6745 	  if (i != parm->hi)
   6746 	    p = (*parm->write_ent) (htab->elf.dynobj, p, i);
   6747 	  else
   6748 	    p = (*parm->write_tail) (htab->elf.dynobj, p, i);
   6749 	  htab->sfpr->size = p - htab->sfpr->contents;
   6750 	}
   6751     }
   6752 
   6753   return TRUE;
   6754 }
   6755 
   6756 static bfd_byte *
   6757 savegpr0 (bfd *abfd, bfd_byte *p, int r)
   6758 {
   6759   bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6760   return p + 4;
   6761 }
   6762 
   6763 static bfd_byte *
   6764 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6765 {
   6766   p = savegpr0 (abfd, p, r);
   6767   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
   6768   p = p + 4;
   6769   bfd_put_32 (abfd, BLR, p);
   6770   return p + 4;
   6771 }
   6772 
   6773 static bfd_byte *
   6774 restgpr0 (bfd *abfd, bfd_byte *p, int r)
   6775 {
   6776   bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6777   return p + 4;
   6778 }
   6779 
   6780 static bfd_byte *
   6781 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6782 {
   6783   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
   6784   p = p + 4;
   6785   p = restgpr0 (abfd, p, r);
   6786   bfd_put_32 (abfd, MTLR_R0, p);
   6787   p = p + 4;
   6788   if (r == 29)
   6789     {
   6790       p = restgpr0 (abfd, p, 30);
   6791       p = restgpr0 (abfd, p, 31);
   6792     }
   6793   bfd_put_32 (abfd, BLR, p);
   6794   return p + 4;
   6795 }
   6796 
   6797 static bfd_byte *
   6798 savegpr1 (bfd *abfd, bfd_byte *p, int r)
   6799 {
   6800   bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6801   return p + 4;
   6802 }
   6803 
   6804 static bfd_byte *
   6805 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6806 {
   6807   p = savegpr1 (abfd, p, r);
   6808   bfd_put_32 (abfd, BLR, p);
   6809   return p + 4;
   6810 }
   6811 
   6812 static bfd_byte *
   6813 restgpr1 (bfd *abfd, bfd_byte *p, int r)
   6814 {
   6815   bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6816   return p + 4;
   6817 }
   6818 
   6819 static bfd_byte *
   6820 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6821 {
   6822   p = restgpr1 (abfd, p, r);
   6823   bfd_put_32 (abfd, BLR, p);
   6824   return p + 4;
   6825 }
   6826 
   6827 static bfd_byte *
   6828 savefpr (bfd *abfd, bfd_byte *p, int r)
   6829 {
   6830   bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6831   return p + 4;
   6832 }
   6833 
   6834 static bfd_byte *
   6835 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6836 {
   6837   p = savefpr (abfd, p, r);
   6838   bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
   6839   p = p + 4;
   6840   bfd_put_32 (abfd, BLR, p);
   6841   return p + 4;
   6842 }
   6843 
   6844 static bfd_byte *
   6845 restfpr (bfd *abfd, bfd_byte *p, int r)
   6846 {
   6847   bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
   6848   return p + 4;
   6849 }
   6850 
   6851 static bfd_byte *
   6852 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
   6853 {
   6854   bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
   6855   p = p + 4;
   6856   p = restfpr (abfd, p, r);
   6857   bfd_put_32 (abfd, MTLR_R0, p);
   6858   p = p + 4;
   6859   if (r == 29)
   6860     {
   6861       p = restfpr (abfd, p, 30);
   6862       p = restfpr (abfd, p, 31);
   6863     }
   6864   bfd_put_32 (abfd, BLR, p);
   6865   return p + 4;
   6866 }
   6867 
   6868 static bfd_byte *
   6869 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6870 {
   6871   p = savefpr (abfd, p, r);
   6872   bfd_put_32 (abfd, BLR, p);
   6873   return p + 4;
   6874 }
   6875 
   6876 static bfd_byte *
   6877 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
   6878 {
   6879   p = restfpr (abfd, p, r);
   6880   bfd_put_32 (abfd, BLR, p);
   6881   return p + 4;
   6882 }
   6883 
   6884 static bfd_byte *
   6885 savevr (bfd *abfd, bfd_byte *p, int r)
   6886 {
   6887   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6888   p = p + 4;
   6889   bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
   6890   return p + 4;
   6891 }
   6892 
   6893 static bfd_byte *
   6894 savevr_tail (bfd *abfd, bfd_byte *p, int r)
   6895 {
   6896   p = savevr (abfd, p, r);
   6897   bfd_put_32 (abfd, BLR, p);
   6898   return p + 4;
   6899 }
   6900 
   6901 static bfd_byte *
   6902 restvr (bfd *abfd, bfd_byte *p, int r)
   6903 {
   6904   bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
   6905   p = p + 4;
   6906   bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
   6907   return p + 4;
   6908 }
   6909 
   6910 static bfd_byte *
   6911 restvr_tail (bfd *abfd, bfd_byte *p, int r)
   6912 {
   6913   p = restvr (abfd, p, r);
   6914   bfd_put_32 (abfd, BLR, p);
   6915   return p + 4;
   6916 }
   6917 
   6918 /* Called via elf_link_hash_traverse to transfer dynamic linking
   6919    information on function code symbol entries to their corresponding
   6920    function descriptor symbol entries.  */
   6921 
   6922 static bfd_boolean
   6923 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
   6924 {
   6925   struct bfd_link_info *info;
   6926   struct ppc_link_hash_table *htab;
   6927   struct plt_entry *ent;
   6928   struct ppc_link_hash_entry *fh;
   6929   struct ppc_link_hash_entry *fdh;
   6930   bfd_boolean force_local;
   6931 
   6932   fh = (struct ppc_link_hash_entry *) h;
   6933   if (fh->elf.root.type == bfd_link_hash_indirect)
   6934     return TRUE;
   6935 
   6936   info = inf;
   6937   htab = ppc_hash_table (info);
   6938   if (htab == NULL)
   6939     return FALSE;
   6940 
   6941   /* Resolve undefined references to dot-symbols as the value
   6942      in the function descriptor, if we have one in a regular object.
   6943      This is to satisfy cases like ".quad .foo".  Calls to functions
   6944      in dynamic objects are handled elsewhere.  */
   6945   if (fh->elf.root.type == bfd_link_hash_undefweak
   6946       && fh->was_undefined
   6947       && (fdh = defined_func_desc (fh)) != NULL
   6948       && get_opd_info (fdh->elf.root.u.def.section) != NULL
   6949       && opd_entry_value (fdh->elf.root.u.def.section,
   6950 			  fdh->elf.root.u.def.value,
   6951 			  &fh->elf.root.u.def.section,
   6952 			  &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
   6953     {
   6954       fh->elf.root.type = fdh->elf.root.type;
   6955       fh->elf.forced_local = 1;
   6956       fh->elf.def_regular = fdh->elf.def_regular;
   6957       fh->elf.def_dynamic = fdh->elf.def_dynamic;
   6958     }
   6959 
   6960   /* If this is a function code symbol, transfer dynamic linking
   6961      information to the function descriptor symbol.  */
   6962   if (!fh->is_func)
   6963     return TRUE;
   6964 
   6965   for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
   6966     if (ent->plt.refcount > 0)
   6967       break;
   6968   if (ent == NULL
   6969       || fh->elf.root.root.string[0] != '.'
   6970       || fh->elf.root.root.string[1] == '\0')
   6971     return TRUE;
   6972 
   6973   /* Find the corresponding function descriptor symbol.  Create it
   6974      as undefined if necessary.  */
   6975 
   6976   fdh = lookup_fdh (fh, htab);
   6977   if (fdh == NULL
   6978       && !bfd_link_executable (info)
   6979       && (fh->elf.root.type == bfd_link_hash_undefined
   6980 	  || fh->elf.root.type == bfd_link_hash_undefweak))
   6981     {
   6982       fdh = make_fdh (info, fh);
   6983       if (fdh == NULL)
   6984 	return FALSE;
   6985     }
   6986 
   6987   /* Fake function descriptors are made undefweak.  If the function
   6988      code symbol is strong undefined, make the fake sym the same.
   6989      If the function code symbol is defined, then force the fake
   6990      descriptor local;  We can't support overriding of symbols in a
   6991      shared library on a fake descriptor.  */
   6992 
   6993   if (fdh != NULL
   6994       && fdh->fake
   6995       && fdh->elf.root.type == bfd_link_hash_undefweak)
   6996     {
   6997       if (fh->elf.root.type == bfd_link_hash_undefined)
   6998 	{
   6999 	  fdh->elf.root.type = bfd_link_hash_undefined;
   7000 	  bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
   7001 	}
   7002       else if (fh->elf.root.type == bfd_link_hash_defined
   7003 	       || fh->elf.root.type == bfd_link_hash_defweak)
   7004 	{
   7005 	  _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
   7006 	}
   7007     }
   7008 
   7009   if (fdh != NULL
   7010       && !fdh->elf.forced_local
   7011       && (!bfd_link_executable (info)
   7012 	  || fdh->elf.def_dynamic
   7013 	  || fdh->elf.ref_dynamic
   7014 	  || (fdh->elf.root.type == bfd_link_hash_undefweak
   7015 	      && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
   7016     {
   7017       if (fdh->elf.dynindx == -1)
   7018 	if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
   7019 	  return FALSE;
   7020       fdh->elf.ref_regular |= fh->elf.ref_regular;
   7021       fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
   7022       fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
   7023       fdh->elf.non_got_ref |= fh->elf.non_got_ref;
   7024       if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
   7025 	{
   7026 	  move_plt_plist (fh, fdh);
   7027 	  fdh->elf.needs_plt = 1;
   7028 	}
   7029       fdh->is_func_descriptor = 1;
   7030       fdh->oh = fh;
   7031       fh->oh = fdh;
   7032     }
   7033 
   7034   /* Now that the info is on the function descriptor, clear the
   7035      function code sym info.  Any function code syms for which we
   7036      don't have a definition in a regular file, we force local.
   7037      This prevents a shared library from exporting syms that have
   7038      been imported from another library.  Function code syms that
   7039      are really in the library we must leave global to prevent the
   7040      linker dragging in a definition from a static library.  */
   7041   force_local = (!fh->elf.def_regular
   7042 		 || fdh == NULL
   7043 		 || !fdh->elf.def_regular
   7044 		 || fdh->elf.forced_local);
   7045   _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   7046 
   7047   return TRUE;
   7048 }
   7049 
   7050 static const struct sfpr_def_parms save_res_funcs[] =
   7051   {
   7052     { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
   7053     { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
   7054     { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
   7055     { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
   7056     { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
   7057     { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
   7058     { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
   7059     { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
   7060     { "._savef", 14, 31, savefpr, savefpr1_tail },
   7061     { "._restf", 14, 31, restfpr, restfpr1_tail },
   7062     { "_savevr_", 20, 31, savevr, savevr_tail },
   7063     { "_restvr_", 20, 31, restvr, restvr_tail }
   7064   };
   7065 
   7066 /* Called near the start of bfd_elf_size_dynamic_sections.  We use
   7067    this hook to a) provide some gcc support functions, and b) transfer
   7068    dynamic linking information gathered so far on function code symbol
   7069    entries, to their corresponding function descriptor symbol entries.  */
   7070 
   7071 static bfd_boolean
   7072 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
   7073 			    struct bfd_link_info *info)
   7074 {
   7075   struct ppc_link_hash_table *htab;
   7076 
   7077   htab = ppc_hash_table (info);
   7078   if (htab == NULL)
   7079     return FALSE;
   7080 
   7081   /* Provide any missing _save* and _rest* functions.  */
   7082   if (htab->sfpr != NULL)
   7083     {
   7084       unsigned int i;
   7085 
   7086       htab->sfpr->size = 0;
   7087       for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
   7088 	if (!sfpr_define (info, &save_res_funcs[i], NULL))
   7089 	  return FALSE;
   7090       if (htab->sfpr->size == 0)
   7091 	htab->sfpr->flags |= SEC_EXCLUDE;
   7092     }
   7093 
   7094   if (bfd_link_relocatable (info))
   7095     return TRUE;
   7096 
   7097   if (htab->elf.hgot != NULL)
   7098     {
   7099       _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
   7100       /* Make .TOC. defined so as to prevent it being made dynamic.
   7101 	 The wrong value here is fixed later in ppc64_elf_set_toc.  */
   7102       if (!htab->elf.hgot->def_regular
   7103 	  || htab->elf.hgot->root.type != bfd_link_hash_defined)
   7104 	{
   7105 	  htab->elf.hgot->root.type = bfd_link_hash_defined;
   7106 	  htab->elf.hgot->root.u.def.value = 0;
   7107 	  htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
   7108 	  htab->elf.hgot->def_regular = 1;
   7109 	  htab->elf.hgot->root.linker_def = 1;
   7110 	}
   7111       htab->elf.hgot->type = STT_OBJECT;
   7112       htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
   7113 			       | STV_HIDDEN);
   7114     }
   7115 
   7116   elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
   7117 
   7118   return TRUE;
   7119 }
   7120 
   7121 /* Return true if we have dynamic relocs against H that apply to
   7122    read-only sections.  */
   7123 
   7124 static bfd_boolean
   7125 readonly_dynrelocs (struct elf_link_hash_entry *h)
   7126 {
   7127   struct ppc_link_hash_entry *eh;
   7128   struct elf_dyn_relocs *p;
   7129 
   7130   eh = (struct ppc_link_hash_entry *) h;
   7131   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   7132     {
   7133       asection *s = p->sec->output_section;
   7134 
   7135       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   7136 	return TRUE;
   7137     }
   7138   return FALSE;
   7139 }
   7140 
   7141 
   7142 /* Return true if a global entry stub will be created for H.  Valid
   7143    for ELFv2 before plt entries have been allocated.  */
   7144 
   7145 static bfd_boolean
   7146 global_entry_stub (struct elf_link_hash_entry *h)
   7147 {
   7148   struct plt_entry *pent;
   7149 
   7150   if (!h->pointer_equality_needed
   7151       || h->def_regular)
   7152     return FALSE;
   7153 
   7154   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   7155     if (pent->plt.refcount > 0
   7156 	&& pent->addend == 0)
   7157       return TRUE;
   7158 
   7159   return FALSE;
   7160 }
   7161 
   7162 /* Adjust a symbol defined by a dynamic object and referenced by a
   7163    regular object.  The current definition is in some section of the
   7164    dynamic object, but we're not including those sections.  We have to
   7165    change the definition to something the rest of the link can
   7166    understand.  */
   7167 
   7168 static bfd_boolean
   7169 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   7170 				 struct elf_link_hash_entry *h)
   7171 {
   7172   struct ppc_link_hash_table *htab;
   7173   asection *s;
   7174 
   7175   htab = ppc_hash_table (info);
   7176   if (htab == NULL)
   7177     return FALSE;
   7178 
   7179   /* Deal with function syms.  */
   7180   if (h->type == STT_FUNC
   7181       || h->type == STT_GNU_IFUNC
   7182       || h->needs_plt)
   7183     {
   7184       /* Clear procedure linkage table information for any symbol that
   7185 	 won't need a .plt entry.  */
   7186       struct plt_entry *ent;
   7187       for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   7188 	if (ent->plt.refcount > 0)
   7189 	  break;
   7190       if (ent == NULL
   7191 	  || (h->type != STT_GNU_IFUNC
   7192 	      && (SYMBOL_CALLS_LOCAL (info, h)
   7193 		  || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
   7194 		      && h->root.type == bfd_link_hash_undefweak)))
   7195 	  || ((struct ppc_link_hash_entry *) h)->save_res)
   7196 	{
   7197 	  h->plt.plist = NULL;
   7198 	  h->needs_plt = 0;
   7199 	  h->pointer_equality_needed = 0;
   7200 	}
   7201       else if (abiversion (info->output_bfd) >= 2)
   7202 	{
   7203 	  /* Taking a function's address in a read/write section
   7204 	     doesn't require us to define the function symbol in the
   7205 	     executable on a global entry stub.  A dynamic reloc can
   7206 	     be used instead.  The reason we prefer a few more dynamic
   7207 	     relocs is that calling via a global entry stub costs a
   7208 	     few more instructions, and pointer_equality_needed causes
   7209 	     extra work in ld.so when resolving these symbols.  */
   7210 	  if (global_entry_stub (h)
   7211 	      && !readonly_dynrelocs (h))
   7212 	    {
   7213 	      h->pointer_equality_needed = 0;
   7214 	      /* After adjust_dynamic_symbol, non_got_ref set in
   7215 		 the non-pic case means that dyn_relocs for this
   7216 		 symbol should be discarded.  */
   7217 	      h->non_got_ref = 0;
   7218 	    }
   7219 
   7220 	  /* If making a plt entry, then we don't need copy relocs.  */
   7221 	  return TRUE;
   7222 	}
   7223     }
   7224   else
   7225     h->plt.plist = NULL;
   7226 
   7227   /* If this is a weak symbol, and there is a real definition, the
   7228      processor independent code will have arranged for us to see the
   7229      real definition first, and we can just use the same value.  */
   7230   if (h->u.weakdef != NULL)
   7231     {
   7232       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   7233 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   7234       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   7235       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   7236       if (ELIMINATE_COPY_RELOCS)
   7237 	h->non_got_ref = h->u.weakdef->non_got_ref;
   7238       return TRUE;
   7239     }
   7240 
   7241   /* If we are creating a shared library, we must presume that the
   7242      only references to the symbol are via the global offset table.
   7243      For such cases we need not do anything here; the relocations will
   7244      be handled correctly by relocate_section.  */
   7245   if (bfd_link_pic (info))
   7246     return TRUE;
   7247 
   7248   /* If there are no references to this symbol that do not use the
   7249      GOT, we don't need to generate a copy reloc.  */
   7250   if (!h->non_got_ref)
   7251     return TRUE;
   7252 
   7253   /* Don't generate a copy reloc for symbols defined in the executable.  */
   7254   if (!h->def_dynamic || !h->ref_regular || h->def_regular
   7255 
   7256       /* If -z nocopyreloc was given, don't generate them either.  */
   7257       || info->nocopyreloc
   7258 
   7259       /* If we didn't find any dynamic relocs in read-only sections, then
   7260 	 we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
   7261       || (ELIMINATE_COPY_RELOCS && !readonly_dynrelocs (h))
   7262 
   7263       /* Protected variables do not work with .dynbss.  The copy in
   7264 	 .dynbss won't be used by the shared library with the protected
   7265 	 definition for the variable.  Text relocations are preferable
   7266 	 to an incorrect program.  */
   7267       || h->protected_def)
   7268     {
   7269       h->non_got_ref = 0;
   7270       return TRUE;
   7271     }
   7272 
   7273   if (h->plt.plist != NULL)
   7274     {
   7275       /* We should never get here, but unfortunately there are versions
   7276 	 of gcc out there that improperly (for this ABI) put initialized
   7277 	 function pointers, vtable refs and suchlike in read-only
   7278 	 sections.  Allow them to proceed, but warn that this might
   7279 	 break at runtime.  */
   7280       info->callbacks->einfo
   7281 	(_("%P: copy reloc against `%T' requires lazy plt linking; "
   7282 	   "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
   7283 	 h->root.root.string);
   7284     }
   7285 
   7286   /* This is a reference to a symbol defined by a dynamic object which
   7287      is not a function.  */
   7288 
   7289   /* We must allocate the symbol in our .dynbss section, which will
   7290      become part of the .bss section of the executable.  There will be
   7291      an entry for this symbol in the .dynsym section.  The dynamic
   7292      object will contain position independent code, so all references
   7293      from the dynamic object to this symbol will go through the global
   7294      offset table.  The dynamic linker will use the .dynsym entry to
   7295      determine the address it must put in the global offset table, so
   7296      both the dynamic object and the regular object will refer to the
   7297      same memory location for the variable.  */
   7298 
   7299   /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
   7300      to copy the initial value out of the dynamic object and into the
   7301      runtime process image.  We need to remember the offset into the
   7302      .rela.bss section we are going to use.  */
   7303   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
   7304     {
   7305       htab->relbss->size += sizeof (Elf64_External_Rela);
   7306       h->needs_copy = 1;
   7307     }
   7308 
   7309   s = htab->dynbss;
   7310 
   7311   return _bfd_elf_adjust_dynamic_copy (info, h, s);
   7312 }
   7313 
   7314 /* If given a function descriptor symbol, hide both the function code
   7315    sym and the descriptor.  */
   7316 static void
   7317 ppc64_elf_hide_symbol (struct bfd_link_info *info,
   7318 		       struct elf_link_hash_entry *h,
   7319 		       bfd_boolean force_local)
   7320 {
   7321   struct ppc_link_hash_entry *eh;
   7322   _bfd_elf_link_hash_hide_symbol (info, h, force_local);
   7323 
   7324   eh = (struct ppc_link_hash_entry *) h;
   7325   if (eh->is_func_descriptor)
   7326     {
   7327       struct ppc_link_hash_entry *fh = eh->oh;
   7328 
   7329       if (fh == NULL)
   7330 	{
   7331 	  const char *p, *q;
   7332 	  struct ppc_link_hash_table *htab;
   7333 	  char save;
   7334 
   7335 	  /* We aren't supposed to use alloca in BFD because on
   7336 	     systems which do not have alloca the version in libiberty
   7337 	     calls xmalloc, which might cause the program to crash
   7338 	     when it runs out of memory.  This function doesn't have a
   7339 	     return status, so there's no way to gracefully return an
   7340 	     error.  So cheat.  We know that string[-1] can be safely
   7341 	     accessed;  It's either a string in an ELF string table,
   7342 	     or allocated in an objalloc structure.  */
   7343 
   7344 	  p = eh->elf.root.root.string - 1;
   7345 	  save = *p;
   7346 	  *(char *) p = '.';
   7347 	  htab = ppc_hash_table (info);
   7348 	  if (htab == NULL)
   7349 	    return;
   7350 
   7351 	  fh = (struct ppc_link_hash_entry *)
   7352 	    elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   7353 	  *(char *) p = save;
   7354 
   7355 	  /* Unfortunately, if it so happens that the string we were
   7356 	     looking for was allocated immediately before this string,
   7357 	     then we overwrote the string terminator.  That's the only
   7358 	     reason the lookup should fail.  */
   7359 	  if (fh == NULL)
   7360 	    {
   7361 	      q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
   7362 	      while (q >= eh->elf.root.root.string && *q == *p)
   7363 		--q, --p;
   7364 	      if (q < eh->elf.root.root.string && *p == '.')
   7365 		fh = (struct ppc_link_hash_entry *)
   7366 		  elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
   7367 	    }
   7368 	  if (fh != NULL)
   7369 	    {
   7370 	      eh->oh = fh;
   7371 	      fh->oh = eh;
   7372 	    }
   7373 	}
   7374       if (fh != NULL)
   7375 	_bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
   7376     }
   7377 }
   7378 
   7379 static bfd_boolean
   7380 get_sym_h (struct elf_link_hash_entry **hp,
   7381 	   Elf_Internal_Sym **symp,
   7382 	   asection **symsecp,
   7383 	   unsigned char **tls_maskp,
   7384 	   Elf_Internal_Sym **locsymsp,
   7385 	   unsigned long r_symndx,
   7386 	   bfd *ibfd)
   7387 {
   7388   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   7389 
   7390   if (r_symndx >= symtab_hdr->sh_info)
   7391     {
   7392       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   7393       struct elf_link_hash_entry *h;
   7394 
   7395       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   7396       h = elf_follow_link (h);
   7397 
   7398       if (hp != NULL)
   7399 	*hp = h;
   7400 
   7401       if (symp != NULL)
   7402 	*symp = NULL;
   7403 
   7404       if (symsecp != NULL)
   7405 	{
   7406 	  asection *symsec = NULL;
   7407 	  if (h->root.type == bfd_link_hash_defined
   7408 	      || h->root.type == bfd_link_hash_defweak)
   7409 	    symsec = h->root.u.def.section;
   7410 	  *symsecp = symsec;
   7411 	}
   7412 
   7413       if (tls_maskp != NULL)
   7414 	{
   7415 	  struct ppc_link_hash_entry *eh;
   7416 
   7417 	  eh = (struct ppc_link_hash_entry *) h;
   7418 	  *tls_maskp = &eh->tls_mask;
   7419 	}
   7420     }
   7421   else
   7422     {
   7423       Elf_Internal_Sym *sym;
   7424       Elf_Internal_Sym *locsyms = *locsymsp;
   7425 
   7426       if (locsyms == NULL)
   7427 	{
   7428 	  locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
   7429 	  if (locsyms == NULL)
   7430 	    locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
   7431 					    symtab_hdr->sh_info,
   7432 					    0, NULL, NULL, NULL);
   7433 	  if (locsyms == NULL)
   7434 	    return FALSE;
   7435 	  *locsymsp = locsyms;
   7436 	}
   7437       sym = locsyms + r_symndx;
   7438 
   7439       if (hp != NULL)
   7440 	*hp = NULL;
   7441 
   7442       if (symp != NULL)
   7443 	*symp = sym;
   7444 
   7445       if (symsecp != NULL)
   7446 	*symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
   7447 
   7448       if (tls_maskp != NULL)
   7449 	{
   7450 	  struct got_entry **lgot_ents;
   7451 	  unsigned char *tls_mask;
   7452 
   7453 	  tls_mask = NULL;
   7454 	  lgot_ents = elf_local_got_ents (ibfd);
   7455 	  if (lgot_ents != NULL)
   7456 	    {
   7457 	      struct plt_entry **local_plt = (struct plt_entry **)
   7458 		(lgot_ents + symtab_hdr->sh_info);
   7459 	      unsigned char *lgot_masks = (unsigned char *)
   7460 		(local_plt + symtab_hdr->sh_info);
   7461 	      tls_mask = &lgot_masks[r_symndx];
   7462 	    }
   7463 	  *tls_maskp = tls_mask;
   7464 	}
   7465     }
   7466   return TRUE;
   7467 }
   7468 
   7469 /* Returns TLS_MASKP for the given REL symbol.  Function return is 0 on
   7470    error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
   7471    type suitable for optimization, and 1 otherwise.  */
   7472 
   7473 static int
   7474 get_tls_mask (unsigned char **tls_maskp,
   7475 	      unsigned long *toc_symndx,
   7476 	      bfd_vma *toc_addend,
   7477 	      Elf_Internal_Sym **locsymsp,
   7478 	      const Elf_Internal_Rela *rel,
   7479 	      bfd *ibfd)
   7480 {
   7481   unsigned long r_symndx;
   7482   int next_r;
   7483   struct elf_link_hash_entry *h;
   7484   Elf_Internal_Sym *sym;
   7485   asection *sec;
   7486   bfd_vma off;
   7487 
   7488   r_symndx = ELF64_R_SYM (rel->r_info);
   7489   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   7490     return 0;
   7491 
   7492   if ((*tls_maskp != NULL && **tls_maskp != 0)
   7493       || sec == NULL
   7494       || ppc64_elf_section_data (sec) == NULL
   7495       || ppc64_elf_section_data (sec)->sec_type != sec_toc)
   7496     return 1;
   7497 
   7498   /* Look inside a TOC section too.  */
   7499   if (h != NULL)
   7500     {
   7501       BFD_ASSERT (h->root.type == bfd_link_hash_defined);
   7502       off = h->root.u.def.value;
   7503     }
   7504   else
   7505     off = sym->st_value;
   7506   off += rel->r_addend;
   7507   BFD_ASSERT (off % 8 == 0);
   7508   r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
   7509   next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
   7510   if (toc_symndx != NULL)
   7511     *toc_symndx = r_symndx;
   7512   if (toc_addend != NULL)
   7513     *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
   7514   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
   7515     return 0;
   7516   if ((h == NULL || is_static_defined (h))
   7517       && (next_r == -1 || next_r == -2))
   7518     return 1 - next_r;
   7519   return 1;
   7520 }
   7521 
   7522 /* Find (or create) an entry in the tocsave hash table.  */
   7523 
   7524 static struct tocsave_entry *
   7525 tocsave_find (struct ppc_link_hash_table *htab,
   7526 	      enum insert_option insert,
   7527 	      Elf_Internal_Sym **local_syms,
   7528 	      const Elf_Internal_Rela *irela,
   7529 	      bfd *ibfd)
   7530 {
   7531   unsigned long r_indx;
   7532   struct elf_link_hash_entry *h;
   7533   Elf_Internal_Sym *sym;
   7534   struct tocsave_entry ent, *p;
   7535   hashval_t hash;
   7536   struct tocsave_entry **slot;
   7537 
   7538   r_indx = ELF64_R_SYM (irela->r_info);
   7539   if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
   7540     return NULL;
   7541   if (ent.sec == NULL || ent.sec->output_section == NULL)
   7542     {
   7543       (*_bfd_error_handler)
   7544 	(_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
   7545       return NULL;
   7546     }
   7547 
   7548   if (h != NULL)
   7549     ent.offset = h->root.u.def.value;
   7550   else
   7551     ent.offset = sym->st_value;
   7552   ent.offset += irela->r_addend;
   7553 
   7554   hash = tocsave_htab_hash (&ent);
   7555   slot = ((struct tocsave_entry **)
   7556 	  htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
   7557   if (slot == NULL)
   7558     return NULL;
   7559 
   7560   if (*slot == NULL)
   7561     {
   7562       p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
   7563       if (p == NULL)
   7564 	return NULL;
   7565       *p = ent;
   7566       *slot = p;
   7567     }
   7568   return *slot;
   7569 }
   7570 
   7571 /* Adjust all global syms defined in opd sections.  In gcc generated
   7572    code for the old ABI, these will already have been done.  */
   7573 
   7574 static bfd_boolean
   7575 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   7576 {
   7577   struct ppc_link_hash_entry *eh;
   7578   asection *sym_sec;
   7579   struct _opd_sec_data *opd;
   7580 
   7581   if (h->root.type == bfd_link_hash_indirect)
   7582     return TRUE;
   7583 
   7584   if (h->root.type != bfd_link_hash_defined
   7585       && h->root.type != bfd_link_hash_defweak)
   7586     return TRUE;
   7587 
   7588   eh = (struct ppc_link_hash_entry *) h;
   7589   if (eh->adjust_done)
   7590     return TRUE;
   7591 
   7592   sym_sec = eh->elf.root.u.def.section;
   7593   opd = get_opd_info (sym_sec);
   7594   if (opd != NULL && opd->adjust != NULL)
   7595     {
   7596       long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
   7597       if (adjust == -1)
   7598 	{
   7599 	  /* This entry has been deleted.  */
   7600 	  asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
   7601 	  if (dsec == NULL)
   7602 	    {
   7603 	      for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
   7604 		if (discarded_section (dsec))
   7605 		  {
   7606 		    ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
   7607 		    break;
   7608 		  }
   7609 	    }
   7610 	  eh->elf.root.u.def.value = 0;
   7611 	  eh->elf.root.u.def.section = dsec;
   7612 	}
   7613       else
   7614 	eh->elf.root.u.def.value += adjust;
   7615       eh->adjust_done = 1;
   7616     }
   7617   return TRUE;
   7618 }
   7619 
   7620 /* Handles decrementing dynamic reloc counts for the reloc specified by
   7621    R_INFO in section SEC.  If LOCAL_SYMS is NULL, then H and SYM
   7622    have already been determined.  */
   7623 
   7624 static bfd_boolean
   7625 dec_dynrel_count (bfd_vma r_info,
   7626 		  asection *sec,
   7627 		  struct bfd_link_info *info,
   7628 		  Elf_Internal_Sym **local_syms,
   7629 		  struct elf_link_hash_entry *h,
   7630 		  Elf_Internal_Sym *sym)
   7631 {
   7632   enum elf_ppc64_reloc_type r_type;
   7633   asection *sym_sec = NULL;
   7634 
   7635   /* Can this reloc be dynamic?  This switch, and later tests here
   7636      should be kept in sync with the code in check_relocs.  */
   7637   r_type = ELF64_R_TYPE (r_info);
   7638   switch (r_type)
   7639     {
   7640     default:
   7641       return TRUE;
   7642 
   7643     case R_PPC64_TPREL16:
   7644     case R_PPC64_TPREL16_LO:
   7645     case R_PPC64_TPREL16_HI:
   7646     case R_PPC64_TPREL16_HA:
   7647     case R_PPC64_TPREL16_DS:
   7648     case R_PPC64_TPREL16_LO_DS:
   7649     case R_PPC64_TPREL16_HIGH:
   7650     case R_PPC64_TPREL16_HIGHA:
   7651     case R_PPC64_TPREL16_HIGHER:
   7652     case R_PPC64_TPREL16_HIGHERA:
   7653     case R_PPC64_TPREL16_HIGHEST:
   7654     case R_PPC64_TPREL16_HIGHESTA:
   7655       if (!bfd_link_pic (info))
   7656 	return TRUE;
   7657 
   7658     case R_PPC64_TPREL64:
   7659     case R_PPC64_DTPMOD64:
   7660     case R_PPC64_DTPREL64:
   7661     case R_PPC64_ADDR64:
   7662     case R_PPC64_REL30:
   7663     case R_PPC64_REL32:
   7664     case R_PPC64_REL64:
   7665     case R_PPC64_ADDR14:
   7666     case R_PPC64_ADDR14_BRNTAKEN:
   7667     case R_PPC64_ADDR14_BRTAKEN:
   7668     case R_PPC64_ADDR16:
   7669     case R_PPC64_ADDR16_DS:
   7670     case R_PPC64_ADDR16_HA:
   7671     case R_PPC64_ADDR16_HI:
   7672     case R_PPC64_ADDR16_HIGH:
   7673     case R_PPC64_ADDR16_HIGHA:
   7674     case R_PPC64_ADDR16_HIGHER:
   7675     case R_PPC64_ADDR16_HIGHERA:
   7676     case R_PPC64_ADDR16_HIGHEST:
   7677     case R_PPC64_ADDR16_HIGHESTA:
   7678     case R_PPC64_ADDR16_LO:
   7679     case R_PPC64_ADDR16_LO_DS:
   7680     case R_PPC64_ADDR24:
   7681     case R_PPC64_ADDR32:
   7682     case R_PPC64_UADDR16:
   7683     case R_PPC64_UADDR32:
   7684     case R_PPC64_UADDR64:
   7685     case R_PPC64_TOC:
   7686       break;
   7687     }
   7688 
   7689   if (local_syms != NULL)
   7690     {
   7691       unsigned long r_symndx;
   7692       bfd *ibfd = sec->owner;
   7693 
   7694       r_symndx = ELF64_R_SYM (r_info);
   7695       if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
   7696 	return FALSE;
   7697     }
   7698 
   7699   if ((bfd_link_pic (info)
   7700        && (must_be_dyn_reloc (info, r_type)
   7701 	   || (h != NULL
   7702 	       && (!SYMBOLIC_BIND (info, h)
   7703 		   || h->root.type == bfd_link_hash_defweak
   7704 		   || !h->def_regular))))
   7705       || (ELIMINATE_COPY_RELOCS
   7706 	  && !bfd_link_pic (info)
   7707 	  && h != NULL
   7708 	  && (h->root.type == bfd_link_hash_defweak
   7709 	      || !h->def_regular)))
   7710     ;
   7711   else
   7712     return TRUE;
   7713 
   7714   if (h != NULL)
   7715     {
   7716       struct elf_dyn_relocs *p;
   7717       struct elf_dyn_relocs **pp;
   7718       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
   7719 
   7720       /* elf_gc_sweep may have already removed all dyn relocs associated
   7721 	 with local syms for a given section.  Also, symbol flags are
   7722 	 changed by elf_gc_sweep_symbol, confusing the test above.  Don't
   7723 	 report a dynreloc miscount.  */
   7724       if (*pp == NULL && info->gc_sections)
   7725 	return TRUE;
   7726 
   7727       while ((p = *pp) != NULL)
   7728 	{
   7729 	  if (p->sec == sec)
   7730 	    {
   7731 	      if (!must_be_dyn_reloc (info, r_type))
   7732 		p->pc_count -= 1;
   7733 	      p->count -= 1;
   7734 	      if (p->count == 0)
   7735 		*pp = p->next;
   7736 	      return TRUE;
   7737 	    }
   7738 	  pp = &p->next;
   7739 	}
   7740     }
   7741   else
   7742     {
   7743       struct ppc_dyn_relocs *p;
   7744       struct ppc_dyn_relocs **pp;
   7745       void *vpp;
   7746       bfd_boolean is_ifunc;
   7747 
   7748       if (local_syms == NULL)
   7749 	sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
   7750       if (sym_sec == NULL)
   7751 	sym_sec = sec;
   7752 
   7753       vpp = &elf_section_data (sym_sec)->local_dynrel;
   7754       pp = (struct ppc_dyn_relocs **) vpp;
   7755 
   7756       if (*pp == NULL && info->gc_sections)
   7757 	return TRUE;
   7758 
   7759       is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
   7760       while ((p = *pp) != NULL)
   7761 	{
   7762 	  if (p->sec == sec && p->ifunc == is_ifunc)
   7763 	    {
   7764 	      p->count -= 1;
   7765 	      if (p->count == 0)
   7766 		*pp = p->next;
   7767 	      return TRUE;
   7768 	    }
   7769 	  pp = &p->next;
   7770 	}
   7771     }
   7772 
   7773   info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
   7774 			  sec->owner, sec);
   7775   bfd_set_error (bfd_error_bad_value);
   7776   return FALSE;
   7777 }
   7778 
   7779 /* Remove unused Official Procedure Descriptor entries.  Currently we
   7780    only remove those associated with functions in discarded link-once
   7781    sections, or weakly defined functions that have been overridden.  It
   7782    would be possible to remove many more entries for statically linked
   7783    applications.  */
   7784 
   7785 bfd_boolean
   7786 ppc64_elf_edit_opd (struct bfd_link_info *info)
   7787 {
   7788   bfd *ibfd;
   7789   bfd_boolean some_edited = FALSE;
   7790   asection *need_pad = NULL;
   7791   struct ppc_link_hash_table *htab;
   7792 
   7793   htab = ppc_hash_table (info);
   7794   if (htab == NULL)
   7795     return FALSE;
   7796 
   7797   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   7798     {
   7799       asection *sec;
   7800       Elf_Internal_Rela *relstart, *rel, *relend;
   7801       Elf_Internal_Shdr *symtab_hdr;
   7802       Elf_Internal_Sym *local_syms;
   7803       struct _opd_sec_data *opd;
   7804       bfd_boolean need_edit, add_aux_fields, broken;
   7805       bfd_size_type cnt_16b = 0;
   7806 
   7807       if (!is_ppc64_elf (ibfd))
   7808 	continue;
   7809 
   7810       sec = bfd_get_section_by_name (ibfd, ".opd");
   7811       if (sec == NULL || sec->size == 0)
   7812 	continue;
   7813 
   7814       if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
   7815 	continue;
   7816 
   7817       if (sec->output_section == bfd_abs_section_ptr)
   7818 	continue;
   7819 
   7820       /* Look through the section relocs.  */
   7821       if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
   7822 	continue;
   7823 
   7824       local_syms = NULL;
   7825       symtab_hdr = &elf_symtab_hdr (ibfd);
   7826 
   7827       /* Read the relocations.  */
   7828       relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   7829 					    info->keep_memory);
   7830       if (relstart == NULL)
   7831 	return FALSE;
   7832 
   7833       /* First run through the relocs to check they are sane, and to
   7834 	 determine whether we need to edit this opd section.  */
   7835       need_edit = FALSE;
   7836       broken = FALSE;
   7837       need_pad = sec;
   7838       relend = relstart + sec->reloc_count;
   7839       for (rel = relstart; rel < relend; )
   7840 	{
   7841 	  enum elf_ppc64_reloc_type r_type;
   7842 	  unsigned long r_symndx;
   7843 	  asection *sym_sec;
   7844 	  struct elf_link_hash_entry *h;
   7845 	  Elf_Internal_Sym *sym;
   7846 	  bfd_vma offset;
   7847 
   7848 	  /* .opd contains an array of 16 or 24 byte entries.  We're
   7849 	     only interested in the reloc pointing to a function entry
   7850 	     point.  */
   7851 	  offset = rel->r_offset;
   7852 	  if (rel + 1 == relend
   7853 	      || rel[1].r_offset != offset + 8)
   7854 	    {
   7855 	      /* If someone messes with .opd alignment then after a
   7856 		 "ld -r" we might have padding in the middle of .opd.
   7857 		 Also, there's nothing to prevent someone putting
   7858 		 something silly in .opd with the assembler.  No .opd
   7859 		 optimization for them!  */
   7860 	    broken_opd:
   7861 	      (*_bfd_error_handler)
   7862 		(_("%B: .opd is not a regular array of opd entries"), ibfd);
   7863 	      broken = TRUE;
   7864 	      break;
   7865 	    }
   7866 
   7867 	  if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
   7868 	      || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
   7869 	    {
   7870 	      (*_bfd_error_handler)
   7871 		(_("%B: unexpected reloc type %u in .opd section"),
   7872 		 ibfd, r_type);
   7873 	      broken = TRUE;
   7874 	      break;
   7875 	    }
   7876 
   7877 	  r_symndx = ELF64_R_SYM (rel->r_info);
   7878 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   7879 			  r_symndx, ibfd))
   7880 	    goto error_ret;
   7881 
   7882 	  if (sym_sec == NULL || sym_sec->owner == NULL)
   7883 	    {
   7884 	      const char *sym_name;
   7885 	      if (h != NULL)
   7886 		sym_name = h->root.root.string;
   7887 	      else
   7888 		sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
   7889 					     sym_sec);
   7890 
   7891 	      (*_bfd_error_handler)
   7892 		(_("%B: undefined sym `%s' in .opd section"),
   7893 		 ibfd, sym_name);
   7894 	      broken = TRUE;
   7895 	      break;
   7896 	    }
   7897 
   7898 	  /* opd entries are always for functions defined in the
   7899 	     current input bfd.  If the symbol isn't defined in the
   7900 	     input bfd, then we won't be using the function in this
   7901 	     bfd;  It must be defined in a linkonce section in another
   7902 	     bfd, or is weak.  It's also possible that we are
   7903 	     discarding the function due to a linker script /DISCARD/,
   7904 	     which we test for via the output_section.  */
   7905 	  if (sym_sec->owner != ibfd
   7906 	      || sym_sec->output_section == bfd_abs_section_ptr)
   7907 	    need_edit = TRUE;
   7908 
   7909 	  rel += 2;
   7910 	  if (rel + 1 == relend
   7911 	      || (rel + 2 < relend
   7912 		  && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
   7913 	    ++rel;
   7914 
   7915 	  if (rel == relend)
   7916 	    {
   7917 	      if (sec->size == offset + 24)
   7918 		{
   7919 		  need_pad = NULL;
   7920 		  break;
   7921 		}
   7922 	      if (sec->size == offset + 16)
   7923 		{
   7924 		  cnt_16b++;
   7925 		  break;
   7926 		}
   7927 	      goto broken_opd;
   7928 	    }
   7929 	  else if (rel + 1 < relend
   7930 		   && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
   7931 		   && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
   7932 	    {
   7933 	      if (rel[0].r_offset == offset + 16)
   7934 		cnt_16b++;
   7935 	      else if (rel[0].r_offset != offset + 24)
   7936 		goto broken_opd;
   7937 	    }
   7938 	  else
   7939 	    goto broken_opd;
   7940 	}
   7941 
   7942       add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
   7943 
   7944       if (!broken && (need_edit || add_aux_fields))
   7945 	{
   7946 	  Elf_Internal_Rela *write_rel;
   7947 	  Elf_Internal_Shdr *rel_hdr;
   7948 	  bfd_byte *rptr, *wptr;
   7949 	  bfd_byte *new_contents;
   7950 	  bfd_size_type amt;
   7951 
   7952 	  new_contents = NULL;
   7953 	  amt = OPD_NDX (sec->size) * sizeof (long);
   7954 	  opd = &ppc64_elf_section_data (sec)->u.opd;
   7955 	  opd->adjust = bfd_zalloc (sec->owner, amt);
   7956 	  if (opd->adjust == NULL)
   7957 	    return FALSE;
   7958 	  ppc64_elf_section_data (sec)->sec_type = sec_opd;
   7959 
   7960 	  /* This seems a waste of time as input .opd sections are all
   7961 	     zeros as generated by gcc, but I suppose there's no reason
   7962 	     this will always be so.  We might start putting something in
   7963 	     the third word of .opd entries.  */
   7964 	  if ((sec->flags & SEC_IN_MEMORY) == 0)
   7965 	    {
   7966 	      bfd_byte *loc;
   7967 	      if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
   7968 		{
   7969 		  if (loc != NULL)
   7970 		    free (loc);
   7971 		error_ret:
   7972 		  if (local_syms != NULL
   7973 		      && symtab_hdr->contents != (unsigned char *) local_syms)
   7974 		    free (local_syms);
   7975 		  if (elf_section_data (sec)->relocs != relstart)
   7976 		    free (relstart);
   7977 		  return FALSE;
   7978 		}
   7979 	      sec->contents = loc;
   7980 	      sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   7981 	    }
   7982 
   7983 	  elf_section_data (sec)->relocs = relstart;
   7984 
   7985 	  new_contents = sec->contents;
   7986 	  if (add_aux_fields)
   7987 	    {
   7988 	      new_contents = bfd_malloc (sec->size + cnt_16b * 8);
   7989 	      if (new_contents == NULL)
   7990 		return FALSE;
   7991 	      need_pad = NULL;
   7992 	    }
   7993 	  wptr = new_contents;
   7994 	  rptr = sec->contents;
   7995 	  write_rel = relstart;
   7996 	  for (rel = relstart; rel < relend; )
   7997 	    {
   7998 	      unsigned long r_symndx;
   7999 	      asection *sym_sec;
   8000 	      struct elf_link_hash_entry *h;
   8001 	      struct ppc_link_hash_entry *fdh = NULL;
   8002 	      Elf_Internal_Sym *sym;
   8003 	      long opd_ent_size;
   8004 	      Elf_Internal_Rela *next_rel;
   8005 	      bfd_boolean skip;
   8006 
   8007 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8008 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8009 			      r_symndx, ibfd))
   8010 		goto error_ret;
   8011 
   8012 	      next_rel = rel + 2;
   8013 	      if (next_rel + 1 == relend
   8014 		  || (next_rel + 2 < relend
   8015 		      && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
   8016 		++next_rel;
   8017 
   8018 	      /* See if the .opd entry is full 24 byte or
   8019 		 16 byte (with fd_aux entry overlapped with next
   8020 		 fd_func).  */
   8021 	      opd_ent_size = 24;
   8022 	      if (next_rel == relend)
   8023 		{
   8024 		  if (sec->size == rel->r_offset + 16)
   8025 		    opd_ent_size = 16;
   8026 		}
   8027 	      else if (next_rel->r_offset == rel->r_offset + 16)
   8028 		opd_ent_size = 16;
   8029 
   8030 	      if (h != NULL
   8031 		  && h->root.root.string[0] == '.')
   8032 		{
   8033 		  fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
   8034 		  if (fdh != NULL
   8035 		      && fdh->elf.root.type != bfd_link_hash_defined
   8036 		      && fdh->elf.root.type != bfd_link_hash_defweak)
   8037 		    fdh = NULL;
   8038 		}
   8039 
   8040 	      skip = (sym_sec->owner != ibfd
   8041 		      || sym_sec->output_section == bfd_abs_section_ptr);
   8042 	      if (skip)
   8043 		{
   8044 		  if (fdh != NULL && sym_sec->owner == ibfd)
   8045 		    {
   8046 		      /* Arrange for the function descriptor sym
   8047 			 to be dropped.  */
   8048 		      fdh->elf.root.u.def.value = 0;
   8049 		      fdh->elf.root.u.def.section = sym_sec;
   8050 		    }
   8051 		  opd->adjust[OPD_NDX (rel->r_offset)] = -1;
   8052 
   8053 		  if (NO_OPD_RELOCS || bfd_link_relocatable (info))
   8054 		    rel = next_rel;
   8055 		  else
   8056 		    while (1)
   8057 		      {
   8058 			if (!dec_dynrel_count (rel->r_info, sec, info,
   8059 					       NULL, h, sym))
   8060 			  goto error_ret;
   8061 
   8062 			if (++rel == next_rel)
   8063 			  break;
   8064 
   8065 			r_symndx = ELF64_R_SYM (rel->r_info);
   8066 			if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8067 					r_symndx, ibfd))
   8068 			  goto error_ret;
   8069 		      }
   8070 		}
   8071 	      else
   8072 		{
   8073 		  /* We'll be keeping this opd entry.  */
   8074 		  long adjust;
   8075 
   8076 		  if (fdh != NULL)
   8077 		    {
   8078 		      /* Redefine the function descriptor symbol to
   8079 			 this location in the opd section.  It is
   8080 			 necessary to update the value here rather
   8081 			 than using an array of adjustments as we do
   8082 			 for local symbols, because various places
   8083 			 in the generic ELF code use the value
   8084 			 stored in u.def.value.  */
   8085 		      fdh->elf.root.u.def.value = wptr - new_contents;
   8086 		      fdh->adjust_done = 1;
   8087 		    }
   8088 
   8089 		  /* Local syms are a bit tricky.  We could
   8090 		     tweak them as they can be cached, but
   8091 		     we'd need to look through the local syms
   8092 		     for the function descriptor sym which we
   8093 		     don't have at the moment.  So keep an
   8094 		     array of adjustments.  */
   8095 		  adjust = (wptr - new_contents) - (rptr - sec->contents);
   8096 		  opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
   8097 
   8098 		  if (wptr != rptr)
   8099 		    memcpy (wptr, rptr, opd_ent_size);
   8100 		  wptr += opd_ent_size;
   8101 		  if (add_aux_fields && opd_ent_size == 16)
   8102 		    {
   8103 		      memset (wptr, '\0', 8);
   8104 		      wptr += 8;
   8105 		    }
   8106 
   8107 		  /* We need to adjust any reloc offsets to point to the
   8108 		     new opd entries.  */
   8109 		  for ( ; rel != next_rel; ++rel)
   8110 		    {
   8111 		      rel->r_offset += adjust;
   8112 		      if (write_rel != rel)
   8113 			memcpy (write_rel, rel, sizeof (*rel));
   8114 		      ++write_rel;
   8115 		    }
   8116 		}
   8117 
   8118 	      rptr += opd_ent_size;
   8119 	    }
   8120 
   8121 	  sec->size = wptr - new_contents;
   8122 	  sec->reloc_count = write_rel - relstart;
   8123 	  if (add_aux_fields)
   8124 	    {
   8125 	      free (sec->contents);
   8126 	      sec->contents = new_contents;
   8127 	    }
   8128 
   8129 	  /* Fudge the header size too, as this is used later in
   8130 	     elf_bfd_final_link if we are emitting relocs.  */
   8131 	  rel_hdr = _bfd_elf_single_rel_hdr (sec);
   8132 	  rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
   8133 	  some_edited = TRUE;
   8134 	}
   8135       else if (elf_section_data (sec)->relocs != relstart)
   8136 	free (relstart);
   8137 
   8138       if (local_syms != NULL
   8139 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   8140 	{
   8141 	  if (!info->keep_memory)
   8142 	    free (local_syms);
   8143 	  else
   8144 	    symtab_hdr->contents = (unsigned char *) local_syms;
   8145 	}
   8146     }
   8147 
   8148   if (some_edited)
   8149     elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
   8150 
   8151   /* If we are doing a final link and the last .opd entry is just 16 byte
   8152      long, add a 8 byte padding after it.  */
   8153   if (need_pad != NULL && !bfd_link_relocatable (info))
   8154     {
   8155       bfd_byte *p;
   8156 
   8157       if ((need_pad->flags & SEC_IN_MEMORY) == 0)
   8158 	{
   8159 	  BFD_ASSERT (need_pad->size > 0);
   8160 
   8161 	  p = bfd_malloc (need_pad->size + 8);
   8162 	  if (p == NULL)
   8163 	    return FALSE;
   8164 
   8165 	  if (! bfd_get_section_contents (need_pad->owner, need_pad,
   8166 					  p, 0, need_pad->size))
   8167 	    return FALSE;
   8168 
   8169 	  need_pad->contents = p;
   8170 	  need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
   8171 	}
   8172       else
   8173 	{
   8174 	  p = bfd_realloc (need_pad->contents, need_pad->size + 8);
   8175 	  if (p == NULL)
   8176 	    return FALSE;
   8177 
   8178 	  need_pad->contents = p;
   8179 	}
   8180 
   8181       memset (need_pad->contents + need_pad->size, 0, 8);
   8182       need_pad->size += 8;
   8183     }
   8184 
   8185   return TRUE;
   8186 }
   8187 
   8188 /* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
   8189 
   8190 asection *
   8191 ppc64_elf_tls_setup (struct bfd_link_info *info)
   8192 {
   8193   struct ppc_link_hash_table *htab;
   8194 
   8195   htab = ppc_hash_table (info);
   8196   if (htab == NULL)
   8197     return NULL;
   8198 
   8199   if (abiversion (info->output_bfd) == 1)
   8200     htab->opd_abi = 1;
   8201 
   8202   if (htab->params->no_multi_toc)
   8203     htab->do_multi_toc = 0;
   8204   else if (!htab->do_multi_toc)
   8205     htab->params->no_multi_toc = 1;
   8206 
   8207   htab->tls_get_addr = ((struct ppc_link_hash_entry *)
   8208 			elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
   8209 					      FALSE, FALSE, TRUE));
   8210   /* Move dynamic linking info to the function descriptor sym.  */
   8211   if (htab->tls_get_addr != NULL)
   8212     func_desc_adjust (&htab->tls_get_addr->elf, info);
   8213   htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
   8214 			   elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
   8215 						 FALSE, FALSE, TRUE));
   8216   if (htab->params->tls_get_addr_opt)
   8217     {
   8218       struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
   8219 
   8220       opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
   8221 				  FALSE, FALSE, TRUE);
   8222       if (opt != NULL)
   8223 	func_desc_adjust (opt, info);
   8224       opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
   8225 				     FALSE, FALSE, TRUE);
   8226       if (opt_fd != NULL
   8227 	  && (opt_fd->root.type == bfd_link_hash_defined
   8228 	      || opt_fd->root.type == bfd_link_hash_defweak))
   8229 	{
   8230 	  /* If glibc supports an optimized __tls_get_addr call stub,
   8231 	     signalled by the presence of __tls_get_addr_opt, and we'll
   8232 	     be calling __tls_get_addr via a plt call stub, then
   8233 	     make __tls_get_addr point to __tls_get_addr_opt.  */
   8234 	  tga_fd = &htab->tls_get_addr_fd->elf;
   8235 	  if (htab->elf.dynamic_sections_created
   8236 	      && tga_fd != NULL
   8237 	      && (tga_fd->type == STT_FUNC
   8238 		  || tga_fd->needs_plt)
   8239 	      && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
   8240 		   || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
   8241 		       && tga_fd->root.type == bfd_link_hash_undefweak)))
   8242 	    {
   8243 	      struct plt_entry *ent;
   8244 
   8245 	      for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
   8246 		if (ent->plt.refcount > 0)
   8247 		  break;
   8248 	      if (ent != NULL)
   8249 		{
   8250 		  tga_fd->root.type = bfd_link_hash_indirect;
   8251 		  tga_fd->root.u.i.link = &opt_fd->root;
   8252 		  ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
   8253 		  opt_fd->forced_local = 0;
   8254 		  if (opt_fd->dynindx != -1)
   8255 		    {
   8256 		      /* Use __tls_get_addr_opt in dynamic relocations.  */
   8257 		      opt_fd->dynindx = -1;
   8258 		      _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
   8259 					      opt_fd->dynstr_index);
   8260 		      if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
   8261 			return NULL;
   8262 		    }
   8263 		  htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
   8264 		  tga = &htab->tls_get_addr->elf;
   8265 		  if (opt != NULL && tga != NULL)
   8266 		    {
   8267 		      tga->root.type = bfd_link_hash_indirect;
   8268 		      tga->root.u.i.link = &opt->root;
   8269 		      ppc64_elf_copy_indirect_symbol (info, opt, tga);
   8270 		      opt->forced_local = 0;
   8271 		      _bfd_elf_link_hash_hide_symbol (info, opt,
   8272 						      tga->forced_local);
   8273 		      htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
   8274 		    }
   8275 		  htab->tls_get_addr_fd->oh = htab->tls_get_addr;
   8276 		  htab->tls_get_addr_fd->is_func_descriptor = 1;
   8277 		  if (htab->tls_get_addr != NULL)
   8278 		    {
   8279 		      htab->tls_get_addr->oh = htab->tls_get_addr_fd;
   8280 		      htab->tls_get_addr->is_func = 1;
   8281 		    }
   8282 		}
   8283 	    }
   8284 	}
   8285       else if (htab->params->tls_get_addr_opt < 0)
   8286 	htab->params->tls_get_addr_opt = 0;
   8287     }
   8288   return _bfd_elf_tls_setup (info->output_bfd, info);
   8289 }
   8290 
   8291 /* Return TRUE iff REL is a branch reloc with a global symbol matching
   8292    HASH1 or HASH2.  */
   8293 
   8294 static bfd_boolean
   8295 branch_reloc_hash_match (const bfd *ibfd,
   8296 			 const Elf_Internal_Rela *rel,
   8297 			 const struct ppc_link_hash_entry *hash1,
   8298 			 const struct ppc_link_hash_entry *hash2)
   8299 {
   8300   Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
   8301   enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
   8302   unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
   8303 
   8304   if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
   8305     {
   8306       struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
   8307       struct elf_link_hash_entry *h;
   8308 
   8309       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   8310       h = elf_follow_link (h);
   8311       if (h == &hash1->elf || h == &hash2->elf)
   8312 	return TRUE;
   8313     }
   8314   return FALSE;
   8315 }
   8316 
   8317 /* Run through all the TLS relocs looking for optimization
   8318    opportunities.  The linker has been hacked (see ppc64elf.em) to do
   8319    a preliminary section layout so that we know the TLS segment
   8320    offsets.  We can't optimize earlier because some optimizations need
   8321    to know the tp offset, and we need to optimize before allocating
   8322    dynamic relocations.  */
   8323 
   8324 bfd_boolean
   8325 ppc64_elf_tls_optimize (struct bfd_link_info *info)
   8326 {
   8327   bfd *ibfd;
   8328   asection *sec;
   8329   struct ppc_link_hash_table *htab;
   8330   unsigned char *toc_ref;
   8331   int pass;
   8332 
   8333   if (!bfd_link_executable (info))
   8334     return TRUE;
   8335 
   8336   htab = ppc_hash_table (info);
   8337   if (htab == NULL)
   8338     return FALSE;
   8339 
   8340   /* Make two passes over the relocs.  On the first pass, mark toc
   8341      entries involved with tls relocs, and check that tls relocs
   8342      involved in setting up a tls_get_addr call are indeed followed by
   8343      such a call.  If they are not, we can't do any tls optimization.
   8344      On the second pass twiddle tls_mask flags to notify
   8345      relocate_section that optimization can be done, and adjust got
   8346      and plt refcounts.  */
   8347   toc_ref = NULL;
   8348   for (pass = 0; pass < 2; ++pass)
   8349     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   8350       {
   8351 	Elf_Internal_Sym *locsyms = NULL;
   8352 	asection *toc = bfd_get_section_by_name (ibfd, ".toc");
   8353 
   8354 	for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8355 	  if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
   8356 	    {
   8357 	      Elf_Internal_Rela *relstart, *rel, *relend;
   8358 	      bfd_boolean found_tls_get_addr_arg = 0;
   8359 
   8360 	      /* Read the relocations.  */
   8361 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   8362 						    info->keep_memory);
   8363 	      if (relstart == NULL)
   8364 		{
   8365 		  free (toc_ref);
   8366 		  return FALSE;
   8367 		}
   8368 
   8369 	      relend = relstart + sec->reloc_count;
   8370 	      for (rel = relstart; rel < relend; rel++)
   8371 		{
   8372 		  enum elf_ppc64_reloc_type r_type;
   8373 		  unsigned long r_symndx;
   8374 		  struct elf_link_hash_entry *h;
   8375 		  Elf_Internal_Sym *sym;
   8376 		  asection *sym_sec;
   8377 		  unsigned char *tls_mask;
   8378 		  unsigned char tls_set, tls_clear, tls_type = 0;
   8379 		  bfd_vma value;
   8380 		  bfd_boolean ok_tprel, is_local;
   8381 		  long toc_ref_index = 0;
   8382 		  int expecting_tls_get_addr = 0;
   8383 		  bfd_boolean ret = FALSE;
   8384 
   8385 		  r_symndx = ELF64_R_SYM (rel->r_info);
   8386 		  if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
   8387 				  r_symndx, ibfd))
   8388 		    {
   8389 		    err_free_rel:
   8390 		      if (elf_section_data (sec)->relocs != relstart)
   8391 			free (relstart);
   8392 		      if (toc_ref != NULL)
   8393 			free (toc_ref);
   8394 		      if (locsyms != NULL
   8395 			  && (elf_symtab_hdr (ibfd).contents
   8396 			      != (unsigned char *) locsyms))
   8397 			free (locsyms);
   8398 		      return ret;
   8399 		    }
   8400 
   8401 		  if (h != NULL)
   8402 		    {
   8403 		      if (h->root.type == bfd_link_hash_defined
   8404 			  || h->root.type == bfd_link_hash_defweak)
   8405 			value = h->root.u.def.value;
   8406 		      else if (h->root.type == bfd_link_hash_undefweak)
   8407 			value = 0;
   8408 		      else
   8409 			{
   8410 			  found_tls_get_addr_arg = 0;
   8411 			  continue;
   8412 			}
   8413 		    }
   8414 		  else
   8415 		    /* Symbols referenced by TLS relocs must be of type
   8416 		       STT_TLS.  So no need for .opd local sym adjust.  */
   8417 		    value = sym->st_value;
   8418 
   8419 		  ok_tprel = FALSE;
   8420 		  is_local = FALSE;
   8421 		  if (h == NULL
   8422 		      || !h->def_dynamic)
   8423 		    {
   8424 		      is_local = TRUE;
   8425 		      if (h != NULL
   8426 			  && h->root.type == bfd_link_hash_undefweak)
   8427 			ok_tprel = TRUE;
   8428 		      else if (sym_sec != NULL
   8429 			       && sym_sec->output_section != NULL)
   8430 			{
   8431 			  value += sym_sec->output_offset;
   8432 			  value += sym_sec->output_section->vma;
   8433 			  value -= htab->elf.tls_sec->vma;
   8434 			  ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
   8435 				      < (bfd_vma) 1 << 32);
   8436 			}
   8437 		    }
   8438 
   8439 		  r_type = ELF64_R_TYPE (rel->r_info);
   8440 		  /* If this section has old-style __tls_get_addr calls
   8441 		     without marker relocs, then check that each
   8442 		     __tls_get_addr call reloc is preceded by a reloc
   8443 		     that conceivably belongs to the __tls_get_addr arg
   8444 		     setup insn.  If we don't find matching arg setup
   8445 		     relocs, don't do any tls optimization.  */
   8446 		  if (pass == 0
   8447 		      && sec->has_tls_get_addr_call
   8448 		      && h != NULL
   8449 		      && (h == &htab->tls_get_addr->elf
   8450 			  || h == &htab->tls_get_addr_fd->elf)
   8451 		      && !found_tls_get_addr_arg
   8452 		      && is_branch_reloc (r_type))
   8453 		    {
   8454 		      info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
   8455 						"TLS optimization disabled\n"),
   8456 					      ibfd, sec, rel->r_offset);
   8457 		      ret = TRUE;
   8458 		      goto err_free_rel;
   8459 		    }
   8460 
   8461 		  found_tls_get_addr_arg = 0;
   8462 		  switch (r_type)
   8463 		    {
   8464 		    case R_PPC64_GOT_TLSLD16:
   8465 		    case R_PPC64_GOT_TLSLD16_LO:
   8466 		      expecting_tls_get_addr = 1;
   8467 		      found_tls_get_addr_arg = 1;
   8468 		      /* Fall thru */
   8469 
   8470 		    case R_PPC64_GOT_TLSLD16_HI:
   8471 		    case R_PPC64_GOT_TLSLD16_HA:
   8472 		      /* These relocs should never be against a symbol
   8473 			 defined in a shared lib.  Leave them alone if
   8474 			 that turns out to be the case.  */
   8475 		      if (!is_local)
   8476 			continue;
   8477 
   8478 		      /* LD -> LE */
   8479 		      tls_set = 0;
   8480 		      tls_clear = TLS_LD;
   8481 		      tls_type = TLS_TLS | TLS_LD;
   8482 		      break;
   8483 
   8484 		    case R_PPC64_GOT_TLSGD16:
   8485 		    case R_PPC64_GOT_TLSGD16_LO:
   8486 		      expecting_tls_get_addr = 1;
   8487 		      found_tls_get_addr_arg = 1;
   8488 		      /* Fall thru */
   8489 
   8490 		    case R_PPC64_GOT_TLSGD16_HI:
   8491 		    case R_PPC64_GOT_TLSGD16_HA:
   8492 		      if (ok_tprel)
   8493 			/* GD -> LE */
   8494 			tls_set = 0;
   8495 		      else
   8496 			/* GD -> IE */
   8497 			tls_set = TLS_TLS | TLS_TPRELGD;
   8498 		      tls_clear = TLS_GD;
   8499 		      tls_type = TLS_TLS | TLS_GD;
   8500 		      break;
   8501 
   8502 		    case R_PPC64_GOT_TPREL16_DS:
   8503 		    case R_PPC64_GOT_TPREL16_LO_DS:
   8504 		    case R_PPC64_GOT_TPREL16_HI:
   8505 		    case R_PPC64_GOT_TPREL16_HA:
   8506 		      if (ok_tprel)
   8507 			{
   8508 			  /* IE -> LE */
   8509 			  tls_set = 0;
   8510 			  tls_clear = TLS_TPREL;
   8511 			  tls_type = TLS_TLS | TLS_TPREL;
   8512 			  break;
   8513 			}
   8514 		      continue;
   8515 
   8516 		    case R_PPC64_TLSGD:
   8517 		    case R_PPC64_TLSLD:
   8518 		      found_tls_get_addr_arg = 1;
   8519 		      /* Fall thru */
   8520 
   8521 		    case R_PPC64_TLS:
   8522 		    case R_PPC64_TOC16:
   8523 		    case R_PPC64_TOC16_LO:
   8524 		      if (sym_sec == NULL || sym_sec != toc)
   8525 			continue;
   8526 
   8527 		      /* Mark this toc entry as referenced by a TLS
   8528 			 code sequence.  We can do that now in the
   8529 			 case of R_PPC64_TLS, and after checking for
   8530 			 tls_get_addr for the TOC16 relocs.  */
   8531 		      if (toc_ref == NULL)
   8532 			toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
   8533 		      if (toc_ref == NULL)
   8534 			goto err_free_rel;
   8535 
   8536 		      if (h != NULL)
   8537 			value = h->root.u.def.value;
   8538 		      else
   8539 			value = sym->st_value;
   8540 		      value += rel->r_addend;
   8541 		      if (value % 8 != 0)
   8542 			continue;
   8543 		      BFD_ASSERT (value < toc->size
   8544 				  && toc->output_offset % 8 == 0);
   8545 		      toc_ref_index = (value + toc->output_offset) / 8;
   8546 		      if (r_type == R_PPC64_TLS
   8547 			  || r_type == R_PPC64_TLSGD
   8548 			  || r_type == R_PPC64_TLSLD)
   8549 			{
   8550 			  toc_ref[toc_ref_index] = 1;
   8551 			  continue;
   8552 			}
   8553 
   8554 		      if (pass != 0 && toc_ref[toc_ref_index] == 0)
   8555 			continue;
   8556 
   8557 		      tls_set = 0;
   8558 		      tls_clear = 0;
   8559 		      expecting_tls_get_addr = 2;
   8560 		      break;
   8561 
   8562 		    case R_PPC64_TPREL64:
   8563 		      if (pass == 0
   8564 			  || sec != toc
   8565 			  || toc_ref == NULL
   8566 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   8567 			continue;
   8568 		      if (ok_tprel)
   8569 			{
   8570 			  /* IE -> LE */
   8571 			  tls_set = TLS_EXPLICIT;
   8572 			  tls_clear = TLS_TPREL;
   8573 			  break;
   8574 			}
   8575 		      continue;
   8576 
   8577 		    case R_PPC64_DTPMOD64:
   8578 		      if (pass == 0
   8579 			  || sec != toc
   8580 			  || toc_ref == NULL
   8581 			  || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
   8582 			continue;
   8583 		      if (rel + 1 < relend
   8584 			  && (rel[1].r_info
   8585 			      == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
   8586 			  && rel[1].r_offset == rel->r_offset + 8)
   8587 			{
   8588 			  if (ok_tprel)
   8589 			    /* GD -> LE */
   8590 			    tls_set = TLS_EXPLICIT | TLS_GD;
   8591 			  else
   8592 			    /* GD -> IE */
   8593 			    tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
   8594 			  tls_clear = TLS_GD;
   8595 			}
   8596 		      else
   8597 			{
   8598 			  if (!is_local)
   8599 			    continue;
   8600 
   8601 			  /* LD -> LE */
   8602 			  tls_set = TLS_EXPLICIT;
   8603 			  tls_clear = TLS_LD;
   8604 			}
   8605 		      break;
   8606 
   8607 		    default:
   8608 		      continue;
   8609 		    }
   8610 
   8611 		  if (pass == 0)
   8612 		    {
   8613 		      if (!expecting_tls_get_addr
   8614 			  || !sec->has_tls_get_addr_call)
   8615 			continue;
   8616 
   8617 		      if (rel + 1 < relend
   8618 			  && branch_reloc_hash_match (ibfd, rel + 1,
   8619 						      htab->tls_get_addr,
   8620 						      htab->tls_get_addr_fd))
   8621 			{
   8622 			  if (expecting_tls_get_addr == 2)
   8623 			    {
   8624 			      /* Check for toc tls entries.  */
   8625 			      unsigned char *toc_tls;
   8626 			      int retval;
   8627 
   8628 			      retval = get_tls_mask (&toc_tls, NULL, NULL,
   8629 						     &locsyms,
   8630 						     rel, ibfd);
   8631 			      if (retval == 0)
   8632 				goto err_free_rel;
   8633 			      if (toc_tls != NULL)
   8634 				{
   8635 				  if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
   8636 				    found_tls_get_addr_arg = 1;
   8637 				  if (retval > 1)
   8638 				    toc_ref[toc_ref_index] = 1;
   8639 				}
   8640 			    }
   8641 			  continue;
   8642 			}
   8643 
   8644 		      if (expecting_tls_get_addr != 1)
   8645 			continue;
   8646 
   8647 		      /* Uh oh, we didn't find the expected call.  We
   8648 			 could just mark this symbol to exclude it
   8649 			 from tls optimization but it's safer to skip
   8650 			 the entire optimization.  */
   8651 		      info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
   8652 						"TLS optimization disabled\n"),
   8653 					      ibfd, sec, rel->r_offset);
   8654 		      ret = TRUE;
   8655 		      goto err_free_rel;
   8656 		    }
   8657 
   8658 		  if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
   8659 		    {
   8660 		      struct plt_entry *ent;
   8661 		      for (ent = htab->tls_get_addr->elf.plt.plist;
   8662 			   ent != NULL;
   8663 			   ent = ent->next)
   8664 			if (ent->addend == 0)
   8665 			  {
   8666 			    if (ent->plt.refcount > 0)
   8667 			      {
   8668 				ent->plt.refcount -= 1;
   8669 				expecting_tls_get_addr = 0;
   8670 			      }
   8671 			    break;
   8672 			  }
   8673 		    }
   8674 
   8675 		  if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
   8676 		    {
   8677 		      struct plt_entry *ent;
   8678 		      for (ent = htab->tls_get_addr_fd->elf.plt.plist;
   8679 			   ent != NULL;
   8680 			   ent = ent->next)
   8681 			if (ent->addend == 0)
   8682 			  {
   8683 			    if (ent->plt.refcount > 0)
   8684 			      ent->plt.refcount -= 1;
   8685 			    break;
   8686 			  }
   8687 		    }
   8688 
   8689 		  if (tls_clear == 0)
   8690 		    continue;
   8691 
   8692 		  if ((tls_set & TLS_EXPLICIT) == 0)
   8693 		    {
   8694 		      struct got_entry *ent;
   8695 
   8696 		      /* Adjust got entry for this reloc.  */
   8697 		      if (h != NULL)
   8698 			ent = h->got.glist;
   8699 		      else
   8700 			ent = elf_local_got_ents (ibfd)[r_symndx];
   8701 
   8702 		      for (; ent != NULL; ent = ent->next)
   8703 			if (ent->addend == rel->r_addend
   8704 			    && ent->owner == ibfd
   8705 			    && ent->tls_type == tls_type)
   8706 			  break;
   8707 		      if (ent == NULL)
   8708 			abort ();
   8709 
   8710 		      if (tls_set == 0)
   8711 			{
   8712 			  /* We managed to get rid of a got entry.  */
   8713 			  if (ent->got.refcount > 0)
   8714 			    ent->got.refcount -= 1;
   8715 			}
   8716 		    }
   8717 		  else
   8718 		    {
   8719 		      /* If we got rid of a DTPMOD/DTPREL reloc pair then
   8720 			 we'll lose one or two dyn relocs.  */
   8721 		      if (!dec_dynrel_count (rel->r_info, sec, info,
   8722 					     NULL, h, sym))
   8723 			return FALSE;
   8724 
   8725 		      if (tls_set == (TLS_EXPLICIT | TLS_GD))
   8726 			{
   8727 			  if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
   8728 						 NULL, h, sym))
   8729 			    return FALSE;
   8730 			}
   8731 		    }
   8732 
   8733 		  *tls_mask |= tls_set;
   8734 		  *tls_mask &= ~tls_clear;
   8735 		}
   8736 
   8737 	      if (elf_section_data (sec)->relocs != relstart)
   8738 		free (relstart);
   8739 	    }
   8740 
   8741 	if (locsyms != NULL
   8742 	    && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
   8743 	  {
   8744 	    if (!info->keep_memory)
   8745 	      free (locsyms);
   8746 	    else
   8747 	      elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
   8748 	  }
   8749       }
   8750 
   8751   if (toc_ref != NULL)
   8752     free (toc_ref);
   8753   return TRUE;
   8754 }
   8755 
   8756 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
   8757    the values of any global symbols in a toc section that has been
   8758    edited.  Globals in toc sections should be a rarity, so this function
   8759    sets a flag if any are found in toc sections other than the one just
   8760    edited, so that futher hash table traversals can be avoided.  */
   8761 
   8762 struct adjust_toc_info
   8763 {
   8764   asection *toc;
   8765   unsigned long *skip;
   8766   bfd_boolean global_toc_syms;
   8767 };
   8768 
   8769 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
   8770 
   8771 static bfd_boolean
   8772 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
   8773 {
   8774   struct ppc_link_hash_entry *eh;
   8775   struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
   8776   unsigned long i;
   8777 
   8778   if (h->root.type != bfd_link_hash_defined
   8779       && h->root.type != bfd_link_hash_defweak)
   8780     return TRUE;
   8781 
   8782   eh = (struct ppc_link_hash_entry *) h;
   8783   if (eh->adjust_done)
   8784     return TRUE;
   8785 
   8786   if (eh->elf.root.u.def.section == toc_inf->toc)
   8787     {
   8788       if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
   8789 	i = toc_inf->toc->rawsize >> 3;
   8790       else
   8791 	i = eh->elf.root.u.def.value >> 3;
   8792 
   8793       if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
   8794 	{
   8795 	  (*_bfd_error_handler)
   8796 	    (_("%s defined on removed toc entry"), eh->elf.root.root.string);
   8797 	  do
   8798 	    ++i;
   8799 	  while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
   8800 	  eh->elf.root.u.def.value = (bfd_vma) i << 3;
   8801 	}
   8802 
   8803       eh->elf.root.u.def.value -= toc_inf->skip[i];
   8804       eh->adjust_done = 1;
   8805     }
   8806   else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
   8807     toc_inf->global_toc_syms = TRUE;
   8808 
   8809   return TRUE;
   8810 }
   8811 
   8812 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
   8813    on a _LO variety toc/got reloc.  */
   8814 
   8815 static bfd_boolean
   8816 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
   8817 {
   8818   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
   8819 	  || (insn & (0x3f << 26)) == 14u << 26 /* addi */
   8820 	  || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
   8821 	  || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
   8822 	  || (insn & (0x3f << 26)) == 36u << 26 /* stw */
   8823 	  || (insn & (0x3f << 26)) == 38u << 26 /* stb */
   8824 	  || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
   8825 	  || (insn & (0x3f << 26)) == 42u << 26 /* lha */
   8826 	  || (insn & (0x3f << 26)) == 44u << 26 /* sth */
   8827 	  || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
   8828 	  || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
   8829 	  || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
   8830 	  || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
   8831 	  || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
   8832 	  || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
   8833 	  || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
   8834 	  || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
   8835 	      /* Exclude lfqu by testing reloc.  If relocs are ever
   8836 		 defined for the reduced D field in psq_lu then those
   8837 		 will need testing too.  */
   8838 	      && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
   8839 	  || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
   8840 	      && (insn & 1) == 0)
   8841 	  || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
   8842 	  || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
   8843 	      /* Exclude stfqu.  psq_stu as above for psq_lu.  */
   8844 	      && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
   8845 	  || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
   8846 	      && (insn & 1) == 0));
   8847 }
   8848 
   8849 /* Examine all relocs referencing .toc sections in order to remove
   8850    unused .toc entries.  */
   8851 
   8852 bfd_boolean
   8853 ppc64_elf_edit_toc (struct bfd_link_info *info)
   8854 {
   8855   bfd *ibfd;
   8856   struct adjust_toc_info toc_inf;
   8857   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   8858 
   8859   htab->do_toc_opt = 1;
   8860   toc_inf.global_toc_syms = TRUE;
   8861   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   8862     {
   8863       asection *toc, *sec;
   8864       Elf_Internal_Shdr *symtab_hdr;
   8865       Elf_Internal_Sym *local_syms;
   8866       Elf_Internal_Rela *relstart, *rel, *toc_relocs;
   8867       unsigned long *skip, *drop;
   8868       unsigned char *used;
   8869       unsigned char *keep, last, some_unused;
   8870 
   8871       if (!is_ppc64_elf (ibfd))
   8872 	continue;
   8873 
   8874       toc = bfd_get_section_by_name (ibfd, ".toc");
   8875       if (toc == NULL
   8876 	  || toc->size == 0
   8877 	  || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
   8878 	  || discarded_section (toc))
   8879 	continue;
   8880 
   8881       toc_relocs = NULL;
   8882       local_syms = NULL;
   8883       symtab_hdr = &elf_symtab_hdr (ibfd);
   8884 
   8885       /* Look at sections dropped from the final link.  */
   8886       skip = NULL;
   8887       relstart = NULL;
   8888       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   8889 	{
   8890 	  if (sec->reloc_count == 0
   8891 	      || !discarded_section (sec)
   8892 	      || get_opd_info (sec)
   8893 	      || (sec->flags & SEC_ALLOC) == 0
   8894 	      || (sec->flags & SEC_DEBUGGING) != 0)
   8895 	    continue;
   8896 
   8897 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
   8898 	  if (relstart == NULL)
   8899 	    goto error_ret;
   8900 
   8901 	  /* Run through the relocs to see which toc entries might be
   8902 	     unused.  */
   8903 	  for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   8904 	    {
   8905 	      enum elf_ppc64_reloc_type r_type;
   8906 	      unsigned long r_symndx;
   8907 	      asection *sym_sec;
   8908 	      struct elf_link_hash_entry *h;
   8909 	      Elf_Internal_Sym *sym;
   8910 	      bfd_vma val;
   8911 
   8912 	      r_type = ELF64_R_TYPE (rel->r_info);
   8913 	      switch (r_type)
   8914 		{
   8915 		default:
   8916 		  continue;
   8917 
   8918 		case R_PPC64_TOC16:
   8919 		case R_PPC64_TOC16_LO:
   8920 		case R_PPC64_TOC16_HI:
   8921 		case R_PPC64_TOC16_HA:
   8922 		case R_PPC64_TOC16_DS:
   8923 		case R_PPC64_TOC16_LO_DS:
   8924 		  break;
   8925 		}
   8926 
   8927 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8928 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8929 			      r_symndx, ibfd))
   8930 		goto error_ret;
   8931 
   8932 	      if (sym_sec != toc)
   8933 		continue;
   8934 
   8935 	      if (h != NULL)
   8936 		val = h->root.u.def.value;
   8937 	      else
   8938 		val = sym->st_value;
   8939 	      val += rel->r_addend;
   8940 
   8941 	      if (val >= toc->size)
   8942 		continue;
   8943 
   8944 	      /* Anything in the toc ought to be aligned to 8 bytes.
   8945 		 If not, don't mark as unused.  */
   8946 	      if (val & 7)
   8947 		continue;
   8948 
   8949 	      if (skip == NULL)
   8950 		{
   8951 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   8952 		  if (skip == NULL)
   8953 		    goto error_ret;
   8954 		}
   8955 
   8956 	      skip[val >> 3] = ref_from_discarded;
   8957 	    }
   8958 
   8959 	  if (elf_section_data (sec)->relocs != relstart)
   8960 	    free (relstart);
   8961 	}
   8962 
   8963       /* For largetoc loads of address constants, we can convert
   8964 	 .  addis rx,2,addr@got@ha
   8965 	 .  ld ry,addr@got@l(rx)
   8966 	 to
   8967 	 .  addis rx,2,addr@toc@ha
   8968 	 .  addi ry,rx,addr@toc@l
   8969 	 when addr is within 2G of the toc pointer.  This then means
   8970 	 that the word storing "addr" in the toc is no longer needed.  */
   8971 
   8972       if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
   8973 	  && toc->output_section->rawsize < (bfd_vma) 1 << 31
   8974 	  && toc->reloc_count != 0)
   8975 	{
   8976 	  /* Read toc relocs.  */
   8977 	  toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   8978 						  info->keep_memory);
   8979 	  if (toc_relocs == NULL)
   8980 	    goto error_ret;
   8981 
   8982 	  for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   8983 	    {
   8984 	      enum elf_ppc64_reloc_type r_type;
   8985 	      unsigned long r_symndx;
   8986 	      asection *sym_sec;
   8987 	      struct elf_link_hash_entry *h;
   8988 	      Elf_Internal_Sym *sym;
   8989 	      bfd_vma val, addr;
   8990 
   8991 	      r_type = ELF64_R_TYPE (rel->r_info);
   8992 	      if (r_type != R_PPC64_ADDR64)
   8993 		continue;
   8994 
   8995 	      r_symndx = ELF64_R_SYM (rel->r_info);
   8996 	      if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   8997 			      r_symndx, ibfd))
   8998 		goto error_ret;
   8999 
   9000 	      if (sym_sec == NULL
   9001 		  || sym_sec->output_section == NULL
   9002 		  || discarded_section (sym_sec))
   9003 		continue;
   9004 
   9005 	      if (!SYMBOL_REFERENCES_LOCAL (info, h))
   9006 		continue;
   9007 
   9008 	      if (h != NULL)
   9009 		{
   9010 		  if (h->type == STT_GNU_IFUNC)
   9011 		    continue;
   9012 		  val = h->root.u.def.value;
   9013 		}
   9014 	      else
   9015 		{
   9016 		  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   9017 		    continue;
   9018 		  val = sym->st_value;
   9019 		}
   9020 	      val += rel->r_addend;
   9021 	      val += sym_sec->output_section->vma + sym_sec->output_offset;
   9022 
   9023 	      /* We don't yet know the exact toc pointer value, but we
   9024 		 know it will be somewhere in the toc section.  Don't
   9025 		 optimize if the difference from any possible toc
   9026 		 pointer is outside [ff..f80008000, 7fff7fff].  */
   9027 	      addr = toc->output_section->vma + TOC_BASE_OFF;
   9028 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   9029 		continue;
   9030 
   9031 	      addr = toc->output_section->vma + toc->output_section->rawsize;
   9032 	      if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
   9033 		continue;
   9034 
   9035 	      if (skip == NULL)
   9036 		{
   9037 		  skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
   9038 		  if (skip == NULL)
   9039 		    goto error_ret;
   9040 		}
   9041 
   9042 	      skip[rel->r_offset >> 3]
   9043 		|= can_optimize | ((rel - toc_relocs) << 2);
   9044 	    }
   9045 	}
   9046 
   9047       if (skip == NULL)
   9048 	continue;
   9049 
   9050       used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
   9051       if (used == NULL)
   9052 	{
   9053 	error_ret:
   9054 	  if (local_syms != NULL
   9055 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   9056 	    free (local_syms);
   9057 	  if (sec != NULL
   9058 	      && relstart != NULL
   9059 	      && elf_section_data (sec)->relocs != relstart)
   9060 	    free (relstart);
   9061 	  if (toc_relocs != NULL
   9062 	      && elf_section_data (toc)->relocs != toc_relocs)
   9063 	    free (toc_relocs);
   9064 	  if (skip != NULL)
   9065 	    free (skip);
   9066 	  return FALSE;
   9067 	}
   9068 
   9069       /* Now check all kept sections that might reference the toc.
   9070 	 Check the toc itself last.  */
   9071       for (sec = (ibfd->sections == toc && toc->next ? toc->next
   9072 		  : ibfd->sections);
   9073 	   sec != NULL;
   9074 	   sec = (sec == toc ? NULL
   9075 		  : sec->next == NULL ? toc
   9076 		  : sec->next == toc && toc->next ? toc->next
   9077 		  : sec->next))
   9078 	{
   9079 	  int repeat;
   9080 
   9081 	  if (sec->reloc_count == 0
   9082 	      || discarded_section (sec)
   9083 	      || get_opd_info (sec)
   9084 	      || (sec->flags & SEC_ALLOC) == 0
   9085 	      || (sec->flags & SEC_DEBUGGING) != 0)
   9086 	    continue;
   9087 
   9088 	  relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   9089 						info->keep_memory);
   9090 	  if (relstart == NULL)
   9091 	    {
   9092 	      free (used);
   9093 	      goto error_ret;
   9094 	    }
   9095 
   9096 	  /* Mark toc entries referenced as used.  */
   9097 	  do
   9098 	    {
   9099 	      repeat = 0;
   9100 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   9101 		{
   9102 		  enum elf_ppc64_reloc_type r_type;
   9103 		  unsigned long r_symndx;
   9104 		  asection *sym_sec;
   9105 		  struct elf_link_hash_entry *h;
   9106 		  Elf_Internal_Sym *sym;
   9107 		  bfd_vma val;
   9108 		  enum {no_check, check_lo, check_ha} insn_check;
   9109 
   9110 		  r_type = ELF64_R_TYPE (rel->r_info);
   9111 		  switch (r_type)
   9112 		    {
   9113 		    default:
   9114 		      insn_check = no_check;
   9115 		      break;
   9116 
   9117 		    case R_PPC64_GOT_TLSLD16_HA:
   9118 		    case R_PPC64_GOT_TLSGD16_HA:
   9119 		    case R_PPC64_GOT_TPREL16_HA:
   9120 		    case R_PPC64_GOT_DTPREL16_HA:
   9121 		    case R_PPC64_GOT16_HA:
   9122 		    case R_PPC64_TOC16_HA:
   9123 		      insn_check = check_ha;
   9124 		      break;
   9125 
   9126 		    case R_PPC64_GOT_TLSLD16_LO:
   9127 		    case R_PPC64_GOT_TLSGD16_LO:
   9128 		    case R_PPC64_GOT_TPREL16_LO_DS:
   9129 		    case R_PPC64_GOT_DTPREL16_LO_DS:
   9130 		    case R_PPC64_GOT16_LO:
   9131 		    case R_PPC64_GOT16_LO_DS:
   9132 		    case R_PPC64_TOC16_LO:
   9133 		    case R_PPC64_TOC16_LO_DS:
   9134 		      insn_check = check_lo;
   9135 		      break;
   9136 		    }
   9137 
   9138 		  if (insn_check != no_check)
   9139 		    {
   9140 		      bfd_vma off = rel->r_offset & ~3;
   9141 		      unsigned char buf[4];
   9142 		      unsigned int insn;
   9143 
   9144 		      if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
   9145 			{
   9146 			  free (used);
   9147 			  goto error_ret;
   9148 			}
   9149 		      insn = bfd_get_32 (ibfd, buf);
   9150 		      if (insn_check == check_lo
   9151 			  ? !ok_lo_toc_insn (insn, r_type)
   9152 			  : ((insn & ((0x3f << 26) | 0x1f << 16))
   9153 			     != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
   9154 			{
   9155 			  char str[12];
   9156 
   9157 			  ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
   9158 			  sprintf (str, "%#08x", insn);
   9159 			  info->callbacks->einfo
   9160 			    (_("%P: %H: toc optimization is not supported for"
   9161 			       " %s instruction.\n"),
   9162 			     ibfd, sec, rel->r_offset & ~3, str);
   9163 			}
   9164 		    }
   9165 
   9166 		  switch (r_type)
   9167 		    {
   9168 		    case R_PPC64_TOC16:
   9169 		    case R_PPC64_TOC16_LO:
   9170 		    case R_PPC64_TOC16_HI:
   9171 		    case R_PPC64_TOC16_HA:
   9172 		    case R_PPC64_TOC16_DS:
   9173 		    case R_PPC64_TOC16_LO_DS:
   9174 		      /* In case we're taking addresses of toc entries.  */
   9175 		    case R_PPC64_ADDR64:
   9176 		      break;
   9177 
   9178 		    default:
   9179 		      continue;
   9180 		    }
   9181 
   9182 		  r_symndx = ELF64_R_SYM (rel->r_info);
   9183 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   9184 				  r_symndx, ibfd))
   9185 		    {
   9186 		      free (used);
   9187 		      goto error_ret;
   9188 		    }
   9189 
   9190 		  if (sym_sec != toc)
   9191 		    continue;
   9192 
   9193 		  if (h != NULL)
   9194 		    val = h->root.u.def.value;
   9195 		  else
   9196 		    val = sym->st_value;
   9197 		  val += rel->r_addend;
   9198 
   9199 		  if (val >= toc->size)
   9200 		    continue;
   9201 
   9202 		  if ((skip[val >> 3] & can_optimize) != 0)
   9203 		    {
   9204 		      bfd_vma off;
   9205 		      unsigned char opc;
   9206 
   9207 		      switch (r_type)
   9208 			{
   9209 			case R_PPC64_TOC16_HA:
   9210 			  break;
   9211 
   9212 			case R_PPC64_TOC16_LO_DS:
   9213 			  off = rel->r_offset;
   9214 			  off += (bfd_big_endian (ibfd) ? -2 : 3);
   9215 			  if (!bfd_get_section_contents (ibfd, sec, &opc,
   9216 							 off, 1))
   9217 			    {
   9218 			      free (used);
   9219 			      goto error_ret;
   9220 			    }
   9221 			  if ((opc & (0x3f << 2)) == (58u << 2))
   9222 			    break;
   9223 			  /* Fall thru */
   9224 
   9225 			default:
   9226 			  /* Wrong sort of reloc, or not a ld.  We may
   9227 			     as well clear ref_from_discarded too.  */
   9228 			  skip[val >> 3] = 0;
   9229 			}
   9230 		    }
   9231 
   9232 		  if (sec != toc)
   9233 		    used[val >> 3] = 1;
   9234 		  /* For the toc section, we only mark as used if this
   9235 		     entry itself isn't unused.  */
   9236 		  else if ((used[rel->r_offset >> 3]
   9237 			    || !(skip[rel->r_offset >> 3] & ref_from_discarded))
   9238 			   && !used[val >> 3])
   9239 		    {
   9240 		      /* Do all the relocs again, to catch reference
   9241 			 chains.  */
   9242 		      repeat = 1;
   9243 		      used[val >> 3] = 1;
   9244 		    }
   9245 		}
   9246 	    }
   9247 	  while (repeat);
   9248 
   9249 	  if (elf_section_data (sec)->relocs != relstart)
   9250 	    free (relstart);
   9251 	}
   9252 
   9253       /* Merge the used and skip arrays.  Assume that TOC
   9254 	 doublewords not appearing as either used or unused belong
   9255 	 to to an entry more than one doubleword in size.  */
   9256       for (drop = skip, keep = used, last = 0, some_unused = 0;
   9257 	   drop < skip + (toc->size + 7) / 8;
   9258 	   ++drop, ++keep)
   9259 	{
   9260 	  if (*keep)
   9261 	    {
   9262 	      *drop &= ~ref_from_discarded;
   9263 	      if ((*drop & can_optimize) != 0)
   9264 		some_unused = 1;
   9265 	      last = 0;
   9266 	    }
   9267 	  else if ((*drop & ref_from_discarded) != 0)
   9268 	    {
   9269 	      some_unused = 1;
   9270 	      last = ref_from_discarded;
   9271 	    }
   9272 	  else
   9273 	    *drop = last;
   9274 	}
   9275 
   9276       free (used);
   9277 
   9278       if (some_unused)
   9279 	{
   9280 	  bfd_byte *contents, *src;
   9281 	  unsigned long off;
   9282 	  Elf_Internal_Sym *sym;
   9283 	  bfd_boolean local_toc_syms = FALSE;
   9284 
   9285 	  /* Shuffle the toc contents, and at the same time convert the
   9286 	     skip array from booleans into offsets.  */
   9287 	  if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
   9288 	    goto error_ret;
   9289 
   9290 	  elf_section_data (toc)->this_hdr.contents = contents;
   9291 
   9292 	  for (src = contents, off = 0, drop = skip;
   9293 	       src < contents + toc->size;
   9294 	       src += 8, ++drop)
   9295 	    {
   9296 	      if ((*drop & (can_optimize | ref_from_discarded)) != 0)
   9297 		off += 8;
   9298 	      else if (off != 0)
   9299 		{
   9300 		  *drop = off;
   9301 		  memcpy (src - off, src, 8);
   9302 		}
   9303 	    }
   9304 	  *drop = off;
   9305 	  toc->rawsize = toc->size;
   9306 	  toc->size = src - contents - off;
   9307 
   9308 	  /* Adjust addends for relocs against the toc section sym,
   9309 	     and optimize any accesses we can.  */
   9310 	  for (sec = ibfd->sections; sec != NULL; sec = sec->next)
   9311 	    {
   9312 	      if (sec->reloc_count == 0
   9313 		  || discarded_section (sec))
   9314 		continue;
   9315 
   9316 	      relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
   9317 						    info->keep_memory);
   9318 	      if (relstart == NULL)
   9319 		goto error_ret;
   9320 
   9321 	      for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
   9322 		{
   9323 		  enum elf_ppc64_reloc_type r_type;
   9324 		  unsigned long r_symndx;
   9325 		  asection *sym_sec;
   9326 		  struct elf_link_hash_entry *h;
   9327 		  bfd_vma val;
   9328 
   9329 		  r_type = ELF64_R_TYPE (rel->r_info);
   9330 		  switch (r_type)
   9331 		    {
   9332 		    default:
   9333 		      continue;
   9334 
   9335 		    case R_PPC64_TOC16:
   9336 		    case R_PPC64_TOC16_LO:
   9337 		    case R_PPC64_TOC16_HI:
   9338 		    case R_PPC64_TOC16_HA:
   9339 		    case R_PPC64_TOC16_DS:
   9340 		    case R_PPC64_TOC16_LO_DS:
   9341 		    case R_PPC64_ADDR64:
   9342 		      break;
   9343 		    }
   9344 
   9345 		  r_symndx = ELF64_R_SYM (rel->r_info);
   9346 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   9347 				  r_symndx, ibfd))
   9348 		    goto error_ret;
   9349 
   9350 		  if (sym_sec != toc)
   9351 		    continue;
   9352 
   9353 		  if (h != NULL)
   9354 		    val = h->root.u.def.value;
   9355 		  else
   9356 		    {
   9357 		      val = sym->st_value;
   9358 		      if (val != 0)
   9359 			local_toc_syms = TRUE;
   9360 		    }
   9361 
   9362 		  val += rel->r_addend;
   9363 
   9364 		  if (val > toc->rawsize)
   9365 		    val = toc->rawsize;
   9366 		  else if ((skip[val >> 3] & ref_from_discarded) != 0)
   9367 		    continue;
   9368 		  else if ((skip[val >> 3] & can_optimize) != 0)
   9369 		    {
   9370 		      Elf_Internal_Rela *tocrel
   9371 			= toc_relocs + (skip[val >> 3] >> 2);
   9372 		      unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
   9373 
   9374 		      switch (r_type)
   9375 			{
   9376 			case R_PPC64_TOC16_HA:
   9377 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
   9378 			  break;
   9379 
   9380 			case R_PPC64_TOC16_LO_DS:
   9381 			  rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
   9382 			  break;
   9383 
   9384 			default:
   9385 			  if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   9386 			    ppc_howto_init ();
   9387 			  info->callbacks->einfo
   9388 			    (_("%P: %H: %s references "
   9389 			       "optimized away TOC entry\n"),
   9390 			     ibfd, sec, rel->r_offset,
   9391 			     ppc64_elf_howto_table[r_type]->name);
   9392 			  bfd_set_error (bfd_error_bad_value);
   9393 			  goto error_ret;
   9394 			}
   9395 		      rel->r_addend = tocrel->r_addend;
   9396 		      elf_section_data (sec)->relocs = relstart;
   9397 		      continue;
   9398 		    }
   9399 
   9400 		  if (h != NULL || sym->st_value != 0)
   9401 		    continue;
   9402 
   9403 		  rel->r_addend -= skip[val >> 3];
   9404 		  elf_section_data (sec)->relocs = relstart;
   9405 		}
   9406 
   9407 	      if (elf_section_data (sec)->relocs != relstart)
   9408 		free (relstart);
   9409 	    }
   9410 
   9411 	  /* We shouldn't have local or global symbols defined in the TOC,
   9412 	     but handle them anyway.  */
   9413 	  if (local_syms != NULL)
   9414 	    for (sym = local_syms;
   9415 		 sym < local_syms + symtab_hdr->sh_info;
   9416 		 ++sym)
   9417 	      if (sym->st_value != 0
   9418 		  && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
   9419 		{
   9420 		  unsigned long i;
   9421 
   9422 		  if (sym->st_value > toc->rawsize)
   9423 		    i = toc->rawsize >> 3;
   9424 		  else
   9425 		    i = sym->st_value >> 3;
   9426 
   9427 		  if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
   9428 		    {
   9429 		      if (local_toc_syms)
   9430 			(*_bfd_error_handler)
   9431 			  (_("%s defined on removed toc entry"),
   9432 			   bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
   9433 		      do
   9434 			++i;
   9435 		      while ((skip[i] & (ref_from_discarded | can_optimize)));
   9436 		      sym->st_value = (bfd_vma) i << 3;
   9437 		    }
   9438 
   9439 		  sym->st_value -= skip[i];
   9440 		  symtab_hdr->contents = (unsigned char *) local_syms;
   9441 		}
   9442 
   9443 	  /* Adjust any global syms defined in this toc input section.  */
   9444 	  if (toc_inf.global_toc_syms)
   9445 	    {
   9446 	      toc_inf.toc = toc;
   9447 	      toc_inf.skip = skip;
   9448 	      toc_inf.global_toc_syms = FALSE;
   9449 	      elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
   9450 				      &toc_inf);
   9451 	    }
   9452 
   9453 	  if (toc->reloc_count != 0)
   9454 	    {
   9455 	      Elf_Internal_Shdr *rel_hdr;
   9456 	      Elf_Internal_Rela *wrel;
   9457 	      bfd_size_type sz;
   9458 
   9459 	      /* Remove unused toc relocs, and adjust those we keep.  */
   9460 	      if (toc_relocs == NULL)
   9461 		toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
   9462 							info->keep_memory);
   9463 	      if (toc_relocs == NULL)
   9464 		goto error_ret;
   9465 
   9466 	      wrel = toc_relocs;
   9467 	      for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
   9468 		if ((skip[rel->r_offset >> 3]
   9469 		     & (ref_from_discarded | can_optimize)) == 0)
   9470 		  {
   9471 		    wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
   9472 		    wrel->r_info = rel->r_info;
   9473 		    wrel->r_addend = rel->r_addend;
   9474 		    ++wrel;
   9475 		  }
   9476 		else if (!dec_dynrel_count (rel->r_info, toc, info,
   9477 					    &local_syms, NULL, NULL))
   9478 		  goto error_ret;
   9479 
   9480 	      elf_section_data (toc)->relocs = toc_relocs;
   9481 	      toc->reloc_count = wrel - toc_relocs;
   9482 	      rel_hdr = _bfd_elf_single_rel_hdr (toc);
   9483 	      sz = rel_hdr->sh_entsize;
   9484 	      rel_hdr->sh_size = toc->reloc_count * sz;
   9485 	    }
   9486 	}
   9487       else if (toc_relocs != NULL
   9488 	       && elf_section_data (toc)->relocs != toc_relocs)
   9489 	free (toc_relocs);
   9490 
   9491       if (local_syms != NULL
   9492 	  && symtab_hdr->contents != (unsigned char *) local_syms)
   9493 	{
   9494 	  if (!info->keep_memory)
   9495 	    free (local_syms);
   9496 	  else
   9497 	    symtab_hdr->contents = (unsigned char *) local_syms;
   9498 	}
   9499       free (skip);
   9500     }
   9501 
   9502   return TRUE;
   9503 }
   9504 
   9505 /* Return true iff input section I references the TOC using
   9506    instructions limited to +/-32k offsets.  */
   9507 
   9508 bfd_boolean
   9509 ppc64_elf_has_small_toc_reloc (asection *i)
   9510 {
   9511   return (is_ppc64_elf (i->owner)
   9512 	  && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
   9513 }
   9514 
   9515 /* Allocate space for one GOT entry.  */
   9516 
   9517 static void
   9518 allocate_got (struct elf_link_hash_entry *h,
   9519 	      struct bfd_link_info *info,
   9520 	      struct got_entry *gent)
   9521 {
   9522   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   9523   bfd_boolean dyn;
   9524   struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
   9525   int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
   9526 		 ? 16 : 8);
   9527   int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
   9528 		  ? 2 : 1) * sizeof (Elf64_External_Rela);
   9529   asection *got = ppc64_elf_tdata (gent->owner)->got;
   9530 
   9531   gent->got.offset = got->size;
   9532   got->size += entsize;
   9533 
   9534   dyn = htab->elf.dynamic_sections_created;
   9535   if (h->type == STT_GNU_IFUNC)
   9536     {
   9537       htab->elf.irelplt->size += rentsize;
   9538       htab->got_reli_size += rentsize;
   9539     }
   9540   else if ((bfd_link_pic (info)
   9541 	    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
   9542 	   && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   9543 	       || h->root.type != bfd_link_hash_undefweak))
   9544     {
   9545       asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
   9546       relgot->size += rentsize;
   9547     }
   9548 }
   9549 
   9550 /* This function merges got entries in the same toc group.  */
   9551 
   9552 static void
   9553 merge_got_entries (struct got_entry **pent)
   9554 {
   9555   struct got_entry *ent, *ent2;
   9556 
   9557   for (ent = *pent; ent != NULL; ent = ent->next)
   9558     if (!ent->is_indirect)
   9559       for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
   9560 	if (!ent2->is_indirect
   9561 	    && ent2->addend == ent->addend
   9562 	    && ent2->tls_type == ent->tls_type
   9563 	    && elf_gp (ent2->owner) == elf_gp (ent->owner))
   9564 	  {
   9565 	    ent2->is_indirect = TRUE;
   9566 	    ent2->got.ent = ent;
   9567 	  }
   9568 }
   9569 
   9570 /* Allocate space in .plt, .got and associated reloc sections for
   9571    dynamic relocs.  */
   9572 
   9573 static bfd_boolean
   9574 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   9575 {
   9576   struct bfd_link_info *info;
   9577   struct ppc_link_hash_table *htab;
   9578   asection *s;
   9579   struct ppc_link_hash_entry *eh;
   9580   struct got_entry **pgent, *gent;
   9581 
   9582   if (h->root.type == bfd_link_hash_indirect)
   9583     return TRUE;
   9584 
   9585   info = (struct bfd_link_info *) inf;
   9586   htab = ppc_hash_table (info);
   9587   if (htab == NULL)
   9588     return FALSE;
   9589 
   9590   eh = (struct ppc_link_hash_entry *) h;
   9591   /* Run through the TLS GD got entries first if we're changing them
   9592      to TPREL.  */
   9593   if ((eh->tls_mask & TLS_TPRELGD) != 0)
   9594     for (gent = h->got.glist; gent != NULL; gent = gent->next)
   9595       if (gent->got.refcount > 0
   9596 	  && (gent->tls_type & TLS_GD) != 0)
   9597 	{
   9598 	  /* This was a GD entry that has been converted to TPREL.  If
   9599 	     there happens to be a TPREL entry we can use that one.  */
   9600 	  struct got_entry *ent;
   9601 	  for (ent = h->got.glist; ent != NULL; ent = ent->next)
   9602 	    if (ent->got.refcount > 0
   9603 		&& (ent->tls_type & TLS_TPREL) != 0
   9604 		&& ent->addend == gent->addend
   9605 		&& ent->owner == gent->owner)
   9606 	      {
   9607 		gent->got.refcount = 0;
   9608 		break;
   9609 	      }
   9610 
   9611 	  /* If not, then we'll be using our own TPREL entry.  */
   9612 	  if (gent->got.refcount != 0)
   9613 	    gent->tls_type = TLS_TLS | TLS_TPREL;
   9614 	}
   9615 
   9616   /* Remove any list entry that won't generate a word in the GOT before
   9617      we call merge_got_entries.  Otherwise we risk merging to empty
   9618      entries.  */
   9619   pgent = &h->got.glist;
   9620   while ((gent = *pgent) != NULL)
   9621     if (gent->got.refcount > 0)
   9622       {
   9623 	if ((gent->tls_type & TLS_LD) != 0
   9624 	    && !h->def_dynamic)
   9625 	  {
   9626 	    ppc64_tlsld_got (gent->owner)->got.refcount += 1;
   9627 	    *pgent = gent->next;
   9628 	  }
   9629 	else
   9630 	  pgent = &gent->next;
   9631       }
   9632     else
   9633       *pgent = gent->next;
   9634 
   9635   if (!htab->do_multi_toc)
   9636     merge_got_entries (&h->got.glist);
   9637 
   9638   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   9639     if (!gent->is_indirect)
   9640       {
   9641 	/* Make sure this symbol is output as a dynamic symbol.
   9642 	   Undefined weak syms won't yet be marked as dynamic,
   9643 	   nor will all TLS symbols.  */
   9644 	if (h->dynindx == -1
   9645 	    && !h->forced_local
   9646 	    && h->type != STT_GNU_IFUNC
   9647 	    && htab->elf.dynamic_sections_created)
   9648 	  {
   9649 	    if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9650 	      return FALSE;
   9651 	  }
   9652 
   9653 	if (!is_ppc64_elf (gent->owner))
   9654 	  abort ();
   9655 
   9656 	allocate_got (h, info, gent);
   9657       }
   9658 
   9659   if (!htab->elf.dynamic_sections_created
   9660       && h->type != STT_GNU_IFUNC)
   9661     eh->dyn_relocs = NULL;
   9662 
   9663   if (eh->dyn_relocs != NULL)
   9664     {
   9665       struct elf_dyn_relocs *p, **pp;
   9666 
   9667       /* In the shared -Bsymbolic case, discard space allocated for
   9668 	 dynamic pc-relative relocs against symbols which turn out to
   9669 	 be defined in regular objects.  For the normal shared case,
   9670 	 discard space for relocs that have become local due to symbol
   9671 	 visibility changes.  */
   9672 
   9673       if (bfd_link_pic (info))
   9674 	{
   9675 	  /* Relocs that use pc_count are those that appear on a call
   9676 	     insn, or certain REL relocs (see must_be_dyn_reloc) that
   9677 	     can be generated via assembly.  We want calls to
   9678 	     protected symbols to resolve directly to the function
   9679 	     rather than going via the plt.  If people want function
   9680 	     pointer comparisons to work as expected then they should
   9681 	     avoid writing weird assembly.  */
   9682 	  if (SYMBOL_CALLS_LOCAL (info, h))
   9683 	    {
   9684 	      for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
   9685 		{
   9686 		  p->count -= p->pc_count;
   9687 		  p->pc_count = 0;
   9688 		  if (p->count == 0)
   9689 		    *pp = p->next;
   9690 		  else
   9691 		    pp = &p->next;
   9692 		}
   9693 	    }
   9694 
   9695 	  /* Also discard relocs on undefined weak syms with
   9696 	     non-default visibility.  */
   9697 	  if (eh->dyn_relocs != NULL
   9698 	      && h->root.type == bfd_link_hash_undefweak)
   9699 	    {
   9700 	      if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
   9701 		eh->dyn_relocs = NULL;
   9702 
   9703 	      /* Make sure this symbol is output as a dynamic symbol.
   9704 		 Undefined weak syms won't yet be marked as dynamic.  */
   9705 	      else if (h->dynindx == -1
   9706 		       && !h->forced_local)
   9707 		{
   9708 		  if (! bfd_elf_link_record_dynamic_symbol (info, h))
   9709 		    return FALSE;
   9710 		}
   9711 	    }
   9712 	}
   9713       else if (h->type == STT_GNU_IFUNC)
   9714 	{
   9715 	  /* A plt entry is always created when making direct calls to
   9716 	     an ifunc, even when building a static executable, but
   9717 	     that doesn't cover all cases.  We may have only an ifunc
   9718 	     initialised function pointer for a given ifunc symbol.
   9719 
   9720 	     For ELFv2, dynamic relocations are not required when
   9721 	     generating a global entry PLT stub.  */
   9722 	  if (abiversion (info->output_bfd) >= 2)
   9723 	    {
   9724 	      if (global_entry_stub (h))
   9725 		eh->dyn_relocs = NULL;
   9726 	    }
   9727 
   9728 	  /* For ELFv1 we have function descriptors.  Descriptors need
   9729 	     to be treated like PLT entries and thus have dynamic
   9730 	     relocations.  One exception is when the function
   9731 	     descriptor is copied into .dynbss (which should only
   9732 	     happen with ancient versions of gcc).  */
   9733 	  else if (h->needs_copy)
   9734 	    eh->dyn_relocs = NULL;
   9735 	}
   9736       else if (ELIMINATE_COPY_RELOCS)
   9737 	{
   9738 	  /* For the non-pic case, discard space for relocs against
   9739 	     symbols which turn out to need copy relocs or are not
   9740 	     dynamic.  */
   9741 
   9742 	  /* First make sure this symbol is output as a dynamic symbol.
   9743 	     Undefined weak syms won't yet be marked as dynamic.  */
   9744 	  if (h->root.type == bfd_link_hash_undefweak
   9745 	      && !h->non_got_ref
   9746 	      && !h->def_regular
   9747 	      && h->dynindx == -1
   9748 	      && !h->forced_local
   9749 	      && !bfd_elf_link_record_dynamic_symbol (info, h))
   9750 	    return FALSE;
   9751 
   9752 	  if (h->non_got_ref
   9753 	      || h->def_regular
   9754 	      || h->dynindx == -1)
   9755 	    eh->dyn_relocs = NULL;
   9756 	}
   9757 
   9758       /* Finally, allocate space.  */
   9759       for (p = eh->dyn_relocs; p != NULL; p = p->next)
   9760 	{
   9761 	  asection *sreloc = elf_section_data (p->sec)->sreloc;
   9762 	  if (eh->elf.type == STT_GNU_IFUNC)
   9763 	    sreloc = htab->elf.irelplt;
   9764 	  sreloc->size += p->count * sizeof (Elf64_External_Rela);
   9765 	}
   9766     }
   9767 
   9768   if ((htab->elf.dynamic_sections_created
   9769        && h->dynindx != -1
   9770        && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
   9771       || h->type == STT_GNU_IFUNC)
   9772     {
   9773       struct plt_entry *pent;
   9774       bfd_boolean doneone = FALSE;
   9775       for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   9776 	if (pent->plt.refcount > 0)
   9777 	  {
   9778 	    if (!htab->elf.dynamic_sections_created
   9779 		|| h->dynindx == -1)
   9780 	      {
   9781 		s = htab->elf.iplt;
   9782 		pent->plt.offset = s->size;
   9783 		s->size += PLT_ENTRY_SIZE (htab);
   9784 		s = htab->elf.irelplt;
   9785 	      }
   9786 	    else
   9787 	      {
   9788 		/* If this is the first .plt entry, make room for the special
   9789 		   first entry.  */
   9790 		s = htab->elf.splt;
   9791 		if (s->size == 0)
   9792 		  s->size += PLT_INITIAL_ENTRY_SIZE (htab);
   9793 
   9794 		pent->plt.offset = s->size;
   9795 
   9796 		/* Make room for this entry.  */
   9797 		s->size += PLT_ENTRY_SIZE (htab);
   9798 
   9799 		/* Make room for the .glink code.  */
   9800 		s = htab->glink;
   9801 		if (s->size == 0)
   9802 		  s->size += GLINK_CALL_STUB_SIZE;
   9803 		if (htab->opd_abi)
   9804 		  {
   9805 		    /* We need bigger stubs past index 32767.  */
   9806 		    if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
   9807 		      s->size += 4;
   9808 		    s->size += 2*4;
   9809 		  }
   9810 		else
   9811 		  s->size += 4;
   9812 
   9813 		/* We also need to make an entry in the .rela.plt section.  */
   9814 		s = htab->elf.srelplt;
   9815 	      }
   9816 	    s->size += sizeof (Elf64_External_Rela);
   9817 	    doneone = TRUE;
   9818 	  }
   9819 	else
   9820 	  pent->plt.offset = (bfd_vma) -1;
   9821       if (!doneone)
   9822 	{
   9823 	  h->plt.plist = NULL;
   9824 	  h->needs_plt = 0;
   9825 	}
   9826     }
   9827   else
   9828     {
   9829       h->plt.plist = NULL;
   9830       h->needs_plt = 0;
   9831     }
   9832 
   9833   return TRUE;
   9834 }
   9835 
   9836 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
   9837    to set up space for global entry stubs.  These are put in glink,
   9838    after the branch table.  */
   9839 
   9840 static bfd_boolean
   9841 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
   9842 {
   9843   struct bfd_link_info *info;
   9844   struct ppc_link_hash_table *htab;
   9845   struct plt_entry *pent;
   9846   asection *s;
   9847 
   9848   if (h->root.type == bfd_link_hash_indirect)
   9849     return TRUE;
   9850 
   9851   if (!h->pointer_equality_needed)
   9852     return TRUE;
   9853 
   9854   if (h->def_regular)
   9855     return TRUE;
   9856 
   9857   info = inf;
   9858   htab = ppc_hash_table (info);
   9859   if (htab == NULL)
   9860     return FALSE;
   9861 
   9862   s = htab->glink;
   9863   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   9864     if (pent->plt.offset != (bfd_vma) -1
   9865 	&& pent->addend == 0)
   9866       {
   9867 	/* For ELFv2, if this symbol is not defined in a regular file
   9868 	   and we are not generating a shared library or pie, then we
   9869 	   need to define the symbol in the executable on a call stub.
   9870 	   This is to avoid text relocations.  */
   9871 	s->size = (s->size + 15) & -16;
   9872 	h->root.type = bfd_link_hash_defined;
   9873 	h->root.u.def.section = s;
   9874 	h->root.u.def.value = s->size;
   9875 	s->size += 16;
   9876 	break;
   9877       }
   9878   return TRUE;
   9879 }
   9880 
   9881 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
   9882    read-only sections.  */
   9883 
   9884 static bfd_boolean
   9885 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
   9886 {
   9887   if (h->root.type == bfd_link_hash_indirect)
   9888     return TRUE;
   9889 
   9890   if (readonly_dynrelocs (h))
   9891     {
   9892       ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
   9893 
   9894       /* Not an error, just cut short the traversal.  */
   9895       return FALSE;
   9896     }
   9897   return TRUE;
   9898 }
   9899 
   9900 /* Set the sizes of the dynamic sections.  */
   9901 
   9902 static bfd_boolean
   9903 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
   9904 				 struct bfd_link_info *info)
   9905 {
   9906   struct ppc_link_hash_table *htab;
   9907   bfd *dynobj;
   9908   asection *s;
   9909   bfd_boolean relocs;
   9910   bfd *ibfd;
   9911   struct got_entry *first_tlsld;
   9912 
   9913   htab = ppc_hash_table (info);
   9914   if (htab == NULL)
   9915     return FALSE;
   9916 
   9917   dynobj = htab->elf.dynobj;
   9918   if (dynobj == NULL)
   9919     abort ();
   9920 
   9921   if (htab->elf.dynamic_sections_created)
   9922     {
   9923       /* Set the contents of the .interp section to the interpreter.  */
   9924       if (bfd_link_executable (info) && !info->nointerp)
   9925 	{
   9926 	  s = bfd_get_linker_section (dynobj, ".interp");
   9927 	  if (s == NULL)
   9928 	    abort ();
   9929 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   9930 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   9931 	}
   9932     }
   9933 
   9934   /* Set up .got offsets for local syms, and space for local dynamic
   9935      relocs.  */
   9936   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   9937     {
   9938       struct got_entry **lgot_ents;
   9939       struct got_entry **end_lgot_ents;
   9940       struct plt_entry **local_plt;
   9941       struct plt_entry **end_local_plt;
   9942       unsigned char *lgot_masks;
   9943       bfd_size_type locsymcount;
   9944       Elf_Internal_Shdr *symtab_hdr;
   9945 
   9946       if (!is_ppc64_elf (ibfd))
   9947 	continue;
   9948 
   9949       for (s = ibfd->sections; s != NULL; s = s->next)
   9950 	{
   9951 	  struct ppc_dyn_relocs *p;
   9952 
   9953 	  for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
   9954 	    {
   9955 	      if (!bfd_is_abs_section (p->sec)
   9956 		  && bfd_is_abs_section (p->sec->output_section))
   9957 		{
   9958 		  /* Input section has been discarded, either because
   9959 		     it is a copy of a linkonce section or due to
   9960 		     linker script /DISCARD/, so we'll be discarding
   9961 		     the relocs too.  */
   9962 		}
   9963 	      else if (p->count != 0)
   9964 		{
   9965 		  asection *srel = elf_section_data (p->sec)->sreloc;
   9966 		  if (p->ifunc)
   9967 		    srel = htab->elf.irelplt;
   9968 		  srel->size += p->count * sizeof (Elf64_External_Rela);
   9969 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   9970 		    info->flags |= DF_TEXTREL;
   9971 		}
   9972 	    }
   9973 	}
   9974 
   9975       lgot_ents = elf_local_got_ents (ibfd);
   9976       if (!lgot_ents)
   9977 	continue;
   9978 
   9979       symtab_hdr = &elf_symtab_hdr (ibfd);
   9980       locsymcount = symtab_hdr->sh_info;
   9981       end_lgot_ents = lgot_ents + locsymcount;
   9982       local_plt = (struct plt_entry **) end_lgot_ents;
   9983       end_local_plt = local_plt + locsymcount;
   9984       lgot_masks = (unsigned char *) end_local_plt;
   9985       s = ppc64_elf_tdata (ibfd)->got;
   9986       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   9987 	{
   9988 	  struct got_entry **pent, *ent;
   9989 
   9990 	  pent = lgot_ents;
   9991 	  while ((ent = *pent) != NULL)
   9992 	    if (ent->got.refcount > 0)
   9993 	      {
   9994 		if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
   9995 		  {
   9996 		    ppc64_tlsld_got (ibfd)->got.refcount += 1;
   9997 		    *pent = ent->next;
   9998 		  }
   9999 		else
   10000 		  {
   10001 		    unsigned int ent_size = 8;
   10002 		    unsigned int rel_size = sizeof (Elf64_External_Rela);
   10003 
   10004 		    ent->got.offset = s->size;
   10005 		    if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   10006 		      {
   10007 			ent_size *= 2;
   10008 			rel_size *= 2;
   10009 		      }
   10010 		    s->size += ent_size;
   10011 		    if ((*lgot_masks & PLT_IFUNC) != 0)
   10012 		      {
   10013 			htab->elf.irelplt->size += rel_size;
   10014 			htab->got_reli_size += rel_size;
   10015 		      }
   10016 		    else if (bfd_link_pic (info))
   10017 		      {
   10018 			asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10019 			srel->size += rel_size;
   10020 		      }
   10021 		    pent = &ent->next;
   10022 		  }
   10023 	      }
   10024 	    else
   10025 	      *pent = ent->next;
   10026 	}
   10027 
   10028       /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt.  */
   10029       for (; local_plt < end_local_plt; ++local_plt)
   10030 	{
   10031 	  struct plt_entry *ent;
   10032 
   10033 	  for (ent = *local_plt; ent != NULL; ent = ent->next)
   10034 	    if (ent->plt.refcount > 0)
   10035 	      {
   10036 		s = htab->elf.iplt;
   10037 		ent->plt.offset = s->size;
   10038 		s->size += PLT_ENTRY_SIZE (htab);
   10039 
   10040 		htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
   10041 	      }
   10042 	    else
   10043 	      ent->plt.offset = (bfd_vma) -1;
   10044 	}
   10045     }
   10046 
   10047   /* Allocate global sym .plt and .got entries, and space for global
   10048      sym dynamic relocs.  */
   10049   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
   10050   /* Stash the end of glink branch table.  */
   10051   if (htab->glink != NULL)
   10052     htab->glink->rawsize = htab->glink->size;
   10053 
   10054   if (!htab->opd_abi && !bfd_link_pic (info))
   10055     elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
   10056 
   10057   first_tlsld = NULL;
   10058   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   10059     {
   10060       struct got_entry *ent;
   10061 
   10062       if (!is_ppc64_elf (ibfd))
   10063 	continue;
   10064 
   10065       ent = ppc64_tlsld_got (ibfd);
   10066       if (ent->got.refcount > 0)
   10067 	{
   10068 	  if (!htab->do_multi_toc && first_tlsld != NULL)
   10069 	    {
   10070 	      ent->is_indirect = TRUE;
   10071 	      ent->got.ent = first_tlsld;
   10072 	    }
   10073 	  else
   10074 	    {
   10075 	      if (first_tlsld == NULL)
   10076 		first_tlsld = ent;
   10077 	      s = ppc64_elf_tdata (ibfd)->got;
   10078 	      ent->got.offset = s->size;
   10079 	      ent->owner = ibfd;
   10080 	      s->size += 16;
   10081 	      if (bfd_link_pic (info))
   10082 		{
   10083 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   10084 		  srel->size += sizeof (Elf64_External_Rela);
   10085 		}
   10086 	    }
   10087 	}
   10088       else
   10089 	ent->got.offset = (bfd_vma) -1;
   10090     }
   10091 
   10092   /* We now have determined the sizes of the various dynamic sections.
   10093      Allocate memory for them.  */
   10094   relocs = FALSE;
   10095   for (s = dynobj->sections; s != NULL; s = s->next)
   10096     {
   10097       if ((s->flags & SEC_LINKER_CREATED) == 0)
   10098 	continue;
   10099 
   10100       if (s == htab->brlt || s == htab->relbrlt)
   10101 	/* These haven't been allocated yet;  don't strip.  */
   10102 	continue;
   10103       else if (s == htab->elf.sgot
   10104 	       || s == htab->elf.splt
   10105 	       || s == htab->elf.iplt
   10106 	       || s == htab->glink
   10107 	       || s == htab->dynbss)
   10108 	{
   10109 	  /* Strip this section if we don't need it; see the
   10110 	     comment below.  */
   10111 	}
   10112       else if (s == htab->glink_eh_frame)
   10113 	{
   10114 	  if (!bfd_is_abs_section (s->output_section))
   10115 	    /* Not sized yet.  */
   10116 	    continue;
   10117 	}
   10118       else if (CONST_STRNEQ (s->name, ".rela"))
   10119 	{
   10120 	  if (s->size != 0)
   10121 	    {
   10122 	      if (s != htab->elf.srelplt)
   10123 		relocs = TRUE;
   10124 
   10125 	      /* We use the reloc_count field as a counter if we need
   10126 		 to copy relocs into the output file.  */
   10127 	      s->reloc_count = 0;
   10128 	    }
   10129 	}
   10130       else
   10131 	{
   10132 	  /* It's not one of our sections, so don't allocate space.  */
   10133 	  continue;
   10134 	}
   10135 
   10136       if (s->size == 0)
   10137 	{
   10138 	  /* If we don't need this section, strip it from the
   10139 	     output file.  This is mostly to handle .rela.bss and
   10140 	     .rela.plt.  We must create both sections in
   10141 	     create_dynamic_sections, because they must be created
   10142 	     before the linker maps input sections to output
   10143 	     sections.  The linker does that before
   10144 	     adjust_dynamic_symbol is called, and it is that
   10145 	     function which decides whether anything needs to go
   10146 	     into these sections.  */
   10147 	  s->flags |= SEC_EXCLUDE;
   10148 	  continue;
   10149 	}
   10150 
   10151       if ((s->flags & SEC_HAS_CONTENTS) == 0)
   10152 	continue;
   10153 
   10154       /* Allocate memory for the section contents.  We use bfd_zalloc
   10155 	 here in case unused entries are not reclaimed before the
   10156 	 section's contents are written out.  This should not happen,
   10157 	 but this way if it does we get a R_PPC64_NONE reloc in .rela
   10158 	 sections instead of garbage.
   10159 	 We also rely on the section contents being zero when writing
   10160 	 the GOT.  */
   10161       s->contents = bfd_zalloc (dynobj, s->size);
   10162       if (s->contents == NULL)
   10163 	return FALSE;
   10164     }
   10165 
   10166   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   10167     {
   10168       if (!is_ppc64_elf (ibfd))
   10169 	continue;
   10170 
   10171       s = ppc64_elf_tdata (ibfd)->got;
   10172       if (s != NULL && s != htab->elf.sgot)
   10173 	{
   10174 	  if (s->size == 0)
   10175 	    s->flags |= SEC_EXCLUDE;
   10176 	  else
   10177 	    {
   10178 	      s->contents = bfd_zalloc (ibfd, s->size);
   10179 	      if (s->contents == NULL)
   10180 		return FALSE;
   10181 	    }
   10182 	}
   10183       s = ppc64_elf_tdata (ibfd)->relgot;
   10184       if (s != NULL)
   10185 	{
   10186 	  if (s->size == 0)
   10187 	    s->flags |= SEC_EXCLUDE;
   10188 	  else
   10189 	    {
   10190 	      s->contents = bfd_zalloc (ibfd, s->size);
   10191 	      if (s->contents == NULL)
   10192 		return FALSE;
   10193 	      relocs = TRUE;
   10194 	      s->reloc_count = 0;
   10195 	    }
   10196 	}
   10197     }
   10198 
   10199   if (htab->elf.dynamic_sections_created)
   10200     {
   10201       bfd_boolean tls_opt;
   10202 
   10203       /* Add some entries to the .dynamic section.  We fill in the
   10204 	 values later, in ppc64_elf_finish_dynamic_sections, but we
   10205 	 must add the entries now so that we get the correct size for
   10206 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   10207 	 dynamic linker and used by the debugger.  */
   10208 #define add_dynamic_entry(TAG, VAL) \
   10209   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   10210 
   10211       if (bfd_link_executable (info))
   10212 	{
   10213 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   10214 	    return FALSE;
   10215 	}
   10216 
   10217       if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
   10218 	{
   10219 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   10220 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   10221 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   10222 	      || !add_dynamic_entry (DT_JMPREL, 0)
   10223 	      || !add_dynamic_entry (DT_PPC64_GLINK, 0))
   10224 	    return FALSE;
   10225 	}
   10226 
   10227       if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
   10228 	{
   10229 	  if (!add_dynamic_entry (DT_PPC64_OPD, 0)
   10230 	      || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
   10231 	    return FALSE;
   10232 	}
   10233 
   10234       tls_opt = (htab->params->tls_get_addr_opt
   10235 		 && htab->tls_get_addr_fd != NULL
   10236 		 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
   10237       if (tls_opt || !htab->opd_abi)
   10238 	{
   10239 	  if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
   10240 	    return FALSE;
   10241 	}
   10242 
   10243       if (relocs)
   10244 	{
   10245 	  if (!add_dynamic_entry (DT_RELA, 0)
   10246 	      || !add_dynamic_entry (DT_RELASZ, 0)
   10247 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
   10248 	    return FALSE;
   10249 
   10250 	  /* If any dynamic relocs apply to a read-only section,
   10251 	     then we need a DT_TEXTREL entry.  */
   10252 	  if ((info->flags & DF_TEXTREL) == 0)
   10253 	    elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
   10254 
   10255 	  if ((info->flags & DF_TEXTREL) != 0)
   10256 	    {
   10257 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   10258 		return FALSE;
   10259 	    }
   10260 	}
   10261     }
   10262 #undef add_dynamic_entry
   10263 
   10264   return TRUE;
   10265 }
   10266 
   10267 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
   10268 
   10269 static bfd_boolean
   10270 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
   10271 {
   10272   if (h->plt.plist != NULL
   10273       && !h->def_regular
   10274       && !h->pointer_equality_needed)
   10275     return FALSE;
   10276 
   10277   return _bfd_elf_hash_symbol (h);
   10278 }
   10279 
   10280 /* Determine the type of stub needed, if any, for a call.  */
   10281 
   10282 static inline enum ppc_stub_type
   10283 ppc_type_of_stub (asection *input_sec,
   10284 		  const Elf_Internal_Rela *rel,
   10285 		  struct ppc_link_hash_entry **hash,
   10286 		  struct plt_entry **plt_ent,
   10287 		  bfd_vma destination,
   10288 		  unsigned long local_off)
   10289 {
   10290   struct ppc_link_hash_entry *h = *hash;
   10291   bfd_vma location;
   10292   bfd_vma branch_offset;
   10293   bfd_vma max_branch_offset;
   10294   enum elf_ppc64_reloc_type r_type;
   10295 
   10296   if (h != NULL)
   10297     {
   10298       struct plt_entry *ent;
   10299       struct ppc_link_hash_entry *fdh = h;
   10300       if (h->oh != NULL
   10301 	  && h->oh->is_func_descriptor)
   10302 	{
   10303 	  fdh = ppc_follow_link (h->oh);
   10304 	  *hash = fdh;
   10305 	}
   10306 
   10307       for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
   10308 	if (ent->addend == rel->r_addend
   10309 	    && ent->plt.offset != (bfd_vma) -1)
   10310 	  {
   10311 	    *plt_ent = ent;
   10312 	    return ppc_stub_plt_call;
   10313 	  }
   10314 
   10315       /* Here, we know we don't have a plt entry.  If we don't have a
   10316 	 either a defined function descriptor or a defined entry symbol
   10317 	 in a regular object file, then it is pointless trying to make
   10318 	 any other type of stub.  */
   10319       if (!is_static_defined (&fdh->elf)
   10320 	  && !is_static_defined (&h->elf))
   10321 	return ppc_stub_none;
   10322     }
   10323   else if (elf_local_got_ents (input_sec->owner) != NULL)
   10324     {
   10325       Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
   10326       struct plt_entry **local_plt = (struct plt_entry **)
   10327 	elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
   10328       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
   10329 
   10330       if (local_plt[r_symndx] != NULL)
   10331 	{
   10332 	  struct plt_entry *ent;
   10333 
   10334 	  for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
   10335 	    if (ent->addend == rel->r_addend
   10336 		&& ent->plt.offset != (bfd_vma) -1)
   10337 	      {
   10338 		*plt_ent = ent;
   10339 		return ppc_stub_plt_call;
   10340 	      }
   10341 	}
   10342     }
   10343 
   10344   /* Determine where the call point is.  */
   10345   location = (input_sec->output_offset
   10346 	      + input_sec->output_section->vma
   10347 	      + rel->r_offset);
   10348 
   10349   branch_offset = destination - location;
   10350   r_type = ELF64_R_TYPE (rel->r_info);
   10351 
   10352   /* Determine if a long branch stub is needed.  */
   10353   max_branch_offset = 1 << 25;
   10354   if (r_type != R_PPC64_REL24)
   10355     max_branch_offset = 1 << 15;
   10356 
   10357   if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
   10358     /* We need a stub.  Figure out whether a long_branch or plt_branch
   10359        is needed later.  */
   10360     return ppc_stub_long_branch;
   10361 
   10362   return ppc_stub_none;
   10363 }
   10364 
   10365 /* With power7 weakly ordered memory model, it is possible for ld.so
   10366    to update a plt entry in one thread and have another thread see a
   10367    stale zero toc entry.  To avoid this we need some sort of acquire
   10368    barrier in the call stub.  One solution is to make the load of the
   10369    toc word seem to appear to depend on the load of the function entry
   10370    word.  Another solution is to test for r2 being zero, and branch to
   10371    the appropriate glink entry if so.
   10372 
   10373    .	fake dep barrier	compare
   10374    .	ld 12,xxx(2)		ld 12,xxx(2)
   10375    .	mtctr 12		mtctr 12
   10376    .	xor 11,12,12		ld 2,xxx+8(2)
   10377    .	add 2,2,11		cmpldi 2,0
   10378    .	ld 2,xxx+8(2)		bnectr+
   10379    .	bctr			b <glink_entry>
   10380 
   10381    The solution involving the compare turns out to be faster, so
   10382    that's what we use unless the branch won't reach.  */
   10383 
   10384 #define ALWAYS_USE_FAKE_DEP 0
   10385 #define ALWAYS_EMIT_R2SAVE 0
   10386 
   10387 #define PPC_LO(v) ((v) & 0xffff)
   10388 #define PPC_HI(v) (((v) >> 16) & 0xffff)
   10389 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
   10390 
   10391 static inline unsigned int
   10392 plt_stub_size (struct ppc_link_hash_table *htab,
   10393 	       struct ppc_stub_hash_entry *stub_entry,
   10394 	       bfd_vma off)
   10395 {
   10396   unsigned size = 12;
   10397 
   10398   if (ALWAYS_EMIT_R2SAVE
   10399       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10400     size += 4;
   10401   if (PPC_HA (off) != 0)
   10402     size += 4;
   10403   if (htab->opd_abi)
   10404     {
   10405       size += 4;
   10406       if (htab->params->plt_static_chain)
   10407 	size += 4;
   10408       if (htab->params->plt_thread_safe
   10409 	  && htab->elf.dynamic_sections_created
   10410 	  && stub_entry->h != NULL
   10411 	  && stub_entry->h->elf.dynindx != -1)
   10412 	size += 8;
   10413       if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
   10414 	size += 4;
   10415     }
   10416   if (stub_entry->h != NULL
   10417       && (stub_entry->h == htab->tls_get_addr_fd
   10418 	  || stub_entry->h == htab->tls_get_addr)
   10419       && htab->params->tls_get_addr_opt)
   10420     size += 13 * 4;
   10421   return size;
   10422 }
   10423 
   10424 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
   10425    then return the padding needed to do so.  */
   10426 static inline unsigned int
   10427 plt_stub_pad (struct ppc_link_hash_table *htab,
   10428 	      struct ppc_stub_hash_entry *stub_entry,
   10429 	      bfd_vma plt_off)
   10430 {
   10431   int stub_align = 1 << htab->params->plt_stub_align;
   10432   unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
   10433   bfd_vma stub_off = stub_entry->group->stub_sec->size;
   10434 
   10435   if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
   10436       > ((stub_size - 1) & -stub_align))
   10437     return stub_align - (stub_off & (stub_align - 1));
   10438   return 0;
   10439 }
   10440 
   10441 /* Build a .plt call stub.  */
   10442 
   10443 static inline bfd_byte *
   10444 build_plt_stub (struct ppc_link_hash_table *htab,
   10445 		struct ppc_stub_hash_entry *stub_entry,
   10446 		bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   10447 {
   10448   bfd *obfd = htab->params->stub_bfd;
   10449   bfd_boolean plt_load_toc = htab->opd_abi;
   10450   bfd_boolean plt_static_chain = htab->params->plt_static_chain;
   10451   bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
   10452 				 && htab->elf.dynamic_sections_created
   10453 				 && stub_entry->h != NULL
   10454 				 && stub_entry->h->elf.dynindx != -1);
   10455   bfd_boolean use_fake_dep = plt_thread_safe;
   10456   bfd_vma cmp_branch_off = 0;
   10457 
   10458   if (!ALWAYS_USE_FAKE_DEP
   10459       && plt_load_toc
   10460       && plt_thread_safe
   10461       && !((stub_entry->h == htab->tls_get_addr_fd
   10462 	    || stub_entry->h == htab->tls_get_addr)
   10463 	   && htab->params->tls_get_addr_opt))
   10464     {
   10465       bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
   10466       bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
   10467 			  / PLT_ENTRY_SIZE (htab));
   10468       bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
   10469       bfd_vma to, from;
   10470 
   10471       if (pltindex > 32768)
   10472 	glinkoff += (pltindex - 32768) * 4;
   10473       to = (glinkoff
   10474 	    + htab->glink->output_offset
   10475 	    + htab->glink->output_section->vma);
   10476       from = (p - stub_entry->group->stub_sec->contents
   10477 	      + 4 * (ALWAYS_EMIT_R2SAVE
   10478 		     || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10479 	      + 4 * (PPC_HA (offset) != 0)
   10480 	      + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
   10481 		     != PPC_HA (offset))
   10482 	      + 4 * (plt_static_chain != 0)
   10483 	      + 20
   10484 	      + stub_entry->group->stub_sec->output_offset
   10485 	      + stub_entry->group->stub_sec->output_section->vma);
   10486       cmp_branch_off = to - from;
   10487       use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
   10488     }
   10489 
   10490   if (PPC_HA (offset) != 0)
   10491     {
   10492       if (r != NULL)
   10493 	{
   10494 	  if (ALWAYS_EMIT_R2SAVE
   10495 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10496 	    r[0].r_offset += 4;
   10497 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   10498 	  r[1].r_offset = r[0].r_offset + 4;
   10499 	  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10500 	  r[1].r_addend = r[0].r_addend;
   10501 	  if (plt_load_toc)
   10502 	    {
   10503 	      if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   10504 		{
   10505 		  r[2].r_offset = r[1].r_offset + 4;
   10506 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
   10507 		  r[2].r_addend = r[0].r_addend;
   10508 		}
   10509 	      else
   10510 		{
   10511 		  r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
   10512 		  r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10513 		  r[2].r_addend = r[0].r_addend + 8;
   10514 		  if (plt_static_chain)
   10515 		    {
   10516 		      r[3].r_offset = r[2].r_offset + 4;
   10517 		      r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10518 		      r[3].r_addend = r[0].r_addend + 16;
   10519 		    }
   10520 		}
   10521 	    }
   10522 	}
   10523       if (ALWAYS_EMIT_R2SAVE
   10524 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10525 	bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   10526       if (plt_load_toc)
   10527 	{
   10528 	  bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p),	p += 4;
   10529 	  bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p),	p += 4;
   10530 	}
   10531       else
   10532 	{
   10533 	  bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p),	p += 4;
   10534 	  bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p),	p += 4;
   10535 	}
   10536       if (plt_load_toc
   10537 	  && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   10538 	{
   10539 	  bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p),	p += 4;
   10540 	  offset = 0;
   10541 	}
   10542       bfd_put_32 (obfd, MTCTR_R12, p),				p += 4;
   10543       if (plt_load_toc)
   10544 	{
   10545 	  if (use_fake_dep)
   10546 	    {
   10547 	      bfd_put_32 (obfd, XOR_R2_R12_R12, p),		p += 4;
   10548 	      bfd_put_32 (obfd, ADD_R11_R11_R2, p),		p += 4;
   10549 	    }
   10550 	  bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
   10551 	  if (plt_static_chain)
   10552 	    bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
   10553 	}
   10554     }
   10555   else
   10556     {
   10557       if (r != NULL)
   10558 	{
   10559 	  if (ALWAYS_EMIT_R2SAVE
   10560 	      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10561 	    r[0].r_offset += 4;
   10562 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10563 	  if (plt_load_toc)
   10564 	    {
   10565 	      if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   10566 		{
   10567 		  r[1].r_offset = r[0].r_offset + 4;
   10568 		  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
   10569 		  r[1].r_addend = r[0].r_addend;
   10570 		}
   10571 	      else
   10572 		{
   10573 		  r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
   10574 		  r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10575 		  r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
   10576 		  if (plt_static_chain)
   10577 		    {
   10578 		      r[2].r_offset = r[1].r_offset + 4;
   10579 		      r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10580 		      r[2].r_addend = r[0].r_addend + 8;
   10581 		    }
   10582 		}
   10583 	    }
   10584 	}
   10585       if (ALWAYS_EMIT_R2SAVE
   10586 	  || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   10587 	bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   10588       bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p),	p += 4;
   10589       if (plt_load_toc
   10590 	  && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
   10591 	{
   10592 	  bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p),	p += 4;
   10593 	  offset = 0;
   10594 	}
   10595       bfd_put_32 (obfd, MTCTR_R12, p),				p += 4;
   10596       if (plt_load_toc)
   10597 	{
   10598 	  if (use_fake_dep)
   10599 	    {
   10600 	      bfd_put_32 (obfd, XOR_R11_R12_R12, p),		p += 4;
   10601 	      bfd_put_32 (obfd, ADD_R2_R2_R11, p),		p += 4;
   10602 	    }
   10603 	  if (plt_static_chain)
   10604 	    bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
   10605 	  bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
   10606 	}
   10607     }
   10608   if (plt_load_toc && plt_thread_safe && !use_fake_dep)
   10609     {
   10610       bfd_put_32 (obfd, CMPLDI_R2_0, p),			p += 4;
   10611       bfd_put_32 (obfd, BNECTR_P4, p),				p += 4;
   10612       bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
   10613     }
   10614   else
   10615     bfd_put_32 (obfd, BCTR, p),					p += 4;
   10616   return p;
   10617 }
   10618 
   10619 /* Build a special .plt call stub for __tls_get_addr.  */
   10620 
   10621 #define LD_R11_0R3	0xe9630000
   10622 #define LD_R12_0R3	0xe9830000
   10623 #define MR_R0_R3	0x7c601b78
   10624 #define CMPDI_R11_0	0x2c2b0000
   10625 #define ADD_R3_R12_R13	0x7c6c6a14
   10626 #define BEQLR		0x4d820020
   10627 #define MR_R3_R0	0x7c030378
   10628 #define STD_R11_0R1	0xf9610000
   10629 #define BCTRL		0x4e800421
   10630 #define LD_R11_0R1	0xe9610000
   10631 #define MTLR_R11	0x7d6803a6
   10632 
   10633 static inline bfd_byte *
   10634 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
   10635 			 struct ppc_stub_hash_entry *stub_entry,
   10636 			 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
   10637 {
   10638   bfd *obfd = htab->params->stub_bfd;
   10639 
   10640   bfd_put_32 (obfd, LD_R11_0R3 + 0, p),		p += 4;
   10641   bfd_put_32 (obfd, LD_R12_0R3 + 8, p),		p += 4;
   10642   bfd_put_32 (obfd, MR_R0_R3, p),		p += 4;
   10643   bfd_put_32 (obfd, CMPDI_R11_0, p),		p += 4;
   10644   bfd_put_32 (obfd, ADD_R3_R12_R13, p),		p += 4;
   10645   bfd_put_32 (obfd, BEQLR, p),			p += 4;
   10646   bfd_put_32 (obfd, MR_R3_R0, p),		p += 4;
   10647   bfd_put_32 (obfd, MFLR_R11, p),		p += 4;
   10648   bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
   10649 
   10650   if (r != NULL)
   10651     r[0].r_offset += 9 * 4;
   10652   p = build_plt_stub (htab, stub_entry, p, offset, r);
   10653   bfd_put_32 (obfd, BCTRL, p - 4);
   10654 
   10655   bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p),	p += 4;
   10656   bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p),	p += 4;
   10657   bfd_put_32 (obfd, MTLR_R11, p),		p += 4;
   10658   bfd_put_32 (obfd, BLR, p),			p += 4;
   10659 
   10660   return p;
   10661 }
   10662 
   10663 static Elf_Internal_Rela *
   10664 get_relocs (asection *sec, int count)
   10665 {
   10666   Elf_Internal_Rela *relocs;
   10667   struct bfd_elf_section_data *elfsec_data;
   10668 
   10669   elfsec_data = elf_section_data (sec);
   10670   relocs = elfsec_data->relocs;
   10671   if (relocs == NULL)
   10672     {
   10673       bfd_size_type relsize;
   10674       relsize = sec->reloc_count * sizeof (*relocs);
   10675       relocs = bfd_alloc (sec->owner, relsize);
   10676       if (relocs == NULL)
   10677 	return NULL;
   10678       elfsec_data->relocs = relocs;
   10679       elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
   10680 					  sizeof (Elf_Internal_Shdr));
   10681       if (elfsec_data->rela.hdr == NULL)
   10682 	return NULL;
   10683       elfsec_data->rela.hdr->sh_size = (sec->reloc_count
   10684 					* sizeof (Elf64_External_Rela));
   10685       elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
   10686       sec->reloc_count = 0;
   10687     }
   10688   relocs += sec->reloc_count;
   10689   sec->reloc_count += count;
   10690   return relocs;
   10691 }
   10692 
   10693 static bfd_vma
   10694 get_r2off (struct bfd_link_info *info,
   10695 	   struct ppc_stub_hash_entry *stub_entry)
   10696 {
   10697   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   10698   bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
   10699 
   10700   if (r2off == 0)
   10701     {
   10702       /* Support linking -R objects.  Get the toc pointer from the
   10703 	 opd entry.  */
   10704       char buf[8];
   10705       if (!htab->opd_abi)
   10706 	return r2off;
   10707       asection *opd = stub_entry->h->elf.root.u.def.section;
   10708       bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
   10709 
   10710       if (strcmp (opd->name, ".opd") != 0
   10711 	  || opd->reloc_count != 0)
   10712 	{
   10713 	  info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
   10714 				  stub_entry->h->elf.root.root.string);
   10715 	  bfd_set_error (bfd_error_bad_value);
   10716 	  return (bfd_vma) -1;
   10717 	}
   10718       if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
   10719 	return (bfd_vma) -1;
   10720       r2off = bfd_get_64 (opd->owner, buf);
   10721       r2off -= elf_gp (info->output_bfd);
   10722     }
   10723   r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
   10724   return r2off;
   10725 }
   10726 
   10727 static bfd_boolean
   10728 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   10729 {
   10730   struct ppc_stub_hash_entry *stub_entry;
   10731   struct ppc_branch_hash_entry *br_entry;
   10732   struct bfd_link_info *info;
   10733   struct ppc_link_hash_table *htab;
   10734   bfd_byte *loc;
   10735   bfd_byte *p;
   10736   bfd_vma dest, off;
   10737   int size;
   10738   Elf_Internal_Rela *r;
   10739   asection *plt;
   10740 
   10741   /* Massage our args to the form they really have.  */
   10742   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   10743   info = in_arg;
   10744 
   10745   htab = ppc_hash_table (info);
   10746   if (htab == NULL)
   10747     return FALSE;
   10748 
   10749   /* Make a note of the offset within the stubs for this entry.  */
   10750   stub_entry->stub_offset = stub_entry->group->stub_sec->size;
   10751   loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
   10752 
   10753   htab->stub_count[stub_entry->stub_type - 1] += 1;
   10754   switch (stub_entry->stub_type)
   10755     {
   10756     case ppc_stub_long_branch:
   10757     case ppc_stub_long_branch_r2off:
   10758       /* Branches are relative.  This is where we are going to.  */
   10759       dest = (stub_entry->target_value
   10760 	      + stub_entry->target_section->output_offset
   10761 	      + stub_entry->target_section->output_section->vma);
   10762       dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   10763       off = dest;
   10764 
   10765       /* And this is where we are coming from.  */
   10766       off -= (stub_entry->stub_offset
   10767 	      + stub_entry->group->stub_sec->output_offset
   10768 	      + stub_entry->group->stub_sec->output_section->vma);
   10769 
   10770       size = 4;
   10771       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   10772 	{
   10773 	  bfd_vma r2off = get_r2off (info, stub_entry);
   10774 
   10775 	  if (r2off == (bfd_vma) -1)
   10776 	    {
   10777 	      htab->stub_error = TRUE;
   10778 	      return FALSE;
   10779 	    }
   10780 	  bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
   10781 	  loc += 4;
   10782 	  size = 8;
   10783 	  if (PPC_HA (r2off) != 0)
   10784 	    {
   10785 	      bfd_put_32 (htab->params->stub_bfd,
   10786 			  ADDIS_R2_R2 | PPC_HA (r2off), loc);
   10787 	      loc += 4;
   10788 	      size += 4;
   10789 	    }
   10790 	  if (PPC_LO (r2off) != 0)
   10791 	    {
   10792 	      bfd_put_32 (htab->params->stub_bfd,
   10793 			  ADDI_R2_R2 | PPC_LO (r2off), loc);
   10794 	      loc += 4;
   10795 	      size += 4;
   10796 	    }
   10797 	  off -= size - 4;
   10798 	}
   10799       bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
   10800 
   10801       if (off + (1 << 25) >= (bfd_vma) (1 << 26))
   10802 	{
   10803 	  info->callbacks->einfo
   10804 	    (_("%P: long branch stub `%s' offset overflow\n"),
   10805 	     stub_entry->root.string);
   10806 	  htab->stub_error = TRUE;
   10807 	  return FALSE;
   10808 	}
   10809 
   10810       if (info->emitrelocations)
   10811 	{
   10812 	  r = get_relocs (stub_entry->group->stub_sec, 1);
   10813 	  if (r == NULL)
   10814 	    return FALSE;
   10815 	  r->r_offset = loc - stub_entry->group->stub_sec->contents;
   10816 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
   10817 	  r->r_addend = dest;
   10818 	  if (stub_entry->h != NULL)
   10819 	    {
   10820 	      struct elf_link_hash_entry **hashes;
   10821 	      unsigned long symndx;
   10822 	      struct ppc_link_hash_entry *h;
   10823 
   10824 	      hashes = elf_sym_hashes (htab->params->stub_bfd);
   10825 	      if (hashes == NULL)
   10826 		{
   10827 		  bfd_size_type hsize;
   10828 
   10829 		  hsize = (htab->stub_globals + 1) * sizeof (*hashes);
   10830 		  hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
   10831 		  if (hashes == NULL)
   10832 		    return FALSE;
   10833 		  elf_sym_hashes (htab->params->stub_bfd) = hashes;
   10834 		  htab->stub_globals = 1;
   10835 		}
   10836 	      symndx = htab->stub_globals++;
   10837 	      h = stub_entry->h;
   10838 	      hashes[symndx] = &h->elf;
   10839 	      r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
   10840 	      if (h->oh != NULL && h->oh->is_func)
   10841 		h = ppc_follow_link (h->oh);
   10842 	      if (h->elf.root.u.def.section != stub_entry->target_section)
   10843 		/* H is an opd symbol.  The addend must be zero.  */
   10844 		r->r_addend = 0;
   10845 	      else
   10846 		{
   10847 		  off = (h->elf.root.u.def.value
   10848 			 + h->elf.root.u.def.section->output_offset
   10849 			 + h->elf.root.u.def.section->output_section->vma);
   10850 		  r->r_addend -= off;
   10851 		}
   10852 	    }
   10853 	}
   10854       break;
   10855 
   10856     case ppc_stub_plt_branch:
   10857     case ppc_stub_plt_branch_r2off:
   10858       br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   10859 					 stub_entry->root.string + 9,
   10860 					 FALSE, FALSE);
   10861       if (br_entry == NULL)
   10862 	{
   10863 	  info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
   10864 				  stub_entry->root.string);
   10865 	  htab->stub_error = TRUE;
   10866 	  return FALSE;
   10867 	}
   10868 
   10869       dest = (stub_entry->target_value
   10870 	      + stub_entry->target_section->output_offset
   10871 	      + stub_entry->target_section->output_section->vma);
   10872       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10873 	dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   10874 
   10875       bfd_put_64 (htab->brlt->owner, dest,
   10876 		  htab->brlt->contents + br_entry->offset);
   10877 
   10878       if (br_entry->iter == htab->stub_iteration)
   10879 	{
   10880 	  br_entry->iter = 0;
   10881 
   10882 	  if (htab->relbrlt != NULL)
   10883 	    {
   10884 	      /* Create a reloc for the branch lookup table entry.  */
   10885 	      Elf_Internal_Rela rela;
   10886 	      bfd_byte *rl;
   10887 
   10888 	      rela.r_offset = (br_entry->offset
   10889 			       + htab->brlt->output_offset
   10890 			       + htab->brlt->output_section->vma);
   10891 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   10892 	      rela.r_addend = dest;
   10893 
   10894 	      rl = htab->relbrlt->contents;
   10895 	      rl += (htab->relbrlt->reloc_count++
   10896 		     * sizeof (Elf64_External_Rela));
   10897 	      bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
   10898 	    }
   10899 	  else if (info->emitrelocations)
   10900 	    {
   10901 	      r = get_relocs (htab->brlt, 1);
   10902 	      if (r == NULL)
   10903 		return FALSE;
   10904 	      /* brlt, being SEC_LINKER_CREATED does not go through the
   10905 		 normal reloc processing.  Symbols and offsets are not
   10906 		 translated from input file to output file form, so
   10907 		 set up the offset per the output file.  */
   10908 	      r->r_offset = (br_entry->offset
   10909 			     + htab->brlt->output_offset
   10910 			     + htab->brlt->output_section->vma);
   10911 	      r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   10912 	      r->r_addend = dest;
   10913 	    }
   10914 	}
   10915 
   10916       dest = (br_entry->offset
   10917 	      + htab->brlt->output_offset
   10918 	      + htab->brlt->output_section->vma);
   10919 
   10920       off = (dest
   10921 	     - elf_gp (htab->brlt->output_section->owner)
   10922 	     - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   10923 
   10924       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   10925 	{
   10926 	  info->callbacks->einfo
   10927 	    (_("%P: linkage table error against `%T'\n"),
   10928 	     stub_entry->root.string);
   10929 	  bfd_set_error (bfd_error_bad_value);
   10930 	  htab->stub_error = TRUE;
   10931 	  return FALSE;
   10932 	}
   10933 
   10934       if (info->emitrelocations)
   10935 	{
   10936 	  r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
   10937 	  if (r == NULL)
   10938 	    return FALSE;
   10939 	  r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
   10940 	  if (bfd_big_endian (info->output_bfd))
   10941 	    r[0].r_offset += 2;
   10942 	  if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
   10943 	    r[0].r_offset += 4;
   10944 	  r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
   10945 	  r[0].r_addend = dest;
   10946 	  if (PPC_HA (off) != 0)
   10947 	    {
   10948 	      r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
   10949 	      r[1].r_offset = r[0].r_offset + 4;
   10950 	      r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
   10951 	      r[1].r_addend = r[0].r_addend;
   10952 	    }
   10953 	}
   10954 
   10955       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   10956 	{
   10957 	  if (PPC_HA (off) != 0)
   10958 	    {
   10959 	      size = 16;
   10960 	      bfd_put_32 (htab->params->stub_bfd,
   10961 			  ADDIS_R12_R2 | PPC_HA (off), loc);
   10962 	      loc += 4;
   10963 	      bfd_put_32 (htab->params->stub_bfd,
   10964 			  LD_R12_0R12 | PPC_LO (off), loc);
   10965 	    }
   10966 	  else
   10967 	    {
   10968 	      size = 12;
   10969 	      bfd_put_32 (htab->params->stub_bfd,
   10970 			  LD_R12_0R2 | PPC_LO (off), loc);
   10971 	    }
   10972 	}
   10973       else
   10974 	{
   10975 	  bfd_vma r2off = get_r2off (info, stub_entry);
   10976 
   10977 	  if (r2off == (bfd_vma) -1)
   10978 	    {
   10979 	      htab->stub_error = TRUE;
   10980 	      return FALSE;
   10981 	    }
   10982 
   10983 	  bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
   10984 	  loc += 4;
   10985 	  size = 16;
   10986 	  if (PPC_HA (off) != 0)
   10987 	    {
   10988 	      size += 4;
   10989 	      bfd_put_32 (htab->params->stub_bfd,
   10990 			  ADDIS_R12_R2 | PPC_HA (off), loc);
   10991 	      loc += 4;
   10992 	      bfd_put_32 (htab->params->stub_bfd,
   10993 			  LD_R12_0R12 | PPC_LO (off), loc);
   10994 	    }
   10995 	  else
   10996 	    bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
   10997 
   10998 	  if (PPC_HA (r2off) != 0)
   10999 	    {
   11000 	      size += 4;
   11001 	      loc += 4;
   11002 	      bfd_put_32 (htab->params->stub_bfd,
   11003 			  ADDIS_R2_R2 | PPC_HA (r2off), loc);
   11004 	    }
   11005 	  if (PPC_LO (r2off) != 0)
   11006 	    {
   11007 	      size += 4;
   11008 	      loc += 4;
   11009 	      bfd_put_32 (htab->params->stub_bfd,
   11010 			  ADDI_R2_R2 | PPC_LO (r2off), loc);
   11011 	    }
   11012 	}
   11013       loc += 4;
   11014       bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
   11015       loc += 4;
   11016       bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
   11017       break;
   11018 
   11019     case ppc_stub_plt_call:
   11020     case ppc_stub_plt_call_r2save:
   11021       if (stub_entry->h != NULL
   11022 	  && stub_entry->h->is_func_descriptor
   11023 	  && stub_entry->h->oh != NULL)
   11024 	{
   11025 	  struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
   11026 
   11027 	  /* If the old-ABI "dot-symbol" is undefined make it weak so
   11028 	     we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.  */
   11029 	  if (fh->elf.root.type == bfd_link_hash_undefined)
   11030 	    fh->elf.root.type = bfd_link_hash_undefweak;
   11031 	  /* Stop undo_symbol_twiddle changing it back to undefined.  */
   11032 	  fh->was_undefined = 0;
   11033 	}
   11034 
   11035       /* Now build the stub.  */
   11036       dest = stub_entry->plt_ent->plt.offset & ~1;
   11037       if (dest >= (bfd_vma) -2)
   11038 	abort ();
   11039 
   11040       plt = htab->elf.splt;
   11041       if (!htab->elf.dynamic_sections_created
   11042 	  || stub_entry->h == NULL
   11043 	  || stub_entry->h->elf.dynindx == -1)
   11044 	plt = htab->elf.iplt;
   11045 
   11046       dest += plt->output_offset + plt->output_section->vma;
   11047 
   11048       if (stub_entry->h == NULL
   11049 	  && (stub_entry->plt_ent->plt.offset & 1) == 0)
   11050 	{
   11051 	  Elf_Internal_Rela rela;
   11052 	  bfd_byte *rl;
   11053 
   11054 	  rela.r_offset = dest;
   11055 	  if (htab->opd_abi)
   11056 	    rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   11057 	  else
   11058 	    rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   11059 	  rela.r_addend = (stub_entry->target_value
   11060 			   + stub_entry->target_section->output_offset
   11061 			   + stub_entry->target_section->output_section->vma);
   11062 
   11063 	  rl = (htab->elf.irelplt->contents
   11064 		+ (htab->elf.irelplt->reloc_count++
   11065 		   * sizeof (Elf64_External_Rela)));
   11066 	  bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
   11067 	  stub_entry->plt_ent->plt.offset |= 1;
   11068 	}
   11069 
   11070       off = (dest
   11071 	     - elf_gp (plt->output_section->owner)
   11072 	     - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   11073 
   11074       if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
   11075 	{
   11076 	  info->callbacks->einfo
   11077 	    (_("%P: linkage table error against `%T'\n"),
   11078 	     stub_entry->h != NULL
   11079 	     ? stub_entry->h->elf.root.root.string
   11080 	     : "<local sym>");
   11081 	  bfd_set_error (bfd_error_bad_value);
   11082 	  htab->stub_error = TRUE;
   11083 	  return FALSE;
   11084 	}
   11085 
   11086       if (htab->params->plt_stub_align != 0)
   11087 	{
   11088 	  unsigned pad = plt_stub_pad (htab, stub_entry, off);
   11089 
   11090 	  stub_entry->group->stub_sec->size += pad;
   11091 	  stub_entry->stub_offset = stub_entry->group->stub_sec->size;
   11092 	  loc += pad;
   11093 	}
   11094 
   11095       r = NULL;
   11096       if (info->emitrelocations)
   11097 	{
   11098 	  r = get_relocs (stub_entry->group->stub_sec,
   11099 			  ((PPC_HA (off) != 0)
   11100 			   + (htab->opd_abi
   11101 			      ? 2 + (htab->params->plt_static_chain
   11102 				     && PPC_HA (off + 16) == PPC_HA (off))
   11103 			      : 1)));
   11104 	  if (r == NULL)
   11105 	    return FALSE;
   11106 	  r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
   11107 	  if (bfd_big_endian (info->output_bfd))
   11108 	    r[0].r_offset += 2;
   11109 	  r[0].r_addend = dest;
   11110 	}
   11111       if (stub_entry->h != NULL
   11112 	  && (stub_entry->h == htab->tls_get_addr_fd
   11113 	      || stub_entry->h == htab->tls_get_addr)
   11114 	  && htab->params->tls_get_addr_opt)
   11115 	p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
   11116       else
   11117 	p = build_plt_stub (htab, stub_entry, loc, off, r);
   11118       size = p - loc;
   11119       break;
   11120 
   11121     case ppc_stub_save_res:
   11122       return TRUE;
   11123 
   11124     default:
   11125       BFD_FAIL ();
   11126       return FALSE;
   11127     }
   11128 
   11129   stub_entry->group->stub_sec->size += size;
   11130 
   11131   if (htab->params->emit_stub_syms)
   11132     {
   11133       struct elf_link_hash_entry *h;
   11134       size_t len1, len2;
   11135       char *name;
   11136       const char *const stub_str[] = { "long_branch",
   11137 				       "long_branch_r2off",
   11138 				       "plt_branch",
   11139 				       "plt_branch_r2off",
   11140 				       "plt_call",
   11141 				       "plt_call" };
   11142 
   11143       len1 = strlen (stub_str[stub_entry->stub_type - 1]);
   11144       len2 = strlen (stub_entry->root.string);
   11145       name = bfd_malloc (len1 + len2 + 2);
   11146       if (name == NULL)
   11147 	return FALSE;
   11148       memcpy (name, stub_entry->root.string, 9);
   11149       memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
   11150       memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
   11151       h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   11152       if (h == NULL)
   11153 	return FALSE;
   11154       if (h->root.type == bfd_link_hash_new)
   11155 	{
   11156 	  h->root.type = bfd_link_hash_defined;
   11157 	  h->root.u.def.section = stub_entry->group->stub_sec;
   11158 	  h->root.u.def.value = stub_entry->stub_offset;
   11159 	  h->ref_regular = 1;
   11160 	  h->def_regular = 1;
   11161 	  h->ref_regular_nonweak = 1;
   11162 	  h->forced_local = 1;
   11163 	  h->non_elf = 0;
   11164 	  h->root.linker_def = 1;
   11165 	}
   11166     }
   11167 
   11168   return TRUE;
   11169 }
   11170 
   11171 /* As above, but don't actually build the stub.  Just bump offset so
   11172    we know stub section sizes, and select plt_branch stubs where
   11173    long_branch stubs won't do.  */
   11174 
   11175 static bfd_boolean
   11176 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   11177 {
   11178   struct ppc_stub_hash_entry *stub_entry;
   11179   struct bfd_link_info *info;
   11180   struct ppc_link_hash_table *htab;
   11181   bfd_vma off;
   11182   int size;
   11183 
   11184   /* Massage our args to the form they really have.  */
   11185   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
   11186   info = in_arg;
   11187 
   11188   htab = ppc_hash_table (info);
   11189   if (htab == NULL)
   11190     return FALSE;
   11191 
   11192   if (stub_entry->h != NULL
   11193       && stub_entry->h->save_res
   11194       && stub_entry->h->elf.root.type == bfd_link_hash_defined
   11195       && stub_entry->h->elf.root.u.def.section == htab->sfpr)
   11196     {
   11197       /* Don't make stubs to out-of-line register save/restore
   11198 	 functions.  Instead, emit copies of the functions.  */
   11199       stub_entry->group->needs_save_res = 1;
   11200       stub_entry->stub_type = ppc_stub_save_res;
   11201       return TRUE;
   11202     }
   11203 
   11204   if (stub_entry->stub_type == ppc_stub_plt_call
   11205       || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   11206     {
   11207       asection *plt;
   11208       off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
   11209       if (off >= (bfd_vma) -2)
   11210 	abort ();
   11211       plt = htab->elf.splt;
   11212       if (!htab->elf.dynamic_sections_created
   11213 	  || stub_entry->h == NULL
   11214 	  || stub_entry->h->elf.dynindx == -1)
   11215 	plt = htab->elf.iplt;
   11216       off += (plt->output_offset
   11217 	      + plt->output_section->vma
   11218 	      - elf_gp (plt->output_section->owner)
   11219 	      - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   11220 
   11221       size = plt_stub_size (htab, stub_entry, off);
   11222       if (htab->params->plt_stub_align)
   11223 	size += plt_stub_pad (htab, stub_entry, off);
   11224       if (info->emitrelocations)
   11225 	{
   11226 	  stub_entry->group->stub_sec->reloc_count
   11227 	    += ((PPC_HA (off) != 0)
   11228 		+ (htab->opd_abi
   11229 		   ? 2 + (htab->params->plt_static_chain
   11230 			  && PPC_HA (off + 16) == PPC_HA (off))
   11231 		   : 1));
   11232 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   11233 	}
   11234     }
   11235   else
   11236     {
   11237       /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
   11238 	 variants.  */
   11239       bfd_vma r2off = 0;
   11240       bfd_vma local_off = 0;
   11241 
   11242       off = (stub_entry->target_value
   11243 	     + stub_entry->target_section->output_offset
   11244 	     + stub_entry->target_section->output_section->vma);
   11245       off -= (stub_entry->group->stub_sec->size
   11246 	      + stub_entry->group->stub_sec->output_offset
   11247 	      + stub_entry->group->stub_sec->output_section->vma);
   11248 
   11249       /* Reset the stub type from the plt variant in case we now
   11250 	 can reach with a shorter stub.  */
   11251       if (stub_entry->stub_type >= ppc_stub_plt_branch)
   11252 	stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
   11253 
   11254       size = 4;
   11255       if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
   11256 	{
   11257 	  r2off = get_r2off (info, stub_entry);
   11258 	  if (r2off == (bfd_vma) -1)
   11259 	    {
   11260 	      htab->stub_error = TRUE;
   11261 	      return FALSE;
   11262 	    }
   11263 	  size = 8;
   11264 	  if (PPC_HA (r2off) != 0)
   11265 	    size += 4;
   11266 	  if (PPC_LO (r2off) != 0)
   11267 	    size += 4;
   11268 	  off -= size - 4;
   11269 	}
   11270 
   11271       local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
   11272 
   11273       /* If the branch offset if too big, use a ppc_stub_plt_branch.
   11274 	 Do the same for -R objects without function descriptors.  */
   11275       if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
   11276 	  || (stub_entry->stub_type == ppc_stub_long_branch_r2off
   11277 	      && r2off == 0
   11278 	      && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
   11279 	{
   11280 	  struct ppc_branch_hash_entry *br_entry;
   11281 
   11282 	  br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
   11283 					     stub_entry->root.string + 9,
   11284 					     TRUE, FALSE);
   11285 	  if (br_entry == NULL)
   11286 	    {
   11287 	      info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
   11288 				      stub_entry->root.string);
   11289 	      htab->stub_error = TRUE;
   11290 	      return FALSE;
   11291 	    }
   11292 
   11293 	  if (br_entry->iter != htab->stub_iteration)
   11294 	    {
   11295 	      br_entry->iter = htab->stub_iteration;
   11296 	      br_entry->offset = htab->brlt->size;
   11297 	      htab->brlt->size += 8;
   11298 
   11299 	      if (htab->relbrlt != NULL)
   11300 		htab->relbrlt->size += sizeof (Elf64_External_Rela);
   11301 	      else if (info->emitrelocations)
   11302 		{
   11303 		  htab->brlt->reloc_count += 1;
   11304 		  htab->brlt->flags |= SEC_RELOC;
   11305 		}
   11306 	    }
   11307 
   11308 	  stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
   11309 	  off = (br_entry->offset
   11310 		 + htab->brlt->output_offset
   11311 		 + htab->brlt->output_section->vma
   11312 		 - elf_gp (htab->brlt->output_section->owner)
   11313 		 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
   11314 
   11315 	  if (info->emitrelocations)
   11316 	    {
   11317 	      stub_entry->group->stub_sec->reloc_count
   11318 		+= 1 + (PPC_HA (off) != 0);
   11319 	      stub_entry->group->stub_sec->flags |= SEC_RELOC;
   11320 	    }
   11321 
   11322 	  if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
   11323 	    {
   11324 	      size = 12;
   11325 	      if (PPC_HA (off) != 0)
   11326 		size = 16;
   11327 	    }
   11328 	  else
   11329 	    {
   11330 	      size = 16;
   11331 	      if (PPC_HA (off) != 0)
   11332 		size += 4;
   11333 
   11334 	      if (PPC_HA (r2off) != 0)
   11335 		size += 4;
   11336 	      if (PPC_LO (r2off) != 0)
   11337 		size += 4;
   11338 	    }
   11339 	}
   11340       else if (info->emitrelocations)
   11341 	{
   11342 	  stub_entry->group->stub_sec->reloc_count += 1;
   11343 	  stub_entry->group->stub_sec->flags |= SEC_RELOC;
   11344 	}
   11345     }
   11346 
   11347   stub_entry->group->stub_sec->size += size;
   11348   return TRUE;
   11349 }
   11350 
   11351 /* Set up various things so that we can make a list of input sections
   11352    for each output section included in the link.  Returns -1 on error,
   11353    0 when no stubs will be needed, and 1 on success.  */
   11354 
   11355 int
   11356 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
   11357 {
   11358   unsigned int id;
   11359   bfd_size_type amt;
   11360   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11361 
   11362   if (htab == NULL)
   11363     return -1;
   11364 
   11365   htab->sec_info_arr_size = bfd_get_next_section_id ();
   11366   amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
   11367   htab->sec_info = bfd_zmalloc (amt);
   11368   if (htab->sec_info == NULL)
   11369     return -1;
   11370 
   11371   /* Set toc_off for com, und, abs and ind sections.  */
   11372   for (id = 0; id < 3; id++)
   11373     htab->sec_info[id].toc_off = TOC_BASE_OFF;
   11374 
   11375   return 1;
   11376 }
   11377 
   11378 /* Set up for first pass at multitoc partitioning.  */
   11379 
   11380 void
   11381 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
   11382 {
   11383   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11384 
   11385   htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
   11386   htab->toc_bfd = NULL;
   11387   htab->toc_first_sec = NULL;
   11388 }
   11389 
   11390 /* The linker repeatedly calls this function for each TOC input section
   11391    and linker generated GOT section.  Group input bfds such that the toc
   11392    within a group is less than 64k in size.  */
   11393 
   11394 bfd_boolean
   11395 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
   11396 {
   11397   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11398   bfd_vma addr, off, limit;
   11399 
   11400   if (htab == NULL)
   11401     return FALSE;
   11402 
   11403   if (!htab->second_toc_pass)
   11404     {
   11405       /* Keep track of the first .toc or .got section for this input bfd.  */
   11406       bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
   11407 
   11408       if (new_bfd)
   11409 	{
   11410 	  htab->toc_bfd = isec->owner;
   11411 	  htab->toc_first_sec = isec;
   11412 	}
   11413 
   11414       addr = isec->output_offset + isec->output_section->vma;
   11415       off = addr - htab->toc_curr;
   11416       limit = 0x80008000;
   11417       if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
   11418 	limit = 0x10000;
   11419       if (off + isec->size > limit)
   11420 	{
   11421 	  addr = (htab->toc_first_sec->output_offset
   11422 		  + htab->toc_first_sec->output_section->vma);
   11423 	  htab->toc_curr = addr;
   11424 	  htab->toc_curr &= -TOC_BASE_ALIGN;
   11425 	}
   11426 
   11427       /* toc_curr is the base address of this toc group.  Set elf_gp
   11428 	 for the input section to be the offset relative to the
   11429 	 output toc base plus 0x8000.  Making the input elf_gp an
   11430 	 offset allows us to move the toc as a whole without
   11431 	 recalculating input elf_gp.  */
   11432       off = htab->toc_curr - elf_gp (isec->output_section->owner);
   11433       off += TOC_BASE_OFF;
   11434 
   11435       /* Die if someone uses a linker script that doesn't keep input
   11436 	 file .toc and .got together.  */
   11437       if (new_bfd
   11438 	  && elf_gp (isec->owner) != 0
   11439 	  && elf_gp (isec->owner) != off)
   11440 	return FALSE;
   11441 
   11442       elf_gp (isec->owner) = off;
   11443       return TRUE;
   11444     }
   11445 
   11446   /* During the second pass toc_first_sec points to the start of
   11447      a toc group, and toc_curr is used to track the old elf_gp.
   11448      We use toc_bfd to ensure we only look at each bfd once.  */
   11449   if (htab->toc_bfd == isec->owner)
   11450     return TRUE;
   11451   htab->toc_bfd = isec->owner;
   11452 
   11453   if (htab->toc_first_sec == NULL
   11454       || htab->toc_curr != elf_gp (isec->owner))
   11455     {
   11456       htab->toc_curr = elf_gp (isec->owner);
   11457       htab->toc_first_sec = isec;
   11458     }
   11459   addr = (htab->toc_first_sec->output_offset
   11460 	  + htab->toc_first_sec->output_section->vma);
   11461   off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
   11462   elf_gp (isec->owner) = off;
   11463 
   11464   return TRUE;
   11465 }
   11466 
   11467 /* Called via elf_link_hash_traverse to merge GOT entries for global
   11468    symbol H.  */
   11469 
   11470 static bfd_boolean
   11471 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   11472 {
   11473   if (h->root.type == bfd_link_hash_indirect)
   11474     return TRUE;
   11475 
   11476   merge_got_entries (&h->got.glist);
   11477 
   11478   return TRUE;
   11479 }
   11480 
   11481 /* Called via elf_link_hash_traverse to allocate GOT entries for global
   11482    symbol H.  */
   11483 
   11484 static bfd_boolean
   11485 reallocate_got (struct elf_link_hash_entry *h, void *inf)
   11486 {
   11487   struct got_entry *gent;
   11488 
   11489   if (h->root.type == bfd_link_hash_indirect)
   11490     return TRUE;
   11491 
   11492   for (gent = h->got.glist; gent != NULL; gent = gent->next)
   11493     if (!gent->is_indirect)
   11494       allocate_got (h, (struct bfd_link_info *) inf, gent);
   11495   return TRUE;
   11496 }
   11497 
   11498 /* Called on the first multitoc pass after the last call to
   11499    ppc64_elf_next_toc_section.  This function removes duplicate GOT
   11500    entries.  */
   11501 
   11502 bfd_boolean
   11503 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
   11504 {
   11505   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11506   struct bfd *ibfd, *ibfd2;
   11507   bfd_boolean done_something;
   11508 
   11509   htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
   11510 
   11511   if (!htab->do_multi_toc)
   11512     return FALSE;
   11513 
   11514   /* Merge global sym got entries within a toc group.  */
   11515   elf_link_hash_traverse (&htab->elf, merge_global_got, info);
   11516 
   11517   /* And tlsld_got.  */
   11518   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11519     {
   11520       struct got_entry *ent, *ent2;
   11521 
   11522       if (!is_ppc64_elf (ibfd))
   11523 	continue;
   11524 
   11525       ent = ppc64_tlsld_got (ibfd);
   11526       if (!ent->is_indirect
   11527 	  && ent->got.offset != (bfd_vma) -1)
   11528 	{
   11529 	  for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
   11530 	    {
   11531 	      if (!is_ppc64_elf (ibfd2))
   11532 		continue;
   11533 
   11534 	      ent2 = ppc64_tlsld_got (ibfd2);
   11535 	      if (!ent2->is_indirect
   11536 		  && ent2->got.offset != (bfd_vma) -1
   11537 		  && elf_gp (ibfd2) == elf_gp (ibfd))
   11538 		{
   11539 		  ent2->is_indirect = TRUE;
   11540 		  ent2->got.ent = ent;
   11541 		}
   11542 	    }
   11543 	}
   11544     }
   11545 
   11546   /* Zap sizes of got sections.  */
   11547   htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
   11548   htab->elf.irelplt->size -= htab->got_reli_size;
   11549   htab->got_reli_size = 0;
   11550 
   11551   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11552     {
   11553       asection *got, *relgot;
   11554 
   11555       if (!is_ppc64_elf (ibfd))
   11556 	continue;
   11557 
   11558       got = ppc64_elf_tdata (ibfd)->got;
   11559       if (got != NULL)
   11560 	{
   11561 	  got->rawsize = got->size;
   11562 	  got->size = 0;
   11563 	  relgot = ppc64_elf_tdata (ibfd)->relgot;
   11564 	  relgot->rawsize = relgot->size;
   11565 	  relgot->size = 0;
   11566 	}
   11567     }
   11568 
   11569   /* Now reallocate the got, local syms first.  We don't need to
   11570      allocate section contents again since we never increase size.  */
   11571   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11572     {
   11573       struct got_entry **lgot_ents;
   11574       struct got_entry **end_lgot_ents;
   11575       struct plt_entry **local_plt;
   11576       struct plt_entry **end_local_plt;
   11577       unsigned char *lgot_masks;
   11578       bfd_size_type locsymcount;
   11579       Elf_Internal_Shdr *symtab_hdr;
   11580       asection *s;
   11581 
   11582       if (!is_ppc64_elf (ibfd))
   11583 	continue;
   11584 
   11585       lgot_ents = elf_local_got_ents (ibfd);
   11586       if (!lgot_ents)
   11587 	continue;
   11588 
   11589       symtab_hdr = &elf_symtab_hdr (ibfd);
   11590       locsymcount = symtab_hdr->sh_info;
   11591       end_lgot_ents = lgot_ents + locsymcount;
   11592       local_plt = (struct plt_entry **) end_lgot_ents;
   11593       end_local_plt = local_plt + locsymcount;
   11594       lgot_masks = (unsigned char *) end_local_plt;
   11595       s = ppc64_elf_tdata (ibfd)->got;
   11596       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
   11597 	{
   11598 	  struct got_entry *ent;
   11599 
   11600 	  for (ent = *lgot_ents; ent != NULL; ent = ent->next)
   11601 	    {
   11602 	      unsigned int ent_size = 8;
   11603 	      unsigned int rel_size = sizeof (Elf64_External_Rela);
   11604 
   11605 	      ent->got.offset = s->size;
   11606 	      if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
   11607 		{
   11608 		  ent_size *= 2;
   11609 		  rel_size *= 2;
   11610 		}
   11611 	      s->size += ent_size;
   11612 	      if ((*lgot_masks & PLT_IFUNC) != 0)
   11613 		{
   11614 		  htab->elf.irelplt->size += rel_size;
   11615 		  htab->got_reli_size += rel_size;
   11616 		}
   11617 	      else if (bfd_link_pic (info))
   11618 		{
   11619 		  asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   11620 		  srel->size += rel_size;
   11621 		}
   11622 	    }
   11623 	}
   11624     }
   11625 
   11626   elf_link_hash_traverse (&htab->elf, reallocate_got, info);
   11627 
   11628   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11629     {
   11630       struct got_entry *ent;
   11631 
   11632       if (!is_ppc64_elf (ibfd))
   11633 	continue;
   11634 
   11635       ent = ppc64_tlsld_got (ibfd);
   11636       if (!ent->is_indirect
   11637 	  && ent->got.offset != (bfd_vma) -1)
   11638 	{
   11639 	  asection *s = ppc64_elf_tdata (ibfd)->got;
   11640 	  ent->got.offset = s->size;
   11641 	  s->size += 16;
   11642 	  if (bfd_link_pic (info))
   11643 	    {
   11644 	      asection *srel = ppc64_elf_tdata (ibfd)->relgot;
   11645 	      srel->size += sizeof (Elf64_External_Rela);
   11646 	    }
   11647 	}
   11648     }
   11649 
   11650   done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
   11651   if (!done_something)
   11652     for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   11653       {
   11654 	asection *got;
   11655 
   11656 	if (!is_ppc64_elf (ibfd))
   11657 	  continue;
   11658 
   11659 	got = ppc64_elf_tdata (ibfd)->got;
   11660 	if (got != NULL)
   11661 	  {
   11662 	    done_something = got->rawsize != got->size;
   11663 	    if (done_something)
   11664 	      break;
   11665 	  }
   11666       }
   11667 
   11668   if (done_something)
   11669     (*htab->params->layout_sections_again) ();
   11670 
   11671   /* Set up for second pass over toc sections to recalculate elf_gp
   11672      on input sections.  */
   11673   htab->toc_bfd = NULL;
   11674   htab->toc_first_sec = NULL;
   11675   htab->second_toc_pass = TRUE;
   11676   return done_something;
   11677 }
   11678 
   11679 /* Called after second pass of multitoc partitioning.  */
   11680 
   11681 void
   11682 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
   11683 {
   11684   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11685 
   11686   /* After the second pass, toc_curr tracks the TOC offset used
   11687      for code sections below in ppc64_elf_next_input_section.  */
   11688   htab->toc_curr = TOC_BASE_OFF;
   11689 }
   11690 
   11691 /* No toc references were found in ISEC.  If the code in ISEC makes no
   11692    calls, then there's no need to use toc adjusting stubs when branching
   11693    into ISEC.  Actually, indirect calls from ISEC are OK as they will
   11694    load r2.  Returns -1 on error, 0 for no stub needed, 1 for stub
   11695    needed, and 2 if a cyclical call-graph was found but no other reason
   11696    for a stub was detected.  If called from the top level, a return of
   11697    2 means the same as a return of 0.  */
   11698 
   11699 static int
   11700 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   11701 {
   11702   int ret;
   11703 
   11704   /* Mark this section as checked.  */
   11705   isec->call_check_done = 1;
   11706 
   11707   /* We know none of our code bearing sections will need toc stubs.  */
   11708   if ((isec->flags & SEC_LINKER_CREATED) != 0)
   11709     return 0;
   11710 
   11711   if (isec->size == 0)
   11712     return 0;
   11713 
   11714   if (isec->output_section == NULL)
   11715     return 0;
   11716 
   11717   ret = 0;
   11718   if (isec->reloc_count != 0)
   11719     {
   11720       Elf_Internal_Rela *relstart, *rel;
   11721       Elf_Internal_Sym *local_syms;
   11722       struct ppc_link_hash_table *htab;
   11723 
   11724       relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
   11725 					    info->keep_memory);
   11726       if (relstart == NULL)
   11727 	return -1;
   11728 
   11729       /* Look for branches to outside of this section.  */
   11730       local_syms = NULL;
   11731       htab = ppc_hash_table (info);
   11732       if (htab == NULL)
   11733 	return -1;
   11734 
   11735       for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
   11736 	{
   11737 	  enum elf_ppc64_reloc_type r_type;
   11738 	  unsigned long r_symndx;
   11739 	  struct elf_link_hash_entry *h;
   11740 	  struct ppc_link_hash_entry *eh;
   11741 	  Elf_Internal_Sym *sym;
   11742 	  asection *sym_sec;
   11743 	  struct _opd_sec_data *opd;
   11744 	  bfd_vma sym_value;
   11745 	  bfd_vma dest;
   11746 
   11747 	  r_type = ELF64_R_TYPE (rel->r_info);
   11748 	  if (r_type != R_PPC64_REL24
   11749 	      && r_type != R_PPC64_REL14
   11750 	      && r_type != R_PPC64_REL14_BRTAKEN
   11751 	      && r_type != R_PPC64_REL14_BRNTAKEN)
   11752 	    continue;
   11753 
   11754 	  r_symndx = ELF64_R_SYM (rel->r_info);
   11755 	  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
   11756 			  isec->owner))
   11757 	    {
   11758 	      ret = -1;
   11759 	      break;
   11760 	    }
   11761 
   11762 	  /* Calls to dynamic lib functions go through a plt call stub
   11763 	     that uses r2.  */
   11764 	  eh = (struct ppc_link_hash_entry *) h;
   11765 	  if (eh != NULL
   11766 	      && (eh->elf.plt.plist != NULL
   11767 		  || (eh->oh != NULL
   11768 		      && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
   11769 	    {
   11770 	      ret = 1;
   11771 	      break;
   11772 	    }
   11773 
   11774 	  if (sym_sec == NULL)
   11775 	    /* Ignore other undefined symbols.  */
   11776 	    continue;
   11777 
   11778 	  /* Assume branches to other sections not included in the
   11779 	     link need stubs too, to cover -R and absolute syms.  */
   11780 	  if (sym_sec->output_section == NULL)
   11781 	    {
   11782 	      ret = 1;
   11783 	      break;
   11784 	    }
   11785 
   11786 	  if (h == NULL)
   11787 	    sym_value = sym->st_value;
   11788 	  else
   11789 	    {
   11790 	      if (h->root.type != bfd_link_hash_defined
   11791 		  && h->root.type != bfd_link_hash_defweak)
   11792 		abort ();
   11793 	      sym_value = h->root.u.def.value;
   11794 	    }
   11795 	  sym_value += rel->r_addend;
   11796 
   11797 	  /* If this branch reloc uses an opd sym, find the code section.  */
   11798 	  opd = get_opd_info (sym_sec);
   11799 	  if (opd != NULL)
   11800 	    {
   11801 	      if (h == NULL && opd->adjust != NULL)
   11802 		{
   11803 		  long adjust;
   11804 
   11805 		  adjust = opd->adjust[OPD_NDX (sym_value)];
   11806 		  if (adjust == -1)
   11807 		    /* Assume deleted functions won't ever be called.  */
   11808 		    continue;
   11809 		  sym_value += adjust;
   11810 		}
   11811 
   11812 	      dest = opd_entry_value (sym_sec, sym_value,
   11813 				      &sym_sec, NULL, FALSE);
   11814 	      if (dest == (bfd_vma) -1)
   11815 		continue;
   11816 	    }
   11817 	  else
   11818 	    dest = (sym_value
   11819 		    + sym_sec->output_offset
   11820 		    + sym_sec->output_section->vma);
   11821 
   11822 	  /* Ignore branch to self.  */
   11823 	  if (sym_sec == isec)
   11824 	    continue;
   11825 
   11826 	  /* If the called function uses the toc, we need a stub.  */
   11827 	  if (sym_sec->has_toc_reloc
   11828 	      || sym_sec->makes_toc_func_call)
   11829 	    {
   11830 	      ret = 1;
   11831 	      break;
   11832 	    }
   11833 
   11834 	  /* Assume any branch that needs a long branch stub might in fact
   11835 	     need a plt_branch stub.  A plt_branch stub uses r2.  */
   11836 	  else if (dest - (isec->output_offset
   11837 			   + isec->output_section->vma
   11838 			   + rel->r_offset) + (1 << 25)
   11839 		   >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
   11840 							     ? h->other
   11841 							     : sym->st_other))
   11842 	    {
   11843 	      ret = 1;
   11844 	      break;
   11845 	    }
   11846 
   11847 	  /* If calling back to a section in the process of being
   11848 	     tested, we can't say for sure that no toc adjusting stubs
   11849 	     are needed, so don't return zero.  */
   11850 	  else if (sym_sec->call_check_in_progress)
   11851 	    ret = 2;
   11852 
   11853 	  /* Branches to another section that itself doesn't have any TOC
   11854 	     references are OK.  Recursively call ourselves to check.  */
   11855 	  else if (!sym_sec->call_check_done)
   11856 	    {
   11857 	      int recur;
   11858 
   11859 	      /* Mark current section as indeterminate, so that other
   11860 		 sections that call back to current won't be marked as
   11861 		 known.  */
   11862 	      isec->call_check_in_progress = 1;
   11863 	      recur = toc_adjusting_stub_needed (info, sym_sec);
   11864 	      isec->call_check_in_progress = 0;
   11865 
   11866 	      if (recur != 0)
   11867 		{
   11868 		  ret = recur;
   11869 		  if (recur != 2)
   11870 		    break;
   11871 		}
   11872 	    }
   11873 	}
   11874 
   11875       if (local_syms != NULL
   11876 	  && (elf_symtab_hdr (isec->owner).contents
   11877 	      != (unsigned char *) local_syms))
   11878 	free (local_syms);
   11879       if (elf_section_data (isec)->relocs != relstart)
   11880 	free (relstart);
   11881     }
   11882 
   11883   if ((ret & 1) == 0
   11884       && isec->map_head.s != NULL
   11885       && (strcmp (isec->output_section->name, ".init") == 0
   11886 	  || strcmp (isec->output_section->name, ".fini") == 0))
   11887     {
   11888       if (isec->map_head.s->has_toc_reloc
   11889 	  || isec->map_head.s->makes_toc_func_call)
   11890 	ret = 1;
   11891       else if (!isec->map_head.s->call_check_done)
   11892 	{
   11893 	  int recur;
   11894 	  isec->call_check_in_progress = 1;
   11895 	  recur = toc_adjusting_stub_needed (info, isec->map_head.s);
   11896 	  isec->call_check_in_progress = 0;
   11897 	  if (recur != 0)
   11898 	    ret = recur;
   11899 	}
   11900     }
   11901 
   11902   if (ret == 1)
   11903     isec->makes_toc_func_call = 1;
   11904 
   11905   return ret;
   11906 }
   11907 
   11908 /* The linker repeatedly calls this function for each input section,
   11909    in the order that input sections are linked into output sections.
   11910    Build lists of input sections to determine groupings between which
   11911    we may insert linker stubs.  */
   11912 
   11913 bfd_boolean
   11914 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
   11915 {
   11916   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11917 
   11918   if (htab == NULL)
   11919     return FALSE;
   11920 
   11921   if ((isec->output_section->flags & SEC_CODE) != 0
   11922       && isec->output_section->id < htab->sec_info_arr_size)
   11923     {
   11924       /* This happens to make the list in reverse order,
   11925 	 which is what we want.  */
   11926       htab->sec_info[isec->id].u.list
   11927 	= htab->sec_info[isec->output_section->id].u.list;
   11928       htab->sec_info[isec->output_section->id].u.list = isec;
   11929     }
   11930 
   11931   if (htab->multi_toc_needed)
   11932     {
   11933       /* Analyse sections that aren't already flagged as needing a
   11934 	 valid toc pointer.  Exclude .fixup for the linux kernel.
   11935 	 .fixup contains branches, but only back to the function that
   11936 	 hit an exception.  */
   11937       if (!(isec->has_toc_reloc
   11938 	    || (isec->flags & SEC_CODE) == 0
   11939 	    || strcmp (isec->name, ".fixup") == 0
   11940 	    || isec->call_check_done))
   11941 	{
   11942 	  if (toc_adjusting_stub_needed (info, isec) < 0)
   11943 	    return FALSE;
   11944 	}
   11945       /* Make all sections use the TOC assigned for this object file.
   11946 	 This will be wrong for pasted sections;  We fix that in
   11947 	 check_pasted_section().  */
   11948       if (elf_gp (isec->owner) != 0)
   11949 	htab->toc_curr = elf_gp (isec->owner);
   11950     }
   11951 
   11952   htab->sec_info[isec->id].toc_off = htab->toc_curr;
   11953   return TRUE;
   11954 }
   11955 
   11956 /* Check that all .init and .fini sections use the same toc, if they
   11957    have toc relocs.  */
   11958 
   11959 static bfd_boolean
   11960 check_pasted_section (struct bfd_link_info *info, const char *name)
   11961 {
   11962   asection *o = bfd_get_section_by_name (info->output_bfd, name);
   11963 
   11964   if (o != NULL)
   11965     {
   11966       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   11967       bfd_vma toc_off = 0;
   11968       asection *i;
   11969 
   11970       for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11971 	if (i->has_toc_reloc)
   11972 	  {
   11973 	    if (toc_off == 0)
   11974 	      toc_off = htab->sec_info[i->id].toc_off;
   11975 	    else if (toc_off != htab->sec_info[i->id].toc_off)
   11976 	      return FALSE;
   11977 	  }
   11978 
   11979       if (toc_off == 0)
   11980 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11981 	  if (i->makes_toc_func_call)
   11982 	    {
   11983 	      toc_off = htab->sec_info[i->id].toc_off;
   11984 	      break;
   11985 	    }
   11986 
   11987       /* Make sure the whole pasted function uses the same toc offset.  */
   11988       if (toc_off != 0)
   11989 	for (i = o->map_head.s; i != NULL; i = i->map_head.s)
   11990 	  htab->sec_info[i->id].toc_off = toc_off;
   11991     }
   11992   return TRUE;
   11993 }
   11994 
   11995 bfd_boolean
   11996 ppc64_elf_check_init_fini (struct bfd_link_info *info)
   11997 {
   11998   return (check_pasted_section (info, ".init")
   11999 	  & check_pasted_section (info, ".fini"));
   12000 }
   12001 
   12002 /* See whether we can group stub sections together.  Grouping stub
   12003    sections may result in fewer stubs.  More importantly, we need to
   12004    put all .init* and .fini* stubs at the beginning of the .init or
   12005    .fini output sections respectively, because glibc splits the
   12006    _init and _fini functions into multiple parts.  Putting a stub in
   12007    the middle of a function is not a good idea.  */
   12008 
   12009 static bfd_boolean
   12010 group_sections (struct bfd_link_info *info,
   12011 		bfd_size_type stub_group_size,
   12012 		bfd_boolean stubs_always_before_branch)
   12013 {
   12014   struct ppc_link_hash_table *htab;
   12015   asection *osec;
   12016   bfd_size_type stub14_group_size;
   12017   bfd_boolean suppress_size_errors;
   12018 
   12019   htab = ppc_hash_table (info);
   12020   if (htab == NULL)
   12021     return FALSE;
   12022 
   12023   suppress_size_errors = FALSE;
   12024   stub14_group_size = stub_group_size >> 10;
   12025   if (stub_group_size == 1)
   12026     {
   12027       /* Default values.  */
   12028       if (stubs_always_before_branch)
   12029 	{
   12030 	  stub_group_size = 0x1e00000;
   12031 	  stub14_group_size = 0x7800;
   12032 	}
   12033       else
   12034 	{
   12035 	  stub_group_size = 0x1c00000;
   12036 	  stub14_group_size = 0x7000;
   12037 	}
   12038       suppress_size_errors = TRUE;
   12039     }
   12040 
   12041   for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
   12042     {
   12043       asection *tail;
   12044 
   12045       if (osec->id >= htab->sec_info_arr_size)
   12046 	continue;
   12047 
   12048       tail = htab->sec_info[osec->id].u.list;
   12049       while (tail != NULL)
   12050 	{
   12051 	  asection *curr;
   12052 	  asection *prev;
   12053 	  bfd_size_type total;
   12054 	  bfd_boolean big_sec;
   12055 	  bfd_vma curr_toc;
   12056 	  struct map_stub *group;
   12057 
   12058 	  curr = tail;
   12059 	  total = tail->size;
   12060 	  big_sec = total > (ppc64_elf_section_data (tail) != NULL
   12061 			     && ppc64_elf_section_data (tail)->has_14bit_branch
   12062 			     ? stub14_group_size : stub_group_size);
   12063 	  if (big_sec && !suppress_size_errors)
   12064 	    (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
   12065 				     tail->owner, tail);
   12066 	  curr_toc = htab->sec_info[tail->id].toc_off;
   12067 
   12068 	  while ((prev = htab->sec_info[curr->id].u.list) != NULL
   12069 		 && ((total += curr->output_offset - prev->output_offset)
   12070 		     < (ppc64_elf_section_data (prev) != NULL
   12071 			&& ppc64_elf_section_data (prev)->has_14bit_branch
   12072 			? stub14_group_size : stub_group_size))
   12073 		 && htab->sec_info[prev->id].toc_off == curr_toc)
   12074 	    curr = prev;
   12075 
   12076 	  /* OK, the size from the start of CURR to the end is less
   12077 	     than stub_group_size and thus can be handled by one stub
   12078 	     section.  (or the tail section is itself larger than
   12079 	     stub_group_size, in which case we may be toast.)  We
   12080 	     should really be keeping track of the total size of stubs
   12081 	     added here, as stubs contribute to the final output
   12082 	     section size.  That's a little tricky, and this way will
   12083 	     only break if stubs added make the total size more than
   12084 	     2^25, ie. for the default stub_group_size, if stubs total
   12085 	     more than 2097152 bytes, or nearly 75000 plt call stubs.  */
   12086 	  group = bfd_alloc (curr->owner, sizeof (*group));
   12087 	  if (group == NULL)
   12088 	    return FALSE;
   12089 	  group->link_sec = curr;
   12090 	  group->stub_sec = NULL;
   12091 	  group->needs_save_res = 0;
   12092 	  group->next = htab->group;
   12093 	  htab->group = group;
   12094 	  do
   12095 	    {
   12096 	      prev = htab->sec_info[tail->id].u.list;
   12097 	      /* Set up this stub group.  */
   12098 	      htab->sec_info[tail->id].u.group = group;
   12099 	    }
   12100 	  while (tail != curr && (tail = prev) != NULL);
   12101 
   12102 	  /* But wait, there's more!  Input sections up to stub_group_size
   12103 	     bytes before the stub section can be handled by it too.
   12104 	     Don't do this if we have a really large section after the
   12105 	     stubs, as adding more stubs increases the chance that
   12106 	     branches may not reach into the stub section.  */
   12107 	  if (!stubs_always_before_branch && !big_sec)
   12108 	    {
   12109 	      total = 0;
   12110 	      while (prev != NULL
   12111 		     && ((total += tail->output_offset - prev->output_offset)
   12112 			 < (ppc64_elf_section_data (prev) != NULL
   12113 			    && ppc64_elf_section_data (prev)->has_14bit_branch
   12114 			    ? stub14_group_size : stub_group_size))
   12115 		     && htab->sec_info[prev->id].toc_off == curr_toc)
   12116 		{
   12117 		  tail = prev;
   12118 		  prev = htab->sec_info[tail->id].u.list;
   12119 		  htab->sec_info[tail->id].u.group = group;
   12120 		}
   12121 	    }
   12122 	  tail = prev;
   12123 	}
   12124     }
   12125   return TRUE;
   12126 }
   12127 
   12128 static const unsigned char glink_eh_frame_cie[] =
   12129 {
   12130   0, 0, 0, 16,				/* length.  */
   12131   0, 0, 0, 0,				/* id.  */
   12132   1,					/* CIE version.  */
   12133   'z', 'R', 0,				/* Augmentation string.  */
   12134   4,					/* Code alignment.  */
   12135   0x78,					/* Data alignment.  */
   12136   65,					/* RA reg.  */
   12137   1,					/* Augmentation size.  */
   12138   DW_EH_PE_pcrel | DW_EH_PE_sdata4,	/* FDE encoding.  */
   12139   DW_CFA_def_cfa, 1, 0,			/* def_cfa: r1 offset 0.  */
   12140   0, 0, 0, 0
   12141 };
   12142 
   12143 /* Stripping output sections is normally done before dynamic section
   12144    symbols have been allocated.  This function is called later, and
   12145    handles cases like htab->brlt which is mapped to its own output
   12146    section.  */
   12147 
   12148 static void
   12149 maybe_strip_output (struct bfd_link_info *info, asection *isec)
   12150 {
   12151   if (isec->size == 0
   12152       && isec->output_section->size == 0
   12153       && !(isec->output_section->flags & SEC_KEEP)
   12154       && !bfd_section_removed_from_list (info->output_bfd,
   12155 					 isec->output_section)
   12156       && elf_section_data (isec->output_section)->dynindx == 0)
   12157     {
   12158       isec->output_section->flags |= SEC_EXCLUDE;
   12159       bfd_section_list_remove (info->output_bfd, isec->output_section);
   12160       info->output_bfd->section_count--;
   12161     }
   12162 }
   12163 
   12164 /* Determine and set the size of the stub section for a final link.
   12165 
   12166    The basic idea here is to examine all the relocations looking for
   12167    PC-relative calls to a target that is unreachable with a "bl"
   12168    instruction.  */
   12169 
   12170 bfd_boolean
   12171 ppc64_elf_size_stubs (struct bfd_link_info *info)
   12172 {
   12173   bfd_size_type stub_group_size;
   12174   bfd_boolean stubs_always_before_branch;
   12175   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12176 
   12177   if (htab == NULL)
   12178     return FALSE;
   12179 
   12180   if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
   12181     htab->params->plt_thread_safe = 1;
   12182   if (!htab->opd_abi)
   12183     htab->params->plt_thread_safe = 0;
   12184   else if (htab->params->plt_thread_safe == -1)
   12185     {
   12186       static const char *const thread_starter[] =
   12187 	{
   12188 	  "pthread_create",
   12189 	  /* libstdc++ */
   12190 	  "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
   12191 	  /* librt */
   12192 	  "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
   12193 	  "mq_notify", "create_timer",
   12194 	  /* libanl */
   12195 	  "getaddrinfo_a",
   12196 	  /* libgomp */
   12197 	  "GOMP_parallel",
   12198 	  "GOMP_parallel_start",
   12199 	  "GOMP_parallel_loop_static",
   12200 	  "GOMP_parallel_loop_static_start",
   12201 	  "GOMP_parallel_loop_dynamic",
   12202 	  "GOMP_parallel_loop_dynamic_start",
   12203 	  "GOMP_parallel_loop_guided",
   12204 	  "GOMP_parallel_loop_guided_start",
   12205 	  "GOMP_parallel_loop_runtime",
   12206 	  "GOMP_parallel_loop_runtime_start",
   12207 	  "GOMP_parallel_sections",
   12208 	  "GOMP_parallel_sections_start",
   12209 	  /* libgo */
   12210 	  "__go_go",
   12211 	};
   12212       unsigned i;
   12213 
   12214       for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
   12215 	{
   12216 	  struct elf_link_hash_entry *h;
   12217 	  h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
   12218 				    FALSE, FALSE, TRUE);
   12219 	  htab->params->plt_thread_safe = h != NULL && h->ref_regular;
   12220 	  if (htab->params->plt_thread_safe)
   12221 	    break;
   12222 	}
   12223     }
   12224   stubs_always_before_branch = htab->params->group_size < 0;
   12225   if (htab->params->group_size < 0)
   12226     stub_group_size = -htab->params->group_size;
   12227   else
   12228     stub_group_size = htab->params->group_size;
   12229 
   12230   if (!group_sections (info, stub_group_size, stubs_always_before_branch))
   12231     return FALSE;
   12232 
   12233 #define STUB_SHRINK_ITER 20
   12234   /* Loop until no stubs added.  After iteration 20 of this loop we may
   12235      exit on a stub section shrinking.  This is to break out of a
   12236      pathological case where adding stubs on one iteration decreases
   12237      section gaps (perhaps due to alignment), which then requires
   12238      fewer or smaller stubs on the next iteration.  */
   12239 
   12240   while (1)
   12241     {
   12242       bfd *input_bfd;
   12243       unsigned int bfd_indx;
   12244       struct map_stub *group;
   12245       asection *stub_sec;
   12246 
   12247       htab->stub_iteration += 1;
   12248 
   12249       for (input_bfd = info->input_bfds, bfd_indx = 0;
   12250 	   input_bfd != NULL;
   12251 	   input_bfd = input_bfd->link.next, bfd_indx++)
   12252 	{
   12253 	  Elf_Internal_Shdr *symtab_hdr;
   12254 	  asection *section;
   12255 	  Elf_Internal_Sym *local_syms = NULL;
   12256 
   12257 	  if (!is_ppc64_elf (input_bfd))
   12258 	    continue;
   12259 
   12260 	  /* We'll need the symbol table in a second.  */
   12261 	  symtab_hdr = &elf_symtab_hdr (input_bfd);
   12262 	  if (symtab_hdr->sh_info == 0)
   12263 	    continue;
   12264 
   12265 	  /* Walk over each section attached to the input bfd.  */
   12266 	  for (section = input_bfd->sections;
   12267 	       section != NULL;
   12268 	       section = section->next)
   12269 	    {
   12270 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
   12271 
   12272 	      /* If there aren't any relocs, then there's nothing more
   12273 		 to do.  */
   12274 	      if ((section->flags & SEC_RELOC) == 0
   12275 		  || (section->flags & SEC_ALLOC) == 0
   12276 		  || (section->flags & SEC_LOAD) == 0
   12277 		  || (section->flags & SEC_CODE) == 0
   12278 		  || section->reloc_count == 0)
   12279 		continue;
   12280 
   12281 	      /* If this section is a link-once section that will be
   12282 		 discarded, then don't create any stubs.  */
   12283 	      if (section->output_section == NULL
   12284 		  || section->output_section->owner != info->output_bfd)
   12285 		continue;
   12286 
   12287 	      /* Get the relocs.  */
   12288 	      internal_relocs
   12289 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
   12290 					     info->keep_memory);
   12291 	      if (internal_relocs == NULL)
   12292 		goto error_ret_free_local;
   12293 
   12294 	      /* Now examine each relocation.  */
   12295 	      irela = internal_relocs;
   12296 	      irelaend = irela + section->reloc_count;
   12297 	      for (; irela < irelaend; irela++)
   12298 		{
   12299 		  enum elf_ppc64_reloc_type r_type;
   12300 		  unsigned int r_indx;
   12301 		  enum ppc_stub_type stub_type;
   12302 		  struct ppc_stub_hash_entry *stub_entry;
   12303 		  asection *sym_sec, *code_sec;
   12304 		  bfd_vma sym_value, code_value;
   12305 		  bfd_vma destination;
   12306 		  unsigned long local_off;
   12307 		  bfd_boolean ok_dest;
   12308 		  struct ppc_link_hash_entry *hash;
   12309 		  struct ppc_link_hash_entry *fdh;
   12310 		  struct elf_link_hash_entry *h;
   12311 		  Elf_Internal_Sym *sym;
   12312 		  char *stub_name;
   12313 		  const asection *id_sec;
   12314 		  struct _opd_sec_data *opd;
   12315 		  struct plt_entry *plt_ent;
   12316 
   12317 		  r_type = ELF64_R_TYPE (irela->r_info);
   12318 		  r_indx = ELF64_R_SYM (irela->r_info);
   12319 
   12320 		  if (r_type >= R_PPC64_max)
   12321 		    {
   12322 		      bfd_set_error (bfd_error_bad_value);
   12323 		      goto error_ret_free_internal;
   12324 		    }
   12325 
   12326 		  /* Only look for stubs on branch instructions.  */
   12327 		  if (r_type != R_PPC64_REL24
   12328 		      && r_type != R_PPC64_REL14
   12329 		      && r_type != R_PPC64_REL14_BRTAKEN
   12330 		      && r_type != R_PPC64_REL14_BRNTAKEN)
   12331 		    continue;
   12332 
   12333 		  /* Now determine the call target, its name, value,
   12334 		     section.  */
   12335 		  if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
   12336 				  r_indx, input_bfd))
   12337 		    goto error_ret_free_internal;
   12338 		  hash = (struct ppc_link_hash_entry *) h;
   12339 
   12340 		  ok_dest = FALSE;
   12341 		  fdh = NULL;
   12342 		  sym_value = 0;
   12343 		  if (hash == NULL)
   12344 		    {
   12345 		      sym_value = sym->st_value;
   12346 		      if (sym_sec != NULL
   12347 			  && sym_sec->output_section != NULL)
   12348 			ok_dest = TRUE;
   12349 		    }
   12350 		  else if (hash->elf.root.type == bfd_link_hash_defined
   12351 			   || hash->elf.root.type == bfd_link_hash_defweak)
   12352 		    {
   12353 		      sym_value = hash->elf.root.u.def.value;
   12354 		      if (sym_sec->output_section != NULL)
   12355 			ok_dest = TRUE;
   12356 		    }
   12357 		  else if (hash->elf.root.type == bfd_link_hash_undefweak
   12358 			   || hash->elf.root.type == bfd_link_hash_undefined)
   12359 		    {
   12360 		      /* Recognise an old ABI func code entry sym, and
   12361 			 use the func descriptor sym instead if it is
   12362 			 defined.  */
   12363 		      if (hash->elf.root.root.string[0] == '.'
   12364 			  && (fdh = lookup_fdh (hash, htab)) != NULL)
   12365 			{
   12366 			  if (fdh->elf.root.type == bfd_link_hash_defined
   12367 			      || fdh->elf.root.type == bfd_link_hash_defweak)
   12368 			    {
   12369 			      sym_sec = fdh->elf.root.u.def.section;
   12370 			      sym_value = fdh->elf.root.u.def.value;
   12371 			      if (sym_sec->output_section != NULL)
   12372 				ok_dest = TRUE;
   12373 			    }
   12374 			  else
   12375 			    fdh = NULL;
   12376 			}
   12377 		    }
   12378 		  else
   12379 		    {
   12380 		      bfd_set_error (bfd_error_bad_value);
   12381 		      goto error_ret_free_internal;
   12382 		    }
   12383 
   12384 		  destination = 0;
   12385 		  local_off = 0;
   12386 		  if (ok_dest)
   12387 		    {
   12388 		      sym_value += irela->r_addend;
   12389 		      destination = (sym_value
   12390 				     + sym_sec->output_offset
   12391 				     + sym_sec->output_section->vma);
   12392 		      local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
   12393 							    ? hash->elf.other
   12394 							    : sym->st_other);
   12395 		    }
   12396 
   12397 		  code_sec = sym_sec;
   12398 		  code_value = sym_value;
   12399 		  opd = get_opd_info (sym_sec);
   12400 		  if (opd != NULL)
   12401 		    {
   12402 		      bfd_vma dest;
   12403 
   12404 		      if (hash == NULL && opd->adjust != NULL)
   12405 			{
   12406 			  long adjust = opd->adjust[OPD_NDX (sym_value)];
   12407 			  if (adjust == -1)
   12408 			    continue;
   12409 			  code_value += adjust;
   12410 			  sym_value += adjust;
   12411 			}
   12412 		      dest = opd_entry_value (sym_sec, sym_value,
   12413 					      &code_sec, &code_value, FALSE);
   12414 		      if (dest != (bfd_vma) -1)
   12415 			{
   12416 			  destination = dest;
   12417 			  if (fdh != NULL)
   12418 			    {
   12419 			      /* Fixup old ABI sym to point at code
   12420 				 entry.  */
   12421 			      hash->elf.root.type = bfd_link_hash_defweak;
   12422 			      hash->elf.root.u.def.section = code_sec;
   12423 			      hash->elf.root.u.def.value = code_value;
   12424 			    }
   12425 			}
   12426 		    }
   12427 
   12428 		  /* Determine what (if any) linker stub is needed.  */
   12429 		  plt_ent = NULL;
   12430 		  stub_type = ppc_type_of_stub (section, irela, &hash,
   12431 						&plt_ent, destination,
   12432 						local_off);
   12433 
   12434 		  if (stub_type != ppc_stub_plt_call)
   12435 		    {
   12436 		      /* Check whether we need a TOC adjusting stub.
   12437 			 Since the linker pastes together pieces from
   12438 			 different object files when creating the
   12439 			 _init and _fini functions, it may be that a
   12440 			 call to what looks like a local sym is in
   12441 			 fact a call needing a TOC adjustment.  */
   12442 		      if (code_sec != NULL
   12443 			  && code_sec->output_section != NULL
   12444 			  && (htab->sec_info[code_sec->id].toc_off
   12445 			      != htab->sec_info[section->id].toc_off)
   12446 			  && (code_sec->has_toc_reloc
   12447 			      || code_sec->makes_toc_func_call))
   12448 			stub_type = ppc_stub_long_branch_r2off;
   12449 		    }
   12450 
   12451 		  if (stub_type == ppc_stub_none)
   12452 		    continue;
   12453 
   12454 		  /* __tls_get_addr calls might be eliminated.  */
   12455 		  if (stub_type != ppc_stub_plt_call
   12456 		      && hash != NULL
   12457 		      && (hash == htab->tls_get_addr
   12458 			  || hash == htab->tls_get_addr_fd)
   12459 		      && section->has_tls_reloc
   12460 		      && irela != internal_relocs)
   12461 		    {
   12462 		      /* Get tls info.  */
   12463 		      unsigned char *tls_mask;
   12464 
   12465 		      if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
   12466 					 irela - 1, input_bfd))
   12467 			goto error_ret_free_internal;
   12468 		      if (*tls_mask != 0)
   12469 			continue;
   12470 		    }
   12471 
   12472 		  if (stub_type == ppc_stub_plt_call
   12473 		      && irela + 1 < irelaend
   12474 		      && irela[1].r_offset == irela->r_offset + 4
   12475 		      && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
   12476 		    {
   12477 		      if (!tocsave_find (htab, INSERT,
   12478 					 &local_syms, irela + 1, input_bfd))
   12479 			goto error_ret_free_internal;
   12480 		    }
   12481 		  else if (stub_type == ppc_stub_plt_call)
   12482 		    stub_type = ppc_stub_plt_call_r2save;
   12483 
   12484 		  /* Support for grouping stub sections.  */
   12485 		  id_sec = htab->sec_info[section->id].u.group->link_sec;
   12486 
   12487 		  /* Get the name of this stub.  */
   12488 		  stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
   12489 		  if (!stub_name)
   12490 		    goto error_ret_free_internal;
   12491 
   12492 		  stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
   12493 						     stub_name, FALSE, FALSE);
   12494 		  if (stub_entry != NULL)
   12495 		    {
   12496 		      /* The proper stub has already been created.  */
   12497 		      free (stub_name);
   12498 		      if (stub_type == ppc_stub_plt_call_r2save)
   12499 			stub_entry->stub_type = stub_type;
   12500 		      continue;
   12501 		    }
   12502 
   12503 		  stub_entry = ppc_add_stub (stub_name, section, info);
   12504 		  if (stub_entry == NULL)
   12505 		    {
   12506 		      free (stub_name);
   12507 		    error_ret_free_internal:
   12508 		      if (elf_section_data (section)->relocs == NULL)
   12509 			free (internal_relocs);
   12510 		    error_ret_free_local:
   12511 		      if (local_syms != NULL
   12512 			  && (symtab_hdr->contents
   12513 			      != (unsigned char *) local_syms))
   12514 			free (local_syms);
   12515 		      return FALSE;
   12516 		    }
   12517 
   12518 		  stub_entry->stub_type = stub_type;
   12519 		  if (stub_type != ppc_stub_plt_call
   12520 		      && stub_type != ppc_stub_plt_call_r2save)
   12521 		    {
   12522 		      stub_entry->target_value = code_value;
   12523 		      stub_entry->target_section = code_sec;
   12524 		    }
   12525 		  else
   12526 		    {
   12527 		      stub_entry->target_value = sym_value;
   12528 		      stub_entry->target_section = sym_sec;
   12529 		    }
   12530 		  stub_entry->h = hash;
   12531 		  stub_entry->plt_ent = plt_ent;
   12532 		  stub_entry->other = hash ? hash->elf.other : sym->st_other;
   12533 
   12534 		  if (stub_entry->h != NULL)
   12535 		    htab->stub_globals += 1;
   12536 		}
   12537 
   12538 	      /* We're done with the internal relocs, free them.  */
   12539 	      if (elf_section_data (section)->relocs != internal_relocs)
   12540 		free (internal_relocs);
   12541 	    }
   12542 
   12543 	  if (local_syms != NULL
   12544 	      && symtab_hdr->contents != (unsigned char *) local_syms)
   12545 	    {
   12546 	      if (!info->keep_memory)
   12547 		free (local_syms);
   12548 	      else
   12549 		symtab_hdr->contents = (unsigned char *) local_syms;
   12550 	    }
   12551 	}
   12552 
   12553       /* We may have added some stubs.  Find out the new size of the
   12554 	 stub sections.  */
   12555       for (stub_sec = htab->params->stub_bfd->sections;
   12556 	   stub_sec != NULL;
   12557 	   stub_sec = stub_sec->next)
   12558 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12559 	  {
   12560 	    if (htab->stub_iteration <= STUB_SHRINK_ITER
   12561 		|| stub_sec->rawsize < stub_sec->size)
   12562 	      /* Past STUB_SHRINK_ITER, rawsize is the max size seen.  */
   12563 	      stub_sec->rawsize = stub_sec->size;
   12564 	    stub_sec->size = 0;
   12565 	    stub_sec->reloc_count = 0;
   12566 	    stub_sec->flags &= ~SEC_RELOC;
   12567 	  }
   12568 
   12569       htab->brlt->size = 0;
   12570       htab->brlt->reloc_count = 0;
   12571       htab->brlt->flags &= ~SEC_RELOC;
   12572       if (htab->relbrlt != NULL)
   12573 	htab->relbrlt->size = 0;
   12574 
   12575       bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
   12576 
   12577       for (group = htab->group; group != NULL; group = group->next)
   12578 	if (group->needs_save_res)
   12579 	  group->stub_sec->size += htab->sfpr->size;
   12580 
   12581       if (info->emitrelocations
   12582 	  && htab->glink != NULL && htab->glink->size != 0)
   12583 	{
   12584 	  htab->glink->reloc_count = 1;
   12585 	  htab->glink->flags |= SEC_RELOC;
   12586 	}
   12587 
   12588       if (htab->glink_eh_frame != NULL
   12589 	  && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
   12590 	  && htab->glink_eh_frame->output_section->size != 0)
   12591 	{
   12592 	  size_t size = 0, align;
   12593 
   12594 	  for (stub_sec = htab->params->stub_bfd->sections;
   12595 	       stub_sec != NULL;
   12596 	       stub_sec = stub_sec->next)
   12597 	    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12598 	      size += 24;
   12599 	  if (htab->glink != NULL && htab->glink->size != 0)
   12600 	    size += 24;
   12601 	  if (size != 0)
   12602 	    size += sizeof (glink_eh_frame_cie);
   12603 	  align = 1;
   12604 	  align <<= htab->glink_eh_frame->output_section->alignment_power;
   12605 	  align -= 1;
   12606 	  size = (size + align) & ~align;
   12607 	  htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
   12608 	  htab->glink_eh_frame->size = size;
   12609 	}
   12610 
   12611       if (htab->params->plt_stub_align != 0)
   12612 	for (stub_sec = htab->params->stub_bfd->sections;
   12613 	     stub_sec != NULL;
   12614 	     stub_sec = stub_sec->next)
   12615 	  if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12616 	    stub_sec->size = ((stub_sec->size
   12617 			       + (1 << htab->params->plt_stub_align) - 1)
   12618 			      & -(1 << htab->params->plt_stub_align));
   12619 
   12620       for (stub_sec = htab->params->stub_bfd->sections;
   12621 	   stub_sec != NULL;
   12622 	   stub_sec = stub_sec->next)
   12623 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   12624 	    && stub_sec->rawsize != stub_sec->size
   12625 	    && (htab->stub_iteration <= STUB_SHRINK_ITER
   12626 		|| stub_sec->rawsize < stub_sec->size))
   12627 	  break;
   12628 
   12629       if (stub_sec == NULL
   12630 	  && (htab->glink_eh_frame == NULL
   12631 	      || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
   12632 	break;
   12633 
   12634       /* Ask the linker to do its stuff.  */
   12635       (*htab->params->layout_sections_again) ();
   12636     }
   12637 
   12638   if (htab->glink_eh_frame != NULL
   12639       && htab->glink_eh_frame->size != 0)
   12640     {
   12641       bfd_vma val;
   12642       bfd_byte *p, *last_fde;
   12643       size_t last_fde_len, size, align, pad;
   12644       asection *stub_sec;
   12645 
   12646       p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
   12647       if (p == NULL)
   12648 	return FALSE;
   12649       htab->glink_eh_frame->contents = p;
   12650       last_fde = p;
   12651 
   12652       memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
   12653       /* CIE length (rewrite in case little-endian).  */
   12654       last_fde_len = sizeof (glink_eh_frame_cie) - 4;
   12655       bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
   12656       p += sizeof (glink_eh_frame_cie);
   12657 
   12658       for (stub_sec = htab->params->stub_bfd->sections;
   12659 	   stub_sec != NULL;
   12660 	   stub_sec = stub_sec->next)
   12661 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   12662 	  {
   12663 	    last_fde = p;
   12664 	    last_fde_len = 20;
   12665 	    /* FDE length.  */
   12666 	    bfd_put_32 (htab->elf.dynobj, 20, p);
   12667 	    p += 4;
   12668 	    /* CIE pointer.  */
   12669 	    val = p - htab->glink_eh_frame->contents;
   12670 	    bfd_put_32 (htab->elf.dynobj, val, p);
   12671 	    p += 4;
   12672 	    /* Offset to stub section, written later.  */
   12673 	    p += 4;
   12674 	    /* stub section size.  */
   12675 	    bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
   12676 	    p += 4;
   12677 	    /* Augmentation.  */
   12678 	    p += 1;
   12679 	    /* Pad.  */
   12680 	    p += 7;
   12681 	  }
   12682       if (htab->glink != NULL && htab->glink->size != 0)
   12683 	{
   12684 	  last_fde = p;
   12685 	  last_fde_len = 20;
   12686 	  /* FDE length.  */
   12687 	  bfd_put_32 (htab->elf.dynobj, 20, p);
   12688 	  p += 4;
   12689 	  /* CIE pointer.  */
   12690 	  val = p - htab->glink_eh_frame->contents;
   12691 	  bfd_put_32 (htab->elf.dynobj, val, p);
   12692 	  p += 4;
   12693 	  /* Offset to .glink, written later.  */
   12694 	  p += 4;
   12695 	  /* .glink size.  */
   12696 	  bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
   12697 	  p += 4;
   12698 	  /* Augmentation.  */
   12699 	  p += 1;
   12700 
   12701 	  *p++ = DW_CFA_advance_loc + 1;
   12702 	  *p++ = DW_CFA_register;
   12703 	  *p++ = 65;
   12704 	  *p++ = htab->opd_abi ? 12 : 0;
   12705 	  *p++ = DW_CFA_advance_loc + 4;
   12706 	  *p++ = DW_CFA_restore_extended;
   12707 	  *p++ = 65;
   12708 	}
   12709       /* Subsume any padding into the last FDE if user .eh_frame
   12710 	 sections are aligned more than glink_eh_frame.  Otherwise any
   12711 	 zero padding will be seen as a terminator.  */
   12712       size = p - htab->glink_eh_frame->contents;
   12713       align = 1;
   12714       align <<= htab->glink_eh_frame->output_section->alignment_power;
   12715       align -= 1;
   12716       pad = ((size + align) & ~align) - size;
   12717       htab->glink_eh_frame->size = size + pad;
   12718       bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
   12719     }
   12720 
   12721   maybe_strip_output (info, htab->brlt);
   12722   if (htab->glink_eh_frame != NULL)
   12723     maybe_strip_output (info, htab->glink_eh_frame);
   12724 
   12725   return TRUE;
   12726 }
   12727 
   12728 /* Called after we have determined section placement.  If sections
   12729    move, we'll be called again.  Provide a value for TOCstart.  */
   12730 
   12731 bfd_vma
   12732 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
   12733 {
   12734   asection *s;
   12735   bfd_vma TOCstart, adjust;
   12736 
   12737   if (info != NULL)
   12738     {
   12739       struct elf_link_hash_entry *h;
   12740       struct elf_link_hash_table *htab = elf_hash_table (info);
   12741 
   12742       if (is_elf_hash_table (htab)
   12743 	  && htab->hgot != NULL)
   12744 	h = htab->hgot;
   12745       else
   12746 	{
   12747 	  h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
   12748 	  if (is_elf_hash_table (htab))
   12749 	    htab->hgot = h;
   12750 	}
   12751       if (h != NULL
   12752 	  && h->root.type == bfd_link_hash_defined
   12753 	  && !h->root.linker_def
   12754 	  && (!is_elf_hash_table (htab)
   12755 	      || h->def_regular))
   12756 	{
   12757 	  TOCstart = (h->root.u.def.value - TOC_BASE_OFF
   12758 		      + h->root.u.def.section->output_offset
   12759 		      + h->root.u.def.section->output_section->vma);
   12760 	  _bfd_set_gp_value (obfd, TOCstart);
   12761 	  return TOCstart;
   12762 	}
   12763     }
   12764 
   12765   /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
   12766      order.  The TOC starts where the first of these sections starts.  */
   12767   s = bfd_get_section_by_name (obfd, ".got");
   12768   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   12769     s = bfd_get_section_by_name (obfd, ".toc");
   12770   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   12771     s = bfd_get_section_by_name (obfd, ".tocbss");
   12772   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   12773     s = bfd_get_section_by_name (obfd, ".plt");
   12774   if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
   12775     {
   12776       /* This may happen for
   12777 	 o  references to TOC base (SYM@toc / TOC[tc0]) without a
   12778 	 .toc directive
   12779 	 o  bad linker script
   12780 	 o --gc-sections and empty TOC sections
   12781 
   12782 	 FIXME: Warn user?  */
   12783 
   12784       /* Look for a likely section.  We probably won't even be
   12785 	 using TOCstart.  */
   12786       for (s = obfd->sections; s != NULL; s = s->next)
   12787 	if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
   12788 			 | SEC_EXCLUDE))
   12789 	    == (SEC_ALLOC | SEC_SMALL_DATA))
   12790 	  break;
   12791       if (s == NULL)
   12792 	for (s = obfd->sections; s != NULL; s = s->next)
   12793 	  if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
   12794 	      == (SEC_ALLOC | SEC_SMALL_DATA))
   12795 	    break;
   12796       if (s == NULL)
   12797 	for (s = obfd->sections; s != NULL; s = s->next)
   12798 	  if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
   12799 	      == SEC_ALLOC)
   12800 	    break;
   12801       if (s == NULL)
   12802 	for (s = obfd->sections; s != NULL; s = s->next)
   12803 	  if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
   12804 	    break;
   12805     }
   12806 
   12807   TOCstart = 0;
   12808   if (s != NULL)
   12809     TOCstart = s->output_section->vma + s->output_offset;
   12810 
   12811   /* Force alignment.  */
   12812   adjust = TOCstart & (TOC_BASE_ALIGN - 1);
   12813   TOCstart -= adjust;
   12814   _bfd_set_gp_value (obfd, TOCstart);
   12815 
   12816   if (info != NULL && s != NULL)
   12817     {
   12818       struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12819 
   12820       if (htab != NULL)
   12821 	{
   12822 	  if (htab->elf.hgot != NULL)
   12823 	    {
   12824 	      htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
   12825 	      htab->elf.hgot->root.u.def.section = s;
   12826 	    }
   12827 	}
   12828       else
   12829 	{
   12830 	  struct bfd_link_hash_entry *bh = NULL;
   12831 	  _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
   12832 					    s, TOC_BASE_OFF - adjust,
   12833 					    NULL, FALSE, FALSE, &bh);
   12834 	}
   12835     }
   12836   return TOCstart;
   12837 }
   12838 
   12839 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
   12840    write out any global entry stubs.  */
   12841 
   12842 static bfd_boolean
   12843 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
   12844 {
   12845   struct bfd_link_info *info;
   12846   struct ppc_link_hash_table *htab;
   12847   struct plt_entry *pent;
   12848   asection *s;
   12849 
   12850   if (h->root.type == bfd_link_hash_indirect)
   12851     return TRUE;
   12852 
   12853   if (!h->pointer_equality_needed)
   12854     return TRUE;
   12855 
   12856   if (h->def_regular)
   12857     return TRUE;
   12858 
   12859   info = inf;
   12860   htab = ppc_hash_table (info);
   12861   if (htab == NULL)
   12862     return FALSE;
   12863 
   12864   s = htab->glink;
   12865   for (pent = h->plt.plist; pent != NULL; pent = pent->next)
   12866     if (pent->plt.offset != (bfd_vma) -1
   12867 	&& pent->addend == 0)
   12868       {
   12869 	bfd_byte *p;
   12870 	asection *plt;
   12871 	bfd_vma off;
   12872 
   12873 	p = s->contents + h->root.u.def.value;
   12874 	plt = htab->elf.splt;
   12875 	if (!htab->elf.dynamic_sections_created
   12876 	    || h->dynindx == -1)
   12877 	  plt = htab->elf.iplt;
   12878 	off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
   12879 	off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
   12880 
   12881 	if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
   12882 	  {
   12883 	    info->callbacks->einfo
   12884 	      (_("%P: linkage table error against `%T'\n"),
   12885 	       h->root.root.string);
   12886 	    bfd_set_error (bfd_error_bad_value);
   12887 	    htab->stub_error = TRUE;
   12888 	  }
   12889 
   12890 	htab->stub_count[ppc_stub_global_entry - 1] += 1;
   12891 	if (htab->params->emit_stub_syms)
   12892 	  {
   12893 	    size_t len = strlen (h->root.root.string);
   12894 	    char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
   12895 
   12896 	    if (name == NULL)
   12897 	      return FALSE;
   12898 
   12899 	    sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
   12900 	    h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
   12901 	    if (h == NULL)
   12902 	      return FALSE;
   12903 	    if (h->root.type == bfd_link_hash_new)
   12904 	      {
   12905 		h->root.type = bfd_link_hash_defined;
   12906 		h->root.u.def.section = s;
   12907 		h->root.u.def.value = p - s->contents;
   12908 		h->ref_regular = 1;
   12909 		h->def_regular = 1;
   12910 		h->ref_regular_nonweak = 1;
   12911 		h->forced_local = 1;
   12912 		h->non_elf = 0;
   12913 		h->root.linker_def = 1;
   12914 	      }
   12915 	  }
   12916 
   12917 	if (PPC_HA (off) != 0)
   12918 	  {
   12919 	    bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
   12920 	    p += 4;
   12921 	  }
   12922 	bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
   12923 	p += 4;
   12924 	bfd_put_32 (s->owner, MTCTR_R12, p);
   12925 	p += 4;
   12926 	bfd_put_32 (s->owner, BCTR, p);
   12927 	break;
   12928       }
   12929   return TRUE;
   12930 }
   12931 
   12932 /* Build all the stubs associated with the current output file.
   12933    The stubs are kept in a hash table attached to the main linker
   12934    hash table.  This function is called via gldelf64ppc_finish.  */
   12935 
   12936 bfd_boolean
   12937 ppc64_elf_build_stubs (struct bfd_link_info *info,
   12938 		       char **stats)
   12939 {
   12940   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   12941   struct map_stub *group;
   12942   asection *stub_sec;
   12943   bfd_byte *p;
   12944   int stub_sec_count = 0;
   12945 
   12946   if (htab == NULL)
   12947     return FALSE;
   12948 
   12949   /* Allocate memory to hold the linker stubs.  */
   12950   for (stub_sec = htab->params->stub_bfd->sections;
   12951        stub_sec != NULL;
   12952        stub_sec = stub_sec->next)
   12953     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
   12954 	&& stub_sec->size != 0)
   12955       {
   12956 	stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
   12957 	if (stub_sec->contents == NULL)
   12958 	  return FALSE;
   12959 	stub_sec->size = 0;
   12960       }
   12961 
   12962   if (htab->glink != NULL && htab->glink->size != 0)
   12963     {
   12964       unsigned int indx;
   12965       bfd_vma plt0;
   12966 
   12967       /* Build the .glink plt call stub.  */
   12968       if (htab->params->emit_stub_syms)
   12969 	{
   12970 	  struct elf_link_hash_entry *h;
   12971 	  h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
   12972 				    TRUE, FALSE, FALSE);
   12973 	  if (h == NULL)
   12974 	    return FALSE;
   12975 	  if (h->root.type == bfd_link_hash_new)
   12976 	    {
   12977 	      h->root.type = bfd_link_hash_defined;
   12978 	      h->root.u.def.section = htab->glink;
   12979 	      h->root.u.def.value = 8;
   12980 	      h->ref_regular = 1;
   12981 	      h->def_regular = 1;
   12982 	      h->ref_regular_nonweak = 1;
   12983 	      h->forced_local = 1;
   12984 	      h->non_elf = 0;
   12985 	      h->root.linker_def = 1;
   12986 	    }
   12987 	}
   12988       plt0 = (htab->elf.splt->output_section->vma
   12989 	      + htab->elf.splt->output_offset
   12990 	      - 16);
   12991       if (info->emitrelocations)
   12992 	{
   12993 	  Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
   12994 	  if (r == NULL)
   12995 	    return FALSE;
   12996 	  r->r_offset = (htab->glink->output_offset
   12997 			 + htab->glink->output_section->vma);
   12998 	  r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
   12999 	  r->r_addend = plt0;
   13000 	}
   13001       p = htab->glink->contents;
   13002       plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
   13003       bfd_put_64 (htab->glink->owner, plt0, p);
   13004       p += 8;
   13005       if (htab->opd_abi)
   13006 	{
   13007 	  bfd_put_32 (htab->glink->owner, MFLR_R12, p);
   13008 	  p += 4;
   13009 	  bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   13010 	  p += 4;
   13011 	  bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   13012 	  p += 4;
   13013 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
   13014 	  p += 4;
   13015 	  bfd_put_32 (htab->glink->owner, MTLR_R12, p);
   13016 	  p += 4;
   13017 	  bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
   13018 	  p += 4;
   13019 	  bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
   13020 	  p += 4;
   13021 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
   13022 	  p += 4;
   13023 	  bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
   13024 	  p += 4;
   13025 	  bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
   13026 	  p += 4;
   13027 	}
   13028       else
   13029 	{
   13030 	  bfd_put_32 (htab->glink->owner, MFLR_R0, p);
   13031 	  p += 4;
   13032 	  bfd_put_32 (htab->glink->owner, BCL_20_31, p);
   13033 	  p += 4;
   13034 	  bfd_put_32 (htab->glink->owner, MFLR_R11, p);
   13035 	  p += 4;
   13036 	  bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
   13037 	  p += 4;
   13038 	  bfd_put_32 (htab->glink->owner, MTLR_R0, p);
   13039 	  p += 4;
   13040 	  bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
   13041 	  p += 4;
   13042 	  bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
   13043 	  p += 4;
   13044 	  bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
   13045 	  p += 4;
   13046 	  bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
   13047 	  p += 4;
   13048 	  bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
   13049 	  p += 4;
   13050 	  bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
   13051 	  p += 4;
   13052 	  bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
   13053 	  p += 4;
   13054 	}
   13055       bfd_put_32 (htab->glink->owner, BCTR, p);
   13056       p += 4;
   13057       while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
   13058 	{
   13059 	  bfd_put_32 (htab->glink->owner, NOP, p);
   13060 	  p += 4;
   13061 	}
   13062 
   13063       /* Build the .glink lazy link call stubs.  */
   13064       indx = 0;
   13065       while (p < htab->glink->contents + htab->glink->rawsize)
   13066 	{
   13067 	  if (htab->opd_abi)
   13068 	    {
   13069 	      if (indx < 0x8000)
   13070 		{
   13071 		  bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
   13072 		  p += 4;
   13073 		}
   13074 	      else
   13075 		{
   13076 		  bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
   13077 		  p += 4;
   13078 		  bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
   13079 			      p);
   13080 		  p += 4;
   13081 		}
   13082 	    }
   13083 	  bfd_put_32 (htab->glink->owner,
   13084 		      B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
   13085 	  indx++;
   13086 	  p += 4;
   13087 	}
   13088 
   13089       /* Build .glink global entry stubs.  */
   13090       if (htab->glink->size > htab->glink->rawsize)
   13091 	elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
   13092     }
   13093 
   13094   if (htab->brlt != NULL && htab->brlt->size != 0)
   13095     {
   13096       htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
   13097 					 htab->brlt->size);
   13098       if (htab->brlt->contents == NULL)
   13099 	return FALSE;
   13100     }
   13101   if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
   13102     {
   13103       htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
   13104 					    htab->relbrlt->size);
   13105       if (htab->relbrlt->contents == NULL)
   13106 	return FALSE;
   13107     }
   13108 
   13109   /* Build the stubs as directed by the stub hash table.  */
   13110   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
   13111 
   13112   for (group = htab->group; group != NULL; group = group->next)
   13113     if (group->needs_save_res)
   13114       {
   13115 	stub_sec = group->stub_sec;
   13116 	memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
   13117 		htab->sfpr->size);
   13118 	if (htab->params->emit_stub_syms)
   13119 	  {
   13120 	    unsigned int i;
   13121 
   13122 	    for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
   13123 	      if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
   13124 		return FALSE;
   13125 	  }
   13126 	stub_sec->size += htab->sfpr->size;
   13127       }
   13128 
   13129   if (htab->relbrlt != NULL)
   13130     htab->relbrlt->reloc_count = 0;
   13131 
   13132   if (htab->params->plt_stub_align != 0)
   13133     for (stub_sec = htab->params->stub_bfd->sections;
   13134 	 stub_sec != NULL;
   13135 	 stub_sec = stub_sec->next)
   13136       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   13137 	stub_sec->size = ((stub_sec->size
   13138 			   + (1 << htab->params->plt_stub_align) - 1)
   13139 			  & -(1 << htab->params->plt_stub_align));
   13140 
   13141   for (stub_sec = htab->params->stub_bfd->sections;
   13142        stub_sec != NULL;
   13143        stub_sec = stub_sec->next)
   13144     if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   13145       {
   13146 	stub_sec_count += 1;
   13147 	if (stub_sec->rawsize != stub_sec->size
   13148 	    && (htab->stub_iteration <= STUB_SHRINK_ITER
   13149 		|| stub_sec->rawsize < stub_sec->size))
   13150 	  break;
   13151       }
   13152 
   13153   /* Note that the glink_eh_frame check here is not only testing that
   13154      the generated size matched the calculated size but also that
   13155      bfd_elf_discard_info didn't make any changes to the section.  */
   13156   if (stub_sec != NULL
   13157       || (htab->glink_eh_frame != NULL
   13158 	  && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
   13159     {
   13160       htab->stub_error = TRUE;
   13161       info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
   13162     }
   13163 
   13164   if (htab->stub_error)
   13165     return FALSE;
   13166 
   13167   if (stats != NULL)
   13168     {
   13169       *stats = bfd_malloc (500);
   13170       if (*stats == NULL)
   13171 	return FALSE;
   13172 
   13173       sprintf (*stats, _("linker stubs in %u group%s\n"
   13174 			 "  branch       %lu\n"
   13175 			 "  toc adjust   %lu\n"
   13176 			 "  long branch  %lu\n"
   13177 			 "  long toc adj %lu\n"
   13178 			 "  plt call     %lu\n"
   13179 			 "  plt call toc %lu\n"
   13180 			 "  global entry %lu"),
   13181 	       stub_sec_count,
   13182 	       stub_sec_count == 1 ? "" : "s",
   13183 	       htab->stub_count[ppc_stub_long_branch - 1],
   13184 	       htab->stub_count[ppc_stub_long_branch_r2off - 1],
   13185 	       htab->stub_count[ppc_stub_plt_branch - 1],
   13186 	       htab->stub_count[ppc_stub_plt_branch_r2off - 1],
   13187 	       htab->stub_count[ppc_stub_plt_call - 1],
   13188 	       htab->stub_count[ppc_stub_plt_call_r2save - 1],
   13189 	       htab->stub_count[ppc_stub_global_entry - 1]);
   13190     }
   13191   return TRUE;
   13192 }
   13193 
   13194 /* This function undoes the changes made by add_symbol_adjust.  */
   13195 
   13196 static bfd_boolean
   13197 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
   13198 {
   13199   struct ppc_link_hash_entry *eh;
   13200 
   13201   if (h->root.type == bfd_link_hash_indirect)
   13202     return TRUE;
   13203 
   13204   eh = (struct ppc_link_hash_entry *) h;
   13205   if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
   13206     return TRUE;
   13207 
   13208   eh->elf.root.type = bfd_link_hash_undefined;
   13209   return TRUE;
   13210 }
   13211 
   13212 void
   13213 ppc64_elf_restore_symbols (struct bfd_link_info *info)
   13214 {
   13215   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   13216 
   13217   if (htab != NULL)
   13218     elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
   13219 }
   13220 
   13221 /* What to do when ld finds relocations against symbols defined in
   13222    discarded sections.  */
   13223 
   13224 static unsigned int
   13225 ppc64_elf_action_discarded (asection *sec)
   13226 {
   13227   if (strcmp (".opd", sec->name) == 0)
   13228     return 0;
   13229 
   13230   if (strcmp (".toc", sec->name) == 0)
   13231     return 0;
   13232 
   13233   if (strcmp (".toc1", sec->name) == 0)
   13234     return 0;
   13235 
   13236   return _bfd_elf_default_action_discarded (sec);
   13237 }
   13238 
   13239 /* The RELOCATE_SECTION function is called by the ELF backend linker
   13240    to handle the relocations for a section.
   13241 
   13242    The relocs are always passed as Rela structures; if the section
   13243    actually uses Rel structures, the r_addend field will always be
   13244    zero.
   13245 
   13246    This function is responsible for adjust the section contents as
   13247    necessary, and (if using Rela relocs and generating a
   13248    relocatable output file) adjusting the reloc addend as
   13249    necessary.
   13250 
   13251    This function does not have to worry about setting the reloc
   13252    address or the reloc symbol index.
   13253 
   13254    LOCAL_SYMS is a pointer to the swapped in local symbols.
   13255 
   13256    LOCAL_SECTIONS is an array giving the section in the input file
   13257    corresponding to the st_shndx field of each local symbol.
   13258 
   13259    The global hash table entry for the global symbols can be found
   13260    via elf_sym_hashes (input_bfd).
   13261 
   13262    When generating relocatable output, this function must handle
   13263    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   13264    going to be the section symbol corresponding to the output
   13265    section, which means that the addend must be adjusted
   13266    accordingly.  */
   13267 
   13268 static bfd_boolean
   13269 ppc64_elf_relocate_section (bfd *output_bfd,
   13270 			    struct bfd_link_info *info,
   13271 			    bfd *input_bfd,
   13272 			    asection *input_section,
   13273 			    bfd_byte *contents,
   13274 			    Elf_Internal_Rela *relocs,
   13275 			    Elf_Internal_Sym *local_syms,
   13276 			    asection **local_sections)
   13277 {
   13278   struct ppc_link_hash_table *htab;
   13279   Elf_Internal_Shdr *symtab_hdr;
   13280   struct elf_link_hash_entry **sym_hashes;
   13281   Elf_Internal_Rela *rel;
   13282   Elf_Internal_Rela *wrel;
   13283   Elf_Internal_Rela *relend;
   13284   Elf_Internal_Rela outrel;
   13285   bfd_byte *loc;
   13286   struct got_entry **local_got_ents;
   13287   bfd_vma TOCstart;
   13288   bfd_boolean ret = TRUE;
   13289   bfd_boolean is_opd;
   13290   /* Assume 'at' branch hints.  */
   13291   bfd_boolean is_isa_v2 = TRUE;
   13292   bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
   13293 
   13294   /* Initialize howto table if needed.  */
   13295   if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
   13296     ppc_howto_init ();
   13297 
   13298   htab = ppc_hash_table (info);
   13299   if (htab == NULL)
   13300     return FALSE;
   13301 
   13302   /* Don't relocate stub sections.  */
   13303   if (input_section->owner == htab->params->stub_bfd)
   13304     return TRUE;
   13305 
   13306   BFD_ASSERT (is_ppc64_elf (input_bfd));
   13307 
   13308   local_got_ents = elf_local_got_ents (input_bfd);
   13309   TOCstart = elf_gp (output_bfd);
   13310   symtab_hdr = &elf_symtab_hdr (input_bfd);
   13311   sym_hashes = elf_sym_hashes (input_bfd);
   13312   is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
   13313 
   13314   rel = wrel = relocs;
   13315   relend = relocs + input_section->reloc_count;
   13316   for (; rel < relend; wrel++, rel++)
   13317     {
   13318       enum elf_ppc64_reloc_type r_type;
   13319       bfd_vma addend;
   13320       bfd_reloc_status_type r;
   13321       Elf_Internal_Sym *sym;
   13322       asection *sec;
   13323       struct elf_link_hash_entry *h_elf;
   13324       struct ppc_link_hash_entry *h;
   13325       struct ppc_link_hash_entry *fdh;
   13326       const char *sym_name;
   13327       unsigned long r_symndx, toc_symndx;
   13328       bfd_vma toc_addend;
   13329       unsigned char tls_mask, tls_gd, tls_type;
   13330       unsigned char sym_type;
   13331       bfd_vma relocation;
   13332       bfd_boolean unresolved_reloc;
   13333       bfd_boolean warned;
   13334       enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
   13335       unsigned int insn;
   13336       unsigned int mask;
   13337       struct ppc_stub_hash_entry *stub_entry;
   13338       bfd_vma max_br_offset;
   13339       bfd_vma from;
   13340       Elf_Internal_Rela orig_rel;
   13341       reloc_howto_type *howto;
   13342       struct reloc_howto_struct alt_howto;
   13343 
   13344     again:
   13345       orig_rel = *rel;
   13346 
   13347       r_type = ELF64_R_TYPE (rel->r_info);
   13348       r_symndx = ELF64_R_SYM (rel->r_info);
   13349 
   13350       /* For old style R_PPC64_TOC relocs with a zero symbol, use the
   13351 	 symbol of the previous ADDR64 reloc.  The symbol gives us the
   13352 	 proper TOC base to use.  */
   13353       if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
   13354 	  && wrel != relocs
   13355 	  && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
   13356 	  && is_opd)
   13357 	r_symndx = ELF64_R_SYM (wrel[-1].r_info);
   13358 
   13359       sym = NULL;
   13360       sec = NULL;
   13361       h_elf = NULL;
   13362       sym_name = NULL;
   13363       unresolved_reloc = FALSE;
   13364       warned = FALSE;
   13365 
   13366       if (r_symndx < symtab_hdr->sh_info)
   13367 	{
   13368 	  /* It's a local symbol.  */
   13369 	  struct _opd_sec_data *opd;
   13370 
   13371 	  sym = local_syms + r_symndx;
   13372 	  sec = local_sections[r_symndx];
   13373 	  sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
   13374 	  sym_type = ELF64_ST_TYPE (sym->st_info);
   13375 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   13376 	  opd = get_opd_info (sec);
   13377 	  if (opd != NULL && opd->adjust != NULL)
   13378 	    {
   13379 	      long adjust = opd->adjust[OPD_NDX (sym->st_value
   13380 						 + rel->r_addend)];
   13381 	      if (adjust == -1)
   13382 		relocation = 0;
   13383 	      else
   13384 		{
   13385 		  /* If this is a relocation against the opd section sym
   13386 		     and we have edited .opd, adjust the reloc addend so
   13387 		     that ld -r and ld --emit-relocs output is correct.
   13388 		     If it is a reloc against some other .opd symbol,
   13389 		     then the symbol value will be adjusted later.  */
   13390 		  if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   13391 		    rel->r_addend += adjust;
   13392 		  else
   13393 		    relocation += adjust;
   13394 		}
   13395 	    }
   13396 	}
   13397       else
   13398 	{
   13399 	  bfd_boolean ignored;
   13400 
   13401 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   13402 				   r_symndx, symtab_hdr, sym_hashes,
   13403 				   h_elf, sec, relocation,
   13404 				   unresolved_reloc, warned, ignored);
   13405 	  sym_name = h_elf->root.root.string;
   13406 	  sym_type = h_elf->type;
   13407 	  if (sec != NULL
   13408 	      && sec->owner == output_bfd
   13409 	      && strcmp (sec->name, ".opd") == 0)
   13410 	    {
   13411 	      /* This is a symbol defined in a linker script.  All
   13412 		 such are defined in output sections, even those
   13413 		 defined by simple assignment from a symbol defined in
   13414 		 an input section.  Transfer the symbol to an
   13415 		 appropriate input .opd section, so that a branch to
   13416 		 this symbol will be mapped to the location specified
   13417 		 by the opd entry.  */
   13418 	      struct bfd_link_order *lo;
   13419 	      for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
   13420 		if (lo->type == bfd_indirect_link_order)
   13421 		  {
   13422 		    asection *isec = lo->u.indirect.section;
   13423 		    if (h_elf->root.u.def.value >= isec->output_offset
   13424 			&& h_elf->root.u.def.value < (isec->output_offset
   13425 						      + isec->size))
   13426 		      {
   13427 			h_elf->root.u.def.value -= isec->output_offset;
   13428 			h_elf->root.u.def.section = isec;
   13429 			sec = isec;
   13430 			break;
   13431 		      }
   13432 		  }
   13433 	    }
   13434 	}
   13435       h = (struct ppc_link_hash_entry *) h_elf;
   13436 
   13437       if (sec != NULL && discarded_section (sec))
   13438 	{
   13439 	  _bfd_clear_contents (ppc64_elf_howto_table[r_type],
   13440 			       input_bfd, input_section,
   13441 			       contents + rel->r_offset);
   13442 	  wrel->r_offset = rel->r_offset;
   13443 	  wrel->r_info = 0;
   13444 	  wrel->r_addend = 0;
   13445 
   13446 	  /* For ld -r, remove relocations in debug sections against
   13447 	     sections defined in discarded sections.  Not done for
   13448 	     non-debug to preserve relocs in .eh_frame which the
   13449 	     eh_frame editing code expects to be present.  */
   13450 	  if (bfd_link_relocatable (info)
   13451 	      && (input_section->flags & SEC_DEBUGGING))
   13452 	    wrel--;
   13453 
   13454 	  continue;
   13455 	}
   13456 
   13457       if (bfd_link_relocatable (info))
   13458 	goto copy_reloc;
   13459 
   13460       if (h != NULL && &h->elf == htab->elf.hgot)
   13461 	{
   13462 	  relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
   13463 	  sec = bfd_abs_section_ptr;
   13464 	  unresolved_reloc = FALSE;
   13465 	}
   13466 
   13467       /* TLS optimizations.  Replace instruction sequences and relocs
   13468 	 based on information we collected in tls_optimize.  We edit
   13469 	 RELOCS so that --emit-relocs will output something sensible
   13470 	 for the final instruction stream.  */
   13471       tls_mask = 0;
   13472       tls_gd = 0;
   13473       toc_symndx = 0;
   13474       if (h != NULL)
   13475 	tls_mask = h->tls_mask;
   13476       else if (local_got_ents != NULL)
   13477 	{
   13478 	  struct plt_entry **local_plt = (struct plt_entry **)
   13479 	    (local_got_ents + symtab_hdr->sh_info);
   13480 	  unsigned char *lgot_masks = (unsigned char *)
   13481 	    (local_plt + symtab_hdr->sh_info);
   13482 	  tls_mask = lgot_masks[r_symndx];
   13483 	}
   13484       if (tls_mask == 0
   13485 	  && (r_type == R_PPC64_TLS
   13486 	      || r_type == R_PPC64_TLSGD
   13487 	      || r_type == R_PPC64_TLSLD))
   13488 	{
   13489 	  /* Check for toc tls entries.  */
   13490 	  unsigned char *toc_tls;
   13491 
   13492 	  if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   13493 			     &local_syms, rel, input_bfd))
   13494 	    return FALSE;
   13495 
   13496 	  if (toc_tls)
   13497 	    tls_mask = *toc_tls;
   13498 	}
   13499 
   13500       /* Check that tls relocs are used with tls syms, and non-tls
   13501 	 relocs are used with non-tls syms.  */
   13502       if (r_symndx != STN_UNDEF
   13503 	  && r_type != R_PPC64_NONE
   13504 	  && (h == NULL
   13505 	      || h->elf.root.type == bfd_link_hash_defined
   13506 	      || h->elf.root.type == bfd_link_hash_defweak)
   13507 	  && (IS_PPC64_TLS_RELOC (r_type)
   13508 	      != (sym_type == STT_TLS
   13509 		  || (sym_type == STT_SECTION
   13510 		      && (sec->flags & SEC_THREAD_LOCAL) != 0))))
   13511 	{
   13512 	  if (tls_mask != 0
   13513 	      && (r_type == R_PPC64_TLS
   13514 		  || r_type == R_PPC64_TLSGD
   13515 		  || r_type == R_PPC64_TLSLD))
   13516 	    /* R_PPC64_TLS is OK against a symbol in the TOC.  */
   13517 	    ;
   13518 	  else
   13519 	    info->callbacks->einfo
   13520 	      (!IS_PPC64_TLS_RELOC (r_type)
   13521 	       ? _("%P: %H: %s used with TLS symbol `%T'\n")
   13522 	       : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
   13523 	       input_bfd, input_section, rel->r_offset,
   13524 	       ppc64_elf_howto_table[r_type]->name,
   13525 	       sym_name);
   13526 	}
   13527 
   13528       /* Ensure reloc mapping code below stays sane.  */
   13529       if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
   13530 	  || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
   13531 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TLSGD16 & 3)
   13532 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
   13533 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
   13534 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
   13535 	  || (R_PPC64_GOT_TLSLD16 & 3)    != (R_PPC64_GOT_TPREL16_DS & 3)
   13536 	  || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
   13537 	  || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
   13538 	  || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
   13539 	abort ();
   13540 
   13541       switch (r_type)
   13542 	{
   13543 	default:
   13544 	  break;
   13545 
   13546 	case R_PPC64_LO_DS_OPT:
   13547 	  insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
   13548 	  if ((insn & (0x3f << 26)) != 58u << 26)
   13549 	    abort ();
   13550 	  insn += (14u << 26) - (58u << 26);
   13551 	  bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
   13552 	  r_type = R_PPC64_TOC16_LO;
   13553 	  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13554 	  break;
   13555 
   13556 	case R_PPC64_TOC16:
   13557 	case R_PPC64_TOC16_LO:
   13558 	case R_PPC64_TOC16_DS:
   13559 	case R_PPC64_TOC16_LO_DS:
   13560 	  {
   13561 	    /* Check for toc tls entries.  */
   13562 	    unsigned char *toc_tls;
   13563 	    int retval;
   13564 
   13565 	    retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
   13566 				   &local_syms, rel, input_bfd);
   13567 	    if (retval == 0)
   13568 	      return FALSE;
   13569 
   13570 	    if (toc_tls)
   13571 	      {
   13572 		tls_mask = *toc_tls;
   13573 		if (r_type == R_PPC64_TOC16_DS
   13574 		    || r_type == R_PPC64_TOC16_LO_DS)
   13575 		  {
   13576 		    if (tls_mask != 0
   13577 			&& (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
   13578 		      goto toctprel;
   13579 		  }
   13580 		else
   13581 		  {
   13582 		    /* If we found a GD reloc pair, then we might be
   13583 		       doing a GD->IE transition.  */
   13584 		    if (retval == 2)
   13585 		      {
   13586 			tls_gd = TLS_TPRELGD;
   13587 			if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   13588 			  goto tls_ldgd_opt;
   13589 		      }
   13590 		    else if (retval == 3)
   13591 		      {
   13592 			if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   13593 			  goto tls_ldgd_opt;
   13594 		      }
   13595 		  }
   13596 	      }
   13597 	  }
   13598 	  break;
   13599 
   13600 	case R_PPC64_GOT_TPREL16_HI:
   13601 	case R_PPC64_GOT_TPREL16_HA:
   13602 	  if (tls_mask != 0
   13603 	      && (tls_mask & TLS_TPREL) == 0)
   13604 	    {
   13605 	      rel->r_offset -= d_offset;
   13606 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   13607 	      r_type = R_PPC64_NONE;
   13608 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13609 	    }
   13610 	  break;
   13611 
   13612 	case R_PPC64_GOT_TPREL16_DS:
   13613 	case R_PPC64_GOT_TPREL16_LO_DS:
   13614 	  if (tls_mask != 0
   13615 	      && (tls_mask & TLS_TPREL) == 0)
   13616 	    {
   13617 	    toctprel:
   13618 	      insn = bfd_get_32 (output_bfd,
   13619 				 contents + rel->r_offset - d_offset);
   13620 	      insn &= 31 << 21;
   13621 	      insn |= 0x3c0d0000;	/* addis 0,13,0 */
   13622 	      bfd_put_32 (output_bfd, insn,
   13623 			  contents + rel->r_offset - d_offset);
   13624 	      r_type = R_PPC64_TPREL16_HA;
   13625 	      if (toc_symndx != 0)
   13626 		{
   13627 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   13628 		  rel->r_addend = toc_addend;
   13629 		  /* We changed the symbol.  Start over in order to
   13630 		     get h, sym, sec etc. right.  */
   13631 		  goto again;
   13632 		}
   13633 	      else
   13634 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13635 	    }
   13636 	  break;
   13637 
   13638 	case R_PPC64_TLS:
   13639 	  if (tls_mask != 0
   13640 	      && (tls_mask & TLS_TPREL) == 0)
   13641 	    {
   13642 	      insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
   13643 	      insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
   13644 	      if (insn == 0)
   13645 		abort ();
   13646 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   13647 	      /* Was PPC64_TLS which sits on insn boundary, now
   13648 		 PPC64_TPREL16_LO which is at low-order half-word.  */
   13649 	      rel->r_offset += d_offset;
   13650 	      r_type = R_PPC64_TPREL16_LO;
   13651 	      if (toc_symndx != 0)
   13652 		{
   13653 		  rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
   13654 		  rel->r_addend = toc_addend;
   13655 		  /* We changed the symbol.  Start over in order to
   13656 		     get h, sym, sec etc. right.  */
   13657 		  goto again;
   13658 		}
   13659 	      else
   13660 		rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13661 	    }
   13662 	  break;
   13663 
   13664 	case R_PPC64_GOT_TLSGD16_HI:
   13665 	case R_PPC64_GOT_TLSGD16_HA:
   13666 	  tls_gd = TLS_TPRELGD;
   13667 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   13668 	    goto tls_gdld_hi;
   13669 	  break;
   13670 
   13671 	case R_PPC64_GOT_TLSLD16_HI:
   13672 	case R_PPC64_GOT_TLSLD16_HA:
   13673 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   13674 	    {
   13675 	    tls_gdld_hi:
   13676 	      if ((tls_mask & tls_gd) != 0)
   13677 		r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   13678 			  + R_PPC64_GOT_TPREL16_DS);
   13679 	      else
   13680 		{
   13681 		  rel->r_offset -= d_offset;
   13682 		  bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   13683 		  r_type = R_PPC64_NONE;
   13684 		}
   13685 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13686 	    }
   13687 	  break;
   13688 
   13689 	case R_PPC64_GOT_TLSGD16:
   13690 	case R_PPC64_GOT_TLSGD16_LO:
   13691 	  tls_gd = TLS_TPRELGD;
   13692 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   13693 	    goto tls_ldgd_opt;
   13694 	  break;
   13695 
   13696 	case R_PPC64_GOT_TLSLD16:
   13697 	case R_PPC64_GOT_TLSLD16_LO:
   13698 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   13699 	    {
   13700 	      unsigned int insn1, insn2, insn3;
   13701 	      bfd_vma offset;
   13702 
   13703 	    tls_ldgd_opt:
   13704 	      offset = (bfd_vma) -1;
   13705 	      /* If not using the newer R_PPC64_TLSGD/LD to mark
   13706 		 __tls_get_addr calls, we must trust that the call
   13707 		 stays with its arg setup insns, ie. that the next
   13708 		 reloc is the __tls_get_addr call associated with
   13709 		 the current reloc.  Edit both insns.  */
   13710 	      if (input_section->has_tls_get_addr_call
   13711 		  && rel + 1 < relend
   13712 		  && branch_reloc_hash_match (input_bfd, rel + 1,
   13713 					      htab->tls_get_addr,
   13714 					      htab->tls_get_addr_fd))
   13715 		offset = rel[1].r_offset;
   13716 	      /* We read the low GOT_TLS (or TOC16) insn because we
   13717 		 need to keep the destination reg.  It may be
   13718 		 something other than the usual r3, and moved to r3
   13719 		 before the call by intervening code.  */
   13720 	      insn1 = bfd_get_32 (output_bfd,
   13721 				  contents + rel->r_offset - d_offset);
   13722 	      if ((tls_mask & tls_gd) != 0)
   13723 		{
   13724 		  /* IE */
   13725 		  insn1 &= (0x1f << 21) | (0x1f << 16);
   13726 		  insn1 |= 58 << 26;	/* ld */
   13727 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   13728 		  if (offset != (bfd_vma) -1)
   13729 		    rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   13730 		  if ((tls_mask & TLS_EXPLICIT) == 0)
   13731 		    r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
   13732 			      + R_PPC64_GOT_TPREL16_DS);
   13733 		  else
   13734 		    r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
   13735 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13736 		}
   13737 	      else
   13738 		{
   13739 		  /* LE */
   13740 		  insn1 &= 0x1f << 21;
   13741 		  insn1 |= 0x3c0d0000;	/* addis r,13,0 */
   13742 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   13743 		  if (tls_gd == 0)
   13744 		    {
   13745 		      /* Was an LD reloc.  */
   13746 		      if (toc_symndx)
   13747 			sec = local_sections[toc_symndx];
   13748 		      for (r_symndx = 0;
   13749 			   r_symndx < symtab_hdr->sh_info;
   13750 			   r_symndx++)
   13751 			if (local_sections[r_symndx] == sec)
   13752 			  break;
   13753 		      if (r_symndx >= symtab_hdr->sh_info)
   13754 			r_symndx = STN_UNDEF;
   13755 		      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   13756 		      if (r_symndx != STN_UNDEF)
   13757 			rel->r_addend -= (local_syms[r_symndx].st_value
   13758 					  + sec->output_offset
   13759 					  + sec->output_section->vma);
   13760 		    }
   13761 		  else if (toc_symndx != 0)
   13762 		    {
   13763 		      r_symndx = toc_symndx;
   13764 		      rel->r_addend = toc_addend;
   13765 		    }
   13766 		  r_type = R_PPC64_TPREL16_HA;
   13767 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13768 		  if (offset != (bfd_vma) -1)
   13769 		    {
   13770 		      rel[1].r_info = ELF64_R_INFO (r_symndx,
   13771 						    R_PPC64_TPREL16_LO);
   13772 		      rel[1].r_offset = offset + d_offset;
   13773 		      rel[1].r_addend = rel->r_addend;
   13774 		    }
   13775 		}
   13776 	      bfd_put_32 (output_bfd, insn1,
   13777 			  contents + rel->r_offset - d_offset);
   13778 	      if (offset != (bfd_vma) -1)
   13779 		{
   13780 		  insn3 = bfd_get_32 (output_bfd,
   13781 				      contents + offset + 4);
   13782 		  if (insn3 == NOP
   13783 		      || insn3 == CROR_151515 || insn3 == CROR_313131)
   13784 		    {
   13785 		      rel[1].r_offset += 4;
   13786 		      bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   13787 		      insn2 = NOP;
   13788 		    }
   13789 		  bfd_put_32 (output_bfd, insn2, contents + offset);
   13790 		}
   13791 	      if ((tls_mask & tls_gd) == 0
   13792 		  && (tls_gd == 0 || toc_symndx != 0))
   13793 		{
   13794 		  /* We changed the symbol.  Start over in order
   13795 		     to get h, sym, sec etc. right.  */
   13796 		  goto again;
   13797 		}
   13798 	    }
   13799 	  break;
   13800 
   13801 	case R_PPC64_TLSGD:
   13802 	  if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
   13803 	    {
   13804 	      unsigned int insn2, insn3;
   13805 	      bfd_vma offset = rel->r_offset;
   13806 
   13807 	      if ((tls_mask & TLS_TPRELGD) != 0)
   13808 		{
   13809 		  /* IE */
   13810 		  r_type = R_PPC64_NONE;
   13811 		  insn2 = 0x7c636a14;	/* add 3,3,13 */
   13812 		}
   13813 	      else
   13814 		{
   13815 		  /* LE */
   13816 		  if (toc_symndx != 0)
   13817 		    {
   13818 		      r_symndx = toc_symndx;
   13819 		      rel->r_addend = toc_addend;
   13820 		    }
   13821 		  r_type = R_PPC64_TPREL16_LO;
   13822 		  rel->r_offset = offset + d_offset;
   13823 		  insn2 = 0x38630000;	/* addi 3,3,0 */
   13824 		}
   13825 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13826 	      /* Zap the reloc on the _tls_get_addr call too.  */
   13827 	      BFD_ASSERT (offset == rel[1].r_offset);
   13828 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   13829 	      insn3 = bfd_get_32 (output_bfd,
   13830 				  contents + offset + 4);
   13831 	      if (insn3 == NOP
   13832 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   13833 		{
   13834 		  rel->r_offset += 4;
   13835 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   13836 		  insn2 = NOP;
   13837 		}
   13838 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   13839 	      if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
   13840 		goto again;
   13841 	    }
   13842 	  break;
   13843 
   13844 	case R_PPC64_TLSLD:
   13845 	  if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
   13846 	    {
   13847 	      unsigned int insn2, insn3;
   13848 	      bfd_vma offset = rel->r_offset;
   13849 
   13850 	      if (toc_symndx)
   13851 		sec = local_sections[toc_symndx];
   13852 	      for (r_symndx = 0;
   13853 		   r_symndx < symtab_hdr->sh_info;
   13854 		   r_symndx++)
   13855 		if (local_sections[r_symndx] == sec)
   13856 		  break;
   13857 	      if (r_symndx >= symtab_hdr->sh_info)
   13858 		r_symndx = STN_UNDEF;
   13859 	      rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
   13860 	      if (r_symndx != STN_UNDEF)
   13861 		rel->r_addend -= (local_syms[r_symndx].st_value
   13862 				  + sec->output_offset
   13863 				  + sec->output_section->vma);
   13864 
   13865 	      r_type = R_PPC64_TPREL16_LO;
   13866 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13867 	      rel->r_offset = offset + d_offset;
   13868 	      /* Zap the reloc on the _tls_get_addr call too.  */
   13869 	      BFD_ASSERT (offset == rel[1].r_offset);
   13870 	      rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
   13871 	      insn2 = 0x38630000;	/* addi 3,3,0 */
   13872 	      insn3 = bfd_get_32 (output_bfd,
   13873 				  contents + offset + 4);
   13874 	      if (insn3 == NOP
   13875 		  || insn3 == CROR_151515 || insn3 == CROR_313131)
   13876 		{
   13877 		  rel->r_offset += 4;
   13878 		  bfd_put_32 (output_bfd, insn2, contents + offset + 4);
   13879 		  insn2 = NOP;
   13880 		}
   13881 	      bfd_put_32 (output_bfd, insn2, contents + offset);
   13882 	      goto again;
   13883 	    }
   13884 	  break;
   13885 
   13886 	case R_PPC64_DTPMOD64:
   13887 	  if (rel + 1 < relend
   13888 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
   13889 	      && rel[1].r_offset == rel->r_offset + 8)
   13890 	    {
   13891 	      if ((tls_mask & TLS_GD) == 0)
   13892 		{
   13893 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
   13894 		  if ((tls_mask & TLS_TPRELGD) != 0)
   13895 		    r_type = R_PPC64_TPREL64;
   13896 		  else
   13897 		    {
   13898 		      bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   13899 		      r_type = R_PPC64_NONE;
   13900 		    }
   13901 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13902 		}
   13903 	    }
   13904 	  else
   13905 	    {
   13906 	      if ((tls_mask & TLS_LD) == 0)
   13907 		{
   13908 		  bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
   13909 		  r_type = R_PPC64_NONE;
   13910 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13911 		}
   13912 	    }
   13913 	  break;
   13914 
   13915 	case R_PPC64_TPREL64:
   13916 	  if ((tls_mask & TLS_TPREL) == 0)
   13917 	    {
   13918 	      r_type = R_PPC64_NONE;
   13919 	      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13920 	    }
   13921 	  break;
   13922 
   13923 	case R_PPC64_ENTRY:
   13924 	  relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
   13925 	  if (!bfd_link_pic (info)
   13926 	      && !info->traditional_format
   13927 	      && relocation + 0x80008000 <= 0xffffffff)
   13928 	    {
   13929 	      unsigned int insn1, insn2;
   13930 
   13931 	      insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
   13932 	      insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   13933 	      if ((insn1 & ~0xfffc) == LD_R2_0R12
   13934 		  && insn2 == ADD_R2_R2_R12)
   13935 		{
   13936 		  bfd_put_32 (output_bfd,
   13937 			      LIS_R2 + PPC_HA (relocation),
   13938 			      contents + rel->r_offset);
   13939 		  bfd_put_32 (output_bfd,
   13940 			      ADDI_R2_R2 + PPC_LO (relocation),
   13941 			      contents + rel->r_offset + 4);
   13942 		}
   13943 	    }
   13944 	  else
   13945 	    {
   13946 	      relocation -= (rel->r_offset
   13947 			     + input_section->output_offset
   13948 			     + input_section->output_section->vma);
   13949 	      if (relocation + 0x80008000 <= 0xffffffff)
   13950 		{
   13951 		  unsigned int insn1, insn2;
   13952 
   13953 		  insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
   13954 		  insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
   13955 		  if ((insn1 & ~0xfffc) == LD_R2_0R12
   13956 		      && insn2 == ADD_R2_R2_R12)
   13957 		    {
   13958 		      bfd_put_32 (output_bfd,
   13959 				  ADDIS_R2_R12 + PPC_HA (relocation),
   13960 				  contents + rel->r_offset);
   13961 		      bfd_put_32 (output_bfd,
   13962 				  ADDI_R2_R2 + PPC_LO (relocation),
   13963 				  contents + rel->r_offset + 4);
   13964 		    }
   13965 		}
   13966 	    }
   13967 	  break;
   13968 
   13969 	case R_PPC64_REL16_HA:
   13970 	  /* If we are generating a non-PIC executable, edit
   13971 	     .	0:	addis 2,12,.TOC.-0b@ha
   13972 	     .		addi 2,2,.TOC.-0b@l
   13973 	     used by ELFv2 global entry points to set up r2, to
   13974 	     .		lis 2,.TOC.@ha
   13975 	     .		addi 2,2,.TOC.@l
   13976 	     if .TOC. is in range.  */
   13977 	  if (!bfd_link_pic (info)
   13978 	      && !info->traditional_format
   13979 	      && !htab->opd_abi
   13980 	      && rel->r_addend == d_offset
   13981 	      && h != NULL && &h->elf == htab->elf.hgot
   13982 	      && rel + 1 < relend
   13983 	      && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
   13984 	      && rel[1].r_offset == rel->r_offset + 4
   13985 	      && rel[1].r_addend == rel->r_addend + 4
   13986 	      && relocation + 0x80008000 <= 0xffffffff)
   13987 	    {
   13988 	      unsigned int insn1, insn2;
   13989 	      bfd_vma offset = rel->r_offset - d_offset;
   13990 	      insn1 = bfd_get_32 (output_bfd, contents + offset);
   13991 	      insn2 = bfd_get_32 (output_bfd, contents + offset + 4);
   13992 	      if ((insn1 & 0xffff0000) == ADDIS_R2_R12
   13993 		  && (insn2 & 0xffff0000) == ADDI_R2_R2)
   13994 		{
   13995 		  r_type = R_PPC64_ADDR16_HA;
   13996 		  rel->r_info = ELF64_R_INFO (r_symndx, r_type);
   13997 		  rel->r_addend -= d_offset;
   13998 		  rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
   13999 		  rel[1].r_addend -= d_offset + 4;
   14000 		  bfd_put_32 (output_bfd, LIS_R2, contents + offset);
   14001 		}
   14002 	    }
   14003 	  break;
   14004 	}
   14005 
   14006       /* Handle other relocations that tweak non-addend part of insn.  */
   14007       insn = 0;
   14008       max_br_offset = 1 << 25;
   14009       addend = rel->r_addend;
   14010       reloc_dest = DEST_NORMAL;
   14011       switch (r_type)
   14012 	{
   14013 	default:
   14014 	  break;
   14015 
   14016 	case R_PPC64_TOCSAVE:
   14017 	  if (relocation + addend == (rel->r_offset
   14018 				      + input_section->output_offset
   14019 				      + input_section->output_section->vma)
   14020 	      && tocsave_find (htab, NO_INSERT,
   14021 			       &local_syms, rel, input_bfd))
   14022 	    {
   14023 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   14024 	      if (insn == NOP
   14025 		  || insn == CROR_151515 || insn == CROR_313131)
   14026 		bfd_put_32 (input_bfd,
   14027 			    STD_R2_0R1 + STK_TOC (htab),
   14028 			    contents + rel->r_offset);
   14029 	    }
   14030 	  break;
   14031 
   14032 	  /* Branch taken prediction relocations.  */
   14033 	case R_PPC64_ADDR14_BRTAKEN:
   14034 	case R_PPC64_REL14_BRTAKEN:
   14035 	  insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field.  */
   14036 	  /* Fall thru.  */
   14037 
   14038 	  /* Branch not taken prediction relocations.  */
   14039 	case R_PPC64_ADDR14_BRNTAKEN:
   14040 	case R_PPC64_REL14_BRNTAKEN:
   14041 	  insn |= bfd_get_32 (output_bfd,
   14042 			      contents + rel->r_offset) & ~(0x01 << 21);
   14043 	  /* Fall thru.  */
   14044 
   14045 	case R_PPC64_REL14:
   14046 	  max_br_offset = 1 << 15;
   14047 	  /* Fall thru.  */
   14048 
   14049 	case R_PPC64_REL24:
   14050 	  /* Calls to functions with a different TOC, such as calls to
   14051 	     shared objects, need to alter the TOC pointer.  This is
   14052 	     done using a linkage stub.  A REL24 branching to these
   14053 	     linkage stubs needs to be followed by a nop, as the nop
   14054 	     will be replaced with an instruction to restore the TOC
   14055 	     base pointer.  */
   14056 	  fdh = h;
   14057 	  if (h != NULL
   14058 	      && h->oh != NULL
   14059 	      && h->oh->is_func_descriptor)
   14060 	    fdh = ppc_follow_link (h->oh);
   14061 	  stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
   14062 					   htab);
   14063 	  if (stub_entry != NULL
   14064 	      && (stub_entry->stub_type == ppc_stub_plt_call
   14065 		  || stub_entry->stub_type == ppc_stub_plt_call_r2save
   14066 		  || stub_entry->stub_type == ppc_stub_plt_branch_r2off
   14067 		  || stub_entry->stub_type == ppc_stub_long_branch_r2off))
   14068 	    {
   14069 	      bfd_boolean can_plt_call = FALSE;
   14070 
   14071 	      /* All of these stubs will modify r2, so there must be a
   14072 		 branch and link followed by a nop.  The nop is
   14073 		 replaced by an insn to restore r2.  */
   14074 	      if (rel->r_offset + 8 <= input_section->size)
   14075 		{
   14076 		  unsigned long br;
   14077 
   14078 		  br = bfd_get_32 (input_bfd,
   14079 				   contents + rel->r_offset);
   14080 		  if ((br & 1) != 0)
   14081 		    {
   14082 		      unsigned long nop;
   14083 
   14084 		      nop = bfd_get_32 (input_bfd,
   14085 					contents + rel->r_offset + 4);
   14086 		      if (nop == NOP
   14087 			  || nop == CROR_151515 || nop == CROR_313131)
   14088 			{
   14089 			  if (h != NULL
   14090 			      && (h == htab->tls_get_addr_fd
   14091 				  || h == htab->tls_get_addr)
   14092 			      && htab->params->tls_get_addr_opt)
   14093 			    {
   14094 			      /* Special stub used, leave nop alone.  */
   14095 			    }
   14096 			  else
   14097 			    bfd_put_32 (input_bfd,
   14098 					LD_R2_0R1 + STK_TOC (htab),
   14099 					contents + rel->r_offset + 4);
   14100 			  can_plt_call = TRUE;
   14101 			}
   14102 		    }
   14103 		}
   14104 
   14105 	      if (!can_plt_call && h != NULL)
   14106 		{
   14107 		  const char *name = h->elf.root.root.string;
   14108 
   14109 		  if (*name == '.')
   14110 		    ++name;
   14111 
   14112 		  if (strncmp (name, "__libc_start_main", 17) == 0
   14113 		      && (name[17] == 0 || name[17] == '@'))
   14114 		    {
   14115 		      /* Allow crt1 branch to go via a toc adjusting
   14116 			 stub.  Other calls that never return could do
   14117 			 the same, if we could detect such.  */
   14118 		      can_plt_call = TRUE;
   14119 		    }
   14120 		}
   14121 
   14122 	      if (!can_plt_call)
   14123 		{
   14124 		  /* g++ as of 20130507 emits self-calls without a
   14125 		     following nop.  This is arguably wrong since we
   14126 		     have conflicting information.  On the one hand a
   14127 		     global symbol and on the other a local call
   14128 		     sequence, but don't error for this special case.
   14129 		     It isn't possible to cheaply verify we have
   14130 		     exactly such a call.  Allow all calls to the same
   14131 		     section.  */
   14132 		  asection *code_sec = sec;
   14133 
   14134 		  if (get_opd_info (sec) != NULL)
   14135 		    {
   14136 		      bfd_vma off = (relocation + addend
   14137 				     - sec->output_section->vma
   14138 				     - sec->output_offset);
   14139 
   14140 		      opd_entry_value (sec, off, &code_sec, NULL, FALSE);
   14141 		    }
   14142 		  if (code_sec == input_section)
   14143 		    can_plt_call = TRUE;
   14144 		}
   14145 
   14146 	      if (!can_plt_call)
   14147 		{
   14148 		  if (stub_entry->stub_type == ppc_stub_plt_call
   14149 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   14150 		    info->callbacks->einfo
   14151 		      (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
   14152 			 "recompile with -fPIC\n"),
   14153 		       input_bfd, input_section, rel->r_offset, sym_name);
   14154 		  else
   14155 		    info->callbacks->einfo
   14156 		      (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
   14157 			 "(-mcmodel=small toc adjust stub)\n"),
   14158 		       input_bfd, input_section, rel->r_offset, sym_name);
   14159 
   14160 		  bfd_set_error (bfd_error_bad_value);
   14161 		  ret = FALSE;
   14162 		}
   14163 
   14164 	      if (can_plt_call
   14165 		  && (stub_entry->stub_type == ppc_stub_plt_call
   14166 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save))
   14167 		unresolved_reloc = FALSE;
   14168 	    }
   14169 
   14170 	  if ((stub_entry == NULL
   14171 	       || stub_entry->stub_type == ppc_stub_long_branch
   14172 	       || stub_entry->stub_type == ppc_stub_plt_branch)
   14173 	      && get_opd_info (sec) != NULL)
   14174 	    {
   14175 	      /* The branch destination is the value of the opd entry. */
   14176 	      bfd_vma off = (relocation + addend
   14177 			     - sec->output_section->vma
   14178 			     - sec->output_offset);
   14179 	      bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
   14180 	      if (dest != (bfd_vma) -1)
   14181 		{
   14182 		  relocation = dest;
   14183 		  addend = 0;
   14184 		  reloc_dest = DEST_OPD;
   14185 		}
   14186 	    }
   14187 
   14188 	  /* If the branch is out of reach we ought to have a long
   14189 	     branch stub.  */
   14190 	  from = (rel->r_offset
   14191 		  + input_section->output_offset
   14192 		  + input_section->output_section->vma);
   14193 
   14194 	  relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
   14195 						  ? fdh->elf.other
   14196 						  : sym->st_other);
   14197 
   14198 	  if (stub_entry != NULL
   14199 	      && (stub_entry->stub_type == ppc_stub_long_branch
   14200 		  || stub_entry->stub_type == ppc_stub_plt_branch)
   14201 	      && (r_type == R_PPC64_ADDR14_BRTAKEN
   14202 		  || r_type == R_PPC64_ADDR14_BRNTAKEN
   14203 		  || (relocation + addend - from + max_br_offset
   14204 		      < 2 * max_br_offset)))
   14205 	    /* Don't use the stub if this branch is in range.  */
   14206 	    stub_entry = NULL;
   14207 
   14208 	  if (stub_entry != NULL)
   14209 	    {
   14210 	      /* Munge up the value and addend so that we call the stub
   14211 		 rather than the procedure directly.  */
   14212 	      asection *stub_sec = stub_entry->group->stub_sec;
   14213 
   14214 	      if (stub_entry->stub_type == ppc_stub_save_res)
   14215 		relocation += (stub_sec->output_offset
   14216 			       + stub_sec->output_section->vma
   14217 			       + stub_sec->size - htab->sfpr->size
   14218 			       - htab->sfpr->output_offset
   14219 			       - htab->sfpr->output_section->vma);
   14220 	      else
   14221 		relocation = (stub_entry->stub_offset
   14222 			      + stub_sec->output_offset
   14223 			      + stub_sec->output_section->vma);
   14224 	      addend = 0;
   14225 	      reloc_dest = DEST_STUB;
   14226 
   14227  	      if ((stub_entry->stub_type == ppc_stub_plt_call
   14228 		   || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   14229 		  && (ALWAYS_EMIT_R2SAVE
   14230 		      || stub_entry->stub_type == ppc_stub_plt_call_r2save)
   14231 		  && rel + 1 < relend
   14232 		  && rel[1].r_offset == rel->r_offset + 4
   14233 		  && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
   14234 		relocation += 4;
   14235 	    }
   14236 
   14237 	  if (insn != 0)
   14238 	    {
   14239 	      if (is_isa_v2)
   14240 		{
   14241 		  /* Set 'a' bit.  This is 0b00010 in BO field for branch
   14242 		     on CR(BI) insns (BO == 001at or 011at), and 0b01000
   14243 		     for branch on CTR insns (BO == 1a00t or 1a01t).  */
   14244 		  if ((insn & (0x14 << 21)) == (0x04 << 21))
   14245 		    insn |= 0x02 << 21;
   14246 		  else if ((insn & (0x14 << 21)) == (0x10 << 21))
   14247 		    insn |= 0x08 << 21;
   14248 		  else
   14249 		    break;
   14250 		}
   14251 	      else
   14252 		{
   14253 		  /* Invert 'y' bit if not the default.  */
   14254 		  if ((bfd_signed_vma) (relocation + addend - from) < 0)
   14255 		    insn ^= 0x01 << 21;
   14256 		}
   14257 
   14258 	      bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
   14259 	    }
   14260 
   14261 	  /* NOP out calls to undefined weak functions.
   14262 	     We can thus call a weak function without first
   14263 	     checking whether the function is defined.  */
   14264 	  else if (h != NULL
   14265 		   && h->elf.root.type == bfd_link_hash_undefweak
   14266 		   && h->elf.dynindx == -1
   14267 		   && r_type == R_PPC64_REL24
   14268 		   && relocation == 0
   14269 		   && addend == 0)
   14270 	    {
   14271 	      bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
   14272 	      goto copy_reloc;
   14273 	    }
   14274 	  break;
   14275 	}
   14276 
   14277       /* Set `addend'.  */
   14278       tls_type = 0;
   14279       switch (r_type)
   14280 	{
   14281 	default:
   14282 	  info->callbacks->einfo
   14283 	    (_("%P: %B: unknown relocation type %d for `%T'\n"),
   14284 	     input_bfd, (int) r_type, sym_name);
   14285 
   14286 	  bfd_set_error (bfd_error_bad_value);
   14287 	  ret = FALSE;
   14288 	  goto copy_reloc;
   14289 
   14290 	case R_PPC64_NONE:
   14291 	case R_PPC64_TLS:
   14292 	case R_PPC64_TLSGD:
   14293 	case R_PPC64_TLSLD:
   14294 	case R_PPC64_TOCSAVE:
   14295 	case R_PPC64_GNU_VTINHERIT:
   14296 	case R_PPC64_GNU_VTENTRY:
   14297 	case R_PPC64_ENTRY:
   14298 	  goto copy_reloc;
   14299 
   14300 	  /* GOT16 relocations.  Like an ADDR16 using the symbol's
   14301 	     address in the GOT as relocation value instead of the
   14302 	     symbol's value itself.  Also, create a GOT entry for the
   14303 	     symbol and put the symbol value there.  */
   14304 	case R_PPC64_GOT_TLSGD16:
   14305 	case R_PPC64_GOT_TLSGD16_LO:
   14306 	case R_PPC64_GOT_TLSGD16_HI:
   14307 	case R_PPC64_GOT_TLSGD16_HA:
   14308 	  tls_type = TLS_TLS | TLS_GD;
   14309 	  goto dogot;
   14310 
   14311 	case R_PPC64_GOT_TLSLD16:
   14312 	case R_PPC64_GOT_TLSLD16_LO:
   14313 	case R_PPC64_GOT_TLSLD16_HI:
   14314 	case R_PPC64_GOT_TLSLD16_HA:
   14315 	  tls_type = TLS_TLS | TLS_LD;
   14316 	  goto dogot;
   14317 
   14318 	case R_PPC64_GOT_TPREL16_DS:
   14319 	case R_PPC64_GOT_TPREL16_LO_DS:
   14320 	case R_PPC64_GOT_TPREL16_HI:
   14321 	case R_PPC64_GOT_TPREL16_HA:
   14322 	  tls_type = TLS_TLS | TLS_TPREL;
   14323 	  goto dogot;
   14324 
   14325 	case R_PPC64_GOT_DTPREL16_DS:
   14326 	case R_PPC64_GOT_DTPREL16_LO_DS:
   14327 	case R_PPC64_GOT_DTPREL16_HI:
   14328 	case R_PPC64_GOT_DTPREL16_HA:
   14329 	  tls_type = TLS_TLS | TLS_DTPREL;
   14330 	  goto dogot;
   14331 
   14332 	case R_PPC64_GOT16:
   14333 	case R_PPC64_GOT16_LO:
   14334 	case R_PPC64_GOT16_HI:
   14335 	case R_PPC64_GOT16_HA:
   14336 	case R_PPC64_GOT16_DS:
   14337 	case R_PPC64_GOT16_LO_DS:
   14338 	dogot:
   14339 	  {
   14340 	    /* Relocation is to the entry for this symbol in the global
   14341 	       offset table.  */
   14342 	    asection *got;
   14343 	    bfd_vma *offp;
   14344 	    bfd_vma off;
   14345 	    unsigned long indx = 0;
   14346 	    struct got_entry *ent;
   14347 
   14348 	    if (tls_type == (TLS_TLS | TLS_LD)
   14349 		&& (h == NULL
   14350 		    || !h->elf.def_dynamic))
   14351 	      ent = ppc64_tlsld_got (input_bfd);
   14352 	    else
   14353 	      {
   14354 
   14355 		if (h != NULL)
   14356 		  {
   14357 		    bfd_boolean dyn = htab->elf.dynamic_sections_created;
   14358 		    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
   14359 							  &h->elf)
   14360 			|| (bfd_link_pic (info)
   14361 			    && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
   14362 		      /* This is actually a static link, or it is a
   14363 			 -Bsymbolic link and the symbol is defined
   14364 			 locally, or the symbol was forced to be local
   14365 			 because of a version file.  */
   14366 		      ;
   14367 		    else
   14368 		      {
   14369 			BFD_ASSERT (h->elf.dynindx != -1);
   14370 			indx = h->elf.dynindx;
   14371 			unresolved_reloc = FALSE;
   14372 		      }
   14373 		    ent = h->elf.got.glist;
   14374 		  }
   14375 		else
   14376 		  {
   14377 		    if (local_got_ents == NULL)
   14378 		      abort ();
   14379 		    ent = local_got_ents[r_symndx];
   14380 		  }
   14381 
   14382 		for (; ent != NULL; ent = ent->next)
   14383 		  if (ent->addend == orig_rel.r_addend
   14384 		      && ent->owner == input_bfd
   14385 		      && ent->tls_type == tls_type)
   14386 		    break;
   14387 	      }
   14388 
   14389 	    if (ent == NULL)
   14390 	      abort ();
   14391 	    if (ent->is_indirect)
   14392 	      ent = ent->got.ent;
   14393 	    offp = &ent->got.offset;
   14394 	    got = ppc64_elf_tdata (ent->owner)->got;
   14395 	    if (got == NULL)
   14396 	      abort ();
   14397 
   14398 	    /* The offset must always be a multiple of 8.  We use the
   14399 	       least significant bit to record whether we have already
   14400 	       processed this entry.  */
   14401 	    off = *offp;
   14402 	    if ((off & 1) != 0)
   14403 	      off &= ~1;
   14404 	    else
   14405 	      {
   14406 		/* Generate relocs for the dynamic linker, except in
   14407 		   the case of TLSLD where we'll use one entry per
   14408 		   module.  */
   14409 		asection *relgot;
   14410 		bfd_boolean ifunc;
   14411 
   14412 		*offp = off | 1;
   14413 		relgot = NULL;
   14414 		ifunc = (h != NULL
   14415 			 ? h->elf.type == STT_GNU_IFUNC
   14416 			 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
   14417 		if (ifunc)
   14418 		  relgot = htab->elf.irelplt;
   14419 		else if ((bfd_link_pic (info) || indx != 0)
   14420 			 && (h == NULL
   14421 			     || (tls_type == (TLS_TLS | TLS_LD)
   14422 				 && !h->elf.def_dynamic)
   14423 			     || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   14424 			     || h->elf.root.type != bfd_link_hash_undefweak))
   14425 		  relgot = ppc64_elf_tdata (ent->owner)->relgot;
   14426 		if (relgot != NULL)
   14427 		  {
   14428 		    outrel.r_offset = (got->output_section->vma
   14429 				       + got->output_offset
   14430 				       + off);
   14431 		    outrel.r_addend = addend;
   14432 		    if (tls_type & (TLS_LD | TLS_GD))
   14433 		      {
   14434 			outrel.r_addend = 0;
   14435 			outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
   14436 			if (tls_type == (TLS_TLS | TLS_GD))
   14437 			  {
   14438 			    loc = relgot->contents;
   14439 			    loc += (relgot->reloc_count++
   14440 				    * sizeof (Elf64_External_Rela));
   14441 			    bfd_elf64_swap_reloca_out (output_bfd,
   14442 						       &outrel, loc);
   14443 			    outrel.r_offset += 8;
   14444 			    outrel.r_addend = addend;
   14445 			    outrel.r_info
   14446 			      = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   14447 			  }
   14448 		      }
   14449 		    else if (tls_type == (TLS_TLS | TLS_DTPREL))
   14450 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
   14451 		    else if (tls_type == (TLS_TLS | TLS_TPREL))
   14452 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
   14453 		    else if (indx != 0)
   14454 		      outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
   14455 		    else
   14456 		      {
   14457 			if (ifunc)
   14458 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   14459 			else
   14460 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   14461 
   14462 			/* Write the .got section contents for the sake
   14463 			   of prelink.  */
   14464 			loc = got->contents + off;
   14465 			bfd_put_64 (output_bfd, outrel.r_addend + relocation,
   14466 				    loc);
   14467 		      }
   14468 
   14469 		    if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
   14470 		      {
   14471 			outrel.r_addend += relocation;
   14472 			if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
   14473 			  {
   14474 			    if (htab->elf.tls_sec == NULL)
   14475 			      outrel.r_addend = 0;
   14476 			    else
   14477 			      outrel.r_addend -= htab->elf.tls_sec->vma;
   14478 			  }
   14479 		      }
   14480 		    loc = relgot->contents;
   14481 		    loc += (relgot->reloc_count++
   14482 			    * sizeof (Elf64_External_Rela));
   14483 		    bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   14484 		  }
   14485 
   14486 		/* Init the .got section contents here if we're not
   14487 		   emitting a reloc.  */
   14488 		else
   14489 		  {
   14490 		    relocation += addend;
   14491 		    if (tls_type == (TLS_TLS | TLS_LD))
   14492 		      relocation = 1;
   14493 		    else if (tls_type != 0)
   14494 		      {
   14495 			if (htab->elf.tls_sec == NULL)
   14496 			  relocation = 0;
   14497 			else
   14498 			  {
   14499 			    relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
   14500 			    if (tls_type == (TLS_TLS | TLS_TPREL))
   14501 			      relocation += DTP_OFFSET - TP_OFFSET;
   14502 			  }
   14503 
   14504 			if (tls_type == (TLS_TLS | TLS_GD))
   14505 			  {
   14506 			    bfd_put_64 (output_bfd, relocation,
   14507 					got->contents + off + 8);
   14508 			    relocation = 1;
   14509 			  }
   14510 		      }
   14511 
   14512 		    bfd_put_64 (output_bfd, relocation,
   14513 				got->contents + off);
   14514 		  }
   14515 	      }
   14516 
   14517 	    if (off >= (bfd_vma) -2)
   14518 	      abort ();
   14519 
   14520 	    relocation = got->output_section->vma + got->output_offset + off;
   14521 	    addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
   14522 	  }
   14523 	  break;
   14524 
   14525 	case R_PPC64_PLT16_HA:
   14526 	case R_PPC64_PLT16_HI:
   14527 	case R_PPC64_PLT16_LO:
   14528 	case R_PPC64_PLT32:
   14529 	case R_PPC64_PLT64:
   14530 	  /* Relocation is to the entry for this symbol in the
   14531 	     procedure linkage table.  */
   14532 	  {
   14533 	    struct plt_entry **plt_list = NULL;
   14534 	    if (h != NULL)
   14535 	      plt_list = &h->elf.plt.plist;
   14536 	    else if (local_got_ents != NULL)
   14537 	      {
   14538 		struct plt_entry **local_plt = (struct plt_entry **)
   14539 		  (local_got_ents + symtab_hdr->sh_info);
   14540 		unsigned char *local_got_tls_masks = (unsigned char *)
   14541 		  (local_plt + symtab_hdr->sh_info);
   14542 		if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
   14543 		  plt_list = local_plt + r_symndx;
   14544 	      }
   14545 	    if (plt_list)
   14546 	      {
   14547 		struct plt_entry *ent;
   14548 
   14549 		for (ent = *plt_list; ent != NULL; ent = ent->next)
   14550 		  if (ent->plt.offset != (bfd_vma) -1
   14551 		      && ent->addend == orig_rel.r_addend)
   14552 		    {
   14553 		      asection *plt;
   14554 
   14555 		      plt = htab->elf.splt;
   14556 		      if (!htab->elf.dynamic_sections_created
   14557 			  || h == NULL
   14558 			  || h->elf.dynindx == -1)
   14559 			plt = htab->elf.iplt;
   14560 		      relocation = (plt->output_section->vma
   14561 				    + plt->output_offset
   14562 				    + ent->plt.offset);
   14563 		      addend = 0;
   14564 		      unresolved_reloc = FALSE;
   14565 		      break;
   14566 		    }
   14567 	      }
   14568 	  }
   14569 	  break;
   14570 
   14571 	case R_PPC64_TOC:
   14572 	  /* Relocation value is TOC base.  */
   14573 	  relocation = TOCstart;
   14574 	  if (r_symndx == STN_UNDEF)
   14575 	    relocation += htab->sec_info[input_section->id].toc_off;
   14576 	  else if (unresolved_reloc)
   14577 	    ;
   14578 	  else if (sec != NULL && sec->id < htab->sec_info_arr_size)
   14579 	    relocation += htab->sec_info[sec->id].toc_off;
   14580 	  else
   14581 	    unresolved_reloc = TRUE;
   14582 	  goto dodyn;
   14583 
   14584 	  /* TOC16 relocs.  We want the offset relative to the TOC base,
   14585 	     which is the address of the start of the TOC plus 0x8000.
   14586 	     The TOC consists of sections .got, .toc, .tocbss, and .plt,
   14587 	     in this order.  */
   14588 	case R_PPC64_TOC16:
   14589 	case R_PPC64_TOC16_LO:
   14590 	case R_PPC64_TOC16_HI:
   14591 	case R_PPC64_TOC16_DS:
   14592 	case R_PPC64_TOC16_LO_DS:
   14593 	case R_PPC64_TOC16_HA:
   14594 	  addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
   14595 	  break;
   14596 
   14597 	  /* Relocate against the beginning of the section.  */
   14598 	case R_PPC64_SECTOFF:
   14599 	case R_PPC64_SECTOFF_LO:
   14600 	case R_PPC64_SECTOFF_HI:
   14601 	case R_PPC64_SECTOFF_DS:
   14602 	case R_PPC64_SECTOFF_LO_DS:
   14603 	case R_PPC64_SECTOFF_HA:
   14604 	  if (sec != NULL)
   14605 	    addend -= sec->output_section->vma;
   14606 	  break;
   14607 
   14608 	case R_PPC64_REL16:
   14609 	case R_PPC64_REL16_LO:
   14610 	case R_PPC64_REL16_HI:
   14611 	case R_PPC64_REL16_HA:
   14612 	case R_PPC64_REL16DX_HA:
   14613 	  break;
   14614 
   14615 	case R_PPC64_REL14:
   14616 	case R_PPC64_REL14_BRNTAKEN:
   14617 	case R_PPC64_REL14_BRTAKEN:
   14618 	case R_PPC64_REL24:
   14619 	  break;
   14620 
   14621 	case R_PPC64_TPREL16:
   14622 	case R_PPC64_TPREL16_LO:
   14623 	case R_PPC64_TPREL16_HI:
   14624 	case R_PPC64_TPREL16_HA:
   14625 	case R_PPC64_TPREL16_DS:
   14626 	case R_PPC64_TPREL16_LO_DS:
   14627 	case R_PPC64_TPREL16_HIGH:
   14628 	case R_PPC64_TPREL16_HIGHA:
   14629 	case R_PPC64_TPREL16_HIGHER:
   14630 	case R_PPC64_TPREL16_HIGHERA:
   14631 	case R_PPC64_TPREL16_HIGHEST:
   14632 	case R_PPC64_TPREL16_HIGHESTA:
   14633 	  if (h != NULL
   14634 	      && h->elf.root.type == bfd_link_hash_undefweak
   14635 	      && h->elf.dynindx == -1)
   14636 	    {
   14637 	      /* Make this relocation against an undefined weak symbol
   14638 		 resolve to zero.  This is really just a tweak, since
   14639 		 code using weak externs ought to check that they are
   14640 		 defined before using them.  */
   14641 	      bfd_byte *p = contents + rel->r_offset - d_offset;
   14642 
   14643 	      insn = bfd_get_32 (output_bfd, p);
   14644 	      insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
   14645 	      if (insn != 0)
   14646 		bfd_put_32 (output_bfd, insn, p);
   14647 	      break;
   14648 	    }
   14649 	  if (htab->elf.tls_sec != NULL)
   14650 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   14651 	  if (bfd_link_pic (info))
   14652 	    /* The TPREL16 relocs shouldn't really be used in shared
   14653 	       libs as they will result in DT_TEXTREL being set, but
   14654 	       support them anyway.  */
   14655 	    goto dodyn;
   14656 	  break;
   14657 
   14658 	case R_PPC64_DTPREL16:
   14659 	case R_PPC64_DTPREL16_LO:
   14660 	case R_PPC64_DTPREL16_HI:
   14661 	case R_PPC64_DTPREL16_HA:
   14662 	case R_PPC64_DTPREL16_DS:
   14663 	case R_PPC64_DTPREL16_LO_DS:
   14664 	case R_PPC64_DTPREL16_HIGH:
   14665 	case R_PPC64_DTPREL16_HIGHA:
   14666 	case R_PPC64_DTPREL16_HIGHER:
   14667 	case R_PPC64_DTPREL16_HIGHERA:
   14668 	case R_PPC64_DTPREL16_HIGHEST:
   14669 	case R_PPC64_DTPREL16_HIGHESTA:
   14670 	  if (htab->elf.tls_sec != NULL)
   14671 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   14672 	  break;
   14673 
   14674 	case R_PPC64_ADDR64_LOCAL:
   14675 	  addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
   14676 					      ? h->elf.other
   14677 					      : sym->st_other);
   14678 	  break;
   14679 
   14680 	case R_PPC64_DTPMOD64:
   14681 	  relocation = 1;
   14682 	  addend = 0;
   14683 	  goto dodyn;
   14684 
   14685 	case R_PPC64_TPREL64:
   14686 	  if (htab->elf.tls_sec != NULL)
   14687 	    addend -= htab->elf.tls_sec->vma + TP_OFFSET;
   14688 	  goto dodyn;
   14689 
   14690 	case R_PPC64_DTPREL64:
   14691 	  if (htab->elf.tls_sec != NULL)
   14692 	    addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
   14693 	  /* Fall thru */
   14694 
   14695 	  /* Relocations that may need to be propagated if this is a
   14696 	     dynamic object.  */
   14697 	case R_PPC64_REL30:
   14698 	case R_PPC64_REL32:
   14699 	case R_PPC64_REL64:
   14700 	case R_PPC64_ADDR14:
   14701 	case R_PPC64_ADDR14_BRNTAKEN:
   14702 	case R_PPC64_ADDR14_BRTAKEN:
   14703 	case R_PPC64_ADDR16:
   14704 	case R_PPC64_ADDR16_DS:
   14705 	case R_PPC64_ADDR16_HA:
   14706 	case R_PPC64_ADDR16_HI:
   14707 	case R_PPC64_ADDR16_HIGH:
   14708 	case R_PPC64_ADDR16_HIGHA:
   14709 	case R_PPC64_ADDR16_HIGHER:
   14710 	case R_PPC64_ADDR16_HIGHERA:
   14711 	case R_PPC64_ADDR16_HIGHEST:
   14712 	case R_PPC64_ADDR16_HIGHESTA:
   14713 	case R_PPC64_ADDR16_LO:
   14714 	case R_PPC64_ADDR16_LO_DS:
   14715 	case R_PPC64_ADDR24:
   14716 	case R_PPC64_ADDR32:
   14717 	case R_PPC64_ADDR64:
   14718 	case R_PPC64_UADDR16:
   14719 	case R_PPC64_UADDR32:
   14720 	case R_PPC64_UADDR64:
   14721 	dodyn:
   14722 	  if ((input_section->flags & SEC_ALLOC) == 0)
   14723 	    break;
   14724 
   14725 	  if (NO_OPD_RELOCS && is_opd)
   14726 	    break;
   14727 
   14728 	  if (bfd_link_pic (info)
   14729 	      ? ((h == NULL
   14730 		  || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
   14731 		  || h->elf.root.type != bfd_link_hash_undefweak)
   14732 		 && (must_be_dyn_reloc (info, r_type)
   14733 		     || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
   14734 	      : (h == NULL
   14735 		 ? ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
   14736 		 : (h->elf.type == STT_GNU_IFUNC
   14737 		    ? (abiversion (output_bfd) >= 2
   14738 		       ? !(h->elf.pointer_equality_needed
   14739 			   && !h->elf.def_regular
   14740 			   && h->elf.root.type == bfd_link_hash_defined
   14741 			   && h->elf.root.u.def.section == htab->glink)
   14742 		       : !h->elf.needs_copy)
   14743 		    : (ELIMINATE_COPY_RELOCS
   14744 		       && !(h->elf.non_got_ref
   14745 			    || h->elf.def_regular
   14746 			    || h->elf.dynindx == -1)))))
   14747 	    {
   14748 	      bfd_boolean skip, relocate;
   14749 	      asection *sreloc;
   14750 	      bfd_vma out_off;
   14751 
   14752 	      /* When generating a dynamic object, these relocations
   14753 		 are copied into the output file to be resolved at run
   14754 		 time.  */
   14755 
   14756 	      skip = FALSE;
   14757 	      relocate = FALSE;
   14758 
   14759 	      out_off = _bfd_elf_section_offset (output_bfd, info,
   14760 						 input_section, rel->r_offset);
   14761 	      if (out_off == (bfd_vma) -1)
   14762 		skip = TRUE;
   14763 	      else if (out_off == (bfd_vma) -2)
   14764 		skip = TRUE, relocate = TRUE;
   14765 	      out_off += (input_section->output_section->vma
   14766 			  + input_section->output_offset);
   14767 	      outrel.r_offset = out_off;
   14768 	      outrel.r_addend = rel->r_addend;
   14769 
   14770 	      /* Optimize unaligned reloc use.  */
   14771 	      if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
   14772 		  || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
   14773 		r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
   14774 	      else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
   14775 		       || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
   14776 		r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
   14777 	      else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
   14778 		       || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
   14779 		r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
   14780 
   14781 	      if (skip)
   14782 		memset (&outrel, 0, sizeof outrel);
   14783 	      else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
   14784 		       && !is_opd
   14785 		       && r_type != R_PPC64_TOC)
   14786 		{
   14787 		  BFD_ASSERT (h->elf.dynindx != -1);
   14788 		  outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
   14789 		}
   14790 	      else
   14791 		{
   14792 		  /* This symbol is local, or marked to become local,
   14793 		     or this is an opd section reloc which must point
   14794 		     at a local function.  */
   14795 		  outrel.r_addend += relocation;
   14796 		  if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
   14797 		    {
   14798 		      if (is_opd && h != NULL)
   14799 			{
   14800 			  /* Lie about opd entries.  This case occurs
   14801 			     when building shared libraries and we
   14802 			     reference a function in another shared
   14803 			     lib.  The same thing happens for a weak
   14804 			     definition in an application that's
   14805 			     overridden by a strong definition in a
   14806 			     shared lib.  (I believe this is a generic
   14807 			     bug in binutils handling of weak syms.)
   14808 			     In these cases we won't use the opd
   14809 			     entry in this lib.  */
   14810 			  unresolved_reloc = FALSE;
   14811 			}
   14812 		      if (!is_opd
   14813 			  && r_type == R_PPC64_ADDR64
   14814 			  && (h != NULL
   14815 			      ? h->elf.type == STT_GNU_IFUNC
   14816 			      : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
   14817 			outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   14818 		      else
   14819 			{
   14820 			  outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
   14821 
   14822 			  /* We need to relocate .opd contents for ld.so.
   14823 			     Prelink also wants simple and consistent rules
   14824 			     for relocs.  This make all RELATIVE relocs have
   14825 			     *r_offset equal to r_addend.  */
   14826 			  relocate = TRUE;
   14827 			}
   14828 		    }
   14829 		  else
   14830 		    {
   14831 		      long indx = 0;
   14832 
   14833 		      if (h != NULL
   14834 			  ? h->elf.type == STT_GNU_IFUNC
   14835 			  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   14836 			{
   14837 			  info->callbacks->einfo
   14838 			    (_("%P: %H: %s for indirect "
   14839 			       "function `%T' unsupported\n"),
   14840 			     input_bfd, input_section, rel->r_offset,
   14841 			     ppc64_elf_howto_table[r_type]->name,
   14842 			     sym_name);
   14843 			  ret = FALSE;
   14844 			}
   14845 		      else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
   14846 			;
   14847 		      else if (sec == NULL || sec->owner == NULL)
   14848 			{
   14849 			  bfd_set_error (bfd_error_bad_value);
   14850 			  return FALSE;
   14851 			}
   14852 		      else
   14853 			{
   14854 			  asection *osec;
   14855 
   14856 			  osec = sec->output_section;
   14857 			  indx = elf_section_data (osec)->dynindx;
   14858 
   14859 			  if (indx == 0)
   14860 			    {
   14861 			      if ((osec->flags & SEC_READONLY) == 0
   14862 				  && htab->elf.data_index_section != NULL)
   14863 				osec = htab->elf.data_index_section;
   14864 			      else
   14865 				osec = htab->elf.text_index_section;
   14866 			      indx = elf_section_data (osec)->dynindx;
   14867 			    }
   14868 			  BFD_ASSERT (indx != 0);
   14869 
   14870 			  /* We are turning this relocation into one
   14871 			     against a section symbol, so subtract out
   14872 			     the output section's address but not the
   14873 			     offset of the input section in the output
   14874 			     section.  */
   14875 			  outrel.r_addend -= osec->vma;
   14876 			}
   14877 
   14878 		      outrel.r_info = ELF64_R_INFO (indx, r_type);
   14879 		    }
   14880 		}
   14881 
   14882 	      sreloc = elf_section_data (input_section)->sreloc;
   14883 	      if (h != NULL
   14884 		  ? h->elf.type == STT_GNU_IFUNC
   14885 		  : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
   14886 		sreloc = htab->elf.irelplt;
   14887 	      if (sreloc == NULL)
   14888 		abort ();
   14889 
   14890 	      if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
   14891 		  >= sreloc->size)
   14892 		abort ();
   14893 	      loc = sreloc->contents;
   14894 	      loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
   14895 	      bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
   14896 
   14897 	      /* If this reloc is against an external symbol, it will
   14898 		 be computed at runtime, so there's no need to do
   14899 		 anything now.  However, for the sake of prelink ensure
   14900 		 that the section contents are a known value.  */
   14901 	      if (! relocate)
   14902 		{
   14903 		  unresolved_reloc = FALSE;
   14904 		  /* The value chosen here is quite arbitrary as ld.so
   14905 		     ignores section contents except for the special
   14906 		     case of .opd where the contents might be accessed
   14907 		     before relocation.  Choose zero, as that won't
   14908 		     cause reloc overflow.  */
   14909 		  relocation = 0;
   14910 		  addend = 0;
   14911 		  /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
   14912 		     to improve backward compatibility with older
   14913 		     versions of ld.  */
   14914 		  if (r_type == R_PPC64_ADDR64)
   14915 		    addend = outrel.r_addend;
   14916 		  /* Adjust pc_relative relocs to have zero in *r_offset.  */
   14917 		  else if (ppc64_elf_howto_table[r_type]->pc_relative)
   14918 		    addend = (input_section->output_section->vma
   14919 			      + input_section->output_offset
   14920 			      + rel->r_offset);
   14921 		}
   14922 	    }
   14923 	  break;
   14924 
   14925 	case R_PPC64_COPY:
   14926 	case R_PPC64_GLOB_DAT:
   14927 	case R_PPC64_JMP_SLOT:
   14928 	case R_PPC64_JMP_IREL:
   14929 	case R_PPC64_RELATIVE:
   14930 	  /* We shouldn't ever see these dynamic relocs in relocatable
   14931 	     files.  */
   14932 	  /* Fall through.  */
   14933 
   14934 	case R_PPC64_PLTGOT16:
   14935 	case R_PPC64_PLTGOT16_DS:
   14936 	case R_PPC64_PLTGOT16_HA:
   14937 	case R_PPC64_PLTGOT16_HI:
   14938 	case R_PPC64_PLTGOT16_LO:
   14939 	case R_PPC64_PLTGOT16_LO_DS:
   14940 	case R_PPC64_PLTREL32:
   14941 	case R_PPC64_PLTREL64:
   14942 	  /* These ones haven't been implemented yet.  */
   14943 
   14944 	  info->callbacks->einfo
   14945 	    (_("%P: %B: %s is not supported for `%T'\n"),
   14946 	     input_bfd,
   14947 	     ppc64_elf_howto_table[r_type]->name, sym_name);
   14948 
   14949 	  bfd_set_error (bfd_error_invalid_operation);
   14950 	  ret = FALSE;
   14951 	  goto copy_reloc;
   14952 	}
   14953 
   14954       /* Multi-instruction sequences that access the TOC can be
   14955 	 optimized, eg. addis ra,r2,0; addi rb,ra,x;
   14956 	 to             nop;           addi rb,r2,x;  */
   14957       switch (r_type)
   14958 	{
   14959 	default:
   14960 	  break;
   14961 
   14962 	case R_PPC64_GOT_TLSLD16_HI:
   14963 	case R_PPC64_GOT_TLSGD16_HI:
   14964 	case R_PPC64_GOT_TPREL16_HI:
   14965 	case R_PPC64_GOT_DTPREL16_HI:
   14966 	case R_PPC64_GOT16_HI:
   14967 	case R_PPC64_TOC16_HI:
   14968 	  /* These relocs would only be useful if building up an
   14969 	     offset to later add to r2, perhaps in an indexed
   14970 	     addressing mode instruction.  Don't try to optimize.
   14971 	     Unfortunately, the possibility of someone building up an
   14972 	     offset like this or even with the HA relocs, means that
   14973 	     we need to check the high insn when optimizing the low
   14974 	     insn.  */
   14975 	  break;
   14976 
   14977 	case R_PPC64_GOT_TLSLD16_HA:
   14978 	case R_PPC64_GOT_TLSGD16_HA:
   14979 	case R_PPC64_GOT_TPREL16_HA:
   14980 	case R_PPC64_GOT_DTPREL16_HA:
   14981 	case R_PPC64_GOT16_HA:
   14982 	case R_PPC64_TOC16_HA:
   14983 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   14984 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   14985 	    {
   14986 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   14987 	      bfd_put_32 (input_bfd, NOP, p);
   14988 	    }
   14989 	  break;
   14990 
   14991 	case R_PPC64_GOT_TLSLD16_LO:
   14992 	case R_PPC64_GOT_TLSGD16_LO:
   14993 	case R_PPC64_GOT_TPREL16_LO_DS:
   14994 	case R_PPC64_GOT_DTPREL16_LO_DS:
   14995 	case R_PPC64_GOT16_LO:
   14996 	case R_PPC64_GOT16_LO_DS:
   14997 	case R_PPC64_TOC16_LO:
   14998 	case R_PPC64_TOC16_LO_DS:
   14999 	  if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
   15000 	      && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
   15001 	    {
   15002 	      bfd_byte *p = contents + (rel->r_offset & ~3);
   15003 	      insn = bfd_get_32 (input_bfd, p);
   15004 	      if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
   15005 		{
   15006 		  /* Transform addic to addi when we change reg.  */
   15007 		  insn &= ~((0x3f << 26) | (0x1f << 16));
   15008 		  insn |= (14u << 26) | (2 << 16);
   15009 		}
   15010 	      else
   15011 		{
   15012 		  insn &= ~(0x1f << 16);
   15013 		  insn |= 2 << 16;
   15014 		}
   15015 	      bfd_put_32 (input_bfd, insn, p);
   15016 	    }
   15017 	  break;
   15018 	}
   15019 
   15020       /* Do any further special processing.  */
   15021       howto = ppc64_elf_howto_table[(int) r_type];
   15022       switch (r_type)
   15023 	{
   15024 	default:
   15025 	  break;
   15026 
   15027 	case R_PPC64_REL16_HA:
   15028 	case R_PPC64_REL16DX_HA:
   15029 	case R_PPC64_ADDR16_HA:
   15030 	case R_PPC64_ADDR16_HIGHA:
   15031 	case R_PPC64_ADDR16_HIGHERA:
   15032 	case R_PPC64_ADDR16_HIGHESTA:
   15033 	case R_PPC64_TOC16_HA:
   15034 	case R_PPC64_SECTOFF_HA:
   15035 	case R_PPC64_TPREL16_HA:
   15036 	case R_PPC64_TPREL16_HIGHA:
   15037 	case R_PPC64_TPREL16_HIGHERA:
   15038 	case R_PPC64_TPREL16_HIGHESTA:
   15039 	case R_PPC64_DTPREL16_HA:
   15040 	case R_PPC64_DTPREL16_HIGHA:
   15041 	case R_PPC64_DTPREL16_HIGHERA:
   15042 	case R_PPC64_DTPREL16_HIGHESTA:
   15043 	  /* It's just possible that this symbol is a weak symbol
   15044 	     that's not actually defined anywhere. In that case,
   15045 	     'sec' would be NULL, and we should leave the symbol
   15046 	     alone (it will be set to zero elsewhere in the link).  */
   15047 	  if (sec == NULL)
   15048 	    break;
   15049 	  /* Fall thru */
   15050 
   15051 	case R_PPC64_GOT16_HA:
   15052 	case R_PPC64_PLTGOT16_HA:
   15053 	case R_PPC64_PLT16_HA:
   15054 	case R_PPC64_GOT_TLSGD16_HA:
   15055 	case R_PPC64_GOT_TLSLD16_HA:
   15056 	case R_PPC64_GOT_TPREL16_HA:
   15057 	case R_PPC64_GOT_DTPREL16_HA:
   15058 	  /* Add 0x10000 if sign bit in 0:15 is set.
   15059 	     Bits 0:15 are not used.  */
   15060 	  addend += 0x8000;
   15061 	  break;
   15062 
   15063 	case R_PPC64_ADDR16_DS:
   15064 	case R_PPC64_ADDR16_LO_DS:
   15065 	case R_PPC64_GOT16_DS:
   15066 	case R_PPC64_GOT16_LO_DS:
   15067 	case R_PPC64_PLT16_LO_DS:
   15068 	case R_PPC64_SECTOFF_DS:
   15069 	case R_PPC64_SECTOFF_LO_DS:
   15070 	case R_PPC64_TOC16_DS:
   15071 	case R_PPC64_TOC16_LO_DS:
   15072 	case R_PPC64_PLTGOT16_DS:
   15073 	case R_PPC64_PLTGOT16_LO_DS:
   15074 	case R_PPC64_GOT_TPREL16_DS:
   15075 	case R_PPC64_GOT_TPREL16_LO_DS:
   15076 	case R_PPC64_GOT_DTPREL16_DS:
   15077 	case R_PPC64_GOT_DTPREL16_LO_DS:
   15078 	case R_PPC64_TPREL16_DS:
   15079 	case R_PPC64_TPREL16_LO_DS:
   15080 	case R_PPC64_DTPREL16_DS:
   15081 	case R_PPC64_DTPREL16_LO_DS:
   15082 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   15083 	  mask = 3;
   15084 	  /* If this reloc is against an lq, lxv, or stxv insn, then
   15085 	     the value must be a multiple of 16.  This is somewhat of
   15086 	     a hack, but the "correct" way to do this by defining _DQ
   15087 	     forms of all the _DS relocs bloats all reloc switches in
   15088 	     this file.  It doesn't make much sense to use these
   15089 	     relocs in data, so testing the insn should be safe.  */
   15090 	  if ((insn & (0x3f << 26)) == (56u << 26)
   15091 	      || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
   15092 	    mask = 15;
   15093 	  relocation += addend;
   15094 	  addend = insn & (mask ^ 3);
   15095 	  if ((relocation & mask) != 0)
   15096 	    {
   15097 	      relocation ^= relocation & mask;
   15098 	      info->callbacks->einfo
   15099 		(_("%P: %H: error: %s not a multiple of %u\n"),
   15100 		 input_bfd, input_section, rel->r_offset,
   15101 		 howto->name,
   15102 		 mask + 1);
   15103 	      bfd_set_error (bfd_error_bad_value);
   15104 	      ret = FALSE;
   15105 	      goto copy_reloc;
   15106 	    }
   15107 	  break;
   15108 	}
   15109 
   15110       /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
   15111 	 because such sections are not SEC_ALLOC and thus ld.so will
   15112 	 not process them.  */
   15113       if (unresolved_reloc
   15114 	  && !((input_section->flags & SEC_DEBUGGING) != 0
   15115 	       && h->elf.def_dynamic)
   15116 	  && _bfd_elf_section_offset (output_bfd, info, input_section,
   15117 				      rel->r_offset) != (bfd_vma) -1)
   15118 	{
   15119 	  info->callbacks->einfo
   15120 	    (_("%P: %H: unresolvable %s against `%T'\n"),
   15121 	     input_bfd, input_section, rel->r_offset,
   15122 	     howto->name,
   15123 	     h->elf.root.root.string);
   15124 	  ret = FALSE;
   15125 	}
   15126 
   15127       /* 16-bit fields in insns mostly have signed values, but a
   15128 	 few insns have 16-bit unsigned values.  Really, we should
   15129 	 have different reloc types.  */
   15130       if (howto->complain_on_overflow != complain_overflow_dont
   15131 	  && howto->dst_mask == 0xffff
   15132 	  && (input_section->flags & SEC_CODE) != 0)
   15133 	{
   15134 	  enum complain_overflow complain = complain_overflow_signed;
   15135 
   15136 	  insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
   15137 	  if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
   15138 	    complain = complain_overflow_bitfield;
   15139 	  else if (howto->rightshift == 0
   15140 		   ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
   15141 		      || (insn & (0x3f << 26)) == 24u << 26 /* ori */
   15142 		      || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
   15143 		   : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
   15144 		      || (insn & (0x3f << 26)) == 25u << 26 /* oris */
   15145 		      || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
   15146 	    complain = complain_overflow_unsigned;
   15147 	  if (howto->complain_on_overflow != complain)
   15148 	    {
   15149 	      alt_howto = *howto;
   15150 	      alt_howto.complain_on_overflow = complain;
   15151 	      howto = &alt_howto;
   15152 	    }
   15153 	}
   15154 
   15155       if (r_type == R_PPC64_REL16DX_HA)
   15156 	{
   15157 	  /* Split field reloc isn't handled by _bfd_final_link_relocate.  */
   15158 	  if (rel->r_offset + 4 > input_section->size)
   15159 	    r = bfd_reloc_outofrange;
   15160 	  else
   15161 	    {
   15162 	      relocation += addend;
   15163 	      relocation -= (rel->r_offset
   15164 			     + input_section->output_offset
   15165 			     + input_section->output_section->vma);
   15166 	      relocation = (bfd_signed_vma) relocation >> 16;
   15167 	      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
   15168 	      insn &= ~0x1fffc1;
   15169 	      insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
   15170 	      bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
   15171 	      r = bfd_reloc_ok;
   15172 	      if (relocation + 0x8000 > 0xffff)
   15173 		r = bfd_reloc_overflow;
   15174 	    }
   15175 	}
   15176       else
   15177 	r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
   15178 				      rel->r_offset, relocation, addend);
   15179 
   15180       if (r != bfd_reloc_ok)
   15181 	{
   15182 	  char *more_info = NULL;
   15183 	  const char *reloc_name = howto->name;
   15184 
   15185 	  if (reloc_dest != DEST_NORMAL)
   15186 	    {
   15187 	      more_info = bfd_malloc (strlen (reloc_name) + 8);
   15188 	      if (more_info != NULL)
   15189 		{
   15190 		  strcpy (more_info, reloc_name);
   15191 		  strcat (more_info, (reloc_dest == DEST_OPD
   15192 				      ? " (OPD)" : " (stub)"));
   15193 		  reloc_name = more_info;
   15194 		}
   15195 	    }
   15196 
   15197 	  if (r == bfd_reloc_overflow)
   15198 	    {
   15199 	      /* On code like "if (foo) foo();" don't report overflow
   15200 		 on a branch to zero when foo is undefined.  */
   15201 	      if (!warned
   15202 		  && (reloc_dest == DEST_STUB
   15203 		      || !(h != NULL
   15204 			   && (h->elf.root.type == bfd_link_hash_undefweak
   15205 			       || h->elf.root.type == bfd_link_hash_undefined)
   15206 			   && is_branch_reloc (r_type))))
   15207 		info->callbacks->reloc_overflow (info, &h->elf.root,
   15208 						 sym_name, reloc_name,
   15209 						 orig_rel.r_addend,
   15210 						 input_bfd, input_section,
   15211 						 rel->r_offset);
   15212 	    }
   15213 	  else
   15214 	    {
   15215 	      info->callbacks->einfo
   15216 		(_("%P: %H: %s against `%T': error %d\n"),
   15217 		 input_bfd, input_section, rel->r_offset,
   15218 		 reloc_name, sym_name, (int) r);
   15219 	      ret = FALSE;
   15220 	    }
   15221 	  if (more_info != NULL)
   15222 	    free (more_info);
   15223 	}
   15224     copy_reloc:
   15225       if (wrel != rel)
   15226 	*wrel = *rel;
   15227     }
   15228 
   15229   if (wrel != rel)
   15230     {
   15231       Elf_Internal_Shdr *rel_hdr;
   15232       size_t deleted = rel - wrel;
   15233 
   15234       rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
   15235       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   15236       if (rel_hdr->sh_size == 0)
   15237 	{
   15238 	  /* It is too late to remove an empty reloc section.  Leave
   15239 	     one NONE reloc.
   15240 	     ??? What is wrong with an empty section???  */
   15241 	  rel_hdr->sh_size = rel_hdr->sh_entsize;
   15242 	  deleted -= 1;
   15243 	}
   15244       rel_hdr = _bfd_elf_single_rel_hdr (input_section);
   15245       rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
   15246       input_section->reloc_count -= deleted;
   15247     }
   15248 
   15249   /* If we're emitting relocations, then shortly after this function
   15250      returns, reloc offsets and addends for this section will be
   15251      adjusted.  Worse, reloc symbol indices will be for the output
   15252      file rather than the input.  Save a copy of the relocs for
   15253      opd_entry_value.  */
   15254   if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
   15255     {
   15256       bfd_size_type amt;
   15257       amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
   15258       rel = bfd_alloc (input_bfd, amt);
   15259       BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
   15260       ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
   15261       if (rel == NULL)
   15262 	return FALSE;
   15263       memcpy (rel, relocs, amt);
   15264     }
   15265   return ret;
   15266 }
   15267 
   15268 /* Adjust the value of any local symbols in opd sections.  */
   15269 
   15270 static int
   15271 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
   15272 			      const char *name ATTRIBUTE_UNUSED,
   15273 			      Elf_Internal_Sym *elfsym,
   15274 			      asection *input_sec,
   15275 			      struct elf_link_hash_entry *h)
   15276 {
   15277   struct _opd_sec_data *opd;
   15278   long adjust;
   15279   bfd_vma value;
   15280 
   15281   if (h != NULL)
   15282     return 1;
   15283 
   15284   opd = get_opd_info (input_sec);
   15285   if (opd == NULL || opd->adjust == NULL)
   15286     return 1;
   15287 
   15288   value = elfsym->st_value - input_sec->output_offset;
   15289   if (!bfd_link_relocatable (info))
   15290     value -= input_sec->output_section->vma;
   15291 
   15292   adjust = opd->adjust[OPD_NDX (value)];
   15293   if (adjust == -1)
   15294     return 2;
   15295 
   15296   elfsym->st_value += adjust;
   15297   return 1;
   15298 }
   15299 
   15300 /* Finish up dynamic symbol handling.  We set the contents of various
   15301    dynamic sections here.  */
   15302 
   15303 static bfd_boolean
   15304 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
   15305 				 struct bfd_link_info *info,
   15306 				 struct elf_link_hash_entry *h,
   15307 				 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
   15308 {
   15309   struct ppc_link_hash_table *htab;
   15310   struct plt_entry *ent;
   15311   Elf_Internal_Rela rela;
   15312   bfd_byte *loc;
   15313 
   15314   htab = ppc_hash_table (info);
   15315   if (htab == NULL)
   15316     return FALSE;
   15317 
   15318   for (ent = h->plt.plist; ent != NULL; ent = ent->next)
   15319     if (ent->plt.offset != (bfd_vma) -1)
   15320       {
   15321 	/* This symbol has an entry in the procedure linkage
   15322 	   table.  Set it up.  */
   15323 	if (!htab->elf.dynamic_sections_created
   15324 	    || h->dynindx == -1)
   15325 	  {
   15326 	    BFD_ASSERT (h->type == STT_GNU_IFUNC
   15327 			&& h->def_regular
   15328 			&& (h->root.type == bfd_link_hash_defined
   15329 			    || h->root.type == bfd_link_hash_defweak));
   15330 	    rela.r_offset = (htab->elf.iplt->output_section->vma
   15331 			     + htab->elf.iplt->output_offset
   15332 			     + ent->plt.offset);
   15333 	    if (htab->opd_abi)
   15334 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
   15335 	    else
   15336 	      rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
   15337 	    rela.r_addend = (h->root.u.def.value
   15338 			     + h->root.u.def.section->output_offset
   15339 			     + h->root.u.def.section->output_section->vma
   15340 			     + ent->addend);
   15341 	    loc = (htab->elf.irelplt->contents
   15342 		   + (htab->elf.irelplt->reloc_count++
   15343 		      * sizeof (Elf64_External_Rela)));
   15344 	  }
   15345 	else
   15346 	  {
   15347 	    rela.r_offset = (htab->elf.splt->output_section->vma
   15348 			     + htab->elf.splt->output_offset
   15349 			     + ent->plt.offset);
   15350 	    rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
   15351 	    rela.r_addend = ent->addend;
   15352 	    loc = (htab->elf.srelplt->contents
   15353 		   + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
   15354 		      / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
   15355 	  }
   15356 	bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   15357 
   15358 	if (!htab->opd_abi)
   15359 	  {
   15360 	    if (!h->def_regular)
   15361 	      {
   15362 		/* Mark the symbol as undefined, rather than as
   15363 		   defined in glink.  Leave the value if there were
   15364 		   any relocations where pointer equality matters
   15365 		   (this is a clue for the dynamic linker, to make
   15366 		   function pointer comparisons work between an
   15367 		   application and shared library), otherwise set it
   15368 		   to zero.  */
   15369 		sym->st_shndx = SHN_UNDEF;
   15370 		if (!h->pointer_equality_needed)
   15371 		  sym->st_value = 0;
   15372 		else if (!h->ref_regular_nonweak)
   15373 		  {
   15374 		    /* This breaks function pointer comparisons, but
   15375 		       that is better than breaking tests for a NULL
   15376 		       function pointer.  */
   15377 		    sym->st_value = 0;
   15378 		  }
   15379 	      }
   15380 	  }
   15381       }
   15382 
   15383   if (h->needs_copy)
   15384     {
   15385       /* This symbol needs a copy reloc.  Set it up.  */
   15386 
   15387       if (h->dynindx == -1
   15388 	  || (h->root.type != bfd_link_hash_defined
   15389 	      && h->root.type != bfd_link_hash_defweak)
   15390 	  || htab->relbss == NULL)
   15391 	abort ();
   15392 
   15393       rela.r_offset = (h->root.u.def.value
   15394 		       + h->root.u.def.section->output_section->vma
   15395 		       + h->root.u.def.section->output_offset);
   15396       rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
   15397       rela.r_addend = 0;
   15398       loc = htab->relbss->contents;
   15399       loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
   15400       bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
   15401     }
   15402 
   15403   return TRUE;
   15404 }
   15405 
   15406 /* Used to decide how to sort relocs in an optimal manner for the
   15407    dynamic linker, before writing them out.  */
   15408 
   15409 static enum elf_reloc_type_class
   15410 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
   15411 			    const asection *rel_sec,
   15412 			    const Elf_Internal_Rela *rela)
   15413 {
   15414   enum elf_ppc64_reloc_type r_type;
   15415   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   15416 
   15417   if (rel_sec == htab->elf.irelplt)
   15418     return reloc_class_ifunc;
   15419 
   15420   r_type = ELF64_R_TYPE (rela->r_info);
   15421   switch (r_type)
   15422     {
   15423     case R_PPC64_RELATIVE:
   15424       return reloc_class_relative;
   15425     case R_PPC64_JMP_SLOT:
   15426       return reloc_class_plt;
   15427     case R_PPC64_COPY:
   15428       return reloc_class_copy;
   15429     default:
   15430       return reloc_class_normal;
   15431     }
   15432 }
   15433 
   15434 /* Finish up the dynamic sections.  */
   15435 
   15436 static bfd_boolean
   15437 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
   15438 				   struct bfd_link_info *info)
   15439 {
   15440   struct ppc_link_hash_table *htab;
   15441   bfd *dynobj;
   15442   asection *sdyn;
   15443 
   15444   htab = ppc_hash_table (info);
   15445   if (htab == NULL)
   15446     return FALSE;
   15447 
   15448   dynobj = htab->elf.dynobj;
   15449   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
   15450 
   15451   if (htab->elf.dynamic_sections_created)
   15452     {
   15453       Elf64_External_Dyn *dyncon, *dynconend;
   15454 
   15455       if (sdyn == NULL || htab->elf.sgot == NULL)
   15456 	abort ();
   15457 
   15458       dyncon = (Elf64_External_Dyn *) sdyn->contents;
   15459       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
   15460       for (; dyncon < dynconend; dyncon++)
   15461 	{
   15462 	  Elf_Internal_Dyn dyn;
   15463 	  asection *s;
   15464 
   15465 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
   15466 
   15467 	  switch (dyn.d_tag)
   15468 	    {
   15469 	    default:
   15470 	      continue;
   15471 
   15472 	    case DT_PPC64_GLINK:
   15473 	      s = htab->glink;
   15474 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   15475 	      /* We stupidly defined DT_PPC64_GLINK to be the start
   15476 		 of glink rather than the first entry point, which is
   15477 		 what ld.so needs, and now have a bigger stub to
   15478 		 support automatic multiple TOCs.  */
   15479 	      dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
   15480 	      break;
   15481 
   15482 	    case DT_PPC64_OPD:
   15483 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   15484 	      if (s == NULL)
   15485 		continue;
   15486 	      dyn.d_un.d_ptr = s->vma;
   15487 	      break;
   15488 
   15489 	    case DT_PPC64_OPT:
   15490 	      if (htab->do_multi_toc && htab->multi_toc_needed)
   15491 		dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
   15492 	      break;
   15493 
   15494 	    case DT_PPC64_OPDSZ:
   15495 	      s = bfd_get_section_by_name (output_bfd, ".opd");
   15496 	      if (s == NULL)
   15497 		continue;
   15498 	      dyn.d_un.d_val = s->size;
   15499 	      break;
   15500 
   15501 	    case DT_PLTGOT:
   15502 	      s = htab->elf.splt;
   15503 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   15504 	      break;
   15505 
   15506 	    case DT_JMPREL:
   15507 	      s = htab->elf.srelplt;
   15508 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
   15509 	      break;
   15510 
   15511 	    case DT_PLTRELSZ:
   15512 	      dyn.d_un.d_val = htab->elf.srelplt->size;
   15513 	      break;
   15514 
   15515 	    case DT_RELASZ:
   15516 	      /* Don't count procedure linkage table relocs in the
   15517 		 overall reloc count.  */
   15518 	      s = htab->elf.srelplt;
   15519 	      if (s == NULL)
   15520 		continue;
   15521 	      dyn.d_un.d_val -= s->size;
   15522 	      break;
   15523 
   15524 	    case DT_RELA:
   15525 	      /* We may not be using the standard ELF linker script.
   15526 		 If .rela.plt is the first .rela section, we adjust
   15527 		 DT_RELA to not include it.  */
   15528 	      s = htab->elf.srelplt;
   15529 	      if (s == NULL)
   15530 		continue;
   15531 	      if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
   15532 		continue;
   15533 	      dyn.d_un.d_ptr += s->size;
   15534 	      break;
   15535 	    }
   15536 
   15537 	  bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
   15538 	}
   15539     }
   15540 
   15541   if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
   15542       && htab->elf.sgot->output_section != bfd_abs_section_ptr)
   15543     {
   15544       /* Fill in the first entry in the global offset table.
   15545 	 We use it to hold the link-time TOCbase.  */
   15546       bfd_put_64 (output_bfd,
   15547 		  elf_gp (output_bfd) + TOC_BASE_OFF,
   15548 		  htab->elf.sgot->contents);
   15549 
   15550       /* Set .got entry size.  */
   15551       elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
   15552     }
   15553 
   15554   if (htab->elf.splt != NULL && htab->elf.splt->size != 0
   15555       && htab->elf.splt->output_section != bfd_abs_section_ptr)
   15556     {
   15557       /* Set .plt entry size.  */
   15558       elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
   15559 	= PLT_ENTRY_SIZE (htab);
   15560     }
   15561 
   15562   /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
   15563      brlt ourselves if emitrelocations.  */
   15564   if (htab->brlt != NULL
   15565       && htab->brlt->reloc_count != 0
   15566       && !_bfd_elf_link_output_relocs (output_bfd,
   15567 				       htab->brlt,
   15568 				       elf_section_data (htab->brlt)->rela.hdr,
   15569 				       elf_section_data (htab->brlt)->relocs,
   15570 				       NULL))
   15571     return FALSE;
   15572 
   15573   if (htab->glink != NULL
   15574       && htab->glink->reloc_count != 0
   15575       && !_bfd_elf_link_output_relocs (output_bfd,
   15576 				       htab->glink,
   15577 				       elf_section_data (htab->glink)->rela.hdr,
   15578 				       elf_section_data (htab->glink)->relocs,
   15579 				       NULL))
   15580     return FALSE;
   15581 
   15582   if (htab->glink_eh_frame != NULL
   15583       && htab->glink_eh_frame->size != 0)
   15584     {
   15585       bfd_vma val;
   15586       bfd_byte *p;
   15587       asection *stub_sec;
   15588 
   15589       p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
   15590       for (stub_sec = htab->params->stub_bfd->sections;
   15591 	   stub_sec != NULL;
   15592 	   stub_sec = stub_sec->next)
   15593 	if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
   15594 	  {
   15595 	    /* FDE length.  */
   15596 	    p += 4;
   15597 	    /* CIE pointer.  */
   15598 	    p += 4;
   15599 	    /* Offset to stub section.  */
   15600 	    val = (stub_sec->output_section->vma
   15601 		   + stub_sec->output_offset);
   15602 	    val -= (htab->glink_eh_frame->output_section->vma
   15603 		    + htab->glink_eh_frame->output_offset
   15604 		    + (p - htab->glink_eh_frame->contents));
   15605 	    if (val + 0x80000000 > 0xffffffff)
   15606 	      {
   15607 		info->callbacks->einfo
   15608 		  (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
   15609 		   stub_sec->name);
   15610 		return FALSE;
   15611 	      }
   15612 	    bfd_put_32 (dynobj, val, p);
   15613 	    p += 4;
   15614 	    /* stub section size.  */
   15615 	    p += 4;
   15616 	    /* Augmentation.  */
   15617 	    p += 1;
   15618 	    /* Pad.  */
   15619 	    p += 7;
   15620 	  }
   15621       if (htab->glink != NULL && htab->glink->size != 0)
   15622 	{
   15623 	  /* FDE length.  */
   15624 	  p += 4;
   15625 	  /* CIE pointer.  */
   15626 	  p += 4;
   15627 	  /* Offset to .glink.  */
   15628 	  val = (htab->glink->output_section->vma
   15629 		 + htab->glink->output_offset
   15630 		 + 8);
   15631 	  val -= (htab->glink_eh_frame->output_section->vma
   15632 		  + htab->glink_eh_frame->output_offset
   15633 		  + (p - htab->glink_eh_frame->contents));
   15634 	  if (val + 0x80000000 > 0xffffffff)
   15635 	    {
   15636 	      info->callbacks->einfo
   15637 		(_("%P: %s offset too large for .eh_frame sdata4 encoding"),
   15638 		 htab->glink->name);
   15639 	      return FALSE;
   15640 	    }
   15641 	  bfd_put_32 (dynobj, val, p);
   15642 	  p += 4;
   15643 	  /* .glink size.  */
   15644 	  p += 4;
   15645 	  /* Augmentation.  */
   15646 	  p += 1;
   15647 	  /* Ops.  */
   15648 	  p += 7;
   15649 	}
   15650 
   15651       if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
   15652 	  && !_bfd_elf_write_section_eh_frame (output_bfd, info,
   15653 					       htab->glink_eh_frame,
   15654 					       htab->glink_eh_frame->contents))
   15655 	return FALSE;
   15656     }
   15657 
   15658   /* We need to handle writing out multiple GOT sections ourselves,
   15659      since we didn't add them to DYNOBJ.  We know dynobj is the first
   15660      bfd.  */
   15661   while ((dynobj = dynobj->link.next) != NULL)
   15662     {
   15663       asection *s;
   15664 
   15665       if (!is_ppc64_elf (dynobj))
   15666 	continue;
   15667 
   15668       s = ppc64_elf_tdata (dynobj)->got;
   15669       if (s != NULL
   15670 	  && s->size != 0
   15671 	  && s->output_section != bfd_abs_section_ptr
   15672 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   15673 					s->contents, s->output_offset,
   15674 					s->size))
   15675 	return FALSE;
   15676       s = ppc64_elf_tdata (dynobj)->relgot;
   15677       if (s != NULL
   15678 	  && s->size != 0
   15679 	  && s->output_section != bfd_abs_section_ptr
   15680 	  && !bfd_set_section_contents (output_bfd, s->output_section,
   15681 					s->contents, s->output_offset,
   15682 					s->size))
   15683 	return FALSE;
   15684     }
   15685 
   15686   return TRUE;
   15687 }
   15688 
   15689 #include "elf64-target.h"
   15690 
   15691 /* FreeBSD support */
   15692 
   15693 #undef  TARGET_LITTLE_SYM
   15694 #undef  TARGET_LITTLE_NAME
   15695 
   15696 #undef  TARGET_BIG_SYM
   15697 #define TARGET_BIG_SYM	powerpc_elf64_fbsd_vec
   15698 #undef  TARGET_BIG_NAME
   15699 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
   15700 
   15701 #undef  ELF_OSABI
   15702 #define	ELF_OSABI       ELFOSABI_FREEBSD
   15703 
   15704 #undef  elf64_bed
   15705 #define elf64_bed	elf64_powerpc_fbsd_bed
   15706 
   15707 #include "elf64-target.h"
   15708 
   15709